summaryrefslogtreecommitdiffstats
path: root/changes
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2004-03-01 18:22:58 (GMT)
committerdgp <dgp@users.sourceforge.net>2004-03-01 18:22:58 (GMT)
commite1afee6bb276d510ee7b22252af4662652500d75 (patch)
treef0edd0014e3faeba1746e37633dcecbd4a3fa394 /changes
parentfa9272d4b37328eddeeff3f647a4eecfedcc8c53 (diff)
downloadtcl-e1afee6bb276d510ee7b22252af4662652500d75.zip
tcl-e1afee6bb276d510ee7b22252af4662652500d75.tar.gz
tcl-e1afee6bb276d510ee7b22252af4662652500d75.tar.bz2
updated release note files for 8.4.6core_8_4_6
Diffstat (limited to 'changes')
-rw-r--r--changes14
1 files changed, 12 insertions, 2 deletions
diff --git a/changes b/changes
index 6c40281..b49c76a 100644
--- a/changes
+++ b/changes
@@ -1,6 +1,6 @@
Recent user-visible changes to Tcl:
-RCS: @(#) $Id: changes,v 1.79.2.6 2004/02/20 18:35:48 dgp Exp $
+RCS: @(#) $Id: changes,v 1.79.2.7 2004/03/01 18:22:59 dgp Exp $
1. No more [command1] [command2] construct for grouping multiple
commands on a single command line.
@@ -5952,4 +5952,14 @@ various odd regexp "can't happen" bugs.
2004-02-17 (new default) tcltest::configure -verbose {body error}
---- Released 8.4.6, February 27, 2004 --- See ChangeLog for details ---
+2004-02-19 (bug fix) init.tcl search path with unusual --libdir (samson)
+
+2004-02-25 (bug fix)[554068] stopped broken [exec] quoting of { (gravereaux)
+
+2004-02-25 (bug fix)[888777] plugged memory leak with long host names (cassoff)
+
+2004-03-01 (bug fix)[462580] corrected level interpretation of Tcl_CreateTrace
+
+2004-03-01 (platform support)[218561] Allow 64-bit configure on IRIX64-6.5*
+
+--- Released 8.4.6, March 1, 2004 --- See ChangeLog for details ---
ss='rem' style='width: 10.6%;'/> -rw-r--r--Modules/nismodule.c566
-rw-r--r--Modules/operator.c750
-rw-r--r--Modules/ossaudiodev.c26
-rw-r--r--Modules/parsermodule.c214
-rw-r--r--Modules/pwdmodule.c224
-rw-r--r--Modules/pyexpat.c390
-rw-r--r--Modules/python.c246
-rw-r--r--Modules/readline.c970
-rw-r--r--Modules/resource.c388
-rw-r--r--Modules/rotatingtree.c160
-rw-r--r--Modules/selectmodule.c2476
-rw-r--r--Modules/sha1module.c78
-rw-r--r--Modules/sha256module.c126
-rw-r--r--Modules/sha512module.c146
-rw-r--r--Modules/signalmodule.c860
-rw-r--r--Modules/socketmodule.c6006
-rw-r--r--Modules/socketmodule.h84
-rw-r--r--Modules/spwdmodule.c196
-rw-r--r--Modules/symtablemodule.c148
-rw-r--r--Modules/syslogmodule.c290
-rw-r--r--Modules/termios.c904
-rw-r--r--Modules/testcapi_long.h348
-rw-r--r--Modules/timemodule.c1274
-rw-r--r--Modules/tkappinit.c192
-rw-r--r--Modules/unicodedata.c286
-rw-r--r--Modules/xxmodule.c520
-rw-r--r--Modules/xxsubtype.c410
-rw-r--r--Modules/zipimport.c1736
-rw-r--r--Modules/zlibmodule.c462
-rw-r--r--Objects/abstract.c3730
-rw-r--r--Objects/boolobject.c226
-rw-r--r--Objects/bytes_methods.c314
-rw-r--r--Objects/bytesobject.c5186
-rw-r--r--Objects/cellobject.c240
-rw-r--r--Objects/classobject.c888
-rw-r--r--Objects/codeobject.c954
-rw-r--r--Objects/complexobject.c1640
-rw-r--r--Objects/descrobject.c1866
-rw-r--r--Objects/dictobject.c4070
-rw-r--r--Objects/enumobject.c566
-rw-r--r--Objects/exceptions.c46
-rw-r--r--Objects/fileobject.c744
-rw-r--r--Objects/floatobject.c3710
-rw-r--r--Objects/frameobject.c1438
-rw-r--r--Objects/funcobject.c1324
-rw-r--r--Objects/genobject.c596
-rw-r--r--Objects/iterobject.c328
-rw-r--r--Objects/listobject.c4206
-rw-r--r--Objects/longobject.c6668
-rw-r--r--Objects/methodobject.c396
-rw-r--r--Objects/moduleobject.c632
-rw-r--r--Objects/object.c2442
-rw-r--r--Objects/obmalloc.c2064
-rw-r--r--Objects/rangeobject.c254
-rw-r--r--Objects/setobject.c3578
-rw-r--r--Objects/sliceobject.c518
-rw-r--r--Objects/stringlib/eq.h22
-rw-r--r--Objects/stringlib/partition.h46
-rw-r--r--Objects/stringlib/string_format.h174
-rw-r--r--Objects/stringlib/transmogrify.h84
-rw-r--r--Objects/structseq.c822
-rw-r--r--Objects/tupleobject.c1472
-rw-r--r--Objects/typeobject.c9558
-rw-r--r--Objects/unicodectype.c116
-rw-r--r--Objects/weakrefobject.c116
-rw-r--r--PC/VS7.1/make_buildinfo.c126
-rw-r--r--PC/VS8.0/make_buildinfo.c128
-rw-r--r--PC/_msi.c538
-rw-r--r--PC/_subprocess.c792
-rw-r--r--PC/bdist_wininst/archive.h94
-rw-r--r--PC/bdist_wininst/extract.c534
-rw-r--r--PC/bdist_wininst/install.c4214
-rw-r--r--PC/config.c134
-rw-r--r--PC/dl_nt.c90
-rw-r--r--PC/errmap.h148
-rw-r--r--PC/example_nt/example.c30
-rw-r--r--PC/frozen_dllmain.c116
-rw-r--r--PC/generrmap.c24
-rw-r--r--PC/getpathp.c910
-rw-r--r--PC/import_nt.c122
-rw-r--r--PC/make_versioninfo.c26
-rwxr-xr-xPC/msvcrtmodule.c456
-rw-r--r--PC/os2emx/config.c110
-rw-r--r--PC/os2emx/dlfcn.c286
-rw-r--r--PC/os2emx/dllentry.c44
-rw-r--r--PC/os2emx/getpathp.c480
-rw-r--r--PC/os2emx/pythonpm.c144
-rw-r--r--PC/os2vacpp/getpathp.c636
-rw-r--r--PC/winreg.c1940
-rw-r--r--PC/winsound.c138
-rw-r--r--PCbuild/make_buildinfo.c128
-rw-r--r--Parser/acceler.c168
-rw-r--r--Parser/bitset.c58
-rw-r--r--Parser/firstsets.c180
-rw-r--r--Parser/grammar.c380
-rw-r--r--Parser/grammar1.c70
-rw-r--r--Parser/intrcheck.c70
-rw-r--r--Parser/listnode.c78
-rw-r--r--Parser/metagrammar.c170
-rw-r--r--Parser/myreadline.c240
-rw-r--r--Parser/node.c122
-rw-r--r--Parser/parser.c540
-rw-r--r--Parser/parsetok.c348
-rw-r--r--Parser/pgen.c964
-rw-r--r--Parser/pgenmain.c194
-rw-r--r--Parser/printgrammar.c152
-rw-r--r--Parser/tokenizer.c2676
-rw-r--r--Parser/tokenizer.h88
-rw-r--r--Python/_warnings.c80
-rw-r--r--Python/asdl.c88
-rw-r--r--Python/ast.c188
-rw-r--r--Python/bltinmodule.c3258
-rw-r--r--Python/ceval.c6980
-rw-r--r--Python/codecs.c1044
-rw-r--r--Python/compile.c6496
-rw-r--r--Python/dynload_aix.c280
-rw-r--r--Python/dynload_atheos.c62
-rw-r--r--Python/dynload_hpux.c74
-rw-r--r--Python/dynload_next.c150
-rw-r--r--Python/dynload_os2.c58
-rw-r--r--Python/dynload_shlib.c152
-rw-r--r--Python/dynload_win.c420
-rw-r--r--Python/errors.c1244
-rw-r--r--Python/frozen.c28
-rw-r--r--Python/frozenmain.c154
-rw-r--r--Python/future.c232
-rw-r--r--Python/getargs.c3284
-rw-r--r--Python/getcwd.c78
-rw-r--r--Python/getopt.c150
-rw-r--r--Python/import.c5010
-rw-r--r--Python/importdl.c116
-rw-r--r--Python/importdl.h28
-rwxr-xr-xPython/makeopcodetargets.py2
-rw-r--r--Python/marshal.c2138
-rw-r--r--Python/modsupport.c964
-rw-r--r--Python/mysnprintf.c112
-rw-r--r--Python/mystrtoul.c440
-rw-r--r--Python/opcode_targets.h512
-rw-r--r--Python/peephole.c1196
-rw-r--r--Python/pyarena.c300
-rw-r--r--Python/pymath.c276
-rw-r--r--Python/pystate.c776
-rw-r--r--Python/pystrcmp.c24
-rw-r--r--Python/pystrtod.c1966
-rw-r--r--Python/pythonrun.c3284
-rw-r--r--Python/structmember.c548
-rw-r--r--Python/symtable.c2628
-rw-r--r--Python/sysmodule.c1852
-rw-r--r--Python/thread.c264
-rw-r--r--Python/thread_atheos.h270
-rw-r--r--Python/thread_cthread.h136
-rw-r--r--Python/thread_foobar.h70
-rw-r--r--Python/thread_lwp.h150
-rw-r--r--Python/thread_nt.h334
-rw-r--r--Python/thread_os2.h316
-rw-r--r--Python/thread_pth.h218
-rw-r--r--Python/thread_pthread.h474
-rw-r--r--Python/thread_sgi.h480
-rw-r--r--Python/thread_solaris.h186
-rw-r--r--Python/thread_wince.h96
-rw-r--r--Python/traceback.c562
-rw-r--r--Tools/msi/msisupport.c112
251 files changed, 117669 insertions, 117669 deletions
diff --git a/Demo/embed/demo.c b/Demo/embed/demo.c
index b9ea6f5..22bfaff 100644
--- a/Demo/embed/demo.c
+++ b/Demo/embed/demo.c
@@ -6,44 +6,44 @@ PyObject* PyInit_xyzzy(void); /* Forward */
main(int argc, char **argv)
{
- /* Ignore passed-in argc/argv. If desired, conversion
- should use mbstowcs to convert them. */
- wchar_t *args[] = {L"embed", L"hello", 0};
+ /* Ignore passed-in argc/argv. If desired, conversion
+ should use mbstowcs to convert them. */
+ wchar_t *args[] = {L"embed", L"hello", 0};
- /* Pass argv[0] to the Python interpreter */
- Py_SetProgramName(args[0]);
+ /* Pass argv[0] to the Python interpreter */
+ Py_SetProgramName(args[0]);
- /* Add a static module */
- PyImport_AppendInittab("xyzzy", PyInit_xyzzy);
+ /* Add a static module */
+ PyImport_AppendInittab("xyzzy", PyInit_xyzzy);
- /* Initialize the Python interpreter. Required. */
- Py_Initialize();
+ /* Initialize the Python interpreter. Required. */
+ Py_Initialize();
- /* Define sys.argv. It is up to the application if you
- want this; you can also let it undefined (since the Python
- code is generally not a main program it has no business
- touching sys.argv...) */
- PySys_SetArgv(2, args);
+ /* Define sys.argv. It is up to the application if you
+ want this; you can also let it undefined (since the Python
+ code is generally not a main program it has no business
+ touching sys.argv...) */
+ PySys_SetArgv(2, args);
- /* Do some application specific code */
- printf("Hello, brave new world\n\n");
+ /* Do some application specific code */
+ printf("Hello, brave new world\n\n");
- /* Execute some Python statements (in module __main__) */
- PyRun_SimpleString("import sys\n");
- PyRun_SimpleString("print(sys.builtin_module_names)\n");
- PyRun_SimpleString("print(sys.modules.keys())\n");
- PyRun_SimpleString("print(sys.executable)\n");
- PyRun_SimpleString("print(sys.argv)\n");
+ /* Execute some Python statements (in module __main__) */
+ PyRun_SimpleString("import sys\n");
+ PyRun_SimpleString("print(sys.builtin_module_names)\n");
+ PyRun_SimpleString("print(sys.modules.keys())\n");
+ PyRun_SimpleString("print(sys.executable)\n");
+ PyRun_SimpleString("print(sys.argv)\n");
- /* Note that you can call any public function of the Python
- interpreter here, e.g. call_object(). */
+ /* Note that you can call any public function of the Python
+ interpreter here, e.g. call_object(). */
- /* Some more application specific code */
- printf("\nGoodbye, cruel world\n");
+ /* Some more application specific code */
+ printf("\nGoodbye, cruel world\n");
- /* Exit, cleaning up the interpreter */
- Py_Exit(0);
- /*NOTREACHED*/
+ /* Exit, cleaning up the interpreter */
+ Py_Exit(0);
+ /*NOTREACHED*/
}
/* A static module */
@@ -52,29 +52,29 @@ main(int argc, char **argv)
static PyObject *
xyzzy_foo(PyObject *self, PyObject* args)
{
- return PyLong_FromLong(42L);
+ return PyLong_FromLong(42L);
}
static PyMethodDef xyzzy_methods[] = {
- {"foo", xyzzy_foo, METH_NOARGS,
- "Return the meaning of everything."},
- {NULL, NULL} /* sentinel */
+ {"foo", xyzzy_foo, METH_NOARGS,
+ "Return the meaning of everything."},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef xyzzymodule = {
- {}, /* m_base */
- "xyzzy", /* m_name */
- 0, /* m_doc */
- 0, /* m_size */
- xyzzy_methods, /* m_methods */
- 0, /* m_reload */
- 0, /* m_traverse */
- 0, /* m_clear */
- 0, /* m_free */
+ {}, /* m_base */
+ "xyzzy", /* m_name */
+ 0, /* m_doc */
+ 0, /* m_size */
+ xyzzy_methods, /* m_methods */
+ 0, /* m_reload */
+ 0, /* m_traverse */
+ 0, /* m_clear */
+ 0, /* m_free */
};
PyObject*
PyInit_xyzzy(void)
{
- return PyModule_Create(&xyzzymodule);
+ return PyModule_Create(&xyzzymodule);
}
diff --git a/Demo/embed/loop.c b/Demo/embed/loop.c
index d5af829..2f7fe62 100644
--- a/Demo/embed/loop.c
+++ b/Demo/embed/loop.c
@@ -6,28 +6,28 @@
main(int argc, char **argv)
{
- int count = -1;
- char *command;
+ int count = -1;
+ char *command;
- if (argc < 2 || argc > 3) {
- fprintf(stderr, "usage: loop <python-command> [count]\n");
- exit(2);
- }
- command = argv[1];
+ if (argc < 2 || argc > 3) {
+ fprintf(stderr, "usage: loop <python-command> [count]\n");
+ exit(2);
+ }
+ command = argv[1];
- if (argc == 3) {
- count = atoi(argv[2]);
- }
+ if (argc == 3) {
+ count = atoi(argv[2]);
+ }
- Py_SetProgramName(argv[0]);
+ Py_SetProgramName(argv[0]);
- /* uncomment this if you don't want to load site.py */
- /* Py_NoSiteFlag = 1; */
+ /* uncomment this if you don't want to load site.py */
+ /* Py_NoSiteFlag = 1; */
- while (count == -1 || --count >= 0 ) {
- Py_Initialize();
- PyRun_SimpleString(command);
- Py_Finalize();
- }
- return 0;
+ while (count == -1 || --count >= 0 ) {
+ Py_Initialize();
+ PyRun_SimpleString(command);
+ Py_Finalize();
+ }
+ return 0;
}
diff --git a/Demo/pysvr/pysvr.c b/Demo/pysvr/pysvr.c
index cced6da..706cd2a 100644
--- a/Demo/pysvr/pysvr.c
+++ b/Demo/pysvr/pysvr.c
@@ -34,8 +34,8 @@ extern int Py_VerboseFlag;
#endif
struct workorder {
- int conn;
- struct sockaddr_in addr;
+ int conn;
+ struct sockaddr_in addr;
};
/* Forward */
@@ -55,40 +55,40 @@ static PyThreadState *gtstate;
main(int argc, char **argv)
{
- int port = PORT;
- int c;
-
- if (argc > 0 && argv[0] != NULL && argv[0][0] != '\0')
- progname = argv[0];
-
- while ((c = getopt(argc, argv, "v")) != EOF) {
- switch (c) {
- case 'v':
- Py_VerboseFlag++;
- break;
- default:
- usage();
- }
- }
-
- if (optind < argc) {
- if (optind+1 < argc) {
- oprogname();
- fprintf(stderr, "too many arguments\n");
- usage();
- }
- port = atoi(argv[optind]);
- if (port <= 0) {
- fprintf(stderr, "bad port (%s)\n", argv[optind]);
- usage();
- }
- }
-
- main_thread(port);
-
- fprintf(stderr, "Bye.\n");
-
- exit(0);
+ int port = PORT;
+ int c;
+
+ if (argc > 0 && argv[0] != NULL && argv[0][0] != '\0')
+ progname = argv[0];
+
+ while ((c = getopt(argc, argv, "v")) != EOF) {
+ switch (c) {
+ case 'v':
+ Py_VerboseFlag++;
+ break;
+ default:
+ usage();
+ }
+ }
+
+ if (optind < argc) {
+ if (optind+1 < argc) {
+ oprogname();
+ fprintf(stderr, "too many arguments\n");
+ usage();
+ }
+ port = atoi(argv[optind]);
+ if (port <= 0) {
+ fprintf(stderr, "bad port (%s)\n", argv[optind]);
+ usage();
+ }
+ }
+
+ main_thread(port);
+
+ fprintf(stderr, "Bye.\n");
+
+ exit(0);
}
static char usage_line[] = "usage: %s [port]\n";
@@ -96,120 +96,120 @@ static char usage_line[] = "usage: %s [port]\n";
static void
usage(void)
{
- fprintf(stderr, usage_line, progname);
- exit(2);
+ fprintf(stderr, usage_line, progname);
+ exit(2);
}
static void
main_thread(int port)
{
- int sock, conn, size, i;
- struct sockaddr_in addr, clientaddr;
+ int sock, conn, size, i;
+ struct sockaddr_in addr, clientaddr;
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- oprogname();
- perror("can't create socket");
- exit(1);
- }
+ sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (sock < 0) {
+ oprogname();
+ perror("can't create socket");
+ exit(1);
+ }
#ifdef SO_REUSEADDR
- i = 1;
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof i);
+ i = 1;
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof i);
#endif
- memset((char *)&addr, '\0', sizeof addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = 0L;
- if (bind(sock, (struct sockaddr *)&addr, sizeof addr) < 0) {
- oprogname();
- perror("can't bind socket to address");
- exit(1);
- }
-
- if (listen(sock, 5) < 0) {
- oprogname();
- perror("can't listen on socket");
- exit(1);
- }
-
- fprintf(stderr, "Listening on port %d...\n", port);
-
- for (i = 0; ; i++) {
- size = sizeof clientaddr;
- memset((char *) &clientaddr, '\0', size);
- conn = accept(sock, (struct sockaddr *) &clientaddr, &size);
- if (conn < 0) {
- oprogname();
- perror("can't accept connection from socket");
- exit(1);
- }
-
- size = sizeof addr;
- memset((char *) &addr, '\0', size);
- if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) {
- oprogname();
- perror("can't get socket name of connection");
- exit(1);
- }
- if (clientaddr.sin_addr.s_addr != addr.sin_addr.s_addr) {
- oprogname();
- perror("connection from non-local host refused");
- fprintf(stderr, "(addr=%lx, clientaddr=%lx)\n",
- ntohl(addr.sin_addr.s_addr),
- ntohl(clientaddr.sin_addr.s_addr));
- close(conn);
- continue;
- }
- if (i == 4) {
- close(conn);
- break;
- }
- create_thread(conn, &clientaddr);
- }
-
- close(sock);
-
- if (gtstate) {
- PyEval_AcquireThread(gtstate);
- gtstate = NULL;
- Py_Finalize();
- /* And a second time, just because we can. */
- Py_Finalize(); /* This should be harmless. */
- }
- exit(0);
+ memset((char *)&addr, '\0', sizeof addr);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ addr.sin_addr.s_addr = 0L;
+ if (bind(sock, (struct sockaddr *)&addr, sizeof addr) < 0) {
+ oprogname();
+ perror("can't bind socket to address");
+ exit(1);
+ }
+
+ if (listen(sock, 5) < 0) {
+ oprogname();
+ perror("can't listen on socket");
+ exit(1);
+ }
+
+ fprintf(stderr, "Listening on port %d...\n", port);
+
+ for (i = 0; ; i++) {
+ size = sizeof clientaddr;
+ memset((char *) &clientaddr, '\0', size);
+ conn = accept(sock, (struct sockaddr *) &clientaddr, &size);
+ if (conn < 0) {
+ oprogname();
+ perror("can't accept connection from socket");
+ exit(1);
+ }
+
+ size = sizeof addr;
+ memset((char *) &addr, '\0', size);
+ if (getsockname(conn, (struct sockaddr *)&addr, &size) < 0) {
+ oprogname();
+ perror("can't get socket name of connection");
+ exit(1);
+ }
+ if (clientaddr.sin_addr.s_addr != addr.sin_addr.s_addr) {
+ oprogname();
+ perror("connection from non-local host refused");
+ fprintf(stderr, "(addr=%lx, clientaddr=%lx)\n",
+ ntohl(addr.sin_addr.s_addr),
+ ntohl(clientaddr.sin_addr.s_addr));
+ close(conn);
+ continue;
+ }
+ if (i == 4) {
+ close(conn);
+ break;
+ }
+ create_thread(conn, &clientaddr);
+ }
+
+ close(sock);
+
+ if (gtstate) {
+ PyEval_AcquireThread(gtstate);
+ gtstate = NULL;
+ Py_Finalize();
+ /* And a second time, just because we can. */
+ Py_Finalize(); /* This should be harmless. */
+ }
+ exit(0);
}
static void
create_thread(int conn, struct sockaddr_in *addr)
{
- struct workorder *work;
- pthread_t tdata;
-
- work = malloc(sizeof(struct workorder));
- if (work == NULL) {
- oprogname();
- fprintf(stderr, "out of memory for thread.\n");
- close(conn);
- return;
- }
- work->conn = conn;
- work->addr = *addr;
-
- init_python();
-
- if (pthread_create(&tdata, NULL, (void *)service_thread, work) < 0) {
- oprogname();
- perror("can't create new thread");
- close(conn);
- return;
- }
-
- if (pthread_detach(tdata) < 0) {
- oprogname();
- perror("can't detach from thread");
- }
+ struct workorder *work;
+ pthread_t tdata;
+
+ work = malloc(sizeof(struct workorder));
+ if (work == NULL) {
+ oprogname();
+ fprintf(stderr, "out of memory for thread.\n");
+ close(conn);
+ return;
+ }
+ work->conn = conn;
+ work->addr = *addr;
+
+ init_python();
+
+ if (pthread_create(&tdata, NULL, (void *)service_thread, work) < 0) {
+ oprogname();
+ perror("can't create new thread");
+ close(conn);
+ return;
+ }
+
+ if (pthread_detach(tdata) < 0) {
+ oprogname();
+ perror("can't detach from thread");
+ }
}
static PyThreadState *the_tstate;
@@ -219,152 +219,152 @@ static PyObject *the_builtins;
static void
init_python(void)
{
- if (gtstate)
- return;
- Py_Initialize(); /* Initialize the interpreter */
- PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */
- gtstate = PyEval_SaveThread(); /* Release the thread state */
+ if (gtstate)
+ return;
+ Py_Initialize(); /* Initialize the interpreter */
+ PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */
+ gtstate = PyEval_SaveThread(); /* Release the thread state */
}
static void *
service_thread(struct workorder *work)
{
- FILE *input, *output;
+ FILE *input, *output;
- fprintf(stderr, "Start thread for connection %d.\n", work->conn);
+ fprintf(stderr, "Start thread for connection %d.\n", work->conn);
- ps();
+ ps();
- input = fdopen(work->conn, "r");
- if (input == NULL) {
- oprogname();
- perror("can't create input stream");
- goto done;
- }
+ input = fdopen(work->conn, "r");
+ if (input == NULL) {
+ oprogname();
+ perror("can't create input stream");
+ goto done;
+ }
- output = fdopen(work->conn, "w");
- if (output == NULL) {
- oprogname();
- perror("can't create output stream");
- fclose(input);
- goto done;
- }
+ output = fdopen(work->conn, "w");
+ if (output == NULL) {
+ oprogname();
+ perror("can't create output stream");
+ fclose(input);
+ goto done;
+ }
- setvbuf(input, NULL, _IONBF, 0);
- setvbuf(output, NULL, _IONBF, 0);
+ setvbuf(input, NULL, _IONBF, 0);
+ setvbuf(output, NULL, _IONBF, 0);
- run_interpreter(input, output);
+ run_interpreter(input, output);
- fclose(input);
- fclose(output);
+ fclose(input);
+ fclose(output);
done:
- fprintf(stderr, "End thread for connection %d.\n", work->conn);
- close(work->conn);
- free(work);
+ fprintf(stderr, "End thread for connection %d.\n", work->conn);
+ close(work->conn);
+ free(work);
}
static void
oprogname(void)
{
- int save = errno;
- fprintf(stderr, "%s: ", progname);
- errno = save;
+ int save = errno;
+ fprintf(stderr, "%s: ", progname);
+ errno = save;
}
static void
run_interpreter(FILE *input, FILE *output)
{
- PyThreadState *tstate;
- PyObject *new_stdin, *new_stdout;
- PyObject *mainmod, *globals;
- char buffer[1000];
- char *p, *q;
- int n, end;
-
- PyEval_AcquireLock();
- tstate = Py_NewInterpreter();
- if (tstate == NULL) {
- fprintf(output, "Sorry -- can't create an interpreter\n");
- return;
- }
-
- mainmod = PyImport_AddModule("__main__");
- globals = PyModule_GetDict(mainmod);
- Py_INCREF(globals);
-
- new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL);
- new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL);
-
- PySys_SetObject("stdin", new_stdin);
- PySys_SetObject("stdout", new_stdout);
- PySys_SetObject("stderr", new_stdout);
-
- for (n = 1; !PyErr_Occurred(); n++) {
- Py_BEGIN_ALLOW_THREADS
- fprintf(output, "%d> ", n);
- p = fgets(buffer, sizeof buffer, input);
- Py_END_ALLOW_THREADS
-
- if (p == NULL)
- break;
- if (p[0] == '\377' && p[1] == '\354')
- break;
-
- q = strrchr(p, '\r');
- if (q && q[1] == '\n' && q[2] == '\0') {
- *q++ = '\n';
- *q++ = '\0';
- }
-
- while (*p && isspace(*p))
- p++;
- if (p[0] == '#' || p[0] == '\0')
- continue;
-
- end = run_command(buffer, globals);
- if (end < 0)
- PyErr_Print();
-
- if (end)
- break;
- }
-
- Py_XDECREF(globals);
- Py_XDECREF(new_stdin);
- Py_XDECREF(new_stdout);
-
- Py_EndInterpreter(tstate);
- PyEval_ReleaseLock();
-
- fprintf(output, "Goodbye!\n");
+ PyThreadState *tstate;
+ PyObject *new_stdin, *new_stdout;
+ PyObject *mainmod, *globals;
+ char buffer[1000];
+ char *p, *q;
+ int n, end;
+
+ PyEval_AcquireLock();
+ tstate = Py_NewInterpreter();
+ if (tstate == NULL) {
+ fprintf(output, "Sorry -- can't create an interpreter\n");
+ return;
+ }
+
+ mainmod = PyImport_AddModule("__main__");
+ globals = PyModule_GetDict(mainmod);
+ Py_INCREF(globals);
+
+ new_stdin = PyFile_FromFile(input, "<socket-in>", "r", NULL);
+ new_stdout = PyFile_FromFile(output, "<socket-out>", "w", NULL);
+
+ PySys_SetObject("stdin", new_stdin);
+ PySys_SetObject("stdout", new_stdout);
+ PySys_SetObject("stderr", new_stdout);
+
+ for (n = 1; !PyErr_Occurred(); n++) {
+ Py_BEGIN_ALLOW_THREADS
+ fprintf(output, "%d> ", n);
+ p = fgets(buffer, sizeof buffer, input);
+ Py_END_ALLOW_THREADS
+
+ if (p == NULL)
+ break;
+ if (p[0] == '\377' && p[1] == '\354')
+ break;
+
+ q = strrchr(p, '\r');
+ if (q && q[1] == '\n' && q[2] == '\0') {
+ *q++ = '\n';
+ *q++ = '\0';
+ }
+
+ while (*p && isspace(*p))
+ p++;
+ if (p[0] == '#' || p[0] == '\0')
+ continue;
+
+ end = run_command(buffer, globals);
+ if (end < 0)
+ PyErr_Print();
+
+ if (end)
+ break;
+ }
+
+ Py_XDECREF(globals);
+ Py_XDECREF(new_stdin);
+ Py_XDECREF(new_stdout);
+
+ Py_EndInterpreter(tstate);
+ PyEval_ReleaseLock();
+
+ fprintf(output, "Goodbye!\n");
}
static int
run_command(char *buffer, PyObject *globals)
{
- PyObject *m, *d, *v;
- fprintf(stderr, "run_command: %s", buffer);
- if (strchr(buffer, '\n') == NULL)
- fprintf(stderr, "\n");
- v = PyRun_String(buffer, Py_single_input, globals, globals);
- if (v == NULL) {
- if (PyErr_Occurred() == PyExc_SystemExit) {
- PyErr_Clear();
- return 1;
- }
- PyErr_Print();
- return 0;
- }
- Py_DECREF(v);
- return 0;
+ PyObject *m, *d, *v;
+ fprintf(stderr, "run_command: %s", buffer);
+ if (strchr(buffer, '\n') == NULL)
+ fprintf(stderr, "\n");
+ v = PyRun_String(buffer, Py_single_input, globals, globals);
+ if (v == NULL) {
+ if (PyErr_Occurred() == PyExc_SystemExit) {
+ PyErr_Clear();
+ return 1;
+ }
+ PyErr_Print();
+ return 0;
+ }
+ Py_DECREF(v);
+ return 0;
}
static void
ps(void)
{
- char buffer[100];
- PyOS_snprintf(buffer, sizeof(buffer),
- "ps -l -p %d </dev/null | sed 1d\n", getpid());
- system(buffer);
+ char buffer[100];
+ PyOS_snprintf(buffer, sizeof(buffer),
+ "ps -l -p %d </dev/null | sed 1d\n", getpid());
+ system(buffer);
}
diff --git a/Include/abstract.h b/Include/abstract.h
index 6d40ef7..6a6c94c 100644
--- a/Include/abstract.h
+++ b/Include/abstract.h
@@ -32,7 +32,7 @@ Problem
If the programmer wants to get an item from another type of object
that provides sequence behavior, there is no clear way to do it
- correctly.
+ correctly.
The persistent programmer may peruse object.h and find that the
_typeobject structure provides a means of invoking up to (currently
@@ -44,7 +44,7 @@ Problem
Also, certain semantics, especially memory management semantics, may
differ by the type of object being used. Unfortunately, these
semantics are not clearly described in the current include files.
- An abstract interface providing more consistent semantics is needed.
+ An abstract interface providing more consistent semantics is needed.
Proposal
@@ -77,7 +77,7 @@ Proposal
Python distributions.
From the point of view of Python accessing services provided by C
- modules:
+ modules:
- "Python module interface": this interface consist of the basic
routines used to define modules and their members. Most of the
@@ -133,11 +133,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_Print(PyObject *o, FILE *fp, int flags);
- Print an object, o, on file, fp. Returns -1 on
- error. The flags argument is used to enable certain printing
- options. The only option currently supported is Py_Print_RAW.
+ Print an object, o, on file, fp. Returns -1 on
+ error. The flags argument is used to enable certain printing
+ options. The only option currently supported is Py_Print_RAW.
- (What should be said about Py_Print_RAW?)
+ (What should be said about Py_Print_RAW?)
*/
@@ -145,11 +145,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_HasAttrString(PyObject *o, char *attr_name);
- Returns 1 if o has the attribute attr_name, and 0 otherwise.
- This is equivalent to the Python expression:
- hasattr(o,attr_name).
+ Returns 1 if o has the attribute attr_name, and 0 otherwise.
+ This is equivalent to the Python expression:
+ hasattr(o,attr_name).
- This function always succeeds.
+ This function always succeeds.
*/
@@ -157,9 +157,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name);
- Retrieve an attributed named attr_name form object o.
- Returns the attribute value on success, or NULL on failure.
- This is the equivalent of the Python expression: o.attr_name.
+ Retrieve an attributed named attr_name form object o.
+ Returns the attribute value on success, or NULL on failure.
+ This is the equivalent of the Python expression: o.attr_name.
*/
@@ -167,11 +167,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
- Returns 1 if o has the attribute attr_name, and 0 otherwise.
- This is equivalent to the Python expression:
- hasattr(o,attr_name).
+ Returns 1 if o has the attribute attr_name, and 0 otherwise.
+ This is equivalent to the Python expression:
+ hasattr(o,attr_name).
- This function always succeeds.
+ This function always succeeds.
*/
@@ -179,9 +179,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
- Retrieve an attributed named attr_name form object o.
- Returns the attribute value on success, or NULL on failure.
- This is the equivalent of the Python expression: o.attr_name.
+ Retrieve an attributed named attr_name form object o.
+ Returns the attribute value on success, or NULL on failure.
+ This is the equivalent of the Python expression: o.attr_name.
*/
@@ -190,9 +190,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v);
- Set the value of the attribute named attr_name, for object o,
- to the value, v. Returns -1 on failure. This is
- the equivalent of the Python statement: o.attr_name=v.
+ Set the value of the attribute named attr_name, for object o,
+ to the value, v. Returns -1 on failure. This is
+ the equivalent of the Python statement: o.attr_name=v.
*/
@@ -200,9 +200,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
- Set the value of the attribute named attr_name, for object o,
- to the value, v. Returns -1 on failure. This is
- the equivalent of the Python statement: o.attr_name=v.
+ Set the value of the attribute named attr_name, for object o,
+ to the value, v. Returns -1 on failure. This is
+ the equivalent of the Python statement: o.attr_name=v.
*/
@@ -210,9 +210,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_DelAttrString(PyObject *o, char *attr_name);
- Delete attribute named attr_name, for object o. Returns
- -1 on failure. This is the equivalent of the Python
- statement: del o.attr_name.
+ Delete attribute named attr_name, for object o. Returns
+ -1 on failure. This is the equivalent of the Python
+ statement: del o.attr_name.
*/
#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
@@ -221,9 +221,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
- Delete attribute named attr_name, for object o. Returns -1
- on failure. This is the equivalent of the Python
- statement: del o.attr_name.
+ Delete attribute named attr_name, for object o. Returns -1
+ on failure. This is the equivalent of the Python
+ statement: del o.attr_name.
*/
#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
@@ -232,11 +232,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyObject *PyObject_Repr(PyObject *o);
- Compute the string representation of object, o. Returns the
- string representation on success, NULL on failure. This is
- the equivalent of the Python expression: repr(o).
+ Compute the string representation of object, o. Returns the
+ string representation on success, NULL on failure. This is
+ the equivalent of the Python expression: repr(o).
- Called by the repr() built-in function.
+ Called by the repr() built-in function.
*/
@@ -244,11 +244,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyObject *PyObject_Str(PyObject *o);
- Compute the string representation of object, o. Returns the
- string representation on success, NULL on failure. This is
- the equivalent of the Python expression: str(o).)
+ Compute the string representation of object, o. Returns the
+ string representation on success, NULL on failure. This is
+ the equivalent of the Python expression: str(o).)
- Called by the str() and print() built-in functions.
+ Called by the str() and print() built-in functions.
*/
@@ -256,42 +256,42 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
- Determine if the object, o, is callable. Return 1 if the
- object is callable and 0 otherwise.
+ Determine if the object, o, is callable. Return 1 if the
+ object is callable and 0 otherwise.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
- PyObject *args, PyObject *kw);
+ PyObject *args, PyObject *kw);
/*
- Call a callable Python object, callable_object, with
- arguments and keywords arguments. The 'args' argument can not be
- NULL, but the 'kw' argument can be NULL.
+ Call a callable Python object, callable_object, with
+ arguments and keywords arguments. The 'args' argument can not be
+ NULL, but the 'kw' argument can be NULL.
*/
-
+
PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable_object,
PyObject *args);
/*
- Call a callable Python object, callable_object, with
- arguments given by the tuple, args. If no arguments are
- needed, then args may be NULL. Returns the result of the
- call on success, or NULL on failure. This is the equivalent
- of the Python expression: o(*args).
+ Call a callable Python object, callable_object, with
+ arguments given by the tuple, args. If no arguments are
+ needed, then args may be NULL. Returns the result of the
+ call on success, or NULL on failure. This is the equivalent
+ of the Python expression: o(*args).
*/
PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable_object,
char *format, ...);
/*
- Call a callable Python object, callable_object, with a
- variable number of C arguments. The C arguments are described
- using a mkvalue-style format string. The format may be NULL,
- indicating that no arguments are provided. Returns the
- result of the call on success, or NULL on failure. This is
- the equivalent of the Python expression: o(*args).
+ Call a callable Python object, callable_object, with a
+ variable number of C arguments. The C arguments are described
+ using a mkvalue-style format string. The format may be NULL,
+ indicating that no arguments are provided. Returns the
+ result of the call on success, or NULL on failure. This is
+ the equivalent of the Python expression: o(*args).
*/
@@ -299,29 +299,29 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
char *format, ...);
/*
- Call the method named m of object o with a variable number of
- C arguments. The C arguments are described by a mkvalue
- format string. The format may be NULL, indicating that no
- arguments are provided. Returns the result of the call on
- success, or NULL on failure. This is the equivalent of the
- Python expression: o.method(args).
+ Call the method named m of object o with a variable number of
+ C arguments. The C arguments are described by a mkvalue
+ format string. The format may be NULL, indicating that no
+ arguments are provided. Returns the result of the call on
+ success, or NULL on failure. This is the equivalent of the
+ Python expression: o.method(args).
*/
PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
- char *format, ...);
+ char *format, ...);
PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *o,
- char *name,
- char *format, ...);
+ char *name,
+ char *format, ...);
PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
...);
/*
- Call a callable Python object, callable_object, with a
- variable number of C arguments. The C arguments are provided
- as PyObject * values, terminated by a NULL. Returns the
- result of the call on success, or NULL on failure. This is
- the equivalent of the Python expression: o(*args).
+ Call a callable Python object, callable_object, with a
+ variable number of C arguments. The C arguments are provided
+ as PyObject * values, terminated by a NULL. Returns the
+ result of the call on success, or NULL on failure. This is
+ the equivalent of the Python expression: o(*args).
*/
@@ -329,11 +329,11 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyObject *method, ...);
/*
- Call the method named m of object o with a variable number of
- C arguments. The C arguments are provided as PyObject *
- values, terminated by NULL. Returns the result of the call
- on success, or NULL on failure. This is the equivalent of
- the Python expression: o.method(args).
+ Call the method named m of object o with a variable number of
+ C arguments. The C arguments are provided as PyObject *
+ values, terminated by NULL. Returns the result of the call
+ on success, or NULL on failure. This is the equivalent of
+ the Python expression: o.method(args).
*/
@@ -341,9 +341,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
long PyObject_Hash(PyObject *o);
- Compute and return the hash, hash_value, of an object, o. On
- failure, return -1. This is the equivalent of the Python
- expression: hash(o).
+ Compute and return the hash, hash_value, of an object, o. On
+ failure, return -1. This is the equivalent of the Python
+ expression: hash(o).
*/
@@ -351,35 +351,35 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyObject_IsTrue(PyObject *o);
- Returns 1 if the object, o, is considered to be true, 0 if o is
- considered to be false and -1 on failure. This is equivalent to the
- Python expression: not not o
+ Returns 1 if the object, o, is considered to be true, 0 if o is
+ considered to be false and -1 on failure. This is equivalent to the
+ Python expression: not not o
*/
/* Implemented elsewhere:
int PyObject_Not(PyObject *o);
- Returns 0 if the object, o, is considered to be true, 1 if o is
- considered to be false and -1 on failure. This is equivalent to the
- Python expression: not o
+ Returns 0 if the object, o, is considered to be true, 1 if o is
+ considered to be false and -1 on failure. This is equivalent to the
+ Python expression: not o
*/
PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
/*
- On success, returns a type object corresponding to the object
- type of object o. On failure, returns NULL. This is
- equivalent to the Python expression: type(o).
+ On success, returns a type object corresponding to the object
+ type of object o. On failure, returns NULL. This is
+ equivalent to the Python expression: type(o).
*/
PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
/*
- Return the size of object o. If the object, o, provides
- both sequence and mapping protocols, the sequence size is
- returned. On error, -1 is returned. This is the equivalent
- to the Python expression: len(o).
+ Return the size of object o. If the object, o, provides
+ both sequence and mapping protocols, the sequence size is
+ returned. On error, -1 is returned. This is the equivalent
+ to the Python expression: len(o).
*/
/* For DLL compatibility */
@@ -390,203 +390,203 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(Py_ssize_t) _PyObject_LengthHint(PyObject *o, Py_ssize_t);
/*
- Guess the size of object o using len(o) or o.__length_hint__().
- If neither of those return a non-negative value, then return the
- default value. If one of the calls fails, this function returns -1.
+ Guess the size of object o using len(o) or o.__length_hint__().
+ If neither of those return a non-negative value, then return the
+ default value. If one of the calls fails, this function returns -1.
*/
PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
/*
- Return element of o corresponding to the object, key, or NULL
- on failure. This is the equivalent of the Python expression:
- o[key].
+ Return element of o corresponding to the object, key, or NULL
+ on failure. This is the equivalent of the Python expression:
+ o[key].
*/
PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
/*
- Map the object, key, to the value, v. Returns
- -1 on failure. This is the equivalent of the Python
- statement: o[key]=v.
+ Map the object, key, to the value, v. Returns
+ -1 on failure. This is the equivalent of the Python
+ statement: o[key]=v.
*/
PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, char *key);
/*
- Remove the mapping for object, key, from the object *o.
- Returns -1 on failure. This is equivalent to
- the Python statement: del o[key].
+ Remove the mapping for object, key, from the object *o.
+ Returns -1 on failure. This is equivalent to
+ the Python statement: del o[key].
*/
PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
/*
- Delete the mapping for key from *o. Returns -1 on failure.
- This is the equivalent of the Python statement: del o[key].
+ Delete the mapping for key from *o. Returns -1 on failure.
+ This is the equivalent of the Python statement: del o[key].
*/
- /* old buffer API
- FIXME: usage of these should all be replaced in Python itself
- but for backwards compatibility we will implement them.
- Their usage without a corresponding "unlock" mechansim
- may create issues (but they would already be there). */
+ /* old buffer API
+ FIXME: usage of these should all be replaced in Python itself
+ but for backwards compatibility we will implement them.
+ Their usage without a corresponding "unlock" mechansim
+ may create issues (but they would already be there). */
PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
- const char **buffer,
- Py_ssize_t *buffer_len);
+ const char **buffer,
+ Py_ssize_t *buffer_len);
- /*
- Takes an arbitrary object which must support the (character,
- single segment) buffer interface and returns a pointer to a
- read-only memory location useable as character based input
- for subsequent processing.
+ /*
+ Takes an arbitrary object which must support the (character,
+ single segment) buffer interface and returns a pointer to a
+ read-only memory location useable as character based input
+ for subsequent processing.
- 0 is returned on success. buffer and buffer_len are only
- set in case no error occurs. Otherwise, -1 is returned and
- an exception set.
+ 0 is returned on success. buffer and buffer_len are only
+ set in case no error occurs. Otherwise, -1 is returned and
+ an exception set.
*/
PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj);
- /*
- Checks whether an arbitrary object supports the (character,
- single segment) buffer interface. Returns 1 on success, 0
- on failure.
+ /*
+ Checks whether an arbitrary object supports the (character,
+ single segment) buffer interface. Returns 1 on success, 0
+ on failure.
*/
PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
- const void **buffer,
- Py_ssize_t *buffer_len);
+ const void **buffer,
+ Py_ssize_t *buffer_len);
- /*
- Same as PyObject_AsCharBuffer() except that this API expects
- (readable, single segment) buffer interface and returns a
- pointer to a read-only memory location which can contain
- arbitrary data.
+ /*
+ Same as PyObject_AsCharBuffer() except that this API expects
+ (readable, single segment) buffer interface and returns a
+ pointer to a read-only memory location which can contain
+ arbitrary data.
- 0 is returned on success. buffer and buffer_len are only
- set in case no error occurrs. Otherwise, -1 is returned and
- an exception set.
+ 0 is returned on success. buffer and buffer_len are only
+ set in case no error occurrs. Otherwise, -1 is returned and
+ an exception set.
*/
PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
- void **buffer,
- Py_ssize_t *buffer_len);
+ void **buffer,
+ Py_ssize_t *buffer_len);
- /*
- Takes an arbitrary object which must support the (writable,
- single segment) buffer interface and returns a pointer to a
- writable memory location in buffer of size buffer_len.
+ /*
+ Takes an arbitrary object which must support the (writable,
+ single segment) buffer interface and returns a pointer to a
+ writable memory location in buffer of size buffer_len.
- 0 is returned on success. buffer and buffer_len are only
- set in case no error occurrs. Otherwise, -1 is returned and
- an exception set.
+ 0 is returned on success. buffer and buffer_len are only
+ set in case no error occurrs. Otherwise, -1 is returned and
+ an exception set.
*/
- /* new buffer API */
+ /* new buffer API */
#define PyObject_CheckBuffer(obj) \
- (((obj)->ob_type->tp_as_buffer != NULL) && \
- ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
+ (((obj)->ob_type->tp_as_buffer != NULL) && \
+ ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
- /* Return 1 if the getbuffer function is available, otherwise
- return 0 */
+ /* Return 1 if the getbuffer function is available, otherwise
+ return 0 */
- PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
- int flags);
+ PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
+ int flags);
- /* This is a C-API version of the getbuffer function call. It checks
- to make sure object has the required function pointer and issues the
- call. Returns -1 and raises an error on failure and returns 0 on
- success
- */
+ /* This is a C-API version of the getbuffer function call. It checks
+ to make sure object has the required function pointer and issues the
+ call. Returns -1 and raises an error on failure and returns 0 on
+ success
+ */
PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
-
- /* Get the memory area pointed to by the indices for the buffer given.
- Note that view->ndim is the assumed size of indices
- */
+
+ /* Get the memory area pointed to by the indices for the buffer given.
+ Note that view->ndim is the assumed size of indices
+ */
PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
-
- /* Return the implied itemsize of the data-format area from a
- struct-style description */
-
-
+ /* Return the implied itemsize of the data-format area from a
+ struct-style description */
+
+
+
PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
- Py_ssize_t len, char fort);
+ Py_ssize_t len, char fort);
- PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
- Py_ssize_t len, char fort);
+ PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
+ Py_ssize_t len, char fort);
- /* Copy len bytes of data from the contiguous chunk of memory
- pointed to by buf into the buffer exported by obj. Return
- 0 on success and return -1 and raise a PyBuffer_Error on
- error (i.e. the object does not have a buffer interface or
- it is not working).
+ /* Copy len bytes of data from the contiguous chunk of memory
+ pointed to by buf into the buffer exported by obj. Return
+ 0 on success and return -1 and raise a PyBuffer_Error on
+ error (i.e. the object does not have a buffer interface or
+ it is not working).
- If fort is 'F', then if the object is multi-dimensional,
- then the data will be copied into the array in
- Fortran-style (first dimension varies the fastest). If
- fort is 'C', then the data will be copied into the array
- in C-style (last dimension varies the fastest). If fort
- is 'A', then it does not matter and the copy will be made
- in whatever way is more efficient.
+ If fort is 'F', then if the object is multi-dimensional,
+ then the data will be copied into the array in
+ Fortran-style (first dimension varies the fastest). If
+ fort is 'C', then the data will be copied into the array
+ in C-style (last dimension varies the fastest). If fort
+ is 'A', then it does not matter and the copy will be made
+ in whatever way is more efficient.
- */
+ */
PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
-
- /* Copy the data from the src buffer to the buffer of destination
- */
+
+ /* Copy the data from the src buffer to the buffer of destination
+ */
PyAPI_FUNC(int) PyBuffer_IsContiguous(Py_buffer *view, char fort);
- PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
- Py_ssize_t *shape,
- Py_ssize_t *strides,
- int itemsize,
- char fort);
+ PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
+ Py_ssize_t *shape,
+ Py_ssize_t *strides,
+ int itemsize,
+ char fort);
- /* Fill the strides array with byte-strides of a contiguous
- (Fortran-style if fort is 'F' or C-style otherwise)
- array of the given shape with the given number of bytes
- per element.
- */
+ /* Fill the strides array with byte-strides of a contiguous
+ (Fortran-style if fort is 'F' or C-style otherwise)
+ array of the given shape with the given number of bytes
+ per element.
+ */
PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
- Py_ssize_t len, int readonly,
- int flags);
+ Py_ssize_t len, int readonly,
+ int flags);
- /* Fills in a buffer-info structure correctly for an exporter
- that can only share a contiguous chunk of memory of
- "unsigned bytes" of the given length. Returns 0 on success
- and -1 (with raising an error) on error.
- */
+ /* Fills in a buffer-info structure correctly for an exporter
+ that can only share a contiguous chunk of memory of
+ "unsigned bytes" of the given length. Returns 0 on success
+ and -1 (with raising an error) on error.
+ */
PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
/* Releases a Py_buffer obtained from getbuffer ParseTuple's s*.
- */
+ */
PyAPI_FUNC(PyObject *) PyObject_Format(PyObject* obj,
- PyObject *format_spec);
+ PyObject *format_spec);
/*
- Takes an arbitrary object and returns the result of
- calling obj.__format__(format_spec).
+ Takes an arbitrary object and returns the result of
+ calling obj.__format__(format_spec).
*/
/* Iterators */
PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
/* Takes an object and returns an iterator for it.
- This is typically a new iterator but if the argument
- is an iterator, this returns itself. */
+ This is typically a new iterator but if the argument
+ is an iterator, this returns itself. */
#define PyIter_Check(obj) \
((obj)->ob_type->tp_iternext != NULL && \
@@ -594,314 +594,314 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
/* Takes an iterator object and calls its tp_iternext slot,
- returning the next value. If the iterator is exhausted,
- this returns NULL without setting an exception.
- NULL with an exception means an error occurred. */
+ returning the next value. If the iterator is exhausted,
+ this returns NULL without setting an exception.
+ NULL with an exception means an error occurred. */
/* Number Protocol:*/
PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
/*
- Returns 1 if the object, o, provides numeric protocols, and
- false otherwise.
+ Returns 1 if the object, o, provides numeric protocols, and
+ false otherwise.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
/*
- Returns the result of adding o1 and o2, or null on failure.
- This is the equivalent of the Python expression: o1+o2.
+ Returns the result of adding o1 and o2, or null on failure.
+ This is the equivalent of the Python expression: o1+o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
/*
- Returns the result of subtracting o2 from o1, or null on
- failure. This is the equivalent of the Python expression:
- o1-o2.
+ Returns the result of subtracting o2 from o1, or null on
+ failure. This is the equivalent of the Python expression:
+ o1-o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
/*
- Returns the result of multiplying o1 and o2, or null on
- failure. This is the equivalent of the Python expression:
- o1*o2.
+ Returns the result of multiplying o1 and o2, or null on
+ failure. This is the equivalent of the Python expression:
+ o1*o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
/*
- Returns the result of dividing o1 by o2 giving an integral result,
- or null on failure.
- This is the equivalent of the Python expression: o1//o2.
+ Returns the result of dividing o1 by o2 giving an integral result,
+ or null on failure.
+ This is the equivalent of the Python expression: o1//o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
/*
- Returns the result of dividing o1 by o2 giving a float result,
- or null on failure.
- This is the equivalent of the Python expression: o1/o2.
+ Returns the result of dividing o1 by o2 giving a float result,
+ or null on failure.
+ This is the equivalent of the Python expression: o1/o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
/*
- Returns the remainder of dividing o1 by o2, or null on
- failure. This is the equivalent of the Python expression:
- o1%o2.
+ Returns the remainder of dividing o1 by o2, or null on
+ failure. This is the equivalent of the Python expression:
+ o1%o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
/*
- See the built-in function divmod. Returns NULL on failure.
- This is the equivalent of the Python expression:
- divmod(o1,o2).
+ See the built-in function divmod. Returns NULL on failure.
+ This is the equivalent of the Python expression:
+ divmod(o1,o2).
*/
PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
PyObject *o3);
/*
- See the built-in function pow. Returns NULL on failure.
- This is the equivalent of the Python expression:
- pow(o1,o2,o3), where o3 is optional.
+ See the built-in function pow. Returns NULL on failure.
+ This is the equivalent of the Python expression:
+ pow(o1,o2,o3), where o3 is optional.
*/
PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
/*
- Returns the negation of o on success, or null on failure.
- This is the equivalent of the Python expression: -o.
+ Returns the negation of o on success, or null on failure.
+ This is the equivalent of the Python expression: -o.
*/
PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
/*
- Returns the (what?) of o on success, or NULL on failure.
- This is the equivalent of the Python expression: +o.
+ Returns the (what?) of o on success, or NULL on failure.
+ This is the equivalent of the Python expression: +o.
*/
PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
/*
- Returns the absolute value of o, or null on failure. This is
- the equivalent of the Python expression: abs(o).
+ Returns the absolute value of o, or null on failure. This is
+ the equivalent of the Python expression: abs(o).
*/
PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
/*
- Returns the bitwise negation of o on success, or NULL on
- failure. This is the equivalent of the Python expression:
- ~o.
+ Returns the bitwise negation of o on success, or NULL on
+ failure. This is the equivalent of the Python expression:
+ ~o.
*/
PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
/*
- Returns the result of left shifting o1 by o2 on success, or
- NULL on failure. This is the equivalent of the Python
- expression: o1 << o2.
+ Returns the result of left shifting o1 by o2 on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o1 << o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
/*
- Returns the result of right shifting o1 by o2 on success, or
- NULL on failure. This is the equivalent of the Python
- expression: o1 >> o2.
+ Returns the result of right shifting o1 by o2 on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o1 >> o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
/*
- Returns the result of bitwise and of o1 and o2 on success, or
- NULL on failure. This is the equivalent of the Python
- expression: o1&o2.
+ Returns the result of bitwise and of o1 and o2 on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o1&o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
/*
- Returns the bitwise exclusive or of o1 by o2 on success, or
- NULL on failure. This is the equivalent of the Python
- expression: o1^o2.
+ Returns the bitwise exclusive or of o1 by o2 on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o1^o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
/*
- Returns the result of bitwise or on o1 and o2 on success, or
- NULL on failure. This is the equivalent of the Python
- expression: o1|o2.
+ Returns the result of bitwise or on o1 and o2 on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o1|o2.
*/
#define PyIndex_Check(obj) \
((obj)->ob_type->tp_as_number != NULL && \
(obj)->ob_type->tp_as_number->nb_index != NULL)
-
+
PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
/*
- Returns the object converted to a Python long or int
- or NULL with an error raised on failure.
+ Returns the object converted to a Python long or int
+ or NULL with an error raised on failure.
*/
PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
/*
- Returns the Integral instance converted to an int. The
- instance is expected to be int or long or have an __int__
- method. Steals integral's reference. error_format will be
- used to create the TypeError if integral isn't actually an
- Integral instance. error_format should be a format string
- that can accept a char* naming integral's type.
+ Returns the Integral instance converted to an int. The
+ instance is expected to be int or long or have an __int__
+ method. Steals integral's reference. error_format will be
+ used to create the TypeError if integral isn't actually an
+ Integral instance. error_format should be a format string
+ that can accept a char* naming integral's type.
*/
PyAPI_FUNC(PyObject *) _PyNumber_ConvertIntegralToInt(
- PyObject *integral,
- const char* error_format);
+ PyObject *integral,
+ const char* error_format);
/*
- Returns the object converted to Py_ssize_t by going through
- PyNumber_Index first. If an overflow error occurs while
- converting the int-or-long to Py_ssize_t, then the second argument
- is the error-type to return. If it is NULL, then the overflow error
- is cleared and the value is clipped.
+ Returns the object converted to Py_ssize_t by going through
+ PyNumber_Index first. If an overflow error occurs while
+ converting the int-or-long to Py_ssize_t, then the second argument
+ is the error-type to return. If it is NULL, then the overflow error
+ is cleared and the value is clipped.
*/
PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
/*
- Returns the o converted to an integer object on success, or
- NULL on failure. This is the equivalent of the Python
- expression: int(o).
+ Returns the o converted to an integer object on success, or
+ NULL on failure. This is the equivalent of the Python
+ expression: int(o).
*/
PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
/*
- Returns the o converted to a float object on success, or NULL
- on failure. This is the equivalent of the Python expression:
- float(o).
+ Returns the o converted to a float object on success, or NULL
+ on failure. This is the equivalent of the Python expression:
+ float(o).
*/
-
+
/* In-place variants of (some of) the above number protocol functions */
PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
/*
- Returns the result of adding o2 to o1, possibly in-place, or null
- on failure. This is the equivalent of the Python expression:
- o1 += o2.
+ Returns the result of adding o2 to o1, possibly in-place, or null
+ on failure. This is the equivalent of the Python expression:
+ o1 += o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
/*
- Returns the result of subtracting o2 from o1, possibly in-place or
- null on failure. This is the equivalent of the Python expression:
- o1 -= o2.
+ Returns the result of subtracting o2 from o1, possibly in-place or
+ null on failure. This is the equivalent of the Python expression:
+ o1 -= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
/*
- Returns the result of multiplying o1 by o2, possibly in-place, or
- null on failure. This is the equivalent of the Python expression:
- o1 *= o2.
+ Returns the result of multiplying o1 by o2, possibly in-place, or
+ null on failure. This is the equivalent of the Python expression:
+ o1 *= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
- PyObject *o2);
+ PyObject *o2);
/*
- Returns the result of dividing o1 by o2 giving an integral result,
- possibly in-place, or null on failure.
- This is the equivalent of the Python expression:
- o1 /= o2.
+ Returns the result of dividing o1 by o2 giving an integral result,
+ possibly in-place, or null on failure.
+ This is the equivalent of the Python expression:
+ o1 /= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
- PyObject *o2);
+ PyObject *o2);
/*
- Returns the result of dividing o1 by o2 giving a float result,
- possibly in-place, or null on failure.
- This is the equivalent of the Python expression:
- o1 /= o2.
+ Returns the result of dividing o1 by o2 giving a float result,
+ possibly in-place, or null on failure.
+ This is the equivalent of the Python expression:
+ o1 /= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
/*
- Returns the remainder of dividing o1 by o2, possibly in-place, or
- null on failure. This is the equivalent of the Python expression:
- o1 %= o2.
+ Returns the remainder of dividing o1 by o2, possibly in-place, or
+ null on failure. This is the equivalent of the Python expression:
+ o1 %= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
- PyObject *o3);
+ PyObject *o3);
/*
- Returns the result of raising o1 to the power of o2, possibly
- in-place, or null on failure. This is the equivalent of the Python
- expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
+ Returns the result of raising o1 to the power of o2, possibly
+ in-place, or null on failure. This is the equivalent of the Python
+ expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
/*
- Returns the result of left shifting o1 by o2, possibly in-place, or
- null on failure. This is the equivalent of the Python expression:
- o1 <<= o2.
+ Returns the result of left shifting o1 by o2, possibly in-place, or
+ null on failure. This is the equivalent of the Python expression:
+ o1 <<= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
/*
- Returns the result of right shifting o1 by o2, possibly in-place or
- null on failure. This is the equivalent of the Python expression:
- o1 >>= o2.
+ Returns the result of right shifting o1 by o2, possibly in-place or
+ null on failure. This is the equivalent of the Python expression:
+ o1 >>= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
/*
- Returns the result of bitwise and of o1 and o2, possibly in-place,
- or null on failure. This is the equivalent of the Python
- expression: o1 &= o2.
+ Returns the result of bitwise and of o1 and o2, possibly in-place,
+ or null on failure. This is the equivalent of the Python
+ expression: o1 &= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
/*
- Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
- null on failure. This is the equivalent of the Python expression:
- o1 ^= o2.
+ Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
+ null on failure. This is the equivalent of the Python expression:
+ o1 ^= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
/*
- Returns the result of bitwise or of o1 and o2, possibly in-place,
- or null on failure. This is the equivalent of the Python
- expression: o1 |= o2.
+ Returns the result of bitwise or of o1 and o2, possibly in-place,
+ or null on failure. This is the equivalent of the Python
+ expression: o1 |= o2.
*/
PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
/*
- Returns the integer n converted to a string with a base, with a base
- marker of 0b, 0o or 0x prefixed if applicable.
- If n is not an int object, it is converted with PyNumber_Index first.
+ Returns the integer n converted to a string with a base, with a base
+ marker of 0b, 0o or 0x prefixed if applicable.
+ If n is not an int object, it is converted with PyNumber_Index first.
*/
@@ -910,16 +910,16 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(int) PySequence_Check(PyObject *o);
/*
- Return 1 if the object provides sequence protocol, and zero
- otherwise.
+ Return 1 if the object provides sequence protocol, and zero
+ otherwise.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
/*
- Return the size of sequence object o, or -1 on failure.
+ Return the size of sequence object o, or -1 on failure.
*/
/* For DLL compatibility */
@@ -931,147 +931,147 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
/*
- Return the concatenation of o1 and o2 on success, and NULL on
- failure. This is the equivalent of the Python
- expression: o1+o2.
+ Return the concatenation of o1 and o2 on success, and NULL on
+ failure. This is the equivalent of the Python
+ expression: o1+o2.
*/
PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
/*
- Return the result of repeating sequence object o count times,
- or NULL on failure. This is the equivalent of the Python
- expression: o1*count.
+ Return the result of repeating sequence object o count times,
+ or NULL on failure. This is the equivalent of the Python
+ expression: o1*count.
*/
PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
/*
- Return the ith element of o, or NULL on failure. This is the
- equivalent of the Python expression: o[i].
+ Return the ith element of o, or NULL on failure. This is the
+ equivalent of the Python expression: o[i].
*/
PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
/*
- Return the slice of sequence object o between i1 and i2, or
- NULL on failure. This is the equivalent of the Python
- expression: o[i1:i2].
+ Return the slice of sequence object o between i1 and i2, or
+ NULL on failure. This is the equivalent of the Python
+ expression: o[i1:i2].
*/
PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
/*
- Assign object v to the ith element of o. Returns
- -1 on failure. This is the equivalent of the Python
- statement: o[i]=v.
+ Assign object v to the ith element of o. Returns
+ -1 on failure. This is the equivalent of the Python
+ statement: o[i]=v.
*/
PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
/*
- Delete the ith element of object v. Returns
- -1 on failure. This is the equivalent of the Python
- statement: del o[i].
+ Delete the ith element of object v. Returns
+ -1 on failure. This is the equivalent of the Python
+ statement: del o[i].
*/
PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
PyObject *v);
/*
- Assign the sequence object, v, to the slice in sequence
- object, o, from i1 to i2. Returns -1 on failure. This is the
- equivalent of the Python statement: o[i1:i2]=v.
+ Assign the sequence object, v, to the slice in sequence
+ object, o, from i1 to i2. Returns -1 on failure. This is the
+ equivalent of the Python statement: o[i1:i2]=v.
*/
PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
/*
- Delete the slice in sequence object, o, from i1 to i2.
- Returns -1 on failure. This is the equivalent of the Python
- statement: del o[i1:i2].
+ Delete the slice in sequence object, o, from i1 to i2.
+ Returns -1 on failure. This is the equivalent of the Python
+ statement: del o[i1:i2].
*/
PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
/*
- Returns the sequence, o, as a tuple on success, and NULL on failure.
- This is equivalent to the Python expression: tuple(o)
+ Returns the sequence, o, as a tuple on success, and NULL on failure.
+ This is equivalent to the Python expression: tuple(o)
*/
PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
/*
- Returns the sequence, o, as a list on success, and NULL on failure.
- This is equivalent to the Python expression: list(o)
+ Returns the sequence, o, as a list on success, and NULL on failure.
+ This is equivalent to the Python expression: list(o)
*/
PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
/*
- Returns the sequence, o, as a tuple, unless it's already a
- tuple or list. Use PySequence_Fast_GET_ITEM to access the
- members of this list, and PySequence_Fast_GET_SIZE to get its length.
+ Returns the sequence, o, as a tuple, unless it's already a
+ tuple or list. Use PySequence_Fast_GET_ITEM to access the
+ members of this list, and PySequence_Fast_GET_SIZE to get its length.
- Returns NULL on failure. If the object does not support iteration,
- raises a TypeError exception with m as the message text.
+ Returns NULL on failure. If the object does not support iteration,
+ raises a TypeError exception with m as the message text.
*/
#define PySequence_Fast_GET_SIZE(o) \
- (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
+ (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
/*
- Return the size of o, assuming that o was returned by
- PySequence_Fast and is not NULL.
+ Return the size of o, assuming that o was returned by
+ PySequence_Fast and is not NULL.
*/
#define PySequence_Fast_GET_ITEM(o, i)\
(PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
/*
- Return the ith element of o, assuming that o was returned by
- PySequence_Fast, and that i is within bounds.
+ Return the ith element of o, assuming that o was returned by
+ PySequence_Fast, and that i is within bounds.
*/
#define PySequence_ITEM(o, i)\
- ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
+ ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
/* Assume tp_as_sequence and sq_item exist and that i does not
- need to be corrected for a negative index
- */
+ need to be corrected for a negative index
+ */
#define PySequence_Fast_ITEMS(sf) \
- (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
- : ((PyTupleObject *)(sf))->ob_item)
- /* Return a pointer to the underlying item array for
- an object retured by PySequence_Fast */
+ (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
+ : ((PyTupleObject *)(sf))->ob_item)
+ /* Return a pointer to the underlying item array for
+ an object retured by PySequence_Fast */
PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
/*
- Return the number of occurrences on value on o, that is,
- return the number of keys for which o[key]==value. On
- failure, return -1. This is equivalent to the Python
- expression: o.count(value).
+ Return the number of occurrences on value on o, that is,
+ return the number of keys for which o[key]==value. On
+ failure, return -1. This is equivalent to the Python
+ expression: o.count(value).
*/
PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
/*
- Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
- Use __contains__ if possible, else _PySequence_IterSearch().
+ Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
+ Use __contains__ if possible, else _PySequence_IterSearch().
*/
#define PY_ITERSEARCH_COUNT 1
#define PY_ITERSEARCH_INDEX 2
#define PY_ITERSEARCH_CONTAINS 3
PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
- PyObject *obj, int operation);
- /*
- Iterate over seq. Result depends on the operation:
- PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
- error.
- PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
- obj in seq; set ValueError and return -1 if none found;
- also return -1 on error.
- PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
- error.
- */
+ PyObject *obj, int operation);
+ /*
+ Iterate over seq. Result depends on the operation:
+ PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
+ error.
+ PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
+ obj in seq; set ValueError and return -1 if none found;
+ also return -1 on error.
+ PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
+ error.
+ */
/* For DLL-level backwards compatibility */
#undef PySequence_In
@@ -1081,17 +1081,17 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
#define PySequence_In PySequence_Contains
/*
- Determine if o contains value. If an item in o is equal to
- X, return 1, otherwise return 0. On error, return -1. This
- is equivalent to the Python expression: value in o.
+ Determine if o contains value. If an item in o is equal to
+ X, return 1, otherwise return 0. On error, return -1. This
+ is equivalent to the Python expression: value in o.
*/
PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
/*
- Return the first index for which o[i]=value. On error,
- return -1. This is equivalent to the Python
- expression: o.index(value).
+ Return the first index for which o[i]=value. On error,
+ return -1. This is equivalent to the Python
+ expression: o.index(value).
*/
/* In-place versions of some of the above Sequence functions. */
@@ -1099,18 +1099,18 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
/*
- Append o2 to o1, in-place when possible. Return the resulting
- object, which could be o1, or NULL on failure. This is the
- equivalent of the Python expression: o1 += o2.
+ Append o2 to o1, in-place when possible. Return the resulting
+ object, which could be o1, or NULL on failure. This is the
+ equivalent of the Python expression: o1 += o2.
*/
PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
/*
- Repeat o1 by count, in-place when possible. Return the resulting
- object, which could be o1, or NULL on failure. This is the
- equivalent of the Python expression: o1 *= count.
+ Repeat o1 by count, in-place when possible. Return the resulting
+ object, which could be o1, or NULL on failure. This is the
+ equivalent of the Python expression: o1 *= count.
*/
@@ -1119,18 +1119,18 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
/*
- Return 1 if the object provides mapping protocol, and zero
- otherwise.
+ Return 1 if the object provides mapping protocol, and zero
+ otherwise.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
/*
- Returns the number of keys in object o on success, and -1 on
- failure. For objects that do not provide sequence protocol,
- this is equivalent to the Python expression: len(o).
+ Returns the number of keys in object o on success, and -1 on
+ failure. For objects that do not provide sequence protocol,
+ this is equivalent to the Python expression: len(o).
*/
/* For DLL compatibility */
@@ -1143,9 +1143,9 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyMapping_DelItemString(PyObject *o, char *key);
- Remove the mapping for object, key, from the object *o.
- Returns -1 on failure. This is equivalent to
- the Python statement: del o[key].
+ Remove the mapping for object, key, from the object *o.
+ Returns -1 on failure. This is equivalent to
+ the Python statement: del o[key].
*/
#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
@@ -1153,71 +1153,71 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
int PyMapping_DelItem(PyObject *o, PyObject *key);
- Remove the mapping for object, key, from the object *o.
- Returns -1 on failure. This is equivalent to
- the Python statement: del o[key].
+ Remove the mapping for object, key, from the object *o.
+ Returns -1 on failure. This is equivalent to
+ the Python statement: del o[key].
*/
#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, char *key);
/*
- On success, return 1 if the mapping object has the key, key,
- and 0 otherwise. This is equivalent to the Python expression:
- key in o.
+ On success, return 1 if the mapping object has the key, key,
+ and 0 otherwise. This is equivalent to the Python expression:
+ key in o.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
/*
- Return 1 if the mapping object has the key, key,
- and 0 otherwise. This is equivalent to the Python expression:
- key in o.
+ Return 1 if the mapping object has the key, key,
+ and 0 otherwise. This is equivalent to the Python expression:
+ key in o.
- This function always succeeds.
+ This function always succeeds.
*/
PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
/*
- On success, return a list or tuple of the keys in object o.
- On failure, return NULL.
+ On success, return a list or tuple of the keys in object o.
+ On failure, return NULL.
*/
PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
/*
- On success, return a list or tuple of the values in object o.
- On failure, return NULL.
+ On success, return a list or tuple of the values in object o.
+ On failure, return NULL.
*/
PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
/*
- On success, return a list or tuple of the items in object o,
- where each item is a tuple containing a key-value pair.
- On failure, return NULL.
+ On success, return a list or tuple of the items in object o,
+ where each item is a tuple containing a key-value pair.
+ On failure, return NULL.
*/
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
/*
- Return element of o corresponding to the object, key, or NULL
- on failure. This is the equivalent of the Python expression:
- o[key].
+ Return element of o corresponding to the object, key, or NULL
+ on failure. This is the equivalent of the Python expression:
+ o[key].
*/
PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, char *key,
PyObject *value);
/*
- Map the object, key, to the value, v. Returns
- -1 on failure. This is the equivalent of the Python
- statement: o[key]=v.
+ Map the object, key, to the value, v. Returns
+ -1 on failure. This is the equivalent of the Python
+ statement: o[key]=v.
*/
diff --git a/Include/ceval.h b/Include/ceval.h
index 7bd8179..6d240e5 100644
--- a/Include/ceval.h
+++ b/Include/ceval.h
@@ -8,7 +8,7 @@ extern "C" {
/* Interface to random parts in ceval.c */
PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
- PyObject *, PyObject *, PyObject *);
+ PyObject *, PyObject *, PyObject *);
/* DLL-level Backwards compatibility: */
#undef PyEval_CallObject
@@ -16,7 +16,7 @@ PyAPI_FUNC(PyObject *) PyEval_CallObject(PyObject *, PyObject *);
/* Inline this */
#define PyEval_CallObject(func,arg) \
- PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
+ PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
const char *format, ...);
@@ -49,7 +49,7 @@ PyAPI_FUNC(int) Py_MakePendingCalls(void);
exceeds the current recursion limit. It raises a RuntimeError, and sets
the "overflowed" flag in the thread state structure. This flag
temporarily *disables* the normal protection; this allows cleanup code
- to potentially outgrow the recursion limit while processing the
+ to potentially outgrow the recursion limit while processing the
RuntimeError.
* "last chance" anti-recursion protection is triggered when the recursion
level exceeds "current recursion limit + 50". By construction, this
@@ -71,12 +71,12 @@ PyAPI_FUNC(void) Py_SetRecursionLimit(int);
PyAPI_FUNC(int) Py_GetRecursionLimit(void);
#define Py_EnterRecursiveCall(where) \
- (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
- _Py_CheckRecursiveCall(where))
-#define Py_LeaveRecursiveCall() \
+ (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
+ _Py_CheckRecursiveCall(where))
+#define Py_LeaveRecursiveCall() \
do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth)) \
- PyThreadState_GET()->overflowed = 0; \
- } while(0)
+ PyThreadState_GET()->overflowed = 0; \
+ } while(0)
PyAPI_FUNC(int) _Py_CheckRecursiveCall(char *where);
PyAPI_DATA(int) _Py_CheckRecursionLimit;
@@ -87,15 +87,15 @@ PyAPI_DATA(int) _Py_CheckRecursionLimit;
of _Py_CheckRecursionLimit for _Py_MakeEndRecCheck() to function properly.
*/
# define _Py_MakeRecCheck(x) \
- (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1))
+ (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1))
#else
# define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
#endif
#define _Py_MakeEndRecCheck(x) \
- (--(x) < ((_Py_CheckRecursionLimit > 100) \
- ? (_Py_CheckRecursionLimit - 50) \
- : (3 * (_Py_CheckRecursionLimit >> 2))))
+ (--(x) < ((_Py_CheckRecursionLimit > 100) \
+ ? (_Py_CheckRecursionLimit - 50) \
+ : (3 * (_Py_CheckRecursionLimit >> 2))))
#define Py_ALLOW_RECURSION \
do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
@@ -122,31 +122,31 @@ PyAPI_DATA(int) _Py_CheckInterval;
that lasts a long time and doesn't touch Python data) can allow other
threads to run as follows:
- ...preparations here...
- Py_BEGIN_ALLOW_THREADS
- ...blocking system call here...
- Py_END_ALLOW_THREADS
- ...interpret result here...
+ ...preparations here...
+ Py_BEGIN_ALLOW_THREADS
+ ...blocking system call here...
+ Py_END_ALLOW_THREADS
+ ...interpret result here...
The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
{}-surrounded block.
To leave the block in the middle (e.g., with return), you must insert
a line containing Py_BLOCK_THREADS before the return, e.g.
- if (...premature_exit...) {
- Py_BLOCK_THREADS
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
+ if (...premature_exit...) {
+ Py_BLOCK_THREADS
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
An alternative is:
- Py_BLOCK_THREADS
- if (...premature_exit...) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_UNBLOCK_THREADS
+ Py_BLOCK_THREADS
+ if (...premature_exit...) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_UNBLOCK_THREADS
For convenience, that the value of 'errno' is restored across
Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
@@ -175,12 +175,12 @@ PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
PyAPI_FUNC(void) PyEval_ReInitThreads(void);
#define Py_BEGIN_ALLOW_THREADS { \
- PyThreadState *_save; \
- _save = PyEval_SaveThread();
-#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
-#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
-#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
- }
+ PyThreadState *_save; \
+ _save = PyEval_SaveThread();
+#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
+#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
+#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
+ }
#else /* !WITH_THREAD */
diff --git a/Include/datetime.h b/Include/datetime.h
index 4b506b2..cb96799 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -11,13 +11,13 @@ extern "C" {
* big-endian, unless otherwise noted:
*
* byte offset
- * 0 year 2 bytes, 1-9999
- * 2 month 1 byte, 1-12
- * 3 day 1 byte, 1-31
- * 4 hour 1 byte, 0-23
- * 5 minute 1 byte, 0-59
- * 6 second 1 byte, 0-59
- * 7 usecond 3 bytes, 0-999999
+ * 0 year 2 bytes, 1-9999
+ * 2 month 1 byte, 1-12
+ * 3 day 1 byte, 1-31
+ * 4 hour 1 byte, 0-23
+ * 5 minute 1 byte, 0-59
+ * 6 second 1 byte, 0-59
+ * 7 usecond 3 bytes, 0-999999
* 10
*/
@@ -33,26 +33,26 @@ extern "C" {
typedef struct
{
- PyObject_HEAD
- long hashcode; /* -1 when unknown */
- int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
- int seconds; /* 0 <= seconds < 24*3600 is invariant */
- int microseconds; /* 0 <= microseconds < 1000000 is invariant */
+ PyObject_HEAD
+ long hashcode; /* -1 when unknown */
+ int days; /* -MAX_DELTA_DAYS <= days <= MAX_DELTA_DAYS */
+ int seconds; /* 0 <= seconds < 24*3600 is invariant */
+ int microseconds; /* 0 <= microseconds < 1000000 is invariant */
} PyDateTime_Delta;
typedef struct
{
- PyObject_HEAD /* a pure abstract base clase */
+ PyObject_HEAD /* a pure abstract base clase */
} PyDateTime_TZInfo;
/* The datetime and time types have hashcodes, and an optional tzinfo member,
* present if and only if hastzinfo is true.
*/
-#define _PyTZINFO_HEAD \
- PyObject_HEAD \
- long hashcode; \
- char hastzinfo; /* boolean flag */
+#define _PyTZINFO_HEAD \
+ PyObject_HEAD \
+ long hashcode; \
+ char hastzinfo; /* boolean flag */
/* No _PyDateTime_BaseTZInfo is allocated; it's just to have something
* convenient to cast to, when getting at the hastzinfo member of objects
@@ -60,7 +60,7 @@ typedef struct
*/
typedef struct
{
- _PyTZINFO_HEAD
+ _PyTZINFO_HEAD
} _PyDateTime_BaseTZInfo;
/* All time objects are of PyDateTime_TimeType, but that can be allocated
@@ -69,20 +69,20 @@ typedef struct
* internal struct used to allocate the right amount of space for the
* "without" case.
*/
-#define _PyDateTime_TIMEHEAD \
- _PyTZINFO_HEAD \
- unsigned char data[_PyDateTime_TIME_DATASIZE];
+#define _PyDateTime_TIMEHEAD \
+ _PyTZINFO_HEAD \
+ unsigned char data[_PyDateTime_TIME_DATASIZE];
typedef struct
{
- _PyDateTime_TIMEHEAD
-} _PyDateTime_BaseTime; /* hastzinfo false */
+ _PyDateTime_TIMEHEAD
+} _PyDateTime_BaseTime; /* hastzinfo false */
typedef struct
{
- _PyDateTime_TIMEHEAD
- PyObject *tzinfo;
-} PyDateTime_Time; /* hastzinfo true */
+ _PyDateTime_TIMEHEAD
+ PyObject *tzinfo;
+} PyDateTime_Time; /* hastzinfo true */
/* All datetime objects are of PyDateTime_DateTimeType, but that can be
@@ -92,48 +92,48 @@ typedef struct
*/
typedef struct
{
- _PyTZINFO_HEAD
- unsigned char data[_PyDateTime_DATE_DATASIZE];
+ _PyTZINFO_HEAD
+ unsigned char data[_PyDateTime_DATE_DATASIZE];
} PyDateTime_Date;
-#define _PyDateTime_DATETIMEHEAD \
- _PyTZINFO_HEAD \
- unsigned char data[_PyDateTime_DATETIME_DATASIZE];
+#define _PyDateTime_DATETIMEHEAD \
+ _PyTZINFO_HEAD \
+ unsigned char data[_PyDateTime_DATETIME_DATASIZE];
typedef struct
{
- _PyDateTime_DATETIMEHEAD
-} _PyDateTime_BaseDateTime; /* hastzinfo false */
+ _PyDateTime_DATETIMEHEAD
+} _PyDateTime_BaseDateTime; /* hastzinfo false */
typedef struct
{
- _PyDateTime_DATETIMEHEAD
- PyObject *tzinfo;
-} PyDateTime_DateTime; /* hastzinfo true */
+ _PyDateTime_DATETIMEHEAD
+ PyObject *tzinfo;
+} PyDateTime_DateTime; /* hastzinfo true */
/* Apply for date and datetime instances. */
#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
- ((PyDateTime_Date*)o)->data[1])
+ ((PyDateTime_Date*)o)->data[1])
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
#define PyDateTime_GET_DAY(o) (((PyDateTime_Date*)o)->data[3])
#define PyDateTime_DATE_GET_HOUR(o) (((PyDateTime_DateTime*)o)->data[4])
#define PyDateTime_DATE_GET_MINUTE(o) (((PyDateTime_DateTime*)o)->data[5])
#define PyDateTime_DATE_GET_SECOND(o) (((PyDateTime_DateTime*)o)->data[6])
-#define PyDateTime_DATE_GET_MICROSECOND(o) \
- ((((PyDateTime_DateTime*)o)->data[7] << 16) | \
- (((PyDateTime_DateTime*)o)->data[8] << 8) | \
- ((PyDateTime_DateTime*)o)->data[9])
+#define PyDateTime_DATE_GET_MICROSECOND(o) \
+ ((((PyDateTime_DateTime*)o)->data[7] << 16) | \
+ (((PyDateTime_DateTime*)o)->data[8] << 8) | \
+ ((PyDateTime_DateTime*)o)->data[9])
/* Apply for time instances. */
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
#define PyDateTime_TIME_GET_MINUTE(o) (((PyDateTime_Time*)o)->data[1])
#define PyDateTime_TIME_GET_SECOND(o) (((PyDateTime_Time*)o)->data[2])
-#define PyDateTime_TIME_GET_MICROSECOND(o) \
- ((((PyDateTime_Time*)o)->data[3] << 16) | \
- (((PyDateTime_Time*)o)->data[4] << 8) | \
- ((PyDateTime_Time*)o)->data[5])
+#define PyDateTime_TIME_GET_MICROSECOND(o) \
+ ((((PyDateTime_Time*)o)->data[3] << 16) | \
+ (((PyDateTime_Time*)o)->data[4] << 8) | \
+ ((PyDateTime_Time*)o)->data[5])
/* Define structure for C API. */
@@ -148,7 +148,7 @@ typedef struct {
/* constructors */
PyObject *(*Date_FromDate)(int, int, int, PyTypeObject*);
PyObject *(*DateTime_FromDateAndTime)(int, int, int, int, int, int, int,
- PyObject*, PyTypeObject*);
+ PyObject*, PyTypeObject*);
PyObject *(*Time_FromTime)(int, int, int, int, PyObject*, PyTypeObject*);
PyObject *(*Delta_FromDelta)(int, int, int, int, PyTypeObject*);
@@ -185,7 +185,7 @@ typedef struct {
static PyDateTime_CAPI *PyDateTimeAPI;
#define PyDateTime_IMPORT \
- PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
+ PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
/* Macros for type checking when not building the Python core. */
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
@@ -205,30 +205,30 @@ static PyDateTime_CAPI *PyDateTimeAPI;
/* Macros for accessing constructors in a simplified fashion. */
#define PyDate_FromDate(year, month, day) \
- PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
+ PyDateTimeAPI->Date_FromDate(year, month, day, PyDateTimeAPI->DateType)
#define PyDateTime_FromDateAndTime(year, month, day, hour, min, sec, usec) \
- PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
- min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
+ PyDateTimeAPI->DateTime_FromDateAndTime(year, month, day, hour, \
+ min, sec, usec, Py_None, PyDateTimeAPI->DateTimeType)
#define PyTime_FromTime(hour, minute, second, usecond) \
- PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
- Py_None, PyDateTimeAPI->TimeType)
+ PyDateTimeAPI->Time_FromTime(hour, minute, second, usecond, \
+ Py_None, PyDateTimeAPI->TimeType)
#define PyDelta_FromDSU(days, seconds, useconds) \
- PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
- PyDateTimeAPI->DeltaType)
+ PyDateTimeAPI->Delta_FromDelta(days, seconds, useconds, 1, \
+ PyDateTimeAPI->DeltaType)
/* Macros supporting the DB API. */
#define PyDateTime_FromTimestamp(args) \
- PyDateTimeAPI->DateTime_FromTimestamp( \
- (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
+ PyDateTimeAPI->DateTime_FromTimestamp( \
+ (PyObject*) (PyDateTimeAPI->DateTimeType), args, NULL)
#define PyDate_FromTimestamp(args) \
- PyDateTimeAPI->Date_FromTimestamp( \
- (PyObject*) (PyDateTimeAPI->DateType), args)
+ PyDateTimeAPI->Date_FromTimestamp( \
+ (PyObject*) (PyDateTimeAPI->DateType), args)
-#endif /* Py_BUILD_CORE */
+#endif /* Py_BUILD_CORE */
#ifdef __cplusplus
}
diff --git a/Include/descrobject.h b/Include/descrobject.h
index f06b421..d2fe109 100644
--- a/Include/descrobject.h
+++ b/Include/descrobject.h
@@ -9,27 +9,27 @@ typedef PyObject *(*getter)(PyObject *, void *);
typedef int (*setter)(PyObject *, PyObject *, void *);
typedef struct PyGetSetDef {
- char *name;
- getter get;
- setter set;
- char *doc;
- void *closure;
+ char *name;
+ getter get;
+ setter set;
+ char *doc;
+ void *closure;
} PyGetSetDef;
typedef PyObject *(*wrapperfunc)(PyObject *self, PyObject *args,
- void *wrapped);
+ void *wrapped);
typedef PyObject *(*wrapperfunc_kwds)(PyObject *self, PyObject *args,
- void *wrapped, PyObject *kwds);
+ void *wrapped, PyObject *kwds);
struct wrapperbase {
- char *name;
- int offset;
- void *function;
- wrapperfunc wrapper;
- char *doc;
- int flags;
- PyObject *name_strobj;
+ char *name;
+ int offset;
+ void *function;
+ wrapperfunc wrapper;
+ char *doc;
+ int flags;
+ PyObject *name_strobj;
};
/* Flags for above struct */
@@ -38,33 +38,33 @@ struct wrapperbase {
/* Various kinds of descriptor objects */
#define PyDescr_COMMON \
- PyObject_HEAD \
- PyTypeObject *d_type; \
- PyObject *d_name
+ PyObject_HEAD \
+ PyTypeObject *d_type; \
+ PyObject *d_name
typedef struct {
- PyDescr_COMMON;
+ PyDescr_COMMON;
} PyDescrObject;
typedef struct {
- PyDescr_COMMON;
- PyMethodDef *d_method;
+ PyDescr_COMMON;
+ PyMethodDef *d_method;
} PyMethodDescrObject;
typedef struct {
- PyDescr_COMMON;
- struct PyMemberDef *d_member;
+ PyDescr_COMMON;
+ struct PyMemberDef *d_member;
} PyMemberDescrObject;
typedef struct {
- PyDescr_COMMON;
- PyGetSetDef *d_getset;
+ PyDescr_COMMON;
+ PyGetSetDef *d_getset;
} PyGetSetDescrObject;
typedef struct {
- PyDescr_COMMON;
- struct wrapperbase *d_base;
- void *d_wrapped; /* This can be any function pointer */
+ PyDescr_COMMON;
+ struct wrapperbase *d_base;
+ void *d_wrapped; /* This can be any function pointer */
} PyWrapperDescrObject;
PyAPI_DATA(PyTypeObject) PyClassMethodDescr_Type;
@@ -77,11 +77,11 @@ PyAPI_DATA(PyTypeObject) PyDictProxy_Type;
PyAPI_FUNC(PyObject *) PyDescr_NewMethod(PyTypeObject *, PyMethodDef *);
PyAPI_FUNC(PyObject *) PyDescr_NewClassMethod(PyTypeObject *, PyMethodDef *);
PyAPI_FUNC(PyObject *) PyDescr_NewMember(PyTypeObject *,
- struct PyMemberDef *);
+ struct PyMemberDef *);
PyAPI_FUNC(PyObject *) PyDescr_NewGetSet(PyTypeObject *,
- struct PyGetSetDef *);
+ struct PyGetSetDef *);
PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
- struct wrapperbase *, void *);
+ struct wrapperbase *, void *);
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)
PyAPI_FUNC(PyObject *) PyDictProxy_New(PyObject *);
diff --git a/Include/dictobject.h b/Include/dictobject.h
index d8c409e..d1b576c 100644
--- a/Include/dictobject.h
+++ b/Include/dictobject.h
@@ -48,13 +48,13 @@ meaning otherwise.
#define PyDict_MINSIZE 8
typedef struct {
- /* Cached hash code of me_key. Note that hash codes are C longs.
- * We have to use Py_ssize_t instead because dict_popitem() abuses
- * me_hash to hold a search finger.
- */
- Py_ssize_t me_hash;
- PyObject *me_key;
- PyObject *me_value;
+ /* Cached hash code of me_key. Note that hash codes are C longs.
+ * We have to use Py_ssize_t instead because dict_popitem() abuses
+ * me_hash to hold a search finger.
+ */
+ Py_ssize_t me_hash;
+ PyObject *me_key;
+ PyObject *me_value;
} PyDictEntry;
/*
@@ -68,24 +68,24 @@ it's two-thirds full.
*/
typedef struct _dictobject PyDictObject;
struct _dictobject {
- PyObject_HEAD
- Py_ssize_t ma_fill; /* # Active + # Dummy */
- Py_ssize_t ma_used; /* # Active */
-
- /* The table contains ma_mask + 1 slots, and that's a power of 2.
- * We store the mask instead of the size because the mask is more
- * frequently needed.
- */
- Py_ssize_t ma_mask;
-
- /* ma_table points to ma_smalltable for small tables, else to
- * additional malloc'ed memory. ma_table is never NULL! This rule
- * saves repeated runtime null-tests in the workhorse getitem and
- * setitem calls.
- */
- PyDictEntry *ma_table;
- PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);
- PyDictEntry ma_smalltable[PyDict_MINSIZE];
+ PyObject_HEAD
+ Py_ssize_t ma_fill; /* # Active + # Dummy */
+ Py_ssize_t ma_used; /* # Active */
+
+ /* The table contains ma_mask + 1 slots, and that's a power of 2.
+ * We store the mask instead of the size because the mask is more
+ * frequently needed.
+ */
+ Py_ssize_t ma_mask;
+
+ /* ma_table points to ma_smalltable for small tables, else to
+ * additional malloc'ed memory. ma_table is never NULL! This rule
+ * saves repeated runtime null-tests in the workhorse getitem and
+ * setitem calls.
+ */
+ PyDictEntry *ma_table;
+ PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);
+ PyDictEntry ma_smalltable[PyDict_MINSIZE];
};
PyAPI_DATA(PyTypeObject) PyDict_Type;
@@ -104,7 +104,7 @@ PyAPI_DATA(PyTypeObject) PyDictValues_Type;
#define PyDictValues_Check(op) (Py_TYPE(op) == &PyDictValues_Type)
/* This excludes Values, since they are not sets. */
# define PyDictViewSet_Check(op) \
- (PyDictKeys_Check(op) || PyDictItems_Check(op))
+ (PyDictKeys_Check(op) || PyDictItems_Check(op))
PyAPI_FUNC(PyObject *) PyDict_New(void);
@@ -114,9 +114,9 @@ PyAPI_FUNC(int) PyDict_SetItem(PyObject *mp, PyObject *key, PyObject *item);
PyAPI_FUNC(int) PyDict_DelItem(PyObject *mp, PyObject *key);
PyAPI_FUNC(void) PyDict_Clear(PyObject *mp);
PyAPI_FUNC(int) PyDict_Next(
- PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
+ PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value);
PyAPI_FUNC(int) _PyDict_Next(
- PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, long *hash);
+ PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, long *hash);
PyAPI_FUNC(PyObject *) PyDict_Keys(PyObject *mp);
PyAPI_FUNC(PyObject *) PyDict_Values(PyObject *mp);
PyAPI_FUNC(PyObject *) PyDict_Items(PyObject *mp);
@@ -136,8 +136,8 @@ PyAPI_FUNC(int) PyDict_Update(PyObject *mp, PyObject *other);
dict.update(other) is equivalent to PyDict_Merge(dict, other, 1).
*/
PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
- PyObject *other,
- int override);
+ PyObject *other,
+ int override);
/* PyDict_MergeFromSeq2 updates/merges from an iterable object producing
iterable objects of length 2. If override is true, the last occurrence
@@ -145,8 +145,8 @@ PyAPI_FUNC(int) PyDict_Merge(PyObject *mp,
is equivalent to dict={}; PyDict_MergeFromSeq(dict, seq2, 1).
*/
PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
- PyObject *seq2,
- int override);
+ PyObject *seq2,
+ int override);
PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
diff --git a/Include/object.h b/Include/object.h
index 772bbb3..6e744bc 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -63,9 +63,9 @@ whose size is determined when the object is allocated.
#ifdef Py_TRACE_REFS
/* Define pointers to support a doubly-linked list of all live heap objects. */
-#define _PyObject_HEAD_EXTRA \
- struct _object *_ob_next; \
- struct _object *_ob_prev;
+#define _PyObject_HEAD_EXTRA \
+ struct _object *_ob_next; \
+ struct _object *_ob_prev;
#define _PyObject_EXTRA_INIT 0, 0,
@@ -75,14 +75,14 @@ whose size is determined when the object is allocated.
#endif
/* PyObject_HEAD defines the initial segment of every PyObject. */
-#define PyObject_HEAD PyObject ob_base;
+#define PyObject_HEAD PyObject ob_base;
-#define PyObject_HEAD_INIT(type) \
- { _PyObject_EXTRA_INIT \
- 1, type },
+#define PyObject_HEAD_INIT(type) \
+ { _PyObject_EXTRA_INIT \
+ 1, type },
-#define PyVarObject_HEAD_INIT(type, size) \
- { PyObject_HEAD_INIT(type) size },
+#define PyVarObject_HEAD_INIT(type, size) \
+ { PyObject_HEAD_INIT(type) size },
/* PyObject_VAR_HEAD defines the initial segment of all variable-size
* container objects. These end with a declaration of an array with 1
@@ -99,19 +99,19 @@ whose size is determined when the object is allocated.
* in addition, be cast to PyVarObject*.
*/
typedef struct _object {
- _PyObject_HEAD_EXTRA
- Py_ssize_t ob_refcnt;
- struct _typeobject *ob_type;
+ _PyObject_HEAD_EXTRA
+ Py_ssize_t ob_refcnt;
+ struct _typeobject *ob_type;
} PyObject;
typedef struct {
- PyObject ob_base;
- Py_ssize_t ob_size; /* Number of items in variable part */
+ PyObject ob_base;
+ Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;
-#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
-#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
-#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
+#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
+#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
+#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
/*
Type objects contain a string containing the type name (to help somewhat
@@ -142,26 +142,26 @@ typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
/* buffer interface */
typedef struct bufferinfo {
- void *buf;
- PyObject *obj; /* owned reference */
- Py_ssize_t len;
- Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
- pointed to by strides in simple case.*/
- int readonly;
- int ndim;
- char *format;
- Py_ssize_t *shape;
- Py_ssize_t *strides;
- Py_ssize_t *suboffsets;
- Py_ssize_t smalltable[2]; /* static store for shape and strides of
- mono-dimensional buffers. */
- void *internal;
+ void *buf;
+ PyObject *obj; /* owned reference */
+ Py_ssize_t len;
+ Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
+ pointed to by strides in simple case.*/
+ int readonly;
+ int ndim;
+ char *format;
+ Py_ssize_t *shape;
+ Py_ssize_t *strides;
+ Py_ssize_t *suboffsets;
+ Py_ssize_t smalltable[2]; /* static store for shape and strides of
+ mono-dimensional buffers. */
+ void *internal;
} Py_buffer;
typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
- /* Flags for getting buffers */
+ /* Flags for getting buffers */
#define PyBUF_SIMPLE 0
#define PyBUF_WRITABLE 0x0001
/* we used to include an E, backwards compatible alias */
@@ -198,67 +198,67 @@ typedef int (*visitproc)(PyObject *, void *);
typedef int (*traverseproc)(PyObject *, visitproc, void *);
typedef struct {
- /* Number implementations must check *both*
- arguments for proper type and implement the necessary conversions
- in the slot functions themselves. */
-
- binaryfunc nb_add;
- binaryfunc nb_subtract;
- binaryfunc nb_multiply;
- binaryfunc nb_remainder;
- binaryfunc nb_divmod;
- ternaryfunc nb_power;
- unaryfunc nb_negative;
- unaryfunc nb_positive;
- unaryfunc nb_absolute;
- inquiry nb_bool;
- unaryfunc nb_invert;
- binaryfunc nb_lshift;
- binaryfunc nb_rshift;
- binaryfunc nb_and;
- binaryfunc nb_xor;
- binaryfunc nb_or;
- unaryfunc nb_int;
- void *nb_reserved; /* the slot formerly known as nb_long */
- unaryfunc nb_float;
-
- binaryfunc nb_inplace_add;
- binaryfunc nb_inplace_subtract;
- binaryfunc nb_inplace_multiply;
- binaryfunc nb_inplace_remainder;
- ternaryfunc nb_inplace_power;
- binaryfunc nb_inplace_lshift;
- binaryfunc nb_inplace_rshift;
- binaryfunc nb_inplace_and;
- binaryfunc nb_inplace_xor;
- binaryfunc nb_inplace_or;
-
- binaryfunc nb_floor_divide;
- binaryfunc nb_true_divide;
- binaryfunc nb_inplace_floor_divide;
- binaryfunc nb_inplace_true_divide;
-
- unaryfunc nb_index;
+ /* Number implementations must check *both*
+ arguments for proper type and implement the necessary conversions
+ in the slot functions themselves. */
+
+ binaryfunc nb_add;
+ binaryfunc nb_subtract;
+ binaryfunc nb_multiply;
+ binaryfunc nb_remainder;
+ binaryfunc nb_divmod;
+ ternaryfunc nb_power;
+ unaryfunc nb_negative;
+ unaryfunc nb_positive;
+ unaryfunc nb_absolute;
+ inquiry nb_bool;
+ unaryfunc nb_invert;
+ binaryfunc nb_lshift;
+ binaryfunc nb_rshift;
+ binaryfunc nb_and;
+ binaryfunc nb_xor;
+ binaryfunc nb_or;
+ unaryfunc nb_int;
+ void *nb_reserved; /* the slot formerly known as nb_long */
+ unaryfunc nb_float;
+
+ binaryfunc nb_inplace_add;
+ binaryfunc nb_inplace_subtract;
+ binaryfunc nb_inplace_multiply;
+ binaryfunc nb_inplace_remainder;
+ ternaryfunc nb_inplace_power;
+ binaryfunc nb_inplace_lshift;
+ binaryfunc nb_inplace_rshift;
+ binaryfunc nb_inplace_and;
+ binaryfunc nb_inplace_xor;
+ binaryfunc nb_inplace_or;
+
+ binaryfunc nb_floor_divide;
+ binaryfunc nb_true_divide;
+ binaryfunc nb_inplace_floor_divide;
+ binaryfunc nb_inplace_true_divide;
+
+ unaryfunc nb_index;
} PyNumberMethods;
typedef struct {
- lenfunc sq_length;
- binaryfunc sq_concat;
- ssizeargfunc sq_repeat;
- ssizeargfunc sq_item;
- void *was_sq_slice;
- ssizeobjargproc sq_ass_item;
- void *was_sq_ass_slice;
- objobjproc sq_contains;
-
- binaryfunc sq_inplace_concat;
- ssizeargfunc sq_inplace_repeat;
+ lenfunc sq_length;
+ binaryfunc sq_concat;
+ ssizeargfunc sq_repeat;
+ ssizeargfunc sq_item;
+ void *was_sq_slice;
+ ssizeobjargproc sq_ass_item;
+ void *was_sq_ass_slice;
+ objobjproc sq_contains;
+
+ binaryfunc sq_inplace_concat;
+ ssizeargfunc sq_inplace_repeat;
} PySequenceMethods;
typedef struct {
- lenfunc mp_length;
- binaryfunc mp_subscript;
- objobjargproc mp_ass_subscript;
+ lenfunc mp_length;
+ binaryfunc mp_subscript;
+ objobjargproc mp_ass_subscript;
} PyMappingMethods;
@@ -286,109 +286,109 @@ typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
typedef struct _typeobject {
- PyObject_VAR_HEAD
- const char *tp_name; /* For printing, in format "<module>.<name>" */
- Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
-
- /* Methods to implement standard operations */
-
- destructor tp_dealloc;
- printfunc tp_print;
- getattrfunc tp_getattr;
- setattrfunc tp_setattr;
- void *tp_reserved; /* formerly known as tp_compare */
- reprfunc tp_repr;
-
- /* Method suites for standard classes */
-
- PyNumberMethods *tp_as_number;
- PySequenceMethods *tp_as_sequence;
- PyMappingMethods *tp_as_mapping;
-
- /* More standard operations (here for binary compatibility) */
-
- hashfunc tp_hash;
- ternaryfunc tp_call;
- reprfunc tp_str;
- getattrofunc tp_getattro;
- setattrofunc tp_setattro;
-
- /* Functions to access object as input/output buffer */
- PyBufferProcs *tp_as_buffer;
-
- /* Flags to define presence of optional/expanded features */
- long tp_flags;
-
- const char *tp_doc; /* Documentation string */
-
- /* Assigned meaning in release 2.0 */
- /* call function for all accessible objects */
- traverseproc tp_traverse;
-
- /* delete references to contained objects */
- inquiry tp_clear;
-
- /* Assigned meaning in release 2.1 */
- /* rich comparisons */
- richcmpfunc tp_richcompare;
-
- /* weak reference enabler */
- Py_ssize_t tp_weaklistoffset;
-
- /* Iterators */
- getiterfunc tp_iter;
- iternextfunc tp_iternext;
-
- /* Attribute descriptor and subclassing stuff */
- struct PyMethodDef *tp_methods;
- struct PyMemberDef *tp_members;
- struct PyGetSetDef *tp_getset;
- struct _typeobject *tp_base;
- PyObject *tp_dict;
- descrgetfunc tp_descr_get;
- descrsetfunc tp_descr_set;
- Py_ssize_t tp_dictoffset;
- initproc tp_init;
- allocfunc tp_alloc;
- newfunc tp_new;
- freefunc tp_free; /* Low-level free-memory routine */
- inquiry tp_is_gc; /* For PyObject_IS_GC */
- PyObject *tp_bases;
- PyObject *tp_mro; /* method resolution order */
- PyObject *tp_cache;
- PyObject *tp_subclasses;
- PyObject *tp_weaklist;
- destructor tp_del;
-
- /* Type attribute cache version tag. Added in version 2.6 */
- unsigned int tp_version_tag;
+ PyObject_VAR_HEAD
+ const char *tp_name; /* For printing, in format "<module>.<name>" */
+ Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
+
+ /* Methods to implement standard operations */
+
+ destructor tp_dealloc;
+ printfunc tp_print;
+ getattrfunc tp_getattr;
+ setattrfunc tp_setattr;
+ void *tp_reserved; /* formerly known as tp_compare */
+ reprfunc tp_repr;
+
+ /* Method suites for standard classes */
+
+ PyNumberMethods *tp_as_number;
+ PySequenceMethods *tp_as_sequence;
+ PyMappingMethods *tp_as_mapping;
+
+ /* More standard operations (here for binary compatibility) */
+
+ hashfunc tp_hash;
+ ternaryfunc tp_call;
+ reprfunc tp_str;
+ getattrofunc tp_getattro;
+ setattrofunc tp_setattro;
+
+ /* Functions to access object as input/output buffer */
+ PyBufferProcs *tp_as_buffer;
+
+ /* Flags to define presence of optional/expanded features */
+ long tp_flags;
+
+ const char *tp_doc; /* Documentation string */
+
+ /* Assigned meaning in release 2.0 */
+ /* call function for all accessible objects */
+ traverseproc tp_traverse;
+
+ /* delete references to contained objects */
+ inquiry tp_clear;
+
+ /* Assigned meaning in release 2.1 */
+ /* rich comparisons */
+ richcmpfunc tp_richcompare;
+
+ /* weak reference enabler */
+ Py_ssize_t tp_weaklistoffset;
+
+ /* Iterators */
+ getiterfunc tp_iter;
+ iternextfunc tp_iternext;
+
+ /* Attribute descriptor and subclassing stuff */
+ struct PyMethodDef *tp_methods;
+ struct PyMemberDef *tp_members;
+ struct PyGetSetDef *tp_getset;
+ struct _typeobject *tp_base;
+ PyObject *tp_dict;
+ descrgetfunc tp_descr_get;
+ descrsetfunc tp_descr_set;
+ Py_ssize_t tp_dictoffset;
+ initproc tp_init;
+ allocfunc tp_alloc;
+ newfunc tp_new;
+ freefunc tp_free; /* Low-level free-memory routine */
+ inquiry tp_is_gc; /* For PyObject_IS_GC */
+ PyObject *tp_bases;
+ PyObject *tp_mro; /* method resolution order */
+ PyObject *tp_cache;
+ PyObject *tp_subclasses;
+ PyObject *tp_weaklist;
+ destructor tp_del;
+
+ /* Type attribute cache version tag. Added in version 2.6 */
+ unsigned int tp_version_tag;
#ifdef COUNT_ALLOCS
- /* these must be last and never explicitly initialized */
- Py_ssize_t tp_allocs;
- Py_ssize_t tp_frees;
- Py_ssize_t tp_maxalloc;
- struct _typeobject *tp_prev;
- struct _typeobject *tp_next;
+ /* these must be last and never explicitly initialized */
+ Py_ssize_t tp_allocs;
+ Py_ssize_t tp_frees;
+ Py_ssize_t tp_maxalloc;
+ struct _typeobject *tp_prev;
+ struct _typeobject *tp_next;
#endif
} PyTypeObject;
/* The *real* layout of a type object when allocated on the heap */
typedef struct _heaptypeobject {
- /* Note: there's a dependency on the order of these members
- in slotptr() in typeobject.c . */
- PyTypeObject ht_type;
- PyNumberMethods as_number;
- PyMappingMethods as_mapping;
- PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
- so that the mapping wins when both
- the mapping and the sequence define
- a given operator (e.g. __getitem__).
- see add_operators() in typeobject.c . */
- PyBufferProcs as_buffer;
- PyObject *ht_name, *ht_slots;
- /* here are optional user slots, followed by the members. */
+ /* Note: there's a dependency on the order of these members
+ in slotptr() in typeobject.c . */
+ PyTypeObject ht_type;
+ PyNumberMethods as_number;
+ PyMappingMethods as_mapping;
+ PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
+ so that the mapping wins when both
+ the mapping and the sequence define
+ a given operator (e.g. __getitem__).
+ see add_operators() in typeobject.c . */
+ PyBufferProcs as_buffer;
+ PyObject *ht_name, *ht_slots;
+ /* here are optional user slots, followed by the members. */
} PyHeapTypeObject;
/* access macro to the members which are floating "behind" the object */
@@ -399,20 +399,20 @@ typedef struct _heaptypeobject {
/* Generic type check */
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
#define PyObject_TypeCheck(ob, tp) \
- (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
+ (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
#define PyType_Check(op) \
- PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
+ PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
#define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
- PyObject *, PyObject *);
+ PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, char *, PyObject **);
PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
@@ -439,7 +439,7 @@ PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
- PyObject *, PyObject *);
+ PyObject *, PyObject *);
PyAPI_FUNC(long) PyObject_Hash(PyObject *);
PyAPI_FUNC(long) PyObject_HashNotImplemented(PyObject *);
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
@@ -469,7 +469,7 @@ PyAPI_FUNC(long) _Py_HashPointer(void*);
#define PyObject_REPR(obj) _PyUnicode_AsString(PyObject_Repr(obj))
/* Flag bits for printing: */
-#define Py_PRINT_RAW 1 /* No string quotes etc. */
+#define Py_PRINT_RAW 1 /* No string quotes etc. */
/*
`Type flags (tp_flags)
@@ -524,20 +524,20 @@ given type object has a specified feature.
#define Py_TPFLAGS_IS_ABSTRACT (1L<<20)
/* These flags are used to determine if a type is a subclass. */
-#define Py_TPFLAGS_INT_SUBCLASS (1L<<23)
-#define Py_TPFLAGS_LONG_SUBCLASS (1L<<24)
-#define Py_TPFLAGS_LIST_SUBCLASS (1L<<25)
-#define Py_TPFLAGS_TUPLE_SUBCLASS (1L<<26)
-#define Py_TPFLAGS_BYTES_SUBCLASS (1L<<27)
-#define Py_TPFLAGS_UNICODE_SUBCLASS (1L<<28)
-#define Py_TPFLAGS_DICT_SUBCLASS (1L<<29)
-#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30)
-#define Py_TPFLAGS_TYPE_SUBCLASS (1L<<31)
+#define Py_TPFLAGS_INT_SUBCLASS (1L<<23)
+#define Py_TPFLAGS_LONG_SUBCLASS (1L<<24)
+#define Py_TPFLAGS_LIST_SUBCLASS (1L<<25)
+#define Py_TPFLAGS_TUPLE_SUBCLASS (1L<<26)
+#define Py_TPFLAGS_BYTES_SUBCLASS (1L<<27)
+#define Py_TPFLAGS_UNICODE_SUBCLASS (1L<<28)
+#define Py_TPFLAGS_DICT_SUBCLASS (1L<<29)
+#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30)
+#define Py_TPFLAGS_TYPE_SUBCLASS (1L<<31)
#define Py_TPFLAGS_DEFAULT ( \
- Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
- Py_TPFLAGS_HAVE_VERSION_TAG | \
- 0)
+ Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
+ Py_TPFLAGS_HAVE_VERSION_TAG | \
+ 0)
#define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
#define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
@@ -589,32 +589,32 @@ environment the global variable trick is not safe.)
#ifdef Py_REF_DEBUG
PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname,
- int lineno, PyObject *op);
+ int lineno, PyObject *op);
PyAPI_FUNC(PyObject *) _PyDict_Dummy(void);
PyAPI_FUNC(PyObject *) _PySet_Dummy(void);
PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
-#define _Py_INC_REFTOTAL _Py_RefTotal++
-#define _Py_DEC_REFTOTAL _Py_RefTotal--
-#define _Py_REF_DEBUG_COMMA ,
-#define _Py_CHECK_REFCNT(OP) \
-{ if (((PyObject*)OP)->ob_refcnt < 0) \
- _Py_NegativeRefcount(__FILE__, __LINE__, \
- (PyObject *)(OP)); \
+#define _Py_INC_REFTOTAL _Py_RefTotal++
+#define _Py_DEC_REFTOTAL _Py_RefTotal--
+#define _Py_REF_DEBUG_COMMA ,
+#define _Py_CHECK_REFCNT(OP) \
+{ if (((PyObject*)OP)->ob_refcnt < 0) \
+ _Py_NegativeRefcount(__FILE__, __LINE__, \
+ (PyObject *)(OP)); \
}
#else
#define _Py_INC_REFTOTAL
#define _Py_DEC_REFTOTAL
#define _Py_REF_DEBUG_COMMA
-#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
+#define _Py_CHECK_REFCNT(OP) /* a semicolon */;
#endif /* Py_REF_DEBUG */
#ifdef COUNT_ALLOCS
PyAPI_FUNC(void) inc_count(PyTypeObject *);
PyAPI_FUNC(void) dec_count(PyTypeObject *);
-#define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP))
-#define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP))
-#define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
-#define _Py_COUNT_ALLOCS_COMMA ,
+#define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP))
+#define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP))
+#define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
+#define _Py_COUNT_ALLOCS_COMMA ,
#else
#define _Py_INC_TPALLOCS(OP)
#define _Py_INC_TPFREES(OP)
@@ -635,30 +635,30 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
/* Without Py_TRACE_REFS, there's little enough to do that we expand code
* inline.
*/
-#define _Py_NewReference(op) ( \
- _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
- _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
- Py_REFCNT(op) = 1)
+#define _Py_NewReference(op) ( \
+ _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
+ _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
+ Py_REFCNT(op) = 1)
#define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
-#define _Py_Dealloc(op) ( \
- _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
- (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
+#define _Py_Dealloc(op) ( \
+ _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
+ (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
#endif /* !Py_TRACE_REFS */
-#define Py_INCREF(op) ( \
- _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
- ((PyObject*)(op))->ob_refcnt++)
+#define Py_INCREF(op) ( \
+ _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
+ ((PyObject*)(op))->ob_refcnt++)
-#define Py_DECREF(op) \
- do { \
- if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
- --((PyObject*)(op))->ob_refcnt != 0) \
- _Py_CHECK_REFCNT(op) \
- else \
- _Py_Dealloc((PyObject *)(op)); \
- } while (0)
+#define Py_DECREF(op) \
+ do { \
+ if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
+ --((PyObject*)(op))->ob_refcnt != 0) \
+ _Py_CHECK_REFCNT(op) \
+ else \
+ _Py_Dealloc((PyObject *)(op)); \
+ } while (0)
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
* and tp_dealloc implementatons.
@@ -694,14 +694,14 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
* Python integers aren't currently weakly referencable. Best practice is
* to use Py_CLEAR() even if you can't think of a reason for why you need to.
*/
-#define Py_CLEAR(op) \
- do { \
- if (op) { \
- PyObject *_py_tmp = (PyObject *)(op); \
- (op) = NULL; \
- Py_DECREF(_py_tmp); \
- } \
- } while (0)
+#define Py_CLEAR(op) \
+ do { \
+ if (op) { \
+ PyObject *_py_tmp = (PyObject *)(op); \
+ (op) = NULL; \
+ Py_DECREF(_py_tmp); \
+ } \
+ } while (0)
/* Macros to use in case the object pointer may be NULL: */
#define Py_XINCREF(op) do { if ((op) == NULL) ; else Py_INCREF(op); } while (0)
@@ -813,13 +813,13 @@ bracketing the body of its tp_dealloc function with a pair of macros:
static void
mytype_dealloc(mytype *p)
{
- ... declarations go here ...
+ ... declarations go here ...
- PyObject_GC_UnTrack(p); // must untrack first
- Py_TRASHCAN_SAFE_BEGIN(p)
- ... The body of the deallocator goes here, including all calls ...
- ... to Py_DECREF on contained objects. ...
- Py_TRASHCAN_SAFE_END(p)
+ PyObject_GC_UnTrack(p); // must untrack first
+ Py_TRASHCAN_SAFE_BEGIN(p)
+ ... The body of the deallocator goes here, including all calls ...
+ ... to Py_DECREF on contained objects. ...
+ Py_TRASHCAN_SAFE_END(p)
}
CAUTION: Never return from the middle of the body! If the body needs to
@@ -849,16 +849,16 @@ PyAPI_DATA(PyObject *) _PyTrash_delete_later;
#define PyTrash_UNWIND_LEVEL 50
#define Py_TRASHCAN_SAFE_BEGIN(op) \
- if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
- ++_PyTrash_delete_nesting;
- /* The body of the deallocator is here. */
+ if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
+ ++_PyTrash_delete_nesting;
+ /* The body of the deallocator is here. */
#define Py_TRASHCAN_SAFE_END(op) \
- --_PyTrash_delete_nesting; \
- if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
- _PyTrash_destroy_chain(); \
- } \
- else \
- _PyTrash_deposit_object((PyObject*)op);
+ --_PyTrash_delete_nesting; \
+ if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \
+ _PyTrash_destroy_chain(); \
+ } \
+ else \
+ _PyTrash_deposit_object((PyObject*)op);
#ifdef __cplusplus
}
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 5a27382..6d5a0c4 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -101,35 +101,35 @@ PyAPI_FUNC(void) PyObject_Free(void *);
/* Macros */
#ifdef WITH_PYMALLOC
-#ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
+#ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */
PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
-#define PyObject_MALLOC _PyObject_DebugMalloc
-#define PyObject_Malloc _PyObject_DebugMalloc
-#define PyObject_REALLOC _PyObject_DebugRealloc
-#define PyObject_Realloc _PyObject_DebugRealloc
-#define PyObject_FREE _PyObject_DebugFree
-#define PyObject_Free _PyObject_DebugFree
-
-#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
-#define PyObject_MALLOC PyObject_Malloc
-#define PyObject_REALLOC PyObject_Realloc
-#define PyObject_FREE PyObject_Free
+#define PyObject_MALLOC _PyObject_DebugMalloc
+#define PyObject_Malloc _PyObject_DebugMalloc
+#define PyObject_REALLOC _PyObject_DebugRealloc
+#define PyObject_Realloc _PyObject_DebugRealloc
+#define PyObject_FREE _PyObject_DebugFree
+#define PyObject_Free _PyObject_DebugFree
+
+#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
+#define PyObject_MALLOC PyObject_Malloc
+#define PyObject_REALLOC PyObject_Realloc
+#define PyObject_FREE PyObject_Free
#endif
-#else /* ! WITH_PYMALLOC */
-#define PyObject_MALLOC PyMem_MALLOC
-#define PyObject_REALLOC PyMem_REALLOC
-#define PyObject_FREE PyMem_FREE
+#else /* ! WITH_PYMALLOC */
+#define PyObject_MALLOC PyMem_MALLOC
+#define PyObject_REALLOC PyMem_REALLOC
+#define PyObject_FREE PyMem_FREE
-#endif /* WITH_PYMALLOC */
+#endif /* WITH_PYMALLOC */
-#define PyObject_Del PyObject_Free
-#define PyObject_DEL PyObject_FREE
+#define PyObject_Del PyObject_Free
+#define PyObject_DEL PyObject_FREE
/*
* Generic object allocator interface
@@ -144,16 +144,16 @@ PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
#define PyObject_New(type, typeobj) \
- ( (type *) _PyObject_New(typeobj) )
+ ( (type *) _PyObject_New(typeobj) )
#define PyObject_NewVar(type, typeobj, n) \
- ( (type *) _PyObject_NewVar((typeobj), (n)) )
+ ( (type *) _PyObject_NewVar((typeobj), (n)) )
/* Macros trading binary compatibility for speed. See also pymem.h.
Note that these macros expect non-NULL object pointers.*/
#define PyObject_INIT(op, typeobj) \
- ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
+ ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
#define PyObject_INIT_VAR(op, typeobj, size) \
- ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
+ ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
@@ -171,17 +171,17 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
#endif
-#define _PyObject_VAR_SIZE(typeobj, nitems) \
- (size_t) \
- ( ( (typeobj)->tp_basicsize + \
- (nitems)*(typeobj)->tp_itemsize + \
- (SIZEOF_VOID_P - 1) \
- ) & ~(SIZEOF_VOID_P - 1) \
- )
+#define _PyObject_VAR_SIZE(typeobj, nitems) \
+ (size_t) \
+ ( ( (typeobj)->tp_basicsize + \
+ (nitems)*(typeobj)->tp_itemsize + \
+ (SIZEOF_VOID_P - 1) \
+ ) & ~(SIZEOF_VOID_P - 1) \
+ )
#define PyObject_NEW(type, typeobj) \
( (type *) PyObject_Init( \
- (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
+ (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
#define PyObject_NEW_VAR(type, typeobj, n) \
( (type *) PyObject_InitVar( \
@@ -193,7 +193,7 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
distinction between two steps (at least):
1) the actual allocation of the object storage;
2) the initialization of the Python specific fields
- in this storage with PyObject_{Init, InitVar}.
+ in this storage with PyObject_{Init, InitVar}.
PyObject *
YourObject_New(...)
@@ -202,7 +202,7 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
if (op == NULL)
- return PyErr_NoMemory();
+ return PyErr_NoMemory();
PyObject_Init(op, &YourTypeStruct);
@@ -229,44 +229,44 @@ PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
/* Test if an object has a GC head */
#define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
- (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
+ (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
#define PyObject_GC_Resize(type, op, n) \
- ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
+ ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
/* for source compatibility with 2.2 */
#define _PyObject_GC_Del PyObject_GC_Del
/* GC information is stored BEFORE the object structure. */
typedef union _gc_head {
- struct {
- union _gc_head *gc_next;
- union _gc_head *gc_prev;
- Py_ssize_t gc_refs;
- } gc;
- long double dummy; /* force worst-case alignment */
+ struct {
+ union _gc_head *gc_next;
+ union _gc_head *gc_prev;
+ Py_ssize_t gc_refs;
+ } gc;
+ long double dummy; /* force worst-case alignment */
} PyGC_Head;
extern PyGC_Head *_PyGC_generation0;
#define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
-#define _PyGC_REFS_UNTRACKED (-2)
-#define _PyGC_REFS_REACHABLE (-3)
-#define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
+#define _PyGC_REFS_UNTRACKED (-2)
+#define _PyGC_REFS_REACHABLE (-3)
+#define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
/* Tell the GC to track this object. NB: While the object is tracked the
* collector it must be safe to call the ob_traverse method. */
#define _PyObject_GC_TRACK(o) do { \
- PyGC_Head *g = _Py_AS_GC(o); \
- if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
- Py_FatalError("GC object already tracked"); \
- g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
- g->gc.gc_next = _PyGC_generation0; \
- g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
- g->gc.gc_prev->gc.gc_next = g; \
- _PyGC_generation0->gc.gc_prev = g; \
+ PyGC_Head *g = _Py_AS_GC(o); \
+ if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
+ Py_FatalError("GC object already tracked"); \
+ g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
+ g->gc.gc_next = _PyGC_generation0; \
+ g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
+ g->gc.gc_prev->gc.gc_next = g; \
+ _PyGC_generation0->gc.gc_prev = g; \
} while (0);
/* Tell the GC to stop tracking this object.
@@ -274,23 +274,23 @@ extern PyGC_Head *_PyGC_generation0;
* way to provoke memory errors if calling code is confused.
*/
#define _PyObject_GC_UNTRACK(o) do { \
- PyGC_Head *g = _Py_AS_GC(o); \
- assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
- g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
- g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
- g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
- g->gc.gc_next = NULL; \
+ PyGC_Head *g = _Py_AS_GC(o); \
+ assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
+ g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
+ g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
+ g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
+ g->gc.gc_next = NULL; \
} while (0);
/* True if the object is currently tracked by the GC. */
#define _PyObject_GC_IS_TRACKED(o) \
- ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED)
-
+ ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED)
+
/* True if the object may be tracked by the GC in the future, or already is.
This can be useful to implement some optimizations. */
#define _PyObject_GC_MAY_BE_TRACKED(obj) \
- (PyObject_IS_GC(obj) && \
- (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
+ (PyObject_IS_GC(obj) && \
+ (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
@@ -301,9 +301,9 @@ PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
PyAPI_FUNC(void) PyObject_GC_Del(void *);
#define PyObject_GC_New(type, typeobj) \
- ( (type *) _PyObject_GC_New(typeobj) )
+ ( (type *) _PyObject_GC_New(typeobj) )
#define PyObject_GC_NewVar(type, typeobj, n) \
- ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
+ ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
/* Utility macro to help write tp_traverse functions.
@@ -311,14 +311,14 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
* "visit" and "arg". This is intended to keep tp_traverse functions
* looking as much alike as possible.
*/
-#define Py_VISIT(op) \
- do { \
- if (op) { \
- int vret = visit((PyObject *)(op), arg); \
- if (vret) \
- return vret; \
- } \
- } while (0)
+#define Py_VISIT(op) \
+ do { \
+ if (op) { \
+ int vret = visit((PyObject *)(op), arg); \
+ if (vret) \
+ return vret; \
+ } \
+ } while (0)
/* This is here for the sake of backwards compatibility. Extensions that
* use the old GC API will still compile but the objects will not be
@@ -334,7 +334,7 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
#define PyObject_GET_WEAKREFS_LISTPTR(o) \
- ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
+ ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
#ifdef __cplusplus
}
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index aafb2a0..66a7030 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -8,8 +8,8 @@ extern "C" {
/* PyException_HEAD defines the initial segment of every exception class. */
#define PyException_HEAD PyObject_HEAD PyObject *dict;\
- PyObject *args; PyObject *traceback;\
- PyObject *context; PyObject *cause;
+ PyObject *args; PyObject *traceback;\
+ PyObject *context; PyObject *cause;
typedef struct {
PyException_HEAD
@@ -92,15 +92,15 @@ PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
/* */
-#define PyExceptionClass_Check(x) \
- (PyType_Check((x)) && \
- PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
+#define PyExceptionClass_Check(x) \
+ (PyType_Check((x)) && \
+ PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
-#define PyExceptionInstance_Check(x) \
- PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
+#define PyExceptionInstance_Check(x) \
+ PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
#define PyExceptionClass_Name(x) \
- ((char *)(((PyTypeObject*)(x))->tp_name))
+ ((char *)(((PyTypeObject*)(x))->tp_name))
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
@@ -175,29 +175,29 @@ PyAPI_FUNC(int) PyErr_BadArgument(void);
PyAPI_FUNC(PyObject *) PyErr_NoMemory(void);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrno(PyObject *);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilenameObject(
- PyObject *, PyObject *);
+ PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithFilename(PyObject *, const char *);
#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromErrnoWithUnicodeFilename(
- PyObject *, const Py_UNICODE *);
+ PyObject *, const Py_UNICODE *);
#endif /* MS_WINDOWS */
PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...);
#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject(
- int, const char *);
+ int, const char *);
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
- int, const char *);
+ int, const char *);
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithUnicodeFilename(
- int, const Py_UNICODE *);
+ int, const Py_UNICODE *);
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErr(int);
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilenameObject(
- PyObject *,int, PyObject *);
+ PyObject *,int, PyObject *);
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithFilename(
- PyObject *,int, const char *);
+ PyObject *,int, const char *);
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErrWithUnicodeFilename(
- PyObject *,int, const Py_UNICODE *);
+ PyObject *,int, const Py_UNICODE *);
PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
#endif /* MS_WINDOWS */
@@ -229,15 +229,15 @@ PyAPI_FUNC(PyObject *) PyErr_ProgramText(const char *, int);
/* create a UnicodeDecodeError object */
PyAPI_FUNC(PyObject *) PyUnicodeDecodeError_Create(
- const char *, const char *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
+ const char *, const char *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
/* create a UnicodeEncodeError object */
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_Create(
- const char *, const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
+ const char *, const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
/* create a UnicodeTranslateError object */
PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_Create(
- const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
+ const Py_UNICODE *, Py_ssize_t, Py_ssize_t, Py_ssize_t, const char *);
/* get the encoding attribute */
PyAPI_FUNC(PyObject *) PyUnicodeEncodeError_GetEncoding(PyObject *);
@@ -280,11 +280,11 @@ PyAPI_FUNC(PyObject *) PyUnicodeTranslateError_GetReason(PyObject *);
/* assign a new value to the reason attribute
return 0 on success, -1 on failure */
PyAPI_FUNC(int) PyUnicodeEncodeError_SetReason(
- PyObject *, const char *);
+ PyObject *, const char *);
PyAPI_FUNC(int) PyUnicodeDecodeError_SetReason(
- PyObject *, const char *);
+ PyObject *, const char *);
PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
- PyObject *, const char *);
+ PyObject *, const char *);
/* These APIs aren't really part of the error implementation, but
@@ -303,9 +303,9 @@ PyAPI_FUNC(int) PyUnicodeTranslateError_SetReason(
#include <stdarg.h>
PyAPI_FUNC(int) PyOS_snprintf(char *str, size_t size, const char *format, ...)
- Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
+ Py_GCC_ATTRIBUTE((format(printf, 3, 4)));
PyAPI_FUNC(int) PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
- Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
+ Py_GCC_ATTRIBUTE((format(printf, 3, 0)));
#ifdef __cplusplus
}
diff --git a/Include/pymacconfig.h b/Include/pymacconfig.h
index 50b429c..9e058c1 100644
--- a/Include/pymacconfig.h
+++ b/Include/pymacconfig.h
@@ -4,7 +4,7 @@
* This file moves some of the autoconf magic to compile-time
* when building on MacOSX. This is needed for building 4-way
* universal binaries and for 64-bit universal binaries because
- * the values redefined below aren't configure-time constant but
+ * the values redefined below aren't configure-time constant but
* only compile-time constant in these scenarios.
*/
@@ -34,36 +34,36 @@
# undef SIZEOF_LONG
# ifdef __LP64__
-# define SIZEOF__BOOL 1
-# define SIZEOF__BOOL 1
-# define SIZEOF_LONG 8
-# define SIZEOF_PTHREAD_T 8
-# define SIZEOF_SIZE_T 8
-# define SIZEOF_TIME_T 8
-# define SIZEOF_VOID_P 8
+# define SIZEOF__BOOL 1
+# define SIZEOF__BOOL 1
+# define SIZEOF_LONG 8
+# define SIZEOF_PTHREAD_T 8
+# define SIZEOF_SIZE_T 8
+# define SIZEOF_TIME_T 8
+# define SIZEOF_VOID_P 8
# else
# ifdef __ppc__
-# define SIZEOF__BOOL 4
+# define SIZEOF__BOOL 4
# else
-# define SIZEOF__BOOL 1
+# define SIZEOF__BOOL 1
# endif
-# define SIZEOF_LONG 4
-# define SIZEOF_PTHREAD_T 4
-# define SIZEOF_SIZE_T 4
-# define SIZEOF_TIME_T 4
-# define SIZEOF_VOID_P 4
+# define SIZEOF_LONG 4
+# define SIZEOF_PTHREAD_T 4
+# define SIZEOF_SIZE_T 4
+# define SIZEOF_TIME_T 4
+# define SIZEOF_VOID_P 4
# endif
# if defined(__LP64__)
- /* MacOSX 10.4 (the first release to suppport 64-bit code
- * at all) only supports 64-bit in the UNIX layer.
- * Therefore surpress the toolbox-glue in 64-bit mode.
- */
+ /* MacOSX 10.4 (the first release to suppport 64-bit code
+ * at all) only supports 64-bit in the UNIX layer.
+ * Therefore surpress the toolbox-glue in 64-bit mode.
+ */
- /* In 64-bit mode setpgrp always has no argments, in 32-bit
- * mode that depends on the compilation environment
- */
-# undef SETPGRP_HAVE_ARG
+ /* In 64-bit mode setpgrp always has no argments, in 32-bit
+ * mode that depends on the compilation environment
+ */
+# undef SETPGRP_HAVE_ARG
# endif
@@ -78,17 +78,17 @@
# define HAVE_GCC_ASM_FOR_X87
#endif
- /*
- * The definition in pyconfig.h is only valid on the OS release
- * where configure ran on and not necessarily for all systems where
- * the executable can be used on.
- *
- * Specifically: OSX 10.4 has limited supported for '%zd', while
- * 10.5 has full support for '%zd'. A binary built on 10.5 won't
- * work properly on 10.4 unless we surpress the definition
- * of PY_FORMAT_SIZE_T
- */
-#undef PY_FORMAT_SIZE_T
+ /*
+ * The definition in pyconfig.h is only valid on the OS release
+ * where configure ran on and not necessarily for all systems where
+ * the executable can be used on.
+ *
+ * Specifically: OSX 10.4 has limited supported for '%zd', while
+ * 10.5 has full support for '%zd'. A binary built on 10.5 won't
+ * work properly on 10.4 unless we surpress the definition
+ * of PY_FORMAT_SIZE_T
+ */
+#undef PY_FORMAT_SIZE_T
#endif /* defined(_APPLE__) */
diff --git a/Include/pyport.h b/Include/pyport.h
index 01b0c3a..6af0f96 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -132,20 +132,20 @@ Used in: PY_LONG_LONG
* integral type.
*/
#ifdef HAVE_UINTPTR_T
-typedef uintptr_t Py_uintptr_t;
-typedef intptr_t Py_intptr_t;
+typedef uintptr_t Py_uintptr_t;
+typedef intptr_t Py_intptr_t;
#elif SIZEOF_VOID_P <= SIZEOF_INT
-typedef unsigned int Py_uintptr_t;
-typedef int Py_intptr_t;
+typedef unsigned int Py_uintptr_t;
+typedef int Py_intptr_t;
#elif SIZEOF_VOID_P <= SIZEOF_LONG
-typedef unsigned long Py_uintptr_t;
-typedef long Py_intptr_t;
+typedef unsigned long Py_uintptr_t;
+typedef long Py_intptr_t;
#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
-typedef unsigned PY_LONG_LONG Py_uintptr_t;
-typedef PY_LONG_LONG Py_intptr_t;
+typedef unsigned PY_LONG_LONG Py_uintptr_t;
+typedef PY_LONG_LONG Py_intptr_t;
#else
# error "Python needs a typedef for Py_uintptr_t in pyport.h."
@@ -156,9 +156,9 @@ typedef PY_LONG_LONG Py_intptr_t;
* unsigned integral type). See PEP 353 for details.
*/
#ifdef HAVE_SSIZE_T
-typedef ssize_t Py_ssize_t;
+typedef ssize_t Py_ssize_t;
#elif SIZEOF_VOID_P == SIZEOF_SIZE_T
-typedef Py_intptr_t Py_ssize_t;
+typedef Py_intptr_t Py_ssize_t;
#else
# error "Python needs a typedef for Py_ssize_t in pyport.h."
#endif
@@ -166,7 +166,7 @@ typedef Py_intptr_t Py_ssize_t;
/* Largest possible value of size_t.
SIZE_MAX is part of C99, so it might be defined on some
platforms. If it is not defined, (size_t)-1 is a portable
- definition for C89, due to the way signed->unsigned
+ definition for C89, due to the way signed->unsigned
conversion is defined. */
#ifdef SIZE_MAX
#define PY_SIZE_MAX SIZE_MAX
@@ -243,7 +243,7 @@ typedef Py_intptr_t Py_ssize_t;
/* enable more aggressive optimization for visual studio */
#pragma optimize("agtw", on)
#endif
-/* ignore warnings if the compiler decides not to inline a function */
+/* ignore warnings if the compiler decides not to inline a function */
#pragma warning(disable: 4710)
/* fastest possible local call under MSVC */
#define Py_LOCAL(type) static type __fastcall
@@ -263,16 +263,16 @@ typedef Py_intptr_t Py_ssize_t;
*/
#if defined(_MSC_VER)
-#define Py_MEMCPY(target, source, length) do { \
- size_t i_, n_ = (length); \
- char *t_ = (void*) (target); \
- const char *s_ = (void*) (source); \
- if (n_ >= 16) \
- memcpy(t_, s_, n_); \
- else \
- for (i_ = 0; i_ < n_; i_++) \
- t_[i_] = s_[i_]; \
- } while (0)
+#define Py_MEMCPY(target, source, length) do { \
+ size_t i_, n_ = (length); \
+ char *t_ = (void*) (target); \
+ const char *s_ = (void*) (source); \
+ if (n_ >= 16) \
+ memcpy(t_, s_, n_); \
+ else \
+ for (i_ = 0; i_ < n_; i_++) \
+ t_[i_] = s_[i_]; \
+ } while (0)
#else
#define Py_MEMCPY memcpy
#endif
@@ -383,7 +383,7 @@ extern "C" {
*/
#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
- ((I) < 0 ? -1-((-1-(I)) >> (J)) : (I) >> (J))
+ ((I) < 0 ? -1-((-1-(I)) >> (J)) : (I) >> (J))
#else
#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
#endif
@@ -403,7 +403,7 @@ extern "C" {
*/
#ifdef Py_DEBUG
#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
- (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
+ (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
#else
#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
#endif
@@ -423,13 +423,13 @@ extern "C" {
#define _Py_SET_EDOM_FOR_NAN(X) ;
#endif
#define Py_SET_ERRNO_ON_MATH_ERROR(X) \
- do { \
- if (errno == 0) { \
- if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
- errno = ERANGE; \
- else _Py_SET_EDOM_FOR_NAN(X) \
- } \
- } while(0)
+ do { \
+ if (errno == 0) { \
+ if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
+ errno = ERANGE; \
+ else _Py_SET_EDOM_FOR_NAN(X) \
+ } \
+ } while(0)
/* Py_SET_ERANGE_ON_OVERFLOW(x)
* An alias of Py_SET_ERRNO_ON_MATH_ERROR for backward-compatibility.
@@ -450,26 +450,26 @@ extern "C" {
* This isn't reliable. See Py_OVERFLOWED comments.
* X and Y may be evaluated more than once.
*/
-#define Py_ADJUST_ERANGE1(X) \
- do { \
- if (errno == 0) { \
- if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
- errno = ERANGE; \
- } \
- else if (errno == ERANGE && (X) == 0.0) \
- errno = 0; \
- } while(0)
-
-#define Py_ADJUST_ERANGE2(X, Y) \
- do { \
- if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
- (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
- if (errno == 0) \
- errno = ERANGE; \
- } \
- else if (errno == ERANGE) \
- errno = 0; \
- } while(0)
+#define Py_ADJUST_ERANGE1(X) \
+ do { \
+ if (errno == 0) { \
+ if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL) \
+ errno = ERANGE; \
+ } \
+ else if (errno == ERANGE && (X) == 0.0) \
+ errno = 0; \
+ } while(0)
+
+#define Py_ADJUST_ERANGE2(X, Y) \
+ do { \
+ if ((X) == Py_HUGE_VAL || (X) == -Py_HUGE_VAL || \
+ (Y) == Py_HUGE_VAL || (Y) == -Py_HUGE_VAL) { \
+ if (errno == 0) \
+ errno = ERANGE; \
+ } \
+ else if (errno == ERANGE) \
+ errno = 0; \
+ } while(0)
/* The functions _Py_dg_strtod and _Py_dg_dtoa in Python/dtoa.c (which are
* required to support the short float repr introduced in Python 3.1) require
@@ -498,18 +498,18 @@ extern "C" {
#ifdef HAVE_GCC_ASM_FOR_X87
#define HAVE_PY_SET_53BIT_PRECISION 1
/* _Py_get/set_387controlword functions are defined in Python/pymath.c */
-#define _Py_SET_53BIT_PRECISION_HEADER \
- unsigned short old_387controlword, new_387controlword
-#define _Py_SET_53BIT_PRECISION_START \
- do { \
- old_387controlword = _Py_get_387controlword(); \
- new_387controlword = (old_387controlword & ~0x0f00) | 0x0200; \
- if (new_387controlword != old_387controlword) \
- _Py_set_387controlword(new_387controlword); \
- } while (0)
-#define _Py_SET_53BIT_PRECISION_END \
- if (new_387controlword != old_387controlword) \
- _Py_set_387controlword(old_387controlword)
+#define _Py_SET_53BIT_PRECISION_HEADER \
+ unsigned short old_387controlword, new_387controlword
+#define _Py_SET_53BIT_PRECISION_START \
+ do { \
+ old_387controlword = _Py_get_387controlword(); \
+ new_387controlword = (old_387controlword & ~0x0f00) | 0x0200; \
+ if (new_387controlword != old_387controlword) \
+ _Py_set_387controlword(new_387controlword); \
+ } while (0)
+#define _Py_SET_53BIT_PRECISION_END \
+ if (new_387controlword != old_387controlword) \
+ _Py_set_387controlword(old_387controlword)
#endif
/* default definitions are empty */
@@ -553,7 +553,7 @@ extern "C" {
* extern int x() Py_DEPRECATED(2.5);
*/
#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
- (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
+ (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
#define Py_DEPRECATED(VERSION_UNUSED) __attribute__((__deprecated__))
#else
#define Py_DEPRECATED(VERSION_UNUSED)
@@ -573,7 +573,7 @@ extern int gethostname(char *, int);
#endif
#ifdef HAVE__GETPTY
-#include <sys/types.h> /* we need to import mode_t */
+#include <sys/types.h> /* we need to import mode_t */
extern char * _getpty(int *, int, mode_t, int);
#endif
@@ -654,54 +654,54 @@ extern pid_t forkpty(int *, char *, struct termios *, struct winsize *);
linkage handling and it uses __declspec().
*/
#if defined(__CYGWIN__)
-# define HAVE_DECLSPEC_DLL
+# define HAVE_DECLSPEC_DLL
#endif
/* only get special linkage if built as shared or platform is Cygwin */
#if defined(Py_ENABLE_SHARED) || defined(__CYGWIN__)
-# if defined(HAVE_DECLSPEC_DLL)
-# ifdef Py_BUILD_CORE
-# define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
-# define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
- /* module init functions inside the core need no external linkage */
- /* except for Cygwin to handle embedding */
-# if defined(__CYGWIN__)
-# define PyMODINIT_FUNC __declspec(dllexport) PyObject*
-# else /* __CYGWIN__ */
-# define PyMODINIT_FUNC PyObject*
-# endif /* __CYGWIN__ */
-# else /* Py_BUILD_CORE */
- /* Building an extension module, or an embedded situation */
- /* public Python functions and data are imported */
- /* Under Cygwin, auto-import functions to prevent compilation */
- /* failures similar to http://python.org/doc/FAQ.html#3.24 */
-# if !defined(__CYGWIN__)
-# define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
-# endif /* !__CYGWIN__ */
-# define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
- /* module init functions outside the core must be exported */
-# if defined(__cplusplus)
-# define PyMODINIT_FUNC extern "C" __declspec(dllexport) PyObject*
-# else /* __cplusplus */
-# define PyMODINIT_FUNC __declspec(dllexport) PyObject*
-# endif /* __cplusplus */
-# endif /* Py_BUILD_CORE */
-# endif /* HAVE_DECLSPEC */
+# if defined(HAVE_DECLSPEC_DLL)
+# ifdef Py_BUILD_CORE
+# define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE
+# define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE
+ /* module init functions inside the core need no external linkage */
+ /* except for Cygwin to handle embedding */
+# if defined(__CYGWIN__)
+# define PyMODINIT_FUNC __declspec(dllexport) PyObject*
+# else /* __CYGWIN__ */
+# define PyMODINIT_FUNC PyObject*
+# endif /* __CYGWIN__ */
+# else /* Py_BUILD_CORE */
+ /* Building an extension module, or an embedded situation */
+ /* public Python functions and data are imported */
+ /* Under Cygwin, auto-import functions to prevent compilation */
+ /* failures similar to http://python.org/doc/FAQ.html#3.24 */
+# if !defined(__CYGWIN__)
+# define PyAPI_FUNC(RTYPE) __declspec(dllimport) RTYPE
+# endif /* !__CYGWIN__ */
+# define PyAPI_DATA(RTYPE) extern __declspec(dllimport) RTYPE
+ /* module init functions outside the core must be exported */
+# if defined(__cplusplus)
+# define PyMODINIT_FUNC extern "C" __declspec(dllexport) PyObject*
+# else /* __cplusplus */
+# define PyMODINIT_FUNC __declspec(dllexport) PyObject*
+# endif /* __cplusplus */
+# endif /* Py_BUILD_CORE */
+# endif /* HAVE_DECLSPEC */
#endif /* Py_ENABLE_SHARED */
/* If no external linkage macros defined by now, create defaults */
#ifndef PyAPI_FUNC
-# define PyAPI_FUNC(RTYPE) RTYPE
+# define PyAPI_FUNC(RTYPE) RTYPE
#endif
#ifndef PyAPI_DATA
-# define PyAPI_DATA(RTYPE) extern RTYPE
+# define PyAPI_DATA(RTYPE) extern RTYPE
#endif
#ifndef PyMODINIT_FUNC
-# if defined(__cplusplus)
-# define PyMODINIT_FUNC extern "C" PyObject*
-# else /* __cplusplus */
-# define PyMODINIT_FUNC PyObject*
-# endif /* __cplusplus */
+# if defined(__cplusplus)
+# define PyMODINIT_FUNC extern "C" PyObject*
+# else /* __cplusplus */
+# define PyMODINIT_FUNC PyObject*
+# endif /* __cplusplus */
#endif
/* limits.h constants that may be missing */
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index 09d3ee5..0c380e6 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -17,7 +17,7 @@ extern "C" {
#define PyCF_IGNORE_COOKIE 0x0800
typedef struct {
- int cf_flags; /* bitmask of CO_xxx flags relevant to future */
+ int cf_flags; /* bitmask of CO_xxx flags relevant to future */
} PyCompilerFlags;
PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
@@ -40,33 +40,33 @@ PyAPI_FUNC(int) PyRun_SimpleFileExFlags(FILE *, const char *, int, PyCompilerFla
PyAPI_FUNC(int) PyRun_InteractiveOneFlags(FILE *, const char *, PyCompilerFlags *);
PyAPI_FUNC(int) PyRun_InteractiveLoopFlags(FILE *, const char *, PyCompilerFlags *);
-PyAPI_FUNC(struct _mod *) PyParser_ASTFromString(const char *, const char *,
- int, PyCompilerFlags *flags,
+PyAPI_FUNC(struct _mod *) PyParser_ASTFromString(const char *, const char *,
+ int, PyCompilerFlags *flags,
PyArena *);
-PyAPI_FUNC(struct _mod *) PyParser_ASTFromFile(FILE *, const char *,
- const char*, int,
- char *, char *,
+PyAPI_FUNC(struct _mod *) PyParser_ASTFromFile(FILE *, const char *,
+ const char*, int,
+ char *, char *,
PyCompilerFlags *, int *,
PyArena *);
#define PyParser_SimpleParseString(S, B) \
- PyParser_SimpleParseStringFlags(S, B, 0)
+ PyParser_SimpleParseStringFlags(S, B, 0)
#define PyParser_SimpleParseFile(FP, S, B) \
- PyParser_SimpleParseFileFlags(FP, S, B, 0)
-PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlags(const char *, int,
- int);
+ PyParser_SimpleParseFileFlags(FP, S, B, 0)
+PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlags(const char *, int,
+ int);
PyAPI_FUNC(struct _node *) PyParser_SimpleParseFileFlags(FILE *, const char *,
- int, int);
+ int, int);
-PyAPI_FUNC(PyObject *) PyRun_StringFlags(const char *, int, PyObject *,
- PyObject *, PyCompilerFlags *);
+PyAPI_FUNC(PyObject *) PyRun_StringFlags(const char *, int, PyObject *,
+ PyObject *, PyCompilerFlags *);
-PyAPI_FUNC(PyObject *) PyRun_FileExFlags(FILE *, const char *, int,
- PyObject *, PyObject *, int,
- PyCompilerFlags *);
+PyAPI_FUNC(PyObject *) PyRun_FileExFlags(FILE *, const char *, int,
+ PyObject *, PyObject *, int,
+ PyCompilerFlags *);
#define Py_CompileString(str, p, s) Py_CompileStringFlags(str, p, s, NULL)
PyAPI_FUNC(PyObject *) Py_CompileStringFlags(const char *, const char *, int,
- PyCompilerFlags *);
+ PyCompilerFlags *);
PyAPI_FUNC(struct symtable *) Py_SymtableString(const char *, const char *, int);
PyAPI_FUNC(void) PyErr_Print(void);
@@ -90,20 +90,20 @@ PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
#define PyRun_String(str, s, g, l) PyRun_StringFlags(str, s, g, l, NULL)
#define PyRun_AnyFile(fp, name) PyRun_AnyFileExFlags(fp, name, 0, NULL)
#define PyRun_AnyFileEx(fp, name, closeit) \
- PyRun_AnyFileExFlags(fp, name, closeit, NULL)
+ PyRun_AnyFileExFlags(fp, name, closeit, NULL)
#define PyRun_AnyFileFlags(fp, name, flags) \
- PyRun_AnyFileExFlags(fp, name, 0, flags)
+ PyRun_AnyFileExFlags(fp, name, 0, flags)
#define PyRun_SimpleString(s) PyRun_SimpleStringFlags(s, NULL)
#define PyRun_SimpleFile(f, p) PyRun_SimpleFileExFlags(f, p, 0, NULL)
#define PyRun_SimpleFileEx(f, p, c) PyRun_SimpleFileExFlags(f, p, c, NULL)
#define PyRun_InteractiveOne(f, p) PyRun_InteractiveOneFlags(f, p, NULL)
#define PyRun_InteractiveLoop(f, p) PyRun_InteractiveLoopFlags(f, p, NULL)
#define PyRun_File(fp, p, s, g, l) \
- PyRun_FileExFlags(fp, p, s, g, l, 0, NULL)
+ PyRun_FileExFlags(fp, p, s, g, l, 0, NULL)
#define PyRun_FileEx(fp, p, s, g, l, c) \
- PyRun_FileExFlags(fp, p, s, g, l, c, NULL)
+ PyRun_FileExFlags(fp, p, s, g, l, c, NULL)
#define PyRun_FileFlags(fp, p, s, g, l, flags) \
- PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
+ PyRun_FileExFlags(fp, p, s, g, l, 0, flags)
/* In getpath.c */
PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
diff --git a/Include/setobject.h b/Include/setobject.h
index 60bd73b..4a9baff 100644
--- a/Include/setobject.h
+++ b/Include/setobject.h
@@ -22,8 +22,8 @@ no meaning otherwise.
#define PySet_MINSIZE 8
typedef struct {
- long hash; /* cached hash code for the entry key */
- PyObject *key;
+ long hash; /* cached hash code for the entry key */
+ PyObject *key;
} setentry;
@@ -33,27 +33,27 @@ This data structure is shared by set and frozenset objects.
typedef struct _setobject PySetObject;
struct _setobject {
- PyObject_HEAD
-
- Py_ssize_t fill; /* # Active + # Dummy */
- Py_ssize_t used; /* # Active */
-
- /* The table contains mask + 1 slots, and that's a power of 2.
- * We store the mask instead of the size because the mask is more
- * frequently needed.
- */
- Py_ssize_t mask;
-
- /* table points to smalltable for small tables, else to
- * additional malloc'ed memory. table is never NULL! This rule
- * saves repeated runtime null-tests.
- */
- setentry *table;
- setentry *(*lookup)(PySetObject *so, PyObject *key, long hash);
- setentry smalltable[PySet_MINSIZE];
-
- long hash; /* only used by frozenset objects */
- PyObject *weakreflist; /* List of weak references */
+ PyObject_HEAD
+
+ Py_ssize_t fill; /* # Active + # Dummy */
+ Py_ssize_t used; /* # Active */
+
+ /* The table contains mask + 1 slots, and that's a power of 2.
+ * We store the mask instead of the size because the mask is more
+ * frequently needed.
+ */
+ Py_ssize_t mask;
+
+ /* table points to smalltable for small tables, else to
+ * additional malloc'ed memory. table is never NULL! This rule
+ * saves repeated runtime null-tests.
+ */
+ setentry *table;
+ setentry *(*lookup)(PySetObject *so, PyObject *key, long hash);
+ setentry smalltable[PySet_MINSIZE];
+
+ long hash; /* only used by frozenset objects */
+ PyObject *weakreflist; /* List of weak references */
};
PyAPI_DATA(PyTypeObject) PySet_Type;
@@ -69,17 +69,17 @@ PyAPI_DATA(PyTypeObject) PySetIter_Type;
#define PyFrozenSet_CheckExact(ob) (Py_TYPE(ob) == &PyFrozenSet_Type)
#define PyAnySet_CheckExact(ob) \
- (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
+ (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type)
#define PyAnySet_Check(ob) \
- (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
- PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
- PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
+ (Py_TYPE(ob) == &PySet_Type || Py_TYPE(ob) == &PyFrozenSet_Type || \
+ PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
+ PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
#define PySet_Check(ob) \
- (Py_TYPE(ob) == &PySet_Type || \
- PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
+ (Py_TYPE(ob) == &PySet_Type || \
+ PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
#define PyFrozenSet_Check(ob) \
- (Py_TYPE(ob) == &PyFrozenSet_Type || \
- PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
+ (Py_TYPE(ob) == &PyFrozenSet_Type || \
+ PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);
diff --git a/Include/structseq.h b/Include/structseq.h
index a482bd8..0b8d387 100644
--- a/Include/structseq.h
+++ b/Include/structseq.h
@@ -8,35 +8,35 @@ extern "C" {
#endif
typedef struct PyStructSequence_Field {
- char *name;
- char *doc;
+ char *name;
+ char *doc;
} PyStructSequence_Field;
typedef struct PyStructSequence_Desc {
- char *name;
- char *doc;
- struct PyStructSequence_Field *fields;
- int n_in_sequence;
+ char *name;
+ char *doc;
+ struct PyStructSequence_Field *fields;
+ int n_in_sequence;
} PyStructSequence_Desc;
extern char* PyStructSequence_UnnamedField;
PyAPI_FUNC(void) PyStructSequence_InitType(PyTypeObject *type,
- PyStructSequence_Desc *desc);
+ PyStructSequence_Desc *desc);
PyAPI_FUNC(PyObject *) PyStructSequence_New(PyTypeObject* type);
typedef struct {
- PyObject_VAR_HEAD
- PyObject *ob_item[1];
+ PyObject_VAR_HEAD
+ PyObject *ob_item[1];
} PyStructSequence;
/* Macro, *only* to be used to fill in brand new objects */
#define PyStructSequence_SET_ITEM(op, i, v) \
- (((PyStructSequence *)(op))->ob_item[i] = v)
+ (((PyStructSequence *)(op))->ob_item[i] = v)
#define PyStructSequence_GET_ITEM(op, i) \
- (((PyStructSequence *)(op))->ob_item[i])
+ (((PyStructSequence *)(op))->ob_item[i])
#ifdef __cplusplus
diff --git a/Include/symtable.h b/Include/symtable.h
index b07d77c..9abc5b2 100644
--- a/Include/symtable.h
+++ b/Include/symtable.h
@@ -15,40 +15,40 @@ typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
struct _symtable_entry;
struct symtable {
- const char *st_filename; /* name of file being compiled */
- struct _symtable_entry *st_cur; /* current symbol table entry */
- struct _symtable_entry *st_top; /* symbol table entry for module */
- PyObject *st_blocks; /* dict: map AST node addresses
- * to symbol table entries */
- PyObject *st_stack; /* list: stack of namespace info */
- PyObject *st_global; /* borrowed ref to st_top->st_symbols */
- int st_nblocks; /* number of blocks used */
- PyObject *st_private; /* name of current class or NULL */
- PyFutureFeatures *st_future; /* module's future features */
+ const char *st_filename; /* name of file being compiled */
+ struct _symtable_entry *st_cur; /* current symbol table entry */
+ struct _symtable_entry *st_top; /* symbol table entry for module */
+ PyObject *st_blocks; /* dict: map AST node addresses
+ * to symbol table entries */
+ PyObject *st_stack; /* list: stack of namespace info */
+ PyObject *st_global; /* borrowed ref to st_top->st_symbols */
+ int st_nblocks; /* number of blocks used */
+ PyObject *st_private; /* name of current class or NULL */
+ PyFutureFeatures *st_future; /* module's future features */
};
typedef struct _symtable_entry {
- PyObject_HEAD
- PyObject *ste_id; /* int: key in ste_table->st_blocks */
- PyObject *ste_symbols; /* dict: variable names to flags */
- PyObject *ste_name; /* string: name of current block */
- PyObject *ste_varnames; /* list of variable names */
- PyObject *ste_children; /* list of child blocks */
- _Py_block_ty ste_type; /* module, class, or function */
- int ste_unoptimized; /* false if namespace is optimized */
- int ste_nested; /* true if block is nested */
- unsigned ste_free : 1; /* true if block has free variables */
- unsigned ste_child_free : 1; /* true if a child block has free vars,
- including free refs to globals */
- unsigned ste_generator : 1; /* true if namespace is a generator */
- unsigned ste_varargs : 1; /* true if block has varargs */
- unsigned ste_varkeywords : 1; /* true if block has varkeywords */
- unsigned ste_returns_value : 1; /* true if namespace uses return with
- an argument */
- int ste_lineno; /* first line of block */
- int ste_opt_lineno; /* lineno of last exec or import * */
- int ste_tmpname; /* counter for listcomp temp vars */
- struct symtable *ste_table;
+ PyObject_HEAD
+ PyObject *ste_id; /* int: key in ste_table->st_blocks */
+ PyObject *ste_symbols; /* dict: variable names to flags */
+ PyObject *ste_name; /* string: name of current block */
+ PyObject *ste_varnames; /* list of variable names */
+ PyObject *ste_children; /* list of child blocks */
+ _Py_block_ty ste_type; /* module, class, or function */
+ int ste_unoptimized; /* false if namespace is optimized */
+ int ste_nested; /* true if block is nested */
+ unsigned ste_free : 1; /* true if block has free variables */
+ unsigned ste_child_free : 1; /* true if a child block has free vars,
+ including free refs to globals */
+ unsigned ste_generator : 1; /* true if namespace is a generator */
+ unsigned ste_varargs : 1; /* true if block has varargs */
+ unsigned ste_varkeywords : 1; /* true if block has varkeywords */
+ unsigned ste_returns_value : 1; /* true if namespace uses return with
+ an argument */
+ int ste_lineno; /* first line of block */
+ int ste_opt_lineno; /* lineno of last exec or import * */
+ int ste_tmpname; /* counter for listcomp temp vars */
+ struct symtable *ste_table;
} PySTEntryObject;
PyAPI_DATA(PyTypeObject) PySTEntry_Type;
@@ -57,8 +57,8 @@ PyAPI_DATA(PyTypeObject) PySTEntry_Type;
PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
-PyAPI_FUNC(struct symtable *) PySymtable_Build(mod_ty, const char *,
- PyFutureFeatures *);
+PyAPI_FUNC(struct symtable *) PySymtable_Build(mod_ty, const char *,
+ PyFutureFeatures *);
PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
@@ -81,7 +81,7 @@ PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
- table. GLOBAL is returned from PyST_GetScope() for either of them.
+ table. GLOBAL is returned from PyST_GetScope() for either of them.
It is stored in ste_symbols at bits 12-15.
*/
#define SCOPE_OFFSET 11
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index 08b518a..d21dd96 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -28,14 +28,14 @@ Copyright (c) Corporation for National Research Initiatives.
*
* --------------------------------------------------------------------
* This Unicode String Type is
- *
+ *
* Copyright (c) 1999 by Secret Labs AB
* Copyright (c) 1999 by Fredrik Lundh
- *
+ *
* By obtaining, using, and/or copying this software and/or its
* associated documentation, you agree that you have read, understood,
* and will comply with the following terms and conditions:
- *
+ *
* Permission to use, copy, modify, and distribute this software and its
* associated documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appears in all
@@ -44,7 +44,7 @@ Copyright (c) Corporation for National Research Initiatives.
* AB or the author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
- *
+ *
* SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
@@ -61,7 +61,7 @@ Copyright (c) Corporation for National Research Initiatives.
/* --- Internal Unicode Format -------------------------------------------- */
/* Python 3.x requires unicode */
-#define Py_USING_UNICODE
+#define Py_USING_UNICODE
/* FIXME: MvL's new implementation assumes that Py_UNICODE_SIZE is
properly set, but the default rules below doesn't set it. I'll
@@ -120,10 +120,10 @@ Copyright (c) Corporation for National Research Initiatives.
* Use this typedef when you need to represent a UTF-16 surrogate pair
* as single unsigned integer.
*/
-#if SIZEOF_INT >= 4
-typedef unsigned int Py_UCS4;
+#if SIZEOF_INT >= 4
+typedef unsigned int Py_UCS4;
#elif SIZEOF_LONG >= 4
-typedef unsigned long Py_UCS4;
+typedef unsigned long Py_UCS4;
#endif
/* Py_UNICODE is the native Unicode storage format (code unit) used by
@@ -382,7 +382,7 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
*/
#define Py_UNICODE_ISSPACE(ch) \
- ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
+ ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
#define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
#define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
@@ -408,16 +408,16 @@ typedef PY_UNICODE_TYPE Py_UNICODE;
#define Py_UNICODE_ISALNUM(ch) \
(Py_UNICODE_ISALPHA(ch) || \
- Py_UNICODE_ISDECIMAL(ch) || \
- Py_UNICODE_ISDIGIT(ch) || \
- Py_UNICODE_ISNUMERIC(ch))
+ Py_UNICODE_ISDECIMAL(ch) || \
+ Py_UNICODE_ISDIGIT(ch) || \
+ Py_UNICODE_ISNUMERIC(ch))
-#define Py_UNICODE_COPY(target, source, length) \
- Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
+#define Py_UNICODE_COPY(target, source, length) \
+ Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
#define Py_UNICODE_FILL(target, value, length) \
do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
- for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
+ for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
} while (0)
/* Check if substring matches at given offset. the offset must be
@@ -436,15 +436,15 @@ extern "C" {
typedef struct {
PyObject_HEAD
- Py_ssize_t length; /* Length of raw Unicode data in buffer */
- Py_UNICODE *str; /* Raw Unicode buffer */
- long hash; /* Hash value; -1 if not set */
- int state; /* != 0 if interned. In this case the two
- * references from the dictionary to this object
- * are *not* counted in ob_refcnt. */
- PyObject *defenc; /* (Default) Encoded version as Python
- string, or NULL; this is used for
- implementing the buffer protocol */
+ Py_ssize_t length; /* Length of raw Unicode data in buffer */
+ Py_UNICODE *str; /* Raw Unicode buffer */
+ long hash; /* Hash value; -1 if not set */
+ int state; /* != 0 if interned. In this case the two
+ * references from the dictionary to this object
+ * are *not* counted in ob_refcnt. */
+ PyObject *defenc; /* (Default) Encoded version as Python
+ string, or NULL; this is used for
+ implementing the buffer protocol */
} PyUnicodeObject;
PyAPI_DATA(PyTypeObject) PyUnicode_Type;
@@ -460,13 +460,13 @@ PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
/* Fast access macros */
#define PyUnicode_GET_SIZE(op) \
- (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length))
+ (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length))
#define PyUnicode_GET_DATA_SIZE(op) \
- (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length * sizeof(Py_UNICODE)))
+ (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length * sizeof(Py_UNICODE)))
#define PyUnicode_AS_UNICODE(op) \
- (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->str))
+ (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->str))
#define PyUnicode_AS_DATA(op) \
- (assert(PyUnicode_Check(op)),((const char *)((PyUnicodeObject *)(op))->str))
+ (assert(PyUnicode_Check(op)),((const char *)((PyUnicodeObject *)(op))->str))
/* --- Constants ---------------------------------------------------------- */
@@ -482,7 +482,7 @@ PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
/* --- Plain Py_UNICODE --------------------------------------------------- */
/* Create a Unicode Object from the Py_UNICODE buffer u of the given
- size.
+ size.
u may be NULL which causes the contents to be undefined. It is the
user's responsibility to fill in the needed data afterwards. Note
@@ -512,13 +512,13 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromString(
Py_UNICODE buffer. */
PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
/* Get the length of the Unicode object. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
/* Get the maximum ordinal for a Unicode character. */
@@ -539,8 +539,8 @@ PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
*/
PyAPI_FUNC(int) PyUnicode_Resize(
- PyObject **unicode, /* Pointer to the Unicode object */
- Py_ssize_t length /* New length */
+ PyObject **unicode, /* Pointer to the Unicode object */
+ Py_ssize_t length /* New length */
);
/* Coerce obj to an Unicode object and return a reference with
@@ -561,14 +561,14 @@ PyAPI_FUNC(int) PyUnicode_Resize(
*/
PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
- register PyObject *obj, /* Object */
+ register PyObject *obj, /* Object */
const char *encoding, /* encoding */
const char *errors /* error handling */
);
/* Coerce obj to an Unicode object and return a reference with
*incremented* refcount.
-
+
Unicode objects are passed back as-is (subclasses are converted to
true Unicode objects), all other objects are delegated to
PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in
@@ -580,7 +580,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
*/
PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
- register PyObject *obj /* Object */
+ register PyObject *obj /* Object */
);
PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(const char*, va_list);
@@ -636,8 +636,8 @@ PyAPI_FUNC(Py_ssize_t) PyUnicode_AsWideChar(
/* --- Unicode ordinals --------------------------------------------------- */
-/* Create a Unicode Object from the given Unicode code point ordinal.
-
+/* Create a Unicode Object from the given Unicode code point ordinal.
+
The ordinal must be in range(0x10000) on narrow Python builds
(UCS2), and range(0x110000) on wide builds (UCS4). A ValueError is
raised in case it is not.
@@ -657,11 +657,11 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromOrdinal(int ordinal);
PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
-/* === Builtin Codecs =====================================================
+/* === Builtin Codecs =====================================================
Many of these APIs take two arguments encoding and errors. These
parameters encoding and errors have the same semantics as the ones
- of the builtin unicode() API.
+ of the builtin unicode() API.
Setting encoding to NULL causes the default encoding to be used.
@@ -678,7 +678,7 @@ PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
/* --- Manage the default encoding ---------------------------------------- */
/* Return a Python string holding the default encoded value of the
- Unicode object.
+ Unicode object.
The resulting string is cached in the Unicode object for subsequent
usage by this function. The cached version is needed to implement
@@ -710,7 +710,7 @@ PyAPI_FUNC(PyObject *) _PyUnicode_AsDefaultEncodedString(
*/
PyAPI_FUNC(char *) _PyUnicode_AsStringAndSize(
- PyObject *unicode,
+ PyObject *unicode,
Py_ssize_t *size);
/* Returns a pointer to the default encoding (normally, UTf-8) of the
@@ -735,7 +735,7 @@ PyAPI_FUNC(char *) _PyUnicode_AsString(PyObject *unicode);
process global. This may change in future versions of the
interpreter to become a parameter which is managed on a per-thread
basis.
-
+
*/
PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
@@ -743,11 +743,11 @@ PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
/* Sets the currently active default encoding.
Returns 0 on success, -1 in case of an error.
-
+
*/
PyAPI_FUNC(int) PyUnicode_SetDefaultEncoding(
- const char *encoding /* Encoding name in standard form */
+ const char *encoding /* Encoding name in standard form */
);
/* --- Generic Codecs ----------------------------------------------------- */
@@ -766,21 +766,21 @@ PyAPI_FUNC(PyObject*) PyUnicode_Decode(
object. */
PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject(
- PyObject *unicode, /* Unicode object */
- const char *encoding, /* encoding */
- const char *errors /* error handling */
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ const char *errors /* error handling */
);
/* Decode a Unicode object unicode and return the result as Unicode
object. */
PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode(
- PyObject *unicode, /* Unicode object */
- const char *encoding, /* encoding */
- const char *errors /* error handling */
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ const char *errors /* error handling */
);
-/* Encodes a Py_UNICODE buffer of the given size and returns a
+/* Encodes a Py_UNICODE buffer of the given size and returns a
Python string object. */
PyAPI_FUNC(PyObject*) PyUnicode_Encode(
@@ -794,27 +794,27 @@ PyAPI_FUNC(PyObject*) PyUnicode_Encode(
object. */
PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
- PyObject *unicode, /* Unicode object */
- const char *encoding, /* encoding */
- const char *errors /* error handling */
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ const char *errors /* error handling */
);
/* Encodes a Unicode object and returns the result as Python string
object. */
PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
- PyObject *unicode, /* Unicode object */
- const char *encoding, /* encoding */
- const char *errors /* error handling */
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ const char *errors /* error handling */
);
/* Encodes a Unicode object and returns the result as Unicode
object. */
PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode(
- PyObject *unicode, /* Unicode object */
- const char *encoding, /* encoding */
- const char *errors /* error handling */
+ PyObject *unicode, /* Unicode object */
+ const char *encoding, /* encoding */
+ const char *errors /* error handling */
);
/* Build an encoding map. */
@@ -826,49 +826,49 @@ PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap(
/* --- UTF-7 Codecs ------------------------------------------------------- */
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7(
- const char *string, /* UTF-7 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* UTF-7 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7Stateful(
- const char *string, /* UTF-7 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- Py_ssize_t *consumed /* bytes consumed */
+ const char *string, /* UTF-7 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ Py_ssize_t *consumed /* bytes consumed */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* number of Py_UNICODE chars to encode */
- int base64SetO, /* Encode RFC2152 Set O characters in base64 */
- int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* number of Py_UNICODE chars to encode */
+ int base64SetO, /* Encode RFC2152 Set O characters in base64 */
+ int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
+ const char *errors /* error handling */
);
/* --- UTF-8 Codecs ------------------------------------------------------- */
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8(
- const char *string, /* UTF-8 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* UTF-8 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8Stateful(
- const char *string, /* UTF-8 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- Py_ssize_t *consumed /* bytes consumed */
+ const char *string, /* UTF-8 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ Py_ssize_t *consumed /* bytes consumed */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* number of Py_UNICODE chars to encode */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* number of Py_UNICODE chars to encode */
+ const char *errors /* error handling */
);
/* --- UTF-32 Codecs ------------------------------------------------------ */
@@ -877,14 +877,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
the corresponding Unicode object.
errors (if non-NULL) defines the error handling. It defaults
- to "strict".
+ to "strict".
If byteorder is non-NULL, the decoder starts decoding using the
given byte order:
- *byteorder == -1: little endian
- *byteorder == 0: native order
- *byteorder == 1: big endian
+ *byteorder == -1: little endian
+ *byteorder == 0: native order
+ *byteorder == 1: big endian
In native mode, the first four bytes of the stream are checked for a
BOM mark. If found, the BOM mark is analysed, the byte order
@@ -897,29 +897,29 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
*/
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32(
- const char *string, /* UTF-32 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- int *byteorder /* pointer to byteorder to use
- 0=native;-1=LE,1=BE; updated on
- exit */
+ const char *string, /* UTF-32 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ int *byteorder /* pointer to byteorder to use
+ 0=native;-1=LE,1=BE; updated on
+ exit */
);
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32Stateful(
- const char *string, /* UTF-32 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- int *byteorder, /* pointer to byteorder to use
- 0=native;-1=LE,1=BE; updated on
- exit */
- Py_ssize_t *consumed /* bytes consumed */
+ const char *string, /* UTF-32 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ int *byteorder, /* pointer to byteorder to use
+ 0=native;-1=LE,1=BE; updated on
+ exit */
+ Py_ssize_t *consumed /* bytes consumed */
);
/* Returns a Python string using the UTF-32 encoding in native byte
order. The string always starts with a BOM mark. */
PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
/* Returns a Python string object holding the UTF-32 encoded value of
@@ -939,10 +939,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
*/
PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* number of Py_UNICODE chars to encode */
- const char *errors, /* error handling */
- int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* number of Py_UNICODE chars to encode */
+ const char *errors, /* error handling */
+ int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
/* --- UTF-16 Codecs ------------------------------------------------------ */
@@ -951,14 +951,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
the corresponding Unicode object.
errors (if non-NULL) defines the error handling. It defaults
- to "strict".
+ to "strict".
If byteorder is non-NULL, the decoder starts decoding using the
given byte order:
- *byteorder == -1: little endian
- *byteorder == 0: native order
- *byteorder == 1: big endian
+ *byteorder == -1: little endian
+ *byteorder == 0: native order
+ *byteorder == 1: big endian
In native mode, the first two bytes of the stream are checked for a
BOM mark. If found, the BOM mark is analysed, the byte order
@@ -971,29 +971,29 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
*/
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16(
- const char *string, /* UTF-16 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- int *byteorder /* pointer to byteorder to use
- 0=native;-1=LE,1=BE; updated on
- exit */
+ const char *string, /* UTF-16 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ int *byteorder /* pointer to byteorder to use
+ 0=native;-1=LE,1=BE; updated on
+ exit */
);
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16Stateful(
- const char *string, /* UTF-16 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors, /* error handling */
- int *byteorder, /* pointer to byteorder to use
- 0=native;-1=LE,1=BE; updated on
- exit */
- Py_ssize_t *consumed /* bytes consumed */
+ const char *string, /* UTF-16 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors, /* error handling */
+ int *byteorder, /* pointer to byteorder to use
+ 0=native;-1=LE,1=BE; updated on
+ exit */
+ Py_ssize_t *consumed /* bytes consumed */
);
/* Returns a Python string using the UTF-16 encoding in native byte
order. The string always starts with a BOM mark. */
PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
/* Returns a Python string object holding the UTF-16 encoded value of
@@ -1017,44 +1017,44 @@ PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
*/
PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* number of Py_UNICODE chars to encode */
- const char *errors, /* error handling */
- int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* number of Py_UNICODE chars to encode */
+ const char *errors, /* error handling */
+ int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
);
/* --- Unicode-Escape Codecs ---------------------------------------------- */
PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
- const char *string, /* Unicode-Escape encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* Unicode-Escape encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length /* Number of Py_UNICODE chars to encode */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length /* Number of Py_UNICODE chars to encode */
);
/* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
PyAPI_FUNC(PyObject*) PyUnicode_DecodeRawUnicodeEscape(
- const char *string, /* Raw-Unicode-Escape encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* Raw-Unicode-Escape encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length /* Number of Py_UNICODE chars to encode */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length /* Number of Py_UNICODE chars to encode */
);
/* --- Unicode Internal Codec ---------------------------------------------
@@ -1067,53 +1067,53 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(
const char *errors
);
-/* --- Latin-1 Codecs -----------------------------------------------------
+/* --- Latin-1 Codecs -----------------------------------------------------
Note: Latin-1 corresponds to the first 256 Unicode ordinals.
*/
PyAPI_FUNC(PyObject*) PyUnicode_DecodeLatin1(
- const char *string, /* Latin-1 encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* Latin-1 encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsLatin1String(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
+ const char *errors /* error handling */
);
-/* --- ASCII Codecs -------------------------------------------------------
+/* --- ASCII Codecs -------------------------------------------------------
Only 7-bit ASCII data is excepted. All other codes generate errors.
*/
PyAPI_FUNC(PyObject*) PyUnicode_DecodeASCII(
- const char *string, /* ASCII encoded string */
- Py_ssize_t length, /* size of string */
- const char *errors /* error handling */
+ const char *string, /* ASCII encoded string */
+ Py_ssize_t length, /* size of string */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsASCIIString(
- PyObject *unicode /* Unicode object */
+ PyObject *unicode /* Unicode object */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
+ const char *errors /* error handling */
);
-/* --- Character Map Codecs -----------------------------------------------
+/* --- Character Map Codecs -----------------------------------------------
- This codec uses mappings to encode and decode characters.
+ This codec uses mappings to encode and decode characters.
Decoding mappings must map single string characters to single
Unicode characters, integers (which are then interpreted as Unicode
@@ -1134,25 +1134,25 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
*/
PyAPI_FUNC(PyObject*) PyUnicode_DecodeCharmap(
- const char *string, /* Encoded string */
- Py_ssize_t length, /* size of string */
- PyObject *mapping, /* character mapping
- (char ordinal -> unicode ordinal) */
- const char *errors /* error handling */
+ const char *string, /* Encoded string */
+ Py_ssize_t length, /* size of string */
+ PyObject *mapping, /* character mapping
+ (char ordinal -> unicode ordinal) */
+ const char *errors /* error handling */
);
PyAPI_FUNC(PyObject*) PyUnicode_AsCharmapString(
- PyObject *unicode, /* Unicode object */
- PyObject *mapping /* character mapping
- (unicode ordinal -> char ordinal) */
+ PyObject *unicode, /* Unicode object */
+ PyObject *mapping /* character mapping
+ (unicode ordinal -> char ordinal) */
);
PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
- PyObject *mapping, /* character mapping
- (unicode ordinal -> char ordinal) */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
+ PyObject *mapping, /* character mapping
+ (unicode ordinal -> char ordinal) */
+ const char *errors /* error handling */
);
/* Translate a Py_UNICODE buffer of the given length by applying a
@@ -1160,7 +1160,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
object.
The mapping table must map Unicode ordinal integers to Unicode
- ordinal integers or None (causing deletion of the character).
+ ordinal integers or None (causing deletion of the character).
Mapping tables may be dictionaries or sequences. Unmapped character
ordinals (ones which cause a LookupError) are left untouched and
@@ -1169,10 +1169,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
*/
PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
- const Py_UNICODE *data, /* Unicode char buffer */
- Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
- PyObject *table, /* Translate table */
- const char *errors /* error handling */
+ const Py_UNICODE *data, /* Unicode char buffer */
+ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
+ PyObject *table, /* Translate table */
+ const char *errors /* error handling */
);
#ifdef MS_WIN32
@@ -1221,7 +1221,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
NULL or "strict": raise a ValueError
"ignore": ignore the wrong characters (these are not copied to the
- output buffer)
+ output buffer)
"replace": replaces illegal characters with '?'
Returns 0 on success, -1 on failure.
@@ -1229,10 +1229,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
*/
PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
- Py_UNICODE *s, /* Unicode buffer */
- Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
- char *output, /* Output buffer; must have size >= length */
- const char *errors /* error handling */
+ Py_UNICODE *s, /* Unicode buffer */
+ Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
+ char *output, /* Output buffer; must have size >= length */
+ const char *errors /* error handling */
);
/* --- File system encoding ---------------------------------------------- */
@@ -1271,24 +1271,24 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
/* Concat two strings giving a new Unicode string. */
PyAPI_FUNC(PyObject*) PyUnicode_Concat(
- PyObject *left, /* Left string */
- PyObject *right /* Right string */
+ PyObject *left, /* Left string */
+ PyObject *right /* Right string */
);
/* Concat two strings and put the result in *pleft
(sets *pleft to NULL on error) */
PyAPI_FUNC(void) PyUnicode_Append(
- PyObject **pleft, /* Pointer to left string */
- PyObject *right /* Right string */
+ PyObject **pleft, /* Pointer to left string */
+ PyObject *right /* Right string */
);
/* Concat two strings, put the result in *pleft and drop the right object
(sets *pleft to NULL on error) */
PyAPI_FUNC(void) PyUnicode_AppendAndDel(
- PyObject **pleft, /* Pointer to left string */
- PyObject *right /* Right string */
+ PyObject **pleft, /* Pointer to left string */
+ PyObject *right /* Right string */
);
/* Split a string giving a list of Unicode strings.
@@ -1303,35 +1303,35 @@ PyAPI_FUNC(void) PyUnicode_AppendAndDel(
*/
PyAPI_FUNC(PyObject*) PyUnicode_Split(
- PyObject *s, /* String to split */
- PyObject *sep, /* String separator */
- Py_ssize_t maxsplit /* Maxsplit count */
- );
+ PyObject *s, /* String to split */
+ PyObject *sep, /* String separator */
+ Py_ssize_t maxsplit /* Maxsplit count */
+ );
/* Dito, but split at line breaks.
CRLF is considered to be one line break. Line breaks are not
included in the resulting list. */
-
+
PyAPI_FUNC(PyObject*) PyUnicode_Splitlines(
- PyObject *s, /* String to split */
- int keepends /* If true, line end markers are included */
- );
+ PyObject *s, /* String to split */
+ int keepends /* If true, line end markers are included */
+ );
/* Partition a string using a given separator. */
PyAPI_FUNC(PyObject*) PyUnicode_Partition(
- PyObject *s, /* String to partition */
- PyObject *sep /* String separator */
- );
+ PyObject *s, /* String to partition */
+ PyObject *sep /* String separator */
+ );
/* Partition a string using a given separator, searching from the end of the
string. */
PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
- PyObject *s, /* String to partition */
- PyObject *sep /* String separator */
- );
+ PyObject *s, /* String to partition */
+ PyObject *sep /* String separator */
+ );
/* Split a string giving a list of Unicode strings.
@@ -1347,16 +1347,16 @@ PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
*/
PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
- PyObject *s, /* String to split */
- PyObject *sep, /* String separator */
- Py_ssize_t maxsplit /* Maxsplit count */
- );
+ PyObject *s, /* String to split */
+ PyObject *sep, /* String separator */
+ Py_ssize_t maxsplit /* Maxsplit count */
+ );
/* Translate a string by applying a character mapping table to it and
return the resulting Unicode object.
The mapping table must map Unicode ordinal integers to Unicode
- ordinal integers or None (causing deletion of the character).
+ ordinal integers or None (causing deletion of the character).
Mapping tables may be dictionaries or sequences. Unmapped character
ordinals (ones which cause a LookupError) are left untouched and
@@ -1365,28 +1365,28 @@ PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
*/
PyAPI_FUNC(PyObject *) PyUnicode_Translate(
- PyObject *str, /* String */
- PyObject *table, /* Translate table */
- const char *errors /* error handling */
+ PyObject *str, /* String */
+ PyObject *table, /* Translate table */
+ const char *errors /* error handling */
);
/* Join a sequence of strings using the given separator and return
the resulting Unicode string. */
-
+
PyAPI_FUNC(PyObject*) PyUnicode_Join(
- PyObject *separator, /* Separator string */
- PyObject *seq /* Sequence object */
+ PyObject *separator, /* Separator string */
+ PyObject *seq /* Sequence object */
);
/* Return 1 if substr matches str[start:end] at the given tail end, 0
otherwise. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
- PyObject *str, /* String */
- PyObject *substr, /* Prefix or Suffix string */
- Py_ssize_t start, /* Start index */
- Py_ssize_t end, /* Stop index */
- int direction /* Tail end: -1 prefix, +1 suffix */
+ PyObject *str, /* String */
+ PyObject *substr, /* Prefix or Suffix string */
+ Py_ssize_t start, /* Start index */
+ Py_ssize_t end, /* Stop index */
+ int direction /* Tail end: -1 prefix, +1 suffix */
);
/* Return the first position of substr in str[start:end] using the
@@ -1394,39 +1394,39 @@ PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
an error occurred and an exception is set. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
- PyObject *str, /* String */
- PyObject *substr, /* Substring to find */
- Py_ssize_t start, /* Start index */
- Py_ssize_t end, /* Stop index */
- int direction /* Find direction: +1 forward, -1 backward */
+ PyObject *str, /* String */
+ PyObject *substr, /* Substring to find */
+ Py_ssize_t start, /* Start index */
+ Py_ssize_t end, /* Stop index */
+ int direction /* Find direction: +1 forward, -1 backward */
);
/* Count the number of occurrences of substr in str[start:end]. */
PyAPI_FUNC(Py_ssize_t) PyUnicode_Count(
- PyObject *str, /* String */
- PyObject *substr, /* Substring to count */
- Py_ssize_t start, /* Start index */
- Py_ssize_t end /* Stop index */
+ PyObject *str, /* String */
+ PyObject *substr, /* Substring to count */
+ Py_ssize_t start, /* Start index */
+ Py_ssize_t end /* Stop index */
);
/* Replace at most maxcount occurrences of substr in str with replstr
and return the resulting Unicode object. */
PyAPI_FUNC(PyObject *) PyUnicode_Replace(
- PyObject *str, /* String */
- PyObject *substr, /* Substring to find */
- PyObject *replstr, /* Substring to replace */
- Py_ssize_t maxcount /* Max. number of replacements to apply;
- -1 = all */
+ PyObject *str, /* String */
+ PyObject *substr, /* Substring to find */
+ PyObject *replstr, /* Substring to replace */
+ Py_ssize_t maxcount /* Max. number of replacements to apply;
+ -1 = all */
);
/* Compare two strings and return -1, 0, 1 for less than, equal,
greater than resp. */
PyAPI_FUNC(int) PyUnicode_Compare(
- PyObject *left, /* Left string */
- PyObject *right /* Right string */
+ PyObject *left, /* Left string */
+ PyObject *right /* Right string */
);
PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
@@ -1451,17 +1451,17 @@ PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
*/
PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
- PyObject *left, /* Left string */
- PyObject *right, /* Right string */
- int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
+ PyObject *left, /* Left string */
+ PyObject *right, /* Right string */
+ int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
);
/* Apply a argument tuple or dictionary to a format string and return
the resulting Unicode string. */
PyAPI_FUNC(PyObject *) PyUnicode_Format(
- PyObject *format, /* Format string */
- PyObject *args /* Argument tuple or dictionary */
+ PyObject *format, /* Format string */
+ PyObject *args /* Argument tuple or dictionary */
);
/* Checks whether element is contained in container and return 1/0
@@ -1471,8 +1471,8 @@ PyAPI_FUNC(PyObject *) PyUnicode_Format(
returned in case of an error. */
PyAPI_FUNC(int) PyUnicode_Contains(
- PyObject *container, /* Container string */
- PyObject *element /* Element string */
+ PyObject *container, /* Container string */
+ PyObject *element /* Element string */
);
/* Checks whether argument is a valid identifier. */
@@ -1513,82 +1513,82 @@ PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
/* These should not be used directly. Use the Py_UNICODE_IS* and
- Py_UNICODE_TO* macros instead.
+ Py_UNICODE_TO* macros instead.
These APIs are implemented in Objects/unicodectype.c.
*/
PyAPI_FUNC(int) _PyUnicode_IsLowercase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsUppercase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsXidStart(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
- const Py_UNICODE ch /* Unicode character */
+ const Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
- const Py_UNICODE ch /* Unicode character */
+ const Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToLowercase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToUppercase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToTitlecase(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_ToDigit(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(double) _PyUnicode_ToNumeric(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsDigit(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsNumeric(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsPrintable(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(int) _PyUnicode_IsAlpha(
- Py_UNICODE ch /* Unicode character */
+ Py_UNICODE ch /* Unicode character */
);
PyAPI_FUNC(size_t) Py_UNICODE_strlen(const Py_UNICODE *u);
diff --git a/Misc/setuid-prog.c b/Misc/setuid-prog.c
index 2841acd..3785d99 100644
--- a/Misc/setuid-prog.c
+++ b/Misc/setuid-prog.c
@@ -21,28 +21,28 @@
Assuming the script is a Bourne shell script, the first line of the
script should be
- #!/bin/sh -
+ #!/bin/sh -
The - is important, don't omit it. If you're using esh, the first
line should be
- #!/usr/local/bin/esh -f
+ #!/usr/local/bin/esh -f
and for ksh, the first line should be
- #!/usr/local/bin/ksh -p
+ #!/usr/local/bin/ksh -p
The script should then set the variable IFS to the string
consisting of <space>, <tab>, and <newline>. After this (*not*
before!), the PATH variable should be set to a reasonable value and
exported. Do not expect the PATH to have a reasonable value, so do
not trust the old value of PATH. You should then set the umask of
the program by calling
- umask 077 # or 022 if you want the files to be readable
+ umask 077 # or 022 if you want the files to be readable
If you plan to change directories, you should either unset CDPATH
or set it to a good value. Setting CDPATH to just ``.'' (dot) is a
good idea.
If, for some reason, you want to use csh, the first line should be
- #!/bin/csh -fb
+ #!/bin/csh -fb
You should then set the path variable to something reasonable,
without trusting the inherited path. Here too, you should set the
umask using the command
- umask 077 # or 022 if you want the files to be readable
+ umask 077 # or 022 if you want the files to be readable
*/
#include <unistd.h>
@@ -54,14 +54,14 @@
/* CONFIGURATION SECTION */
-#ifndef FULL_PATH /* so that this can be specified from the Makefile */
+#ifndef FULL_PATH /* so that this can be specified from the Makefile */
/* Uncomment the following line:
-#define FULL_PATH "/full/path/of/script"
+#define FULL_PATH "/full/path/of/script"
* Then comment out the #error line. */
#error "You must define FULL_PATH somewhere"
#endif
#ifndef UMASK
-#define UMASK 077
+#define UMASK 077
#endif
/* END OF CONFIGURATION SECTION */
@@ -101,76 +101,76 @@ char def_ENV[] = "ENV=:";
void
clean_environ(void)
{
- char **p;
- extern char **environ;
-
- for (p = environ; *p; p++) {
- if (strncmp(*p, "LD_", 3) == 0)
- **p = 'X';
- else if (strncmp(*p, "_RLD", 4) == 0)
- **p = 'X';
- else if (strncmp(*p, "PYTHON", 6) == 0)
- **p = 'X';
- else if (strncmp(*p, "IFS=", 4) == 0)
- *p = def_IFS;
- else if (strncmp(*p, "CDPATH=", 7) == 0)
- *p = def_CDPATH;
- else if (strncmp(*p, "ENV=", 4) == 0)
- *p = def_ENV;
- }
- putenv(def_PATH);
+ char **p;
+ extern char **environ;
+
+ for (p = environ; *p; p++) {
+ if (strncmp(*p, "LD_", 3) == 0)
+ **p = 'X';
+ else if (strncmp(*p, "_RLD", 4) == 0)
+ **p = 'X';
+ else if (strncmp(*p, "PYTHON", 6) == 0)
+ **p = 'X';
+ else if (strncmp(*p, "IFS=", 4) == 0)
+ *p = def_IFS;
+ else if (strncmp(*p, "CDPATH=", 7) == 0)
+ *p = def_CDPATH;
+ else if (strncmp(*p, "ENV=", 4) == 0)
+ *p = def_ENV;
+ }
+ putenv(def_PATH);
}
int
main(int argc, char **argv)
{
- struct stat statb;
- gid_t egid = getegid();
- uid_t euid = geteuid();
-
- /*
- Sanity check #1.
- This check should be made compile-time, but that's not possible.
- If you're sure that you specified a full path name for FULL_PATH,
- you can omit this check.
- */
- if (FULL_PATH[0] != '/') {
- fprintf(stderr, "%s: %s is not a full path name\n", argv[0],
- FULL_PATH);
- fprintf(stderr, "You can only use this wrapper if you\n");
- fprintf(stderr, "compile it with an absolute path.\n");
- exit(1);
- }
-
- /*
- Sanity check #2.
- Check that the owner of the script is equal to either the
- effective uid or the super user.
- */
- if (stat(FULL_PATH, &statb) < 0) {
- perror("stat");
- exit(1);
- }
- if (statb.st_uid != 0 && statb.st_uid != euid) {
- fprintf(stderr, "%s: %s has the wrong owner\n", argv[0],
- FULL_PATH);
- fprintf(stderr, "The script should be owned by root,\n");
- fprintf(stderr, "and shouldn't be writable by anyone.\n");
- exit(1);
- }
-
- if (setregid(egid, egid) < 0)
- perror("setregid");
- if (setreuid(euid, euid) < 0)
- perror("setreuid");
-
- clean_environ();
-
- umask(UMASK);
-
- while (**argv == '-') /* don't let argv[0] start with '-' */
- (*argv)++;
- execv(FULL_PATH, argv);
- fprintf(stderr, "%s: could not execute the script\n", argv[0]);
- exit(1);
+ struct stat statb;
+ gid_t egid = getegid();
+ uid_t euid = geteuid();
+
+ /*
+ Sanity check #1.
+ This check should be made compile-time, but that's not possible.
+ If you're sure that you specified a full path name for FULL_PATH,
+ you can omit this check.
+ */
+ if (FULL_PATH[0] != '/') {
+ fprintf(stderr, "%s: %s is not a full path name\n", argv[0],
+ FULL_PATH);
+ fprintf(stderr, "You can only use this wrapper if you\n");
+ fprintf(stderr, "compile it with an absolute path.\n");
+ exit(1);
+ }
+
+ /*
+ Sanity check #2.
+ Check that the owner of the script is equal to either the
+ effective uid or the super user.
+ */
+ if (stat(FULL_PATH, &statb) < 0) {
+ perror("stat");
+ exit(1);
+ }
+ if (statb.st_uid != 0 && statb.st_uid != euid) {
+ fprintf(stderr, "%s: %s has the wrong owner\n", argv[0],
+ FULL_PATH);
+ fprintf(stderr, "The script should be owned by root,\n");
+ fprintf(stderr, "and shouldn't be writable by anyone.\n");
+ exit(1);
+ }
+
+ if (setregid(egid, egid) < 0)
+ perror("setregid");
+ if (setreuid(euid, euid) < 0)
+ perror("setreuid");
+
+ clean_environ();
+
+ umask(UMASK);
+
+ while (**argv == '-') /* don't let argv[0] start with '-' */
+ (*argv)++;
+ execv(FULL_PATH, argv);
+ fprintf(stderr, "%s: could not execute the script\n", argv[0]);
+ exit(1);
}
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index fe2e110..f65a583 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -8,51 +8,51 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
static Py_ssize_t
internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
{
- PyObject *litem;
- Py_ssize_t mid, res;
-
- if (lo < 0) {
- PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
- return -1;
- }
- if (hi == -1) {
- hi = PySequence_Size(list);
- if (hi < 0)
- return -1;
- }
- while (lo < hi) {
- mid = (lo + hi) / 2;
- litem = PySequence_GetItem(list, mid);
- if (litem == NULL)
- return -1;
- res = PyObject_RichCompareBool(item, litem, Py_LT);
- Py_DECREF(litem);
- if (res < 0)
- return -1;
- if (res)
- hi = mid;
- else
- lo = mid + 1;
- }
- return lo;
+ PyObject *litem;
+ Py_ssize_t mid, res;
+
+ if (lo < 0) {
+ PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
+ return -1;
+ }
+ if (hi == -1) {
+ hi = PySequence_Size(list);
+ if (hi < 0)
+ return -1;
+ }
+ while (lo < hi) {
+ mid = (lo + hi) / 2;
+ litem = PySequence_GetItem(list, mid);
+ if (litem == NULL)
+ return -1;
+ res = PyObject_RichCompareBool(item, litem, Py_LT);
+ Py_DECREF(litem);
+ if (res < 0)
+ return -1;
+ if (res)
+ hi = mid;
+ else
+ lo = mid + 1;
+ }
+ return lo;
}
static PyObject *
bisect_right(PyObject *self, PyObject *args, PyObject *kw)
{
- PyObject *list, *item;
- Py_ssize_t lo = 0;
- Py_ssize_t hi = -1;
- Py_ssize_t index;
- static char *keywords[] = {"a", "x", "lo", "hi", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
- keywords, &list, &item, &lo, &hi))
- return NULL;
- index = internal_bisect_right(list, item, lo, hi);
- if (index < 0)
- return NULL;
- return PyLong_FromSsize_t(index);
+ PyObject *list, *item;
+ Py_ssize_t lo = 0;
+ Py_ssize_t hi = -1;
+ Py_ssize_t index;
+ static char *keywords[] = {"a", "x", "lo", "hi", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
+ keywords, &list, &item, &lo, &hi))
+ return NULL;
+ index = internal_bisect_right(list, item, lo, hi);
+ if (index < 0)
+ return NULL;
+ return PyLong_FromSsize_t(index);
}
PyDoc_STRVAR(bisect_right_doc,
@@ -70,30 +70,30 @@ slice of a to be searched.\n");
static PyObject *
insort_right(PyObject *self, PyObject *args, PyObject *kw)
{
- PyObject *list, *item, *result;
- Py_ssize_t lo = 0;
- Py_ssize_t hi = -1;
- Py_ssize_t index;
- static char *keywords[] = {"a", "x", "lo", "hi", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
- keywords, &list, &item, &lo, &hi))
- return NULL;
- index = internal_bisect_right(list, item, lo, hi);
- if (index < 0)
- return NULL;
- if (PyList_CheckExact(list)) {
- if (PyList_Insert(list, index, item) < 0)
- return NULL;
- } else {
- result = PyObject_CallMethod(list, "insert", "nO",
- index, item);
- if (result == NULL)
- return NULL;
- Py_DECREF(result);
- }
-
- Py_RETURN_NONE;
+ PyObject *list, *item, *result;
+ Py_ssize_t lo = 0;
+ Py_ssize_t hi = -1;
+ Py_ssize_t index;
+ static char *keywords[] = {"a", "x", "lo", "hi", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
+ keywords, &list, &item, &lo, &hi))
+ return NULL;
+ index = internal_bisect_right(list, item, lo, hi);
+ if (index < 0)
+ return NULL;
+ if (PyList_CheckExact(list)) {
+ if (PyList_Insert(list, index, item) < 0)
+ return NULL;
+ } else {
+ result = PyObject_CallMethod(list, "insert", "nO",
+ index, item);
+ if (result == NULL)
+ return NULL;
+ Py_DECREF(result);
+ }
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(insort_right_doc,
@@ -109,51 +109,51 @@ slice of a to be searched.\n");
static Py_ssize_t
internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
{
- PyObject *litem;
- Py_ssize_t mid, res;
-
- if (lo < 0) {
- PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
- return -1;
- }
- if (hi == -1) {
- hi = PySequence_Size(list);
- if (hi < 0)
- return -1;
- }
- while (lo < hi) {
- mid = (lo + hi) / 2;
- litem = PySequence_GetItem(list, mid);
- if (litem == NULL)
- return -1;
- res = PyObject_RichCompareBool(litem, item, Py_LT);
- Py_DECREF(litem);
- if (res < 0)
- return -1;
- if (res)
- lo = mid + 1;
- else
- hi = mid;
- }
- return lo;
+ PyObject *litem;
+ Py_ssize_t mid, res;
+
+ if (lo < 0) {
+ PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
+ return -1;
+ }
+ if (hi == -1) {
+ hi = PySequence_Size(list);
+ if (hi < 0)
+ return -1;
+ }
+ while (lo < hi) {
+ mid = (lo + hi) / 2;
+ litem = PySequence_GetItem(list, mid);
+ if (litem == NULL)
+ return -1;
+ res = PyObject_RichCompareBool(litem, item, Py_LT);
+ Py_DECREF(litem);
+ if (res < 0)
+ return -1;
+ if (res)
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+ return lo;
}
static PyObject *
bisect_left(PyObject *self, PyObject *args, PyObject *kw)
{
- PyObject *list, *item;
- Py_ssize_t lo = 0;
- Py_ssize_t hi = -1;
- Py_ssize_t index;
- static char *keywords[] = {"a", "x", "lo", "hi", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
- keywords, &list, &item, &lo, &hi))
- return NULL;
- index = internal_bisect_left(list, item, lo, hi);
- if (index < 0)
- return NULL;
- return PyLong_FromSsize_t(index);
+ PyObject *list, *item;
+ Py_ssize_t lo = 0;
+ Py_ssize_t hi = -1;
+ Py_ssize_t index;
+ static char *keywords[] = {"a", "x", "lo", "hi", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
+ keywords, &list, &item, &lo, &hi))
+ return NULL;
+ index = internal_bisect_left(list, item, lo, hi);
+ if (index < 0)
+ return NULL;
+ return PyLong_FromSsize_t(index);
}
PyDoc_STRVAR(bisect_left_doc,
@@ -171,30 +171,30 @@ slice of a to be searched.\n");
static PyObject *
insort_left(PyObject *self, PyObject *args, PyObject *kw)
{
- PyObject *list, *item, *result;
- Py_ssize_t lo = 0;
- Py_ssize_t hi = -1;
- Py_ssize_t index;
- static char *keywords[] = {"a", "x", "lo", "hi", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
- keywords, &list, &item, &lo, &hi))
- return NULL;
- index = internal_bisect_left(list, item, lo, hi);
- if (index < 0)
- return NULL;
- if (PyList_CheckExact(list)) {
- if (PyList_Insert(list, index, item) < 0)
- return NULL;
- } else {
- result = PyObject_CallMethod(list, "insert", "iO",
- index, item);
- if (result == NULL)
- return NULL;
- Py_DECREF(result);
- }
-
- Py_RETURN_NONE;
+ PyObject *list, *item, *result;
+ Py_ssize_t lo = 0;
+ Py_ssize_t hi = -1;
+ Py_ssize_t index;
+ static char *keywords[] = {"a", "x", "lo", "hi", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
+ keywords, &list, &item, &lo, &hi))
+ return NULL;
+ index = internal_bisect_left(list, item, lo, hi);
+ if (index < 0)
+ return NULL;
+ if (PyList_CheckExact(list)) {
+ if (PyList_Insert(list, index, item) < 0)
+ return NULL;
+ } else {
+ result = PyObject_CallMethod(list, "insert", "iO",
+ index, item);
+ if (result == NULL)
+ return NULL;
+ Py_DECREF(result);
+ }
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(insort_left_doc,
@@ -211,19 +211,19 @@ PyDoc_STRVAR(bisect_doc, "Alias for bisect_right().\n");
PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n");
static PyMethodDef bisect_methods[] = {
- {"bisect_right", (PyCFunction)bisect_right,
- METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
- {"bisect", (PyCFunction)bisect_right,
- METH_VARARGS|METH_KEYWORDS, bisect_doc},
- {"insort_right", (PyCFunction)insort_right,
- METH_VARARGS|METH_KEYWORDS, insort_right_doc},
- {"insort", (PyCFunction)insort_right,
- METH_VARARGS|METH_KEYWORDS, insort_doc},
- {"bisect_left", (PyCFunction)bisect_left,
- METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
- {"insort_left", (PyCFunction)insort_left,
- METH_VARARGS|METH_KEYWORDS, insort_left_doc},
- {NULL, NULL} /* sentinel */
+ {"bisect_right", (PyCFunction)bisect_right,
+ METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
+ {"bisect", (PyCFunction)bisect_right,
+ METH_VARARGS|METH_KEYWORDS, bisect_doc},
+ {"insort_right", (PyCFunction)insort_right,
+ METH_VARARGS|METH_KEYWORDS, insort_right_doc},
+ {"insort", (PyCFunction)insort_right,
+ METH_VARARGS|METH_KEYWORDS, insort_doc},
+ {"bisect_left", (PyCFunction)bisect_left,
+ METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
+ {"insort_left", (PyCFunction)insort_left,
+ METH_VARARGS|METH_KEYWORDS, insort_left_doc},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc,
@@ -236,19 +236,19 @@ common approach.\n");
static struct PyModuleDef _bisectmodule = {
- PyModuleDef_HEAD_INIT,
- "_bisect",
- module_doc,
- -1,
- bisect_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_bisect",
+ module_doc,
+ -1,
+ bisect_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__bisect(void)
{
- return PyModule_Create(&_bisectmodule);
+ return PyModule_Create(&_bisectmodule);
}
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 55b593b..0958f9c 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -15,7 +15,7 @@
The builtin Unicode codecs use the following interface:
<encoding>_encode(Unicode_object[,errors='strict']) ->
- (string object, bytes consumed)
+ (string object, bytes consumed)
<encoding>_decode(char_buffer_obj[,errors='strict']) ->
(Unicode object, bytes consumed)
@@ -95,7 +95,7 @@ codec_encode(PyObject *self, PyObject *args)
return NULL;
if (encoding == NULL)
- encoding = PyUnicode_GetDefaultEncoding();
+ encoding = PyUnicode_GetDefaultEncoding();
/* Encode via the codec registry */
return PyCodec_Encode(v, encoding, errors);
@@ -122,7 +122,7 @@ codec_decode(PyObject *self, PyObject *args)
return NULL;
if (encoding == NULL)
- encoding = PyUnicode_GetDefaultEncoding();
+ encoding = PyUnicode_GetDefaultEncoding();
/* Decode via the codec registry */
return PyCodec_Decode(v, encoding, errors);
@@ -132,7 +132,7 @@ codec_decode(PyObject *self, PyObject *args)
static
PyObject *codec_tuple(PyObject *unicode,
- Py_ssize_t len)
+ Py_ssize_t len)
{
PyObject *v;
if (unicode == NULL)
@@ -145,86 +145,86 @@ PyObject *codec_tuple(PyObject *unicode,
/* --- String codecs ------------------------------------------------------ */
static PyObject *
escape_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
const char *errors = NULL;
const char *data;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "s#|z:escape_decode",
- &data, &size, &errors))
- return NULL;
+ &data, &size, &errors))
+ return NULL;
return codec_tuple(PyBytes_DecodeEscape(data, size, errors, 0, NULL),
- size);
+ size);
}
static PyObject *
escape_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- static const char *hexdigits = "0123456789abcdef";
- PyObject *str;
- Py_ssize_t size;
- Py_ssize_t newsize;
- const char *errors = NULL;
- PyObject *v;
-
- if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
- &PyBytes_Type, &str, &errors))
- return NULL;
-
- size = PyBytes_GET_SIZE(str);
- newsize = 4*size;
- if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
- PyErr_SetString(PyExc_OverflowError,
- "string is too large to encode");
- return NULL;
- }
- v = PyBytes_FromStringAndSize(NULL, newsize);
-
- if (v == NULL) {
- return NULL;
- }
- else {
- register Py_ssize_t i;
- register char c;
- register char *p = PyBytes_AS_STRING(v);
-
- for (i = 0; i < size; i++) {
- /* There's at least enough room for a hex escape */
- assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
- c = PyBytes_AS_STRING(str)[i];
- if (c == '\'' || c == '\\')
- *p++ = '\\', *p++ = c;
- else if (c == '\t')
- *p++ = '\\', *p++ = 't';
- else if (c == '\n')
- *p++ = '\\', *p++ = 'n';
- else if (c == '\r')
- *p++ = '\\', *p++ = 'r';
- else if (c < ' ' || c >= 0x7f) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(c & 0xf0) >> 4];
- *p++ = hexdigits[c & 0xf];
- }
- else
- *p++ = c;
- }
- *p = '\0';
- if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
- return NULL;
- }
- }
-
- return codec_tuple(v, PyBytes_Size(v));
+ static const char *hexdigits = "0123456789abcdef";
+ PyObject *str;
+ Py_ssize_t size;
+ Py_ssize_t newsize;
+ const char *errors = NULL;
+ PyObject *v;
+
+ if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
+ &PyBytes_Type, &str, &errors))
+ return NULL;
+
+ size = PyBytes_GET_SIZE(str);
+ newsize = 4*size;
+ if (newsize > PY_SSIZE_T_MAX || newsize / 4 != size) {
+ PyErr_SetString(PyExc_OverflowError,
+ "string is too large to encode");
+ return NULL;
+ }
+ v = PyBytes_FromStringAndSize(NULL, newsize);
+
+ if (v == NULL) {
+ return NULL;
+ }
+ else {
+ register Py_ssize_t i;
+ register char c;
+ register char *p = PyBytes_AS_STRING(v);
+
+ for (i = 0; i < size; i++) {
+ /* There's at least enough room for a hex escape */
+ assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
+ c = PyBytes_AS_STRING(str)[i];
+ if (c == '\'' || c == '\\')
+ *p++ = '\\', *p++ = c;
+ else if (c == '\t')
+ *p++ = '\\', *p++ = 't';
+ else if (c == '\n')
+ *p++ = '\\', *p++ = 'n';
+ else if (c == '\r')
+ *p++ = '\\', *p++ = 'r';
+ else if (c < ' ' || c >= 0x7f) {
+ *p++ = '\\';
+ *p++ = 'x';
+ *p++ = hexdigits[(c & 0xf0) >> 4];
+ *p++ = hexdigits[c & 0xf];
+ }
+ else
+ *p++ = c;
+ }
+ *p = '\0';
+ if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
+ return NULL;
+ }
+ }
+
+ return codec_tuple(v, PyBytes_Size(v));
}
/* --- Decoder ------------------------------------------------------------ */
static PyObject *
unicode_internal_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *obj;
const char *errors = NULL;
@@ -232,19 +232,19 @@ unicode_internal_decode(PyObject *self,
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
- &obj, &errors))
- return NULL;
+ &obj, &errors))
+ return NULL;
if (PyUnicode_Check(obj)) {
- Py_INCREF(obj);
- return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
+ Py_INCREF(obj);
+ return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
}
else {
- if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
- return NULL;
+ if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
+ return NULL;
- return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
- size);
+ return codec_tuple(_PyUnicode_DecodeUnicodeInternal(data, size, errors),
+ size);
}
}
@@ -252,20 +252,20 @@ static PyObject *
utf_7_decode(PyObject *self,
PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
PyObject *decoded = NULL;
if (!PyArg_ParseTuple(args, "y*|zi:utf_7_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len;
decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,
- final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
return NULL;
return codec_tuple(decoded, consumed);
@@ -273,32 +273,32 @@ utf_7_decode(PyObject *self,
static PyObject *
utf_8_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
PyObject *decoded = NULL;
if (!PyArg_ParseTuple(args, "y*|zi:utf_8_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len;
decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,
- final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
static PyObject *
utf_16_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
int final = 0;
@@ -306,22 +306,22 @@ utf_16_decode(PyObject *self,
PyObject *decoded;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
static PyObject *
utf_16_le_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = -1;
int final = 0;
@@ -329,23 +329,23 @@ utf_16_le_decode(PyObject *self,
PyObject *decoded = NULL;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_le_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
static PyObject *
utf_16_be_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 1;
int final = 0;
@@ -353,15 +353,15 @@ utf_16_be_decode(PyObject *self,
PyObject *decoded = NULL;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_be_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
@@ -375,9 +375,9 @@ utf_16_be_decode(PyObject *self,
static PyObject *
utf_16_ex_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
PyObject *unicode, *tuple;
@@ -385,14 +385,14 @@ utf_16_ex_decode(PyObject *self,
Py_ssize_t consumed;
if (!PyArg_ParseTuple(args, "y*|zii:utf_16_ex_decode",
- &pbuf, &errors, &byteorder, &final))
- return NULL;
+ &pbuf, &errors, &byteorder, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (unicode == NULL)
- return NULL;
+ return NULL;
tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Py_DECREF(unicode);
return tuple;
@@ -400,9 +400,9 @@ utf_16_ex_decode(PyObject *self,
static PyObject *
utf_32_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
int final = 0;
@@ -410,22 +410,22 @@ utf_32_decode(PyObject *self,
PyObject *decoded;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
static PyObject *
utf_32_le_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = -1;
int final = 0;
@@ -433,22 +433,22 @@ utf_32_le_decode(PyObject *self,
PyObject *decoded;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_le_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
static PyObject *
utf_32_be_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 1;
int final = 0;
@@ -456,14 +456,14 @@ utf_32_be_decode(PyObject *self,
PyObject *decoded;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_be_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
@@ -477,9 +477,9 @@ utf_32_be_decode(PyObject *self,
static PyObject *
utf_32_ex_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int byteorder = 0;
PyObject *unicode, *tuple;
@@ -487,14 +487,14 @@ utf_32_ex_decode(PyObject *self,
Py_ssize_t consumed;
if (!PyArg_ParseTuple(args, "y*|zii:utf_32_ex_decode",
- &pbuf, &errors, &byteorder, &final))
- return NULL;
+ &pbuf, &errors, &byteorder, &final))
+ return NULL;
consumed = pbuf.len; /* This is overwritten unless final is true. */
unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
- &byteorder, final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ &byteorder, final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (unicode == NULL)
- return NULL;
+ return NULL;
tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
Py_DECREF(unicode);
return tuple;
@@ -502,114 +502,114 @@ utf_32_ex_decode(PyObject *self,
static PyObject *
unicode_escape_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
- PyObject *unicode;
+ PyObject *unicode;
if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",
- &pbuf, &errors))
- return NULL;
+ &pbuf, &errors))
+ return NULL;
- unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
raw_unicode_escape_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
- PyObject *unicode;
+ PyObject *unicode;
if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
- &pbuf, &errors))
- return NULL;
+ &pbuf, &errors))
+ return NULL;
- unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
latin_1_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode",
- &pbuf, &errors))
- return NULL;
+ &pbuf, &errors))
+ return NULL;
- unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
ascii_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:ascii_decode",
- &pbuf, &errors))
- return NULL;
+ &pbuf, &errors))
+ return NULL;
- unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
static PyObject *
charmap_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
- PyObject *unicode;
+ Py_buffer pbuf;
+ PyObject *unicode;
const char *errors = NULL;
PyObject *mapping = NULL;
if (!PyArg_ParseTuple(args, "y*|zO:charmap_decode",
- &pbuf, &errors, &mapping))
- return NULL;
+ &pbuf, &errors, &mapping))
+ return NULL;
if (mapping == Py_None)
- mapping = NULL;
+ mapping = NULL;
- unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
- PyBuffer_Release(&pbuf);
- return codec_tuple(unicode, pbuf.len);
+ unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
+ PyBuffer_Release(&pbuf);
+ return codec_tuple(unicode, pbuf.len);
}
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
static PyObject *
mbcs_decode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
- Py_buffer pbuf;
+ Py_buffer pbuf;
const char *errors = NULL;
int final = 0;
Py_ssize_t consumed;
PyObject *decoded = NULL;
if (!PyArg_ParseTuple(args, "y*|zi:mbcs_decode",
- &pbuf, &errors, &final))
- return NULL;
+ &pbuf, &errors, &final))
+ return NULL;
consumed = pbuf.len;
decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,
- final ? NULL : &consumed);
- PyBuffer_Release(&pbuf);
+ final ? NULL : &consumed);
+ PyBuffer_Release(&pbuf);
if (decoded == NULL)
- return NULL;
+ return NULL;
return codec_tuple(decoded, consumed);
}
@@ -619,7 +619,7 @@ mbcs_decode(PyObject *self,
static PyObject *
readbuffer_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
Py_buffer pdata;
const char *data;
@@ -628,8 +628,8 @@ readbuffer_encode(PyObject *self,
PyObject *result;
if (!PyArg_ParseTuple(args, "s*|z:readbuffer_encode",
- &pdata, &errors))
- return NULL;
+ &pdata, &errors))
+ return NULL;
data = pdata.buf;
size = pdata.len;
@@ -640,22 +640,22 @@ readbuffer_encode(PyObject *self,
static PyObject *
charbuffer_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
const char *data;
Py_ssize_t size;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
- &data, &size, &errors))
- return NULL;
+ &data, &size, &errors))
+ return NULL;
return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
}
static PyObject *
unicode_internal_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *obj;
const char *errors = NULL;
@@ -663,64 +663,64 @@ unicode_internal_encode(PyObject *self,
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
- &obj, &errors))
- return NULL;
+ &obj, &errors))
+ return NULL;
if (PyUnicode_Check(obj)) {
- data = PyUnicode_AS_DATA(obj);
- size = PyUnicode_GET_DATA_SIZE(obj);
- return codec_tuple(PyBytes_FromStringAndSize(data, size),
- PyUnicode_GET_SIZE(obj));
+ data = PyUnicode_AS_DATA(obj);
+ size = PyUnicode_GET_DATA_SIZE(obj);
+ return codec_tuple(PyBytes_FromStringAndSize(data, size),
+ PyUnicode_GET_SIZE(obj));
}
else {
- if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
- return NULL;
- return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
+ if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
+ return NULL;
+ return codec_tuple(PyBytes_FromStringAndSize(data, size), size);
}
}
static PyObject *
utf_7_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- 0,
- 0,
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ 0,
+ 0,
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_8_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -734,70 +734,70 @@ utf_8_encode(PyObject *self,
static PyObject *
utf_16_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
int byteorder = 0;
if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
- &str, &errors, &byteorder))
- return NULL;
+ &str, &errors, &byteorder))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- byteorder),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ byteorder),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_16_le_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- -1),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ -1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_16_be_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- +1),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ +1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -811,186 +811,186 @@ utf_16_be_encode(PyObject *self,
static PyObject *
utf_32_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
int byteorder = 0;
if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode",
- &str, &errors, &byteorder))
- return NULL;
+ &str, &errors, &byteorder))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- byteorder),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ byteorder),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_32_le_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- -1),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ -1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
utf_32_be_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- +1),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str),
+ errors,
+ +1),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
unicode_escape_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str)),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
raw_unicode_escape_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str)),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
latin_1_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeLatin1(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
ascii_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeASCII(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
static PyObject *
charmap_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
PyObject *mapping = NULL;
if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
- &str, &errors, &mapping))
- return NULL;
+ &str, &errors, &mapping))
+ return NULL;
if (mapping == Py_None)
- mapping = NULL;
+ mapping = NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeCharmap(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- mapping,
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ mapping,
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -1008,23 +1008,23 @@ charmap_build(PyObject *self, PyObject *args)
static PyObject *
mbcs_encode(PyObject *self,
- PyObject *args)
+ PyObject *args)
{
PyObject *str, *v;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
- &str, &errors))
- return NULL;
+ &str, &errors))
+ return NULL;
str = PyUnicode_FromObject(str);
if (str == NULL)
- return NULL;
+ return NULL;
v = codec_tuple(PyUnicode_EncodeMBCS(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
Py_DECREF(str);
return v;
}
@@ -1048,8 +1048,8 @@ static PyObject *register_error(PyObject *self, PyObject *args)
PyObject *handler;
if (!PyArg_ParseTuple(args, "sO:register_error",
- &name, &handler))
- return NULL;
+ &name, &handler))
+ return NULL;
if (PyCodec_RegisterError(name, handler))
return NULL;
Py_RETURN_NONE;
@@ -1066,82 +1066,82 @@ static PyObject *lookup_error(PyObject *self, PyObject *args)
const char *name;
if (!PyArg_ParseTuple(args, "s:lookup_error",
- &name))
- return NULL;
+ &name))
+ return NULL;
return PyCodec_LookupError(name);
}
/* --- Module API --------------------------------------------------------- */
static PyMethodDef _codecs_functions[] = {
- {"register", codec_register, METH_O,
+ {"register", codec_register, METH_O,
register__doc__},
- {"lookup", codec_lookup, METH_VARARGS,
+ {"lookup", codec_lookup, METH_VARARGS,
lookup__doc__},
- {"encode", codec_encode, METH_VARARGS,
- encode__doc__},
- {"decode", codec_decode, METH_VARARGS,
- decode__doc__},
- {"escape_encode", escape_encode, METH_VARARGS},
- {"escape_decode", escape_decode, METH_VARARGS},
- {"utf_8_encode", utf_8_encode, METH_VARARGS},
- {"utf_8_decode", utf_8_decode, METH_VARARGS},
- {"utf_7_encode", utf_7_encode, METH_VARARGS},
- {"utf_7_decode", utf_7_decode, METH_VARARGS},
- {"utf_16_encode", utf_16_encode, METH_VARARGS},
- {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
- {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
- {"utf_16_decode", utf_16_decode, METH_VARARGS},
- {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
- {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
- {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
- {"utf_32_encode", utf_32_encode, METH_VARARGS},
- {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS},
- {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS},
- {"utf_32_decode", utf_32_decode, METH_VARARGS},
- {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS},
- {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS},
- {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS},
- {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
- {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
- {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
- {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
- {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
- {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
- {"latin_1_encode", latin_1_encode, METH_VARARGS},
- {"latin_1_decode", latin_1_decode, METH_VARARGS},
- {"ascii_encode", ascii_encode, METH_VARARGS},
- {"ascii_decode", ascii_decode, METH_VARARGS},
- {"charmap_encode", charmap_encode, METH_VARARGS},
- {"charmap_decode", charmap_decode, METH_VARARGS},
- {"charmap_build", charmap_build, METH_VARARGS},
- {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
- {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
+ {"encode", codec_encode, METH_VARARGS,
+ encode__doc__},
+ {"decode", codec_decode, METH_VARARGS,
+ decode__doc__},
+ {"escape_encode", escape_encode, METH_VARARGS},
+ {"escape_decode", escape_decode, METH_VARARGS},
+ {"utf_8_encode", utf_8_encode, METH_VARARGS},
+ {"utf_8_decode", utf_8_decode, METH_VARARGS},
+ {"utf_7_encode", utf_7_encode, METH_VARARGS},
+ {"utf_7_decode", utf_7_decode, METH_VARARGS},
+ {"utf_16_encode", utf_16_encode, METH_VARARGS},
+ {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS},
+ {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS},
+ {"utf_16_decode", utf_16_decode, METH_VARARGS},
+ {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS},
+ {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS},
+ {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS},
+ {"utf_32_encode", utf_32_encode, METH_VARARGS},
+ {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS},
+ {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS},
+ {"utf_32_decode", utf_32_decode, METH_VARARGS},
+ {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS},
+ {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS},
+ {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS},
+ {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS},
+ {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS},
+ {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS},
+ {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS},
+ {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS},
+ {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS},
+ {"latin_1_encode", latin_1_encode, METH_VARARGS},
+ {"latin_1_decode", latin_1_decode, METH_VARARGS},
+ {"ascii_encode", ascii_encode, METH_VARARGS},
+ {"ascii_decode", ascii_decode, METH_VARARGS},
+ {"charmap_encode", charmap_encode, METH_VARARGS},
+ {"charmap_decode", charmap_decode, METH_VARARGS},
+ {"charmap_build", charmap_build, METH_VARARGS},
+ {"readbuffer_encode", readbuffer_encode, METH_VARARGS},
+ {"charbuffer_encode", charbuffer_encode, METH_VARARGS},
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
- {"mbcs_encode", mbcs_encode, METH_VARARGS},
- {"mbcs_decode", mbcs_decode, METH_VARARGS},
+ {"mbcs_encode", mbcs_encode, METH_VARARGS},
+ {"mbcs_decode", mbcs_decode, METH_VARARGS},
#endif
- {"register_error", register_error, METH_VARARGS,
+ {"register_error", register_error, METH_VARARGS,
register_error__doc__},
- {"lookup_error", lookup_error, METH_VARARGS,
+ {"lookup_error", lookup_error, METH_VARARGS,
lookup_error__doc__},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef codecsmodule = {
- PyModuleDef_HEAD_INIT,
- "_codecs",
- NULL,
- -1,
- _codecs_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_codecs",
+ NULL,
+ -1,
+ _codecs_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__codecs(void)
{
- return PyModule_Create(&codecsmodule);
+ return PyModule_Create(&codecsmodule);
}
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index d20c839..0c77bc8 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -46,9 +46,9 @@
*/
typedef struct BLOCK {
- struct BLOCK *leftlink;
- struct BLOCK *rightlink;
- PyObject *data[BLOCKLEN];
+ struct BLOCK *leftlink;
+ struct BLOCK *rightlink;
+ PyObject *data[BLOCKLEN];
} block;
#define MAXFREEBLOCKS 10
@@ -57,71 +57,71 @@ static block *freeblocks[MAXFREEBLOCKS];
static block *
newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
- block *b;
- /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
- * refuse to allocate new blocks if the current len is dangerously
- * close. There is some extra margin to prevent spurious arithmetic
- * overflows at various places. The following check ensures that
- * the blocks allocated to the deque, in the worst case, can only
- * have PY_SSIZE_T_MAX-2 entries in total.
- */
- if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
- PyErr_SetString(PyExc_OverflowError,
- "cannot add more blocks to the deque");
- return NULL;
- }
- if (numfreeblocks) {
- numfreeblocks -= 1;
- b = freeblocks[numfreeblocks];
- } else {
- b = PyMem_Malloc(sizeof(block));
- if (b == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- }
- b->leftlink = leftlink;
- b->rightlink = rightlink;
- return b;
+ block *b;
+ /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
+ * refuse to allocate new blocks if the current len is dangerously
+ * close. There is some extra margin to prevent spurious arithmetic
+ * overflows at various places. The following check ensures that
+ * the blocks allocated to the deque, in the worst case, can only
+ * have PY_SSIZE_T_MAX-2 entries in total.
+ */
+ if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "cannot add more blocks to the deque");
+ return NULL;
+ }
+ if (numfreeblocks) {
+ numfreeblocks -= 1;
+ b = freeblocks[numfreeblocks];
+ } else {
+ b = PyMem_Malloc(sizeof(block));
+ if (b == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ }
+ b->leftlink = leftlink;
+ b->rightlink = rightlink;
+ return b;
}
static void
freeblock(block *b)
{
- if (numfreeblocks < MAXFREEBLOCKS) {
- freeblocks[numfreeblocks] = b;
- numfreeblocks++;
- } else {
- PyMem_Free(b);
- }
+ if (numfreeblocks < MAXFREEBLOCKS) {
+ freeblocks[numfreeblocks] = b;
+ numfreeblocks++;
+ } else {
+ PyMem_Free(b);
+ }
}
typedef struct {
- PyObject_HEAD
- block *leftblock;
- block *rightblock;
- Py_ssize_t leftindex; /* in range(BLOCKLEN) */
- Py_ssize_t rightindex; /* in range(BLOCKLEN) */
- Py_ssize_t len;
- Py_ssize_t maxlen;
- long state; /* incremented whenever the indices move */
- PyObject *weakreflist; /* List of weak references */
+ PyObject_HEAD
+ block *leftblock;
+ block *rightblock;
+ Py_ssize_t leftindex; /* in range(BLOCKLEN) */
+ Py_ssize_t rightindex; /* in range(BLOCKLEN) */
+ Py_ssize_t len;
+ Py_ssize_t maxlen;
+ long state; /* incremented whenever the indices move */
+ PyObject *weakreflist; /* List of weak references */
} dequeobject;
/* The deque's size limit is d.maxlen. The limit can be zero or positive.
* If there is no limit, then d.maxlen == -1.
- *
+ *
* After an item is added to a deque, we check to see if the size has grown past
* the limit. If it has, we get the size back down to the limit by popping an
* item off of the opposite end. The methods that can trigger this are append(),
* appendleft(), extend(), and extendleft().
*/
-#define TRIM(d, popfunction) \
- if (d->maxlen != -1 && d->len > d->maxlen) { \
- PyObject *rv = popfunction(d, NULL); \
- assert(rv != NULL && d->len <= d->maxlen); \
- Py_DECREF(rv); \
+#define TRIM(d, popfunction) \
+ if (d->maxlen != -1 && d->len > d->maxlen) { \
+ PyObject *rv = popfunction(d, NULL); \
+ assert(rv != NULL && d->len <= d->maxlen); \
+ Py_DECREF(rv); \
}
static PyTypeObject deque_type;
@@ -129,65 +129,65 @@ static PyTypeObject deque_type;
static PyObject *
deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- dequeobject *deque;
- block *b;
-
- /* create dequeobject structure */
- deque = (dequeobject *)type->tp_alloc(type, 0);
- if (deque == NULL)
- return NULL;
-
- b = newblock(NULL, NULL, 0);
- if (b == NULL) {
- Py_DECREF(deque);
- return NULL;
- }
-
- assert(BLOCKLEN >= 2);
- deque->leftblock = b;
- deque->rightblock = b;
- deque->leftindex = CENTER + 1;
- deque->rightindex = CENTER;
- deque->len = 0;
- deque->state = 0;
- deque->weakreflist = NULL;
- deque->maxlen = -1;
-
- return (PyObject *)deque;
+ dequeobject *deque;
+ block *b;
+
+ /* create dequeobject structure */
+ deque = (dequeobject *)type->tp_alloc(type, 0);
+ if (deque == NULL)
+ return NULL;
+
+ b = newblock(NULL, NULL, 0);
+ if (b == NULL) {
+ Py_DECREF(deque);
+ return NULL;
+ }
+
+ assert(BLOCKLEN >= 2);
+ deque->leftblock = b;
+ deque->rightblock = b;
+ deque->leftindex = CENTER + 1;
+ deque->rightindex = CENTER;
+ deque->len = 0;
+ deque->state = 0;
+ deque->weakreflist = NULL;
+ deque->maxlen = -1;
+
+ return (PyObject *)deque;
}
static PyObject *
deque_pop(dequeobject *deque, PyObject *unused)
{
- PyObject *item;
- block *prevblock;
-
- if (deque->len == 0) {
- PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
- return NULL;
- }
- item = deque->rightblock->data[deque->rightindex];
- deque->rightindex--;
- deque->len--;
- deque->state++;
-
- if (deque->rightindex == -1) {
- if (deque->len == 0) {
- assert(deque->leftblock == deque->rightblock);
- assert(deque->leftindex == deque->rightindex+1);
- /* re-center instead of freeing a block */
- deque->leftindex = CENTER + 1;
- deque->rightindex = CENTER;
- } else {
- prevblock = deque->rightblock->leftlink;
- assert(deque->leftblock != deque->rightblock);
- freeblock(deque->rightblock);
- prevblock->rightlink = NULL;
- deque->rightblock = prevblock;
- deque->rightindex = BLOCKLEN - 1;
- }
- }
- return item;
+ PyObject *item;
+ block *prevblock;
+
+ if (deque->len == 0) {
+ PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
+ return NULL;
+ }
+ item = deque->rightblock->data[deque->rightindex];
+ deque->rightindex--;
+ deque->len--;
+ deque->state++;
+
+ if (deque->rightindex == -1) {
+ if (deque->len == 0) {
+ assert(deque->leftblock == deque->rightblock);
+ assert(deque->leftindex == deque->rightindex+1);
+ /* re-center instead of freeing a block */
+ deque->leftindex = CENTER + 1;
+ deque->rightindex = CENTER;
+ } else {
+ prevblock = deque->rightblock->leftlink;
+ assert(deque->leftblock != deque->rightblock);
+ freeblock(deque->rightblock);
+ prevblock->rightlink = NULL;
+ deque->rightblock = prevblock;
+ deque->rightindex = BLOCKLEN - 1;
+ }
+ }
+ return item;
}
PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
@@ -195,37 +195,37 @@ PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
static PyObject *
deque_popleft(dequeobject *deque, PyObject *unused)
{
- PyObject *item;
- block *prevblock;
-
- if (deque->len == 0) {
- PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
- return NULL;
- }
- assert(deque->leftblock != NULL);
- item = deque->leftblock->data[deque->leftindex];
- deque->leftindex++;
- deque->len--;
- deque->state++;
-
- if (deque->leftindex == BLOCKLEN) {
- if (deque->len == 0) {
- assert(deque->leftblock == deque->rightblock);
- assert(deque->leftindex == deque->rightindex+1);
- /* re-center instead of freeing a block */
- deque->leftindex = CENTER + 1;
- deque->rightindex = CENTER;
- } else {
- assert(deque->leftblock != deque->rightblock);
- prevblock = deque->leftblock->rightlink;
- freeblock(deque->leftblock);
- assert(prevblock != NULL);
- prevblock->leftlink = NULL;
- deque->leftblock = prevblock;
- deque->leftindex = 0;
- }
- }
- return item;
+ PyObject *item;
+ block *prevblock;
+
+ if (deque->len == 0) {
+ PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
+ return NULL;
+ }
+ assert(deque->leftblock != NULL);
+ item = deque->leftblock->data[deque->leftindex];
+ deque->leftindex++;
+ deque->len--;
+ deque->state++;
+
+ if (deque->leftindex == BLOCKLEN) {
+ if (deque->len == 0) {
+ assert(deque->leftblock == deque->rightblock);
+ assert(deque->leftindex == deque->rightindex+1);
+ /* re-center instead of freeing a block */
+ deque->leftindex = CENTER + 1;
+ deque->rightindex = CENTER;
+ } else {
+ assert(deque->leftblock != deque->rightblock);
+ prevblock = deque->leftblock->rightlink;
+ freeblock(deque->leftblock);
+ assert(prevblock != NULL);
+ prevblock->leftlink = NULL;
+ deque->leftblock = prevblock;
+ deque->leftindex = 0;
+ }
+ }
+ return item;
}
PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
@@ -233,22 +233,22 @@ PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
static PyObject *
deque_append(dequeobject *deque, PyObject *item)
{
- deque->state++;
- if (deque->rightindex == BLOCKLEN-1) {
- block *b = newblock(deque->rightblock, NULL, deque->len);
- if (b == NULL)
- return NULL;
- assert(deque->rightblock->rightlink == NULL);
- deque->rightblock->rightlink = b;
- deque->rightblock = b;
- deque->rightindex = -1;
- }
- Py_INCREF(item);
- deque->len++;
- deque->rightindex++;
- deque->rightblock->data[deque->rightindex] = item;
- TRIM(deque, deque_popleft);
- Py_RETURN_NONE;
+ deque->state++;
+ if (deque->rightindex == BLOCKLEN-1) {
+ block *b = newblock(deque->rightblock, NULL, deque->len);
+ if (b == NULL)
+ return NULL;
+ assert(deque->rightblock->rightlink == NULL);
+ deque->rightblock->rightlink = b;
+ deque->rightblock = b;
+ deque->rightindex = -1;
+ }
+ Py_INCREF(item);
+ deque->len++;
+ deque->rightindex++;
+ deque->rightblock->data[deque->rightindex] = item;
+ TRIM(deque, deque_popleft);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
@@ -256,90 +256,90 @@ PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
static PyObject *
deque_appendleft(dequeobject *deque, PyObject *item)
{
- deque->state++;
- if (deque->leftindex == 0) {
- block *b = newblock(NULL, deque->leftblock, deque->len);
- if (b == NULL)
- return NULL;
- assert(deque->leftblock->leftlink == NULL);
- deque->leftblock->leftlink = b;
- deque->leftblock = b;
- deque->leftindex = BLOCKLEN;
- }
- Py_INCREF(item);
- deque->len++;
- deque->leftindex--;
- deque->leftblock->data[deque->leftindex] = item;
- TRIM(deque, deque_pop);
- Py_RETURN_NONE;
+ deque->state++;
+ if (deque->leftindex == 0) {
+ block *b = newblock(NULL, deque->leftblock, deque->len);
+ if (b == NULL)
+ return NULL;
+ assert(deque->leftblock->leftlink == NULL);
+ deque->leftblock->leftlink = b;
+ deque->leftblock = b;
+ deque->leftindex = BLOCKLEN;
+ }
+ Py_INCREF(item);
+ deque->len++;
+ deque->leftindex--;
+ deque->leftblock->data[deque->leftindex] = item;
+ TRIM(deque, deque_pop);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
-/* Run an iterator to exhaustion. Shortcut for
+/* Run an iterator to exhaustion. Shortcut for
the extend/extendleft methods when maxlen == 0. */
static PyObject*
consume_iterator(PyObject *it)
{
- PyObject *item;
-
- while ((item = PyIter_Next(it)) != NULL) {
- Py_DECREF(item);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return NULL;
- Py_RETURN_NONE;
+ PyObject *item;
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ Py_DECREF(item);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
}
static PyObject *
deque_extend(dequeobject *deque, PyObject *iterable)
{
- PyObject *it, *item;
-
- /* Handle case where id(deque) == id(iterable) */
- if ((PyObject *)deque == iterable) {
- PyObject *result;
- PyObject *s = PySequence_List(iterable);
- if (s == NULL)
- return NULL;
- result = deque_extend(deque, s);
- Py_DECREF(s);
- return result;
- }
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- if (deque->maxlen == 0)
- return consume_iterator(it);
-
- while ((item = PyIter_Next(it)) != NULL) {
- deque->state++;
- if (deque->rightindex == BLOCKLEN-1) {
- block *b = newblock(deque->rightblock, NULL,
- deque->len);
- if (b == NULL) {
- Py_DECREF(item);
- Py_DECREF(it);
- return NULL;
- }
- assert(deque->rightblock->rightlink == NULL);
- deque->rightblock->rightlink = b;
- deque->rightblock = b;
- deque->rightindex = -1;
- }
- deque->len++;
- deque->rightindex++;
- deque->rightblock->data[deque->rightindex] = item;
- TRIM(deque, deque_popleft);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return NULL;
- Py_RETURN_NONE;
+ PyObject *it, *item;
+
+ /* Handle case where id(deque) == id(iterable) */
+ if ((PyObject *)deque == iterable) {
+ PyObject *result;
+ PyObject *s = PySequence_List(iterable);
+ if (s == NULL)
+ return NULL;
+ result = deque_extend(deque, s);
+ Py_DECREF(s);
+ return result;
+ }
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ if (deque->maxlen == 0)
+ return consume_iterator(it);
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ deque->state++;
+ if (deque->rightindex == BLOCKLEN-1) {
+ block *b = newblock(deque->rightblock, NULL,
+ deque->len);
+ if (b == NULL) {
+ Py_DECREF(item);
+ Py_DECREF(it);
+ return NULL;
+ }
+ assert(deque->rightblock->rightlink == NULL);
+ deque->rightblock->rightlink = b;
+ deque->rightblock = b;
+ deque->rightindex = -1;
+ }
+ deque->len++;
+ deque->rightindex++;
+ deque->rightblock->data[deque->rightindex] = item;
+ TRIM(deque, deque_popleft);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(extend_doc,
@@ -348,50 +348,50 @@ PyDoc_STRVAR(extend_doc,
static PyObject *
deque_extendleft(dequeobject *deque, PyObject *iterable)
{
- PyObject *it, *item;
-
- /* Handle case where id(deque) == id(iterable) */
- if ((PyObject *)deque == iterable) {
- PyObject *result;
- PyObject *s = PySequence_List(iterable);
- if (s == NULL)
- return NULL;
- result = deque_extendleft(deque, s);
- Py_DECREF(s);
- return result;
- }
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- if (deque->maxlen == 0)
- return consume_iterator(it);
-
- while ((item = PyIter_Next(it)) != NULL) {
- deque->state++;
- if (deque->leftindex == 0) {
- block *b = newblock(NULL, deque->leftblock,
- deque->len);
- if (b == NULL) {
- Py_DECREF(item);
- Py_DECREF(it);
- return NULL;
- }
- assert(deque->leftblock->leftlink == NULL);
- deque->leftblock->leftlink = b;
- deque->leftblock = b;
- deque->leftindex = BLOCKLEN;
- }
- deque->len++;
- deque->leftindex--;
- deque->leftblock->data[deque->leftindex] = item;
- TRIM(deque, deque_pop);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return NULL;
- Py_RETURN_NONE;
+ PyObject *it, *item;
+
+ /* Handle case where id(deque) == id(iterable) */
+ if ((PyObject *)deque == iterable) {
+ PyObject *result;
+ PyObject *s = PySequence_List(iterable);
+ if (s == NULL)
+ return NULL;
+ result = deque_extendleft(deque, s);
+ Py_DECREF(s);
+ return result;
+ }
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ if (deque->maxlen == 0)
+ return consume_iterator(it);
+
+ while ((item = PyIter_Next(it)) != NULL) {
+ deque->state++;
+ if (deque->leftindex == 0) {
+ block *b = newblock(NULL, deque->leftblock,
+ deque->len);
+ if (b == NULL) {
+ Py_DECREF(item);
+ Py_DECREF(it);
+ return NULL;
+ }
+ assert(deque->leftblock->leftlink == NULL);
+ deque->leftblock->leftlink = b;
+ deque->leftblock = b;
+ deque->leftindex = BLOCKLEN;
+ }
+ deque->len++;
+ deque->leftindex--;
+ deque->leftblock->data[deque->leftindex] = item;
+ TRIM(deque, deque_pop);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(extendleft_doc,
@@ -400,63 +400,63 @@ PyDoc_STRVAR(extendleft_doc,
static PyObject *
deque_inplace_concat(dequeobject *deque, PyObject *other)
{
- PyObject *result;
-
- result = deque_extend(deque, other);
- if (result == NULL)
- return result;
- Py_DECREF(result);
- Py_INCREF(deque);
- return (PyObject *)deque;
+ PyObject *result;
+
+ result = deque_extend(deque, other);
+ if (result == NULL)
+ return result;
+ Py_DECREF(result);
+ Py_INCREF(deque);
+ return (PyObject *)deque;
}
static int
_deque_rotate(dequeobject *deque, Py_ssize_t n)
{
- Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
- PyObject *item, *rv;
-
- if (len == 0)
- return 0;
- if (n > halflen || n < -halflen) {
- n %= len;
- if (n > halflen)
- n -= len;
- else if (n < -halflen)
- n += len;
- }
-
- for (i=0 ; i<n ; i++) {
- item = deque_pop(deque, NULL);
- assert (item != NULL);
- rv = deque_appendleft(deque, item);
- Py_DECREF(item);
- if (rv == NULL)
- return -1;
- Py_DECREF(rv);
- }
- for (i=0 ; i>n ; i--) {
- item = deque_popleft(deque, NULL);
- assert (item != NULL);
- rv = deque_append(deque, item);
- Py_DECREF(item);
- if (rv == NULL)
- return -1;
- Py_DECREF(rv);
- }
- return 0;
+ Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
+ PyObject *item, *rv;
+
+ if (len == 0)
+ return 0;
+ if (n > halflen || n < -halflen) {
+ n %= len;
+ if (n > halflen)
+ n -= len;
+ else if (n < -halflen)
+ n += len;
+ }
+
+ for (i=0 ; i<n ; i++) {
+ item = deque_pop(deque, NULL);
+ assert (item != NULL);
+ rv = deque_appendleft(deque, item);
+ Py_DECREF(item);
+ if (rv == NULL)
+ return -1;
+ Py_DECREF(rv);
+ }
+ for (i=0 ; i>n ; i--) {
+ item = deque_popleft(deque, NULL);
+ assert (item != NULL);
+ rv = deque_append(deque, item);
+ Py_DECREF(item);
+ if (rv == NULL)
+ return -1;
+ Py_DECREF(rv);
+ }
+ return 0;
}
static PyObject *
deque_rotate(dequeobject *deque, PyObject *args)
{
- Py_ssize_t n=1;
+ Py_ssize_t n=1;
- if (!PyArg_ParseTuple(args, "|n:rotate", &n))
- return NULL;
- if (_deque_rotate(deque, n) == 0)
- Py_RETURN_NONE;
- return NULL;
+ if (!PyArg_ParseTuple(args, "|n:rotate", &n))
+ return NULL;
+ if (_deque_rotate(deque, n) == 0)
+ Py_RETURN_NONE;
+ return NULL;
}
PyDoc_STRVAR(rotate_doc,
@@ -465,39 +465,39 @@ PyDoc_STRVAR(rotate_doc,
static Py_ssize_t
deque_len(dequeobject *deque)
{
- return deque->len;
+ return deque->len;
}
static PyObject *
deque_remove(dequeobject *deque, PyObject *value)
{
- Py_ssize_t i, n=deque->len;
-
- for (i=0 ; i<n ; i++) {
- PyObject *item = deque->leftblock->data[deque->leftindex];
- int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
-
- if (deque->len != n) {
- PyErr_SetString(PyExc_IndexError,
- "deque mutated during remove().");
- return NULL;
- }
- if (cmp > 0) {
- PyObject *tgt = deque_popleft(deque, NULL);
- assert (tgt != NULL);
- Py_DECREF(tgt);
- if (_deque_rotate(deque, i) == -1)
- return NULL;
- Py_RETURN_NONE;
- }
- else if (cmp < 0) {
- _deque_rotate(deque, i);
- return NULL;
- }
- _deque_rotate(deque, -1);
- }
- PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
- return NULL;
+ Py_ssize_t i, n=deque->len;
+
+ for (i=0 ; i<n ; i++) {
+ PyObject *item = deque->leftblock->data[deque->leftindex];
+ int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
+
+ if (deque->len != n) {
+ PyErr_SetString(PyExc_IndexError,
+ "deque mutated during remove().");
+ return NULL;
+ }
+ if (cmp > 0) {
+ PyObject *tgt = deque_popleft(deque, NULL);
+ assert (tgt != NULL);
+ Py_DECREF(tgt);
+ if (_deque_rotate(deque, i) == -1)
+ return NULL;
+ Py_RETURN_NONE;
+ }
+ else if (cmp < 0) {
+ _deque_rotate(deque, i);
+ return NULL;
+ }
+ _deque_rotate(deque, -1);
+ }
+ PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
+ return NULL;
}
PyDoc_STRVAR(remove_doc,
@@ -506,56 +506,56 @@ PyDoc_STRVAR(remove_doc,
static int
deque_clear(dequeobject *deque)
{
- PyObject *item;
-
- while (deque->len) {
- item = deque_pop(deque, NULL);
- assert (item != NULL);
- Py_DECREF(item);
- }
- assert(deque->leftblock == deque->rightblock &&
- deque->leftindex - 1 == deque->rightindex &&
- deque->len == 0);
- return 0;
+ PyObject *item;
+
+ while (deque->len) {
+ item = deque_pop(deque, NULL);
+ assert (item != NULL);
+ Py_DECREF(item);
+ }
+ assert(deque->leftblock == deque->rightblock &&
+ deque->leftindex - 1 == deque->rightindex &&
+ deque->len == 0);
+ return 0;
}
static PyObject *
deque_item(dequeobject *deque, Py_ssize_t i)
{
- block *b;
- PyObject *item;
- Py_ssize_t n, index=i;
-
- if (i < 0 || i >= deque->len) {
- PyErr_SetString(PyExc_IndexError,
- "deque index out of range");
- return NULL;
- }
-
- if (i == 0) {
- i = deque->leftindex;
- b = deque->leftblock;
- } else if (i == deque->len - 1) {
- i = deque->rightindex;
- b = deque->rightblock;
- } else {
- i += deque->leftindex;
- n = i / BLOCKLEN;
- i %= BLOCKLEN;
- if (index < (deque->len >> 1)) {
- b = deque->leftblock;
- while (n--)
- b = b->rightlink;
- } else {
- n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
- b = deque->rightblock;
- while (n--)
- b = b->leftlink;
- }
- }
- item = b->data[i];
- Py_INCREF(item);
- return item;
+ block *b;
+ PyObject *item;
+ Py_ssize_t n, index=i;
+
+ if (i < 0 || i >= deque->len) {
+ PyErr_SetString(PyExc_IndexError,
+ "deque index out of range");
+ return NULL;
+ }
+
+ if (i == 0) {
+ i = deque->leftindex;
+ b = deque->leftblock;
+ } else if (i == deque->len - 1) {
+ i = deque->rightindex;
+ b = deque->rightblock;
+ } else {
+ i += deque->leftindex;
+ n = i / BLOCKLEN;
+ i %= BLOCKLEN;
+ if (index < (deque->len >> 1)) {
+ b = deque->leftblock;
+ while (n--)
+ b = b->rightlink;
+ } else {
+ n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
+ b = deque->rightblock;
+ while (n--)
+ b = b->leftlink;
+ }
+ }
+ item = b->data[i];
+ Py_INCREF(item);
+ return item;
}
/* delitem() implemented in terms of rotate for simplicity and reasonable
@@ -568,62 +568,62 @@ deque_item(dequeobject *deque, Py_ssize_t i)
static int
deque_del_item(dequeobject *deque, Py_ssize_t i)
{
- PyObject *item;
+ PyObject *item;
- assert (i >= 0 && i < deque->len);
- if (_deque_rotate(deque, -i) == -1)
- return -1;
+ assert (i >= 0 && i < deque->len);
+ if (_deque_rotate(deque, -i) == -1)
+ return -1;
- item = deque_popleft(deque, NULL);
- assert (item != NULL);
- Py_DECREF(item);
+ item = deque_popleft(deque, NULL);
+ assert (item != NULL);
+ Py_DECREF(item);
- return _deque_rotate(deque, i);
+ return _deque_rotate(deque, i);
}
static int
deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
{
- PyObject *old_value;
- block *b;
- Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
-
- if (i < 0 || i >= len) {
- PyErr_SetString(PyExc_IndexError,
- "deque index out of range");
- return -1;
- }
- if (v == NULL)
- return deque_del_item(deque, i);
-
- i += deque->leftindex;
- n = i / BLOCKLEN;
- i %= BLOCKLEN;
- if (index <= halflen) {
- b = deque->leftblock;
- while (n--)
- b = b->rightlink;
- } else {
- n = (deque->leftindex + len - 1) / BLOCKLEN - n;
- b = deque->rightblock;
- while (n--)
- b = b->leftlink;
- }
- Py_INCREF(v);
- old_value = b->data[i];
- b->data[i] = v;
- Py_DECREF(old_value);
- return 0;
+ PyObject *old_value;
+ block *b;
+ Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
+
+ if (i < 0 || i >= len) {
+ PyErr_SetString(PyExc_IndexError,
+ "deque index out of range");
+ return -1;
+ }
+ if (v == NULL)
+ return deque_del_item(deque, i);
+
+ i += deque->leftindex;
+ n = i / BLOCKLEN;
+ i %= BLOCKLEN;
+ if (index <= halflen) {
+ b = deque->leftblock;
+ while (n--)
+ b = b->rightlink;
+ } else {
+ n = (deque->leftindex + len - 1) / BLOCKLEN - n;
+ b = deque->rightblock;
+ while (n--)
+ b = b->leftlink;
+ }
+ Py_INCREF(v);
+ old_value = b->data[i];
+ b->data[i] = v;
+ Py_DECREF(old_value);
+ return 0;
}
static PyObject *
deque_clearmethod(dequeobject *deque)
{
- int rv;
+ int rv;
- rv = deque_clear(deque);
- assert (rv != -1);
- Py_RETURN_NONE;
+ rv = deque_clear(deque);
+ assert (rv != -1);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
@@ -631,49 +631,49 @@ PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
static void
deque_dealloc(dequeobject *deque)
{
- PyObject_GC_UnTrack(deque);
- if (deque->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) deque);
- if (deque->leftblock != NULL) {
- deque_clear(deque);
- assert(deque->leftblock != NULL);
- freeblock(deque->leftblock);
- }
- deque->leftblock = NULL;
- deque->rightblock = NULL;
- Py_TYPE(deque)->tp_free(deque);
+ PyObject_GC_UnTrack(deque);
+ if (deque->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) deque);
+ if (deque->leftblock != NULL) {
+ deque_clear(deque);
+ assert(deque->leftblock != NULL);
+ freeblock(deque->leftblock);
+ }
+ deque->leftblock = NULL;
+ deque->rightblock = NULL;
+ Py_TYPE(deque)->tp_free(deque);
}
static int
deque_traverse(dequeobject *deque, visitproc visit, void *arg)
{
- block *b;
- PyObject *item;
- Py_ssize_t index;
- Py_ssize_t indexlo = deque->leftindex;
-
- for (b = deque->leftblock; b != NULL; b = b->rightlink) {
- const Py_ssize_t indexhi = b == deque->rightblock ?
- deque->rightindex :
- BLOCKLEN - 1;
-
- for (index = indexlo; index <= indexhi; ++index) {
- item = b->data[index];
- Py_VISIT(item);
- }
- indexlo = 0;
- }
- return 0;
+ block *b;
+ PyObject *item;
+ Py_ssize_t index;
+ Py_ssize_t indexlo = deque->leftindex;
+
+ for (b = deque->leftblock; b != NULL; b = b->rightlink) {
+ const Py_ssize_t indexhi = b == deque->rightblock ?
+ deque->rightindex :
+ BLOCKLEN - 1;
+
+ for (index = indexlo; index <= indexhi; ++index) {
+ item = b->data[index];
+ Py_VISIT(item);
+ }
+ indexlo = 0;
+ }
+ return 0;
}
static PyObject *
deque_copy(PyObject *deque)
{
- if (((dequeobject *)deque)->maxlen == -1)
- return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
- else
- return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
- deque, ((dequeobject *)deque)->maxlen, NULL);
+ if (((dequeobject *)deque)->maxlen == -1)
+ return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
+ else
+ return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
+ deque, ((dequeobject *)deque)->maxlen, NULL);
}
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
@@ -681,30 +681,30 @@ PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
static PyObject *
deque_reduce(dequeobject *deque)
{
- PyObject *dict, *result, *aslist;
-
- dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
- if (dict == NULL)
- PyErr_Clear();
- aslist = PySequence_List((PyObject *)deque);
- if (aslist == NULL) {
- Py_XDECREF(dict);
- return NULL;
- }
- if (dict == NULL) {
- if (deque->maxlen == -1)
- result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
- else
- result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
- } else {
- if (deque->maxlen == -1)
- result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
- else
- result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
- }
- Py_XDECREF(dict);
- Py_DECREF(aslist);
- return result;
+ PyObject *dict, *result, *aslist;
+
+ dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
+ if (dict == NULL)
+ PyErr_Clear();
+ aslist = PySequence_List((PyObject *)deque);
+ if (aslist == NULL) {
+ Py_XDECREF(dict);
+ return NULL;
+ }
+ if (dict == NULL) {
+ if (deque->maxlen == -1)
+ result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
+ else
+ result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
+ } else {
+ if (deque->maxlen == -1)
+ result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
+ else
+ result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
+ }
+ Py_XDECREF(dict);
+ Py_DECREF(aslist);
+ return result;
}
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
@@ -712,166 +712,166 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyObject *
deque_repr(PyObject *deque)
{
- PyObject *aslist, *result;
- int i;
-
- i = Py_ReprEnter(deque);
- if (i != 0) {
- if (i < 0)
- return NULL;
- return PyUnicode_FromString("[...]");
- }
-
- aslist = PySequence_List(deque);
- if (aslist == NULL) {
- Py_ReprLeave(deque);
- return NULL;
- }
- if (((dequeobject *)deque)->maxlen != -1)
-
- result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
- aslist, ((dequeobject *)deque)->maxlen);
- else
- result = PyUnicode_FromFormat("deque(%R)", aslist);
- Py_DECREF(aslist);
- Py_ReprLeave(deque);
- return result;
+ PyObject *aslist, *result;
+ int i;
+
+ i = Py_ReprEnter(deque);
+ if (i != 0) {
+ if (i < 0)
+ return NULL;
+ return PyUnicode_FromString("[...]");
+ }
+
+ aslist = PySequence_List(deque);
+ if (aslist == NULL) {
+ Py_ReprLeave(deque);
+ return NULL;
+ }
+ if (((dequeobject *)deque)->maxlen != -1)
+
+ result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
+ aslist, ((dequeobject *)deque)->maxlen);
+ else
+ result = PyUnicode_FromFormat("deque(%R)", aslist);
+ Py_DECREF(aslist);
+ Py_ReprLeave(deque);
+ return result;
}
static PyObject *
deque_richcompare(PyObject *v, PyObject *w, int op)
{
- PyObject *it1=NULL, *it2=NULL, *x, *y;
- Py_ssize_t vs, ws;
- int b, cmp=-1;
-
- if (!PyObject_TypeCheck(v, &deque_type) ||
- !PyObject_TypeCheck(w, &deque_type)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- /* Shortcuts */
- vs = ((dequeobject *)v)->len;
- ws = ((dequeobject *)w)->len;
- if (op == Py_EQ) {
- if (v == w)
- Py_RETURN_TRUE;
- if (vs != ws)
- Py_RETURN_FALSE;
- }
- if (op == Py_NE) {
- if (v == w)
- Py_RETURN_FALSE;
- if (vs != ws)
- Py_RETURN_TRUE;
- }
-
- /* Search for the first index where items are different */
- it1 = PyObject_GetIter(v);
- if (it1 == NULL)
- goto done;
- it2 = PyObject_GetIter(w);
- if (it2 == NULL)
- goto done;
- for (;;) {
- x = PyIter_Next(it1);
- if (x == NULL && PyErr_Occurred())
- goto done;
- y = PyIter_Next(it2);
- if (x == NULL || y == NULL)
- break;
- b = PyObject_RichCompareBool(x, y, Py_EQ);
- if (b == 0) {
- cmp = PyObject_RichCompareBool(x, y, op);
- Py_DECREF(x);
- Py_DECREF(y);
- goto done;
- }
- Py_DECREF(x);
- Py_DECREF(y);
- if (b == -1)
- goto done;
- }
- /* We reached the end of one deque or both */
- Py_XDECREF(x);
- Py_XDECREF(y);
- if (PyErr_Occurred())
- goto done;
- switch (op) {
- case Py_LT: cmp = y != NULL; break; /* if w was longer */
- case Py_LE: cmp = x == NULL; break; /* if v was not longer */
- case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
- case Py_NE: cmp = x != y; break; /* if one deque continues */
- case Py_GT: cmp = x != NULL; break; /* if v was longer */
- case Py_GE: cmp = y == NULL; break; /* if w was not longer */
- }
+ PyObject *it1=NULL, *it2=NULL, *x, *y;
+ Py_ssize_t vs, ws;
+ int b, cmp=-1;
+
+ if (!PyObject_TypeCheck(v, &deque_type) ||
+ !PyObject_TypeCheck(w, &deque_type)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ /* Shortcuts */
+ vs = ((dequeobject *)v)->len;
+ ws = ((dequeobject *)w)->len;
+ if (op == Py_EQ) {
+ if (v == w)
+ Py_RETURN_TRUE;
+ if (vs != ws)
+ Py_RETURN_FALSE;
+ }
+ if (op == Py_NE) {
+ if (v == w)
+ Py_RETURN_FALSE;
+ if (vs != ws)
+ Py_RETURN_TRUE;
+ }
+
+ /* Search for the first index where items are different */
+ it1 = PyObject_GetIter(v);
+ if (it1 == NULL)
+ goto done;
+ it2 = PyObject_GetIter(w);
+ if (it2 == NULL)
+ goto done;
+ for (;;) {
+ x = PyIter_Next(it1);
+ if (x == NULL && PyErr_Occurred())
+ goto done;
+ y = PyIter_Next(it2);
+ if (x == NULL || y == NULL)
+ break;
+ b = PyObject_RichCompareBool(x, y, Py_EQ);
+ if (b == 0) {
+ cmp = PyObject_RichCompareBool(x, y, op);
+ Py_DECREF(x);
+ Py_DECREF(y);
+ goto done;
+ }
+ Py_DECREF(x);
+ Py_DECREF(y);
+ if (b == -1)
+ goto done;
+ }
+ /* We reached the end of one deque or both */
+ Py_XDECREF(x);
+ Py_XDECREF(y);
+ if (PyErr_Occurred())
+ goto done;
+ switch (op) {
+ case Py_LT: cmp = y != NULL; break; /* if w was longer */
+ case Py_LE: cmp = x == NULL; break; /* if v was not longer */
+ case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
+ case Py_NE: cmp = x != y; break; /* if one deque continues */
+ case Py_GT: cmp = x != NULL; break; /* if v was longer */
+ case Py_GE: cmp = y == NULL; break; /* if w was not longer */
+ }
done:
- Py_XDECREF(it1);
- Py_XDECREF(it2);
- if (cmp == 1)
- Py_RETURN_TRUE;
- if (cmp == 0)
- Py_RETURN_FALSE;
- return NULL;
+ Py_XDECREF(it1);
+ Py_XDECREF(it2);
+ if (cmp == 1)
+ Py_RETURN_TRUE;
+ if (cmp == 0)
+ Py_RETURN_FALSE;
+ return NULL;
}
static int
deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
{
- PyObject *iterable = NULL;
- PyObject *maxlenobj = NULL;
- Py_ssize_t maxlen = -1;
- char *kwlist[] = {"iterable", "maxlen", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
- return -1;
- if (maxlenobj != NULL && maxlenobj != Py_None) {
- maxlen = PyLong_AsSsize_t(maxlenobj);
- if (maxlen == -1 && PyErr_Occurred())
- return -1;
- if (maxlen < 0) {
- PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
- return -1;
- }
- }
- deque->maxlen = maxlen;
- deque_clear(deque);
- if (iterable != NULL) {
- PyObject *rv = deque_extend(deque, iterable);
- if (rv == NULL)
- return -1;
- Py_DECREF(rv);
- }
- return 0;
+ PyObject *iterable = NULL;
+ PyObject *maxlenobj = NULL;
+ Py_ssize_t maxlen = -1;
+ char *kwlist[] = {"iterable", "maxlen", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
+ return -1;
+ if (maxlenobj != NULL && maxlenobj != Py_None) {
+ maxlen = PyLong_AsSsize_t(maxlenobj);
+ if (maxlen == -1 && PyErr_Occurred())
+ return -1;
+ if (maxlen < 0) {
+ PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
+ return -1;
+ }
+ }
+ deque->maxlen = maxlen;
+ deque_clear(deque);
+ if (iterable != NULL) {
+ PyObject *rv = deque_extend(deque, iterable);
+ if (rv == NULL)
+ return -1;
+ Py_DECREF(rv);
+ }
+ return 0;
}
static PyObject *
deque_get_maxlen(dequeobject *deque)
{
- if (deque->maxlen == -1)
- Py_RETURN_NONE;
- return PyLong_FromSsize_t(deque->maxlen);
+ if (deque->maxlen == -1)
+ Py_RETURN_NONE;
+ return PyLong_FromSsize_t(deque->maxlen);
}
static PyGetSetDef deque_getset[] = {
- {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
- "maximum size of a deque or None if unbounded"},
- {0}
+ {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
+ "maximum size of a deque or None if unbounded"},
+ {0}
};
static PySequenceMethods deque_as_sequence = {
- (lenfunc)deque_len, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- (ssizeargfunc)deque_item, /* sq_item */
- 0, /* sq_slice */
- (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
- 0, /* sq_ass_slice */
- 0, /* sq_contains */
- (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ (lenfunc)deque_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ (ssizeargfunc)deque_item, /* sq_item */
+ 0, /* sq_slice */
+ (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ 0, /* sq_contains */
+ (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
};
@@ -880,34 +880,34 @@ static PySequenceMethods deque_as_sequence = {
static PyObject *deque_iter(dequeobject *deque);
static PyObject *deque_reviter(dequeobject *deque);
PyDoc_STRVAR(reversed_doc,
- "D.__reversed__() -- return a reverse iterator over the deque");
+ "D.__reversed__() -- return a reverse iterator over the deque");
static PyMethodDef deque_methods[] = {
- {"append", (PyCFunction)deque_append,
- METH_O, append_doc},
- {"appendleft", (PyCFunction)deque_appendleft,
- METH_O, appendleft_doc},
- {"clear", (PyCFunction)deque_clearmethod,
- METH_NOARGS, clear_doc},
- {"__copy__", (PyCFunction)deque_copy,
- METH_NOARGS, copy_doc},
- {"extend", (PyCFunction)deque_extend,
- METH_O, extend_doc},
- {"extendleft", (PyCFunction)deque_extendleft,
- METH_O, extendleft_doc},
- {"pop", (PyCFunction)deque_pop,
- METH_NOARGS, pop_doc},
- {"popleft", (PyCFunction)deque_popleft,
- METH_NOARGS, popleft_doc},
- {"__reduce__", (PyCFunction)deque_reduce,
- METH_NOARGS, reduce_doc},
- {"remove", (PyCFunction)deque_remove,
- METH_O, remove_doc},
- {"__reversed__", (PyCFunction)deque_reviter,
- METH_NOARGS, reversed_doc},
- {"rotate", (PyCFunction)deque_rotate,
- METH_VARARGS, rotate_doc},
- {NULL, NULL} /* sentinel */
+ {"append", (PyCFunction)deque_append,
+ METH_O, append_doc},
+ {"appendleft", (PyCFunction)deque_appendleft,
+ METH_O, appendleft_doc},
+ {"clear", (PyCFunction)deque_clearmethod,
+ METH_NOARGS, clear_doc},
+ {"__copy__", (PyCFunction)deque_copy,
+ METH_NOARGS, copy_doc},
+ {"extend", (PyCFunction)deque_extend,
+ METH_O, extend_doc},
+ {"extendleft", (PyCFunction)deque_extendleft,
+ METH_O, extendleft_doc},
+ {"pop", (PyCFunction)deque_pop,
+ METH_NOARGS, pop_doc},
+ {"popleft", (PyCFunction)deque_popleft,
+ METH_NOARGS, popleft_doc},
+ {"__reduce__", (PyCFunction)deque_reduce,
+ METH_NOARGS, reduce_doc},
+ {"remove", (PyCFunction)deque_remove,
+ METH_O, remove_doc},
+ {"__reversed__", (PyCFunction)deque_reviter,
+ METH_NOARGS, reversed_doc},
+ {"rotate", (PyCFunction)deque_rotate,
+ METH_VARARGS, rotate_doc},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(deque_doc,
@@ -916,58 +916,58 @@ PyDoc_STRVAR(deque_doc,
Build an ordered collection accessible from endpoints only.");
static PyTypeObject deque_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "collections.deque", /* tp_name */
- sizeof(dequeobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)deque_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- deque_repr, /* tp_repr */
- 0, /* tp_as_number */
- &deque_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- /* tp_flags */
- deque_doc, /* tp_doc */
- (traverseproc)deque_traverse, /* tp_traverse */
- (inquiry)deque_clear, /* tp_clear */
- (richcmpfunc)deque_richcompare, /* tp_richcompare */
- offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
- (getiterfunc)deque_iter, /* tp_iter */
- 0, /* tp_iternext */
- deque_methods, /* tp_methods */
- 0, /* tp_members */
- deque_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)deque_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- deque_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "collections.deque", /* tp_name */
+ sizeof(dequeobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)deque_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ deque_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &deque_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ /* tp_flags */
+ deque_doc, /* tp_doc */
+ (traverseproc)deque_traverse, /* tp_traverse */
+ (inquiry)deque_clear, /* tp_clear */
+ (richcmpfunc)deque_richcompare, /* tp_richcompare */
+ offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
+ (getiterfunc)deque_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ deque_methods, /* tp_methods */
+ 0, /* tp_members */
+ deque_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)deque_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ deque_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/*********************** Deque Iterator **************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t index;
- block *b;
- dequeobject *deque;
- long state; /* state when the iterator is created */
- Py_ssize_t counter; /* number of items remaining for iteration */
+ PyObject_HEAD
+ Py_ssize_t index;
+ block *b;
+ dequeobject *deque;
+ long state; /* state when the iterator is created */
+ Py_ssize_t counter; /* number of items remaining for iteration */
} dequeiterobject;
static PyTypeObject dequeiter_type;
@@ -975,107 +975,107 @@ static PyTypeObject dequeiter_type;
static PyObject *
deque_iter(dequeobject *deque)
{
- dequeiterobject *it;
-
- it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
- if (it == NULL)
- return NULL;
- it->b = deque->leftblock;
- it->index = deque->leftindex;
- Py_INCREF(deque);
- it->deque = deque;
- it->state = deque->state;
- it->counter = deque->len;
- PyObject_GC_Track(it);
- return (PyObject *)it;
+ dequeiterobject *it;
+
+ it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
+ if (it == NULL)
+ return NULL;
+ it->b = deque->leftblock;
+ it->index = deque->leftindex;
+ Py_INCREF(deque);
+ it->deque = deque;
+ it->state = deque->state;
+ it->counter = deque->len;
+ PyObject_GC_Track(it);
+ return (PyObject *)it;
}
static int
dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
{
- Py_VISIT(dio->deque);
- return 0;
+ Py_VISIT(dio->deque);
+ return 0;
}
static void
dequeiter_dealloc(dequeiterobject *dio)
{
- Py_XDECREF(dio->deque);
- PyObject_GC_Del(dio);
+ Py_XDECREF(dio->deque);
+ PyObject_GC_Del(dio);
}
static PyObject *
dequeiter_next(dequeiterobject *it)
{
- PyObject *item;
-
- if (it->deque->state != it->state) {
- it->counter = 0;
- PyErr_SetString(PyExc_RuntimeError,
- "deque mutated during iteration");
- return NULL;
- }
- if (it->counter == 0)
- return NULL;
- assert (!(it->b == it->deque->rightblock &&
- it->index > it->deque->rightindex));
-
- item = it->b->data[it->index];
- it->index++;
- it->counter--;
- if (it->index == BLOCKLEN && it->counter > 0) {
- assert (it->b->rightlink != NULL);
- it->b = it->b->rightlink;
- it->index = 0;
- }
- Py_INCREF(item);
- return item;
+ PyObject *item;
+
+ if (it->deque->state != it->state) {
+ it->counter = 0;
+ PyErr_SetString(PyExc_RuntimeError,
+ "deque mutated during iteration");
+ return NULL;
+ }
+ if (it->counter == 0)
+ return NULL;
+ assert (!(it->b == it->deque->rightblock &&
+ it->index > it->deque->rightindex));
+
+ item = it->b->data[it->index];
+ it->index++;
+ it->counter--;
+ if (it->index == BLOCKLEN && it->counter > 0) {
+ assert (it->b->rightlink != NULL);
+ it->b = it->b->rightlink;
+ it->index = 0;
+ }
+ Py_INCREF(item);
+ return item;
}
static PyObject *
dequeiter_len(dequeiterobject *it)
{
- return PyLong_FromLong(it->counter);
+ return PyLong_FromLong(it->counter);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef dequeiter_methods[] = {
- {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject dequeiter_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "deque_iterator", /* tp_name */
- sizeof(dequeiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dequeiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dequeiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dequeiter_next, /* tp_iternext */
- dequeiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "deque_iterator", /* tp_name */
+ sizeof(dequeiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dequeiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dequeiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dequeiter_next, /* tp_iternext */
+ dequeiter_methods, /* tp_methods */
+ 0,
};
/*********************** Deque Reverse Iterator **************************/
@@ -1085,87 +1085,87 @@ static PyTypeObject dequereviter_type;
static PyObject *
deque_reviter(dequeobject *deque)
{
- dequeiterobject *it;
-
- it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
- if (it == NULL)
- return NULL;
- it->b = deque->rightblock;
- it->index = deque->rightindex;
- Py_INCREF(deque);
- it->deque = deque;
- it->state = deque->state;
- it->counter = deque->len;
- PyObject_GC_Track(it);
- return (PyObject *)it;
+ dequeiterobject *it;
+
+ it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
+ if (it == NULL)
+ return NULL;
+ it->b = deque->rightblock;
+ it->index = deque->rightindex;
+ Py_INCREF(deque);
+ it->deque = deque;
+ it->state = deque->state;
+ it->counter = deque->len;
+ PyObject_GC_Track(it);
+ return (PyObject *)it;
}
static PyObject *
dequereviter_next(dequeiterobject *it)
{
- PyObject *item;
- if (it->counter == 0)
- return NULL;
-
- if (it->deque->state != it->state) {
- it->counter = 0;
- PyErr_SetString(PyExc_RuntimeError,
- "deque mutated during iteration");
- return NULL;
- }
- assert (!(it->b == it->deque->leftblock &&
- it->index < it->deque->leftindex));
-
- item = it->b->data[it->index];
- it->index--;
- it->counter--;
- if (it->index == -1 && it->counter > 0) {
- assert (it->b->leftlink != NULL);
- it->b = it->b->leftlink;
- it->index = BLOCKLEN - 1;
- }
- Py_INCREF(item);
- return item;
+ PyObject *item;
+ if (it->counter == 0)
+ return NULL;
+
+ if (it->deque->state != it->state) {
+ it->counter = 0;
+ PyErr_SetString(PyExc_RuntimeError,
+ "deque mutated during iteration");
+ return NULL;
+ }
+ assert (!(it->b == it->deque->leftblock &&
+ it->index < it->deque->leftindex));
+
+ item = it->b->data[it->index];
+ it->index--;
+ it->counter--;
+ if (it->index == -1 && it->counter > 0) {
+ assert (it->b->leftlink != NULL);
+ it->b = it->b->leftlink;
+ it->index = BLOCKLEN - 1;
+ }
+ Py_INCREF(item);
+ return item;
}
static PyTypeObject dequereviter_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "deque_reverse_iterator", /* tp_name */
- sizeof(dequeiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dequeiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dequeiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dequereviter_next, /* tp_iternext */
- dequeiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "deque_reverse_iterator", /* tp_name */
+ sizeof(dequeiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dequeiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dequeiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dequereviter_next, /* tp_iternext */
+ dequeiter_methods, /* tp_methods */
+ 0,
};
/* defaultdict type *********************************************************/
typedef struct {
- PyDictObject dict;
- PyObject *default_factory;
+ PyDictObject dict;
+ PyObject *default_factory;
} defdictobject;
static PyTypeObject defdict_type; /* Forward */
@@ -1180,25 +1180,25 @@ PyDoc_STRVAR(defdict_missing_doc,
static PyObject *
defdict_missing(defdictobject *dd, PyObject *key)
{
- PyObject *factory = dd->default_factory;
- PyObject *value;
- if (factory == NULL || factory == Py_None) {
- /* XXX Call dict.__missing__(key) */
- PyObject *tup;
- tup = PyTuple_Pack(1, key);
- if (!tup) return NULL;
- PyErr_SetObject(PyExc_KeyError, tup);
- Py_DECREF(tup);
- return NULL;
- }
- value = PyEval_CallObject(factory, NULL);
- if (value == NULL)
- return value;
- if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
- Py_DECREF(value);
- return NULL;
- }
- return value;
+ PyObject *factory = dd->default_factory;
+ PyObject *value;
+ if (factory == NULL || factory == Py_None) {
+ /* XXX Call dict.__missing__(key) */
+ PyObject *tup;
+ tup = PyTuple_Pack(1, key);
+ if (!tup) return NULL;
+ PyErr_SetObject(PyExc_KeyError, tup);
+ Py_DECREF(tup);
+ return NULL;
+ }
+ value = PyEval_CallObject(factory, NULL);
+ if (value == NULL)
+ return value;
+ if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
+ Py_DECREF(value);
+ return NULL;
+ }
+ return value;
}
PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
@@ -1206,175 +1206,175 @@ PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
static PyObject *
defdict_copy(defdictobject *dd)
{
- /* This calls the object's class. That only works for subclasses
- whose class constructor has the same signature. Subclasses that
- define a different constructor signature must override copy().
- */
-
- if (dd->default_factory == NULL)
- return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
- return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
- dd->default_factory, dd, NULL);
+ /* This calls the object's class. That only works for subclasses
+ whose class constructor has the same signature. Subclasses that
+ define a different constructor signature must override copy().
+ */
+
+ if (dd->default_factory == NULL)
+ return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
+ return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
+ dd->default_factory, dd, NULL);
}
static PyObject *
defdict_reduce(defdictobject *dd)
{
- /* __reduce__ must return a 5-tuple as follows:
-
- - factory function
- - tuple of args for the factory function
- - additional state (here None)
- - sequence iterator (here None)
- - dictionary iterator (yielding successive (key, value) pairs
-
- This API is used by pickle.py and copy.py.
-
- For this to be useful with pickle.py, the default_factory
- must be picklable; e.g., None, a built-in, or a global
- function in a module or package.
-
- Both shallow and deep copying are supported, but for deep
- copying, the default_factory must be deep-copyable; e.g. None,
- or a built-in (functions are not copyable at this time).
-
- This only works for subclasses as long as their constructor
- signature is compatible; the first argument must be the
- optional default_factory, defaulting to None.
- */
- PyObject *args;
- PyObject *items;
- PyObject *iter;
- PyObject *result;
- if (dd->default_factory == NULL || dd->default_factory == Py_None)
- args = PyTuple_New(0);
- else
- args = PyTuple_Pack(1, dd->default_factory);
- if (args == NULL)
- return NULL;
- items = PyObject_CallMethod((PyObject *)dd, "items", "()");
- if (items == NULL) {
- Py_DECREF(args);
- return NULL;
- }
- iter = PyObject_GetIter(items);
- if (iter == NULL) {
- Py_DECREF(items);
- Py_DECREF(args);
- return NULL;
- }
- result = PyTuple_Pack(5, Py_TYPE(dd), args,
- Py_None, Py_None, iter);
- Py_DECREF(iter);
- Py_DECREF(items);
- Py_DECREF(args);
- return result;
+ /* __reduce__ must return a 5-tuple as follows:
+
+ - factory function
+ - tuple of args for the factory function
+ - additional state (here None)
+ - sequence iterator (here None)
+ - dictionary iterator (yielding successive (key, value) pairs
+
+ This API is used by pickle.py and copy.py.
+
+ For this to be useful with pickle.py, the default_factory
+ must be picklable; e.g., None, a built-in, or a global
+ function in a module or package.
+
+ Both shallow and deep copying are supported, but for deep
+ copying, the default_factory must be deep-copyable; e.g. None,
+ or a built-in (functions are not copyable at this time).
+
+ This only works for subclasses as long as their constructor
+ signature is compatible; the first argument must be the
+ optional default_factory, defaulting to None.
+ */
+ PyObject *args;
+ PyObject *items;
+ PyObject *iter;
+ PyObject *result;
+ if (dd->default_factory == NULL || dd->default_factory == Py_None)
+ args = PyTuple_New(0);
+ else
+ args = PyTuple_Pack(1, dd->default_factory);
+ if (args == NULL)
+ return NULL;
+ items = PyObject_CallMethod((PyObject *)dd, "items", "()");
+ if (items == NULL) {
+ Py_DECREF(args);
+ return NULL;
+ }
+ iter = PyObject_GetIter(items);
+ if (iter == NULL) {
+ Py_DECREF(items);
+ Py_DECREF(args);
+ return NULL;
+ }
+ result = PyTuple_Pack(5, Py_TYPE(dd), args,
+ Py_None, Py_None, iter);
+ Py_DECREF(iter);
+ Py_DECREF(items);
+ Py_DECREF(args);
+ return result;
}
static PyMethodDef defdict_methods[] = {
- {"__missing__", (PyCFunction)defdict_missing, METH_O,
- defdict_missing_doc},
- {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
- defdict_copy_doc},
- {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
- defdict_copy_doc},
- {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
- reduce_doc},
- {NULL}
+ {"__missing__", (PyCFunction)defdict_missing, METH_O,
+ defdict_missing_doc},
+ {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
+ defdict_copy_doc},
+ {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
+ defdict_copy_doc},
+ {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
+ reduce_doc},
+ {NULL}
};
static PyMemberDef defdict_members[] = {
- {"default_factory", T_OBJECT,
- offsetof(defdictobject, default_factory), 0,
- PyDoc_STR("Factory for default value called by __missing__().")},
- {NULL}
+ {"default_factory", T_OBJECT,
+ offsetof(defdictobject, default_factory), 0,
+ PyDoc_STR("Factory for default value called by __missing__().")},
+ {NULL}
};
static void
defdict_dealloc(defdictobject *dd)
{
- Py_CLEAR(dd->default_factory);
- PyDict_Type.tp_dealloc((PyObject *)dd);
+ Py_CLEAR(dd->default_factory);
+ PyDict_Type.tp_dealloc((PyObject *)dd);
}
static PyObject *
defdict_repr(defdictobject *dd)
{
- PyObject *baserepr;
- PyObject *defrepr;
- PyObject *result;
- baserepr = PyDict_Type.tp_repr((PyObject *)dd);
- if (baserepr == NULL)
- return NULL;
- if (dd->default_factory == NULL)
- defrepr = PyUnicode_FromString("None");
- else
- {
- int status = Py_ReprEnter(dd->default_factory);
- if (status != 0) {
- if (status < 0)
- return NULL;
- defrepr = PyUnicode_FromString("...");
- }
- else
- defrepr = PyObject_Repr(dd->default_factory);
- Py_ReprLeave(dd->default_factory);
- }
- if (defrepr == NULL) {
- Py_DECREF(baserepr);
- return NULL;
- }
- result = PyUnicode_FromFormat("defaultdict(%U, %U)",
- defrepr, baserepr);
- Py_DECREF(defrepr);
- Py_DECREF(baserepr);
- return result;
+ PyObject *baserepr;
+ PyObject *defrepr;
+ PyObject *result;
+ baserepr = PyDict_Type.tp_repr((PyObject *)dd);
+ if (baserepr == NULL)
+ return NULL;
+ if (dd->default_factory == NULL)
+ defrepr = PyUnicode_FromString("None");
+ else
+ {
+ int status = Py_ReprEnter(dd->default_factory);
+ if (status != 0) {
+ if (status < 0)
+ return NULL;
+ defrepr = PyUnicode_FromString("...");
+ }
+ else
+ defrepr = PyObject_Repr(dd->default_factory);
+ Py_ReprLeave(dd->default_factory);
+ }
+ if (defrepr == NULL) {
+ Py_DECREF(baserepr);
+ return NULL;
+ }
+ result = PyUnicode_FromFormat("defaultdict(%U, %U)",
+ defrepr, baserepr);
+ Py_DECREF(defrepr);
+ Py_DECREF(baserepr);
+ return result;
}
static int
defdict_traverse(PyObject *self, visitproc visit, void *arg)
{
- Py_VISIT(((defdictobject *)self)->default_factory);
- return PyDict_Type.tp_traverse(self, visit, arg);
+ Py_VISIT(((defdictobject *)self)->default_factory);
+ return PyDict_Type.tp_traverse(self, visit, arg);
}
static int
defdict_tp_clear(defdictobject *dd)
{
- Py_CLEAR(dd->default_factory);
- return PyDict_Type.tp_clear((PyObject *)dd);
+ Py_CLEAR(dd->default_factory);
+ return PyDict_Type.tp_clear((PyObject *)dd);
}
static int
defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- defdictobject *dd = (defdictobject *)self;
- PyObject *olddefault = dd->default_factory;
- PyObject *newdefault = NULL;
- PyObject *newargs;
- int result;
- if (args == NULL || !PyTuple_Check(args))
- newargs = PyTuple_New(0);
- else {
- Py_ssize_t n = PyTuple_GET_SIZE(args);
- if (n > 0) {
- newdefault = PyTuple_GET_ITEM(args, 0);
- if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "first argument must be callable");
- return -1;
- }
- }
- newargs = PySequence_GetSlice(args, 1, n);
- }
- if (newargs == NULL)
- return -1;
- Py_XINCREF(newdefault);
- dd->default_factory = newdefault;
- result = PyDict_Type.tp_init(self, newargs, kwds);
- Py_DECREF(newargs);
- Py_XDECREF(olddefault);
- return result;
+ defdictobject *dd = (defdictobject *)self;
+ PyObject *olddefault = dd->default_factory;
+ PyObject *newdefault = NULL;
+ PyObject *newargs;
+ int result;
+ if (args == NULL || !PyTuple_Check(args))
+ newargs = PyTuple_New(0);
+ else {
+ Py_ssize_t n = PyTuple_GET_SIZE(args);
+ if (n > 0) {
+ newdefault = PyTuple_GET_ITEM(args, 0);
+ if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "first argument must be callable");
+ return -1;
+ }
+ }
+ newargs = PySequence_GetSlice(args, 1, n);
+ }
+ if (newargs == NULL)
+ return -1;
+ Py_XINCREF(newdefault);
+ dd->default_factory = newdefault;
+ result = PyDict_Type.tp_init(self, newargs, kwds);
+ Py_DECREF(newargs);
+ Py_XDECREF(olddefault);
+ return result;
}
PyDoc_STRVAR(defdict_doc,
@@ -1389,47 +1389,47 @@ A defaultdict compares equal to a dict with the same items.\n\
#define DEFERRED_ADDRESS(ADDR) 0
static PyTypeObject defdict_type = {
- PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
- "collections.defaultdict", /* tp_name */
- sizeof(defdictobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)defdict_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)defdict_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- /* tp_flags */
- defdict_doc, /* tp_doc */
- defdict_traverse, /* tp_traverse */
- (inquiry)defdict_tp_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset*/
- 0, /* tp_iter */
- 0, /* tp_iternext */
- defdict_methods, /* tp_methods */
- defdict_members, /* tp_members */
- 0, /* tp_getset */
- DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- defdict_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- 0, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
+ "collections.defaultdict", /* tp_name */
+ sizeof(defdictobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)defdict_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)defdict_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ /* tp_flags */
+ defdict_doc, /* tp_doc */
+ defdict_traverse, /* tp_traverse */
+ (inquiry)defdict_tp_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset*/
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ defdict_methods, /* tp_methods */
+ defdict_members, /* tp_members */
+ 0, /* tp_getset */
+ DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ defdict_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* module level code ********************************************************/
@@ -1442,42 +1442,42 @@ PyDoc_STRVAR(module_doc,
static struct PyModuleDef _collectionsmodule = {
- PyModuleDef_HEAD_INIT,
- "_collections",
- module_doc,
- -1,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_collections",
+ module_doc,
+ -1,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__collections(void)
{
- PyObject *m;
+ PyObject *m;
- m = PyModule_Create(&_collectionsmodule);
- if (m == NULL)
- return NULL;
+ m = PyModule_Create(&_collectionsmodule);
+ if (m == NULL)
+ return NULL;
- if (PyType_Ready(&deque_type) < 0)
- return NULL;
- Py_INCREF(&deque_type);
- PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
+ if (PyType_Ready(&deque_type) < 0)
+ return NULL;
+ Py_INCREF(&deque_type);
+ PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
- defdict_type.tp_base = &PyDict_Type;
- if (PyType_Ready(&defdict_type) < 0)
- return NULL;
- Py_INCREF(&defdict_type);
- PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
+ defdict_type.tp_base = &PyDict_Type;
+ if (PyType_Ready(&defdict_type) < 0)
+ return NULL;
+ Py_INCREF(&defdict_type);
+ PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
- if (PyType_Ready(&dequeiter_type) < 0)
- return NULL;
+ if (PyType_Ready(&dequeiter_type) < 0)
+ return NULL;
- if (PyType_Ready(&dequereviter_type) < 0)
- return NULL;
+ if (PyType_Ready(&dequereviter_type) < 0)
+ return NULL;
- return m;
+ return m;
}
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 1fbba41..26d4cf0 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -18,65 +18,65 @@ module instead.
#include "structmember.h"
#define IS_BASESTRING(o) \
- PyUnicode_Check(o)
+ PyUnicode_Check(o)
-static PyObject *error_obj; /* CSV exception */
+static PyObject *error_obj; /* CSV exception */
static PyObject *dialects; /* Dialect registry */
-static long field_limit = 128 * 1024; /* max parsed field size */
+static long field_limit = 128 * 1024; /* max parsed field size */
typedef enum {
- START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
- IN_QUOTED_FIELD, ESCAPE_IN_QUOTED_FIELD, QUOTE_IN_QUOTED_FIELD,
- EAT_CRNL
+ START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
+ IN_QUOTED_FIELD, ESCAPE_IN_QUOTED_FIELD, QUOTE_IN_QUOTED_FIELD,
+ EAT_CRNL
} ParserState;
typedef enum {
- QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
+ QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
} QuoteStyle;
typedef struct {
- QuoteStyle style;
- char *name;
+ QuoteStyle style;
+ char *name;
} StyleDesc;
static StyleDesc quote_styles[] = {
- { QUOTE_MINIMAL, "QUOTE_MINIMAL" },
- { QUOTE_ALL, "QUOTE_ALL" },
- { QUOTE_NONNUMERIC, "QUOTE_NONNUMERIC" },
- { QUOTE_NONE, "QUOTE_NONE" },
- { 0 }
+ { QUOTE_MINIMAL, "QUOTE_MINIMAL" },
+ { QUOTE_ALL, "QUOTE_ALL" },
+ { QUOTE_NONNUMERIC, "QUOTE_NONNUMERIC" },
+ { QUOTE_NONE, "QUOTE_NONE" },
+ { 0 }
};
typedef struct {
- PyObject_HEAD
+ PyObject_HEAD
- int doublequote; /* is " represented by ""? */
- Py_UNICODE delimiter; /* field separator */
- Py_UNICODE quotechar; /* quote character */
- Py_UNICODE escapechar; /* escape character */
- int skipinitialspace; /* ignore spaces following delimiter? */
- PyObject *lineterminator; /* string to write between records */
- int quoting; /* style of quoting to write */
+ int doublequote; /* is " represented by ""? */
+ Py_UNICODE delimiter; /* field separator */
+ Py_UNICODE quotechar; /* quote character */
+ Py_UNICODE escapechar; /* escape character */
+ int skipinitialspace; /* ignore spaces following delimiter? */
+ PyObject *lineterminator; /* string to write between records */
+ int quoting; /* style of quoting to write */
- int strict; /* raise exception on bad CSV */
+ int strict; /* raise exception on bad CSV */
} DialectObj;
static PyTypeObject Dialect_Type;
typedef struct {
- PyObject_HEAD
+ PyObject_HEAD
- PyObject *input_iter; /* iterate over this for input lines */
+ PyObject *input_iter; /* iterate over this for input lines */
- DialectObj *dialect; /* parsing dialect */
+ DialectObj *dialect; /* parsing dialect */
- PyObject *fields; /* field list for current record */
- ParserState state; /* current CSV parse state */
- Py_UNICODE *field; /* build current field in here */
- int field_size; /* size of allocated buffer */
- Py_ssize_t field_len; /* length of current field */
- int numeric_field; /* treat field as numeric */
- unsigned long line_num; /* Source-file line number */
+ PyObject *fields; /* field list for current record */
+ ParserState state; /* current CSV parse state */
+ Py_UNICODE *field; /* build current field in here */
+ int field_size; /* size of allocated buffer */
+ Py_ssize_t field_len; /* length of current field */
+ int numeric_field; /* treat field as numeric */
+ unsigned long line_num; /* Source-file line number */
} ReaderObj;
static PyTypeObject Reader_Type;
@@ -84,16 +84,16 @@ static PyTypeObject Reader_Type;
#define ReaderObject_Check(v) (Py_TYPE(v) == &Reader_Type)
typedef struct {
- PyObject_HEAD
+ PyObject_HEAD
- PyObject *writeline; /* write output lines to this file */
+ PyObject *writeline; /* write output lines to this file */
- DialectObj *dialect; /* parsing dialect */
+ DialectObj *dialect; /* parsing dialect */
- Py_UNICODE *rec; /* buffer for parser.join */
- int rec_size; /* size of allocated record */
- Py_ssize_t rec_len; /* length of record */
- int num_fields; /* number of fields in record */
+ Py_UNICODE *rec; /* buffer for parser.join */
+ int rec_size; /* size of allocated record */
+ Py_ssize_t rec_len; /* length of record */
+ int num_fields; /* number of fields in record */
} WriterObj;
static PyTypeObject Writer_Type;
@@ -105,375 +105,375 @@ static PyTypeObject Writer_Type;
static PyObject *
get_dialect_from_registry(PyObject * name_obj)
{
- PyObject *dialect_obj;
-
- dialect_obj = PyDict_GetItem(dialects, name_obj);
- if (dialect_obj == NULL) {
- if (!PyErr_Occurred())
- PyErr_Format(error_obj, "unknown dialect");
- }
- else
- Py_INCREF(dialect_obj);
- return dialect_obj;
+ PyObject *dialect_obj;
+
+ dialect_obj = PyDict_GetItem(dialects, name_obj);
+ if (dialect_obj == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_Format(error_obj, "unknown dialect");
+ }
+ else
+ Py_INCREF(dialect_obj);
+ return dialect_obj;
}
static PyObject *
get_string(PyObject *str)
{
- Py_XINCREF(str);
- return str;
+ Py_XINCREF(str);
+ return str;
}
static PyObject *
get_nullchar_as_None(Py_UNICODE c)
{
- if (c == '\0') {
- Py_INCREF(Py_None);
- return Py_None;
- }
- else
- return PyUnicode_FromUnicode((Py_UNICODE *)&c, 1);
+ if (c == '\0') {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else
+ return PyUnicode_FromUnicode((Py_UNICODE *)&c, 1);
}
static PyObject *
Dialect_get_lineterminator(DialectObj *self)
{
- return get_string(self->lineterminator);
+ return get_string(self->lineterminator);
}
static PyObject *
Dialect_get_delimiter(DialectObj *self)
{
- return get_nullchar_as_None(self->delimiter);
+ return get_nullchar_as_None(self->delimiter);
}
static PyObject *
Dialect_get_escapechar(DialectObj *self)
{
- return get_nullchar_as_None(self->escapechar);
+ return get_nullchar_as_None(self->escapechar);
}
static PyObject *
Dialect_get_quotechar(DialectObj *self)
{
- return get_nullchar_as_None(self->quotechar);
+ return get_nullchar_as_None(self->quotechar);
}
static PyObject *
Dialect_get_quoting(DialectObj *self)
{
- return PyLong_FromLong(self->quoting);
+ return PyLong_FromLong(self->quoting);
}
static int
_set_bool(const char *name, int *target, PyObject *src, int dflt)
{
- if (src == NULL)
- *target = dflt;
- else
- *target = PyObject_IsTrue(src);
- return 0;
+ if (src == NULL)
+ *target = dflt;
+ else
+ *target = PyObject_IsTrue(src);
+ return 0;
}
static int
_set_int(const char *name, int *target, PyObject *src, int dflt)
{
- if (src == NULL)
- *target = dflt;
- else {
- long value;
- if (!PyLong_CheckExact(src)) {
- PyErr_Format(PyExc_TypeError,
- "\"%s\" must be an integer", name);
- return -1;
- }
- value = PyLong_AsLong(src);
- if (value == -1 && PyErr_Occurred())
- return -1;
+ if (src == NULL)
+ *target = dflt;
+ else {
+ long value;
+ if (!PyLong_CheckExact(src)) {
+ PyErr_Format(PyExc_TypeError,
+ "\"%s\" must be an integer", name);
+ return -1;
+ }
+ value = PyLong_AsLong(src);
+ if (value == -1 && PyErr_Occurred())
+ return -1;
#if SIZEOF_LONG > SIZEOF_INT
- if (value > INT_MAX || value < INT_MIN) {
- PyErr_Format(PyExc_ValueError,
- "integer out of range for \"%s\"", name);
- return -1;
- }
+ if (value > INT_MAX || value < INT_MIN) {
+ PyErr_Format(PyExc_ValueError,
+ "integer out of range for \"%s\"", name);
+ return -1;
+ }
#endif
- *target = (int)value;
- }
- return 0;
+ *target = (int)value;
+ }
+ return 0;
}
static int
_set_char(const char *name, Py_UNICODE *target, PyObject *src, Py_UNICODE dflt)
{
- if (src == NULL)
- *target = dflt;
- else {
- *target = '\0';
- if (src != Py_None) {
- Py_UNICODE *buf;
- Py_ssize_t len;
- buf = PyUnicode_AsUnicode(src);
- len = PyUnicode_GetSize(src);
- if (buf == NULL || len > 1) {
- PyErr_Format(PyExc_TypeError,
- "\"%s\" must be an 1-character string",
- name);
- return -1;
- }
- if (len > 0)
- *target = buf[0];
- }
- }
- return 0;
+ if (src == NULL)
+ *target = dflt;
+ else {
+ *target = '\0';
+ if (src != Py_None) {
+ Py_UNICODE *buf;
+ Py_ssize_t len;
+ buf = PyUnicode_AsUnicode(src);
+ len = PyUnicode_GetSize(src);
+ if (buf == NULL || len > 1) {
+ PyErr_Format(PyExc_TypeError,
+ "\"%s\" must be an 1-character string",
+ name);
+ return -1;
+ }
+ if (len > 0)
+ *target = buf[0];
+ }
+ }
+ return 0;
}
static int
_set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
{
- if (src == NULL)
- *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL);
- else {
- if (src == Py_None)
- *target = NULL;
- else if (!IS_BASESTRING(src)) {
- PyErr_Format(PyExc_TypeError,
- "\"%s\" must be a string", name);
- return -1;
- }
- else {
- Py_XDECREF(*target);
- Py_INCREF(src);
- *target = src;
- }
- }
- return 0;
+ if (src == NULL)
+ *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL);
+ else {
+ if (src == Py_None)
+ *target = NULL;
+ else if (!IS_BASESTRING(src)) {
+ PyErr_Format(PyExc_TypeError,
+ "\"%s\" must be a string", name);
+ return -1;
+ }
+ else {
+ Py_XDECREF(*target);
+ Py_INCREF(src);
+ *target = src;
+ }
+ }
+ return 0;
}
static int
dialect_check_quoting(int quoting)
{
- StyleDesc *qs = quote_styles;
-
- for (qs = quote_styles; qs->name; qs++) {
- if (qs->style == quoting)
- return 0;
- }
- PyErr_Format(PyExc_TypeError, "bad \"quoting\" value");
- return -1;
+ StyleDesc *qs = quote_styles;
+
+ for (qs = quote_styles; qs->name; qs++) {
+ if (qs->style == quoting)
+ return 0;
+ }
+ PyErr_Format(PyExc_TypeError, "bad \"quoting\" value");
+ return -1;
}
#define D_OFF(x) offsetof(DialectObj, x)
static struct PyMemberDef Dialect_memberlist[] = {
- { "skipinitialspace", T_INT, D_OFF(skipinitialspace), READONLY },
- { "doublequote", T_INT, D_OFF(doublequote), READONLY },
- { "strict", T_INT, D_OFF(strict), READONLY },
- { NULL }
+ { "skipinitialspace", T_INT, D_OFF(skipinitialspace), READONLY },
+ { "doublequote", T_INT, D_OFF(doublequote), READONLY },
+ { "strict", T_INT, D_OFF(strict), READONLY },
+ { NULL }
};
static PyGetSetDef Dialect_getsetlist[] = {
- { "delimiter", (getter)Dialect_get_delimiter},
- { "escapechar", (getter)Dialect_get_escapechar},
- { "lineterminator", (getter)Dialect_get_lineterminator},
- { "quotechar", (getter)Dialect_get_quotechar},
- { "quoting", (getter)Dialect_get_quoting},
- {NULL},
+ { "delimiter", (getter)Dialect_get_delimiter},
+ { "escapechar", (getter)Dialect_get_escapechar},
+ { "lineterminator", (getter)Dialect_get_lineterminator},
+ { "quotechar", (getter)Dialect_get_quotechar},
+ { "quoting", (getter)Dialect_get_quoting},
+ {NULL},
};
static void
Dialect_dealloc(DialectObj *self)
{
- Py_XDECREF(self->lineterminator);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ Py_XDECREF(self->lineterminator);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
static char *dialect_kws[] = {
- "dialect",
- "delimiter",
- "doublequote",
- "escapechar",
- "lineterminator",
- "quotechar",
- "quoting",
- "skipinitialspace",
- "strict",
- NULL
+ "dialect",
+ "delimiter",
+ "doublequote",
+ "escapechar",
+ "lineterminator",
+ "quotechar",
+ "quoting",
+ "skipinitialspace",
+ "strict",
+ NULL
};
static PyObject *
dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
- DialectObj *self;
- PyObject *ret = NULL;
- PyObject *dialect = NULL;
- PyObject *delimiter = NULL;
- PyObject *doublequote = NULL;
- PyObject *escapechar = NULL;
- PyObject *lineterminator = NULL;
- PyObject *quotechar = NULL;
- PyObject *quoting = NULL;
- PyObject *skipinitialspace = NULL;
- PyObject *strict = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs,
- "|OOOOOOOOO", dialect_kws,
- &dialect,
- &delimiter,
- &doublequote,
- &escapechar,
- &lineterminator,
- &quotechar,
- &quoting,
- &skipinitialspace,
- &strict))
- return NULL;
-
- if (dialect != NULL) {
- if (IS_BASESTRING(dialect)) {
- dialect = get_dialect_from_registry(dialect);
- if (dialect == NULL)
- return NULL;
- }
- else
- Py_INCREF(dialect);
- /* Can we reuse this instance? */
- if (PyObject_TypeCheck(dialect, &Dialect_Type) &&
- delimiter == 0 &&
- doublequote == 0 &&
- escapechar == 0 &&
- lineterminator == 0 &&
- quotechar == 0 &&
- quoting == 0 &&
- skipinitialspace == 0 &&
- strict == 0)
- return dialect;
- }
-
- self = (DialectObj *)type->tp_alloc(type, 0);
- if (self == NULL) {
- Py_XDECREF(dialect);
- return NULL;
- }
- self->lineterminator = NULL;
-
- Py_XINCREF(delimiter);
- Py_XINCREF(doublequote);
- Py_XINCREF(escapechar);
- Py_XINCREF(lineterminator);
- Py_XINCREF(quotechar);
- Py_XINCREF(quoting);
- Py_XINCREF(skipinitialspace);
- Py_XINCREF(strict);
- if (dialect != NULL) {
+ DialectObj *self;
+ PyObject *ret = NULL;
+ PyObject *dialect = NULL;
+ PyObject *delimiter = NULL;
+ PyObject *doublequote = NULL;
+ PyObject *escapechar = NULL;
+ PyObject *lineterminator = NULL;
+ PyObject *quotechar = NULL;
+ PyObject *quoting = NULL;
+ PyObject *skipinitialspace = NULL;
+ PyObject *strict = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+ "|OOOOOOOOO", dialect_kws,
+ &dialect,
+ &delimiter,
+ &doublequote,
+ &escapechar,
+ &lineterminator,
+ &quotechar,
+ &quoting,
+ &skipinitialspace,
+ &strict))
+ return NULL;
+
+ if (dialect != NULL) {
+ if (IS_BASESTRING(dialect)) {
+ dialect = get_dialect_from_registry(dialect);
+ if (dialect == NULL)
+ return NULL;
+ }
+ else
+ Py_INCREF(dialect);
+ /* Can we reuse this instance? */
+ if (PyObject_TypeCheck(dialect, &Dialect_Type) &&
+ delimiter == 0 &&
+ doublequote == 0 &&
+ escapechar == 0 &&
+ lineterminator == 0 &&
+ quotechar == 0 &&
+ quoting == 0 &&
+ skipinitialspace == 0 &&
+ strict == 0)
+ return dialect;
+ }
+
+ self = (DialectObj *)type->tp_alloc(type, 0);
+ if (self == NULL) {
+ Py_XDECREF(dialect);
+ return NULL;
+ }
+ self->lineterminator = NULL;
+
+ Py_XINCREF(delimiter);
+ Py_XINCREF(doublequote);
+ Py_XINCREF(escapechar);
+ Py_XINCREF(lineterminator);
+ Py_XINCREF(quotechar);
+ Py_XINCREF(quoting);
+ Py_XINCREF(skipinitialspace);
+ Py_XINCREF(strict);
+ if (dialect != NULL) {
#define DIALECT_GETATTR(v, n) \
- if (v == NULL) \
- v = PyObject_GetAttrString(dialect, n)
- DIALECT_GETATTR(delimiter, "delimiter");
- DIALECT_GETATTR(doublequote, "doublequote");
- DIALECT_GETATTR(escapechar, "escapechar");
- DIALECT_GETATTR(lineterminator, "lineterminator");
- DIALECT_GETATTR(quotechar, "quotechar");
- DIALECT_GETATTR(quoting, "quoting");
- DIALECT_GETATTR(skipinitialspace, "skipinitialspace");
- DIALECT_GETATTR(strict, "strict");
- PyErr_Clear();
- }
-
- /* check types and convert to C values */
+ if (v == NULL) \
+ v = PyObject_GetAttrString(dialect, n)
+ DIALECT_GETATTR(delimiter, "delimiter");
+ DIALECT_GETATTR(doublequote, "doublequote");
+ DIALECT_GETATTR(escapechar, "escapechar");
+ DIALECT_GETATTR(lineterminator, "lineterminator");
+ DIALECT_GETATTR(quotechar, "quotechar");
+ DIALECT_GETATTR(quoting, "quoting");
+ DIALECT_GETATTR(skipinitialspace, "skipinitialspace");
+ DIALECT_GETATTR(strict, "strict");
+ PyErr_Clear();
+ }
+
+ /* check types and convert to C values */
#define DIASET(meth, name, target, src, dflt) \
- if (meth(name, target, src, dflt)) \
- goto err
- DIASET(_set_char, "delimiter", &self->delimiter, delimiter, ',');
- DIASET(_set_bool, "doublequote", &self->doublequote, doublequote, 1);
- DIASET(_set_char, "escapechar", &self->escapechar, escapechar, 0);
- DIASET(_set_str, "lineterminator", &self->lineterminator, lineterminator, "\r\n");
- DIASET(_set_char, "quotechar", &self->quotechar, quotechar, '"');
- DIASET(_set_int, "quoting", &self->quoting, quoting, QUOTE_MINIMAL);
- DIASET(_set_bool, "skipinitialspace", &self->skipinitialspace, skipinitialspace, 0);
- DIASET(_set_bool, "strict", &self->strict, strict, 0);
-
- /* validate options */
- if (dialect_check_quoting(self->quoting))
- goto err;
- if (self->delimiter == 0) {
- PyErr_SetString(PyExc_TypeError, "delimiter must be set");
- goto err;
- }
- if (quotechar == Py_None && quoting == NULL)
- self->quoting = QUOTE_NONE;
- if (self->quoting != QUOTE_NONE && self->quotechar == 0) {
- PyErr_SetString(PyExc_TypeError,
- "quotechar must be set if quoting enabled");
- goto err;
- }
- if (self->lineterminator == 0) {
- PyErr_SetString(PyExc_TypeError, "lineterminator must be set");
- goto err;
- }
-
- ret = (PyObject *)self;
- Py_INCREF(self);
+ if (meth(name, target, src, dflt)) \
+ goto err
+ DIASET(_set_char, "delimiter", &self->delimiter, delimiter, ',');
+ DIASET(_set_bool, "doublequote", &self->doublequote, doublequote, 1);
+ DIASET(_set_char, "escapechar", &self->escapechar, escapechar, 0);
+ DIASET(_set_str, "lineterminator", &self->lineterminator, lineterminator, "\r\n");
+ DIASET(_set_char, "quotechar", &self->quotechar, quotechar, '"');
+ DIASET(_set_int, "quoting", &self->quoting, quoting, QUOTE_MINIMAL);
+ DIASET(_set_bool, "skipinitialspace", &self->skipinitialspace, skipinitialspace, 0);
+ DIASET(_set_bool, "strict", &self->strict, strict, 0);
+
+ /* validate options */
+ if (dialect_check_quoting(self->quoting))
+ goto err;
+ if (self->delimiter == 0) {
+ PyErr_SetString(PyExc_TypeError, "delimiter must be set");
+ goto err;
+ }
+ if (quotechar == Py_None && quoting == NULL)
+ self->quoting = QUOTE_NONE;
+ if (self->quoting != QUOTE_NONE && self->quotechar == 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "quotechar must be set if quoting enabled");
+ goto err;
+ }
+ if (self->lineterminator == 0) {
+ PyErr_SetString(PyExc_TypeError, "lineterminator must be set");
+ goto err;
+ }
+
+ ret = (PyObject *)self;
+ Py_INCREF(self);
err:
- Py_XDECREF(self);
- Py_XDECREF(dialect);
- Py_XDECREF(delimiter);
- Py_XDECREF(doublequote);
- Py_XDECREF(escapechar);
- Py_XDECREF(lineterminator);
- Py_XDECREF(quotechar);
- Py_XDECREF(quoting);
- Py_XDECREF(skipinitialspace);
- Py_XDECREF(strict);
- return ret;
+ Py_XDECREF(self);
+ Py_XDECREF(dialect);
+ Py_XDECREF(delimiter);
+ Py_XDECREF(doublequote);
+ Py_XDECREF(escapechar);
+ Py_XDECREF(lineterminator);
+ Py_XDECREF(quotechar);
+ Py_XDECREF(quoting);
+ Py_XDECREF(skipinitialspace);
+ Py_XDECREF(strict);
+ return ret;
}
-PyDoc_STRVAR(Dialect_Type_doc,
+PyDoc_STRVAR(Dialect_Type_doc,
"CSV dialect\n"
"\n"
"The Dialect type records CSV parsing and generation options.\n");
static PyTypeObject Dialect_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_csv.Dialect", /* tp_name */
- sizeof(DialectObj), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)Dialect_dealloc, /* tp_dealloc */
- (printfunc)0, /* tp_print */
- (getattrfunc)0, /* tp_getattr */
- (setattrfunc)0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)0, /* tp_hash */
- (ternaryfunc)0, /* tp_call */
- (reprfunc)0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- Dialect_Type_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- Dialect_memberlist, /* tp_members */
- Dialect_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- dialect_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_csv.Dialect", /* tp_name */
+ sizeof(DialectObj), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)Dialect_dealloc, /* tp_dealloc */
+ (printfunc)0, /* tp_print */
+ (getattrfunc)0, /* tp_getattr */
+ (setattrfunc)0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)0, /* tp_hash */
+ (ternaryfunc)0, /* tp_call */
+ (reprfunc)0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ Dialect_Type_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ Dialect_memberlist, /* tp_members */
+ Dialect_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ dialect_new, /* tp_new */
+ 0, /* tp_free */
};
/*
@@ -483,15 +483,15 @@ static PyTypeObject Dialect_Type = {
static PyObject *
_call_dialect(PyObject *dialect_inst, PyObject *kwargs)
{
- PyObject *ctor_args;
- PyObject *dialect;
-
- ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst);
- if (ctor_args == NULL)
- return NULL;
- dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs);
- Py_DECREF(ctor_args);
- return dialect;
+ PyObject *ctor_args;
+ PyObject *dialect;
+
+ ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst);
+ if (ctor_args == NULL)
+ return NULL;
+ dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs);
+ Py_DECREF(ctor_args);
+ return dialect;
}
/*
@@ -500,339 +500,339 @@ _call_dialect(PyObject *dialect_inst, PyObject *kwargs)
static int
parse_save_field(ReaderObj *self)
{
- PyObject *field;
-
- field = PyUnicode_FromUnicode(self->field, self->field_len);
- if (field == NULL)
- return -1;
- self->field_len = 0;
- if (self->numeric_field) {
- PyObject *tmp;
-
- self->numeric_field = 0;
- tmp = PyNumber_Float(field);
- if (tmp == NULL) {
- Py_DECREF(field);
- return -1;
- }
- Py_DECREF(field);
- field = tmp;
- }
- PyList_Append(self->fields, field);
- Py_DECREF(field);
- return 0;
+ PyObject *field;
+
+ field = PyUnicode_FromUnicode(self->field, self->field_len);
+ if (field == NULL)
+ return -1;
+ self->field_len = 0;
+ if (self->numeric_field) {
+ PyObject *tmp;
+
+ self->numeric_field = 0;
+ tmp = PyNumber_Float(field);
+ if (tmp == NULL) {
+ Py_DECREF(field);
+ return -1;
+ }
+ Py_DECREF(field);
+ field = tmp;
+ }
+ PyList_Append(self->fields, field);
+ Py_DECREF(field);
+ return 0;
}
static int
parse_grow_buff(ReaderObj *self)
{
- if (self->field_size == 0) {
- self->field_size = 4096;
- if (self->field != NULL)
- PyMem_Free(self->field);
- self->field = PyMem_New(Py_UNICODE, self->field_size);
- }
- else {
- if (self->field_size > INT_MAX / 2) {
- PyErr_NoMemory();
- return 0;
- }
- self->field_size *= 2;
- self->field = PyMem_Resize(self->field, Py_UNICODE,
- self->field_size);
- }
- if (self->field == NULL) {
- PyErr_NoMemory();
- return 0;
- }
- return 1;
+ if (self->field_size == 0) {
+ self->field_size = 4096;
+ if (self->field != NULL)
+ PyMem_Free(self->field);
+ self->field = PyMem_New(Py_UNICODE, self->field_size);
+ }
+ else {
+ if (self->field_size > INT_MAX / 2) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ self->field_size *= 2;
+ self->field = PyMem_Resize(self->field, Py_UNICODE,
+ self->field_size);
+ }
+ if (self->field == NULL) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ return 1;
}
static int
parse_add_char(ReaderObj *self, Py_UNICODE c)
{
- if (self->field_len >= field_limit) {
- PyErr_Format(error_obj, "field larger than field limit (%ld)",
- field_limit);
- return -1;
- }
- if (self->field_len == self->field_size && !parse_grow_buff(self))
- return -1;
- self->field[self->field_len++] = c;
- return 0;
+ if (self->field_len >= field_limit) {
+ PyErr_Format(error_obj, "field larger than field limit (%ld)",
+ field_limit);
+ return -1;
+ }
+ if (self->field_len == self->field_size && !parse_grow_buff(self))
+ return -1;
+ self->field[self->field_len++] = c;
+ return 0;
}
static int
parse_process_char(ReaderObj *self, Py_UNICODE c)
{
- DialectObj *dialect = self->dialect;
-
- switch (self->state) {
- case START_RECORD:
- /* start of record */
- if (c == '\0')
- /* empty line - return [] */
- break;
- else if (c == '\n' || c == '\r') {
- self->state = EAT_CRNL;
- break;
- }
- /* normal character - handle as START_FIELD */
- self->state = START_FIELD;
- /* fallthru */
- case START_FIELD:
- /* expecting field */
- if (c == '\n' || c == '\r' || c == '\0') {
- /* save empty field - return [fields] */
- if (parse_save_field(self) < 0)
- return -1;
- self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
- }
- else if (c == dialect->quotechar &&
- dialect->quoting != QUOTE_NONE) {
- /* start quoted field */
- self->state = IN_QUOTED_FIELD;
- }
- else if (c == dialect->escapechar) {
- /* possible escaped character */
- self->state = ESCAPED_CHAR;
- }
- else if (c == ' ' && dialect->skipinitialspace)
- /* ignore space at start of field */
- ;
- else if (c == dialect->delimiter) {
- /* save empty field */
- if (parse_save_field(self) < 0)
- return -1;
- }
- else {
- /* begin new unquoted field */
- if (dialect->quoting == QUOTE_NONNUMERIC)
- self->numeric_field = 1;
- if (parse_add_char(self, c) < 0)
- return -1;
- self->state = IN_FIELD;
- }
- break;
-
- case ESCAPED_CHAR:
- if (c == '\0')
- c = '\n';
- if (parse_add_char(self, c) < 0)
- return -1;
- self->state = IN_FIELD;
- break;
-
- case IN_FIELD:
- /* in unquoted field */
- if (c == '\n' || c == '\r' || c == '\0') {
- /* end of line - return [fields] */
- if (parse_save_field(self) < 0)
- return -1;
- self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
- }
- else if (c == dialect->escapechar) {
- /* possible escaped character */
- self->state = ESCAPED_CHAR;
- }
- else if (c == dialect->delimiter) {
- /* save field - wait for new field */
- if (parse_save_field(self) < 0)
- return -1;
- self->state = START_FIELD;
- }
- else {
- /* normal character - save in field */
- if (parse_add_char(self, c) < 0)
- return -1;
- }
- break;
-
- case IN_QUOTED_FIELD:
- /* in quoted field */
- if (c == '\0')
- ;
- else if (c == dialect->escapechar) {
- /* Possible escape character */
- self->state = ESCAPE_IN_QUOTED_FIELD;
- }
- else if (c == dialect->quotechar &&
- dialect->quoting != QUOTE_NONE) {
- if (dialect->doublequote) {
- /* doublequote; " represented by "" */
- self->state = QUOTE_IN_QUOTED_FIELD;
- }
- else {
- /* end of quote part of field */
- self->state = IN_FIELD;
- }
- }
- else {
- /* normal character - save in field */
- if (parse_add_char(self, c) < 0)
- return -1;
- }
- break;
-
- case ESCAPE_IN_QUOTED_FIELD:
- if (c == '\0')
- c = '\n';
- if (parse_add_char(self, c) < 0)
- return -1;
- self->state = IN_QUOTED_FIELD;
- break;
-
- case QUOTE_IN_QUOTED_FIELD:
- /* doublequote - seen a quote in an quoted field */
- if (dialect->quoting != QUOTE_NONE &&
- c == dialect->quotechar) {
- /* save "" as " */
- if (parse_add_char(self, c) < 0)
- return -1;
- self->state = IN_QUOTED_FIELD;
- }
- else if (c == dialect->delimiter) {
- /* save field - wait for new field */
- if (parse_save_field(self) < 0)
- return -1;
- self->state = START_FIELD;
- }
- else if (c == '\n' || c == '\r' || c == '\0') {
- /* end of line - return [fields] */
- if (parse_save_field(self) < 0)
- return -1;
- self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
- }
- else if (!dialect->strict) {
- if (parse_add_char(self, c) < 0)
- return -1;
- self->state = IN_FIELD;
- }
- else {
- /* illegal */
- PyErr_Format(error_obj, "'%c' expected after '%c'",
- dialect->delimiter,
- dialect->quotechar);
- return -1;
- }
- break;
-
- case EAT_CRNL:
- if (c == '\n' || c == '\r')
- ;
- else if (c == '\0')
- self->state = START_RECORD;
- else {
- PyErr_Format(error_obj, "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?");
- return -1;
- }
- break;
-
- }
- return 0;
+ DialectObj *dialect = self->dialect;
+
+ switch (self->state) {
+ case START_RECORD:
+ /* start of record */
+ if (c == '\0')
+ /* empty line - return [] */
+ break;
+ else if (c == '\n' || c == '\r') {
+ self->state = EAT_CRNL;
+ break;
+ }
+ /* normal character - handle as START_FIELD */
+ self->state = START_FIELD;
+ /* fallthru */
+ case START_FIELD:
+ /* expecting field */
+ if (c == '\n' || c == '\r' || c == '\0') {
+ /* save empty field - return [fields] */
+ if (parse_save_field(self) < 0)
+ return -1;
+ self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
+ }
+ else if (c == dialect->quotechar &&
+ dialect->quoting != QUOTE_NONE) {
+ /* start quoted field */
+ self->state = IN_QUOTED_FIELD;
+ }
+ else if (c == dialect->escapechar) {
+ /* possible escaped character */
+ self->state = ESCAPED_CHAR;
+ }
+ else if (c == ' ' && dialect->skipinitialspace)
+ /* ignore space at start of field */
+ ;
+ else if (c == dialect->delimiter) {
+ /* save empty field */
+ if (parse_save_field(self) < 0)
+ return -1;
+ }
+ else {
+ /* begin new unquoted field */
+ if (dialect->quoting == QUOTE_NONNUMERIC)
+ self->numeric_field = 1;
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ self->state = IN_FIELD;
+ }
+ break;
+
+ case ESCAPED_CHAR:
+ if (c == '\0')
+ c = '\n';
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ self->state = IN_FIELD;
+ break;
+
+ case IN_FIELD:
+ /* in unquoted field */
+ if (c == '\n' || c == '\r' || c == '\0') {
+ /* end of line - return [fields] */
+ if (parse_save_field(self) < 0)
+ return -1;
+ self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
+ }
+ else if (c == dialect->escapechar) {
+ /* possible escaped character */
+ self->state = ESCAPED_CHAR;
+ }
+ else if (c == dialect->delimiter) {
+ /* save field - wait for new field */
+ if (parse_save_field(self) < 0)
+ return -1;
+ self->state = START_FIELD;
+ }
+ else {
+ /* normal character - save in field */
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ }
+ break;
+
+ case IN_QUOTED_FIELD:
+ /* in quoted field */
+ if (c == '\0')
+ ;
+ else if (c == dialect->escapechar) {
+ /* Possible escape character */
+ self->state = ESCAPE_IN_QUOTED_FIELD;
+ }
+ else if (c == dialect->quotechar &&
+ dialect->quoting != QUOTE_NONE) {
+ if (dialect->doublequote) {
+ /* doublequote; " represented by "" */
+ self->state = QUOTE_IN_QUOTED_FIELD;
+ }
+ else {
+ /* end of quote part of field */
+ self->state = IN_FIELD;
+ }
+ }
+ else {
+ /* normal character - save in field */
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ }
+ break;
+
+ case ESCAPE_IN_QUOTED_FIELD:
+ if (c == '\0')
+ c = '\n';
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ self->state = IN_QUOTED_FIELD;
+ break;
+
+ case QUOTE_IN_QUOTED_FIELD:
+ /* doublequote - seen a quote in an quoted field */
+ if (dialect->quoting != QUOTE_NONE &&
+ c == dialect->quotechar) {
+ /* save "" as " */
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ self->state = IN_QUOTED_FIELD;
+ }
+ else if (c == dialect->delimiter) {
+ /* save field - wait for new field */
+ if (parse_save_field(self) < 0)
+ return -1;
+ self->state = START_FIELD;
+ }
+ else if (c == '\n' || c == '\r' || c == '\0') {
+ /* end of line - return [fields] */
+ if (parse_save_field(self) < 0)
+ return -1;
+ self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
+ }
+ else if (!dialect->strict) {
+ if (parse_add_char(self, c) < 0)
+ return -1;
+ self->state = IN_FIELD;
+ }
+ else {
+ /* illegal */
+ PyErr_Format(error_obj, "'%c' expected after '%c'",
+ dialect->delimiter,
+ dialect->quotechar);
+ return -1;
+ }
+ break;
+
+ case EAT_CRNL:
+ if (c == '\n' || c == '\r')
+ ;
+ else if (c == '\0')
+ self->state = START_RECORD;
+ else {
+ PyErr_Format(error_obj, "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?");
+ return -1;
+ }
+ break;
+
+ }
+ return 0;
}
static int
parse_reset(ReaderObj *self)
{
- Py_XDECREF(self->fields);
- self->fields = PyList_New(0);
- if (self->fields == NULL)
- return -1;
- self->field_len = 0;
- self->state = START_RECORD;
- self->numeric_field = 0;
- return 0;
+ Py_XDECREF(self->fields);
+ self->fields = PyList_New(0);
+ if (self->fields == NULL)
+ return -1;
+ self->field_len = 0;
+ self->state = START_RECORD;
+ self->numeric_field = 0;
+ return 0;
}
static PyObject *
Reader_iternext(ReaderObj *self)
{
- PyObject *lineobj;
- PyObject *fields = NULL;
- Py_UNICODE *line, c;
- Py_ssize_t linelen;
-
- if (parse_reset(self) < 0)
- return NULL;
- do {
- lineobj = PyIter_Next(self->input_iter);
- if (lineobj == NULL) {
- /* End of input OR exception */
- if (!PyErr_Occurred() && self->field_len != 0)
- PyErr_Format(error_obj,
- "newline inside string");
- return NULL;
- }
- if (!PyUnicode_Check(lineobj)) {
- PyErr_Format(error_obj,
- "iterator should return strings, "
- "not %.200s "
- "(did you open the file in text mode?)",
- lineobj->ob_type->tp_name
- );
- Py_DECREF(lineobj);
- return NULL;
- }
- ++self->line_num;
- line = PyUnicode_AsUnicode(lineobj);
- linelen = PyUnicode_GetSize(lineobj);
- if (line == NULL || linelen < 0) {
- Py_DECREF(lineobj);
- return NULL;
- }
- while (linelen--) {
- c = *line++;
- if (c == '\0') {
- Py_DECREF(lineobj);
- PyErr_Format(error_obj,
- "line contains NULL byte");
- goto err;
- }
- if (parse_process_char(self, c) < 0) {
- Py_DECREF(lineobj);
- goto err;
- }
- }
+ PyObject *lineobj;
+ PyObject *fields = NULL;
+ Py_UNICODE *line, c;
+ Py_ssize_t linelen;
+
+ if (parse_reset(self) < 0)
+ return NULL;
+ do {
+ lineobj = PyIter_Next(self->input_iter);
+ if (lineobj == NULL) {
+ /* End of input OR exception */
+ if (!PyErr_Occurred() && self->field_len != 0)
+ PyErr_Format(error_obj,
+ "newline inside string");
+ return NULL;
+ }
+ if (!PyUnicode_Check(lineobj)) {
+ PyErr_Format(error_obj,
+ "iterator should return strings, "
+ "not %.200s "
+ "(did you open the file in text mode?)",
+ lineobj->ob_type->tp_name
+ );
+ Py_DECREF(lineobj);
+ return NULL;
+ }
+ ++self->line_num;
+ line = PyUnicode_AsUnicode(lineobj);
+ linelen = PyUnicode_GetSize(lineobj);
+ if (line == NULL || linelen < 0) {
+ Py_DECREF(lineobj);
+ return NULL;
+ }
+ while (linelen--) {
+ c = *line++;
+ if (c == '\0') {
Py_DECREF(lineobj);
- if (parse_process_char(self, 0) < 0)
- goto err;
- } while (self->state != START_RECORD);
+ PyErr_Format(error_obj,
+ "line contains NULL byte");
+ goto err;
+ }
+ if (parse_process_char(self, c) < 0) {
+ Py_DECREF(lineobj);
+ goto err;
+ }
+ }
+ Py_DECREF(lineobj);
+ if (parse_process_char(self, 0) < 0)
+ goto err;
+ } while (self->state != START_RECORD);
- fields = self->fields;
- self->fields = NULL;
+ fields = self->fields;
+ self->fields = NULL;
err:
- return fields;
+ return fields;
}
static void
Reader_dealloc(ReaderObj *self)
{
- PyObject_GC_UnTrack(self);
- Py_XDECREF(self->dialect);
- Py_XDECREF(self->input_iter);
- Py_XDECREF(self->fields);
- if (self->field != NULL)
- PyMem_Free(self->field);
- PyObject_GC_Del(self);
+ PyObject_GC_UnTrack(self);
+ Py_XDECREF(self->dialect);
+ Py_XDECREF(self->input_iter);
+ Py_XDECREF(self->fields);
+ if (self->field != NULL)
+ PyMem_Free(self->field);
+ PyObject_GC_Del(self);
}
static int
Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
{
- Py_VISIT(self->dialect);
- Py_VISIT(self->input_iter);
- Py_VISIT(self->fields);
- return 0;
+ Py_VISIT(self->dialect);
+ Py_VISIT(self->input_iter);
+ Py_VISIT(self->fields);
+ return 0;
}
static int
Reader_clear(ReaderObj *self)
{
- Py_CLEAR(self->dialect);
- Py_CLEAR(self->input_iter);
- Py_CLEAR(self->fields);
- return 0;
+ Py_CLEAR(self->dialect);
+ Py_CLEAR(self->input_iter);
+ Py_CLEAR(self->fields);
+ return 0;
}
PyDoc_STRVAR(Reader_Type_doc,
@@ -843,93 +843,93 @@ PyDoc_STRVAR(Reader_Type_doc,
);
static struct PyMethodDef Reader_methods[] = {
- { NULL, NULL }
+ { NULL, NULL }
};
#define R_OFF(x) offsetof(ReaderObj, x)
static struct PyMemberDef Reader_memberlist[] = {
- { "dialect", T_OBJECT, R_OFF(dialect), READONLY },
- { "line_num", T_ULONG, R_OFF(line_num), READONLY },
- { NULL }
+ { "dialect", T_OBJECT, R_OFF(dialect), READONLY },
+ { "line_num", T_ULONG, R_OFF(line_num), READONLY },
+ { NULL }
};
static PyTypeObject Reader_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_csv.reader", /*tp_name*/
- sizeof(ReaderObj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)Reader_dealloc, /*tp_dealloc*/
- (printfunc)0, /*tp_print*/
- (getattrfunc)0, /*tp_getattr*/
- (setattrfunc)0, /*tp_setattr*/
- 0, /*tp_reserved*/
- (reprfunc)0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- (hashfunc)0, /*tp_hash*/
- (ternaryfunc)0, /*tp_call*/
- (reprfunc)0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_HAVE_GC, /*tp_flags*/
- Reader_Type_doc, /*tp_doc*/
- (traverseproc)Reader_traverse, /*tp_traverse*/
- (inquiry)Reader_clear, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- PyObject_SelfIter, /*tp_iter*/
- (getiterfunc)Reader_iternext, /*tp_iternext*/
- Reader_methods, /*tp_methods*/
- Reader_memberlist, /*tp_members*/
- 0, /*tp_getset*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_csv.reader", /*tp_name*/
+ sizeof(ReaderObj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)Reader_dealloc, /*tp_dealloc*/
+ (printfunc)0, /*tp_print*/
+ (getattrfunc)0, /*tp_getattr*/
+ (setattrfunc)0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ (reprfunc)0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ (hashfunc)0, /*tp_hash*/
+ (ternaryfunc)0, /*tp_call*/
+ (reprfunc)0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+ Reader_Type_doc, /*tp_doc*/
+ (traverseproc)Reader_traverse, /*tp_traverse*/
+ (inquiry)Reader_clear, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ PyObject_SelfIter, /*tp_iter*/
+ (getiterfunc)Reader_iternext, /*tp_iternext*/
+ Reader_methods, /*tp_methods*/
+ Reader_memberlist, /*tp_members*/
+ 0, /*tp_getset*/
};
static PyObject *
csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
{
- PyObject * iterator, * dialect = NULL;
- ReaderObj * self = PyObject_GC_New(ReaderObj, &Reader_Type);
-
- if (!self)
- return NULL;
-
- self->dialect = NULL;
- self->fields = NULL;
- self->input_iter = NULL;
- self->field = NULL;
- self->field_size = 0;
- self->line_num = 0;
-
- if (parse_reset(self) < 0) {
- Py_DECREF(self);
- return NULL;
- }
-
- if (!PyArg_UnpackTuple(args, "", 1, 2, &iterator, &dialect)) {
- Py_DECREF(self);
- return NULL;
- }
- self->input_iter = PyObject_GetIter(iterator);
- if (self->input_iter == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "argument 1 must be an iterator");
- Py_DECREF(self);
- return NULL;
- }
- self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
- if (self->dialect == NULL) {
- Py_DECREF(self);
- return NULL;
- }
-
- PyObject_GC_Track(self);
- return (PyObject *)self;
+ PyObject * iterator, * dialect = NULL;
+ ReaderObj * self = PyObject_GC_New(ReaderObj, &Reader_Type);
+
+ if (!self)
+ return NULL;
+
+ self->dialect = NULL;
+ self->fields = NULL;
+ self->input_iter = NULL;
+ self->field = NULL;
+ self->field_size = 0;
+ self->line_num = 0;
+
+ if (parse_reset(self) < 0) {
+ Py_DECREF(self);
+ return NULL;
+ }
+
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &iterator, &dialect)) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ self->input_iter = PyObject_GetIter(iterator);
+ if (self->input_iter == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "argument 1 must be an iterator");
+ Py_DECREF(self);
+ return NULL;
+ }
+ self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
+ if (self->dialect == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
+
+ PyObject_GC_Track(self);
+ return (PyObject *)self;
}
/*
@@ -939,8 +939,8 @@ csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
static void
join_reset(WriterObj *self)
{
- self->rec_len = 0;
- self->num_fields = 0;
+ self->rec_len = 0;
+ self->num_fields = 0;
}
#define MEM_INCR 32768
@@ -952,90 +952,90 @@ static int
join_append_data(WriterObj *self, Py_UNICODE *field, int quote_empty,
int *quoted, int copy_phase)
{
- DialectObj *dialect = self->dialect;
- int i;
- int rec_len;
- Py_UNICODE *lineterm;
+ DialectObj *dialect = self->dialect;
+ int i;
+ int rec_len;
+ Py_UNICODE *lineterm;
#define ADDCH(c) \
- do {\
- if (copy_phase) \
- self->rec[rec_len] = c;\
- rec_len++;\
- } while(0)
-
- lineterm = PyUnicode_AsUnicode(dialect->lineterminator);
- if (lineterm == NULL)
- return -1;
-
- rec_len = self->rec_len;
-
- /* If this is not the first field we need a field separator */
- if (self->num_fields > 0)
- ADDCH(dialect->delimiter);
-
- /* Handle preceding quote */
- if (copy_phase && *quoted)
- ADDCH(dialect->quotechar);
-
- /* Copy/count field data */
- /* If field is null just pass over */
- for (i = 0; field; i++) {
- Py_UNICODE c = field[i];
- int want_escape = 0;
-
- if (c == '\0')
- break;
-
- if (c == dialect->delimiter ||
- c == dialect->escapechar ||
- c == dialect->quotechar ||
- Py_UNICODE_strchr(lineterm, c)) {
- if (dialect->quoting == QUOTE_NONE)
- want_escape = 1;
- else {
- if (c == dialect->quotechar) {
- if (dialect->doublequote)
- ADDCH(dialect->quotechar);
- else
- want_escape = 1;
- }
- if (!want_escape)
- *quoted = 1;
- }
- if (want_escape) {
- if (!dialect->escapechar) {
- PyErr_Format(error_obj,
- "need to escape, but no escapechar set");
- return -1;
- }
- ADDCH(dialect->escapechar);
- }
- }
- /* Copy field character into record buffer.
- */
- ADDCH(c);
- }
-
- /* If field is empty check if it needs to be quoted.
- */
- if (i == 0 && quote_empty) {
- if (dialect->quoting == QUOTE_NONE) {
- PyErr_Format(error_obj,
- "single empty field record must be quoted");
- return -1;
- }
- else
- *quoted = 1;
- }
-
- if (*quoted) {
- if (copy_phase)
- ADDCH(dialect->quotechar);
- else
- rec_len += 2;
- }
- return rec_len;
+ do {\
+ if (copy_phase) \
+ self->rec[rec_len] = c;\
+ rec_len++;\
+ } while(0)
+
+ lineterm = PyUnicode_AsUnicode(dialect->lineterminator);
+ if (lineterm == NULL)
+ return -1;
+
+ rec_len = self->rec_len;
+
+ /* If this is not the first field we need a field separator */
+ if (self->num_fields > 0)
+ ADDCH(dialect->delimiter);
+
+ /* Handle preceding quote */
+ if (copy_phase && *quoted)
+ ADDCH(dialect->quotechar);
+
+ /* Copy/count field data */
+ /* If field is null just pass over */
+ for (i = 0; field; i++) {
+ Py_UNICODE c = field[i];
+ int want_escape = 0;
+
+ if (c == '\0')
+ break;
+
+ if (c == dialect->delimiter ||
+ c == dialect->escapechar ||
+ c == dialect->quotechar ||
+ Py_UNICODE_strchr(lineterm, c)) {
+ if (dialect->quoting == QUOTE_NONE)
+ want_escape = 1;
+ else {
+ if (c == dialect->quotechar) {
+ if (dialect->doublequote)
+ ADDCH(dialect->quotechar);
+ else
+ want_escape = 1;
+ }
+ if (!want_escape)
+ *quoted = 1;
+ }
+ if (want_escape) {
+ if (!dialect->escapechar) {
+ PyErr_Format(error_obj,
+ "need to escape, but no escapechar set");
+ return -1;
+ }
+ ADDCH(dialect->escapechar);
+ }
+ }
+ /* Copy field character into record buffer.
+ */
+ ADDCH(c);
+ }
+
+ /* If field is empty check if it needs to be quoted.
+ */
+ if (i == 0 && quote_empty) {
+ if (dialect->quoting == QUOTE_NONE) {
+ PyErr_Format(error_obj,
+ "single empty field record must be quoted");
+ return -1;
+ }
+ else
+ *quoted = 1;
+ }
+
+ if (*quoted) {
+ if (copy_phase)
+ ADDCH(dialect->quotechar);
+ else
+ rec_len += 2;
+ }
+ return rec_len;
#undef ADDCH
}
@@ -1043,76 +1043,76 @@ static int
join_check_rec_size(WriterObj *self, int rec_len)
{
- if (rec_len < 0 || rec_len > INT_MAX - MEM_INCR) {
- PyErr_NoMemory();
- return 0;
- }
-
- if (rec_len > self->rec_size) {
- if (self->rec_size == 0) {
- self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
- if (self->rec != NULL)
- PyMem_Free(self->rec);
- self->rec = PyMem_New(Py_UNICODE, self->rec_size);
- }
- else {
- Py_UNICODE* old_rec = self->rec;
-
- self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
- self->rec = PyMem_Resize(self->rec, Py_UNICODE,
- self->rec_size);
- if (self->rec == NULL)
- PyMem_Free(old_rec);
- }
- if (self->rec == NULL) {
- PyErr_NoMemory();
- return 0;
- }
- }
- return 1;
+ if (rec_len < 0 || rec_len > INT_MAX - MEM_INCR) {
+ PyErr_NoMemory();
+ return 0;
+ }
+
+ if (rec_len > self->rec_size) {
+ if (self->rec_size == 0) {
+ self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
+ if (self->rec != NULL)
+ PyMem_Free(self->rec);
+ self->rec = PyMem_New(Py_UNICODE, self->rec_size);
+ }
+ else {
+ Py_UNICODE* old_rec = self->rec;
+
+ self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
+ self->rec = PyMem_Resize(self->rec, Py_UNICODE,
+ self->rec_size);
+ if (self->rec == NULL)
+ PyMem_Free(old_rec);
+ }
+ if (self->rec == NULL) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ }
+ return 1;
}
static int
join_append(WriterObj *self, Py_UNICODE *field, int *quoted, int quote_empty)
{
- int rec_len;
+ int rec_len;
- rec_len = join_append_data(self, field, quote_empty, quoted, 0);
- if (rec_len < 0)
- return 0;
+ rec_len = join_append_data(self, field, quote_empty, quoted, 0);
+ if (rec_len < 0)
+ return 0;
- /* grow record buffer if necessary */
- if (!join_check_rec_size(self, rec_len))
- return 0;
+ /* grow record buffer if necessary */
+ if (!join_check_rec_size(self, rec_len))
+ return 0;
- self->rec_len = join_append_data(self, field, quote_empty, quoted, 1);
- self->num_fields++;
+ self->rec_len = join_append_data(self, field, quote_empty, quoted, 1);
+ self->num_fields++;
- return 1;
+ return 1;
}
static int
join_append_lineterminator(WriterObj *self)
{
- int terminator_len;
- Py_UNICODE *terminator;
+ int terminator_len;
+ Py_UNICODE *terminator;
- terminator_len = PyUnicode_GetSize(self->dialect->lineterminator);
- if (terminator_len == -1)
- return 0;
+ terminator_len = PyUnicode_GetSize(self->dialect->lineterminator);
+ if (terminator_len == -1)
+ return 0;
- /* grow record buffer if necessary */
- if (!join_check_rec_size(self, self->rec_len + terminator_len))
- return 0;
+ /* grow record buffer if necessary */
+ if (!join_check_rec_size(self, self->rec_len + terminator_len))
+ return 0;
- terminator = PyUnicode_AsUnicode(self->dialect->lineterminator);
- if (terminator == NULL)
- return 0;
- memmove(self->rec + self->rec_len, terminator,
- sizeof(Py_UNICODE)*terminator_len);
- self->rec_len += terminator_len;
+ terminator = PyUnicode_AsUnicode(self->dialect->lineterminator);
+ if (terminator == NULL)
+ return 0;
+ memmove(self->rec + self->rec_len, terminator,
+ sizeof(Py_UNICODE)*terminator_len);
+ self->rec_len += terminator_len;
- return 1;
+ return 1;
}
PyDoc_STRVAR(csv_writerow_doc,
@@ -1124,75 +1124,75 @@ PyDoc_STRVAR(csv_writerow_doc,
static PyObject *
csv_writerow(WriterObj *self, PyObject *seq)
{
- DialectObj *dialect = self->dialect;
- int len, i;
-
- if (!PySequence_Check(seq))
- return PyErr_Format(error_obj, "sequence expected");
-
- len = PySequence_Length(seq);
- if (len < 0)
- return NULL;
-
- /* Join all fields in internal buffer.
- */
- join_reset(self);
- for (i = 0; i < len; i++) {
- PyObject *field;
- int append_ok;
- int quoted;
-
- field = PySequence_GetItem(seq, i);
- if (field == NULL)
- return NULL;
-
- switch (dialect->quoting) {
- case QUOTE_NONNUMERIC:
- quoted = !PyNumber_Check(field);
- break;
- case QUOTE_ALL:
- quoted = 1;
- break;
- default:
- quoted = 0;
- break;
- }
-
- if (PyUnicode_Check(field)) {
- append_ok = join_append(self,
- PyUnicode_AS_UNICODE(field),
- &quoted, len == 1);
- Py_DECREF(field);
- }
- else if (field == Py_None) {
- append_ok = join_append(self, NULL,
+ DialectObj *dialect = self->dialect;
+ int len, i;
+
+ if (!PySequence_Check(seq))
+ return PyErr_Format(error_obj, "sequence expected");
+
+ len = PySequence_Length(seq);
+ if (len < 0)
+ return NULL;
+
+ /* Join all fields in internal buffer.
+ */
+ join_reset(self);
+ for (i = 0; i < len; i++) {
+ PyObject *field;
+ int append_ok;
+ int quoted;
+
+ field = PySequence_GetItem(seq, i);
+ if (field == NULL)
+ return NULL;
+
+ switch (dialect->quoting) {
+ case QUOTE_NONNUMERIC:
+ quoted = !PyNumber_Check(field);
+ break;
+ case QUOTE_ALL:
+ quoted = 1;
+ break;
+ default:
+ quoted = 0;
+ break;
+ }
+
+ if (PyUnicode_Check(field)) {
+ append_ok = join_append(self,
+ PyUnicode_AS_UNICODE(field),
&quoted, len == 1);
- Py_DECREF(field);
- }
- else {
- PyObject *str;
-
- str = PyObject_Str(field);
- Py_DECREF(field);
- if (str == NULL)
- return NULL;
- append_ok = join_append(self,
- PyUnicode_AS_UNICODE(str),
- &quoted, len == 1);
- Py_DECREF(str);
- }
- if (!append_ok)
- return NULL;
- }
-
- /* Add line terminator.
- */
- if (!join_append_lineterminator(self))
- return 0;
-
- return PyObject_CallFunction(self->writeline,
- "(u#)", self->rec,
- self->rec_len);
+ Py_DECREF(field);
+ }
+ else if (field == Py_None) {
+ append_ok = join_append(self, NULL,
+ &quoted, len == 1);
+ Py_DECREF(field);
+ }
+ else {
+ PyObject *str;
+
+ str = PyObject_Str(field);
+ Py_DECREF(field);
+ if (str == NULL)
+ return NULL;
+ append_ok = join_append(self,
+ PyUnicode_AS_UNICODE(str),
+ &quoted, len == 1);
+ Py_DECREF(str);
+ }
+ if (!append_ok)
+ return NULL;
+ }
+
+ /* Add line terminator.
+ */
+ if (!join_append_lineterminator(self))
+ return 0;
+
+ return PyObject_CallFunction(self->writeline,
+ "(u#)", self->rec,
+ self->rec_len);
}
PyDoc_STRVAR(csv_writerows_doc,
@@ -1204,72 +1204,72 @@ PyDoc_STRVAR(csv_writerows_doc,
static PyObject *
csv_writerows(WriterObj *self, PyObject *seqseq)
{
- PyObject *row_iter, *row_obj, *result;
-
- row_iter = PyObject_GetIter(seqseq);
- if (row_iter == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "writerows() argument must be iterable");
- return NULL;
- }
- while ((row_obj = PyIter_Next(row_iter))) {
- result = csv_writerow(self, row_obj);
- Py_DECREF(row_obj);
- if (!result) {
- Py_DECREF(row_iter);
- return NULL;
- }
- else
- Py_DECREF(result);
+ PyObject *row_iter, *row_obj, *result;
+
+ row_iter = PyObject_GetIter(seqseq);
+ if (row_iter == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "writerows() argument must be iterable");
+ return NULL;
+ }
+ while ((row_obj = PyIter_Next(row_iter))) {
+ result = csv_writerow(self, row_obj);
+ Py_DECREF(row_obj);
+ if (!result) {
+ Py_DECREF(row_iter);
+ return NULL;
}
- Py_DECREF(row_iter);
- if (PyErr_Occurred())
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ else
+ Py_DECREF(result);
+ }
+ Py_DECREF(row_iter);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static struct PyMethodDef Writer_methods[] = {
- { "writerow", (PyCFunction)csv_writerow, METH_O, csv_writerow_doc},
- { "writerows", (PyCFunction)csv_writerows, METH_O, csv_writerows_doc},
- { NULL, NULL }
+ { "writerow", (PyCFunction)csv_writerow, METH_O, csv_writerow_doc},
+ { "writerows", (PyCFunction)csv_writerows, METH_O, csv_writerows_doc},
+ { NULL, NULL }
};
#define W_OFF(x) offsetof(WriterObj, x)
static struct PyMemberDef Writer_memberlist[] = {
- { "dialect", T_OBJECT, W_OFF(dialect), READONLY },
- { NULL }
+ { "dialect", T_OBJECT, W_OFF(dialect), READONLY },
+ { NULL }
};
static void
Writer_dealloc(WriterObj *self)
{
- PyObject_GC_UnTrack(self);
- Py_XDECREF(self->dialect);
- Py_XDECREF(self->writeline);
- if (self->rec != NULL)
- PyMem_Free(self->rec);
- PyObject_GC_Del(self);
+ PyObject_GC_UnTrack(self);
+ Py_XDECREF(self->dialect);
+ Py_XDECREF(self->writeline);
+ if (self->rec != NULL)
+ PyMem_Free(self->rec);
+ PyObject_GC_Del(self);
}
static int
Writer_traverse(WriterObj *self, visitproc visit, void *arg)
{
- Py_VISIT(self->dialect);
- Py_VISIT(self->writeline);
- return 0;
+ Py_VISIT(self->dialect);
+ Py_VISIT(self->writeline);
+ return 0;
}
static int
Writer_clear(WriterObj *self)
{
- Py_CLEAR(self->dialect);
- Py_CLEAR(self->writeline);
- return 0;
+ Py_CLEAR(self->dialect);
+ Py_CLEAR(self->writeline);
+ return 0;
}
-PyDoc_STRVAR(Writer_Type_doc,
+PyDoc_STRVAR(Writer_Type_doc,
"CSV writer\n"
"\n"
"Writer objects are responsible for generating tabular data\n"
@@ -1277,75 +1277,75 @@ PyDoc_STRVAR(Writer_Type_doc,
);
static PyTypeObject Writer_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_csv.writer", /*tp_name*/
- sizeof(WriterObj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)Writer_dealloc, /*tp_dealloc*/
- (printfunc)0, /*tp_print*/
- (getattrfunc)0, /*tp_getattr*/
- (setattrfunc)0, /*tp_setattr*/
- 0, /*tp_reserved*/
- (reprfunc)0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- (hashfunc)0, /*tp_hash*/
- (ternaryfunc)0, /*tp_call*/
- (reprfunc)0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_HAVE_GC, /*tp_flags*/
- Writer_Type_doc,
- (traverseproc)Writer_traverse, /*tp_traverse*/
- (inquiry)Writer_clear, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- (getiterfunc)0, /*tp_iter*/
- (getiterfunc)0, /*tp_iternext*/
- Writer_methods, /*tp_methods*/
- Writer_memberlist, /*tp_members*/
- 0, /*tp_getset*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_csv.writer", /*tp_name*/
+ sizeof(WriterObj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)Writer_dealloc, /*tp_dealloc*/
+ (printfunc)0, /*tp_print*/
+ (getattrfunc)0, /*tp_getattr*/
+ (setattrfunc)0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ (reprfunc)0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ (hashfunc)0, /*tp_hash*/
+ (ternaryfunc)0, /*tp_call*/
+ (reprfunc)0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+ Writer_Type_doc,
+ (traverseproc)Writer_traverse, /*tp_traverse*/
+ (inquiry)Writer_clear, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ (getiterfunc)0, /*tp_iter*/
+ (getiterfunc)0, /*tp_iternext*/
+ Writer_methods, /*tp_methods*/
+ Writer_memberlist, /*tp_members*/
+ 0, /*tp_getset*/
};
static PyObject *
csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
{
- PyObject * output_file, * dialect = NULL;
- WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
-
- if (!self)
- return NULL;
-
- self->dialect = NULL;
- self->writeline = NULL;
-
- self->rec = NULL;
- self->rec_size = 0;
- self->rec_len = 0;
- self->num_fields = 0;
-
- if (!PyArg_UnpackTuple(args, "", 1, 2, &output_file, &dialect)) {
- Py_DECREF(self);
- return NULL;
- }
- self->writeline = PyObject_GetAttrString(output_file, "write");
- if (self->writeline == NULL || !PyCallable_Check(self->writeline)) {
- PyErr_SetString(PyExc_TypeError,
- "argument 1 must have a \"write\" method");
- Py_DECREF(self);
- return NULL;
- }
- self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
- if (self->dialect == NULL) {
- Py_DECREF(self);
- return NULL;
- }
- PyObject_GC_Track(self);
- return (PyObject *)self;
+ PyObject * output_file, * dialect = NULL;
+ WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
+
+ if (!self)
+ return NULL;
+
+ self->dialect = NULL;
+ self->writeline = NULL;
+
+ self->rec = NULL;
+ self->rec_size = 0;
+ self->rec_len = 0;
+ self->num_fields = 0;
+
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &output_file, &dialect)) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ self->writeline = PyObject_GetAttrString(output_file, "write");
+ if (self->writeline == NULL || !PyCallable_Check(self->writeline)) {
+ PyErr_SetString(PyExc_TypeError,
+ "argument 1 must have a \"write\" method");
+ Py_DECREF(self);
+ return NULL;
+ }
+ self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
+ if (self->dialect == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ PyObject_GC_Track(self);
+ return (PyObject *)self;
}
/*
@@ -1354,70 +1354,70 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
static PyObject *
csv_list_dialects(PyObject *module, PyObject *args)
{
- return PyDict_Keys(dialects);
+ return PyDict_Keys(dialects);
}
static PyObject *
csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
{
- PyObject *name_obj, *dialect_obj = NULL;
- PyObject *dialect;
-
- if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj))
- return NULL;
- if (!IS_BASESTRING(name_obj)) {
- PyErr_SetString(PyExc_TypeError,
- "dialect name must be a string or unicode");
- return NULL;
- }
- dialect = _call_dialect(dialect_obj, kwargs);
- if (dialect == NULL)
- return NULL;
- if (PyDict_SetItem(dialects, name_obj, dialect) < 0) {
- Py_DECREF(dialect);
- return NULL;
- }
- Py_DECREF(dialect);
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *name_obj, *dialect_obj = NULL;
+ PyObject *dialect;
+
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj))
+ return NULL;
+ if (!IS_BASESTRING(name_obj)) {
+ PyErr_SetString(PyExc_TypeError,
+ "dialect name must be a string or unicode");
+ return NULL;
+ }
+ dialect = _call_dialect(dialect_obj, kwargs);
+ if (dialect == NULL)
+ return NULL;
+ if (PyDict_SetItem(dialects, name_obj, dialect) < 0) {
+ Py_DECREF(dialect);
+ return NULL;
+ }
+ Py_DECREF(dialect);
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
csv_unregister_dialect(PyObject *module, PyObject *name_obj)
{
- if (PyDict_DelItem(dialects, name_obj) < 0)
- return PyErr_Format(error_obj, "unknown dialect");
- Py_INCREF(Py_None);
- return Py_None;
+ if (PyDict_DelItem(dialects, name_obj) < 0)
+ return PyErr_Format(error_obj, "unknown dialect");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
csv_get_dialect(PyObject *module, PyObject *name_obj)
{
- return get_dialect_from_registry(name_obj);
+ return get_dialect_from_registry(name_obj);
}
static PyObject *
csv_field_size_limit(PyObject *module, PyObject *args)
{
- PyObject *new_limit = NULL;
- long old_limit = field_limit;
-
- if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
- return NULL;
- if (new_limit != NULL) {
- if (!PyLong_CheckExact(new_limit)) {
- PyErr_Format(PyExc_TypeError,
- "limit must be an integer");
- return NULL;
- }
- field_limit = PyLong_AsLong(new_limit);
- if (field_limit == -1 && PyErr_Occurred()) {
- field_limit = old_limit;
- return NULL;
- }
- }
- return PyLong_FromLong(old_limit);
+ PyObject *new_limit = NULL;
+ long old_limit = field_limit;
+
+ if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
+ return NULL;
+ if (new_limit != NULL) {
+ if (!PyLong_CheckExact(new_limit)) {
+ PyErr_Format(PyExc_TypeError,
+ "limit must be an integer");
+ return NULL;
+ }
+ field_limit = PyLong_AsLong(new_limit);
+ if (field_limit == -1 && PyErr_Occurred()) {
+ field_limit = old_limit;
+ return NULL;
+ }
+ }
+ return PyLong_FromLong(old_limit);
}
/*
@@ -1535,84 +1535,84 @@ PyDoc_STRVAR(csv_field_size_limit_doc,
"the old limit is returned");
static struct PyMethodDef csv_methods[] = {
- { "reader", (PyCFunction)csv_reader,
- METH_VARARGS | METH_KEYWORDS, csv_reader_doc},
- { "writer", (PyCFunction)csv_writer,
- METH_VARARGS | METH_KEYWORDS, csv_writer_doc},
- { "list_dialects", (PyCFunction)csv_list_dialects,
- METH_NOARGS, csv_list_dialects_doc},
- { "register_dialect", (PyCFunction)csv_register_dialect,
- METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc},
- { "unregister_dialect", (PyCFunction)csv_unregister_dialect,
- METH_O, csv_unregister_dialect_doc},
- { "get_dialect", (PyCFunction)csv_get_dialect,
- METH_O, csv_get_dialect_doc},
- { "field_size_limit", (PyCFunction)csv_field_size_limit,
- METH_VARARGS, csv_field_size_limit_doc},
- { NULL, NULL }
+ { "reader", (PyCFunction)csv_reader,
+ METH_VARARGS | METH_KEYWORDS, csv_reader_doc},
+ { "writer", (PyCFunction)csv_writer,
+ METH_VARARGS | METH_KEYWORDS, csv_writer_doc},
+ { "list_dialects", (PyCFunction)csv_list_dialects,
+ METH_NOARGS, csv_list_dialects_doc},
+ { "register_dialect", (PyCFunction)csv_register_dialect,
+ METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc},
+ { "unregister_dialect", (PyCFunction)csv_unregister_dialect,
+ METH_O, csv_unregister_dialect_doc},
+ { "get_dialect", (PyCFunction)csv_get_dialect,
+ METH_O, csv_get_dialect_doc},
+ { "field_size_limit", (PyCFunction)csv_field_size_limit,
+ METH_VARARGS, csv_field_size_limit_doc},
+ { NULL, NULL }
};
static struct PyModuleDef _csvmodule = {
- PyModuleDef_HEAD_INIT,
- "_csv",
- csv_module_doc,
- -1,
- csv_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_csv",
+ csv_module_doc,
+ -1,
+ csv_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__csv(void)
{
- PyObject *module;
- StyleDesc *style;
-
- if (PyType_Ready(&Dialect_Type) < 0)
- return NULL;
-
- if (PyType_Ready(&Reader_Type) < 0)
- return NULL;
-
- if (PyType_Ready(&Writer_Type) < 0)
- return NULL;
-
- /* Create the module and add the functions */
- module = PyModule_Create(&_csvmodule);
- if (module == NULL)
- return NULL;
-
- /* Add version to the module. */
- if (PyModule_AddStringConstant(module, "__version__",
- MODULE_VERSION) == -1)
- return NULL;
-
- /* Add _dialects dictionary */
- dialects = PyDict_New();
- if (dialects == NULL)
- return NULL;
- if (PyModule_AddObject(module, "_dialects", dialects))
- return NULL;
-
- /* Add quote styles into dictionary */
- for (style = quote_styles; style->name; style++) {
- if (PyModule_AddIntConstant(module, style->name,
- style->style) == -1)
- return NULL;
- }
-
- /* Add the Dialect type */
- Py_INCREF(&Dialect_Type);
- if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
- return NULL;
-
- /* Add the CSV exception object to the module. */
- error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
- if (error_obj == NULL)
- return NULL;
- PyModule_AddObject(module, "Error", error_obj);
- return module;
+ PyObject *module;
+ StyleDesc *style;
+
+ if (PyType_Ready(&Dialect_Type) < 0)
+ return NULL;
+
+ if (PyType_Ready(&Reader_Type) < 0)
+ return NULL;
+
+ if (PyType_Ready(&Writer_Type) < 0)
+ return NULL;
+
+ /* Create the module and add the functions */
+ module = PyModule_Create(&_csvmodule);
+ if (module == NULL)
+ return NULL;
+
+ /* Add version to the module. */
+ if (PyModule_AddStringConstant(module, "__version__",
+ MODULE_VERSION) == -1)
+ return NULL;
+
+ /* Add _dialects dictionary */
+ dialects = PyDict_New();
+ if (dialects == NULL)
+ return NULL;
+ if (PyModule_AddObject(module, "_dialects", dialects))
+ return NULL;
+
+ /* Add quote styles into dictionary */
+ for (style = quote_styles; style->name; style++) {
+ if (PyModule_AddIntConstant(module, style->name,
+ style->style) == -1)
+ return NULL;
+ }
+
+ /* Add the Dialect type */
+ Py_INCREF(&Dialect_Type);
+ if (PyModule_AddObject(module, "Dialect", (PyObject *)&Dialect_Type))
+ return NULL;
+
+ /* Add the CSV exception object to the module. */
+ error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
+ if (error_obj == NULL)
+ return NULL;
+ PyModule_AddObject(module, "Error", error_obj);
+ return module;
}
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 21961ae..c1b3093 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -20,20 +20,20 @@
/*
-Name methods, members, getsets
+Name methods, members, getsets
==============================================================================
-PyCStructType_Type __new__(), from_address(), __mul__(), from_param()
-UnionType_Type __new__(), from_address(), __mul__(), from_param()
-PyCPointerType_Type __new__(), from_address(), __mul__(), from_param(), set_type()
-PyCArrayType_Type __new__(), from_address(), __mul__(), from_param()
-PyCSimpleType_Type __new__(), from_address(), __mul__(), from_param()
+PyCStructType_Type __new__(), from_address(), __mul__(), from_param()
+UnionType_Type __new__(), from_address(), __mul__(), from_param()
+PyCPointerType_Type __new__(), from_address(), __mul__(), from_param(), set_type()
+PyCArrayType_Type __new__(), from_address(), __mul__(), from_param()
+PyCSimpleType_Type __new__(), from_address(), __mul__(), from_param()
PyCData_Type
- Struct_Type __new__(), __init__()
- PyCPointer_Type __new__(), __init__(), _as_parameter_, contents
- PyCArray_Type __new__(), __init__(), _as_parameter_, __get/setitem__(), __len__()
- Simple_Type __new__(), __init__(), _as_parameter_
+ Struct_Type __new__(), __init__()
+ PyCPointer_Type __new__(), __init__(), _as_parameter_, contents
+ PyCArray_Type __new__(), __init__(), _as_parameter_, __get/setitem__(), __len__()
+ Simple_Type __new__(), __init__(), _as_parameter_
PyCField_Type
PyCStgDict_Type
@@ -45,28 +45,28 @@ class methods
It has some similarity to the byref() construct compared to pointer()
from_address(addr)
- - construct an instance from a given memory block (sharing this memory block)
+ - construct an instance from a given memory block (sharing this memory block)
from_param(obj)
- - typecheck and convert a Python object into a C function call parameter
- the result may be an instance of the type, or an integer or tuple
- (typecode, value[, obj])
+ - typecheck and convert a Python object into a C function call parameter
+ the result may be an instance of the type, or an integer or tuple
+ (typecode, value[, obj])
instance methods/properties
---------------------------
_as_parameter_
- - convert self into a C function call parameter
- This is either an integer, or a 3-tuple (typecode, value, obj)
+ - convert self into a C function call parameter
+ This is either an integer, or a 3-tuple (typecode, value, obj)
functions
---------
sizeof(cdata)
- - return the number of bytes the buffer contains
+ - return the number of bytes the buffer contains
sizeof(ctype)
- - return the number of bytes the buffer of an instance would contain
+ - return the number of bytes the buffer of an instance would contain
byref(cdata)
@@ -77,7 +77,7 @@ pointer(cdata)
POINTER(ctype)
bytes(cdata)
- - return the buffer contents as a sequence of bytes (which is currently a string)
+ - return the buffer contents as a sequence of bytes (which is currently a string)
*/
@@ -98,7 +98,7 @@ bytes(cdata)
* PyCField_Type
*
*/
-
+
#define PY_SSIZE_T_CLEAN
#include "Python.h"
@@ -135,128 +135,128 @@ static PyObject *_unpickle;
char *_ctypes_conversion_encoding = NULL;
char *_ctypes_conversion_errors = NULL;
-
+
/****************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *key;
- PyObject *dict;
+ PyObject_HEAD
+ PyObject *key;
+ PyObject *dict;
} DictRemoverObject;
static void
_DictRemover_dealloc(PyObject *_self)
{
- DictRemoverObject *self = (DictRemoverObject *)_self;
- Py_XDECREF(self->key);
- Py_XDECREF(self->dict);
- Py_TYPE(self)->tp_free(_self);
+ DictRemoverObject *self = (DictRemoverObject *)_self;
+ Py_XDECREF(self->key);
+ Py_XDECREF(self->dict);
+ Py_TYPE(self)->tp_free(_self);
}
static PyObject *
_DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw)
{
- DictRemoverObject *self = (DictRemoverObject *)_self;
- if (self->key && self->dict) {
- if (-1 == PyDict_DelItem(self->dict, self->key))
- /* XXX Error context */
- PyErr_WriteUnraisable(Py_None);
- Py_DECREF(self->key);
- self->key = NULL;
- Py_DECREF(self->dict);
- self->dict = NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ DictRemoverObject *self = (DictRemoverObject *)_self;
+ if (self->key && self->dict) {
+ if (-1 == PyDict_DelItem(self->dict, self->key))
+ /* XXX Error context */
+ PyErr_WriteUnraisable(Py_None);
+ Py_DECREF(self->key);
+ self->key = NULL;
+ Py_DECREF(self->dict);
+ self->dict = NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyTypeObject DictRemover_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.DictRemover", /* tp_name */
- sizeof(DictRemoverObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- _DictRemover_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- _DictRemover_call, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.DictRemover", /* tp_name */
+ sizeof(DictRemoverObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ _DictRemover_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ _DictRemover_call, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
/* XXX should participate in GC? */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- "deletes a key from a dictionary", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- 0, /* tp_free */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ "deletes a key from a dictionary", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
};
int
PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
{
- PyObject *obj;
- DictRemoverObject *remover;
- PyObject *proxy;
- int result;
-
- obj = PyObject_CallObject((PyObject *)&DictRemover_Type, NULL);
- if (obj == NULL)
- return -1;
-
- remover = (DictRemoverObject *)obj;
- assert(remover->key == NULL);
- assert(remover->dict == NULL);
- Py_INCREF(key);
- remover->key = key;
- Py_INCREF(dict);
- remover->dict = dict;
-
- proxy = PyWeakref_NewProxy(item, obj);
- Py_DECREF(obj);
- if (proxy == NULL)
- return -1;
-
- result = PyDict_SetItem(dict, key, proxy);
- Py_DECREF(proxy);
- return result;
+ PyObject *obj;
+ DictRemoverObject *remover;
+ PyObject *proxy;
+ int result;
+
+ obj = PyObject_CallObject((PyObject *)&DictRemover_Type, NULL);
+ if (obj == NULL)
+ return -1;
+
+ remover = (DictRemoverObject *)obj;
+ assert(remover->key == NULL);
+ assert(remover->dict == NULL);
+ Py_INCREF(key);
+ remover->key = key;
+ Py_INCREF(dict);
+ remover->dict = dict;
+
+ proxy = PyWeakref_NewProxy(item, obj);
+ Py_DECREF(obj);
+ if (proxy == NULL)
+ return -1;
+
+ result = PyDict_SetItem(dict, key, proxy);
+ Py_DECREF(proxy);
+ return result;
}
PyObject *
PyDict_GetItemProxy(PyObject *dict, PyObject *key)
{
- PyObject *result;
- PyObject *item = PyDict_GetItem(dict, key);
-
- if (item == NULL)
- return NULL;
- if (!PyWeakref_CheckProxy(item))
- return item;
- result = PyWeakref_GET_OBJECT(item);
- if (result == Py_None)
- return NULL;
- return result;
+ PyObject *result;
+ PyObject *item = PyDict_GetItem(dict, key);
+
+ if (item == NULL)
+ return NULL;
+ if (!PyWeakref_CheckProxy(item))
+ return item;
+ result = PyWeakref_GET_OBJECT(item);
+ if (result == Py_None)
+ return NULL;
+ return result;
}
/******************************************************************/
@@ -269,25 +269,25 @@ PyDict_GetItemProxy(PyObject *dict, PyObject *key)
char *
_ctypes_alloc_format_string(const char *prefix, const char *suffix)
{
- size_t len;
- char *result;
-
- if (suffix == NULL) {
- assert(PyErr_Occurred());
- return NULL;
- }
- len = strlen(suffix);
- if (prefix)
- len += strlen(prefix);
- result = PyMem_Malloc(len + 1);
- if (result == NULL)
- return NULL;
- if (prefix)
- strcpy(result, prefix);
- else
- result[0] = '\0';
- strcat(result, suffix);
- return result;
+ size_t len;
+ char *result;
+
+ if (suffix == NULL) {
+ assert(PyErr_Occurred());
+ return NULL;
+ }
+ len = strlen(suffix);
+ if (prefix)
+ len += strlen(prefix);
+ result = PyMem_Malloc(len + 1);
+ if (result == NULL)
+ return NULL;
+ if (prefix)
+ strcpy(result, prefix);
+ else
+ result[0] = '\0';
+ strcat(result, suffix);
+ return result;
}
/*
@@ -300,100 +300,100 @@ _ctypes_alloc_format_string(const char *prefix, const char *suffix)
static PyCArgObject *
StructUnionType_paramfunc(CDataObject *self)
{
- PyCArgObject *parg;
- StgDictObject *stgdict;
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = 'V';
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for structure/union instances */
- parg->pffi_type = &stgdict->ffi_type_pointer;
- /* For structure parameters (by value), parg->value doesn't contain the structure
- data itself, instead parg->value.p *points* to the structure's data
- See also _ctypes.c, function _call_function_pointer().
- */
- parg->value.p = self->b_ptr;
- parg->size = self->b_size;
- Py_INCREF(self);
- parg->obj = (PyObject *)self;
- return parg;
+ PyCArgObject *parg;
+ StgDictObject *stgdict;
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = 'V';
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for structure/union instances */
+ parg->pffi_type = &stgdict->ffi_type_pointer;
+ /* For structure parameters (by value), parg->value doesn't contain the structure
+ data itself, instead parg->value.p *points* to the structure's data
+ See also _ctypes.c, function _call_function_pointer().
+ */
+ parg->value.p = self->b_ptr;
+ parg->size = self->b_size;
+ Py_INCREF(self);
+ parg->obj = (PyObject *)self;
+ return parg;
}
static PyObject *
StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct)
{
- PyTypeObject *result;
- PyObject *fields;
- StgDictObject *dict;
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
- if (!result)
- return NULL;
-
- /* keep this for bw compatibility */
- if (PyDict_GetItemString(result->tp_dict, "_abstract_"))
- return (PyObject *)result;
-
- dict = (StgDictObject *)PyObject_CallObject((PyObject *)&PyCStgDict_Type, NULL);
- if (!dict) {
- Py_DECREF(result);
- return NULL;
- }
- /* replace the class dict by our updated stgdict, which holds info
- about storage requirements of the instances */
- if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)dict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)dict;
- dict->format = _ctypes_alloc_format_string(NULL, "B");
- if (dict->format == NULL) {
- Py_DECREF(result);
- return NULL;
- }
-
- dict->paramfunc = StructUnionType_paramfunc;
-
- fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
- if (!fields) {
- StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
-
- if (basedict == NULL)
- return (PyObject *)result;
- /* copy base dict */
- if (-1 == PyCStgDict_clone(dict, basedict)) {
- Py_DECREF(result);
- return NULL;
- }
- dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */
- basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */
- return (PyObject *)result;
- }
-
- if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
+ PyTypeObject *result;
+ PyObject *fields;
+ StgDictObject *dict;
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
+ if (!result)
+ return NULL;
+
+ /* keep this for bw compatibility */
+ if (PyDict_GetItemString(result->tp_dict, "_abstract_"))
+ return (PyObject *)result;
+
+ dict = (StgDictObject *)PyObject_CallObject((PyObject *)&PyCStgDict_Type, NULL);
+ if (!dict) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ /* replace the class dict by our updated stgdict, which holds info
+ about storage requirements of the instances */
+ if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)dict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)dict;
+ dict->format = _ctypes_alloc_format_string(NULL, "B");
+ if (dict->format == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ dict->paramfunc = StructUnionType_paramfunc;
+
+ fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
+ if (!fields) {
+ StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
+
+ if (basedict == NULL)
+ return (PyObject *)result;
+ /* copy base dict */
+ if (-1 == PyCStgDict_clone(dict, basedict)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */
+ basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */
+ return (PyObject *)result;
+ }
+
+ if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ return (PyObject *)result;
}
static PyObject *
PyCStructType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- return StructUnionType_new(type, args, kwds, 1);
+ return StructUnionType_new(type, args, kwds, 1);
}
static PyObject *
UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- return StructUnionType_new(type, args, kwds, 0);
+ return StructUnionType_new(type, args, kwds, 0);
}
static char from_address_doc[] =
@@ -402,16 +402,16 @@ static char from_address_doc[] =
static PyObject *
CDataType_from_address(PyObject *type, PyObject *value)
{
- void *buf;
- if (!PyLong_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "integer expected");
- return NULL;
- }
- buf = (void *)PyLong_AsVoidPtr(value);
- if (PyErr_Occurred())
- return NULL;
- return PyCData_AtAddress(type, buf);
+ void *buf;
+ if (!PyLong_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "integer expected");
+ return NULL;
+ }
+ buf = (void *)PyLong_AsVoidPtr(value);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyCData_AtAddress(type, buf);
}
static char from_buffer_doc[] =
@@ -423,51 +423,51 @@ KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep);
static PyObject *
CDataType_from_buffer(PyObject *type, PyObject *args)
{
- void *buffer;
- Py_ssize_t buffer_len;
- Py_ssize_t offset = 0;
- PyObject *obj, *result;
- StgDictObject *dict = PyType_stgdict(type);
- assert (dict);
-
- if (!PyArg_ParseTuple(args,
+ void *buffer;
+ Py_ssize_t buffer_len;
+ Py_ssize_t offset = 0;
+ PyObject *obj, *result;
+ StgDictObject *dict = PyType_stgdict(type);
+ assert (dict);
+
+ if (!PyArg_ParseTuple(args,
#if (PY_VERSION_HEX < 0x02050000)
- "O|i:from_buffer",
+ "O|i:from_buffer",
#else
- "O|n:from_buffer",
+ "O|n:from_buffer",
#endif
- &obj, &offset))
- return NULL;
-
- if (-1 == PyObject_AsWriteBuffer(obj, &buffer, &buffer_len))
- return NULL;
-
- if (offset < 0) {
- PyErr_SetString(PyExc_ValueError,
- "offset cannot be negative");
- return NULL;
- }
- if (dict->size > buffer_len - offset) {
- PyErr_Format(PyExc_ValueError,
+ &obj, &offset))
+ return NULL;
+
+ if (-1 == PyObject_AsWriteBuffer(obj, &buffer, &buffer_len))
+ return NULL;
+
+ if (offset < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "offset cannot be negative");
+ return NULL;
+ }
+ if (dict->size > buffer_len - offset) {
+ PyErr_Format(PyExc_ValueError,
#if (PY_VERSION_HEX < 0x02050000)
- "Buffer size too small (%d instead of at least %d bytes)",
+ "Buffer size too small (%d instead of at least %d bytes)",
#else
- "Buffer size too small (%zd instead of at least %zd bytes)",
+ "Buffer size too small (%zd instead of at least %zd bytes)",
#endif
- buffer_len, dict->size + offset);
- return NULL;
- }
-
- result = PyCData_AtAddress(type, (char *)buffer + offset);
- if (result == NULL)
- return NULL;
-
- Py_INCREF(obj);
- if (-1 == KeepRef((CDataObject *)result, -1, obj)) {
- Py_DECREF(result);
- return NULL;
- }
- return result;
+ buffer_len, dict->size + offset);
+ return NULL;
+ }
+
+ result = PyCData_AtAddress(type, (char *)buffer + offset);
+ if (result == NULL)
+ return NULL;
+
+ Py_INCREF(obj);
+ if (-1 == KeepRef((CDataObject *)result, -1, obj)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ return result;
}
static char from_buffer_copy_doc[] =
@@ -479,48 +479,48 @@ GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
CDataType_from_buffer_copy(PyObject *type, PyObject *args)
{
- const void *buffer;
- Py_ssize_t buffer_len;
- Py_ssize_t offset = 0;
- PyObject *obj, *result;
- StgDictObject *dict = PyType_stgdict(type);
- assert (dict);
-
- if (!PyArg_ParseTuple(args,
+ const void *buffer;
+ Py_ssize_t buffer_len;
+ Py_ssize_t offset = 0;
+ PyObject *obj, *result;
+ StgDictObject *dict = PyType_stgdict(type);
+ assert (dict);
+
+ if (!PyArg_ParseTuple(args,
#if (PY_VERSION_HEX < 0x02050000)
- "O|i:from_buffer",
+ "O|i:from_buffer",
#else
- "O|n:from_buffer",
+ "O|n:from_buffer",
#endif
- &obj, &offset))
- return NULL;
+ &obj, &offset))
+ return NULL;
- if (-1 == PyObject_AsReadBuffer(obj, (const void**)&buffer, &buffer_len))
- return NULL;
+ if (-1 == PyObject_AsReadBuffer(obj, (const void**)&buffer, &buffer_len))
+ return NULL;
- if (offset < 0) {
- PyErr_SetString(PyExc_ValueError,
- "offset cannot be negative");
- return NULL;
- }
+ if (offset < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "offset cannot be negative");
+ return NULL;
+ }
- if (dict->size > buffer_len - offset) {
- PyErr_Format(PyExc_ValueError,
+ if (dict->size > buffer_len - offset) {
+ PyErr_Format(PyExc_ValueError,
#if (PY_VERSION_HEX < 0x02050000)
- "Buffer size too small (%d instead of at least %d bytes)",
+ "Buffer size too small (%d instead of at least %d bytes)",
#else
- "Buffer size too small (%zd instead of at least %zd bytes)",
+ "Buffer size too small (%zd instead of at least %zd bytes)",
#endif
- buffer_len, dict->size + offset);
- return NULL;
- }
-
- result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL);
- if (result == NULL)
- return NULL;
- memcpy(((CDataObject *)result)->b_ptr,
- (char *)buffer+offset, dict->size);
- return result;
+ buffer_len, dict->size + offset);
+ return NULL;
+ }
+
+ result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL);
+ if (result == NULL)
+ return NULL;
+ memcpy(((CDataObject *)result)->b_ptr,
+ (char *)buffer+offset, dict->size);
+ return result;
}
static char in_dll_doc[] =
@@ -529,55 +529,55 @@ static char in_dll_doc[] =
static PyObject *
CDataType_in_dll(PyObject *type, PyObject *args)
{
- PyObject *dll;
- char *name;
- PyObject *obj;
- void *handle;
- void *address;
-
- if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name))
- return NULL;
-
- obj = PyObject_GetAttrString(dll, "_handle");
- if (!obj)
- return NULL;
- if (!PyLong_Check(obj)) {
- PyErr_SetString(PyExc_TypeError,
- "the _handle attribute of the second argument must be an integer");
- Py_DECREF(obj);
- return NULL;
- }
- handle = (void *)PyLong_AsVoidPtr(obj);
- Py_DECREF(obj);
- if (PyErr_Occurred()) {
- PyErr_SetString(PyExc_ValueError,
- "could not convert the _handle attribute to a pointer");
- return NULL;
- }
+ PyObject *dll;
+ char *name;
+ PyObject *obj;
+ void *handle;
+ void *address;
+
+ if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name))
+ return NULL;
+
+ obj = PyObject_GetAttrString(dll, "_handle");
+ if (!obj)
+ return NULL;
+ if (!PyLong_Check(obj)) {
+ PyErr_SetString(PyExc_TypeError,
+ "the _handle attribute of the second argument must be an integer");
+ Py_DECREF(obj);
+ return NULL;
+ }
+ handle = (void *)PyLong_AsVoidPtr(obj);
+ Py_DECREF(obj);
+ if (PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "could not convert the _handle attribute to a pointer");
+ return NULL;
+ }
#ifdef MS_WIN32
- address = (void *)GetProcAddress(handle, name);
- if (!address) {
- PyErr_Format(PyExc_ValueError,
- "symbol '%s' not found",
- name);
- return NULL;
- }
+ address = (void *)GetProcAddress(handle, name);
+ if (!address) {
+ PyErr_Format(PyExc_ValueError,
+ "symbol '%s' not found",
+ name);
+ return NULL;
+ }
#else
- address = (void *)ctypes_dlsym(handle, name);
- if (!address) {
+ address = (void *)ctypes_dlsym(handle, name);
+ if (!address) {
#ifdef __CYGWIN__
/* dlerror() isn't very helpful on cygwin */
- PyErr_Format(PyExc_ValueError,
- "symbol '%s' not found (%s) ",
- name);
+ PyErr_Format(PyExc_ValueError,
+ "symbol '%s' not found (%s) ",
+ name);
#else
- PyErr_SetString(PyExc_ValueError, ctypes_dlerror());
+ PyErr_SetString(PyExc_ValueError, ctypes_dlerror());
#endif
- return NULL;
- }
+ return NULL;
+ }
#endif
- return PyCData_AtAddress(type, address);
+ return PyCData_AtAddress(type, address);
}
static char from_param_doc[] =
@@ -586,210 +586,210 @@ static char from_param_doc[] =
static PyObject *
CDataType_from_param(PyObject *type, PyObject *value)
{
- PyObject *as_parameter;
- if (1 == PyObject_IsInstance(value, type)) {
- Py_INCREF(value);
- return value;
- }
- if (PyCArg_CheckExact(value)) {
- PyCArgObject *p = (PyCArgObject *)value;
- PyObject *ob = p->obj;
- const char *ob_name;
- StgDictObject *dict;
- dict = PyType_stgdict(type);
-
- /* If we got a PyCArgObject, we must check if the object packed in it
- is an instance of the type's dict->proto */
- if(dict && ob
- && PyObject_IsInstance(ob, dict->proto)) {
- Py_INCREF(value);
- return value;
- }
- ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
- PyErr_Format(PyExc_TypeError,
- "expected %s instance instead of pointer to %s",
- ((PyTypeObject *)type)->tp_name, ob_name);
- return NULL;
- }
-
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
- if (as_parameter) {
- value = CDataType_from_param(type, as_parameter);
- Py_DECREF(as_parameter);
- return value;
- }
- PyErr_Format(PyExc_TypeError,
- "expected %s instance instead of %s",
- ((PyTypeObject *)type)->tp_name,
- Py_TYPE(value)->tp_name);
- return NULL;
+ PyObject *as_parameter;
+ if (1 == PyObject_IsInstance(value, type)) {
+ Py_INCREF(value);
+ return value;
+ }
+ if (PyCArg_CheckExact(value)) {
+ PyCArgObject *p = (PyCArgObject *)value;
+ PyObject *ob = p->obj;
+ const char *ob_name;
+ StgDictObject *dict;
+ dict = PyType_stgdict(type);
+
+ /* If we got a PyCArgObject, we must check if the object packed in it
+ is an instance of the type's dict->proto */
+ if(dict && ob
+ && PyObject_IsInstance(ob, dict->proto)) {
+ Py_INCREF(value);
+ return value;
+ }
+ ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
+ PyErr_Format(PyExc_TypeError,
+ "expected %s instance instead of pointer to %s",
+ ((PyTypeObject *)type)->tp_name, ob_name);
+ return NULL;
+ }
+
+ as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (as_parameter) {
+ value = CDataType_from_param(type, as_parameter);
+ Py_DECREF(as_parameter);
+ return value;
+ }
+ PyErr_Format(PyExc_TypeError,
+ "expected %s instance instead of %s",
+ ((PyTypeObject *)type)->tp_name,
+ Py_TYPE(value)->tp_name);
+ return NULL;
}
static PyMethodDef CDataType_methods[] = {
- { "from_param", CDataType_from_param, METH_O, from_param_doc },
- { "from_address", CDataType_from_address, METH_O, from_address_doc },
- { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
- { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
- { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc },
- { NULL, NULL },
+ { "from_param", CDataType_from_param, METH_O, from_param_doc },
+ { "from_address", CDataType_from_address, METH_O, from_address_doc },
+ { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
+ { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
+ { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc },
+ { NULL, NULL },
};
static PyObject *
CDataType_repeat(PyObject *self, Py_ssize_t length)
{
- if (length < 0)
- return PyErr_Format(PyExc_ValueError,
- "Array length must be >= 0, not %zd",
- length);
- return PyCArrayType_from_ctype(self, length);
+ if (length < 0)
+ return PyErr_Format(PyExc_ValueError,
+ "Array length must be >= 0, not %zd",
+ length);
+ return PyCArrayType_from_ctype(self, length);
}
static PySequenceMethods CDataType_as_sequence = {
- 0, /* inquiry sq_length; */
- 0, /* binaryfunc sq_concat; */
- CDataType_repeat, /* intargfunc sq_repeat; */
- 0, /* intargfunc sq_item; */
- 0, /* intintargfunc sq_slice; */
- 0, /* intobjargproc sq_ass_item; */
- 0, /* intintobjargproc sq_ass_slice; */
- 0, /* objobjproc sq_contains; */
-
- 0, /* binaryfunc sq_inplace_concat; */
- 0, /* intargfunc sq_inplace_repeat; */
+ 0, /* inquiry sq_length; */
+ 0, /* binaryfunc sq_concat; */
+ CDataType_repeat, /* intargfunc sq_repeat; */
+ 0, /* intargfunc sq_item; */
+ 0, /* intintargfunc sq_slice; */
+ 0, /* intobjargproc sq_ass_item; */
+ 0, /* intintobjargproc sq_ass_slice; */
+ 0, /* objobjproc sq_contains; */
+
+ 0, /* binaryfunc sq_inplace_concat; */
+ 0, /* intargfunc sq_inplace_repeat; */
};
static int
CDataType_clear(PyTypeObject *self)
{
- StgDictObject *dict = PyType_stgdict((PyObject *)self);
- if (dict)
- Py_CLEAR(dict->proto);
- return PyType_Type.tp_clear((PyObject *)self);
+ StgDictObject *dict = PyType_stgdict((PyObject *)self);
+ if (dict)
+ Py_CLEAR(dict->proto);
+ return PyType_Type.tp_clear((PyObject *)self);
}
static int
CDataType_traverse(PyTypeObject *self, visitproc visit, void *arg)
{
- StgDictObject *dict = PyType_stgdict((PyObject *)self);
- if (dict)
- Py_VISIT(dict->proto);
- return PyType_Type.tp_traverse((PyObject *)self, visit, arg);
+ StgDictObject *dict = PyType_stgdict((PyObject *)self);
+ if (dict)
+ Py_VISIT(dict->proto);
+ return PyType_Type.tp_traverse((PyObject *)self, visit, arg);
}
static int
PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
{
- /* XXX Should we disallow deleting _fields_? */
- if (-1 == PyType_Type.tp_setattro(self, key, value))
- return -1;
-
- if (value && PyUnicode_Check(key) &&
- /* XXX struni _PyUnicode_AsString can fail (also in other places)! */
- 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
- return PyCStructUnionType_update_stgdict(self, value, 1);
- return 0;
+ /* XXX Should we disallow deleting _fields_? */
+ if (-1 == PyType_Type.tp_setattro(self, key, value))
+ return -1;
+
+ if (value && PyUnicode_Check(key) &&
+ /* XXX struni _PyUnicode_AsString can fail (also in other places)! */
+ 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
+ return PyCStructUnionType_update_stgdict(self, value, 1);
+ return 0;
}
static int
UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
{
- /* XXX Should we disallow deleting _fields_? */
- if (-1 == PyObject_GenericSetAttr(self, key, value))
- return -1;
-
- if (PyUnicode_Check(key) &&
- 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
- return PyCStructUnionType_update_stgdict(self, value, 0);
- return 0;
+ /* XXX Should we disallow deleting _fields_? */
+ if (-1 == PyObject_GenericSetAttr(self, key, value))
+ return -1;
+
+ if (PyUnicode_Check(key) &&
+ 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
+ return PyCStructUnionType_update_stgdict(self, value, 0);
+ return 0;
}
PyTypeObject PyCStructType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCStructType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- PyCStructType_setattro, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "metatype for the CData Objects", /* tp_doc */
- (traverseproc)CDataType_traverse, /* tp_traverse */
- (inquiry)CDataType_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- CDataType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCStructType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCStructType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ PyCStructType_setattro, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "metatype for the CData Objects", /* tp_doc */
+ (traverseproc)CDataType_traverse, /* tp_traverse */
+ (inquiry)CDataType_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ CDataType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCStructType_new, /* tp_new */
+ 0, /* tp_free */
};
static PyTypeObject UnionType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.UnionType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- UnionType_setattro, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "metatype for the CData Objects", /* tp_doc */
- (traverseproc)CDataType_traverse, /* tp_traverse */
- (inquiry)CDataType_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- CDataType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- UnionType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.UnionType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ UnionType_setattro, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "metatype for the CData Objects", /* tp_doc */
+ (traverseproc)CDataType_traverse, /* tp_traverse */
+ (inquiry)CDataType_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ CDataType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ UnionType_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
@@ -809,124 +809,124 @@ size property/method, and the sequence protocol.
static int
PyCPointerType_SetProto(StgDictObject *stgdict, PyObject *proto)
{
- if (!proto || !PyType_Check(proto)) {
- PyErr_SetString(PyExc_TypeError,
- "_type_ must be a type");
- return -1;
- }
- if (!PyType_stgdict(proto)) {
- PyErr_SetString(PyExc_TypeError,
- "_type_ must have storage info");
- return -1;
- }
- Py_INCREF(proto);
- Py_XDECREF(stgdict->proto);
- stgdict->proto = proto;
- return 0;
+ if (!proto || !PyType_Check(proto)) {
+ PyErr_SetString(PyExc_TypeError,
+ "_type_ must be a type");
+ return -1;
+ }
+ if (!PyType_stgdict(proto)) {
+ PyErr_SetString(PyExc_TypeError,
+ "_type_ must have storage info");
+ return -1;
+ }
+ Py_INCREF(proto);
+ Py_XDECREF(stgdict->proto);
+ stgdict->proto = proto;
+ return 0;
}
static PyCArgObject *
PyCPointerType_paramfunc(CDataObject *self)
{
- PyCArgObject *parg;
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = 'P';
- parg->pffi_type = &ffi_type_pointer;
- Py_INCREF(self);
- parg->obj = (PyObject *)self;
- parg->value.p = *(void **)self->b_ptr;
- return parg;
+ PyCArgObject *parg;
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = 'P';
+ parg->pffi_type = &ffi_type_pointer;
+ Py_INCREF(self);
+ parg->obj = (PyObject *)self;
+ parg->value.p = *(void **)self->b_ptr;
+ return parg;
}
static PyObject *
PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyTypeObject *result;
- StgDictObject *stgdict;
- PyObject *proto;
- PyObject *typedict;
-
- typedict = PyTuple_GetItem(args, 2);
- if (!typedict)
- return NULL;
+ PyTypeObject *result;
+ StgDictObject *stgdict;
+ PyObject *proto;
+ PyObject *typedict;
+
+ typedict = PyTuple_GetItem(args, 2);
+ if (!typedict)
+ return NULL;
/*
stgdict items size, align, length contain info about pointers itself,
stgdict->proto has info about the pointed to type!
*/
- stgdict = (StgDictObject *)PyObject_CallObject(
- (PyObject *)&PyCStgDict_Type, NULL);
- if (!stgdict)
- return NULL;
- stgdict->size = sizeof(void *);
- stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
- stgdict->length = 1;
- stgdict->ffi_type_pointer = ffi_type_pointer;
- stgdict->paramfunc = PyCPointerType_paramfunc;
- stgdict->flags |= TYPEFLAG_ISPOINTER;
-
- proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
- if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
- if (proto) {
- StgDictObject *itemdict = PyType_stgdict(proto);
- assert(itemdict);
- /* If itemdict->format is NULL, then this is a pointer to an
- incomplete type. We create a generic format string
- 'pointer to bytes' in this case. XXX Better would be to
- fix the format string later...
- */
- stgdict->format = _ctypes_alloc_format_string("&",
- itemdict->format ? itemdict->format : "B");
- if (stgdict->format == NULL) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- }
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
- if (result == NULL) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
- /* replace the class dict by our updated spam dict */
- if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)stgdict;
-
- return (PyObject *)result;
+ stgdict = (StgDictObject *)PyObject_CallObject(
+ (PyObject *)&PyCStgDict_Type, NULL);
+ if (!stgdict)
+ return NULL;
+ stgdict->size = sizeof(void *);
+ stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
+ stgdict->length = 1;
+ stgdict->ffi_type_pointer = ffi_type_pointer;
+ stgdict->paramfunc = PyCPointerType_paramfunc;
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+
+ proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
+ if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+
+ if (proto) {
+ StgDictObject *itemdict = PyType_stgdict(proto);
+ assert(itemdict);
+ /* If itemdict->format is NULL, then this is a pointer to an
+ incomplete type. We create a generic format string
+ 'pointer to bytes' in this case. XXX Better would be to
+ fix the format string later...
+ */
+ stgdict->format = _ctypes_alloc_format_string("&",
+ itemdict->format ? itemdict->format : "B");
+ if (stgdict->format == NULL) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ }
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
+ if (result == NULL) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+
+ /* replace the class dict by our updated spam dict */
+ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)stgdict;
+
+ return (PyObject *)result;
}
static PyObject *
PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
{
- StgDictObject *dict;
+ StgDictObject *dict;
- dict = PyType_stgdict((PyObject *)self);
- assert(dict);
+ dict = PyType_stgdict((PyObject *)self);
+ assert(dict);
- if (-1 == PyCPointerType_SetProto(dict, type))
- return NULL;
+ if (-1 == PyCPointerType_SetProto(dict, type))
+ return NULL;
- if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
- return NULL;
+ if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
+ return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *_byref(PyObject *);
@@ -934,98 +934,98 @@ static PyObject *_byref(PyObject *);
static PyObject *
PyCPointerType_from_param(PyObject *type, PyObject *value)
{
- StgDictObject *typedict;
-
- if (value == Py_None) {
- /* ConvParam will convert to a NULL pointer later */
- Py_INCREF(value);
- return value;
- }
-
- typedict = PyType_stgdict(type);
- assert(typedict); /* Cannot be NULL for pointer types */
-
- /* If we expect POINTER(<type>), but receive a <type> instance, accept
- it by calling byref(<type>).
- */
- switch (PyObject_IsInstance(value, typedict->proto)) {
- case 1:
- Py_INCREF(value); /* _byref steals a refcount */
- return _byref(value);
- case -1:
- PyErr_Clear();
- break;
- default:
- break;
- }
-
- if (PointerObject_Check(value) || ArrayObject_Check(value)) {
- /* Array instances are also pointers when
- the item types are the same.
- */
- StgDictObject *v = PyObject_stgdict(value);
- assert(v); /* Cannot be NULL for pointer or array objects */
- if (PyObject_IsSubclass(v->proto, typedict->proto)) {
- Py_INCREF(value);
- return value;
- }
- }
- return CDataType_from_param(type, value);
+ StgDictObject *typedict;
+
+ if (value == Py_None) {
+ /* ConvParam will convert to a NULL pointer later */
+ Py_INCREF(value);
+ return value;
+ }
+
+ typedict = PyType_stgdict(type);
+ assert(typedict); /* Cannot be NULL for pointer types */
+
+ /* If we expect POINTER(<type>), but receive a <type> instance, accept
+ it by calling byref(<type>).
+ */
+ switch (PyObject_IsInstance(value, typedict->proto)) {
+ case 1:
+ Py_INCREF(value); /* _byref steals a refcount */
+ return _byref(value);
+ case -1:
+ PyErr_Clear();
+ break;
+ default:
+ break;
+ }
+
+ if (PointerObject_Check(value) || ArrayObject_Check(value)) {
+ /* Array instances are also pointers when
+ the item types are the same.
+ */
+ StgDictObject *v = PyObject_stgdict(value);
+ assert(v); /* Cannot be NULL for pointer or array objects */
+ if (PyObject_IsSubclass(v->proto, typedict->proto)) {
+ Py_INCREF(value);
+ return value;
+ }
+ }
+ return CDataType_from_param(type, value);
}
static PyMethodDef PyCPointerType_methods[] = {
- { "from_address", CDataType_from_address, METH_O, from_address_doc },
- { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
- { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
- { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
- { "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc},
- { "set_type", (PyCFunction)PyCPointerType_set_type, METH_O },
- { NULL, NULL },
+ { "from_address", CDataType_from_address, METH_O, from_address_doc },
+ { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
+ { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
+ { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
+ { "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc},
+ { "set_type", (PyCFunction)PyCPointerType_set_type, METH_O },
+ { NULL, NULL },
};
PyTypeObject PyCPointerType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCPointerType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "metatype for the Pointer Objects", /* tp_doc */
- (traverseproc)CDataType_traverse, /* tp_traverse */
- (inquiry)CDataType_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- PyCPointerType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCPointerType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCPointerType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "metatype for the Pointer Objects", /* tp_doc */
+ (traverseproc)CDataType_traverse, /* tp_traverse */
+ (inquiry)CDataType_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ PyCPointerType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCPointerType_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
PyCArrayType_Type
@@ -1038,152 +1038,152 @@ PyTypeObject PyCPointerType_Type = {
static int
CharArray_set_raw(CDataObject *self, PyObject *value)
{
- char *ptr;
- Py_ssize_t size;
- Py_buffer view;
-
- if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
- return -1;
- size = view.len;
- ptr = view.buf;
- if (size > self->b_size) {
- PyErr_SetString(PyExc_ValueError,
- "string too long");
- goto fail;
- }
-
- memcpy(self->b_ptr, ptr, size);
-
- PyBuffer_Release(&view);
- return 0;
- fail:
- PyBuffer_Release(&view);
+ char *ptr;
+ Py_ssize_t size;
+ Py_buffer view;
+
+ if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
return -1;
+ size = view.len;
+ ptr = view.buf;
+ if (size > self->b_size) {
+ PyErr_SetString(PyExc_ValueError,
+ "string too long");
+ goto fail;
+ }
+
+ memcpy(self->b_ptr, ptr, size);
+
+ PyBuffer_Release(&view);
+ return 0;
+ fail:
+ PyBuffer_Release(&view);
+ return -1;
}
static PyObject *
CharArray_get_raw(CDataObject *self)
{
- return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
+ return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
}
static PyObject *
CharArray_get_value(CDataObject *self)
{
- int i;
- char *ptr = self->b_ptr;
- for (i = 0; i < self->b_size; ++i)
- if (*ptr++ == '\0')
- break;
- return PyBytes_FromStringAndSize(self->b_ptr, i);
+ int i;
+ char *ptr = self->b_ptr;
+ for (i = 0; i < self->b_size; ++i)
+ if (*ptr++ == '\0')
+ break;
+ return PyBytes_FromStringAndSize(self->b_ptr, i);
}
static int
CharArray_set_value(CDataObject *self, PyObject *value)
{
- char *ptr;
- Py_ssize_t size;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete attribute");
- return -1;
- }
-
- if (PyUnicode_Check(value)) {
- value = PyUnicode_AsEncodedString(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return -1;
- } else if (!PyBytes_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "str/bytes expected instead of %s instance",
- Py_TYPE(value)->tp_name);
- return -1;
- } else
- Py_INCREF(value);
- size = PyBytes_GET_SIZE(value);
- if (size > self->b_size) {
- PyErr_SetString(PyExc_ValueError,
- "string too long");
- Py_DECREF(value);
- return -1;
- }
-
- ptr = PyBytes_AS_STRING(value);
- memcpy(self->b_ptr, ptr, size);
- if (size < self->b_size)
- self->b_ptr[size] = '\0';
- Py_DECREF(value);
-
- return 0;
+ char *ptr;
+ Py_ssize_t size;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete attribute");
+ return -1;
+ }
+
+ if (PyUnicode_Check(value)) {
+ value = PyUnicode_AsEncodedString(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return -1;
+ } else if (!PyBytes_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "str/bytes expected instead of %s instance",
+ Py_TYPE(value)->tp_name);
+ return -1;
+ } else
+ Py_INCREF(value);
+ size = PyBytes_GET_SIZE(value);
+ if (size > self->b_size) {
+ PyErr_SetString(PyExc_ValueError,
+ "string too long");
+ Py_DECREF(value);
+ return -1;
+ }
+
+ ptr = PyBytes_AS_STRING(value);
+ memcpy(self->b_ptr, ptr, size);
+ if (size < self->b_size)
+ self->b_ptr[size] = '\0';
+ Py_DECREF(value);
+
+ return 0;
}
static PyGetSetDef CharArray_getsets[] = {
- { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw,
- "value", NULL },
- { "value", (getter)CharArray_get_value, (setter)CharArray_set_value,
- "string value"},
- { NULL, NULL }
+ { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw,
+ "value", NULL },
+ { "value", (getter)CharArray_get_value, (setter)CharArray_set_value,
+ "string value"},
+ { NULL, NULL }
};
#ifdef CTYPES_UNICODE
static PyObject *
WCharArray_get_value(CDataObject *self)
{
- unsigned int i;
- wchar_t *ptr = (wchar_t *)self->b_ptr;
- for (i = 0; i < self->b_size/sizeof(wchar_t); ++i)
- if (*ptr++ == (wchar_t)0)
- break;
- return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i);
+ unsigned int i;
+ wchar_t *ptr = (wchar_t *)self->b_ptr;
+ for (i = 0; i < self->b_size/sizeof(wchar_t); ++i)
+ if (*ptr++ == (wchar_t)0)
+ break;
+ return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i);
}
static int
WCharArray_set_value(CDataObject *self, PyObject *value)
{
- Py_ssize_t result = 0;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete attribute");
- return -1;
- }
- if (PyBytes_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return -1;
- } else if (!PyUnicode_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "unicode string expected instead of %s instance",
- Py_TYPE(value)->tp_name);
- return -1;
- } else
- Py_INCREF(value);
- if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) {
- PyErr_SetString(PyExc_ValueError,
- "string too long");
- result = -1;
- goto done;
- }
- result = PyUnicode_AsWideChar((PyUnicodeObject *)value,
- (wchar_t *)self->b_ptr,
- self->b_size/sizeof(wchar_t));
- if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t))
- ((wchar_t *)self->b_ptr)[result] = (wchar_t)0;
+ Py_ssize_t result = 0;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete attribute");
+ return -1;
+ }
+ if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return -1;
+ } else if (!PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "unicode string expected instead of %s instance",
+ Py_TYPE(value)->tp_name);
+ return -1;
+ } else
+ Py_INCREF(value);
+ if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) {
+ PyErr_SetString(PyExc_ValueError,
+ "string too long");
+ result = -1;
+ goto done;
+ }
+ result = PyUnicode_AsWideChar((PyUnicodeObject *)value,
+ (wchar_t *)self->b_ptr,
+ self->b_size/sizeof(wchar_t));
+ if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t))
+ ((wchar_t *)self->b_ptr)[result] = (wchar_t)0;
done:
- Py_DECREF(value);
+ Py_DECREF(value);
- return result >= 0 ? 0 : -1;
+ return result >= 0 ? 0 : -1;
}
static PyGetSetDef WCharArray_getsets[] = {
- { "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value,
- "string value"},
- { NULL, NULL }
+ { "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value,
+ "string value"},
+ { NULL, NULL }
};
#endif
@@ -1198,236 +1198,236 @@ static PyGetSetDef WCharArray_getsets[] = {
static int
add_methods(PyTypeObject *type, PyMethodDef *meth)
{
- PyObject *dict = type->tp_dict;
- for (; meth->ml_name != NULL; meth++) {
- PyObject *descr;
- descr = PyDescr_NewMethod(type, meth);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ PyObject *dict = type->tp_dict;
+ for (; meth->ml_name != NULL; meth++) {
+ PyObject *descr;
+ descr = PyDescr_NewMethod(type, meth);
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
static int
add_members(PyTypeObject *type, PyMemberDef *memb)
{
- PyObject *dict = type->tp_dict;
- for (; memb->name != NULL; memb++) {
- PyObject *descr;
- descr = PyDescr_NewMember(type, memb);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict, memb->name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ PyObject *dict = type->tp_dict;
+ for (; memb->name != NULL; memb++) {
+ PyObject *descr;
+ descr = PyDescr_NewMember(type, memb);
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, memb->name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
*/
static int
add_getset(PyTypeObject *type, PyGetSetDef *gsp)
{
- PyObject *dict = type->tp_dict;
- for (; gsp->name != NULL; gsp++) {
- PyObject *descr;
- descr = PyDescr_NewGetSet(type, gsp);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ PyObject *dict = type->tp_dict;
+ for (; gsp->name != NULL; gsp++) {
+ PyObject *descr;
+ descr = PyDescr_NewGetSet(type, gsp);
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
static PyCArgObject *
PyCArrayType_paramfunc(CDataObject *self)
{
- PyCArgObject *p = PyCArgObject_new();
- if (p == NULL)
- return NULL;
- p->tag = 'P';
- p->pffi_type = &ffi_type_pointer;
- p->value.p = (char *)self->b_ptr;
- Py_INCREF(self);
- p->obj = (PyObject *)self;
- return p;
+ PyCArgObject *p = PyCArgObject_new();
+ if (p == NULL)
+ return NULL;
+ p->tag = 'P';
+ p->pffi_type = &ffi_type_pointer;
+ p->value.p = (char *)self->b_ptr;
+ Py_INCREF(self);
+ p->obj = (PyObject *)self;
+ return p;
}
static PyObject *
PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyTypeObject *result;
- StgDictObject *stgdict;
- StgDictObject *itemdict;
- PyObject *proto;
- PyObject *typedict;
- long length;
- int overflow;
- Py_ssize_t itemsize, itemalign;
- char buf[32];
-
- typedict = PyTuple_GetItem(args, 2);
- if (!typedict)
- return NULL;
-
- proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */
- if (!proto || !PyLong_Check(proto)) {
- PyErr_SetString(PyExc_AttributeError,
- "class must define a '_length_' attribute, "
- "which must be a positive integer");
- return NULL;
- }
- length = PyLong_AsLongAndOverflow(proto, &overflow);
- if (overflow) {
- PyErr_SetString(PyExc_OverflowError,
- "The '_length_' attribute is too large");
- return NULL;
- }
-
- proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
- if (!proto) {
- PyErr_SetString(PyExc_AttributeError,
- "class must define a '_type_' attribute");
- return NULL;
- }
-
- stgdict = (StgDictObject *)PyObject_CallObject(
- (PyObject *)&PyCStgDict_Type, NULL);
- if (!stgdict)
- return NULL;
-
- itemdict = PyType_stgdict(proto);
- if (!itemdict) {
- PyErr_SetString(PyExc_TypeError,
- "_type_ must have storage info");
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
- assert(itemdict->format);
- if (itemdict->format[0] == '(') {
- sprintf(buf, "(%ld,", length);
- stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format+1);
- } else {
- sprintf(buf, "(%ld)", length);
- stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format);
- }
- if (stgdict->format == NULL) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- stgdict->ndim = itemdict->ndim + 1;
- stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim);
- if (stgdict->shape == NULL) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- stgdict->shape[0] = length;
- memmove(&stgdict->shape[1], itemdict->shape,
- sizeof(Py_ssize_t) * (stgdict->ndim - 1));
-
- itemsize = itemdict->size;
- if (length * itemsize < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "array too large");
- return NULL;
- }
-
- itemalign = itemdict->align;
-
- if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
- stgdict->flags |= TYPEFLAG_HASPOINTER;
-
- stgdict->size = itemsize * length;
- stgdict->align = itemalign;
- stgdict->length = length;
- Py_INCREF(proto);
- stgdict->proto = proto;
-
- stgdict->paramfunc = &PyCArrayType_paramfunc;
-
- /* Arrays are passed as pointers to function calls. */
- stgdict->ffi_type_pointer = ffi_type_pointer;
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
- if (result == NULL)
- return NULL;
-
- /* replace the class dict by our updated spam dict */
- if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)stgdict;
-
- /* Special case for character arrays.
- A permanent annoyance: char arrays are also strings!
- */
- if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
- if (-1 == add_getset(result, CharArray_getsets))
- return NULL;
+ PyTypeObject *result;
+ StgDictObject *stgdict;
+ StgDictObject *itemdict;
+ PyObject *proto;
+ PyObject *typedict;
+ long length;
+ int overflow;
+ Py_ssize_t itemsize, itemalign;
+ char buf[32];
+
+ typedict = PyTuple_GetItem(args, 2);
+ if (!typedict)
+ return NULL;
+
+ proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */
+ if (!proto || !PyLong_Check(proto)) {
+ PyErr_SetString(PyExc_AttributeError,
+ "class must define a '_length_' attribute, "
+ "which must be a positive integer");
+ return NULL;
+ }
+ length = PyLong_AsLongAndOverflow(proto, &overflow);
+ if (overflow) {
+ PyErr_SetString(PyExc_OverflowError,
+ "The '_length_' attribute is too large");
+ return NULL;
+ }
+
+ proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
+ if (!proto) {
+ PyErr_SetString(PyExc_AttributeError,
+ "class must define a '_type_' attribute");
+ return NULL;
+ }
+
+ stgdict = (StgDictObject *)PyObject_CallObject(
+ (PyObject *)&PyCStgDict_Type, NULL);
+ if (!stgdict)
+ return NULL;
+
+ itemdict = PyType_stgdict(proto);
+ if (!itemdict) {
+ PyErr_SetString(PyExc_TypeError,
+ "_type_ must have storage info");
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+
+ assert(itemdict->format);
+ if (itemdict->format[0] == '(') {
+ sprintf(buf, "(%ld,", length);
+ stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format+1);
+ } else {
+ sprintf(buf, "(%ld)", length);
+ stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format);
+ }
+ if (stgdict->format == NULL) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ stgdict->ndim = itemdict->ndim + 1;
+ stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim);
+ if (stgdict->shape == NULL) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ stgdict->shape[0] = length;
+ memmove(&stgdict->shape[1], itemdict->shape,
+ sizeof(Py_ssize_t) * (stgdict->ndim - 1));
+
+ itemsize = itemdict->size;
+ if (length * itemsize < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "array too large");
+ return NULL;
+ }
+
+ itemalign = itemdict->align;
+
+ if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
+ stgdict->flags |= TYPEFLAG_HASPOINTER;
+
+ stgdict->size = itemsize * length;
+ stgdict->align = itemalign;
+ stgdict->length = length;
+ Py_INCREF(proto);
+ stgdict->proto = proto;
+
+ stgdict->paramfunc = &PyCArrayType_paramfunc;
+
+ /* Arrays are passed as pointers to function calls. */
+ stgdict->ffi_type_pointer = ffi_type_pointer;
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
+ if (result == NULL)
+ return NULL;
+
+ /* replace the class dict by our updated spam dict */
+ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)stgdict;
+
+ /* Special case for character arrays.
+ A permanent annoyance: char arrays are also strings!
+ */
+ if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
+ if (-1 == add_getset(result, CharArray_getsets))
+ return NULL;
#ifdef CTYPES_UNICODE
- } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
- if (-1 == add_getset(result, WCharArray_getsets))
- return NULL;
+ } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
+ if (-1 == add_getset(result, WCharArray_getsets))
+ return NULL;
#endif
- }
+ }
- return (PyObject *)result;
+ return (PyObject *)result;
}
PyTypeObject PyCArrayType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCArrayType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "metatype for the Array Objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- CDataType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCArrayType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCArrayType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "metatype for the Array Objects", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ CDataType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCArrayType_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
PyCSimpleType_Type
@@ -1444,273 +1444,273 @@ static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g";
static PyObject *
c_wchar_p_from_param(PyObject *type, PyObject *value)
{
- PyObject *as_parameter;
- if (value == Py_None) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- if (PyUnicode_Check(value) || PyBytes_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("Z");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
- if (PyObject_IsInstance(value, type)) {
- Py_INCREF(value);
- return value;
- }
- if (ArrayObject_Check(value) || PointerObject_Check(value)) {
- /* c_wchar array instance or pointer(c_wchar(...)) */
- StgDictObject *dt = PyObject_stgdict(value);
- StgDictObject *dict;
- assert(dt); /* Cannot be NULL for pointer or array objects */
- dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
- if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
- Py_INCREF(value);
- return value;
- }
- }
- if (PyCArg_CheckExact(value)) {
- /* byref(c_char(...)) */
- PyCArgObject *a = (PyCArgObject *)value;
- StgDictObject *dict = PyObject_stgdict(a->obj);
- if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
- Py_INCREF(value);
- return value;
- }
- }
-
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
- if (as_parameter) {
- value = c_wchar_p_from_param(type, as_parameter);
- Py_DECREF(as_parameter);
- return value;
- }
- /* XXX better message */
- PyErr_SetString(PyExc_TypeError,
- "wrong type");
- return NULL;
+ PyObject *as_parameter;
+ if (value == Py_None) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ if (PyUnicode_Check(value) || PyBytes_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("Z");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'Z';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
+ if (PyObject_IsInstance(value, type)) {
+ Py_INCREF(value);
+ return value;
+ }
+ if (ArrayObject_Check(value) || PointerObject_Check(value)) {
+ /* c_wchar array instance or pointer(c_wchar(...)) */
+ StgDictObject *dt = PyObject_stgdict(value);
+ StgDictObject *dict;
+ assert(dt); /* Cannot be NULL for pointer or array objects */
+ dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
+ if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
+ Py_INCREF(value);
+ return value;
+ }
+ }
+ if (PyCArg_CheckExact(value)) {
+ /* byref(c_char(...)) */
+ PyCArgObject *a = (PyCArgObject *)value;
+ StgDictObject *dict = PyObject_stgdict(a->obj);
+ if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
+ Py_INCREF(value);
+ return value;
+ }
+ }
+
+ as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (as_parameter) {
+ value = c_wchar_p_from_param(type, as_parameter);
+ Py_DECREF(as_parameter);
+ return value;
+ }
+ /* XXX better message */
+ PyErr_SetString(PyExc_TypeError,
+ "wrong type");
+ return NULL;
}
static PyObject *
c_char_p_from_param(PyObject *type, PyObject *value)
{
- PyObject *as_parameter;
- if (value == Py_None) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- if (PyBytes_Check(value) || PyUnicode_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("z");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'z';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
- if (PyObject_IsInstance(value, type)) {
- Py_INCREF(value);
- return value;
- }
- if (ArrayObject_Check(value) || PointerObject_Check(value)) {
- /* c_char array instance or pointer(c_char(...)) */
- StgDictObject *dt = PyObject_stgdict(value);
- StgDictObject *dict;
- assert(dt); /* Cannot be NULL for pointer or array objects */
- dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
- if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
- Py_INCREF(value);
- return value;
- }
- }
- if (PyCArg_CheckExact(value)) {
- /* byref(c_char(...)) */
- PyCArgObject *a = (PyCArgObject *)value;
- StgDictObject *dict = PyObject_stgdict(a->obj);
- if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
- Py_INCREF(value);
- return value;
- }
- }
-
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
- if (as_parameter) {
- value = c_char_p_from_param(type, as_parameter);
- Py_DECREF(as_parameter);
- return value;
- }
- /* XXX better message */
- PyErr_SetString(PyExc_TypeError,
- "wrong type");
- return NULL;
+ PyObject *as_parameter;
+ if (value == Py_None) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ if (PyBytes_Check(value) || PyUnicode_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("z");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'z';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
+ if (PyObject_IsInstance(value, type)) {
+ Py_INCREF(value);
+ return value;
+ }
+ if (ArrayObject_Check(value) || PointerObject_Check(value)) {
+ /* c_char array instance or pointer(c_char(...)) */
+ StgDictObject *dt = PyObject_stgdict(value);
+ StgDictObject *dict;
+ assert(dt); /* Cannot be NULL for pointer or array objects */
+ dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
+ if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
+ Py_INCREF(value);
+ return value;
+ }
+ }
+ if (PyCArg_CheckExact(value)) {
+ /* byref(c_char(...)) */
+ PyCArgObject *a = (PyCArgObject *)value;
+ StgDictObject *dict = PyObject_stgdict(a->obj);
+ if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
+ Py_INCREF(value);
+ return value;
+ }
+ }
+
+ as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (as_parameter) {
+ value = c_char_p_from_param(type, as_parameter);
+ Py_DECREF(as_parameter);
+ return value;
+ }
+ /* XXX better message */
+ PyErr_SetString(PyExc_TypeError,
+ "wrong type");
+ return NULL;
}
static PyObject *
c_void_p_from_param(PyObject *type, PyObject *value)
{
- StgDictObject *stgd;
- PyObject *as_parameter;
+ StgDictObject *stgd;
+ PyObject *as_parameter;
/* None */
- if (value == Py_None) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- /* Should probably allow buffer interface as well */
+ if (value == Py_None) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ /* Should probably allow buffer interface as well */
/* int, long */
- if (PyLong_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("P");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'P';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
- /* XXX struni: remove later */
+ if (PyLong_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("P");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'P';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
+ /* XXX struni: remove later */
/* string */
- if (PyBytes_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("z");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'z';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
+ if (PyBytes_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("z");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'z';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
/* bytes */
- if (PyByteArray_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("z");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'z';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
+ if (PyByteArray_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("z");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'z';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
/* unicode */
- if (PyUnicode_Check(value)) {
- PyCArgObject *parg;
- struct fielddesc *fd = _ctypes_get_fielddesc("Z");
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj == NULL) {
- Py_DECREF(parg);
- return NULL;
- }
- return (PyObject *)parg;
- }
+ if (PyUnicode_Check(value)) {
+ PyCArgObject *parg;
+ struct fielddesc *fd = _ctypes_get_fielddesc("Z");
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'Z';
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj == NULL) {
+ Py_DECREF(parg);
+ return NULL;
+ }
+ return (PyObject *)parg;
+ }
/* c_void_p instance (or subclass) */
- if (PyObject_IsInstance(value, type)) {
- /* c_void_p instances */
- Py_INCREF(value);
- return value;
- }
+ if (PyObject_IsInstance(value, type)) {
+ /* c_void_p instances */
+ Py_INCREF(value);
+ return value;
+ }
/* ctypes array or pointer instance */
- if (ArrayObject_Check(value) || PointerObject_Check(value)) {
- /* Any array or pointer is accepted */
- Py_INCREF(value);
- return value;
- }
+ if (ArrayObject_Check(value) || PointerObject_Check(value)) {
+ /* Any array or pointer is accepted */
+ Py_INCREF(value);
+ return value;
+ }
/* byref(...) */
- if (PyCArg_CheckExact(value)) {
- /* byref(c_xxx()) */
- PyCArgObject *a = (PyCArgObject *)value;
- if (a->tag == 'P') {
- Py_INCREF(value);
- return value;
- }
- }
+ if (PyCArg_CheckExact(value)) {
+ /* byref(c_xxx()) */
+ PyCArgObject *a = (PyCArgObject *)value;
+ if (a->tag == 'P') {
+ Py_INCREF(value);
+ return value;
+ }
+ }
/* function pointer */
- if (PyCFuncPtrObject_Check(value)) {
- PyCArgObject *parg;
- PyCFuncPtrObject *func;
- func = (PyCFuncPtrObject *)value;
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'P';
- Py_INCREF(value);
- parg->value.p = *(void **)func->b_ptr;
- parg->obj = value;
- return (PyObject *)parg;
- }
+ if (PyCFuncPtrObject_Check(value)) {
+ PyCArgObject *parg;
+ PyCFuncPtrObject *func;
+ func = (PyCFuncPtrObject *)value;
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'P';
+ Py_INCREF(value);
+ parg->value.p = *(void **)func->b_ptr;
+ parg->obj = value;
+ return (PyObject *)parg;
+ }
/* c_char_p, c_wchar_p */
- stgd = PyObject_stgdict(value);
- if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) {
- PyCArgObject *parg;
-
- switch (_PyUnicode_AsString(stgd->proto)[0]) {
- case 'z': /* c_char_p */
- case 'Z': /* c_wchar_p */
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
- parg->pffi_type = &ffi_type_pointer;
- parg->tag = 'Z';
- Py_INCREF(value);
- parg->obj = value;
- /* Remember: b_ptr points to where the pointer is stored! */
- parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
- return (PyObject *)parg;
- }
- }
-
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
- if (as_parameter) {
- value = c_void_p_from_param(type, as_parameter);
- Py_DECREF(as_parameter);
- return value;
- }
- /* XXX better message */
- PyErr_SetString(PyExc_TypeError,
- "wrong type");
- return NULL;
+ stgd = PyObject_stgdict(value);
+ if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) {
+ PyCArgObject *parg;
+
+ switch (_PyUnicode_AsString(stgd->proto)[0]) {
+ case 'z': /* c_char_p */
+ case 'Z': /* c_wchar_p */
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+ parg->pffi_type = &ffi_type_pointer;
+ parg->tag = 'Z';
+ Py_INCREF(value);
+ parg->obj = value;
+ /* Remember: b_ptr points to where the pointer is stored! */
+ parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
+ return (PyObject *)parg;
+ }
+ }
+
+ as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (as_parameter) {
+ value = c_void_p_from_param(type, as_parameter);
+ Py_DECREF(as_parameter);
+ return value;
+ }
+ /* XXX better message */
+ PyErr_SetString(PyExc_TypeError,
+ "wrong type");
+ return NULL;
}
static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O };
@@ -1718,278 +1718,278 @@ static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O
static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH_O };
static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds,
- PyObject *proto, struct fielddesc *fmt)
+ PyObject *proto, struct fielddesc *fmt)
{
- PyTypeObject *result;
- StgDictObject *stgdict;
- PyObject *name = PyTuple_GET_ITEM(args, 0);
- PyObject *newname;
- PyObject *swapped_args;
- static PyObject *suffix;
- Py_ssize_t i;
-
- swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
- if (!swapped_args)
- return NULL;
-
- if (suffix == NULL)
+ PyTypeObject *result;
+ StgDictObject *stgdict;
+ PyObject *name = PyTuple_GET_ITEM(args, 0);
+ PyObject *newname;
+ PyObject *swapped_args;
+ static PyObject *suffix;
+ Py_ssize_t i;
+
+ swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
+ if (!swapped_args)
+ return NULL;
+
+ if (suffix == NULL)
#ifdef WORDS_BIGENDIAN
- suffix = PyUnicode_InternFromString("_le");
+ suffix = PyUnicode_InternFromString("_le");
#else
- suffix = PyUnicode_InternFromString("_be");
+ suffix = PyUnicode_InternFromString("_be");
#endif
- newname = PyUnicode_Concat(name, suffix);
- if (newname == NULL) {
- return NULL;
- }
-
- PyTuple_SET_ITEM(swapped_args, 0, newname);
- for (i=1; i<PyTuple_GET_SIZE(args); ++i) {
- PyObject *v = PyTuple_GET_ITEM(args, i);
- Py_INCREF(v);
- PyTuple_SET_ITEM(swapped_args, i, v);
- }
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds);
- Py_DECREF(swapped_args);
- if (result == NULL)
- return NULL;
-
- stgdict = (StgDictObject *)PyObject_CallObject(
- (PyObject *)&PyCStgDict_Type, NULL);
- if (!stgdict) /* XXX leaks result! */
- return NULL;
-
- stgdict->ffi_type_pointer = *fmt->pffi_type;
- stgdict->align = fmt->pffi_type->alignment;
- stgdict->length = 0;
- stgdict->size = fmt->pffi_type->size;
- stgdict->setfunc = fmt->setfunc_swapped;
- stgdict->getfunc = fmt->getfunc_swapped;
-
- Py_INCREF(proto);
- stgdict->proto = proto;
-
- /* replace the class dict by our updated spam dict */
- if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)stgdict;
-
- return (PyObject *)result;
+ newname = PyUnicode_Concat(name, suffix);
+ if (newname == NULL) {
+ return NULL;
+ }
+
+ PyTuple_SET_ITEM(swapped_args, 0, newname);
+ for (i=1; i<PyTuple_GET_SIZE(args); ++i) {
+ PyObject *v = PyTuple_GET_ITEM(args, i);
+ Py_INCREF(v);
+ PyTuple_SET_ITEM(swapped_args, i, v);
+ }
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds);
+ Py_DECREF(swapped_args);
+ if (result == NULL)
+ return NULL;
+
+ stgdict = (StgDictObject *)PyObject_CallObject(
+ (PyObject *)&PyCStgDict_Type, NULL);
+ if (!stgdict) /* XXX leaks result! */
+ return NULL;
+
+ stgdict->ffi_type_pointer = *fmt->pffi_type;
+ stgdict->align = fmt->pffi_type->alignment;
+ stgdict->length = 0;
+ stgdict->size = fmt->pffi_type->size;
+ stgdict->setfunc = fmt->setfunc_swapped;
+ stgdict->getfunc = fmt->getfunc_swapped;
+
+ Py_INCREF(proto);
+ stgdict->proto = proto;
+
+ /* replace the class dict by our updated spam dict */
+ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)stgdict;
+
+ return (PyObject *)result;
}
static PyCArgObject *
PyCSimpleType_paramfunc(CDataObject *self)
{
- StgDictObject *dict;
- char *fmt;
- PyCArgObject *parg;
- struct fielddesc *fd;
-
- dict = PyObject_stgdict((PyObject *)self);
- assert(dict); /* Cannot be NULL for CDataObject instances */
- fmt = _PyUnicode_AsString(dict->proto);
- assert(fmt);
-
- fd = _ctypes_get_fielddesc(fmt);
- assert(fd);
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = fmt[0];
- parg->pffi_type = fd->pffi_type;
- Py_INCREF(self);
- parg->obj = (PyObject *)self;
- memcpy(&parg->value, self->b_ptr, self->b_size);
- return parg;
+ StgDictObject *dict;
+ char *fmt;
+ PyCArgObject *parg;
+ struct fielddesc *fd;
+
+ dict = PyObject_stgdict((PyObject *)self);
+ assert(dict); /* Cannot be NULL for CDataObject instances */
+ fmt = _PyUnicode_AsString(dict->proto);
+ assert(fmt);
+
+ fd = _ctypes_get_fielddesc(fmt);
+ assert(fd);
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = fmt[0];
+ parg->pffi_type = fd->pffi_type;
+ Py_INCREF(self);
+ parg->obj = (PyObject *)self;
+ memcpy(&parg->value, self->b_ptr, self->b_size);
+ return parg;
}
static PyObject *
PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyTypeObject *result;
- StgDictObject *stgdict;
- PyObject *proto;
- const char *proto_str;
- Py_ssize_t proto_len;
- PyMethodDef *ml;
- struct fielddesc *fmt;
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
- if (result == NULL)
- return NULL;
-
- proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
- if (!proto) {
- PyErr_SetString(PyExc_AttributeError,
- "class must define a '_type_' attribute");
+ PyTypeObject *result;
+ StgDictObject *stgdict;
+ PyObject *proto;
+ const char *proto_str;
+ Py_ssize_t proto_len;
+ PyMethodDef *ml;
+ struct fielddesc *fmt;
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
+ if (result == NULL)
+ return NULL;
+
+ proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
+ if (!proto) {
+ PyErr_SetString(PyExc_AttributeError,
+ "class must define a '_type_' attribute");
error:
- Py_XDECREF(proto);
- Py_XDECREF(result);
- return NULL;
- }
- if (PyUnicode_Check(proto)) {
- PyObject *v = _PyUnicode_AsDefaultEncodedString(proto, NULL);
- if (!v)
- goto error;
- proto_str = PyBytes_AS_STRING(v);
- proto_len = PyBytes_GET_SIZE(v);
- } else {
- PyErr_SetString(PyExc_TypeError,
- "class must define a '_type_' string attribute");
- goto error;
- }
- if (proto_len != 1) {
- PyErr_SetString(PyExc_ValueError,
- "class must define a '_type_' attribute "
- "which must be a string of length 1");
- goto error;
- }
- if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
- PyErr_Format(PyExc_AttributeError,
- "class must define a '_type_' attribute which must be\n"
- "a single character string containing one of '%s'.",
- SIMPLE_TYPE_CHARS);
- goto error;
- }
- fmt = _ctypes_get_fielddesc(proto_str);
- if (fmt == NULL) {
- PyErr_Format(PyExc_ValueError,
- "_type_ '%s' not supported", proto_str);
- goto error;
- }
-
- stgdict = (StgDictObject *)PyObject_CallObject(
- (PyObject *)&PyCStgDict_Type, NULL);
- if (!stgdict)
- goto error;
-
- stgdict->ffi_type_pointer = *fmt->pffi_type;
- stgdict->align = fmt->pffi_type->alignment;
- stgdict->length = 0;
- stgdict->size = fmt->pffi_type->size;
- stgdict->setfunc = fmt->setfunc;
- stgdict->getfunc = fmt->getfunc;
+ Py_XDECREF(proto);
+ Py_XDECREF(result);
+ return NULL;
+ }
+ if (PyUnicode_Check(proto)) {
+ PyObject *v = _PyUnicode_AsDefaultEncodedString(proto, NULL);
+ if (!v)
+ goto error;
+ proto_str = PyBytes_AS_STRING(v);
+ proto_len = PyBytes_GET_SIZE(v);
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "class must define a '_type_' string attribute");
+ goto error;
+ }
+ if (proto_len != 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "class must define a '_type_' attribute "
+ "which must be a string of length 1");
+ goto error;
+ }
+ if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
+ PyErr_Format(PyExc_AttributeError,
+ "class must define a '_type_' attribute which must be\n"
+ "a single character string containing one of '%s'.",
+ SIMPLE_TYPE_CHARS);
+ goto error;
+ }
+ fmt = _ctypes_get_fielddesc(proto_str);
+ if (fmt == NULL) {
+ PyErr_Format(PyExc_ValueError,
+ "_type_ '%s' not supported", proto_str);
+ goto error;
+ }
+
+ stgdict = (StgDictObject *)PyObject_CallObject(
+ (PyObject *)&PyCStgDict_Type, NULL);
+ if (!stgdict)
+ goto error;
+
+ stgdict->ffi_type_pointer = *fmt->pffi_type;
+ stgdict->align = fmt->pffi_type->alignment;
+ stgdict->length = 0;
+ stgdict->size = fmt->pffi_type->size;
+ stgdict->setfunc = fmt->setfunc;
+ stgdict->getfunc = fmt->getfunc;
#ifdef WORDS_BIGENDIAN
- stgdict->format = _ctypes_alloc_format_string(">", proto_str);
+ stgdict->format = _ctypes_alloc_format_string(">", proto_str);
#else
- stgdict->format = _ctypes_alloc_format_string("<", proto_str);
+ stgdict->format = _ctypes_alloc_format_string("<", proto_str);
#endif
- if (stgdict->format == NULL) {
- Py_DECREF(result);
- Py_DECREF(proto);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
- stgdict->paramfunc = PyCSimpleType_paramfunc;
+ if (stgdict->format == NULL) {
+ Py_DECREF(result);
+ Py_DECREF(proto);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+
+ stgdict->paramfunc = PyCSimpleType_paramfunc;
/*
- if (result->tp_base != &Simple_Type) {
- stgdict->setfunc = NULL;
- stgdict->getfunc = NULL;
- }
+ if (result->tp_base != &Simple_Type) {
+ stgdict->setfunc = NULL;
+ stgdict->getfunc = NULL;
+ }
*/
- /* This consumes the refcount on proto which we have */
- stgdict->proto = proto;
-
- /* replace the class dict by our updated spam dict */
- if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)stgdict;
-
- /* Install from_param class methods in ctypes base classes.
- Overrides the PyCSimpleType_from_param generic method.
- */
- if (result->tp_base == &Simple_Type) {
- switch (*proto_str) {
- case 'z': /* c_char_p */
- ml = &c_char_p_method;
- stgdict->flags |= TYPEFLAG_ISPOINTER;
- break;
- case 'Z': /* c_wchar_p */
- ml = &c_wchar_p_method;
- stgdict->flags |= TYPEFLAG_ISPOINTER;
- break;
- case 'P': /* c_void_p */
- ml = &c_void_p_method;
- stgdict->flags |= TYPEFLAG_ISPOINTER;
- break;
- case 's':
- case 'X':
- case 'O':
- ml = NULL;
- stgdict->flags |= TYPEFLAG_ISPOINTER;
- break;
- default:
- ml = NULL;
- break;
- }
-
- if (ml) {
- PyObject *meth;
- int x;
- meth = PyDescr_NewClassMethod(result, ml);
- if (!meth)
- return NULL;
- x = PyDict_SetItemString(result->tp_dict,
- ml->ml_name,
- meth);
- Py_DECREF(meth);
- if (x == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- }
-
- if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) {
- PyObject *swapped = CreateSwappedType(type, args, kwds,
- proto, fmt);
- StgDictObject *sw_dict;
- if (swapped == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- sw_dict = PyType_stgdict(swapped);
+ /* This consumes the refcount on proto which we have */
+ stgdict->proto = proto;
+
+ /* replace the class dict by our updated spam dict */
+ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)stgdict;
+
+ /* Install from_param class methods in ctypes base classes.
+ Overrides the PyCSimpleType_from_param generic method.
+ */
+ if (result->tp_base == &Simple_Type) {
+ switch (*proto_str) {
+ case 'z': /* c_char_p */
+ ml = &c_char_p_method;
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+ break;
+ case 'Z': /* c_wchar_p */
+ ml = &c_wchar_p_method;
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+ break;
+ case 'P': /* c_void_p */
+ ml = &c_void_p_method;
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+ break;
+ case 's':
+ case 'X':
+ case 'O':
+ ml = NULL;
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+ break;
+ default:
+ ml = NULL;
+ break;
+ }
+
+ if (ml) {
+ PyObject *meth;
+ int x;
+ meth = PyDescr_NewClassMethod(result, ml);
+ if (!meth)
+ return NULL;
+ x = PyDict_SetItemString(result->tp_dict,
+ ml->ml_name,
+ meth);
+ Py_DECREF(meth);
+ if (x == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ }
+
+ if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) {
+ PyObject *swapped = CreateSwappedType(type, args, kwds,
+ proto, fmt);
+ StgDictObject *sw_dict;
+ if (swapped == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ sw_dict = PyType_stgdict(swapped);
#ifdef WORDS_BIGENDIAN
- PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
- PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
- PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
- PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
- /* We are creating the type for the OTHER endian */
- sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1);
+ PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
+ PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
+ PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
+ PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
+ /* We are creating the type for the OTHER endian */
+ sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1);
#else
- PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
- PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
- PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
- PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
- /* We are creating the type for the OTHER endian */
- sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1);
+ PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
+ PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
+ PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
+ PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
+ /* We are creating the type for the OTHER endian */
+ sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1);
#endif
- Py_DECREF(swapped);
- if (PyErr_Occurred()) {
- Py_DECREF(result);
- return NULL;
- }
- };
-
- return (PyObject *)result;
+ Py_DECREF(swapped);
+ if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ };
+
+ return (PyObject *)result;
}
/*
@@ -1999,101 +1999,101 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
PyCSimpleType_from_param(PyObject *type, PyObject *value)
{
- StgDictObject *dict;
- char *fmt;
- PyCArgObject *parg;
- struct fielddesc *fd;
- PyObject *as_parameter;
-
- /* If the value is already an instance of the requested type,
- we can use it as is */
- if (1 == PyObject_IsInstance(value, type)) {
- Py_INCREF(value);
- return value;
- }
-
- dict = PyType_stgdict(type);
- assert(dict);
-
- /* I think we can rely on this being a one-character string */
- fmt = _PyUnicode_AsString(dict->proto);
- assert(fmt);
-
- fd = _ctypes_get_fielddesc(fmt);
- assert(fd);
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = fmt[0];
- parg->pffi_type = fd->pffi_type;
- parg->obj = fd->setfunc(&parg->value, value, 0);
- if (parg->obj)
- return (PyObject *)parg;
- PyErr_Clear();
- Py_DECREF(parg);
-
- as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
- if (as_parameter) {
- value = PyCSimpleType_from_param(type, as_parameter);
- Py_DECREF(as_parameter);
- return value;
- }
- PyErr_SetString(PyExc_TypeError,
- "wrong type");
- return NULL;
+ StgDictObject *dict;
+ char *fmt;
+ PyCArgObject *parg;
+ struct fielddesc *fd;
+ PyObject *as_parameter;
+
+ /* If the value is already an instance of the requested type,
+ we can use it as is */
+ if (1 == PyObject_IsInstance(value, type)) {
+ Py_INCREF(value);
+ return value;
+ }
+
+ dict = PyType_stgdict(type);
+ assert(dict);
+
+ /* I think we can rely on this being a one-character string */
+ fmt = _PyUnicode_AsString(dict->proto);
+ assert(fmt);
+
+ fd = _ctypes_get_fielddesc(fmt);
+ assert(fd);
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = fmt[0];
+ parg->pffi_type = fd->pffi_type;
+ parg->obj = fd->setfunc(&parg->value, value, 0);
+ if (parg->obj)
+ return (PyObject *)parg;
+ PyErr_Clear();
+ Py_DECREF(parg);
+
+ as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
+ if (as_parameter) {
+ value = PyCSimpleType_from_param(type, as_parameter);
+ Py_DECREF(as_parameter);
+ return value;
+ }
+ PyErr_SetString(PyExc_TypeError,
+ "wrong type");
+ return NULL;
}
static PyMethodDef PyCSimpleType_methods[] = {
- { "from_param", PyCSimpleType_from_param, METH_O, from_param_doc },
- { "from_address", CDataType_from_address, METH_O, from_address_doc },
- { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
- { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
- { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
- { NULL, NULL },
+ { "from_param", PyCSimpleType_from_param, METH_O, from_param_doc },
+ { "from_address", CDataType_from_address, METH_O, from_address_doc },
+ { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
+ { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
+ { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
+ { NULL, NULL },
};
PyTypeObject PyCSimpleType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCSimpleType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "metatype for the PyCSimpleType Objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- PyCSimpleType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCSimpleType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCSimpleType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "metatype for the PyCSimpleType Objects", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ PyCSimpleType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCSimpleType_new, /* tp_new */
+ 0, /* tp_free */
};
/******************************************************************/
@@ -2104,217 +2104,217 @@ PyTypeObject PyCSimpleType_Type = {
static PyObject *
converters_from_argtypes(PyObject *ob)
{
- PyObject *converters;
- Py_ssize_t i;
- Py_ssize_t nArgs;
-
- ob = PySequence_Tuple(ob); /* new reference */
- if (!ob) {
- PyErr_SetString(PyExc_TypeError,
- "_argtypes_ must be a sequence of types");
- return NULL;
- }
-
- nArgs = PyTuple_GET_SIZE(ob);
- converters = PyTuple_New(nArgs);
- if (!converters)
- return NULL;
-
- /* I have to check if this is correct. Using c_char, which has a size
- of 1, will be assumed to be pushed as only one byte!
- Aren't these promoted to integers by the C compiler and pushed as 4 bytes?
- */
-
- for (i = 0; i < nArgs; ++i) {
- PyObject *tp = PyTuple_GET_ITEM(ob, i);
- PyObject *cnv = PyObject_GetAttrString(tp, "from_param");
- if (!cnv)
- goto argtypes_error_1;
- PyTuple_SET_ITEM(converters, i, cnv);
- }
- Py_DECREF(ob);
- return converters;
+ PyObject *converters;
+ Py_ssize_t i;
+ Py_ssize_t nArgs;
+
+ ob = PySequence_Tuple(ob); /* new reference */
+ if (!ob) {
+ PyErr_SetString(PyExc_TypeError,
+ "_argtypes_ must be a sequence of types");
+ return NULL;
+ }
+
+ nArgs = PyTuple_GET_SIZE(ob);
+ converters = PyTuple_New(nArgs);
+ if (!converters)
+ return NULL;
+
+ /* I have to check if this is correct. Using c_char, which has a size
+ of 1, will be assumed to be pushed as only one byte!
+ Aren't these promoted to integers by the C compiler and pushed as 4 bytes?
+ */
+
+ for (i = 0; i < nArgs; ++i) {
+ PyObject *tp = PyTuple_GET_ITEM(ob, i);
+ PyObject *cnv = PyObject_GetAttrString(tp, "from_param");
+ if (!cnv)
+ goto argtypes_error_1;
+ PyTuple_SET_ITEM(converters, i, cnv);
+ }
+ Py_DECREF(ob);
+ return converters;
argtypes_error_1:
- Py_XDECREF(converters);
- Py_DECREF(ob);
- PyErr_Format(PyExc_TypeError,
- "item %zd in _argtypes_ has no from_param method",
- i+1);
- return NULL;
+ Py_XDECREF(converters);
+ Py_DECREF(ob);
+ PyErr_Format(PyExc_TypeError,
+ "item %zd in _argtypes_ has no from_param method",
+ i+1);
+ return NULL;
}
static int
make_funcptrtype_dict(StgDictObject *stgdict)
{
- PyObject *ob;
- PyObject *converters = NULL;
-
- stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
- stgdict->length = 1;
- stgdict->size = sizeof(void *);
- stgdict->setfunc = NULL;
- stgdict->getfunc = NULL;
- stgdict->ffi_type_pointer = ffi_type_pointer;
-
- ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
- if (!ob || !PyLong_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "class must define _flags_ which must be an integer");
- return -1;
- }
- stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
-
- /* _argtypes_ is optional... */
- ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
- if (ob) {
- converters = converters_from_argtypes(ob);
- if (!converters)
- goto error;
- Py_INCREF(ob);
- stgdict->argtypes = ob;
- stgdict->converters = converters;
- }
-
- ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_");
- if (ob) {
- if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "_restype_ must be a type, a callable, or None");
- return -1;
- }
- Py_INCREF(ob);
- stgdict->restype = ob;
- stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_");
- if (stgdict->checker == NULL)
- PyErr_Clear();
- }
+ PyObject *ob;
+ PyObject *converters = NULL;
+
+ stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
+ stgdict->length = 1;
+ stgdict->size = sizeof(void *);
+ stgdict->setfunc = NULL;
+ stgdict->getfunc = NULL;
+ stgdict->ffi_type_pointer = ffi_type_pointer;
+
+ ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
+ if (!ob || !PyLong_Check(ob)) {
+ PyErr_SetString(PyExc_TypeError,
+ "class must define _flags_ which must be an integer");
+ return -1;
+ }
+ stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
+
+ /* _argtypes_ is optional... */
+ ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
+ if (ob) {
+ converters = converters_from_argtypes(ob);
+ if (!converters)
+ goto error;
+ Py_INCREF(ob);
+ stgdict->argtypes = ob;
+ stgdict->converters = converters;
+ }
+
+ ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_");
+ if (ob) {
+ if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
+ PyErr_SetString(PyExc_TypeError,
+ "_restype_ must be a type, a callable, or None");
+ return -1;
+ }
+ Py_INCREF(ob);
+ stgdict->restype = ob;
+ stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_");
+ if (stgdict->checker == NULL)
+ PyErr_Clear();
+ }
/* XXX later, maybe.
- ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_");
- if (ob) {
- if (!PyCallable_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "_errcheck_ must be callable");
- return -1;
- }
- Py_INCREF(ob);
- stgdict->errcheck = ob;
- }
+ ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_");
+ if (ob) {
+ if (!PyCallable_Check(ob)) {
+ PyErr_SetString(PyExc_TypeError,
+ "_errcheck_ must be callable");
+ return -1;
+ }
+ Py_INCREF(ob);
+ stgdict->errcheck = ob;
+ }
*/
- return 0;
+ return 0;
error:
- Py_XDECREF(converters);
- return -1;
+ Py_XDECREF(converters);
+ return -1;
}
static PyCArgObject *
PyCFuncPtrType_paramfunc(CDataObject *self)
{
- PyCArgObject *parg;
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = 'P';
- parg->pffi_type = &ffi_type_pointer;
- Py_INCREF(self);
- parg->obj = (PyObject *)self;
- parg->value.p = *(void **)self->b_ptr;
- return parg;
+ PyCArgObject *parg;
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = 'P';
+ parg->pffi_type = &ffi_type_pointer;
+ Py_INCREF(self);
+ parg->obj = (PyObject *)self;
+ parg->value.p = *(void **)self->b_ptr;
+ return parg;
}
static PyObject *
PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyTypeObject *result;
- StgDictObject *stgdict;
-
- stgdict = (StgDictObject *)PyObject_CallObject(
- (PyObject *)&PyCStgDict_Type, NULL);
- if (!stgdict)
- return NULL;
-
- stgdict->paramfunc = PyCFuncPtrType_paramfunc;
- /* We do NOT expose the function signature in the format string. It
- is impossible, generally, because the only requirement for the
- argtypes items is that they have a .from_param method - we do not
- know the types of the arguments (although, in practice, most
- argtypes would be a ctypes type).
- */
- stgdict->format = _ctypes_alloc_format_string(NULL, "X{}");
- stgdict->flags |= TYPEFLAG_ISPOINTER;
-
- /* create the new instance (which is a class,
- since we are a metatype!) */
- result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
- if (result == NULL) {
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
-
- /* replace the class dict by our updated storage dict */
- if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
- Py_DECREF(result);
- Py_DECREF((PyObject *)stgdict);
- return NULL;
- }
- Py_DECREF(result->tp_dict);
- result->tp_dict = (PyObject *)stgdict;
-
- if (-1 == make_funcptrtype_dict(stgdict)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return (PyObject *)result;
+ PyTypeObject *result;
+ StgDictObject *stgdict;
+
+ stgdict = (StgDictObject *)PyObject_CallObject(
+ (PyObject *)&PyCStgDict_Type, NULL);
+ if (!stgdict)
+ return NULL;
+
+ stgdict->paramfunc = PyCFuncPtrType_paramfunc;
+ /* We do NOT expose the function signature in the format string. It
+ is impossible, generally, because the only requirement for the
+ argtypes items is that they have a .from_param method - we do not
+ know the types of the arguments (although, in practice, most
+ argtypes would be a ctypes type).
+ */
+ stgdict->format = _ctypes_alloc_format_string(NULL, "X{}");
+ stgdict->flags |= TYPEFLAG_ISPOINTER;
+
+ /* create the new instance (which is a class,
+ since we are a metatype!) */
+ result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
+ if (result == NULL) {
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+
+ /* replace the class dict by our updated storage dict */
+ if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
+ Py_DECREF(result);
+ Py_DECREF((PyObject *)stgdict);
+ return NULL;
+ }
+ Py_DECREF(result->tp_dict);
+ result->tp_dict = (PyObject *)stgdict;
+
+ if (-1 == make_funcptrtype_dict(stgdict)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ return (PyObject *)result;
}
PyTypeObject PyCFuncPtrType_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCFuncPtrType", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &CDataType_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "metatype for C function pointers", /* tp_doc */
- (traverseproc)CDataType_traverse, /* tp_traverse */
- (inquiry)CDataType_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- CDataType_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCFuncPtrType_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCFuncPtrType", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &CDataType_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "metatype for C function pointers", /* tp_doc */
+ (traverseproc)CDataType_traverse, /* tp_traverse */
+ (inquiry)CDataType_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ CDataType_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCFuncPtrType_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/*****************************************************************
* Code to keep needed objects alive
*/
@@ -2322,46 +2322,46 @@ PyTypeObject PyCFuncPtrType_Type = {
static CDataObject *
PyCData_GetContainer(CDataObject *self)
{
- while (self->b_base)
- self = self->b_base;
- if (self->b_objects == NULL) {
- if (self->b_length) {
- self->b_objects = PyDict_New();
- } else {
- Py_INCREF(Py_None);
- self->b_objects = Py_None;
- }
- }
- return self;
+ while (self->b_base)
+ self = self->b_base;
+ if (self->b_objects == NULL) {
+ if (self->b_length) {
+ self->b_objects = PyDict_New();
+ } else {
+ Py_INCREF(Py_None);
+ self->b_objects = Py_None;
+ }
+ }
+ return self;
}
static PyObject *
GetKeepedObjects(CDataObject *target)
{
- return PyCData_GetContainer(target)->b_objects;
+ return PyCData_GetContainer(target)->b_objects;
}
static PyObject *
unique_key(CDataObject *target, Py_ssize_t index)
{
- char string[256];
- char *cp = string;
- size_t bytes_left;
-
- assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
- cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
- while (target->b_base) {
- bytes_left = sizeof(string) - (cp - string) - 1;
- /* Hex format needs 2 characters per byte */
- if (bytes_left < sizeof(Py_ssize_t) * 2) {
- PyErr_SetString(PyExc_ValueError,
- "ctypes object structure too deep");
- return NULL;
- }
- cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int));
- target = target->b_base;
- }
- return PyUnicode_FromStringAndSize(string, cp-string);
+ char string[256];
+ char *cp = string;
+ size_t bytes_left;
+
+ assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
+ cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
+ while (target->b_base) {
+ bytes_left = sizeof(string) - (cp - string) - 1;
+ /* Hex format needs 2 characters per byte */
+ if (bytes_left < sizeof(Py_ssize_t) * 2) {
+ PyErr_SetString(PyExc_ValueError,
+ "ctypes object structure too deep");
+ return NULL;
+ }
+ cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int));
+ target = target->b_base;
+ }
+ return PyUnicode_FromStringAndSize(string, cp-string);
}
/*
@@ -2385,30 +2385,30 @@ unique_key(CDataObject *target, Py_ssize_t index)
static int
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep)
{
- int result;
- CDataObject *ob;
- PyObject *key;
+ int result;
+ CDataObject *ob;
+ PyObject *key;
/* Optimization: no need to store None */
- if (keep == Py_None) {
- Py_DECREF(Py_None);
- return 0;
- }
- ob = PyCData_GetContainer(target);
- if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) {
- Py_XDECREF(ob->b_objects);
- ob->b_objects = keep; /* refcount consumed */
- return 0;
- }
- key = unique_key(target, index);
- if (key == NULL) {
- Py_DECREF(keep);
- return -1;
- }
- result = PyDict_SetItem(ob->b_objects, key, keep);
- Py_DECREF(key);
- Py_DECREF(keep);
- return result;
+ if (keep == Py_None) {
+ Py_DECREF(Py_None);
+ return 0;
+ }
+ ob = PyCData_GetContainer(target);
+ if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) {
+ Py_XDECREF(ob->b_objects);
+ ob->b_objects = keep; /* refcount consumed */
+ return 0;
+ }
+ key = unique_key(target, index);
+ if (key == NULL) {
+ Py_DECREF(keep);
+ return -1;
+ }
+ result = PyDict_SetItem(ob->b_objects, key, keep);
+ Py_DECREF(key);
+ Py_DECREF(keep);
+ return result;
}
/******************************************************************/
@@ -2418,75 +2418,75 @@ KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep)
static int
PyCData_traverse(CDataObject *self, visitproc visit, void *arg)
{
- Py_VISIT(self->b_objects);
- Py_VISIT((PyObject *)self->b_base);
- return 0;
+ Py_VISIT(self->b_objects);
+ Py_VISIT((PyObject *)self->b_base);
+ return 0;
}
static int
PyCData_clear(CDataObject *self)
{
- StgDictObject *dict = PyObject_stgdict((PyObject *)self);
- assert(dict); /* Cannot be NULL for CDataObject instances */
- Py_CLEAR(self->b_objects);
- if ((self->b_needsfree)
- && ((size_t)dict->size > sizeof(self->b_value)))
- PyMem_Free(self->b_ptr);
- self->b_ptr = NULL;
- Py_CLEAR(self->b_base);
- return 0;
+ StgDictObject *dict = PyObject_stgdict((PyObject *)self);
+ assert(dict); /* Cannot be NULL for CDataObject instances */
+ Py_CLEAR(self->b_objects);
+ if ((self->b_needsfree)
+ && ((size_t)dict->size > sizeof(self->b_value)))
+ PyMem_Free(self->b_ptr);
+ self->b_ptr = NULL;
+ Py_CLEAR(self->b_base);
+ return 0;
}
static void
PyCData_dealloc(PyObject *self)
{
- PyCData_clear((CDataObject *)self);
- Py_TYPE(self)->tp_free(self);
+ PyCData_clear((CDataObject *)self);
+ Py_TYPE(self)->tp_free(self);
}
static PyMemberDef PyCData_members[] = {
- { "_b_base_", T_OBJECT,
- offsetof(CDataObject, b_base), READONLY,
- "the base object" },
- { "_b_needsfree_", T_INT,
- offsetof(CDataObject, b_needsfree), READONLY,
- "whether the object owns the memory or not" },
- { "_objects", T_OBJECT,
- offsetof(CDataObject, b_objects), READONLY,
- "internal objects tree (NEVER CHANGE THIS OBJECT!)"},
- { NULL },
+ { "_b_base_", T_OBJECT,
+ offsetof(CDataObject, b_base), READONLY,
+ "the base object" },
+ { "_b_needsfree_", T_INT,
+ offsetof(CDataObject, b_needsfree), READONLY,
+ "whether the object owns the memory or not" },
+ { "_objects", T_OBJECT,
+ offsetof(CDataObject, b_objects), READONLY,
+ "internal objects tree (NEVER CHANGE THIS OBJECT!)"},
+ { NULL },
};
static int PyCData_NewGetBuffer(PyObject *_self, Py_buffer *view, int flags)
{
- CDataObject *self = (CDataObject *)_self;
- StgDictObject *dict = PyObject_stgdict(_self);
- Py_ssize_t i;
-
- if (view == NULL) return 0;
-
- view->buf = self->b_ptr;
- view->obj = _self;
- Py_INCREF(_self);
- view->len = self->b_size;
- view->readonly = 0;
- /* use default format character if not set */
- view->format = dict->format ? dict->format : "B";
- view->ndim = dict->ndim;
- view->shape = dict->shape;
- view->itemsize = self->b_size;
- for (i = 0; i < view->ndim; ++i) {
- view->itemsize /= dict->shape[i];
- }
- view->strides = NULL;
- view->suboffsets = NULL;
- view->internal = NULL;
- return 0;
+ CDataObject *self = (CDataObject *)_self;
+ StgDictObject *dict = PyObject_stgdict(_self);
+ Py_ssize_t i;
+
+ if (view == NULL) return 0;
+
+ view->buf = self->b_ptr;
+ view->obj = _self;
+ Py_INCREF(_self);
+ view->len = self->b_size;
+ view->readonly = 0;
+ /* use default format character if not set */
+ view->format = dict->format ? dict->format : "B";
+ view->ndim = dict->ndim;
+ view->shape = dict->shape;
+ view->itemsize = self->b_size;
+ for (i = 0; i < view->ndim; ++i) {
+ view->itemsize /= dict->shape[i];
+ }
+ view->strides = NULL;
+ view->suboffsets = NULL;
+ view->internal = NULL;
+ return 0;
}
static PyBufferProcs PyCData_as_buffer = {
- PyCData_NewGetBuffer,
- NULL,
+ PyCData_NewGetBuffer,
+ NULL,
};
/*
@@ -2495,47 +2495,47 @@ static PyBufferProcs PyCData_as_buffer = {
static long
PyCData_nohash(PyObject *self)
{
- PyErr_SetString(PyExc_TypeError, "unhashable type");
- return -1;
+ PyErr_SetString(PyExc_TypeError, "unhashable type");
+ return -1;
}
static PyObject *
PyCData_reduce(PyObject *_self, PyObject *args)
{
- CDataObject *self = (CDataObject *)_self;
-
- if (PyObject_stgdict(_self)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER)) {
- PyErr_SetString(PyExc_ValueError,
- "ctypes objects containing pointers cannot be pickled");
- return NULL;
- }
- return Py_BuildValue("O(O(NN))",
- _unpickle,
- Py_TYPE(_self),
- PyObject_GetAttrString(_self, "__dict__"),
- PyBytes_FromStringAndSize(self->b_ptr, self->b_size));
+ CDataObject *self = (CDataObject *)_self;
+
+ if (PyObject_stgdict(_self)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER)) {
+ PyErr_SetString(PyExc_ValueError,
+ "ctypes objects containing pointers cannot be pickled");
+ return NULL;
+ }
+ return Py_BuildValue("O(O(NN))",
+ _unpickle,
+ Py_TYPE(_self),
+ PyObject_GetAttrString(_self, "__dict__"),
+ PyBytes_FromStringAndSize(self->b_ptr, self->b_size));
}
static PyObject *
PyCData_setstate(PyObject *_self, PyObject *args)
{
- void *data;
- Py_ssize_t len;
- int res;
- PyObject *dict, *mydict;
- CDataObject *self = (CDataObject *)_self;
- if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len))
- return NULL;
- if (len > self->b_size)
- len = self->b_size;
- memmove(self->b_ptr, data, len);
- mydict = PyObject_GetAttrString(_self, "__dict__");
- res = PyDict_Update(mydict, dict);
- Py_DECREF(mydict);
- if (res == -1)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ void *data;
+ Py_ssize_t len;
+ int res;
+ PyObject *dict, *mydict;
+ CDataObject *self = (CDataObject *)_self;
+ if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len))
+ return NULL;
+ if (len > self->b_size)
+ len = self->b_size;
+ memmove(self->b_ptr, data, len);
+ mydict = PyObject_GetAttrString(_self, "__dict__");
+ res = PyDict_Update(mydict, dict);
+ Py_DECREF(mydict);
+ if (res == -1)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/*
@@ -2544,124 +2544,124 @@ PyCData_setstate(PyObject *_self, PyObject *args)
static PyObject *
PyCData_from_outparam(PyObject *self, PyObject *args)
{
- Py_INCREF(self);
- return self;
+ Py_INCREF(self);
+ return self;
}
static PyMethodDef PyCData_methods[] = {
- { "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, },
- { "__reduce__", PyCData_reduce, METH_NOARGS, },
- { "__setstate__", PyCData_setstate, METH_VARARGS, },
- { NULL, NULL },
+ { "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, },
+ { "__reduce__", PyCData_reduce, METH_NOARGS, },
+ { "__setstate__", PyCData_setstate, METH_VARARGS, },
+ { NULL, NULL },
};
PyTypeObject PyCData_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes._CData",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- PyCData_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- PyCData_nohash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "XXX to be provided", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- PyCData_methods, /* tp_methods */
- PyCData_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes._CData",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ PyCData_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ PyCData_nohash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "XXX to be provided", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ PyCData_methods, /* tp_methods */
+ PyCData_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
};
static int PyCData_MallocBuffer(CDataObject *obj, StgDictObject *dict)
{
- if ((size_t)dict->size <= sizeof(obj->b_value)) {
- /* No need to call malloc, can use the default buffer */
- obj->b_ptr = (char *)&obj->b_value;
- /* The b_needsfree flag does not mean that we actually did
- call PyMem_Malloc to allocate the memory block; instead it
- means we are the *owner* of the memory and are responsible
- for freeing resources associated with the memory. This is
- also the reason that b_needsfree is exposed to Python.
- */
- obj->b_needsfree = 1;
- } else {
- /* In python 2.4, and ctypes 0.9.6, the malloc call took about
- 33% of the creation time for c_int().
- */
- obj->b_ptr = (char *)PyMem_Malloc(dict->size);
- if (obj->b_ptr == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- obj->b_needsfree = 1;
- memset(obj->b_ptr, 0, dict->size);
- }
- obj->b_size = dict->size;
- return 0;
+ if ((size_t)dict->size <= sizeof(obj->b_value)) {
+ /* No need to call malloc, can use the default buffer */
+ obj->b_ptr = (char *)&obj->b_value;
+ /* The b_needsfree flag does not mean that we actually did
+ call PyMem_Malloc to allocate the memory block; instead it
+ means we are the *owner* of the memory and are responsible
+ for freeing resources associated with the memory. This is
+ also the reason that b_needsfree is exposed to Python.
+ */
+ obj->b_needsfree = 1;
+ } else {
+ /* In python 2.4, and ctypes 0.9.6, the malloc call took about
+ 33% of the creation time for c_int().
+ */
+ obj->b_ptr = (char *)PyMem_Malloc(dict->size);
+ if (obj->b_ptr == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ obj->b_needsfree = 1;
+ memset(obj->b_ptr, 0, dict->size);
+ }
+ obj->b_size = dict->size;
+ return 0;
}
PyObject *
PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr)
{
- CDataObject *cmem;
- StgDictObject *dict;
-
- assert(PyType_Check(type));
- dict = PyType_stgdict(type);
- if (!dict) {
- PyErr_SetString(PyExc_TypeError,
- "abstract class");
- return NULL;
- }
- dict->flags |= DICTFLAG_FINAL;
- cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
- if (cmem == NULL)
- return NULL;
- assert(CDataObject_Check(cmem));
-
- cmem->b_length = dict->length;
- cmem->b_size = dict->size;
- if (base) { /* use base's buffer */
- assert(CDataObject_Check(base));
- cmem->b_ptr = adr;
- cmem->b_needsfree = 0;
- Py_INCREF(base);
- cmem->b_base = (CDataObject *)base;
- cmem->b_index = index;
- } else { /* copy contents of adr */
- if (-1 == PyCData_MallocBuffer(cmem, dict)) {
- return NULL;
- Py_DECREF(cmem);
- }
- memcpy(cmem->b_ptr, adr, dict->size);
- cmem->b_index = index;
- }
- return (PyObject *)cmem;
+ CDataObject *cmem;
+ StgDictObject *dict;
+
+ assert(PyType_Check(type));
+ dict = PyType_stgdict(type);
+ if (!dict) {
+ PyErr_SetString(PyExc_TypeError,
+ "abstract class");
+ return NULL;
+ }
+ dict->flags |= DICTFLAG_FINAL;
+ cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
+ if (cmem == NULL)
+ return NULL;
+ assert(CDataObject_Check(cmem));
+
+ cmem->b_length = dict->length;
+ cmem->b_size = dict->size;
+ if (base) { /* use base's buffer */
+ assert(CDataObject_Check(base));
+ cmem->b_ptr = adr;
+ cmem->b_needsfree = 0;
+ Py_INCREF(base);
+ cmem->b_base = (CDataObject *)base;
+ cmem->b_index = index;
+ } else { /* copy contents of adr */
+ if (-1 == PyCData_MallocBuffer(cmem, dict)) {
+ return NULL;
+ Py_DECREF(cmem);
+ }
+ memcpy(cmem->b_ptr, adr, dict->size);
+ cmem->b_index = index;
+ }
+ return (PyObject *)cmem;
}
/*
@@ -2670,26 +2670,26 @@ PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr)
PyObject *
PyCData_AtAddress(PyObject *type, void *buf)
{
- CDataObject *pd;
- StgDictObject *dict;
-
- assert(PyType_Check(type));
- dict = PyType_stgdict(type);
- if (!dict) {
- PyErr_SetString(PyExc_TypeError,
- "abstract class");
- return NULL;
- }
- dict->flags |= DICTFLAG_FINAL;
-
- pd = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
- if (!pd)
- return NULL;
- assert(CDataObject_Check(pd));
- pd->b_ptr = (char *)buf;
- pd->b_length = dict->length;
- pd->b_size = dict->size;
- return (PyObject *)pd;
+ CDataObject *pd;
+ StgDictObject *dict;
+
+ assert(PyType_Check(type));
+ dict = PyType_stgdict(type);
+ if (!dict) {
+ PyErr_SetString(PyExc_TypeError,
+ "abstract class");
+ return NULL;
+ }
+ dict->flags |= DICTFLAG_FINAL;
+
+ pd = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
+ if (!pd)
+ return NULL;
+ assert(CDataObject_Check(pd));
+ pd->b_ptr = (char *)buf;
+ pd->b_length = dict->length;
+ pd->b_size = dict->size;
+ return (PyObject *)pd;
}
/*
@@ -2699,25 +2699,25 @@ PyCData_AtAddress(PyObject *type, void *buf)
*/
int _ctypes_simple_instance(PyObject *obj)
{
- PyTypeObject *type = (PyTypeObject *)obj;
+ PyTypeObject *type = (PyTypeObject *)obj;
- if (PyCSimpleTypeObject_Check(type))
- return type->tp_base != &Simple_Type;
- return 0;
+ if (PyCSimpleTypeObject_Check(type))
+ return type->tp_base != &Simple_Type;
+ return 0;
}
PyObject *
PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
- Py_ssize_t index, Py_ssize_t size, char *adr)
+ Py_ssize_t index, Py_ssize_t size, char *adr)
{
- StgDictObject *dict;
- if (getfunc)
- return getfunc(adr, size);
- assert(type);
- dict = PyType_stgdict(type);
- if (dict && dict->getfunc && !_ctypes_simple_instance(type))
- return dict->getfunc(adr, size);
- return PyCData_FromBaseObj(type, src, index, adr);
+ StgDictObject *dict;
+ if (getfunc)
+ return getfunc(adr, size);
+ assert(type);
+ dict = PyType_stgdict(type);
+ if (dict && dict->getfunc && !_ctypes_simple_instance(type))
+ return dict->getfunc(adr, size);
+ return PyCData_FromBaseObj(type, src, index, adr);
}
/*
@@ -2725,96 +2725,96 @@ PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
*/
static PyObject *
_PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
- Py_ssize_t size, char *ptr)
+ Py_ssize_t size, char *ptr)
{
- CDataObject *src;
-
- if (setfunc)
- return setfunc(ptr, value, size);
-
- if (!CDataObject_Check(value)) {
- StgDictObject *dict = PyType_stgdict(type);
- if (dict && dict->setfunc)
- return dict->setfunc(ptr, value, size);
- /*
- If value is a tuple, we try to call the type with the tuple
- and use the result!
- */
- assert(PyType_Check(type));
- if (PyTuple_Check(value)) {
- PyObject *ob;
- PyObject *result;
- ob = PyObject_CallObject(type, value);
- if (ob == NULL) {
- _ctypes_extend_error(PyExc_RuntimeError, "(%s) ",
- ((PyTypeObject *)type)->tp_name);
- return NULL;
- }
- result = _PyCData_set(dst, type, setfunc, ob,
- size, ptr);
- Py_DECREF(ob);
- return result;
- } else if (value == Py_None && PyCPointerTypeObject_Check(type)) {
- *(void **)ptr = NULL;
- Py_INCREF(Py_None);
- return Py_None;
- } else {
- PyErr_Format(PyExc_TypeError,
- "expected %s instance, got %s",
- ((PyTypeObject *)type)->tp_name,
- Py_TYPE(value)->tp_name);
- return NULL;
- }
- }
- src = (CDataObject *)value;
-
- if (PyObject_IsInstance(value, type)) {
- memcpy(ptr,
- src->b_ptr,
- size);
-
- if (PyCPointerTypeObject_Check(type))
- /* XXX */;
-
- value = GetKeepedObjects(src);
- Py_INCREF(value);
- return value;
- }
-
- if (PyCPointerTypeObject_Check(type)
- && ArrayObject_Check(value)) {
- StgDictObject *p1, *p2;
- PyObject *keep;
- p1 = PyObject_stgdict(value);
- assert(p1); /* Cannot be NULL for array instances */
- p2 = PyType_stgdict(type);
- assert(p2); /* Cannot be NULL for pointer types */
-
- if (p1->proto != p2->proto) {
- PyErr_Format(PyExc_TypeError,
- "incompatible types, %s instance instead of %s instance",
- Py_TYPE(value)->tp_name,
- ((PyTypeObject *)type)->tp_name);
- return NULL;
- }
- *(void **)ptr = src->b_ptr;
-
- keep = GetKeepedObjects(src);
- /*
- We are assigning an array object to a field which represents
- a pointer. This has the same effect as converting an array
- into a pointer. So, again, we have to keep the whole object
- pointed to (which is the array in this case) alive, and not
- only it's object list. So we create a tuple, containing
- b_objects list PLUS the array itself, and return that!
- */
- return PyTuple_Pack(2, keep, value);
- }
- PyErr_Format(PyExc_TypeError,
- "incompatible types, %s instance instead of %s instance",
- Py_TYPE(value)->tp_name,
- ((PyTypeObject *)type)->tp_name);
- return NULL;
+ CDataObject *src;
+
+ if (setfunc)
+ return setfunc(ptr, value, size);
+
+ if (!CDataObject_Check(value)) {
+ StgDictObject *dict = PyType_stgdict(type);
+ if (dict && dict->setfunc)
+ return dict->setfunc(ptr, value, size);
+ /*
+ If value is a tuple, we try to call the type with the tuple
+ and use the result!
+ */
+ assert(PyType_Check(type));
+ if (PyTuple_Check(value)) {
+ PyObject *ob;
+ PyObject *result;
+ ob = PyObject_CallObject(type, value);
+ if (ob == NULL) {
+ _ctypes_extend_error(PyExc_RuntimeError, "(%s) ",
+ ((PyTypeObject *)type)->tp_name);
+ return NULL;
+ }
+ result = _PyCData_set(dst, type, setfunc, ob,
+ size, ptr);
+ Py_DECREF(ob);
+ return result;
+ } else if (value == Py_None && PyCPointerTypeObject_Check(type)) {
+ *(void **)ptr = NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "expected %s instance, got %s",
+ ((PyTypeObject *)type)->tp_name,
+ Py_TYPE(value)->tp_name);
+ return NULL;
+ }
+ }
+ src = (CDataObject *)value;
+
+ if (PyObject_IsInstance(value, type)) {
+ memcpy(ptr,
+ src->b_ptr,
+ size);
+
+ if (PyCPointerTypeObject_Check(type))
+ /* XXX */;
+
+ value = GetKeepedObjects(src);
+ Py_INCREF(value);
+ return value;
+ }
+
+ if (PyCPointerTypeObject_Check(type)
+ && ArrayObject_Check(value)) {
+ StgDictObject *p1, *p2;
+ PyObject *keep;
+ p1 = PyObject_stgdict(value);
+ assert(p1); /* Cannot be NULL for array instances */
+ p2 = PyType_stgdict(type);
+ assert(p2); /* Cannot be NULL for pointer types */
+
+ if (p1->proto != p2->proto) {
+ PyErr_Format(PyExc_TypeError,
+ "incompatible types, %s instance instead of %s instance",
+ Py_TYPE(value)->tp_name,
+ ((PyTypeObject *)type)->tp_name);
+ return NULL;
+ }
+ *(void **)ptr = src->b_ptr;
+
+ keep = GetKeepedObjects(src);
+ /*
+ We are assigning an array object to a field which represents
+ a pointer. This has the same effect as converting an array
+ into a pointer. So, again, we have to keep the whole object
+ pointed to (which is the array in this case) alive, and not
+ only it's object list. So we create a tuple, containing
+ b_objects list PLUS the array itself, and return that!
+ */
+ return PyTuple_Pack(2, keep, value);
+ }
+ PyErr_Format(PyExc_TypeError,
+ "incompatible types, %s instance instead of %s instance",
+ Py_TYPE(value)->tp_name,
+ ((PyTypeObject *)type)->tp_name);
+ return NULL;
}
/*
@@ -2823,58 +2823,58 @@ _PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
*/
int
PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
- Py_ssize_t index, Py_ssize_t size, char *ptr)
+ Py_ssize_t index, Py_ssize_t size, char *ptr)
{
- CDataObject *mem = (CDataObject *)dst;
- PyObject *result;
-
- if (!CDataObject_Check(dst)) {
- PyErr_SetString(PyExc_TypeError,
- "not a ctype instance");
- return -1;
- }
-
- result = _PyCData_set(mem, type, setfunc, value,
- size, ptr);
- if (result == NULL)
- return -1;
-
- /* KeepRef steals a refcount from it's last argument */
- /* If KeepRef fails, we are stumped. The dst memory block has already
- been changed */
- return KeepRef(mem, index, result);
+ CDataObject *mem = (CDataObject *)dst;
+ PyObject *result;
+
+ if (!CDataObject_Check(dst)) {
+ PyErr_SetString(PyExc_TypeError,
+ "not a ctype instance");
+ return -1;
+ }
+
+ result = _PyCData_set(mem, type, setfunc, value,
+ size, ptr);
+ if (result == NULL)
+ return -1;
+
+ /* KeepRef steals a refcount from it's last argument */
+ /* If KeepRef fails, we are stumped. The dst memory block has already
+ been changed */
+ return KeepRef(mem, index, result);
}
-
+
/******************************************************************/
static PyObject *
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- CDataObject *obj;
- StgDictObject *dict;
-
- dict = PyType_stgdict((PyObject *)type);
- if (!dict) {
- PyErr_SetString(PyExc_TypeError,
- "abstract class");
- return NULL;
- }
- dict->flags |= DICTFLAG_FINAL;
-
- obj = (CDataObject *)type->tp_alloc(type, 0);
- if (!obj)
- return NULL;
-
- obj->b_base = NULL;
- obj->b_index = 0;
- obj->b_objects = NULL;
- obj->b_length = dict->length;
-
- if (-1 == PyCData_MallocBuffer(obj, dict)) {
- Py_DECREF(obj);
- return NULL;
- }
- return (PyObject *)obj;
+ CDataObject *obj;
+ StgDictObject *dict;
+
+ dict = PyType_stgdict((PyObject *)type);
+ if (!dict) {
+ PyErr_SetString(PyExc_TypeError,
+ "abstract class");
+ return NULL;
+ }
+ dict->flags |= DICTFLAG_FINAL;
+
+ obj = (CDataObject *)type->tp_alloc(type, 0);
+ if (!obj)
+ return NULL;
+
+ obj->b_base = NULL;
+ obj->b_index = 0;
+ obj->b_objects = NULL;
+ obj->b_length = dict->length;
+
+ if (-1 == PyCData_MallocBuffer(obj, dict)) {
+ Py_DECREF(obj);
+ return NULL;
+ }
+ return (PyObject *)obj;
}
/*****************************************************************/
/*
@@ -2884,165 +2884,165 @@ GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob)
{
- if (ob && !PyCallable_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "the errcheck attribute must be callable");
- return -1;
- }
- Py_XDECREF(self->errcheck);
- Py_XINCREF(ob);
- self->errcheck = ob;
- return 0;
+ if (ob && !PyCallable_Check(ob)) {
+ PyErr_SetString(PyExc_TypeError,
+ "the errcheck attribute must be callable");
+ return -1;
+ }
+ Py_XDECREF(self->errcheck);
+ Py_XINCREF(ob);
+ self->errcheck = ob;
+ return 0;
}
static PyObject *
PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self)
{
- if (self->errcheck) {
- Py_INCREF(self->errcheck);
- return self->errcheck;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ if (self->errcheck) {
+ Py_INCREF(self->errcheck);
+ return self->errcheck;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static int
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
{
- if (ob == NULL) {
- Py_XDECREF(self->restype);
- self->restype = NULL;
- Py_XDECREF(self->checker);
- self->checker = NULL;
- return 0;
- }
- if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
- PyErr_SetString(PyExc_TypeError,
- "restype must be a type, a callable, or None");
- return -1;
- }
- Py_XDECREF(self->checker);
- Py_XDECREF(self->restype);
- Py_INCREF(ob);
- self->restype = ob;
- self->checker = PyObject_GetAttrString(ob, "_check_retval_");
- if (self->checker == NULL)
- PyErr_Clear();
- return 0;
+ if (ob == NULL) {
+ Py_XDECREF(self->restype);
+ self->restype = NULL;
+ Py_XDECREF(self->checker);
+ self->checker = NULL;
+ return 0;
+ }
+ if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
+ PyErr_SetString(PyExc_TypeError,
+ "restype must be a type, a callable, or None");
+ return -1;
+ }
+ Py_XDECREF(self->checker);
+ Py_XDECREF(self->restype);
+ Py_INCREF(ob);
+ self->restype = ob;
+ self->checker = PyObject_GetAttrString(ob, "_check_retval_");
+ if (self->checker == NULL)
+ PyErr_Clear();
+ return 0;
}
static PyObject *
PyCFuncPtr_get_restype(PyCFuncPtrObject *self)
{
- StgDictObject *dict;
- if (self->restype) {
- Py_INCREF(self->restype);
- return self->restype;
- }
- dict = PyObject_stgdict((PyObject *)self);
- assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
- if (dict->restype) {
- Py_INCREF(dict->restype);
- return dict->restype;
- } else {
- Py_INCREF(Py_None);
- return Py_None;
- }
+ StgDictObject *dict;
+ if (self->restype) {
+ Py_INCREF(self->restype);
+ return self->restype;
+ }
+ dict = PyObject_stgdict((PyObject *)self);
+ assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
+ if (dict->restype) {
+ Py_INCREF(dict->restype);
+ return dict->restype;
+ } else {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
static int
PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob)
{
- PyObject *converters;
-
- if (ob == NULL || ob == Py_None) {
- Py_XDECREF(self->converters);
- self->converters = NULL;
- Py_XDECREF(self->argtypes);
- self->argtypes = NULL;
- } else {
- converters = converters_from_argtypes(ob);
- if (!converters)
- return -1;
- Py_XDECREF(self->converters);
- self->converters = converters;
- Py_XDECREF(self->argtypes);
- Py_INCREF(ob);
- self->argtypes = ob;
- }
- return 0;
+ PyObject *converters;
+
+ if (ob == NULL || ob == Py_None) {
+ Py_XDECREF(self->converters);
+ self->converters = NULL;
+ Py_XDECREF(self->argtypes);
+ self->argtypes = NULL;
+ } else {
+ converters = converters_from_argtypes(ob);
+ if (!converters)
+ return -1;
+ Py_XDECREF(self->converters);
+ self->converters = converters;
+ Py_XDECREF(self->argtypes);
+ Py_INCREF(ob);
+ self->argtypes = ob;
+ }
+ return 0;
}
static PyObject *
PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self)
{
- StgDictObject *dict;
- if (self->argtypes) {
- Py_INCREF(self->argtypes);
- return self->argtypes;
- }
- dict = PyObject_stgdict((PyObject *)self);
- assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
- if (dict->argtypes) {
- Py_INCREF(dict->argtypes);
- return dict->argtypes;
- } else {
- Py_INCREF(Py_None);
- return Py_None;
- }
+ StgDictObject *dict;
+ if (self->argtypes) {
+ Py_INCREF(self->argtypes);
+ return self->argtypes;
+ }
+ dict = PyObject_stgdict((PyObject *)self);
+ assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
+ if (dict->argtypes) {
+ Py_INCREF(dict->argtypes);
+ return dict->argtypes;
+ } else {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
static PyGetSetDef PyCFuncPtr_getsets[] = {
- { "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck,
- "a function to check for errors", NULL },
- { "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype,
- "specify the result type", NULL },
- { "argtypes", (getter)PyCFuncPtr_get_argtypes,
- (setter)PyCFuncPtr_set_argtypes,
- "specify the argument types", NULL },
- { NULL, NULL }
+ { "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck,
+ "a function to check for errors", NULL },
+ { "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype,
+ "specify the result type", NULL },
+ { "argtypes", (getter)PyCFuncPtr_get_argtypes,
+ (setter)PyCFuncPtr_set_argtypes,
+ "specify the argument types", NULL },
+ { NULL, NULL }
};
#ifdef MS_WIN32
static PPROC FindAddress(void *handle, char *name, PyObject *type)
{
#ifdef MS_WIN64
- /* win64 has no stdcall calling conv, so it should
- also not have the name mangling of it.
- */
- return (PPROC)GetProcAddress(handle, name);
+ /* win64 has no stdcall calling conv, so it should
+ also not have the name mangling of it.
+ */
+ return (PPROC)GetProcAddress(handle, name);
#else
- PPROC address;
- char *mangled_name;
- int i;
- StgDictObject *dict;
-
- address = (PPROC)GetProcAddress(handle, name);
- if (address)
- return address;
- if (((size_t)name & ~0xFFFF) == 0) {
- return NULL;
- }
-
- dict = PyType_stgdict((PyObject *)type);
- /* It should not happen that dict is NULL, but better be safe */
- if (dict==NULL || dict->flags & FUNCFLAG_CDECL)
- return address;
-
- /* for stdcall, try mangled names:
- funcname -> _funcname@<n>
- where n is 0, 4, 8, 12, ..., 128
- */
- mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */
- if (!mangled_name)
- return NULL;
- for (i = 0; i < 32; ++i) {
- sprintf(mangled_name, "_%s@%d", name, i*4);
- address = (PPROC)GetProcAddress(handle, mangled_name);
- if (address)
- return address;
- }
- return NULL;
+ PPROC address;
+ char *mangled_name;
+ int i;
+ StgDictObject *dict;
+
+ address = (PPROC)GetProcAddress(handle, name);
+ if (address)
+ return address;
+ if (((size_t)name & ~0xFFFF) == 0) {
+ return NULL;
+ }
+
+ dict = PyType_stgdict((PyObject *)type);
+ /* It should not happen that dict is NULL, but better be safe */
+ if (dict==NULL || dict->flags & FUNCFLAG_CDECL)
+ return address;
+
+ /* for stdcall, try mangled names:
+ funcname -> _funcname@<n>
+ where n is 0, 4, 8, 12, ..., 128
+ */
+ mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */
+ if (!mangled_name)
+ return NULL;
+ for (i = 0; i < 32; ++i) {
+ sprintf(mangled_name, "_%s@%d", name, i*4);
+ address = (PPROC)GetProcAddress(handle, mangled_name);
+ if (address)
+ return address;
+ }
+ return NULL;
#endif
}
#endif
@@ -3051,227 +3051,227 @@ static PPROC FindAddress(void *handle, char *name, PyObject *type)
static int
_check_outarg_type(PyObject *arg, Py_ssize_t index)
{
- StgDictObject *dict;
+ StgDictObject *dict;
- if (PyCPointerTypeObject_Check(arg))
- return 1;
+ if (PyCPointerTypeObject_Check(arg))
+ return 1;
- if (PyCArrayTypeObject_Check(arg))
- return 1;
+ if (PyCArrayTypeObject_Check(arg))
+ return 1;
- dict = PyType_stgdict(arg);
- if (dict
- /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
- && PyUnicode_Check(dict->proto)
+ dict = PyType_stgdict(arg);
+ if (dict
+ /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
+ && PyUnicode_Check(dict->proto)
/* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter type */
- && (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) {
- return 1;
- }
-
- PyErr_Format(PyExc_TypeError,
- "'out' parameter %d must be a pointer type, not %s",
- Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
- PyType_Check(arg) ?
- ((PyTypeObject *)arg)->tp_name :
- Py_TYPE(arg)->tp_name);
- return 0;
+ && (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) {
+ return 1;
+ }
+
+ PyErr_Format(PyExc_TypeError,
+ "'out' parameter %d must be a pointer type, not %s",
+ Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
+ PyType_Check(arg) ?
+ ((PyTypeObject *)arg)->tp_name :
+ Py_TYPE(arg)->tp_name);
+ return 0;
}
/* Returns 1 on success, 0 on error */
static int
_validate_paramflags(PyTypeObject *type, PyObject *paramflags)
{
- Py_ssize_t i, len;
- StgDictObject *dict;
- PyObject *argtypes;
-
- dict = PyType_stgdict((PyObject *)type);
- assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */
- argtypes = dict->argtypes;
-
- if (paramflags == NULL || dict->argtypes == NULL)
- return 1;
-
- if (!PyTuple_Check(paramflags)) {
- PyErr_SetString(PyExc_TypeError,
- "paramflags must be a tuple or None");
- return 0;
- }
-
- len = PyTuple_GET_SIZE(paramflags);
- if (len != PyTuple_GET_SIZE(dict->argtypes)) {
- PyErr_SetString(PyExc_ValueError,
- "paramflags must have the same length as argtypes");
- return 0;
- }
-
- for (i = 0; i < len; ++i) {
- PyObject *item = PyTuple_GET_ITEM(paramflags, i);
- int flag;
- char *name;
- PyObject *defval;
- PyObject *typ;
- if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
- PyErr_SetString(PyExc_TypeError,
- "paramflags must be a sequence of (int [,string [,value]]) tuples");
- return 0;
- }
- typ = PyTuple_GET_ITEM(argtypes, i);
- switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
- case 0:
- case PARAMFLAG_FIN:
- case PARAMFLAG_FIN | PARAMFLAG_FLCID:
- case PARAMFLAG_FIN | PARAMFLAG_FOUT:
- break;
- case PARAMFLAG_FOUT:
- if (!_check_outarg_type(typ, i+1))
- return 0;
- break;
- default:
- PyErr_Format(PyExc_TypeError,
- "paramflag value %d not supported",
- flag);
- return 0;
- }
- }
- return 1;
+ Py_ssize_t i, len;
+ StgDictObject *dict;
+ PyObject *argtypes;
+
+ dict = PyType_stgdict((PyObject *)type);
+ assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */
+ argtypes = dict->argtypes;
+
+ if (paramflags == NULL || dict->argtypes == NULL)
+ return 1;
+
+ if (!PyTuple_Check(paramflags)) {
+ PyErr_SetString(PyExc_TypeError,
+ "paramflags must be a tuple or None");
+ return 0;
+ }
+
+ len = PyTuple_GET_SIZE(paramflags);
+ if (len != PyTuple_GET_SIZE(dict->argtypes)) {
+ PyErr_SetString(PyExc_ValueError,
+ "paramflags must have the same length as argtypes");
+ return 0;
+ }
+
+ for (i = 0; i < len; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(paramflags, i);
+ int flag;
+ char *name;
+ PyObject *defval;
+ PyObject *typ;
+ if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
+ PyErr_SetString(PyExc_TypeError,
+ "paramflags must be a sequence of (int [,string [,value]]) tuples");
+ return 0;
+ }
+ typ = PyTuple_GET_ITEM(argtypes, i);
+ switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
+ case 0:
+ case PARAMFLAG_FIN:
+ case PARAMFLAG_FIN | PARAMFLAG_FLCID:
+ case PARAMFLAG_FIN | PARAMFLAG_FOUT:
+ break;
+ case PARAMFLAG_FOUT:
+ if (!_check_outarg_type(typ, i+1))
+ return 0;
+ break;
+ default:
+ PyErr_Format(PyExc_TypeError,
+ "paramflag value %d not supported",
+ flag);
+ return 0;
+ }
+ }
+ return 1;
}
static int
_get_name(PyObject *obj, char **pname)
{
#ifdef MS_WIN32
- if (PyLong_Check(obj)) {
- /* We have to use MAKEINTRESOURCEA for Windows CE.
- Works on Windows as well, of course.
- */
- *pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF);
- return 1;
- }
+ if (PyLong_Check(obj)) {
+ /* We have to use MAKEINTRESOURCEA for Windows CE.
+ Works on Windows as well, of course.
+ */
+ *pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF);
+ return 1;
+ }
#endif
- if (PyBytes_Check(obj)) {
- *pname = PyBytes_AS_STRING(obj);
- return *pname ? 1 : 0;
- }
- if (PyUnicode_Check(obj)) {
- *pname = _PyUnicode_AsString(obj);
- return *pname ? 1 : 0;
- }
- PyErr_SetString(PyExc_TypeError,
- "function name must be string or integer");
- return 0;
+ if (PyBytes_Check(obj)) {
+ *pname = PyBytes_AS_STRING(obj);
+ return *pname ? 1 : 0;
+ }
+ if (PyUnicode_Check(obj)) {
+ *pname = _PyUnicode_AsString(obj);
+ return *pname ? 1 : 0;
+ }
+ PyErr_SetString(PyExc_TypeError,
+ "function name must be string or integer");
+ return 0;
}
static PyObject *
PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- char *name;
- int (* address)(void);
- PyObject *dll;
- PyObject *obj;
- PyCFuncPtrObject *self;
- void *handle;
- PyObject *paramflags = NULL;
-
- if (!PyArg_ParseTuple(args, "(O&O)|O", _get_name, &name, &dll, &paramflags))
- return NULL;
- if (paramflags == Py_None)
- paramflags = NULL;
-
- obj = PyObject_GetAttrString(dll, "_handle");
- if (!obj)
- return NULL;
- if (!PyLong_Check(obj)) {
- PyErr_SetString(PyExc_TypeError,
- "the _handle attribute of the second argument must be an integer");
- Py_DECREF(obj);
- return NULL;
- }
- handle = (void *)PyLong_AsVoidPtr(obj);
- Py_DECREF(obj);
- if (PyErr_Occurred()) {
- PyErr_SetString(PyExc_ValueError,
- "could not convert the _handle attribute to a pointer");
- return NULL;
- }
+ char *name;
+ int (* address)(void);
+ PyObject *dll;
+ PyObject *obj;
+ PyCFuncPtrObject *self;
+ void *handle;
+ PyObject *paramflags = NULL;
+
+ if (!PyArg_ParseTuple(args, "(O&O)|O", _get_name, &name, &dll, &paramflags))
+ return NULL;
+ if (paramflags == Py_None)
+ paramflags = NULL;
+
+ obj = PyObject_GetAttrString(dll, "_handle");
+ if (!obj)
+ return NULL;
+ if (!PyLong_Check(obj)) {
+ PyErr_SetString(PyExc_TypeError,
+ "the _handle attribute of the second argument must be an integer");
+ Py_DECREF(obj);
+ return NULL;
+ }
+ handle = (void *)PyLong_AsVoidPtr(obj);
+ Py_DECREF(obj);
+ if (PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "could not convert the _handle attribute to a pointer");
+ return NULL;
+ }
#ifdef MS_WIN32
- address = FindAddress(handle, name, (PyObject *)type);
- if (!address) {
- if (!IS_INTRESOURCE(name))
- PyErr_Format(PyExc_AttributeError,
- "function '%s' not found",
- name);
- else
- PyErr_Format(PyExc_AttributeError,
- "function ordinal %d not found",
- (WORD)(size_t)name);
- return NULL;
- }
+ address = FindAddress(handle, name, (PyObject *)type);
+ if (!address) {
+ if (!IS_INTRESOURCE(name))
+ PyErr_Format(PyExc_AttributeError,
+ "function '%s' not found",
+ name);
+ else
+ PyErr_Format(PyExc_AttributeError,
+ "function ordinal %d not found",
+ (WORD)(size_t)name);
+ return NULL;
+ }
#else
- address = (PPROC)ctypes_dlsym(handle, name);
- if (!address) {
+ address = (PPROC)ctypes_dlsym(handle, name);
+ if (!address) {
#ifdef __CYGWIN__
/* dlerror() isn't very helpful on cygwin */
- PyErr_Format(PyExc_AttributeError,
- "function '%s' not found (%s) ",
- name);
+ PyErr_Format(PyExc_AttributeError,
+ "function '%s' not found (%s) ",
+ name);
#else
- PyErr_SetString(PyExc_AttributeError, ctypes_dlerror());
+ PyErr_SetString(PyExc_AttributeError, ctypes_dlerror());
#endif
- return NULL;
- }
+ return NULL;
+ }
#endif
- if (!_validate_paramflags(type, paramflags))
- return NULL;
+ if (!_validate_paramflags(type, paramflags))
+ return NULL;
- self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
- if (!self)
- return NULL;
+ self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
+ if (!self)
+ return NULL;
- Py_XINCREF(paramflags);
- self->paramflags = paramflags;
+ Py_XINCREF(paramflags);
+ self->paramflags = paramflags;
- *(void **)self->b_ptr = address;
+ *(void **)self->b_ptr = address;
- Py_INCREF((PyObject *)dll); /* for KeepRef */
- if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
- Py_DECREF((PyObject *)self);
- return NULL;
- }
+ Py_INCREF((PyObject *)dll); /* for KeepRef */
+ if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
+ Py_DECREF((PyObject *)self);
+ return NULL;
+ }
- Py_INCREF(self);
- self->callable = (PyObject *)self;
- return (PyObject *)self;
+ Py_INCREF(self);
+ self->callable = (PyObject *)self;
+ return (PyObject *)self;
}
#ifdef MS_WIN32
static PyObject *
PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyCFuncPtrObject *self;
- int index;
- char *name = NULL;
- PyObject *paramflags = NULL;
- GUID *iid = NULL;
- Py_ssize_t iid_len = 0;
-
- if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, &paramflags, &iid, &iid_len))
- return NULL;
- if (paramflags == Py_None)
- paramflags = NULL;
-
- if (!_validate_paramflags(type, paramflags))
- return NULL;
-
- self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
- self->index = index + 0x1000;
- Py_XINCREF(paramflags);
- self->paramflags = paramflags;
- if (iid_len == sizeof(GUID))
- self->iid = iid;
- return (PyObject *)self;
+ PyCFuncPtrObject *self;
+ int index;
+ char *name = NULL;
+ PyObject *paramflags = NULL;
+ GUID *iid = NULL;
+ Py_ssize_t iid_len = 0;
+
+ if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, &paramflags, &iid, &iid_len))
+ return NULL;
+ if (paramflags == Py_None)
+ paramflags = NULL;
+
+ if (!_validate_paramflags(type, paramflags))
+ return NULL;
+
+ self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
+ self->index = index + 0x1000;
+ Py_XINCREF(paramflags);
+ self->paramflags = paramflags;
+ if (iid_len == sizeof(GUID))
+ self->iid = iid;
+ return (PyObject *)self;
}
#endif
@@ -3291,90 +3291,90 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyCFuncPtrObject *self;
- PyObject *callable;
- StgDictObject *dict;
- CThunkObject *thunk;
+ PyCFuncPtrObject *self;
+ PyObject *callable;
+ StgDictObject *dict;
+ CThunkObject *thunk;
- if (PyTuple_GET_SIZE(args) == 0)
- return GenericPyCData_new(type, args, kwds);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return GenericPyCData_new(type, args, kwds);
- if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0)))
- return PyCFuncPtr_FromDll(type, args, kwds);
+ if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0)))
+ return PyCFuncPtr_FromDll(type, args, kwds);
#ifdef MS_WIN32
- if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0)))
- return PyCFuncPtr_FromVtblIndex(type, args, kwds);
+ if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0)))
+ return PyCFuncPtr_FromVtblIndex(type, args, kwds);
#endif
- if (1 == PyTuple_GET_SIZE(args)
- && (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
- CDataObject *ob;
- void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
- if (ptr == NULL && PyErr_Occurred())
- return NULL;
- ob = (CDataObject *)GenericPyCData_new(type, args, kwds);
- if (ob == NULL)
- return NULL;
- *(void **)ob->b_ptr = ptr;
- return (PyObject *)ob;
- }
-
- if (!PyArg_ParseTuple(args, "O", &callable))
- return NULL;
- if (!PyCallable_Check(callable)) {
- PyErr_SetString(PyExc_TypeError,
- "argument must be callable or integer function address");
- return NULL;
- }
-
- /* XXX XXX This would allow to pass additional options. For COM
- method *implementations*, we would probably want different
- behaviour than in 'normal' callback functions: return a HRESULT if
- an exception occurrs in the callback, and print the traceback not
- only on the console, but also to OutputDebugString() or something
- like that.
- */
+ if (1 == PyTuple_GET_SIZE(args)
+ && (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
+ CDataObject *ob;
+ void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
+ if (ptr == NULL && PyErr_Occurred())
+ return NULL;
+ ob = (CDataObject *)GenericPyCData_new(type, args, kwds);
+ if (ob == NULL)
+ return NULL;
+ *(void **)ob->b_ptr = ptr;
+ return (PyObject *)ob;
+ }
+
+ if (!PyArg_ParseTuple(args, "O", &callable))
+ return NULL;
+ if (!PyCallable_Check(callable)) {
+ PyErr_SetString(PyExc_TypeError,
+ "argument must be callable or integer function address");
+ return NULL;
+ }
+
+ /* XXX XXX This would allow to pass additional options. For COM
+ method *implementations*, we would probably want different
+ behaviour than in 'normal' callback functions: return a HRESULT if
+ an exception occurrs in the callback, and print the traceback not
+ only on the console, but also to OutputDebugString() or something
+ like that.
+ */
/*
- if (kwds && PyDict_GetItemString(kwds, "options")) {
- ...
- }
+ if (kwds && PyDict_GetItemString(kwds, "options")) {
+ ...
+ }
*/
- dict = PyType_stgdict((PyObject *)type);
- /* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */
- if (!dict || !dict->argtypes) {
- PyErr_SetString(PyExc_TypeError,
- "cannot construct instance of this class:"
- " no argtypes");
- return NULL;
- }
-
- thunk = _ctypes_alloc_callback(callable,
- dict->argtypes,
- dict->restype,
- dict->flags);
- if (!thunk)
- return NULL;
-
- self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
- if (self == NULL) {
- Py_DECREF(thunk);
- return NULL;
- }
-
- Py_INCREF(callable);
- self->callable = callable;
-
- self->thunk = thunk;
- *(void **)self->b_ptr = (void *)thunk->pcl;
-
- Py_INCREF((PyObject *)thunk); /* for KeepRef */
- if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
- Py_DECREF((PyObject *)self);
- return NULL;
- }
- return (PyObject *)self;
+ dict = PyType_stgdict((PyObject *)type);
+ /* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */
+ if (!dict || !dict->argtypes) {
+ PyErr_SetString(PyExc_TypeError,
+ "cannot construct instance of this class:"
+ " no argtypes");
+ return NULL;
+ }
+
+ thunk = _ctypes_alloc_callback(callable,
+ dict->argtypes,
+ dict->restype,
+ dict->flags);
+ if (!thunk)
+ return NULL;
+
+ self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
+ if (self == NULL) {
+ Py_DECREF(thunk);
+ return NULL;
+ }
+
+ Py_INCREF(callable);
+ self->callable = callable;
+
+ self->thunk = thunk;
+ *(void **)self->b_ptr = (void *)thunk->pcl;
+
+ Py_INCREF((PyObject *)thunk); /* for KeepRef */
+ if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
+ Py_DECREF((PyObject *)self);
+ return NULL;
+ }
+ return (PyObject *)self;
}
@@ -3384,54 +3384,54 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
_byref(PyObject *obj)
{
- PyCArgObject *parg;
- if (!CDataObject_Check(obj)) {
- PyErr_SetString(PyExc_TypeError,
- "expected CData instance");
- return NULL;
- }
-
- parg = PyCArgObject_new();
- if (parg == NULL) {
- Py_DECREF(obj);
- return NULL;
- }
-
- parg->tag = 'P';
- parg->pffi_type = &ffi_type_pointer;
- parg->obj = obj;
- parg->value.p = ((CDataObject *)obj)->b_ptr;
- return (PyObject *)parg;
+ PyCArgObject *parg;
+ if (!CDataObject_Check(obj)) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected CData instance");
+ return NULL;
+ }
+
+ parg = PyCArgObject_new();
+ if (parg == NULL) {
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ parg->tag = 'P';
+ parg->pffi_type = &ffi_type_pointer;
+ parg->obj = obj;
+ parg->value.p = ((CDataObject *)obj)->b_ptr;
+ return (PyObject *)parg;
}
static PyObject *
_get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds)
{
- PyObject *v;
-
- if (*pindex < PyTuple_GET_SIZE(inargs)) {
- v = PyTuple_GET_ITEM(inargs, *pindex);
- ++*pindex;
- Py_INCREF(v);
- return v;
- }
- if (kwds && (v = PyDict_GetItem(kwds, name))) {
- ++*pindex;
- Py_INCREF(v);
- return v;
- }
- if (defval) {
- Py_INCREF(defval);
- return defval;
- }
- /* we can't currently emit a better error message */
- if (name)
- PyErr_Format(PyExc_TypeError,
- "required argument '%S' missing", name);
- else
- PyErr_Format(PyExc_TypeError,
- "not enough arguments");
- return NULL;
+ PyObject *v;
+
+ if (*pindex < PyTuple_GET_SIZE(inargs)) {
+ v = PyTuple_GET_ITEM(inargs, *pindex);
+ ++*pindex;
+ Py_INCREF(v);
+ return v;
+ }
+ if (kwds && (v = PyDict_GetItem(kwds, name))) {
+ ++*pindex;
+ Py_INCREF(v);
+ return v;
+ }
+ if (defval) {
+ Py_INCREF(defval);
+ return defval;
+ }
+ /* we can't currently emit a better error message */
+ if (name)
+ PyErr_Format(PyExc_TypeError,
+ "required argument '%S' missing", name);
+ else
+ PyErr_Format(PyExc_TypeError,
+ "not enough arguments");
+ return NULL;
}
/*
@@ -3454,166 +3454,166 @@ _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje
*/
static PyObject *
_build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
- PyObject *inargs, PyObject *kwds,
- int *poutmask, int *pinoutmask, unsigned int *pnumretvals)
+ PyObject *inargs, PyObject *kwds,
+ int *poutmask, int *pinoutmask, unsigned int *pnumretvals)
{
- PyObject *paramflags = self->paramflags;
- PyObject *callargs;
- StgDictObject *dict;
- Py_ssize_t i, len;
- int inargs_index = 0;
- /* It's a little bit difficult to determine how many arguments the
- function call requires/accepts. For simplicity, we count the consumed
- args and compare this to the number of supplied args. */
- Py_ssize_t actual_args;
-
- *poutmask = 0;
- *pinoutmask = 0;
- *pnumretvals = 0;
-
- /* Trivial cases, where we either return inargs itself, or a slice of it. */
- if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) {
+ PyObject *paramflags = self->paramflags;
+ PyObject *callargs;
+ StgDictObject *dict;
+ Py_ssize_t i, len;
+ int inargs_index = 0;
+ /* It's a little bit difficult to determine how many arguments the
+ function call requires/accepts. For simplicity, we count the consumed
+ args and compare this to the number of supplied args. */
+ Py_ssize_t actual_args;
+
+ *poutmask = 0;
+ *pinoutmask = 0;
+ *pnumretvals = 0;
+
+ /* Trivial cases, where we either return inargs itself, or a slice of it. */
+ if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) {
#ifdef MS_WIN32
- if (self->index)
- return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs));
+ if (self->index)
+ return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs));
#endif
- Py_INCREF(inargs);
- return inargs;
- }
+ Py_INCREF(inargs);
+ return inargs;
+ }
- len = PyTuple_GET_SIZE(argtypes);
- callargs = PyTuple_New(len); /* the argument tuple we build */
- if (callargs == NULL)
- return NULL;
+ len = PyTuple_GET_SIZE(argtypes);
+ callargs = PyTuple_New(len); /* the argument tuple we build */
+ if (callargs == NULL)
+ return NULL;
#ifdef MS_WIN32
- /* For a COM method, skip the first arg */
- if (self->index) {
- inargs_index = 1;
- }
+ /* For a COM method, skip the first arg */
+ if (self->index) {
+ inargs_index = 1;
+ }
#endif
- for (i = 0; i < len; ++i) {
- PyObject *item = PyTuple_GET_ITEM(paramflags, i);
- PyObject *ob;
- int flag;
- PyObject *name = NULL;
- PyObject *defval = NULL;
-
- /* This way seems to be ~2 us faster than the PyArg_ParseTuple
- calls below. */
- /* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
- Py_ssize_t tsize = PyTuple_GET_SIZE(item);
- flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
- name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
- defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
-
- switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
- case PARAMFLAG_FIN | PARAMFLAG_FLCID:
- /* ['in', 'lcid'] parameter. Always taken from defval,
- if given, else the integer 0. */
- if (defval == NULL) {
- defval = PyLong_FromLong(0);
- if (defval == NULL)
- goto error;
- } else
- Py_INCREF(defval);
- PyTuple_SET_ITEM(callargs, i, defval);
- break;
- case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
- *pinoutmask |= (1 << i); /* mark as inout arg */
- (*pnumretvals)++;
- /* fall through to PARAMFLAG_FIN... */
- case 0:
- case PARAMFLAG_FIN:
- /* 'in' parameter. Copy it from inargs. */
- ob =_get_arg(&inargs_index, name, defval, inargs, kwds);
- if (ob == NULL)
- goto error;
- PyTuple_SET_ITEM(callargs, i, ob);
- break;
- case PARAMFLAG_FOUT:
- /* XXX Refactor this code into a separate function. */
- /* 'out' parameter.
- argtypes[i] must be a POINTER to a c type.
-
- Cannot by supplied in inargs, but a defval will be used
- if available. XXX Should we support getting it from kwds?
- */
- if (defval) {
- /* XXX Using mutable objects as defval will
- make the function non-threadsafe, unless we
- copy the object in each invocation */
- Py_INCREF(defval);
- PyTuple_SET_ITEM(callargs, i, defval);
- *poutmask |= (1 << i); /* mark as out arg */
- (*pnumretvals)++;
- break;
- }
- ob = PyTuple_GET_ITEM(argtypes, i);
- dict = PyType_stgdict(ob);
- if (dict == NULL) {
- /* Cannot happen: _validate_paramflags()
- would not accept such an object */
- PyErr_Format(PyExc_RuntimeError,
- "NULL stgdict unexpected");
- goto error;
- }
- if (PyUnicode_Check(dict->proto)) {
- PyErr_Format(
- PyExc_TypeError,
- "%s 'out' parameter must be passed as default value",
- ((PyTypeObject *)ob)->tp_name);
- goto error;
- }
- if (PyCArrayTypeObject_Check(ob))
- ob = PyObject_CallObject(ob, NULL);
- else
- /* Create an instance of the pointed-to type */
- ob = PyObject_CallObject(dict->proto, NULL);
- /*
- XXX Is the following correct any longer?
- We must not pass a byref() to the array then but
- the array instance itself. Then, we cannot retrive
- the result from the PyCArgObject.
- */
- if (ob == NULL)
- goto error;
- /* The .from_param call that will ocurr later will pass this
- as a byref parameter. */
- PyTuple_SET_ITEM(callargs, i, ob);
- *poutmask |= (1 << i); /* mark as out arg */
- (*pnumretvals)++;
- break;
- default:
- PyErr_Format(PyExc_ValueError,
- "paramflag %d not yet implemented", flag);
- goto error;
- break;
- }
- }
-
- /* We have counted the arguments we have consumed in 'inargs_index'. This
- must be the same as len(inargs) + len(kwds), otherwise we have
- either too much or not enough arguments. */
-
- actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
- if (actual_args != inargs_index) {
- /* When we have default values or named parameters, this error
- message is misleading. See unittests/test_paramflags.py
- */
- PyErr_Format(PyExc_TypeError,
- "call takes exactly %d arguments (%zd given)",
- inargs_index, actual_args);
- goto error;
- }
-
- /* outmask is a bitmask containing indexes into callargs. Items at
- these indexes contain values to return.
- */
- return callargs;
+ for (i = 0; i < len; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(paramflags, i);
+ PyObject *ob;
+ int flag;
+ PyObject *name = NULL;
+ PyObject *defval = NULL;
+
+ /* This way seems to be ~2 us faster than the PyArg_ParseTuple
+ calls below. */
+ /* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
+ Py_ssize_t tsize = PyTuple_GET_SIZE(item);
+ flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
+ name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
+ defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
+
+ switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
+ case PARAMFLAG_FIN | PARAMFLAG_FLCID:
+ /* ['in', 'lcid'] parameter. Always taken from defval,
+ if given, else the integer 0. */
+ if (defval == NULL) {
+ defval = PyLong_FromLong(0);
+ if (defval == NULL)
+ goto error;
+ } else
+ Py_INCREF(defval);
+ PyTuple_SET_ITEM(callargs, i, defval);
+ break;
+ case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
+ *pinoutmask |= (1 << i); /* mark as inout arg */
+ (*pnumretvals)++;
+ /* fall through to PARAMFLAG_FIN... */
+ case 0:
+ case PARAMFLAG_FIN:
+ /* 'in' parameter. Copy it from inargs. */
+ ob =_get_arg(&inargs_index, name, defval, inargs, kwds);
+ if (ob == NULL)
+ goto error;
+ PyTuple_SET_ITEM(callargs, i, ob);
+ break;
+ case PARAMFLAG_FOUT:
+ /* XXX Refactor this code into a separate function. */
+ /* 'out' parameter.
+ argtypes[i] must be a POINTER to a c type.
+
+ Cannot by supplied in inargs, but a defval will be used
+ if available. XXX Should we support getting it from kwds?
+ */
+ if (defval) {
+ /* XXX Using mutable objects as defval will
+ make the function non-threadsafe, unless we
+ copy the object in each invocation */
+ Py_INCREF(defval);
+ PyTuple_SET_ITEM(callargs, i, defval);
+ *poutmask |= (1 << i); /* mark as out arg */
+ (*pnumretvals)++;
+ break;
+ }
+ ob = PyTuple_GET_ITEM(argtypes, i);
+ dict = PyType_stgdict(ob);
+ if (dict == NULL) {
+ /* Cannot happen: _validate_paramflags()
+ would not accept such an object */
+ PyErr_Format(PyExc_RuntimeError,
+ "NULL stgdict unexpected");
+ goto error;
+ }
+ if (PyUnicode_Check(dict->proto)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "%s 'out' parameter must be passed as default value",
+ ((PyTypeObject *)ob)->tp_name);
+ goto error;
+ }
+ if (PyCArrayTypeObject_Check(ob))
+ ob = PyObject_CallObject(ob, NULL);
+ else
+ /* Create an instance of the pointed-to type */
+ ob = PyObject_CallObject(dict->proto, NULL);
+ /*
+ XXX Is the following correct any longer?
+ We must not pass a byref() to the array then but
+ the array instance itself. Then, we cannot retrive
+ the result from the PyCArgObject.
+ */
+ if (ob == NULL)
+ goto error;
+ /* The .from_param call that will ocurr later will pass this
+ as a byref parameter. */
+ PyTuple_SET_ITEM(callargs, i, ob);
+ *poutmask |= (1 << i); /* mark as out arg */
+ (*pnumretvals)++;
+ break;
+ default:
+ PyErr_Format(PyExc_ValueError,
+ "paramflag %d not yet implemented", flag);
+ goto error;
+ break;
+ }
+ }
+
+ /* We have counted the arguments we have consumed in 'inargs_index'. This
+ must be the same as len(inargs) + len(kwds), otherwise we have
+ either too much or not enough arguments. */
+
+ actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
+ if (actual_args != inargs_index) {
+ /* When we have default values or named parameters, this error
+ message is misleading. See unittests/test_paramflags.py
+ */
+ PyErr_Format(PyExc_TypeError,
+ "call takes exactly %d arguments (%zd given)",
+ inargs_index, actual_args);
+ goto error;
+ }
+
+ /* outmask is a bitmask containing indexes into callargs. Items at
+ these indexes contain values to return.
+ */
+ return callargs;
error:
- Py_DECREF(callargs);
- return NULL;
+ Py_DECREF(callargs);
+ return NULL;
}
/* See also:
@@ -3626,307 +3626,307 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
*/
static PyObject *
_build_result(PyObject *result, PyObject *callargs,
- int outmask, int inoutmask, unsigned int numretvals)
+ int outmask, int inoutmask, unsigned int numretvals)
{
- unsigned int i, index;
- int bit;
- PyObject *tup = NULL;
-
- if (callargs == NULL)
- return result;
- if (result == NULL || numretvals == 0) {
- Py_DECREF(callargs);
- return result;
- }
- Py_DECREF(result);
-
- /* tup will not be allocated if numretvals == 1 */
- /* allocate tuple to hold the result */
- if (numretvals > 1) {
- tup = PyTuple_New(numretvals);
- if (tup == NULL) {
- Py_DECREF(callargs);
- return NULL;
- }
- }
-
- index = 0;
- for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) {
- PyObject *v;
- if (bit & inoutmask) {
- v = PyTuple_GET_ITEM(callargs, i);
- Py_INCREF(v);
- if (numretvals == 1) {
- Py_DECREF(callargs);
- return v;
- }
- PyTuple_SET_ITEM(tup, index, v);
- index++;
- } else if (bit & outmask) {
- v = PyTuple_GET_ITEM(callargs, i);
- v = PyObject_CallMethod(v, "__ctypes_from_outparam__", NULL);
- if (v == NULL || numretvals == 1) {
- Py_DECREF(callargs);
- return v;
- }
- PyTuple_SET_ITEM(tup, index, v);
- index++;
- }
- if (index == numretvals)
- break;
- }
-
- Py_DECREF(callargs);
- return tup;
+ unsigned int i, index;
+ int bit;
+ PyObject *tup = NULL;
+
+ if (callargs == NULL)
+ return result;
+ if (result == NULL || numretvals == 0) {
+ Py_DECREF(callargs);
+ return result;
+ }
+ Py_DECREF(result);
+
+ /* tup will not be allocated if numretvals == 1 */
+ /* allocate tuple to hold the result */
+ if (numretvals > 1) {
+ tup = PyTuple_New(numretvals);
+ if (tup == NULL) {
+ Py_DECREF(callargs);
+ return NULL;
+ }
+ }
+
+ index = 0;
+ for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) {
+ PyObject *v;
+ if (bit & inoutmask) {
+ v = PyTuple_GET_ITEM(callargs, i);
+ Py_INCREF(v);
+ if (numretvals == 1) {
+ Py_DECREF(callargs);
+ return v;
+ }
+ PyTuple_SET_ITEM(tup, index, v);
+ index++;
+ } else if (bit & outmask) {
+ v = PyTuple_GET_ITEM(callargs, i);
+ v = PyObject_CallMethod(v, "__ctypes_from_outparam__", NULL);
+ if (v == NULL || numretvals == 1) {
+ Py_DECREF(callargs);
+ return v;
+ }
+ PyTuple_SET_ITEM(tup, index, v);
+ index++;
+ }
+ if (index == numretvals)
+ break;
+ }
+
+ Py_DECREF(callargs);
+ return tup;
}
static PyObject *
PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
{
- PyObject *restype;
- PyObject *converters;
- PyObject *checker;
- PyObject *argtypes;
- StgDictObject *dict = PyObject_stgdict((PyObject *)self);
- PyObject *result;
- PyObject *callargs;
- PyObject *errcheck;
+ PyObject *restype;
+ PyObject *converters;
+ PyObject *checker;
+ PyObject *argtypes;
+ StgDictObject *dict = PyObject_stgdict((PyObject *)self);
+ PyObject *result;
+ PyObject *callargs;
+ PyObject *errcheck;
#ifdef MS_WIN32
- IUnknown *piunk = NULL;
+ IUnknown *piunk = NULL;
#endif
- void *pProc = NULL;
+ void *pProc = NULL;
- int inoutmask;
- int outmask;
- unsigned int numretvals;
+ int inoutmask;
+ int outmask;
+ unsigned int numretvals;
- assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
- restype = self->restype ? self->restype : dict->restype;
- converters = self->converters ? self->converters : dict->converters;
- checker = self->checker ? self->checker : dict->checker;
- argtypes = self->argtypes ? self->argtypes : dict->argtypes;
+ assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
+ restype = self->restype ? self->restype : dict->restype;
+ converters = self->converters ? self->converters : dict->converters;
+ checker = self->checker ? self->checker : dict->checker;
+ argtypes = self->argtypes ? self->argtypes : dict->argtypes;
/* later, we probably want to have an errcheck field in stgdict */
- errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */;
+ errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */;
- pProc = *(void **)self->b_ptr;
+ pProc = *(void **)self->b_ptr;
#ifdef MS_WIN32
- if (self->index) {
- /* It's a COM method */
- CDataObject *this;
- this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */
- if (!this) {
- PyErr_SetString(PyExc_ValueError,
- "native com method call without 'this' parameter");
- return NULL;
- }
- if (!CDataObject_Check(this)) {
- PyErr_SetString(PyExc_TypeError,
- "Expected a COM this pointer as first argument");
- return NULL;
- }
- /* there should be more checks? No, in Python */
- /* First arg is an pointer to an interface instance */
- if (!this->b_ptr || *(void **)this->b_ptr == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "NULL COM pointer access");
- return NULL;
- }
- piunk = *(IUnknown **)this->b_ptr;
- if (NULL == piunk->lpVtbl) {
- PyErr_SetString(PyExc_ValueError,
- "COM method call without VTable");
- return NULL;
- }
- pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000];
- }
+ if (self->index) {
+ /* It's a COM method */
+ CDataObject *this;
+ this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */
+ if (!this) {
+ PyErr_SetString(PyExc_ValueError,
+ "native com method call without 'this' parameter");
+ return NULL;
+ }
+ if (!CDataObject_Check(this)) {
+ PyErr_SetString(PyExc_TypeError,
+ "Expected a COM this pointer as first argument");
+ return NULL;
+ }
+ /* there should be more checks? No, in Python */
+ /* First arg is an pointer to an interface instance */
+ if (!this->b_ptr || *(void **)this->b_ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "NULL COM pointer access");
+ return NULL;
+ }
+ piunk = *(IUnknown **)this->b_ptr;
+ if (NULL == piunk->lpVtbl) {
+ PyErr_SetString(PyExc_ValueError,
+ "COM method call without VTable");
+ return NULL;
+ }
+ pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000];
+ }
#endif
- callargs = _build_callargs(self, argtypes,
- inargs, kwds,
- &outmask, &inoutmask, &numretvals);
- if (callargs == NULL)
- return NULL;
-
- if (converters) {
- int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters),
- Py_ssize_t, int);
- int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs),
- Py_ssize_t, int);
-
- if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) {
- /* For cdecl functions, we allow more actual arguments
- than the length of the argtypes tuple.
- */
- if (required > actual) {
- Py_DECREF(callargs);
- PyErr_Format(PyExc_TypeError,
- "this function takes at least %d argument%s (%d given)",
- required,
- required == 1 ? "" : "s",
- actual);
- return NULL;
- }
- } else if (required != actual) {
- Py_DECREF(callargs);
- PyErr_Format(PyExc_TypeError,
- "this function takes %d argument%s (%d given)",
- required,
- required == 1 ? "" : "s",
- actual);
- return NULL;
- }
- }
-
- result = _ctypes_callproc(pProc,
- callargs,
+ callargs = _build_callargs(self, argtypes,
+ inargs, kwds,
+ &outmask, &inoutmask, &numretvals);
+ if (callargs == NULL)
+ return NULL;
+
+ if (converters) {
+ int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters),
+ Py_ssize_t, int);
+ int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs),
+ Py_ssize_t, int);
+
+ if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) {
+ /* For cdecl functions, we allow more actual arguments
+ than the length of the argtypes tuple.
+ */
+ if (required > actual) {
+ Py_DECREF(callargs);
+ PyErr_Format(PyExc_TypeError,
+ "this function takes at least %d argument%s (%d given)",
+ required,
+ required == 1 ? "" : "s",
+ actual);
+ return NULL;
+ }
+ } else if (required != actual) {
+ Py_DECREF(callargs);
+ PyErr_Format(PyExc_TypeError,
+ "this function takes %d argument%s (%d given)",
+ required,
+ required == 1 ? "" : "s",
+ actual);
+ return NULL;
+ }
+ }
+
+ result = _ctypes_callproc(pProc,
+ callargs,
#ifdef MS_WIN32
- piunk,
- self->iid,
+ piunk,
+ self->iid,
#endif
- dict->flags,
- converters,
- restype,
- checker);
+ dict->flags,
+ converters,
+ restype,
+ checker);
/* The 'errcheck' protocol */
- if (result != NULL && errcheck) {
- PyObject *v = PyObject_CallFunctionObjArgs(errcheck,
- result,
- self,
- callargs,
- NULL);
- /* If the errcheck funtion failed, return NULL.
- If the errcheck function returned callargs unchanged,
- continue normal processing.
- If the errcheck function returned something else,
- use that as result.
- */
- if (v == NULL || v != callargs) {
- Py_DECREF(result);
- Py_DECREF(callargs);
- return v;
- }
- Py_DECREF(v);
- }
-
- return _build_result(result, callargs,
- outmask, inoutmask, numretvals);
+ if (result != NULL && errcheck) {
+ PyObject *v = PyObject_CallFunctionObjArgs(errcheck,
+ result,
+ self,
+ callargs,
+ NULL);
+ /* If the errcheck funtion failed, return NULL.
+ If the errcheck function returned callargs unchanged,
+ continue normal processing.
+ If the errcheck function returned something else,
+ use that as result.
+ */
+ if (v == NULL || v != callargs) {
+ Py_DECREF(result);
+ Py_DECREF(callargs);
+ return v;
+ }
+ Py_DECREF(v);
+ }
+
+ return _build_result(result, callargs,
+ outmask, inoutmask, numretvals);
}
static int
PyCFuncPtr_traverse(PyCFuncPtrObject *self, visitproc visit, void *arg)
{
- Py_VISIT(self->callable);
- Py_VISIT(self->restype);
- Py_VISIT(self->checker);
- Py_VISIT(self->errcheck);
- Py_VISIT(self->argtypes);
- Py_VISIT(self->converters);
- Py_VISIT(self->paramflags);
- Py_VISIT(self->thunk);
- return PyCData_traverse((CDataObject *)self, visit, arg);
+ Py_VISIT(self->callable);
+ Py_VISIT(self->restype);
+ Py_VISIT(self->checker);
+ Py_VISIT(self->errcheck);
+ Py_VISIT(self->argtypes);
+ Py_VISIT(self->converters);
+ Py_VISIT(self->paramflags);
+ Py_VISIT(self->thunk);
+ return PyCData_traverse((CDataObject *)self, visit, arg);
}
static int
PyCFuncPtr_clear(PyCFuncPtrObject *self)
{
- Py_CLEAR(self->callable);
- Py_CLEAR(self->restype);
- Py_CLEAR(self->checker);
- Py_CLEAR(self->errcheck);
- Py_CLEAR(self->argtypes);
- Py_CLEAR(self->converters);
- Py_CLEAR(self->paramflags);
- Py_CLEAR(self->thunk);
- return PyCData_clear((CDataObject *)self);
+ Py_CLEAR(self->callable);
+ Py_CLEAR(self->restype);
+ Py_CLEAR(self->checker);
+ Py_CLEAR(self->errcheck);
+ Py_CLEAR(self->argtypes);
+ Py_CLEAR(self->converters);
+ Py_CLEAR(self->paramflags);
+ Py_CLEAR(self->thunk);
+ return PyCData_clear((CDataObject *)self);
}
static void
PyCFuncPtr_dealloc(PyCFuncPtrObject *self)
{
- PyCFuncPtr_clear(self);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ PyCFuncPtr_clear(self);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *
PyCFuncPtr_repr(PyCFuncPtrObject *self)
{
#ifdef MS_WIN32
- if (self->index)
- return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
- self->index - 0x1000,
- Py_TYPE(self)->tp_name,
- self);
+ if (self->index)
+ return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
+ self->index - 0x1000,
+ Py_TYPE(self)->tp_name,
+ self);
#endif
- return PyUnicode_FromFormat("<%s object at %p>",
- Py_TYPE(self)->tp_name,
- self);
+ return PyUnicode_FromFormat("<%s object at %p>",
+ Py_TYPE(self)->tp_name,
+ self);
}
static int
PyCFuncPtr_bool(PyCFuncPtrObject *self)
{
- return ((*(void **)self->b_ptr != NULL)
+ return ((*(void **)self->b_ptr != NULL)
#ifdef MS_WIN32
- || (self->index != 0)
+ || (self->index != 0)
#endif
- );
+ );
}
static PyNumberMethods PyCFuncPtr_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)PyCFuncPtr_bool, /* nb_bool */
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ (inquiry)PyCFuncPtr_bool, /* nb_bool */
};
PyTypeObject PyCFuncPtr_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.PyCFuncPtr",
- sizeof(PyCFuncPtrObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)PyCFuncPtr_repr, /* tp_repr */
- &PyCFuncPtr_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)PyCFuncPtr_call, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "Function Pointer", /* tp_doc */
- (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */
- (inquiry)PyCFuncPtr_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- PyCFuncPtr_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCFuncPtr_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.PyCFuncPtr",
+ sizeof(PyCFuncPtrObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)PyCFuncPtr_repr, /* tp_repr */
+ &PyCFuncPtr_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)PyCFuncPtr_call, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "Function Pointer", /* tp_doc */
+ (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */
+ (inquiry)PyCFuncPtr_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ PyCFuncPtr_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCFuncPtr_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/*****************************************************************/
/*
Struct_Type
@@ -3941,61 +3941,61 @@ PyTypeObject PyCFuncPtr_Type = {
*/
static int
_init_pos_args(PyObject *self, PyTypeObject *type,
- PyObject *args, PyObject *kwds,
- int index)
+ PyObject *args, PyObject *kwds,
+ int index)
{
- StgDictObject *dict;
- PyObject *fields;
- int i;
-
- if (PyType_stgdict((PyObject *)type->tp_base)) {
- index = _init_pos_args(self, type->tp_base,
- args, kwds,
- index);
- if (index == -1)
- return -1;
- }
-
- dict = PyType_stgdict((PyObject *)type);
- fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
- if (fields == NULL)
- return index;
-
- for (i = 0;
- i < dict->length && (i+index) < PyTuple_GET_SIZE(args);
- ++i) {
- PyObject *pair = PySequence_GetItem(fields, i);
- PyObject *name, *val;
- int res;
- if (!pair)
- return -1;
- name = PySequence_GetItem(pair, 0);
- if (!name) {
- Py_DECREF(pair);
- return -1;
- }
- val = PyTuple_GET_ITEM(args, i + index);
- if (kwds && PyDict_GetItem(kwds, name)) {
- char *field = PyBytes_AsString(name);
- if (field == NULL) {
- PyErr_Clear();
- field = "???";
- }
- PyErr_Format(PyExc_TypeError,
- "duplicate values for field '%s'",
- field);
- Py_DECREF(pair);
- Py_DECREF(name);
- return -1;
- }
-
- res = PyObject_SetAttr(self, name, val);
- Py_DECREF(pair);
- Py_DECREF(name);
- if (res == -1)
- return -1;
- }
- return index + dict->length;
+ StgDictObject *dict;
+ PyObject *fields;
+ int i;
+
+ if (PyType_stgdict((PyObject *)type->tp_base)) {
+ index = _init_pos_args(self, type->tp_base,
+ args, kwds,
+ index);
+ if (index == -1)
+ return -1;
+ }
+
+ dict = PyType_stgdict((PyObject *)type);
+ fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
+ if (fields == NULL)
+ return index;
+
+ for (i = 0;
+ i < dict->length && (i+index) < PyTuple_GET_SIZE(args);
+ ++i) {
+ PyObject *pair = PySequence_GetItem(fields, i);
+ PyObject *name, *val;
+ int res;
+ if (!pair)
+ return -1;
+ name = PySequence_GetItem(pair, 0);
+ if (!name) {
+ Py_DECREF(pair);
+ return -1;
+ }
+ val = PyTuple_GET_ITEM(args, i + index);
+ if (kwds && PyDict_GetItem(kwds, name)) {
+ char *field = PyBytes_AsString(name);
+ if (field == NULL) {
+ PyErr_Clear();
+ field = "???";
+ }
+ PyErr_Format(PyExc_TypeError,
+ "duplicate values for field '%s'",
+ field);
+ Py_DECREF(pair);
+ Py_DECREF(name);
+ return -1;
+ }
+
+ res = PyObject_SetAttr(self, name, val);
+ Py_DECREF(pair);
+ Py_DECREF(name);
+ if (res == -1)
+ return -1;
+ }
+ return index + dict->length;
}
static int
@@ -4004,119 +4004,119 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
/* Optimization possible: Store the attribute names _fields_[x][0]
* in C accessible fields somewhere ?
*/
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_TypeError,
- "args not a tuple?");
- return -1;
- }
- if (PyTuple_GET_SIZE(args)) {
- int res = _init_pos_args(self, Py_TYPE(self),
- args, kwds, 0);
- if (res == -1)
- return -1;
- if (res < PyTuple_GET_SIZE(args)) {
- PyErr_SetString(PyExc_TypeError,
- "too many initializers");
- return -1;
- }
- }
-
- if (kwds) {
- PyObject *key, *value;
- Py_ssize_t pos = 0;
- while(PyDict_Next(kwds, &pos, &key, &value)) {
- if (-1 == PyObject_SetAttr(self, key, value))
- return -1;
- }
- }
- return 0;
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "args not a tuple?");
+ return -1;
+ }
+ if (PyTuple_GET_SIZE(args)) {
+ int res = _init_pos_args(self, Py_TYPE(self),
+ args, kwds, 0);
+ if (res == -1)
+ return -1;
+ if (res < PyTuple_GET_SIZE(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "too many initializers");
+ return -1;
+ }
+ }
+
+ if (kwds) {
+ PyObject *key, *value;
+ Py_ssize_t pos = 0;
+ while(PyDict_Next(kwds, &pos, &key, &value)) {
+ if (-1 == PyObject_SetAttr(self, key, value))
+ return -1;
+ }
+ }
+ return 0;
}
static PyTypeObject Struct_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.Structure",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "Structure base class", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- Struct_init, /* tp_init */
- 0, /* tp_alloc */
- GenericPyCData_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.Structure",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "Structure base class", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ Struct_init, /* tp_init */
+ 0, /* tp_alloc */
+ GenericPyCData_new, /* tp_new */
+ 0, /* tp_free */
};
static PyTypeObject Union_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.Union",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "Union base class", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- Struct_init, /* tp_init */
- 0, /* tp_alloc */
- GenericPyCData_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.Union",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "Union base class", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ Struct_init, /* tp_init */
+ 0, /* tp_alloc */
+ GenericPyCData_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
PyCArray_Type
@@ -4124,371 +4124,371 @@ static PyTypeObject Union_Type = {
static int
Array_init(CDataObject *self, PyObject *args, PyObject *kw)
{
- Py_ssize_t i;
- Py_ssize_t n;
-
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_TypeError,
- "args not a tuple?");
- return -1;
- }
- n = PyTuple_GET_SIZE(args);
- for (i = 0; i < n; ++i) {
- PyObject *v;
- v = PyTuple_GET_ITEM(args, i);
- if (-1 == PySequence_SetItem((PyObject *)self, i, v))
- return -1;
- }
- return 0;
+ Py_ssize_t i;
+ Py_ssize_t n;
+
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "args not a tuple?");
+ return -1;
+ }
+ n = PyTuple_GET_SIZE(args);
+ for (i = 0; i < n; ++i) {
+ PyObject *v;
+ v = PyTuple_GET_ITEM(args, i);
+ if (-1 == PySequence_SetItem((PyObject *)self, i, v))
+ return -1;
+ }
+ return 0;
}
static PyObject *
Array_item(PyObject *_self, Py_ssize_t index)
{
- CDataObject *self = (CDataObject *)_self;
- Py_ssize_t offset, size;
- StgDictObject *stgdict;
-
-
- if (index < 0 || index >= self->b_length) {
- PyErr_SetString(PyExc_IndexError,
- "invalid index");
- return NULL;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for array instances */
- /* Would it be clearer if we got the item size from
- stgdict->proto's stgdict?
- */
- size = stgdict->size / stgdict->length;
- offset = index * size;
-
- return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self,
- index, size, self->b_ptr + offset);
+ CDataObject *self = (CDataObject *)_self;
+ Py_ssize_t offset, size;
+ StgDictObject *stgdict;
+
+
+ if (index < 0 || index >= self->b_length) {
+ PyErr_SetString(PyExc_IndexError,
+ "invalid index");
+ return NULL;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for array instances */
+ /* Would it be clearer if we got the item size from
+ stgdict->proto's stgdict?
+ */
+ size = stgdict->size / stgdict->length;
+ offset = index * size;
+
+ return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self,
+ index, size, self->b_ptr + offset);
}
static PyObject *
Array_subscript(PyObject *_self, PyObject *item)
{
- CDataObject *self = (CDataObject *)_self;
-
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
-
- if (i == -1 && PyErr_Occurred())
- return NULL;
- if (i < 0)
- i += self->b_length;
- return Array_item(_self, i);
- }
- else if PySlice_Check(item) {
- StgDictObject *stgdict, *itemdict;
- PyObject *proto;
- PyObject *np;
- Py_ssize_t start, stop, step, slicelen, cur, i;
-
- if (PySlice_GetIndicesEx((PySliceObject *)item,
- self->b_length, &start, &stop,
- &step, &slicelen) < 0) {
- return NULL;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for array object instances */
- proto = stgdict->proto;
- itemdict = PyType_stgdict(proto);
- assert(itemdict); /* proto is the item type of the array, a
- ctypes type, so this cannot be NULL */
-
- if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
- char *ptr = (char *)self->b_ptr;
- char *dest;
-
- if (slicelen <= 0)
- return PyBytes_FromStringAndSize("", 0);
- if (step == 1) {
- return PyBytes_FromStringAndSize(ptr + start,
- slicelen);
- }
- dest = (char *)PyMem_Malloc(slicelen);
-
- if (dest == NULL)
- return PyErr_NoMemory();
-
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- dest[i] = ptr[cur];
- }
-
- np = PyBytes_FromStringAndSize(dest, slicelen);
- PyMem_Free(dest);
- return np;
- }
+ CDataObject *self = (CDataObject *)_self;
+
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += self->b_length;
+ return Array_item(_self, i);
+ }
+ else if PySlice_Check(item) {
+ StgDictObject *stgdict, *itemdict;
+ PyObject *proto;
+ PyObject *np;
+ Py_ssize_t start, stop, step, slicelen, cur, i;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item,
+ self->b_length, &start, &stop,
+ &step, &slicelen) < 0) {
+ return NULL;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for array object instances */
+ proto = stgdict->proto;
+ itemdict = PyType_stgdict(proto);
+ assert(itemdict); /* proto is the item type of the array, a
+ ctypes type, so this cannot be NULL */
+
+ if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
+ char *ptr = (char *)self->b_ptr;
+ char *dest;
+
+ if (slicelen <= 0)
+ return PyBytes_FromStringAndSize("", 0);
+ if (step == 1) {
+ return PyBytes_FromStringAndSize(ptr + start,
+ slicelen);
+ }
+ dest = (char *)PyMem_Malloc(slicelen);
+
+ if (dest == NULL)
+ return PyErr_NoMemory();
+
+ for (cur = start, i = 0; i < slicelen;
+ cur += step, i++) {
+ dest[i] = ptr[cur];
+ }
+
+ np = PyBytes_FromStringAndSize(dest, slicelen);
+ PyMem_Free(dest);
+ return np;
+ }
#ifdef CTYPES_UNICODE
- if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
- wchar_t *ptr = (wchar_t *)self->b_ptr;
- wchar_t *dest;
-
- if (slicelen <= 0)
- return PyUnicode_FromUnicode(NULL, 0);
- if (step == 1) {
- return PyUnicode_FromWideChar(ptr + start,
- slicelen);
- }
-
- dest = (wchar_t *)PyMem_Malloc(
- slicelen * sizeof(wchar_t));
-
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- dest[i] = ptr[cur];
- }
-
- np = PyUnicode_FromWideChar(dest, slicelen);
- PyMem_Free(dest);
- return np;
- }
+ if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
+ wchar_t *ptr = (wchar_t *)self->b_ptr;
+ wchar_t *dest;
+
+ if (slicelen <= 0)
+ return PyUnicode_FromUnicode(NULL, 0);
+ if (step == 1) {
+ return PyUnicode_FromWideChar(ptr + start,
+ slicelen);
+ }
+
+ dest = (wchar_t *)PyMem_Malloc(
+ slicelen * sizeof(wchar_t));
+
+ for (cur = start, i = 0; i < slicelen;
+ cur += step, i++) {
+ dest[i] = ptr[cur];
+ }
+
+ np = PyUnicode_FromWideChar(dest, slicelen);
+ PyMem_Free(dest);
+ return np;
+ }
#endif
- np = PyList_New(slicelen);
- if (np == NULL)
- return NULL;
-
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- PyObject *v = Array_item(_self, cur);
- PyList_SET_ITEM(np, i, v);
- }
- return np;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "indices must be integers");
- return NULL;
- }
+ np = PyList_New(slicelen);
+ if (np == NULL)
+ return NULL;
+
+ for (cur = start, i = 0; i < slicelen;
+ cur += step, i++) {
+ PyObject *v = Array_item(_self, cur);
+ PyList_SET_ITEM(np, i, v);
+ }
+ return np;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "indices must be integers");
+ return NULL;
+ }
}
static int
Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
{
- CDataObject *self = (CDataObject *)_self;
- Py_ssize_t size, offset;
- StgDictObject *stgdict;
- char *ptr;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "Array does not support item deletion");
- return -1;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for array object instances */
- if (index < 0 || index >= stgdict->length) {
- PyErr_SetString(PyExc_IndexError,
- "invalid index");
- return -1;
- }
- size = stgdict->size / stgdict->length;
- offset = index * size;
- ptr = self->b_ptr + offset;
-
- return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value,
- index, size, ptr);
+ CDataObject *self = (CDataObject *)_self;
+ Py_ssize_t size, offset;
+ StgDictObject *stgdict;
+ char *ptr;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "Array does not support item deletion");
+ return -1;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for array object instances */
+ if (index < 0 || index >= stgdict->length) {
+ PyErr_SetString(PyExc_IndexError,
+ "invalid index");
+ return -1;
+ }
+ size = stgdict->size / stgdict->length;
+ offset = index * size;
+ ptr = self->b_ptr + offset;
+
+ return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value,
+ index, size, ptr);
}
static int
Array_ass_subscript(PyObject *_self, PyObject *item, PyObject *value)
{
- CDataObject *self = (CDataObject *)_self;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "Array does not support item deletion");
- return -1;
- }
-
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
-
- if (i == -1 && PyErr_Occurred())
- return -1;
- if (i < 0)
- i += self->b_length;
- return Array_ass_item(_self, i, value);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
-
- if (PySlice_GetIndicesEx((PySliceObject *)item,
- self->b_length, &start, &stop,
- &step, &slicelen) < 0) {
- return -1;
- }
- if ((step < 0 && start < stop) ||
- (step > 0 && start > stop))
- stop = start;
-
- otherlen = PySequence_Length(value);
- if (otherlen != slicelen) {
- PyErr_SetString(PyExc_ValueError,
- "Can only assign sequence of same size");
- return -1;
- }
- for (cur = start, i = 0; i < otherlen; cur += step, i++) {
- PyObject *item = PySequence_GetItem(value, i);
- int result;
- if (item == NULL)
- return -1;
- result = Array_ass_item(_self, cur, item);
- Py_DECREF(item);
- if (result == -1)
- return -1;
- }
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "indices must be integer");
- return -1;
- }
+ CDataObject *self = (CDataObject *)_self;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "Array does not support item deletion");
+ return -1;
+ }
+
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0)
+ i += self->b_length;
+ return Array_ass_item(_self, i, value);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item,
+ self->b_length, &start, &stop,
+ &step, &slicelen) < 0) {
+ return -1;
+ }
+ if ((step < 0 && start < stop) ||
+ (step > 0 && start > stop))
+ stop = start;
+
+ otherlen = PySequence_Length(value);
+ if (otherlen != slicelen) {
+ PyErr_SetString(PyExc_ValueError,
+ "Can only assign sequence of same size");
+ return -1;
+ }
+ for (cur = start, i = 0; i < otherlen; cur += step, i++) {
+ PyObject *item = PySequence_GetItem(value, i);
+ int result;
+ if (item == NULL)
+ return -1;
+ result = Array_ass_item(_self, cur, item);
+ Py_DECREF(item);
+ if (result == -1)
+ return -1;
+ }
+ return 0;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "indices must be integer");
+ return -1;
+ }
}
static Py_ssize_t
Array_length(PyObject *_self)
{
- CDataObject *self = (CDataObject *)_self;
- return self->b_length;
+ CDataObject *self = (CDataObject *)_self;
+ return self->b_length;
}
static PySequenceMethods Array_as_sequence = {
- Array_length, /* sq_length; */
- 0, /* sq_concat; */
- 0, /* sq_repeat; */
- Array_item, /* sq_item; */
- 0, /* sq_slice; */
- Array_ass_item, /* sq_ass_item; */
- 0, /* sq_ass_slice; */
- 0, /* sq_contains; */
-
- 0, /* sq_inplace_concat; */
- 0, /* sq_inplace_repeat; */
+ Array_length, /* sq_length; */
+ 0, /* sq_concat; */
+ 0, /* sq_repeat; */
+ Array_item, /* sq_item; */
+ 0, /* sq_slice; */
+ Array_ass_item, /* sq_ass_item; */
+ 0, /* sq_ass_slice; */
+ 0, /* sq_contains; */
+
+ 0, /* sq_inplace_concat; */
+ 0, /* sq_inplace_repeat; */
};
static PyMappingMethods Array_as_mapping = {
- Array_length,
- Array_subscript,
- Array_ass_subscript,
+ Array_length,
+ Array_subscript,
+ Array_ass_subscript,
};
PyTypeObject PyCArray_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.Array",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &Array_as_sequence, /* tp_as_sequence */
- &Array_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "XXX to be provided", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)Array_init, /* tp_init */
- 0, /* tp_alloc */
- GenericPyCData_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.Array",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &Array_as_sequence, /* tp_as_sequence */
+ &Array_as_mapping, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "XXX to be provided", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)Array_init, /* tp_init */
+ 0, /* tp_alloc */
+ GenericPyCData_new, /* tp_new */
+ 0, /* tp_free */
};
PyObject *
PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
{
- static PyObject *cache;
- PyObject *key;
- PyObject *result;
- char name[256];
- PyObject *len;
-
- if (cache == NULL) {
- cache = PyDict_New();
- if (cache == NULL)
- return NULL;
- }
- len = PyLong_FromSsize_t(length);
- if (len == NULL)
- return NULL;
- key = PyTuple_Pack(2, itemtype, len);
- Py_DECREF(len);
- if (!key)
- return NULL;
- result = PyDict_GetItemProxy(cache, key);
- if (result) {
- Py_INCREF(result);
- Py_DECREF(key);
- return result;
- }
-
- if (!PyType_Check(itemtype)) {
- PyErr_SetString(PyExc_TypeError,
- "Expected a type object");
- return NULL;
- }
+ static PyObject *cache;
+ PyObject *key;
+ PyObject *result;
+ char name[256];
+ PyObject *len;
+
+ if (cache == NULL) {
+ cache = PyDict_New();
+ if (cache == NULL)
+ return NULL;
+ }
+ len = PyLong_FromSsize_t(length);
+ if (len == NULL)
+ return NULL;
+ key = PyTuple_Pack(2, itemtype, len);
+ Py_DECREF(len);
+ if (!key)
+ return NULL;
+ result = PyDict_GetItemProxy(cache, key);
+ if (result) {
+ Py_INCREF(result);
+ Py_DECREF(key);
+ return result;
+ }
+
+ if (!PyType_Check(itemtype)) {
+ PyErr_SetString(PyExc_TypeError,
+ "Expected a type object");
+ return NULL;
+ }
#ifdef MS_WIN64
- sprintf(name, "%.200s_Array_%Id",
- ((PyTypeObject *)itemtype)->tp_name, length);
+ sprintf(name, "%.200s_Array_%Id",
+ ((PyTypeObject *)itemtype)->tp_name, length);
#else
- sprintf(name, "%.200s_Array_%ld",
- ((PyTypeObject *)itemtype)->tp_name, (long)length);
+ sprintf(name, "%.200s_Array_%ld",
+ ((PyTypeObject *)itemtype)->tp_name, (long)length);
#endif
- result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type,
- "U(O){s:n,s:O}",
- name,
- &PyCArray_Type,
- "_length_",
- length,
- "_type_",
- itemtype
- );
- if (result == NULL) {
- Py_DECREF(key);
- return NULL;
- }
- if (-1 == PyDict_SetItemProxy(cache, key, result)) {
- Py_DECREF(key);
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(key);
- return result;
+ result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type,
+ "U(O){s:n,s:O}",
+ name,
+ &PyCArray_Type,
+ "_length_",
+ length,
+ "_type_",
+ itemtype
+ );
+ if (result == NULL) {
+ Py_DECREF(key);
+ return NULL;
+ }
+ if (-1 == PyDict_SetItemProxy(cache, key, result)) {
+ Py_DECREF(key);
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(key);
+ return result;
}
-
+
/******************************************************************/
/*
Simple_Type
@@ -4497,166 +4497,166 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
static int
Simple_set_value(CDataObject *self, PyObject *value)
{
- PyObject *result;
- StgDictObject *dict = PyObject_stgdict((PyObject *)self);
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete attribute");
- return -1;
- }
- assert(dict); /* Cannot be NULL for CDataObject instances */
- assert(dict->setfunc);
- result = dict->setfunc(self->b_ptr, value, dict->size);
- if (!result)
- return -1;
-
- /* consumes the refcount the setfunc returns */
- return KeepRef(self, 0, result);
+ PyObject *result;
+ StgDictObject *dict = PyObject_stgdict((PyObject *)self);
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete attribute");
+ return -1;
+ }
+ assert(dict); /* Cannot be NULL for CDataObject instances */
+ assert(dict->setfunc);
+ result = dict->setfunc(self->b_ptr, value, dict->size);
+ if (!result)
+ return -1;
+
+ /* consumes the refcount the setfunc returns */
+ return KeepRef(self, 0, result);
}
static int
Simple_init(CDataObject *self, PyObject *args, PyObject *kw)
{
- PyObject *value = NULL;
- if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value))
- return -1;
- if (value)
- return Simple_set_value(self, value);
- return 0;
+ PyObject *value = NULL;
+ if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value))
+ return -1;
+ if (value)
+ return Simple_set_value(self, value);
+ return 0;
}
static PyObject *
Simple_get_value(CDataObject *self)
{
- StgDictObject *dict;
- dict = PyObject_stgdict((PyObject *)self);
- assert(dict); /* Cannot be NULL for CDataObject instances */
- assert(dict->getfunc);
- return dict->getfunc(self->b_ptr, self->b_size);
+ StgDictObject *dict;
+ dict = PyObject_stgdict((PyObject *)self);
+ assert(dict); /* Cannot be NULL for CDataObject instances */
+ assert(dict->getfunc);
+ return dict->getfunc(self->b_ptr, self->b_size);
}
static PyGetSetDef Simple_getsets[] = {
- { "value", (getter)Simple_get_value, (setter)Simple_set_value,
- "current value", NULL },
- { NULL, NULL }
+ { "value", (getter)Simple_get_value, (setter)Simple_set_value,
+ "current value", NULL },
+ { NULL, NULL }
};
static PyObject *
Simple_from_outparm(PyObject *self, PyObject *args)
{
- if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) {
- Py_INCREF(self);
- return self;
- }
- /* call stgdict->getfunc */
- return Simple_get_value((CDataObject *)self);
+ if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) {
+ Py_INCREF(self);
+ return self;
+ }
+ /* call stgdict->getfunc */
+ return Simple_get_value((CDataObject *)self);
}
static PyMethodDef Simple_methods[] = {
- { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, },
- { NULL, NULL },
+ { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, },
+ { NULL, NULL },
};
static int Simple_bool(CDataObject *self)
{
- return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size);
+ return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size);
}
static PyNumberMethods Simple_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)Simple_bool, /* nb_bool */
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ (inquiry)Simple_bool, /* nb_bool */
};
/* "%s(%s)" % (self.__class__.__name__, self.value) */
static PyObject *
Simple_repr(CDataObject *self)
{
- PyObject *val, *name, *args, *result;
- static PyObject *format;
-
- if (Py_TYPE(self)->tp_base != &Simple_Type) {
- return PyUnicode_FromFormat("<%s object at %p>",
- Py_TYPE(self)->tp_name, self);
- }
-
- if (format == NULL) {
- format = PyUnicode_InternFromString("%s(%r)");
- if (format == NULL)
- return NULL;
- }
-
- val = Simple_get_value(self);
- if (val == NULL)
- return NULL;
-
- name = PyUnicode_FromString(Py_TYPE(self)->tp_name);
- if (name == NULL) {
- Py_DECREF(val);
- return NULL;
- }
-
- args = PyTuple_Pack(2, name, val);
- Py_DECREF(name);
- Py_DECREF(val);
- if (args == NULL)
- return NULL;
-
- result = PyUnicode_Format(format, args);
- Py_DECREF(args);
- return result;
+ PyObject *val, *name, *args, *result;
+ static PyObject *format;
+
+ if (Py_TYPE(self)->tp_base != &Simple_Type) {
+ return PyUnicode_FromFormat("<%s object at %p>",
+ Py_TYPE(self)->tp_name, self);
+ }
+
+ if (format == NULL) {
+ format = PyUnicode_InternFromString("%s(%r)");
+ if (format == NULL)
+ return NULL;
+ }
+
+ val = Simple_get_value(self);
+ if (val == NULL)
+ return NULL;
+
+ name = PyUnicode_FromString(Py_TYPE(self)->tp_name);
+ if (name == NULL) {
+ Py_DECREF(val);
+ return NULL;
+ }
+
+ args = PyTuple_Pack(2, name, val);
+ Py_DECREF(name);
+ Py_DECREF(val);
+ if (args == NULL)
+ return NULL;
+
+ result = PyUnicode_Format(format, args);
+ Py_DECREF(args);
+ return result;
}
static PyTypeObject Simple_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes._SimpleCData",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)&Simple_repr, /* tp_repr */
- &Simple_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "XXX to be provided", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- Simple_methods, /* tp_methods */
- 0, /* tp_members */
- Simple_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)Simple_init, /* tp_init */
- 0, /* tp_alloc */
- GenericPyCData_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes._SimpleCData",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)&Simple_repr, /* tp_repr */
+ &Simple_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "XXX to be provided", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ Simple_methods, /* tp_methods */
+ 0, /* tp_members */
+ Simple_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)Simple_init, /* tp_init */
+ 0, /* tp_alloc */
+ GenericPyCData_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
PyCPointer_Type
@@ -4664,377 +4664,377 @@ static PyTypeObject Simple_Type = {
static PyObject *
Pointer_item(PyObject *_self, Py_ssize_t index)
{
- CDataObject *self = (CDataObject *)_self;
- Py_ssize_t size;
- Py_ssize_t offset;
- StgDictObject *stgdict, *itemdict;
- PyObject *proto;
-
- if (*(void **)self->b_ptr == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "NULL pointer access");
- return NULL;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for pointer object instances */
-
- proto = stgdict->proto;
- assert(proto);
- itemdict = PyType_stgdict(proto);
- assert(itemdict); /* proto is the item type of the pointer, a ctypes
- type, so this cannot be NULL */
-
- size = itemdict->size;
- offset = index * itemdict->size;
-
- return PyCData_get(proto, stgdict->getfunc, (PyObject *)self,
- index, size, (*(char **)self->b_ptr) + offset);
+ CDataObject *self = (CDataObject *)_self;
+ Py_ssize_t size;
+ Py_ssize_t offset;
+ StgDictObject *stgdict, *itemdict;
+ PyObject *proto;
+
+ if (*(void **)self->b_ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "NULL pointer access");
+ return NULL;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for pointer object instances */
+
+ proto = stgdict->proto;
+ assert(proto);
+ itemdict = PyType_stgdict(proto);
+ assert(itemdict); /* proto is the item type of the pointer, a ctypes
+ type, so this cannot be NULL */
+
+ size = itemdict->size;
+ offset = index * itemdict->size;
+
+ return PyCData_get(proto, stgdict->getfunc, (PyObject *)self,
+ index, size, (*(char **)self->b_ptr) + offset);
}
static int
Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
{
- CDataObject *self = (CDataObject *)_self;
- Py_ssize_t size;
- Py_ssize_t offset;
- StgDictObject *stgdict, *itemdict;
- PyObject *proto;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "Pointer does not support item deletion");
- return -1;
- }
-
- if (*(void **)self->b_ptr == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "NULL pointer access");
- return -1;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL fr pointer instances */
-
- proto = stgdict->proto;
- assert(proto);
-
- itemdict = PyType_stgdict(proto);
- assert(itemdict); /* Cannot be NULL because the itemtype of a pointer
- is always a ctypes type */
-
- size = itemdict->size;
- offset = index * itemdict->size;
-
- return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value,
- index, size, (*(char **)self->b_ptr) + offset);
+ CDataObject *self = (CDataObject *)_self;
+ Py_ssize_t size;
+ Py_ssize_t offset;
+ StgDictObject *stgdict, *itemdict;
+ PyObject *proto;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "Pointer does not support item deletion");
+ return -1;
+ }
+
+ if (*(void **)self->b_ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "NULL pointer access");
+ return -1;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL fr pointer instances */
+
+ proto = stgdict->proto;
+ assert(proto);
+
+ itemdict = PyType_stgdict(proto);
+ assert(itemdict); /* Cannot be NULL because the itemtype of a pointer
+ is always a ctypes type */
+
+ size = itemdict->size;
+ offset = index * itemdict->size;
+
+ return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value,
+ index, size, (*(char **)self->b_ptr) + offset);
}
static PyObject *
Pointer_get_contents(CDataObject *self, void *closure)
{
- StgDictObject *stgdict;
-
- if (*(void **)self->b_ptr == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "NULL pointer access");
- return NULL;
- }
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL fr pointer instances */
- return PyCData_FromBaseObj(stgdict->proto,
- (PyObject *)self, 0,
- *(void **)self->b_ptr);
+ StgDictObject *stgdict;
+
+ if (*(void **)self->b_ptr == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "NULL pointer access");
+ return NULL;
+ }
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL fr pointer instances */
+ return PyCData_FromBaseObj(stgdict->proto,
+ (PyObject *)self, 0,
+ *(void **)self->b_ptr);
}
static int
Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
{
- StgDictObject *stgdict;
- CDataObject *dst;
- PyObject *keep;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "Pointer does not support item deletion");
- return -1;
- }
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL fr pointer instances */
- assert(stgdict->proto);
- if (!CDataObject_Check(value)
- || 0 == PyObject_IsInstance(value, stgdict->proto)) {
- /* XXX PyObject_IsInstance could return -1! */
- PyErr_Format(PyExc_TypeError,
- "expected %s instead of %s",
- ((PyTypeObject *)(stgdict->proto))->tp_name,
- Py_TYPE(value)->tp_name);
- return -1;
- }
-
- dst = (CDataObject *)value;
- *(void **)self->b_ptr = dst->b_ptr;
-
- /*
- A Pointer instance must keep a the value it points to alive. So, a
- pointer instance has b_length set to 2 instead of 1, and we set
- 'value' itself as the second item of the b_objects list, additionally.
- */
- Py_INCREF(value);
- if (-1 == KeepRef(self, 1, value))
- return -1;
-
- keep = GetKeepedObjects(dst);
- Py_INCREF(keep);
- return KeepRef(self, 0, keep);
+ StgDictObject *stgdict;
+ CDataObject *dst;
+ PyObject *keep;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "Pointer does not support item deletion");
+ return -1;
+ }
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL fr pointer instances */
+ assert(stgdict->proto);
+ if (!CDataObject_Check(value)
+ || 0 == PyObject_IsInstance(value, stgdict->proto)) {
+ /* XXX PyObject_IsInstance could return -1! */
+ PyErr_Format(PyExc_TypeError,
+ "expected %s instead of %s",
+ ((PyTypeObject *)(stgdict->proto))->tp_name,
+ Py_TYPE(value)->tp_name);
+ return -1;
+ }
+
+ dst = (CDataObject *)value;
+ *(void **)self->b_ptr = dst->b_ptr;
+
+ /*
+ A Pointer instance must keep a the value it points to alive. So, a
+ pointer instance has b_length set to 2 instead of 1, and we set
+ 'value' itself as the second item of the b_objects list, additionally.
+ */
+ Py_INCREF(value);
+ if (-1 == KeepRef(self, 1, value))
+ return -1;
+
+ keep = GetKeepedObjects(dst);
+ Py_INCREF(keep);
+ return KeepRef(self, 0, keep);
}
static PyGetSetDef Pointer_getsets[] = {
- { "contents", (getter)Pointer_get_contents,
- (setter)Pointer_set_contents,
- "the object this pointer points to (read-write)", NULL },
- { NULL, NULL }
+ { "contents", (getter)Pointer_get_contents,
+ (setter)Pointer_set_contents,
+ "the object this pointer points to (read-write)", NULL },
+ { NULL, NULL }
};
static int
Pointer_init(CDataObject *self, PyObject *args, PyObject *kw)
{
- PyObject *value = NULL;
+ PyObject *value = NULL;
- if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value))
- return -1;
- if (value == NULL)
- return 0;
- return Pointer_set_contents(self, value, NULL);
+ if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value))
+ return -1;
+ if (value == NULL)
+ return 0;
+ return Pointer_set_contents(self, value, NULL);
}
static PyObject *
Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- StgDictObject *dict = PyType_stgdict((PyObject *)type);
- if (!dict || !dict->proto) {
- PyErr_SetString(PyExc_TypeError,
- "Cannot create instance: has no _type_");
- return NULL;
- }
- return GenericPyCData_new(type, args, kw);
+ StgDictObject *dict = PyType_stgdict((PyObject *)type);
+ if (!dict || !dict->proto) {
+ PyErr_SetString(PyExc_TypeError,
+ "Cannot create instance: has no _type_");
+ return NULL;
+ }
+ return GenericPyCData_new(type, args, kw);
}
static PyObject *
Pointer_subscript(PyObject *_self, PyObject *item)
{
- CDataObject *self = (CDataObject *)_self;
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- return Pointer_item(_self, i);
- }
- else if (PySlice_Check(item)) {
- PySliceObject *slice = (PySliceObject *)item;
- Py_ssize_t start, stop, step;
- PyObject *np;
- StgDictObject *stgdict, *itemdict;
- PyObject *proto;
- Py_ssize_t i, len, cur;
-
- /* Since pointers have no length, and we want to apply
- different semantics to negative indices than normal
- slicing, we have to dissect the slice object ourselves.*/
- if (slice->step == Py_None) {
- step = 1;
- }
- else {
- step = PyNumber_AsSsize_t(slice->step,
- PyExc_ValueError);
- if (step == -1 && PyErr_Occurred())
- return NULL;
- if (step == 0) {
- PyErr_SetString(PyExc_ValueError,
- "slice step cannot be zero");
- return NULL;
- }
- }
- if (slice->start == Py_None) {
- if (step < 0) {
- PyErr_SetString(PyExc_ValueError,
- "slice start is required "
- "for step < 0");
- return NULL;
- }
- start = 0;
- }
- else {
- start = PyNumber_AsSsize_t(slice->start,
- PyExc_ValueError);
- if (start == -1 && PyErr_Occurred())
- return NULL;
- }
- if (slice->stop == Py_None) {
- PyErr_SetString(PyExc_ValueError,
- "slice stop is required");
- return NULL;
- }
- stop = PyNumber_AsSsize_t(slice->stop,
- PyExc_ValueError);
- if (stop == -1 && PyErr_Occurred())
- return NULL;
- if ((step > 0 && start > stop) ||
- (step < 0 && start < stop))
- len = 0;
- else if (step > 0)
- len = (stop - start - 1) / step + 1;
- else
- len = (stop - start + 1) / step + 1;
-
- stgdict = PyObject_stgdict((PyObject *)self);
- assert(stgdict); /* Cannot be NULL for pointer instances */
- proto = stgdict->proto;
- assert(proto);
- itemdict = PyType_stgdict(proto);
- assert(itemdict);
- if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
- char *ptr = *(char **)self->b_ptr;
- char *dest;
-
- if (len <= 0)
- return PyBytes_FromStringAndSize("", 0);
- if (step == 1) {
- return PyBytes_FromStringAndSize(ptr + start,
- len);
- }
- dest = (char *)PyMem_Malloc(len);
- if (dest == NULL)
- return PyErr_NoMemory();
- for (cur = start, i = 0; i < len; cur += step, i++) {
- dest[i] = ptr[cur];
- }
- np = PyBytes_FromStringAndSize(dest, len);
- PyMem_Free(dest);
- return np;
- }
+ CDataObject *self = (CDataObject *)_self;
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ return Pointer_item(_self, i);
+ }
+ else if (PySlice_Check(item)) {
+ PySliceObject *slice = (PySliceObject *)item;
+ Py_ssize_t start, stop, step;
+ PyObject *np;
+ StgDictObject *stgdict, *itemdict;
+ PyObject *proto;
+ Py_ssize_t i, len, cur;
+
+ /* Since pointers have no length, and we want to apply
+ different semantics to negative indices than normal
+ slicing, we have to dissect the slice object ourselves.*/
+ if (slice->step == Py_None) {
+ step = 1;
+ }
+ else {
+ step = PyNumber_AsSsize_t(slice->step,
+ PyExc_ValueError);
+ if (step == -1 && PyErr_Occurred())
+ return NULL;
+ if (step == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "slice step cannot be zero");
+ return NULL;
+ }
+ }
+ if (slice->start == Py_None) {
+ if (step < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "slice start is required "
+ "for step < 0");
+ return NULL;
+ }
+ start = 0;
+ }
+ else {
+ start = PyNumber_AsSsize_t(slice->start,
+ PyExc_ValueError);
+ if (start == -1 && PyErr_Occurred())
+ return NULL;
+ }
+ if (slice->stop == Py_None) {
+ PyErr_SetString(PyExc_ValueError,
+ "slice stop is required");
+ return NULL;
+ }
+ stop = PyNumber_AsSsize_t(slice->stop,
+ PyExc_ValueError);
+ if (stop == -1 && PyErr_Occurred())
+ return NULL;
+ if ((step > 0 && start > stop) ||
+ (step < 0 && start < stop))
+ len = 0;
+ else if (step > 0)
+ len = (stop - start - 1) / step + 1;
+ else
+ len = (stop - start + 1) / step + 1;
+
+ stgdict = PyObject_stgdict((PyObject *)self);
+ assert(stgdict); /* Cannot be NULL for pointer instances */
+ proto = stgdict->proto;
+ assert(proto);
+ itemdict = PyType_stgdict(proto);
+ assert(itemdict);
+ if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
+ char *ptr = *(char **)self->b_ptr;
+ char *dest;
+
+ if (len <= 0)
+ return PyBytes_FromStringAndSize("", 0);
+ if (step == 1) {
+ return PyBytes_FromStringAndSize(ptr + start,
+ len);
+ }
+ dest = (char *)PyMem_Malloc(len);
+ if (dest == NULL)
+ return PyErr_NoMemory();
+ for (cur = start, i = 0; i < len; cur += step, i++) {
+ dest[i] = ptr[cur];
+ }
+ np = PyBytes_FromStringAndSize(dest, len);
+ PyMem_Free(dest);
+ return np;
+ }
#ifdef CTYPES_UNICODE
- if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
- wchar_t *ptr = *(wchar_t **)self->b_ptr;
- wchar_t *dest;
-
- if (len <= 0)
- return PyUnicode_FromUnicode(NULL, 0);
- if (step == 1) {
- return PyUnicode_FromWideChar(ptr + start,
- len);
- }
- dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t));
- if (dest == NULL)
- return PyErr_NoMemory();
- for (cur = start, i = 0; i < len; cur += step, i++) {
- dest[i] = ptr[cur];
- }
- np = PyUnicode_FromWideChar(dest, len);
- PyMem_Free(dest);
- return np;
- }
+ if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
+ wchar_t *ptr = *(wchar_t **)self->b_ptr;
+ wchar_t *dest;
+
+ if (len <= 0)
+ return PyUnicode_FromUnicode(NULL, 0);
+ if (step == 1) {
+ return PyUnicode_FromWideChar(ptr + start,
+ len);
+ }
+ dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t));
+ if (dest == NULL)
+ return PyErr_NoMemory();
+ for (cur = start, i = 0; i < len; cur += step, i++) {
+ dest[i] = ptr[cur];
+ }
+ np = PyUnicode_FromWideChar(dest, len);
+ PyMem_Free(dest);
+ return np;
+ }
#endif
- np = PyList_New(len);
- if (np == NULL)
- return NULL;
-
- for (cur = start, i = 0; i < len; cur += step, i++) {
- PyObject *v = Pointer_item(_self, cur);
- PyList_SET_ITEM(np, i, v);
- }
- return np;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "Pointer indices must be integer");
- return NULL;
- }
+ np = PyList_New(len);
+ if (np == NULL)
+ return NULL;
+
+ for (cur = start, i = 0; i < len; cur += step, i++) {
+ PyObject *v = Pointer_item(_self, cur);
+ PyList_SET_ITEM(np, i, v);
+ }
+ return np;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "Pointer indices must be integer");
+ return NULL;
+ }
}
static PySequenceMethods Pointer_as_sequence = {
- 0, /* inquiry sq_length; */
- 0, /* binaryfunc sq_concat; */
- 0, /* intargfunc sq_repeat; */
- Pointer_item, /* intargfunc sq_item; */
- 0, /* intintargfunc sq_slice; */
- Pointer_ass_item, /* intobjargproc sq_ass_item; */
- 0, /* intintobjargproc sq_ass_slice; */
- 0, /* objobjproc sq_contains; */
- /* Added in release 2.0 */
- 0, /* binaryfunc sq_inplace_concat; */
- 0, /* intargfunc sq_inplace_repeat; */
+ 0, /* inquiry sq_length; */
+ 0, /* binaryfunc sq_concat; */
+ 0, /* intargfunc sq_repeat; */
+ Pointer_item, /* intargfunc sq_item; */
+ 0, /* intintargfunc sq_slice; */
+ Pointer_ass_item, /* intobjargproc sq_ass_item; */
+ 0, /* intintobjargproc sq_ass_slice; */
+ 0, /* objobjproc sq_contains; */
+ /* Added in release 2.0 */
+ 0, /* binaryfunc sq_inplace_concat; */
+ 0, /* intargfunc sq_inplace_repeat; */
};
static PyMappingMethods Pointer_as_mapping = {
- 0,
- Pointer_subscript,
+ 0,
+ Pointer_subscript,
};
static int
Pointer_bool(CDataObject *self)
{
- return (*(void **)self->b_ptr != NULL);
+ return (*(void **)self->b_ptr != NULL);
}
static PyNumberMethods Pointer_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)Pointer_bool, /* nb_bool */
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ (inquiry)Pointer_bool, /* nb_bool */
};
PyTypeObject PyCPointer_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes._Pointer",
- sizeof(CDataObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- &Pointer_as_number, /* tp_as_number */
- &Pointer_as_sequence, /* tp_as_sequence */
- &Pointer_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- &PyCData_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- "XXX to be provided", /* tp_doc */
- (traverseproc)PyCData_traverse, /* tp_traverse */
- (inquiry)PyCData_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- Pointer_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)Pointer_init, /* tp_init */
- 0, /* tp_alloc */
- Pointer_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes._Pointer",
+ sizeof(CDataObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ &Pointer_as_number, /* tp_as_number */
+ &Pointer_as_sequence, /* tp_as_sequence */
+ &Pointer_as_mapping, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ &PyCData_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ "XXX to be provided", /* tp_doc */
+ (traverseproc)PyCData_traverse, /* tp_traverse */
+ (inquiry)PyCData_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ Pointer_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)Pointer_init, /* tp_init */
+ 0, /* tp_alloc */
+ Pointer_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
* Module initialization.
@@ -5056,27 +5056,27 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
int status;
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
- return -1;
+ return -1;
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
- return -1;
+ return -1;
a = PySequence_GetSlice(args, 1, PySequence_Size(args));
if (!a)
- return -1;
+ return -1;
status = PyObject_SetAttrString(self, "args", a);
Py_DECREF(a);
if (status < 0)
- return -1;
+ return -1;
if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
- return -1;
+ return -1;
if (PyObject_SetAttrString(self, "text", text) < 0)
- return -1;
+ return -1;
if (PyObject_SetAttrString(self, "details", details) < 0)
- return -1;
+ return -1;
bself = (PyBaseExceptionObject *)self;
Py_DECREF(bself->args);
@@ -5131,11 +5131,11 @@ static PyTypeObject PyComError_Type = {
static int
create_comerror(void)
{
- PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
- if (PyType_Ready(&PyComError_Type) < 0)
- return -1;
- ComError = (PyObject*)&PyComError_Type;
- return 0;
+ PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
+ if (PyType_Ready(&PyComError_Type) < 0)
+ return -1;
+ ComError = (PyObject*)&PyComError_Type;
+ return 0;
}
#endif
@@ -5143,259 +5143,259 @@ create_comerror(void)
static PyObject *
string_at(const char *ptr, int size)
{
- if (size == -1)
- return PyBytes_FromStringAndSize(ptr, strlen(ptr));
- return PyBytes_FromStringAndSize(ptr, size);
+ if (size == -1)
+ return PyBytes_FromStringAndSize(ptr, strlen(ptr));
+ return PyBytes_FromStringAndSize(ptr, size);
}
static int
cast_check_pointertype(PyObject *arg)
{
- StgDictObject *dict;
-
- if (PyCPointerTypeObject_Check(arg))
- return 1;
- if (PyCFuncPtrTypeObject_Check(arg))
- return 1;
- dict = PyType_stgdict(arg);
- if (dict) {
- if (PyUnicode_Check(dict->proto)
- && (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) {
- /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
- return 1;
- }
- }
- PyErr_Format(PyExc_TypeError,
- "cast() argument 2 must be a pointer type, not %s",
- PyType_Check(arg)
- ? ((PyTypeObject *)arg)->tp_name
- : Py_TYPE(arg)->tp_name);
- return 0;
+ StgDictObject *dict;
+
+ if (PyCPointerTypeObject_Check(arg))
+ return 1;
+ if (PyCFuncPtrTypeObject_Check(arg))
+ return 1;
+ dict = PyType_stgdict(arg);
+ if (dict) {
+ if (PyUnicode_Check(dict->proto)
+ && (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) {
+ /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
+ return 1;
+ }
+ }
+ PyErr_Format(PyExc_TypeError,
+ "cast() argument 2 must be a pointer type, not %s",
+ PyType_Check(arg)
+ ? ((PyTypeObject *)arg)->tp_name
+ : Py_TYPE(arg)->tp_name);
+ return 0;
}
static PyObject *
cast(void *ptr, PyObject *src, PyObject *ctype)
{
- CDataObject *result;
- if (0 == cast_check_pointertype(ctype))
- return NULL;
- result = (CDataObject *)PyObject_CallFunctionObjArgs(ctype, NULL);
- if (result == NULL)
- return NULL;
-
- /*
- The casted objects '_objects' member:
-
- It must certainly contain the source objects one.
- It must contain the source object itself.
- */
- if (CDataObject_Check(src)) {
- CDataObject *obj = (CDataObject *)src;
- /* PyCData_GetContainer will initialize src.b_objects, we need
- this so it can be shared */
- PyCData_GetContainer(obj);
- /* But we need a dictionary! */
- if (obj->b_objects == Py_None) {
- Py_DECREF(Py_None);
- obj->b_objects = PyDict_New();
- if (obj->b_objects == NULL)
- goto failed;
- }
- Py_XINCREF(obj->b_objects);
- result->b_objects = obj->b_objects;
- if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
- PyObject *index;
- int rc;
- index = PyLong_FromVoidPtr((void *)src);
- if (index == NULL)
- goto failed;
- rc = PyDict_SetItem(result->b_objects, index, src);
- Py_DECREF(index);
- if (rc == -1)
- goto failed;
- }
- }
- /* Should we assert that result is a pointer type? */
- memcpy(result->b_ptr, &ptr, sizeof(void *));
- return (PyObject *)result;
+ CDataObject *result;
+ if (0 == cast_check_pointertype(ctype))
+ return NULL;
+ result = (CDataObject *)PyObject_CallFunctionObjArgs(ctype, NULL);
+ if (result == NULL)
+ return NULL;
+
+ /*
+ The casted objects '_objects' member:
+
+ It must certainly contain the source objects one.
+ It must contain the source object itself.
+ */
+ if (CDataObject_Check(src)) {
+ CDataObject *obj = (CDataObject *)src;
+ /* PyCData_GetContainer will initialize src.b_objects, we need
+ this so it can be shared */
+ PyCData_GetContainer(obj);
+ /* But we need a dictionary! */
+ if (obj->b_objects == Py_None) {
+ Py_DECREF(Py_None);
+ obj->b_objects = PyDict_New();
+ if (obj->b_objects == NULL)
+ goto failed;
+ }
+ Py_XINCREF(obj->b_objects);
+ result->b_objects = obj->b_objects;
+ if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
+ PyObject *index;
+ int rc;
+ index = PyLong_FromVoidPtr((void *)src);
+ if (index == NULL)
+ goto failed;
+ rc = PyDict_SetItem(result->b_objects, index, src);
+ Py_DECREF(index);
+ if (rc == -1)
+ goto failed;
+ }
+ }
+ /* Should we assert that result is a pointer type? */
+ memcpy(result->b_ptr, &ptr, sizeof(void *));
+ return (PyObject *)result;
failed:
- Py_DECREF(result);
- return NULL;
+ Py_DECREF(result);
+ return NULL;
}
#ifdef CTYPES_UNICODE
static PyObject *
wstring_at(const wchar_t *ptr, int size)
{
- Py_ssize_t ssize = size;
- if (ssize == -1)
- ssize = wcslen(ptr);
- return PyUnicode_FromWideChar(ptr, ssize);
+ Py_ssize_t ssize = size;
+ if (ssize == -1)
+ ssize = wcslen(ptr);
+ return PyUnicode_FromWideChar(ptr, ssize);
}
#endif
static struct PyModuleDef _ctypesmodule = {
- PyModuleDef_HEAD_INIT,
- "_ctypes",
- module_docs,
- -1,
- _ctypes_module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_ctypes",
+ module_docs,
+ -1,
+ _ctypes_module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__ctypes(void)
{
- PyObject *m;
+ PyObject *m;
/* Note:
ob_type is the metatype (the 'type'), defaults to PyType_Type,
tp_base is the base type, defaults to 'object' aka PyBaseObject_Type.
*/
#ifdef WITH_THREAD
- PyEval_InitThreads();
+ PyEval_InitThreads();
#endif
- m = PyModule_Create(&_ctypesmodule);
- if (!m)
- return NULL;
-
- _ctypes_ptrtype_cache = PyDict_New();
- if (_ctypes_ptrtype_cache == NULL)
- return NULL;
-
- PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache);
-
- _unpickle = PyObject_GetAttrString(m, "_unpickle");
- if (_unpickle == NULL)
- return NULL;
-
- if (PyType_Ready(&PyCArg_Type) < 0)
- return NULL;
-
- if (PyType_Ready(&PyCThunk_Type) < 0)
- return NULL;
-
- /* StgDict is derived from PyDict_Type */
- PyCStgDict_Type.tp_base = &PyDict_Type;
- if (PyType_Ready(&PyCStgDict_Type) < 0)
- return NULL;
-
- /*************************************************
- *
- * Metaclasses
- */
-
- PyCStructType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&PyCStructType_Type) < 0)
- return NULL;
-
- UnionType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&UnionType_Type) < 0)
- return NULL;
-
- PyCPointerType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&PyCPointerType_Type) < 0)
- return NULL;
-
- PyCArrayType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&PyCArrayType_Type) < 0)
- return NULL;
-
- PyCSimpleType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&PyCSimpleType_Type) < 0)
- return NULL;
-
- PyCFuncPtrType_Type.tp_base = &PyType_Type;
- if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
- return NULL;
-
- /*************************************************
- *
- * Classes using a custom metaclass
- */
-
- if (PyType_Ready(&PyCData_Type) < 0)
- return NULL;
-
- Py_TYPE(&Struct_Type) = &PyCStructType_Type;
- Struct_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&Struct_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
-
- Py_TYPE(&Union_Type) = &UnionType_Type;
- Union_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&Union_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
-
- Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
- PyCPointer_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&PyCPointer_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
-
- Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
- PyCArray_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&PyCArray_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
-
- Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
- Simple_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&Simple_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
-
- Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
- PyCFuncPtr_Type.tp_base = &PyCData_Type;
- if (PyType_Ready(&PyCFuncPtr_Type) < 0)
- return NULL;
- PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
-
- /*************************************************
- *
- * Simple classes
- */
-
- /* PyCField_Type is derived from PyBaseObject_Type */
- if (PyType_Ready(&PyCField_Type) < 0)
- return NULL;
-
- /*************************************************
- *
- * Other stuff
- */
-
- DictRemover_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&DictRemover_Type) < 0)
- return NULL;
+ m = PyModule_Create(&_ctypesmodule);
+ if (!m)
+ return NULL;
+
+ _ctypes_ptrtype_cache = PyDict_New();
+ if (_ctypes_ptrtype_cache == NULL)
+ return NULL;
+
+ PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache);
+
+ _unpickle = PyObject_GetAttrString(m, "_unpickle");
+ if (_unpickle == NULL)
+ return NULL;
+
+ if (PyType_Ready(&PyCArg_Type) < 0)
+ return NULL;
+
+ if (PyType_Ready(&PyCThunk_Type) < 0)
+ return NULL;
+
+ /* StgDict is derived from PyDict_Type */
+ PyCStgDict_Type.tp_base = &PyDict_Type;
+ if (PyType_Ready(&PyCStgDict_Type) < 0)
+ return NULL;
+
+ /*************************************************
+ *
+ * Metaclasses
+ */
+
+ PyCStructType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&PyCStructType_Type) < 0)
+ return NULL;
+
+ UnionType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&UnionType_Type) < 0)
+ return NULL;
+
+ PyCPointerType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&PyCPointerType_Type) < 0)
+ return NULL;
+
+ PyCArrayType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&PyCArrayType_Type) < 0)
+ return NULL;
+
+ PyCSimpleType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&PyCSimpleType_Type) < 0)
+ return NULL;
+
+ PyCFuncPtrType_Type.tp_base = &PyType_Type;
+ if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
+ return NULL;
+
+ /*************************************************
+ *
+ * Classes using a custom metaclass
+ */
+
+ if (PyType_Ready(&PyCData_Type) < 0)
+ return NULL;
+
+ Py_TYPE(&Struct_Type) = &PyCStructType_Type;
+ Struct_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&Struct_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
+
+ Py_TYPE(&Union_Type) = &UnionType_Type;
+ Union_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&Union_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
+
+ Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
+ PyCPointer_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&PyCPointer_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
+
+ Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
+ PyCArray_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&PyCArray_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
+
+ Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
+ Simple_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&Simple_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
+
+ Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
+ PyCFuncPtr_Type.tp_base = &PyCData_Type;
+ if (PyType_Ready(&PyCFuncPtr_Type) < 0)
+ return NULL;
+ PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
+
+ /*************************************************
+ *
+ * Simple classes
+ */
+
+ /* PyCField_Type is derived from PyBaseObject_Type */
+ if (PyType_Ready(&PyCField_Type) < 0)
+ return NULL;
+
+ /*************************************************
+ *
+ * Other stuff
+ */
+
+ DictRemover_Type.tp_new = PyType_GenericNew;
+ if (PyType_Ready(&DictRemover_Type) < 0)
+ return NULL;
#ifdef MS_WIN32
- if (create_comerror() < 0)
- return NULL;
- PyModule_AddObject(m, "COMError", ComError);
+ if (create_comerror() < 0)
+ return NULL;
+ PyModule_AddObject(m, "COMError", ComError);
- PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT));
- PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL));
+ PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT));
+ PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL));
#endif
- PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL));
- PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO));
- PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR));
- PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI));
- PyModule_AddStringConstant(m, "__version__", "1.1.0");
-
- PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
- PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
- PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
- PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
+ PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL));
+ PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO));
+ PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR));
+ PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI));
+ PyModule_AddStringConstant(m, "__version__", "1.1.0");
+
+ PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
+ PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
+ PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
+ PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
#ifdef CTYPES_UNICODE
- PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
+ PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
#endif
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
@@ -5410,15 +5410,15 @@ PyInit__ctypes(void)
#define RTLD_GLOBAL RTLD_LOCAL
#endif
- PyModule_AddObject(m, "RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL));
- PyModule_AddObject(m, "RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL));
-
- PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL);
- if (PyExc_ArgError) {
- Py_INCREF(PyExc_ArgError);
- PyModule_AddObject(m, "ArgumentError", PyExc_ArgError);
- }
- return m;
+ PyModule_AddObject(m, "RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL));
+ PyModule_AddObject(m, "RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL));
+
+ PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL);
+ if (PyExc_ArgError) {
+ Py_INCREF(PyExc_ArgError);
+ PyModule_AddObject(m, "ArgumentError", PyExc_ArgError);
+ }
+ return m;
}
/*
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
index 2d957de..36bd0a1 100644
--- a/Modules/_ctypes/_ctypes_test.c
+++ b/Modules/_ctypes/_ctypes_test.c
@@ -14,152 +14,152 @@
EXPORT(void)testfunc_array(int values[4])
{
- printf("testfunc_array %d %d %d %d\n",
- values[0],
- values[1],
- values[2],
- values[3]);
+ printf("testfunc_array %d %d %d %d\n",
+ values[0],
+ values[1],
+ values[2],
+ values[3]);
}
EXPORT(long double)testfunc_Ddd(double a, double b)
{
- long double result = (long double)(a * b);
- printf("testfunc_Ddd(%p, %p)\n", &a, &b);
- printf("testfunc_Ddd(%g, %g)\n", a, b);
- return result;
+ long double result = (long double)(a * b);
+ printf("testfunc_Ddd(%p, %p)\n", &a, &b);
+ printf("testfunc_Ddd(%g, %g)\n", a, b);
+ return result;
}
EXPORT(long double)testfunc_DDD(long double a, long double b)
{
- long double result = a * b;
- printf("testfunc_DDD(%p, %p)\n", &a, &b);
- printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
- return result;
+ long double result = a * b;
+ printf("testfunc_DDD(%p, %p)\n", &a, &b);
+ printf("testfunc_DDD(%Lg, %Lg)\n", a, b);
+ return result;
}
EXPORT(int)testfunc_iii(int a, int b)
{
- int result = a * b;
- printf("testfunc_iii(%p, %p)\n", &a, &b);
- return result;
+ int result = a * b;
+ printf("testfunc_iii(%p, %p)\n", &a, &b);
+ return result;
}
EXPORT(int)myprintf(char *fmt, ...)
{
- int result;
- va_list argptr;
- va_start(argptr, fmt);
- result = vprintf(fmt, argptr);
- va_end(argptr);
- return result;
+ int result;
+ va_list argptr;
+ va_start(argptr, fmt);
+ result = vprintf(fmt, argptr);
+ va_end(argptr);
+ return result;
}
EXPORT(char *)my_strtok(char *token, const char *delim)
{
- return strtok(token, delim);
+ return strtok(token, delim);
}
EXPORT(char *)my_strchr(const char *s, int c)
{
- return strchr(s, c);
+ return strchr(s, c);
}
EXPORT(double) my_sqrt(double a)
{
- return sqrt(a);
+ return sqrt(a);
}
EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*))
{
- qsort(base, num, width, compare);
+ qsort(base, num, width, compare);
}
EXPORT(int *) _testfunc_ai8(int a[8])
{
- return a;
+ return a;
}
EXPORT(void) _testfunc_v(int a, int b, int *presult)
{
- *presult = a + b;
+ *presult = a + b;
}
EXPORT(int) _testfunc_i_bhilfd(signed char b, short h, int i, long l, float f, double d)
{
-/* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
- b, h, i, l, f, d);
+/* printf("_testfunc_i_bhilfd got %d %d %d %ld %f %f\n",
+ b, h, i, l, f, d);
*/
- return (int)(b + h + i + l + f + d);
+ return (int)(b + h + i + l + f + d);
}
EXPORT(float) _testfunc_f_bhilfd(signed char b, short h, int i, long l, float f, double d)
{
-/* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
- b, h, i, l, f, d);
+/* printf("_testfunc_f_bhilfd got %d %d %d %ld %f %f\n",
+ b, h, i, l, f, d);
*/
- return (float)(b + h + i + l + f + d);
+ return (float)(b + h + i + l + f + d);
}
EXPORT(double) _testfunc_d_bhilfd(signed char b, short h, int i, long l, float f, double d)
{
-/* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
- b, h, i, l, f, d);
+/* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
+ b, h, i, l, f, d);
*/
- return (double)(b + h + i + l + f + d);
+ return (double)(b + h + i + l + f + d);
}
EXPORT(long double) _testfunc_D_bhilfD(signed char b, short h, int i, long l, float f, long double d)
{
-/* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
- b, h, i, l, f, d);
+/* printf("_testfunc_d_bhilfd got %d %d %d %ld %f %f\n",
+ b, h, i, l, f, d);
*/
- return (long double)(b + h + i + l + f + d);
+ return (long double)(b + h + i + l + f + d);
}
EXPORT(char *) _testfunc_p_p(void *s)
{
- return (char *)s;
+ return (char *)s;
}
EXPORT(void *) _testfunc_c_p_p(int *argcp, char **argv)
{
- return argv[(*argcp)-1];
+ return argv[(*argcp)-1];
}
EXPORT(void *) get_strchr(void)
{
- return (void *)strchr;
+ return (void *)strchr;
}
EXPORT(char *) my_strdup(char *src)
{
- char *dst = (char *)malloc(strlen(src)+1);
- if (!dst)
- return NULL;
- strcpy(dst, src);
- return dst;
+ char *dst = (char *)malloc(strlen(src)+1);
+ if (!dst)
+ return NULL;
+ strcpy(dst, src);
+ return dst;
}
EXPORT(void)my_free(void *ptr)
{
- free(ptr);
+ free(ptr);
}
#ifdef HAVE_WCHAR_H
EXPORT(wchar_t *) my_wcsdup(wchar_t *src)
{
- size_t len = wcslen(src);
- wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
- if (ptr == NULL)
- return NULL;
- memcpy(ptr, src, (len+1) * sizeof(wchar_t));
- return ptr;
+ size_t len = wcslen(src);
+ wchar_t *ptr = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
+ if (ptr == NULL)
+ return NULL;
+ memcpy(ptr, src, (len+1) * sizeof(wchar_t));
+ return ptr;
}
EXPORT(size_t) my_wcslen(wchar_t *src)
{
- return wcslen(src);
+ return wcslen(src);
}
#endif
@@ -170,158 +170,158 @@ EXPORT(size_t) my_wcslen(wchar_t *src)
#endif
typedef struct {
- int (*c)(int, int);
- int (__stdcall *s)(int, int);
+ int (*c)(int, int);
+ int (__stdcall *s)(int, int);
} FUNCS;
EXPORT(int) _testfunc_callfuncp(FUNCS *fp)
{
- fp->c(1, 2);
- fp->s(3, 4);
- return 0;
+ fp->c(1, 2);
+ fp->s(3, 4);
+ return 0;
}
EXPORT(int) _testfunc_deref_pointer(int *pi)
{
- return *pi;
+ return *pi;
}
#ifdef MS_WIN32
EXPORT(int) _testfunc_piunk(IUnknown FAR *piunk)
{
- piunk->lpVtbl->AddRef(piunk);
- return piunk->lpVtbl->Release(piunk);
+ piunk->lpVtbl->AddRef(piunk);
+ return piunk->lpVtbl->Release(piunk);
}
#endif
EXPORT(int) _testfunc_callback_with_pointer(int (*func)(int *))
{
- int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ int table[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- return (*func)(table);
+ return (*func)(table);
}
#ifdef HAVE_LONG_LONG
EXPORT(PY_LONG_LONG) _testfunc_q_bhilfdq(signed char b, short h, int i, long l, float f,
- double d, PY_LONG_LONG q)
+ double d, PY_LONG_LONG q)
{
- return (PY_LONG_LONG)(b + h + i + l + f + d + q);
+ return (PY_LONG_LONG)(b + h + i + l + f + d + q);
}
EXPORT(PY_LONG_LONG) _testfunc_q_bhilfd(signed char b, short h, int i, long l, float f, double d)
{
- return (PY_LONG_LONG)(b + h + i + l + f + d);
+ return (PY_LONG_LONG)(b + h + i + l + f + d);
}
EXPORT(int) _testfunc_callback_i_if(int value, int (*func)(int))
{
- int sum = 0;
- while (value != 0) {
- sum += func(value);
- value /= 2;
- }
- return sum;
+ int sum = 0;
+ while (value != 0) {
+ sum += func(value);
+ value /= 2;
+ }
+ return sum;
}
EXPORT(PY_LONG_LONG) _testfunc_callback_q_qf(PY_LONG_LONG value,
- PY_LONG_LONG (*func)(PY_LONG_LONG))
+ PY_LONG_LONG (*func)(PY_LONG_LONG))
{
- PY_LONG_LONG sum = 0;
+ PY_LONG_LONG sum = 0;
- while (value != 0) {
- sum += func(value);
- value /= 2;
- }
- return sum;
+ while (value != 0) {
+ sum += func(value);
+ value /= 2;
+ }
+ return sum;
}
#endif
typedef struct {
- char *name;
- char *value;
+ char *name;
+ char *value;
} SPAM;
typedef struct {
- char *name;
- int num_spams;
- SPAM *spams;
+ char *name;
+ int num_spams;
+ SPAM *spams;
} EGG;
SPAM my_spams[2] = {
- { "name1", "value1" },
- { "name2", "value2" },
+ { "name1", "value1" },
+ { "name2", "value2" },
};
EGG my_eggs[1] = {
- { "first egg", 1, my_spams }
+ { "first egg", 1, my_spams }
};
EXPORT(int) getSPAMANDEGGS(EGG **eggs)
{
- *eggs = my_eggs;
- return 1;
+ *eggs = my_eggs;
+ return 1;
}
typedef struct tagpoint {
- int x;
- int y;
+ int x;
+ int y;
} point;
EXPORT(int) _testfunc_byval(point in, point *pout)
{
- if (pout) {
- pout->x = in.x;
- pout->y = in.y;
- }
- return in.x + in.y;
+ if (pout) {
+ pout->x = in.x;
+ pout->y = in.y;
+ }
+ return in.x + in.y;
}
EXPORT (int) an_integer = 42;
EXPORT(int) get_an_integer(void)
{
- return an_integer;
+ return an_integer;
}
EXPORT(double)
integrate(double a, double b, double (*f)(double), long nstep)
{
- double x, sum=0.0, dx=(b-a)/(double)nstep;
- for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
- sum += f(x);
- return sum/(double)nstep;
+ double x, sum=0.0, dx=(b-a)/(double)nstep;
+ for(x=a+0.5*dx; (b-x)*(x-a)>0.0; x+=dx)
+ sum += f(x);
+ return sum/(double)nstep;
}
typedef struct {
- void (*initialize)(void *(*)(int), void(*)(void *));
+ void (*initialize)(void *(*)(int), void(*)(void *));
} xxx_library;
static void _xxx_init(void *(*Xalloc)(int), void (*Xfree)(void *))
{
- void *ptr;
-
- printf("_xxx_init got %p %p\n", Xalloc, Xfree);
- printf("calling\n");
- ptr = Xalloc(32);
- Xfree(ptr);
- printf("calls done, ptr was %p\n", ptr);
+ void *ptr;
+
+ printf("_xxx_init got %p %p\n", Xalloc, Xfree);
+ printf("calling\n");
+ ptr = Xalloc(32);
+ Xfree(ptr);
+ printf("calls done, ptr was %p\n", ptr);
}
xxx_library _xxx_lib = {
- _xxx_init
+ _xxx_init
};
EXPORT(xxx_library) *library_get(void)
{
- return &_xxx_lib;
+ return &_xxx_lib;
}
#ifdef MS_WIN32
/* See Don Box (german), pp 79ff. */
EXPORT(void) GetString(BSTR *pbstr)
{
- *pbstr = SysAllocString(L"Goodbye!");
+ *pbstr = SysAllocString(L"Goodbye!");
}
#endif
@@ -330,12 +330,12 @@ EXPORT(void) GetString(BSTR *pbstr)
*/
PyObject *py_func_si(PyObject *self, PyObject *args)
{
- char *name;
- int i;
- if (!PyArg_ParseTuple(args, "si", &name, &i))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ char *name;
+ int i;
+ if (!PyArg_ParseTuple(args, "si", &name, &i))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
EXPORT(void) _py_func_si(char *s, int i)
@@ -344,8 +344,8 @@ EXPORT(void) _py_func_si(char *s, int i)
PyObject *py_func(PyObject *self, PyObject *args)
{
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
EXPORT(void) _py_func(void)
@@ -356,64 +356,64 @@ EXPORT(PY_LONG_LONG) last_tf_arg_s;
EXPORT(unsigned PY_LONG_LONG) last_tf_arg_u;
struct BITS {
- int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
- short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
+ int A: 1, B:2, C:3, D:4, E: 5, F: 6, G: 7, H: 8, I: 9;
+ short M: 1, N: 2, O: 3, P: 4, Q: 5, R: 6, S: 7;
};
EXPORT(void) set_bitfields(struct BITS *bits, char name, int value)
{
- switch (name) {
- case 'A': bits->A = value; break;
- case 'B': bits->B = value; break;
- case 'C': bits->C = value; break;
- case 'D': bits->D = value; break;
- case 'E': bits->E = value; break;
- case 'F': bits->F = value; break;
- case 'G': bits->G = value; break;
- case 'H': bits->H = value; break;
- case 'I': bits->I = value; break;
-
- case 'M': bits->M = value; break;
- case 'N': bits->N = value; break;
- case 'O': bits->O = value; break;
- case 'P': bits->P = value; break;
- case 'Q': bits->Q = value; break;
- case 'R': bits->R = value; break;
- case 'S': bits->S = value; break;
- }
+ switch (name) {
+ case 'A': bits->A = value; break;
+ case 'B': bits->B = value; break;
+ case 'C': bits->C = value; break;
+ case 'D': bits->D = value; break;
+ case 'E': bits->E = value; break;
+ case 'F': bits->F = value; break;
+ case 'G': bits->G = value; break;
+ case 'H': bits->H = value; break;
+ case 'I': bits->I = value; break;
+
+ case 'M': bits->M = value; break;
+ case 'N': bits->N = value; break;
+ case 'O': bits->O = value; break;
+ case 'P': bits->P = value; break;
+ case 'Q': bits->Q = value; break;
+ case 'R': bits->R = value; break;
+ case 'S': bits->S = value; break;
+ }
}
EXPORT(int) unpack_bitfields(struct BITS *bits, char name)
{
- switch (name) {
- case 'A': return bits->A;
- case 'B': return bits->B;
- case 'C': return bits->C;
- case 'D': return bits->D;
- case 'E': return bits->E;
- case 'F': return bits->F;
- case 'G': return bits->G;
- case 'H': return bits->H;
- case 'I': return bits->I;
-
- case 'M': return bits->M;
- case 'N': return bits->N;
- case 'O': return bits->O;
- case 'P': return bits->P;
- case 'Q': return bits->Q;
- case 'R': return bits->R;
- case 'S': return bits->S;
- }
- return 0;
+ switch (name) {
+ case 'A': return bits->A;
+ case 'B': return bits->B;
+ case 'C': return bits->C;
+ case 'D': return bits->D;
+ case 'E': return bits->E;
+ case 'F': return bits->F;
+ case 'G': return bits->G;
+ case 'H': return bits->H;
+ case 'I': return bits->I;
+
+ case 'M': return bits->M;
+ case 'N': return bits->N;
+ case 'O': return bits->O;
+ case 'P': return bits->P;
+ case 'Q': return bits->Q;
+ case 'R': return bits->R;
+ case 'S': return bits->S;
+ }
+ return 0;
}
static PyMethodDef module_methods[] = {
-/* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
- {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
+/* {"get_last_tf_arg_s", get_last_tf_arg_s, METH_NOARGS},
+ {"get_last_tf_arg_u", get_last_tf_arg_u, METH_NOARGS},
*/
- {"func_si", py_func_si, METH_VARARGS},
- {"func", py_func, METH_NOARGS},
- { NULL, NULL, 0, NULL},
+ {"func_si", py_func_si, METH_VARARGS},
+ {"func", py_func, METH_NOARGS},
+ { NULL, NULL, 0, NULL},
};
#define S last_tf_arg_s = (PY_LONG_LONG)c
@@ -483,80 +483,80 @@ EXPORT(void) __stdcall s_tv_i(int c) { S; return; }
#endif
/********/
-
+
#ifndef MS_WIN32
typedef struct {
- long x;
- long y;
+ long x;
+ long y;
} POINT;
typedef struct {
- long left;
- long top;
- long right;
- long bottom;
+ long left;
+ long top;
+ long right;
+ long bottom;
} RECT;
#endif
EXPORT(int) PointInRect(RECT *prc, POINT pt)
{
- if (pt.x < prc->left)
- return 0;
- if (pt.x > prc->right)
- return 0;
- if (pt.y < prc->top)
- return 0;
- if (pt.y > prc->bottom)
- return 0;
- return 1;
+ if (pt.x < prc->left)
+ return 0;
+ if (pt.x > prc->right)
+ return 0;
+ if (pt.y < prc->top)
+ return 0;
+ if (pt.y > prc->bottom)
+ return 0;
+ return 1;
}
typedef struct {
- short x;
- short y;
+ short x;
+ short y;
} S2H;
EXPORT(S2H) ret_2h_func(S2H inp)
{
- inp.x *= 2;
- inp.y *= 3;
- return inp;
+ inp.x *= 2;
+ inp.y *= 3;
+ return inp;
}
typedef struct {
- int a, b, c, d, e, f, g, h;
+ int a, b, c, d, e, f, g, h;
} S8I;
EXPORT(S8I) ret_8i_func(S8I inp)
{
- inp.a *= 2;
- inp.b *= 3;
- inp.c *= 4;
- inp.d *= 5;
- inp.e *= 6;
- inp.f *= 7;
- inp.g *= 8;
- inp.h *= 9;
- return inp;
+ inp.a *= 2;
+ inp.b *= 3;
+ inp.c *= 4;
+ inp.d *= 5;
+ inp.e *= 6;
+ inp.f *= 7;
+ inp.g *= 8;
+ inp.h *= 9;
+ return inp;
}
EXPORT(int) GetRectangle(int flag, RECT *prect)
{
- if (flag == 0)
- return 0;
- prect->left = (int)flag;
- prect->top = (int)flag + 1;
- prect->right = (int)flag + 2;
- prect->bottom = (int)flag + 3;
- return 1;
+ if (flag == 0)
+ return 0;
+ prect->left = (int)flag;
+ prect->top = (int)flag + 1;
+ prect->right = (int)flag + 2;
+ prect->bottom = (int)flag + 3;
+ return 1;
}
EXPORT(void) TwoOutArgs(int a, int *pi, int b, int *pj)
{
- *pi += a;
- *pj += b;
+ *pi += a;
+ *pj += b;
}
#ifdef MS_WIN32
@@ -571,32 +571,32 @@ EXPORT(S8I) __stdcall s_ret_8i_func(S8I inp) { return ret_8i_func(inp); }
EXPORT (HRESULT) KeepObject(IUnknown *punk)
{
- static IUnknown *pobj;
- if (punk)
- punk->lpVtbl->AddRef(punk);
- if (pobj)
- pobj->lpVtbl->Release(pobj);
- pobj = punk;
- return S_OK;
+ static IUnknown *pobj;
+ if (punk)
+ punk->lpVtbl->AddRef(punk);
+ if (pobj)
+ pobj->lpVtbl->Release(pobj);
+ pobj = punk;
+ return S_OK;
}
#endif
static struct PyModuleDef _ctypes_testmodule = {
- PyModuleDef_HEAD_INIT,
- "_ctypes_test",
- NULL,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_ctypes_test",
+ NULL,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__ctypes_test(void)
{
- return PyModule_Create(&_ctypes_testmodule);
+ return PyModule_Create(&_ctypes_testmodule);
}
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 9b2cd3f..5c82f77 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -12,65 +12,65 @@
static void
CThunkObject_dealloc(PyObject *_self)
{
- CThunkObject *self = (CThunkObject *)_self;
- Py_XDECREF(self->converters);
- Py_XDECREF(self->callable);
- Py_XDECREF(self->restype);
- if (self->pcl)
- _ctypes_free_closure(self->pcl);
- PyObject_GC_Del(self);
+ CThunkObject *self = (CThunkObject *)_self;
+ Py_XDECREF(self->converters);
+ Py_XDECREF(self->callable);
+ Py_XDECREF(self->restype);
+ if (self->pcl)
+ _ctypes_free_closure(self->pcl);
+ PyObject_GC_Del(self);
}
static int
CThunkObject_traverse(PyObject *_self, visitproc visit, void *arg)
{
- CThunkObject *self = (CThunkObject *)_self;
- Py_VISIT(self->converters);
- Py_VISIT(self->callable);
- Py_VISIT(self->restype);
- return 0;
+ CThunkObject *self = (CThunkObject *)_self;
+ Py_VISIT(self->converters);
+ Py_VISIT(self->callable);
+ Py_VISIT(self->restype);
+ return 0;
}
static int
CThunkObject_clear(PyObject *_self)
{
- CThunkObject *self = (CThunkObject *)_self;
- Py_CLEAR(self->converters);
- Py_CLEAR(self->callable);
- Py_CLEAR(self->restype);
- return 0;
+ CThunkObject *self = (CThunkObject *)_self;
+ Py_CLEAR(self->converters);
+ Py_CLEAR(self->callable);
+ Py_CLEAR(self->restype);
+ return 0;
}
PyTypeObject PyCThunk_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.CThunkObject",
- sizeof(CThunkObject), /* tp_basicsize */
- sizeof(ffi_type), /* tp_itemsize */
- CThunkObject_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "CThunkObject", /* tp_doc */
- CThunkObject_traverse, /* tp_traverse */
- CThunkObject_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.CThunkObject",
+ sizeof(CThunkObject), /* tp_basicsize */
+ sizeof(ffi_type), /* tp_itemsize */
+ CThunkObject_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "CThunkObject", /* tp_doc */
+ CThunkObject_traverse, /* tp_traverse */
+ CThunkObject_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
};
/**************************************************************/
@@ -78,75 +78,75 @@ PyTypeObject PyCThunk_Type = {
static void
PrintError(char *msg, ...)
{
- char buf[512];
- PyObject *f = PySys_GetObject("stderr");
- va_list marker;
-
- va_start(marker, msg);
- vsnprintf(buf, sizeof(buf), msg, marker);
- va_end(marker);
- if (f != NULL && f != Py_None)
- PyFile_WriteString(buf, f);
- PyErr_Print();
+ char buf[512];
+ PyObject *f = PySys_GetObject("stderr");
+ va_list marker;
+
+ va_start(marker, msg);
+ vsnprintf(buf, sizeof(buf), msg, marker);
+ va_end(marker);
+ if (f != NULL && f != Py_None)
+ PyFile_WriteString(buf, f);
+ PyErr_Print();
}
/* after code that pyrex generates */
void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
{
- PyObject *py_srcfile = 0;
- PyObject *py_funcname = 0;
- PyObject *py_globals = 0;
- PyObject *empty_tuple = 0;
- PyObject *empty_string = 0;
- PyCodeObject *py_code = 0;
- PyFrameObject *py_frame = 0;
-
- py_srcfile = PyUnicode_DecodeFSDefault(filename);
- if (!py_srcfile) goto bad;
- py_funcname = PyUnicode_FromString(funcname);
- if (!py_funcname) goto bad;
- py_globals = PyDict_New();
- if (!py_globals) goto bad;
- empty_tuple = PyTuple_New(0);
- if (!empty_tuple) goto bad;
- empty_string = PyBytes_FromString("");
- if (!empty_string) goto bad;
- py_code = PyCode_New(
- 0, /*int argcount,*/
- 0, /*int kwonlyargcount,*/
- 0, /*int nlocals,*/
- 0, /*int stacksize,*/
- 0, /*int flags,*/
- empty_string, /*PyObject *code,*/
- empty_tuple, /*PyObject *consts,*/
- empty_tuple, /*PyObject *names,*/
- empty_tuple, /*PyObject *varnames,*/
- empty_tuple, /*PyObject *freevars,*/
- empty_tuple, /*PyObject *cellvars,*/
- py_srcfile, /*PyObject *filename,*/
- py_funcname, /*PyObject *name,*/
- lineno, /*int firstlineno,*/
- empty_string /*PyObject *lnotab*/
- );
- if (!py_code) goto bad;
- py_frame = PyFrame_New(
- PyThreadState_Get(), /*PyThreadState *tstate,*/
- py_code, /*PyCodeObject *code,*/
- py_globals, /*PyObject *globals,*/
- 0 /*PyObject *locals*/
- );
- if (!py_frame) goto bad;
- py_frame->f_lineno = lineno;
- PyTraceBack_Here(py_frame);
+ PyObject *py_srcfile = 0;
+ PyObject *py_funcname = 0;
+ PyObject *py_globals = 0;
+ PyObject *empty_tuple = 0;
+ PyObject *empty_string = 0;
+ PyCodeObject *py_code = 0;
+ PyFrameObject *py_frame = 0;
+
+ py_srcfile = PyUnicode_DecodeFSDefault(filename);
+ if (!py_srcfile) goto bad;
+ py_funcname = PyUnicode_FromString(funcname);
+ if (!py_funcname) goto bad;
+ py_globals = PyDict_New();
+ if (!py_globals) goto bad;
+ empty_tuple = PyTuple_New(0);
+ if (!empty_tuple) goto bad;
+ empty_string = PyBytes_FromString("");
+ if (!empty_string) goto bad;
+ py_code = PyCode_New(
+ 0, /*int argcount,*/
+ 0, /*int kwonlyargcount,*/
+ 0, /*int nlocals,*/
+ 0, /*int stacksize,*/
+ 0, /*int flags,*/
+ empty_string, /*PyObject *code,*/
+ empty_tuple, /*PyObject *consts,*/
+ empty_tuple, /*PyObject *names,*/
+ empty_tuple, /*PyObject *varnames,*/
+ empty_tuple, /*PyObject *freevars,*/
+ empty_tuple, /*PyObject *cellvars,*/
+ py_srcfile, /*PyObject *filename,*/
+ py_funcname, /*PyObject *name,*/
+ lineno, /*int firstlineno,*/
+ empty_string /*PyObject *lnotab*/
+ );
+ if (!py_code) goto bad;
+ py_frame = PyFrame_New(
+ PyThreadState_Get(), /*PyThreadState *tstate,*/
+ py_code, /*PyCodeObject *code,*/
+ py_globals, /*PyObject *globals,*/
+ 0 /*PyObject *locals*/
+ );
+ if (!py_frame) goto bad;
+ py_frame->f_lineno = lineno;
+ PyTraceBack_Here(py_frame);
bad:
- Py_XDECREF(py_globals);
- Py_XDECREF(py_srcfile);
- Py_XDECREF(py_funcname);
- Py_XDECREF(empty_tuple);
- Py_XDECREF(empty_string);
- Py_XDECREF(py_code);
- Py_XDECREF(py_frame);
+ Py_XDECREF(py_globals);
+ Py_XDECREF(py_srcfile);
+ Py_XDECREF(py_funcname);
+ Py_XDECREF(empty_tuple);
+ Py_XDECREF(empty_string);
+ Py_XDECREF(py_code);
+ Py_XDECREF(py_frame);
}
#ifdef MS_WIN32
@@ -163,15 +163,15 @@ void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
static void
TryAddRef(StgDictObject *dict, CDataObject *obj)
{
- IUnknown *punk;
+ IUnknown *punk;
- if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_"))
- return;
+ if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_"))
+ return;
- punk = *(IUnknown **)obj->b_ptr;
- if (punk)
- punk->lpVtbl->AddRef(punk);
- return;
+ punk = *(IUnknown **)obj->b_ptr;
+ if (punk)
+ punk->lpVtbl->AddRef(punk);
+ return;
}
#endif
@@ -181,421 +181,421 @@ TryAddRef(StgDictObject *dict, CDataObject *obj)
*
*/
static void _CallPythonObject(void *mem,
- ffi_type *restype,
- SETFUNC setfunc,
- PyObject *callable,
- PyObject *converters,
- int flags,
- void **pArgs)
+ ffi_type *restype,
+ SETFUNC setfunc,
+ PyObject *callable,
+ PyObject *converters,
+ int flags,
+ void **pArgs)
{
- Py_ssize_t i;
- PyObject *result;
- PyObject *arglist = NULL;
- Py_ssize_t nArgs;
- PyObject *error_object = NULL;
- int *space;
+ Py_ssize_t i;
+ PyObject *result;
+ PyObject *arglist = NULL;
+ Py_ssize_t nArgs;
+ PyObject *error_object = NULL;
+ int *space;
#ifdef WITH_THREAD
- PyGILState_STATE state = PyGILState_Ensure();
+ PyGILState_STATE state = PyGILState_Ensure();
#endif
- nArgs = PySequence_Length(converters);
- /* Hm. What to return in case of error?
- For COM, 0xFFFFFFFF seems better than 0.
- */
- if (nArgs < 0) {
- PrintError("BUG: PySequence_Length");
- goto Done;
- }
-
- arglist = PyTuple_New(nArgs);
- if (!arglist) {
- PrintError("PyTuple_New()");
- goto Done;
- }
- for (i = 0; i < nArgs; ++i) {
- /* Note: new reference! */
- PyObject *cnv = PySequence_GetItem(converters, i);
- StgDictObject *dict;
- if (cnv)
- dict = PyType_stgdict(cnv);
- else {
- PrintError("Getting argument converter %d\n", i);
- goto Done;
- }
-
- if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
- PyObject *v = dict->getfunc(*pArgs, dict->size);
- if (!v) {
- PrintError("create argument %d:\n", i);
- Py_DECREF(cnv);
- goto Done;
- }
- PyTuple_SET_ITEM(arglist, i, v);
- /* XXX XXX XX
- We have the problem that c_byte or c_short have dict->size of
- 1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
- BTW, the same problem occurrs when they are pushed as parameters
- */
- } else if (dict) {
- /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
- CDataObject *obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
- if (!obj) {
- PrintError("create argument %d:\n", i);
- Py_DECREF(cnv);
- goto Done;
- }
- if (!CDataObject_Check(obj)) {
- Py_DECREF(obj);
- Py_DECREF(cnv);
- PrintError("unexpected result of create argument %d:\n", i);
- goto Done;
- }
- memcpy(obj->b_ptr, *pArgs, dict->size);
- PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
+ nArgs = PySequence_Length(converters);
+ /* Hm. What to return in case of error?
+ For COM, 0xFFFFFFFF seems better than 0.
+ */
+ if (nArgs < 0) {
+ PrintError("BUG: PySequence_Length");
+ goto Done;
+ }
+
+ arglist = PyTuple_New(nArgs);
+ if (!arglist) {
+ PrintError("PyTuple_New()");
+ goto Done;
+ }
+ for (i = 0; i < nArgs; ++i) {
+ /* Note: new reference! */
+ PyObject *cnv = PySequence_GetItem(converters, i);
+ StgDictObject *dict;
+ if (cnv)
+ dict = PyType_stgdict(cnv);
+ else {
+ PrintError("Getting argument converter %d\n", i);
+ goto Done;
+ }
+
+ if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
+ PyObject *v = dict->getfunc(*pArgs, dict->size);
+ if (!v) {
+ PrintError("create argument %d:\n", i);
+ Py_DECREF(cnv);
+ goto Done;
+ }
+ PyTuple_SET_ITEM(arglist, i, v);
+ /* XXX XXX XX
+ We have the problem that c_byte or c_short have dict->size of
+ 1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
+ BTW, the same problem occurrs when they are pushed as parameters
+ */
+ } else if (dict) {
+ /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
+ CDataObject *obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
+ if (!obj) {
+ PrintError("create argument %d:\n", i);
+ Py_DECREF(cnv);
+ goto Done;
+ }
+ if (!CDataObject_Check(obj)) {
+ Py_DECREF(obj);
+ Py_DECREF(cnv);
+ PrintError("unexpected result of create argument %d:\n", i);
+ goto Done;
+ }
+ memcpy(obj->b_ptr, *pArgs, dict->size);
+ PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
#ifdef MS_WIN32
- TryAddRef(dict, obj);
+ TryAddRef(dict, obj);
#endif
- } else {
- PyErr_SetString(PyExc_TypeError,
- "cannot build parameter");
- PrintError("Parsing argument %d\n", i);
- Py_DECREF(cnv);
- goto Done;
- }
- Py_DECREF(cnv);
- /* XXX error handling! */
- pArgs++;
- }
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "cannot build parameter");
+ PrintError("Parsing argument %d\n", i);
+ Py_DECREF(cnv);
+ goto Done;
+ }
+ Py_DECREF(cnv);
+ /* XXX error handling! */
+ pArgs++;
+ }
#define CHECK(what, x) \
if (x == NULL) _ctypes_add_traceback(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print()
- if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
- error_object = _ctypes_get_errobj(&space);
- if (error_object == NULL)
- goto Done;
- if (flags & FUNCFLAG_USE_ERRNO) {
- int temp = space[0];
- space[0] = errno;
- errno = temp;
- }
+ if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
+ error_object = _ctypes_get_errobj(&space);
+ if (error_object == NULL)
+ goto Done;
+ if (flags & FUNCFLAG_USE_ERRNO) {
+ int temp = space[0];
+ space[0] = errno;
+ errno = temp;
+ }
#ifdef MS_WIN32
- if (flags & FUNCFLAG_USE_LASTERROR) {
- int temp = space[1];
- space[1] = GetLastError();
- SetLastError(temp);
- }
+ if (flags & FUNCFLAG_USE_LASTERROR) {
+ int temp = space[1];
+ space[1] = GetLastError();
+ SetLastError(temp);
+ }
#endif
- }
+ }
- result = PyObject_CallObject(callable, arglist);
- CHECK("'calling callback function'", result);
+ result = PyObject_CallObject(callable, arglist);
+ CHECK("'calling callback function'", result);
#ifdef MS_WIN32
- if (flags & FUNCFLAG_USE_LASTERROR) {
- int temp = space[1];
- space[1] = GetLastError();
- SetLastError(temp);
- }
+ if (flags & FUNCFLAG_USE_LASTERROR) {
+ int temp = space[1];
+ space[1] = GetLastError();
+ SetLastError(temp);
+ }
#endif
- if (flags & FUNCFLAG_USE_ERRNO) {
- int temp = space[0];
- space[0] = errno;
- errno = temp;
- }
- Py_XDECREF(error_object);
-
- if ((restype != &ffi_type_void) && result) {
- PyObject *keep;
- assert(setfunc);
+ if (flags & FUNCFLAG_USE_ERRNO) {
+ int temp = space[0];
+ space[0] = errno;
+ errno = temp;
+ }
+ Py_XDECREF(error_object);
+
+ if ((restype != &ffi_type_void) && result) {
+ PyObject *keep;
+ assert(setfunc);
#ifdef WORDS_BIGENDIAN
- /* See the corresponding code in callproc.c, around line 961 */
- if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg))
- mem = (char *)mem + sizeof(ffi_arg) - restype->size;
+ /* See the corresponding code in callproc.c, around line 961 */
+ if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg))
+ mem = (char *)mem + sizeof(ffi_arg) - restype->size;
#endif
- keep = setfunc(mem, result, 0);
- CHECK("'converting callback result'", keep);
- /* keep is an object we have to keep alive so that the result
- stays valid. If there is no such object, the setfunc will
- have returned Py_None.
-
- If there is such an object, we have no choice than to keep
- it alive forever - but a refcount and/or memory leak will
- be the result. EXCEPT when restype is py_object - Python
- itself knows how to manage the refcount of these objects.
- */
- if (keep == NULL) /* Could not convert callback result. */
- PyErr_WriteUnraisable(callable);
- else if (keep == Py_None) /* Nothing to keep */
- Py_DECREF(keep);
- else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
- if (-1 == PyErr_WarnEx(PyExc_RuntimeWarning,
- "memory leak in callback function.",
- 1))
- PyErr_WriteUnraisable(callable);
- }
- }
- Py_XDECREF(result);
+ keep = setfunc(mem, result, 0);
+ CHECK("'converting callback result'", keep);
+ /* keep is an object we have to keep alive so that the result
+ stays valid. If there is no such object, the setfunc will
+ have returned Py_None.
+
+ If there is such an object, we have no choice than to keep
+ it alive forever - but a refcount and/or memory leak will
+ be the result. EXCEPT when restype is py_object - Python
+ itself knows how to manage the refcount of these objects.
+ */
+ if (keep == NULL) /* Could not convert callback result. */
+ PyErr_WriteUnraisable(callable);
+ else if (keep == Py_None) /* Nothing to keep */
+ Py_DECREF(keep);
+ else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
+ if (-1 == PyErr_WarnEx(PyExc_RuntimeWarning,
+ "memory leak in callback function.",
+ 1))
+ PyErr_WriteUnraisable(callable);
+ }
+ }
+ Py_XDECREF(result);
Done:
- Py_XDECREF(arglist);
+ Py_XDECREF(arglist);
#ifdef WITH_THREAD
- PyGILState_Release(state);
+ PyGILState_Release(state);
#endif
}
static void closure_fcn(ffi_cif *cif,
- void *resp,
- void **args,
- void *userdata)
+ void *resp,
+ void **args,
+ void *userdata)
{
- CThunkObject *p = (CThunkObject *)userdata;
-
- _CallPythonObject(resp,
- p->ffi_restype,
- p->setfunc,
- p->callable,
- p->converters,
- p->flags,
- args);
+ CThunkObject *p = (CThunkObject *)userdata;
+
+ _CallPythonObject(resp,
+ p->ffi_restype,
+ p->setfunc,
+ p->callable,
+ p->converters,
+ p->flags,
+ args);
}
static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
{
- CThunkObject *p;
- int i;
-
- p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
- if (p == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- p->pcl = NULL;
- memset(&p->cif, 0, sizeof(p->cif));
- p->converters = NULL;
- p->callable = NULL;
- p->setfunc = NULL;
- p->ffi_restype = NULL;
-
- for (i = 0; i < nArgs + 1; ++i)
- p->atypes[i] = NULL;
- PyObject_GC_Track((PyObject *)p);
- return p;
+ CThunkObject *p;
+ int i;
+
+ p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
+ if (p == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ p->pcl = NULL;
+ memset(&p->cif, 0, sizeof(p->cif));
+ p->converters = NULL;
+ p->callable = NULL;
+ p->setfunc = NULL;
+ p->ffi_restype = NULL;
+
+ for (i = 0; i < nArgs + 1; ++i)
+ p->atypes[i] = NULL;
+ PyObject_GC_Track((PyObject *)p);
+ return p;
}
CThunkObject *_ctypes_alloc_callback(PyObject *callable,
- PyObject *converters,
- PyObject *restype,
- int flags)
+ PyObject *converters,
+ PyObject *restype,
+ int flags)
{
- int result;
- CThunkObject *p;
- Py_ssize_t nArgs, i;
- ffi_abi cc;
-
- nArgs = PySequence_Size(converters);
- p = CThunkObject_new(nArgs);
- if (p == NULL)
- return NULL;
-
- assert(CThunk_CheckExact((PyObject *)p));
-
- p->pcl = _ctypes_alloc_closure();
- if (p->pcl == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- p->flags = flags;
- for (i = 0; i < nArgs; ++i) {
- PyObject *cnv = PySequence_GetItem(converters, i);
- if (cnv == NULL)
- goto error;
- p->atypes[i] = _ctypes_get_ffi_type(cnv);
- Py_DECREF(cnv);
- }
- p->atypes[i] = NULL;
-
- Py_INCREF(restype);
- p->restype = restype;
- if (restype == Py_None) {
- p->setfunc = NULL;
- p->ffi_restype = &ffi_type_void;
- } else {
- StgDictObject *dict = PyType_stgdict(restype);
- if (dict == NULL || dict->setfunc == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "invalid result type for callback function");
- goto error;
- }
- p->setfunc = dict->setfunc;
- p->ffi_restype = &dict->ffi_type_pointer;
- }
-
- cc = FFI_DEFAULT_ABI;
+ int result;
+ CThunkObject *p;
+ Py_ssize_t nArgs, i;
+ ffi_abi cc;
+
+ nArgs = PySequence_Size(converters);
+ p = CThunkObject_new(nArgs);
+ if (p == NULL)
+ return NULL;
+
+ assert(CThunk_CheckExact((PyObject *)p));
+
+ p->pcl = _ctypes_alloc_closure();
+ if (p->pcl == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+
+ p->flags = flags;
+ for (i = 0; i < nArgs; ++i) {
+ PyObject *cnv = PySequence_GetItem(converters, i);
+ if (cnv == NULL)
+ goto error;
+ p->atypes[i] = _ctypes_get_ffi_type(cnv);
+ Py_DECREF(cnv);
+ }
+ p->atypes[i] = NULL;
+
+ Py_INCREF(restype);
+ p->restype = restype;
+ if (restype == Py_None) {
+ p->setfunc = NULL;
+ p->ffi_restype = &ffi_type_void;
+ } else {
+ StgDictObject *dict = PyType_stgdict(restype);
+ if (dict == NULL || dict->setfunc == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "invalid result type for callback function");
+ goto error;
+ }
+ p->setfunc = dict->setfunc;
+ p->ffi_restype = &dict->ffi_type_pointer;
+ }
+
+ cc = FFI_DEFAULT_ABI;
#if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64)
- if ((flags & FUNCFLAG_CDECL) == 0)
- cc = FFI_STDCALL;
+ if ((flags & FUNCFLAG_CDECL) == 0)
+ cc = FFI_STDCALL;
#endif
- result = ffi_prep_cif(&p->cif, cc,
- Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int),
- _ctypes_get_ffi_type(restype),
- &p->atypes[0]);
- if (result != FFI_OK) {
- PyErr_Format(PyExc_RuntimeError,
- "ffi_prep_cif failed with %d", result);
- goto error;
- }
- result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
- if (result != FFI_OK) {
- PyErr_Format(PyExc_RuntimeError,
- "ffi_prep_closure failed with %d", result);
- goto error;
- }
-
- Py_INCREF(converters);
- p->converters = converters;
- Py_INCREF(callable);
- p->callable = callable;
- return p;
+ result = ffi_prep_cif(&p->cif, cc,
+ Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int),
+ _ctypes_get_ffi_type(restype),
+ &p->atypes[0]);
+ if (result != FFI_OK) {
+ PyErr_Format(PyExc_RuntimeError,
+ "ffi_prep_cif failed with %d", result);
+ goto error;
+ }
+ result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
+ if (result != FFI_OK) {
+ PyErr_Format(PyExc_RuntimeError,
+ "ffi_prep_closure failed with %d", result);
+ goto error;
+ }
+
+ Py_INCREF(converters);
+ p->converters = converters;
+ Py_INCREF(callable);
+ p->callable = callable;
+ return p;
error:
- Py_XDECREF(p);
- return NULL;
+ Py_XDECREF(p);
+ return NULL;
}
#ifdef MS_WIN32
static void LoadPython(void)
{
- if (!Py_IsInitialized()) {
+ if (!Py_IsInitialized()) {
#ifdef WITH_THREAD
- PyEval_InitThreads();
+ PyEval_InitThreads();
#endif
- Py_Initialize();
- }
+ Py_Initialize();
+ }
}
/******************************************************************/
long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
- PyObject *mod, *func, *result;
- long retval;
- static PyObject *context;
-
- if (context == NULL)
- context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
-
- mod = PyImport_ImportModuleNoBlock("ctypes");
- if (!mod) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- /* There has been a warning before about this already */
- return E_FAIL;
- }
-
- func = PyObject_GetAttrString(mod, "DllGetClassObject");
- Py_DECREF(mod);
- if (!func) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- return E_FAIL;
- }
-
- {
- PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
- PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
- PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
- if (!py_rclsid || !py_riid || !py_ppv) {
- Py_XDECREF(py_rclsid);
- Py_XDECREF(py_riid);
- Py_XDECREF(py_ppv);
- Py_DECREF(func);
- PyErr_WriteUnraisable(context ? context : Py_None);
- return E_FAIL;
- }
- result = PyObject_CallFunctionObjArgs(func,
- py_rclsid,
- py_riid,
- py_ppv,
- NULL);
- Py_DECREF(py_rclsid);
- Py_DECREF(py_riid);
- Py_DECREF(py_ppv);
- }
- Py_DECREF(func);
- if (!result) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- return E_FAIL;
- }
-
- retval = PyLong_AsLong(result);
- if (PyErr_Occurred()) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- retval = E_FAIL;
- }
- Py_DECREF(result);
- return retval;
+ PyObject *mod, *func, *result;
+ long retval;
+ static PyObject *context;
+
+ if (context == NULL)
+ context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
+
+ mod = PyImport_ImportModuleNoBlock("ctypes");
+ if (!mod) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ /* There has been a warning before about this already */
+ return E_FAIL;
+ }
+
+ func = PyObject_GetAttrString(mod, "DllGetClassObject");
+ Py_DECREF(mod);
+ if (!func) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ return E_FAIL;
+ }
+
+ {
+ PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
+ PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
+ PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
+ if (!py_rclsid || !py_riid || !py_ppv) {
+ Py_XDECREF(py_rclsid);
+ Py_XDECREF(py_riid);
+ Py_XDECREF(py_ppv);
+ Py_DECREF(func);
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ return E_FAIL;
+ }
+ result = PyObject_CallFunctionObjArgs(func,
+ py_rclsid,
+ py_riid,
+ py_ppv,
+ NULL);
+ Py_DECREF(py_rclsid);
+ Py_DECREF(py_riid);
+ Py_DECREF(py_ppv);
+ }
+ Py_DECREF(func);
+ if (!result) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ return E_FAIL;
+ }
+
+ retval = PyLong_AsLong(result);
+ if (PyErr_Occurred()) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ retval = E_FAIL;
+ }
+ Py_DECREF(result);
+ return retval;
}
STDAPI DllGetClassObject(REFCLSID rclsid,
- REFIID riid,
- LPVOID *ppv)
+ REFIID riid,
+ LPVOID *ppv)
{
- long result;
+ long result;
#ifdef WITH_THREAD
- PyGILState_STATE state;
+ PyGILState_STATE state;
#endif
- LoadPython();
+ LoadPython();
#ifdef WITH_THREAD
- state = PyGILState_Ensure();
+ state = PyGILState_Ensure();
#endif
- result = Call_GetClassObject(rclsid, riid, ppv);
+ result = Call_GetClassObject(rclsid, riid, ppv);
#ifdef WITH_THREAD
- PyGILState_Release(state);
+ PyGILState_Release(state);
#endif
- return result;
+ return result;
}
long Call_CanUnloadNow(void)
{
- PyObject *mod, *func, *result;
- long retval;
- static PyObject *context;
-
- if (context == NULL)
- context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
-
- mod = PyImport_ImportModuleNoBlock("ctypes");
- if (!mod) {
-/* OutputDebugString("Could not import ctypes"); */
- /* We assume that this error can only occur when shutting
- down, so we silently ignore it */
- PyErr_Clear();
- return E_FAIL;
- }
- /* Other errors cannot be raised, but are printed to stderr */
- func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
- Py_DECREF(mod);
- if (!func) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- return E_FAIL;
- }
-
- result = PyObject_CallFunction(func, NULL);
- Py_DECREF(func);
- if (!result) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- return E_FAIL;
- }
-
- retval = PyLong_AsLong(result);
- if (PyErr_Occurred()) {
- PyErr_WriteUnraisable(context ? context : Py_None);
- retval = E_FAIL;
- }
- Py_DECREF(result);
- return retval;
+ PyObject *mod, *func, *result;
+ long retval;
+ static PyObject *context;
+
+ if (context == NULL)
+ context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
+
+ mod = PyImport_ImportModuleNoBlock("ctypes");
+ if (!mod) {
+/* OutputDebugString("Could not import ctypes"); */
+ /* We assume that this error can only occur when shutting
+ down, so we silently ignore it */
+ PyErr_Clear();
+ return E_FAIL;
+ }
+ /* Other errors cannot be raised, but are printed to stderr */
+ func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
+ Py_DECREF(mod);
+ if (!func) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ return E_FAIL;
+ }
+
+ result = PyObject_CallFunction(func, NULL);
+ Py_DECREF(func);
+ if (!result) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ return E_FAIL;
+ }
+
+ retval = PyLong_AsLong(result);
+ if (PyErr_Occurred()) {
+ PyErr_WriteUnraisable(context ? context : Py_None);
+ retval = E_FAIL;
+ }
+ Py_DECREF(result);
+ return retval;
}
/*
@@ -604,26 +604,26 @@ long Call_CanUnloadNow(void)
STDAPI DllCanUnloadNow(void)
{
- long result;
+ long result;
#ifdef WITH_THREAD
- PyGILState_STATE state = PyGILState_Ensure();
+ PyGILState_STATE state = PyGILState_Ensure();
#endif
- result = Call_CanUnloadNow();
+ result = Call_CanUnloadNow();
#ifdef WITH_THREAD
- PyGILState_Release(state);
+ PyGILState_Release(state);
#endif
- return result;
+ return result;
}
#ifndef Py_NO_ENABLE_SHARED
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes)
{
- switch(fdwReason) {
- case DLL_PROCESS_ATTACH:
- DisableThreadLibraryCalls(hinstDLL);
- break;
- }
- return TRUE;
+ switch(fdwReason) {
+ case DLL_PROCESS_ATTACH:
+ DisableThreadLibraryCalls(hinstDLL);
+ break;
+ }
+ return TRUE;
}
#endif
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 022cceb..0e4f1b1 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -4,9 +4,9 @@
*/
/*
* Related Work:
- * - calldll http://www.nightmare.com/software.html
- * - libffi http://sourceware.cygnus.com/libffi/
- * - ffcall http://clisp.cons.org/~haible/packages-ffcall.html
+ * - calldll http://www.nightmare.com/software.html
+ * - libffi http://sourceware.cygnus.com/libffi/
+ * - ffcall http://clisp.cons.org/~haible/packages-ffcall.html
* and, of course, Don Beaudry's MESS package, but this is more ctypes
* related.
*/
@@ -82,17 +82,17 @@
static void pymem_destructor(PyObject *ptr)
{
- void *p = PyCapsule_GetPointer(ptr, CTYPES_CAPSULE_NAME_PYMEM);
- if (p) {
- PyMem_Free(p);
- }
+ void *p = PyCapsule_GetPointer(ptr, CTYPES_CAPSULE_NAME_PYMEM);
+ if (p) {
+ PyMem_Free(p);
+ }
}
/*
ctypes maintains thread-local storage that has space for two error numbers:
private copies of the system 'errno' value and, on Windows, the system error code
accessed by the GetLastError() and SetLastError() api functions.
-
+
Foreign functions created with CDLL(..., use_errno=True), when called, swap
the system 'errno' value with the private copy just before the actual
function call, and swapped again immediately afterwards. The 'use_errno'
@@ -125,88 +125,88 @@ static void pymem_destructor(PyObject *ptr)
PyObject *
_ctypes_get_errobj(int **pspace)
{
- PyObject *dict = PyThreadState_GetDict();
- PyObject *errobj;
- static PyObject *error_object_name;
- if (dict == 0) {
- PyErr_SetString(PyExc_RuntimeError,
- "cannot get thread state");
- return NULL;
- }
- if (error_object_name == NULL) {
- error_object_name = PyUnicode_InternFromString("ctypes.error_object");
- if (error_object_name == NULL)
- return NULL;
- }
- errobj = PyDict_GetItem(dict, error_object_name);
- if (errobj) {
- if (!PyCapsule_IsValid(errobj, CTYPES_CAPSULE_NAME_PYMEM)) {
- PyErr_SetString(PyExc_RuntimeError,
- "ctypes.error_object is an invalid capsule");
- return NULL;
- }
- Py_INCREF(errobj);
- }
- else {
- void *space = PyMem_Malloc(sizeof(int) * 2);
- if (space == NULL)
- return NULL;
- memset(space, 0, sizeof(int) * 2);
- errobj = PyCapsule_New(space, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
- if (errobj == NULL)
- return NULL;
- if (-1 == PyDict_SetItem(dict, error_object_name,
- errobj)) {
- Py_DECREF(errobj);
- return NULL;
- }
- }
- *pspace = (int *)PyCapsule_GetPointer(errobj, CTYPES_CAPSULE_NAME_PYMEM);
- return errobj;
+ PyObject *dict = PyThreadState_GetDict();
+ PyObject *errobj;
+ static PyObject *error_object_name;
+ if (dict == 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "cannot get thread state");
+ return NULL;
+ }
+ if (error_object_name == NULL) {
+ error_object_name = PyUnicode_InternFromString("ctypes.error_object");
+ if (error_object_name == NULL)
+ return NULL;
+ }
+ errobj = PyDict_GetItem(dict, error_object_name);
+ if (errobj) {
+ if (!PyCapsule_IsValid(errobj, CTYPES_CAPSULE_NAME_PYMEM)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "ctypes.error_object is an invalid capsule");
+ return NULL;
+ }
+ Py_INCREF(errobj);
+ }
+ else {
+ void *space = PyMem_Malloc(sizeof(int) * 2);
+ if (space == NULL)
+ return NULL;
+ memset(space, 0, sizeof(int) * 2);
+ errobj = PyCapsule_New(space, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
+ if (errobj == NULL)
+ return NULL;
+ if (-1 == PyDict_SetItem(dict, error_object_name,
+ errobj)) {
+ Py_DECREF(errobj);
+ return NULL;
+ }
+ }
+ *pspace = (int *)PyCapsule_GetPointer(errobj, CTYPES_CAPSULE_NAME_PYMEM);
+ return errobj;
}
static PyObject *
get_error_internal(PyObject *self, PyObject *args, int index)
{
- int *space;
- PyObject *errobj = _ctypes_get_errobj(&space);
- PyObject *result;
-
- if (errobj == NULL)
- return NULL;
- result = PyLong_FromLong(space[index]);
- Py_DECREF(errobj);
- return result;
+ int *space;
+ PyObject *errobj = _ctypes_get_errobj(&space);
+ PyObject *result;
+
+ if (errobj == NULL)
+ return NULL;
+ result = PyLong_FromLong(space[index]);
+ Py_DECREF(errobj);
+ return result;
}
static PyObject *
set_error_internal(PyObject *self, PyObject *args, int index)
{
- int new_errno, old_errno;
- PyObject *errobj;
- int *space;
-
- if (!PyArg_ParseTuple(args, "i", &new_errno))
- return NULL;
- errobj = _ctypes_get_errobj(&space);
- if (errobj == NULL)
- return NULL;
- old_errno = space[index];
- space[index] = new_errno;
- Py_DECREF(errobj);
- return PyLong_FromLong(old_errno);
+ int new_errno, old_errno;
+ PyObject *errobj;
+ int *space;
+
+ if (!PyArg_ParseTuple(args, "i", &new_errno))
+ return NULL;
+ errobj = _ctypes_get_errobj(&space);
+ if (errobj == NULL)
+ return NULL;
+ old_errno = space[index];
+ space[index] = new_errno;
+ Py_DECREF(errobj);
+ return PyLong_FromLong(old_errno);
}
static PyObject *
get_errno(PyObject *self, PyObject *args)
{
- return get_error_internal(self, args, 0);
+ return get_error_internal(self, args, 0);
}
static PyObject *
set_errno(PyObject *self, PyObject *args)
{
- return set_error_internal(self, args, 0);
+ return set_error_internal(self, args, 0);
}
#ifdef MS_WIN32
@@ -214,202 +214,202 @@ set_errno(PyObject *self, PyObject *args)
static PyObject *
get_last_error(PyObject *self, PyObject *args)
{
- return get_error_internal(self, args, 1);
+ return get_error_internal(self, args, 1);
}
static PyObject *
set_last_error(PyObject *self, PyObject *args)
{
- return set_error_internal(self, args, 1);
+ return set_error_internal(self, args, 1);
}
PyObject *ComError;
static WCHAR *FormatError(DWORD code)
{
- WCHAR *lpMsgBuf;
- DWORD n;
- n = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
- (LPWSTR) &lpMsgBuf,
- 0,
- NULL);
- if (n) {
- while (iswspace(lpMsgBuf[n-1]))
- --n;
- lpMsgBuf[n] = L'\0'; /* rstrip() */
- }
- return lpMsgBuf;
+ WCHAR *lpMsgBuf;
+ DWORD n;
+ n = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL,
+ code,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
+ (LPWSTR) &lpMsgBuf,
+ 0,
+ NULL);
+ if (n) {
+ while (iswspace(lpMsgBuf[n-1]))
+ --n;
+ lpMsgBuf[n] = L'\0'; /* rstrip() */
+ }
+ return lpMsgBuf;
}
#ifndef DONT_USE_SEH
static void SetException(DWORD code, EXCEPTION_RECORD *pr)
{
- /* The 'code' is a normal win32 error code so it could be handled by
- PyErr_SetFromWindowsErr(). However, for some errors, we have additional
- information not included in the error code. We handle those here and
- delegate all others to the generic function. */
- switch (code) {
- case EXCEPTION_ACCESS_VIOLATION:
- /* The thread attempted to read from or write
- to a virtual address for which it does not
- have the appropriate access. */
- if (pr->ExceptionInformation[0] == 0)
- PyErr_Format(PyExc_WindowsError,
- "exception: access violation reading %p",
- pr->ExceptionInformation[1]);
- else
- PyErr_Format(PyExc_WindowsError,
- "exception: access violation writing %p",
- pr->ExceptionInformation[1]);
- break;
-
- case EXCEPTION_BREAKPOINT:
- /* A breakpoint was encountered. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: breakpoint encountered");
- break;
-
- case EXCEPTION_DATATYPE_MISALIGNMENT:
- /* The thread attempted to read or write data that is
- misaligned on hardware that does not provide
- alignment. For example, 16-bit values must be
- aligned on 2-byte boundaries, 32-bit values on
- 4-byte boundaries, and so on. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: datatype misalignment");
- break;
-
- case EXCEPTION_SINGLE_STEP:
- /* A trace trap or other single-instruction mechanism
- signaled that one instruction has been executed. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: single step");
- break;
-
- case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
- /* The thread attempted to access an array element
- that is out of bounds, and the underlying hardware
- supports bounds checking. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: array bounds exceeded");
- break;
-
- case EXCEPTION_FLT_DENORMAL_OPERAND:
- /* One of the operands in a floating-point operation
- is denormal. A denormal value is one that is too
- small to represent as a standard floating-point
- value. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: floating-point operand denormal");
- break;
-
- case EXCEPTION_FLT_DIVIDE_BY_ZERO:
- /* The thread attempted to divide a floating-point
- value by a floating-point divisor of zero. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: float divide by zero");
- break;
-
- case EXCEPTION_FLT_INEXACT_RESULT:
- /* The result of a floating-point operation cannot be
- represented exactly as a decimal fraction. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: float inexact");
- break;
-
- case EXCEPTION_FLT_INVALID_OPERATION:
- /* This exception represents any floating-point
- exception not included in this list. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: float invalid operation");
- break;
-
- case EXCEPTION_FLT_OVERFLOW:
- /* The exponent of a floating-point operation is
- greater than the magnitude allowed by the
- corresponding type. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: float overflow");
- break;
-
- case EXCEPTION_FLT_STACK_CHECK:
- /* The stack overflowed or underflowed as the result
- of a floating-point operation. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: stack over/underflow");
- break;
-
- case EXCEPTION_STACK_OVERFLOW:
- /* The stack overflowed or underflowed as the result
- of a floating-point operation. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: stack overflow");
- break;
-
- case EXCEPTION_FLT_UNDERFLOW:
- /* The exponent of a floating-point operation is less
- than the magnitude allowed by the corresponding
- type. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: float underflow");
- break;
-
- case EXCEPTION_INT_DIVIDE_BY_ZERO:
- /* The thread attempted to divide an integer value by
- an integer divisor of zero. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: integer divide by zero");
- break;
-
- case EXCEPTION_INT_OVERFLOW:
- /* The result of an integer operation caused a carry
- out of the most significant bit of the result. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: integer overflow");
- break;
-
- case EXCEPTION_PRIV_INSTRUCTION:
- /* The thread attempted to execute an instruction
- whose operation is not allowed in the current
- machine mode. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: priviledged instruction");
- break;
-
- case EXCEPTION_NONCONTINUABLE_EXCEPTION:
- /* The thread attempted to continue execution after a
- noncontinuable exception occurred. */
- PyErr_SetString(PyExc_WindowsError,
- "exception: nocontinuable");
- break;
-
- default:
- PyErr_SetFromWindowsErr(code);
- break;
- }
+ /* The 'code' is a normal win32 error code so it could be handled by
+ PyErr_SetFromWindowsErr(). However, for some errors, we have additional
+ information not included in the error code. We handle those here and
+ delegate all others to the generic function. */
+ switch (code) {
+ case EXCEPTION_ACCESS_VIOLATION:
+ /* The thread attempted to read from or write
+ to a virtual address for which it does not
+ have the appropriate access. */
+ if (pr->ExceptionInformation[0] == 0)
+ PyErr_Format(PyExc_WindowsError,
+ "exception: access violation reading %p",
+ pr->ExceptionInformation[1]);
+ else
+ PyErr_Format(PyExc_WindowsError,
+ "exception: access violation writing %p",
+ pr->ExceptionInformation[1]);
+ break;
+
+ case EXCEPTION_BREAKPOINT:
+ /* A breakpoint was encountered. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: breakpoint encountered");
+ break;
+
+ case EXCEPTION_DATATYPE_MISALIGNMENT:
+ /* The thread attempted to read or write data that is
+ misaligned on hardware that does not provide
+ alignment. For example, 16-bit values must be
+ aligned on 2-byte boundaries, 32-bit values on
+ 4-byte boundaries, and so on. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: datatype misalignment");
+ break;
+
+ case EXCEPTION_SINGLE_STEP:
+ /* A trace trap or other single-instruction mechanism
+ signaled that one instruction has been executed. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: single step");
+ break;
+
+ case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
+ /* The thread attempted to access an array element
+ that is out of bounds, and the underlying hardware
+ supports bounds checking. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: array bounds exceeded");
+ break;
+
+ case EXCEPTION_FLT_DENORMAL_OPERAND:
+ /* One of the operands in a floating-point operation
+ is denormal. A denormal value is one that is too
+ small to represent as a standard floating-point
+ value. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: floating-point operand denormal");
+ break;
+
+ case EXCEPTION_FLT_DIVIDE_BY_ZERO:
+ /* The thread attempted to divide a floating-point
+ value by a floating-point divisor of zero. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: float divide by zero");
+ break;
+
+ case EXCEPTION_FLT_INEXACT_RESULT:
+ /* The result of a floating-point operation cannot be
+ represented exactly as a decimal fraction. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: float inexact");
+ break;
+
+ case EXCEPTION_FLT_INVALID_OPERATION:
+ /* This exception represents any floating-point
+ exception not included in this list. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: float invalid operation");
+ break;
+
+ case EXCEPTION_FLT_OVERFLOW:
+ /* The exponent of a floating-point operation is
+ greater than the magnitude allowed by the
+ corresponding type. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: float overflow");
+ break;
+
+ case EXCEPTION_FLT_STACK_CHECK:
+ /* The stack overflowed or underflowed as the result
+ of a floating-point operation. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: stack over/underflow");
+ break;
+
+ case EXCEPTION_STACK_OVERFLOW:
+ /* The stack overflowed or underflowed as the result
+ of a floating-point operation. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: stack overflow");
+ break;
+
+ case EXCEPTION_FLT_UNDERFLOW:
+ /* The exponent of a floating-point operation is less
+ than the magnitude allowed by the corresponding
+ type. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: float underflow");
+ break;
+
+ case EXCEPTION_INT_DIVIDE_BY_ZERO:
+ /* The thread attempted to divide an integer value by
+ an integer divisor of zero. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: integer divide by zero");
+ break;
+
+ case EXCEPTION_INT_OVERFLOW:
+ /* The result of an integer operation caused a carry
+ out of the most significant bit of the result. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: integer overflow");
+ break;
+
+ case EXCEPTION_PRIV_INSTRUCTION:
+ /* The thread attempted to execute an instruction
+ whose operation is not allowed in the current
+ machine mode. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: priviledged instruction");
+ break;
+
+ case EXCEPTION_NONCONTINUABLE_EXCEPTION:
+ /* The thread attempted to continue execution after a
+ noncontinuable exception occurred. */
+ PyErr_SetString(PyExc_WindowsError,
+ "exception: nocontinuable");
+ break;
+
+ default:
+ PyErr_SetFromWindowsErr(code);
+ break;
+ }
}
static DWORD HandleException(EXCEPTION_POINTERS *ptrs,
- DWORD *pdw, EXCEPTION_RECORD *record)
+ DWORD *pdw, EXCEPTION_RECORD *record)
{
- *pdw = ptrs->ExceptionRecord->ExceptionCode;
- *record = *ptrs->ExceptionRecord;
- return EXCEPTION_EXECUTE_HANDLER;
+ *pdw = ptrs->ExceptionRecord->ExceptionCode;
+ *record = *ptrs->ExceptionRecord;
+ return EXCEPTION_EXECUTE_HANDLER;
}
#endif
static PyObject *
check_hresult(PyObject *self, PyObject *args)
{
- HRESULT hr;
- if (!PyArg_ParseTuple(args, "i", &hr))
- return NULL;
- if (FAILED(hr))
- return PyErr_SetFromWindowsErr(hr);
- return PyLong_FromLong(hr);
+ HRESULT hr;
+ if (!PyArg_ParseTuple(args, "i", &hr))
+ return NULL;
+ if (FAILED(hr))
+ return PyErr_SetFromWindowsErr(hr);
+ return PyLong_FromLong(hr);
}
#endif
@@ -419,132 +419,132 @@ check_hresult(PyObject *self, PyObject *args)
PyCArgObject *
PyCArgObject_new(void)
{
- PyCArgObject *p;
- p = PyObject_New(PyCArgObject, &PyCArg_Type);
- if (p == NULL)
- return NULL;
- p->pffi_type = NULL;
- p->tag = '\0';
- p->obj = NULL;
- memset(&p->value, 0, sizeof(p->value));
- return p;
+ PyCArgObject *p;
+ p = PyObject_New(PyCArgObject, &PyCArg_Type);
+ if (p == NULL)
+ return NULL;
+ p->pffi_type = NULL;
+ p->tag = '\0';
+ p->obj = NULL;
+ memset(&p->value, 0, sizeof(p->value));
+ return p;
}
static void
PyCArg_dealloc(PyCArgObject *self)
{
- Py_XDECREF(self->obj);
- PyObject_Del(self);
+ Py_XDECREF(self->obj);
+ PyObject_Del(self);
}
static PyObject *
PyCArg_repr(PyCArgObject *self)
{
- char buffer[256];
- switch(self->tag) {
- case 'b':
- case 'B':
- sprintf(buffer, "<cparam '%c' (%d)>",
- self->tag, self->value.b);
- break;
- case 'h':
- case 'H':
- sprintf(buffer, "<cparam '%c' (%d)>",
- self->tag, self->value.h);
- break;
- case 'i':
- case 'I':
- sprintf(buffer, "<cparam '%c' (%d)>",
- self->tag, self->value.i);
- break;
- case 'l':
- case 'L':
- sprintf(buffer, "<cparam '%c' (%ld)>",
- self->tag, self->value.l);
- break;
-
+ char buffer[256];
+ switch(self->tag) {
+ case 'b':
+ case 'B':
+ sprintf(buffer, "<cparam '%c' (%d)>",
+ self->tag, self->value.b);
+ break;
+ case 'h':
+ case 'H':
+ sprintf(buffer, "<cparam '%c' (%d)>",
+ self->tag, self->value.h);
+ break;
+ case 'i':
+ case 'I':
+ sprintf(buffer, "<cparam '%c' (%d)>",
+ self->tag, self->value.i);
+ break;
+ case 'l':
+ case 'L':
+ sprintf(buffer, "<cparam '%c' (%ld)>",
+ self->tag, self->value.l);
+ break;
+
#ifdef HAVE_LONG_LONG
- case 'q':
- case 'Q':
- sprintf(buffer,
+ case 'q':
+ case 'Q':
+ sprintf(buffer,
#ifdef MS_WIN32
- "<cparam '%c' (%I64d)>",
+ "<cparam '%c' (%I64d)>",
#else
- "<cparam '%c' (%qd)>",
+ "<cparam '%c' (%qd)>",
#endif
- self->tag, self->value.q);
- break;
+ self->tag, self->value.q);
+ break;
#endif
- case 'd':
- sprintf(buffer, "<cparam '%c' (%f)>",
- self->tag, self->value.d);
- break;
- case 'f':
- sprintf(buffer, "<cparam '%c' (%f)>",
- self->tag, self->value.f);
- break;
-
- case 'c':
- sprintf(buffer, "<cparam '%c' (%c)>",
- self->tag, self->value.c);
- break;
+ case 'd':
+ sprintf(buffer, "<cparam '%c' (%f)>",
+ self->tag, self->value.d);
+ break;
+ case 'f':
+ sprintf(buffer, "<cparam '%c' (%f)>",
+ self->tag, self->value.f);
+ break;
+
+ case 'c':
+ sprintf(buffer, "<cparam '%c' (%c)>",
+ self->tag, self->value.c);
+ break;
/* Hm, are these 'z' and 'Z' codes useful at all?
Shouldn't they be replaced by the functionality of c_string
and c_wstring ?
*/
- case 'z':
- case 'Z':
- case 'P':
- sprintf(buffer, "<cparam '%c' (%p)>",
- self->tag, self->value.p);
- break;
-
- default:
- sprintf(buffer, "<cparam '%c' at %p>",
- self->tag, self);
- break;
- }
- return PyUnicode_FromString(buffer);
+ case 'z':
+ case 'Z':
+ case 'P':
+ sprintf(buffer, "<cparam '%c' (%p)>",
+ self->tag, self->value.p);
+ break;
+
+ default:
+ sprintf(buffer, "<cparam '%c' at %p>",
+ self->tag, self);
+ break;
+ }
+ return PyUnicode_FromString(buffer);
}
static PyMemberDef PyCArgType_members[] = {
- { "_obj", T_OBJECT,
- offsetof(PyCArgObject, obj), READONLY,
- "the wrapped object" },
- { NULL },
+ { "_obj", T_OBJECT,
+ offsetof(PyCArgObject, obj), READONLY,
+ "the wrapped object" },
+ { NULL },
};
PyTypeObject PyCArg_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "CArgObject",
- sizeof(PyCArgObject),
- 0,
- (destructor)PyCArg_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)PyCArg_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- PyCArgType_members, /* tp_members */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "CArgObject",
+ sizeof(PyCArgObject),
+ 0,
+ (destructor)PyCArg_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)PyCArg_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ PyCArgType_members, /* tp_members */
};
/****************************************************************/
@@ -577,24 +577,24 @@ PyTypeObject PyCArg_Type = {
*/
union result {
- char c;
- char b;
- short h;
- int i;
- long l;
+ char c;
+ char b;
+ short h;
+ int i;
+ long l;
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG q;
+ PY_LONG_LONG q;
#endif
- long double D;
- double d;
- float f;
- void *p;
+ long double D;
+ double d;
+ float f;
+ void *p;
};
struct argument {
- ffi_type *ffi_type;
- PyObject *keep;
- union result value;
+ ffi_type *ffi_type;
+ PyObject *keep;
+ union result value;
};
/*
@@ -602,134 +602,134 @@ struct argument {
*/
static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
{
- StgDictObject *dict;
- pa->keep = NULL; /* so we cannot forget it later */
-
- dict = PyObject_stgdict(obj);
- if (dict) {
- PyCArgObject *carg;
- assert(dict->paramfunc);
- /* If it has an stgdict, it is a CDataObject */
- carg = dict->paramfunc((CDataObject *)obj);
- pa->ffi_type = carg->pffi_type;
- memcpy(&pa->value, &carg->value, sizeof(pa->value));
- pa->keep = (PyObject *)carg;
- return 0;
- }
-
- if (PyCArg_CheckExact(obj)) {
- PyCArgObject *carg = (PyCArgObject *)obj;
- pa->ffi_type = carg->pffi_type;
- Py_INCREF(obj);
- pa->keep = obj;
- memcpy(&pa->value, &carg->value, sizeof(pa->value));
- return 0;
- }
-
- /* check for None, integer, string or unicode and use directly if successful */
- if (obj == Py_None) {
- pa->ffi_type = &ffi_type_pointer;
- pa->value.p = NULL;
- return 0;
- }
-
- if (PyLong_Check(obj)) {
- pa->ffi_type = &ffi_type_sint;
- pa->value.i = (long)PyLong_AsUnsignedLong(obj);
- if (pa->value.i == -1 && PyErr_Occurred()) {
- PyErr_Clear();
- pa->value.i = PyLong_AsLong(obj);
- if (pa->value.i == -1 && PyErr_Occurred()) {
- PyErr_SetString(PyExc_OverflowError,
- "long int too long to convert");
- return -1;
- }
- }
- return 0;
- }
-
- if (PyBytes_Check(obj)) {
- pa->ffi_type = &ffi_type_pointer;
- pa->value.p = PyBytes_AsString(obj);
- Py_INCREF(obj);
- pa->keep = obj;
- return 0;
- }
+ StgDictObject *dict;
+ pa->keep = NULL; /* so we cannot forget it later */
+
+ dict = PyObject_stgdict(obj);
+ if (dict) {
+ PyCArgObject *carg;
+ assert(dict->paramfunc);
+ /* If it has an stgdict, it is a CDataObject */
+ carg = dict->paramfunc((CDataObject *)obj);
+ pa->ffi_type = carg->pffi_type;
+ memcpy(&pa->value, &carg->value, sizeof(pa->value));
+ pa->keep = (PyObject *)carg;
+ return 0;
+ }
+
+ if (PyCArg_CheckExact(obj)) {
+ PyCArgObject *carg = (PyCArgObject *)obj;
+ pa->ffi_type = carg->pffi_type;
+ Py_INCREF(obj);
+ pa->keep = obj;
+ memcpy(&pa->value, &carg->value, sizeof(pa->value));
+ return 0;
+ }
+
+ /* check for None, integer, string or unicode and use directly if successful */
+ if (obj == Py_None) {
+ pa->ffi_type = &ffi_type_pointer;
+ pa->value.p = NULL;
+ return 0;
+ }
+
+ if (PyLong_Check(obj)) {
+ pa->ffi_type = &ffi_type_sint;
+ pa->value.i = (long)PyLong_AsUnsignedLong(obj);
+ if (pa->value.i == -1 && PyErr_Occurred()) {
+ PyErr_Clear();
+ pa->value.i = PyLong_AsLong(obj);
+ if (pa->value.i == -1 && PyErr_Occurred()) {
+ PyErr_SetString(PyExc_OverflowError,
+ "long int too long to convert");
+ return -1;
+ }
+ }
+ return 0;
+ }
+
+ if (PyBytes_Check(obj)) {
+ pa->ffi_type = &ffi_type_pointer;
+ pa->value.p = PyBytes_AsString(obj);
+ Py_INCREF(obj);
+ pa->keep = obj;
+ return 0;
+ }
#ifdef CTYPES_UNICODE
- if (PyUnicode_Check(obj)) {
+ if (PyUnicode_Check(obj)) {
#ifdef HAVE_USABLE_WCHAR_T
- pa->ffi_type = &ffi_type_pointer;
- pa->value.p = PyUnicode_AS_UNICODE(obj);
- Py_INCREF(obj);
- pa->keep = obj;
- return 0;
+ pa->ffi_type = &ffi_type_pointer;
+ pa->value.p = PyUnicode_AS_UNICODE(obj);
+ Py_INCREF(obj);
+ pa->keep = obj;
+ return 0;
#else
- int size = PyUnicode_GET_SIZE(obj);
- pa->ffi_type = &ffi_type_pointer;
- size += 1; /* terminating NUL */
- size *= sizeof(wchar_t);
- pa->value.p = PyMem_Malloc(size);
- if (!pa->value.p) {
- PyErr_NoMemory();
- return -1;
- }
- memset(pa->value.p, 0, size);
- pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
- if (!pa->keep) {
- PyMem_Free(pa->value.p);
- return -1;
- }
- if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)obj,
- pa->value.p, PyUnicode_GET_SIZE(obj)))
- return -1;
- return 0;
+ int size = PyUnicode_GET_SIZE(obj);
+ pa->ffi_type = &ffi_type_pointer;
+ size += 1; /* terminating NUL */
+ size *= sizeof(wchar_t);
+ pa->value.p = PyMem_Malloc(size);
+ if (!pa->value.p) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ memset(pa->value.p, 0, size);
+ pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
+ if (!pa->keep) {
+ PyMem_Free(pa->value.p);
+ return -1;
+ }
+ if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)obj,
+ pa->value.p, PyUnicode_GET_SIZE(obj)))
+ return -1;
+ return 0;
#endif
- }
+ }
#endif
- {
- PyObject *arg;
- arg = PyObject_GetAttrString(obj, "_as_parameter_");
- /* Which types should we exactly allow here?
- integers are required for using Python classes
- as parameters (they have to expose the '_as_parameter_'
- attribute)
- */
- if (arg) {
- int result;
- result = ConvParam(arg, index, pa);
- Py_DECREF(arg);
- return result;
- }
- PyErr_Format(PyExc_TypeError,
- "Don't know how to convert parameter %d",
- Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
- return -1;
- }
+ {
+ PyObject *arg;
+ arg = PyObject_GetAttrString(obj, "_as_parameter_");
+ /* Which types should we exactly allow here?
+ integers are required for using Python classes
+ as parameters (they have to expose the '_as_parameter_'
+ attribute)
+ */
+ if (arg) {
+ int result;
+ result = ConvParam(arg, index, pa);
+ Py_DECREF(arg);
+ return result;
+ }
+ PyErr_Format(PyExc_TypeError,
+ "Don't know how to convert parameter %d",
+ Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
+ return -1;
+ }
}
ffi_type *_ctypes_get_ffi_type(PyObject *obj)
{
- StgDictObject *dict;
- if (obj == NULL)
- return &ffi_type_sint;
- dict = PyType_stgdict(obj);
- if (dict == NULL)
- return &ffi_type_sint;
+ StgDictObject *dict;
+ if (obj == NULL)
+ return &ffi_type_sint;
+ dict = PyType_stgdict(obj);
+ if (dict == NULL)
+ return &ffi_type_sint;
#if defined(MS_WIN32) && !defined(_WIN32_WCE)
- /* This little trick works correctly with MSVC.
- It returns small structures in registers
- */
- if (dict->ffi_type_pointer.type == FFI_TYPE_STRUCT) {
- if (dict->ffi_type_pointer.size <= 4)
- return &ffi_type_sint32;
- else if (dict->ffi_type_pointer.size <= 8)
- return &ffi_type_sint64;
- }
+ /* This little trick works correctly with MSVC.
+ It returns small structures in registers
+ */
+ if (dict->ffi_type_pointer.type == FFI_TYPE_STRUCT) {
+ if (dict->ffi_type_pointer.size <= 4)
+ return &ffi_type_sint32;
+ else if (dict->ffi_type_pointer.size <= 8)
+ return &ffi_type_sint64;
+ }
#endif
- return &dict->ffi_type_pointer;
+ return &dict->ffi_type_pointer;
}
@@ -737,7 +737,7 @@ ffi_type *_ctypes_get_ffi_type(PyObject *obj)
* libffi uses:
*
* ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi,
- * unsigned int nargs,
+ * unsigned int nargs,
* ffi_type *rtype,
* ffi_type **atypes);
*
@@ -746,139 +746,139 @@ ffi_type *_ctypes_get_ffi_type(PyObject *obj)
* void ffi_call(ffi_cif *cif, void *fn, void *rvalue, void **avalues);
*/
static int _call_function_pointer(int flags,
- PPROC pProc,
- void **avalues,
- ffi_type **atypes,
- ffi_type *restype,
- void *resmem,
- int argcount)
+ PPROC pProc,
+ void **avalues,
+ ffi_type **atypes,
+ ffi_type *restype,
+ void *resmem,
+ int argcount)
{
#ifdef WITH_THREAD
- PyThreadState *_save = NULL; /* For Py_BLOCK_THREADS and Py_UNBLOCK_THREADS */
+ PyThreadState *_save = NULL; /* For Py_BLOCK_THREADS and Py_UNBLOCK_THREADS */
#endif
- PyObject *error_object = NULL;
- int *space;
- ffi_cif cif;
- int cc;
+ PyObject *error_object = NULL;
+ int *space;
+ ffi_cif cif;
+ int cc;
#ifdef MS_WIN32
- int delta;
+ int delta;
#ifndef DONT_USE_SEH
- DWORD dwExceptionCode = 0;
- EXCEPTION_RECORD record;
+ DWORD dwExceptionCode = 0;
+ EXCEPTION_RECORD record;
#endif
#endif
- /* XXX check before here */
- if (restype == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "No ffi_type for result");
- return -1;
- }
-
- cc = FFI_DEFAULT_ABI;
+ /* XXX check before here */
+ if (restype == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "No ffi_type for result");
+ return -1;
+ }
+
+ cc = FFI_DEFAULT_ABI;
#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(_WIN32_WCE)
- if ((flags & FUNCFLAG_CDECL) == 0)
- cc = FFI_STDCALL;
+ if ((flags & FUNCFLAG_CDECL) == 0)
+ cc = FFI_STDCALL;
#endif
- if (FFI_OK != ffi_prep_cif(&cif,
- cc,
- argcount,
- restype,
- atypes)) {
- PyErr_SetString(PyExc_RuntimeError,
- "ffi_prep_cif failed");
- return -1;
- }
-
- if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
- error_object = _ctypes_get_errobj(&space);
- if (error_object == NULL)
- return -1;
- }
+ if (FFI_OK != ffi_prep_cif(&cif,
+ cc,
+ argcount,
+ restype,
+ atypes)) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "ffi_prep_cif failed");
+ return -1;
+ }
+
+ if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
+ error_object = _ctypes_get_errobj(&space);
+ if (error_object == NULL)
+ return -1;
+ }
#ifdef WITH_THREAD
- if ((flags & FUNCFLAG_PYTHONAPI) == 0)
- Py_UNBLOCK_THREADS
+ if ((flags & FUNCFLAG_PYTHONAPI) == 0)
+ Py_UNBLOCK_THREADS
#endif
- if (flags & FUNCFLAG_USE_ERRNO) {
- int temp = space[0];
- space[0] = errno;
- errno = temp;
- }
+ if (flags & FUNCFLAG_USE_ERRNO) {
+ int temp = space[0];
+ space[0] = errno;
+ errno = temp;
+ }
#ifdef MS_WIN32
- if (flags & FUNCFLAG_USE_LASTERROR) {
- int temp = space[1];
- space[1] = GetLastError();
- SetLastError(temp);
- }
+ if (flags & FUNCFLAG_USE_LASTERROR) {
+ int temp = space[1];
+ space[1] = GetLastError();
+ SetLastError(temp);
+ }
#ifndef DONT_USE_SEH
- __try {
+ __try {
#endif
- delta =
+ delta =
#endif
- ffi_call(&cif, (void *)pProc, resmem, avalues);
+ ffi_call(&cif, (void *)pProc, resmem, avalues);
#ifdef MS_WIN32
#ifndef DONT_USE_SEH
- }
- __except (HandleException(GetExceptionInformation(),
- &dwExceptionCode, &record)) {
- ;
- }
+ }
+ __except (HandleException(GetExceptionInformation(),
+ &dwExceptionCode, &record)) {
+ ;
+ }
#endif
- if (flags & FUNCFLAG_USE_LASTERROR) {
- int temp = space[1];
- space[1] = GetLastError();
- SetLastError(temp);
- }
+ if (flags & FUNCFLAG_USE_LASTERROR) {
+ int temp = space[1];
+ space[1] = GetLastError();
+ SetLastError(temp);
+ }
#endif
- if (flags & FUNCFLAG_USE_ERRNO) {
- int temp = space[0];
- space[0] = errno;
- errno = temp;
- }
- Py_XDECREF(error_object);
+ if (flags & FUNCFLAG_USE_ERRNO) {
+ int temp = space[0];
+ space[0] = errno;
+ errno = temp;
+ }
+ Py_XDECREF(error_object);
#ifdef WITH_THREAD
- if ((flags & FUNCFLAG_PYTHONAPI) == 0)
- Py_BLOCK_THREADS
+ if ((flags & FUNCFLAG_PYTHONAPI) == 0)
+ Py_BLOCK_THREADS
#endif
#ifdef MS_WIN32
#ifndef DONT_USE_SEH
- if (dwExceptionCode) {
- SetException(dwExceptionCode, &record);
- return -1;
- }
+ if (dwExceptionCode) {
+ SetException(dwExceptionCode, &record);
+ return -1;
+ }
#endif
#ifdef MS_WIN64
- if (delta != 0) {
- PyErr_Format(PyExc_RuntimeError,
- "ffi_call failed with code %d",
- delta);
- return -1;
- }
+ if (delta != 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "ffi_call failed with code %d",
+ delta);
+ return -1;
+ }
#else
- if (delta < 0) {
- if (flags & FUNCFLAG_CDECL)
- PyErr_Format(PyExc_ValueError,
- "Procedure called with not enough "
- "arguments (%d bytes missing) "
- "or wrong calling convention",
- -delta);
- else
- PyErr_Format(PyExc_ValueError,
- "Procedure probably called with not enough "
- "arguments (%d bytes missing)",
- -delta);
- return -1;
- } else if (delta > 0) {
- PyErr_Format(PyExc_ValueError,
- "Procedure probably called with too many "
- "arguments (%d bytes in excess)",
- delta);
- return -1;
- }
+ if (delta < 0) {
+ if (flags & FUNCFLAG_CDECL)
+ PyErr_Format(PyExc_ValueError,
+ "Procedure called with not enough "
+ "arguments (%d bytes missing) "
+ "or wrong calling convention",
+ -delta);
+ else
+ PyErr_Format(PyExc_ValueError,
+ "Procedure probably called with not enough "
+ "arguments (%d bytes missing)",
+ -delta);
+ return -1;
+ } else if (delta > 0) {
+ PyErr_Format(PyExc_ValueError,
+ "Procedure probably called with too many "
+ "arguments (%d bytes in excess)",
+ delta);
+ return -1;
+ }
#endif
#endif
- if ((flags & FUNCFLAG_PYTHONAPI) && PyErr_Occurred())
- return -1;
- return 0;
+ if ((flags & FUNCFLAG_PYTHONAPI) && PyErr_Occurred())
+ return -1;
+ return 0;
}
/*
@@ -893,41 +893,41 @@ static int _call_function_pointer(int flags,
*/
static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
{
- StgDictObject *dict;
- PyObject *retval, *v;
-
- if (restype == NULL)
- return PyLong_FromLong(*(int *)result);
-
- if (restype == Py_None) {
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- dict = PyType_stgdict(restype);
- if (dict == NULL)
- return PyObject_CallFunction(restype, "i", *(int *)result);
-
- if (dict->getfunc && !_ctypes_simple_instance(restype)) {
- retval = dict->getfunc(result, dict->size);
- /* If restype is py_object (detected by comparing getfunc with
- O_get), we have to call Py_DECREF because O_get has already
- called Py_INCREF.
- */
- if (dict->getfunc == _ctypes_get_fielddesc("O")->getfunc) {
- Py_DECREF(retval);
- }
- } else
- retval = PyCData_FromBaseObj(restype, NULL, 0, result);
-
- if (!checker || !retval)
- return retval;
-
- v = PyObject_CallFunctionObjArgs(checker, retval, NULL);
- if (v == NULL)
- _ctypes_add_traceback("GetResult", "_ctypes/callproc.c", __LINE__-2);
- Py_DECREF(retval);
- return v;
+ StgDictObject *dict;
+ PyObject *retval, *v;
+
+ if (restype == NULL)
+ return PyLong_FromLong(*(int *)result);
+
+ if (restype == Py_None) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ dict = PyType_stgdict(restype);
+ if (dict == NULL)
+ return PyObject_CallFunction(restype, "i", *(int *)result);
+
+ if (dict->getfunc && !_ctypes_simple_instance(restype)) {
+ retval = dict->getfunc(result, dict->size);
+ /* If restype is py_object (detected by comparing getfunc with
+ O_get), we have to call Py_DECREF because O_get has already
+ called Py_INCREF.
+ */
+ if (dict->getfunc == _ctypes_get_fielddesc("O")->getfunc) {
+ Py_DECREF(retval);
+ }
+ } else
+ retval = PyCData_FromBaseObj(restype, NULL, 0, result);
+
+ if (!checker || !retval)
+ return retval;
+
+ v = PyObject_CallFunctionObjArgs(checker, retval, NULL);
+ if (v == NULL)
+ _ctypes_add_traceback("GetResult", "_ctypes/callproc.c", __LINE__-2);
+ Py_DECREF(retval);
+ return v;
}
/*
@@ -936,40 +936,40 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
*/
void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...)
{
- va_list vargs;
- PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
-
- va_start(vargs, fmt);
- s = PyUnicode_FromFormatV(fmt, vargs);
- va_end(vargs);
- if (!s)
- return;
-
- PyErr_Fetch(&tp, &v, &tb);
- PyErr_NormalizeException(&tp, &v, &tb);
- cls_str = PyObject_Str(tp);
- if (cls_str) {
- PyUnicode_AppendAndDel(&s, cls_str);
- PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
- if (s == NULL)
- goto error;
- } else
- PyErr_Clear();
- msg_str = PyObject_Str(v);
- if (msg_str)
- PyUnicode_AppendAndDel(&s, msg_str);
- else {
- PyErr_Clear();
- PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
- if (s == NULL)
- goto error;
- }
- PyErr_SetObject(exc_class, s);
+ va_list vargs;
+ PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
+
+ va_start(vargs, fmt);
+ s = PyUnicode_FromFormatV(fmt, vargs);
+ va_end(vargs);
+ if (!s)
+ return;
+
+ PyErr_Fetch(&tp, &v, &tb);
+ PyErr_NormalizeException(&tp, &v, &tb);
+ cls_str = PyObject_Str(tp);
+ if (cls_str) {
+ PyUnicode_AppendAndDel(&s, cls_str);
+ PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
+ if (s == NULL)
+ goto error;
+ } else
+ PyErr_Clear();
+ msg_str = PyObject_Str(v);
+ if (msg_str)
+ PyUnicode_AppendAndDel(&s, msg_str);
+ else {
+ PyErr_Clear();
+ PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
+ if (s == NULL)
+ goto error;
+ }
+ PyErr_SetObject(exc_class, s);
error:
- Py_XDECREF(tp);
- Py_XDECREF(v);
- Py_XDECREF(tb);
- Py_XDECREF(s);
+ Py_XDECREF(tp);
+ Py_XDECREF(v);
+ Py_XDECREF(tb);
+ Py_XDECREF(s);
}
@@ -978,73 +978,73 @@ error:
static PyObject *
GetComError(HRESULT errcode, GUID *riid, IUnknown *pIunk)
{
- HRESULT hr;
- ISupportErrorInfo *psei = NULL;
- IErrorInfo *pei = NULL;
- BSTR descr=NULL, helpfile=NULL, source=NULL;
- GUID guid;
- DWORD helpcontext=0;
- LPOLESTR progid;
- PyObject *obj;
- LPOLESTR text;
-
- /* We absolutely have to release the GIL during COM method calls,
- otherwise we may get a deadlock!
- */
+ HRESULT hr;
+ ISupportErrorInfo *psei = NULL;
+ IErrorInfo *pei = NULL;
+ BSTR descr=NULL, helpfile=NULL, source=NULL;
+ GUID guid;
+ DWORD helpcontext=0;
+ LPOLESTR progid;
+ PyObject *obj;
+ LPOLESTR text;
+
+ /* We absolutely have to release the GIL during COM method calls,
+ otherwise we may get a deadlock!
+ */
#ifdef WITH_THREAD
- Py_BEGIN_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
#endif
- hr = pIunk->lpVtbl->QueryInterface(pIunk, &IID_ISupportErrorInfo, (void **)&psei);
- if (FAILED(hr))
- goto failed;
+ hr = pIunk->lpVtbl->QueryInterface(pIunk, &IID_ISupportErrorInfo, (void **)&psei);
+ if (FAILED(hr))
+ goto failed;
- hr = psei->lpVtbl->InterfaceSupportsErrorInfo(psei, riid);
- psei->lpVtbl->Release(psei);
- if (FAILED(hr))
- goto failed;
+ hr = psei->lpVtbl->InterfaceSupportsErrorInfo(psei, riid);
+ psei->lpVtbl->Release(psei);
+ if (FAILED(hr))
+ goto failed;
- hr = GetErrorInfo(0, &pei);
- if (hr != S_OK)
- goto failed;
+ hr = GetErrorInfo(0, &pei);
+ if (hr != S_OK)
+ goto failed;
- pei->lpVtbl->GetDescription(pei, &descr);
- pei->lpVtbl->GetGUID(pei, &guid);
- pei->lpVtbl->GetHelpContext(pei, &helpcontext);
- pei->lpVtbl->GetHelpFile(pei, &helpfile);
- pei->lpVtbl->GetSource(pei, &source);
+ pei->lpVtbl->GetDescription(pei, &descr);
+ pei->lpVtbl->GetGUID(pei, &guid);
+ pei->lpVtbl->GetHelpContext(pei, &helpcontext);
+ pei->lpVtbl->GetHelpFile(pei, &helpfile);
+ pei->lpVtbl->GetSource(pei, &source);
- pei->lpVtbl->Release(pei);
+ pei->lpVtbl->Release(pei);
failed:
#ifdef WITH_THREAD
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
#endif
- progid = NULL;
- ProgIDFromCLSID(&guid, &progid);
-
- text = FormatError(errcode);
- obj = Py_BuildValue(
- "iu(uuuiu)",
- errcode,
- text,
- descr, source, helpfile, helpcontext,
- progid);
- if (obj) {
- PyErr_SetObject(ComError, obj);
- Py_DECREF(obj);
- }
- LocalFree(text);
-
- if (descr)
- SysFreeString(descr);
- if (helpfile)
- SysFreeString(helpfile);
- if (source)
- SysFreeString(source);
-
- return NULL;
+ progid = NULL;
+ ProgIDFromCLSID(&guid, &progid);
+
+ text = FormatError(errcode);
+ obj = Py_BuildValue(
+ "iu(uuuiu)",
+ errcode,
+ text,
+ descr, source, helpfile, helpcontext,
+ progid);
+ if (obj) {
+ PyErr_SetObject(ComError, obj);
+ Py_DECREF(obj);
+ }
+ LocalFree(text);
+
+ if (descr)
+ SysFreeString(descr);
+ if (helpfile)
+ SysFreeString(helpfile);
+ if (source)
+ SysFreeString(source);
+
+ return NULL;
}
#endif
@@ -1056,158 +1056,158 @@ GetComError(HRESULT errcode, GUID *riid, IUnknown *pIunk)
* - XXX various requirements for restype, not yet collected
*/
PyObject *_ctypes_callproc(PPROC pProc,
- PyObject *argtuple,
+ PyObject *argtuple,
#ifdef MS_WIN32
- IUnknown *pIunk,
- GUID *iid,
+ IUnknown *pIunk,
+ GUID *iid,
#endif
- int flags,
- PyObject *argtypes, /* misleading name: This is a tuple of
- methods, not types: the .from_param
- class methods of the types */
- PyObject *restype,
- PyObject *checker)
+ int flags,
+ PyObject *argtypes, /* misleading name: This is a tuple of
+ methods, not types: the .from_param
+ class methods of the types */
+ PyObject *restype,
+ PyObject *checker)
{
- Py_ssize_t i, n, argcount, argtype_count;
- void *resbuf;
- struct argument *args, *pa;
- ffi_type **atypes;
- ffi_type *rtype;
- void **avalues;
- PyObject *retval = NULL;
-
- n = argcount = PyTuple_GET_SIZE(argtuple);
+ Py_ssize_t i, n, argcount, argtype_count;
+ void *resbuf;
+ struct argument *args, *pa;
+ ffi_type **atypes;
+ ffi_type *rtype;
+ void **avalues;
+ PyObject *retval = NULL;
+
+ n = argcount = PyTuple_GET_SIZE(argtuple);
#ifdef MS_WIN32
- /* an optional COM object this pointer */
- if (pIunk)
- ++argcount;
+ /* an optional COM object this pointer */
+ if (pIunk)
+ ++argcount;
#endif
- args = (struct argument *)alloca(sizeof(struct argument) * argcount);
- if (!args) {
- PyErr_NoMemory();
- return NULL;
- }
- memset(args, 0, sizeof(struct argument) * argcount);
- argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0;
+ args = (struct argument *)alloca(sizeof(struct argument) * argcount);
+ if (!args) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(args, 0, sizeof(struct argument) * argcount);
+ argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0;
#ifdef MS_WIN32
- if (pIunk) {
- args[0].ffi_type = &ffi_type_pointer;
- args[0].value.p = pIunk;
- pa = &args[1];
- } else
+ if (pIunk) {
+ args[0].ffi_type = &ffi_type_pointer;
+ args[0].value.p = pIunk;
+ pa = &args[1];
+ } else
#endif
- pa = &args[0];
-
- /* Convert the arguments */
- for (i = 0; i < n; ++i, ++pa) {
- PyObject *converter;
- PyObject *arg;
- int err;
-
- arg = PyTuple_GET_ITEM(argtuple, i); /* borrowed ref */
- /* For cdecl functions, we allow more actual arguments
- than the length of the argtypes tuple.
- This is checked in _ctypes::PyCFuncPtr_Call
- */
- if (argtypes && argtype_count > i) {
- PyObject *v;
- converter = PyTuple_GET_ITEM(argtypes, i);
- v = PyObject_CallFunctionObjArgs(converter,
- arg,
- NULL);
- if (v == NULL) {
- _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
- goto cleanup;
- }
-
- err = ConvParam(v, i+1, pa);
- Py_DECREF(v);
- if (-1 == err) {
- _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
- goto cleanup;
- }
- } else {
- err = ConvParam(arg, i+1, pa);
- if (-1 == err) {
- _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
- goto cleanup; /* leaking ? */
- }
- }
- }
-
- rtype = _ctypes_get_ffi_type(restype);
- resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
-
- avalues = (void **)alloca(sizeof(void *) * argcount);
- atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount);
- if (!resbuf || !avalues || !atypes) {
- PyErr_NoMemory();
- goto cleanup;
- }
- for (i = 0; i < argcount; ++i) {
- atypes[i] = args[i].ffi_type;
- if (atypes[i]->type == FFI_TYPE_STRUCT
+ pa = &args[0];
+
+ /* Convert the arguments */
+ for (i = 0; i < n; ++i, ++pa) {
+ PyObject *converter;
+ PyObject *arg;
+ int err;
+
+ arg = PyTuple_GET_ITEM(argtuple, i); /* borrowed ref */
+ /* For cdecl functions, we allow more actual arguments
+ than the length of the argtypes tuple.
+ This is checked in _ctypes::PyCFuncPtr_Call
+ */
+ if (argtypes && argtype_count > i) {
+ PyObject *v;
+ converter = PyTuple_GET_ITEM(argtypes, i);
+ v = PyObject_CallFunctionObjArgs(converter,
+ arg,
+ NULL);
+ if (v == NULL) {
+ _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
+ goto cleanup;
+ }
+
+ err = ConvParam(v, i+1, pa);
+ Py_DECREF(v);
+ if (-1 == err) {
+ _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
+ goto cleanup;
+ }
+ } else {
+ err = ConvParam(arg, i+1, pa);
+ if (-1 == err) {
+ _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
+ goto cleanup; /* leaking ? */
+ }
+ }
+ }
+
+ rtype = _ctypes_get_ffi_type(restype);
+ resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
+
+ avalues = (void **)alloca(sizeof(void *) * argcount);
+ atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount);
+ if (!resbuf || !avalues || !atypes) {
+ PyErr_NoMemory();
+ goto cleanup;
+ }
+ for (i = 0; i < argcount; ++i) {
+ atypes[i] = args[i].ffi_type;
+ if (atypes[i]->type == FFI_TYPE_STRUCT
#ifdef _WIN64
- && atypes[i]->size <= sizeof(void *)
+ && atypes[i]->size <= sizeof(void *)
#endif
- )
- avalues[i] = (void *)args[i].value.p;
- else
- avalues[i] = (void *)&args[i].value;
- }
-
- if (-1 == _call_function_pointer(flags, pProc, avalues, atypes,
- rtype, resbuf,
- Py_SAFE_DOWNCAST(argcount,
- Py_ssize_t,
- int)))
- goto cleanup;
+ )
+ avalues[i] = (void *)args[i].value.p;
+ else
+ avalues[i] = (void *)&args[i].value;
+ }
+
+ if (-1 == _call_function_pointer(flags, pProc, avalues, atypes,
+ rtype, resbuf,
+ Py_SAFE_DOWNCAST(argcount,
+ Py_ssize_t,
+ int)))
+ goto cleanup;
#ifdef WORDS_BIGENDIAN
- /* libffi returns the result in a buffer with sizeof(ffi_arg). This
- causes problems on big endian machines, since the result buffer
- address cannot simply be used as result pointer, instead we must
- adjust the pointer value:
- */
- /*
- XXX I should find out and clarify why this is needed at all,
- especially why adjusting for ffi_type_float must be avoided on
- 64-bit platforms.
- */
- if (rtype->type != FFI_TYPE_FLOAT
- && rtype->type != FFI_TYPE_STRUCT
- && rtype->size < sizeof(ffi_arg))
- resbuf = (char *)resbuf + sizeof(ffi_arg) - rtype->size;
+ /* libffi returns the result in a buffer with sizeof(ffi_arg). This
+ causes problems on big endian machines, since the result buffer
+ address cannot simply be used as result pointer, instead we must
+ adjust the pointer value:
+ */
+ /*
+ XXX I should find out and clarify why this is needed at all,
+ especially why adjusting for ffi_type_float must be avoided on
+ 64-bit platforms.
+ */
+ if (rtype->type != FFI_TYPE_FLOAT
+ && rtype->type != FFI_TYPE_STRUCT
+ && rtype->size < sizeof(ffi_arg))
+ resbuf = (char *)resbuf + sizeof(ffi_arg) - rtype->size;
#endif
#ifdef MS_WIN32
- if (iid && pIunk) {
- if (*(int *)resbuf & 0x80000000)
- retval = GetComError(*(HRESULT *)resbuf, iid, pIunk);
- else
- retval = PyLong_FromLong(*(int *)resbuf);
- } else if (flags & FUNCFLAG_HRESULT) {
- if (*(int *)resbuf & 0x80000000)
- retval = PyErr_SetFromWindowsErr(*(int *)resbuf);
- else
- retval = PyLong_FromLong(*(int *)resbuf);
- } else
+ if (iid && pIunk) {
+ if (*(int *)resbuf & 0x80000000)
+ retval = GetComError(*(HRESULT *)resbuf, iid, pIunk);
+ else
+ retval = PyLong_FromLong(*(int *)resbuf);
+ } else if (flags & FUNCFLAG_HRESULT) {
+ if (*(int *)resbuf & 0x80000000)
+ retval = PyErr_SetFromWindowsErr(*(int *)resbuf);
+ else
+ retval = PyLong_FromLong(*(int *)resbuf);
+ } else
#endif
- retval = GetResult(restype, resbuf, checker);
+ retval = GetResult(restype, resbuf, checker);
cleanup:
- for (i = 0; i < argcount; ++i)
- Py_XDECREF(args[i].keep);
- return retval;
+ for (i = 0; i < argcount; ++i)
+ Py_XDECREF(args[i].keep);
+ return retval;
}
static int
_parse_voidp(PyObject *obj, void **address)
{
- *address = PyLong_AsVoidPtr(obj);
- if (*address == NULL)
- return 0;
- return 1;
+ *address = PyLong_AsVoidPtr(obj);
+ if (*address == NULL)
+ return 0;
+ return 1;
}
#ifdef MS_WIN32
@@ -1219,21 +1219,21 @@ Convert a win32 error code into a string. If the error code is not\n\
given, the return value of a call to GetLastError() is used.\n";
static PyObject *format_error(PyObject *self, PyObject *args)
{
- PyObject *result;
- wchar_t *lpMsgBuf;
- DWORD code = 0;
- if (!PyArg_ParseTuple(args, "|i:FormatError", &code))
- return NULL;
- if (code == 0)
- code = GetLastError();
- lpMsgBuf = FormatError(code);
- if (lpMsgBuf) {
- result = PyUnicode_FromWideChar(lpMsgBuf, wcslen(lpMsgBuf));
- LocalFree(lpMsgBuf);
- } else {
- result = PyUnicode_FromString("<no description>");
- }
- return result;
+ PyObject *result;
+ wchar_t *lpMsgBuf;
+ DWORD code = 0;
+ if (!PyArg_ParseTuple(args, "|i:FormatError", &code))
+ return NULL;
+ if (code == 0)
+ code = GetLastError();
+ lpMsgBuf = FormatError(code);
+ if (lpMsgBuf) {
+ result = PyUnicode_FromWideChar(lpMsgBuf, wcslen(lpMsgBuf));
+ LocalFree(lpMsgBuf);
+ } else {
+ result = PyUnicode_FromString("<no description>");
+ }
+ return result;
}
static char load_library_doc[] =
@@ -1244,24 +1244,24 @@ The handle may be used to locate exported functions in this\n\
module.\n";
static PyObject *load_library(PyObject *self, PyObject *args)
{
- WCHAR *name;
- PyObject *nameobj;
- PyObject *ignored;
- HMODULE hMod;
- if (!PyArg_ParseTuple(args, "O|O:LoadLibrary", &nameobj, &ignored))
- return NULL;
-
- name = PyUnicode_AsUnicode(nameobj);
- if (!name)
- return NULL;
-
- hMod = LoadLibraryW(name);
- if (!hMod)
- return PyErr_SetFromWindowsErr(GetLastError());
+ WCHAR *name;
+ PyObject *nameobj;
+ PyObject *ignored;
+ HMODULE hMod;
+ if (!PyArg_ParseTuple(args, "O|O:LoadLibrary", &nameobj, &ignored))
+ return NULL;
+
+ name = PyUnicode_AsUnicode(nameobj);
+ if (!name)
+ return NULL;
+
+ hMod = LoadLibraryW(name);
+ if (!hMod)
+ return PyErr_SetFromWindowsErr(GetLastError());
#ifdef _WIN64
- return PyLong_FromVoidPtr(hMod);
+ return PyLong_FromVoidPtr(hMod);
#else
- return Py_BuildValue("i", hMod);
+ return Py_BuildValue("i", hMod);
#endif
}
@@ -1271,13 +1271,13 @@ static char free_library_doc[] =
Free the handle of an executable previously loaded by LoadLibrary.\n";
static PyObject *free_library(PyObject *self, PyObject *args)
{
- void *hMod;
- if (!PyArg_ParseTuple(args, "O&:FreeLibrary", &_parse_voidp, &hMod))
- return NULL;
- if (!FreeLibrary((HMODULE)hMod))
- return PyErr_SetFromWindowsErr(GetLastError());
- Py_INCREF(Py_None);
- return Py_None;
+ void *hMod;
+ if (!PyArg_ParseTuple(args, "O&:FreeLibrary", &_parse_voidp, &hMod))
+ return NULL;
+ if (!FreeLibrary((HMODULE)hMod))
+ return PyErr_SetFromWindowsErr(GetLastError());
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* obsolete, should be removed */
@@ -1285,55 +1285,55 @@ static PyObject *free_library(PyObject *self, PyObject *args)
static PyObject *
call_commethod(PyObject *self, PyObject *args)
{
- IUnknown *pIunk;
- int index;
- PyObject *arguments;
- PPROC *lpVtbl;
- PyObject *result;
- CDataObject *pcom;
- PyObject *argtypes = NULL;
-
- if (!PyArg_ParseTuple(args,
- "OiO!|O!",
- &pcom, &index,
- &PyTuple_Type, &arguments,
- &PyTuple_Type, &argtypes))
- return NULL;
-
- if (argtypes && (PyTuple_GET_SIZE(arguments) != PyTuple_GET_SIZE(argtypes))) {
- PyErr_Format(PyExc_TypeError,
- "Method takes %d arguments (%d given)",
- PyTuple_GET_SIZE(argtypes), PyTuple_GET_SIZE(arguments));
- return NULL;
- }
-
- if (!CDataObject_Check(pcom) || (pcom->b_size != sizeof(void *))) {
- PyErr_Format(PyExc_TypeError,
- "COM Pointer expected instead of %s instance",
- Py_TYPE(pcom)->tp_name);
- return NULL;
- }
-
- if ((*(void **)(pcom->b_ptr)) == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "The COM 'this' pointer is NULL");
- return NULL;
- }
-
- pIunk = (IUnknown *)(*(void **)(pcom->b_ptr));
- lpVtbl = (PPROC *)(pIunk->lpVtbl);
-
- result = _ctypes_callproc(lpVtbl[index],
- arguments,
+ IUnknown *pIunk;
+ int index;
+ PyObject *arguments;
+ PPROC *lpVtbl;
+ PyObject *result;
+ CDataObject *pcom;
+ PyObject *argtypes = NULL;
+
+ if (!PyArg_ParseTuple(args,
+ "OiO!|O!",
+ &pcom, &index,
+ &PyTuple_Type, &arguments,
+ &PyTuple_Type, &argtypes))
+ return NULL;
+
+ if (argtypes && (PyTuple_GET_SIZE(arguments) != PyTuple_GET_SIZE(argtypes))) {
+ PyErr_Format(PyExc_TypeError,
+ "Method takes %d arguments (%d given)",
+ PyTuple_GET_SIZE(argtypes), PyTuple_GET_SIZE(arguments));
+ return NULL;
+ }
+
+ if (!CDataObject_Check(pcom) || (pcom->b_size != sizeof(void *))) {
+ PyErr_Format(PyExc_TypeError,
+ "COM Pointer expected instead of %s instance",
+ Py_TYPE(pcom)->tp_name);
+ return NULL;
+ }
+
+ if ((*(void **)(pcom->b_ptr)) == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "The COM 'this' pointer is NULL");
+ return NULL;
+ }
+
+ pIunk = (IUnknown *)(*(void **)(pcom->b_ptr));
+ lpVtbl = (PPROC *)(pIunk->lpVtbl);
+
+ result = _ctypes_callproc(lpVtbl[index],
+ arguments,
#ifdef MS_WIN32
- pIunk,
- NULL,
+ pIunk,
+ NULL,
#endif
- FUNCFLAG_HRESULT, /* flags */
- argtypes, /* self->argtypes */
- NULL, /* self->restype */
- NULL); /* checker */
- return result;
+ FUNCFLAG_HRESULT, /* flags */
+ argtypes, /* self->argtypes */
+ NULL, /* self->restype */
+ NULL); /* checker */
+ return result;
}
static char copy_com_pointer_doc[] =
@@ -1342,102 +1342,102 @@ static char copy_com_pointer_doc[] =
static PyObject *
copy_com_pointer(PyObject *self, PyObject *args)
{
- PyObject *p1, *p2, *r = NULL;
- struct argument a, b;
- IUnknown *src, **pdst;
- if (!PyArg_ParseTuple(args, "OO:CopyComPointer", &p1, &p2))
- return NULL;
- a.keep = b.keep = NULL;
-
- if (-1 == ConvParam(p1, 0, &a) || -1 == ConvParam(p2, 1, &b))
- goto done;
- src = (IUnknown *)a.value.p;
- pdst = (IUnknown **)b.value.p;
-
- if (pdst == NULL)
- r = PyLong_FromLong(E_POINTER);
- else {
- if (src)
- src->lpVtbl->AddRef(src);
- *pdst = src;
- r = PyLong_FromLong(S_OK);
- }
+ PyObject *p1, *p2, *r = NULL;
+ struct argument a, b;
+ IUnknown *src, **pdst;
+ if (!PyArg_ParseTuple(args, "OO:CopyComPointer", &p1, &p2))
+ return NULL;
+ a.keep = b.keep = NULL;
+
+ if (-1 == ConvParam(p1, 0, &a) || -1 == ConvParam(p2, 1, &b))
+ goto done;
+ src = (IUnknown *)a.value.p;
+ pdst = (IUnknown **)b.value.p;
+
+ if (pdst == NULL)
+ r = PyLong_FromLong(E_POINTER);
+ else {
+ if (src)
+ src->lpVtbl->AddRef(src);
+ *pdst = src;
+ r = PyLong_FromLong(S_OK);
+ }
done:
- Py_XDECREF(a.keep);
- Py_XDECREF(b.keep);
- return r;
+ Py_XDECREF(a.keep);
+ Py_XDECREF(b.keep);
+ return r;
}
#else
static PyObject *py_dl_open(PyObject *self, PyObject *args)
{
- PyObject *name, *name2;
- char *name_str;
- void * handle;
-#ifdef RTLD_LOCAL
- int mode = RTLD_NOW | RTLD_LOCAL;
+ PyObject *name, *name2;
+ char *name_str;
+ void * handle;
+#ifdef RTLD_LOCAL
+ int mode = RTLD_NOW | RTLD_LOCAL;
#else
- /* cygwin doesn't define RTLD_LOCAL */
- int mode = RTLD_NOW;
+ /* cygwin doesn't define RTLD_LOCAL */
+ int mode = RTLD_NOW;
#endif
- if (!PyArg_ParseTuple(args, "O|i:dlopen", &name, &mode))
- return NULL;
- mode |= RTLD_NOW;
- if (name != Py_None) {
- if (PyUnicode_FSConverter(name, &name2) == 0)
- return NULL;
- if (PyBytes_Check(name2))
- name_str = PyBytes_AS_STRING(name2);
- else
- name_str = PyByteArray_AS_STRING(name2);
- } else {
- name_str = NULL;
- name2 = NULL;
- }
- handle = ctypes_dlopen(name_str, mode);
- Py_XDECREF(name2);
- if (!handle) {
- char *errmsg = ctypes_dlerror();
- if (!errmsg)
- errmsg = "dlopen() error";
- PyErr_SetString(PyExc_OSError,
- errmsg);
- return NULL;
- }
- return PyLong_FromVoidPtr(handle);
+ if (!PyArg_ParseTuple(args, "O|i:dlopen", &name, &mode))
+ return NULL;
+ mode |= RTLD_NOW;
+ if (name != Py_None) {
+ if (PyUnicode_FSConverter(name, &name2) == 0)
+ return NULL;
+ if (PyBytes_Check(name2))
+ name_str = PyBytes_AS_STRING(name2);
+ else
+ name_str = PyByteArray_AS_STRING(name2);
+ } else {
+ name_str = NULL;
+ name2 = NULL;
+ }
+ handle = ctypes_dlopen(name_str, mode);
+ Py_XDECREF(name2);
+ if (!handle) {
+ char *errmsg = ctypes_dlerror();
+ if (!errmsg)
+ errmsg = "dlopen() error";
+ PyErr_SetString(PyExc_OSError,
+ errmsg);
+ return NULL;
+ }
+ return PyLong_FromVoidPtr(handle);
}
static PyObject *py_dl_close(PyObject *self, PyObject *args)
{
- void *handle;
-
- if (!PyArg_ParseTuple(args, "O&:dlclose", &_parse_voidp, &handle))
- return NULL;
- if (dlclose(handle)) {
- PyErr_SetString(PyExc_OSError,
- ctypes_dlerror());
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ void *handle;
+
+ if (!PyArg_ParseTuple(args, "O&:dlclose", &_parse_voidp, &handle))
+ return NULL;
+ if (dlclose(handle)) {
+ PyErr_SetString(PyExc_OSError,
+ ctypes_dlerror());
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *py_dl_sym(PyObject *self, PyObject *args)
{
- char *name;
- void *handle;
- void *ptr;
-
- if (!PyArg_ParseTuple(args, "O&s:dlsym",
- &_parse_voidp, &handle, &name))
- return NULL;
- ptr = ctypes_dlsym((void*)handle, name);
- if (!ptr) {
- PyErr_SetString(PyExc_OSError,
- ctypes_dlerror());
- return NULL;
- }
- return PyLong_FromVoidPtr(ptr);
+ char *name;
+ void *handle;
+ void *ptr;
+
+ if (!PyArg_ParseTuple(args, "O&s:dlsym",
+ &_parse_voidp, &handle, &name))
+ return NULL;
+ ptr = ctypes_dlsym((void*)handle, name);
+ if (!ptr) {
+ PyErr_SetString(PyExc_OSError,
+ ctypes_dlerror());
+ return NULL;
+ }
+ return PyLong_FromVoidPtr(ptr);
}
#endif
@@ -1449,27 +1449,27 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args)
static PyObject *
call_function(PyObject *self, PyObject *args)
{
- void *func;
- PyObject *arguments;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args,
- "O&O!",
- &_parse_voidp, &func,
- &PyTuple_Type, &arguments))
- return NULL;
-
- result = _ctypes_callproc((PPROC)func,
- arguments,
+ void *func;
+ PyObject *arguments;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args,
+ "O&O!",
+ &_parse_voidp, &func,
+ &PyTuple_Type, &arguments))
+ return NULL;
+
+ result = _ctypes_callproc((PPROC)func,
+ arguments,
#ifdef MS_WIN32
- NULL,
- NULL,
+ NULL,
+ NULL,
#endif
- 0, /* flags */
- NULL, /* self->argtypes */
- NULL, /* self->restype */
- NULL); /* checker */
- return result;
+ 0, /* flags */
+ NULL, /* self->argtypes */
+ NULL, /* self->restype */
+ NULL); /* checker */
+ return result;
}
/*
@@ -1480,27 +1480,27 @@ call_function(PyObject *self, PyObject *args)
static PyObject *
call_cdeclfunction(PyObject *self, PyObject *args)
{
- void *func;
- PyObject *arguments;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args,
- "O&O!",
- &_parse_voidp, &func,
- &PyTuple_Type, &arguments))
- return NULL;
-
- result = _ctypes_callproc((PPROC)func,
- arguments,
+ void *func;
+ PyObject *arguments;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args,
+ "O&O!",
+ &_parse_voidp, &func,
+ &PyTuple_Type, &arguments))
+ return NULL;
+
+ result = _ctypes_callproc((PPROC)func,
+ arguments,
#ifdef MS_WIN32
- NULL,
- NULL,
+ NULL,
+ NULL,
#endif
- FUNCFLAG_CDECL, /* flags */
- NULL, /* self->argtypes */
- NULL, /* self->restype */
- NULL); /* checker */
- return result;
+ FUNCFLAG_CDECL, /* flags */
+ NULL, /* self->argtypes */
+ NULL, /* self->restype */
+ NULL); /* checker */
+ return result;
}
/*****************************************************************
@@ -1514,17 +1514,17 @@ static char sizeof_doc[] =
static PyObject *
sizeof_func(PyObject *self, PyObject *obj)
{
- StgDictObject *dict;
+ StgDictObject *dict;
- dict = PyType_stgdict(obj);
- if (dict)
- return PyLong_FromSsize_t(dict->size);
+ dict = PyType_stgdict(obj);
+ if (dict)
+ return PyLong_FromSsize_t(dict->size);
- if (CDataObject_Check(obj))
- return PyLong_FromSsize_t(((CDataObject *)obj)->b_size);
- PyErr_SetString(PyExc_TypeError,
- "this type has no size");
- return NULL;
+ if (CDataObject_Check(obj))
+ return PyLong_FromSsize_t(((CDataObject *)obj)->b_size);
+ PyErr_SetString(PyExc_TypeError,
+ "this type has no size");
+ return NULL;
}
static char alignment_doc[] =
@@ -1535,19 +1535,19 @@ static char alignment_doc[] =
static PyObject *
align_func(PyObject *self, PyObject *obj)
{
- StgDictObject *dict;
+ StgDictObject *dict;
- dict = PyType_stgdict(obj);
- if (dict)
- return PyLong_FromSsize_t(dict->align);
+ dict = PyType_stgdict(obj);
+ if (dict)
+ return PyLong_FromSsize_t(dict->align);
- dict = PyObject_stgdict(obj);
- if (dict)
- return PyLong_FromSsize_t(dict->align);
+ dict = PyObject_stgdict(obj);
+ if (dict)
+ return PyLong_FromSsize_t(dict->align);
- PyErr_SetString(PyExc_TypeError,
- "no alignment info");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "no alignment info");
+ return NULL;
}
static char byref_doc[] =
@@ -1562,36 +1562,36 @@ static char byref_doc[] =
static PyObject *
byref(PyObject *self, PyObject *args)
{
- PyCArgObject *parg;
- PyObject *obj;
- PyObject *pyoffset = NULL;
- Py_ssize_t offset = 0;
-
- if (!PyArg_UnpackTuple(args, "byref", 1, 2,
- &obj, &pyoffset))
- return NULL;
- if (pyoffset) {
- offset = PyNumber_AsSsize_t(pyoffset, NULL);
- if (offset == -1 && PyErr_Occurred())
- return NULL;
- }
- if (!CDataObject_Check(obj)) {
- PyErr_Format(PyExc_TypeError,
- "byref() argument must be a ctypes instance, not '%s'",
- Py_TYPE(obj)->tp_name);
- return NULL;
- }
-
- parg = PyCArgObject_new();
- if (parg == NULL)
- return NULL;
-
- parg->tag = 'P';
- parg->pffi_type = &ffi_type_pointer;
- Py_INCREF(obj);
- parg->obj = obj;
- parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
- return (PyObject *)parg;
+ PyCArgObject *parg;
+ PyObject *obj;
+ PyObject *pyoffset = NULL;
+ Py_ssize_t offset = 0;
+
+ if (!PyArg_UnpackTuple(args, "byref", 1, 2,
+ &obj, &pyoffset))
+ return NULL;
+ if (pyoffset) {
+ offset = PyNumber_AsSsize_t(pyoffset, NULL);
+ if (offset == -1 && PyErr_Occurred())
+ return NULL;
+ }
+ if (!CDataObject_Check(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "byref() argument must be a ctypes instance, not '%s'",
+ Py_TYPE(obj)->tp_name);
+ return NULL;
+ }
+
+ parg = PyCArgObject_new();
+ if (parg == NULL)
+ return NULL;
+
+ parg->tag = 'P';
+ parg->pffi_type = &ffi_type_pointer;
+ Py_INCREF(obj);
+ parg->obj = obj;
+ parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
+ return (PyObject *)parg;
}
static char addressof_doc[] =
@@ -1601,44 +1601,44 @@ static char addressof_doc[] =
static PyObject *
addressof(PyObject *self, PyObject *obj)
{
- if (CDataObject_Check(obj))
- return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr);
- PyErr_SetString(PyExc_TypeError,
- "invalid type");
- return NULL;
+ if (CDataObject_Check(obj))
+ return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr);
+ PyErr_SetString(PyExc_TypeError,
+ "invalid type");
+ return NULL;
}
static int
converter(PyObject *obj, void **address)
{
- *address = PyLong_AsVoidPtr(obj);
- return *address != NULL;
+ *address = PyLong_AsVoidPtr(obj);
+ return *address != NULL;
}
static PyObject *
My_PyObj_FromPtr(PyObject *self, PyObject *args)
{
- PyObject *ob;
- if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob))
- return NULL;
- Py_INCREF(ob);
- return ob;
+ PyObject *ob;
+ if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob))
+ return NULL;
+ Py_INCREF(ob);
+ return ob;
}
static PyObject *
My_Py_INCREF(PyObject *self, PyObject *arg)
{
- Py_INCREF(arg); /* that's what this function is for */
- Py_INCREF(arg); /* that for returning it */
- return arg;
+ Py_INCREF(arg); /* that's what this function is for */
+ Py_INCREF(arg); /* that for returning it */
+ return arg;
}
static PyObject *
My_Py_DECREF(PyObject *self, PyObject *arg)
{
- Py_DECREF(arg); /* that's what this function is for */
- Py_INCREF(arg); /* that's for returning it */
- return arg;
+ Py_DECREF(arg); /* that's what this function is for */
+ Py_INCREF(arg); /* that's for returning it */
+ return arg;
}
#ifdef CTYPES_UNICODE
@@ -1652,234 +1652,234 @@ between unicode and strings. Returns the previous values.\n";
static PyObject *
set_conversion_mode(PyObject *self, PyObject *args)
{
- char *coding, *mode;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args, "zs:set_conversion_mode", &coding, &mode))
- return NULL;
- result = Py_BuildValue("(zz)", _ctypes_conversion_encoding, _ctypes_conversion_errors);
- if (coding) {
- PyMem_Free(_ctypes_conversion_encoding);
- _ctypes_conversion_encoding = PyMem_Malloc(strlen(coding) + 1);
- strcpy(_ctypes_conversion_encoding, coding);
- } else {
- _ctypes_conversion_encoding = NULL;
- }
- PyMem_Free(_ctypes_conversion_errors);
- _ctypes_conversion_errors = PyMem_Malloc(strlen(mode) + 1);
- strcpy(_ctypes_conversion_errors, mode);
- return result;
+ char *coding, *mode;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "zs:set_conversion_mode", &coding, &mode))
+ return NULL;
+ result = Py_BuildValue("(zz)", _ctypes_conversion_encoding, _ctypes_conversion_errors);
+ if (coding) {
+ PyMem_Free(_ctypes_conversion_encoding);
+ _ctypes_conversion_encoding = PyMem_Malloc(strlen(coding) + 1);
+ strcpy(_ctypes_conversion_encoding, coding);
+ } else {
+ _ctypes_conversion_encoding = NULL;
+ }
+ PyMem_Free(_ctypes_conversion_errors);
+ _ctypes_conversion_errors = PyMem_Malloc(strlen(mode) + 1);
+ strcpy(_ctypes_conversion_errors, mode);
+ return result;
}
#endif
static PyObject *
resize(PyObject *self, PyObject *args)
{
- CDataObject *obj;
- StgDictObject *dict;
- Py_ssize_t size;
-
- if (!PyArg_ParseTuple(args,
- "On:resize",
- &obj, &size))
- return NULL;
-
- dict = PyObject_stgdict((PyObject *)obj);
- if (dict == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "excepted ctypes instance");
- return NULL;
- }
- if (size < dict->size) {
- PyErr_Format(PyExc_ValueError,
- "minimum size is %zd",
- dict->size);
- return NULL;
- }
- if (obj->b_needsfree == 0) {
- PyErr_Format(PyExc_ValueError,
- "Memory cannot be resized because this object doesn't own it");
- return NULL;
- }
- if (size <= sizeof(obj->b_value)) {
- /* internal default buffer is large enough */
- obj->b_size = size;
- goto done;
- }
- if (obj->b_size <= sizeof(obj->b_value)) {
- /* We are currently using the objects default buffer, but it
- isn't large enough any more. */
- void *ptr = PyMem_Malloc(size);
- if (ptr == NULL)
- return PyErr_NoMemory();
- memset(ptr, 0, size);
- memmove(ptr, obj->b_ptr, obj->b_size);
- obj->b_ptr = ptr;
- obj->b_size = size;
- } else {
- void * ptr = PyMem_Realloc(obj->b_ptr, size);
- if (ptr == NULL)
- return PyErr_NoMemory();
- obj->b_ptr = ptr;
- obj->b_size = size;
- }
+ CDataObject *obj;
+ StgDictObject *dict;
+ Py_ssize_t size;
+
+ if (!PyArg_ParseTuple(args,
+ "On:resize",
+ &obj, &size))
+ return NULL;
+
+ dict = PyObject_stgdict((PyObject *)obj);
+ if (dict == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "excepted ctypes instance");
+ return NULL;
+ }
+ if (size < dict->size) {
+ PyErr_Format(PyExc_ValueError,
+ "minimum size is %zd",
+ dict->size);
+ return NULL;
+ }
+ if (obj->b_needsfree == 0) {
+ PyErr_Format(PyExc_ValueError,
+ "Memory cannot be resized because this object doesn't own it");
+ return NULL;
+ }
+ if (size <= sizeof(obj->b_value)) {
+ /* internal default buffer is large enough */
+ obj->b_size = size;
+ goto done;
+ }
+ if (obj->b_size <= sizeof(obj->b_value)) {
+ /* We are currently using the objects default buffer, but it
+ isn't large enough any more. */
+ void *ptr = PyMem_Malloc(size);
+ if (ptr == NULL)
+ return PyErr_NoMemory();
+ memset(ptr, 0, size);
+ memmove(ptr, obj->b_ptr, obj->b_size);
+ obj->b_ptr = ptr;
+ obj->b_size = size;
+ } else {
+ void * ptr = PyMem_Realloc(obj->b_ptr, size);
+ if (ptr == NULL)
+ return PyErr_NoMemory();
+ obj->b_ptr = ptr;
+ obj->b_size = size;
+ }
done:
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
unpickle(PyObject *self, PyObject *args)
{
- PyObject *typ;
- PyObject *state;
- PyObject *result;
- PyObject *tmp;
-
- if (!PyArg_ParseTuple(args, "OO", &typ, &state))
- return NULL;
- result = PyObject_CallMethod(typ, "__new__", "O", typ);
- if (result == NULL)
- return NULL;
- tmp = PyObject_CallMethod(result, "__setstate__", "O", state);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(tmp);
- return result;
+ PyObject *typ;
+ PyObject *state;
+ PyObject *result;
+ PyObject *tmp;
+
+ if (!PyArg_ParseTuple(args, "OO", &typ, &state))
+ return NULL;
+ result = PyObject_CallMethod(typ, "__new__", "O", typ);
+ if (result == NULL)
+ return NULL;
+ tmp = PyObject_CallMethod(result, "__setstate__", "O", state);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(tmp);
+ return result;
}
static PyObject *
POINTER(PyObject *self, PyObject *cls)
{
- PyObject *result;
- PyTypeObject *typ;
- PyObject *key;
- char *buf;
-
- result = PyDict_GetItem(_ctypes_ptrtype_cache, cls);
- if (result) {
- Py_INCREF(result);
- return result;
- }
- if (PyUnicode_CheckExact(cls)) {
- char *name = _PyUnicode_AsString(cls);
- buf = alloca(strlen(name) + 3 + 1);
- sprintf(buf, "LP_%s", name);
- result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
- "s(O){}",
- buf,
- &PyCPointer_Type);
- if (result == NULL)
- return result;
- key = PyLong_FromVoidPtr(result);
- } else if (PyType_Check(cls)) {
- typ = (PyTypeObject *)cls;
- buf = alloca(strlen(typ->tp_name) + 3 + 1);
- sprintf(buf, "LP_%s", typ->tp_name);
- result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
- "s(O){sO}",
- buf,
- &PyCPointer_Type,
- "_type_", cls);
- if (result == NULL)
- return result;
- Py_INCREF(cls);
- key = cls;
- } else {
- PyErr_SetString(PyExc_TypeError, "must be a ctypes type");
- return NULL;
- }
- if (-1 == PyDict_SetItem(_ctypes_ptrtype_cache, key, result)) {
- Py_DECREF(result);
- Py_DECREF(key);
- return NULL;
- }
- Py_DECREF(key);
- return result;
+ PyObject *result;
+ PyTypeObject *typ;
+ PyObject *key;
+ char *buf;
+
+ result = PyDict_GetItem(_ctypes_ptrtype_cache, cls);
+ if (result) {
+ Py_INCREF(result);
+ return result;
+ }
+ if (PyUnicode_CheckExact(cls)) {
+ char *name = _PyUnicode_AsString(cls);
+ buf = alloca(strlen(name) + 3 + 1);
+ sprintf(buf, "LP_%s", name);
+ result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
+ "s(O){}",
+ buf,
+ &PyCPointer_Type);
+ if (result == NULL)
+ return result;
+ key = PyLong_FromVoidPtr(result);
+ } else if (PyType_Check(cls)) {
+ typ = (PyTypeObject *)cls;
+ buf = alloca(strlen(typ->tp_name) + 3 + 1);
+ sprintf(buf, "LP_%s", typ->tp_name);
+ result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
+ "s(O){sO}",
+ buf,
+ &PyCPointer_Type,
+ "_type_", cls);
+ if (result == NULL)
+ return result;
+ Py_INCREF(cls);
+ key = cls;
+ } else {
+ PyErr_SetString(PyExc_TypeError, "must be a ctypes type");
+ return NULL;
+ }
+ if (-1 == PyDict_SetItem(_ctypes_ptrtype_cache, key, result)) {
+ Py_DECREF(result);
+ Py_DECREF(key);
+ return NULL;
+ }
+ Py_DECREF(key);
+ return result;
}
static PyObject *
pointer(PyObject *self, PyObject *arg)
{
- PyObject *result;
- PyObject *typ;
-
- typ = PyDict_GetItem(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg));
- if (typ)
- return PyObject_CallFunctionObjArgs(typ, arg, NULL);
- typ = POINTER(NULL, (PyObject *)Py_TYPE(arg));
- if (typ == NULL)
- return NULL;
- result = PyObject_CallFunctionObjArgs(typ, arg, NULL);
- Py_DECREF(typ);
- return result;
+ PyObject *result;
+ PyObject *typ;
+
+ typ = PyDict_GetItem(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg));
+ if (typ)
+ return PyObject_CallFunctionObjArgs(typ, arg, NULL);
+ typ = POINTER(NULL, (PyObject *)Py_TYPE(arg));
+ if (typ == NULL)
+ return NULL;
+ result = PyObject_CallFunctionObjArgs(typ, arg, NULL);
+ Py_DECREF(typ);
+ return result;
}
static PyObject *
buffer_info(PyObject *self, PyObject *arg)
{
- StgDictObject *dict = PyType_stgdict(arg);
- PyObject *shape;
- Py_ssize_t i;
-
- if (dict == NULL)
- dict = PyObject_stgdict(arg);
- if (dict == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "not a ctypes type or object");
- return NULL;
- }
- shape = PyTuple_New(dict->ndim);
- if (shape == NULL)
- return NULL;
- for (i = 0; i < (int)dict->ndim; ++i)
- PyTuple_SET_ITEM(shape, i, PyLong_FromSsize_t(dict->shape[i]));
-
- if (PyErr_Occurred()) {
- Py_DECREF(shape);
- return NULL;
- }
- return Py_BuildValue("siN", dict->format, dict->ndim, shape);
+ StgDictObject *dict = PyType_stgdict(arg);
+ PyObject *shape;
+ Py_ssize_t i;
+
+ if (dict == NULL)
+ dict = PyObject_stgdict(arg);
+ if (dict == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "not a ctypes type or object");
+ return NULL;
+ }
+ shape = PyTuple_New(dict->ndim);
+ if (shape == NULL)
+ return NULL;
+ for (i = 0; i < (int)dict->ndim; ++i)
+ PyTuple_SET_ITEM(shape, i, PyLong_FromSsize_t(dict->shape[i]));
+
+ if (PyErr_Occurred()) {
+ Py_DECREF(shape);
+ return NULL;
+ }
+ return Py_BuildValue("siN", dict->format, dict->ndim, shape);
}
PyMethodDef _ctypes_module_methods[] = {
- {"get_errno", get_errno, METH_NOARGS},
- {"set_errno", set_errno, METH_VARARGS},
- {"POINTER", POINTER, METH_O },
- {"pointer", pointer, METH_O },
- {"_unpickle", unpickle, METH_VARARGS },
- {"buffer_info", buffer_info, METH_O, "Return buffer interface information"},
- {"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"},
+ {"get_errno", get_errno, METH_NOARGS},
+ {"set_errno", set_errno, METH_VARARGS},
+ {"POINTER", POINTER, METH_O },
+ {"pointer", pointer, METH_O },
+ {"_unpickle", unpickle, METH_VARARGS },
+ {"buffer_info", buffer_info, METH_O, "Return buffer interface information"},
+ {"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"},
#ifdef CTYPES_UNICODE
- {"set_conversion_mode", set_conversion_mode, METH_VARARGS, set_conversion_mode_doc},
+ {"set_conversion_mode", set_conversion_mode, METH_VARARGS, set_conversion_mode_doc},
#endif
#ifdef MS_WIN32
- {"get_last_error", get_last_error, METH_NOARGS},
- {"set_last_error", set_last_error, METH_VARARGS},
- {"CopyComPointer", copy_com_pointer, METH_VARARGS, copy_com_pointer_doc},
- {"FormatError", format_error, METH_VARARGS, format_error_doc},
- {"LoadLibrary", load_library, METH_VARARGS, load_library_doc},
- {"FreeLibrary", free_library, METH_VARARGS, free_library_doc},
- {"call_commethod", call_commethod, METH_VARARGS },
- {"_check_HRESULT", check_hresult, METH_VARARGS},
+ {"get_last_error", get_last_error, METH_NOARGS},
+ {"set_last_error", set_last_error, METH_VARARGS},
+ {"CopyComPointer", copy_com_pointer, METH_VARARGS, copy_com_pointer_doc},
+ {"FormatError", format_error, METH_VARARGS, format_error_doc},
+ {"LoadLibrary", load_library, METH_VARARGS, load_library_doc},
+ {"FreeLibrary", free_library, METH_VARARGS, free_library_doc},
+ {"call_commethod", call_commethod, METH_VARARGS },
+ {"_check_HRESULT", check_hresult, METH_VARARGS},
#else
- {"dlopen", py_dl_open, METH_VARARGS,
- "dlopen(name, flag={RTLD_GLOBAL|RTLD_LOCAL}) open a shared library"},
- {"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
- {"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
+ {"dlopen", py_dl_open, METH_VARARGS,
+ "dlopen(name, flag={RTLD_GLOBAL|RTLD_LOCAL}) open a shared library"},
+ {"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"},
+ {"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"},
#endif
- {"alignment", align_func, METH_O, alignment_doc},
- {"sizeof", sizeof_func, METH_O, sizeof_doc},
- {"byref", byref, METH_VARARGS, byref_doc},
- {"addressof", addressof, METH_O, addressof_doc},
- {"call_function", call_function, METH_VARARGS },
- {"call_cdeclfunction", call_cdeclfunction, METH_VARARGS },
- {"PyObj_FromPtr", My_PyObj_FromPtr, METH_VARARGS },
- {"Py_INCREF", My_Py_INCREF, METH_O },
- {"Py_DECREF", My_Py_DECREF, METH_O },
- {NULL, NULL} /* Sentinel */
+ {"alignment", align_func, METH_O, alignment_doc},
+ {"sizeof", sizeof_func, METH_O, sizeof_doc},
+ {"byref", byref, METH_VARARGS, byref_doc},
+ {"addressof", addressof, METH_O, addressof_doc},
+ {"call_function", call_function, METH_VARARGS },
+ {"call_cdeclfunction", call_cdeclfunction, METH_VARARGS },
+ {"PyObj_FromPtr", My_PyObj_FromPtr, METH_VARARGS },
+ {"Py_INCREF", My_Py_INCREF, METH_O },
+ {"Py_DECREF", My_Py_DECREF, METH_O },
+ {NULL, NULL} /* Sentinel */
};
/*
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index f7fd7fa..441ed9e 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -11,10 +11,10 @@
static void pymem_destructor(PyObject *ptr)
{
- void *p = PyCapsule_GetPointer(ptr, CTYPES_CFIELD_CAPSULE_NAME_PYMEM);
- if (p) {
- PyMem_Free(p);
- }
+ void *p = PyCapsule_GetPointer(ptr, CTYPES_CFIELD_CAPSULE_NAME_PYMEM);
+ if (p) {
+ PyMem_Free(p);
+ }
}
@@ -25,9 +25,9 @@ static void pymem_destructor(PyObject *ptr)
static PyObject *
PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- CFieldObject *obj;
- obj = (CFieldObject *)type->tp_alloc(type, 0);
- return (PyObject *)obj;
+ CFieldObject *obj;
+ obj = (CFieldObject *)type->tp_alloc(type, 0);
+ return (PyObject *)obj;
}
/*
@@ -44,297 +44,297 @@ PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
*/
PyObject *
PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
- Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
- Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
- int pack, int big_endian)
-{
- CFieldObject *self;
- PyObject *proto;
- Py_ssize_t size, align, length;
- SETFUNC setfunc = NULL;
- GETFUNC getfunc = NULL;
- StgDictObject *dict;
- int fieldtype;
+ Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
+ Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
+ int pack, int big_endian)
+{
+ CFieldObject *self;
+ PyObject *proto;
+ Py_ssize_t size, align, length;
+ SETFUNC setfunc = NULL;
+ GETFUNC getfunc = NULL;
+ StgDictObject *dict;
+ int fieldtype;
#define NO_BITFIELD 0
#define NEW_BITFIELD 1
#define CONT_BITFIELD 2
#define EXPAND_BITFIELD 3
- self = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type,
- NULL);
- if (self == NULL)
- return NULL;
- dict = PyType_stgdict(desc);
- if (!dict) {
- PyErr_SetString(PyExc_TypeError,
- "has no _stginfo_");
- Py_DECREF(self);
- return NULL;
- }
- if (bitsize /* this is a bitfield request */
- && *pfield_size /* we have a bitfield open */
+ self = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type,
+ NULL);
+ if (self == NULL)
+ return NULL;
+ dict = PyType_stgdict(desc);
+ if (!dict) {
+ PyErr_SetString(PyExc_TypeError,
+ "has no _stginfo_");
+ Py_DECREF(self);
+ return NULL;
+ }
+ if (bitsize /* this is a bitfield request */
+ && *pfield_size /* we have a bitfield open */
#ifdef MS_WIN32
- /* MSVC, GCC with -mms-bitfields */
- && dict->size * 8 == *pfield_size
+ /* MSVC, GCC with -mms-bitfields */
+ && dict->size * 8 == *pfield_size
#else
- /* GCC */
- && dict->size * 8 <= *pfield_size
+ /* GCC */
+ && dict->size * 8 <= *pfield_size
#endif
- && (*pbitofs + bitsize) <= *pfield_size) {
- /* continue bit field */
- fieldtype = CONT_BITFIELD;
+ && (*pbitofs + bitsize) <= *pfield_size) {
+ /* continue bit field */
+ fieldtype = CONT_BITFIELD;
#ifndef MS_WIN32
- } else if (bitsize /* this is a bitfield request */
- && *pfield_size /* we have a bitfield open */
- && dict->size * 8 >= *pfield_size
- && (*pbitofs + bitsize) <= dict->size * 8) {
- /* expand bit field */
- fieldtype = EXPAND_BITFIELD;
+ } else if (bitsize /* this is a bitfield request */
+ && *pfield_size /* we have a bitfield open */
+ && dict->size * 8 >= *pfield_size
+ && (*pbitofs + bitsize) <= dict->size * 8) {
+ /* expand bit field */
+ fieldtype = EXPAND_BITFIELD;
#endif
- } else if (bitsize) {
- /* start new bitfield */
- fieldtype = NEW_BITFIELD;
- *pbitofs = 0;
- *pfield_size = dict->size * 8;
- } else {
- /* not a bit field */
- fieldtype = NO_BITFIELD;
- *pbitofs = 0;
- *pfield_size = 0;
- }
-
- size = dict->size;
- length = dict->length;
- proto = desc;
-
- /* Field descriptors for 'c_char * n' are be scpecial cased to
- return a Python string instead of an Array object instance...
- */
- if (PyCArrayTypeObject_Check(proto)) {
- StgDictObject *adict = PyType_stgdict(proto);
- StgDictObject *idict;
- if (adict && adict->proto) {
- idict = PyType_stgdict(adict->proto);
- if (!idict) {
- PyErr_SetString(PyExc_TypeError,
- "has no _stginfo_");
- Py_DECREF(self);
- return NULL;
- }
- if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
- struct fielddesc *fd = _ctypes_get_fielddesc("s");
- getfunc = fd->getfunc;
- setfunc = fd->setfunc;
- }
+ } else if (bitsize) {
+ /* start new bitfield */
+ fieldtype = NEW_BITFIELD;
+ *pbitofs = 0;
+ *pfield_size = dict->size * 8;
+ } else {
+ /* not a bit field */
+ fieldtype = NO_BITFIELD;
+ *pbitofs = 0;
+ *pfield_size = 0;
+ }
+
+ size = dict->size;
+ length = dict->length;
+ proto = desc;
+
+ /* Field descriptors for 'c_char * n' are be scpecial cased to
+ return a Python string instead of an Array object instance...
+ */
+ if (PyCArrayTypeObject_Check(proto)) {
+ StgDictObject *adict = PyType_stgdict(proto);
+ StgDictObject *idict;
+ if (adict && adict->proto) {
+ idict = PyType_stgdict(adict->proto);
+ if (!idict) {
+ PyErr_SetString(PyExc_TypeError,
+ "has no _stginfo_");
+ Py_DECREF(self);
+ return NULL;
+ }
+ if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
+ struct fielddesc *fd = _ctypes_get_fielddesc("s");
+ getfunc = fd->getfunc;
+ setfunc = fd->setfunc;
+ }
#ifdef CTYPES_UNICODE
- if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
- struct fielddesc *fd = _ctypes_get_fielddesc("U");
- getfunc = fd->getfunc;
- setfunc = fd->setfunc;
- }
+ if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
+ struct fielddesc *fd = _ctypes_get_fielddesc("U");
+ getfunc = fd->getfunc;
+ setfunc = fd->setfunc;
+ }
#endif
- }
- }
-
- self->setfunc = setfunc;
- self->getfunc = getfunc;
- self->index = index;
-
- Py_INCREF(proto);
- self->proto = proto;
-
- switch (fieldtype) {
- case NEW_BITFIELD:
- if (big_endian)
- self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
- else
- self->size = (bitsize << 16) + *pbitofs;
- *pbitofs = bitsize;
- /* fall through */
- case NO_BITFIELD:
- if (pack)
- align = min(pack, dict->align);
- else
- align = dict->align;
- if (align && *poffset % align) {
- Py_ssize_t delta = align - (*poffset % align);
- *psize += delta;
- *poffset += delta;
- }
-
- if (bitsize == 0)
- self->size = size;
- *psize += size;
-
- self->offset = *poffset;
- *poffset += size;
-
- *palign = align;
- break;
-
- case EXPAND_BITFIELD:
- *poffset += dict->size - *pfield_size/8;
- *psize += dict->size - *pfield_size/8;
-
- *pfield_size = dict->size * 8;
-
- if (big_endian)
- self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
- else
- self->size = (bitsize << 16) + *pbitofs;
-
- self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
- *pbitofs += bitsize;
- break;
-
- case CONT_BITFIELD:
- if (big_endian)
- self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
- else
- self->size = (bitsize << 16) + *pbitofs;
-
- self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
- *pbitofs += bitsize;
- break;
- }
-
- return (PyObject *)self;
+ }
+ }
+
+ self->setfunc = setfunc;
+ self->getfunc = getfunc;
+ self->index = index;
+
+ Py_INCREF(proto);
+ self->proto = proto;
+
+ switch (fieldtype) {
+ case NEW_BITFIELD:
+ if (big_endian)
+ self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
+ else
+ self->size = (bitsize << 16) + *pbitofs;
+ *pbitofs = bitsize;
+ /* fall through */
+ case NO_BITFIELD:
+ if (pack)
+ align = min(pack, dict->align);
+ else
+ align = dict->align;
+ if (align && *poffset % align) {
+ Py_ssize_t delta = align - (*poffset % align);
+ *psize += delta;
+ *poffset += delta;
+ }
+
+ if (bitsize == 0)
+ self->size = size;
+ *psize += size;
+
+ self->offset = *poffset;
+ *poffset += size;
+
+ *palign = align;
+ break;
+
+ case EXPAND_BITFIELD:
+ *poffset += dict->size - *pfield_size/8;
+ *psize += dict->size - *pfield_size/8;
+
+ *pfield_size = dict->size * 8;
+
+ if (big_endian)
+ self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
+ else
+ self->size = (bitsize << 16) + *pbitofs;
+
+ self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
+ *pbitofs += bitsize;
+ break;
+
+ case CONT_BITFIELD:
+ if (big_endian)
+ self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
+ else
+ self->size = (bitsize << 16) + *pbitofs;
+
+ self->offset = *poffset - size; /* poffset is already updated for the NEXT field */
+ *pbitofs += bitsize;
+ break;
+ }
+
+ return (PyObject *)self;
}
static int
PyCField_set(CFieldObject *self, PyObject *inst, PyObject *value)
{
- CDataObject *dst;
- char *ptr;
- assert(CDataObject_Check(inst));
- dst = (CDataObject *)inst;
- ptr = dst->b_ptr + self->offset;
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete attribute");
- return -1;
- }
- return PyCData_set(inst, self->proto, self->setfunc, value,
- self->index, self->size, ptr);
+ CDataObject *dst;
+ char *ptr;
+ assert(CDataObject_Check(inst));
+ dst = (CDataObject *)inst;
+ ptr = dst->b_ptr + self->offset;
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete attribute");
+ return -1;
+ }
+ return PyCData_set(inst, self->proto, self->setfunc, value,
+ self->index, self->size, ptr);
}
static PyObject *
PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type)
{
- CDataObject *src;
- if (inst == NULL) {
- Py_INCREF(self);
- return (PyObject *)self;
- }
- assert(CDataObject_Check(inst));
- src = (CDataObject *)inst;
- return PyCData_get(self->proto, self->getfunc, inst,
- self->index, self->size, src->b_ptr + self->offset);
+ CDataObject *src;
+ if (inst == NULL) {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
+ assert(CDataObject_Check(inst));
+ src = (CDataObject *)inst;
+ return PyCData_get(self->proto, self->getfunc, inst,
+ self->index, self->size, src->b_ptr + self->offset);
}
static PyObject *
PyCField_get_offset(PyObject *self, void *data)
{
- return PyLong_FromSsize_t(((CFieldObject *)self)->offset);
+ return PyLong_FromSsize_t(((CFieldObject *)self)->offset);
}
static PyObject *
PyCField_get_size(PyObject *self, void *data)
{
- return PyLong_FromSsize_t(((CFieldObject *)self)->size);
+ return PyLong_FromSsize_t(((CFieldObject *)self)->size);
}
static PyGetSetDef PyCField_getset[] = {
- { "offset", PyCField_get_offset, NULL, "offset in bytes of this field" },
- { "size", PyCField_get_size, NULL, "size in bytes of this field" },
- { NULL, NULL, NULL, NULL },
+ { "offset", PyCField_get_offset, NULL, "offset in bytes of this field" },
+ { "size", PyCField_get_size, NULL, "size in bytes of this field" },
+ { NULL, NULL, NULL, NULL },
};
static int
PyCField_traverse(CFieldObject *self, visitproc visit, void *arg)
{
- Py_VISIT(self->proto);
- return 0;
+ Py_VISIT(self->proto);
+ return 0;
}
static int
PyCField_clear(CFieldObject *self)
{
- Py_CLEAR(self->proto);
- return 0;
+ Py_CLEAR(self->proto);
+ return 0;
}
static void
PyCField_dealloc(PyObject *self)
{
- PyCField_clear((CFieldObject *)self);
- self->ob_type->tp_free((PyObject *)self);
+ PyCField_clear((CFieldObject *)self);
+ self->ob_type->tp_free((PyObject *)self);
}
static PyObject *
PyCField_repr(CFieldObject *self)
{
- PyObject *result;
- Py_ssize_t bits = self->size >> 16;
- Py_ssize_t size = self->size & 0xFFFF;
- const char *name;
+ PyObject *result;
+ Py_ssize_t bits = self->size >> 16;
+ Py_ssize_t size = self->size & 0xFFFF;
+ const char *name;
- name = ((PyTypeObject *)self->proto)->tp_name;
+ name = ((PyTypeObject *)self->proto)->tp_name;
- if (bits)
- result = PyUnicode_FromFormat(
- "<Field type=%s, ofs=%zd:%zd, bits=%zd>",
- name, self->offset, size, bits);
- else
- result = PyUnicode_FromFormat(
- "<Field type=%s, ofs=%zd, size=%zd>",
- name, self->offset, size);
- return result;
+ if (bits)
+ result = PyUnicode_FromFormat(
+ "<Field type=%s, ofs=%zd:%zd, bits=%zd>",
+ name, self->offset, size, bits);
+ else
+ result = PyUnicode_FromFormat(
+ "<Field type=%s, ofs=%zd, size=%zd>",
+ name, self->offset, size);
+ return result;
}
PyTypeObject PyCField_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_ctypes.CField", /* tp_name */
- sizeof(CFieldObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- PyCField_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)PyCField_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- "Structure/Union member", /* tp_doc */
- (traverseproc)PyCField_traverse, /* tp_traverse */
- (inquiry)PyCField_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- PyCField_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)PyCField_get, /* tp_descr_get */
- (descrsetfunc)PyCField_set, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyCField_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_ctypes.CField", /* tp_name */
+ sizeof(CFieldObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ PyCField_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)PyCField_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ "Structure/Union member", /* tp_doc */
+ (traverseproc)PyCField_traverse, /* tp_traverse */
+ (inquiry)PyCField_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ PyCField_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)PyCField_get, /* tp_descr_get */
+ (descrsetfunc)PyCField_set, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyCField_new, /* tp_new */
+ 0, /* tp_free */
};
-
+
/******************************************************************/
/*
Accessor functions
@@ -347,18 +347,18 @@ PyTypeObject PyCField_Type = {
static int
get_long(PyObject *v, long *p)
{
- long x;
+ long x;
- if (PyFloat_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "int expected instead of float");
- return -1;
- }
- x = PyLong_AsUnsignedLongMask(v);
- if (x == -1 && PyErr_Occurred())
- return -1;
- *p = x;
- return 0;
+ if (PyFloat_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "int expected instead of float");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLongMask(v);
+ if (x == -1 && PyErr_Occurred())
+ return -1;
+ *p = x;
+ return 0;
}
/* Same, but handling unsigned long */
@@ -366,18 +366,18 @@ get_long(PyObject *v, long *p)
static int
get_ulong(PyObject *v, unsigned long *p)
{
- unsigned long x;
+ unsigned long x;
- if (PyFloat_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "int expected instead of float");
- return -1;
- }
- x = PyLong_AsUnsignedLongMask(v);
- if (x == (unsigned long)-1 && PyErr_Occurred())
- return -1;
- *p = x;
- return 0;
+ if (PyFloat_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "int expected instead of float");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLongMask(v);
+ if (x == (unsigned long)-1 && PyErr_Occurred())
+ return -1;
+ *p = x;
+ return 0;
}
#ifdef HAVE_LONG_LONG
@@ -387,17 +387,17 @@ get_ulong(PyObject *v, unsigned long *p)
static int
get_longlong(PyObject *v, PY_LONG_LONG *p)
{
- PY_LONG_LONG x;
- if (PyFloat_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "int expected instead of float");
- return -1;
- }
- x = PyLong_AsUnsignedLongLongMask(v);
- if (x == -1 && PyErr_Occurred())
- return -1;
- *p = x;
- return 0;
+ PY_LONG_LONG x;
+ if (PyFloat_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "int expected instead of float");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLongLongMask(v);
+ if (x == -1 && PyErr_Occurred())
+ return -1;
+ *p = x;
+ return 0;
}
/* Same, but handling native unsigned long long. */
@@ -405,17 +405,17 @@ get_longlong(PyObject *v, PY_LONG_LONG *p)
static int
get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
{
- unsigned PY_LONG_LONG x;
- if (PyFloat_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "int expected instead of float");
- return -1;
- }
- x = PyLong_AsUnsignedLongLongMask(v);
- if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
- return -1;
- *p = x;
- return 0;
+ unsigned PY_LONG_LONG x;
+ if (PyFloat_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "int expected instead of float");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLongLongMask(v);
+ if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
+ return -1;
+ *p = x;
+ return 0;
}
#endif
@@ -438,49 +438,49 @@ get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
/* This macro CHANGES the first parameter IN PLACE. For proper sign handling,
we must first shift left, then right.
*/
-#define GET_BITFIELD(v, size) \
- if (NUM_BITS(size)) { \
- v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \
- v >>= (sizeof(v)*8 - NUM_BITS(size)); \
- }
+#define GET_BITFIELD(v, size) \
+ if (NUM_BITS(size)) { \
+ v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \
+ v >>= (sizeof(v)*8 - NUM_BITS(size)); \
+ }
/* This macro RETURNS the first parameter with the bit field CHANGED. */
-#define SET(x, v, size) \
- (NUM_BITS(size) ? \
- ( ( x & ~(BIT_MASK(size) << LOW_BIT(size)) ) | ( (v & BIT_MASK(size)) << LOW_BIT(size) ) ) \
- : v)
+#define SET(x, v, size) \
+ (NUM_BITS(size) ? \
+ ( ( x & ~(BIT_MASK(size) << LOW_BIT(size)) ) | ( (v & BIT_MASK(size)) << LOW_BIT(size) ) ) \
+ : v)
/* byte swapping macros */
-#define SWAP_2(v) \
- ( ( (v >> 8) & 0x00FF) | \
- ( (v << 8) & 0xFF00) )
+#define SWAP_2(v) \
+ ( ( (v >> 8) & 0x00FF) | \
+ ( (v << 8) & 0xFF00) )
-#define SWAP_4(v) \
- ( ( (v & 0x000000FF) << 24 ) | \
- ( (v & 0x0000FF00) << 8 ) | \
- ( (v & 0x00FF0000) >> 8 ) | \
- ( ((v >> 24) & 0xFF)) )
+#define SWAP_4(v) \
+ ( ( (v & 0x000000FF) << 24 ) | \
+ ( (v & 0x0000FF00) << 8 ) | \
+ ( (v & 0x00FF0000) >> 8 ) | \
+ ( ((v >> 24) & 0xFF)) )
#ifdef _MSC_VER
-#define SWAP_8(v) \
- ( ( (v & 0x00000000000000FFL) << 56 ) | \
- ( (v & 0x000000000000FF00L) << 40 ) | \
- ( (v & 0x0000000000FF0000L) << 24 ) | \
- ( (v & 0x00000000FF000000L) << 8 ) | \
- ( (v & 0x000000FF00000000L) >> 8 ) | \
- ( (v & 0x0000FF0000000000L) >> 24 ) | \
- ( (v & 0x00FF000000000000L) >> 40 ) | \
- ( ((v >> 56) & 0xFF)) )
+#define SWAP_8(v) \
+ ( ( (v & 0x00000000000000FFL) << 56 ) | \
+ ( (v & 0x000000000000FF00L) << 40 ) | \
+ ( (v & 0x0000000000FF0000L) << 24 ) | \
+ ( (v & 0x00000000FF000000L) << 8 ) | \
+ ( (v & 0x000000FF00000000L) >> 8 ) | \
+ ( (v & 0x0000FF0000000000L) >> 24 ) | \
+ ( (v & 0x00FF000000000000L) >> 40 ) | \
+ ( ((v >> 56) & 0xFF)) )
#else
-#define SWAP_8(v) \
- ( ( (v & 0x00000000000000FFLL) << 56 ) | \
- ( (v & 0x000000000000FF00LL) << 40 ) | \
- ( (v & 0x0000000000FF0000LL) << 24 ) | \
- ( (v & 0x00000000FF000000LL) << 8 ) | \
- ( (v & 0x000000FF00000000LL) >> 8 ) | \
- ( (v & 0x0000FF0000000000LL) >> 24 ) | \
- ( (v & 0x00FF000000000000LL) >> 40 ) | \
- ( ((v >> 56) & 0xFF)) )
+#define SWAP_8(v) \
+ ( ( (v & 0x00000000000000FFLL) << 56 ) | \
+ ( (v & 0x000000000000FF00LL) << 40 ) | \
+ ( (v & 0x0000000000FF0000LL) << 24 ) | \
+ ( (v & 0x00000000FF000000LL) << 8 ) | \
+ ( (v & 0x000000FF00000000LL) >> 8 ) | \
+ ( (v & 0x0000FF0000000000LL) >> 24 ) | \
+ ( (v & 0x00FF000000000000LL) >> 40 ) | \
+ ( ((v >> 56) & 0xFF)) )
#endif
#define SWAP_INT SWAP_4
@@ -517,184 +517,184 @@ get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
static PyObject *
b_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- if (get_long(value, &val) < 0)
- return NULL;
- *(signed char *)ptr = (signed char)SET(*(signed char *)ptr, (signed char)val, size);
- _RET(value);
+ long val;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ *(signed char *)ptr = (signed char)SET(*(signed char *)ptr, (signed char)val, size);
+ _RET(value);
}
static PyObject *
b_get(void *ptr, Py_ssize_t size)
{
- signed char val = *(signed char *)ptr;
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ signed char val = *(signed char *)ptr;
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
B_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- if (get_ulong(value, &val) < 0)
- return NULL;
- *(unsigned char *)ptr = (unsigned char)SET(*(unsigned char*)ptr,
- (unsigned short)val, size);
- _RET(value);
+ unsigned long val;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ *(unsigned char *)ptr = (unsigned char)SET(*(unsigned char*)ptr,
+ (unsigned short)val, size);
+ _RET(value);
}
static PyObject *
B_get(void *ptr, Py_ssize_t size)
{
- unsigned char val = *(unsigned char *)ptr;
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ unsigned char val = *(unsigned char *)ptr;
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
h_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- short x;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, (short)val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ long val;
+ short x;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (short)val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
h_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- short field;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_2(field);
- field = SET(field, (short)val, size);
- field = SWAP_2(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ long val;
+ short field;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_2(field);
+ field = SET(field, (short)val, size);
+ field = SWAP_2(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
h_get(void *ptr, Py_ssize_t size)
{
- short val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromLong((long)val);
+ short val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong((long)val);
}
static PyObject *
h_get_sw(void *ptr, Py_ssize_t size)
{
- short val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_2(val);
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ short val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_2(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
H_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned short x;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, (unsigned short)val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ unsigned long val;
+ unsigned short x;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (unsigned short)val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
H_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned short field;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_2(field);
- field = SET(field, (unsigned short)val, size);
- field = SWAP_2(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ unsigned long val;
+ unsigned short field;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_2(field);
+ field = SET(field, (unsigned short)val, size);
+ field = SWAP_2(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
H_get(void *ptr, Py_ssize_t size)
{
- unsigned short val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ unsigned short val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
H_get_sw(void *ptr, Py_ssize_t size)
{
- unsigned short val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_2(val);
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ unsigned short val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_2(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
i_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- int x;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, (int)val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ long val;
+ int x;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (int)val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
i_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- int field;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_INT(field);
- field = SET(field, (int)val, size);
- field = SWAP_INT(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ long val;
+ int field;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_INT(field);
+ field = SET(field, (int)val, size);
+ field = SWAP_INT(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
i_get(void *ptr, Py_ssize_t size)
{
- int val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ int val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
i_get_sw(void *ptr, Py_ssize_t size)
{
- int val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_INT(val);
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ int val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_INT(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
#ifdef MS_WIN32
@@ -702,22 +702,22 @@ i_get_sw(void *ptr, Py_ssize_t size)
static PyObject *
vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- switch (PyObject_IsTrue(value)) {
- case -1:
- return NULL;
- case 0:
- *(short int *)ptr = VARIANT_FALSE;
- _RET(value);
- default:
- *(short int *)ptr = VARIANT_TRUE;
- _RET(value);
- }
+ switch (PyObject_IsTrue(value)) {
+ case -1:
+ return NULL;
+ case 0:
+ *(short int *)ptr = VARIANT_FALSE;
+ _RET(value);
+ default:
+ *(short int *)ptr = VARIANT_TRUE;
+ _RET(value);
+ }
}
static PyObject *
vBOOL_get(void *ptr, Py_ssize_t size)
{
- return PyBool_FromLong((long)*(short int *)ptr);
+ return PyBool_FromLong((long)*(short int *)ptr);
}
#endif
@@ -732,260 +732,260 @@ vBOOL_get(void *ptr, Py_ssize_t size)
static PyObject *
bool_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- switch (PyObject_IsTrue(value)) {
- case -1:
- return NULL;
- case 0:
- *(BOOL_TYPE *)ptr = 0;
- _RET(value);
- default:
- *(BOOL_TYPE *)ptr = 1;
- _RET(value);
- }
+ switch (PyObject_IsTrue(value)) {
+ case -1:
+ return NULL;
+ case 0:
+ *(BOOL_TYPE *)ptr = 0;
+ _RET(value);
+ default:
+ *(BOOL_TYPE *)ptr = 1;
+ _RET(value);
+ }
}
static PyObject *
bool_get(void *ptr, Py_ssize_t size)
{
- return PyBool_FromLong((long)*(BOOL_TYPE *)ptr);
+ return PyBool_FromLong((long)*(BOOL_TYPE *)ptr);
}
static PyObject *
I_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned int x;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, (unsigned int)val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ unsigned long val;
+ unsigned int x;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (unsigned int)val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
I_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned int field;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = (unsigned int)SET(field, (unsigned int)val, size);
- field = SWAP_INT(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ unsigned long val;
+ unsigned int field;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = (unsigned int)SET(field, (unsigned int)val, size);
+ field = SWAP_INT(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
I_get(void *ptr, Py_ssize_t size)
{
- unsigned int val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLong(val);
+ unsigned int val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLong(val);
}
static PyObject *
I_get_sw(void *ptr, Py_ssize_t size)
{
- unsigned int val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_INT(val);
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLong(val);
+ unsigned int val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_INT(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLong(val);
}
static PyObject *
l_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- long x;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ long val;
+ long x;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
l_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- long val;
- long field;
- if (get_long(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_LONG(field);
- field = (long)SET(field, val, size);
- field = SWAP_LONG(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ long val;
+ long field;
+ if (get_long(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_LONG(field);
+ field = (long)SET(field, val, size);
+ field = SWAP_LONG(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
l_get(void *ptr, Py_ssize_t size)
{
- long val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ long val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
l_get_sw(void *ptr, Py_ssize_t size)
{
- long val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_LONG(val);
- GET_BITFIELD(val, size);
- return PyLong_FromLong(val);
+ long val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_LONG(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromLong(val);
}
static PyObject *
L_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned long x;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ unsigned long val;
+ unsigned long x;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
L_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned long val;
- unsigned long field;
- if (get_ulong(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_LONG(field);
- field = (unsigned long)SET(field, val, size);
- field = SWAP_LONG(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ unsigned long val;
+ unsigned long field;
+ if (get_ulong(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_LONG(field);
+ field = (unsigned long)SET(field, val, size);
+ field = SWAP_LONG(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
L_get(void *ptr, Py_ssize_t size)
{
- unsigned long val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLong(val);
+ unsigned long val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLong(val);
}
static PyObject *
L_get_sw(void *ptr, Py_ssize_t size)
{
- unsigned long val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_LONG(val);
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLong(val);
+ unsigned long val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_LONG(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLong(val);
}
#ifdef HAVE_LONG_LONG
static PyObject *
q_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- PY_LONG_LONG val;
- PY_LONG_LONG x;
- if (get_longlong(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ PY_LONG_LONG val;
+ PY_LONG_LONG x;
+ if (get_longlong(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
q_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- PY_LONG_LONG val;
- PY_LONG_LONG field;
- if (get_longlong(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_8(field);
- field = (PY_LONG_LONG)SET(field, val, size);
- field = SWAP_8(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ PY_LONG_LONG val;
+ PY_LONG_LONG field;
+ if (get_longlong(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_8(field);
+ field = (PY_LONG_LONG)SET(field, val, size);
+ field = SWAP_8(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
q_get(void *ptr, Py_ssize_t size)
{
- PY_LONG_LONG val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromLongLong(val);
+ PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromLongLong(val);
}
static PyObject *
q_get_sw(void *ptr, Py_ssize_t size)
{
- PY_LONG_LONG val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_8(val);
- GET_BITFIELD(val, size);
- return PyLong_FromLongLong(val);
+ PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_8(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromLongLong(val);
}
static PyObject *
Q_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned PY_LONG_LONG val;
- unsigned PY_LONG_LONG x;
- if (get_ulonglong(value, &val) < 0)
- return NULL;
- memcpy(&x, ptr, sizeof(x));
- x = SET(x, val, size);
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ unsigned PY_LONG_LONG val;
+ unsigned PY_LONG_LONG x;
+ if (get_ulonglong(value, &val) < 0)
+ return NULL;
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
Q_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- unsigned PY_LONG_LONG val;
- unsigned PY_LONG_LONG field;
- if (get_ulonglong(value, &val) < 0)
- return NULL;
- memcpy(&field, ptr, sizeof(field));
- field = SWAP_8(field);
- field = (unsigned PY_LONG_LONG)SET(field, val, size);
- field = SWAP_8(field);
- memcpy(ptr, &field, sizeof(field));
- _RET(value);
+ unsigned PY_LONG_LONG val;
+ unsigned PY_LONG_LONG field;
+ if (get_ulonglong(value, &val) < 0)
+ return NULL;
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_8(field);
+ field = (unsigned PY_LONG_LONG)SET(field, val, size);
+ field = SWAP_8(field);
+ memcpy(ptr, &field, sizeof(field));
+ _RET(value);
}
static PyObject *
Q_get(void *ptr, Py_ssize_t size)
{
- unsigned PY_LONG_LONG val;
- memcpy(&val, ptr, sizeof(val));
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLongLong(val);
+ unsigned PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLongLong(val);
}
static PyObject *
Q_get_sw(void *ptr, Py_ssize_t size)
{
- unsigned PY_LONG_LONG val;
- memcpy(&val, ptr, sizeof(val));
- val = SWAP_8(val);
- GET_BITFIELD(val, size);
- return PyLong_FromUnsignedLongLong(val);
+ unsigned PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
+ val = SWAP_8(val);
+ GET_BITFIELD(val, size);
+ return PyLong_FromUnsignedLongLong(val);
}
#endif
@@ -997,136 +997,136 @@ Q_get_sw(void *ptr, Py_ssize_t size)
static PyObject *
g_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- long double x;
+ long double x;
- x = PyFloat_AsDouble(value);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_Format(PyExc_TypeError,
- " float expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
- memcpy(ptr, &x, sizeof(long double));
- _RET(value);
+ x = PyFloat_AsDouble(value);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ " float expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
+ memcpy(ptr, &x, sizeof(long double));
+ _RET(value);
}
static PyObject *
g_get(void *ptr, Py_ssize_t size)
{
- long double val;
- memcpy(&val, ptr, sizeof(long double));
- return PyFloat_FromDouble(val);
+ long double val;
+ memcpy(&val, ptr, sizeof(long double));
+ return PyFloat_FromDouble(val);
}
static PyObject *
d_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- double x;
+ double x;
- x = PyFloat_AsDouble(value);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_Format(PyExc_TypeError,
- " float expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
- memcpy(ptr, &x, sizeof(double));
- _RET(value);
+ x = PyFloat_AsDouble(value);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ " float expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
+ memcpy(ptr, &x, sizeof(double));
+ _RET(value);
}
static PyObject *
d_get(void *ptr, Py_ssize_t size)
{
- double val;
- memcpy(&val, ptr, sizeof(val));
- return PyFloat_FromDouble(val);
+ double val;
+ memcpy(&val, ptr, sizeof(val));
+ return PyFloat_FromDouble(val);
}
static PyObject *
d_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- double x;
+ double x;
- x = PyFloat_AsDouble(value);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_Format(PyExc_TypeError,
- " float expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
+ x = PyFloat_AsDouble(value);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ " float expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
#ifdef WORDS_BIGENDIAN
- if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
- return NULL;
+ if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
+ return NULL;
#else
- if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0))
- return NULL;
+ if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0))
+ return NULL;
#endif
- _RET(value);
+ _RET(value);
}
static PyObject *
d_get_sw(void *ptr, Py_ssize_t size)
{
#ifdef WORDS_BIGENDIAN
- return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1));
+ return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1));
#else
- return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0));
+ return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0));
#endif
}
static PyObject *
f_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- float x;
+ float x;
- x = (float)PyFloat_AsDouble(value);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_Format(PyExc_TypeError,
- " float expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
- memcpy(ptr, &x, sizeof(x));
- _RET(value);
+ x = (float)PyFloat_AsDouble(value);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ " float expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
+ memcpy(ptr, &x, sizeof(x));
+ _RET(value);
}
static PyObject *
f_get(void *ptr, Py_ssize_t size)
{
- float val;
- memcpy(&val, ptr, sizeof(val));
- return PyFloat_FromDouble(val);
+ float val;
+ memcpy(&val, ptr, sizeof(val));
+ return PyFloat_FromDouble(val);
}
static PyObject *
f_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
- float x;
+ float x;
- x = (float)PyFloat_AsDouble(value);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_Format(PyExc_TypeError,
- " float expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
+ x = (float)PyFloat_AsDouble(value);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_Format(PyExc_TypeError,
+ " float expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
#ifdef WORDS_BIGENDIAN
- if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
- return NULL;
+ if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
+ return NULL;
#else
- if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0))
- return NULL;
+ if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0))
+ return NULL;
#endif
- _RET(value);
+ _RET(value);
}
static PyObject *
f_get_sw(void *ptr, Py_ssize_t size)
{
#ifdef WORDS_BIGENDIAN
- return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1));
+ return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1));
#else
- return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0));
+ return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0));
#endif
}
@@ -1143,72 +1143,72 @@ f_get_sw(void *ptr, Py_ssize_t size)
static PyObject *
O_get(void *ptr, Py_ssize_t size)
{
- PyObject *ob = *(PyObject **)ptr;
- if (ob == NULL) {
- if (!PyErr_Occurred())
- /* Set an error if not yet set */
- PyErr_SetString(PyExc_ValueError,
- "PyObject is NULL");
- return NULL;
- }
- Py_INCREF(ob);
- return ob;
+ PyObject *ob = *(PyObject **)ptr;
+ if (ob == NULL) {
+ if (!PyErr_Occurred())
+ /* Set an error if not yet set */
+ PyErr_SetString(PyExc_ValueError,
+ "PyObject is NULL");
+ return NULL;
+ }
+ Py_INCREF(ob);
+ return ob;
}
static PyObject *
O_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- /* Hm, does the memory block need it's own refcount or not? */
- *(PyObject **)ptr = value;
- Py_INCREF(value);
- return value;
+ /* Hm, does the memory block need it's own refcount or not? */
+ *(PyObject **)ptr = value;
+ Py_INCREF(value);
+ return value;
}
static PyObject *
c_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- if (PyUnicode_Check(value)) {
- value = PyUnicode_AsEncodedString(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (value == NULL)
- return NULL;
- if (PyBytes_GET_SIZE(value) != 1) {
- Py_DECREF(value);
- goto error;
- }
- *(char *)ptr = PyBytes_AS_STRING(value)[0];
- Py_DECREF(value);
- _RET(value);
- }
- if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
- *(char *)ptr = PyBytes_AS_STRING(value)[0];
- _RET(value);
- }
- if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) {
- *(char *)ptr = PyByteArray_AS_STRING(value)[0];
- _RET(value);
- }
- if (PyLong_Check(value))
- {
- long longval = PyLong_AS_LONG(value);
- if (longval < 0 || longval >= 256)
- goto error;
- *(char *)ptr = (char)longval;
- _RET(value);
- }
+ if (PyUnicode_Check(value)) {
+ value = PyUnicode_AsEncodedString(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (value == NULL)
+ return NULL;
+ if (PyBytes_GET_SIZE(value) != 1) {
+ Py_DECREF(value);
+ goto error;
+ }
+ *(char *)ptr = PyBytes_AS_STRING(value)[0];
+ Py_DECREF(value);
+ _RET(value);
+ }
+ if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
+ *(char *)ptr = PyBytes_AS_STRING(value)[0];
+ _RET(value);
+ }
+ if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) {
+ *(char *)ptr = PyByteArray_AS_STRING(value)[0];
+ _RET(value);
+ }
+ if (PyLong_Check(value))
+ {
+ long longval = PyLong_AS_LONG(value);
+ if (longval < 0 || longval >= 256)
+ goto error;
+ *(char *)ptr = (char)longval;
+ _RET(value);
+ }
error:
- PyErr_Format(PyExc_TypeError,
- "one character string expected");
- return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "one character string expected");
+ return NULL;
}
static PyObject *
c_get(void *ptr, Py_ssize_t size)
{
- return PyBytes_FromStringAndSize((char *)ptr, 1);
+ return PyBytes_FromStringAndSize((char *)ptr, 1);
}
#ifdef CTYPES_UNICODE
@@ -1216,107 +1216,107 @@ c_get(void *ptr, Py_ssize_t size)
static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- Py_ssize_t len;
- if (PyBytes_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return NULL;
- } else if (!PyUnicode_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "unicode string expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- } else
- Py_INCREF(value);
+ Py_ssize_t len;
+ if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return NULL;
+ } else if (!PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "unicode string expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ } else
+ Py_INCREF(value);
- len = PyUnicode_GET_SIZE(value);
- if (len != 1) {
- Py_DECREF(value);
- PyErr_SetString(PyExc_TypeError,
- "one character unicode string expected");
- return NULL;
- }
+ len = PyUnicode_GET_SIZE(value);
+ if (len != 1) {
+ Py_DECREF(value);
+ PyErr_SetString(PyExc_TypeError,
+ "one character unicode string expected");
+ return NULL;
+ }
- *(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0];
- Py_DECREF(value);
+ *(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0];
+ Py_DECREF(value);
- _RET(value);
+ _RET(value);
}
static PyObject *
u_get(void *ptr, Py_ssize_t size)
{
- return PyUnicode_FromWideChar((wchar_t *)ptr, 1);
+ return PyUnicode_FromWideChar((wchar_t *)ptr, 1);
}
/* U - a unicode string */
static PyObject *
U_get(void *ptr, Py_ssize_t size)
{
- PyObject *result;
- Py_ssize_t len;
- Py_UNICODE *p;
+ PyObject *result;
+ Py_ssize_t len;
+ Py_UNICODE *p;
- size /= sizeof(wchar_t); /* we count character units here, not bytes */
+ size /= sizeof(wchar_t); /* we count character units here, not bytes */
- result = PyUnicode_FromWideChar((wchar_t *)ptr, size);
- if (!result)
- return NULL;
- /* We need 'result' to be able to count the characters with wcslen,
- since ptr may not be NUL terminated. If the length is smaller (if
- it was actually NUL terminated, we construct a new one and throw
- away the result.
- */
- /* chop off at the first NUL character, if any. */
- p = PyUnicode_AS_UNICODE(result);
- for (len = 0; len < size; ++len)
- if (!p[len])
- break;
+ result = PyUnicode_FromWideChar((wchar_t *)ptr, size);
+ if (!result)
+ return NULL;
+ /* We need 'result' to be able to count the characters with wcslen,
+ since ptr may not be NUL terminated. If the length is smaller (if
+ it was actually NUL terminated, we construct a new one and throw
+ away the result.
+ */
+ /* chop off at the first NUL character, if any. */
+ p = PyUnicode_AS_UNICODE(result);
+ for (len = 0; len < size; ++len)
+ if (!p[len])
+ break;
- if (len < size) {
- PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len);
- Py_DECREF(result);
- return ob;
- }
- return result;
+ if (len < size) {
+ PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len);
+ Py_DECREF(result);
+ return ob;
+ }
+ return result;
}
static PyObject *
U_set(void *ptr, PyObject *value, Py_ssize_t length)
{
- Py_ssize_t size;
-
- /* It's easier to calculate in characters than in bytes */
- length /= sizeof(wchar_t);
-
- if (PyBytes_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return NULL;
- } else if (!PyUnicode_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "unicode string expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- } else
- Py_INCREF(value);
- size = PyUnicode_GET_SIZE(value);
- if (size > length) {
- PyErr_Format(PyExc_ValueError,
- "string too long (%zd, maximum length %zd)",
- size, length);
- Py_DECREF(value);
- return NULL;
- } else if (size < length-1)
- /* copy terminating NUL character if there is space */
- size += 1;
- PyUnicode_AsWideChar((PyUnicodeObject *)value, (wchar_t *)ptr, size);
- return value;
+ Py_ssize_t size;
+
+ /* It's easier to calculate in characters than in bytes */
+ length /= sizeof(wchar_t);
+
+ if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return NULL;
+ } else if (!PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "unicode string expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ } else
+ Py_INCREF(value);
+ size = PyUnicode_GET_SIZE(value);
+ if (size > length) {
+ PyErr_Format(PyExc_ValueError,
+ "string too long (%zd, maximum length %zd)",
+ size, length);
+ Py_DECREF(value);
+ return NULL;
+ } else if (size < length-1)
+ /* copy terminating NUL character if there is space */
+ size += 1;
+ PyUnicode_AsWideChar((PyUnicodeObject *)value, (wchar_t *)ptr, size);
+ return value;
}
#endif
@@ -1324,210 +1324,210 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
static PyObject *
s_get(void *ptr, Py_ssize_t size)
{
- Py_ssize_t i;
- char *p;
+ Py_ssize_t i;
+ char *p;
- p = (char *)ptr;
- for (i = 0; i < size; ++i) {
- if (*p++ == '\0')
- break;
- }
+ p = (char *)ptr;
+ for (i = 0; i < size; ++i) {
+ if (*p++ == '\0')
+ break;
+ }
- return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
+ return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
}
static PyObject *
s_set(void *ptr, PyObject *value, Py_ssize_t length)
{
- char *data;
- Py_ssize_t size;
-
- if (PyUnicode_Check(value)) {
- value = PyUnicode_AsEncodedString(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (value == NULL)
- return NULL;
- assert(PyBytes_Check(value));
- } else if(PyBytes_Check(value)) {
- Py_INCREF(value);
- } else {
- PyErr_Format(PyExc_TypeError,
- "expected string, %s found",
- value->ob_type->tp_name);
- return NULL;
- }
-
- data = PyBytes_AS_STRING(value);
- if (!data)
- return NULL;
- size = strlen(data); /* XXX Why not Py_SIZE(value)? */
- if (size < length) {
- /* This will copy the leading NUL character
- * if there is space for it.
- */
- ++size;
- } else if (size > length) {
- PyErr_Format(PyExc_ValueError,
- "string too long (%zd, maximum length %zd)",
- size, length);
- Py_DECREF(value);
- return NULL;
- }
- /* Also copy the terminating NUL character if there is space */
- memcpy((char *)ptr, data, size);
-
- Py_DECREF(value);
- _RET(value);
+ char *data;
+ Py_ssize_t size;
+
+ if (PyUnicode_Check(value)) {
+ value = PyUnicode_AsEncodedString(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (value == NULL)
+ return NULL;
+ assert(PyBytes_Check(value));
+ } else if(PyBytes_Check(value)) {
+ Py_INCREF(value);
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "expected string, %s found",
+ value->ob_type->tp_name);
+ return NULL;
+ }
+
+ data = PyBytes_AS_STRING(value);
+ if (!data)
+ return NULL;
+ size = strlen(data); /* XXX Why not Py_SIZE(value)? */
+ if (size < length) {
+ /* This will copy the leading NUL character
+ * if there is space for it.
+ */
+ ++size;
+ } else if (size > length) {
+ PyErr_Format(PyExc_ValueError,
+ "string too long (%zd, maximum length %zd)",
+ size, length);
+ Py_DECREF(value);
+ return NULL;
+ }
+ /* Also copy the terminating NUL character if there is space */
+ memcpy((char *)ptr, data, size);
+
+ Py_DECREF(value);
+ _RET(value);
}
static PyObject *
z_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- if (value == Py_None) {
- *(char **)ptr = NULL;
- Py_INCREF(value);
- return value;
- }
- if (PyBytes_Check(value)) {
- *(char **)ptr = PyBytes_AsString(value);
- Py_INCREF(value);
- return value;
- } else if (PyUnicode_Check(value)) {
- PyObject *str = PyUnicode_AsEncodedString(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (str == NULL)
- return NULL;
- *(char **)ptr = PyBytes_AS_STRING(str);
- return str;
- } else if (PyLong_Check(value)) {
+ if (value == Py_None) {
+ *(char **)ptr = NULL;
+ Py_INCREF(value);
+ return value;
+ }
+ if (PyBytes_Check(value)) {
+ *(char **)ptr = PyBytes_AsString(value);
+ Py_INCREF(value);
+ return value;
+ } else if (PyUnicode_Check(value)) {
+ PyObject *str = PyUnicode_AsEncodedString(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (str == NULL)
+ return NULL;
+ *(char **)ptr = PyBytes_AS_STRING(str);
+ return str;
+ } else if (PyLong_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
- *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value);
+ *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value);
#else
- *(char **)ptr = (char *)PyLong_AsUnsignedLongMask(value);
+ *(char **)ptr = (char *)PyLong_AsUnsignedLongMask(value);
#endif
- _RET(value);
- }
- PyErr_Format(PyExc_TypeError,
- "string or integer address expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
+ _RET(value);
+ }
+ PyErr_Format(PyExc_TypeError,
+ "string or integer address expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
}
static PyObject *
z_get(void *ptr, Py_ssize_t size)
{
- /* XXX What about invalid pointers ??? */
- if (*(void **)ptr) {
+ /* XXX What about invalid pointers ??? */
+ if (*(void **)ptr) {
#if defined(MS_WIN32) && !defined(_WIN32_WCE)
- if (IsBadStringPtrA(*(char **)ptr, -1)) {
- PyErr_Format(PyExc_ValueError,
- "invalid string pointer %p",
- *(char **)ptr);
- return NULL;
- }
+ if (IsBadStringPtrA(*(char **)ptr, -1)) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid string pointer %p",
+ *(char **)ptr);
+ return NULL;
+ }
#endif
- return PyBytes_FromStringAndSize(*(char **)ptr,
- strlen(*(char **)ptr));
- } else {
- Py_INCREF(Py_None);
- return Py_None;
- }
+ return PyBytes_FromStringAndSize(*(char **)ptr,
+ strlen(*(char **)ptr));
+ } else {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
#ifdef CTYPES_UNICODE
static PyObject *
Z_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- if (value == Py_None) {
- *(wchar_t **)ptr = NULL;
- Py_INCREF(value);
- return value;
- }
- if (PyLong_Check(value) || PyLong_Check(value)) {
+ if (value == Py_None) {
+ *(wchar_t **)ptr = NULL;
+ Py_INCREF(value);
+ return value;
+ }
+ if (PyLong_Check(value) || PyLong_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
- *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongLongMask(value);
+ *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongLongMask(value);
#else
- *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value);
+ *(wchar_t **)ptr = (wchar_t *)PyLong_AsUnsignedLongMask(value);
#endif
- Py_INCREF(Py_None);
- return Py_None;
- }
- if (PyBytes_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return NULL;
- } else if (!PyUnicode_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "unicode string or integer address expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- } else
- Py_INCREF(value);
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return NULL;
+ } else if (!PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "unicode string or integer address expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ } else
+ Py_INCREF(value);
#ifdef HAVE_USABLE_WCHAR_T
- /* HAVE_USABLE_WCHAR_T means that Py_UNICODE and wchar_t is the same
- type. So we can copy directly. Hm, are unicode objects always NUL
- terminated in Python, internally?
- */
- *(wchar_t **)ptr = PyUnicode_AS_UNICODE(value);
- return value;
+ /* HAVE_USABLE_WCHAR_T means that Py_UNICODE and wchar_t is the same
+ type. So we can copy directly. Hm, are unicode objects always NUL
+ terminated in Python, internally?
+ */
+ *(wchar_t **)ptr = PyUnicode_AS_UNICODE(value);
+ return value;
#else
- {
- /* We must create a wchar_t* buffer from the unicode object,
- and keep it alive */
- PyObject *keep;
- wchar_t *buffer;
-
- int size = PyUnicode_GET_SIZE(value);
- size += 1; /* terminating NUL */
- size *= sizeof(wchar_t);
- buffer = (wchar_t *)PyMem_Malloc(size);
- if (!buffer) {
- Py_DECREF(value);
- return PyErr_NoMemory();
- }
- memset(buffer, 0, size);
- keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
- if (!keep) {
- Py_DECREF(value);
- PyMem_Free(buffer);
- return NULL;
- }
- *(wchar_t **)ptr = (wchar_t *)buffer;
- if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value,
- buffer, PyUnicode_GET_SIZE(value))) {
- Py_DECREF(value);
- Py_DECREF(keep);
- return NULL;
- }
- Py_DECREF(value);
- return keep;
- }
+ {
+ /* We must create a wchar_t* buffer from the unicode object,
+ and keep it alive */
+ PyObject *keep;
+ wchar_t *buffer;
+
+ int size = PyUnicode_GET_SIZE(value);
+ size += 1; /* terminating NUL */
+ size *= sizeof(wchar_t);
+ buffer = (wchar_t *)PyMem_Malloc(size);
+ if (!buffer) {
+ Py_DECREF(value);
+ return PyErr_NoMemory();
+ }
+ memset(buffer, 0, size);
+ keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
+ if (!keep) {
+ Py_DECREF(value);
+ PyMem_Free(buffer);
+ return NULL;
+ }
+ *(wchar_t **)ptr = (wchar_t *)buffer;
+ if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value,
+ buffer, PyUnicode_GET_SIZE(value))) {
+ Py_DECREF(value);
+ Py_DECREF(keep);
+ return NULL;
+ }
+ Py_DECREF(value);
+ return keep;
+ }
#endif
}
static PyObject *
Z_get(void *ptr, Py_ssize_t size)
{
- wchar_t *p;
- p = *(wchar_t **)ptr;
- if (p) {
+ wchar_t *p;
+ p = *(wchar_t **)ptr;
+ if (p) {
#if defined(MS_WIN32) && !defined(_WIN32_WCE)
- if (IsBadStringPtrW(*(wchar_t **)ptr, -1)) {
- PyErr_Format(PyExc_ValueError,
- "invalid string pointer %p",
- *(wchar_t **)ptr);
- return NULL;
- }
+ if (IsBadStringPtrW(*(wchar_t **)ptr, -1)) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid string pointer %p",
+ *(wchar_t **)ptr);
+ return NULL;
+ }
#endif
- return PyUnicode_FromWideChar(p, wcslen(p));
- } else {
- Py_INCREF(Py_None);
- return Py_None;
- }
+ return PyUnicode_FromWideChar(p, wcslen(p));
+ } else {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
#endif
@@ -1535,166 +1535,166 @@ Z_get(void *ptr, Py_ssize_t size)
static PyObject *
BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- BSTR bstr;
-
- /* convert value into a PyUnicodeObject or NULL */
- if (Py_None == value) {
- value = NULL;
- } else if (PyBytes_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- _ctypes_conversion_encoding,
- _ctypes_conversion_errors);
- if (!value)
- return NULL;
- } else if (PyUnicode_Check(value)) {
- Py_INCREF(value); /* for the descref below */
- } else {
- PyErr_Format(PyExc_TypeError,
- "unicode string expected instead of %s instance",
- value->ob_type->tp_name);
- return NULL;
- }
-
- /* create a BSTR from value */
- if (value) {
- Py_ssize_t size = PyUnicode_GET_SIZE(value);
- if ((unsigned) size != size) {
- PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
- return NULL;
- }
- bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value),
- (unsigned)size);
- Py_DECREF(value);
- } else
- bstr = NULL;
-
- /* free the previous contents, if any */
- if (*(BSTR *)ptr)
- SysFreeString(*(BSTR *)ptr);
-
- /* and store it */
- *(BSTR *)ptr = bstr;
-
- /* We don't need to keep any other object */
- _RET(value);
+ BSTR bstr;
+
+ /* convert value into a PyUnicodeObject or NULL */
+ if (Py_None == value) {
+ value = NULL;
+ } else if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ _ctypes_conversion_encoding,
+ _ctypes_conversion_errors);
+ if (!value)
+ return NULL;
+ } else if (PyUnicode_Check(value)) {
+ Py_INCREF(value); /* for the descref below */
+ } else {
+ PyErr_Format(PyExc_TypeError,
+ "unicode string expected instead of %s instance",
+ value->ob_type->tp_name);
+ return NULL;
+ }
+
+ /* create a BSTR from value */
+ if (value) {
+ Py_ssize_t size = PyUnicode_GET_SIZE(value);
+ if ((unsigned) size != size) {
+ PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
+ return NULL;
+ }
+ bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value),
+ (unsigned)size);
+ Py_DECREF(value);
+ } else
+ bstr = NULL;
+
+ /* free the previous contents, if any */
+ if (*(BSTR *)ptr)
+ SysFreeString(*(BSTR *)ptr);
+
+ /* and store it */
+ *(BSTR *)ptr = bstr;
+
+ /* We don't need to keep any other object */
+ _RET(value);
}
static PyObject *
BSTR_get(void *ptr, Py_ssize_t size)
{
- BSTR p;
- p = *(BSTR *)ptr;
- if (p)
- return PyUnicode_FromWideChar(p, SysStringLen(p));
- else {
- /* Hm, it seems NULL pointer and zero length string are the
- same in BSTR, see Don Box, p 81
- */
- Py_INCREF(Py_None);
- return Py_None;
- }
+ BSTR p;
+ p = *(BSTR *)ptr;
+ if (p)
+ return PyUnicode_FromWideChar(p, SysStringLen(p));
+ else {
+ /* Hm, it seems NULL pointer and zero length string are the
+ same in BSTR, see Don Box, p 81
+ */
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
#endif
static PyObject *
P_set(void *ptr, PyObject *value, Py_ssize_t size)
{
- void *v;
- if (value == Py_None) {
- *(void **)ptr = NULL;
- _RET(value);
- }
+ void *v;
+ if (value == Py_None) {
+ *(void **)ptr = NULL;
+ _RET(value);
+ }
- if (!PyLong_Check(value) && !PyLong_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "cannot be converted to pointer");
- return NULL;
- }
+ if (!PyLong_Check(value) && !PyLong_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "cannot be converted to pointer");
+ return NULL;
+ }
#if SIZEOF_VOID_P <= SIZEOF_LONG
- v = (void *)PyLong_AsUnsignedLongMask(value);
+ v = (void *)PyLong_AsUnsignedLongMask(value);
#else
#ifndef HAVE_LONG_LONG
# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#elif SIZEOF_LONG_LONG < SIZEOF_VOID_P
# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
- v = (void *)PyLong_AsUnsignedLongLongMask(value);
+ v = (void *)PyLong_AsUnsignedLongLongMask(value);
#endif
- if (PyErr_Occurred())
- return NULL;
+ if (PyErr_Occurred())
+ return NULL;
- *(void **)ptr = v;
- _RET(value);
+ *(void **)ptr = v;
+ _RET(value);
}
static PyObject *
P_get(void *ptr, Py_ssize_t size)
{
- if (*(void **)ptr == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyLong_FromVoidPtr(*(void **)ptr);
+ if (*(void **)ptr == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyLong_FromVoidPtr(*(void **)ptr);
}
static struct fielddesc formattable[] = {
- { 's', s_set, s_get, &ffi_type_pointer},
- { 'b', b_set, b_get, &ffi_type_schar},
- { 'B', B_set, B_get, &ffi_type_uchar},
- { 'c', c_set, c_get, &ffi_type_schar},
- { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw},
- { 'g', g_set, g_get, &ffi_type_longdouble},
- { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw},
- { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw},
- { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw},
- { 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw},
- { 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw},
+ { 's', s_set, s_get, &ffi_type_pointer},
+ { 'b', b_set, b_get, &ffi_type_schar},
+ { 'B', B_set, B_get, &ffi_type_uchar},
+ { 'c', c_set, c_get, &ffi_type_schar},
+ { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw},
+ { 'g', g_set, g_get, &ffi_type_longdouble},
+ { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw},
+ { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw},
+ { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw},
+ { 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw},
+ { 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw},
/* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */
/* As soon as we can get rid of the type codes, this is no longer a problem */
#if SIZEOF_LONG == 4
- { 'l', l_set, l_get, &ffi_type_sint32, l_set_sw, l_get_sw},
- { 'L', L_set, L_get, &ffi_type_uint32, L_set_sw, L_get_sw},
+ { 'l', l_set, l_get, &ffi_type_sint32, l_set_sw, l_get_sw},
+ { 'L', L_set, L_get, &ffi_type_uint32, L_set_sw, L_get_sw},
#elif SIZEOF_LONG == 8
- { 'l', l_set, l_get, &ffi_type_sint64, l_set_sw, l_get_sw},
- { 'L', L_set, L_get, &ffi_type_uint64, L_set_sw, L_get_sw},
+ { 'l', l_set, l_get, &ffi_type_sint64, l_set_sw, l_get_sw},
+ { 'L', L_set, L_get, &ffi_type_uint64, L_set_sw, L_get_sw},
#else
# error
#endif
#ifdef HAVE_LONG_LONG
#if SIZEOF_LONG_LONG == 8
- { 'q', q_set, q_get, &ffi_type_sint64, q_set_sw, q_get_sw},
- { 'Q', Q_set, Q_get, &ffi_type_uint64, Q_set_sw, Q_get_sw},
+ { 'q', q_set, q_get, &ffi_type_sint64, q_set_sw, q_get_sw},
+ { 'Q', Q_set, Q_get, &ffi_type_uint64, Q_set_sw, Q_get_sw},
#else
# error
#endif
#endif
- { 'P', P_set, P_get, &ffi_type_pointer},
- { 'z', z_set, z_get, &ffi_type_pointer},
+ { 'P', P_set, P_get, &ffi_type_pointer},
+ { 'z', z_set, z_get, &ffi_type_pointer},
#ifdef CTYPES_UNICODE
- { 'u', u_set, u_get, NULL}, /* ffi_type set later */
- { 'U', U_set, U_get, &ffi_type_pointer},
- { 'Z', Z_set, Z_get, &ffi_type_pointer},
+ { 'u', u_set, u_get, NULL}, /* ffi_type set later */
+ { 'U', U_set, U_get, &ffi_type_pointer},
+ { 'Z', Z_set, Z_get, &ffi_type_pointer},
#endif
#ifdef MS_WIN32
- { 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
- { 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
+ { 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
+ { 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
#endif
#if SIZEOF__BOOL == 1
- { '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
+ { '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
#elif SIZEOF__BOOL == SIZEOF_SHORT
- { '?', bool_set, bool_get, &ffi_type_ushort},
+ { '?', bool_set, bool_get, &ffi_type_ushort},
#elif SIZEOF__BOOL == SIZEOF_INT
- { '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw},
+ { '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw},
#elif SIZEOF__BOOL == SIZEOF_LONG
- { '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw},
+ { '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw},
#elif SIZEOF__BOOL == SIZEOF_LONG_LONG
- { '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
+ { '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
#endif /* SIZEOF__BOOL */
- { 'O', O_set, O_get, &ffi_type_pointer},
- { 0, NULL, NULL, NULL},
+ { 'O', O_set, O_get, &ffi_type_pointer},
+ { 0, NULL, NULL, NULL},
};
/*
@@ -1705,26 +1705,26 @@ static struct fielddesc formattable[] = {
struct fielddesc *
_ctypes_get_fielddesc(const char *fmt)
{
- static int initialized = 0;
- struct fielddesc *table = formattable;
+ static int initialized = 0;
+ struct fielddesc *table = formattable;
- if (!initialized) {
- initialized = 1;
+ if (!initialized) {
+ initialized = 1;
#ifdef CTYPES_UNICODE
- if (sizeof(wchar_t) == sizeof(short))
- _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
- else if (sizeof(wchar_t) == sizeof(int))
- _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
- else if (sizeof(wchar_t) == sizeof(long))
- _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
+ if (sizeof(wchar_t) == sizeof(short))
+ _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
+ else if (sizeof(wchar_t) == sizeof(int))
+ _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
+ else if (sizeof(wchar_t) == sizeof(long))
+ _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
#endif
- }
+ }
- for (; table->code; ++table) {
- if (table->code == fmt[0])
- return table;
- }
- return NULL;
+ for (; table->code; ++table) {
+ if (table->code == fmt[0])
+ return table;
+ }
+ return NULL;
}
typedef struct { char c; char x; } s_char;
@@ -1768,10 +1768,10 @@ typedef struct { char c; PY_LONG_LONG x; } s_long_long;
/* from ffi.h:
typedef struct _ffi_type
{
- size_t size;
- unsigned short alignment;
- unsigned short type;
- struct _ffi_type **elements;
+ size_t size;
+ unsigned short alignment;
+ unsigned short type;
+ struct _ffi_type **elements;
} ffi_type;
*/
@@ -1798,7 +1798,7 @@ ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE };
#endif
/* This is already defined on OSX */
ffi_type ffi_type_longdouble = { sizeof(long double), LONGDOUBLE_ALIGN,
- FFI_TYPE_LONGDOUBLE };
+ FFI_TYPE_LONGDOUBLE };
ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER };
diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h
index b4047a4..e0bad07 100644
--- a/Modules/_ctypes/ctypes.h
+++ b/Modules/_ctypes/ctypes.h
@@ -21,16 +21,16 @@ Making CDataObject a variable size object would be a better solution, but more
difficult in the presence of PyCFuncPtrObject. Maybe later.
*/
union value {
- char c[16];
- short s;
- int i;
- long l;
- float f;
- double d;
+ char c[16];
+ short s;
+ int i;
+ long l;
+ float f;
+ double d;
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG ll;
+ PY_LONG_LONG ll;
#endif
- long double D;
+ long double D;
};
/*
@@ -40,67 +40,67 @@ union value {
*/
struct tagCDataObject {
- PyObject_HEAD
- char *b_ptr; /* pointer to memory block */
- int b_needsfree; /* need _we_ free the memory? */
- CDataObject *b_base; /* pointer to base object or NULL */
- Py_ssize_t b_size; /* size of memory block in bytes */
- Py_ssize_t b_length; /* number of references we need */
- Py_ssize_t b_index; /* index of this object into base's
- b_object list */
- PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
- union value b_value;
+ PyObject_HEAD
+ char *b_ptr; /* pointer to memory block */
+ int b_needsfree; /* need _we_ free the memory? */
+ CDataObject *b_base; /* pointer to base object or NULL */
+ Py_ssize_t b_size; /* size of memory block in bytes */
+ Py_ssize_t b_length; /* number of references we need */
+ Py_ssize_t b_index; /* index of this object into base's
+ b_object list */
+ PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
+ union value b_value;
};
typedef struct {
- PyObject_VAR_HEAD
- ffi_closure *pcl; /* the C callable */
- ffi_cif cif;
- int flags;
- PyObject *converters;
- PyObject *callable;
- PyObject *restype;
- SETFUNC setfunc;
- ffi_type *ffi_restype;
- ffi_type *atypes[1];
+ PyObject_VAR_HEAD
+ ffi_closure *pcl; /* the C callable */
+ ffi_cif cif;
+ int flags;
+ PyObject *converters;
+ PyObject *callable;
+ PyObject *restype;
+ SETFUNC setfunc;
+ ffi_type *ffi_restype;
+ ffi_type *atypes[1];
} CThunkObject;
extern PyTypeObject PyCThunk_Type;
-#define CThunk_CheckExact(v) ((v)->ob_type == &PyCThunk_Type)
+#define CThunk_CheckExact(v) ((v)->ob_type == &PyCThunk_Type)
typedef struct {
- /* First part identical to tagCDataObject */
- PyObject_HEAD
- char *b_ptr; /* pointer to memory block */
- int b_needsfree; /* need _we_ free the memory? */
- CDataObject *b_base; /* pointer to base object or NULL */
- Py_ssize_t b_size; /* size of memory block in bytes */
- Py_ssize_t b_length; /* number of references we need */
- Py_ssize_t b_index; /* index of this object into base's
- b_object list */
- PyObject *b_objects; /* list of references we need to keep */
- union value b_value;
- /* end of tagCDataObject, additional fields follow */
-
- CThunkObject *thunk;
- PyObject *callable;
-
- /* These two fields will override the ones in the type's stgdict if
- they are set */
- PyObject *converters;
- PyObject *argtypes;
- PyObject *restype;
- PyObject *checker;
- PyObject *errcheck;
+ /* First part identical to tagCDataObject */
+ PyObject_HEAD
+ char *b_ptr; /* pointer to memory block */
+ int b_needsfree; /* need _we_ free the memory? */
+ CDataObject *b_base; /* pointer to base object or NULL */
+ Py_ssize_t b_size; /* size of memory block in bytes */
+ Py_ssize_t b_length; /* number of references we need */
+ Py_ssize_t b_index; /* index of this object into base's
+ b_object list */
+ PyObject *b_objects; /* list of references we need to keep */
+ union value b_value;
+ /* end of tagCDataObject, additional fields follow */
+
+ CThunkObject *thunk;
+ PyObject *callable;
+
+ /* These two fields will override the ones in the type's stgdict if
+ they are set */
+ PyObject *converters;
+ PyObject *argtypes;
+ PyObject *restype;
+ PyObject *checker;
+ PyObject *errcheck;
#ifdef MS_WIN32
- int index;
- GUID *iid;
+ int index;
+ GUID *iid;
#endif
- PyObject *paramflags;
+ PyObject *paramflags;
} PyCFuncPtrObject;
extern PyTypeObject PyCStgDict_Type;
-#define PyCStgDict_CheckExact(v) ((v)->ob_type == &PyCStgDict_Type)
-#define PyCStgDict_Check(v) PyObject_TypeCheck(v, &PyCStgDict_Type)
+#define PyCStgDict_CheckExact(v) ((v)->ob_type == &PyCStgDict_Type)
+#define PyCStgDict_Check(v) PyObject_TypeCheck(v, &PyCStgDict_Type)
extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
@@ -109,12 +109,12 @@ extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palig
extern PyTypeObject PyCData_Type;
-#define CDataObject_CheckExact(v) ((v)->ob_type == &PyCData_Type)
-#define CDataObject_Check(v) PyObject_TypeCheck(v, &PyCData_Type)
+#define CDataObject_CheckExact(v) ((v)->ob_type == &PyCData_Type)
+#define CDataObject_Check(v) PyObject_TypeCheck(v, &PyCData_Type)
extern PyTypeObject PyCSimpleType_Type;
-#define PyCSimpleTypeObject_CheckExact(v) ((v)->ob_type == &PyCSimpleType_Type)
-#define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type)
+#define PyCSimpleTypeObject_CheckExact(v) ((v)->ob_type == &PyCSimpleType_Type)
+#define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type)
extern PyTypeObject PyCField_Type;
extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
@@ -122,9 +122,9 @@ extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
extern PyObject *
PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
- Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
- Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
- int pack, int is_big_endian);
+ Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
+ Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
+ int pack, int is_big_endian);
extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
@@ -137,13 +137,13 @@ extern PyTypeObject PyCFuncPtr_Type;
extern PyTypeObject PyCFuncPtrType_Type;
extern PyTypeObject PyCStructType_Type;
-#define PyCArrayTypeObject_Check(v) PyObject_TypeCheck(v, &PyCArrayType_Type)
-#define ArrayObject_Check(v) PyObject_TypeCheck(v, &PyCArray_Type)
-#define PointerObject_Check(v) PyObject_TypeCheck(v, &PyCPointer_Type)
-#define PyCPointerTypeObject_Check(v) PyObject_TypeCheck(v, &PyCPointerType_Type)
-#define PyCFuncPtrObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtr_Type)
-#define PyCFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
-#define PyCStructTypeObject_Check(v) PyObject_TypeCheck(v, &PyCStructType_Type)
+#define PyCArrayTypeObject_Check(v) PyObject_TypeCheck(v, &PyCArrayType_Type)
+#define ArrayObject_Check(v) PyObject_TypeCheck(v, &PyCArray_Type)
+#define PointerObject_Check(v) PyObject_TypeCheck(v, &PyCPointer_Type)
+#define PyCPointerTypeObject_Check(v) PyObject_TypeCheck(v, &PyCPointerType_Type)
+#define PyCFuncPtrObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtr_Type)
+#define PyCFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
+#define PyCStructTypeObject_Check(v) PyObject_TypeCheck(v, &PyCStructType_Type)
extern PyObject *
PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
@@ -151,35 +151,35 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
extern PyMethodDef _ctypes_module_methods[];
extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
- PyObject *converters,
- PyObject *restype,
- int flags);
+ PyObject *converters,
+ PyObject *restype,
+ int flags);
/* a table entry describing a predefined ctypes type */
struct fielddesc {
- char code;
- SETFUNC setfunc;
- GETFUNC getfunc;
- ffi_type *pffi_type; /* always statically allocated */
- SETFUNC setfunc_swapped;
- GETFUNC getfunc_swapped;
+ char code;
+ SETFUNC setfunc;
+ GETFUNC getfunc;
+ ffi_type *pffi_type; /* always statically allocated */
+ SETFUNC setfunc_swapped;
+ GETFUNC getfunc_swapped;
};
typedef struct {
- PyObject_HEAD
- Py_ssize_t offset;
- Py_ssize_t size;
- Py_ssize_t index; /* Index into CDataObject's
- object array */
- PyObject *proto; /* a type or NULL */
- GETFUNC getfunc; /* getter function if proto is NULL */
- SETFUNC setfunc; /* setter function if proto is NULL */
- int anonymous;
+ PyObject_HEAD
+ Py_ssize_t offset;
+ Py_ssize_t size;
+ Py_ssize_t index; /* Index into CDataObject's
+ object array */
+ PyObject *proto; /* a type or NULL */
+ GETFUNC getfunc; /* getter function if proto is NULL */
+ SETFUNC setfunc; /* setter function if proto is NULL */
+ int anonymous;
} CFieldObject;
/* A subclass of PyDictObject, used as the instance dictionary of ctypes
metatypes */
typedef struct {
- PyDictObject dict; /* first part identical to PyDictObject */
+ PyDictObject dict; /* first part identical to PyDictObject */
/* The size and align fields are unneeded, they are in ffi_type as well. As
an experiment shows, it's trivial to get rid of them, the only thing to
remember is that in PyCArrayType_new the ffi_type fields must be filled in -
@@ -188,28 +188,28 @@ typedef struct {
too much risk to change that now, and there are other fields which doen't
belong into this structure anyway. Maybe in ctypes 2.0... (ctypes 2000?)
*/
- Py_ssize_t size; /* number of bytes */
- Py_ssize_t align; /* alignment requirements */
- Py_ssize_t length; /* number of fields */
- ffi_type ffi_type_pointer;
- PyObject *proto; /* Only for Pointer/ArrayObject */
- SETFUNC setfunc; /* Only for simple objects */
- GETFUNC getfunc; /* Only for simple objects */
- PARAMFUNC paramfunc;
-
- /* Following fields only used by PyCFuncPtrType_Type instances */
- PyObject *argtypes; /* tuple of CDataObjects */
- PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
- PyObject *restype; /* CDataObject or NULL */
- PyObject *checker;
- int flags; /* calling convention and such */
-
- /* pep3118 fields, pointers neeed PyMem_Free */
- char *format;
- int ndim;
- Py_ssize_t *shape;
-/* Py_ssize_t *strides; */ /* unused in ctypes */
-/* Py_ssize_t *suboffsets; */ /* unused in ctypes */
+ Py_ssize_t size; /* number of bytes */
+ Py_ssize_t align; /* alignment requirements */
+ Py_ssize_t length; /* number of fields */
+ ffi_type ffi_type_pointer;
+ PyObject *proto; /* Only for Pointer/ArrayObject */
+ SETFUNC setfunc; /* Only for simple objects */
+ GETFUNC getfunc; /* Only for simple objects */
+ PARAMFUNC paramfunc;
+
+ /* Following fields only used by PyCFuncPtrType_Type instances */
+ PyObject *argtypes; /* tuple of CDataObjects */
+ PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
+ PyObject *restype; /* CDataObject or NULL */
+ PyObject *checker;
+ int flags; /* calling convention and such */
+
+ /* pep3118 fields, pointers neeed PyMem_Free */
+ char *format;
+ int ndim;
+ Py_ssize_t *shape;
+/* Py_ssize_t *strides; */ /* unused in ctypes */
+/* Py_ssize_t *suboffsets; */ /* unused in ctypes */
} StgDictObject;
@@ -264,16 +264,16 @@ extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
typedef int(* PPROC)(void);
PyObject *_ctypes_callproc(PPROC pProc,
- PyObject *arguments,
+ PyObject *arguments,
#ifdef MS_WIN32
- IUnknown *pIUnk,
- GUID *iid,
+ IUnknown *pIUnk,
+ GUID *iid,
#endif
- int flags,
- PyObject *argtypes,
- PyObject *restype,
- PyObject *checker);
-
+ int flags,
+ PyObject *argtypes,
+ PyObject *restype,
+ PyObject *checker);
+
#define FUNCFLAG_STDCALL 0x0
#define FUNCFLAG_CDECL 0x1
@@ -288,45 +288,45 @@ PyObject *_ctypes_callproc(PPROC pProc,
#define DICTFLAG_FINAL 0x1000
struct tagPyCArgObject {
- PyObject_HEAD
- ffi_type *pffi_type;
- char tag;
- union {
- char c;
- char b;
- short h;
- int i;
- long l;
+ PyObject_HEAD
+ ffi_type *pffi_type;
+ char tag;
+ union {
+ char c;
+ char b;
+ short h;
+ int i;
+ long l;
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG q;
+ PY_LONG_LONG q;
#endif
- long double D;
- double d;
- float f;
- void *p;
- } value;
- PyObject *obj;
- Py_ssize_t size; /* for the 'V' tag */
+ long double D;
+ double d;
+ float f;
+ void *p;
+ } value;
+ PyObject *obj;
+ Py_ssize_t size; /* for the 'V' tag */
};
extern PyTypeObject PyCArg_Type;
-#define PyCArg_CheckExact(v) ((v)->ob_type == &PyCArg_Type)
+#define PyCArg_CheckExact(v) ((v)->ob_type == &PyCArg_Type)
extern PyCArgObject *PyCArgObject_new(void);
extern PyObject *
PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
- Py_ssize_t index, Py_ssize_t size, char *ptr);
+ Py_ssize_t index, Py_ssize_t size, char *ptr);
extern int
PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
- Py_ssize_t index, Py_ssize_t size, char *ptr);
+ Py_ssize_t index, Py_ssize_t size, char *ptr);
extern void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...);
struct basespec {
- CDataObject *base;
- Py_ssize_t index;
- char *adr;
+ CDataObject *base;
+ Py_ssize_t index;
+ char *adr;
};
extern char basespec_string[];
diff --git a/Modules/_ctypes/darwin/dlfcn_simple.c b/Modules/_ctypes/darwin/dlfcn_simple.c
index 4b55323..2b293bb 100644
--- a/Modules/_ctypes/darwin/dlfcn_simple.c
+++ b/Modules/_ctypes/darwin/dlfcn_simple.c
@@ -81,167 +81,167 @@ static const char *error(int setget, const char *str, ...);
/* Set and get the error string for use by dlerror */
static const char *error(int setget, const char *str, ...)
{
- static char errstr[ERR_STR_LEN];
- static int err_filled = 0;
- const char *retval;
- va_list arg;
- if (setget == 0)
- {
- va_start(arg, str);
- strncpy(errstr, "dlcompat: ", ERR_STR_LEN);
- vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
- va_end(arg);
- err_filled = 1;
- retval = NULL;
- }
- else
- {
- if (!err_filled)
- retval = NULL;
- else
- retval = errstr;
- err_filled = 0;
- }
- return retval;
+ static char errstr[ERR_STR_LEN];
+ static int err_filled = 0;
+ const char *retval;
+ va_list arg;
+ if (setget == 0)
+ {
+ va_start(arg, str);
+ strncpy(errstr, "dlcompat: ", ERR_STR_LEN);
+ vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
+ va_end(arg);
+ err_filled = 1;
+ retval = NULL;
+ }
+ else
+ {
+ if (!err_filled)
+ retval = NULL;
+ else
+ retval = errstr;
+ err_filled = 0;
+ }
+ return retval;
}
/* darwin_dlopen */
static void *darwin_dlopen(const char *path, int mode)
{
- void *module = 0;
- NSObjectFileImage ofi = 0;
- NSObjectFileImageReturnCode ofirc;
+ void *module = 0;
+ NSObjectFileImage ofi = 0;
+ NSObjectFileImageReturnCode ofirc;
- /* If we got no path, the app wants the global namespace, use -1 as the marker
- in this case */
- if (!path)
- return (void *)-1;
+ /* If we got no path, the app wants the global namespace, use -1 as the marker
+ in this case */
+ if (!path)
+ return (void *)-1;
- /* Create the object file image, works for things linked with the -bundle arg to ld */
- ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
- switch (ofirc)
- {
- case NSObjectFileImageSuccess:
- /* It was okay, so use NSLinkModule to link in the image */
- module = NSLinkModule(ofi, path,
- NSLINKMODULE_OPTION_RETURN_ON_ERROR
- | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE
- | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
- NSDestroyObjectFileImage(ofi);
- break;
- case NSObjectFileImageInappropriateFile:
- /* It may have been a dynamic library rather than a bundle, try to load it */
- module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
- break;
- default:
- /* God knows what we got */
- error(0, "Can not open \"%s\"", path);
- return 0;
- }
- if (!module)
- error(0, "Can not open \"%s\"", path);
- return module;
+ /* Create the object file image, works for things linked with the -bundle arg to ld */
+ ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
+ switch (ofirc)
+ {
+ case NSObjectFileImageSuccess:
+ /* It was okay, so use NSLinkModule to link in the image */
+ module = NSLinkModule(ofi, path,
+ NSLINKMODULE_OPTION_RETURN_ON_ERROR
+ | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE
+ | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
+ NSDestroyObjectFileImage(ofi);
+ break;
+ case NSObjectFileImageInappropriateFile:
+ /* It may have been a dynamic library rather than a bundle, try to load it */
+ module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
+ break;
+ default:
+ /* God knows what we got */
+ error(0, "Can not open \"%s\"", path);
+ return 0;
+ }
+ if (!module)
+ error(0, "Can not open \"%s\"", path);
+ return module;
}
/* dlsymIntern is used by dlsym to find the symbol */
static void *dlsymIntern(void *handle, const char *symbol)
{
- NSSymbol nssym = 0;
- /* If the handle is -1, if is the app global context */
- if (handle == (void *)-1)
- {
- /* Global context, use NSLookupAndBindSymbol */
- if (NSIsSymbolNameDefined(symbol))
- {
- nssym = NSLookupAndBindSymbol(symbol);
- }
+ NSSymbol nssym = 0;
+ /* If the handle is -1, if is the app global context */
+ if (handle == (void *)-1)
+ {
+ /* Global context, use NSLookupAndBindSymbol */
+ if (NSIsSymbolNameDefined(symbol))
+ {
+ nssym = NSLookupAndBindSymbol(symbol);
+ }
- }
- /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
- for libraries, and NSLookupSymbolInModule for bundles */
- else
- {
- /* Check for both possible magic numbers depending on x86/ppc byte order */
- if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
- (((struct mach_header *)handle)->magic == MH_CIGAM))
- {
- if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
- {
- nssym = NSLookupSymbolInImage((struct mach_header *)handle,
- symbol,
- NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
- | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
- }
+ }
+ /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
+ for libraries, and NSLookupSymbolInModule for bundles */
+ else
+ {
+ /* Check for both possible magic numbers depending on x86/ppc byte order */
+ if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
+ (((struct mach_header *)handle)->magic == MH_CIGAM))
+ {
+ if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
+ {
+ nssym = NSLookupSymbolInImage((struct mach_header *)handle,
+ symbol,
+ NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
+ | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
+ }
- }
- else
- {
- nssym = NSLookupSymbolInModule(handle, symbol);
- }
- }
- if (!nssym)
- {
- error(0, "Symbol \"%s\" Not found", symbol);
- return NULL;
- }
- return NSAddressOfSymbol(nssym);
+ }
+ else
+ {
+ nssym = NSLookupSymbolInModule(handle, symbol);
+ }
+ }
+ if (!nssym)
+ {
+ error(0, "Symbol \"%s\" Not found", symbol);
+ return NULL;
+ }
+ return NSAddressOfSymbol(nssym);
}
static const char *darwin_dlerror(void)
{
- return error(1, (char *)NULL);
+ return error(1, (char *)NULL);
}
static int darwin_dlclose(void *handle)
{
- if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
- (((struct mach_header *)handle)->magic == MH_CIGAM))
- {
- error(0, "Can't remove dynamic libraries on darwin");
- return 0;
- }
- if (!NSUnLinkModule(handle, 0))
- {
- error(0, "unable to unlink module %s", NSNameOfModule(handle));
- return 1;
- }
- return 0;
+ if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
+ (((struct mach_header *)handle)->magic == MH_CIGAM))
+ {
+ error(0, "Can't remove dynamic libraries on darwin");
+ return 0;
+ }
+ if (!NSUnLinkModule(handle, 0))
+ {
+ error(0, "unable to unlink module %s", NSNameOfModule(handle));
+ return 1;
+ }
+ return 0;
}
/* dlsym, prepend the underscore and call dlsymIntern */
static void *darwin_dlsym(void *handle, const char *symbol)
{
- static char undersym[257]; /* Saves calls to malloc(3) */
- int sym_len = strlen(symbol);
- void *value = NULL;
- char *malloc_sym = NULL;
+ static char undersym[257]; /* Saves calls to malloc(3) */
+ int sym_len = strlen(symbol);
+ void *value = NULL;
+ char *malloc_sym = NULL;
- if (sym_len < 256)
- {
- snprintf(undersym, 256, "_%s", symbol);
- value = dlsymIntern(handle, undersym);
- }
- else
- {
- malloc_sym = malloc(sym_len + 2);
- if (malloc_sym)
- {
- sprintf(malloc_sym, "_%s", symbol);
- value = dlsymIntern(handle, malloc_sym);
- free(malloc_sym);
- }
- else
- {
- error(0, "Unable to allocate memory");
- }
- }
- return value;
+ if (sym_len < 256)
+ {
+ snprintf(undersym, 256, "_%s", symbol);
+ value = dlsymIntern(handle, undersym);
+ }
+ else
+ {
+ malloc_sym = malloc(sym_len + 2);
+ if (malloc_sym)
+ {
+ sprintf(malloc_sym, "_%s", symbol);
+ value = dlsymIntern(handle, malloc_sym);
+ free(malloc_sym);
+ }
+ else
+ {
+ error(0, "Unable to allocate memory");
+ }
+ }
+ return value;
}
static int darwin_dladdr(const void *handle, Dl_info *info) {
- return 0;
+ return 0;
}
#endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */
@@ -252,21 +252,21 @@ static void __attribute__ ((constructor)) ctypes_dlfcn_init(void);
static
#endif
void ctypes_dlfcn_init(void) {
- if (dlopen != NULL) {
- ctypes_dlsym = dlsym;
- ctypes_dlopen = dlopen;
- ctypes_dlerror = dlerror;
- ctypes_dlclose = dlclose;
- ctypes_dladdr = dladdr;
- } else {
+ if (dlopen != NULL) {
+ ctypes_dlsym = dlsym;
+ ctypes_dlopen = dlopen;
+ ctypes_dlerror = dlerror;
+ ctypes_dlclose = dlclose;
+ ctypes_dladdr = dladdr;
+ } else {
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3
- ctypes_dlsym = darwin_dlsym;
- ctypes_dlopen = darwin_dlopen;
- ctypes_dlerror = darwin_dlerror;
- ctypes_dlclose = darwin_dlclose;
- ctypes_dladdr = darwin_dladdr;
+ ctypes_dlsym = darwin_dlsym;
+ ctypes_dlopen = darwin_dlopen;
+ ctypes_dlerror = darwin_dlerror;
+ ctypes_dlclose = darwin_dlclose;
+ ctypes_dladdr = darwin_dladdr;
#endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */
- }
+ }
}
#endif /* CTYPES_DARWIN_DLFCN */
diff --git a/Modules/_ctypes/malloc_closure.c b/Modules/_ctypes/malloc_closure.c
index 9a5bfcf..ae857a2 100644
--- a/Modules/_ctypes/malloc_closure.c
+++ b/Modules/_ctypes/malloc_closure.c
@@ -23,8 +23,8 @@
/******************************************************************/
typedef union _tagITEM {
- ffi_closure closure;
- union _tagITEM *next;
+ ffi_closure closure;
+ union _tagITEM *next;
} ITEM;
static ITEM *free_list;
@@ -32,58 +32,58 @@ static int _pagesize;
static void more_core(void)
{
- ITEM *item;
- int count, i;
+ ITEM *item;
+ int count, i;
/* determine the pagesize */
#ifdef MS_WIN32
- if (!_pagesize) {
- SYSTEM_INFO systeminfo;
- GetSystemInfo(&systeminfo);
- _pagesize = systeminfo.dwPageSize;
- }
+ if (!_pagesize) {
+ SYSTEM_INFO systeminfo;
+ GetSystemInfo(&systeminfo);
+ _pagesize = systeminfo.dwPageSize;
+ }
#else
- if (!_pagesize) {
+ if (!_pagesize) {
#ifdef _SC_PAGESIZE
- _pagesize = sysconf(_SC_PAGESIZE);
+ _pagesize = sysconf(_SC_PAGESIZE);
#else
- _pagesize = getpagesize();
+ _pagesize = getpagesize();
#endif
- }
+ }
#endif
- /* calculate the number of nodes to allocate */
- count = BLOCKSIZE / sizeof(ITEM);
+ /* calculate the number of nodes to allocate */
+ count = BLOCKSIZE / sizeof(ITEM);
- /* allocate a memory block */
+ /* allocate a memory block */
#ifdef MS_WIN32
- item = (ITEM *)VirtualAlloc(NULL,
- count * sizeof(ITEM),
- MEM_COMMIT,
- PAGE_EXECUTE_READWRITE);
- if (item == NULL)
- return;
+ item = (ITEM *)VirtualAlloc(NULL,
+ count * sizeof(ITEM),
+ MEM_COMMIT,
+ PAGE_EXECUTE_READWRITE);
+ if (item == NULL)
+ return;
#else
- item = (ITEM *)mmap(NULL,
- count * sizeof(ITEM),
- PROT_READ | PROT_WRITE | PROT_EXEC,
- MAP_PRIVATE | MAP_ANONYMOUS,
- -1,
- 0);
- if (item == (void *)MAP_FAILED)
- return;
+ item = (ITEM *)mmap(NULL,
+ count * sizeof(ITEM),
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_PRIVATE | MAP_ANONYMOUS,
+ -1,
+ 0);
+ if (item == (void *)MAP_FAILED)
+ return;
#endif
#ifdef MALLOC_CLOSURE_DEBUG
- printf("block at %p allocated (%d bytes), %d ITEMs\n",
- item, count * sizeof(ITEM), count);
+ printf("block at %p allocated (%d bytes), %d ITEMs\n",
+ item, count * sizeof(ITEM), count);
#endif
- /* put them into the free list */
- for (i = 0; i < count; ++i) {
- item->next = free_list;
- free_list = item;
- ++item;
- }
+ /* put them into the free list */
+ for (i = 0; i < count; ++i) {
+ item->next = free_list;
+ free_list = item;
+ ++item;
+ }
}
/******************************************************************/
@@ -91,20 +91,20 @@ static void more_core(void)
/* put the item back into the free list */
void _ctypes_free_closure(void *p)
{
- ITEM *item = (ITEM *)p;
- item->next = free_list;
- free_list = item;
+ ITEM *item = (ITEM *)p;
+ item->next = free_list;
+ free_list = item;
}
/* return one item from the free list, allocating more if needed */
void *_ctypes_alloc_closure(void)
{
- ITEM *item;
- if (!free_list)
- more_core();
- if (!free_list)
- return NULL;
- item = free_list;
- free_list = item->next;
- return item;
+ ITEM *item;
+ if (!free_list)
+ more_core();
+ if (!free_list)
+ return NULL;
+ item = free_list;
+ free_list = item->next;
+ return item;
}
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index 6c81017..105e0df 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -19,143 +19,143 @@
static int
PyCStgDict_init(StgDictObject *self, PyObject *args, PyObject *kwds)
{
- if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
- return -1;
- self->format = NULL;
- self->ndim = 0;
- self->shape = NULL;
- return 0;
+ if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
+ return -1;
+ self->format = NULL;
+ self->ndim = 0;
+ self->shape = NULL;
+ return 0;
}
static int
PyCStgDict_clear(StgDictObject *self)
{
- Py_CLEAR(self->proto);
- Py_CLEAR(self->argtypes);
- Py_CLEAR(self->converters);
- Py_CLEAR(self->restype);
- Py_CLEAR(self->checker);
- return 0;
+ Py_CLEAR(self->proto);
+ Py_CLEAR(self->argtypes);
+ Py_CLEAR(self->converters);
+ Py_CLEAR(self->restype);
+ Py_CLEAR(self->checker);
+ return 0;
}
static void
PyCStgDict_dealloc(StgDictObject *self)
{
- PyCStgDict_clear(self);
- PyMem_Free(self->format);
- PyMem_Free(self->shape);
- PyMem_Free(self->ffi_type_pointer.elements);
- PyDict_Type.tp_dealloc((PyObject *)self);
+ PyCStgDict_clear(self);
+ PyMem_Free(self->format);
+ PyMem_Free(self->shape);
+ PyMem_Free(self->ffi_type_pointer.elements);
+ PyDict_Type.tp_dealloc((PyObject *)self);
}
int
PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
{
- char *d, *s;
- Py_ssize_t size;
-
- PyCStgDict_clear(dst);
- PyMem_Free(dst->ffi_type_pointer.elements);
- PyMem_Free(dst->format);
- dst->format = NULL;
- PyMem_Free(dst->shape);
- dst->shape = NULL;
- dst->ffi_type_pointer.elements = NULL;
-
- d = (char *)dst;
- s = (char *)src;
- memcpy(d + sizeof(PyDictObject),
- s + sizeof(PyDictObject),
- sizeof(StgDictObject) - sizeof(PyDictObject));
-
- Py_XINCREF(dst->proto);
- Py_XINCREF(dst->argtypes);
- Py_XINCREF(dst->converters);
- Py_XINCREF(dst->restype);
- Py_XINCREF(dst->checker);
-
- if (src->format) {
- dst->format = PyMem_Malloc(strlen(src->format) + 1);
- if (dst->format == NULL)
- return -1;
- strcpy(dst->format, src->format);
- }
- if (src->shape) {
- dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
- if (dst->shape == NULL)
- return -1;
- memcpy(dst->shape, src->shape,
- sizeof(Py_ssize_t) * src->ndim);
- }
-
- if (src->ffi_type_pointer.elements == NULL)
- return 0;
- size = sizeof(ffi_type *) * (src->length + 1);
- dst->ffi_type_pointer.elements = PyMem_Malloc(size);
- if (dst->ffi_type_pointer.elements == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- memcpy(dst->ffi_type_pointer.elements,
- src->ffi_type_pointer.elements,
- size);
- return 0;
+ char *d, *s;
+ Py_ssize_t size;
+
+ PyCStgDict_clear(dst);
+ PyMem_Free(dst->ffi_type_pointer.elements);
+ PyMem_Free(dst->format);
+ dst->format = NULL;
+ PyMem_Free(dst->shape);
+ dst->shape = NULL;
+ dst->ffi_type_pointer.elements = NULL;
+
+ d = (char *)dst;
+ s = (char *)src;
+ memcpy(d + sizeof(PyDictObject),
+ s + sizeof(PyDictObject),
+ sizeof(StgDictObject) - sizeof(PyDictObject));
+
+ Py_XINCREF(dst->proto);
+ Py_XINCREF(dst->argtypes);
+ Py_XINCREF(dst->converters);
+ Py_XINCREF(dst->restype);
+ Py_XINCREF(dst->checker);
+
+ if (src->format) {
+ dst->format = PyMem_Malloc(strlen(src->format) + 1);
+ if (dst->format == NULL)
+ return -1;
+ strcpy(dst->format, src->format);
+ }
+ if (src->shape) {
+ dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
+ if (dst->shape == NULL)
+ return -1;
+ memcpy(dst->shape, src->shape,
+ sizeof(Py_ssize_t) * src->ndim);
+ }
+
+ if (src->ffi_type_pointer.elements == NULL)
+ return 0;
+ size = sizeof(ffi_type *) * (src->length + 1);
+ dst->ffi_type_pointer.elements = PyMem_Malloc(size);
+ if (dst->ffi_type_pointer.elements == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ memcpy(dst->ffi_type_pointer.elements,
+ src->ffi_type_pointer.elements,
+ size);
+ return 0;
}
PyTypeObject PyCStgDict_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "StgDict",
- sizeof(StgDictObject),
- 0,
- (destructor)PyCStgDict_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)PyCStgDict_init, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "StgDict",
+ sizeof(StgDictObject),
+ 0,
+ (destructor)PyCStgDict_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)PyCStgDict_init, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
};
/* May return NULL, but does not set an exception! */
StgDictObject *
PyType_stgdict(PyObject *obj)
{
- PyTypeObject *type;
-
- if (!PyType_Check(obj))
- return NULL;
- type = (PyTypeObject *)obj;
- if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
- return NULL;
- return (StgDictObject *)type->tp_dict;
+ PyTypeObject *type;
+
+ if (!PyType_Check(obj))
+ return NULL;
+ type = (PyTypeObject *)obj;
+ if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
+ return NULL;
+ return (StgDictObject *)type->tp_dict;
}
/* May return NULL, but does not set an exception! */
@@ -166,10 +166,10 @@ PyType_stgdict(PyObject *obj)
StgDictObject *
PyObject_stgdict(PyObject *self)
{
- PyTypeObject *type = self->ob_type;
- if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
- return NULL;
- return (StgDictObject *)type->tp_dict;
+ PyTypeObject *type = self->ob_type;
+ if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
+ return NULL;
+ return (StgDictObject *)type->tp_dict;
}
/* descr is the descriptor for a field marked as anonymous. Get all the
@@ -178,78 +178,78 @@ PyObject_stgdict(PyObject *self)
*/
static int
MakeFields(PyObject *type, CFieldObject *descr,
- Py_ssize_t index, Py_ssize_t offset)
+ Py_ssize_t index, Py_ssize_t offset)
{
- Py_ssize_t i;
- PyObject *fields;
- PyObject *fieldlist;
-
- fields = PyObject_GetAttrString(descr->proto, "_fields_");
- if (fields == NULL)
- return -1;
- fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence");
- Py_DECREF(fields);
- if (fieldlist == NULL)
- return -1;
-
- for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) {
- PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */
- PyObject *fname, *ftype, *bits;
- CFieldObject *fdescr;
- CFieldObject *new_descr;
- /* Convert to PyArg_UnpackTuple... */
- if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) {
- Py_DECREF(fieldlist);
- return -1;
- }
- fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname);
- if (fdescr == NULL) {
- Py_DECREF(fieldlist);
- return -1;
- }
- if (Py_TYPE(fdescr) != &PyCField_Type) {
- PyErr_SetString(PyExc_TypeError, "unexpected type");
- Py_DECREF(fdescr);
- Py_DECREF(fieldlist);
- return -1;
- }
- if (fdescr->anonymous) {
- int rc = MakeFields(type, fdescr,
- index + fdescr->index,
- offset + fdescr->offset);
- Py_DECREF(fdescr);
- if (rc == -1) {
- Py_DECREF(fieldlist);
- return -1;
- }
- continue;
- }
- new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, NULL);
- if (new_descr == NULL) {
- Py_DECREF(fdescr);
- Py_DECREF(fieldlist);
- return -1;
- }
- assert(Py_TYPE(new_descr) == &PyCField_Type);
- new_descr->size = fdescr->size;
- new_descr->offset = fdescr->offset + offset;
- new_descr->index = fdescr->index + index;
- new_descr->proto = fdescr->proto;
- Py_XINCREF(new_descr->proto);
- new_descr->getfunc = fdescr->getfunc;
- new_descr->setfunc = fdescr->setfunc;
-
- Py_DECREF(fdescr);
-
- if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) {
- Py_DECREF(fieldlist);
- Py_DECREF(new_descr);
- return -1;
- }
- Py_DECREF(new_descr);
- }
- Py_DECREF(fieldlist);
- return 0;
+ Py_ssize_t i;
+ PyObject *fields;
+ PyObject *fieldlist;
+
+ fields = PyObject_GetAttrString(descr->proto, "_fields_");
+ if (fields == NULL)
+ return -1;
+ fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence");
+ Py_DECREF(fields);
+ if (fieldlist == NULL)
+ return -1;
+
+ for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) {
+ PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */
+ PyObject *fname, *ftype, *bits;
+ CFieldObject *fdescr;
+ CFieldObject *new_descr;
+ /* Convert to PyArg_UnpackTuple... */
+ if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) {
+ Py_DECREF(fieldlist);
+ return -1;
+ }
+ fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname);
+ if (fdescr == NULL) {
+ Py_DECREF(fieldlist);
+ return -1;
+ }
+ if (Py_TYPE(fdescr) != &PyCField_Type) {
+ PyErr_SetString(PyExc_TypeError, "unexpected type");
+ Py_DECREF(fdescr);
+ Py_DECREF(fieldlist);
+ return -1;
+ }
+ if (fdescr->anonymous) {
+ int rc = MakeFields(type, fdescr,
+ index + fdescr->index,
+ offset + fdescr->offset);
+ Py_DECREF(fdescr);
+ if (rc == -1) {
+ Py_DECREF(fieldlist);
+ return -1;
+ }
+ continue;
+ }
+ new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, NULL);
+ if (new_descr == NULL) {
+ Py_DECREF(fdescr);
+ Py_DECREF(fieldlist);
+ return -1;
+ }
+ assert(Py_TYPE(new_descr) == &PyCField_Type);
+ new_descr->size = fdescr->size;
+ new_descr->offset = fdescr->offset + offset;
+ new_descr->index = fdescr->index + index;
+ new_descr->proto = fdescr->proto;
+ Py_XINCREF(new_descr->proto);
+ new_descr->getfunc = fdescr->getfunc;
+ new_descr->setfunc = fdescr->setfunc;
+
+ Py_DECREF(fdescr);
+
+ if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) {
+ Py_DECREF(fieldlist);
+ Py_DECREF(new_descr);
+ return -1;
+ }
+ Py_DECREF(new_descr);
+ }
+ Py_DECREF(fieldlist);
+ return 0;
}
/* Iterate over the names in the type's _anonymous_ attribute, if present,
@@ -257,43 +257,43 @@ MakeFields(PyObject *type, CFieldObject *descr,
static int
MakeAnonFields(PyObject *type)
{
- PyObject *anon;
- PyObject *anon_names;
- Py_ssize_t i;
-
- anon = PyObject_GetAttrString(type, "_anonymous_");
- if (anon == NULL) {
- PyErr_Clear();
- return 0;
- }
- anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence");
- Py_DECREF(anon);
- if (anon_names == NULL)
- return -1;
-
- for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) {
- PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */
- CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname);
- if (descr == NULL) {
- Py_DECREF(anon_names);
- return -1;
- }
- assert(Py_TYPE(descr) == &PyCField_Type);
- descr->anonymous = 1;
-
- /* descr is in the field descriptor. */
- if (-1 == MakeFields(type, (CFieldObject *)descr,
- ((CFieldObject *)descr)->index,
- ((CFieldObject *)descr)->offset)) {
- Py_DECREF(descr);
- Py_DECREF(anon_names);
- return -1;
- }
- Py_DECREF(descr);
- }
-
- Py_DECREF(anon_names);
- return 0;
+ PyObject *anon;
+ PyObject *anon_names;
+ Py_ssize_t i;
+
+ anon = PyObject_GetAttrString(type, "_anonymous_");
+ if (anon == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+ anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence");
+ Py_DECREF(anon);
+ if (anon_names == NULL)
+ return -1;
+
+ for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) {
+ PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */
+ CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname);
+ if (descr == NULL) {
+ Py_DECREF(anon_names);
+ return -1;
+ }
+ assert(Py_TYPE(descr) == &PyCField_Type);
+ descr->anonymous = 1;
+
+ /* descr is in the field descriptor. */
+ if (-1 == MakeFields(type, (CFieldObject *)descr,
+ ((CFieldObject *)descr)->index,
+ ((CFieldObject *)descr)->offset)) {
+ Py_DECREF(descr);
+ Py_DECREF(anon_names);
+ return -1;
+ }
+ Py_DECREF(descr);
+ }
+
+ Py_DECREF(anon_names);
+ return 0;
}
/*
@@ -303,261 +303,261 @@ MakeAnonFields(PyObject *type)
int
PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
{
- StgDictObject *stgdict, *basedict;
- Py_ssize_t len, offset, size, align, i;
- Py_ssize_t union_size, total_align;
- Py_ssize_t field_size = 0;
- int bitofs;
- PyObject *isPacked;
- int pack = 0;
- Py_ssize_t ffi_ofs;
- int big_endian;
-
- /* HACK Alert: I cannot be bothered to fix ctypes.com, so there has to
- be a way to use the old, broken sematics: _fields_ are not extended
- but replaced in subclasses.
-
- XXX Remove this in ctypes 1.0!
- */
- int use_broken_old_ctypes_semantics;
-
- if (fields == NULL)
- return 0;
+ StgDictObject *stgdict, *basedict;
+ Py_ssize_t len, offset, size, align, i;
+ Py_ssize_t union_size, total_align;
+ Py_ssize_t field_size = 0;
+ int bitofs;
+ PyObject *isPacked;
+ int pack = 0;
+ Py_ssize_t ffi_ofs;
+ int big_endian;
+
+ /* HACK Alert: I cannot be bothered to fix ctypes.com, so there has to
+ be a way to use the old, broken sematics: _fields_ are not extended
+ but replaced in subclasses.
+
+ XXX Remove this in ctypes 1.0!
+ */
+ int use_broken_old_ctypes_semantics;
+
+ if (fields == NULL)
+ return 0;
#ifdef WORDS_BIGENDIAN
- big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1;
+ big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1;
#else
- big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0;
+ big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0;
#endif
- use_broken_old_ctypes_semantics = \
- PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_");
-
- isPacked = PyObject_GetAttrString(type, "_pack_");
- if (isPacked) {
- pack = PyLong_AsLong(isPacked);
- if (pack < 0 || PyErr_Occurred()) {
- Py_XDECREF(isPacked);
- PyErr_SetString(PyExc_ValueError,
- "_pack_ must be a non-negative integer");
- return -1;
- }
- Py_DECREF(isPacked);
- } else
- PyErr_Clear();
-
- len = PySequence_Length(fields);
- if (len == -1) {
- PyErr_SetString(PyExc_TypeError,
- "'_fields_' must be a sequence of pairs");
- return -1;
- }
-
- stgdict = PyType_stgdict(type);
- if (!stgdict)
- return -1;
- /* If this structure/union is already marked final we cannot assign
- _fields_ anymore. */
-
- if (stgdict->flags & DICTFLAG_FINAL) {/* is final ? */
- PyErr_SetString(PyExc_AttributeError,
- "_fields_ is final");
- return -1;
- }
-
- if (stgdict->format) {
- PyMem_Free(stgdict->format);
- stgdict->format = NULL;
- }
-
- if (stgdict->ffi_type_pointer.elements)
- PyMem_Free(stgdict->ffi_type_pointer.elements);
-
- basedict = PyType_stgdict((PyObject *)((PyTypeObject *)type)->tp_base);
- if (basedict && !use_broken_old_ctypes_semantics) {
- size = offset = basedict->size;
- align = basedict->align;
- union_size = 0;
- total_align = align ? align : 1;
- stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
- stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (basedict->length + len + 1));
- if (stgdict->ffi_type_pointer.elements == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- memset(stgdict->ffi_type_pointer.elements, 0,
- sizeof(ffi_type *) * (basedict->length + len + 1));
- memcpy(stgdict->ffi_type_pointer.elements,
- basedict->ffi_type_pointer.elements,
- sizeof(ffi_type *) * (basedict->length));
- ffi_ofs = basedict->length;
- } else {
- offset = 0;
- size = 0;
- align = 0;
- union_size = 0;
- total_align = 1;
- stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
- stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (len + 1));
- if (stgdict->ffi_type_pointer.elements == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- memset(stgdict->ffi_type_pointer.elements, 0,
- sizeof(ffi_type *) * (len + 1));
- ffi_ofs = 0;
- }
-
- assert(stgdict->format == NULL);
- if (isStruct && !isPacked) {
- stgdict->format = _ctypes_alloc_format_string(NULL, "T{");
- } else {
- /* PEP3118 doesn't support union, or packed structures (well,
- only standard packing, but we dont support the pep for
- that). Use 'B' for bytes. */
- stgdict->format = _ctypes_alloc_format_string(NULL, "B");
- }
+ use_broken_old_ctypes_semantics = \
+ PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_");
+
+ isPacked = PyObject_GetAttrString(type, "_pack_");
+ if (isPacked) {
+ pack = PyLong_AsLong(isPacked);
+ if (pack < 0 || PyErr_Occurred()) {
+ Py_XDECREF(isPacked);
+ PyErr_SetString(PyExc_ValueError,
+ "_pack_ must be a non-negative integer");
+ return -1;
+ }
+ Py_DECREF(isPacked);
+ } else
+ PyErr_Clear();
+
+ len = PySequence_Length(fields);
+ if (len == -1) {
+ PyErr_SetString(PyExc_TypeError,
+ "'_fields_' must be a sequence of pairs");
+ return -1;
+ }
+
+ stgdict = PyType_stgdict(type);
+ if (!stgdict)
+ return -1;
+ /* If this structure/union is already marked final we cannot assign
+ _fields_ anymore. */
+
+ if (stgdict->flags & DICTFLAG_FINAL) {/* is final ? */
+ PyErr_SetString(PyExc_AttributeError,
+ "_fields_ is final");
+ return -1;
+ }
+
+ if (stgdict->format) {
+ PyMem_Free(stgdict->format);
+ stgdict->format = NULL;
+ }
+
+ if (stgdict->ffi_type_pointer.elements)
+ PyMem_Free(stgdict->ffi_type_pointer.elements);
+
+ basedict = PyType_stgdict((PyObject *)((PyTypeObject *)type)->tp_base);
+ if (basedict && !use_broken_old_ctypes_semantics) {
+ size = offset = basedict->size;
+ align = basedict->align;
+ union_size = 0;
+ total_align = align ? align : 1;
+ stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
+ stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (basedict->length + len + 1));
+ if (stgdict->ffi_type_pointer.elements == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ memset(stgdict->ffi_type_pointer.elements, 0,
+ sizeof(ffi_type *) * (basedict->length + len + 1));
+ memcpy(stgdict->ffi_type_pointer.elements,
+ basedict->ffi_type_pointer.elements,
+ sizeof(ffi_type *) * (basedict->length));
+ ffi_ofs = basedict->length;
+ } else {
+ offset = 0;
+ size = 0;
+ align = 0;
+ union_size = 0;
+ total_align = 1;
+ stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
+ stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (len + 1));
+ if (stgdict->ffi_type_pointer.elements == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ memset(stgdict->ffi_type_pointer.elements, 0,
+ sizeof(ffi_type *) * (len + 1));
+ ffi_ofs = 0;
+ }
+
+ assert(stgdict->format == NULL);
+ if (isStruct && !isPacked) {
+ stgdict->format = _ctypes_alloc_format_string(NULL, "T{");
+ } else {
+ /* PEP3118 doesn't support union, or packed structures (well,
+ only standard packing, but we dont support the pep for
+ that). Use 'B' for bytes. */
+ stgdict->format = _ctypes_alloc_format_string(NULL, "B");
+ }
#define realdict ((PyObject *)&stgdict->dict)
- for (i = 0; i < len; ++i) {
- PyObject *name = NULL, *desc = NULL;
- PyObject *pair = PySequence_GetItem(fields, i);
- PyObject *prop;
- StgDictObject *dict;
- int bitsize = 0;
-
- if (!pair || !PyArg_ParseTuple(pair, "OO|i", &name, &desc, &bitsize)) {
- PyErr_SetString(PyExc_AttributeError,
- "'_fields_' must be a sequence of pairs");
- Py_XDECREF(pair);
- return -1;
- }
- dict = PyType_stgdict(desc);
- if (dict == NULL) {
- Py_DECREF(pair);
- PyErr_Format(PyExc_TypeError,
- "second item in _fields_ tuple (index %zd) must be a C type",
- i);
- return -1;
- }
- stgdict->ffi_type_pointer.elements[ffi_ofs + i] = &dict->ffi_type_pointer;
- if (dict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
- stgdict->flags |= TYPEFLAG_HASPOINTER;
- dict->flags |= DICTFLAG_FINAL; /* mark field type final */
- if (PyTuple_Size(pair) == 3) { /* bits specified */
- switch(dict->ffi_type_pointer.type) {
- case FFI_TYPE_UINT8:
- case FFI_TYPE_UINT16:
- case FFI_TYPE_UINT32:
- case FFI_TYPE_SINT64:
- case FFI_TYPE_UINT64:
- break;
-
- case FFI_TYPE_SINT8:
- case FFI_TYPE_SINT16:
- case FFI_TYPE_SINT32:
- if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
+ for (i = 0; i < len; ++i) {
+ PyObject *name = NULL, *desc = NULL;
+ PyObject *pair = PySequence_GetItem(fields, i);
+ PyObject *prop;
+ StgDictObject *dict;
+ int bitsize = 0;
+
+ if (!pair || !PyArg_ParseTuple(pair, "OO|i", &name, &desc, &bitsize)) {
+ PyErr_SetString(PyExc_AttributeError,
+ "'_fields_' must be a sequence of pairs");
+ Py_XDECREF(pair);
+ return -1;
+ }
+ dict = PyType_stgdict(desc);
+ if (dict == NULL) {
+ Py_DECREF(pair);
+ PyErr_Format(PyExc_TypeError,
+ "second item in _fields_ tuple (index %zd) must be a C type",
+ i);
+ return -1;
+ }
+ stgdict->ffi_type_pointer.elements[ffi_ofs + i] = &dict->ffi_type_pointer;
+ if (dict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
+ stgdict->flags |= TYPEFLAG_HASPOINTER;
+ dict->flags |= DICTFLAG_FINAL; /* mark field type final */
+ if (PyTuple_Size(pair) == 3) { /* bits specified */
+ switch(dict->ffi_type_pointer.type) {
+ case FFI_TYPE_UINT8:
+ case FFI_TYPE_UINT16:
+ case FFI_TYPE_UINT32:
+ case FFI_TYPE_SINT64:
+ case FFI_TYPE_UINT64:
+ break;
+
+ case FFI_TYPE_SINT8:
+ case FFI_TYPE_SINT16:
+ case FFI_TYPE_SINT32:
+ if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
#ifdef CTYPES_UNICODE
- && dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
+ && dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
#endif
- )
- break;
- /* else fall through */
- default:
- PyErr_Format(PyExc_TypeError,
- "bit fields not allowed for type %s",
- ((PyTypeObject *)desc)->tp_name);
- Py_DECREF(pair);
- return -1;
- }
- if (bitsize <= 0 || bitsize > dict->size * 8) {
- PyErr_SetString(PyExc_ValueError,
- "number of bits invalid for bit field");
- Py_DECREF(pair);
- return -1;
- }
- } else
- bitsize = 0;
- if (isStruct && !isPacked) {
- char *fieldfmt = dict->format ? dict->format : "B";
- char *fieldname = _PyUnicode_AsString(name);
- char *ptr;
- Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt);
- char *buf = alloca(len + 2 + 1);
-
- sprintf(buf, "%s:%s:", fieldfmt, fieldname);
-
- ptr = stgdict->format;
- stgdict->format = _ctypes_alloc_format_string(stgdict->format, buf);
- PyMem_Free(ptr);
-
- if (stgdict->format == NULL) {
- Py_DECREF(pair);
- return -1;
- }
- }
- if (isStruct) {
- prop = PyCField_FromDesc(desc, i,
- &field_size, bitsize, &bitofs,
- &size, &offset, &align,
- pack, big_endian);
- } else /* union */ {
- size = 0;
- offset = 0;
- align = 0;
- prop = PyCField_FromDesc(desc, i,
- &field_size, bitsize, &bitofs,
- &size, &offset, &align,
- pack, big_endian);
- union_size = max(size, union_size);
- }
- total_align = max(align, total_align);
-
- if (!prop) {
- Py_DECREF(pair);
- return -1;
- }
- if (-1 == PyObject_SetAttr(type, name, prop)) {
- Py_DECREF(prop);
- Py_DECREF(pair);
- return -1;
- }
- Py_DECREF(pair);
- Py_DECREF(prop);
- }
+ )
+ break;
+ /* else fall through */
+ default:
+ PyErr_Format(PyExc_TypeError,
+ "bit fields not allowed for type %s",
+ ((PyTypeObject *)desc)->tp_name);
+ Py_DECREF(pair);
+ return -1;
+ }
+ if (bitsize <= 0 || bitsize > dict->size * 8) {
+ PyErr_SetString(PyExc_ValueError,
+ "number of bits invalid for bit field");
+ Py_DECREF(pair);
+ return -1;
+ }
+ } else
+ bitsize = 0;
+ if (isStruct && !isPacked) {
+ char *fieldfmt = dict->format ? dict->format : "B";
+ char *fieldname = _PyUnicode_AsString(name);
+ char *ptr;
+ Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt);
+ char *buf = alloca(len + 2 + 1);
+
+ sprintf(buf, "%s:%s:", fieldfmt, fieldname);
+
+ ptr = stgdict->format;
+ stgdict->format = _ctypes_alloc_format_string(stgdict->format, buf);
+ PyMem_Free(ptr);
+
+ if (stgdict->format == NULL) {
+ Py_DECREF(pair);
+ return -1;
+ }
+ }
+ if (isStruct) {
+ prop = PyCField_FromDesc(desc, i,
+ &field_size, bitsize, &bitofs,
+ &size, &offset, &align,
+ pack, big_endian);
+ } else /* union */ {
+ size = 0;
+ offset = 0;
+ align = 0;
+ prop = PyCField_FromDesc(desc, i,
+ &field_size, bitsize, &bitofs,
+ &size, &offset, &align,
+ pack, big_endian);
+ union_size = max(size, union_size);
+ }
+ total_align = max(align, total_align);
+
+ if (!prop) {
+ Py_DECREF(pair);
+ return -1;
+ }
+ if (-1 == PyObject_SetAttr(type, name, prop)) {
+ Py_DECREF(prop);
+ Py_DECREF(pair);
+ return -1;
+ }
+ Py_DECREF(pair);
+ Py_DECREF(prop);
+ }
#undef realdict
- if (isStruct && !isPacked) {
- char *ptr = stgdict->format;
- stgdict->format = _ctypes_alloc_format_string(stgdict->format, "}");
- PyMem_Free(ptr);
- if (stgdict->format == NULL)
- return -1;
- }
-
- if (!isStruct)
- size = union_size;
-
- /* Adjust the size according to the alignment requirements */
- size = ((size + total_align - 1) / total_align) * total_align;
-
- stgdict->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align,
- Py_ssize_t,
- unsigned short);
- stgdict->ffi_type_pointer.size = size;
-
- stgdict->size = size;
- stgdict->align = total_align;
- stgdict->length = len; /* ADD ffi_ofs? */
-
- /* We did check that this flag was NOT set above, it must not
- have been set until now. */
- if (stgdict->flags & DICTFLAG_FINAL) {
- PyErr_SetString(PyExc_AttributeError,
- "Structure or union cannot contain itself");
- return -1;
- }
- stgdict->flags |= DICTFLAG_FINAL;
-
- return MakeAnonFields(type);
+ if (isStruct && !isPacked) {
+ char *ptr = stgdict->format;
+ stgdict->format = _ctypes_alloc_format_string(stgdict->format, "}");
+ PyMem_Free(ptr);
+ if (stgdict->format == NULL)
+ return -1;
+ }
+
+ if (!isStruct)
+ size = union_size;
+
+ /* Adjust the size according to the alignment requirements */
+ size = ((size + total_align - 1) / total_align) * total_align;
+
+ stgdict->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align,
+ Py_ssize_t,
+ unsigned short);
+ stgdict->ffi_type_pointer.size = size;
+
+ stgdict->size = size;
+ stgdict->align = total_align;
+ stgdict->length = len; /* ADD ffi_ofs? */
+
+ /* We did check that this flag was NOT set above, it must not
+ have been set until now. */
+ if (stgdict->flags & DICTFLAG_FINAL) {
+ PyErr_SetString(PyExc_AttributeError,
+ "Structure or union cannot contain itself");
+ return -1;
+ }
+ stgdict->flags |= DICTFLAG_FINAL;
+
+ return MakeAnonFields(type);
}
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
index ff1276d..8561a2e 100644
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -22,7 +22,7 @@ static PyObject *PyCursesError;
/* Utility Functions */
/*
- * Check the return code from a curses function and return None
+ * Check the return code from a curses function and return None
* or raise an exception as appropriate.
*/
@@ -30,15 +30,15 @@ static PyObject *
PyCursesCheckERR(int code, char *fname)
{
if (code != ERR) {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
} else {
- if (fname == NULL) {
- PyErr_SetString(PyCursesError, catchall_ERR);
- } else {
- PyErr_Format(PyCursesError, "%s() returned ERR", fname);
- }
- return NULL;
+ if (fname == NULL) {
+ PyErr_SetString(PyCursesError, catchall_ERR);
+ } else {
+ PyErr_Format(PyCursesError, "%s() returned ERR", fname);
+ }
+ return NULL;
}
}
@@ -51,12 +51,12 @@ PyCursesCheckERR(int code, char *fname)
typedef struct {
PyObject_HEAD
PANEL *pan;
- PyCursesWindowObject *wo; /* for reference counts */
+ PyCursesWindowObject *wo; /* for reference counts */
} PyCursesPanelObject;
PyTypeObject PyCursesPanel_Type;
-#define PyCursesPanel_Check(v) (Py_TYPE(v) == &PyCursesPanel_Type)
+#define PyCursesPanel_Check(v) (Py_TYPE(v) == &PyCursesPanel_Type)
/* Some helper functions. The problem is that there's always a window
associated with a panel. To ensure that Python's GC doesn't pull
@@ -88,10 +88,10 @@ static int
insert_lop(PyCursesPanelObject *po)
{
list_of_panels *new;
-
+
if ((new = (list_of_panels *)malloc(sizeof(list_of_panels))) == NULL) {
- PyErr_NoMemory();
- return -1;
+ PyErr_NoMemory();
+ return -1;
}
new->po = po;
new->next = lop;
@@ -107,17 +107,17 @@ remove_lop(PyCursesPanelObject *po)
temp = lop;
if (temp->po == po) {
- lop = temp->next;
- free(temp);
- return;
+ lop = temp->next;
+ free(temp);
+ return;
}
while (temp->next == NULL || temp->next->po != po) {
- if (temp->next == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "remove_lop: can't find Panel Object");
- return;
- }
- temp = temp->next;
+ if (temp->next == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "remove_lop: can't find Panel Object");
+ return;
+ }
+ temp = temp->next;
}
n = temp->next->next;
free(temp->next);
@@ -131,7 +131,7 @@ find_po(PANEL *pan)
{
list_of_panels *temp;
for (temp = lop; temp->po->pan != pan; temp = temp->next)
- if (temp->next == NULL) return NULL; /* not found!? */
+ if (temp->next == NULL) return NULL; /* not found!? */
return temp->po;
}
@@ -179,9 +179,9 @@ PyCursesPanel_New(PANEL *pan, PyCursesWindowObject *wo)
if (po == NULL) return NULL;
po->pan = pan;
if (insert_lop(po) < 0) {
- po->wo = NULL;
- Py_DECREF(po);
- return NULL;
+ po->wo = NULL;
+ Py_DECREF(po);
+ return NULL;
}
po->wo = wo;
Py_INCREF(wo);
@@ -193,8 +193,8 @@ PyCursesPanel_Dealloc(PyCursesPanelObject *po)
{
(void)del_panel(po->pan);
if (po->wo != NULL) {
- Py_DECREF(po->wo);
- remove_lop(po);
+ Py_DECREF(po->wo);
+ remove_lop(po);
}
PyObject_DEL(po);
}
@@ -206,19 +206,19 @@ PyCursesPanel_above(PyCursesPanelObject *self)
{
PANEL *pan;
PyCursesPanelObject *po;
-
+
pan = panel_above(self->pan);
- if (pan == NULL) { /* valid output, it means the calling panel
- is on top of the stack */
- Py_INCREF(Py_None);
- return Py_None;
+ if (pan == NULL) { /* valid output, it means the calling panel
+ is on top of the stack */
+ Py_INCREF(Py_None);
+ return Py_None;
}
po = find_po(pan);
if (po == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "panel_above: can't find Panel Object");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,
+ "panel_above: can't find Panel Object");
+ return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
@@ -231,19 +231,19 @@ PyCursesPanel_below(PyCursesPanelObject *self)
{
PANEL *pan;
PyCursesPanelObject *po;
-
+
pan = panel_below(self->pan);
-
- if (pan == NULL) { /* valid output, it means the calling panel
- is on the bottom of the stack */
- Py_INCREF(Py_None);
- return Py_None;
+
+ if (pan == NULL) { /* valid output, it means the calling panel
+ is on the bottom of the stack */
+ Py_INCREF(Py_None);
+ return Py_None;
}
po = find_po(pan);
if (po == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "panel_below: can't find Panel Object");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,
+ "panel_below: can't find Panel Object");
+ return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
@@ -262,26 +262,26 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
PyCursesPanelObject *po;
PyCursesWindowObject *temp;
int rtn;
-
+
if (PyTuple_Size(args) != 1) {
- PyErr_SetString(PyExc_TypeError, "replace requires one argument");
- return NULL;
+ PyErr_SetString(PyExc_TypeError, "replace requires one argument");
+ return NULL;
}
if (!PyArg_ParseTuple(args, "O!;window object",
- &PyCursesWindow_Type, &temp))
- return NULL;
+ &PyCursesWindow_Type, &temp))
+ return NULL;
po = find_po(self->pan);
if (po == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "replace_panel: can't find Panel Object");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,
+ "replace_panel: can't find Panel Object");
+ return NULL;
}
rtn = replace_panel(self->pan, temp->win);
if (rtn == ERR) {
- PyErr_SetString(PyCursesError, "replace_panel() returned ERR");
- return NULL;
+ PyErr_SetString(PyCursesError, "replace_panel() returned ERR");
+ return NULL;
}
Py_DECREF(po->wo);
po->wo = temp;
@@ -302,11 +302,11 @@ static PyObject *
PyCursesPanel_userptr(PyCursesPanelObject *self)
{
PyObject *obj;
- PyCursesInitialised;
+ PyCursesInitialised;
obj = (PyObject *) panel_userptr(self->pan);
if (obj == NULL) {
- PyErr_SetString(PyCursesError, "no userptr set");
- return NULL;
+ PyErr_SetString(PyCursesError, "no userptr set");
+ return NULL;
}
Py_INCREF(obj);
@@ -329,40 +329,40 @@ static PyMethodDef PyCursesPanel_Methods[] = {
{"top", (PyCFunction)PyCursesPanel_top_panel, METH_NOARGS},
{"userptr", (PyCFunction)PyCursesPanel_userptr, METH_NOARGS},
{"window", (PyCFunction)PyCursesPanel_window, METH_NOARGS},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
/* -------------------------------------------------------*/
PyTypeObject PyCursesPanel_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_curses_panel.curses panel", /*tp_name*/
- sizeof(PyCursesPanelObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
+ "_curses_panel.curses panel", /*tp_name*/
+ sizeof(PyCursesPanelObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
/* methods */
(destructor)PyCursesPanel_Dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
PyCursesPanel_Methods, /*tp_methods*/
};
@@ -380,16 +380,16 @@ PyCurses_bottom_panel(PyObject *self)
pan = panel_above(NULL);
- if (pan == NULL) { /* valid output, it means
- there's no panel at all */
- Py_INCREF(Py_None);
- return Py_None;
+ if (pan == NULL) { /* valid output, it means
+ there's no panel at all */
+ Py_INCREF(Py_None);
+ return Py_None;
}
po = find_po(pan);
if (po == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "panel_above: can't find Panel Object");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,
+ "panel_above: can't find Panel Object");
+ return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
@@ -405,8 +405,8 @@ PyCurses_new_panel(PyObject *self, PyObject *args)
return NULL;
pan = new_panel(win->win);
if (pan == NULL) {
- PyErr_SetString(PyCursesError, catchall_NULL);
- return NULL;
+ PyErr_SetString(PyCursesError, catchall_NULL);
+ return NULL;
}
return (PyObject *)PyCursesPanel_New(pan, win);
}
@@ -421,28 +421,28 @@ PyCurses_top_panel(PyObject *self)
{
PANEL *pan;
PyCursesPanelObject *po;
-
+
PyCursesInitialised;
pan = panel_below(NULL);
- if (pan == NULL) { /* valid output, it means
- there's no panel at all */
- Py_INCREF(Py_None);
- return Py_None;
+ if (pan == NULL) { /* valid output, it means
+ there's no panel at all */
+ Py_INCREF(Py_None);
+ return Py_None;
}
po = find_po(pan);
if (po == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "panel_below: can't find Panel Object");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,
+ "panel_below: can't find Panel Object");
+ return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
}
static PyObject *PyCurses_update_panels(PyObject *self)
-{
+{
PyCursesInitialised;
update_panels();
Py_INCREF(Py_None);
@@ -457,22 +457,22 @@ static PyMethodDef PyCurses_methods[] = {
{"new_panel", (PyCFunction)PyCurses_new_panel, METH_VARARGS},
{"top_panel", (PyCFunction)PyCurses_top_panel, METH_NOARGS},
{"update_panels", (PyCFunction)PyCurses_update_panels, METH_NOARGS},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
/* Initialization function for the module */
static struct PyModuleDef _curses_panelmodule = {
- PyModuleDef_HEAD_INIT,
- "_curses_panel",
- NULL,
- -1,
- PyCurses_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_curses_panel",
+ NULL,
+ -1,
+ PyCurses_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -489,7 +489,7 @@ PyInit__curses_panel(void)
/* Create the module and add the functions */
m = PyModule_Create(&_curses_panelmodule);
if (m == NULL)
- return NULL;
+ return NULL;
d = PyModule_GetDict(m);
/* For exception _curses_panel.error */
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 885c3aa..827acce 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -33,9 +33,9 @@ static char *which_dbm = "Berkeley DB";
#endif
typedef struct {
- PyObject_HEAD
- int di_size; /* -1 means recompute */
- DBM *di_dbm;
+ PyObject_HEAD
+ int di_size; /* -1 means recompute */
+ DBM *di_dbm;
} dbmobject;
static PyTypeObject Dbmtype;
@@ -50,18 +50,18 @@ static PyObject *DbmError;
static PyObject *
newdbmobject(char *file, int flags, int mode)
{
- dbmobject *dp;
-
- dp = PyObject_New(dbmobject, &Dbmtype);
- if (dp == NULL)
- return NULL;
- dp->di_size = -1;
- if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
- PyErr_SetFromErrno(DbmError);
- Py_DECREF(dp);
- return NULL;
- }
- return (PyObject *)dp;
+ dbmobject *dp;
+
+ dp = PyObject_New(dbmobject, &Dbmtype);
+ if (dp == NULL)
+ return NULL;
+ dp->di_size = -1;
+ if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
+ PyErr_SetFromErrno(DbmError);
+ Py_DECREF(dp);
+ return NULL;
+ }
+ return (PyObject *)dp;
}
/* Methods */
@@ -69,294 +69,294 @@ newdbmobject(char *file, int flags, int mode)
static void
dbm_dealloc(register dbmobject *dp)
{
- if ( dp->di_dbm )
- dbm_close(dp->di_dbm);
- PyObject_Del(dp);
+ if ( dp->di_dbm )
+ dbm_close(dp->di_dbm);
+ PyObject_Del(dp);
}
static Py_ssize_t
dbm_length(dbmobject *dp)
{
- if (dp->di_dbm == NULL) {
- PyErr_SetString(DbmError, "DBM object has already been closed");
- return -1;
- }
- if ( dp->di_size < 0 ) {
- datum key;
- int size;
-
- size = 0;
- for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
- key = dbm_nextkey(dp->di_dbm))
- size++;
- dp->di_size = size;
- }
- return dp->di_size;
+ if (dp->di_dbm == NULL) {
+ PyErr_SetString(DbmError, "DBM object has already been closed");
+ return -1;
+ }
+ if ( dp->di_size < 0 ) {
+ datum key;
+ int size;
+
+ size = 0;
+ for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
+ key = dbm_nextkey(dp->di_dbm))
+ size++;
+ dp->di_size = size;
+ }
+ return dp->di_size;
}
static PyObject *
dbm_subscript(dbmobject *dp, register PyObject *key)
{
- datum drec, krec;
- Py_ssize_t tmp_size;
-
- if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
- return NULL;
-
- krec.dsize = tmp_size;
- check_dbmobject_open(dp);
- drec = dbm_fetch(dp->di_dbm, krec);
- if ( drec.dptr == 0 ) {
- PyErr_SetObject(PyExc_KeyError, key);
- return NULL;
- }
- if ( dbm_error(dp->di_dbm) ) {
- dbm_clearerr(dp->di_dbm);
- PyErr_SetString(DbmError, "");
- return NULL;
- }
- return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
+ datum drec, krec;
+ Py_ssize_t tmp_size;
+
+ if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
+ return NULL;
+
+ krec.dsize = tmp_size;
+ check_dbmobject_open(dp);
+ drec = dbm_fetch(dp->di_dbm, krec);
+ if ( drec.dptr == 0 ) {
+ PyErr_SetObject(PyExc_KeyError, key);
+ return NULL;
+ }
+ if ( dbm_error(dp->di_dbm) ) {
+ dbm_clearerr(dp->di_dbm);
+ PyErr_SetString(DbmError, "");
+ return NULL;
+ }
+ return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
}
static int
dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
{
- datum krec, drec;
- Py_ssize_t tmp_size;
-
- if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
- PyErr_SetString(PyExc_TypeError,
- "dbm mappings have bytes or string keys only");
- return -1;
- }
- krec.dsize = tmp_size;
- if (dp->di_dbm == NULL) {
- PyErr_SetString(DbmError, "DBM object has already been closed");
- return -1;
+ datum krec, drec;
+ Py_ssize_t tmp_size;
+
+ if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
+ PyErr_SetString(PyExc_TypeError,
+ "dbm mappings have bytes or string keys only");
+ return -1;
+ }
+ krec.dsize = tmp_size;
+ if (dp->di_dbm == NULL) {
+ PyErr_SetString(DbmError, "DBM object has already been closed");
+ return -1;
+ }
+ dp->di_size = -1;
+ if (w == NULL) {
+ if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
+ dbm_clearerr(dp->di_dbm);
+ PyErr_SetObject(PyExc_KeyError, v);
+ return -1;
+ }
+ } else {
+ if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
+ PyErr_SetString(PyExc_TypeError,
+ "dbm mappings have byte or string elements only");
+ return -1;
+ }
+ drec.dsize = tmp_size;
+ if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
+ dbm_clearerr(dp->di_dbm);
+ PyErr_SetString(DbmError,
+ "cannot add item to database");
+ return -1;
}
- dp->di_size = -1;
- if (w == NULL) {
- if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
- dbm_clearerr(dp->di_dbm);
- PyErr_SetObject(PyExc_KeyError, v);
- return -1;
- }
- } else {
- if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
- PyErr_SetString(PyExc_TypeError,
- "dbm mappings have byte or string elements only");
- return -1;
- }
- drec.dsize = tmp_size;
- if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
- dbm_clearerr(dp->di_dbm);
- PyErr_SetString(DbmError,
- "cannot add item to database");
- return -1;
- }
- }
- if ( dbm_error(dp->di_dbm) ) {
- dbm_clearerr(dp->di_dbm);
- PyErr_SetString(DbmError, "");
- return -1;
- }
- return 0;
+ }
+ if ( dbm_error(dp->di_dbm) ) {
+ dbm_clearerr(dp->di_dbm);
+ PyErr_SetString(DbmError, "");
+ return -1;
+ }
+ return 0;
}
static PyMappingMethods dbm_as_mapping = {
- (lenfunc)dbm_length, /*mp_length*/
- (binaryfunc)dbm_subscript, /*mp_subscript*/
- (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
+ (lenfunc)dbm_length, /*mp_length*/
+ (binaryfunc)dbm_subscript, /*mp_subscript*/
+ (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
};
static PyObject *
dbm__close(register dbmobject *dp, PyObject *unused)
{
- if (dp->di_dbm)
- dbm_close(dp->di_dbm);
- dp->di_dbm = NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (dp->di_dbm)
+ dbm_close(dp->di_dbm);
+ dp->di_dbm = NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
dbm_keys(register dbmobject *dp, PyObject *unused)
{
- register PyObject *v, *item;
- datum key;
- int err;
-
- check_dbmobject_open(dp);
- v = PyList_New(0);
- if (v == NULL)
- return NULL;
- for (key = dbm_firstkey(dp->di_dbm); key.dptr;
- key = dbm_nextkey(dp->di_dbm)) {
- item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
- if (item == NULL) {
- Py_DECREF(v);
- return NULL;
- }
- err = PyList_Append(v, item);
- Py_DECREF(item);
- if (err != 0) {
- Py_DECREF(v);
- return NULL;
- }
- }
- return v;
+ register PyObject *v, *item;
+ datum key;
+ int err;
+
+ check_dbmobject_open(dp);
+ v = PyList_New(0);
+ if (v == NULL)
+ return NULL;
+ for (key = dbm_firstkey(dp->di_dbm); key.dptr;
+ key = dbm_nextkey(dp->di_dbm)) {
+ item = PyBytes_FromStringAndSize(key.dptr, key.dsize);
+ if (item == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ err = PyList_Append(v, item);
+ Py_DECREF(item);
+ if (err != 0) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ }
+ return v;
}
static int
dbm_contains(PyObject *self, PyObject *arg)
{
- dbmobject *dp = (dbmobject *)self;
- datum key, val;
-
- if ((dp)->di_dbm == NULL) {
- PyErr_SetString(DbmError,
- "DBM object has already been closed");
- return -1;
- }
- if (PyUnicode_Check(arg)) {
- arg = _PyUnicode_AsDefaultEncodedString(arg, NULL);
- if (arg == NULL)
- return -1;
- }
- if (!PyBytes_Check(arg)) {
- PyErr_Format(PyExc_TypeError,
- "dbm key must be string, not %.100s",
- arg->ob_type->tp_name);
- return -1;
- }
- key.dptr = PyBytes_AS_STRING(arg);
- key.dsize = PyBytes_GET_SIZE(arg);
- val = dbm_fetch(dp->di_dbm, key);
- return val.dptr != NULL;
+ dbmobject *dp = (dbmobject *)self;
+ datum key, val;
+
+ if ((dp)->di_dbm == NULL) {
+ PyErr_SetString(DbmError,
+ "DBM object has already been closed");
+ return -1;
+ }
+ if (PyUnicode_Check(arg)) {
+ arg = _PyUnicode_AsDefaultEncodedString(arg, NULL);
+ if (arg == NULL)
+ return -1;
+ }
+ if (!PyBytes_Check(arg)) {
+ PyErr_Format(PyExc_TypeError,
+ "dbm key must be string, not %.100s",
+ arg->ob_type->tp_name);
+ return -1;
+ }
+ key.dptr = PyBytes_AS_STRING(arg);
+ key.dsize = PyBytes_GET_SIZE(arg);
+ val = dbm_fetch(dp->di_dbm, key);
+ return val.dptr != NULL;
}
static PySequenceMethods dbm_as_sequence = {
- 0, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- dbm_contains, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ dbm_contains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
};
static PyObject *
dbm_get(register dbmobject *dp, PyObject *args)
{
- datum key, val;
- PyObject *defvalue = Py_None;
- char *tmp_ptr;
- Py_ssize_t tmp_size;
-
- if (!PyArg_ParseTuple(args, "s#|O:get",
- &tmp_ptr, &tmp_size, &defvalue))
- return NULL;
- key.dptr = tmp_ptr;
- key.dsize = tmp_size;
- check_dbmobject_open(dp);
- val = dbm_fetch(dp->di_dbm, key);
- if (val.dptr != NULL)
- return PyBytes_FromStringAndSize(val.dptr, val.dsize);
- else {
- Py_INCREF(defvalue);
- return defvalue;
- }
+ datum key, val;
+ PyObject *defvalue = Py_None;
+ char *tmp_ptr;
+ Py_ssize_t tmp_size;
+
+ if (!PyArg_ParseTuple(args, "s#|O:get",
+ &tmp_ptr, &tmp_size, &defvalue))
+ return NULL;
+ key.dptr = tmp_ptr;
+ key.dsize = tmp_size;
+ check_dbmobject_open(dp);
+ val = dbm_fetch(dp->di_dbm, key);
+ if (val.dptr != NULL)
+ return PyBytes_FromStringAndSize(val.dptr, val.dsize);
+ else {
+ Py_INCREF(defvalue);
+ return defvalue;
+ }
}
static PyObject *
dbm_setdefault(register dbmobject *dp, PyObject *args)
{
- datum key, val;
- PyObject *defvalue = NULL;
- char *tmp_ptr;
- Py_ssize_t tmp_size;
-
- if (!PyArg_ParseTuple(args, "s#|O:setdefault",
- &tmp_ptr, &tmp_size, &defvalue))
- return NULL;
- key.dptr = tmp_ptr;
- key.dsize = tmp_size;
- check_dbmobject_open(dp);
- val = dbm_fetch(dp->di_dbm, key);
- if (val.dptr != NULL)
- return PyBytes_FromStringAndSize(val.dptr, val.dsize);
- if (defvalue == NULL) {
- defvalue = PyBytes_FromStringAndSize(NULL, 0);
- if (defvalue == NULL)
- return NULL;
- val.dptr = NULL;
- val.dsize = 0;
- }
- else {
- if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) {
- PyErr_SetString(PyExc_TypeError,
- "dbm mappings have byte string elements only");
- return NULL;
- }
- val.dsize = tmp_size;
- Py_INCREF(defvalue);
- }
- if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
- dbm_clearerr(dp->di_dbm);
- PyErr_SetString(DbmError, "cannot add item to database");
- Py_DECREF(defvalue);
- return NULL;
- }
- return defvalue;
+ datum key, val;
+ PyObject *defvalue = NULL;
+ char *tmp_ptr;
+ Py_ssize_t tmp_size;
+
+ if (!PyArg_ParseTuple(args, "s#|O:setdefault",
+ &tmp_ptr, &tmp_size, &defvalue))
+ return NULL;
+ key.dptr = tmp_ptr;
+ key.dsize = tmp_size;
+ check_dbmobject_open(dp);
+ val = dbm_fetch(dp->di_dbm, key);
+ if (val.dptr != NULL)
+ return PyBytes_FromStringAndSize(val.dptr, val.dsize);
+ if (defvalue == NULL) {
+ defvalue = PyBytes_FromStringAndSize(NULL, 0);
+ if (defvalue == NULL)
+ return NULL;
+ val.dptr = NULL;
+ val.dsize = 0;
+ }
+ else {
+ if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) {
+ PyErr_SetString(PyExc_TypeError,
+ "dbm mappings have byte string elements only");
+ return NULL;
+ }
+ val.dsize = tmp_size;
+ Py_INCREF(defvalue);
+ }
+ if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
+ dbm_clearerr(dp->di_dbm);
+ PyErr_SetString(DbmError, "cannot add item to database");
+ Py_DECREF(defvalue);
+ return NULL;
+ }
+ return defvalue;
}
static PyMethodDef dbm_methods[] = {
- {"close", (PyCFunction)dbm__close, METH_NOARGS,
- "close()\nClose the database."},
- {"keys", (PyCFunction)dbm_keys, METH_NOARGS,
- "keys() -> list\nReturn a list of all keys in the database."},
- {"get", (PyCFunction)dbm_get, METH_VARARGS,
- "get(key[, default]) -> value\n"
- "Return the value for key if present, otherwise default."},
- {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
- "setdefault(key[, default]) -> value\n"
- "Return the value for key if present, otherwise default. If key\n"
- "is not in the database, it is inserted with default as the value."},
- {NULL, NULL} /* sentinel */
+ {"close", (PyCFunction)dbm__close, METH_NOARGS,
+ "close()\nClose the database."},
+ {"keys", (PyCFunction)dbm_keys, METH_NOARGS,
+ "keys() -> list\nReturn a list of all keys in the database."},
+ {"get", (PyCFunction)dbm_get, METH_VARARGS,
+ "get(key[, default]) -> value\n"
+ "Return the value for key if present, otherwise default."},
+ {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
+ "setdefault(key[, default]) -> value\n"
+ "Return the value for key if present, otherwise default. If key\n"
+ "is not in the database, it is inserted with default as the value."},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject Dbmtype = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_dbm.dbm",
- sizeof(dbmobject),
- 0,
- (destructor)dbm_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- &dbm_as_sequence, /*tp_as_sequence*/
- &dbm_as_mapping, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- dbm_methods, /*tp_methods*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_dbm.dbm",
+ sizeof(dbmobject),
+ 0,
+ (destructor)dbm_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ &dbm_as_sequence, /*tp_as_sequence*/
+ &dbm_as_mapping, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ dbm_methods, /*tp_methods*/
};
/* ----------------------------------------------------------------- */
@@ -364,74 +364,74 @@ static PyTypeObject Dbmtype = {
static PyObject *
dbmopen(PyObject *self, PyObject *args)
{
- char *name;
- char *flags = "r";
- int iflags;
- int mode = 0666;
-
- if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
- return NULL;
- if ( strcmp(flags, "r") == 0 )
- iflags = O_RDONLY;
- else if ( strcmp(flags, "w") == 0 )
- iflags = O_RDWR;
- else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
- iflags = O_RDWR|O_CREAT;
- else if ( strcmp(flags, "c") == 0 )
- iflags = O_RDWR|O_CREAT;
- else if ( strcmp(flags, "n") == 0 )
- iflags = O_RDWR|O_CREAT|O_TRUNC;
- else {
- PyErr_SetString(DbmError,
- "arg 2 to open should be 'r', 'w', 'c', or 'n'");
- return NULL;
- }
- return newdbmobject(name, iflags, mode);
+ char *name;
+ char *flags = "r";
+ int iflags;
+ int mode = 0666;
+
+ if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
+ return NULL;
+ if ( strcmp(flags, "r") == 0 )
+ iflags = O_RDONLY;
+ else if ( strcmp(flags, "w") == 0 )
+ iflags = O_RDWR;
+ else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
+ iflags = O_RDWR|O_CREAT;
+ else if ( strcmp(flags, "c") == 0 )
+ iflags = O_RDWR|O_CREAT;
+ else if ( strcmp(flags, "n") == 0 )
+ iflags = O_RDWR|O_CREAT|O_TRUNC;
+ else {
+ PyErr_SetString(DbmError,
+ "arg 2 to open should be 'r', 'w', 'c', or 'n'");
+ return NULL;
+ }
+ return newdbmobject(name, iflags, mode);
}
static PyMethodDef dbmmodule_methods[] = {
- { "open", (PyCFunction)dbmopen, METH_VARARGS,
- "open(path[, flag[, mode]]) -> mapping\n"
- "Return a database object."},
- { 0, 0 },
+ { "open", (PyCFunction)dbmopen, METH_VARARGS,
+ "open(path[, flag[, mode]]) -> mapping\n"
+ "Return a database object."},
+ { 0, 0 },
};
static struct PyModuleDef _dbmmodule = {
- PyModuleDef_HEAD_INIT,
- "_dbm",
- NULL,
- -1,
- dbmmodule_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_dbm",
+ NULL,
+ -1,
+ dbmmodule_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__dbm(void) {
- PyObject *m, *d, *s;
-
- if (PyType_Ready(&Dbmtype) < 0)
- return NULL;
- m = PyModule_Create(&_dbmmodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
- if (DbmError == NULL)
- DbmError = PyErr_NewException("_dbm.error",
- PyExc_IOError, NULL);
- s = PyUnicode_FromString(which_dbm);
- if (s != NULL) {
- PyDict_SetItemString(d, "library", s);
- Py_DECREF(s);
- }
- if (DbmError != NULL)
- PyDict_SetItemString(d, "error", DbmError);
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- m = NULL;
- }
- return m;
+ PyObject *m, *d, *s;
+
+ if (PyType_Ready(&Dbmtype) < 0)
+ return NULL;
+ m = PyModule_Create(&_dbmmodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+ if (DbmError == NULL)
+ DbmError = PyErr_NewException("_dbm.error",
+ PyExc_IOError, NULL);
+ s = PyUnicode_FromString(which_dbm);
+ if (s != NULL) {
+ PyDict_SetItemString(d, "library", s);
+ Py_DECREF(s);
+ }
+ if (DbmError != NULL)
+ PyDict_SetItemString(d, "error", DbmError);
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ m = NULL;
+ }
+ return m;
}
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index a9cdcb8..bf2ea3b 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -2,7 +2,7 @@
#include "Python.h"
#include "structmember.h"
-/* _functools module written and maintained
+/* _functools module written and maintained
by Hye-Shik Chang <perky@FreeBSD.org>
with adaptations by Raymond Hettinger <python@rcn.com>
Copyright (c) 2004, 2005, 2006 Python Software Foundation.
@@ -12,12 +12,12 @@
/* partial object **********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *fn;
- PyObject *args;
- PyObject *kw;
- PyObject *dict;
- PyObject *weakreflist; /* List of weak references */
+ PyObject_HEAD
+ PyObject *fn;
+ PyObject *args;
+ PyObject *kw;
+ PyObject *dict;
+ PyObject *weakreflist; /* List of weak references */
} partialobject;
static PyTypeObject partial_type;
@@ -25,175 +25,175 @@ static PyTypeObject partial_type;
static PyObject *
partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *func;
- partialobject *pto;
-
- if (PyTuple_GET_SIZE(args) < 1) {
- PyErr_SetString(PyExc_TypeError,
- "type 'partial' takes at least one argument");
- return NULL;
- }
-
- func = PyTuple_GET_ITEM(args, 0);
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError,
- "the first argument must be callable");
- return NULL;
- }
-
- /* create partialobject structure */
- pto = (partialobject *)type->tp_alloc(type, 0);
- if (pto == NULL)
- return NULL;
-
- pto->fn = func;
- Py_INCREF(func);
- pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
- if (pto->args == NULL) {
- pto->kw = NULL;
- Py_DECREF(pto);
- return NULL;
- }
- if (kw != NULL) {
- pto->kw = PyDict_Copy(kw);
- if (pto->kw == NULL) {
- Py_DECREF(pto);
- return NULL;
- }
- } else {
- pto->kw = Py_None;
- Py_INCREF(Py_None);
- }
-
- pto->weakreflist = NULL;
- pto->dict = NULL;
-
- return (PyObject *)pto;
+ PyObject *func;
+ partialobject *pto;
+
+ if (PyTuple_GET_SIZE(args) < 1) {
+ PyErr_SetString(PyExc_TypeError,
+ "type 'partial' takes at least one argument");
+ return NULL;
+ }
+
+ func = PyTuple_GET_ITEM(args, 0);
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError,
+ "the first argument must be callable");
+ return NULL;
+ }
+
+ /* create partialobject structure */
+ pto = (partialobject *)type->tp_alloc(type, 0);
+ if (pto == NULL)
+ return NULL;
+
+ pto->fn = func;
+ Py_INCREF(func);
+ pto->args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
+ if (pto->args == NULL) {
+ pto->kw = NULL;
+ Py_DECREF(pto);
+ return NULL;
+ }
+ if (kw != NULL) {
+ pto->kw = PyDict_Copy(kw);
+ if (pto->kw == NULL) {
+ Py_DECREF(pto);
+ return NULL;
+ }
+ } else {
+ pto->kw = Py_None;
+ Py_INCREF(Py_None);
+ }
+
+ pto->weakreflist = NULL;
+ pto->dict = NULL;
+
+ return (PyObject *)pto;
}
static void
partial_dealloc(partialobject *pto)
{
- PyObject_GC_UnTrack(pto);
- if (pto->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) pto);
- Py_XDECREF(pto->fn);
- Py_XDECREF(pto->args);
- Py_XDECREF(pto->kw);
- Py_XDECREF(pto->dict);
- Py_TYPE(pto)->tp_free(pto);
+ PyObject_GC_UnTrack(pto);
+ if (pto->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) pto);
+ Py_XDECREF(pto->fn);
+ Py_XDECREF(pto->args);
+ Py_XDECREF(pto->kw);
+ Py_XDECREF(pto->dict);
+ Py_TYPE(pto)->tp_free(pto);
}
static PyObject *
partial_call(partialobject *pto, PyObject *args, PyObject *kw)
{
- PyObject *ret;
- PyObject *argappl = NULL, *kwappl = NULL;
-
- assert (PyCallable_Check(pto->fn));
- assert (PyTuple_Check(pto->args));
- assert (pto->kw == Py_None || PyDict_Check(pto->kw));
-
- if (PyTuple_GET_SIZE(pto->args) == 0) {
- argappl = args;
- Py_INCREF(args);
- } else if (PyTuple_GET_SIZE(args) == 0) {
- argappl = pto->args;
- Py_INCREF(pto->args);
- } else {
- argappl = PySequence_Concat(pto->args, args);
- if (argappl == NULL)
- return NULL;
- }
-
- if (pto->kw == Py_None) {
- kwappl = kw;
- Py_XINCREF(kw);
- } else {
- kwappl = PyDict_Copy(pto->kw);
- if (kwappl == NULL) {
- Py_DECREF(argappl);
- return NULL;
- }
- if (kw != NULL) {
- if (PyDict_Merge(kwappl, kw, 1) != 0) {
- Py_DECREF(argappl);
- Py_DECREF(kwappl);
- return NULL;
- }
- }
- }
-
- ret = PyObject_Call(pto->fn, argappl, kwappl);
- Py_DECREF(argappl);
- Py_XDECREF(kwappl);
- return ret;
+ PyObject *ret;
+ PyObject *argappl = NULL, *kwappl = NULL;
+
+ assert (PyCallable_Check(pto->fn));
+ assert (PyTuple_Check(pto->args));
+ assert (pto->kw == Py_None || PyDict_Check(pto->kw));
+
+ if (PyTuple_GET_SIZE(pto->args) == 0) {
+ argappl = args;
+ Py_INCREF(args);
+ } else if (PyTuple_GET_SIZE(args) == 0) {
+ argappl = pto->args;
+ Py_INCREF(pto->args);
+ } else {
+ argappl = PySequence_Concat(pto->args, args);
+ if (argappl == NULL)
+ return NULL;
+ }
+
+ if (pto->kw == Py_None) {
+ kwappl = kw;
+ Py_XINCREF(kw);
+ } else {
+ kwappl = PyDict_Copy(pto->kw);
+ if (kwappl == NULL) {
+ Py_DECREF(argappl);
+ return NULL;
+ }
+ if (kw != NULL) {
+ if (PyDict_Merge(kwappl, kw, 1) != 0) {
+ Py_DECREF(argappl);
+ Py_DECREF(kwappl);
+ return NULL;
+ }
+ }
+ }
+
+ ret = PyObject_Call(pto->fn, argappl, kwappl);
+ Py_DECREF(argappl);
+ Py_XDECREF(kwappl);
+ return ret;
}
static int
partial_traverse(partialobject *pto, visitproc visit, void *arg)
{
- Py_VISIT(pto->fn);
- Py_VISIT(pto->args);
- Py_VISIT(pto->kw);
- Py_VISIT(pto->dict);
- return 0;
+ Py_VISIT(pto->fn);
+ Py_VISIT(pto->args);
+ Py_VISIT(pto->kw);
+ Py_VISIT(pto->dict);
+ return 0;
}
PyDoc_STRVAR(partial_doc,
"partial(func, *args, **keywords) - new function with partial application\n\
- of the given arguments and keywords.\n");
+ of the given arguments and keywords.\n");
#define OFF(x) offsetof(partialobject, x)
static PyMemberDef partial_memberlist[] = {
- {"func", T_OBJECT, OFF(fn), READONLY,
- "function object to use in future partial calls"},
- {"args", T_OBJECT, OFF(args), READONLY,
- "tuple of arguments to future partial calls"},
- {"keywords", T_OBJECT, OFF(kw), READONLY,
- "dictionary of keyword arguments to future partial calls"},
- {NULL} /* Sentinel */
+ {"func", T_OBJECT, OFF(fn), READONLY,
+ "function object to use in future partial calls"},
+ {"args", T_OBJECT, OFF(args), READONLY,
+ "tuple of arguments to future partial calls"},
+ {"keywords", T_OBJECT, OFF(kw), READONLY,
+ "dictionary of keyword arguments to future partial calls"},
+ {NULL} /* Sentinel */
};
static PyObject *
partial_get_dict(partialobject *pto)
{
- if (pto->dict == NULL) {
- pto->dict = PyDict_New();
- if (pto->dict == NULL)
- return NULL;
- }
- Py_INCREF(pto->dict);
- return pto->dict;
+ if (pto->dict == NULL) {
+ pto->dict = PyDict_New();
+ if (pto->dict == NULL)
+ return NULL;
+ }
+ Py_INCREF(pto->dict);
+ return pto->dict;
}
static int
partial_set_dict(partialobject *pto, PyObject *value)
{
- PyObject *tmp;
-
- /* It is illegal to del p.__dict__ */
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "a partial object's dictionary may not be deleted");
- return -1;
- }
- /* Can only set __dict__ to a dictionary */
- if (!PyDict_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "setting partial object's dictionary to a non-dict");
- return -1;
- }
- tmp = pto->dict;
- Py_INCREF(value);
- pto->dict = value;
- Py_XDECREF(tmp);
- return 0;
+ PyObject *tmp;
+
+ /* It is illegal to del p.__dict__ */
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "a partial object's dictionary may not be deleted");
+ return -1;
+ }
+ /* Can only set __dict__ to a dictionary */
+ if (!PyDict_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "setting partial object's dictionary to a non-dict");
+ return -1;
+ }
+ tmp = pto->dict;
+ Py_INCREF(value);
+ pto->dict = value;
+ Py_XDECREF(tmp);
+ return 0;
}
static PyGetSetDef partial_getsetlist[] = {
- {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
- {NULL} /* Sentinel */
+ {"__dict__", (getter)partial_get_dict, (setter)partial_set_dict},
+ {NULL} /* Sentinel */
};
/* Pickle strategy:
@@ -206,85 +206,85 @@ static PyGetSetDef partial_getsetlist[] = {
static PyObject *
partial_reduce(partialobject *pto, PyObject *unused)
{
- return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
- pto->args, pto->kw,
- pto->dict ? pto->dict : Py_None);
+ return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
+ pto->args, pto->kw,
+ pto->dict ? pto->dict : Py_None);
}
static PyObject *
partial_setstate(partialobject *pto, PyObject *args)
{
- PyObject *fn, *fnargs, *kw, *dict;
- if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
- &fn, &fnargs, &kw, &dict))
- return NULL;
- Py_XDECREF(pto->fn);
- Py_XDECREF(pto->args);
- Py_XDECREF(pto->kw);
- Py_XDECREF(pto->dict);
- pto->fn = fn;
- pto->args = fnargs;
- pto->kw = kw;
- if (dict != Py_None) {
- pto->dict = dict;
- Py_INCREF(dict);
- } else {
- pto->dict = NULL;
- }
- Py_INCREF(fn);
- Py_INCREF(fnargs);
- Py_INCREF(kw);
- Py_RETURN_NONE;
+ PyObject *fn, *fnargs, *kw, *dict;
+ if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
+ &fn, &fnargs, &kw, &dict))
+ return NULL;
+ Py_XDECREF(pto->fn);
+ Py_XDECREF(pto->args);
+ Py_XDECREF(pto->kw);
+ Py_XDECREF(pto->dict);
+ pto->fn = fn;
+ pto->args = fnargs;
+ pto->kw = kw;
+ if (dict != Py_None) {
+ pto->dict = dict;
+ Py_INCREF(dict);
+ } else {
+ pto->dict = NULL;
+ }
+ Py_INCREF(fn);
+ Py_INCREF(fnargs);
+ Py_INCREF(kw);
+ Py_RETURN_NONE;
}
static PyMethodDef partial_methods[] = {
- {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
- {"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
+ {"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject partial_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "functools.partial", /* tp_name */
- sizeof(partialobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)partial_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)partial_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- partial_doc, /* tp_doc */
- (traverseproc)partial_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- partial_methods, /* tp_methods */
- partial_memberlist, /* tp_members */
- partial_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- offsetof(partialobject, dict), /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- partial_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "functools.partial", /* tp_name */
+ sizeof(partialobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)partial_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)partial_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ partial_doc, /* tp_doc */
+ (traverseproc)partial_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(partialobject, weakreflist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ partial_methods, /* tp_methods */
+ partial_memberlist, /* tp_members */
+ partial_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ offsetof(partialobject, dict), /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ partial_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -293,64 +293,64 @@ static PyTypeObject partial_type = {
static PyObject *
functools_reduce(PyObject *self, PyObject *args)
{
- PyObject *seq, *func, *result = NULL, *it;
-
- if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
- return NULL;
- if (result != NULL)
- Py_INCREF(result);
-
- it = PyObject_GetIter(seq);
- if (it == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "reduce() arg 2 must support iteration");
- Py_XDECREF(result);
- return NULL;
- }
-
- if ((args = PyTuple_New(2)) == NULL)
- goto Fail;
-
- for (;;) {
- PyObject *op2;
-
- if (args->ob_refcnt > 1) {
- Py_DECREF(args);
- if ((args = PyTuple_New(2)) == NULL)
- goto Fail;
- }
-
- op2 = PyIter_Next(it);
- if (op2 == NULL) {
- if (PyErr_Occurred())
- goto Fail;
- break;
- }
-
- if (result == NULL)
- result = op2;
- else {
- PyTuple_SetItem(args, 0, result);
- PyTuple_SetItem(args, 1, op2);
- if ((result = PyEval_CallObject(func, args)) == NULL)
- goto Fail;
- }
- }
-
- Py_DECREF(args);
-
- if (result == NULL)
- PyErr_SetString(PyExc_TypeError,
- "reduce() of empty sequence with no initial value");
-
- Py_DECREF(it);
- return result;
+ PyObject *seq, *func, *result = NULL, *it;
+
+ if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
+ return NULL;
+ if (result != NULL)
+ Py_INCREF(result);
+
+ it = PyObject_GetIter(seq);
+ if (it == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "reduce() arg 2 must support iteration");
+ Py_XDECREF(result);
+ return NULL;
+ }
+
+ if ((args = PyTuple_New(2)) == NULL)
+ goto Fail;
+
+ for (;;) {
+ PyObject *op2;
+
+ if (args->ob_refcnt > 1) {
+ Py_DECREF(args);
+ if ((args = PyTuple_New(2)) == NULL)
+ goto Fail;
+ }
+
+ op2 = PyIter_Next(it);
+ if (op2 == NULL) {
+ if (PyErr_Occurred())
+ goto Fail;
+ break;
+ }
+
+ if (result == NULL)
+ result = op2;
+ else {
+ PyTuple_SetItem(args, 0, result);
+ PyTuple_SetItem(args, 1, op2);
+ if ((result = PyEval_CallObject(func, args)) == NULL)
+ goto Fail;
+ }
+ }
+
+ Py_DECREF(args);
+
+ if (result == NULL)
+ PyErr_SetString(PyExc_TypeError,
+ "reduce() of empty sequence with no initial value");
+
+ Py_DECREF(it);
+ return result;
Fail:
- Py_XDECREF(args);
- Py_XDECREF(result);
- Py_DECREF(it);
- return NULL;
+ Py_XDECREF(args);
+ Py_XDECREF(result);
+ Py_DECREF(it);
+ return NULL;
}
PyDoc_STRVAR(functools_reduce_doc,
@@ -369,47 +369,47 @@ PyDoc_STRVAR(module_doc,
"Tools that operate on functions.");
static PyMethodDef module_methods[] = {
- {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
- {NULL, NULL} /* sentinel */
+ {"reduce", functools_reduce, METH_VARARGS, functools_reduce_doc},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef _functoolsmodule = {
- PyModuleDef_HEAD_INIT,
- "_functools",
- module_doc,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_functools",
+ module_doc,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__functools(void)
{
- int i;
- PyObject *m;
- char *name;
- PyTypeObject *typelist[] = {
- &partial_type,
- NULL
- };
-
- m = PyModule_Create(&_functoolsmodule);
- if (m == NULL)
- return NULL;
-
- for (i=0 ; typelist[i] != NULL ; i++) {
- if (PyType_Ready(typelist[i]) < 0) {
- Py_DECREF(m);
- return NULL;
- }
- name = strchr(typelist[i]->tp_name, '.');
- assert (name != NULL);
- Py_INCREF(typelist[i]);
- PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
- }
- return m;
+ int i;
+ PyObject *m;
+ char *name;
+ PyTypeObject *typelist[] = {
+ &partial_type,
+ NULL
+ };
+
+ m = PyModule_Create(&_functoolsmodule);
+ if (m == NULL)
+ return NULL;
+
+ for (i=0 ; typelist[i] != NULL ; i++) {
+ if (PyType_Ready(typelist[i]) < 0) {
+ Py_DECREF(m);
+ return NULL;
+ }
+ name = strchr(typelist[i]->tp_name, '.');
+ assert (name != NULL);
+ Py_INCREF(typelist[i]);
+ PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
+ }
+ return m;
}
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index de453ac..f15fefd 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -30,7 +30,7 @@ supported.");
typedef struct {
PyObject_HEAD
- int di_size; /* -1 means recompute */
+ int di_size; /* -1 means recompute */
GDBM_FILE di_dbm;
} dbmobject;
@@ -177,7 +177,7 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
}
static PyMappingMethods dbm_as_mapping = {
- (lenfunc)dbm_length, /*mp_length*/
+ (lenfunc)dbm_length, /*mp_length*/
(binaryfunc)dbm_subscript, /*mp_subscript*/
(objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
};
@@ -247,15 +247,15 @@ dbm_contains(PyObject *self, PyObject *arg)
datum key;
if ((dp)->di_dbm == NULL) {
- PyErr_SetString(DbmError,
- "GDBM object has already been closed");
- return -1;
+ PyErr_SetString(DbmError,
+ "GDBM object has already been closed");
+ return -1;
}
if (!PyBytes_Check(arg)) {
- PyErr_Format(PyExc_TypeError,
- "gdbm key must be bytes, not %.100s",
- arg->ob_type->tp_name);
- return -1;
+ PyErr_Format(PyExc_TypeError,
+ "gdbm key must be bytes, not %.100s",
+ arg->ob_type->tp_name);
+ return -1;
}
key.dptr = PyBytes_AS_STRING(arg);
key.dsize = PyBytes_GET_SIZE(arg);
@@ -263,16 +263,16 @@ dbm_contains(PyObject *self, PyObject *arg)
}
static PySequenceMethods dbm_as_sequence = {
- 0, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- dbm_contains, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ dbm_contains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
};
PyDoc_STRVAR(dbm_firstkey__doc__,
@@ -372,13 +372,13 @@ dbm_sync(register dbmobject *dp, PyObject *unused)
}
static PyMethodDef dbm_methods[] = {
- {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__},
- {"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__},
+ {"close", (PyCFunction)dbm_close, METH_NOARGS, dbm_close__doc__},
+ {"keys", (PyCFunction)dbm_keys, METH_NOARGS, dbm_keys__doc__},
{"firstkey", (PyCFunction)dbm_firstkey,METH_NOARGS, dbm_firstkey__doc__},
- {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__},
+ {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__},
{"reorganize",(PyCFunction)dbm_reorganize,METH_NOARGS, dbm_reorganize__doc__},
{"sync", (PyCFunction)dbm_sync, METH_NOARGS, dbm_sync__doc__},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject Dbmtype = {
@@ -486,7 +486,7 @@ dbmopen(PyObject *self, PyObject *args)
#endif
default:
PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.",
- *flags);
+ *flags);
PyErr_SetString(DbmError, buf);
return NULL;
}
@@ -514,15 +514,15 @@ static PyMethodDef dbmmodule_methods[] = {
static struct PyModuleDef _gdbmmodule = {
- PyModuleDef_HEAD_INIT,
- "_gdbm",
- gdbmmodule__doc__,
- -1,
- dbmmodule_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_gdbm",
+ gdbmmodule__doc__,
+ -1,
+ dbmmodule_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -530,10 +530,10 @@ PyInit__gdbm(void) {
PyObject *m, *d, *s;
if (PyType_Ready(&Dbmtype) < 0)
- return NULL;
+ return NULL;
m = PyModule_Create(&_gdbmmodule);
if (m == NULL)
- return NULL;
+ return NULL;
d = PyModule_GetDict(m);
DbmError = PyErr_NewException("_gdbm.error", PyExc_IOError, NULL);
if (DbmError != NULL) {
diff --git a/Modules/_gestalt.c b/Modules/_gestalt.c
index 462a6d2..a45780f 100644
--- a/Modules/_gestalt.c
+++ b/Modules/_gestalt.c
@@ -4,10 +4,10 @@ The Netherlands.
All Rights Reserved
-Permission to use, copy, modify, and distribute this software and its
-documentation for any purpose and without fee is hereby granted,
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
-both that copyright notice and this permission notice appear in
+both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
@@ -34,9 +34,9 @@ convert_to_OSType(PyObject *v, OSType *pr)
{
uint32_t tmp;
if (!PyUnicode_Check(v) || PyUnicode_GetSize(v) != 4) {
- PyErr_SetString(PyExc_TypeError,
- "OSType arg must be string of 4 chars");
- return 0;
+ PyErr_SetString(PyExc_TypeError,
+ "OSType arg must be string of 4 chars");
+ return 0;
}
memcpy((char *)&tmp, _PyUnicode_AsString(v), 4);
*pr = (OSType)ntohl(tmp);
@@ -50,12 +50,12 @@ gestalt_gestalt(PyObject *self, PyObject *args)
OSType selector;
SInt32 response;
if (!PyArg_ParseTuple(args, "O&", convert_to_OSType, &selector))
- return NULL;
+ return NULL;
iErr = Gestalt(selector, &response);
if (iErr != 0) {
- PyErr_SetString(PyExc_OSError,
- "non-zero exit code!");
- return NULL;
+ PyErr_SetString(PyExc_OSError,
+ "non-zero exit code!");
+ return NULL;
}
return PyLong_FromLong(response);
}
@@ -66,19 +66,19 @@ static struct PyMethodDef gestalt_methods[] = {
};
static struct PyModuleDef gestaltmodule = {
- PyModuleDef_HEAD_INIT,
- "_gestalt",
- NULL,
- -1,
- gestalt_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_gestalt",
+ NULL,
+ -1,
+ gestalt_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__gestalt(void)
{
- return PyModule_Create(&gestaltmodule);
+ return PyModule_Create(&gestaltmodule);
}
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
index 7921823..366fb3d 100644
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -1,4 +1,4 @@
-/* Drop in replacement for heapq.py
+/* Drop in replacement for heapq.py
C implementation derived directly from heapq.py in Py2.3
which was written by Kevin O'Connor, augmented by Tim Peters,
@@ -11,115 +11,115 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
static int
cmp_lt(PyObject *x, PyObject *y)
{
- return PyObject_RichCompareBool(x, y, Py_LT);
+ return PyObject_RichCompareBool(x, y, Py_LT);
}
static int
_siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
- PyObject *newitem, *parent;
- int cmp;
- Py_ssize_t parentpos;
-
- assert(PyList_Check(heap));
- if (pos >= PyList_GET_SIZE(heap)) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return -1;
- }
-
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
- /* Follow the path to the root, moving parents down until finding
- a place newitem fits. */
- while (pos > startpos){
- parentpos = (pos - 1) >> 1;
- parent = PyList_GET_ITEM(heap, parentpos);
- cmp = cmp_lt(newitem, parent);
- if (cmp == -1) {
- Py_DECREF(newitem);
- return -1;
- }
- if (cmp == 0)
- break;
- Py_INCREF(parent);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, parent);
- pos = parentpos;
- }
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return 0;
+ PyObject *newitem, *parent;
+ int cmp;
+ Py_ssize_t parentpos;
+
+ assert(PyList_Check(heap));
+ if (pos >= PyList_GET_SIZE(heap)) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return -1;
+ }
+
+ newitem = PyList_GET_ITEM(heap, pos);
+ Py_INCREF(newitem);
+ /* Follow the path to the root, moving parents down until finding
+ a place newitem fits. */
+ while (pos > startpos){
+ parentpos = (pos - 1) >> 1;
+ parent = PyList_GET_ITEM(heap, parentpos);
+ cmp = cmp_lt(newitem, parent);
+ if (cmp == -1) {
+ Py_DECREF(newitem);
+ return -1;
+ }
+ if (cmp == 0)
+ break;
+ Py_INCREF(parent);
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, parent);
+ pos = parentpos;
+ }
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, newitem);
+ return 0;
}
static int
_siftup(PyListObject *heap, Py_ssize_t pos)
{
- Py_ssize_t startpos, endpos, childpos, rightpos;
- int cmp;
- PyObject *newitem, *tmp;
-
- assert(PyList_Check(heap));
- endpos = PyList_GET_SIZE(heap);
- startpos = pos;
- if (pos >= endpos) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return -1;
- }
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
-
- /* Bubble up the smaller child until hitting a leaf. */
- childpos = 2*pos + 1; /* leftmost child position */
- while (childpos < endpos) {
- /* Set childpos to index of smaller child. */
- rightpos = childpos + 1;
- if (rightpos < endpos) {
- cmp = cmp_lt(
- PyList_GET_ITEM(heap, childpos),
- PyList_GET_ITEM(heap, rightpos));
- if (cmp == -1) {
- Py_DECREF(newitem);
- return -1;
- }
- if (cmp == 0)
- childpos = rightpos;
- }
- /* Move the smaller child up. */
- tmp = PyList_GET_ITEM(heap, childpos);
- Py_INCREF(tmp);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, tmp);
- pos = childpos;
- childpos = 2*pos + 1;
- }
-
- /* The leaf at pos is empty now. Put newitem there, and and bubble
- it up to its final resting place (by sifting its parents down). */
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return _siftdown(heap, startpos, pos);
+ Py_ssize_t startpos, endpos, childpos, rightpos;
+ int cmp;
+ PyObject *newitem, *tmp;
+
+ assert(PyList_Check(heap));
+ endpos = PyList_GET_SIZE(heap);
+ startpos = pos;
+ if (pos >= endpos) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return -1;
+ }
+ newitem = PyList_GET_ITEM(heap, pos);
+ Py_INCREF(newitem);
+
+ /* Bubble up the smaller child until hitting a leaf. */
+ childpos = 2*pos + 1; /* leftmost child position */
+ while (childpos < endpos) {
+ /* Set childpos to index of smaller child. */
+ rightpos = childpos + 1;
+ if (rightpos < endpos) {
+ cmp = cmp_lt(
+ PyList_GET_ITEM(heap, childpos),
+ PyList_GET_ITEM(heap, rightpos));
+ if (cmp == -1) {
+ Py_DECREF(newitem);
+ return -1;
+ }
+ if (cmp == 0)
+ childpos = rightpos;
+ }
+ /* Move the smaller child up. */
+ tmp = PyList_GET_ITEM(heap, childpos);
+ Py_INCREF(tmp);
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, tmp);
+ pos = childpos;
+ childpos = 2*pos + 1;
+ }
+
+ /* The leaf at pos is empty now. Put newitem there, and and bubble
+ it up to its final resting place (by sifting its parents down). */
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, newitem);
+ return _siftdown(heap, startpos, pos);
}
static PyObject *
heappush(PyObject *self, PyObject *args)
{
- PyObject *heap, *item;
+ PyObject *heap, *item;
- if (!PyArg_UnpackTuple(args, "heappush", 2, 2, &heap, &item))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "heappush", 2, 2, &heap, &item))
+ return NULL;
- if (!PyList_Check(heap)) {
- PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
- return NULL;
- }
+ if (!PyList_Check(heap)) {
+ PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
+ return NULL;
+ }
- if (PyList_Append(heap, item) == -1)
- return NULL;
+ if (PyList_Append(heap, item) == -1)
+ return NULL;
- if (_siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (_siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(heappush_doc,
@@ -128,35 +128,35 @@ PyDoc_STRVAR(heappush_doc,
static PyObject *
heappop(PyObject *self, PyObject *heap)
{
- PyObject *lastelt, *returnitem;
- Py_ssize_t n;
-
- if (!PyList_Check(heap)) {
- PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
- return NULL;
- }
-
- /* # raises appropriate IndexError if heap is empty */
- n = PyList_GET_SIZE(heap);
- if (n == 0) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return NULL;
- }
-
- lastelt = PyList_GET_ITEM(heap, n-1) ;
- Py_INCREF(lastelt);
- PyList_SetSlice(heap, n-1, n, NULL);
- n--;
-
- if (!n)
- return lastelt;
- returnitem = PyList_GET_ITEM(heap, 0);
- PyList_SET_ITEM(heap, 0, lastelt);
- if (_siftup((PyListObject *)heap, 0) == -1) {
- Py_DECREF(returnitem);
- return NULL;
- }
- return returnitem;
+ PyObject *lastelt, *returnitem;
+ Py_ssize_t n;
+
+ if (!PyList_Check(heap)) {
+ PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
+ return NULL;
+ }
+
+ /* # raises appropriate IndexError if heap is empty */
+ n = PyList_GET_SIZE(heap);
+ if (n == 0) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return NULL;
+ }
+
+ lastelt = PyList_GET_ITEM(heap, n-1) ;
+ Py_INCREF(lastelt);
+ PyList_SetSlice(heap, n-1, n, NULL);
+ n--;
+
+ if (!n)
+ return lastelt;
+ returnitem = PyList_GET_ITEM(heap, 0);
+ PyList_SET_ITEM(heap, 0, lastelt);
+ if (_siftup((PyListObject *)heap, 0) == -1) {
+ Py_DECREF(returnitem);
+ return NULL;
+ }
+ return returnitem;
}
PyDoc_STRVAR(heappop_doc,
@@ -165,29 +165,29 @@ PyDoc_STRVAR(heappop_doc,
static PyObject *
heapreplace(PyObject *self, PyObject *args)
{
- PyObject *heap, *item, *returnitem;
-
- if (!PyArg_UnpackTuple(args, "heapreplace", 2, 2, &heap, &item))
- return NULL;
-
- if (!PyList_Check(heap)) {
- PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
- return NULL;
- }
-
- if (PyList_GET_SIZE(heap) < 1) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return NULL;
- }
-
- returnitem = PyList_GET_ITEM(heap, 0);
- Py_INCREF(item);
- PyList_SET_ITEM(heap, 0, item);
- if (_siftup((PyListObject *)heap, 0) == -1) {
- Py_DECREF(returnitem);
- return NULL;
- }
- return returnitem;
+ PyObject *heap, *item, *returnitem;
+
+ if (!PyArg_UnpackTuple(args, "heapreplace", 2, 2, &heap, &item))
+ return NULL;
+
+ if (!PyList_Check(heap)) {
+ PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
+ return NULL;
+ }
+
+ if (PyList_GET_SIZE(heap) < 1) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return NULL;
+ }
+
+ returnitem = PyList_GET_ITEM(heap, 0);
+ Py_INCREF(item);
+ PyList_SET_ITEM(heap, 0, item);
+ if (_siftup((PyListObject *)heap, 0) == -1) {
+ Py_DECREF(returnitem);
+ return NULL;
+ }
+ return returnitem;
}
PyDoc_STRVAR(heapreplace_doc,
@@ -197,44 +197,44 @@ This is more efficient than heappop() followed by heappush(), and can be\n\
more appropriate when using a fixed-size heap. Note that the value\n\
returned may be larger than item! That constrains reasonable uses of\n\
this routine unless written as part of a conditional replacement:\n\n\
- if item > heap[0]:\n\
- item = heapreplace(heap, item)\n");
+ if item > heap[0]:\n\
+ item = heapreplace(heap, item)\n");
static PyObject *
heappushpop(PyObject *self, PyObject *args)
{
- PyObject *heap, *item, *returnitem;
- int cmp;
-
- if (!PyArg_UnpackTuple(args, "heappushpop", 2, 2, &heap, &item))
- return NULL;
-
- if (!PyList_Check(heap)) {
- PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
- return NULL;
- }
-
- if (PyList_GET_SIZE(heap) < 1) {
- Py_INCREF(item);
- return item;
- }
-
- cmp = cmp_lt(PyList_GET_ITEM(heap, 0), item);
- if (cmp == -1)
- return NULL;
- if (cmp == 0) {
- Py_INCREF(item);
- return item;
- }
-
- returnitem = PyList_GET_ITEM(heap, 0);
- Py_INCREF(item);
- PyList_SET_ITEM(heap, 0, item);
- if (_siftup((PyListObject *)heap, 0) == -1) {
- Py_DECREF(returnitem);
- return NULL;
- }
- return returnitem;
+ PyObject *heap, *item, *returnitem;
+ int cmp;
+
+ if (!PyArg_UnpackTuple(args, "heappushpop", 2, 2, &heap, &item))
+ return NULL;
+
+ if (!PyList_Check(heap)) {
+ PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
+ return NULL;
+ }
+
+ if (PyList_GET_SIZE(heap) < 1) {
+ Py_INCREF(item);
+ return item;
+ }
+
+ cmp = cmp_lt(PyList_GET_ITEM(heap, 0), item);
+ if (cmp == -1)
+ return NULL;
+ if (cmp == 0) {
+ Py_INCREF(item);
+ return item;
+ }
+
+ returnitem = PyList_GET_ITEM(heap, 0);
+ Py_INCREF(item);
+ PyList_SET_ITEM(heap, 0, item);
+ if (_siftup((PyListObject *)heap, 0) == -1) {
+ Py_DECREF(returnitem);
+ return NULL;
+ }
+ return returnitem;
}
PyDoc_STRVAR(heappushpop_doc,
@@ -245,26 +245,26 @@ heappush() followed by a separate call to heappop().");
static PyObject *
heapify(PyObject *self, PyObject *heap)
{
- Py_ssize_t i, n;
-
- if (!PyList_Check(heap)) {
- PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
- return NULL;
- }
-
- n = PyList_GET_SIZE(heap);
- /* Transform bottom-up. The largest index there's any point to
- looking at is the largest with a child index in-range, so must
- have 2*i + 1 < n, or i < (n-1)/2. If n is even = 2*j, this is
- (2*j-1)/2 = j-1/2 so j-1 is the largest, which is n//2 - 1. If
- n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
- and that's again n//2-1.
- */
- for (i=n/2-1 ; i>=0 ; i--)
- if(_siftup((PyListObject *)heap, i) == -1)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_ssize_t i, n;
+
+ if (!PyList_Check(heap)) {
+ PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
+ return NULL;
+ }
+
+ n = PyList_GET_SIZE(heap);
+ /* Transform bottom-up. The largest index there's any point to
+ looking at is the largest with a child index in-range, so must
+ have 2*i + 1 < n, or i < (n-1)/2. If n is even = 2*j, this is
+ (2*j-1)/2 = j-1/2 so j-1 is the largest, which is n//2 - 1. If
+ n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
+ and that's again n//2-1.
+ */
+ for (i=n/2-1 ; i>=0 ; i--)
+ if(_siftup((PyListObject *)heap, i) == -1)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(heapify_doc,
@@ -273,79 +273,79 @@ PyDoc_STRVAR(heapify_doc,
static PyObject *
nlargest(PyObject *self, PyObject *args)
{
- PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
- Py_ssize_t i, n;
- int cmp;
-
- if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable))
- return NULL;
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- heap = PyList_New(0);
- if (heap == NULL)
- goto fail;
-
- for (i=0 ; i<n ; i++ ){
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- if (PyList_Append(heap, elem) == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- Py_DECREF(elem);
- }
- if (PyList_GET_SIZE(heap) == 0)
- goto sortit;
-
- for (i=n/2-1 ; i>=0 ; i--)
- if(_siftup((PyListObject *)heap, i) == -1)
- goto fail;
-
- sol = PyList_GET_ITEM(heap, 0);
- while (1) {
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- cmp = cmp_lt(sol, elem);
- if (cmp == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- if (cmp == 0) {
- Py_DECREF(elem);
- continue;
- }
- oldelem = PyList_GET_ITEM(heap, 0);
- PyList_SET_ITEM(heap, 0, elem);
- Py_DECREF(oldelem);
- if (_siftup((PyListObject *)heap, 0) == -1)
- goto fail;
- sol = PyList_GET_ITEM(heap, 0);
- }
+ PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
+ Py_ssize_t i, n;
+ int cmp;
+
+ if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable))
+ return NULL;
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ heap = PyList_New(0);
+ if (heap == NULL)
+ goto fail;
+
+ for (i=0 ; i<n ; i++ ){
+ elem = PyIter_Next(it);
+ if (elem == NULL) {
+ if (PyErr_Occurred())
+ goto fail;
+ else
+ goto sortit;
+ }
+ if (PyList_Append(heap, elem) == -1) {
+ Py_DECREF(elem);
+ goto fail;
+ }
+ Py_DECREF(elem);
+ }
+ if (PyList_GET_SIZE(heap) == 0)
+ goto sortit;
+
+ for (i=n/2-1 ; i>=0 ; i--)
+ if(_siftup((PyListObject *)heap, i) == -1)
+ goto fail;
+
+ sol = PyList_GET_ITEM(heap, 0);
+ while (1) {
+ elem = PyIter_Next(it);
+ if (elem == NULL) {
+ if (PyErr_Occurred())
+ goto fail;
+ else
+ goto sortit;
+ }
+ cmp = cmp_lt(sol, elem);
+ if (cmp == -1) {
+ Py_DECREF(elem);
+ goto fail;
+ }
+ if (cmp == 0) {
+ Py_DECREF(elem);
+ continue;
+ }
+ oldelem = PyList_GET_ITEM(heap, 0);
+ PyList_SET_ITEM(heap, 0, elem);
+ Py_DECREF(oldelem);
+ if (_siftup((PyListObject *)heap, 0) == -1)
+ goto fail;
+ sol = PyList_GET_ITEM(heap, 0);
+ }
sortit:
- if (PyList_Sort(heap) == -1)
- goto fail;
- if (PyList_Reverse(heap) == -1)
- goto fail;
- Py_DECREF(it);
- return heap;
+ if (PyList_Sort(heap) == -1)
+ goto fail;
+ if (PyList_Reverse(heap) == -1)
+ goto fail;
+ Py_DECREF(it);
+ return heap;
fail:
- Py_DECREF(it);
- Py_XDECREF(heap);
- return NULL;
+ Py_DECREF(it);
+ Py_XDECREF(heap);
+ return NULL;
}
PyDoc_STRVAR(nlargest_doc,
@@ -356,166 +356,166 @@ Equivalent to: sorted(iterable, reverse=True)[:n]\n");
static int
_siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
- PyObject *newitem, *parent;
- int cmp;
- Py_ssize_t parentpos;
-
- assert(PyList_Check(heap));
- if (pos >= PyList_GET_SIZE(heap)) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return -1;
- }
-
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
- /* Follow the path to the root, moving parents down until finding
- a place newitem fits. */
- while (pos > startpos){
- parentpos = (pos - 1) >> 1;
- parent = PyList_GET_ITEM(heap, parentpos);
- cmp = cmp_lt(parent, newitem);
- if (cmp == -1) {
- Py_DECREF(newitem);
- return -1;
- }
- if (cmp == 0)
- break;
- Py_INCREF(parent);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, parent);
- pos = parentpos;
- }
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return 0;
+ PyObject *newitem, *parent;
+ int cmp;
+ Py_ssize_t parentpos;
+
+ assert(PyList_Check(heap));
+ if (pos >= PyList_GET_SIZE(heap)) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return -1;
+ }
+
+ newitem = PyList_GET_ITEM(heap, pos);
+ Py_INCREF(newitem);
+ /* Follow the path to the root, moving parents down until finding
+ a place newitem fits. */
+ while (pos > startpos){
+ parentpos = (pos - 1) >> 1;
+ parent = PyList_GET_ITEM(heap, parentpos);
+ cmp = cmp_lt(parent, newitem);
+ if (cmp == -1) {
+ Py_DECREF(newitem);
+ return -1;
+ }
+ if (cmp == 0)
+ break;
+ Py_INCREF(parent);
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, parent);
+ pos = parentpos;
+ }
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, newitem);
+ return 0;
}
static int
_siftupmax(PyListObject *heap, Py_ssize_t pos)
{
- Py_ssize_t startpos, endpos, childpos, rightpos;
- int cmp;
- PyObject *newitem, *tmp;
-
- assert(PyList_Check(heap));
- endpos = PyList_GET_SIZE(heap);
- startpos = pos;
- if (pos >= endpos) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return -1;
- }
- newitem = PyList_GET_ITEM(heap, pos);
- Py_INCREF(newitem);
-
- /* Bubble up the smaller child until hitting a leaf. */
- childpos = 2*pos + 1; /* leftmost child position */
- while (childpos < endpos) {
- /* Set childpos to index of smaller child. */
- rightpos = childpos + 1;
- if (rightpos < endpos) {
- cmp = cmp_lt(
- PyList_GET_ITEM(heap, rightpos),
- PyList_GET_ITEM(heap, childpos));
- if (cmp == -1) {
- Py_DECREF(newitem);
- return -1;
- }
- if (cmp == 0)
- childpos = rightpos;
- }
- /* Move the smaller child up. */
- tmp = PyList_GET_ITEM(heap, childpos);
- Py_INCREF(tmp);
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, tmp);
- pos = childpos;
- childpos = 2*pos + 1;
- }
-
- /* The leaf at pos is empty now. Put newitem there, and and bubble
- it up to its final resting place (by sifting its parents down). */
- Py_DECREF(PyList_GET_ITEM(heap, pos));
- PyList_SET_ITEM(heap, pos, newitem);
- return _siftdownmax(heap, startpos, pos);
+ Py_ssize_t startpos, endpos, childpos, rightpos;
+ int cmp;
+ PyObject *newitem, *tmp;
+
+ assert(PyList_Check(heap));
+ endpos = PyList_GET_SIZE(heap);
+ startpos = pos;
+ if (pos >= endpos) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return -1;
+ }
+ newitem = PyList_GET_ITEM(heap, pos);
+ Py_INCREF(newitem);
+
+ /* Bubble up the smaller child until hitting a leaf. */
+ childpos = 2*pos + 1; /* leftmost child position */
+ while (childpos < endpos) {
+ /* Set childpos to index of smaller child. */
+ rightpos = childpos + 1;
+ if (rightpos < endpos) {
+ cmp = cmp_lt(
+ PyList_GET_ITEM(heap, rightpos),
+ PyList_GET_ITEM(heap, childpos));
+ if (cmp == -1) {
+ Py_DECREF(newitem);
+ return -1;
+ }
+ if (cmp == 0)
+ childpos = rightpos;
+ }
+ /* Move the smaller child up. */
+ tmp = PyList_GET_ITEM(heap, childpos);
+ Py_INCREF(tmp);
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, tmp);
+ pos = childpos;
+ childpos = 2*pos + 1;
+ }
+
+ /* The leaf at pos is empty now. Put newitem there, and and bubble
+ it up to its final resting place (by sifting its parents down). */
+ Py_DECREF(PyList_GET_ITEM(heap, pos));
+ PyList_SET_ITEM(heap, pos, newitem);
+ return _siftdownmax(heap, startpos, pos);
}
static PyObject *
nsmallest(PyObject *self, PyObject *args)
{
- PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
- Py_ssize_t i, n;
- int cmp;
-
- if (!PyArg_ParseTuple(args, "nO:nsmallest", &n, &iterable))
- return NULL;
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- heap = PyList_New(0);
- if (heap == NULL)
- goto fail;
-
- for (i=0 ; i<n ; i++ ){
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- if (PyList_Append(heap, elem) == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- Py_DECREF(elem);
- }
- n = PyList_GET_SIZE(heap);
- if (n == 0)
- goto sortit;
-
- for (i=n/2-1 ; i>=0 ; i--)
- if(_siftupmax((PyListObject *)heap, i) == -1)
- goto fail;
-
- los = PyList_GET_ITEM(heap, 0);
- while (1) {
- elem = PyIter_Next(it);
- if (elem == NULL) {
- if (PyErr_Occurred())
- goto fail;
- else
- goto sortit;
- }
- cmp = cmp_lt(elem, los);
- if (cmp == -1) {
- Py_DECREF(elem);
- goto fail;
- }
- if (cmp == 0) {
- Py_DECREF(elem);
- continue;
- }
-
- oldelem = PyList_GET_ITEM(heap, 0);
- PyList_SET_ITEM(heap, 0, elem);
- Py_DECREF(oldelem);
- if (_siftupmax((PyListObject *)heap, 0) == -1)
- goto fail;
- los = PyList_GET_ITEM(heap, 0);
- }
+ PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
+ Py_ssize_t i, n;
+ int cmp;
+
+ if (!PyArg_ParseTuple(args, "nO:nsmallest", &n, &iterable))
+ return NULL;
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ heap = PyList_New(0);
+ if (heap == NULL)
+ goto fail;
+
+ for (i=0 ; i<n ; i++ ){
+ elem = PyIter_Next(it);
+ if (elem == NULL) {
+ if (PyErr_Occurred())
+ goto fail;
+ else
+ goto sortit;
+ }
+ if (PyList_Append(heap, elem) == -1) {
+ Py_DECREF(elem);
+ goto fail;
+ }
+ Py_DECREF(elem);
+ }
+ n = PyList_GET_SIZE(heap);
+ if (n == 0)
+ goto sortit;
+
+ for (i=n/2-1 ; i>=0 ; i--)
+ if(_siftupmax((PyListObject *)heap, i) == -1)
+ goto fail;
+
+ los = PyList_GET_ITEM(heap, 0);
+ while (1) {
+ elem = PyIter_Next(it);
+ if (elem == NULL) {
+ if (PyErr_Occurred())
+ goto fail;
+ else
+ goto sortit;
+ }
+ cmp = cmp_lt(elem, los);
+ if (cmp == -1) {
+ Py_DECREF(elem);
+ goto fail;
+ }
+ if (cmp == 0) {
+ Py_DECREF(elem);
+ continue;
+ }
+
+ oldelem = PyList_GET_ITEM(heap, 0);
+ PyList_SET_ITEM(heap, 0, elem);
+ Py_DECREF(oldelem);
+ if (_siftupmax((PyListObject *)heap, 0) == -1)
+ goto fail;
+ los = PyList_GET_ITEM(heap, 0);
+ }
sortit:
- if (PyList_Sort(heap) == -1)
- goto fail;
- Py_DECREF(it);
- return heap;
+ if (PyList_Sort(heap) == -1)
+ goto fail;
+ Py_DECREF(it);
+ return heap;
fail:
- Py_DECREF(it);
- Py_XDECREF(heap);
- return NULL;
+ Py_DECREF(it);
+ Py_XDECREF(heap);
+ return NULL;
}
PyDoc_STRVAR(nsmallest_doc,
@@ -524,21 +524,21 @@ PyDoc_STRVAR(nsmallest_doc,
Equivalent to: sorted(iterable)[:n]\n");
static PyMethodDef heapq_methods[] = {
- {"heappush", (PyCFunction)heappush,
- METH_VARARGS, heappush_doc},
- {"heappushpop", (PyCFunction)heappushpop,
- METH_VARARGS, heappushpop_doc},
- {"heappop", (PyCFunction)heappop,
- METH_O, heappop_doc},
- {"heapreplace", (PyCFunction)heapreplace,
- METH_VARARGS, heapreplace_doc},
- {"heapify", (PyCFunction)heapify,
- METH_O, heapify_doc},
- {"nlargest", (PyCFunction)nlargest,
- METH_VARARGS, nlargest_doc},
- {"nsmallest", (PyCFunction)nsmallest,
- METH_VARARGS, nsmallest_doc},
- {NULL, NULL} /* sentinel */
+ {"heappush", (PyCFunction)heappush,
+ METH_VARARGS, heappush_doc},
+ {"heappushpop", (PyCFunction)heappushpop,
+ METH_VARARGS, heappushpop_doc},
+ {"heappop", (PyCFunction)heappop,
+ METH_O, heappop_doc},
+ {"heapreplace", (PyCFunction)heapreplace,
+ METH_VARARGS, heapreplace_doc},
+ {"heapify", (PyCFunction)heapify,
+ METH_O, heapify_doc},
+ {"nlargest", (PyCFunction)nlargest,
+ METH_VARARGS, nlargest_doc},
+ {"nsmallest", (PyCFunction)nsmallest,
+ METH_VARARGS, nsmallest_doc},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc,
@@ -668,27 +668,27 @@ From all times, sorting has always been a Great Art! :-)\n");
static struct PyModuleDef _heapqmodule = {
- PyModuleDef_HEAD_INIT,
- "_heapq",
- module_doc,
- -1,
- heapq_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_heapq",
+ module_doc,
+ -1,
+ heapq_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__heapq(void)
{
- PyObject *m, *about;
-
- m = PyModule_Create(&_heapqmodule);
- if (m == NULL)
- return NULL;
- about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL);
- PyModule_AddObject(m, "__about__", about);
- return m;
+ PyObject *m, *about;
+
+ m = PyModule_Create(&_heapqmodule);
+ if (m == NULL)
+ return NULL;
+ about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL);
+ PyModule_AddObject(m, "__about__", about);
+ return m;
}
diff --git a/Modules/_json.c b/Modules/_json.c
index e555439..c965176 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -511,7 +511,7 @@ py_scanstring(PyObject* self UNUSED, PyObject *args)
rval = scanstring_unicode(pystr, end, strict, &next_end);
}
else {
- PyErr_Format(PyExc_TypeError,
+ PyErr_Format(PyExc_TypeError,
"first argument must be a string or bytes, not %.80s",
Py_TYPE(pystr)->tp_name);
return NULL;
@@ -1394,7 +1394,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
if (PyObject_IsTrue(s->sort_keys)) {
if (code == NULL) {
- code = Py_CompileString("sorted(d.items(), key=lambda kv: kv[0])",
+ code = Py_CompileString("sorted(d.items(), key=lambda kv: kv[0])",
"_json.c", Py_eval_input);
if (code == NULL)
goto bail;
@@ -1409,14 +1409,14 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
}
items = PyEval_EvalCode((PyCodeObject *)code, PyEval_GetGlobals(), mapping);
Py_DECREF(mapping);
- } else {
+ } else {
items = PyMapping_Items(dct);
- }
- if (items == NULL)
+ }
+ if (items == NULL)
goto bail;
it = PyObject_GetIter(items);
- Py_DECREF(items);
- if (it == NULL)
+ Py_DECREF(items);
+ if (it == NULL)
goto bail;
skipkeys = PyObject_IsTrue(s->skipkeys);
idx = 0;
@@ -1437,8 +1437,8 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
goto bail;
}
else if (key == Py_True || key == Py_False || key == Py_None) {
- /* This must come before the PyLong_Check because
- True and False are also 1 and 0.*/
+ /* This must come before the PyLong_Check because
+ True and False are also 1 and 0.*/
kstr = _encoded_const(key);
if (kstr == NULL)
goto bail;
@@ -1704,15 +1704,15 @@ PyDoc_STRVAR(module_doc,
"json speedups\n");
static struct PyModuleDef jsonmodule = {
- PyModuleDef_HEAD_INIT,
- "_json",
- module_doc,
- -1,
- speedups_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_json",
+ module_doc,
+ -1,
+ speedups_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyObject*
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index aabc3ac..84e393e 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -77,7 +77,7 @@ str2uni(const char* s)
if (dest != smallbuf)
PyMem_Free(dest);
return res2;
-}
+}
/* support functions for formatting floating point numbers */
@@ -243,7 +243,7 @@ PyLocale_strcoll(PyObject* self, PyObject* args)
PyObject *os1, *os2, *result = NULL;
wchar_t *ws1 = NULL, *ws2 = NULL;
Py_ssize_t len1, len2;
-
+
if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
return NULL;
/* Convert the unicode strings to wchar[]. */
@@ -373,9 +373,9 @@ PyLocale_getdefaultlocale(PyObject* self)
#ifdef HAVE_LANGINFO_H
#define LANGINFO(X) {#X, X}
static struct langinfo_constant{
- char* name;
- int value;
-} langinfo_constants[] =
+ char* name;
+ int value;
+} langinfo_constants[] =
{
/* These constants should exist on any langinfo implementation */
LANGINFO(DAY_1),
@@ -514,10 +514,10 @@ PyDoc_STRVAR(gettext__doc__,
static PyObject*
PyIntl_gettext(PyObject* self, PyObject *args)
{
- char *in;
- if (!PyArg_ParseTuple(args, "s", &in))
- return 0;
- return str2uni(gettext(in));
+ char *in;
+ if (!PyArg_ParseTuple(args, "s", &in))
+ return 0;
+ return str2uni(gettext(in));
}
PyDoc_STRVAR(dgettext__doc__,
@@ -527,10 +527,10 @@ PyDoc_STRVAR(dgettext__doc__,
static PyObject*
PyIntl_dgettext(PyObject* self, PyObject *args)
{
- char *domain, *in;
- if (!PyArg_ParseTuple(args, "zs", &domain, &in))
- return 0;
- return str2uni(dgettext(domain, in));
+ char *domain, *in;
+ if (!PyArg_ParseTuple(args, "zs", &domain, &in))
+ return 0;
+ return str2uni(dgettext(domain, in));
}
PyDoc_STRVAR(dcgettext__doc__,
@@ -540,11 +540,11 @@ PyDoc_STRVAR(dcgettext__doc__,
static PyObject*
PyIntl_dcgettext(PyObject *self, PyObject *args)
{
- char *domain, *msgid;
- int category;
- if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
- return 0;
- return str2uni(dcgettext(domain,msgid,category));
+ char *domain, *msgid;
+ int category;
+ if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
+ return 0;
+ return str2uni(dcgettext(domain,msgid,category));
}
PyDoc_STRVAR(textdomain__doc__,
@@ -554,15 +554,15 @@ PyDoc_STRVAR(textdomain__doc__,
static PyObject*
PyIntl_textdomain(PyObject* self, PyObject* args)
{
- char *domain;
- if (!PyArg_ParseTuple(args, "z", &domain))
- return 0;
- domain = textdomain(domain);
- if (!domain) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- return str2uni(domain);
+ char *domain;
+ if (!PyArg_ParseTuple(args, "z", &domain))
+ return 0;
+ domain = textdomain(domain);
+ if (!domain) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ return str2uni(domain);
}
PyDoc_STRVAR(bindtextdomain__doc__,
@@ -572,19 +572,19 @@ PyDoc_STRVAR(bindtextdomain__doc__,
static PyObject*
PyIntl_bindtextdomain(PyObject* self,PyObject*args)
{
- char *domain, *dirname;
- if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
- return 0;
- if (!strlen(domain)) {
- PyErr_SetString(Error, "domain must be a non-empty string");
- return 0;
- }
- dirname = bindtextdomain(domain, dirname);
- if (!dirname) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- return str2uni(dirname);
+ char *domain, *dirname;
+ if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))
+ return 0;
+ if (!strlen(domain)) {
+ PyErr_SetString(Error, "domain must be a non-empty string");
+ return 0;
+ }
+ dirname = bindtextdomain(domain, dirname);
+ if (!dirname) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ return str2uni(dirname);
}
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
@@ -595,32 +595,32 @@ PyDoc_STRVAR(bind_textdomain_codeset__doc__,
static PyObject*
PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
{
- char *domain,*codeset;
- if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
- return NULL;
- codeset = bind_textdomain_codeset(domain, codeset);
- if (codeset)
- return str2uni(codeset);
- Py_RETURN_NONE;
+ char *domain,*codeset;
+ if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))
+ return NULL;
+ codeset = bind_textdomain_codeset(domain, codeset);
+ if (codeset)
+ return str2uni(codeset);
+ Py_RETURN_NONE;
}
#endif
#endif
static struct PyMethodDef PyLocale_Methods[] = {
- {"setlocale", (PyCFunction) PyLocale_setlocale,
+ {"setlocale", (PyCFunction) PyLocale_setlocale,
METH_VARARGS, setlocale__doc__},
- {"localeconv", (PyCFunction) PyLocale_localeconv,
+ {"localeconv", (PyCFunction) PyLocale_localeconv,
METH_NOARGS, localeconv__doc__},
#ifdef HAVE_WCSCOLL
- {"strcoll", (PyCFunction) PyLocale_strcoll,
+ {"strcoll", (PyCFunction) PyLocale_strcoll,
METH_VARARGS, strcoll__doc__},
#endif
#ifdef HAVE_WCSXFRM
- {"strxfrm", (PyCFunction) PyLocale_strxfrm,
+ {"strxfrm", (PyCFunction) PyLocale_strxfrm,
METH_VARARGS, strxfrm__doc__},
#endif
-#if defined(MS_WINDOWS)
+#if defined(MS_WINDOWS)
{"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},
#endif
#ifdef HAVE_LANGINFO_H
@@ -642,21 +642,21 @@ static struct PyMethodDef PyLocale_Methods[] = {
{"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,
METH_VARARGS, bind_textdomain_codeset__doc__},
#endif
-#endif
+#endif
{NULL, NULL}
};
static struct PyModuleDef _localemodule = {
- PyModuleDef_HEAD_INIT,
- "_locale",
- locale__doc__,
- -1,
- PyLocale_Methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_locale",
+ locale__doc__,
+ -1,
+ PyLocale_Methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -669,7 +669,7 @@ PyInit__locale(void)
m = PyModule_Create(&_localemodule);
if (m == NULL)
- return NULL;
+ return NULL;
d = PyModule_GetDict(m);
@@ -712,14 +712,14 @@ PyInit__locale(void)
#ifdef HAVE_LANGINFO_H
for (i = 0; langinfo_constants[i].name; i++) {
- PyModule_AddIntConstant(m, langinfo_constants[i].name,
- langinfo_constants[i].value);
+ PyModule_AddIntConstant(m, langinfo_constants[i].name,
+ langinfo_constants[i].value);
}
#endif
return m;
}
-/*
+/*
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index a4951c0..1fd660b 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -17,19 +17,19 @@
static PY_LONG_LONG
hpTimer(void)
{
- LARGE_INTEGER li;
- QueryPerformanceCounter(&li);
- return li.QuadPart;
+ LARGE_INTEGER li;
+ QueryPerformanceCounter(&li);
+ return li.QuadPart;
}
static double
hpTimerUnit(void)
{
- LARGE_INTEGER li;
- if (QueryPerformanceFrequency(&li))
- return 1.0 / li.QuadPart;
- else
- return 0.000001; /* unlikely */
+ LARGE_INTEGER li;
+ if (QueryPerformanceFrequency(&li))
+ return 1.0 / li.QuadPart;
+ else
+ return 0.000001; /* unlikely */
}
#else /* !MS_WINDOWS */
@@ -48,22 +48,22 @@ hpTimerUnit(void)
static PY_LONG_LONG
hpTimer(void)
{
- struct timeval tv;
- PY_LONG_LONG ret;
+ struct timeval tv;
+ PY_LONG_LONG ret;
#ifdef GETTIMEOFDAY_NO_TZ
- gettimeofday(&tv);
+ gettimeofday(&tv);
#else
- gettimeofday(&tv, (struct timezone *)NULL);
+ gettimeofday(&tv, (struct timezone *)NULL);
#endif
- ret = tv.tv_sec;
- ret = ret * 1000000 + tv.tv_usec;
- return ret;
+ ret = tv.tv_sec;
+ ret = ret * 1000000 + tv.tv_usec;
+ return ret;
}
static double
hpTimerUnit(void)
{
- return 0.000001;
+ return 0.000001;
}
#endif /* MS_WINDOWS */
@@ -75,41 +75,41 @@ struct _ProfilerEntry;
/* represents a function called from another function */
typedef struct _ProfilerSubEntry {
- rotating_node_t header;
- PY_LONG_LONG tt;
- PY_LONG_LONG it;
- long callcount;
- long recursivecallcount;
- long recursionLevel;
+ rotating_node_t header;
+ PY_LONG_LONG tt;
+ PY_LONG_LONG it;
+ long callcount;
+ long recursivecallcount;
+ long recursionLevel;
} ProfilerSubEntry;
/* represents a function or user defined block */
typedef struct _ProfilerEntry {
- rotating_node_t header;
- PyObject *userObj; /* PyCodeObject, or a descriptive str for builtins */
- PY_LONG_LONG tt; /* total time in this entry */
- PY_LONG_LONG it; /* inline time in this entry (not in subcalls) */
- long callcount; /* how many times this was called */
- long recursivecallcount; /* how many times called recursively */
- long recursionLevel;
- rotating_node_t *calls;
+ rotating_node_t header;
+ PyObject *userObj; /* PyCodeObject, or a descriptive str for builtins */
+ PY_LONG_LONG tt; /* total time in this entry */
+ PY_LONG_LONG it; /* inline time in this entry (not in subcalls) */
+ long callcount; /* how many times this was called */
+ long recursivecallcount; /* how many times called recursively */
+ long recursionLevel;
+ rotating_node_t *calls;
} ProfilerEntry;
typedef struct _ProfilerContext {
- PY_LONG_LONG t0;
- PY_LONG_LONG subt;
- struct _ProfilerContext *previous;
- ProfilerEntry *ctxEntry;
+ PY_LONG_LONG t0;
+ PY_LONG_LONG subt;
+ struct _ProfilerContext *previous;
+ ProfilerEntry *ctxEntry;
} ProfilerContext;
typedef struct {
- PyObject_HEAD
- rotating_node_t *profilerEntries;
- ProfilerContext *currentProfilerContext;
- ProfilerContext *freelistProfilerContext;
- int flags;
- PyObject *externalTimer;
- double externalTimerUnit;
+ PyObject_HEAD
+ rotating_node_t *profilerEntries;
+ ProfilerContext *currentProfilerContext;
+ ProfilerContext *freelistProfilerContext;
+ int flags;
+ PyObject *externalTimer;
+ double externalTimerUnit;
} ProfilerObject;
#define POF_ENABLED 0x001
@@ -129,407 +129,407 @@ static PyObject *empty_tuple;
static PY_LONG_LONG CallExternalTimer(ProfilerObject *pObj)
{
- PY_LONG_LONG result;
- PyObject *o = PyObject_Call(pObj->externalTimer, empty_tuple, NULL);
- if (o == NULL) {
- PyErr_WriteUnraisable(pObj->externalTimer);
- return 0;
- }
- if (pObj->externalTimerUnit > 0.0) {
- /* interpret the result as an integer that will be scaled
- in profiler_getstats() */
- result = PyLong_AsLongLong(o);
- }
- else {
- /* interpret the result as a double measured in seconds.
- As the profiler works with PY_LONG_LONG internally
- we convert it to a large integer */
- double val = PyFloat_AsDouble(o);
- /* error handling delayed to the code below */
- result = (PY_LONG_LONG) (val * DOUBLE_TIMER_PRECISION);
- }
- Py_DECREF(o);
- if (PyErr_Occurred()) {
- PyErr_WriteUnraisable(pObj->externalTimer);
- return 0;
- }
- return result;
+ PY_LONG_LONG result;
+ PyObject *o = PyObject_Call(pObj->externalTimer, empty_tuple, NULL);
+ if (o == NULL) {
+ PyErr_WriteUnraisable(pObj->externalTimer);
+ return 0;
+ }
+ if (pObj->externalTimerUnit > 0.0) {
+ /* interpret the result as an integer that will be scaled
+ in profiler_getstats() */
+ result = PyLong_AsLongLong(o);
+ }
+ else {
+ /* interpret the result as a double measured in seconds.
+ As the profiler works with PY_LONG_LONG internally
+ we convert it to a large integer */
+ double val = PyFloat_AsDouble(o);
+ /* error handling delayed to the code below */
+ result = (PY_LONG_LONG) (val * DOUBLE_TIMER_PRECISION);
+ }
+ Py_DECREF(o);
+ if (PyErr_Occurred()) {
+ PyErr_WriteUnraisable(pObj->externalTimer);
+ return 0;
+ }
+ return result;
}
-#define CALL_TIMER(pObj) ((pObj)->externalTimer ? \
- CallExternalTimer(pObj) : \
- hpTimer())
+#define CALL_TIMER(pObj) ((pObj)->externalTimer ? \
+ CallExternalTimer(pObj) : \
+ hpTimer())
/*** ProfilerObject ***/
static PyObject *
normalizeUserObj(PyObject *obj)
{
- PyCFunctionObject *fn;
- if (!PyCFunction_Check(obj)) {
- Py_INCREF(obj);
- return obj;
- }
- /* Replace built-in function objects with a descriptive string
- because of built-in methods -- keeping a reference to
- __self__ is probably not a good idea. */
- fn = (PyCFunctionObject *)obj;
-
- if (fn->m_self == NULL) {
- /* built-in function: look up the module name */
- PyObject *mod = fn->m_module;
- const char *modname;
- if (mod && PyUnicode_Check(mod)) {
- modname = _PyUnicode_AsString(mod);
- }
- else if (mod && PyModule_Check(mod)) {
- modname = PyModule_GetName(mod);
- if (modname == NULL) {
- PyErr_Clear();
- modname = "builtins";
- }
- }
- else {
- modname = "builtins";
- }
- if (strcmp(modname, "builtins") != 0)
- return PyUnicode_FromFormat("<%s.%s>",
- modname,
- fn->m_ml->ml_name);
- else
- return PyUnicode_FromFormat("<%s>",
- fn->m_ml->ml_name);
- }
- else {
- /* built-in method: try to return
- repr(getattr(type(__self__), __name__))
- */
- PyObject *self = fn->m_self;
- PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name);
- if (name != NULL) {
- PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
- Py_XINCREF(mo);
- Py_DECREF(name);
- if (mo != NULL) {
- PyObject *res = PyObject_Repr(mo);
- Py_DECREF(mo);
- if (res != NULL)
- return res;
- }
- }
- PyErr_Clear();
- return PyUnicode_FromFormat("<built-in method %s>",
- fn->m_ml->ml_name);
- }
+ PyCFunctionObject *fn;
+ if (!PyCFunction_Check(obj)) {
+ Py_INCREF(obj);
+ return obj;
+ }
+ /* Replace built-in function objects with a descriptive string
+ because of built-in methods -- keeping a reference to
+ __self__ is probably not a good idea. */
+ fn = (PyCFunctionObject *)obj;
+
+ if (fn->m_self == NULL) {
+ /* built-in function: look up the module name */
+ PyObject *mod = fn->m_module;
+ const char *modname;
+ if (mod && PyUnicode_Check(mod)) {
+ modname = _PyUnicode_AsString(mod);
+ }
+ else if (mod && PyModule_Check(mod)) {
+ modname = PyModule_GetName(mod);
+ if (modname == NULL) {
+ PyErr_Clear();
+ modname = "builtins";
+ }
+ }
+ else {
+ modname = "builtins";
+ }
+ if (strcmp(modname, "builtins") != 0)
+ return PyUnicode_FromFormat("<%s.%s>",
+ modname,
+ fn->m_ml->ml_name);
+ else
+ return PyUnicode_FromFormat("<%s>",
+ fn->m_ml->ml_name);
+ }
+ else {
+ /* built-in method: try to return
+ repr(getattr(type(__self__), __name__))
+ */
+ PyObject *self = fn->m_self;
+ PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name);
+ if (name != NULL) {
+ PyObject *mo = _PyType_Lookup(Py_TYPE(self), name);
+ Py_XINCREF(mo);
+ Py_DECREF(name);
+ if (mo != NULL) {
+ PyObject *res = PyObject_Repr(mo);
+ Py_DECREF(mo);
+ if (res != NULL)
+ return res;
+ }
+ }
+ PyErr_Clear();
+ return PyUnicode_FromFormat("<built-in method %s>",
+ fn->m_ml->ml_name);
+ }
}
static ProfilerEntry*
newProfilerEntry(ProfilerObject *pObj, void *key, PyObject *userObj)
{
- ProfilerEntry *self;
- self = (ProfilerEntry*) malloc(sizeof(ProfilerEntry));
- if (self == NULL) {
- pObj->flags |= POF_NOMEMORY;
- return NULL;
- }
- userObj = normalizeUserObj(userObj);
- if (userObj == NULL) {
- PyErr_Clear();
- free(self);
- pObj->flags |= POF_NOMEMORY;
- return NULL;
- }
- self->header.key = key;
- self->userObj = userObj;
- self->tt = 0;
- self->it = 0;
- self->callcount = 0;
- self->recursivecallcount = 0;
- self->recursionLevel = 0;
- self->calls = EMPTY_ROTATING_TREE;
- RotatingTree_Add(&pObj->profilerEntries, &self->header);
- return self;
+ ProfilerEntry *self;
+ self = (ProfilerEntry*) malloc(sizeof(ProfilerEntry));
+ if (self == NULL) {
+ pObj->flags |= POF_NOMEMORY;
+ return NULL;
+ }
+ userObj = normalizeUserObj(userObj);
+ if (userObj == NULL) {
+ PyErr_Clear();
+ free(self);
+ pObj->flags |= POF_NOMEMORY;
+ return NULL;
+ }
+ self->header.key = key;
+ self->userObj = userObj;
+ self->tt = 0;
+ self->it = 0;
+ self->callcount = 0;
+ self->recursivecallcount = 0;
+ self->recursionLevel = 0;
+ self->calls = EMPTY_ROTATING_TREE;
+ RotatingTree_Add(&pObj->profilerEntries, &self->header);
+ return self;
}
static ProfilerEntry*
getEntry(ProfilerObject *pObj, void *key)
{
- return (ProfilerEntry*) RotatingTree_Get(&pObj->profilerEntries, key);
+ return (ProfilerEntry*) RotatingTree_Get(&pObj->profilerEntries, key);
}
-static ProfilerSubEntry *
+static ProfilerSubEntry *
getSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
{
- return (ProfilerSubEntry*) RotatingTree_Get(&caller->calls,
- (void *)entry);
+ return (ProfilerSubEntry*) RotatingTree_Get(&caller->calls,
+ (void *)entry);
}
static ProfilerSubEntry *
newSubEntry(ProfilerObject *pObj, ProfilerEntry *caller, ProfilerEntry* entry)
{
- ProfilerSubEntry *self;
- self = (ProfilerSubEntry*) malloc(sizeof(ProfilerSubEntry));
- if (self == NULL) {
- pObj->flags |= POF_NOMEMORY;
- return NULL;
- }
- self->header.key = (void *)entry;
- self->tt = 0;
- self->it = 0;
- self->callcount = 0;
- self->recursivecallcount = 0;
- self->recursionLevel = 0;
- RotatingTree_Add(&caller->calls, &self->header);
- return self;
+ ProfilerSubEntry *self;
+ self = (ProfilerSubEntry*) malloc(sizeof(ProfilerSubEntry));
+ if (self == NULL) {
+ pObj->flags |= POF_NOMEMORY;
+ return NULL;
+ }
+ self->header.key = (void *)entry;
+ self->tt = 0;
+ self->it = 0;
+ self->callcount = 0;
+ self->recursivecallcount = 0;
+ self->recursionLevel = 0;
+ RotatingTree_Add(&caller->calls, &self->header);
+ return self;
}
static int freeSubEntry(rotating_node_t *header, void *arg)
{
- ProfilerSubEntry *subentry = (ProfilerSubEntry*) header;
- free(subentry);
- return 0;
+ ProfilerSubEntry *subentry = (ProfilerSubEntry*) header;
+ free(subentry);
+ return 0;
}
static int freeEntry(rotating_node_t *header, void *arg)
{
- ProfilerEntry *entry = (ProfilerEntry*) header;
- RotatingTree_Enum(entry->calls, freeSubEntry, NULL);
- Py_DECREF(entry->userObj);
- free(entry);
- return 0;
+ ProfilerEntry *entry = (ProfilerEntry*) header;
+ RotatingTree_Enum(entry->calls, freeSubEntry, NULL);
+ Py_DECREF(entry->userObj);
+ free(entry);
+ return 0;
}
static void clearEntries(ProfilerObject *pObj)
{
- RotatingTree_Enum(pObj->profilerEntries, freeEntry, NULL);
- pObj->profilerEntries = EMPTY_ROTATING_TREE;
- /* release the memory hold by the ProfilerContexts */
- if (pObj->currentProfilerContext) {
- free(pObj->currentProfilerContext);
- pObj->currentProfilerContext = NULL;
- }
- while (pObj->freelistProfilerContext) {
- ProfilerContext *c = pObj->freelistProfilerContext;
- pObj->freelistProfilerContext = c->previous;
- free(c);
- }
- pObj->freelistProfilerContext = NULL;
+ RotatingTree_Enum(pObj->profilerEntries, freeEntry, NULL);
+ pObj->profilerEntries = EMPTY_ROTATING_TREE;
+ /* release the memory hold by the ProfilerContexts */
+ if (pObj->currentProfilerContext) {
+ free(pObj->currentProfilerContext);
+ pObj->currentProfilerContext = NULL;
+ }
+ while (pObj->freelistProfilerContext) {
+ ProfilerContext *c = pObj->freelistProfilerContext;
+ pObj->freelistProfilerContext = c->previous;
+ free(c);
+ }
+ pObj->freelistProfilerContext = NULL;
}
static void
initContext(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
{
- self->ctxEntry = entry;
- self->subt = 0;
- self->previous = pObj->currentProfilerContext;
- pObj->currentProfilerContext = self;
- ++entry->recursionLevel;
- if ((pObj->flags & POF_SUBCALLS) && self->previous) {
- /* find or create an entry for me in my caller's entry */
- ProfilerEntry *caller = self->previous->ctxEntry;
- ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
- if (subentry == NULL)
- subentry = newSubEntry(pObj, caller, entry);
- if (subentry)
- ++subentry->recursionLevel;
- }
- self->t0 = CALL_TIMER(pObj);
+ self->ctxEntry = entry;
+ self->subt = 0;
+ self->previous = pObj->currentProfilerContext;
+ pObj->currentProfilerContext = self;
+ ++entry->recursionLevel;
+ if ((pObj->flags & POF_SUBCALLS) && self->previous) {
+ /* find or create an entry for me in my caller's entry */
+ ProfilerEntry *caller = self->previous->ctxEntry;
+ ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
+ if (subentry == NULL)
+ subentry = newSubEntry(pObj, caller, entry);
+ if (subentry)
+ ++subentry->recursionLevel;
+ }
+ self->t0 = CALL_TIMER(pObj);
}
static void
Stop(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
{
- PY_LONG_LONG tt = CALL_TIMER(pObj) - self->t0;
- PY_LONG_LONG it = tt - self->subt;
- if (self->previous)
- self->previous->subt += tt;
- pObj->currentProfilerContext = self->previous;
- if (--entry->recursionLevel == 0)
- entry->tt += tt;
- else
- ++entry->recursivecallcount;
- entry->it += it;
- entry->callcount++;
- if ((pObj->flags & POF_SUBCALLS) && self->previous) {
- /* find or create an entry for me in my caller's entry */
- ProfilerEntry *caller = self->previous->ctxEntry;
- ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
- if (subentry) {
- if (--subentry->recursionLevel == 0)
- subentry->tt += tt;
- else
- ++subentry->recursivecallcount;
- subentry->it += it;
- ++subentry->callcount;
- }
- }
+ PY_LONG_LONG tt = CALL_TIMER(pObj) - self->t0;
+ PY_LONG_LONG it = tt - self->subt;
+ if (self->previous)
+ self->previous->subt += tt;
+ pObj->currentProfilerContext = self->previous;
+ if (--entry->recursionLevel == 0)
+ entry->tt += tt;
+ else
+ ++entry->recursivecallcount;
+ entry->it += it;
+ entry->callcount++;
+ if ((pObj->flags & POF_SUBCALLS) && self->previous) {
+ /* find or create an entry for me in my caller's entry */
+ ProfilerEntry *caller = self->previous->ctxEntry;
+ ProfilerSubEntry *subentry = getSubEntry(pObj, caller, entry);
+ if (subentry) {
+ if (--subentry->recursionLevel == 0)
+ subentry->tt += tt;
+ else
+ ++subentry->recursivecallcount;
+ subentry->it += it;
+ ++subentry->callcount;
+ }
+ }
}
static void
ptrace_enter_call(PyObject *self, void *key, PyObject *userObj)
{
- /* entering a call to the function identified by 'key'
- (which can be a PyCodeObject or a PyMethodDef pointer) */
- ProfilerObject *pObj = (ProfilerObject*)self;
- ProfilerEntry *profEntry;
- ProfilerContext *pContext;
-
- /* In the case of entering a generator expression frame via a
- * throw (gen_send_ex(.., 1)), we may already have an
- * Exception set here. We must not mess around with this
- * exception, and some of the code under here assumes that
- * PyErr_* is its own to mess around with, so we have to
- * save and restore any current exception. */
- PyObject *last_type, *last_value, *last_tb;
- PyErr_Fetch(&last_type, &last_value, &last_tb);
-
- profEntry = getEntry(pObj, key);
- if (profEntry == NULL) {
- profEntry = newProfilerEntry(pObj, key, userObj);
- if (profEntry == NULL)
- goto restorePyerr;
- }
- /* grab a ProfilerContext out of the free list */
- pContext = pObj->freelistProfilerContext;
- if (pContext) {
- pObj->freelistProfilerContext = pContext->previous;
- }
- else {
- /* free list exhausted, allocate a new one */
- pContext = (ProfilerContext*)
- malloc(sizeof(ProfilerContext));
- if (pContext == NULL) {
- pObj->flags |= POF_NOMEMORY;
- goto restorePyerr;
- }
- }
- initContext(pObj, pContext, profEntry);
+ /* entering a call to the function identified by 'key'
+ (which can be a PyCodeObject or a PyMethodDef pointer) */
+ ProfilerObject *pObj = (ProfilerObject*)self;
+ ProfilerEntry *profEntry;
+ ProfilerContext *pContext;
+
+ /* In the case of entering a generator expression frame via a
+ * throw (gen_send_ex(.., 1)), we may already have an
+ * Exception set here. We must not mess around with this
+ * exception, and some of the code under here assumes that
+ * PyErr_* is its own to mess around with, so we have to
+ * save and restore any current exception. */
+ PyObject *last_type, *last_value, *last_tb;
+ PyErr_Fetch(&last_type, &last_value, &last_tb);
+
+ profEntry = getEntry(pObj, key);
+ if (profEntry == NULL) {
+ profEntry = newProfilerEntry(pObj, key, userObj);
+ if (profEntry == NULL)
+ goto restorePyerr;
+ }
+ /* grab a ProfilerContext out of the free list */
+ pContext = pObj->freelistProfilerContext;
+ if (pContext) {
+ pObj->freelistProfilerContext = pContext->previous;
+ }
+ else {
+ /* free list exhausted, allocate a new one */
+ pContext = (ProfilerContext*)
+ malloc(sizeof(ProfilerContext));
+ if (pContext == NULL) {
+ pObj->flags |= POF_NOMEMORY;
+ goto restorePyerr;
+ }
+ }
+ initContext(pObj, pContext, profEntry);
restorePyerr:
- PyErr_Restore(last_type, last_value, last_tb);
+ PyErr_Restore(last_type, last_value, last_tb);
}
static void
ptrace_leave_call(PyObject *self, void *key)
{
- /* leaving a call to the function identified by 'key' */
- ProfilerObject *pObj = (ProfilerObject*)self;
- ProfilerEntry *profEntry;
- ProfilerContext *pContext;
-
- pContext = pObj->currentProfilerContext;
- if (pContext == NULL)
- return;
- profEntry = getEntry(pObj, key);
- if (profEntry) {
- Stop(pObj, pContext, profEntry);
- }
- else {
- pObj->currentProfilerContext = pContext->previous;
- }
- /* put pContext into the free list */
- pContext->previous = pObj->freelistProfilerContext;
- pObj->freelistProfilerContext = pContext;
+ /* leaving a call to the function identified by 'key' */
+ ProfilerObject *pObj = (ProfilerObject*)self;
+ ProfilerEntry *profEntry;
+ ProfilerContext *pContext;
+
+ pContext = pObj->currentProfilerContext;
+ if (pContext == NULL)
+ return;
+ profEntry = getEntry(pObj, key);
+ if (profEntry) {
+ Stop(pObj, pContext, profEntry);
+ }
+ else {
+ pObj->currentProfilerContext = pContext->previous;
+ }
+ /* put pContext into the free list */
+ pContext->previous = pObj->freelistProfilerContext;
+ pObj->freelistProfilerContext = pContext;
}
static int
profiler_callback(PyObject *self, PyFrameObject *frame, int what,
- PyObject *arg)
+ PyObject *arg)
{
- switch (what) {
-
- /* the 'frame' of a called function is about to start its execution */
- case PyTrace_CALL:
- ptrace_enter_call(self, (void *)frame->f_code,
- (PyObject *)frame->f_code);
- break;
-
- /* the 'frame' of a called function is about to finish
- (either normally or with an exception) */
- case PyTrace_RETURN:
- ptrace_leave_call(self, (void *)frame->f_code);
- break;
-
- /* case PyTrace_EXCEPTION:
- If the exception results in the function exiting, a
- PyTrace_RETURN event will be generated, so we don't need to
- handle it. */
-
-#ifdef PyTrace_C_CALL /* not defined in Python <= 2.3 */
- /* the Python function 'frame' is issuing a call to the built-in
- function 'arg' */
- case PyTrace_C_CALL:
- if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
- && PyCFunction_Check(arg)) {
- ptrace_enter_call(self,
- ((PyCFunctionObject *)arg)->m_ml,
- arg);
- }
- break;
-
- /* the call to the built-in function 'arg' is returning into its
- caller 'frame' */
- case PyTrace_C_RETURN: /* ...normally */
- case PyTrace_C_EXCEPTION: /* ...with an exception set */
- if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
- && PyCFunction_Check(arg)) {
- ptrace_leave_call(self,
- ((PyCFunctionObject *)arg)->m_ml);
- }
- break;
+ switch (what) {
+
+ /* the 'frame' of a called function is about to start its execution */
+ case PyTrace_CALL:
+ ptrace_enter_call(self, (void *)frame->f_code,
+ (PyObject *)frame->f_code);
+ break;
+
+ /* the 'frame' of a called function is about to finish
+ (either normally or with an exception) */
+ case PyTrace_RETURN:
+ ptrace_leave_call(self, (void *)frame->f_code);
+ break;
+
+ /* case PyTrace_EXCEPTION:
+ If the exception results in the function exiting, a
+ PyTrace_RETURN event will be generated, so we don't need to
+ handle it. */
+
+#ifdef PyTrace_C_CALL /* not defined in Python <= 2.3 */
+ /* the Python function 'frame' is issuing a call to the built-in
+ function 'arg' */
+ case PyTrace_C_CALL:
+ if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
+ && PyCFunction_Check(arg)) {
+ ptrace_enter_call(self,
+ ((PyCFunctionObject *)arg)->m_ml,
+ arg);
+ }
+ break;
+
+ /* the call to the built-in function 'arg' is returning into its
+ caller 'frame' */
+ case PyTrace_C_RETURN: /* ...normally */
+ case PyTrace_C_EXCEPTION: /* ...with an exception set */
+ if ((((ProfilerObject *)self)->flags & POF_BUILTINS)
+ && PyCFunction_Check(arg)) {
+ ptrace_leave_call(self,
+ ((PyCFunctionObject *)arg)->m_ml);
+ }
+ break;
#endif
- default:
- break;
- }
- return 0;
+ default:
+ break;
+ }
+ return 0;
}
static int
pending_exception(ProfilerObject *pObj)
{
- if (pObj->flags & POF_NOMEMORY) {
- pObj->flags -= POF_NOMEMORY;
- PyErr_SetString(PyExc_MemoryError,
- "memory was exhausted while profiling");
- return -1;
- }
- return 0;
+ if (pObj->flags & POF_NOMEMORY) {
+ pObj->flags -= POF_NOMEMORY;
+ PyErr_SetString(PyExc_MemoryError,
+ "memory was exhausted while profiling");
+ return -1;
+ }
+ return 0;
}
/************************************************************/
static PyStructSequence_Field profiler_entry_fields[] = {
- {"code", "code object or built-in function name"},
- {"callcount", "how many times this was called"},
- {"reccallcount", "how many times called recursively"},
- {"totaltime", "total time in this entry"},
- {"inlinetime", "inline time in this entry (not in subcalls)"},
- {"calls", "details of the calls"},
- {0}
+ {"code", "code object or built-in function name"},
+ {"callcount", "how many times this was called"},
+ {"reccallcount", "how many times called recursively"},
+ {"totaltime", "total time in this entry"},
+ {"inlinetime", "inline time in this entry (not in subcalls)"},
+ {"calls", "details of the calls"},
+ {0}
};
static PyStructSequence_Field profiler_subentry_fields[] = {
- {"code", "called code object or built-in function name"},
- {"callcount", "how many times this is called"},
- {"reccallcount", "how many times this is called recursively"},
- {"totaltime", "total time spent in this call"},
- {"inlinetime", "inline time (not in further subcalls)"},
- {0}
+ {"code", "called code object or built-in function name"},
+ {"callcount", "how many times this is called"},
+ {"reccallcount", "how many times this is called recursively"},
+ {"totaltime", "total time spent in this call"},
+ {"inlinetime", "inline time (not in further subcalls)"},
+ {0}
};
static PyStructSequence_Desc profiler_entry_desc = {
- "_lsprof.profiler_entry", /* name */
- NULL, /* doc */
- profiler_entry_fields,
- 6
+ "_lsprof.profiler_entry", /* name */
+ NULL, /* doc */
+ profiler_entry_fields,
+ 6
};
static PyStructSequence_Desc profiler_subentry_desc = {
- "_lsprof.profiler_subentry", /* name */
- NULL, /* doc */
- profiler_subentry_fields,
- 5
+ "_lsprof.profiler_subentry", /* name */
+ NULL, /* doc */
+ profiler_subentry_fields,
+ 5
};
static int initialized;
@@ -538,70 +538,70 @@ static PyTypeObject StatsSubEntryType;
typedef struct {
- PyObject *list;
- PyObject *sublist;
- double factor;
+ PyObject *list;
+ PyObject *sublist;
+ double factor;
} statscollector_t;
static int statsForSubEntry(rotating_node_t *node, void *arg)
{
- ProfilerSubEntry *sentry = (ProfilerSubEntry*) node;
- statscollector_t *collect = (statscollector_t*) arg;
- ProfilerEntry *entry = (ProfilerEntry*) sentry->header.key;
- int err;
- PyObject *sinfo;
- sinfo = PyObject_CallFunction((PyObject*) &StatsSubEntryType,
- "((Olldd))",
- entry->userObj,
- sentry->callcount,
- sentry->recursivecallcount,
- collect->factor * sentry->tt,
- collect->factor * sentry->it);
- if (sinfo == NULL)
- return -1;
- err = PyList_Append(collect->sublist, sinfo);
- Py_DECREF(sinfo);
- return err;
+ ProfilerSubEntry *sentry = (ProfilerSubEntry*) node;
+ statscollector_t *collect = (statscollector_t*) arg;
+ ProfilerEntry *entry = (ProfilerEntry*) sentry->header.key;
+ int err;
+ PyObject *sinfo;
+ sinfo = PyObject_CallFunction((PyObject*) &StatsSubEntryType,
+ "((Olldd))",
+ entry->userObj,
+ sentry->callcount,
+ sentry->recursivecallcount,
+ collect->factor * sentry->tt,
+ collect->factor * sentry->it);
+ if (sinfo == NULL)
+ return -1;
+ err = PyList_Append(collect->sublist, sinfo);
+ Py_DECREF(sinfo);
+ return err;
}
static int statsForEntry(rotating_node_t *node, void *arg)
{
- ProfilerEntry *entry = (ProfilerEntry*) node;
- statscollector_t *collect = (statscollector_t*) arg;
- PyObject *info;
- int err;
- if (entry->callcount == 0)
- return 0; /* skip */
-
- if (entry->calls != EMPTY_ROTATING_TREE) {
- collect->sublist = PyList_New(0);
- if (collect->sublist == NULL)
- return -1;
- if (RotatingTree_Enum(entry->calls,
- statsForSubEntry, collect) != 0) {
- Py_DECREF(collect->sublist);
- return -1;
- }
- }
- else {
- Py_INCREF(Py_None);
- collect->sublist = Py_None;
- }
-
- info = PyObject_CallFunction((PyObject*) &StatsEntryType,
- "((OllddO))",
- entry->userObj,
- entry->callcount,
- entry->recursivecallcount,
- collect->factor * entry->tt,
- collect->factor * entry->it,
- collect->sublist);
- Py_DECREF(collect->sublist);
- if (info == NULL)
- return -1;
- err = PyList_Append(collect->list, info);
- Py_DECREF(info);
- return err;
+ ProfilerEntry *entry = (ProfilerEntry*) node;
+ statscollector_t *collect = (statscollector_t*) arg;
+ PyObject *info;
+ int err;
+ if (entry->callcount == 0)
+ return 0; /* skip */
+
+ if (entry->calls != EMPTY_ROTATING_TREE) {
+ collect->sublist = PyList_New(0);
+ if (collect->sublist == NULL)
+ return -1;
+ if (RotatingTree_Enum(entry->calls,
+ statsForSubEntry, collect) != 0) {
+ Py_DECREF(collect->sublist);
+ return -1;
+ }
+ }
+ else {
+ Py_INCREF(Py_None);
+ collect->sublist = Py_None;
+ }
+
+ info = PyObject_CallFunction((PyObject*) &StatsEntryType,
+ "((OllddO))",
+ entry->userObj,
+ entry->callcount,
+ entry->recursivecallcount,
+ collect->factor * entry->tt,
+ collect->factor * entry->it,
+ collect->sublist);
+ Py_DECREF(collect->sublist);
+ if (info == NULL)
+ return -1;
+ err = PyList_Append(collect->list, info);
+ Py_DECREF(info);
+ return err;
}
PyDoc_STRVAR(getstats_doc, "\
@@ -631,51 +631,51 @@ profiler_subentry objects:\n\
static PyObject*
profiler_getstats(ProfilerObject *pObj, PyObject* noarg)
{
- statscollector_t collect;
- if (pending_exception(pObj))
- return NULL;
- if (!pObj->externalTimer)
- collect.factor = hpTimerUnit();
- else if (pObj->externalTimerUnit > 0.0)
- collect.factor = pObj->externalTimerUnit;
- else
- collect.factor = 1.0 / DOUBLE_TIMER_PRECISION;
- collect.list = PyList_New(0);
- if (collect.list == NULL)
- return NULL;
- if (RotatingTree_Enum(pObj->profilerEntries, statsForEntry, &collect)
- != 0) {
- Py_DECREF(collect.list);
- return NULL;
- }
- return collect.list;
+ statscollector_t collect;
+ if (pending_exception(pObj))
+ return NULL;
+ if (!pObj->externalTimer)
+ collect.factor = hpTimerUnit();
+ else if (pObj->externalTimerUnit > 0.0)
+ collect.factor = pObj->externalTimerUnit;
+ else
+ collect.factor = 1.0 / DOUBLE_TIMER_PRECISION;
+ collect.list = PyList_New(0);
+ if (collect.list == NULL)
+ return NULL;
+ if (RotatingTree_Enum(pObj->profilerEntries, statsForEntry, &collect)
+ != 0) {
+ Py_DECREF(collect.list);
+ return NULL;
+ }
+ return collect.list;
}
static int
setSubcalls(ProfilerObject *pObj, int nvalue)
{
- if (nvalue == 0)
- pObj->flags &= ~POF_SUBCALLS;
- else if (nvalue > 0)
- pObj->flags |= POF_SUBCALLS;
- return 0;
+ if (nvalue == 0)
+ pObj->flags &= ~POF_SUBCALLS;
+ else if (nvalue > 0)
+ pObj->flags |= POF_SUBCALLS;
+ return 0;
}
static int
setBuiltins(ProfilerObject *pObj, int nvalue)
{
- if (nvalue == 0)
- pObj->flags &= ~POF_BUILTINS;
- else if (nvalue > 0) {
+ if (nvalue == 0)
+ pObj->flags &= ~POF_BUILTINS;
+ else if (nvalue > 0) {
#ifndef PyTrace_C_CALL
- PyErr_SetString(PyExc_ValueError,
- "builtins=True requires Python >= 2.4");
- return -1;
+ PyErr_SetString(PyExc_ValueError,
+ "builtins=True requires Python >= 2.4");
+ return -1;
#else
- pObj->flags |= POF_BUILTINS;
+ pObj->flags |= POF_BUILTINS;
#endif
- }
- return 0;
+ }
+ return 0;
}
PyDoc_STRVAR(enable_doc, "\
@@ -691,33 +691,33 @@ built-in functions separately from their caller.\n\
static PyObject*
profiler_enable(ProfilerObject *self, PyObject *args, PyObject *kwds)
{
- int subcalls = -1;
- int builtins = -1;
- static char *kwlist[] = {"subcalls", "builtins", 0};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:enable",
- kwlist, &subcalls, &builtins))
- return NULL;
- if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0)
- return NULL;
- PyEval_SetProfile(profiler_callback, (PyObject*)self);
- self->flags |= POF_ENABLED;
- Py_INCREF(Py_None);
- return Py_None;
+ int subcalls = -1;
+ int builtins = -1;
+ static char *kwlist[] = {"subcalls", "builtins", 0};
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:enable",
+ kwlist, &subcalls, &builtins))
+ return NULL;
+ if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0)
+ return NULL;
+ PyEval_SetProfile(profiler_callback, (PyObject*)self);
+ self->flags |= POF_ENABLED;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static void
flush_unmatched(ProfilerObject *pObj)
{
- while (pObj->currentProfilerContext) {
- ProfilerContext *pContext = pObj->currentProfilerContext;
- ProfilerEntry *profEntry= pContext->ctxEntry;
- if (profEntry)
- Stop(pObj, pContext, profEntry);
- else
- pObj->currentProfilerContext = pContext->previous;
- if (pContext)
- free(pContext);
- }
+ while (pObj->currentProfilerContext) {
+ ProfilerContext *pContext = pObj->currentProfilerContext;
+ ProfilerEntry *profEntry= pContext->ctxEntry;
+ if (profEntry)
+ Stop(pObj, pContext, profEntry);
+ else
+ pObj->currentProfilerContext = pContext->previous;
+ if (pContext)
+ free(pContext);
+ }
}
@@ -730,13 +730,13 @@ Stop collecting profiling information.\n\
static PyObject*
profiler_disable(ProfilerObject *self, PyObject* noarg)
{
- self->flags &= ~POF_ENABLED;
- PyEval_SetProfile(NULL, NULL);
- flush_unmatched(self);
- if (pending_exception(self))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ self->flags &= ~POF_ENABLED;
+ PyEval_SetProfile(NULL, NULL);
+ flush_unmatched(self);
+ if (pending_exception(self))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(clear_doc, "\
@@ -748,62 +748,62 @@ Clear all profiling information collected so far.\n\
static PyObject*
profiler_clear(ProfilerObject *pObj, PyObject* noarg)
{
- clearEntries(pObj);
- Py_INCREF(Py_None);
- return Py_None;
+ clearEntries(pObj);
+ Py_INCREF(Py_None);
+ return Py_None;
}
static void
profiler_dealloc(ProfilerObject *op)
{
- if (op->flags & POF_ENABLED)
- PyEval_SetProfile(NULL, NULL);
- flush_unmatched(op);
- clearEntries(op);
- Py_XDECREF(op->externalTimer);
- Py_TYPE(op)->tp_free(op);
+ if (op->flags & POF_ENABLED)
+ PyEval_SetProfile(NULL, NULL);
+ flush_unmatched(op);
+ clearEntries(op);
+ Py_XDECREF(op->externalTimer);
+ Py_TYPE(op)->tp_free(op);
}
static int
profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
{
- PyObject *o;
- PyObject *timer = NULL;
- double timeunit = 0.0;
- int subcalls = 1;
+ PyObject *o;
+ PyObject *timer = NULL;
+ double timeunit = 0.0;
+ int subcalls = 1;
#ifdef PyTrace_C_CALL
- int builtins = 1;
+ int builtins = 1;
#else
- int builtins = 0;
+ int builtins = 0;
#endif
- static char *kwlist[] = {"timer", "timeunit",
- "subcalls", "builtins", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "|Odii:Profiler", kwlist,
- &timer, &timeunit,
- &subcalls, &builtins))
- return -1;
-
- if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
- return -1;
- o = pObj->externalTimer;
- pObj->externalTimer = timer;
- Py_XINCREF(timer);
- Py_XDECREF(o);
- pObj->externalTimerUnit = timeunit;
- return 0;
+ static char *kwlist[] = {"timer", "timeunit",
+ "subcalls", "builtins", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Odii:Profiler", kwlist,
+ &timer, &timeunit,
+ &subcalls, &builtins))
+ return -1;
+
+ if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
+ return -1;
+ o = pObj->externalTimer;
+ pObj->externalTimer = timer;
+ Py_XINCREF(timer);
+ Py_XDECREF(o);
+ pObj->externalTimerUnit = timeunit;
+ return 0;
}
static PyMethodDef profiler_methods[] = {
- {"getstats", (PyCFunction)profiler_getstats,
- METH_NOARGS, getstats_doc},
- {"enable", (PyCFunction)profiler_enable,
- METH_VARARGS | METH_KEYWORDS, enable_doc},
- {"disable", (PyCFunction)profiler_disable,
- METH_NOARGS, disable_doc},
- {"clear", (PyCFunction)profiler_clear,
- METH_NOARGS, clear_doc},
- {NULL, NULL}
+ {"getstats", (PyCFunction)profiler_getstats,
+ METH_NOARGS, getstats_doc},
+ {"enable", (PyCFunction)profiler_enable,
+ METH_VARARGS | METH_KEYWORDS, enable_doc},
+ {"disable", (PyCFunction)profiler_disable,
+ METH_NOARGS, disable_doc},
+ {"clear", (PyCFunction)profiler_clear,
+ METH_NOARGS, clear_doc},
+ {NULL, NULL}
};
PyDoc_STRVAR(profiler_doc, "\
@@ -817,89 +817,89 @@ Profiler(custom_timer=None, time_unit=None, subcalls=True, builtins=True)\n\
");
static PyTypeObject PyProfiler_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_lsprof.Profiler", /* tp_name */
- sizeof(ProfilerObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)profiler_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- profiler_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- profiler_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)profiler_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_lsprof.Profiler", /* tp_name */
+ sizeof(ProfilerObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)profiler_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ profiler_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ profiler_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)profiler_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_Del, /* tp_free */
};
static PyMethodDef moduleMethods[] = {
- {NULL, NULL}
+ {NULL, NULL}
};
static struct PyModuleDef _lsprofmodule = {
- PyModuleDef_HEAD_INIT,
- "_lsprof",
- "Fast profiler",
- -1,
- moduleMethods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_lsprof",
+ "Fast profiler",
+ -1,
+ moduleMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__lsprof(void)
{
- PyObject *module, *d;
- module = PyModule_Create(&_lsprofmodule);
- if (module == NULL)
- return NULL;
- d = PyModule_GetDict(module);
- if (PyType_Ready(&PyProfiler_Type) < 0)
- return NULL;
- PyDict_SetItemString(d, "Profiler", (PyObject *)&PyProfiler_Type);
-
- if (!initialized) {
- PyStructSequence_InitType(&StatsEntryType,
- &profiler_entry_desc);
- PyStructSequence_InitType(&StatsSubEntryType,
- &profiler_subentry_desc);
- }
- Py_INCREF((PyObject*) &StatsEntryType);
- Py_INCREF((PyObject*) &StatsSubEntryType);
- PyModule_AddObject(module, "profiler_entry",
- (PyObject*) &StatsEntryType);
- PyModule_AddObject(module, "profiler_subentry",
- (PyObject*) &StatsSubEntryType);
- empty_tuple = PyTuple_New(0);
- initialized = 1;
- return module;
+ PyObject *module, *d;
+ module = PyModule_Create(&_lsprofmodule);
+ if (module == NULL)
+ return NULL;
+ d = PyModule_GetDict(module);
+ if (PyType_Ready(&PyProfiler_Type) < 0)
+ return NULL;
+ PyDict_SetItemString(d, "Profiler", (PyObject *)&PyProfiler_Type);
+
+ if (!initialized) {
+ PyStructSequence_InitType(&StatsEntryType,
+ &profiler_entry_desc);
+ PyStructSequence_InitType(&StatsSubEntryType,
+ &profiler_subentry_desc);
+ }
+ Py_INCREF((PyObject*) &StatsEntryType);
+ Py_INCREF((PyObject*) &StatsSubEntryType);
+ PyModule_AddObject(module, "profiler_entry",
+ (PyObject*) &StatsEntryType);
+ PyModule_AddObject(module, "profiler_subentry",
+ (PyObject*) &StatsSubEntryType);
+ empty_tuple = PyTuple_New(0);
+ initialized = 1;
+ return module;
}
diff --git a/Modules/_multiprocessing/connection.h b/Modules/_multiprocessing/connection.h
index 2ee5368..002d5aa 100644
--- a/Modules/_multiprocessing/connection.h
+++ b/Modules/_multiprocessing/connection.h
@@ -1,5 +1,5 @@
/*
- * Definition of a `Connection` type.
+ * Definition of a `Connection` type.
* Used by `socket_connection.c` and `pipe_connection.c`.
*
* connection.h
@@ -19,14 +19,14 @@
#define CHECK_READABLE(self) \
if (!(self->flags & READABLE)) { \
- PyErr_SetString(PyExc_IOError, "connection is write-only"); \
- return NULL; \
+ PyErr_SetString(PyExc_IOError, "connection is write-only"); \
+ return NULL; \
}
#define CHECK_WRITABLE(self) \
if (!(self->flags & WRITABLE)) { \
- PyErr_SetString(PyExc_IOError, "connection is read-only"); \
- return NULL; \
+ PyErr_SetString(PyExc_IOError, "connection is read-only"); \
+ return NULL; \
}
/*
@@ -36,57 +36,57 @@
static PyObject *
connection_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- ConnectionObject *self;
- HANDLE handle;
- BOOL readable = TRUE, writable = TRUE;
-
- static char *kwlist[] = {"handle", "readable", "writable", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, F_HANDLE "|ii", kwlist,
- &handle, &readable, &writable))
- return NULL;
-
- if (handle == INVALID_HANDLE_VALUE || (Py_ssize_t)handle < 0) {
- PyErr_Format(PyExc_IOError, "invalid handle %zd",
- (Py_ssize_t)handle);
- return NULL;
- }
-
- if (!readable && !writable) {
- PyErr_SetString(PyExc_ValueError,
- "either readable or writable must be true");
- return NULL;
- }
-
- self = PyObject_New(ConnectionObject, type);
- if (self == NULL)
- return NULL;
-
- self->weakreflist = NULL;
- self->handle = handle;
- self->flags = 0;
-
- if (readable)
- self->flags |= READABLE;
- if (writable)
- self->flags |= WRITABLE;
- assert(self->flags >= 1 && self->flags <= 3);
-
- return (PyObject*)self;
+ ConnectionObject *self;
+ HANDLE handle;
+ BOOL readable = TRUE, writable = TRUE;
+
+ static char *kwlist[] = {"handle", "readable", "writable", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, F_HANDLE "|ii", kwlist,
+ &handle, &readable, &writable))
+ return NULL;
+
+ if (handle == INVALID_HANDLE_VALUE || (Py_ssize_t)handle < 0) {
+ PyErr_Format(PyExc_IOError, "invalid handle %zd",
+ (Py_ssize_t)handle);
+ return NULL;
+ }
+
+ if (!readable && !writable) {
+ PyErr_SetString(PyExc_ValueError,
+ "either readable or writable must be true");
+ return NULL;
+ }
+
+ self = PyObject_New(ConnectionObject, type);
+ if (self == NULL)
+ return NULL;
+
+ self->weakreflist = NULL;
+ self->handle = handle;
+ self->flags = 0;
+
+ if (readable)
+ self->flags |= READABLE;
+ if (writable)
+ self->flags |= WRITABLE;
+ assert(self->flags >= 1 && self->flags <= 3);
+
+ return (PyObject*)self;
}
static void
connection_dealloc(ConnectionObject* self)
{
- if (self->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject*)self);
-
- if (self->handle != INVALID_HANDLE_VALUE) {
- Py_BEGIN_ALLOW_THREADS
- CLOSE(self->handle);
- Py_END_ALLOW_THREADS
- }
- PyObject_Del(self);
+ if (self->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject*)self);
+
+ if (self->handle != INVALID_HANDLE_VALUE) {
+ Py_BEGIN_ALLOW_THREADS
+ CLOSE(self->handle);
+ Py_END_ALLOW_THREADS
+ }
+ PyObject_Del(self);
}
/*
@@ -96,168 +96,168 @@ connection_dealloc(ConnectionObject* self)
static PyObject *
connection_sendbytes(ConnectionObject *self, PyObject *args)
{
- Py_buffer pbuffer;
- char *buffer;
- Py_ssize_t length, offset=0, size=PY_SSIZE_T_MIN;
- int res;
-
- if (!PyArg_ParseTuple(args, F_RBUFFER "*|" F_PY_SSIZE_T F_PY_SSIZE_T,
- &pbuffer, &offset, &size))
- return NULL;
- buffer = pbuffer.buf;
- length = pbuffer.len;
-
- CHECK_WRITABLE(self); /* XXX release buffer in case of failure */
-
- if (offset < 0) {
- PyBuffer_Release(&pbuffer);
- PyErr_SetString(PyExc_ValueError, "offset is negative");
- return NULL;
- }
- if (length < offset) {
- PyBuffer_Release(&pbuffer);
- PyErr_SetString(PyExc_ValueError, "buffer length < offset");
- return NULL;
- }
-
- if (size == PY_SSIZE_T_MIN) {
- size = length - offset;
- } else {
- if (size < 0) {
- PyBuffer_Release(&pbuffer);
- PyErr_SetString(PyExc_ValueError, "size is negative");
- return NULL;
- }
- if (offset + size > length) {
- PyBuffer_Release(&pbuffer);
- PyErr_SetString(PyExc_ValueError,
- "buffer length < offset + size");
- return NULL;
- }
- }
-
- res = conn_send_string(self, buffer + offset, size);
-
- PyBuffer_Release(&pbuffer);
- if (res < 0) {
- if (PyErr_Occurred())
- return NULL;
- else
- return mp_SetError(PyExc_IOError, res);
- }
-
- Py_RETURN_NONE;
+ Py_buffer pbuffer;
+ char *buffer;
+ Py_ssize_t length, offset=0, size=PY_SSIZE_T_MIN;
+ int res;
+
+ if (!PyArg_ParseTuple(args, F_RBUFFER "*|" F_PY_SSIZE_T F_PY_SSIZE_T,
+ &pbuffer, &offset, &size))
+ return NULL;
+ buffer = pbuffer.buf;
+ length = pbuffer.len;
+
+ CHECK_WRITABLE(self); /* XXX release buffer in case of failure */
+
+ if (offset < 0) {
+ PyBuffer_Release(&pbuffer);
+ PyErr_SetString(PyExc_ValueError, "offset is negative");
+ return NULL;
+ }
+ if (length < offset) {
+ PyBuffer_Release(&pbuffer);
+ PyErr_SetString(PyExc_ValueError, "buffer length < offset");
+ return NULL;
+ }
+
+ if (size == PY_SSIZE_T_MIN) {
+ size = length - offset;
+ } else {
+ if (size < 0) {
+ PyBuffer_Release(&pbuffer);
+ PyErr_SetString(PyExc_ValueError, "size is negative");
+ return NULL;
+ }
+ if (offset + size > length) {
+ PyBuffer_Release(&pbuffer);
+ PyErr_SetString(PyExc_ValueError,
+ "buffer length < offset + size");
+ return NULL;
+ }
+ }
+
+ res = conn_send_string(self, buffer + offset, size);
+
+ PyBuffer_Release(&pbuffer);
+ if (res < 0) {
+ if (PyErr_Occurred())
+ return NULL;
+ else
+ return mp_SetError(PyExc_IOError, res);
+ }
+
+ Py_RETURN_NONE;
}
static PyObject *
-connection_recvbytes(ConnectionObject *self, PyObject *args)
+connection_recvbytes(ConnectionObject *self, PyObject *args)
{
- char *freeme = NULL;
- Py_ssize_t res, maxlength = PY_SSIZE_T_MAX;
- PyObject *result = NULL;
-
- if (!PyArg_ParseTuple(args, "|" F_PY_SSIZE_T, &maxlength))
- return NULL;
-
- CHECK_READABLE(self);
-
- if (maxlength < 0) {
- PyErr_SetString(PyExc_ValueError, "maxlength < 0");
- return NULL;
- }
-
- res = conn_recv_string(self, self->buffer, CONNECTION_BUFFER_SIZE,
- &freeme, maxlength);
-
- if (res < 0) {
- if (res == MP_BAD_MESSAGE_LENGTH) {
- if ((self->flags & WRITABLE) == 0) {
- Py_BEGIN_ALLOW_THREADS
- CLOSE(self->handle);
- Py_END_ALLOW_THREADS
- self->handle = INVALID_HANDLE_VALUE;
- } else {
- self->flags = WRITABLE;
- }
- }
- mp_SetError(PyExc_IOError, res);
- } else {
- if (freeme == NULL) {
- result = PyBytes_FromStringAndSize(self->buffer, res);
- } else {
- result = PyBytes_FromStringAndSize(freeme, res);
- PyMem_Free(freeme);
- }
- }
-
- return result;
+ char *freeme = NULL;
+ Py_ssize_t res, maxlength = PY_SSIZE_T_MAX;
+ PyObject *result = NULL;
+
+ if (!PyArg_ParseTuple(args, "|" F_PY_SSIZE_T, &maxlength))
+ return NULL;
+
+ CHECK_READABLE(self);
+
+ if (maxlength < 0) {
+ PyErr_SetString(PyExc_ValueError, "maxlength < 0");
+ return NULL;
+ }
+
+ res = conn_recv_string(self, self->buffer, CONNECTION_BUFFER_SIZE,
+ &freeme, maxlength);
+
+ if (res < 0) {
+ if (res == MP_BAD_MESSAGE_LENGTH) {
+ if ((self->flags & WRITABLE) == 0) {
+ Py_BEGIN_ALLOW_THREADS
+ CLOSE(self->handle);
+ Py_END_ALLOW_THREADS
+ self->handle = INVALID_HANDLE_VALUE;
+ } else {
+ self->flags = WRITABLE;
+ }
+ }
+ mp_SetError(PyExc_IOError, res);
+ } else {
+ if (freeme == NULL) {
+ result = PyBytes_FromStringAndSize(self->buffer, res);
+ } else {
+ result = PyBytes_FromStringAndSize(freeme, res);
+ PyMem_Free(freeme);
+ }
+ }
+
+ return result;
}
static PyObject *
-connection_recvbytes_into(ConnectionObject *self, PyObject *args)
+connection_recvbytes_into(ConnectionObject *self, PyObject *args)
{
- char *freeme = NULL, *buffer = NULL;
- Py_ssize_t res, length, offset = 0;
- PyObject *result = NULL;
- Py_buffer pbuf;
-
- CHECK_READABLE(self);
-
- if (!PyArg_ParseTuple(args, "w*|" F_PY_SSIZE_T,
- &pbuf, &offset))
- return NULL;
-
- buffer = pbuf.buf;
- length = pbuf.len;
-
- if (offset < 0) {
- PyErr_SetString(PyExc_ValueError, "negative offset");
- goto _error;
- }
-
- if (offset > length) {
- PyErr_SetString(PyExc_ValueError, "offset too large");
- goto _error;
- }
-
- res = conn_recv_string(self, buffer+offset, length-offset,
- &freeme, PY_SSIZE_T_MAX);
-
- if (res < 0) {
- if (res == MP_BAD_MESSAGE_LENGTH) {
- if ((self->flags & WRITABLE) == 0) {
- Py_BEGIN_ALLOW_THREADS
- CLOSE(self->handle);
- Py_END_ALLOW_THREADS
- self->handle = INVALID_HANDLE_VALUE;
- } else {
- self->flags = WRITABLE;
- }
- }
- mp_SetError(PyExc_IOError, res);
- } else {
- if (freeme == NULL) {
- result = PyInt_FromSsize_t(res);
- } else {
- result = PyObject_CallFunction(BufferTooShort,
- F_RBUFFER "#",
- freeme, res);
- PyMem_Free(freeme);
- if (result) {
- PyErr_SetObject(BufferTooShort, result);
- Py_DECREF(result);
- }
- goto _error;
- }
- }
+ char *freeme = NULL, *buffer = NULL;
+ Py_ssize_t res, length, offset = 0;
+ PyObject *result = NULL;
+ Py_buffer pbuf;
+
+ CHECK_READABLE(self);
+
+ if (!PyArg_ParseTuple(args, "w*|" F_PY_SSIZE_T,
+ &pbuf, &offset))
+ return NULL;
+
+ buffer = pbuf.buf;
+ length = pbuf.len;
+
+ if (offset < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative offset");
+ goto _error;
+ }
+
+ if (offset > length) {
+ PyErr_SetString(PyExc_ValueError, "offset too large");
+ goto _error;
+ }
+
+ res = conn_recv_string(self, buffer+offset, length-offset,
+ &freeme, PY_SSIZE_T_MAX);
+
+ if (res < 0) {
+ if (res == MP_BAD_MESSAGE_LENGTH) {
+ if ((self->flags & WRITABLE) == 0) {
+ Py_BEGIN_ALLOW_THREADS
+ CLOSE(self->handle);
+ Py_END_ALLOW_THREADS
+ self->handle = INVALID_HANDLE_VALUE;
+ } else {
+ self->flags = WRITABLE;
+ }
+ }
+ mp_SetError(PyExc_IOError, res);
+ } else {
+ if (freeme == NULL) {
+ result = PyInt_FromSsize_t(res);
+ } else {
+ result = PyObject_CallFunction(BufferTooShort,
+ F_RBUFFER "#",
+ freeme, res);
+ PyMem_Free(freeme);
+ if (result) {
+ PyErr_SetObject(BufferTooShort, result);
+ Py_DECREF(result);
+ }
+ goto _error;
+ }
+ }
_cleanup:
- PyBuffer_Release(&pbuf);
- return result;
+ PyBuffer_Release(&pbuf);
+ return result;
_error:
- result = NULL;
- goto _cleanup;
+ result = NULL;
+ goto _cleanup;
}
/*
@@ -267,74 +267,74 @@ _error:
static PyObject *
connection_send_obj(ConnectionObject *self, PyObject *obj)
{
- char *buffer;
- int res;
- Py_ssize_t length;
- PyObject *pickled_string = NULL;
+ char *buffer;
+ int res;
+ Py_ssize_t length;
+ PyObject *pickled_string = NULL;
- CHECK_WRITABLE(self);
+ CHECK_WRITABLE(self);
- pickled_string = PyObject_CallFunctionObjArgs(pickle_dumps, obj,
- pickle_protocol, NULL);
- if (!pickled_string)
- goto failure;
+ pickled_string = PyObject_CallFunctionObjArgs(pickle_dumps, obj,
+ pickle_protocol, NULL);
+ if (!pickled_string)
+ goto failure;
- if (PyBytes_AsStringAndSize(pickled_string, &buffer, &length) < 0)
- goto failure;
+ if (PyBytes_AsStringAndSize(pickled_string, &buffer, &length) < 0)
+ goto failure;
- res = conn_send_string(self, buffer, (int)length);
+ res = conn_send_string(self, buffer, (int)length);
- if (res < 0) {
- mp_SetError(PyExc_IOError, res);
- goto failure;
- }
+ if (res < 0) {
+ mp_SetError(PyExc_IOError, res);
+ goto failure;
+ }
- Py_XDECREF(pickled_string);
- Py_RETURN_NONE;
+ Py_XDECREF(pickled_string);
+ Py_RETURN_NONE;
failure:
- Py_XDECREF(pickled_string);
- return NULL;
+ Py_XDECREF(pickled_string);
+ return NULL;
}
static PyObject *
connection_recv_obj(ConnectionObject *self)
{
- char *freeme = NULL;
- Py_ssize_t res;
- PyObject *temp = NULL, *result = NULL;
-
- CHECK_READABLE(self);
-
- res = conn_recv_string(self, self->buffer, CONNECTION_BUFFER_SIZE,
- &freeme, PY_SSIZE_T_MAX);
-
- if (res < 0) {
- if (res == MP_BAD_MESSAGE_LENGTH) {
- if ((self->flags & WRITABLE) == 0) {
- Py_BEGIN_ALLOW_THREADS
- CLOSE(self->handle);
- Py_END_ALLOW_THREADS
- self->handle = INVALID_HANDLE_VALUE;
- } else {
- self->flags = WRITABLE;
- }
- }
- mp_SetError(PyExc_IOError, res);
- } else {
- if (freeme == NULL) {
- temp = PyBytes_FromStringAndSize(self->buffer, res);
- } else {
- temp = PyBytes_FromStringAndSize(freeme, res);
- PyMem_Free(freeme);
- }
- }
-
- if (temp)
- result = PyObject_CallFunctionObjArgs(pickle_loads,
- temp, NULL);
- Py_XDECREF(temp);
- return result;
+ char *freeme = NULL;
+ Py_ssize_t res;
+ PyObject *temp = NULL, *result = NULL;
+
+ CHECK_READABLE(self);
+
+ res = conn_recv_string(self, self->buffer, CONNECTION_BUFFER_SIZE,
+ &freeme, PY_SSIZE_T_MAX);
+
+ if (res < 0) {
+ if (res == MP_BAD_MESSAGE_LENGTH) {
+ if ((self->flags & WRITABLE) == 0) {
+ Py_BEGIN_ALLOW_THREADS
+ CLOSE(self->handle);
+ Py_END_ALLOW_THREADS
+ self->handle = INVALID_HANDLE_VALUE;
+ } else {
+ self->flags = WRITABLE;
+ }
+ }
+ mp_SetError(PyExc_IOError, res);
+ } else {
+ if (freeme == NULL) {
+ temp = PyBytes_FromStringAndSize(self->buffer, res);
+ } else {
+ temp = PyBytes_FromStringAndSize(freeme, res);
+ PyMem_Free(freeme);
+ }
+ }
+
+ if (temp)
+ result = PyObject_CallFunctionObjArgs(pickle_loads,
+ temp, NULL);
+ Py_XDECREF(temp);
+ return result;
}
/*
@@ -344,73 +344,73 @@ connection_recv_obj(ConnectionObject *self)
static PyObject *
connection_poll(ConnectionObject *self, PyObject *args)
{
- PyObject *timeout_obj = NULL;
- double timeout = 0.0;
- int res;
-
- CHECK_READABLE(self);
-
- if (!PyArg_ParseTuple(args, "|O", &timeout_obj))
- return NULL;
-
- if (timeout_obj == NULL) {
- timeout = 0.0;
- } else if (timeout_obj == Py_None) {
- timeout = -1.0; /* block forever */
- } else {
- timeout = PyFloat_AsDouble(timeout_obj);
- if (PyErr_Occurred())
- return NULL;
- if (timeout < 0.0)
- timeout = 0.0;
- }
-
- Py_BEGIN_ALLOW_THREADS
- res = conn_poll(self, timeout, _save);
- Py_END_ALLOW_THREADS
-
- switch (res) {
- case TRUE:
- Py_RETURN_TRUE;
- case FALSE:
- Py_RETURN_FALSE;
- default:
- return mp_SetError(PyExc_IOError, res);
- }
+ PyObject *timeout_obj = NULL;
+ double timeout = 0.0;
+ int res;
+
+ CHECK_READABLE(self);
+
+ if (!PyArg_ParseTuple(args, "|O", &timeout_obj))
+ return NULL;
+
+ if (timeout_obj == NULL) {
+ timeout = 0.0;
+ } else if (timeout_obj == Py_None) {
+ timeout = -1.0; /* block forever */
+ } else {
+ timeout = PyFloat_AsDouble(timeout_obj);
+ if (PyErr_Occurred())
+ return NULL;
+ if (timeout < 0.0)
+ timeout = 0.0;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ res = conn_poll(self, timeout, _save);
+ Py_END_ALLOW_THREADS
+
+ switch (res) {
+ case TRUE:
+ Py_RETURN_TRUE;
+ case FALSE:
+ Py_RETURN_FALSE;
+ default:
+ return mp_SetError(PyExc_IOError, res);
+ }
}
static PyObject *
connection_fileno(ConnectionObject* self)
{
- if (self->handle == INVALID_HANDLE_VALUE) {
- PyErr_SetString(PyExc_IOError, "handle is invalid");
- return NULL;
- }
- return PyInt_FromLong((long)self->handle);
+ if (self->handle == INVALID_HANDLE_VALUE) {
+ PyErr_SetString(PyExc_IOError, "handle is invalid");
+ return NULL;
+ }
+ return PyInt_FromLong((long)self->handle);
}
static PyObject *
connection_close(ConnectionObject *self)
{
- if (self->handle != INVALID_HANDLE_VALUE) {
- Py_BEGIN_ALLOW_THREADS
- CLOSE(self->handle);
- Py_END_ALLOW_THREADS
- self->handle = INVALID_HANDLE_VALUE;
- }
-
- Py_RETURN_NONE;
+ if (self->handle != INVALID_HANDLE_VALUE) {
+ Py_BEGIN_ALLOW_THREADS
+ CLOSE(self->handle);
+ Py_END_ALLOW_THREADS
+ self->handle = INVALID_HANDLE_VALUE;
+ }
+
+ Py_RETURN_NONE;
}
static PyObject *
connection_repr(ConnectionObject *self)
{
- static char *conn_type[] = {"read-only", "write-only", "read-write"};
+ static char *conn_type[] = {"read-only", "write-only", "read-write"};
- assert(self->flags >= 1 && self->flags <= 3);
- return FROM_FORMAT("<%s %s, handle %zd>",
- conn_type[self->flags - 1],
- CONNECTION_NAME, (Py_ssize_t)self->handle);
+ assert(self->flags >= 1 && self->flags <= 3);
+ return FROM_FORMAT("<%s %s, handle %zd>",
+ conn_type[self->flags - 1],
+ CONNECTION_NAME, (Py_ssize_t)self->handle);
}
/*
@@ -420,19 +420,19 @@ connection_repr(ConnectionObject *self)
static PyObject *
connection_closed(ConnectionObject *self, void *closure)
{
- return PyBool_FromLong((long)(self->handle == INVALID_HANDLE_VALUE));
+ return PyBool_FromLong((long)(self->handle == INVALID_HANDLE_VALUE));
}
static PyObject *
connection_readable(ConnectionObject *self, void *closure)
{
- return PyBool_FromLong((long)(self->flags & READABLE));
+ return PyBool_FromLong((long)(self->flags & READABLE));
}
static PyObject *
connection_writable(ConnectionObject *self, void *closure)
{
- return PyBool_FromLong((long)(self->flags & WRITABLE));
+ return PyBool_FromLong((long)(self->flags & WRITABLE));
}
/*
@@ -440,37 +440,37 @@ connection_writable(ConnectionObject *self, void *closure)
*/
static PyMethodDef connection_methods[] = {
- {"send_bytes", (PyCFunction)connection_sendbytes, METH_VARARGS,
- "send the byte data from a readable buffer-like object"},
- {"recv_bytes", (PyCFunction)connection_recvbytes, METH_VARARGS,
- "receive byte data as a string"},
- {"recv_bytes_into",(PyCFunction)connection_recvbytes_into,METH_VARARGS,
- "receive byte data into a writeable buffer-like object\n"
- "returns the number of bytes read"},
-
- {"send", (PyCFunction)connection_send_obj, METH_O,
- "send a (picklable) object"},
- {"recv", (PyCFunction)connection_recv_obj, METH_NOARGS,
- "receive a (picklable) object"},
-
- {"poll", (PyCFunction)connection_poll, METH_VARARGS,
- "whether there is any input available to be read"},
- {"fileno", (PyCFunction)connection_fileno, METH_NOARGS,
- "file descriptor or handle of the connection"},
- {"close", (PyCFunction)connection_close, METH_NOARGS,
- "close the connection"},
-
- {NULL} /* Sentinel */
+ {"send_bytes", (PyCFunction)connection_sendbytes, METH_VARARGS,
+ "send the byte data from a readable buffer-like object"},
+ {"recv_bytes", (PyCFunction)connection_recvbytes, METH_VARARGS,
+ "receive byte data as a string"},
+ {"recv_bytes_into",(PyCFunction)connection_recvbytes_into,METH_VARARGS,
+ "receive byte data into a writeable buffer-like object\n"
+ "returns the number of bytes read"},
+
+ {"send", (PyCFunction)connection_send_obj, METH_O,
+ "send a (picklable) object"},
+ {"recv", (PyCFunction)connection_recv_obj, METH_NOARGS,
+ "receive a (picklable) object"},
+
+ {"poll", (PyCFunction)connection_poll, METH_VARARGS,
+ "whether there is any input available to be read"},
+ {"fileno", (PyCFunction)connection_fileno, METH_NOARGS,
+ "file descriptor or handle of the connection"},
+ {"close", (PyCFunction)connection_close, METH_NOARGS,
+ "close the connection"},
+
+ {NULL} /* Sentinel */
};
static PyGetSetDef connection_getset[] = {
- {"closed", (getter)connection_closed, NULL,
- "True if the connection is closed", NULL},
- {"readable", (getter)connection_readable, NULL,
- "True if the connection is readable", NULL},
- {"writable", (getter)connection_writable, NULL,
- "True if the connection is writable", NULL},
- {NULL}
+ {"closed", (getter)connection_closed, NULL,
+ "True if the connection is closed", NULL},
+ {"readable", (getter)connection_readable, NULL,
+ "True if the connection is readable", NULL},
+ {"writable", (getter)connection_writable, NULL,
+ "True if the connection is writable", NULL},
+ {NULL}
};
/*
@@ -478,50 +478,50 @@ static PyGetSetDef connection_getset[] = {
*/
PyDoc_STRVAR(connection_doc,
- "Connection type whose constructor signature is\n\n"
- " Connection(handle, readable=True, writable=True).\n\n"
- "The constructor does *not* duplicate the handle.");
+ "Connection type whose constructor signature is\n\n"
+ " Connection(handle, readable=True, writable=True).\n\n"
+ "The constructor does *not* duplicate the handle.");
PyTypeObject CONNECTION_TYPE = {
- PyVarObject_HEAD_INIT(NULL, 0)
- /* tp_name */ "_multiprocessing." CONNECTION_NAME,
- /* tp_basicsize */ sizeof(ConnectionObject),
- /* tp_itemsize */ 0,
- /* tp_dealloc */ (destructor)connection_dealloc,
- /* tp_print */ 0,
- /* tp_getattr */ 0,
- /* tp_setattr */ 0,
- /* tp_reserved */ 0,
- /* tp_repr */ (reprfunc)connection_repr,
- /* tp_as_number */ 0,
- /* tp_as_sequence */ 0,
- /* tp_as_mapping */ 0,
- /* tp_hash */ 0,
- /* tp_call */ 0,
- /* tp_str */ 0,
- /* tp_getattro */ 0,
- /* tp_setattro */ 0,
- /* tp_as_buffer */ 0,
- /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_HAVE_WEAKREFS,
- /* tp_doc */ connection_doc,
- /* tp_traverse */ 0,
- /* tp_clear */ 0,
- /* tp_richcompare */ 0,
- /* tp_weaklistoffset */ offsetof(ConnectionObject, weakreflist),
- /* tp_iter */ 0,
- /* tp_iternext */ 0,
- /* tp_methods */ connection_methods,
- /* tp_members */ 0,
- /* tp_getset */ connection_getset,
- /* tp_base */ 0,
- /* tp_dict */ 0,
- /* tp_descr_get */ 0,
- /* tp_descr_set */ 0,
- /* tp_dictoffset */ 0,
- /* tp_init */ 0,
- /* tp_alloc */ 0,
- /* tp_new */ connection_new,
+ PyVarObject_HEAD_INIT(NULL, 0)
+ /* tp_name */ "_multiprocessing." CONNECTION_NAME,
+ /* tp_basicsize */ sizeof(ConnectionObject),
+ /* tp_itemsize */ 0,
+ /* tp_dealloc */ (destructor)connection_dealloc,
+ /* tp_print */ 0,
+ /* tp_getattr */ 0,
+ /* tp_setattr */ 0,
+ /* tp_reserved */ 0,
+ /* tp_repr */ (reprfunc)connection_repr,
+ /* tp_as_number */ 0,
+ /* tp_as_sequence */ 0,
+ /* tp_as_mapping */ 0,
+ /* tp_hash */ 0,
+ /* tp_call */ 0,
+ /* tp_str */ 0,
+ /* tp_getattro */ 0,
+ /* tp_setattro */ 0,
+ /* tp_as_buffer */ 0,
+ /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_WEAKREFS,
+ /* tp_doc */ connection_doc,
+ /* tp_traverse */ 0,
+ /* tp_clear */ 0,
+ /* tp_richcompare */ 0,
+ /* tp_weaklistoffset */ offsetof(ConnectionObject, weakreflist),
+ /* tp_iter */ 0,
+ /* tp_iternext */ 0,
+ /* tp_methods */ connection_methods,
+ /* tp_members */ 0,
+ /* tp_getset */ connection_getset,
+ /* tp_base */ 0,
+ /* tp_dict */ 0,
+ /* tp_descr_get */ 0,
+ /* tp_descr_set */ 0,
+ /* tp_dictoffset */ 0,
+ /* tp_init */ 0,
+ /* tp_alloc */ 0,
+ /* tp_new */ connection_new,
};
#endif /* CONNECTION_H */
diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c
index b09fb25..c584f2b 100644
--- a/Modules/_multiprocessing/multiprocessing.c
+++ b/Modules/_multiprocessing/multiprocessing.c
@@ -9,9 +9,9 @@
#include "multiprocessing.h"
#ifdef SCM_RIGHTS
- #define HAVE_FD_TRANSFER 1
+ #define HAVE_FD_TRANSFER 1
#else
- #define HAVE_FD_TRANSFER 0
+ #define HAVE_FD_TRANSFER 0
#endif
PyObject *create_win32_namespace(void);
@@ -26,46 +26,46 @@ PyObject *ProcessError, *BufferTooShort;
PyObject *
mp_SetError(PyObject *Type, int num)
{
- switch (num) {
+ switch (num) {
#ifdef MS_WINDOWS
- case MP_STANDARD_ERROR:
- if (Type == NULL)
- Type = PyExc_WindowsError;
- PyErr_SetExcFromWindowsErr(Type, 0);
- break;
- case MP_SOCKET_ERROR:
- if (Type == NULL)
- Type = PyExc_WindowsError;
- PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
- break;
+ case MP_STANDARD_ERROR:
+ if (Type == NULL)
+ Type = PyExc_WindowsError;
+ PyErr_SetExcFromWindowsErr(Type, 0);
+ break;
+ case MP_SOCKET_ERROR:
+ if (Type == NULL)
+ Type = PyExc_WindowsError;
+ PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
+ break;
#else /* !MS_WINDOWS */
- case MP_STANDARD_ERROR:
- case MP_SOCKET_ERROR:
- if (Type == NULL)
- Type = PyExc_OSError;
- PyErr_SetFromErrno(Type);
- break;
+ case MP_STANDARD_ERROR:
+ case MP_SOCKET_ERROR:
+ if (Type == NULL)
+ Type = PyExc_OSError;
+ PyErr_SetFromErrno(Type);
+ break;
#endif /* !MS_WINDOWS */
- case MP_MEMORY_ERROR:
- PyErr_NoMemory();
- break;
- case MP_END_OF_FILE:
- PyErr_SetNone(PyExc_EOFError);
- break;
- case MP_EARLY_END_OF_FILE:
- PyErr_SetString(PyExc_IOError,
- "got end of file during message");
- break;
- case MP_BAD_MESSAGE_LENGTH:
- PyErr_SetString(PyExc_IOError, "bad message length");
- break;
- case MP_EXCEPTION_HAS_BEEN_SET:
- break;
- default:
- PyErr_Format(PyExc_RuntimeError,
- "unkown error number %d", num);
- }
- return NULL;
+ case MP_MEMORY_ERROR:
+ PyErr_NoMemory();
+ break;
+ case MP_END_OF_FILE:
+ PyErr_SetNone(PyExc_EOFError);
+ break;
+ case MP_EARLY_END_OF_FILE:
+ PyErr_SetString(PyExc_IOError,
+ "got end of file during message");
+ break;
+ case MP_BAD_MESSAGE_LENGTH:
+ PyErr_SetString(PyExc_IOError, "bad message length");
+ break;
+ case MP_EXCEPTION_HAS_BEEN_SET:
+ break;
+ default:
+ PyErr_Format(PyExc_RuntimeError,
+ "unkown error number %d", num);
+ }
+ return NULL;
}
@@ -82,8 +82,8 @@ HANDLE sigint_event = NULL;
static BOOL WINAPI
ProcessingCtrlHandler(DWORD dwCtrlType)
{
- SetEvent(sigint_event);
- return FALSE;
+ SetEvent(sigint_event);
+ return FALSE;
}
/*
@@ -101,72 +101,72 @@ ProcessingCtrlHandler(DWORD dwCtrlType)
static PyObject *
multiprocessing_sendfd(PyObject *self, PyObject *args)
{
- int conn, fd, res;
- char dummy_char;
- char buf[CMSG_SPACE(sizeof(int))];
- struct msghdr msg = {0};
- struct iovec dummy_iov;
- struct cmsghdr *cmsg;
-
- if (!PyArg_ParseTuple(args, "ii", &conn, &fd))
- return NULL;
-
- dummy_iov.iov_base = &dummy_char;
- dummy_iov.iov_len = 1;
- msg.msg_control = buf;
- msg.msg_controllen = sizeof(buf);
- msg.msg_iov = &dummy_iov;
- msg.msg_iovlen = 1;
- cmsg = CMSG_FIRSTHDR(&msg);
- cmsg->cmsg_level = SOL_SOCKET;
- cmsg->cmsg_type = SCM_RIGHTS;
- cmsg->cmsg_len = CMSG_LEN(sizeof(int));
- msg.msg_controllen = cmsg->cmsg_len;
- *(int*)CMSG_DATA(cmsg) = fd;
-
- Py_BEGIN_ALLOW_THREADS
- res = sendmsg(conn, &msg, 0);
- Py_END_ALLOW_THREADS
-
- if (res < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
- Py_RETURN_NONE;
+ int conn, fd, res;
+ char dummy_char;
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = {0};
+ struct iovec dummy_iov;
+ struct cmsghdr *cmsg;
+
+ if (!PyArg_ParseTuple(args, "ii", &conn, &fd))
+ return NULL;
+
+ dummy_iov.iov_base = &dummy_char;
+ dummy_iov.iov_len = 1;
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+ msg.msg_iov = &dummy_iov;
+ msg.msg_iovlen = 1;
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ msg.msg_controllen = cmsg->cmsg_len;
+ *(int*)CMSG_DATA(cmsg) = fd;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = sendmsg(conn, &msg, 0);
+ Py_END_ALLOW_THREADS
+
+ if (res < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+ Py_RETURN_NONE;
}
static PyObject *
multiprocessing_recvfd(PyObject *self, PyObject *args)
{
- int conn, fd, res;
- char dummy_char;
- char buf[CMSG_SPACE(sizeof(int))];
- struct msghdr msg = {0};
- struct iovec dummy_iov;
- struct cmsghdr *cmsg;
-
- if (!PyArg_ParseTuple(args, "i", &conn))
- return NULL;
-
- dummy_iov.iov_base = &dummy_char;
- dummy_iov.iov_len = 1;
- msg.msg_control = buf;
- msg.msg_controllen = sizeof(buf);
- msg.msg_iov = &dummy_iov;
- msg.msg_iovlen = 1;
- cmsg = CMSG_FIRSTHDR(&msg);
- cmsg->cmsg_level = SOL_SOCKET;
- cmsg->cmsg_type = SCM_RIGHTS;
- cmsg->cmsg_len = CMSG_LEN(sizeof(int));
- msg.msg_controllen = cmsg->cmsg_len;
-
- Py_BEGIN_ALLOW_THREADS
- res = recvmsg(conn, &msg, 0);
- Py_END_ALLOW_THREADS
-
- if (res < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
-
- fd = *(int*)CMSG_DATA(cmsg);
- return Py_BuildValue("i", fd);
+ int conn, fd, res;
+ char dummy_char;
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = {0};
+ struct iovec dummy_iov;
+ struct cmsghdr *cmsg;
+
+ if (!PyArg_ParseTuple(args, "i", &conn))
+ return NULL;
+
+ dummy_iov.iov_base = &dummy_char;
+ dummy_iov.iov_len = 1;
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+ msg.msg_iov = &dummy_iov;
+ msg.msg_iovlen = 1;
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ msg.msg_controllen = cmsg->cmsg_len;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = recvmsg(conn, &msg, 0);
+ Py_END_ALLOW_THREADS
+
+ if (res < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ fd = *(int*)CMSG_DATA(cmsg);
+ return Py_BuildValue("i", fd);
}
#endif /* HAVE_FD_TRANSFER */
@@ -181,14 +181,14 @@ multiprocessing_recvfd(PyObject *self, PyObject *args)
static PyObject*
multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
{
- void *buffer;
- Py_ssize_t buffer_len;
+ void *buffer;
+ Py_ssize_t buffer_len;
- if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
- return NULL;
+ if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
+ return NULL;
- return Py_BuildValue("N" F_PY_SSIZE_T,
- PyLong_FromVoidPtr(buffer), buffer_len);
+ return Py_BuildValue("N" F_PY_SSIZE_T,
+ PyLong_FromVoidPtr(buffer), buffer_len);
}
@@ -197,20 +197,20 @@ multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
*/
static PyMethodDef module_methods[] = {
- {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
- "address_of_buffer(obj) -> int\n"
- "Return address of obj assuming obj supports buffer inteface"},
+ {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
+ "address_of_buffer(obj) -> int\n"
+ "Return address of obj assuming obj supports buffer inteface"},
#if HAVE_FD_TRANSFER
- {"sendfd", multiprocessing_sendfd, METH_VARARGS,
- "sendfd(sockfd, fd) -> None\n"
- "Send file descriptor given by fd over the unix domain socket\n"
- "whose file decriptor is sockfd"},
- {"recvfd", multiprocessing_recvfd, METH_VARARGS,
- "recvfd(sockfd) -> fd\n"
- "Receive a file descriptor over a unix domain socket\n"
- "whose file decriptor is sockfd"},
+ {"sendfd", multiprocessing_sendfd, METH_VARARGS,
+ "sendfd(sockfd, fd) -> None\n"
+ "Send file descriptor given by fd over the unix domain socket\n"
+ "whose file decriptor is sockfd"},
+ {"recvfd", multiprocessing_recvfd, METH_VARARGS,
+ "recvfd(sockfd) -> fd\n"
+ "Receive a file descriptor over a unix domain socket\n"
+ "whose file decriptor is sockfd"},
#endif
- {NULL}
+ {NULL}
};
@@ -219,117 +219,117 @@ static PyMethodDef module_methods[] = {
*/
static struct PyModuleDef multiprocessing_module = {
- PyModuleDef_HEAD_INIT,
- "_multiprocessing",
- NULL,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_multiprocessing",
+ NULL,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
-PyMODINIT_FUNC
+PyMODINIT_FUNC
PyInit__multiprocessing(void)
{
- PyObject *module, *temp, *value;
-
- /* Initialize module */
- module = PyModule_Create(&multiprocessing_module);
- if (!module)
- return NULL;
-
- /* Get copy of objects from pickle */
- temp = PyImport_ImportModule(PICKLE_MODULE);
- if (!temp)
- return NULL;
- pickle_dumps = PyObject_GetAttrString(temp, "dumps");
- pickle_loads = PyObject_GetAttrString(temp, "loads");
- pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL");
- Py_XDECREF(temp);
-
- /* Get copy of BufferTooShort */
- temp = PyImport_ImportModule("multiprocessing");
- if (!temp)
- return NULL;
- BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
- Py_XDECREF(temp);
-
- /* Add connection type to module */
- if (PyType_Ready(&ConnectionType) < 0)
- return NULL;
- Py_INCREF(&ConnectionType);
- PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);
-
-#if defined(MS_WINDOWS) || \
+ PyObject *module, *temp, *value;
+
+ /* Initialize module */
+ module = PyModule_Create(&multiprocessing_module);
+ if (!module)
+ return NULL;
+
+ /* Get copy of objects from pickle */
+ temp = PyImport_ImportModule(PICKLE_MODULE);
+ if (!temp)
+ return NULL;
+ pickle_dumps = PyObject_GetAttrString(temp, "dumps");
+ pickle_loads = PyObject_GetAttrString(temp, "loads");
+ pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL");
+ Py_XDECREF(temp);
+
+ /* Get copy of BufferTooShort */
+ temp = PyImport_ImportModule("multiprocessing");
+ if (!temp)
+ return NULL;
+ BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
+ Py_XDECREF(temp);
+
+ /* Add connection type to module */
+ if (PyType_Ready(&ConnectionType) < 0)
+ return NULL;
+ Py_INCREF(&ConnectionType);
+ PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);
+
+#if defined(MS_WINDOWS) || \
(defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
- /* Add SemLock type to module */
- if (PyType_Ready(&SemLockType) < 0)
- return NULL;
- Py_INCREF(&SemLockType);
- PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
- Py_BuildValue("i", SEM_VALUE_MAX));
- PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
+ /* Add SemLock type to module */
+ if (PyType_Ready(&SemLockType) < 0)
+ return NULL;
+ Py_INCREF(&SemLockType);
+ PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
+ Py_BuildValue("i", SEM_VALUE_MAX));
+ PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
#endif
#ifdef MS_WINDOWS
- /* Add PipeConnection to module */
- if (PyType_Ready(&PipeConnectionType) < 0)
- return NULL;
- Py_INCREF(&PipeConnectionType);
- PyModule_AddObject(module, "PipeConnection",
- (PyObject*)&PipeConnectionType);
-
- /* Initialize win32 class and add to multiprocessing */
- temp = create_win32_namespace();
- if (!temp)
- return NULL;
- PyModule_AddObject(module, "win32", temp);
-
- /* Initialize the event handle used to signal Ctrl-C */
- sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (!sigint_event) {
- PyErr_SetFromWindowsErr(0);
- return NULL;
- }
- if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
- PyErr_SetFromWindowsErr(0);
- return NULL;
- }
+ /* Add PipeConnection to module */
+ if (PyType_Ready(&PipeConnectionType) < 0)
+ return NULL;
+ Py_INCREF(&PipeConnectionType);
+ PyModule_AddObject(module, "PipeConnection",
+ (PyObject*)&PipeConnectionType);
+
+ /* Initialize win32 class and add to multiprocessing */
+ temp = create_win32_namespace();
+ if (!temp)
+ return NULL;
+ PyModule_AddObject(module, "win32", temp);
+
+ /* Initialize the event handle used to signal Ctrl-C */
+ sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!sigint_event) {
+ PyErr_SetFromWindowsErr(0);
+ return NULL;
+ }
+ if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
+ PyErr_SetFromWindowsErr(0);
+ return NULL;
+ }
#endif
- /* Add configuration macros */
- temp = PyDict_New();
- if (!temp)
- return NULL;
-
-#define ADD_FLAG(name) \
- value = Py_BuildValue("i", name); \
- if (value == NULL) { Py_DECREF(temp); return NULL; } \
- if (PyDict_SetItemString(temp, #name, value) < 0) { \
- Py_DECREF(temp); Py_DECREF(value); return NULL; } \
- Py_DECREF(value)
-
+ /* Add configuration macros */
+ temp = PyDict_New();
+ if (!temp)
+ return NULL;
+
+#define ADD_FLAG(name) \
+ value = Py_BuildValue("i", name); \
+ if (value == NULL) { Py_DECREF(temp); return NULL; } \
+ if (PyDict_SetItemString(temp, #name, value) < 0) { \
+ Py_DECREF(temp); Py_DECREF(value); return NULL; } \
+ Py_DECREF(value)
+
#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
- ADD_FLAG(HAVE_SEM_OPEN);
+ ADD_FLAG(HAVE_SEM_OPEN);
#endif
#ifdef HAVE_SEM_TIMEDWAIT
- ADD_FLAG(HAVE_SEM_TIMEDWAIT);
+ ADD_FLAG(HAVE_SEM_TIMEDWAIT);
#endif
#ifdef HAVE_FD_TRANSFER
- ADD_FLAG(HAVE_FD_TRANSFER);
+ ADD_FLAG(HAVE_FD_TRANSFER);
#endif
#ifdef HAVE_BROKEN_SEM_GETVALUE
- ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
+ ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
#endif
#ifdef HAVE_BROKEN_SEM_UNLINK
- ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
+ ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
#endif
- if (PyModule_AddObject(module, "flags", temp) < 0)
- return NULL;
+ if (PyModule_AddObject(module, "flags", temp) < 0)
+ return NULL;
- return module;
+ return module;
}
diff --git a/Modules/_multiprocessing/multiprocessing.h b/Modules/_multiprocessing/multiprocessing.h
index 3dd0199..c149634 100644
--- a/Modules/_multiprocessing/multiprocessing.h
+++ b/Modules/_multiprocessing/multiprocessing.h
@@ -15,7 +15,7 @@
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <winsock2.h>
-# include <process.h> /* getpid() */
+# include <process.h> /* getpid() */
# ifdef Py_DEBUG
# include <crtdbg.h>
# endif
@@ -45,15 +45,15 @@
* Issue 3110 - Solaris does not define SEM_VALUE_MAX
*/
#ifndef SEM_VALUE_MAX
- #if defined(HAVE_SYSCONF) && defined(_SC_SEM_VALUE_MAX)
- # define SEM_VALUE_MAX sysconf(_SC_SEM_VALUE_MAX)
- #elif defined(_SEM_VALUE_MAX)
- # define SEM_VALUE_MAX _SEM_VALUE_MAX
- #elif defined(_POSIX_SEM_VALUE_MAX)
- # define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX
- #else
- # define SEM_VALUE_MAX INT_MAX
- #endif
+ #if defined(HAVE_SYSCONF) && defined(_SC_SEM_VALUE_MAX)
+ # define SEM_VALUE_MAX sysconf(_SC_SEM_VALUE_MAX)
+ #elif defined(_SEM_VALUE_MAX)
+ # define SEM_VALUE_MAX _SEM_VALUE_MAX
+ #elif defined(_POSIX_SEM_VALUE_MAX)
+ # define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX
+ #else
+ # define SEM_VALUE_MAX INT_MAX
+ #endif
#endif
@@ -162,11 +162,11 @@ extern HANDLE sigint_event;
#define CONNECTION_BUFFER_SIZE 1024
typedef struct {
- PyObject_HEAD
- HANDLE handle;
- int flags;
- PyObject *weakreflist;
- char buffer[CONNECTION_BUFFER_SIZE];
+ PyObject_HEAD
+ HANDLE handle;
+ int flags;
+ PyObject *weakreflist;
+ char buffer[CONNECTION_BUFFER_SIZE];
} ConnectionObject;
/*
diff --git a/Modules/_multiprocessing/pipe_connection.c b/Modules/_multiprocessing/pipe_connection.c
index 66947c8..05dde0c 100644
--- a/Modules/_multiprocessing/pipe_connection.c
+++ b/Modules/_multiprocessing/pipe_connection.c
@@ -17,19 +17,19 @@
static Py_ssize_t
conn_send_string(ConnectionObject *conn, char *string, size_t length)
{
- DWORD amount_written;
- BOOL ret;
+ DWORD amount_written;
+ BOOL ret;
- Py_BEGIN_ALLOW_THREADS
- ret = WriteFile(conn->handle, string, length, &amount_written, NULL);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ ret = WriteFile(conn->handle, string, length, &amount_written, NULL);
+ Py_END_ALLOW_THREADS
- if (ret == 0 && GetLastError() == ERROR_NO_SYSTEM_RESOURCES) {
- PyErr_Format(PyExc_ValueError, "Cannnot send %" PY_FORMAT_SIZE_T "d bytes over connection", length);
- return MP_STANDARD_ERROR;
- }
+ if (ret == 0 && GetLastError() == ERROR_NO_SYSTEM_RESOURCES) {
+ PyErr_Format(PyExc_ValueError, "Cannnot send %" PY_FORMAT_SIZE_T "d bytes over connection", length);
+ return MP_STANDARD_ERROR;
+ }
- return ret ? MP_SUCCESS : MP_STANDARD_ERROR;
+ return ret ? MP_SUCCESS : MP_STANDARD_ERROR;
}
/*
@@ -39,50 +39,50 @@ conn_send_string(ConnectionObject *conn, char *string, size_t length)
*/
static Py_ssize_t
-conn_recv_string(ConnectionObject *conn, char *buffer,
- size_t buflength, char **newbuffer, size_t maxlength)
+conn_recv_string(ConnectionObject *conn, char *buffer,
+ size_t buflength, char **newbuffer, size_t maxlength)
{
- DWORD left, length, full_length, err;
- BOOL ret;
- *newbuffer = NULL;
-
- Py_BEGIN_ALLOW_THREADS
- ret = ReadFile(conn->handle, buffer, MIN(buflength, maxlength),
- &length, NULL);
- Py_END_ALLOW_THREADS
- if (ret)
- return length;
-
- err = GetLastError();
- if (err != ERROR_MORE_DATA) {
- if (err == ERROR_BROKEN_PIPE)
- return MP_END_OF_FILE;
- return MP_STANDARD_ERROR;
- }
-
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
- return MP_STANDARD_ERROR;
-
- full_length = length + left;
- if (full_length > maxlength)
- return MP_BAD_MESSAGE_LENGTH;
-
- *newbuffer = PyMem_Malloc(full_length);
- if (*newbuffer == NULL)
- return MP_MEMORY_ERROR;
-
- memcpy(*newbuffer, buffer, length);
-
- Py_BEGIN_ALLOW_THREADS
- ret = ReadFile(conn->handle, *newbuffer+length, left, &length, NULL);
- Py_END_ALLOW_THREADS
- if (ret) {
- assert(length == left);
- return full_length;
- } else {
- PyMem_Free(*newbuffer);
- return MP_STANDARD_ERROR;
- }
+ DWORD left, length, full_length, err;
+ BOOL ret;
+ *newbuffer = NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ ret = ReadFile(conn->handle, buffer, MIN(buflength, maxlength),
+ &length, NULL);
+ Py_END_ALLOW_THREADS
+ if (ret)
+ return length;
+
+ err = GetLastError();
+ if (err != ERROR_MORE_DATA) {
+ if (err == ERROR_BROKEN_PIPE)
+ return MP_END_OF_FILE;
+ return MP_STANDARD_ERROR;
+ }
+
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
+ return MP_STANDARD_ERROR;
+
+ full_length = length + left;
+ if (full_length > maxlength)
+ return MP_BAD_MESSAGE_LENGTH;
+
+ *newbuffer = PyMem_Malloc(full_length);
+ if (*newbuffer == NULL)
+ return MP_MEMORY_ERROR;
+
+ memcpy(*newbuffer, buffer, length);
+
+ Py_BEGIN_ALLOW_THREADS
+ ret = ReadFile(conn->handle, *newbuffer+length, left, &length, NULL);
+ Py_END_ALLOW_THREADS
+ if (ret) {
+ assert(length == left);
+ return full_length;
+ } else {
+ PyMem_Free(*newbuffer);
+ return MP_STANDARD_ERROR;
+ }
}
/*
@@ -92,51 +92,51 @@ conn_recv_string(ConnectionObject *conn, char *buffer,
static int
conn_poll(ConnectionObject *conn, double timeout, PyThreadState *_save)
{
- DWORD bytes, deadline, delay;
- int difference, res;
- BOOL block = FALSE;
-
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
- return MP_STANDARD_ERROR;
-
- if (timeout == 0.0)
- return bytes > 0;
-
- if (timeout < 0.0)
- block = TRUE;
- else
- /* XXX does not check for overflow */
- deadline = GetTickCount() + (DWORD)(1000 * timeout + 0.5);
-
- Sleep(0);
-
- for (delay = 1 ; ; delay += 1) {
- if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
- return MP_STANDARD_ERROR;
- else if (bytes > 0)
- return TRUE;
-
- if (!block) {
- difference = deadline - GetTickCount();
- if (difference < 0)
- return FALSE;
- if ((int)delay > difference)
- delay = difference;
- }
-
- if (delay > 20)
- delay = 20;
-
- Sleep(delay);
-
- /* check for signals */
- Py_BLOCK_THREADS
- res = PyErr_CheckSignals();
- Py_UNBLOCK_THREADS
-
- if (res)
- return MP_EXCEPTION_HAS_BEEN_SET;
- }
+ DWORD bytes, deadline, delay;
+ int difference, res;
+ BOOL block = FALSE;
+
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
+ return MP_STANDARD_ERROR;
+
+ if (timeout == 0.0)
+ return bytes > 0;
+
+ if (timeout < 0.0)
+ block = TRUE;
+ else
+ /* XXX does not check for overflow */
+ deadline = GetTickCount() + (DWORD)(1000 * timeout + 0.5);
+
+ Sleep(0);
+
+ for (delay = 1 ; ; delay += 1) {
+ if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
+ return MP_STANDARD_ERROR;
+ else if (bytes > 0)
+ return TRUE;
+
+ if (!block) {
+ difference = deadline - GetTickCount();
+ if (difference < 0)
+ return FALSE;
+ if ((int)delay > difference)
+ delay = difference;
+ }
+
+ if (delay > 20)
+ delay = 20;
+
+ Sleep(delay);
+
+ /* check for signals */
+ Py_BLOCK_THREADS
+ res = PyErr_CheckSignals();
+ Py_UNBLOCK_THREADS
+
+ if (res)
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ }
}
/*
diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c
index c0944c9..3ab0314 100644
--- a/Modules/_multiprocessing/semaphore.c
+++ b/Modules/_multiprocessing/semaphore.c
@@ -11,12 +11,12 @@
enum { RECURSIVE_MUTEX, SEMAPHORE };
typedef struct {
- PyObject_HEAD
- SEM_HANDLE handle;
- long last_tid;
- int count;
- int maxvalue;
- int kind;
+ PyObject_HEAD
+ SEM_HANDLE handle;
+ long last_tid;
+ int count;
+ int maxvalue;
+ int kind;
} SemLockObject;
#define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
@@ -40,148 +40,148 @@ typedef struct {
static int
_GetSemaphoreValue(HANDLE handle, long *value)
{
- long previous;
-
- switch (WaitForSingleObject(handle, 0)) {
- case WAIT_OBJECT_0:
- if (!ReleaseSemaphore(handle, 1, &previous))
- return MP_STANDARD_ERROR;
- *value = previous + 1;
- return 0;
- case WAIT_TIMEOUT:
- *value = 0;
- return 0;
- default:
- return MP_STANDARD_ERROR;
- }
+ long previous;
+
+ switch (WaitForSingleObject(handle, 0)) {
+ case WAIT_OBJECT_0:
+ if (!ReleaseSemaphore(handle, 1, &previous))
+ return MP_STANDARD_ERROR;
+ *value = previous + 1;
+ return 0;
+ case WAIT_TIMEOUT:
+ *value = 0;
+ return 0;
+ default:
+ return MP_STANDARD_ERROR;
+ }
}
static PyObject *
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
{
- int blocking = 1;
- double timeout;
- PyObject *timeout_obj = Py_None;
- DWORD res, full_msecs, msecs, start, ticks;
-
- static char *kwlist[] = {"block", "timeout", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
- &blocking, &timeout_obj))
- return NULL;
-
- /* calculate timeout */
- if (!blocking) {
- full_msecs = 0;
- } else if (timeout_obj == Py_None) {
- full_msecs = INFINITE;
- } else {
- timeout = PyFloat_AsDouble(timeout_obj);
- if (PyErr_Occurred())
- return NULL;
- timeout *= 1000.0; /* convert to millisecs */
- if (timeout < 0.0) {
- timeout = 0.0;
- } else if (timeout >= 0.5 * INFINITE) { /* 25 days */
- PyErr_SetString(PyExc_OverflowError,
- "timeout is too large");
- return NULL;
- }
- full_msecs = (DWORD)(timeout + 0.5);
- }
-
- /* check whether we already own the lock */
- if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
- ++self->count;
- Py_RETURN_TRUE;
- }
-
- /* check whether we can acquire without blocking */
- if (WaitForSingleObject(self->handle, 0) == WAIT_OBJECT_0) {
- self->last_tid = GetCurrentThreadId();
- ++self->count;
- Py_RETURN_TRUE;
- }
-
- msecs = full_msecs;
- start = GetTickCount();
-
- for ( ; ; ) {
- HANDLE handles[2] = {self->handle, sigint_event};
-
- /* do the wait */
- Py_BEGIN_ALLOW_THREADS
- ResetEvent(sigint_event);
- res = WaitForMultipleObjects(2, handles, FALSE, msecs);
- Py_END_ALLOW_THREADS
-
- /* handle result */
- if (res != WAIT_OBJECT_0 + 1)
- break;
-
- /* got SIGINT so give signal handler a chance to run */
- Sleep(1);
-
- /* if this is main thread let KeyboardInterrupt be raised */
- if (PyErr_CheckSignals())
- return NULL;
-
- /* recalculate timeout */
- if (msecs != INFINITE) {
- ticks = GetTickCount();
- if ((DWORD)(ticks - start) >= full_msecs)
- Py_RETURN_FALSE;
- msecs = full_msecs - (ticks - start);
- }
- }
-
- /* handle result */
- switch (res) {
- case WAIT_TIMEOUT:
- Py_RETURN_FALSE;
- case WAIT_OBJECT_0:
- self->last_tid = GetCurrentThreadId();
- ++self->count;
- Py_RETURN_TRUE;
- case WAIT_FAILED:
- return PyErr_SetFromWindowsErr(0);
- default:
- PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
- "WaitForMultipleObjects() gave unrecognized "
- "value %d", res);
- return NULL;
- }
+ int blocking = 1;
+ double timeout;
+ PyObject *timeout_obj = Py_None;
+ DWORD res, full_msecs, msecs, start, ticks;
+
+ static char *kwlist[] = {"block", "timeout", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
+ &blocking, &timeout_obj))
+ return NULL;
+
+ /* calculate timeout */
+ if (!blocking) {
+ full_msecs = 0;
+ } else if (timeout_obj == Py_None) {
+ full_msecs = INFINITE;
+ } else {
+ timeout = PyFloat_AsDouble(timeout_obj);
+ if (PyErr_Occurred())
+ return NULL;
+ timeout *= 1000.0; /* convert to millisecs */
+ if (timeout < 0.0) {
+ timeout = 0.0;
+ } else if (timeout >= 0.5 * INFINITE) { /* 25 days */
+ PyErr_SetString(PyExc_OverflowError,
+ "timeout is too large");
+ return NULL;
+ }
+ full_msecs = (DWORD)(timeout + 0.5);
+ }
+
+ /* check whether we already own the lock */
+ if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
+ ++self->count;
+ Py_RETURN_TRUE;
+ }
+
+ /* check whether we can acquire without blocking */
+ if (WaitForSingleObject(self->handle, 0) == WAIT_OBJECT_0) {
+ self->last_tid = GetCurrentThreadId();
+ ++self->count;
+ Py_RETURN_TRUE;
+ }
+
+ msecs = full_msecs;
+ start = GetTickCount();
+
+ for ( ; ; ) {
+ HANDLE handles[2] = {self->handle, sigint_event};
+
+ /* do the wait */
+ Py_BEGIN_ALLOW_THREADS
+ ResetEvent(sigint_event);
+ res = WaitForMultipleObjects(2, handles, FALSE, msecs);
+ Py_END_ALLOW_THREADS
+
+ /* handle result */
+ if (res != WAIT_OBJECT_0 + 1)
+ break;
+
+ /* got SIGINT so give signal handler a chance to run */
+ Sleep(1);
+
+ /* if this is main thread let KeyboardInterrupt be raised */
+ if (PyErr_CheckSignals())
+ return NULL;
+
+ /* recalculate timeout */
+ if (msecs != INFINITE) {
+ ticks = GetTickCount();
+ if ((DWORD)(ticks - start) >= full_msecs)
+ Py_RETURN_FALSE;
+ msecs = full_msecs - (ticks - start);
+ }
+ }
+
+ /* handle result */
+ switch (res) {
+ case WAIT_TIMEOUT:
+ Py_RETURN_FALSE;
+ case WAIT_OBJECT_0:
+ self->last_tid = GetCurrentThreadId();
+ ++self->count;
+ Py_RETURN_TRUE;
+ case WAIT_FAILED:
+ return PyErr_SetFromWindowsErr(0);
+ default:
+ PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
+ "WaitForMultipleObjects() gave unrecognized "
+ "value %d", res);
+ return NULL;
+ }
}
static PyObject *
semlock_release(SemLockObject *self, PyObject *args)
{
- if (self->kind == RECURSIVE_MUTEX) {
- if (!ISMINE(self)) {
- PyErr_SetString(PyExc_AssertionError, "attempt to "
- "release recursive lock not owned "
- "by thread");
- return NULL;
- }
- if (self->count > 1) {
- --self->count;
- Py_RETURN_NONE;
- }
- assert(self->count == 1);
- }
-
- if (!ReleaseSemaphore(self->handle, 1, NULL)) {
- if (GetLastError() == ERROR_TOO_MANY_POSTS) {
- PyErr_SetString(PyExc_ValueError, "semaphore or lock "
- "released too many times");
- return NULL;
- } else {
- return PyErr_SetFromWindowsErr(0);
- }
- }
-
- --self->count;
- Py_RETURN_NONE;
+ if (self->kind == RECURSIVE_MUTEX) {
+ if (!ISMINE(self)) {
+ PyErr_SetString(PyExc_AssertionError, "attempt to "
+ "release recursive lock not owned "
+ "by thread");
+ return NULL;
+ }
+ if (self->count > 1) {
+ --self->count;
+ Py_RETURN_NONE;
+ }
+ assert(self->count == 1);
+ }
+
+ if (!ReleaseSemaphore(self->handle, 1, NULL)) {
+ if (GetLastError() == ERROR_TOO_MANY_POSTS) {
+ PyErr_SetString(PyExc_ValueError, "semaphore or lock "
+ "released too many times");
+ return NULL;
+ } else {
+ return PyErr_SetFromWindowsErr(0);
+ }
+ }
+
+ --self->count;
+ Py_RETURN_NONE;
}
#else /* !MS_WINDOWS */
@@ -207,59 +207,59 @@ semlock_release(SemLockObject *self, PyObject *args)
int
sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
{
- int res;
- unsigned long delay, difference;
- struct timeval now, tvdeadline, tvdelay;
-
- errno = 0;
- tvdeadline.tv_sec = deadline->tv_sec;
- tvdeadline.tv_usec = deadline->tv_nsec / 1000;
-
- for (delay = 0 ; ; delay += 1000) {
- /* poll */
- if (sem_trywait(sem) == 0)
- return 0;
- else if (errno != EAGAIN)
- return MP_STANDARD_ERROR;
-
- /* get current time */
- if (gettimeofday(&now, NULL) < 0)
- return MP_STANDARD_ERROR;
-
- /* check for timeout */
- if (tvdeadline.tv_sec < now.tv_sec ||
- (tvdeadline.tv_sec == now.tv_sec &&
- tvdeadline.tv_usec <= now.tv_usec)) {
- errno = ETIMEDOUT;
- return MP_STANDARD_ERROR;
- }
-
- /* calculate how much time is left */
- difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
- (tvdeadline.tv_usec - now.tv_usec);
-
- /* check delay not too long -- maximum is 20 msecs */
- if (delay > 20000)
- delay = 20000;
- if (delay > difference)
- delay = difference;
-
- /* sleep */
- tvdelay.tv_sec = delay / 1000000;
- tvdelay.tv_usec = delay % 1000000;
- if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
- return MP_STANDARD_ERROR;
-
- /* check for signals */
- Py_BLOCK_THREADS
- res = PyErr_CheckSignals();
- Py_UNBLOCK_THREADS
-
- if (res) {
- errno = EINTR;
- return MP_EXCEPTION_HAS_BEEN_SET;
- }
- }
+ int res;
+ unsigned long delay, difference;
+ struct timeval now, tvdeadline, tvdelay;
+
+ errno = 0;
+ tvdeadline.tv_sec = deadline->tv_sec;
+ tvdeadline.tv_usec = deadline->tv_nsec / 1000;
+
+ for (delay = 0 ; ; delay += 1000) {
+ /* poll */
+ if (sem_trywait(sem) == 0)
+ return 0;
+ else if (errno != EAGAIN)
+ return MP_STANDARD_ERROR;
+
+ /* get current time */
+ if (gettimeofday(&now, NULL) < 0)
+ return MP_STANDARD_ERROR;
+
+ /* check for timeout */
+ if (tvdeadline.tv_sec < now.tv_sec ||
+ (tvdeadline.tv_sec == now.tv_sec &&
+ tvdeadline.tv_usec <= now.tv_usec)) {
+ errno = ETIMEDOUT;
+ return MP_STANDARD_ERROR;
+ }
+
+ /* calculate how much time is left */
+ difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
+ (tvdeadline.tv_usec - now.tv_usec);
+
+ /* check delay not too long -- maximum is 20 msecs */
+ if (delay > 20000)
+ delay = 20000;
+ if (delay > difference)
+ delay = difference;
+
+ /* sleep */
+ tvdelay.tv_sec = delay / 1000000;
+ tvdelay.tv_usec = delay % 1000000;
+ if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
+ return MP_STANDARD_ERROR;
+
+ /* check for signals */
+ Py_BLOCK_THREADS
+ res = PyErr_CheckSignals();
+ Py_UNBLOCK_THREADS
+
+ if (res) {
+ errno = EINTR;
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ }
+ }
}
#endif /* !HAVE_SEM_TIMEDWAIT */
@@ -267,129 +267,129 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
static PyObject *
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
{
- int blocking = 1, res;
- double timeout;
- PyObject *timeout_obj = Py_None;
- struct timespec deadline = {0};
- struct timeval now;
- long sec, nsec;
-
- static char *kwlist[] = {"block", "timeout", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
- &blocking, &timeout_obj))
- return NULL;
-
- if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
- ++self->count;
- Py_RETURN_TRUE;
- }
-
- if (timeout_obj != Py_None) {
- timeout = PyFloat_AsDouble(timeout_obj);
- if (PyErr_Occurred())
- return NULL;
- if (timeout < 0.0)
- timeout = 0.0;
-
- if (gettimeofday(&now, NULL) < 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- sec = (long) timeout;
- nsec = (long) (1e9 * (timeout - sec) + 0.5);
- deadline.tv_sec = now.tv_sec + sec;
- deadline.tv_nsec = now.tv_usec * 1000 + nsec;
- deadline.tv_sec += (deadline.tv_nsec / 1000000000);
- deadline.tv_nsec %= 1000000000;
- }
-
- do {
- Py_BEGIN_ALLOW_THREADS
- if (blocking && timeout_obj == Py_None)
- res = sem_wait(self->handle);
- else if (!blocking)
- res = sem_trywait(self->handle);
- else
- res = sem_timedwait(self->handle, &deadline);
- Py_END_ALLOW_THREADS
- if (res == MP_EXCEPTION_HAS_BEEN_SET)
- break;
- } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
-
- if (res < 0) {
- if (errno == EAGAIN || errno == ETIMEDOUT)
- Py_RETURN_FALSE;
- else if (errno == EINTR)
- return NULL;
- else
- return PyErr_SetFromErrno(PyExc_OSError);
- }
-
- ++self->count;
- self->last_tid = PyThread_get_thread_ident();
-
- Py_RETURN_TRUE;
+ int blocking = 1, res;
+ double timeout;
+ PyObject *timeout_obj = Py_None;
+ struct timespec deadline = {0};
+ struct timeval now;
+ long sec, nsec;
+
+ static char *kwlist[] = {"block", "timeout", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
+ &blocking, &timeout_obj))
+ return NULL;
+
+ if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
+ ++self->count;
+ Py_RETURN_TRUE;
+ }
+
+ if (timeout_obj != Py_None) {
+ timeout = PyFloat_AsDouble(timeout_obj);
+ if (PyErr_Occurred())
+ return NULL;
+ if (timeout < 0.0)
+ timeout = 0.0;
+
+ if (gettimeofday(&now, NULL) < 0) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ sec = (long) timeout;
+ nsec = (long) (1e9 * (timeout - sec) + 0.5);
+ deadline.tv_sec = now.tv_sec + sec;
+ deadline.tv_nsec = now.tv_usec * 1000 + nsec;
+ deadline.tv_sec += (deadline.tv_nsec / 1000000000);
+ deadline.tv_nsec %= 1000000000;
+ }
+
+ do {
+ Py_BEGIN_ALLOW_THREADS
+ if (blocking && timeout_obj == Py_None)
+ res = sem_wait(self->handle);
+ else if (!blocking)
+ res = sem_trywait(self->handle);
+ else
+ res = sem_timedwait(self->handle, &deadline);
+ Py_END_ALLOW_THREADS
+ if (res == MP_EXCEPTION_HAS_BEEN_SET)
+ break;
+ } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
+
+ if (res < 0) {
+ if (errno == EAGAIN || errno == ETIMEDOUT)
+ Py_RETURN_FALSE;
+ else if (errno == EINTR)
+ return NULL;
+ else
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+
+ ++self->count;
+ self->last_tid = PyThread_get_thread_ident();
+
+ Py_RETURN_TRUE;
}
static PyObject *
semlock_release(SemLockObject *self, PyObject *args)
{
- if (self->kind == RECURSIVE_MUTEX) {
- if (!ISMINE(self)) {
- PyErr_SetString(PyExc_AssertionError, "attempt to "
- "release recursive lock not owned "
- "by thread");
- return NULL;
- }
- if (self->count > 1) {
- --self->count;
- Py_RETURN_NONE;
- }
- assert(self->count == 1);
- } else {
+ if (self->kind == RECURSIVE_MUTEX) {
+ if (!ISMINE(self)) {
+ PyErr_SetString(PyExc_AssertionError, "attempt to "
+ "release recursive lock not owned "
+ "by thread");
+ return NULL;
+ }
+ if (self->count > 1) {
+ --self->count;
+ Py_RETURN_NONE;
+ }
+ assert(self->count == 1);
+ } else {
#ifdef HAVE_BROKEN_SEM_GETVALUE
- /* We will only check properly the maxvalue == 1 case */
- if (self->maxvalue == 1) {
- /* make sure that already locked */
- if (sem_trywait(self->handle) < 0) {
- if (errno != EAGAIN) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- /* it is already locked as expected */
- } else {
- /* it was not locked so undo wait and raise */
- if (sem_post(self->handle) < 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "semaphore "
- "or lock released too many "
- "times");
- return NULL;
- }
- }
+ /* We will only check properly the maxvalue == 1 case */
+ if (self->maxvalue == 1) {
+ /* make sure that already locked */
+ if (sem_trywait(self->handle) < 0) {
+ if (errno != EAGAIN) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ /* it is already locked as expected */
+ } else {
+ /* it was not locked so undo wait and raise */
+ if (sem_post(self->handle) < 0) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "semaphore "
+ "or lock released too many "
+ "times");
+ return NULL;
+ }
+ }
#else
- int sval;
-
- /* This check is not an absolute guarantee that the semaphore
- does not rise above maxvalue. */
- if (sem_getvalue(self->handle, &sval) < 0) {
- return PyErr_SetFromErrno(PyExc_OSError);
- } else if (sval >= self->maxvalue) {
- PyErr_SetString(PyExc_ValueError, "semaphore or lock "
- "released too many times");
- return NULL;
- }
+ int sval;
+
+ /* This check is not an absolute guarantee that the semaphore
+ does not rise above maxvalue. */
+ if (sem_getvalue(self->handle, &sval) < 0) {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ } else if (sval >= self->maxvalue) {
+ PyErr_SetString(PyExc_ValueError, "semaphore or lock "
+ "released too many times");
+ return NULL;
+ }
#endif
- }
+ }
- if (sem_post(self->handle) < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
+ if (sem_post(self->handle) < 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
- --self->count;
- Py_RETURN_NONE;
+ --self->count;
+ Py_RETURN_NONE;
}
#endif /* !MS_WINDOWS */
@@ -401,111 +401,111 @@ semlock_release(SemLockObject *self, PyObject *args)
static PyObject *
newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue)
{
- SemLockObject *self;
-
- self = PyObject_New(SemLockObject, type);
- if (!self)
- return NULL;
- self->handle = handle;
- self->kind = kind;
- self->count = 0;
- self->last_tid = 0;
- self->maxvalue = maxvalue;
- return (PyObject*)self;
+ SemLockObject *self;
+
+ self = PyObject_New(SemLockObject, type);
+ if (!self)
+ return NULL;
+ self->handle = handle;
+ self->kind = kind;
+ self->count = 0;
+ self->last_tid = 0;
+ self->maxvalue = maxvalue;
+ return (PyObject*)self;
}
static PyObject *
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- char buffer[256];
- SEM_HANDLE handle = SEM_FAILED;
- int kind, maxvalue, value;
- PyObject *result;
- static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
- static int counter = 0;
+ char buffer[256];
+ SEM_HANDLE handle = SEM_FAILED;
+ int kind, maxvalue, value;
+ PyObject *result;
+ static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
+ static int counter = 0;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist,
- &kind, &value, &maxvalue))
- return NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist,
+ &kind, &value, &maxvalue))
+ return NULL;
- if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
- PyErr_SetString(PyExc_ValueError, "unrecognized kind");
- return NULL;
- }
+ if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
+ PyErr_SetString(PyExc_ValueError, "unrecognized kind");
+ return NULL;
+ }
- PyOS_snprintf(buffer, sizeof(buffer), "/mp%d-%d", getpid(), counter++);
+ PyOS_snprintf(buffer, sizeof(buffer), "/mp%d-%d", getpid(), counter++);
- SEM_CLEAR_ERROR();
- handle = SEM_CREATE(buffer, value, maxvalue);
- /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
- if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
- goto failure;
+ SEM_CLEAR_ERROR();
+ handle = SEM_CREATE(buffer, value, maxvalue);
+ /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
+ if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
+ goto failure;
- if (SEM_UNLINK(buffer) < 0)
- goto failure;
+ if (SEM_UNLINK(buffer) < 0)
+ goto failure;
- result = newsemlockobject(type, handle, kind, maxvalue);
- if (!result)
- goto failure;
+ result = newsemlockobject(type, handle, kind, maxvalue);
+ if (!result)
+ goto failure;
- return result;
+ return result;
failure:
- if (handle != SEM_FAILED)
- SEM_CLOSE(handle);
- mp_SetError(NULL, MP_STANDARD_ERROR);
- return NULL;
+ if (handle != SEM_FAILED)
+ SEM_CLOSE(handle);
+ mp_SetError(NULL, MP_STANDARD_ERROR);
+ return NULL;
}
static PyObject *
semlock_rebuild(PyTypeObject *type, PyObject *args)
{
- SEM_HANDLE handle;
- int kind, maxvalue;
+ SEM_HANDLE handle;
+ int kind, maxvalue;
- if (!PyArg_ParseTuple(args, F_SEM_HANDLE "ii",
- &handle, &kind, &maxvalue))
- return NULL;
+ if (!PyArg_ParseTuple(args, F_SEM_HANDLE "ii",
+ &handle, &kind, &maxvalue))
+ return NULL;
- return newsemlockobject(type, handle, kind, maxvalue);
+ return newsemlockobject(type, handle, kind, maxvalue);
}
static void
semlock_dealloc(SemLockObject* self)
{
- if (self->handle != SEM_FAILED)
- SEM_CLOSE(self->handle);
- PyObject_Del(self);
+ if (self->handle != SEM_FAILED)
+ SEM_CLOSE(self->handle);
+ PyObject_Del(self);
}
static PyObject *
semlock_count(SemLockObject *self)
{
- return PyInt_FromLong((long)self->count);
+ return PyInt_FromLong((long)self->count);
}
static PyObject *
semlock_ismine(SemLockObject *self)
{
- /* only makes sense for a lock */
- return PyBool_FromLong(ISMINE(self));
+ /* only makes sense for a lock */
+ return PyBool_FromLong(ISMINE(self));
}
static PyObject *
semlock_getvalue(SemLockObject *self)
{
#ifdef HAVE_BROKEN_SEM_GETVALUE
- PyErr_SetNone(PyExc_NotImplementedError);
- return NULL;
+ PyErr_SetNone(PyExc_NotImplementedError);
+ return NULL;
#else
- int sval;
- if (SEM_GETVALUE(self->handle, &sval) < 0)
- return mp_SetError(NULL, MP_STANDARD_ERROR);
- /* some posix implementations use negative numbers to indicate
- the number of waiting threads */
- if (sval < 0)
- sval = 0;
- return PyInt_FromLong((long)sval);
+ int sval;
+ if (SEM_GETVALUE(self->handle, &sval) < 0)
+ return mp_SetError(NULL, MP_STANDARD_ERROR);
+ /* some posix implementations use negative numbers to indicate
+ the number of waiting threads */
+ if (sval < 0)
+ sval = 0;
+ return PyInt_FromLong((long)sval);
#endif
}
@@ -513,28 +513,28 @@ static PyObject *
semlock_iszero(SemLockObject *self)
{
#ifdef HAVE_BROKEN_SEM_GETVALUE
- if (sem_trywait(self->handle) < 0) {
- if (errno == EAGAIN)
- Py_RETURN_TRUE;
- return mp_SetError(NULL, MP_STANDARD_ERROR);
- } else {
- if (sem_post(self->handle) < 0)
- return mp_SetError(NULL, MP_STANDARD_ERROR);
- Py_RETURN_FALSE;
- }
+ if (sem_trywait(self->handle) < 0) {
+ if (errno == EAGAIN)
+ Py_RETURN_TRUE;
+ return mp_SetError(NULL, MP_STANDARD_ERROR);
+ } else {
+ if (sem_post(self->handle) < 0)
+ return mp_SetError(NULL, MP_STANDARD_ERROR);
+ Py_RETURN_FALSE;
+ }
#else
- int sval;
- if (SEM_GETVALUE(self->handle, &sval) < 0)
- return mp_SetError(NULL, MP_STANDARD_ERROR);
- return PyBool_FromLong((long)sval == 0);
+ int sval;
+ if (SEM_GETVALUE(self->handle, &sval) < 0)
+ return mp_SetError(NULL, MP_STANDARD_ERROR);
+ return PyBool_FromLong((long)sval == 0);
#endif
}
static PyObject *
semlock_afterfork(SemLockObject *self)
{
- self->count = 0;
- Py_RETURN_NONE;
+ self->count = 0;
+ Py_RETURN_NONE;
}
/*
@@ -542,27 +542,27 @@ semlock_afterfork(SemLockObject *self)
*/
static PyMethodDef semlock_methods[] = {
- {"acquire", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS,
- "acquire the semaphore/lock"},
- {"release", (PyCFunction)semlock_release, METH_NOARGS,
- "release the semaphore/lock"},
+ {"acquire", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS,
+ "acquire the semaphore/lock"},
+ {"release", (PyCFunction)semlock_release, METH_NOARGS,
+ "release the semaphore/lock"},
{"__enter__", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS,
- "enter the semaphore/lock"},
- {"__exit__", (PyCFunction)semlock_release, METH_VARARGS,
- "exit the semaphore/lock"},
- {"_count", (PyCFunction)semlock_count, METH_NOARGS,
- "num of `acquire()`s minus num of `release()`s for this process"},
- {"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS,
- "whether the lock is owned by this thread"},
- {"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS,
- "get the value of the semaphore"},
- {"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS,
- "returns whether semaphore has value zero"},
- {"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS,
- ""},
- {"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS,
- "rezero the net acquisition count after fork()"},
- {NULL}
+ "enter the semaphore/lock"},
+ {"__exit__", (PyCFunction)semlock_release, METH_VARARGS,
+ "exit the semaphore/lock"},
+ {"_count", (PyCFunction)semlock_count, METH_NOARGS,
+ "num of `acquire()`s minus num of `release()`s for this process"},
+ {"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS,
+ "whether the lock is owned by this thread"},
+ {"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS,
+ "get the value of the semaphore"},
+ {"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS,
+ "returns whether semaphore has value zero"},
+ {"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS,
+ ""},
+ {"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS,
+ "rezero the net acquisition count after fork()"},
+ {NULL}
};
/*
@@ -570,13 +570,13 @@ static PyMethodDef semlock_methods[] = {
*/
static PyMemberDef semlock_members[] = {
- {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
- ""},
- {"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
- ""},
- {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
- ""},
- {NULL}
+ {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
+ ""},
+ {"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
+ ""},
+ {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
+ ""},
+ {NULL}
};
/*
@@ -584,42 +584,42 @@ static PyMemberDef semlock_members[] = {
*/
PyTypeObject SemLockType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- /* tp_name */ "_multiprocessing.SemLock",
- /* tp_basicsize */ sizeof(SemLockObject),
- /* tp_itemsize */ 0,
- /* tp_dealloc */ (destructor)semlock_dealloc,
- /* tp_print */ 0,
- /* tp_getattr */ 0,
- /* tp_setattr */ 0,
- /* tp_reserved */ 0,
- /* tp_repr */ 0,
- /* tp_as_number */ 0,
- /* tp_as_sequence */ 0,
- /* tp_as_mapping */ 0,
- /* tp_hash */ 0,
- /* tp_call */ 0,
- /* tp_str */ 0,
- /* tp_getattro */ 0,
- /* tp_setattro */ 0,
- /* tp_as_buffer */ 0,
- /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- /* tp_doc */ "Semaphore/Mutex type",
- /* tp_traverse */ 0,
- /* tp_clear */ 0,
- /* tp_richcompare */ 0,
- /* tp_weaklistoffset */ 0,
- /* tp_iter */ 0,
- /* tp_iternext */ 0,
- /* tp_methods */ semlock_methods,
- /* tp_members */ semlock_members,
- /* tp_getset */ 0,
- /* tp_base */ 0,
- /* tp_dict */ 0,
- /* tp_descr_get */ 0,
- /* tp_descr_set */ 0,
- /* tp_dictoffset */ 0,
- /* tp_init */ 0,
- /* tp_alloc */ 0,
- /* tp_new */ semlock_new,
+ PyVarObject_HEAD_INIT(NULL, 0)
+ /* tp_name */ "_multiprocessing.SemLock",
+ /* tp_basicsize */ sizeof(SemLockObject),
+ /* tp_itemsize */ 0,
+ /* tp_dealloc */ (destructor)semlock_dealloc,
+ /* tp_print */ 0,
+ /* tp_getattr */ 0,
+ /* tp_setattr */ 0,
+ /* tp_reserved */ 0,
+ /* tp_repr */ 0,
+ /* tp_as_number */ 0,
+ /* tp_as_sequence */ 0,
+ /* tp_as_mapping */ 0,
+ /* tp_hash */ 0,
+ /* tp_call */ 0,
+ /* tp_str */ 0,
+ /* tp_getattro */ 0,
+ /* tp_setattro */ 0,
+ /* tp_as_buffer */ 0,
+ /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ /* tp_doc */ "Semaphore/Mutex type",
+ /* tp_traverse */ 0,
+ /* tp_clear */ 0,
+ /* tp_richcompare */ 0,
+ /* tp_weaklistoffset */ 0,
+ /* tp_iter */ 0,
+ /* tp_iternext */ 0,
+ /* tp_methods */ semlock_methods,
+ /* tp_members */ semlock_members,
+ /* tp_getset */ 0,
+ /* tp_base */ 0,
+ /* tp_dict */ 0,
+ /* tp_descr_get */ 0,
+ /* tp_descr_set */ 0,
+ /* tp_dictoffset */ 0,
+ /* tp_init */ 0,
+ /* tp_alloc */ 0,
+ /* tp_new */ semlock_new,
};
diff --git a/Modules/_multiprocessing/socket_connection.c b/Modules/_multiprocessing/socket_connection.c
index ad4005b..7ebf338 100644
--- a/Modules/_multiprocessing/socket_connection.c
+++ b/Modules/_multiprocessing/socket_connection.c
@@ -25,45 +25,45 @@
static Py_ssize_t
_conn_sendall(HANDLE h, char *string, size_t length)
{
- char *p = string;
- Py_ssize_t res;
-
- while (length > 0) {
- res = WRITE(h, p, length);
- if (res < 0)
- return MP_SOCKET_ERROR;
- length -= res;
- p += res;
- }
-
- return MP_SUCCESS;
+ char *p = string;
+ Py_ssize_t res;
+
+ while (length > 0) {
+ res = WRITE(h, p, length);
+ if (res < 0)
+ return MP_SOCKET_ERROR;
+ length -= res;
+ p += res;
+ }
+
+ return MP_SUCCESS;
}
/*
- * Receive string of exact length from file descriptor
+ * Receive string of exact length from file descriptor
*/
static Py_ssize_t
_conn_recvall(HANDLE h, char *buffer, size_t length)
{
- size_t remaining = length;
- Py_ssize_t temp;
- char *p = buffer;
-
- while (remaining > 0) {
- temp = READ(h, p, remaining);
- if (temp <= 0) {
- if (temp == 0)
- return remaining == length ?
- MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
- else
- return temp;
- }
- remaining -= temp;
- p += temp;
- }
-
- return MP_SUCCESS;
+ size_t remaining = length;
+ Py_ssize_t temp;
+ char *p = buffer;
+
+ while (remaining > 0) {
+ temp = READ(h, p, remaining);
+ if (temp <= 0) {
+ if (temp == 0)
+ return remaining == length ?
+ MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
+ else
+ return temp;
+ }
+ remaining -= temp;
+ p += temp;
+ }
+
+ return MP_SUCCESS;
}
/*
@@ -73,38 +73,38 @@ _conn_recvall(HANDLE h, char *buffer, size_t length)
static Py_ssize_t
conn_send_string(ConnectionObject *conn, char *string, size_t length)
{
- Py_ssize_t res;
- /* The "header" of the message is a 32 bit unsigned number (in
- network order) which specifies the length of the "body". If
- the message is shorter than about 16kb then it is quicker to
- combine the "header" and the "body" of the message and send
- them at once. */
- if (length < (16*1024)) {
- char *message;
-
- message = PyMem_Malloc(length+4);
- if (message == NULL)
- return MP_MEMORY_ERROR;
-
- *(UINT32*)message = htonl((UINT32)length);
- memcpy(message+4, string, length);
- Py_BEGIN_ALLOW_THREADS
- res = _conn_sendall(conn->handle, message, length+4);
- Py_END_ALLOW_THREADS
- PyMem_Free(message);
- } else {
- UINT32 lenbuff;
-
- if (length > MAX_MESSAGE_LENGTH)
- return MP_BAD_MESSAGE_LENGTH;
-
- lenbuff = htonl((UINT32)length);
- Py_BEGIN_ALLOW_THREADS
- res = _conn_sendall(conn->handle, (char*)&lenbuff, 4) ||
- _conn_sendall(conn->handle, string, length);
- Py_END_ALLOW_THREADS
- }
- return res;
+ Py_ssize_t res;
+ /* The "header" of the message is a 32 bit unsigned number (in
+ network order) which specifies the length of the "body". If
+ the message is shorter than about 16kb then it is quicker to
+ combine the "header" and the "body" of the message and send
+ them at once. */
+ if (length < (16*1024)) {
+ char *message;
+
+ message = PyMem_Malloc(length+4);
+ if (message == NULL)
+ return MP_MEMORY_ERROR;
+
+ *(UINT32*)message = htonl((UINT32)length);
+ memcpy(message+4, string, length);
+ Py_BEGIN_ALLOW_THREADS
+ res = _conn_sendall(conn->handle, message, length+4);
+ Py_END_ALLOW_THREADS
+ PyMem_Free(message);
+ } else {
+ UINT32 lenbuff;
+
+ if (length > MAX_MESSAGE_LENGTH)
+ return MP_BAD_MESSAGE_LENGTH;
+
+ lenbuff = htonl((UINT32)length);
+ Py_BEGIN_ALLOW_THREADS
+ res = _conn_sendall(conn->handle, (char*)&lenbuff, 4) ||
+ _conn_sendall(conn->handle, string, length);
+ Py_END_ALLOW_THREADS
+ }
+ return res;
}
/*
@@ -114,38 +114,38 @@ conn_send_string(ConnectionObject *conn, char *string, size_t length)
*/
static Py_ssize_t
-conn_recv_string(ConnectionObject *conn, char *buffer,
- size_t buflength, char **newbuffer, size_t maxlength)
+conn_recv_string(ConnectionObject *conn, char *buffer,
+ size_t buflength, char **newbuffer, size_t maxlength)
{
- int res;
- UINT32 ulength;
-
- *newbuffer = NULL;
-
- Py_BEGIN_ALLOW_THREADS
- res = _conn_recvall(conn->handle, (char*)&ulength, 4);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return res;
-
- ulength = ntohl(ulength);
- if (ulength > maxlength)
- return MP_BAD_MESSAGE_LENGTH;
-
- if (ulength <= buflength) {
- Py_BEGIN_ALLOW_THREADS
- res = _conn_recvall(conn->handle, buffer, (size_t)ulength);
- Py_END_ALLOW_THREADS
- return res < 0 ? res : ulength;
- } else {
- *newbuffer = PyMem_Malloc((size_t)ulength);
- if (*newbuffer == NULL)
- return MP_MEMORY_ERROR;
- Py_BEGIN_ALLOW_THREADS
- res = _conn_recvall(conn->handle, *newbuffer, (size_t)ulength);
- Py_END_ALLOW_THREADS
- return res < 0 ? (Py_ssize_t)res : (Py_ssize_t)ulength;
- }
+ int res;
+ UINT32 ulength;
+
+ *newbuffer = NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = _conn_recvall(conn->handle, (char*)&ulength, 4);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return res;
+
+ ulength = ntohl(ulength);
+ if (ulength > maxlength)
+ return MP_BAD_MESSAGE_LENGTH;
+
+ if (ulength <= buflength) {
+ Py_BEGIN_ALLOW_THREADS
+ res = _conn_recvall(conn->handle, buffer, (size_t)ulength);
+ Py_END_ALLOW_THREADS
+ return res < 0 ? res : ulength;
+ } else {
+ *newbuffer = PyMem_Malloc((size_t)ulength);
+ if (*newbuffer == NULL)
+ return MP_MEMORY_ERROR;
+ Py_BEGIN_ALLOW_THREADS
+ res = _conn_recvall(conn->handle, *newbuffer, (size_t)ulength);
+ Py_END_ALLOW_THREADS
+ return res < 0 ? (Py_ssize_t)res : (Py_ssize_t)ulength;
+ }
}
/*
@@ -155,41 +155,41 @@ conn_recv_string(ConnectionObject *conn, char *buffer,
static int
conn_poll(ConnectionObject *conn, double timeout, PyThreadState *_save)
{
- int res;
- fd_set rfds;
-
- /*
- * Verify the handle, issue 3321. Not required for windows.
- */
- #ifndef MS_WINDOWS
- if (((int)conn->handle) < 0 || ((int)conn->handle) >= FD_SETSIZE) {
- Py_BLOCK_THREADS
- PyErr_SetString(PyExc_IOError, "handle out of range in select()");
- Py_UNBLOCK_THREADS
- return MP_EXCEPTION_HAS_BEEN_SET;
- }
- #endif
-
- FD_ZERO(&rfds);
- FD_SET((SOCKET)conn->handle, &rfds);
-
- if (timeout < 0.0) {
- res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
- } else {
- struct timeval tv;
- tv.tv_sec = (long)timeout;
- tv.tv_usec = (long)((timeout - tv.tv_sec) * 1e6 + 0.5);
- res = select((int)conn->handle+1, &rfds, NULL, NULL, &tv);
- }
-
- if (res < 0) {
- return MP_SOCKET_ERROR;
- } else if (FD_ISSET(conn->handle, &rfds)) {
- return TRUE;
- } else {
- assert(res == 0);
- return FALSE;
- }
+ int res;
+ fd_set rfds;
+
+ /*
+ * Verify the handle, issue 3321. Not required for windows.
+ */
+ #ifndef MS_WINDOWS
+ if (((int)conn->handle) < 0 || ((int)conn->handle) >= FD_SETSIZE) {
+ Py_BLOCK_THREADS
+ PyErr_SetString(PyExc_IOError, "handle out of range in select()");
+ Py_UNBLOCK_THREADS
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ }
+ #endif
+
+ FD_ZERO(&rfds);
+ FD_SET((SOCKET)conn->handle, &rfds);
+
+ if (timeout < 0.0) {
+ res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
+ } else {
+ struct timeval tv;
+ tv.tv_sec = (long)timeout;
+ tv.tv_usec = (long)((timeout - tv.tv_sec) * 1e6 + 0.5);
+ res = select((int)conn->handle+1, &rfds, NULL, NULL, &tv);
+ }
+
+ if (res < 0) {
+ return MP_SOCKET_ERROR;
+ } else if (FD_ISSET(conn->handle, &rfds)) {
+ return TRUE;
+ } else {
+ assert(res == 0);
+ return FALSE;
+ }
}
/*
diff --git a/Modules/_multiprocessing/win32_functions.c b/Modules/_multiprocessing/win32_functions.c
index ba82c23..452d608 100644
--- a/Modules/_multiprocessing/win32_functions.c
+++ b/Modules/_multiprocessing/win32_functions.c
@@ -19,248 +19,248 @@
static PyObject *
win32_CloseHandle(PyObject *self, PyObject *args)
{
- HANDLE hObject;
- BOOL success;
+ HANDLE hObject;
+ BOOL success;
- if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))
- return NULL;
+ if (!PyArg_ParseTuple(args, F_HANDLE, &hObject))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- success = CloseHandle(hObject);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ success = CloseHandle(hObject);
+ Py_END_ALLOW_THREADS
- if (!success)
- return PyErr_SetFromWindowsErr(0);
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyObject *
win32_ConnectNamedPipe(PyObject *self, PyObject *args)
{
- HANDLE hNamedPipe;
- LPOVERLAPPED lpOverlapped;
- BOOL success;
+ HANDLE hNamedPipe;
+ LPOVERLAPPED lpOverlapped;
+ BOOL success;
- if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER,
- &hNamedPipe, &lpOverlapped))
- return NULL;
+ if (!PyArg_ParseTuple(args, F_HANDLE F_POINTER,
+ &hNamedPipe, &lpOverlapped))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- success = ConnectNamedPipe(hNamedPipe, lpOverlapped);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ success = ConnectNamedPipe(hNamedPipe, lpOverlapped);
+ Py_END_ALLOW_THREADS
- if (!success)
- return PyErr_SetFromWindowsErr(0);
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyObject *
win32_CreateFile(PyObject *self, PyObject *args)
{
- LPCTSTR lpFileName;
- DWORD dwDesiredAccess;
- DWORD dwShareMode;
- LPSECURITY_ATTRIBUTES lpSecurityAttributes;
- DWORD dwCreationDisposition;
- DWORD dwFlagsAndAttributes;
- HANDLE hTemplateFile;
- HANDLE handle;
-
- if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER
- F_DWORD F_DWORD F_HANDLE,
- &lpFileName, &dwDesiredAccess, &dwShareMode,
- &lpSecurityAttributes, &dwCreationDisposition,
- &dwFlagsAndAttributes, &hTemplateFile))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- handle = CreateFile(lpFileName, dwDesiredAccess,
- dwShareMode, lpSecurityAttributes,
- dwCreationDisposition,
- dwFlagsAndAttributes, hTemplateFile);
- Py_END_ALLOW_THREADS
-
- if (handle == INVALID_HANDLE_VALUE)
- return PyErr_SetFromWindowsErr(0);
-
- return Py_BuildValue(F_HANDLE, handle);
+ LPCTSTR lpFileName;
+ DWORD dwDesiredAccess;
+ DWORD dwShareMode;
+ LPSECURITY_ATTRIBUTES lpSecurityAttributes;
+ DWORD dwCreationDisposition;
+ DWORD dwFlagsAndAttributes;
+ HANDLE hTemplateFile;
+ HANDLE handle;
+
+ if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER
+ F_DWORD F_DWORD F_HANDLE,
+ &lpFileName, &dwDesiredAccess, &dwShareMode,
+ &lpSecurityAttributes, &dwCreationDisposition,
+ &dwFlagsAndAttributes, &hTemplateFile))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ handle = CreateFile(lpFileName, dwDesiredAccess,
+ dwShareMode, lpSecurityAttributes,
+ dwCreationDisposition,
+ dwFlagsAndAttributes, hTemplateFile);
+ Py_END_ALLOW_THREADS
+
+ if (handle == INVALID_HANDLE_VALUE)
+ return PyErr_SetFromWindowsErr(0);
+
+ return Py_BuildValue(F_HANDLE, handle);
}
static PyObject *
win32_CreateNamedPipe(PyObject *self, PyObject *args)
{
- LPCTSTR lpName;
- DWORD dwOpenMode;
- DWORD dwPipeMode;
- DWORD nMaxInstances;
- DWORD nOutBufferSize;
- DWORD nInBufferSize;
- DWORD nDefaultTimeOut;
- LPSECURITY_ATTRIBUTES lpSecurityAttributes;
- HANDLE handle;
-
- if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD
- F_DWORD F_DWORD F_DWORD F_POINTER,
- &lpName, &dwOpenMode, &dwPipeMode,
- &nMaxInstances, &nOutBufferSize,
- &nInBufferSize, &nDefaultTimeOut,
- &lpSecurityAttributes))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode,
- nMaxInstances, nOutBufferSize,
- nInBufferSize, nDefaultTimeOut,
- lpSecurityAttributes);
- Py_END_ALLOW_THREADS
-
- if (handle == INVALID_HANDLE_VALUE)
- return PyErr_SetFromWindowsErr(0);
-
- return Py_BuildValue(F_HANDLE, handle);
+ LPCTSTR lpName;
+ DWORD dwOpenMode;
+ DWORD dwPipeMode;
+ DWORD nMaxInstances;
+ DWORD nOutBufferSize;
+ DWORD nInBufferSize;
+ DWORD nDefaultTimeOut;
+ LPSECURITY_ATTRIBUTES lpSecurityAttributes;
+ HANDLE handle;
+
+ if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD
+ F_DWORD F_DWORD F_DWORD F_POINTER,
+ &lpName, &dwOpenMode, &dwPipeMode,
+ &nMaxInstances, &nOutBufferSize,
+ &nInBufferSize, &nDefaultTimeOut,
+ &lpSecurityAttributes))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode,
+ nMaxInstances, nOutBufferSize,
+ nInBufferSize, nDefaultTimeOut,
+ lpSecurityAttributes);
+ Py_END_ALLOW_THREADS
+
+ if (handle == INVALID_HANDLE_VALUE)
+ return PyErr_SetFromWindowsErr(0);
+
+ return Py_BuildValue(F_HANDLE, handle);
}
static PyObject *
win32_ExitProcess(PyObject *self, PyObject *args)
{
- UINT uExitCode;
+ UINT uExitCode;
- if (!PyArg_ParseTuple(args, "I", &uExitCode))
- return NULL;
+ if (!PyArg_ParseTuple(args, "I", &uExitCode))
+ return NULL;
- #if defined(Py_DEBUG)
- SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
- _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
- #endif
+ #if defined(Py_DEBUG)
+ SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
+ _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
+ #endif
- ExitProcess(uExitCode);
+ ExitProcess(uExitCode);
- return NULL;
+ return NULL;
}
static PyObject *
win32_GetLastError(PyObject *self, PyObject *args)
{
- return Py_BuildValue(F_DWORD, GetLastError());
+ return Py_BuildValue(F_DWORD, GetLastError());
}
static PyObject *
win32_OpenProcess(PyObject *self, PyObject *args)
{
- DWORD dwDesiredAccess;
- BOOL bInheritHandle;
- DWORD dwProcessId;
- HANDLE handle;
+ DWORD dwDesiredAccess;
+ BOOL bInheritHandle;
+ DWORD dwProcessId;
+ HANDLE handle;
- if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD,
- &dwDesiredAccess, &bInheritHandle, &dwProcessId))
- return NULL;
+ if (!PyArg_ParseTuple(args, F_DWORD "i" F_DWORD,
+ &dwDesiredAccess, &bInheritHandle, &dwProcessId))
+ return NULL;
- handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
- if (handle == NULL)
- return PyErr_SetFromWindowsErr(0);
+ handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
+ if (handle == NULL)
+ return PyErr_SetFromWindowsErr(0);
- return Py_BuildValue(F_HANDLE, handle);
+ return Py_BuildValue(F_HANDLE, handle);
}
static PyObject *
win32_SetNamedPipeHandleState(PyObject *self, PyObject *args)
{
- HANDLE hNamedPipe;
- PyObject *oArgs[3];
- DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
- int i;
-
- if (!PyArg_ParseTuple(args, F_HANDLE "OOO",
- &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
- return NULL;
-
- PyErr_Clear();
-
- for (i = 0 ; i < 3 ; i++) {
- if (oArgs[i] != Py_None) {
- dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
- if (PyErr_Occurred())
- return NULL;
- pArgs[i] = &dwArgs[i];
- }
- }
-
- if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
- return PyErr_SetFromWindowsErr(0);
-
- Py_RETURN_NONE;
+ HANDLE hNamedPipe;
+ PyObject *oArgs[3];
+ DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
+ int i;
+
+ if (!PyArg_ParseTuple(args, F_HANDLE "OOO",
+ &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
+ return NULL;
+
+ PyErr_Clear();
+
+ for (i = 0 ; i < 3 ; i++) {
+ if (oArgs[i] != Py_None) {
+ dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
+ if (PyErr_Occurred())
+ return NULL;
+ pArgs[i] = &dwArgs[i];
+ }
+ }
+
+ if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
+ return PyErr_SetFromWindowsErr(0);
+
+ Py_RETURN_NONE;
}
static PyObject *
win32_WaitNamedPipe(PyObject *self, PyObject *args)
{
- LPCTSTR lpNamedPipeName;
- DWORD nTimeOut;
- BOOL success;
+ LPCTSTR lpNamedPipeName;
+ DWORD nTimeOut;
+ BOOL success;
- if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
+ Py_END_ALLOW_THREADS
- if (!success)
- return PyErr_SetFromWindowsErr(0);
+ if (!success)
+ return PyErr_SetFromWindowsErr(0);
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyMethodDef win32_methods[] = {
- WIN32_FUNCTION(CloseHandle),
- WIN32_FUNCTION(GetLastError),
- WIN32_FUNCTION(OpenProcess),
- WIN32_FUNCTION(ExitProcess),
- WIN32_FUNCTION(ConnectNamedPipe),
- WIN32_FUNCTION(CreateFile),
- WIN32_FUNCTION(CreateNamedPipe),
- WIN32_FUNCTION(SetNamedPipeHandleState),
- WIN32_FUNCTION(WaitNamedPipe),
- {NULL}
+ WIN32_FUNCTION(CloseHandle),
+ WIN32_FUNCTION(GetLastError),
+ WIN32_FUNCTION(OpenProcess),
+ WIN32_FUNCTION(ExitProcess),
+ WIN32_FUNCTION(ConnectNamedPipe),
+ WIN32_FUNCTION(CreateFile),
+ WIN32_FUNCTION(CreateNamedPipe),
+ WIN32_FUNCTION(SetNamedPipeHandleState),
+ WIN32_FUNCTION(WaitNamedPipe),
+ {NULL}
};
PyTypeObject Win32Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
+ PyVarObject_HEAD_INIT(NULL, 0)
};
PyObject *
create_win32_namespace(void)
{
- Win32Type.tp_name = "_multiprocessing.win32";
- Win32Type.tp_methods = win32_methods;
- if (PyType_Ready(&Win32Type) < 0)
- return NULL;
- Py_INCREF(&Win32Type);
-
- WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
- WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
- WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
- WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
- WIN32_CONSTANT(F_DWORD, GENERIC_READ);
- WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
- WIN32_CONSTANT(F_DWORD, INFINITE);
- WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
- WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);
- WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
- WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
- WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
- WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
- WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
- WIN32_CONSTANT(F_DWORD, PIPE_WAIT);
- WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
-
- WIN32_CONSTANT("i", NULL);
-
- return (PyObject*)&Win32Type;
+ Win32Type.tp_name = "_multiprocessing.win32";
+ Win32Type.tp_methods = win32_methods;
+ if (PyType_Ready(&Win32Type) < 0)
+ return NULL;
+ Py_INCREF(&Win32Type);
+
+ WIN32_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
+ WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
+ WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
+ WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
+ WIN32_CONSTANT(F_DWORD, GENERIC_READ);
+ WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
+ WIN32_CONSTANT(F_DWORD, INFINITE);
+ WIN32_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
+ WIN32_CONSTANT(F_DWORD, OPEN_EXISTING);
+ WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
+ WIN32_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
+ WIN32_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
+ WIN32_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
+ WIN32_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
+ WIN32_CONSTANT(F_DWORD, PIPE_WAIT);
+ WIN32_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
+
+ WIN32_CONSTANT("i", NULL);
+
+ return (PyObject*)&Win32Type;
}
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 05c09d8..57f5395 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -2,23 +2,23 @@
/* ------------------------------------------------------------------
The code in this module was based on a download from:
- http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
+ http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
It was modified in 2002 by Raymond Hettinger as follows:
- * the principal computational lines untouched except for tabbing.
+ * the principal computational lines untouched except for tabbing.
- * renamed genrand_res53() to random_random() and wrapped
- in python calling/return code.
+ * renamed genrand_res53() to random_random() and wrapped
+ in python calling/return code.
- * genrand_int32() and the helper functions, init_genrand()
- and init_by_array(), were declared static, wrapped in
- Python calling/return code. also, their global data
- references were replaced with structure references.
+ * genrand_int32() and the helper functions, init_genrand()
+ and init_by_array(), were declared static, wrapped in
+ Python calling/return code. also, their global data
+ references were replaced with structure references.
- * unused functions from the original were deleted.
- new, original C python code was added to implement the
- Random() interface.
+ * unused functions from the original were deleted.
+ new, original C python code was added to implement the
+ Random() interface.
The following are the verbatim comments from the original code:
@@ -36,15 +36,15 @@
are met:
1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
+ notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
- products derived from this software without specific prior written
- permission.
+ products derived from this software without specific prior written
+ permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -67,24 +67,24 @@
/* ---------------------------------------------------------------*/
#include "Python.h"
-#include <time.h> /* for seeding to current time */
+#include <time.h> /* for seeding to current time */
/* Period parameters -- These are all magic. Don't change. */
#define N 624
#define M 397
-#define MATRIX_A 0x9908b0dfUL /* constant vector a */
+#define MATRIX_A 0x9908b0dfUL /* constant vector a */
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
typedef struct {
- PyObject_HEAD
- unsigned long state[N];
- int index;
+ PyObject_HEAD
+ unsigned long state[N];
+ int index;
} RandomObject;
static PyTypeObject Random_Type;
-#define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
+#define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
/* Random methods */
@@ -94,28 +94,28 @@ static PyTypeObject Random_Type;
static unsigned long
genrand_int32(RandomObject *self)
{
- unsigned long y;
- static unsigned long mag01[2]={0x0UL, MATRIX_A};
- /* mag01[x] = x * MATRIX_A for x=0,1 */
- unsigned long *mt;
-
- mt = self->state;
- if (self->index >= N) { /* generate N words at one time */
- int kk;
-
- for (kk=0;kk<N-M;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
- }
- for (;kk<N-1;kk++) {
- y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
- mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
- }
- y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
- mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
-
- self->index = 0;
- }
+ unsigned long y;
+ static unsigned long mag01[2]={0x0UL, MATRIX_A};
+ /* mag01[x] = x * MATRIX_A for x=0,1 */
+ unsigned long *mt;
+
+ mt = self->state;
+ if (self->index >= N) { /* generate N words at one time */
+ int kk;
+
+ for (kk=0;kk<N-M;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
+ }
+ for (;kk<N-1;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
+ }
+ y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
+ mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
+
+ self->index = 0;
+ }
y = mt[self->index++];
y ^= (y >> 11);
@@ -137,31 +137,31 @@ genrand_int32(RandomObject *self)
static PyObject *
random_random(RandomObject *self)
{
- unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
- return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
+ unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
+ return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
}
/* initializes mt[N] with a seed */
static void
init_genrand(RandomObject *self, unsigned long s)
{
- int mti;
- unsigned long *mt;
-
- mt = self->state;
- mt[0]= s & 0xffffffffUL;
- for (mti=1; mti<N; mti++) {
- mt[mti] =
- (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
- /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
- /* In the previous versions, MSBs of the seed affect */
- /* only MSBs of the array mt[]. */
- /* 2002/01/09 modified by Makoto Matsumoto */
- mt[mti] &= 0xffffffffUL;
- /* for >32 bit machines */
- }
- self->index = mti;
- return;
+ int mti;
+ unsigned long *mt;
+
+ mt = self->state;
+ mt[0]= s & 0xffffffffUL;
+ for (mti=1; mti<N; mti++) {
+ mt[mti] =
+ (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+ /* In the previous versions, MSBs of the seed affect */
+ /* only MSBs of the array mt[]. */
+ /* 2002/01/09 modified by Makoto Matsumoto */
+ mt[mti] &= 0xffffffffUL;
+ /* for >32 bit machines */
+ }
+ self->index = mti;
+ return;
}
/* initialize by an array with array-length */
@@ -170,28 +170,28 @@ init_genrand(RandomObject *self, unsigned long s)
static PyObject *
init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
{
- unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
- unsigned long *mt;
-
- mt = self->state;
- init_genrand(self, 19650218UL);
- i=1; j=0;
- k = (N>key_length ? N : key_length);
- for (; k; k--) {
- mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
- + init_key[j] + j; /* non linear */
- mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
- i++; j++;
- if (i>=N) { mt[0] = mt[N-1]; i=1; }
- if (j>=key_length) j=0;
- }
- for (k=N-1; k; k--) {
- mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- - i; /* non linear */
- mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
- i++;
- if (i>=N) { mt[0] = mt[N-1]; i=1; }
- }
+ unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
+ unsigned long *mt;
+
+ mt = self->state;
+ init_genrand(self, 19650218UL);
+ i=1; j=0;
+ k = (N>key_length ? N : key_length);
+ for (; k; k--) {
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ + init_key[j] + j; /* non linear */
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
+ i++; j++;
+ if (i>=N) { mt[0] = mt[N-1]; i=1; }
+ if (j>=key_length) j=0;
+ }
+ for (k=N-1; k; k--) {
+ mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
+ - i; /* non linear */
+ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
+ i++;
+ if (i>=N) { mt[0] = mt[N-1]; i=1; }
+ }
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
Py_INCREF(Py_None);
@@ -206,291 +206,291 @@ init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_le
static PyObject *
random_seed(RandomObject *self, PyObject *args)
{
- PyObject *result = NULL; /* guilty until proved innocent */
- PyObject *masklower = NULL;
- PyObject *thirtytwo = NULL;
- PyObject *n = NULL;
- unsigned long *key = NULL;
- unsigned long keymax; /* # of allocated slots in key */
- unsigned long keyused; /* # of used slots in key */
- int err;
-
- PyObject *arg = NULL;
-
- if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
- return NULL;
-
- if (arg == NULL || arg == Py_None) {
- time_t now;
-
- time(&now);
- init_genrand(self, (unsigned long)now);
- Py_INCREF(Py_None);
- return Py_None;
- }
- /* If the arg is an int or long, use its absolute value; else use
- * the absolute value of its hash code.
- */
- if (PyLong_Check(arg))
- n = PyNumber_Absolute(arg);
- else {
- long hash = PyObject_Hash(arg);
- if (hash == -1)
- goto Done;
- n = PyLong_FromUnsignedLong((unsigned long)hash);
- }
- if (n == NULL)
- goto Done;
-
- /* Now split n into 32-bit chunks, from the right. Each piece is
- * stored into key, which has a capacity of keymax chunks, of which
- * keyused are filled. Alas, the repeated shifting makes this a
- * quadratic-time algorithm; we'd really like to use
- * _PyLong_AsByteArray here, but then we'd have to break into the
- * long representation to figure out how big an array was needed
- * in advance.
- */
- keymax = 8; /* arbitrary; grows later if needed */
- keyused = 0;
- key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
- if (key == NULL)
- goto Done;
-
- masklower = PyLong_FromUnsignedLong(0xffffffffU);
- if (masklower == NULL)
- goto Done;
- thirtytwo = PyLong_FromLong(32L);
- if (thirtytwo == NULL)
- goto Done;
- while ((err=PyObject_IsTrue(n))) {
- PyObject *newn;
- PyObject *pychunk;
- unsigned long chunk;
-
- if (err == -1)
- goto Done;
- pychunk = PyNumber_And(n, masklower);
- if (pychunk == NULL)
- goto Done;
- chunk = PyLong_AsUnsignedLong(pychunk);
- Py_DECREF(pychunk);
- if (chunk == (unsigned long)-1 && PyErr_Occurred())
- goto Done;
- newn = PyNumber_Rshift(n, thirtytwo);
- if (newn == NULL)
- goto Done;
- Py_DECREF(n);
- n = newn;
- if (keyused >= keymax) {
- unsigned long bigger = keymax << 1;
- if ((bigger >> 1) != keymax) {
- PyErr_NoMemory();
- goto Done;
- }
- key = (unsigned long *)PyMem_Realloc(key,
- bigger * sizeof(*key));
- if (key == NULL)
- goto Done;
- keymax = bigger;
- }
- assert(keyused < keymax);
- key[keyused++] = chunk;
- }
-
- if (keyused == 0)
- key[keyused++] = 0UL;
- result = init_by_array(self, key, keyused);
+ PyObject *result = NULL; /* guilty until proved innocent */
+ PyObject *masklower = NULL;
+ PyObject *thirtytwo = NULL;
+ PyObject *n = NULL;
+ unsigned long *key = NULL;
+ unsigned long keymax; /* # of allocated slots in key */
+ unsigned long keyused; /* # of used slots in key */
+ int err;
+
+ PyObject *arg = NULL;
+
+ if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
+ return NULL;
+
+ if (arg == NULL || arg == Py_None) {
+ time_t now;
+
+ time(&now);
+ init_genrand(self, (unsigned long)now);
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ /* If the arg is an int or long, use its absolute value; else use
+ * the absolute value of its hash code.
+ */
+ if (PyLong_Check(arg))
+ n = PyNumber_Absolute(arg);
+ else {
+ long hash = PyObject_Hash(arg);
+ if (hash == -1)
+ goto Done;
+ n = PyLong_FromUnsignedLong((unsigned long)hash);
+ }
+ if (n == NULL)
+ goto Done;
+
+ /* Now split n into 32-bit chunks, from the right. Each piece is
+ * stored into key, which has a capacity of keymax chunks, of which
+ * keyused are filled. Alas, the repeated shifting makes this a
+ * quadratic-time algorithm; we'd really like to use
+ * _PyLong_AsByteArray here, but then we'd have to break into the
+ * long representation to figure out how big an array was needed
+ * in advance.
+ */
+ keymax = 8; /* arbitrary; grows later if needed */
+ keyused = 0;
+ key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
+ if (key == NULL)
+ goto Done;
+
+ masklower = PyLong_FromUnsignedLong(0xffffffffU);
+ if (masklower == NULL)
+ goto Done;
+ thirtytwo = PyLong_FromLong(32L);
+ if (thirtytwo == NULL)
+ goto Done;
+ while ((err=PyObject_IsTrue(n))) {
+ PyObject *newn;
+ PyObject *pychunk;
+ unsigned long chunk;
+
+ if (err == -1)
+ goto Done;
+ pychunk = PyNumber_And(n, masklower);
+ if (pychunk == NULL)
+ goto Done;
+ chunk = PyLong_AsUnsignedLong(pychunk);
+ Py_DECREF(pychunk);
+ if (chunk == (unsigned long)-1 && PyErr_Occurred())
+ goto Done;
+ newn = PyNumber_Rshift(n, thirtytwo);
+ if (newn == NULL)
+ goto Done;
+ Py_DECREF(n);
+ n = newn;
+ if (keyused >= keymax) {
+ unsigned long bigger = keymax << 1;
+ if ((bigger >> 1) != keymax) {
+ PyErr_NoMemory();
+ goto Done;
+ }
+ key = (unsigned long *)PyMem_Realloc(key,
+ bigger * sizeof(*key));
+ if (key == NULL)
+ goto Done;
+ keymax = bigger;
+ }
+ assert(keyused < keymax);
+ key[keyused++] = chunk;
+ }
+
+ if (keyused == 0)
+ key[keyused++] = 0UL;
+ result = init_by_array(self, key, keyused);
Done:
- Py_XDECREF(masklower);
- Py_XDECREF(thirtytwo);
- Py_XDECREF(n);
- PyMem_Free(key);
- return result;
+ Py_XDECREF(masklower);
+ Py_XDECREF(thirtytwo);
+ Py_XDECREF(n);
+ PyMem_Free(key);
+ return result;
}
static PyObject *
random_getstate(RandomObject *self)
{
- PyObject *state;
- PyObject *element;
- int i;
-
- state = PyTuple_New(N+1);
- if (state == NULL)
- return NULL;
- for (i=0; i<N ; i++) {
- element = PyLong_FromUnsignedLong(self->state[i]);
- if (element == NULL)
- goto Fail;
- PyTuple_SET_ITEM(state, i, element);
- }
- element = PyLong_FromLong((long)(self->index));
- if (element == NULL)
- goto Fail;
- PyTuple_SET_ITEM(state, i, element);
- return state;
+ PyObject *state;
+ PyObject *element;
+ int i;
+
+ state = PyTuple_New(N+1);
+ if (state == NULL)
+ return NULL;
+ for (i=0; i<N ; i++) {
+ element = PyLong_FromUnsignedLong(self->state[i]);
+ if (element == NULL)
+ goto Fail;
+ PyTuple_SET_ITEM(state, i, element);
+ }
+ element = PyLong_FromLong((long)(self->index));
+ if (element == NULL)
+ goto Fail;
+ PyTuple_SET_ITEM(state, i, element);
+ return state;
Fail:
- Py_DECREF(state);
- return NULL;
+ Py_DECREF(state);
+ return NULL;
}
static PyObject *
random_setstate(RandomObject *self, PyObject *state)
{
- int i;
- unsigned long element;
- long index;
-
- if (!PyTuple_Check(state)) {
- PyErr_SetString(PyExc_TypeError,
- "state vector must be a tuple");
- return NULL;
- }
- if (PyTuple_Size(state) != N+1) {
- PyErr_SetString(PyExc_ValueError,
- "state vector is the wrong size");
- return NULL;
- }
-
- for (i=0; i<N ; i++) {
- element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
- if (element == (unsigned long)-1 && PyErr_Occurred())
- return NULL;
- self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
- }
-
- index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
- if (index == -1 && PyErr_Occurred())
- return NULL;
- self->index = (int)index;
-
- Py_INCREF(Py_None);
- return Py_None;
+ int i;
+ unsigned long element;
+ long index;
+
+ if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError,
+ "state vector must be a tuple");
+ return NULL;
+ }
+ if (PyTuple_Size(state) != N+1) {
+ PyErr_SetString(PyExc_ValueError,
+ "state vector is the wrong size");
+ return NULL;
+ }
+
+ for (i=0; i<N ; i++) {
+ element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
+ if (element == (unsigned long)-1 && PyErr_Occurred())
+ return NULL;
+ self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
+ }
+
+ index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
+ if (index == -1 && PyErr_Occurred())
+ return NULL;
+ self->index = (int)index;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
random_getrandbits(RandomObject *self, PyObject *args)
{
- int k, i, bytes;
- unsigned long r;
- unsigned char *bytearray;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
- return NULL;
-
- if (k <= 0) {
- PyErr_SetString(PyExc_ValueError,
- "number of bits must be greater than zero");
- return NULL;
- }
-
- bytes = ((k - 1) / 32 + 1) * 4;
- bytearray = (unsigned char *)PyMem_Malloc(bytes);
- if (bytearray == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- /* Fill-out whole words, byte-by-byte to avoid endianness issues */
- for (i=0 ; i<bytes ; i+=4, k-=32) {
- r = genrand_int32(self);
- if (k < 32)
- r >>= (32 - k);
- bytearray[i+0] = (unsigned char)r;
- bytearray[i+1] = (unsigned char)(r >> 8);
- bytearray[i+2] = (unsigned char)(r >> 16);
- bytearray[i+3] = (unsigned char)(r >> 24);
- }
-
- /* little endian order to match bytearray assignment order */
- result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
- PyMem_Free(bytearray);
- return result;
+ int k, i, bytes;
+ unsigned long r;
+ unsigned char *bytearray;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
+ return NULL;
+
+ if (k <= 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "number of bits must be greater than zero");
+ return NULL;
+ }
+
+ bytes = ((k - 1) / 32 + 1) * 4;
+ bytearray = (unsigned char *)PyMem_Malloc(bytes);
+ if (bytearray == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ /* Fill-out whole words, byte-by-byte to avoid endianness issues */
+ for (i=0 ; i<bytes ; i+=4, k-=32) {
+ r = genrand_int32(self);
+ if (k < 32)
+ r >>= (32 - k);
+ bytearray[i+0] = (unsigned char)r;
+ bytearray[i+1] = (unsigned char)(r >> 8);
+ bytearray[i+2] = (unsigned char)(r >> 16);
+ bytearray[i+3] = (unsigned char)(r >> 24);
+ }
+
+ /* little endian order to match bytearray assignment order */
+ result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
+ PyMem_Free(bytearray);
+ return result;
}
static PyObject *
random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- RandomObject *self;
- PyObject *tmp;
-
- if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
- return NULL;
-
- self = (RandomObject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
- tmp = random_seed(self, args);
- if (tmp == NULL) {
- Py_DECREF(self);
- return NULL;
- }
- Py_DECREF(tmp);
- return (PyObject *)self;
+ RandomObject *self;
+ PyObject *tmp;
+
+ if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
+ return NULL;
+
+ self = (RandomObject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+ tmp = random_seed(self, args);
+ if (tmp == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ Py_DECREF(tmp);
+ return (PyObject *)self;
}
static PyMethodDef random_methods[] = {
- {"random", (PyCFunction)random_random, METH_NOARGS,
- PyDoc_STR("random() -> x in the interval [0, 1).")},
- {"seed", (PyCFunction)random_seed, METH_VARARGS,
- PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
- {"getstate", (PyCFunction)random_getstate, METH_NOARGS,
- PyDoc_STR("getstate() -> tuple containing the current state.")},
- {"setstate", (PyCFunction)random_setstate, METH_O,
- PyDoc_STR("setstate(state) -> None. Restores generator state.")},
- {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
- PyDoc_STR("getrandbits(k) -> x. Generates a long int with "
- "k random bits.")},
- {NULL, NULL} /* sentinel */
+ {"random", (PyCFunction)random_random, METH_NOARGS,
+ PyDoc_STR("random() -> x in the interval [0, 1).")},
+ {"seed", (PyCFunction)random_seed, METH_VARARGS,
+ PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
+ {"getstate", (PyCFunction)random_getstate, METH_NOARGS,
+ PyDoc_STR("getstate() -> tuple containing the current state.")},
+ {"setstate", (PyCFunction)random_setstate, METH_O,
+ PyDoc_STR("setstate(state) -> None. Restores generator state.")},
+ {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
+ PyDoc_STR("getrandbits(k) -> x. Generates a long int with "
+ "k random bits.")},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(random_doc,
"Random() -> create a random number generator with its own internal state.");
static PyTypeObject Random_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_random.Random", /*tp_name*/
- sizeof(RandomObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- PyObject_GenericGetAttr, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- random_doc, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- random_methods, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- random_new, /*tp_new*/
- PyObject_Free, /*tp_free*/
- 0, /*tp_is_gc*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_random.Random", /*tp_name*/
+ sizeof(RandomObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ random_doc, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ random_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ random_new, /*tp_new*/
+ PyObject_Free, /*tp_free*/
+ 0, /*tp_is_gc*/
};
PyDoc_STRVAR(module_doc,
@@ -498,28 +498,28 @@ PyDoc_STRVAR(module_doc,
static struct PyModuleDef _randommodule = {
- PyModuleDef_HEAD_INIT,
- "_random",
- module_doc,
- -1,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_random",
+ module_doc,
+ -1,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__random(void)
{
- PyObject *m;
-
- if (PyType_Ready(&Random_Type) < 0)
- return NULL;
- m = PyModule_Create(&_randommodule);
- if (m == NULL)
- return NULL;
- Py_INCREF(&Random_Type);
- PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
- return m;
+ PyObject *m;
+
+ if (PyType_Ready(&Random_Type) < 0)
+ return NULL;
+ m = PyModule_Create(&_randommodule);
+ if (m == NULL)
+ return NULL;
+ Py_INCREF(&Random_Type);
+ PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
+ return m;
}
diff --git a/Modules/_scproxy.c b/Modules/_scproxy.c
index 602fbe3..30afa6a 100644
--- a/Modules/_scproxy.c
+++ b/Modules/_scproxy.c
@@ -5,167 +5,167 @@
#include <Python.h>
#include <SystemConfiguration/SystemConfiguration.h>
-static int32_t
+static int32_t
cfnum_to_int32(CFNumberRef num)
{
- int32_t result;
+ int32_t result;
- CFNumberGetValue(num, kCFNumberSInt32Type, &result);
- return result;
+ CFNumberGetValue(num, kCFNumberSInt32Type, &result);
+ return result;
}
static PyObject*
cfstring_to_pystring(CFStringRef ref)
{
- const char* s;
-
- s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8);
- if (s) {
- return PyUnicode_DecodeUTF8(
- s, strlen(s), NULL);
-
- } else {
- CFIndex len = CFStringGetLength(ref);
- Boolean ok;
- PyObject* result;
- char* buf;
-
- buf = PyMem_Malloc(len*4);
- if (buf == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- ok = CFStringGetCString(ref,
- buf, len * 4,
- kCFStringEncodingUTF8);
- if (!ok) {
- PyMem_Free(buf);
- return NULL;
- } else {
- result = PyUnicode_DecodeUTF8(
- buf, strlen(buf), NULL);
- PyMem_Free(buf);
- }
- return result;
- }
+ const char* s;
+
+ s = CFStringGetCStringPtr(ref, kCFStringEncodingUTF8);
+ if (s) {
+ return PyUnicode_DecodeUTF8(
+ s, strlen(s), NULL);
+
+ } else {
+ CFIndex len = CFStringGetLength(ref);
+ Boolean ok;
+ PyObject* result;
+ char* buf;
+
+ buf = PyMem_Malloc(len*4);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ ok = CFStringGetCString(ref,
+ buf, len * 4,
+ kCFStringEncodingUTF8);
+ if (!ok) {
+ PyMem_Free(buf);
+ return NULL;
+ } else {
+ result = PyUnicode_DecodeUTF8(
+ buf, strlen(buf), NULL);
+ PyMem_Free(buf);
+ }
+ return result;
+ }
}
static PyObject*
get_proxy_settings(PyObject* mod __attribute__((__unused__)))
{
- CFDictionaryRef proxyDict = NULL;
- CFNumberRef aNum = NULL;
- CFArrayRef anArray = NULL;
- PyObject* result = NULL;
- PyObject* v;
- int r;
-
- proxyDict = SCDynamicStoreCopyProxies(NULL);
- if (!proxyDict) {
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- result = PyDict_New();
- if (result == NULL) goto error;
-
- if (&kSCPropNetProxiesExcludeSimpleHostnames != NULL) {
- aNum = CFDictionaryGetValue(proxyDict,
- kSCPropNetProxiesExcludeSimpleHostnames);
- if (aNum == NULL) {
- v = PyBool_FromLong(1);
- } else {
- v = PyBool_FromLong(cfnum_to_int32(aNum));
- }
- } else {
- v = PyBool_FromLong(1);
- }
-
- if (v == NULL) goto error;
-
- r = PyDict_SetItemString(result, "exclude_simple", v);
- Py_DECREF(v); v = NULL;
- if (r == -1) goto error;
-
- anArray = CFDictionaryGetValue(proxyDict,
- kSCPropNetProxiesExceptionsList);
- if (anArray != NULL) {
- CFIndex len = CFArrayGetCount(anArray);
- CFIndex i;
- v = PyTuple_New(len);
- if (v == NULL) goto error;
-
- r = PyDict_SetItemString(result, "exceptions", v);
- Py_DECREF(v);
- if (r == -1) goto error;
-
- for (i = 0; i < len; i++) {
- CFStringRef aString = NULL;
-
- aString = CFArrayGetValueAtIndex(anArray, i);
- if (aString == NULL) {
- PyTuple_SetItem(v, i, Py_None);
- Py_INCREF(Py_None);
- } else {
- PyObject* t = cfstring_to_pystring(aString);
- if (!t) {
- PyTuple_SetItem(v, i, Py_None);
- Py_INCREF(Py_None);
- } else {
- PyTuple_SetItem(v, i, t);
- }
- }
- }
- }
-
- CFRelease(proxyDict);
- return result;
+ CFDictionaryRef proxyDict = NULL;
+ CFNumberRef aNum = NULL;
+ CFArrayRef anArray = NULL;
+ PyObject* result = NULL;
+ PyObject* v;
+ int r;
+
+ proxyDict = SCDynamicStoreCopyProxies(NULL);
+ if (!proxyDict) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ result = PyDict_New();
+ if (result == NULL) goto error;
+
+ if (&kSCPropNetProxiesExcludeSimpleHostnames != NULL) {
+ aNum = CFDictionaryGetValue(proxyDict,
+ kSCPropNetProxiesExcludeSimpleHostnames);
+ if (aNum == NULL) {
+ v = PyBool_FromLong(1);
+ } else {
+ v = PyBool_FromLong(cfnum_to_int32(aNum));
+ }
+ } else {
+ v = PyBool_FromLong(1);
+ }
+
+ if (v == NULL) goto error;
+
+ r = PyDict_SetItemString(result, "exclude_simple", v);
+ Py_DECREF(v); v = NULL;
+ if (r == -1) goto error;
+
+ anArray = CFDictionaryGetValue(proxyDict,
+ kSCPropNetProxiesExceptionsList);
+ if (anArray != NULL) {
+ CFIndex len = CFArrayGetCount(anArray);
+ CFIndex i;
+ v = PyTuple_New(len);
+ if (v == NULL) goto error;
+
+ r = PyDict_SetItemString(result, "exceptions", v);
+ Py_DECREF(v);
+ if (r == -1) goto error;
+
+ for (i = 0; i < len; i++) {
+ CFStringRef aString = NULL;
+
+ aString = CFArrayGetValueAtIndex(anArray, i);
+ if (aString == NULL) {
+ PyTuple_SetItem(v, i, Py_None);
+ Py_INCREF(Py_None);
+ } else {
+ PyObject* t = cfstring_to_pystring(aString);
+ if (!t) {
+ PyTuple_SetItem(v, i, Py_None);
+ Py_INCREF(Py_None);
+ } else {
+ PyTuple_SetItem(v, i, t);
+ }
+ }
+ }
+ }
+
+ CFRelease(proxyDict);
+ return result;
error:
- if (proxyDict) CFRelease(proxyDict);
- Py_XDECREF(result);
- return NULL;
+ if (proxyDict) CFRelease(proxyDict);
+ Py_XDECREF(result);
+ return NULL;
}
static int
set_proxy(PyObject* proxies, char* proto, CFDictionaryRef proxyDict,
- CFStringRef enabledKey,
- CFStringRef hostKey, CFStringRef portKey)
+ CFStringRef enabledKey,
+ CFStringRef hostKey, CFStringRef portKey)
{
- CFNumberRef aNum;
-
- aNum = CFDictionaryGetValue(proxyDict, enabledKey);
- if (aNum && cfnum_to_int32(aNum)) {
- CFStringRef hostString;
-
- hostString = CFDictionaryGetValue(proxyDict, hostKey);
- aNum = CFDictionaryGetValue(proxyDict, portKey);
-
- if (hostString) {
- int r;
- PyObject* h = cfstring_to_pystring(hostString);
- PyObject* v;
- if (h) {
- if (aNum) {
- int32_t port = cfnum_to_int32(aNum);
- v = PyUnicode_FromFormat("http://%U:%ld",
- h, (long)port);
- } else {
- v = PyUnicode_FromFormat("http://%U", h);
- }
- Py_DECREF(h);
- if (!v) return -1;
- r = PyDict_SetItemString(proxies, proto,
- v);
- Py_DECREF(v);
- return r;
- }
- }
-
- }
- return 0;
+ CFNumberRef aNum;
+
+ aNum = CFDictionaryGetValue(proxyDict, enabledKey);
+ if (aNum && cfnum_to_int32(aNum)) {
+ CFStringRef hostString;
+
+ hostString = CFDictionaryGetValue(proxyDict, hostKey);
+ aNum = CFDictionaryGetValue(proxyDict, portKey);
+
+ if (hostString) {
+ int r;
+ PyObject* h = cfstring_to_pystring(hostString);
+ PyObject* v;
+ if (h) {
+ if (aNum) {
+ int32_t port = cfnum_to_int32(aNum);
+ v = PyUnicode_FromFormat("http://%U:%ld",
+ h, (long)port);
+ } else {
+ v = PyUnicode_FromFormat("http://%U", h);
+ }
+ Py_DECREF(h);
+ if (!v) return -1;
+ r = PyDict_SetItemString(proxies, proto,
+ v);
+ Py_DECREF(v);
+ return r;
+ }
+ }
+
+ }
+ return 0;
}
@@ -173,75 +173,75 @@ set_proxy(PyObject* proxies, char* proto, CFDictionaryRef proxyDict,
static PyObject*
get_proxies(PyObject* mod __attribute__((__unused__)))
{
- PyObject* result = NULL;
- int r;
- CFDictionaryRef proxyDict = NULL;
-
- proxyDict = SCDynamicStoreCopyProxies(NULL);
- if (proxyDict == NULL) {
- return PyDict_New();
- }
-
- result = PyDict_New();
- if (result == NULL) goto error;
-
- r = set_proxy(result, "http", proxyDict,
- kSCPropNetProxiesHTTPEnable,
- kSCPropNetProxiesHTTPProxy,
- kSCPropNetProxiesHTTPPort);
- if (r == -1) goto error;
- r = set_proxy(result, "https", proxyDict,
- kSCPropNetProxiesHTTPSEnable,
- kSCPropNetProxiesHTTPSProxy,
- kSCPropNetProxiesHTTPSPort);
- if (r == -1) goto error;
- r = set_proxy(result, "ftp", proxyDict,
- kSCPropNetProxiesFTPEnable,
- kSCPropNetProxiesFTPProxy,
- kSCPropNetProxiesFTPPort);
- if (r == -1) goto error;
- r = set_proxy(result, "gopher", proxyDict,
- kSCPropNetProxiesGopherEnable,
- kSCPropNetProxiesGopherProxy,
- kSCPropNetProxiesGopherPort);
- if (r == -1) goto error;
-
- CFRelease(proxyDict);
- return result;
+ PyObject* result = NULL;
+ int r;
+ CFDictionaryRef proxyDict = NULL;
+
+ proxyDict = SCDynamicStoreCopyProxies(NULL);
+ if (proxyDict == NULL) {
+ return PyDict_New();
+ }
+
+ result = PyDict_New();
+ if (result == NULL) goto error;
+
+ r = set_proxy(result, "http", proxyDict,
+ kSCPropNetProxiesHTTPEnable,
+ kSCPropNetProxiesHTTPProxy,
+ kSCPropNetProxiesHTTPPort);
+ if (r == -1) goto error;
+ r = set_proxy(result, "https", proxyDict,
+ kSCPropNetProxiesHTTPSEnable,
+ kSCPropNetProxiesHTTPSProxy,
+ kSCPropNetProxiesHTTPSPort);
+ if (r == -1) goto error;
+ r = set_proxy(result, "ftp", proxyDict,
+ kSCPropNetProxiesFTPEnable,
+ kSCPropNetProxiesFTPProxy,
+ kSCPropNetProxiesFTPPort);
+ if (r == -1) goto error;
+ r = set_proxy(result, "gopher", proxyDict,
+ kSCPropNetProxiesGopherEnable,
+ kSCPropNetProxiesGopherProxy,
+ kSCPropNetProxiesGopherPort);
+ if (r == -1) goto error;
+
+ CFRelease(proxyDict);
+ return result;
error:
- if (proxyDict) CFRelease(proxyDict);
- Py_XDECREF(result);
- return NULL;
+ if (proxyDict) CFRelease(proxyDict);
+ Py_XDECREF(result);
+ return NULL;
}
static PyMethodDef mod_methods[] = {
- {
- "_get_proxy_settings",
- (PyCFunction)get_proxy_settings,
- METH_NOARGS,
- NULL,
- },
- {
- "_get_proxies",
- (PyCFunction)get_proxies,
- METH_NOARGS,
- NULL,
- },
- { 0, 0, 0, 0 }
+ {
+ "_get_proxy_settings",
+ (PyCFunction)get_proxy_settings,
+ METH_NOARGS,
+ NULL,
+ },
+ {
+ "_get_proxies",
+ (PyCFunction)get_proxies,
+ METH_NOARGS,
+ NULL,
+ },
+ { 0, 0, 0, 0 }
};
static struct PyModuleDef mod_module = {
- PyModuleDef_HEAD_INIT,
- "_scproxy",
- NULL,
- -1,
- mod_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_scproxy",
+ NULL,
+ -1,
+ mod_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
@@ -249,10 +249,10 @@ static struct PyModuleDef mod_module = {
extern "C" {
#endif
-PyObject*
+PyObject*
PyInit__scproxy(void)
{
- return PyModule_Create(&mod_module);
+ return PyModule_Create(&mod_module);
}
#ifdef __cplusplus
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 8e54699..155703c 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -64,7 +64,7 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
&database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
{
- return NULL;
+ return NULL;
}
if (factory == NULL) {
@@ -93,7 +93,7 @@ static PyObject* module_complete(PyObject* self, PyObject* args, PyObject*
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &statement))
{
- return NULL;
+ return NULL;
}
if (sqlite3_complete(statement)) {
@@ -122,7 +122,7 @@ static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyOb
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &do_enable))
{
- return NULL;
+ return NULL;
}
rc = sqlite3_enable_shared_cache(do_enable);
@@ -302,15 +302,15 @@ static IntConstantPair _int_constants[] = {
static struct PyModuleDef _sqlite3module = {
- PyModuleDef_HEAD_INIT,
- "_sqlite3",
- NULL,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_sqlite3",
+ NULL,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC PyInit__sqlite3(void)
@@ -329,7 +329,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
(pysqlite_statement_setup_types() < 0) ||
(pysqlite_prepare_protocol_setup_types() < 0)
) {
- Py_DECREF(module);
+ Py_DECREF(module);
return NULL;
}
@@ -448,7 +448,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
/* Original comment from _bsddb.c in the Python core. This is also still
* needed nowadays for Python 2.3/2.4.
- *
+ *
* PyEval_InitThreads is called here due to a quirk in python 1.5
* - 2.2.1 (at least) according to Russell Williamson <merel@wt.net>:
* The global interpreter lock is not initialized until the first
@@ -467,8 +467,8 @@ error:
if (PyErr_Occurred())
{
PyErr_SetString(PyExc_ImportError, MODULE_NAME ": init failed");
- Py_DECREF(module);
- module = NULL;
+ Py_DECREF(module);
+ module = NULL;
}
return module;
}
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 6a89d8c..3f01c25 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -14,30 +14,30 @@ static PyTypeObject PyStructType;
/* The translation function for each format character is table driven */
typedef struct _formatdef {
- char format;
- Py_ssize_t size;
- Py_ssize_t alignment;
- PyObject* (*unpack)(const char *,
- const struct _formatdef *);
- int (*pack)(char *, PyObject *,
- const struct _formatdef *);
+ char format;
+ Py_ssize_t size;
+ Py_ssize_t alignment;
+ PyObject* (*unpack)(const char *,
+ const struct _formatdef *);
+ int (*pack)(char *, PyObject *,
+ const struct _formatdef *);
} formatdef;
typedef struct _formatcode {
- const struct _formatdef *fmtdef;
- Py_ssize_t offset;
- Py_ssize_t size;
+ const struct _formatdef *fmtdef;
+ Py_ssize_t offset;
+ Py_ssize_t size;
} formatcode;
/* Struct object interface */
typedef struct {
- PyObject_HEAD
- Py_ssize_t s_size;
- Py_ssize_t s_len;
- formatcode *s_codes;
- PyObject *s_format;
- PyObject *weakreflist; /* List of weak references */
+ PyObject_HEAD
+ Py_ssize_t s_size;
+ Py_ssize_t s_len;
+ formatcode *s_codes;
+ PyObject *s_format;
+ PyObject *weakreflist; /* List of weak references */
} PyStructObject;
@@ -94,15 +94,15 @@ typedef struct { char c; _Bool x; } s_bool;
static PyObject *
get_pylong(PyObject *v)
{
- assert(v != NULL);
- if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return NULL;
- }
+ assert(v != NULL);
+ if (!PyLong_Check(v)) {
+ PyErr_SetString(StructError,
+ "required argument is not an integer");
+ return NULL;
+ }
- Py_INCREF(v);
- return v;
+ Py_INCREF(v);
+ return v;
}
/* Helper routine to get a C long and raise the appropriate error if it isn't
@@ -111,22 +111,22 @@ get_pylong(PyObject *v)
static int
get_long(PyObject *v, long *p)
{
- long x;
+ long x;
- if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return -1;
- }
- x = PyLong_AsLong(v);
- if (x == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(StructError,
- "argument out of range");
- return -1;
- }
- *p = x;
- return 0;
+ if (!PyLong_Check(v)) {
+ PyErr_SetString(StructError,
+ "required argument is not an integer");
+ return -1;
+ }
+ x = PyLong_AsLong(v);
+ if (x == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_SetString(StructError,
+ "argument out of range");
+ return -1;
+ }
+ *p = x;
+ return 0;
}
@@ -136,22 +136,22 @@ get_long(PyObject *v, long *p)
static int
get_ulong(PyObject *v, unsigned long *p)
{
- unsigned long x;
-
- if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return -1;
- }
- x = PyLong_AsUnsignedLong(v);
- if (x == (unsigned long)-1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(StructError,
- "argument out of range");
- return -1;
- }
- *p = x;
- return 0;
+ unsigned long x;
+
+ if (!PyLong_Check(v)) {
+ PyErr_SetString(StructError,
+ "required argument is not an integer");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLong(v);
+ if (x == (unsigned long)-1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_SetString(StructError,
+ "argument out of range");
+ return -1;
+ }
+ *p = x;
+ return 0;
}
#endif /* PY_STRUCT_OVERFLOW_MASKING */
@@ -162,21 +162,21 @@ get_ulong(PyObject *v, unsigned long *p)
static int
get_longlong(PyObject *v, PY_LONG_LONG *p)
{
- PY_LONG_LONG x;
- if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return -1;
- }
- x = PyLong_AsLongLong(v);
- if (x == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(StructError,
- "argument out of range");
- return -1;
- }
- *p = x;
- return 0;
+ PY_LONG_LONG x;
+ if (!PyLong_Check(v)) {
+ PyErr_SetString(StructError,
+ "required argument is not an integer");
+ return -1;
+ }
+ x = PyLong_AsLongLong(v);
+ if (x == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_SetString(StructError,
+ "argument out of range");
+ return -1;
+ }
+ *p = x;
+ return 0;
}
/* Same, but handling native unsigned long long. */
@@ -184,21 +184,21 @@ get_longlong(PyObject *v, PY_LONG_LONG *p)
static int
get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
{
- unsigned PY_LONG_LONG x;
- if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return -1;
- }
- x = PyLong_AsUnsignedLongLong(v);
- if (x == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(StructError,
- "argument out of range");
- return -1;
- }
- *p = x;
- return 0;
+ unsigned PY_LONG_LONG x;
+ if (!PyLong_Check(v)) {
+ PyErr_SetString(StructError,
+ "required argument is not an integer");
+ return -1;
+ }
+ x = PyLong_AsUnsignedLongLong(v);
+ if (x == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_SetString(StructError,
+ "argument out of range");
+ return -1;
+ }
+ *p = x;
+ return 0;
}
#endif
@@ -211,57 +211,57 @@ get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
static PyObject *
unpack_float(const char *p, /* start of 4-byte string */
- int le) /* true for little-endian, false for big-endian */
+ int le) /* true for little-endian, false for big-endian */
{
- double x;
+ double x;
- x = _PyFloat_Unpack4((unsigned char *)p, le);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(x);
+ x = _PyFloat_Unpack4((unsigned char *)p, le);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(x);
}
static PyObject *
unpack_double(const char *p, /* start of 8-byte string */
- int le) /* true for little-endian, false for big-endian */
+ int le) /* true for little-endian, false for big-endian */
{
- double x;
+ double x;
- x = _PyFloat_Unpack8((unsigned char *)p, le);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(x);
+ x = _PyFloat_Unpack8((unsigned char *)p, le);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(x);
}
/* Helper to format the range error exceptions */
static int
_range_error(const formatdef *f, int is_unsigned)
{
- /* ulargest is the largest unsigned value with f->size bytes.
- * Note that the simpler:
- * ((size_t)1 << (f->size * 8)) - 1
- * doesn't work when f->size == sizeof(size_t) because C doesn't
- * define what happens when a left shift count is >= the number of
- * bits in the integer being shifted; e.g., on some boxes it doesn't
- * shift at all when they're equal.
- */
- const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
- assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
- if (is_unsigned)
- PyErr_Format(StructError,
- "'%c' format requires 0 <= number <= %zu",
- f->format,
- ulargest);
- else {
- const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
- PyErr_Format(StructError,
- "'%c' format requires %zd <= number <= %zd",
- f->format,
- ~ largest,
- largest);
- }
-
- return -1;
+ /* ulargest is the largest unsigned value with f->size bytes.
+ * Note that the simpler:
+ * ((size_t)1 << (f->size * 8)) - 1
+ * doesn't work when f->size == sizeof(size_t) because C doesn't
+ * define what happens when a left shift count is >= the number of
+ * bits in the integer being shifted; e.g., on some boxes it doesn't
+ * shift at all when they're equal.
+ */
+ const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
+ assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
+ if (is_unsigned)
+ PyErr_Format(StructError,
+ "'%c' format requires 0 <= number <= %zu",
+ f->format,
+ ulargest);
+ else {
+ const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
+ PyErr_Format(StructError,
+ "'%c' format requires %zd <= number <= %zd",
+ f->format,
+ ~ largest,
+ largest);
+ }
+
+ return -1;
}
@@ -288,75 +288,75 @@ _range_error(const formatdef *f, int is_unsigned)
static PyObject *
nu_char(const char *p, const formatdef *f)
{
- return PyBytes_FromStringAndSize(p, 1);
+ return PyBytes_FromStringAndSize(p, 1);
}
static PyObject *
nu_byte(const char *p, const formatdef *f)
{
- return PyLong_FromLong((long) *(signed char *)p);
+ return PyLong_FromLong((long) *(signed char *)p);
}
static PyObject *
nu_ubyte(const char *p, const formatdef *f)
{
- return PyLong_FromLong((long) *(unsigned char *)p);
+ return PyLong_FromLong((long) *(unsigned char *)p);
}
static PyObject *
nu_short(const char *p, const formatdef *f)
{
- short x;
- memcpy((char *)&x, p, sizeof x);
- return PyLong_FromLong((long)x);
+ short x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyLong_FromLong((long)x);
}
static PyObject *
nu_ushort(const char *p, const formatdef *f)
{
- unsigned short x;
- memcpy((char *)&x, p, sizeof x);
- return PyLong_FromLong((long)x);
+ unsigned short x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyLong_FromLong((long)x);
}
static PyObject *
nu_int(const char *p, const formatdef *f)
{
- int x;
- memcpy((char *)&x, p, sizeof x);
- return PyLong_FromLong((long)x);
+ int x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyLong_FromLong((long)x);
}
static PyObject *
nu_uint(const char *p, const formatdef *f)
{
- unsigned int x;
- memcpy((char *)&x, p, sizeof x);
+ unsigned int x;
+ memcpy((char *)&x, p, sizeof x);
#if (SIZEOF_LONG > SIZEOF_INT)
- return PyLong_FromLong((long)x);
+ return PyLong_FromLong((long)x);
#else
- if (x <= ((unsigned int)LONG_MAX))
- return PyLong_FromLong((long)x);
- return PyLong_FromUnsignedLong((unsigned long)x);
+ if (x <= ((unsigned int)LONG_MAX))
+ return PyLong_FromLong((long)x);
+ return PyLong_FromUnsignedLong((unsigned long)x);
#endif
}
static PyObject *
nu_long(const char *p, const formatdef *f)
{
- long x;
- memcpy((char *)&x, p, sizeof x);
- return PyLong_FromLong(x);
+ long x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyLong_FromLong(x);
}
static PyObject *
nu_ulong(const char *p, const formatdef *f)
{
- unsigned long x;
- memcpy((char *)&x, p, sizeof x);
- if (x <= LONG_MAX)
- return PyLong_FromLong((long)x);
- return PyLong_FromUnsignedLong(x);
+ unsigned long x;
+ memcpy((char *)&x, p, sizeof x);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong((long)x);
+ return PyLong_FromUnsignedLong(x);
}
/* Native mode doesn't support q or Q unless the platform C supports
@@ -367,21 +367,21 @@ nu_ulong(const char *p, const formatdef *f)
static PyObject *
nu_longlong(const char *p, const formatdef *f)
{
- PY_LONG_LONG x;
- memcpy((char *)&x, p, sizeof x);
- if (x >= LONG_MIN && x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
- return PyLong_FromLongLong(x);
+ PY_LONG_LONG x;
+ memcpy((char *)&x, p, sizeof x);
+ if (x >= LONG_MIN && x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+ return PyLong_FromLongLong(x);
}
static PyObject *
nu_ulonglong(const char *p, const formatdef *f)
{
- unsigned PY_LONG_LONG x;
- memcpy((char *)&x, p, sizeof x);
- if (x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
- return PyLong_FromUnsignedLongLong(x);
+ unsigned PY_LONG_LONG x;
+ memcpy((char *)&x, p, sizeof x);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+ return PyLong_FromUnsignedLongLong(x);
}
#endif
@@ -389,168 +389,168 @@ nu_ulonglong(const char *p, const formatdef *f)
static PyObject *
nu_bool(const char *p, const formatdef *f)
{
- BOOL_TYPE x;
- memcpy((char *)&x, p, sizeof x);
- return PyBool_FromLong(x != 0);
+ BOOL_TYPE x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyBool_FromLong(x != 0);
}
static PyObject *
nu_float(const char *p, const formatdef *f)
{
- float x;
- memcpy((char *)&x, p, sizeof x);
- return PyFloat_FromDouble((double)x);
+ float x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyFloat_FromDouble((double)x);
}
static PyObject *
nu_double(const char *p, const formatdef *f)
{
- double x;
- memcpy((char *)&x, p, sizeof x);
- return PyFloat_FromDouble(x);
+ double x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyFloat_FromDouble(x);
}
static PyObject *
nu_void_p(const char *p, const formatdef *f)
{
- void *x;
- memcpy((char *)&x, p, sizeof x);
- return PyLong_FromVoidPtr(x);
+ void *x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyLong_FromVoidPtr(x);
}
static int
np_byte(char *p, PyObject *v, const formatdef *f)
{
- long x;
- if (get_long(v, &x) < 0)
- return -1;
- if (x < -128 || x > 127){
- PyErr_SetString(StructError,
- "byte format requires -128 <= number <= 127");
- return -1;
- }
- *p = (char)x;
- return 0;
+ long x;
+ if (get_long(v, &x) < 0)
+ return -1;
+ if (x < -128 || x > 127){
+ PyErr_SetString(StructError,
+ "byte format requires -128 <= number <= 127");
+ return -1;
+ }
+ *p = (char)x;
+ return 0;
}
static int
np_ubyte(char *p, PyObject *v, const formatdef *f)
{
- long x;
- if (get_long(v, &x) < 0)
- return -1;
- if (x < 0 || x > 255){
- PyErr_SetString(StructError,
- "ubyte format requires 0 <= number <= 255");
- return -1;
- }
- *p = (char)x;
- return 0;
+ long x;
+ if (get_long(v, &x) < 0)
+ return -1;
+ if (x < 0 || x > 255){
+ PyErr_SetString(StructError,
+ "ubyte format requires 0 <= number <= 255");
+ return -1;
+ }
+ *p = (char)x;
+ return 0;
}
static int
np_char(char *p, PyObject *v, const formatdef *f)
{
- if (PyUnicode_Check(v)) {
- v = _PyUnicode_AsDefaultEncodedString(v, NULL);
- if (v == NULL)
- return -1;
- }
- if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
- PyErr_SetString(StructError,
- "char format requires bytes or string of length 1");
- return -1;
- }
- *p = *PyBytes_AsString(v);
- return 0;
+ if (PyUnicode_Check(v)) {
+ v = _PyUnicode_AsDefaultEncodedString(v, NULL);
+ if (v == NULL)
+ return -1;
+ }
+ if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
+ PyErr_SetString(StructError,
+ "char format requires bytes or string of length 1");
+ return -1;
+ }
+ *p = *PyBytes_AsString(v);
+ return 0;
}
static int
np_short(char *p, PyObject *v, const formatdef *f)
{
- long x;
- short y;
- if (get_long(v, &x) < 0)
- return -1;
- if (x < SHRT_MIN || x > SHRT_MAX){
- PyErr_SetString(StructError,
- "short format requires " STRINGIFY(SHRT_MIN)
- " <= number <= " STRINGIFY(SHRT_MAX));
- return -1;
- }
- y = (short)x;
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ long x;
+ short y;
+ if (get_long(v, &x) < 0)
+ return -1;
+ if (x < SHRT_MIN || x > SHRT_MAX){
+ PyErr_SetString(StructError,
+ "short format requires " STRINGIFY(SHRT_MIN)
+ " <= number <= " STRINGIFY(SHRT_MAX));
+ return -1;
+ }
+ y = (short)x;
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static int
np_ushort(char *p, PyObject *v, const formatdef *f)
{
- long x;
- unsigned short y;
- if (get_long(v, &x) < 0)
- return -1;
- if (x < 0 || x > USHRT_MAX){
- PyErr_SetString(StructError,
- "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
- return -1;
- }
- y = (unsigned short)x;
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ long x;
+ unsigned short y;
+ if (get_long(v, &x) < 0)
+ return -1;
+ if (x < 0 || x > USHRT_MAX){
+ PyErr_SetString(StructError,
+ "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
+ return -1;
+ }
+ y = (unsigned short)x;
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static int
np_int(char *p, PyObject *v, const formatdef *f)
{
- long x;
- int y;
- if (get_long(v, &x) < 0)
- return -1;
+ long x;
+ int y;
+ if (get_long(v, &x) < 0)
+ return -1;
#if (SIZEOF_LONG > SIZEOF_INT)
- if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
- RANGE_ERROR(x, f, 0, -1);
+ if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
+ RANGE_ERROR(x, f, 0, -1);
#endif
- y = (int)x;
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ y = (int)x;
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static int
np_uint(char *p, PyObject *v, const formatdef *f)
{
- unsigned long x;
- unsigned int y;
- if (get_ulong(v, &x) < 0)
- return -1;
- y = (unsigned int)x;
+ unsigned long x;
+ unsigned int y;
+ if (get_ulong(v, &x) < 0)
+ return -1;
+ y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT)
- if (x > ((unsigned long)UINT_MAX))
- RANGE_ERROR(y, f, 1, -1);
+ if (x > ((unsigned long)UINT_MAX))
+ RANGE_ERROR(y, f, 1, -1);
#endif
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static int
np_long(char *p, PyObject *v, const formatdef *f)
{
- long x;
- if (get_long(v, &x) < 0)
- return -1;
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ long x;
+ if (get_long(v, &x) < 0)
+ return -1;
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
static int
np_ulong(char *p, PyObject *v, const formatdef *f)
{
- unsigned long x;
- if (get_ulong(v, &x) < 0)
- return -1;
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ unsigned long x;
+ if (get_ulong(v, &x) < 0)
+ return -1;
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
#ifdef HAVE_LONG_LONG
@@ -558,21 +558,21 @@ np_ulong(char *p, PyObject *v, const formatdef *f)
static int
np_longlong(char *p, PyObject *v, const formatdef *f)
{
- PY_LONG_LONG x;
- if (get_longlong(v, &x) < 0)
- return -1;
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ PY_LONG_LONG x;
+ if (get_longlong(v, &x) < 0)
+ return -1;
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
static int
np_ulonglong(char *p, PyObject *v, const formatdef *f)
{
- unsigned PY_LONG_LONG x;
- if (get_ulonglong(v, &x) < 0)
- return -1;
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ unsigned PY_LONG_LONG x;
+ if (get_ulonglong(v, &x) < 0)
+ return -1;
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
#endif
@@ -580,77 +580,77 @@ np_ulonglong(char *p, PyObject *v, const formatdef *f)
static int
np_bool(char *p, PyObject *v, const formatdef *f)
{
- BOOL_TYPE y;
- y = PyObject_IsTrue(v);
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ BOOL_TYPE y;
+ y = PyObject_IsTrue(v);
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static int
np_float(char *p, PyObject *v, const formatdef *f)
{
- float x = (float)PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ float x = (float)PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
static int
np_double(char *p, PyObject *v, const formatdef *f)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- memcpy(p, (char *)&x, sizeof(double));
- return 0;
+ double x = PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ memcpy(p, (char *)&x, sizeof(double));
+ return 0;
}
static int
np_void_p(char *p, PyObject *v, const formatdef *f)
{
- void *x;
+ void *x;
- v = get_pylong(v);
- if (v == NULL)
- return -1;
- assert(PyLong_Check(v));
- x = PyLong_AsVoidPtr(v);
- Py_DECREF(v);
- if (x == NULL && PyErr_Occurred())
- return -1;
- memcpy(p, (char *)&x, sizeof x);
- return 0;
+ v = get_pylong(v);
+ if (v == NULL)
+ return -1;
+ assert(PyLong_Check(v));
+ x = PyLong_AsVoidPtr(v);
+ Py_DECREF(v);
+ if (x == NULL && PyErr_Occurred())
+ return -1;
+ memcpy(p, (char *)&x, sizeof x);
+ return 0;
}
static formatdef native_table[] = {
- {'x', sizeof(char), 0, NULL},
- {'b', sizeof(char), 0, nu_byte, np_byte},
- {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
- {'c', sizeof(char), 0, nu_char, np_char},
- {'s', sizeof(char), 0, NULL},
- {'p', sizeof(char), 0, NULL},
- {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
- {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
- {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
- {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
- {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
- {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
+ {'x', sizeof(char), 0, NULL},
+ {'b', sizeof(char), 0, nu_byte, np_byte},
+ {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
+ {'c', sizeof(char), 0, nu_char, np_char},
+ {'s', sizeof(char), 0, NULL},
+ {'p', sizeof(char), 0, NULL},
+ {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
+ {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
+ {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
+ {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
+ {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
+ {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
#ifdef HAVE_LONG_LONG
- {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
- {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
+ {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
+ {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
#endif
- {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
- {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
- {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
- {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
- {0}
+ {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
+ {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
+ {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
+ {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
+ {0}
};
/* Big-endian routines. *****************************************************/
@@ -658,53 +658,53 @@ static formatdef native_table[] = {
static PyObject *
bu_int(const char *p, const formatdef *f)
{
- long x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | *bytes++;
- } while (--i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG > f->size)
- x |= -(x & (1L << ((8 * f->size) - 1)));
- return PyLong_FromLong(x);
+ long x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | *bytes++;
+ } while (--i > 0);
+ /* Extend the sign bit. */
+ if (SIZEOF_LONG > f->size)
+ x |= -(x & (1L << ((8 * f->size) - 1)));
+ return PyLong_FromLong(x);
}
static PyObject *
bu_uint(const char *p, const formatdef *f)
{
- unsigned long x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | *bytes++;
- } while (--i > 0);
- if (x <= LONG_MAX)
- return PyLong_FromLong((long)x);
- return PyLong_FromUnsignedLong(x);
+ unsigned long x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | *bytes++;
+ } while (--i > 0);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong((long)x);
+ return PyLong_FromUnsignedLong(x);
}
static PyObject *
bu_longlong(const char *p, const formatdef *f)
{
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | *bytes++;
- } while (--i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG_LONG > f->size)
- x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
- if (x >= LONG_MIN && x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
- return PyLong_FromLongLong(x);
+ PY_LONG_LONG x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | *bytes++;
+ } while (--i > 0);
+ /* Extend the sign bit. */
+ if (SIZEOF_LONG_LONG > f->size)
+ x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
+ if (x >= LONG_MIN && x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+ return PyLong_FromLongLong(x);
#else
- return _PyLong_FromByteArray((const unsigned char *)p,
- 8,
- 0, /* little-endian */
- 1 /* signed */);
+ return _PyLong_FromByteArray((const unsigned char *)p,
+ 8,
+ 0, /* little-endian */
+ 1 /* signed */);
#endif
}
@@ -712,171 +712,171 @@ static PyObject *
bu_ulonglong(const char *p, const formatdef *f)
{
#ifdef HAVE_LONG_LONG
- unsigned PY_LONG_LONG x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | *bytes++;
- } while (--i > 0);
- if (x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
- return PyLong_FromUnsignedLongLong(x);
+ unsigned PY_LONG_LONG x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | *bytes++;
+ } while (--i > 0);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+ return PyLong_FromUnsignedLongLong(x);
#else
- return _PyLong_FromByteArray((const unsigned char *)p,
- 8,
- 0, /* little-endian */
- 0 /* signed */);
+ return _PyLong_FromByteArray((const unsigned char *)p,
+ 8,
+ 0, /* little-endian */
+ 0 /* signed */);
#endif
}
static PyObject *
bu_float(const char *p, const formatdef *f)
{
- return unpack_float(p, 0);
+ return unpack_float(p, 0);
}
static PyObject *
bu_double(const char *p, const formatdef *f)
{
- return unpack_double(p, 0);
+ return unpack_double(p, 0);
}
static PyObject *
bu_bool(const char *p, const formatdef *f)
{
- char x;
- memcpy((char *)&x, p, sizeof x);
- return PyBool_FromLong(x != 0);
+ char x;
+ memcpy((char *)&x, p, sizeof x);
+ return PyBool_FromLong(x != 0);
}
static int
bp_int(char *p, PyObject *v, const formatdef *f)
{
- long x;
- Py_ssize_t i;
- if (get_long(v, &x) < 0)
- return -1;
- i = f->size;
- if (i != SIZEOF_LONG) {
- if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(x, f, 0, 0xffffL);
+ long x;
+ Py_ssize_t i;
+ if (get_long(v, &x) < 0)
+ return -1;
+ i = f->size;
+ if (i != SIZEOF_LONG) {
+ if ((i == 2) && (x < -32768 || x > 32767))
+ RANGE_ERROR(x, f, 0, 0xffffL);
#if (SIZEOF_LONG != 4)
- else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(x, f, 0, 0xffffffffL);
+ else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
+ RANGE_ERROR(x, f, 0, 0xffffffffL);
#endif
- }
- do {
- p[--i] = (char)x;
- x >>= 8;
- } while (i > 0);
- return 0;
+ }
+ do {
+ p[--i] = (char)x;
+ x >>= 8;
+ } while (i > 0);
+ return 0;
}
static int
bp_uint(char *p, PyObject *v, const formatdef *f)
{
- unsigned long x;
- Py_ssize_t i;
- if (get_ulong(v, &x) < 0)
- return -1;
- i = f->size;
- if (i != SIZEOF_LONG) {
- unsigned long maxint = 1;
- maxint <<= (unsigned long)(i * 8);
- if (x >= maxint)
- RANGE_ERROR(x, f, 1, maxint - 1);
- }
- do {
- p[--i] = (char)x;
- x >>= 8;
- } while (i > 0);
- return 0;
+ unsigned long x;
+ Py_ssize_t i;
+ if (get_ulong(v, &x) < 0)
+ return -1;
+ i = f->size;
+ if (i != SIZEOF_LONG) {
+ unsigned long maxint = 1;
+ maxint <<= (unsigned long)(i * 8);
+ if (x >= maxint)
+ RANGE_ERROR(x, f, 1, maxint - 1);
+ }
+ do {
+ p[--i] = (char)x;
+ x >>= 8;
+ } while (i > 0);
+ return 0;
}
static int
bp_longlong(char *p, PyObject *v, const formatdef *f)
{
- int res;
- v = get_pylong(v);
- if (v == NULL)
- return -1;
- res = _PyLong_AsByteArray((PyLongObject *)v,
- (unsigned char *)p,
- 8,
- 0, /* little_endian */
- 1 /* signed */);
- Py_DECREF(v);
- return res;
+ int res;
+ v = get_pylong(v);
+ if (v == NULL)
+ return -1;
+ res = _PyLong_AsByteArray((PyLongObject *)v,
+ (unsigned char *)p,
+ 8,
+ 0, /* little_endian */
+ 1 /* signed */);
+ Py_DECREF(v);
+ return res;
}
static int
bp_ulonglong(char *p, PyObject *v, const formatdef *f)
{
- int res;
- v = get_pylong(v);
- if (v == NULL)
- return -1;
- res = _PyLong_AsByteArray((PyLongObject *)v,
- (unsigned char *)p,
- 8,
- 0, /* little_endian */
- 0 /* signed */);
- Py_DECREF(v);
- return res;
+ int res;
+ v = get_pylong(v);
+ if (v == NULL)
+ return -1;
+ res = _PyLong_AsByteArray((PyLongObject *)v,
+ (unsigned char *)p,
+ 8,
+ 0, /* little_endian */
+ 0 /* signed */);
+ Py_DECREF(v);
+ return res;
}
static int
bp_float(char *p, PyObject *v, const formatdef *f)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- return _PyFloat_Pack4(x, (unsigned char *)p, 0);
+ double x = PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ return _PyFloat_Pack4(x, (unsigned char *)p, 0);
}
static int
bp_double(char *p, PyObject *v, const formatdef *f)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- return _PyFloat_Pack8(x, (unsigned char *)p, 0);
+ double x = PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ return _PyFloat_Pack8(x, (unsigned char *)p, 0);
}
static int
bp_bool(char *p, PyObject *v, const formatdef *f)
{
- char y;
- y = PyObject_IsTrue(v);
- memcpy(p, (char *)&y, sizeof y);
- return 0;
+ char y;
+ y = PyObject_IsTrue(v);
+ memcpy(p, (char *)&y, sizeof y);
+ return 0;
}
static formatdef bigendian_table[] = {
- {'x', 1, 0, NULL},
- {'b', 1, 0, nu_byte, np_byte},
- {'B', 1, 0, nu_ubyte, np_ubyte},
- {'c', 1, 0, nu_char, np_char},
- {'s', 1, 0, NULL},
- {'p', 1, 0, NULL},
- {'h', 2, 0, bu_int, bp_int},
- {'H', 2, 0, bu_uint, bp_uint},
- {'i', 4, 0, bu_int, bp_int},
- {'I', 4, 0, bu_uint, bp_uint},
- {'l', 4, 0, bu_int, bp_int},
- {'L', 4, 0, bu_uint, bp_uint},
- {'q', 8, 0, bu_longlong, bp_longlong},
- {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
- {'?', 1, 0, bu_bool, bp_bool},
- {'f', 4, 0, bu_float, bp_float},
- {'d', 8, 0, bu_double, bp_double},
- {0}
+ {'x', 1, 0, NULL},
+ {'b', 1, 0, nu_byte, np_byte},
+ {'B', 1, 0, nu_ubyte, np_ubyte},
+ {'c', 1, 0, nu_char, np_char},
+ {'s', 1, 0, NULL},
+ {'p', 1, 0, NULL},
+ {'h', 2, 0, bu_int, bp_int},
+ {'H', 2, 0, bu_uint, bp_uint},
+ {'i', 4, 0, bu_int, bp_int},
+ {'I', 4, 0, bu_uint, bp_uint},
+ {'l', 4, 0, bu_int, bp_int},
+ {'L', 4, 0, bu_uint, bp_uint},
+ {'q', 8, 0, bu_longlong, bp_longlong},
+ {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
+ {'?', 1, 0, bu_bool, bp_bool},
+ {'f', 4, 0, bu_float, bp_float},
+ {'d', 8, 0, bu_double, bp_double},
+ {0}
};
/* Little-endian routines. *****************************************************/
@@ -884,53 +884,53 @@ static formatdef bigendian_table[] = {
static PyObject *
lu_int(const char *p, const formatdef *f)
{
- long x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | bytes[--i];
- } while (i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG > f->size)
- x |= -(x & (1L << ((8 * f->size) - 1)));
- return PyLong_FromLong(x);
+ long x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | bytes[--i];
+ } while (i > 0);
+ /* Extend the sign bit. */
+ if (SIZEOF_LONG > f->size)
+ x |= -(x & (1L << ((8 * f->size) - 1)));
+ return PyLong_FromLong(x);
}
static PyObject *
lu_uint(const char *p, const formatdef *f)
{
- unsigned long x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | bytes[--i];
- } while (i > 0);
- if (x <= LONG_MAX)
- return PyLong_FromLong((long)x);
- return PyLong_FromUnsignedLong((long)x);
+ unsigned long x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | bytes[--i];
+ } while (i > 0);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong((long)x);
+ return PyLong_FromUnsignedLong((long)x);
}
static PyObject *
lu_longlong(const char *p, const formatdef *f)
{
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | bytes[--i];
- } while (i > 0);
- /* Extend the sign bit. */
- if (SIZEOF_LONG_LONG > f->size)
- x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
- if (x >= LONG_MIN && x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
- return PyLong_FromLongLong(x);
+ PY_LONG_LONG x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | bytes[--i];
+ } while (i > 0);
+ /* Extend the sign bit. */
+ if (SIZEOF_LONG_LONG > f->size)
+ x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
+ if (x >= LONG_MIN && x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+ return PyLong_FromLongLong(x);
#else
- return _PyLong_FromByteArray((const unsigned char *)p,
- 8,
- 1, /* little-endian */
- 1 /* signed */);
+ return _PyLong_FromByteArray((const unsigned char *)p,
+ 8,
+ 1, /* little-endian */
+ 1 /* signed */);
#endif
}
@@ -938,182 +938,182 @@ static PyObject *
lu_ulonglong(const char *p, const formatdef *f)
{
#ifdef HAVE_LONG_LONG
- unsigned PY_LONG_LONG x = 0;
- Py_ssize_t i = f->size;
- const unsigned char *bytes = (const unsigned char *)p;
- do {
- x = (x<<8) | bytes[--i];
- } while (i > 0);
- if (x <= LONG_MAX)
- return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
- return PyLong_FromUnsignedLongLong(x);
+ unsigned PY_LONG_LONG x = 0;
+ Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
+ do {
+ x = (x<<8) | bytes[--i];
+ } while (i > 0);
+ if (x <= LONG_MAX)
+ return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+ return PyLong_FromUnsignedLongLong(x);
#else
- return _PyLong_FromByteArray((const unsigned char *)p,
- 8,
- 1, /* little-endian */
- 0 /* signed */);
+ return _PyLong_FromByteArray((const unsigned char *)p,
+ 8,
+ 1, /* little-endian */
+ 0 /* signed */);
#endif
}
static PyObject *
lu_float(const char *p, const formatdef *f)
{
- return unpack_float(p, 1);
+ return unpack_float(p, 1);
}
static PyObject *
lu_double(const char *p, const formatdef *f)
{
- return unpack_double(p, 1);
+ return unpack_double(p, 1);
}
static int
lp_int(char *p, PyObject *v, const formatdef *f)
{
- long x;
- Py_ssize_t i;
- if (get_long(v, &x) < 0)
- return -1;
- i = f->size;
- if (i != SIZEOF_LONG) {
- if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(x, f, 0, 0xffffL);
+ long x;
+ Py_ssize_t i;
+ if (get_long(v, &x) < 0)
+ return -1;
+ i = f->size;
+ if (i != SIZEOF_LONG) {
+ if ((i == 2) && (x < -32768 || x > 32767))
+ RANGE_ERROR(x, f, 0, 0xffffL);
#if (SIZEOF_LONG != 4)
- else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(x, f, 0, 0xffffffffL);
+ else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
+ RANGE_ERROR(x, f, 0, 0xffffffffL);
#endif
- }
- do {
- *p++ = (char)x;
- x >>= 8;
- } while (--i > 0);
- return 0;
+ }
+ do {
+ *p++ = (char)x;
+ x >>= 8;
+ } while (--i > 0);
+ return 0;
}
static int
lp_uint(char *p, PyObject *v, const formatdef *f)
{
- unsigned long x;
- Py_ssize_t i;
- if (get_ulong(v, &x) < 0)
- return -1;
- i = f->size;
- if (i != SIZEOF_LONG) {
- unsigned long maxint = 1;
- maxint <<= (unsigned long)(i * 8);
- if (x >= maxint)
- RANGE_ERROR(x, f, 1, maxint - 1);
- }
- do {
- *p++ = (char)x;
- x >>= 8;
- } while (--i > 0);
- return 0;
+ unsigned long x;
+ Py_ssize_t i;
+ if (get_ulong(v, &x) < 0)
+ return -1;
+ i = f->size;
+ if (i != SIZEOF_LONG) {
+ unsigned long maxint = 1;
+ maxint <<= (unsigned long)(i * 8);
+ if (x >= maxint)
+ RANGE_ERROR(x, f, 1, maxint - 1);
+ }
+ do {
+ *p++ = (char)x;
+ x >>= 8;
+ } while (--i > 0);
+ return 0;
}
static int
lp_longlong(char *p, PyObject *v, const formatdef *f)
{
- int res;
- v = get_pylong(v);
- if (v == NULL)
- return -1;
- res = _PyLong_AsByteArray((PyLongObject*)v,
- (unsigned char *)p,
- 8,
- 1, /* little_endian */
- 1 /* signed */);
- Py_DECREF(v);
- return res;
+ int res;
+ v = get_pylong(v);
+ if (v == NULL)
+ return -1;
+ res = _PyLong_AsByteArray((PyLongObject*)v,
+ (unsigned char *)p,
+ 8,
+ 1, /* little_endian */
+ 1 /* signed */);
+ Py_DECREF(v);
+ return res;
}
static int
lp_ulonglong(char *p, PyObject *v, const formatdef *f)
{
- int res;
- v = get_pylong(v);
- if (v == NULL)
- return -1;
- res = _PyLong_AsByteArray((PyLongObject*)v,
- (unsigned char *)p,
- 8,
- 1, /* little_endian */
- 0 /* signed */);
- Py_DECREF(v);
- return res;
+ int res;
+ v = get_pylong(v);
+ if (v == NULL)
+ return -1;
+ res = _PyLong_AsByteArray((PyLongObject*)v,
+ (unsigned char *)p,
+ 8,
+ 1, /* little_endian */
+ 0 /* signed */);
+ Py_DECREF(v);
+ return res;
}
static int
lp_float(char *p, PyObject *v, const formatdef *f)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- return _PyFloat_Pack4(x, (unsigned char *)p, 1);
+ double x = PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ return _PyFloat_Pack4(x, (unsigned char *)p, 1);
}
static int
lp_double(char *p, PyObject *v, const formatdef *f)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1 && PyErr_Occurred()) {
- PyErr_SetString(StructError,
- "required argument is not a float");
- return -1;
- }
- return _PyFloat_Pack8(x, (unsigned char *)p, 1);
+ double x = PyFloat_AsDouble(v);
+ if (x == -1 && PyErr_Occurred()) {
+ PyErr_SetString(StructError,
+ "required argument is not a float");
+ return -1;
+ }
+ return _PyFloat_Pack8(x, (unsigned char *)p, 1);
}
static formatdef lilendian_table[] = {
- {'x', 1, 0, NULL},
- {'b', 1, 0, nu_byte, np_byte},
- {'B', 1, 0, nu_ubyte, np_ubyte},
- {'c', 1, 0, nu_char, np_char},
- {'s', 1, 0, NULL},
- {'p', 1, 0, NULL},
- {'h', 2, 0, lu_int, lp_int},
- {'H', 2, 0, lu_uint, lp_uint},
- {'i', 4, 0, lu_int, lp_int},
- {'I', 4, 0, lu_uint, lp_uint},
- {'l', 4, 0, lu_int, lp_int},
- {'L', 4, 0, lu_uint, lp_uint},
- {'q', 8, 0, lu_longlong, lp_longlong},
- {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
- {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
- but potentially different from native rep -- reuse bx_bool funcs. */
- {'f', 4, 0, lu_float, lp_float},
- {'d', 8, 0, lu_double, lp_double},
- {0}
+ {'x', 1, 0, NULL},
+ {'b', 1, 0, nu_byte, np_byte},
+ {'B', 1, 0, nu_ubyte, np_ubyte},
+ {'c', 1, 0, nu_char, np_char},
+ {'s', 1, 0, NULL},
+ {'p', 1, 0, NULL},
+ {'h', 2, 0, lu_int, lp_int},
+ {'H', 2, 0, lu_uint, lp_uint},
+ {'i', 4, 0, lu_int, lp_int},
+ {'I', 4, 0, lu_uint, lp_uint},
+ {'l', 4, 0, lu_int, lp_int},
+ {'L', 4, 0, lu_uint, lp_uint},
+ {'q', 8, 0, lu_longlong, lp_longlong},
+ {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
+ {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
+ but potentially different from native rep -- reuse bx_bool funcs. */
+ {'f', 4, 0, lu_float, lp_float},
+ {'d', 8, 0, lu_double, lp_double},
+ {0}
};
static const formatdef *
whichtable(char **pfmt)
{
- const char *fmt = (*pfmt)++; /* May be backed out of later */
- switch (*fmt) {
- case '<':
- return lilendian_table;
- case '>':
- case '!': /* Network byte order is big-endian */
- return bigendian_table;
- case '=': { /* Host byte order -- different from native in aligment! */
- int n = 1;
- char *p = (char *) &n;
- if (*p == 1)
- return lilendian_table;
- else
- return bigendian_table;
- }
- default:
- --*pfmt; /* Back out of pointer increment */
- /* Fall through */
- case '@':
- return native_table;
- }
+ const char *fmt = (*pfmt)++; /* May be backed out of later */
+ switch (*fmt) {
+ case '<':
+ return lilendian_table;
+ case '>':
+ case '!': /* Network byte order is big-endian */
+ return bigendian_table;
+ case '=': { /* Host byte order -- different from native in aligment! */
+ int n = 1;
+ char *p = (char *) &n;
+ if (*p == 1)
+ return lilendian_table;
+ else
+ return bigendian_table;
+ }
+ default:
+ --*pfmt; /* Back out of pointer increment */
+ /* Fall through */
+ case '@':
+ return native_table;
+ }
}
@@ -1122,13 +1122,13 @@ whichtable(char **pfmt)
static const formatdef *
getentry(int c, const formatdef *f)
{
- for (; f->format != '\0'; f++) {
- if (f->format == c) {
- return f;
- }
- }
- PyErr_SetString(StructError, "bad char in struct format");
- return NULL;
+ for (; f->format != '\0'; f++) {
+ if (f->format == c) {
+ return f;
+ }
+ }
+ PyErr_SetString(StructError, "bad char in struct format");
+ return NULL;
}
@@ -1137,14 +1137,14 @@ getentry(int c, const formatdef *f)
static int
align(Py_ssize_t size, char c, const formatdef *e)
{
- if (e->format == c) {
- if (e->alignment) {
- size = ((size + e->alignment - 1)
- / e->alignment)
- * e->alignment;
- }
- }
- return size;
+ if (e->format == c) {
+ if (e->alignment) {
+ size = ((size + e->alignment - 1)
+ / e->alignment)
+ * e->alignment;
+ }
+ }
+ return size;
}
@@ -1153,224 +1153,224 @@ align(Py_ssize_t size, char c, const formatdef *e)
static int
prepare_s(PyStructObject *self)
{
- const formatdef *f;
- const formatdef *e;
- formatcode *codes;
-
- const char *s;
- const char *fmt;
- char c;
- Py_ssize_t size, len, num, itemsize, x;
-
- fmt = PyBytes_AS_STRING(self->s_format);
-
- f = whichtable((char **)&fmt);
-
- s = fmt;
- size = 0;
- len = 0;
- while ((c = *s++) != '\0') {
- if (isspace(Py_CHARMASK(c)))
- continue;
- if ('0' <= c && c <= '9') {
- num = c - '0';
- while ('0' <= (c = *s++) && c <= '9') {
- x = num*10 + (c - '0');
- if (x/10 != num) {
- PyErr_SetString(
- StructError,
- "overflow in item count");
- return -1;
- }
- num = x;
- }
- if (c == '\0')
- break;
- }
- else
- num = 1;
-
- e = getentry(c, f);
- if (e == NULL)
- return -1;
-
- switch (c) {
- case 's': /* fall through */
- case 'p': len++; break;
- case 'x': break;
- default: len += num; break;
- }
-
- itemsize = e->size;
- size = align(size, c, e);
- x = num * itemsize;
- size += x;
- if (x/itemsize != num || size < 0) {
- PyErr_SetString(StructError,
- "total struct size too long");
- return -1;
- }
- }
-
- /* check for overflow */
- if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
- PyErr_NoMemory();
- return -1;
- }
-
- self->s_size = size;
- self->s_len = len;
- codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
- if (codes == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- self->s_codes = codes;
-
- s = fmt;
- size = 0;
- while ((c = *s++) != '\0') {
- if (isspace(Py_CHARMASK(c)))
- continue;
- if ('0' <= c && c <= '9') {
- num = c - '0';
- while ('0' <= (c = *s++) && c <= '9')
- num = num*10 + (c - '0');
- if (c == '\0')
- break;
- }
- else
- num = 1;
-
- e = getentry(c, f);
-
- size = align(size, c, e);
- if (c == 's' || c == 'p') {
- codes->offset = size;
- codes->size = num;
- codes->fmtdef = e;
- codes++;
- size += num;
- } else if (c == 'x') {
- size += num;
- } else {
- while (--num >= 0) {
- codes->offset = size;
- codes->size = e->size;
- codes->fmtdef = e;
- codes++;
- size += e->size;
- }
- }
- }
- codes->fmtdef = NULL;
- codes->offset = size;
- codes->size = 0;
-
- return 0;
+ const formatdef *f;
+ const formatdef *e;
+ formatcode *codes;
+
+ const char *s;
+ const char *fmt;
+ char c;
+ Py_ssize_t size, len, num, itemsize, x;
+
+ fmt = PyBytes_AS_STRING(self->s_format);
+
+ f = whichtable((char **)&fmt);
+
+ s = fmt;
+ size = 0;
+ len = 0;
+ while ((c = *s++) != '\0') {
+ if (isspace(Py_CHARMASK(c)))
+ continue;
+ if ('0' <= c && c <= '9') {
+ num = c - '0';
+ while ('0' <= (c = *s++) && c <= '9') {
+ x = num*10 + (c - '0');
+ if (x/10 != num) {
+ PyErr_SetString(
+ StructError,
+ "overflow in item count");
+ return -1;
+ }
+ num = x;
+ }
+ if (c == '\0')
+ break;
+ }
+ else
+ num = 1;
+
+ e = getentry(c, f);
+ if (e == NULL)
+ return -1;
+
+ switch (c) {
+ case 's': /* fall through */
+ case 'p': len++; break;
+ case 'x': break;
+ default: len += num; break;
+ }
+
+ itemsize = e->size;
+ size = align(size, c, e);
+ x = num * itemsize;
+ size += x;
+ if (x/itemsize != num || size < 0) {
+ PyErr_SetString(StructError,
+ "total struct size too long");
+ return -1;
+ }
+ }
+
+ /* check for overflow */
+ if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ self->s_size = size;
+ self->s_len = len;
+ codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
+ if (codes == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ self->s_codes = codes;
+
+ s = fmt;
+ size = 0;
+ while ((c = *s++) != '\0') {
+ if (isspace(Py_CHARMASK(c)))
+ continue;
+ if ('0' <= c && c <= '9') {
+ num = c - '0';
+ while ('0' <= (c = *s++) && c <= '9')
+ num = num*10 + (c - '0');
+ if (c == '\0')
+ break;
+ }
+ else
+ num = 1;
+
+ e = getentry(c, f);
+
+ size = align(size, c, e);
+ if (c == 's' || c == 'p') {
+ codes->offset = size;
+ codes->size = num;
+ codes->fmtdef = e;
+ codes++;
+ size += num;
+ } else if (c == 'x') {
+ size += num;
+ } else {
+ while (--num >= 0) {
+ codes->offset = size;
+ codes->size = e->size;
+ codes->fmtdef = e;
+ codes++;
+ size += e->size;
+ }
+ }
+ }
+ codes->fmtdef = NULL;
+ codes->offset = size;
+ codes->size = 0;
+
+ return 0;
}
static PyObject *
s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *self;
+ PyObject *self;
- assert(type != NULL && type->tp_alloc != NULL);
+ assert(type != NULL && type->tp_alloc != NULL);
- self = type->tp_alloc(type, 0);
- if (self != NULL) {
- PyStructObject *s = (PyStructObject*)self;
- Py_INCREF(Py_None);
- s->s_format = Py_None;
- s->s_codes = NULL;
- s->s_size = -1;
- s->s_len = -1;
- }
- return self;
+ self = type->tp_alloc(type, 0);
+ if (self != NULL) {
+ PyStructObject *s = (PyStructObject*)self;
+ Py_INCREF(Py_None);
+ s->s_format = Py_None;
+ s->s_codes = NULL;
+ s->s_size = -1;
+ s->s_len = -1;
+ }
+ return self;
}
static int
s_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyStructObject *soself = (PyStructObject *)self;
- PyObject *o_format = NULL;
- int ret = 0;
- static char *kwlist[] = {"format", 0};
+ PyStructObject *soself = (PyStructObject *)self;
+ PyObject *o_format = NULL;
+ int ret = 0;
+ static char *kwlist[] = {"format", 0};
- assert(PyStruct_Check(self));
+ assert(PyStruct_Check(self));
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
- &o_format))
- return -1;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist,
+ &o_format))
+ return -1;
- if (PyUnicode_Check(o_format)) {
- o_format = PyUnicode_AsASCIIString(o_format);
- if (o_format == NULL)
- return -1;
- }
- /* XXX support buffer interface, too */
- else {
- Py_INCREF(o_format);
- }
+ if (PyUnicode_Check(o_format)) {
+ o_format = PyUnicode_AsASCIIString(o_format);
+ if (o_format == NULL)
+ return -1;
+ }
+ /* XXX support buffer interface, too */
+ else {
+ Py_INCREF(o_format);
+ }
- if (!PyBytes_Check(o_format)) {
- Py_DECREF(o_format);
- PyErr_Format(PyExc_TypeError,
- "Struct() argument 1 must be bytes, not %.200s",
- Py_TYPE(o_format)->tp_name);
- return -1;
- }
+ if (!PyBytes_Check(o_format)) {
+ Py_DECREF(o_format);
+ PyErr_Format(PyExc_TypeError,
+ "Struct() argument 1 must be bytes, not %.200s",
+ Py_TYPE(o_format)->tp_name);
+ return -1;
+ }
- Py_CLEAR(soself->s_format);
- soself->s_format = o_format;
+ Py_CLEAR(soself->s_format);
+ soself->s_format = o_format;
- ret = prepare_s(soself);
- return ret;
+ ret = prepare_s(soself);
+ return ret;
}
static void
s_dealloc(PyStructObject *s)
{
- if (s->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *)s);
- if (s->s_codes != NULL) {
- PyMem_FREE(s->s_codes);
- }
- Py_XDECREF(s->s_format);
- Py_TYPE(s)->tp_free((PyObject *)s);
+ if (s->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)s);
+ if (s->s_codes != NULL) {
+ PyMem_FREE(s->s_codes);
+ }
+ Py_XDECREF(s->s_format);
+ Py_TYPE(s)->tp_free((PyObject *)s);
}
static PyObject *
s_unpack_internal(PyStructObject *soself, char *startfrom) {
- formatcode *code;
- Py_ssize_t i = 0;
- PyObject *result = PyTuple_New(soself->s_len);
- if (result == NULL)
- return NULL;
-
- for (code = soself->s_codes; code->fmtdef != NULL; code++) {
- PyObject *v;
- const formatdef *e = code->fmtdef;
- const char *res = startfrom + code->offset;
- if (e->format == 's') {
- v = PyBytes_FromStringAndSize(res, code->size);
- } else if (e->format == 'p') {
- Py_ssize_t n = *(unsigned char*)res;
- if (n >= code->size)
- n = code->size - 1;
- v = PyBytes_FromStringAndSize(res + 1, n);
- } else {
- v = e->unpack(res, e);
- }
- if (v == NULL)
- goto fail;
- PyTuple_SET_ITEM(result, i++, v);
- }
-
- return result;
+ formatcode *code;
+ Py_ssize_t i = 0;
+ PyObject *result = PyTuple_New(soself->s_len);
+ if (result == NULL)
+ return NULL;
+
+ for (code = soself->s_codes; code->fmtdef != NULL; code++) {
+ PyObject *v;
+ const formatdef *e = code->fmtdef;
+ const char *res = startfrom + code->offset;
+ if (e->format == 's') {
+ v = PyBytes_FromStringAndSize(res, code->size);
+ } else if (e->format == 'p') {
+ Py_ssize_t n = *(unsigned char*)res;
+ if (n >= code->size)
+ n = code->size - 1;
+ v = PyBytes_FromStringAndSize(res + 1, n);
+ } else {
+ v = e->unpack(res, e);
+ }
+ if (v == NULL)
+ goto fail;
+ PyTuple_SET_ITEM(result, i++, v);
+ }
+
+ return result;
fail:
- Py_DECREF(result);
- return NULL;
+ Py_DECREF(result);
+ return NULL;
}
@@ -1384,24 +1384,24 @@ strings.");
static PyObject *
s_unpack(PyObject *self, PyObject *input)
{
- Py_buffer vbuf;
- PyObject *result;
- PyStructObject *soself = (PyStructObject *)self;
-
- assert(PyStruct_Check(self));
- assert(soself->s_codes != NULL);
- if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
- return NULL;
- if (vbuf.len != soself->s_size) {
- PyErr_Format(StructError,
- "unpack requires a bytes argument of length %zd",
- soself->s_size);
- PyBuffer_Release(&vbuf);
- return NULL;
- }
- result = s_unpack_internal(soself, vbuf.buf);
- PyBuffer_Release(&vbuf);
- return result;
+ Py_buffer vbuf;
+ PyObject *result;
+ PyStructObject *soself = (PyStructObject *)self;
+
+ assert(PyStruct_Check(self));
+ assert(soself->s_codes != NULL);
+ if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
+ return NULL;
+ if (vbuf.len != soself->s_size) {
+ PyErr_Format(StructError,
+ "unpack requires a bytes argument of length %zd",
+ soself->s_size);
+ PyBuffer_Release(&vbuf);
+ return NULL;
+ }
+ result = s_unpack_internal(soself, vbuf.buf);
+ PyBuffer_Release(&vbuf);
+ return result;
}
PyDoc_STRVAR(s_unpack_from__doc__,
@@ -1415,35 +1415,35 @@ See struct.__doc__ for more on format strings.");
static PyObject *
s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"buffer", "offset", 0};
-
- PyObject *input;
- Py_ssize_t offset = 0;
- Py_buffer vbuf;
- PyObject *result;
- PyStructObject *soself = (PyStructObject *)self;
-
- assert(PyStruct_Check(self));
- assert(soself->s_codes != NULL);
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds,
- "O|n:unpack_from", kwlist,
- &input, &offset))
- return NULL;
- if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
- return NULL;
- if (offset < 0)
- offset += vbuf.len;
- if (offset < 0 || vbuf.len - offset < soself->s_size) {
- PyErr_Format(StructError,
- "unpack_from requires a buffer of at least %zd bytes",
- soself->s_size);
- PyBuffer_Release(&vbuf);
- return NULL;
- }
- result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
- PyBuffer_Release(&vbuf);
- return result;
+ static char *kwlist[] = {"buffer", "offset", 0};
+
+ PyObject *input;
+ Py_ssize_t offset = 0;
+ Py_buffer vbuf;
+ PyObject *result;
+ PyStructObject *soself = (PyStructObject *)self;
+
+ assert(PyStruct_Check(self));
+ assert(soself->s_codes != NULL);
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds,
+ "O|n:unpack_from", kwlist,
+ &input, &offset))
+ return NULL;
+ if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
+ return NULL;
+ if (offset < 0)
+ offset += vbuf.len;
+ if (offset < 0 || vbuf.len - offset < soself->s_size) {
+ PyErr_Format(StructError,
+ "unpack_from requires a buffer of at least %zd bytes",
+ soself->s_size);
+ PyBuffer_Release(&vbuf);
+ return NULL;
+ }
+ result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
+ PyBuffer_Release(&vbuf);
+ return result;
}
@@ -1460,85 +1460,85 @@ s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
static int
s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
{
- formatcode *code;
- /* XXX(nnorwitz): why does i need to be a local? can we use
- the offset parameter or do we need the wider width? */
- Py_ssize_t i;
-
- memset(buf, '\0', soself->s_size);
- i = offset;
- for (code = soself->s_codes; code->fmtdef != NULL; code++) {
- Py_ssize_t n;
- PyObject *v = PyTuple_GET_ITEM(args, i++);
- const formatdef *e = code->fmtdef;
- char *res = buf + code->offset;
- if (e->format == 's') {
- int isstring;
- void *p;
- if (PyUnicode_Check(v)) {
- v = _PyUnicode_AsDefaultEncodedString(v, NULL);
- if (v == NULL)
- return -1;
- }
- isstring = PyBytes_Check(v);
- if (!isstring && !PyByteArray_Check(v)) {
- PyErr_SetString(StructError,
- "argument for 's' must be a bytes or string");
- return -1;
- }
- if (isstring) {
- n = PyBytes_GET_SIZE(v);
- p = PyBytes_AS_STRING(v);
- }
- else {
- n = PyByteArray_GET_SIZE(v);
- p = PyByteArray_AS_STRING(v);
- }
- if (n > code->size)
- n = code->size;
- if (n > 0)
- memcpy(res, p, n);
- } else if (e->format == 'p') {
- int isstring;
- void *p;
- if (PyUnicode_Check(v)) {
- v = _PyUnicode_AsDefaultEncodedString(v, NULL);
- if (v == NULL)
- return -1;
- }
- isstring = PyBytes_Check(v);
- if (!isstring && !PyByteArray_Check(v)) {
- PyErr_SetString(StructError,
- "argument for 'p' must be a bytes or string");
- return -1;
- }
- if (isstring) {
- n = PyBytes_GET_SIZE(v);
- p = PyBytes_AS_STRING(v);
- }
- else {
- n = PyByteArray_GET_SIZE(v);
- p = PyByteArray_AS_STRING(v);
- }
- if (n > (code->size - 1))
- n = code->size - 1;
- if (n > 0)
- memcpy(res + 1, p, n);
- if (n > 255)
- n = 255;
- *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
- } else {
- if (e->pack(res, v, e) < 0) {
- if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(StructError,
- "long too large to convert to int");
- return -1;
- }
- }
- }
-
- /* Success */
- return 0;
+ formatcode *code;
+ /* XXX(nnorwitz): why does i need to be a local? can we use
+ the offset parameter or do we need the wider width? */
+ Py_ssize_t i;
+
+ memset(buf, '\0', soself->s_size);
+ i = offset;
+ for (code = soself->s_codes; code->fmtdef != NULL; code++) {
+ Py_ssize_t n;
+ PyObject *v = PyTuple_GET_ITEM(args, i++);
+ const formatdef *e = code->fmtdef;
+ char *res = buf + code->offset;
+ if (e->format == 's') {
+ int isstring;
+ void *p;
+ if (PyUnicode_Check(v)) {
+ v = _PyUnicode_AsDefaultEncodedString(v, NULL);
+ if (v == NULL)
+ return -1;
+ }
+ isstring = PyBytes_Check(v);
+ if (!isstring && !PyByteArray_Check(v)) {
+ PyErr_SetString(StructError,
+ "argument for 's' must be a bytes or string");
+ return -1;
+ }
+ if (isstring) {
+ n = PyBytes_GET_SIZE(v);
+ p = PyBytes_AS_STRING(v);
+ }
+ else {
+ n = PyByteArray_GET_SIZE(v);
+ p = PyByteArray_AS_STRING(v);
+ }
+ if (n > code->size)
+ n = code->size;
+ if (n > 0)
+ memcpy(res, p, n);
+ } else if (e->format == 'p') {
+ int isstring;
+ void *p;
+ if (PyUnicode_Check(v)) {
+ v = _PyUnicode_AsDefaultEncodedString(v, NULL);
+ if (v == NULL)
+ return -1;
+ }
+ isstring = PyBytes_Check(v);
+ if (!isstring && !PyByteArray_Check(v)) {
+ PyErr_SetString(StructError,
+ "argument for 'p' must be a bytes or string");
+ return -1;
+ }
+ if (isstring) {
+ n = PyBytes_GET_SIZE(v);
+ p = PyBytes_AS_STRING(v);
+ }
+ else {
+ n = PyByteArray_GET_SIZE(v);
+ p = PyByteArray_AS_STRING(v);
+ }
+ if (n > (code->size - 1))
+ n = code->size - 1;
+ if (n > 0)
+ memcpy(res + 1, p, n);
+ if (n > 255)
+ n = 255;
+ *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
+ } else {
+ if (e->pack(res, v, e) < 0) {
+ if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_SetString(StructError,
+ "long too large to convert to int");
+ return -1;
+ }
+ }
+ }
+
+ /* Success */
+ return 0;
}
@@ -1551,32 +1551,32 @@ Struct's format. See struct.__doc__ for more on format strings.");
static PyObject *
s_pack(PyObject *self, PyObject *args)
{
- PyStructObject *soself;
- PyObject *result;
+ PyStructObject *soself;
+ PyObject *result;
- /* Validate arguments. */
- soself = (PyStructObject *)self;
- assert(PyStruct_Check(self));
- assert(soself->s_codes != NULL);
- if (PyTuple_GET_SIZE(args) != soself->s_len)
- {
- PyErr_Format(StructError,
- "pack requires exactly %zd arguments", soself->s_len);
- return NULL;
- }
+ /* Validate arguments. */
+ soself = (PyStructObject *)self;
+ assert(PyStruct_Check(self));
+ assert(soself->s_codes != NULL);
+ if (PyTuple_GET_SIZE(args) != soself->s_len)
+ {
+ PyErr_Format(StructError,
+ "pack requires exactly %zd arguments", soself->s_len);
+ return NULL;
+ }
- /* Allocate a new string */
- result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
- if (result == NULL)
- return NULL;
+ /* Allocate a new string */
+ result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
+ if (result == NULL)
+ return NULL;
- /* Call the guts */
- if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
- Py_DECREF(result);
- return NULL;
- }
+ /* Call the guts */
+ if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
+ Py_DECREF(result);
+ return NULL;
+ }
- return result;
+ return result;
}
PyDoc_STRVAR(s_pack_into__doc__,
@@ -1590,59 +1590,59 @@ more on format strings.");
static PyObject *
s_pack_into(PyObject *self, PyObject *args)
{
- PyStructObject *soself;
- char *buffer;
- Py_ssize_t buffer_len, offset;
-
- /* Validate arguments. +1 is for the first arg as buffer. */
- soself = (PyStructObject *)self;
- assert(PyStruct_Check(self));
- assert(soself->s_codes != NULL);
- if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
- {
- PyErr_Format(StructError,
- "pack_into requires exactly %zd arguments",
- (soself->s_len + 2));
- return NULL;
- }
-
- /* Extract a writable memory buffer from the first argument */
- if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
- (void**)&buffer, &buffer_len) == -1 ) {
- return NULL;
- }
- assert( buffer_len >= 0 );
-
- /* Extract the offset from the first argument */
- offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
- if (offset == -1 && PyErr_Occurred())
- return NULL;
-
- /* Support negative offsets. */
- if (offset < 0)
- offset += buffer_len;
-
- /* Check boundaries */
- if (offset < 0 || (buffer_len - offset) < soself->s_size) {
- PyErr_Format(StructError,
- "pack_into requires a buffer of at least %zd bytes",
- soself->s_size);
- return NULL;
- }
-
- /* Call the guts */
- if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
- return NULL;
- }
-
- Py_RETURN_NONE;
+ PyStructObject *soself;
+ char *buffer;
+ Py_ssize_t buffer_len, offset;
+
+ /* Validate arguments. +1 is for the first arg as buffer. */
+ soself = (PyStructObject *)self;
+ assert(PyStruct_Check(self));
+ assert(soself->s_codes != NULL);
+ if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
+ {
+ PyErr_Format(StructError,
+ "pack_into requires exactly %zd arguments",
+ (soself->s_len + 2));
+ return NULL;
+ }
+
+ /* Extract a writable memory buffer from the first argument */
+ if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
+ (void**)&buffer, &buffer_len) == -1 ) {
+ return NULL;
+ }
+ assert( buffer_len >= 0 );
+
+ /* Extract the offset from the first argument */
+ offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
+ if (offset == -1 && PyErr_Occurred())
+ return NULL;
+
+ /* Support negative offsets. */
+ if (offset < 0)
+ offset += buffer_len;
+
+ /* Check boundaries */
+ if (offset < 0 || (buffer_len - offset) < soself->s_size) {
+ PyErr_Format(StructError,
+ "pack_into requires a buffer of at least %zd bytes",
+ soself->s_size);
+ return NULL;
+ }
+
+ /* Call the guts */
+ if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
}
static PyObject *
s_get_format(PyStructObject *self, void *unused)
{
- Py_INCREF(self->s_format);
- return self->s_format;
+ Py_INCREF(self->s_format);
+ return self->s_format;
}
static PyObject *
@@ -1654,12 +1654,12 @@ s_get_size(PyStructObject *self, void *unused)
/* List of functions */
static struct PyMethodDef s_methods[] = {
- {"pack", s_pack, METH_VARARGS, s_pack__doc__},
- {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
- {"unpack", s_unpack, METH_O, s_unpack__doc__},
- {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
- s_unpack_from__doc__},
- {NULL, NULL} /* sentinel */
+ {"pack", s_pack, METH_VARARGS, s_pack__doc__},
+ {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
+ {"unpack", s_unpack, METH_O, s_unpack__doc__},
+ {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
+ s_unpack_from__doc__},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(s__doc__, "Compiled struct object");
@@ -1667,52 +1667,52 @@ PyDoc_STRVAR(s__doc__, "Compiled struct object");
#define OFF(x) offsetof(PyStructObject, x)
static PyGetSetDef s_getsetlist[] = {
- {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
- {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
- {NULL} /* sentinel */
+ {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
+ {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
+ {NULL} /* sentinel */
};
static
PyTypeObject PyStructType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "Struct",
- sizeof(PyStructObject),
- 0,
- (destructor)s_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- s__doc__, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- s_methods, /* tp_methods */
- NULL, /* tp_members */
- s_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- s_init, /* tp_init */
- PyType_GenericAlloc,/* tp_alloc */
- s_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "Struct",
+ sizeof(PyStructObject),
+ 0,
+ (destructor)s_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ s__doc__, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ s_methods, /* tp_methods */
+ NULL, /* tp_members */
+ s_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ s_init, /* tp_init */
+ PyType_GenericAlloc,/* tp_alloc */
+ s_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
@@ -1724,29 +1724,29 @@ static PyObject *cache = NULL;
static PyObject *
cache_struct(PyObject *fmt)
{
- PyObject * s_object;
+ PyObject * s_object;
- if (cache == NULL) {
- cache = PyDict_New();
- if (cache == NULL)
- return NULL;
- }
+ if (cache == NULL) {
+ cache = PyDict_New();
+ if (cache == NULL)
+ return NULL;
+ }
- s_object = PyDict_GetItem(cache, fmt);
- if (s_object != NULL) {
- Py_INCREF(s_object);
- return s_object;
- }
+ s_object = PyDict_GetItem(cache, fmt);
+ if (s_object != NULL) {
+ Py_INCREF(s_object);
+ return s_object;
+ }
- s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
- if (s_object != NULL) {
- if (PyDict_Size(cache) >= MAXCACHE)
- PyDict_Clear(cache);
- /* Attempt to cache the result */
- if (PyDict_SetItem(cache, fmt, s_object) == -1)
- PyErr_Clear();
- }
- return s_object;
+ s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
+ if (s_object != NULL) {
+ if (PyDict_Size(cache) >= MAXCACHE)
+ PyDict_Clear(cache);
+ /* Attempt to cache the result */
+ if (PyDict_SetItem(cache, fmt, s_object) == -1)
+ PyErr_Clear();
+ }
+ return s_object;
}
PyDoc_STRVAR(clearcache_doc,
@@ -1755,8 +1755,8 @@ PyDoc_STRVAR(clearcache_doc,
static PyObject *
clearcache(PyObject *self)
{
- Py_CLEAR(cache);
- Py_RETURN_NONE;
+ Py_CLEAR(cache);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(calcsize_doc,
@@ -1765,13 +1765,13 @@ PyDoc_STRVAR(calcsize_doc,
static PyObject *
calcsize(PyObject *self, PyObject *fmt)
{
- Py_ssize_t n;
- PyObject *s_object = cache_struct(fmt);
- if (s_object == NULL)
- return NULL;
- n = ((PyStructObject *)s_object)->s_size;
- Py_DECREF(s_object);
- return PyLong_FromSsize_t(n);
+ Py_ssize_t n;
+ PyObject *s_object = cache_struct(fmt);
+ if (s_object == NULL)
+ return NULL;
+ n = ((PyStructObject *)s_object)->s_size;
+ Py_DECREF(s_object);
+ return PyLong_FromSsize_t(n);
}
PyDoc_STRVAR(pack_doc,
@@ -1780,27 +1780,27 @@ PyDoc_STRVAR(pack_doc,
static PyObject *
pack(PyObject *self, PyObject *args)
{
- PyObject *s_object, *fmt, *newargs, *result;
- Py_ssize_t n = PyTuple_GET_SIZE(args);
-
- if (n == 0) {
- PyErr_SetString(PyExc_TypeError, "missing format argument");
- return NULL;
- }
- fmt = PyTuple_GET_ITEM(args, 0);
- newargs = PyTuple_GetSlice(args, 1, n);
- if (newargs == NULL)
- return NULL;
-
- s_object = cache_struct(fmt);
- if (s_object == NULL) {
- Py_DECREF(newargs);
- return NULL;
- }
- result = s_pack(s_object, newargs);
- Py_DECREF(newargs);
- Py_DECREF(s_object);
- return result;
+ PyObject *s_object, *fmt, *newargs, *result;
+ Py_ssize_t n = PyTuple_GET_SIZE(args);
+
+ if (n == 0) {
+ PyErr_SetString(PyExc_TypeError, "missing format argument");
+ return NULL;
+ }
+ fmt = PyTuple_GET_ITEM(args, 0);
+ newargs = PyTuple_GetSlice(args, 1, n);
+ if (newargs == NULL)
+ return NULL;
+
+ s_object = cache_struct(fmt);
+ if (s_object == NULL) {
+ Py_DECREF(newargs);
+ return NULL;
+ }
+ result = s_pack(s_object, newargs);
+ Py_DECREF(newargs);
+ Py_DECREF(s_object);
+ return result;
}
PyDoc_STRVAR(pack_into_doc,
@@ -1810,27 +1810,27 @@ Write the packed bytes into the writable buffer buf starting at offset.");
static PyObject *
pack_into(PyObject *self, PyObject *args)
{
- PyObject *s_object, *fmt, *newargs, *result;
- Py_ssize_t n = PyTuple_GET_SIZE(args);
-
- if (n == 0) {
- PyErr_SetString(PyExc_TypeError, "missing format argument");
- return NULL;
- }
- fmt = PyTuple_GET_ITEM(args, 0);
- newargs = PyTuple_GetSlice(args, 1, n);
- if (newargs == NULL)
- return NULL;
-
- s_object = cache_struct(fmt);
- if (s_object == NULL) {
- Py_DECREF(newargs);
- return NULL;
- }
- result = s_pack_into(s_object, newargs);
- Py_DECREF(newargs);
- Py_DECREF(s_object);
- return result;
+ PyObject *s_object, *fmt, *newargs, *result;
+ Py_ssize_t n = PyTuple_GET_SIZE(args);
+
+ if (n == 0) {
+ PyErr_SetString(PyExc_TypeError, "missing format argument");
+ return NULL;
+ }
+ fmt = PyTuple_GET_ITEM(args, 0);
+ newargs = PyTuple_GetSlice(args, 1, n);
+ if (newargs == NULL)
+ return NULL;
+
+ s_object = cache_struct(fmt);
+ if (s_object == NULL) {
+ Py_DECREF(newargs);
+ return NULL;
+ }
+ result = s_pack_into(s_object, newargs);
+ Py_DECREF(newargs);
+ Py_DECREF(s_object);
+ return result;
}
PyDoc_STRVAR(unpack_doc,
@@ -1840,17 +1840,17 @@ Requires len(bytes) == calcsize(fmt).");
static PyObject *
unpack(PyObject *self, PyObject *args)
{
- PyObject *s_object, *fmt, *inputstr, *result;
+ PyObject *s_object, *fmt, *inputstr, *result;
- if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
+ return NULL;
- s_object = cache_struct(fmt);
- if (s_object == NULL)
- return NULL;
- result = s_unpack(s_object, inputstr);
- Py_DECREF(s_object);
- return result;
+ s_object = cache_struct(fmt);
+ if (s_object == NULL)
+ return NULL;
+ result = s_unpack(s_object, inputstr);
+ Py_DECREF(s_object);
+ return result;
}
PyDoc_STRVAR(unpack_from_doc,
@@ -1860,38 +1860,38 @@ fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
static PyObject *
unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *s_object, *fmt, *newargs, *result;
- Py_ssize_t n = PyTuple_GET_SIZE(args);
-
- if (n == 0) {
- PyErr_SetString(PyExc_TypeError, "missing format argument");
- return NULL;
- }
- fmt = PyTuple_GET_ITEM(args, 0);
- newargs = PyTuple_GetSlice(args, 1, n);
- if (newargs == NULL)
- return NULL;
-
- s_object = cache_struct(fmt);
- if (s_object == NULL) {
- Py_DECREF(newargs);
- return NULL;
- }
- result = s_unpack_from(s_object, newargs, kwds);
- Py_DECREF(newargs);
- Py_DECREF(s_object);
- return result;
+ PyObject *s_object, *fmt, *newargs, *result;
+ Py_ssize_t n = PyTuple_GET_SIZE(args);
+
+ if (n == 0) {
+ PyErr_SetString(PyExc_TypeError, "missing format argument");
+ return NULL;
+ }
+ fmt = PyTuple_GET_ITEM(args, 0);
+ newargs = PyTuple_GetSlice(args, 1, n);
+ if (newargs == NULL)
+ return NULL;
+
+ s_object = cache_struct(fmt);
+ if (s_object == NULL) {
+ Py_DECREF(newargs);
+ return NULL;
+ }
+ result = s_unpack_from(s_object, newargs, kwds);
+ Py_DECREF(newargs);
+ Py_DECREF(s_object);
+ return result;
}
static struct PyMethodDef module_functions[] = {
- {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
- {"calcsize", calcsize, METH_O, calcsize_doc},
- {"pack", pack, METH_VARARGS, pack_doc},
- {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
- {"unpack", unpack, METH_VARARGS, unpack_doc},
- {"unpack_from", (PyCFunction)unpack_from,
- METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
- {NULL, NULL} /* sentinel */
+ {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
+ {"calcsize", calcsize, METH_O, calcsize_doc},
+ {"pack", pack, METH_VARARGS, pack_doc},
+ {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
+ {"unpack", unpack, METH_VARARGS, unpack_doc},
+ {"unpack_from", (PyCFunction)unpack_from,
+ METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -1928,87 +1928,87 @@ The variable struct.error is an exception raised on errors.\n");
static struct PyModuleDef _structmodule = {
- PyModuleDef_HEAD_INIT,
- "_struct",
- module_doc,
- -1,
- module_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_struct",
+ module_doc,
+ -1,
+ module_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__struct(void)
{
- PyObject *ver, *m;
-
- ver = PyBytes_FromString("0.3");
- if (ver == NULL)
- return NULL;
-
- m = PyModule_Create(&_structmodule);
- if (m == NULL)
- return NULL;
-
- Py_TYPE(&PyStructType) = &PyType_Type;
- if (PyType_Ready(&PyStructType) < 0)
- return NULL;
-
- /* Check endian and swap in faster functions */
- {
- int one = 1;
- formatdef *native = native_table;
- formatdef *other, *ptr;
- if ((int)*(unsigned char*)&one)
- other = lilendian_table;
- else
- other = bigendian_table;
- /* Scan through the native table, find a matching
- entry in the endian table and swap in the
- native implementations whenever possible
- (64-bit platforms may not have "standard" sizes) */
- while (native->format != '\0' && other->format != '\0') {
- ptr = other;
- while (ptr->format != '\0') {
- if (ptr->format == native->format) {
- /* Match faster when formats are
- listed in the same order */
- if (ptr == other)
- other++;
- /* Only use the trick if the
- size matches */
- if (ptr->size != native->size)
- break;
- /* Skip float and double, could be
- "unknown" float format */
- if (ptr->format == 'd' || ptr->format == 'f')
- break;
- ptr->pack = native->pack;
- ptr->unpack = native->unpack;
- break;
- }
- ptr++;
- }
- native++;
- }
- }
-
- /* Add some symbolic constants to the module */
- if (StructError == NULL) {
- StructError = PyErr_NewException("struct.error", NULL, NULL);
- if (StructError == NULL)
- return NULL;
- }
-
- Py_INCREF(StructError);
- PyModule_AddObject(m, "error", StructError);
-
- Py_INCREF((PyObject*)&PyStructType);
- PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
-
- PyModule_AddObject(m, "__version__", ver);
-
- return m;
+ PyObject *ver, *m;
+
+ ver = PyBytes_FromString("0.3");
+ if (ver == NULL)
+ return NULL;
+
+ m = PyModule_Create(&_structmodule);
+ if (m == NULL)
+ return NULL;
+
+ Py_TYPE(&PyStructType) = &PyType_Type;
+ if (PyType_Ready(&PyStructType) < 0)
+ return NULL;
+
+ /* Check endian and swap in faster functions */
+ {
+ int one = 1;
+ formatdef *native = native_table;
+ formatdef *other, *ptr;
+ if ((int)*(unsigned char*)&one)
+ other = lilendian_table;
+ else
+ other = bigendian_table;
+ /* Scan through the native table, find a matching
+ entry in the endian table and swap in the
+ native implementations whenever possible
+ (64-bit platforms may not have "standard" sizes) */
+ while (native->format != '\0' && other->format != '\0') {
+ ptr = other;
+ while (ptr->format != '\0') {
+ if (ptr->format == native->format) {
+ /* Match faster when formats are
+ listed in the same order */
+ if (ptr == other)
+ other++;
+ /* Only use the trick if the
+ size matches */
+ if (ptr->size != native->size)
+ break;
+ /* Skip float and double, could be
+ "unknown" float format */
+ if (ptr->format == 'd' || ptr->format == 'f')
+ break;
+ ptr->pack = native->pack;
+ ptr->unpack = native->unpack;
+ break;
+ }
+ ptr++;
+ }
+ native++;
+ }
+ }
+
+ /* Add some symbolic constants to the module */
+ if (StructError == NULL) {
+ StructError = PyErr_NewException("struct.error", NULL, NULL);
+ if (StructError == NULL)
+ return NULL;
+ }
+
+ Py_INCREF(StructError);
+ PyModule_AddObject(m, "error", StructError);
+
+ Py_INCREF((PyObject*)&PyStructType);
+ PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
+
+ PyModule_AddObject(m, "__version__", ver);
+
+ return m;
}
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index ab28e9d..3e356d5 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -14,22 +14,22 @@
#ifdef WITH_THREAD
#include "pythread.h"
#endif /* WITH_THREAD */
-static PyObject *TestError; /* set to exception object in init */
+static PyObject *TestError; /* set to exception object in init */
/* Raise TestError with test_name + ": " + msg, and return NULL. */
static PyObject *
raiseTestError(const char* test_name, const char* msg)
{
- char buf[2048];
-
- if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
- PyErr_SetString(TestError, "internal error msg too large");
- else {
- PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
- PyErr_SetString(TestError, buf);
- }
- return NULL;
+ char buf[2048];
+
+ if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
+ PyErr_SetString(TestError, "internal error msg too large");
+ else {
+ PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
+ PyErr_SetString(TestError, buf);
+ }
+ return NULL;
}
/* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
@@ -40,138 +40,138 @@ raiseTestError(const char* test_name, const char* msg)
*/
static PyObject*
sizeof_error(const char* fatname, const char* typname,
- int expected, int got)
+ int expected, int got)
{
- char buf[1024];
- PyOS_snprintf(buf, sizeof(buf),
- "%.200s #define == %d but sizeof(%.200s) == %d",
- fatname, expected, typname, got);
- PyErr_SetString(TestError, buf);
- return (PyObject*)NULL;
+ char buf[1024];
+ PyOS_snprintf(buf, sizeof(buf),
+ "%.200s #define == %d but sizeof(%.200s) == %d",
+ fatname, expected, typname, got);
+ PyErr_SetString(TestError, buf);
+ return (PyObject*)NULL;
}
static PyObject*
test_config(PyObject *self)
{
#define CHECK_SIZEOF(FATNAME, TYPE) \
- if (FATNAME != sizeof(TYPE)) \
- return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
-
- CHECK_SIZEOF(SIZEOF_SHORT, short);
- CHECK_SIZEOF(SIZEOF_INT, int);
- CHECK_SIZEOF(SIZEOF_LONG, long);
- CHECK_SIZEOF(SIZEOF_VOID_P, void*);
- CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
+ if (FATNAME != sizeof(TYPE)) \
+ return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
+
+ CHECK_SIZEOF(SIZEOF_SHORT, short);
+ CHECK_SIZEOF(SIZEOF_INT, int);
+ CHECK_SIZEOF(SIZEOF_LONG, long);
+ CHECK_SIZEOF(SIZEOF_VOID_P, void*);
+ CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
#ifdef HAVE_LONG_LONG
- CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
+ CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
#endif
#undef CHECK_SIZEOF
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject*
test_list_api(PyObject *self)
{
- PyObject* list;
- int i;
+ PyObject* list;
+ int i;
- /* SF bug 132008: PyList_Reverse segfaults */
+ /* SF bug 132008: PyList_Reverse segfaults */
#define NLIST 30
- list = PyList_New(NLIST);
- if (list == (PyObject*)NULL)
- return (PyObject*)NULL;
- /* list = range(NLIST) */
- for (i = 0; i < NLIST; ++i) {
- PyObject* anint = PyLong_FromLong(i);
- if (anint == (PyObject*)NULL) {
- Py_DECREF(list);
- return (PyObject*)NULL;
- }
- PyList_SET_ITEM(list, i, anint);
- }
- /* list.reverse(), via PyList_Reverse() */
- i = PyList_Reverse(list); /* should not blow up! */
- if (i != 0) {
- Py_DECREF(list);
- return (PyObject*)NULL;
- }
- /* Check that list == range(29, -1, -1) now */
- for (i = 0; i < NLIST; ++i) {
- PyObject* anint = PyList_GET_ITEM(list, i);
- if (PyLong_AS_LONG(anint) != NLIST-1-i) {
- PyErr_SetString(TestError,
- "test_list_api: reverse screwed up");
- Py_DECREF(list);
- return (PyObject*)NULL;
- }
- }
- Py_DECREF(list);
+ list = PyList_New(NLIST);
+ if (list == (PyObject*)NULL)
+ return (PyObject*)NULL;
+ /* list = range(NLIST) */
+ for (i = 0; i < NLIST; ++i) {
+ PyObject* anint = PyLong_FromLong(i);
+ if (anint == (PyObject*)NULL) {
+ Py_DECREF(list);
+ return (PyObject*)NULL;
+ }
+ PyList_SET_ITEM(list, i, anint);
+ }
+ /* list.reverse(), via PyList_Reverse() */
+ i = PyList_Reverse(list); /* should not blow up! */
+ if (i != 0) {
+ Py_DECREF(list);
+ return (PyObject*)NULL;
+ }
+ /* Check that list == range(29, -1, -1) now */
+ for (i = 0; i < NLIST; ++i) {
+ PyObject* anint = PyList_GET_ITEM(list, i);
+ if (PyLong_AS_LONG(anint) != NLIST-1-i) {
+ PyErr_SetString(TestError,
+ "test_list_api: reverse screwed up");
+ Py_DECREF(list);
+ return (PyObject*)NULL;
+ }
+ }
+ Py_DECREF(list);
#undef NLIST
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static int
test_dict_inner(int count)
{
- Py_ssize_t pos = 0, iterations = 0;
- int i;
- PyObject *dict = PyDict_New();
- PyObject *v, *k;
-
- if (dict == NULL)
- return -1;
-
- for (i = 0; i < count; i++) {
- v = PyLong_FromLong(i);
- PyDict_SetItem(dict, v, v);
- Py_DECREF(v);
- }
-
- while (PyDict_Next(dict, &pos, &k, &v)) {
- PyObject *o;
- iterations++;
-
- i = PyLong_AS_LONG(v) + 1;
- o = PyLong_FromLong(i);
- if (o == NULL)
- return -1;
- if (PyDict_SetItem(dict, k, o) < 0) {
- Py_DECREF(o);
- return -1;
- }
- Py_DECREF(o);
- }
-
- Py_DECREF(dict);
-
- if (iterations != count) {
- PyErr_SetString(
- TestError,
- "test_dict_iteration: dict iteration went wrong ");
- return -1;
- } else {
- return 0;
- }
+ Py_ssize_t pos = 0, iterations = 0;
+ int i;
+ PyObject *dict = PyDict_New();
+ PyObject *v, *k;
+
+ if (dict == NULL)
+ return -1;
+
+ for (i = 0; i < count; i++) {
+ v = PyLong_FromLong(i);
+ PyDict_SetItem(dict, v, v);
+ Py_DECREF(v);
+ }
+
+ while (PyDict_Next(dict, &pos, &k, &v)) {
+ PyObject *o;
+ iterations++;
+
+ i = PyLong_AS_LONG(v) + 1;
+ o = PyLong_FromLong(i);
+ if (o == NULL)
+ return -1;
+ if (PyDict_SetItem(dict, k, o) < 0) {
+ Py_DECREF(o);
+ return -1;
+ }
+ Py_DECREF(o);
+ }
+
+ Py_DECREF(dict);
+
+ if (iterations != count) {
+ PyErr_SetString(
+ TestError,
+ "test_dict_iteration: dict iteration went wrong ");
+ return -1;
+ } else {
+ return 0;
+ }
}
static PyObject*
test_dict_iteration(PyObject* self)
{
- int i;
+ int i;
- for (i = 0; i < 200; i++) {
- if (test_dict_inner(i) < 0) {
- return NULL;
- }
- }
+ for (i = 0; i < 200; i++) {
+ if (test_dict_inner(i) < 0) {
+ return NULL;
+ }
+ }
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
@@ -179,107 +179,107 @@ test_dict_iteration(PyObject* self)
* PyType_Ready if it hasn't already been called
*/
static PyTypeObject _HashInheritanceTester_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "hashinheritancetester", /* Name of this type */
- sizeof(PyObject), /* Basic object size */
- 0, /* Item size for varobject */
- (destructor)PyObject_Del, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "hashinheritancetester", /* Name of this type */
+ sizeof(PyObject), /* Basic object size */
+ 0, /* Item size for varobject */
+ (destructor)PyObject_Del, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
};
static PyObject*
test_lazy_hash_inheritance(PyObject* self)
{
- PyTypeObject *type;
- PyObject *obj;
- long hash;
-
- type = &_HashInheritanceTester_Type;
-
- if (type->tp_dict != NULL)
- /* The type has already been initialized. This probably means
- -R is being used. */
- Py_RETURN_NONE;
-
-
- obj = PyObject_New(PyObject, type);
- if (obj == NULL) {
- PyErr_Clear();
- PyErr_SetString(
- TestError,
- "test_lazy_hash_inheritance: failed to create object");
- return NULL;
- }
-
- if (type->tp_dict != NULL) {
- PyErr_SetString(
- TestError,
- "test_lazy_hash_inheritance: type initialised too soon");
- Py_DECREF(obj);
- return NULL;
- }
-
- hash = PyObject_Hash(obj);
- if ((hash == -1) && PyErr_Occurred()) {
- PyErr_Clear();
- PyErr_SetString(
- TestError,
- "test_lazy_hash_inheritance: could not hash object");
- Py_DECREF(obj);
- return NULL;
- }
-
- if (type->tp_dict == NULL) {
- PyErr_SetString(
- TestError,
- "test_lazy_hash_inheritance: type not initialised by hash()");
- Py_DECREF(obj);
- return NULL;
- }
-
- if (type->tp_hash != PyType_Type.tp_hash) {
- PyErr_SetString(
- TestError,
- "test_lazy_hash_inheritance: unexpected hash function");
- Py_DECREF(obj);
- return NULL;
- }
-
- Py_DECREF(obj);
-
- Py_RETURN_NONE;
+ PyTypeObject *type;
+ PyObject *obj;
+ long hash;
+
+ type = &_HashInheritanceTester_Type;
+
+ if (type->tp_dict != NULL)
+ /* The type has already been initialized. This probably means
+ -R is being used. */
+ Py_RETURN_NONE;
+
+
+ obj = PyObject_New(PyObject, type);
+ if (obj == NULL) {
+ PyErr_Clear();
+ PyErr_SetString(
+ TestError,
+ "test_lazy_hash_inheritance: failed to create object");
+ return NULL;
+ }
+
+ if (type->tp_dict != NULL) {
+ PyErr_SetString(
+ TestError,
+ "test_lazy_hash_inheritance: type initialised too soon");
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ hash = PyObject_Hash(obj);
+ if ((hash == -1) && PyErr_Occurred()) {
+ PyErr_Clear();
+ PyErr_SetString(
+ TestError,
+ "test_lazy_hash_inheritance: could not hash object");
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ if (type->tp_dict == NULL) {
+ PyErr_SetString(
+ TestError,
+ "test_lazy_hash_inheritance: type not initialised by hash()");
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ if (type->tp_hash != PyType_Type.tp_hash) {
+ PyErr_SetString(
+ TestError,
+ "test_lazy_hash_inheritance: unexpected hash function");
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ Py_DECREF(obj);
+
+ Py_RETURN_NONE;
}
@@ -290,85 +290,85 @@ test_lazy_hash_inheritance(PyObject* self)
static int
broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags)
{
- PyErr_SetString(
- TestError,
- "test_broken_memoryview: expected error in bf_getbuffer");
- return -1;
+ PyErr_SetString(
+ TestError,
+ "test_broken_memoryview: expected error in bf_getbuffer");
+ return -1;
}
static PyBufferProcs memoryviewtester_as_buffer = {
- (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
- 0, /* bf_releasebuffer */
+ (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
+ 0, /* bf_releasebuffer */
};
static PyTypeObject _MemoryViewTester_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "memoryviewtester", /* Name of this type */
- sizeof(PyObject), /* Basic object size */
- 0, /* Item size for varobject */
- (destructor)PyObject_Del, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_compare */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- &memoryviewtester_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "memoryviewtester", /* Name of this type */
+ sizeof(PyObject), /* Basic object size */
+ 0, /* Item size for varobject */
+ (destructor)PyObject_Del, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_compare */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ &memoryviewtester_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
};
static PyObject*
test_broken_memoryview(PyObject* self)
{
- PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type);
- PyObject *res;
-
- if (obj == NULL) {
- PyErr_Clear();
- PyErr_SetString(
- TestError,
- "test_broken_memoryview: failed to create object");
- return NULL;
- }
-
- res = PyMemoryView_FromObject(obj);
- if (res || !PyErr_Occurred()){
- PyErr_SetString(
- TestError,
- "test_broken_memoryview: memoryview() didn't raise an Exception");
- Py_XDECREF(res);
- Py_DECREF(obj);
- return NULL;
- }
-
- PyErr_Clear();
- Py_DECREF(obj);
- Py_RETURN_NONE;
+ PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type);
+ PyObject *res;
+
+ if (obj == NULL) {
+ PyErr_Clear();
+ PyErr_SetString(
+ TestError,
+ "test_broken_memoryview: failed to create object");
+ return NULL;
+ }
+
+ res = PyMemoryView_FromObject(obj);
+ if (res || !PyErr_Occurred()){
+ PyErr_SetString(
+ TestError,
+ "test_broken_memoryview: memoryview() didn't raise an Exception");
+ Py_XDECREF(res);
+ Py_DECREF(obj);
+ return NULL;
+ }
+
+ PyErr_Clear();
+ Py_DECREF(obj);
+ Py_RETURN_NONE;
}
@@ -392,22 +392,22 @@ test_broken_memoryview(PyObject* self)
static PyObject *
raise_test_long_error(const char* msg)
{
- return raiseTestError("test_long_api", msg);
+ return raiseTestError("test_long_api", msg);
}
-#define TESTNAME test_long_api_inner
-#define TYPENAME long
-#define F_S_TO_PY PyLong_FromLong
-#define F_PY_TO_S PyLong_AsLong
-#define F_U_TO_PY PyLong_FromUnsignedLong
-#define F_PY_TO_U PyLong_AsUnsignedLong
+#define TESTNAME test_long_api_inner
+#define TYPENAME long
+#define F_S_TO_PY PyLong_FromLong
+#define F_PY_TO_S PyLong_AsLong
+#define F_U_TO_PY PyLong_FromUnsignedLong
+#define F_PY_TO_U PyLong_AsUnsignedLong
#include "testcapi_long.h"
static PyObject *
test_long_api(PyObject* self)
{
- return TESTNAME(raise_test_long_error);
+ return TESTNAME(raise_test_long_error);
}
#undef TESTNAME
@@ -422,22 +422,22 @@ test_long_api(PyObject* self)
static PyObject *
raise_test_longlong_error(const char* msg)
{
- return raiseTestError("test_longlong_api", msg);
+ return raiseTestError("test_longlong_api", msg);
}
-#define TESTNAME test_longlong_api_inner
-#define TYPENAME PY_LONG_LONG
-#define F_S_TO_PY PyLong_FromLongLong
-#define F_PY_TO_S PyLong_AsLongLong
-#define F_U_TO_PY PyLong_FromUnsignedLongLong
-#define F_PY_TO_U PyLong_AsUnsignedLongLong
+#define TESTNAME test_longlong_api_inner
+#define TYPENAME PY_LONG_LONG
+#define F_S_TO_PY PyLong_FromLongLong
+#define F_PY_TO_S PyLong_AsLongLong
+#define F_U_TO_PY PyLong_FromUnsignedLongLong
+#define F_PY_TO_U PyLong_AsUnsignedLongLong
#include "testcapi_long.h"
static PyObject *
test_longlong_api(PyObject* self, PyObject *args)
{
- return TESTNAME(raise_test_longlong_error);
+ return TESTNAME(raise_test_longlong_error);
}
#undef TESTNAME
@@ -454,71 +454,71 @@ test_longlong_api(PyObject* self, PyObject *args)
static PyObject *
test_L_code(PyObject *self)
{
- PyObject *tuple, *num;
- PY_LONG_LONG value;
-
- tuple = PyTuple_New(1);
- if (tuple == NULL)
- return NULL;
-
- num = PyLong_FromLong(42);
- if (num == NULL)
- return NULL;
-
- PyTuple_SET_ITEM(tuple, 0, num);
-
- value = -1;
- if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
- return NULL;
- if (value != 42)
- return raiseTestError("test_L_code",
- "L code returned wrong value for long 42");
-
- Py_DECREF(num);
- num = PyLong_FromLong(42);
- if (num == NULL)
- return NULL;
-
- PyTuple_SET_ITEM(tuple, 0, num);
-
- value = -1;
- if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
- return NULL;
- if (value != 42)
- return raiseTestError("test_L_code",
- "L code returned wrong value for int 42");
-
- Py_DECREF(tuple);
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *tuple, *num;
+ PY_LONG_LONG value;
+
+ tuple = PyTuple_New(1);
+ if (tuple == NULL)
+ return NULL;
+
+ num = PyLong_FromLong(42);
+ if (num == NULL)
+ return NULL;
+
+ PyTuple_SET_ITEM(tuple, 0, num);
+
+ value = -1;
+ if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
+ return NULL;
+ if (value != 42)
+ return raiseTestError("test_L_code",
+ "L code returned wrong value for long 42");
+
+ Py_DECREF(num);
+ num = PyLong_FromLong(42);
+ if (num == NULL)
+ return NULL;
+
+ PyTuple_SET_ITEM(tuple, 0, num);
+
+ value = -1;
+ if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
+ return NULL;
+ if (value != 42)
+ return raiseTestError("test_L_code",
+ "L code returned wrong value for int 42");
+
+ Py_DECREF(tuple);
+ Py_INCREF(Py_None);
+ return Py_None;
}
-#endif /* ifdef HAVE_LONG_LONG */
+#endif /* ifdef HAVE_LONG_LONG */
/* Test tuple argument processing */
static PyObject *
getargs_tuple(PyObject *self, PyObject *args)
{
- int a, b, c;
- if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
- return NULL;
- return Py_BuildValue("iii", a, b, c);
+ int a, b, c;
+ if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
+ return NULL;
+ return Py_BuildValue("iii", a, b, c);
}
/* test PyArg_ParseTupleAndKeywords */
static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
{
- static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
- static char *fmt="(ii)i|(i(ii))(iii)i";
- int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
- &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
- &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
- return NULL;
- return Py_BuildValue("iiiiiiiiii",
- int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
- int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
+ static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
+ static char *fmt="(ii)i|(i(ii))(iii)i";
+ int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
+ &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
+ &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
+ return NULL;
+ return Py_BuildValue("iiiiiiiiii",
+ int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
+ int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
}
/* Functions to call PyArg_ParseTuple with integer format codes,
@@ -527,92 +527,92 @@ static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwar
static PyObject *
getargs_b(PyObject *self, PyObject *args)
{
- unsigned char value;
- if (!PyArg_ParseTuple(args, "b", &value))
- return NULL;
- return PyLong_FromUnsignedLong((unsigned long)value);
+ unsigned char value;
+ if (!PyArg_ParseTuple(args, "b", &value))
+ return NULL;
+ return PyLong_FromUnsignedLong((unsigned long)value);
}
static PyObject *
getargs_B(PyObject *self, PyObject *args)
{
- unsigned char value;
- if (!PyArg_ParseTuple(args, "B", &value))
- return NULL;
- return PyLong_FromUnsignedLong((unsigned long)value);
+ unsigned char value;
+ if (!PyArg_ParseTuple(args, "B", &value))
+ return NULL;
+ return PyLong_FromUnsignedLong((unsigned long)value);
}
static PyObject *
getargs_H(PyObject *self, PyObject *args)
{
- unsigned short value;
- if (!PyArg_ParseTuple(args, "H", &value))
- return NULL;
- return PyLong_FromUnsignedLong((unsigned long)value);
+ unsigned short value;
+ if (!PyArg_ParseTuple(args, "H", &value))
+ return NULL;
+ return PyLong_FromUnsignedLong((unsigned long)value);
}
static PyObject *
getargs_I(PyObject *self, PyObject *args)
{
- unsigned int value;
- if (!PyArg_ParseTuple(args, "I", &value))
- return NULL;
- return PyLong_FromUnsignedLong((unsigned long)value);
+ unsigned int value;
+ if (!PyArg_ParseTuple(args, "I", &value))
+ return NULL;
+ return PyLong_FromUnsignedLong((unsigned long)value);
}
static PyObject *
getargs_k(PyObject *self, PyObject *args)
{
- unsigned long value;
- if (!PyArg_ParseTuple(args, "k", &value))
- return NULL;
- return PyLong_FromUnsignedLong(value);
+ unsigned long value;
+ if (!PyArg_ParseTuple(args, "k", &value))
+ return NULL;
+ return PyLong_FromUnsignedLong(value);
}
static PyObject *
getargs_i(PyObject *self, PyObject *args)
{
- int value;
- if (!PyArg_ParseTuple(args, "i", &value))
- return NULL;
- return PyLong_FromLong((long)value);
+ int value;
+ if (!PyArg_ParseTuple(args, "i", &value))
+ return NULL;
+ return PyLong_FromLong((long)value);
}
static PyObject *
getargs_l(PyObject *self, PyObject *args)
{
- long value;
- if (!PyArg_ParseTuple(args, "l", &value))
- return NULL;
- return PyLong_FromLong(value);
+ long value;
+ if (!PyArg_ParseTuple(args, "l", &value))
+ return NULL;
+ return PyLong_FromLong(value);
}
static PyObject *
getargs_n(PyObject *self, PyObject *args)
{
- Py_ssize_t value;
- if (!PyArg_ParseTuple(args, "n", &value))
- return NULL;
- return PyLong_FromSsize_t(value);
+ Py_ssize_t value;
+ if (!PyArg_ParseTuple(args, "n", &value))
+ return NULL;
+ return PyLong_FromSsize_t(value);
}
#ifdef HAVE_LONG_LONG
static PyObject *
getargs_L(PyObject *self, PyObject *args)
{
- PY_LONG_LONG value;
- if (!PyArg_ParseTuple(args, "L", &value))
- return NULL;
- return PyLong_FromLongLong(value);
+ PY_LONG_LONG value;
+ if (!PyArg_ParseTuple(args, "L", &value))
+ return NULL;
+ return PyLong_FromLongLong(value);
}
static PyObject *
getargs_K(PyObject *self, PyObject *args)
{
- unsigned PY_LONG_LONG value;
- if (!PyArg_ParseTuple(args, "K", &value))
- return NULL;
- return PyLong_FromUnsignedLongLong(value);
+ unsigned PY_LONG_LONG value;
+ if (!PyArg_ParseTuple(args, "K", &value))
+ return NULL;
+ return PyLong_FromUnsignedLongLong(value);
}
#endif
@@ -621,54 +621,54 @@ getargs_K(PyObject *self, PyObject *args)
static PyObject *
test_k_code(PyObject *self)
{
- PyObject *tuple, *num;
- unsigned long value;
-
- tuple = PyTuple_New(1);
- if (tuple == NULL)
- return NULL;
-
- /* a number larger than ULONG_MAX even on 64-bit platforms */
- num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
- if (num == NULL)
- return NULL;
-
- value = PyLong_AsUnsignedLongMask(num);
- if (value != ULONG_MAX)
- return raiseTestError("test_k_code",
- "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
-
- PyTuple_SET_ITEM(tuple, 0, num);
-
- value = 0;
- if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
- return NULL;
- if (value != ULONG_MAX)
- return raiseTestError("test_k_code",
- "k code returned wrong value for long 0xFFF...FFF");
-
- Py_DECREF(num);
- num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
- if (num == NULL)
- return NULL;
-
- value = PyLong_AsUnsignedLongMask(num);
- if (value != (unsigned long)-0x42)
- return raiseTestError("test_k_code",
- "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
-
- PyTuple_SET_ITEM(tuple, 0, num);
-
- value = 0;
- if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
- return NULL;
- if (value != (unsigned long)-0x42)
- return raiseTestError("test_k_code",
- "k code returned wrong value for long -0xFFF..000042");
-
- Py_DECREF(tuple);
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *tuple, *num;
+ unsigned long value;
+
+ tuple = PyTuple_New(1);
+ if (tuple == NULL)
+ return NULL;
+
+ /* a number larger than ULONG_MAX even on 64-bit platforms */
+ num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
+ if (num == NULL)
+ return NULL;
+
+ value = PyLong_AsUnsignedLongMask(num);
+ if (value != ULONG_MAX)
+ return raiseTestError("test_k_code",
+ "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
+
+ PyTuple_SET_ITEM(tuple, 0, num);
+
+ value = 0;
+ if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
+ return NULL;
+ if (value != ULONG_MAX)
+ return raiseTestError("test_k_code",
+ "k code returned wrong value for long 0xFFF...FFF");
+
+ Py_DECREF(num);
+ num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
+ if (num == NULL)
+ return NULL;
+
+ value = PyLong_AsUnsignedLongMask(num);
+ if (value != (unsigned long)-0x42)
+ return raiseTestError("test_k_code",
+ "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
+
+ PyTuple_SET_ITEM(tuple, 0, num);
+
+ value = 0;
+ if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
+ return NULL;
+ if (value != (unsigned long)-0x42)
+ return raiseTestError("test_k_code",
+ "k code returned wrong value for long -0xFFF..000042");
+
+ Py_DECREF(tuple);
+ Py_INCREF(Py_None);
+ return Py_None;
}
@@ -683,23 +683,23 @@ test_s_code(PyObject *self)
tuple = PyTuple_New(1);
if (tuple == NULL)
- return NULL;
+ return NULL;
obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
- "latin-1", NULL);
+ "latin-1", NULL);
if (obj == NULL)
- return NULL;
+ return NULL;
PyTuple_SET_ITEM(tuple, 0, obj);
/* These two blocks used to raise a TypeError:
- * "argument must be string without null bytes, not str"
+ * "argument must be string without null bytes, not str"
*/
if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0)
- return NULL;
+ return NULL;
if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0)
- return NULL;
+ return NULL;
Py_DECREF(tuple);
Py_RETURN_NONE;
@@ -708,46 +708,46 @@ test_s_code(PyObject *self)
static PyObject *
test_bug_7414(PyObject *self)
{
- /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being
- skipped properly in skipitem() */
- int a = 0, b = 0, result;
- char *kwlist[] = {"a", "b", NULL};
- PyObject *tuple = NULL, *dict = NULL, *b_str;
-
- tuple = PyTuple_New(0);
- if (tuple == NULL)
- goto failure;
- dict = PyDict_New();
- if (dict == NULL)
- goto failure;
- b_str = PyUnicode_FromString("b");
- if (b_str == NULL)
- goto failure;
- result = PyDict_SetItemString(dict, "b", b_str);
- Py_DECREF(b_str);
- if (result < 0)
- goto failure;
-
- result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC",
- kwlist, &a, &b);
- if (!result)
- goto failure;
-
- if (a != 0)
- return raiseTestError("test_bug_7414",
- "C format code not skipped properly");
- if (b != 'b')
- return raiseTestError("test_bug_7414",
- "C format code returned wrong value");
-
- Py_DECREF(dict);
- Py_DECREF(tuple);
- Py_RETURN_NONE;
+ /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being
+ skipped properly in skipitem() */
+ int a = 0, b = 0, result;
+ char *kwlist[] = {"a", "b", NULL};
+ PyObject *tuple = NULL, *dict = NULL, *b_str;
+
+ tuple = PyTuple_New(0);
+ if (tuple == NULL)
+ goto failure;
+ dict = PyDict_New();
+ if (dict == NULL)
+ goto failure;
+ b_str = PyUnicode_FromString("b");
+ if (b_str == NULL)
+ goto failure;
+ result = PyDict_SetItemString(dict, "b", b_str);
+ Py_DECREF(b_str);
+ if (result < 0)
+ goto failure;
+
+ result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC",
+ kwlist, &a, &b);
+ if (!result)
+ goto failure;
+
+ if (a != 0)
+ return raiseTestError("test_bug_7414",
+ "C format code not skipped properly");
+ if (b != 'b')
+ return raiseTestError("test_bug_7414",
+ "C format code returned wrong value");
+
+ Py_DECREF(dict);
+ Py_DECREF(tuple);
+ Py_RETURN_NONE;
failure:
- Py_XDECREF(dict);
- Py_XDECREF(tuple);
- return NULL;
+ Py_XDECREF(dict);
+ Py_XDECREF(tuple);
+ return NULL;
}
@@ -759,186 +759,186 @@ static volatile int x;
static PyObject *
test_u_code(PyObject *self)
{
- PyObject *tuple, *obj;
- Py_UNICODE *value;
- Py_ssize_t len;
- int x;
-
- /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
- /* Just use the macro and check that it compiles */
- x = Py_UNICODE_ISSPACE(25);
-
- tuple = PyTuple_New(1);
- if (tuple == NULL)
- return NULL;
-
- obj = PyUnicode_Decode("test", strlen("test"),
- "ascii", NULL);
- if (obj == NULL)
- return NULL;
-
- PyTuple_SET_ITEM(tuple, 0, obj);
-
- value = 0;
- if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
- return NULL;
- if (value != PyUnicode_AS_UNICODE(obj))
- return raiseTestError("test_u_code",
- "u code returned wrong value for u'test'");
- value = 0;
- if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
- return NULL;
- if (value != PyUnicode_AS_UNICODE(obj) ||
- len != PyUnicode_GET_SIZE(obj))
- return raiseTestError("test_u_code",
- "u# code returned wrong values for u'test'");
-
- Py_DECREF(tuple);
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *tuple, *obj;
+ Py_UNICODE *value;
+ Py_ssize_t len;
+ int x;
+
+ /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
+ /* Just use the macro and check that it compiles */
+ x = Py_UNICODE_ISSPACE(25);
+
+ tuple = PyTuple_New(1);
+ if (tuple == NULL)
+ return NULL;
+
+ obj = PyUnicode_Decode("test", strlen("test"),
+ "ascii", NULL);
+ if (obj == NULL)
+ return NULL;
+
+ PyTuple_SET_ITEM(tuple, 0, obj);
+
+ value = 0;
+ if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
+ return NULL;
+ if (value != PyUnicode_AS_UNICODE(obj))
+ return raiseTestError("test_u_code",
+ "u code returned wrong value for u'test'");
+ value = 0;
+ if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
+ return NULL;
+ if (value != PyUnicode_AS_UNICODE(obj) ||
+ len != PyUnicode_GET_SIZE(obj))
+ return raiseTestError("test_u_code",
+ "u# code returned wrong values for u'test'");
+
+ Py_DECREF(tuple);
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* Test Z and Z# codes for PyArg_ParseTuple */
static PyObject *
test_Z_code(PyObject *self)
{
- PyObject *tuple, *obj;
- Py_UNICODE *value1, *value2;
- Py_ssize_t len1, len2;
-
- tuple = PyTuple_New(2);
- if (tuple == NULL)
- return NULL;
-
- obj = PyUnicode_FromString("test");
- PyTuple_SET_ITEM(tuple, 0, obj);
- Py_INCREF(Py_None);
- PyTuple_SET_ITEM(tuple, 1, Py_None);
-
- /* swap values on purpose */
- value1 = NULL;
- value2 = PyUnicode_AS_UNICODE(obj);
-
- /* Test Z for both values */
- if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
- return NULL;
- if (value1 != PyUnicode_AS_UNICODE(obj))
- return raiseTestError("test_Z_code",
- "Z code returned wrong value for 'test'");
- if (value2 != NULL)
- return raiseTestError("test_Z_code",
- "Z code returned wrong value for None");
-
- value1 = NULL;
- value2 = PyUnicode_AS_UNICODE(obj);
- len1 = -1;
- len2 = -1;
-
- /* Test Z# for both values */
- if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
- &value2, &len2) < 0)
- return NULL;
- if (value1 != PyUnicode_AS_UNICODE(obj) ||
- len1 != PyUnicode_GET_SIZE(obj))
- return raiseTestError("test_Z_code",
- "Z# code returned wrong values for 'test'");
- if (value2 != NULL ||
- len2 != 0)
- return raiseTestError("test_Z_code",
- "Z# code returned wrong values for None'");
-
- Py_DECREF(tuple);
- Py_RETURN_NONE;
+ PyObject *tuple, *obj;
+ Py_UNICODE *value1, *value2;
+ Py_ssize_t len1, len2;
+
+ tuple = PyTuple_New(2);
+ if (tuple == NULL)
+ return NULL;
+
+ obj = PyUnicode_FromString("test");
+ PyTuple_SET_ITEM(tuple, 0, obj);
+ Py_INCREF(Py_None);
+ PyTuple_SET_ITEM(tuple, 1, Py_None);
+
+ /* swap values on purpose */
+ value1 = NULL;
+ value2 = PyUnicode_AS_UNICODE(obj);
+
+ /* Test Z for both values */
+ if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
+ return NULL;
+ if (value1 != PyUnicode_AS_UNICODE(obj))
+ return raiseTestError("test_Z_code",
+ "Z code returned wrong value for 'test'");
+ if (value2 != NULL)
+ return raiseTestError("test_Z_code",
+ "Z code returned wrong value for None");
+
+ value1 = NULL;
+ value2 = PyUnicode_AS_UNICODE(obj);
+ len1 = -1;
+ len2 = -1;
+
+ /* Test Z# for both values */
+ if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
+ &value2, &len2) < 0)
+ return NULL;
+ if (value1 != PyUnicode_AS_UNICODE(obj) ||
+ len1 != PyUnicode_GET_SIZE(obj))
+ return raiseTestError("test_Z_code",
+ "Z# code returned wrong values for 'test'");
+ if (value2 != NULL ||
+ len2 != 0)
+ return raiseTestError("test_Z_code",
+ "Z# code returned wrong values for None'");
+
+ Py_DECREF(tuple);
+ Py_RETURN_NONE;
}
static PyObject *
test_widechar(PyObject *self)
{
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
- const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
- size_t wtextlen = 1;
+ const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
+ size_t wtextlen = 1;
#else
- const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
- size_t wtextlen = 2;
+ const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
+ size_t wtextlen = 2;
#endif
- PyObject *wide, *utf8;
-
- wide = PyUnicode_FromWideChar(wtext, wtextlen);
- if (wide == NULL)
- return NULL;
-
- utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
- if (utf8 == NULL) {
- Py_DECREF(wide);
- return NULL;
- }
-
- if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
- Py_DECREF(wide);
- Py_DECREF(utf8);
- return raiseTestError("test_widechar",
- "wide string and utf8 string "
- "have different length");
- }
- if (PyUnicode_Compare(wide, utf8)) {
- Py_DECREF(wide);
- Py_DECREF(utf8);
- if (PyErr_Occurred())
- return NULL;
- return raiseTestError("test_widechar",
- "wide string and utf8 string "
- "are different");
- }
-
- Py_DECREF(wide);
- Py_DECREF(utf8);
- Py_RETURN_NONE;
+ PyObject *wide, *utf8;
+
+ wide = PyUnicode_FromWideChar(wtext, wtextlen);
+ if (wide == NULL)
+ return NULL;
+
+ utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
+ if (utf8 == NULL) {
+ Py_DECREF(wide);
+ return NULL;
+ }
+
+ if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
+ Py_DECREF(wide);
+ Py_DECREF(utf8);
+ return raiseTestError("test_widechar",
+ "wide string and utf8 string "
+ "have different length");
+ }
+ if (PyUnicode_Compare(wide, utf8)) {
+ Py_DECREF(wide);
+ Py_DECREF(utf8);
+ if (PyErr_Occurred())
+ return NULL;
+ return raiseTestError("test_widechar",
+ "wide string and utf8 string "
+ "are different");
+ }
+
+ Py_DECREF(wide);
+ Py_DECREF(utf8);
+ Py_RETURN_NONE;
}
static PyObject *
test_empty_argparse(PyObject *self)
{
- /* Test that formats can begin with '|'. See issue #4720. */
- PyObject *tuple, *dict = NULL;
- static char *kwlist[] = {NULL};
- int result;
- tuple = PyTuple_New(0);
- if (!tuple)
- return NULL;
- if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
- goto done;
- dict = PyDict_New();
- if (!dict)
- goto done;
- result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
+ /* Test that formats can begin with '|'. See issue #4720. */
+ PyObject *tuple, *dict = NULL;
+ static char *kwlist[] = {NULL};
+ int result;
+ tuple = PyTuple_New(0);
+ if (!tuple)
+ return NULL;
+ if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
+ goto done;
+ dict = PyDict_New();
+ if (!dict)
+ goto done;
+ result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
done:
- Py_DECREF(tuple);
- Py_XDECREF(dict);
- if (result < 0)
- return NULL;
- else {
- Py_RETURN_NONE;
- }
+ Py_DECREF(tuple);
+ Py_XDECREF(dict);
+ if (result < 0)
+ return NULL;
+ else {
+ Py_RETURN_NONE;
+ }
}
static PyObject *
codec_incrementalencoder(PyObject *self, PyObject *args)
{
- const char *encoding, *errors = NULL;
- if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
- &encoding, &errors))
- return NULL;
- return PyCodec_IncrementalEncoder(encoding, errors);
+ const char *encoding, *errors = NULL;
+ if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
+ &encoding, &errors))
+ return NULL;
+ return PyCodec_IncrementalEncoder(encoding, errors);
}
static PyObject *
codec_incrementaldecoder(PyObject *self, PyObject *args)
{
- const char *encoding, *errors = NULL;
- if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
- &encoding, &errors))
- return NULL;
- return PyCodec_IncrementalDecoder(encoding, errors);
+ const char *encoding, *errors = NULL;
+ if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
+ &encoding, &errors))
+ return NULL;
+ return PyCodec_IncrementalDecoder(encoding, errors);
}
@@ -946,42 +946,42 @@ codec_incrementaldecoder(PyObject *self, PyObject *args)
static PyObject *
test_long_numbits(PyObject *self)
{
- struct triple {
- long input;
- size_t nbits;
- int sign;
- } testcases[] = {{0, 0, 0},
- {1L, 1, 1},
- {-1L, 1, -1},
- {2L, 2, 1},
- {-2L, 2, -1},
- {3L, 2, 1},
- {-3L, 2, -1},
- {4L, 3, 1},
- {-4L, 3, -1},
- {0x7fffL, 15, 1}, /* one Python long digit */
- {-0x7fffL, 15, -1},
- {0xffffL, 16, 1},
- {-0xffffL, 16, -1},
- {0xfffffffL, 28, 1},
- {-0xfffffffL, 28, -1}};
- int i;
-
- for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
- PyObject *plong = PyLong_FromLong(testcases[i].input);
- size_t nbits = _PyLong_NumBits(plong);
- int sign = _PyLong_Sign(plong);
-
- Py_DECREF(plong);
- if (nbits != testcases[i].nbits)
- return raiseTestError("test_long_numbits",
- "wrong result for _PyLong_NumBits");
- if (sign != testcases[i].sign)
- return raiseTestError("test_long_numbits",
- "wrong result for _PyLong_Sign");
- }
- Py_INCREF(Py_None);
- return Py_None;
+ struct triple {
+ long input;
+ size_t nbits;
+ int sign;
+ } testcases[] = {{0, 0, 0},
+ {1L, 1, 1},
+ {-1L, 1, -1},
+ {2L, 2, 1},
+ {-2L, 2, -1},
+ {3L, 2, 1},
+ {-3L, 2, -1},
+ {4L, 3, 1},
+ {-4L, 3, -1},
+ {0x7fffL, 15, 1}, /* one Python long digit */
+ {-0x7fffL, 15, -1},
+ {0xffffL, 16, 1},
+ {-0xffffL, 16, -1},
+ {0xfffffffL, 28, 1},
+ {-0xfffffffL, 28, -1}};
+ int i;
+
+ for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
+ PyObject *plong = PyLong_FromLong(testcases[i].input);
+ size_t nbits = _PyLong_NumBits(plong);
+ int sign = _PyLong_Sign(plong);
+
+ Py_DECREF(plong);
+ if (nbits != testcases[i].nbits)
+ return raiseTestError("test_long_numbits",
+ "wrong result for _PyLong_NumBits");
+ if (sign != testcases[i].sign)
+ return raiseTestError("test_long_numbits",
+ "wrong result for _PyLong_Sign");
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* Example passing NULLs to PyObject_Str(NULL). */
@@ -989,38 +989,38 @@ test_long_numbits(PyObject *self)
static PyObject *
test_null_strings(PyObject *self)
{
- PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
- PyObject *tuple = PyTuple_Pack(2, o1, o2);
- Py_XDECREF(o1);
- Py_XDECREF(o2);
- return tuple;
+ PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
+ PyObject *tuple = PyTuple_Pack(2, o1, o2);
+ Py_XDECREF(o1);
+ Py_XDECREF(o2);
+ return tuple;
}
static PyObject *
raise_exception(PyObject *self, PyObject *args)
{
- PyObject *exc;
- PyObject *exc_args, *v;
- int num_args, i;
-
- if (!PyArg_ParseTuple(args, "Oi:raise_exception",
- &exc, &num_args))
- return NULL;
-
- exc_args = PyTuple_New(num_args);
- if (exc_args == NULL)
- return NULL;
- for (i = 0; i < num_args; ++i) {
- v = PyLong_FromLong(i);
- if (v == NULL) {
- Py_DECREF(exc_args);
- return NULL;
- }
- PyTuple_SET_ITEM(exc_args, i, v);
- }
- PyErr_SetObject(exc, exc_args);
- Py_DECREF(exc_args);
- return NULL;
+ PyObject *exc;
+ PyObject *exc_args, *v;
+ int num_args, i;
+
+ if (!PyArg_ParseTuple(args, "Oi:raise_exception",
+ &exc, &num_args))
+ return NULL;
+
+ exc_args = PyTuple_New(num_args);
+ if (exc_args == NULL)
+ return NULL;
+ for (i = 0; i < num_args; ++i) {
+ v = PyLong_FromLong(i);
+ if (v == NULL) {
+ Py_DECREF(exc_args);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(exc_args, i, v);
+ }
+ PyErr_SetObject(exc, exc_args);
+ Py_DECREF(exc_args);
+ return NULL;
}
#ifdef WITH_THREAD
@@ -1037,14 +1037,14 @@ static PyThread_type_lock thread_done = NULL;
static int
_make_call(void *callable)
{
- PyObject *rc;
- int success;
- PyGILState_STATE s = PyGILState_Ensure();
- rc = PyObject_CallFunction((PyObject *)callable, "");
- success = (rc != NULL);
- Py_XDECREF(rc);
- PyGILState_Release(s);
- return success;
+ PyObject *rc;
+ int success;
+ PyGILState_STATE s = PyGILState_Ensure();
+ rc = PyObject_CallFunction((PyObject *)callable, "");
+ success = (rc != NULL);
+ Py_XDECREF(rc);
+ PyGILState_Release(s);
+ return success;
}
/* Same thing, but releases `thread_done` when it returns. This variant
@@ -1053,70 +1053,70 @@ _make_call(void *callable)
static void
_make_call_from_thread(void *callable)
{
- _make_call(callable);
- PyThread_release_lock(thread_done);
+ _make_call(callable);
+ PyThread_release_lock(thread_done);
}
static PyObject *
test_thread_state(PyObject *self, PyObject *args)
{
- PyObject *fn;
- int success = 1;
-
- if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
- return NULL;
-
- if (!PyCallable_Check(fn)) {
- PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
- fn->ob_type->tp_name);
- return NULL;
- }
-
- /* Ensure Python is set up for threading */
- PyEval_InitThreads();
- thread_done = PyThread_allocate_lock();
- if (thread_done == NULL)
- return PyErr_NoMemory();
- PyThread_acquire_lock(thread_done, 1);
-
- /* Start a new thread with our callback. */
- PyThread_start_new_thread(_make_call_from_thread, fn);
- /* Make the callback with the thread lock held by this thread */
- success &= _make_call(fn);
- /* Do it all again, but this time with the thread-lock released */
- Py_BEGIN_ALLOW_THREADS
- success &= _make_call(fn);
- PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
- Py_END_ALLOW_THREADS
-
- /* And once more with and without a thread
- XXX - should use a lock and work out exactly what we are trying
- to test <wink>
- */
- Py_BEGIN_ALLOW_THREADS
- PyThread_start_new_thread(_make_call_from_thread, fn);
- success &= _make_call(fn);
- PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
- Py_END_ALLOW_THREADS
-
- /* Release lock we acquired above. This is required on HP-UX. */
- PyThread_release_lock(thread_done);
-
- PyThread_free_lock(thread_done);
- if (!success)
- return NULL;
- Py_RETURN_NONE;
+ PyObject *fn;
+ int success = 1;
+
+ if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
+ return NULL;
+
+ if (!PyCallable_Check(fn)) {
+ PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
+ fn->ob_type->tp_name);
+ return NULL;
+ }
+
+ /* Ensure Python is set up for threading */
+ PyEval_InitThreads();
+ thread_done = PyThread_allocate_lock();
+ if (thread_done == NULL)
+ return PyErr_NoMemory();
+ PyThread_acquire_lock(thread_done, 1);
+
+ /* Start a new thread with our callback. */
+ PyThread_start_new_thread(_make_call_from_thread, fn);
+ /* Make the callback with the thread lock held by this thread */
+ success &= _make_call(fn);
+ /* Do it all again, but this time with the thread-lock released */
+ Py_BEGIN_ALLOW_THREADS
+ success &= _make_call(fn);
+ PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
+ Py_END_ALLOW_THREADS
+
+ /* And once more with and without a thread
+ XXX - should use a lock and work out exactly what we are trying
+ to test <wink>
+ */
+ Py_BEGIN_ALLOW_THREADS
+ PyThread_start_new_thread(_make_call_from_thread, fn);
+ success &= _make_call(fn);
+ PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
+ Py_END_ALLOW_THREADS
+
+ /* Release lock we acquired above. This is required on HP-UX. */
+ PyThread_release_lock(thread_done);
+
+ PyThread_free_lock(thread_done);
+ if (!success)
+ return NULL;
+ Py_RETURN_NONE;
}
/* test Py_AddPendingCalls using threads */
static int _pending_callback(void *arg)
{
- /* we assume the argument is callable object to which we own a reference */
- PyObject *callable = (PyObject *)arg;
- PyObject *r = PyObject_CallObject(callable, NULL);
- Py_DECREF(callable);
- Py_XDECREF(r);
- return r != NULL ? 0 : -1;
+ /* we assume the argument is callable object to which we own a reference */
+ PyObject *callable = (PyObject *)arg;
+ PyObject *r = PyObject_CallObject(callable, NULL);
+ Py_DECREF(callable);
+ Py_XDECREF(r);
+ return r != NULL ? 0 : -1;
}
/* The following requests n callbacks to _pending_callback. It can be
@@ -1124,25 +1124,25 @@ static int _pending_callback(void *arg)
*/
PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
{
- PyObject *callable;
- int r;
- if (PyArg_ParseTuple(arg, "O", &callable) == 0)
- return NULL;
-
- /* create the reference for the callbackwhile we hold the lock */
- Py_INCREF(callable);
-
- Py_BEGIN_ALLOW_THREADS
- r = Py_AddPendingCall(&_pending_callback, callable);
- Py_END_ALLOW_THREADS
-
- if (r<0) {
- Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
- Py_INCREF(Py_False);
- return Py_False;
- }
- Py_INCREF(Py_True);
- return Py_True;
+ PyObject *callable;
+ int r;
+ if (PyArg_ParseTuple(arg, "O", &callable) == 0)
+ return NULL;
+
+ /* create the reference for the callbackwhile we hold the lock */
+ Py_INCREF(callable);
+
+ Py_BEGIN_ALLOW_THREADS
+ r = Py_AddPendingCall(&_pending_callback, callable);
+ Py_END_ALLOW_THREADS
+
+ if (r<0) {
+ Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
+ Py_INCREF(Py_True);
+ return Py_True;
}
#endif
@@ -1150,34 +1150,34 @@ PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
static PyObject *
test_string_from_format(PyObject *self, PyObject *args)
{
- PyObject *result;
- char *msg;
-
-#define CHECK_1_FORMAT(FORMAT, TYPE) \
- result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
- if (result == NULL) \
- return NULL; \
- if (strcmp(_PyUnicode_AsString(result), "1")) { \
- msg = FORMAT " failed at 1"; \
- goto Fail; \
- } \
- Py_DECREF(result)
-
- CHECK_1_FORMAT("%d", int);
- CHECK_1_FORMAT("%ld", long);
- /* The z width modifier was added in Python 2.5. */
- CHECK_1_FORMAT("%zd", Py_ssize_t);
-
- /* The u type code was added in Python 2.5. */
- CHECK_1_FORMAT("%u", unsigned int);
- CHECK_1_FORMAT("%lu", unsigned long);
- CHECK_1_FORMAT("%zu", size_t);
-
- Py_RETURN_NONE;
+ PyObject *result;
+ char *msg;
+
+#define CHECK_1_FORMAT(FORMAT, TYPE) \
+ result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
+ if (result == NULL) \
+ return NULL; \
+ if (strcmp(_PyUnicode_AsString(result), "1")) { \
+ msg = FORMAT " failed at 1"; \
+ goto Fail; \
+ } \
+ Py_DECREF(result)
+
+ CHECK_1_FORMAT("%d", int);
+ CHECK_1_FORMAT("%ld", long);
+ /* The z width modifier was added in Python 2.5. */
+ CHECK_1_FORMAT("%zd", Py_ssize_t);
+
+ /* The u type code was added in Python 2.5. */
+ CHECK_1_FORMAT("%u", unsigned int);
+ CHECK_1_FORMAT("%lu", unsigned long);
+ CHECK_1_FORMAT("%zu", size_t);
+
+ Py_RETURN_NONE;
Fail:
- Py_XDECREF(result);
- return raiseTestError("test_string_from_format", msg);
+ Py_XDECREF(result);
+ return raiseTestError("test_string_from_format", msg);
#undef CHECK_1_FORMAT
}
@@ -1185,70 +1185,70 @@ test_string_from_format(PyObject *self, PyObject *args)
static PyObject *
test_unicode_compare_with_ascii(PyObject *self) {
- PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
- int result;
- if (py_s == NULL)
- return NULL;
- result = PyUnicode_CompareWithASCIIString(py_s, "str");
- Py_DECREF(py_s);
- if (!result) {
- PyErr_SetString(TestError, "Python string ending in NULL "
- "should not compare equal to c string.");
- return NULL;
- }
- Py_RETURN_NONE;
+ PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
+ int result;
+ if (py_s == NULL)
+ return NULL;
+ result = PyUnicode_CompareWithASCIIString(py_s, "str");
+ Py_DECREF(py_s);
+ if (!result) {
+ PyErr_SetString(TestError, "Python string ending in NULL "
+ "should not compare equal to c string.");
+ return NULL;
+ }
+ Py_RETURN_NONE;
};
/* This is here to provide a docstring for test_descr. */
static PyObject *
test_with_docstring(PyObject *self)
{
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
/* Test PyOS_string_to_double. */
static PyObject *
test_string_to_double(PyObject *self) {
- double result;
- char *msg;
-
-#define CHECK_STRING(STR, expected) \
- result = PyOS_string_to_double(STR, NULL, NULL); \
- if (result == -1.0 && PyErr_Occurred()) \
- return NULL; \
- if (result != expected) { \
- msg = "conversion of " STR " to float failed"; \
- goto fail; \
- }
-
-#define CHECK_INVALID(STR) \
- result = PyOS_string_to_double(STR, NULL, NULL); \
- if (result == -1.0 && PyErr_Occurred()) { \
- if (PyErr_ExceptionMatches(PyExc_ValueError)) \
- PyErr_Clear(); \
- else \
- return NULL; \
- } \
- else { \
- msg = "conversion of " STR " didn't raise ValueError"; \
- goto fail; \
- }
-
- CHECK_STRING("0.1", 0.1);
- CHECK_STRING("1.234", 1.234);
- CHECK_STRING("-1.35", -1.35);
- CHECK_STRING(".1e01", 1.0);
- CHECK_STRING("2.e-2", 0.02);
-
- CHECK_INVALID(" 0.1");
- CHECK_INVALID("\t\n-3");
- CHECK_INVALID(".123 ");
- CHECK_INVALID("3\n");
- CHECK_INVALID("123abc");
-
- Py_RETURN_NONE;
+ double result;
+ char *msg;
+
+#define CHECK_STRING(STR, expected) \
+ result = PyOS_string_to_double(STR, NULL, NULL); \
+ if (result == -1.0 && PyErr_Occurred()) \
+ return NULL; \
+ if (result != expected) { \
+ msg = "conversion of " STR " to float failed"; \
+ goto fail; \
+ }
+
+#define CHECK_INVALID(STR) \
+ result = PyOS_string_to_double(STR, NULL, NULL); \
+ if (result == -1.0 && PyErr_Occurred()) { \
+ if (PyErr_ExceptionMatches(PyExc_ValueError)) \
+ PyErr_Clear(); \
+ else \
+ return NULL; \
+ } \
+ else { \
+ msg = "conversion of " STR " didn't raise ValueError"; \
+ goto fail; \
+ }
+
+ CHECK_STRING("0.1", 0.1);
+ CHECK_STRING("1.234", 1.234);
+ CHECK_STRING("-1.35", -1.35);
+ CHECK_STRING(".1e01", 1.0);
+ CHECK_STRING("2.e-2", 0.02);
+
+ CHECK_INVALID(" 0.1");
+ CHECK_INVALID("\t\n-3");
+ CHECK_INVALID(".123 ");
+ CHECK_INVALID("3\n");
+ CHECK_INVALID("123abc");
+
+ Py_RETURN_NONE;
fail:
- return raiseTestError("test_string_to_double", msg);
+ return raiseTestError("test_string_to_double", msg);
#undef CHECK_STRING
#undef CHECK_INVALID
}
@@ -1265,143 +1265,143 @@ capsule_destructor_call_count = 0;
static void
capsule_destructor(PyObject *o) {
- capsule_destructor_call_count++;
- if (PyCapsule_GetContext(o) != capsule_context) {
- capsule_error = "context did not match in destructor!";
- } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
- capsule_error = "destructor did not match in destructor! (woah!)";
- } else if (PyCapsule_GetName(o) != capsule_name) {
- capsule_error = "name did not match in destructor!";
- } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
- capsule_error = "pointer did not match in destructor!";
- }
+ capsule_destructor_call_count++;
+ if (PyCapsule_GetContext(o) != capsule_context) {
+ capsule_error = "context did not match in destructor!";
+ } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
+ capsule_error = "destructor did not match in destructor! (woah!)";
+ } else if (PyCapsule_GetName(o) != capsule_name) {
+ capsule_error = "name did not match in destructor!";
+ } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
+ capsule_error = "pointer did not match in destructor!";
+ }
}
typedef struct {
- char *name;
- char *module;
- char *attribute;
+ char *name;
+ char *module;
+ char *attribute;
} known_capsule;
static PyObject *
test_capsule(PyObject *self, PyObject *args)
{
- PyObject *object;
- const char *error = NULL;
- void *pointer;
- void *pointer2;
- known_capsule known_capsules[] = {
- #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
- KNOWN_CAPSULE("_socket", "CAPI"),
- KNOWN_CAPSULE("_curses", "_C_API"),
- KNOWN_CAPSULE("datetime", "datetime_CAPI"),
- { NULL, NULL },
- };
- known_capsule *known = &known_capsules[0];
+ PyObject *object;
+ const char *error = NULL;
+ void *pointer;
+ void *pointer2;
+ known_capsule known_capsules[] = {
+ #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
+ KNOWN_CAPSULE("_socket", "CAPI"),
+ KNOWN_CAPSULE("_curses", "_C_API"),
+ KNOWN_CAPSULE("datetime", "datetime_CAPI"),
+ { NULL, NULL },
+ };
+ known_capsule *known = &known_capsules[0];
#define FAIL(x) { error = (x); goto exit; }
#define CHECK_DESTRUCTOR \
- if (capsule_error) { \
- FAIL(capsule_error); \
- } \
- else if (!capsule_destructor_call_count) { \
- FAIL("destructor not called!"); \
- } \
- capsule_destructor_call_count = 0; \
-
- object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
- PyCapsule_SetContext(object, capsule_context);
- capsule_destructor(object);
- CHECK_DESTRUCTOR;
- Py_DECREF(object);
- CHECK_DESTRUCTOR;
-
- object = PyCapsule_New(known, "ignored", NULL);
- PyCapsule_SetPointer(object, capsule_pointer);
- PyCapsule_SetName(object, capsule_name);
- PyCapsule_SetDestructor(object, capsule_destructor);
- PyCapsule_SetContext(object, capsule_context);
- capsule_destructor(object);
- CHECK_DESTRUCTOR;
- /* intentionally access using the wrong name */
- pointer2 = PyCapsule_GetPointer(object, "the wrong name");
- if (!PyErr_Occurred()) {
- FAIL("PyCapsule_GetPointer should have failed but did not!");
- }
- PyErr_Clear();
- if (pointer2) {
- if (pointer2 == capsule_pointer) {
- FAIL("PyCapsule_GetPointer should not have"
- " returned the internal pointer!");
- } else {
- FAIL("PyCapsule_GetPointer should have "
- "returned NULL pointer but did not!");
- }
- }
- PyCapsule_SetDestructor(object, NULL);
- Py_DECREF(object);
- if (capsule_destructor_call_count) {
- FAIL("destructor called when it should not have been!");
- }
-
- for (known = &known_capsules[0]; known->module != NULL; known++) {
- /* yeah, ordinarily I wouldn't do this either,
- but it's fine for this test harness.
- */
- static char buffer[256];
+ if (capsule_error) { \
+ FAIL(capsule_error); \
+ } \
+ else if (!capsule_destructor_call_count) { \
+ FAIL("destructor not called!"); \
+ } \
+ capsule_destructor_call_count = 0; \
+
+ object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
+ PyCapsule_SetContext(object, capsule_context);
+ capsule_destructor(object);
+ CHECK_DESTRUCTOR;
+ Py_DECREF(object);
+ CHECK_DESTRUCTOR;
+
+ object = PyCapsule_New(known, "ignored", NULL);
+ PyCapsule_SetPointer(object, capsule_pointer);
+ PyCapsule_SetName(object, capsule_name);
+ PyCapsule_SetDestructor(object, capsule_destructor);
+ PyCapsule_SetContext(object, capsule_context);
+ capsule_destructor(object);
+ CHECK_DESTRUCTOR;
+ /* intentionally access using the wrong name */
+ pointer2 = PyCapsule_GetPointer(object, "the wrong name");
+ if (!PyErr_Occurred()) {
+ FAIL("PyCapsule_GetPointer should have failed but did not!");
+ }
+ PyErr_Clear();
+ if (pointer2) {
+ if (pointer2 == capsule_pointer) {
+ FAIL("PyCapsule_GetPointer should not have"
+ " returned the internal pointer!");
+ } else {
+ FAIL("PyCapsule_GetPointer should have "
+ "returned NULL pointer but did not!");
+ }
+ }
+ PyCapsule_SetDestructor(object, NULL);
+ Py_DECREF(object);
+ if (capsule_destructor_call_count) {
+ FAIL("destructor called when it should not have been!");
+ }
+
+ for (known = &known_capsules[0]; known->module != NULL; known++) {
+ /* yeah, ordinarily I wouldn't do this either,
+ but it's fine for this test harness.
+ */
+ static char buffer[256];
#undef FAIL
#define FAIL(x) \
- { \
- sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
- x, known->module, known->attribute); \
- error = buffer; \
- goto exit; \
- } \
-
- PyObject *module = PyImport_ImportModule(known->module);
- if (module) {
- pointer = PyCapsule_Import(known->name, 0);
- if (!pointer) {
- Py_DECREF(module);
- FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
- }
- object = PyObject_GetAttrString(module, known->attribute);
- if (!object) {
- Py_DECREF(module);
- return NULL;
- }
- pointer2 = PyCapsule_GetPointer(object,
- "weebles wobble but they don't fall down");
- if (!PyErr_Occurred()) {
- Py_DECREF(object);
- Py_DECREF(module);
- FAIL("PyCapsule_GetPointer should have failed but did not!");
- }
- PyErr_Clear();
- if (pointer2) {
- Py_DECREF(module);
- Py_DECREF(object);
- if (pointer2 == pointer) {
- FAIL("PyCapsule_GetPointer should not have"
- " returned its internal pointer!");
- } else {
- FAIL("PyCapsule_GetPointer should have"
- " returned NULL pointer but did not!");
- }
- }
- Py_DECREF(object);
- Py_DECREF(module);
- }
- else
- PyErr_Clear();
- }
+ { \
+ sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
+ x, known->module, known->attribute); \
+ error = buffer; \
+ goto exit; \
+ } \
+
+ PyObject *module = PyImport_ImportModule(known->module);
+ if (module) {
+ pointer = PyCapsule_Import(known->name, 0);
+ if (!pointer) {
+ Py_DECREF(module);
+ FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
+ }
+ object = PyObject_GetAttrString(module, known->attribute);
+ if (!object) {
+ Py_DECREF(module);
+ return NULL;
+ }
+ pointer2 = PyCapsule_GetPointer(object,
+ "weebles wobble but they don't fall down");
+ if (!PyErr_Occurred()) {
+ Py_DECREF(object);
+ Py_DECREF(module);
+ FAIL("PyCapsule_GetPointer should have failed but did not!");
+ }
+ PyErr_Clear();
+ if (pointer2) {
+ Py_DECREF(module);
+ Py_DECREF(object);
+ if (pointer2 == pointer) {
+ FAIL("PyCapsule_GetPointer should not have"
+ " returned its internal pointer!");
+ } else {
+ FAIL("PyCapsule_GetPointer should have"
+ " returned NULL pointer but did not!");
+ }
+ }
+ Py_DECREF(object);
+ Py_DECREF(module);
+ }
+ else
+ PyErr_Clear();
+ }
exit:
- if (error) {
- return raiseTestError("test_capsule", error);
- }
- Py_RETURN_NONE;
+ if (error) {
+ return raiseTestError("test_capsule", error);
+ }
+ Py_RETURN_NONE;
#undef FAIL
}
@@ -1409,112 +1409,112 @@ test_capsule(PyObject *self, PyObject *args)
/* Profiling of integer performance */
static void print_delta(int test, struct timeval *s, struct timeval *e)
{
- e->tv_sec -= s->tv_sec;
- e->tv_usec -= s->tv_usec;
- if (e->tv_usec < 0) {
- e->tv_sec -=1;
- e->tv_usec += 1000000;
- }
- printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
+ e->tv_sec -= s->tv_sec;
+ e->tv_usec -= s->tv_usec;
+ if (e->tv_usec < 0) {
+ e->tv_sec -=1;
+ e->tv_usec += 1000000;
+ }
+ printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
}
static PyObject *
profile_int(PyObject *self, PyObject* args)
{
- int i, k;
- struct timeval start, stop;
- PyObject *single, **multiple, *op1, *result;
-
- /* Test 1: Allocate and immediately deallocate
- many small integers */
- gettimeofday(&start, NULL);
- for(k=0; k < 20000; k++)
- for(i=0; i < 1000; i++) {
- single = PyLong_FromLong(i);
- Py_DECREF(single);
- }
- gettimeofday(&stop, NULL);
- print_delta(1, &start, &stop);
-
- /* Test 2: Allocate and immediately deallocate
- many large integers */
- gettimeofday(&start, NULL);
- for(k=0; k < 20000; k++)
- for(i=0; i < 1000; i++) {
- single = PyLong_FromLong(i+1000000);
- Py_DECREF(single);
- }
- gettimeofday(&stop, NULL);
- print_delta(2, &start, &stop);
-
- /* Test 3: Allocate a few integers, then release
- them all simultaneously. */
- multiple = malloc(sizeof(PyObject*) * 1000);
- gettimeofday(&start, NULL);
- for(k=0; k < 20000; k++) {
- for(i=0; i < 1000; i++) {
- multiple[i] = PyLong_FromLong(i+1000000);
- }
- for(i=0; i < 1000; i++) {
- Py_DECREF(multiple[i]);
- }
- }
- gettimeofday(&stop, NULL);
- print_delta(3, &start, &stop);
-
- /* Test 4: Allocate many integers, then release
- them all simultaneously. */
- multiple = malloc(sizeof(PyObject*) * 1000000);
- gettimeofday(&start, NULL);
- for(k=0; k < 20; k++) {
- for(i=0; i < 1000000; i++) {
- multiple[i] = PyLong_FromLong(i+1000000);
- }
- for(i=0; i < 1000000; i++) {
- Py_DECREF(multiple[i]);
- }
- }
- gettimeofday(&stop, NULL);
- print_delta(4, &start, &stop);
-
- /* Test 5: Allocate many integers < 32000 */
- multiple = malloc(sizeof(PyObject*) * 1000000);
- gettimeofday(&start, NULL);
- for(k=0; k < 10; k++) {
- for(i=0; i < 1000000; i++) {
- multiple[i] = PyLong_FromLong(i+1000);
- }
- for(i=0; i < 1000000; i++) {
- Py_DECREF(multiple[i]);
- }
- }
- gettimeofday(&stop, NULL);
- print_delta(5, &start, &stop);
-
- /* Test 6: Perform small int addition */
- op1 = PyLong_FromLong(1);
- gettimeofday(&start, NULL);
- for(i=0; i < 10000000; i++) {
- result = PyNumber_Add(op1, op1);
- Py_DECREF(result);
- }
- gettimeofday(&stop, NULL);
- Py_DECREF(op1);
- print_delta(6, &start, &stop);
-
- /* Test 7: Perform medium int addition */
- op1 = PyLong_FromLong(1000);
- gettimeofday(&start, NULL);
- for(i=0; i < 10000000; i++) {
- result = PyNumber_Add(op1, op1);
- Py_DECREF(result);
- }
- gettimeofday(&stop, NULL);
- Py_DECREF(op1);
- print_delta(7, &start, &stop);
-
- Py_INCREF(Py_None);
- return Py_None;
+ int i, k;
+ struct timeval start, stop;
+ PyObject *single, **multiple, *op1, *result;
+
+ /* Test 1: Allocate and immediately deallocate
+ many small integers */
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++)
+ for(i=0; i < 1000; i++) {
+ single = PyLong_FromLong(i);
+ Py_DECREF(single);
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(1, &start, &stop);
+
+ /* Test 2: Allocate and immediately deallocate
+ many large integers */
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++)
+ for(i=0; i < 1000; i++) {
+ single = PyLong_FromLong(i+1000000);
+ Py_DECREF(single);
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(2, &start, &stop);
+
+ /* Test 3: Allocate a few integers, then release
+ them all simultaneously. */
+ multiple = malloc(sizeof(PyObject*) * 1000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20000; k++) {
+ for(i=0; i < 1000; i++) {
+ multiple[i] = PyLong_FromLong(i+1000000);
+ }
+ for(i=0; i < 1000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(3, &start, &stop);
+
+ /* Test 4: Allocate many integers, then release
+ them all simultaneously. */
+ multiple = malloc(sizeof(PyObject*) * 1000000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 20; k++) {
+ for(i=0; i < 1000000; i++) {
+ multiple[i] = PyLong_FromLong(i+1000000);
+ }
+ for(i=0; i < 1000000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(4, &start, &stop);
+
+ /* Test 5: Allocate many integers < 32000 */
+ multiple = malloc(sizeof(PyObject*) * 1000000);
+ gettimeofday(&start, NULL);
+ for(k=0; k < 10; k++) {
+ for(i=0; i < 1000000; i++) {
+ multiple[i] = PyLong_FromLong(i+1000);
+ }
+ for(i=0; i < 1000000; i++) {
+ Py_DECREF(multiple[i]);
+ }
+ }
+ gettimeofday(&stop, NULL);
+ print_delta(5, &start, &stop);
+
+ /* Test 6: Perform small int addition */
+ op1 = PyLong_FromLong(1);
+ gettimeofday(&start, NULL);
+ for(i=0; i < 10000000; i++) {
+ result = PyNumber_Add(op1, op1);
+ Py_DECREF(result);
+ }
+ gettimeofday(&stop, NULL);
+ Py_DECREF(op1);
+ print_delta(6, &start, &stop);
+
+ /* Test 7: Perform medium int addition */
+ op1 = PyLong_FromLong(1000);
+ gettimeofday(&start, NULL);
+ for(i=0; i < 10000000; i++) {
+ result = PyNumber_Add(op1, op1);
+ Py_DECREF(result);
+ }
+ gettimeofday(&stop, NULL);
+ Py_DECREF(op1);
+ print_delta(7, &start, &stop);
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
#endif
@@ -1522,40 +1522,40 @@ profile_int(PyObject *self, PyObject* args)
static PyObject *
traceback_print(PyObject *self, PyObject *args)
{
- PyObject *file;
- PyObject *traceback;
- int result;
-
- if (!PyArg_ParseTuple(args, "OO:traceback_print",
- &traceback, &file))
- return NULL;
-
- result = PyTraceBack_Print(traceback, file);
- if (result < 0)
- return NULL;
- Py_RETURN_NONE;
+ PyObject *file;
+ PyObject *traceback;
+ int result;
+
+ if (!PyArg_ParseTuple(args, "OO:traceback_print",
+ &traceback, &file))
+ return NULL;
+
+ result = PyTraceBack_Print(traceback, file);
+ if (result < 0)
+ return NULL;
+ Py_RETURN_NONE;
}
/* To test the format of exceptions as printed out. */
static PyObject *
exception_print(PyObject *self, PyObject *args)
{
- PyObject *value;
- PyObject *tb;
-
- if (!PyArg_ParseTuple(args, "O:exception_print",
- &value))
- return NULL;
- if (!PyExceptionInstance_Check(value)) {
- PyErr_Format(PyExc_TypeError, "an exception instance is required");
- return NULL;
- }
-
- tb = PyException_GetTraceback(value);
- PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
- Py_XDECREF(tb);
-
- Py_RETURN_NONE;
+ PyObject *value;
+ PyObject *tb;
+
+ if (!PyArg_ParseTuple(args, "O:exception_print",
+ &value))
+ return NULL;
+ if (!PyExceptionInstance_Check(value)) {
+ PyErr_Format(PyExc_TypeError, "an exception instance is required");
+ return NULL;
+ }
+
+ tb = PyException_GetTraceback(value);
+ PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
+ Py_XDECREF(tb);
+
+ Py_RETURN_NONE;
}
@@ -1565,8 +1565,8 @@ exception_print(PyObject *self, PyObject *args)
static PyObject *
raise_memoryerror(PyObject *self)
{
- PyErr_NoMemory();
- return NULL;
+ PyErr_NoMemory();
+ return NULL;
}
/* Issue 6012 */
@@ -1574,304 +1574,304 @@ static PyObject *str1, *str2;
static int
failing_converter(PyObject *obj, void *arg)
{
- /* Clone str1, then let the conversion fail. */
- assert(str1);
- str2 = str1;
- Py_INCREF(str2);
- return 0;
+ /* Clone str1, then let the conversion fail. */
+ assert(str1);
+ str2 = str1;
+ Py_INCREF(str2);
+ return 0;
}
static PyObject*
argparsing(PyObject *o, PyObject *args)
{
- PyObject *res;
- str1 = str2 = NULL;
- if (!PyArg_ParseTuple(args, "O&O&",
- PyUnicode_FSConverter, &str1,
- failing_converter, &str2)) {
- if (!str2)
- /* argument converter not called? */
- return NULL;
- /* Should be 1 */
- res = PyLong_FromLong(Py_REFCNT(str2));
- Py_DECREF(str2);
- PyErr_Clear();
- return res;
- }
- Py_RETURN_NONE;
+ PyObject *res;
+ str1 = str2 = NULL;
+ if (!PyArg_ParseTuple(args, "O&O&",
+ PyUnicode_FSConverter, &str1,
+ failing_converter, &str2)) {
+ if (!str2)
+ /* argument converter not called? */
+ return NULL;
+ /* Should be 1 */
+ res = PyLong_FromLong(Py_REFCNT(str2));
+ Py_DECREF(str2);
+ PyErr_Clear();
+ return res;
+ }
+ Py_RETURN_NONE;
}
static PyMethodDef TestMethods[] = {
- {"raise_exception", raise_exception, METH_VARARGS},
- {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
- {"test_config", (PyCFunction)test_config, METH_NOARGS},
- {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
- {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
- {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
- {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
- {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
- {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
- {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
- {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
- {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS},
- {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
- {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
- {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
- PyDoc_STR("This is a pretty normal docstring.")},
- {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
- {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
- {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
- {"getargs_tuple", getargs_tuple, METH_VARARGS},
- {"getargs_keywords", (PyCFunction)getargs_keywords,
- METH_VARARGS|METH_KEYWORDS},
- {"getargs_b", getargs_b, METH_VARARGS},
- {"getargs_B", getargs_B, METH_VARARGS},
- {"getargs_H", getargs_H, METH_VARARGS},
- {"getargs_I", getargs_I, METH_VARARGS},
- {"getargs_k", getargs_k, METH_VARARGS},
- {"getargs_i", getargs_i, METH_VARARGS},
- {"getargs_l", getargs_l, METH_VARARGS},
- {"getargs_n", getargs_n, METH_VARARGS},
+ {"raise_exception", raise_exception, METH_VARARGS},
+ {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
+ {"test_config", (PyCFunction)test_config, METH_NOARGS},
+ {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
+ {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
+ {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
+ {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
+ {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
+ {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
+ {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
+ {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
+ {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS},
+ {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
+ {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
+ {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
+ PyDoc_STR("This is a pretty normal docstring.")},
+ {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
+ {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
+ {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
+ {"getargs_tuple", getargs_tuple, METH_VARARGS},
+ {"getargs_keywords", (PyCFunction)getargs_keywords,
+ METH_VARARGS|METH_KEYWORDS},
+ {"getargs_b", getargs_b, METH_VARARGS},
+ {"getargs_B", getargs_B, METH_VARARGS},
+ {"getargs_H", getargs_H, METH_VARARGS},
+ {"getargs_I", getargs_I, METH_VARARGS},
+ {"getargs_k", getargs_k, METH_VARARGS},
+ {"getargs_i", getargs_i, METH_VARARGS},
+ {"getargs_l", getargs_l, METH_VARARGS},
+ {"getargs_n", getargs_n, METH_VARARGS},
#ifdef HAVE_LONG_LONG
- {"getargs_L", getargs_L, METH_VARARGS},
- {"getargs_K", getargs_K, METH_VARARGS},
- {"test_longlong_api", test_longlong_api, METH_NOARGS},
- {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
- {"codec_incrementalencoder",
- (PyCFunction)codec_incrementalencoder, METH_VARARGS},
- {"codec_incrementaldecoder",
- (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
+ {"getargs_L", getargs_L, METH_VARARGS},
+ {"getargs_K", getargs_K, METH_VARARGS},
+ {"test_longlong_api", test_longlong_api, METH_NOARGS},
+ {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
+ {"codec_incrementalencoder",
+ (PyCFunction)codec_incrementalencoder, METH_VARARGS},
+ {"codec_incrementaldecoder",
+ (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
#endif
- {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
- {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
- {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
- {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
+ {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
+ {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
+ {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
+ {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
#ifdef WITH_THREAD
- {"_test_thread_state", test_thread_state, METH_VARARGS},
- {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
+ {"_test_thread_state", test_thread_state, METH_VARARGS},
+ {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
#endif
#ifdef HAVE_GETTIMEOFDAY
- {"profile_int", profile_int, METH_NOARGS},
+ {"profile_int", profile_int, METH_NOARGS},
#endif
- {"traceback_print", traceback_print, METH_VARARGS},
- {"exception_print", exception_print, METH_VARARGS},
- {"argparsing", argparsing, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"traceback_print", traceback_print, METH_VARARGS},
+ {"exception_print", exception_print, METH_VARARGS},
+ {"argparsing", argparsing, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
typedef struct {
- char bool_member;
- char byte_member;
- unsigned char ubyte_member;
- short short_member;
- unsigned short ushort_member;
- int int_member;
- unsigned int uint_member;
- long long_member;
- unsigned long ulong_member;
- Py_ssize_t pyssizet_member;
- float float_member;
- double double_member;
- char inplace_member[6];
+ char bool_member;
+ char byte_member;
+ unsigned char ubyte_member;
+ short short_member;
+ unsigned short ushort_member;
+ int int_member;
+ unsigned int uint_member;
+ long long_member;
+ unsigned long ulong_member;
+ Py_ssize_t pyssizet_member;
+ float float_member;
+ double double_member;
+ char inplace_member[6];
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG longlong_member;
- unsigned PY_LONG_LONG ulonglong_member;
+ PY_LONG_LONG longlong_member;
+ unsigned PY_LONG_LONG ulonglong_member;
#endif
} all_structmembers;
typedef struct {
PyObject_HEAD
- all_structmembers structmembers;
+ all_structmembers structmembers;
} test_structmembers;
static struct PyMemberDef test_members[] = {
- {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
- {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
- {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
- {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
- {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
- {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
- {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
- {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
- {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
- {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
- {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
- {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
- {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
+ {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
+ {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
+ {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
+ {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
+ {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
+ {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
+ {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
+ {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
+ {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
+ {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
+ {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
+ {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
+ {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
#ifdef HAVE_LONG_LONG
- {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
- {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
+ {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
+ {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
#endif
- {NULL}
+ {NULL}
};
static PyObject *
test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
- static char *keywords[] = {
- "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
- "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
- "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
+ static char *keywords[] = {
+ "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
+ "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
+ "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
#ifdef HAVE_LONG_LONG
- "T_LONGLONG", "T_ULONGLONG",
+ "T_LONGLONG", "T_ULONGLONG",
#endif
- NULL};
- static char *fmt = "|bbBhHiIlknfds#"
+ NULL};
+ static char *fmt = "|bbBhHiIlknfds#"
#ifdef HAVE_LONG_LONG
- "LK"
+ "LK"
#endif
- ;
- test_structmembers *ob;
- const char *s = NULL;
- Py_ssize_t string_len = 0;
- ob = PyObject_New(test_structmembers, type);
- if (ob == NULL)
- return NULL;
- memset(&ob->structmembers, 0, sizeof(all_structmembers));
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
- &ob->structmembers.bool_member,
- &ob->structmembers.byte_member,
- &ob->structmembers.ubyte_member,
- &ob->structmembers.short_member,
- &ob->structmembers.ushort_member,
- &ob->structmembers.int_member,
- &ob->structmembers.uint_member,
- &ob->structmembers.long_member,
- &ob->structmembers.ulong_member,
- &ob->structmembers.pyssizet_member,
- &ob->structmembers.float_member,
- &ob->structmembers.double_member,
- &s, &string_len
+ ;
+ test_structmembers *ob;
+ const char *s = NULL;
+ Py_ssize_t string_len = 0;
+ ob = PyObject_New(test_structmembers, type);
+ if (ob == NULL)
+ return NULL;
+ memset(&ob->structmembers, 0, sizeof(all_structmembers));
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
+ &ob->structmembers.bool_member,
+ &ob->structmembers.byte_member,
+ &ob->structmembers.ubyte_member,
+ &ob->structmembers.short_member,
+ &ob->structmembers.ushort_member,
+ &ob->structmembers.int_member,
+ &ob->structmembers.uint_member,
+ &ob->structmembers.long_member,
+ &ob->structmembers.ulong_member,
+ &ob->structmembers.pyssizet_member,
+ &ob->structmembers.float_member,
+ &ob->structmembers.double_member,
+ &s, &string_len
#ifdef HAVE_LONG_LONG
- , &ob->structmembers.longlong_member,
- &ob->structmembers.ulonglong_member
+ , &ob->structmembers.longlong_member,
+ &ob->structmembers.ulonglong_member
#endif
- )) {
- Py_DECREF(ob);
- return NULL;
- }
- if (s != NULL) {
- if (string_len > 5) {
- Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError, "string too long");
- return NULL;
- }
- strcpy(ob->structmembers.inplace_member, s);
- }
- else {
- strcpy(ob->structmembers.inplace_member, "");
- }
- return (PyObject *)ob;
+ )) {
+ Py_DECREF(ob);
+ return NULL;
+ }
+ if (s != NULL) {
+ if (string_len > 5) {
+ Py_DECREF(ob);
+ PyErr_SetString(PyExc_ValueError, "string too long");
+ return NULL;
+ }
+ strcpy(ob->structmembers.inplace_member, s);
+ }
+ else {
+ strcpy(ob->structmembers.inplace_member, "");
+ }
+ return (PyObject *)ob;
}
static void
test_structmembers_free(PyObject *ob)
{
- PyObject_FREE(ob);
+ PyObject_FREE(ob);
}
static PyTypeObject test_structmembersType = {
PyVarObject_HEAD_INIT(NULL, 0)
- "test_structmembersType",
- sizeof(test_structmembers), /* tp_basicsize */
- 0, /* tp_itemsize */
- test_structmembers_free, /* destructor tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- 0, /* tp_flags */
- "Type containing all structmember types",
- 0, /* traverseproc tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- test_members, /* tp_members */
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- test_structmembers_new, /* tp_new */
+ "test_structmembersType",
+ sizeof(test_structmembers), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ test_structmembers_free, /* destructor tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ 0, /* tp_flags */
+ "Type containing all structmember types",
+ 0, /* traverseproc tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ test_members, /* tp_members */
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ 0,
+ test_structmembers_new, /* tp_new */
};
static struct PyModuleDef _testcapimodule = {
- PyModuleDef_HEAD_INIT,
- "_testcapi",
- NULL,
- -1,
- TestMethods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_testcapi",
+ NULL,
+ -1,
+ TestMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__testcapi(void)
{
- PyObject *m;
-
- m = PyModule_Create(&_testcapimodule);
- if (m == NULL)
- return NULL;
-
- Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
- Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type;
-
- Py_TYPE(&test_structmembersType)=&PyType_Type;
- Py_INCREF(&test_structmembersType);
- /* don't use a name starting with "test", since we don't want
- test_capi to automatically call this */
- PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
-
- PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
- PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
- PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
- PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
- PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
- PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
- PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
- PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
- PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
- PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
- PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
- PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
- PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
- PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
- PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
- PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
- PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
- PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
- PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
- PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
- PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
- PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
- Py_INCREF(&PyInstanceMethod_Type);
- PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
-
- TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
- Py_INCREF(TestError);
- PyModule_AddObject(m, "error", TestError);
- return m;
+ PyObject *m;
+
+ m = PyModule_Create(&_testcapimodule);
+ if (m == NULL)
+ return NULL;
+
+ Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
+ Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type;
+
+ Py_TYPE(&test_structmembersType)=&PyType_Type;
+ Py_INCREF(&test_structmembersType);
+ /* don't use a name starting with "test", since we don't want
+ test_capi to automatically call this */
+ PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
+
+ PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
+ PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
+ PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
+ PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
+ PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
+ PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
+ PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
+ PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
+ PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
+ PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
+ PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
+ PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
+ PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
+ PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
+ PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
+ PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
+ PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
+ PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
+ PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
+ PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
+ PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
+ PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
+ Py_INCREF(&PyInstanceMethod_Type);
+ PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
+
+ TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
+ Py_INCREF(TestError);
+ PyModule_AddObject(m, "error", TestError);
+ return m;
}
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 3d4578a..e218ed6 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -18,36 +18,36 @@ static PyObject *ThreadError;
/* Lock objects */
typedef struct {
- PyObject_HEAD
- PyThread_type_lock lock_lock;
+ PyObject_HEAD
+ PyThread_type_lock lock_lock;
} lockobject;
static void
lock_dealloc(lockobject *self)
{
- if (self->lock_lock != NULL) {
- /* Unlock the lock so it's safe to free it */
- PyThread_acquire_lock(self->lock_lock, 0);
- PyThread_release_lock(self->lock_lock);
-
- PyThread_free_lock(self->lock_lock);
- }
- PyObject_Del(self);
+ if (self->lock_lock != NULL) {
+ /* Unlock the lock so it's safe to free it */
+ PyThread_acquire_lock(self->lock_lock, 0);
+ PyThread_release_lock(self->lock_lock);
+
+ PyThread_free_lock(self->lock_lock);
+ }
+ PyObject_Del(self);
}
static PyObject *
lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
{
- int i = 1;
+ int i = 1;
- if (!PyArg_ParseTuple(args, "|i:acquire", &i))
- return NULL;
+ if (!PyArg_ParseTuple(args, "|i:acquire", &i))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- i = PyThread_acquire_lock(self->lock_lock, i);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ i = PyThread_acquire_lock(self->lock_lock, i);
+ Py_END_ALLOW_THREADS
- return PyBool_FromLong((long)i);
+ return PyBool_FromLong((long)i);
}
PyDoc_STRVAR(acquire_doc,
@@ -64,16 +64,16 @@ The blocking operation is not interruptible.");
static PyObject *
lock_PyThread_release_lock(lockobject *self)
{
- /* Sanity check: the lock must be locked */
- if (PyThread_acquire_lock(self->lock_lock, 0)) {
- PyThread_release_lock(self->lock_lock);
- PyErr_SetString(ThreadError, "release unlocked lock");
- return NULL;
- }
-
- PyThread_release_lock(self->lock_lock);
- Py_INCREF(Py_None);
- return Py_None;
+ /* Sanity check: the lock must be locked */
+ if (PyThread_acquire_lock(self->lock_lock, 0)) {
+ PyThread_release_lock(self->lock_lock);
+ PyErr_SetString(ThreadError, "release unlocked lock");
+ return NULL;
+ }
+
+ PyThread_release_lock(self->lock_lock);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(release_doc,
@@ -87,11 +87,11 @@ but it needn't be locked by the same thread that unlocks it.");
static PyObject *
lock_locked_lock(lockobject *self)
{
- if (PyThread_acquire_lock(self->lock_lock, 0)) {
- PyThread_release_lock(self->lock_lock);
- return PyBool_FromLong(0L);
- }
- return PyBool_FromLong(1L);
+ if (PyThread_acquire_lock(self->lock_lock, 0)) {
+ PyThread_release_lock(self->lock_lock);
+ return PyBool_FromLong(0L);
+ }
+ return PyBool_FromLong(1L);
}
PyDoc_STRVAR(locked_doc,
@@ -101,71 +101,71 @@ PyDoc_STRVAR(locked_doc,
Return whether the lock is in the locked state.");
static PyMethodDef lock_methods[] = {
- {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
- METH_VARARGS, acquire_doc},
- {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
- METH_VARARGS, acquire_doc},
- {"release_lock", (PyCFunction)lock_PyThread_release_lock,
- METH_NOARGS, release_doc},
- {"release", (PyCFunction)lock_PyThread_release_lock,
- METH_NOARGS, release_doc},
- {"locked_lock", (PyCFunction)lock_locked_lock,
- METH_NOARGS, locked_doc},
- {"locked", (PyCFunction)lock_locked_lock,
- METH_NOARGS, locked_doc},
- {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
- METH_VARARGS, acquire_doc},
- {"__exit__", (PyCFunction)lock_PyThread_release_lock,
- METH_VARARGS, release_doc},
- {NULL, NULL} /* sentinel */
+ {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
+ METH_VARARGS, acquire_doc},
+ {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
+ METH_VARARGS, acquire_doc},
+ {"release_lock", (PyCFunction)lock_PyThread_release_lock,
+ METH_NOARGS, release_doc},
+ {"release", (PyCFunction)lock_PyThread_release_lock,
+ METH_NOARGS, release_doc},
+ {"locked_lock", (PyCFunction)lock_locked_lock,
+ METH_NOARGS, locked_doc},
+ {"locked", (PyCFunction)lock_locked_lock,
+ METH_NOARGS, locked_doc},
+ {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
+ METH_VARARGS, acquire_doc},
+ {"__exit__", (PyCFunction)lock_PyThread_release_lock,
+ METH_VARARGS, release_doc},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject Locktype = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "_thread.lock", /*tp_name*/
- sizeof(lockobject), /*tp_size*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)lock_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- lock_methods, /*tp_methods*/
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "_thread.lock", /*tp_name*/
+ sizeof(lockobject), /*tp_size*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)lock_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ lock_methods, /*tp_methods*/
};
static lockobject *
newlockobject(void)
{
- lockobject *self;
- self = PyObject_New(lockobject, &Locktype);
- if (self == NULL)
- return NULL;
- self->lock_lock = PyThread_allocate_lock();
- if (self->lock_lock == NULL) {
- Py_DECREF(self);
- PyErr_SetString(ThreadError, "can't allocate lock");
- return NULL;
- }
- return self;
+ lockobject *self;
+ self = PyObject_New(lockobject, &Locktype);
+ if (self == NULL)
+ return NULL;
+ self->lock_lock = PyThread_allocate_lock();
+ if (self->lock_lock == NULL) {
+ Py_DECREF(self);
+ PyErr_SetString(ThreadError, "can't allocate lock");
+ return NULL;
+ }
+ return self;
}
/* Thread-local objects */
@@ -173,351 +173,351 @@ newlockobject(void)
#include "structmember.h"
typedef struct {
- PyObject_HEAD
- PyObject *key;
- PyObject *args;
- PyObject *kw;
- PyObject *dict;
+ PyObject_HEAD
+ PyObject *key;
+ PyObject *args;
+ PyObject *kw;
+ PyObject *dict;
} localobject;
static PyObject *
local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- localobject *self;
- PyObject *tdict;
-
- if (type->tp_init == PyBaseObject_Type.tp_init
- && ((args && PyObject_IsTrue(args))
- || (kw && PyObject_IsTrue(kw)))) {
- PyErr_SetString(PyExc_TypeError,
- "Initialization arguments are not supported");
- return NULL;
- }
-
- self = (localobject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- Py_XINCREF(args);
- self->args = args;
- Py_XINCREF(kw);
- self->kw = kw;
- self->dict = NULL; /* making sure */
- self->key = PyUnicode_FromFormat("thread.local.%p", self);
- if (self->key == NULL)
- goto err;
-
- self->dict = PyDict_New();
- if (self->dict == NULL)
- goto err;
-
- tdict = PyThreadState_GetDict();
- if (tdict == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "Couldn't get thread-state dictionary");
- goto err;
- }
-
- if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
- goto err;
-
- return (PyObject *)self;
+ localobject *self;
+ PyObject *tdict;
+
+ if (type->tp_init == PyBaseObject_Type.tp_init
+ && ((args && PyObject_IsTrue(args))
+ || (kw && PyObject_IsTrue(kw)))) {
+ PyErr_SetString(PyExc_TypeError,
+ "Initialization arguments are not supported");
+ return NULL;
+ }
+
+ self = (localobject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ Py_XINCREF(args);
+ self->args = args;
+ Py_XINCREF(kw);
+ self->kw = kw;
+ self->dict = NULL; /* making sure */
+ self->key = PyUnicode_FromFormat("thread.local.%p", self);
+ if (self->key == NULL)
+ goto err;
+
+ self->dict = PyDict_New();
+ if (self->dict == NULL)
+ goto err;
+
+ tdict = PyThreadState_GetDict();
+ if (tdict == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "Couldn't get thread-state dictionary");
+ goto err;
+ }
+
+ if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
+ goto err;
+
+ return (PyObject *)self;
err:
- Py_DECREF(self);
- return NULL;
+ Py_DECREF(self);
+ return NULL;
}
static int
local_traverse(localobject *self, visitproc visit, void *arg)
{
- Py_VISIT(self->args);
- Py_VISIT(self->kw);
- Py_VISIT(self->dict);
- return 0;
+ Py_VISIT(self->args);
+ Py_VISIT(self->kw);
+ Py_VISIT(self->dict);
+ return 0;
}
static int
local_clear(localobject *self)
{
- Py_CLEAR(self->args);
- Py_CLEAR(self->kw);
- Py_CLEAR(self->dict);
- return 0;
+ Py_CLEAR(self->args);
+ Py_CLEAR(self->kw);
+ Py_CLEAR(self->dict);
+ return 0;
}
static void
local_dealloc(localobject *self)
{
- PyThreadState *tstate;
- if (self->key
- && (tstate = PyThreadState_Get())
- && tstate->interp) {
- for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
- tstate;
- tstate = PyThreadState_Next(tstate))
- if (tstate->dict &&
- PyDict_GetItem(tstate->dict, self->key))
- PyDict_DelItem(tstate->dict, self->key);
- }
-
- Py_XDECREF(self->key);
- local_clear(self);
- Py_TYPE(self)->tp_free((PyObject*)self);
+ PyThreadState *tstate;
+ if (self->key
+ && (tstate = PyThreadState_Get())
+ && tstate->interp) {
+ for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
+ tstate;
+ tstate = PyThreadState_Next(tstate))
+ if (tstate->dict &&
+ PyDict_GetItem(tstate->dict, self->key))
+ PyDict_DelItem(tstate->dict, self->key);
+ }
+
+ Py_XDECREF(self->key);
+ local_clear(self);
+ Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject *
_ldict(localobject *self)
{
- PyObject *tdict, *ldict;
-
- tdict = PyThreadState_GetDict();
- if (tdict == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "Couldn't get thread-state dictionary");
- return NULL;
- }
-
- ldict = PyDict_GetItem(tdict, self->key);
- if (ldict == NULL) {
- ldict = PyDict_New(); /* we own ldict */
-
- if (ldict == NULL)
- return NULL;
- else {
- int i = PyDict_SetItem(tdict, self->key, ldict);
- Py_DECREF(ldict); /* now ldict is borrowed */
- if (i < 0)
- return NULL;
- }
-
- Py_CLEAR(self->dict);
- Py_INCREF(ldict);
- self->dict = ldict; /* still borrowed */
-
- if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
- Py_TYPE(self)->tp_init((PyObject*)self,
- self->args, self->kw) < 0) {
- /* we need to get rid of ldict from thread so
- we create a new one the next time we do an attr
- acces */
- PyDict_DelItem(tdict, self->key);
- return NULL;
- }
-
- }
-
- /* The call to tp_init above may have caused another thread to run.
- Install our ldict again. */
- if (self->dict != ldict) {
- Py_CLEAR(self->dict);
- Py_INCREF(ldict);
- self->dict = ldict;
- }
-
- return ldict;
+ PyObject *tdict, *ldict;
+
+ tdict = PyThreadState_GetDict();
+ if (tdict == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "Couldn't get thread-state dictionary");
+ return NULL;
+ }
+
+ ldict = PyDict_GetItem(tdict, self->key);
+ if (ldict == NULL) {
+ ldict = PyDict_New(); /* we own ldict */
+
+ if (ldict == NULL)
+ return NULL;
+ else {
+ int i = PyDict_SetItem(tdict, self->key, ldict);
+ Py_DECREF(ldict); /* now ldict is borrowed */
+ if (i < 0)
+ return NULL;
+ }
+
+ Py_CLEAR(self->dict);
+ Py_INCREF(ldict);
+ self->dict = ldict; /* still borrowed */
+
+ if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
+ Py_TYPE(self)->tp_init((PyObject*)self,
+ self->args, self->kw) < 0) {
+ /* we need to get rid of ldict from thread so
+ we create a new one the next time we do an attr
+ acces */
+ PyDict_DelItem(tdict, self->key);
+ return NULL;
+ }
+
+ }
+
+ /* The call to tp_init above may have caused another thread to run.
+ Install our ldict again. */
+ if (self->dict != ldict) {
+ Py_CLEAR(self->dict);
+ Py_INCREF(ldict);
+ self->dict = ldict;
+ }
+
+ return ldict;
}
static int
local_setattro(localobject *self, PyObject *name, PyObject *v)
{
- PyObject *ldict;
-
- ldict = _ldict(self);
- if (ldict == NULL)
- return -1;
+ PyObject *ldict;
- return PyObject_GenericSetAttr((PyObject *)self, name, v);
+ ldict = _ldict(self);
+ if (ldict == NULL)
+ return -1;
+
+ return PyObject_GenericSetAttr((PyObject *)self, name, v);
}
static PyObject *
local_getdict(localobject *self, void *closure)
{
- if (self->dict == NULL) {
- PyErr_SetString(PyExc_AttributeError, "__dict__");
- return NULL;
- }
+ if (self->dict == NULL) {
+ PyErr_SetString(PyExc_AttributeError, "__dict__");
+ return NULL;
+ }
- Py_INCREF(self->dict);
- return self->dict;
+ Py_INCREF(self->dict);
+ return self->dict;
}
static PyGetSetDef local_getset[] = {
- {"__dict__", (getter)local_getdict, (setter)NULL,
- "Local-data dictionary", NULL},
- {NULL} /* Sentinel */
+ {"__dict__", (getter)local_getdict, (setter)NULL,
+ "Local-data dictionary", NULL},
+ {NULL} /* Sentinel */
};
static PyObject *local_getattro(localobject *, PyObject *);
static PyTypeObject localtype = {
- PyVarObject_HEAD_INIT(NULL, 0)
- /* tp_name */ "_thread._local",
- /* tp_basicsize */ sizeof(localobject),
- /* tp_itemsize */ 0,
- /* tp_dealloc */ (destructor)local_dealloc,
- /* tp_print */ 0,
- /* tp_getattr */ 0,
- /* tp_setattr */ 0,
- /* tp_reserved */ 0,
- /* tp_repr */ 0,
- /* tp_as_number */ 0,
- /* tp_as_sequence */ 0,
- /* tp_as_mapping */ 0,
- /* tp_hash */ 0,
- /* tp_call */ 0,
- /* tp_str */ 0,
- /* tp_getattro */ (getattrofunc)local_getattro,
- /* tp_setattro */ (setattrofunc)local_setattro,
- /* tp_as_buffer */ 0,
- /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- /* tp_doc */ "Thread-local data",
- /* tp_traverse */ (traverseproc)local_traverse,
- /* tp_clear */ (inquiry)local_clear,
- /* tp_richcompare */ 0,
- /* tp_weaklistoffset */ 0,
- /* tp_iter */ 0,
- /* tp_iternext */ 0,
- /* tp_methods */ 0,
- /* tp_members */ 0,
- /* tp_getset */ local_getset,
- /* tp_base */ 0,
- /* tp_dict */ 0, /* internal use */
- /* tp_descr_get */ 0,
- /* tp_descr_set */ 0,
- /* tp_dictoffset */ offsetof(localobject, dict),
- /* tp_init */ 0,
- /* tp_alloc */ 0,
- /* tp_new */ local_new,
- /* tp_free */ 0, /* Low-level free-mem routine */
- /* tp_is_gc */ 0, /* For PyObject_IS_GC */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ /* tp_name */ "_thread._local",
+ /* tp_basicsize */ sizeof(localobject),
+ /* tp_itemsize */ 0,
+ /* tp_dealloc */ (destructor)local_dealloc,
+ /* tp_print */ 0,
+ /* tp_getattr */ 0,
+ /* tp_setattr */ 0,
+ /* tp_reserved */ 0,
+ /* tp_repr */ 0,
+ /* tp_as_number */ 0,
+ /* tp_as_sequence */ 0,
+ /* tp_as_mapping */ 0,
+ /* tp_hash */ 0,
+ /* tp_call */ 0,
+ /* tp_str */ 0,
+ /* tp_getattro */ (getattrofunc)local_getattro,
+ /* tp_setattro */ (setattrofunc)local_setattro,
+ /* tp_as_buffer */ 0,
+ /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ /* tp_doc */ "Thread-local data",
+ /* tp_traverse */ (traverseproc)local_traverse,
+ /* tp_clear */ (inquiry)local_clear,
+ /* tp_richcompare */ 0,
+ /* tp_weaklistoffset */ 0,
+ /* tp_iter */ 0,
+ /* tp_iternext */ 0,
+ /* tp_methods */ 0,
+ /* tp_members */ 0,
+ /* tp_getset */ local_getset,
+ /* tp_base */ 0,
+ /* tp_dict */ 0, /* internal use */
+ /* tp_descr_get */ 0,
+ /* tp_descr_set */ 0,
+ /* tp_dictoffset */ offsetof(localobject, dict),
+ /* tp_init */ 0,
+ /* tp_alloc */ 0,
+ /* tp_new */ local_new,
+ /* tp_free */ 0, /* Low-level free-mem routine */
+ /* tp_is_gc */ 0, /* For PyObject_IS_GC */
};
static PyObject *
local_getattro(localobject *self, PyObject *name)
{
- PyObject *ldict, *value;
+ PyObject *ldict, *value;
- ldict = _ldict(self);
- if (ldict == NULL)
- return NULL;
+ ldict = _ldict(self);
+ if (ldict == NULL)
+ return NULL;
- if (Py_TYPE(self) != &localtype)
- /* use generic lookup for subtypes */
- return PyObject_GenericGetAttr((PyObject *)self, name);
+ if (Py_TYPE(self) != &localtype)
+ /* use generic lookup for subtypes */
+ return PyObject_GenericGetAttr((PyObject *)self, name);
- /* Optimization: just look in dict ourselves */
- value = PyDict_GetItem(ldict, name);
- if (value == NULL)
- /* Fall back on generic to get __class__ and __dict__ */
- return PyObject_GenericGetAttr((PyObject *)self, name);
+ /* Optimization: just look in dict ourselves */
+ value = PyDict_GetItem(ldict, name);
+ if (value == NULL)
+ /* Fall back on generic to get __class__ and __dict__ */
+ return PyObject_GenericGetAttr((PyObject *)self, name);
- Py_INCREF(value);
- return value;
+ Py_INCREF(value);
+ return value;
}
/* Module functions */
struct bootstate {
- PyInterpreterState *interp;
- PyObject *func;
- PyObject *args;
- PyObject *keyw;
- PyThreadState *tstate;
+ PyInterpreterState *interp;
+ PyObject *func;
+ PyObject *args;
+ PyObject *keyw;
+ PyThreadState *tstate;
};
static void
t_bootstrap(void *boot_raw)
{
- struct bootstate *boot = (struct bootstate *) boot_raw;
- PyThreadState *tstate;
- PyObject *res;
-
- tstate = boot->tstate;
- tstate->thread_id = PyThread_get_thread_ident();
- _PyThreadState_Init(tstate);
- PyEval_AcquireThread(tstate);
- res = PyEval_CallObjectWithKeywords(
- boot->func, boot->args, boot->keyw);
- if (res == NULL) {
- if (PyErr_ExceptionMatches(PyExc_SystemExit))
- PyErr_Clear();
- else {
- PyObject *file;
- PySys_WriteStderr(
- "Unhandled exception in thread started by ");
- file = PySys_GetObject("stderr");
- if (file != NULL && file != Py_None)
- PyFile_WriteObject(boot->func, file, 0);
- else
- PyObject_Print(boot->func, stderr, 0);
- PySys_WriteStderr("\n");
- PyErr_PrintEx(0);
- }
- }
- else
- Py_DECREF(res);
- Py_DECREF(boot->func);
- Py_DECREF(boot->args);
- Py_XDECREF(boot->keyw);
- PyMem_DEL(boot_raw);
- PyThreadState_Clear(tstate);
- PyThreadState_DeleteCurrent();
- PyThread_exit_thread();
+ struct bootstate *boot = (struct bootstate *) boot_raw;
+ PyThreadState *tstate;
+ PyObject *res;
+
+ tstate = boot->tstate;
+ tstate->thread_id = PyThread_get_thread_ident();
+ _PyThreadState_Init(tstate);
+ PyEval_AcquireThread(tstate);
+ res = PyEval_CallObjectWithKeywords(
+ boot->func, boot->args, boot->keyw);
+ if (res == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_SystemExit))
+ PyErr_Clear();
+ else {
+ PyObject *file;
+ PySys_WriteStderr(
+ "Unhandled exception in thread started by ");
+ file = PySys_GetObject("stderr");
+ if (file != NULL && file != Py_None)
+ PyFile_WriteObject(boot->func, file, 0);
+ else
+ PyObject_Print(boot->func, stderr, 0);
+ PySys_WriteStderr("\n");
+ PyErr_PrintEx(0);
+ }
+ }
+ else
+ Py_DECREF(res);
+ Py_DECREF(boot->func);
+ Py_DECREF(boot->args);
+ Py_XDECREF(boot->keyw);
+ PyMem_DEL(boot_raw);
+ PyThreadState_Clear(tstate);
+ PyThreadState_DeleteCurrent();
+ PyThread_exit_thread();
}
static PyObject *
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
{
- PyObject *func, *args, *keyw = NULL;
- struct bootstate *boot;
- long ident;
-
- if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
- &func, &args, &keyw))
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError,
- "first arg must be callable");
- return NULL;
- }
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_TypeError,
- "2nd arg must be a tuple");
- return NULL;
- }
- if (keyw != NULL && !PyDict_Check(keyw)) {
- PyErr_SetString(PyExc_TypeError,
- "optional 3rd arg must be a dictionary");
- return NULL;
- }
- boot = PyMem_NEW(struct bootstate, 1);
- if (boot == NULL)
- return PyErr_NoMemory();
- boot->interp = PyThreadState_GET()->interp;
- boot->func = func;
- boot->args = args;
- boot->keyw = keyw;
- boot->tstate = _PyThreadState_Prealloc(boot->interp);
- if (boot->tstate == NULL) {
- PyMem_DEL(boot);
- return PyErr_NoMemory();
- }
- Py_INCREF(func);
- Py_INCREF(args);
- Py_XINCREF(keyw);
- PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
- ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
- if (ident == -1) {
- PyErr_SetString(ThreadError, "can't start new thread");
- Py_DECREF(func);
- Py_DECREF(args);
- Py_XDECREF(keyw);
- PyThreadState_Clear(boot->tstate);
- PyMem_DEL(boot);
- return NULL;
- }
- return PyLong_FromLong(ident);
+ PyObject *func, *args, *keyw = NULL;
+ struct bootstate *boot;
+ long ident;
+
+ if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
+ &func, &args, &keyw))
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError,
+ "first arg must be callable");
+ return NULL;
+ }
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "2nd arg must be a tuple");
+ return NULL;
+ }
+ if (keyw != NULL && !PyDict_Check(keyw)) {
+ PyErr_SetString(PyExc_TypeError,
+ "optional 3rd arg must be a dictionary");
+ return NULL;
+ }
+ boot = PyMem_NEW(struct bootstate, 1);
+ if (boot == NULL)
+ return PyErr_NoMemory();
+ boot->interp = PyThreadState_GET()->interp;
+ boot->func = func;
+ boot->args = args;
+ boot->keyw = keyw;
+ boot->tstate = _PyThreadState_Prealloc(boot->interp);
+ if (boot->tstate == NULL) {
+ PyMem_DEL(boot);
+ return PyErr_NoMemory();
+ }
+ Py_INCREF(func);
+ Py_INCREF(args);
+ Py_XINCREF(keyw);
+ PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
+ ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
+ if (ident == -1) {
+ PyErr_SetString(ThreadError, "can't start new thread");
+ Py_DECREF(func);
+ Py_DECREF(args);
+ Py_XDECREF(keyw);
+ PyThreadState_Clear(boot->tstate);
+ PyMem_DEL(boot);
+ return NULL;
+ }
+ return PyLong_FromLong(ident);
}
PyDoc_STRVAR(start_new_doc,
@@ -534,8 +534,8 @@ printed unless the exception is SystemExit.\n");
static PyObject *
thread_PyThread_exit_thread(PyObject *self)
{
- PyErr_SetNone(PyExc_SystemExit);
- return NULL;
+ PyErr_SetNone(PyExc_SystemExit);
+ return NULL;
}
PyDoc_STRVAR(exit_doc,
@@ -548,9 +548,9 @@ thread to exit silently unless the exception is caught.");
static PyObject *
thread_PyThread_interrupt_main(PyObject * self)
{
- PyErr_SetInterrupt();
- Py_INCREF(Py_None);
- return Py_None;
+ PyErr_SetInterrupt();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(interrupt_doc,
@@ -564,11 +564,11 @@ A subthread can use this function to interrupt the main thread."
static PyObject *
thread_PyThread_exit_prog(PyObject *self, PyObject *args)
{
- int sts;
- if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
- return NULL;
- Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
- for (;;) { } /* Should not be reached */
+ int sts;
+ if (!PyArg_ParseTuple(args, "i:exit_prog", &sts))
+ return NULL;
+ Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */
+ for (;;) { } /* Should not be reached */
}
#endif
@@ -577,7 +577,7 @@ static lockobject *newlockobject(void);
static PyObject *
thread_PyThread_allocate_lock(PyObject *self)
{
- return (PyObject *) newlockobject();
+ return (PyObject *) newlockobject();
}
PyDoc_STRVAR(allocate_doc,
@@ -589,13 +589,13 @@ Create a new lock object. See LockType.__doc__ for information about locks.");
static PyObject *
thread_get_ident(PyObject *self)
{
- long ident;
- ident = PyThread_get_thread_ident();
- if (ident == -1) {
- PyErr_SetString(ThreadError, "no current thread ident");
- return NULL;
- }
- return PyLong_FromLong(ident);
+ long ident;
+ ident = PyThread_get_thread_ident();
+ if (ident == -1) {
+ PyErr_SetString(ThreadError, "no current thread ident");
+ return NULL;
+ }
+ return PyLong_FromLong(ident);
}
PyDoc_STRVAR(get_ident_doc,
@@ -612,35 +612,35 @@ A thread's identity may be reused for another thread after it exits.");
static PyObject *
thread_stack_size(PyObject *self, PyObject *args)
{
- size_t old_size;
- Py_ssize_t new_size = 0;
- int rc;
-
- if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
- return NULL;
-
- if (new_size < 0) {
- PyErr_SetString(PyExc_ValueError,
- "size must be 0 or a positive value");
- return NULL;
- }
-
- old_size = PyThread_get_stacksize();
-
- rc = PyThread_set_stacksize((size_t) new_size);
- if (rc == -1) {
- PyErr_Format(PyExc_ValueError,
- "size not valid: %zd bytes",
- new_size);
- return NULL;
- }
- if (rc == -2) {
- PyErr_SetString(ThreadError,
- "setting stack size not supported");
- return NULL;
- }
-
- return PyLong_FromSsize_t((Py_ssize_t) old_size);
+ size_t old_size;
+ Py_ssize_t new_size = 0;
+ int rc;
+
+ if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
+ return NULL;
+
+ if (new_size < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "size must be 0 or a positive value");
+ return NULL;
+ }
+
+ old_size = PyThread_get_stacksize();
+
+ rc = PyThread_set_stacksize((size_t) new_size);
+ if (rc == -1) {
+ PyErr_Format(PyExc_ValueError,
+ "size not valid: %zd bytes",
+ new_size);
+ return NULL;
+ }
+ if (rc == -2) {
+ PyErr_SetString(ThreadError,
+ "setting stack size not supported");
+ return NULL;
+ }
+
+ return PyLong_FromSsize_t((Py_ssize_t) old_size);
}
PyDoc_STRVAR(stack_size_doc,
@@ -664,32 +664,32 @@ requiring allocation in multiples of the system memory page size\n\
the suggested approach in the absence of more specific information).");
static PyMethodDef thread_methods[] = {
- {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
- METH_VARARGS,
- start_new_doc},
- {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
- METH_VARARGS,
- start_new_doc},
- {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
- METH_NOARGS, allocate_doc},
- {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
- METH_NOARGS, allocate_doc},
- {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
- METH_NOARGS, exit_doc},
- {"exit", (PyCFunction)thread_PyThread_exit_thread,
- METH_NOARGS, exit_doc},
- {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
- METH_NOARGS, interrupt_doc},
- {"get_ident", (PyCFunction)thread_get_ident,
- METH_NOARGS, get_ident_doc},
- {"stack_size", (PyCFunction)thread_stack_size,
- METH_VARARGS,
- stack_size_doc},
+ {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
+ METH_VARARGS,
+ start_new_doc},
+ {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
+ METH_VARARGS,
+ start_new_doc},
+ {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
+ METH_NOARGS, allocate_doc},
+ {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
+ METH_NOARGS, allocate_doc},
+ {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
+ METH_NOARGS, exit_doc},
+ {"exit", (PyCFunction)thread_PyThread_exit_thread,
+ METH_NOARGS, exit_doc},
+ {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
+ METH_NOARGS, interrupt_doc},
+ {"get_ident", (PyCFunction)thread_get_ident,
+ METH_NOARGS, get_ident_doc},
+ {"stack_size", (PyCFunction)thread_stack_size,
+ METH_VARARGS,
+ stack_size_doc},
#ifndef NO_EXIT_PROG
- {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
- METH_VARARGS},
+ {"exit_prog", (PyCFunction)thread_PyThread_exit_prog,
+ METH_VARARGS},
#endif
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
@@ -712,47 +712,47 @@ unlock it. A thread attempting to lock a lock that it has already locked\n\
will block until another thread unlocks it. Deadlocks may ensue.");
static struct PyModuleDef threadmodule = {
- PyModuleDef_HEAD_INIT,
- "_thread",
- thread_doc,
- -1,
- thread_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_thread",
+ thread_doc,
+ -1,
+ thread_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__thread(void)
{
- PyObject *m, *d;
-
- /* Initialize types: */
- if (PyType_Ready(&localtype) < 0)
- return NULL;
- if (PyType_Ready(&Locktype) < 0)
- return NULL;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&threadmodule);
- if (m == NULL)
- return NULL;
-
- /* Add a symbolic constant */
- d = PyModule_GetDict(m);
- ThreadError = PyErr_NewException("_thread.error", NULL, NULL);
- PyDict_SetItemString(d, "error", ThreadError);
- Locktype.tp_doc = lock_doc;
- Py_INCREF(&Locktype);
- PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
-
- Py_INCREF(&localtype);
- if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
- return NULL;
-
- /* Initialize the C thread library */
- PyThread_init_thread();
- return m;
+ PyObject *m, *d;
+
+ /* Initialize types: */
+ if (PyType_Ready(&localtype) < 0)
+ return NULL;
+ if (PyType_Ready(&Locktype) < 0)
+ return NULL;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&threadmodule);
+ if (m == NULL)
+ return NULL;
+
+ /* Add a symbolic constant */
+ d = PyModule_GetDict(m);
+ ThreadError = PyErr_NewException("_thread.error", NULL, NULL);
+ PyDict_SetItemString(d, "error", ThreadError);
+ Locktype.tp_doc = lock_doc;
+ Py_INCREF(&Locktype);
+ PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
+
+ Py_INCREF(&localtype);
+ if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
+ return NULL;
+
+ /* Initialize the C thread library */
+ PyThread_init_thread();
+ return m;
}
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index ef38ea3..8552575 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -9,9 +9,9 @@ Copyright (C) 1994 Steen Lumholt.
/* TCL/TK VERSION INFO:
- Only Tcl/Tk 8.3.1 and later are supported. Older versions are not
- supported. Use Python 2.6 or older if you cannot upgrade your
- Tcl/Tk libraries.
+ Only Tcl/Tk 8.3.1 and later are supported. Older versions are not
+ supported. Use Python 2.6 or older if you cannot upgrade your
+ Tcl/Tk libraries.
*/
/* XXX Further speed-up ideas, involving Tcl 8.0 features:
@@ -197,32 +197,32 @@ static PyThreadState *tcl_tstate = NULL;
#endif
#define ENTER_TCL \
- { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \
- if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate;
+ { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \
+ if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate;
#define LEAVE_TCL \
tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS}
#define ENTER_OVERLAP \
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
#define LEAVE_OVERLAP_TCL \
- tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
+ tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
#define ENTER_PYTHON \
- { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
- if(tcl_lock)PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }
+ { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
+ if(tcl_lock)PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }
#define LEAVE_PYTHON \
- { PyThreadState *tstate = PyEval_SaveThread(); \
- if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; }
+ { PyThreadState *tstate = PyEval_SaveThread(); \
+ if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; }
#define CHECK_TCL_APPARTMENT \
- if (((TkappObject *)self)->threaded && \
- ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \
- PyErr_SetString(PyExc_RuntimeError, "Calling Tcl from different appartment"); \
- return 0; \
- }
+ if (((TkappObject *)self)->threaded && \
+ ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \
+ PyErr_SetString(PyExc_RuntimeError, "Calling Tcl from different appartment"); \
+ return 0; \
+ }
#else
@@ -245,21 +245,21 @@ static PyThreadState *tcl_tstate = NULL;
static PyTypeObject Tkapp_Type;
typedef struct {
- PyObject_HEAD
- Tcl_Interp *interp;
- int wantobjects;
- int threaded; /* True if tcl_platform[threaded] */
- Tcl_ThreadId thread_id;
- int dispatching;
- /* We cannot include tclInt.h, as this is internal.
- So we cache interesting types here. */
- Tcl_ObjType *BooleanType;
- Tcl_ObjType *ByteArrayType;
- Tcl_ObjType *DoubleType;
- Tcl_ObjType *IntType;
- Tcl_ObjType *ListType;
- Tcl_ObjType *ProcBodyType;
- Tcl_ObjType *StringType;
+ PyObject_HEAD
+ Tcl_Interp *interp;
+ int wantobjects;
+ int threaded; /* True if tcl_platform[threaded] */
+ Tcl_ThreadId thread_id;
+ int dispatching;
+ /* We cannot include tclInt.h, as this is internal.
+ So we cache interesting types here. */
+ Tcl_ObjType *BooleanType;
+ Tcl_ObjType *ByteArrayType;
+ Tcl_ObjType *DoubleType;
+ Tcl_ObjType *IntType;
+ Tcl_ObjType *ListType;
+ Tcl_ObjType *ProcBodyType;
+ Tcl_ObjType *StringType;
} TkappObject;
#define Tkapp_Check(v) (Py_TYPE(v) == &Tkapp_Type)
@@ -270,7 +270,7 @@ typedef struct {
(void *) v, Py_REFCNT(v)))
-
+
/**** Error Handling ****/
static PyObject *Tkinter_TclError;
@@ -284,16 +284,16 @@ static PyObject *trbInCmd;
static int tk_load_failed = 0;
#endif
-
+
static PyObject *
Tkinter_Error(PyObject *v)
{
- PyErr_SetString(Tkinter_TclError, Tkapp_Result(v));
- return NULL;
+ PyErr_SetString(Tkinter_TclError, Tkapp_Result(v));
+ return NULL;
}
-
+
/**** Utils ****/
static int Tkinter_busywaitinterval = 20;
@@ -306,11 +306,11 @@ static int Tkinter_busywaitinterval = 20;
static void
Sleep(int milli)
{
- /* XXX Too bad if you don't have select(). */
- struct timeval t;
- t.tv_sec = milli/1000;
- t.tv_usec = (milli%1000) * 1000;
- select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
+ /* XXX Too bad if you don't have select(). */
+ struct timeval t;
+ t.tv_sec = milli/1000;
+ t.tv_usec = (milli%1000) * 1000;
+ select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
}
#endif /* MS_WINDOWS */
@@ -319,172 +319,172 @@ Sleep(int milli)
static int
WaitForMainloop(TkappObject* self)
{
- int i;
- for (i = 0; i < 10; i++) {
- if (self->dispatching)
- return 1;
- Py_BEGIN_ALLOW_THREADS
- Sleep(100);
- Py_END_ALLOW_THREADS
- }
- if (self->dispatching)
- return 1;
- PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop");
- return 0;
+ int i;
+ for (i = 0; i < 10; i++) {
+ if (self->dispatching)
+ return 1;
+ Py_BEGIN_ALLOW_THREADS
+ Sleep(100);
+ Py_END_ALLOW_THREADS
+ }
+ if (self->dispatching)
+ return 1;
+ PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop");
+ return 0;
}
#endif /* WITH_THREAD */
-
+
static char *
AsString(PyObject *value, PyObject *tmp)
{
- if (PyBytes_Check(value))
- return PyBytes_AsString(value);
- else if (PyUnicode_Check(value)) {
- PyObject *v = PyUnicode_AsUTF8String(value);
- if (v == NULL)
- return NULL;
- if (PyList_Append(tmp, v) != 0) {
- Py_DECREF(v);
- return NULL;
- }
- Py_DECREF(v);
- return PyBytes_AsString(v);
- }
- else {
- PyObject *v = PyObject_Str(value);
- if (v == NULL)
- return NULL;
- if (PyList_Append(tmp, v) != 0) {
- Py_DECREF(v);
- return NULL;
- }
- Py_DECREF(v);
- return PyBytes_AsString(v);
- }
-}
-
-
-
+ if (PyBytes_Check(value))
+ return PyBytes_AsString(value);
+ else if (PyUnicode_Check(value)) {
+ PyObject *v = PyUnicode_AsUTF8String(value);
+ if (v == NULL)
+ return NULL;
+ if (PyList_Append(tmp, v) != 0) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ Py_DECREF(v);
+ return PyBytes_AsString(v);
+ }
+ else {
+ PyObject *v = PyObject_Str(value);
+ if (v == NULL)
+ return NULL;
+ if (PyList_Append(tmp, v) != 0) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ Py_DECREF(v);
+ return PyBytes_AsString(v);
+ }
+}
+
+
+
#define ARGSZ 64
static char *
Merge(PyObject *args)
{
- PyObject *tmp = NULL;
- char *argvStore[ARGSZ];
- char **argv = NULL;
- int fvStore[ARGSZ];
- int *fv = NULL;
- int argc = 0, fvc = 0, i;
- char *res = NULL;
-
- if (!(tmp = PyList_New(0)))
- return NULL;
-
- argv = argvStore;
- fv = fvStore;
-
- if (args == NULL)
- argc = 0;
-
- else if (!PyTuple_Check(args)) {
- argc = 1;
- fv[0] = 0;
- if (!(argv[0] = AsString(args, tmp)))
- goto finally;
- }
- else {
- argc = PyTuple_Size(args);
-
- if (argc > ARGSZ) {
- argv = (char **)ckalloc(argc * sizeof(char *));
- fv = (int *)ckalloc(argc * sizeof(int));
- if (argv == NULL || fv == NULL) {
- PyErr_NoMemory();
- goto finally;
- }
- }
-
- for (i = 0; i < argc; i++) {
- PyObject *v = PyTuple_GetItem(args, i);
- if (PyTuple_Check(v)) {
- fv[i] = 1;
- if (!(argv[i] = Merge(v)))
- goto finally;
- fvc++;
- }
- else if (v == Py_None) {
- argc = i;
- break;
- }
- else {
- fv[i] = 0;
- if (!(argv[i] = AsString(v, tmp)))
- goto finally;
- fvc++;
- }
- }
- }
- res = Tcl_Merge(argc, argv);
- if (res == NULL)
- PyErr_SetString(Tkinter_TclError, "merge failed");
+ PyObject *tmp = NULL;
+ char *argvStore[ARGSZ];
+ char **argv = NULL;
+ int fvStore[ARGSZ];
+ int *fv = NULL;
+ int argc = 0, fvc = 0, i;
+ char *res = NULL;
+
+ if (!(tmp = PyList_New(0)))
+ return NULL;
+
+ argv = argvStore;
+ fv = fvStore;
+
+ if (args == NULL)
+ argc = 0;
+
+ else if (!PyTuple_Check(args)) {
+ argc = 1;
+ fv[0] = 0;
+ if (!(argv[0] = AsString(args, tmp)))
+ goto finally;
+ }
+ else {
+ argc = PyTuple_Size(args);
+
+ if (argc > ARGSZ) {
+ argv = (char **)ckalloc(argc * sizeof(char *));
+ fv = (int *)ckalloc(argc * sizeof(int));
+ if (argv == NULL || fv == NULL) {
+ PyErr_NoMemory();
+ goto finally;
+ }
+ }
+
+ for (i = 0; i < argc; i++) {
+ PyObject *v = PyTuple_GetItem(args, i);
+ if (PyTuple_Check(v)) {
+ fv[i] = 1;
+ if (!(argv[i] = Merge(v)))
+ goto finally;
+ fvc++;
+ }
+ else if (v == Py_None) {
+ argc = i;
+ break;
+ }
+ else {
+ fv[i] = 0;
+ if (!(argv[i] = AsString(v, tmp)))
+ goto finally;
+ fvc++;
+ }
+ }
+ }
+ res = Tcl_Merge(argc, argv);
+ if (res == NULL)
+ PyErr_SetString(Tkinter_TclError, "merge failed");
finally:
- for (i = 0; i < fvc; i++)
- if (fv[i]) {
- ckfree(argv[i]);
- }
- if (argv != argvStore)
- ckfree(FREECAST argv);
- if (fv != fvStore)
- ckfree(FREECAST fv);
+ for (i = 0; i < fvc; i++)
+ if (fv[i]) {
+ ckfree(argv[i]);
+ }
+ if (argv != argvStore)
+ ckfree(FREECAST argv);
+ if (fv != fvStore)
+ ckfree(FREECAST fv);
- Py_DECREF(tmp);
- return res;
+ Py_DECREF(tmp);
+ return res;
}
-
+
static PyObject *
Split(char *list)
{
- int argc;
- char **argv;
- PyObject *v;
-
- if (list == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
- /* Not a list.
- * Could be a quoted string containing funnies, e.g. {"}.
- * Return the string itself.
- */
- return PyUnicode_FromString(list);
- }
-
- if (argc == 0)
- v = PyUnicode_FromString("");
- else if (argc == 1)
- v = PyUnicode_FromString(argv[0]);
- else if ((v = PyTuple_New(argc)) != NULL) {
- int i;
- PyObject *w;
-
- for (i = 0; i < argc; i++) {
- if ((w = Split(argv[i])) == NULL) {
- Py_DECREF(v);
- v = NULL;
- break;
- }
- PyTuple_SetItem(v, i, w);
- }
- }
- Tcl_Free(FREECAST argv);
- return v;
+ int argc;
+ char **argv;
+ PyObject *v;
+
+ if (list == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
+ /* Not a list.
+ * Could be a quoted string containing funnies, e.g. {"}.
+ * Return the string itself.
+ */
+ return PyUnicode_FromString(list);
+ }
+
+ if (argc == 0)
+ v = PyUnicode_FromString("");
+ else if (argc == 1)
+ v = PyUnicode_FromString(argv[0]);
+ else if ((v = PyTuple_New(argc)) != NULL) {
+ int i;
+ PyObject *w;
+
+ for (i = 0; i < argc; i++) {
+ if ((w = Split(argv[i])) == NULL) {
+ Py_DECREF(v);
+ v = NULL;
+ break;
+ }
+ PyTuple_SetItem(v, i, w);
+ }
+ }
+ Tcl_Free(FREECAST argv);
+ return v;
}
/* In some cases, Tcl will still return strings that are supposed to be
@@ -494,103 +494,103 @@ Split(char *list)
static PyObject *
SplitObj(PyObject *arg)
{
- if (PyTuple_Check(arg)) {
- int i, size;
- PyObject *elem, *newelem, *result;
-
- size = PyTuple_Size(arg);
- result = NULL;
- /* Recursively invoke SplitObj for all tuple items.
- If this does not return a new object, no action is
- needed. */
- for(i = 0; i < size; i++) {
- elem = PyTuple_GetItem(arg, i);
- newelem = SplitObj(elem);
- if (!newelem) {
- Py_XDECREF(result);
- return NULL;
- }
- if (!result) {
- int k;
- if (newelem == elem) {
- Py_DECREF(newelem);
- continue;
- }
- result = PyTuple_New(size);
- if (!result)
- return NULL;
- for(k = 0; k < i; k++) {
- elem = PyTuple_GetItem(arg, k);
- Py_INCREF(elem);
- PyTuple_SetItem(result, k, elem);
- }
- }
- PyTuple_SetItem(result, i, newelem);
- }
- if (result)
- return result;
- /* Fall through, returning arg. */
- }
- else if (PyBytes_Check(arg)) {
- int argc;
- char **argv;
- char *list = PyBytes_AsString(arg);
-
- if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
- Py_INCREF(arg);
- return arg;
- }
- Tcl_Free(FREECAST argv);
- if (argc > 1)
- return Split(PyBytes_AsString(arg));
- /* Fall through, returning arg. */
- }
- Py_INCREF(arg);
- return arg;
-}
-
-
+ if (PyTuple_Check(arg)) {
+ int i, size;
+ PyObject *elem, *newelem, *result;
+
+ size = PyTuple_Size(arg);
+ result = NULL;
+ /* Recursively invoke SplitObj for all tuple items.
+ If this does not return a new object, no action is
+ needed. */
+ for(i = 0; i < size; i++) {
+ elem = PyTuple_GetItem(arg, i);
+ newelem = SplitObj(elem);
+ if (!newelem) {
+ Py_XDECREF(result);
+ return NULL;
+ }
+ if (!result) {
+ int k;
+ if (newelem == elem) {
+ Py_DECREF(newelem);
+ continue;
+ }
+ result = PyTuple_New(size);
+ if (!result)
+ return NULL;
+ for(k = 0; k < i; k++) {
+ elem = PyTuple_GetItem(arg, k);
+ Py_INCREF(elem);
+ PyTuple_SetItem(result, k, elem);
+ }
+ }
+ PyTuple_SetItem(result, i, newelem);
+ }
+ if (result)
+ return result;
+ /* Fall through, returning arg. */
+ }
+ else if (PyBytes_Check(arg)) {
+ int argc;
+ char **argv;
+ char *list = PyBytes_AsString(arg);
+
+ if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
+ Py_INCREF(arg);
+ return arg;
+ }
+ Tcl_Free(FREECAST argv);
+ if (argc > 1)
+ return Split(PyBytes_AsString(arg));
+ /* Fall through, returning arg. */
+ }
+ Py_INCREF(arg);
+ return arg;
+}
+
+
/**** Tkapp Object ****/
#ifndef WITH_APPINIT
int
Tcl_AppInit(Tcl_Interp *interp)
{
- const char * _tkinter_skip_tk_init;
+ const char * _tkinter_skip_tk_init;
- if (Tcl_Init(interp) == TCL_ERROR) {
- PySys_WriteStderr("Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
- return TCL_ERROR;
- }
+ if (Tcl_Init(interp) == TCL_ERROR) {
+ PySys_WriteStderr("Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
+ return TCL_ERROR;
+ }
- _tkinter_skip_tk_init = Tcl_GetVar(interp,
- "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
- if (_tkinter_skip_tk_init != NULL &&
- strcmp(_tkinter_skip_tk_init, "1") == 0) {
- return TCL_OK;
- }
+ _tkinter_skip_tk_init = Tcl_GetVar(interp,
+ "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
+ if (_tkinter_skip_tk_init != NULL &&
+ strcmp(_tkinter_skip_tk_init, "1") == 0) {
+ return TCL_OK;
+ }
#ifdef TKINTER_PROTECT_LOADTK
- if (tk_load_failed) {
- PySys_WriteStderr("Tk_Init error: %s\n", TKINTER_LOADTK_ERRMSG);
- return TCL_ERROR;
- }
+ if (tk_load_failed) {
+ PySys_WriteStderr("Tk_Init error: %s\n", TKINTER_LOADTK_ERRMSG);
+ return TCL_ERROR;
+ }
#endif
- if (Tk_Init(interp) == TCL_ERROR) {
+ if (Tk_Init(interp) == TCL_ERROR) {
#ifdef TKINTER_PROTECT_LOADTK
- tk_load_failed = 1;
+ tk_load_failed = 1;
#endif
- PySys_WriteStderr("Tk_Init error: %s\n", Tcl_GetStringResult(interp));
- return TCL_ERROR;
- }
+ PySys_WriteStderr("Tk_Init error: %s\n", Tcl_GetStringResult(interp));
+ return TCL_ERROR;
+ }
- return TCL_OK;
+ return TCL_OK;
}
#endif /* !WITH_APPINIT */
-
+
/* Initialize the Tk application; see the `main' function in
* `tkMain.c'.
@@ -601,189 +601,189 @@ static void DisableEventHook(void); /* Forward */
static TkappObject *
Tkapp_New(char *screenName, char *className,
- int interactive, int wantobjects, int wantTk, int sync, char *use)
+ int interactive, int wantobjects, int wantTk, int sync, char *use)
{
- TkappObject *v;
- char *argv0;
+ TkappObject *v;
+ char *argv0;
- v = PyObject_New(TkappObject, &Tkapp_Type);
- if (v == NULL)
- return NULL;
+ v = PyObject_New(TkappObject, &Tkapp_Type);
+ if (v == NULL)
+ return NULL;
- v->interp = Tcl_CreateInterp();
- v->wantobjects = wantobjects;
- v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded",
- TCL_GLOBAL_ONLY) != NULL;
- v->thread_id = Tcl_GetCurrentThread();
- v->dispatching = 0;
+ v->interp = Tcl_CreateInterp();
+ v->wantobjects = wantobjects;
+ v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded",
+ TCL_GLOBAL_ONLY) != NULL;
+ v->thread_id = Tcl_GetCurrentThread();
+ v->dispatching = 0;
#ifndef TCL_THREADS
- if (v->threaded) {
- PyErr_SetString(PyExc_RuntimeError, "Tcl is threaded but _tkinter is not");
- Py_DECREF(v);
- return 0;
- }
+ if (v->threaded) {
+ PyErr_SetString(PyExc_RuntimeError, "Tcl is threaded but _tkinter is not");
+ Py_DECREF(v);
+ return 0;
+ }
#endif
#ifdef WITH_THREAD
- if (v->threaded && tcl_lock) {
- /* If Tcl is threaded, we don't need the lock. */
- PyThread_free_lock(tcl_lock);
- tcl_lock = NULL;
- }
+ if (v->threaded && tcl_lock) {
+ /* If Tcl is threaded, we don't need the lock. */
+ PyThread_free_lock(tcl_lock);
+ tcl_lock = NULL;
+ }
#endif
- v->BooleanType = Tcl_GetObjType("boolean");
- v->ByteArrayType = Tcl_GetObjType("bytearray");
- v->DoubleType = Tcl_GetObjType("double");
- v->IntType = Tcl_GetObjType("int");
- v->ListType = Tcl_GetObjType("list");
- v->ProcBodyType = Tcl_GetObjType("procbody");
- v->StringType = Tcl_GetObjType("string");
-
- /* Delete the 'exit' command, which can screw things up */
- Tcl_DeleteCommand(v->interp, "exit");
-
- if (screenName != NULL)
- Tcl_SetVar2(v->interp, "env", "DISPLAY",
- screenName, TCL_GLOBAL_ONLY);
-
- if (interactive)
- Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
- else
- Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
-
- /* This is used to get the application class for Tk 4.1 and up */
- argv0 = (char*)ckalloc(strlen(className) + 1);
- if (!argv0) {
- PyErr_NoMemory();
- Py_DECREF(v);
- return NULL;
- }
-
- strcpy(argv0, className);
- if (isupper(Py_CHARMASK(argv0[0])))
- argv0[0] = tolower(Py_CHARMASK(argv0[0]));
- Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
- ckfree(argv0);
-
- if (! wantTk) {
- Tcl_SetVar(v->interp,
- "_tkinter_skip_tk_init", "1", TCL_GLOBAL_ONLY);
- }
+ v->BooleanType = Tcl_GetObjType("boolean");
+ v->ByteArrayType = Tcl_GetObjType("bytearray");
+ v->DoubleType = Tcl_GetObjType("double");
+ v->IntType = Tcl_GetObjType("int");
+ v->ListType = Tcl_GetObjType("list");
+ v->ProcBodyType = Tcl_GetObjType("procbody");
+ v->StringType = Tcl_GetObjType("string");
+
+ /* Delete the 'exit' command, which can screw things up */
+ Tcl_DeleteCommand(v->interp, "exit");
+
+ if (screenName != NULL)
+ Tcl_SetVar2(v->interp, "env", "DISPLAY",
+ screenName, TCL_GLOBAL_ONLY);
+
+ if (interactive)
+ Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
+ else
+ Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
+
+ /* This is used to get the application class for Tk 4.1 and up */
+ argv0 = (char*)ckalloc(strlen(className) + 1);
+ if (!argv0) {
+ PyErr_NoMemory();
+ Py_DECREF(v);
+ return NULL;
+ }
+
+ strcpy(argv0, className);
+ if (isupper(Py_CHARMASK(argv0[0])))
+ argv0[0] = tolower(Py_CHARMASK(argv0[0]));
+ Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
+ ckfree(argv0);
+
+ if (! wantTk) {
+ Tcl_SetVar(v->interp,
+ "_tkinter_skip_tk_init", "1", TCL_GLOBAL_ONLY);
+ }
#ifdef TKINTER_PROTECT_LOADTK
- else if (tk_load_failed) {
- Tcl_SetVar(v->interp,
- "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
- }
+ else if (tk_load_failed) {
+ Tcl_SetVar(v->interp,
+ "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
+ }
#endif
- /* some initial arguments need to be in argv */
- if (sync || use) {
- char *args;
- int len = 0;
-
- if (sync)
- len += sizeof "-sync";
- if (use)
- len += strlen(use) + sizeof "-use ";
-
- args = (char*)ckalloc(len);
- if (!args) {
- PyErr_NoMemory();
- Py_DECREF(v);
- return NULL;
- }
-
- args[0] = '\0';
- if (sync)
- strcat(args, "-sync");
- if (use) {
- if (sync)
- strcat(args, " ");
- strcat(args, "-use ");
- strcat(args, use);
- }
-
- Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY);
- ckfree(args);
- }
-
- if (Tcl_AppInit(v->interp) != TCL_OK) {
- PyObject *result = Tkinter_Error((PyObject *)v);
+ /* some initial arguments need to be in argv */
+ if (sync || use) {
+ char *args;
+ int len = 0;
+
+ if (sync)
+ len += sizeof "-sync";
+ if (use)
+ len += strlen(use) + sizeof "-use ";
+
+ args = (char*)ckalloc(len);
+ if (!args) {
+ PyErr_NoMemory();
+ Py_DECREF(v);
+ return NULL;
+ }
+
+ args[0] = '\0';
+ if (sync)
+ strcat(args, "-sync");
+ if (use) {
+ if (sync)
+ strcat(args, " ");
+ strcat(args, "-use ");
+ strcat(args, use);
+ }
+
+ Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY);
+ ckfree(args);
+ }
+
+ if (Tcl_AppInit(v->interp) != TCL_OK) {
+ PyObject *result = Tkinter_Error((PyObject *)v);
#ifdef TKINTER_PROTECT_LOADTK
- if (wantTk) {
- const char *_tkinter_tk_failed;
- _tkinter_tk_failed = Tcl_GetVar(v->interp,
- "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
-
- if ( _tkinter_tk_failed != NULL &&
- strcmp(_tkinter_tk_failed, "1") == 0) {
- tk_load_failed = 1;
- }
- }
+ if (wantTk) {
+ const char *_tkinter_tk_failed;
+ _tkinter_tk_failed = Tcl_GetVar(v->interp,
+ "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
+
+ if ( _tkinter_tk_failed != NULL &&
+ strcmp(_tkinter_tk_failed, "1") == 0) {
+ tk_load_failed = 1;
+ }
+ }
#endif
- Py_DECREF((PyObject *)v);
- return (TkappObject *)result;
- }
+ Py_DECREF((PyObject *)v);
+ return (TkappObject *)result;
+ }
- EnableEventHook();
+ EnableEventHook();
- return v;
+ return v;
}
#ifdef WITH_THREAD
static void
Tkapp_ThreadSend(TkappObject *self, Tcl_Event *ev,
- Tcl_Condition *cond, Tcl_Mutex *mutex)
+ Tcl_Condition *cond, Tcl_Mutex *mutex)
{
- Py_BEGIN_ALLOW_THREADS;
- Tcl_MutexLock(mutex);
- Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL);
- Tcl_ThreadAlert(self->thread_id);
- Tcl_ConditionWait(cond, mutex, NULL);
- Tcl_MutexUnlock(mutex);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS;
+ Tcl_MutexLock(mutex);
+ Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL);
+ Tcl_ThreadAlert(self->thread_id);
+ Tcl_ConditionWait(cond, mutex, NULL);
+ Tcl_MutexUnlock(mutex);
+ Py_END_ALLOW_THREADS
}
#endif
-
+
/** Tcl Eval **/
typedef struct {
- PyObject_HEAD
- Tcl_Obj *value;
- PyObject *string; /* This cannot cause cycles. */
+ PyObject_HEAD
+ Tcl_Obj *value;
+ PyObject *string; /* This cannot cause cycles. */
} PyTclObject;
static PyTypeObject PyTclObject_Type;
-#define PyTclObject_Check(v) ((v)->ob_type == &PyTclObject_Type)
+#define PyTclObject_Check(v) ((v)->ob_type == &PyTclObject_Type)
static PyObject *
newPyTclObject(Tcl_Obj *arg)
{
- PyTclObject *self;
- self = PyObject_New(PyTclObject, &PyTclObject_Type);
- if (self == NULL)
- return NULL;
- Tcl_IncrRefCount(arg);
- self->value = arg;
- self->string = NULL;
- return (PyObject*)self;
+ PyTclObject *self;
+ self = PyObject_New(PyTclObject, &PyTclObject_Type);
+ if (self == NULL)
+ return NULL;
+ Tcl_IncrRefCount(arg);
+ self->value = arg;
+ self->string = NULL;
+ return (PyObject*)self;
}
static void
PyTclObject_dealloc(PyTclObject *self)
{
- Tcl_DecrRefCount(self->value);
- Py_XDECREF(self->string);
- PyObject_Del(self);
+ Tcl_DecrRefCount(self->value);
+ Py_XDECREF(self->string);
+ PyObject_Del(self);
}
static char*
PyTclObject_TclString(PyObject *self)
{
- return Tcl_GetString(((PyTclObject*)self)->value);
+ return Tcl_GetString(((PyTclObject*)self)->value);
}
/* Like _str, but create Unicode if necessary. */
@@ -793,37 +793,37 @@ PyDoc_STRVAR(PyTclObject_string__doc__,
static PyObject *
PyTclObject_string(PyTclObject *self, void *ignored)
{
- char *s;
- int len;
- if (!self->string) {
- s = Tcl_GetStringFromObj(self->value, &len);
- self->string = PyUnicode_FromStringAndSize(s, len);
- if (!self->string)
- return NULL;
- }
- Py_INCREF(self->string);
- return self->string;
+ char *s;
+ int len;
+ if (!self->string) {
+ s = Tcl_GetStringFromObj(self->value, &len);
+ self->string = PyUnicode_FromStringAndSize(s, len);
+ if (!self->string)
+ return NULL;
+ }
+ Py_INCREF(self->string);
+ return self->string;
}
static PyObject *
PyTclObject_str(PyTclObject *self, void *ignored)
{
- char *s;
- int len;
- if (self->string && PyUnicode_Check(self->string)) {
- Py_INCREF(self->string);
- return self->string;
- }
- /* XXX Could chache result if it is non-ASCII. */
- s = Tcl_GetStringFromObj(self->value, &len);
- return PyUnicode_DecodeUTF8(s, len, "strict");
+ char *s;
+ int len;
+ if (self->string && PyUnicode_Check(self->string)) {
+ Py_INCREF(self->string);
+ return self->string;
+ }
+ /* XXX Could chache result if it is non-ASCII. */
+ s = Tcl_GetStringFromObj(self->value, &len);
+ return PyUnicode_DecodeUTF8(s, len, "strict");
}
static PyObject *
PyTclObject_repr(PyTclObject *self)
{
- return PyUnicode_FromFormat("<%s object at %p>",
- self->value->typePtr->name, self->value);
+ return PyUnicode_FromFormat("<%s object at %p>",
+ self->value->typePtr->name, self->value);
}
#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
@@ -831,54 +831,54 @@ PyTclObject_repr(PyTclObject *self)
static PyObject *
PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
{
- int result;
- PyObject *v;
-
- /* neither argument should be NULL, unless something's gone wrong */
- if (self == NULL || other == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- /* both arguments should be instances of PyTclObject */
- if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
- v = Py_NotImplemented;
- goto finished;
- }
-
- if (self == other)
- /* fast path when self and other are identical */
- result = 0;
- else
- result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
- Tcl_GetString(((PyTclObject *)other)->value));
- /* Convert return value to a Boolean */
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- case Py_LE:
- v = TEST_COND(result <= 0);
- break;
- case Py_GE:
- v = TEST_COND(result >= 0);
- break;
- case Py_LT:
- v = TEST_COND(result < 0);
- break;
- case Py_GT:
- v = TEST_COND(result > 0);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
+ int result;
+ PyObject *v;
+
+ /* neither argument should be NULL, unless something's gone wrong */
+ if (self == NULL || other == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ /* both arguments should be instances of PyTclObject */
+ if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
+ v = Py_NotImplemented;
+ goto finished;
+ }
+
+ if (self == other)
+ /* fast path when self and other are identical */
+ result = 0;
+ else
+ result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
+ Tcl_GetString(((PyTclObject *)other)->value));
+ /* Convert return value to a Boolean */
+ switch (op) {
+ case Py_EQ:
+ v = TEST_COND(result == 0);
+ break;
+ case Py_NE:
+ v = TEST_COND(result != 0);
+ break;
+ case Py_LE:
+ v = TEST_COND(result <= 0);
+ break;
+ case Py_GE:
+ v = TEST_COND(result >= 0);
+ break;
+ case Py_LT:
+ v = TEST_COND(result < 0);
+ break;
+ case Py_GT:
+ v = TEST_COND(result > 0);
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
finished:
- Py_INCREF(v);
- return v;
+ Py_INCREF(v);
+ return v;
}
PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
@@ -886,230 +886,230 @@ PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
static PyObject*
get_typename(PyTclObject* obj, void* ignored)
{
- return PyUnicode_FromString(obj->value->typePtr->name);
+ return PyUnicode_FromString(obj->value->typePtr->name);
}
static PyGetSetDef PyTclObject_getsetlist[] = {
- {"typename", (getter)get_typename, NULL, get_typename__doc__},
- {"string", (getter)PyTclObject_string, NULL,
- PyTclObject_string__doc__},
- {0},
+ {"typename", (getter)get_typename, NULL, get_typename__doc__},
+ {"string", (getter)PyTclObject_string, NULL,
+ PyTclObject_string__doc__},
+ {0},
};
static PyTypeObject PyTclObject_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_tkinter.Tcl_Obj", /*tp_name*/
- sizeof(PyTclObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)PyTclObject_dealloc,/*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- (reprfunc)PyTclObject_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- (reprfunc)PyTclObject_str, /*tp_str*/
- PyObject_GenericGetAttr, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- PyTclObject_richcompare, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- 0, /*tp_methods*/
- 0, /*tp_members*/
- PyTclObject_getsetlist, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_tkinter.Tcl_Obj", /*tp_name*/
+ sizeof(PyTclObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)PyTclObject_dealloc,/*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ (reprfunc)PyTclObject_repr, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ (reprfunc)PyTclObject_str, /*tp_str*/
+ PyObject_GenericGetAttr, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ PyTclObject_richcompare, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ PyTclObject_getsetlist, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
};
static Tcl_Obj*
AsObj(PyObject *value)
{
- Tcl_Obj *result;
- long longVal;
- int overflow;
-
- if (PyBytes_Check(value))
- return Tcl_NewStringObj(PyBytes_AS_STRING(value),
- PyBytes_GET_SIZE(value));
- else if (PyBool_Check(value))
- return Tcl_NewBooleanObj(PyObject_IsTrue(value));
- else if (PyLong_CheckExact(value) &&
- ((longVal = PyLong_AsLongAndOverflow(value, &overflow)),
- !overflow)) {
- /* If there is an overflow in the long conversion,
- fall through to default object handling. */
- return Tcl_NewLongObj(longVal);
- }
- else if (PyFloat_Check(value))
- return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
- else if (PyTuple_Check(value)) {
- Tcl_Obj **argv = (Tcl_Obj**)
- ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*));
- int i;
- if(!argv)
- return 0;
- for(i=0;i<PyTuple_Size(value);i++)
- argv[i] = AsObj(PyTuple_GetItem(value,i));
- result = Tcl_NewListObj(PyTuple_Size(value), argv);
- ckfree(FREECAST argv);
- return result;
- }
- else if (PyUnicode_Check(value)) {
- Py_UNICODE *inbuf = PyUnicode_AS_UNICODE(value);
- Py_ssize_t size = PyUnicode_GET_SIZE(value);
- /* This #ifdef assumes that Tcl uses UCS-2.
- See TCL_UTF_MAX test above. */
+ Tcl_Obj *result;
+ long longVal;
+ int overflow;
+
+ if (PyBytes_Check(value))
+ return Tcl_NewStringObj(PyBytes_AS_STRING(value),
+ PyBytes_GET_SIZE(value));
+ else if (PyBool_Check(value))
+ return Tcl_NewBooleanObj(PyObject_IsTrue(value));
+ else if (PyLong_CheckExact(value) &&
+ ((longVal = PyLong_AsLongAndOverflow(value, &overflow)),
+ !overflow)) {
+ /* If there is an overflow in the long conversion,
+ fall through to default object handling. */
+ return Tcl_NewLongObj(longVal);
+ }
+ else if (PyFloat_Check(value))
+ return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
+ else if (PyTuple_Check(value)) {
+ Tcl_Obj **argv = (Tcl_Obj**)
+ ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*));
+ int i;
+ if(!argv)
+ return 0;
+ for(i=0;i<PyTuple_Size(value);i++)
+ argv[i] = AsObj(PyTuple_GetItem(value,i));
+ result = Tcl_NewListObj(PyTuple_Size(value), argv);
+ ckfree(FREECAST argv);
+ return result;
+ }
+ else if (PyUnicode_Check(value)) {
+ Py_UNICODE *inbuf = PyUnicode_AS_UNICODE(value);
+ Py_ssize_t size = PyUnicode_GET_SIZE(value);
+ /* This #ifdef assumes that Tcl uses UCS-2.
+ See TCL_UTF_MAX test above. */
#if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX == 3
- Tcl_UniChar *outbuf = NULL;
- Py_ssize_t i;
- size_t allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
- if (allocsize >= size)
- outbuf = (Tcl_UniChar*)ckalloc(allocsize);
- /* Else overflow occurred, and we take the next exit */
- if (!outbuf) {
- PyErr_NoMemory();
- return NULL;
- }
- for (i = 0; i < size; i++) {
- if (inbuf[i] >= 0x10000) {
- /* Tcl doesn't do UTF-16, yet. */
- PyErr_SetString(PyExc_ValueError,
- "unsupported character");
- ckfree(FREECAST outbuf);
- return NULL;
- }
- outbuf[i] = inbuf[i];
- }
- result = Tcl_NewUnicodeObj(outbuf, size);
- ckfree(FREECAST outbuf);
- return result;
+ Tcl_UniChar *outbuf = NULL;
+ Py_ssize_t i;
+ size_t allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
+ if (allocsize >= size)
+ outbuf = (Tcl_UniChar*)ckalloc(allocsize);
+ /* Else overflow occurred, and we take the next exit */
+ if (!outbuf) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ for (i = 0; i < size; i++) {
+ if (inbuf[i] >= 0x10000) {
+ /* Tcl doesn't do UTF-16, yet. */
+ PyErr_SetString(PyExc_ValueError,
+ "unsupported character");
+ ckfree(FREECAST outbuf);
+ return NULL;
+ }
+ outbuf[i] = inbuf[i];
+ }
+ result = Tcl_NewUnicodeObj(outbuf, size);
+ ckfree(FREECAST outbuf);
+ return result;
#else
- return Tcl_NewUnicodeObj(inbuf, size);
+ return Tcl_NewUnicodeObj(inbuf, size);
#endif
- }
- else if(PyTclObject_Check(value)) {
- Tcl_Obj *v = ((PyTclObject*)value)->value;
- Tcl_IncrRefCount(v);
- return v;
- }
- else {
- PyObject *v = PyObject_Str(value);
- if (!v)
- return 0;
- result = AsObj(v);
- Py_DECREF(v);
- return result;
- }
+ }
+ else if(PyTclObject_Check(value)) {
+ Tcl_Obj *v = ((PyTclObject*)value)->value;
+ Tcl_IncrRefCount(v);
+ return v;
+ }
+ else {
+ PyObject *v = PyObject_Str(value);
+ if (!v)
+ return 0;
+ result = AsObj(v);
+ Py_DECREF(v);
+ return result;
+ }
}
static PyObject*
FromObj(PyObject* tkapp, Tcl_Obj *value)
{
- PyObject *result = NULL;
- TkappObject *app = (TkappObject*)tkapp;
-
- if (value->typePtr == NULL) {
- return PyUnicode_FromStringAndSize(value->bytes,
- value->length);
- }
-
- if (value->typePtr == app->BooleanType) {
- result = value->internalRep.longValue ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
- }
-
- if (value->typePtr == app->ByteArrayType) {
- int size;
- char *data = (char*)Tcl_GetByteArrayFromObj(value, &size);
- return PyBytes_FromStringAndSize(data, size);
- }
-
- if (value->typePtr == app->DoubleType) {
- return PyFloat_FromDouble(value->internalRep.doubleValue);
- }
-
- if (value->typePtr == app->IntType) {
- return PyLong_FromLong(value->internalRep.longValue);
- }
-
- if (value->typePtr == app->ListType) {
- int size;
- int i, status;
- PyObject *elem;
- Tcl_Obj *tcl_elem;
-
- status = Tcl_ListObjLength(Tkapp_Interp(tkapp), value, &size);
- if (status == TCL_ERROR)
- return Tkinter_Error(tkapp);
- result = PyTuple_New(size);
- if (!result)
- return NULL;
- for (i = 0; i < size; i++) {
- status = Tcl_ListObjIndex(Tkapp_Interp(tkapp),
- value, i, &tcl_elem);
- if (status == TCL_ERROR) {
- Py_DECREF(result);
- return Tkinter_Error(tkapp);
- }
- elem = FromObj(tkapp, tcl_elem);
- if (!elem) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SetItem(result, i, elem);
- }
- return result;
- }
-
- if (value->typePtr == app->ProcBodyType) {
- /* fall through: return tcl object. */
- }
-
- if (value->typePtr == app->StringType) {
+ PyObject *result = NULL;
+ TkappObject *app = (TkappObject*)tkapp;
+
+ if (value->typePtr == NULL) {
+ return PyUnicode_FromStringAndSize(value->bytes,
+ value->length);
+ }
+
+ if (value->typePtr == app->BooleanType) {
+ result = value->internalRep.longValue ? Py_True : Py_False;
+ Py_INCREF(result);
+ return result;
+ }
+
+ if (value->typePtr == app->ByteArrayType) {
+ int size;
+ char *data = (char*)Tcl_GetByteArrayFromObj(value, &size);
+ return PyBytes_FromStringAndSize(data, size);
+ }
+
+ if (value->typePtr == app->DoubleType) {
+ return PyFloat_FromDouble(value->internalRep.doubleValue);
+ }
+
+ if (value->typePtr == app->IntType) {
+ return PyLong_FromLong(value->internalRep.longValue);
+ }
+
+ if (value->typePtr == app->ListType) {
+ int size;
+ int i, status;
+ PyObject *elem;
+ Tcl_Obj *tcl_elem;
+
+ status = Tcl_ListObjLength(Tkapp_Interp(tkapp), value, &size);
+ if (status == TCL_ERROR)
+ return Tkinter_Error(tkapp);
+ result = PyTuple_New(size);
+ if (!result)
+ return NULL;
+ for (i = 0; i < size; i++) {
+ status = Tcl_ListObjIndex(Tkapp_Interp(tkapp),
+ value, i, &tcl_elem);
+ if (status == TCL_ERROR) {
+ Py_DECREF(result);
+ return Tkinter_Error(tkapp);
+ }
+ elem = FromObj(tkapp, tcl_elem);
+ if (!elem) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyTuple_SetItem(result, i, elem);
+ }
+ return result;
+ }
+
+ if (value->typePtr == app->ProcBodyType) {
+ /* fall through: return tcl object. */
+ }
+
+ if (value->typePtr == app->StringType) {
#if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==3
- PyObject *result;
- int size;
- Tcl_UniChar *input;
- Py_UNICODE *output;
-
- size = Tcl_GetCharLength(value);
- result = PyUnicode_FromUnicode(NULL, size);
- if (!result)
- return NULL;
- input = Tcl_GetUnicode(value);
- output = PyUnicode_AS_UNICODE(result);
- while (size--)
- *output++ = *input++;
- return result;
+ PyObject *result;
+ int size;
+ Tcl_UniChar *input;
+ Py_UNICODE *output;
+
+ size = Tcl_GetCharLength(value);
+ result = PyUnicode_FromUnicode(NULL, size);
+ if (!result)
+ return NULL;
+ input = Tcl_GetUnicode(value);
+ output = PyUnicode_AS_UNICODE(result);
+ while (size--)
+ *output++ = *input++;
+ return result;
#else
- return PyUnicode_FromUnicode(Tcl_GetUnicode(value),
- Tcl_GetCharLength(value));
+ return PyUnicode_FromUnicode(Tcl_GetUnicode(value),
+ Tcl_GetCharLength(value));
#endif
- }
+ }
- return newPyTclObject(value);
+ return newPyTclObject(value);
}
#ifdef WITH_THREAD
@@ -1117,24 +1117,24 @@ FromObj(PyObject* tkapp, Tcl_Obj *value)
TCL_DECLARE_MUTEX(call_mutex)
typedef struct Tkapp_CallEvent {
- Tcl_Event ev; /* Must be first */
- TkappObject *self;
- PyObject *args;
- int flags;
- PyObject **res;
- PyObject **exc_type, **exc_value, **exc_tb;
- Tcl_Condition *done;
+ Tcl_Event ev; /* Must be first */
+ TkappObject *self;
+ PyObject *args;
+ int flags;
+ PyObject **res;
+ PyObject **exc_type, **exc_value, **exc_tb;
+ Tcl_Condition *done;
} Tkapp_CallEvent;
#endif
void
Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc)
{
- int i;
- for (i = 0; i < objc; i++)
- Tcl_DecrRefCount(objv[i]);
- if (objv != objStore)
- ckfree(FREECAST objv);
+ int i;
+ for (i = 0; i < objc; i++)
+ Tcl_DecrRefCount(objv[i]);
+ if (objv != objStore)
+ ckfree(FREECAST objv);
}
/* Convert Python objects to Tcl objects. This must happen in the
@@ -1143,51 +1143,51 @@ Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc)
static Tcl_Obj**
Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
{
- Tcl_Obj **objv = objStore;
- int objc = 0, i;
- if (args == NULL)
- /* do nothing */;
-
- else if (!PyTuple_Check(args)) {
- objv[0] = AsObj(args);
- if (objv[0] == 0)
- goto finally;
- objc = 1;
- Tcl_IncrRefCount(objv[0]);
- }
- else {
- objc = PyTuple_Size(args);
-
- if (objc > ARGSZ) {
- objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *));
- if (objv == NULL) {
- PyErr_NoMemory();
- objc = 0;
- goto finally;
- }
- }
-
- for (i = 0; i < objc; i++) {
- PyObject *v = PyTuple_GetItem(args, i);
- if (v == Py_None) {
- objc = i;
- break;
- }
- objv[i] = AsObj(v);
- if (!objv[i]) {
- /* Reset objc, so it attempts to clear
- objects only up to i. */
- objc = i;
- goto finally;
- }
- Tcl_IncrRefCount(objv[i]);
- }
- }
- *pobjc = objc;
- return objv;
+ Tcl_Obj **objv = objStore;
+ int objc = 0, i;
+ if (args == NULL)
+ /* do nothing */;
+
+ else if (!PyTuple_Check(args)) {
+ objv[0] = AsObj(args);
+ if (objv[0] == 0)
+ goto finally;
+ objc = 1;
+ Tcl_IncrRefCount(objv[0]);
+ }
+ else {
+ objc = PyTuple_Size(args);
+
+ if (objc > ARGSZ) {
+ objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *));
+ if (objv == NULL) {
+ PyErr_NoMemory();
+ objc = 0;
+ goto finally;
+ }
+ }
+
+ for (i = 0; i < objc; i++) {
+ PyObject *v = PyTuple_GetItem(args, i);
+ if (v == Py_None) {
+ objc = i;
+ break;
+ }
+ objv[i] = AsObj(v);
+ if (!objv[i]) {
+ /* Reset objc, so it attempts to clear
+ objects only up to i. */
+ objc = i;
+ goto finally;
+ }
+ Tcl_IncrRefCount(objv[i]);
+ }
+ }
+ *pobjc = objc;
+ return objv;
finally:
- Tkapp_CallDeallocArgs(objv, objStore, objc);
- return NULL;
+ Tkapp_CallDeallocArgs(objv, objStore, objc);
+ return NULL;
}
/* Convert the results of a command call into a Python objects. */
@@ -1195,22 +1195,22 @@ finally:
static PyObject*
Tkapp_CallResult(TkappObject *self)
{
- PyObject *res = NULL;
- if(self->wantobjects) {
- Tcl_Obj *value = Tcl_GetObjResult(self->interp);
- /* Not sure whether the IncrRef is necessary, but something
- may overwrite the interpreter result while we are
- converting it. */
- Tcl_IncrRefCount(value);
- res = FromObj((PyObject*)self, value);
- Tcl_DecrRefCount(value);
- } else {
- const char *s = Tcl_GetStringResult(self->interp);
- const char *p = s;
+ PyObject *res = NULL;
+ if(self->wantobjects) {
+ Tcl_Obj *value = Tcl_GetObjResult(self->interp);
+ /* Not sure whether the IncrRef is necessary, but something
+ may overwrite the interpreter result while we are
+ converting it. */
+ Tcl_IncrRefCount(value);
+ res = FromObj((PyObject*)self, value);
+ Tcl_DecrRefCount(value);
+ } else {
+ const char *s = Tcl_GetStringResult(self->interp);
+ const char *p = s;
- res = PyUnicode_FromStringAndSize(s, (int)(p-s));
- }
- return res;
+ res = PyUnicode_FromStringAndSize(s, (int)(p-s));
+ }
+ return res;
}
#ifdef WITH_THREAD
@@ -1222,41 +1222,41 @@ Tkapp_CallResult(TkappObject *self)
static int
Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
{
- Tcl_Obj *objStore[ARGSZ];
- Tcl_Obj **objv;
- int objc;
- int i;
- ENTER_PYTHON
- objv = Tkapp_CallArgs(e->args, objStore, &objc);
- if (!objv) {
- PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
- *(e->res) = NULL;
- }
- LEAVE_PYTHON
- if (!objv)
- goto done;
- i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags);
- ENTER_PYTHON
- if (i == TCL_ERROR) {
- *(e->res) = NULL;
- *(e->exc_type) = NULL;
- *(e->exc_tb) = NULL;
- *(e->exc_value) = PyObject_CallFunction(
- Tkinter_TclError, "s",
- Tcl_GetStringResult(e->self->interp));
- }
- else {
- *(e->res) = Tkapp_CallResult(e->self);
- }
- LEAVE_PYTHON
-
- Tkapp_CallDeallocArgs(objv, objStore, objc);
+ Tcl_Obj *objStore[ARGSZ];
+ Tcl_Obj **objv;
+ int objc;
+ int i;
+ ENTER_PYTHON
+ objv = Tkapp_CallArgs(e->args, objStore, &objc);
+ if (!objv) {
+ PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb);
+ *(e->res) = NULL;
+ }
+ LEAVE_PYTHON
+ if (!objv)
+ goto done;
+ i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags);
+ ENTER_PYTHON
+ if (i == TCL_ERROR) {
+ *(e->res) = NULL;
+ *(e->exc_type) = NULL;
+ *(e->exc_tb) = NULL;
+ *(e->exc_value) = PyObject_CallFunction(
+ Tkinter_TclError, "s",
+ Tcl_GetStringResult(e->self->interp));
+ }
+ else {
+ *(e->res) = Tkapp_CallResult(e->self);
+ }
+ LEAVE_PYTHON
+
+ Tkapp_CallDeallocArgs(objv, objStore, objc);
done:
- /* Wake up calling thread. */
- Tcl_MutexLock(&call_mutex);
- Tcl_ConditionNotify(e->done);
- Tcl_MutexUnlock(&call_mutex);
- return 1;
+ /* Wake up calling thread. */
+ Tcl_MutexLock(&call_mutex);
+ Tcl_ConditionNotify(e->done);
+ Tcl_MutexUnlock(&call_mutex);
+ return 1;
}
#endif
@@ -1276,218 +1276,218 @@ done:
static PyObject *
Tkapp_Call(PyObject *selfptr, PyObject *args)
{
- Tcl_Obj *objStore[ARGSZ];
- Tcl_Obj **objv = NULL;
- int objc, i;
- PyObject *res = NULL;
- TkappObject *self = (TkappObject*)selfptr;
- int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL;
-
- /* If args is a single tuple, replace with contents of tuple */
- if (1 == PyTuple_Size(args)){
- PyObject* item = PyTuple_GetItem(args, 0);
- if (PyTuple_Check(item))
- args = item;
- }
+ Tcl_Obj *objStore[ARGSZ];
+ Tcl_Obj **objv = NULL;
+ int objc, i;
+ PyObject *res = NULL;
+ TkappObject *self = (TkappObject*)selfptr;
+ int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL;
+
+ /* If args is a single tuple, replace with contents of tuple */
+ if (1 == PyTuple_Size(args)){
+ PyObject* item = PyTuple_GetItem(args, 0);
+ if (PyTuple_Check(item))
+ args = item;
+ }
#ifdef WITH_THREAD
- if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
- /* We cannot call the command directly. Instead, we must
- marshal the parameters to the interpreter thread. */
- Tkapp_CallEvent *ev;
- Tcl_Condition cond = NULL;
- PyObject *exc_type, *exc_value, *exc_tb;
- if (!WaitForMainloop(self))
- return NULL;
- ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent));
- ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
- ev->self = self;
- ev->args = args;
- ev->res = &res;
- ev->exc_type = &exc_type;
- ev->exc_value = &exc_value;
- ev->exc_tb = &exc_tb;
- ev->done = &cond;
-
- Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
-
- if (res == NULL) {
- if (exc_type)
- PyErr_Restore(exc_type, exc_value, exc_tb);
- else
- PyErr_SetObject(Tkinter_TclError, exc_value);
- }
- Tcl_ConditionFinalize(&cond);
- }
- else
+ if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
+ /* We cannot call the command directly. Instead, we must
+ marshal the parameters to the interpreter thread. */
+ Tkapp_CallEvent *ev;
+ Tcl_Condition cond = NULL;
+ PyObject *exc_type, *exc_value, *exc_tb;
+ if (!WaitForMainloop(self))
+ return NULL;
+ ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent));
+ ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
+ ev->self = self;
+ ev->args = args;
+ ev->res = &res;
+ ev->exc_type = &exc_type;
+ ev->exc_value = &exc_value;
+ ev->exc_tb = &exc_tb;
+ ev->done = &cond;
+
+ Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
+
+ if (res == NULL) {
+ if (exc_type)
+ PyErr_Restore(exc_type, exc_value, exc_tb);
+ else
+ PyErr_SetObject(Tkinter_TclError, exc_value);
+ }
+ Tcl_ConditionFinalize(&cond);
+ }
+ else
#endif
- {
+ {
- objv = Tkapp_CallArgs(args, objStore, &objc);
- if (!objv)
- return NULL;
+ objv = Tkapp_CallArgs(args, objStore, &objc);
+ if (!objv)
+ return NULL;
- ENTER_TCL
+ ENTER_TCL
- i = Tcl_EvalObjv(self->interp, objc, objv, flags);
+ i = Tcl_EvalObjv(self->interp, objc, objv, flags);
- ENTER_OVERLAP
+ ENTER_OVERLAP
- if (i == TCL_ERROR)
- Tkinter_Error(selfptr);
- else
- res = Tkapp_CallResult(self);
+ if (i == TCL_ERROR)
+ Tkinter_Error(selfptr);
+ else
+ res = Tkapp_CallResult(self);
- LEAVE_OVERLAP_TCL
+ LEAVE_OVERLAP_TCL
- Tkapp_CallDeallocArgs(objv, objStore, objc);
- }
- return res;
+ Tkapp_CallDeallocArgs(objv, objStore, objc);
+ }
+ return res;
}
static PyObject *
Tkapp_GlobalCall(PyObject *self, PyObject *args)
{
- /* Could do the same here as for Tkapp_Call(), but this is not used
- much, so I can't be bothered. Unfortunately Tcl doesn't export a
- way for the user to do what all its Global* variants do (save and
- reset the scope pointer, call the local version, restore the saved
- scope pointer). */
+ /* Could do the same here as for Tkapp_Call(), but this is not used
+ much, so I can't be bothered. Unfortunately Tcl doesn't export a
+ way for the user to do what all its Global* variants do (save and
+ reset the scope pointer, call the local version, restore the saved
+ scope pointer). */
- char *cmd;
- PyObject *res = NULL;
+ char *cmd;
+ PyObject *res = NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- cmd = Merge(args);
- if (cmd) {
- int err;
- ENTER_TCL
- err = Tcl_GlobalEval(Tkapp_Interp(self), cmd);
- ENTER_OVERLAP
- if (err == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = PyUnicode_FromString(Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- ckfree(cmd);
- }
+ cmd = Merge(args);
+ if (cmd) {
+ int err;
+ ENTER_TCL
+ err = Tcl_GlobalEval(Tkapp_Interp(self), cmd);
+ ENTER_OVERLAP
+ if (err == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = PyUnicode_FromString(Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ ckfree(cmd);
+ }
- return res;
+ return res;
}
static PyObject *
Tkapp_Eval(PyObject *self, PyObject *args)
{
- char *script;
- PyObject *res = NULL;
- int err;
+ char *script;
+ PyObject *res = NULL;
+ int err;
- if (!PyArg_ParseTuple(args, "s:eval", &script))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:eval", &script))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- err = Tcl_Eval(Tkapp_Interp(self), script);
- ENTER_OVERLAP
- if (err == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = PyUnicode_FromString(Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ err = Tcl_Eval(Tkapp_Interp(self), script);
+ ENTER_OVERLAP
+ if (err == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = PyUnicode_FromString(Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_GlobalEval(PyObject *self, PyObject *args)
{
- char *script;
- PyObject *res = NULL;
- int err;
+ char *script;
+ PyObject *res = NULL;
+ int err;
- if (!PyArg_ParseTuple(args, "s:globaleval", &script))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:globaleval", &script))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- err = Tcl_GlobalEval(Tkapp_Interp(self), script);
- ENTER_OVERLAP
- if (err == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = PyUnicode_FromString(Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ err = Tcl_GlobalEval(Tkapp_Interp(self), script);
+ ENTER_OVERLAP
+ if (err == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = PyUnicode_FromString(Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_EvalFile(PyObject *self, PyObject *args)
{
- char *fileName;
- PyObject *res = NULL;
- int err;
+ char *fileName;
+ PyObject *res = NULL;
+ int err;
- if (!PyArg_ParseTuple(args, "s:evalfile", &fileName))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:evalfile", &fileName))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- err = Tcl_EvalFile(Tkapp_Interp(self), fileName);
- ENTER_OVERLAP
- if (err == TCL_ERROR)
- res = Tkinter_Error(self);
+ ENTER_TCL
+ err = Tcl_EvalFile(Tkapp_Interp(self), fileName);
+ ENTER_OVERLAP
+ if (err == TCL_ERROR)
+ res = Tkinter_Error(self);
- else
- res = PyUnicode_FromString(Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- return res;
+ else
+ res = PyUnicode_FromString(Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_Record(PyObject *self, PyObject *args)
{
- char *script;
- PyObject *res = NULL;
- int err;
+ char *script;
+ PyObject *res = NULL;
+ int err;
- if (!PyArg_ParseTuple(args, "s", &script))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s", &script))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL);
- ENTER_OVERLAP
- if (err == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = PyUnicode_FromString(Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL);
+ ENTER_OVERLAP
+ if (err == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = PyUnicode_FromString(Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_AddErrorInfo(PyObject *self, PyObject *args)
{
- char *msg;
+ char *msg;
- if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg))
- return NULL;
- CHECK_TCL_APPARTMENT;
+ if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg))
+ return NULL;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- Tcl_AddErrorInfo(Tkapp_Interp(self), msg);
- LEAVE_TCL
+ ENTER_TCL
+ Tcl_AddErrorInfo(Tkapp_Interp(self), msg);
+ LEAVE_TCL
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
-
+
/** Tcl Variable **/
typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags);
@@ -1496,36 +1496,36 @@ typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags);
TCL_DECLARE_MUTEX(var_mutex)
typedef struct VarEvent {
- Tcl_Event ev; /* must be first */
- PyObject *self;
- PyObject *args;
- int flags;
- EventFunc func;
- PyObject **res;
- PyObject **exc_type;
- PyObject **exc_val;
- Tcl_Condition *cond;
+ Tcl_Event ev; /* must be first */
+ PyObject *self;
+ PyObject *args;
+ int flags;
+ EventFunc func;
+ PyObject **res;
+ PyObject **exc_type;
+ PyObject **exc_val;
+ Tcl_Condition *cond;
} VarEvent;
#endif
static int
varname_converter(PyObject *in, void *_out)
{
- char **out = (char**)_out;
- if (PyBytes_Check(in)) {
- *out = PyBytes_AsString(in);
- return 1;
- }
- if (PyUnicode_Check(in)) {
- *out = _PyUnicode_AsString(in);
- return 1;
- }
- if (PyTclObject_Check(in)) {
- *out = PyTclObject_TclString(in);
- return 1;
- }
- /* XXX: Should give diagnostics. */
- return 0;
+ char **out = (char**)_out;
+ if (PyBytes_Check(in)) {
+ *out = PyBytes_AsString(in);
+ return 1;
+ }
+ if (PyUnicode_Check(in)) {
+ *out = _PyUnicode_AsString(in);
+ return 1;
+ }
+ if (PyTclObject_Check(in)) {
+ *out = PyTclObject_TclString(in);
+ return 1;
+ }
+ /* XXX: Should give diagnostics. */
+ return 0;
}
#ifdef WITH_THREAD
@@ -1533,28 +1533,28 @@ varname_converter(PyObject *in, void *_out)
static void
var_perform(VarEvent *ev)
{
- *(ev->res) = ev->func(ev->self, ev->args, ev->flags);
- if (!*(ev->res)) {
- PyObject *exc, *val, *tb;
- PyErr_Fetch(&exc, &val, &tb);
- PyErr_NormalizeException(&exc, &val, &tb);
- *(ev->exc_type) = exc;
- *(ev->exc_val) = val;
- Py_DECREF(tb);
- }
+ *(ev->res) = ev->func(ev->self, ev->args, ev->flags);
+ if (!*(ev->res)) {
+ PyObject *exc, *val, *tb;
+ PyErr_Fetch(&exc, &val, &tb);
+ PyErr_NormalizeException(&exc, &val, &tb);
+ *(ev->exc_type) = exc;
+ *(ev->exc_val) = val;
+ Py_DECREF(tb);
+ }
}
static int
var_proc(VarEvent* ev, int flags)
{
- ENTER_PYTHON
- var_perform(ev);
- Tcl_MutexLock(&var_mutex);
- Tcl_ConditionNotify(ev->cond);
- Tcl_MutexUnlock(&var_mutex);
- LEAVE_PYTHON
- return 1;
+ ENTER_PYTHON
+ var_perform(ev);
+ Tcl_MutexLock(&var_mutex);
+ Tcl_ConditionNotify(ev->cond);
+ Tcl_MutexUnlock(&var_mutex);
+ LEAVE_PYTHON
+ return 1;
}
#endif
@@ -1563,439 +1563,439 @@ static PyObject*
var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
{
#ifdef WITH_THREAD
- TkappObject *self = (TkappObject*)selfptr;
- if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
- TkappObject *self = (TkappObject*)selfptr;
- VarEvent *ev;
- PyObject *res, *exc_type, *exc_val;
- Tcl_Condition cond = NULL;
-
- /* The current thread is not the interpreter thread. Marshal
- the call to the interpreter thread, then wait for
- completion. */
- if (!WaitForMainloop(self))
- return NULL;
-
- ev = (VarEvent*)ckalloc(sizeof(VarEvent));
-
- ev->self = selfptr;
- ev->args = args;
- ev->flags = flags;
- ev->func = func;
- ev->res = &res;
- ev->exc_type = &exc_type;
- ev->exc_val = &exc_val;
- ev->cond = &cond;
- ev->ev.proc = (Tcl_EventProc*)var_proc;
- Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
- Tcl_ConditionFinalize(&cond);
- if (!res) {
- PyErr_SetObject(exc_type, exc_val);
- Py_DECREF(exc_type);
- Py_DECREF(exc_val);
- return NULL;
- }
- return res;
- }
+ TkappObject *self = (TkappObject*)selfptr;
+ if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
+ TkappObject *self = (TkappObject*)selfptr;
+ VarEvent *ev;
+ PyObject *res, *exc_type, *exc_val;
+ Tcl_Condition cond = NULL;
+
+ /* The current thread is not the interpreter thread. Marshal
+ the call to the interpreter thread, then wait for
+ completion. */
+ if (!WaitForMainloop(self))
+ return NULL;
+
+ ev = (VarEvent*)ckalloc(sizeof(VarEvent));
+
+ ev->self = selfptr;
+ ev->args = args;
+ ev->flags = flags;
+ ev->func = func;
+ ev->res = &res;
+ ev->exc_type = &exc_type;
+ ev->exc_val = &exc_val;
+ ev->cond = &cond;
+ ev->ev.proc = (Tcl_EventProc*)var_proc;
+ Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
+ Tcl_ConditionFinalize(&cond);
+ if (!res) {
+ PyErr_SetObject(exc_type, exc_val);
+ Py_DECREF(exc_type);
+ Py_DECREF(exc_val);
+ return NULL;
+ }
+ return res;
+ }
#endif
- /* Tcl is not threaded, or this is the interpreter thread. */
- return func(selfptr, args, flags);
+ /* Tcl is not threaded, or this is the interpreter thread. */
+ return func(selfptr, args, flags);
}
static PyObject *
SetVar(PyObject *self, PyObject *args, int flags)
{
- char *name1, *name2;
- PyObject *newValue;
- PyObject *res = NULL;
- Tcl_Obj *newval, *ok;
-
- if (PyArg_ParseTuple(args, "O&O:setvar",
- varname_converter, &name1, &newValue)) {
- /* XXX Acquire tcl lock??? */
- newval = AsObj(newValue);
- if (newval == NULL)
- return NULL;
- ENTER_TCL
- ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
- newval, flags);
- ENTER_OVERLAP
- if (!ok)
- Tkinter_Error(self);
- else {
- res = Py_None;
- Py_INCREF(res);
- }
- LEAVE_OVERLAP_TCL
- }
- else {
- PyErr_Clear();
- if (PyArg_ParseTuple(args, "ssO:setvar",
- &name1, &name2, &newValue)) {
- /* XXX must hold tcl lock already??? */
- newval = AsObj(newValue);
- ENTER_TCL
- ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags);
- ENTER_OVERLAP
- if (!ok)
- Tkinter_Error(self);
- else {
- res = Py_None;
- Py_INCREF(res);
- }
- LEAVE_OVERLAP_TCL
- }
- else {
- return NULL;
- }
- }
- return res;
+ char *name1, *name2;
+ PyObject *newValue;
+ PyObject *res = NULL;
+ Tcl_Obj *newval, *ok;
+
+ if (PyArg_ParseTuple(args, "O&O:setvar",
+ varname_converter, &name1, &newValue)) {
+ /* XXX Acquire tcl lock??? */
+ newval = AsObj(newValue);
+ if (newval == NULL)
+ return NULL;
+ ENTER_TCL
+ ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
+ newval, flags);
+ ENTER_OVERLAP
+ if (!ok)
+ Tkinter_Error(self);
+ else {
+ res = Py_None;
+ Py_INCREF(res);
+ }
+ LEAVE_OVERLAP_TCL
+ }
+ else {
+ PyErr_Clear();
+ if (PyArg_ParseTuple(args, "ssO:setvar",
+ &name1, &name2, &newValue)) {
+ /* XXX must hold tcl lock already??? */
+ newval = AsObj(newValue);
+ ENTER_TCL
+ ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags);
+ ENTER_OVERLAP
+ if (!ok)
+ Tkinter_Error(self);
+ else {
+ res = Py_None;
+ Py_INCREF(res);
+ }
+ LEAVE_OVERLAP_TCL
+ }
+ else {
+ return NULL;
+ }
+ }
+ return res;
}
static PyObject *
Tkapp_SetVar(PyObject *self, PyObject *args)
{
- return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG);
+ return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG);
}
static PyObject *
Tkapp_GlobalSetVar(PyObject *self, PyObject *args)
{
- return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
+ return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
}
-
+
static PyObject *
GetVar(PyObject *self, PyObject *args, int flags)
{
- char *name1, *name2=NULL;
- PyObject *res = NULL;
- Tcl_Obj *tres;
-
- if (!PyArg_ParseTuple(args, "O&|s:getvar",
- varname_converter, &name1, &name2))
- return NULL;
-
- ENTER_TCL
- tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags);
- ENTER_OVERLAP
- if (tres == NULL) {
- PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self)));
- } else {
- if (((TkappObject*)self)->wantobjects) {
- res = FromObj(self, tres);
- }
- else {
- res = PyUnicode_FromString(Tcl_GetString(tres));
- }
- }
- LEAVE_OVERLAP_TCL
- return res;
+ char *name1, *name2=NULL;
+ PyObject *res = NULL;
+ Tcl_Obj *tres;
+
+ if (!PyArg_ParseTuple(args, "O&|s:getvar",
+ varname_converter, &name1, &name2))
+ return NULL;
+
+ ENTER_TCL
+ tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags);
+ ENTER_OVERLAP
+ if (tres == NULL) {
+ PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self)));
+ } else {
+ if (((TkappObject*)self)->wantobjects) {
+ res = FromObj(self, tres);
+ }
+ else {
+ res = PyUnicode_FromString(Tcl_GetString(tres));
+ }
+ }
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_GetVar(PyObject *self, PyObject *args)
{
- return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG);
+ return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG);
}
static PyObject *
Tkapp_GlobalGetVar(PyObject *self, PyObject *args)
{
- return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
+ return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
}
-
+
static PyObject *
UnsetVar(PyObject *self, PyObject *args, int flags)
{
- char *name1, *name2=NULL;
- int code;
- PyObject *res = NULL;
+ char *name1, *name2=NULL;
+ int code;
+ PyObject *res = NULL;
- if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2))
+ return NULL;
- ENTER_TCL
- code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags);
- ENTER_OVERLAP
- if (code == TCL_ERROR)
- res = Tkinter_Error(self);
- else {
- Py_INCREF(Py_None);
- res = Py_None;
- }
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags);
+ ENTER_OVERLAP
+ if (code == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else {
+ Py_INCREF(Py_None);
+ res = Py_None;
+ }
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_UnsetVar(PyObject *self, PyObject *args)
{
- return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG);
+ return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG);
}
static PyObject *
Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args)
{
- return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
+ return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
}
-
+
/** Tcl to Python **/
static PyObject *
Tkapp_GetInt(PyObject *self, PyObject *args)
{
- char *s;
- int v;
+ char *s;
+ int v;
- if (PyTuple_Size(args) == 1) {
- PyObject* o = PyTuple_GetItem(args, 0);
- if (PyLong_Check(o)) {
- Py_INCREF(o);
- return o;
- }
- }
- if (!PyArg_ParseTuple(args, "s:getint", &s))
- return NULL;
- if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR)
- return Tkinter_Error(self);
- return Py_BuildValue("i", v);
+ if (PyTuple_Size(args) == 1) {
+ PyObject* o = PyTuple_GetItem(args, 0);
+ if (PyLong_Check(o)) {
+ Py_INCREF(o);
+ return o;
+ }
+ }
+ if (!PyArg_ParseTuple(args, "s:getint", &s))
+ return NULL;
+ if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR)
+ return Tkinter_Error(self);
+ return Py_BuildValue("i", v);
}
static PyObject *
Tkapp_GetDouble(PyObject *self, PyObject *args)
{
- char *s;
- double v;
+ char *s;
+ double v;
- if (PyTuple_Size(args) == 1) {
- PyObject *o = PyTuple_GetItem(args, 0);
- if (PyFloat_Check(o)) {
- Py_INCREF(o);
- return o;
- }
- }
- if (!PyArg_ParseTuple(args, "s:getdouble", &s))
- return NULL;
- if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR)
- return Tkinter_Error(self);
- return Py_BuildValue("d", v);
+ if (PyTuple_Size(args) == 1) {
+ PyObject *o = PyTuple_GetItem(args, 0);
+ if (PyFloat_Check(o)) {
+ Py_INCREF(o);
+ return o;
+ }
+ }
+ if (!PyArg_ParseTuple(args, "s:getdouble", &s))
+ return NULL;
+ if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR)
+ return Tkinter_Error(self);
+ return Py_BuildValue("d", v);
}
static PyObject *
Tkapp_GetBoolean(PyObject *self, PyObject *args)
{
- char *s;
- int v;
+ char *s;
+ int v;
- if (PyTuple_Size(args) == 1) {
- PyObject *o = PyTuple_GetItem(args, 0);
- if (PyLong_Check(o)) {
- Py_INCREF(o);
- return o;
- }
- }
- if (!PyArg_ParseTuple(args, "s:getboolean", &s))
- return NULL;
- if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR)
- return Tkinter_Error(self);
- return PyBool_FromLong(v);
+ if (PyTuple_Size(args) == 1) {
+ PyObject *o = PyTuple_GetItem(args, 0);
+ if (PyLong_Check(o)) {
+ Py_INCREF(o);
+ return o;
+ }
+ }
+ if (!PyArg_ParseTuple(args, "s:getboolean", &s))
+ return NULL;
+ if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR)
+ return Tkinter_Error(self);
+ return PyBool_FromLong(v);
}
static PyObject *
Tkapp_ExprString(PyObject *self, PyObject *args)
{
- char *s;
- PyObject *res = NULL;
- int retval;
+ char *s;
+ PyObject *res = NULL;
+ int retval;
- if (!PyArg_ParseTuple(args, "s:exprstring", &s))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:exprstring", &s))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- retval = Tcl_ExprString(Tkapp_Interp(self), s);
- ENTER_OVERLAP
- if (retval == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = Py_BuildValue("s", Tkapp_Result(self));
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ retval = Tcl_ExprString(Tkapp_Interp(self), s);
+ ENTER_OVERLAP
+ if (retval == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = Py_BuildValue("s", Tkapp_Result(self));
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_ExprLong(PyObject *self, PyObject *args)
{
- char *s;
- PyObject *res = NULL;
- int retval;
- long v;
+ char *s;
+ PyObject *res = NULL;
+ int retval;
+ long v;
- if (!PyArg_ParseTuple(args, "s:exprlong", &s))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:exprlong", &s))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- ENTER_TCL
- retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v);
- ENTER_OVERLAP
- if (retval == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = Py_BuildValue("l", v);
- LEAVE_OVERLAP_TCL
- return res;
+ ENTER_TCL
+ retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v);
+ ENTER_OVERLAP
+ if (retval == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = Py_BuildValue("l", v);
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_ExprDouble(PyObject *self, PyObject *args)
{
- char *s;
- PyObject *res = NULL;
- double v;
- int retval;
-
- if (!PyArg_ParseTuple(args, "s:exprdouble", &s))
- return NULL;
- CHECK_TCL_APPARTMENT;
- PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0)
- ENTER_TCL
- retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v);
- ENTER_OVERLAP
- PyFPE_END_PROTECT(retval)
- if (retval == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = Py_BuildValue("d", v);
- LEAVE_OVERLAP_TCL
- return res;
+ char *s;
+ PyObject *res = NULL;
+ double v;
+ int retval;
+
+ if (!PyArg_ParseTuple(args, "s:exprdouble", &s))
+ return NULL;
+ CHECK_TCL_APPARTMENT;
+ PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0)
+ ENTER_TCL
+ retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v);
+ ENTER_OVERLAP
+ PyFPE_END_PROTECT(retval)
+ if (retval == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = Py_BuildValue("d", v);
+ LEAVE_OVERLAP_TCL
+ return res;
}
static PyObject *
Tkapp_ExprBoolean(PyObject *self, PyObject *args)
{
- char *s;
- PyObject *res = NULL;
- int retval;
- int v;
+ char *s;
+ PyObject *res = NULL;
+ int retval;
+ int v;
- if (!PyArg_ParseTuple(args, "s:exprboolean", &s))
- return NULL;
- CHECK_TCL_APPARTMENT;
- ENTER_TCL
- retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v);
- ENTER_OVERLAP
- if (retval == TCL_ERROR)
- res = Tkinter_Error(self);
- else
- res = Py_BuildValue("i", v);
- LEAVE_OVERLAP_TCL
- return res;
+ if (!PyArg_ParseTuple(args, "s:exprboolean", &s))
+ return NULL;
+ CHECK_TCL_APPARTMENT;
+ ENTER_TCL
+ retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v);
+ ENTER_OVERLAP
+ if (retval == TCL_ERROR)
+ res = Tkinter_Error(self);
+ else
+ res = Py_BuildValue("i", v);
+ LEAVE_OVERLAP_TCL
+ return res;
}
-
+
static PyObject *
Tkapp_SplitList(PyObject *self, PyObject *args)
{
- char *list;
- int argc;
- char **argv;
- PyObject *v;
- int i;
-
- if (PyTuple_Size(args) == 1) {
- v = PyTuple_GetItem(args, 0);
- if (PyTuple_Check(v)) {
- Py_INCREF(v);
- return v;
- }
- }
- if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list))
- return NULL;
-
- if (Tcl_SplitList(Tkapp_Interp(self), list,
- &argc, &argv) == TCL_ERROR) {
- PyMem_Free(list);
- return Tkinter_Error(self);
- }
-
- if (!(v = PyTuple_New(argc)))
- goto finally;
-
- for (i = 0; i < argc; i++) {
- PyObject *s = PyUnicode_FromString(argv[i]);
- if (!s || PyTuple_SetItem(v, i, s)) {
- Py_DECREF(v);
- v = NULL;
- goto finally;
- }
- }
+ char *list;
+ int argc;
+ char **argv;
+ PyObject *v;
+ int i;
+
+ if (PyTuple_Size(args) == 1) {
+ v = PyTuple_GetItem(args, 0);
+ if (PyTuple_Check(v)) {
+ Py_INCREF(v);
+ return v;
+ }
+ }
+ if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list))
+ return NULL;
+
+ if (Tcl_SplitList(Tkapp_Interp(self), list,
+ &argc, &argv) == TCL_ERROR) {
+ PyMem_Free(list);
+ return Tkinter_Error(self);
+ }
+
+ if (!(v = PyTuple_New(argc)))
+ goto finally;
+
+ for (i = 0; i < argc; i++) {
+ PyObject *s = PyUnicode_FromString(argv[i]);
+ if (!s || PyTuple_SetItem(v, i, s)) {
+ Py_DECREF(v);
+ v = NULL;
+ goto finally;
+ }
+ }
finally:
- ckfree(FREECAST argv);
- PyMem_Free(list);
- return v;
+ ckfree(FREECAST argv);
+ PyMem_Free(list);
+ return v;
}
static PyObject *
Tkapp_Split(PyObject *self, PyObject *args)
{
- PyObject *v;
- char *list;
+ PyObject *v;
+ char *list;
- if (PyTuple_Size(args) == 1) {
- PyObject* o = PyTuple_GetItem(args, 0);
- if (PyTuple_Check(o)) {
- o = SplitObj(o);
- return o;
- }
- }
- if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list))
- return NULL;
- v = Split(list);
- PyMem_Free(list);
- return v;
+ if (PyTuple_Size(args) == 1) {
+ PyObject* o = PyTuple_GetItem(args, 0);
+ if (PyTuple_Check(o)) {
+ o = SplitObj(o);
+ return o;
+ }
+ }
+ if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list))
+ return NULL;
+ v = Split(list);
+ PyMem_Free(list);
+ return v;
}
static PyObject *
Tkapp_Merge(PyObject *self, PyObject *args)
{
- char *s = Merge(args);
- PyObject *res = NULL;
+ char *s = Merge(args);
+ PyObject *res = NULL;
- if (s) {
- res = PyUnicode_FromString(s);
- ckfree(s);
- }
+ if (s) {
+ res = PyUnicode_FromString(s);
+ ckfree(s);
+ }
- return res;
+ return res;
}
-
+
/** Tcl Command **/
/* Client data struct */
typedef struct {
- PyObject *self;
- PyObject *func;
+ PyObject *self;
+ PyObject *func;
} PythonCmd_ClientData;
static int
PythonCmd_Error(Tcl_Interp *interp)
{
- errorInCmd = 1;
- PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
- LEAVE_PYTHON
- return TCL_ERROR;
+ errorInCmd = 1;
+ PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
+ LEAVE_PYTHON
+ return TCL_ERROR;
}
/* This is the Tcl command that acts as a wrapper for Python
@@ -2004,211 +2004,211 @@ PythonCmd_Error(Tcl_Interp *interp)
static int
PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
- PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
- PyObject *self, *func, *arg, *res;
- int i, rv;
- Tcl_Obj *obj_res;
+ PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
+ PyObject *self, *func, *arg, *res;
+ int i, rv;
+ Tcl_Obj *obj_res;
- ENTER_PYTHON
+ ENTER_PYTHON
- /* TBD: no error checking here since we know, via the
- * Tkapp_CreateCommand() that the client data is a two-tuple
- */
- self = data->self;
- func = data->func;
+ /* TBD: no error checking here since we know, via the
+ * Tkapp_CreateCommand() that the client data is a two-tuple
+ */
+ self = data->self;
+ func = data->func;
- /* Create argument list (argv1, ..., argvN) */
- if (!(arg = PyTuple_New(argc - 1)))
- return PythonCmd_Error(interp);
+ /* Create argument list (argv1, ..., argvN) */
+ if (!(arg = PyTuple_New(argc - 1)))
+ return PythonCmd_Error(interp);
- for (i = 0; i < (argc - 1); i++) {
- PyObject *s = PyUnicode_FromString(argv[i + 1]);
- if (!s || PyTuple_SetItem(arg, i, s)) {
- Py_DECREF(arg);
- return PythonCmd_Error(interp);
- }
- }
- res = PyEval_CallObject(func, arg);
- Py_DECREF(arg);
+ for (i = 0; i < (argc - 1); i++) {
+ PyObject *s = PyUnicode_FromString(argv[i + 1]);
+ if (!s || PyTuple_SetItem(arg, i, s)) {
+ Py_DECREF(arg);
+ return PythonCmd_Error(interp);
+ }
+ }
+ res = PyEval_CallObject(func, arg);
+ Py_DECREF(arg);
- if (res == NULL)
- return PythonCmd_Error(interp);
+ if (res == NULL)
+ return PythonCmd_Error(interp);
- obj_res = AsObj(res);
- if (obj_res == NULL) {
- Py_DECREF(res);
- return PythonCmd_Error(interp);
- }
- else {
- Tcl_SetObjResult(interp, obj_res);
- rv = TCL_OK;
- }
+ obj_res = AsObj(res);
+ if (obj_res == NULL) {
+ Py_DECREF(res);
+ return PythonCmd_Error(interp);
+ }
+ else {
+ Tcl_SetObjResult(interp, obj_res);
+ rv = TCL_OK;
+ }
- Py_DECREF(res);
+ Py_DECREF(res);
- LEAVE_PYTHON
+ LEAVE_PYTHON
- return rv;
+ return rv;
}
static void
PythonCmdDelete(ClientData clientData)
{
- PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
+ PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
- ENTER_PYTHON
- Py_XDECREF(data->self);
- Py_XDECREF(data->func);
- PyMem_DEL(data);
- LEAVE_PYTHON
+ ENTER_PYTHON
+ Py_XDECREF(data->self);
+ Py_XDECREF(data->func);
+ PyMem_DEL(data);
+ LEAVE_PYTHON
}
-
+
#ifdef WITH_THREAD
TCL_DECLARE_MUTEX(command_mutex)
typedef struct CommandEvent{
- Tcl_Event ev;
- Tcl_Interp* interp;
- char *name;
- int create;
- int *status;
- ClientData *data;
- Tcl_Condition *done;
+ Tcl_Event ev;
+ Tcl_Interp* interp;
+ char *name;
+ int create;
+ int *status;
+ ClientData *data;
+ Tcl_Condition *done;
} CommandEvent;
static int
Tkapp_CommandProc(CommandEvent *ev, int flags)
{
- if (ev->create)
- *ev->status = Tcl_CreateCommand(
- ev->interp, ev->name, PythonCmd,
- ev->data, PythonCmdDelete) == NULL;
- else
- *ev->status = Tcl_DeleteCommand(ev->interp, ev->name);
- Tcl_MutexLock(&command_mutex);
- Tcl_ConditionNotify(ev->done);
- Tcl_MutexUnlock(&command_mutex);
- return 1;
+ if (ev->create)
+ *ev->status = Tcl_CreateCommand(
+ ev->interp, ev->name, PythonCmd,
+ ev->data, PythonCmdDelete) == NULL;
+ else
+ *ev->status = Tcl_DeleteCommand(ev->interp, ev->name);
+ Tcl_MutexLock(&command_mutex);
+ Tcl_ConditionNotify(ev->done);
+ Tcl_MutexUnlock(&command_mutex);
+ return 1;
}
#endif
static PyObject *
Tkapp_CreateCommand(PyObject *selfptr, PyObject *args)
{
- TkappObject *self = (TkappObject*)selfptr;
- PythonCmd_ClientData *data;
- char *cmdName;
- PyObject *func;
- int err;
+ TkappObject *self = (TkappObject*)selfptr;
+ PythonCmd_ClientData *data;
+ char *cmdName;
+ PyObject *func;
+ int err;
- if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func))
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError, "command not callable");
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func))
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError, "command not callable");
+ return NULL;
+ }
#ifdef WITH_THREAD
- if (self->threaded && self->thread_id != Tcl_GetCurrentThread() &&
- !WaitForMainloop(self))
- return NULL;
+ if (self->threaded && self->thread_id != Tcl_GetCurrentThread() &&
+ !WaitForMainloop(self))
+ return NULL;
#endif
- data = PyMem_NEW(PythonCmd_ClientData, 1);
- if (!data)
- return PyErr_NoMemory();
- Py_INCREF(self);
- Py_INCREF(func);
- data->self = selfptr;
- data->func = func;
+ data = PyMem_NEW(PythonCmd_ClientData, 1);
+ if (!data)
+ return PyErr_NoMemory();
+ Py_INCREF(self);
+ Py_INCREF(func);
+ data->self = selfptr;
+ data->func = func;
#ifdef WITH_THREAD
- if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
- Tcl_Condition cond = NULL;
- CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent));
- ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
- ev->interp = self->interp;
- ev->create = 1;
- ev->name = cmdName;
- ev->data = (ClientData)data;
- ev->status = &err;
- ev->done = &cond;
- Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex);
- Tcl_ConditionFinalize(&cond);
- }
- else
+ if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
+ Tcl_Condition cond = NULL;
+ CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent));
+ ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
+ ev->interp = self->interp;
+ ev->create = 1;
+ ev->name = cmdName;
+ ev->data = (ClientData)data;
+ ev->status = &err;
+ ev->done = &cond;
+ Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex);
+ Tcl_ConditionFinalize(&cond);
+ }
+ else
#endif
- {
- ENTER_TCL
- err = Tcl_CreateCommand(
- Tkapp_Interp(self), cmdName, PythonCmd,
- (ClientData)data, PythonCmdDelete) == NULL;
- LEAVE_TCL
- }
- if (err) {
- PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
- PyMem_DEL(data);
- return NULL;
- }
+ {
+ ENTER_TCL
+ err = Tcl_CreateCommand(
+ Tkapp_Interp(self), cmdName, PythonCmd,
+ (ClientData)data, PythonCmdDelete) == NULL;
+ LEAVE_TCL
+ }
+ if (err) {
+ PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
+ PyMem_DEL(data);
+ return NULL;
+ }
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
-
+
static PyObject *
Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args)
{
- TkappObject *self = (TkappObject*)selfptr;
- char *cmdName;
- int err;
+ TkappObject *self = (TkappObject*)selfptr;
+ char *cmdName;
+ int err;
- if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName))
+ return NULL;
#ifdef WITH_THREAD
- if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
- Tcl_Condition cond = NULL;
- CommandEvent *ev;
- ev = (CommandEvent*)ckalloc(sizeof(CommandEvent));
- ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
- ev->interp = self->interp;
- ev->create = 0;
- ev->name = cmdName;
- ev->status = &err;
- ev->done = &cond;
- Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond,
- &command_mutex);
- Tcl_ConditionFinalize(&cond);
- }
- else
+ if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
+ Tcl_Condition cond = NULL;
+ CommandEvent *ev;
+ ev = (CommandEvent*)ckalloc(sizeof(CommandEvent));
+ ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
+ ev->interp = self->interp;
+ ev->create = 0;
+ ev->name = cmdName;
+ ev->status = &err;
+ ev->done = &cond;
+ Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond,
+ &command_mutex);
+ Tcl_ConditionFinalize(&cond);
+ }
+ else
#endif
- {
- ENTER_TCL
- err = Tcl_DeleteCommand(self->interp, cmdName);
- LEAVE_TCL
- }
- if (err == -1) {
- PyErr_SetString(Tkinter_TclError, "can't delete Tcl command");
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ {
+ ENTER_TCL
+ err = Tcl_DeleteCommand(self->interp, cmdName);
+ LEAVE_TCL
+ }
+ if (err == -1) {
+ PyErr_SetString(Tkinter_TclError, "can't delete Tcl command");
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
-
+
#ifdef HAVE_CREATEFILEHANDLER
/** File Handler **/
typedef struct _fhcdata {
- PyObject *func;
- PyObject *file;
- int id;
- struct _fhcdata *next;
+ PyObject *func;
+ PyObject *file;
+ int id;
+ struct _fhcdata *next;
} FileHandler_ClientData;
static FileHandler_ClientData *HeadFHCD;
@@ -2216,712 +2216,712 @@ static FileHandler_ClientData *HeadFHCD;
static FileHandler_ClientData *
NewFHCD(PyObject *func, PyObject *file, int id)
{
- FileHandler_ClientData *p;
- p = PyMem_NEW(FileHandler_ClientData, 1);
- if (p != NULL) {
- Py_XINCREF(func);
- Py_XINCREF(file);
- p->func = func;
- p->file = file;
- p->id = id;
- p->next = HeadFHCD;
- HeadFHCD = p;
- }
- return p;
+ FileHandler_ClientData *p;
+ p = PyMem_NEW(FileHandler_ClientData, 1);
+ if (p != NULL) {
+ Py_XINCREF(func);
+ Py_XINCREF(file);
+ p->func = func;
+ p->file = file;
+ p->id = id;
+ p->next = HeadFHCD;
+ HeadFHCD = p;
+ }
+ return p;
}
static void
DeleteFHCD(int id)
{
- FileHandler_ClientData *p, **pp;
+ FileHandler_ClientData *p, **pp;
- pp = &HeadFHCD;
- while ((p = *pp) != NULL) {
- if (p->id == id) {
- *pp = p->next;
- Py_XDECREF(p->func);
- Py_XDECREF(p->file);
- PyMem_DEL(p);
- }
- else
- pp = &p->next;
- }
+ pp = &HeadFHCD;
+ while ((p = *pp) != NULL) {
+ if (p->id == id) {
+ *pp = p->next;
+ Py_XDECREF(p->func);
+ Py_XDECREF(p->file);
+ PyMem_DEL(p);
+ }
+ else
+ pp = &p->next;
+ }
}
static void
FileHandler(ClientData clientData, int mask)
{
- FileHandler_ClientData *data = (FileHandler_ClientData *)clientData;
- PyObject *func, *file, *arg, *res;
+ FileHandler_ClientData *data = (FileHandler_ClientData *)clientData;
+ PyObject *func, *file, *arg, *res;
- ENTER_PYTHON
- func = data->func;
- file = data->file;
+ ENTER_PYTHON
+ func = data->func;
+ file = data->file;
- arg = Py_BuildValue("(Oi)", file, (long) mask);
- res = PyEval_CallObject(func, arg);
- Py_DECREF(arg);
+ arg = Py_BuildValue("(Oi)", file, (long) mask);
+ res = PyEval_CallObject(func, arg);
+ Py_DECREF(arg);
- if (res == NULL) {
- errorInCmd = 1;
- PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
- }
- Py_XDECREF(res);
- LEAVE_PYTHON
+ if (res == NULL) {
+ errorInCmd = 1;
+ PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
+ }
+ Py_XDECREF(res);
+ LEAVE_PYTHON
}
static PyObject *
Tkapp_CreateFileHandler(PyObject *self, PyObject *args)
/* args is (file, mask, func) */
{
- FileHandler_ClientData *data;
- PyObject *file, *func;
- int mask, tfile;
+ FileHandler_ClientData *data;
+ PyObject *file, *func;
+ int mask, tfile;
- if (!PyArg_ParseTuple(args, "OiO:createfilehandler",
- &file, &mask, &func))
- return NULL;
+ if (!PyArg_ParseTuple(args, "OiO:createfilehandler",
+ &file, &mask, &func))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- tfile = PyObject_AsFileDescriptor(file);
- if (tfile < 0)
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError, "bad argument list");
- return NULL;
- }
+ tfile = PyObject_AsFileDescriptor(file);
+ if (tfile < 0)
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError, "bad argument list");
+ return NULL;
+ }
- data = NewFHCD(func, file, tfile);
- if (data == NULL)
- return NULL;
+ data = NewFHCD(func, file, tfile);
+ if (data == NULL)
+ return NULL;
- /* Ought to check for null Tcl_File object... */
- ENTER_TCL
- Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data);
- LEAVE_TCL
- Py_INCREF(Py_None);
- return Py_None;
+ /* Ought to check for null Tcl_File object... */
+ ENTER_TCL
+ Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data);
+ LEAVE_TCL
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
Tkapp_DeleteFileHandler(PyObject *self, PyObject *args)
{
- PyObject *file;
- int tfile;
+ PyObject *file;
+ int tfile;
- if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file))
+ return NULL;
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- tfile = PyObject_AsFileDescriptor(file);
- if (tfile < 0)
- return NULL;
+ tfile = PyObject_AsFileDescriptor(file);
+ if (tfile < 0)
+ return NULL;
- DeleteFHCD(tfile);
+ DeleteFHCD(tfile);
- /* Ought to check for null Tcl_File object... */
- ENTER_TCL
- Tcl_DeleteFileHandler(tfile);
- LEAVE_TCL
- Py_INCREF(Py_None);
- return Py_None;
+ /* Ought to check for null Tcl_File object... */
+ ENTER_TCL
+ Tcl_DeleteFileHandler(tfile);
+ LEAVE_TCL
+ Py_INCREF(Py_None);
+ return Py_None;
}
#endif /* HAVE_CREATEFILEHANDLER */
-
+
/**** Tktt Object (timer token) ****/
static PyTypeObject Tktt_Type;
typedef struct {
- PyObject_HEAD
- Tcl_TimerToken token;
- PyObject *func;
+ PyObject_HEAD
+ Tcl_TimerToken token;
+ PyObject *func;
} TkttObject;
static PyObject *
Tktt_DeleteTimerHandler(PyObject *self, PyObject *args)
{
- TkttObject *v = (TkttObject *)self;
- PyObject *func = v->func;
+ TkttObject *v = (TkttObject *)self;
+ PyObject *func = v->func;
- if (!PyArg_ParseTuple(args, ":deletetimerhandler"))
- return NULL;
- if (v->token != NULL) {
- Tcl_DeleteTimerHandler(v->token);
- v->token = NULL;
- }
- if (func != NULL) {
- v->func = NULL;
- Py_DECREF(func);
- Py_DECREF(v); /* See Tktt_New() */
- }
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, ":deletetimerhandler"))
+ return NULL;
+ if (v->token != NULL) {
+ Tcl_DeleteTimerHandler(v->token);
+ v->token = NULL;
+ }
+ if (func != NULL) {
+ v->func = NULL;
+ Py_DECREF(func);
+ Py_DECREF(v); /* See Tktt_New() */
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyMethodDef Tktt_methods[] =
{
- {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS},
- {NULL, NULL}
+ {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS},
+ {NULL, NULL}
};
static TkttObject *
Tktt_New(PyObject *func)
{
- TkttObject *v;
+ TkttObject *v;
- v = PyObject_New(TkttObject, &Tktt_Type);
- if (v == NULL)
- return NULL;
+ v = PyObject_New(TkttObject, &Tktt_Type);
+ if (v == NULL)
+ return NULL;
- Py_INCREF(func);
- v->token = NULL;
- v->func = func;
+ Py_INCREF(func);
+ v->token = NULL;
+ v->func = func;
- /* Extra reference, deleted when called or when handler is deleted */
- Py_INCREF(v);
- return v;
+ /* Extra reference, deleted when called or when handler is deleted */
+ Py_INCREF(v);
+ return v;
}
static void
Tktt_Dealloc(PyObject *self)
{
- TkttObject *v = (TkttObject *)self;
- PyObject *func = v->func;
+ TkttObject *v = (TkttObject *)self;
+ PyObject *func = v->func;
- Py_XDECREF(func);
+ Py_XDECREF(func);
- PyObject_Del(self);
+ PyObject_Del(self);
}
static PyObject *
Tktt_Repr(PyObject *self)
{
- TkttObject *v = (TkttObject *)self;
- char buf[100];
+ TkttObject *v = (TkttObject *)self;
+ char buf[100];
- PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
- v->func == NULL ? ", handler deleted" : "");
- return PyUnicode_FromString(buf);
+ PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
+ v->func == NULL ? ", handler deleted" : "");
+ return PyUnicode_FromString(buf);
}
static PyTypeObject Tktt_Type =
{
- PyVarObject_HEAD_INIT(NULL, 0)
- "tktimertoken", /*tp_name */
- sizeof(TkttObject), /*tp_basicsize */
- 0, /*tp_itemsize */
- Tktt_Dealloc, /*tp_dealloc */
- 0, /*tp_print */
- 0, /*tp_getattr */
- 0, /*tp_setattr */
- 0, /*tp_reserved */
- Tktt_Repr, /*tp_repr */
- 0, /*tp_as_number */
- 0, /*tp_as_sequence */
- 0, /*tp_as_mapping */
- 0, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- Tktt_methods, /*tp_methods*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "tktimertoken", /*tp_name */
+ sizeof(TkttObject), /*tp_basicsize */
+ 0, /*tp_itemsize */
+ Tktt_Dealloc, /*tp_dealloc */
+ 0, /*tp_print */
+ 0, /*tp_getattr */
+ 0, /*tp_setattr */
+ 0, /*tp_reserved */
+ Tktt_Repr, /*tp_repr */
+ 0, /*tp_as_number */
+ 0, /*tp_as_sequence */
+ 0, /*tp_as_mapping */
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ Tktt_methods, /*tp_methods*/
};
-
+
/** Timer Handler **/
static void
TimerHandler(ClientData clientData)
{
- TkttObject *v = (TkttObject *)clientData;
- PyObject *func = v->func;
- PyObject *res;
+ TkttObject *v = (TkttObject *)clientData;
+ PyObject *func = v->func;
+ PyObject *res;
- if (func == NULL)
- return;
+ if (func == NULL)
+ return;
- v->func = NULL;
+ v->func = NULL;
- ENTER_PYTHON
+ ENTER_PYTHON
- res = PyEval_CallObject(func, NULL);
- Py_DECREF(func);
- Py_DECREF(v); /* See Tktt_New() */
+ res = PyEval_CallObject(func, NULL);
+ Py_DECREF(func);
+ Py_DECREF(v); /* See Tktt_New() */
- if (res == NULL) {
- errorInCmd = 1;
- PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
- }
- else
- Py_DECREF(res);
+ if (res == NULL) {
+ errorInCmd = 1;
+ PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd);
+ }
+ else
+ Py_DECREF(res);
- LEAVE_PYTHON
+ LEAVE_PYTHON
}
static PyObject *
Tkapp_CreateTimerHandler(PyObject *self, PyObject *args)
{
- int milliseconds;
- PyObject *func;
- TkttObject *v;
+ int milliseconds;
+ PyObject *func;
+ TkttObject *v;
- if (!PyArg_ParseTuple(args, "iO:createtimerhandler",
- &milliseconds, &func))
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError, "bad argument list");
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "iO:createtimerhandler",
+ &milliseconds, &func))
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError, "bad argument list");
+ return NULL;
+ }
- CHECK_TCL_APPARTMENT;
+ CHECK_TCL_APPARTMENT;
- v = Tktt_New(func);
- if (v) {
- v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler,
- (ClientData)v);
- }
+ v = Tktt_New(func);
+ if (v) {
+ v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler,
+ (ClientData)v);
+ }
- return (PyObject *) v;
+ return (PyObject *) v;
}
-
+
/** Event Loop **/
static PyObject *
Tkapp_MainLoop(PyObject *selfptr, PyObject *args)
{
- int threshold = 0;
- TkappObject *self = (TkappObject*)selfptr;
+ int threshold = 0;
+ TkappObject *self = (TkappObject*)selfptr;
#ifdef WITH_THREAD
- PyThreadState *tstate = PyThreadState_Get();
+ PyThreadState *tstate = PyThreadState_Get();
#endif
- if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold))
- return NULL;
+ if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold))
+ return NULL;
- CHECK_TCL_APPARTMENT;
- self->dispatching = 1;
+ CHECK_TCL_APPARTMENT;
+ self->dispatching = 1;
- quitMainLoop = 0;
- while (Tk_GetNumMainWindows() > threshold &&
- !quitMainLoop &&
- !errorInCmd)
- {
- int result;
+ quitMainLoop = 0;
+ while (Tk_GetNumMainWindows() > threshold &&
+ !quitMainLoop &&
+ !errorInCmd)
+ {
+ int result;
#ifdef WITH_THREAD
- if (self->threaded) {
- /* Allow other Python threads to run. */
- ENTER_TCL
- result = Tcl_DoOneEvent(0);
- LEAVE_TCL
- }
- else {
- Py_BEGIN_ALLOW_THREADS
- if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
- tcl_tstate = tstate;
- result = Tcl_DoOneEvent(TCL_DONT_WAIT);
- tcl_tstate = NULL;
- if(tcl_lock)PyThread_release_lock(tcl_lock);
- if (result == 0)
- Sleep(Tkinter_busywaitinterval);
- Py_END_ALLOW_THREADS
- }
+ if (self->threaded) {
+ /* Allow other Python threads to run. */
+ ENTER_TCL
+ result = Tcl_DoOneEvent(0);
+ LEAVE_TCL
+ }
+ else {
+ Py_BEGIN_ALLOW_THREADS
+ if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
+ tcl_tstate = tstate;
+ result = Tcl_DoOneEvent(TCL_DONT_WAIT);
+ tcl_tstate = NULL;
+ if(tcl_lock)PyThread_release_lock(tcl_lock);
+ if (result == 0)
+ Sleep(Tkinter_busywaitinterval);
+ Py_END_ALLOW_THREADS
+ }
#else
- result = Tcl_DoOneEvent(0);
+ result = Tcl_DoOneEvent(0);
#endif
- if (PyErr_CheckSignals() != 0) {
- self->dispatching = 0;
- return NULL;
- }
- if (result < 0)
- break;
- }
- self->dispatching = 0;
- quitMainLoop = 0;
-
- if (errorInCmd) {
- errorInCmd = 0;
- PyErr_Restore(excInCmd, valInCmd, trbInCmd);
- excInCmd = valInCmd = trbInCmd = NULL;
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ if (PyErr_CheckSignals() != 0) {
+ self->dispatching = 0;
+ return NULL;
+ }
+ if (result < 0)
+ break;
+ }
+ self->dispatching = 0;
+ quitMainLoop = 0;
+
+ if (errorInCmd) {
+ errorInCmd = 0;
+ PyErr_Restore(excInCmd, valInCmd, trbInCmd);
+ excInCmd = valInCmd = trbInCmd = NULL;
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
Tkapp_DoOneEvent(PyObject *self, PyObject *args)
{
- int flags = 0;
- int rv;
+ int flags = 0;
+ int rv;
- if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags))
- return NULL;
+ if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags))
+ return NULL;
- ENTER_TCL
- rv = Tcl_DoOneEvent(flags);
- LEAVE_TCL
- return Py_BuildValue("i", rv);
+ ENTER_TCL
+ rv = Tcl_DoOneEvent(flags);
+ LEAVE_TCL
+ return Py_BuildValue("i", rv);
}
static PyObject *
Tkapp_Quit(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":quit"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":quit"))
+ return NULL;
- quitMainLoop = 1;
- Py_INCREF(Py_None);
- return Py_None;
+ quitMainLoop = 1;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
Tkapp_InterpAddr(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":interpaddr"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":interpaddr"))
+ return NULL;
- return PyLong_FromLong((long)Tkapp_Interp(self));
+ return PyLong_FromLong((long)Tkapp_Interp(self));
}
-static PyObject *
+static PyObject *
Tkapp_TkInit(PyObject *self, PyObject *args)
{
- Tcl_Interp *interp = Tkapp_Interp(self);
- const char * _tk_exists = NULL;
- int err;
+ Tcl_Interp *interp = Tkapp_Interp(self);
+ const char * _tk_exists = NULL;
+ int err;
#ifdef TKINTER_PROTECT_LOADTK
- /* Up to Tk 8.4.13, Tk_Init deadlocks on the second call when the
- * first call failed.
- * To avoid the deadlock, we just refuse the second call through
- * a static variable.
- */
- if (tk_load_failed) {
- PyErr_SetString(Tkinter_TclError, TKINTER_LOADTK_ERRMSG);
- return NULL;
- }
+ /* Up to Tk 8.4.13, Tk_Init deadlocks on the second call when the
+ * first call failed.
+ * To avoid the deadlock, we just refuse the second call through
+ * a static variable.
+ */
+ if (tk_load_failed) {
+ PyErr_SetString(Tkinter_TclError, TKINTER_LOADTK_ERRMSG);
+ return NULL;
+ }
#endif
- /* We want to guard against calling Tk_Init() multiple times */
- CHECK_TCL_APPARTMENT;
- ENTER_TCL
- err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version");
- ENTER_OVERLAP
- if (err == TCL_ERROR) {
- /* This sets an exception, but we cannot return right
- away because we need to exit the overlap first. */
- Tkinter_Error(self);
- } else {
- _tk_exists = Tkapp_Result(self);
- }
- LEAVE_OVERLAP_TCL
- if (err == TCL_ERROR) {
- return NULL;
- }
- if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) {
- if (Tk_Init(interp) == TCL_ERROR) {
- PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self)));
+ /* We want to guard against calling Tk_Init() multiple times */
+ CHECK_TCL_APPARTMENT;
+ ENTER_TCL
+ err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version");
+ ENTER_OVERLAP
+ if (err == TCL_ERROR) {
+ /* This sets an exception, but we cannot return right
+ away because we need to exit the overlap first. */
+ Tkinter_Error(self);
+ } else {
+ _tk_exists = Tkapp_Result(self);
+ }
+ LEAVE_OVERLAP_TCL
+ if (err == TCL_ERROR) {
+ return NULL;
+ }
+ if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) {
+ if (Tk_Init(interp) == TCL_ERROR) {
+ PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self)));
#ifdef TKINTER_PROTECT_LOADTK
- tk_load_failed = 1;
+ tk_load_failed = 1;
#endif
- return NULL;
- }
- }
- Py_INCREF(Py_None);
- return Py_None;
+ return NULL;
+ }
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
Tkapp_WantObjects(PyObject *self, PyObject *args)
{
- int wantobjects = -1;
- if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects))
- return NULL;
- if (wantobjects == -1)
- return PyBool_FromLong(((TkappObject*)self)->wantobjects);
- ((TkappObject*)self)->wantobjects = wantobjects;
+ int wantobjects = -1;
+ if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects))
+ return NULL;
+ if (wantobjects == -1)
+ return PyBool_FromLong(((TkappObject*)self)->wantobjects);
+ ((TkappObject*)self)->wantobjects = wantobjects;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
Tkapp_WillDispatch(PyObject *self, PyObject *args)
{
- ((TkappObject*)self)->dispatching = 1;
+ ((TkappObject*)self)->dispatching = 1;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
-
+
/**** Tkapp Method List ****/
static PyMethodDef Tkapp_methods[] =
{
- {"willdispatch", Tkapp_WillDispatch, METH_NOARGS},
- {"wantobjects", Tkapp_WantObjects, METH_VARARGS},
- {"call", Tkapp_Call, METH_VARARGS},
- {"globalcall", Tkapp_GlobalCall, METH_VARARGS},
- {"eval", Tkapp_Eval, METH_VARARGS},
- {"globaleval", Tkapp_GlobalEval, METH_VARARGS},
- {"evalfile", Tkapp_EvalFile, METH_VARARGS},
- {"record", Tkapp_Record, METH_VARARGS},
- {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS},
- {"setvar", Tkapp_SetVar, METH_VARARGS},
- {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS},
- {"getvar", Tkapp_GetVar, METH_VARARGS},
- {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS},
- {"unsetvar", Tkapp_UnsetVar, METH_VARARGS},
- {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS},
- {"getint", Tkapp_GetInt, METH_VARARGS},
- {"getdouble", Tkapp_GetDouble, METH_VARARGS},
- {"getboolean", Tkapp_GetBoolean, METH_VARARGS},
- {"exprstring", Tkapp_ExprString, METH_VARARGS},
- {"exprlong", Tkapp_ExprLong, METH_VARARGS},
- {"exprdouble", Tkapp_ExprDouble, METH_VARARGS},
- {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS},
- {"splitlist", Tkapp_SplitList, METH_VARARGS},
- {"split", Tkapp_Split, METH_VARARGS},
- {"merge", Tkapp_Merge, METH_VARARGS},
- {"createcommand", Tkapp_CreateCommand, METH_VARARGS},
- {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS},
+ {"willdispatch", Tkapp_WillDispatch, METH_NOARGS},
+ {"wantobjects", Tkapp_WantObjects, METH_VARARGS},
+ {"call", Tkapp_Call, METH_VARARGS},
+ {"globalcall", Tkapp_GlobalCall, METH_VARARGS},
+ {"eval", Tkapp_Eval, METH_VARARGS},
+ {"globaleval", Tkapp_GlobalEval, METH_VARARGS},
+ {"evalfile", Tkapp_EvalFile, METH_VARARGS},
+ {"record", Tkapp_Record, METH_VARARGS},
+ {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS},
+ {"setvar", Tkapp_SetVar, METH_VARARGS},
+ {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS},
+ {"getvar", Tkapp_GetVar, METH_VARARGS},
+ {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS},
+ {"unsetvar", Tkapp_UnsetVar, METH_VARARGS},
+ {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS},
+ {"getint", Tkapp_GetInt, METH_VARARGS},
+ {"getdouble", Tkapp_GetDouble, METH_VARARGS},
+ {"getboolean", Tkapp_GetBoolean, METH_VARARGS},
+ {"exprstring", Tkapp_ExprString, METH_VARARGS},
+ {"exprlong", Tkapp_ExprLong, METH_VARARGS},
+ {"exprdouble", Tkapp_ExprDouble, METH_VARARGS},
+ {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS},
+ {"splitlist", Tkapp_SplitList, METH_VARARGS},
+ {"split", Tkapp_Split, METH_VARARGS},
+ {"merge", Tkapp_Merge, METH_VARARGS},
+ {"createcommand", Tkapp_CreateCommand, METH_VARARGS},
+ {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS},
#ifdef HAVE_CREATEFILEHANDLER
- {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS},
- {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS},
+ {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS},
+ {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS},
#endif
- {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS},
- {"mainloop", Tkapp_MainLoop, METH_VARARGS},
- {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS},
- {"quit", Tkapp_Quit, METH_VARARGS},
- {"interpaddr", Tkapp_InterpAddr, METH_VARARGS},
- {"loadtk", Tkapp_TkInit, METH_NOARGS},
- {NULL, NULL}
+ {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS},
+ {"mainloop", Tkapp_MainLoop, METH_VARARGS},
+ {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS},
+ {"quit", Tkapp_Quit, METH_VARARGS},
+ {"interpaddr", Tkapp_InterpAddr, METH_VARARGS},
+ {"loadtk", Tkapp_TkInit, METH_NOARGS},
+ {NULL, NULL}
};
-
+
/**** Tkapp Type Methods ****/
static void
Tkapp_Dealloc(PyObject *self)
{
- /*CHECK_TCL_APPARTMENT;*/
- ENTER_TCL
- Tcl_DeleteInterp(Tkapp_Interp(self));
- LEAVE_TCL
- PyObject_Del(self);
- DisableEventHook();
+ /*CHECK_TCL_APPARTMENT;*/
+ ENTER_TCL
+ Tcl_DeleteInterp(Tkapp_Interp(self));
+ LEAVE_TCL
+ PyObject_Del(self);
+ DisableEventHook();
}
static PyTypeObject Tkapp_Type =
{
- PyVarObject_HEAD_INIT(NULL, 0)
- "tkapp", /*tp_name */
- sizeof(TkappObject), /*tp_basicsize */
- 0, /*tp_itemsize */
- Tkapp_Dealloc, /*tp_dealloc */
- 0, /*tp_print */
- 0, /*tp_getattr */
- 0, /*tp_setattr */
- 0, /*tp_reserved */
- 0, /*tp_repr */
- 0, /*tp_as_number */
- 0, /*tp_as_sequence */
- 0, /*tp_as_mapping */
- 0, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- Tkapp_methods, /*tp_methods*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "tkapp", /*tp_name */
+ sizeof(TkappObject), /*tp_basicsize */
+ 0, /*tp_itemsize */
+ Tkapp_Dealloc, /*tp_dealloc */
+ 0, /*tp_print */
+ 0, /*tp_getattr */
+ 0, /*tp_setattr */
+ 0, /*tp_reserved */
+ 0, /*tp_repr */
+ 0, /*tp_as_number */
+ 0, /*tp_as_sequence */
+ 0, /*tp_as_mapping */
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ Tkapp_methods, /*tp_methods*/
};
-
+
/**** Tkinter Module ****/
typedef struct {
- PyObject* tuple;
- int size; /* current size */
- int maxsize; /* allocated size */
+ PyObject* tuple;
+ int size; /* current size */
+ int maxsize; /* allocated size */
} FlattenContext;
static int
_bump(FlattenContext* context, int size)
{
- /* expand tuple to hold (at least) size new items.
- return true if successful, false if an exception was raised */
+ /* expand tuple to hold (at least) size new items.
+ return true if successful, false if an exception was raised */
- int maxsize = context->maxsize * 2;
+ int maxsize = context->maxsize * 2;
- if (maxsize < context->size + size)
- maxsize = context->size + size;
+ if (maxsize < context->size + size)
+ maxsize = context->size + size;
- context->maxsize = maxsize;
+ context->maxsize = maxsize;
- return _PyTuple_Resize(&context->tuple, maxsize) >= 0;
+ return _PyTuple_Resize(&context->tuple, maxsize) >= 0;
}
static int
_flatten1(FlattenContext* context, PyObject* item, int depth)
{
- /* add tuple or list to argument tuple (recursively) */
-
- int i, size;
-
- if (depth > 1000) {
- PyErr_SetString(PyExc_ValueError,
- "nesting too deep in _flatten");
- return 0;
- } else if (PyList_Check(item)) {
- size = PyList_GET_SIZE(item);
- /* preallocate (assume no nesting) */
- if (context->size + size > context->maxsize &&
- !_bump(context, size))
- return 0;
- /* copy items to output tuple */
- for (i = 0; i < size; i++) {
- PyObject *o = PyList_GET_ITEM(item, i);
- if (PyList_Check(o) || PyTuple_Check(o)) {
- if (!_flatten1(context, o, depth + 1))
- return 0;
- } else if (o != Py_None) {
- if (context->size + 1 > context->maxsize &&
- !_bump(context, 1))
- return 0;
- Py_INCREF(o);
- PyTuple_SET_ITEM(context->tuple,
- context->size++, o);
- }
- }
- } else if (PyTuple_Check(item)) {
- /* same, for tuples */
- size = PyTuple_GET_SIZE(item);
- if (context->size + size > context->maxsize &&
- !_bump(context, size))
- return 0;
- for (i = 0; i < size; i++) {
- PyObject *o = PyTuple_GET_ITEM(item, i);
- if (PyList_Check(o) || PyTuple_Check(o)) {
- if (!_flatten1(context, o, depth + 1))
- return 0;
- } else if (o != Py_None) {
- if (context->size + 1 > context->maxsize &&
- !_bump(context, 1))
- return 0;
- Py_INCREF(o);
- PyTuple_SET_ITEM(context->tuple,
- context->size++, o);
- }
- }
- } else {
- PyErr_SetString(PyExc_TypeError, "argument must be sequence");
- return 0;
- }
- return 1;
+ /* add tuple or list to argument tuple (recursively) */
+
+ int i, size;
+
+ if (depth > 1000) {
+ PyErr_SetString(PyExc_ValueError,
+ "nesting too deep in _flatten");
+ return 0;
+ } else if (PyList_Check(item)) {
+ size = PyList_GET_SIZE(item);
+ /* preallocate (assume no nesting) */
+ if (context->size + size > context->maxsize &&
+ !_bump(context, size))
+ return 0;
+ /* copy items to output tuple */
+ for (i = 0; i < size; i++) {
+ PyObject *o = PyList_GET_ITEM(item, i);
+ if (PyList_Check(o) || PyTuple_Check(o)) {
+ if (!_flatten1(context, o, depth + 1))
+ return 0;
+ } else if (o != Py_None) {
+ if (context->size + 1 > context->maxsize &&
+ !_bump(context, 1))
+ return 0;
+ Py_INCREF(o);
+ PyTuple_SET_ITEM(context->tuple,
+ context->size++, o);
+ }
+ }
+ } else if (PyTuple_Check(item)) {
+ /* same, for tuples */
+ size = PyTuple_GET_SIZE(item);
+ if (context->size + size > context->maxsize &&
+ !_bump(context, size))
+ return 0;
+ for (i = 0; i < size; i++) {
+ PyObject *o = PyTuple_GET_ITEM(item, i);
+ if (PyList_Check(o) || PyTuple_Check(o)) {
+ if (!_flatten1(context, o, depth + 1))
+ return 0;
+ } else if (o != Py_None) {
+ if (context->size + 1 > context->maxsize &&
+ !_bump(context, 1))
+ return 0;
+ Py_INCREF(o);
+ PyTuple_SET_ITEM(context->tuple,
+ context->size++, o);
+ }
+ }
+ } else {
+ PyErr_SetString(PyExc_TypeError, "argument must be sequence");
+ return 0;
+ }
+ return 1;
}
static PyObject *
Tkinter_Flatten(PyObject* self, PyObject* args)
{
- FlattenContext context;
- PyObject* item;
+ FlattenContext context;
+ PyObject* item;
- if (!PyArg_ParseTuple(args, "O:_flatten", &item))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O:_flatten", &item))
+ return NULL;
- context.maxsize = PySequence_Size(item);
- if (context.maxsize < 0)
- return NULL;
- if (context.maxsize == 0)
- return PyTuple_New(0);
+ context.maxsize = PySequence_Size(item);
+ if (context.maxsize < 0)
+ return NULL;
+ if (context.maxsize == 0)
+ return PyTuple_New(0);
- context.tuple = PyTuple_New(context.maxsize);
- if (!context.tuple)
- return NULL;
+ context.tuple = PyTuple_New(context.maxsize);
+ if (!context.tuple)
+ return NULL;
- context.size = 0;
+ context.size = 0;
- if (!_flatten1(&context, item,0))
- return NULL;
+ if (!_flatten1(&context, item,0))
+ return NULL;
- if (_PyTuple_Resize(&context.tuple, context.size))
- return NULL;
+ if (_PyTuple_Resize(&context.tuple, context.size))
+ return NULL;
- return context.tuple;
+ return context.tuple;
}
static PyObject *
Tkinter_Create(PyObject *self, PyObject *args)
{
- char *screenName = NULL;
- char *baseName = NULL; /* XXX this is not used anymore;
- try getting rid of it. */
- char *className = NULL;
- int interactive = 0;
- int wantobjects = 0;
- int wantTk = 1; /* If false, then Tk_Init() doesn't get called */
- int sync = 0; /* pass -sync to wish */
- char *use = NULL; /* pass -use to wish */
+ char *screenName = NULL;
+ char *baseName = NULL; /* XXX this is not used anymore;
+ try getting rid of it. */
+ char *className = NULL;
+ int interactive = 0;
+ int wantobjects = 0;
+ int wantTk = 1; /* If false, then Tk_Init() doesn't get called */
+ int sync = 0; /* pass -sync to wish */
+ char *use = NULL; /* pass -use to wish */
- className = "Tk";
+ className = "Tk";
- if (!PyArg_ParseTuple(args, "|zssiiiiz:create",
- &screenName, &baseName, &className,
- &interactive, &wantobjects, &wantTk,
- &sync, &use))
- return NULL;
+ if (!PyArg_ParseTuple(args, "|zssiiiiz:create",
+ &screenName, &baseName, &className,
+ &interactive, &wantobjects, &wantTk,
+ &sync, &use))
+ return NULL;
- return (PyObject *) Tkapp_New(screenName, className,
- interactive, wantobjects, wantTk,
- sync, use);
+ return (PyObject *) Tkapp_New(screenName, className,
+ interactive, wantobjects, wantTk,
+ sync, use);
}
static PyObject *
Tkinter_setbusywaitinterval(PyObject *self, PyObject *args)
{
- int new_val;
- if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val))
- return NULL;
- if (new_val < 0) {
- PyErr_SetString(PyExc_ValueError,
- "busywaitinterval must be >= 0");
- return NULL;
- }
- Tkinter_busywaitinterval = new_val;
- Py_INCREF(Py_None);
- return Py_None;
+ int new_val;
+ if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val))
+ return NULL;
+ if (new_val < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "busywaitinterval must be >= 0");
+ return NULL;
+ }
+ Tkinter_busywaitinterval = new_val;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static char setbusywaitinterval_doc[] =
@@ -2935,7 +2935,7 @@ frames in an animation.";
static PyObject *
Tkinter_getbusywaitinterval(PyObject *self, PyObject *args)
{
- return PyLong_FromLong(Tkinter_busywaitinterval);
+ return PyLong_FromLong(Tkinter_busywaitinterval);
}
static char getbusywaitinterval_doc[] =
@@ -2946,13 +2946,13 @@ calls to Tcl_DoOneEvent in a threaded Python interpreter.";
static PyMethodDef moduleMethods[] =
{
- {"_flatten", Tkinter_Flatten, METH_VARARGS},
- {"create", Tkinter_Create, METH_VARARGS},
- {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS,
- setbusywaitinterval_doc},
- {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval,
- METH_NOARGS, getbusywaitinterval_doc},
- {NULL, NULL}
+ {"_flatten", Tkinter_Flatten, METH_VARARGS},
+ {"create", Tkinter_Create, METH_VARARGS},
+ {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS,
+ setbusywaitinterval_doc},
+ {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval,
+ METH_NOARGS, getbusywaitinterval_doc},
+ {NULL, NULL}
};
#ifdef WAIT_FOR_STDIN
@@ -2963,7 +2963,7 @@ static int stdin_ready = 0;
static void
MyFileProc(void *clientData, int mask)
{
- stdin_ready = 1;
+ stdin_ready = 1;
}
#endif
@@ -2975,57 +2975,57 @@ static int
EventHook(void)
{
#ifndef MS_WINDOWS
- int tfile;
+ int tfile;
#endif
#ifdef WITH_THREAD
- PyEval_RestoreThread(event_tstate);
+ PyEval_RestoreThread(event_tstate);
#endif
- stdin_ready = 0;
- errorInCmd = 0;
+ stdin_ready = 0;
+ errorInCmd = 0;
#ifndef MS_WINDOWS
- tfile = fileno(stdin);
- Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL);
+ tfile = fileno(stdin);
+ Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL);
#endif
- while (!errorInCmd && !stdin_ready) {
- int result;
+ while (!errorInCmd && !stdin_ready) {
+ int result;
#ifdef MS_WINDOWS
- if (_kbhit()) {
- stdin_ready = 1;
- break;
- }
+ if (_kbhit()) {
+ stdin_ready = 1;
+ break;
+ }
#endif
#if defined(WITH_THREAD) || defined(MS_WINDOWS)
- Py_BEGIN_ALLOW_THREADS
- if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
- tcl_tstate = event_tstate;
+ Py_BEGIN_ALLOW_THREADS
+ if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
+ tcl_tstate = event_tstate;
- result = Tcl_DoOneEvent(TCL_DONT_WAIT);
+ result = Tcl_DoOneEvent(TCL_DONT_WAIT);
- tcl_tstate = NULL;
- if(tcl_lock)PyThread_release_lock(tcl_lock);
- if (result == 0)
- Sleep(Tkinter_busywaitinterval);
- Py_END_ALLOW_THREADS
+ tcl_tstate = NULL;
+ if(tcl_lock)PyThread_release_lock(tcl_lock);
+ if (result == 0)
+ Sleep(Tkinter_busywaitinterval);
+ Py_END_ALLOW_THREADS
#else
- result = Tcl_DoOneEvent(0);
+ result = Tcl_DoOneEvent(0);
#endif
- if (result < 0)
- break;
- }
+ if (result < 0)
+ break;
+ }
#ifndef MS_WINDOWS
- Tcl_DeleteFileHandler(tfile);
+ Tcl_DeleteFileHandler(tfile);
#endif
- if (errorInCmd) {
- errorInCmd = 0;
- PyErr_Restore(excInCmd, valInCmd, trbInCmd);
- excInCmd = valInCmd = trbInCmd = NULL;
- PyErr_Print();
- }
+ if (errorInCmd) {
+ errorInCmd = 0;
+ PyErr_Restore(excInCmd, valInCmd, trbInCmd);
+ excInCmd = valInCmd = trbInCmd = NULL;
+ PyErr_Print();
+ }
#ifdef WITH_THREAD
- PyEval_SaveThread();
+ PyEval_SaveThread();
#endif
- return 0;
+ return 0;
}
#endif
@@ -3034,12 +3034,12 @@ static void
EnableEventHook(void)
{
#ifdef WAIT_FOR_STDIN
- if (PyOS_InputHook == NULL) {
+ if (PyOS_InputHook == NULL) {
#ifdef WITH_THREAD
- event_tstate = PyThreadState_Get();
+ event_tstate = PyThreadState_Get();
#endif
- PyOS_InputHook = EventHook;
- }
+ PyOS_InputHook = EventHook;
+ }
#endif
}
@@ -3047,9 +3047,9 @@ static void
DisableEventHook(void)
{
#ifdef WAIT_FOR_STDIN
- if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
- PyOS_InputHook = NULL;
- }
+ if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
+ PyOS_InputHook = NULL;
+ }
#endif
}
@@ -3058,114 +3058,114 @@ DisableEventHook(void)
static void
ins_long(PyObject *d, char *name, long val)
{
- PyObject *v = PyLong_FromLong(val);
- if (v) {
- PyDict_SetItemString(d, name, v);
- Py_DECREF(v);
- }
+ PyObject *v = PyLong_FromLong(val);
+ if (v) {
+ PyDict_SetItemString(d, name, v);
+ Py_DECREF(v);
+ }
}
static void
ins_string(PyObject *d, char *name, char *val)
{
- PyObject *v = PyUnicode_FromString(val);
- if (v) {
- PyDict_SetItemString(d, name, v);
- Py_DECREF(v);
- }
+ PyObject *v = PyUnicode_FromString(val);
+ if (v) {
+ PyDict_SetItemString(d, name, v);
+ Py_DECREF(v);
+ }
}
static struct PyModuleDef _tkintermodule = {
- PyModuleDef_HEAD_INIT,
- "_tkinter",
- NULL,
- -1,
- moduleMethods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_tkinter",
+ NULL,
+ -1,
+ moduleMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__tkinter(void)
{
- PyObject *m, *d, *uexe, *cexe;
+ PyObject *m, *d, *uexe, *cexe;
- if (PyType_Ready(&Tkapp_Type) < 0)
- return NULL;
+ if (PyType_Ready(&Tkapp_Type) < 0)
+ return NULL;
#ifdef WITH_THREAD
- tcl_lock = PyThread_allocate_lock();
+ tcl_lock = PyThread_allocate_lock();
#endif
- m = PyModule_Create(&_tkintermodule);
- if (m == NULL)
- return NULL;
+ m = PyModule_Create(&_tkintermodule);
+ if (m == NULL)
+ return NULL;
- d = PyModule_GetDict(m);
- Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL);
- PyDict_SetItemString(d, "TclError", Tkinter_TclError);
+ d = PyModule_GetDict(m);
+ Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL);
+ PyDict_SetItemString(d, "TclError", Tkinter_TclError);
- ins_long(d, "READABLE", TCL_READABLE);
- ins_long(d, "WRITABLE", TCL_WRITABLE);
- ins_long(d, "EXCEPTION", TCL_EXCEPTION);
- ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS);
- ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS);
- ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS);
- ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS);
- ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS);
- ins_long(d, "DONT_WAIT", TCL_DONT_WAIT);
- ins_string(d, "TK_VERSION", TK_VERSION);
- ins_string(d, "TCL_VERSION", TCL_VERSION);
+ ins_long(d, "READABLE", TCL_READABLE);
+ ins_long(d, "WRITABLE", TCL_WRITABLE);
+ ins_long(d, "EXCEPTION", TCL_EXCEPTION);
+ ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS);
+ ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS);
+ ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS);
+ ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS);
+ ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS);
+ ins_long(d, "DONT_WAIT", TCL_DONT_WAIT);
+ ins_string(d, "TK_VERSION", TK_VERSION);
+ ins_string(d, "TCL_VERSION", TCL_VERSION);
- PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type);
+ PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type);
- if (PyType_Ready(&Tktt_Type) < 0)
- return NULL;
- PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type);
+ if (PyType_Ready(&Tktt_Type) < 0)
+ return NULL;
+ PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type);
- Py_TYPE(&PyTclObject_Type) = &PyType_Type;
- PyDict_SetItemString(d, "Tcl_Obj", (PyObject *)&PyTclObject_Type);
+ Py_TYPE(&PyTclObject_Type) = &PyType_Type;
+ PyDict_SetItemString(d, "Tcl_Obj", (PyObject *)&PyTclObject_Type);
#ifdef TK_AQUA
- /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems
- * start waking up. Note that Tcl_FindExecutable will do this, this
- * code must be above it! The original warning from
- * tkMacOSXAppInit.c is copied below.
- *
- * NB - You have to swap in the Tk Notifier BEFORE you start up the
- * Tcl interpreter for now. It probably should work to do this
- * in the other order, but for now it doesn't seem to.
- *
- */
- Tk_MacOSXSetupTkNotifier();
+ /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems
+ * start waking up. Note that Tcl_FindExecutable will do this, this
+ * code must be above it! The original warning from
+ * tkMacOSXAppInit.c is copied below.
+ *
+ * NB - You have to swap in the Tk Notifier BEFORE you start up the
+ * Tcl interpreter for now. It probably should work to do this
+ * in the other order, but for now it doesn't seem to.
+ *
+ */
+ Tk_MacOSXSetupTkNotifier();
#endif
- /* This helps the dynamic loader; in Unicode aware Tcl versions
- it also helps Tcl find its encodings. */
- uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
- if (uexe) {
- cexe = PyUnicode_AsEncodedString(uexe,
- Py_FileSystemDefaultEncoding,
- NULL);
- if (cexe)
- Tcl_FindExecutable(PyBytes_AsString(cexe));
- Py_XDECREF(cexe);
- Py_DECREF(uexe);
- }
-
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- return NULL;
- }
+ /* This helps the dynamic loader; in Unicode aware Tcl versions
+ it also helps Tcl find its encodings. */
+ uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
+ if (uexe) {
+ cexe = PyUnicode_AsEncodedString(uexe,
+ Py_FileSystemDefaultEncoding,
+ NULL);
+ if (cexe)
+ Tcl_FindExecutable(PyBytes_AsString(cexe));
+ Py_XDECREF(cexe);
+ Py_DECREF(uexe);
+ }
+
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ return NULL;
+ }
#if 0
- /* This was not a good idea; through <Destroy> bindings,
- Tcl_Finalize() may invoke Python code but at that point the
- interpreter and thread state have already been destroyed! */
- Py_AtExit(Tcl_Finalize);
+ /* This was not a good idea; through <Destroy> bindings,
+ Tcl_Finalize() may invoke Python code but at that point the
+ interpreter and thread state have already been destroyed! */
+ Py_AtExit(Tcl_Finalize);
#endif
- return m;
+ return m;
}
diff --git a/Modules/addrinfo.h b/Modules/addrinfo.h
index fe7e96f..1cb6f0e 100644
--- a/Modules/addrinfo.h
+++ b/Modules/addrinfo.h
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -13,7 +13,7 @@
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -55,20 +55,20 @@
#define getaddrinfo fake_getaddrinfo
#endif /* EAI_ADDRFAMILY */
-#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
-#define EAI_AGAIN 2 /* temporary failure in name resolution */
-#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
-#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
-#define EAI_FAMILY 5 /* ai_family not supported */
-#define EAI_MEMORY 6 /* memory allocation failure */
-#define EAI_NODATA 7 /* no address associated with hostname */
-#define EAI_NONAME 8 /* hostname nor servname provided, or not known */
-#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
-#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
-#define EAI_SYSTEM 11 /* system error returned in errno */
-#define EAI_BADHINTS 12
-#define EAI_PROTOCOL 13
-#define EAI_MAX 14
+#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
+#define EAI_AGAIN 2 /* temporary failure in name resolution */
+#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
+#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
+#define EAI_FAMILY 5 /* ai_family not supported */
+#define EAI_MEMORY 6 /* memory allocation failure */
+#define EAI_NODATA 7 /* no address associated with hostname */
+#define EAI_NONAME 8 /* hostname nor servname provided, or not known */
+#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
+#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
+#define EAI_SYSTEM 11 /* system error returned in errno */
+#define EAI_BADHINTS 12
+#define EAI_PROTOCOL 13
+#define EAI_MAX 14
/*
* Flag values for getaddrinfo()
@@ -85,18 +85,18 @@
#undef AI_DEFAULT
#endif /* AI_PASSIVE */
-#define AI_PASSIVE 0x00000001 /* get address to use bind() */
-#define AI_CANONNAME 0x00000002 /* fill ai_canonname */
-#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */
+#define AI_PASSIVE 0x00000001 /* get address to use bind() */
+#define AI_CANONNAME 0x00000002 /* fill ai_canonname */
+#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */
/* valid flags for addrinfo */
-#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST)
+#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST)
-#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
-#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */
-#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */
-#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */
+#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
+#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */
+#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */
+#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */
/* special recommended flags for getipnodebyname */
-#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
+#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
#endif /* !HAVE_GETADDRINFO */
@@ -106,33 +106,33 @@
* Constants for getnameinfo()
*/
#ifndef NI_MAXHOST
-#define NI_MAXHOST 1025
-#define NI_MAXSERV 32
+#define NI_MAXHOST 1025
+#define NI_MAXSERV 32
#endif /* !NI_MAXHOST */
/*
* Flag values for getnameinfo()
*/
#ifndef NI_NOFQDN
-#define NI_NOFQDN 0x00000001
-#define NI_NUMERICHOST 0x00000002
-#define NI_NAMEREQD 0x00000004
-#define NI_NUMERICSERV 0x00000008
-#define NI_DGRAM 0x00000010
+#define NI_NOFQDN 0x00000001
+#define NI_NUMERICHOST 0x00000002
+#define NI_NAMEREQD 0x00000004
+#define NI_NUMERICSERV 0x00000008
+#define NI_DGRAM 0x00000010
#endif /* !NI_NOFQDN */
#endif /* !HAVE_GETNAMEINFO */
#ifndef HAVE_ADDRINFO
struct addrinfo {
- int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
- int ai_family; /* PF_xxx */
- int ai_socktype; /* SOCK_xxx */
- int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
- size_t ai_addrlen; /* length of ai_addr */
- char *ai_canonname; /* canonical name for hostname */
- struct sockaddr *ai_addr; /* binary address */
- struct addrinfo *ai_next; /* next structure in linked list */
+ int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
+ int ai_family; /* PF_xxx */
+ int ai_socktype; /* SOCK_xxx */
+ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
+ size_t ai_addrlen; /* length of ai_addr */
+ char *ai_canonname; /* canonical name for hostname */
+ struct sockaddr *ai_addr; /* binary address */
+ struct addrinfo *ai_next; /* next structure in linked list */
};
#endif /* !HAVE_ADDRINFO */
@@ -140,30 +140,30 @@ struct addrinfo {
/*
* RFC 2553: protocol-independent placeholder for socket addresses
*/
-#define _SS_MAXSIZE 128
+#define _SS_MAXSIZE 128
#ifdef HAVE_LONG_LONG
-#define _SS_ALIGNSIZE (sizeof(PY_LONG_LONG))
+#define _SS_ALIGNSIZE (sizeof(PY_LONG_LONG))
#else
-#define _SS_ALIGNSIZE (sizeof(double))
+#define _SS_ALIGNSIZE (sizeof(double))
#endif /* HAVE_LONG_LONG */
-#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(u_char) * 2)
-#define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(u_char) * 2 - \
- _SS_PAD1SIZE - _SS_ALIGNSIZE)
+#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(u_char) * 2)
+#define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(u_char) * 2 - \
+ _SS_PAD1SIZE - _SS_ALIGNSIZE)
struct sockaddr_storage {
#ifdef HAVE_SOCKADDR_SA_LEN
- unsigned char ss_len; /* address length */
- unsigned char ss_family; /* address family */
+ unsigned char ss_len; /* address length */
+ unsigned char ss_family; /* address family */
#else
- unsigned short ss_family; /* address family */
+ unsigned short ss_family; /* address family */
#endif /* HAVE_SOCKADDR_SA_LEN */
- char __ss_pad1[_SS_PAD1SIZE];
+ char __ss_pad1[_SS_PAD1SIZE];
#ifdef HAVE_LONG_LONG
- PY_LONG_LONG __ss_align; /* force desired structure storage alignment */
+ PY_LONG_LONG __ss_align; /* force desired structure storage alignment */
#else
- double __ss_align; /* force desired structure storage alignment */
+ double __ss_align; /* force desired structure storage alignment */
#endif /* HAVE_LONG_LONG */
- char __ss_pad2[_SS_PAD2SIZE];
+ char __ss_pad2[_SS_PAD2SIZE];
};
#endif /* !HAVE_SOCKADDR_STORAGE */
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index cb7ba8e..b6cffa0 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -11,7 +11,7 @@
#include <stddef.h>
#else /* !STDC_HEADERS */
#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h> /* For size_t */
+#include <sys/types.h> /* For size_t */
#endif /* HAVE_SYS_TYPES_H */
#endif /* !STDC_HEADERS */
@@ -22,20 +22,20 @@ struct arrayobject; /* Forward */
* functions aren't visible yet.
*/
struct arraydescr {
- int typecode;
- int itemsize;
- PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
- int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
- char *formats;
+ int typecode;
+ int itemsize;
+ PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
+ int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
+ char *formats;
};
typedef struct arrayobject {
- PyObject_VAR_HEAD
- char *ob_item;
- Py_ssize_t allocated;
- struct arraydescr *ob_descr;
- PyObject *weakreflist; /* List of weak references */
- int ob_exports; /* Number of exported buffers */
+ PyObject_VAR_HEAD
+ char *ob_item;
+ Py_ssize_t allocated;
+ struct arraydescr *ob_descr;
+ PyObject *weakreflist; /* List of weak references */
+ int ob_exports; /* Number of exported buffers */
} arrayobject;
static PyTypeObject Arraytype;
@@ -46,63 +46,63 @@ static PyTypeObject Arraytype;
static int
array_resize(arrayobject *self, Py_ssize_t newsize)
{
- char *items;
- size_t _new_size;
-
- if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
- PyErr_SetString(PyExc_BufferError,
- "cannot resize an array that is exporting buffers");
- return -1;
- }
-
- /* Bypass realloc() when a previous overallocation is large enough
- to accommodate the newsize. If the newsize is 16 smaller than the
- current size, then proceed with the realloc() to shrink the array.
- */
-
- if (self->allocated >= newsize &&
- Py_SIZE(self) < newsize + 16 &&
- self->ob_item != NULL) {
- Py_SIZE(self) = newsize;
- return 0;
- }
-
- if (newsize == 0) {
- PyMem_FREE(self->ob_item);
- self->ob_item = NULL;
- Py_SIZE(self) = 0;
- self->allocated = 0;
- return 0;
- }
-
- /* This over-allocates proportional to the array size, making room
- * for additional growth. The over-allocation is mild, but is
- * enough to give linear-time amortized behavior over a long
- * sequence of appends() in the presence of a poorly-performing
- * system realloc().
- * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
- * Note, the pattern starts out the same as for lists but then
- * grows at a smaller rate so that larger arrays only overallocate
- * by about 1/16th -- this is done because arrays are presumed to be more
- * memory critical.
- */
-
- _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
- items = self->ob_item;
- /* XXX The following multiplication and division does not optimize away
- like it does for lists since the size is not known at compile time */
- if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
- PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
- else
- items = NULL;
- if (items == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- self->ob_item = items;
- Py_SIZE(self) = newsize;
- self->allocated = _new_size;
- return 0;
+ char *items;
+ size_t _new_size;
+
+ if (self->ob_exports > 0 && newsize != Py_SIZE(self)) {
+ PyErr_SetString(PyExc_BufferError,
+ "cannot resize an array that is exporting buffers");
+ return -1;
+ }
+
+ /* Bypass realloc() when a previous overallocation is large enough
+ to accommodate the newsize. If the newsize is 16 smaller than the
+ current size, then proceed with the realloc() to shrink the array.
+ */
+
+ if (self->allocated >= newsize &&
+ Py_SIZE(self) < newsize + 16 &&
+ self->ob_item != NULL) {
+ Py_SIZE(self) = newsize;
+ return 0;
+ }
+
+ if (newsize == 0) {
+ PyMem_FREE(self->ob_item);
+ self->ob_item = NULL;
+ Py_SIZE(self) = 0;
+ self->allocated = 0;
+ return 0;
+ }
+
+ /* This over-allocates proportional to the array size, making room
+ * for additional growth. The over-allocation is mild, but is
+ * enough to give linear-time amortized behavior over a long
+ * sequence of appends() in the presence of a poorly-performing
+ * system realloc().
+ * The growth pattern is: 0, 4, 8, 16, 25, 34, 46, 56, 67, 79, ...
+ * Note, the pattern starts out the same as for lists but then
+ * grows at a smaller rate so that larger arrays only overallocate
+ * by about 1/16th -- this is done because arrays are presumed to be more
+ * memory critical.
+ */
+
+ _new_size = (newsize >> 4) + (Py_SIZE(self) < 8 ? 3 : 7) + newsize;
+ items = self->ob_item;
+ /* XXX The following multiplication and division does not optimize away
+ like it does for lists since the size is not known at compile time */
+ if (_new_size <= ((~(size_t)0) / self->ob_descr->itemsize))
+ PyMem_RESIZE(items, char, (_new_size * self->ob_descr->itemsize));
+ else
+ items = NULL;
+ if (items == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ self->ob_item = items;
+ Py_SIZE(self) = newsize;
+ self->allocated = _new_size;
+ return 0;
}
/****************************************************************************
@@ -120,289 +120,289 @@ in bounds; that's the responsibility of the caller.
static PyObject *
b_getitem(arrayobject *ap, Py_ssize_t i)
{
- long x = ((char *)ap->ob_item)[i];
- if (x >= 128)
- x -= 256;
- return PyLong_FromLong(x);
+ long x = ((char *)ap->ob_item)[i];
+ if (x >= 128)
+ x -= 256;
+ return PyLong_FromLong(x);
}
static int
b_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- short x;
- /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
- must use the next size up that is signed ('h') and manually do
- the overflow checking */
- if (!PyArg_Parse(v, "h;array item must be integer", &x))
- return -1;
- else if (x < -128) {
- PyErr_SetString(PyExc_OverflowError,
- "signed char is less than minimum");
- return -1;
- }
- else if (x > 127) {
- PyErr_SetString(PyExc_OverflowError,
- "signed char is greater than maximum");
- return -1;
- }
- if (i >= 0)
- ((char *)ap->ob_item)[i] = (char)x;
- return 0;
+ short x;
+ /* PyArg_Parse's 'b' formatter is for an unsigned char, therefore
+ must use the next size up that is signed ('h') and manually do
+ the overflow checking */
+ if (!PyArg_Parse(v, "h;array item must be integer", &x))
+ return -1;
+ else if (x < -128) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed char is less than minimum");
+ return -1;
+ }
+ else if (x > 127) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed char is greater than maximum");
+ return -1;
+ }
+ if (i >= 0)
+ ((char *)ap->ob_item)[i] = (char)x;
+ return 0;
}
static PyObject *
BB_getitem(arrayobject *ap, Py_ssize_t i)
{
- long x = ((unsigned char *)ap->ob_item)[i];
- return PyLong_FromLong(x);
+ long x = ((unsigned char *)ap->ob_item)[i];
+ return PyLong_FromLong(x);
}
static int
BB_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- unsigned char x;
- /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
- if (!PyArg_Parse(v, "b;array item must be integer", &x))
- return -1;
- if (i >= 0)
- ((char *)ap->ob_item)[i] = x;
- return 0;
+ unsigned char x;
+ /* 'B' == unsigned char, maps to PyArg_Parse's 'b' formatter */
+ if (!PyArg_Parse(v, "b;array item must be integer", &x))
+ return -1;
+ if (i >= 0)
+ ((char *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
u_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
+ return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
}
static int
u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- Py_UNICODE *p;
- Py_ssize_t len;
+ Py_UNICODE *p;
+ Py_ssize_t len;
- if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
- return -1;
- if (len != 1) {
- PyErr_SetString(PyExc_TypeError,
- "array item must be unicode character");
- return -1;
- }
- if (i >= 0)
- ((Py_UNICODE *)ap->ob_item)[i] = p[0];
- return 0;
+ if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
+ return -1;
+ if (len != 1) {
+ PyErr_SetString(PyExc_TypeError,
+ "array item must be unicode character");
+ return -1;
+ }
+ if (i >= 0)
+ ((Py_UNICODE *)ap->ob_item)[i] = p[0];
+ return 0;
}
static PyObject *
h_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
+ return PyLong_FromLong((long) ((short *)ap->ob_item)[i]);
}
static int
h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- short x;
- /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
- if (!PyArg_Parse(v, "h;array item must be integer", &x))
- return -1;
- if (i >= 0)
- ((short *)ap->ob_item)[i] = x;
- return 0;
+ short x;
+ /* 'h' == signed short, maps to PyArg_Parse's 'h' formatter */
+ if (!PyArg_Parse(v, "h;array item must be integer", &x))
+ return -1;
+ if (i >= 0)
+ ((short *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
HH_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
+ return PyLong_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
}
static int
HH_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- int x;
- /* PyArg_Parse's 'h' formatter is for a signed short, therefore
- must use the next size up and manually do the overflow checking */
- if (!PyArg_Parse(v, "i;array item must be integer", &x))
- return -1;
- else if (x < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned short is less than minimum");
- return -1;
- }
- else if (x > USHRT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned short is greater than maximum");
- return -1;
- }
- if (i >= 0)
- ((short *)ap->ob_item)[i] = (short)x;
- return 0;
+ int x;
+ /* PyArg_Parse's 'h' formatter is for a signed short, therefore
+ must use the next size up and manually do the overflow checking */
+ if (!PyArg_Parse(v, "i;array item must be integer", &x))
+ return -1;
+ else if (x < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned short is less than minimum");
+ return -1;
+ }
+ else if (x > USHRT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned short is greater than maximum");
+ return -1;
+ }
+ if (i >= 0)
+ ((short *)ap->ob_item)[i] = (short)x;
+ return 0;
}
static PyObject *
i_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
+ return PyLong_FromLong((long) ((int *)ap->ob_item)[i]);
}
static int
i_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- int x;
- /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
- if (!PyArg_Parse(v, "i;array item must be integer", &x))
- return -1;
- if (i >= 0)
- ((int *)ap->ob_item)[i] = x;
- return 0;
+ int x;
+ /* 'i' == signed int, maps to PyArg_Parse's 'i' formatter */
+ if (!PyArg_Parse(v, "i;array item must be integer", &x))
+ return -1;
+ if (i >= 0)
+ ((int *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
II_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromUnsignedLong(
- (unsigned long) ((unsigned int *)ap->ob_item)[i]);
+ return PyLong_FromUnsignedLong(
+ (unsigned long) ((unsigned int *)ap->ob_item)[i]);
}
static int
II_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- unsigned long x;
- if (PyLong_Check(v)) {
- x = PyLong_AsUnsignedLong(v);
- if (x == (unsigned long) -1 && PyErr_Occurred())
- return -1;
- }
- else {
- long y;
- if (!PyArg_Parse(v, "l;array item must be integer", &y))
- return -1;
- if (y < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned int is less than minimum");
- return -1;
- }
- x = (unsigned long)y;
-
- }
- if (x > UINT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned int is greater than maximum");
- return -1;
- }
-
- if (i >= 0)
- ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
- return 0;
+ unsigned long x;
+ if (PyLong_Check(v)) {
+ x = PyLong_AsUnsignedLong(v);
+ if (x == (unsigned long) -1 && PyErr_Occurred())
+ return -1;
+ }
+ else {
+ long y;
+ if (!PyArg_Parse(v, "l;array item must be integer", &y))
+ return -1;
+ if (y < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned int is less than minimum");
+ return -1;
+ }
+ x = (unsigned long)y;
+
+ }
+ if (x > UINT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned int is greater than maximum");
+ return -1;
+ }
+
+ if (i >= 0)
+ ((unsigned int *)ap->ob_item)[i] = (unsigned int)x;
+ return 0;
}
static PyObject *
l_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromLong(((long *)ap->ob_item)[i]);
+ return PyLong_FromLong(((long *)ap->ob_item)[i]);
}
static int
l_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- long x;
- if (!PyArg_Parse(v, "l;array item must be integer", &x))
- return -1;
- if (i >= 0)
- ((long *)ap->ob_item)[i] = x;
- return 0;
+ long x;
+ if (!PyArg_Parse(v, "l;array item must be integer", &x))
+ return -1;
+ if (i >= 0)
+ ((long *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
LL_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
+ return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
}
static int
LL_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- unsigned long x;
- if (PyLong_Check(v)) {
- x = PyLong_AsUnsignedLong(v);
- if (x == (unsigned long) -1 && PyErr_Occurred())
- return -1;
- }
- else {
- long y;
- if (!PyArg_Parse(v, "l;array item must be integer", &y))
- return -1;
- if (y < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned long is less than minimum");
- return -1;
- }
- x = (unsigned long)y;
-
- }
- if (x > ULONG_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned long is greater than maximum");
- return -1;
- }
-
- if (i >= 0)
- ((unsigned long *)ap->ob_item)[i] = x;
- return 0;
+ unsigned long x;
+ if (PyLong_Check(v)) {
+ x = PyLong_AsUnsignedLong(v);
+ if (x == (unsigned long) -1 && PyErr_Occurred())
+ return -1;
+ }
+ else {
+ long y;
+ if (!PyArg_Parse(v, "l;array item must be integer", &y))
+ return -1;
+ if (y < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned long is less than minimum");
+ return -1;
+ }
+ x = (unsigned long)y;
+
+ }
+ if (x > ULONG_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned long is greater than maximum");
+ return -1;
+ }
+
+ if (i >= 0)
+ ((unsigned long *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
f_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
+ return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
}
static int
f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- float x;
- if (!PyArg_Parse(v, "f;array item must be float", &x))
- return -1;
- if (i >= 0)
- ((float *)ap->ob_item)[i] = x;
- return 0;
+ float x;
+ if (!PyArg_Parse(v, "f;array item must be float", &x))
+ return -1;
+ if (i >= 0)
+ ((float *)ap->ob_item)[i] = x;
+ return 0;
}
static PyObject *
d_getitem(arrayobject *ap, Py_ssize_t i)
{
- return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
+ return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
}
static int
d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
{
- double x;
- if (!PyArg_Parse(v, "d;array item must be float", &x))
- return -1;
- if (i >= 0)
- ((double *)ap->ob_item)[i] = x;
- return 0;
+ double x;
+ if (!PyArg_Parse(v, "d;array item must be float", &x))
+ return -1;
+ if (i >= 0)
+ ((double *)ap->ob_item)[i] = x;
+ return 0;
}
/* Description of types */
static struct arraydescr descriptors[] = {
- {'b', 1, b_getitem, b_setitem, "b"},
- {'B', 1, BB_getitem, BB_setitem, "B"},
- {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
- {'h', sizeof(short), h_getitem, h_setitem, "h"},
- {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
- {'i', sizeof(int), i_getitem, i_setitem, "i"},
- {'I', sizeof(int), II_getitem, II_setitem, "I"},
- {'l', sizeof(long), l_getitem, l_setitem, "l"},
- {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
- {'f', sizeof(float), f_getitem, f_setitem, "f"},
- {'d', sizeof(double), d_getitem, d_setitem, "d"},
- {'\0', 0, 0, 0, 0} /* Sentinel */
+ {'b', 1, b_getitem, b_setitem, "b"},
+ {'B', 1, BB_getitem, BB_setitem, "B"},
+ {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u"},
+ {'h', sizeof(short), h_getitem, h_setitem, "h"},
+ {'H', sizeof(short), HH_getitem, HH_setitem, "H"},
+ {'i', sizeof(int), i_getitem, i_setitem, "i"},
+ {'I', sizeof(int), II_getitem, II_setitem, "I"},
+ {'l', sizeof(long), l_getitem, l_setitem, "l"},
+ {'L', sizeof(long), LL_getitem, LL_setitem, "L"},
+ {'f', sizeof(float), f_getitem, f_setitem, "f"},
+ {'d', sizeof(double), d_getitem, d_setitem, "d"},
+ {'\0', 0, 0, 0, 0} /* Sentinel */
};
/****************************************************************************
@@ -412,79 +412,79 @@ Implementations of array object methods.
static PyObject *
newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
{
- arrayobject *op;
- size_t nbytes;
-
- if (size < 0) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- nbytes = size * descr->itemsize;
- /* Check for overflow */
- if (nbytes / descr->itemsize != (size_t)size) {
- return PyErr_NoMemory();
- }
- op = (arrayobject *) type->tp_alloc(type, 0);
- if (op == NULL) {
- return NULL;
- }
- op->ob_descr = descr;
- op->allocated = size;
- op->weakreflist = NULL;
- Py_SIZE(op) = size;
- if (size <= 0) {
- op->ob_item = NULL;
- }
- else {
- op->ob_item = PyMem_NEW(char, nbytes);
- if (op->ob_item == NULL) {
- Py_DECREF(op);
- return PyErr_NoMemory();
- }
- }
- op->ob_exports = 0;
- return (PyObject *) op;
+ arrayobject *op;
+ size_t nbytes;
+
+ if (size < 0) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ nbytes = size * descr->itemsize;
+ /* Check for overflow */
+ if (nbytes / descr->itemsize != (size_t)size) {
+ return PyErr_NoMemory();
+ }
+ op = (arrayobject *) type->tp_alloc(type, 0);
+ if (op == NULL) {
+ return NULL;
+ }
+ op->ob_descr = descr;
+ op->allocated = size;
+ op->weakreflist = NULL;
+ Py_SIZE(op) = size;
+ if (size <= 0) {
+ op->ob_item = NULL;
+ }
+ else {
+ op->ob_item = PyMem_NEW(char, nbytes);
+ if (op->ob_item == NULL) {
+ Py_DECREF(op);
+ return PyErr_NoMemory();
+ }
+ }
+ op->ob_exports = 0;
+ return (PyObject *) op;
}
static PyObject *
getarrayitem(PyObject *op, Py_ssize_t i)
{
- register arrayobject *ap;
- assert(array_Check(op));
- ap = (arrayobject *)op;
- assert(i>=0 && i<Py_SIZE(ap));
- return (*ap->ob_descr->getitem)(ap, i);
+ register arrayobject *ap;
+ assert(array_Check(op));
+ ap = (arrayobject *)op;
+ assert(i>=0 && i<Py_SIZE(ap));
+ return (*ap->ob_descr->getitem)(ap, i);
}
static int
ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
{
- char *items;
- Py_ssize_t n = Py_SIZE(self);
- if (v == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
- if ((*self->ob_descr->setitem)(self, -1, v) < 0)
- return -1;
-
- if (array_resize(self, n+1) == -1)
- return -1;
- items = self->ob_item;
- if (where < 0) {
- where += n;
- if (where < 0)
- where = 0;
- }
- if (where > n)
- where = n;
- /* appends don't need to call memmove() */
- if (where != n)
- memmove(items + (where+1)*self->ob_descr->itemsize,
- items + where*self->ob_descr->itemsize,
- (n-where)*self->ob_descr->itemsize);
- return (*self->ob_descr->setitem)(self, where, v);
+ char *items;
+ Py_ssize_t n = Py_SIZE(self);
+ if (v == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if ((*self->ob_descr->setitem)(self, -1, v) < 0)
+ return -1;
+
+ if (array_resize(self, n+1) == -1)
+ return -1;
+ items = self->ob_item;
+ if (where < 0) {
+ where += n;
+ if (where < 0)
+ where = 0;
+ }
+ if (where > n)
+ where = n;
+ /* appends don't need to call memmove() */
+ if (where != n)
+ memmove(items + (where+1)*self->ob_descr->itemsize,
+ items + where*self->ob_descr->itemsize,
+ (n-where)*self->ob_descr->itemsize);
+ return (*self->ob_descr->setitem)(self, where, v);
}
/* Methods */
@@ -492,141 +492,141 @@ ins1(arrayobject *self, Py_ssize_t where, PyObject *v)
static void
array_dealloc(arrayobject *op)
{
- if (op->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) op);
- if (op->ob_item != NULL)
- PyMem_DEL(op->ob_item);
- Py_TYPE(op)->tp_free((PyObject *)op);
+ if (op->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) op);
+ if (op->ob_item != NULL)
+ PyMem_DEL(op->ob_item);
+ Py_TYPE(op)->tp_free((PyObject *)op);
}
static PyObject *
array_richcompare(PyObject *v, PyObject *w, int op)
{
- arrayobject *va, *wa;
- PyObject *vi = NULL;
- PyObject *wi = NULL;
- Py_ssize_t i, k;
- PyObject *res;
-
- if (!array_Check(v) || !array_Check(w)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- va = (arrayobject *)v;
- wa = (arrayobject *)w;
-
- if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
- /* Shortcut: if the lengths differ, the arrays differ */
- if (op == Py_EQ)
- res = Py_False;
- else
- res = Py_True;
- Py_INCREF(res);
- return res;
- }
-
- /* Search for the first index where items are different */
- k = 1;
- for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
- vi = getarrayitem(v, i);
- wi = getarrayitem(w, i);
- if (vi == NULL || wi == NULL) {
- Py_XDECREF(vi);
- Py_XDECREF(wi);
- return NULL;
- }
- k = PyObject_RichCompareBool(vi, wi, Py_EQ);
- if (k == 0)
- break; /* Keeping vi and wi alive! */
- Py_DECREF(vi);
- Py_DECREF(wi);
- if (k < 0)
- return NULL;
- }
-
- if (k) {
- /* No more items to compare -- compare sizes */
- Py_ssize_t vs = Py_SIZE(va);
- Py_ssize_t ws = Py_SIZE(wa);
- int cmp;
- switch (op) {
- case Py_LT: cmp = vs < ws; break;
- case Py_LE: cmp = vs <= ws; break;
- case Py_EQ: cmp = vs == ws; break;
- case Py_NE: cmp = vs != ws; break;
- case Py_GT: cmp = vs > ws; break;
- case Py_GE: cmp = vs >= ws; break;
- default: return NULL; /* cannot happen */
- }
- if (cmp)
- res = Py_True;
- else
- res = Py_False;
- Py_INCREF(res);
- return res;
- }
-
- /* We have an item that differs. First, shortcuts for EQ/NE */
- if (op == Py_EQ) {
- Py_INCREF(Py_False);
- res = Py_False;
- }
- else if (op == Py_NE) {
- Py_INCREF(Py_True);
- res = Py_True;
- }
- else {
- /* Compare the final item again using the proper operator */
- res = PyObject_RichCompare(vi, wi, op);
- }
- Py_DECREF(vi);
- Py_DECREF(wi);
- return res;
+ arrayobject *va, *wa;
+ PyObject *vi = NULL;
+ PyObject *wi = NULL;
+ Py_ssize_t i, k;
+ PyObject *res;
+
+ if (!array_Check(v) || !array_Check(w)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ va = (arrayobject *)v;
+ wa = (arrayobject *)w;
+
+ if (Py_SIZE(va) != Py_SIZE(wa) && (op == Py_EQ || op == Py_NE)) {
+ /* Shortcut: if the lengths differ, the arrays differ */
+ if (op == Py_EQ)
+ res = Py_False;
+ else
+ res = Py_True;
+ Py_INCREF(res);
+ return res;
+ }
+
+ /* Search for the first index where items are different */
+ k = 1;
+ for (i = 0; i < Py_SIZE(va) && i < Py_SIZE(wa); i++) {
+ vi = getarrayitem(v, i);
+ wi = getarrayitem(w, i);
+ if (vi == NULL || wi == NULL) {
+ Py_XDECREF(vi);
+ Py_XDECREF(wi);
+ return NULL;
+ }
+ k = PyObject_RichCompareBool(vi, wi, Py_EQ);
+ if (k == 0)
+ break; /* Keeping vi and wi alive! */
+ Py_DECREF(vi);
+ Py_DECREF(wi);
+ if (k < 0)
+ return NULL;
+ }
+
+ if (k) {
+ /* No more items to compare -- compare sizes */
+ Py_ssize_t vs = Py_SIZE(va);
+ Py_ssize_t ws = Py_SIZE(wa);
+ int cmp;
+ switch (op) {
+ case Py_LT: cmp = vs < ws; break;
+ case Py_LE: cmp = vs <= ws; break;
+ case Py_EQ: cmp = vs == ws; break;
+ case Py_NE: cmp = vs != ws; break;
+ case Py_GT: cmp = vs > ws; break;
+ case Py_GE: cmp = vs >= ws; break;
+ default: return NULL; /* cannot happen */
+ }
+ if (cmp)
+ res = Py_True;
+ else
+ res = Py_False;
+ Py_INCREF(res);
+ return res;
+ }
+
+ /* We have an item that differs. First, shortcuts for EQ/NE */
+ if (op == Py_EQ) {
+ Py_INCREF(Py_False);
+ res = Py_False;
+ }
+ else if (op == Py_NE) {
+ Py_INCREF(Py_True);
+ res = Py_True;
+ }
+ else {
+ /* Compare the final item again using the proper operator */
+ res = PyObject_RichCompare(vi, wi, op);
+ }
+ Py_DECREF(vi);
+ Py_DECREF(wi);
+ return res;
}
static Py_ssize_t
array_length(arrayobject *a)
{
- return Py_SIZE(a);
+ return Py_SIZE(a);
}
static PyObject *
array_item(arrayobject *a, Py_ssize_t i)
{
- if (i < 0 || i >= Py_SIZE(a)) {
- PyErr_SetString(PyExc_IndexError, "array index out of range");
- return NULL;
- }
- return getarrayitem((PyObject *)a, i);
+ if (i < 0 || i >= Py_SIZE(a)) {
+ PyErr_SetString(PyExc_IndexError, "array index out of range");
+ return NULL;
+ }
+ return getarrayitem((PyObject *)a, i);
}
static PyObject *
array_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
{
- arrayobject *np;
- if (ilow < 0)
- ilow = 0;
- else if (ilow > Py_SIZE(a))
- ilow = Py_SIZE(a);
- if (ihigh < 0)
- ihigh = 0;
- if (ihigh < ilow)
- ihigh = ilow;
- else if (ihigh > Py_SIZE(a))
- ihigh = Py_SIZE(a);
- np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
- if (np == NULL)
- return NULL;
- memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
- (ihigh-ilow) * a->ob_descr->itemsize);
- return (PyObject *)np;
+ arrayobject *np;
+ if (ilow < 0)
+ ilow = 0;
+ else if (ilow > Py_SIZE(a))
+ ilow = Py_SIZE(a);
+ if (ihigh < 0)
+ ihigh = 0;
+ if (ihigh < ilow)
+ ihigh = ilow;
+ else if (ihigh > Py_SIZE(a))
+ ihigh = Py_SIZE(a);
+ np = (arrayobject *) newarrayobject(&Arraytype, ihigh - ilow, a->ob_descr);
+ if (np == NULL)
+ return NULL;
+ memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
+ (ihigh-ilow) * a->ob_descr->itemsize);
+ return (PyObject *)np;
}
static PyObject *
array_copy(arrayobject *a, PyObject *unused)
{
- return array_slice(a, 0, Py_SIZE(a));
+ return array_slice(a, 0, Py_SIZE(a));
}
PyDoc_STRVAR(copy_doc,
@@ -637,278 +637,278 @@ PyDoc_STRVAR(copy_doc,
static PyObject *
array_concat(arrayobject *a, PyObject *bb)
{
- Py_ssize_t size;
- arrayobject *np;
- if (!array_Check(bb)) {
- PyErr_Format(PyExc_TypeError,
- "can only append array (not \"%.200s\") to array",
- Py_TYPE(bb)->tp_name);
- return NULL;
- }
+ Py_ssize_t size;
+ arrayobject *np;
+ if (!array_Check(bb)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only append array (not \"%.200s\") to array",
+ Py_TYPE(bb)->tp_name);
+ return NULL;
+ }
#define b ((arrayobject *)bb)
- if (a->ob_descr != b->ob_descr) {
- PyErr_BadArgument();
- return NULL;
- }
- if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
- return PyErr_NoMemory();
- }
- size = Py_SIZE(a) + Py_SIZE(b);
- np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
- if (np == NULL) {
- return NULL;
- }
- memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
- memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
- b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
- return (PyObject *)np;
+ if (a->ob_descr != b->ob_descr) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) {
+ return PyErr_NoMemory();
+ }
+ size = Py_SIZE(a) + Py_SIZE(b);
+ np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
+ if (np == NULL) {
+ return NULL;
+ }
+ memcpy(np->ob_item, a->ob_item, Py_SIZE(a)*a->ob_descr->itemsize);
+ memcpy(np->ob_item + Py_SIZE(a)*a->ob_descr->itemsize,
+ b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
+ return (PyObject *)np;
#undef b
}
static PyObject *
array_repeat(arrayobject *a, Py_ssize_t n)
{
- Py_ssize_t i;
- Py_ssize_t size;
- arrayobject *np;
- char *p;
- Py_ssize_t nbytes;
- if (n < 0)
- n = 0;
- if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
- return PyErr_NoMemory();
- }
- size = Py_SIZE(a) * n;
- np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
- if (np == NULL)
- return NULL;
- p = np->ob_item;
- nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
- for (i = 0; i < n; i++) {
- memcpy(p, a->ob_item, nbytes);
- p += nbytes;
- }
- return (PyObject *) np;
+ Py_ssize_t i;
+ Py_ssize_t size;
+ arrayobject *np;
+ char *p;
+ Py_ssize_t nbytes;
+ if (n < 0)
+ n = 0;
+ if ((Py_SIZE(a) != 0) && (n > PY_SSIZE_T_MAX / Py_SIZE(a))) {
+ return PyErr_NoMemory();
+ }
+ size = Py_SIZE(a) * n;
+ np = (arrayobject *) newarrayobject(&Arraytype, size, a->ob_descr);
+ if (np == NULL)
+ return NULL;
+ p = np->ob_item;
+ nbytes = Py_SIZE(a) * a->ob_descr->itemsize;
+ for (i = 0; i < n; i++) {
+ memcpy(p, a->ob_item, nbytes);
+ p += nbytes;
+ }
+ return (PyObject *) np;
}
static int
array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
- char *item;
- Py_ssize_t n; /* Size of replacement array */
- Py_ssize_t d; /* Change in size */
+ char *item;
+ Py_ssize_t n; /* Size of replacement array */
+ Py_ssize_t d; /* Change in size */
#define b ((arrayobject *)v)
- if (v == NULL)
- n = 0;
- else if (array_Check(v)) {
- n = Py_SIZE(b);
- if (a == b) {
- /* Special case "a[i:j] = a" -- copy b first */
- int ret;
- v = array_slice(b, 0, n);
- if (!v)
- return -1;
- ret = array_ass_slice(a, ilow, ihigh, v);
- Py_DECREF(v);
- return ret;
- }
- if (b->ob_descr != a->ob_descr) {
- PyErr_BadArgument();
- return -1;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "can only assign array (not \"%.200s\") to array slice",
- Py_TYPE(v)->tp_name);
- return -1;
- }
- if (ilow < 0)
- ilow = 0;
- else if (ilow > Py_SIZE(a))
- ilow = Py_SIZE(a);
- if (ihigh < 0)
- ihigh = 0;
- if (ihigh < ilow)
- ihigh = ilow;
- else if (ihigh > Py_SIZE(a))
- ihigh = Py_SIZE(a);
- item = a->ob_item;
- d = n - (ihigh-ilow);
- /* Issue #4509: If the array has exported buffers and the slice
- assignment would change the size of the array, fail early to make
- sure we don't modify it. */
- if (d != 0 && a->ob_exports > 0) {
- PyErr_SetString(PyExc_BufferError,
- "cannot resize an array that is exporting buffers");
- return -1;
- }
- if (d < 0) { /* Delete -d items */
- memmove(item + (ihigh+d)*a->ob_descr->itemsize,
- item + ihigh*a->ob_descr->itemsize,
- (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
- if (array_resize(a, Py_SIZE(a) + d) == -1)
- return -1;
- }
- else if (d > 0) { /* Insert d items */
- if (array_resize(a, Py_SIZE(a) + d))
- return -1;
- memmove(item + (ihigh+d)*a->ob_descr->itemsize,
- item + ihigh*a->ob_descr->itemsize,
- (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
- }
- if (n > 0)
- memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
- n*b->ob_descr->itemsize);
- return 0;
+ if (v == NULL)
+ n = 0;
+ else if (array_Check(v)) {
+ n = Py_SIZE(b);
+ if (a == b) {
+ /* Special case "a[i:j] = a" -- copy b first */
+ int ret;
+ v = array_slice(b, 0, n);
+ if (!v)
+ return -1;
+ ret = array_ass_slice(a, ilow, ihigh, v);
+ Py_DECREF(v);
+ return ret;
+ }
+ if (b->ob_descr != a->ob_descr) {
+ PyErr_BadArgument();
+ return -1;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "can only assign array (not \"%.200s\") to array slice",
+ Py_TYPE(v)->tp_name);
+ return -1;
+ }
+ if (ilow < 0)
+ ilow = 0;
+ else if (ilow > Py_SIZE(a))
+ ilow = Py_SIZE(a);
+ if (ihigh < 0)
+ ihigh = 0;
+ if (ihigh < ilow)
+ ihigh = ilow;
+ else if (ihigh > Py_SIZE(a))
+ ihigh = Py_SIZE(a);
+ item = a->ob_item;
+ d = n - (ihigh-ilow);
+ /* Issue #4509: If the array has exported buffers and the slice
+ assignment would change the size of the array, fail early to make
+ sure we don't modify it. */
+ if (d != 0 && a->ob_exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "cannot resize an array that is exporting buffers");
+ return -1;
+ }
+ if (d < 0) { /* Delete -d items */
+ memmove(item + (ihigh+d)*a->ob_descr->itemsize,
+ item + ihigh*a->ob_descr->itemsize,
+ (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
+ if (array_resize(a, Py_SIZE(a) + d) == -1)
+ return -1;
+ }
+ else if (d > 0) { /* Insert d items */
+ if (array_resize(a, Py_SIZE(a) + d))
+ return -1;
+ memmove(item + (ihigh+d)*a->ob_descr->itemsize,
+ item + ihigh*a->ob_descr->itemsize,
+ (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize);
+ }
+ if (n > 0)
+ memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
+ n*b->ob_descr->itemsize);
+ return 0;
#undef b
}
static int
array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v)
{
- if (i < 0 || i >= Py_SIZE(a)) {
- PyErr_SetString(PyExc_IndexError,
- "array assignment index out of range");
- return -1;
- }
- if (v == NULL)
- return array_ass_slice(a, i, i+1, v);
- return (*a->ob_descr->setitem)(a, i, v);
+ if (i < 0 || i >= Py_SIZE(a)) {
+ PyErr_SetString(PyExc_IndexError,
+ "array assignment index out of range");
+ return -1;
+ }
+ if (v == NULL)
+ return array_ass_slice(a, i, i+1, v);
+ return (*a->ob_descr->setitem)(a, i, v);
}
static int
setarrayitem(PyObject *a, Py_ssize_t i, PyObject *v)
{
- assert(array_Check(a));
- return array_ass_item((arrayobject *)a, i, v);
+ assert(array_Check(a));
+ return array_ass_item((arrayobject *)a, i, v);
}
static int
array_iter_extend(arrayobject *self, PyObject *bb)
{
- PyObject *it, *v;
+ PyObject *it, *v;
- it = PyObject_GetIter(bb);
- if (it == NULL)
- return -1;
+ it = PyObject_GetIter(bb);
+ if (it == NULL)
+ return -1;
- while ((v = PyIter_Next(it)) != NULL) {
- if (ins1(self, (int) Py_SIZE(self), v) != 0) {
- Py_DECREF(v);
- Py_DECREF(it);
- return -1;
- }
- Py_DECREF(v);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return -1;
- return 0;
+ while ((v = PyIter_Next(it)) != NULL) {
+ if (ins1(self, (int) Py_SIZE(self), v) != 0) {
+ Py_DECREF(v);
+ Py_DECREF(it);
+ return -1;
+ }
+ Py_DECREF(v);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return -1;
+ return 0;
}
static int
array_do_extend(arrayobject *self, PyObject *bb)
{
- Py_ssize_t size, oldsize, bbsize;
-
- if (!array_Check(bb))
- return array_iter_extend(self, bb);
+ Py_ssize_t size, oldsize, bbsize;
+
+ if (!array_Check(bb))
+ return array_iter_extend(self, bb);
#define b ((arrayobject *)bb)
- if (self->ob_descr != b->ob_descr) {
- PyErr_SetString(PyExc_TypeError,
- "can only extend with array of same kind");
- return -1;
- }
- if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
- ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
- PyErr_NoMemory();
- return -1;
- }
- oldsize = Py_SIZE(self);
- /* Get the size of bb before resizing the array since bb could be self. */
- bbsize = Py_SIZE(bb);
- size = oldsize + Py_SIZE(b);
- if (array_resize(self, size) == -1)
- return -1;
- memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
- b->ob_item, bbsize * b->ob_descr->itemsize);
-
- return 0;
+ if (self->ob_descr != b->ob_descr) {
+ PyErr_SetString(PyExc_TypeError,
+ "can only extend with array of same kind");
+ return -1;
+ }
+ if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
+ ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ oldsize = Py_SIZE(self);
+ /* Get the size of bb before resizing the array since bb could be self. */
+ bbsize = Py_SIZE(bb);
+ size = oldsize + Py_SIZE(b);
+ if (array_resize(self, size) == -1)
+ return -1;
+ memcpy(self->ob_item + oldsize * self->ob_descr->itemsize,
+ b->ob_item, bbsize * b->ob_descr->itemsize);
+
+ return 0;
#undef b
}
static PyObject *
array_inplace_concat(arrayobject *self, PyObject *bb)
{
- if (!array_Check(bb)) {
- PyErr_Format(PyExc_TypeError,
- "can only extend array with array (not \"%.200s\")",
- Py_TYPE(bb)->tp_name);
- return NULL;
- }
- if (array_do_extend(self, bb) == -1)
- return NULL;
- Py_INCREF(self);
- return (PyObject *)self;
+ if (!array_Check(bb)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only extend array with array (not \"%.200s\")",
+ Py_TYPE(bb)->tp_name);
+ return NULL;
+ }
+ if (array_do_extend(self, bb) == -1)
+ return NULL;
+ Py_INCREF(self);
+ return (PyObject *)self;
}
static PyObject *
array_inplace_repeat(arrayobject *self, Py_ssize_t n)
{
- char *items, *p;
- Py_ssize_t size, i;
-
- if (Py_SIZE(self) > 0) {
- if (n < 0)
- n = 0;
- items = self->ob_item;
- if ((self->ob_descr->itemsize != 0) &&
- (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
- return PyErr_NoMemory();
- }
- size = Py_SIZE(self) * self->ob_descr->itemsize;
- if (n > 0 && size > PY_SSIZE_T_MAX / n) {
- return PyErr_NoMemory();
- }
- if (array_resize(self, n * Py_SIZE(self)) == -1)
- return NULL;
- items = p = self->ob_item;
- for (i = 1; i < n; i++) {
- p += size;
- memcpy(p, items, size);
- }
- }
- Py_INCREF(self);
- return (PyObject *)self;
+ char *items, *p;
+ Py_ssize_t size, i;
+
+ if (Py_SIZE(self) > 0) {
+ if (n < 0)
+ n = 0;
+ items = self->ob_item;
+ if ((self->ob_descr->itemsize != 0) &&
+ (Py_SIZE(self) > PY_SSIZE_T_MAX / self->ob_descr->itemsize)) {
+ return PyErr_NoMemory();
+ }
+ size = Py_SIZE(self) * self->ob_descr->itemsize;
+ if (n > 0 && size > PY_SSIZE_T_MAX / n) {
+ return PyErr_NoMemory();
+ }
+ if (array_resize(self, n * Py_SIZE(self)) == -1)
+ return NULL;
+ items = p = self->ob_item;
+ for (i = 1; i < n; i++) {
+ p += size;
+ memcpy(p, items, size);
+ }
+ }
+ Py_INCREF(self);
+ return (PyObject *)self;
}
static PyObject *
ins(arrayobject *self, Py_ssize_t where, PyObject *v)
{
- if (ins1(self, where, v) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (ins1(self, where, v) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
array_count(arrayobject *self, PyObject *v)
{
- Py_ssize_t count = 0;
- Py_ssize_t i;
+ Py_ssize_t count = 0;
+ Py_ssize_t i;
- for (i = 0; i < Py_SIZE(self); i++) {
- PyObject *selfi = getarrayitem((PyObject *)self, i);
- int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
- Py_DECREF(selfi);
- if (cmp > 0)
- count++;
- else if (cmp < 0)
- return NULL;
- }
- return PyLong_FromSsize_t(count);
+ for (i = 0; i < Py_SIZE(self); i++) {
+ PyObject *selfi = getarrayitem((PyObject *)self, i);
+ int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
+ Py_DECREF(selfi);
+ if (cmp > 0)
+ count++;
+ else if (cmp < 0)
+ return NULL;
+ }
+ return PyLong_FromSsize_t(count);
}
PyDoc_STRVAR(count_doc,
@@ -919,20 +919,20 @@ Return number of occurrences of x in the array.");
static PyObject *
array_index(arrayobject *self, PyObject *v)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i = 0; i < Py_SIZE(self); i++) {
- PyObject *selfi = getarrayitem((PyObject *)self, i);
- int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
- Py_DECREF(selfi);
- if (cmp > 0) {
- return PyLong_FromLong((long)i);
- }
- else if (cmp < 0)
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
- return NULL;
+ for (i = 0; i < Py_SIZE(self); i++) {
+ PyObject *selfi = getarrayitem((PyObject *)self, i);
+ int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
+ Py_DECREF(selfi);
+ if (cmp > 0) {
+ return PyLong_FromLong((long)i);
+ }
+ else if (cmp < 0)
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "array.index(x): x not in list");
+ return NULL;
}
PyDoc_STRVAR(index_doc,
@@ -943,38 +943,38 @@ Return index of first occurrence of x in the array.");
static int
array_contains(arrayobject *self, PyObject *v)
{
- Py_ssize_t i;
- int cmp;
+ Py_ssize_t i;
+ int cmp;
- for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
- PyObject *selfi = getarrayitem((PyObject *)self, i);
- cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
- Py_DECREF(selfi);
- }
- return cmp;
+ for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
+ PyObject *selfi = getarrayitem((PyObject *)self, i);
+ cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
+ Py_DECREF(selfi);
+ }
+ return cmp;
}
static PyObject *
array_remove(arrayobject *self, PyObject *v)
{
- int i;
-
- for (i = 0; i < Py_SIZE(self); i++) {
- PyObject *selfi = getarrayitem((PyObject *)self,i);
- int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
- Py_DECREF(selfi);
- if (cmp > 0) {
- if (array_ass_slice(self, i, i+1,
- (PyObject *)NULL) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
- }
- else if (cmp < 0)
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
- return NULL;
+ int i;
+
+ for (i = 0; i < Py_SIZE(self); i++) {
+ PyObject *selfi = getarrayitem((PyObject *)self,i);
+ int cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
+ Py_DECREF(selfi);
+ if (cmp > 0) {
+ if (array_ass_slice(self, i, i+1,
+ (PyObject *)NULL) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else if (cmp < 0)
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in list");
+ return NULL;
}
PyDoc_STRVAR(remove_doc,
@@ -985,27 +985,27 @@ Remove the first occurrence of x in the array.");
static PyObject *
array_pop(arrayobject *self, PyObject *args)
{
- Py_ssize_t i = -1;
- PyObject *v;
- if (!PyArg_ParseTuple(args, "|n:pop", &i))
- return NULL;
- if (Py_SIZE(self) == 0) {
- /* Special-case most common failure cause */
- PyErr_SetString(PyExc_IndexError, "pop from empty array");
- return NULL;
- }
- if (i < 0)
- i += Py_SIZE(self);
- if (i < 0 || i >= Py_SIZE(self)) {
- PyErr_SetString(PyExc_IndexError, "pop index out of range");
- return NULL;
- }
- v = getarrayitem((PyObject *)self,i);
- if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
- Py_DECREF(v);
- return NULL;
- }
- return v;
+ Py_ssize_t i = -1;
+ PyObject *v;
+ if (!PyArg_ParseTuple(args, "|n:pop", &i))
+ return NULL;
+ if (Py_SIZE(self) == 0) {
+ /* Special-case most common failure cause */
+ PyErr_SetString(PyExc_IndexError, "pop from empty array");
+ return NULL;
+ }
+ if (i < 0)
+ i += Py_SIZE(self);
+ if (i < 0 || i >= Py_SIZE(self)) {
+ PyErr_SetString(PyExc_IndexError, "pop index out of range");
+ return NULL;
+ }
+ v = getarrayitem((PyObject *)self,i);
+ if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ return v;
}
PyDoc_STRVAR(pop_doc,
@@ -1016,10 +1016,10 @@ Return the i-th element and delete it from the array. i defaults to -1.");
static PyObject *
array_extend(arrayobject *self, PyObject *bb)
{
- if (array_do_extend(self, bb) == -1)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (array_do_extend(self, bb) == -1)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(extend_doc,
@@ -1030,11 +1030,11 @@ PyDoc_STRVAR(extend_doc,
static PyObject *
array_insert(arrayobject *self, PyObject *args)
{
- Py_ssize_t i;
- PyObject *v;
- if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
- return NULL;
- return ins(self, i, v);
+ Py_ssize_t i;
+ PyObject *v;
+ if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
+ return NULL;
+ return ins(self, i, v);
}
PyDoc_STRVAR(insert_doc,
@@ -1046,15 +1046,15 @@ Insert a new item x into the array before position i.");
static PyObject *
array_buffer_info(arrayobject *self, PyObject *unused)
{
- PyObject* retval = NULL;
- retval = PyTuple_New(2);
- if (!retval)
- return NULL;
+ PyObject* retval = NULL;
+ retval = PyTuple_New(2);
+ if (!retval)
+ return NULL;
- PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
- PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
+ PyTuple_SET_ITEM(retval, 0, PyLong_FromVoidPtr(self->ob_item));
+ PyTuple_SET_ITEM(retval, 1, PyLong_FromLong((long)(Py_SIZE(self))));
- return retval;
+ return retval;
}
PyDoc_STRVAR(buffer_info_doc,
@@ -1069,7 +1069,7 @@ the buffer length in bytes.");
static PyObject *
array_append(arrayobject *self, PyObject *v)
{
- return ins(self, (int) Py_SIZE(self), v);
+ return ins(self, (int) Py_SIZE(self), v);
}
PyDoc_STRVAR(append_doc,
@@ -1081,52 +1081,52 @@ Append new value x to the end of the array.");
static PyObject *
array_byteswap(arrayobject *self, PyObject *unused)
{
- char *p;
- Py_ssize_t i;
-
- switch (self->ob_descr->itemsize) {
- case 1:
- break;
- case 2:
- for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
- char p0 = p[0];
- p[0] = p[1];
- p[1] = p0;
- }
- break;
- case 4:
- for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
- char p0 = p[0];
- char p1 = p[1];
- p[0] = p[3];
- p[1] = p[2];
- p[2] = p1;
- p[3] = p0;
- }
- break;
- case 8:
- for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
- char p0 = p[0];
- char p1 = p[1];
- char p2 = p[2];
- char p3 = p[3];
- p[0] = p[7];
- p[1] = p[6];
- p[2] = p[5];
- p[3] = p[4];
- p[4] = p3;
- p[5] = p2;
- p[6] = p1;
- p[7] = p0;
- }
- break;
- default:
- PyErr_SetString(PyExc_RuntimeError,
- "don't know how to byteswap this array type");
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ char *p;
+ Py_ssize_t i;
+
+ switch (self->ob_descr->itemsize) {
+ case 1:
+ break;
+ case 2:
+ for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 2) {
+ char p0 = p[0];
+ p[0] = p[1];
+ p[1] = p0;
+ }
+ break;
+ case 4:
+ for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 4) {
+ char p0 = p[0];
+ char p1 = p[1];
+ p[0] = p[3];
+ p[1] = p[2];
+ p[2] = p1;
+ p[3] = p0;
+ }
+ break;
+ case 8:
+ for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
+ char p0 = p[0];
+ char p1 = p[1];
+ char p2 = p[2];
+ char p3 = p[3];
+ p[0] = p[7];
+ p[1] = p[6];
+ p[2] = p[5];
+ p[3] = p[4];
+ p[4] = p3;
+ p[5] = p2;
+ p[6] = p1;
+ p[7] = p0;
+ }
+ break;
+ default:
+ PyErr_SetString(PyExc_RuntimeError,
+ "don't know how to byteswap this array type");
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(byteswap_doc,
@@ -1138,33 +1138,33 @@ Byteswap all items of the array. If the items in the array are not 1, 2,\n\
static PyObject *
array_reduce(arrayobject *array)
{
- PyObject *dict, *result;
-
- dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
- if (dict == NULL) {
- PyErr_Clear();
- dict = Py_None;
- Py_INCREF(dict);
- }
- if (Py_SIZE(array) > 0) {
- if (array->ob_descr->itemsize
- > PY_SSIZE_T_MAX / Py_SIZE(array)) {
- return PyErr_NoMemory();
- }
- result = Py_BuildValue("O(Cy#)O",
- Py_TYPE(array),
- array->ob_descr->typecode,
- array->ob_item,
- Py_SIZE(array) * array->ob_descr->itemsize,
- dict);
- } else {
- result = Py_BuildValue("O(C)O",
- Py_TYPE(array),
- array->ob_descr->typecode,
- dict);
- }
- Py_DECREF(dict);
- return result;
+ PyObject *dict, *result;
+
+ dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
+ if (dict == NULL) {
+ PyErr_Clear();
+ dict = Py_None;
+ Py_INCREF(dict);
+ }
+ if (Py_SIZE(array) > 0) {
+ if (array->ob_descr->itemsize
+ > PY_SSIZE_T_MAX / Py_SIZE(array)) {
+ return PyErr_NoMemory();
+ }
+ result = Py_BuildValue("O(Cy#)O",
+ Py_TYPE(array),
+ array->ob_descr->typecode,
+ array->ob_item,
+ Py_SIZE(array) * array->ob_descr->itemsize,
+ dict);
+ } else {
+ result = Py_BuildValue("O(C)O",
+ Py_TYPE(array),
+ array->ob_descr->typecode,
+ dict);
+ }
+ Py_DECREF(dict);
+ return result;
}
PyDoc_STRVAR(array_doc, "Return state information for pickling.");
@@ -1172,28 +1172,28 @@ PyDoc_STRVAR(array_doc, "Return state information for pickling.");
static PyObject *
array_reverse(arrayobject *self, PyObject *unused)
{
- register Py_ssize_t itemsize = self->ob_descr->itemsize;
- register char *p, *q;
- /* little buffer to hold items while swapping */
- char tmp[256]; /* 8 is probably enough -- but why skimp */
- assert((size_t)itemsize <= sizeof(tmp));
-
- if (Py_SIZE(self) > 1) {
- for (p = self->ob_item,
- q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
- p < q;
- p += itemsize, q -= itemsize) {
- /* memory areas guaranteed disjoint, so memcpy
- * is safe (& memmove may be slower).
- */
- memcpy(tmp, p, itemsize);
- memcpy(p, q, itemsize);
- memcpy(q, tmp, itemsize);
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ register Py_ssize_t itemsize = self->ob_descr->itemsize;
+ register char *p, *q;
+ /* little buffer to hold items while swapping */
+ char tmp[256]; /* 8 is probably enough -- but why skimp */
+ assert((size_t)itemsize <= sizeof(tmp));
+
+ if (Py_SIZE(self) > 1) {
+ for (p = self->ob_item,
+ q = self->ob_item + (Py_SIZE(self) - 1)*itemsize;
+ p < q;
+ p += itemsize, q -= itemsize) {
+ /* memory areas guaranteed disjoint, so memcpy
+ * is safe (& memmove may be slower).
+ */
+ memcpy(tmp, p, itemsize);
+ memcpy(p, q, itemsize);
+ memcpy(q, tmp, itemsize);
+ }
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(reverse_doc,
@@ -1208,51 +1208,51 @@ static PyObject *array_fromstring(arrayobject *self, PyObject *args);
static PyObject *
array_fromfile(arrayobject *self, PyObject *args)
{
- PyObject *f, *b, *res;
- Py_ssize_t itemsize = self->ob_descr->itemsize;
- Py_ssize_t n, nbytes;
- int not_enough_bytes;
+ PyObject *f, *b, *res;
+ Py_ssize_t itemsize = self->ob_descr->itemsize;
+ Py_ssize_t n, nbytes;
+ int not_enough_bytes;
- if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
- return NULL;
+ if (!PyArg_ParseTuple(args, "On:fromfile", &f, &n))
+ return NULL;
- nbytes = n * itemsize;
- if (nbytes < 0 || nbytes/itemsize != n) {
- PyErr_NoMemory();
- return NULL;
- }
+ nbytes = n * itemsize;
+ if (nbytes < 0 || nbytes/itemsize != n) {
+ PyErr_NoMemory();
+ return NULL;
+ }
- b = PyObject_CallMethod(f, "read", "n", nbytes);
- if (b == NULL)
- return NULL;
+ b = PyObject_CallMethod(f, "read", "n", nbytes);
+ if (b == NULL)
+ return NULL;
- if (!PyBytes_Check(b)) {
- PyErr_SetString(PyExc_TypeError,
- "read() didn't return bytes");
- Py_DECREF(b);
- return NULL;
- }
+ if (!PyBytes_Check(b)) {
+ PyErr_SetString(PyExc_TypeError,
+ "read() didn't return bytes");
+ Py_DECREF(b);
+ return NULL;
+ }
- not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
+ not_enough_bytes = (PyBytes_GET_SIZE(b) != nbytes);
- args = Py_BuildValue("(O)", b);
- Py_DECREF(b);
- if (args == NULL)
- return NULL;
+ args = Py_BuildValue("(O)", b);
+ Py_DECREF(b);
+ if (args == NULL)
+ return NULL;
- res = array_fromstring(self, args);
- Py_DECREF(args);
- if (res == NULL)
- return NULL;
+ res = array_fromstring(self, args);
+ Py_DECREF(args);
+ if (res == NULL)
+ return NULL;
- if (not_enough_bytes) {
- PyErr_SetString(PyExc_EOFError,
- "read() didn't return enough bytes");
- Py_DECREF(res);
- return NULL;
- }
+ if (not_enough_bytes) {
+ PyErr_SetString(PyExc_EOFError,
+ "read() didn't return enough bytes");
+ Py_DECREF(res);
+ return NULL;
+ }
- return res;
+ return res;
}
PyDoc_STRVAR(fromfile_doc,
@@ -1265,35 +1265,35 @@ array.");
static PyObject *
array_tofile(arrayobject *self, PyObject *f)
{
- Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
- /* Write 64K blocks at a time */
- /* XXX Make the block size settable */
- int BLOCKSIZE = 64*1024;
- Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
- Py_ssize_t i;
-
- if (Py_SIZE(self) == 0)
- goto done;
-
- for (i = 0; i < nblocks; i++) {
- char* ptr = self->ob_item + i*BLOCKSIZE;
- Py_ssize_t size = BLOCKSIZE;
- PyObject *bytes, *res;
- if (i*BLOCKSIZE + size > nbytes)
- size = nbytes - i*BLOCKSIZE;
- bytes = PyBytes_FromStringAndSize(ptr, size);
- if (bytes == NULL)
- return NULL;
- res = PyObject_CallMethod(f, "write", "O", bytes);
- Py_DECREF(bytes);
- if (res == NULL)
- return NULL;
- Py_DECREF(res); /* drop write result */
- }
+ Py_ssize_t nbytes = Py_SIZE(self) * self->ob_descr->itemsize;
+ /* Write 64K blocks at a time */
+ /* XXX Make the block size settable */
+ int BLOCKSIZE = 64*1024;
+ Py_ssize_t nblocks = (nbytes + BLOCKSIZE - 1) / BLOCKSIZE;
+ Py_ssize_t i;
+
+ if (Py_SIZE(self) == 0)
+ goto done;
+
+ for (i = 0; i < nblocks; i++) {
+ char* ptr = self->ob_item + i*BLOCKSIZE;
+ Py_ssize_t size = BLOCKSIZE;
+ PyObject *bytes, *res;
+ if (i*BLOCKSIZE + size > nbytes)
+ size = nbytes - i*BLOCKSIZE;
+ bytes = PyBytes_FromStringAndSize(ptr, size);
+ if (bytes == NULL)
+ return NULL;
+ res = PyObject_CallMethod(f, "write", "O", bytes);
+ Py_DECREF(bytes);
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res); /* drop write result */
+ }
done:
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(tofile_doc,
@@ -1305,29 +1305,29 @@ Write all items (as machine values) to the file object f.");
static PyObject *
array_fromlist(arrayobject *self, PyObject *list)
{
- Py_ssize_t n;
-
- if (!PyList_Check(list)) {
- PyErr_SetString(PyExc_TypeError, "arg must be list");
- return NULL;
- }
- n = PyList_Size(list);
- if (n > 0) {
- Py_ssize_t i, old_size;
- old_size = Py_SIZE(self);
- if (array_resize(self, old_size + n) == -1)
- return NULL;
- for (i = 0; i < n; i++) {
- PyObject *v = PyList_GetItem(list, i);
- if ((*self->ob_descr->setitem)(self,
- Py_SIZE(self) - n + i, v) != 0) {
- array_resize(self, old_size);
- return NULL;
- }
- }
- }
- Py_INCREF(Py_None);
- return Py_None;
+ Py_ssize_t n;
+
+ if (!PyList_Check(list)) {
+ PyErr_SetString(PyExc_TypeError, "arg must be list");
+ return NULL;
+ }
+ n = PyList_Size(list);
+ if (n > 0) {
+ Py_ssize_t i, old_size;
+ old_size = Py_SIZE(self);
+ if (array_resize(self, old_size + n) == -1)
+ return NULL;
+ for (i = 0; i < n; i++) {
+ PyObject *v = PyList_GetItem(list, i);
+ if ((*self->ob_descr->setitem)(self,
+ Py_SIZE(self) - n + i, v) != 0) {
+ array_resize(self, old_size);
+ return NULL;
+ }
+ }
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(fromlist_doc,
@@ -1338,20 +1338,20 @@ Append items to array from list.");
static PyObject *
array_tolist(arrayobject *self, PyObject *unused)
{
- PyObject *list = PyList_New(Py_SIZE(self));
- Py_ssize_t i;
+ PyObject *list = PyList_New(Py_SIZE(self));
+ Py_ssize_t i;
- if (list == NULL)
- return NULL;
- for (i = 0; i < Py_SIZE(self); i++) {
- PyObject *v = getarrayitem((PyObject *)self, i);
- if (v == NULL) {
- Py_DECREF(list);
- return NULL;
- }
- PyList_SetItem(list, i, v);
- }
- return list;
+ if (list == NULL)
+ return NULL;
+ for (i = 0; i < Py_SIZE(self); i++) {
+ PyObject *v = getarrayitem((PyObject *)self, i);
+ if (v == NULL) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ PyList_SetItem(list, i, v);
+ }
+ return list;
}
PyDoc_STRVAR(tolist_doc,
@@ -1363,30 +1363,30 @@ Convert array to an ordinary list with the same items.");
static PyObject *
array_fromstring(arrayobject *self, PyObject *args)
{
- char *str;
- Py_ssize_t n;
- int itemsize = self->ob_descr->itemsize;
- if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
- return NULL;
- if (n % itemsize != 0) {
- PyErr_SetString(PyExc_ValueError,
- "string length not a multiple of item size");
- return NULL;
- }
- n = n / itemsize;
- if (n > 0) {
- Py_ssize_t old_size = Py_SIZE(self);
- if ((n > PY_SSIZE_T_MAX - old_size) ||
- ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
- return PyErr_NoMemory();
- }
- if (array_resize(self, old_size + n) == -1)
- return NULL;
- memcpy(self->ob_item + old_size * itemsize,
- str, n * itemsize);
- }
- Py_INCREF(Py_None);
- return Py_None;
+ char *str;
+ Py_ssize_t n;
+ int itemsize = self->ob_descr->itemsize;
+ if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
+ return NULL;
+ if (n % itemsize != 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "string length not a multiple of item size");
+ return NULL;
+ }
+ n = n / itemsize;
+ if (n > 0) {
+ Py_ssize_t old_size = Py_SIZE(self);
+ if ((n > PY_SSIZE_T_MAX - old_size) ||
+ ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) {
+ return PyErr_NoMemory();
+ }
+ if (array_resize(self, old_size + n) == -1)
+ return NULL;
+ memcpy(self->ob_item + old_size * itemsize,
+ str, n * itemsize);
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(fromstring_doc,
@@ -1399,12 +1399,12 @@ values, as if it had been read from a file using the fromfile() method).");
static PyObject *
array_tostring(arrayobject *self, PyObject *unused)
{
- if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
- return PyBytes_FromStringAndSize(self->ob_item,
- Py_SIZE(self) * self->ob_descr->itemsize);
- } else {
- return PyErr_NoMemory();
- }
+ if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) {
+ return PyBytes_FromStringAndSize(self->ob_item,
+ Py_SIZE(self) * self->ob_descr->itemsize);
+ } else {
+ return PyErr_NoMemory();
+ }
}
PyDoc_STRVAR(tostring_doc,
@@ -1418,29 +1418,29 @@ representation.");
static PyObject *
array_fromunicode(arrayobject *self, PyObject *args)
{
- Py_UNICODE *ustr;
- Py_ssize_t n;
- char typecode;
-
- if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
- return NULL;
- typecode = self->ob_descr->typecode;
- if ((typecode != 'u')) {
- PyErr_SetString(PyExc_ValueError,
- "fromunicode() may only be called on "
- "unicode type arrays");
- return NULL;
- }
- if (n > 0) {
- Py_ssize_t old_size = Py_SIZE(self);
- if (array_resize(self, old_size + n) == -1)
- return NULL;
- memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
- ustr, n * sizeof(Py_UNICODE));
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ Py_UNICODE *ustr;
+ Py_ssize_t n;
+ char typecode;
+
+ if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
+ return NULL;
+ typecode = self->ob_descr->typecode;
+ if ((typecode != 'u')) {
+ PyErr_SetString(PyExc_ValueError,
+ "fromunicode() may only be called on "
+ "unicode type arrays");
+ return NULL;
+ }
+ if (n > 0) {
+ Py_ssize_t old_size = Py_SIZE(self);
+ if (array_resize(self, old_size + n) == -1)
+ return NULL;
+ memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
+ ustr, n * sizeof(Py_UNICODE));
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(fromunicode_doc,
@@ -1455,14 +1455,14 @@ append Unicode data to an array of some other type.");
static PyObject *
array_tounicode(arrayobject *self, PyObject *unused)
{
- char typecode;
- typecode = self->ob_descr->typecode;
- if ((typecode != 'u')) {
- PyErr_SetString(PyExc_ValueError,
- "tounicode() may only be called on unicode type arrays");
- return NULL;
- }
- return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
+ char typecode;
+ typecode = self->ob_descr->typecode;
+ if ((typecode != 'u')) {
+ PyErr_SetString(PyExc_ValueError,
+ "tounicode() may only be called on unicode type arrays");
+ return NULL;
+ }
+ return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
}
PyDoc_STRVAR(tounicode_doc,
@@ -1478,313 +1478,313 @@ an array of some other type.");
static PyObject *
array_get_typecode(arrayobject *a, void *closure)
{
- char tc = a->ob_descr->typecode;
- return PyUnicode_FromStringAndSize(&tc, 1);
+ char tc = a->ob_descr->typecode;
+ return PyUnicode_FromStringAndSize(&tc, 1);
}
static PyObject *
array_get_itemsize(arrayobject *a, void *closure)
{
- return PyLong_FromLong((long)a->ob_descr->itemsize);
+ return PyLong_FromLong((long)a->ob_descr->itemsize);
}
static PyGetSetDef array_getsets [] = {
- {"typecode", (getter) array_get_typecode, NULL,
- "the typecode character used to create the array"},
- {"itemsize", (getter) array_get_itemsize, NULL,
- "the size, in bytes, of one array item"},
- {NULL}
+ {"typecode", (getter) array_get_typecode, NULL,
+ "the typecode character used to create the array"},
+ {"itemsize", (getter) array_get_itemsize, NULL,
+ "the size, in bytes, of one array item"},
+ {NULL}
};
static PyMethodDef array_methods[] = {
- {"append", (PyCFunction)array_append, METH_O,
- append_doc},
- {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
- buffer_info_doc},
- {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
- byteswap_doc},
- {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
- copy_doc},
- {"count", (PyCFunction)array_count, METH_O,
- count_doc},
- {"__deepcopy__",(PyCFunction)array_copy, METH_O,
- copy_doc},
- {"extend", (PyCFunction)array_extend, METH_O,
- extend_doc},
- {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
- fromfile_doc},
- {"fromlist", (PyCFunction)array_fromlist, METH_O,
- fromlist_doc},
- {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
- fromstring_doc},
- {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
- fromunicode_doc},
- {"index", (PyCFunction)array_index, METH_O,
- index_doc},
- {"insert", (PyCFunction)array_insert, METH_VARARGS,
- insert_doc},
- {"pop", (PyCFunction)array_pop, METH_VARARGS,
- pop_doc},
- {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
- array_doc},
- {"remove", (PyCFunction)array_remove, METH_O,
- remove_doc},
- {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
- reverse_doc},
-/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
- sort_doc},*/
- {"tofile", (PyCFunction)array_tofile, METH_O,
- tofile_doc},
- {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
- tolist_doc},
- {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
- tostring_doc},
- {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
- tounicode_doc},
- {NULL, NULL} /* sentinel */
+ {"append", (PyCFunction)array_append, METH_O,
+ append_doc},
+ {"buffer_info", (PyCFunction)array_buffer_info, METH_NOARGS,
+ buffer_info_doc},
+ {"byteswap", (PyCFunction)array_byteswap, METH_NOARGS,
+ byteswap_doc},
+ {"__copy__", (PyCFunction)array_copy, METH_NOARGS,
+ copy_doc},
+ {"count", (PyCFunction)array_count, METH_O,
+ count_doc},
+ {"__deepcopy__",(PyCFunction)array_copy, METH_O,
+ copy_doc},
+ {"extend", (PyCFunction)array_extend, METH_O,
+ extend_doc},
+ {"fromfile", (PyCFunction)array_fromfile, METH_VARARGS,
+ fromfile_doc},
+ {"fromlist", (PyCFunction)array_fromlist, METH_O,
+ fromlist_doc},
+ {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS,
+ fromstring_doc},
+ {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS,
+ fromunicode_doc},
+ {"index", (PyCFunction)array_index, METH_O,
+ index_doc},
+ {"insert", (PyCFunction)array_insert, METH_VARARGS,
+ insert_doc},
+ {"pop", (PyCFunction)array_pop, METH_VARARGS,
+ pop_doc},
+ {"__reduce__", (PyCFunction)array_reduce, METH_NOARGS,
+ array_doc},
+ {"remove", (PyCFunction)array_remove, METH_O,
+ remove_doc},
+ {"reverse", (PyCFunction)array_reverse, METH_NOARGS,
+ reverse_doc},
+/* {"sort", (PyCFunction)array_sort, METH_VARARGS,
+ sort_doc},*/
+ {"tofile", (PyCFunction)array_tofile, METH_O,
+ tofile_doc},
+ {"tolist", (PyCFunction)array_tolist, METH_NOARGS,
+ tolist_doc},
+ {"tostring", (PyCFunction)array_tostring, METH_NOARGS,
+ tostring_doc},
+ {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
+ tounicode_doc},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
array_repr(arrayobject *a)
{
- char typecode;
- PyObject *s, *v = NULL;
- Py_ssize_t len;
+ char typecode;
+ PyObject *s, *v = NULL;
+ Py_ssize_t len;
- len = Py_SIZE(a);
- typecode = a->ob_descr->typecode;
- if (len == 0) {
- return PyUnicode_FromFormat("array('%c')", typecode);
- }
- if ((typecode == 'u'))
- v = array_tounicode(a, NULL);
- else
- v = array_tolist(a, NULL);
+ len = Py_SIZE(a);
+ typecode = a->ob_descr->typecode;
+ if (len == 0) {
+ return PyUnicode_FromFormat("array('%c')", typecode);
+ }
+ if ((typecode == 'u'))
+ v = array_tounicode(a, NULL);
+ else
+ v = array_tolist(a, NULL);
- s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
- Py_DECREF(v);
- return s;
+ s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
+ Py_DECREF(v);
+ return s;
}
static PyObject*
array_subscr(arrayobject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i==-1 && PyErr_Occurred()) {
- return NULL;
- }
- if (i < 0)
- i += Py_SIZE(self);
- return array_item(self, i);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelength, cur, i;
- PyObject* result;
- arrayobject* ar;
- int itemsize = self->ob_descr->itemsize;
-
- if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
- &start, &stop, &step, &slicelength) < 0) {
- return NULL;
- }
-
- if (slicelength <= 0) {
- return newarrayobject(&Arraytype, 0, self->ob_descr);
- }
- else if (step == 1) {
- PyObject *result = newarrayobject(&Arraytype,
- slicelength, self->ob_descr);
- if (result == NULL)
- return NULL;
- memcpy(((arrayobject *)result)->ob_item,
- self->ob_item + start * itemsize,
- slicelength * itemsize);
- return result;
- }
- else {
- result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
- if (!result) return NULL;
-
- ar = (arrayobject*)result;
-
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- memcpy(ar->ob_item + i*itemsize,
- self->ob_item + cur*itemsize,
- itemsize);
- }
-
- return result;
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "array indices must be integers");
- return NULL;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i==-1 && PyErr_Occurred()) {
+ return NULL;
+ }
+ if (i < 0)
+ i += Py_SIZE(self);
+ return array_item(self, i);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength, cur, i;
+ PyObject* result;
+ arrayobject* ar;
+ int itemsize = self->ob_descr->itemsize;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
+ &start, &stop, &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ return newarrayobject(&Arraytype, 0, self->ob_descr);
+ }
+ else if (step == 1) {
+ PyObject *result = newarrayobject(&Arraytype,
+ slicelength, self->ob_descr);
+ if (result == NULL)
+ return NULL;
+ memcpy(((arrayobject *)result)->ob_item,
+ self->ob_item + start * itemsize,
+ slicelength * itemsize);
+ return result;
+ }
+ else {
+ result = newarrayobject(&Arraytype, slicelength, self->ob_descr);
+ if (!result) return NULL;
+
+ ar = (arrayobject*)result;
+
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ memcpy(ar->ob_item + i*itemsize,
+ self->ob_item + cur*itemsize,
+ itemsize);
+ }
+
+ return result;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "array indices must be integers");
+ return NULL;
+ }
}
static int
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
{
- Py_ssize_t start, stop, step, slicelength, needed;
- arrayobject* other;
- int itemsize;
-
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
-
- if (i == -1 && PyErr_Occurred())
- return -1;
- if (i < 0)
- i += Py_SIZE(self);
- if (i < 0 || i >= Py_SIZE(self)) {
- PyErr_SetString(PyExc_IndexError,
- "array assignment index out of range");
- return -1;
- }
- if (value == NULL) {
- /* Fall through to slice assignment */
- start = i;
- stop = i + 1;
- step = 1;
- slicelength = 1;
- }
- else
- return (*self->ob_descr->setitem)(self, i, value);
- }
- else if (PySlice_Check(item)) {
- if (PySlice_GetIndicesEx((PySliceObject *)item,
- Py_SIZE(self), &start, &stop,
- &step, &slicelength) < 0) {
- return -1;
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "array indices must be integer");
- return -1;
- }
- if (value == NULL) {
- other = NULL;
- needed = 0;
- }
- else if (array_Check(value)) {
- other = (arrayobject *)value;
- needed = Py_SIZE(other);
- if (self == other) {
- /* Special case "self[i:j] = self" -- copy self first */
- int ret;
- value = array_slice(other, 0, needed);
- if (value == NULL)
- return -1;
- ret = array_ass_subscr(self, item, value);
- Py_DECREF(value);
- return ret;
- }
- if (other->ob_descr != self->ob_descr) {
- PyErr_BadArgument();
- return -1;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "can only assign array (not \"%.200s\") to array slice",
- Py_TYPE(value)->tp_name);
- return -1;
- }
- itemsize = self->ob_descr->itemsize;
- /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
- if ((step > 0 && stop < start) ||
- (step < 0 && stop > start))
- stop = start;
-
- /* Issue #4509: If the array has exported buffers and the slice
- assignment would change the size of the array, fail early to make
- sure we don't modify it. */
- if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
- PyErr_SetString(PyExc_BufferError,
- "cannot resize an array that is exporting buffers");
- return -1;
- }
-
- if (step == 1) {
- if (slicelength > needed) {
- memmove(self->ob_item + (start + needed) * itemsize,
- self->ob_item + stop * itemsize,
- (Py_SIZE(self) - stop) * itemsize);
- if (array_resize(self, Py_SIZE(self) +
- needed - slicelength) < 0)
- return -1;
- }
- else if (slicelength < needed) {
- if (array_resize(self, Py_SIZE(self) +
- needed - slicelength) < 0)
- return -1;
- memmove(self->ob_item + (start + needed) * itemsize,
- self->ob_item + stop * itemsize,
- (Py_SIZE(self) - start - needed) * itemsize);
- }
- if (needed > 0)
- memcpy(self->ob_item + start * itemsize,
- other->ob_item, needed * itemsize);
- return 0;
- }
- else if (needed == 0) {
- /* Delete slice */
- size_t cur;
- Py_ssize_t i;
-
- if (step < 0) {
- stop = start + 1;
- start = stop + step * (slicelength - 1) - 1;
- step = -step;
- }
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- Py_ssize_t lim = step - 1;
-
- if (cur + step >= (size_t)Py_SIZE(self))
- lim = Py_SIZE(self) - cur - 1;
- memmove(self->ob_item + (cur - i) * itemsize,
- self->ob_item + (cur + 1) * itemsize,
- lim * itemsize);
- }
- cur = start + slicelength * step;
- if (cur < (size_t)Py_SIZE(self)) {
- memmove(self->ob_item + (cur-slicelength) * itemsize,
- self->ob_item + cur * itemsize,
- (Py_SIZE(self) - cur) * itemsize);
- }
- if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
- return -1;
- return 0;
- }
- else {
- Py_ssize_t cur, i;
-
- if (needed != slicelength) {
- PyErr_Format(PyExc_ValueError,
- "attempt to assign array of size %zd "
- "to extended slice of size %zd",
- needed, slicelength);
- return -1;
- }
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- memcpy(self->ob_item + cur * itemsize,
- other->ob_item + i * itemsize,
- itemsize);
- }
- return 0;
- }
+ Py_ssize_t start, stop, step, slicelength, needed;
+ arrayobject* other;
+ int itemsize;
+
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0)
+ i += Py_SIZE(self);
+ if (i < 0 || i >= Py_SIZE(self)) {
+ PyErr_SetString(PyExc_IndexError,
+ "array assignment index out of range");
+ return -1;
+ }
+ if (value == NULL) {
+ /* Fall through to slice assignment */
+ start = i;
+ stop = i + 1;
+ step = 1;
+ slicelength = 1;
+ }
+ else
+ return (*self->ob_descr->setitem)(self, i, value);
+ }
+ else if (PySlice_Check(item)) {
+ if (PySlice_GetIndicesEx((PySliceObject *)item,
+ Py_SIZE(self), &start, &stop,
+ &step, &slicelength) < 0) {
+ return -1;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "array indices must be integer");
+ return -1;
+ }
+ if (value == NULL) {
+ other = NULL;
+ needed = 0;
+ }
+ else if (array_Check(value)) {
+ other = (arrayobject *)value;
+ needed = Py_SIZE(other);
+ if (self == other) {
+ /* Special case "self[i:j] = self" -- copy self first */
+ int ret;
+ value = array_slice(other, 0, needed);
+ if (value == NULL)
+ return -1;
+ ret = array_ass_subscr(self, item, value);
+ Py_DECREF(value);
+ return ret;
+ }
+ if (other->ob_descr != self->ob_descr) {
+ PyErr_BadArgument();
+ return -1;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "can only assign array (not \"%.200s\") to array slice",
+ Py_TYPE(value)->tp_name);
+ return -1;
+ }
+ itemsize = self->ob_descr->itemsize;
+ /* for 'a[2:1] = ...', the insertion point is 'start', not 'stop' */
+ if ((step > 0 && stop < start) ||
+ (step < 0 && stop > start))
+ stop = start;
+
+ /* Issue #4509: If the array has exported buffers and the slice
+ assignment would change the size of the array, fail early to make
+ sure we don't modify it. */
+ if ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "cannot resize an array that is exporting buffers");
+ return -1;
+ }
+
+ if (step == 1) {
+ if (slicelength > needed) {
+ memmove(self->ob_item + (start + needed) * itemsize,
+ self->ob_item + stop * itemsize,
+ (Py_SIZE(self) - stop) * itemsize);
+ if (array_resize(self, Py_SIZE(self) +
+ needed - slicelength) < 0)
+ return -1;
+ }
+ else if (slicelength < needed) {
+ if (array_resize(self, Py_SIZE(self) +
+ needed - slicelength) < 0)
+ return -1;
+ memmove(self->ob_item + (start + needed) * itemsize,
+ self->ob_item + stop * itemsize,
+ (Py_SIZE(self) - start - needed) * itemsize);
+ }
+ if (needed > 0)
+ memcpy(self->ob_item + start * itemsize,
+ other->ob_item, needed * itemsize);
+ return 0;
+ }
+ else if (needed == 0) {
+ /* Delete slice */
+ size_t cur;
+ Py_ssize_t i;
+
+ if (step < 0) {
+ stop = start + 1;
+ start = stop + step * (slicelength - 1) - 1;
+ step = -step;
+ }
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ Py_ssize_t lim = step - 1;
+
+ if (cur + step >= (size_t)Py_SIZE(self))
+ lim = Py_SIZE(self) - cur - 1;
+ memmove(self->ob_item + (cur - i) * itemsize,
+ self->ob_item + (cur + 1) * itemsize,
+ lim * itemsize);
+ }
+ cur = start + slicelength * step;
+ if (cur < (size_t)Py_SIZE(self)) {
+ memmove(self->ob_item + (cur-slicelength) * itemsize,
+ self->ob_item + cur * itemsize,
+ (Py_SIZE(self) - cur) * itemsize);
+ }
+ if (array_resize(self, Py_SIZE(self) - slicelength) < 0)
+ return -1;
+ return 0;
+ }
+ else {
+ Py_ssize_t cur, i;
+
+ if (needed != slicelength) {
+ PyErr_Format(PyExc_ValueError,
+ "attempt to assign array of size %zd "
+ "to extended slice of size %zd",
+ needed, slicelength);
+ return -1;
+ }
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ memcpy(self->ob_item + cur * itemsize,
+ other->ob_item + i * itemsize,
+ itemsize);
+ }
+ return 0;
+ }
}
static PyMappingMethods array_as_mapping = {
- (lenfunc)array_length,
- (binaryfunc)array_subscr,
- (objobjargproc)array_ass_subscr
+ (lenfunc)array_length,
+ (binaryfunc)array_subscr,
+ (objobjargproc)array_ass_subscr
};
static const void *emptybuf = "";
@@ -1793,173 +1793,173 @@ static const void *emptybuf = "";
static int
array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
{
- if (view==NULL) goto finish;
-
- view->buf = (void *)self->ob_item;
- view->obj = (PyObject*)self;
- Py_INCREF(self);
- if (view->buf == NULL)
- view->buf = (void *)emptybuf;
- view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
- view->readonly = 0;
- view->ndim = 1;
- view->itemsize = self->ob_descr->itemsize;
- view->suboffsets = NULL;
- view->shape = NULL;
- if ((flags & PyBUF_ND)==PyBUF_ND) {
- view->shape = &((Py_SIZE(self)));
- }
- view->strides = NULL;
- if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
- view->strides = &(view->itemsize);
- view->format = NULL;
- view->internal = NULL;
- if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
- view->format = self->ob_descr->formats;
+ if (view==NULL) goto finish;
+
+ view->buf = (void *)self->ob_item;
+ view->obj = (PyObject*)self;
+ Py_INCREF(self);
+ if (view->buf == NULL)
+ view->buf = (void *)emptybuf;
+ view->len = (Py_SIZE(self)) * self->ob_descr->itemsize;
+ view->readonly = 0;
+ view->ndim = 1;
+ view->itemsize = self->ob_descr->itemsize;
+ view->suboffsets = NULL;
+ view->shape = NULL;
+ if ((flags & PyBUF_ND)==PyBUF_ND) {
+ view->shape = &((Py_SIZE(self)));
+ }
+ view->strides = NULL;
+ if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
+ view->strides = &(view->itemsize);
+ view->format = NULL;
+ view->internal = NULL;
+ if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
+ view->format = self->ob_descr->formats;
#ifdef Py_UNICODE_WIDE
- if (self->ob_descr->typecode == 'u') {
- view->format = "w";
- }
-#endif
+ if (self->ob_descr->typecode == 'u') {
+ view->format = "w";
}
+#endif
+ }
finish:
- self->ob_exports++;
- return 0;
+ self->ob_exports++;
+ return 0;
}
static void
array_buffer_relbuf(arrayobject *self, Py_buffer *view)
{
- self->ob_exports--;
+ self->ob_exports--;
}
static PySequenceMethods array_as_sequence = {
- (lenfunc)array_length, /*sq_length*/
- (binaryfunc)array_concat, /*sq_concat*/
- (ssizeargfunc)array_repeat, /*sq_repeat*/
- (ssizeargfunc)array_item, /*sq_item*/
- 0, /*sq_slice*/
- (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
- 0, /*sq_ass_slice*/
- (objobjproc)array_contains, /*sq_contains*/
- (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
- (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
+ (lenfunc)array_length, /*sq_length*/
+ (binaryfunc)array_concat, /*sq_concat*/
+ (ssizeargfunc)array_repeat, /*sq_repeat*/
+ (ssizeargfunc)array_item, /*sq_item*/
+ 0, /*sq_slice*/
+ (ssizeobjargproc)array_ass_item, /*sq_ass_item*/
+ 0, /*sq_ass_slice*/
+ (objobjproc)array_contains, /*sq_contains*/
+ (binaryfunc)array_inplace_concat, /*sq_inplace_concat*/
+ (ssizeargfunc)array_inplace_repeat /*sq_inplace_repeat*/
};
static PyBufferProcs array_as_buffer = {
- (getbufferproc)array_buffer_getbuf,
- (releasebufferproc)array_buffer_relbuf
+ (getbufferproc)array_buffer_getbuf,
+ (releasebufferproc)array_buffer_relbuf
};
static PyObject *
array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- int c;
- PyObject *initial = NULL, *it = NULL;
- struct arraydescr *descr;
-
- if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
- return NULL;
-
- if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
- return NULL;
-
- if (!(initial == NULL || PyList_Check(initial)
- || PyByteArray_Check(initial)
- || PyBytes_Check(initial)
- || PyTuple_Check(initial)
- || ((c=='u') && PyUnicode_Check(initial)))) {
- it = PyObject_GetIter(initial);
- if (it == NULL)
- return NULL;
- /* We set initial to NULL so that the subsequent code
- will create an empty array of the appropriate type
- and afterwards we can use array_iter_extend to populate
- the array.
- */
- initial = NULL;
- }
- for (descr = descriptors; descr->typecode != '\0'; descr++) {
- if (descr->typecode == c) {
- PyObject *a;
- Py_ssize_t len;
-
- if (initial == NULL || !(PyList_Check(initial)
- || PyTuple_Check(initial)))
- len = 0;
- else
- len = PySequence_Size(initial);
-
- a = newarrayobject(type, len, descr);
- if (a == NULL)
- return NULL;
-
- if (len > 0) {
- Py_ssize_t i;
- for (i = 0; i < len; i++) {
- PyObject *v =
- PySequence_GetItem(initial, i);
- if (v == NULL) {
- Py_DECREF(a);
- return NULL;
- }
- if (setarrayitem(a, i, v) != 0) {
- Py_DECREF(v);
- Py_DECREF(a);
- return NULL;
- }
- Py_DECREF(v);
- }
- }
- else if (initial != NULL && (PyByteArray_Check(initial) ||
- PyBytes_Check(initial))) {
- PyObject *t_initial, *v;
- t_initial = PyTuple_Pack(1, initial);
- if (t_initial == NULL) {
- Py_DECREF(a);
- return NULL;
- }
- v = array_fromstring((arrayobject *)a,
- t_initial);
- Py_DECREF(t_initial);
- if (v == NULL) {
- Py_DECREF(a);
- return NULL;
- }
- Py_DECREF(v);
- }
- else if (initial != NULL && PyUnicode_Check(initial)) {
- Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
- if (n > 0) {
- arrayobject *self = (arrayobject *)a;
- char *item = self->ob_item;
- item = (char *)PyMem_Realloc(item, n);
- if (item == NULL) {
- PyErr_NoMemory();
- Py_DECREF(a);
- return NULL;
- }
- self->ob_item = item;
- Py_SIZE(self) = n / sizeof(Py_UNICODE);
- memcpy(item, PyUnicode_AS_DATA(initial), n);
- self->allocated = Py_SIZE(self);
- }
- }
- if (it != NULL) {
- if (array_iter_extend((arrayobject *)a, it) == -1) {
- Py_DECREF(it);
- Py_DECREF(a);
- return NULL;
- }
- Py_DECREF(it);
- }
- return a;
- }
- }
- PyErr_SetString(PyExc_ValueError,
- "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
- return NULL;
+ int c;
+ PyObject *initial = NULL, *it = NULL;
+ struct arraydescr *descr;
+
+ if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
+ return NULL;
+
+ if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
+ return NULL;
+
+ if (!(initial == NULL || PyList_Check(initial)
+ || PyByteArray_Check(initial)
+ || PyBytes_Check(initial)
+ || PyTuple_Check(initial)
+ || ((c=='u') && PyUnicode_Check(initial)))) {
+ it = PyObject_GetIter(initial);
+ if (it == NULL)
+ return NULL;
+ /* We set initial to NULL so that the subsequent code
+ will create an empty array of the appropriate type
+ and afterwards we can use array_iter_extend to populate
+ the array.
+ */
+ initial = NULL;
+ }
+ for (descr = descriptors; descr->typecode != '\0'; descr++) {
+ if (descr->typecode == c) {
+ PyObject *a;
+ Py_ssize_t len;
+
+ if (initial == NULL || !(PyList_Check(initial)
+ || PyTuple_Check(initial)))
+ len = 0;
+ else
+ len = PySequence_Size(initial);
+
+ a = newarrayobject(type, len, descr);
+ if (a == NULL)
+ return NULL;
+
+ if (len > 0) {
+ Py_ssize_t i;
+ for (i = 0; i < len; i++) {
+ PyObject *v =
+ PySequence_GetItem(initial, i);
+ if (v == NULL) {
+ Py_DECREF(a);
+ return NULL;
+ }
+ if (setarrayitem(a, i, v) != 0) {
+ Py_DECREF(v);
+ Py_DECREF(a);
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ }
+ else if (initial != NULL && (PyByteArray_Check(initial) ||
+ PyBytes_Check(initial))) {
+ PyObject *t_initial, *v;
+ t_initial = PyTuple_Pack(1, initial);
+ if (t_initial == NULL) {
+ Py_DECREF(a);
+ return NULL;
+ }
+ v = array_fromstring((arrayobject *)a,
+ t_initial);
+ Py_DECREF(t_initial);
+ if (v == NULL) {
+ Py_DECREF(a);
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ else if (initial != NULL && PyUnicode_Check(initial)) {
+ Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial);
+ if (n > 0) {
+ arrayobject *self = (arrayobject *)a;
+ char *item = self->ob_item;
+ item = (char *)PyMem_Realloc(item, n);
+ if (item == NULL) {
+ PyErr_NoMemory();
+ Py_DECREF(a);
+ return NULL;
+ }
+ self->ob_item = item;
+ Py_SIZE(self) = n / sizeof(Py_UNICODE);
+ memcpy(item, PyUnicode_AS_DATA(initial), n);
+ self->allocated = Py_SIZE(self);
+ }
+ }
+ if (it != NULL) {
+ if (array_iter_extend((arrayobject *)a, it) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(a);
+ return NULL;
+ }
+ Py_DECREF(it);
+ }
+ return a;
+ }
+ }
+ PyErr_SetString(PyExc_ValueError,
+ "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
+ return NULL;
}
@@ -2030,55 +2030,55 @@ itemsize -- the length in bytes of one array item\n\
static PyObject *array_iter(arrayobject *ao);
static PyTypeObject Arraytype = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "array.array",
- sizeof(arrayobject),
- 0,
- (destructor)array_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)array_repr, /* tp_repr */
- 0, /* tp_as_number*/
- &array_as_sequence, /* tp_as_sequence*/
- &array_as_mapping, /* tp_as_mapping*/
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- &array_as_buffer, /* tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- arraytype_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- array_richcompare, /* tp_richcompare */
- offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
- (getiterfunc)array_iter, /* tp_iter */
- 0, /* tp_iternext */
- array_methods, /* tp_methods */
- 0, /* tp_members */
- array_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- array_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "array.array",
+ sizeof(arrayobject),
+ 0,
+ (destructor)array_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)array_repr, /* tp_repr */
+ 0, /* tp_as_number*/
+ &array_as_sequence, /* tp_as_sequence*/
+ &array_as_mapping, /* tp_as_mapping*/
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ &array_as_buffer, /* tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ arraytype_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ array_richcompare, /* tp_richcompare */
+ offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */
+ (getiterfunc)array_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ array_methods, /* tp_methods */
+ 0, /* tp_members */
+ array_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ array_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
/*********************** Array Iterator **************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t index;
- arrayobject *ao;
- PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
+ PyObject_HEAD
+ Py_ssize_t index;
+ arrayobject *ao;
+ PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
} arrayiterobject;
static PyTypeObject PyArrayIter_Type;
@@ -2088,79 +2088,79 @@ static PyTypeObject PyArrayIter_Type;
static PyObject *
array_iter(arrayobject *ao)
{
- arrayiterobject *it;
+ arrayiterobject *it;
- if (!array_Check(ao)) {
- PyErr_BadInternalCall();
- return NULL;
- }
+ if (!array_Check(ao)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
- it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
- if (it == NULL)
- return NULL;
+ it = PyObject_GC_New(arrayiterobject, &PyArrayIter_Type);
+ if (it == NULL)
+ return NULL;
- Py_INCREF(ao);
- it->ao = ao;
- it->index = 0;
- it->getitem = ao->ob_descr->getitem;
- PyObject_GC_Track(it);
- return (PyObject *)it;
+ Py_INCREF(ao);
+ it->ao = ao;
+ it->index = 0;
+ it->getitem = ao->ob_descr->getitem;
+ PyObject_GC_Track(it);
+ return (PyObject *)it;
}
static PyObject *
arrayiter_next(arrayiterobject *it)
{
- assert(PyArrayIter_Check(it));
- if (it->index < Py_SIZE(it->ao))
- return (*it->getitem)(it->ao, it->index++);
- return NULL;
+ assert(PyArrayIter_Check(it));
+ if (it->index < Py_SIZE(it->ao))
+ return (*it->getitem)(it->ao, it->index++);
+ return NULL;
}
static void
arrayiter_dealloc(arrayiterobject *it)
{
- PyObject_GC_UnTrack(it);
- Py_XDECREF(it->ao);
- PyObject_GC_Del(it);
+ PyObject_GC_UnTrack(it);
+ Py_XDECREF(it->ao);
+ PyObject_GC_Del(it);
}
static int
arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->ao);
- return 0;
+ Py_VISIT(it->ao);
+ return 0;
}
static PyTypeObject PyArrayIter_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "arrayiterator", /* tp_name */
- sizeof(arrayiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)arrayiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)arrayiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)arrayiter_next, /* tp_iternext */
- 0, /* tp_methods */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "arrayiterator", /* tp_name */
+ sizeof(arrayiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)arrayiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)arrayiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)arrayiter_next, /* tp_iternext */
+ 0, /* tp_methods */
};
@@ -2172,54 +2172,54 @@ static PyMethodDef a_methods[] = {
};
static struct PyModuleDef arraymodule = {
- PyModuleDef_HEAD_INIT,
- "array",
- module_doc,
- -1,
- a_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "array",
+ module_doc,
+ -1,
+ a_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_array(void)
{
- PyObject *m;
- PyObject *typecodes;
- Py_ssize_t size = 0;
- register Py_UNICODE *p;
- struct arraydescr *descr;
-
- if (PyType_Ready(&Arraytype) < 0)
- return NULL;
- Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
- m = PyModule_Create(&arraymodule);
- if (m == NULL)
- return NULL;
-
- Py_INCREF((PyObject *)&Arraytype);
- PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
- Py_INCREF((PyObject *)&Arraytype);
- PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
-
- for (descr=descriptors; descr->typecode != '\0'; descr++) {
- size++;
- }
-
- typecodes = PyUnicode_FromStringAndSize(NULL, size);
- p = PyUnicode_AS_UNICODE(typecodes);
- for (descr = descriptors; descr->typecode != '\0'; descr++) {
- *p++ = (char)descr->typecode;
- }
-
- PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
-
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- m = NULL;
- }
- return m;
+ PyObject *m;
+ PyObject *typecodes;
+ Py_ssize_t size = 0;
+ register Py_UNICODE *p;
+ struct arraydescr *descr;
+
+ if (PyType_Ready(&Arraytype) < 0)
+ return NULL;
+ Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
+ m = PyModule_Create(&arraymodule);
+ if (m == NULL)
+ return NULL;
+
+ Py_INCREF((PyObject *)&Arraytype);
+ PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
+ Py_INCREF((PyObject *)&Arraytype);
+ PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
+
+ for (descr=descriptors; descr->typecode != '\0'; descr++) {
+ size++;
+ }
+
+ typecodes = PyUnicode_FromStringAndSize(NULL, size);
+ p = PyUnicode_AS_UNICODE(typecodes);
+ for (descr = descriptors; descr->typecode != '\0'; descr++) {
+ *p++ = (char)descr->typecode;
+ }
+
+ PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
+
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ m = NULL;
+ }
+ return m;
}
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 0acf672..3985ca6 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -53,13 +53,13 @@ static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
static PyInt16
search(PyInt16 val, PyInt16 *table, int size)
{
- int i;
+ int i;
- for (i = 0; i < size; i++) {
- if (val <= *table++)
- return (i);
- }
- return (size);
+ for (i = 0; i < size; i++) {
+ if (val <= *table++)
+ return (i);
+ }
+ return (size);
}
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
@@ -83,7 +83,7 @@ static PyInt16 _st_ulaw2linear16[256] = {
-228, -212, -196, -180, -164, -148, -132,
-120, -112, -104, -96, -88, -80, -72,
-64, -56, -48, -40, -32, -24, -16,
- -8, 0, 32124, 31100, 30076, 29052, 28028,
+ -8, 0, 32124, 31100, 30076, 29052, 28028,
27004, 25980, 24956, 23932, 22908, 21884, 20860,
19836, 18812, 17788, 16764, 15996, 15484, 14972,
14460, 13948, 13436, 12924, 12412, 11900, 11388,
@@ -100,8 +100,8 @@ static PyInt16 _st_ulaw2linear16[256] = {
372, 356, 340, 324, 308, 292, 276,
260, 244, 228, 212, 196, 180, 164,
148, 132, 120, 112, 104, 96, 88,
- 80, 72, 64, 56, 48, 40, 32,
- 24, 16, 8, 0
+ 80, 72, 64, 56, 48, 40, 32,
+ 24, 16, 8, 0
};
/*
@@ -137,39 +137,39 @@ static PyInt16 _st_ulaw2linear16[256] = {
* John Wiley & Sons, pps 98-111 and 472-476.
*/
static unsigned char
-st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
+st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
{
- PyInt16 mask;
- PyInt16 seg;
- unsigned char uval;
-
- /* The original sox code does this in the calling function, not here */
- pcm_val = pcm_val >> 2;
-
- /* u-law inverts all bits */
- /* Get the sign and the magnitude of the value. */
- if (pcm_val < 0) {
- pcm_val = -pcm_val;
- mask = 0x7F;
- } else {
- mask = 0xFF;
- }
- if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
- pcm_val += (BIAS >> 2);
-
- /* Convert the scaled magnitude to segment number. */
- seg = search(pcm_val, seg_uend, 8);
-
- /*
- * Combine the sign, segment, quantization bits;
- * and complement the code word.
- */
- if (seg >= 8) /* out of range, return maximum value. */
- return (unsigned char) (0x7F ^ mask);
- else {
- uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
- return (uval ^ mask);
- }
+ PyInt16 mask;
+ PyInt16 seg;
+ unsigned char uval;
+
+ /* The original sox code does this in the calling function, not here */
+ pcm_val = pcm_val >> 2;
+
+ /* u-law inverts all bits */
+ /* Get the sign and the magnitude of the value. */
+ if (pcm_val < 0) {
+ pcm_val = -pcm_val;
+ mask = 0x7F;
+ } else {
+ mask = 0xFF;
+ }
+ if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
+ pcm_val += (BIAS >> 2);
+
+ /* Convert the scaled magnitude to segment number. */
+ seg = search(pcm_val, seg_uend, 8);
+
+ /*
+ * Combine the sign, segment, quantization bits;
+ * and complement the code word.
+ */
+ if (seg >= 8) /* out of range, return maximum value. */
+ return (unsigned char) (0x7F ^ mask);
+ else {
+ uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
+ return (uval ^ mask);
+ }
}
@@ -234,59 +234,59 @@ static PyInt16 _st_alaw2linear16[256] = {
* John Wiley & Sons, pps 98-111 and 472-476.
*/
static unsigned char
-st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
+st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
{
- PyInt16 mask;
- short seg;
- unsigned char aval;
-
- /* The original sox code does this in the calling function, not here */
- pcm_val = pcm_val >> 3;
-
- /* A-law using even bit inversion */
- if (pcm_val >= 0) {
- mask = 0xD5; /* sign (7th) bit = 1 */
- } else {
- mask = 0x55; /* sign bit = 0 */
- pcm_val = -pcm_val - 1;
- }
-
- /* Convert the scaled magnitude to segment number. */
- seg = search(pcm_val, seg_aend, 8);
-
- /* Combine the sign, segment, and quantization bits. */
-
- if (seg >= 8) /* out of range, return maximum value. */
- return (unsigned char) (0x7F ^ mask);
- else {
- aval = (unsigned char) seg << SEG_SHIFT;
- if (seg < 2)
- aval |= (pcm_val >> 1) & QUANT_MASK;
- else
- aval |= (pcm_val >> seg) & QUANT_MASK;
- return (aval ^ mask);
- }
+ PyInt16 mask;
+ short seg;
+ unsigned char aval;
+
+ /* The original sox code does this in the calling function, not here */
+ pcm_val = pcm_val >> 3;
+
+ /* A-law using even bit inversion */
+ if (pcm_val >= 0) {
+ mask = 0xD5; /* sign (7th) bit = 1 */
+ } else {
+ mask = 0x55; /* sign bit = 0 */
+ pcm_val = -pcm_val - 1;
+ }
+
+ /* Convert the scaled magnitude to segment number. */
+ seg = search(pcm_val, seg_aend, 8);
+
+ /* Combine the sign, segment, and quantization bits. */
+
+ if (seg >= 8) /* out of range, return maximum value. */
+ return (unsigned char) (0x7F ^ mask);
+ else {
+ aval = (unsigned char) seg << SEG_SHIFT;
+ if (seg < 2)
+ aval |= (pcm_val >> 1) & QUANT_MASK;
+ else
+ aval |= (pcm_val >> seg) & QUANT_MASK;
+ return (aval ^ mask);
+ }
}
/* End of code taken from sox */
/* Intel ADPCM step variation table */
static int indexTable[16] = {
- -1, -1, -1, -1, 2, 4, 6, 8,
- -1, -1, -1, -1, 2, 4, 6, 8,
+ -1, -1, -1, -1, 2, 4, 6, 8,
+ -1, -1, -1, -1, 2, 4, 6, 8,
};
static int stepsizeTable[89] = {
- 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
- 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
- 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
- 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
- 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
- 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
- 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
- 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
- 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
+ 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
+ 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
+ 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
+ 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
+ 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
+ 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
+ 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
+ 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
+ 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
-
+
#define CHARP(cp, i) ((signed char *)(cp+i))
#define SHORTP(cp, i) ((short *)(cp+i))
#define LONGP(cp, i) ((Py_Int32 *)(cp+i))
@@ -298,137 +298,137 @@ static PyObject *AudioopError;
static PyObject *
audioop_getsample(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- if ( i < 0 || i >= len/size ) {
- PyErr_SetString(AudioopError, "Index out of range");
- return 0;
- }
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
- else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
- return PyLong_FromLong(val);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ if ( i < 0 || i >= len/size ) {
+ PyErr_SetString(AudioopError, "Index out of range");
+ return 0;
+ }
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
+ return PyLong_FromLong(val);
}
static PyObject *
audioop_max(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
- int max = 0;
-
- if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- if ( val < 0 ) val = (-val);
- if ( val > max ) max = val;
- }
- return PyLong_FromLong(max);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+ int max = 0;
+
+ if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ for ( i=0; i<len; i+= size) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ if ( val < 0 ) val = (-val);
+ if ( val > max ) max = val;
+ }
+ return PyLong_FromLong(max);
}
static PyObject *
audioop_minmax(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
- int min = 0x7fffffff, max = -0x7fffffff;
-
- if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
- return NULL;
- if (size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return NULL;
- }
- for (i = 0; i < len; i += size) {
- if (size == 1) val = (int) *CHARP(cp, i);
- else if (size == 2) val = (int) *SHORTP(cp, i);
- else if (size == 4) val = (int) *LONGP(cp, i);
- if (val > max) max = val;
- if (val < min) min = val;
- }
- return Py_BuildValue("(ii)", min, max);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+ int min = 0x7fffffff, max = -0x7fffffff;
+
+ if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
+ return NULL;
+ if (size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return NULL;
+ }
+ for (i = 0; i < len; i += size) {
+ if (size == 1) val = (int) *CHARP(cp, i);
+ else if (size == 2) val = (int) *SHORTP(cp, i);
+ else if (size == 4) val = (int) *LONGP(cp, i);
+ if (val > max) max = val;
+ if (val < min) min = val;
+ }
+ return Py_BuildValue("(ii)", min, max);
}
static PyObject *
audioop_avg(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
- double avg = 0.0;
-
- if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- avg += val;
- }
- if ( len == 0 )
- val = 0;
- else
- val = (int)(avg / (double)(len/size));
- return PyLong_FromLong(val);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+ double avg = 0.0;
+
+ if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ for ( i=0; i<len; i+= size) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ avg += val;
+ }
+ if ( len == 0 )
+ val = 0;
+ else
+ val = (int)(avg / (double)(len/size));
+ return PyLong_FromLong(val);
}
static PyObject *
audioop_rms(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
- double sum_squares = 0.0;
-
- if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- sum_squares += (double)val*(double)val;
- }
- if ( len == 0 )
- val = 0;
- else
- val = (int)sqrt(sum_squares / (double)(len/size));
- return PyLong_FromLong(val);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+ double sum_squares = 0.0;
+
+ if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ for ( i=0; i<len; i+= size) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ sum_squares += (double)val*(double)val;
+ }
+ if ( len == 0 )
+ val = 0;
+ else
+ val = (int)sqrt(sum_squares / (double)(len/size));
+ return PyLong_FromLong(val);
}
static double _sum2(short *a, short *b, int len)
{
- int i;
- double sum = 0.0;
+ int i;
+ double sum = 0.0;
- for( i=0; i<len; i++) {
- sum = sum + (double)a[i]*(double)b[i];
- }
- return sum;
+ for( i=0; i<len; i++) {
+ sum = sum + (double)a[i]*(double)b[i];
+ }
+ return sum;
}
/*
@@ -466,60 +466,60 @@ static double _sum2(short *a, short *b, int len)
static PyObject *
audioop_findfit(PyObject *self, PyObject *args)
{
- short *cp1, *cp2;
- int len1, len2;
- int j, best_j;
- double aj_m1, aj_lm1;
- double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
-
- /* Passing a short** for an 's' argument is correct only
- if the string contents is aligned for interpretation
- as short[]. Due to the definition of PyBytesObject,
- this is currently (Python 2.6) the case. */
- if ( !PyArg_ParseTuple(args, "s#s#:findfit",
- (char**)&cp1, &len1, (char**)&cp2, &len2) )
- return 0;
- if ( len1 & 1 || len2 & 1 ) {
- PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
- }
- len1 >>= 1;
- len2 >>= 1;
-
- if ( len1 < len2 ) {
- PyErr_SetString(AudioopError, "First sample should be longer");
- return 0;
- }
- sum_ri_2 = _sum2(cp2, cp2, len2);
- sum_aij_2 = _sum2(cp1, cp1, len2);
- sum_aij_ri = _sum2(cp1, cp2, len2);
-
- result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
-
- best_result = result;
- best_j = 0;
- j = 0;
-
- for ( j=1; j<=len1-len2; j++) {
- aj_m1 = (double)cp1[j-1];
- aj_lm1 = (double)cp1[j+len2-1];
-
- sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
- sum_aij_ri = _sum2(cp1+j, cp2, len2);
-
- result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
- / sum_aij_2;
-
- if ( result < best_result ) {
- best_result = result;
- best_j = j;
- }
-
- }
-
- factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
-
- return Py_BuildValue("(if)", best_j, factor);
+ short *cp1, *cp2;
+ int len1, len2;
+ int j, best_j;
+ double aj_m1, aj_lm1;
+ double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
+
+ /* Passing a short** for an 's' argument is correct only
+ if the string contents is aligned for interpretation
+ as short[]. Due to the definition of PyBytesObject,
+ this is currently (Python 2.6) the case. */
+ if ( !PyArg_ParseTuple(args, "s#s#:findfit",
+ (char**)&cp1, &len1, (char**)&cp2, &len2) )
+ return 0;
+ if ( len1 & 1 || len2 & 1 ) {
+ PyErr_SetString(AudioopError, "Strings should be even-sized");
+ return 0;
+ }
+ len1 >>= 1;
+ len2 >>= 1;
+
+ if ( len1 < len2 ) {
+ PyErr_SetString(AudioopError, "First sample should be longer");
+ return 0;
+ }
+ sum_ri_2 = _sum2(cp2, cp2, len2);
+ sum_aij_2 = _sum2(cp1, cp1, len2);
+ sum_aij_ri = _sum2(cp1, cp2, len2);
+
+ result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
+
+ best_result = result;
+ best_j = 0;
+ j = 0;
+
+ for ( j=1; j<=len1-len2; j++) {
+ aj_m1 = (double)cp1[j-1];
+ aj_lm1 = (double)cp1[j+len2-1];
+
+ sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
+ sum_aij_ri = _sum2(cp1+j, cp2, len2);
+
+ result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
+ / sum_aij_2;
+
+ if ( result < best_result ) {
+ best_result = result;
+ best_j = j;
+ }
+
+ }
+
+ factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
+
+ return Py_BuildValue("(if)", best_j, factor);
}
/*
@@ -529,28 +529,28 @@ audioop_findfit(PyObject *self, PyObject *args)
static PyObject *
audioop_findfactor(PyObject *self, PyObject *args)
{
- short *cp1, *cp2;
- int len1, len2;
- double sum_ri_2, sum_aij_ri, result;
-
- if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
- (char**)&cp1, &len1, (char**)&cp2, &len2) )
- return 0;
- if ( len1 & 1 || len2 & 1 ) {
- PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
- }
- if ( len1 != len2 ) {
- PyErr_SetString(AudioopError, "Samples should be same size");
- return 0;
- }
- len2 >>= 1;
- sum_ri_2 = _sum2(cp2, cp2, len2);
- sum_aij_ri = _sum2(cp1, cp2, len2);
-
- result = sum_aij_ri / sum_ri_2;
-
- return PyFloat_FromDouble(result);
+ short *cp1, *cp2;
+ int len1, len2;
+ double sum_ri_2, sum_aij_ri, result;
+
+ if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
+ (char**)&cp1, &len1, (char**)&cp2, &len2) )
+ return 0;
+ if ( len1 & 1 || len2 & 1 ) {
+ PyErr_SetString(AudioopError, "Strings should be even-sized");
+ return 0;
+ }
+ if ( len1 != len2 ) {
+ PyErr_SetString(AudioopError, "Samples should be same size");
+ return 0;
+ }
+ len2 >>= 1;
+ sum_ri_2 = _sum2(cp2, cp2, len2);
+ sum_aij_ri = _sum2(cp1, cp2, len2);
+
+ result = sum_aij_ri / sum_ri_2;
+
+ return PyFloat_FromDouble(result);
}
/*
@@ -560,1114 +560,1114 @@ audioop_findfactor(PyObject *self, PyObject *args)
static PyObject *
audioop_findmax(PyObject *self, PyObject *args)
{
- short *cp1;
- int len1, len2;
- int j, best_j;
- double aj_m1, aj_lm1;
- double result, best_result;
-
- if ( !PyArg_ParseTuple(args, "s#i:findmax",
- (char**)&cp1, &len1, &len2) )
- return 0;
- if ( len1 & 1 ) {
- PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
- }
- len1 >>= 1;
-
- if ( len2 < 0 || len1 < len2 ) {
- PyErr_SetString(AudioopError, "Input sample should be longer");
- return 0;
- }
+ short *cp1;
+ int len1, len2;
+ int j, best_j;
+ double aj_m1, aj_lm1;
+ double result, best_result;
- result = _sum2(cp1, cp1, len2);
+ if ( !PyArg_ParseTuple(args, "s#i:findmax",
+ (char**)&cp1, &len1, &len2) )
+ return 0;
+ if ( len1 & 1 ) {
+ PyErr_SetString(AudioopError, "Strings should be even-sized");
+ return 0;
+ }
+ len1 >>= 1;
- best_result = result;
- best_j = 0;
- j = 0;
+ if ( len2 < 0 || len1 < len2 ) {
+ PyErr_SetString(AudioopError, "Input sample should be longer");
+ return 0;
+ }
- for ( j=1; j<=len1-len2; j++) {
- aj_m1 = (double)cp1[j-1];
- aj_lm1 = (double)cp1[j+len2-1];
+ result = _sum2(cp1, cp1, len2);
- result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
+ best_result = result;
+ best_j = 0;
+ j = 0;
- if ( result > best_result ) {
- best_result = result;
- best_j = j;
- }
-
+ for ( j=1; j<=len1-len2; j++) {
+ aj_m1 = (double)cp1[j-1];
+ aj_lm1 = (double)cp1[j+len2-1];
+
+ result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
+
+ if ( result > best_result ) {
+ best_result = result;
+ best_j = j;
}
- return PyLong_FromLong(best_j);
+ }
+
+ return PyLong_FromLong(best_j);
}
static PyObject *
audioop_avgpp(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0, prevval = 0, prevextremevalid = 0,
- prevextreme = 0;
- int i;
- double avg = 0.0;
- int diff, prevdiff, extremediff, nextreme = 0;
-
- if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- /* Compute first delta value ahead. Also automatically makes us
- ** skip the first extreme value
- */
- if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
- else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
- else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
- if ( size == 1 ) val = (int)*CHARP(cp, size);
- else if ( size == 2 ) val = (int)*SHORTP(cp, size);
- else if ( size == 4 ) val = (int)*LONGP(cp, size);
- prevdiff = val - prevval;
-
- for ( i=size; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- diff = val - prevval;
- if ( diff*prevdiff < 0 ) {
- /* Derivative changed sign. Compute difference to last
- ** extreme value and remember.
- */
- if ( prevextremevalid ) {
- extremediff = prevval - prevextreme;
- if ( extremediff < 0 )
- extremediff = -extremediff;
- avg += extremediff;
- nextreme++;
- }
- prevextremevalid = 1;
- prevextreme = prevval;
- }
- prevval = val;
- if ( diff != 0 )
- prevdiff = diff;
- }
- if ( nextreme == 0 )
- val = 0;
- else
- val = (int)(avg / (double)nextreme);
- return PyLong_FromLong(val);
+ signed char *cp;
+ int len, size, val = 0, prevval = 0, prevextremevalid = 0,
+ prevextreme = 0;
+ int i;
+ double avg = 0.0;
+ int diff, prevdiff, extremediff, nextreme = 0;
+
+ if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ /* Compute first delta value ahead. Also automatically makes us
+ ** skip the first extreme value
+ */
+ if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
+ else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
+ else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
+ if ( size == 1 ) val = (int)*CHARP(cp, size);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, size);
+ else if ( size == 4 ) val = (int)*LONGP(cp, size);
+ prevdiff = val - prevval;
+
+ for ( i=size; i<len; i+= size) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ diff = val - prevval;
+ if ( diff*prevdiff < 0 ) {
+ /* Derivative changed sign. Compute difference to last
+ ** extreme value and remember.
+ */
+ if ( prevextremevalid ) {
+ extremediff = prevval - prevextreme;
+ if ( extremediff < 0 )
+ extremediff = -extremediff;
+ avg += extremediff;
+ nextreme++;
+ }
+ prevextremevalid = 1;
+ prevextreme = prevval;
+ }
+ prevval = val;
+ if ( diff != 0 )
+ prevdiff = diff;
+ }
+ if ( nextreme == 0 )
+ val = 0;
+ else
+ val = (int)(avg / (double)nextreme);
+ return PyLong_FromLong(val);
}
static PyObject *
audioop_maxpp(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0, prevval = 0, prevextremevalid = 0,
- prevextreme = 0;
- int i;
- int max = 0;
- int diff, prevdiff, extremediff;
-
- if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- /* Compute first delta value ahead. Also automatically makes us
- ** skip the first extreme value
- */
- if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
- else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
- else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
- if ( size == 1 ) val = (int)*CHARP(cp, size);
- else if ( size == 2 ) val = (int)*SHORTP(cp, size);
- else if ( size == 4 ) val = (int)*LONGP(cp, size);
- prevdiff = val - prevval;
-
- for ( i=size; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- diff = val - prevval;
- if ( diff*prevdiff < 0 ) {
- /* Derivative changed sign. Compute difference to
- ** last extreme value and remember.
- */
- if ( prevextremevalid ) {
- extremediff = prevval - prevextreme;
- if ( extremediff < 0 )
- extremediff = -extremediff;
- if ( extremediff > max )
- max = extremediff;
- }
- prevextremevalid = 1;
- prevextreme = prevval;
- }
- prevval = val;
- if ( diff != 0 )
- prevdiff = diff;
- }
- return PyLong_FromLong(max);
+ signed char *cp;
+ int len, size, val = 0, prevval = 0, prevextremevalid = 0,
+ prevextreme = 0;
+ int i;
+ int max = 0;
+ int diff, prevdiff, extremediff;
+
+ if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ /* Compute first delta value ahead. Also automatically makes us
+ ** skip the first extreme value
+ */
+ if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
+ else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
+ else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
+ if ( size == 1 ) val = (int)*CHARP(cp, size);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, size);
+ else if ( size == 4 ) val = (int)*LONGP(cp, size);
+ prevdiff = val - prevval;
+
+ for ( i=size; i<len; i+= size) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ diff = val - prevval;
+ if ( diff*prevdiff < 0 ) {
+ /* Derivative changed sign. Compute difference to
+ ** last extreme value and remember.
+ */
+ if ( prevextremevalid ) {
+ extremediff = prevval - prevextreme;
+ if ( extremediff < 0 )
+ extremediff = -extremediff;
+ if ( extremediff > max )
+ max = extremediff;
+ }
+ prevextremevalid = 1;
+ prevextreme = prevval;
+ }
+ prevval = val;
+ if ( diff != 0 )
+ prevdiff = diff;
+ }
+ return PyLong_FromLong(max);
}
static PyObject *
audioop_cross(PyObject *self, PyObject *args)
{
- signed char *cp;
- int len, size, val = 0;
- int i;
- int prevval, ncross;
-
- if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
- return 0;
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
- ncross = -1;
- prevval = 17; /* Anything <> 0,1 */
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
- else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
- val = val & 1;
- if ( val != prevval ) ncross++;
- prevval = val;
- }
- return PyLong_FromLong(ncross);
+ signed char *cp;
+ int len, size, val = 0;
+ int i;
+ int prevval, ncross;
+
+ if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
+ return 0;
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+ ncross = -1;
+ prevval = 17; /* Anything <> 0,1 */
+ for ( i=0; i<len; i+= size) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
+ else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
+ val = val & 1;
+ if ( val != prevval ) ncross++;
+ prevval = val;
+ }
+ return PyLong_FromLong(ncross);
}
static PyObject *
audioop_mul(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- int len, size, val = 0;
- double factor, fval, maxval;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
- return 0;
-
- if ( size == 1 ) maxval = (double) 0x7f;
- else if ( size == 2 ) maxval = (double) 0x7fff;
- else if ( size == 4 ) maxval = (double) 0x7fffffff;
- else {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- fval = (double)val*factor;
- if ( fval > maxval ) fval = maxval;
- else if ( fval < -maxval ) fval = -maxval;
- val = (int)fval;
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
- }
- return rv;
+ signed char *cp, *ncp;
+ int len, size, val = 0;
+ double factor, fval, maxval;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
+ return 0;
+
+ if ( size == 1 ) maxval = (double) 0x7f;
+ else if ( size == 2 ) maxval = (double) 0x7fff;
+ else if ( size == 4 ) maxval = (double) 0x7fffffff;
+ else {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ fval = (double)val*factor;
+ if ( fval > maxval ) fval = maxval;
+ else if ( fval < -maxval ) fval = -maxval;
+ val = (int)fval;
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
+ }
+ return rv;
}
static PyObject *
audioop_tomono(PyObject *self, PyObject *args)
{
- Py_buffer pcp;
- signed char *cp, *ncp;
- int len, size, val1 = 0, val2 = 0;
- double fac1, fac2, fval, maxval;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s*idd:tomono",
- &pcp, &size, &fac1, &fac2 ) )
- return 0;
- cp = pcp.buf;
- len = pcp.len;
-
- if ( size == 1 ) maxval = (double) 0x7f;
- else if ( size == 2 ) maxval = (double) 0x7fff;
- else if ( size == 4 ) maxval = (double) 0x7fffffff;
- else {
- PyBuffer_Release(&pcp);
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len/2);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
-
- for ( i=0; i < len; i += size*2 ) {
- if ( size == 1 ) val1 = (int)*CHARP(cp, i);
- else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
- if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
- else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
- else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
- fval = (double)val1*fac1 + (double)val2*fac2;
- if ( fval > maxval ) fval = maxval;
- else if ( fval < -maxval ) fval = -maxval;
- val1 = (int)fval;
- if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
- else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
- else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
- }
- PyBuffer_Release(&pcp);
- return rv;
+ Py_buffer pcp;
+ signed char *cp, *ncp;
+ int len, size, val1 = 0, val2 = 0;
+ double fac1, fac2, fval, maxval;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s*idd:tomono",
+ &pcp, &size, &fac1, &fac2 ) )
+ return 0;
+ cp = pcp.buf;
+ len = pcp.len;
+
+ if ( size == 1 ) maxval = (double) 0x7f;
+ else if ( size == 2 ) maxval = (double) 0x7fff;
+ else if ( size == 4 ) maxval = (double) 0x7fffffff;
+ else {
+ PyBuffer_Release(&pcp);
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len/2);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+
+ for ( i=0; i < len; i += size*2 ) {
+ if ( size == 1 ) val1 = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
+ if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
+ else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
+ else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
+ fval = (double)val1*fac1 + (double)val2*fac2;
+ if ( fval > maxval ) fval = maxval;
+ else if ( fval < -maxval ) fval = -maxval;
+ val1 = (int)fval;
+ if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
+ else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
+ else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
+ }
+ PyBuffer_Release(&pcp);
+ return rv;
}
static PyObject *
audioop_tostereo(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- int len, new_len, size, val1, val2, val = 0;
- double fac1, fac2, fval, maxval;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
- &cp, &len, &size, &fac1, &fac2 ) )
- return 0;
-
- if ( size == 1 ) maxval = (double) 0x7f;
- else if ( size == 2 ) maxval = (double) 0x7fff;
- else if ( size == 4 ) maxval = (double) 0x7fffffff;
- else {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- new_len = len*2;
- if (new_len < 0) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, new_len);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
-
- fval = (double)val*fac1;
- if ( fval > maxval ) fval = maxval;
- else if ( fval < -maxval ) fval = -maxval;
- val1 = (int)fval;
-
- fval = (double)val*fac2;
- if ( fval > maxval ) fval = maxval;
- else if ( fval < -maxval ) fval = -maxval;
- val2 = (int)fval;
-
- if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
- else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
- else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
-
- if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
- else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
- else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
- }
- return rv;
+ signed char *cp, *ncp;
+ int len, new_len, size, val1, val2, val = 0;
+ double fac1, fac2, fval, maxval;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
+ &cp, &len, &size, &fac1, &fac2 ) )
+ return 0;
+
+ if ( size == 1 ) maxval = (double) 0x7f;
+ else if ( size == 2 ) maxval = (double) 0x7fff;
+ else if ( size == 4 ) maxval = (double) 0x7fffffff;
+ else {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ new_len = len*2;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, new_len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+
+ fval = (double)val*fac1;
+ if ( fval > maxval ) fval = maxval;
+ else if ( fval < -maxval ) fval = -maxval;
+ val1 = (int)fval;
+
+ fval = (double)val*fac2;
+ if ( fval > maxval ) fval = maxval;
+ else if ( fval < -maxval ) fval = -maxval;
+ val2 = (int)fval;
+
+ if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
+ else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
+ else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
+
+ if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
+ else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
+ else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
+ }
+ return rv;
}
static PyObject *
audioop_add(PyObject *self, PyObject *args)
{
- signed char *cp1, *cp2, *ncp;
- int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#s#i:add",
- &cp1, &len1, &cp2, &len2, &size ) )
- return 0;
-
- if ( len1 != len2 ) {
- PyErr_SetString(AudioopError, "Lengths should be the same");
- return 0;
- }
-
- if ( size == 1 ) maxval = 0x7f;
- else if ( size == 2 ) maxval = 0x7fff;
- else if ( size == 4 ) maxval = 0x7fffffff;
- else {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len1);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len1; i += size ) {
- if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
- else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
- else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
-
- if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
- else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
- else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
-
- newval = val1 + val2;
- /* truncate in case of overflow */
- if (newval > maxval) newval = maxval;
- else if (newval < -maxval) newval = -maxval;
- else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
- newval = val1 > 0 ? maxval : - maxval;
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
- }
- return rv;
+ signed char *cp1, *cp2, *ncp;
+ int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#s#i:add",
+ &cp1, &len1, &cp2, &len2, &size ) )
+ return 0;
+
+ if ( len1 != len2 ) {
+ PyErr_SetString(AudioopError, "Lengths should be the same");
+ return 0;
+ }
+
+ if ( size == 1 ) maxval = 0x7f;
+ else if ( size == 2 ) maxval = 0x7fff;
+ else if ( size == 4 ) maxval = 0x7fffffff;
+ else {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len1);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < len1; i += size ) {
+ if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
+ else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
+ else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
+
+ if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
+ else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
+ else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
+
+ newval = val1 + val2;
+ /* truncate in case of overflow */
+ if (newval > maxval) newval = maxval;
+ else if (newval < -maxval) newval = -maxval;
+ else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
+ newval = val1 > 0 ? maxval : - maxval;
+
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
+ }
+ return rv;
}
static PyObject *
audioop_bias(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- int len, size, val = 0;
- PyObject *rv;
- int i;
- int bias;
-
- if ( !PyArg_ParseTuple(args, "s#ii:bias",
- &cp, &len, &size , &bias) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
- }
- return rv;
+ signed char *cp, *ncp;
+ int len, size, val = 0;
+ PyObject *rv;
+ int i;
+ int bias;
+
+ if ( !PyArg_ParseTuple(args, "s#ii:bias",
+ &cp, &len, &size , &bias) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = (int)*CHARP(cp, i);
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = (int)*LONGP(cp, i);
+
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
+ }
+ return rv;
}
static PyObject *
audioop_reverse(PyObject *self, PyObject *args)
{
- signed char *cp;
- unsigned char *ncp;
- int len, size, val = 0;
- PyObject *rv;
- int i, j;
-
- if ( !PyArg_ParseTuple(args, "s#i:reverse",
- &cp, &len, &size) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4 ) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
-
- j = len - i - size;
-
- if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
- else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
- else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
- }
- return rv;
+ signed char *cp;
+ unsigned char *ncp;
+ int len, size, val = 0;
+ PyObject *rv;
+ int i, j;
+
+ if ( !PyArg_ParseTuple(args, "s#i:reverse",
+ &cp, &len, &size) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4 ) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+
+ j = len - i - size;
+
+ if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
+ else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
+ else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
+ }
+ return rv;
}
static PyObject *
audioop_lin2lin(PyObject *self, PyObject *args)
{
- signed char *cp;
- unsigned char *ncp;
- int len, new_len, size, size2, val = 0;
- PyObject *rv;
- int i, j;
-
- if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
- &cp, &len, &size, &size2) )
- return 0;
-
- if ( (size != 1 && size != 2 && size != 4) ||
- (size2 != 1 && size2 != 2 && size2 != 4)) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- new_len = (len/size)*size2;
- if (new_len < 0) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
- }
- rv = PyBytes_FromStringAndSize(NULL, new_len);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
-
- for ( i=0, j=0; i < len; i += size, j += size2 ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
-
- if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
- else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
- else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
- }
- return rv;
+ signed char *cp;
+ unsigned char *ncp;
+ int len, new_len, size, size2, val = 0;
+ PyObject *rv;
+ int i, j;
+
+ if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
+ &cp, &len, &size, &size2) )
+ return 0;
+
+ if ( (size != 1 && size != 2 && size != 4) ||
+ (size2 != 1 && size2 != 2 && size2 != 4)) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ new_len = (len/size)*size2;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+ rv = PyBytes_FromStringAndSize(NULL, new_len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
+
+ for ( i=0, j=0; i < len; i += size, j += size2 ) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+
+ if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
+ else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
+ else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
+ }
+ return rv;
}
static int
gcd(int a, int b)
{
- while (b > 0) {
- int tmp = a % b;
- a = b;
- b = tmp;
- }
- return a;
+ while (b > 0) {
+ int tmp = a % b;
+ a = b;
+ b = tmp;
+ }
+ return a;
}
static PyObject *
audioop_ratecv(PyObject *self, PyObject *args)
{
- char *cp, *ncp;
- int len, size, nchannels, inrate, outrate, weightA, weightB;
- int chan, d, *prev_i, *cur_i, cur_o;
- PyObject *state, *samps, *str, *rv = NULL;
- int bytes_per_frame;
- size_t alloc_size;
-
- weightA = 1;
- weightB = 0;
- if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
- &nchannels, &inrate, &outrate, &state,
- &weightA, &weightB))
- return NULL;
- if (size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return NULL;
- }
- if (nchannels < 1) {
- PyErr_SetString(AudioopError, "# of channels should be >= 1");
- return NULL;
- }
- bytes_per_frame = size * nchannels;
- if (bytes_per_frame / nchannels != size) {
- /* This overflow test is rigorously correct because
- both multiplicands are >= 1. Use the argument names
- from the docs for the error msg. */
- PyErr_SetString(PyExc_OverflowError,
- "width * nchannels too big for a C int");
- return NULL;
- }
- if (weightA < 1 || weightB < 0) {
- PyErr_SetString(AudioopError,
- "weightA should be >= 1, weightB should be >= 0");
- return NULL;
- }
- if (len % bytes_per_frame != 0) {
- PyErr_SetString(AudioopError, "not a whole number of frames");
- return NULL;
- }
- if (inrate <= 0 || outrate <= 0) {
- PyErr_SetString(AudioopError, "sampling rate not > 0");
- return NULL;
- }
- /* divide inrate and outrate by their greatest common divisor */
- d = gcd(inrate, outrate);
- inrate /= d;
- outrate /= d;
-
- alloc_size = sizeof(int) * (unsigned)nchannels;
- if (alloc_size < (unsigned)nchannels) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
- }
- prev_i = (int *) malloc(alloc_size);
- cur_i = (int *) malloc(alloc_size);
- if (prev_i == NULL || cur_i == NULL) {
- (void) PyErr_NoMemory();
+ char *cp, *ncp;
+ int len, size, nchannels, inrate, outrate, weightA, weightB;
+ int chan, d, *prev_i, *cur_i, cur_o;
+ PyObject *state, *samps, *str, *rv = NULL;
+ int bytes_per_frame;
+ size_t alloc_size;
+
+ weightA = 1;
+ weightB = 0;
+ if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
+ &nchannels, &inrate, &outrate, &state,
+ &weightA, &weightB))
+ return NULL;
+ if (size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return NULL;
+ }
+ if (nchannels < 1) {
+ PyErr_SetString(AudioopError, "# of channels should be >= 1");
+ return NULL;
+ }
+ bytes_per_frame = size * nchannels;
+ if (bytes_per_frame / nchannels != size) {
+ /* This overflow test is rigorously correct because
+ both multiplicands are >= 1. Use the argument names
+ from the docs for the error msg. */
+ PyErr_SetString(PyExc_OverflowError,
+ "width * nchannels too big for a C int");
+ return NULL;
+ }
+ if (weightA < 1 || weightB < 0) {
+ PyErr_SetString(AudioopError,
+ "weightA should be >= 1, weightB should be >= 0");
+ return NULL;
+ }
+ if (len % bytes_per_frame != 0) {
+ PyErr_SetString(AudioopError, "not a whole number of frames");
+ return NULL;
+ }
+ if (inrate <= 0 || outrate <= 0) {
+ PyErr_SetString(AudioopError, "sampling rate not > 0");
+ return NULL;
+ }
+ /* divide inrate and outrate by their greatest common divisor */
+ d = gcd(inrate, outrate);
+ inrate /= d;
+ outrate /= d;
+
+ alloc_size = sizeof(int) * (unsigned)nchannels;
+ if (alloc_size < (unsigned)nchannels) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+ prev_i = (int *) malloc(alloc_size);
+ cur_i = (int *) malloc(alloc_size);
+ if (prev_i == NULL || cur_i == NULL) {
+ (void) PyErr_NoMemory();
+ goto exit;
+ }
+
+ len /= bytes_per_frame; /* # of frames */
+
+ if (state == Py_None) {
+ d = -outrate;
+ for (chan = 0; chan < nchannels; chan++)
+ prev_i[chan] = cur_i[chan] = 0;
+ }
+ else {
+ if (!PyArg_ParseTuple(state,
+ "iO!;audioop.ratecv: illegal state argument",
+ &d, &PyTuple_Type, &samps))
+ goto exit;
+ if (PyTuple_Size(samps) != nchannels) {
+ PyErr_SetString(AudioopError,
+ "illegal state argument");
+ goto exit;
+ }
+ for (chan = 0; chan < nchannels; chan++) {
+ if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
+ "ii:ratecv", &prev_i[chan],
+ &cur_i[chan]))
goto exit;
}
-
- len /= bytes_per_frame; /* # of frames */
-
- if (state == Py_None) {
- d = -outrate;
+ }
+
+ /* str <- Space for the output buffer. */
+ {
+ /* There are len input frames, so we need (mathematically)
+ ceiling(len*outrate/inrate) output frames, and each frame
+ requires bytes_per_frame bytes. Computing this
+ without spurious overflow is the challenge; we can
+ settle for a reasonable upper bound, though. */
+ int ceiling; /* the number of output frames */
+ int nbytes; /* the number of output bytes needed */
+ int q = len / inrate;
+ /* Now len = q * inrate + r exactly (with r = len % inrate),
+ and this is less than q * inrate + inrate = (q+1)*inrate.
+ So a reasonable upper bound on len*outrate/inrate is
+ ((q+1)*inrate)*outrate/inrate =
+ (q+1)*outrate.
+ */
+ ceiling = (q+1) * outrate;
+ nbytes = ceiling * bytes_per_frame;
+ /* See whether anything overflowed; if not, get the space. */
+ if (q+1 < 0 ||
+ ceiling / outrate != q+1 ||
+ nbytes / bytes_per_frame != ceiling)
+ str = NULL;
+ else
+ str = PyBytes_FromStringAndSize(NULL, nbytes);
+
+ if (str == NULL) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ goto exit;
+ }
+ }
+ ncp = PyBytes_AsString(str);
+
+ for (;;) {
+ while (d < 0) {
+ if (len == 0) {
+ samps = PyTuple_New(nchannels);
+ if (samps == NULL)
+ goto exit;
for (chan = 0; chan < nchannels; chan++)
- prev_i[chan] = cur_i[chan] = 0;
- }
- else {
- if (!PyArg_ParseTuple(state,
- "iO!;audioop.ratecv: illegal state argument",
- &d, &PyTuple_Type, &samps))
- goto exit;
- if (PyTuple_Size(samps) != nchannels) {
- PyErr_SetString(AudioopError,
- "illegal state argument");
- goto exit;
- }
- for (chan = 0; chan < nchannels; chan++) {
- if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
- "ii:ratecv", &prev_i[chan],
- &cur_i[chan]))
- goto exit;
- }
- }
-
- /* str <- Space for the output buffer. */
- {
- /* There are len input frames, so we need (mathematically)
- ceiling(len*outrate/inrate) output frames, and each frame
- requires bytes_per_frame bytes. Computing this
- without spurious overflow is the challenge; we can
- settle for a reasonable upper bound, though. */
- int ceiling; /* the number of output frames */
- int nbytes; /* the number of output bytes needed */
- int q = len / inrate;
- /* Now len = q * inrate + r exactly (with r = len % inrate),
- and this is less than q * inrate + inrate = (q+1)*inrate.
- So a reasonable upper bound on len*outrate/inrate is
- ((q+1)*inrate)*outrate/inrate =
- (q+1)*outrate.
- */
- ceiling = (q+1) * outrate;
- nbytes = ceiling * bytes_per_frame;
- /* See whether anything overflowed; if not, get the space. */
- if (q+1 < 0 ||
- ceiling / outrate != q+1 ||
- nbytes / bytes_per_frame != ceiling)
- str = NULL;
- else
- str = PyBytes_FromStringAndSize(NULL, nbytes);
-
- if (str == NULL) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- goto exit;
- }
- }
- ncp = PyBytes_AsString(str);
-
- for (;;) {
- while (d < 0) {
- if (len == 0) {
- samps = PyTuple_New(nchannels);
- if (samps == NULL)
- goto exit;
- for (chan = 0; chan < nchannels; chan++)
- PyTuple_SetItem(samps, chan,
- Py_BuildValue("(ii)",
- prev_i[chan],
- cur_i[chan]));
- if (PyErr_Occurred())
- goto exit;
- /* We have checked before that the length
- * of the string fits into int. */
- len = (int)(ncp - PyBytes_AsString(str));
- rv = PyBytes_FromStringAndSize
- (PyBytes_AsString(str), len);
- Py_DECREF(str);
- str = rv;
- if (str == NULL)
- goto exit;
- rv = Py_BuildValue("(O(iO))", str, d, samps);
- Py_DECREF(samps);
- Py_DECREF(str);
- goto exit; /* return rv */
- }
- for (chan = 0; chan < nchannels; chan++) {
- prev_i[chan] = cur_i[chan];
- if (size == 1)
- cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
- else if (size == 2)
- cur_i[chan] = (int)*SHORTP(cp, 0);
- else if (size == 4)
- cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
- cp += size;
- /* implements a simple digital filter */
- cur_i[chan] =
- (weightA * cur_i[chan] +
- weightB * prev_i[chan]) /
- (weightA + weightB);
- }
- len--;
- d += outrate;
- }
- while (d >= 0) {
- for (chan = 0; chan < nchannels; chan++) {
- cur_o = (prev_i[chan] * d +
- cur_i[chan] * (outrate - d)) /
- outrate;
- if (size == 1)
- *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
- else if (size == 2)
- *SHORTP(ncp, 0) = (short)(cur_o);
- else if (size == 4)
- *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
- ncp += size;
- }
- d -= inrate;
- }
- }
+ PyTuple_SetItem(samps, chan,
+ Py_BuildValue("(ii)",
+ prev_i[chan],
+ cur_i[chan]));
+ if (PyErr_Occurred())
+ goto exit;
+ /* We have checked before that the length
+ * of the string fits into int. */
+ len = (int)(ncp - PyBytes_AsString(str));
+ rv = PyBytes_FromStringAndSize
+ (PyBytes_AsString(str), len);
+ Py_DECREF(str);
+ str = rv;
+ if (str == NULL)
+ goto exit;
+ rv = Py_BuildValue("(O(iO))", str, d, samps);
+ Py_DECREF(samps);
+ Py_DECREF(str);
+ goto exit; /* return rv */
+ }
+ for (chan = 0; chan < nchannels; chan++) {
+ prev_i[chan] = cur_i[chan];
+ if (size == 1)
+ cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
+ else if (size == 2)
+ cur_i[chan] = (int)*SHORTP(cp, 0);
+ else if (size == 4)
+ cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
+ cp += size;
+ /* implements a simple digital filter */
+ cur_i[chan] =
+ (weightA * cur_i[chan] +
+ weightB * prev_i[chan]) /
+ (weightA + weightB);
+ }
+ len--;
+ d += outrate;
+ }
+ while (d >= 0) {
+ for (chan = 0; chan < nchannels; chan++) {
+ cur_o = (prev_i[chan] * d +
+ cur_i[chan] * (outrate - d)) /
+ outrate;
+ if (size == 1)
+ *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
+ else if (size == 2)
+ *SHORTP(ncp, 0) = (short)(cur_o);
+ else if (size == 4)
+ *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
+ ncp += size;
+ }
+ d -= inrate;
+ }
+ }
exit:
- if (prev_i != NULL)
- free(prev_i);
- if (cur_i != NULL)
- free(cur_i);
- return rv;
+ if (prev_i != NULL)
+ free(prev_i);
+ if (cur_i != NULL)
+ free(cur_i);
+ return rv;
}
static PyObject *
audioop_lin2ulaw(PyObject *self, PyObject *args)
{
- signed char *cp;
- unsigned char *ncp;
- int len, size, val = 0;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
- &cp, &len, &size) )
- return 0 ;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len/size);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
-
- *ncp++ = st_14linear2ulaw(val);
- }
- return rv;
+ signed char *cp;
+ unsigned char *ncp;
+ int len, size, val = 0;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
+ &cp, &len, &size) )
+ return 0 ;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len/size);
+ if ( rv == 0 )
+ return 0;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+
+ *ncp++ = st_14linear2ulaw(val);
+ }
+ return rv;
}
static PyObject *
audioop_ulaw2lin(PyObject *self, PyObject *args)
{
- unsigned char *cp;
- unsigned char cval;
- signed char *ncp;
- int len, new_len, size, val;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
- &cp, &len, &size) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- new_len = len*size;
- if (new_len < 0) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
- }
- rv = PyBytes_FromStringAndSize(NULL, new_len);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < new_len; i += size ) {
- cval = *cp++;
- val = st_ulaw2linear16(cval);
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
- }
- return rv;
+ unsigned char *cp;
+ unsigned char cval;
+ signed char *ncp;
+ int len, new_len, size, val;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
+ &cp, &len, &size) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ new_len = len*size;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+ rv = PyBytes_FromStringAndSize(NULL, new_len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < new_len; i += size ) {
+ cval = *cp++;
+ val = st_ulaw2linear16(cval);
+
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
+ }
+ return rv;
}
static PyObject *
audioop_lin2alaw(PyObject *self, PyObject *args)
{
- signed char *cp;
- unsigned char *ncp;
- int len, size, val = 0;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
- &cp, &len, &size) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- rv = PyBytes_FromStringAndSize(NULL, len/size);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
-
- *ncp++ = st_linear2alaw(val);
- }
- return rv;
+ signed char *cp;
+ unsigned char *ncp;
+ int len, size, val = 0;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
+ &cp, &len, &size) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ rv = PyBytes_FromStringAndSize(NULL, len/size);
+ if ( rv == 0 )
+ return 0;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+
+ *ncp++ = st_linear2alaw(val);
+ }
+ return rv;
}
static PyObject *
audioop_alaw2lin(PyObject *self, PyObject *args)
{
- unsigned char *cp;
- unsigned char cval;
- signed char *ncp;
- int len, new_len, size, val;
- PyObject *rv;
- int i;
-
- if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
- &cp, &len, &size) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- new_len = len*size;
- if (new_len < 0) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
- }
- rv = PyBytes_FromStringAndSize(NULL, new_len);
- if ( rv == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < new_len; i += size ) {
- cval = *cp++;
- val = st_alaw2linear16(cval);
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
- }
- return rv;
+ unsigned char *cp;
+ unsigned char cval;
+ signed char *ncp;
+ int len, new_len, size, val;
+ PyObject *rv;
+ int i;
+
+ if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
+ &cp, &len, &size) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ new_len = len*size;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+ rv = PyBytes_FromStringAndSize(NULL, new_len);
+ if ( rv == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(rv);
+
+ for ( i=0; i < new_len; i += size ) {
+ cval = *cp++;
+ val = st_alaw2linear16(cval);
+
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
+ }
+ return rv;
}
static PyObject *
audioop_lin2adpcm(PyObject *self, PyObject *args)
{
- signed char *cp;
- signed char *ncp;
- int len, size, val = 0, step, valpred, delta,
- index, sign, vpdiff, diff;
- PyObject *rv, *state, *str;
- int i, outputbuffer = 0, bufferstep;
-
- if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
- &cp, &len, &size, &state) )
- return 0;
-
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
+ signed char *cp;
+ signed char *ncp;
+ int len, size, val = 0, step, valpred, delta,
+ index, sign, vpdiff, diff;
+ PyObject *rv, *state, *str;
+ int i, outputbuffer = 0, bufferstep;
+
+ if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
+ &cp, &len, &size, &state) )
+ return 0;
+
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ str = PyBytes_FromStringAndSize(NULL, len/(size*2));
+ if ( str == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(str);
+
+ /* Decode state, should have (value, step) */
+ if ( state == Py_None ) {
+ /* First time, it seems. Set defaults */
+ valpred = 0;
+ step = 7;
+ index = 0;
+ } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
+ return 0;
+
+ step = stepsizeTable[index];
+ bufferstep = 1;
+
+ for ( i=0; i < len; i += size ) {
+ if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
+ else if ( size == 2 ) val = (int)*SHORTP(cp, i);
+ else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+
+ /* Step 1 - compute difference with previous value */
+ diff = val - valpred;
+ sign = (diff < 0) ? 8 : 0;
+ if ( sign ) diff = (-diff);
+
+ /* Step 2 - Divide and clamp */
+ /* Note:
+ ** This code *approximately* computes:
+ ** delta = diff*4/step;
+ ** vpdiff = (delta+0.5)*step/4;
+ ** but in shift step bits are dropped. The net result of this
+ ** is that even if you have fast mul/div hardware you cannot
+ ** put it to good use since the fixup would be too expensive.
+ */
+ delta = 0;
+ vpdiff = (step >> 3);
+
+ if ( diff >= step ) {
+ delta = 4;
+ diff -= step;
+ vpdiff += step;
}
-
- str = PyBytes_FromStringAndSize(NULL, len/(size*2));
- if ( str == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(str);
-
- /* Decode state, should have (value, step) */
- if ( state == Py_None ) {
- /* First time, it seems. Set defaults */
- valpred = 0;
- step = 7;
- index = 0;
- } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
- return 0;
+ step >>= 1;
+ if ( diff >= step ) {
+ delta |= 2;
+ diff -= step;
+ vpdiff += step;
+ }
+ step >>= 1;
+ if ( diff >= step ) {
+ delta |= 1;
+ vpdiff += step;
+ }
+
+ /* Step 3 - Update previous value */
+ if ( sign )
+ valpred -= vpdiff;
+ else
+ valpred += vpdiff;
+ /* Step 4 - Clamp previous value to 16 bits */
+ if ( valpred > 32767 )
+ valpred = 32767;
+ else if ( valpred < -32768 )
+ valpred = -32768;
+
+ /* Step 5 - Assemble value, update index and step values */
+ delta |= sign;
+
+ index += indexTable[delta];
+ if ( index < 0 ) index = 0;
+ if ( index > 88 ) index = 88;
step = stepsizeTable[index];
- bufferstep = 1;
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
-
- /* Step 1 - compute difference with previous value */
- diff = val - valpred;
- sign = (diff < 0) ? 8 : 0;
- if ( sign ) diff = (-diff);
-
- /* Step 2 - Divide and clamp */
- /* Note:
- ** This code *approximately* computes:
- ** delta = diff*4/step;
- ** vpdiff = (delta+0.5)*step/4;
- ** but in shift step bits are dropped. The net result of this
- ** is that even if you have fast mul/div hardware you cannot
- ** put it to good use since the fixup would be too expensive.
- */
- delta = 0;
- vpdiff = (step >> 3);
-
- if ( diff >= step ) {
- delta = 4;
- diff -= step;
- vpdiff += step;
- }
- step >>= 1;
- if ( diff >= step ) {
- delta |= 2;
- diff -= step;
- vpdiff += step;
- }
- step >>= 1;
- if ( diff >= step ) {
- delta |= 1;
- vpdiff += step;
- }
-
- /* Step 3 - Update previous value */
- if ( sign )
- valpred -= vpdiff;
- else
- valpred += vpdiff;
-
- /* Step 4 - Clamp previous value to 16 bits */
- if ( valpred > 32767 )
- valpred = 32767;
- else if ( valpred < -32768 )
- valpred = -32768;
-
- /* Step 5 - Assemble value, update index and step values */
- delta |= sign;
-
- index += indexTable[delta];
- if ( index < 0 ) index = 0;
- if ( index > 88 ) index = 88;
- step = stepsizeTable[index];
-
- /* Step 6 - Output value */
- if ( bufferstep ) {
- outputbuffer = (delta << 4) & 0xf0;
- } else {
- *ncp++ = (delta & 0x0f) | outputbuffer;
- }
- bufferstep = !bufferstep;
+
+ /* Step 6 - Output value */
+ if ( bufferstep ) {
+ outputbuffer = (delta << 4) & 0xf0;
+ } else {
+ *ncp++ = (delta & 0x0f) | outputbuffer;
}
- rv = Py_BuildValue("(O(ii))", str, valpred, index);
- Py_DECREF(str);
- return rv;
+ bufferstep = !bufferstep;
+ }
+ rv = Py_BuildValue("(O(ii))", str, valpred, index);
+ Py_DECREF(str);
+ return rv;
}
static PyObject *
audioop_adpcm2lin(PyObject *self, PyObject *args)
{
- signed char *cp;
- signed char *ncp;
- int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
- PyObject *rv, *str, *state;
- int i, inputbuffer = 0, bufferstep;
-
- if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
- &cp, &len, &size, &state) )
- return 0;
-
- if ( size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
- return 0;
- }
-
- /* Decode state, should have (value, step) */
- if ( state == Py_None ) {
- /* First time, it seems. Set defaults */
- valpred = 0;
- step = 7;
- index = 0;
- } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
- return 0;
-
- new_len = len*size*2;
- if (new_len < 0) {
- PyErr_SetString(PyExc_MemoryError,
- "not enough memory for output buffer");
- return 0;
+ signed char *cp;
+ signed char *ncp;
+ int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
+ PyObject *rv, *str, *state;
+ int i, inputbuffer = 0, bufferstep;
+
+ if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
+ &cp, &len, &size, &state) )
+ return 0;
+
+ if ( size != 1 && size != 2 && size != 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ return 0;
+ }
+
+ /* Decode state, should have (value, step) */
+ if ( state == Py_None ) {
+ /* First time, it seems. Set defaults */
+ valpred = 0;
+ step = 7;
+ index = 0;
+ } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
+ return 0;
+
+ new_len = len*size*2;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_MemoryError,
+ "not enough memory for output buffer");
+ return 0;
+ }
+ str = PyBytes_FromStringAndSize(NULL, new_len);
+ if ( str == 0 )
+ return 0;
+ ncp = (signed char *)PyBytes_AsString(str);
+
+ step = stepsizeTable[index];
+ bufferstep = 0;
+
+ for ( i=0; i < new_len; i += size ) {
+ /* Step 1 - get the delta value and compute next index */
+ if ( bufferstep ) {
+ delta = inputbuffer & 0xf;
+ } else {
+ inputbuffer = *cp++;
+ delta = (inputbuffer >> 4) & 0xf;
}
- str = PyBytes_FromStringAndSize(NULL, new_len);
- if ( str == 0 )
- return 0;
- ncp = (signed char *)PyBytes_AsString(str);
+ bufferstep = !bufferstep;
+
+ /* Step 2 - Find new index value (for later) */
+ index += indexTable[delta];
+ if ( index < 0 ) index = 0;
+ if ( index > 88 ) index = 88;
+
+ /* Step 3 - Separate sign and magnitude */
+ sign = delta & 8;
+ delta = delta & 7;
+
+ /* Step 4 - Compute difference and new predicted value */
+ /*
+ ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
+ ** in adpcm_coder.
+ */
+ vpdiff = step >> 3;
+ if ( delta & 4 ) vpdiff += step;
+ if ( delta & 2 ) vpdiff += step>>1;
+ if ( delta & 1 ) vpdiff += step>>2;
+
+ if ( sign )
+ valpred -= vpdiff;
+ else
+ valpred += vpdiff;
+
+ /* Step 5 - clamp output value */
+ if ( valpred > 32767 )
+ valpred = 32767;
+ else if ( valpred < -32768 )
+ valpred = -32768;
+
+ /* Step 6 - Update step value */
step = stepsizeTable[index];
- bufferstep = 0;
-
- for ( i=0; i < new_len; i += size ) {
- /* Step 1 - get the delta value and compute next index */
- if ( bufferstep ) {
- delta = inputbuffer & 0xf;
- } else {
- inputbuffer = *cp++;
- delta = (inputbuffer >> 4) & 0xf;
- }
-
- bufferstep = !bufferstep;
-
- /* Step 2 - Find new index value (for later) */
- index += indexTable[delta];
- if ( index < 0 ) index = 0;
- if ( index > 88 ) index = 88;
-
- /* Step 3 - Separate sign and magnitude */
- sign = delta & 8;
- delta = delta & 7;
-
- /* Step 4 - Compute difference and new predicted value */
- /*
- ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
- ** in adpcm_coder.
- */
- vpdiff = step >> 3;
- if ( delta & 4 ) vpdiff += step;
- if ( delta & 2 ) vpdiff += step>>1;
- if ( delta & 1 ) vpdiff += step>>2;
-
- if ( sign )
- valpred -= vpdiff;
- else
- valpred += vpdiff;
-
- /* Step 5 - clamp output value */
- if ( valpred > 32767 )
- valpred = 32767;
- else if ( valpred < -32768 )
- valpred = -32768;
-
- /* Step 6 - Update step value */
- step = stepsizeTable[index];
-
- /* Step 6 - Output value */
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
- }
- rv = Py_BuildValue("(O(ii))", str, valpred, index);
- Py_DECREF(str);
- return rv;
+ /* Step 6 - Output value */
+ if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
+ else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
+ else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
+ }
+
+ rv = Py_BuildValue("(O(ii))", str, valpred, index);
+ Py_DECREF(str);
+ return rv;
}
static PyMethodDef audioop_methods[] = {
- { "max", audioop_max, METH_VARARGS },
- { "minmax", audioop_minmax, METH_VARARGS },
- { "avg", audioop_avg, METH_VARARGS },
- { "maxpp", audioop_maxpp, METH_VARARGS },
- { "avgpp", audioop_avgpp, METH_VARARGS },
- { "rms", audioop_rms, METH_VARARGS },
- { "findfit", audioop_findfit, METH_VARARGS },
- { "findmax", audioop_findmax, METH_VARARGS },
- { "findfactor", audioop_findfactor, METH_VARARGS },
- { "cross", audioop_cross, METH_VARARGS },
- { "mul", audioop_mul, METH_VARARGS },
- { "add", audioop_add, METH_VARARGS },
- { "bias", audioop_bias, METH_VARARGS },
- { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
- { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
- { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
- { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
- { "lin2lin", audioop_lin2lin, METH_VARARGS },
- { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
- { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
- { "tomono", audioop_tomono, METH_VARARGS },
- { "tostereo", audioop_tostereo, METH_VARARGS },
- { "getsample", audioop_getsample, METH_VARARGS },
- { "reverse", audioop_reverse, METH_VARARGS },
- { "ratecv", audioop_ratecv, METH_VARARGS },
- { 0, 0 }
+ { "max", audioop_max, METH_VARARGS },
+ { "minmax", audioop_minmax, METH_VARARGS },
+ { "avg", audioop_avg, METH_VARARGS },
+ { "maxpp", audioop_maxpp, METH_VARARGS },
+ { "avgpp", audioop_avgpp, METH_VARARGS },
+ { "rms", audioop_rms, METH_VARARGS },
+ { "findfit", audioop_findfit, METH_VARARGS },
+ { "findmax", audioop_findmax, METH_VARARGS },
+ { "findfactor", audioop_findfactor, METH_VARARGS },
+ { "cross", audioop_cross, METH_VARARGS },
+ { "mul", audioop_mul, METH_VARARGS },
+ { "add", audioop_add, METH_VARARGS },
+ { "bias", audioop_bias, METH_VARARGS },
+ { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
+ { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
+ { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
+ { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
+ { "lin2lin", audioop_lin2lin, METH_VARARGS },
+ { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
+ { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
+ { "tomono", audioop_tomono, METH_VARARGS },
+ { "tostereo", audioop_tostereo, METH_VARARGS },
+ { "getsample", audioop_getsample, METH_VARARGS },
+ { "reverse", audioop_reverse, METH_VARARGS },
+ { "ratecv", audioop_ratecv, METH_VARARGS },
+ { 0, 0 }
};
static struct PyModuleDef audioopmodule = {
- PyModuleDef_HEAD_INIT,
- "audioop",
- NULL,
- -1,
- audioop_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "audioop",
+ NULL,
+ -1,
+ audioop_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_audioop(void)
{
- PyObject *m, *d;
- m = PyModule_Create(&audioopmodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
- if (d == NULL)
- return NULL;
- AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
- if (AudioopError != NULL)
- PyDict_SetItemString(d,"error",AudioopError);
- return m;
+ PyObject *m, *d;
+ m = PyModule_Create(&audioopmodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+ if (d == NULL)
+ return NULL;
+ AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
+ if (AudioopError != NULL)
+ PyDict_SetItemString(d,"error",AudioopError);
+ return m;
}
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 833283f..e5418a4 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -3,40 +3,40 @@
**
** This module currently supports the following encodings:
** uuencode:
-** each line encodes 45 bytes (except possibly the last)
-** First char encodes (binary) length, rest data
-** each char encodes 6 bits, as follows:
-** binary: 01234567 abcdefgh ijklmnop
-** ascii: 012345 67abcd efghij klmnop
-** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc.
-** short binary data is zero-extended (so the bits are always in the
-** right place), this does *not* reflect in the length.
+** each line encodes 45 bytes (except possibly the last)
+** First char encodes (binary) length, rest data
+** each char encodes 6 bits, as follows:
+** binary: 01234567 abcdefgh ijklmnop
+** ascii: 012345 67abcd efghij klmnop
+** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc.
+** short binary data is zero-extended (so the bits are always in the
+** right place), this does *not* reflect in the length.
** base64:
** Line breaks are insignificant, but lines are at most 76 chars
** each char encodes 6 bits, in similar order as uucode/hqx. Encoding
** is done via a table.
** Short binary data is filled (in ASCII) with '='.
** hqx:
-** File starts with introductory text, real data starts and ends
-** with colons.
-** Data consists of three similar parts: info, datafork, resourcefork.
-** Each part is protected (at the end) with a 16-bit crc
-** The binary data is run-length encoded, and then ascii-fied:
-** binary: 01234567 abcdefgh ijklmnop
-** ascii: 012345 67abcd efghij klmnop
-** ASCII encoding is table-driven, see the code.
-** Short binary data results in the runt ascii-byte being output with
-** the bits in the right place.
+** File starts with introductory text, real data starts and ends
+** with colons.
+** Data consists of three similar parts: info, datafork, resourcefork.
+** Each part is protected (at the end) with a 16-bit crc
+** The binary data is run-length encoded, and then ascii-fied:
+** binary: 01234567 abcdefgh ijklmnop
+** ascii: 012345 67abcd efghij klmnop
+** ASCII encoding is table-driven, see the code.
+** Short binary data results in the runt ascii-byte being output with
+** the bits in the right place.
**
** While I was reading dozens of programs that encode or decode the formats
** here (documentation? hihi:-) I have formulated Jansen's Observation:
**
-** Programs that encode binary data in ASCII are written in
-** such a style that they are as unreadable as possible. Devices used
-** include unnecessary global variables, burying important tables
-** in unrelated sourcefiles, putting functions in include files,
-** using seemingly-descriptive variable names for different purposes,
-** calls to empty subroutines and a host of others.
+** Programs that encode binary data in ASCII are written in
+** such a style that they are as unreadable as possible. Devices used
+** include unnecessary global variables, burying important tables
+** in unrelated sourcefiles, putting functions in include files,
+** using seemingly-descriptive variable names for different purposes,
+** calls to empty subroutines and a host of others.
**
** I have attempted to break with this tradition, but I guess that that
** does make the performance sub-optimal. Oh well, too bad...
@@ -75,67 +75,67 @@ static PyObject *Incomplete;
static unsigned char table_a2b_hqx[256] = {
/* ^@ ^A ^B ^C ^D ^E ^F ^G */
-/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* \b \t \n ^K ^L \r ^N ^O */
-/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
+/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
/* ^P ^Q ^R ^S ^T ^U ^V ^W */
-/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
-/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* ! " # $ % & ' */
-/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
/* ( ) * + , - . / */
-/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
+/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
/* 0 1 2 3 4 5 6 7 */
-/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
+/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
/* 8 9 : ; < = > ? */
-/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
+/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
/* @ A B C D E F G */
-/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
+/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
/* H I J K L M N O */
-/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
+/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
/* P Q R S T U V W */
-/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
+/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
/* X Y Z [ \ ] ^ _ */
-/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
+/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
/* ` a b c d e f g */
-/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
+/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
/* h i j k l m n o */
-/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
+/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
/* p q r s t u v w */
-/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
+/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
/* x y z { | } ~ ^? */
-/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
-/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
- FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
+ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
};
static unsigned char table_b2a_hqx[] =
"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
static char table_a2b_base64[] = {
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
- -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
- 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
- -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
- 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
- -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
- 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
+ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+ -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
+ 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
+ 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
+ -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
+ 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};
#define BASE64_PAD '='
@@ -149,38 +149,38 @@ static unsigned char table_b2a_base64[] =
static unsigned short crctab_hqx[256] = {
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
- 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
- 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
- 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
- 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
- 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
- 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
- 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
- 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
- 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
- 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
- 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
- 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
- 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
- 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
- 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
- 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
- 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
- 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
- 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
- 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
- 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
- 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
- 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
- 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
- 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
- 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
- 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
- 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
- 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
- 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
- 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
+ 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
+ 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
+ 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
+ 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
+ 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
+ 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
+ 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
+ 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
+ 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
+ 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
+ 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
+ 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
+ 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
+ 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
+ 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
+ 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
+ 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
+ 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
+ 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
+ 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
+ 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
+ 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
+ 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
+ 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
+ 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
+ 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
+ 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
+ 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
+ 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
+ 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
+ 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
+ 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
};
PyDoc_STRVAR(doc_a2b_uu, "(ascii) -> bin. Decode a line of uuencoded data");
@@ -188,85 +188,85 @@ PyDoc_STRVAR(doc_a2b_uu, "(ascii) -> bin. Decode a line of uuencoded data");
static PyObject *
binascii_a2b_uu(PyObject *self, PyObject *args)
{
- Py_buffer pascii;
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t ascii_len, bin_len;
-
- if ( !PyArg_ParseTuple(args, "y*:a2b_uu", &pascii) )
- return NULL;
- ascii_data = pascii.buf;
- ascii_len = pascii.len;
-
- assert(ascii_len >= 0);
-
- /* First byte: binary data length (in bytes) */
- bin_len = (*ascii_data++ - ' ') & 077;
- ascii_len--;
-
- /* Allocate the buffer */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
- PyBuffer_Release(&pascii);
- return NULL;
- }
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
- /* XXX is it really best to add NULs if there's no more data */
- this_ch = (ascii_len > 0) ? *ascii_data : 0;
- if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
- /*
- ** Whitespace. Assume some spaces got eaten at
- ** end-of-line. (We check this later)
- */
- this_ch = 0;
- } else {
- /* Check the character for legality
- ** The 64 in stead of the expected 63 is because
- ** there are a few uuencodes out there that use
- ** '`' as zero instead of space.
- */
- if ( this_ch < ' ' || this_ch > (' ' + 64)) {
- PyErr_SetString(Error, "Illegal char");
- PyBuffer_Release(&pascii);
- Py_DECREF(rv);
- return NULL;
- }
- this_ch = (this_ch - ' ') & 077;
- }
- /*
- ** Shift it in on the low end, and see if there's
- ** a byte ready for output.
- */
- leftchar = (leftchar << 6) | (this_ch);
- leftbits += 6;
- if ( leftbits >= 8 ) {
- leftbits -= 8;
- *bin_data++ = (leftchar >> leftbits) & 0xff;
- leftchar &= ((1 << leftbits) - 1);
- bin_len--;
- }
- }
- /*
- ** Finally, check that if there's anything left on the line
- ** that it's whitespace only.
- */
- while( ascii_len-- > 0 ) {
- this_ch = *ascii_data++;
- /* Extra '`' may be written as padding in some cases */
- if ( this_ch != ' ' && this_ch != ' '+64 &&
- this_ch != '\n' && this_ch != '\r' ) {
- PyErr_SetString(Error, "Trailing garbage");
- PyBuffer_Release(&pascii);
- Py_DECREF(rv);
- return NULL;
- }
- }
- PyBuffer_Release(&pascii);
- return rv;
+ Py_buffer pascii;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t ascii_len, bin_len;
+
+ if ( !PyArg_ParseTuple(args, "y*:a2b_uu", &pascii) )
+ return NULL;
+ ascii_data = pascii.buf;
+ ascii_len = pascii.len;
+
+ assert(ascii_len >= 0);
+
+ /* First byte: binary data length (in bytes) */
+ bin_len = (*ascii_data++ - ' ') & 077;
+ ascii_len--;
+
+ /* Allocate the buffer */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
+ PyBuffer_Release(&pascii);
+ return NULL;
+ }
+ bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
+ /* XXX is it really best to add NULs if there's no more data */
+ this_ch = (ascii_len > 0) ? *ascii_data : 0;
+ if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
+ /*
+ ** Whitespace. Assume some spaces got eaten at
+ ** end-of-line. (We check this later)
+ */
+ this_ch = 0;
+ } else {
+ /* Check the character for legality
+ ** The 64 in stead of the expected 63 is because
+ ** there are a few uuencodes out there that use
+ ** '`' as zero instead of space.
+ */
+ if ( this_ch < ' ' || this_ch > (' ' + 64)) {
+ PyErr_SetString(Error, "Illegal char");
+ PyBuffer_Release(&pascii);
+ Py_DECREF(rv);
+ return NULL;
+ }
+ this_ch = (this_ch - ' ') & 077;
+ }
+ /*
+ ** Shift it in on the low end, and see if there's
+ ** a byte ready for output.
+ */
+ leftchar = (leftchar << 6) | (this_ch);
+ leftbits += 6;
+ if ( leftbits >= 8 ) {
+ leftbits -= 8;
+ *bin_data++ = (leftchar >> leftbits) & 0xff;
+ leftchar &= ((1 << leftbits) - 1);
+ bin_len--;
+ }
+ }
+ /*
+ ** Finally, check that if there's anything left on the line
+ ** that it's whitespace only.
+ */
+ while( ascii_len-- > 0 ) {
+ this_ch = *ascii_data++;
+ /* Extra '`' may be written as padding in some cases */
+ if ( this_ch != ' ' && this_ch != ' '+64 &&
+ this_ch != '\n' && this_ch != '\r' ) {
+ PyErr_SetString(Error, "Trailing garbage");
+ PyBuffer_Release(&pascii);
+ Py_DECREF(rv);
+ return NULL;
+ }
+ }
+ PyBuffer_Release(&pascii);
+ return rv;
}
PyDoc_STRVAR(doc_b2a_uu, "(bin) -> ascii. Uuencode line of data");
@@ -274,86 +274,86 @@ PyDoc_STRVAR(doc_b2a_uu, "(bin) -> ascii. Uuencode line of data");
static PyObject *
binascii_b2a_uu(PyObject *self, PyObject *args)
{
- Py_buffer pbin;
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t bin_len;
-
- if ( !PyArg_ParseTuple(args, "y*:b2a_uu", &pbin) )
- return NULL;
- bin_data = pbin.buf;
- bin_len = pbin.len;
- if ( bin_len > 45 ) {
- /* The 45 is a limit that appears in all uuencode's */
- PyErr_SetString(Error, "At most 45 bytes at once");
- PyBuffer_Release(&pbin);
- return NULL;
- }
-
- /* We're lazy and allocate to much (fixed up later) */
- if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL ) {
- PyBuffer_Release(&pbin);
- return NULL;
- }
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- /* Store the length */
- *ascii_data++ = ' ' + (bin_len & 077);
-
- for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
- /* Shift the data (or padding) into our buffer */
- if ( bin_len > 0 ) /* Data */
- leftchar = (leftchar << 8) | *bin_data;
- else /* Padding */
- leftchar <<= 8;
- leftbits += 8;
-
- /* See if there are 6-bit groups ready */
- while ( leftbits >= 6 ) {
- this_ch = (leftchar >> (leftbits-6)) & 0x3f;
- leftbits -= 6;
- *ascii_data++ = this_ch + ' ';
- }
- }
- *ascii_data++ = '\n'; /* Append a courtesy newline */
-
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- PyBuffer_Release(&pbin);
- return rv;
+ Py_buffer pbin;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t bin_len;
+
+ if ( !PyArg_ParseTuple(args, "y*:b2a_uu", &pbin) )
+ return NULL;
+ bin_data = pbin.buf;
+ bin_len = pbin.len;
+ if ( bin_len > 45 ) {
+ /* The 45 is a limit that appears in all uuencode's */
+ PyErr_SetString(Error, "At most 45 bytes at once");
+ PyBuffer_Release(&pbin);
+ return NULL;
+ }
+
+ /* We're lazy and allocate to much (fixed up later) */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL ) {
+ PyBuffer_Release(&pbin);
+ return NULL;
+ }
+ ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ /* Store the length */
+ *ascii_data++ = ' ' + (bin_len & 077);
+
+ for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
+ /* Shift the data (or padding) into our buffer */
+ if ( bin_len > 0 ) /* Data */
+ leftchar = (leftchar << 8) | *bin_data;
+ else /* Padding */
+ leftchar <<= 8;
+ leftbits += 8;
+
+ /* See if there are 6-bit groups ready */
+ while ( leftbits >= 6 ) {
+ this_ch = (leftchar >> (leftbits-6)) & 0x3f;
+ leftbits -= 6;
+ *ascii_data++ = this_ch + ' ';
+ }
+ }
+ *ascii_data++ = '\n'; /* Append a courtesy newline */
+
+ if (_PyBytes_Resize(&rv,
+ (ascii_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ PyBuffer_Release(&pbin);
+ return rv;
}
static int
binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
{
- /* Finds & returns the (num+1)th
- ** valid character for base64, or -1 if none.
- */
-
- int ret = -1;
- unsigned char c, b64val;
-
- while ((slen > 0) && (ret == -1)) {
- c = *s;
- b64val = table_a2b_base64[c & 0x7f];
- if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
- if (num == 0)
- ret = *s;
- num--;
- }
-
- s++;
- slen--;
- }
- return ret;
+ /* Finds & returns the (num+1)th
+ ** valid character for base64, or -1 if none.
+ */
+
+ int ret = -1;
+ unsigned char c, b64val;
+
+ while ((slen > 0) && (ret == -1)) {
+ c = *s;
+ b64val = table_a2b_base64[c & 0x7f];
+ if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
+ if (num == 0)
+ ret = *s;
+ num--;
+ }
+
+ s++;
+ slen--;
+ }
+ return ret;
}
PyDoc_STRVAR(doc_a2b_base64, "(ascii) -> bin. Decode a line of base64 data");
@@ -361,108 +361,108 @@ PyDoc_STRVAR(doc_a2b_base64, "(ascii) -> bin. Decode a line of base64 data");
static PyObject *
binascii_a2b_base64(PyObject *self, PyObject *args)
{
- Py_buffer pascii;
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t ascii_len, bin_len;
- int quad_pos = 0;
-
- if ( !PyArg_ParseTuple(args, "y*:a2b_base64", &pascii) )
- return NULL;
- ascii_data = pascii.buf;
- ascii_len = pascii.len;
-
- assert(ascii_len >= 0);
-
- if (ascii_len > PY_SSIZE_T_MAX - 3) {
- PyBuffer_Release(&pascii);
- return PyErr_NoMemory();
- }
-
- bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
-
- /* Allocate the buffer */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
- PyBuffer_Release(&pascii);
- return NULL;
- }
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
- bin_len = 0;
-
- for( ; ascii_len > 0; ascii_len--, ascii_data++) {
- this_ch = *ascii_data;
-
- if (this_ch > 0x7f ||
- this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
- continue;
-
- /* Check for pad sequences and ignore
- ** the invalid ones.
- */
- if (this_ch == BASE64_PAD) {
- if ( (quad_pos < 2) ||
- ((quad_pos == 2) &&
- (binascii_find_valid(ascii_data, ascii_len, 1)
- != BASE64_PAD)) )
- {
- continue;
- }
- else {
- /* A pad sequence means no more input.
- ** We've already interpreted the data
- ** from the quad at this point.
- */
- leftbits = 0;
- break;
- }
- }
-
- this_ch = table_a2b_base64[*ascii_data];
- if ( this_ch == (unsigned char) -1 )
- continue;
-
- /*
- ** Shift it in on the low end, and see if there's
- ** a byte ready for output.
- */
- quad_pos = (quad_pos + 1) & 0x03;
- leftchar = (leftchar << 6) | (this_ch);
- leftbits += 6;
-
- if ( leftbits >= 8 ) {
- leftbits -= 8;
- *bin_data++ = (leftchar >> leftbits) & 0xff;
- bin_len++;
- leftchar &= ((1 << leftbits) - 1);
- }
- }
-
- if (leftbits != 0) {
- PyBuffer_Release(&pascii);
- PyErr_SetString(Error, "Incorrect padding");
- Py_DECREF(rv);
- return NULL;
- }
-
- /* And set string size correctly. If the result string is empty
- ** (because the input was all invalid) return the shared empty
- ** string instead; _PyBytes_Resize() won't do this for us.
- */
- if (bin_len > 0) {
- if (_PyBytes_Resize(&rv, bin_len) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- }
- else {
- Py_DECREF(rv);
- rv = PyBytes_FromStringAndSize("", 0);
- }
- PyBuffer_Release(&pascii);
- return rv;
+ Py_buffer pascii;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t ascii_len, bin_len;
+ int quad_pos = 0;
+
+ if ( !PyArg_ParseTuple(args, "y*:a2b_base64", &pascii) )
+ return NULL;
+ ascii_data = pascii.buf;
+ ascii_len = pascii.len;
+
+ assert(ascii_len >= 0);
+
+ if (ascii_len > PY_SSIZE_T_MAX - 3) {
+ PyBuffer_Release(&pascii);
+ return PyErr_NoMemory();
+ }
+
+ bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
+
+ /* Allocate the buffer */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
+ PyBuffer_Release(&pascii);
+ return NULL;
+ }
+ bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+ bin_len = 0;
+
+ for( ; ascii_len > 0; ascii_len--, ascii_data++) {
+ this_ch = *ascii_data;
+
+ if (this_ch > 0x7f ||
+ this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
+ continue;
+
+ /* Check for pad sequences and ignore
+ ** the invalid ones.
+ */
+ if (this_ch == BASE64_PAD) {
+ if ( (quad_pos < 2) ||
+ ((quad_pos == 2) &&
+ (binascii_find_valid(ascii_data, ascii_len, 1)
+ != BASE64_PAD)) )
+ {
+ continue;
+ }
+ else {
+ /* A pad sequence means no more input.
+ ** We've already interpreted the data
+ ** from the quad at this point.
+ */
+ leftbits = 0;
+ break;
+ }
+ }
+
+ this_ch = table_a2b_base64[*ascii_data];
+ if ( this_ch == (unsigned char) -1 )
+ continue;
+
+ /*
+ ** Shift it in on the low end, and see if there's
+ ** a byte ready for output.
+ */
+ quad_pos = (quad_pos + 1) & 0x03;
+ leftchar = (leftchar << 6) | (this_ch);
+ leftbits += 6;
+
+ if ( leftbits >= 8 ) {
+ leftbits -= 8;
+ *bin_data++ = (leftchar >> leftbits) & 0xff;
+ bin_len++;
+ leftchar &= ((1 << leftbits) - 1);
+ }
+ }
+
+ if (leftbits != 0) {
+ PyBuffer_Release(&pascii);
+ PyErr_SetString(Error, "Incorrect padding");
+ Py_DECREF(rv);
+ return NULL;
+ }
+
+ /* And set string size correctly. If the result string is empty
+ ** (because the input was all invalid) return the shared empty
+ ** string instead; _PyBytes_Resize() won't do this for us.
+ */
+ if (bin_len > 0) {
+ if (_PyBytes_Resize(&rv, bin_len) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ }
+ else {
+ Py_DECREF(rv);
+ rv = PyBytes_FromStringAndSize("", 0);
+ }
+ PyBuffer_Release(&pascii);
+ return rv;
}
PyDoc_STRVAR(doc_b2a_base64, "(bin) -> ascii. Base64-code line of data");
@@ -470,66 +470,66 @@ PyDoc_STRVAR(doc_b2a_base64, "(bin) -> ascii. Base64-code line of data");
static PyObject *
binascii_b2a_base64(PyObject *self, PyObject *args)
{
- Py_buffer pbuf;
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t bin_len;
-
- if ( !PyArg_ParseTuple(args, "y*:b2a_base64", &pbuf) )
- return NULL;
- bin_data = pbuf.buf;
- bin_len = pbuf.len;
-
- assert(bin_len >= 0);
-
- if ( bin_len > BASE64_MAXBIN ) {
- PyErr_SetString(Error, "Too much data for base64 line");
- PyBuffer_Release(&pbuf);
- return NULL;
- }
-
- /* We're lazy and allocate too much (fixed up later).
- "+3" leaves room for up to two pad characters and a trailing
- newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) {
- PyBuffer_Release(&pbuf);
- return NULL;
- }
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
- /* Shift the data into our buffer */
- leftchar = (leftchar << 8) | *bin_data;
- leftbits += 8;
-
- /* See if there are 6-bit groups ready */
- while ( leftbits >= 6 ) {
- this_ch = (leftchar >> (leftbits-6)) & 0x3f;
- leftbits -= 6;
- *ascii_data++ = table_b2a_base64[this_ch];
- }
- }
- if ( leftbits == 2 ) {
- *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
- *ascii_data++ = BASE64_PAD;
- *ascii_data++ = BASE64_PAD;
- } else if ( leftbits == 4 ) {
- *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
- *ascii_data++ = BASE64_PAD;
- }
- *ascii_data++ = '\n'; /* Append a courtesy newline */
-
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- PyBuffer_Release(&pbuf);
- return rv;
+ Py_buffer pbuf;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t bin_len;
+
+ if ( !PyArg_ParseTuple(args, "y*:b2a_base64", &pbuf) )
+ return NULL;
+ bin_data = pbuf.buf;
+ bin_len = pbuf.len;
+
+ assert(bin_len >= 0);
+
+ if ( bin_len > BASE64_MAXBIN ) {
+ PyErr_SetString(Error, "Too much data for base64 line");
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
+
+ /* We're lazy and allocate too much (fixed up later).
+ "+3" leaves room for up to two pad characters and a trailing
+ newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) {
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
+ ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
+ /* Shift the data into our buffer */
+ leftchar = (leftchar << 8) | *bin_data;
+ leftbits += 8;
+
+ /* See if there are 6-bit groups ready */
+ while ( leftbits >= 6 ) {
+ this_ch = (leftchar >> (leftbits-6)) & 0x3f;
+ leftbits -= 6;
+ *ascii_data++ = table_b2a_base64[this_ch];
+ }
+ }
+ if ( leftbits == 2 ) {
+ *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
+ *ascii_data++ = BASE64_PAD;
+ *ascii_data++ = BASE64_PAD;
+ } else if ( leftbits == 4 ) {
+ *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
+ *ascii_data++ = BASE64_PAD;
+ }
+ *ascii_data++ = '\n'; /* Append a courtesy newline */
+
+ if (_PyBytes_Resize(&rv,
+ (ascii_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ PyBuffer_Release(&pbuf);
+ return rv;
}
PyDoc_STRVAR(doc_a2b_hqx, "ascii -> bin, done. Decode .hqx coding");
@@ -537,74 +537,74 @@ PyDoc_STRVAR(doc_a2b_hqx, "ascii -> bin, done. Decode .hqx coding");
static PyObject *
binascii_a2b_hqx(PyObject *self, PyObject *args)
{
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t len;
- int done = 0;
-
- if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
- return NULL;
-
- assert(len >= 0);
-
- if (len > PY_SSIZE_T_MAX - 2)
- return PyErr_NoMemory();
-
- /* Allocate a string that is too big (fixed later)
- Add two to the initial length to prevent interning which
- would preclude subsequent resizing. */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
- return NULL;
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- for( ; len > 0 ; len--, ascii_data++ ) {
- /* Get the byte and look it up */
- this_ch = table_a2b_hqx[*ascii_data];
- if ( this_ch == SKIP )
- continue;
- if ( this_ch == FAIL ) {
- PyErr_SetString(Error, "Illegal char");
- Py_DECREF(rv);
- return NULL;
- }
- if ( this_ch == DONE ) {
- /* The terminating colon */
- done = 1;
- break;
- }
-
- /* Shift it into the buffer and see if any bytes are ready */
- leftchar = (leftchar << 6) | (this_ch);
- leftbits += 6;
- if ( leftbits >= 8 ) {
- leftbits -= 8;
- *bin_data++ = (leftchar >> leftbits) & 0xff;
- leftchar &= ((1 << leftbits) - 1);
- }
- }
-
- if ( leftbits && !done ) {
- PyErr_SetString(Incomplete,
- "String has incomplete number of bytes");
- Py_DECREF(rv);
- return NULL;
- }
- if (_PyBytes_Resize(&rv,
- (bin_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- if (rv) {
- PyObject *rrv = Py_BuildValue("Oi", rv, done);
- Py_DECREF(rv);
- return rrv;
- }
-
- return NULL;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t len;
+ int done = 0;
+
+ if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
+ return NULL;
+
+ assert(len >= 0);
+
+ if (len > PY_SSIZE_T_MAX - 2)
+ return PyErr_NoMemory();
+
+ /* Allocate a string that is too big (fixed later)
+ Add two to the initial length to prevent interning which
+ would preclude subsequent resizing. */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
+ return NULL;
+ bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ for( ; len > 0 ; len--, ascii_data++ ) {
+ /* Get the byte and look it up */
+ this_ch = table_a2b_hqx[*ascii_data];
+ if ( this_ch == SKIP )
+ continue;
+ if ( this_ch == FAIL ) {
+ PyErr_SetString(Error, "Illegal char");
+ Py_DECREF(rv);
+ return NULL;
+ }
+ if ( this_ch == DONE ) {
+ /* The terminating colon */
+ done = 1;
+ break;
+ }
+
+ /* Shift it into the buffer and see if any bytes are ready */
+ leftchar = (leftchar << 6) | (this_ch);
+ leftbits += 6;
+ if ( leftbits >= 8 ) {
+ leftbits -= 8;
+ *bin_data++ = (leftchar >> leftbits) & 0xff;
+ leftchar &= ((1 << leftbits) - 1);
+ }
+ }
+
+ if ( leftbits && !done ) {
+ PyErr_SetString(Incomplete,
+ "String has incomplete number of bytes");
+ Py_DECREF(rv);
+ return NULL;
+ }
+ if (_PyBytes_Resize(&rv,
+ (bin_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ if (rv) {
+ PyObject *rrv = Py_BuildValue("Oi", rv, done);
+ Py_DECREF(rv);
+ return rrv;
+ }
+
+ return NULL;
}
PyDoc_STRVAR(doc_rlecode_hqx, "Binhex RLE-code binary data");
@@ -612,63 +612,63 @@ PyDoc_STRVAR(doc_rlecode_hqx, "Binhex RLE-code binary data");
static PyObject *
binascii_rlecode_hqx(PyObject *self, PyObject *args)
{
- Py_buffer pbuf;
- unsigned char *in_data, *out_data;
- PyObject *rv;
- unsigned char ch;
- Py_ssize_t in, inend, len;
-
- if ( !PyArg_ParseTuple(args, "y*:rlecode_hqx", &pbuf) )
- return NULL;
- in_data = pbuf.buf;
- len = pbuf.len;
-
- assert(len >= 0);
-
- if (len > PY_SSIZE_T_MAX / 2 - 2) {
- PyBuffer_Release(&pbuf);
- return PyErr_NoMemory();
- }
-
- /* Worst case: output is twice as big as input (fixed later) */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
- PyBuffer_Release(&pbuf);
- return NULL;
- }
- out_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- for( in=0; in<len; in++) {
- ch = in_data[in];
- if ( ch == RUNCHAR ) {
- /* RUNCHAR. Escape it. */
- *out_data++ = RUNCHAR;
- *out_data++ = 0;
- } else {
- /* Check how many following are the same */
- for(inend=in+1;
- inend<len && in_data[inend] == ch &&
- inend < in+255;
- inend++) ;
- if ( inend - in > 3 ) {
- /* More than 3 in a row. Output RLE. */
- *out_data++ = ch;
- *out_data++ = RUNCHAR;
- *out_data++ = inend-in;
- in = inend-1;
- } else {
- /* Less than 3. Output the byte itself */
- *out_data++ = ch;
- }
- }
- }
- if (_PyBytes_Resize(&rv,
- (out_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- PyBuffer_Release(&pbuf);
- return rv;
+ Py_buffer pbuf;
+ unsigned char *in_data, *out_data;
+ PyObject *rv;
+ unsigned char ch;
+ Py_ssize_t in, inend, len;
+
+ if ( !PyArg_ParseTuple(args, "y*:rlecode_hqx", &pbuf) )
+ return NULL;
+ in_data = pbuf.buf;
+ len = pbuf.len;
+
+ assert(len >= 0);
+
+ if (len > PY_SSIZE_T_MAX / 2 - 2) {
+ PyBuffer_Release(&pbuf);
+ return PyErr_NoMemory();
+ }
+
+ /* Worst case: output is twice as big as input (fixed later) */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
+ out_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ for( in=0; in<len; in++) {
+ ch = in_data[in];
+ if ( ch == RUNCHAR ) {
+ /* RUNCHAR. Escape it. */
+ *out_data++ = RUNCHAR;
+ *out_data++ = 0;
+ } else {
+ /* Check how many following are the same */
+ for(inend=in+1;
+ inend<len && in_data[inend] == ch &&
+ inend < in+255;
+ inend++) ;
+ if ( inend - in > 3 ) {
+ /* More than 3 in a row. Output RLE. */
+ *out_data++ = ch;
+ *out_data++ = RUNCHAR;
+ *out_data++ = inend-in;
+ in = inend-1;
+ } else {
+ /* Less than 3. Output the byte itself */
+ *out_data++ = ch;
+ }
+ }
+ }
+ if (_PyBytes_Resize(&rv,
+ (out_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ PyBuffer_Release(&pbuf);
+ return rv;
}
PyDoc_STRVAR(doc_b2a_hqx, "Encode .hqx data");
@@ -676,56 +676,56 @@ PyDoc_STRVAR(doc_b2a_hqx, "Encode .hqx data");
static PyObject *
binascii_b2a_hqx(PyObject *self, PyObject *args)
{
- Py_buffer pbin;
- unsigned char *ascii_data, *bin_data;
- int leftbits = 0;
- unsigned char this_ch;
- unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t len;
-
- if ( !PyArg_ParseTuple(args, "y*:b2a_hqx", &pbin) )
- return NULL;
- bin_data = pbin.buf;
- len = pbin.len;
-
- assert(len >= 0);
-
- if (len > PY_SSIZE_T_MAX / 2 - 2) {
- PyBuffer_Release(&pbin);
- return PyErr_NoMemory();
- }
-
- /* Allocate a buffer that is at least large enough */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
- PyBuffer_Release(&pbin);
- return NULL;
- }
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- for( ; len > 0 ; len--, bin_data++ ) {
- /* Shift into our buffer, and output any 6bits ready */
- leftchar = (leftchar << 8) | *bin_data;
- leftbits += 8;
- while ( leftbits >= 6 ) {
- this_ch = (leftchar >> (leftbits-6)) & 0x3f;
- leftbits -= 6;
- *ascii_data++ = table_b2a_hqx[this_ch];
- }
- }
- /* Output a possible runt byte */
- if ( leftbits ) {
- leftchar <<= (6-leftbits);
- *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
- }
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- PyBuffer_Release(&pbin);
- return rv;
+ Py_buffer pbin;
+ unsigned char *ascii_data, *bin_data;
+ int leftbits = 0;
+ unsigned char this_ch;
+ unsigned int leftchar = 0;
+ PyObject *rv;
+ Py_ssize_t len;
+
+ if ( !PyArg_ParseTuple(args, "y*:b2a_hqx", &pbin) )
+ return NULL;
+ bin_data = pbin.buf;
+ len = pbin.len;
+
+ assert(len >= 0);
+
+ if (len > PY_SSIZE_T_MAX / 2 - 2) {
+ PyBuffer_Release(&pbin);
+ return PyErr_NoMemory();
+ }
+
+ /* Allocate a buffer that is at least large enough */
+ if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
+ PyBuffer_Release(&pbin);
+ return NULL;
+ }
+ ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ for( ; len > 0 ; len--, bin_data++ ) {
+ /* Shift into our buffer, and output any 6bits ready */
+ leftchar = (leftchar << 8) | *bin_data;
+ leftbits += 8;
+ while ( leftbits >= 6 ) {
+ this_ch = (leftchar >> (leftbits-6)) & 0x3f;
+ leftbits -= 6;
+ *ascii_data++ = table_b2a_hqx[this_ch];
+ }
+ }
+ /* Output a possible runt byte */
+ if ( leftbits ) {
+ leftchar <<= (6-leftbits);
+ *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
+ }
+ if (_PyBytes_Resize(&rv,
+ (ascii_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ PyBuffer_Release(&pbin);
+ return rv;
}
PyDoc_STRVAR(doc_rledecode_hqx, "Decode hexbin RLE-coded string");
@@ -733,116 +733,116 @@ PyDoc_STRVAR(doc_rledecode_hqx, "Decode hexbin RLE-coded string");
static PyObject *
binascii_rledecode_hqx(PyObject *self, PyObject *args)
{
- Py_buffer pin;
- unsigned char *in_data, *out_data;
- unsigned char in_byte, in_repeat;
- PyObject *rv;
- Py_ssize_t in_len, out_len, out_len_left;
-
- if ( !PyArg_ParseTuple(args, "s*:rledecode_hqx", &pin) )
- return NULL;
- in_data = pin.buf;
- in_len = pin.len;
-
- assert(in_len >= 0);
-
- /* Empty string is a special case */
- if ( in_len == 0 ) {
- PyBuffer_Release(&pin);
- return PyBytes_FromStringAndSize("", 0);
- }
- else if (in_len > PY_SSIZE_T_MAX / 2) {
- PyBuffer_Release(&pin);
- return PyErr_NoMemory();
- }
-
- /* Allocate a buffer of reasonable size. Resized when needed */
- out_len = in_len*2;
- if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) {
- PyBuffer_Release(&pin);
- return NULL;
- }
- out_len_left = out_len;
- out_data = (unsigned char *)PyBytes_AS_STRING(rv);
-
- /*
- ** We need two macros here to get/put bytes and handle
- ** end-of-buffer for input and output strings.
- */
+ Py_buffer pin;
+ unsigned char *in_data, *out_data;
+ unsigned char in_byte, in_repeat;
+ PyObject *rv;
+ Py_ssize_t in_len, out_len, out_len_left;
+
+ if ( !PyArg_ParseTuple(args, "s*:rledecode_hqx", &pin) )
+ return NULL;
+ in_data = pin.buf;
+ in_len = pin.len;
+
+ assert(in_len >= 0);
+
+ /* Empty string is a special case */
+ if ( in_len == 0 ) {
+ PyBuffer_Release(&pin);
+ return PyBytes_FromStringAndSize("", 0);
+ }
+ else if (in_len > PY_SSIZE_T_MAX / 2) {
+ PyBuffer_Release(&pin);
+ return PyErr_NoMemory();
+ }
+
+ /* Allocate a buffer of reasonable size. Resized when needed */
+ out_len = in_len*2;
+ if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) {
+ PyBuffer_Release(&pin);
+ return NULL;
+ }
+ out_len_left = out_len;
+ out_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ /*
+ ** We need two macros here to get/put bytes and handle
+ ** end-of-buffer for input and output strings.
+ */
#define INBYTE(b) \
- do { \
- if ( --in_len < 0 ) { \
- PyErr_SetString(Incomplete, ""); \
- Py_DECREF(rv); \
- PyBuffer_Release(&pin); \
- return NULL; \
- } \
- b = *in_data++; \
- } while(0)
+ do { \
+ if ( --in_len < 0 ) { \
+ PyErr_SetString(Incomplete, ""); \
+ Py_DECREF(rv); \
+ PyBuffer_Release(&pin); \
+ return NULL; \
+ } \
+ b = *in_data++; \
+ } while(0)
#define OUTBYTE(b) \
- do { \
- if ( --out_len_left < 0 ) { \
- if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
- if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
- { Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
- out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
- + out_len; \
- out_len_left = out_len-1; \
- out_len = out_len * 2; \
- } \
- *out_data++ = b; \
- } while(0)
-
- /*
- ** Handle first byte separately (since we have to get angry
- ** in case of an orphaned RLE code).
- */
- INBYTE(in_byte);
-
- if (in_byte == RUNCHAR) {
- INBYTE(in_repeat);
- if (in_repeat != 0) {
- /* Note Error, not Incomplete (which is at the end
- ** of the string only). This is a programmer error.
- */
- PyErr_SetString(Error, "Orphaned RLE code at start");
- PyBuffer_Release(&pin);
- Py_DECREF(rv);
- return NULL;
- }
- OUTBYTE(RUNCHAR);
- } else {
- OUTBYTE(in_byte);
- }
-
- while( in_len > 0 ) {
- INBYTE(in_byte);
-
- if (in_byte == RUNCHAR) {
- INBYTE(in_repeat);
- if ( in_repeat == 0 ) {
- /* Just an escaped RUNCHAR value */
- OUTBYTE(RUNCHAR);
- } else {
- /* Pick up value and output a sequence of it */
- in_byte = out_data[-1];
- while ( --in_repeat > 0 )
- OUTBYTE(in_byte);
- }
- } else {
- /* Normal byte */
- OUTBYTE(in_byte);
- }
- }
- if (_PyBytes_Resize(&rv,
- (out_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_DECREF(rv);
- rv = NULL;
- }
- PyBuffer_Release(&pin);
- return rv;
+ do { \
+ if ( --out_len_left < 0 ) { \
+ if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
+ if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
+ { Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
+ out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
+ + out_len; \
+ out_len_left = out_len-1; \
+ out_len = out_len * 2; \
+ } \
+ *out_data++ = b; \
+ } while(0)
+
+ /*
+ ** Handle first byte separately (since we have to get angry
+ ** in case of an orphaned RLE code).
+ */
+ INBYTE(in_byte);
+
+ if (in_byte == RUNCHAR) {
+ INBYTE(in_repeat);
+ if (in_repeat != 0) {
+ /* Note Error, not Incomplete (which is at the end
+ ** of the string only). This is a programmer error.
+ */
+ PyErr_SetString(Error, "Orphaned RLE code at start");
+ PyBuffer_Release(&pin);
+ Py_DECREF(rv);
+ return NULL;
+ }
+ OUTBYTE(RUNCHAR);
+ } else {
+ OUTBYTE(in_byte);
+ }
+
+ while( in_len > 0 ) {
+ INBYTE(in_byte);
+
+ if (in_byte == RUNCHAR) {
+ INBYTE(in_repeat);
+ if ( in_repeat == 0 ) {
+ /* Just an escaped RUNCHAR value */
+ OUTBYTE(RUNCHAR);
+ } else {
+ /* Pick up value and output a sequence of it */
+ in_byte = out_data[-1];
+ while ( --in_repeat > 0 )
+ OUTBYTE(in_byte);
+ }
+ } else {
+ /* Normal byte */
+ OUTBYTE(in_byte);
+ }
+ }
+ if (_PyBytes_Resize(&rv,
+ (out_data -
+ (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
+ Py_DECREF(rv);
+ rv = NULL;
+ }
+ PyBuffer_Release(&pin);
+ return rv;
}
PyDoc_STRVAR(doc_crc_hqx,
@@ -851,22 +851,22 @@ PyDoc_STRVAR(doc_crc_hqx,
static PyObject *
binascii_crc_hqx(PyObject *self, PyObject *args)
{
- Py_buffer pin;
- unsigned char *bin_data;
- unsigned int crc;
- Py_ssize_t len;
-
- if ( !PyArg_ParseTuple(args, "y*i:crc_hqx", &pin, &crc) )
- return NULL;
- bin_data = pin.buf;
- len = pin.len;
-
- while(len-- > 0) {
- crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
- }
-
- PyBuffer_Release(&pin);
- return Py_BuildValue("i", crc);
+ Py_buffer pin;
+ unsigned char *bin_data;
+ unsigned int crc;
+ Py_ssize_t len;
+
+ if ( !PyArg_ParseTuple(args, "y*i:crc_hqx", &pin, &crc) )
+ return NULL;
+ bin_data = pin.buf;
+ len = pin.len;
+
+ while(len-- > 0) {
+ crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
+ }
+
+ PyBuffer_Release(&pin);
+ return Py_BuildValue("i", crc);
}
PyDoc_STRVAR(doc_crc32,
@@ -884,7 +884,7 @@ binascii_crc32(PyObject *self, PyObject *args)
int signed_val;
if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
- return NULL;
+ return NULL;
buf = (Byte*)pbuf.buf;
len = pbuf.len;
signed_val = crc32(crc32val, buf, len);
@@ -1013,26 +1013,26 @@ static unsigned int crc_32_tab[256] = {
static PyObject *
binascii_crc32(PyObject *self, PyObject *args)
{ /* By Jim Ahlstrom; All rights transferred to CNRI */
- Py_buffer pbin;
- unsigned char *bin_data;
- unsigned int crc = 0; /* initial value of CRC */
- Py_ssize_t len;
- unsigned int result;
-
- if ( !PyArg_ParseTuple(args, "y*|I:crc32", &pbin, &crc) )
- return NULL;
- bin_data = pbin.buf;
- len = pbin.len;
-
- crc = ~ crc;
- while (len-- > 0) {
- crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
- /* Note: (crc >> 8) MUST zero fill on left */
- }
-
- result = (crc ^ 0xFFFFFFFF);
- PyBuffer_Release(&pbin);
- return PyLong_FromUnsignedLong(result & 0xffffffff);
+ Py_buffer pbin;
+ unsigned char *bin_data;
+ unsigned int crc = 0; /* initial value of CRC */
+ Py_ssize_t len;
+ unsigned int result;
+
+ if ( !PyArg_ParseTuple(args, "y*|I:crc32", &pbin, &crc) )
+ return NULL;
+ bin_data = pbin.buf;
+ len = pbin.len;
+
+ crc = ~ crc;
+ while (len-- > 0) {
+ crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
+ /* Note: (crc >> 8) MUST zero fill on left */
+ }
+
+ result = (crc ^ 0xFFFFFFFF);
+ PyBuffer_Release(&pbin);
+ return PyLong_FromUnsignedLong(result & 0xffffffff);
}
#endif /* USE_ZLIB_CRC32 */
@@ -1040,43 +1040,43 @@ binascii_crc32(PyObject *self, PyObject *args)
static PyObject *
binascii_hexlify(PyObject *self, PyObject *args)
{
- Py_buffer parg;
- char* argbuf;
- Py_ssize_t arglen;
- PyObject *retval;
- char* retbuf;
- Py_ssize_t i, j;
-
- if (!PyArg_ParseTuple(args, "y*:b2a_hex", &parg))
- return NULL;
- argbuf = parg.buf;
- arglen = parg.len;
-
- assert(arglen >= 0);
- if (arglen > PY_SSIZE_T_MAX / 2) {
- PyBuffer_Release(&parg);
- return PyErr_NoMemory();
- }
-
- retval = PyBytes_FromStringAndSize(NULL, arglen*2);
- if (!retval) {
- PyBuffer_Release(&parg);
- return NULL;
- }
- retbuf = PyBytes_AS_STRING(retval);
-
- /* make hex version of string, taken from shamodule.c */
- for (i=j=0; i < arglen; i++) {
- char c;
- c = (argbuf[i] >> 4) & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
- retbuf[j++] = c;
- c = argbuf[i] & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
- retbuf[j++] = c;
- }
- PyBuffer_Release(&parg);
- return retval;
+ Py_buffer parg;
+ char* argbuf;
+ Py_ssize_t arglen;
+ PyObject *retval;
+ char* retbuf;
+ Py_ssize_t i, j;
+
+ if (!PyArg_ParseTuple(args, "y*:b2a_hex", &parg))
+ return NULL;
+ argbuf = parg.buf;
+ arglen = parg.len;
+
+ assert(arglen >= 0);
+ if (arglen > PY_SSIZE_T_MAX / 2) {
+ PyBuffer_Release(&parg);
+ return PyErr_NoMemory();
+ }
+
+ retval = PyBytes_FromStringAndSize(NULL, arglen*2);
+ if (!retval) {
+ PyBuffer_Release(&parg);
+ return NULL;
+ }
+ retbuf = PyBytes_AS_STRING(retval);
+
+ /* make hex version of string, taken from shamodule.c */
+ for (i=j=0; i < arglen; i++) {
+ char c;
+ c = (argbuf[i] >> 4) & 0xf;
+ c = (c>9) ? c+'a'-10 : c + '0';
+ retbuf[j++] = c;
+ c = argbuf[i] & 0xf;
+ c = (c>9) ? c+'a'-10 : c + '0';
+ retbuf[j++] = c;
+ }
+ PyBuffer_Release(&parg);
+ return retval;
}
PyDoc_STRVAR(doc_hexlify,
@@ -1088,69 +1088,69 @@ This function is also available as \"hexlify()\".");
static int
to_int(int c)
{
- if (isdigit(c))
- return c - '0';
- else {
- if (isupper(c))
- c = tolower(c);
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- }
- return -1;
+ if (isdigit(c))
+ return c - '0';
+ else {
+ if (isupper(c))
+ c = tolower(c);
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ }
+ return -1;
}
static PyObject *
binascii_unhexlify(PyObject *self, PyObject *args)
{
- Py_buffer parg;
- char* argbuf;
- Py_ssize_t arglen;
- PyObject *retval;
- char* retbuf;
- Py_ssize_t i, j;
-
- if (!PyArg_ParseTuple(args, "s*:a2b_hex", &parg))
- return NULL;
- argbuf = parg.buf;
- arglen = parg.len;
-
- assert(arglen >= 0);
-
- /* XXX What should we do about strings with an odd length? Should
- * we add an implicit leading zero, or a trailing zero? For now,
- * raise an exception.
- */
- if (arglen % 2) {
- PyBuffer_Release(&parg);
- PyErr_SetString(Error, "Odd-length string");
- return NULL;
- }
-
- retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
- if (!retval) {
- PyBuffer_Release(&parg);
- return NULL;
- }
- retbuf = PyBytes_AS_STRING(retval);
-
- for (i=j=0; i < arglen; i += 2) {
- int top = to_int(Py_CHARMASK(argbuf[i]));
- int bot = to_int(Py_CHARMASK(argbuf[i+1]));
- if (top == -1 || bot == -1) {
- PyErr_SetString(Error,
- "Non-hexadecimal digit found");
- goto finally;
- }
- retbuf[j++] = (top << 4) + bot;
- }
- PyBuffer_Release(&parg);
- return retval;
+ Py_buffer parg;
+ char* argbuf;
+ Py_ssize_t arglen;
+ PyObject *retval;
+ char* retbuf;
+ Py_ssize_t i, j;
+
+ if (!PyArg_ParseTuple(args, "s*:a2b_hex", &parg))
+ return NULL;
+ argbuf = parg.buf;
+ arglen = parg.len;
+
+ assert(arglen >= 0);
+
+ /* XXX What should we do about strings with an odd length? Should
+ * we add an implicit leading zero, or a trailing zero? For now,
+ * raise an exception.
+ */
+ if (arglen % 2) {
+ PyBuffer_Release(&parg);
+ PyErr_SetString(Error, "Odd-length string");
+ return NULL;
+ }
+
+ retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
+ if (!retval) {
+ PyBuffer_Release(&parg);
+ return NULL;
+ }
+ retbuf = PyBytes_AS_STRING(retval);
+
+ for (i=j=0; i < arglen; i += 2) {
+ int top = to_int(Py_CHARMASK(argbuf[i]));
+ int bot = to_int(Py_CHARMASK(argbuf[i+1]));
+ if (top == -1 || bot == -1) {
+ PyErr_SetString(Error,
+ "Non-hexadecimal digit found");
+ goto finally;
+ }
+ retbuf[j++] = (top << 4) + bot;
+ }
+ PyBuffer_Release(&parg);
+ return retval;
finally:
- PyBuffer_Release(&parg);
- Py_DECREF(retval);
- return NULL;
+ PyBuffer_Release(&parg);
+ Py_DECREF(retval);
+ return NULL;
}
PyDoc_STRVAR(doc_unhexlify,
@@ -1179,96 +1179,96 @@ PyDoc_STRVAR(doc_a2b_qp, "Decode a string of qp-encoded data");
static PyObject*
binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
{
- Py_ssize_t in, out;
- char ch;
- Py_buffer pdata;
- unsigned char *data, *odata;
- Py_ssize_t datalen = 0;
- PyObject *rv;
- static char *kwlist[] = {"data", "header", NULL};
- int header = 0;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i", kwlist, &pdata,
- &header))
- return NULL;
- data = pdata.buf;
- datalen = pdata.len;
-
- /* We allocate the output same size as input, this is overkill.
- * The previous implementation used calloc() so we'll zero out the
- * memory here too, since PyMem_Malloc() does not guarantee that.
- */
- odata = (unsigned char *) PyMem_Malloc(datalen);
- if (odata == NULL) {
- PyBuffer_Release(&pdata);
- PyErr_NoMemory();
- return NULL;
- }
- memset(odata, 0, datalen);
-
- in = out = 0;
- while (in < datalen) {
- if (data[in] == '=') {
- in++;
- if (in >= datalen) break;
- /* Soft line breaks */
- if ((data[in] == '\n') || (data[in] == '\r')) {
- if (data[in] != '\n') {
- while (in < datalen && data[in] != '\n') in++;
- }
- if (in < datalen) in++;
- }
- else if (data[in] == '=') {
- /* broken case from broken python qp */
- odata[out++] = '=';
- in++;
- }
- else if (((data[in] >= 'A' && data[in] <= 'F') ||
- (data[in] >= 'a' && data[in] <= 'f') ||
- (data[in] >= '0' && data[in] <= '9')) &&
- ((data[in+1] >= 'A' && data[in+1] <= 'F') ||
- (data[in+1] >= 'a' && data[in+1] <= 'f') ||
- (data[in+1] >= '0' && data[in+1] <= '9'))) {
- /* hexval */
- ch = hexval(data[in]) << 4;
- in++;
- ch |= hexval(data[in]);
- in++;
- odata[out++] = ch;
- }
- else {
- odata[out++] = '=';
- }
- }
- else if (header && data[in] == '_') {
- odata[out++] = ' ';
- in++;
- }
- else {
- odata[out] = data[in];
- in++;
- out++;
- }
- }
- if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
- PyBuffer_Release(&pdata);
- PyMem_Free(odata);
- return NULL;
- }
- PyBuffer_Release(&pdata);
- PyMem_Free(odata);
- return rv;
+ Py_ssize_t in, out;
+ char ch;
+ Py_buffer pdata;
+ unsigned char *data, *odata;
+ Py_ssize_t datalen = 0;
+ PyObject *rv;
+ static char *kwlist[] = {"data", "header", NULL};
+ int header = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i", kwlist, &pdata,
+ &header))
+ return NULL;
+ data = pdata.buf;
+ datalen = pdata.len;
+
+ /* We allocate the output same size as input, this is overkill.
+ * The previous implementation used calloc() so we'll zero out the
+ * memory here too, since PyMem_Malloc() does not guarantee that.
+ */
+ odata = (unsigned char *) PyMem_Malloc(datalen);
+ if (odata == NULL) {
+ PyBuffer_Release(&pdata);
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(odata, 0, datalen);
+
+ in = out = 0;
+ while (in < datalen) {
+ if (data[in] == '=') {
+ in++;
+ if (in >= datalen) break;
+ /* Soft line breaks */
+ if ((data[in] == '\n') || (data[in] == '\r')) {
+ if (data[in] != '\n') {
+ while (in < datalen && data[in] != '\n') in++;
+ }
+ if (in < datalen) in++;
+ }
+ else if (data[in] == '=') {
+ /* broken case from broken python qp */
+ odata[out++] = '=';
+ in++;
+ }
+ else if (((data[in] >= 'A' && data[in] <= 'F') ||
+ (data[in] >= 'a' && data[in] <= 'f') ||
+ (data[in] >= '0' && data[in] <= '9')) &&
+ ((data[in+1] >= 'A' && data[in+1] <= 'F') ||
+ (data[in+1] >= 'a' && data[in+1] <= 'f') ||
+ (data[in+1] >= '0' && data[in+1] <= '9'))) {
+ /* hexval */
+ ch = hexval(data[in]) << 4;
+ in++;
+ ch |= hexval(data[in]);
+ in++;
+ odata[out++] = ch;
+ }
+ else {
+ odata[out++] = '=';
+ }
+ }
+ else if (header && data[in] == '_') {
+ odata[out++] = ' ';
+ in++;
+ }
+ else {
+ odata[out] = data[in];
+ in++;
+ out++;
+ }
+ }
+ if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
+ PyBuffer_Release(&pdata);
+ PyMem_Free(odata);
+ return NULL;
+ }
+ PyBuffer_Release(&pdata);
+ PyMem_Free(odata);
+ return rv;
}
static int
to_hex (unsigned char ch, unsigned char *s)
{
- unsigned int uvalue = ch;
+ unsigned int uvalue = ch;
- s[1] = "0123456789ABCDEF"[uvalue % 16];
- uvalue = (uvalue / 16);
- s[0] = "0123456789ABCDEF"[uvalue % 16];
- return 0;
+ s[1] = "0123456789ABCDEF"[uvalue % 16];
+ uvalue = (uvalue / 16);
+ s[0] = "0123456789ABCDEF"[uvalue % 16];
+ return 0;
}
PyDoc_STRVAR(doc_b2a_qp,
@@ -1285,210 +1285,210 @@ both encoded. When quotetabs is set, space and tabs are encoded.");
static PyObject*
binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
{
- Py_ssize_t in, out;
- Py_buffer pdata;
- unsigned char *data, *odata;
- Py_ssize_t datalen = 0, odatalen = 0;
- PyObject *rv;
- unsigned int linelen = 0;
- static char *kwlist[] = {"data", "quotetabs", "istext",
- "header", NULL};
- int istext = 1;
- int quotetabs = 0;
- int header = 0;
- unsigned char ch;
- int crlf = 0;
- unsigned char *p;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii", kwlist, &pdata,
- &quotetabs, &istext, &header))
- return NULL;
- data = pdata.buf;
- datalen = pdata.len;
-
- /* See if this string is using CRLF line ends */
- /* XXX: this function has the side effect of converting all of
- * the end of lines to be the same depending on this detection
- * here */
- p = (unsigned char *) memchr(data, '\n', datalen);
- if ((p != NULL) && (p > data) && (*(p-1) == '\r'))
- crlf = 1;
-
- /* First, scan to see how many characters need to be encoded */
- in = 0;
- while (in < datalen) {
- if ((data[in] > 126) ||
- (data[in] == '=') ||
- (header && data[in] == '_') ||
- ((data[in] == '.') && (linelen == 0) &&
- (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
- (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
- ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
- ((data[in] < 33) &&
- (data[in] != '\r') && (data[in] != '\n') &&
- (quotetabs ||
- (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
- {
- if ((linelen + 3) >= MAXLINESIZE) {
- linelen = 0;
- if (crlf)
- odatalen += 3;
- else
- odatalen += 2;
- }
- linelen += 3;
- odatalen += 3;
- in++;
- }
- else {
- if (istext &&
- ((data[in] == '\n') ||
- ((in+1 < datalen) && (data[in] == '\r') &&
- (data[in+1] == '\n'))))
- {
- linelen = 0;
- /* Protect against whitespace on end of line */
- if (in && ((data[in-1] == ' ') || (data[in-1] == '\t')))
- odatalen += 2;
- if (crlf)
- odatalen += 2;
- else
- odatalen += 1;
- if (data[in] == '\r')
- in += 2;
- else
- in++;
- }
- else {
- if ((in + 1 != datalen) &&
- (data[in+1] != '\n') &&
- (linelen + 1) >= MAXLINESIZE) {
- linelen = 0;
- if (crlf)
- odatalen += 3;
- else
- odatalen += 2;
- }
- linelen++;
- odatalen++;
- in++;
- }
- }
- }
-
- /* We allocate the output same size as input, this is overkill.
- * The previous implementation used calloc() so we'll zero out the
- * memory here too, since PyMem_Malloc() does not guarantee that.
- */
- odata = (unsigned char *) PyMem_Malloc(odatalen);
- if (odata == NULL) {
- PyBuffer_Release(&pdata);
- PyErr_NoMemory();
- return NULL;
- }
- memset(odata, 0, odatalen);
-
- in = out = linelen = 0;
- while (in < datalen) {
- if ((data[in] > 126) ||
- (data[in] == '=') ||
- (header && data[in] == '_') ||
- ((data[in] == '.') && (linelen == 0) &&
- (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
- (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
- ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
- ((data[in] < 33) &&
- (data[in] != '\r') && (data[in] != '\n') &&
- (quotetabs ||
- (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
- {
- if ((linelen + 3 )>= MAXLINESIZE) {
- odata[out++] = '=';
- if (crlf) odata[out++] = '\r';
- odata[out++] = '\n';
- linelen = 0;
- }
- odata[out++] = '=';
- to_hex(data[in], &odata[out]);
- out += 2;
- in++;
- linelen += 3;
- }
- else {
- if (istext &&
- ((data[in] == '\n') ||
- ((in+1 < datalen) && (data[in] == '\r') &&
- (data[in+1] == '\n'))))
- {
- linelen = 0;
- /* Protect against whitespace on end of line */
- if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
- ch = odata[out-1];
- odata[out-1] = '=';
- to_hex(ch, &odata[out]);
- out += 2;
- }
-
- if (crlf) odata[out++] = '\r';
- odata[out++] = '\n';
- if (data[in] == '\r')
- in += 2;
- else
- in++;
- }
- else {
- if ((in + 1 != datalen) &&
- (data[in+1] != '\n') &&
- (linelen + 1) >= MAXLINESIZE) {
- odata[out++] = '=';
- if (crlf) odata[out++] = '\r';
- odata[out++] = '\n';
- linelen = 0;
- }
- linelen++;
- if (header && data[in] == ' ') {
- odata[out++] = '_';
- in++;
- }
- else {
- odata[out++] = data[in++];
- }
- }
- }
- }
- if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
- PyBuffer_Release(&pdata);
- PyMem_Free(odata);
- return NULL;
- }
- PyBuffer_Release(&pdata);
- PyMem_Free(odata);
- return rv;
+ Py_ssize_t in, out;
+ Py_buffer pdata;
+ unsigned char *data, *odata;
+ Py_ssize_t datalen = 0, odatalen = 0;
+ PyObject *rv;
+ unsigned int linelen = 0;
+ static char *kwlist[] = {"data", "quotetabs", "istext",
+ "header", NULL};
+ int istext = 1;
+ int quotetabs = 0;
+ int header = 0;
+ unsigned char ch;
+ int crlf = 0;
+ unsigned char *p;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii", kwlist, &pdata,
+ &quotetabs, &istext, &header))
+ return NULL;
+ data = pdata.buf;
+ datalen = pdata.len;
+
+ /* See if this string is using CRLF line ends */
+ /* XXX: this function has the side effect of converting all of
+ * the end of lines to be the same depending on this detection
+ * here */
+ p = (unsigned char *) memchr(data, '\n', datalen);
+ if ((p != NULL) && (p > data) && (*(p-1) == '\r'))
+ crlf = 1;
+
+ /* First, scan to see how many characters need to be encoded */
+ in = 0;
+ while (in < datalen) {
+ if ((data[in] > 126) ||
+ (data[in] == '=') ||
+ (header && data[in] == '_') ||
+ ((data[in] == '.') && (linelen == 0) &&
+ (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
+ (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
+ ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
+ ((data[in] < 33) &&
+ (data[in] != '\r') && (data[in] != '\n') &&
+ (quotetabs ||
+ (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
+ {
+ if ((linelen + 3) >= MAXLINESIZE) {
+ linelen = 0;
+ if (crlf)
+ odatalen += 3;
+ else
+ odatalen += 2;
+ }
+ linelen += 3;
+ odatalen += 3;
+ in++;
+ }
+ else {
+ if (istext &&
+ ((data[in] == '\n') ||
+ ((in+1 < datalen) && (data[in] == '\r') &&
+ (data[in+1] == '\n'))))
+ {
+ linelen = 0;
+ /* Protect against whitespace on end of line */
+ if (in && ((data[in-1] == ' ') || (data[in-1] == '\t')))
+ odatalen += 2;
+ if (crlf)
+ odatalen += 2;
+ else
+ odatalen += 1;
+ if (data[in] == '\r')
+ in += 2;
+ else
+ in++;
+ }
+ else {
+ if ((in + 1 != datalen) &&
+ (data[in+1] != '\n') &&
+ (linelen + 1) >= MAXLINESIZE) {
+ linelen = 0;
+ if (crlf)
+ odatalen += 3;
+ else
+ odatalen += 2;
+ }
+ linelen++;
+ odatalen++;
+ in++;
+ }
+ }
+ }
+
+ /* We allocate the output same size as input, this is overkill.
+ * The previous implementation used calloc() so we'll zero out the
+ * memory here too, since PyMem_Malloc() does not guarantee that.
+ */
+ odata = (unsigned char *) PyMem_Malloc(odatalen);
+ if (odata == NULL) {
+ PyBuffer_Release(&pdata);
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(odata, 0, odatalen);
+
+ in = out = linelen = 0;
+ while (in < datalen) {
+ if ((data[in] > 126) ||
+ (data[in] == '=') ||
+ (header && data[in] == '_') ||
+ ((data[in] == '.') && (linelen == 0) &&
+ (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
+ (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
+ ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
+ ((data[in] < 33) &&
+ (data[in] != '\r') && (data[in] != '\n') &&
+ (quotetabs ||
+ (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
+ {
+ if ((linelen + 3 )>= MAXLINESIZE) {
+ odata[out++] = '=';
+ if (crlf) odata[out++] = '\r';
+ odata[out++] = '\n';
+ linelen = 0;
+ }
+ odata[out++] = '=';
+ to_hex(data[in], &odata[out]);
+ out += 2;
+ in++;
+ linelen += 3;
+ }
+ else {
+ if (istext &&
+ ((data[in] == '\n') ||
+ ((in+1 < datalen) && (data[in] == '\r') &&
+ (data[in+1] == '\n'))))
+ {
+ linelen = 0;
+ /* Protect against whitespace on end of line */
+ if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
+ ch = odata[out-1];
+ odata[out-1] = '=';
+ to_hex(ch, &odata[out]);
+ out += 2;
+ }
+
+ if (crlf) odata[out++] = '\r';
+ odata[out++] = '\n';
+ if (data[in] == '\r')
+ in += 2;
+ else
+ in++;
+ }
+ else {
+ if ((in + 1 != datalen) &&
+ (data[in+1] != '\n') &&
+ (linelen + 1) >= MAXLINESIZE) {
+ odata[out++] = '=';
+ if (crlf) odata[out++] = '\r';
+ odata[out++] = '\n';
+ linelen = 0;
+ }
+ linelen++;
+ if (header && data[in] == ' ') {
+ odata[out++] = '_';
+ in++;
+ }
+ else {
+ odata[out++] = data[in++];
+ }
+ }
+ }
+ }
+ if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
+ PyBuffer_Release(&pdata);
+ PyMem_Free(odata);
+ return NULL;
+ }
+ PyBuffer_Release(&pdata);
+ PyMem_Free(odata);
+ return rv;
}
/* List of functions defined in the module */
static struct PyMethodDef binascii_module_methods[] = {
- {"a2b_uu", binascii_a2b_uu, METH_VARARGS, doc_a2b_uu},
- {"b2a_uu", binascii_b2a_uu, METH_VARARGS, doc_b2a_uu},
- {"a2b_base64", binascii_a2b_base64, METH_VARARGS, doc_a2b_base64},
- {"b2a_base64", binascii_b2a_base64, METH_VARARGS, doc_b2a_base64},
- {"a2b_hqx", binascii_a2b_hqx, METH_VARARGS, doc_a2b_hqx},
- {"b2a_hqx", binascii_b2a_hqx, METH_VARARGS, doc_b2a_hqx},
- {"b2a_hex", binascii_hexlify, METH_VARARGS, doc_hexlify},
- {"a2b_hex", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
- {"hexlify", binascii_hexlify, METH_VARARGS, doc_hexlify},
- {"unhexlify", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
- {"rlecode_hqx", binascii_rlecode_hqx, METH_VARARGS, doc_rlecode_hqx},
- {"rledecode_hqx", binascii_rledecode_hqx, METH_VARARGS,
- doc_rledecode_hqx},
- {"crc_hqx", binascii_crc_hqx, METH_VARARGS, doc_crc_hqx},
- {"crc32", binascii_crc32, METH_VARARGS, doc_crc32},
- {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS | METH_KEYWORDS,
- doc_a2b_qp},
- {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS | METH_KEYWORDS,
- doc_b2a_qp},
- {NULL, NULL} /* sentinel */
+ {"a2b_uu", binascii_a2b_uu, METH_VARARGS, doc_a2b_uu},
+ {"b2a_uu", binascii_b2a_uu, METH_VARARGS, doc_b2a_uu},
+ {"a2b_base64", binascii_a2b_base64, METH_VARARGS, doc_a2b_base64},
+ {"b2a_base64", binascii_b2a_base64, METH_VARARGS, doc_b2a_base64},
+ {"a2b_hqx", binascii_a2b_hqx, METH_VARARGS, doc_a2b_hqx},
+ {"b2a_hqx", binascii_b2a_hqx, METH_VARARGS, doc_b2a_hqx},
+ {"b2a_hex", binascii_hexlify, METH_VARARGS, doc_hexlify},
+ {"a2b_hex", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
+ {"hexlify", binascii_hexlify, METH_VARARGS, doc_hexlify},
+ {"unhexlify", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
+ {"rlecode_hqx", binascii_rlecode_hqx, METH_VARARGS, doc_rlecode_hqx},
+ {"rledecode_hqx", binascii_rledecode_hqx, METH_VARARGS,
+ doc_rledecode_hqx},
+ {"crc_hqx", binascii_crc_hqx, METH_VARARGS, doc_crc_hqx},
+ {"crc32", binascii_crc32, METH_VARARGS, doc_crc32},
+ {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS | METH_KEYWORDS,
+ doc_a2b_qp},
+ {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS | METH_KEYWORDS,
+ doc_b2a_qp},
+ {NULL, NULL} /* sentinel */
};
@@ -1497,36 +1497,36 @@ PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
static struct PyModuleDef binasciimodule = {
- PyModuleDef_HEAD_INIT,
- "binascii",
- doc_binascii,
- -1,
- binascii_module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "binascii",
+ doc_binascii,
+ -1,
+ binascii_module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_binascii(void)
{
- PyObject *m, *d;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&binasciimodule);
- if (m == NULL)
- return NULL;
-
- d = PyModule_GetDict(m);
-
- Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
- PyDict_SetItemString(d, "Error", Error);
- Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
- PyDict_SetItemString(d, "Incomplete", Incomplete);
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- m = NULL;
- }
- return m;
+ PyObject *m, *d;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&binasciimodule);
+ if (m == NULL)
+ return NULL;
+
+ d = PyModule_GetDict(m);
+
+ Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
+ PyDict_SetItemString(d, "Error", Error);
+ Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
+ PyDict_SetItemString(d, "Incomplete", Incomplete);
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ m = NULL;
+ }
+ return m;
}
diff --git a/Modules/bz2module.c b/Modules/bz2module.c
index 3e8eb8b..7c7c0e0 100644
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -41,20 +41,20 @@ typedef fpos_t Py_off_t;
#define MODE_READ_EOF 2
#define MODE_WRITE 3
-#define BZ2FileObject_Check(v) (Py_TYPE(v) == &BZ2File_Type)
+#define BZ2FileObject_Check(v) (Py_TYPE(v) == &BZ2File_Type)
#ifdef BZ_CONFIG_ERROR
#if SIZEOF_LONG >= 8
#define BZS_TOTAL_OUT(bzs) \
- (((long)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
+ (((long)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
#elif SIZEOF_LONG_LONG >= 8
#define BZS_TOTAL_OUT(bzs) \
- (((PY_LONG_LONG)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
+ (((PY_LONG_LONG)bzs->total_out_hi32 << 32) + bzs->total_out_lo32)
#else
#define BZS_TOTAL_OUT(bzs) \
- bzs->total_out_lo32
+ bzs->total_out_lo32
#endif
#else /* ! BZ_CONFIG_ERROR */
@@ -79,11 +79,11 @@ typedef fpos_t Py_off_t;
#ifdef WITH_THREAD
#define ACQUIRE_LOCK(obj) do { \
- if (!PyThread_acquire_lock(obj->lock, 0)) { \
- Py_BEGIN_ALLOW_THREADS \
- PyThread_acquire_lock(obj->lock, 1); \
- Py_END_ALLOW_THREADS \
- } } while(0)
+ if (!PyThread_acquire_lock(obj->lock, 0)) { \
+ Py_BEGIN_ALLOW_THREADS \
+ PyThread_acquire_lock(obj->lock, 1); \
+ Py_END_ALLOW_THREADS \
+ } } while(0)
#define RELEASE_LOCK(obj) PyThread_release_lock(obj->lock)
#else
#define ACQUIRE_LOCK(obj)
@@ -91,47 +91,47 @@ typedef fpos_t Py_off_t;
#endif
/* Bits in f_newlinetypes */
-#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
-#define NEWLINE_CR 1 /* \r newline seen */
-#define NEWLINE_LF 2 /* \n newline seen */
-#define NEWLINE_CRLF 4 /* \r\n newline seen */
+#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
+#define NEWLINE_CR 1 /* \r newline seen */
+#define NEWLINE_LF 2 /* \n newline seen */
+#define NEWLINE_CRLF 4 /* \r\n newline seen */
/* ===================================================================== */
/* Structure definitions. */
typedef struct {
- PyObject_HEAD
- FILE *rawfp;
+ PyObject_HEAD
+ FILE *rawfp;
- char* f_buf; /* Allocated readahead buffer */
- char* f_bufend; /* Points after last occupied position */
- char* f_bufptr; /* Current buffer position */
+ char* f_buf; /* Allocated readahead buffer */
+ char* f_bufend; /* Points after last occupied position */
+ char* f_bufptr; /* Current buffer position */
- BZFILE *fp;
- int mode;
- Py_off_t pos;
- Py_off_t size;
+ BZFILE *fp;
+ int mode;
+ Py_off_t pos;
+ Py_off_t size;
#ifdef WITH_THREAD
- PyThread_type_lock lock;
+ PyThread_type_lock lock;
#endif
} BZ2FileObject;
typedef struct {
- PyObject_HEAD
- bz_stream bzs;
- int running;
+ PyObject_HEAD
+ bz_stream bzs;
+ int running;
#ifdef WITH_THREAD
- PyThread_type_lock lock;
+ PyThread_type_lock lock;
#endif
} BZ2CompObject;
typedef struct {
- PyObject_HEAD
- bz_stream bzs;
- int running;
- PyObject *unused_data;
+ PyObject_HEAD
+ bz_stream bzs;
+ int running;
+ PyObject *unused_data;
#ifdef WITH_THREAD
- PyThread_type_lock lock;
+ PyThread_type_lock lock;
#endif
} BZ2DecompObject;
@@ -141,59 +141,59 @@ typedef struct {
static int
Util_CatchBZ2Error(int bzerror)
{
- int ret = 0;
- switch(bzerror) {
- case BZ_OK:
- case BZ_STREAM_END:
- break;
+ int ret = 0;
+ switch(bzerror) {
+ case BZ_OK:
+ case BZ_STREAM_END:
+ break;
#ifdef BZ_CONFIG_ERROR
- case BZ_CONFIG_ERROR:
- PyErr_SetString(PyExc_SystemError,
- "the bz2 library was not compiled "
- "correctly");
- ret = 1;
- break;
+ case BZ_CONFIG_ERROR:
+ PyErr_SetString(PyExc_SystemError,
+ "the bz2 library was not compiled "
+ "correctly");
+ ret = 1;
+ break;
#endif
- case BZ_PARAM_ERROR:
- PyErr_SetString(PyExc_ValueError,
- "the bz2 library has received wrong "
- "parameters");
- ret = 1;
- break;
-
- case BZ_MEM_ERROR:
- PyErr_NoMemory();
- ret = 1;
- break;
-
- case BZ_DATA_ERROR:
- case BZ_DATA_ERROR_MAGIC:
- PyErr_SetString(PyExc_IOError, "invalid data stream");
- ret = 1;
- break;
-
- case BZ_IO_ERROR:
- PyErr_SetString(PyExc_IOError, "unknown IO error");
- ret = 1;
- break;
-
- case BZ_UNEXPECTED_EOF:
- PyErr_SetString(PyExc_EOFError,
- "compressed file ended before the "
- "logical end-of-stream was detected");
- ret = 1;
- break;
-
- case BZ_SEQUENCE_ERROR:
- PyErr_SetString(PyExc_RuntimeError,
- "wrong sequence of bz2 library "
- "commands used");
- ret = 1;
- break;
- }
- return ret;
+ case BZ_PARAM_ERROR:
+ PyErr_SetString(PyExc_ValueError,
+ "the bz2 library has received wrong "
+ "parameters");
+ ret = 1;
+ break;
+
+ case BZ_MEM_ERROR:
+ PyErr_NoMemory();
+ ret = 1;
+ break;
+
+ case BZ_DATA_ERROR:
+ case BZ_DATA_ERROR_MAGIC:
+ PyErr_SetString(PyExc_IOError, "invalid data stream");
+ ret = 1;
+ break;
+
+ case BZ_IO_ERROR:
+ PyErr_SetString(PyExc_IOError, "unknown IO error");
+ ret = 1;
+ break;
+
+ case BZ_UNEXPECTED_EOF:
+ PyErr_SetString(PyExc_EOFError,
+ "compressed file ended before the "
+ "logical end-of-stream was detected");
+ ret = 1;
+ break;
+
+ case BZ_SEQUENCE_ERROR:
+ PyErr_SetString(PyExc_RuntimeError,
+ "wrong sequence of bz2 library "
+ "commands used");
+ ret = 1;
+ break;
+ }
+ return ret;
}
#if BUFSIZ < 8192
@@ -212,134 +212,134 @@ Util_CatchBZ2Error(int bzerror)
static size_t
Util_NewBufferSize(size_t currentsize)
{
- if (currentsize > SMALLCHUNK) {
- /* Keep doubling until we reach BIGCHUNK;
- then keep adding BIGCHUNK. */
- if (currentsize <= BIGCHUNK)
- return currentsize + currentsize;
- else
- return currentsize + BIGCHUNK;
- }
- return currentsize + SMALLCHUNK;
+ if (currentsize > SMALLCHUNK) {
+ /* Keep doubling until we reach BIGCHUNK;
+ then keep adding BIGCHUNK. */
+ if (currentsize <= BIGCHUNK)
+ return currentsize + currentsize;
+ else
+ return currentsize + BIGCHUNK;
+ }
+ return currentsize + SMALLCHUNK;
}
/* This is a hacked version of Python's fileobject.c:get_line(). */
static PyObject *
Util_GetLine(BZ2FileObject *f, int n)
{
- char c;
- char *buf, *end;
- size_t total_v_size; /* total # of slots in buffer */
- size_t used_v_size; /* # used slots in buffer */
- size_t increment; /* amount to increment the buffer */
- PyObject *v;
- int bzerror;
- int bytes_read;
-
- total_v_size = n > 0 ? n : 100;
- v = PyBytes_FromStringAndSize((char *)NULL, total_v_size);
- if (v == NULL)
- return NULL;
-
- buf = BUF(v);
- end = buf + total_v_size;
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- do {
- bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
- f->pos++;
- if (bytes_read == 0)
- break;
- *buf++ = c;
- } while (bzerror == BZ_OK && c != '\n' && buf != end);
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- f->size = f->pos;
- f->mode = MODE_READ_EOF;
- break;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- Py_DECREF(v);
- return NULL;
- }
- if (c == '\n')
- break;
- /* Must be because buf == end */
- if (n > 0)
- break;
- used_v_size = total_v_size;
- increment = total_v_size >> 2; /* mild exponential growth */
- total_v_size += increment;
- if (total_v_size > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "line is longer than a Python string can hold");
- Py_DECREF(v);
- return NULL;
- }
- if (_PyBytes_Resize(&v, total_v_size) < 0) {
- return NULL;
- }
- buf = BUF(v) + used_v_size;
- end = BUF(v) + total_v_size;
- }
-
- used_v_size = buf - BUF(v);
- if (used_v_size != total_v_size) {
- if (_PyBytes_Resize(&v, used_v_size) < 0) {
- v = NULL;
- }
- }
- return v;
+ char c;
+ char *buf, *end;
+ size_t total_v_size; /* total # of slots in buffer */
+ size_t used_v_size; /* # used slots in buffer */
+ size_t increment; /* amount to increment the buffer */
+ PyObject *v;
+ int bzerror;
+ int bytes_read;
+
+ total_v_size = n > 0 ? n : 100;
+ v = PyBytes_FromStringAndSize((char *)NULL, total_v_size);
+ if (v == NULL)
+ return NULL;
+
+ buf = BUF(v);
+ end = buf + total_v_size;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ do {
+ bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
+ f->pos++;
+ if (bytes_read == 0)
+ break;
+ *buf++ = c;
+ } while (bzerror == BZ_OK && c != '\n' && buf != end);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ f->size = f->pos;
+ f->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (c == '\n')
+ break;
+ /* Must be because buf == end */
+ if (n > 0)
+ break;
+ used_v_size = total_v_size;
+ increment = total_v_size >> 2; /* mild exponential growth */
+ total_v_size += increment;
+ if (total_v_size > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "line is longer than a Python string can hold");
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (_PyBytes_Resize(&v, total_v_size) < 0) {
+ return NULL;
+ }
+ buf = BUF(v) + used_v_size;
+ end = BUF(v) + total_v_size;
+ }
+
+ used_v_size = buf - BUF(v);
+ if (used_v_size != total_v_size) {
+ if (_PyBytes_Resize(&v, used_v_size) < 0) {
+ v = NULL;
+ }
+ }
+ return v;
}
/* This is a hacked version of Python's fileobject.c:drop_readahead(). */
static void
Util_DropReadAhead(BZ2FileObject *f)
{
- if (f->f_buf != NULL) {
- PyMem_Free(f->f_buf);
- f->f_buf = NULL;
- }
+ if (f->f_buf != NULL) {
+ PyMem_Free(f->f_buf);
+ f->f_buf = NULL;
+ }
}
/* This is a hacked version of Python's fileobject.c:readahead(). */
static int
Util_ReadAhead(BZ2FileObject *f, int bufsize)
{
- int chunksize;
- int bzerror;
-
- if (f->f_buf != NULL) {
- if((f->f_bufend - f->f_bufptr) >= 1)
- return 0;
- else
- Util_DropReadAhead(f);
- }
- if (f->mode == MODE_READ_EOF) {
- f->f_bufptr = f->f_buf;
- f->f_bufend = f->f_buf;
- return 0;
- }
- if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- Py_BEGIN_ALLOW_THREADS
- chunksize = BZ2_bzRead(&bzerror, f->fp, f->f_buf, bufsize);
- Py_END_ALLOW_THREADS
- f->pos += chunksize;
- if (bzerror == BZ_STREAM_END) {
- f->size = f->pos;
- f->mode = MODE_READ_EOF;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- Util_DropReadAhead(f);
- return -1;
- }
- f->f_bufptr = f->f_buf;
- f->f_bufend = f->f_buf + chunksize;
- return 0;
+ int chunksize;
+ int bzerror;
+
+ if (f->f_buf != NULL) {
+ if((f->f_bufend - f->f_bufptr) >= 1)
+ return 0;
+ else
+ Util_DropReadAhead(f);
+ }
+ if (f->mode == MODE_READ_EOF) {
+ f->f_bufptr = f->f_buf;
+ f->f_bufend = f->f_buf;
+ return 0;
+ }
+ if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = BZ2_bzRead(&bzerror, f->fp, f->f_buf, bufsize);
+ Py_END_ALLOW_THREADS
+ f->pos += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ f->size = f->pos;
+ f->mode = MODE_READ_EOF;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Util_DropReadAhead(f);
+ return -1;
+ }
+ f->f_bufptr = f->f_buf;
+ f->f_bufend = f->f_buf + chunksize;
+ return 0;
}
/* This is a hacked version of Python's
@@ -347,45 +347,45 @@ Util_ReadAhead(BZ2FileObject *f, int bufsize)
static PyBytesObject *
Util_ReadAheadGetLineSkip(BZ2FileObject *f, int skip, int bufsize)
{
- PyBytesObject* s;
- char *bufptr;
- char *buf;
- int len;
-
- if (f->f_buf == NULL)
- if (Util_ReadAhead(f, bufsize) < 0)
- return NULL;
-
- len = f->f_bufend - f->f_bufptr;
- if (len == 0)
- return (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, skip);
- bufptr = memchr(f->f_bufptr, '\n', len);
- if (bufptr != NULL) {
- bufptr++; /* Count the '\n' */
- len = bufptr - f->f_bufptr;
- s = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, skip+len);
- if (s == NULL)
- return NULL;
- memcpy(PyBytes_AS_STRING(s)+skip, f->f_bufptr, len);
- f->f_bufptr = bufptr;
- if (bufptr == f->f_bufend)
- Util_DropReadAhead(f);
- } else {
- bufptr = f->f_bufptr;
- buf = f->f_buf;
- f->f_buf = NULL; /* Force new readahead buffer */
- s = Util_ReadAheadGetLineSkip(f, skip+len,
- bufsize + (bufsize>>2));
- if (s == NULL) {
- PyMem_Free(buf);
- return NULL;
- }
- memcpy(PyBytes_AS_STRING(s)+skip, bufptr, len);
- PyMem_Free(buf);
- }
- return s;
+ PyBytesObject* s;
+ char *bufptr;
+ char *buf;
+ int len;
+
+ if (f->f_buf == NULL)
+ if (Util_ReadAhead(f, bufsize) < 0)
+ return NULL;
+
+ len = f->f_bufend - f->f_bufptr;
+ if (len == 0)
+ return (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, skip);
+ bufptr = memchr(f->f_bufptr, '\n', len);
+ if (bufptr != NULL) {
+ bufptr++; /* Count the '\n' */
+ len = bufptr - f->f_bufptr;
+ s = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, skip+len);
+ if (s == NULL)
+ return NULL;
+ memcpy(PyBytes_AS_STRING(s)+skip, f->f_bufptr, len);
+ f->f_bufptr = bufptr;
+ if (bufptr == f->f_bufend)
+ Util_DropReadAhead(f);
+ } else {
+ bufptr = f->f_bufptr;
+ buf = f->f_buf;
+ f->f_buf = NULL; /* Force new readahead buffer */
+ s = Util_ReadAheadGetLineSkip(f, skip+len,
+ bufsize + (bufsize>>2));
+ if (s == NULL) {
+ PyMem_Free(buf);
+ return NULL;
+ }
+ memcpy(PyBytes_AS_STRING(s)+skip, bufptr, len);
+ PyMem_Free(buf);
+ }
+ return s;
}
/* ===================================================================== */
@@ -402,83 +402,83 @@ argument is negative or omitted, read until EOF is reached.\n\
static PyObject *
BZ2File_read(BZ2FileObject *self, PyObject *args)
{
- long bytesrequested = -1;
- size_t bytesread, buffersize, chunksize;
- int bzerror;
- PyObject *ret = NULL;
-
- if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
- return NULL;
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_READ:
- break;
- case MODE_READ_EOF:
- ret = PyBytes_FromStringAndSize("", 0);
- goto cleanup;
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
- default:
- PyErr_SetString(PyExc_IOError,
- "file is not ready for reading");
- goto cleanup;
- }
-
- if (bytesrequested < 0)
- buffersize = Util_NewBufferSize((size_t)0);
- else
- buffersize = bytesrequested;
- if (buffersize > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "requested number of bytes is "
- "more than a Python string can hold");
- goto cleanup;
- }
- ret = PyBytes_FromStringAndSize((char *)NULL, buffersize);
- if (ret == NULL || buffersize == 0)
- goto cleanup;
- bytesread = 0;
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- chunksize = BZ2_bzRead(&bzerror, self->fp,
- BUF(ret)+bytesread,
- buffersize-bytesread);
- self->pos += chunksize;
- Py_END_ALLOW_THREADS
- bytesread += chunksize;
- if (bzerror == BZ_STREAM_END) {
- self->size = self->pos;
- self->mode = MODE_READ_EOF;
- break;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- Py_DECREF(ret);
- ret = NULL;
- goto cleanup;
- }
- if (bytesrequested < 0) {
- buffersize = Util_NewBufferSize(buffersize);
- if (_PyBytes_Resize(&ret, buffersize) < 0) {
- ret = NULL;
- goto cleanup;
- }
- } else {
- break;
- }
- }
- if (bytesread != buffersize) {
- if (_PyBytes_Resize(&ret, bytesread) < 0) {
- ret = NULL;
- }
- }
+ long bytesrequested = -1;
+ size_t bytesread, buffersize, chunksize;
+ int bzerror;
+ PyObject *ret = NULL;
+
+ if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ ret = PyBytes_FromStringAndSize("", 0);
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if (bytesrequested < 0)
+ buffersize = Util_NewBufferSize((size_t)0);
+ else
+ buffersize = bytesrequested;
+ if (buffersize > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "requested number of bytes is "
+ "more than a Python string can hold");
+ goto cleanup;
+ }
+ ret = PyBytes_FromStringAndSize((char *)NULL, buffersize);
+ if (ret == NULL || buffersize == 0)
+ goto cleanup;
+ bytesread = 0;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = BZ2_bzRead(&bzerror, self->fp,
+ BUF(ret)+bytesread,
+ buffersize-bytesread);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ ret = NULL;
+ goto cleanup;
+ }
+ if (bytesrequested < 0) {
+ buffersize = Util_NewBufferSize(buffersize);
+ if (_PyBytes_Resize(&ret, buffersize) < 0) {
+ ret = NULL;
+ goto cleanup;
+ }
+ } else {
+ break;
+ }
+ }
+ if (bytesread != buffersize) {
+ if (_PyBytes_Resize(&ret, bytesread) < 0) {
+ ret = NULL;
+ }
+ }
cleanup:
- RELEASE_LOCK(self);
- return ret;
+ RELEASE_LOCK(self);
+ return ret;
}
PyDoc_STRVAR(BZ2File_readline__doc__,
@@ -493,37 +493,37 @@ string at EOF.\n\
static PyObject *
BZ2File_readline(BZ2FileObject *self, PyObject *args)
{
- PyObject *ret = NULL;
- int sizehint = -1;
-
- if (!PyArg_ParseTuple(args, "|i:readline", &sizehint))
- return NULL;
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_READ:
- break;
- case MODE_READ_EOF:
- ret = PyBytes_FromStringAndSize("", 0);
- goto cleanup;
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
- default:
- PyErr_SetString(PyExc_IOError,
- "file is not ready for reading");
- goto cleanup;
- }
-
- if (sizehint == 0)
- ret = PyBytes_FromStringAndSize("", 0);
- else
- ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint);
+ PyObject *ret = NULL;
+ int sizehint = -1;
+
+ if (!PyArg_ParseTuple(args, "|i:readline", &sizehint))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ ret = PyBytes_FromStringAndSize("", 0);
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if (sizehint == 0)
+ ret = PyBytes_FromStringAndSize("", 0);
+ else
+ ret = Util_GetLine(self, (sizehint < 0) ? 0 : sizehint);
cleanup:
- RELEASE_LOCK(self);
- return ret;
+ RELEASE_LOCK(self);
+ return ret;
}
PyDoc_STRVAR(BZ2File_readlines__doc__,
@@ -538,150 +538,150 @@ total number of bytes in the lines returned.\n\
static PyObject *
BZ2File_readlines(BZ2FileObject *self, PyObject *args)
{
- long sizehint = 0;
- PyObject *list = NULL;
- PyObject *line;
- char small_buffer[SMALLCHUNK];
- char *buffer = small_buffer;
- size_t buffersize = SMALLCHUNK;
- PyObject *big_buffer = NULL;
- size_t nfilled = 0;
- size_t nread;
- size_t totalread = 0;
- char *p, *q, *end;
- int err;
- int shortread = 0;
- int bzerror;
-
- if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
- return NULL;
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_READ:
- break;
- case MODE_READ_EOF:
- list = PyList_New(0);
- goto cleanup;
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
- default:
- PyErr_SetString(PyExc_IOError,
- "file is not ready for reading");
- goto cleanup;
- }
-
- if ((list = PyList_New(0)) == NULL)
- goto cleanup;
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- nread = BZ2_bzRead(&bzerror, self->fp,
- buffer+nfilled, buffersize-nfilled);
- self->pos += nread;
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- self->size = self->pos;
- self->mode = MODE_READ_EOF;
- if (nread == 0) {
- sizehint = 0;
- break;
- }
- shortread = 1;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- error:
- Py_DECREF(list);
- list = NULL;
- goto cleanup;
- }
- totalread += nread;
- p = memchr(buffer+nfilled, '\n', nread);
- if (!shortread && p == NULL) {
- /* Need a larger buffer to fit this line */
- nfilled += nread;
- buffersize *= 2;
- if (buffersize > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "line is longer than a Python string can hold");
- goto error;
- }
- if (big_buffer == NULL) {
- /* Create the big buffer */
- big_buffer = PyBytes_FromStringAndSize(
- NULL, buffersize);
- if (big_buffer == NULL)
- goto error;
- buffer = PyBytes_AS_STRING(big_buffer);
- memcpy(buffer, small_buffer, nfilled);
- }
- else {
- /* Grow the big buffer */
- if (_PyBytes_Resize(&big_buffer, buffersize) < 0){
- big_buffer = NULL;
- goto error;
- }
- buffer = PyBytes_AS_STRING(big_buffer);
- }
- continue;
- }
- end = buffer+nfilled+nread;
- q = buffer;
- while (p != NULL) {
- /* Process complete lines */
- p++;
- line = PyBytes_FromStringAndSize(q, p-q);
- if (line == NULL)
- goto error;
- err = PyList_Append(list, line);
- Py_DECREF(line);
- if (err != 0)
- goto error;
- q = p;
- p = memchr(q, '\n', end-q);
- }
- /* Move the remaining incomplete line to the start */
- nfilled = end-q;
- memmove(buffer, q, nfilled);
- if (sizehint > 0)
- if (totalread >= (size_t)sizehint)
- break;
- if (shortread) {
- sizehint = 0;
- break;
- }
- }
- if (nfilled != 0) {
- /* Partial last line */
- line = PyBytes_FromStringAndSize(buffer, nfilled);
- if (line == NULL)
- goto error;
- if (sizehint > 0) {
- /* Need to complete the last line */
- PyObject *rest = Util_GetLine(self, 0);
- if (rest == NULL) {
- Py_DECREF(line);
- goto error;
- }
- PyBytes_Concat(&line, rest);
- Py_DECREF(rest);
- if (line == NULL)
- goto error;
- }
- err = PyList_Append(list, line);
- Py_DECREF(line);
- if (err != 0)
- goto error;
- }
+ long sizehint = 0;
+ PyObject *list = NULL;
+ PyObject *line;
+ char small_buffer[SMALLCHUNK];
+ char *buffer = small_buffer;
+ size_t buffersize = SMALLCHUNK;
+ PyObject *big_buffer = NULL;
+ size_t nfilled = 0;
+ size_t nread;
+ size_t totalread = 0;
+ char *p, *q, *end;
+ int err;
+ int shortread = 0;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ break;
+ case MODE_READ_EOF:
+ list = PyList_New(0);
+ goto cleanup;
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for reading");
+ goto cleanup;
+ }
+
+ if ((list = PyList_New(0)) == NULL)
+ goto cleanup;
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ nread = BZ2_bzRead(&bzerror, self->fp,
+ buffer+nfilled, buffersize-nfilled);
+ self->pos += nread;
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ if (nread == 0) {
+ sizehint = 0;
+ break;
+ }
+ shortread = 1;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ error:
+ Py_DECREF(list);
+ list = NULL;
+ goto cleanup;
+ }
+ totalread += nread;
+ p = memchr(buffer+nfilled, '\n', nread);
+ if (!shortread && p == NULL) {
+ /* Need a larger buffer to fit this line */
+ nfilled += nread;
+ buffersize *= 2;
+ if (buffersize > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "line is longer than a Python string can hold");
+ goto error;
+ }
+ if (big_buffer == NULL) {
+ /* Create the big buffer */
+ big_buffer = PyBytes_FromStringAndSize(
+ NULL, buffersize);
+ if (big_buffer == NULL)
+ goto error;
+ buffer = PyBytes_AS_STRING(big_buffer);
+ memcpy(buffer, small_buffer, nfilled);
+ }
+ else {
+ /* Grow the big buffer */
+ if (_PyBytes_Resize(&big_buffer, buffersize) < 0){
+ big_buffer = NULL;
+ goto error;
+ }
+ buffer = PyBytes_AS_STRING(big_buffer);
+ }
+ continue;
+ }
+ end = buffer+nfilled+nread;
+ q = buffer;
+ while (p != NULL) {
+ /* Process complete lines */
+ p++;
+ line = PyBytes_FromStringAndSize(q, p-q);
+ if (line == NULL)
+ goto error;
+ err = PyList_Append(list, line);
+ Py_DECREF(line);
+ if (err != 0)
+ goto error;
+ q = p;
+ p = memchr(q, '\n', end-q);
+ }
+ /* Move the remaining incomplete line to the start */
+ nfilled = end-q;
+ memmove(buffer, q, nfilled);
+ if (sizehint > 0)
+ if (totalread >= (size_t)sizehint)
+ break;
+ if (shortread) {
+ sizehint = 0;
+ break;
+ }
+ }
+ if (nfilled != 0) {
+ /* Partial last line */
+ line = PyBytes_FromStringAndSize(buffer, nfilled);
+ if (line == NULL)
+ goto error;
+ if (sizehint > 0) {
+ /* Need to complete the last line */
+ PyObject *rest = Util_GetLine(self, 0);
+ if (rest == NULL) {
+ Py_DECREF(line);
+ goto error;
+ }
+ PyBytes_Concat(&line, rest);
+ Py_DECREF(rest);
+ if (line == NULL)
+ goto error;
+ }
+ err = PyList_Append(list, line);
+ Py_DECREF(line);
+ if (err != 0)
+ goto error;
+ }
cleanup:
- RELEASE_LOCK(self);
- if (big_buffer) {
- Py_DECREF(big_buffer);
- }
- return list;
+ RELEASE_LOCK(self);
+ if (big_buffer) {
+ Py_DECREF(big_buffer);
+ }
+ return list;
}
PyDoc_STRVAR(BZ2File_write__doc__,
@@ -695,50 +695,50 @@ be needed before the file on disk reflects the data written.\n\
static PyObject *
BZ2File_write(BZ2FileObject *self, PyObject *args)
{
- PyObject *ret = NULL;
- Py_buffer pbuf;
- char *buf;
- int len;
- int bzerror;
-
- if (!PyArg_ParseTuple(args, "y*:write", &pbuf))
- return NULL;
- buf = pbuf.buf;
- len = pbuf.len;
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_WRITE:
- break;
-
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
-
- default:
- PyErr_SetString(PyExc_IOError,
- "file is not ready for writing");
- goto cleanup;
- }
-
- Py_BEGIN_ALLOW_THREADS
- BZ2_bzWrite (&bzerror, self->fp, buf, len);
- self->pos += len;
- Py_END_ALLOW_THREADS
-
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto cleanup;
- }
-
- Py_INCREF(Py_None);
- ret = Py_None;
+ PyObject *ret = NULL;
+ Py_buffer pbuf;
+ char *buf;
+ int len;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "y*:write", &pbuf))
+ return NULL;
+ buf = pbuf.buf;
+ len = pbuf.len;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_WRITE:
+ break;
+
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for writing");
+ goto cleanup;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ BZ2_bzWrite (&bzerror, self->fp, buf, len);
+ self->pos += len;
+ Py_END_ALLOW_THREADS
+
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+
+ Py_INCREF(Py_None);
+ ret = Py_None;
cleanup:
- PyBuffer_Release(&pbuf);
- RELEASE_LOCK(self);
- return ret;
+ PyBuffer_Release(&pbuf);
+ RELEASE_LOCK(self);
+ return ret;
}
PyDoc_STRVAR(BZ2File_writelines__doc__,
@@ -754,122 +754,122 @@ static PyObject *
BZ2File_writelines(BZ2FileObject *self, PyObject *seq)
{
#define CHUNKSIZE 1000
- PyObject *list = NULL;
- PyObject *iter = NULL;
- PyObject *ret = NULL;
- PyObject *line;
- int i, j, index, len, islist;
- int bzerror;
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_WRITE:
- break;
-
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto error;
-
- default:
- PyErr_SetString(PyExc_IOError,
- "file is not ready for writing");
- goto error;
- }
-
- islist = PyList_Check(seq);
- if (!islist) {
- iter = PyObject_GetIter(seq);
- if (iter == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "writelines() requires an iterable argument");
- goto error;
- }
- list = PyList_New(CHUNKSIZE);
- if (list == NULL)
- goto error;
- }
-
- /* Strategy: slurp CHUNKSIZE lines into a private list,
- checking that they are all strings, then write that list
- without holding the interpreter lock, then come back for more. */
- for (index = 0; ; index += CHUNKSIZE) {
- if (islist) {
- Py_XDECREF(list);
- list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
- if (list == NULL)
- goto error;
- j = PyList_GET_SIZE(list);
- }
- else {
- for (j = 0; j < CHUNKSIZE; j++) {
- line = PyIter_Next(iter);
- if (line == NULL) {
- if (PyErr_Occurred())
- goto error;
- break;
- }
- PyList_SetItem(list, j, line);
- }
- }
- if (j == 0)
- break;
-
- /* Check that all entries are indeed byte strings. If not,
- apply the same rules as for file.write() and
- convert the rets to strings. This is slow, but
- seems to be the only way since all conversion APIs
- could potentially execute Python code. */
- for (i = 0; i < j; i++) {
- PyObject *v = PyList_GET_ITEM(list, i);
- if (!PyBytes_Check(v)) {
- const char *buffer;
- Py_ssize_t len;
- if (PyObject_AsCharBuffer(v, &buffer, &len)) {
- PyErr_SetString(PyExc_TypeError,
- "writelines() "
- "argument must be "
- "a sequence of "
- "bytes objects");
- goto error;
- }
- line = PyBytes_FromStringAndSize(buffer,
- len);
- if (line == NULL)
- goto error;
- Py_DECREF(v);
- PyList_SET_ITEM(list, i, line);
- }
- }
-
- /* Since we are releasing the global lock, the
- following code may *not* execute Python code. */
- Py_BEGIN_ALLOW_THREADS
- for (i = 0; i < j; i++) {
- line = PyList_GET_ITEM(list, i);
- len = PyBytes_GET_SIZE(line);
- BZ2_bzWrite (&bzerror, self->fp,
- PyBytes_AS_STRING(line), len);
- if (bzerror != BZ_OK) {
- Py_BLOCK_THREADS
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
- }
- Py_END_ALLOW_THREADS
-
- if (j < CHUNKSIZE)
- break;
- }
-
- Py_INCREF(Py_None);
- ret = Py_None;
+ PyObject *list = NULL;
+ PyObject *iter = NULL;
+ PyObject *ret = NULL;
+ PyObject *line;
+ int i, j, index, len, islist;
+ int bzerror;
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_WRITE:
+ break;
+
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto error;
+
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "file is not ready for writing");
+ goto error;
+ }
+
+ islist = PyList_Check(seq);
+ if (!islist) {
+ iter = PyObject_GetIter(seq);
+ if (iter == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "writelines() requires an iterable argument");
+ goto error;
+ }
+ list = PyList_New(CHUNKSIZE);
+ if (list == NULL)
+ goto error;
+ }
+
+ /* Strategy: slurp CHUNKSIZE lines into a private list,
+ checking that they are all strings, then write that list
+ without holding the interpreter lock, then come back for more. */
+ for (index = 0; ; index += CHUNKSIZE) {
+ if (islist) {
+ Py_XDECREF(list);
+ list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
+ if (list == NULL)
+ goto error;
+ j = PyList_GET_SIZE(list);
+ }
+ else {
+ for (j = 0; j < CHUNKSIZE; j++) {
+ line = PyIter_Next(iter);
+ if (line == NULL) {
+ if (PyErr_Occurred())
+ goto error;
+ break;
+ }
+ PyList_SetItem(list, j, line);
+ }
+ }
+ if (j == 0)
+ break;
+
+ /* Check that all entries are indeed byte strings. If not,
+ apply the same rules as for file.write() and
+ convert the rets to strings. This is slow, but
+ seems to be the only way since all conversion APIs
+ could potentially execute Python code. */
+ for (i = 0; i < j; i++) {
+ PyObject *v = PyList_GET_ITEM(list, i);
+ if (!PyBytes_Check(v)) {
+ const char *buffer;
+ Py_ssize_t len;
+ if (PyObject_AsCharBuffer(v, &buffer, &len)) {
+ PyErr_SetString(PyExc_TypeError,
+ "writelines() "
+ "argument must be "
+ "a sequence of "
+ "bytes objects");
+ goto error;
+ }
+ line = PyBytes_FromStringAndSize(buffer,
+ len);
+ if (line == NULL)
+ goto error;
+ Py_DECREF(v);
+ PyList_SET_ITEM(list, i, line);
+ }
+ }
+
+ /* Since we are releasing the global lock, the
+ following code may *not* execute Python code. */
+ Py_BEGIN_ALLOW_THREADS
+ for (i = 0; i < j; i++) {
+ line = PyList_GET_ITEM(list, i);
+ len = PyBytes_GET_SIZE(line);
+ BZ2_bzWrite (&bzerror, self->fp,
+ PyBytes_AS_STRING(line), len);
+ if (bzerror != BZ_OK) {
+ Py_BLOCK_THREADS
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ }
+ Py_END_ALLOW_THREADS
+
+ if (j < CHUNKSIZE)
+ break;
+ }
+
+ Py_INCREF(Py_None);
+ ret = Py_None;
error:
- RELEASE_LOCK(self);
- Py_XDECREF(list);
- Py_XDECREF(iter);
- return ret;
+ RELEASE_LOCK(self);
+ Py_XDECREF(list);
+ Py_XDECREF(iter);
+ return ret;
#undef CHUNKSIZE
}
@@ -889,135 +889,135 @@ the operation may be extremely slow.\n\
static PyObject *
BZ2File_seek(BZ2FileObject *self, PyObject *args)
{
- int where = 0;
- PyObject *offobj;
- Py_off_t offset;
- char small_buffer[SMALLCHUNK];
- char *buffer = small_buffer;
- size_t buffersize = SMALLCHUNK;
- Py_off_t bytesread = 0;
- size_t readsize;
- int chunksize;
- int bzerror;
- PyObject *ret = NULL;
-
- if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &where))
- return NULL;
+ int where = 0;
+ PyObject *offobj;
+ Py_off_t offset;
+ char small_buffer[SMALLCHUNK];
+ char *buffer = small_buffer;
+ size_t buffersize = SMALLCHUNK;
+ Py_off_t bytesread = 0;
+ size_t readsize;
+ int chunksize;
+ int bzerror;
+ PyObject *ret = NULL;
+
+ if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &where))
+ return NULL;
#if !defined(HAVE_LARGEFILE_SUPPORT)
- offset = PyLong_AsLong(offobj);
+ offset = PyLong_AsLong(offobj);
#else
- offset = PyLong_Check(offobj) ?
- PyLong_AsLongLong(offobj) : PyLong_AsLong(offobj);
+ offset = PyLong_Check(offobj) ?
+ PyLong_AsLongLong(offobj) : PyLong_AsLong(offobj);
#endif
- if (PyErr_Occurred())
- return NULL;
-
- ACQUIRE_LOCK(self);
- Util_DropReadAhead(self);
- switch (self->mode) {
- case MODE_READ:
- case MODE_READ_EOF:
- break;
-
- case MODE_CLOSED:
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
-
- default:
- PyErr_SetString(PyExc_IOError,
- "seek works only while reading");
- goto cleanup;
- }
-
- if (where == 2) {
- if (self->size == -1) {
- assert(self->mode != MODE_READ_EOF);
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- chunksize = BZ2_bzRead(&bzerror, self->fp,
- buffer, buffersize);
- self->pos += chunksize;
- Py_END_ALLOW_THREADS
-
- bytesread += chunksize;
- if (bzerror == BZ_STREAM_END) {
- break;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto cleanup;
- }
- }
- self->mode = MODE_READ_EOF;
- self->size = self->pos;
- bytesread = 0;
- }
- offset = self->size + offset;
- } else if (where == 1) {
- offset = self->pos + offset;
- }
-
- /* Before getting here, offset must be the absolute position the file
- * pointer should be set to. */
-
- if (offset >= self->pos) {
- /* we can move forward */
- offset -= self->pos;
- } else {
- /* we cannot move back, so rewind the stream */
- BZ2_bzReadClose(&bzerror, self->fp);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto cleanup;
- }
- rewind(self->rawfp);
- self->pos = 0;
- self->fp = BZ2_bzReadOpen(&bzerror, self->rawfp,
- 0, 0, NULL, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto cleanup;
- }
- self->mode = MODE_READ;
- }
-
- if (offset <= 0 || self->mode == MODE_READ_EOF)
- goto exit;
-
- /* Before getting here, offset must be set to the number of bytes
- * to walk forward. */
- for (;;) {
- if (offset-bytesread > buffersize)
- readsize = buffersize;
- else
- /* offset might be wider that readsize, but the result
- * of the subtraction is bound by buffersize (see the
- * condition above). buffersize is 8192. */
- readsize = (size_t)(offset-bytesread);
- Py_BEGIN_ALLOW_THREADS
- chunksize = BZ2_bzRead(&bzerror, self->fp, buffer, readsize);
- self->pos += chunksize;
- Py_END_ALLOW_THREADS
- bytesread += chunksize;
- if (bzerror == BZ_STREAM_END) {
- self->size = self->pos;
- self->mode = MODE_READ_EOF;
- break;
- } else if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto cleanup;
- }
- if (bytesread == offset)
- break;
- }
+ if (PyErr_Occurred())
+ return NULL;
+
+ ACQUIRE_LOCK(self);
+ Util_DropReadAhead(self);
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ break;
+
+ case MODE_CLOSED:
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+
+ default:
+ PyErr_SetString(PyExc_IOError,
+ "seek works only while reading");
+ goto cleanup;
+ }
+
+ if (where == 2) {
+ if (self->size == -1) {
+ assert(self->mode != MODE_READ_EOF);
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = BZ2_bzRead(&bzerror, self->fp,
+ buffer, buffersize);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ }
+ self->mode = MODE_READ_EOF;
+ self->size = self->pos;
+ bytesread = 0;
+ }
+ offset = self->size + offset;
+ } else if (where == 1) {
+ offset = self->pos + offset;
+ }
+
+ /* Before getting here, offset must be the absolute position the file
+ * pointer should be set to. */
+
+ if (offset >= self->pos) {
+ /* we can move forward */
+ offset -= self->pos;
+ } else {
+ /* we cannot move back, so rewind the stream */
+ BZ2_bzReadClose(&bzerror, self->fp);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ rewind(self->rawfp);
+ self->pos = 0;
+ self->fp = BZ2_bzReadOpen(&bzerror, self->rawfp,
+ 0, 0, NULL, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ self->mode = MODE_READ;
+ }
+
+ if (offset <= 0 || self->mode == MODE_READ_EOF)
+ goto exit;
+
+ /* Before getting here, offset must be set to the number of bytes
+ * to walk forward. */
+ for (;;) {
+ if (offset-bytesread > buffersize)
+ readsize = buffersize;
+ else
+ /* offset might be wider that readsize, but the result
+ * of the subtraction is bound by buffersize (see the
+ * condition above). buffersize is 8192. */
+ readsize = (size_t)(offset-bytesread);
+ Py_BEGIN_ALLOW_THREADS
+ chunksize = BZ2_bzRead(&bzerror, self->fp, buffer, readsize);
+ self->pos += chunksize;
+ Py_END_ALLOW_THREADS
+ bytesread += chunksize;
+ if (bzerror == BZ_STREAM_END) {
+ self->size = self->pos;
+ self->mode = MODE_READ_EOF;
+ break;
+ } else if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto cleanup;
+ }
+ if (bytesread == offset)
+ break;
+ }
exit:
- Py_INCREF(Py_None);
- ret = Py_None;
+ Py_INCREF(Py_None);
+ ret = Py_None;
cleanup:
- RELEASE_LOCK(self);
- return ret;
+ RELEASE_LOCK(self);
+ return ret;
}
PyDoc_STRVAR(BZ2File_tell__doc__,
@@ -1029,22 +1029,22 @@ Return the current file position, an integer (may be a long integer).\n\
static PyObject *
BZ2File_tell(BZ2FileObject *self, PyObject *args)
{
- PyObject *ret = NULL;
+ PyObject *ret = NULL;
- if (self->mode == MODE_CLOSED) {
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- goto cleanup;
- }
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ goto cleanup;
+ }
#if !defined(HAVE_LARGEFILE_SUPPORT)
- ret = PyLong_FromLong(self->pos);
+ ret = PyLong_FromLong(self->pos);
#else
- ret = PyLong_FromLongLong(self->pos);
+ ret = PyLong_FromLongLong(self->pos);
#endif
cleanup:
- return ret;
+ return ret;
}
PyDoc_STRVAR(BZ2File_close__doc__,
@@ -1058,37 +1058,37 @@ than once without error.\n\
static PyObject *
BZ2File_close(BZ2FileObject *self)
{
- PyObject *ret = NULL;
- int bzerror = BZ_OK;
-
- if (self->mode == MODE_CLOSED) {
- Py_RETURN_NONE;
- }
-
- ACQUIRE_LOCK(self);
- switch (self->mode) {
- case MODE_READ:
- case MODE_READ_EOF:
- BZ2_bzReadClose(&bzerror, self->fp);
- break;
- case MODE_WRITE:
- BZ2_bzWriteClose(&bzerror, self->fp,
- 0, NULL, NULL);
- break;
- }
- self->mode = MODE_CLOSED;
- fclose(self->rawfp);
- self->rawfp = NULL;
- if (bzerror == BZ_OK) {
- Py_INCREF(Py_None);
- ret = Py_None;
- }
- else {
- Util_CatchBZ2Error(bzerror);
- }
-
- RELEASE_LOCK(self);
- return ret;
+ PyObject *ret = NULL;
+ int bzerror = BZ_OK;
+
+ if (self->mode == MODE_CLOSED) {
+ Py_RETURN_NONE;
+ }
+
+ ACQUIRE_LOCK(self);
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ BZ2_bzReadClose(&bzerror, self->fp);
+ break;
+ case MODE_WRITE:
+ BZ2_bzWriteClose(&bzerror, self->fp,
+ 0, NULL, NULL);
+ break;
+ }
+ self->mode = MODE_CLOSED;
+ fclose(self->rawfp);
+ self->rawfp = NULL;
+ if (bzerror == BZ_OK) {
+ Py_INCREF(Py_None);
+ ret = Py_None;
+ }
+ else {
+ Util_CatchBZ2Error(bzerror);
+ }
+
+ RELEASE_LOCK(self);
+ return ret;
}
PyDoc_STRVAR(BZ2File_enter_doc,
@@ -1097,13 +1097,13 @@ PyDoc_STRVAR(BZ2File_enter_doc,
static PyObject *
BZ2File_enter(BZ2FileObject *self)
{
- if (self->mode == MODE_CLOSED) {
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- return NULL;
- }
- Py_INCREF(self);
- return (PyObject *) self;
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ return NULL;
+ }
+ Py_INCREF(self);
+ return (PyObject *) self;
}
PyDoc_STRVAR(BZ2File_exit_doc,
@@ -1112,29 +1112,29 @@ PyDoc_STRVAR(BZ2File_exit_doc,
static PyObject *
BZ2File_exit(BZ2FileObject *self, PyObject *args)
{
- PyObject *ret = PyObject_CallMethod((PyObject *) self, "close", NULL);
- if (!ret)
- /* If error occurred, pass through */
- return NULL;
- Py_DECREF(ret);
- Py_RETURN_NONE;
+ PyObject *ret = PyObject_CallMethod((PyObject *) self, "close", NULL);
+ if (!ret)
+ /* If error occurred, pass through */
+ return NULL;
+ Py_DECREF(ret);
+ Py_RETURN_NONE;
}
static PyObject *BZ2File_getiter(BZ2FileObject *self);
static PyMethodDef BZ2File_methods[] = {
- {"read", (PyCFunction)BZ2File_read, METH_VARARGS, BZ2File_read__doc__},
- {"readline", (PyCFunction)BZ2File_readline, METH_VARARGS, BZ2File_readline__doc__},
- {"readlines", (PyCFunction)BZ2File_readlines, METH_VARARGS, BZ2File_readlines__doc__},
- {"write", (PyCFunction)BZ2File_write, METH_VARARGS, BZ2File_write__doc__},
- {"writelines", (PyCFunction)BZ2File_writelines, METH_O, BZ2File_writelines__doc__},
- {"seek", (PyCFunction)BZ2File_seek, METH_VARARGS, BZ2File_seek__doc__},
- {"tell", (PyCFunction)BZ2File_tell, METH_NOARGS, BZ2File_tell__doc__},
- {"close", (PyCFunction)BZ2File_close, METH_NOARGS, BZ2File_close__doc__},
- {"__enter__", (PyCFunction)BZ2File_enter, METH_NOARGS, BZ2File_enter_doc},
- {"__exit__", (PyCFunction)BZ2File_exit, METH_VARARGS, BZ2File_exit_doc},
- {NULL, NULL} /* sentinel */
+ {"read", (PyCFunction)BZ2File_read, METH_VARARGS, BZ2File_read__doc__},
+ {"readline", (PyCFunction)BZ2File_readline, METH_VARARGS, BZ2File_readline__doc__},
+ {"readlines", (PyCFunction)BZ2File_readlines, METH_VARARGS, BZ2File_readlines__doc__},
+ {"write", (PyCFunction)BZ2File_write, METH_VARARGS, BZ2File_write__doc__},
+ {"writelines", (PyCFunction)BZ2File_writelines, METH_O, BZ2File_writelines__doc__},
+ {"seek", (PyCFunction)BZ2File_seek, METH_VARARGS, BZ2File_seek__doc__},
+ {"tell", (PyCFunction)BZ2File_tell, METH_NOARGS, BZ2File_tell__doc__},
+ {"close", (PyCFunction)BZ2File_close, METH_NOARGS, BZ2File_close__doc__},
+ {"__enter__", (PyCFunction)BZ2File_enter, METH_NOARGS, BZ2File_enter_doc},
+ {"__exit__", (PyCFunction)BZ2File_exit, METH_VARARGS, BZ2File_exit_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -1144,13 +1144,13 @@ static PyMethodDef BZ2File_methods[] = {
static PyObject *
BZ2File_get_closed(BZ2FileObject *self, void *closure)
{
- return PyLong_FromLong(self->mode == MODE_CLOSED);
+ return PyLong_FromLong(self->mode == MODE_CLOSED);
}
static PyGetSetDef BZ2File_getset[] = {
- {"closed", (getter)BZ2File_get_closed, NULL,
- "True if the file is closed"},
- {NULL} /* Sentinel */
+ {"closed", (getter)BZ2File_get_closed, NULL,
+ "True if the file is closed"},
+ {NULL} /* Sentinel */
};
@@ -1160,151 +1160,151 @@ static PyGetSetDef BZ2File_getset[] = {
static int
BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
{
- static char *kwlist[] = {"filename", "mode", "buffering",
- "compresslevel", 0};
- PyObject *name_obj = NULL;
- char *name;
- char *mode = "r";
- int buffering = -1;
- int compresslevel = 9;
- int bzerror;
- int mode_char = 0;
-
- self->size = -1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sii:BZ2File",
- kwlist, PyUnicode_FSConverter, &name_obj,
- &mode, &buffering,
- &compresslevel))
- return -1;
-
- if (PyBytes_Check(name_obj))
- name = PyBytes_AsString(name_obj);
- else
- name = PyByteArray_AsString(name_obj);
- if (compresslevel < 1 || compresslevel > 9) {
- PyErr_SetString(PyExc_ValueError,
- "compresslevel must be between 1 and 9");
- Py_DECREF(name_obj);
- return -1;
- }
-
- for (;;) {
- int error = 0;
- switch (*mode) {
- case 'r':
- case 'w':
- if (mode_char)
- error = 1;
- mode_char = *mode;
- break;
-
- case 'b':
- break;
-
- default:
- error = 1;
- break;
- }
- if (error) {
- PyErr_Format(PyExc_ValueError,
- "invalid mode char %c", *mode);
- Py_DECREF(name_obj);
- return -1;
- }
- mode++;
- if (*mode == '\0')
- break;
- }
-
- if (mode_char == 0) {
- mode_char = 'r';
- }
-
- mode = (mode_char == 'r') ? "rb" : "wb";
-
- self->rawfp = fopen(name, mode);
- Py_DECREF(name_obj);
- if (self->rawfp == NULL) {
- PyErr_SetFromErrno(PyExc_IOError);
- return -1;
- }
- /* XXX Ignore buffering */
-
- /* From now on, we have stuff to dealloc, so jump to error label
- * instead of returning */
+ static char *kwlist[] = {"filename", "mode", "buffering",
+ "compresslevel", 0};
+ PyObject *name_obj = NULL;
+ char *name;
+ char *mode = "r";
+ int buffering = -1;
+ int compresslevel = 9;
+ int bzerror;
+ int mode_char = 0;
+
+ self->size = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sii:BZ2File",
+ kwlist, PyUnicode_FSConverter, &name_obj,
+ &mode, &buffering,
+ &compresslevel))
+ return -1;
+
+ if (PyBytes_Check(name_obj))
+ name = PyBytes_AsString(name_obj);
+ else
+ name = PyByteArray_AsString(name_obj);
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ Py_DECREF(name_obj);
+ return -1;
+ }
+
+ for (;;) {
+ int error = 0;
+ switch (*mode) {
+ case 'r':
+ case 'w':
+ if (mode_char)
+ error = 1;
+ mode_char = *mode;
+ break;
+
+ case 'b':
+ break;
+
+ default:
+ error = 1;
+ break;
+ }
+ if (error) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid mode char %c", *mode);
+ Py_DECREF(name_obj);
+ return -1;
+ }
+ mode++;
+ if (*mode == '\0')
+ break;
+ }
+
+ if (mode_char == 0) {
+ mode_char = 'r';
+ }
+
+ mode = (mode_char == 'r') ? "rb" : "wb";
+
+ self->rawfp = fopen(name, mode);
+ Py_DECREF(name_obj);
+ if (self->rawfp == NULL) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ /* XXX Ignore buffering */
+
+ /* From now on, we have stuff to dealloc, so jump to error label
+ * instead of returning */
#ifdef WITH_THREAD
- self->lock = PyThread_allocate_lock();
- if (!self->lock) {
- PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
- goto error;
- }
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock) {
+ PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
+ goto error;
+ }
#endif
- if (mode_char == 'r')
- self->fp = BZ2_bzReadOpen(&bzerror, self->rawfp,
- 0, 0, NULL, 0);
- else
- self->fp = BZ2_bzWriteOpen(&bzerror, self->rawfp,
- compresslevel, 0, 0);
+ if (mode_char == 'r')
+ self->fp = BZ2_bzReadOpen(&bzerror, self->rawfp,
+ 0, 0, NULL, 0);
+ else
+ self->fp = BZ2_bzWriteOpen(&bzerror, self->rawfp,
+ compresslevel, 0, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
- self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
+ self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
- return 0;
+ return 0;
error:
- fclose(self->rawfp);
- self->rawfp = NULL;
+ fclose(self->rawfp);
+ self->rawfp = NULL;
#ifdef WITH_THREAD
- if (self->lock) {
- PyThread_free_lock(self->lock);
- self->lock = NULL;
- }
+ if (self->lock) {
+ PyThread_free_lock(self->lock);
+ self->lock = NULL;
+ }
#endif
- return -1;
+ return -1;
}
static void
BZ2File_dealloc(BZ2FileObject *self)
{
- int bzerror;
+ int bzerror;
#ifdef WITH_THREAD
- if (self->lock)
- PyThread_free_lock(self->lock);
+ if (self->lock)
+ PyThread_free_lock(self->lock);
#endif
- switch (self->mode) {
- case MODE_READ:
- case MODE_READ_EOF:
- BZ2_bzReadClose(&bzerror, self->fp);
- break;
- case MODE_WRITE:
- BZ2_bzWriteClose(&bzerror, self->fp,
- 0, NULL, NULL);
- break;
- }
- Util_DropReadAhead(self);
- if (self->rawfp != NULL)
- fclose(self->rawfp);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ switch (self->mode) {
+ case MODE_READ:
+ case MODE_READ_EOF:
+ BZ2_bzReadClose(&bzerror, self->fp);
+ break;
+ case MODE_WRITE:
+ BZ2_bzWriteClose(&bzerror, self->fp,
+ 0, NULL, NULL);
+ break;
+ }
+ Util_DropReadAhead(self);
+ if (self->rawfp != NULL)
+ fclose(self->rawfp);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
/* This is a hacked version of Python's fileobject.c:file_getiter(). */
static PyObject *
BZ2File_getiter(BZ2FileObject *self)
{
- if (self->mode == MODE_CLOSED) {
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- return NULL;
- }
- Py_INCREF((PyObject*)self);
- return (PyObject *)self;
+ if (self->mode == MODE_CLOSED) {
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ return NULL;
+ }
+ Py_INCREF((PyObject*)self);
+ return (PyObject *)self;
}
/* This is a hacked version of Python's fileobject.c:file_iternext(). */
@@ -1312,21 +1312,21 @@ BZ2File_getiter(BZ2FileObject *self)
static PyObject *
BZ2File_iternext(BZ2FileObject *self)
{
- PyBytesObject* ret;
- ACQUIRE_LOCK(self);
- if (self->mode == MODE_CLOSED) {
- RELEASE_LOCK(self);
- PyErr_SetString(PyExc_ValueError,
- "I/O operation on closed file");
- return NULL;
- }
- ret = Util_ReadAheadGetLineSkip(self, 0, READAHEAD_BUFSIZE);
- RELEASE_LOCK(self);
- if (ret == NULL || PyBytes_GET_SIZE(ret) == 0) {
- Py_XDECREF(ret);
- return NULL;
- }
- return (PyObject *)ret;
+ PyBytesObject* ret;
+ ACQUIRE_LOCK(self);
+ if (self->mode == MODE_CLOSED) {
+ RELEASE_LOCK(self);
+ PyErr_SetString(PyExc_ValueError,
+ "I/O operation on closed file");
+ return NULL;
+ }
+ ret = Util_ReadAheadGetLineSkip(self, 0, READAHEAD_BUFSIZE);
+ RELEASE_LOCK(self);
+ if (ret == NULL || PyBytes_GET_SIZE(ret) == 0) {
+ Py_XDECREF(ret);
+ return NULL;
+ }
+ return (PyObject *)ret;
}
/* ===================================================================== */
@@ -1345,46 +1345,46 @@ Data read is always returned in bytes; data written ought to be bytes.\n\
");
static PyTypeObject BZ2File_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "bz2.BZ2File", /*tp_name*/
- sizeof(BZ2FileObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- (destructor)BZ2File_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- PyObject_GenericGetAttr,/*tp_getattro*/
- PyObject_GenericSetAttr,/*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
- BZ2File__doc__, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- (getiterfunc)BZ2File_getiter, /*tp_iter*/
- (iternextfunc)BZ2File_iternext, /*tp_iternext*/
- BZ2File_methods, /*tp_methods*/
- 0, /*tp_members*/
- BZ2File_getset, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- (initproc)BZ2File_init, /*tp_init*/
- PyType_GenericAlloc, /*tp_alloc*/
- PyType_GenericNew, /*tp_new*/
- PyObject_Free, /*tp_free*/
- 0, /*tp_is_gc*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "bz2.BZ2File", /*tp_name*/
+ sizeof(BZ2FileObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2File_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2File__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ (getiterfunc)BZ2File_getiter, /*tp_iter*/
+ (iternextfunc)BZ2File_iternext, /*tp_iternext*/
+ BZ2File_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ BZ2File_getset, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2File_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ PyObject_Free, /*tp_free*/
+ 0, /*tp_is_gc*/
};
@@ -1403,78 +1403,78 @@ and return what is left in the internal buffers.\n\
static PyObject *
BZ2Comp_compress(BZ2CompObject *self, PyObject *args)
{
- Py_buffer pdata;
- char *data;
- int datasize;
- int bufsize = SMALLCHUNK;
- PY_LONG_LONG totalout;
- PyObject *ret = NULL;
- bz_stream *bzs = &self->bzs;
- int bzerror;
-
- if (!PyArg_ParseTuple(args, "y*:compress", &pdata))
- return NULL;
- data = pdata.buf;
- datasize = pdata.len;
-
- if (datasize == 0) {
- PyBuffer_Release(&pdata);
- return PyBytes_FromStringAndSize("", 0);
- }
-
- ACQUIRE_LOCK(self);
- if (!self->running) {
- PyErr_SetString(PyExc_ValueError,
- "this object was already flushed");
- goto error;
- }
-
- ret = PyBytes_FromStringAndSize(NULL, bufsize);
- if (!ret)
- goto error;
-
- bzs->next_in = data;
- bzs->avail_in = datasize;
- bzs->next_out = BUF(ret);
- bzs->avail_out = bufsize;
-
- totalout = BZS_TOTAL_OUT(bzs);
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- bzerror = BZ2_bzCompress(bzs, BZ_RUN);
- Py_END_ALLOW_THREADS
- if (bzerror != BZ_RUN_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
- if (bzs->avail_in == 0)
- break; /* no more input data */
- if (bzs->avail_out == 0) {
- bufsize = Util_NewBufferSize(bufsize);
- if (_PyBytes_Resize(&ret, bufsize) < 0) {
- BZ2_bzCompressEnd(bzs);
- goto error;
- }
- bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
- - totalout);
- bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
- }
- }
-
- if (_PyBytes_Resize(&ret,
- (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
- goto error;
-
- RELEASE_LOCK(self);
- PyBuffer_Release(&pdata);
- return ret;
+ Py_buffer pdata;
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PY_LONG_LONG totalout;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "y*:compress", &pdata))
+ return NULL;
+ data = pdata.buf;
+ datasize = pdata.len;
+
+ if (datasize == 0) {
+ PyBuffer_Release(&pdata);
+ return PyBytes_FromStringAndSize("", 0);
+ }
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_ValueError,
+ "this object was already flushed");
+ goto error;
+ }
+
+ ret = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_RUN);
+ Py_END_ALLOW_THREADS
+ if (bzerror != BZ_RUN_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_in == 0)
+ break; /* no more input data */
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyBytes_Resize(&ret, bufsize) < 0) {
+ BZ2_bzCompressEnd(bzs);
+ goto error;
+ }
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (_PyBytes_Resize(&ret,
+ (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
+ goto error;
+
+ RELEASE_LOCK(self);
+ PyBuffer_Release(&pdata);
+ return ret;
error:
- RELEASE_LOCK(self);
- PyBuffer_Release(&pdata);
- Py_XDECREF(ret);
- return NULL;
+ RELEASE_LOCK(self);
+ PyBuffer_Release(&pdata);
+ Py_XDECREF(ret);
+ return NULL;
}
PyDoc_STRVAR(BZ2Comp_flush__doc__,
@@ -1487,71 +1487,71 @@ You must not use the compressor object after calling this method.\n\
static PyObject *
BZ2Comp_flush(BZ2CompObject *self)
{
- int bufsize = SMALLCHUNK;
- PyObject *ret = NULL;
- bz_stream *bzs = &self->bzs;
- PY_LONG_LONG totalout;
- int bzerror;
-
- ACQUIRE_LOCK(self);
- if (!self->running) {
- PyErr_SetString(PyExc_ValueError, "object was already "
- "flushed");
- goto error;
- }
- self->running = 0;
-
- ret = PyBytes_FromStringAndSize(NULL, bufsize);
- if (!ret)
- goto error;
-
- bzs->next_out = BUF(ret);
- bzs->avail_out = bufsize;
-
- totalout = BZS_TOTAL_OUT(bzs);
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- break;
- } else if (bzerror != BZ_FINISH_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
- if (bzs->avail_out == 0) {
- bufsize = Util_NewBufferSize(bufsize);
- if (_PyBytes_Resize(&ret, bufsize) < 0)
- goto error;
- bzs->next_out = BUF(ret);
- bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
- - totalout);
- bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
- }
- }
-
- if (bzs->avail_out != 0) {
- if (_PyBytes_Resize(&ret,
- (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
- goto error;
- }
-
- RELEASE_LOCK(self);
- return ret;
+ int bufsize = SMALLCHUNK;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ PY_LONG_LONG totalout;
+ int bzerror;
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_ValueError, "object was already "
+ "flushed");
+ goto error;
+ }
+ self->running = 0;
+
+ ret = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_FINISH_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyBytes_Resize(&ret, bufsize) < 0)
+ goto error;
+ bzs->next_out = BUF(ret);
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0) {
+ if (_PyBytes_Resize(&ret,
+ (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
+ goto error;
+ }
+
+ RELEASE_LOCK(self);
+ return ret;
error:
- RELEASE_LOCK(self);
- Py_XDECREF(ret);
- return NULL;
+ RELEASE_LOCK(self);
+ Py_XDECREF(ret);
+ return NULL;
}
static PyMethodDef BZ2Comp_methods[] = {
- {"compress", (PyCFunction)BZ2Comp_compress, METH_VARARGS,
- BZ2Comp_compress__doc__},
- {"flush", (PyCFunction)BZ2Comp_flush, METH_NOARGS,
- BZ2Comp_flush__doc__},
- {NULL, NULL} /* sentinel */
+ {"compress", (PyCFunction)BZ2Comp_compress, METH_VARARGS,
+ BZ2Comp_compress__doc__},
+ {"flush", (PyCFunction)BZ2Comp_flush, METH_NOARGS,
+ BZ2Comp_flush__doc__},
+ {NULL, NULL} /* sentinel */
};
@@ -1561,57 +1561,57 @@ static PyMethodDef BZ2Comp_methods[] = {
static int
BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
{
- int compresslevel = 9;
- int bzerror;
- static char *kwlist[] = {"compresslevel", 0};
+ int compresslevel = 9;
+ int bzerror;
+ static char *kwlist[] = {"compresslevel", 0};
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:BZ2Compressor",
- kwlist, &compresslevel))
- return -1;
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:BZ2Compressor",
+ kwlist, &compresslevel))
+ return -1;
- if (compresslevel < 1 || compresslevel > 9) {
- PyErr_SetString(PyExc_ValueError,
- "compresslevel must be between 1 and 9");
- goto error;
- }
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ goto error;
+ }
#ifdef WITH_THREAD
- self->lock = PyThread_allocate_lock();
- if (!self->lock) {
- PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
- goto error;
- }
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock) {
+ PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
+ goto error;
+ }
#endif
- memset(&self->bzs, 0, sizeof(bz_stream));
- bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
+ memset(&self->bzs, 0, sizeof(bz_stream));
+ bzerror = BZ2_bzCompressInit(&self->bzs, compresslevel, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
- self->running = 1;
+ self->running = 1;
- return 0;
+ return 0;
error:
#ifdef WITH_THREAD
- if (self->lock) {
- PyThread_free_lock(self->lock);
- self->lock = NULL;
- }
+ if (self->lock) {
+ PyThread_free_lock(self->lock);
+ self->lock = NULL;
+ }
#endif
- return -1;
+ return -1;
}
static void
BZ2Comp_dealloc(BZ2CompObject *self)
{
#ifdef WITH_THREAD
- if (self->lock)
- PyThread_free_lock(self->lock);
+ if (self->lock)
+ PyThread_free_lock(self->lock);
#endif
- BZ2_bzCompressEnd(&self->bzs);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ BZ2_bzCompressEnd(&self->bzs);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
@@ -1628,46 +1628,46 @@ must be a number between 1 and 9.\n\
");
static PyTypeObject BZ2Comp_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "bz2.BZ2Compressor", /*tp_name*/
- sizeof(BZ2CompObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- (destructor)BZ2Comp_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- PyObject_GenericGetAttr,/*tp_getattro*/
- PyObject_GenericSetAttr,/*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
- BZ2Comp__doc__, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- BZ2Comp_methods, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- (initproc)BZ2Comp_init, /*tp_init*/
- PyType_GenericAlloc, /*tp_alloc*/
- PyType_GenericNew, /*tp_new*/
- PyObject_Free, /*tp_free*/
- 0, /*tp_is_gc*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "bz2.BZ2Compressor", /*tp_name*/
+ sizeof(BZ2CompObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2Comp_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2Comp__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ BZ2Comp_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2Comp_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ PyObject_Free, /*tp_free*/
+ 0, /*tp_is_gc*/
};
@@ -1678,8 +1678,8 @@ static PyTypeObject BZ2Comp_Type = {
#define OFF(x) offsetof(BZ2DecompObject, x)
static PyMemberDef BZ2Decomp_members[] = {
- {"unused_data", T_OBJECT, OFF(unused_data), READONLY},
- {NULL} /* Sentinel */
+ {"unused_data", T_OBJECT, OFF(unused_data), READONLY},
+ {NULL} /* Sentinel */
};
@@ -1699,91 +1699,91 @@ unused_data attribute.\n\
static PyObject *
BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args)
{
- Py_buffer pdata;
- char *data;
- int datasize;
- int bufsize = SMALLCHUNK;
- PY_LONG_LONG totalout;
- PyObject *ret = NULL;
- bz_stream *bzs = &self->bzs;
- int bzerror;
-
- if (!PyArg_ParseTuple(args, "y*:decompress", &pdata))
- return NULL;
- data = pdata.buf;
- datasize = pdata.len;
-
- ACQUIRE_LOCK(self);
- if (!self->running) {
- PyErr_SetString(PyExc_EOFError, "end of stream was "
- "already found");
- goto error;
- }
-
- ret = PyBytes_FromStringAndSize(NULL, bufsize);
- if (!ret)
- goto error;
-
- bzs->next_in = data;
- bzs->avail_in = datasize;
- bzs->next_out = BUF(ret);
- bzs->avail_out = bufsize;
-
- totalout = BZS_TOTAL_OUT(bzs);
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- bzerror = BZ2_bzDecompress(bzs);
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- if (bzs->avail_in != 0) {
- Py_DECREF(self->unused_data);
- self->unused_data =
- PyBytes_FromStringAndSize(bzs->next_in,
- bzs->avail_in);
- }
- self->running = 0;
- break;
- }
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
- if (bzs->avail_in == 0)
- break; /* no more input data */
- if (bzs->avail_out == 0) {
- bufsize = Util_NewBufferSize(bufsize);
- if (_PyBytes_Resize(&ret, bufsize) < 0) {
- BZ2_bzDecompressEnd(bzs);
- goto error;
- }
- bzs->next_out = BUF(ret);
- bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
- - totalout);
- bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
- }
- }
-
- if (bzs->avail_out != 0) {
- if (_PyBytes_Resize(&ret,
- (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
- goto error;
- }
-
- RELEASE_LOCK(self);
- PyBuffer_Release(&pdata);
- return ret;
+ Py_buffer pdata;
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PY_LONG_LONG totalout;
+ PyObject *ret = NULL;
+ bz_stream *bzs = &self->bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "y*:decompress", &pdata))
+ return NULL;
+ data = pdata.buf;
+ datasize = pdata.len;
+
+ ACQUIRE_LOCK(self);
+ if (!self->running) {
+ PyErr_SetString(PyExc_EOFError, "end of stream was "
+ "already found");
+ goto error;
+ }
+
+ ret = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (!ret)
+ goto error;
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ totalout = BZS_TOTAL_OUT(bzs);
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzDecompress(bzs);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ if (bzs->avail_in != 0) {
+ Py_DECREF(self->unused_data);
+ self->unused_data =
+ PyBytes_FromStringAndSize(bzs->next_in,
+ bzs->avail_in);
+ }
+ self->running = 0;
+ break;
+ }
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
+ if (bzs->avail_in == 0)
+ break; /* no more input data */
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyBytes_Resize(&ret, bufsize) < 0) {
+ BZ2_bzDecompressEnd(bzs);
+ goto error;
+ }
+ bzs->next_out = BUF(ret);
+ bzs->next_out = BUF(ret) + (BZS_TOTAL_OUT(bzs)
+ - totalout);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0) {
+ if (_PyBytes_Resize(&ret,
+ (Py_ssize_t)(BZS_TOTAL_OUT(bzs) - totalout)) < 0)
+ goto error;
+ }
+
+ RELEASE_LOCK(self);
+ PyBuffer_Release(&pdata);
+ return ret;
error:
- RELEASE_LOCK(self);
- PyBuffer_Release(&pdata);
- Py_XDECREF(ret);
- return NULL;
+ RELEASE_LOCK(self);
+ PyBuffer_Release(&pdata);
+ Py_XDECREF(ret);
+ return NULL;
}
static PyMethodDef BZ2Decomp_methods[] = {
- {"decompress", (PyCFunction)BZ2Decomp_decompress, METH_VARARGS, BZ2Decomp_decompress__doc__},
- {NULL, NULL} /* sentinel */
+ {"decompress", (PyCFunction)BZ2Decomp_decompress, METH_VARARGS, BZ2Decomp_decompress__doc__},
+ {NULL, NULL} /* sentinel */
};
@@ -1793,55 +1793,55 @@ static PyMethodDef BZ2Decomp_methods[] = {
static int
BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
{
- int bzerror;
+ int bzerror;
- if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
- return -1;
+ if (!PyArg_ParseTuple(args, ":BZ2Decompressor"))
+ return -1;
#ifdef WITH_THREAD
- self->lock = PyThread_allocate_lock();
- if (!self->lock) {
- PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
- goto error;
- }
+ self->lock = PyThread_allocate_lock();
+ if (!self->lock) {
+ PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
+ goto error;
+ }
#endif
- self->unused_data = PyBytes_FromStringAndSize("", 0);
- if (!self->unused_data)
- goto error;
+ self->unused_data = PyBytes_FromStringAndSize("", 0);
+ if (!self->unused_data)
+ goto error;
- memset(&self->bzs, 0, sizeof(bz_stream));
- bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- goto error;
- }
+ memset(&self->bzs, 0, sizeof(bz_stream));
+ bzerror = BZ2_bzDecompressInit(&self->bzs, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ goto error;
+ }
- self->running = 1;
+ self->running = 1;
- return 0;
+ return 0;
error:
#ifdef WITH_THREAD
- if (self->lock) {
- PyThread_free_lock(self->lock);
- self->lock = NULL;
- }
+ if (self->lock) {
+ PyThread_free_lock(self->lock);
+ self->lock = NULL;
+ }
#endif
- Py_CLEAR(self->unused_data);
- return -1;
+ Py_CLEAR(self->unused_data);
+ return -1;
}
static void
BZ2Decomp_dealloc(BZ2DecompObject *self)
{
#ifdef WITH_THREAD
- if (self->lock)
- PyThread_free_lock(self->lock);
+ if (self->lock)
+ PyThread_free_lock(self->lock);
#endif
- Py_XDECREF(self->unused_data);
- BZ2_bzDecompressEnd(&self->bzs);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ Py_XDECREF(self->unused_data);
+ BZ2_bzDecompressEnd(&self->bzs);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
@@ -1857,46 +1857,46 @@ decompress() function instead.\n\
");
static PyTypeObject BZ2Decomp_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "bz2.BZ2Decompressor", /*tp_name*/
- sizeof(BZ2DecompObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- (destructor)BZ2Decomp_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- PyObject_GenericGetAttr,/*tp_getattro*/
- PyObject_GenericSetAttr,/*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
- BZ2Decomp__doc__, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- BZ2Decomp_methods, /*tp_methods*/
- BZ2Decomp_members, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- (initproc)BZ2Decomp_init, /*tp_init*/
- PyType_GenericAlloc, /*tp_alloc*/
- PyType_GenericNew, /*tp_new*/
- PyObject_Free, /*tp_free*/
- 0, /*tp_is_gc*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "bz2.BZ2Decompressor", /*tp_name*/
+ sizeof(BZ2DecompObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor)BZ2Decomp_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr,/*tp_getattro*/
+ PyObject_GenericSetAttr,/*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ BZ2Decomp__doc__, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ BZ2Decomp_methods, /*tp_methods*/
+ BZ2Decomp_members, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ (initproc)BZ2Decomp_init, /*tp_init*/
+ PyType_GenericAlloc, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ PyObject_Free, /*tp_free*/
+ 0, /*tp_is_gc*/
};
@@ -1914,90 +1914,90 @@ given, must be a number between 1 and 9.\n\
static PyObject *
bz2_compress(PyObject *self, PyObject *args, PyObject *kwargs)
{
- int compresslevel=9;
- Py_buffer pdata;
- char *data;
- int datasize;
- int bufsize;
- PyObject *ret = NULL;
- bz_stream _bzs;
- bz_stream *bzs = &_bzs;
- int bzerror;
- static char *kwlist[] = {"data", "compresslevel", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i",
- kwlist, &pdata,
- &compresslevel))
- return NULL;
- data = pdata.buf;
- datasize = pdata.len;
-
- if (compresslevel < 1 || compresslevel > 9) {
- PyErr_SetString(PyExc_ValueError,
- "compresslevel must be between 1 and 9");
- PyBuffer_Release(&pdata);
- return NULL;
- }
-
- /* Conforming to bz2 manual, this is large enough to fit compressed
- * data in one shot. We will check it later anyway. */
- bufsize = datasize + (datasize/100+1) + 600;
-
- ret = PyBytes_FromStringAndSize(NULL, bufsize);
- if (!ret) {
- PyBuffer_Release(&pdata);
- return NULL;
- }
-
- memset(bzs, 0, sizeof(bz_stream));
-
- bzs->next_in = data;
- bzs->avail_in = datasize;
- bzs->next_out = BUF(ret);
- bzs->avail_out = bufsize;
-
- bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- PyBuffer_Release(&pdata);
- Py_DECREF(ret);
- return NULL;
- }
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- break;
- } else if (bzerror != BZ_FINISH_OK) {
- BZ2_bzCompressEnd(bzs);
- Util_CatchBZ2Error(bzerror);
- PyBuffer_Release(&pdata);
- Py_DECREF(ret);
- return NULL;
- }
- if (bzs->avail_out == 0) {
- bufsize = Util_NewBufferSize(bufsize);
- if (_PyBytes_Resize(&ret, bufsize) < 0) {
- BZ2_bzCompressEnd(bzs);
- PyBuffer_Release(&pdata);
- return NULL;
- }
- bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
- bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
- }
- }
-
- if (bzs->avail_out != 0) {
- if (_PyBytes_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) {
- ret = NULL;
- }
- }
- BZ2_bzCompressEnd(bzs);
-
- PyBuffer_Release(&pdata);
- return ret;
+ int compresslevel=9;
+ Py_buffer pdata;
+ char *data;
+ int datasize;
+ int bufsize;
+ PyObject *ret = NULL;
+ bz_stream _bzs;
+ bz_stream *bzs = &_bzs;
+ int bzerror;
+ static char *kwlist[] = {"data", "compresslevel", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i",
+ kwlist, &pdata,
+ &compresslevel))
+ return NULL;
+ data = pdata.buf;
+ datasize = pdata.len;
+
+ if (compresslevel < 1 || compresslevel > 9) {
+ PyErr_SetString(PyExc_ValueError,
+ "compresslevel must be between 1 and 9");
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+
+ /* Conforming to bz2 manual, this is large enough to fit compressed
+ * data in one shot. We will check it later anyway. */
+ bufsize = datasize + (datasize/100+1) + 600;
+
+ ret = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (!ret) {
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+
+ memset(bzs, 0, sizeof(bz_stream));
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ PyBuffer_Release(&pdata);
+ Py_DECREF(ret);
+ return NULL;
+ }
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzCompress(bzs, BZ_FINISH);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_FINISH_OK) {
+ BZ2_bzCompressEnd(bzs);
+ Util_CatchBZ2Error(bzerror);
+ PyBuffer_Release(&pdata);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyBytes_Resize(&ret, bufsize) < 0) {
+ BZ2_bzCompressEnd(bzs);
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+ bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0) {
+ if (_PyBytes_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) {
+ ret = NULL;
+ }
+ }
+ BZ2_bzCompressEnd(bzs);
+
+ PyBuffer_Release(&pdata);
+ return ret;
}
PyDoc_STRVAR(bz2_decompress__doc__,
@@ -2010,96 +2010,96 @@ use an instance of BZ2Decompressor instead.\n\
static PyObject *
bz2_decompress(PyObject *self, PyObject *args)
{
- Py_buffer pdata;
- char *data;
- int datasize;
- int bufsize = SMALLCHUNK;
- PyObject *ret;
- bz_stream _bzs;
- bz_stream *bzs = &_bzs;
- int bzerror;
-
- if (!PyArg_ParseTuple(args, "y*:decompress", &pdata))
- return NULL;
- data = pdata.buf;
- datasize = pdata.len;
-
- if (datasize == 0) {
- PyBuffer_Release(&pdata);
- return PyBytes_FromStringAndSize("", 0);
- }
-
- ret = PyBytes_FromStringAndSize(NULL, bufsize);
- if (!ret) {
- PyBuffer_Release(&pdata);
- return NULL;
- }
-
- memset(bzs, 0, sizeof(bz_stream));
-
- bzs->next_in = data;
- bzs->avail_in = datasize;
- bzs->next_out = BUF(ret);
- bzs->avail_out = bufsize;
-
- bzerror = BZ2_bzDecompressInit(bzs, 0, 0);
- if (bzerror != BZ_OK) {
- Util_CatchBZ2Error(bzerror);
- Py_DECREF(ret);
- PyBuffer_Release(&pdata);
- return NULL;
- }
-
- for (;;) {
- Py_BEGIN_ALLOW_THREADS
- bzerror = BZ2_bzDecompress(bzs);
- Py_END_ALLOW_THREADS
- if (bzerror == BZ_STREAM_END) {
- break;
- } else if (bzerror != BZ_OK) {
- BZ2_bzDecompressEnd(bzs);
- Util_CatchBZ2Error(bzerror);
- PyBuffer_Release(&pdata);
- Py_DECREF(ret);
- return NULL;
- }
- if (bzs->avail_in == 0) {
- BZ2_bzDecompressEnd(bzs);
- PyErr_SetString(PyExc_ValueError,
- "couldn't find end of stream");
- PyBuffer_Release(&pdata);
- Py_DECREF(ret);
- return NULL;
- }
- if (bzs->avail_out == 0) {
- bufsize = Util_NewBufferSize(bufsize);
- if (_PyBytes_Resize(&ret, bufsize) < 0) {
- BZ2_bzDecompressEnd(bzs);
- PyBuffer_Release(&pdata);
- return NULL;
- }
- bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
- bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
- }
- }
-
- if (bzs->avail_out != 0) {
- if (_PyBytes_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) {
- ret = NULL;
- }
- }
- BZ2_bzDecompressEnd(bzs);
- PyBuffer_Release(&pdata);
-
- return ret;
+ Py_buffer pdata;
+ char *data;
+ int datasize;
+ int bufsize = SMALLCHUNK;
+ PyObject *ret;
+ bz_stream _bzs;
+ bz_stream *bzs = &_bzs;
+ int bzerror;
+
+ if (!PyArg_ParseTuple(args, "y*:decompress", &pdata))
+ return NULL;
+ data = pdata.buf;
+ datasize = pdata.len;
+
+ if (datasize == 0) {
+ PyBuffer_Release(&pdata);
+ return PyBytes_FromStringAndSize("", 0);
+ }
+
+ ret = PyBytes_FromStringAndSize(NULL, bufsize);
+ if (!ret) {
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+
+ memset(bzs, 0, sizeof(bz_stream));
+
+ bzs->next_in = data;
+ bzs->avail_in = datasize;
+ bzs->next_out = BUF(ret);
+ bzs->avail_out = bufsize;
+
+ bzerror = BZ2_bzDecompressInit(bzs, 0, 0);
+ if (bzerror != BZ_OK) {
+ Util_CatchBZ2Error(bzerror);
+ Py_DECREF(ret);
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+
+ for (;;) {
+ Py_BEGIN_ALLOW_THREADS
+ bzerror = BZ2_bzDecompress(bzs);
+ Py_END_ALLOW_THREADS
+ if (bzerror == BZ_STREAM_END) {
+ break;
+ } else if (bzerror != BZ_OK) {
+ BZ2_bzDecompressEnd(bzs);
+ Util_CatchBZ2Error(bzerror);
+ PyBuffer_Release(&pdata);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ if (bzs->avail_in == 0) {
+ BZ2_bzDecompressEnd(bzs);
+ PyErr_SetString(PyExc_ValueError,
+ "couldn't find end of stream");
+ PyBuffer_Release(&pdata);
+ Py_DECREF(ret);
+ return NULL;
+ }
+ if (bzs->avail_out == 0) {
+ bufsize = Util_NewBufferSize(bufsize);
+ if (_PyBytes_Resize(&ret, bufsize) < 0) {
+ BZ2_bzDecompressEnd(bzs);
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+ bzs->next_out = BUF(ret) + BZS_TOTAL_OUT(bzs);
+ bzs->avail_out = bufsize - (bzs->next_out - BUF(ret));
+ }
+ }
+
+ if (bzs->avail_out != 0) {
+ if (_PyBytes_Resize(&ret, (Py_ssize_t)BZS_TOTAL_OUT(bzs)) < 0) {
+ ret = NULL;
+ }
+ }
+ BZ2_bzDecompressEnd(bzs);
+ PyBuffer_Release(&pdata);
+
+ return ret;
}
static PyMethodDef bz2_methods[] = {
- {"compress", (PyCFunction) bz2_compress, METH_VARARGS|METH_KEYWORDS,
- bz2_compress__doc__},
- {"decompress", (PyCFunction) bz2_decompress, METH_VARARGS,
- bz2_decompress__doc__},
- {NULL, NULL} /* sentinel */
+ {"compress", (PyCFunction) bz2_compress, METH_VARARGS|METH_KEYWORDS,
+ bz2_compress__doc__},
+ {"decompress", (PyCFunction) bz2_decompress, METH_VARARGS,
+ bz2_decompress__doc__},
+ {NULL, NULL} /* sentinel */
};
/* ===================================================================== */
@@ -2114,39 +2114,39 @@ sequential (de)compression.\n\
static struct PyModuleDef bz2module = {
- PyModuleDef_HEAD_INIT,
- "bz2",
- bz2__doc__,
- -1,
- bz2_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "bz2",
+ bz2__doc__,
+ -1,
+ bz2_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_bz2(void)
{
- PyObject *m;
+ PyObject *m;
- Py_TYPE(&BZ2File_Type) = &PyType_Type;
- Py_TYPE(&BZ2Comp_Type) = &PyType_Type;
- Py_TYPE(&BZ2Decomp_Type) = &PyType_Type;
+ Py_TYPE(&BZ2File_Type) = &PyType_Type;
+ Py_TYPE(&BZ2Comp_Type) = &PyType_Type;
+ Py_TYPE(&BZ2Decomp_Type) = &PyType_Type;
- m = PyModule_Create(&bz2module);
- if (m == NULL)
- return NULL;
+ m = PyModule_Create(&bz2module);
+ if (m == NULL)
+ return NULL;
- PyModule_AddObject(m, "__author__", PyUnicode_FromString(__author__));
+ PyModule_AddObject(m, "__author__", PyUnicode_FromString(__author__));
- Py_INCREF(&BZ2File_Type);
- PyModule_AddObject(m, "BZ2File", (PyObject *)&BZ2File_Type);
+ Py_INCREF(&BZ2File_Type);
+ PyModule_AddObject(m, "BZ2File", (PyObject *)&BZ2File_Type);
- Py_INCREF(&BZ2Comp_Type);
- PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Comp_Type);
+ Py_INCREF(&BZ2Comp_Type);
+ PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Comp_Type);
- Py_INCREF(&BZ2Decomp_Type);
- PyModule_AddObject(m, "BZ2Decompressor", (PyObject *)&BZ2Decomp_Type);
- return m;
+ Py_INCREF(&BZ2Decomp_Type);
+ PyModule_AddObject(m, "BZ2Decompressor", (PyObject *)&BZ2Decomp_Type);
+ return m;
}
diff --git a/Modules/cjkcodecs/_codecs_cn.c b/Modules/cjkcodecs/_codecs_cn.c
index 4542ce6..ab4e659 100644
--- a/Modules/cjkcodecs/_codecs_cn.c
+++ b/Modules/cjkcodecs/_codecs_cn.c
@@ -17,24 +17,24 @@
/* GBK and GB2312 map differently in few codepoints that are listed below:
*
- * gb2312 gbk
- * A1A4 U+30FB KATAKANA MIDDLE DOT U+00B7 MIDDLE DOT
- * A1AA U+2015 HORIZONTAL BAR U+2014 EM DASH
- * A844 undefined U+2015 HORIZONTAL BAR
+ * gb2312 gbk
+ * A1A4 U+30FB KATAKANA MIDDLE DOT U+00B7 MIDDLE DOT
+ * A1AA U+2015 HORIZONTAL BAR U+2014 EM DASH
+ * A844 undefined U+2015 HORIZONTAL BAR
*/
#define GBK_DECODE(dc1, dc2, assi) \
- if ((dc1) == 0xa1 && (dc2) == 0xaa) (assi) = 0x2014; \
- else if ((dc1) == 0xa8 && (dc2) == 0x44) (assi) = 0x2015; \
- else if ((dc1) == 0xa1 && (dc2) == 0xa4) (assi) = 0x00b7; \
- else TRYMAP_DEC(gb2312, assi, dc1 ^ 0x80, dc2 ^ 0x80); \
- else TRYMAP_DEC(gbkext, assi, dc1, dc2);
+ if ((dc1) == 0xa1 && (dc2) == 0xaa) (assi) = 0x2014; \
+ else if ((dc1) == 0xa8 && (dc2) == 0x44) (assi) = 0x2015; \
+ else if ((dc1) == 0xa1 && (dc2) == 0xa4) (assi) = 0x00b7; \
+ else TRYMAP_DEC(gb2312, assi, dc1 ^ 0x80, dc2 ^ 0x80); \
+ else TRYMAP_DEC(gbkext, assi, dc1, dc2);
#define GBK_ENCODE(code, assi) \
- if ((code) == 0x2014) (assi) = 0xa1aa; \
- else if ((code) == 0x2015) (assi) = 0xa844; \
- else if ((code) == 0x00b7) (assi) = 0xa1a4; \
- else if ((code) != 0x30fb && TRYMAP_ENC_COND(gbcommon, assi, code));
+ if ((code) == 0x2014) (assi) = 0xa1aa; \
+ else if ((code) == 0x2015) (assi) = 0xa844; \
+ else if ((code) == 0x00b7) (assi) = 0xa1a4; \
+ else if ((code) != 0x30fb && TRYMAP_ENC_COND(gbcommon, assi, code));
/*
* GB2312 codec
@@ -42,53 +42,53 @@
ENCODER(gb2312)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
- TRYMAP_ENC(gbcommon, code, c);
- else return 1;
-
- if (code & 0x8000) /* MSB set: GBK */
- return 1;
-
- OUT1((code >> 8) | 0x80)
- OUT2((code & 0xFF) | 0x80)
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+ TRYMAP_ENC(gbcommon, code, c);
+ else return 1;
+
+ if (code & 0x8000) /* MSB set: GBK */
+ return 1;
+
+ OUT1((code >> 8) | 0x80)
+ OUT2((code & 0xFF) | 0x80)
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(gb2312)
{
- while (inleft > 0) {
- unsigned char c = **inbuf;
+ while (inleft > 0) {
+ unsigned char c = **inbuf;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_INBUF(2)
- TRYMAP_DEC(gb2312, **outbuf, c ^ 0x80, IN2 ^ 0x80) {
- NEXT(2, 1)
- }
- else return 2;
- }
+ REQUIRE_INBUF(2)
+ TRYMAP_DEC(gb2312, **outbuf, c ^ 0x80, IN2 ^ 0x80) {
+ NEXT(2, 1)
+ }
+ else return 2;
+ }
- return 0;
+ return 0;
}
@@ -98,55 +98,55 @@ DECODER(gb2312)
ENCODER(gbk)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
-
- GBK_ENCODE(c, code)
- else return 1;
-
- OUT1((code >> 8) | 0x80)
- if (code & 0x8000)
- OUT2((code & 0xFF)) /* MSB set: GBK */
- else
- OUT2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+
+ GBK_ENCODE(c, code)
+ else return 1;
+
+ OUT1((code >> 8) | 0x80)
+ if (code & 0x8000)
+ OUT2((code & 0xFF)) /* MSB set: GBK */
+ else
+ OUT2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(gbk)
{
- while (inleft > 0) {
- unsigned char c = IN1;
+ while (inleft > 0) {
+ unsigned char c = IN1;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_INBUF(2)
+ REQUIRE_INBUF(2)
- GBK_DECODE(c, IN2, **outbuf)
- else return 2;
+ GBK_DECODE(c, IN2, **outbuf)
+ else return 2;
- NEXT(2, 1)
- }
+ NEXT(2, 1)
+ }
- return 0;
+ return 0;
}
@@ -156,153 +156,153 @@ DECODER(gbk)
ENCODER(gb18030)
{
- while (inleft > 0) {
- ucs4_t c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1(c)
- NEXT(1, 1)
- continue;
- }
-
- DECODE_SURROGATE(c)
- if (c > 0x10FFFF)
+ while (inleft > 0) {
+ ucs4_t c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ DECODE_SURROGATE(c)
+ if (c > 0x10FFFF)
#if Py_UNICODE_SIZE == 2
- return 2; /* surrogates pair */
+ return 2; /* surrogates pair */
#else
- return 1;
+ return 1;
#endif
- else if (c >= 0x10000) {
- ucs4_t tc = c - 0x10000;
+ else if (c >= 0x10000) {
+ ucs4_t tc = c - 0x10000;
- REQUIRE_OUTBUF(4)
+ REQUIRE_OUTBUF(4)
- OUT4((unsigned char)(tc % 10) + 0x30)
- tc /= 10;
- OUT3((unsigned char)(tc % 126) + 0x81)
- tc /= 126;
- OUT2((unsigned char)(tc % 10) + 0x30)
- tc /= 10;
- OUT1((unsigned char)(tc + 0x90))
+ OUT4((unsigned char)(tc % 10) + 0x30)
+ tc /= 10;
+ OUT3((unsigned char)(tc % 126) + 0x81)
+ tc /= 126;
+ OUT2((unsigned char)(tc % 10) + 0x30)
+ tc /= 10;
+ OUT1((unsigned char)(tc + 0x90))
#if Py_UNICODE_SIZE == 2
- NEXT(2, 4) /* surrogates pair */
+ NEXT(2, 4) /* surrogates pair */
#else
- NEXT(1, 4)
+ NEXT(1, 4)
#endif
- continue;
- }
-
- REQUIRE_OUTBUF(2)
-
- GBK_ENCODE(c, code)
- else TRYMAP_ENC(gb18030ext, code, c);
- else {
- const struct _gb18030_to_unibmp_ranges *utrrange;
-
- REQUIRE_OUTBUF(4)
-
- for (utrrange = gb18030_to_unibmp_ranges;
- utrrange->first != 0;
- utrrange++)
- if (utrrange->first <= c &&
- c <= utrrange->last) {
- Py_UNICODE tc;
-
- tc = c - utrrange->first +
- utrrange->base;
-
- OUT4((unsigned char)(tc % 10) + 0x30)
- tc /= 10;
- OUT3((unsigned char)(tc % 126) + 0x81)
- tc /= 126;
- OUT2((unsigned char)(tc % 10) + 0x30)
- tc /= 10;
- OUT1((unsigned char)tc + 0x81)
-
- NEXT(1, 4)
- break;
- }
-
- if (utrrange->first == 0)
- return 1;
- continue;
- }
-
- OUT1((code >> 8) | 0x80)
- if (code & 0x8000)
- OUT2((code & 0xFF)) /* MSB set: GBK or GB18030ext */
- else
- OUT2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */
-
- NEXT(1, 2)
- }
-
- return 0;
+ continue;
+ }
+
+ REQUIRE_OUTBUF(2)
+
+ GBK_ENCODE(c, code)
+ else TRYMAP_ENC(gb18030ext, code, c);
+ else {
+ const struct _gb18030_to_unibmp_ranges *utrrange;
+
+ REQUIRE_OUTBUF(4)
+
+ for (utrrange = gb18030_to_unibmp_ranges;
+ utrrange->first != 0;
+ utrrange++)
+ if (utrrange->first <= c &&
+ c <= utrrange->last) {
+ Py_UNICODE tc;
+
+ tc = c - utrrange->first +
+ utrrange->base;
+
+ OUT4((unsigned char)(tc % 10) + 0x30)
+ tc /= 10;
+ OUT3((unsigned char)(tc % 126) + 0x81)
+ tc /= 126;
+ OUT2((unsigned char)(tc % 10) + 0x30)
+ tc /= 10;
+ OUT1((unsigned char)tc + 0x81)
+
+ NEXT(1, 4)
+ break;
+ }
+
+ if (utrrange->first == 0)
+ return 1;
+ continue;
+ }
+
+ OUT1((code >> 8) | 0x80)
+ if (code & 0x8000)
+ OUT2((code & 0xFF)) /* MSB set: GBK or GB18030ext */
+ else
+ OUT2((code & 0xFF) | 0x80) /* MSB unset: GB2312 */
+
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(gb18030)
{
- while (inleft > 0) {
- unsigned char c = IN1, c2;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_INBUF(2)
-
- c2 = IN2;
- if (c2 >= 0x30 && c2 <= 0x39) { /* 4 bytes seq */
- const struct _gb18030_to_unibmp_ranges *utr;
- unsigned char c3, c4;
- ucs4_t lseq;
-
- REQUIRE_INBUF(4)
- c3 = IN3;
- c4 = IN4;
- if (c < 0x81 || c3 < 0x81 || c4 < 0x30 || c4 > 0x39)
- return 4;
- c -= 0x81; c2 -= 0x30;
- c3 -= 0x81; c4 -= 0x30;
-
- if (c < 4) { /* U+0080 - U+FFFF */
- lseq = ((ucs4_t)c * 10 + c2) * 1260 +
- (ucs4_t)c3 * 10 + c4;
- if (lseq < 39420) {
- for (utr = gb18030_to_unibmp_ranges;
- lseq >= (utr + 1)->base;
- utr++) ;
- OUT1(utr->first - utr->base + lseq)
- NEXT(4, 1)
- continue;
- }
- }
- else if (c >= 15) { /* U+10000 - U+10FFFF */
- lseq = 0x10000 + (((ucs4_t)c-15) * 10 + c2)
- * 1260 + (ucs4_t)c3 * 10 + c4;
- if (lseq <= 0x10FFFF) {
- WRITEUCS4(lseq);
- NEXT_IN(4)
- continue;
- }
- }
- return 4;
- }
-
- GBK_DECODE(c, c2, **outbuf)
- else TRYMAP_DEC(gb18030ext, **outbuf, c, c2);
- else return 2;
-
- NEXT(2, 1)
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1, c2;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_INBUF(2)
+
+ c2 = IN2;
+ if (c2 >= 0x30 && c2 <= 0x39) { /* 4 bytes seq */
+ const struct _gb18030_to_unibmp_ranges *utr;
+ unsigned char c3, c4;
+ ucs4_t lseq;
+
+ REQUIRE_INBUF(4)
+ c3 = IN3;
+ c4 = IN4;
+ if (c < 0x81 || c3 < 0x81 || c4 < 0x30 || c4 > 0x39)
+ return 4;
+ c -= 0x81; c2 -= 0x30;
+ c3 -= 0x81; c4 -= 0x30;
+
+ if (c < 4) { /* U+0080 - U+FFFF */
+ lseq = ((ucs4_t)c * 10 + c2) * 1260 +
+ (ucs4_t)c3 * 10 + c4;
+ if (lseq < 39420) {
+ for (utr = gb18030_to_unibmp_ranges;
+ lseq >= (utr + 1)->base;
+ utr++) ;
+ OUT1(utr->first - utr->base + lseq)
+ NEXT(4, 1)
+ continue;
+ }
+ }
+ else if (c >= 15) { /* U+10000 - U+10FFFF */
+ lseq = 0x10000 + (((ucs4_t)c-15) * 10 + c2)
+ * 1260 + (ucs4_t)c3 * 10 + c4;
+ if (lseq <= 0x10FFFF) {
+ WRITEUCS4(lseq);
+ NEXT_IN(4)
+ continue;
+ }
+ }
+ return 4;
+ }
+
+ GBK_DECODE(c, c2, **outbuf)
+ else TRYMAP_DEC(gb18030ext, **outbuf, c, c2);
+ else return 2;
+
+ NEXT(2, 1)
+ }
+
+ return 0;
}
@@ -312,118 +312,118 @@ DECODER(gb18030)
ENCODER_INIT(hz)
{
- state->i = 0;
- return 0;
+ state->i = 0;
+ return 0;
}
ENCODER_RESET(hz)
{
- if (state->i != 0) {
- WRITE2('~', '}')
- state->i = 0;
- NEXT_OUT(2)
- }
- return 0;
+ if (state->i != 0) {
+ WRITE2('~', '}')
+ state->i = 0;
+ NEXT_OUT(2)
+ }
+ return 0;
}
ENCODER(hz)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- if (state->i == 0) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- }
- else {
- WRITE3('~', '}', (unsigned char)c)
- NEXT(1, 3)
- state->i = 0;
- }
- continue;
- }
-
- UCS4INVALID(c)
-
- TRYMAP_ENC(gbcommon, code, c);
- else return 1;
-
- if (code & 0x8000) /* MSB set: GBK */
- return 1;
-
- if (state->i == 0) {
- WRITE4('~', '{', code >> 8, code & 0xff)
- NEXT(1, 4)
- state->i = 1;
- }
- else {
- WRITE2(code >> 8, code & 0xff)
- NEXT(1, 2)
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ if (state->i == 0) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ }
+ else {
+ WRITE3('~', '}', (unsigned char)c)
+ NEXT(1, 3)
+ state->i = 0;
+ }
+ continue;
+ }
+
+ UCS4INVALID(c)
+
+ TRYMAP_ENC(gbcommon, code, c);
+ else return 1;
+
+ if (code & 0x8000) /* MSB set: GBK */
+ return 1;
+
+ if (state->i == 0) {
+ WRITE4('~', '{', code >> 8, code & 0xff)
+ NEXT(1, 4)
+ state->i = 1;
+ }
+ else {
+ WRITE2(code >> 8, code & 0xff)
+ NEXT(1, 2)
+ }
+ }
+
+ return 0;
}
DECODER_INIT(hz)
{
- state->i = 0;
- return 0;
+ state->i = 0;
+ return 0;
}
DECODER_RESET(hz)
{
- state->i = 0;
- return 0;
+ state->i = 0;
+ return 0;
}
DECODER(hz)
{
- while (inleft > 0) {
- unsigned char c = IN1;
-
- if (c == '~') {
- unsigned char c2 = IN2;
-
- REQUIRE_INBUF(2)
- if (c2 == '~') {
- WRITE1('~')
- NEXT(2, 1)
- continue;
- }
- else if (c2 == '{' && state->i == 0)
- state->i = 1; /* set GB */
- else if (c2 == '}' && state->i == 1)
- state->i = 0; /* set ASCII */
- else if (c2 == '\n')
- ; /* line-continuation */
- else
- return 2;
- NEXT(2, 0);
- continue;
- }
-
- if (c & 0x80)
- return 1;
-
- if (state->i == 0) { /* ASCII mode */
- WRITE1(c)
- NEXT(1, 1)
- }
- else { /* GB mode */
- REQUIRE_INBUF(2)
- REQUIRE_OUTBUF(1)
- TRYMAP_DEC(gb2312, **outbuf, c, IN2) {
- NEXT(2, 1)
- }
- else
- return 2;
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1;
+
+ if (c == '~') {
+ unsigned char c2 = IN2;
+
+ REQUIRE_INBUF(2)
+ if (c2 == '~') {
+ WRITE1('~')
+ NEXT(2, 1)
+ continue;
+ }
+ else if (c2 == '{' && state->i == 0)
+ state->i = 1; /* set GB */
+ else if (c2 == '}' && state->i == 1)
+ state->i = 0; /* set ASCII */
+ else if (c2 == '\n')
+ ; /* line-continuation */
+ else
+ return 2;
+ NEXT(2, 0);
+ continue;
+ }
+
+ if (c & 0x80)
+ return 1;
+
+ if (state->i == 0) { /* ASCII mode */
+ WRITE1(c)
+ NEXT(1, 1)
+ }
+ else { /* GB mode */
+ REQUIRE_INBUF(2)
+ REQUIRE_OUTBUF(1)
+ TRYMAP_DEC(gb2312, **outbuf, c, IN2) {
+ NEXT(2, 1)
+ }
+ else
+ return 2;
+ }
+ }
+
+ return 0;
}
diff --git a/Modules/cjkcodecs/_codecs_hk.c b/Modules/cjkcodecs/_codecs_hk.c
index 4bbd622..aaf103d 100644
--- a/Modules/cjkcodecs/_codecs_hk.c
+++ b/Modules/cjkcodecs/_codecs_hk.c
@@ -18,12 +18,12 @@ static const decode_map *big5_decmap = NULL;
CODEC_INIT(big5hkscs)
{
- static int initialized = 0;
+ static int initialized = 0;
- if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
- return -1;
- initialized = 1;
- return 0;
+ if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
+ return -1;
+ initialized = 1;
+ return 0;
}
/*
@@ -38,135 +38,135 @@ static const DBCHAR big5hkscs_pairenc_table[4] = {0x8862, 0x8864, 0x88a3, 0x88a5
ENCODER(big5hkscs)
{
- while (inleft > 0) {
- ucs4_t c = **inbuf;
- DBCHAR code;
- Py_ssize_t insize;
-
- if (c < 0x80) {
- REQUIRE_OUTBUF(1)
- **outbuf = (unsigned char)c;
- NEXT(1, 1)
- continue;
- }
-
- DECODE_SURROGATE(c)
- insize = GET_INSIZE(c);
-
- REQUIRE_OUTBUF(2)
-
- if (c < 0x10000) {
- TRYMAP_ENC(big5hkscs_bmp, code, c) {
- if (code == MULTIC) {
- if (inleft >= 2 &&
- ((c & 0xffdf) == 0x00ca) &&
- (((*inbuf)[1] & 0xfff7) == 0x0304)) {
- code = big5hkscs_pairenc_table[
- ((c >> 4) |
- ((*inbuf)[1] >> 3)) & 3];
- insize = 2;
- }
- else if (inleft < 2 &&
- !(flags & MBENC_FLUSH))
- return MBERR_TOOFEW;
- else {
- if (c == 0xca)
- code = 0x8866;
- else /* c == 0xea */
- code = 0x88a7;
- }
- }
- }
- else TRYMAP_ENC(big5, code, c);
- else return 1;
- }
- else if (c < 0x20000)
- return insize;
- else if (c < 0x30000) {
- TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff);
- else return insize;
- }
- else
- return insize;
-
- OUT1(code >> 8)
- OUT2(code & 0xFF)
- NEXT(insize, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ ucs4_t c = **inbuf;
+ DBCHAR code;
+ Py_ssize_t insize;
+
+ if (c < 0x80) {
+ REQUIRE_OUTBUF(1)
+ **outbuf = (unsigned char)c;
+ NEXT(1, 1)
+ continue;
+ }
+
+ DECODE_SURROGATE(c)
+ insize = GET_INSIZE(c);
+
+ REQUIRE_OUTBUF(2)
+
+ if (c < 0x10000) {
+ TRYMAP_ENC(big5hkscs_bmp, code, c) {
+ if (code == MULTIC) {
+ if (inleft >= 2 &&
+ ((c & 0xffdf) == 0x00ca) &&
+ (((*inbuf)[1] & 0xfff7) == 0x0304)) {
+ code = big5hkscs_pairenc_table[
+ ((c >> 4) |
+ ((*inbuf)[1] >> 3)) & 3];
+ insize = 2;
+ }
+ else if (inleft < 2 &&
+ !(flags & MBENC_FLUSH))
+ return MBERR_TOOFEW;
+ else {
+ if (c == 0xca)
+ code = 0x8866;
+ else /* c == 0xea */
+ code = 0x88a7;
+ }
+ }
+ }
+ else TRYMAP_ENC(big5, code, c);
+ else return 1;
+ }
+ else if (c < 0x20000)
+ return insize;
+ else if (c < 0x30000) {
+ TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff);
+ else return insize;
+ }
+ else
+ return insize;
+
+ OUT1(code >> 8)
+ OUT2(code & 0xFF)
+ NEXT(insize, 2)
+ }
+
+ return 0;
}
#define BH2S(c1, c2) (((c1) - 0x87) * (0xfe - 0x40 + 1) + ((c2) - 0x40))
DECODER(big5hkscs)
{
- while (inleft > 0) {
- unsigned char c = IN1;
- ucs4_t decoded;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_INBUF(2)
-
- if (0xc6 <= c && c <= 0xc8 && (c >= 0xc7 || IN2 >= 0xa1))
- goto hkscsdec;
-
- TRYMAP_DEC(big5, **outbuf, c, IN2) {
- NEXT(2, 1)
- }
- else
-hkscsdec: TRYMAP_DEC(big5hkscs, decoded, c, IN2) {
- int s = BH2S(c, IN2);
- const unsigned char *hintbase;
-
- assert(0x87 <= c && c <= 0xfe);
- assert(0x40 <= IN2 && IN2 <= 0xfe);
-
- if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
- hintbase = big5hkscs_phint_0;
- s -= BH2S(0x87, 0x40);
- }
- else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
- hintbase = big5hkscs_phint_12130;
- s -= BH2S(0xc6, 0xa1);
- }
- else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
- hintbase = big5hkscs_phint_21924;
- s -= BH2S(0xf9, 0xd6);
- }
- else
- return MBERR_INTERNAL;
-
- if (hintbase[s >> 3] & (1 << (s & 7))) {
- WRITEUCS4(decoded | 0x20000)
- NEXT_IN(2)
- }
- else {
- OUT1(decoded)
- NEXT(2, 1)
- }
- }
- else {
- switch ((c << 8) | IN2) {
- case 0x8862: WRITE2(0x00ca, 0x0304); break;
- case 0x8864: WRITE2(0x00ca, 0x030c); break;
- case 0x88a3: WRITE2(0x00ea, 0x0304); break;
- case 0x88a5: WRITE2(0x00ea, 0x030c); break;
- default: return 2;
- }
-
- NEXT(2, 2) /* all decoded codepoints are pairs, above. */
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1;
+ ucs4_t decoded;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_INBUF(2)
+
+ if (0xc6 <= c && c <= 0xc8 && (c >= 0xc7 || IN2 >= 0xa1))
+ goto hkscsdec;
+
+ TRYMAP_DEC(big5, **outbuf, c, IN2) {
+ NEXT(2, 1)
+ }
+ else
+hkscsdec: TRYMAP_DEC(big5hkscs, decoded, c, IN2) {
+ int s = BH2S(c, IN2);
+ const unsigned char *hintbase;
+
+ assert(0x87 <= c && c <= 0xfe);
+ assert(0x40 <= IN2 && IN2 <= 0xfe);
+
+ if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
+ hintbase = big5hkscs_phint_0;
+ s -= BH2S(0x87, 0x40);
+ }
+ else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
+ hintbase = big5hkscs_phint_12130;
+ s -= BH2S(0xc6, 0xa1);
+ }
+ else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
+ hintbase = big5hkscs_phint_21924;
+ s -= BH2S(0xf9, 0xd6);
+ }
+ else
+ return MBERR_INTERNAL;
+
+ if (hintbase[s >> 3] & (1 << (s & 7))) {
+ WRITEUCS4(decoded | 0x20000)
+ NEXT_IN(2)
+ }
+ else {
+ OUT1(decoded)
+ NEXT(2, 1)
+ }
+ }
+ else {
+ switch ((c << 8) | IN2) {
+ case 0x8862: WRITE2(0x00ca, 0x0304); break;
+ case 0x8864: WRITE2(0x00ca, 0x030c); break;
+ case 0x88a3: WRITE2(0x00ea, 0x0304); break;
+ case 0x88a5: WRITE2(0x00ea, 0x030c); break;
+ default: return 2;
+ }
+
+ NEXT(2, 2) /* all decoded codepoints are pairs, above. */
+ }
+ }
+
+ return 0;
}
diff --git a/Modules/cjkcodecs/_codecs_iso2022.c b/Modules/cjkcodecs/_codecs_iso2022.c
index 9ce7b75..25c1a36 100644
--- a/Modules/cjkcodecs/_codecs_iso2022.c
+++ b/Modules/cjkcodecs/_codecs_iso2022.c
@@ -19,85 +19,85 @@
state->c[0-3]
- 00000000
- ||^^^^^|
- |+-----+---- G0-3 Character Set
- +----------- Is G0-3 double byte?
+ 00000000
+ ||^^^^^|
+ |+-----+---- G0-3 Character Set
+ +----------- Is G0-3 double byte?
state->c[4]
- 00000000
- ||
- |+---- Locked-Shift?
- +----- ESC Throughout
+ 00000000
+ ||
+ |+---- Locked-Shift?
+ +----- ESC Throughout
*/
-#define ESC 0x1B
-#define SO 0x0E
-#define SI 0x0F
-#define LF 0x0A
-
-#define MAX_ESCSEQLEN 16
-
-#define CHARSET_ISO8859_1 'A'
-#define CHARSET_ASCII 'B'
-#define CHARSET_ISO8859_7 'F'
-#define CHARSET_JISX0201_K 'I'
-#define CHARSET_JISX0201_R 'J'
-
-#define CHARSET_GB2312 ('A'|CHARSET_DBCS)
-#define CHARSET_JISX0208 ('B'|CHARSET_DBCS)
-#define CHARSET_KSX1001 ('C'|CHARSET_DBCS)
-#define CHARSET_JISX0212 ('D'|CHARSET_DBCS)
-#define CHARSET_GB2312_8565 ('E'|CHARSET_DBCS)
-#define CHARSET_CNS11643_1 ('G'|CHARSET_DBCS)
-#define CHARSET_CNS11643_2 ('H'|CHARSET_DBCS)
-#define CHARSET_JISX0213_2000_1 ('O'|CHARSET_DBCS)
-#define CHARSET_JISX0213_2 ('P'|CHARSET_DBCS)
-#define CHARSET_JISX0213_2004_1 ('Q'|CHARSET_DBCS)
-#define CHARSET_JISX0208_O ('@'|CHARSET_DBCS)
-
-#define CHARSET_DBCS 0x80
-#define ESCMARK(mark) ((mark) & 0x7f)
-
-#define IS_ESCEND(c) (((c) >= 'A' && (c) <= 'Z') || (c) == '@')
+#define ESC 0x1B
+#define SO 0x0E
+#define SI 0x0F
+#define LF 0x0A
+
+#define MAX_ESCSEQLEN 16
+
+#define CHARSET_ISO8859_1 'A'
+#define CHARSET_ASCII 'B'
+#define CHARSET_ISO8859_7 'F'
+#define CHARSET_JISX0201_K 'I'
+#define CHARSET_JISX0201_R 'J'
+
+#define CHARSET_GB2312 ('A'|CHARSET_DBCS)
+#define CHARSET_JISX0208 ('B'|CHARSET_DBCS)
+#define CHARSET_KSX1001 ('C'|CHARSET_DBCS)
+#define CHARSET_JISX0212 ('D'|CHARSET_DBCS)
+#define CHARSET_GB2312_8565 ('E'|CHARSET_DBCS)
+#define CHARSET_CNS11643_1 ('G'|CHARSET_DBCS)
+#define CHARSET_CNS11643_2 ('H'|CHARSET_DBCS)
+#define CHARSET_JISX0213_2000_1 ('O'|CHARSET_DBCS)
+#define CHARSET_JISX0213_2 ('P'|CHARSET_DBCS)
+#define CHARSET_JISX0213_2004_1 ('Q'|CHARSET_DBCS)
+#define CHARSET_JISX0208_O ('@'|CHARSET_DBCS)
+
+#define CHARSET_DBCS 0x80
+#define ESCMARK(mark) ((mark) & 0x7f)
+
+#define IS_ESCEND(c) (((c) >= 'A' && (c) <= 'Z') || (c) == '@')
#define IS_ISO2022ESC(c2) \
- ((c2) == '(' || (c2) == ')' || (c2) == '$' || \
- (c2) == '.' || (c2) == '&')
- /* this is not a complete list of ISO-2022 escape sequence headers.
- * but, it's enough to implement CJK instances of iso-2022. */
-
-#define MAP_UNMAPPABLE 0xFFFF
-#define MAP_MULTIPLE_AVAIL 0xFFFE /* for JIS X 0213 */
-
-#define F_SHIFTED 0x01
-#define F_ESCTHROUGHOUT 0x02
-
-#define STATE_SETG(dn, v) ((state)->c[dn]) = (v);
-#define STATE_GETG(dn) ((state)->c[dn])
-
-#define STATE_G0 STATE_GETG(0)
-#define STATE_G1 STATE_GETG(1)
-#define STATE_G2 STATE_GETG(2)
-#define STATE_G3 STATE_GETG(3)
-#define STATE_SETG0(v) STATE_SETG(0, v)
-#define STATE_SETG1(v) STATE_SETG(1, v)
-#define STATE_SETG2(v) STATE_SETG(2, v)
-#define STATE_SETG3(v) STATE_SETG(3, v)
-
-#define STATE_SETFLAG(f) ((state)->c[4]) |= (f);
-#define STATE_GETFLAG(f) ((state)->c[4] & (f))
-#define STATE_CLEARFLAG(f) ((state)->c[4]) &= ~(f);
-#define STATE_CLEARFLAGS() ((state)->c[4]) = 0;
-
-#define ISO2022_CONFIG ((const struct iso2022_config *)config)
-#define CONFIG_ISSET(flag) (ISO2022_CONFIG->flags & (flag))
-#define CONFIG_DESIGNATIONS (ISO2022_CONFIG->designations)
+ ((c2) == '(' || (c2) == ')' || (c2) == '$' || \
+ (c2) == '.' || (c2) == '&')
+ /* this is not a complete list of ISO-2022 escape sequence headers.
+ * but, it's enough to implement CJK instances of iso-2022. */
+
+#define MAP_UNMAPPABLE 0xFFFF
+#define MAP_MULTIPLE_AVAIL 0xFFFE /* for JIS X 0213 */
+
+#define F_SHIFTED 0x01
+#define F_ESCTHROUGHOUT 0x02
+
+#define STATE_SETG(dn, v) ((state)->c[dn]) = (v);
+#define STATE_GETG(dn) ((state)->c[dn])
+
+#define STATE_G0 STATE_GETG(0)
+#define STATE_G1 STATE_GETG(1)
+#define STATE_G2 STATE_GETG(2)
+#define STATE_G3 STATE_GETG(3)
+#define STATE_SETG0(v) STATE_SETG(0, v)
+#define STATE_SETG1(v) STATE_SETG(1, v)
+#define STATE_SETG2(v) STATE_SETG(2, v)
+#define STATE_SETG3(v) STATE_SETG(3, v)
+
+#define STATE_SETFLAG(f) ((state)->c[4]) |= (f);
+#define STATE_GETFLAG(f) ((state)->c[4] & (f))
+#define STATE_CLEARFLAG(f) ((state)->c[4]) &= ~(f);
+#define STATE_CLEARFLAGS() ((state)->c[4]) = 0;
+
+#define ISO2022_CONFIG ((const struct iso2022_config *)config)
+#define CONFIG_ISSET(flag) (ISO2022_CONFIG->flags & (flag))
+#define CONFIG_DESIGNATIONS (ISO2022_CONFIG->designations)
/* iso2022_config.flags */
-#define NO_SHIFT 0x01
-#define USE_G2 0x02
-#define USE_JISX0208_EXT 0x04
+#define NO_SHIFT 0x01
+#define USE_G2 0x02
+#define USE_JISX0208_EXT 0x04
/*-*- internal data structures -*-*/
@@ -106,434 +106,434 @@ typedef ucs4_t (*iso2022_decode_func)(const unsigned char *data);
typedef DBCHAR (*iso2022_encode_func)(const ucs4_t *data, Py_ssize_t *length);
struct iso2022_designation {
- unsigned char mark;
- unsigned char plane;
- unsigned char width;
- iso2022_init_func initializer;
- iso2022_decode_func decoder;
- iso2022_encode_func encoder;
+ unsigned char mark;
+ unsigned char plane;
+ unsigned char width;
+ iso2022_init_func initializer;
+ iso2022_decode_func decoder;
+ iso2022_encode_func encoder;
};
struct iso2022_config {
- int flags;
- const struct iso2022_designation *designations; /* non-ascii desigs */
+ int flags;
+ const struct iso2022_designation *designations; /* non-ascii desigs */
};
/*-*- iso-2022 codec implementation -*-*/
CODEC_INIT(iso2022)
{
- const struct iso2022_designation *desig = CONFIG_DESIGNATIONS;
- for (desig = CONFIG_DESIGNATIONS; desig->mark; desig++)
- if (desig->initializer != NULL && desig->initializer() != 0)
- return -1;
- return 0;
+ const struct iso2022_designation *desig = CONFIG_DESIGNATIONS;
+ for (desig = CONFIG_DESIGNATIONS; desig->mark; desig++)
+ if (desig->initializer != NULL && desig->initializer() != 0)
+ return -1;
+ return 0;
}
ENCODER_INIT(iso2022)
{
- STATE_CLEARFLAGS()
- STATE_SETG0(CHARSET_ASCII)
- STATE_SETG1(CHARSET_ASCII)
- return 0;
+ STATE_CLEARFLAGS()
+ STATE_SETG0(CHARSET_ASCII)
+ STATE_SETG1(CHARSET_ASCII)
+ return 0;
}
ENCODER_RESET(iso2022)
{
- if (STATE_GETFLAG(F_SHIFTED)) {
- WRITE1(SI)
- NEXT_OUT(1)
- STATE_CLEARFLAG(F_SHIFTED)
- }
- if (STATE_G0 != CHARSET_ASCII) {
- WRITE3(ESC, '(', 'B')
- NEXT_OUT(3)
- STATE_SETG0(CHARSET_ASCII)
- }
- return 0;
+ if (STATE_GETFLAG(F_SHIFTED)) {
+ WRITE1(SI)
+ NEXT_OUT(1)
+ STATE_CLEARFLAG(F_SHIFTED)
+ }
+ if (STATE_G0 != CHARSET_ASCII) {
+ WRITE3(ESC, '(', 'B')
+ NEXT_OUT(3)
+ STATE_SETG0(CHARSET_ASCII)
+ }
+ return 0;
}
ENCODER(iso2022)
{
- while (inleft > 0) {
- const struct iso2022_designation *dsg;
- DBCHAR encoded;
- ucs4_t c = **inbuf;
- Py_ssize_t insize;
-
- if (c < 0x80) {
- if (STATE_G0 != CHARSET_ASCII) {
- WRITE3(ESC, '(', 'B')
- STATE_SETG0(CHARSET_ASCII)
- NEXT_OUT(3)
- }
- if (STATE_GETFLAG(F_SHIFTED)) {
- WRITE1(SI)
- STATE_CLEARFLAG(F_SHIFTED)
- NEXT_OUT(1)
- }
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
-
- DECODE_SURROGATE(c)
- insize = GET_INSIZE(c);
-
- encoded = MAP_UNMAPPABLE;
- for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++) {
- Py_ssize_t length = 1;
- encoded = dsg->encoder(&c, &length);
- if (encoded == MAP_MULTIPLE_AVAIL) {
- /* this implementation won't work for pair
- * of non-bmp characters. */
- if (inleft < 2) {
- if (!(flags & MBENC_FLUSH))
- return MBERR_TOOFEW;
- length = -1;
- }
- else
- length = 2;
+ while (inleft > 0) {
+ const struct iso2022_designation *dsg;
+ DBCHAR encoded;
+ ucs4_t c = **inbuf;
+ Py_ssize_t insize;
+
+ if (c < 0x80) {
+ if (STATE_G0 != CHARSET_ASCII) {
+ WRITE3(ESC, '(', 'B')
+ STATE_SETG0(CHARSET_ASCII)
+ NEXT_OUT(3)
+ }
+ if (STATE_GETFLAG(F_SHIFTED)) {
+ WRITE1(SI)
+ STATE_CLEARFLAG(F_SHIFTED)
+ NEXT_OUT(1)
+ }
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ DECODE_SURROGATE(c)
+ insize = GET_INSIZE(c);
+
+ encoded = MAP_UNMAPPABLE;
+ for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++) {
+ Py_ssize_t length = 1;
+ encoded = dsg->encoder(&c, &length);
+ if (encoded == MAP_MULTIPLE_AVAIL) {
+ /* this implementation won't work for pair
+ * of non-bmp characters. */
+ if (inleft < 2) {
+ if (!(flags & MBENC_FLUSH))
+ return MBERR_TOOFEW;
+ length = -1;
+ }
+ else
+ length = 2;
#if Py_UNICODE_SIZE == 2
- if (length == 2) {
- ucs4_t u4in[2];
- u4in[0] = (ucs4_t)IN1;
- u4in[1] = (ucs4_t)IN2;
- encoded = dsg->encoder(u4in, &length);
- } else
- encoded = dsg->encoder(&c, &length);
+ if (length == 2) {
+ ucs4_t u4in[2];
+ u4in[0] = (ucs4_t)IN1;
+ u4in[1] = (ucs4_t)IN2;
+ encoded = dsg->encoder(u4in, &length);
+ } else
+ encoded = dsg->encoder(&c, &length);
#else
- encoded = dsg->encoder(&c, &length);
+ encoded = dsg->encoder(&c, &length);
#endif
- if (encoded != MAP_UNMAPPABLE) {
- insize = length;
- break;
- }
- }
- else if (encoded != MAP_UNMAPPABLE)
- break;
- }
-
- if (!dsg->mark)
- return 1;
- assert(dsg->width == 1 || dsg->width == 2);
-
- switch (dsg->plane) {
- case 0: /* G0 */
- if (STATE_GETFLAG(F_SHIFTED)) {
- WRITE1(SI)
- STATE_CLEARFLAG(F_SHIFTED)
- NEXT_OUT(1)
- }
- if (STATE_G0 != dsg->mark) {
- if (dsg->width == 1) {
- WRITE3(ESC, '(', ESCMARK(dsg->mark))
- STATE_SETG0(dsg->mark)
- NEXT_OUT(3)
- }
- else if (dsg->mark == CHARSET_JISX0208) {
- WRITE3(ESC, '$', ESCMARK(dsg->mark))
- STATE_SETG0(dsg->mark)
- NEXT_OUT(3)
- }
- else {
- WRITE4(ESC, '$', '(',
- ESCMARK(dsg->mark))
- STATE_SETG0(dsg->mark)
- NEXT_OUT(4)
- }
- }
- break;
- case 1: /* G1 */
- if (STATE_G1 != dsg->mark) {
- if (dsg->width == 1) {
- WRITE3(ESC, ')', ESCMARK(dsg->mark))
- STATE_SETG1(dsg->mark)
- NEXT_OUT(3)
- }
- else {
- WRITE4(ESC, '$', ')',
- ESCMARK(dsg->mark))
- STATE_SETG1(dsg->mark)
- NEXT_OUT(4)
- }
- }
- if (!STATE_GETFLAG(F_SHIFTED)) {
- WRITE1(SO)
- STATE_SETFLAG(F_SHIFTED)
- NEXT_OUT(1)
- }
- break;
- default: /* G2 and G3 is not supported: no encoding in
- * CJKCodecs are using them yet */
- return MBERR_INTERNAL;
- }
-
- if (dsg->width == 1) {
- WRITE1((unsigned char)encoded)
- NEXT_OUT(1)
- }
- else {
- WRITE2(encoded >> 8, encoded & 0xff)
- NEXT_OUT(2)
- }
- NEXT_IN(insize)
- }
-
- return 0;
+ if (encoded != MAP_UNMAPPABLE) {
+ insize = length;
+ break;
+ }
+ }
+ else if (encoded != MAP_UNMAPPABLE)
+ break;
+ }
+
+ if (!dsg->mark)
+ return 1;
+ assert(dsg->width == 1 || dsg->width == 2);
+
+ switch (dsg->plane) {
+ case 0: /* G0 */
+ if (STATE_GETFLAG(F_SHIFTED)) {
+ WRITE1(SI)
+ STATE_CLEARFLAG(F_SHIFTED)
+ NEXT_OUT(1)
+ }
+ if (STATE_G0 != dsg->mark) {
+ if (dsg->width == 1) {
+ WRITE3(ESC, '(', ESCMARK(dsg->mark))
+ STATE_SETG0(dsg->mark)
+ NEXT_OUT(3)
+ }
+ else if (dsg->mark == CHARSET_JISX0208) {
+ WRITE3(ESC, '$', ESCMARK(dsg->mark))
+ STATE_SETG0(dsg->mark)
+ NEXT_OUT(3)
+ }
+ else {
+ WRITE4(ESC, '$', '(',
+ ESCMARK(dsg->mark))
+ STATE_SETG0(dsg->mark)
+ NEXT_OUT(4)
+ }
+ }
+ break;
+ case 1: /* G1 */
+ if (STATE_G1 != dsg->mark) {
+ if (dsg->width == 1) {
+ WRITE3(ESC, ')', ESCMARK(dsg->mark))
+ STATE_SETG1(dsg->mark)
+ NEXT_OUT(3)
+ }
+ else {
+ WRITE4(ESC, '$', ')',
+ ESCMARK(dsg->mark))
+ STATE_SETG1(dsg->mark)
+ NEXT_OUT(4)
+ }
+ }
+ if (!STATE_GETFLAG(F_SHIFTED)) {
+ WRITE1(SO)
+ STATE_SETFLAG(F_SHIFTED)
+ NEXT_OUT(1)
+ }
+ break;
+ default: /* G2 and G3 is not supported: no encoding in
+ * CJKCodecs are using them yet */
+ return MBERR_INTERNAL;
+ }
+
+ if (dsg->width == 1) {
+ WRITE1((unsigned char)encoded)
+ NEXT_OUT(1)
+ }
+ else {
+ WRITE2(encoded >> 8, encoded & 0xff)
+ NEXT_OUT(2)
+ }
+ NEXT_IN(insize)
+ }
+
+ return 0;
}
DECODER_INIT(iso2022)
{
- STATE_CLEARFLAGS()
- STATE_SETG0(CHARSET_ASCII)
- STATE_SETG1(CHARSET_ASCII)
- STATE_SETG2(CHARSET_ASCII)
- return 0;
+ STATE_CLEARFLAGS()
+ STATE_SETG0(CHARSET_ASCII)
+ STATE_SETG1(CHARSET_ASCII)
+ STATE_SETG2(CHARSET_ASCII)
+ return 0;
}
DECODER_RESET(iso2022)
{
- STATE_SETG0(CHARSET_ASCII)
- STATE_CLEARFLAG(F_SHIFTED)
- return 0;
+ STATE_SETG0(CHARSET_ASCII)
+ STATE_CLEARFLAG(F_SHIFTED)
+ return 0;
}
static Py_ssize_t
iso2022processesc(const void *config, MultibyteCodec_State *state,
- const unsigned char **inbuf, Py_ssize_t *inleft)
+ const unsigned char **inbuf, Py_ssize_t *inleft)
{
- unsigned char charset, designation;
- Py_ssize_t i, esclen;
-
- for (i = 1;i < MAX_ESCSEQLEN;i++) {
- if (i >= *inleft)
- return MBERR_TOOFEW;
- if (IS_ESCEND((*inbuf)[i])) {
- esclen = i + 1;
- break;
- }
- else if (CONFIG_ISSET(USE_JISX0208_EXT) && i+1 < *inleft &&
- (*inbuf)[i] == '&' && (*inbuf)[i+1] == '@')
- i += 2;
- }
-
- if (i >= MAX_ESCSEQLEN)
- return 1; /* unterminated escape sequence */
-
- switch (esclen) {
- case 3:
- if (IN2 == '$') {
- charset = IN3 | CHARSET_DBCS;
- designation = 0;
- }
- else {
- charset = IN3;
- if (IN2 == '(') designation = 0;
- else if (IN2 == ')') designation = 1;
- else if (CONFIG_ISSET(USE_G2) && IN2 == '.')
- designation = 2;
- else return 3;
- }
- break;
- case 4:
- if (IN2 != '$')
- return 4;
-
- charset = IN4 | CHARSET_DBCS;
- if (IN3 == '(') designation = 0;
- else if (IN3 == ')') designation = 1;
- else return 4;
- break;
- case 6: /* designation with prefix */
- if (CONFIG_ISSET(USE_JISX0208_EXT) &&
- (*inbuf)[3] == ESC && (*inbuf)[4] == '$' &&
- (*inbuf)[5] == 'B') {
- charset = 'B' | CHARSET_DBCS;
- designation = 0;
- }
- else
- return 6;
- break;
- default:
- return esclen;
- }
-
- /* raise error when the charset is not designated for this encoding */
- if (charset != CHARSET_ASCII) {
- const struct iso2022_designation *dsg;
-
- for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++)
- if (dsg->mark == charset)
- break;
- if (!dsg->mark)
- return esclen;
- }
-
- STATE_SETG(designation, charset)
- *inleft -= esclen;
- (*inbuf) += esclen;
- return 0;
+ unsigned char charset, designation;
+ Py_ssize_t i, esclen;
+
+ for (i = 1;i < MAX_ESCSEQLEN;i++) {
+ if (i >= *inleft)
+ return MBERR_TOOFEW;
+ if (IS_ESCEND((*inbuf)[i])) {
+ esclen = i + 1;
+ break;
+ }
+ else if (CONFIG_ISSET(USE_JISX0208_EXT) && i+1 < *inleft &&
+ (*inbuf)[i] == '&' && (*inbuf)[i+1] == '@')
+ i += 2;
+ }
+
+ if (i >= MAX_ESCSEQLEN)
+ return 1; /* unterminated escape sequence */
+
+ switch (esclen) {
+ case 3:
+ if (IN2 == '$') {
+ charset = IN3 | CHARSET_DBCS;
+ designation = 0;
+ }
+ else {
+ charset = IN3;
+ if (IN2 == '(') designation = 0;
+ else if (IN2 == ')') designation = 1;
+ else if (CONFIG_ISSET(USE_G2) && IN2 == '.')
+ designation = 2;
+ else return 3;
+ }
+ break;
+ case 4:
+ if (IN2 != '$')
+ return 4;
+
+ charset = IN4 | CHARSET_DBCS;
+ if (IN3 == '(') designation = 0;
+ else if (IN3 == ')') designation = 1;
+ else return 4;
+ break;
+ case 6: /* designation with prefix */
+ if (CONFIG_ISSET(USE_JISX0208_EXT) &&
+ (*inbuf)[3] == ESC && (*inbuf)[4] == '$' &&
+ (*inbuf)[5] == 'B') {
+ charset = 'B' | CHARSET_DBCS;
+ designation = 0;
+ }
+ else
+ return 6;
+ break;
+ default:
+ return esclen;
+ }
+
+ /* raise error when the charset is not designated for this encoding */
+ if (charset != CHARSET_ASCII) {
+ const struct iso2022_designation *dsg;
+
+ for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++)
+ if (dsg->mark == charset)
+ break;
+ if (!dsg->mark)
+ return esclen;
+ }
+
+ STATE_SETG(designation, charset)
+ *inleft -= esclen;
+ (*inbuf) += esclen;
+ return 0;
}
-#define ISO8859_7_DECODE(c, assi) \
- if ((c) < 0xa0) (assi) = (c); \
- else if ((c) < 0xc0 && (0x288f3bc9L & (1L << ((c)-0xa0)))) \
- (assi) = (c); \
- else if ((c) >= 0xb4 && (c) <= 0xfe && ((c) >= 0xd4 || \
- (0xbffffd77L & (1L << ((c)-0xb4))))) \
- (assi) = 0x02d0 + (c); \
- else if ((c) == 0xa1) (assi) = 0x2018; \
- else if ((c) == 0xa2) (assi) = 0x2019; \
- else if ((c) == 0xaf) (assi) = 0x2015;
+#define ISO8859_7_DECODE(c, assi) \
+ if ((c) < 0xa0) (assi) = (c); \
+ else if ((c) < 0xc0 && (0x288f3bc9L & (1L << ((c)-0xa0)))) \
+ (assi) = (c); \
+ else if ((c) >= 0xb4 && (c) <= 0xfe && ((c) >= 0xd4 || \
+ (0xbffffd77L & (1L << ((c)-0xb4))))) \
+ (assi) = 0x02d0 + (c); \
+ else if ((c) == 0xa1) (assi) = 0x2018; \
+ else if ((c) == 0xa2) (assi) = 0x2019; \
+ else if ((c) == 0xaf) (assi) = 0x2015;
static Py_ssize_t
iso2022processg2(const void *config, MultibyteCodec_State *state,
- const unsigned char **inbuf, Py_ssize_t *inleft,
- Py_UNICODE **outbuf, Py_ssize_t *outleft)
+ const unsigned char **inbuf, Py_ssize_t *inleft,
+ Py_UNICODE **outbuf, Py_ssize_t *outleft)
{
- /* not written to use encoder, decoder functions because only few
- * encodings use G2 designations in CJKCodecs */
- if (STATE_G2 == CHARSET_ISO8859_1) {
- if (IN3 < 0x80)
- OUT1(IN3 + 0x80)
- else
- return 3;
- }
- else if (STATE_G2 == CHARSET_ISO8859_7) {
- ISO8859_7_DECODE(IN3 ^ 0x80, **outbuf)
- else return 3;
- }
- else if (STATE_G2 == CHARSET_ASCII) {
- if (IN3 & 0x80) return 3;
- else **outbuf = IN3;
- }
- else
- return MBERR_INTERNAL;
-
- (*inbuf) += 3;
- *inleft -= 3;
- (*outbuf) += 1;
- *outleft -= 1;
- return 0;
+ /* not written to use encoder, decoder functions because only few
+ * encodings use G2 designations in CJKCodecs */
+ if (STATE_G2 == CHARSET_ISO8859_1) {
+ if (IN3 < 0x80)
+ OUT1(IN3 + 0x80)
+ else
+ return 3;
+ }
+ else if (STATE_G2 == CHARSET_ISO8859_7) {
+ ISO8859_7_DECODE(IN3 ^ 0x80, **outbuf)
+ else return 3;
+ }
+ else if (STATE_G2 == CHARSET_ASCII) {
+ if (IN3 & 0x80) return 3;
+ else **outbuf = IN3;
+ }
+ else
+ return MBERR_INTERNAL;
+
+ (*inbuf) += 3;
+ *inleft -= 3;
+ (*outbuf) += 1;
+ *outleft -= 1;
+ return 0;
}
DECODER(iso2022)
{
- const struct iso2022_designation *dsgcache = NULL;
-
- while (inleft > 0) {
- unsigned char c = IN1;
- Py_ssize_t err;
-
- if (STATE_GETFLAG(F_ESCTHROUGHOUT)) {
- /* ESC throughout mode:
- * for non-iso2022 escape sequences */
- WRITE1(c) /* assume as ISO-8859-1 */
- NEXT(1, 1)
- if (IS_ESCEND(c)) {
- STATE_CLEARFLAG(F_ESCTHROUGHOUT)
- }
- continue;
- }
-
- switch (c) {
- case ESC:
- REQUIRE_INBUF(2)
- if (IS_ISO2022ESC(IN2)) {
- err = iso2022processesc(config, state,
- inbuf, &inleft);
- if (err != 0)
- return err;
- }
- else if (CONFIG_ISSET(USE_G2) && IN2 == 'N') {/* SS2 */
- REQUIRE_INBUF(3)
- err = iso2022processg2(config, state,
- inbuf, &inleft, outbuf, &outleft);
- if (err != 0)
- return err;
- }
- else {
- WRITE1(ESC)
- STATE_SETFLAG(F_ESCTHROUGHOUT)
- NEXT(1, 1)
- }
- break;
- case SI:
- if (CONFIG_ISSET(NO_SHIFT))
- goto bypass;
- STATE_CLEARFLAG(F_SHIFTED)
- NEXT_IN(1)
- break;
- case SO:
- if (CONFIG_ISSET(NO_SHIFT))
- goto bypass;
- STATE_SETFLAG(F_SHIFTED)
- NEXT_IN(1)
- break;
- case LF:
- STATE_CLEARFLAG(F_SHIFTED)
- WRITE1(LF)
- NEXT(1, 1)
- break;
- default:
- if (c < 0x20) /* C0 */
- goto bypass;
- else if (c >= 0x80)
- return 1;
- else {
- const struct iso2022_designation *dsg;
- unsigned char charset;
- ucs4_t decoded;
-
- if (STATE_GETFLAG(F_SHIFTED))
- charset = STATE_G1;
- else
- charset = STATE_G0;
-
- if (charset == CHARSET_ASCII) {
-bypass: WRITE1(c)
- NEXT(1, 1)
- break;
- }
-
- if (dsgcache != NULL &&
- dsgcache->mark == charset)
- dsg = dsgcache;
- else {
- for (dsg = CONFIG_DESIGNATIONS;
- dsg->mark != charset
+ const struct iso2022_designation *dsgcache = NULL;
+
+ while (inleft > 0) {
+ unsigned char c = IN1;
+ Py_ssize_t err;
+
+ if (STATE_GETFLAG(F_ESCTHROUGHOUT)) {
+ /* ESC throughout mode:
+ * for non-iso2022 escape sequences */
+ WRITE1(c) /* assume as ISO-8859-1 */
+ NEXT(1, 1)
+ if (IS_ESCEND(c)) {
+ STATE_CLEARFLAG(F_ESCTHROUGHOUT)
+ }
+ continue;
+ }
+
+ switch (c) {
+ case ESC:
+ REQUIRE_INBUF(2)
+ if (IS_ISO2022ESC(IN2)) {
+ err = iso2022processesc(config, state,
+ inbuf, &inleft);
+ if (err != 0)
+ return err;
+ }
+ else if (CONFIG_ISSET(USE_G2) && IN2 == 'N') {/* SS2 */
+ REQUIRE_INBUF(3)
+ err = iso2022processg2(config, state,
+ inbuf, &inleft, outbuf, &outleft);
+ if (err != 0)
+ return err;
+ }
+ else {
+ WRITE1(ESC)
+ STATE_SETFLAG(F_ESCTHROUGHOUT)
+ NEXT(1, 1)
+ }
+ break;
+ case SI:
+ if (CONFIG_ISSET(NO_SHIFT))
+ goto bypass;
+ STATE_CLEARFLAG(F_SHIFTED)
+ NEXT_IN(1)
+ break;
+ case SO:
+ if (CONFIG_ISSET(NO_SHIFT))
+ goto bypass;
+ STATE_SETFLAG(F_SHIFTED)
+ NEXT_IN(1)
+ break;
+ case LF:
+ STATE_CLEARFLAG(F_SHIFTED)
+ WRITE1(LF)
+ NEXT(1, 1)
+ break;
+ default:
+ if (c < 0x20) /* C0 */
+ goto bypass;
+ else if (c >= 0x80)
+ return 1;
+ else {
+ const struct iso2022_designation *dsg;
+ unsigned char charset;
+ ucs4_t decoded;
+
+ if (STATE_GETFLAG(F_SHIFTED))
+ charset = STATE_G1;
+ else
+ charset = STATE_G0;
+
+ if (charset == CHARSET_ASCII) {
+bypass: WRITE1(c)
+ NEXT(1, 1)
+ break;
+ }
+
+ if (dsgcache != NULL &&
+ dsgcache->mark == charset)
+ dsg = dsgcache;
+ else {
+ for (dsg = CONFIG_DESIGNATIONS;
+ dsg->mark != charset
#ifdef Py_DEBUG
- && dsg->mark != '\0'
+ && dsg->mark != '\0'
#endif
- ;dsg++)
- /* noop */;
- assert(dsg->mark != '\0');
- dsgcache = dsg;
- }
-
- REQUIRE_INBUF(dsg->width)
- decoded = dsg->decoder(*inbuf);
- if (decoded == MAP_UNMAPPABLE)
- return dsg->width;
-
- if (decoded < 0x10000) {
- WRITE1(decoded)
- NEXT_OUT(1)
- }
- else if (decoded < 0x30000) {
- WRITEUCS4(decoded)
- }
- else { /* JIS X 0213 pairs */
- WRITE2(decoded >> 16, decoded & 0xffff)
- NEXT_OUT(2)
- }
- NEXT_IN(dsg->width)
- }
- break;
- }
- }
- return 0;
+ ;dsg++)
+ /* noop */;
+ assert(dsg->mark != '\0');
+ dsgcache = dsg;
+ }
+
+ REQUIRE_INBUF(dsg->width)
+ decoded = dsg->decoder(*inbuf);
+ if (decoded == MAP_UNMAPPABLE)
+ return dsg->width;
+
+ if (decoded < 0x10000) {
+ WRITE1(decoded)
+ NEXT_OUT(1)
+ }
+ else if (decoded < 0x30000) {
+ WRITEUCS4(decoded)
+ }
+ else { /* JIS X 0213 pairs */
+ WRITE2(decoded >> 16, decoded & 0xffff)
+ NEXT_OUT(2)
+ }
+ NEXT_IN(dsg->width)
+ }
+ break;
+ }
+ }
+ return 0;
}
/*-*- mapping table holders -*-*/
@@ -567,542 +567,542 @@ DECMAP(gb2312)
static int
ksx1001_init(void)
{
- static int initialized = 0;
-
- if (!initialized && (
- IMPORT_MAP(kr, cp949, &cp949_encmap, NULL) ||
- IMPORT_MAP(kr, ksx1001, NULL, &ksx1001_decmap)))
- return -1;
- initialized = 1;
- return 0;
+ static int initialized = 0;
+
+ if (!initialized && (
+ IMPORT_MAP(kr, cp949, &cp949_encmap, NULL) ||
+ IMPORT_MAP(kr, ksx1001, NULL, &ksx1001_decmap)))
+ return -1;
+ initialized = 1;
+ return 0;
}
static ucs4_t
ksx1001_decoder(const unsigned char *data)
{
- ucs4_t u;
- TRYMAP_DEC(ksx1001, u, data[0], data[1])
- return u;
- else
- return MAP_UNMAPPABLE;
+ ucs4_t u;
+ TRYMAP_DEC(ksx1001, u, data[0], data[1])
+ return u;
+ else
+ return MAP_UNMAPPABLE;
}
static DBCHAR
ksx1001_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- assert(*length == 1);
- if (*data < 0x10000) {
- TRYMAP_ENC(cp949, coded, *data)
- if (!(coded & 0x8000))
- return coded;
- }
- return MAP_UNMAPPABLE;
+ DBCHAR coded;
+ assert(*length == 1);
+ if (*data < 0x10000) {
+ TRYMAP_ENC(cp949, coded, *data)
+ if (!(coded & 0x8000))
+ return coded;
+ }
+ return MAP_UNMAPPABLE;
}
static int
jisx0208_init(void)
{
- static int initialized = 0;
-
- if (!initialized && (
- IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
- IMPORT_MAP(jp, jisx0208, NULL, &jisx0208_decmap)))
- return -1;
- initialized = 1;
- return 0;
+ static int initialized = 0;
+
+ if (!initialized && (
+ IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
+ IMPORT_MAP(jp, jisx0208, NULL, &jisx0208_decmap)))
+ return -1;
+ initialized = 1;
+ return 0;
}
static ucs4_t
jisx0208_decoder(const unsigned char *data)
{
- ucs4_t u;
- if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
- return 0xff3c;
- else TRYMAP_DEC(jisx0208, u, data[0], data[1])
- return u;
- else
- return MAP_UNMAPPABLE;
+ ucs4_t u;
+ if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
+ return 0xff3c;
+ else TRYMAP_DEC(jisx0208, u, data[0], data[1])
+ return u;
+ else
+ return MAP_UNMAPPABLE;
}
static DBCHAR
jisx0208_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- assert(*length == 1);
- if (*data < 0x10000) {
- if (*data == 0xff3c) /* F/W REVERSE SOLIDUS */
- return 0x2140;
- else TRYMAP_ENC(jisxcommon, coded, *data) {
- if (!(coded & 0x8000))
- return coded;
- }
- }
- return MAP_UNMAPPABLE;
+ DBCHAR coded;
+ assert(*length == 1);
+ if (*data < 0x10000) {
+ if (*data == 0xff3c) /* F/W REVERSE SOLIDUS */
+ return 0x2140;
+ else TRYMAP_ENC(jisxcommon, coded, *data) {
+ if (!(coded & 0x8000))
+ return coded;
+ }
+ }
+ return MAP_UNMAPPABLE;
}
static int
jisx0212_init(void)
{
- static int initialized = 0;
-
- if (!initialized && (
- IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
- IMPORT_MAP(jp, jisx0212, NULL, &jisx0212_decmap)))
- return -1;
- initialized = 1;
- return 0;
+ static int initialized = 0;
+
+ if (!initialized && (
+ IMPORT_MAP(jp, jisxcommon, &jisxcommon_encmap, NULL) ||
+ IMPORT_MAP(jp, jisx0212, NULL, &jisx0212_decmap)))
+ return -1;
+ initialized = 1;
+ return 0;
}
static ucs4_t
jisx0212_decoder(const unsigned char *data)
{
- ucs4_t u;
- TRYMAP_DEC(jisx0212, u, data[0], data[1])
- return u;
- else
- return MAP_UNMAPPABLE;
+ ucs4_t u;
+ TRYMAP_DEC(jisx0212, u, data[0], data[1])
+ return u;
+ else
+ return MAP_UNMAPPABLE;
}
static DBCHAR
jisx0212_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- assert(*length == 1);
- if (*data < 0x10000) {
- TRYMAP_ENC(jisxcommon, coded, *data) {
- if (coded & 0x8000)
- return coded & 0x7fff;
- }
- }
- return MAP_UNMAPPABLE;
+ DBCHAR coded;
+ assert(*length == 1);
+ if (*data < 0x10000) {
+ TRYMAP_ENC(jisxcommon, coded, *data) {
+ if (coded & 0x8000)
+ return coded & 0x7fff;
+ }
+ }
+ return MAP_UNMAPPABLE;
}
static int
jisx0213_init(void)
{
- static int initialized = 0;
-
- if (!initialized && (
- jisx0208_init() ||
- IMPORT_MAP(jp, jisx0213_bmp,
- &jisx0213_bmp_encmap, NULL) ||
- IMPORT_MAP(jp, jisx0213_1_bmp,
- NULL, &jisx0213_1_bmp_decmap) ||
- IMPORT_MAP(jp, jisx0213_2_bmp,
- NULL, &jisx0213_2_bmp_decmap) ||
- IMPORT_MAP(jp, jisx0213_emp,
- &jisx0213_emp_encmap, NULL) ||
- IMPORT_MAP(jp, jisx0213_1_emp,
- NULL, &jisx0213_1_emp_decmap) ||
- IMPORT_MAP(jp, jisx0213_2_emp,
- NULL, &jisx0213_2_emp_decmap) ||
- IMPORT_MAP(jp, jisx0213_pair, &jisx0213_pair_encmap,
- &jisx0213_pair_decmap)))
- return -1;
- initialized = 1;
- return 0;
+ static int initialized = 0;
+
+ if (!initialized && (
+ jisx0208_init() ||
+ IMPORT_MAP(jp, jisx0213_bmp,
+ &jisx0213_bmp_encmap, NULL) ||
+ IMPORT_MAP(jp, jisx0213_1_bmp,
+ NULL, &jisx0213_1_bmp_decmap) ||
+ IMPORT_MAP(jp, jisx0213_2_bmp,
+ NULL, &jisx0213_2_bmp_decmap) ||
+ IMPORT_MAP(jp, jisx0213_emp,
+ &jisx0213_emp_encmap, NULL) ||
+ IMPORT_MAP(jp, jisx0213_1_emp,
+ NULL, &jisx0213_1_emp_decmap) ||
+ IMPORT_MAP(jp, jisx0213_2_emp,
+ NULL, &jisx0213_2_emp_decmap) ||
+ IMPORT_MAP(jp, jisx0213_pair, &jisx0213_pair_encmap,
+ &jisx0213_pair_decmap)))
+ return -1;
+ initialized = 1;
+ return 0;
}
#define config ((void *)2000)
static ucs4_t
jisx0213_2000_1_decoder(const unsigned char *data)
{
- ucs4_t u;
- EMULATE_JISX0213_2000_DECODE_PLANE1(u, data[0], data[1])
- else if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
- return 0xff3c;
- else TRYMAP_DEC(jisx0208, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_1_bmp, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_1_emp, u, data[0], data[1])
- u |= 0x20000;
- else TRYMAP_DEC(jisx0213_pair, u, data[0], data[1]);
- else
- return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ EMULATE_JISX0213_2000_DECODE_PLANE1(u, data[0], data[1])
+ else if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
+ return 0xff3c;
+ else TRYMAP_DEC(jisx0208, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_1_bmp, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_1_emp, u, data[0], data[1])
+ u |= 0x20000;
+ else TRYMAP_DEC(jisx0213_pair, u, data[0], data[1]);
+ else
+ return MAP_UNMAPPABLE;
+ return u;
}
static ucs4_t
jisx0213_2000_2_decoder(const unsigned char *data)
{
- ucs4_t u;
- EMULATE_JISX0213_2000_DECODE_PLANE2(u, data[0], data[1])
- TRYMAP_DEC(jisx0213_2_bmp, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_2_emp, u, data[0], data[1])
- u |= 0x20000;
- else
- return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ EMULATE_JISX0213_2000_DECODE_PLANE2(u, data[0], data[1])
+ TRYMAP_DEC(jisx0213_2_bmp, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_2_emp, u, data[0], data[1])
+ u |= 0x20000;
+ else
+ return MAP_UNMAPPABLE;
+ return u;
}
#undef config
static ucs4_t
jisx0213_2004_1_decoder(const unsigned char *data)
{
- ucs4_t u;
- if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
- return 0xff3c;
- else TRYMAP_DEC(jisx0208, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_1_bmp, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_1_emp, u, data[0], data[1])
- u |= 0x20000;
- else TRYMAP_DEC(jisx0213_pair, u, data[0], data[1]);
- else
- return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
+ return 0xff3c;
+ else TRYMAP_DEC(jisx0208, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_1_bmp, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_1_emp, u, data[0], data[1])
+ u |= 0x20000;
+ else TRYMAP_DEC(jisx0213_pair, u, data[0], data[1]);
+ else
+ return MAP_UNMAPPABLE;
+ return u;
}
static ucs4_t
jisx0213_2004_2_decoder(const unsigned char *data)
{
- ucs4_t u;
- TRYMAP_DEC(jisx0213_2_bmp, u, data[0], data[1]);
- else TRYMAP_DEC(jisx0213_2_emp, u, data[0], data[1])
- u |= 0x20000;
- else
- return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ TRYMAP_DEC(jisx0213_2_bmp, u, data[0], data[1]);
+ else TRYMAP_DEC(jisx0213_2_emp, u, data[0], data[1])
+ u |= 0x20000;
+ else
+ return MAP_UNMAPPABLE;
+ return u;
}
static DBCHAR
jisx0213_encoder(const ucs4_t *data, Py_ssize_t *length, void *config)
{
- DBCHAR coded;
-
- switch (*length) {
- case 1: /* first character */
- if (*data >= 0x10000) {
- if ((*data) >> 16 == 0x20000 >> 16) {
- EMULATE_JISX0213_2000_ENCODE_EMP(coded, *data)
- else TRYMAP_ENC(jisx0213_emp, coded,
- (*data) & 0xffff)
- return coded;
- }
- return MAP_UNMAPPABLE;
- }
-
- EMULATE_JISX0213_2000_ENCODE_BMP(coded, *data)
- else TRYMAP_ENC(jisx0213_bmp, coded, *data) {
- if (coded == MULTIC)
- return MAP_MULTIPLE_AVAIL;
- }
- else TRYMAP_ENC(jisxcommon, coded, *data) {
- if (coded & 0x8000)
- return MAP_UNMAPPABLE;
- }
- else
- return MAP_UNMAPPABLE;
- return coded;
- case 2: /* second character of unicode pair */
- coded = find_pairencmap((ucs2_t)data[0], (ucs2_t)data[1],
- jisx0213_pair_encmap, JISX0213_ENCPAIRS);
- if (coded == DBCINV) {
- *length = 1;
- coded = find_pairencmap((ucs2_t)data[0], 0,
- jisx0213_pair_encmap, JISX0213_ENCPAIRS);
- if (coded == DBCINV)
- return MAP_UNMAPPABLE;
- }
- else
- return coded;
- case -1: /* flush unterminated */
- *length = 1;
- coded = find_pairencmap((ucs2_t)data[0], 0,
- jisx0213_pair_encmap, JISX0213_ENCPAIRS);
- if (coded == DBCINV)
- return MAP_UNMAPPABLE;
- else
- return coded;
- default:
- return MAP_UNMAPPABLE;
- }
+ DBCHAR coded;
+
+ switch (*length) {
+ case 1: /* first character */
+ if (*data >= 0x10000) {
+ if ((*data) >> 16 == 0x20000 >> 16) {
+ EMULATE_JISX0213_2000_ENCODE_EMP(coded, *data)
+ else TRYMAP_ENC(jisx0213_emp, coded,
+ (*data) & 0xffff)
+ return coded;
+ }
+ return MAP_UNMAPPABLE;
+ }
+
+ EMULATE_JISX0213_2000_ENCODE_BMP(coded, *data)
+ else TRYMAP_ENC(jisx0213_bmp, coded, *data) {
+ if (coded == MULTIC)
+ return MAP_MULTIPLE_AVAIL;
+ }
+ else TRYMAP_ENC(jisxcommon, coded, *data) {
+ if (coded & 0x8000)
+ return MAP_UNMAPPABLE;
+ }
+ else
+ return MAP_UNMAPPABLE;
+ return coded;
+ case 2: /* second character of unicode pair */
+ coded = find_pairencmap((ucs2_t)data[0], (ucs2_t)data[1],
+ jisx0213_pair_encmap, JISX0213_ENCPAIRS);
+ if (coded == DBCINV) {
+ *length = 1;
+ coded = find_pairencmap((ucs2_t)data[0], 0,
+ jisx0213_pair_encmap, JISX0213_ENCPAIRS);
+ if (coded == DBCINV)
+ return MAP_UNMAPPABLE;
+ }
+ else
+ return coded;
+ case -1: /* flush unterminated */
+ *length = 1;
+ coded = find_pairencmap((ucs2_t)data[0], 0,
+ jisx0213_pair_encmap, JISX0213_ENCPAIRS);
+ if (coded == DBCINV)
+ return MAP_UNMAPPABLE;
+ else
+ return coded;
+ default:
+ return MAP_UNMAPPABLE;
+ }
}
static DBCHAR
jisx0213_2000_1_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded = jisx0213_encoder(data, length, (void *)2000);
- if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
- return coded;
- else if (coded & 0x8000)
- return MAP_UNMAPPABLE;
- else
- return coded;
+ DBCHAR coded = jisx0213_encoder(data, length, (void *)2000);
+ if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
+ return coded;
+ else if (coded & 0x8000)
+ return MAP_UNMAPPABLE;
+ else
+ return coded;
}
static DBCHAR
jisx0213_2000_1_encoder_paironly(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- Py_ssize_t ilength = *length;
-
- coded = jisx0213_encoder(data, length, (void *)2000);
- switch (ilength) {
- case 1:
- if (coded == MAP_MULTIPLE_AVAIL)
- return MAP_MULTIPLE_AVAIL;
- else
- return MAP_UNMAPPABLE;
- case 2:
- if (*length != 2)
- return MAP_UNMAPPABLE;
- else
- return coded;
- default:
- return MAP_UNMAPPABLE;
- }
+ DBCHAR coded;
+ Py_ssize_t ilength = *length;
+
+ coded = jisx0213_encoder(data, length, (void *)2000);
+ switch (ilength) {
+ case 1:
+ if (coded == MAP_MULTIPLE_AVAIL)
+ return MAP_MULTIPLE_AVAIL;
+ else
+ return MAP_UNMAPPABLE;
+ case 2:
+ if (*length != 2)
+ return MAP_UNMAPPABLE;
+ else
+ return coded;
+ default:
+ return MAP_UNMAPPABLE;
+ }
}
static DBCHAR
jisx0213_2000_2_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded = jisx0213_encoder(data, length, (void *)2000);
- if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
- return coded;
- else if (coded & 0x8000)
- return coded & 0x7fff;
- else
- return MAP_UNMAPPABLE;
+ DBCHAR coded = jisx0213_encoder(data, length, (void *)2000);
+ if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
+ return coded;
+ else if (coded & 0x8000)
+ return coded & 0x7fff;
+ else
+ return MAP_UNMAPPABLE;
}
static DBCHAR
jisx0213_2004_1_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded = jisx0213_encoder(data, length, NULL);
- if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
- return coded;
- else if (coded & 0x8000)
- return MAP_UNMAPPABLE;
- else
- return coded;
+ DBCHAR coded = jisx0213_encoder(data, length, NULL);
+ if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
+ return coded;
+ else if (coded & 0x8000)
+ return MAP_UNMAPPABLE;
+ else
+ return coded;
}
static DBCHAR
jisx0213_2004_1_encoder_paironly(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- Py_ssize_t ilength = *length;
-
- coded = jisx0213_encoder(data, length, NULL);
- switch (ilength) {
- case 1:
- if (coded == MAP_MULTIPLE_AVAIL)
- return MAP_MULTIPLE_AVAIL;
- else
- return MAP_UNMAPPABLE;
- case 2:
- if (*length != 2)
- return MAP_UNMAPPABLE;
- else
- return coded;
- default:
- return MAP_UNMAPPABLE;
- }
+ DBCHAR coded;
+ Py_ssize_t ilength = *length;
+
+ coded = jisx0213_encoder(data, length, NULL);
+ switch (ilength) {
+ case 1:
+ if (coded == MAP_MULTIPLE_AVAIL)
+ return MAP_MULTIPLE_AVAIL;
+ else
+ return MAP_UNMAPPABLE;
+ case 2:
+ if (*length != 2)
+ return MAP_UNMAPPABLE;
+ else
+ return coded;
+ default:
+ return MAP_UNMAPPABLE;
+ }
}
static DBCHAR
jisx0213_2004_2_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded = jisx0213_encoder(data, length, NULL);
- if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
- return coded;
- else if (coded & 0x8000)
- return coded & 0x7fff;
- else
- return MAP_UNMAPPABLE;
+ DBCHAR coded = jisx0213_encoder(data, length, NULL);
+ if (coded == MAP_UNMAPPABLE || coded == MAP_MULTIPLE_AVAIL)
+ return coded;
+ else if (coded & 0x8000)
+ return coded & 0x7fff;
+ else
+ return MAP_UNMAPPABLE;
}
static ucs4_t
jisx0201_r_decoder(const unsigned char *data)
{
- ucs4_t u;
- JISX0201_R_DECODE(*data, u)
- else return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ JISX0201_R_DECODE(*data, u)
+ else return MAP_UNMAPPABLE;
+ return u;
}
static DBCHAR
jisx0201_r_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- JISX0201_R_ENCODE(*data, coded)
- else return MAP_UNMAPPABLE;
- return coded;
+ DBCHAR coded;
+ JISX0201_R_ENCODE(*data, coded)
+ else return MAP_UNMAPPABLE;
+ return coded;
}
static ucs4_t
jisx0201_k_decoder(const unsigned char *data)
{
- ucs4_t u;
- JISX0201_K_DECODE(*data ^ 0x80, u)
- else return MAP_UNMAPPABLE;
- return u;
+ ucs4_t u;
+ JISX0201_K_DECODE(*data ^ 0x80, u)
+ else return MAP_UNMAPPABLE;
+ return u;
}
static DBCHAR
jisx0201_k_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- JISX0201_K_ENCODE(*data, coded)
- else return MAP_UNMAPPABLE;
- return coded - 0x80;
+ DBCHAR coded;
+ JISX0201_K_ENCODE(*data, coded)
+ else return MAP_UNMAPPABLE;
+ return coded - 0x80;
}
static int
gb2312_init(void)
{
- static int initialized = 0;
-
- if (!initialized && (
- IMPORT_MAP(cn, gbcommon, &gbcommon_encmap, NULL) ||
- IMPORT_MAP(cn, gb2312, NULL, &gb2312_decmap)))
- return -1;
- initialized = 1;
- return 0;
+ static int initialized = 0;
+
+ if (!initialized && (
+ IMPORT_MAP(cn, gbcommon, &gbcommon_encmap, NULL) ||
+ IMPORT_MAP(cn, gb2312, NULL, &gb2312_decmap)))
+ return -1;
+ initialized = 1;
+ return 0;
}
static ucs4_t
gb2312_decoder(const unsigned char *data)
{
- ucs4_t u;
- TRYMAP_DEC(gb2312, u, data[0], data[1])
- return u;
- else
- return MAP_UNMAPPABLE;
+ ucs4_t u;
+ TRYMAP_DEC(gb2312, u, data[0], data[1])
+ return u;
+ else
+ return MAP_UNMAPPABLE;
}
static DBCHAR
gb2312_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- DBCHAR coded;
- assert(*length == 1);
- if (*data < 0x10000) {
- TRYMAP_ENC(gbcommon, coded, *data) {
- if (!(coded & 0x8000))
- return coded;
- }
- }
- return MAP_UNMAPPABLE;
+ DBCHAR coded;
+ assert(*length == 1);
+ if (*data < 0x10000) {
+ TRYMAP_ENC(gbcommon, coded, *data) {
+ if (!(coded & 0x8000))
+ return coded;
+ }
+ }
+ return MAP_UNMAPPABLE;
}
static ucs4_t
dummy_decoder(const unsigned char *data)
{
- return MAP_UNMAPPABLE;
+ return MAP_UNMAPPABLE;
}
static DBCHAR
dummy_encoder(const ucs4_t *data, Py_ssize_t *length)
{
- return MAP_UNMAPPABLE;
+ return MAP_UNMAPPABLE;
}
/*-*- registry tables -*-*/
-#define REGISTRY_KSX1001_G0 { CHARSET_KSX1001, 0, 2, \
- ksx1001_init, \
- ksx1001_decoder, ksx1001_encoder }
-#define REGISTRY_KSX1001_G1 { CHARSET_KSX1001, 1, 2, \
- ksx1001_init, \
- ksx1001_decoder, ksx1001_encoder }
-#define REGISTRY_JISX0201_R { CHARSET_JISX0201_R, 0, 1, \
- NULL, \
- jisx0201_r_decoder, jisx0201_r_encoder }
-#define REGISTRY_JISX0201_K { CHARSET_JISX0201_K, 0, 1, \
- NULL, \
- jisx0201_k_decoder, jisx0201_k_encoder }
-#define REGISTRY_JISX0208 { CHARSET_JISX0208, 0, 2, \
- jisx0208_init, \
- jisx0208_decoder, jisx0208_encoder }
-#define REGISTRY_JISX0208_O { CHARSET_JISX0208_O, 0, 2, \
- jisx0208_init, \
- jisx0208_decoder, jisx0208_encoder }
-#define REGISTRY_JISX0212 { CHARSET_JISX0212, 0, 2, \
- jisx0212_init, \
- jisx0212_decoder, jisx0212_encoder }
-#define REGISTRY_JISX0213_2000_1 { CHARSET_JISX0213_2000_1, 0, 2, \
- jisx0213_init, \
- jisx0213_2000_1_decoder, \
- jisx0213_2000_1_encoder }
+#define REGISTRY_KSX1001_G0 { CHARSET_KSX1001, 0, 2, \
+ ksx1001_init, \
+ ksx1001_decoder, ksx1001_encoder }
+#define REGISTRY_KSX1001_G1 { CHARSET_KSX1001, 1, 2, \
+ ksx1001_init, \
+ ksx1001_decoder, ksx1001_encoder }
+#define REGISTRY_JISX0201_R { CHARSET_JISX0201_R, 0, 1, \
+ NULL, \
+ jisx0201_r_decoder, jisx0201_r_encoder }
+#define REGISTRY_JISX0201_K { CHARSET_JISX0201_K, 0, 1, \
+ NULL, \
+ jisx0201_k_decoder, jisx0201_k_encoder }
+#define REGISTRY_JISX0208 { CHARSET_JISX0208, 0, 2, \
+ jisx0208_init, \
+ jisx0208_decoder, jisx0208_encoder }
+#define REGISTRY_JISX0208_O { CHARSET_JISX0208_O, 0, 2, \
+ jisx0208_init, \
+ jisx0208_decoder, jisx0208_encoder }
+#define REGISTRY_JISX0212 { CHARSET_JISX0212, 0, 2, \
+ jisx0212_init, \
+ jisx0212_decoder, jisx0212_encoder }
+#define REGISTRY_JISX0213_2000_1 { CHARSET_JISX0213_2000_1, 0, 2, \
+ jisx0213_init, \
+ jisx0213_2000_1_decoder, \
+ jisx0213_2000_1_encoder }
#define REGISTRY_JISX0213_2000_1_PAIRONLY { CHARSET_JISX0213_2000_1, 0, 2, \
- jisx0213_init, \
- jisx0213_2000_1_decoder, \
- jisx0213_2000_1_encoder_paironly }
-#define REGISTRY_JISX0213_2000_2 { CHARSET_JISX0213_2, 0, 2, \
- jisx0213_init, \
- jisx0213_2000_2_decoder, \
- jisx0213_2000_2_encoder }
-#define REGISTRY_JISX0213_2004_1 { CHARSET_JISX0213_2004_1, 0, 2, \
- jisx0213_init, \
- jisx0213_2004_1_decoder, \
- jisx0213_2004_1_encoder }
+ jisx0213_init, \
+ jisx0213_2000_1_decoder, \
+ jisx0213_2000_1_encoder_paironly }
+#define REGISTRY_JISX0213_2000_2 { CHARSET_JISX0213_2, 0, 2, \
+ jisx0213_init, \
+ jisx0213_2000_2_decoder, \
+ jisx0213_2000_2_encoder }
+#define REGISTRY_JISX0213_2004_1 { CHARSET_JISX0213_2004_1, 0, 2, \
+ jisx0213_init, \
+ jisx0213_2004_1_decoder, \
+ jisx0213_2004_1_encoder }
#define REGISTRY_JISX0213_2004_1_PAIRONLY { CHARSET_JISX0213_2004_1, 0, 2, \
- jisx0213_init, \
- jisx0213_2004_1_decoder, \
- jisx0213_2004_1_encoder_paironly }
-#define REGISTRY_JISX0213_2004_2 { CHARSET_JISX0213_2, 0, 2, \
- jisx0213_init, \
- jisx0213_2004_2_decoder, \
- jisx0213_2004_2_encoder }
-#define REGISTRY_GB2312 { CHARSET_GB2312, 0, 2, \
- gb2312_init, \
- gb2312_decoder, gb2312_encoder }
-#define REGISTRY_CNS11643_1 { CHARSET_CNS11643_1, 1, 2, \
- cns11643_init, \
- cns11643_1_decoder, cns11643_1_encoder }
-#define REGISTRY_CNS11643_2 { CHARSET_CNS11643_2, 2, 2, \
- cns11643_init, \
- cns11643_2_decoder, cns11643_2_encoder }
-#define REGISTRY_ISO8859_1 { CHARSET_ISO8859_1, 2, 1, \
- NULL, dummy_decoder, dummy_encoder }
-#define REGISTRY_ISO8859_7 { CHARSET_ISO8859_7, 2, 1, \
- NULL, dummy_decoder, dummy_encoder }
-#define REGISTRY_SENTINEL { 0, }
-#define CONFIGDEF(var, attrs) \
- static const struct iso2022_config iso2022_##var##_config = { \
- attrs, iso2022_##var##_designations \
- };
+ jisx0213_init, \
+ jisx0213_2004_1_decoder, \
+ jisx0213_2004_1_encoder_paironly }
+#define REGISTRY_JISX0213_2004_2 { CHARSET_JISX0213_2, 0, 2, \
+ jisx0213_init, \
+ jisx0213_2004_2_decoder, \
+ jisx0213_2004_2_encoder }
+#define REGISTRY_GB2312 { CHARSET_GB2312, 0, 2, \
+ gb2312_init, \
+ gb2312_decoder, gb2312_encoder }
+#define REGISTRY_CNS11643_1 { CHARSET_CNS11643_1, 1, 2, \
+ cns11643_init, \
+ cns11643_1_decoder, cns11643_1_encoder }
+#define REGISTRY_CNS11643_2 { CHARSET_CNS11643_2, 2, 2, \
+ cns11643_init, \
+ cns11643_2_decoder, cns11643_2_encoder }
+#define REGISTRY_ISO8859_1 { CHARSET_ISO8859_1, 2, 1, \
+ NULL, dummy_decoder, dummy_encoder }
+#define REGISTRY_ISO8859_7 { CHARSET_ISO8859_7, 2, 1, \
+ NULL, dummy_decoder, dummy_encoder }
+#define REGISTRY_SENTINEL { 0, }
+#define CONFIGDEF(var, attrs) \
+ static const struct iso2022_config iso2022_##var##_config = { \
+ attrs, iso2022_##var##_designations \
+ };
static const struct iso2022_designation iso2022_kr_designations[] = {
- REGISTRY_KSX1001_G1, REGISTRY_SENTINEL
+ REGISTRY_KSX1001_G1, REGISTRY_SENTINEL
};
CONFIGDEF(kr, 0)
static const struct iso2022_designation iso2022_jp_designations[] = {
- REGISTRY_JISX0208, REGISTRY_JISX0201_R, REGISTRY_JISX0208_O,
- REGISTRY_SENTINEL
+ REGISTRY_JISX0208, REGISTRY_JISX0201_R, REGISTRY_JISX0208_O,
+ REGISTRY_SENTINEL
};
CONFIGDEF(jp, NO_SHIFT | USE_JISX0208_EXT)
static const struct iso2022_designation iso2022_jp_1_designations[] = {
- REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_JISX0201_R,
- REGISTRY_JISX0208_O, REGISTRY_SENTINEL
+ REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_JISX0201_R,
+ REGISTRY_JISX0208_O, REGISTRY_SENTINEL
};
CONFIGDEF(jp_1, NO_SHIFT | USE_JISX0208_EXT)
static const struct iso2022_designation iso2022_jp_2_designations[] = {
- REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_KSX1001_G0,
- REGISTRY_GB2312, REGISTRY_JISX0201_R, REGISTRY_JISX0208_O,
- REGISTRY_ISO8859_1, REGISTRY_ISO8859_7, REGISTRY_SENTINEL
+ REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_KSX1001_G0,
+ REGISTRY_GB2312, REGISTRY_JISX0201_R, REGISTRY_JISX0208_O,
+ REGISTRY_ISO8859_1, REGISTRY_ISO8859_7, REGISTRY_SENTINEL
};
CONFIGDEF(jp_2, NO_SHIFT | USE_G2 | USE_JISX0208_EXT)
static const struct iso2022_designation iso2022_jp_2004_designations[] = {
- REGISTRY_JISX0213_2004_1_PAIRONLY, REGISTRY_JISX0208,
- REGISTRY_JISX0213_2004_1, REGISTRY_JISX0213_2004_2, REGISTRY_SENTINEL
+ REGISTRY_JISX0213_2004_1_PAIRONLY, REGISTRY_JISX0208,
+ REGISTRY_JISX0213_2004_1, REGISTRY_JISX0213_2004_2, REGISTRY_SENTINEL
};
CONFIGDEF(jp_2004, NO_SHIFT | USE_JISX0208_EXT)
static const struct iso2022_designation iso2022_jp_3_designations[] = {
- REGISTRY_JISX0213_2000_1_PAIRONLY, REGISTRY_JISX0208,
- REGISTRY_JISX0213_2000_1, REGISTRY_JISX0213_2000_2, REGISTRY_SENTINEL
+ REGISTRY_JISX0213_2000_1_PAIRONLY, REGISTRY_JISX0208,
+ REGISTRY_JISX0213_2000_1, REGISTRY_JISX0213_2000_2, REGISTRY_SENTINEL
};
CONFIGDEF(jp_3, NO_SHIFT | USE_JISX0208_EXT)
static const struct iso2022_designation iso2022_jp_ext_designations[] = {
- REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_JISX0201_R,
- REGISTRY_JISX0201_K, REGISTRY_JISX0208_O, REGISTRY_SENTINEL
+ REGISTRY_JISX0208, REGISTRY_JISX0212, REGISTRY_JISX0201_R,
+ REGISTRY_JISX0201_K, REGISTRY_JISX0208_O, REGISTRY_SENTINEL
};
CONFIGDEF(jp_ext, NO_SHIFT | USE_JISX0208_EXT)
@@ -1111,11 +1111,11 @@ BEGIN_MAPPINGS_LIST
/* no mapping table here */
END_MAPPINGS_LIST
-#define ISO2022_CODEC(variation) { \
- "iso2022_" #variation, \
- &iso2022_##variation##_config, \
- iso2022_codec_init, \
- _STATEFUL_METHODS(iso2022) \
+#define ISO2022_CODEC(variation) { \
+ "iso2022_" #variation, \
+ &iso2022_##variation##_config, \
+ iso2022_codec_init, \
+ _STATEFUL_METHODS(iso2022) \
},
BEGIN_CODECS_LIST
diff --git a/Modules/cjkcodecs/_codecs_jp.c b/Modules/cjkcodecs/_codecs_jp.c
index f49a10b..901d3be 100644
--- a/Modules/cjkcodecs/_codecs_jp.c
+++ b/Modules/cjkcodecs/_codecs_jp.c
@@ -19,124 +19,124 @@
ENCODER(cp932)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
- unsigned char c1, c2;
-
- if (c <= 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- else if (c >= 0xff61 && c <= 0xff9f) {
- WRITE1(c - 0xfec0)
- NEXT(1, 1)
- continue;
- }
- else if (c >= 0xf8f0 && c <= 0xf8f3) {
- /* Windows compatibility */
- REQUIRE_OUTBUF(1)
- if (c == 0xf8f0)
- OUT1(0xa0)
- else
- OUT1(c - 0xfef1 + 0xfd)
- NEXT(1, 1)
- continue;
- }
-
- UCS4INVALID(c)
- REQUIRE_OUTBUF(2)
-
- TRYMAP_ENC(cp932ext, code, c) {
- OUT1(code >> 8)
- OUT2(code & 0xff)
- }
- else TRYMAP_ENC(jisxcommon, code, c) {
- if (code & 0x8000) /* MSB set: JIS X 0212 */
- return 1;
-
- /* JIS X 0208 */
- c1 = code >> 8;
- c2 = code & 0xff;
- c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
- c1 = (c1 - 0x21) >> 1;
- OUT1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1)
- OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
- }
- else if (c >= 0xe000 && c < 0xe758) {
- /* User-defined area */
- c1 = (Py_UNICODE)(c - 0xe000) / 188;
- c2 = (Py_UNICODE)(c - 0xe000) % 188;
- OUT1(c1 + 0xf0)
- OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
- }
- else
- return 1;
-
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+ unsigned char c1, c2;
+
+ if (c <= 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ else if (c >= 0xff61 && c <= 0xff9f) {
+ WRITE1(c - 0xfec0)
+ NEXT(1, 1)
+ continue;
+ }
+ else if (c >= 0xf8f0 && c <= 0xf8f3) {
+ /* Windows compatibility */
+ REQUIRE_OUTBUF(1)
+ if (c == 0xf8f0)
+ OUT1(0xa0)
+ else
+ OUT1(c - 0xfef1 + 0xfd)
+ NEXT(1, 1)
+ continue;
+ }
+
+ UCS4INVALID(c)
+ REQUIRE_OUTBUF(2)
+
+ TRYMAP_ENC(cp932ext, code, c) {
+ OUT1(code >> 8)
+ OUT2(code & 0xff)
+ }
+ else TRYMAP_ENC(jisxcommon, code, c) {
+ if (code & 0x8000) /* MSB set: JIS X 0212 */
+ return 1;
+
+ /* JIS X 0208 */
+ c1 = code >> 8;
+ c2 = code & 0xff;
+ c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
+ c1 = (c1 - 0x21) >> 1;
+ OUT1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1)
+ OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
+ }
+ else if (c >= 0xe000 && c < 0xe758) {
+ /* User-defined area */
+ c1 = (Py_UNICODE)(c - 0xe000) / 188;
+ c2 = (Py_UNICODE)(c - 0xe000) % 188;
+ OUT1(c1 + 0xf0)
+ OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
+ }
+ else
+ return 1;
+
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(cp932)
{
- while (inleft > 0) {
- unsigned char c = IN1, c2;
-
- REQUIRE_OUTBUF(1)
- if (c <= 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
- else if (c >= 0xa0 && c <= 0xdf) {
- if (c == 0xa0)
- OUT1(0xf8f0) /* half-width katakana */
- else
- OUT1(0xfec0 + c)
- NEXT(1, 1)
- continue;
- }
- else if (c >= 0xfd/* && c <= 0xff*/) {
- /* Windows compatibility */
- OUT1(0xf8f1 - 0xfd + c)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_INBUF(2)
- c2 = IN2;
-
- TRYMAP_DEC(cp932ext, **outbuf, c, c2);
- else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){
- if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
- return 2;
-
- c = (c < 0xe0 ? c - 0x81 : c - 0xc1);
- c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
- c = (2 * c + (c2 < 0x5e ? 0 : 1) + 0x21);
- c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
-
- TRYMAP_DEC(jisx0208, **outbuf, c, c2);
- else return 2;
- }
- else if (c >= 0xf0 && c <= 0xf9) {
- if ((c2 >= 0x40 && c2 <= 0x7e) ||
- (c2 >= 0x80 && c2 <= 0xfc))
- OUT1(0xe000 + 188 * (c - 0xf0) +
- (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41))
- else
- return 2;
- }
- else
- return 2;
-
- NEXT(2, 1)
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1, c2;
+
+ REQUIRE_OUTBUF(1)
+ if (c <= 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+ else if (c >= 0xa0 && c <= 0xdf) {
+ if (c == 0xa0)
+ OUT1(0xf8f0) /* half-width katakana */
+ else
+ OUT1(0xfec0 + c)
+ NEXT(1, 1)
+ continue;
+ }
+ else if (c >= 0xfd/* && c <= 0xff*/) {
+ /* Windows compatibility */
+ OUT1(0xf8f1 - 0xfd + c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+
+ TRYMAP_DEC(cp932ext, **outbuf, c, c2);
+ else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){
+ if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
+ return 2;
+
+ c = (c < 0xe0 ? c - 0x81 : c - 0xc1);
+ c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
+ c = (2 * c + (c2 < 0x5e ? 0 : 1) + 0x21);
+ c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
+
+ TRYMAP_DEC(jisx0208, **outbuf, c, c2);
+ else return 2;
+ }
+ else if (c >= 0xf0 && c <= 0xf9) {
+ if ((c2 >= 0x40 && c2 <= 0x7e) ||
+ (c2 >= 0x80 && c2 <= 0xfc))
+ OUT1(0xe000 + 188 * (c - 0xf0) +
+ (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41))
+ else
+ return 2;
+ }
+ else
+ return 2;
+
+ NEXT(2, 1)
+ }
+
+ return 0;
}
@@ -146,166 +146,166 @@ DECODER(cp932)
ENCODER(euc_jis_2004)
{
- while (inleft > 0) {
- ucs4_t c = IN1;
- DBCHAR code;
- Py_ssize_t insize;
-
- if (c < 0x80) {
- WRITE1(c)
- NEXT(1, 1)
- continue;
- }
-
- DECODE_SURROGATE(c)
- insize = GET_INSIZE(c);
-
- if (c <= 0xFFFF) {
- EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
- else TRYMAP_ENC(jisx0213_bmp, code, c) {
- if (code == MULTIC) {
- if (inleft < 2) {
- if (flags & MBENC_FLUSH) {
- code = find_pairencmap(
- (ucs2_t)c, 0,
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV)
- return 1;
- }
- else
- return MBERR_TOOFEW;
- }
- else {
- code = find_pairencmap(
- (ucs2_t)c, (*inbuf)[1],
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV) {
- code = find_pairencmap(
- (ucs2_t)c, 0,
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV)
- return 1;
- } else
- insize = 2;
- }
- }
- }
- else TRYMAP_ENC(jisxcommon, code, c);
- else if (c >= 0xff61 && c <= 0xff9f) {
- /* JIS X 0201 half-width katakana */
- WRITE2(0x8e, c - 0xfec0)
- NEXT(1, 2)
- continue;
- }
- else if (c == 0xff3c)
- /* F/W REVERSE SOLIDUS (see NOTES) */
- code = 0x2140;
- else if (c == 0xff5e)
- /* F/W TILDE (see NOTES) */
- code = 0x2232;
- else
- return 1;
- }
- else if (c >> 16 == EMPBASE >> 16) {
- EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
- else TRYMAP_ENC(jisx0213_emp, code, c & 0xffff);
- else return insize;
- }
- else
- return insize;
-
- if (code & 0x8000) {
- /* Codeset 2 */
- WRITE3(0x8f, code >> 8, (code & 0xFF) | 0x80)
- NEXT(insize, 3)
- } else {
- /* Codeset 1 */
- WRITE2((code >> 8) | 0x80, (code & 0xFF) | 0x80)
- NEXT(insize, 2)
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ ucs4_t c = IN1;
+ DBCHAR code;
+ Py_ssize_t insize;
+
+ if (c < 0x80) {
+ WRITE1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ DECODE_SURROGATE(c)
+ insize = GET_INSIZE(c);
+
+ if (c <= 0xFFFF) {
+ EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
+ else TRYMAP_ENC(jisx0213_bmp, code, c) {
+ if (code == MULTIC) {
+ if (inleft < 2) {
+ if (flags & MBENC_FLUSH) {
+ code = find_pairencmap(
+ (ucs2_t)c, 0,
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV)
+ return 1;
+ }
+ else
+ return MBERR_TOOFEW;
+ }
+ else {
+ code = find_pairencmap(
+ (ucs2_t)c, (*inbuf)[1],
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV) {
+ code = find_pairencmap(
+ (ucs2_t)c, 0,
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV)
+ return 1;
+ } else
+ insize = 2;
+ }
+ }
+ }
+ else TRYMAP_ENC(jisxcommon, code, c);
+ else if (c >= 0xff61 && c <= 0xff9f) {
+ /* JIS X 0201 half-width katakana */
+ WRITE2(0x8e, c - 0xfec0)
+ NEXT(1, 2)
+ continue;
+ }
+ else if (c == 0xff3c)
+ /* F/W REVERSE SOLIDUS (see NOTES) */
+ code = 0x2140;
+ else if (c == 0xff5e)
+ /* F/W TILDE (see NOTES) */
+ code = 0x2232;
+ else
+ return 1;
+ }
+ else if (c >> 16 == EMPBASE >> 16) {
+ EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
+ else TRYMAP_ENC(jisx0213_emp, code, c & 0xffff);
+ else return insize;
+ }
+ else
+ return insize;
+
+ if (code & 0x8000) {
+ /* Codeset 2 */
+ WRITE3(0x8f, code >> 8, (code & 0xFF) | 0x80)
+ NEXT(insize, 3)
+ } else {
+ /* Codeset 1 */
+ WRITE2((code >> 8) | 0x80, (code & 0xFF) | 0x80)
+ NEXT(insize, 2)
+ }
+ }
+
+ return 0;
}
DECODER(euc_jis_2004)
{
- while (inleft > 0) {
- unsigned char c = IN1;
- ucs4_t code;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- if (c == 0x8e) {
- /* JIS X 0201 half-width katakana */
- unsigned char c2;
-
- REQUIRE_INBUF(2)
- c2 = IN2;
- if (c2 >= 0xa1 && c2 <= 0xdf) {
- OUT1(0xfec0 + c2)
- NEXT(2, 1)
- }
- else
- return 2;
- }
- else if (c == 0x8f) {
- unsigned char c2, c3;
-
- REQUIRE_INBUF(3)
- c2 = IN2 ^ 0x80;
- c3 = IN3 ^ 0x80;
-
- /* JIS X 0213 Plane 2 or JIS X 0212 (see NOTES) */
- EMULATE_JISX0213_2000_DECODE_PLANE2(**outbuf, c2, c3)
- else TRYMAP_DEC(jisx0213_2_bmp, **outbuf, c2, c3) ;
- else TRYMAP_DEC(jisx0213_2_emp, code, c2, c3) {
- WRITEUCS4(EMPBASE | code)
- NEXT_IN(3)
- continue;
- }
- else TRYMAP_DEC(jisx0212, **outbuf, c2, c3) ;
- else return 3;
- NEXT(3, 1)
- }
- else {
- unsigned char c2;
-
- REQUIRE_INBUF(2)
- c ^= 0x80;
- c2 = IN2 ^ 0x80;
-
- /* JIS X 0213 Plane 1 */
- EMULATE_JISX0213_2000_DECODE_PLANE1(**outbuf, c, c2)
- else if (c == 0x21 && c2 == 0x40) **outbuf = 0xff3c;
- else if (c == 0x22 && c2 == 0x32) **outbuf = 0xff5e;
- else TRYMAP_DEC(jisx0208, **outbuf, c, c2);
- else TRYMAP_DEC(jisx0213_1_bmp, **outbuf, c, c2);
- else TRYMAP_DEC(jisx0213_1_emp, code, c, c2) {
- WRITEUCS4(EMPBASE | code)
- NEXT_IN(2)
- continue;
- }
- else TRYMAP_DEC(jisx0213_pair, code, c, c2) {
- WRITE2(code >> 16, code & 0xffff)
- NEXT(2, 2)
- continue;
- }
- else return 2;
- NEXT(2, 1)
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1;
+ ucs4_t code;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ if (c == 0x8e) {
+ /* JIS X 0201 half-width katakana */
+ unsigned char c2;
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+ if (c2 >= 0xa1 && c2 <= 0xdf) {
+ OUT1(0xfec0 + c2)
+ NEXT(2, 1)
+ }
+ else
+ return 2;
+ }
+ else if (c == 0x8f) {
+ unsigned char c2, c3;
+
+ REQUIRE_INBUF(3)
+ c2 = IN2 ^ 0x80;
+ c3 = IN3 ^ 0x80;
+
+ /* JIS X 0213 Plane 2 or JIS X 0212 (see NOTES) */
+ EMULATE_JISX0213_2000_DECODE_PLANE2(**outbuf, c2, c3)
+ else TRYMAP_DEC(jisx0213_2_bmp, **outbuf, c2, c3) ;
+ else TRYMAP_DEC(jisx0213_2_emp, code, c2, c3) {
+ WRITEUCS4(EMPBASE | code)
+ NEXT_IN(3)
+ continue;
+ }
+ else TRYMAP_DEC(jisx0212, **outbuf, c2, c3) ;
+ else return 3;
+ NEXT(3, 1)
+ }
+ else {
+ unsigned char c2;
+
+ REQUIRE_INBUF(2)
+ c ^= 0x80;
+ c2 = IN2 ^ 0x80;
+
+ /* JIS X 0213 Plane 1 */
+ EMULATE_JISX0213_2000_DECODE_PLANE1(**outbuf, c, c2)
+ else if (c == 0x21 && c2 == 0x40) **outbuf = 0xff3c;
+ else if (c == 0x22 && c2 == 0x32) **outbuf = 0xff5e;
+ else TRYMAP_DEC(jisx0208, **outbuf, c, c2);
+ else TRYMAP_DEC(jisx0213_1_bmp, **outbuf, c, c2);
+ else TRYMAP_DEC(jisx0213_1_emp, code, c, c2) {
+ WRITEUCS4(EMPBASE | code)
+ NEXT_IN(2)
+ continue;
+ }
+ else TRYMAP_DEC(jisx0213_pair, code, c, c2) {
+ WRITE2(code >> 16, code & 0xffff)
+ NEXT(2, 2)
+ continue;
+ }
+ else return 2;
+ NEXT(2, 1)
+ }
+ }
+
+ return 0;
}
@@ -315,114 +315,114 @@ DECODER(euc_jis_2004)
ENCODER(euc_jp)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
-
- UCS4INVALID(c)
-
- TRYMAP_ENC(jisxcommon, code, c);
- else if (c >= 0xff61 && c <= 0xff9f) {
- /* JIS X 0201 half-width katakana */
- WRITE2(0x8e, c - 0xfec0)
- NEXT(1, 2)
- continue;
- }
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ UCS4INVALID(c)
+
+ TRYMAP_ENC(jisxcommon, code, c);
+ else if (c >= 0xff61 && c <= 0xff9f) {
+ /* JIS X 0201 half-width katakana */
+ WRITE2(0x8e, c - 0xfec0)
+ NEXT(1, 2)
+ continue;
+ }
#ifndef STRICT_BUILD
- else if (c == 0xff3c) /* FULL-WIDTH REVERSE SOLIDUS */
- code = 0x2140;
- else if (c == 0xa5) { /* YEN SIGN */
- WRITE1(0x5c);
- NEXT(1, 1)
- continue;
- } else if (c == 0x203e) { /* OVERLINE */
- WRITE1(0x7e);
- NEXT(1, 1)
- continue;
- }
+ else if (c == 0xff3c) /* FULL-WIDTH REVERSE SOLIDUS */
+ code = 0x2140;
+ else if (c == 0xa5) { /* YEN SIGN */
+ WRITE1(0x5c);
+ NEXT(1, 1)
+ continue;
+ } else if (c == 0x203e) { /* OVERLINE */
+ WRITE1(0x7e);
+ NEXT(1, 1)
+ continue;
+ }
#endif
- else
- return 1;
-
- if (code & 0x8000) {
- /* JIS X 0212 */
- WRITE3(0x8f, code >> 8, (code & 0xFF) | 0x80)
- NEXT(1, 3)
- } else {
- /* JIS X 0208 */
- WRITE2((code >> 8) | 0x80, (code & 0xFF) | 0x80)
- NEXT(1, 2)
- }
- }
-
- return 0;
+ else
+ return 1;
+
+ if (code & 0x8000) {
+ /* JIS X 0212 */
+ WRITE3(0x8f, code >> 8, (code & 0xFF) | 0x80)
+ NEXT(1, 3)
+ } else {
+ /* JIS X 0208 */
+ WRITE2((code >> 8) | 0x80, (code & 0xFF) | 0x80)
+ NEXT(1, 2)
+ }
+ }
+
+ return 0;
}
DECODER(euc_jp)
{
- while (inleft > 0) {
- unsigned char c = IN1;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- if (c == 0x8e) {
- /* JIS X 0201 half-width katakana */
- unsigned char c2;
-
- REQUIRE_INBUF(2)
- c2 = IN2;
- if (c2 >= 0xa1 && c2 <= 0xdf) {
- OUT1(0xfec0 + c2)
- NEXT(2, 1)
- }
- else
- return 2;
- }
- else if (c == 0x8f) {
- unsigned char c2, c3;
-
- REQUIRE_INBUF(3)
- c2 = IN2;
- c3 = IN3;
- /* JIS X 0212 */
- TRYMAP_DEC(jisx0212, **outbuf, c2 ^ 0x80, c3 ^ 0x80) {
- NEXT(3, 1)
- }
- else
- return 3;
- }
- else {
- unsigned char c2;
-
- REQUIRE_INBUF(2)
- c2 = IN2;
- /* JIS X 0208 */
+ while (inleft > 0) {
+ unsigned char c = IN1;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ if (c == 0x8e) {
+ /* JIS X 0201 half-width katakana */
+ unsigned char c2;
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+ if (c2 >= 0xa1 && c2 <= 0xdf) {
+ OUT1(0xfec0 + c2)
+ NEXT(2, 1)
+ }
+ else
+ return 2;
+ }
+ else if (c == 0x8f) {
+ unsigned char c2, c3;
+
+ REQUIRE_INBUF(3)
+ c2 = IN2;
+ c3 = IN3;
+ /* JIS X 0212 */
+ TRYMAP_DEC(jisx0212, **outbuf, c2 ^ 0x80, c3 ^ 0x80) {
+ NEXT(3, 1)
+ }
+ else
+ return 3;
+ }
+ else {
+ unsigned char c2;
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+ /* JIS X 0208 */
#ifndef STRICT_BUILD
- if (c == 0xa1 && c2 == 0xc0)
- /* FULL-WIDTH REVERSE SOLIDUS */
- **outbuf = 0xff3c;
- else
+ if (c == 0xa1 && c2 == 0xc0)
+ /* FULL-WIDTH REVERSE SOLIDUS */
+ **outbuf = 0xff3c;
+ else
#endif
- TRYMAP_DEC(jisx0208, **outbuf,
- c ^ 0x80, c2 ^ 0x80) ;
- else return 2;
- NEXT(2, 1)
- }
- }
-
- return 0;
+ TRYMAP_DEC(jisx0208, **outbuf,
+ c ^ 0x80, c2 ^ 0x80) ;
+ else return 2;
+ NEXT(2, 1)
+ }
+ }
+
+ return 0;
}
@@ -432,105 +432,105 @@ DECODER(euc_jp)
ENCODER(shift_jis)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
- unsigned char c1, c2;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+ unsigned char c1, c2;
#ifdef STRICT_BUILD
- JISX0201_R_ENCODE(c, code)
+ JISX0201_R_ENCODE(c, code)
#else
- if (c < 0x80) code = c;
- else if (c == 0x00a5) code = 0x5c; /* YEN SIGN */
- else if (c == 0x203e) code = 0x7e; /* OVERLINE */
+ if (c < 0x80) code = c;
+ else if (c == 0x00a5) code = 0x5c; /* YEN SIGN */
+ else if (c == 0x203e) code = 0x7e; /* OVERLINE */
#endif
- else JISX0201_K_ENCODE(c, code)
- else UCS4INVALID(c)
- else code = NOCHAR;
+ else JISX0201_K_ENCODE(c, code)
+ else UCS4INVALID(c)
+ else code = NOCHAR;
- if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
- REQUIRE_OUTBUF(1)
+ if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
+ REQUIRE_OUTBUF(1)
- OUT1((unsigned char)code)
- NEXT(1, 1)
- continue;
- }
+ OUT1((unsigned char)code)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_OUTBUF(2)
+ REQUIRE_OUTBUF(2)
- if (code == NOCHAR) {
- TRYMAP_ENC(jisxcommon, code, c);
+ if (code == NOCHAR) {
+ TRYMAP_ENC(jisxcommon, code, c);
#ifndef STRICT_BUILD
- else if (c == 0xff3c)
- code = 0x2140; /* FULL-WIDTH REVERSE SOLIDUS */
+ else if (c == 0xff3c)
+ code = 0x2140; /* FULL-WIDTH REVERSE SOLIDUS */
#endif
- else
- return 1;
-
- if (code & 0x8000) /* MSB set: JIS X 0212 */
- return 1;
- }
-
- c1 = code >> 8;
- c2 = code & 0xff;
- c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
- c1 = (c1 - 0x21) >> 1;
- OUT1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1)
- OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
- NEXT(1, 2)
- }
-
- return 0;
+ else
+ return 1;
+
+ if (code & 0x8000) /* MSB set: JIS X 0212 */
+ return 1;
+ }
+
+ c1 = code >> 8;
+ c2 = code & 0xff;
+ c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
+ c1 = (c1 - 0x21) >> 1;
+ OUT1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1)
+ OUT2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41)
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(shift_jis)
{
- while (inleft > 0) {
- unsigned char c = IN1;
+ while (inleft > 0) {
+ unsigned char c = IN1;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
#ifdef STRICT_BUILD
- JISX0201_R_DECODE(c, **outbuf)
+ JISX0201_R_DECODE(c, **outbuf)
#else
- if (c < 0x80) **outbuf = c;
+ if (c < 0x80) **outbuf = c;
#endif
- else JISX0201_K_DECODE(c, **outbuf)
- else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){
- unsigned char c1, c2;
+ else JISX0201_K_DECODE(c, **outbuf)
+ else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){
+ unsigned char c1, c2;
- REQUIRE_INBUF(2)
- c2 = IN2;
- if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
- return 2;
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+ if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
+ return 2;
- c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1);
- c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
- c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1) + 0x21);
- c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
+ c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1);
+ c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
+ c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1) + 0x21);
+ c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
#ifndef STRICT_BUILD
- if (c1 == 0x21 && c2 == 0x40) {
- /* FULL-WIDTH REVERSE SOLIDUS */
- OUT1(0xff3c)
- NEXT(2, 1)
- continue;
- }
+ if (c1 == 0x21 && c2 == 0x40) {
+ /* FULL-WIDTH REVERSE SOLIDUS */
+ OUT1(0xff3c)
+ NEXT(2, 1)
+ continue;
+ }
#endif
- TRYMAP_DEC(jisx0208, **outbuf, c1, c2) {
- NEXT(2, 1)
- continue;
- }
- else
- return 2;
- }
- else
- return 2;
-
- NEXT(1, 1) /* JIS X 0201 */
- }
-
- return 0;
+ TRYMAP_DEC(jisx0208, **outbuf, c1, c2) {
+ NEXT(2, 1)
+ continue;
+ }
+ else
+ return 2;
+ }
+ else
+ return 2;
+
+ NEXT(1, 1) /* JIS X 0201 */
+ }
+
+ return 0;
}
@@ -540,167 +540,167 @@ DECODER(shift_jis)
ENCODER(shift_jis_2004)
{
- while (inleft > 0) {
- ucs4_t c = IN1;
- DBCHAR code = NOCHAR;
- int c1, c2;
- Py_ssize_t insize;
-
- JISX0201_ENCODE(c, code)
- else DECODE_SURROGATE(c)
-
- if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
- WRITE1((unsigned char)code)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_OUTBUF(2)
- insize = GET_INSIZE(c);
-
- if (code == NOCHAR) {
- if (c <= 0xffff) {
- EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
- else TRYMAP_ENC(jisx0213_bmp, code, c) {
- if (code == MULTIC) {
- if (inleft < 2) {
- if (flags & MBENC_FLUSH) {
- code = find_pairencmap
- ((ucs2_t)c, 0,
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV)
- return 1;
- }
- else
- return MBERR_TOOFEW;
- }
- else {
- code = find_pairencmap(
- (ucs2_t)c, IN2,
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV) {
- code = find_pairencmap(
- (ucs2_t)c, 0,
- jisx0213_pair_encmap,
- JISX0213_ENCPAIRS);
- if (code == DBCINV)
- return 1;
- }
- else
- insize = 2;
- }
- }
- }
- else TRYMAP_ENC(jisxcommon, code, c) {
- /* abandon JIS X 0212 codes */
- if (code & 0x8000)
- return 1;
- }
- else return 1;
- }
- else if (c >> 16 == EMPBASE >> 16) {
- EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
- else TRYMAP_ENC(jisx0213_emp, code, c&0xffff);
- else return insize;
- }
- else
- return insize;
- }
-
- c1 = code >> 8;
- c2 = (code & 0xff) - 0x21;
-
- if (c1 & 0x80) { /* Plane 2 */
- if (c1 >= 0xee) c1 -= 0x87;
- else if (c1 >= 0xac || c1 == 0xa8) c1 -= 0x49;
- else c1 -= 0x43;
- }
- else /* Plane 1 */
- c1 -= 0x21;
-
- if (c1 & 1) c2 += 0x5e;
- c1 >>= 1;
- OUT1(c1 + (c1 < 0x1f ? 0x81 : 0xc1))
- OUT2(c2 + (c2 < 0x3f ? 0x40 : 0x41))
-
- NEXT(insize, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ ucs4_t c = IN1;
+ DBCHAR code = NOCHAR;
+ int c1, c2;
+ Py_ssize_t insize;
+
+ JISX0201_ENCODE(c, code)
+ else DECODE_SURROGATE(c)
+
+ if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) {
+ WRITE1((unsigned char)code)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_OUTBUF(2)
+ insize = GET_INSIZE(c);
+
+ if (code == NOCHAR) {
+ if (c <= 0xffff) {
+ EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
+ else TRYMAP_ENC(jisx0213_bmp, code, c) {
+ if (code == MULTIC) {
+ if (inleft < 2) {
+ if (flags & MBENC_FLUSH) {
+ code = find_pairencmap
+ ((ucs2_t)c, 0,
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV)
+ return 1;
+ }
+ else
+ return MBERR_TOOFEW;
+ }
+ else {
+ code = find_pairencmap(
+ (ucs2_t)c, IN2,
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV) {
+ code = find_pairencmap(
+ (ucs2_t)c, 0,
+ jisx0213_pair_encmap,
+ JISX0213_ENCPAIRS);
+ if (code == DBCINV)
+ return 1;
+ }
+ else
+ insize = 2;
+ }
+ }
+ }
+ else TRYMAP_ENC(jisxcommon, code, c) {
+ /* abandon JIS X 0212 codes */
+ if (code & 0x8000)
+ return 1;
+ }
+ else return 1;
+ }
+ else if (c >> 16 == EMPBASE >> 16) {
+ EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
+ else TRYMAP_ENC(jisx0213_emp, code, c&0xffff);
+ else return insize;
+ }
+ else
+ return insize;
+ }
+
+ c1 = code >> 8;
+ c2 = (code & 0xff) - 0x21;
+
+ if (c1 & 0x80) { /* Plane 2 */
+ if (c1 >= 0xee) c1 -= 0x87;
+ else if (c1 >= 0xac || c1 == 0xa8) c1 -= 0x49;
+ else c1 -= 0x43;
+ }
+ else /* Plane 1 */
+ c1 -= 0x21;
+
+ if (c1 & 1) c2 += 0x5e;
+ c1 >>= 1;
+ OUT1(c1 + (c1 < 0x1f ? 0x81 : 0xc1))
+ OUT2(c2 + (c2 < 0x3f ? 0x40 : 0x41))
+
+ NEXT(insize, 2)
+ }
+
+ return 0;
}
DECODER(shift_jis_2004)
{
- while (inleft > 0) {
- unsigned char c = IN1;
-
- REQUIRE_OUTBUF(1)
- JISX0201_DECODE(c, **outbuf)
- else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)){
- unsigned char c1, c2;
- ucs4_t code;
-
- REQUIRE_INBUF(2)
- c2 = IN2;
- if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
- return 2;
-
- c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1);
- c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
- c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1));
- c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
-
- if (c1 < 0x5e) { /* Plane 1 */
- c1 += 0x21;
- EMULATE_JISX0213_2000_DECODE_PLANE1(**outbuf,
- c1, c2)
- else TRYMAP_DEC(jisx0208, **outbuf, c1, c2) {
- NEXT_OUT(1)
- }
- else TRYMAP_DEC(jisx0213_1_bmp, **outbuf,
- c1, c2) {
- NEXT_OUT(1)
- }
- else TRYMAP_DEC(jisx0213_1_emp, code, c1, c2) {
- WRITEUCS4(EMPBASE | code)
- }
- else TRYMAP_DEC(jisx0213_pair, code, c1, c2) {
- WRITE2(code >> 16, code & 0xffff)
- NEXT_OUT(2)
- }
- else
- return 2;
- NEXT_IN(2)
- }
- else { /* Plane 2 */
- if (c1 >= 0x67) c1 += 0x07;
- else if (c1 >= 0x63 || c1 == 0x5f) c1 -= 0x37;
- else c1 -= 0x3d;
-
- EMULATE_JISX0213_2000_DECODE_PLANE2(**outbuf,
- c1, c2)
- else TRYMAP_DEC(jisx0213_2_bmp, **outbuf,
- c1, c2) ;
- else TRYMAP_DEC(jisx0213_2_emp, code, c1, c2) {
- WRITEUCS4(EMPBASE | code)
- NEXT_IN(2)
- continue;
- }
- else
- return 2;
- NEXT(2, 1)
- }
- continue;
- }
- else
- return 2;
-
- NEXT(1, 1) /* JIS X 0201 */
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1;
+
+ REQUIRE_OUTBUF(1)
+ JISX0201_DECODE(c, **outbuf)
+ else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)){
+ unsigned char c1, c2;
+ ucs4_t code;
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+ if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc)
+ return 2;
+
+ c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1);
+ c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41);
+ c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1));
+ c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21;
+
+ if (c1 < 0x5e) { /* Plane 1 */
+ c1 += 0x21;
+ EMULATE_JISX0213_2000_DECODE_PLANE1(**outbuf,
+ c1, c2)
+ else TRYMAP_DEC(jisx0208, **outbuf, c1, c2) {
+ NEXT_OUT(1)
+ }
+ else TRYMAP_DEC(jisx0213_1_bmp, **outbuf,
+ c1, c2) {
+ NEXT_OUT(1)
+ }
+ else TRYMAP_DEC(jisx0213_1_emp, code, c1, c2) {
+ WRITEUCS4(EMPBASE | code)
+ }
+ else TRYMAP_DEC(jisx0213_pair, code, c1, c2) {
+ WRITE2(code >> 16, code & 0xffff)
+ NEXT_OUT(2)
+ }
+ else
+ return 2;
+ NEXT_IN(2)
+ }
+ else { /* Plane 2 */
+ if (c1 >= 0x67) c1 += 0x07;
+ else if (c1 >= 0x63 || c1 == 0x5f) c1 -= 0x37;
+ else c1 -= 0x3d;
+
+ EMULATE_JISX0213_2000_DECODE_PLANE2(**outbuf,
+ c1, c2)
+ else TRYMAP_DEC(jisx0213_2_bmp, **outbuf,
+ c1, c2) ;
+ else TRYMAP_DEC(jisx0213_2_emp, code, c1, c2) {
+ WRITEUCS4(EMPBASE | code)
+ NEXT_IN(2)
+ continue;
+ }
+ else
+ return 2;
+ NEXT(2, 1)
+ }
+ continue;
+ }
+ else
+ return 2;
+
+ NEXT(1, 1) /* JIS X 0201 */
+ }
+
+ return 0;
}
diff --git a/Modules/cjkcodecs/_codecs_kr.c b/Modules/cjkcodecs/_codecs_kr.c
index 161967e..9272e36 100644
--- a/Modules/cjkcodecs/_codecs_kr.c
+++ b/Modules/cjkcodecs/_codecs_kr.c
@@ -11,151 +11,151 @@
* EUC-KR codec
*/
-#define EUCKR_JAMO_FIRSTBYTE 0xA4
-#define EUCKR_JAMO_FILLER 0xD4
+#define EUCKR_JAMO_FIRSTBYTE 0xA4
+#define EUCKR_JAMO_FILLER 0xD4
static const unsigned char u2cgk_choseong[19] = {
- 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xa9, 0xb1, 0xb2,
- 0xb3, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
- 0xbc, 0xbd, 0xbe
+ 0xa1, 0xa2, 0xa4, 0xa7, 0xa8, 0xa9, 0xb1, 0xb2,
+ 0xb3, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
+ 0xbc, 0xbd, 0xbe
};
static const unsigned char u2cgk_jungseong[21] = {
- 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
- 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
- 0xcf, 0xd0, 0xd1, 0xd2, 0xd3
+ 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
+ 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
+ 0xcf, 0xd0, 0xd1, 0xd2, 0xd3
};
static const unsigned char u2cgk_jongseong[28] = {
- 0xd4, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0,
- 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xba,
- 0xbb, 0xbc, 0xbd, 0xbe
+ 0xd4, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0,
+ 0xb1, 0xb2, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xba,
+ 0xbb, 0xbc, 0xbd, 0xbe
};
ENCODER(euc_kr)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
- TRYMAP_ENC(cp949, code, c);
- else return 1;
-
- if ((code & 0x8000) == 0) {
- /* KS X 1001 coded character */
- OUT1((code >> 8) | 0x80)
- OUT2((code & 0xFF) | 0x80)
- NEXT(1, 2)
- }
- else { /* Mapping is found in CP949 extension,
- * but we encode it in KS X 1001:1998 Annex 3,
- * make-up sequence for EUC-KR. */
-
- REQUIRE_OUTBUF(8)
-
- /* syllable composition precedence */
- OUT1(EUCKR_JAMO_FIRSTBYTE)
- OUT2(EUCKR_JAMO_FILLER)
-
- /* All codepoints in CP949 extension are in unicode
- * Hangul Syllable area. */
- assert(0xac00 <= c && c <= 0xd7a3);
- c -= 0xac00;
-
- OUT3(EUCKR_JAMO_FIRSTBYTE)
- OUT4(u2cgk_choseong[c / 588])
- NEXT_OUT(4)
-
- OUT1(EUCKR_JAMO_FIRSTBYTE)
- OUT2(u2cgk_jungseong[(c / 28) % 21])
- OUT3(EUCKR_JAMO_FIRSTBYTE)
- OUT4(u2cgk_jongseong[c % 28])
- NEXT(1, 4)
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+ TRYMAP_ENC(cp949, code, c);
+ else return 1;
+
+ if ((code & 0x8000) == 0) {
+ /* KS X 1001 coded character */
+ OUT1((code >> 8) | 0x80)
+ OUT2((code & 0xFF) | 0x80)
+ NEXT(1, 2)
+ }
+ else { /* Mapping is found in CP949 extension,
+ * but we encode it in KS X 1001:1998 Annex 3,
+ * make-up sequence for EUC-KR. */
+
+ REQUIRE_OUTBUF(8)
+
+ /* syllable composition precedence */
+ OUT1(EUCKR_JAMO_FIRSTBYTE)
+ OUT2(EUCKR_JAMO_FILLER)
+
+ /* All codepoints in CP949 extension are in unicode
+ * Hangul Syllable area. */
+ assert(0xac00 <= c && c <= 0xd7a3);
+ c -= 0xac00;
+
+ OUT3(EUCKR_JAMO_FIRSTBYTE)
+ OUT4(u2cgk_choseong[c / 588])
+ NEXT_OUT(4)
+
+ OUT1(EUCKR_JAMO_FIRSTBYTE)
+ OUT2(u2cgk_jungseong[(c / 28) % 21])
+ OUT3(EUCKR_JAMO_FIRSTBYTE)
+ OUT4(u2cgk_jongseong[c % 28])
+ NEXT(1, 4)
+ }
+ }
+
+ return 0;
}
-#define NONE 127
+#define NONE 127
static const unsigned char cgk2u_choseong[] = { /* [A1, BE] */
- 0, 1, NONE, 2, NONE, NONE, 3, 4,
- 5, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
- 6, 7, 8, NONE, 9, 10, 11, 12,
- 13, 14, 15, 16, 17, 18
+ 0, 1, NONE, 2, NONE, NONE, 3, 4,
+ 5, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
+ 6, 7, 8, NONE, 9, 10, 11, 12,
+ 13, 14, 15, 16, 17, 18
};
static const unsigned char cgk2u_jongseong[] = { /* [A1, BE] */
- 1, 2, 3, 4, 5, 6, 7, NONE,
- 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, NONE, 18, 19, 20, 21, 22,
- NONE, 23, 24, 25, 26, 27
+ 1, 2, 3, 4, 5, 6, 7, NONE,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, NONE, 18, 19, 20, 21, 22,
+ NONE, 23, 24, 25, 26, 27
};
DECODER(euc_kr)
{
- while (inleft > 0) {
- unsigned char c = IN1;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_INBUF(2)
-
- if (c == EUCKR_JAMO_FIRSTBYTE &&
- IN2 == EUCKR_JAMO_FILLER) {
- /* KS X 1001:1998 Annex 3 make-up sequence */
- DBCHAR cho, jung, jong;
-
- REQUIRE_INBUF(8)
- if ((*inbuf)[2] != EUCKR_JAMO_FIRSTBYTE ||
- (*inbuf)[4] != EUCKR_JAMO_FIRSTBYTE ||
- (*inbuf)[6] != EUCKR_JAMO_FIRSTBYTE)
- return 8;
-
- c = (*inbuf)[3];
- if (0xa1 <= c && c <= 0xbe)
- cho = cgk2u_choseong[c - 0xa1];
- else
- cho = NONE;
-
- c = (*inbuf)[5];
- jung = (0xbf <= c && c <= 0xd3) ? c - 0xbf : NONE;
-
- c = (*inbuf)[7];
- if (c == EUCKR_JAMO_FILLER)
- jong = 0;
- else if (0xa1 <= c && c <= 0xbe)
- jong = cgk2u_jongseong[c - 0xa1];
- else
- jong = NONE;
-
- if (cho == NONE || jung == NONE || jong == NONE)
- return 8;
-
- OUT1(0xac00 + cho*588 + jung*28 + jong);
- NEXT(8, 1)
- }
- else TRYMAP_DEC(ksx1001, **outbuf, c ^ 0x80, IN2 ^ 0x80) {
- NEXT(2, 1)
- }
- else
- return 2;
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_INBUF(2)
+
+ if (c == EUCKR_JAMO_FIRSTBYTE &&
+ IN2 == EUCKR_JAMO_FILLER) {
+ /* KS X 1001:1998 Annex 3 make-up sequence */
+ DBCHAR cho, jung, jong;
+
+ REQUIRE_INBUF(8)
+ if ((*inbuf)[2] != EUCKR_JAMO_FIRSTBYTE ||
+ (*inbuf)[4] != EUCKR_JAMO_FIRSTBYTE ||
+ (*inbuf)[6] != EUCKR_JAMO_FIRSTBYTE)
+ return 8;
+
+ c = (*inbuf)[3];
+ if (0xa1 <= c && c <= 0xbe)
+ cho = cgk2u_choseong[c - 0xa1];
+ else
+ cho = NONE;
+
+ c = (*inbuf)[5];
+ jung = (0xbf <= c && c <= 0xd3) ? c - 0xbf : NONE;
+
+ c = (*inbuf)[7];
+ if (c == EUCKR_JAMO_FILLER)
+ jong = 0;
+ else if (0xa1 <= c && c <= 0xbe)
+ jong = cgk2u_jongseong[c - 0xa1];
+ else
+ jong = NONE;
+
+ if (cho == NONE || jung == NONE || jong == NONE)
+ return 8;
+
+ OUT1(0xac00 + cho*588 + jung*28 + jong);
+ NEXT(8, 1)
+ }
+ else TRYMAP_DEC(ksx1001, **outbuf, c ^ 0x80, IN2 ^ 0x80) {
+ NEXT(2, 1)
+ }
+ else
+ return 2;
+ }
+
+ return 0;
}
#undef NONE
@@ -166,54 +166,54 @@ DECODER(euc_kr)
ENCODER(cp949)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
- TRYMAP_ENC(cp949, code, c);
- else return 1;
-
- OUT1((code >> 8) | 0x80)
- if (code & 0x8000)
- OUT2(code & 0xFF) /* MSB set: CP949 */
- else
- OUT2((code & 0xFF) | 0x80) /* MSB unset: ks x 1001 */
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+ TRYMAP_ENC(cp949, code, c);
+ else return 1;
+
+ OUT1((code >> 8) | 0x80)
+ if (code & 0x8000)
+ OUT2(code & 0xFF) /* MSB set: CP949 */
+ else
+ OUT2((code & 0xFF) | 0x80) /* MSB unset: ks x 1001 */
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(cp949)
{
- while (inleft > 0) {
- unsigned char c = IN1;
+ while (inleft > 0) {
+ unsigned char c = IN1;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_INBUF(2)
- TRYMAP_DEC(ksx1001, **outbuf, c ^ 0x80, IN2 ^ 0x80);
- else TRYMAP_DEC(cp949ext, **outbuf, c, IN2);
- else return 2;
+ REQUIRE_INBUF(2)
+ TRYMAP_DEC(ksx1001, **outbuf, c ^ 0x80, IN2 ^ 0x80);
+ else TRYMAP_DEC(cp949ext, **outbuf, c, IN2);
+ else return 2;
- NEXT(2, 1)
- }
+ NEXT(2, 1)
+ }
- return 0;
+ return 0;
}
@@ -250,58 +250,58 @@ static const DBCHAR u2johabjamo[] = {
ENCODER(johab)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
-
- if (c >= 0xac00 && c <= 0xd7a3) {
- c -= 0xac00;
- code = 0x8000 |
- (u2johabidx_choseong[c / 588] << 10) |
- (u2johabidx_jungseong[(c / 28) % 21] << 5) |
- u2johabidx_jongseong[c % 28];
- }
- else if (c >= 0x3131 && c <= 0x3163)
- code = u2johabjamo[c - 0x3131];
- else TRYMAP_ENC(cp949, code, c) {
- unsigned char c1, c2, t2;
- unsigned short t1;
-
- assert((code & 0x8000) == 0);
- c1 = code >> 8;
- c2 = code & 0xff;
- if (((c1 >= 0x21 && c1 <= 0x2c) ||
- (c1 >= 0x4a && c1 <= 0x7d)) &&
- (c2 >= 0x21 && c2 <= 0x7e)) {
- t1 = (c1 < 0x4a ? (c1 - 0x21 + 0x1b2) :
- (c1 - 0x21 + 0x197));
- t2 = ((t1 & 1) ? 0x5e : 0) + (c2 - 0x21);
- OUT1(t1 >> 1)
- OUT2(t2 < 0x4e ? t2 + 0x31 : t2 + 0x43)
- NEXT(1, 2)
- continue;
- }
- else
- return 1;
- }
- else
- return 1;
-
- OUT1(code >> 8)
- OUT2(code & 0xff)
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+
+ if (c >= 0xac00 && c <= 0xd7a3) {
+ c -= 0xac00;
+ code = 0x8000 |
+ (u2johabidx_choseong[c / 588] << 10) |
+ (u2johabidx_jungseong[(c / 28) % 21] << 5) |
+ u2johabidx_jongseong[c % 28];
+ }
+ else if (c >= 0x3131 && c <= 0x3163)
+ code = u2johabjamo[c - 0x3131];
+ else TRYMAP_ENC(cp949, code, c) {
+ unsigned char c1, c2, t2;
+ unsigned short t1;
+
+ assert((code & 0x8000) == 0);
+ c1 = code >> 8;
+ c2 = code & 0xff;
+ if (((c1 >= 0x21 && c1 <= 0x2c) ||
+ (c1 >= 0x4a && c1 <= 0x7d)) &&
+ (c2 >= 0x21 && c2 <= 0x7e)) {
+ t1 = (c1 < 0x4a ? (c1 - 0x21 + 0x1b2) :
+ (c1 - 0x21 + 0x197));
+ t2 = ((t1 & 1) ? 0x5e : 0) + (c2 - 0x21);
+ OUT1(t1 >> 1)
+ OUT2(t2 < 0x4e ? t2 + 0x31 : t2 + 0x43)
+ NEXT(1, 2)
+ continue;
+ }
+ else
+ return 1;
+ }
+ else
+ return 1;
+
+ OUT1(code >> 8)
+ OUT2(code & 0xff)
+ NEXT(1, 2)
+ }
+
+ return 0;
}
#define FILL 0xfd
@@ -347,91 +347,91 @@ static const unsigned char johabjamo_jongseong[32] = {
DECODER(johab)
{
- while (inleft > 0) {
- unsigned char c = IN1, c2;
-
- REQUIRE_OUTBUF(1)
-
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
-
- REQUIRE_INBUF(2)
- c2 = IN2;
-
- if (c < 0xd8) {
- /* johab hangul */
- unsigned char c_cho, c_jung, c_jong;
- unsigned char i_cho, i_jung, i_jong;
-
- c_cho = (c >> 2) & 0x1f;
- c_jung = ((c << 3) | c2 >> 5) & 0x1f;
- c_jong = c2 & 0x1f;
-
- i_cho = johabidx_choseong[c_cho];
- i_jung = johabidx_jungseong[c_jung];
- i_jong = johabidx_jongseong[c_jong];
-
- if (i_cho == NONE || i_jung == NONE || i_jong == NONE)
- return 2;
-
- /* we don't use U+1100 hangul jamo yet. */
- if (i_cho == FILL) {
- if (i_jung == FILL) {
- if (i_jong == FILL)
- OUT1(0x3000)
- else
- OUT1(0x3100 |
- johabjamo_jongseong[c_jong])
- }
- else {
- if (i_jong == FILL)
- OUT1(0x3100 |
- johabjamo_jungseong[c_jung])
- else
- return 2;
- }
- } else {
- if (i_jung == FILL) {
- if (i_jong == FILL)
- OUT1(0x3100 |
- johabjamo_choseong[c_cho])
- else
- return 2;
- }
- else
- OUT1(0xac00 +
- i_cho * 588 +
- i_jung * 28 +
- (i_jong == FILL ? 0 : i_jong))
- }
- NEXT(2, 1)
- } else {
- /* KS X 1001 except hangul jamos and syllables */
- if (c == 0xdf || c > 0xf9 ||
- c2 < 0x31 || (c2 >= 0x80 && c2 < 0x91) ||
- (c2 & 0x7f) == 0x7f ||
- (c == 0xda && (c2 >= 0xa1 && c2 <= 0xd3)))
- return 2;
- else {
- unsigned char t1, t2;
-
- t1 = (c < 0xe0 ? 2 * (c - 0xd9) :
- 2 * c - 0x197);
- t2 = (c2 < 0x91 ? c2 - 0x31 : c2 - 0x43);
- t1 = t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
- t2 = (t2 < 0x5e ? t2 : t2 - 0x5e) + 0x21;
-
- TRYMAP_DEC(ksx1001, **outbuf, t1, t2);
- else return 2;
- NEXT(2, 1)
- }
- }
- }
-
- return 0;
+ while (inleft > 0) {
+ unsigned char c = IN1, c2;
+
+ REQUIRE_OUTBUF(1)
+
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
+
+ REQUIRE_INBUF(2)
+ c2 = IN2;
+
+ if (c < 0xd8) {
+ /* johab hangul */
+ unsigned char c_cho, c_jung, c_jong;
+ unsigned char i_cho, i_jung, i_jong;
+
+ c_cho = (c >> 2) & 0x1f;
+ c_jung = ((c << 3) | c2 >> 5) & 0x1f;
+ c_jong = c2 & 0x1f;
+
+ i_cho = johabidx_choseong[c_cho];
+ i_jung = johabidx_jungseong[c_jung];
+ i_jong = johabidx_jongseong[c_jong];
+
+ if (i_cho == NONE || i_jung == NONE || i_jong == NONE)
+ return 2;
+
+ /* we don't use U+1100 hangul jamo yet. */
+ if (i_cho == FILL) {
+ if (i_jung == FILL) {
+ if (i_jong == FILL)
+ OUT1(0x3000)
+ else
+ OUT1(0x3100 |
+ johabjamo_jongseong[c_jong])
+ }
+ else {
+ if (i_jong == FILL)
+ OUT1(0x3100 |
+ johabjamo_jungseong[c_jung])
+ else
+ return 2;
+ }
+ } else {
+ if (i_jung == FILL) {
+ if (i_jong == FILL)
+ OUT1(0x3100 |
+ johabjamo_choseong[c_cho])
+ else
+ return 2;
+ }
+ else
+ OUT1(0xac00 +
+ i_cho * 588 +
+ i_jung * 28 +
+ (i_jong == FILL ? 0 : i_jong))
+ }
+ NEXT(2, 1)
+ } else {
+ /* KS X 1001 except hangul jamos and syllables */
+ if (c == 0xdf || c > 0xf9 ||
+ c2 < 0x31 || (c2 >= 0x80 && c2 < 0x91) ||
+ (c2 & 0x7f) == 0x7f ||
+ (c == 0xda && (c2 >= 0xa1 && c2 <= 0xd3)))
+ return 2;
+ else {
+ unsigned char t1, t2;
+
+ t1 = (c < 0xe0 ? 2 * (c - 0xd9) :
+ 2 * c - 0x197);
+ t2 = (c2 < 0x91 ? c2 - 0x31 : c2 - 0x43);
+ t1 = t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
+ t2 = (t2 < 0x5e ? t2 : t2 - 0x5e) + 0x21;
+
+ TRYMAP_DEC(ksx1001, **outbuf, t1, t2);
+ else return 2;
+ NEXT(2, 1)
+ }
+ }
+ }
+
+ return 0;
}
#undef NONE
#undef FILL
diff --git a/Modules/cjkcodecs/_codecs_tw.c b/Modules/cjkcodecs/_codecs_tw.c
index 8ccbca1..38cf723 100644
--- a/Modules/cjkcodecs/_codecs_tw.c
+++ b/Modules/cjkcodecs/_codecs_tw.c
@@ -13,52 +13,52 @@
ENCODER(big5)
{
- while (inleft > 0) {
- Py_UNICODE c = **inbuf;
- DBCHAR code;
+ while (inleft > 0) {
+ Py_UNICODE c = **inbuf;
+ DBCHAR code;
- if (c < 0x80) {
- REQUIRE_OUTBUF(1)
- **outbuf = (unsigned char)c;
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
+ if (c < 0x80) {
+ REQUIRE_OUTBUF(1)
+ **outbuf = (unsigned char)c;
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
- REQUIRE_OUTBUF(2)
+ REQUIRE_OUTBUF(2)
- TRYMAP_ENC(big5, code, c);
- else return 1;
+ TRYMAP_ENC(big5, code, c);
+ else return 1;
- OUT1(code >> 8)
- OUT2(code & 0xFF)
- NEXT(1, 2)
- }
+ OUT1(code >> 8)
+ OUT2(code & 0xFF)
+ NEXT(1, 2)
+ }
- return 0;
+ return 0;
}
DECODER(big5)
{
- while (inleft > 0) {
- unsigned char c = IN1;
+ while (inleft > 0) {
+ unsigned char c = IN1;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_INBUF(2)
- TRYMAP_DEC(big5, **outbuf, c, IN2) {
- NEXT(2, 1)
- }
- else return 2;
- }
+ REQUIRE_INBUF(2)
+ TRYMAP_DEC(big5, **outbuf, c, IN2) {
+ NEXT(2, 1)
+ }
+ else return 2;
+ }
- return 0;
+ return 0;
}
@@ -68,53 +68,53 @@ DECODER(big5)
ENCODER(cp950)
{
- while (inleft > 0) {
- Py_UNICODE c = IN1;
- DBCHAR code;
-
- if (c < 0x80) {
- WRITE1((unsigned char)c)
- NEXT(1, 1)
- continue;
- }
- UCS4INVALID(c)
-
- REQUIRE_OUTBUF(2)
- TRYMAP_ENC(cp950ext, code, c);
- else TRYMAP_ENC(big5, code, c);
- else return 1;
-
- OUT1(code >> 8)
- OUT2(code & 0xFF)
- NEXT(1, 2)
- }
-
- return 0;
+ while (inleft > 0) {
+ Py_UNICODE c = IN1;
+ DBCHAR code;
+
+ if (c < 0x80) {
+ WRITE1((unsigned char)c)
+ NEXT(1, 1)
+ continue;
+ }
+ UCS4INVALID(c)
+
+ REQUIRE_OUTBUF(2)
+ TRYMAP_ENC(cp950ext, code, c);
+ else TRYMAP_ENC(big5, code, c);
+ else return 1;
+
+ OUT1(code >> 8)
+ OUT2(code & 0xFF)
+ NEXT(1, 2)
+ }
+
+ return 0;
}
DECODER(cp950)
{
- while (inleft > 0) {
- unsigned char c = IN1;
+ while (inleft > 0) {
+ unsigned char c = IN1;
- REQUIRE_OUTBUF(1)
+ REQUIRE_OUTBUF(1)
- if (c < 0x80) {
- OUT1(c)
- NEXT(1, 1)
- continue;
- }
+ if (c < 0x80) {
+ OUT1(c)
+ NEXT(1, 1)
+ continue;
+ }
- REQUIRE_INBUF(2)
+ REQUIRE_INBUF(2)
- TRYMAP_DEC(cp950ext, **outbuf, c, IN2);
- else TRYMAP_DEC(big5, **outbuf, c, IN2);
- else return 2;
+ TRYMAP_DEC(cp950ext, **outbuf, c, IN2);
+ else TRYMAP_DEC(big5, **outbuf, c, IN2);
+ else return 2;
- NEXT(2, 1)
- }
+ NEXT(2, 1)
+ }
- return 0;
+ return 0;
}
diff --git a/Modules/cjkcodecs/alg_jisx0201.h b/Modules/cjkcodecs/alg_jisx0201.h
index 1fca06b..0bc7db5 100644
--- a/Modules/cjkcodecs/alg_jisx0201.h
+++ b/Modules/cjkcodecs/alg_jisx0201.h
@@ -1,24 +1,24 @@
-#define JISX0201_R_ENCODE(c, assi) \
- if ((c) < 0x80 && (c) != 0x5c && (c) != 0x7e) \
- (assi) = (c); \
- else if ((c) == 0x00a5) (assi) = 0x5c; \
- else if ((c) == 0x203e) (assi) = 0x7e;
-#define JISX0201_K_ENCODE(c, assi) \
- if ((c) >= 0xff61 && (c) <= 0xff9f) \
- (assi) = (c) - 0xfec0;
-#define JISX0201_ENCODE(c, assi) \
- JISX0201_R_ENCODE(c, assi) \
- else JISX0201_K_ENCODE(c, assi)
+#define JISX0201_R_ENCODE(c, assi) \
+ if ((c) < 0x80 && (c) != 0x5c && (c) != 0x7e) \
+ (assi) = (c); \
+ else if ((c) == 0x00a5) (assi) = 0x5c; \
+ else if ((c) == 0x203e) (assi) = 0x7e;
+#define JISX0201_K_ENCODE(c, assi) \
+ if ((c) >= 0xff61 && (c) <= 0xff9f) \
+ (assi) = (c) - 0xfec0;
+#define JISX0201_ENCODE(c, assi) \
+ JISX0201_R_ENCODE(c, assi) \
+ else JISX0201_K_ENCODE(c, assi)
-#define JISX0201_R_DECODE(c, assi) \
- if ((c) < 0x5c) (assi) = (c); \
- else if ((c) == 0x5c) (assi) = 0x00a5; \
- else if ((c) < 0x7e) (assi) = (c); \
- else if ((c) == 0x7e) (assi) = 0x203e; \
- else if ((c) == 0x7f) (assi) = 0x7f;
-#define JISX0201_K_DECODE(c, assi) \
- if ((c) >= 0xa1 && (c) <= 0xdf) \
- (assi) = 0xfec0 + (c);
-#define JISX0201_DECODE(c, assi) \
- JISX0201_R_DECODE(c, assi) \
- else JISX0201_K_DECODE(c, assi)
+#define JISX0201_R_DECODE(c, assi) \
+ if ((c) < 0x5c) (assi) = (c); \
+ else if ((c) == 0x5c) (assi) = 0x00a5; \
+ else if ((c) < 0x7e) (assi) = (c); \
+ else if ((c) == 0x7e) (assi) = 0x203e; \
+ else if ((c) == 0x7f) (assi) = 0x7f;
+#define JISX0201_K_DECODE(c, assi) \
+ if ((c) >= 0xa1 && (c) <= 0xdf) \
+ (assi) = 0xfec0 + (c);
+#define JISX0201_DECODE(c, assi) \
+ JISX0201_R_DECODE(c, assi) \
+ else JISX0201_K_DECODE(c, assi)
diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h
index e630671..ab0682a 100644
--- a/Modules/cjkcodecs/cjkcodecs.h
+++ b/Modules/cjkcodecs/cjkcodecs.h
@@ -13,12 +13,12 @@
/* a unicode "undefined" codepoint */
-#define UNIINV 0xFFFE
+#define UNIINV 0xFFFE
/* internal-use DBCS codepoints which aren't used by any charsets */
-#define NOCHAR 0xFFFF
-#define MULTIC 0xFFFE
-#define DBCINV 0xFFFD
+#define NOCHAR 0xFFFF
+#define MULTIC 0xFFFE
+#define DBCINV 0xFFFD
/* shorter macros to save source size of mapping tables */
#define U UNIINV
@@ -27,94 +27,94 @@
#define D DBCINV
struct dbcs_index {
- const ucs2_t *map;
- unsigned char bottom, top;
+ const ucs2_t *map;
+ unsigned char bottom, top;
};
typedef struct dbcs_index decode_map;
struct widedbcs_index {
- const ucs4_t *map;
- unsigned char bottom, top;
+ const ucs4_t *map;
+ unsigned char bottom, top;
};
typedef struct widedbcs_index widedecode_map;
struct unim_index {
- const DBCHAR *map;
- unsigned char bottom, top;
+ const DBCHAR *map;
+ unsigned char bottom, top;
};
typedef struct unim_index encode_map;
struct unim_index_bytebased {
- const unsigned char *map;
- unsigned char bottom, top;
+ const unsigned char *map;
+ unsigned char bottom, top;
};
struct dbcs_map {
- const char *charset;
- const struct unim_index *encmap;
- const struct dbcs_index *decmap;
+ const char *charset;
+ const struct unim_index *encmap;
+ const struct dbcs_index *decmap;
};
struct pair_encodemap {
- ucs4_t uniseq;
- DBCHAR code;
+ ucs4_t uniseq;
+ DBCHAR code;
};
static const MultibyteCodec *codec_list;
static const struct dbcs_map *mapping_list;
-#define CODEC_INIT(encoding) \
- static int encoding##_codec_init(const void *config)
-
-#define ENCODER_INIT(encoding) \
- static int encoding##_encode_init( \
- MultibyteCodec_State *state, const void *config)
-#define ENCODER(encoding) \
- static Py_ssize_t encoding##_encode( \
- MultibyteCodec_State *state, const void *config, \
- const Py_UNICODE **inbuf, Py_ssize_t inleft, \
- unsigned char **outbuf, Py_ssize_t outleft, int flags)
-#define ENCODER_RESET(encoding) \
- static Py_ssize_t encoding##_encode_reset( \
- MultibyteCodec_State *state, const void *config, \
- unsigned char **outbuf, Py_ssize_t outleft)
-
-#define DECODER_INIT(encoding) \
- static int encoding##_decode_init( \
- MultibyteCodec_State *state, const void *config)
-#define DECODER(encoding) \
- static Py_ssize_t encoding##_decode( \
- MultibyteCodec_State *state, const void *config, \
- const unsigned char **inbuf, Py_ssize_t inleft, \
- Py_UNICODE **outbuf, Py_ssize_t outleft)
-#define DECODER_RESET(encoding) \
- static Py_ssize_t encoding##_decode_reset( \
- MultibyteCodec_State *state, const void *config)
+#define CODEC_INIT(encoding) \
+ static int encoding##_codec_init(const void *config)
+
+#define ENCODER_INIT(encoding) \
+ static int encoding##_encode_init( \
+ MultibyteCodec_State *state, const void *config)
+#define ENCODER(encoding) \
+ static Py_ssize_t encoding##_encode( \
+ MultibyteCodec_State *state, const void *config, \
+ const Py_UNICODE **inbuf, Py_ssize_t inleft, \
+ unsigned char **outbuf, Py_ssize_t outleft, int flags)
+#define ENCODER_RESET(encoding) \
+ static Py_ssize_t encoding##_encode_reset( \
+ MultibyteCodec_State *state, const void *config, \
+ unsigned char **outbuf, Py_ssize_t outleft)
+
+#define DECODER_INIT(encoding) \
+ static int encoding##_decode_init( \
+ MultibyteCodec_State *state, const void *config)
+#define DECODER(encoding) \
+ static Py_ssize_t encoding##_decode( \
+ MultibyteCodec_State *state, const void *config, \
+ const unsigned char **inbuf, Py_ssize_t inleft, \
+ Py_UNICODE **outbuf, Py_ssize_t outleft)
+#define DECODER_RESET(encoding) \
+ static Py_ssize_t encoding##_decode_reset( \
+ MultibyteCodec_State *state, const void *config)
#if Py_UNICODE_SIZE == 4
-#define UCS4INVALID(code) \
- if ((code) > 0xFFFF) \
- return 1;
+#define UCS4INVALID(code) \
+ if ((code) > 0xFFFF) \
+ return 1;
#else
-#define UCS4INVALID(code) \
- if (0) ;
+#define UCS4INVALID(code) \
+ if (0) ;
#endif
-#define NEXT_IN(i) \
- (*inbuf) += (i); \
- (inleft) -= (i);
-#define NEXT_OUT(o) \
- (*outbuf) += (o); \
- (outleft) -= (o);
-#define NEXT(i, o) \
- NEXT_IN(i) NEXT_OUT(o)
-
-#define REQUIRE_INBUF(n) \
- if (inleft < (n)) \
- return MBERR_TOOFEW;
-#define REQUIRE_OUTBUF(n) \
- if (outleft < (n)) \
- return MBERR_TOOSMALL;
+#define NEXT_IN(i) \
+ (*inbuf) += (i); \
+ (inleft) -= (i);
+#define NEXT_OUT(o) \
+ (*outbuf) += (o); \
+ (outleft) -= (o);
+#define NEXT(i, o) \
+ NEXT_IN(i) NEXT_OUT(o)
+
+#define REQUIRE_INBUF(n) \
+ if (inleft < (n)) \
+ return MBERR_TOOFEW;
+#define REQUIRE_OUTBUF(n) \
+ if (outleft < (n)) \
+ return MBERR_TOOSMALL;
#define IN1 ((*inbuf)[0])
#define IN2 ((*inbuf)[1])
@@ -126,289 +126,289 @@ static const struct dbcs_map *mapping_list;
#define OUT3(c) ((*outbuf)[2]) = (c);
#define OUT4(c) ((*outbuf)[3]) = (c);
-#define WRITE1(c1) \
- REQUIRE_OUTBUF(1) \
- (*outbuf)[0] = (c1);
-#define WRITE2(c1, c2) \
- REQUIRE_OUTBUF(2) \
- (*outbuf)[0] = (c1); \
- (*outbuf)[1] = (c2);
-#define WRITE3(c1, c2, c3) \
- REQUIRE_OUTBUF(3) \
- (*outbuf)[0] = (c1); \
- (*outbuf)[1] = (c2); \
- (*outbuf)[2] = (c3);
-#define WRITE4(c1, c2, c3, c4) \
- REQUIRE_OUTBUF(4) \
- (*outbuf)[0] = (c1); \
- (*outbuf)[1] = (c2); \
- (*outbuf)[2] = (c3); \
- (*outbuf)[3] = (c4);
+#define WRITE1(c1) \
+ REQUIRE_OUTBUF(1) \
+ (*outbuf)[0] = (c1);
+#define WRITE2(c1, c2) \
+ REQUIRE_OUTBUF(2) \
+ (*outbuf)[0] = (c1); \
+ (*outbuf)[1] = (c2);
+#define WRITE3(c1, c2, c3) \
+ REQUIRE_OUTBUF(3) \
+ (*outbuf)[0] = (c1); \
+ (*outbuf)[1] = (c2); \
+ (*outbuf)[2] = (c3);
+#define WRITE4(c1, c2, c3, c4) \
+ REQUIRE_OUTBUF(4) \
+ (*outbuf)[0] = (c1); \
+ (*outbuf)[1] = (c2); \
+ (*outbuf)[2] = (c3); \
+ (*outbuf)[3] = (c4);
#if Py_UNICODE_SIZE == 2
-# define WRITEUCS4(c) \
- REQUIRE_OUTBUF(2) \
- (*outbuf)[0] = 0xd800 + (((c) - 0x10000) >> 10); \
- (*outbuf)[1] = 0xdc00 + (((c) - 0x10000) & 0x3ff); \
- NEXT_OUT(2)
+# define WRITEUCS4(c) \
+ REQUIRE_OUTBUF(2) \
+ (*outbuf)[0] = 0xd800 + (((c) - 0x10000) >> 10); \
+ (*outbuf)[1] = 0xdc00 + (((c) - 0x10000) & 0x3ff); \
+ NEXT_OUT(2)
#else
-# define WRITEUCS4(c) \
- REQUIRE_OUTBUF(1) \
- **outbuf = (Py_UNICODE)(c); \
- NEXT_OUT(1)
+# define WRITEUCS4(c) \
+ REQUIRE_OUTBUF(1) \
+ **outbuf = (Py_UNICODE)(c); \
+ NEXT_OUT(1)
#endif
-#define _TRYMAP_ENC(m, assi, val) \
- ((m)->map != NULL && (val) >= (m)->bottom && \
- (val)<= (m)->top && ((assi) = (m)->map[(val) - \
- (m)->bottom]) != NOCHAR)
-#define TRYMAP_ENC_COND(charset, assi, uni) \
- _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
-#define TRYMAP_ENC(charset, assi, uni) \
- if TRYMAP_ENC_COND(charset, assi, uni)
-
-#define _TRYMAP_DEC(m, assi, val) \
- ((m)->map != NULL && (val) >= (m)->bottom && \
- (val)<= (m)->top && ((assi) = (m)->map[(val) - \
- (m)->bottom]) != UNIINV)
-#define TRYMAP_DEC(charset, assi, c1, c2) \
- if _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
-
-#define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \
- ((m)->map != NULL && (val) >= (m)->bottom && \
- (val)<= (m)->top && \
- ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \
- (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \
- (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1))
-#define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \
- if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \
- assplane, asshi, asslo, (uni) & 0xff)
-#define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \
- if _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2)
+#define _TRYMAP_ENC(m, assi, val) \
+ ((m)->map != NULL && (val) >= (m)->bottom && \
+ (val)<= (m)->top && ((assi) = (m)->map[(val) - \
+ (m)->bottom]) != NOCHAR)
+#define TRYMAP_ENC_COND(charset, assi, uni) \
+ _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
+#define TRYMAP_ENC(charset, assi, uni) \
+ if TRYMAP_ENC_COND(charset, assi, uni)
+
+#define _TRYMAP_DEC(m, assi, val) \
+ ((m)->map != NULL && (val) >= (m)->bottom && \
+ (val)<= (m)->top && ((assi) = (m)->map[(val) - \
+ (m)->bottom]) != UNIINV)
+#define TRYMAP_DEC(charset, assi, c1, c2) \
+ if _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
+
+#define _TRYMAP_ENC_MPLANE(m, assplane, asshi, asslo, val) \
+ ((m)->map != NULL && (val) >= (m)->bottom && \
+ (val)<= (m)->top && \
+ ((assplane) = (m)->map[((val) - (m)->bottom)*3]) != 0 && \
+ (((asshi) = (m)->map[((val) - (m)->bottom)*3 + 1]), 1) && \
+ (((asslo) = (m)->map[((val) - (m)->bottom)*3 + 2]), 1))
+#define TRYMAP_ENC_MPLANE(charset, assplane, asshi, asslo, uni) \
+ if _TRYMAP_ENC_MPLANE(&charset##_encmap[(uni) >> 8], \
+ assplane, asshi, asslo, (uni) & 0xff)
+#define TRYMAP_DEC_MPLANE(charset, assi, plane, c1, c2) \
+ if _TRYMAP_DEC(&charset##_decmap[plane][c1], assi, c2)
#if Py_UNICODE_SIZE == 2
-#define DECODE_SURROGATE(c) \
- if (c >> 10 == 0xd800 >> 10) { /* high surrogate */ \
- REQUIRE_INBUF(2) \
- if (IN2 >> 10 == 0xdc00 >> 10) { /* low surrogate */ \
- c = 0x10000 + ((ucs4_t)(c - 0xd800) << 10) + \
- ((ucs4_t)(IN2) - 0xdc00); \
- } \
- }
-#define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1)
+#define DECODE_SURROGATE(c) \
+ if (c >> 10 == 0xd800 >> 10) { /* high surrogate */ \
+ REQUIRE_INBUF(2) \
+ if (IN2 >> 10 == 0xdc00 >> 10) { /* low surrogate */ \
+ c = 0x10000 + ((ucs4_t)(c - 0xd800) << 10) + \
+ ((ucs4_t)(IN2) - 0xdc00); \
+ } \
+ }
+#define GET_INSIZE(c) ((c) > 0xffff ? 2 : 1)
#else
#define DECODE_SURROGATE(c) {;}
-#define GET_INSIZE(c) 1
+#define GET_INSIZE(c) 1
#endif
#define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
#define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
#define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
#define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
-#define END_MAPPINGS_LIST \
- {"", NULL, NULL} }; \
- static const struct dbcs_map *mapping_list = \
- (const struct dbcs_map *)_mapping_list;
+#define END_MAPPINGS_LIST \
+ {"", NULL, NULL} }; \
+ static const struct dbcs_map *mapping_list = \
+ (const struct dbcs_map *)_mapping_list;
#define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
-#define _STATEFUL_METHODS(enc) \
- enc##_encode, \
- enc##_encode_init, \
- enc##_encode_reset, \
- enc##_decode, \
- enc##_decode_init, \
- enc##_decode_reset,
-#define _STATELESS_METHODS(enc) \
- enc##_encode, NULL, NULL, \
- enc##_decode, NULL, NULL,
-#define CODEC_STATEFUL(enc) { \
- #enc, NULL, NULL, \
- _STATEFUL_METHODS(enc) \
+#define _STATEFUL_METHODS(enc) \
+ enc##_encode, \
+ enc##_encode_init, \
+ enc##_encode_reset, \
+ enc##_decode, \
+ enc##_decode_init, \
+ enc##_decode_reset,
+#define _STATELESS_METHODS(enc) \
+ enc##_encode, NULL, NULL, \
+ enc##_decode, NULL, NULL,
+#define CODEC_STATEFUL(enc) { \
+ #enc, NULL, NULL, \
+ _STATEFUL_METHODS(enc) \
},
-#define CODEC_STATELESS(enc) { \
- #enc, NULL, NULL, \
- _STATELESS_METHODS(enc) \
+#define CODEC_STATELESS(enc) { \
+ #enc, NULL, NULL, \
+ _STATELESS_METHODS(enc) \
},
-#define CODEC_STATELESS_WINIT(enc) { \
- #enc, NULL, \
- enc##_codec_init, \
- _STATELESS_METHODS(enc) \
+#define CODEC_STATELESS_WINIT(enc) { \
+ #enc, NULL, \
+ enc##_codec_init, \
+ _STATELESS_METHODS(enc) \
},
-#define END_CODECS_LIST \
- {"", NULL,} }; \
- static const MultibyteCodec *codec_list = \
- (const MultibyteCodec *)_codec_list;
+#define END_CODECS_LIST \
+ {"", NULL,} }; \
+ static const MultibyteCodec *codec_list = \
+ (const MultibyteCodec *)_codec_list;
static PyObject *
getmultibytecodec(void)
{
- static PyObject *cofunc = NULL;
-
- if (cofunc == NULL) {
- PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
- if (mod == NULL)
- return NULL;
- cofunc = PyObject_GetAttrString(mod, "__create_codec");
- Py_DECREF(mod);
- }
- return cofunc;
+ static PyObject *cofunc = NULL;
+
+ if (cofunc == NULL) {
+ PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
+ if (mod == NULL)
+ return NULL;
+ cofunc = PyObject_GetAttrString(mod, "__create_codec");
+ Py_DECREF(mod);
+ }
+ return cofunc;
}
static PyObject *
getcodec(PyObject *self, PyObject *encoding)
{
- PyObject *codecobj, *r, *cofunc;
- const MultibyteCodec *codec;
- const char *enc;
-
- if (!PyUnicode_Check(encoding)) {
- PyErr_SetString(PyExc_TypeError,
- "encoding name must be a string.");
- return NULL;
- }
- enc = _PyUnicode_AsString(encoding);
- if (enc == NULL)
- return NULL;
-
- cofunc = getmultibytecodec();
- if (cofunc == NULL)
- return NULL;
-
- for (codec = codec_list; codec->encoding[0]; codec++)
- if (strcmp(codec->encoding, enc) == 0)
- break;
-
- if (codec->encoding[0] == '\0') {
- PyErr_SetString(PyExc_LookupError,
- "no such codec is supported.");
- return NULL;
- }
-
- codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL);
- if (codecobj == NULL)
- return NULL;
-
- r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
- Py_DECREF(codecobj);
-
- return r;
+ PyObject *codecobj, *r, *cofunc;
+ const MultibyteCodec *codec;
+ const char *enc;
+
+ if (!PyUnicode_Check(encoding)) {
+ PyErr_SetString(PyExc_TypeError,
+ "encoding name must be a string.");
+ return NULL;
+ }
+ enc = _PyUnicode_AsString(encoding);
+ if (enc == NULL)
+ return NULL;
+
+ cofunc = getmultibytecodec();
+ if (cofunc == NULL)
+ return NULL;
+
+ for (codec = codec_list; codec->encoding[0]; codec++)
+ if (strcmp(codec->encoding, enc) == 0)
+ break;
+
+ if (codec->encoding[0] == '\0') {
+ PyErr_SetString(PyExc_LookupError,
+ "no such codec is supported.");
+ return NULL;
+ }
+
+ codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL);
+ if (codecobj == NULL)
+ return NULL;
+
+ r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
+ Py_DECREF(codecobj);
+
+ return r;
}
static struct PyMethodDef __methods[] = {
- {"getcodec", (PyCFunction)getcodec, METH_O, ""},
- {NULL, NULL},
+ {"getcodec", (PyCFunction)getcodec, METH_O, ""},
+ {NULL, NULL},
};
static int
register_maps(PyObject *module)
{
- const struct dbcs_map *h;
-
- for (h = mapping_list; h->charset[0] != '\0'; h++) {
- char mhname[256] = "__map_";
- int r;
- strcpy(mhname + sizeof("__map_") - 1, h->charset);
- r = PyModule_AddObject(module, mhname,
- PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL));
- if (r == -1)
- return -1;
- }
- return 0;
+ const struct dbcs_map *h;
+
+ for (h = mapping_list; h->charset[0] != '\0'; h++) {
+ char mhname[256] = "__map_";
+ int r;
+ strcpy(mhname + sizeof("__map_") - 1, h->charset);
+ r = PyModule_AddObject(module, mhname,
+ PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL));
+ if (r == -1)
+ return -1;
+ }
+ return 0;
}
#ifdef USING_BINARY_PAIR_SEARCH
static DBCHAR
find_pairencmap(ucs2_t body, ucs2_t modifier,
- const struct pair_encodemap *haystack, int haystacksize)
+ const struct pair_encodemap *haystack, int haystacksize)
{
- int pos, min, max;
- ucs4_t value = body << 16 | modifier;
-
- min = 0;
- max = haystacksize;
-
- for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1)
- if (value < haystack[pos].uniseq) {
- if (max == pos) break;
- else max = pos;
- }
- else if (value > haystack[pos].uniseq) {
- if (min == pos) break;
- else min = pos;
- }
- else
- break;
-
- if (value == haystack[pos].uniseq)
- return haystack[pos].code;
- else
- return DBCINV;
+ int pos, min, max;
+ ucs4_t value = body << 16 | modifier;
+
+ min = 0;
+ max = haystacksize;
+
+ for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1)
+ if (value < haystack[pos].uniseq) {
+ if (max == pos) break;
+ else max = pos;
+ }
+ else if (value > haystack[pos].uniseq) {
+ if (min == pos) break;
+ else min = pos;
+ }
+ else
+ break;
+
+ if (value == haystack[pos].uniseq)
+ return haystack[pos].code;
+ else
+ return DBCINV;
}
#endif
#ifdef USING_IMPORTED_MAPS
#define IMPORT_MAP(locale, charset, encmap, decmap) \
- importmap("_codecs_" #locale, "__map_" #charset, \
- (const void**)encmap, (const void**)decmap)
+ importmap("_codecs_" #locale, "__map_" #charset, \
+ (const void**)encmap, (const void**)decmap)
static int
importmap(const char *modname, const char *symbol,
- const void **encmap, const void **decmap)
+ const void **encmap, const void **decmap)
{
- PyObject *o, *mod;
-
- mod = PyImport_ImportModule((char *)modname);
- if (mod == NULL)
- return -1;
-
- o = PyObject_GetAttrString(mod, (char*)symbol);
- if (o == NULL)
- goto errorexit;
- else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
- PyErr_SetString(PyExc_ValueError,
- "map data must be a Capsule.");
- goto errorexit;
- }
- else {
- struct dbcs_map *map;
- map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME);
- if (encmap != NULL)
- *encmap = map->encmap;
- if (decmap != NULL)
- *decmap = map->decmap;
- Py_DECREF(o);
- }
-
- Py_DECREF(mod);
- return 0;
+ PyObject *o, *mod;
+
+ mod = PyImport_ImportModule((char *)modname);
+ if (mod == NULL)
+ return -1;
+
+ o = PyObject_GetAttrString(mod, (char*)symbol);
+ if (o == NULL)
+ goto errorexit;
+ else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
+ PyErr_SetString(PyExc_ValueError,
+ "map data must be a Capsule.");
+ goto errorexit;
+ }
+ else {
+ struct dbcs_map *map;
+ map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME);
+ if (encmap != NULL)
+ *encmap = map->encmap;
+ if (decmap != NULL)
+ *decmap = map->decmap;
+ Py_DECREF(o);
+ }
+
+ Py_DECREF(mod);
+ return 0;
errorexit:
- Py_DECREF(mod);
- return -1;
+ Py_DECREF(mod);
+ return -1;
}
#endif
-#define I_AM_A_MODULE_FOR(loc) \
- static struct PyModuleDef __module = { \
- PyModuleDef_HEAD_INIT, \
- "_codecs_"#loc, \
- NULL, \
- 0, \
- __methods, \
- NULL, \
- NULL, \
- NULL, \
- NULL \
- }; \
- PyObject* \
- PyInit__codecs_##loc(void) \
- { \
- PyObject *m = PyModule_Create(&__module); \
- if (m != NULL) \
- (void)register_maps(m); \
- return m; \
- }
+#define I_AM_A_MODULE_FOR(loc) \
+ static struct PyModuleDef __module = { \
+ PyModuleDef_HEAD_INIT, \
+ "_codecs_"#loc, \
+ NULL, \
+ 0, \
+ __methods, \
+ NULL, \
+ NULL, \
+ NULL, \
+ NULL \
+ }; \
+ PyObject* \
+ PyInit__codecs_##loc(void) \
+ { \
+ PyObject *m = PyModule_Create(&__module); \
+ if (m != NULL) \
+ (void)register_maps(m); \
+ return m; \
+ }
#endif
diff --git a/Modules/cjkcodecs/emu_jisx0213_2000.h b/Modules/cjkcodecs/emu_jisx0213_2000.h
index 250c673..4227fb2 100644
--- a/Modules/cjkcodecs/emu_jisx0213_2000.h
+++ b/Modules/cjkcodecs/emu_jisx0213_2000.h
@@ -5,39 +5,39 @@
#define EMULATE_JISX0213_2000_ENCODE_INVALID 1
#endif
-#define EMULATE_JISX0213_2000_ENCODE_BMP(assi, c) \
- if (config == (void *)2000 && ( \
- (c) == 0x9B1C || (c) == 0x4FF1 || \
- (c) == 0x525D || (c) == 0x541E || \
- (c) == 0x5653 || (c) == 0x59F8 || \
- (c) == 0x5C5B || (c) == 0x5E77 || \
- (c) == 0x7626 || (c) == 0x7E6B)) \
- return EMULATE_JISX0213_2000_ENCODE_INVALID; \
- else if (config == (void *)2000 && (c) == 0x9B1D) \
- (assi) = 0x8000 | 0x7d3b; \
+#define EMULATE_JISX0213_2000_ENCODE_BMP(assi, c) \
+ if (config == (void *)2000 && ( \
+ (c) == 0x9B1C || (c) == 0x4FF1 || \
+ (c) == 0x525D || (c) == 0x541E || \
+ (c) == 0x5653 || (c) == 0x59F8 || \
+ (c) == 0x5C5B || (c) == 0x5E77 || \
+ (c) == 0x7626 || (c) == 0x7E6B)) \
+ return EMULATE_JISX0213_2000_ENCODE_INVALID; \
+ else if (config == (void *)2000 && (c) == 0x9B1D) \
+ (assi) = 0x8000 | 0x7d3b; \
-#define EMULATE_JISX0213_2000_ENCODE_EMP(assi, c) \
- if (config == (void *)2000 && (c) == 0x20B9F) \
- return EMULATE_JISX0213_2000_ENCODE_INVALID;
+#define EMULATE_JISX0213_2000_ENCODE_EMP(assi, c) \
+ if (config == (void *)2000 && (c) == 0x20B9F) \
+ return EMULATE_JISX0213_2000_ENCODE_INVALID;
#ifndef EMULATE_JISX0213_2000_DECODE_INVALID
#define EMULATE_JISX0213_2000_DECODE_INVALID 2
#endif
-#define EMULATE_JISX0213_2000_DECODE_PLANE1(assi, c1, c2) \
- if (config == (void *)2000 && \
- (((c1) == 0x2E && (c2) == 0x21) || \
- ((c1) == 0x2F && (c2) == 0x7E) || \
- ((c1) == 0x4F && (c2) == 0x54) || \
- ((c1) == 0x4F && (c2) == 0x7E) || \
- ((c1) == 0x74 && (c2) == 0x27) || \
- ((c1) == 0x7E && (c2) == 0x7A) || \
- ((c1) == 0x7E && (c2) == 0x7B) || \
- ((c1) == 0x7E && (c2) == 0x7C) || \
- ((c1) == 0x7E && (c2) == 0x7D) || \
- ((c1) == 0x7E && (c2) == 0x7E))) \
- return EMULATE_JISX0213_2000_DECODE_INVALID;
+#define EMULATE_JISX0213_2000_DECODE_PLANE1(assi, c1, c2) \
+ if (config == (void *)2000 && \
+ (((c1) == 0x2E && (c2) == 0x21) || \
+ ((c1) == 0x2F && (c2) == 0x7E) || \
+ ((c1) == 0x4F && (c2) == 0x54) || \
+ ((c1) == 0x4F && (c2) == 0x7E) || \
+ ((c1) == 0x74 && (c2) == 0x27) || \
+ ((c1) == 0x7E && (c2) == 0x7A) || \
+ ((c1) == 0x7E && (c2) == 0x7B) || \
+ ((c1) == 0x7E && (c2) == 0x7C) || \
+ ((c1) == 0x7E && (c2) == 0x7D) || \
+ ((c1) == 0x7E && (c2) == 0x7E))) \
+ return EMULATE_JISX0213_2000_DECODE_INVALID;
-#define EMULATE_JISX0213_2000_DECODE_PLANE2(assi, c1, c2) \
- if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) \
- (assi) = 0x9B1D;
+#define EMULATE_JISX0213_2000_DECODE_PLANE2(assi, c1, c2) \
+ if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) \
+ (assi) = 0x9B1D;
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 1735dfd..af7ea5b 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -45,179 +45,179 @@ static char *incrementalkwarglist[] = {"input", "final", NULL};
static char *streamkwarglist[] = {"stream", "errors", NULL};
static PyObject *multibytecodec_encode(MultibyteCodec *,
- MultibyteCodec_State *, const Py_UNICODE **, Py_ssize_t,
- PyObject *, int);
+ MultibyteCodec_State *, const Py_UNICODE **, Py_ssize_t,
+ PyObject *, int);
-#define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */
+#define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */
static PyObject *
make_tuple(PyObject *object, Py_ssize_t len)
{
- PyObject *v, *w;
-
- if (object == NULL)
- return NULL;
-
- v = PyTuple_New(2);
- if (v == NULL) {
- Py_DECREF(object);
- return NULL;
- }
- PyTuple_SET_ITEM(v, 0, object);
-
- w = PyLong_FromSsize_t(len);
- if (w == NULL) {
- Py_DECREF(v);
- return NULL;
- }
- PyTuple_SET_ITEM(v, 1, w);
-
- return v;
+ PyObject *v, *w;
+
+ if (object == NULL)
+ return NULL;
+
+ v = PyTuple_New(2);
+ if (v == NULL) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(v, 0, object);
+
+ w = PyLong_FromSsize_t(len);
+ if (w == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(v, 1, w);
+
+ return v;
}
static PyObject *
internal_error_callback(const char *errors)
{
- if (errors == NULL || strcmp(errors, "strict") == 0)
- return ERROR_STRICT;
- else if (strcmp(errors, "ignore") == 0)
- return ERROR_IGNORE;
- else if (strcmp(errors, "replace") == 0)
- return ERROR_REPLACE;
- else
- return PyUnicode_FromString(errors);
+ if (errors == NULL || strcmp(errors, "strict") == 0)
+ return ERROR_STRICT;
+ else if (strcmp(errors, "ignore") == 0)
+ return ERROR_IGNORE;
+ else if (strcmp(errors, "replace") == 0)
+ return ERROR_REPLACE;
+ else
+ return PyUnicode_FromString(errors);
}
static PyObject *
call_error_callback(PyObject *errors, PyObject *exc)
{
- PyObject *args, *cb, *r;
- const char *str;
-
- assert(PyUnicode_Check(errors));
- str = _PyUnicode_AsString(errors);
- if (str == NULL)
- return NULL;
- cb = PyCodec_LookupError(str);
- if (cb == NULL)
- return NULL;
-
- args = PyTuple_New(1);
- if (args == NULL) {
- Py_DECREF(cb);
- return NULL;
- }
-
- PyTuple_SET_ITEM(args, 0, exc);
- Py_INCREF(exc);
-
- r = PyObject_CallObject(cb, args);
- Py_DECREF(args);
- Py_DECREF(cb);
- return r;
+ PyObject *args, *cb, *r;
+ const char *str;
+
+ assert(PyUnicode_Check(errors));
+ str = _PyUnicode_AsString(errors);
+ if (str == NULL)
+ return NULL;
+ cb = PyCodec_LookupError(str);
+ if (cb == NULL)
+ return NULL;
+
+ args = PyTuple_New(1);
+ if (args == NULL) {
+ Py_DECREF(cb);
+ return NULL;
+ }
+
+ PyTuple_SET_ITEM(args, 0, exc);
+ Py_INCREF(exc);
+
+ r = PyObject_CallObject(cb, args);
+ Py_DECREF(args);
+ Py_DECREF(cb);
+ return r;
}
static PyObject *
codecctx_errors_get(MultibyteStatefulCodecContext *self)
{
- const char *errors;
-
- if (self->errors == ERROR_STRICT)
- errors = "strict";
- else if (self->errors == ERROR_IGNORE)
- errors = "ignore";
- else if (self->errors == ERROR_REPLACE)
- errors = "replace";
- else {
- Py_INCREF(self->errors);
- return self->errors;
- }
-
- return PyUnicode_FromString(errors);
+ const char *errors;
+
+ if (self->errors == ERROR_STRICT)
+ errors = "strict";
+ else if (self->errors == ERROR_IGNORE)
+ errors = "ignore";
+ else if (self->errors == ERROR_REPLACE)
+ errors = "replace";
+ else {
+ Py_INCREF(self->errors);
+ return self->errors;
+ }
+
+ return PyUnicode_FromString(errors);
}
static int
codecctx_errors_set(MultibyteStatefulCodecContext *self, PyObject *value,
- void *closure)
+ void *closure)
{
- PyObject *cb;
- const char *str;
+ PyObject *cb;
+ const char *str;
- if (!PyUnicode_Check(value)) {
- PyErr_SetString(PyExc_TypeError, "errors must be a string");
- return -1;
- }
+ if (!PyUnicode_Check(value)) {
+ PyErr_SetString(PyExc_TypeError, "errors must be a string");
+ return -1;
+ }
- str = _PyUnicode_AsString(value);
- if (str == NULL)
- return -1;
+ str = _PyUnicode_AsString(value);
+ if (str == NULL)
+ return -1;
- cb = internal_error_callback(str);
- if (cb == NULL)
- return -1;
+ cb = internal_error_callback(str);
+ if (cb == NULL)
+ return -1;
- ERROR_DECREF(self->errors);
- self->errors = cb;
- return 0;
+ ERROR_DECREF(self->errors);
+ self->errors = cb;
+ return 0;
}
/* This getset handlers list is used by all the stateful codec objects */
static PyGetSetDef codecctx_getsets[] = {
- {"errors", (getter)codecctx_errors_get,
- (setter)codecctx_errors_set,
- PyDoc_STR("how to treat errors")},
- {NULL,}
+ {"errors", (getter)codecctx_errors_get,
+ (setter)codecctx_errors_set,
+ PyDoc_STR("how to treat errors")},
+ {NULL,}
};
static int
expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
{
- Py_ssize_t orgpos, orgsize, incsize;
+ Py_ssize_t orgpos, orgsize, incsize;
- orgpos = (Py_ssize_t)((char *)buf->outbuf -
- PyBytes_AS_STRING(buf->outobj));
- orgsize = PyBytes_GET_SIZE(buf->outobj);
- incsize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize);
+ orgpos = (Py_ssize_t)((char *)buf->outbuf -
+ PyBytes_AS_STRING(buf->outobj));
+ orgsize = PyBytes_GET_SIZE(buf->outobj);
+ incsize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize);
- if (orgsize > PY_SSIZE_T_MAX - incsize)
- return -1;
+ if (orgsize > PY_SSIZE_T_MAX - incsize)
+ return -1;
- if (_PyBytes_Resize(&buf->outobj, orgsize + incsize) == -1)
- return -1;
+ if (_PyBytes_Resize(&buf->outobj, orgsize + incsize) == -1)
+ return -1;
- buf->outbuf = (unsigned char *)PyBytes_AS_STRING(buf->outobj) +orgpos;
- buf->outbuf_end = (unsigned char *)PyBytes_AS_STRING(buf->outobj)
- + PyBytes_GET_SIZE(buf->outobj);
+ buf->outbuf = (unsigned char *)PyBytes_AS_STRING(buf->outobj) +orgpos;
+ buf->outbuf_end = (unsigned char *)PyBytes_AS_STRING(buf->outobj)
+ + PyBytes_GET_SIZE(buf->outobj);
- return 0;
+ return 0;
}
-#define REQUIRE_ENCODEBUFFER(buf, s) { \
- if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \
- if (expand_encodebuffer(buf, s) == -1) \
- goto errorexit; \
+#define REQUIRE_ENCODEBUFFER(buf, s) { \
+ if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \
+ if (expand_encodebuffer(buf, s) == -1) \
+ goto errorexit; \
}
static int
expand_decodebuffer(MultibyteDecodeBuffer *buf, Py_ssize_t esize)
{
- Py_ssize_t orgpos, orgsize;
+ Py_ssize_t orgpos, orgsize;
- orgpos = (Py_ssize_t)(buf->outbuf - PyUnicode_AS_UNICODE(buf->outobj));
- orgsize = PyUnicode_GET_SIZE(buf->outobj);
- if (PyUnicode_Resize(&buf->outobj, orgsize + (
- esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1)
- return -1;
+ orgpos = (Py_ssize_t)(buf->outbuf - PyUnicode_AS_UNICODE(buf->outobj));
+ orgsize = PyUnicode_GET_SIZE(buf->outobj);
+ if (PyUnicode_Resize(&buf->outobj, orgsize + (
+ esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1)
+ return -1;
- buf->outbuf = PyUnicode_AS_UNICODE(buf->outobj) + orgpos;
- buf->outbuf_end = PyUnicode_AS_UNICODE(buf->outobj)
- + PyUnicode_GET_SIZE(buf->outobj);
+ buf->outbuf = PyUnicode_AS_UNICODE(buf->outobj) + orgpos;
+ buf->outbuf_end = PyUnicode_AS_UNICODE(buf->outobj)
+ + PyUnicode_GET_SIZE(buf->outobj);
- return 0;
+ return 0;
}
-#define REQUIRE_DECODEBUFFER(buf, s) { \
- if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \
- if (expand_decodebuffer(buf, s) == -1) \
- goto errorexit; \
+#define REQUIRE_DECODEBUFFER(buf, s) { \
+ if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \
+ if (expand_decodebuffer(buf, s) == -1) \
+ goto errorexit; \
}
@@ -227,504 +227,504 @@ expand_decodebuffer(MultibyteDecodeBuffer *buf, Py_ssize_t esize)
static int
multibytecodec_encerror(MultibyteCodec *codec,
- MultibyteCodec_State *state,
- MultibyteEncodeBuffer *buf,
- PyObject *errors, Py_ssize_t e)
+ MultibyteCodec_State *state,
+ MultibyteEncodeBuffer *buf,
+ PyObject *errors, Py_ssize_t e)
{
- PyObject *retobj = NULL, *retstr = NULL, *tobj;
- Py_ssize_t retstrsize, newpos;
- Py_ssize_t esize, start, end;
- const char *reason;
-
- if (e > 0) {
- reason = "illegal multibyte sequence";
- esize = e;
- }
- else {
- switch (e) {
- case MBERR_TOOSMALL:
- REQUIRE_ENCODEBUFFER(buf, -1);
- return 0; /* retry it */
- case MBERR_TOOFEW:
- reason = "incomplete multibyte sequence";
- esize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
- break;
- case MBERR_INTERNAL:
- PyErr_SetString(PyExc_RuntimeError,
- "internal codec error");
- return -1;
- default:
- PyErr_SetString(PyExc_RuntimeError,
- "unknown runtime error");
- return -1;
- }
- }
-
- if (errors == ERROR_REPLACE) {
- const Py_UNICODE replchar = '?', *inbuf = &replchar;
- Py_ssize_t r;
-
- for (;;) {
- Py_ssize_t outleft;
-
- outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
- r = codec->encode(state, codec->config, &inbuf, 1,
- &buf->outbuf, outleft, 0);
- if (r == MBERR_TOOSMALL) {
- REQUIRE_ENCODEBUFFER(buf, -1);
- continue;
- }
- else
- break;
- }
-
- if (r != 0) {
- REQUIRE_ENCODEBUFFER(buf, 1);
- *buf->outbuf++ = '?';
- }
- }
- if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) {
- buf->inbuf += esize;
- return 0;
- }
-
- start = (Py_ssize_t)(buf->inbuf - buf->inbuf_top);
- end = start + esize;
-
- /* use cached exception object if available */
- if (buf->excobj == NULL) {
- buf->excobj = PyUnicodeEncodeError_Create(codec->encoding,
- buf->inbuf_top,
- buf->inbuf_end - buf->inbuf_top,
- start, end, reason);
- if (buf->excobj == NULL)
- goto errorexit;
- }
- else
- if (PyUnicodeEncodeError_SetStart(buf->excobj, start) != 0 ||
- PyUnicodeEncodeError_SetEnd(buf->excobj, end) != 0 ||
- PyUnicodeEncodeError_SetReason(buf->excobj, reason) != 0)
- goto errorexit;
-
- if (errors == ERROR_STRICT) {
- PyCodec_StrictErrors(buf->excobj);
- goto errorexit;
- }
-
- retobj = call_error_callback(errors, buf->excobj);
- if (retobj == NULL)
- goto errorexit;
-
- if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
- !PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) ||
- !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) {
- PyErr_SetString(PyExc_TypeError,
- "encoding error handler must return "
- "(unicode, int) tuple");
- goto errorexit;
- }
-
- {
- const Py_UNICODE *uraw = PyUnicode_AS_UNICODE(tobj);
-
- retstr = multibytecodec_encode(codec, state, &uraw,
- PyUnicode_GET_SIZE(tobj), ERROR_STRICT,
- MBENC_FLUSH);
- if (retstr == NULL)
- goto errorexit;
- }
-
- assert(PyBytes_Check(retstr));
- retstrsize = PyBytes_GET_SIZE(retstr);
- REQUIRE_ENCODEBUFFER(buf, retstrsize);
-
- memcpy(buf->outbuf, PyBytes_AS_STRING(retstr), retstrsize);
- buf->outbuf += retstrsize;
-
- newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1));
- if (newpos < 0 && !PyErr_Occurred())
- newpos += (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top);
- if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) {
- PyErr_Clear();
- PyErr_Format(PyExc_IndexError,
- "position %zd from error handler out of bounds",
- newpos);
- goto errorexit;
- }
- buf->inbuf = buf->inbuf_top + newpos;
-
- Py_DECREF(retobj);
- Py_DECREF(retstr);
- return 0;
+ PyObject *retobj = NULL, *retstr = NULL, *tobj;
+ Py_ssize_t retstrsize, newpos;
+ Py_ssize_t esize, start, end;
+ const char *reason;
+
+ if (e > 0) {
+ reason = "illegal multibyte sequence";
+ esize = e;
+ }
+ else {
+ switch (e) {
+ case MBERR_TOOSMALL:
+ REQUIRE_ENCODEBUFFER(buf, -1);
+ return 0; /* retry it */
+ case MBERR_TOOFEW:
+ reason = "incomplete multibyte sequence";
+ esize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
+ break;
+ case MBERR_INTERNAL:
+ PyErr_SetString(PyExc_RuntimeError,
+ "internal codec error");
+ return -1;
+ default:
+ PyErr_SetString(PyExc_RuntimeError,
+ "unknown runtime error");
+ return -1;
+ }
+ }
+
+ if (errors == ERROR_REPLACE) {
+ const Py_UNICODE replchar = '?', *inbuf = &replchar;
+ Py_ssize_t r;
+
+ for (;;) {
+ Py_ssize_t outleft;
+
+ outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
+ r = codec->encode(state, codec->config, &inbuf, 1,
+ &buf->outbuf, outleft, 0);
+ if (r == MBERR_TOOSMALL) {
+ REQUIRE_ENCODEBUFFER(buf, -1);
+ continue;
+ }
+ else
+ break;
+ }
+
+ if (r != 0) {
+ REQUIRE_ENCODEBUFFER(buf, 1);
+ *buf->outbuf++ = '?';
+ }
+ }
+ if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) {
+ buf->inbuf += esize;
+ return 0;
+ }
+
+ start = (Py_ssize_t)(buf->inbuf - buf->inbuf_top);
+ end = start + esize;
+
+ /* use cached exception object if available */
+ if (buf->excobj == NULL) {
+ buf->excobj = PyUnicodeEncodeError_Create(codec->encoding,
+ buf->inbuf_top,
+ buf->inbuf_end - buf->inbuf_top,
+ start, end, reason);
+ if (buf->excobj == NULL)
+ goto errorexit;
+ }
+ else
+ if (PyUnicodeEncodeError_SetStart(buf->excobj, start) != 0 ||
+ PyUnicodeEncodeError_SetEnd(buf->excobj, end) != 0 ||
+ PyUnicodeEncodeError_SetReason(buf->excobj, reason) != 0)
+ goto errorexit;
+
+ if (errors == ERROR_STRICT) {
+ PyCodec_StrictErrors(buf->excobj);
+ goto errorexit;
+ }
+
+ retobj = call_error_callback(errors, buf->excobj);
+ if (retobj == NULL)
+ goto errorexit;
+
+ if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
+ !PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) ||
+ !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) {
+ PyErr_SetString(PyExc_TypeError,
+ "encoding error handler must return "
+ "(unicode, int) tuple");
+ goto errorexit;
+ }
+
+ {
+ const Py_UNICODE *uraw = PyUnicode_AS_UNICODE(tobj);
+
+ retstr = multibytecodec_encode(codec, state, &uraw,
+ PyUnicode_GET_SIZE(tobj), ERROR_STRICT,
+ MBENC_FLUSH);
+ if (retstr == NULL)
+ goto errorexit;
+ }
+
+ assert(PyBytes_Check(retstr));
+ retstrsize = PyBytes_GET_SIZE(retstr);
+ REQUIRE_ENCODEBUFFER(buf, retstrsize);
+
+ memcpy(buf->outbuf, PyBytes_AS_STRING(retstr), retstrsize);
+ buf->outbuf += retstrsize;
+
+ newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1));
+ if (newpos < 0 && !PyErr_Occurred())
+ newpos += (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top);
+ if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) {
+ PyErr_Clear();
+ PyErr_Format(PyExc_IndexError,
+ "position %zd from error handler out of bounds",
+ newpos);
+ goto errorexit;
+ }
+ buf->inbuf = buf->inbuf_top + newpos;
+
+ Py_DECREF(retobj);
+ Py_DECREF(retstr);
+ return 0;
errorexit:
- Py_XDECREF(retobj);
- Py_XDECREF(retstr);
- return -1;
+ Py_XDECREF(retobj);
+ Py_XDECREF(retstr);
+ return -1;
}
static int
multibytecodec_decerror(MultibyteCodec *codec,
- MultibyteCodec_State *state,
- MultibyteDecodeBuffer *buf,
- PyObject *errors, Py_ssize_t e)
+ MultibyteCodec_State *state,
+ MultibyteDecodeBuffer *buf,
+ PyObject *errors, Py_ssize_t e)
{
- PyObject *retobj = NULL, *retuni = NULL;
- Py_ssize_t retunisize, newpos;
- const char *reason;
- Py_ssize_t esize, start, end;
-
- if (e > 0) {
- reason = "illegal multibyte sequence";
- esize = e;
- }
- else {
- switch (e) {
- case MBERR_TOOSMALL:
- REQUIRE_DECODEBUFFER(buf, -1);
- return 0; /* retry it */
- case MBERR_TOOFEW:
- reason = "incomplete multibyte sequence";
- esize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
- break;
- case MBERR_INTERNAL:
- PyErr_SetString(PyExc_RuntimeError,
- "internal codec error");
- return -1;
- default:
- PyErr_SetString(PyExc_RuntimeError,
- "unknown runtime error");
- return -1;
- }
- }
-
- if (errors == ERROR_REPLACE) {
- REQUIRE_DECODEBUFFER(buf, 1);
- *buf->outbuf++ = Py_UNICODE_REPLACEMENT_CHARACTER;
- }
- if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) {
- buf->inbuf += esize;
- return 0;
- }
-
- start = (Py_ssize_t)(buf->inbuf - buf->inbuf_top);
- end = start + esize;
-
- /* use cached exception object if available */
- if (buf->excobj == NULL) {
- buf->excobj = PyUnicodeDecodeError_Create(codec->encoding,
- (const char *)buf->inbuf_top,
- (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top),
- start, end, reason);
- if (buf->excobj == NULL)
- goto errorexit;
- }
- else
- if (PyUnicodeDecodeError_SetStart(buf->excobj, start) ||
- PyUnicodeDecodeError_SetEnd(buf->excobj, end) ||
- PyUnicodeDecodeError_SetReason(buf->excobj, reason))
- goto errorexit;
-
- if (errors == ERROR_STRICT) {
- PyCodec_StrictErrors(buf->excobj);
- goto errorexit;
- }
-
- retobj = call_error_callback(errors, buf->excobj);
- if (retobj == NULL)
- goto errorexit;
-
- if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
- !PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) ||
- !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) {
- PyErr_SetString(PyExc_TypeError,
- "decoding error handler must return "
- "(unicode, int) tuple");
- goto errorexit;
- }
-
- retunisize = PyUnicode_GET_SIZE(retuni);
- if (retunisize > 0) {
- REQUIRE_DECODEBUFFER(buf, retunisize);
- memcpy((char *)buf->outbuf, PyUnicode_AS_DATA(retuni),
- retunisize * Py_UNICODE_SIZE);
- buf->outbuf += retunisize;
- }
-
- newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1));
- if (newpos < 0 && !PyErr_Occurred())
- newpos += (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top);
- if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) {
- PyErr_Clear();
- PyErr_Format(PyExc_IndexError,
- "position %zd from error handler out of bounds",
- newpos);
- goto errorexit;
- }
- buf->inbuf = buf->inbuf_top + newpos;
- Py_DECREF(retobj);
- return 0;
+ PyObject *retobj = NULL, *retuni = NULL;
+ Py_ssize_t retunisize, newpos;
+ const char *reason;
+ Py_ssize_t esize, start, end;
+
+ if (e > 0) {
+ reason = "illegal multibyte sequence";
+ esize = e;
+ }
+ else {
+ switch (e) {
+ case MBERR_TOOSMALL:
+ REQUIRE_DECODEBUFFER(buf, -1);
+ return 0; /* retry it */
+ case MBERR_TOOFEW:
+ reason = "incomplete multibyte sequence";
+ esize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
+ break;
+ case MBERR_INTERNAL:
+ PyErr_SetString(PyExc_RuntimeError,
+ "internal codec error");
+ return -1;
+ default:
+ PyErr_SetString(PyExc_RuntimeError,
+ "unknown runtime error");
+ return -1;
+ }
+ }
+
+ if (errors == ERROR_REPLACE) {
+ REQUIRE_DECODEBUFFER(buf, 1);
+ *buf->outbuf++ = Py_UNICODE_REPLACEMENT_CHARACTER;
+ }
+ if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) {
+ buf->inbuf += esize;
+ return 0;
+ }
+
+ start = (Py_ssize_t)(buf->inbuf - buf->inbuf_top);
+ end = start + esize;
+
+ /* use cached exception object if available */
+ if (buf->excobj == NULL) {
+ buf->excobj = PyUnicodeDecodeError_Create(codec->encoding,
+ (const char *)buf->inbuf_top,
+ (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top),
+ start, end, reason);
+ if (buf->excobj == NULL)
+ goto errorexit;
+ }
+ else
+ if (PyUnicodeDecodeError_SetStart(buf->excobj, start) ||
+ PyUnicodeDecodeError_SetEnd(buf->excobj, end) ||
+ PyUnicodeDecodeError_SetReason(buf->excobj, reason))
+ goto errorexit;
+
+ if (errors == ERROR_STRICT) {
+ PyCodec_StrictErrors(buf->excobj);
+ goto errorexit;
+ }
+
+ retobj = call_error_callback(errors, buf->excobj);
+ if (retobj == NULL)
+ goto errorexit;
+
+ if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
+ !PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) ||
+ !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) {
+ PyErr_SetString(PyExc_TypeError,
+ "decoding error handler must return "
+ "(unicode, int) tuple");
+ goto errorexit;
+ }
+
+ retunisize = PyUnicode_GET_SIZE(retuni);
+ if (retunisize > 0) {
+ REQUIRE_DECODEBUFFER(buf, retunisize);
+ memcpy((char *)buf->outbuf, PyUnicode_AS_DATA(retuni),
+ retunisize * Py_UNICODE_SIZE);
+ buf->outbuf += retunisize;
+ }
+
+ newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1));
+ if (newpos < 0 && !PyErr_Occurred())
+ newpos += (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top);
+ if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) {
+ PyErr_Clear();
+ PyErr_Format(PyExc_IndexError,
+ "position %zd from error handler out of bounds",
+ newpos);
+ goto errorexit;
+ }
+ buf->inbuf = buf->inbuf_top + newpos;
+ Py_DECREF(retobj);
+ return 0;
errorexit:
- Py_XDECREF(retobj);
- return -1;
+ Py_XDECREF(retobj);
+ return -1;
}
static PyObject *
multibytecodec_encode(MultibyteCodec *codec,
- MultibyteCodec_State *state,
- const Py_UNICODE **data, Py_ssize_t datalen,
- PyObject *errors, int flags)
+ MultibyteCodec_State *state,
+ const Py_UNICODE **data, Py_ssize_t datalen,
+ PyObject *errors, int flags)
{
- MultibyteEncodeBuffer buf;
- Py_ssize_t finalsize, r = 0;
-
- if (datalen == 0)
- return PyBytes_FromStringAndSize(NULL, 0);
-
- buf.excobj = NULL;
- buf.inbuf = buf.inbuf_top = *data;
- buf.inbuf_end = buf.inbuf_top + datalen;
-
- if (datalen > (PY_SSIZE_T_MAX - 16) / 2) {
- PyErr_NoMemory();
- goto errorexit;
- }
-
- buf.outobj = PyBytes_FromStringAndSize(NULL, datalen * 2 + 16);
- if (buf.outobj == NULL)
- goto errorexit;
- buf.outbuf = (unsigned char *)PyBytes_AS_STRING(buf.outobj);
- buf.outbuf_end = buf.outbuf + PyBytes_GET_SIZE(buf.outobj);
-
- while (buf.inbuf < buf.inbuf_end) {
- Py_ssize_t inleft, outleft;
-
- /* we don't reuse inleft and outleft here.
- * error callbacks can relocate the cursor anywhere on buffer*/
- inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf);
- outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
- r = codec->encode(state, codec->config, &buf.inbuf, inleft,
- &buf.outbuf, outleft, flags);
- if ((r == 0) || (r == MBERR_TOOFEW && !(flags & MBENC_FLUSH)))
- break;
- else if (multibytecodec_encerror(codec, state, &buf, errors,r))
- goto errorexit;
- else if (r == MBERR_TOOFEW)
- break;
- }
-
- if (codec->encreset != NULL)
- for (;;) {
- Py_ssize_t outleft;
-
- outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
- r = codec->encreset(state, codec->config, &buf.outbuf,
- outleft);
- if (r == 0)
- break;
- else if (multibytecodec_encerror(codec, state,
- &buf, errors, r))
- goto errorexit;
- }
-
- finalsize = (Py_ssize_t)((char *)buf.outbuf -
- PyBytes_AS_STRING(buf.outobj));
-
- if (finalsize != PyBytes_GET_SIZE(buf.outobj))
- if (_PyBytes_Resize(&buf.outobj, finalsize) == -1)
- goto errorexit;
-
- *data = buf.inbuf;
- Py_XDECREF(buf.excobj);
- return buf.outobj;
+ MultibyteEncodeBuffer buf;
+ Py_ssize_t finalsize, r = 0;
+
+ if (datalen == 0)
+ return PyBytes_FromStringAndSize(NULL, 0);
+
+ buf.excobj = NULL;
+ buf.inbuf = buf.inbuf_top = *data;
+ buf.inbuf_end = buf.inbuf_top + datalen;
+
+ if (datalen > (PY_SSIZE_T_MAX - 16) / 2) {
+ PyErr_NoMemory();
+ goto errorexit;
+ }
+
+ buf.outobj = PyBytes_FromStringAndSize(NULL, datalen * 2 + 16);
+ if (buf.outobj == NULL)
+ goto errorexit;
+ buf.outbuf = (unsigned char *)PyBytes_AS_STRING(buf.outobj);
+ buf.outbuf_end = buf.outbuf + PyBytes_GET_SIZE(buf.outobj);
+
+ while (buf.inbuf < buf.inbuf_end) {
+ Py_ssize_t inleft, outleft;
+
+ /* we don't reuse inleft and outleft here.
+ * error callbacks can relocate the cursor anywhere on buffer*/
+ inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf);
+ outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
+ r = codec->encode(state, codec->config, &buf.inbuf, inleft,
+ &buf.outbuf, outleft, flags);
+ if ((r == 0) || (r == MBERR_TOOFEW && !(flags & MBENC_FLUSH)))
+ break;
+ else if (multibytecodec_encerror(codec, state, &buf, errors,r))
+ goto errorexit;
+ else if (r == MBERR_TOOFEW)
+ break;
+ }
+
+ if (codec->encreset != NULL)
+ for (;;) {
+ Py_ssize_t outleft;
+
+ outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
+ r = codec->encreset(state, codec->config, &buf.outbuf,
+ outleft);
+ if (r == 0)
+ break;
+ else if (multibytecodec_encerror(codec, state,
+ &buf, errors, r))
+ goto errorexit;
+ }
+
+ finalsize = (Py_ssize_t)((char *)buf.outbuf -
+ PyBytes_AS_STRING(buf.outobj));
+
+ if (finalsize != PyBytes_GET_SIZE(buf.outobj))
+ if (_PyBytes_Resize(&buf.outobj, finalsize) == -1)
+ goto errorexit;
+
+ *data = buf.inbuf;
+ Py_XDECREF(buf.excobj);
+ return buf.outobj;
errorexit:
- Py_XDECREF(buf.excobj);
- Py_XDECREF(buf.outobj);
- return NULL;
+ Py_XDECREF(buf.excobj);
+ Py_XDECREF(buf.outobj);
+ return NULL;
}
static PyObject *
MultibyteCodec_Encode(MultibyteCodecObject *self,
- PyObject *args, PyObject *kwargs)
+ PyObject *args, PyObject *kwargs)
{
- MultibyteCodec_State state;
- Py_UNICODE *data;
- PyObject *errorcb, *r, *arg, *ucvt;
- const char *errors = NULL;
- Py_ssize_t datalen;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode",
- codeckwarglist, &arg, &errors))
- return NULL;
-
- if (PyUnicode_Check(arg))
- ucvt = NULL;
- else {
- arg = ucvt = PyObject_Str(arg);
- if (arg == NULL)
- return NULL;
- else if (!PyUnicode_Check(arg)) {
- PyErr_SetString(PyExc_TypeError,
- "couldn't convert the object to unicode.");
- Py_DECREF(ucvt);
- return NULL;
- }
- }
-
- data = PyUnicode_AS_UNICODE(arg);
- datalen = PyUnicode_GET_SIZE(arg);
-
- errorcb = internal_error_callback(errors);
- if (errorcb == NULL) {
- Py_XDECREF(ucvt);
- return NULL;
- }
-
- if (self->codec->encinit != NULL &&
- self->codec->encinit(&state, self->codec->config) != 0)
- goto errorexit;
- r = multibytecodec_encode(self->codec, &state,
- (const Py_UNICODE **)&data, datalen, errorcb,
- MBENC_FLUSH | MBENC_RESET);
- if (r == NULL)
- goto errorexit;
-
- ERROR_DECREF(errorcb);
- Py_XDECREF(ucvt);
- return make_tuple(r, datalen);
+ MultibyteCodec_State state;
+ Py_UNICODE *data;
+ PyObject *errorcb, *r, *arg, *ucvt;
+ const char *errors = NULL;
+ Py_ssize_t datalen;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode",
+ codeckwarglist, &arg, &errors))
+ return NULL;
+
+ if (PyUnicode_Check(arg))
+ ucvt = NULL;
+ else {
+ arg = ucvt = PyObject_Str(arg);
+ if (arg == NULL)
+ return NULL;
+ else if (!PyUnicode_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError,
+ "couldn't convert the object to unicode.");
+ Py_DECREF(ucvt);
+ return NULL;
+ }
+ }
+
+ data = PyUnicode_AS_UNICODE(arg);
+ datalen = PyUnicode_GET_SIZE(arg);
+
+ errorcb = internal_error_callback(errors);
+ if (errorcb == NULL) {
+ Py_XDECREF(ucvt);
+ return NULL;
+ }
+
+ if (self->codec->encinit != NULL &&
+ self->codec->encinit(&state, self->codec->config) != 0)
+ goto errorexit;
+ r = multibytecodec_encode(self->codec, &state,
+ (const Py_UNICODE **)&data, datalen, errorcb,
+ MBENC_FLUSH | MBENC_RESET);
+ if (r == NULL)
+ goto errorexit;
+
+ ERROR_DECREF(errorcb);
+ Py_XDECREF(ucvt);
+ return make_tuple(r, datalen);
errorexit:
- ERROR_DECREF(errorcb);
- Py_XDECREF(ucvt);
- return NULL;
+ ERROR_DECREF(errorcb);
+ Py_XDECREF(ucvt);
+ return NULL;
}
static PyObject *
MultibyteCodec_Decode(MultibyteCodecObject *self,
- PyObject *args, PyObject *kwargs)
+ PyObject *args, PyObject *kwargs)
{
- MultibyteCodec_State state;
- MultibyteDecodeBuffer buf;
- PyObject *errorcb;
- Py_buffer pdata;
- const char *data, *errors = NULL;
- Py_ssize_t datalen, finalsize;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode",
- codeckwarglist, &pdata, &errors))
- return NULL;
- data = pdata.buf;
- datalen = pdata.len;
-
- errorcb = internal_error_callback(errors);
- if (errorcb == NULL) {
- PyBuffer_Release(&pdata);
- return NULL;
- }
-
- if (datalen == 0) {
- PyBuffer_Release(&pdata);
- ERROR_DECREF(errorcb);
- return make_tuple(PyUnicode_FromUnicode(NULL, 0), 0);
- }
-
- buf.excobj = NULL;
- buf.inbuf = buf.inbuf_top = (unsigned char *)data;
- buf.inbuf_end = buf.inbuf_top + datalen;
- buf.outobj = PyUnicode_FromUnicode(NULL, datalen);
- if (buf.outobj == NULL)
- goto errorexit;
- buf.outbuf = PyUnicode_AS_UNICODE(buf.outobj);
- buf.outbuf_end = buf.outbuf + PyUnicode_GET_SIZE(buf.outobj);
-
- if (self->codec->decinit != NULL &&
- self->codec->decinit(&state, self->codec->config) != 0)
- goto errorexit;
-
- while (buf.inbuf < buf.inbuf_end) {
- Py_ssize_t inleft, outleft, r;
-
- inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf);
- outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
-
- r = self->codec->decode(&state, self->codec->config,
- &buf.inbuf, inleft, &buf.outbuf, outleft);
- if (r == 0)
- break;
- else if (multibytecodec_decerror(self->codec, &state,
- &buf, errorcb, r))
- goto errorexit;
- }
-
- finalsize = (Py_ssize_t)(buf.outbuf -
- PyUnicode_AS_UNICODE(buf.outobj));
-
- if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
- if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
- goto errorexit;
-
- PyBuffer_Release(&pdata);
- Py_XDECREF(buf.excobj);
- ERROR_DECREF(errorcb);
- return make_tuple(buf.outobj, datalen);
+ MultibyteCodec_State state;
+ MultibyteDecodeBuffer buf;
+ PyObject *errorcb;
+ Py_buffer pdata;
+ const char *data, *errors = NULL;
+ Py_ssize_t datalen, finalsize;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode",
+ codeckwarglist, &pdata, &errors))
+ return NULL;
+ data = pdata.buf;
+ datalen = pdata.len;
+
+ errorcb = internal_error_callback(errors);
+ if (errorcb == NULL) {
+ PyBuffer_Release(&pdata);
+ return NULL;
+ }
+
+ if (datalen == 0) {
+ PyBuffer_Release(&pdata);
+ ERROR_DECREF(errorcb);
+ return make_tuple(PyUnicode_FromUnicode(NULL, 0), 0);
+ }
+
+ buf.excobj = NULL;
+ buf.inbuf = buf.inbuf_top = (unsigned char *)data;
+ buf.inbuf_end = buf.inbuf_top + datalen;
+ buf.outobj = PyUnicode_FromUnicode(NULL, datalen);
+ if (buf.outobj == NULL)
+ goto errorexit;
+ buf.outbuf = PyUnicode_AS_UNICODE(buf.outobj);
+ buf.outbuf_end = buf.outbuf + PyUnicode_GET_SIZE(buf.outobj);
+
+ if (self->codec->decinit != NULL &&
+ self->codec->decinit(&state, self->codec->config) != 0)
+ goto errorexit;
+
+ while (buf.inbuf < buf.inbuf_end) {
+ Py_ssize_t inleft, outleft, r;
+
+ inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf);
+ outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
+
+ r = self->codec->decode(&state, self->codec->config,
+ &buf.inbuf, inleft, &buf.outbuf, outleft);
+ if (r == 0)
+ break;
+ else if (multibytecodec_decerror(self->codec, &state,
+ &buf, errorcb, r))
+ goto errorexit;
+ }
+
+ finalsize = (Py_ssize_t)(buf.outbuf -
+ PyUnicode_AS_UNICODE(buf.outobj));
+
+ if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
+ if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
+ goto errorexit;
+
+ PyBuffer_Release(&pdata);
+ Py_XDECREF(buf.excobj);
+ ERROR_DECREF(errorcb);
+ return make_tuple(buf.outobj, datalen);
errorexit:
- PyBuffer_Release(&pdata);
- ERROR_DECREF(errorcb);
- Py_XDECREF(buf.excobj);
- Py_XDECREF(buf.outobj);
+ PyBuffer_Release(&pdata);
+ ERROR_DECREF(errorcb);
+ Py_XDECREF(buf.excobj);
+ Py_XDECREF(buf.outobj);
- return NULL;
+ return NULL;
}
static struct PyMethodDef multibytecodec_methods[] = {
- {"encode", (PyCFunction)MultibyteCodec_Encode,
- METH_VARARGS | METH_KEYWORDS,
- MultibyteCodec_Encode__doc__},
- {"decode", (PyCFunction)MultibyteCodec_Decode,
- METH_VARARGS | METH_KEYWORDS,
- MultibyteCodec_Decode__doc__},
- {NULL, NULL},
+ {"encode", (PyCFunction)MultibyteCodec_Encode,
+ METH_VARARGS | METH_KEYWORDS,
+ MultibyteCodec_Encode__doc__},
+ {"decode", (PyCFunction)MultibyteCodec_Decode,
+ METH_VARARGS | METH_KEYWORDS,
+ MultibyteCodec_Decode__doc__},
+ {NULL, NULL},
};
static void
multibytecodec_dealloc(MultibyteCodecObject *self)
{
- PyObject_Del(self);
+ PyObject_Del(self);
}
static PyTypeObject MultibyteCodec_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "MultibyteCodec", /* tp_name */
- sizeof(MultibyteCodecObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)multibytecodec_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iterext */
- multibytecodec_methods, /* tp_methods */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MultibyteCodec", /* tp_name */
+ sizeof(MultibyteCodecObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)multibytecodec_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iterext */
+ multibytecodec_methods, /* tp_methods */
};
@@ -732,150 +732,150 @@ static PyTypeObject MultibyteCodec_Type = {
* Utility functions for stateful codec mechanism
*/
-#define STATEFUL_DCTX(o) ((MultibyteStatefulDecoderContext *)(o))
-#define STATEFUL_ECTX(o) ((MultibyteStatefulEncoderContext *)(o))
+#define STATEFUL_DCTX(o) ((MultibyteStatefulDecoderContext *)(o))
+#define STATEFUL_ECTX(o) ((MultibyteStatefulEncoderContext *)(o))
static PyObject *
encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
- PyObject *unistr, int final)
+ PyObject *unistr, int final)
{
- PyObject *ucvt, *r = NULL;
- Py_UNICODE *inbuf, *inbuf_end, *inbuf_tmp = NULL;
- Py_ssize_t datalen, origpending;
-
- if (PyUnicode_Check(unistr))
- ucvt = NULL;
- else {
- unistr = ucvt = PyObject_Str(unistr);
- if (unistr == NULL)
- return NULL;
- else if (!PyUnicode_Check(unistr)) {
- PyErr_SetString(PyExc_TypeError,
- "couldn't convert the object to unicode.");
- Py_DECREF(ucvt);
- return NULL;
- }
- }
-
- datalen = PyUnicode_GET_SIZE(unistr);
- origpending = ctx->pendingsize;
-
- if (origpending > 0) {
- if (datalen > PY_SSIZE_T_MAX - ctx->pendingsize) {
- PyErr_NoMemory();
- /* inbuf_tmp == NULL */
- goto errorexit;
- }
- inbuf_tmp = PyMem_New(Py_UNICODE, datalen + ctx->pendingsize);
- if (inbuf_tmp == NULL)
- goto errorexit;
- memcpy(inbuf_tmp, ctx->pending,
- Py_UNICODE_SIZE * ctx->pendingsize);
- memcpy(inbuf_tmp + ctx->pendingsize,
- PyUnicode_AS_UNICODE(unistr),
- Py_UNICODE_SIZE * datalen);
- datalen += ctx->pendingsize;
- ctx->pendingsize = 0;
- inbuf = inbuf_tmp;
- }
- else
- inbuf = (Py_UNICODE *)PyUnicode_AS_UNICODE(unistr);
-
- inbuf_end = inbuf + datalen;
-
- r = multibytecodec_encode(ctx->codec, &ctx->state,
- (const Py_UNICODE **)&inbuf,
- datalen, ctx->errors, final ? MBENC_FLUSH : 0);
- if (r == NULL) {
- /* recover the original pending buffer */
- if (origpending > 0)
- memcpy(ctx->pending, inbuf_tmp,
- Py_UNICODE_SIZE * origpending);
- ctx->pendingsize = origpending;
- goto errorexit;
- }
-
- if (inbuf < inbuf_end) {
- ctx->pendingsize = (Py_ssize_t)(inbuf_end - inbuf);
- if (ctx->pendingsize > MAXENCPENDING) {
- /* normal codecs can't reach here */
- ctx->pendingsize = 0;
- PyErr_SetString(PyExc_UnicodeError,
- "pending buffer overflow");
- goto errorexit;
- }
- memcpy(ctx->pending, inbuf,
- ctx->pendingsize * Py_UNICODE_SIZE);
- }
-
- if (inbuf_tmp != NULL)
- PyMem_Del(inbuf_tmp);
- Py_XDECREF(ucvt);
- return r;
+ PyObject *ucvt, *r = NULL;
+ Py_UNICODE *inbuf, *inbuf_end, *inbuf_tmp = NULL;
+ Py_ssize_t datalen, origpending;
+
+ if (PyUnicode_Check(unistr))
+ ucvt = NULL;
+ else {
+ unistr = ucvt = PyObject_Str(unistr);
+ if (unistr == NULL)
+ return NULL;
+ else if (!PyUnicode_Check(unistr)) {
+ PyErr_SetString(PyExc_TypeError,
+ "couldn't convert the object to unicode.");
+ Py_DECREF(ucvt);
+ return NULL;
+ }
+ }
+
+ datalen = PyUnicode_GET_SIZE(unistr);
+ origpending = ctx->pendingsize;
+
+ if (origpending > 0) {
+ if (datalen > PY_SSIZE_T_MAX - ctx->pendingsize) {
+ PyErr_NoMemory();
+ /* inbuf_tmp == NULL */
+ goto errorexit;
+ }
+ inbuf_tmp = PyMem_New(Py_UNICODE, datalen + ctx->pendingsize);
+ if (inbuf_tmp == NULL)
+ goto errorexit;
+ memcpy(inbuf_tmp, ctx->pending,
+ Py_UNICODE_SIZE * ctx->pendingsize);
+ memcpy(inbuf_tmp + ctx->pendingsize,
+ PyUnicode_AS_UNICODE(unistr),
+ Py_UNICODE_SIZE * datalen);
+ datalen += ctx->pendingsize;
+ ctx->pendingsize = 0;
+ inbuf = inbuf_tmp;
+ }
+ else
+ inbuf = (Py_UNICODE *)PyUnicode_AS_UNICODE(unistr);
+
+ inbuf_end = inbuf + datalen;
+
+ r = multibytecodec_encode(ctx->codec, &ctx->state,
+ (const Py_UNICODE **)&inbuf,
+ datalen, ctx->errors, final ? MBENC_FLUSH : 0);
+ if (r == NULL) {
+ /* recover the original pending buffer */
+ if (origpending > 0)
+ memcpy(ctx->pending, inbuf_tmp,
+ Py_UNICODE_SIZE * origpending);
+ ctx->pendingsize = origpending;
+ goto errorexit;
+ }
+
+ if (inbuf < inbuf_end) {
+ ctx->pendingsize = (Py_ssize_t)(inbuf_end - inbuf);
+ if (ctx->pendingsize > MAXENCPENDING) {
+ /* normal codecs can't reach here */
+ ctx->pendingsize = 0;
+ PyErr_SetString(PyExc_UnicodeError,
+ "pending buffer overflow");
+ goto errorexit;
+ }
+ memcpy(ctx->pending, inbuf,
+ ctx->pendingsize * Py_UNICODE_SIZE);
+ }
+
+ if (inbuf_tmp != NULL)
+ PyMem_Del(inbuf_tmp);
+ Py_XDECREF(ucvt);
+ return r;
errorexit:
- if (inbuf_tmp != NULL)
- PyMem_Del(inbuf_tmp);
- Py_XDECREF(r);
- Py_XDECREF(ucvt);
- return NULL;
+ if (inbuf_tmp != NULL)
+ PyMem_Del(inbuf_tmp);
+ Py_XDECREF(r);
+ Py_XDECREF(ucvt);
+ return NULL;
}
static int
decoder_append_pending(MultibyteStatefulDecoderContext *ctx,
- MultibyteDecodeBuffer *buf)
+ MultibyteDecodeBuffer *buf)
{
- Py_ssize_t npendings;
-
- npendings = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
- if (npendings + ctx->pendingsize > MAXDECPENDING ||
- npendings > PY_SSIZE_T_MAX - ctx->pendingsize) {
- PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow");
- return -1;
- }
- memcpy(ctx->pending + ctx->pendingsize, buf->inbuf, npendings);
- ctx->pendingsize += npendings;
- return 0;
+ Py_ssize_t npendings;
+
+ npendings = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
+ if (npendings + ctx->pendingsize > MAXDECPENDING ||
+ npendings > PY_SSIZE_T_MAX - ctx->pendingsize) {
+ PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow");
+ return -1;
+ }
+ memcpy(ctx->pending + ctx->pendingsize, buf->inbuf, npendings);
+ ctx->pendingsize += npendings;
+ return 0;
}
static int
decoder_prepare_buffer(MultibyteDecodeBuffer *buf, const char *data,
- Py_ssize_t size)
+ Py_ssize_t size)
{
- buf->inbuf = buf->inbuf_top = (const unsigned char *)data;
- buf->inbuf_end = buf->inbuf_top + size;
- if (buf->outobj == NULL) { /* only if outobj is not allocated yet */
- buf->outobj = PyUnicode_FromUnicode(NULL, size);
- if (buf->outobj == NULL)
- return -1;
- buf->outbuf = PyUnicode_AS_UNICODE(buf->outobj);
- buf->outbuf_end = buf->outbuf +
- PyUnicode_GET_SIZE(buf->outobj);
- }
-
- return 0;
+ buf->inbuf = buf->inbuf_top = (const unsigned char *)data;
+ buf->inbuf_end = buf->inbuf_top + size;
+ if (buf->outobj == NULL) { /* only if outobj is not allocated yet */
+ buf->outobj = PyUnicode_FromUnicode(NULL, size);
+ if (buf->outobj == NULL)
+ return -1;
+ buf->outbuf = PyUnicode_AS_UNICODE(buf->outobj);
+ buf->outbuf_end = buf->outbuf +
+ PyUnicode_GET_SIZE(buf->outobj);
+ }
+
+ return 0;
}
static int
decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx,
- MultibyteDecodeBuffer *buf)
+ MultibyteDecodeBuffer *buf)
{
- while (buf->inbuf < buf->inbuf_end) {
- Py_ssize_t inleft, outleft;
- Py_ssize_t r;
-
- inleft = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
- outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
-
- r = ctx->codec->decode(&ctx->state, ctx->codec->config,
- &buf->inbuf, inleft, &buf->outbuf, outleft);
- if (r == 0 || r == MBERR_TOOFEW)
- break;
- else if (multibytecodec_decerror(ctx->codec, &ctx->state,
- buf, ctx->errors, r))
- return -1;
- }
- return 0;
+ while (buf->inbuf < buf->inbuf_end) {
+ Py_ssize_t inleft, outleft;
+ Py_ssize_t r;
+
+ inleft = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
+ outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
+
+ r = ctx->codec->decode(&ctx->state, ctx->codec->config,
+ &buf->inbuf, inleft, &buf->outbuf, outleft);
+ if (r == 0 || r == MBERR_TOOFEW)
+ break;
+ else if (multibytecodec_decerror(ctx->codec, &ctx->state,
+ buf, ctx->errors, r))
+ return -1;
+ }
+ return 0;
}
@@ -885,142 +885,142 @@ decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx,
static PyObject *
mbiencoder_encode(MultibyteIncrementalEncoderObject *self,
- PyObject *args, PyObject *kwargs)
+ PyObject *args, PyObject *kwargs)
{
- PyObject *data;
- int final = 0;
+ PyObject *data;
+ int final = 0;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode",
- incrementalkwarglist, &data, &final))
- return NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode",
+ incrementalkwarglist, &data, &final))
+ return NULL;
- return encoder_encode_stateful(STATEFUL_ECTX(self), data, final);
+ return encoder_encode_stateful(STATEFUL_ECTX(self), data, final);
}
static PyObject *
mbiencoder_reset(MultibyteIncrementalEncoderObject *self)
{
- if (self->codec->decreset != NULL &&
- self->codec->decreset(&self->state, self->codec->config) != 0)
- return NULL;
- self->pendingsize = 0;
+ if (self->codec->decreset != NULL &&
+ self->codec->decreset(&self->state, self->codec->config) != 0)
+ return NULL;
+ self->pendingsize = 0;
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static struct PyMethodDef mbiencoder_methods[] = {
- {"encode", (PyCFunction)mbiencoder_encode,
- METH_VARARGS | METH_KEYWORDS, NULL},
- {"reset", (PyCFunction)mbiencoder_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ {"encode", (PyCFunction)mbiencoder_encode,
+ METH_VARARGS | METH_KEYWORDS, NULL},
+ {"reset", (PyCFunction)mbiencoder_reset,
+ METH_NOARGS, NULL},
+ {NULL, NULL},
};
static PyObject *
mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- MultibyteIncrementalEncoderObject *self;
- PyObject *codec = NULL;
- char *errors = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalEncoder",
- incnewkwarglist, &errors))
- return NULL;
-
- self = (MultibyteIncrementalEncoderObject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- codec = PyObject_GetAttrString((PyObject *)type, "codec");
- if (codec == NULL)
- goto errorexit;
- if (!MultibyteCodec_Check(codec)) {
- PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
- goto errorexit;
- }
-
- self->codec = ((MultibyteCodecObject *)codec)->codec;
- self->pendingsize = 0;
- self->errors = internal_error_callback(errors);
- if (self->errors == NULL)
- goto errorexit;
- if (self->codec->encinit != NULL &&
- self->codec->encinit(&self->state, self->codec->config) != 0)
- goto errorexit;
-
- Py_DECREF(codec);
- return (PyObject *)self;
+ MultibyteIncrementalEncoderObject *self;
+ PyObject *codec = NULL;
+ char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalEncoder",
+ incnewkwarglist, &errors))
+ return NULL;
+
+ self = (MultibyteIncrementalEncoderObject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ codec = PyObject_GetAttrString((PyObject *)type, "codec");
+ if (codec == NULL)
+ goto errorexit;
+ if (!MultibyteCodec_Check(codec)) {
+ PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
+ goto errorexit;
+ }
+
+ self->codec = ((MultibyteCodecObject *)codec)->codec;
+ self->pendingsize = 0;
+ self->errors = internal_error_callback(errors);
+ if (self->errors == NULL)
+ goto errorexit;
+ if (self->codec->encinit != NULL &&
+ self->codec->encinit(&self->state, self->codec->config) != 0)
+ goto errorexit;
+
+ Py_DECREF(codec);
+ return (PyObject *)self;
errorexit:
- Py_XDECREF(self);
- Py_XDECREF(codec);
- return NULL;
+ Py_XDECREF(self);
+ Py_XDECREF(codec);
+ return NULL;
}
static int
mbiencoder_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- return 0;
+ return 0;
}
static int
mbiencoder_traverse(MultibyteIncrementalEncoderObject *self,
- visitproc visit, void *arg)
+ visitproc visit, void *arg)
{
- if (ERROR_ISCUSTOM(self->errors))
- Py_VISIT(self->errors);
- return 0;
+ if (ERROR_ISCUSTOM(self->errors))
+ Py_VISIT(self->errors);
+ return 0;
}
static void
mbiencoder_dealloc(MultibyteIncrementalEncoderObject *self)
{
- PyObject_GC_UnTrack(self);
- ERROR_DECREF(self->errors);
- Py_TYPE(self)->tp_free(self);
+ PyObject_GC_UnTrack(self);
+ ERROR_DECREF(self->errors);
+ Py_TYPE(self)->tp_free(self);
}
static PyTypeObject MultibyteIncrementalEncoder_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "MultibyteIncrementalEncoder", /* tp_name */
- sizeof(MultibyteIncrementalEncoderObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)mbiencoder_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
- | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- (traverseproc)mbiencoder_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iterext */
- mbiencoder_methods, /* tp_methods */
- 0, /* tp_members */
- codecctx_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- mbiencoder_init, /* tp_init */
- 0, /* tp_alloc */
- mbiencoder_new, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MultibyteIncrementalEncoder", /* tp_name */
+ sizeof(MultibyteIncrementalEncoderObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)mbiencoder_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
+ | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)mbiencoder_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iterext */
+ mbiencoder_methods, /* tp_methods */
+ 0, /* tp_members */
+ codecctx_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ mbiencoder_init, /* tp_init */
+ 0, /* tp_alloc */
+ mbiencoder_new, /* tp_new */
};
@@ -1030,206 +1030,206 @@ static PyTypeObject MultibyteIncrementalEncoder_Type = {
static PyObject *
mbidecoder_decode(MultibyteIncrementalDecoderObject *self,
- PyObject *args, PyObject *kwargs)
+ PyObject *args, PyObject *kwargs)
{
- MultibyteDecodeBuffer buf;
- char *data, *wdata = NULL;
- Py_buffer pdata;
- Py_ssize_t wsize, finalsize = 0, size, origpending;
- int final = 0;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode",
- incrementalkwarglist, &pdata, &final))
- return NULL;
- data = pdata.buf;
- size = pdata.len;
-
- buf.outobj = buf.excobj = NULL;
- origpending = self->pendingsize;
-
- if (self->pendingsize == 0) {
- wsize = size;
- wdata = data;
- }
- else {
- if (size > PY_SSIZE_T_MAX - self->pendingsize) {
- PyErr_NoMemory();
- goto errorexit;
- }
- wsize = size + self->pendingsize;
- wdata = PyMem_Malloc(wsize);
- if (wdata == NULL)
- goto errorexit;
- memcpy(wdata, self->pending, self->pendingsize);
- memcpy(wdata + self->pendingsize, data, size);
- self->pendingsize = 0;
- }
-
- if (decoder_prepare_buffer(&buf, wdata, wsize) != 0)
- goto errorexit;
-
- if (decoder_feed_buffer(STATEFUL_DCTX(self), &buf))
- goto errorexit;
-
- if (final && buf.inbuf < buf.inbuf_end) {
- if (multibytecodec_decerror(self->codec, &self->state,
- &buf, self->errors, MBERR_TOOFEW)) {
- /* recover the original pending buffer */
- memcpy(self->pending, wdata, origpending);
- self->pendingsize = origpending;
- goto errorexit;
- }
- }
-
- if (buf.inbuf < buf.inbuf_end) { /* pending sequence still exists */
- if (decoder_append_pending(STATEFUL_DCTX(self), &buf) != 0)
- goto errorexit;
- }
-
- finalsize = (Py_ssize_t)(buf.outbuf - PyUnicode_AS_UNICODE(buf.outobj));
- if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
- if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
- goto errorexit;
-
- PyBuffer_Release(&pdata);
- if (wdata != data)
- PyMem_Del(wdata);
- Py_XDECREF(buf.excobj);
- return buf.outobj;
+ MultibyteDecodeBuffer buf;
+ char *data, *wdata = NULL;
+ Py_buffer pdata;
+ Py_ssize_t wsize, finalsize = 0, size, origpending;
+ int final = 0;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode",
+ incrementalkwarglist, &pdata, &final))
+ return NULL;
+ data = pdata.buf;
+ size = pdata.len;
+
+ buf.outobj = buf.excobj = NULL;
+ origpending = self->pendingsize;
+
+ if (self->pendingsize == 0) {
+ wsize = size;
+ wdata = data;
+ }
+ else {
+ if (size > PY_SSIZE_T_MAX - self->pendingsize) {
+ PyErr_NoMemory();
+ goto errorexit;
+ }
+ wsize = size + self->pendingsize;
+ wdata = PyMem_Malloc(wsize);
+ if (wdata == NULL)
+ goto errorexit;
+ memcpy(wdata, self->pending, self->pendingsize);
+ memcpy(wdata + self->pendingsize, data, size);
+ self->pendingsize = 0;
+ }
+
+ if (decoder_prepare_buffer(&buf, wdata, wsize) != 0)
+ goto errorexit;
+
+ if (decoder_feed_buffer(STATEFUL_DCTX(self), &buf))
+ goto errorexit;
+
+ if (final && buf.inbuf < buf.inbuf_end) {
+ if (multibytecodec_decerror(self->codec, &self->state,
+ &buf, self->errors, MBERR_TOOFEW)) {
+ /* recover the original pending buffer */
+ memcpy(self->pending, wdata, origpending);
+ self->pendingsize = origpending;
+ goto errorexit;
+ }
+ }
+
+ if (buf.inbuf < buf.inbuf_end) { /* pending sequence still exists */
+ if (decoder_append_pending(STATEFUL_DCTX(self), &buf) != 0)
+ goto errorexit;
+ }
+
+ finalsize = (Py_ssize_t)(buf.outbuf - PyUnicode_AS_UNICODE(buf.outobj));
+ if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
+ if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
+ goto errorexit;
+
+ PyBuffer_Release(&pdata);
+ if (wdata != data)
+ PyMem_Del(wdata);
+ Py_XDECREF(buf.excobj);
+ return buf.outobj;
errorexit:
- PyBuffer_Release(&pdata);
- if (wdata != NULL && wdata != data)
- PyMem_Del(wdata);
- Py_XDECREF(buf.excobj);
- Py_XDECREF(buf.outobj);
- return NULL;
+ PyBuffer_Release(&pdata);
+ if (wdata != NULL && wdata != data)
+ PyMem_Del(wdata);
+ Py_XDECREF(buf.excobj);
+ Py_XDECREF(buf.outobj);
+ return NULL;
}
static PyObject *
mbidecoder_reset(MultibyteIncrementalDecoderObject *self)
{
- if (self->codec->decreset != NULL &&
- self->codec->decreset(&self->state, self->codec->config) != 0)
- return NULL;
- self->pendingsize = 0;
+ if (self->codec->decreset != NULL &&
+ self->codec->decreset(&self->state, self->codec->config) != 0)
+ return NULL;
+ self->pendingsize = 0;
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static struct PyMethodDef mbidecoder_methods[] = {
- {"decode", (PyCFunction)mbidecoder_decode,
- METH_VARARGS | METH_KEYWORDS, NULL},
- {"reset", (PyCFunction)mbidecoder_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ {"decode", (PyCFunction)mbidecoder_decode,
+ METH_VARARGS | METH_KEYWORDS, NULL},
+ {"reset", (PyCFunction)mbidecoder_reset,
+ METH_NOARGS, NULL},
+ {NULL, NULL},
};
static PyObject *
mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- MultibyteIncrementalDecoderObject *self;
- PyObject *codec = NULL;
- char *errors = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalDecoder",
- incnewkwarglist, &errors))
- return NULL;
-
- self = (MultibyteIncrementalDecoderObject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- codec = PyObject_GetAttrString((PyObject *)type, "codec");
- if (codec == NULL)
- goto errorexit;
- if (!MultibyteCodec_Check(codec)) {
- PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
- goto errorexit;
- }
-
- self->codec = ((MultibyteCodecObject *)codec)->codec;
- self->pendingsize = 0;
- self->errors = internal_error_callback(errors);
- if (self->errors == NULL)
- goto errorexit;
- if (self->codec->decinit != NULL &&
- self->codec->decinit(&self->state, self->codec->config) != 0)
- goto errorexit;
-
- Py_DECREF(codec);
- return (PyObject *)self;
+ MultibyteIncrementalDecoderObject *self;
+ PyObject *codec = NULL;
+ char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalDecoder",
+ incnewkwarglist, &errors))
+ return NULL;
+
+ self = (MultibyteIncrementalDecoderObject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ codec = PyObject_GetAttrString((PyObject *)type, "codec");
+ if (codec == NULL)
+ goto errorexit;
+ if (!MultibyteCodec_Check(codec)) {
+ PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
+ goto errorexit;
+ }
+
+ self->codec = ((MultibyteCodecObject *)codec)->codec;
+ self->pendingsize = 0;
+ self->errors = internal_error_callback(errors);
+ if (self->errors == NULL)
+ goto errorexit;
+ if (self->codec->decinit != NULL &&
+ self->codec->decinit(&self->state, self->codec->config) != 0)
+ goto errorexit;
+
+ Py_DECREF(codec);
+ return (PyObject *)self;
errorexit:
- Py_XDECREF(self);
- Py_XDECREF(codec);
- return NULL;
+ Py_XDECREF(self);
+ Py_XDECREF(codec);
+ return NULL;
}
static int
mbidecoder_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- return 0;
+ return 0;
}
static int
mbidecoder_traverse(MultibyteIncrementalDecoderObject *self,
- visitproc visit, void *arg)
+ visitproc visit, void *arg)
{
- if (ERROR_ISCUSTOM(self->errors))
- Py_VISIT(self->errors);
- return 0;
+ if (ERROR_ISCUSTOM(self->errors))
+ Py_VISIT(self->errors);
+ return 0;
}
static void
mbidecoder_dealloc(MultibyteIncrementalDecoderObject *self)
{
- PyObject_GC_UnTrack(self);
- ERROR_DECREF(self->errors);
- Py_TYPE(self)->tp_free(self);
+ PyObject_GC_UnTrack(self);
+ ERROR_DECREF(self->errors);
+ Py_TYPE(self)->tp_free(self);
}
static PyTypeObject MultibyteIncrementalDecoder_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "MultibyteIncrementalDecoder", /* tp_name */
- sizeof(MultibyteIncrementalDecoderObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)mbidecoder_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
- | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- (traverseproc)mbidecoder_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iterext */
- mbidecoder_methods, /* tp_methods */
- 0, /* tp_members */
- codecctx_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- mbidecoder_init, /* tp_init */
- 0, /* tp_alloc */
- mbidecoder_new, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MultibyteIncrementalDecoder", /* tp_name */
+ sizeof(MultibyteIncrementalDecoderObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)mbidecoder_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
+ | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)mbidecoder_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iterext */
+ mbidecoder_methods, /* tp_methods */
+ 0, /* tp_members */
+ codecctx_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ mbidecoder_init, /* tp_init */
+ 0, /* tp_alloc */
+ mbidecoder_new, /* tp_new */
};
@@ -1239,327 +1239,327 @@ static PyTypeObject MultibyteIncrementalDecoder_Type = {
static PyObject *
mbstreamreader_iread(MultibyteStreamReaderObject *self,
- const char *method, Py_ssize_t sizehint)
+ const char *method, Py_ssize_t sizehint)
{
- MultibyteDecodeBuffer buf;
- PyObject *cres;
- Py_ssize_t rsize, finalsize = 0;
-
- if (sizehint == 0)
- return PyUnicode_FromUnicode(NULL, 0);
-
- buf.outobj = buf.excobj = NULL;
- cres = NULL;
-
- for (;;) {
- int endoffile;
-
- if (sizehint < 0)
- cres = PyObject_CallMethod(self->stream,
- (char *)method, NULL);
- else
- cres = PyObject_CallMethod(self->stream,
- (char *)method, "i", sizehint);
- if (cres == NULL)
- goto errorexit;
-
- if (!PyBytes_Check(cres)) {
- PyErr_Format(PyExc_TypeError,
- "stream function returned a "
- "non-bytes object (%.100s)",
- cres->ob_type->tp_name);
- goto errorexit;
- }
-
- endoffile = (PyBytes_GET_SIZE(cres) == 0);
-
- if (self->pendingsize > 0) {
- PyObject *ctr;
- char *ctrdata;
-
- if (PyBytes_GET_SIZE(cres) > PY_SSIZE_T_MAX - self->pendingsize) {
- PyErr_NoMemory();
- goto errorexit;
- }
- rsize = PyBytes_GET_SIZE(cres) + self->pendingsize;
- ctr = PyBytes_FromStringAndSize(NULL, rsize);
- if (ctr == NULL)
- goto errorexit;
- ctrdata = PyBytes_AS_STRING(ctr);
- memcpy(ctrdata, self->pending, self->pendingsize);
- memcpy(ctrdata + self->pendingsize,
- PyBytes_AS_STRING(cres),
- PyBytes_GET_SIZE(cres));
- Py_DECREF(cres);
- cres = ctr;
- self->pendingsize = 0;
- }
-
- rsize = PyBytes_GET_SIZE(cres);
- if (decoder_prepare_buffer(&buf, PyBytes_AS_STRING(cres),
- rsize) != 0)
- goto errorexit;
-
- if (rsize > 0 && decoder_feed_buffer(
- (MultibyteStatefulDecoderContext *)self, &buf))
- goto errorexit;
-
- if (endoffile || sizehint < 0) {
- if (buf.inbuf < buf.inbuf_end &&
- multibytecodec_decerror(self->codec, &self->state,
- &buf, self->errors, MBERR_TOOFEW))
- goto errorexit;
- }
-
- if (buf.inbuf < buf.inbuf_end) { /* pending sequence exists */
- if (decoder_append_pending(STATEFUL_DCTX(self),
- &buf) != 0)
- goto errorexit;
- }
-
- finalsize = (Py_ssize_t)(buf.outbuf -
- PyUnicode_AS_UNICODE(buf.outobj));
- Py_DECREF(cres);
- cres = NULL;
-
- if (sizehint < 0 || finalsize != 0 || rsize == 0)
- break;
-
- sizehint = 1; /* read 1 more byte and retry */
- }
-
- if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
- if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
- goto errorexit;
-
- Py_XDECREF(cres);
- Py_XDECREF(buf.excobj);
- return buf.outobj;
+ MultibyteDecodeBuffer buf;
+ PyObject *cres;
+ Py_ssize_t rsize, finalsize = 0;
+
+ if (sizehint == 0)
+ return PyUnicode_FromUnicode(NULL, 0);
+
+ buf.outobj = buf.excobj = NULL;
+ cres = NULL;
+
+ for (;;) {
+ int endoffile;
+
+ if (sizehint < 0)
+ cres = PyObject_CallMethod(self->stream,
+ (char *)method, NULL);
+ else
+ cres = PyObject_CallMethod(self->stream,
+ (char *)method, "i", sizehint);
+ if (cres == NULL)
+ goto errorexit;
+
+ if (!PyBytes_Check(cres)) {
+ PyErr_Format(PyExc_TypeError,
+ "stream function returned a "
+ "non-bytes object (%.100s)",
+ cres->ob_type->tp_name);
+ goto errorexit;
+ }
+
+ endoffile = (PyBytes_GET_SIZE(cres) == 0);
+
+ if (self->pendingsize > 0) {
+ PyObject *ctr;
+ char *ctrdata;
+
+ if (PyBytes_GET_SIZE(cres) > PY_SSIZE_T_MAX - self->pendingsize) {
+ PyErr_NoMemory();
+ goto errorexit;
+ }
+ rsize = PyBytes_GET_SIZE(cres) + self->pendingsize;
+ ctr = PyBytes_FromStringAndSize(NULL, rsize);
+ if (ctr == NULL)
+ goto errorexit;
+ ctrdata = PyBytes_AS_STRING(ctr);
+ memcpy(ctrdata, self->pending, self->pendingsize);
+ memcpy(ctrdata + self->pendingsize,
+ PyBytes_AS_STRING(cres),
+ PyBytes_GET_SIZE(cres));
+ Py_DECREF(cres);
+ cres = ctr;
+ self->pendingsize = 0;
+ }
+
+ rsize = PyBytes_GET_SIZE(cres);
+ if (decoder_prepare_buffer(&buf, PyBytes_AS_STRING(cres),
+ rsize) != 0)
+ goto errorexit;
+
+ if (rsize > 0 && decoder_feed_buffer(
+ (MultibyteStatefulDecoderContext *)self, &buf))
+ goto errorexit;
+
+ if (endoffile || sizehint < 0) {
+ if (buf.inbuf < buf.inbuf_end &&
+ multibytecodec_decerror(self->codec, &self->state,
+ &buf, self->errors, MBERR_TOOFEW))
+ goto errorexit;
+ }
+
+ if (buf.inbuf < buf.inbuf_end) { /* pending sequence exists */
+ if (decoder_append_pending(STATEFUL_DCTX(self),
+ &buf) != 0)
+ goto errorexit;
+ }
+
+ finalsize = (Py_ssize_t)(buf.outbuf -
+ PyUnicode_AS_UNICODE(buf.outobj));
+ Py_DECREF(cres);
+ cres = NULL;
+
+ if (sizehint < 0 || finalsize != 0 || rsize == 0)
+ break;
+
+ sizehint = 1; /* read 1 more byte and retry */
+ }
+
+ if (finalsize != PyUnicode_GET_SIZE(buf.outobj))
+ if (PyUnicode_Resize(&buf.outobj, finalsize) == -1)
+ goto errorexit;
+
+ Py_XDECREF(cres);
+ Py_XDECREF(buf.excobj);
+ return buf.outobj;
errorexit:
- Py_XDECREF(cres);
- Py_XDECREF(buf.excobj);
- Py_XDECREF(buf.outobj);
- return NULL;
+ Py_XDECREF(cres);
+ Py_XDECREF(buf.excobj);
+ Py_XDECREF(buf.outobj);
+ return NULL;
}
static PyObject *
mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args)
{
- PyObject *sizeobj = NULL;
- Py_ssize_t size;
+ PyObject *sizeobj = NULL;
+ Py_ssize_t size;
- if (!PyArg_UnpackTuple(args, "read", 0, 1, &sizeobj))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "read", 0, 1, &sizeobj))
+ return NULL;
- if (sizeobj == Py_None || sizeobj == NULL)
- size = -1;
- else if (PyLong_Check(sizeobj))
- size = PyLong_AsSsize_t(sizeobj);
- else {
- PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
- return NULL;
- }
+ if (sizeobj == Py_None || sizeobj == NULL)
+ size = -1;
+ else if (PyLong_Check(sizeobj))
+ size = PyLong_AsSsize_t(sizeobj);
+ else {
+ PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
+ return NULL;
+ }
- if (size == -1 && PyErr_Occurred())
- return NULL;
+ if (size == -1 && PyErr_Occurred())
+ return NULL;
- return mbstreamreader_iread(self, "read", size);
+ return mbstreamreader_iread(self, "read", size);
}
static PyObject *
mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args)
{
- PyObject *sizeobj = NULL;
- Py_ssize_t size;
+ PyObject *sizeobj = NULL;
+ Py_ssize_t size;
- if (!PyArg_UnpackTuple(args, "readline", 0, 1, &sizeobj))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "readline", 0, 1, &sizeobj))
+ return NULL;
- if (sizeobj == Py_None || sizeobj == NULL)
- size = -1;
- else if (PyLong_Check(sizeobj))
- size = PyLong_AsSsize_t(sizeobj);
- else {
- PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
- return NULL;
- }
+ if (sizeobj == Py_None || sizeobj == NULL)
+ size = -1;
+ else if (PyLong_Check(sizeobj))
+ size = PyLong_AsSsize_t(sizeobj);
+ else {
+ PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
+ return NULL;
+ }
- if (size == -1 && PyErr_Occurred())
- return NULL;
+ if (size == -1 && PyErr_Occurred())
+ return NULL;
- return mbstreamreader_iread(self, "readline", size);
+ return mbstreamreader_iread(self, "readline", size);
}
static PyObject *
mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args)
{
- PyObject *sizehintobj = NULL, *r, *sr;
- Py_ssize_t sizehint;
-
- if (!PyArg_UnpackTuple(args, "readlines", 0, 1, &sizehintobj))
- return NULL;
-
- if (sizehintobj == Py_None || sizehintobj == NULL)
- sizehint = -1;
- else if (PyLong_Check(sizehintobj))
- sizehint = PyLong_AsSsize_t(sizehintobj);
- else {
- PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
- return NULL;
- }
-
- if (sizehint == -1 && PyErr_Occurred())
- return NULL;
-
- r = mbstreamreader_iread(self, "read", sizehint);
- if (r == NULL)
- return NULL;
-
- sr = PyUnicode_Splitlines(r, 1);
- Py_DECREF(r);
- return sr;
+ PyObject *sizehintobj = NULL, *r, *sr;
+ Py_ssize_t sizehint;
+
+ if (!PyArg_UnpackTuple(args, "readlines", 0, 1, &sizehintobj))
+ return NULL;
+
+ if (sizehintobj == Py_None || sizehintobj == NULL)
+ sizehint = -1;
+ else if (PyLong_Check(sizehintobj))
+ sizehint = PyLong_AsSsize_t(sizehintobj);
+ else {
+ PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer");
+ return NULL;
+ }
+
+ if (sizehint == -1 && PyErr_Occurred())
+ return NULL;
+
+ r = mbstreamreader_iread(self, "read", sizehint);
+ if (r == NULL)
+ return NULL;
+
+ sr = PyUnicode_Splitlines(r, 1);
+ Py_DECREF(r);
+ return sr;
}
static PyObject *
mbstreamreader_reset(MultibyteStreamReaderObject *self)
{
- if (self->codec->decreset != NULL &&
- self->codec->decreset(&self->state, self->codec->config) != 0)
- return NULL;
- self->pendingsize = 0;
+ if (self->codec->decreset != NULL &&
+ self->codec->decreset(&self->state, self->codec->config) != 0)
+ return NULL;
+ self->pendingsize = 0;
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static struct PyMethodDef mbstreamreader_methods[] = {
- {"read", (PyCFunction)mbstreamreader_read,
- METH_VARARGS, NULL},
- {"readline", (PyCFunction)mbstreamreader_readline,
- METH_VARARGS, NULL},
- {"readlines", (PyCFunction)mbstreamreader_readlines,
- METH_VARARGS, NULL},
- {"reset", (PyCFunction)mbstreamreader_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ {"read", (PyCFunction)mbstreamreader_read,
+ METH_VARARGS, NULL},
+ {"readline", (PyCFunction)mbstreamreader_readline,
+ METH_VARARGS, NULL},
+ {"readlines", (PyCFunction)mbstreamreader_readlines,
+ METH_VARARGS, NULL},
+ {"reset", (PyCFunction)mbstreamreader_reset,
+ METH_NOARGS, NULL},
+ {NULL, NULL},
};
static PyMemberDef mbstreamreader_members[] = {
- {"stream", T_OBJECT,
- offsetof(MultibyteStreamReaderObject, stream),
- READONLY, NULL},
- {NULL,}
+ {"stream", T_OBJECT,
+ offsetof(MultibyteStreamReaderObject, stream),
+ READONLY, NULL},
+ {NULL,}
};
static PyObject *
mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- MultibyteStreamReaderObject *self;
- PyObject *stream, *codec = NULL;
- char *errors = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamReader",
- streamkwarglist, &stream, &errors))
- return NULL;
-
- self = (MultibyteStreamReaderObject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- codec = PyObject_GetAttrString((PyObject *)type, "codec");
- if (codec == NULL)
- goto errorexit;
- if (!MultibyteCodec_Check(codec)) {
- PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
- goto errorexit;
- }
-
- self->codec = ((MultibyteCodecObject *)codec)->codec;
- self->stream = stream;
- Py_INCREF(stream);
- self->pendingsize = 0;
- self->errors = internal_error_callback(errors);
- if (self->errors == NULL)
- goto errorexit;
- if (self->codec->decinit != NULL &&
- self->codec->decinit(&self->state, self->codec->config) != 0)
- goto errorexit;
-
- Py_DECREF(codec);
- return (PyObject *)self;
+ MultibyteStreamReaderObject *self;
+ PyObject *stream, *codec = NULL;
+ char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamReader",
+ streamkwarglist, &stream, &errors))
+ return NULL;
+
+ self = (MultibyteStreamReaderObject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ codec = PyObject_GetAttrString((PyObject *)type, "codec");
+ if (codec == NULL)
+ goto errorexit;
+ if (!MultibyteCodec_Check(codec)) {
+ PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
+ goto errorexit;
+ }
+
+ self->codec = ((MultibyteCodecObject *)codec)->codec;
+ self->stream = stream;
+ Py_INCREF(stream);
+ self->pendingsize = 0;
+ self->errors = internal_error_callback(errors);
+ if (self->errors == NULL)
+ goto errorexit;
+ if (self->codec->decinit != NULL &&
+ self->codec->decinit(&self->state, self->codec->config) != 0)
+ goto errorexit;
+
+ Py_DECREF(codec);
+ return (PyObject *)self;
errorexit:
- Py_XDECREF(self);
- Py_XDECREF(codec);
- return NULL;
+ Py_XDECREF(self);
+ Py_XDECREF(codec);
+ return NULL;
}
static int
mbstreamreader_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- return 0;
+ return 0;
}
static int
mbstreamreader_traverse(MultibyteStreamReaderObject *self,
- visitproc visit, void *arg)
+ visitproc visit, void *arg)
{
- if (ERROR_ISCUSTOM(self->errors))
- Py_VISIT(self->errors);
- Py_VISIT(self->stream);
- return 0;
+ if (ERROR_ISCUSTOM(self->errors))
+ Py_VISIT(self->errors);
+ Py_VISIT(self->stream);
+ return 0;
}
static void
mbstreamreader_dealloc(MultibyteStreamReaderObject *self)
{
- PyObject_GC_UnTrack(self);
- ERROR_DECREF(self->errors);
- Py_XDECREF(self->stream);
- Py_TYPE(self)->tp_free(self);
+ PyObject_GC_UnTrack(self);
+ ERROR_DECREF(self->errors);
+ Py_XDECREF(self->stream);
+ Py_TYPE(self)->tp_free(self);
}
static PyTypeObject MultibyteStreamReader_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "MultibyteStreamReader", /* tp_name */
- sizeof(MultibyteStreamReaderObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)mbstreamreader_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
- | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- (traverseproc)mbstreamreader_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iterext */
- mbstreamreader_methods, /* tp_methods */
- mbstreamreader_members, /* tp_members */
- codecctx_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- mbstreamreader_init, /* tp_init */
- 0, /* tp_alloc */
- mbstreamreader_new, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MultibyteStreamReader", /* tp_name */
+ sizeof(MultibyteStreamReaderObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)mbstreamreader_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
+ | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)mbstreamreader_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iterext */
+ mbstreamreader_methods, /* tp_methods */
+ mbstreamreader_members, /* tp_members */
+ codecctx_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ mbstreamreader_init, /* tp_init */
+ 0, /* tp_alloc */
+ mbstreamreader_new, /* tp_new */
};
@@ -1569,217 +1569,217 @@ static PyTypeObject MultibyteStreamReader_Type = {
static int
mbstreamwriter_iwrite(MultibyteStreamWriterObject *self,
- PyObject *unistr)
+ PyObject *unistr)
{
- PyObject *str, *wr;
+ PyObject *str, *wr;
- str = encoder_encode_stateful(STATEFUL_ECTX(self), unistr, 0);
- if (str == NULL)
- return -1;
+ str = encoder_encode_stateful(STATEFUL_ECTX(self), unistr, 0);
+ if (str == NULL)
+ return -1;
- wr = PyObject_CallMethod(self->stream, "write", "O", str);
- Py_DECREF(str);
- if (wr == NULL)
- return -1;
+ wr = PyObject_CallMethod(self->stream, "write", "O", str);
+ Py_DECREF(str);
+ if (wr == NULL)
+ return -1;
- Py_DECREF(wr);
- return 0;
+ Py_DECREF(wr);
+ return 0;
}
static PyObject *
mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *strobj)
{
- if (mbstreamwriter_iwrite(self, strobj))
- return NULL;
- else
- Py_RETURN_NONE;
+ if (mbstreamwriter_iwrite(self, strobj))
+ return NULL;
+ else
+ Py_RETURN_NONE;
}
static PyObject *
mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *lines)
{
- PyObject *strobj;
- int i, r;
-
- if (!PySequence_Check(lines)) {
- PyErr_SetString(PyExc_TypeError,
- "arg must be a sequence object");
- return NULL;
- }
-
- for (i = 0; i < PySequence_Length(lines); i++) {
- /* length can be changed even within this loop */
- strobj = PySequence_GetItem(lines, i);
- if (strobj == NULL)
- return NULL;
-
- r = mbstreamwriter_iwrite(self, strobj);
- Py_DECREF(strobj);
- if (r == -1)
- return NULL;
- }
-
- Py_RETURN_NONE;
+ PyObject *strobj;
+ int i, r;
+
+ if (!PySequence_Check(lines)) {
+ PyErr_SetString(PyExc_TypeError,
+ "arg must be a sequence object");
+ return NULL;
+ }
+
+ for (i = 0; i < PySequence_Length(lines); i++) {
+ /* length can be changed even within this loop */
+ strobj = PySequence_GetItem(lines, i);
+ if (strobj == NULL)
+ return NULL;
+
+ r = mbstreamwriter_iwrite(self, strobj);
+ Py_DECREF(strobj);
+ if (r == -1)
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
}
static PyObject *
mbstreamwriter_reset(MultibyteStreamWriterObject *self)
{
- const Py_UNICODE *pending;
- PyObject *pwrt;
-
- pending = self->pending;
- pwrt = multibytecodec_encode(self->codec, &self->state,
- &pending, self->pendingsize, self->errors,
- MBENC_FLUSH | MBENC_RESET);
- /* some pending buffer can be truncated when UnicodeEncodeError is
- * raised on 'strict' mode. but, 'reset' method is designed to
- * reset the pending buffer or states so failed string sequence
- * ought to be missed */
- self->pendingsize = 0;
- if (pwrt == NULL)
- return NULL;
-
- assert(PyBytes_Check(pwrt));
- if (PyBytes_Size(pwrt) > 0) {
- PyObject *wr;
- wr = PyObject_CallMethod(self->stream, "write", "O", pwrt);
- if (wr == NULL) {
- Py_DECREF(pwrt);
- return NULL;
- }
- }
- Py_DECREF(pwrt);
-
- Py_RETURN_NONE;
+ const Py_UNICODE *pending;
+ PyObject *pwrt;
+
+ pending = self->pending;
+ pwrt = multibytecodec_encode(self->codec, &self->state,
+ &pending, self->pendingsize, self->errors,
+ MBENC_FLUSH | MBENC_RESET);
+ /* some pending buffer can be truncated when UnicodeEncodeError is
+ * raised on 'strict' mode. but, 'reset' method is designed to
+ * reset the pending buffer or states so failed string sequence
+ * ought to be missed */
+ self->pendingsize = 0;
+ if (pwrt == NULL)
+ return NULL;
+
+ assert(PyBytes_Check(pwrt));
+ if (PyBytes_Size(pwrt) > 0) {
+ PyObject *wr;
+ wr = PyObject_CallMethod(self->stream, "write", "O", pwrt);
+ if (wr == NULL) {
+ Py_DECREF(pwrt);
+ return NULL;
+ }
+ }
+ Py_DECREF(pwrt);
+
+ Py_RETURN_NONE;
}
static PyObject *
mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- MultibyteStreamWriterObject *self;
- PyObject *stream, *codec = NULL;
- char *errors = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamWriter",
- streamkwarglist, &stream, &errors))
- return NULL;
-
- self = (MultibyteStreamWriterObject *)type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- codec = PyObject_GetAttrString((PyObject *)type, "codec");
- if (codec == NULL)
- goto errorexit;
- if (!MultibyteCodec_Check(codec)) {
- PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
- goto errorexit;
- }
-
- self->codec = ((MultibyteCodecObject *)codec)->codec;
- self->stream = stream;
- Py_INCREF(stream);
- self->pendingsize = 0;
- self->errors = internal_error_callback(errors);
- if (self->errors == NULL)
- goto errorexit;
- if (self->codec->encinit != NULL &&
- self->codec->encinit(&self->state, self->codec->config) != 0)
- goto errorexit;
-
- Py_DECREF(codec);
- return (PyObject *)self;
+ MultibyteStreamWriterObject *self;
+ PyObject *stream, *codec = NULL;
+ char *errors = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamWriter",
+ streamkwarglist, &stream, &errors))
+ return NULL;
+
+ self = (MultibyteStreamWriterObject *)type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ codec = PyObject_GetAttrString((PyObject *)type, "codec");
+ if (codec == NULL)
+ goto errorexit;
+ if (!MultibyteCodec_Check(codec)) {
+ PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
+ goto errorexit;
+ }
+
+ self->codec = ((MultibyteCodecObject *)codec)->codec;
+ self->stream = stream;
+ Py_INCREF(stream);
+ self->pendingsize = 0;
+ self->errors = internal_error_callback(errors);
+ if (self->errors == NULL)
+ goto errorexit;
+ if (self->codec->encinit != NULL &&
+ self->codec->encinit(&self->state, self->codec->config) != 0)
+ goto errorexit;
+
+ Py_DECREF(codec);
+ return (PyObject *)self;
errorexit:
- Py_XDECREF(self);
- Py_XDECREF(codec);
- return NULL;
+ Py_XDECREF(self);
+ Py_XDECREF(codec);
+ return NULL;
}
static int
mbstreamwriter_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- return 0;
+ return 0;
}
static int
mbstreamwriter_traverse(MultibyteStreamWriterObject *self,
- visitproc visit, void *arg)
+ visitproc visit, void *arg)
{
- if (ERROR_ISCUSTOM(self->errors))
- Py_VISIT(self->errors);
- Py_VISIT(self->stream);
- return 0;
+ if (ERROR_ISCUSTOM(self->errors))
+ Py_VISIT(self->errors);
+ Py_VISIT(self->stream);
+ return 0;
}
static void
mbstreamwriter_dealloc(MultibyteStreamWriterObject *self)
{
- PyObject_GC_UnTrack(self);
- ERROR_DECREF(self->errors);
- Py_XDECREF(self->stream);
- Py_TYPE(self)->tp_free(self);
+ PyObject_GC_UnTrack(self);
+ ERROR_DECREF(self->errors);
+ Py_XDECREF(self->stream);
+ Py_TYPE(self)->tp_free(self);
}
static struct PyMethodDef mbstreamwriter_methods[] = {
- {"write", (PyCFunction)mbstreamwriter_write,
- METH_O, NULL},
- {"writelines", (PyCFunction)mbstreamwriter_writelines,
- METH_O, NULL},
- {"reset", (PyCFunction)mbstreamwriter_reset,
- METH_NOARGS, NULL},
- {NULL, NULL},
+ {"write", (PyCFunction)mbstreamwriter_write,
+ METH_O, NULL},
+ {"writelines", (PyCFunction)mbstreamwriter_writelines,
+ METH_O, NULL},
+ {"reset", (PyCFunction)mbstreamwriter_reset,
+ METH_NOARGS, NULL},
+ {NULL, NULL},
};
static PyMemberDef mbstreamwriter_members[] = {
- {"stream", T_OBJECT,
- offsetof(MultibyteStreamWriterObject, stream),
- READONLY, NULL},
- {NULL,}
+ {"stream", T_OBJECT,
+ offsetof(MultibyteStreamWriterObject, stream),
+ READONLY, NULL},
+ {NULL,}
};
static PyTypeObject MultibyteStreamWriter_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "MultibyteStreamWriter", /* tp_name */
- sizeof(MultibyteStreamWriterObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)mbstreamwriter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
- | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- (traverseproc)mbstreamwriter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iterext */
- mbstreamwriter_methods, /* tp_methods */
- mbstreamwriter_members, /* tp_members */
- codecctx_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- mbstreamwriter_init, /* tp_init */
- 0, /* tp_alloc */
- mbstreamwriter_new, /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "MultibyteStreamWriter", /* tp_name */
+ sizeof(MultibyteStreamWriterObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)mbstreamwriter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC
+ | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)mbstreamwriter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iterext */
+ mbstreamwriter_methods, /* tp_methods */
+ mbstreamwriter_members, /* tp_members */
+ codecctx_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ mbstreamwriter_init, /* tp_init */
+ 0, /* tp_alloc */
+ mbstreamwriter_new, /* tp_new */
};
@@ -1790,76 +1790,76 @@ static PyTypeObject MultibyteStreamWriter_Type = {
static PyObject *
__create_codec(PyObject *ignore, PyObject *arg)
{
- MultibyteCodecObject *self;
- MultibyteCodec *codec;
+ MultibyteCodecObject *self;
+ MultibyteCodec *codec;
- if (!PyCapsule_IsValid(arg, PyMultibyteCodec_CAPSULE_NAME)) {
- PyErr_SetString(PyExc_ValueError, "argument type invalid");
- return NULL;
- }
+ if (!PyCapsule_IsValid(arg, PyMultibyteCodec_CAPSULE_NAME)) {
+ PyErr_SetString(PyExc_ValueError, "argument type invalid");
+ return NULL;
+ }
- codec = PyCapsule_GetPointer(arg, PyMultibyteCodec_CAPSULE_NAME);
- if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0)
- return NULL;
+ codec = PyCapsule_GetPointer(arg, PyMultibyteCodec_CAPSULE_NAME);
+ if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0)
+ return NULL;
- self = PyObject_New(MultibyteCodecObject, &MultibyteCodec_Type);
- if (self == NULL)
- return NULL;
- self->codec = codec;
+ self = PyObject_New(MultibyteCodecObject, &MultibyteCodec_Type);
+ if (self == NULL)
+ return NULL;
+ self->codec = codec;
- return (PyObject *)self;
+ return (PyObject *)self;
}
static struct PyMethodDef __methods[] = {
- {"__create_codec", (PyCFunction)__create_codec, METH_O},
- {NULL, NULL},
+ {"__create_codec", (PyCFunction)__create_codec, METH_O},
+ {NULL, NULL},
};
static struct PyModuleDef _multibytecodecmodule = {
- PyModuleDef_HEAD_INIT,
- "_multibytecodec",
- NULL,
- -1,
- __methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_multibytecodec",
+ NULL,
+ -1,
+ __methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__multibytecodec(void)
{
- int i;
- PyObject *m;
- PyTypeObject *typelist[] = {
- &MultibyteIncrementalEncoder_Type,
- &MultibyteIncrementalDecoder_Type,
- &MultibyteStreamReader_Type,
- &MultibyteStreamWriter_Type,
- NULL
- };
-
- if (PyType_Ready(&MultibyteCodec_Type) < 0)
- return NULL;
-
- m = PyModule_Create(&_multibytecodecmodule);
- if (m == NULL)
- return NULL;
-
- for (i = 0; typelist[i] != NULL; i++) {
- if (PyType_Ready(typelist[i]) < 0)
- return NULL;
- Py_INCREF(typelist[i]);
- PyModule_AddObject(m, typelist[i]->tp_name,
- (PyObject *)typelist[i]);
- }
-
- if (PyErr_Occurred()) {
- Py_FatalError("can't initialize the _multibytecodec module");
- Py_DECREF(m);
- m = NULL;
- }
- return m;
+ int i;
+ PyObject *m;
+ PyTypeObject *typelist[] = {
+ &MultibyteIncrementalEncoder_Type,
+ &MultibyteIncrementalDecoder_Type,
+ &MultibyteStreamReader_Type,
+ &MultibyteStreamWriter_Type,
+ NULL
+ };
+
+ if (PyType_Ready(&MultibyteCodec_Type) < 0)
+ return NULL;
+
+ m = PyModule_Create(&_multibytecodecmodule);
+ if (m == NULL)
+ return NULL;
+
+ for (i = 0; typelist[i] != NULL; i++) {
+ if (PyType_Ready(typelist[i]) < 0)
+ return NULL;
+ Py_INCREF(typelist[i]);
+ PyModule_AddObject(m, typelist[i]->tp_name,
+ (PyObject *)typelist[i]);
+ }
+
+ if (PyErr_Occurred()) {
+ Py_FatalError("can't initialize the _multibytecodec module");
+ Py_DECREF(m);
+ m = NULL;
+ }
+ return m;
}
diff --git a/Modules/cjkcodecs/multibytecodec.h b/Modules/cjkcodecs/multibytecodec.h
index 71c02cc..1b6ef55 100644
--- a/Modules/cjkcodecs/multibytecodec.h
+++ b/Modules/cjkcodecs/multibytecodec.h
@@ -23,114 +23,114 @@ typedef unsigned short ucs2_t, DBCHAR;
#endif
typedef union {
- void *p;
- int i;
- unsigned char c[8];
- ucs2_t u2[4];
- ucs4_t u4[2];
+ void *p;
+ int i;
+ unsigned char c[8];
+ ucs2_t u2[4];
+ ucs4_t u4[2];
} MultibyteCodec_State;
typedef int (*mbcodec_init)(const void *config);
typedef Py_ssize_t (*mbencode_func)(MultibyteCodec_State *state,
- const void *config,
- const Py_UNICODE **inbuf, Py_ssize_t inleft,
- unsigned char **outbuf, Py_ssize_t outleft,
- int flags);
+ const void *config,
+ const Py_UNICODE **inbuf, Py_ssize_t inleft,
+ unsigned char **outbuf, Py_ssize_t outleft,
+ int flags);
typedef int (*mbencodeinit_func)(MultibyteCodec_State *state,
- const void *config);
+ const void *config);
typedef Py_ssize_t (*mbencodereset_func)(MultibyteCodec_State *state,
- const void *config,
- unsigned char **outbuf, Py_ssize_t outleft);
+ const void *config,
+ unsigned char **outbuf, Py_ssize_t outleft);
typedef Py_ssize_t (*mbdecode_func)(MultibyteCodec_State *state,
- const void *config,
- const unsigned char **inbuf, Py_ssize_t inleft,
- Py_UNICODE **outbuf, Py_ssize_t outleft);
+ const void *config,
+ const unsigned char **inbuf, Py_ssize_t inleft,
+ Py_UNICODE **outbuf, Py_ssize_t outleft);
typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state,
- const void *config);
+ const void *config);
typedef Py_ssize_t (*mbdecodereset_func)(MultibyteCodec_State *state,
- const void *config);
+ const void *config);
typedef struct {
- const char *encoding;
- const void *config;
- mbcodec_init codecinit;
- mbencode_func encode;
- mbencodeinit_func encinit;
- mbencodereset_func encreset;
- mbdecode_func decode;
- mbdecodeinit_func decinit;
- mbdecodereset_func decreset;
+ const char *encoding;
+ const void *config;
+ mbcodec_init codecinit;
+ mbencode_func encode;
+ mbencodeinit_func encinit;
+ mbencodereset_func encreset;
+ mbdecode_func decode;
+ mbdecodeinit_func decinit;
+ mbdecodereset_func decreset;
} MultibyteCodec;
typedef struct {
- PyObject_HEAD
- MultibyteCodec *codec;
+ PyObject_HEAD
+ MultibyteCodec *codec;
} MultibyteCodecObject;
#define MultibyteCodec_Check(op) ((op)->ob_type == &MultibyteCodec_Type)
-#define _MultibyteStatefulCodec_HEAD \
- PyObject_HEAD \
- MultibyteCodec *codec; \
- MultibyteCodec_State state; \
- PyObject *errors;
+#define _MultibyteStatefulCodec_HEAD \
+ PyObject_HEAD \
+ MultibyteCodec *codec; \
+ MultibyteCodec_State state; \
+ PyObject *errors;
typedef struct {
- _MultibyteStatefulCodec_HEAD
+ _MultibyteStatefulCodec_HEAD
} MultibyteStatefulCodecContext;
-#define MAXENCPENDING 2
-#define _MultibyteStatefulEncoder_HEAD \
- _MultibyteStatefulCodec_HEAD \
- Py_UNICODE pending[MAXENCPENDING]; \
- Py_ssize_t pendingsize;
+#define MAXENCPENDING 2
+#define _MultibyteStatefulEncoder_HEAD \
+ _MultibyteStatefulCodec_HEAD \
+ Py_UNICODE pending[MAXENCPENDING]; \
+ Py_ssize_t pendingsize;
typedef struct {
- _MultibyteStatefulEncoder_HEAD
+ _MultibyteStatefulEncoder_HEAD
} MultibyteStatefulEncoderContext;
-#define MAXDECPENDING 8
-#define _MultibyteStatefulDecoder_HEAD \
- _MultibyteStatefulCodec_HEAD \
- unsigned char pending[MAXDECPENDING]; \
- Py_ssize_t pendingsize;
+#define MAXDECPENDING 8
+#define _MultibyteStatefulDecoder_HEAD \
+ _MultibyteStatefulCodec_HEAD \
+ unsigned char pending[MAXDECPENDING]; \
+ Py_ssize_t pendingsize;
typedef struct {
- _MultibyteStatefulDecoder_HEAD
+ _MultibyteStatefulDecoder_HEAD
} MultibyteStatefulDecoderContext;
typedef struct {
- _MultibyteStatefulEncoder_HEAD
+ _MultibyteStatefulEncoder_HEAD
} MultibyteIncrementalEncoderObject;
typedef struct {
- _MultibyteStatefulDecoder_HEAD
+ _MultibyteStatefulDecoder_HEAD
} MultibyteIncrementalDecoderObject;
typedef struct {
- _MultibyteStatefulDecoder_HEAD
- PyObject *stream;
+ _MultibyteStatefulDecoder_HEAD
+ PyObject *stream;
} MultibyteStreamReaderObject;
typedef struct {
- _MultibyteStatefulEncoder_HEAD
- PyObject *stream;
+ _MultibyteStatefulEncoder_HEAD
+ PyObject *stream;
} MultibyteStreamWriterObject;
/* positive values for illegal sequences */
-#define MBERR_TOOSMALL (-1) /* insufficient output buffer space */
-#define MBERR_TOOFEW (-2) /* incomplete input buffer */
-#define MBERR_INTERNAL (-3) /* internal runtime error */
-
-#define ERROR_STRICT (PyObject *)(1)
-#define ERROR_IGNORE (PyObject *)(2)
-#define ERROR_REPLACE (PyObject *)(3)
-#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
-#define ERROR_DECREF(p) do { \
- if (p != NULL && ERROR_ISCUSTOM(p)) { \
- Py_DECREF(p); \
- } \
+#define MBERR_TOOSMALL (-1) /* insufficient output buffer space */
+#define MBERR_TOOFEW (-2) /* incomplete input buffer */
+#define MBERR_INTERNAL (-3) /* internal runtime error */
+
+#define ERROR_STRICT (PyObject *)(1)
+#define ERROR_IGNORE (PyObject *)(2)
+#define ERROR_REPLACE (PyObject *)(3)
+#define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
+#define ERROR_DECREF(p) do { \
+ if (p != NULL && ERROR_ISCUSTOM(p)) { \
+ Py_DECREF(p); \
+ } \
} while (0);
-#define MBENC_FLUSH 0x0001 /* encode all characters encodable */
-#define MBENC_MAX MBENC_FLUSH
+#define MBENC_FLUSH 0x0001 /* encode all characters encodable */
+#define MBENC_MAX MBENC_FLUSH
#define PyMultibyteCodec_CAPSULE_NAME "multibytecodec.__map_*"
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index fbf6ece..545e834 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -31,7 +31,7 @@
#define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE))
#define CM_SQRT_DBL_MIN (sqrt(DBL_MIN))
-/*
+/*
CM_SCALE_UP is an odd integer chosen such that multiplication by
2**CM_SCALE_UP is sufficient to turn a subnormal into a normal.
CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute
@@ -62,46 +62,46 @@ static PyObject * math_error(void);
*/
enum special_types {
- ST_NINF, /* 0, negative infinity */
- ST_NEG, /* 1, negative finite number (nonzero) */
- ST_NZERO, /* 2, -0. */
- ST_PZERO, /* 3, +0. */
- ST_POS, /* 4, positive finite number (nonzero) */
- ST_PINF, /* 5, positive infinity */
- ST_NAN /* 6, Not a Number */
+ ST_NINF, /* 0, negative infinity */
+ ST_NEG, /* 1, negative finite number (nonzero) */
+ ST_NZERO, /* 2, -0. */
+ ST_PZERO, /* 3, +0. */
+ ST_POS, /* 4, positive finite number (nonzero) */
+ ST_PINF, /* 5, positive infinity */
+ ST_NAN /* 6, Not a Number */
};
static enum special_types
special_type(double d)
{
- if (Py_IS_FINITE(d)) {
- if (d != 0) {
- if (copysign(1., d) == 1.)
- return ST_POS;
- else
- return ST_NEG;
- }
- else {
- if (copysign(1., d) == 1.)
- return ST_PZERO;
- else
- return ST_NZERO;
- }
- }
- if (Py_IS_NAN(d))
- return ST_NAN;
- if (copysign(1., d) == 1.)
- return ST_PINF;
- else
- return ST_NINF;
+ if (Py_IS_FINITE(d)) {
+ if (d != 0) {
+ if (copysign(1., d) == 1.)
+ return ST_POS;
+ else
+ return ST_NEG;
+ }
+ else {
+ if (copysign(1., d) == 1.)
+ return ST_PZERO;
+ else
+ return ST_NZERO;
+ }
+ }
+ if (Py_IS_NAN(d))
+ return ST_NAN;
+ if (copysign(1., d) == 1.)
+ return ST_PINF;
+ else
+ return ST_NINF;
}
-#define SPECIAL_VALUE(z, table) \
- if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
- errno = 0; \
- return table[special_type((z).real)] \
- [special_type((z).imag)]; \
- }
+#define SPECIAL_VALUE(z, table) \
+ if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
+ errno = 0; \
+ return table[special_type((z).real)] \
+ [special_type((z).imag)]; \
+ }
#define P Py_MATH_PI
#define P14 0.25*Py_MATH_PI
@@ -125,34 +125,34 @@ static Py_complex acos_special_values[7][7];
static Py_complex
c_acos(Py_complex z)
{
- Py_complex s1, s2, r;
-
- SPECIAL_VALUE(z, acos_special_values);
-
- if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
- /* avoid unnecessary overflow for large arguments */
- r.real = atan2(fabs(z.imag), z.real);
- /* split into cases to make sure that the branch cut has the
- correct continuity on systems with unsigned zeros */
- if (z.real < 0.) {
- r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
- M_LN2*2., z.imag);
- } else {
- r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
- M_LN2*2., -z.imag);
- }
- } else {
- s1.real = 1.-z.real;
- s1.imag = -z.imag;
- s1 = c_sqrt(s1);
- s2.real = 1.+z.real;
- s2.imag = z.imag;
- s2 = c_sqrt(s2);
- r.real = 2.*atan2(s1.real, s2.real);
- r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real);
- }
- errno = 0;
- return r;
+ Py_complex s1, s2, r;
+
+ SPECIAL_VALUE(z, acos_special_values);
+
+ if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
+ /* avoid unnecessary overflow for large arguments */
+ r.real = atan2(fabs(z.imag), z.real);
+ /* split into cases to make sure that the branch cut has the
+ correct continuity on systems with unsigned zeros */
+ if (z.real < 0.) {
+ r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) +
+ M_LN2*2., z.imag);
+ } else {
+ r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) +
+ M_LN2*2., -z.imag);
+ }
+ } else {
+ s1.real = 1.-z.real;
+ s1.imag = -z.imag;
+ s1 = c_sqrt(s1);
+ s2.real = 1.+z.real;
+ s2.imag = z.imag;
+ s2 = c_sqrt(s2);
+ r.real = 2.*atan2(s1.real, s2.real);
+ r.imag = asinh(s2.real*s1.imag - s2.imag*s1.real);
+ }
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_acos_doc,
@@ -166,26 +166,26 @@ static Py_complex acosh_special_values[7][7];
static Py_complex
c_acosh(Py_complex z)
{
- Py_complex s1, s2, r;
-
- SPECIAL_VALUE(z, acosh_special_values);
-
- if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
- /* avoid unnecessary overflow for large arguments */
- r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
- r.imag = atan2(z.imag, z.real);
- } else {
- s1.real = z.real - 1.;
- s1.imag = z.imag;
- s1 = c_sqrt(s1);
- s2.real = z.real + 1.;
- s2.imag = z.imag;
- s2 = c_sqrt(s2);
- r.real = asinh(s1.real*s2.real + s1.imag*s2.imag);
- r.imag = 2.*atan2(s1.imag, s2.real);
- }
- errno = 0;
- return r;
+ Py_complex s1, s2, r;
+
+ SPECIAL_VALUE(z, acosh_special_values);
+
+ if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
+ /* avoid unnecessary overflow for large arguments */
+ r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.;
+ r.imag = atan2(z.imag, z.real);
+ } else {
+ s1.real = z.real - 1.;
+ s1.imag = z.imag;
+ s1 = c_sqrt(s1);
+ s2.real = z.real + 1.;
+ s2.imag = z.imag;
+ s2 = c_sqrt(s2);
+ r.real = asinh(s1.real*s2.real + s1.imag*s2.imag);
+ r.imag = 2.*atan2(s1.imag, s2.real);
+ }
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_acosh_doc,
@@ -197,14 +197,14 @@ PyDoc_STRVAR(c_acosh_doc,
static Py_complex
c_asin(Py_complex z)
{
- /* asin(z) = -i asinh(iz) */
- Py_complex s, r;
- s.real = -z.imag;
- s.imag = z.real;
- s = c_asinh(s);
- r.real = s.imag;
- r.imag = -s.real;
- return r;
+ /* asin(z) = -i asinh(iz) */
+ Py_complex s, r;
+ s.real = -z.imag;
+ s.imag = z.real;
+ s = c_asinh(s);
+ r.real = s.imag;
+ r.imag = -s.real;
+ return r;
}
PyDoc_STRVAR(c_asin_doc,
@@ -218,31 +218,31 @@ static Py_complex asinh_special_values[7][7];
static Py_complex
c_asinh(Py_complex z)
{
- Py_complex s1, s2, r;
-
- SPECIAL_VALUE(z, asinh_special_values);
-
- if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
- if (z.imag >= 0.) {
- r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
- M_LN2*2., z.real);
- } else {
- r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
- M_LN2*2., -z.real);
- }
- r.imag = atan2(z.imag, fabs(z.real));
- } else {
- s1.real = 1.+z.imag;
- s1.imag = -z.real;
- s1 = c_sqrt(s1);
- s2.real = 1.-z.imag;
- s2.imag = z.real;
- s2 = c_sqrt(s2);
- r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
- r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
- }
- errno = 0;
- return r;
+ Py_complex s1, s2, r;
+
+ SPECIAL_VALUE(z, asinh_special_values);
+
+ if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
+ if (z.imag >= 0.) {
+ r.real = copysign(log(hypot(z.real/2., z.imag/2.)) +
+ M_LN2*2., z.real);
+ } else {
+ r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
+ M_LN2*2., -z.real);
+ }
+ r.imag = atan2(z.imag, fabs(z.real));
+ } else {
+ s1.real = 1.+z.imag;
+ s1.imag = -z.real;
+ s1 = c_sqrt(s1);
+ s2.real = 1.-z.imag;
+ s2.imag = z.real;
+ s2 = c_sqrt(s2);
+ r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
+ r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
+ }
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_asinh_doc,
@@ -254,14 +254,14 @@ PyDoc_STRVAR(c_asinh_doc,
static Py_complex
c_atan(Py_complex z)
{
- /* atan(z) = -i atanh(iz) */
- Py_complex s, r;
- s.real = -z.imag;
- s.imag = z.real;
- s = c_atanh(s);
- r.real = s.imag;
- r.imag = -s.real;
- return r;
+ /* atan(z) = -i atanh(iz) */
+ Py_complex s, r;
+ s.real = -z.imag;
+ s.imag = z.real;
+ s = c_atanh(s);
+ r.real = s.imag;
+ r.imag = -s.real;
+ return r;
}
/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
@@ -269,29 +269,29 @@ c_atan(Py_complex z)
static double
c_atan2(Py_complex z)
{
- if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
- return Py_NAN;
- if (Py_IS_INFINITY(z.imag)) {
- if (Py_IS_INFINITY(z.real)) {
- if (copysign(1., z.real) == 1.)
- /* atan2(+-inf, +inf) == +-pi/4 */
- return copysign(0.25*Py_MATH_PI, z.imag);
- else
- /* atan2(+-inf, -inf) == +-pi*3/4 */
- return copysign(0.75*Py_MATH_PI, z.imag);
- }
- /* atan2(+-inf, x) == +-pi/2 for finite x */
- return copysign(0.5*Py_MATH_PI, z.imag);
- }
- if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
- if (copysign(1., z.real) == 1.)
- /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
- return copysign(0., z.imag);
- else
- /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
- return copysign(Py_MATH_PI, z.imag);
- }
- return atan2(z.imag, z.real);
+ if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
+ return Py_NAN;
+ if (Py_IS_INFINITY(z.imag)) {
+ if (Py_IS_INFINITY(z.real)) {
+ if (copysign(1., z.real) == 1.)
+ /* atan2(+-inf, +inf) == +-pi/4 */
+ return copysign(0.25*Py_MATH_PI, z.imag);
+ else
+ /* atan2(+-inf, -inf) == +-pi*3/4 */
+ return copysign(0.75*Py_MATH_PI, z.imag);
+ }
+ /* atan2(+-inf, x) == +-pi/2 for finite x */
+ return copysign(0.5*Py_MATH_PI, z.imag);
+ }
+ if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
+ if (copysign(1., z.real) == 1.)
+ /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
+ return copysign(0., z.imag);
+ else
+ /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
+ return copysign(Py_MATH_PI, z.imag);
+ }
+ return atan2(z.imag, z.real);
}
PyDoc_STRVAR(c_atan_doc,
@@ -305,48 +305,48 @@ static Py_complex atanh_special_values[7][7];
static Py_complex
c_atanh(Py_complex z)
{
- Py_complex r;
- double ay, h;
-
- SPECIAL_VALUE(z, atanh_special_values);
-
- /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
- if (z.real < 0.) {
- return c_neg(c_atanh(c_neg(z)));
- }
-
- ay = fabs(z.imag);
- if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
- /*
- if abs(z) is large then we use the approximation
- atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
- of z.imag)
- */
- h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
- r.real = z.real/4./h/h;
- /* the two negations in the next line cancel each other out
- except when working with unsigned zeros: they're there to
- ensure that the branch cut has the correct continuity on
- systems that don't support signed zeros */
- r.imag = -copysign(Py_MATH_PI/2., -z.imag);
- errno = 0;
- } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
- /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
- if (ay == 0.) {
- r.real = INF;
- r.imag = z.imag;
- errno = EDOM;
- } else {
- r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
- r.imag = copysign(atan2(2., -ay)/2, z.imag);
- errno = 0;
- }
- } else {
- r.real = log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
- r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
- errno = 0;
- }
- return r;
+ Py_complex r;
+ double ay, h;
+
+ SPECIAL_VALUE(z, atanh_special_values);
+
+ /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */
+ if (z.real < 0.) {
+ return c_neg(c_atanh(c_neg(z)));
+ }
+
+ ay = fabs(z.imag);
+ if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) {
+ /*
+ if abs(z) is large then we use the approximation
+ atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
+ of z.imag)
+ */
+ h = hypot(z.real/2., z.imag/2.); /* safe from overflow */
+ r.real = z.real/4./h/h;
+ /* the two negations in the next line cancel each other out
+ except when working with unsigned zeros: they're there to
+ ensure that the branch cut has the correct continuity on
+ systems that don't support signed zeros */
+ r.imag = -copysign(Py_MATH_PI/2., -z.imag);
+ errno = 0;
+ } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) {
+ /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */
+ if (ay == 0.) {
+ r.real = INF;
+ r.imag = z.imag;
+ errno = EDOM;
+ } else {
+ r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.)));
+ r.imag = copysign(atan2(2., -ay)/2, z.imag);
+ errno = 0;
+ }
+ } else {
+ r.real = log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.;
+ r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.;
+ errno = 0;
+ }
+ return r;
}
PyDoc_STRVAR(c_atanh_doc,
@@ -358,12 +358,12 @@ PyDoc_STRVAR(c_atanh_doc,
static Py_complex
c_cos(Py_complex z)
{
- /* cos(z) = cosh(iz) */
- Py_complex r;
- r.real = -z.imag;
- r.imag = z.real;
- r = c_cosh(r);
- return r;
+ /* cos(z) = cosh(iz) */
+ Py_complex r;
+ r.real = -z.imag;
+ r.imag = z.real;
+ r = c_cosh(r);
+ return r;
}
PyDoc_STRVAR(c_cos_doc,
@@ -378,51 +378,51 @@ static Py_complex cosh_special_values[7][7];
static Py_complex
c_cosh(Py_complex z)
{
- Py_complex r;
- double x_minus_one;
-
- /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
- if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
- if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
- (z.imag != 0.)) {
- if (z.real > 0) {
- r.real = copysign(INF, cos(z.imag));
- r.imag = copysign(INF, sin(z.imag));
- }
- else {
- r.real = copysign(INF, cos(z.imag));
- r.imag = -copysign(INF, sin(z.imag));
- }
- }
- else {
- r = cosh_special_values[special_type(z.real)]
- [special_type(z.imag)];
- }
- /* need to set errno = EDOM if y is +/- infinity and x is not
- a NaN */
- if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
- errno = EDOM;
- else
- errno = 0;
- return r;
- }
-
- if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
- /* deal correctly with cases where cosh(z.real) overflows but
- cosh(z) does not. */
- x_minus_one = z.real - copysign(1., z.real);
- r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
- r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
- } else {
- r.real = cos(z.imag) * cosh(z.real);
- r.imag = sin(z.imag) * sinh(z.real);
- }
- /* detect overflow, and set errno accordingly */
- if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
- errno = ERANGE;
- else
- errno = 0;
- return r;
+ Py_complex r;
+ double x_minus_one;
+
+ /* special treatment for cosh(+/-inf + iy) if y is not a NaN */
+ if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
+ if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
+ (z.imag != 0.)) {
+ if (z.real > 0) {
+ r.real = copysign(INF, cos(z.imag));
+ r.imag = copysign(INF, sin(z.imag));
+ }
+ else {
+ r.real = copysign(INF, cos(z.imag));
+ r.imag = -copysign(INF, sin(z.imag));
+ }
+ }
+ else {
+ r = cosh_special_values[special_type(z.real)]
+ [special_type(z.imag)];
+ }
+ /* need to set errno = EDOM if y is +/- infinity and x is not
+ a NaN */
+ if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
+ errno = EDOM;
+ else
+ errno = 0;
+ return r;
+ }
+
+ if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
+ /* deal correctly with cases where cosh(z.real) overflows but
+ cosh(z) does not. */
+ x_minus_one = z.real - copysign(1., z.real);
+ r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E;
+ r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E;
+ } else {
+ r.real = cos(z.imag) * cosh(z.real);
+ r.imag = sin(z.imag) * sinh(z.real);
+ }
+ /* detect overflow, and set errno accordingly */
+ if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
+ errno = ERANGE;
+ else
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_cosh_doc,
@@ -438,51 +438,51 @@ static Py_complex exp_special_values[7][7];
static Py_complex
c_exp(Py_complex z)
{
- Py_complex r;
- double l;
-
- if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
- if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
- && (z.imag != 0.)) {
- if (z.real > 0) {
- r.real = copysign(INF, cos(z.imag));
- r.imag = copysign(INF, sin(z.imag));
- }
- else {
- r.real = copysign(0., cos(z.imag));
- r.imag = copysign(0., sin(z.imag));
- }
- }
- else {
- r = exp_special_values[special_type(z.real)]
- [special_type(z.imag)];
- }
- /* need to set errno = EDOM if y is +/- infinity and x is not
- a NaN and not -infinity */
- if (Py_IS_INFINITY(z.imag) &&
- (Py_IS_FINITE(z.real) ||
- (Py_IS_INFINITY(z.real) && z.real > 0)))
- errno = EDOM;
- else
- errno = 0;
- return r;
- }
-
- if (z.real > CM_LOG_LARGE_DOUBLE) {
- l = exp(z.real-1.);
- r.real = l*cos(z.imag)*Py_MATH_E;
- r.imag = l*sin(z.imag)*Py_MATH_E;
- } else {
- l = exp(z.real);
- r.real = l*cos(z.imag);
- r.imag = l*sin(z.imag);
- }
- /* detect overflow, and set errno accordingly */
- if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
- errno = ERANGE;
- else
- errno = 0;
- return r;
+ Py_complex r;
+ double l;
+
+ if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
+ if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
+ && (z.imag != 0.)) {
+ if (z.real > 0) {
+ r.real = copysign(INF, cos(z.imag));
+ r.imag = copysign(INF, sin(z.imag));
+ }
+ else {
+ r.real = copysign(0., cos(z.imag));
+ r.imag = copysign(0., sin(z.imag));
+ }
+ }
+ else {
+ r = exp_special_values[special_type(z.real)]
+ [special_type(z.imag)];
+ }
+ /* need to set errno = EDOM if y is +/- infinity and x is not
+ a NaN and not -infinity */
+ if (Py_IS_INFINITY(z.imag) &&
+ (Py_IS_FINITE(z.real) ||
+ (Py_IS_INFINITY(z.real) && z.real > 0)))
+ errno = EDOM;
+ else
+ errno = 0;
+ return r;
+ }
+
+ if (z.real > CM_LOG_LARGE_DOUBLE) {
+ l = exp(z.real-1.);
+ r.real = l*cos(z.imag)*Py_MATH_E;
+ r.imag = l*sin(z.imag)*Py_MATH_E;
+ } else {
+ l = exp(z.real);
+ r.real = l*cos(z.imag);
+ r.imag = l*sin(z.imag);
+ }
+ /* detect overflow, and set errno accordingly */
+ if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
+ errno = ERANGE;
+ else
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_exp_doc,
@@ -496,85 +496,85 @@ static Py_complex log_special_values[7][7];
static Py_complex
c_log(Py_complex z)
{
- /*
- The usual formula for the real part is log(hypot(z.real, z.imag)).
- There are four situations where this formula is potentially
- problematic:
-
- (1) the absolute value of z is subnormal. Then hypot is subnormal,
- so has fewer than the usual number of bits of accuracy, hence may
- have large relative error. This then gives a large absolute error
- in the log. This can be solved by rescaling z by a suitable power
- of 2.
-
- (2) the absolute value of z is greater than DBL_MAX (e.g. when both
- z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
- Again, rescaling solves this.
-
- (3) the absolute value of z is close to 1. In this case it's
- difficult to achieve good accuracy, at least in part because a
- change of 1ulp in the real or imaginary part of z can result in a
- change of billions of ulps in the correctly rounded answer.
-
- (4) z = 0. The simplest thing to do here is to call the
- floating-point log with an argument of 0, and let its behaviour
- (returning -infinity, signaling a floating-point exception, setting
- errno, or whatever) determine that of c_log. So the usual formula
- is fine here.
-
- */
-
- Py_complex r;
- double ax, ay, am, an, h;
-
- SPECIAL_VALUE(z, log_special_values);
-
- ax = fabs(z.real);
- ay = fabs(z.imag);
-
- if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
- r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
- } else if (ax < DBL_MIN && ay < DBL_MIN) {
- if (ax > 0. || ay > 0.) {
- /* catch cases where hypot(ax, ay) is subnormal */
- r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
- ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
- }
- else {
- /* log(+/-0. +/- 0i) */
- r.real = -INF;
- r.imag = atan2(z.imag, z.real);
- errno = EDOM;
- return r;
- }
- } else {
- h = hypot(ax, ay);
- if (0.71 <= h && h <= 1.73) {
- am = ax > ay ? ax : ay; /* max(ax, ay) */
- an = ax > ay ? ay : ax; /* min(ax, ay) */
- r.real = log1p((am-1)*(am+1)+an*an)/2.;
- } else {
- r.real = log(h);
- }
- }
- r.imag = atan2(z.imag, z.real);
- errno = 0;
- return r;
+ /*
+ The usual formula for the real part is log(hypot(z.real, z.imag)).
+ There are four situations where this formula is potentially
+ problematic:
+
+ (1) the absolute value of z is subnormal. Then hypot is subnormal,
+ so has fewer than the usual number of bits of accuracy, hence may
+ have large relative error. This then gives a large absolute error
+ in the log. This can be solved by rescaling z by a suitable power
+ of 2.
+
+ (2) the absolute value of z is greater than DBL_MAX (e.g. when both
+ z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
+ Again, rescaling solves this.
+
+ (3) the absolute value of z is close to 1. In this case it's
+ difficult to achieve good accuracy, at least in part because a
+ change of 1ulp in the real or imaginary part of z can result in a
+ change of billions of ulps in the correctly rounded answer.
+
+ (4) z = 0. The simplest thing to do here is to call the
+ floating-point log with an argument of 0, and let its behaviour
+ (returning -infinity, signaling a floating-point exception, setting
+ errno, or whatever) determine that of c_log. So the usual formula
+ is fine here.
+
+ */
+
+ Py_complex r;
+ double ax, ay, am, an, h;
+
+ SPECIAL_VALUE(z, log_special_values);
+
+ ax = fabs(z.real);
+ ay = fabs(z.imag);
+
+ if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) {
+ r.real = log(hypot(ax/2., ay/2.)) + M_LN2;
+ } else if (ax < DBL_MIN && ay < DBL_MIN) {
+ if (ax > 0. || ay > 0.) {
+ /* catch cases where hypot(ax, ay) is subnormal */
+ r.real = log(hypot(ldexp(ax, DBL_MANT_DIG),
+ ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2;
+ }
+ else {
+ /* log(+/-0. +/- 0i) */
+ r.real = -INF;
+ r.imag = atan2(z.imag, z.real);
+ errno = EDOM;
+ return r;
+ }
+ } else {
+ h = hypot(ax, ay);
+ if (0.71 <= h && h <= 1.73) {
+ am = ax > ay ? ax : ay; /* max(ax, ay) */
+ an = ax > ay ? ay : ax; /* min(ax, ay) */
+ r.real = log1p((am-1)*(am+1)+an*an)/2.;
+ } else {
+ r.real = log(h);
+ }
+ }
+ r.imag = atan2(z.imag, z.real);
+ errno = 0;
+ return r;
}
static Py_complex
c_log10(Py_complex z)
{
- Py_complex r;
- int errno_save;
-
- r = c_log(z);
- errno_save = errno; /* just in case the divisions affect errno */
- r.real = r.real / M_LN10;
- r.imag = r.imag / M_LN10;
- errno = errno_save;
- return r;
+ Py_complex r;
+ int errno_save;
+
+ r = c_log(z);
+ errno_save = errno; /* just in case the divisions affect errno */
+ r.real = r.real / M_LN10;
+ r.imag = r.imag / M_LN10;
+ errno = errno_save;
+ return r;
}
PyDoc_STRVAR(c_log10_doc,
@@ -586,14 +586,14 @@ PyDoc_STRVAR(c_log10_doc,
static Py_complex
c_sin(Py_complex z)
{
- /* sin(z) = -i sin(iz) */
- Py_complex s, r;
- s.real = -z.imag;
- s.imag = z.real;
- s = c_sinh(s);
- r.real = s.imag;
- r.imag = -s.real;
- return r;
+ /* sin(z) = -i sin(iz) */
+ Py_complex s, r;
+ s.real = -z.imag;
+ s.imag = z.real;
+ s = c_sinh(s);
+ r.real = s.imag;
+ r.imag = -s.real;
+ return r;
}
PyDoc_STRVAR(c_sin_doc,
@@ -608,50 +608,50 @@ static Py_complex sinh_special_values[7][7];
static Py_complex
c_sinh(Py_complex z)
{
- Py_complex r;
- double x_minus_one;
-
- /* special treatment for sinh(+/-inf + iy) if y is finite and
- nonzero */
- if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
- if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
- && (z.imag != 0.)) {
- if (z.real > 0) {
- r.real = copysign(INF, cos(z.imag));
- r.imag = copysign(INF, sin(z.imag));
- }
- else {
- r.real = -copysign(INF, cos(z.imag));
- r.imag = copysign(INF, sin(z.imag));
- }
- }
- else {
- r = sinh_special_values[special_type(z.real)]
- [special_type(z.imag)];
- }
- /* need to set errno = EDOM if y is +/- infinity and x is not
- a NaN */
- if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
- errno = EDOM;
- else
- errno = 0;
- return r;
- }
-
- if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
- x_minus_one = z.real - copysign(1., z.real);
- r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
- r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
- } else {
- r.real = cos(z.imag) * sinh(z.real);
- r.imag = sin(z.imag) * cosh(z.real);
- }
- /* detect overflow, and set errno accordingly */
- if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
- errno = ERANGE;
- else
- errno = 0;
- return r;
+ Py_complex r;
+ double x_minus_one;
+
+ /* special treatment for sinh(+/-inf + iy) if y is finite and
+ nonzero */
+ if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
+ if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
+ && (z.imag != 0.)) {
+ if (z.real > 0) {
+ r.real = copysign(INF, cos(z.imag));
+ r.imag = copysign(INF, sin(z.imag));
+ }
+ else {
+ r.real = -copysign(INF, cos(z.imag));
+ r.imag = copysign(INF, sin(z.imag));
+ }
+ }
+ else {
+ r = sinh_special_values[special_type(z.real)]
+ [special_type(z.imag)];
+ }
+ /* need to set errno = EDOM if y is +/- infinity and x is not
+ a NaN */
+ if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
+ errno = EDOM;
+ else
+ errno = 0;
+ return r;
+ }
+
+ if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
+ x_minus_one = z.real - copysign(1., z.real);
+ r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E;
+ r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E;
+ } else {
+ r.real = cos(z.imag) * sinh(z.real);
+ r.imag = sin(z.imag) * cosh(z.real);
+ }
+ /* detect overflow, and set errno accordingly */
+ if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
+ errno = ERANGE;
+ else
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_sinh_doc,
@@ -665,68 +665,68 @@ static Py_complex sqrt_special_values[7][7];
static Py_complex
c_sqrt(Py_complex z)
{
- /*
- Method: use symmetries to reduce to the case when x = z.real and y
- = z.imag are nonnegative. Then the real part of the result is
- given by
-
- s = sqrt((x + hypot(x, y))/2)
-
- and the imaginary part is
-
- d = (y/2)/s
-
- If either x or y is very large then there's a risk of overflow in
- computation of the expression x + hypot(x, y). We can avoid this
- by rewriting the formula for s as:
-
- s = 2*sqrt(x/8 + hypot(x/8, y/8))
-
- This costs us two extra multiplications/divisions, but avoids the
- overhead of checking for x and y large.
-
- If both x and y are subnormal then hypot(x, y) may also be
- subnormal, so will lack full precision. We solve this by rescaling
- x and y by a sufficiently large power of 2 to ensure that x and y
- are normal.
- */
-
-
- Py_complex r;
- double s,d;
- double ax, ay;
-
- SPECIAL_VALUE(z, sqrt_special_values);
-
- if (z.real == 0. && z.imag == 0.) {
- r.real = 0.;
- r.imag = z.imag;
- return r;
- }
-
- ax = fabs(z.real);
- ay = fabs(z.imag);
-
- if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
- /* here we catch cases where hypot(ax, ay) is subnormal */
- ax = ldexp(ax, CM_SCALE_UP);
- s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
- CM_SCALE_DOWN);
- } else {
- ax /= 8.;
- s = 2.*sqrt(ax + hypot(ax, ay/8.));
- }
- d = ay/(2.*s);
-
- if (z.real >= 0.) {
- r.real = s;
- r.imag = copysign(d, z.imag);
- } else {
- r.real = d;
- r.imag = copysign(s, z.imag);
- }
- errno = 0;
- return r;
+ /*
+ Method: use symmetries to reduce to the case when x = z.real and y
+ = z.imag are nonnegative. Then the real part of the result is
+ given by
+
+ s = sqrt((x + hypot(x, y))/2)
+
+ and the imaginary part is
+
+ d = (y/2)/s
+
+ If either x or y is very large then there's a risk of overflow in
+ computation of the expression x + hypot(x, y). We can avoid this
+ by rewriting the formula for s as:
+
+ s = 2*sqrt(x/8 + hypot(x/8, y/8))
+
+ This costs us two extra multiplications/divisions, but avoids the
+ overhead of checking for x and y large.
+
+ If both x and y are subnormal then hypot(x, y) may also be
+ subnormal, so will lack full precision. We solve this by rescaling
+ x and y by a sufficiently large power of 2 to ensure that x and y
+ are normal.
+ */
+
+
+ Py_complex r;
+ double s,d;
+ double ax, ay;
+
+ SPECIAL_VALUE(z, sqrt_special_values);
+
+ if (z.real == 0. && z.imag == 0.) {
+ r.real = 0.;
+ r.imag = z.imag;
+ return r;
+ }
+
+ ax = fabs(z.real);
+ ay = fabs(z.imag);
+
+ if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) {
+ /* here we catch cases where hypot(ax, ay) is subnormal */
+ ax = ldexp(ax, CM_SCALE_UP);
+ s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))),
+ CM_SCALE_DOWN);
+ } else {
+ ax /= 8.;
+ s = 2.*sqrt(ax + hypot(ax, ay/8.));
+ }
+ d = ay/(2.*s);
+
+ if (z.real >= 0.) {
+ r.real = s;
+ r.imag = copysign(d, z.imag);
+ } else {
+ r.real = d;
+ r.imag = copysign(s, z.imag);
+ }
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_sqrt_doc,
@@ -738,14 +738,14 @@ PyDoc_STRVAR(c_sqrt_doc,
static Py_complex
c_tan(Py_complex z)
{
- /* tan(z) = -i tanh(iz) */
- Py_complex s, r;
- s.real = -z.imag;
- s.imag = z.real;
- s = c_tanh(s);
- r.real = s.imag;
- r.imag = -s.real;
- return r;
+ /* tan(z) = -i tanh(iz) */
+ Py_complex s, r;
+ s.real = -z.imag;
+ s.imag = z.real;
+ s = c_tanh(s);
+ r.real = s.imag;
+ r.imag = -s.real;
+ return r;
}
PyDoc_STRVAR(c_tan_doc,
@@ -760,65 +760,65 @@ static Py_complex tanh_special_values[7][7];
static Py_complex
c_tanh(Py_complex z)
{
- /* Formula:
-
- tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
- (1+tan(y)^2 tanh(x)^2)
-
- To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
- as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
- by 4 exp(-2*x) instead, to avoid possible overflow in the
- computation of cosh(x).
-
- */
-
- Py_complex r;
- double tx, ty, cx, txty, denom;
-
- /* special treatment for tanh(+/-inf + iy) if y is finite and
- nonzero */
- if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
- if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
- && (z.imag != 0.)) {
- if (z.real > 0) {
- r.real = 1.0;
- r.imag = copysign(0.,
- 2.*sin(z.imag)*cos(z.imag));
- }
- else {
- r.real = -1.0;
- r.imag = copysign(0.,
- 2.*sin(z.imag)*cos(z.imag));
- }
- }
- else {
- r = tanh_special_values[special_type(z.real)]
- [special_type(z.imag)];
- }
- /* need to set errno = EDOM if z.imag is +/-infinity and
- z.real is finite */
- if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
- errno = EDOM;
- else
- errno = 0;
- return r;
- }
-
- /* danger of overflow in 2.*z.imag !*/
- if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
- r.real = copysign(1., z.real);
- r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
- } else {
- tx = tanh(z.real);
- ty = tan(z.imag);
- cx = 1./cosh(z.real);
- txty = tx*ty;
- denom = 1. + txty*txty;
- r.real = tx*(1.+ty*ty)/denom;
- r.imag = ((ty/denom)*cx)*cx;
- }
- errno = 0;
- return r;
+ /* Formula:
+
+ tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
+ (1+tan(y)^2 tanh(x)^2)
+
+ To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
+ as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
+ by 4 exp(-2*x) instead, to avoid possible overflow in the
+ computation of cosh(x).
+
+ */
+
+ Py_complex r;
+ double tx, ty, cx, txty, denom;
+
+ /* special treatment for tanh(+/-inf + iy) if y is finite and
+ nonzero */
+ if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
+ if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
+ && (z.imag != 0.)) {
+ if (z.real > 0) {
+ r.real = 1.0;
+ r.imag = copysign(0.,
+ 2.*sin(z.imag)*cos(z.imag));
+ }
+ else {
+ r.real = -1.0;
+ r.imag = copysign(0.,
+ 2.*sin(z.imag)*cos(z.imag));
+ }
+ }
+ else {
+ r = tanh_special_values[special_type(z.real)]
+ [special_type(z.imag)];
+ }
+ /* need to set errno = EDOM if z.imag is +/-infinity and
+ z.real is finite */
+ if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
+ errno = EDOM;
+ else
+ errno = 0;
+ return r;
+ }
+
+ /* danger of overflow in 2.*z.imag !*/
+ if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) {
+ r.real = copysign(1., z.real);
+ r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real));
+ } else {
+ tx = tanh(z.real);
+ ty = tan(z.imag);
+ cx = 1./cosh(z.real);
+ txty = tx*ty;
+ denom = 1. + txty*txty;
+ r.real = tx*(1.+ty*ty)/denom;
+ r.imag = ((ty/denom)*cx)*cx;
+ }
+ errno = 0;
+ return r;
}
PyDoc_STRVAR(c_tanh_doc,
@@ -830,23 +830,23 @@ PyDoc_STRVAR(c_tanh_doc,
static PyObject *
cmath_log(PyObject *self, PyObject *args)
{
- Py_complex x;
- Py_complex y;
-
- if (!PyArg_ParseTuple(args, "D|D", &x, &y))
- return NULL;
-
- errno = 0;
- PyFPE_START_PROTECT("complex function", return 0)
- x = c_log(x);
- if (PyTuple_GET_SIZE(args) == 2) {
- y = c_log(y);
- x = c_quot(x, y);
- }
- PyFPE_END_PROTECT(x)
- if (errno != 0)
- return math_error();
- return PyComplex_FromCComplex(x);
+ Py_complex x;
+ Py_complex y;
+
+ if (!PyArg_ParseTuple(args, "D|D", &x, &y))
+ return NULL;
+
+ errno = 0;
+ PyFPE_START_PROTECT("complex function", return 0)
+ x = c_log(x);
+ if (PyTuple_GET_SIZE(args) == 2) {
+ y = c_log(y);
+ x = c_quot(x, y);
+ }
+ PyFPE_END_PROTECT(x)
+ if (errno != 0)
+ return math_error();
+ return PyComplex_FromCComplex(x);
}
PyDoc_STRVAR(cmath_log_doc,
@@ -859,42 +859,42 @@ If the base not specified, returns the natural logarithm (base e) of x.");
static PyObject *
math_error(void)
{
- if (errno == EDOM)
- PyErr_SetString(PyExc_ValueError, "math domain error");
- else if (errno == ERANGE)
- PyErr_SetString(PyExc_OverflowError, "math range error");
- else /* Unexpected math error */
- PyErr_SetFromErrno(PyExc_ValueError);
- return NULL;
+ if (errno == EDOM)
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ else if (errno == ERANGE)
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ else /* Unexpected math error */
+ PyErr_SetFromErrno(PyExc_ValueError);
+ return NULL;
}
static PyObject *
math_1(PyObject *args, Py_complex (*func)(Py_complex))
{
- Py_complex x,r ;
- if (!PyArg_ParseTuple(args, "D", &x))
- return NULL;
- errno = 0;
- PyFPE_START_PROTECT("complex function", return 0);
- r = (*func)(x);
- PyFPE_END_PROTECT(r);
- if (errno == EDOM) {
- PyErr_SetString(PyExc_ValueError, "math domain error");
- return NULL;
- }
- else if (errno == ERANGE) {
- PyErr_SetString(PyExc_OverflowError, "math range error");
- return NULL;
- }
- else {
- return PyComplex_FromCComplex(r);
- }
+ Py_complex x,r ;
+ if (!PyArg_ParseTuple(args, "D", &x))
+ return NULL;
+ errno = 0;
+ PyFPE_START_PROTECT("complex function", return 0);
+ r = (*func)(x);
+ PyFPE_END_PROTECT(r);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+ return NULL;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError, "math range error");
+ return NULL;
+ }
+ else {
+ return PyComplex_FromCComplex(r);
+ }
}
#define FUNC1(stubname, func) \
- static PyObject * stubname(PyObject *self, PyObject *args) { \
- return math_1(args, func); \
- }
+ static PyObject * stubname(PyObject *self, PyObject *args) { \
+ return math_1(args, func); \
+ }
FUNC1(cmath_acos, c_acos)
FUNC1(cmath_acosh, c_acosh)
@@ -915,18 +915,18 @@ FUNC1(cmath_tanh, c_tanh)
static PyObject *
cmath_phase(PyObject *self, PyObject *args)
{
- Py_complex z;
- double phi;
- if (!PyArg_ParseTuple(args, "D:phase", &z))
- return NULL;
- errno = 0;
- PyFPE_START_PROTECT("arg function", return 0)
- phi = c_atan2(z);
- PyFPE_END_PROTECT(phi)
- if (errno != 0)
- return math_error();
- else
- return PyFloat_FromDouble(phi);
+ Py_complex z;
+ double phi;
+ if (!PyArg_ParseTuple(args, "D:phase", &z))
+ return NULL;
+ errno = 0;
+ PyFPE_START_PROTECT("arg function", return 0)
+ phi = c_atan2(z);
+ PyFPE_END_PROTECT(phi)
+ if (errno != 0)
+ return math_error();
+ else
+ return PyFloat_FromDouble(phi);
}
PyDoc_STRVAR(cmath_phase_doc,
@@ -936,18 +936,18 @@ Return argument, also known as the phase angle, of a complex.");
static PyObject *
cmath_polar(PyObject *self, PyObject *args)
{
- Py_complex z;
- double r, phi;
- if (!PyArg_ParseTuple(args, "D:polar", &z))
- return NULL;
- PyFPE_START_PROTECT("polar function", return 0)
- phi = c_atan2(z); /* should not cause any exception */
- r = c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */
- PyFPE_END_PROTECT(r)
- if (errno != 0)
- return math_error();
- else
- return Py_BuildValue("dd", r, phi);
+ Py_complex z;
+ double r, phi;
+ if (!PyArg_ParseTuple(args, "D:polar", &z))
+ return NULL;
+ PyFPE_START_PROTECT("polar function", return 0)
+ phi = c_atan2(z); /* should not cause any exception */
+ r = c_abs(z); /* sets errno to ERANGE on overflow; otherwise 0 */
+ PyFPE_END_PROTECT(r)
+ if (errno != 0)
+ return math_error();
+ else
+ return Py_BuildValue("dd", r, phi);
}
PyDoc_STRVAR(cmath_polar_doc,
@@ -971,51 +971,51 @@ static Py_complex rect_special_values[7][7];
static PyObject *
cmath_rect(PyObject *self, PyObject *args)
{
- Py_complex z;
- double r, phi;
- if (!PyArg_ParseTuple(args, "dd:rect", &r, &phi))
- return NULL;
- errno = 0;
- PyFPE_START_PROTECT("rect function", return 0)
-
- /* deal with special values */
- if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
- /* if r is +/-infinity and phi is finite but nonzero then
- result is (+-INF +-INF i), but we need to compute cos(phi)
- and sin(phi) to figure out the signs. */
- if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
- && (phi != 0.))) {
- if (r > 0) {
- z.real = copysign(INF, cos(phi));
- z.imag = copysign(INF, sin(phi));
- }
- else {
- z.real = -copysign(INF, cos(phi));
- z.imag = -copysign(INF, sin(phi));
- }
- }
- else {
- z = rect_special_values[special_type(r)]
- [special_type(phi)];
- }
- /* need to set errno = EDOM if r is a nonzero number and phi
- is infinite */
- if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
- errno = EDOM;
- else
- errno = 0;
- }
- else {
- z.real = r * cos(phi);
- z.imag = r * sin(phi);
- errno = 0;
- }
-
- PyFPE_END_PROTECT(z)
- if (errno != 0)
- return math_error();
- else
- return PyComplex_FromCComplex(z);
+ Py_complex z;
+ double r, phi;
+ if (!PyArg_ParseTuple(args, "dd:rect", &r, &phi))
+ return NULL;
+ errno = 0;
+ PyFPE_START_PROTECT("rect function", return 0)
+
+ /* deal with special values */
+ if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
+ /* if r is +/-infinity and phi is finite but nonzero then
+ result is (+-INF +-INF i), but we need to compute cos(phi)
+ and sin(phi) to figure out the signs. */
+ if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
+ && (phi != 0.))) {
+ if (r > 0) {
+ z.real = copysign(INF, cos(phi));
+ z.imag = copysign(INF, sin(phi));
+ }
+ else {
+ z.real = -copysign(INF, cos(phi));
+ z.imag = -copysign(INF, sin(phi));
+ }
+ }
+ else {
+ z = rect_special_values[special_type(r)]
+ [special_type(phi)];
+ }
+ /* need to set errno = EDOM if r is a nonzero number and phi
+ is infinite */
+ if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
+ errno = EDOM;
+ else
+ errno = 0;
+ }
+ else {
+ z.real = r * cos(phi);
+ z.imag = r * sin(phi);
+ errno = 0;
+ }
+
+ PyFPE_END_PROTECT(z)
+ if (errno != 0)
+ return math_error();
+ else
+ return PyComplex_FromCComplex(z);
}
PyDoc_STRVAR(cmath_rect_doc,
@@ -1025,10 +1025,10 @@ Convert from polar coordinates to rectangular coordinates.");
static PyObject *
cmath_isnan(PyObject *self, PyObject *args)
{
- Py_complex z;
- if (!PyArg_ParseTuple(args, "D:isnan", &z))
- return NULL;
- return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
+ Py_complex z;
+ if (!PyArg_ParseTuple(args, "D:isnan", &z))
+ return NULL;
+ return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
}
PyDoc_STRVAR(cmath_isnan_doc,
@@ -1038,11 +1038,11 @@ Checks if the real or imaginary part of z not a number (NaN)");
static PyObject *
cmath_isinf(PyObject *self, PyObject *args)
{
- Py_complex z;
- if (!PyArg_ParseTuple(args, "D:isnan", &z))
- return NULL;
- return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
- Py_IS_INFINITY(z.imag));
+ Py_complex z;
+ if (!PyArg_ParseTuple(args, "D:isnan", &z))
+ return NULL;
+ return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
+ Py_IS_INFINITY(z.imag));
}
PyDoc_STRVAR(cmath_isinf_doc,
@@ -1055,169 +1055,169 @@ PyDoc_STRVAR(module_doc,
"functions for complex numbers.");
static PyMethodDef cmath_methods[] = {
- {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
- {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
- {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
- {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
- {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
- {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
- {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
- {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
- {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
- {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
- {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
- {"log", cmath_log, METH_VARARGS, cmath_log_doc},
- {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
- {"phase", cmath_phase, METH_VARARGS, cmath_phase_doc},
- {"polar", cmath_polar, METH_VARARGS, cmath_polar_doc},
- {"rect", cmath_rect, METH_VARARGS, cmath_rect_doc},
- {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
- {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
- {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
- {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
- {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
- {NULL, NULL} /* sentinel */
+ {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
+ {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
+ {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
+ {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
+ {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
+ {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
+ {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
+ {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
+ {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
+ {"isinf", cmath_isinf, METH_VARARGS, cmath_isinf_doc},
+ {"isnan", cmath_isnan, METH_VARARGS, cmath_isnan_doc},
+ {"log", cmath_log, METH_VARARGS, cmath_log_doc},
+ {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
+ {"phase", cmath_phase, METH_VARARGS, cmath_phase_doc},
+ {"polar", cmath_polar, METH_VARARGS, cmath_polar_doc},
+ {"rect", cmath_rect, METH_VARARGS, cmath_rect_doc},
+ {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
+ {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
+ {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
+ {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
+ {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef cmathmodule = {
- PyModuleDef_HEAD_INIT,
- "cmath",
- module_doc,
- -1,
- cmath_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "cmath",
+ module_doc,
+ -1,
+ cmath_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_cmath(void)
{
- PyObject *m;
+ PyObject *m;
- m = PyModule_Create(&cmathmodule);
- if (m == NULL)
- return NULL;
+ m = PyModule_Create(&cmathmodule);
+ if (m == NULL)
+ return NULL;
- PyModule_AddObject(m, "pi",
- PyFloat_FromDouble(Py_MATH_PI));
- PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
+ PyModule_AddObject(m, "pi",
+ PyFloat_FromDouble(Py_MATH_PI));
+ PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
- /* initialize special value tables */
+ /* initialize special value tables */
#define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY }
#define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p;
- INIT_SPECIAL_VALUES(acos_special_values, {
- C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
- C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
- C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
- C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
- C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
- C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
- C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(acosh_special_values, {
- C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
- C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
- C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(asinh_special_values, {
- C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
- C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
- C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
- C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(atanh_special_values, {
- C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
- C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
- C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
- C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
- C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
- C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
- C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(cosh_special_values, {
- C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
- C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(exp_special_values, {
- C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(log_special_values, {
- C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
- C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
- C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
- C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(sinh_special_values, {
- C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
- C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(sqrt_special_values, {
- C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
- C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
- C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
- C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
- C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
- C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
- C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(tanh_special_values, {
- C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
- C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
- })
-
- INIT_SPECIAL_VALUES(rect_special_values, {
- C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
- C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
- C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
- C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
- C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
- })
- return m;
+ INIT_SPECIAL_VALUES(acos_special_values, {
+ C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF)
+ C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
+ C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
+ C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N)
+ C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N)
+ C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF)
+ C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(acosh_special_values, {
+ C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
+ C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
+ C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(asinh_special_values, {
+ C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N)
+ C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N)
+ C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
+ C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(atanh_special_values, {
+ C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N)
+ C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N)
+ C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N)
+ C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N)
+ C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N)
+ C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N)
+ C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(cosh_special_values, {
+ C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.)
+ C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(exp_special_values, {
+ C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(log_special_values, {
+ C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N)
+ C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N)
+ C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N)
+ C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(sinh_special_values, {
+ C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N)
+ C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(sqrt_special_values, {
+ C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF)
+ C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
+ C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
+ C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N)
+ C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N)
+ C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N)
+ C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(tanh_special_values, {
+ C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.)
+ C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N)
+ })
+
+ INIT_SPECIAL_VALUES(rect_special_values, {
+ C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.)
+ C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.)
+ C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N)
+ C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N)
+ C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N)
+ })
+ return m;
}
diff --git a/Modules/cryptmodule.c b/Modules/cryptmodule.c
index d3d9271..d5a42ff 100644
--- a/Modules/cryptmodule.c
+++ b/Modules/cryptmodule.c
@@ -14,17 +14,17 @@
static PyObject *crypt_crypt(PyObject *self, PyObject *args)
{
- char *word, *salt;
+ char *word, *salt;
#ifndef __VMS
- extern char * crypt(const char *, const char *);
+ extern char * crypt(const char *, const char *);
#endif
- if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) {
- return NULL;
- }
- /* On some platforms (AtheOS) crypt returns NULL for an invalid
- salt. Return None in that case. XXX Maybe raise an exception? */
- return Py_BuildValue("s", crypt(word, salt));
+ if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) {
+ return NULL;
+ }
+ /* On some platforms (AtheOS) crypt returns NULL for an invalid
+ salt. Return None in that case. XXX Maybe raise an exception? */
+ return Py_BuildValue("s", crypt(word, salt));
}
@@ -38,25 +38,25 @@ the same alphabet as the salt.");
static PyMethodDef crypt_methods[] = {
- {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
- {NULL, NULL} /* sentinel */
+ {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef cryptmodule = {
- PyModuleDef_HEAD_INIT,
- "crypt",
- NULL,
- -1,
- crypt_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "crypt",
+ NULL,
+ -1,
+ crypt_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_crypt(void)
{
- return PyModule_Create(&cryptmodule);
+ return PyModule_Create(&cryptmodule);
}
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 8ba3474..6235d6e 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -25,7 +25,7 @@
* final result fits in a C int (this can be an issue on 64-bit boxes).
*/
#if SIZEOF_INT < 4
-# error "datetime.c requires that C int have at least 32 bits"
+# error "datetime.c requires that C int have at least 32 bits"
#endif
#define MINYEAR 1
@@ -39,59 +39,59 @@
#define MAX_DELTA_DAYS 999999999
/* Rename the long macros in datetime.h to more reasonable short names. */
-#define GET_YEAR PyDateTime_GET_YEAR
-#define GET_MONTH PyDateTime_GET_MONTH
-#define GET_DAY PyDateTime_GET_DAY
-#define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR
-#define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE
-#define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND
-#define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND
+#define GET_YEAR PyDateTime_GET_YEAR
+#define GET_MONTH PyDateTime_GET_MONTH
+#define GET_DAY PyDateTime_GET_DAY
+#define DATE_GET_HOUR PyDateTime_DATE_GET_HOUR
+#define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE
+#define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND
+#define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND
/* Date accessors for date and datetime. */
-#define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \
- ((o)->data[1] = ((v) & 0x00ff)))
-#define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v))
-#define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v))
+#define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \
+ ((o)->data[1] = ((v) & 0x00ff)))
+#define SET_MONTH(o, v) (PyDateTime_GET_MONTH(o) = (v))
+#define SET_DAY(o, v) (PyDateTime_GET_DAY(o) = (v))
/* Date/Time accessors for datetime. */
-#define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v))
-#define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v))
-#define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v))
-#define DATE_SET_MICROSECOND(o, v) \
- (((o)->data[7] = ((v) & 0xff0000) >> 16), \
- ((o)->data[8] = ((v) & 0x00ff00) >> 8), \
- ((o)->data[9] = ((v) & 0x0000ff)))
+#define DATE_SET_HOUR(o, v) (PyDateTime_DATE_GET_HOUR(o) = (v))
+#define DATE_SET_MINUTE(o, v) (PyDateTime_DATE_GET_MINUTE(o) = (v))
+#define DATE_SET_SECOND(o, v) (PyDateTime_DATE_GET_SECOND(o) = (v))
+#define DATE_SET_MICROSECOND(o, v) \
+ (((o)->data[7] = ((v) & 0xff0000) >> 16), \
+ ((o)->data[8] = ((v) & 0x00ff00) >> 8), \
+ ((o)->data[9] = ((v) & 0x0000ff)))
/* Time accessors for time. */
-#define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR
-#define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE
-#define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND
-#define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND
-#define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v))
-#define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v))
-#define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v))
-#define TIME_SET_MICROSECOND(o, v) \
- (((o)->data[3] = ((v) & 0xff0000) >> 16), \
- ((o)->data[4] = ((v) & 0x00ff00) >> 8), \
- ((o)->data[5] = ((v) & 0x0000ff)))
+#define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR
+#define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE
+#define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND
+#define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND
+#define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v))
+#define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v))
+#define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v))
+#define TIME_SET_MICROSECOND(o, v) \
+ (((o)->data[3] = ((v) & 0xff0000) >> 16), \
+ ((o)->data[4] = ((v) & 0x00ff00) >> 8), \
+ ((o)->data[5] = ((v) & 0x0000ff)))
/* Delta accessors for timedelta. */
-#define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days)
-#define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds)
-#define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds)
+#define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days)
+#define GET_TD_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds)
+#define GET_TD_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds)
-#define SET_TD_DAYS(o, v) ((o)->days = (v))
-#define SET_TD_SECONDS(o, v) ((o)->seconds = (v))
+#define SET_TD_DAYS(o, v) ((o)->days = (v))
+#define SET_TD_SECONDS(o, v) ((o)->seconds = (v))
#define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v))
/* p is a pointer to a time or a datetime object; HASTZINFO(p) returns
* p->hastzinfo.
*/
-#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
+#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
/* M is a char or int claiming to be a valid month. The macro is equivalent
* to the two-sided Python test
- * 1 <= M <= 12
+ * 1 <= M <= 12
*/
#define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12)
@@ -111,7 +111,7 @@ static PyTypeObject PyDateTime_TZInfoType;
* iff (k^i)&(k^j) has sign bit set.
*/
#define SIGNED_ADD_OVERFLOWED(RESULT, I, J) \
- ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0)
+ ((((RESULT) ^ (I)) & ((RESULT) ^ (J))) < 0)
/* Compute Python divmod(x, y), returning the quotient and storing the
* remainder into *r. The quotient is the floor of x/y, and that's
@@ -125,17 +125,17 @@ static PyTypeObject PyDateTime_TZInfoType;
static int
divmod(int x, int y, int *r)
{
- int quo;
+ int quo;
- assert(y > 0);
- quo = x / y;
- *r = x - quo * y;
- if (*r < 0) {
- --quo;
- *r += y;
- }
- assert(0 <= *r && *r < y);
- return quo;
+ assert(y > 0);
+ quo = x / y;
+ *r = x - quo * y;
+ if (*r < 0) {
+ --quo;
+ *r += y;
+ }
+ assert(0 <= *r && *r < y);
+ return quo;
}
/* Round a double to the nearest long. |x| must be small enough to fit
@@ -144,11 +144,11 @@ divmod(int x, int y, int *r)
static long
round_to_long(double x)
{
- if (x >= 0.0)
- x = floor(x + 0.5);
- else
- x = ceil(x - 0.5);
- return (long)x;
+ if (x >= 0.0)
+ x = floor(x + 0.5);
+ else
+ x = ceil(x - 0.5);
+ return (long)x;
}
/* ---------------------------------------------------------------------------
@@ -160,52 +160,52 @@ round_to_long(double x)
* are correct for non-leap years only.
*/
static int _days_in_month[] = {
- 0, /* unused; this vector uses 1-based indexing */
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+ 0, /* unused; this vector uses 1-based indexing */
+ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static int _days_before_month[] = {
- 0, /* unused; this vector uses 1-based indexing */
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+ 0, /* unused; this vector uses 1-based indexing */
+ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
/* year -> 1 if leap year, else 0. */
static int
is_leap(int year)
{
- /* Cast year to unsigned. The result is the same either way, but
- * C can generate faster code for unsigned mod than for signed
- * mod (especially for % 4 -- a good compiler should just grab
- * the last 2 bits when the LHS is unsigned).
- */
- const unsigned int ayear = (unsigned int)year;
- return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0);
+ /* Cast year to unsigned. The result is the same either way, but
+ * C can generate faster code for unsigned mod than for signed
+ * mod (especially for % 4 -- a good compiler should just grab
+ * the last 2 bits when the LHS is unsigned).
+ */
+ const unsigned int ayear = (unsigned int)year;
+ return ayear % 4 == 0 && (ayear % 100 != 0 || ayear % 400 == 0);
}
/* year, month -> number of days in that month in that year */
static int
days_in_month(int year, int month)
{
- assert(month >= 1);
- assert(month <= 12);
- if (month == 2 && is_leap(year))
- return 29;
- else
- return _days_in_month[month];
+ assert(month >= 1);
+ assert(month <= 12);
+ if (month == 2 && is_leap(year))
+ return 29;
+ else
+ return _days_in_month[month];
}
/* year, month -> number of days in year preceeding first day of month */
static int
days_before_month(int year, int month)
{
- int days;
+ int days;
- assert(month >= 1);
- assert(month <= 12);
- days = _days_before_month[month];
- if (month > 2 && is_leap(year))
- ++days;
- return days;
+ assert(month >= 1);
+ assert(month <= 12);
+ days = _days_before_month[month];
+ if (month > 2 && is_leap(year))
+ ++days;
+ return days;
}
/* year -> number of days before January 1st of year. Remember that we
@@ -214,124 +214,124 @@ days_before_month(int year, int month)
static int
days_before_year(int year)
{
- int y = year - 1;
- /* This is incorrect if year <= 0; we really want the floor
- * here. But so long as MINYEAR is 1, the smallest year this
- * can see is 0 (this can happen in some normalization endcases),
- * so we'll just special-case that.
- */
- assert (year >= 0);
- if (y >= 0)
- return y*365 + y/4 - y/100 + y/400;
- else {
- assert(y == -1);
- return -366;
- }
+ int y = year - 1;
+ /* This is incorrect if year <= 0; we really want the floor
+ * here. But so long as MINYEAR is 1, the smallest year this
+ * can see is 0 (this can happen in some normalization endcases),
+ * so we'll just special-case that.
+ */
+ assert (year >= 0);
+ if (y >= 0)
+ return y*365 + y/4 - y/100 + y/400;
+ else {
+ assert(y == -1);
+ return -366;
+ }
}
/* Number of days in 4, 100, and 400 year cycles. That these have
* the correct values is asserted in the module init function.
*/
-#define DI4Y 1461 /* days_before_year(5); days in 4 years */
-#define DI100Y 36524 /* days_before_year(101); days in 100 years */
-#define DI400Y 146097 /* days_before_year(401); days in 400 years */
+#define DI4Y 1461 /* days_before_year(5); days in 4 years */
+#define DI100Y 36524 /* days_before_year(101); days in 100 years */
+#define DI400Y 146097 /* days_before_year(401); days in 400 years */
/* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
static void
ord_to_ymd(int ordinal, int *year, int *month, int *day)
{
- int n, n1, n4, n100, n400, leapyear, preceding;
-
- /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of
- * leap years repeats exactly every 400 years. The basic strategy is
- * to find the closest 400-year boundary at or before ordinal, then
- * work with the offset from that boundary to ordinal. Life is much
- * clearer if we subtract 1 from ordinal first -- then the values
- * of ordinal at 400-year boundaries are exactly those divisible
- * by DI400Y:
- *
- * D M Y n n-1
- * -- --- ---- ---------- ----------------
- * 31 Dec -400 -DI400Y -DI400Y -1
- * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
- * ...
- * 30 Dec 000 -1 -2
- * 31 Dec 000 0 -1
- * 1 Jan 001 1 0 400-year boundary
- * 2 Jan 001 2 1
- * 3 Jan 001 3 2
- * ...
- * 31 Dec 400 DI400Y DI400Y -1
- * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
- */
- assert(ordinal >= 1);
- --ordinal;
- n400 = ordinal / DI400Y;
- n = ordinal % DI400Y;
- *year = n400 * 400 + 1;
-
- /* Now n is the (non-negative) offset, in days, from January 1 of
- * year, to the desired date. Now compute how many 100-year cycles
- * precede n.
- * Note that it's possible for n100 to equal 4! In that case 4 full
- * 100-year cycles precede the desired day, which implies the
- * desired day is December 31 at the end of a 400-year cycle.
- */
- n100 = n / DI100Y;
- n = n % DI100Y;
-
- /* Now compute how many 4-year cycles precede it. */
- n4 = n / DI4Y;
- n = n % DI4Y;
-
- /* And now how many single years. Again n1 can be 4, and again
- * meaning that the desired day is December 31 at the end of the
- * 4-year cycle.
- */
- n1 = n / 365;
- n = n % 365;
-
- *year += n100 * 100 + n4 * 4 + n1;
- if (n1 == 4 || n100 == 4) {
- assert(n == 0);
- *year -= 1;
- *month = 12;
- *day = 31;
- return;
- }
-
- /* Now the year is correct, and n is the offset from January 1. We
- * find the month via an estimate that's either exact or one too
- * large.
- */
- leapyear = n1 == 3 && (n4 != 24 || n100 == 3);
- assert(leapyear == is_leap(*year));
- *month = (n + 50) >> 5;
- preceding = (_days_before_month[*month] + (*month > 2 && leapyear));
- if (preceding > n) {
- /* estimate is too large */
- *month -= 1;
- preceding -= days_in_month(*year, *month);
- }
- n -= preceding;
- assert(0 <= n);
- assert(n < days_in_month(*year, *month));
-
- *day = n + 1;
+ int n, n1, n4, n100, n400, leapyear, preceding;
+
+ /* ordinal is a 1-based index, starting at 1-Jan-1. The pattern of
+ * leap years repeats exactly every 400 years. The basic strategy is
+ * to find the closest 400-year boundary at or before ordinal, then
+ * work with the offset from that boundary to ordinal. Life is much
+ * clearer if we subtract 1 from ordinal first -- then the values
+ * of ordinal at 400-year boundaries are exactly those divisible
+ * by DI400Y:
+ *
+ * D M Y n n-1
+ * -- --- ---- ---------- ----------------
+ * 31 Dec -400 -DI400Y -DI400Y -1
+ * 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
+ * ...
+ * 30 Dec 000 -1 -2
+ * 31 Dec 000 0 -1
+ * 1 Jan 001 1 0 400-year boundary
+ * 2 Jan 001 2 1
+ * 3 Jan 001 3 2
+ * ...
+ * 31 Dec 400 DI400Y DI400Y -1
+ * 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
+ */
+ assert(ordinal >= 1);
+ --ordinal;
+ n400 = ordinal / DI400Y;
+ n = ordinal % DI400Y;
+ *year = n400 * 400 + 1;
+
+ /* Now n is the (non-negative) offset, in days, from January 1 of
+ * year, to the desired date. Now compute how many 100-year cycles
+ * precede n.
+ * Note that it's possible for n100 to equal 4! In that case 4 full
+ * 100-year cycles precede the desired day, which implies the
+ * desired day is December 31 at the end of a 400-year cycle.
+ */
+ n100 = n / DI100Y;
+ n = n % DI100Y;
+
+ /* Now compute how many 4-year cycles precede it. */
+ n4 = n / DI4Y;
+ n = n % DI4Y;
+
+ /* And now how many single years. Again n1 can be 4, and again
+ * meaning that the desired day is December 31 at the end of the
+ * 4-year cycle.
+ */
+ n1 = n / 365;
+ n = n % 365;
+
+ *year += n100 * 100 + n4 * 4 + n1;
+ if (n1 == 4 || n100 == 4) {
+ assert(n == 0);
+ *year -= 1;
+ *month = 12;
+ *day = 31;
+ return;
+ }
+
+ /* Now the year is correct, and n is the offset from January 1. We
+ * find the month via an estimate that's either exact or one too
+ * large.
+ */
+ leapyear = n1 == 3 && (n4 != 24 || n100 == 3);
+ assert(leapyear == is_leap(*year));
+ *month = (n + 50) >> 5;
+ preceding = (_days_before_month[*month] + (*month > 2 && leapyear));
+ if (preceding > n) {
+ /* estimate is too large */
+ *month -= 1;
+ preceding -= days_in_month(*year, *month);
+ }
+ n -= preceding;
+ assert(0 <= n);
+ assert(n < days_in_month(*year, *month));
+
+ *day = n + 1;
}
/* year, month, day -> ordinal, considering 01-Jan-0001 as day 1. */
static int
ymd_to_ord(int year, int month, int day)
{
- return days_before_year(year) + days_before_month(year, month) + day;
+ return days_before_year(year) + days_before_month(year, month) + day;
}
/* Day of week, where Monday==0, ..., Sunday==6. 1/1/1 was a Monday. */
static int
weekday(int year, int month, int day)
{
- return (ymd_to_ord(year, month, day) + 6) % 7;
+ return (ymd_to_ord(year, month, day) + 6) % 7;
}
/* Ordinal of the Monday starting week 1 of the ISO year. Week 1 is the
@@ -340,15 +340,15 @@ weekday(int year, int month, int day)
static int
iso_week1_monday(int year)
{
- int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */
- /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */
- int first_weekday = (first_day + 6) % 7;
- /* ordinal of closest Monday at or before 1/1 */
- int week1_monday = first_day - first_weekday;
+ int first_day = ymd_to_ord(year, 1, 1); /* ord of 1/1 */
+ /* 0 if 1/1 is a Monday, 1 if a Tue, etc. */
+ int first_weekday = (first_day + 6) % 7;
+ /* ordinal of closest Monday at or before 1/1 */
+ int week1_monday = first_day - first_weekday;
- if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */
- week1_monday += 7;
- return week1_monday;
+ if (first_weekday > 3) /* if 1/1 was Fri, Sat, Sun */
+ week1_monday += 7;
+ return week1_monday;
}
/* ---------------------------------------------------------------------------
@@ -361,12 +361,12 @@ iso_week1_monday(int year)
static int
check_delta_day_range(int days)
{
- if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
- return 0;
- PyErr_Format(PyExc_OverflowError,
- "days=%d; must have magnitude <= %d",
- days, MAX_DELTA_DAYS);
- return -1;
+ if (-MAX_DELTA_DAYS <= days && days <= MAX_DELTA_DAYS)
+ return 0;
+ PyErr_Format(PyExc_OverflowError,
+ "days=%d; must have magnitude <= %d",
+ days, MAX_DELTA_DAYS);
+ return -1;
}
/* Check that date arguments are in range. Return 0 if they are. If they
@@ -376,22 +376,22 @@ static int
check_date_args(int year, int month, int day)
{
- if (year < MINYEAR || year > MAXYEAR) {
- PyErr_SetString(PyExc_ValueError,
- "year is out of range");
- return -1;
- }
- if (month < 1 || month > 12) {
- PyErr_SetString(PyExc_ValueError,
- "month must be in 1..12");
- return -1;
- }
- if (day < 1 || day > days_in_month(year, month)) {
- PyErr_SetString(PyExc_ValueError,
- "day is out of range for month");
- return -1;
- }
- return 0;
+ if (year < MINYEAR || year > MAXYEAR) {
+ PyErr_SetString(PyExc_ValueError,
+ "year is out of range");
+ return -1;
+ }
+ if (month < 1 || month > 12) {
+ PyErr_SetString(PyExc_ValueError,
+ "month must be in 1..12");
+ return -1;
+ }
+ if (day < 1 || day > days_in_month(year, month)) {
+ PyErr_SetString(PyExc_ValueError,
+ "day is out of range for month");
+ return -1;
+ }
+ return 0;
}
/* Check that time arguments are in range. Return 0 if they are. If they
@@ -400,27 +400,27 @@ check_date_args(int year, int month, int day)
static int
check_time_args(int h, int m, int s, int us)
{
- if (h < 0 || h > 23) {
- PyErr_SetString(PyExc_ValueError,
- "hour must be in 0..23");
- return -1;
- }
- if (m < 0 || m > 59) {
- PyErr_SetString(PyExc_ValueError,
- "minute must be in 0..59");
- return -1;
- }
- if (s < 0 || s > 59) {
- PyErr_SetString(PyExc_ValueError,
- "second must be in 0..59");
- return -1;
- }
- if (us < 0 || us > 999999) {
- PyErr_SetString(PyExc_ValueError,
- "microsecond must be in 0..999999");
- return -1;
- }
- return 0;
+ if (h < 0 || h > 23) {
+ PyErr_SetString(PyExc_ValueError,
+ "hour must be in 0..23");
+ return -1;
+ }
+ if (m < 0 || m > 59) {
+ PyErr_SetString(PyExc_ValueError,
+ "minute must be in 0..59");
+ return -1;
+ }
+ if (s < 0 || s > 59) {
+ PyErr_SetString(PyExc_ValueError,
+ "second must be in 0..59");
+ return -1;
+ }
+ if (us < 0 || us > 999999) {
+ PyErr_SetString(PyExc_ValueError,
+ "microsecond must be in 0..999999");
+ return -1;
+ }
+ return 0;
}
/* ---------------------------------------------------------------------------
@@ -436,109 +436,109 @@ check_time_args(int h, int m, int s, int us)
static void
normalize_pair(int *hi, int *lo, int factor)
{
- assert(factor > 0);
- assert(lo != hi);
- if (*lo < 0 || *lo >= factor) {
- const int num_hi = divmod(*lo, factor, lo);
- const int new_hi = *hi + num_hi;
- assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi));
- *hi = new_hi;
- }
- assert(0 <= *lo && *lo < factor);
+ assert(factor > 0);
+ assert(lo != hi);
+ if (*lo < 0 || *lo >= factor) {
+ const int num_hi = divmod(*lo, factor, lo);
+ const int new_hi = *hi + num_hi;
+ assert(! SIGNED_ADD_OVERFLOWED(new_hi, *hi, num_hi));
+ *hi = new_hi;
+ }
+ assert(0 <= *lo && *lo < factor);
}
/* Fiddle days (d), seconds (s), and microseconds (us) so that
- * 0 <= *s < 24*3600
- * 0 <= *us < 1000000
+ * 0 <= *s < 24*3600
+ * 0 <= *us < 1000000
* The input values must be such that the internals don't overflow.
* The way this routine is used, we don't get close.
*/
static void
normalize_d_s_us(int *d, int *s, int *us)
{
- if (*us < 0 || *us >= 1000000) {
- normalize_pair(s, us, 1000000);
- /* |s| can't be bigger than about
- * |original s| + |original us|/1000000 now.
- */
+ if (*us < 0 || *us >= 1000000) {
+ normalize_pair(s, us, 1000000);
+ /* |s| can't be bigger than about
+ * |original s| + |original us|/1000000 now.
+ */
- }
- if (*s < 0 || *s >= 24*3600) {
- normalize_pair(d, s, 24*3600);
- /* |d| can't be bigger than about
- * |original d| +
- * (|original s| + |original us|/1000000) / (24*3600) now.
- */
- }
- assert(0 <= *s && *s < 24*3600);
- assert(0 <= *us && *us < 1000000);
+ }
+ if (*s < 0 || *s >= 24*3600) {
+ normalize_pair(d, s, 24*3600);
+ /* |d| can't be bigger than about
+ * |original d| +
+ * (|original s| + |original us|/1000000) / (24*3600) now.
+ */
+ }
+ assert(0 <= *s && *s < 24*3600);
+ assert(0 <= *us && *us < 1000000);
}
/* Fiddle years (y), months (m), and days (d) so that
- * 1 <= *m <= 12
- * 1 <= *d <= days_in_month(*y, *m)
+ * 1 <= *m <= 12
+ * 1 <= *d <= days_in_month(*y, *m)
* The input values must be such that the internals don't overflow.
* The way this routine is used, we don't get close.
*/
static void
normalize_y_m_d(int *y, int *m, int *d)
{
- int dim; /* # of days in month */
-
- /* This gets muddy: the proper range for day can't be determined
- * without knowing the correct month and year, but if day is, e.g.,
- * plus or minus a million, the current month and year values make
- * no sense (and may also be out of bounds themselves).
- * Saying 12 months == 1 year should be non-controversial.
- */
- if (*m < 1 || *m > 12) {
- --*m;
- normalize_pair(y, m, 12);
- ++*m;
- /* |y| can't be bigger than about
- * |original y| + |original m|/12 now.
- */
- }
- assert(1 <= *m && *m <= 12);
-
- /* Now only day can be out of bounds (year may also be out of bounds
- * for a datetime object, but we don't care about that here).
- * If day is out of bounds, what to do is arguable, but at least the
- * method here is principled and explainable.
- */
- dim = days_in_month(*y, *m);
- if (*d < 1 || *d > dim) {
- /* Move day-1 days from the first of the month. First try to
- * get off cheap if we're only one day out of range
- * (adjustments for timezone alone can't be worse than that).
- */
- if (*d == 0) {
- --*m;
- if (*m > 0)
- *d = days_in_month(*y, *m);
- else {
- --*y;
- *m = 12;
- *d = 31;
- }
- }
- else if (*d == dim + 1) {
- /* move forward a day */
- ++*m;
- *d = 1;
- if (*m > 12) {
- *m = 1;
- ++*y;
- }
- }
- else {
- int ordinal = ymd_to_ord(*y, *m, 1) +
- *d - 1;
- ord_to_ymd(ordinal, y, m, d);
- }
- }
- assert(*m > 0);
- assert(*d > 0);
+ int dim; /* # of days in month */
+
+ /* This gets muddy: the proper range for day can't be determined
+ * without knowing the correct month and year, but if day is, e.g.,
+ * plus or minus a million, the current month and year values make
+ * no sense (and may also be out of bounds themselves).
+ * Saying 12 months == 1 year should be non-controversial.
+ */
+ if (*m < 1 || *m > 12) {
+ --*m;
+ normalize_pair(y, m, 12);
+ ++*m;
+ /* |y| can't be bigger than about
+ * |original y| + |original m|/12 now.
+ */
+ }
+ assert(1 <= *m && *m <= 12);
+
+ /* Now only day can be out of bounds (year may also be out of bounds
+ * for a datetime object, but we don't care about that here).
+ * If day is out of bounds, what to do is arguable, but at least the
+ * method here is principled and explainable.
+ */
+ dim = days_in_month(*y, *m);
+ if (*d < 1 || *d > dim) {
+ /* Move day-1 days from the first of the month. First try to
+ * get off cheap if we're only one day out of range
+ * (adjustments for timezone alone can't be worse than that).
+ */
+ if (*d == 0) {
+ --*m;
+ if (*m > 0)
+ *d = days_in_month(*y, *m);
+ else {
+ --*y;
+ *m = 12;
+ *d = 31;
+ }
+ }
+ else if (*d == dim + 1) {
+ /* move forward a day */
+ ++*m;
+ *d = 1;
+ if (*m > 12) {
+ *m = 1;
+ ++*y;
+ }
+ }
+ else {
+ int ordinal = ymd_to_ord(*y, *m, 1) +
+ *d - 1;
+ ord_to_ymd(ordinal, y, m, d);
+ }
+ }
+ assert(*m > 0);
+ assert(*d > 0);
}
/* Fiddle out-of-bounds months and days so that the result makes some kind
@@ -548,17 +548,17 @@ normalize_y_m_d(int *y, int *m, int *d)
static int
normalize_date(int *year, int *month, int *day)
{
- int result;
+ int result;
- normalize_y_m_d(year, month, day);
- if (MINYEAR <= *year && *year <= MAXYEAR)
- result = 0;
- else {
- PyErr_SetString(PyExc_OverflowError,
- "date value out of range");
- result = -1;
- }
- return result;
+ normalize_y_m_d(year, month, day);
+ if (MINYEAR <= *year && *year <= MAXYEAR)
+ result = 0;
+ else {
+ PyErr_SetString(PyExc_OverflowError,
+ "date value out of range");
+ result = -1;
+ }
+ return result;
}
/* Force all the datetime fields into range. The parameters are both
@@ -569,11 +569,11 @@ normalize_datetime(int *year, int *month, int *day,
int *hour, int *minute, int *second,
int *microsecond)
{
- normalize_pair(second, microsecond, 1000000);
- normalize_pair(minute, second, 60);
- normalize_pair(hour, minute, 60);
- normalize_pair(day, hour, 24);
- return normalize_date(year, month, day);
+ normalize_pair(second, microsecond, 1000000);
+ normalize_pair(minute, second, 60);
+ normalize_pair(hour, minute, 60);
+ normalize_pair(day, hour, 24);
+ return normalize_date(year, month, day);
}
/* ---------------------------------------------------------------------------
@@ -600,31 +600,31 @@ normalize_datetime(int *year, int *month, int *day,
static PyObject *
time_alloc(PyTypeObject *type, Py_ssize_t aware)
{
- PyObject *self;
+ PyObject *self;
- self = (PyObject *)
- PyObject_MALLOC(aware ?
- sizeof(PyDateTime_Time) :
- sizeof(_PyDateTime_BaseTime));
- if (self == NULL)
- return (PyObject *)PyErr_NoMemory();
- PyObject_INIT(self, type);
- return self;
+ self = (PyObject *)
+ PyObject_MALLOC(aware ?
+ sizeof(PyDateTime_Time) :
+ sizeof(_PyDateTime_BaseTime));
+ if (self == NULL)
+ return (PyObject *)PyErr_NoMemory();
+ PyObject_INIT(self, type);
+ return self;
}
static PyObject *
datetime_alloc(PyTypeObject *type, Py_ssize_t aware)
{
- PyObject *self;
+ PyObject *self;
- self = (PyObject *)
- PyObject_MALLOC(aware ?
- sizeof(PyDateTime_DateTime) :
- sizeof(_PyDateTime_BaseDateTime));
- if (self == NULL)
- return (PyObject *)PyErr_NoMemory();
- PyObject_INIT(self, type);
- return self;
+ self = (PyObject *)
+ PyObject_MALLOC(aware ?
+ sizeof(PyDateTime_DateTime) :
+ sizeof(_PyDateTime_BaseDateTime));
+ if (self == NULL)
+ return (PyObject *)PyErr_NoMemory();
+ PyObject_INIT(self, type);
+ return self;
}
/* ---------------------------------------------------------------------------
@@ -636,10 +636,10 @@ datetime_alloc(PyTypeObject *type, Py_ssize_t aware)
static void
set_date_fields(PyDateTime_Date *self, int y, int m, int d)
{
- self->hashcode = -1;
- SET_YEAR(self, y);
- SET_MONTH(self, m);
- SET_DAY(self, d);
+ self->hashcode = -1;
+ SET_YEAR(self, y);
+ SET_MONTH(self, m);
+ SET_DAY(self, d);
}
/* ---------------------------------------------------------------------------
@@ -650,71 +650,71 @@ set_date_fields(PyDateTime_Date *self, int y, int m, int d)
static PyObject *
new_date_ex(int year, int month, int day, PyTypeObject *type)
{
- PyDateTime_Date *self;
+ PyDateTime_Date *self;
- self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
- if (self != NULL)
- set_date_fields(self, year, month, day);
- return (PyObject *) self;
+ self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
+ if (self != NULL)
+ set_date_fields(self, year, month, day);
+ return (PyObject *) self;
}
#define new_date(year, month, day) \
- new_date_ex(year, month, day, &PyDateTime_DateType)
+ new_date_ex(year, month, day, &PyDateTime_DateType)
/* Create a datetime instance with no range checking. */
static PyObject *
new_datetime_ex(int year, int month, int day, int hour, int minute,
- int second, int usecond, PyObject *tzinfo, PyTypeObject *type)
-{
- PyDateTime_DateTime *self;
- char aware = tzinfo != Py_None;
-
- self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware));
- if (self != NULL) {
- self->hastzinfo = aware;
- set_date_fields((PyDateTime_Date *)self, year, month, day);
- DATE_SET_HOUR(self, hour);
- DATE_SET_MINUTE(self, minute);
- DATE_SET_SECOND(self, second);
- DATE_SET_MICROSECOND(self, usecond);
- if (aware) {
- Py_INCREF(tzinfo);
- self->tzinfo = tzinfo;
- }
- }
- return (PyObject *)self;
-}
-
-#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \
- new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \
- &PyDateTime_DateTimeType)
+ int second, int usecond, PyObject *tzinfo, PyTypeObject *type)
+{
+ PyDateTime_DateTime *self;
+ char aware = tzinfo != Py_None;
+
+ self = (PyDateTime_DateTime *) (type->tp_alloc(type, aware));
+ if (self != NULL) {
+ self->hastzinfo = aware;
+ set_date_fields((PyDateTime_Date *)self, year, month, day);
+ DATE_SET_HOUR(self, hour);
+ DATE_SET_MINUTE(self, minute);
+ DATE_SET_SECOND(self, second);
+ DATE_SET_MICROSECOND(self, usecond);
+ if (aware) {
+ Py_INCREF(tzinfo);
+ self->tzinfo = tzinfo;
+ }
+ }
+ return (PyObject *)self;
+}
+
+#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \
+ new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \
+ &PyDateTime_DateTimeType)
/* Create a time instance with no range checking. */
static PyObject *
new_time_ex(int hour, int minute, int second, int usecond,
- PyObject *tzinfo, PyTypeObject *type)
-{
- PyDateTime_Time *self;
- char aware = tzinfo != Py_None;
-
- self = (PyDateTime_Time *) (type->tp_alloc(type, aware));
- if (self != NULL) {
- self->hastzinfo = aware;
- self->hashcode = -1;
- TIME_SET_HOUR(self, hour);
- TIME_SET_MINUTE(self, minute);
- TIME_SET_SECOND(self, second);
- TIME_SET_MICROSECOND(self, usecond);
- if (aware) {
- Py_INCREF(tzinfo);
- self->tzinfo = tzinfo;
- }
- }
- return (PyObject *)self;
-}
-
-#define new_time(hh, mm, ss, us, tzinfo) \
- new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType)
+ PyObject *tzinfo, PyTypeObject *type)
+{
+ PyDateTime_Time *self;
+ char aware = tzinfo != Py_None;
+
+ self = (PyDateTime_Time *) (type->tp_alloc(type, aware));
+ if (self != NULL) {
+ self->hastzinfo = aware;
+ self->hashcode = -1;
+ TIME_SET_HOUR(self, hour);
+ TIME_SET_MINUTE(self, minute);
+ TIME_SET_SECOND(self, second);
+ TIME_SET_MICROSECOND(self, usecond);
+ if (aware) {
+ Py_INCREF(tzinfo);
+ self->tzinfo = tzinfo;
+ }
+ }
+ return (PyObject *)self;
+}
+
+#define new_time(hh, mm, ss, us, tzinfo) \
+ new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType)
/* Create a timedelta instance. Normalize the members iff normalize is
* true. Passing false is a speed optimization, if you know for sure
@@ -724,30 +724,30 @@ new_time_ex(int hour, int minute, int second, int usecond,
*/
static PyObject *
new_delta_ex(int days, int seconds, int microseconds, int normalize,
- PyTypeObject *type)
+ PyTypeObject *type)
{
- PyDateTime_Delta *self;
+ PyDateTime_Delta *self;
- if (normalize)
- normalize_d_s_us(&days, &seconds, &microseconds);
- assert(0 <= seconds && seconds < 24*3600);
- assert(0 <= microseconds && microseconds < 1000000);
+ if (normalize)
+ normalize_d_s_us(&days, &seconds, &microseconds);
+ assert(0 <= seconds && seconds < 24*3600);
+ assert(0 <= microseconds && microseconds < 1000000);
- if (check_delta_day_range(days) < 0)
- return NULL;
+ if (check_delta_day_range(days) < 0)
+ return NULL;
- self = (PyDateTime_Delta *) (type->tp_alloc(type, 0));
- if (self != NULL) {
- self->hashcode = -1;
- SET_TD_DAYS(self, days);
- SET_TD_SECONDS(self, seconds);
- SET_TD_MICROSECONDS(self, microseconds);
- }
- return (PyObject *) self;
+ self = (PyDateTime_Delta *) (type->tp_alloc(type, 0));
+ if (self != NULL) {
+ self->hashcode = -1;
+ SET_TD_DAYS(self, days);
+ SET_TD_SECONDS(self, seconds);
+ SET_TD_MICROSECONDS(self, microseconds);
+ }
+ return (PyObject *) self;
}
-#define new_delta(d, s, us, normalize) \
- new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType)
+#define new_delta(d, s, us, normalize) \
+ new_delta_ex(d, s, us, normalize, &PyDateTime_DeltaType)
/* ---------------------------------------------------------------------------
* tzinfo helpers.
@@ -759,13 +759,13 @@ new_delta_ex(int days, int seconds, int microseconds, int normalize,
static int
check_tzinfo_subclass(PyObject *p)
{
- if (p == Py_None || PyTZInfo_Check(p))
- return 0;
- PyErr_Format(PyExc_TypeError,
- "tzinfo argument must be None or of a tzinfo subclass, "
- "not type '%s'",
- Py_TYPE(p)->tp_name);
- return -1;
+ if (p == Py_None || PyTZInfo_Check(p))
+ return 0;
+ PyErr_Format(PyExc_TypeError,
+ "tzinfo argument must be None or of a tzinfo subclass, "
+ "not type '%s'",
+ Py_TYPE(p)->tp_name);
+ return -1;
}
/* Return tzinfo.methname(tzinfoarg), without any checking of results.
@@ -774,17 +774,17 @@ check_tzinfo_subclass(PyObject *p)
static PyObject *
call_tzinfo_method(PyObject *tzinfo, char *methname, PyObject *tzinfoarg)
{
- PyObject *result;
+ PyObject *result;
- assert(tzinfo && methname && tzinfoarg);
- assert(check_tzinfo_subclass(tzinfo) >= 0);
- if (tzinfo == Py_None) {
- result = Py_None;
- Py_INCREF(result);
- }
- else
- result = PyObject_CallMethod(tzinfo, methname, "O", tzinfoarg);
- return result;
+ assert(tzinfo && methname && tzinfoarg);
+ assert(check_tzinfo_subclass(tzinfo) >= 0);
+ if (tzinfo == Py_None) {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else
+ result = PyObject_CallMethod(tzinfo, methname, "O", tzinfoarg);
+ return result;
}
/* If self has a tzinfo member, return a BORROWED reference to it. Else
@@ -794,14 +794,14 @@ call_tzinfo_method(PyObject *tzinfo, char *methname, PyObject *tzinfoarg)
static PyObject *
get_tzinfo_member(PyObject *self)
{
- PyObject *tzinfo = NULL;
+ PyObject *tzinfo = NULL;
- if (PyDateTime_Check(self) && HASTZINFO(self))
- tzinfo = ((PyDateTime_DateTime *)self)->tzinfo;
- else if (PyTime_Check(self) && HASTZINFO(self))
- tzinfo = ((PyDateTime_Time *)self)->tzinfo;
+ if (PyDateTime_Check(self) && HASTZINFO(self))
+ tzinfo = ((PyDateTime_DateTime *)self)->tzinfo;
+ else if (PyTime_Check(self) && HASTZINFO(self))
+ tzinfo = ((PyDateTime_Time *)self)->tzinfo;
- return tzinfo;
+ return tzinfo;
}
/* Call getattr(tzinfo, name)(tzinfoarg), and extract an int from the
@@ -814,59 +814,59 @@ get_tzinfo_member(PyObject *self)
*/
static int
call_utc_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg,
- int *none)
-{
- PyObject *u;
- int result = -1;
-
- assert(tzinfo != NULL);
- assert(PyTZInfo_Check(tzinfo));
- assert(tzinfoarg != NULL);
-
- *none = 0;
- u = call_tzinfo_method(tzinfo, name, tzinfoarg);
- if (u == NULL)
- return -1;
-
- else if (u == Py_None) {
- result = 0;
- *none = 1;
- }
- else if (PyDelta_Check(u)) {
- const int days = GET_TD_DAYS(u);
- if (days < -1 || days > 0)
- result = 24*60; /* trigger ValueError below */
- else {
- /* next line can't overflow because we know days
- * is -1 or 0 now
- */
- int ss = days * 24 * 3600 + GET_TD_SECONDS(u);
- result = divmod(ss, 60, &ss);
- if (ss || GET_TD_MICROSECONDS(u)) {
- PyErr_Format(PyExc_ValueError,
- "tzinfo.%s() must return a "
- "whole number of minutes",
- name);
- result = -1;
- }
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "tzinfo.%s() must return None or "
- "timedelta, not '%s'",
- name, Py_TYPE(u)->tp_name);
- }
-
- Py_DECREF(u);
- if (result < -1439 || result > 1439) {
- PyErr_Format(PyExc_ValueError,
- "tzinfo.%s() returned %d; must be in "
- "-1439 .. 1439",
- name, result);
- result = -1;
- }
- return result;
+ int *none)
+{
+ PyObject *u;
+ int result = -1;
+
+ assert(tzinfo != NULL);
+ assert(PyTZInfo_Check(tzinfo));
+ assert(tzinfoarg != NULL);
+
+ *none = 0;
+ u = call_tzinfo_method(tzinfo, name, tzinfoarg);
+ if (u == NULL)
+ return -1;
+
+ else if (u == Py_None) {
+ result = 0;
+ *none = 1;
+ }
+ else if (PyDelta_Check(u)) {
+ const int days = GET_TD_DAYS(u);
+ if (days < -1 || days > 0)
+ result = 24*60; /* trigger ValueError below */
+ else {
+ /* next line can't overflow because we know days
+ * is -1 or 0 now
+ */
+ int ss = days * 24 * 3600 + GET_TD_SECONDS(u);
+ result = divmod(ss, 60, &ss);
+ if (ss || GET_TD_MICROSECONDS(u)) {
+ PyErr_Format(PyExc_ValueError,
+ "tzinfo.%s() must return a "
+ "whole number of minutes",
+ name);
+ result = -1;
+ }
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "tzinfo.%s() must return None or "
+ "timedelta, not '%s'",
+ name, Py_TYPE(u)->tp_name);
+ }
+
+ Py_DECREF(u);
+ if (result < -1439 || result > 1439) {
+ PyErr_Format(PyExc_ValueError,
+ "tzinfo.%s() returned %d; must be in "
+ "-1439 .. 1439",
+ name, result);
+ result = -1;
+ }
+ return result;
}
/* Call tzinfo.utcoffset(tzinfoarg), and extract an integer from the
@@ -880,34 +880,34 @@ call_utc_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg,
static int
call_utcoffset(PyObject *tzinfo, PyObject *tzinfoarg, int *none)
{
- return call_utc_tzinfo_method(tzinfo, "utcoffset", tzinfoarg, none);
+ return call_utc_tzinfo_method(tzinfo, "utcoffset", tzinfoarg, none);
}
/* Call tzinfo.name(tzinfoarg), and return the offset as a timedelta or None.
*/
static PyObject *
offset_as_timedelta(PyObject *tzinfo, char *name, PyObject *tzinfoarg) {
- PyObject *result;
-
- assert(tzinfo && name && tzinfoarg);
- if (tzinfo == Py_None) {
- result = Py_None;
- Py_INCREF(result);
- }
- else {
- int none;
- int offset = call_utc_tzinfo_method(tzinfo, name, tzinfoarg,
- &none);
- if (offset < 0 && PyErr_Occurred())
- return NULL;
- if (none) {
- result = Py_None;
- Py_INCREF(result);
- }
- else
- result = new_delta(0, offset * 60, 0, 1);
- }
- return result;
+ PyObject *result;
+
+ assert(tzinfo && name && tzinfoarg);
+ if (tzinfo == Py_None) {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else {
+ int none;
+ int offset = call_utc_tzinfo_method(tzinfo, name, tzinfoarg,
+ &none);
+ if (offset < 0 && PyErr_Occurred())
+ return NULL;
+ if (none) {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else
+ result = new_delta(0, offset * 60, 0, 1);
+ }
+ return result;
}
/* Call tzinfo.dst(tzinfoarg), and extract an integer from the
@@ -921,7 +921,7 @@ offset_as_timedelta(PyObject *tzinfo, char *name, PyObject *tzinfoarg) {
static int
call_dst(PyObject *tzinfo, PyObject *tzinfoarg, int *none)
{
- return call_utc_tzinfo_method(tzinfo, "dst", tzinfoarg, none);
+ return call_utc_tzinfo_method(tzinfo, "dst", tzinfoarg, none);
}
/* Call tzinfo.tzname(tzinfoarg), and return the result. tzinfo must be
@@ -933,55 +933,55 @@ call_dst(PyObject *tzinfo, PyObject *tzinfoarg, int *none)
static PyObject *
call_tzname(PyObject *tzinfo, PyObject *tzinfoarg)
{
- PyObject *result;
-
- assert(tzinfo != NULL);
- assert(check_tzinfo_subclass(tzinfo) >= 0);
- assert(tzinfoarg != NULL);
-
- if (tzinfo == Py_None) {
- result = Py_None;
- Py_INCREF(result);
- }
- else
- result = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
-
- if (result != NULL && result != Py_None) {
- if (!PyUnicode_Check(result)) {
- PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
- "return None or a string, not '%s'",
- Py_TYPE(result)->tp_name);
- Py_DECREF(result);
- result = NULL;
- }
- else if (!PyUnicode_Check(result)) {
- PyObject *temp = PyUnicode_FromObject(result);
- Py_DECREF(result);
- result = temp;
- }
- }
- return result;
+ PyObject *result;
+
+ assert(tzinfo != NULL);
+ assert(check_tzinfo_subclass(tzinfo) >= 0);
+ assert(tzinfoarg != NULL);
+
+ if (tzinfo == Py_None) {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else
+ result = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
+
+ if (result != NULL && result != Py_None) {
+ if (!PyUnicode_Check(result)) {
+ PyErr_Format(PyExc_TypeError, "tzinfo.tzname() must "
+ "return None or a string, not '%s'",
+ Py_TYPE(result)->tp_name);
+ Py_DECREF(result);
+ result = NULL;
+ }
+ else if (!PyUnicode_Check(result)) {
+ PyObject *temp = PyUnicode_FromObject(result);
+ Py_DECREF(result);
+ result = temp;
+ }
+ }
+ return result;
}
typedef enum {
- /* an exception has been set; the caller should pass it on */
- OFFSET_ERROR,
-
- /* type isn't date, datetime, or time subclass */
- OFFSET_UNKNOWN,
-
- /* date,
- * datetime with !hastzinfo
- * datetime with None tzinfo,
- * datetime where utcoffset() returns None
- * time with !hastzinfo
- * time with None tzinfo,
- * time where utcoffset() returns None
- */
- OFFSET_NAIVE,
-
- /* time or datetime where utcoffset() doesn't return None */
- OFFSET_AWARE
+ /* an exception has been set; the caller should pass it on */
+ OFFSET_ERROR,
+
+ /* type isn't date, datetime, or time subclass */
+ OFFSET_UNKNOWN,
+
+ /* date,
+ * datetime with !hastzinfo
+ * datetime with None tzinfo,
+ * datetime where utcoffset() returns None
+ * time with !hastzinfo
+ * time with None tzinfo,
+ * time where utcoffset() returns None
+ */
+ OFFSET_NAIVE,
+
+ /* time or datetime where utcoffset() doesn't return None */
+ OFFSET_AWARE
} naivety;
/* Classify an object as to whether it's naive or offset-aware. See
@@ -993,23 +993,23 @@ typedef enum {
static naivety
classify_utcoffset(PyObject *op, PyObject *tzinfoarg, int *offset)
{
- int none;
- PyObject *tzinfo;
-
- assert(tzinfoarg != NULL);
- *offset = 0;
- tzinfo = get_tzinfo_member(op); /* NULL means no tzinfo, not error */
- if (tzinfo == Py_None)
- return OFFSET_NAIVE;
- if (tzinfo == NULL) {
- /* note that a datetime passes the PyDate_Check test */
- return (PyTime_Check(op) || PyDate_Check(op)) ?
- OFFSET_NAIVE : OFFSET_UNKNOWN;
- }
- *offset = call_utcoffset(tzinfo, tzinfoarg, &none);
- if (*offset == -1 && PyErr_Occurred())
- return OFFSET_ERROR;
- return none ? OFFSET_NAIVE : OFFSET_AWARE;
+ int none;
+ PyObject *tzinfo;
+
+ assert(tzinfoarg != NULL);
+ *offset = 0;
+ tzinfo = get_tzinfo_member(op); /* NULL means no tzinfo, not error */
+ if (tzinfo == Py_None)
+ return OFFSET_NAIVE;
+ if (tzinfo == NULL) {
+ /* note that a datetime passes the PyDate_Check test */
+ return (PyTime_Check(op) || PyDate_Check(op)) ?
+ OFFSET_NAIVE : OFFSET_UNKNOWN;
+ }
+ *offset = call_utcoffset(tzinfo, tzinfoarg, &none);
+ if (*offset == -1 && PyErr_Occurred())
+ return OFFSET_ERROR;
+ return none ? OFFSET_NAIVE : OFFSET_AWARE;
}
/* Classify two objects as to whether they're naive or offset-aware.
@@ -1023,23 +1023,23 @@ classify_utcoffset(PyObject *op, PyObject *tzinfoarg, int *offset)
*/
static int
classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1,
- PyObject *tzinfoarg1,
- PyObject *o2, int *offset2, naivety *n2,
- PyObject *tzinfoarg2)
-{
- if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) {
- *offset1 = *offset2 = 0;
- *n1 = *n2 = OFFSET_NAIVE;
- }
- else {
- *n1 = classify_utcoffset(o1, tzinfoarg1, offset1);
- if (*n1 == OFFSET_ERROR)
- return -1;
- *n2 = classify_utcoffset(o2, tzinfoarg2, offset2);
- if (*n2 == OFFSET_ERROR)
- return -1;
- }
- return 0;
+ PyObject *tzinfoarg1,
+ PyObject *o2, int *offset2, naivety *n2,
+ PyObject *tzinfoarg2)
+{
+ if (get_tzinfo_member(o1) == get_tzinfo_member(o2)) {
+ *offset1 = *offset2 = 0;
+ *n1 = *n2 = OFFSET_NAIVE;
+ }
+ else {
+ *n1 = classify_utcoffset(o1, tzinfoarg1, offset1);
+ if (*n1 == OFFSET_ERROR)
+ return -1;
+ *n2 = classify_utcoffset(o2, tzinfoarg2, offset2);
+ if (*n2 == OFFSET_ERROR)
+ return -1;
+ }
+ return 0;
}
/* repr is like "someclass(arg1, arg2)". If tzinfo isn't None,
@@ -1050,22 +1050,22 @@ classify_two_utcoffsets(PyObject *o1, int *offset1, naivety *n1,
static PyObject *
append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
{
- PyObject *temp;
-
- assert(PyUnicode_Check(repr));
- assert(tzinfo);
- if (tzinfo == Py_None)
- return repr;
- /* Get rid of the trailing ')'. */
- assert(PyUnicode_AS_UNICODE(repr)[PyUnicode_GET_SIZE(repr)-1] == ')');
- temp = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(repr),
- PyUnicode_GET_SIZE(repr) - 1);
- Py_DECREF(repr);
- if (temp == NULL)
- return NULL;
- repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo);
- Py_DECREF(temp);
- return repr;
+ PyObject *temp;
+
+ assert(PyUnicode_Check(repr));
+ assert(tzinfo);
+ if (tzinfo == Py_None)
+ return repr;
+ /* Get rid of the trailing ')'. */
+ assert(PyUnicode_AS_UNICODE(repr)[PyUnicode_GET_SIZE(repr)-1] == ')');
+ temp = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(repr),
+ PyUnicode_GET_SIZE(repr) - 1);
+ Py_DECREF(repr);
+ if (temp == NULL)
+ return NULL;
+ repr = PyUnicode_FromFormat("%U, tzinfo=%R)", temp, tzinfo);
+ Py_DECREF(temp);
+ return repr;
}
/* ---------------------------------------------------------------------------
@@ -1075,20 +1075,20 @@ append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
static PyObject *
format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
{
- static const char *DayNames[] = {
- "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
- };
- static const char *MonthNames[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
+ static const char *DayNames[] = {
+ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
+ };
+ static const char *MonthNames[] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+ };
- int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date));
+ int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date));
- return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d",
- DayNames[wday], MonthNames[GET_MONTH(date)-1],
- GET_DAY(date), hours, minutes, seconds,
- GET_YEAR(date));
+ return PyUnicode_FromFormat("%s %s %2d %02d:%02d:%02d %04d",
+ DayNames[wday], MonthNames[GET_MONTH(date)-1],
+ GET_DAY(date), hours, minutes, seconds,
+ GET_YEAR(date));
}
/* Add an hours & minutes UTC offset string to buf. buf has no more than
@@ -1103,87 +1103,87 @@ format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
*/
static int
format_utcoffset(char *buf, size_t buflen, const char *sep,
- PyObject *tzinfo, PyObject *tzinfoarg)
-{
- int offset;
- int hours;
- int minutes;
- char sign;
- int none;
-
- assert(buflen >= 1);
-
- offset = call_utcoffset(tzinfo, tzinfoarg, &none);
- if (offset == -1 && PyErr_Occurred())
- return -1;
- if (none) {
- *buf = '\0';
- return 0;
- }
- sign = '+';
- if (offset < 0) {
- sign = '-';
- offset = - offset;
- }
- hours = divmod(offset, 60, &minutes);
- PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes);
- return 0;
+ PyObject *tzinfo, PyObject *tzinfoarg)
+{
+ int offset;
+ int hours;
+ int minutes;
+ char sign;
+ int none;
+
+ assert(buflen >= 1);
+
+ offset = call_utcoffset(tzinfo, tzinfoarg, &none);
+ if (offset == -1 && PyErr_Occurred())
+ return -1;
+ if (none) {
+ *buf = '\0';
+ return 0;
+ }
+ sign = '+';
+ if (offset < 0) {
+ sign = '-';
+ offset = - offset;
+ }
+ hours = divmod(offset, 60, &minutes);
+ PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes);
+ return 0;
}
static PyObject *
make_Zreplacement(PyObject *object, PyObject *tzinfoarg)
{
- PyObject *temp;
- PyObject *tzinfo = get_tzinfo_member(object);
- PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0);
- if (Zreplacement == NULL)
- return NULL;
- if (tzinfo == Py_None || tzinfo == NULL)
- return Zreplacement;
-
- assert(tzinfoarg != NULL);
- temp = call_tzname(tzinfo, tzinfoarg);
- if (temp == NULL)
- goto Error;
- if (temp == Py_None) {
- Py_DECREF(temp);
- return Zreplacement;
- }
-
- assert(PyUnicode_Check(temp));
- /* Since the tzname is getting stuffed into the
- * format, we have to double any % signs so that
- * strftime doesn't treat them as format codes.
- */
- Py_DECREF(Zreplacement);
- Zreplacement = PyObject_CallMethod(temp, "replace", "ss", "%", "%%");
- Py_DECREF(temp);
- if (Zreplacement == NULL)
- return NULL;
- if (!PyUnicode_Check(Zreplacement)) {
- PyErr_SetString(PyExc_TypeError,
- "tzname.replace() did not return a string");
- goto Error;
- }
- return Zreplacement;
+ PyObject *temp;
+ PyObject *tzinfo = get_tzinfo_member(object);
+ PyObject *Zreplacement = PyUnicode_FromStringAndSize(NULL, 0);
+ if (Zreplacement == NULL)
+ return NULL;
+ if (tzinfo == Py_None || tzinfo == NULL)
+ return Zreplacement;
+
+ assert(tzinfoarg != NULL);
+ temp = call_tzname(tzinfo, tzinfoarg);
+ if (temp == NULL)
+ goto Error;
+ if (temp == Py_None) {
+ Py_DECREF(temp);
+ return Zreplacement;
+ }
+
+ assert(PyUnicode_Check(temp));
+ /* Since the tzname is getting stuffed into the
+ * format, we have to double any % signs so that
+ * strftime doesn't treat them as format codes.
+ */
+ Py_DECREF(Zreplacement);
+ Zreplacement = PyObject_CallMethod(temp, "replace", "ss", "%", "%%");
+ Py_DECREF(temp);
+ if (Zreplacement == NULL)
+ return NULL;
+ if (!PyUnicode_Check(Zreplacement)) {
+ PyErr_SetString(PyExc_TypeError,
+ "tzname.replace() did not return a string");
+ goto Error;
+ }
+ return Zreplacement;
Error:
- Py_DECREF(Zreplacement);
- return NULL;
+ Py_DECREF(Zreplacement);
+ return NULL;
}
static PyObject *
make_freplacement(PyObject *object)
{
- char freplacement[64];
- if (PyTime_Check(object))
- sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object));
- else if (PyDateTime_Check(object))
- sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object));
- else
- sprintf(freplacement, "%06d", 0);
+ char freplacement[64];
+ if (PyTime_Check(object))
+ sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object));
+ else if (PyDateTime_Check(object))
+ sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object));
+ else
+ sprintf(freplacement, "%06d", 0);
- return PyBytes_FromStringAndSize(freplacement, strlen(freplacement));
+ return PyBytes_FromStringAndSize(freplacement, strlen(freplacement));
}
/* I sure don't want to reproduce the strftime code from the time module,
@@ -1195,190 +1195,190 @@ make_freplacement(PyObject *object)
*/
static PyObject *
wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
- PyObject *tzinfoarg)
-{
- PyObject *result = NULL; /* guilty until proved innocent */
-
- PyObject *zreplacement = NULL; /* py string, replacement for %z */
- PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
- PyObject *freplacement = NULL; /* py string, replacement for %f */
-
- const char *pin; /* pointer to next char in input format */
- Py_ssize_t flen; /* length of input format */
- char ch; /* next char in input format */
-
- PyObject *newfmt = NULL; /* py string, the output format */
- char *pnew; /* pointer to available byte in output format */
- size_t totalnew; /* number bytes total in output format buffer,
- exclusive of trailing \0 */
- size_t usednew; /* number bytes used so far in output format buffer */
-
- const char *ptoappend; /* ptr to string to append to output buffer */
- Py_ssize_t ntoappend; /* # of bytes to append to output buffer */
-
- assert(object && format && timetuple);
- assert(PyUnicode_Check(format));
- /* Convert the input format to a C string and size */
- pin = _PyUnicode_AsStringAndSize(format, &flen);
- if (!pin)
- return NULL;
-
- /* Give up if the year is before 1900.
- * Python strftime() plays games with the year, and different
- * games depending on whether envar PYTHON2K is set. This makes
- * years before 1900 a nightmare, even if the platform strftime
- * supports them (and not all do).
- * We could get a lot farther here by avoiding Python's strftime
- * wrapper and calling the C strftime() directly, but that isn't
- * an option in the Python implementation of this module.
- */
- {
- long year;
- PyObject *pyyear = PySequence_GetItem(timetuple, 0);
- if (pyyear == NULL) return NULL;
- assert(PyLong_Check(pyyear));
- year = PyLong_AsLong(pyyear);
- Py_DECREF(pyyear);
- if (year < 1900) {
- PyErr_Format(PyExc_ValueError, "year=%ld is before "
- "1900; the datetime strftime() "
- "methods require year >= 1900",
- year);
- return NULL;
- }
- }
-
- /* Scan the input format, looking for %z/%Z/%f escapes, building
- * a new format. Since computing the replacements for those codes
- * is expensive, don't unless they're actually used.
- */
- if (flen > INT_MAX - 1) {
- PyErr_NoMemory();
- goto Done;
- }
-
- totalnew = flen + 1; /* realistic if no %z/%Z */
- newfmt = PyBytes_FromStringAndSize(NULL, totalnew);
- if (newfmt == NULL) goto Done;
- pnew = PyBytes_AsString(newfmt);
- usednew = 0;
-
- while ((ch = *pin++) != '\0') {
- if (ch != '%') {
- ptoappend = pin - 1;
- ntoappend = 1;
- }
- else if ((ch = *pin++) == '\0') {
- /* There's a lone trailing %; doesn't make sense. */
- PyErr_SetString(PyExc_ValueError, "strftime format "
- "ends with raw %");
- goto Done;
- }
- /* A % has been seen and ch is the character after it. */
- else if (ch == 'z') {
- if (zreplacement == NULL) {
- /* format utcoffset */
- char buf[100];
- PyObject *tzinfo = get_tzinfo_member(object);
- zreplacement = PyBytes_FromStringAndSize("", 0);
- if (zreplacement == NULL) goto Done;
- if (tzinfo != Py_None && tzinfo != NULL) {
- assert(tzinfoarg != NULL);
- if (format_utcoffset(buf,
- sizeof(buf),
- "",
- tzinfo,
- tzinfoarg) < 0)
- goto Done;
- Py_DECREF(zreplacement);
- zreplacement =
- PyBytes_FromStringAndSize(buf,
- strlen(buf));
- if (zreplacement == NULL)
- goto Done;
- }
- }
- assert(zreplacement != NULL);
- ptoappend = PyBytes_AS_STRING(zreplacement);
- ntoappend = PyBytes_GET_SIZE(zreplacement);
- }
- else if (ch == 'Z') {
- /* format tzname */
- if (Zreplacement == NULL) {
- Zreplacement = make_Zreplacement(object,
- tzinfoarg);
- if (Zreplacement == NULL)
- goto Done;
- }
- assert(Zreplacement != NULL);
- assert(PyUnicode_Check(Zreplacement));
- ptoappend = _PyUnicode_AsStringAndSize(Zreplacement,
- &ntoappend);
- ntoappend = Py_SIZE(Zreplacement);
- }
- else if (ch == 'f') {
- /* format microseconds */
- if (freplacement == NULL) {
- freplacement = make_freplacement(object);
- if (freplacement == NULL)
- goto Done;
- }
- assert(freplacement != NULL);
- assert(PyBytes_Check(freplacement));
- ptoappend = PyBytes_AS_STRING(freplacement);
- ntoappend = PyBytes_GET_SIZE(freplacement);
- }
- else {
- /* percent followed by neither z nor Z */
- ptoappend = pin - 2;
- ntoappend = 2;
- }
-
- /* Append the ntoappend chars starting at ptoappend to
- * the new format.
- */
- if (ntoappend == 0)
- continue;
- assert(ptoappend != NULL);
- assert(ntoappend > 0);
- while (usednew + ntoappend > totalnew) {
- size_t bigger = totalnew << 1;
- if ((bigger >> 1) != totalnew) { /* overflow */
- PyErr_NoMemory();
- goto Done;
- }
- if (_PyBytes_Resize(&newfmt, bigger) < 0)
- goto Done;
- totalnew = bigger;
- pnew = PyBytes_AsString(newfmt) + usednew;
- }
- memcpy(pnew, ptoappend, ntoappend);
- pnew += ntoappend;
- usednew += ntoappend;
- assert(usednew <= totalnew);
- } /* end while() */
-
- if (_PyBytes_Resize(&newfmt, usednew) < 0)
- goto Done;
- {
- PyObject *format;
- PyObject *time = PyImport_ImportModuleNoBlock("time");
- if (time == NULL)
- goto Done;
- format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
- if (format != NULL) {
- result = PyObject_CallMethod(time, "strftime", "OO",
- format, timetuple);
- Py_DECREF(format);
- }
- Py_DECREF(time);
- }
+ PyObject *tzinfoarg)
+{
+ PyObject *result = NULL; /* guilty until proved innocent */
+
+ PyObject *zreplacement = NULL; /* py string, replacement for %z */
+ PyObject *Zreplacement = NULL; /* py string, replacement for %Z */
+ PyObject *freplacement = NULL; /* py string, replacement for %f */
+
+ const char *pin; /* pointer to next char in input format */
+ Py_ssize_t flen; /* length of input format */
+ char ch; /* next char in input format */
+
+ PyObject *newfmt = NULL; /* py string, the output format */
+ char *pnew; /* pointer to available byte in output format */
+ size_t totalnew; /* number bytes total in output format buffer,
+ exclusive of trailing \0 */
+ size_t usednew; /* number bytes used so far in output format buffer */
+
+ const char *ptoappend; /* ptr to string to append to output buffer */
+ Py_ssize_t ntoappend; /* # of bytes to append to output buffer */
+
+ assert(object && format && timetuple);
+ assert(PyUnicode_Check(format));
+ /* Convert the input format to a C string and size */
+ pin = _PyUnicode_AsStringAndSize(format, &flen);
+ if (!pin)
+ return NULL;
+
+ /* Give up if the year is before 1900.
+ * Python strftime() plays games with the year, and different
+ * games depending on whether envar PYTHON2K is set. This makes
+ * years before 1900 a nightmare, even if the platform strftime
+ * supports them (and not all do).
+ * We could get a lot farther here by avoiding Python's strftime
+ * wrapper and calling the C strftime() directly, but that isn't
+ * an option in the Python implementation of this module.
+ */
+ {
+ long year;
+ PyObject *pyyear = PySequence_GetItem(timetuple, 0);
+ if (pyyear == NULL) return NULL;
+ assert(PyLong_Check(pyyear));
+ year = PyLong_AsLong(pyyear);
+ Py_DECREF(pyyear);
+ if (year < 1900) {
+ PyErr_Format(PyExc_ValueError, "year=%ld is before "
+ "1900; the datetime strftime() "
+ "methods require year >= 1900",
+ year);
+ return NULL;
+ }
+ }
+
+ /* Scan the input format, looking for %z/%Z/%f escapes, building
+ * a new format. Since computing the replacements for those codes
+ * is expensive, don't unless they're actually used.
+ */
+ if (flen > INT_MAX - 1) {
+ PyErr_NoMemory();
+ goto Done;
+ }
+
+ totalnew = flen + 1; /* realistic if no %z/%Z */
+ newfmt = PyBytes_FromStringAndSize(NULL, totalnew);
+ if (newfmt == NULL) goto Done;
+ pnew = PyBytes_AsString(newfmt);
+ usednew = 0;
+
+ while ((ch = *pin++) != '\0') {
+ if (ch != '%') {
+ ptoappend = pin - 1;
+ ntoappend = 1;
+ }
+ else if ((ch = *pin++) == '\0') {
+ /* There's a lone trailing %; doesn't make sense. */
+ PyErr_SetString(PyExc_ValueError, "strftime format "
+ "ends with raw %");
+ goto Done;
+ }
+ /* A % has been seen and ch is the character after it. */
+ else if (ch == 'z') {
+ if (zreplacement == NULL) {
+ /* format utcoffset */
+ char buf[100];
+ PyObject *tzinfo = get_tzinfo_member(object);
+ zreplacement = PyBytes_FromStringAndSize("", 0);
+ if (zreplacement == NULL) goto Done;
+ if (tzinfo != Py_None && tzinfo != NULL) {
+ assert(tzinfoarg != NULL);
+ if (format_utcoffset(buf,
+ sizeof(buf),
+ "",
+ tzinfo,
+ tzinfoarg) < 0)
+ goto Done;
+ Py_DECREF(zreplacement);
+ zreplacement =
+ PyBytes_FromStringAndSize(buf,
+ strlen(buf));
+ if (zreplacement == NULL)
+ goto Done;
+ }
+ }
+ assert(zreplacement != NULL);
+ ptoappend = PyBytes_AS_STRING(zreplacement);
+ ntoappend = PyBytes_GET_SIZE(zreplacement);
+ }
+ else if (ch == 'Z') {
+ /* format tzname */
+ if (Zreplacement == NULL) {
+ Zreplacement = make_Zreplacement(object,
+ tzinfoarg);
+ if (Zreplacement == NULL)
+ goto Done;
+ }
+ assert(Zreplacement != NULL);
+ assert(PyUnicode_Check(Zreplacement));
+ ptoappend = _PyUnicode_AsStringAndSize(Zreplacement,
+ &ntoappend);
+ ntoappend = Py_SIZE(Zreplacement);
+ }
+ else if (ch == 'f') {
+ /* format microseconds */
+ if (freplacement == NULL) {
+ freplacement = make_freplacement(object);
+ if (freplacement == NULL)
+ goto Done;
+ }
+ assert(freplacement != NULL);
+ assert(PyBytes_Check(freplacement));
+ ptoappend = PyBytes_AS_STRING(freplacement);
+ ntoappend = PyBytes_GET_SIZE(freplacement);
+ }
+ else {
+ /* percent followed by neither z nor Z */
+ ptoappend = pin - 2;
+ ntoappend = 2;
+ }
+
+ /* Append the ntoappend chars starting at ptoappend to
+ * the new format.
+ */
+ if (ntoappend == 0)
+ continue;
+ assert(ptoappend != NULL);
+ assert(ntoappend > 0);
+ while (usednew + ntoappend > totalnew) {
+ size_t bigger = totalnew << 1;
+ if ((bigger >> 1) != totalnew) { /* overflow */
+ PyErr_NoMemory();
+ goto Done;
+ }
+ if (_PyBytes_Resize(&newfmt, bigger) < 0)
+ goto Done;
+ totalnew = bigger;
+ pnew = PyBytes_AsString(newfmt) + usednew;
+ }
+ memcpy(pnew, ptoappend, ntoappend);
+ pnew += ntoappend;
+ usednew += ntoappend;
+ assert(usednew <= totalnew);
+ } /* end while() */
+
+ if (_PyBytes_Resize(&newfmt, usednew) < 0)
+ goto Done;
+ {
+ PyObject *format;
+ PyObject *time = PyImport_ImportModuleNoBlock("time");
+ if (time == NULL)
+ goto Done;
+ format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
+ if (format != NULL) {
+ result = PyObject_CallMethod(time, "strftime", "OO",
+ format, timetuple);
+ Py_DECREF(format);
+ }
+ Py_DECREF(time);
+ }
Done:
- Py_XDECREF(freplacement);
- Py_XDECREF(zreplacement);
- Py_XDECREF(Zreplacement);
- Py_XDECREF(newfmt);
- return result;
+ Py_XDECREF(freplacement);
+ Py_XDECREF(zreplacement);
+ Py_XDECREF(Zreplacement);
+ Py_XDECREF(newfmt);
+ return result;
}
/* ---------------------------------------------------------------------------
@@ -1390,14 +1390,14 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
static PyObject *
time_time(void)
{
- PyObject *result = NULL;
- PyObject *time = PyImport_ImportModuleNoBlock("time");
+ PyObject *result = NULL;
+ PyObject *time = PyImport_ImportModuleNoBlock("time");
- if (time != NULL) {
- result = PyObject_CallMethod(time, "time", "()");
- Py_DECREF(time);
- }
- return result;
+ if (time != NULL) {
+ result = PyObject_CallMethod(time, "time", "()");
+ Py_DECREF(time);
+ }
+ return result;
}
/* Build a time.struct_time. The weekday and day number are automatically
@@ -1406,21 +1406,21 @@ time_time(void)
static PyObject *
build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
{
- PyObject *time;
- PyObject *result = NULL;
+ PyObject *time;
+ PyObject *result = NULL;
- time = PyImport_ImportModuleNoBlock("time");
- if (time != NULL) {
- result = PyObject_CallMethod(time, "struct_time",
- "((iiiiiiiii))",
- y, m, d,
- hh, mm, ss,
- weekday(y, m, d),
- days_before_month(y, m) + d,
- dstflag);
- Py_DECREF(time);
- }
- return result;
+ time = PyImport_ImportModuleNoBlock("time");
+ if (time != NULL) {
+ result = PyObject_CallMethod(time, "struct_time",
+ "((iiiiiiiii))",
+ y, m, d,
+ hh, mm, ss,
+ weekday(y, m, d),
+ days_before_month(y, m) + d,
+ dstflag);
+ Py_DECREF(time);
+ }
+ return result;
}
/* ---------------------------------------------------------------------------
@@ -1434,33 +1434,33 @@ build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
static PyObject *
diff_to_bool(int diff, int op)
{
- PyObject *result;
- int istrue;
-
- switch (op) {
- case Py_EQ: istrue = diff == 0; break;
- case Py_NE: istrue = diff != 0; break;
- case Py_LE: istrue = diff <= 0; break;
- case Py_GE: istrue = diff >= 0; break;
- case Py_LT: istrue = diff < 0; break;
- case Py_GT: istrue = diff > 0; break;
- default:
- assert(! "op unknown");
- istrue = 0; /* To shut up compiler */
- }
- result = istrue ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
+ PyObject *result;
+ int istrue;
+
+ switch (op) {
+ case Py_EQ: istrue = diff == 0; break;
+ case Py_NE: istrue = diff != 0; break;
+ case Py_LE: istrue = diff <= 0; break;
+ case Py_GE: istrue = diff >= 0; break;
+ case Py_LT: istrue = diff < 0; break;
+ case Py_GT: istrue = diff > 0; break;
+ default:
+ assert(! "op unknown");
+ istrue = 0; /* To shut up compiler */
+ }
+ result = istrue ? Py_True : Py_False;
+ Py_INCREF(result);
+ return result;
}
/* Raises a "can't compare" TypeError and returns NULL. */
static PyObject *
cmperror(PyObject *a, PyObject *b)
{
- PyErr_Format(PyExc_TypeError,
- "can't compare %s to %s",
- Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
- return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "can't compare %s to %s",
+ Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
+ return NULL;
}
/* ---------------------------------------------------------------------------
@@ -1468,13 +1468,13 @@ cmperror(PyObject *a, PyObject *b)
*/
/* Conversion factors. */
-static PyObject *us_per_us = NULL; /* 1 */
-static PyObject *us_per_ms = NULL; /* 1000 */
-static PyObject *us_per_second = NULL; /* 1000000 */
-static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */
-static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */
-static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */
-static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */
+static PyObject *us_per_us = NULL; /* 1 */
+static PyObject *us_per_ms = NULL; /* 1000 */
+static PyObject *us_per_second = NULL; /* 1000000 */
+static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */
+static PyObject *us_per_hour = NULL; /* 1e6 * 3600 as Python long */
+static PyObject *us_per_day = NULL; /* 1e6 * 3600 * 24 as Python long */
+static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */
static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */
/* ---------------------------------------------------------------------------
@@ -1486,7 +1486,7 @@ static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */
*/
/* Convert a timedelta to a number of us,
- * (24*3600*self.days + self.seconds)*1000000 + self.microseconds
+ * (24*3600*self.days + self.seconds)*1000000 + self.microseconds
* as a Python int or long.
* Doing mixed-radix arithmetic by hand instead is excruciating in C,
* due to ubiquitous overflow possibilities.
@@ -1494,49 +1494,49 @@ static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */
static PyObject *
delta_to_microseconds(PyDateTime_Delta *self)
{
- PyObject *x1 = NULL;
- PyObject *x2 = NULL;
- PyObject *x3 = NULL;
- PyObject *result = NULL;
-
- x1 = PyLong_FromLong(GET_TD_DAYS(self));
- if (x1 == NULL)
- goto Done;
- x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */
- if (x2 == NULL)
- goto Done;
- Py_DECREF(x1);
- x1 = NULL;
-
- /* x2 has days in seconds */
- x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */
- if (x1 == NULL)
- goto Done;
- x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */
- if (x3 == NULL)
- goto Done;
- Py_DECREF(x1);
- Py_DECREF(x2);
- x1 = x2 = NULL;
-
- /* x3 has days+seconds in seconds */
- x1 = PyNumber_Multiply(x3, us_per_second); /* us */
- if (x1 == NULL)
- goto Done;
- Py_DECREF(x3);
- x3 = NULL;
-
- /* x1 has days+seconds in us */
- x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self));
- if (x2 == NULL)
- goto Done;
- result = PyNumber_Add(x1, x2);
+ PyObject *x1 = NULL;
+ PyObject *x2 = NULL;
+ PyObject *x3 = NULL;
+ PyObject *result = NULL;
+
+ x1 = PyLong_FromLong(GET_TD_DAYS(self));
+ if (x1 == NULL)
+ goto Done;
+ x2 = PyNumber_Multiply(x1, seconds_per_day); /* days in seconds */
+ if (x2 == NULL)
+ goto Done;
+ Py_DECREF(x1);
+ x1 = NULL;
+
+ /* x2 has days in seconds */
+ x1 = PyLong_FromLong(GET_TD_SECONDS(self)); /* seconds */
+ if (x1 == NULL)
+ goto Done;
+ x3 = PyNumber_Add(x1, x2); /* days and seconds in seconds */
+ if (x3 == NULL)
+ goto Done;
+ Py_DECREF(x1);
+ Py_DECREF(x2);
+ x1 = x2 = NULL;
+
+ /* x3 has days+seconds in seconds */
+ x1 = PyNumber_Multiply(x3, us_per_second); /* us */
+ if (x1 == NULL)
+ goto Done;
+ Py_DECREF(x3);
+ x3 = NULL;
+
+ /* x1 has days+seconds in us */
+ x2 = PyLong_FromLong(GET_TD_MICROSECONDS(self));
+ if (x2 == NULL)
+ goto Done;
+ result = PyNumber_Add(x1, x2);
Done:
- Py_XDECREF(x1);
- Py_XDECREF(x2);
- Py_XDECREF(x3);
- return result;
+ Py_XDECREF(x1);
+ Py_XDECREF(x2);
+ Py_XDECREF(x3);
+ return result;
}
/* Convert a number of us (as a Python int or long) to a timedelta.
@@ -1544,224 +1544,224 @@ Done:
static PyObject *
microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
{
- int us;
- int s;
- int d;
- long temp;
-
- PyObject *tuple = NULL;
- PyObject *num = NULL;
- PyObject *result = NULL;
-
- tuple = PyNumber_Divmod(pyus, us_per_second);
- if (tuple == NULL)
- goto Done;
-
- num = PyTuple_GetItem(tuple, 1); /* us */
- if (num == NULL)
- goto Done;
- temp = PyLong_AsLong(num);
- num = NULL;
- if (temp == -1 && PyErr_Occurred())
- goto Done;
- assert(0 <= temp && temp < 1000000);
- us = (int)temp;
- if (us < 0) {
- /* The divisor was positive, so this must be an error. */
- assert(PyErr_Occurred());
- goto Done;
- }
-
- num = PyTuple_GetItem(tuple, 0); /* leftover seconds */
- if (num == NULL)
- goto Done;
- Py_INCREF(num);
- Py_DECREF(tuple);
-
- tuple = PyNumber_Divmod(num, seconds_per_day);
- if (tuple == NULL)
- goto Done;
- Py_DECREF(num);
-
- num = PyTuple_GetItem(tuple, 1); /* seconds */
- if (num == NULL)
- goto Done;
- temp = PyLong_AsLong(num);
- num = NULL;
- if (temp == -1 && PyErr_Occurred())
- goto Done;
- assert(0 <= temp && temp < 24*3600);
- s = (int)temp;
-
- if (s < 0) {
- /* The divisor was positive, so this must be an error. */
- assert(PyErr_Occurred());
- goto Done;
- }
-
- num = PyTuple_GetItem(tuple, 0); /* leftover days */
- if (num == NULL)
- goto Done;
- Py_INCREF(num);
- temp = PyLong_AsLong(num);
- if (temp == -1 && PyErr_Occurred())
- goto Done;
- d = (int)temp;
- if ((long)d != temp) {
- PyErr_SetString(PyExc_OverflowError, "normalized days too "
- "large to fit in a C int");
- goto Done;
- }
- result = new_delta_ex(d, s, us, 0, type);
+ int us;
+ int s;
+ int d;
+ long temp;
+
+ PyObject *tuple = NULL;
+ PyObject *num = NULL;
+ PyObject *result = NULL;
+
+ tuple = PyNumber_Divmod(pyus, us_per_second);
+ if (tuple == NULL)
+ goto Done;
+
+ num = PyTuple_GetItem(tuple, 1); /* us */
+ if (num == NULL)
+ goto Done;
+ temp = PyLong_AsLong(num);
+ num = NULL;
+ if (temp == -1 && PyErr_Occurred())
+ goto Done;
+ assert(0 <= temp && temp < 1000000);
+ us = (int)temp;
+ if (us < 0) {
+ /* The divisor was positive, so this must be an error. */
+ assert(PyErr_Occurred());
+ goto Done;
+ }
+
+ num = PyTuple_GetItem(tuple, 0); /* leftover seconds */
+ if (num == NULL)
+ goto Done;
+ Py_INCREF(num);
+ Py_DECREF(tuple);
+
+ tuple = PyNumber_Divmod(num, seconds_per_day);
+ if (tuple == NULL)
+ goto Done;
+ Py_DECREF(num);
+
+ num = PyTuple_GetItem(tuple, 1); /* seconds */
+ if (num == NULL)
+ goto Done;
+ temp = PyLong_AsLong(num);
+ num = NULL;
+ if (temp == -1 && PyErr_Occurred())
+ goto Done;
+ assert(0 <= temp && temp < 24*3600);
+ s = (int)temp;
+
+ if (s < 0) {
+ /* The divisor was positive, so this must be an error. */
+ assert(PyErr_Occurred());
+ goto Done;
+ }
+
+ num = PyTuple_GetItem(tuple, 0); /* leftover days */
+ if (num == NULL)
+ goto Done;
+ Py_INCREF(num);
+ temp = PyLong_AsLong(num);
+ if (temp == -1 && PyErr_Occurred())
+ goto Done;
+ d = (int)temp;
+ if ((long)d != temp) {
+ PyErr_SetString(PyExc_OverflowError, "normalized days too "
+ "large to fit in a C int");
+ goto Done;
+ }
+ result = new_delta_ex(d, s, us, 0, type);
Done:
- Py_XDECREF(tuple);
- Py_XDECREF(num);
- return result;
+ Py_XDECREF(tuple);
+ Py_XDECREF(num);
+ return result;
}
-#define microseconds_to_delta(pymicros) \
- microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType)
+#define microseconds_to_delta(pymicros) \
+ microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType)
static PyObject *
multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
{
- PyObject *pyus_in;
- PyObject *pyus_out;
- PyObject *result;
+ PyObject *pyus_in;
+ PyObject *pyus_out;
+ PyObject *result;
- pyus_in = delta_to_microseconds(delta);
- if (pyus_in == NULL)
- return NULL;
+ pyus_in = delta_to_microseconds(delta);
+ if (pyus_in == NULL)
+ return NULL;
- pyus_out = PyNumber_Multiply(pyus_in, intobj);
- Py_DECREF(pyus_in);
- if (pyus_out == NULL)
- return NULL;
+ pyus_out = PyNumber_Multiply(pyus_in, intobj);
+ Py_DECREF(pyus_in);
+ if (pyus_out == NULL)
+ return NULL;
- result = microseconds_to_delta(pyus_out);
- Py_DECREF(pyus_out);
- return result;
+ result = microseconds_to_delta(pyus_out);
+ Py_DECREF(pyus_out);
+ return result;
}
static PyObject *
divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj)
{
- PyObject *pyus_in;
- PyObject *pyus_out;
- PyObject *result;
+ PyObject *pyus_in;
+ PyObject *pyus_out;
+ PyObject *result;
- pyus_in = delta_to_microseconds(delta);
- if (pyus_in == NULL)
- return NULL;
+ pyus_in = delta_to_microseconds(delta);
+ if (pyus_in == NULL)
+ return NULL;
- pyus_out = PyNumber_FloorDivide(pyus_in, intobj);
- Py_DECREF(pyus_in);
- if (pyus_out == NULL)
- return NULL;
+ pyus_out = PyNumber_FloorDivide(pyus_in, intobj);
+ Py_DECREF(pyus_in);
+ if (pyus_out == NULL)
+ return NULL;
- result = microseconds_to_delta(pyus_out);
- Py_DECREF(pyus_out);
- return result;
+ result = microseconds_to_delta(pyus_out);
+ Py_DECREF(pyus_out);
+ return result;
}
static PyObject *
delta_add(PyObject *left, PyObject *right)
{
- PyObject *result = Py_NotImplemented;
+ PyObject *result = Py_NotImplemented;
- if (PyDelta_Check(left) && PyDelta_Check(right)) {
- /* delta + delta */
- /* The C-level additions can't overflow because of the
- * invariant bounds.
- */
- int days = GET_TD_DAYS(left) + GET_TD_DAYS(right);
- int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right);
- int microseconds = GET_TD_MICROSECONDS(left) +
- GET_TD_MICROSECONDS(right);
- result = new_delta(days, seconds, microseconds, 1);
- }
+ if (PyDelta_Check(left) && PyDelta_Check(right)) {
+ /* delta + delta */
+ /* The C-level additions can't overflow because of the
+ * invariant bounds.
+ */
+ int days = GET_TD_DAYS(left) + GET_TD_DAYS(right);
+ int seconds = GET_TD_SECONDS(left) + GET_TD_SECONDS(right);
+ int microseconds = GET_TD_MICROSECONDS(left) +
+ GET_TD_MICROSECONDS(right);
+ result = new_delta(days, seconds, microseconds, 1);
+ }
- if (result == Py_NotImplemented)
- Py_INCREF(result);
- return result;
+ if (result == Py_NotImplemented)
+ Py_INCREF(result);
+ return result;
}
static PyObject *
delta_negative(PyDateTime_Delta *self)
{
- return new_delta(-GET_TD_DAYS(self),
- -GET_TD_SECONDS(self),
- -GET_TD_MICROSECONDS(self),
- 1);
+ return new_delta(-GET_TD_DAYS(self),
+ -GET_TD_SECONDS(self),
+ -GET_TD_MICROSECONDS(self),
+ 1);
}
static PyObject *
delta_positive(PyDateTime_Delta *self)
{
- /* Could optimize this (by returning self) if this isn't a
- * subclass -- but who uses unary + ? Approximately nobody.
- */
- return new_delta(GET_TD_DAYS(self),
- GET_TD_SECONDS(self),
- GET_TD_MICROSECONDS(self),
- 0);
+ /* Could optimize this (by returning self) if this isn't a
+ * subclass -- but who uses unary + ? Approximately nobody.
+ */
+ return new_delta(GET_TD_DAYS(self),
+ GET_TD_SECONDS(self),
+ GET_TD_MICROSECONDS(self),
+ 0);
}
static PyObject *
delta_abs(PyDateTime_Delta *self)
{
- PyObject *result;
+ PyObject *result;
- assert(GET_TD_MICROSECONDS(self) >= 0);
- assert(GET_TD_SECONDS(self) >= 0);
+ assert(GET_TD_MICROSECONDS(self) >= 0);
+ assert(GET_TD_SECONDS(self) >= 0);
- if (GET_TD_DAYS(self) < 0)
- result = delta_negative(self);
- else
- result = delta_positive(self);
+ if (GET_TD_DAYS(self) < 0)
+ result = delta_negative(self);
+ else
+ result = delta_positive(self);
- return result;
+ return result;
}
static PyObject *
delta_subtract(PyObject *left, PyObject *right)
{
- PyObject *result = Py_NotImplemented;
+ PyObject *result = Py_NotImplemented;
- if (PyDelta_Check(left) && PyDelta_Check(right)) {
- /* delta - delta */
- PyObject *minus_right = PyNumber_Negative(right);
- if (minus_right) {
- result = delta_add(left, minus_right);
- Py_DECREF(minus_right);
- }
- else
- result = NULL;
- }
+ if (PyDelta_Check(left) && PyDelta_Check(right)) {
+ /* delta - delta */
+ PyObject *minus_right = PyNumber_Negative(right);
+ if (minus_right) {
+ result = delta_add(left, minus_right);
+ Py_DECREF(minus_right);
+ }
+ else
+ result = NULL;
+ }
- if (result == Py_NotImplemented)
- Py_INCREF(result);
- return result;
+ if (result == Py_NotImplemented)
+ Py_INCREF(result);
+ return result;
}
static PyObject *
delta_richcompare(PyObject *self, PyObject *other, int op)
{
- if (PyDelta_Check(other)) {
- int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
- if (diff == 0) {
- diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
- if (diff == 0)
- diff = GET_TD_MICROSECONDS(self) -
- GET_TD_MICROSECONDS(other);
- }
- return diff_to_bool(diff, op);
- }
- else {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
+ if (PyDelta_Check(other)) {
+ int diff = GET_TD_DAYS(self) - GET_TD_DAYS(other);
+ if (diff == 0) {
+ diff = GET_TD_SECONDS(self) - GET_TD_SECONDS(other);
+ if (diff == 0)
+ diff = GET_TD_MICROSECONDS(self) -
+ GET_TD_MICROSECONDS(other);
+ }
+ return diff_to_bool(diff, op);
+ }
+ else {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
}
static PyObject *delta_getstate(PyDateTime_Delta *self);
@@ -1769,52 +1769,52 @@ static PyObject *delta_getstate(PyDateTime_Delta *self);
static long
delta_hash(PyDateTime_Delta *self)
{
- if (self->hashcode == -1) {
- PyObject *temp = delta_getstate(self);
- if (temp != NULL) {
- self->hashcode = PyObject_Hash(temp);
- Py_DECREF(temp);
- }
- }
- return self->hashcode;
+ if (self->hashcode == -1) {
+ PyObject *temp = delta_getstate(self);
+ if (temp != NULL) {
+ self->hashcode = PyObject_Hash(temp);
+ Py_DECREF(temp);
+ }
+ }
+ return self->hashcode;
}
static PyObject *
delta_multiply(PyObject *left, PyObject *right)
{
- PyObject *result = Py_NotImplemented;
+ PyObject *result = Py_NotImplemented;
- if (PyDelta_Check(left)) {
- /* delta * ??? */
- if (PyLong_Check(right))
- result = multiply_int_timedelta(right,
- (PyDateTime_Delta *) left);
- }
- else if (PyLong_Check(left))
- result = multiply_int_timedelta(left,
- (PyDateTime_Delta *) right);
+ if (PyDelta_Check(left)) {
+ /* delta * ??? */
+ if (PyLong_Check(right))
+ result = multiply_int_timedelta(right,
+ (PyDateTime_Delta *) left);
+ }
+ else if (PyLong_Check(left))
+ result = multiply_int_timedelta(left,
+ (PyDateTime_Delta *) right);
- if (result == Py_NotImplemented)
- Py_INCREF(result);
- return result;
+ if (result == Py_NotImplemented)
+ Py_INCREF(result);
+ return result;
}
static PyObject *
delta_divide(PyObject *left, PyObject *right)
{
- PyObject *result = Py_NotImplemented;
+ PyObject *result = Py_NotImplemented;
- if (PyDelta_Check(left)) {
- /* delta * ??? */
- if (PyLong_Check(right))
- result = divide_timedelta_int(
- (PyDateTime_Delta *)left,
- right);
- }
+ if (PyDelta_Check(left)) {
+ /* delta * ??? */
+ if (PyLong_Check(right))
+ result = divide_timedelta_int(
+ (PyDateTime_Delta *)left,
+ right);
+ }
- if (result == Py_NotImplemented)
- Py_INCREF(result);
- return result;
+ if (result == Py_NotImplemented)
+ Py_INCREF(result);
+ return result;
}
/* Fold in the value of the tag ("seconds", "weeks", etc) component of a
@@ -1830,166 +1830,166 @@ static PyObject *
accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
double *leftover)
{
- PyObject *prod;
- PyObject *sum;
-
- assert(num != NULL);
-
- if (PyLong_Check(num)) {
- prod = PyNumber_Multiply(num, factor);
- if (prod == NULL)
- return NULL;
- sum = PyNumber_Add(sofar, prod);
- Py_DECREF(prod);
- return sum;
- }
-
- if (PyFloat_Check(num)) {
- double dnum;
- double fracpart;
- double intpart;
- PyObject *x;
- PyObject *y;
-
- /* The Plan: decompose num into an integer part and a
- * fractional part, num = intpart + fracpart.
- * Then num * factor ==
- * intpart * factor + fracpart * factor
- * and the LHS can be computed exactly in long arithmetic.
- * The RHS is again broken into an int part and frac part.
- * and the frac part is added into *leftover.
- */
- dnum = PyFloat_AsDouble(num);
- if (dnum == -1.0 && PyErr_Occurred())
- return NULL;
- fracpart = modf(dnum, &intpart);
- x = PyLong_FromDouble(intpart);
- if (x == NULL)
- return NULL;
-
- prod = PyNumber_Multiply(x, factor);
- Py_DECREF(x);
- if (prod == NULL)
- return NULL;
-
- sum = PyNumber_Add(sofar, prod);
- Py_DECREF(prod);
- if (sum == NULL)
- return NULL;
-
- if (fracpart == 0.0)
- return sum;
- /* So far we've lost no information. Dealing with the
- * fractional part requires float arithmetic, and may
- * lose a little info.
- */
- assert(PyLong_Check(factor));
- dnum = PyLong_AsDouble(factor);
-
- dnum *= fracpart;
- fracpart = modf(dnum, &intpart);
- x = PyLong_FromDouble(intpart);
- if (x == NULL) {
- Py_DECREF(sum);
- return NULL;
- }
-
- y = PyNumber_Add(sum, x);
- Py_DECREF(sum);
- Py_DECREF(x);
- *leftover += fracpart;
- return y;
- }
-
- PyErr_Format(PyExc_TypeError,
- "unsupported type for timedelta %s component: %s",
- tag, Py_TYPE(num)->tp_name);
- return NULL;
+ PyObject *prod;
+ PyObject *sum;
+
+ assert(num != NULL);
+
+ if (PyLong_Check(num)) {
+ prod = PyNumber_Multiply(num, factor);
+ if (prod == NULL)
+ return NULL;
+ sum = PyNumber_Add(sofar, prod);
+ Py_DECREF(prod);
+ return sum;
+ }
+
+ if (PyFloat_Check(num)) {
+ double dnum;
+ double fracpart;
+ double intpart;
+ PyObject *x;
+ PyObject *y;
+
+ /* The Plan: decompose num into an integer part and a
+ * fractional part, num = intpart + fracpart.
+ * Then num * factor ==
+ * intpart * factor + fracpart * factor
+ * and the LHS can be computed exactly in long arithmetic.
+ * The RHS is again broken into an int part and frac part.
+ * and the frac part is added into *leftover.
+ */
+ dnum = PyFloat_AsDouble(num);
+ if (dnum == -1.0 && PyErr_Occurred())
+ return NULL;
+ fracpart = modf(dnum, &intpart);
+ x = PyLong_FromDouble(intpart);
+ if (x == NULL)
+ return NULL;
+
+ prod = PyNumber_Multiply(x, factor);
+ Py_DECREF(x);
+ if (prod == NULL)
+ return NULL;
+
+ sum = PyNumber_Add(sofar, prod);
+ Py_DECREF(prod);
+ if (sum == NULL)
+ return NULL;
+
+ if (fracpart == 0.0)
+ return sum;
+ /* So far we've lost no information. Dealing with the
+ * fractional part requires float arithmetic, and may
+ * lose a little info.
+ */
+ assert(PyLong_Check(factor));
+ dnum = PyLong_AsDouble(factor);
+
+ dnum *= fracpart;
+ fracpart = modf(dnum, &intpart);
+ x = PyLong_FromDouble(intpart);
+ if (x == NULL) {
+ Py_DECREF(sum);
+ return NULL;
+ }
+
+ y = PyNumber_Add(sum, x);
+ Py_DECREF(sum);
+ Py_DECREF(x);
+ *leftover += fracpart;
+ return y;
+ }
+
+ PyErr_Format(PyExc_TypeError,
+ "unsupported type for timedelta %s component: %s",
+ tag, Py_TYPE(num)->tp_name);
+ return NULL;
}
static PyObject *
delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *self = NULL;
-
- /* Argument objects. */
- PyObject *day = NULL;
- PyObject *second = NULL;
- PyObject *us = NULL;
- PyObject *ms = NULL;
- PyObject *minute = NULL;
- PyObject *hour = NULL;
- PyObject *week = NULL;
-
- PyObject *x = NULL; /* running sum of microseconds */
- PyObject *y = NULL; /* temp sum of microseconds */
- double leftover_us = 0.0;
-
- static char *keywords[] = {
- "days", "seconds", "microseconds", "milliseconds",
- "minutes", "hours", "weeks", NULL
- };
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__",
- keywords,
- &day, &second, &us,
- &ms, &minute, &hour, &week) == 0)
- goto Done;
-
- x = PyLong_FromLong(0);
- if (x == NULL)
- goto Done;
-
-#define CLEANUP \
- Py_DECREF(x); \
- x = y; \
- if (x == NULL) \
- goto Done
-
- if (us) {
- y = accum("microseconds", x, us, us_per_us, &leftover_us);
- CLEANUP;
- }
- if (ms) {
- y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
- CLEANUP;
- }
- if (second) {
- y = accum("seconds", x, second, us_per_second, &leftover_us);
- CLEANUP;
- }
- if (minute) {
- y = accum("minutes", x, minute, us_per_minute, &leftover_us);
- CLEANUP;
- }
- if (hour) {
- y = accum("hours", x, hour, us_per_hour, &leftover_us);
- CLEANUP;
- }
- if (day) {
- y = accum("days", x, day, us_per_day, &leftover_us);
- CLEANUP;
- }
- if (week) {
- y = accum("weeks", x, week, us_per_week, &leftover_us);
- CLEANUP;
- }
- if (leftover_us) {
- /* Round to nearest whole # of us, and add into x. */
- PyObject *temp = PyLong_FromLong(round_to_long(leftover_us));
- if (temp == NULL) {
- Py_DECREF(x);
- goto Done;
- }
- y = PyNumber_Add(x, temp);
- Py_DECREF(temp);
- CLEANUP;
- }
-
- self = microseconds_to_delta_ex(x, type);
- Py_DECREF(x);
+ PyObject *self = NULL;
+
+ /* Argument objects. */
+ PyObject *day = NULL;
+ PyObject *second = NULL;
+ PyObject *us = NULL;
+ PyObject *ms = NULL;
+ PyObject *minute = NULL;
+ PyObject *hour = NULL;
+ PyObject *week = NULL;
+
+ PyObject *x = NULL; /* running sum of microseconds */
+ PyObject *y = NULL; /* temp sum of microseconds */
+ double leftover_us = 0.0;
+
+ static char *keywords[] = {
+ "days", "seconds", "microseconds", "milliseconds",
+ "minutes", "hours", "weeks", NULL
+ };
+
+ if (PyArg_ParseTupleAndKeywords(args, kw, "|OOOOOOO:__new__",
+ keywords,
+ &day, &second, &us,
+ &ms, &minute, &hour, &week) == 0)
+ goto Done;
+
+ x = PyLong_FromLong(0);
+ if (x == NULL)
+ goto Done;
+
+#define CLEANUP \
+ Py_DECREF(x); \
+ x = y; \
+ if (x == NULL) \
+ goto Done
+
+ if (us) {
+ y = accum("microseconds", x, us, us_per_us, &leftover_us);
+ CLEANUP;
+ }
+ if (ms) {
+ y = accum("milliseconds", x, ms, us_per_ms, &leftover_us);
+ CLEANUP;
+ }
+ if (second) {
+ y = accum("seconds", x, second, us_per_second, &leftover_us);
+ CLEANUP;
+ }
+ if (minute) {
+ y = accum("minutes", x, minute, us_per_minute, &leftover_us);
+ CLEANUP;
+ }
+ if (hour) {
+ y = accum("hours", x, hour, us_per_hour, &leftover_us);
+ CLEANUP;
+ }
+ if (day) {
+ y = accum("days", x, day, us_per_day, &leftover_us);
+ CLEANUP;
+ }
+ if (week) {
+ y = accum("weeks", x, week, us_per_week, &leftover_us);
+ CLEANUP;
+ }
+ if (leftover_us) {
+ /* Round to nearest whole # of us, and add into x. */
+ PyObject *temp = PyLong_FromLong(round_to_long(leftover_us));
+ if (temp == NULL) {
+ Py_DECREF(x);
+ goto Done;
+ }
+ y = PyNumber_Add(x, temp);
+ Py_DECREF(temp);
+ CLEANUP;
+ }
+
+ self = microseconds_to_delta_ex(x, type);
+ Py_DECREF(x);
Done:
- return self;
+ return self;
#undef CLEANUP
}
@@ -1997,57 +1997,57 @@ Done:
static int
delta_bool(PyDateTime_Delta *self)
{
- return (GET_TD_DAYS(self) != 0
- || GET_TD_SECONDS(self) != 0
- || GET_TD_MICROSECONDS(self) != 0);
+ return (GET_TD_DAYS(self) != 0
+ || GET_TD_SECONDS(self) != 0
+ || GET_TD_MICROSECONDS(self) != 0);
}
static PyObject *
delta_repr(PyDateTime_Delta *self)
{
- if (GET_TD_MICROSECONDS(self) != 0)
- return PyUnicode_FromFormat("%s(%d, %d, %d)",
- Py_TYPE(self)->tp_name,
- GET_TD_DAYS(self),
- GET_TD_SECONDS(self),
- GET_TD_MICROSECONDS(self));
- if (GET_TD_SECONDS(self) != 0)
- return PyUnicode_FromFormat("%s(%d, %d)",
- Py_TYPE(self)->tp_name,
- GET_TD_DAYS(self),
- GET_TD_SECONDS(self));
+ if (GET_TD_MICROSECONDS(self) != 0)
+ return PyUnicode_FromFormat("%s(%d, %d, %d)",
+ Py_TYPE(self)->tp_name,
+ GET_TD_DAYS(self),
+ GET_TD_SECONDS(self),
+ GET_TD_MICROSECONDS(self));
+ if (GET_TD_SECONDS(self) != 0)
+ return PyUnicode_FromFormat("%s(%d, %d)",
+ Py_TYPE(self)->tp_name,
+ GET_TD_DAYS(self),
+ GET_TD_SECONDS(self));
- return PyUnicode_FromFormat("%s(%d)",
- Py_TYPE(self)->tp_name,
- GET_TD_DAYS(self));
+ return PyUnicode_FromFormat("%s(%d)",
+ Py_TYPE(self)->tp_name,
+ GET_TD_DAYS(self));
}
static PyObject *
delta_str(PyDateTime_Delta *self)
{
- int us = GET_TD_MICROSECONDS(self);
- int seconds = GET_TD_SECONDS(self);
- int minutes = divmod(seconds, 60, &seconds);
- int hours = divmod(minutes, 60, &minutes);
- int days = GET_TD_DAYS(self);
-
- if (days) {
- if (us)
- return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d",
- days, (days == 1 || days == -1) ? "" : "s",
- hours, minutes, seconds, us);
- else
- return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d",
- days, (days == 1 || days == -1) ? "" : "s",
- hours, minutes, seconds);
- } else {
- if (us)
- return PyUnicode_FromFormat("%d:%02d:%02d.%06d",
- hours, minutes, seconds, us);
- else
- return PyUnicode_FromFormat("%d:%02d:%02d",
- hours, minutes, seconds);
- }
+ int us = GET_TD_MICROSECONDS(self);
+ int seconds = GET_TD_SECONDS(self);
+ int minutes = divmod(seconds, 60, &seconds);
+ int hours = divmod(minutes, 60, &minutes);
+ int days = GET_TD_DAYS(self);
+
+ if (days) {
+ if (us)
+ return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d",
+ days, (days == 1 || days == -1) ? "" : "s",
+ hours, minutes, seconds, us);
+ else
+ return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d",
+ days, (days == 1 || days == -1) ? "" : "s",
+ hours, minutes, seconds);
+ } else {
+ if (us)
+ return PyUnicode_FromFormat("%d:%02d:%02d.%06d",
+ hours, minutes, seconds, us);
+ else
+ return PyUnicode_FromFormat("%d:%02d:%02d",
+ hours, minutes, seconds);
+ }
}
@@ -2057,118 +2057,118 @@ delta_str(PyDateTime_Delta *self)
static PyObject *
delta_getstate(PyDateTime_Delta *self)
{
- return Py_BuildValue("iii", GET_TD_DAYS(self),
- GET_TD_SECONDS(self),
- GET_TD_MICROSECONDS(self));
+ return Py_BuildValue("iii", GET_TD_DAYS(self),
+ GET_TD_SECONDS(self),
+ GET_TD_MICROSECONDS(self));
}
static PyObject *
delta_reduce(PyDateTime_Delta* self)
{
- return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self));
+ return Py_BuildValue("ON", Py_TYPE(self), delta_getstate(self));
}
#define OFFSET(field) offsetof(PyDateTime_Delta, field)
static PyMemberDef delta_members[] = {
- {"days", T_INT, OFFSET(days), READONLY,
- PyDoc_STR("Number of days.")},
+ {"days", T_INT, OFFSET(days), READONLY,
+ PyDoc_STR("Number of days.")},
- {"seconds", T_INT, OFFSET(seconds), READONLY,
- PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
+ {"seconds", T_INT, OFFSET(seconds), READONLY,
+ PyDoc_STR("Number of seconds (>= 0 and less than 1 day).")},
- {"microseconds", T_INT, OFFSET(microseconds), READONLY,
- PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
- {NULL}
+ {"microseconds", T_INT, OFFSET(microseconds), READONLY,
+ PyDoc_STR("Number of microseconds (>= 0 and less than 1 second).")},
+ {NULL}
};
static PyMethodDef delta_methods[] = {
- {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
- PyDoc_STR("__reduce__() -> (cls, state)")},
+ {"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
+ PyDoc_STR("__reduce__() -> (cls, state)")},
- {NULL, NULL},
+ {NULL, NULL},
};
static char delta_doc[] =
PyDoc_STR("Difference between two datetime values.");
static PyNumberMethods delta_as_number = {
- delta_add, /* nb_add */
- delta_subtract, /* nb_subtract */
- delta_multiply, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- (unaryfunc)delta_negative, /* nb_negative */
- (unaryfunc)delta_positive, /* nb_positive */
- (unaryfunc)delta_abs, /* nb_absolute */
- (inquiry)delta_bool, /* nb_bool */
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- 0, /*nb_and*/
- 0, /*nb_xor*/
- 0, /*nb_or*/
- 0, /*nb_int*/
- 0, /*nb_reserved*/
- 0, /*nb_float*/
- 0, /*nb_inplace_add*/
- 0, /*nb_inplace_subtract*/
- 0, /*nb_inplace_multiply*/
- 0, /*nb_inplace_remainder*/
- 0, /*nb_inplace_power*/
- 0, /*nb_inplace_lshift*/
- 0, /*nb_inplace_rshift*/
- 0, /*nb_inplace_and*/
- 0, /*nb_inplace_xor*/
- 0, /*nb_inplace_or*/
- delta_divide, /* nb_floor_divide */
- 0, /* nb_true_divide */
- 0, /* nb_inplace_floor_divide */
- 0, /* nb_inplace_true_divide */
+ delta_add, /* nb_add */
+ delta_subtract, /* nb_subtract */
+ delta_multiply, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ (unaryfunc)delta_negative, /* nb_negative */
+ (unaryfunc)delta_positive, /* nb_positive */
+ (unaryfunc)delta_abs, /* nb_absolute */
+ (inquiry)delta_bool, /* nb_bool */
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ 0, /*nb_and*/
+ 0, /*nb_xor*/
+ 0, /*nb_or*/
+ 0, /*nb_int*/
+ 0, /*nb_reserved*/
+ 0, /*nb_float*/
+ 0, /*nb_inplace_add*/
+ 0, /*nb_inplace_subtract*/
+ 0, /*nb_inplace_multiply*/
+ 0, /*nb_inplace_remainder*/
+ 0, /*nb_inplace_power*/
+ 0, /*nb_inplace_lshift*/
+ 0, /*nb_inplace_rshift*/
+ 0, /*nb_inplace_and*/
+ 0, /*nb_inplace_xor*/
+ 0, /*nb_inplace_or*/
+ delta_divide, /* nb_floor_divide */
+ 0, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
};
static PyTypeObject PyDateTime_DeltaType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datetime.timedelta", /* tp_name */
- sizeof(PyDateTime_Delta), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)delta_repr, /* tp_repr */
- &delta_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)delta_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)delta_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- delta_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- delta_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- delta_methods, /* tp_methods */
- delta_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- delta_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "datetime.timedelta", /* tp_name */
+ sizeof(PyDateTime_Delta), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)delta_repr, /* tp_repr */
+ &delta_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)delta_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)delta_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ delta_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ delta_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ delta_methods, /* tp_methods */
+ delta_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ delta_new, /* tp_new */
+ 0, /* tp_free */
};
/*
@@ -2180,26 +2180,26 @@ static PyTypeObject PyDateTime_DeltaType = {
static PyObject *
date_year(PyDateTime_Date *self, void *unused)
{
- return PyLong_FromLong(GET_YEAR(self));
+ return PyLong_FromLong(GET_YEAR(self));
}
static PyObject *
date_month(PyDateTime_Date *self, void *unused)
{
- return PyLong_FromLong(GET_MONTH(self));
+ return PyLong_FromLong(GET_MONTH(self));
}
static PyObject *
date_day(PyDateTime_Date *self, void *unused)
{
- return PyLong_FromLong(GET_DAY(self));
+ return PyLong_FromLong(GET_DAY(self));
}
static PyGetSetDef date_getset[] = {
- {"year", (getter)date_year},
- {"month", (getter)date_month},
- {"day", (getter)date_day},
- {NULL}
+ {"year", (getter)date_year},
+ {"month", (getter)date_month},
+ {"day", (getter)date_day},
+ {NULL}
};
/* Constructors. */
@@ -2209,60 +2209,60 @@ static char *date_kws[] = {"year", "month", "day", NULL};
static PyObject *
date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *self = NULL;
- PyObject *state;
- int year;
- int month;
- int day;
-
- /* Check for invocation from pickle with __getstate__ state */
- if (PyTuple_GET_SIZE(args) == 1 &&
- PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
- PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
- MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
- {
- PyDateTime_Date *me;
-
- me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
- if (me != NULL) {
- char *pdata = PyBytes_AS_STRING(state);
- memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
- me->hashcode = -1;
- }
- return (PyObject *)me;
- }
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
- &year, &month, &day)) {
- if (check_date_args(year, month, day) < 0)
- return NULL;
- self = new_date_ex(year, month, day, type);
- }
- return self;
+ PyObject *self = NULL;
+ PyObject *state;
+ int year;
+ int month;
+ int day;
+
+ /* Check for invocation from pickle with __getstate__ state */
+ if (PyTuple_GET_SIZE(args) == 1 &&
+ PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
+ PyBytes_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
+ MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
+ {
+ PyDateTime_Date *me;
+
+ me = (PyDateTime_Date *) (type->tp_alloc(type, 0));
+ if (me != NULL) {
+ char *pdata = PyBytes_AS_STRING(state);
+ memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
+ me->hashcode = -1;
+ }
+ return (PyObject *)me;
+ }
+
+ if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
+ &year, &month, &day)) {
+ if (check_date_args(year, month, day) < 0)
+ return NULL;
+ self = new_date_ex(year, month, day, type);
+ }
+ return self;
}
/* Return new date from localtime(t). */
static PyObject *
date_local_from_time_t(PyObject *cls, double ts)
{
- struct tm *tm;
- time_t t;
- PyObject *result = NULL;
-
- t = _PyTime_DoubleToTimet(ts);
- if (t == (time_t)-1 && PyErr_Occurred())
- return NULL;
- tm = localtime(&t);
- if (tm)
- result = PyObject_CallFunction(cls, "iii",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday);
- else
- PyErr_SetString(PyExc_ValueError,
- "timestamp out of range for "
- "platform localtime() function");
- return result;
+ struct tm *tm;
+ time_t t;
+ PyObject *result = NULL;
+
+ t = _PyTime_DoubleToTimet(ts);
+ if (t == (time_t)-1 && PyErr_Occurred())
+ return NULL;
+ tm = localtime(&t);
+ if (tm)
+ result = PyObject_CallFunction(cls, "iii",
+ tm->tm_year + 1900,
+ tm->tm_mon + 1,
+ tm->tm_mday);
+ else
+ PyErr_SetString(PyExc_ValueError,
+ "timestamp out of range for "
+ "platform localtime() function");
+ return result;
}
/* Return new date from current time.
@@ -2273,34 +2273,34 @@ date_local_from_time_t(PyObject *cls, double ts)
static PyObject *
date_today(PyObject *cls, PyObject *dummy)
{
- PyObject *time;
- PyObject *result;
+ PyObject *time;
+ PyObject *result;
- time = time_time();
- if (time == NULL)
- return NULL;
+ time = time_time();
+ if (time == NULL)
+ return NULL;
- /* Note well: today() is a class method, so this may not call
- * date.fromtimestamp. For example, it may call
- * datetime.fromtimestamp. That's why we need all the accuracy
- * time.time() delivers; if someone were gonzo about optimization,
- * date.today() could get away with plain C time().
- */
- result = PyObject_CallMethod(cls, "fromtimestamp", "O", time);
- Py_DECREF(time);
- return result;
+ /* Note well: today() is a class method, so this may not call
+ * date.fromtimestamp. For example, it may call
+ * datetime.fromtimestamp. That's why we need all the accuracy
+ * time.time() delivers; if someone were gonzo about optimization,
+ * date.today() could get away with plain C time().
+ */
+ result = PyObject_CallMethod(cls, "fromtimestamp", "O", time);
+ Py_DECREF(time);
+ return result;
}
/* Return new date from given timestamp (Python timestamp -- a double). */
static PyObject *
date_fromtimestamp(PyObject *cls, PyObject *args)
{
- double timestamp;
- PyObject *result = NULL;
+ double timestamp;
+ PyObject *result = NULL;
- if (PyArg_ParseTuple(args, "d:fromtimestamp", &timestamp))
- result = date_local_from_time_t(cls, timestamp);
- return result;
+ if (PyArg_ParseTuple(args, "d:fromtimestamp", &timestamp))
+ result = date_local_from_time_t(cls, timestamp);
+ return result;
}
/* Return new date from proleptic Gregorian ordinal. Raises ValueError if
@@ -2309,24 +2309,24 @@ date_fromtimestamp(PyObject *cls, PyObject *args)
static PyObject *
date_fromordinal(PyObject *cls, PyObject *args)
{
- PyObject *result = NULL;
- int ordinal;
+ PyObject *result = NULL;
+ int ordinal;
- if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) {
- int year;
- int month;
- int day;
+ if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) {
+ int year;
+ int month;
+ int day;
- if (ordinal < 1)
- PyErr_SetString(PyExc_ValueError, "ordinal must be "
- ">= 1");
- else {
- ord_to_ymd(ordinal, &year, &month, &day);
- result = PyObject_CallFunction(cls, "iii",
- year, month, day);
- }
- }
- return result;
+ if (ordinal < 1)
+ PyErr_SetString(PyExc_ValueError, "ordinal must be "
+ ">= 1");
+ else {
+ ord_to_ymd(ordinal, &year, &month, &day);
+ result = PyObject_CallFunction(cls, "iii",
+ year, month, day);
+ }
+ }
+ return result;
}
/*
@@ -2339,74 +2339,74 @@ date_fromordinal(PyObject *cls, PyObject *args)
static PyObject *
add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate)
{
- PyObject *result = NULL;
- int year = GET_YEAR(date);
- int month = GET_MONTH(date);
- int deltadays = GET_TD_DAYS(delta);
- /* C-level overflow is impossible because |deltadays| < 1e9. */
- int day = GET_DAY(date) + (negate ? -deltadays : deltadays);
+ PyObject *result = NULL;
+ int year = GET_YEAR(date);
+ int month = GET_MONTH(date);
+ int deltadays = GET_TD_DAYS(delta);
+ /* C-level overflow is impossible because |deltadays| < 1e9. */
+ int day = GET_DAY(date) + (negate ? -deltadays : deltadays);
- if (normalize_date(&year, &month, &day) >= 0)
- result = new_date(year, month, day);
- return result;
+ if (normalize_date(&year, &month, &day) >= 0)
+ result = new_date(year, month, day);
+ return result;
}
static PyObject *
date_add(PyObject *left, PyObject *right)
{
- if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- if (PyDate_Check(left)) {
- /* date + ??? */
- if (PyDelta_Check(right))
- /* date + delta */
- return add_date_timedelta((PyDateTime_Date *) left,
- (PyDateTime_Delta *) right,
- 0);
- }
- else {
- /* ??? + date
- * 'right' must be one of us, or we wouldn't have been called
- */
- if (PyDelta_Check(left))
- /* delta + date */
- return add_date_timedelta((PyDateTime_Date *) right,
- (PyDateTime_Delta *) left,
- 0);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ if (PyDate_Check(left)) {
+ /* date + ??? */
+ if (PyDelta_Check(right))
+ /* date + delta */
+ return add_date_timedelta((PyDateTime_Date *) left,
+ (PyDateTime_Delta *) right,
+ 0);
+ }
+ else {
+ /* ??? + date
+ * 'right' must be one of us, or we wouldn't have been called
+ */
+ if (PyDelta_Check(left))
+ /* delta + date */
+ return add_date_timedelta((PyDateTime_Date *) right,
+ (PyDateTime_Delta *) left,
+ 0);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyObject *
date_subtract(PyObject *left, PyObject *right)
{
- if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- if (PyDate_Check(left)) {
- if (PyDate_Check(right)) {
- /* date - date */
- int left_ord = ymd_to_ord(GET_YEAR(left),
- GET_MONTH(left),
- GET_DAY(left));
- int right_ord = ymd_to_ord(GET_YEAR(right),
- GET_MONTH(right),
- GET_DAY(right));
- return new_delta(left_ord - right_ord, 0, 0, 0);
- }
- if (PyDelta_Check(right)) {
- /* date - delta */
- return add_date_timedelta((PyDateTime_Date *) left,
- (PyDateTime_Delta *) right,
- 1);
- }
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ if (PyDateTime_Check(left) || PyDateTime_Check(right)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ if (PyDate_Check(left)) {
+ if (PyDate_Check(right)) {
+ /* date - date */
+ int left_ord = ymd_to_ord(GET_YEAR(left),
+ GET_MONTH(left),
+ GET_DAY(left));
+ int right_ord = ymd_to_ord(GET_YEAR(right),
+ GET_MONTH(right),
+ GET_DAY(right));
+ return new_delta(left_ord - right_ord, 0, 0, 0);
+ }
+ if (PyDelta_Check(right)) {
+ /* date - delta */
+ return add_date_timedelta((PyDateTime_Date *) left,
+ (PyDateTime_Delta *) right,
+ 1);
+ }
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
@@ -2415,69 +2415,69 @@ date_subtract(PyObject *left, PyObject *right)
static PyObject *
date_repr(PyDateTime_Date *self)
{
- return PyUnicode_FromFormat("%s(%d, %d, %d)",
- Py_TYPE(self)->tp_name,
- GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
+ return PyUnicode_FromFormat("%s(%d, %d, %d)",
+ Py_TYPE(self)->tp_name,
+ GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
}
static PyObject *
date_isoformat(PyDateTime_Date *self)
{
- return PyUnicode_FromFormat("%04d-%02d-%02d",
- GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
+ return PyUnicode_FromFormat("%04d-%02d-%02d",
+ GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
}
/* str() calls the appropriate isoformat() method. */
static PyObject *
date_str(PyDateTime_Date *self)
{
- return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
+ return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
}
static PyObject *
date_ctime(PyDateTime_Date *self)
{
- return format_ctime(self, 0, 0, 0);
+ return format_ctime(self, 0, 0, 0);
}
static PyObject *
date_strftime(PyDateTime_Date *self, PyObject *args, PyObject *kw)
{
- /* This method can be inherited, and needs to call the
- * timetuple() method appropriate to self's class.
- */
- PyObject *result;
- PyObject *tuple;
- PyObject *format;
- static char *keywords[] = {"format", NULL};
+ /* This method can be inherited, and needs to call the
+ * timetuple() method appropriate to self's class.
+ */
+ PyObject *result;
+ PyObject *tuple;
+ PyObject *format;
+ static char *keywords[] = {"format", NULL};
- if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
- &format))
- return NULL;
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
+ &format))
+ return NULL;
- tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()");
- if (tuple == NULL)
- return NULL;
- result = wrap_strftime((PyObject *)self, format, tuple,
- (PyObject *)self);
- Py_DECREF(tuple);
- return result;
+ tuple = PyObject_CallMethod((PyObject *)self, "timetuple", "()");
+ if (tuple == NULL)
+ return NULL;
+ result = wrap_strftime((PyObject *)self, format, tuple,
+ (PyObject *)self);
+ Py_DECREF(tuple);
+ return result;
}
static PyObject *
date_format(PyDateTime_Date *self, PyObject *args)
{
- PyObject *format;
+ PyObject *format;
- if (!PyArg_ParseTuple(args, "U:__format__", &format))
- return NULL;
+ if (!PyArg_ParseTuple(args, "U:__format__", &format))
+ return NULL;
- /* if the format is zero length, return str(self) */
- if (PyUnicode_GetSize(format) == 0)
- return PyObject_Str((PyObject *)self);
+ /* if the format is zero length, return str(self) */
+ if (PyUnicode_GetSize(format) == 0)
+ return PyObject_Str((PyObject *)self);
- return PyObject_CallMethod((PyObject *)self, "strftime", "O", format);
+ return PyObject_CallMethod((PyObject *)self, "strftime", "O", format);
}
/* ISO methods. */
@@ -2485,31 +2485,31 @@ date_format(PyDateTime_Date *self, PyObject *args)
static PyObject *
date_isoweekday(PyDateTime_Date *self)
{
- int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
+ int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
- return PyLong_FromLong(dow + 1);
+ return PyLong_FromLong(dow + 1);
}
static PyObject *
date_isocalendar(PyDateTime_Date *self)
{
- int year = GET_YEAR(self);
- int week1_monday = iso_week1_monday(year);
- int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self));
- int week;
- int day;
-
- week = divmod(today - week1_monday, 7, &day);
- if (week < 0) {
- --year;
- week1_monday = iso_week1_monday(year);
- week = divmod(today - week1_monday, 7, &day);
- }
- else if (week >= 52 && today >= iso_week1_monday(year + 1)) {
- ++year;
- week = 0;
- }
- return Py_BuildValue("iii", year, week + 1, day + 1);
+ int year = GET_YEAR(self);
+ int week1_monday = iso_week1_monday(year);
+ int today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self));
+ int week;
+ int day;
+
+ week = divmod(today - week1_monday, 7, &day);
+ if (week < 0) {
+ --year;
+ week1_monday = iso_week1_monday(year);
+ week = divmod(today - week1_monday, 7, &day);
+ }
+ else if (week >= 52 && today >= iso_week1_monday(year + 1)) {
+ ++year;
+ week = 0;
+ }
+ return Py_BuildValue("iii", year, week + 1, day + 1);
}
/* Miscellaneous methods. */
@@ -2517,65 +2517,65 @@ date_isocalendar(PyDateTime_Date *self)
static PyObject *
date_richcompare(PyObject *self, PyObject *other, int op)
{
- if (PyDate_Check(other)) {
- int diff = memcmp(((PyDateTime_Date *)self)->data,
- ((PyDateTime_Date *)other)->data,
- _PyDateTime_DATE_DATASIZE);
- return diff_to_bool(diff, op);
- }
- else {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
+ if (PyDate_Check(other)) {
+ int diff = memcmp(((PyDateTime_Date *)self)->data,
+ ((PyDateTime_Date *)other)->data,
+ _PyDateTime_DATE_DATASIZE);
+ return diff_to_bool(diff, op);
+ }
+ else {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
}
static PyObject *
date_timetuple(PyDateTime_Date *self)
{
- return build_struct_time(GET_YEAR(self),
- GET_MONTH(self),
- GET_DAY(self),
- 0, 0, 0, -1);
+ return build_struct_time(GET_YEAR(self),
+ GET_MONTH(self),
+ GET_DAY(self),
+ 0, 0, 0, -1);
}
static PyObject *
date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
- int year = GET_YEAR(self);
- int month = GET_MONTH(self);
- int day = GET_DAY(self);
+ PyObject *clone;
+ PyObject *tuple;
+ int year = GET_YEAR(self);
+ int month = GET_MONTH(self);
+ int day = GET_DAY(self);
- if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
- &year, &month, &day))
- return NULL;
- tuple = Py_BuildValue("iii", year, month, day);
- if (tuple == NULL)
- return NULL;
- clone = date_new(Py_TYPE(self), tuple, NULL);
- Py_DECREF(tuple);
- return clone;
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
+ &year, &month, &day))
+ return NULL;
+ tuple = Py_BuildValue("iii", year, month, day);
+ if (tuple == NULL)
+ return NULL;
+ clone = date_new(Py_TYPE(self), tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
}
/*
- Borrowed from stringobject.c, originally it was string_hash()
+ Borrowed from stringobject.c, originally it was string_hash()
*/
static long
generic_hash(unsigned char *data, int len)
{
- register unsigned char *p;
- register long x;
+ register unsigned char *p;
+ register long x;
- p = (unsigned char *) data;
- x = *p << 7;
- while (--len >= 0)
- x = (1000003*x) ^ *p++;
- x ^= len;
- if (x == -1)
- x = -2;
+ p = (unsigned char *) data;
+ x = *p << 7;
+ while (--len >= 0)
+ x = (1000003*x) ^ *p++;
+ x ^= len;
+ if (x == -1)
+ x = -2;
- return x;
+ return x;
}
@@ -2584,26 +2584,26 @@ static PyObject *date_getstate(PyDateTime_Date *self);
static long
date_hash(PyDateTime_Date *self)
{
- if (self->hashcode == -1)
- self->hashcode = generic_hash(
- (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE);
+ if (self->hashcode == -1)
+ self->hashcode = generic_hash(
+ (unsigned char *)self->data, _PyDateTime_DATE_DATASIZE);
- return self->hashcode;
+ return self->hashcode;
}
static PyObject *
date_toordinal(PyDateTime_Date *self)
{
- return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self),
- GET_DAY(self)));
+ return PyLong_FromLong(ymd_to_ord(GET_YEAR(self), GET_MONTH(self),
+ GET_DAY(self)));
}
static PyObject *
date_weekday(PyDateTime_Date *self)
{
- int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
+ int dow = weekday(GET_YEAR(self), GET_MONTH(self), GET_DAY(self));
- return PyLong_FromLong(dow);
+ return PyLong_FromLong(dow);
}
/* Pickle support, a simple use of __reduce__. */
@@ -2612,134 +2612,134 @@ date_weekday(PyDateTime_Date *self)
static PyObject *
date_getstate(PyDateTime_Date *self)
{
- PyObject* field;
- field = PyBytes_FromStringAndSize((char*)self->data,
- _PyDateTime_DATE_DATASIZE);
- return Py_BuildValue("(N)", field);
+ PyObject* field;
+ field = PyBytes_FromStringAndSize((char*)self->data,
+ _PyDateTime_DATE_DATASIZE);
+ return Py_BuildValue("(N)", field);
}
static PyObject *
date_reduce(PyDateTime_Date *self, PyObject *arg)
{
- return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self));
+ return Py_BuildValue("(ON)", Py_TYPE(self), date_getstate(self));
}
static PyMethodDef date_methods[] = {
- /* Class methods: */
+ /* Class methods: */
- {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS |
- METH_CLASS,
- PyDoc_STR("timestamp -> local date from a POSIX timestamp (like "
- "time.time()).")},
+ {"fromtimestamp", (PyCFunction)date_fromtimestamp, METH_VARARGS |
+ METH_CLASS,
+ PyDoc_STR("timestamp -> local date from a POSIX timestamp (like "
+ "time.time()).")},
- {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
- METH_CLASS,
- PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
- "ordinal.")},
+ {"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
+ METH_CLASS,
+ PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
+ "ordinal.")},
- {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
- PyDoc_STR("Current date or datetime: same as "
- "self.__class__.fromtimestamp(time.time()).")},
+ {"today", (PyCFunction)date_today, METH_NOARGS | METH_CLASS,
+ PyDoc_STR("Current date or datetime: same as "
+ "self.__class__.fromtimestamp(time.time()).")},
- /* Instance methods: */
+ /* Instance methods: */
- {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
- PyDoc_STR("Return ctime() style string.")},
+ {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
+ PyDoc_STR("Return ctime() style string.")},
- {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("format -> strftime() style string.")},
+ {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("format -> strftime() style string.")},
- {"__format__", (PyCFunction)date_format, METH_VARARGS,
- PyDoc_STR("Formats self with strftime.")},
+ {"__format__", (PyCFunction)date_format, METH_VARARGS,
+ PyDoc_STR("Formats self with strftime.")},
- {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
- PyDoc_STR("Return time tuple, compatible with time.localtime().")},
+ {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
+ PyDoc_STR("Return time tuple, compatible with time.localtime().")},
- {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
- PyDoc_STR("Return a 3-tuple containing ISO year, week number, and "
- "weekday.")},
+ {"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
+ PyDoc_STR("Return a 3-tuple containing ISO year, week number, and "
+ "weekday.")},
- {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS,
- PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")},
+ {"isoformat", (PyCFunction)date_isoformat, METH_NOARGS,
+ PyDoc_STR("Return string in ISO 8601 format, YYYY-MM-DD.")},
- {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
- PyDoc_STR("Return the day of the week represented by the date.\n"
- "Monday == 1 ... Sunday == 7")},
+ {"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
+ PyDoc_STR("Return the day of the week represented by the date.\n"
+ "Monday == 1 ... Sunday == 7")},
- {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
- PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year "
- "1 is day 1.")},
+ {"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
+ PyDoc_STR("Return proleptic Gregorian ordinal. January 1 of year "
+ "1 is day 1.")},
- {"weekday", (PyCFunction)date_weekday, METH_NOARGS,
- PyDoc_STR("Return the day of the week represented by the date.\n"
- "Monday == 0 ... Sunday == 6")},
+ {"weekday", (PyCFunction)date_weekday, METH_NOARGS,
+ PyDoc_STR("Return the day of the week represented by the date.\n"
+ "Monday == 0 ... Sunday == 6")},
- {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("Return date with new specified fields.")},
+ {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("Return date with new specified fields.")},
- {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
- PyDoc_STR("__reduce__() -> (cls, state)")},
+ {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
+ PyDoc_STR("__reduce__() -> (cls, state)")},
- {NULL, NULL}
+ {NULL, NULL}
};
static char date_doc[] =
PyDoc_STR("date(year, month, day) --> date object");
static PyNumberMethods date_as_number = {
- date_add, /* nb_add */
- date_subtract, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- 0, /* nb_bool */
+ date_add, /* nb_add */
+ date_subtract, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, /* nb_bool */
};
static PyTypeObject PyDateTime_DateType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datetime.date", /* tp_name */
- sizeof(PyDateTime_Date), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)date_repr, /* tp_repr */
- &date_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)date_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)date_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- date_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- date_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- date_methods, /* tp_methods */
- 0, /* tp_members */
- date_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- date_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "datetime.date", /* tp_name */
+ sizeof(PyDateTime_Date), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)date_repr, /* tp_repr */
+ &date_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)date_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)date_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ date_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ date_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ date_methods, /* tp_methods */
+ 0, /* tp_members */
+ date_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ date_new, /* tp_new */
+ 0, /* tp_free */
};
/*
@@ -2762,10 +2762,10 @@ static PyTypeObject PyDateTime_DateType = {
static PyObject *
tzinfo_nogo(const char* methodname)
{
- PyErr_Format(PyExc_NotImplementedError,
- "a tzinfo subclass must implement %s()",
- methodname);
- return NULL;
+ PyErr_Format(PyExc_NotImplementedError,
+ "a tzinfo subclass must implement %s()",
+ methodname);
+ return NULL;
}
/* Methods. A subclass must implement these. */
@@ -2773,101 +2773,101 @@ tzinfo_nogo(const char* methodname)
static PyObject *
tzinfo_tzname(PyDateTime_TZInfo *self, PyObject *dt)
{
- return tzinfo_nogo("tzname");
+ return tzinfo_nogo("tzname");
}
static PyObject *
tzinfo_utcoffset(PyDateTime_TZInfo *self, PyObject *dt)
{
- return tzinfo_nogo("utcoffset");
+ return tzinfo_nogo("utcoffset");
}
static PyObject *
tzinfo_dst(PyDateTime_TZInfo *self, PyObject *dt)
{
- return tzinfo_nogo("dst");
+ return tzinfo_nogo("dst");
}
static PyObject *
tzinfo_fromutc(PyDateTime_TZInfo *self, PyDateTime_DateTime *dt)
{
- int y, m, d, hh, mm, ss, us;
-
- PyObject *result;
- int off, dst;
- int none;
- int delta;
-
- if (! PyDateTime_Check(dt)) {
- PyErr_SetString(PyExc_TypeError,
- "fromutc: argument must be a datetime");
- return NULL;
- }
- if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) {
- PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo "
- "is not self");
- return NULL;
- }
-
- off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none);
- if (off == -1 && PyErr_Occurred())
- return NULL;
- if (none) {
- PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
- "utcoffset() result required");
- return NULL;
- }
-
- dst = call_dst(dt->tzinfo, (PyObject *)dt, &none);
- if (dst == -1 && PyErr_Occurred())
- return NULL;
- if (none) {
- PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
- "dst() result required");
- return NULL;
- }
-
- y = GET_YEAR(dt);
- m = GET_MONTH(dt);
- d = GET_DAY(dt);
- hh = DATE_GET_HOUR(dt);
- mm = DATE_GET_MINUTE(dt);
- ss = DATE_GET_SECOND(dt);
- us = DATE_GET_MICROSECOND(dt);
-
- delta = off - dst;
- mm += delta;
- if ((mm < 0 || mm >= 60) &&
- normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
- return NULL;
- result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
- if (result == NULL)
- return result;
-
- dst = call_dst(dt->tzinfo, result, &none);
- if (dst == -1 && PyErr_Occurred())
- goto Fail;
- if (none)
- goto Inconsistent;
- if (dst == 0)
- return result;
-
- mm += dst;
- if ((mm < 0 || mm >= 60) &&
- normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
- goto Fail;
- Py_DECREF(result);
- result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
- return result;
+ int y, m, d, hh, mm, ss, us;
+
+ PyObject *result;
+ int off, dst;
+ int none;
+ int delta;
+
+ if (! PyDateTime_Check(dt)) {
+ PyErr_SetString(PyExc_TypeError,
+ "fromutc: argument must be a datetime");
+ return NULL;
+ }
+ if (! HASTZINFO(dt) || dt->tzinfo != (PyObject *)self) {
+ PyErr_SetString(PyExc_ValueError, "fromutc: dt.tzinfo "
+ "is not self");
+ return NULL;
+ }
+
+ off = call_utcoffset(dt->tzinfo, (PyObject *)dt, &none);
+ if (off == -1 && PyErr_Occurred())
+ return NULL;
+ if (none) {
+ PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
+ "utcoffset() result required");
+ return NULL;
+ }
+
+ dst = call_dst(dt->tzinfo, (PyObject *)dt, &none);
+ if (dst == -1 && PyErr_Occurred())
+ return NULL;
+ if (none) {
+ PyErr_SetString(PyExc_ValueError, "fromutc: non-None "
+ "dst() result required");
+ return NULL;
+ }
+
+ y = GET_YEAR(dt);
+ m = GET_MONTH(dt);
+ d = GET_DAY(dt);
+ hh = DATE_GET_HOUR(dt);
+ mm = DATE_GET_MINUTE(dt);
+ ss = DATE_GET_SECOND(dt);
+ us = DATE_GET_MICROSECOND(dt);
+
+ delta = off - dst;
+ mm += delta;
+ if ((mm < 0 || mm >= 60) &&
+ normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
+ return NULL;
+ result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
+ if (result == NULL)
+ return result;
+
+ dst = call_dst(dt->tzinfo, result, &none);
+ if (dst == -1 && PyErr_Occurred())
+ goto Fail;
+ if (none)
+ goto Inconsistent;
+ if (dst == 0)
+ return result;
+
+ mm += dst;
+ if ((mm < 0 || mm >= 60) &&
+ normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
+ goto Fail;
+ Py_DECREF(result);
+ result = new_datetime(y, m, d, hh, mm, ss, us, dt->tzinfo);
+ return result;
Inconsistent:
- PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave"
- "inconsistent results; cannot convert");
+ PyErr_SetString(PyExc_ValueError, "fromutc: tz.dst() gave"
+ "inconsistent results; cannot convert");
- /* fall thru to failure */
+ /* fall thru to failure */
Fail:
- Py_DECREF(result);
- return NULL;
+ Py_DECREF(result);
+ return NULL;
}
/*
@@ -2878,122 +2878,122 @@ Fail:
static PyObject *
tzinfo_reduce(PyObject *self)
{
- PyObject *args, *state, *tmp;
- PyObject *getinitargs, *getstate;
-
- tmp = PyTuple_New(0);
- if (tmp == NULL)
- return NULL;
-
- getinitargs = PyObject_GetAttrString(self, "__getinitargs__");
- if (getinitargs != NULL) {
- args = PyObject_CallObject(getinitargs, tmp);
- Py_DECREF(getinitargs);
- if (args == NULL) {
- Py_DECREF(tmp);
- return NULL;
- }
- }
- else {
- PyErr_Clear();
- args = tmp;
- Py_INCREF(args);
- }
-
- getstate = PyObject_GetAttrString(self, "__getstate__");
- if (getstate != NULL) {
- state = PyObject_CallObject(getstate, tmp);
- Py_DECREF(getstate);
- if (state == NULL) {
- Py_DECREF(args);
- Py_DECREF(tmp);
- return NULL;
- }
- }
- else {
- PyObject **dictptr;
- PyErr_Clear();
- state = Py_None;
- dictptr = _PyObject_GetDictPtr(self);
- if (dictptr && *dictptr && PyDict_Size(*dictptr))
- state = *dictptr;
- Py_INCREF(state);
- }
-
- Py_DECREF(tmp);
-
- if (state == Py_None) {
- Py_DECREF(state);
- return Py_BuildValue("(ON)", Py_TYPE(self), args);
- }
- else
- return Py_BuildValue("(ONN)", Py_TYPE(self), args, state);
+ PyObject *args, *state, *tmp;
+ PyObject *getinitargs, *getstate;
+
+ tmp = PyTuple_New(0);
+ if (tmp == NULL)
+ return NULL;
+
+ getinitargs = PyObject_GetAttrString(self, "__getinitargs__");
+ if (getinitargs != NULL) {
+ args = PyObject_CallObject(getinitargs, tmp);
+ Py_DECREF(getinitargs);
+ if (args == NULL) {
+ Py_DECREF(tmp);
+ return NULL;
+ }
+ }
+ else {
+ PyErr_Clear();
+ args = tmp;
+ Py_INCREF(args);
+ }
+
+ getstate = PyObject_GetAttrString(self, "__getstate__");
+ if (getstate != NULL) {
+ state = PyObject_CallObject(getstate, tmp);
+ Py_DECREF(getstate);
+ if (state == NULL) {
+ Py_DECREF(args);
+ Py_DECREF(tmp);
+ return NULL;
+ }
+ }
+ else {
+ PyObject **dictptr;
+ PyErr_Clear();
+ state = Py_None;
+ dictptr = _PyObject_GetDictPtr(self);
+ if (dictptr && *dictptr && PyDict_Size(*dictptr))
+ state = *dictptr;
+ Py_INCREF(state);
+ }
+
+ Py_DECREF(tmp);
+
+ if (state == Py_None) {
+ Py_DECREF(state);
+ return Py_BuildValue("(ON)", Py_TYPE(self), args);
+ }
+ else
+ return Py_BuildValue("(ONN)", Py_TYPE(self), args, state);
}
static PyMethodDef tzinfo_methods[] = {
- {"tzname", (PyCFunction)tzinfo_tzname, METH_O,
- PyDoc_STR("datetime -> string name of time zone.")},
+ {"tzname", (PyCFunction)tzinfo_tzname, METH_O,
+ PyDoc_STR("datetime -> string name of time zone.")},
- {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O,
- PyDoc_STR("datetime -> minutes east of UTC (negative for "
- "west of UTC).")},
+ {"utcoffset", (PyCFunction)tzinfo_utcoffset, METH_O,
+ PyDoc_STR("datetime -> minutes east of UTC (negative for "
+ "west of UTC).")},
- {"dst", (PyCFunction)tzinfo_dst, METH_O,
- PyDoc_STR("datetime -> DST offset in minutes east of UTC.")},
+ {"dst", (PyCFunction)tzinfo_dst, METH_O,
+ PyDoc_STR("datetime -> DST offset in minutes east of UTC.")},
- {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O,
- PyDoc_STR("datetime in UTC -> datetime in local time.")},
+ {"fromutc", (PyCFunction)tzinfo_fromutc, METH_O,
+ PyDoc_STR("datetime in UTC -> datetime in local time.")},
- {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS,
- PyDoc_STR("-> (cls, state)")},
+ {"__reduce__", (PyCFunction)tzinfo_reduce, METH_NOARGS,
+ PyDoc_STR("-> (cls, state)")},
- {NULL, NULL}
+ {NULL, NULL}
};
static char tzinfo_doc[] =
PyDoc_STR("Abstract base class for time zone info objects.");
static PyTypeObject PyDateTime_TZInfoType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datetime.tzinfo", /* tp_name */
- sizeof(PyDateTime_TZInfo), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- tzinfo_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- tzinfo_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "datetime.tzinfo", /* tp_name */
+ sizeof(PyDateTime_TZInfo), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ tzinfo_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ tzinfo_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ 0, /* tp_free */
};
/*
@@ -3006,43 +3006,43 @@ static PyTypeObject PyDateTime_TZInfoType = {
static PyObject *
time_hour(PyDateTime_Time *self, void *unused)
{
- return PyLong_FromLong(TIME_GET_HOUR(self));
+ return PyLong_FromLong(TIME_GET_HOUR(self));
}
static PyObject *
time_minute(PyDateTime_Time *self, void *unused)
{
- return PyLong_FromLong(TIME_GET_MINUTE(self));
+ return PyLong_FromLong(TIME_GET_MINUTE(self));
}
/* The name time_second conflicted with some platform header file. */
static PyObject *
py_time_second(PyDateTime_Time *self, void *unused)
{
- return PyLong_FromLong(TIME_GET_SECOND(self));
+ return PyLong_FromLong(TIME_GET_SECOND(self));
}
static PyObject *
time_microsecond(PyDateTime_Time *self, void *unused)
{
- return PyLong_FromLong(TIME_GET_MICROSECOND(self));
+ return PyLong_FromLong(TIME_GET_MICROSECOND(self));
}
static PyObject *
time_tzinfo(PyDateTime_Time *self, void *unused)
{
- PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
- Py_INCREF(result);
- return result;
+ PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
+ Py_INCREF(result);
+ return result;
}
static PyGetSetDef time_getset[] = {
- {"hour", (getter)time_hour},
- {"minute", (getter)time_minute},
- {"second", (getter)py_time_second},
- {"microsecond", (getter)time_microsecond},
- {"tzinfo", (getter)time_tzinfo},
- {NULL}
+ {"hour", (getter)time_hour},
+ {"minute", (getter)time_minute},
+ {"second", (getter)py_time_second},
+ {"microsecond", (getter)time_microsecond},
+ {"tzinfo", (getter)time_tzinfo},
+ {NULL}
};
/*
@@ -3050,64 +3050,64 @@ static PyGetSetDef time_getset[] = {
*/
static char *time_kws[] = {"hour", "minute", "second", "microsecond",
- "tzinfo", NULL};
+ "tzinfo", NULL};
static PyObject *
time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *self = NULL;
- PyObject *state;
- int hour = 0;
- int minute = 0;
- int second = 0;
- int usecond = 0;
- PyObject *tzinfo = Py_None;
-
- /* Check for invocation from pickle with __getstate__ state */
- if (PyTuple_GET_SIZE(args) >= 1 &&
- PyTuple_GET_SIZE(args) <= 2 &&
- PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
- PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
- ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24)
- {
- PyDateTime_Time *me;
- char aware;
-
- if (PyTuple_GET_SIZE(args) == 2) {
- tzinfo = PyTuple_GET_ITEM(args, 1);
- if (check_tzinfo_subclass(tzinfo) < 0) {
- PyErr_SetString(PyExc_TypeError, "bad "
- "tzinfo state arg");
- return NULL;
- }
- }
- aware = (char)(tzinfo != Py_None);
- me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
- if (me != NULL) {
- char *pdata = PyBytes_AS_STRING(state);
-
- memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
- me->hashcode = -1;
- me->hastzinfo = aware;
- if (aware) {
- Py_INCREF(tzinfo);
- me->tzinfo = tzinfo;
- }
- }
- return (PyObject *)me;
- }
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws,
- &hour, &minute, &second, &usecond,
- &tzinfo)) {
- if (check_time_args(hour, minute, second, usecond) < 0)
- return NULL;
- if (check_tzinfo_subclass(tzinfo) < 0)
- return NULL;
- self = new_time_ex(hour, minute, second, usecond, tzinfo,
- type);
- }
- return self;
+ PyObject *self = NULL;
+ PyObject *state;
+ int hour = 0;
+ int minute = 0;
+ int second = 0;
+ int usecond = 0;
+ PyObject *tzinfo = Py_None;
+
+ /* Check for invocation from pickle with __getstate__ state */
+ if (PyTuple_GET_SIZE(args) >= 1 &&
+ PyTuple_GET_SIZE(args) <= 2 &&
+ PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
+ PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE &&
+ ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24)
+ {
+ PyDateTime_Time *me;
+ char aware;
+
+ if (PyTuple_GET_SIZE(args) == 2) {
+ tzinfo = PyTuple_GET_ITEM(args, 1);
+ if (check_tzinfo_subclass(tzinfo) < 0) {
+ PyErr_SetString(PyExc_TypeError, "bad "
+ "tzinfo state arg");
+ return NULL;
+ }
+ }
+ aware = (char)(tzinfo != Py_None);
+ me = (PyDateTime_Time *) (type->tp_alloc(type, aware));
+ if (me != NULL) {
+ char *pdata = PyBytes_AS_STRING(state);
+
+ memcpy(me->data, pdata, _PyDateTime_TIME_DATASIZE);
+ me->hashcode = -1;
+ me->hastzinfo = aware;
+ if (aware) {
+ Py_INCREF(tzinfo);
+ me->tzinfo = tzinfo;
+ }
+ }
+ return (PyObject *)me;
+ }
+
+ if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws,
+ &hour, &minute, &second, &usecond,
+ &tzinfo)) {
+ if (check_time_args(hour, minute, second, usecond) < 0)
+ return NULL;
+ if (check_tzinfo_subclass(tzinfo) < 0)
+ return NULL;
+ self = new_time_ex(hour, minute, second, usecond, tzinfo,
+ type);
+ }
+ return self;
}
/*
@@ -3117,10 +3117,10 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
static void
time_dealloc(PyDateTime_Time *self)
{
- if (HASTZINFO(self)) {
- Py_XDECREF(self->tzinfo);
- }
- Py_TYPE(self)->tp_free((PyObject *)self);
+ if (HASTZINFO(self)) {
+ Py_XDECREF(self->tzinfo);
+ }
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
/*
@@ -3130,20 +3130,20 @@ time_dealloc(PyDateTime_Time *self)
/* These are all METH_NOARGS, so don't need to check the arglist. */
static PyObject *
time_utcoffset(PyDateTime_Time *self, PyObject *unused) {
- return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
- "utcoffset", Py_None);
+ return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
+ "utcoffset", Py_None);
}
static PyObject *
time_dst(PyDateTime_Time *self, PyObject *unused) {
- return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
- "dst", Py_None);
+ return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
+ "dst", Py_None);
}
static PyObject *
time_tzname(PyDateTime_Time *self, PyObject *unused) {
- return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
- Py_None);
+ return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
+ Py_None);
}
/*
@@ -3153,93 +3153,93 @@ time_tzname(PyDateTime_Time *self, PyObject *unused) {
static PyObject *
time_repr(PyDateTime_Time *self)
{
- const char *type_name = Py_TYPE(self)->tp_name;
- int h = TIME_GET_HOUR(self);
- int m = TIME_GET_MINUTE(self);
- int s = TIME_GET_SECOND(self);
- int us = TIME_GET_MICROSECOND(self);
- PyObject *result = NULL;
-
- if (us)
- result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)",
- type_name, h, m, s, us);
- else if (s)
- result = PyUnicode_FromFormat("%s(%d, %d, %d)",
- type_name, h, m, s);
- else
- result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m);
- if (result != NULL && HASTZINFO(self))
- result = append_keyword_tzinfo(result, self->tzinfo);
- return result;
+ const char *type_name = Py_TYPE(self)->tp_name;
+ int h = TIME_GET_HOUR(self);
+ int m = TIME_GET_MINUTE(self);
+ int s = TIME_GET_SECOND(self);
+ int us = TIME_GET_MICROSECOND(self);
+ PyObject *result = NULL;
+
+ if (us)
+ result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)",
+ type_name, h, m, s, us);
+ else if (s)
+ result = PyUnicode_FromFormat("%s(%d, %d, %d)",
+ type_name, h, m, s);
+ else
+ result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m);
+ if (result != NULL && HASTZINFO(self))
+ result = append_keyword_tzinfo(result, self->tzinfo);
+ return result;
}
static PyObject *
time_str(PyDateTime_Time *self)
{
- return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
+ return PyObject_CallMethod((PyObject *)self, "isoformat", "()");
}
static PyObject *
time_isoformat(PyDateTime_Time *self, PyObject *unused)
{
- char buf[100];
- PyObject *result;
- int us = TIME_GET_MICROSECOND(self);;
-
- if (us)
- result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",
- TIME_GET_HOUR(self),
- TIME_GET_MINUTE(self),
- TIME_GET_SECOND(self),
- us);
- else
- result = PyUnicode_FromFormat("%02d:%02d:%02d",
- TIME_GET_HOUR(self),
- TIME_GET_MINUTE(self),
- TIME_GET_SECOND(self));
-
- if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None)
- return result;
-
- /* We need to append the UTC offset. */
- if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
- Py_None) < 0) {
- Py_DECREF(result);
- return NULL;
- }
- PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf));
- return result;
+ char buf[100];
+ PyObject *result;
+ int us = TIME_GET_MICROSECOND(self);;
+
+ if (us)
+ result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",
+ TIME_GET_HOUR(self),
+ TIME_GET_MINUTE(self),
+ TIME_GET_SECOND(self),
+ us);
+ else
+ result = PyUnicode_FromFormat("%02d:%02d:%02d",
+ TIME_GET_HOUR(self),
+ TIME_GET_MINUTE(self),
+ TIME_GET_SECOND(self));
+
+ if (result == NULL || ! HASTZINFO(self) || self->tzinfo == Py_None)
+ return result;
+
+ /* We need to append the UTC offset. */
+ if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo,
+ Py_None) < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buf));
+ return result;
}
static PyObject *
time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
{
- PyObject *result;
- PyObject *tuple;
- PyObject *format;
- static char *keywords[] = {"format", NULL};
-
- if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
- &format))
- return NULL;
-
- /* Python's strftime does insane things with the year part of the
- * timetuple. The year is forced to (the otherwise nonsensical)
- * 1900 to worm around that.
- */
- tuple = Py_BuildValue("iiiiiiiii",
- 1900, 1, 1, /* year, month, day */
- TIME_GET_HOUR(self),
- TIME_GET_MINUTE(self),
- TIME_GET_SECOND(self),
- 0, 1, -1); /* weekday, daynum, dst */
- if (tuple == NULL)
- return NULL;
- assert(PyTuple_Size(tuple) == 9);
- result = wrap_strftime((PyObject *)self, format, tuple,
- Py_None);
- Py_DECREF(tuple);
- return result;
+ PyObject *result;
+ PyObject *tuple;
+ PyObject *format;
+ static char *keywords[] = {"format", NULL};
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "U:strftime", keywords,
+ &format))
+ return NULL;
+
+ /* Python's strftime does insane things with the year part of the
+ * timetuple. The year is forced to (the otherwise nonsensical)
+ * 1900 to worm around that.
+ */
+ tuple = Py_BuildValue("iiiiiiiii",
+ 1900, 1, 1, /* year, month, day */
+ TIME_GET_HOUR(self),
+ TIME_GET_MINUTE(self),
+ TIME_GET_SECOND(self),
+ 0, 1, -1); /* weekday, daynum, dst */
+ if (tuple == NULL)
+ return NULL;
+ assert(PyTuple_Size(tuple) == 9);
+ result = wrap_strftime((PyObject *)self, format, tuple,
+ Py_None);
+ Py_DECREF(tuple);
+ return result;
}
/*
@@ -3249,144 +3249,144 @@ time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
static PyObject *
time_richcompare(PyObject *self, PyObject *other, int op)
{
- int diff;
- naivety n1, n2;
- int offset1, offset2;
-
- if (! PyTime_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- if (classify_two_utcoffsets(self, &offset1, &n1, Py_None,
- other, &offset2, &n2, Py_None) < 0)
- return NULL;
- assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
- /* If they're both naive, or both aware and have the same offsets,
- * we get off cheap. Note that if they're both naive, offset1 ==
- * offset2 == 0 at this point.
- */
- if (n1 == n2 && offset1 == offset2) {
- diff = memcmp(((PyDateTime_Time *)self)->data,
- ((PyDateTime_Time *)other)->data,
- _PyDateTime_TIME_DATASIZE);
- return diff_to_bool(diff, op);
- }
-
- if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
- assert(offset1 != offset2); /* else last "if" handled it */
- /* Convert everything except microseconds to seconds. These
- * can't overflow (no more than the # of seconds in 2 days).
- */
- offset1 = TIME_GET_HOUR(self) * 3600 +
- (TIME_GET_MINUTE(self) - offset1) * 60 +
- TIME_GET_SECOND(self);
- offset2 = TIME_GET_HOUR(other) * 3600 +
- (TIME_GET_MINUTE(other) - offset2) * 60 +
- TIME_GET_SECOND(other);
- diff = offset1 - offset2;
- if (diff == 0)
- diff = TIME_GET_MICROSECOND(self) -
- TIME_GET_MICROSECOND(other);
- return diff_to_bool(diff, op);
- }
-
- assert(n1 != n2);
- PyErr_SetString(PyExc_TypeError,
- "can't compare offset-naive and "
- "offset-aware times");
- return NULL;
+ int diff;
+ naivety n1, n2;
+ int offset1, offset2;
+
+ if (! PyTime_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ if (classify_two_utcoffsets(self, &offset1, &n1, Py_None,
+ other, &offset2, &n2, Py_None) < 0)
+ return NULL;
+ assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
+ /* If they're both naive, or both aware and have the same offsets,
+ * we get off cheap. Note that if they're both naive, offset1 ==
+ * offset2 == 0 at this point.
+ */
+ if (n1 == n2 && offset1 == offset2) {
+ diff = memcmp(((PyDateTime_Time *)self)->data,
+ ((PyDateTime_Time *)other)->data,
+ _PyDateTime_TIME_DATASIZE);
+ return diff_to_bool(diff, op);
+ }
+
+ if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
+ assert(offset1 != offset2); /* else last "if" handled it */
+ /* Convert everything except microseconds to seconds. These
+ * can't overflow (no more than the # of seconds in 2 days).
+ */
+ offset1 = TIME_GET_HOUR(self) * 3600 +
+ (TIME_GET_MINUTE(self) - offset1) * 60 +
+ TIME_GET_SECOND(self);
+ offset2 = TIME_GET_HOUR(other) * 3600 +
+ (TIME_GET_MINUTE(other) - offset2) * 60 +
+ TIME_GET_SECOND(other);
+ diff = offset1 - offset2;
+ if (diff == 0)
+ diff = TIME_GET_MICROSECOND(self) -
+ TIME_GET_MICROSECOND(other);
+ return diff_to_bool(diff, op);
+ }
+
+ assert(n1 != n2);
+ PyErr_SetString(PyExc_TypeError,
+ "can't compare offset-naive and "
+ "offset-aware times");
+ return NULL;
}
static long
time_hash(PyDateTime_Time *self)
{
- if (self->hashcode == -1) {
- naivety n;
- int offset;
- PyObject *temp;
-
- n = classify_utcoffset((PyObject *)self, Py_None, &offset);
- assert(n != OFFSET_UNKNOWN);
- if (n == OFFSET_ERROR)
- return -1;
-
- /* Reduce this to a hash of another object. */
- if (offset == 0) {
- self->hashcode = generic_hash(
- (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE);
- return self->hashcode;
- }
- else {
- int hour;
- int minute;
-
- assert(n == OFFSET_AWARE);
- assert(HASTZINFO(self));
- hour = divmod(TIME_GET_HOUR(self) * 60 +
- TIME_GET_MINUTE(self) - offset,
- 60,
- &minute);
- if (0 <= hour && hour < 24)
- temp = new_time(hour, minute,
- TIME_GET_SECOND(self),
- TIME_GET_MICROSECOND(self),
- Py_None);
- else
- temp = Py_BuildValue("iiii",
- hour, minute,
- TIME_GET_SECOND(self),
- TIME_GET_MICROSECOND(self));
- }
- if (temp != NULL) {
- self->hashcode = PyObject_Hash(temp);
- Py_DECREF(temp);
- }
- }
- return self->hashcode;
+ if (self->hashcode == -1) {
+ naivety n;
+ int offset;
+ PyObject *temp;
+
+ n = classify_utcoffset((PyObject *)self, Py_None, &offset);
+ assert(n != OFFSET_UNKNOWN);
+ if (n == OFFSET_ERROR)
+ return -1;
+
+ /* Reduce this to a hash of another object. */
+ if (offset == 0) {
+ self->hashcode = generic_hash(
+ (unsigned char *)self->data, _PyDateTime_TIME_DATASIZE);
+ return self->hashcode;
+ }
+ else {
+ int hour;
+ int minute;
+
+ assert(n == OFFSET_AWARE);
+ assert(HASTZINFO(self));
+ hour = divmod(TIME_GET_HOUR(self) * 60 +
+ TIME_GET_MINUTE(self) - offset,
+ 60,
+ &minute);
+ if (0 <= hour && hour < 24)
+ temp = new_time(hour, minute,
+ TIME_GET_SECOND(self),
+ TIME_GET_MICROSECOND(self),
+ Py_None);
+ else
+ temp = Py_BuildValue("iiii",
+ hour, minute,
+ TIME_GET_SECOND(self),
+ TIME_GET_MICROSECOND(self));
+ }
+ if (temp != NULL) {
+ self->hashcode = PyObject_Hash(temp);
+ Py_DECREF(temp);
+ }
+ }
+ return self->hashcode;
}
static PyObject *
time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
- int hh = TIME_GET_HOUR(self);
- int mm = TIME_GET_MINUTE(self);
- int ss = TIME_GET_SECOND(self);
- int us = TIME_GET_MICROSECOND(self);
- PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
-
- if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
- time_kws,
- &hh, &mm, &ss, &us, &tzinfo))
- return NULL;
- tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
- if (tuple == NULL)
- return NULL;
- clone = time_new(Py_TYPE(self), tuple, NULL);
- Py_DECREF(tuple);
- return clone;
+ PyObject *clone;
+ PyObject *tuple;
+ int hh = TIME_GET_HOUR(self);
+ int mm = TIME_GET_MINUTE(self);
+ int ss = TIME_GET_SECOND(self);
+ int us = TIME_GET_MICROSECOND(self);
+ PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
+ time_kws,
+ &hh, &mm, &ss, &us, &tzinfo))
+ return NULL;
+ tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
+ if (tuple == NULL)
+ return NULL;
+ clone = time_new(Py_TYPE(self), tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
}
static int
time_bool(PyDateTime_Time *self)
{
- int offset;
- int none;
-
- if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) {
- /* Since utcoffset is in whole minutes, nothing can
- * alter the conclusion that this is nonzero.
- */
- return 1;
- }
- offset = 0;
- if (HASTZINFO(self) && self->tzinfo != Py_None) {
- offset = call_utcoffset(self->tzinfo, Py_None, &none);
- if (offset == -1 && PyErr_Occurred())
- return -1;
- }
- return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0;
+ int offset;
+ int none;
+
+ if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) {
+ /* Since utcoffset is in whole minutes, nothing can
+ * alter the conclusion that this is nonzero.
+ */
+ return 1;
+ }
+ offset = 0;
+ if (HASTZINFO(self) && self->tzinfo != Py_None) {
+ offset = call_utcoffset(self->tzinfo, Py_None, &none);
+ if (offset == -1 && PyErr_Occurred())
+ return -1;
+ }
+ return (TIME_GET_MINUTE(self) - offset + TIME_GET_HOUR(self)*60) != 0;
}
/* Pickle support, a simple use of __reduce__. */
@@ -3399,55 +3399,55 @@ time_bool(PyDateTime_Time *self)
static PyObject *
time_getstate(PyDateTime_Time *self)
{
- PyObject *basestate;
- PyObject *result = NULL;
+ PyObject *basestate;
+ PyObject *result = NULL;
- basestate = PyBytes_FromStringAndSize((char *)self->data,
- _PyDateTime_TIME_DATASIZE);
- if (basestate != NULL) {
- if (! HASTZINFO(self) || self->tzinfo == Py_None)
- result = PyTuple_Pack(1, basestate);
- else
- result = PyTuple_Pack(2, basestate, self->tzinfo);
- Py_DECREF(basestate);
- }
- return result;
+ basestate = PyBytes_FromStringAndSize((char *)self->data,
+ _PyDateTime_TIME_DATASIZE);
+ if (basestate != NULL) {
+ if (! HASTZINFO(self) || self->tzinfo == Py_None)
+ result = PyTuple_Pack(1, basestate);
+ else
+ result = PyTuple_Pack(2, basestate, self->tzinfo);
+ Py_DECREF(basestate);
+ }
+ return result;
}
static PyObject *
time_reduce(PyDateTime_Time *self, PyObject *arg)
{
- return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self));
+ return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self));
}
static PyMethodDef time_methods[] = {
- {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS,
- PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
- "[+HH:MM].")},
+ {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS,
+ PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
+ "[+HH:MM].")},
- {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("format -> strftime() style string.")},
+ {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("format -> strftime() style string.")},
- {"__format__", (PyCFunction)date_format, METH_VARARGS,
- PyDoc_STR("Formats self with strftime.")},
+ {"__format__", (PyCFunction)date_format, METH_VARARGS,
+ PyDoc_STR("Formats self with strftime.")},
- {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
+ {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
- {"tzname", (PyCFunction)time_tzname, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.tzname(self).")},
+ {"tzname", (PyCFunction)time_tzname, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.tzname(self).")},
- {"dst", (PyCFunction)time_dst, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.dst(self).")},
+ {"dst", (PyCFunction)time_dst, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.dst(self).")},
- {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("Return time with new specified fields.")},
+ {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("Return time with new specified fields.")},
- {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
- PyDoc_STR("__reduce__() -> (cls, state)")},
+ {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
+ PyDoc_STR("__reduce__() -> (cls, state)")},
- {NULL, NULL}
+ {NULL, NULL}
};
static char time_doc[] =
@@ -3457,58 +3457,58 @@ All arguments are optional. tzinfo may be None, or an instance of\n\
a tzinfo subclass. The remaining arguments may be ints or longs.\n");
static PyNumberMethods time_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- (inquiry)time_bool, /* nb_bool */
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ (inquiry)time_bool, /* nb_bool */
};
static PyTypeObject PyDateTime_TimeType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datetime.time", /* tp_name */
- sizeof(PyDateTime_Time), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)time_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)time_repr, /* tp_repr */
- &time_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)time_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)time_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- time_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- time_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- time_methods, /* tp_methods */
- 0, /* tp_members */
- time_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- time_alloc, /* tp_alloc */
- time_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "datetime.time", /* tp_name */
+ sizeof(PyDateTime_Time), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)time_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)time_repr, /* tp_repr */
+ &time_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)time_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)time_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ time_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ time_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ time_methods, /* tp_methods */
+ 0, /* tp_members */
+ time_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ time_alloc, /* tp_alloc */
+ time_new, /* tp_new */
+ 0, /* tp_free */
};
/*
@@ -3522,42 +3522,42 @@ static PyTypeObject PyDateTime_TimeType = {
static PyObject *
datetime_hour(PyDateTime_DateTime *self, void *unused)
{
- return PyLong_FromLong(DATE_GET_HOUR(self));
+ return PyLong_FromLong(DATE_GET_HOUR(self));
}
static PyObject *
datetime_minute(PyDateTime_DateTime *self, void *unused)
{
- return PyLong_FromLong(DATE_GET_MINUTE(self));
+ return PyLong_FromLong(DATE_GET_MINUTE(self));
}
static PyObject *
datetime_second(PyDateTime_DateTime *self, void *unused)
{
- return PyLong_FromLong(DATE_GET_SECOND(self));
+ return PyLong_FromLong(DATE_GET_SECOND(self));
}
static PyObject *
datetime_microsecond(PyDateTime_DateTime *self, void *unused)
{
- return PyLong_FromLong(DATE_GET_MICROSECOND(self));
+ return PyLong_FromLong(DATE_GET_MICROSECOND(self));
}
static PyObject *
datetime_tzinfo(PyDateTime_DateTime *self, void *unused)
{
- PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
- Py_INCREF(result);
- return result;
+ PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
+ Py_INCREF(result);
+ return result;
}
static PyGetSetDef datetime_getset[] = {
- {"hour", (getter)datetime_hour},
- {"minute", (getter)datetime_minute},
- {"second", (getter)datetime_second},
- {"microsecond", (getter)datetime_microsecond},
- {"tzinfo", (getter)datetime_tzinfo},
- {NULL}
+ {"hour", (getter)datetime_hour},
+ {"minute", (getter)datetime_minute},
+ {"second", (getter)datetime_second},
+ {"microsecond", (getter)datetime_microsecond},
+ {"tzinfo", (getter)datetime_tzinfo},
+ {NULL}
};
/*
@@ -3565,72 +3565,72 @@ static PyGetSetDef datetime_getset[] = {
*/
static char *datetime_kws[] = {
- "year", "month", "day", "hour", "minute", "second",
- "microsecond", "tzinfo", NULL
+ "year", "month", "day", "hour", "minute", "second",
+ "microsecond", "tzinfo", NULL
};
static PyObject *
datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *self = NULL;
- PyObject *state;
- int year;
- int month;
- int day;
- int hour = 0;
- int minute = 0;
- int second = 0;
- int usecond = 0;
- PyObject *tzinfo = Py_None;
-
- /* Check for invocation from pickle with __getstate__ state */
- if (PyTuple_GET_SIZE(args) >= 1 &&
- PyTuple_GET_SIZE(args) <= 2 &&
- PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
- PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
- MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
- {
- PyDateTime_DateTime *me;
- char aware;
-
- if (PyTuple_GET_SIZE(args) == 2) {
- tzinfo = PyTuple_GET_ITEM(args, 1);
- if (check_tzinfo_subclass(tzinfo) < 0) {
- PyErr_SetString(PyExc_TypeError, "bad "
- "tzinfo state arg");
- return NULL;
- }
- }
- aware = (char)(tzinfo != Py_None);
- me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
- if (me != NULL) {
- char *pdata = PyBytes_AS_STRING(state);
-
- memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
- me->hashcode = -1;
- me->hastzinfo = aware;
- if (aware) {
- Py_INCREF(tzinfo);
- me->tzinfo = tzinfo;
- }
- }
- return (PyObject *)me;
- }
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws,
- &year, &month, &day, &hour, &minute,
- &second, &usecond, &tzinfo)) {
- if (check_date_args(year, month, day) < 0)
- return NULL;
- if (check_time_args(hour, minute, second, usecond) < 0)
- return NULL;
- if (check_tzinfo_subclass(tzinfo) < 0)
- return NULL;
- self = new_datetime_ex(year, month, day,
- hour, minute, second, usecond,
- tzinfo, type);
- }
- return self;
+ PyObject *self = NULL;
+ PyObject *state;
+ int year;
+ int month;
+ int day;
+ int hour = 0;
+ int minute = 0;
+ int second = 0;
+ int usecond = 0;
+ PyObject *tzinfo = Py_None;
+
+ /* Check for invocation from pickle with __getstate__ state */
+ if (PyTuple_GET_SIZE(args) >= 1 &&
+ PyTuple_GET_SIZE(args) <= 2 &&
+ PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) &&
+ PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
+ MONTH_IS_SANE(PyBytes_AS_STRING(state)[2]))
+ {
+ PyDateTime_DateTime *me;
+ char aware;
+
+ if (PyTuple_GET_SIZE(args) == 2) {
+ tzinfo = PyTuple_GET_ITEM(args, 1);
+ if (check_tzinfo_subclass(tzinfo) < 0) {
+ PyErr_SetString(PyExc_TypeError, "bad "
+ "tzinfo state arg");
+ return NULL;
+ }
+ }
+ aware = (char)(tzinfo != Py_None);
+ me = (PyDateTime_DateTime *) (type->tp_alloc(type , aware));
+ if (me != NULL) {
+ char *pdata = PyBytes_AS_STRING(state);
+
+ memcpy(me->data, pdata, _PyDateTime_DATETIME_DATASIZE);
+ me->hashcode = -1;
+ me->hastzinfo = aware;
+ if (aware) {
+ Py_INCREF(tzinfo);
+ me->tzinfo = tzinfo;
+ }
+ }
+ return (PyObject *)me;
+ }
+
+ if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws,
+ &year, &month, &day, &hour, &minute,
+ &second, &usecond, &tzinfo)) {
+ if (check_date_args(year, month, day) < 0)
+ return NULL;
+ if (check_time_args(hour, minute, second, usecond) < 0)
+ return NULL;
+ if (check_tzinfo_subclass(tzinfo) < 0)
+ return NULL;
+ self = new_datetime_ex(year, month, day,
+ hour, minute, second, usecond,
+ tzinfo, type);
+ }
+ return self;
}
/* TM_FUNC is the shared type of localtime() and gmtime(). */
@@ -3642,36 +3642,36 @@ typedef struct tm *(*TM_FUNC)(const time_t *timer);
*/
static PyObject *
datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
- PyObject *tzinfo)
-{
- struct tm *tm;
- PyObject *result = NULL;
-
- tm = f(&timet);
- if (tm) {
- /* The platform localtime/gmtime may insert leap seconds,
- * indicated by tm->tm_sec > 59. We don't care about them,
- * except to the extent that passing them on to the datetime
- * constructor would raise ValueError for a reason that
- * made no sense to the user.
- */
- if (tm->tm_sec > 59)
- tm->tm_sec = 59;
- result = PyObject_CallFunction(cls, "iiiiiiiO",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- us,
- tzinfo);
- }
- else
- PyErr_SetString(PyExc_ValueError,
- "timestamp out of range for "
- "platform localtime()/gmtime() function");
- return result;
+ PyObject *tzinfo)
+{
+ struct tm *tm;
+ PyObject *result = NULL;
+
+ tm = f(&timet);
+ if (tm) {
+ /* The platform localtime/gmtime may insert leap seconds,
+ * indicated by tm->tm_sec > 59. We don't care about them,
+ * except to the extent that passing them on to the datetime
+ * constructor would raise ValueError for a reason that
+ * made no sense to the user.
+ */
+ if (tm->tm_sec > 59)
+ tm->tm_sec = 59;
+ result = PyObject_CallFunction(cls, "iiiiiiiO",
+ tm->tm_year + 1900,
+ tm->tm_mon + 1,
+ tm->tm_mday,
+ tm->tm_hour,
+ tm->tm_min,
+ tm->tm_sec,
+ us,
+ tzinfo);
+ }
+ else
+ PyErr_SetString(PyExc_ValueError,
+ "timestamp out of range for "
+ "platform localtime()/gmtime() function");
+ return result;
}
/* Internal helper.
@@ -3683,31 +3683,31 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
*/
static PyObject *
datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
- PyObject *tzinfo)
-{
- time_t timet;
- double fraction;
- int us;
-
- timet = _PyTime_DoubleToTimet(timestamp);
- if (timet == (time_t)-1 && PyErr_Occurred())
- return NULL;
- fraction = timestamp - (double)timet;
- us = (int)round_to_long(fraction * 1e6);
- if (us < 0) {
- /* Truncation towards zero is not what we wanted
- for negative numbers (Python's mod semantics) */
- timet -= 1;
- us += 1000000;
- }
- /* If timestamp is less than one microsecond smaller than a
- * full second, round up. Otherwise, ValueErrors are raised
- * for some floats. */
- if (us == 1000000) {
- timet += 1;
- us = 0;
- }
- return datetime_from_timet_and_us(cls, f, timet, us, tzinfo);
+ PyObject *tzinfo)
+{
+ time_t timet;
+ double fraction;
+ int us;
+
+ timet = _PyTime_DoubleToTimet(timestamp);
+ if (timet == (time_t)-1 && PyErr_Occurred())
+ return NULL;
+ fraction = timestamp - (double)timet;
+ us = (int)round_to_long(fraction * 1e6);
+ if (us < 0) {
+ /* Truncation towards zero is not what we wanted
+ for negative numbers (Python's mod semantics) */
+ timet -= 1;
+ us += 1000000;
+ }
+ /* If timestamp is less than one microsecond smaller than a
+ * full second, round up. Otherwise, ValueErrors are raised
+ * for some floats. */
+ if (us == 1000000) {
+ timet += 1;
+ us = 0;
+ }
+ return datetime_from_timet_and_us(cls, f, timet, us, tzinfo);
}
/* Internal helper.
@@ -3718,36 +3718,36 @@ static PyObject *
datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
{
#ifdef HAVE_GETTIMEOFDAY
- struct timeval t;
+ struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
- gettimeofday(&t);
+ gettimeofday(&t);
#else
- gettimeofday(&t, (struct timezone *)NULL);
+ gettimeofday(&t, (struct timezone *)NULL);
#endif
- return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec,
- tzinfo);
-
-#else /* ! HAVE_GETTIMEOFDAY */
- /* No flavor of gettimeofday exists on this platform. Python's
- * time.time() does a lot of other platform tricks to get the
- * best time it can on the platform, and we're not going to do
- * better than that (if we could, the better code would belong
- * in time.time()!) We're limited by the precision of a double,
- * though.
- */
- PyObject *time;
- double dtime;
-
- time = time_time();
- if (time == NULL)
- return NULL;
- dtime = PyFloat_AsDouble(time);
- Py_DECREF(time);
- if (dtime == -1.0 && PyErr_Occurred())
- return NULL;
- return datetime_from_timestamp(cls, f, dtime, tzinfo);
-#endif /* ! HAVE_GETTIMEOFDAY */
+ return datetime_from_timet_and_us(cls, f, t.tv_sec, (int)t.tv_usec,
+ tzinfo);
+
+#else /* ! HAVE_GETTIMEOFDAY */
+ /* No flavor of gettimeofday exists on this platform. Python's
+ * time.time() does a lot of other platform tricks to get the
+ * best time it can on the platform, and we're not going to do
+ * better than that (if we could, the better code would belong
+ * in time.time()!) We're limited by the precision of a double,
+ * though.
+ */
+ PyObject *time;
+ double dtime;
+
+ time = time_time();
+ if (time == NULL)
+ return NULL;
+ dtime = PyFloat_AsDouble(time);
+ Py_DECREF(time);
+ if (dtime == -1.0 && PyErr_Occurred())
+ return NULL;
+ return datetime_from_timestamp(cls, f, dtime, tzinfo);
+#endif /* ! HAVE_GETTIMEOFDAY */
}
/* Return best possible local time -- this isn't constrained by the
@@ -3756,26 +3756,26 @@ datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
static PyObject *
datetime_now(PyObject *cls, PyObject *args, PyObject *kw)
{
- PyObject *self;
- PyObject *tzinfo = Py_None;
- static char *keywords[] = {"tz", NULL};
+ PyObject *self;
+ PyObject *tzinfo = Py_None;
+ static char *keywords[] = {"tz", NULL};
- if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
- &tzinfo))
- return NULL;
- if (check_tzinfo_subclass(tzinfo) < 0)
- return NULL;
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
+ &tzinfo))
+ return NULL;
+ if (check_tzinfo_subclass(tzinfo) < 0)
+ return NULL;
- self = datetime_best_possible(cls,
- tzinfo == Py_None ? localtime : gmtime,
- tzinfo);
- if (self != NULL && tzinfo != Py_None) {
- /* Convert UTC to tzinfo's zone. */
- PyObject *temp = self;
- self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
- Py_DECREF(temp);
- }
- return self;
+ self = datetime_best_possible(cls,
+ tzinfo == Py_None ? localtime : gmtime,
+ tzinfo);
+ if (self != NULL && tzinfo != Py_None) {
+ /* Convert UTC to tzinfo's zone. */
+ PyObject *temp = self;
+ self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
+ Py_DECREF(temp);
+ }
+ return self;
}
/* Return best possible UTC time -- this isn't constrained by the
@@ -3784,155 +3784,155 @@ datetime_now(PyObject *cls, PyObject *args, PyObject *kw)
static PyObject *
datetime_utcnow(PyObject *cls, PyObject *dummy)
{
- return datetime_best_possible(cls, gmtime, Py_None);
+ return datetime_best_possible(cls, gmtime, Py_None);
}
/* Return new local datetime from timestamp (Python timestamp -- a double). */
static PyObject *
datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
{
- PyObject *self;
- double timestamp;
- PyObject *tzinfo = Py_None;
- static char *keywords[] = {"timestamp", "tz", NULL};
-
- if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp",
- keywords, &timestamp, &tzinfo))
- return NULL;
- if (check_tzinfo_subclass(tzinfo) < 0)
- return NULL;
-
- self = datetime_from_timestamp(cls,
- tzinfo == Py_None ? localtime : gmtime,
- timestamp,
- tzinfo);
- if (self != NULL && tzinfo != Py_None) {
- /* Convert UTC to tzinfo's zone. */
- PyObject *temp = self;
- self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
- Py_DECREF(temp);
- }
- return self;
+ PyObject *self;
+ double timestamp;
+ PyObject *tzinfo = Py_None;
+ static char *keywords[] = {"timestamp", "tz", NULL};
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp",
+ keywords, &timestamp, &tzinfo))
+ return NULL;
+ if (check_tzinfo_subclass(tzinfo) < 0)
+ return NULL;
+
+ self = datetime_from_timestamp(cls,
+ tzinfo == Py_None ? localtime : gmtime,
+ timestamp,
+ tzinfo);
+ if (self != NULL && tzinfo != Py_None) {
+ /* Convert UTC to tzinfo's zone. */
+ PyObject *temp = self;
+ self = PyObject_CallMethod(tzinfo, "fromutc", "O", self);
+ Py_DECREF(temp);
+ }
+ return self;
}
/* Return new UTC datetime from timestamp (Python timestamp -- a double). */
static PyObject *
datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
{
- double timestamp;
- PyObject *result = NULL;
+ double timestamp;
+ PyObject *result = NULL;
- if (PyArg_ParseTuple(args, "d:utcfromtimestamp", &timestamp))
- result = datetime_from_timestamp(cls, gmtime, timestamp,
- Py_None);
- return result;
+ if (PyArg_ParseTuple(args, "d:utcfromtimestamp", &timestamp))
+ result = datetime_from_timestamp(cls, gmtime, timestamp,
+ Py_None);
+ return result;
}
/* Return new datetime from time.strptime(). */
static PyObject *
datetime_strptime(PyObject *cls, PyObject *args)
{
- static PyObject *module = NULL;
- PyObject *result = NULL, *obj, *st = NULL, *frac = NULL;
- const Py_UNICODE *string, *format;
-
- if (!PyArg_ParseTuple(args, "uu:strptime", &string, &format))
- return NULL;
-
- if (module == NULL &&
- (module = PyImport_ImportModuleNoBlock("_strptime")) == NULL)
- return NULL;
-
- /* _strptime._strptime returns a two-element tuple. The first
- element is a time.struct_time object. The second is the
- microseconds (which are not defined for time.struct_time). */
- obj = PyObject_CallMethod(module, "_strptime", "uu", string, format);
- if (obj != NULL) {
- int i, good_timetuple = 1;
- long int ia[7];
- if (PySequence_Check(obj) && PySequence_Size(obj) == 2) {
- st = PySequence_GetItem(obj, 0);
- frac = PySequence_GetItem(obj, 1);
- if (st == NULL || frac == NULL)
- good_timetuple = 0;
- /* copy y/m/d/h/m/s values out of the
- time.struct_time */
- if (good_timetuple &&
- PySequence_Check(st) &&
- PySequence_Size(st) >= 6) {
- for (i=0; i < 6; i++) {
- PyObject *p = PySequence_GetItem(st, i);
- if (p == NULL) {
- good_timetuple = 0;
- break;
- }
- if (PyLong_Check(p))
- ia[i] = PyLong_AsLong(p);
- else
- good_timetuple = 0;
- Py_DECREF(p);
- }
-/* if (PyLong_CheckExact(p)) {
- ia[i] = PyLong_AsLongAndOverflow(p, &overflow);
- if (overflow)
- good_timetuple = 0;
- }
- else
- good_timetuple = 0;
- Py_DECREF(p);
-*/ }
- else
- good_timetuple = 0;
- /* follow that up with a little dose of microseconds */
- if (PyLong_Check(frac))
- ia[6] = PyLong_AsLong(frac);
- else
- good_timetuple = 0;
- }
- else
- good_timetuple = 0;
- if (good_timetuple)
- result = PyObject_CallFunction(cls, "iiiiiii",
- ia[0], ia[1], ia[2],
- ia[3], ia[4], ia[5],
- ia[6]);
- else
- PyErr_SetString(PyExc_ValueError,
- "unexpected value from _strptime._strptime");
- }
- Py_XDECREF(obj);
- Py_XDECREF(st);
- Py_XDECREF(frac);
- return result;
+ static PyObject *module = NULL;
+ PyObject *result = NULL, *obj, *st = NULL, *frac = NULL;
+ const Py_UNICODE *string, *format;
+
+ if (!PyArg_ParseTuple(args, "uu:strptime", &string, &format))
+ return NULL;
+
+ if (module == NULL &&
+ (module = PyImport_ImportModuleNoBlock("_strptime")) == NULL)
+ return NULL;
+
+ /* _strptime._strptime returns a two-element tuple. The first
+ element is a time.struct_time object. The second is the
+ microseconds (which are not defined for time.struct_time). */
+ obj = PyObject_CallMethod(module, "_strptime", "uu", string, format);
+ if (obj != NULL) {
+ int i, good_timetuple = 1;
+ long int ia[7];
+ if (PySequence_Check(obj) && PySequence_Size(obj) == 2) {
+ st = PySequence_GetItem(obj, 0);
+ frac = PySequence_GetItem(obj, 1);
+ if (st == NULL || frac == NULL)
+ good_timetuple = 0;
+ /* copy y/m/d/h/m/s values out of the
+ time.struct_time */
+ if (good_timetuple &&
+ PySequence_Check(st) &&
+ PySequence_Size(st) >= 6) {
+ for (i=0; i < 6; i++) {
+ PyObject *p = PySequence_GetItem(st, i);
+ if (p == NULL) {
+ good_timetuple = 0;
+ break;
+ }
+ if (PyLong_Check(p))
+ ia[i] = PyLong_AsLong(p);
+ else
+ good_timetuple = 0;
+ Py_DECREF(p);
+ }
+/* if (PyLong_CheckExact(p)) {
+ ia[i] = PyLong_AsLongAndOverflow(p, &overflow);
+ if (overflow)
+ good_timetuple = 0;
+ }
+ else
+ good_timetuple = 0;
+ Py_DECREF(p);
+*/ }
+ else
+ good_timetuple = 0;
+ /* follow that up with a little dose of microseconds */
+ if (PyLong_Check(frac))
+ ia[6] = PyLong_AsLong(frac);
+ else
+ good_timetuple = 0;
+ }
+ else
+ good_timetuple = 0;
+ if (good_timetuple)
+ result = PyObject_CallFunction(cls, "iiiiiii",
+ ia[0], ia[1], ia[2],
+ ia[3], ia[4], ia[5],
+ ia[6]);
+ else
+ PyErr_SetString(PyExc_ValueError,
+ "unexpected value from _strptime._strptime");
+ }
+ Py_XDECREF(obj);
+ Py_XDECREF(st);
+ Py_XDECREF(frac);
+ return result;
}
/* Return new datetime from date/datetime and time arguments. */
static PyObject *
datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
{
- static char *keywords[] = {"date", "time", NULL};
- PyObject *date;
- PyObject *time;
- PyObject *result = NULL;
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords,
- &PyDateTime_DateType, &date,
- &PyDateTime_TimeType, &time)) {
- PyObject *tzinfo = Py_None;
-
- if (HASTZINFO(time))
- tzinfo = ((PyDateTime_Time *)time)->tzinfo;
- result = PyObject_CallFunction(cls, "iiiiiiiO",
- GET_YEAR(date),
- GET_MONTH(date),
- GET_DAY(date),
- TIME_GET_HOUR(time),
- TIME_GET_MINUTE(time),
- TIME_GET_SECOND(time),
- TIME_GET_MICROSECOND(time),
- tzinfo);
- }
- return result;
+ static char *keywords[] = {"date", "time", NULL};
+ PyObject *date;
+ PyObject *time;
+ PyObject *result = NULL;
+
+ if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords,
+ &PyDateTime_DateType, &date,
+ &PyDateTime_TimeType, &time)) {
+ PyObject *tzinfo = Py_None;
+
+ if (HASTZINFO(time))
+ tzinfo = ((PyDateTime_Time *)time)->tzinfo;
+ result = PyObject_CallFunction(cls, "iiiiiiiO",
+ GET_YEAR(date),
+ GET_MONTH(date),
+ GET_DAY(date),
+ TIME_GET_HOUR(time),
+ TIME_GET_MINUTE(time),
+ TIME_GET_SECOND(time),
+ TIME_GET_MICROSECOND(time),
+ tzinfo);
+ }
+ return result;
}
/*
@@ -3942,10 +3942,10 @@ datetime_combine(PyObject *cls, PyObject *args, PyObject *kw)
static void
datetime_dealloc(PyDateTime_DateTime *self)
{
- if (HASTZINFO(self)) {
- Py_XDECREF(self->tzinfo);
- }
- Py_TYPE(self)->tp_free((PyObject *)self);
+ if (HASTZINFO(self)) {
+ Py_XDECREF(self->tzinfo);
+ }
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
/*
@@ -3955,20 +3955,20 @@ datetime_dealloc(PyDateTime_DateTime *self)
/* These are all METH_NOARGS, so don't need to check the arglist. */
static PyObject *
datetime_utcoffset(PyDateTime_DateTime *self, PyObject *unused) {
- return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
- "utcoffset", (PyObject *)self);
+ return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
+ "utcoffset", (PyObject *)self);
}
static PyObject *
datetime_dst(PyDateTime_DateTime *self, PyObject *unused) {
- return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
- "dst", (PyObject *)self);
+ return offset_as_timedelta(HASTZINFO(self) ? self->tzinfo : Py_None,
+ "dst", (PyObject *)self);
}
static PyObject *
datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) {
- return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
- (PyObject *)self);
+ return call_tzname(HASTZINFO(self) ? self->tzinfo : Py_None,
+ (PyObject *)self);
}
/*
@@ -3980,112 +3980,112 @@ datetime_tzname(PyDateTime_DateTime *self, PyObject *unused) {
*/
static PyObject *
add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta,
- int factor)
-{
- /* Note that the C-level additions can't overflow, because of
- * invariant bounds on the member values.
- */
- int year = GET_YEAR(date);
- int month = GET_MONTH(date);
- int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor;
- int hour = DATE_GET_HOUR(date);
- int minute = DATE_GET_MINUTE(date);
- int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor;
- int microsecond = DATE_GET_MICROSECOND(date) +
- GET_TD_MICROSECONDS(delta) * factor;
-
- assert(factor == 1 || factor == -1);
- if (normalize_datetime(&year, &month, &day,
- &hour, &minute, &second, &microsecond) < 0)
- return NULL;
- else
- return new_datetime(year, month, day,
- hour, minute, second, microsecond,
- HASTZINFO(date) ? date->tzinfo : Py_None);
+ int factor)
+{
+ /* Note that the C-level additions can't overflow, because of
+ * invariant bounds on the member values.
+ */
+ int year = GET_YEAR(date);
+ int month = GET_MONTH(date);
+ int day = GET_DAY(date) + GET_TD_DAYS(delta) * factor;
+ int hour = DATE_GET_HOUR(date);
+ int minute = DATE_GET_MINUTE(date);
+ int second = DATE_GET_SECOND(date) + GET_TD_SECONDS(delta) * factor;
+ int microsecond = DATE_GET_MICROSECOND(date) +
+ GET_TD_MICROSECONDS(delta) * factor;
+
+ assert(factor == 1 || factor == -1);
+ if (normalize_datetime(&year, &month, &day,
+ &hour, &minute, &second, &microsecond) < 0)
+ return NULL;
+ else
+ return new_datetime(year, month, day,
+ hour, minute, second, microsecond,
+ HASTZINFO(date) ? date->tzinfo : Py_None);
}
static PyObject *
datetime_add(PyObject *left, PyObject *right)
{
- if (PyDateTime_Check(left)) {
- /* datetime + ??? */
- if (PyDelta_Check(right))
- /* datetime + delta */
- return add_datetime_timedelta(
- (PyDateTime_DateTime *)left,
- (PyDateTime_Delta *)right,
- 1);
- }
- else if (PyDelta_Check(left)) {
- /* delta + datetime */
- return add_datetime_timedelta((PyDateTime_DateTime *) right,
- (PyDateTime_Delta *) left,
- 1);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ if (PyDateTime_Check(left)) {
+ /* datetime + ??? */
+ if (PyDelta_Check(right))
+ /* datetime + delta */
+ return add_datetime_timedelta(
+ (PyDateTime_DateTime *)left,
+ (PyDateTime_Delta *)right,
+ 1);
+ }
+ else if (PyDelta_Check(left)) {
+ /* delta + datetime */
+ return add_datetime_timedelta((PyDateTime_DateTime *) right,
+ (PyDateTime_Delta *) left,
+ 1);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyObject *
datetime_subtract(PyObject *left, PyObject *right)
{
- PyObject *result = Py_NotImplemented;
-
- if (PyDateTime_Check(left)) {
- /* datetime - ??? */
- if (PyDateTime_Check(right)) {
- /* datetime - datetime */
- naivety n1, n2;
- int offset1, offset2;
- int delta_d, delta_s, delta_us;
-
- if (classify_two_utcoffsets(left, &offset1, &n1, left,
- right, &offset2, &n2,
- right) < 0)
- return NULL;
- assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
- if (n1 != n2) {
- PyErr_SetString(PyExc_TypeError,
- "can't subtract offset-naive and "
- "offset-aware datetimes");
- return NULL;
- }
- delta_d = ymd_to_ord(GET_YEAR(left),
- GET_MONTH(left),
- GET_DAY(left)) -
- ymd_to_ord(GET_YEAR(right),
- GET_MONTH(right),
- GET_DAY(right));
- /* These can't overflow, since the values are
- * normalized. At most this gives the number of
- * seconds in one day.
- */
- delta_s = (DATE_GET_HOUR(left) -
- DATE_GET_HOUR(right)) * 3600 +
- (DATE_GET_MINUTE(left) -
- DATE_GET_MINUTE(right)) * 60 +
- (DATE_GET_SECOND(left) -
- DATE_GET_SECOND(right));
- delta_us = DATE_GET_MICROSECOND(left) -
- DATE_GET_MICROSECOND(right);
- /* (left - offset1) - (right - offset2) =
- * (left - right) + (offset2 - offset1)
- */
- delta_s += (offset2 - offset1) * 60;
- result = new_delta(delta_d, delta_s, delta_us, 1);
- }
- else if (PyDelta_Check(right)) {
- /* datetime - delta */
- result = add_datetime_timedelta(
- (PyDateTime_DateTime *)left,
- (PyDateTime_Delta *)right,
- -1);
- }
- }
-
- if (result == Py_NotImplemented)
- Py_INCREF(result);
- return result;
+ PyObject *result = Py_NotImplemented;
+
+ if (PyDateTime_Check(left)) {
+ /* datetime - ??? */
+ if (PyDateTime_Check(right)) {
+ /* datetime - datetime */
+ naivety n1, n2;
+ int offset1, offset2;
+ int delta_d, delta_s, delta_us;
+
+ if (classify_two_utcoffsets(left, &offset1, &n1, left,
+ right, &offset2, &n2,
+ right) < 0)
+ return NULL;
+ assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
+ if (n1 != n2) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't subtract offset-naive and "
+ "offset-aware datetimes");
+ return NULL;
+ }
+ delta_d = ymd_to_ord(GET_YEAR(left),
+ GET_MONTH(left),
+ GET_DAY(left)) -
+ ymd_to_ord(GET_YEAR(right),
+ GET_MONTH(right),
+ GET_DAY(right));
+ /* These can't overflow, since the values are
+ * normalized. At most this gives the number of
+ * seconds in one day.
+ */
+ delta_s = (DATE_GET_HOUR(left) -
+ DATE_GET_HOUR(right)) * 3600 +
+ (DATE_GET_MINUTE(left) -
+ DATE_GET_MINUTE(right)) * 60 +
+ (DATE_GET_SECOND(left) -
+ DATE_GET_SECOND(right));
+ delta_us = DATE_GET_MICROSECOND(left) -
+ DATE_GET_MICROSECOND(right);
+ /* (left - offset1) - (right - offset2) =
+ * (left - right) + (offset2 - offset1)
+ */
+ delta_s += (offset2 - offset1) * 60;
+ result = new_delta(delta_d, delta_s, delta_us, 1);
+ }
+ else if (PyDelta_Check(right)) {
+ /* datetime - delta */
+ result = add_datetime_timedelta(
+ (PyDateTime_DateTime *)left,
+ (PyDateTime_Delta *)right,
+ -1);
+ }
+ }
+
+ if (result == Py_NotImplemented)
+ Py_INCREF(result);
+ return result;
}
/* Various ways to turn a datetime into a string. */
@@ -4093,88 +4093,88 @@ datetime_subtract(PyObject *left, PyObject *right)
static PyObject *
datetime_repr(PyDateTime_DateTime *self)
{
- const char *type_name = Py_TYPE(self)->tp_name;
- PyObject *baserepr;
-
- if (DATE_GET_MICROSECOND(self)) {
- baserepr = PyUnicode_FromFormat(
- "%s(%d, %d, %d, %d, %d, %d, %d)",
- type_name,
- GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self),
- DATE_GET_MICROSECOND(self));
- }
- else if (DATE_GET_SECOND(self)) {
- baserepr = PyUnicode_FromFormat(
- "%s(%d, %d, %d, %d, %d, %d)",
- type_name,
- GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self));
- }
- else {
- baserepr = PyUnicode_FromFormat(
- "%s(%d, %d, %d, %d, %d)",
- type_name,
- GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
- }
- if (baserepr == NULL || ! HASTZINFO(self))
- return baserepr;
- return append_keyword_tzinfo(baserepr, self->tzinfo);
+ const char *type_name = Py_TYPE(self)->tp_name;
+ PyObject *baserepr;
+
+ if (DATE_GET_MICROSECOND(self)) {
+ baserepr = PyUnicode_FromFormat(
+ "%s(%d, %d, %d, %d, %d, %d, %d)",
+ type_name,
+ GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
+ DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self),
+ DATE_GET_MICROSECOND(self));
+ }
+ else if (DATE_GET_SECOND(self)) {
+ baserepr = PyUnicode_FromFormat(
+ "%s(%d, %d, %d, %d, %d, %d)",
+ type_name,
+ GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
+ DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self));
+ }
+ else {
+ baserepr = PyUnicode_FromFormat(
+ "%s(%d, %d, %d, %d, %d)",
+ type_name,
+ GET_YEAR(self), GET_MONTH(self), GET_DAY(self),
+ DATE_GET_HOUR(self), DATE_GET_MINUTE(self));
+ }
+ if (baserepr == NULL || ! HASTZINFO(self))
+ return baserepr;
+ return append_keyword_tzinfo(baserepr, self->tzinfo);
}
static PyObject *
datetime_str(PyDateTime_DateTime *self)
{
- return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " ");
+ return PyObject_CallMethod((PyObject *)self, "isoformat", "(s)", " ");
}
static PyObject *
datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
- int sep = 'T';
- static char *keywords[] = {"sep", NULL};
- char buffer[100];
- PyObject *result;
- int us = DATE_GET_MICROSECOND(self);
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep))
- return NULL;
- if (us)
- result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d",
- GET_YEAR(self), GET_MONTH(self),
- GET_DAY(self), (int)sep,
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self), us);
- else
- result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d",
- GET_YEAR(self), GET_MONTH(self),
- GET_DAY(self), (int)sep,
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self));
-
- if (!result || !HASTZINFO(self))
- return result;
-
- /* We need to append the UTC offset. */
- if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo,
- (PyObject *)self) < 0) {
- Py_DECREF(result);
- return NULL;
- }
- PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer));
- return result;
+ int sep = 'T';
+ static char *keywords[] = {"sep", NULL};
+ char buffer[100];
+ PyObject *result;
+ int us = DATE_GET_MICROSECOND(self);
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep))
+ return NULL;
+ if (us)
+ result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d",
+ GET_YEAR(self), GET_MONTH(self),
+ GET_DAY(self), (int)sep,
+ DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self), us);
+ else
+ result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d",
+ GET_YEAR(self), GET_MONTH(self),
+ GET_DAY(self), (int)sep,
+ DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self));
+
+ if (!result || !HASTZINFO(self))
+ return result;
+
+ /* We need to append the UTC offset. */
+ if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo,
+ (PyObject *)self) < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyUnicode_AppendAndDel(&result, PyUnicode_FromString(buffer));
+ return result;
}
static PyObject *
datetime_ctime(PyDateTime_DateTime *self)
{
- return format_ctime((PyDateTime_Date *)self,
- DATE_GET_HOUR(self),
- DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self));
+ return format_ctime((PyDateTime_Date *)self,
+ DATE_GET_HOUR(self),
+ DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self));
}
/* Miscellaneous methods. */
@@ -4182,292 +4182,292 @@ datetime_ctime(PyDateTime_DateTime *self)
static PyObject *
datetime_richcompare(PyObject *self, PyObject *other, int op)
{
- int diff;
- naivety n1, n2;
- int offset1, offset2;
-
- if (! PyDateTime_Check(other)) {
- if (PyDate_Check(other)) {
- /* Prevent invocation of date_richcompare. We want to
- return NotImplemented here to give the other object
- a chance. But since DateTime is a subclass of
- Date, if the other object is a Date, it would
- compute an ordering based on the date part alone,
- and we don't want that. So force unequal or
- uncomparable here in that case. */
- if (op == Py_EQ)
- Py_RETURN_FALSE;
- if (op == Py_NE)
- Py_RETURN_TRUE;
- return cmperror(self, other);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- if (classify_two_utcoffsets(self, &offset1, &n1, self,
- other, &offset2, &n2, other) < 0)
- return NULL;
- assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
- /* If they're both naive, or both aware and have the same offsets,
- * we get off cheap. Note that if they're both naive, offset1 ==
- * offset2 == 0 at this point.
- */
- if (n1 == n2 && offset1 == offset2) {
- diff = memcmp(((PyDateTime_DateTime *)self)->data,
- ((PyDateTime_DateTime *)other)->data,
- _PyDateTime_DATETIME_DATASIZE);
- return diff_to_bool(diff, op);
- }
-
- if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
- PyDateTime_Delta *delta;
-
- assert(offset1 != offset2); /* else last "if" handled it */
- delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self,
- other);
- if (delta == NULL)
- return NULL;
- diff = GET_TD_DAYS(delta);
- if (diff == 0)
- diff = GET_TD_SECONDS(delta) |
- GET_TD_MICROSECONDS(delta);
- Py_DECREF(delta);
- return diff_to_bool(diff, op);
- }
-
- assert(n1 != n2);
- PyErr_SetString(PyExc_TypeError,
- "can't compare offset-naive and "
- "offset-aware datetimes");
- return NULL;
+ int diff;
+ naivety n1, n2;
+ int offset1, offset2;
+
+ if (! PyDateTime_Check(other)) {
+ if (PyDate_Check(other)) {
+ /* Prevent invocation of date_richcompare. We want to
+ return NotImplemented here to give the other object
+ a chance. But since DateTime is a subclass of
+ Date, if the other object is a Date, it would
+ compute an ordering based on the date part alone,
+ and we don't want that. So force unequal or
+ uncomparable here in that case. */
+ if (op == Py_EQ)
+ Py_RETURN_FALSE;
+ if (op == Py_NE)
+ Py_RETURN_TRUE;
+ return cmperror(self, other);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ if (classify_two_utcoffsets(self, &offset1, &n1, self,
+ other, &offset2, &n2, other) < 0)
+ return NULL;
+ assert(n1 != OFFSET_UNKNOWN && n2 != OFFSET_UNKNOWN);
+ /* If they're both naive, or both aware and have the same offsets,
+ * we get off cheap. Note that if they're both naive, offset1 ==
+ * offset2 == 0 at this point.
+ */
+ if (n1 == n2 && offset1 == offset2) {
+ diff = memcmp(((PyDateTime_DateTime *)self)->data,
+ ((PyDateTime_DateTime *)other)->data,
+ _PyDateTime_DATETIME_DATASIZE);
+ return diff_to_bool(diff, op);
+ }
+
+ if (n1 == OFFSET_AWARE && n2 == OFFSET_AWARE) {
+ PyDateTime_Delta *delta;
+
+ assert(offset1 != offset2); /* else last "if" handled it */
+ delta = (PyDateTime_Delta *)datetime_subtract((PyObject *)self,
+ other);
+ if (delta == NULL)
+ return NULL;
+ diff = GET_TD_DAYS(delta);
+ if (diff == 0)
+ diff = GET_TD_SECONDS(delta) |
+ GET_TD_MICROSECONDS(delta);
+ Py_DECREF(delta);
+ return diff_to_bool(diff, op);
+ }
+
+ assert(n1 != n2);
+ PyErr_SetString(PyExc_TypeError,
+ "can't compare offset-naive and "
+ "offset-aware datetimes");
+ return NULL;
}
static long
datetime_hash(PyDateTime_DateTime *self)
{
- if (self->hashcode == -1) {
- naivety n;
- int offset;
- PyObject *temp;
-
- n = classify_utcoffset((PyObject *)self, (PyObject *)self,
- &offset);
- assert(n != OFFSET_UNKNOWN);
- if (n == OFFSET_ERROR)
- return -1;
-
- /* Reduce this to a hash of another object. */
- if (n == OFFSET_NAIVE) {
- self->hashcode = generic_hash(
- (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE);
- return self->hashcode;
- }
- else {
- int days;
- int seconds;
-
- assert(n == OFFSET_AWARE);
- assert(HASTZINFO(self));
- days = ymd_to_ord(GET_YEAR(self),
- GET_MONTH(self),
- GET_DAY(self));
- seconds = DATE_GET_HOUR(self) * 3600 +
- (DATE_GET_MINUTE(self) - offset) * 60 +
- DATE_GET_SECOND(self);
- temp = new_delta(days,
- seconds,
- DATE_GET_MICROSECOND(self),
- 1);
- }
- if (temp != NULL) {
- self->hashcode = PyObject_Hash(temp);
- Py_DECREF(temp);
- }
- }
- return self->hashcode;
+ if (self->hashcode == -1) {
+ naivety n;
+ int offset;
+ PyObject *temp;
+
+ n = classify_utcoffset((PyObject *)self, (PyObject *)self,
+ &offset);
+ assert(n != OFFSET_UNKNOWN);
+ if (n == OFFSET_ERROR)
+ return -1;
+
+ /* Reduce this to a hash of another object. */
+ if (n == OFFSET_NAIVE) {
+ self->hashcode = generic_hash(
+ (unsigned char *)self->data, _PyDateTime_DATETIME_DATASIZE);
+ return self->hashcode;
+ }
+ else {
+ int days;
+ int seconds;
+
+ assert(n == OFFSET_AWARE);
+ assert(HASTZINFO(self));
+ days = ymd_to_ord(GET_YEAR(self),
+ GET_MONTH(self),
+ GET_DAY(self));
+ seconds = DATE_GET_HOUR(self) * 3600 +
+ (DATE_GET_MINUTE(self) - offset) * 60 +
+ DATE_GET_SECOND(self);
+ temp = new_delta(days,
+ seconds,
+ DATE_GET_MICROSECOND(self),
+ 1);
+ }
+ if (temp != NULL) {
+ self->hashcode = PyObject_Hash(temp);
+ Py_DECREF(temp);
+ }
+ }
+ return self->hashcode;
}
static PyObject *
datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
- int y = GET_YEAR(self);
- int m = GET_MONTH(self);
- int d = GET_DAY(self);
- int hh = DATE_GET_HOUR(self);
- int mm = DATE_GET_MINUTE(self);
- int ss = DATE_GET_SECOND(self);
- int us = DATE_GET_MICROSECOND(self);
- PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
-
- if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
- datetime_kws,
- &y, &m, &d, &hh, &mm, &ss, &us,
- &tzinfo))
- return NULL;
- tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
- if (tuple == NULL)
- return NULL;
- clone = datetime_new(Py_TYPE(self), tuple, NULL);
- Py_DECREF(tuple);
- return clone;
+ PyObject *clone;
+ PyObject *tuple;
+ int y = GET_YEAR(self);
+ int m = GET_MONTH(self);
+ int d = GET_DAY(self);
+ int hh = DATE_GET_HOUR(self);
+ int mm = DATE_GET_MINUTE(self);
+ int ss = DATE_GET_SECOND(self);
+ int us = DATE_GET_MICROSECOND(self);
+ PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
+ datetime_kws,
+ &y, &m, &d, &hh, &mm, &ss, &us,
+ &tzinfo))
+ return NULL;
+ tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
+ if (tuple == NULL)
+ return NULL;
+ clone = datetime_new(Py_TYPE(self), tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
}
static PyObject *
datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
- int y, m, d, hh, mm, ss, us;
- PyObject *result;
- int offset, none;
+ int y, m, d, hh, mm, ss, us;
+ PyObject *result;
+ int offset, none;
- PyObject *tzinfo;
- static char *keywords[] = {"tz", NULL};
+ PyObject *tzinfo;
+ static char *keywords[] = {"tz", NULL};
- if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords,
- &PyDateTime_TZInfoType, &tzinfo))
- return NULL;
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "O!:astimezone", keywords,
+ &PyDateTime_TZInfoType, &tzinfo))
+ return NULL;
- if (!HASTZINFO(self) || self->tzinfo == Py_None)
- goto NeedAware;
+ if (!HASTZINFO(self) || self->tzinfo == Py_None)
+ goto NeedAware;
- /* Conversion to self's own time zone is a NOP. */
- if (self->tzinfo == tzinfo) {
- Py_INCREF(self);
- return (PyObject *)self;
- }
+ /* Conversion to self's own time zone is a NOP. */
+ if (self->tzinfo == tzinfo) {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
- /* Convert self to UTC. */
- offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
- if (offset == -1 && PyErr_Occurred())
- return NULL;
- if (none)
- goto NeedAware;
-
- y = GET_YEAR(self);
- m = GET_MONTH(self);
- d = GET_DAY(self);
- hh = DATE_GET_HOUR(self);
- mm = DATE_GET_MINUTE(self);
- ss = DATE_GET_SECOND(self);
- us = DATE_GET_MICROSECOND(self);
-
- mm -= offset;
- if ((mm < 0 || mm >= 60) &&
- normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
- return NULL;
-
- /* Attach new tzinfo and let fromutc() do the rest. */
- result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo);
- if (result != NULL) {
- PyObject *temp = result;
-
- result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp);
- Py_DECREF(temp);
- }
- return result;
+ /* Convert self to UTC. */
+ offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
+ if (offset == -1 && PyErr_Occurred())
+ return NULL;
+ if (none)
+ goto NeedAware;
+
+ y = GET_YEAR(self);
+ m = GET_MONTH(self);
+ d = GET_DAY(self);
+ hh = DATE_GET_HOUR(self);
+ mm = DATE_GET_MINUTE(self);
+ ss = DATE_GET_SECOND(self);
+ us = DATE_GET_MICROSECOND(self);
+
+ mm -= offset;
+ if ((mm < 0 || mm >= 60) &&
+ normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us) < 0)
+ return NULL;
+
+ /* Attach new tzinfo and let fromutc() do the rest. */
+ result = new_datetime(y, m, d, hh, mm, ss, us, tzinfo);
+ if (result != NULL) {
+ PyObject *temp = result;
+
+ result = PyObject_CallMethod(tzinfo, "fromutc", "O", temp);
+ Py_DECREF(temp);
+ }
+ return result;
NeedAware:
- PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to "
- "a naive datetime");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to "
+ "a naive datetime");
+ return NULL;
}
static PyObject *
datetime_timetuple(PyDateTime_DateTime *self)
{
- int dstflag = -1;
+ int dstflag = -1;
- if (HASTZINFO(self) && self->tzinfo != Py_None) {
- int none;
+ if (HASTZINFO(self) && self->tzinfo != Py_None) {
+ int none;
- dstflag = call_dst(self->tzinfo, (PyObject *)self, &none);
- if (dstflag == -1 && PyErr_Occurred())
- return NULL;
+ dstflag = call_dst(self->tzinfo, (PyObject *)self, &none);
+ if (dstflag == -1 && PyErr_Occurred())
+ return NULL;
- if (none)
- dstflag = -1;
- else if (dstflag != 0)
- dstflag = 1;
+ if (none)
+ dstflag = -1;
+ else if (dstflag != 0)
+ dstflag = 1;
- }
- return build_struct_time(GET_YEAR(self),
- GET_MONTH(self),
- GET_DAY(self),
- DATE_GET_HOUR(self),
- DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self),
- dstflag);
+ }
+ return build_struct_time(GET_YEAR(self),
+ GET_MONTH(self),
+ GET_DAY(self),
+ DATE_GET_HOUR(self),
+ DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self),
+ dstflag);
}
static PyObject *
datetime_getdate(PyDateTime_DateTime *self)
{
- return new_date(GET_YEAR(self),
- GET_MONTH(self),
- GET_DAY(self));
+ return new_date(GET_YEAR(self),
+ GET_MONTH(self),
+ GET_DAY(self));
}
static PyObject *
datetime_gettime(PyDateTime_DateTime *self)
{
- return new_time(DATE_GET_HOUR(self),
- DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self),
- DATE_GET_MICROSECOND(self),
- Py_None);
+ return new_time(DATE_GET_HOUR(self),
+ DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self),
+ DATE_GET_MICROSECOND(self),
+ Py_None);
}
static PyObject *
datetime_gettimetz(PyDateTime_DateTime *self)
{
- return new_time(DATE_GET_HOUR(self),
- DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self),
- DATE_GET_MICROSECOND(self),
- HASTZINFO(self) ? self->tzinfo : Py_None);
+ return new_time(DATE_GET_HOUR(self),
+ DATE_GET_MINUTE(self),
+ DATE_GET_SECOND(self),
+ DATE_GET_MICROSECOND(self),
+ HASTZINFO(self) ? self->tzinfo : Py_None);
}
static PyObject *
datetime_utctimetuple(PyDateTime_DateTime *self)
{
- int y = GET_YEAR(self);
- int m = GET_MONTH(self);
- int d = GET_DAY(self);
- int hh = DATE_GET_HOUR(self);
- int mm = DATE_GET_MINUTE(self);
- int ss = DATE_GET_SECOND(self);
- int us = 0; /* microseconds are ignored in a timetuple */
- int offset = 0;
-
- if (HASTZINFO(self) && self->tzinfo != Py_None) {
- int none;
-
- offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
- if (offset == -1 && PyErr_Occurred())
- return NULL;
- }
- /* Even if offset is 0, don't call timetuple() -- tm_isdst should be
- * 0 in a UTC timetuple regardless of what dst() says.
- */
- if (offset) {
- /* Subtract offset minutes & normalize. */
- int stat;
-
- mm -= offset;
- stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us);
- if (stat < 0) {
- /* At the edges, it's possible we overflowed
- * beyond MINYEAR or MAXYEAR.
- */
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_Clear();
- else
- return NULL;
- }
- }
- return build_struct_time(y, m, d, hh, mm, ss, 0);
+ int y = GET_YEAR(self);
+ int m = GET_MONTH(self);
+ int d = GET_DAY(self);
+ int hh = DATE_GET_HOUR(self);
+ int mm = DATE_GET_MINUTE(self);
+ int ss = DATE_GET_SECOND(self);
+ int us = 0; /* microseconds are ignored in a timetuple */
+ int offset = 0;
+
+ if (HASTZINFO(self) && self->tzinfo != Py_None) {
+ int none;
+
+ offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none);
+ if (offset == -1 && PyErr_Occurred())
+ return NULL;
+ }
+ /* Even if offset is 0, don't call timetuple() -- tm_isdst should be
+ * 0 in a UTC timetuple regardless of what dst() says.
+ */
+ if (offset) {
+ /* Subtract offset minutes & normalize. */
+ int stat;
+
+ mm -= offset;
+ stat = normalize_datetime(&y, &m, &d, &hh, &mm, &ss, &us);
+ if (stat < 0) {
+ /* At the edges, it's possible we overflowed
+ * beyond MINYEAR or MAXYEAR.
+ */
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ }
+ return build_struct_time(y, m, d, hh, mm, ss, 0);
}
/* Pickle support, a simple use of __reduce__. */
@@ -4480,102 +4480,102 @@ datetime_utctimetuple(PyDateTime_DateTime *self)
static PyObject *
datetime_getstate(PyDateTime_DateTime *self)
{
- PyObject *basestate;
- PyObject *result = NULL;
+ PyObject *basestate;
+ PyObject *result = NULL;
- basestate = PyBytes_FromStringAndSize((char *)self->data,
- _PyDateTime_DATETIME_DATASIZE);
- if (basestate != NULL) {
- if (! HASTZINFO(self) || self->tzinfo == Py_None)
- result = PyTuple_Pack(1, basestate);
- else
- result = PyTuple_Pack(2, basestate, self->tzinfo);
- Py_DECREF(basestate);
- }
- return result;
+ basestate = PyBytes_FromStringAndSize((char *)self->data,
+ _PyDateTime_DATETIME_DATASIZE);
+ if (basestate != NULL) {
+ if (! HASTZINFO(self) || self->tzinfo == Py_None)
+ result = PyTuple_Pack(1, basestate);
+ else
+ result = PyTuple_Pack(2, basestate, self->tzinfo);
+ Py_DECREF(basestate);
+ }
+ return result;
}
static PyObject *
datetime_reduce(PyDateTime_DateTime *self, PyObject *arg)
{
- return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self));
+ return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self));
}
static PyMethodDef datetime_methods[] = {
- /* Class methods: */
+ /* Class methods: */
- {"now", (PyCFunction)datetime_now,
- METH_VARARGS | METH_KEYWORDS | METH_CLASS,
- PyDoc_STR("[tz] -> new datetime with tz's local day and time.")},
+ {"now", (PyCFunction)datetime_now,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+ PyDoc_STR("[tz] -> new datetime with tz's local day and time.")},
- {"utcnow", (PyCFunction)datetime_utcnow,
- METH_NOARGS | METH_CLASS,
- PyDoc_STR("Return a new datetime representing UTC day and time.")},
+ {"utcnow", (PyCFunction)datetime_utcnow,
+ METH_NOARGS | METH_CLASS,
+ PyDoc_STR("Return a new datetime representing UTC day and time.")},
- {"fromtimestamp", (PyCFunction)datetime_fromtimestamp,
- METH_VARARGS | METH_KEYWORDS | METH_CLASS,
- PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},
+ {"fromtimestamp", (PyCFunction)datetime_fromtimestamp,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+ PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")},
- {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
- METH_VARARGS | METH_CLASS,
- PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
- "(like time.time()).")},
+ {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp,
+ METH_VARARGS | METH_CLASS,
+ PyDoc_STR("timestamp -> UTC datetime from a POSIX timestamp "
+ "(like time.time()).")},
- {"strptime", (PyCFunction)datetime_strptime,
- METH_VARARGS | METH_CLASS,
- PyDoc_STR("string, format -> new datetime parsed from a string "
- "(like time.strptime()).")},
+ {"strptime", (PyCFunction)datetime_strptime,
+ METH_VARARGS | METH_CLASS,
+ PyDoc_STR("string, format -> new datetime parsed from a string "
+ "(like time.strptime()).")},
- {"combine", (PyCFunction)datetime_combine,
- METH_VARARGS | METH_KEYWORDS | METH_CLASS,
- PyDoc_STR("date, time -> datetime with same date and time fields")},
+ {"combine", (PyCFunction)datetime_combine,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+ PyDoc_STR("date, time -> datetime with same date and time fields")},
- /* Instance methods: */
+ /* Instance methods: */
- {"date", (PyCFunction)datetime_getdate, METH_NOARGS,
- PyDoc_STR("Return date object with same year, month and day.")},
+ {"date", (PyCFunction)datetime_getdate, METH_NOARGS,
+ PyDoc_STR("Return date object with same year, month and day.")},
- {"time", (PyCFunction)datetime_gettime, METH_NOARGS,
- PyDoc_STR("Return time object with same time but with tzinfo=None.")},
+ {"time", (PyCFunction)datetime_gettime, METH_NOARGS,
+ PyDoc_STR("Return time object with same time but with tzinfo=None.")},
- {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS,
- PyDoc_STR("Return time object with same time and tzinfo.")},
+ {"timetz", (PyCFunction)datetime_gettimetz, METH_NOARGS,
+ PyDoc_STR("Return time object with same time and tzinfo.")},
- {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS,
- PyDoc_STR("Return ctime() style string.")},
+ {"ctime", (PyCFunction)datetime_ctime, METH_NOARGS,
+ PyDoc_STR("Return ctime() style string.")},
- {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS,
- PyDoc_STR("Return time tuple, compatible with time.localtime().")},
+ {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS,
+ PyDoc_STR("Return time tuple, compatible with time.localtime().")},
- {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS,
- PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")},
+ {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS,
+ PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")},
- {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("[sep] -> string in ISO 8601 format, "
- "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"
- "sep is used to separate the year from the time, and "
- "defaults to 'T'.")},
+ {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("[sep] -> string in ISO 8601 format, "
+ "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"
+ "sep is used to separate the year from the time, and "
+ "defaults to 'T'.")},
- {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
+ {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
- {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.tzname(self).")},
+ {"tzname", (PyCFunction)datetime_tzname, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.tzname(self).")},
- {"dst", (PyCFunction)datetime_dst, METH_NOARGS,
- PyDoc_STR("Return self.tzinfo.dst(self).")},
+ {"dst", (PyCFunction)datetime_dst, METH_NOARGS,
+ PyDoc_STR("Return self.tzinfo.dst(self).")},
- {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("Return datetime with new specified fields.")},
+ {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("Return datetime with new specified fields.")},
- {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS,
- PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
+ {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
- {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
- PyDoc_STR("__reduce__() -> (cls, state)")},
+ {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
+ PyDoc_STR("__reduce__() -> (cls, state)")},
- {NULL, NULL}
+ {NULL, NULL}
};
static char datetime_doc[] =
@@ -4585,58 +4585,58 @@ The year, month and day arguments are required. tzinfo may be None, or an\n\
instance of a tzinfo subclass. The remaining arguments may be ints or longs.\n");
static PyNumberMethods datetime_as_number = {
- datetime_add, /* nb_add */
- datetime_subtract, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- 0, /* nb_bool */
+ datetime_add, /* nb_add */
+ datetime_subtract, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, /* nb_bool */
};
static PyTypeObject PyDateTime_DateTimeType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "datetime.datetime", /* tp_name */
- sizeof(PyDateTime_DateTime), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)datetime_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)datetime_repr, /* tp_repr */
- &datetime_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)datetime_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)datetime_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- datetime_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- datetime_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- datetime_methods, /* tp_methods */
- 0, /* tp_members */
- datetime_getset, /* tp_getset */
- &PyDateTime_DateType, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- datetime_alloc, /* tp_alloc */
- datetime_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "datetime.datetime", /* tp_name */
+ sizeof(PyDateTime_DateTime), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)datetime_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)datetime_repr, /* tp_repr */
+ &datetime_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)datetime_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)datetime_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ datetime_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ datetime_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ datetime_methods, /* tp_methods */
+ 0, /* tp_members */
+ datetime_getset, /* tp_getset */
+ &PyDateTime_DateType, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ datetime_alloc, /* tp_alloc */
+ datetime_new, /* tp_new */
+ 0, /* tp_free */
};
/* ---------------------------------------------------------------------------
@@ -4644,204 +4644,204 @@ static PyTypeObject PyDateTime_DateTimeType = {
*/
static PyMethodDef module_methods[] = {
- {NULL, NULL}
+ {NULL, NULL}
};
/* C API. Clients get at this via PyDateTime_IMPORT, defined in
* datetime.h.
*/
static PyDateTime_CAPI CAPI = {
- &PyDateTime_DateType,
- &PyDateTime_DateTimeType,
- &PyDateTime_TimeType,
- &PyDateTime_DeltaType,
- &PyDateTime_TZInfoType,
- new_date_ex,
- new_datetime_ex,
- new_time_ex,
- new_delta_ex,
- datetime_fromtimestamp,
- date_fromtimestamp
+ &PyDateTime_DateType,
+ &PyDateTime_DateTimeType,
+ &PyDateTime_TimeType,
+ &PyDateTime_DeltaType,
+ &PyDateTime_TZInfoType,
+ new_date_ex,
+ new_datetime_ex,
+ new_time_ex,
+ new_delta_ex,
+ datetime_fromtimestamp,
+ date_fromtimestamp
};
static struct PyModuleDef datetimemodule = {
- PyModuleDef_HEAD_INIT,
- "datetime",
- "Fast implementation of the datetime type.",
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "datetime",
+ "Fast implementation of the datetime type.",
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_datetime(void)
{
- PyObject *m; /* a module object */
- PyObject *d; /* its dict */
- PyObject *x;
-
- m = PyModule_Create(&datetimemodule);
- if (m == NULL)
- return NULL;
-
- if (PyType_Ready(&PyDateTime_DateType) < 0)
- return NULL;
- if (PyType_Ready(&PyDateTime_DateTimeType) < 0)
- return NULL;
- if (PyType_Ready(&PyDateTime_DeltaType) < 0)
- return NULL;
- if (PyType_Ready(&PyDateTime_TimeType) < 0)
- return NULL;
- if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
- return NULL;
-
- /* timedelta values */
- d = PyDateTime_DeltaType.tp_dict;
-
- x = new_delta(0, 0, 1, 0);
- if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
- if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
- if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- /* date values */
- d = PyDateTime_DateType.tp_dict;
-
- x = new_date(1, 1, 1);
- if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_date(MAXYEAR, 12, 31);
- if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_delta(1, 0, 0, 0);
- if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- /* time values */
- d = PyDateTime_TimeType.tp_dict;
-
- x = new_time(0, 0, 0, 0, Py_None);
- if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_time(23, 59, 59, 999999, Py_None);
- if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_delta(0, 0, 1, 0);
- if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- /* datetime values */
- d = PyDateTime_DateTimeType.tp_dict;
-
- x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None);
- if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None);
- if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- x = new_delta(0, 0, 1, 0);
- if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
- return NULL;
- Py_DECREF(x);
-
- /* module initialization */
- PyModule_AddIntConstant(m, "MINYEAR", MINYEAR);
- PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR);
-
- Py_INCREF(&PyDateTime_DateType);
- PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType);
-
- Py_INCREF(&PyDateTime_DateTimeType);
- PyModule_AddObject(m, "datetime",
- (PyObject *)&PyDateTime_DateTimeType);
-
- Py_INCREF(&PyDateTime_TimeType);
- PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType);
-
- Py_INCREF(&PyDateTime_DeltaType);
- PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType);
-
- Py_INCREF(&PyDateTime_TZInfoType);
- PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType);
+ PyObject *m; /* a module object */
+ PyObject *d; /* its dict */
+ PyObject *x;
+
+ m = PyModule_Create(&datetimemodule);
+ if (m == NULL)
+ return NULL;
+
+ if (PyType_Ready(&PyDateTime_DateType) < 0)
+ return NULL;
+ if (PyType_Ready(&PyDateTime_DateTimeType) < 0)
+ return NULL;
+ if (PyType_Ready(&PyDateTime_DeltaType) < 0)
+ return NULL;
+ if (PyType_Ready(&PyDateTime_TimeType) < 0)
+ return NULL;
+ if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
+ return NULL;
+
+ /* timedelta values */
+ d = PyDateTime_DeltaType.tp_dict;
+
+ x = new_delta(0, 0, 1, 0);
+ if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
+ if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
+ if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ /* date values */
+ d = PyDateTime_DateType.tp_dict;
+
+ x = new_date(1, 1, 1);
+ if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_date(MAXYEAR, 12, 31);
+ if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_delta(1, 0, 0, 0);
+ if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ /* time values */
+ d = PyDateTime_TimeType.tp_dict;
+
+ x = new_time(0, 0, 0, 0, Py_None);
+ if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_time(23, 59, 59, 999999, Py_None);
+ if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_delta(0, 0, 1, 0);
+ if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ /* datetime values */
+ d = PyDateTime_DateTimeType.tp_dict;
+
+ x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None);
+ if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None);
+ if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ x = new_delta(0, 0, 1, 0);
+ if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
+ return NULL;
+ Py_DECREF(x);
+
+ /* module initialization */
+ PyModule_AddIntConstant(m, "MINYEAR", MINYEAR);
+ PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR);
+
+ Py_INCREF(&PyDateTime_DateType);
+ PyModule_AddObject(m, "date", (PyObject *) &PyDateTime_DateType);
+
+ Py_INCREF(&PyDateTime_DateTimeType);
+ PyModule_AddObject(m, "datetime",
+ (PyObject *)&PyDateTime_DateTimeType);
+
+ Py_INCREF(&PyDateTime_TimeType);
+ PyModule_AddObject(m, "time", (PyObject *) &PyDateTime_TimeType);
+
+ Py_INCREF(&PyDateTime_DeltaType);
+ PyModule_AddObject(m, "timedelta", (PyObject *) &PyDateTime_DeltaType);
+
+ Py_INCREF(&PyDateTime_TZInfoType);
+ PyModule_AddObject(m, "tzinfo", (PyObject *) &PyDateTime_TZInfoType);
x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL);
if (x == NULL)
- return NULL;
+ return NULL;
PyModule_AddObject(m, "datetime_CAPI", x);
- /* A 4-year cycle has an extra leap day over what we'd get from
- * pasting together 4 single years.
- */
- assert(DI4Y == 4 * 365 + 1);
- assert(DI4Y == days_before_year(4+1));
-
- /* Similarly, a 400-year cycle has an extra leap day over what we'd
- * get from pasting together 4 100-year cycles.
- */
- assert(DI400Y == 4 * DI100Y + 1);
- assert(DI400Y == days_before_year(400+1));
-
- /* OTOH, a 100-year cycle has one fewer leap day than we'd get from
- * pasting together 25 4-year cycles.
- */
- assert(DI100Y == 25 * DI4Y - 1);
- assert(DI100Y == days_before_year(100+1));
-
- us_per_us = PyLong_FromLong(1);
- us_per_ms = PyLong_FromLong(1000);
- us_per_second = PyLong_FromLong(1000000);
- us_per_minute = PyLong_FromLong(60000000);
- seconds_per_day = PyLong_FromLong(24 * 3600);
- if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL ||
- us_per_minute == NULL || seconds_per_day == NULL)
- return NULL;
-
- /* The rest are too big for 32-bit ints, but even
- * us_per_week fits in 40 bits, so doubles should be exact.
- */
- us_per_hour = PyLong_FromDouble(3600000000.0);
- us_per_day = PyLong_FromDouble(86400000000.0);
- us_per_week = PyLong_FromDouble(604800000000.0);
- if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
- return NULL;
- return m;
+ /* A 4-year cycle has an extra leap day over what we'd get from
+ * pasting together 4 single years.
+ */
+ assert(DI4Y == 4 * 365 + 1);
+ assert(DI4Y == days_before_year(4+1));
+
+ /* Similarly, a 400-year cycle has an extra leap day over what we'd
+ * get from pasting together 4 100-year cycles.
+ */
+ assert(DI400Y == 4 * DI100Y + 1);
+ assert(DI400Y == days_before_year(400+1));
+
+ /* OTOH, a 100-year cycle has one fewer leap day than we'd get from
+ * pasting together 25 4-year cycles.
+ */
+ assert(DI100Y == 25 * DI4Y - 1);
+ assert(DI100Y == days_before_year(100+1));
+
+ us_per_us = PyLong_FromLong(1);
+ us_per_ms = PyLong_FromLong(1000);
+ us_per_second = PyLong_FromLong(1000000);
+ us_per_minute = PyLong_FromLong(60000000);
+ seconds_per_day = PyLong_FromLong(24 * 3600);
+ if (us_per_us == NULL || us_per_ms == NULL || us_per_second == NULL ||
+ us_per_minute == NULL || seconds_per_day == NULL)
+ return NULL;
+
+ /* The rest are too big for 32-bit ints, but even
+ * us_per_week fits in 40 bits, so doubles should be exact.
+ */
+ us_per_hour = PyLong_FromDouble(3600000000.0);
+ us_per_day = PyLong_FromDouble(86400000000.0);
+ us_per_week = PyLong_FromDouble(604800000000.0);
+ if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
+ return NULL;
+ return m;
}
/* ---------------------------------------------------------------------------
Some time zone algebra. For a datetime x, let
x.n = x stripped of its timezone -- its naive time.
x.o = x.utcoffset(), and assuming that doesn't raise an exception or
- return None
+ return None
x.d = x.dst(), and assuming that doesn't raise an exception or
- return None
+ return None
x.s = x's standard offset, x.o - x.d
Now some derived rules, where k is a duration (timedelta).
@@ -4963,13 +4963,13 @@ the justifications for the kinds of substitutions we've done several times
already):
diff' = x.n - (z'.n - z'.o) = replacing z'.n via [7]
- x.n - (z.n + diff - z'.o) = replacing diff via [6]
- x.n - (z.n + x.n - (z.n - z.o) - z'.o) =
- x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n
- - z.n + z.n - z.o + z'.o = cancel z.n
- - z.o + z'.o = #1 twice
- -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo
- z'.d - z.d
+ x.n - (z.n + diff - z'.o) = replacing diff via [6]
+ x.n - (z.n + x.n - (z.n - z.o) - z'.o) =
+ x.n - z.n - x.n + z.n - z.o + z'.o = cancel x.n
+ - z.n + z.n - z.o + z'.o = cancel z.n
+ - z.o + z'.o = #1 twice
+ -z.s - z.d + z'.s + z'.d = z and z' have same tzinfo
+ z'.d - z.d
So z' is UTC-equivalent to x iff z'.d = z.d at this point. If they are equal,
we've found the UTC-equivalent so are done. In fact, we stop with [7] and
diff --git a/Modules/errnomodule.c b/Modules/errnomodule.c
index d5fe460..03c65f7 100644
--- a/Modules/errnomodule.c
+++ b/Modules/errnomodule.c
@@ -11,10 +11,10 @@
/*
* Pull in the system error definitions
- */
+ */
static PyMethodDef errno_methods[] = {
- {NULL, NULL}
+ {NULL, NULL}
};
/* Helper function doing the dictionary inserting */
@@ -22,21 +22,21 @@ static PyMethodDef errno_methods[] = {
static void
_inscode(PyObject *d, PyObject *de, const char *name, int code)
{
- PyObject *u = PyUnicode_FromString(name);
- PyObject *v = PyLong_FromLong((long) code);
+ PyObject *u = PyUnicode_FromString(name);
+ PyObject *v = PyLong_FromLong((long) code);
- /* Don't bother checking for errors; they'll be caught at the end
- * of the module initialization function by the caller of
- * initerrno().
- */
- if (u && v) {
- /* insert in modules dict */
- PyDict_SetItem(d, u, v);
- /* insert in errorcode dict */
- PyDict_SetItem(de, v, u);
- }
- Py_XDECREF(u);
- Py_XDECREF(v);
+ /* Don't bother checking for errors; they'll be caught at the end
+ * of the module initialization function by the caller of
+ * initerrno().
+ */
+ if (u && v) {
+ /* insert in modules dict */
+ PyDict_SetItem(d, u, v);
+ /* insert in errorcode dict */
+ PyDict_SetItem(de, v, u);
+ }
+ Py_XDECREF(u);
+ Py_XDECREF(v);
}
PyDoc_STRVAR(errno__doc__,
@@ -54,749 +54,749 @@ To map error codes to error messages, use the function os.strerror(),\n\
e.g. os.strerror(2) could return 'No such file or directory'.");
static struct PyModuleDef errnomodule = {
- PyModuleDef_HEAD_INIT,
- "errno",
- errno__doc__,
- -1,
- errno_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "errno",
+ errno__doc__,
+ -1,
+ errno_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_errno(void)
{
- PyObject *m, *d, *de;
- m = PyModule_Create(&errnomodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
- de = PyDict_New();
- if (!d || !de || PyDict_SetItemString(d, "errorcode", de) < 0)
- return NULL;
+ PyObject *m, *d, *de;
+ m = PyModule_Create(&errnomodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+ de = PyDict_New();
+ if (!d || !de || PyDict_SetItemString(d, "errorcode", de) < 0)
+ return NULL;
/* Macro so I don't have to edit each and every line below... */
#define inscode(d, ds, de, name, code, comment) _inscode(d, de, name, code)
- /*
- * The names and comments are borrowed from linux/include/errno.h,
- * which should be pretty all-inclusive
- */
+ /*
+ * The names and comments are borrowed from linux/include/errno.h,
+ * which should be pretty all-inclusive
+ */
#ifdef ENODEV
- inscode(d, ds, de, "ENODEV", ENODEV, "No such device");
+ inscode(d, ds, de, "ENODEV", ENODEV, "No such device");
#endif
#ifdef ENOCSI
- inscode(d, ds, de, "ENOCSI", ENOCSI, "No CSI structure available");
+ inscode(d, ds, de, "ENOCSI", ENOCSI, "No CSI structure available");
#endif
#ifdef EHOSTUNREACH
- inscode(d, ds, de, "EHOSTUNREACH", EHOSTUNREACH, "No route to host");
+ inscode(d, ds, de, "EHOSTUNREACH", EHOSTUNREACH, "No route to host");
#else
#ifdef WSAEHOSTUNREACH
- inscode(d, ds, de, "EHOSTUNREACH", WSAEHOSTUNREACH, "No route to host");
+ inscode(d, ds, de, "EHOSTUNREACH", WSAEHOSTUNREACH, "No route to host");
#endif
#endif
#ifdef ENOMSG
- inscode(d, ds, de, "ENOMSG", ENOMSG, "No message of desired type");
+ inscode(d, ds, de, "ENOMSG", ENOMSG, "No message of desired type");
#endif
#ifdef EUCLEAN
- inscode(d, ds, de, "EUCLEAN", EUCLEAN, "Structure needs cleaning");
+ inscode(d, ds, de, "EUCLEAN", EUCLEAN, "Structure needs cleaning");
#endif
#ifdef EL2NSYNC
- inscode(d, ds, de, "EL2NSYNC", EL2NSYNC, "Level 2 not synchronized");
+ inscode(d, ds, de, "EL2NSYNC", EL2NSYNC, "Level 2 not synchronized");
#endif
#ifdef EL2HLT
- inscode(d, ds, de, "EL2HLT", EL2HLT, "Level 2 halted");
+ inscode(d, ds, de, "EL2HLT", EL2HLT, "Level 2 halted");
#endif
#ifdef ENODATA
- inscode(d, ds, de, "ENODATA", ENODATA, "No data available");
+ inscode(d, ds, de, "ENODATA", ENODATA, "No data available");
#endif
#ifdef ENOTBLK
- inscode(d, ds, de, "ENOTBLK", ENOTBLK, "Block device required");
+ inscode(d, ds, de, "ENOTBLK", ENOTBLK, "Block device required");
#endif
#ifdef ENOSYS
- inscode(d, ds, de, "ENOSYS", ENOSYS, "Function not implemented");
+ inscode(d, ds, de, "ENOSYS", ENOSYS, "Function not implemented");
#endif
#ifdef EPIPE
- inscode(d, ds, de, "EPIPE", EPIPE, "Broken pipe");
+ inscode(d, ds, de, "EPIPE", EPIPE, "Broken pipe");
#endif
#ifdef EINVAL
- inscode(d, ds, de, "EINVAL", EINVAL, "Invalid argument");
+ inscode(d, ds, de, "EINVAL", EINVAL, "Invalid argument");
#else
#ifdef WSAEINVAL
- inscode(d, ds, de, "EINVAL", WSAEINVAL, "Invalid argument");
+ inscode(d, ds, de, "EINVAL", WSAEINVAL, "Invalid argument");
#endif
#endif
#ifdef EOVERFLOW
- inscode(d, ds, de, "EOVERFLOW", EOVERFLOW, "Value too large for defined data type");
+ inscode(d, ds, de, "EOVERFLOW", EOVERFLOW, "Value too large for defined data type");
#endif
#ifdef EADV
- inscode(d, ds, de, "EADV", EADV, "Advertise error");
+ inscode(d, ds, de, "EADV", EADV, "Advertise error");
#endif
#ifdef EINTR
- inscode(d, ds, de, "EINTR", EINTR, "Interrupted system call");
+ inscode(d, ds, de, "EINTR", EINTR, "Interrupted system call");
#else
#ifdef WSAEINTR
- inscode(d, ds, de, "EINTR", WSAEINTR, "Interrupted system call");
+ inscode(d, ds, de, "EINTR", WSAEINTR, "Interrupted system call");
#endif
#endif
#ifdef EUSERS
- inscode(d, ds, de, "EUSERS", EUSERS, "Too many users");
+ inscode(d, ds, de, "EUSERS", EUSERS, "Too many users");
#else
#ifdef WSAEUSERS
- inscode(d, ds, de, "EUSERS", WSAEUSERS, "Too many users");
+ inscode(d, ds, de, "EUSERS", WSAEUSERS, "Too many users");
#endif
#endif
#ifdef ENOTEMPTY
- inscode(d, ds, de, "ENOTEMPTY", ENOTEMPTY, "Directory not empty");
+ inscode(d, ds, de, "ENOTEMPTY", ENOTEMPTY, "Directory not empty");
#else
#ifdef WSAENOTEMPTY
- inscode(d, ds, de, "ENOTEMPTY", WSAENOTEMPTY, "Directory not empty");
+ inscode(d, ds, de, "ENOTEMPTY", WSAENOTEMPTY, "Directory not empty");
#endif
#endif
#ifdef ENOBUFS
- inscode(d, ds, de, "ENOBUFS", ENOBUFS, "No buffer space available");
+ inscode(d, ds, de, "ENOBUFS", ENOBUFS, "No buffer space available");
#else
#ifdef WSAENOBUFS
- inscode(d, ds, de, "ENOBUFS", WSAENOBUFS, "No buffer space available");
+ inscode(d, ds, de, "ENOBUFS", WSAENOBUFS, "No buffer space available");
#endif
#endif
#ifdef EPROTO
- inscode(d, ds, de, "EPROTO", EPROTO, "Protocol error");
+ inscode(d, ds, de, "EPROTO", EPROTO, "Protocol error");
#endif
#ifdef EREMOTE
- inscode(d, ds, de, "EREMOTE", EREMOTE, "Object is remote");
+ inscode(d, ds, de, "EREMOTE", EREMOTE, "Object is remote");
#else
#ifdef WSAEREMOTE
- inscode(d, ds, de, "EREMOTE", WSAEREMOTE, "Object is remote");
+ inscode(d, ds, de, "EREMOTE", WSAEREMOTE, "Object is remote");
#endif
#endif
#ifdef ENAVAIL
- inscode(d, ds, de, "ENAVAIL", ENAVAIL, "No XENIX semaphores available");
+ inscode(d, ds, de, "ENAVAIL", ENAVAIL, "No XENIX semaphores available");
#endif
#ifdef ECHILD
- inscode(d, ds, de, "ECHILD", ECHILD, "No child processes");
+ inscode(d, ds, de, "ECHILD", ECHILD, "No child processes");
#endif
#ifdef ELOOP
- inscode(d, ds, de, "ELOOP", ELOOP, "Too many symbolic links encountered");
+ inscode(d, ds, de, "ELOOP", ELOOP, "Too many symbolic links encountered");
#else
#ifdef WSAELOOP
- inscode(d, ds, de, "ELOOP", WSAELOOP, "Too many symbolic links encountered");
+ inscode(d, ds, de, "ELOOP", WSAELOOP, "Too many symbolic links encountered");
#endif
#endif
#ifdef EXDEV
- inscode(d, ds, de, "EXDEV", EXDEV, "Cross-device link");
+ inscode(d, ds, de, "EXDEV", EXDEV, "Cross-device link");
#endif
#ifdef E2BIG
- inscode(d, ds, de, "E2BIG", E2BIG, "Arg list too long");
+ inscode(d, ds, de, "E2BIG", E2BIG, "Arg list too long");
#endif
#ifdef ESRCH
- inscode(d, ds, de, "ESRCH", ESRCH, "No such process");
+ inscode(d, ds, de, "ESRCH", ESRCH, "No such process");
#endif
#ifdef EMSGSIZE
- inscode(d, ds, de, "EMSGSIZE", EMSGSIZE, "Message too long");
+ inscode(d, ds, de, "EMSGSIZE", EMSGSIZE, "Message too long");
#else
#ifdef WSAEMSGSIZE
- inscode(d, ds, de, "EMSGSIZE", WSAEMSGSIZE, "Message too long");
+ inscode(d, ds, de, "EMSGSIZE", WSAEMSGSIZE, "Message too long");
#endif
#endif
#ifdef EAFNOSUPPORT
- inscode(d, ds, de, "EAFNOSUPPORT", EAFNOSUPPORT, "Address family not supported by protocol");
+ inscode(d, ds, de, "EAFNOSUPPORT", EAFNOSUPPORT, "Address family not supported by protocol");
#else
#ifdef WSAEAFNOSUPPORT
- inscode(d, ds, de, "EAFNOSUPPORT", WSAEAFNOSUPPORT, "Address family not supported by protocol");
+ inscode(d, ds, de, "EAFNOSUPPORT", WSAEAFNOSUPPORT, "Address family not supported by protocol");
#endif
#endif
#ifdef EBADR
- inscode(d, ds, de, "EBADR", EBADR, "Invalid request descriptor");
+ inscode(d, ds, de, "EBADR", EBADR, "Invalid request descriptor");
#endif
#ifdef EHOSTDOWN
- inscode(d, ds, de, "EHOSTDOWN", EHOSTDOWN, "Host is down");
+ inscode(d, ds, de, "EHOSTDOWN", EHOSTDOWN, "Host is down");
#else
#ifdef WSAEHOSTDOWN
- inscode(d, ds, de, "EHOSTDOWN", WSAEHOSTDOWN, "Host is down");
+ inscode(d, ds, de, "EHOSTDOWN", WSAEHOSTDOWN, "Host is down");
#endif
#endif
#ifdef EPFNOSUPPORT
- inscode(d, ds, de, "EPFNOSUPPORT", EPFNOSUPPORT, "Protocol family not supported");
+ inscode(d, ds, de, "EPFNOSUPPORT", EPFNOSUPPORT, "Protocol family not supported");
#else
#ifdef WSAEPFNOSUPPORT
- inscode(d, ds, de, "EPFNOSUPPORT", WSAEPFNOSUPPORT, "Protocol family not supported");
+ inscode(d, ds, de, "EPFNOSUPPORT", WSAEPFNOSUPPORT, "Protocol family not supported");
#endif
#endif
#ifdef ENOPROTOOPT
- inscode(d, ds, de, "ENOPROTOOPT", ENOPROTOOPT, "Protocol not available");
+ inscode(d, ds, de, "ENOPROTOOPT", ENOPROTOOPT, "Protocol not available");
#else
#ifdef WSAENOPROTOOPT
- inscode(d, ds, de, "ENOPROTOOPT", WSAENOPROTOOPT, "Protocol not available");
+ inscode(d, ds, de, "ENOPROTOOPT", WSAENOPROTOOPT, "Protocol not available");
#endif
#endif
#ifdef EBUSY
- inscode(d, ds, de, "EBUSY", EBUSY, "Device or resource busy");
+ inscode(d, ds, de, "EBUSY", EBUSY, "Device or resource busy");
#endif
#ifdef EWOULDBLOCK
- inscode(d, ds, de, "EWOULDBLOCK", EWOULDBLOCK, "Operation would block");
+ inscode(d, ds, de, "EWOULDBLOCK", EWOULDBLOCK, "Operation would block");
#else
#ifdef WSAEWOULDBLOCK
- inscode(d, ds, de, "EWOULDBLOCK", WSAEWOULDBLOCK, "Operation would block");
+ inscode(d, ds, de, "EWOULDBLOCK", WSAEWOULDBLOCK, "Operation would block");
#endif
#endif
#ifdef EBADFD
- inscode(d, ds, de, "EBADFD", EBADFD, "File descriptor in bad state");
+ inscode(d, ds, de, "EBADFD", EBADFD, "File descriptor in bad state");
#endif
#ifdef EDOTDOT
- inscode(d, ds, de, "EDOTDOT", EDOTDOT, "RFS specific error");
+ inscode(d, ds, de, "EDOTDOT", EDOTDOT, "RFS specific error");
#endif
#ifdef EISCONN
- inscode(d, ds, de, "EISCONN", EISCONN, "Transport endpoint is already connected");
+ inscode(d, ds, de, "EISCONN", EISCONN, "Transport endpoint is already connected");
#else
#ifdef WSAEISCONN
- inscode(d, ds, de, "EISCONN", WSAEISCONN, "Transport endpoint is already connected");
+ inscode(d, ds, de, "EISCONN", WSAEISCONN, "Transport endpoint is already connected");
#endif
#endif
#ifdef ENOANO
- inscode(d, ds, de, "ENOANO", ENOANO, "No anode");
+ inscode(d, ds, de, "ENOANO", ENOANO, "No anode");
#endif
#ifdef ESHUTDOWN
- inscode(d, ds, de, "ESHUTDOWN", ESHUTDOWN, "Cannot send after transport endpoint shutdown");
+ inscode(d, ds, de, "ESHUTDOWN", ESHUTDOWN, "Cannot send after transport endpoint shutdown");
#else
#ifdef WSAESHUTDOWN
- inscode(d, ds, de, "ESHUTDOWN", WSAESHUTDOWN, "Cannot send after transport endpoint shutdown");
+ inscode(d, ds, de, "ESHUTDOWN", WSAESHUTDOWN, "Cannot send after transport endpoint shutdown");
#endif
#endif
#ifdef ECHRNG
- inscode(d, ds, de, "ECHRNG", ECHRNG, "Channel number out of range");
+ inscode(d, ds, de, "ECHRNG", ECHRNG, "Channel number out of range");
#endif
#ifdef ELIBBAD
- inscode(d, ds, de, "ELIBBAD", ELIBBAD, "Accessing a corrupted shared library");
+ inscode(d, ds, de, "ELIBBAD", ELIBBAD, "Accessing a corrupted shared library");
#endif
#ifdef ENONET
- inscode(d, ds, de, "ENONET", ENONET, "Machine is not on the network");
+ inscode(d, ds, de, "ENONET", ENONET, "Machine is not on the network");
#endif
#ifdef EBADE
- inscode(d, ds, de, "EBADE", EBADE, "Invalid exchange");
+ inscode(d, ds, de, "EBADE", EBADE, "Invalid exchange");
#endif
#ifdef EBADF
- inscode(d, ds, de, "EBADF", EBADF, "Bad file number");
+ inscode(d, ds, de, "EBADF", EBADF, "Bad file number");
#else
#ifdef WSAEBADF
- inscode(d, ds, de, "EBADF", WSAEBADF, "Bad file number");
+ inscode(d, ds, de, "EBADF", WSAEBADF, "Bad file number");
#endif
#endif
#ifdef EMULTIHOP
- inscode(d, ds, de, "EMULTIHOP", EMULTIHOP, "Multihop attempted");
+ inscode(d, ds, de, "EMULTIHOP", EMULTIHOP, "Multihop attempted");
#endif
#ifdef EIO
- inscode(d, ds, de, "EIO", EIO, "I/O error");
+ inscode(d, ds, de, "EIO", EIO, "I/O error");
#endif
#ifdef EUNATCH
- inscode(d, ds, de, "EUNATCH", EUNATCH, "Protocol driver not attached");
+ inscode(d, ds, de, "EUNATCH", EUNATCH, "Protocol driver not attached");
#endif
#ifdef EPROTOTYPE
- inscode(d, ds, de, "EPROTOTYPE", EPROTOTYPE, "Protocol wrong type for socket");
+ inscode(d, ds, de, "EPROTOTYPE", EPROTOTYPE, "Protocol wrong type for socket");
#else
#ifdef WSAEPROTOTYPE
- inscode(d, ds, de, "EPROTOTYPE", WSAEPROTOTYPE, "Protocol wrong type for socket");
+ inscode(d, ds, de, "EPROTOTYPE", WSAEPROTOTYPE, "Protocol wrong type for socket");
#endif
#endif
#ifdef ENOSPC
- inscode(d, ds, de, "ENOSPC", ENOSPC, "No space left on device");
+ inscode(d, ds, de, "ENOSPC", ENOSPC, "No space left on device");
#endif
#ifdef ENOEXEC
- inscode(d, ds, de, "ENOEXEC", ENOEXEC, "Exec format error");
+ inscode(d, ds, de, "ENOEXEC", ENOEXEC, "Exec format error");
#endif
#ifdef EALREADY
- inscode(d, ds, de, "EALREADY", EALREADY, "Operation already in progress");
+ inscode(d, ds, de, "EALREADY", EALREADY, "Operation already in progress");
#else
#ifdef WSAEALREADY
- inscode(d, ds, de, "EALREADY", WSAEALREADY, "Operation already in progress");
+ inscode(d, ds, de, "EALREADY", WSAEALREADY, "Operation already in progress");
#endif
#endif
#ifdef ENETDOWN
- inscode(d, ds, de, "ENETDOWN", ENETDOWN, "Network is down");
+ inscode(d, ds, de, "ENETDOWN", ENETDOWN, "Network is down");
#else
#ifdef WSAENETDOWN
- inscode(d, ds, de, "ENETDOWN", WSAENETDOWN, "Network is down");
+ inscode(d, ds, de, "ENETDOWN", WSAENETDOWN, "Network is down");
#endif
#endif
#ifdef ENOTNAM
- inscode(d, ds, de, "ENOTNAM", ENOTNAM, "Not a XENIX named type file");
+ inscode(d, ds, de, "ENOTNAM", ENOTNAM, "Not a XENIX named type file");
#endif
#ifdef EACCES
- inscode(d, ds, de, "EACCES", EACCES, "Permission denied");
+ inscode(d, ds, de, "EACCES", EACCES, "Permission denied");
#else
#ifdef WSAEACCES
- inscode(d, ds, de, "EACCES", WSAEACCES, "Permission denied");
+ inscode(d, ds, de, "EACCES", WSAEACCES, "Permission denied");
#endif
#endif
#ifdef ELNRNG
- inscode(d, ds, de, "ELNRNG", ELNRNG, "Link number out of range");
+ inscode(d, ds, de, "ELNRNG", ELNRNG, "Link number out of range");
#endif
#ifdef EILSEQ
- inscode(d, ds, de, "EILSEQ", EILSEQ, "Illegal byte sequence");
+ inscode(d, ds, de, "EILSEQ", EILSEQ, "Illegal byte sequence");
#endif
#ifdef ENOTDIR
- inscode(d, ds, de, "ENOTDIR", ENOTDIR, "Not a directory");
+ inscode(d, ds, de, "ENOTDIR", ENOTDIR, "Not a directory");
#endif
#ifdef ENOTUNIQ
- inscode(d, ds, de, "ENOTUNIQ", ENOTUNIQ, "Name not unique on network");
+ inscode(d, ds, de, "ENOTUNIQ", ENOTUNIQ, "Name not unique on network");
#endif
#ifdef EPERM
- inscode(d, ds, de, "EPERM", EPERM, "Operation not permitted");
+ inscode(d, ds, de, "EPERM", EPERM, "Operation not permitted");
#endif
#ifdef EDOM
- inscode(d, ds, de, "EDOM", EDOM, "Math argument out of domain of func");
+ inscode(d, ds, de, "EDOM", EDOM, "Math argument out of domain of func");
#endif
#ifdef EXFULL
- inscode(d, ds, de, "EXFULL", EXFULL, "Exchange full");
+ inscode(d, ds, de, "EXFULL", EXFULL, "Exchange full");
#endif
#ifdef ECONNREFUSED
- inscode(d, ds, de, "ECONNREFUSED", ECONNREFUSED, "Connection refused");
+ inscode(d, ds, de, "ECONNREFUSED", ECONNREFUSED, "Connection refused");
#else
#ifdef WSAECONNREFUSED
- inscode(d, ds, de, "ECONNREFUSED", WSAECONNREFUSED, "Connection refused");
+ inscode(d, ds, de, "ECONNREFUSED", WSAECONNREFUSED, "Connection refused");
#endif
#endif
#ifdef EISDIR
- inscode(d, ds, de, "EISDIR", EISDIR, "Is a directory");
+ inscode(d, ds, de, "EISDIR", EISDIR, "Is a directory");
#endif
#ifdef EPROTONOSUPPORT
- inscode(d, ds, de, "EPROTONOSUPPORT", EPROTONOSUPPORT, "Protocol not supported");
+ inscode(d, ds, de, "EPROTONOSUPPORT", EPROTONOSUPPORT, "Protocol not supported");
#else
#ifdef WSAEPROTONOSUPPORT
- inscode(d, ds, de, "EPROTONOSUPPORT", WSAEPROTONOSUPPORT, "Protocol not supported");
+ inscode(d, ds, de, "EPROTONOSUPPORT", WSAEPROTONOSUPPORT, "Protocol not supported");
#endif
#endif
#ifdef EROFS
- inscode(d, ds, de, "EROFS", EROFS, "Read-only file system");
+ inscode(d, ds, de, "EROFS", EROFS, "Read-only file system");
#endif
#ifdef EADDRNOTAVAIL
- inscode(d, ds, de, "EADDRNOTAVAIL", EADDRNOTAVAIL, "Cannot assign requested address");
+ inscode(d, ds, de, "EADDRNOTAVAIL", EADDRNOTAVAIL, "Cannot assign requested address");
#else
#ifdef WSAEADDRNOTAVAIL
- inscode(d, ds, de, "EADDRNOTAVAIL", WSAEADDRNOTAVAIL, "Cannot assign requested address");
+ inscode(d, ds, de, "EADDRNOTAVAIL", WSAEADDRNOTAVAIL, "Cannot assign requested address");
#endif
#endif
#ifdef EIDRM
- inscode(d, ds, de, "EIDRM", EIDRM, "Identifier removed");
+ inscode(d, ds, de, "EIDRM", EIDRM, "Identifier removed");
#endif
#ifdef ECOMM
- inscode(d, ds, de, "ECOMM", ECOMM, "Communication error on send");
+ inscode(d, ds, de, "ECOMM", ECOMM, "Communication error on send");
#endif
#ifdef ESRMNT
- inscode(d, ds, de, "ESRMNT", ESRMNT, "Srmount error");
+ inscode(d, ds, de, "ESRMNT", ESRMNT, "Srmount error");
#endif
#ifdef EREMOTEIO
- inscode(d, ds, de, "EREMOTEIO", EREMOTEIO, "Remote I/O error");
+ inscode(d, ds, de, "EREMOTEIO", EREMOTEIO, "Remote I/O error");
#endif
#ifdef EL3RST
- inscode(d, ds, de, "EL3RST", EL3RST, "Level 3 reset");
+ inscode(d, ds, de, "EL3RST", EL3RST, "Level 3 reset");
#endif
#ifdef EBADMSG
- inscode(d, ds, de, "EBADMSG", EBADMSG, "Not a data message");
+ inscode(d, ds, de, "EBADMSG", EBADMSG, "Not a data message");
#endif
#ifdef ENFILE
- inscode(d, ds, de, "ENFILE", ENFILE, "File table overflow");
+ inscode(d, ds, de, "ENFILE", ENFILE, "File table overflow");
#endif
#ifdef ELIBMAX
- inscode(d, ds, de, "ELIBMAX", ELIBMAX, "Attempting to link in too many shared libraries");
+ inscode(d, ds, de, "ELIBMAX", ELIBMAX, "Attempting to link in too many shared libraries");
#endif
#ifdef ESPIPE
- inscode(d, ds, de, "ESPIPE", ESPIPE, "Illegal seek");
+ inscode(d, ds, de, "ESPIPE", ESPIPE, "Illegal seek");
#endif
#ifdef ENOLINK
- inscode(d, ds, de, "ENOLINK", ENOLINK, "Link has been severed");
+ inscode(d, ds, de, "ENOLINK", ENOLINK, "Link has been severed");
#endif
#ifdef ENETRESET
- inscode(d, ds, de, "ENETRESET", ENETRESET, "Network dropped connection because of reset");
+ inscode(d, ds, de, "ENETRESET", ENETRESET, "Network dropped connection because of reset");
#else
#ifdef WSAENETRESET
- inscode(d, ds, de, "ENETRESET", WSAENETRESET, "Network dropped connection because of reset");
+ inscode(d, ds, de, "ENETRESET", WSAENETRESET, "Network dropped connection because of reset");
#endif
#endif
#ifdef ETIMEDOUT
- inscode(d, ds, de, "ETIMEDOUT", ETIMEDOUT, "Connection timed out");
+ inscode(d, ds, de, "ETIMEDOUT", ETIMEDOUT, "Connection timed out");
#else
#ifdef WSAETIMEDOUT
- inscode(d, ds, de, "ETIMEDOUT", WSAETIMEDOUT, "Connection timed out");
+ inscode(d, ds, de, "ETIMEDOUT", WSAETIMEDOUT, "Connection timed out");
#endif
#endif
#ifdef ENOENT
- inscode(d, ds, de, "ENOENT", ENOENT, "No such file or directory");
+ inscode(d, ds, de, "ENOENT", ENOENT, "No such file or directory");
#endif
#ifdef EEXIST
- inscode(d, ds, de, "EEXIST", EEXIST, "File exists");
+ inscode(d, ds, de, "EEXIST", EEXIST, "File exists");
#endif
#ifdef EDQUOT
- inscode(d, ds, de, "EDQUOT", EDQUOT, "Quota exceeded");
+ inscode(d, ds, de, "EDQUOT", EDQUOT, "Quota exceeded");
#else
#ifdef WSAEDQUOT
- inscode(d, ds, de, "EDQUOT", WSAEDQUOT, "Quota exceeded");
+ inscode(d, ds, de, "EDQUOT", WSAEDQUOT, "Quota exceeded");
#endif
#endif
#ifdef ENOSTR
- inscode(d, ds, de, "ENOSTR", ENOSTR, "Device not a stream");
+ inscode(d, ds, de, "ENOSTR", ENOSTR, "Device not a stream");
#endif
#ifdef EBADSLT
- inscode(d, ds, de, "EBADSLT", EBADSLT, "Invalid slot");
+ inscode(d, ds, de, "EBADSLT", EBADSLT, "Invalid slot");
#endif
#ifdef EBADRQC
- inscode(d, ds, de, "EBADRQC", EBADRQC, "Invalid request code");
+ inscode(d, ds, de, "EBADRQC", EBADRQC, "Invalid request code");
#endif
#ifdef ELIBACC
- inscode(d, ds, de, "ELIBACC", ELIBACC, "Can not access a needed shared library");
+ inscode(d, ds, de, "ELIBACC", ELIBACC, "Can not access a needed shared library");
#endif
#ifdef EFAULT
- inscode(d, ds, de, "EFAULT", EFAULT, "Bad address");
+ inscode(d, ds, de, "EFAULT", EFAULT, "Bad address");
#else
#ifdef WSAEFAULT
- inscode(d, ds, de, "EFAULT", WSAEFAULT, "Bad address");
+ inscode(d, ds, de, "EFAULT", WSAEFAULT, "Bad address");
#endif
#endif
#ifdef EFBIG
- inscode(d, ds, de, "EFBIG", EFBIG, "File too large");
+ inscode(d, ds, de, "EFBIG", EFBIG, "File too large");
#endif
#ifdef EDEADLK
- inscode(d, ds, de, "EDEADLK", EDEADLK, "Resource deadlock would occur");
+ inscode(d, ds, de, "EDEADLK", EDEADLK, "Resource deadlock would occur");
#endif
#ifdef ENOTCONN
- inscode(d, ds, de, "ENOTCONN", ENOTCONN, "Transport endpoint is not connected");
+ inscode(d, ds, de, "ENOTCONN", ENOTCONN, "Transport endpoint is not connected");
#else
#ifdef WSAENOTCONN
- inscode(d, ds, de, "ENOTCONN", WSAENOTCONN, "Transport endpoint is not connected");
+ inscode(d, ds, de, "ENOTCONN", WSAENOTCONN, "Transport endpoint is not connected");
#endif
#endif
#ifdef EDESTADDRREQ
- inscode(d, ds, de, "EDESTADDRREQ", EDESTADDRREQ, "Destination address required");
+ inscode(d, ds, de, "EDESTADDRREQ", EDESTADDRREQ, "Destination address required");
#else
#ifdef WSAEDESTADDRREQ
- inscode(d, ds, de, "EDESTADDRREQ", WSAEDESTADDRREQ, "Destination address required");
+ inscode(d, ds, de, "EDESTADDRREQ", WSAEDESTADDRREQ, "Destination address required");
#endif
#endif
#ifdef ELIBSCN
- inscode(d, ds, de, "ELIBSCN", ELIBSCN, ".lib section in a.out corrupted");
+ inscode(d, ds, de, "ELIBSCN", ELIBSCN, ".lib section in a.out corrupted");
#endif
#ifdef ENOLCK
- inscode(d, ds, de, "ENOLCK", ENOLCK, "No record locks available");
+ inscode(d, ds, de, "ENOLCK", ENOLCK, "No record locks available");
#endif
#ifdef EISNAM
- inscode(d, ds, de, "EISNAM", EISNAM, "Is a named type file");
+ inscode(d, ds, de, "EISNAM", EISNAM, "Is a named type file");
#endif
#ifdef ECONNABORTED
- inscode(d, ds, de, "ECONNABORTED", ECONNABORTED, "Software caused connection abort");
+ inscode(d, ds, de, "ECONNABORTED", ECONNABORTED, "Software caused connection abort");
#else
#ifdef WSAECONNABORTED
- inscode(d, ds, de, "ECONNABORTED", WSAECONNABORTED, "Software caused connection abort");
+ inscode(d, ds, de, "ECONNABORTED", WSAECONNABORTED, "Software caused connection abort");
#endif
#endif
#ifdef ENETUNREACH
- inscode(d, ds, de, "ENETUNREACH", ENETUNREACH, "Network is unreachable");
+ inscode(d, ds, de, "ENETUNREACH", ENETUNREACH, "Network is unreachable");
#else
#ifdef WSAENETUNREACH
- inscode(d, ds, de, "ENETUNREACH", WSAENETUNREACH, "Network is unreachable");
+ inscode(d, ds, de, "ENETUNREACH", WSAENETUNREACH, "Network is unreachable");
#endif
#endif
#ifdef ESTALE
- inscode(d, ds, de, "ESTALE", ESTALE, "Stale NFS file handle");
+ inscode(d, ds, de, "ESTALE", ESTALE, "Stale NFS file handle");
#else
#ifdef WSAESTALE
- inscode(d, ds, de, "ESTALE", WSAESTALE, "Stale NFS file handle");
+ inscode(d, ds, de, "ESTALE", WSAESTALE, "Stale NFS file handle");
#endif
#endif
#ifdef ENOSR
- inscode(d, ds, de, "ENOSR", ENOSR, "Out of streams resources");
+ inscode(d, ds, de, "ENOSR", ENOSR, "Out of streams resources");
#endif
#ifdef ENOMEM
- inscode(d, ds, de, "ENOMEM", ENOMEM, "Out of memory");
+ inscode(d, ds, de, "ENOMEM", ENOMEM, "Out of memory");
#endif
#ifdef ENOTSOCK
- inscode(d, ds, de, "ENOTSOCK", ENOTSOCK, "Socket operation on non-socket");
+ inscode(d, ds, de, "ENOTSOCK", ENOTSOCK, "Socket operation on non-socket");
#else
#ifdef WSAENOTSOCK
- inscode(d, ds, de, "ENOTSOCK", WSAENOTSOCK, "Socket operation on non-socket");
+ inscode(d, ds, de, "ENOTSOCK", WSAENOTSOCK, "Socket operation on non-socket");
#endif
#endif
#ifdef ESTRPIPE
- inscode(d, ds, de, "ESTRPIPE", ESTRPIPE, "Streams pipe error");
+ inscode(d, ds, de, "ESTRPIPE", ESTRPIPE, "Streams pipe error");
#endif
#ifdef EMLINK
- inscode(d, ds, de, "EMLINK", EMLINK, "Too many links");
+ inscode(d, ds, de, "EMLINK", EMLINK, "Too many links");
#endif
#ifdef ERANGE
- inscode(d, ds, de, "ERANGE", ERANGE, "Math result not representable");
+ inscode(d, ds, de, "ERANGE", ERANGE, "Math result not representable");
#endif
#ifdef ELIBEXEC
- inscode(d, ds, de, "ELIBEXEC", ELIBEXEC, "Cannot exec a shared library directly");
+ inscode(d, ds, de, "ELIBEXEC", ELIBEXEC, "Cannot exec a shared library directly");
#endif
#ifdef EL3HLT
- inscode(d, ds, de, "EL3HLT", EL3HLT, "Level 3 halted");
+ inscode(d, ds, de, "EL3HLT", EL3HLT, "Level 3 halted");
#endif
#ifdef ECONNRESET
- inscode(d, ds, de, "ECONNRESET", ECONNRESET, "Connection reset by peer");
+ inscode(d, ds, de, "ECONNRESET", ECONNRESET, "Connection reset by peer");
#else
#ifdef WSAECONNRESET
- inscode(d, ds, de, "ECONNRESET", WSAECONNRESET, "Connection reset by peer");
+ inscode(d, ds, de, "ECONNRESET", WSAECONNRESET, "Connection reset by peer");
#endif
#endif
#ifdef EADDRINUSE
- inscode(d, ds, de, "EADDRINUSE", EADDRINUSE, "Address already in use");
+ inscode(d, ds, de, "EADDRINUSE", EADDRINUSE, "Address already in use");
#else
#ifdef WSAEADDRINUSE
- inscode(d, ds, de, "EADDRINUSE", WSAEADDRINUSE, "Address already in use");
+ inscode(d, ds, de, "EADDRINUSE", WSAEADDRINUSE, "Address already in use");
#endif
#endif
#ifdef EOPNOTSUPP
- inscode(d, ds, de, "EOPNOTSUPP", EOPNOTSUPP, "Operation not supported on transport endpoint");
+ inscode(d, ds, de, "EOPNOTSUPP", EOPNOTSUPP, "Operation not supported on transport endpoint");
#else
#ifdef WSAEOPNOTSUPP
- inscode(d, ds, de, "EOPNOTSUPP", WSAEOPNOTSUPP, "Operation not supported on transport endpoint");
+ inscode(d, ds, de, "EOPNOTSUPP", WSAEOPNOTSUPP, "Operation not supported on transport endpoint");
#endif
#endif
#ifdef EREMCHG
- inscode(d, ds, de, "EREMCHG", EREMCHG, "Remote address changed");
+ inscode(d, ds, de, "EREMCHG", EREMCHG, "Remote address changed");
#endif
#ifdef EAGAIN
- inscode(d, ds, de, "EAGAIN", EAGAIN, "Try again");
+ inscode(d, ds, de, "EAGAIN", EAGAIN, "Try again");
#endif
#ifdef ENAMETOOLONG
- inscode(d, ds, de, "ENAMETOOLONG", ENAMETOOLONG, "File name too long");
+ inscode(d, ds, de, "ENAMETOOLONG", ENAMETOOLONG, "File name too long");
#else
#ifdef WSAENAMETOOLONG
- inscode(d, ds, de, "ENAMETOOLONG", WSAENAMETOOLONG, "File name too long");
+ inscode(d, ds, de, "ENAMETOOLONG", WSAENAMETOOLONG, "File name too long");
#endif
#endif
#ifdef ENOTTY
- inscode(d, ds, de, "ENOTTY", ENOTTY, "Not a typewriter");
+ inscode(d, ds, de, "ENOTTY", ENOTTY, "Not a typewriter");
#endif
#ifdef ERESTART
- inscode(d, ds, de, "ERESTART", ERESTART, "Interrupted system call should be restarted");
+ inscode(d, ds, de, "ERESTART", ERESTART, "Interrupted system call should be restarted");
#endif
#ifdef ESOCKTNOSUPPORT
- inscode(d, ds, de, "ESOCKTNOSUPPORT", ESOCKTNOSUPPORT, "Socket type not supported");
+ inscode(d, ds, de, "ESOCKTNOSUPPORT", ESOCKTNOSUPPORT, "Socket type not supported");
#else
#ifdef WSAESOCKTNOSUPPORT
- inscode(d, ds, de, "ESOCKTNOSUPPORT", WSAESOCKTNOSUPPORT, "Socket type not supported");
+ inscode(d, ds, de, "ESOCKTNOSUPPORT", WSAESOCKTNOSUPPORT, "Socket type not supported");
#endif
#endif
#ifdef ETIME
- inscode(d, ds, de, "ETIME", ETIME, "Timer expired");
+ inscode(d, ds, de, "ETIME", ETIME, "Timer expired");
#endif
#ifdef EBFONT
- inscode(d, ds, de, "EBFONT", EBFONT, "Bad font file format");
+ inscode(d, ds, de, "EBFONT", EBFONT, "Bad font file format");
#endif
#ifdef EDEADLOCK
- inscode(d, ds, de, "EDEADLOCK", EDEADLOCK, "Error EDEADLOCK");
+ inscode(d, ds, de, "EDEADLOCK", EDEADLOCK, "Error EDEADLOCK");
#endif
#ifdef ETOOMANYREFS
- inscode(d, ds, de, "ETOOMANYREFS", ETOOMANYREFS, "Too many references: cannot splice");
+ inscode(d, ds, de, "ETOOMANYREFS", ETOOMANYREFS, "Too many references: cannot splice");
#else
#ifdef WSAETOOMANYREFS
- inscode(d, ds, de, "ETOOMANYREFS", WSAETOOMANYREFS, "Too many references: cannot splice");
+ inscode(d, ds, de, "ETOOMANYREFS", WSAETOOMANYREFS, "Too many references: cannot splice");
#endif
#endif
#ifdef EMFILE
- inscode(d, ds, de, "EMFILE", EMFILE, "Too many open files");
+ inscode(d, ds, de, "EMFILE", EMFILE, "Too many open files");
#else
#ifdef WSAEMFILE
- inscode(d, ds, de, "EMFILE", WSAEMFILE, "Too many open files");
+ inscode(d, ds, de, "EMFILE", WSAEMFILE, "Too many open files");
#endif
#endif
#ifdef ETXTBSY
- inscode(d, ds, de, "ETXTBSY", ETXTBSY, "Text file busy");
+ inscode(d, ds, de, "ETXTBSY", ETXTBSY, "Text file busy");
#endif
#ifdef EINPROGRESS
- inscode(d, ds, de, "EINPROGRESS", EINPROGRESS, "Operation now in progress");
+ inscode(d, ds, de, "EINPROGRESS", EINPROGRESS, "Operation now in progress");
#else
#ifdef WSAEINPROGRESS
- inscode(d, ds, de, "EINPROGRESS", WSAEINPROGRESS, "Operation now in progress");
+ inscode(d, ds, de, "EINPROGRESS", WSAEINPROGRESS, "Operation now in progress");
#endif
#endif
#ifdef ENXIO
- inscode(d, ds, de, "ENXIO", ENXIO, "No such device or address");
+ inscode(d, ds, de, "ENXIO", ENXIO, "No such device or address");
#endif
#ifdef ENOPKG
- inscode(d, ds, de, "ENOPKG", ENOPKG, "Package not installed");
+ inscode(d, ds, de, "ENOPKG", ENOPKG, "Package not installed");
#endif
#ifdef WSASY
- inscode(d, ds, de, "WSASY", WSASY, "Error WSASY");
+ inscode(d, ds, de, "WSASY", WSASY, "Error WSASY");
#endif
#ifdef WSAEHOSTDOWN
- inscode(d, ds, de, "WSAEHOSTDOWN", WSAEHOSTDOWN, "Host is down");
+ inscode(d, ds, de, "WSAEHOSTDOWN", WSAEHOSTDOWN, "Host is down");
#endif
#ifdef WSAENETDOWN
- inscode(d, ds, de, "WSAENETDOWN", WSAENETDOWN, "Network is down");
+ inscode(d, ds, de, "WSAENETDOWN", WSAENETDOWN, "Network is down");
#endif
#ifdef WSAENOTSOCK
- inscode(d, ds, de, "WSAENOTSOCK", WSAENOTSOCK, "Socket operation on non-socket");
+ inscode(d, ds, de, "WSAENOTSOCK", WSAENOTSOCK, "Socket operation on non-socket");
#endif
#ifdef WSAEHOSTUNREACH
- inscode(d, ds, de, "WSAEHOSTUNREACH", WSAEHOSTUNREACH, "No route to host");
+ inscode(d, ds, de, "WSAEHOSTUNREACH", WSAEHOSTUNREACH, "No route to host");
#endif
#ifdef WSAELOOP
- inscode(d, ds, de, "WSAELOOP", WSAELOOP, "Too many symbolic links encountered");
+ inscode(d, ds, de, "WSAELOOP", WSAELOOP, "Too many symbolic links encountered");
#endif
#ifdef WSAEMFILE
- inscode(d, ds, de, "WSAEMFILE", WSAEMFILE, "Too many open files");
+ inscode(d, ds, de, "WSAEMFILE", WSAEMFILE, "Too many open files");
#endif
#ifdef WSAESTALE
- inscode(d, ds, de, "WSAESTALE", WSAESTALE, "Stale NFS file handle");
+ inscode(d, ds, de, "WSAESTALE", WSAESTALE, "Stale NFS file handle");
#endif
#ifdef WSAVERNOTSUPPORTED
- inscode(d, ds, de, "WSAVERNOTSUPPORTED", WSAVERNOTSUPPORTED, "Error WSAVERNOTSUPPORTED");
+ inscode(d, ds, de, "WSAVERNOTSUPPORTED", WSAVERNOTSUPPORTED, "Error WSAVERNOTSUPPORTED");
#endif
#ifdef WSAENETUNREACH
- inscode(d, ds, de, "WSAENETUNREACH", WSAENETUNREACH, "Network is unreachable");
+ inscode(d, ds, de, "WSAENETUNREACH", WSAENETUNREACH, "Network is unreachable");
#endif
#ifdef WSAEPROCLIM
- inscode(d, ds, de, "WSAEPROCLIM", WSAEPROCLIM, "Error WSAEPROCLIM");
+ inscode(d, ds, de, "WSAEPROCLIM", WSAEPROCLIM, "Error WSAEPROCLIM");
#endif
#ifdef WSAEFAULT
- inscode(d, ds, de, "WSAEFAULT", WSAEFAULT, "Bad address");
+ inscode(d, ds, de, "WSAEFAULT", WSAEFAULT, "Bad address");
#endif
#ifdef WSANOTINITIALISED
- inscode(d, ds, de, "WSANOTINITIALISED", WSANOTINITIALISED, "Error WSANOTINITIALISED");
+ inscode(d, ds, de, "WSANOTINITIALISED", WSANOTINITIALISED, "Error WSANOTINITIALISED");
#endif
#ifdef WSAEUSERS
- inscode(d, ds, de, "WSAEUSERS", WSAEUSERS, "Too many users");
+ inscode(d, ds, de, "WSAEUSERS", WSAEUSERS, "Too many users");
#endif
#ifdef WSAMAKEASYNCREPL
- inscode(d, ds, de, "WSAMAKEASYNCREPL", WSAMAKEASYNCREPL, "Error WSAMAKEASYNCREPL");
+ inscode(d, ds, de, "WSAMAKEASYNCREPL", WSAMAKEASYNCREPL, "Error WSAMAKEASYNCREPL");
#endif
#ifdef WSAENOPROTOOPT
- inscode(d, ds, de, "WSAENOPROTOOPT", WSAENOPROTOOPT, "Protocol not available");
+ inscode(d, ds, de, "WSAENOPROTOOPT", WSAENOPROTOOPT, "Protocol not available");
#endif
#ifdef WSAECONNABORTED
- inscode(d, ds, de, "WSAECONNABORTED", WSAECONNABORTED, "Software caused connection abort");
+ inscode(d, ds, de, "WSAECONNABORTED", WSAECONNABORTED, "Software caused connection abort");
#endif
#ifdef WSAENAMETOOLONG
- inscode(d, ds, de, "WSAENAMETOOLONG", WSAENAMETOOLONG, "File name too long");
+ inscode(d, ds, de, "WSAENAMETOOLONG", WSAENAMETOOLONG, "File name too long");
#endif
#ifdef WSAENOTEMPTY
- inscode(d, ds, de, "WSAENOTEMPTY", WSAENOTEMPTY, "Directory not empty");
+ inscode(d, ds, de, "WSAENOTEMPTY", WSAENOTEMPTY, "Directory not empty");
#endif
#ifdef WSAESHUTDOWN
- inscode(d, ds, de, "WSAESHUTDOWN", WSAESHUTDOWN, "Cannot send after transport endpoint shutdown");
+ inscode(d, ds, de, "WSAESHUTDOWN", WSAESHUTDOWN, "Cannot send after transport endpoint shutdown");
#endif
#ifdef WSAEAFNOSUPPORT
- inscode(d, ds, de, "WSAEAFNOSUPPORT", WSAEAFNOSUPPORT, "Address family not supported by protocol");
+ inscode(d, ds, de, "WSAEAFNOSUPPORT", WSAEAFNOSUPPORT, "Address family not supported by protocol");
#endif
#ifdef WSAETOOMANYREFS
- inscode(d, ds, de, "WSAETOOMANYREFS", WSAETOOMANYREFS, "Too many references: cannot splice");
+ inscode(d, ds, de, "WSAETOOMANYREFS", WSAETOOMANYREFS, "Too many references: cannot splice");
#endif
#ifdef WSAEACCES
- inscode(d, ds, de, "WSAEACCES", WSAEACCES, "Permission denied");
+ inscode(d, ds, de, "WSAEACCES", WSAEACCES, "Permission denied");
#endif
#ifdef WSATR
- inscode(d, ds, de, "WSATR", WSATR, "Error WSATR");
+ inscode(d, ds, de, "WSATR", WSATR, "Error WSATR");
#endif
#ifdef WSABASEERR
- inscode(d, ds, de, "WSABASEERR", WSABASEERR, "Error WSABASEERR");
+ inscode(d, ds, de, "WSABASEERR", WSABASEERR, "Error WSABASEERR");
#endif
#ifdef WSADESCRIPTIO
- inscode(d, ds, de, "WSADESCRIPTIO", WSADESCRIPTIO, "Error WSADESCRIPTIO");
+ inscode(d, ds, de, "WSADESCRIPTIO", WSADESCRIPTIO, "Error WSADESCRIPTIO");
#endif
#ifdef WSAEMSGSIZE
- inscode(d, ds, de, "WSAEMSGSIZE", WSAEMSGSIZE, "Message too long");
+ inscode(d, ds, de, "WSAEMSGSIZE", WSAEMSGSIZE, "Message too long");
#endif
#ifdef WSAEBADF
- inscode(d, ds, de, "WSAEBADF", WSAEBADF, "Bad file number");
+ inscode(d, ds, de, "WSAEBADF", WSAEBADF, "Bad file number");
#endif
#ifdef WSAECONNRESET
- inscode(d, ds, de, "WSAECONNRESET", WSAECONNRESET, "Connection reset by peer");
+ inscode(d, ds, de, "WSAECONNRESET", WSAECONNRESET, "Connection reset by peer");
#endif
#ifdef WSAGETSELECTERRO
- inscode(d, ds, de, "WSAGETSELECTERRO", WSAGETSELECTERRO, "Error WSAGETSELECTERRO");
+ inscode(d, ds, de, "WSAGETSELECTERRO", WSAGETSELECTERRO, "Error WSAGETSELECTERRO");
#endif
#ifdef WSAETIMEDOUT
- inscode(d, ds, de, "WSAETIMEDOUT", WSAETIMEDOUT, "Connection timed out");
+ inscode(d, ds, de, "WSAETIMEDOUT", WSAETIMEDOUT, "Connection timed out");
#endif
#ifdef WSAENOBUFS
- inscode(d, ds, de, "WSAENOBUFS", WSAENOBUFS, "No buffer space available");
+ inscode(d, ds, de, "WSAENOBUFS", WSAENOBUFS, "No buffer space available");
#endif
#ifdef WSAEDISCON
- inscode(d, ds, de, "WSAEDISCON", WSAEDISCON, "Error WSAEDISCON");
+ inscode(d, ds, de, "WSAEDISCON", WSAEDISCON, "Error WSAEDISCON");
#endif
#ifdef WSAEINTR
- inscode(d, ds, de, "WSAEINTR", WSAEINTR, "Interrupted system call");
+ inscode(d, ds, de, "WSAEINTR", WSAEINTR, "Interrupted system call");
#endif
#ifdef WSAEPROTOTYPE
- inscode(d, ds, de, "WSAEPROTOTYPE", WSAEPROTOTYPE, "Protocol wrong type for socket");
+ inscode(d, ds, de, "WSAEPROTOTYPE", WSAEPROTOTYPE, "Protocol wrong type for socket");
#endif
#ifdef WSAHOS
- inscode(d, ds, de, "WSAHOS", WSAHOS, "Error WSAHOS");
+ inscode(d, ds, de, "WSAHOS", WSAHOS, "Error WSAHOS");
#endif
#ifdef WSAEADDRINUSE
- inscode(d, ds, de, "WSAEADDRINUSE", WSAEADDRINUSE, "Address already in use");
+ inscode(d, ds, de, "WSAEADDRINUSE", WSAEADDRINUSE, "Address already in use");
#endif
#ifdef WSAEADDRNOTAVAIL
- inscode(d, ds, de, "WSAEADDRNOTAVAIL", WSAEADDRNOTAVAIL, "Cannot assign requested address");
+ inscode(d, ds, de, "WSAEADDRNOTAVAIL", WSAEADDRNOTAVAIL, "Cannot assign requested address");
#endif
#ifdef WSAEALREADY
- inscode(d, ds, de, "WSAEALREADY", WSAEALREADY, "Operation already in progress");
+ inscode(d, ds, de, "WSAEALREADY", WSAEALREADY, "Operation already in progress");
#endif
#ifdef WSAEPROTONOSUPPORT
- inscode(d, ds, de, "WSAEPROTONOSUPPORT", WSAEPROTONOSUPPORT, "Protocol not supported");
+ inscode(d, ds, de, "WSAEPROTONOSUPPORT", WSAEPROTONOSUPPORT, "Protocol not supported");
#endif
#ifdef WSASYSNOTREADY
- inscode(d, ds, de, "WSASYSNOTREADY", WSASYSNOTREADY, "Error WSASYSNOTREADY");
+ inscode(d, ds, de, "WSASYSNOTREADY", WSASYSNOTREADY, "Error WSASYSNOTREADY");
#endif
#ifdef WSAEWOULDBLOCK
- inscode(d, ds, de, "WSAEWOULDBLOCK", WSAEWOULDBLOCK, "Operation would block");
+ inscode(d, ds, de, "WSAEWOULDBLOCK", WSAEWOULDBLOCK, "Operation would block");
#endif
#ifdef WSAEPFNOSUPPORT
- inscode(d, ds, de, "WSAEPFNOSUPPORT", WSAEPFNOSUPPORT, "Protocol family not supported");
+ inscode(d, ds, de, "WSAEPFNOSUPPORT", WSAEPFNOSUPPORT, "Protocol family not supported");
#endif
#ifdef WSAEOPNOTSUPP
- inscode(d, ds, de, "WSAEOPNOTSUPP", WSAEOPNOTSUPP, "Operation not supported on transport endpoint");
+ inscode(d, ds, de, "WSAEOPNOTSUPP", WSAEOPNOTSUPP, "Operation not supported on transport endpoint");
#endif
#ifdef WSAEISCONN
- inscode(d, ds, de, "WSAEISCONN", WSAEISCONN, "Transport endpoint is already connected");
+ inscode(d, ds, de, "WSAEISCONN", WSAEISCONN, "Transport endpoint is already connected");
#endif
#ifdef WSAEDQUOT
- inscode(d, ds, de, "WSAEDQUOT", WSAEDQUOT, "Quota exceeded");
+ inscode(d, ds, de, "WSAEDQUOT", WSAEDQUOT, "Quota exceeded");
#endif
#ifdef WSAENOTCONN
- inscode(d, ds, de, "WSAENOTCONN", WSAENOTCONN, "Transport endpoint is not connected");
+ inscode(d, ds, de, "WSAENOTCONN", WSAENOTCONN, "Transport endpoint is not connected");
#endif
#ifdef WSAEREMOTE
- inscode(d, ds, de, "WSAEREMOTE", WSAEREMOTE, "Object is remote");
+ inscode(d, ds, de, "WSAEREMOTE", WSAEREMOTE, "Object is remote");
#endif
#ifdef WSAEINVAL
- inscode(d, ds, de, "WSAEINVAL", WSAEINVAL, "Invalid argument");
+ inscode(d, ds, de, "WSAEINVAL", WSAEINVAL, "Invalid argument");
#endif
#ifdef WSAEINPROGRESS
- inscode(d, ds, de, "WSAEINPROGRESS", WSAEINPROGRESS, "Operation now in progress");
+ inscode(d, ds, de, "WSAEINPROGRESS", WSAEINPROGRESS, "Operation now in progress");
#endif
#ifdef WSAGETSELECTEVEN
- inscode(d, ds, de, "WSAGETSELECTEVEN", WSAGETSELECTEVEN, "Error WSAGETSELECTEVEN");
+ inscode(d, ds, de, "WSAGETSELECTEVEN", WSAGETSELECTEVEN, "Error WSAGETSELECTEVEN");
#endif
#ifdef WSAESOCKTNOSUPPORT
- inscode(d, ds, de, "WSAESOCKTNOSUPPORT", WSAESOCKTNOSUPPORT, "Socket type not supported");
+ inscode(d, ds, de, "WSAESOCKTNOSUPPORT", WSAESOCKTNOSUPPORT, "Socket type not supported");
#endif
#ifdef WSAGETASYNCERRO
- inscode(d, ds, de, "WSAGETASYNCERRO", WSAGETASYNCERRO, "Error WSAGETASYNCERRO");
+ inscode(d, ds, de, "WSAGETASYNCERRO", WSAGETASYNCERRO, "Error WSAGETASYNCERRO");
#endif
#ifdef WSAMAKESELECTREPL
- inscode(d, ds, de, "WSAMAKESELECTREPL", WSAMAKESELECTREPL, "Error WSAMAKESELECTREPL");
+ inscode(d, ds, de, "WSAMAKESELECTREPL", WSAMAKESELECTREPL, "Error WSAMAKESELECTREPL");
#endif
#ifdef WSAGETASYNCBUFLE
- inscode(d, ds, de, "WSAGETASYNCBUFLE", WSAGETASYNCBUFLE, "Error WSAGETASYNCBUFLE");
+ inscode(d, ds, de, "WSAGETASYNCBUFLE", WSAGETASYNCBUFLE, "Error WSAGETASYNCBUFLE");
#endif
#ifdef WSAEDESTADDRREQ
- inscode(d, ds, de, "WSAEDESTADDRREQ", WSAEDESTADDRREQ, "Destination address required");
+ inscode(d, ds, de, "WSAEDESTADDRREQ", WSAEDESTADDRREQ, "Destination address required");
#endif
#ifdef WSAECONNREFUSED
- inscode(d, ds, de, "WSAECONNREFUSED", WSAECONNREFUSED, "Connection refused");
+ inscode(d, ds, de, "WSAECONNREFUSED", WSAECONNREFUSED, "Connection refused");
#endif
#ifdef WSAENETRESET
- inscode(d, ds, de, "WSAENETRESET", WSAENETRESET, "Network dropped connection because of reset");
+ inscode(d, ds, de, "WSAENETRESET", WSAENETRESET, "Network dropped connection because of reset");
#endif
#ifdef WSAN
- inscode(d, ds, de, "WSAN", WSAN, "Error WSAN");
+ inscode(d, ds, de, "WSAN", WSAN, "Error WSAN");
#endif
- Py_DECREF(de);
- return m;
+ Py_DECREF(de);
+ return m;
}
diff --git a/Modules/fcntlmodule.c b/Modules/fcntlmodule.c
index 683c5b6..5bf4010 100644
--- a/Modules/fcntlmodule.c
+++ b/Modules/fcntlmodule.c
@@ -21,7 +21,7 @@ conv_descriptor(PyObject *object, int *target)
int fd = PyObject_AsFileDescriptor(object);
if (fd < 0)
- return 0;
+ return 0;
*target = fd;
return 1;
}
@@ -32,48 +32,48 @@ conv_descriptor(PyObject *object, int *target)
static PyObject *
fcntl_fcntl(PyObject *self, PyObject *args)
{
- int fd;
- int code;
- long arg;
- int ret;
- char *str;
- Py_ssize_t len;
- char buf[1024];
-
- if (PyArg_ParseTuple(args, "O&is#:fcntl",
- conv_descriptor, &fd, &code, &str, &len)) {
- if (len > sizeof buf) {
- PyErr_SetString(PyExc_ValueError,
- "fcntl string arg too long");
- return NULL;
- }
- memcpy(buf, str, len);
- Py_BEGIN_ALLOW_THREADS
- ret = fcntl(fd, code, buf);
- Py_END_ALLOW_THREADS
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return PyBytes_FromStringAndSize(buf, len);
- }
-
- PyErr_Clear();
- arg = 0;
- if (!PyArg_ParseTuple(args,
- "O&i|l;fcntl requires a file or file descriptor,"
- " an integer and optionally a third integer or a string",
- conv_descriptor, &fd, &code, &arg)) {
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- ret = fcntl(fd, code, arg);
- Py_END_ALLOW_THREADS
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return PyLong_FromLong((long)ret);
+ int fd;
+ int code;
+ long arg;
+ int ret;
+ char *str;
+ Py_ssize_t len;
+ char buf[1024];
+
+ if (PyArg_ParseTuple(args, "O&is#:fcntl",
+ conv_descriptor, &fd, &code, &str, &len)) {
+ if (len > sizeof buf) {
+ PyErr_SetString(PyExc_ValueError,
+ "fcntl string arg too long");
+ return NULL;
+ }
+ memcpy(buf, str, len);
+ Py_BEGIN_ALLOW_THREADS
+ ret = fcntl(fd, code, buf);
+ Py_END_ALLOW_THREADS
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return PyBytes_FromStringAndSize(buf, len);
+ }
+
+ PyErr_Clear();
+ arg = 0;
+ if (!PyArg_ParseTuple(args,
+ "O&i|l;fcntl requires a file or file descriptor,"
+ " an integer and optionally a third integer or a string",
+ conv_descriptor, &fd, &code, &arg)) {
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ ret = fcntl(fd, code, arg);
+ Py_END_ALLOW_THREADS
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return PyLong_FromLong((long)ret);
}
PyDoc_STRVAR(fcntl_doc,
@@ -96,128 +96,128 @@ static PyObject *
fcntl_ioctl(PyObject *self, PyObject *args)
{
#define IOCTL_BUFSZ 1024
- int fd;
- /* In PyArg_ParseTuple below, we use the unsigned non-checked 'I'
- format for the 'code' parameter because Python turns 0x8000000
- into either a large positive number (PyLong or PyInt on 64-bit
- platforms) or a negative number on others (32-bit PyInt)
- whereas the system expects it to be a 32bit bit field value
- regardless of it being passed as an int or unsigned long on
- various platforms. See the termios.TIOCSWINSZ constant across
- platforms for an example of thise.
-
- If any of the 64bit platforms ever decide to use more than 32bits
- in their unsigned long ioctl codes this will break and need
- special casing based on the platform being built on.
- */
- unsigned int code;
- int arg;
- int ret;
- Py_buffer pstr;
- char *str;
- Py_ssize_t len;
- int mutate_arg = 1;
- char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */
-
- if (PyArg_ParseTuple(args, "O&Iw*|i:ioctl",
- conv_descriptor, &fd, &code,
- &pstr, &mutate_arg)) {
- char *arg;
- str = pstr.buf;
- len = pstr.len;
-
- if (mutate_arg) {
- if (len <= IOCTL_BUFSZ) {
- memcpy(buf, str, len);
- buf[len] = '\0';
- arg = buf;
- }
- else {
- arg = str;
- }
- }
- else {
- if (len > IOCTL_BUFSZ) {
- PyBuffer_Release(&pstr);
- PyErr_SetString(PyExc_ValueError,
- "ioctl string arg too long");
- return NULL;
- }
- else {
- memcpy(buf, str, len);
- buf[len] = '\0';
- arg = buf;
- }
- }
- if (buf == arg) {
- Py_BEGIN_ALLOW_THREADS /* think array.resize() */
- ret = ioctl(fd, code, arg);
- Py_END_ALLOW_THREADS
- }
- else {
- ret = ioctl(fd, code, arg);
- }
- if (mutate_arg && (len < IOCTL_BUFSZ)) {
- memcpy(str, buf, len);
- }
- PyBuffer_Release(&pstr); /* No further access to str below this point */
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- if (mutate_arg) {
- return PyLong_FromLong(ret);
- }
- else {
- return PyBytes_FromStringAndSize(buf, len);
- }
- }
-
- PyErr_Clear();
- if (PyArg_ParseTuple(args, "O&Is*:ioctl",
- conv_descriptor, &fd, &code, &pstr)) {
- str = pstr.buf;
- len = pstr.len;
- if (len > IOCTL_BUFSZ) {
- PyBuffer_Release(&pstr);
- PyErr_SetString(PyExc_ValueError,
- "ioctl string arg too long");
- return NULL;
- }
- memcpy(buf, str, len);
- buf[len] = '\0';
- Py_BEGIN_ALLOW_THREADS
- ret = ioctl(fd, code, buf);
- Py_END_ALLOW_THREADS
- if (ret < 0) {
- PyBuffer_Release(&pstr);
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- PyBuffer_Release(&pstr);
- return PyBytes_FromStringAndSize(buf, len);
- }
-
- PyErr_Clear();
- arg = 0;
- if (!PyArg_ParseTuple(args,
- "O&I|i;ioctl requires a file or file descriptor,"
- " an integer and optionally an integer or buffer argument",
- conv_descriptor, &fd, &code, &arg)) {
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
+ int fd;
+ /* In PyArg_ParseTuple below, we use the unsigned non-checked 'I'
+ format for the 'code' parameter because Python turns 0x8000000
+ into either a large positive number (PyLong or PyInt on 64-bit
+ platforms) or a negative number on others (32-bit PyInt)
+ whereas the system expects it to be a 32bit bit field value
+ regardless of it being passed as an int or unsigned long on
+ various platforms. See the termios.TIOCSWINSZ constant across
+ platforms for an example of thise.
+
+ If any of the 64bit platforms ever decide to use more than 32bits
+ in their unsigned long ioctl codes this will break and need
+ special casing based on the platform being built on.
+ */
+ unsigned int code;
+ int arg;
+ int ret;
+ Py_buffer pstr;
+ char *str;
+ Py_ssize_t len;
+ int mutate_arg = 1;
+ char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */
+
+ if (PyArg_ParseTuple(args, "O&Iw*|i:ioctl",
+ conv_descriptor, &fd, &code,
+ &pstr, &mutate_arg)) {
+ char *arg;
+ str = pstr.buf;
+ len = pstr.len;
+
+ if (mutate_arg) {
+ if (len <= IOCTL_BUFSZ) {
+ memcpy(buf, str, len);
+ buf[len] = '\0';
+ arg = buf;
+ }
+ else {
+ arg = str;
+ }
+ }
+ else {
+ if (len > IOCTL_BUFSZ) {
+ PyBuffer_Release(&pstr);
+ PyErr_SetString(PyExc_ValueError,
+ "ioctl string arg too long");
+ return NULL;
+ }
+ else {
+ memcpy(buf, str, len);
+ buf[len] = '\0';
+ arg = buf;
+ }
+ }
+ if (buf == arg) {
+ Py_BEGIN_ALLOW_THREADS /* think array.resize() */
+ ret = ioctl(fd, code, arg);
+ Py_END_ALLOW_THREADS
+ }
+ else {
+ ret = ioctl(fd, code, arg);
+ }
+ if (mutate_arg && (len < IOCTL_BUFSZ)) {
+ memcpy(str, buf, len);
+ }
+ PyBuffer_Release(&pstr); /* No further access to str below this point */
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ if (mutate_arg) {
+ return PyLong_FromLong(ret);
+ }
+ else {
+ return PyBytes_FromStringAndSize(buf, len);
+ }
+ }
+
+ PyErr_Clear();
+ if (PyArg_ParseTuple(args, "O&Is*:ioctl",
+ conv_descriptor, &fd, &code, &pstr)) {
+ str = pstr.buf;
+ len = pstr.len;
+ if (len > IOCTL_BUFSZ) {
+ PyBuffer_Release(&pstr);
+ PyErr_SetString(PyExc_ValueError,
+ "ioctl string arg too long");
+ return NULL;
+ }
+ memcpy(buf, str, len);
+ buf[len] = '\0';
+ Py_BEGIN_ALLOW_THREADS
+ ret = ioctl(fd, code, buf);
+ Py_END_ALLOW_THREADS
+ if (ret < 0) {
+ PyBuffer_Release(&pstr);
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ PyBuffer_Release(&pstr);
+ return PyBytes_FromStringAndSize(buf, len);
+ }
+
+ PyErr_Clear();
+ arg = 0;
+ if (!PyArg_ParseTuple(args,
+ "O&I|i;ioctl requires a file or file descriptor,"
+ " an integer and optionally an integer or buffer argument",
+ conv_descriptor, &fd, &code, &arg)) {
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
#ifdef __VMS
- ret = ioctl(fd, code, (void *)arg);
+ ret = ioctl(fd, code, (void *)arg);
#else
- ret = ioctl(fd, code, arg);
-#endif
- Py_END_ALLOW_THREADS
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return PyLong_FromLong((long)ret);
+ ret = ioctl(fd, code, arg);
+#endif
+ Py_END_ALLOW_THREADS
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return PyLong_FromLong((long)ret);
#undef IOCTL_BUFSZ
}
@@ -258,51 +258,51 @@ code.");
static PyObject *
fcntl_flock(PyObject *self, PyObject *args)
{
- int fd;
- int code;
- int ret;
+ int fd;
+ int code;
+ int ret;
- if (!PyArg_ParseTuple(args, "O&i:flock",
- conv_descriptor, &fd, &code))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O&i:flock",
+ conv_descriptor, &fd, &code))
+ return NULL;
#ifdef HAVE_FLOCK
- Py_BEGIN_ALLOW_THREADS
- ret = flock(fd, code);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ ret = flock(fd, code);
+ Py_END_ALLOW_THREADS
#else
#ifndef LOCK_SH
-#define LOCK_SH 1 /* shared lock */
-#define LOCK_EX 2 /* exclusive lock */
-#define LOCK_NB 4 /* don't block when locking */
-#define LOCK_UN 8 /* unlock */
-#endif
- {
- struct flock l;
- if (code == LOCK_UN)
- l.l_type = F_UNLCK;
- else if (code & LOCK_SH)
- l.l_type = F_RDLCK;
- else if (code & LOCK_EX)
- l.l_type = F_WRLCK;
- else {
- PyErr_SetString(PyExc_ValueError,
- "unrecognized flock argument");
- return NULL;
- }
- l.l_whence = l.l_start = l.l_len = 0;
- Py_BEGIN_ALLOW_THREADS
- ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
- Py_END_ALLOW_THREADS
- }
+#define LOCK_SH 1 /* shared lock */
+#define LOCK_EX 2 /* exclusive lock */
+#define LOCK_NB 4 /* don't block when locking */
+#define LOCK_UN 8 /* unlock */
+#endif
+ {
+ struct flock l;
+ if (code == LOCK_UN)
+ l.l_type = F_UNLCK;
+ else if (code & LOCK_SH)
+ l.l_type = F_RDLCK;
+ else if (code & LOCK_EX)
+ l.l_type = F_WRLCK;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "unrecognized flock argument");
+ return NULL;
+ }
+ l.l_whence = l.l_start = l.l_len = 0;
+ Py_BEGIN_ALLOW_THREADS
+ ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
+ Py_END_ALLOW_THREADS
+ }
#endif /* HAVE_FLOCK */
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(flock_doc,
@@ -317,72 +317,72 @@ emulated using fcntl().)");
static PyObject *
fcntl_lockf(PyObject *self, PyObject *args)
{
- int fd, code, ret, whence = 0;
- PyObject *lenobj = NULL, *startobj = NULL;
+ int fd, code, ret, whence = 0;
+ PyObject *lenobj = NULL, *startobj = NULL;
- if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
- conv_descriptor, &fd, &code,
- &lenobj, &startobj, &whence))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
+ conv_descriptor, &fd, &code,
+ &lenobj, &startobj, &whence))
+ return NULL;
#if defined(PYOS_OS2) && defined(PYCC_GCC)
- PyErr_SetString(PyExc_NotImplementedError,
- "lockf not supported on OS/2 (EMX)");
- return NULL;
+ PyErr_SetString(PyExc_NotImplementedError,
+ "lockf not supported on OS/2 (EMX)");
+ return NULL;
#else
#ifndef LOCK_SH
-#define LOCK_SH 1 /* shared lock */
-#define LOCK_EX 2 /* exclusive lock */
-#define LOCK_NB 4 /* don't block when locking */
-#define LOCK_UN 8 /* unlock */
+#define LOCK_SH 1 /* shared lock */
+#define LOCK_EX 2 /* exclusive lock */
+#define LOCK_NB 4 /* don't block when locking */
+#define LOCK_UN 8 /* unlock */
#endif /* LOCK_SH */
- {
- struct flock l;
- if (code == LOCK_UN)
- l.l_type = F_UNLCK;
- else if (code & LOCK_SH)
- l.l_type = F_RDLCK;
- else if (code & LOCK_EX)
- l.l_type = F_WRLCK;
- else {
- PyErr_SetString(PyExc_ValueError,
- "unrecognized lockf argument");
- return NULL;
- }
- l.l_start = l.l_len = 0;
- if (startobj != NULL) {
+ {
+ struct flock l;
+ if (code == LOCK_UN)
+ l.l_type = F_UNLCK;
+ else if (code & LOCK_SH)
+ l.l_type = F_RDLCK;
+ else if (code & LOCK_EX)
+ l.l_type = F_WRLCK;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "unrecognized lockf argument");
+ return NULL;
+ }
+ l.l_start = l.l_len = 0;
+ if (startobj != NULL) {
#if !defined(HAVE_LARGEFILE_SUPPORT)
- l.l_start = PyLong_AsLong(startobj);
+ l.l_start = PyLong_AsLong(startobj);
#else
- l.l_start = PyLong_Check(startobj) ?
- PyLong_AsLongLong(startobj) :
- PyLong_AsLong(startobj);
-#endif
- if (PyErr_Occurred())
- return NULL;
- }
- if (lenobj != NULL) {
+ l.l_start = PyLong_Check(startobj) ?
+ PyLong_AsLongLong(startobj) :
+ PyLong_AsLong(startobj);
+#endif
+ if (PyErr_Occurred())
+ return NULL;
+ }
+ if (lenobj != NULL) {
#if !defined(HAVE_LARGEFILE_SUPPORT)
- l.l_len = PyLong_AsLong(lenobj);
+ l.l_len = PyLong_AsLong(lenobj);
#else
- l.l_len = PyLong_Check(lenobj) ?
- PyLong_AsLongLong(lenobj) :
- PyLong_AsLong(lenobj);
-#endif
- if (PyErr_Occurred())
- return NULL;
- }
- l.l_whence = whence;
- Py_BEGIN_ALLOW_THREADS
- ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
- Py_END_ALLOW_THREADS
- }
- if (ret < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ l.l_len = PyLong_Check(lenobj) ?
+ PyLong_AsLongLong(lenobj) :
+ PyLong_AsLong(lenobj);
+#endif
+ if (PyErr_Occurred())
+ return NULL;
+ }
+ l.l_whence = whence;
+ Py_BEGIN_ALLOW_THREADS
+ ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l);
+ Py_END_ALLOW_THREADS
+ }
+ if (ret < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
}
@@ -414,11 +414,11 @@ starts. whence is as with fileobj.seek(), specifically:\n\
/* List of functions */
static PyMethodDef fcntl_methods[] = {
- {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
- {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
- {"flock", fcntl_flock, METH_VARARGS, flock_doc},
- {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
- {NULL, NULL} /* sentinel */
+ {"fcntl", fcntl_fcntl, METH_VARARGS, fcntl_doc},
+ {"ioctl", fcntl_ioctl, METH_VARARGS, ioctl_doc},
+ {"flock", fcntl_flock, METH_VARARGS, flock_doc},
+ {"lockf", fcntl_lockf, METH_VARARGS, lockf_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -433,12 +433,12 @@ a file or socket object.");
static int
ins(PyObject* d, char* symbol, long value)
{
- PyObject* v = PyLong_FromLong(value);
- if (!v || PyDict_SetItemString(d, symbol, v) < 0)
- return -1;
+ PyObject* v = PyLong_FromLong(value);
+ if (!v || PyDict_SetItemString(d, symbol, v) < 0)
+ return -1;
- Py_DECREF(v);
- return 0;
+ Py_DECREF(v);
+ return 0;
}
#define INS(x) if (ins(d, #x, (long)x)) return -1
@@ -446,199 +446,199 @@ ins(PyObject* d, char* symbol, long value)
static int
all_ins(PyObject* d)
{
- if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
- if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
- if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
- if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
+ if (ins(d, "LOCK_SH", (long)LOCK_SH)) return -1;
+ if (ins(d, "LOCK_EX", (long)LOCK_EX)) return -1;
+ if (ins(d, "LOCK_NB", (long)LOCK_NB)) return -1;
+ if (ins(d, "LOCK_UN", (long)LOCK_UN)) return -1;
/* GNU extensions, as of glibc 2.2.4 */
#ifdef LOCK_MAND
- if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1;
+ if (ins(d, "LOCK_MAND", (long)LOCK_MAND)) return -1;
#endif
#ifdef LOCK_READ
- if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1;
+ if (ins(d, "LOCK_READ", (long)LOCK_READ)) return -1;
#endif
#ifdef LOCK_WRITE
- if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1;
+ if (ins(d, "LOCK_WRITE", (long)LOCK_WRITE)) return -1;
#endif
#ifdef LOCK_RW
- if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1;
+ if (ins(d, "LOCK_RW", (long)LOCK_RW)) return -1;
#endif
#ifdef F_DUPFD
- if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1;
+ if (ins(d, "F_DUPFD", (long)F_DUPFD)) return -1;
#endif
#ifdef F_GETFD
- if (ins(d, "F_GETFD", (long)F_GETFD)) return -1;
+ if (ins(d, "F_GETFD", (long)F_GETFD)) return -1;
#endif
#ifdef F_SETFD
- if (ins(d, "F_SETFD", (long)F_SETFD)) return -1;
+ if (ins(d, "F_SETFD", (long)F_SETFD)) return -1;
#endif
#ifdef F_GETFL
- if (ins(d, "F_GETFL", (long)F_GETFL)) return -1;
+ if (ins(d, "F_GETFL", (long)F_GETFL)) return -1;
#endif
#ifdef F_SETFL
- if (ins(d, "F_SETFL", (long)F_SETFL)) return -1;
+ if (ins(d, "F_SETFL", (long)F_SETFL)) return -1;
#endif
#ifdef F_GETLK
- if (ins(d, "F_GETLK", (long)F_GETLK)) return -1;
+ if (ins(d, "F_GETLK", (long)F_GETLK)) return -1;
#endif
#ifdef F_SETLK
- if (ins(d, "F_SETLK", (long)F_SETLK)) return -1;
+ if (ins(d, "F_SETLK", (long)F_SETLK)) return -1;
#endif
#ifdef F_SETLKW
- if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1;
+ if (ins(d, "F_SETLKW", (long)F_SETLKW)) return -1;
#endif
#ifdef F_GETOWN
- if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1;
+ if (ins(d, "F_GETOWN", (long)F_GETOWN)) return -1;
#endif
#ifdef F_SETOWN
- if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1;
+ if (ins(d, "F_SETOWN", (long)F_SETOWN)) return -1;
#endif
#ifdef F_GETSIG
- if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1;
+ if (ins(d, "F_GETSIG", (long)F_GETSIG)) return -1;
#endif
#ifdef F_SETSIG
- if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1;
+ if (ins(d, "F_SETSIG", (long)F_SETSIG)) return -1;
#endif
#ifdef F_RDLCK
- if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1;
+ if (ins(d, "F_RDLCK", (long)F_RDLCK)) return -1;
#endif
#ifdef F_WRLCK
- if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1;
+ if (ins(d, "F_WRLCK", (long)F_WRLCK)) return -1;
#endif
#ifdef F_UNLCK
- if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1;
+ if (ins(d, "F_UNLCK", (long)F_UNLCK)) return -1;
#endif
/* LFS constants */
#ifdef F_GETLK64
- if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1;
+ if (ins(d, "F_GETLK64", (long)F_GETLK64)) return -1;
#endif
#ifdef F_SETLK64
- if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1;
+ if (ins(d, "F_SETLK64", (long)F_SETLK64)) return -1;
#endif
#ifdef F_SETLKW64
- if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1;
+ if (ins(d, "F_SETLKW64", (long)F_SETLKW64)) return -1;
#endif
/* GNU extensions, as of glibc 2.2.4. */
#ifdef FASYNC
- if (ins(d, "FASYNC", (long)FASYNC)) return -1;
+ if (ins(d, "FASYNC", (long)FASYNC)) return -1;
#endif
#ifdef F_SETLEASE
- if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1;
+ if (ins(d, "F_SETLEASE", (long)F_SETLEASE)) return -1;
#endif
#ifdef F_GETLEASE
- if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1;
+ if (ins(d, "F_GETLEASE", (long)F_GETLEASE)) return -1;
#endif
#ifdef F_NOTIFY
- if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1;
+ if (ins(d, "F_NOTIFY", (long)F_NOTIFY)) return -1;
#endif
/* Old BSD flock(). */
#ifdef F_EXLCK
- if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1;
+ if (ins(d, "F_EXLCK", (long)F_EXLCK)) return -1;
#endif
#ifdef F_SHLCK
- if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1;
+ if (ins(d, "F_SHLCK", (long)F_SHLCK)) return -1;
#endif
/* OS X (and maybe others) let you tell the storage device to flush to physical media */
#ifdef F_FULLFSYNC
- if (ins(d, "F_FULLFSYNC", (long)F_FULLFSYNC)) return -1;
+ if (ins(d, "F_FULLFSYNC", (long)F_FULLFSYNC)) return -1;
#endif
/* For F_{GET|SET}FL */
#ifdef FD_CLOEXEC
- if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1;
+ if (ins(d, "FD_CLOEXEC", (long)FD_CLOEXEC)) return -1;
#endif
/* For F_NOTIFY */
#ifdef DN_ACCESS
- if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1;
+ if (ins(d, "DN_ACCESS", (long)DN_ACCESS)) return -1;
#endif
#ifdef DN_MODIFY
- if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1;
+ if (ins(d, "DN_MODIFY", (long)DN_MODIFY)) return -1;
#endif
#ifdef DN_CREATE
- if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1;
+ if (ins(d, "DN_CREATE", (long)DN_CREATE)) return -1;
#endif
#ifdef DN_DELETE
- if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1;
+ if (ins(d, "DN_DELETE", (long)DN_DELETE)) return -1;
#endif
#ifdef DN_RENAME
- if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1;
+ if (ins(d, "DN_RENAME", (long)DN_RENAME)) return -1;
#endif
#ifdef DN_ATTRIB
- if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1;
+ if (ins(d, "DN_ATTRIB", (long)DN_ATTRIB)) return -1;
#endif
#ifdef DN_MULTISHOT
- if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1;
+ if (ins(d, "DN_MULTISHOT", (long)DN_MULTISHOT)) return -1;
#endif
#ifdef HAVE_STROPTS_H
- /* Unix 98 guarantees that these are in stropts.h. */
- INS(I_PUSH);
- INS(I_POP);
- INS(I_LOOK);
- INS(I_FLUSH);
- INS(I_FLUSHBAND);
- INS(I_SETSIG);
- INS(I_GETSIG);
- INS(I_FIND);
- INS(I_PEEK);
- INS(I_SRDOPT);
- INS(I_GRDOPT);
- INS(I_NREAD);
- INS(I_FDINSERT);
- INS(I_STR);
- INS(I_SWROPT);
+ /* Unix 98 guarantees that these are in stropts.h. */
+ INS(I_PUSH);
+ INS(I_POP);
+ INS(I_LOOK);
+ INS(I_FLUSH);
+ INS(I_FLUSHBAND);
+ INS(I_SETSIG);
+ INS(I_GETSIG);
+ INS(I_FIND);
+ INS(I_PEEK);
+ INS(I_SRDOPT);
+ INS(I_GRDOPT);
+ INS(I_NREAD);
+ INS(I_FDINSERT);
+ INS(I_STR);
+ INS(I_SWROPT);
#ifdef I_GWROPT
- /* despite the comment above, old-ish glibcs miss a couple... */
- INS(I_GWROPT);
-#endif
- INS(I_SENDFD);
- INS(I_RECVFD);
- INS(I_LIST);
- INS(I_ATMARK);
- INS(I_CKBAND);
- INS(I_GETBAND);
- INS(I_CANPUT);
- INS(I_SETCLTIME);
+ /* despite the comment above, old-ish glibcs miss a couple... */
+ INS(I_GWROPT);
+#endif
+ INS(I_SENDFD);
+ INS(I_RECVFD);
+ INS(I_LIST);
+ INS(I_ATMARK);
+ INS(I_CKBAND);
+ INS(I_GETBAND);
+ INS(I_CANPUT);
+ INS(I_SETCLTIME);
#ifdef I_GETCLTIME
- INS(I_GETCLTIME);
+ INS(I_GETCLTIME);
#endif
- INS(I_LINK);
- INS(I_UNLINK);
- INS(I_PLINK);
- INS(I_PUNLINK);
+ INS(I_LINK);
+ INS(I_UNLINK);
+ INS(I_PLINK);
+ INS(I_PUNLINK);
#endif
-
- return 0;
+
+ return 0;
}
static struct PyModuleDef fcntlmodule = {
- PyModuleDef_HEAD_INIT,
- "fcntl",
- module_doc,
- -1,
- fcntl_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "fcntl",
+ module_doc,
+ -1,
+ fcntl_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_fcntl(void)
{
- PyObject *m, *d;
+ PyObject *m, *d;
- /* Create the module and add the functions and documentation */
- m = PyModule_Create(&fcntlmodule);
- if (m == NULL)
- return NULL;
+ /* Create the module and add the functions and documentation */
+ m = PyModule_Create(&fcntlmodule);
+ if (m == NULL)
+ return NULL;
- /* Add some symbolic constants to the module */
- d = PyModule_GetDict(m);
- all_ins(d);
- return m;
+ /* Add some symbolic constants to the module */
+ d = PyModule_GetDict(m);
+ all_ins(d);
+ return m;
}
diff --git a/Modules/fpectlmodule.c b/Modules/fpectlmodule.c
index 6e6a616..1bb51cf 100644
--- a/Modules/fpectlmodule.c
+++ b/Modules/fpectlmodule.c
@@ -1,6 +1,6 @@
/*
- ---------------------------------------------------------------------
- / Copyright (c) 1996. \
+ ---------------------------------------------------------------------
+ / Copyright (c) 1996. \
| The Regents of the University of California. |
| All rights reserved. |
| |
@@ -32,12 +32,12 @@
| opinions of authors expressed herein do not necessarily state or |
| reflect those of the United States Government or the University |
| of California, and shall not be used for advertising or product |
- \ endorsement purposes. /
- ---------------------------------------------------------------------
+ \ endorsement purposes. /
+ ---------------------------------------------------------------------
*/
/*
- Floating point exception control module.
+ Floating point exception control module.
This Python module provides bare-bones control over floating point
units from several hardware manufacturers. Specifically, it allows
@@ -96,8 +96,8 @@ static PyObject *turnon_sigfpe (PyObject *self,PyObject *args);
static PyObject *turnoff_sigfpe (PyObject *self,PyObject *args);
static PyMethodDef fpectl_methods[] = {
- {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS},
- {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS},
+ {"turnon_sigfpe", (PyCFunction) turnon_sigfpe, METH_VARARGS},
+ {"turnoff_sigfpe", (PyCFunction) turnoff_sigfpe, METH_VARARGS},
{0,0}
};
@@ -122,19 +122,19 @@ static void fpe_reset(Sigfunc *handler)
* My usage doesn't follow the man page exactly. Maybe somebody
* else can explain handle_sigfpes to me....
* cc -c -I/usr/local/python/include fpectlmodule.c
- * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
+ * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe
*/
#include <sigfpe.h>
typedef void user_routine (unsigned[5], int[2]);
typedef void abort_routine (unsigned long);
handle_sigfpes(_OFF, 0,
- (user_routine *)0,
- _TURN_OFF_HANDLER_ON_ERROR,
- NULL);
+ (user_routine *)0,
+ _TURN_OFF_HANDLER_ON_ERROR,
+ NULL);
handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,
- (user_routine *)0,
- _ABORT_ON_ERROR,
- NULL);
+ (user_routine *)0,
+ _ABORT_ON_ERROR,
+ NULL);
PyOS_setsig(SIGFPE, handler);
/*-- SunOS and Solaris ----------------------------------------------------*/
@@ -195,16 +195,16 @@ static void fpe_reset(Sigfunc *handler)
/*-- DEC ALPHA VMS --------------------------------------------------------*/
#elif defined(__ALPHA) && defined(__VMS)
- IEEE clrmsk;
- IEEE setmsk;
- clrmsk.ieee$q_flags =
- IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
- IEEE$M_MAP_UMZ;
- setmsk.ieee$q_flags =
- IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE |
- IEEE$M_TRAP_ENABLE_OVF;
- sys$ieee_set_fp_control(&clrmsk, &setmsk, 0);
- PyOS_setsig(SIGFPE, handler);
+ IEEE clrmsk;
+ IEEE setmsk;
+ clrmsk.ieee$q_flags =
+ IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
+ IEEE$M_MAP_UMZ;
+ setmsk.ieee$q_flags =
+ IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE |
+ IEEE$M_TRAP_ENABLE_OVF;
+ sys$ieee_set_fp_control(&clrmsk, &setmsk, 0);
+ PyOS_setsig(SIGFPE, handler);
/*-- HP IA64 VMS --------------------------------------------------------*/
#elif defined(__ia64) && defined(__VMS)
@@ -263,13 +263,13 @@ static PyObject *turnoff_sigfpe(PyObject *self,PyObject *args)
fpresetsticky(fpgetsticky());
fpsetmask(0);
#elif defined(__VMS)
- IEEE clrmsk;
- clrmsk.ieee$q_flags =
- IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
- IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV |
- IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF |
- IEEE$M_INHERIT;
- sys$ieee_set_fp_control(&clrmsk, 0, 0);
+ IEEE clrmsk;
+ clrmsk.ieee$q_flags =
+ IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE |
+ IEEE$M_MAP_UMZ | IEEE$M_TRAP_ENABLE_INV |
+ IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF |
+ IEEE$M_INHERIT;
+ sys$ieee_set_fp_control(&clrmsk, 0, 0);
#else
fputs("Operation not implemented\n", stderr);
#endif
@@ -288,15 +288,15 @@ static void sigfpe_handler(int signo)
}
static struct PyModuleDef fpectlmodule = {
- PyModuleDef_HEAD_INIT,
- "fpectl",
- NULL,
- -1,
- fpectl_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "fpectl",
+ NULL,
+ -1,
+ fpectl_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC PyInit_fpectl(void)
@@ -304,11 +304,11 @@ PyMODINIT_FUNC PyInit_fpectl(void)
PyObject *m, *d;
m = PyModule_Create(&fpectlmodule);
if (m == NULL)
- return NULL;
+ return NULL;
d = PyModule_GetDict(m);
fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
if (fpe_error != NULL)
- PyDict_SetItemString(d, "error", fpe_error);
+ PyDict_SetItemString(d, "error", fpe_error);
return m;
}
diff --git a/Modules/fpetestmodule.c b/Modules/fpetestmodule.c
index 64acd3d..5b6c220 100644
--- a/Modules/fpetestmodule.c
+++ b/Modules/fpetestmodule.c
@@ -1,6 +1,6 @@
/*
- ---------------------------------------------------------------------
- / Copyright (c) 1996. \
+ ---------------------------------------------------------------------
+ / Copyright (c) 1996. \
| The Regents of the University of California. |
| All rights reserved. |
| |
@@ -32,12 +32,12 @@
| opinions of authors expressed herein do not necessarily state or |
| reflect those of the United States Government or the University |
| of California, and shall not be used for advertising or product |
- \ endorsement purposes. /
- ---------------------------------------------------------------------
+ \ endorsement purposes. /
+ ---------------------------------------------------------------------
*/
/*
- Floating point exception test module.
+ Floating point exception test module.
*/
@@ -55,7 +55,7 @@ static double nest3(double);
static void printerr(double);
static PyMethodDef fpetest_methods[] = {
- {"test", (PyCFunction) test, METH_VARARGS},
+ {"test", (PyCFunction) test, METH_VARARGS},
{0,0}
};
@@ -174,15 +174,15 @@ static double overflow(double b)
}
static struct PyModuleDef fpetestmodule = {
- PyModuleDef_HEAD_INIT,
- "fpetest",
- NULL,
- -1,
- fpetest_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "fpetest",
+ NULL,
+ -1,
+ fpetest_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC PyInit_fpetest(void)
@@ -191,10 +191,10 @@ PyMODINIT_FUNC PyInit_fpetest(void)
m = PyModule_Create(&fpetestmodule);
if (m == NULL)
- return NULL;
+ return NULL;
d = PyModule_GetDict(m);
fpe_error = PyErr_NewException("fpetest.error", NULL, NULL);
if (fpe_error != NULL)
- PyDict_SetItemString(d, "error", fpe_error);
+ PyDict_SetItemString(d, "error", fpe_error);
return m;
}
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 3058724..3717a27 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -24,7 +24,7 @@
*/
#include "Python.h"
-#include "frameobject.h" /* for PyFrame_ClearFreeList */
+#include "frameobject.h" /* for PyFrame_ClearFreeList */
/* Get an object's GC head */
#define AS_GC(o) ((PyGC_Head *)(o)-1)
@@ -35,10 +35,10 @@
/*** Global GC state ***/
struct gc_generation {
- PyGC_Head head;
- int threshold; /* collection threshold */
- int count; /* count of allocations or collections of younger
- generations */
+ PyGC_Head head;
+ int threshold; /* collection threshold */
+ int count; /* count of allocations or collections of younger
+ generations */
};
#define NUM_GENERATIONS 3
@@ -46,10 +46,10 @@ struct gc_generation {
/* linked lists of container objects */
static struct gc_generation generations[NUM_GENERATIONS] = {
- /* PyGC_Head, threshold, count */
- {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
- {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
- {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
+ /* PyGC_Head, threshold, count */
+ {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
+ {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
+ {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
};
PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
@@ -91,7 +91,7 @@ static Py_ssize_t long_lived_pending = 0;
In addition to the various configurable thresholds, we only trigger a
full collection if the ratio
- long_lived_pending / long_lived_total
+ long_lived_pending / long_lived_total
is above a given value (hardwired to 25%).
The reason is that, while "non-full" collections (i.e., collections of
@@ -113,18 +113,18 @@ static Py_ssize_t long_lived_pending = 0;
This heuristic was suggested by Martin von Löwis on python-dev in
June 2008. His original analysis and proposal can be found at:
- http://mail.python.org/pipermail/python-dev/2008-June/080579.html
+ http://mail.python.org/pipermail/python-dev/2008-June/080579.html
*/
/* set for debugging information */
-#define DEBUG_STATS (1<<0) /* print collection statistics */
-#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
-#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
-#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
-#define DEBUG_LEAK DEBUG_COLLECTABLE | \
- DEBUG_UNCOLLECTABLE | \
- DEBUG_SAVEALL
+#define DEBUG_STATS (1<<0) /* print collection statistics */
+#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
+#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
+#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
+#define DEBUG_LEAK DEBUG_COLLECTABLE | \
+ DEBUG_UNCOLLECTABLE | \
+ DEBUG_SAVEALL
static int debug;
static PyObject *tmod = NULL;
@@ -167,28 +167,28 @@ GC_TENTATIVELY_UNREACHABLE
it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.
----------------------------------------------------------------------------
*/
-#define GC_UNTRACKED _PyGC_REFS_UNTRACKED
-#define GC_REACHABLE _PyGC_REFS_REACHABLE
-#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
+#define GC_UNTRACKED _PyGC_REFS_UNTRACKED
+#define GC_REACHABLE _PyGC_REFS_REACHABLE
+#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)
#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
#define IS_TENTATIVELY_UNREACHABLE(o) ( \
- (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
+ (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
/*** list functions ***/
static void
gc_list_init(PyGC_Head *list)
{
- list->gc.gc_prev = list;
- list->gc.gc_next = list;
+ list->gc.gc_prev = list;
+ list->gc.gc_next = list;
}
static int
gc_list_is_empty(PyGC_Head *list)
{
- return (list->gc.gc_next == list);
+ return (list->gc.gc_next == list);
}
#if 0
@@ -197,10 +197,10 @@ gc_list_is_empty(PyGC_Head *list)
static void
gc_list_append(PyGC_Head *node, PyGC_Head *list)
{
- node->gc.gc_next = list;
- node->gc.gc_prev = list->gc.gc_prev;
- node->gc.gc_prev->gc.gc_next = node;
- list->gc.gc_prev = node;
+ node->gc.gc_next = list;
+ node->gc.gc_prev = list->gc.gc_prev;
+ node->gc.gc_prev->gc.gc_next = node;
+ list->gc.gc_prev = node;
}
#endif
@@ -208,9 +208,9 @@ gc_list_append(PyGC_Head *node, PyGC_Head *list)
static void
gc_list_remove(PyGC_Head *node)
{
- node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
- node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
- node->gc.gc_next = NULL; /* object is not currently tracked */
+ node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
+ node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
+ node->gc.gc_next = NULL; /* object is not currently tracked */
}
/* Move `node` from the gc list it's currently in (which is not explicitly
@@ -220,43 +220,43 @@ gc_list_remove(PyGC_Head *node)
static void
gc_list_move(PyGC_Head *node, PyGC_Head *list)
{
- PyGC_Head *new_prev;
- PyGC_Head *current_prev = node->gc.gc_prev;
- PyGC_Head *current_next = node->gc.gc_next;
- /* Unlink from current list. */
- current_prev->gc.gc_next = current_next;
- current_next->gc.gc_prev = current_prev;
- /* Relink at end of new list. */
- new_prev = node->gc.gc_prev = list->gc.gc_prev;
- new_prev->gc.gc_next = list->gc.gc_prev = node;
- node->gc.gc_next = list;
+ PyGC_Head *new_prev;
+ PyGC_Head *current_prev = node->gc.gc_prev;
+ PyGC_Head *current_next = node->gc.gc_next;
+ /* Unlink from current list. */
+ current_prev->gc.gc_next = current_next;
+ current_next->gc.gc_prev = current_prev;
+ /* Relink at end of new list. */
+ new_prev = node->gc.gc_prev = list->gc.gc_prev;
+ new_prev->gc.gc_next = list->gc.gc_prev = node;
+ node->gc.gc_next = list;
}
/* append list `from` onto list `to`; `from` becomes an empty list */
static void
gc_list_merge(PyGC_Head *from, PyGC_Head *to)
{
- PyGC_Head *tail;
- assert(from != to);
- if (!gc_list_is_empty(from)) {
- tail = to->gc.gc_prev;
- tail->gc.gc_next = from->gc.gc_next;
- tail->gc.gc_next->gc.gc_prev = tail;
- to->gc.gc_prev = from->gc.gc_prev;
- to->gc.gc_prev->gc.gc_next = to;
- }
- gc_list_init(from);
+ PyGC_Head *tail;
+ assert(from != to);
+ if (!gc_list_is_empty(from)) {
+ tail = to->gc.gc_prev;
+ tail->gc.gc_next = from->gc.gc_next;
+ tail->gc.gc_next->gc.gc_prev = tail;
+ to->gc.gc_prev = from->gc.gc_prev;
+ to->gc.gc_prev->gc.gc_next = to;
+ }
+ gc_list_init(from);
}
static Py_ssize_t
gc_list_size(PyGC_Head *list)
{
- PyGC_Head *gc;
- Py_ssize_t n = 0;
- for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
- n++;
- }
- return n;
+ PyGC_Head *gc;
+ Py_ssize_t n = 0;
+ for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
+ n++;
+ }
+ return n;
}
/* Append objects in a GC list to a Python list.
@@ -265,16 +265,16 @@ gc_list_size(PyGC_Head *list)
static int
append_objects(PyObject *py_list, PyGC_Head *gc_list)
{
- PyGC_Head *gc;
- for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
- PyObject *op = FROM_GC(gc);
- if (op != py_list) {
- if (PyList_Append(py_list, op)) {
- return -1; /* exception */
- }
- }
- }
- return 0;
+ PyGC_Head *gc;
+ for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
+ PyObject *op = FROM_GC(gc);
+ if (op != py_list) {
+ if (PyList_Append(py_list, op)) {
+ return -1; /* exception */
+ }
+ }
+ }
+ return 0;
}
/*** end of list stuff ***/
@@ -287,48 +287,48 @@ append_objects(PyObject *py_list, PyGC_Head *gc_list)
static void
update_refs(PyGC_Head *containers)
{
- PyGC_Head *gc = containers->gc.gc_next;
- for (; gc != containers; gc = gc->gc.gc_next) {
- assert(gc->gc.gc_refs == GC_REACHABLE);
- gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
- /* Python's cyclic gc should never see an incoming refcount
- * of 0: if something decref'ed to 0, it should have been
- * deallocated immediately at that time.
- * Possible cause (if the assert triggers): a tp_dealloc
- * routine left a gc-aware object tracked during its teardown
- * phase, and did something-- or allowed something to happen --
- * that called back into Python. gc can trigger then, and may
- * see the still-tracked dying object. Before this assert
- * was added, such mistakes went on to allow gc to try to
- * delete the object again. In a debug build, that caused
- * a mysterious segfault, when _Py_ForgetReference tried
- * to remove the object from the doubly-linked list of all
- * objects a second time. In a release build, an actual
- * double deallocation occurred, which leads to corruption
- * of the allocator's internal bookkeeping pointers. That's
- * so serious that maybe this should be a release-build
- * check instead of an assert?
- */
- assert(gc->gc.gc_refs != 0);
- }
+ PyGC_Head *gc = containers->gc.gc_next;
+ for (; gc != containers; gc = gc->gc.gc_next) {
+ assert(gc->gc.gc_refs == GC_REACHABLE);
+ gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
+ /* Python's cyclic gc should never see an incoming refcount
+ * of 0: if something decref'ed to 0, it should have been
+ * deallocated immediately at that time.
+ * Possible cause (if the assert triggers): a tp_dealloc
+ * routine left a gc-aware object tracked during its teardown
+ * phase, and did something-- or allowed something to happen --
+ * that called back into Python. gc can trigger then, and may
+ * see the still-tracked dying object. Before this assert
+ * was added, such mistakes went on to allow gc to try to
+ * delete the object again. In a debug build, that caused
+ * a mysterious segfault, when _Py_ForgetReference tried
+ * to remove the object from the doubly-linked list of all
+ * objects a second time. In a release build, an actual
+ * double deallocation occurred, which leads to corruption
+ * of the allocator's internal bookkeeping pointers. That's
+ * so serious that maybe this should be a release-build
+ * check instead of an assert?
+ */
+ assert(gc->gc.gc_refs != 0);
+ }
}
/* A traversal callback for subtract_refs. */
static int
visit_decref(PyObject *op, void *data)
{
- assert(op != NULL);
- if (PyObject_IS_GC(op)) {
- PyGC_Head *gc = AS_GC(op);
- /* We're only interested in gc_refs for objects in the
- * generation being collected, which can be recognized
- * because only they have positive gc_refs.
- */
- assert(gc->gc.gc_refs != 0); /* else refcount was too small */
- if (gc->gc.gc_refs > 0)
- gc->gc.gc_refs--;
- }
- return 0;
+ assert(op != NULL);
+ if (PyObject_IS_GC(op)) {
+ PyGC_Head *gc = AS_GC(op);
+ /* We're only interested in gc_refs for objects in the
+ * generation being collected, which can be recognized
+ * because only they have positive gc_refs.
+ */
+ assert(gc->gc.gc_refs != 0); /* else refcount was too small */
+ if (gc->gc.gc_refs > 0)
+ gc->gc.gc_refs--;
+ }
+ return 0;
}
/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
@@ -339,57 +339,57 @@ visit_decref(PyObject *op, void *data)
static void
subtract_refs(PyGC_Head *containers)
{
- traverseproc traverse;
- PyGC_Head *gc = containers->gc.gc_next;
- for (; gc != containers; gc=gc->gc.gc_next) {
- traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
- (void) traverse(FROM_GC(gc),
- (visitproc)visit_decref,
- NULL);
- }
+ traverseproc traverse;
+ PyGC_Head *gc = containers->gc.gc_next;
+ for (; gc != containers; gc=gc->gc.gc_next) {
+ traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
+ (void) traverse(FROM_GC(gc),
+ (visitproc)visit_decref,
+ NULL);
+ }
}
/* A traversal callback for move_unreachable. */
static int
visit_reachable(PyObject *op, PyGC_Head *reachable)
{
- if (PyObject_IS_GC(op)) {
- PyGC_Head *gc = AS_GC(op);
- const Py_ssize_t gc_refs = gc->gc.gc_refs;
-
- if (gc_refs == 0) {
- /* This is in move_unreachable's 'young' list, but
- * the traversal hasn't yet gotten to it. All
- * we need to do is tell move_unreachable that it's
- * reachable.
- */
- gc->gc.gc_refs = 1;
- }
- else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
- /* This had gc_refs = 0 when move_unreachable got
- * to it, but turns out it's reachable after all.
- * Move it back to move_unreachable's 'young' list,
- * and move_unreachable will eventually get to it
- * again.
- */
- gc_list_move(gc, reachable);
- gc->gc.gc_refs = 1;
- }
- /* Else there's nothing to do.
- * If gc_refs > 0, it must be in move_unreachable's 'young'
- * list, and move_unreachable will eventually get to it.
- * If gc_refs == GC_REACHABLE, it's either in some other
- * generation so we don't care about it, or move_unreachable
- * already dealt with it.
- * If gc_refs == GC_UNTRACKED, it must be ignored.
- */
- else {
- assert(gc_refs > 0
- || gc_refs == GC_REACHABLE
- || gc_refs == GC_UNTRACKED);
- }
- }
- return 0;
+ if (PyObject_IS_GC(op)) {
+ PyGC_Head *gc = AS_GC(op);
+ const Py_ssize_t gc_refs = gc->gc.gc_refs;
+
+ if (gc_refs == 0) {
+ /* This is in move_unreachable's 'young' list, but
+ * the traversal hasn't yet gotten to it. All
+ * we need to do is tell move_unreachable that it's
+ * reachable.
+ */
+ gc->gc.gc_refs = 1;
+ }
+ else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
+ /* This had gc_refs = 0 when move_unreachable got
+ * to it, but turns out it's reachable after all.
+ * Move it back to move_unreachable's 'young' list,
+ * and move_unreachable will eventually get to it
+ * again.
+ */
+ gc_list_move(gc, reachable);
+ gc->gc.gc_refs = 1;
+ }
+ /* Else there's nothing to do.
+ * If gc_refs > 0, it must be in move_unreachable's 'young'
+ * list, and move_unreachable will eventually get to it.
+ * If gc_refs == GC_REACHABLE, it's either in some other
+ * generation so we don't care about it, or move_unreachable
+ * already dealt with it.
+ * If gc_refs == GC_UNTRACKED, it must be ignored.
+ */
+ else {
+ assert(gc_refs > 0
+ || gc_refs == GC_REACHABLE
+ || gc_refs == GC_UNTRACKED);
+ }
+ }
+ return 0;
}
/* Move the unreachable objects from young to unreachable. After this,
@@ -403,68 +403,68 @@ visit_reachable(PyObject *op, PyGC_Head *reachable)
static void
move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
{
- PyGC_Head *gc = young->gc.gc_next;
-
- /* Invariants: all objects "to the left" of us in young have gc_refs
- * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
- * from outside the young list as it was at entry. All other objects
- * from the original young "to the left" of us are in unreachable now,
- * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
- * left of us in 'young' now have been scanned, and no objects here
- * or to the right have been scanned yet.
- */
-
- while (gc != young) {
- PyGC_Head *next;
-
- if (gc->gc.gc_refs) {
- /* gc is definitely reachable from outside the
- * original 'young'. Mark it as such, and traverse
- * its pointers to find any other objects that may
- * be directly reachable from it. Note that the
- * call to tp_traverse may append objects to young,
- * so we have to wait until it returns to determine
- * the next object to visit.
- */
- PyObject *op = FROM_GC(gc);
- traverseproc traverse = Py_TYPE(op)->tp_traverse;
- assert(gc->gc.gc_refs > 0);
- gc->gc.gc_refs = GC_REACHABLE;
- (void) traverse(op,
- (visitproc)visit_reachable,
- (void *)young);
- next = gc->gc.gc_next;
- if (PyTuple_CheckExact(op)) {
- _PyTuple_MaybeUntrack(op);
- }
- else if (PyDict_CheckExact(op)) {
- _PyDict_MaybeUntrack(op);
- }
- }
- else {
- /* This *may* be unreachable. To make progress,
- * assume it is. gc isn't directly reachable from
- * any object we've already traversed, but may be
- * reachable from an object we haven't gotten to yet.
- * visit_reachable will eventually move gc back into
- * young if that's so, and we'll see it again.
- */
- next = gc->gc.gc_next;
- gc_list_move(gc, unreachable);
- gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
- }
- gc = next;
- }
+ PyGC_Head *gc = young->gc.gc_next;
+
+ /* Invariants: all objects "to the left" of us in young have gc_refs
+ * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
+ * from outside the young list as it was at entry. All other objects
+ * from the original young "to the left" of us are in unreachable now,
+ * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
+ * left of us in 'young' now have been scanned, and no objects here
+ * or to the right have been scanned yet.
+ */
+
+ while (gc != young) {
+ PyGC_Head *next;
+
+ if (gc->gc.gc_refs) {
+ /* gc is definitely reachable from outside the
+ * original 'young'. Mark it as such, and traverse
+ * its pointers to find any other objects that may
+ * be directly reachable from it. Note that the
+ * call to tp_traverse may append objects to young,
+ * so we have to wait until it returns to determine
+ * the next object to visit.
+ */
+ PyObject *op = FROM_GC(gc);
+ traverseproc traverse = Py_TYPE(op)->tp_traverse;
+ assert(gc->gc.gc_refs > 0);
+ gc->gc.gc_refs = GC_REACHABLE;
+ (void) traverse(op,
+ (visitproc)visit_reachable,
+ (void *)young);
+ next = gc->gc.gc_next;
+ if (PyTuple_CheckExact(op)) {
+ _PyTuple_MaybeUntrack(op);
+ }
+ else if (PyDict_CheckExact(op)) {
+ _PyDict_MaybeUntrack(op);
+ }
+ }
+ else {
+ /* This *may* be unreachable. To make progress,
+ * assume it is. gc isn't directly reachable from
+ * any object we've already traversed, but may be
+ * reachable from an object we haven't gotten to yet.
+ * visit_reachable will eventually move gc back into
+ * young if that's so, and we'll see it again.
+ */
+ next = gc->gc.gc_next;
+ gc_list_move(gc, unreachable);
+ gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
+ }
+ gc = next;
+ }
}
/* Return true if object has a finalization method. */
static int
has_finalizer(PyObject *op)
{
- if (PyGen_CheckExact(op))
- return PyGen_NeedsFinalizing((PyGenObject *)op);
- else
- return op->ob_type->tp_del != NULL;
+ if (PyGen_CheckExact(op))
+ return PyGen_NeedsFinalizing((PyGenObject *)op);
+ else
+ return op->ob_type->tp_del != NULL;
}
/* Move the objects in unreachable with __del__ methods into `finalizers`.
@@ -474,37 +474,37 @@ has_finalizer(PyObject *op)
static void
move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
{
- PyGC_Head *gc;
- PyGC_Head *next;
-
- /* March over unreachable. Move objects with finalizers into
- * `finalizers`.
- */
- for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
- PyObject *op = FROM_GC(gc);
-
- assert(IS_TENTATIVELY_UNREACHABLE(op));
- next = gc->gc.gc_next;
-
- if (has_finalizer(op)) {
- gc_list_move(gc, finalizers);
- gc->gc.gc_refs = GC_REACHABLE;
- }
- }
+ PyGC_Head *gc;
+ PyGC_Head *next;
+
+ /* March over unreachable. Move objects with finalizers into
+ * `finalizers`.
+ */
+ for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
+ PyObject *op = FROM_GC(gc);
+
+ assert(IS_TENTATIVELY_UNREACHABLE(op));
+ next = gc->gc.gc_next;
+
+ if (has_finalizer(op)) {
+ gc_list_move(gc, finalizers);
+ gc->gc.gc_refs = GC_REACHABLE;
+ }
+ }
}
/* A traversal callback for move_finalizer_reachable. */
static int
visit_move(PyObject *op, PyGC_Head *tolist)
{
- if (PyObject_IS_GC(op)) {
- if (IS_TENTATIVELY_UNREACHABLE(op)) {
- PyGC_Head *gc = AS_GC(op);
- gc_list_move(gc, tolist);
- gc->gc.gc_refs = GC_REACHABLE;
- }
- }
- return 0;
+ if (PyObject_IS_GC(op)) {
+ if (IS_TENTATIVELY_UNREACHABLE(op)) {
+ PyGC_Head *gc = AS_GC(op);
+ gc_list_move(gc, tolist);
+ gc->gc.gc_refs = GC_REACHABLE;
+ }
+ }
+ return 0;
}
/* Move objects that are reachable from finalizers, from the unreachable set
@@ -513,15 +513,15 @@ visit_move(PyObject *op, PyGC_Head *tolist)
static void
move_finalizer_reachable(PyGC_Head *finalizers)
{
- traverseproc traverse;
- PyGC_Head *gc = finalizers->gc.gc_next;
- for (; gc != finalizers; gc = gc->gc.gc_next) {
- /* Note that the finalizers list may grow during this. */
- traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
- (void) traverse(FROM_GC(gc),
- (visitproc)visit_move,
- (void *)finalizers);
- }
+ traverseproc traverse;
+ PyGC_Head *gc = finalizers->gc.gc_next;
+ for (; gc != finalizers; gc = gc->gc.gc_next) {
+ /* Note that the finalizers list may grow during this. */
+ traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
+ (void) traverse(FROM_GC(gc),
+ (visitproc)visit_move,
+ (void *)finalizers);
+ }
}
/* Clear all weakrefs to unreachable objects, and if such a weakref has a
@@ -538,150 +538,150 @@ move_finalizer_reachable(PyGC_Head *finalizers)
static int
handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
{
- PyGC_Head *gc;
- PyObject *op; /* generally FROM_GC(gc) */
- PyWeakReference *wr; /* generally a cast of op */
- PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
- PyGC_Head *next;
- int num_freed = 0;
-
- gc_list_init(&wrcb_to_call);
-
- /* Clear all weakrefs to the objects in unreachable. If such a weakref
- * also has a callback, move it into `wrcb_to_call` if the callback
- * needs to be invoked. Note that we cannot invoke any callbacks until
- * all weakrefs to unreachable objects are cleared, lest the callback
- * resurrect an unreachable object via a still-active weakref. We
- * make another pass over wrcb_to_call, invoking callbacks, after this
- * pass completes.
- */
- for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
- PyWeakReference **wrlist;
-
- op = FROM_GC(gc);
- assert(IS_TENTATIVELY_UNREACHABLE(op));
- next = gc->gc.gc_next;
-
- if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
- continue;
-
- /* It supports weakrefs. Does it have any? */
- wrlist = (PyWeakReference **)
- PyObject_GET_WEAKREFS_LISTPTR(op);
-
- /* `op` may have some weakrefs. March over the list, clear
- * all the weakrefs, and move the weakrefs with callbacks
- * that must be called into wrcb_to_call.
- */
- for (wr = *wrlist; wr != NULL; wr = *wrlist) {
- PyGC_Head *wrasgc; /* AS_GC(wr) */
-
- /* _PyWeakref_ClearRef clears the weakref but leaves
- * the callback pointer intact. Obscure: it also
- * changes *wrlist.
- */
- assert(wr->wr_object == op);
- _PyWeakref_ClearRef(wr);
- assert(wr->wr_object == Py_None);
- if (wr->wr_callback == NULL)
- continue; /* no callback */
-
- /* Headache time. `op` is going away, and is weakly referenced by
- * `wr`, which has a callback. Should the callback be invoked? If wr
- * is also trash, no:
- *
- * 1. There's no need to call it. The object and the weakref are
- * both going away, so it's legitimate to pretend the weakref is
- * going away first. The user has to ensure a weakref outlives its
- * referent if they want a guarantee that the wr callback will get
- * invoked.
- *
- * 2. It may be catastrophic to call it. If the callback is also in
- * cyclic trash (CT), then although the CT is unreachable from
- * outside the current generation, CT may be reachable from the
- * callback. Then the callback could resurrect insane objects.
- *
- * Since the callback is never needed and may be unsafe in this case,
- * wr is simply left in the unreachable set. Note that because we
- * already called _PyWeakref_ClearRef(wr), its callback will never
- * trigger.
- *
- * OTOH, if wr isn't part of CT, we should invoke the callback: the
- * weakref outlived the trash. Note that since wr isn't CT in this
- * case, its callback can't be CT either -- wr acted as an external
- * root to this generation, and therefore its callback did too. So
- * nothing in CT is reachable from the callback either, so it's hard
- * to imagine how calling it later could create a problem for us. wr
- * is moved to wrcb_to_call in this case.
- */
- if (IS_TENTATIVELY_UNREACHABLE(wr))
- continue;
- assert(IS_REACHABLE(wr));
-
- /* Create a new reference so that wr can't go away
- * before we can process it again.
- */
- Py_INCREF(wr);
-
- /* Move wr to wrcb_to_call, for the next pass. */
- wrasgc = AS_GC(wr);
- assert(wrasgc != next); /* wrasgc is reachable, but
- next isn't, so they can't
- be the same */
- gc_list_move(wrasgc, &wrcb_to_call);
- }
- }
-
- /* Invoke the callbacks we decided to honor. It's safe to invoke them
- * because they can't reference unreachable objects.
- */
- while (! gc_list_is_empty(&wrcb_to_call)) {
- PyObject *temp;
- PyObject *callback;
-
- gc = wrcb_to_call.gc.gc_next;
- op = FROM_GC(gc);
- assert(IS_REACHABLE(op));
- assert(PyWeakref_Check(op));
- wr = (PyWeakReference *)op;
- callback = wr->wr_callback;
- assert(callback != NULL);
-
- /* copy-paste of weakrefobject.c's handle_callback() */
- temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
- if (temp == NULL)
- PyErr_WriteUnraisable(callback);
- else
- Py_DECREF(temp);
-
- /* Give up the reference we created in the first pass. When
- * op's refcount hits 0 (which it may or may not do right now),
- * op's tp_dealloc will decref op->wr_callback too. Note
- * that the refcount probably will hit 0 now, and because this
- * weakref was reachable to begin with, gc didn't already
- * add it to its count of freed objects. Example: a reachable
- * weak value dict maps some key to this reachable weakref.
- * The callback removes this key->weakref mapping from the
- * dict, leaving no other references to the weakref (excepting
- * ours).
- */
- Py_DECREF(op);
- if (wrcb_to_call.gc.gc_next == gc) {
- /* object is still alive -- move it */
- gc_list_move(gc, old);
- }
- else
- ++num_freed;
- }
-
- return num_freed;
+ PyGC_Head *gc;
+ PyObject *op; /* generally FROM_GC(gc) */
+ PyWeakReference *wr; /* generally a cast of op */
+ PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
+ PyGC_Head *next;
+ int num_freed = 0;
+
+ gc_list_init(&wrcb_to_call);
+
+ /* Clear all weakrefs to the objects in unreachable. If such a weakref
+ * also has a callback, move it into `wrcb_to_call` if the callback
+ * needs to be invoked. Note that we cannot invoke any callbacks until
+ * all weakrefs to unreachable objects are cleared, lest the callback
+ * resurrect an unreachable object via a still-active weakref. We
+ * make another pass over wrcb_to_call, invoking callbacks, after this
+ * pass completes.
+ */
+ for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
+ PyWeakReference **wrlist;
+
+ op = FROM_GC(gc);
+ assert(IS_TENTATIVELY_UNREACHABLE(op));
+ next = gc->gc.gc_next;
+
+ if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
+ continue;
+
+ /* It supports weakrefs. Does it have any? */
+ wrlist = (PyWeakReference **)
+ PyObject_GET_WEAKREFS_LISTPTR(op);
+
+ /* `op` may have some weakrefs. March over the list, clear
+ * all the weakrefs, and move the weakrefs with callbacks
+ * that must be called into wrcb_to_call.
+ */
+ for (wr = *wrlist; wr != NULL; wr = *wrlist) {
+ PyGC_Head *wrasgc; /* AS_GC(wr) */
+
+ /* _PyWeakref_ClearRef clears the weakref but leaves
+ * the callback pointer intact. Obscure: it also
+ * changes *wrlist.
+ */
+ assert(wr->wr_object == op);
+ _PyWeakref_ClearRef(wr);
+ assert(wr->wr_object == Py_None);
+ if (wr->wr_callback == NULL)
+ continue; /* no callback */
+
+ /* Headache time. `op` is going away, and is weakly referenced by
+ * `wr`, which has a callback. Should the callback be invoked? If wr
+ * is also trash, no:
+ *
+ * 1. There's no need to call it. The object and the weakref are
+ * both going away, so it's legitimate to pretend the weakref is
+ * going away first. The user has to ensure a weakref outlives its
+ * referent if they want a guarantee that the wr callback will get
+ * invoked.
+ *
+ * 2. It may be catastrophic to call it. If the callback is also in
+ * cyclic trash (CT), then although the CT is unreachable from
+ * outside the current generation, CT may be reachable from the
+ * callback. Then the callback could resurrect insane objects.
+ *
+ * Since the callback is never needed and may be unsafe in this case,
+ * wr is simply left in the unreachable set. Note that because we
+ * already called _PyWeakref_ClearRef(wr), its callback will never
+ * trigger.
+ *
+ * OTOH, if wr isn't part of CT, we should invoke the callback: the
+ * weakref outlived the trash. Note that since wr isn't CT in this
+ * case, its callback can't be CT either -- wr acted as an external
+ * root to this generation, and therefore its callback did too. So
+ * nothing in CT is reachable from the callback either, so it's hard
+ * to imagine how calling it later could create a problem for us. wr
+ * is moved to wrcb_to_call in this case.
+ */
+ if (IS_TENTATIVELY_UNREACHABLE(wr))
+ continue;
+ assert(IS_REACHABLE(wr));
+
+ /* Create a new reference so that wr can't go away
+ * before we can process it again.
+ */
+ Py_INCREF(wr);
+
+ /* Move wr to wrcb_to_call, for the next pass. */
+ wrasgc = AS_GC(wr);
+ assert(wrasgc != next); /* wrasgc is reachable, but
+ next isn't, so they can't
+ be the same */
+ gc_list_move(wrasgc, &wrcb_to_call);
+ }
+ }
+
+ /* Invoke the callbacks we decided to honor. It's safe to invoke them
+ * because they can't reference unreachable objects.
+ */
+ while (! gc_list_is_empty(&wrcb_to_call)) {
+ PyObject *temp;
+ PyObject *callback;
+
+ gc = wrcb_to_call.gc.gc_next;
+ op = FROM_GC(gc);
+ assert(IS_REACHABLE(op));
+ assert(PyWeakref_Check(op));
+ wr = (PyWeakReference *)op;
+ callback = wr->wr_callback;
+ assert(callback != NULL);
+
+ /* copy-paste of weakrefobject.c's handle_callback() */
+ temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
+ if (temp == NULL)
+ PyErr_WriteUnraisable(callback);
+ else
+ Py_DECREF(temp);
+
+ /* Give up the reference we created in the first pass. When
+ * op's refcount hits 0 (which it may or may not do right now),
+ * op's tp_dealloc will decref op->wr_callback too. Note
+ * that the refcount probably will hit 0 now, and because this
+ * weakref was reachable to begin with, gc didn't already
+ * add it to its count of freed objects. Example: a reachable
+ * weak value dict maps some key to this reachable weakref.
+ * The callback removes this key->weakref mapping from the
+ * dict, leaving no other references to the weakref (excepting
+ * ours).
+ */
+ Py_DECREF(op);
+ if (wrcb_to_call.gc.gc_next == gc) {
+ /* object is still alive -- move it */
+ gc_list_move(gc, old);
+ }
+ else
+ ++num_freed;
+ }
+
+ return num_freed;
}
static void
debug_cycle(char *msg, PyObject *op)
{
- PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
- msg, Py_TYPE(op)->tp_name, op);
+ PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
+ msg, Py_TYPE(op)->tp_name, op);
}
/* Handle uncollectable garbage (cycles with finalizers, and stuff reachable
@@ -696,56 +696,56 @@ debug_cycle(char *msg, PyObject *op)
static int
handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
{
- PyGC_Head *gc = finalizers->gc.gc_next;
-
- if (garbage == NULL) {
- garbage = PyList_New(0);
- if (garbage == NULL)
- Py_FatalError("gc couldn't create gc.garbage list");
- }
- for (; gc != finalizers; gc = gc->gc.gc_next) {
- PyObject *op = FROM_GC(gc);
-
- if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
- if (PyList_Append(garbage, op) < 0)
- return -1;
- }
- }
-
- gc_list_merge(finalizers, old);
- return 0;
+ PyGC_Head *gc = finalizers->gc.gc_next;
+
+ if (garbage == NULL) {
+ garbage = PyList_New(0);
+ if (garbage == NULL)
+ Py_FatalError("gc couldn't create gc.garbage list");
+ }
+ for (; gc != finalizers; gc = gc->gc.gc_next) {
+ PyObject *op = FROM_GC(gc);
+
+ if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
+ if (PyList_Append(garbage, op) < 0)
+ return -1;
+ }
+ }
+
+ gc_list_merge(finalizers, old);
+ return 0;
}
-/* Break reference cycles by clearing the containers involved. This is
+/* Break reference cycles by clearing the containers involved. This is
* tricky business as the lists can be changing and we don't know which
* objects may be freed. It is possible I screwed something up here.
*/
static void
delete_garbage(PyGC_Head *collectable, PyGC_Head *old)
{
- inquiry clear;
-
- while (!gc_list_is_empty(collectable)) {
- PyGC_Head *gc = collectable->gc.gc_next;
- PyObject *op = FROM_GC(gc);
-
- assert(IS_TENTATIVELY_UNREACHABLE(op));
- if (debug & DEBUG_SAVEALL) {
- PyList_Append(garbage, op);
- }
- else {
- if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
- Py_INCREF(op);
- clear(op);
- Py_DECREF(op);
- }
- }
- if (collectable->gc.gc_next == gc) {
- /* object is still alive, move it, it may die later */
- gc_list_move(gc, old);
- gc->gc.gc_refs = GC_REACHABLE;
- }
- }
+ inquiry clear;
+
+ while (!gc_list_is_empty(collectable)) {
+ PyGC_Head *gc = collectable->gc.gc_next;
+ PyObject *op = FROM_GC(gc);
+
+ assert(IS_TENTATIVELY_UNREACHABLE(op));
+ if (debug & DEBUG_SAVEALL) {
+ PyList_Append(garbage, op);
+ }
+ else {
+ if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
+ Py_INCREF(op);
+ clear(op);
+ Py_DECREF(op);
+ }
+ }
+ if (collectable->gc.gc_next == gc) {
+ /* object is still alive, move it, it may die later */
+ gc_list_move(gc, old);
+ gc->gc.gc_refs = GC_REACHABLE;
+ }
+ }
}
/* Clear all free lists
@@ -756,30 +756,30 @@ delete_garbage(PyGC_Head *collectable, PyGC_Head *old)
static void
clear_freelists(void)
{
- (void)PyMethod_ClearFreeList();
- (void)PyFrame_ClearFreeList();
- (void)PyCFunction_ClearFreeList();
- (void)PyTuple_ClearFreeList();
- (void)PyUnicode_ClearFreeList();
- (void)PyFloat_ClearFreeList();
+ (void)PyMethod_ClearFreeList();
+ (void)PyFrame_ClearFreeList();
+ (void)PyCFunction_ClearFreeList();
+ (void)PyTuple_ClearFreeList();
+ (void)PyUnicode_ClearFreeList();
+ (void)PyFloat_ClearFreeList();
}
static double
get_time(void)
{
- double result = 0;
- if (tmod != NULL) {
- PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
- if (f == NULL) {
- PyErr_Clear();
- }
- else {
- if (PyFloat_Check(f))
- result = PyFloat_AsDouble(f);
- Py_DECREF(f);
- }
- }
- return result;
+ double result = 0;
+ if (tmod != NULL) {
+ PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
+ if (f == NULL) {
+ PyErr_Clear();
+ }
+ else {
+ if (PyFloat_Check(f))
+ result = PyFloat_AsDouble(f);
+ Py_DECREF(f);
+ }
+ }
+ return result;
}
/* This is the main function. Read this to understand how the
@@ -787,184 +787,184 @@ get_time(void)
static Py_ssize_t
collect(int generation)
{
- int i;
- Py_ssize_t m = 0; /* # objects collected */
- Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
- PyGC_Head *young; /* the generation we are examining */
- PyGC_Head *old; /* next older generation */
- PyGC_Head unreachable; /* non-problematic unreachable trash */
- PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
- PyGC_Head *gc;
- double t1 = 0.0;
-
- if (delstr == NULL) {
- delstr = PyUnicode_InternFromString("__del__");
- if (delstr == NULL)
- Py_FatalError("gc couldn't allocate \"__del__\"");
- }
-
- if (debug & DEBUG_STATS) {
- PySys_WriteStderr("gc: collecting generation %d...\n",
- generation);
- PySys_WriteStderr("gc: objects in each generation:");
- for (i = 0; i < NUM_GENERATIONS; i++)
- PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
- gc_list_size(GEN_HEAD(i)));
- t1 = get_time();
- PySys_WriteStderr("\n");
- }
-
- /* update collection and allocation counters */
- if (generation+1 < NUM_GENERATIONS)
- generations[generation+1].count += 1;
- for (i = 0; i <= generation; i++)
- generations[i].count = 0;
-
- /* merge younger generations with one we are currently collecting */
- for (i = 0; i < generation; i++) {
- gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
- }
-
- /* handy references */
- young = GEN_HEAD(generation);
- if (generation < NUM_GENERATIONS-1)
- old = GEN_HEAD(generation+1);
- else
- old = young;
-
- /* Using ob_refcnt and gc_refs, calculate which objects in the
- * container set are reachable from outside the set (i.e., have a
- * refcount greater than 0 when all the references within the
- * set are taken into account).
- */
- update_refs(young);
- subtract_refs(young);
-
- /* Leave everything reachable from outside young in young, and move
- * everything else (in young) to unreachable.
- * NOTE: This used to move the reachable objects into a reachable
- * set instead. But most things usually turn out to be reachable,
- * so it's more efficient to move the unreachable things.
- */
- gc_list_init(&unreachable);
- move_unreachable(young, &unreachable);
-
- /* Move reachable objects to next generation. */
- if (young != old) {
- if (generation == NUM_GENERATIONS - 2) {
- long_lived_pending += gc_list_size(young);
- }
- gc_list_merge(young, old);
- }
- else {
- long_lived_pending = 0;
- long_lived_total = gc_list_size(young);
- }
-
- /* All objects in unreachable are trash, but objects reachable from
- * finalizers can't safely be deleted. Python programmers should take
- * care not to create such things. For Python, finalizers means
- * instance objects with __del__ methods. Weakrefs with callbacks
- * can also call arbitrary Python code but they will be dealt with by
- * handle_weakrefs().
- */
- gc_list_init(&finalizers);
- move_finalizers(&unreachable, &finalizers);
- /* finalizers contains the unreachable objects with a finalizer;
- * unreachable objects reachable *from* those are also uncollectable,
- * and we move those into the finalizers list too.
- */
- move_finalizer_reachable(&finalizers);
-
- /* Collect statistics on collectable objects found and print
- * debugging information.
- */
- for (gc = unreachable.gc.gc_next; gc != &unreachable;
- gc = gc->gc.gc_next) {
- m++;
- if (debug & DEBUG_COLLECTABLE) {
- debug_cycle("collectable", FROM_GC(gc));
- }
- }
-
- /* Clear weakrefs and invoke callbacks as necessary. */
- m += handle_weakrefs(&unreachable, old);
-
- /* Call tp_clear on objects in the unreachable set. This will cause
- * the reference cycles to be broken. It may also cause some objects
- * in finalizers to be freed.
- */
- delete_garbage(&unreachable, old);
-
- /* Collect statistics on uncollectable objects found and print
- * debugging information. */
- for (gc = finalizers.gc.gc_next;
- gc != &finalizers;
- gc = gc->gc.gc_next) {
- n++;
- if (debug & DEBUG_UNCOLLECTABLE)
- debug_cycle("uncollectable", FROM_GC(gc));
- }
- if (debug & DEBUG_STATS) {
- double t2 = get_time();
- if (m == 0 && n == 0)
- PySys_WriteStderr("gc: done");
- else
- PySys_WriteStderr(
- "gc: done, "
- "%" PY_FORMAT_SIZE_T "d unreachable, "
- "%" PY_FORMAT_SIZE_T "d uncollectable",
- n+m, n);
- if (t1 && t2) {
- PySys_WriteStderr(", %.4fs elapsed", t2-t1);
- }
- PySys_WriteStderr(".\n");
- }
-
- /* Append instances in the uncollectable set to a Python
- * reachable list of garbage. The programmer has to deal with
- * this if they insist on creating this type of structure.
- */
- (void)handle_finalizers(&finalizers, old);
-
- /* Clear free list only during the collection of the highest
- * generation */
- if (generation == NUM_GENERATIONS-1) {
- clear_freelists();
- }
-
- if (PyErr_Occurred()) {
- if (gc_str == NULL)
- gc_str = PyUnicode_FromString("garbage collection");
- PyErr_WriteUnraisable(gc_str);
- Py_FatalError("unexpected exception during garbage collection");
- }
- return n+m;
+ int i;
+ Py_ssize_t m = 0; /* # objects collected */
+ Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
+ PyGC_Head *young; /* the generation we are examining */
+ PyGC_Head *old; /* next older generation */
+ PyGC_Head unreachable; /* non-problematic unreachable trash */
+ PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
+ PyGC_Head *gc;
+ double t1 = 0.0;
+
+ if (delstr == NULL) {
+ delstr = PyUnicode_InternFromString("__del__");
+ if (delstr == NULL)
+ Py_FatalError("gc couldn't allocate \"__del__\"");
+ }
+
+ if (debug & DEBUG_STATS) {
+ PySys_WriteStderr("gc: collecting generation %d...\n",
+ generation);
+ PySys_WriteStderr("gc: objects in each generation:");
+ for (i = 0; i < NUM_GENERATIONS; i++)
+ PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
+ gc_list_size(GEN_HEAD(i)));
+ t1 = get_time();
+ PySys_WriteStderr("\n");
+ }
+
+ /* update collection and allocation counters */
+ if (generation+1 < NUM_GENERATIONS)
+ generations[generation+1].count += 1;
+ for (i = 0; i <= generation; i++)
+ generations[i].count = 0;
+
+ /* merge younger generations with one we are currently collecting */
+ for (i = 0; i < generation; i++) {
+ gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
+ }
+
+ /* handy references */
+ young = GEN_HEAD(generation);
+ if (generation < NUM_GENERATIONS-1)
+ old = GEN_HEAD(generation+1);
+ else
+ old = young;
+
+ /* Using ob_refcnt and gc_refs, calculate which objects in the
+ * container set are reachable from outside the set (i.e., have a
+ * refcount greater than 0 when all the references within the
+ * set are taken into account).
+ */
+ update_refs(young);
+ subtract_refs(young);
+
+ /* Leave everything reachable from outside young in young, and move
+ * everything else (in young) to unreachable.
+ * NOTE: This used to move the reachable objects into a reachable
+ * set instead. But most things usually turn out to be reachable,
+ * so it's more efficient to move the unreachable things.
+ */
+ gc_list_init(&unreachable);
+ move_unreachable(young, &unreachable);
+
+ /* Move reachable objects to next generation. */
+ if (young != old) {
+ if (generation == NUM_GENERATIONS - 2) {
+ long_lived_pending += gc_list_size(young);
+ }
+ gc_list_merge(young, old);
+ }
+ else {
+ long_lived_pending = 0;
+ long_lived_total = gc_list_size(young);
+ }
+
+ /* All objects in unreachable are trash, but objects reachable from
+ * finalizers can't safely be deleted. Python programmers should take
+ * care not to create such things. For Python, finalizers means
+ * instance objects with __del__ methods. Weakrefs with callbacks
+ * can also call arbitrary Python code but they will be dealt with by
+ * handle_weakrefs().
+ */
+ gc_list_init(&finalizers);
+ move_finalizers(&unreachable, &finalizers);
+ /* finalizers contains the unreachable objects with a finalizer;
+ * unreachable objects reachable *from* those are also uncollectable,
+ * and we move those into the finalizers list too.
+ */
+ move_finalizer_reachable(&finalizers);
+
+ /* Collect statistics on collectable objects found and print
+ * debugging information.
+ */
+ for (gc = unreachable.gc.gc_next; gc != &unreachable;
+ gc = gc->gc.gc_next) {
+ m++;
+ if (debug & DEBUG_COLLECTABLE) {
+ debug_cycle("collectable", FROM_GC(gc));
+ }
+ }
+
+ /* Clear weakrefs and invoke callbacks as necessary. */
+ m += handle_weakrefs(&unreachable, old);
+
+ /* Call tp_clear on objects in the unreachable set. This will cause
+ * the reference cycles to be broken. It may also cause some objects
+ * in finalizers to be freed.
+ */
+ delete_garbage(&unreachable, old);
+
+ /* Collect statistics on uncollectable objects found and print
+ * debugging information. */
+ for (gc = finalizers.gc.gc_next;
+ gc != &finalizers;
+ gc = gc->gc.gc_next) {
+ n++;
+ if (debug & DEBUG_UNCOLLECTABLE)
+ debug_cycle("uncollectable", FROM_GC(gc));
+ }
+ if (debug & DEBUG_STATS) {
+ double t2 = get_time();
+ if (m == 0 && n == 0)
+ PySys_WriteStderr("gc: done");
+ else
+ PySys_WriteStderr(
+ "gc: done, "
+ "%" PY_FORMAT_SIZE_T "d unreachable, "
+ "%" PY_FORMAT_SIZE_T "d uncollectable",
+ n+m, n);
+ if (t1 && t2) {
+ PySys_WriteStderr(", %.4fs elapsed", t2-t1);
+ }
+ PySys_WriteStderr(".\n");
+ }
+
+ /* Append instances in the uncollectable set to a Python
+ * reachable list of garbage. The programmer has to deal with
+ * this if they insist on creating this type of structure.
+ */
+ (void)handle_finalizers(&finalizers, old);
+
+ /* Clear free list only during the collection of the highest
+ * generation */
+ if (generation == NUM_GENERATIONS-1) {
+ clear_freelists();
+ }
+
+ if (PyErr_Occurred()) {
+ if (gc_str == NULL)
+ gc_str = PyUnicode_FromString("garbage collection");
+ PyErr_WriteUnraisable(gc_str);
+ Py_FatalError("unexpected exception during garbage collection");
+ }
+ return n+m;
}
static Py_ssize_t
collect_generations(void)
{
- int i;
- Py_ssize_t n = 0;
-
- /* Find the oldest generation (highest numbered) where the count
- * exceeds the threshold. Objects in the that generation and
- * generations younger than it will be collected. */
- for (i = NUM_GENERATIONS-1; i >= 0; i--) {
- if (generations[i].count > generations[i].threshold) {
- /* Avoid quadratic performance degradation in number
- of tracked objects. See comments at the beginning
- of this file, and issue #4074.
- */
- if (i == NUM_GENERATIONS - 1
- && long_lived_pending < long_lived_total / 4)
- continue;
- n = collect(i);
- break;
- }
- }
- return n;
+ int i;
+ Py_ssize_t n = 0;
+
+ /* Find the oldest generation (highest numbered) where the count
+ * exceeds the threshold. Objects in the that generation and
+ * generations younger than it will be collected. */
+ for (i = NUM_GENERATIONS-1; i >= 0; i--) {
+ if (generations[i].count > generations[i].threshold) {
+ /* Avoid quadratic performance degradation in number
+ of tracked objects. See comments at the beginning
+ of this file, and issue #4074.
+ */
+ if (i == NUM_GENERATIONS - 1
+ && long_lived_pending < long_lived_total / 4)
+ continue;
+ n = collect(i);
+ break;
+ }
+ }
+ return n;
}
PyDoc_STRVAR(gc_enable__doc__,
@@ -975,9 +975,9 @@ PyDoc_STRVAR(gc_enable__doc__,
static PyObject *
gc_enable(PyObject *self, PyObject *noargs)
{
- enabled = 1;
- Py_INCREF(Py_None);
- return Py_None;
+ enabled = 1;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(gc_disable__doc__,
@@ -988,9 +988,9 @@ PyDoc_STRVAR(gc_disable__doc__,
static PyObject *
gc_disable(PyObject *self, PyObject *noargs)
{
- enabled = 0;
- Py_INCREF(Py_None);
- return Py_None;
+ enabled = 0;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(gc_isenabled__doc__,
@@ -1001,7 +1001,7 @@ PyDoc_STRVAR(gc_isenabled__doc__,
static PyObject *
gc_isenabled(PyObject *self, PyObject *noargs)
{
- return PyBool_FromLong((long)enabled);
+ return PyBool_FromLong((long)enabled);
}
PyDoc_STRVAR(gc_collect__doc__,
@@ -1015,27 +1015,27 @@ PyDoc_STRVAR(gc_collect__doc__,
static PyObject *
gc_collect(PyObject *self, PyObject *args, PyObject *kws)
{
- static char *keywords[] = {"generation", NULL};
- int genarg = NUM_GENERATIONS - 1;
- Py_ssize_t n;
-
- if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
- return NULL;
-
- else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
- PyErr_SetString(PyExc_ValueError, "invalid generation");
- return NULL;
- }
-
- if (collecting)
- n = 0; /* already collecting, don't do anything */
- else {
- collecting = 1;
- n = collect(genarg);
- collecting = 0;
- }
-
- return PyLong_FromSsize_t(n);
+ static char *keywords[] = {"generation", NULL};
+ int genarg = NUM_GENERATIONS - 1;
+ Py_ssize_t n;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
+ return NULL;
+
+ else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
+ PyErr_SetString(PyExc_ValueError, "invalid generation");
+ return NULL;
+ }
+
+ if (collecting)
+ n = 0; /* already collecting, don't do anything */
+ else {
+ collecting = 1;
+ n = collect(genarg);
+ collecting = 0;
+ }
+
+ return PyLong_FromSsize_t(n);
}
PyDoc_STRVAR(gc_set_debug__doc__,
@@ -1055,11 +1055,11 @@ PyDoc_STRVAR(gc_set_debug__doc__,
static PyObject *
gc_set_debug(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
+ return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(gc_get_debug__doc__,
@@ -1070,7 +1070,7 @@ PyDoc_STRVAR(gc_get_debug__doc__,
static PyObject *
gc_get_debug(PyObject *self, PyObject *noargs)
{
- return Py_BuildValue("i", debug);
+ return Py_BuildValue("i", debug);
}
PyDoc_STRVAR(gc_set_thresh__doc__,
@@ -1082,19 +1082,19 @@ PyDoc_STRVAR(gc_set_thresh__doc__,
static PyObject *
gc_set_thresh(PyObject *self, PyObject *args)
{
- int i;
- if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
- &generations[0].threshold,
- &generations[1].threshold,
- &generations[2].threshold))
- return NULL;
- for (i = 2; i < NUM_GENERATIONS; i++) {
- /* generations higher than 2 get the same threshold */
- generations[i].threshold = generations[2].threshold;
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ int i;
+ if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
+ &generations[0].threshold,
+ &generations[1].threshold,
+ &generations[2].threshold))
+ return NULL;
+ for (i = 2; i < NUM_GENERATIONS; i++) {
+ /* generations higher than 2 get the same threshold */
+ generations[i].threshold = generations[2].threshold;
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(gc_get_thresh__doc__,
@@ -1105,10 +1105,10 @@ PyDoc_STRVAR(gc_get_thresh__doc__,
static PyObject *
gc_get_thresh(PyObject *self, PyObject *noargs)
{
- return Py_BuildValue("(iii)",
- generations[0].threshold,
- generations[1].threshold,
- generations[2].threshold);
+ return Py_BuildValue("(iii)",
+ generations[0].threshold,
+ generations[1].threshold,
+ generations[2].threshold);
}
PyDoc_STRVAR(gc_get_count__doc__,
@@ -1119,39 +1119,39 @@ PyDoc_STRVAR(gc_get_count__doc__,
static PyObject *
gc_get_count(PyObject *self, PyObject *noargs)
{
- return Py_BuildValue("(iii)",
- generations[0].count,
- generations[1].count,
- generations[2].count);
+ return Py_BuildValue("(iii)",
+ generations[0].count,
+ generations[1].count,
+ generations[2].count);
}
static int
referrersvisit(PyObject* obj, PyObject *objs)
{
- Py_ssize_t i;
- for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
- if (PyTuple_GET_ITEM(objs, i) == obj)
- return 1;
- return 0;
+ Py_ssize_t i;
+ for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
+ if (PyTuple_GET_ITEM(objs, i) == obj)
+ return 1;
+ return 0;
}
static int
gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
{
- PyGC_Head *gc;
- PyObject *obj;
- traverseproc traverse;
- for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
- obj = FROM_GC(gc);
- traverse = Py_TYPE(obj)->tp_traverse;
- if (obj == objs || obj == resultlist)
- continue;
- if (traverse(obj, (visitproc)referrersvisit, objs)) {
- if (PyList_Append(resultlist, obj) < 0)
- return 0; /* error */
- }
- }
- return 1; /* no error */
+ PyGC_Head *gc;
+ PyObject *obj;
+ traverseproc traverse;
+ for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
+ obj = FROM_GC(gc);
+ traverse = Py_TYPE(obj)->tp_traverse;
+ if (obj == objs || obj == resultlist)
+ continue;
+ if (traverse(obj, (visitproc)referrersvisit, objs)) {
+ if (PyList_Append(resultlist, obj) < 0)
+ return 0; /* error */
+ }
+ }
+ return 1; /* no error */
}
PyDoc_STRVAR(gc_get_referrers__doc__,
@@ -1161,24 +1161,24 @@ Return the list of objects that directly refer to any of objs.");
static PyObject *
gc_get_referrers(PyObject *self, PyObject *args)
{
- int i;
- PyObject *result = PyList_New(0);
- if (!result) return NULL;
-
- for (i = 0; i < NUM_GENERATIONS; i++) {
- if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
- Py_DECREF(result);
- return NULL;
- }
- }
- return result;
+ int i;
+ PyObject *result = PyList_New(0);
+ if (!result) return NULL;
+
+ for (i = 0; i < NUM_GENERATIONS; i++) {
+ if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ return result;
}
/* Append obj to list; return true if error (out of memory), false if OK. */
static int
referentsvisit(PyObject *obj, PyObject *list)
{
- return PyList_Append(list, obj) < 0;
+ return PyList_Append(list, obj) < 0;
}
PyDoc_STRVAR(gc_get_referents__doc__,
@@ -1188,27 +1188,27 @@ Return the list of objects that are directly referred to by objs.");
static PyObject *
gc_get_referents(PyObject *self, PyObject *args)
{
- Py_ssize_t i;
- PyObject *result = PyList_New(0);
-
- if (result == NULL)
- return NULL;
-
- for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
- traverseproc traverse;
- PyObject *obj = PyTuple_GET_ITEM(args, i);
-
- if (! PyObject_IS_GC(obj))
- continue;
- traverse = Py_TYPE(obj)->tp_traverse;
- if (! traverse)
- continue;
- if (traverse(obj, (visitproc)referentsvisit, result)) {
- Py_DECREF(result);
- return NULL;
- }
- }
- return result;
+ Py_ssize_t i;
+ PyObject *result = PyList_New(0);
+
+ if (result == NULL)
+ return NULL;
+
+ for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
+ traverseproc traverse;
+ PyObject *obj = PyTuple_GET_ITEM(args, i);
+
+ if (! PyObject_IS_GC(obj))
+ continue;
+ traverse = Py_TYPE(obj)->tp_traverse;
+ if (! traverse)
+ continue;
+ if (traverse(obj, (visitproc)referentsvisit, result)) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ return result;
}
PyDoc_STRVAR(gc_get_objects__doc__,
@@ -1220,19 +1220,19 @@ PyDoc_STRVAR(gc_get_objects__doc__,
static PyObject *
gc_get_objects(PyObject *self, PyObject *noargs)
{
- int i;
- PyObject* result;
-
- result = PyList_New(0);
- if (result == NULL)
- return NULL;
- for (i = 0; i < NUM_GENERATIONS; i++) {
- if (append_objects(result, GEN_HEAD(i))) {
- Py_DECREF(result);
- return NULL;
- }
- }
- return result;
+ int i;
+ PyObject* result;
+
+ result = PyList_New(0);
+ if (result == NULL)
+ return NULL;
+ for (i = 0; i < NUM_GENERATIONS; i++) {
+ if (append_objects(result, GEN_HEAD(i))) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ return result;
}
PyDoc_STRVAR(gc_is_tracked__doc__,
@@ -1245,14 +1245,14 @@ PyDoc_STRVAR(gc_is_tracked__doc__,
static PyObject *
gc_is_tracked(PyObject *self, PyObject *obj)
{
- PyObject *result;
-
- if (PyObject_IS_GC(obj) && IS_TRACKED(obj))
- result = Py_True;
- else
- result = Py_False;
- Py_INCREF(result);
- return result;
+ PyObject *result;
+
+ if (PyObject_IS_GC(obj) && IS_TRACKED(obj))
+ result = Py_True;
+ else
+ result = Py_False;
+ Py_INCREF(result);
+ return result;
}
@@ -1274,101 +1274,101 @@ PyDoc_STRVAR(gc__doc__,
"get_referents() -- Return the list of objects that an object refers to.\n");
static PyMethodDef GcMethods[] = {
- {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
- {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
- {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
- {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
- {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
- {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
- {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
- {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
- {"collect", (PyCFunction)gc_collect,
- METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
- {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
- {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},
- {"get_referrers", gc_get_referrers, METH_VARARGS,
- gc_get_referrers__doc__},
- {"get_referents", gc_get_referents, METH_VARARGS,
- gc_get_referents__doc__},
- {NULL, NULL} /* Sentinel */
+ {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
+ {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
+ {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
+ {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
+ {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
+ {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
+ {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
+ {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
+ {"collect", (PyCFunction)gc_collect,
+ METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
+ {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
+ {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},
+ {"get_referrers", gc_get_referrers, METH_VARARGS,
+ gc_get_referrers__doc__},
+ {"get_referents", gc_get_referents, METH_VARARGS,
+ gc_get_referents__doc__},
+ {NULL, NULL} /* Sentinel */
};
static struct PyModuleDef gcmodule = {
- PyModuleDef_HEAD_INIT,
- "gc",
- gc__doc__,
- -1,
- GcMethods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "gc",
+ gc__doc__,
+ -1,
+ GcMethods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_gc(void)
{
- PyObject *m;
-
- m = PyModule_Create(&gcmodule);
-
- if (m == NULL)
- return NULL;
-
- if (garbage == NULL) {
- garbage = PyList_New(0);
- if (garbage == NULL)
- return NULL;
- }
- Py_INCREF(garbage);
- if (PyModule_AddObject(m, "garbage", garbage) < 0)
- return NULL;
-
- /* Importing can't be done in collect() because collect()
- * can be called via PyGC_Collect() in Py_Finalize().
- * This wouldn't be a problem, except that <initialized> is
- * reset to 0 before calling collect which trips up
- * the import and triggers an assertion.
- */
- if (tmod == NULL) {
- tmod = PyImport_ImportModuleNoBlock("time");
- if (tmod == NULL)
- PyErr_Clear();
- }
+ PyObject *m;
+
+ m = PyModule_Create(&gcmodule);
+
+ if (m == NULL)
+ return NULL;
+
+ if (garbage == NULL) {
+ garbage = PyList_New(0);
+ if (garbage == NULL)
+ return NULL;
+ }
+ Py_INCREF(garbage);
+ if (PyModule_AddObject(m, "garbage", garbage) < 0)
+ return NULL;
+
+ /* Importing can't be done in collect() because collect()
+ * can be called via PyGC_Collect() in Py_Finalize().
+ * This wouldn't be a problem, except that <initialized> is
+ * reset to 0 before calling collect which trips up
+ * the import and triggers an assertion.
+ */
+ if (tmod == NULL) {
+ tmod = PyImport_ImportModuleNoBlock("time");
+ if (tmod == NULL)
+ PyErr_Clear();
+ }
#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
- ADD_INT(DEBUG_STATS);
- ADD_INT(DEBUG_COLLECTABLE);
- ADD_INT(DEBUG_UNCOLLECTABLE);
- ADD_INT(DEBUG_SAVEALL);
- ADD_INT(DEBUG_LEAK);
+ ADD_INT(DEBUG_STATS);
+ ADD_INT(DEBUG_COLLECTABLE);
+ ADD_INT(DEBUG_UNCOLLECTABLE);
+ ADD_INT(DEBUG_SAVEALL);
+ ADD_INT(DEBUG_LEAK);
#undef ADD_INT
- return m;
+ return m;
}
/* API to invoke gc.collect() from C */
Py_ssize_t
PyGC_Collect(void)
{
- Py_ssize_t n;
+ Py_ssize_t n;
- if (collecting)
- n = 0; /* already collecting, don't do anything */
- else {
- collecting = 1;
- n = collect(NUM_GENERATIONS - 1);
- collecting = 0;
- }
+ if (collecting)
+ n = 0; /* already collecting, don't do anything */
+ else {
+ collecting = 1;
+ n = collect(NUM_GENERATIONS - 1);
+ collecting = 0;
+ }
- return n;
+ return n;
}
/* for debugging */
void
_PyGC_Dump(PyGC_Head *g)
{
- _PyObject_Dump(FROM_GC(g));
+ _PyObject_Dump(FROM_GC(g));
}
/* extension modules might be compiled with GC support so these
@@ -1382,7 +1382,7 @@ _PyGC_Dump(PyGC_Head *g)
void
PyObject_GC_Track(void *op)
{
- _PyObject_GC_TRACK(op);
+ _PyObject_GC_TRACK(op);
}
/* for binary compatibility with 2.2 */
@@ -1395,11 +1395,11 @@ _PyObject_GC_Track(PyObject *op)
void
PyObject_GC_UnTrack(void *op)
{
- /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
- * call PyObject_GC_UnTrack twice on an object.
- */
- if (IS_TRACKED(op))
- _PyObject_GC_UNTRACK(op);
+ /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
+ * call PyObject_GC_UnTrack twice on an object.
+ */
+ if (IS_TRACKED(op))
+ _PyObject_GC_UNTRACK(op);
}
/* for binary compatibility with 2.2 */
@@ -1412,73 +1412,73 @@ _PyObject_GC_UnTrack(PyObject *op)
PyObject *
_PyObject_GC_Malloc(size_t basicsize)
{
- PyObject *op;
- PyGC_Head *g;
- if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
- return PyErr_NoMemory();
- g = (PyGC_Head *)PyObject_MALLOC(
- sizeof(PyGC_Head) + basicsize);
- if (g == NULL)
- return PyErr_NoMemory();
- g->gc.gc_refs = GC_UNTRACKED;
- generations[0].count++; /* number of allocated GC objects */
- if (generations[0].count > generations[0].threshold &&
- enabled &&
- generations[0].threshold &&
- !collecting &&
- !PyErr_Occurred()) {
- collecting = 1;
- collect_generations();
- collecting = 0;
- }
- op = FROM_GC(g);
- return op;
+ PyObject *op;
+ PyGC_Head *g;
+ if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
+ return PyErr_NoMemory();
+ g = (PyGC_Head *)PyObject_MALLOC(
+ sizeof(PyGC_Head) + basicsize);
+ if (g == NULL)
+ return PyErr_NoMemory();
+ g->gc.gc_refs = GC_UNTRACKED;
+ generations[0].count++; /* number of allocated GC objects */
+ if (generations[0].count > generations[0].threshold &&
+ enabled &&
+ generations[0].threshold &&
+ !collecting &&
+ !PyErr_Occurred()) {
+ collecting = 1;
+ collect_generations();
+ collecting = 0;
+ }
+ op = FROM_GC(g);
+ return op;
}
PyObject *
_PyObject_GC_New(PyTypeObject *tp)
{
- PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
- if (op != NULL)
- op = PyObject_INIT(op, tp);
- return op;
+ PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
+ if (op != NULL)
+ op = PyObject_INIT(op, tp);
+ return op;
}
PyVarObject *
_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
{
- const size_t size = _PyObject_VAR_SIZE(tp, nitems);
- PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
- if (op != NULL)
- op = PyObject_INIT_VAR(op, tp, nitems);
- return op;
+ const size_t size = _PyObject_VAR_SIZE(tp, nitems);
+ PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
+ if (op != NULL)
+ op = PyObject_INIT_VAR(op, tp, nitems);
+ return op;
}
PyVarObject *
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
{
- const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
- PyGC_Head *g = AS_GC(op);
- if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
- return (PyVarObject *)PyErr_NoMemory();
- g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
- if (g == NULL)
- return (PyVarObject *)PyErr_NoMemory();
- op = (PyVarObject *) FROM_GC(g);
- Py_SIZE(op) = nitems;
- return op;
+ const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
+ PyGC_Head *g = AS_GC(op);
+ if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
+ return (PyVarObject *)PyErr_NoMemory();
+ g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
+ if (g == NULL)
+ return (PyVarObject *)PyErr_NoMemory();
+ op = (PyVarObject *) FROM_GC(g);
+ Py_SIZE(op) = nitems;
+ return op;
}
void
PyObject_GC_Del(void *op)
{
- PyGC_Head *g = AS_GC(op);
- if (IS_TRACKED(op))
- gc_list_remove(g);
- if (generations[0].count > 0) {
- generations[0].count--;
- }
- PyObject_FREE(g);
+ PyGC_Head *g = AS_GC(op);
+ if (IS_TRACKED(op))
+ gc_list_remove(g);
+ if (generations[0].count > 0) {
+ generations[0].count--;
+ }
+ PyObject_FREE(g);
}
/* for binary compatibility with 2.2 */
diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c
index 5bcec2b..a3ca30a 100644
--- a/Modules/getaddrinfo.c
+++ b/Modules/getaddrinfo.c
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -13,7 +13,7 @@
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* GAI_ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -73,52 +73,52 @@ static struct in6_addr faith_prefix = IN6ADDR_GAI_ANY_INIT;
static const char in_addrany[] = { 0, 0, 0, 0 };
static const char in6_addrany[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
-static const char in_loopback[] = { 127, 0, 0, 1 };
+static const char in_loopback[] = { 127, 0, 0, 1 };
static const char in6_loopback[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};
struct sockinet {
- u_char si_len;
- u_char si_family;
- u_short si_port;
+ u_char si_len;
+ u_char si_family;
+ u_short si_port;
};
static struct gai_afd {
- int a_af;
- int a_addrlen;
- int a_socklen;
- int a_off;
- const char *a_addrany;
- const char *a_loopback;
+ int a_af;
+ int a_addrlen;
+ int a_socklen;
+ int a_off;
+ const char *a_addrany;
+ const char *a_loopback;
} gai_afdl [] = {
#ifdef ENABLE_IPV6
#define N_INET6 0
- {PF_INET6, sizeof(struct in6_addr),
- sizeof(struct sockaddr_in6),
- offsetof(struct sockaddr_in6, sin6_addr),
- in6_addrany, in6_loopback},
+ {PF_INET6, sizeof(struct in6_addr),
+ sizeof(struct sockaddr_in6),
+ offsetof(struct sockaddr_in6, sin6_addr),
+ in6_addrany, in6_loopback},
#define N_INET 1
#else
#define N_INET 0
#endif
- {PF_INET, sizeof(struct in_addr),
- sizeof(struct sockaddr_in),
- offsetof(struct sockaddr_in, sin_addr),
- in_addrany, in_loopback},
- {0, 0, 0, 0, NULL, NULL},
+ {PF_INET, sizeof(struct in_addr),
+ sizeof(struct sockaddr_in),
+ offsetof(struct sockaddr_in, sin_addr),
+ in_addrany, in_loopback},
+ {0, 0, 0, 0, NULL, NULL},
};
#ifdef ENABLE_IPV6
-#define PTON_MAX 16
+#define PTON_MAX 16
#else
-#define PTON_MAX 4
+#define PTON_MAX 4
#endif
#ifndef IN_MULTICAST
-#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U)
+#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U)
#endif
#ifndef IN_EXPERIMENTAL
@@ -126,73 +126,73 @@ static struct gai_afd {
#endif
#ifndef IN_LOOPBACKNET
-#define IN_LOOPBACKNET 127
+#define IN_LOOPBACKNET 127
#endif
static int get_name(const char *, struct gai_afd *,
- struct addrinfo **, char *, struct addrinfo *,
- int);
+ struct addrinfo **, char *, struct addrinfo *,
+ int);
static int get_addr(const char *, int, struct addrinfo **,
- struct addrinfo *, int);
+ struct addrinfo *, int);
static int str_isnumber(const char *);
-
+
static char *ai_errlist[] = {
- "success.",
- "address family for hostname not supported.", /* EAI_ADDRFAMILY */
- "temporary failure in name resolution.", /* EAI_AGAIN */
- "invalid value for ai_flags.", /* EAI_BADFLAGS */
- "non-recoverable failure in name resolution.", /* EAI_FAIL */
- "ai_family not supported.", /* EAI_FAMILY */
- "memory allocation failure.", /* EAI_MEMORY */
- "no address associated with hostname.", /* EAI_NODATA */
- "hostname nor servname provided, or not known.",/* EAI_NONAME */
- "servname not supported for ai_socktype.", /* EAI_SERVICE */
- "ai_socktype not supported.", /* EAI_SOCKTYPE */
- "system error returned in errno.", /* EAI_SYSTEM */
- "invalid value for hints.", /* EAI_BADHINTS */
- "resolved protocol is unknown.", /* EAI_PROTOCOL */
- "unknown error.", /* EAI_MAX */
+ "success.",
+ "address family for hostname not supported.", /* EAI_ADDRFAMILY */
+ "temporary failure in name resolution.", /* EAI_AGAIN */
+ "invalid value for ai_flags.", /* EAI_BADFLAGS */
+ "non-recoverable failure in name resolution.", /* EAI_FAIL */
+ "ai_family not supported.", /* EAI_FAMILY */
+ "memory allocation failure.", /* EAI_MEMORY */
+ "no address associated with hostname.", /* EAI_NODATA */
+ "hostname nor servname provided, or not known.",/* EAI_NONAME */
+ "servname not supported for ai_socktype.", /* EAI_SERVICE */
+ "ai_socktype not supported.", /* EAI_SOCKTYPE */
+ "system error returned in errno.", /* EAI_SYSTEM */
+ "invalid value for hints.", /* EAI_BADHINTS */
+ "resolved protocol is unknown.", /* EAI_PROTOCOL */
+ "unknown error.", /* EAI_MAX */
};
#define GET_CANONNAME(ai, str) \
if (pai->ai_flags & AI_CANONNAME) {\
- if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
- strcpy((ai)->ai_canonname, (str));\
- } else {\
- error = EAI_MEMORY;\
- goto free;\
- }\
+ if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
+ strcpy((ai)->ai_canonname, (str));\
+ } else {\
+ error = EAI_MEMORY;\
+ goto free;\
+ }\
}
#ifdef HAVE_SOCKADDR_SA_LEN
#define GET_AI(ai, gai_afd, addr, port) {\
- char *p;\
- if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
- ((gai_afd)->a_socklen)))\
- == NULL) goto free;\
- memcpy(ai, pai, sizeof(struct addrinfo));\
- (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
- memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
- (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\
- (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
- ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
- p = (char *)((ai)->ai_addr);\
- memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
+ char *p;\
+ if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
+ ((gai_afd)->a_socklen)))\
+ == NULL) goto free;\
+ memcpy(ai, pai, sizeof(struct addrinfo));\
+ (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
+ memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
+ (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\
+ (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
+ ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
+ p = (char *)((ai)->ai_addr);\
+ memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
}
#else
#define GET_AI(ai, gai_afd, addr, port) {\
- char *p;\
- if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
- ((gai_afd)->a_socklen)))\
- == NULL) goto free;\
- memcpy(ai, pai, sizeof(struct addrinfo));\
- (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
- memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
- (ai)->ai_addrlen = (gai_afd)->a_socklen;\
- (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
- ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
- p = (char *)((ai)->ai_addr);\
- memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
+ char *p;\
+ if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
+ ((gai_afd)->a_socklen)))\
+ == NULL) goto free;\
+ memcpy(ai, pai, sizeof(struct addrinfo));\
+ (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
+ memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
+ (ai)->ai_addrlen = (gai_afd)->a_socklen;\
+ (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\
+ ((struct sockinet *)(ai)->ai_addr)->si_port = port;\
+ p = (char *)((ai)->ai_addr);\
+ memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\
}
#endif
@@ -201,438 +201,438 @@ if (pai->ai_flags & AI_CANONNAME) {\
char *
gai_strerror(int ecode)
{
- if (ecode < 0 || ecode > EAI_MAX)
- ecode = EAI_MAX;
- return ai_errlist[ecode];
+ if (ecode < 0 || ecode > EAI_MAX)
+ ecode = EAI_MAX;
+ return ai_errlist[ecode];
}
void
freeaddrinfo(struct addrinfo *ai)
{
- struct addrinfo *next;
-
- do {
- next = ai->ai_next;
- if (ai->ai_canonname)
- free(ai->ai_canonname);
- /* no need to free(ai->ai_addr) */
- free(ai);
- } while ((ai = next) != NULL);
+ struct addrinfo *next;
+
+ do {
+ next = ai->ai_next;
+ if (ai->ai_canonname)
+ free(ai->ai_canonname);
+ /* no need to free(ai->ai_addr) */
+ free(ai);
+ } while ((ai = next) != NULL);
}
static int
str_isnumber(const char *p)
{
- unsigned char *q = (unsigned char *)p;
- while (*q) {
- if (! isdigit(*q))
- return NO;
- q++;
- }
- return YES;
+ unsigned char *q = (unsigned char *)p;
+ while (*q) {
+ if (! isdigit(*q))
+ return NO;
+ q++;
+ }
+ return YES;
}
int
getaddrinfo(const char*hostname, const char*servname,
const struct addrinfo *hints, struct addrinfo **res)
{
- struct addrinfo sentinel;
- struct addrinfo *top = NULL;
- struct addrinfo *cur;
- int i, error = 0;
- char pton[PTON_MAX];
- struct addrinfo ai;
- struct addrinfo *pai;
- u_short port;
+ struct addrinfo sentinel;
+ struct addrinfo *top = NULL;
+ struct addrinfo *cur;
+ int i, error = 0;
+ char pton[PTON_MAX];
+ struct addrinfo ai;
+ struct addrinfo *pai;
+ u_short port;
#ifdef FAITH
- static int firsttime = 1;
-
- if (firsttime) {
- /* translator hack */
- {
- char *q = getenv("GAI");
- if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
- translate = YES;
- }
- firsttime = 0;
- }
+ static int firsttime = 1;
+
+ if (firsttime) {
+ /* translator hack */
+ {
+ char *q = getenv("GAI");
+ if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
+ translate = YES;
+ }
+ firsttime = 0;
+ }
#endif
- /* initialize file static vars */
- sentinel.ai_next = NULL;
- cur = &sentinel;
- pai = &ai;
- pai->ai_flags = 0;
- pai->ai_family = PF_UNSPEC;
- pai->ai_socktype = GAI_ANY;
- pai->ai_protocol = GAI_ANY;
- pai->ai_addrlen = 0;
- pai->ai_canonname = NULL;
- pai->ai_addr = NULL;
- pai->ai_next = NULL;
- port = GAI_ANY;
-
- if (hostname == NULL && servname == NULL)
- return EAI_NONAME;
- if (hints) {
- /* error check for hints */
- if (hints->ai_addrlen || hints->ai_canonname ||
- hints->ai_addr || hints->ai_next)
- ERR(EAI_BADHINTS); /* xxx */
- if (hints->ai_flags & ~AI_MASK)
- ERR(EAI_BADFLAGS);
- switch (hints->ai_family) {
- case PF_UNSPEC:
- case PF_INET:
+ /* initialize file static vars */
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
+ pai = &ai;
+ pai->ai_flags = 0;
+ pai->ai_family = PF_UNSPEC;
+ pai->ai_socktype = GAI_ANY;
+ pai->ai_protocol = GAI_ANY;
+ pai->ai_addrlen = 0;
+ pai->ai_canonname = NULL;
+ pai->ai_addr = NULL;
+ pai->ai_next = NULL;
+ port = GAI_ANY;
+
+ if (hostname == NULL && servname == NULL)
+ return EAI_NONAME;
+ if (hints) {
+ /* error check for hints */
+ if (hints->ai_addrlen || hints->ai_canonname ||
+ hints->ai_addr || hints->ai_next)
+ ERR(EAI_BADHINTS); /* xxx */
+ if (hints->ai_flags & ~AI_MASK)
+ ERR(EAI_BADFLAGS);
+ switch (hints->ai_family) {
+ case PF_UNSPEC:
+ case PF_INET:
#ifdef ENABLE_IPV6
- case PF_INET6:
+ case PF_INET6:
#endif
- break;
- default:
- ERR(EAI_FAMILY);
- }
- memcpy(pai, hints, sizeof(*pai));
- switch (pai->ai_socktype) {
- case GAI_ANY:
- switch (pai->ai_protocol) {
- case GAI_ANY:
- break;
- case IPPROTO_UDP:
- pai->ai_socktype = SOCK_DGRAM;
- break;
- case IPPROTO_TCP:
- pai->ai_socktype = SOCK_STREAM;
- break;
- default:
- pai->ai_socktype = SOCK_RAW;
- break;
- }
- break;
- case SOCK_RAW:
- break;
- case SOCK_DGRAM:
- if (pai->ai_protocol != IPPROTO_UDP &&
- pai->ai_protocol != GAI_ANY)
- ERR(EAI_BADHINTS); /*xxx*/
- pai->ai_protocol = IPPROTO_UDP;
- break;
- case SOCK_STREAM:
- if (pai->ai_protocol != IPPROTO_TCP &&
- pai->ai_protocol != GAI_ANY)
- ERR(EAI_BADHINTS); /*xxx*/
- pai->ai_protocol = IPPROTO_TCP;
- break;
- default:
- ERR(EAI_SOCKTYPE);
- /* unreachable */
- }
- }
-
- /*
- * service port
- */
- if (servname) {
- if (str_isnumber(servname)) {
- if (pai->ai_socktype == GAI_ANY) {
- /* caller accept *GAI_ANY* socktype */
- pai->ai_socktype = SOCK_DGRAM;
- pai->ai_protocol = IPPROTO_UDP;
- }
- port = htons((u_short)atoi(servname));
- } else {
- struct servent *sp;
- char *proto;
-
- proto = NULL;
- switch (pai->ai_socktype) {
- case GAI_ANY:
- proto = NULL;
- break;
- case SOCK_DGRAM:
- proto = "udp";
- break;
- case SOCK_STREAM:
- proto = "tcp";
- break;
- default:
- fprintf(stderr, "panic!\n");
- break;
- }
- if ((sp = getservbyname(servname, proto)) == NULL)
- ERR(EAI_SERVICE);
- port = sp->s_port;
- if (pai->ai_socktype == GAI_ANY) {
- if (strcmp(sp->s_proto, "udp") == 0) {
- pai->ai_socktype = SOCK_DGRAM;
- pai->ai_protocol = IPPROTO_UDP;
- } else if (strcmp(sp->s_proto, "tcp") == 0) {
- pai->ai_socktype = SOCK_STREAM;
- pai->ai_protocol = IPPROTO_TCP;
- } else
- ERR(EAI_PROTOCOL); /*xxx*/
- }
- }
- }
-
- /*
- * hostname == NULL.
- * passive socket -> anyaddr (0.0.0.0 or ::)
- * non-passive socket -> localhost (127.0.0.1 or ::1)
- */
- if (hostname == NULL) {
- struct gai_afd *gai_afd;
-
- for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) {
- if (!(pai->ai_family == PF_UNSPEC
- || pai->ai_family == gai_afd->a_af)) {
- continue;
- }
-
- if (pai->ai_flags & AI_PASSIVE) {
- GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port);
- /* xxx meaningless?
- * GET_CANONNAME(cur->ai_next, "anyaddr");
- */
- } else {
- GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback,
- port);
- /* xxx meaningless?
- * GET_CANONNAME(cur->ai_next, "localhost");
- */
- }
- cur = cur->ai_next;
- }
- top = sentinel.ai_next;
- if (top)
- goto good;
- else
- ERR(EAI_FAMILY);
- }
-
- /* hostname as numeric name */
- for (i = 0; gai_afdl[i].a_af; i++) {
- if (inet_pton(gai_afdl[i].a_af, hostname, pton)) {
- u_long v4a;
+ break;
+ default:
+ ERR(EAI_FAMILY);
+ }
+ memcpy(pai, hints, sizeof(*pai));
+ switch (pai->ai_socktype) {
+ case GAI_ANY:
+ switch (pai->ai_protocol) {
+ case GAI_ANY:
+ break;
+ case IPPROTO_UDP:
+ pai->ai_socktype = SOCK_DGRAM;
+ break;
+ case IPPROTO_TCP:
+ pai->ai_socktype = SOCK_STREAM;
+ break;
+ default:
+ pai->ai_socktype = SOCK_RAW;
+ break;
+ }
+ break;
+ case SOCK_RAW:
+ break;
+ case SOCK_DGRAM:
+ if (pai->ai_protocol != IPPROTO_UDP &&
+ pai->ai_protocol != GAI_ANY)
+ ERR(EAI_BADHINTS); /*xxx*/
+ pai->ai_protocol = IPPROTO_UDP;
+ break;
+ case SOCK_STREAM:
+ if (pai->ai_protocol != IPPROTO_TCP &&
+ pai->ai_protocol != GAI_ANY)
+ ERR(EAI_BADHINTS); /*xxx*/
+ pai->ai_protocol = IPPROTO_TCP;
+ break;
+ default:
+ ERR(EAI_SOCKTYPE);
+ /* unreachable */
+ }
+ }
+
+ /*
+ * service port
+ */
+ if (servname) {
+ if (str_isnumber(servname)) {
+ if (pai->ai_socktype == GAI_ANY) {
+ /* caller accept *GAI_ANY* socktype */
+ pai->ai_socktype = SOCK_DGRAM;
+ pai->ai_protocol = IPPROTO_UDP;
+ }
+ port = htons((u_short)atoi(servname));
+ } else {
+ struct servent *sp;
+ char *proto;
+
+ proto = NULL;
+ switch (pai->ai_socktype) {
+ case GAI_ANY:
+ proto = NULL;
+ break;
+ case SOCK_DGRAM:
+ proto = "udp";
+ break;
+ case SOCK_STREAM:
+ proto = "tcp";
+ break;
+ default:
+ fprintf(stderr, "panic!\n");
+ break;
+ }
+ if ((sp = getservbyname(servname, proto)) == NULL)
+ ERR(EAI_SERVICE);
+ port = sp->s_port;
+ if (pai->ai_socktype == GAI_ANY) {
+ if (strcmp(sp->s_proto, "udp") == 0) {
+ pai->ai_socktype = SOCK_DGRAM;
+ pai->ai_protocol = IPPROTO_UDP;
+ } else if (strcmp(sp->s_proto, "tcp") == 0) {
+ pai->ai_socktype = SOCK_STREAM;
+ pai->ai_protocol = IPPROTO_TCP;
+ } else
+ ERR(EAI_PROTOCOL); /*xxx*/
+ }
+ }
+ }
+
+ /*
+ * hostname == NULL.
+ * passive socket -> anyaddr (0.0.0.0 or ::)
+ * non-passive socket -> localhost (127.0.0.1 or ::1)
+ */
+ if (hostname == NULL) {
+ struct gai_afd *gai_afd;
+
+ for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) {
+ if (!(pai->ai_family == PF_UNSPEC
+ || pai->ai_family == gai_afd->a_af)) {
+ continue;
+ }
+
+ if (pai->ai_flags & AI_PASSIVE) {
+ GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "anyaddr");
+ */
+ } else {
+ GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback,
+ port);
+ /* xxx meaningless?
+ * GET_CANONNAME(cur->ai_next, "localhost");
+ */
+ }
+ cur = cur->ai_next;
+ }
+ top = sentinel.ai_next;
+ if (top)
+ goto good;
+ else
+ ERR(EAI_FAMILY);
+ }
+
+ /* hostname as numeric name */
+ for (i = 0; gai_afdl[i].a_af; i++) {
+ if (inet_pton(gai_afdl[i].a_af, hostname, pton)) {
+ u_long v4a;
#ifdef ENABLE_IPV6
- u_char pfx;
+ u_char pfx;
#endif
- switch (gai_afdl[i].a_af) {
- case AF_INET:
- v4a = ((struct in_addr *)pton)->s_addr;
- v4a = ntohl(v4a);
- if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
- pai->ai_flags &= ~AI_CANONNAME;
- v4a >>= IN_CLASSA_NSHIFT;
- if (v4a == 0 || v4a == IN_LOOPBACKNET)
- pai->ai_flags &= ~AI_CANONNAME;
- break;
+ switch (gai_afdl[i].a_af) {
+ case AF_INET:
+ v4a = ((struct in_addr *)pton)->s_addr;
+ v4a = ntohl(v4a);
+ if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
+ pai->ai_flags &= ~AI_CANONNAME;
+ v4a >>= IN_CLASSA_NSHIFT;
+ if (v4a == 0 || v4a == IN_LOOPBACKNET)
+ pai->ai_flags &= ~AI_CANONNAME;
+ break;
#ifdef ENABLE_IPV6
- case AF_INET6:
- pfx = ((struct in6_addr *)pton)->s6_addr8[0];
- if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
- pai->ai_flags &= ~AI_CANONNAME;
- break;
+ case AF_INET6:
+ pfx = ((struct in6_addr *)pton)->s6_addr8[0];
+ if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
+ pai->ai_flags &= ~AI_CANONNAME;
+ break;
#endif
- }
-
- if (pai->ai_family == gai_afdl[i].a_af ||
- pai->ai_family == PF_UNSPEC) {
- if (! (pai->ai_flags & AI_CANONNAME)) {
- GET_AI(top, &gai_afdl[i], pton, port);
- goto good;
- }
- /*
- * if AI_CANONNAME and if reverse lookup
- * fail, return ai anyway to pacify
- * calling application.
- *
- * XXX getaddrinfo() is a name->address
- * translation function, and it looks strange
- * that we do addr->name translation here.
- */
- get_name(pton, &gai_afdl[i], &top, pton, pai, port);
- goto good;
- } else
- ERR(EAI_FAMILY); /*xxx*/
- }
- }
-
- if (pai->ai_flags & AI_NUMERICHOST)
- ERR(EAI_NONAME);
-
- /* hostname as alphabetical name */
- error = get_addr(hostname, pai->ai_family, &top, pai, port);
- if (error == 0) {
- if (top) {
+ }
+
+ if (pai->ai_family == gai_afdl[i].a_af ||
+ pai->ai_family == PF_UNSPEC) {
+ if (! (pai->ai_flags & AI_CANONNAME)) {
+ GET_AI(top, &gai_afdl[i], pton, port);
+ goto good;
+ }
+ /*
+ * if AI_CANONNAME and if reverse lookup
+ * fail, return ai anyway to pacify
+ * calling application.
+ *
+ * XXX getaddrinfo() is a name->address
+ * translation function, and it looks strange
+ * that we do addr->name translation here.
+ */
+ get_name(pton, &gai_afdl[i], &top, pton, pai, port);
+ goto good;
+ } else
+ ERR(EAI_FAMILY); /*xxx*/
+ }
+ }
+
+ if (pai->ai_flags & AI_NUMERICHOST)
+ ERR(EAI_NONAME);
+
+ /* hostname as alphabetical name */
+ error = get_addr(hostname, pai->ai_family, &top, pai, port);
+ if (error == 0) {
+ if (top) {
good:
- *res = top;
- return SUCCESS;
- } else
- error = EAI_FAIL;
- }
+ *res = top;
+ return SUCCESS;
+ } else
+ error = EAI_FAIL;
+ }
free:
- if (top)
- freeaddrinfo(top);
+ if (top)
+ freeaddrinfo(top);
bad:
- *res = NULL;
- return error;
+ *res = NULL;
+ return error;
}
static int
get_name(addr, gai_afd, res, numaddr, pai, port0)
- const char *addr;
- struct gai_afd *gai_afd;
- struct addrinfo **res;
- char *numaddr;
- struct addrinfo *pai;
- int port0;
+ const char *addr;
+ struct gai_afd *gai_afd;
+ struct addrinfo **res;
+ char *numaddr;
+ struct addrinfo *pai;
+ int port0;
{
- u_short port = port0 & 0xffff;
- struct hostent *hp;
- struct addrinfo *cur;
- int error = 0;
+ u_short port = port0 & 0xffff;
+ struct hostent *hp;
+ struct addrinfo *cur;
+ int error = 0;
#ifdef ENABLE_IPV6
- int h_error;
+ int h_error;
#endif
-
+
#ifdef ENABLE_IPV6
- hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error);
+ hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error);
#else
- hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET);
+ hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET);
#endif
- if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
- GET_AI(cur, gai_afd, hp->h_addr_list[0], port);
- GET_CANONNAME(cur, hp->h_name);
- } else
- GET_AI(cur, gai_afd, numaddr, port);
-
+ if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
+ GET_AI(cur, gai_afd, hp->h_addr_list[0], port);
+ GET_CANONNAME(cur, hp->h_name);
+ } else
+ GET_AI(cur, gai_afd, numaddr, port);
+
#ifdef ENABLE_IPV6
- if (hp)
- freehostent(hp);
+ if (hp)
+ freehostent(hp);
#endif
- *res = cur;
- return SUCCESS;
+ *res = cur;
+ return SUCCESS;
free:
- if (cur)
- freeaddrinfo(cur);
+ if (cur)
+ freeaddrinfo(cur);
#ifdef ENABLE_IPV6
- if (hp)
- freehostent(hp);
+ if (hp)
+ freehostent(hp);
#endif
/* bad: */
- *res = NULL;
- return error;
+ *res = NULL;
+ return error;
}
static int
get_addr(hostname, af, res, pai, port0)
- const char *hostname;
- int af;
- struct addrinfo **res;
- struct addrinfo *pai;
- int port0;
+ const char *hostname;
+ int af;
+ struct addrinfo **res;
+ struct addrinfo *pai;
+ int port0;
{
- u_short port = port0 & 0xffff;
- struct addrinfo sentinel;
- struct hostent *hp;
- struct addrinfo *top, *cur;
- struct gai_afd *gai_afd;
- int i, error = 0, h_error;
- char *ap;
-
- top = NULL;
- sentinel.ai_next = NULL;
- cur = &sentinel;
+ u_short port = port0 & 0xffff;
+ struct addrinfo sentinel;
+ struct hostent *hp;
+ struct addrinfo *top, *cur;
+ struct gai_afd *gai_afd;
+ int i, error = 0, h_error;
+ char *ap;
+
+ top = NULL;
+ sentinel.ai_next = NULL;
+ cur = &sentinel;
#ifdef ENABLE_IPV6
- if (af == AF_UNSPEC) {
- hp = getipnodebyname(hostname, AF_INET6,
- AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
- } else
- hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
+ if (af == AF_UNSPEC) {
+ hp = getipnodebyname(hostname, AF_INET6,
+ AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
+ } else
+ hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
#else
- hp = gethostbyname(hostname);
- h_error = h_errno;
+ hp = gethostbyname(hostname);
+ h_error = h_errno;
#endif
- if (hp == NULL) {
- switch (h_error) {
- case HOST_NOT_FOUND:
- case NO_DATA:
- error = EAI_NODATA;
- break;
- case TRY_AGAIN:
- error = EAI_AGAIN;
- break;
- case NO_RECOVERY:
- default:
- error = EAI_FAIL;
- break;
- }
- goto free;
- }
-
- if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
- (hp->h_addr_list[0] == NULL)) {
- error = EAI_FAIL;
- goto free;
- }
-
- for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
- switch (af) {
+ if (hp == NULL) {
+ switch (h_error) {
+ case HOST_NOT_FOUND:
+ case NO_DATA:
+ error = EAI_NODATA;
+ break;
+ case TRY_AGAIN:
+ error = EAI_AGAIN;
+ break;
+ case NO_RECOVERY:
+ default:
+ error = EAI_FAIL;
+ break;
+ }
+ goto free;
+ }
+
+ if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
+ (hp->h_addr_list[0] == NULL)) {
+ error = EAI_FAIL;
+ goto free;
+ }
+
+ for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
+ switch (af) {
#ifdef ENABLE_IPV6
- case AF_INET6:
- gai_afd = &gai_afdl[N_INET6];
- break;
+ case AF_INET6:
+ gai_afd = &gai_afdl[N_INET6];
+ break;
#endif
#ifndef ENABLE_IPV6
- default: /* AF_UNSPEC */
+ default: /* AF_UNSPEC */
#endif
- case AF_INET:
- gai_afd = &gai_afdl[N_INET];
- break;
+ case AF_INET:
+ gai_afd = &gai_afdl[N_INET];
+ break;
#ifdef ENABLE_IPV6
- default: /* AF_UNSPEC */
- if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
- ap += sizeof(struct in6_addr) -
- sizeof(struct in_addr);
- gai_afd = &gai_afdl[N_INET];
- } else
- gai_afd = &gai_afdl[N_INET6];
- break;
+ default: /* AF_UNSPEC */
+ if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
+ ap += sizeof(struct in6_addr) -
+ sizeof(struct in_addr);
+ gai_afd = &gai_afdl[N_INET];
+ } else
+ gai_afd = &gai_afdl[N_INET6];
+ break;
#endif
- }
+ }
#ifdef FAITH
- if (translate && gai_afd->a_af == AF_INET) {
- struct in6_addr *in6;
-
- GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port);
- in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
- memcpy(&in6->s6_addr32[0], &faith_prefix,
- sizeof(struct in6_addr) - sizeof(struct in_addr));
- memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
- } else
+ if (translate && gai_afd->a_af == AF_INET) {
+ struct in6_addr *in6;
+
+ GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port);
+ in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
+ memcpy(&in6->s6_addr32[0], &faith_prefix,
+ sizeof(struct in6_addr) - sizeof(struct in_addr));
+ memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
+ } else
#endif /* FAITH */
- GET_AI(cur->ai_next, gai_afd, ap, port);
- if (cur == &sentinel) {
- top = cur->ai_next;
- GET_CANONNAME(top, hp->h_name);
- }
- cur = cur->ai_next;
- }
+ GET_AI(cur->ai_next, gai_afd, ap, port);
+ if (cur == &sentinel) {
+ top = cur->ai_next;
+ GET_CANONNAME(top, hp->h_name);
+ }
+ cur = cur->ai_next;
+ }
#ifdef ENABLE_IPV6
- freehostent(hp);
+ freehostent(hp);
#endif
- *res = top;
- return SUCCESS;
+ *res = top;
+ return SUCCESS;
free:
- if (top)
- freeaddrinfo(top);
+ if (top)
+ freeaddrinfo(top);
#ifdef ENABLE_IPV6
- if (hp)
- freehostent(hp);
+ if (hp)
+ freehostent(hp);
#endif
/* bad: */
- *res = NULL;
- return error;
+ *res = NULL;
+ return error;
}
diff --git a/Modules/getbuildinfo.c b/Modules/getbuildinfo.c
index f4bd14a..3bac1c5 100644
--- a/Modules/getbuildinfo.c
+++ b/Modules/getbuildinfo.c
@@ -31,22 +31,22 @@
const char *
Py_GetBuildInfo(void)
{
- static char buildinfo[50];
- const char *revision = Py_SubversionRevision();
- const char *sep = *revision ? ":" : "";
- const char *branch = Py_SubversionShortBranch();
- PyOS_snprintf(buildinfo, sizeof(buildinfo),
- "%s%s%s, %.20s, %.9s", branch, sep, revision,
- DATE, TIME);
- return buildinfo;
+ static char buildinfo[50];
+ const char *revision = Py_SubversionRevision();
+ const char *sep = *revision ? ":" : "";
+ const char *branch = Py_SubversionShortBranch();
+ PyOS_snprintf(buildinfo, sizeof(buildinfo),
+ "%s%s%s, %.20s, %.9s", branch, sep, revision,
+ DATE, TIME);
+ return buildinfo;
}
const char *
_Py_svnversion(void)
{
- /* the following string can be modified by subwcrev.exe */
- static const char svnversion[] = SVNVERSION;
- if (svnversion[0] != '$')
- return svnversion; /* it was interpolated, or passed on command line */
- return "Unversioned directory";
+ /* the following string can be modified by subwcrev.exe */
+ static const char svnversion[] = SVNVERSION;
+ if (svnversion[0] != '$')
+ return svnversion; /* it was interpolated, or passed on command line */
+ return "Unversioned directory";
}
diff --git a/Modules/getnameinfo.c b/Modules/getnameinfo.c
index 0190a32..e9aeafb 100644
--- a/Modules/getnameinfo.c
+++ b/Modules/getnameinfo.c
@@ -1,7 +1,7 @@
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -13,7 +13,7 @@
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -53,162 +53,162 @@
#define NO 0
static struct gni_afd {
- int a_af;
- int a_addrlen;
- int a_socklen;
- int a_off;
+ int a_af;
+ int a_addrlen;
+ int a_socklen;
+ int a_off;
} gni_afdl [] = {
#ifdef ENABLE_IPV6
- {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
- offsetof(struct sockaddr_in6, sin6_addr)},
+ {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
+ offsetof(struct sockaddr_in6, sin6_addr)},
#endif
- {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
- offsetof(struct sockaddr_in, sin_addr)},
- {0, 0, 0},
+ {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
+ offsetof(struct sockaddr_in, sin_addr)},
+ {0, 0, 0},
};
struct gni_sockinet {
- u_char si_len;
- u_char si_family;
- u_short si_port;
+ u_char si_len;
+ u_char si_family;
+ u_short si_port;
};
-#define ENI_NOSOCKET 0
-#define ENI_NOSERVNAME 1
-#define ENI_NOHOSTNAME 2
-#define ENI_MEMORY 3
-#define ENI_SYSTEM 4
-#define ENI_FAMILY 5
-#define ENI_SALEN 6
+#define ENI_NOSOCKET 0
+#define ENI_NOSERVNAME 1
+#define ENI_NOHOSTNAME 2
+#define ENI_MEMORY 3
+#define ENI_SYSTEM 4
+#define ENI_FAMILY 5
+#define ENI_SALEN 6
/* forward declaration to make gcc happy */
int getnameinfo(const struct sockaddr *, size_t, char *, size_t,
- char *, size_t, int);
+ char *, size_t, int);
int
getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
- const struct sockaddr *sa;
- size_t salen;
- char *host;
- size_t hostlen;
- char *serv;
- size_t servlen;
- int flags;
+ const struct sockaddr *sa;
+ size_t salen;
+ char *host;
+ size_t hostlen;
+ char *serv;
+ size_t servlen;
+ int flags;
{
- struct gni_afd *gni_afd;
- struct servent *sp;
- struct hostent *hp;
- u_short port;
- int family, len, i;
- char *addr, *p;
- u_long v4a;
+ struct gni_afd *gni_afd;
+ struct servent *sp;
+ struct hostent *hp;
+ u_short port;
+ int family, len, i;
+ char *addr, *p;
+ u_long v4a;
#ifdef ENABLE_IPV6
- u_char pfx;
+ u_char pfx;
#endif
- int h_error;
- char numserv[512];
- char numaddr[512];
+ int h_error;
+ char numserv[512];
+ char numaddr[512];
- if (sa == NULL)
- return ENI_NOSOCKET;
+ if (sa == NULL)
+ return ENI_NOSOCKET;
#ifdef HAVE_SOCKADDR_SA_LEN
- len = sa->sa_len;
- if (len != salen) return ENI_SALEN;
+ len = sa->sa_len;
+ if (len != salen) return ENI_SALEN;
#else
- len = salen;
+ len = salen;
#endif
-
- family = sa->sa_family;
- for (i = 0; gni_afdl[i].a_af; i++)
- if (gni_afdl[i].a_af == family) {
- gni_afd = &gni_afdl[i];
- goto found;
- }
- return ENI_FAMILY;
-
+
+ family = sa->sa_family;
+ for (i = 0; gni_afdl[i].a_af; i++)
+ if (gni_afdl[i].a_af == family) {
+ gni_afd = &gni_afdl[i];
+ goto found;
+ }
+ return ENI_FAMILY;
+
found:
- if (len != gni_afd->a_socklen) return ENI_SALEN;
-
- port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */
- addr = (char *)sa + gni_afd->a_off;
-
- if (serv == NULL || servlen == 0) {
- /* what we should do? */
- } else if (flags & NI_NUMERICSERV) {
- sprintf(numserv, "%d", ntohs(port));
- if (strlen(numserv) > servlen)
- return ENI_MEMORY;
- strcpy(serv, numserv);
- } else {
- sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
- if (sp) {
- if (strlen(sp->s_name) > servlen)
- return ENI_MEMORY;
- strcpy(serv, sp->s_name);
- } else
- return ENI_NOSERVNAME;
- }
-
- switch (sa->sa_family) {
- case AF_INET:
- v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
- if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
- flags |= NI_NUMERICHOST;
- v4a >>= IN_CLASSA_NSHIFT;
- if (v4a == 0 || v4a == IN_LOOPBACKNET)
- flags |= NI_NUMERICHOST;
- break;
+ if (len != gni_afd->a_socklen) return ENI_SALEN;
+
+ port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */
+ addr = (char *)sa + gni_afd->a_off;
+
+ if (serv == NULL || servlen == 0) {
+ /* what we should do? */
+ } else if (flags & NI_NUMERICSERV) {
+ sprintf(numserv, "%d", ntohs(port));
+ if (strlen(numserv) > servlen)
+ return ENI_MEMORY;
+ strcpy(serv, numserv);
+ } else {
+ sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
+ if (sp) {
+ if (strlen(sp->s_name) > servlen)
+ return ENI_MEMORY;
+ strcpy(serv, sp->s_name);
+ } else
+ return ENI_NOSERVNAME;
+ }
+
+ switch (sa->sa_family) {
+ case AF_INET:
+ v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
+ if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
+ flags |= NI_NUMERICHOST;
+ v4a >>= IN_CLASSA_NSHIFT;
+ if (v4a == 0 || v4a == IN_LOOPBACKNET)
+ flags |= NI_NUMERICHOST;
+ break;
#ifdef ENABLE_IPV6
- case AF_INET6:
- pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
- if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
- flags |= NI_NUMERICHOST;
- break;
+ case AF_INET6:
+ pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0];
+ if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
+ flags |= NI_NUMERICHOST;
+ break;
#endif
- }
- if (host == NULL || hostlen == 0) {
- /* what should we do? */
- } else if (flags & NI_NUMERICHOST) {
- if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr))
- == NULL)
- return ENI_SYSTEM;
- if (strlen(numaddr) > hostlen)
- return ENI_MEMORY;
- strcpy(host, numaddr);
- } else {
+ }
+ if (host == NULL || hostlen == 0) {
+ /* what should we do? */
+ } else if (flags & NI_NUMERICHOST) {
+ if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr))
+ == NULL)
+ return ENI_SYSTEM;
+ if (strlen(numaddr) > hostlen)
+ return ENI_MEMORY;
+ strcpy(host, numaddr);
+ } else {
#ifdef ENABLE_IPV6
- hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error);
+ hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error);
#else
- hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af);
- h_error = h_errno;
+ hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af);
+ h_error = h_errno;
#endif
- if (hp) {
- if (flags & NI_NOFQDN) {
- p = strchr(hp->h_name, '.');
- if (p) *p = '\0';
- }
- if (strlen(hp->h_name) > hostlen) {
+ if (hp) {
+ if (flags & NI_NOFQDN) {
+ p = strchr(hp->h_name, '.');
+ if (p) *p = '\0';
+ }
+ if (strlen(hp->h_name) > hostlen) {
#ifdef ENABLE_IPV6
- freehostent(hp);
+ freehostent(hp);
#endif
- return ENI_MEMORY;
- }
- strcpy(host, hp->h_name);
+ return ENI_MEMORY;
+ }
+ strcpy(host, hp->h_name);
#ifdef ENABLE_IPV6
- freehostent(hp);
+ freehostent(hp);
#endif
- } else {
- if (flags & NI_NAMEREQD)
- return ENI_NOHOSTNAME;
- if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr))
- == NULL)
- return ENI_NOHOSTNAME;
- if (strlen(numaddr) > hostlen)
- return ENI_MEMORY;
- strcpy(host, numaddr);
- }
- }
- return SUCCESS;
+ } else {
+ if (flags & NI_NAMEREQD)
+ return ENI_NOHOSTNAME;
+ if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr))
+ == NULL)
+ return ENI_NOHOSTNAME;
+ if (strlen(numaddr) > hostlen)
+ return ENI_MEMORY;
+ strcpy(host, numaddr);
+ }
+ }
+ return SUCCESS;
}
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 3cbfa42..3d45235 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -142,8 +142,8 @@ _wstat(const wchar_t* path, struct stat *buf)
char fname[PATH_MAX];
size_t res = wcstombs(fname, path, sizeof(fname));
if (res == (size_t)-1) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
}
return stat(fname, buf);
}
@@ -155,17 +155,17 @@ _wgetcwd(wchar_t *buf, size_t size)
{
char fname[PATH_MAX];
if (getcwd(fname, PATH_MAX) == NULL)
- return NULL;
+ return NULL;
if (mbstowcs(buf, fname, size) >= size) {
- errno = ERANGE;
- return NULL;
+ errno = ERANGE;
+ return NULL;
}
return buf;
}
#endif
#ifdef HAVE_READLINK
-int
+int
_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
{
char cbuf[PATH_MAX];
@@ -173,24 +173,24 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
int res;
size_t r1 = wcstombs(cpath, path, PATH_MAX);
if (r1 == (size_t)-1 || r1 >= PATH_MAX) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
}
res = (int)readlink(cpath, cbuf, PATH_MAX);
if (res == -1)
- return -1;
+ return -1;
if (res == PATH_MAX) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
}
cbuf[res] = '\0'; /* buf will be null terminated */
r1 = mbstowcs(buf, cbuf, bufsiz);
if (r1 == -1) {
- errno = EINVAL;
- return -1;
+ errno = EINVAL;
+ return -1;
}
return (int)r1;
-
+
}
#endif
@@ -279,7 +279,7 @@ joinpath(wchar_t *buffer, wchar_t *stuff)
buffer[n++] = SEP;
}
if (n > MAXPATHLEN)
- Py_FatalError("buffer overflow in getpath.c's joinpath()");
+ Py_FatalError("buffer overflow in getpath.c's joinpath()");
k = wcslen(stuff);
if (n + k > MAXPATHLEN)
k = MAXPATHLEN - n;
@@ -457,25 +457,25 @@ calculate_path(void)
#else
unsigned long nsexeclength = MAXPATHLEN;
#endif
- char execpath[MAXPATHLEN+1];
+ char execpath[MAXPATHLEN+1];
#endif
if (_path) {
- size_t r = mbstowcs(wpath, _path, MAXPATHLEN+1);
- path = wpath;
- if (r == (size_t)-1 || r > MAXPATHLEN) {
- /* Could not convert PATH, or it's too long. */
- path = NULL;
- }
+ size_t r = mbstowcs(wpath, _path, MAXPATHLEN+1);
+ path = wpath;
+ if (r == (size_t)-1 || r > MAXPATHLEN) {
+ /* Could not convert PATH, or it's too long. */
+ path = NULL;
+ }
}
- /* If there is no slash in the argv0 path, then we have to
- * assume python is on the user's $PATH, since there's no
- * other way to find a directory to start the search from. If
- * $PATH isn't exported, you lose.
- */
- if (wcschr(prog, SEP))
- wcsncpy(progpath, prog, MAXPATHLEN);
+ /* If there is no slash in the argv0 path, then we have to
+ * assume python is on the user's $PATH, since there's no
+ * other way to find a directory to start the search from. If
+ * $PATH isn't exported, you lose.
+ */
+ if (wcschr(prog, SEP))
+ wcsncpy(progpath, prog, MAXPATHLEN);
#ifdef __APPLE__
/* On Mac OS X, if a script uses an interpreter of the form
* "#!/opt/python2.3/bin/python", the kernel only passes "python"
@@ -487,52 +487,52 @@ calculate_path(void)
* will fail if a relative path was used. but in that case,
* absolutize() should help us out below
*/
- else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) {
- size_t r = mbstowcs(progpath, execpath, MAXPATHLEN+1);
- if (r == (size_t)-1 || r > MAXPATHLEN) {
- /* Could not convert execpath, or it's too long. */
- progpath[0] = '\0';
- }
- }
+ else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) {
+ size_t r = mbstowcs(progpath, execpath, MAXPATHLEN+1);
+ if (r == (size_t)-1 || r > MAXPATHLEN) {
+ /* Could not convert execpath, or it's too long. */
+ progpath[0] = '\0';
+ }
+ }
#endif /* __APPLE__ */
- else if (path) {
- while (1) {
- wchar_t *delim = wcschr(path, DELIM);
-
- if (delim) {
- size_t len = delim - path;
- if (len > MAXPATHLEN)
- len = MAXPATHLEN;
- wcsncpy(progpath, path, len);
- *(progpath + len) = '\0';
- }
- else
- wcsncpy(progpath, path, MAXPATHLEN);
-
- joinpath(progpath, prog);
- if (isxfile(progpath))
- break;
-
- if (!delim) {
- progpath[0] = L'\0';
- break;
- }
- path = delim + 1;
- }
- }
- else
- progpath[0] = '\0';
- if (progpath[0] != SEP && progpath[0] != '\0')
- absolutize(progpath);
- wcsncpy(argv0_path, progpath, MAXPATHLEN);
- argv0_path[MAXPATHLEN] = '\0';
+ else if (path) {
+ while (1) {
+ wchar_t *delim = wcschr(path, DELIM);
+
+ if (delim) {
+ size_t len = delim - path;
+ if (len > MAXPATHLEN)
+ len = MAXPATHLEN;
+ wcsncpy(progpath, path, len);
+ *(progpath + len) = '\0';
+ }
+ else
+ wcsncpy(progpath, path, MAXPATHLEN);
+
+ joinpath(progpath, prog);
+ if (isxfile(progpath))
+ break;
+
+ if (!delim) {
+ progpath[0] = L'\0';
+ break;
+ }
+ path = delim + 1;
+ }
+ }
+ else
+ progpath[0] = '\0';
+ if (progpath[0] != SEP && progpath[0] != '\0')
+ absolutize(progpath);
+ wcsncpy(argv0_path, progpath, MAXPATHLEN);
+ argv0_path[MAXPATHLEN] = '\0';
#ifdef WITH_NEXT_FRAMEWORK
- /* On Mac OS X we have a special case if we're running from a framework.
- ** This is because the python home should be set relative to the library,
- ** which is in the framework, not relative to the executable, which may
- ** be outside of the framework. Except when we're in the build directory...
- */
+ /* On Mac OS X we have a special case if we're running from a framework.
+ ** This is because the python home should be set relative to the library,
+ ** which is in the framework, not relative to the executable, which may
+ ** be outside of the framework. Except when we're in the build directory...
+ */
pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
/* Use dylib functions to find out where the framework was loaded from */
buf = (wchar_t *)NSLibraryNameForModule(pythonModule);
@@ -604,7 +604,7 @@ calculate_path(void)
else
wcsncpy(zip_path, L"" PREFIX, MAXPATHLEN);
joinpath(zip_path, L"lib/python00.zip");
- bufsz = wcslen(zip_path); /* Replace "00" with version */
+ bufsz = wcslen(zip_path); /* Replace "00" with version */
zip_path[bufsz - 6] = VERSION[0];
zip_path[bufsz - 5] = VERSION[2];
@@ -626,12 +626,12 @@ calculate_path(void)
bufsz = 0;
if (_rtpypath) {
- size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t));
- if (s == (size_t)-1 || s >=sizeof(rtpypath))
- /* XXX deal with errors more gracefully */
- _rtpypath = NULL;
- if (_rtpypath)
- bufsz += wcslen(rtpypath) + 1;
+ size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t));
+ if (s == (size_t)-1 || s >=sizeof(rtpypath))
+ /* XXX deal with errors more gracefully */
+ _rtpypath = NULL;
+ if (_rtpypath)
+ bufsz += wcslen(rtpypath) + 1;
}
prefixsz = wcslen(prefix) + 1;
@@ -718,10 +718,10 @@ calculate_path(void)
if (pfound > 0) {
reduce(prefix);
reduce(prefix);
- /* The prefix is the root directory, but reduce() chopped
- * off the "/". */
- if (!prefix[0])
- wcscpy(prefix, separator);
+ /* The prefix is the root directory, but reduce() chopped
+ * off the "/". */
+ if (!prefix[0])
+ wcscpy(prefix, separator);
}
else
wcsncpy(prefix, L"" PREFIX, MAXPATHLEN);
@@ -730,8 +730,8 @@ calculate_path(void)
reduce(exec_prefix);
reduce(exec_prefix);
reduce(exec_prefix);
- if (!exec_prefix[0])
- wcscpy(exec_prefix, separator);
+ if (!exec_prefix[0])
+ wcscpy(exec_prefix, separator);
}
else
wcsncpy(exec_prefix, L"" EXEC_PREFIX, MAXPATHLEN);
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 0dcef06..ca3a358 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -10,8 +10,8 @@
static PyStructSequence_Field struct_group_type_fields[] = {
{"gr_name", "group name"},
{"gr_passwd", "password"},
- {"gr_gid", "group id"},
- {"gr_mem", "group memebers"},
+ {"gr_gid", "group id"},
+ {"gr_mem", "group memebers"},
{0}
};
@@ -67,10 +67,10 @@ mkgrent(struct group *p)
Py_INCREF(Py_None);
#else
if (p->gr_passwd)
- SET(setIndex++, FSDECODE(p->gr_passwd));
+ SET(setIndex++, FSDECODE(p->gr_passwd));
else {
- SET(setIndex++, Py_None);
- Py_INCREF(Py_None);
+ SET(setIndex++, Py_None);
+ Py_INCREF(Py_None);
}
#endif
SET(setIndex++, PyLong_FromLong((long) p->gr_gid));
@@ -94,12 +94,12 @@ grp_getgrgid(PyObject *self, PyObject *pyo_id)
py_int_id = PyNumber_Long(pyo_id);
if (!py_int_id)
- return NULL;
+ return NULL;
gid = PyLong_AS_LONG(py_int_id);
Py_DECREF(py_int_id);
if ((p = getgrgid(gid)) == NULL) {
- PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
+ PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
return NULL;
}
return mkgrent(p);
@@ -119,9 +119,9 @@ grp_getgrnam(PyObject *self, PyObject *args)
return NULL;
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
goto out;
-
+
if ((p = getgrnam(name)) == NULL) {
- PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
+ PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
goto out;
}
retval = mkgrent(p);
@@ -154,18 +154,18 @@ grp_getgrall(PyObject *self, PyObject *ignore)
}
static PyMethodDef grp_methods[] = {
- {"getgrgid", grp_getgrgid, METH_O,
+ {"getgrgid", grp_getgrgid, METH_O,
"getgrgid(id) -> tuple\n\
Return the group database entry for the given numeric group ID. If\n\
id is not valid, raise KeyError."},
- {"getgrnam", grp_getgrnam, METH_VARARGS,
+ {"getgrnam", grp_getgrnam, METH_VARARGS,
"getgrnam(name) -> tuple\n\
Return the group database entry for the given group name. If\n\
name is not valid, raise KeyError."},
- {"getgrall", grp_getgrall, METH_NOARGS,
+ {"getgrall", grp_getgrall, METH_NOARGS,
"getgrall() -> list of tuples\n\
Return a list of all available group entries, in arbitrary order."},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(grp__doc__,
@@ -187,15 +187,15 @@ complete membership information.)");
static struct PyModuleDef grpmodule = {
- PyModuleDef_HEAD_INIT,
- "grp",
- grp__doc__,
- -1,
- grp_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "grp",
+ grp__doc__,
+ -1,
+ grp_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -207,7 +207,7 @@ PyInit_grp(void)
return NULL;
d = PyModule_GetDict(m);
if (!initialized)
- PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
+ PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
initialized = 1;
return m;
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 74396b6..ad95047 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2,7 +2,7 @@
#include "Python.h"
#include "structmember.h"
-/* Itertools module written and maintained
+/* Itertools module written and maintained
by Raymond D. Hettinger <python@rcn.com>
Copyright (c) 2003 Python Software Foundation.
All rights reserved.
@@ -12,12 +12,12 @@
/* groupby object ***********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *it;
- PyObject *keyfunc;
- PyObject *tgtkey;
- PyObject *currkey;
- PyObject *currvalue;
+ PyObject_HEAD
+ PyObject *it;
+ PyObject *keyfunc;
+ PyObject *tgtkey;
+ PyObject *currkey;
+ PyObject *currvalue;
} groupbyobject;
static PyTypeObject groupby_type;
@@ -26,112 +26,112 @@ static PyObject *_grouper_create(groupbyobject *, PyObject *);
static PyObject *
groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- static char *kwargs[] = {"iterable", "key", NULL};
- groupbyobject *gbo;
- PyObject *it, *keyfunc = Py_None;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs,
- &it, &keyfunc))
- return NULL;
-
- gbo = (groupbyobject *)type->tp_alloc(type, 0);
- if (gbo == NULL)
- return NULL;
- gbo->tgtkey = NULL;
- gbo->currkey = NULL;
- gbo->currvalue = NULL;
- gbo->keyfunc = keyfunc;
- Py_INCREF(keyfunc);
- gbo->it = PyObject_GetIter(it);
- if (gbo->it == NULL) {
- Py_DECREF(gbo);
- return NULL;
- }
- return (PyObject *)gbo;
+ static char *kwargs[] = {"iterable", "key", NULL};
+ groupbyobject *gbo;
+ PyObject *it, *keyfunc = Py_None;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs,
+ &it, &keyfunc))
+ return NULL;
+
+ gbo = (groupbyobject *)type->tp_alloc(type, 0);
+ if (gbo == NULL)
+ return NULL;
+ gbo->tgtkey = NULL;
+ gbo->currkey = NULL;
+ gbo->currvalue = NULL;
+ gbo->keyfunc = keyfunc;
+ Py_INCREF(keyfunc);
+ gbo->it = PyObject_GetIter(it);
+ if (gbo->it == NULL) {
+ Py_DECREF(gbo);
+ return NULL;
+ }
+ return (PyObject *)gbo;
}
static void
groupby_dealloc(groupbyobject *gbo)
{
- PyObject_GC_UnTrack(gbo);
- Py_XDECREF(gbo->it);
- Py_XDECREF(gbo->keyfunc);
- Py_XDECREF(gbo->tgtkey);
- Py_XDECREF(gbo->currkey);
- Py_XDECREF(gbo->currvalue);
- Py_TYPE(gbo)->tp_free(gbo);
+ PyObject_GC_UnTrack(gbo);
+ Py_XDECREF(gbo->it);
+ Py_XDECREF(gbo->keyfunc);
+ Py_XDECREF(gbo->tgtkey);
+ Py_XDECREF(gbo->currkey);
+ Py_XDECREF(gbo->currvalue);
+ Py_TYPE(gbo)->tp_free(gbo);
}
static int
groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
{
- Py_VISIT(gbo->it);
- Py_VISIT(gbo->keyfunc);
- Py_VISIT(gbo->tgtkey);
- Py_VISIT(gbo->currkey);
- Py_VISIT(gbo->currvalue);
- return 0;
+ Py_VISIT(gbo->it);
+ Py_VISIT(gbo->keyfunc);
+ Py_VISIT(gbo->tgtkey);
+ Py_VISIT(gbo->currkey);
+ Py_VISIT(gbo->currvalue);
+ return 0;
}
static PyObject *
groupby_next(groupbyobject *gbo)
{
- PyObject *newvalue, *newkey, *r, *grouper, *tmp;
-
- /* skip to next iteration group */
- for (;;) {
- if (gbo->currkey == NULL)
- /* pass */;
- else if (gbo->tgtkey == NULL)
- break;
- else {
- int rcmp;
-
- rcmp = PyObject_RichCompareBool(gbo->tgtkey,
- gbo->currkey, Py_EQ);
- if (rcmp == -1)
- return NULL;
- else if (rcmp == 0)
- break;
- }
-
- newvalue = PyIter_Next(gbo->it);
- if (newvalue == NULL)
- return NULL;
-
- if (gbo->keyfunc == Py_None) {
- newkey = newvalue;
- Py_INCREF(newvalue);
- } else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
- newvalue, NULL);
- if (newkey == NULL) {
- Py_DECREF(newvalue);
- return NULL;
- }
- }
-
- tmp = gbo->currkey;
- gbo->currkey = newkey;
- Py_XDECREF(tmp);
-
- tmp = gbo->currvalue;
- gbo->currvalue = newvalue;
- Py_XDECREF(tmp);
- }
-
- Py_INCREF(gbo->currkey);
- tmp = gbo->tgtkey;
- gbo->tgtkey = gbo->currkey;
- Py_XDECREF(tmp);
-
- grouper = _grouper_create(gbo, gbo->tgtkey);
- if (grouper == NULL)
- return NULL;
-
- r = PyTuple_Pack(2, gbo->currkey, grouper);
- Py_DECREF(grouper);
- return r;
+ PyObject *newvalue, *newkey, *r, *grouper, *tmp;
+
+ /* skip to next iteration group */
+ for (;;) {
+ if (gbo->currkey == NULL)
+ /* pass */;
+ else if (gbo->tgtkey == NULL)
+ break;
+ else {
+ int rcmp;
+
+ rcmp = PyObject_RichCompareBool(gbo->tgtkey,
+ gbo->currkey, Py_EQ);
+ if (rcmp == -1)
+ return NULL;
+ else if (rcmp == 0)
+ break;
+ }
+
+ newvalue = PyIter_Next(gbo->it);
+ if (newvalue == NULL)
+ return NULL;
+
+ if (gbo->keyfunc == Py_None) {
+ newkey = newvalue;
+ Py_INCREF(newvalue);
+ } else {
+ newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
+ newvalue, NULL);
+ if (newkey == NULL) {
+ Py_DECREF(newvalue);
+ return NULL;
+ }
+ }
+
+ tmp = gbo->currkey;
+ gbo->currkey = newkey;
+ Py_XDECREF(tmp);
+
+ tmp = gbo->currvalue;
+ gbo->currvalue = newvalue;
+ Py_XDECREF(tmp);
+ }
+
+ Py_INCREF(gbo->currkey);
+ tmp = gbo->tgtkey;
+ gbo->tgtkey = gbo->currkey;
+ Py_XDECREF(tmp);
+
+ grouper = _grouper_create(gbo, gbo->tgtkey);
+ if (grouper == NULL)
+ return NULL;
+
+ r = PyTuple_Pack(2, gbo->currkey, grouper);
+ Py_DECREF(grouper);
+ return r;
}
PyDoc_STRVAR(groupby_doc,
@@ -139,56 +139,56 @@ PyDoc_STRVAR(groupby_doc,
(key, sub-iterator) grouped by each value of key(value).\n");
static PyTypeObject groupby_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.groupby", /* tp_name */
- sizeof(groupbyobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)groupby_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- groupby_doc, /* tp_doc */
- (traverseproc)groupby_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)groupby_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- groupby_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.groupby", /* tp_name */
+ sizeof(groupbyobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)groupby_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ groupby_doc, /* tp_doc */
+ (traverseproc)groupby_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)groupby_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ groupby_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* _grouper object (internal) ************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *parent;
- PyObject *tgtkey;
+ PyObject_HEAD
+ PyObject *parent;
+ PyObject *tgtkey;
} _grouperobject;
static PyTypeObject _grouper_type;
@@ -196,129 +196,129 @@ static PyTypeObject _grouper_type;
static PyObject *
_grouper_create(groupbyobject *parent, PyObject *tgtkey)
{
- _grouperobject *igo;
+ _grouperobject *igo;
- igo = PyObject_GC_New(_grouperobject, &_grouper_type);
- if (igo == NULL)
- return NULL;
- igo->parent = (PyObject *)parent;
- Py_INCREF(parent);
- igo->tgtkey = tgtkey;
- Py_INCREF(tgtkey);
+ igo = PyObject_GC_New(_grouperobject, &_grouper_type);
+ if (igo == NULL)
+ return NULL;
+ igo->parent = (PyObject *)parent;
+ Py_INCREF(parent);
+ igo->tgtkey = tgtkey;
+ Py_INCREF(tgtkey);
- PyObject_GC_Track(igo);
- return (PyObject *)igo;
+ PyObject_GC_Track(igo);
+ return (PyObject *)igo;
}
static void
_grouper_dealloc(_grouperobject *igo)
{
- PyObject_GC_UnTrack(igo);
- Py_DECREF(igo->parent);
- Py_DECREF(igo->tgtkey);
- PyObject_GC_Del(igo);
+ PyObject_GC_UnTrack(igo);
+ Py_DECREF(igo->parent);
+ Py_DECREF(igo->tgtkey);
+ PyObject_GC_Del(igo);
}
static int
_grouper_traverse(_grouperobject *igo, visitproc visit, void *arg)
{
- Py_VISIT(igo->parent);
- Py_VISIT(igo->tgtkey);
- return 0;
+ Py_VISIT(igo->parent);
+ Py_VISIT(igo->tgtkey);
+ return 0;
}
static PyObject *
_grouper_next(_grouperobject *igo)
{
- groupbyobject *gbo = (groupbyobject *)igo->parent;
- PyObject *newvalue, *newkey, *r;
- int rcmp;
-
- if (gbo->currvalue == NULL) {
- newvalue = PyIter_Next(gbo->it);
- if (newvalue == NULL)
- return NULL;
-
- if (gbo->keyfunc == Py_None) {
- newkey = newvalue;
- Py_INCREF(newvalue);
- } else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
- newvalue, NULL);
- if (newkey == NULL) {
- Py_DECREF(newvalue);
- return NULL;
- }
- }
-
- assert(gbo->currkey == NULL);
- gbo->currkey = newkey;
- gbo->currvalue = newvalue;
- }
-
- assert(gbo->currkey != NULL);
- rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ);
- if (rcmp <= 0)
- /* got any error or current group is end */
- return NULL;
-
- r = gbo->currvalue;
- gbo->currvalue = NULL;
- Py_CLEAR(gbo->currkey);
-
- return r;
+ groupbyobject *gbo = (groupbyobject *)igo->parent;
+ PyObject *newvalue, *newkey, *r;
+ int rcmp;
+
+ if (gbo->currvalue == NULL) {
+ newvalue = PyIter_Next(gbo->it);
+ if (newvalue == NULL)
+ return NULL;
+
+ if (gbo->keyfunc == Py_None) {
+ newkey = newvalue;
+ Py_INCREF(newvalue);
+ } else {
+ newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
+ newvalue, NULL);
+ if (newkey == NULL) {
+ Py_DECREF(newvalue);
+ return NULL;
+ }
+ }
+
+ assert(gbo->currkey == NULL);
+ gbo->currkey = newkey;
+ gbo->currvalue = newvalue;
+ }
+
+ assert(gbo->currkey != NULL);
+ rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ);
+ if (rcmp <= 0)
+ /* got any error or current group is end */
+ return NULL;
+
+ r = gbo->currvalue;
+ gbo->currvalue = NULL;
+ Py_CLEAR(gbo->currkey);
+
+ return r;
}
static PyTypeObject _grouper_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools._grouper", /* tp_name */
- sizeof(_grouperobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)_grouper_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- (traverseproc)_grouper_traverse,/* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)_grouper_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools._grouper", /* tp_name */
+ sizeof(_grouperobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)_grouper_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)_grouper_traverse,/* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)_grouper_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
-
+
/* tee object and with supporting function and objects ***************/
/* The teedataobject pre-allocates space for LINKCELLS number of objects.
To help the object fit neatly inside cache lines (space for 16 to 32
- pointers), the value should be a multiple of 16 minus space for
+ pointers), the value should be a multiple of 16 minus space for
the other structure members including PyHEAD overhead. The larger the
value, the less memory overhead per object and the less time spent
allocating/deallocating new links. The smaller the number, the less
@@ -327,18 +327,18 @@ static PyTypeObject _grouper_type = {
#define LINKCELLS 57
typedef struct {
- PyObject_HEAD
- PyObject *it;
- int numread;
- PyObject *nextlink;
- PyObject *(values[LINKCELLS]);
+ PyObject_HEAD
+ PyObject *it;
+ int numread;
+ PyObject *nextlink;
+ PyObject *(values[LINKCELLS]);
} teedataobject;
typedef struct {
- PyObject_HEAD
- teedataobject *dataobj;
- int index;
- PyObject *weakreflist;
+ PyObject_HEAD
+ teedataobject *dataobj;
+ int index;
+ PyObject *weakreflist;
} teeobject;
static PyTypeObject teedataobject_type;
@@ -346,123 +346,123 @@ static PyTypeObject teedataobject_type;
static PyObject *
teedataobject_new(PyObject *it)
{
- teedataobject *tdo;
+ teedataobject *tdo;
- tdo = PyObject_GC_New(teedataobject, &teedataobject_type);
- if (tdo == NULL)
- return NULL;
+ tdo = PyObject_GC_New(teedataobject, &teedataobject_type);
+ if (tdo == NULL)
+ return NULL;
- tdo->numread = 0;
- tdo->nextlink = NULL;
- Py_INCREF(it);
- tdo->it = it;
- PyObject_GC_Track(tdo);
- return (PyObject *)tdo;
+ tdo->numread = 0;
+ tdo->nextlink = NULL;
+ Py_INCREF(it);
+ tdo->it = it;
+ PyObject_GC_Track(tdo);
+ return (PyObject *)tdo;
}
static PyObject *
teedataobject_jumplink(teedataobject *tdo)
{
- if (tdo->nextlink == NULL)
- tdo->nextlink = teedataobject_new(tdo->it);
- Py_XINCREF(tdo->nextlink);
- return tdo->nextlink;
+ if (tdo->nextlink == NULL)
+ tdo->nextlink = teedataobject_new(tdo->it);
+ Py_XINCREF(tdo->nextlink);
+ return tdo->nextlink;
}
static PyObject *
teedataobject_getitem(teedataobject *tdo, int i)
{
- PyObject *value;
+ PyObject *value;
- assert(i < LINKCELLS);
- if (i < tdo->numread)
- value = tdo->values[i];
- else {
- /* this is the lead iterator, so fetch more data */
- assert(i == tdo->numread);
- value = PyIter_Next(tdo->it);
- if (value == NULL)
- return NULL;
- tdo->numread++;
- tdo->values[i] = value;
- }
- Py_INCREF(value);
- return value;
+ assert(i < LINKCELLS);
+ if (i < tdo->numread)
+ value = tdo->values[i];
+ else {
+ /* this is the lead iterator, so fetch more data */
+ assert(i == tdo->numread);
+ value = PyIter_Next(tdo->it);
+ if (value == NULL)
+ return NULL;
+ tdo->numread++;
+ tdo->values[i] = value;
+ }
+ Py_INCREF(value);
+ return value;
}
static int
teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
{
- int i;
- Py_VISIT(tdo->it);
- for (i = 0; i < tdo->numread; i++)
- Py_VISIT(tdo->values[i]);
- Py_VISIT(tdo->nextlink);
- return 0;
+ int i;
+ Py_VISIT(tdo->it);
+ for (i = 0; i < tdo->numread; i++)
+ Py_VISIT(tdo->values[i]);
+ Py_VISIT(tdo->nextlink);
+ return 0;
}
static int
teedataobject_clear(teedataobject *tdo)
{
- int i;
- Py_CLEAR(tdo->it);
- for (i=0 ; i<tdo->numread ; i++)
- Py_CLEAR(tdo->values[i]);
- Py_CLEAR(tdo->nextlink);
- return 0;
+ int i;
+ Py_CLEAR(tdo->it);
+ for (i=0 ; i<tdo->numread ; i++)
+ Py_CLEAR(tdo->values[i]);
+ Py_CLEAR(tdo->nextlink);
+ return 0;
}
static void
teedataobject_dealloc(teedataobject *tdo)
{
- PyObject_GC_UnTrack(tdo);
- teedataobject_clear(tdo);
- PyObject_GC_Del(tdo);
+ PyObject_GC_UnTrack(tdo);
+ teedataobject_clear(tdo);
+ PyObject_GC_Del(tdo);
}
PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects.");
static PyTypeObject teedataobject_type = {
- PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
- "itertools.tee_dataobject", /* tp_name */
- sizeof(teedataobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)teedataobject_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- teedataobject_doc, /* tp_doc */
- (traverseproc)teedataobject_traverse, /* tp_traverse */
- (inquiry)teedataobject_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
+ "itertools.tee_dataobject", /* tp_name */
+ sizeof(teedataobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)teedataobject_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ teedataobject_doc, /* tp_doc */
+ (traverseproc)teedataobject_traverse, /* tp_traverse */
+ (inquiry)teedataobject_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -471,42 +471,42 @@ static PyTypeObject tee_type;
static PyObject *
tee_next(teeobject *to)
{
- PyObject *value, *link;
+ PyObject *value, *link;
- if (to->index >= LINKCELLS) {
- link = teedataobject_jumplink(to->dataobj);
- Py_DECREF(to->dataobj);
- to->dataobj = (teedataobject *)link;
- to->index = 0;
- }
- value = teedataobject_getitem(to->dataobj, to->index);
- if (value == NULL)
- return NULL;
- to->index++;
- return value;
+ if (to->index >= LINKCELLS) {
+ link = teedataobject_jumplink(to->dataobj);
+ Py_DECREF(to->dataobj);
+ to->dataobj = (teedataobject *)link;
+ to->index = 0;
+ }
+ value = teedataobject_getitem(to->dataobj, to->index);
+ if (value == NULL)
+ return NULL;
+ to->index++;
+ return value;
}
static int
tee_traverse(teeobject *to, visitproc visit, void *arg)
{
- Py_VISIT((PyObject *)to->dataobj);
- return 0;
+ Py_VISIT((PyObject *)to->dataobj);
+ return 0;
}
static PyObject *
tee_copy(teeobject *to)
{
- teeobject *newto;
+ teeobject *newto;
- newto = PyObject_GC_New(teeobject, &tee_type);
- if (newto == NULL)
- return NULL;
- Py_INCREF(to->dataobj);
- newto->dataobj = to->dataobj;
- newto->index = to->index;
- newto->weakreflist = NULL;
- PyObject_GC_Track(newto);
- return (PyObject *)newto;
+ newto = PyObject_GC_New(teeobject, &tee_type);
+ if (newto == NULL)
+ return NULL;
+ Py_INCREF(to->dataobj);
+ newto->dataobj = to->dataobj;
+ newto->index = to->index;
+ newto->weakreflist = NULL;
+ PyObject_GC_Track(newto);
+ return (PyObject *)newto;
}
PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator.");
@@ -514,154 +514,154 @@ PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator.");
static PyObject *
tee_fromiterable(PyObject *iterable)
{
- teeobject *to;
- PyObject *it = NULL;
-
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
- if (PyObject_TypeCheck(it, &tee_type)) {
- to = (teeobject *)tee_copy((teeobject *)it);
- goto done;
- }
-
- to = PyObject_GC_New(teeobject, &tee_type);
- if (to == NULL)
- goto done;
- to->dataobj = (teedataobject *)teedataobject_new(it);
- if (!to->dataobj) {
- PyObject_GC_Del(to);
- to = NULL;
- goto done;
- }
-
- to->index = 0;
- to->weakreflist = NULL;
- PyObject_GC_Track(to);
+ teeobject *to;
+ PyObject *it = NULL;
+
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+ if (PyObject_TypeCheck(it, &tee_type)) {
+ to = (teeobject *)tee_copy((teeobject *)it);
+ goto done;
+ }
+
+ to = PyObject_GC_New(teeobject, &tee_type);
+ if (to == NULL)
+ goto done;
+ to->dataobj = (teedataobject *)teedataobject_new(it);
+ if (!to->dataobj) {
+ PyObject_GC_Del(to);
+ to = NULL;
+ goto done;
+ }
+
+ to->index = 0;
+ to->weakreflist = NULL;
+ PyObject_GC_Track(to);
done:
- Py_XDECREF(it);
- return (PyObject *)to;
+ Py_XDECREF(it);
+ return (PyObject *)to;
}
static PyObject *
tee_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *iterable;
+ PyObject *iterable;
- if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable))
- return NULL;
- return tee_fromiterable(iterable);
+ if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable))
+ return NULL;
+ return tee_fromiterable(iterable);
}
static int
tee_clear(teeobject *to)
{
- if (to->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) to);
- Py_CLEAR(to->dataobj);
- return 0;
+ if (to->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) to);
+ Py_CLEAR(to->dataobj);
+ return 0;
}
static void
tee_dealloc(teeobject *to)
{
- PyObject_GC_UnTrack(to);
- tee_clear(to);
- PyObject_GC_Del(to);
+ PyObject_GC_UnTrack(to);
+ tee_clear(to);
+ PyObject_GC_Del(to);
}
PyDoc_STRVAR(teeobject_doc,
"Iterator wrapped to make it copyable");
static PyMethodDef tee_methods[] = {
- {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
- {NULL, NULL} /* sentinel */
+ {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject tee_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.tee", /* tp_name */
- sizeof(teeobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)tee_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- teeobject_doc, /* tp_doc */
- (traverseproc)tee_traverse, /* tp_traverse */
- (inquiry)tee_clear, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(teeobject, weakreflist), /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)tee_next, /* tp_iternext */
- tee_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- tee_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.tee", /* tp_name */
+ sizeof(teeobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)tee_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ teeobject_doc, /* tp_doc */
+ (traverseproc)tee_traverse, /* tp_traverse */
+ (inquiry)tee_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(teeobject, weakreflist), /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)tee_next, /* tp_iternext */
+ tee_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ tee_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
static PyObject *
tee(PyObject *self, PyObject *args)
{
- Py_ssize_t i, n=2;
- PyObject *it, *iterable, *copyable, *result;
-
- if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))
- return NULL;
- if (n < 0) {
- PyErr_SetString(PyExc_ValueError, "n must be >= 0");
- return NULL;
- }
- result = PyTuple_New(n);
- if (result == NULL)
- return NULL;
- if (n == 0)
- return result;
- it = PyObject_GetIter(iterable);
- if (it == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- if (!PyObject_HasAttrString(it, "__copy__")) {
- copyable = tee_fromiterable(it);
- Py_DECREF(it);
- if (copyable == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- } else
- copyable = it;
- PyTuple_SET_ITEM(result, 0, copyable);
- for (i=1 ; i<n ; i++) {
- copyable = PyObject_CallMethod(copyable, "__copy__", NULL);
- if (copyable == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SET_ITEM(result, i, copyable);
- }
- return result;
+ Py_ssize_t i, n=2;
+ PyObject *it, *iterable, *copyable, *result;
+
+ if (!PyArg_ParseTuple(args, "O|n", &iterable, &n))
+ return NULL;
+ if (n < 0) {
+ PyErr_SetString(PyExc_ValueError, "n must be >= 0");
+ return NULL;
+ }
+ result = PyTuple_New(n);
+ if (result == NULL)
+ return NULL;
+ if (n == 0)
+ return result;
+ it = PyObject_GetIter(iterable);
+ if (it == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ if (!PyObject_HasAttrString(it, "__copy__")) {
+ copyable = tee_fromiterable(it);
+ Py_DECREF(it);
+ if (copyable == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ } else
+ copyable = it;
+ PyTuple_SET_ITEM(result, 0, copyable);
+ for (i=1 ; i<n ; i++) {
+ copyable = PyObject_CallMethod(copyable, "__copy__", NULL);
+ if (copyable == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(result, i, copyable);
+ }
+ return result;
}
PyDoc_STRVAR(tee_doc,
@@ -671,10 +671,10 @@ PyDoc_STRVAR(tee_doc,
/* cycle object **********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *it;
- PyObject *saved;
- int firstpass;
+ PyObject_HEAD
+ PyObject *it;
+ PyObject *saved;
+ int firstpass;
} cycleobject;
static PyTypeObject cycle_type;
@@ -682,91 +682,91 @@ static PyTypeObject cycle_type;
static PyObject *
cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *it;
- PyObject *iterable;
- PyObject *saved;
- cycleobject *lz;
+ PyObject *it;
+ PyObject *iterable;
+ PyObject *saved;
+ cycleobject *lz;
- if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds))
- return NULL;
+ if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
+ return NULL;
- /* Get iterator. */
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
+ /* Get iterator. */
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
- saved = PyList_New(0);
- if (saved == NULL) {
- Py_DECREF(it);
- return NULL;
- }
+ saved = PyList_New(0);
+ if (saved == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
- /* create cycleobject structure */
- lz = (cycleobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- Py_DECREF(saved);
- return NULL;
- }
- lz->it = it;
- lz->saved = saved;
- lz->firstpass = 0;
+ /* create cycleobject structure */
+ lz = (cycleobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ Py_DECREF(saved);
+ return NULL;
+ }
+ lz->it = it;
+ lz->saved = saved;
+ lz->firstpass = 0;
- return (PyObject *)lz;
+ return (PyObject *)lz;
}
static void
cycle_dealloc(cycleobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->saved);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->saved);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->saved);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->saved);
+ return 0;
}
static PyObject *
cycle_next(cycleobject *lz)
{
- PyObject *item;
- PyObject *it;
- PyObject *tmp;
-
- while (1) {
- item = PyIter_Next(lz->it);
- if (item != NULL) {
- if (!lz->firstpass && PyList_Append(lz->saved, item)) {
- Py_DECREF(item);
- return NULL;
- }
- return item;
- }
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- return NULL;
- }
- if (PyList_Size(lz->saved) == 0)
- return NULL;
- it = PyObject_GetIter(lz->saved);
- if (it == NULL)
- return NULL;
- tmp = lz->it;
- lz->it = it;
- lz->firstpass = 1;
- Py_DECREF(tmp);
- }
+ PyObject *item;
+ PyObject *it;
+ PyObject *tmp;
+
+ while (1) {
+ item = PyIter_Next(lz->it);
+ if (item != NULL) {
+ if (!lz->firstpass && PyList_Append(lz->saved, item)) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ return item;
+ }
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ if (PyList_Size(lz->saved) == 0)
+ return NULL;
+ it = PyObject_GetIter(lz->saved);
+ if (it == NULL)
+ return NULL;
+ tmp = lz->it;
+ lz->it = it;
+ lz->firstpass = 1;
+ Py_DECREF(tmp);
+ }
}
PyDoc_STRVAR(cycle_doc,
@@ -776,57 +776,57 @@ Return elements from the iterable until it is exhausted.\n\
Then repeat the sequence indefinitely.");
static PyTypeObject cycle_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.cycle", /* tp_name */
- sizeof(cycleobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)cycle_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- cycle_doc, /* tp_doc */
- (traverseproc)cycle_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)cycle_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- cycle_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.cycle", /* tp_name */
+ sizeof(cycleobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)cycle_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ cycle_doc, /* tp_doc */
+ (traverseproc)cycle_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)cycle_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ cycle_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* dropwhile object **********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
- long start;
+ PyObject_HEAD
+ PyObject *func;
+ PyObject *it;
+ long start;
} dropwhileobject;
static PyTypeObject dropwhile_type;
@@ -834,81 +834,81 @@ static PyTypeObject dropwhile_type;
static PyObject *
dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *func, *seq;
- PyObject *it;
- dropwhileobject *lz;
+ PyObject *func, *seq;
+ PyObject *it;
+ dropwhileobject *lz;
- if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds))
- return NULL;
+ if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
+ return NULL;
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
- /* create dropwhileobject structure */
- lz = (dropwhileobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
- lz->start = 0;
+ /* create dropwhileobject structure */
+ lz = (dropwhileobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_INCREF(func);
+ lz->func = func;
+ lz->it = it;
+ lz->start = 0;
- return (PyObject *)lz;
+ return (PyObject *)lz;
}
static void
dropwhile_dealloc(dropwhileobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->func);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
dropwhile_next(dropwhileobject *lz)
{
- PyObject *item, *good;
- PyObject *it = lz->it;
- long ok;
- PyObject *(*iternext)(PyObject *);
-
- iternext = *Py_TYPE(it)->tp_iternext;
- for (;;) {
- item = iternext(it);
- if (item == NULL)
- return NULL;
- if (lz->start == 1)
- return item;
-
- good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
- if (good == NULL) {
- Py_DECREF(item);
- return NULL;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- if (!ok) {
- lz->start = 1;
- return item;
- }
- Py_DECREF(item);
- }
+ PyObject *item, *good;
+ PyObject *it = lz->it;
+ long ok;
+ PyObject *(*iternext)(PyObject *);
+
+ iternext = *Py_TYPE(it)->tp_iternext;
+ for (;;) {
+ item = iternext(it);
+ if (item == NULL)
+ return NULL;
+ if (lz->start == 1)
+ return item;
+
+ good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
+ if (good == NULL) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ if (!ok) {
+ lz->start = 1;
+ return item;
+ }
+ Py_DECREF(item);
+ }
}
PyDoc_STRVAR(dropwhile_doc,
@@ -918,57 +918,57 @@ Drop items from the iterable while predicate(item) is true.\n\
Afterwards, return every element until the iterable is exhausted.");
static PyTypeObject dropwhile_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.dropwhile", /* tp_name */
- sizeof(dropwhileobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dropwhile_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- dropwhile_doc, /* tp_doc */
- (traverseproc)dropwhile_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dropwhile_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- dropwhile_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.dropwhile", /* tp_name */
+ sizeof(dropwhileobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dropwhile_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ dropwhile_doc, /* tp_doc */
+ (traverseproc)dropwhile_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dropwhile_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ dropwhile_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* takewhile object **********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
- long stop;
+ PyObject_HEAD
+ PyObject *func;
+ PyObject *it;
+ long stop;
} takewhileobject;
static PyTypeObject takewhile_type;
@@ -976,78 +976,78 @@ static PyTypeObject takewhile_type;
static PyObject *
takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *func, *seq;
- PyObject *it;
- takewhileobject *lz;
+ PyObject *func, *seq;
+ PyObject *it;
+ takewhileobject *lz;
- if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds))
- return NULL;
+ if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
+ return NULL;
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
- /* create takewhileobject structure */
- lz = (takewhileobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
- lz->stop = 0;
+ /* create takewhileobject structure */
+ lz = (takewhileobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_INCREF(func);
+ lz->func = func;
+ lz->it = it;
+ lz->stop = 0;
- return (PyObject *)lz;
+ return (PyObject *)lz;
}
static void
takewhile_dealloc(takewhileobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->func);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
takewhile_next(takewhileobject *lz)
{
- PyObject *item, *good;
- PyObject *it = lz->it;
- long ok;
+ PyObject *item, *good;
+ PyObject *it = lz->it;
+ long ok;
- if (lz->stop == 1)
- return NULL;
+ if (lz->stop == 1)
+ return NULL;
- item = (*Py_TYPE(it)->tp_iternext)(it);
- if (item == NULL)
- return NULL;
+ item = (*Py_TYPE(it)->tp_iternext)(it);
+ if (item == NULL)
+ return NULL;
- good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
- if (good == NULL) {
- Py_DECREF(item);
- return NULL;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- if (ok)
- return item;
- Py_DECREF(item);
- lz->stop = 1;
- return NULL;
+ good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
+ if (good == NULL) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ if (ok)
+ return item;
+ Py_DECREF(item);
+ lz->stop = 1;
+ return NULL;
}
PyDoc_STRVAR(takewhile_doc,
@@ -1057,59 +1057,59 @@ Return successive entries from an iterable as long as the \n\
predicate evaluates to true for each entry.");
static PyTypeObject takewhile_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.takewhile", /* tp_name */
- sizeof(takewhileobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)takewhile_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- takewhile_doc, /* tp_doc */
- (traverseproc)takewhile_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)takewhile_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- takewhile_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.takewhile", /* tp_name */
+ sizeof(takewhileobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)takewhile_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ takewhile_doc, /* tp_doc */
+ (traverseproc)takewhile_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)takewhile_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ takewhile_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* islice object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *it;
- Py_ssize_t next;
- Py_ssize_t stop;
- Py_ssize_t step;
- Py_ssize_t cnt;
+ PyObject_HEAD
+ PyObject *it;
+ Py_ssize_t next;
+ Py_ssize_t stop;
+ Py_ssize_t step;
+ Py_ssize_t cnt;
} isliceobject;
static PyTypeObject islice_type;
@@ -1117,126 +1117,126 @@ static PyTypeObject islice_type;
static PyObject *
islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *seq;
- Py_ssize_t start=0, stop=-1, step=1;
- PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
- Py_ssize_t numargs;
- isliceobject *lz;
-
- if (type == &islice_type && !_PyArg_NoKeywords("islice()", kwds))
- return NULL;
-
- if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
- return NULL;
-
- numargs = PyTuple_Size(args);
- if (numargs == 2) {
- if (a1 != Py_None) {
- stop = PyLong_AsSsize_t(a1);
- if (stop == -1) {
- if (PyErr_Occurred())
- PyErr_Clear();
- PyErr_SetString(PyExc_ValueError,
- "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
- return NULL;
- }
- }
- } else {
- if (a1 != Py_None)
- start = PyLong_AsSsize_t(a1);
- if (start == -1 && PyErr_Occurred())
- PyErr_Clear();
- if (a2 != Py_None) {
- stop = PyLong_AsSsize_t(a2);
- if (stop == -1) {
- if (PyErr_Occurred())
- PyErr_Clear();
- PyErr_SetString(PyExc_ValueError,
- "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
- return NULL;
- }
- }
- }
- if (start<0 || stop<-1) {
- PyErr_SetString(PyExc_ValueError,
- "Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
- return NULL;
- }
-
- if (a3 != NULL) {
- if (a3 != Py_None)
- step = PyLong_AsSsize_t(a3);
- if (step == -1 && PyErr_Occurred())
- PyErr_Clear();
- }
- if (step<1) {
- PyErr_SetString(PyExc_ValueError,
- "Step for islice() must be a positive integer or None.");
- return NULL;
- }
-
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
-
- /* create isliceobject structure */
- lz = (isliceobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- lz->it = it;
- lz->next = start;
- lz->stop = stop;
- lz->step = step;
- lz->cnt = 0L;
-
- return (PyObject *)lz;
+ PyObject *seq;
+ Py_ssize_t start=0, stop=-1, step=1;
+ PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
+ Py_ssize_t numargs;
+ isliceobject *lz;
+
+ if (type == &islice_type && !_PyArg_NoKeywords("islice()", kwds))
+ return NULL;
+
+ if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
+ return NULL;
+
+ numargs = PyTuple_Size(args);
+ if (numargs == 2) {
+ if (a1 != Py_None) {
+ stop = PyLong_AsSsize_t(a1);
+ if (stop == -1) {
+ if (PyErr_Occurred())
+ PyErr_Clear();
+ PyErr_SetString(PyExc_ValueError,
+ "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ return NULL;
+ }
+ }
+ } else {
+ if (a1 != Py_None)
+ start = PyLong_AsSsize_t(a1);
+ if (start == -1 && PyErr_Occurred())
+ PyErr_Clear();
+ if (a2 != Py_None) {
+ stop = PyLong_AsSsize_t(a2);
+ if (stop == -1) {
+ if (PyErr_Occurred())
+ PyErr_Clear();
+ PyErr_SetString(PyExc_ValueError,
+ "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ return NULL;
+ }
+ }
+ }
+ if (start<0 || stop<-1) {
+ PyErr_SetString(PyExc_ValueError,
+ "Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ return NULL;
+ }
+
+ if (a3 != NULL) {
+ if (a3 != Py_None)
+ step = PyLong_AsSsize_t(a3);
+ if (step == -1 && PyErr_Occurred())
+ PyErr_Clear();
+ }
+ if (step<1) {
+ PyErr_SetString(PyExc_ValueError,
+ "Step for islice() must be a positive integer or None.");
+ return NULL;
+ }
+
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
+
+ /* create isliceobject structure */
+ lz = (isliceobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ lz->it = it;
+ lz->next = start;
+ lz->stop = stop;
+ lz->step = step;
+ lz->cnt = 0L;
+
+ return (PyObject *)lz;
}
static void
islice_dealloc(isliceobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
islice_traverse(isliceobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- return 0;
+ Py_VISIT(lz->it);
+ return 0;
}
static PyObject *
islice_next(isliceobject *lz)
{
- PyObject *item;
- PyObject *it = lz->it;
- Py_ssize_t oldnext;
- PyObject *(*iternext)(PyObject *);
-
- iternext = *Py_TYPE(it)->tp_iternext;
- while (lz->cnt < lz->next) {
- item = iternext(it);
- if (item == NULL)
- return NULL;
- Py_DECREF(item);
- lz->cnt++;
- }
- if (lz->stop != -1 && lz->cnt >= lz->stop)
- return NULL;
- item = iternext(it);
- if (item == NULL)
- return NULL;
- lz->cnt++;
- oldnext = lz->next;
- lz->next += lz->step;
- if (lz->next < oldnext) /* Check for overflow */
- lz->next = lz->stop;
- return item;
+ PyObject *item;
+ PyObject *it = lz->it;
+ Py_ssize_t oldnext;
+ PyObject *(*iternext)(PyObject *);
+
+ iternext = *Py_TYPE(it)->tp_iternext;
+ while (lz->cnt < lz->next) {
+ item = iternext(it);
+ if (item == NULL)
+ return NULL;
+ Py_DECREF(item);
+ lz->cnt++;
+ }
+ if (lz->stop != -1 && lz->cnt >= lz->stop)
+ return NULL;
+ item = iternext(it);
+ if (item == NULL)
+ return NULL;
+ lz->cnt++;
+ oldnext = lz->next;
+ lz->next += lz->step;
+ if (lz->next < oldnext) /* Check for overflow */
+ lz->next = lz->stop;
+ return item;
}
PyDoc_STRVAR(islice_doc,
@@ -1250,56 +1250,56 @@ skipped between successive calls. Works like a slice() on a list\n\
but returns an iterator.");
static PyTypeObject islice_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.islice", /* tp_name */
- sizeof(isliceobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)islice_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- islice_doc, /* tp_doc */
- (traverseproc)islice_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)islice_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- islice_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.islice", /* tp_name */
+ sizeof(isliceobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)islice_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ islice_doc, /* tp_doc */
+ (traverseproc)islice_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)islice_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ islice_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* starmap object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
+ PyObject_HEAD
+ PyObject *func;
+ PyObject *it;
} starmapobject;
static PyTypeObject starmap_type;
@@ -1307,71 +1307,71 @@ static PyTypeObject starmap_type;
static PyObject *
starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *func, *seq;
- PyObject *it;
- starmapobject *lz;
+ PyObject *func, *seq;
+ PyObject *it;
+ starmapobject *lz;
- if (type == &starmap_type && !_PyArg_NoKeywords("starmap()", kwds))
- return NULL;
+ if (type == &starmap_type && !_PyArg_NoKeywords("starmap()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
+ return NULL;
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
- /* create starmapobject structure */
- lz = (starmapobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
+ /* create starmapobject structure */
+ lz = (starmapobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_INCREF(func);
+ lz->func = func;
+ lz->it = it;
- return (PyObject *)lz;
+ return (PyObject *)lz;
}
static void
starmap_dealloc(starmapobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->func);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
starmap_traverse(starmapobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
starmap_next(starmapobject *lz)
{
- PyObject *args;
- PyObject *result;
- PyObject *it = lz->it;
-
- args = (*Py_TYPE(it)->tp_iternext)(it);
- if (args == NULL)
- return NULL;
- if (!PyTuple_CheckExact(args)) {
- PyObject *newargs = PySequence_Tuple(args);
- Py_DECREF(args);
- if (newargs == NULL)
- return NULL;
- args = newargs;
- }
- result = PyObject_Call(lz->func, args, NULL);
- Py_DECREF(args);
- return result;
+ PyObject *args;
+ PyObject *result;
+ PyObject *it = lz->it;
+
+ args = (*Py_TYPE(it)->tp_iternext)(it);
+ if (args == NULL)
+ return NULL;
+ if (!PyTuple_CheckExact(args)) {
+ PyObject *newargs = PySequence_Tuple(args);
+ Py_DECREF(args);
+ if (newargs == NULL)
+ return NULL;
+ args = newargs;
+ }
+ result = PyObject_Call(lz->func, args, NULL);
+ Py_DECREF(args);
+ return result;
}
PyDoc_STRVAR(starmap_doc,
@@ -1381,152 +1381,152 @@ Return an iterator whose values are returned from the function evaluated\n\
with a argument tuple taken from the given sequence.");
static PyTypeObject starmap_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.starmap", /* tp_name */
- sizeof(starmapobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)starmap_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- starmap_doc, /* tp_doc */
- (traverseproc)starmap_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)starmap_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- starmap_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.starmap", /* tp_name */
+ sizeof(starmapobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)starmap_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ starmap_doc, /* tp_doc */
+ (traverseproc)starmap_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)starmap_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ starmap_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* chain object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *source; /* Iterator over input iterables */
- PyObject *active; /* Currently running input iterator */
+ PyObject_HEAD
+ PyObject *source; /* Iterator over input iterables */
+ PyObject *active; /* Currently running input iterator */
} chainobject;
static PyTypeObject chain_type;
-static PyObject *
+static PyObject *
chain_new_internal(PyTypeObject *type, PyObject *source)
{
- chainobject *lz;
+ chainobject *lz;
- lz = (chainobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(source);
- return NULL;
- }
-
- lz->source = source;
- lz->active = NULL;
- return (PyObject *)lz;
+ lz = (chainobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(source);
+ return NULL;
+ }
+
+ lz->source = source;
+ lz->active = NULL;
+ return (PyObject *)lz;
}
static PyObject *
chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *source;
+ PyObject *source;
+
+ if (type == &chain_type && !_PyArg_NoKeywords("chain()", kwds))
+ return NULL;
- if (type == &chain_type && !_PyArg_NoKeywords("chain()", kwds))
- return NULL;
-
- source = PyObject_GetIter(args);
- if (source == NULL)
- return NULL;
+ source = PyObject_GetIter(args);
+ if (source == NULL)
+ return NULL;
- return chain_new_internal(type, source);
+ return chain_new_internal(type, source);
}
static PyObject *
chain_new_from_iterable(PyTypeObject *type, PyObject *arg)
{
- PyObject *source;
-
- source = PyObject_GetIter(arg);
- if (source == NULL)
- return NULL;
+ PyObject *source;
+
+ source = PyObject_GetIter(arg);
+ if (source == NULL)
+ return NULL;
- return chain_new_internal(type, source);
+ return chain_new_internal(type, source);
}
static void
chain_dealloc(chainobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->active);
- Py_XDECREF(lz->source);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->active);
+ Py_XDECREF(lz->source);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
chain_traverse(chainobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->source);
- Py_VISIT(lz->active);
- return 0;
+ Py_VISIT(lz->source);
+ Py_VISIT(lz->active);
+ return 0;
}
static PyObject *
chain_next(chainobject *lz)
{
- PyObject *item;
-
- if (lz->source == NULL)
- return NULL; /* already stopped */
-
- if (lz->active == NULL) {
- PyObject *iterable = PyIter_Next(lz->source);
- if (iterable == NULL) {
- Py_CLEAR(lz->source);
- return NULL; /* no more input sources */
- }
- lz->active = PyObject_GetIter(iterable);
- Py_DECREF(iterable);
- if (lz->active == NULL) {
- Py_CLEAR(lz->source);
- return NULL; /* input not iterable */
- }
- }
- item = PyIter_Next(lz->active);
- if (item != NULL)
- return item;
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- return NULL; /* input raised an exception */
- }
- Py_CLEAR(lz->active);
- return chain_next(lz); /* recurse and use next active */
+ PyObject *item;
+
+ if (lz->source == NULL)
+ return NULL; /* already stopped */
+
+ if (lz->active == NULL) {
+ PyObject *iterable = PyIter_Next(lz->source);
+ if (iterable == NULL) {
+ Py_CLEAR(lz->source);
+ return NULL; /* no more input sources */
+ }
+ lz->active = PyObject_GetIter(iterable);
+ Py_DECREF(iterable);
+ if (lz->active == NULL) {
+ Py_CLEAR(lz->source);
+ return NULL; /* input not iterable */
+ }
+ }
+ item = PyIter_Next(lz->active);
+ if (item != NULL)
+ return item;
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else
+ return NULL; /* input raised an exception */
+ }
+ Py_CLEAR(lz->active);
+ return chain_next(lz); /* recurse and use next active */
}
PyDoc_STRVAR(chain_doc,
@@ -1543,64 +1543,64 @@ Alternate chain() contructor taking a single iterable argument\n\
that evaluates lazily.");
static PyMethodDef chain_methods[] = {
- {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS,
- chain_from_iterable_doc},
- {NULL, NULL} /* sentinel */
+ {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS,
+ chain_from_iterable_doc},
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject chain_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.chain", /* tp_name */
- sizeof(chainobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)chain_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- chain_doc, /* tp_doc */
- (traverseproc)chain_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)chain_next, /* tp_iternext */
- chain_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- chain_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.chain", /* tp_name */
+ sizeof(chainobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)chain_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ chain_doc, /* tp_doc */
+ (traverseproc)chain_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)chain_next, /* tp_iternext */
+ chain_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ chain_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* product object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *pools; /* tuple of pool tuples */
- Py_ssize_t *indices; /* one index per pool */
- PyObject *result; /* most recently returned result tuple */
- int stopped; /* set to 1 when the product iterator is exhausted */
+ PyObject_HEAD
+ PyObject *pools; /* tuple of pool tuples */
+ Py_ssize_t *indices; /* one index per pool */
+ PyObject *result; /* most recently returned result tuple */
+ int stopped; /* set to 1 when the product iterator is exhausted */
} productobject;
static PyTypeObject product_type;
@@ -1608,181 +1608,181 @@ static PyTypeObject product_type;
static PyObject *
product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- productobject *lz;
- Py_ssize_t nargs, npools, repeat=1;
- PyObject *pools = NULL;
- Py_ssize_t *indices = NULL;
- Py_ssize_t i;
-
- if (kwds != NULL) {
- char *kwlist[] = {"repeat", 0};
- PyObject *tmpargs = PyTuple_New(0);
- if (tmpargs == NULL)
- return NULL;
- if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) {
- Py_DECREF(tmpargs);
- return NULL;
- }
- Py_DECREF(tmpargs);
- if (repeat < 0) {
- PyErr_SetString(PyExc_ValueError,
- "repeat argument cannot be negative");
- return NULL;
- }
- }
-
- assert(PyTuple_Check(args));
- nargs = (repeat == 0) ? 0 : PyTuple_GET_SIZE(args);
- npools = nargs * repeat;
-
- indices = PyMem_Malloc(npools * sizeof(Py_ssize_t));
- if (indices == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- pools = PyTuple_New(npools);
- if (pools == NULL)
- goto error;
-
- for (i=0; i < nargs ; ++i) {
- PyObject *item = PyTuple_GET_ITEM(args, i);
- PyObject *pool = PySequence_Tuple(item);
- if (pool == NULL)
- goto error;
- PyTuple_SET_ITEM(pools, i, pool);
- indices[i] = 0;
- }
- for ( ; i < npools; ++i) {
- PyObject *pool = PyTuple_GET_ITEM(pools, i - nargs);
- Py_INCREF(pool);
- PyTuple_SET_ITEM(pools, i, pool);
- indices[i] = 0;
- }
-
- /* create productobject structure */
- lz = (productobject *)type->tp_alloc(type, 0);
- if (lz == NULL)
- goto error;
-
- lz->pools = pools;
- lz->indices = indices;
- lz->result = NULL;
- lz->stopped = 0;
-
- return (PyObject *)lz;
+ productobject *lz;
+ Py_ssize_t nargs, npools, repeat=1;
+ PyObject *pools = NULL;
+ Py_ssize_t *indices = NULL;
+ Py_ssize_t i;
+
+ if (kwds != NULL) {
+ char *kwlist[] = {"repeat", 0};
+ PyObject *tmpargs = PyTuple_New(0);
+ if (tmpargs == NULL)
+ return NULL;
+ if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) {
+ Py_DECREF(tmpargs);
+ return NULL;
+ }
+ Py_DECREF(tmpargs);
+ if (repeat < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "repeat argument cannot be negative");
+ return NULL;
+ }
+ }
+
+ assert(PyTuple_Check(args));
+ nargs = (repeat == 0) ? 0 : PyTuple_GET_SIZE(args);
+ npools = nargs * repeat;
+
+ indices = PyMem_Malloc(npools * sizeof(Py_ssize_t));
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+
+ pools = PyTuple_New(npools);
+ if (pools == NULL)
+ goto error;
+
+ for (i=0; i < nargs ; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(args, i);
+ PyObject *pool = PySequence_Tuple(item);
+ if (pool == NULL)
+ goto error;
+ PyTuple_SET_ITEM(pools, i, pool);
+ indices[i] = 0;
+ }
+ for ( ; i < npools; ++i) {
+ PyObject *pool = PyTuple_GET_ITEM(pools, i - nargs);
+ Py_INCREF(pool);
+ PyTuple_SET_ITEM(pools, i, pool);
+ indices[i] = 0;
+ }
+
+ /* create productobject structure */
+ lz = (productobject *)type->tp_alloc(type, 0);
+ if (lz == NULL)
+ goto error;
+
+ lz->pools = pools;
+ lz->indices = indices;
+ lz->result = NULL;
+ lz->stopped = 0;
+
+ return (PyObject *)lz;
error:
- if (indices != NULL)
- PyMem_Free(indices);
- Py_XDECREF(pools);
- return NULL;
+ if (indices != NULL)
+ PyMem_Free(indices);
+ Py_XDECREF(pools);
+ return NULL;
}
static void
product_dealloc(productobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->pools);
- Py_XDECREF(lz->result);
- if (lz->indices != NULL)
- PyMem_Free(lz->indices);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->pools);
+ Py_XDECREF(lz->result);
+ if (lz->indices != NULL)
+ PyMem_Free(lz->indices);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
product_traverse(productobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->pools);
- Py_VISIT(lz->result);
- return 0;
+ Py_VISIT(lz->pools);
+ Py_VISIT(lz->result);
+ return 0;
}
static PyObject *
product_next(productobject *lz)
{
- PyObject *pool;
- PyObject *elem;
- PyObject *oldelem;
- PyObject *pools = lz->pools;
- PyObject *result = lz->result;
- Py_ssize_t npools = PyTuple_GET_SIZE(pools);
- Py_ssize_t i;
-
- if (lz->stopped)
- return NULL;
-
- if (result == NULL) {
- /* On the first pass, return an initial tuple filled with the
- first element from each pool. */
- result = PyTuple_New(npools);
- if (result == NULL)
- goto empty;
- lz->result = result;
- for (i=0; i < npools; i++) {
- pool = PyTuple_GET_ITEM(pools, i);
- if (PyTuple_GET_SIZE(pool) == 0)
- goto empty;
- elem = PyTuple_GET_ITEM(pool, 0);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- } else {
- Py_ssize_t *indices = lz->indices;
-
- /* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
- PyObject *old_result = result;
- result = PyTuple_New(npools);
- if (result == NULL)
- goto empty;
- lz->result = result;
- for (i=0; i < npools; i++) {
- elem = PyTuple_GET_ITEM(old_result, i);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- Py_DECREF(old_result);
- }
- /* Now, we've got the only copy so we can update it in-place */
- assert (npools==0 || Py_REFCNT(result) == 1);
-
- /* Update the pool indices right-to-left. Only advance to the
- next pool when the previous one rolls-over */
- for (i=npools-1 ; i >= 0 ; i--) {
- pool = PyTuple_GET_ITEM(pools, i);
- indices[i]++;
- if (indices[i] == PyTuple_GET_SIZE(pool)) {
- /* Roll-over and advance to next pool */
- indices[i] = 0;
- elem = PyTuple_GET_ITEM(pool, 0);
- Py_INCREF(elem);
- oldelem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, elem);
- Py_DECREF(oldelem);
- } else {
- /* No rollover. Just increment and stop here. */
- elem = PyTuple_GET_ITEM(pool, indices[i]);
- Py_INCREF(elem);
- oldelem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, elem);
- Py_DECREF(oldelem);
- break;
- }
- }
-
- /* If i is negative, then the indices have all rolled-over
- and we're done. */
- if (i < 0)
- goto empty;
- }
-
- Py_INCREF(result);
- return result;
+ PyObject *pool;
+ PyObject *elem;
+ PyObject *oldelem;
+ PyObject *pools = lz->pools;
+ PyObject *result = lz->result;
+ Py_ssize_t npools = PyTuple_GET_SIZE(pools);
+ Py_ssize_t i;
+
+ if (lz->stopped)
+ return NULL;
+
+ if (result == NULL) {
+ /* On the first pass, return an initial tuple filled with the
+ first element from each pool. */
+ result = PyTuple_New(npools);
+ if (result == NULL)
+ goto empty;
+ lz->result = result;
+ for (i=0; i < npools; i++) {
+ pool = PyTuple_GET_ITEM(pools, i);
+ if (PyTuple_GET_SIZE(pool) == 0)
+ goto empty;
+ elem = PyTuple_GET_ITEM(pool, 0);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ } else {
+ Py_ssize_t *indices = lz->indices;
+
+ /* Copy the previous result tuple or re-use it if available */
+ if (Py_REFCNT(result) > 1) {
+ PyObject *old_result = result;
+ result = PyTuple_New(npools);
+ if (result == NULL)
+ goto empty;
+ lz->result = result;
+ for (i=0; i < npools; i++) {
+ elem = PyTuple_GET_ITEM(old_result, i);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ Py_DECREF(old_result);
+ }
+ /* Now, we've got the only copy so we can update it in-place */
+ assert (npools==0 || Py_REFCNT(result) == 1);
+
+ /* Update the pool indices right-to-left. Only advance to the
+ next pool when the previous one rolls-over */
+ for (i=npools-1 ; i >= 0 ; i--) {
+ pool = PyTuple_GET_ITEM(pools, i);
+ indices[i]++;
+ if (indices[i] == PyTuple_GET_SIZE(pool)) {
+ /* Roll-over and advance to next pool */
+ indices[i] = 0;
+ elem = PyTuple_GET_ITEM(pool, 0);
+ Py_INCREF(elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
+ } else {
+ /* No rollover. Just increment and stop here. */
+ elem = PyTuple_GET_ITEM(pool, indices[i]);
+ Py_INCREF(elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
+ break;
+ }
+ }
+
+ /* If i is negative, then the indices have all rolled-over
+ and we're done. */
+ if (i < 0)
+ goto empty;
+ }
+
+ Py_INCREF(result);
+ return result;
empty:
- lz->stopped = 1;
- return NULL;
+ lz->stopped = 1;
+ return NULL;
}
PyDoc_STRVAR(product_doc,
@@ -1800,59 +1800,59 @@ product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\
product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...");
static PyTypeObject product_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.product", /* tp_name */
- sizeof(productobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)product_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- product_doc, /* tp_doc */
- (traverseproc)product_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)product_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- product_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.product", /* tp_name */
+ sizeof(productobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)product_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ product_doc, /* tp_doc */
+ (traverseproc)product_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)product_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ product_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* combinations object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
- Py_ssize_t *indices; /* one index per result element */
- PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the combinations iterator is exhausted */
+ PyObject_HEAD
+ PyObject *pool; /* input converted to a tuple */
+ Py_ssize_t *indices; /* one index per result element */
+ PyObject *result; /* most recently returned result tuple */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the combinations iterator is exhausted */
} combinationsobject;
static PyTypeObject combinations_type;
@@ -1860,160 +1860,160 @@ static PyTypeObject combinations_type;
static PyObject *
combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- combinationsobject *co;
- Py_ssize_t n;
- Py_ssize_t r;
- PyObject *pool = NULL;
- PyObject *iterable = NULL;
- Py_ssize_t *indices = NULL;
- Py_ssize_t i;
- static char *kwargs[] = {"iterable", "r", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations", kwargs,
- &iterable, &r))
- return NULL;
-
- pool = PySequence_Tuple(iterable);
- if (pool == NULL)
- goto error;
- n = PyTuple_GET_SIZE(pool);
- if (r < 0) {
- PyErr_SetString(PyExc_ValueError, "r must be non-negative");
- goto error;
- }
-
- indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
- if (indices == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- for (i=0 ; i<r ; i++)
- indices[i] = i;
-
- /* create combinationsobject structure */
- co = (combinationsobject *)type->tp_alloc(type, 0);
- if (co == NULL)
- goto error;
-
- co->pool = pool;
- co->indices = indices;
- co->result = NULL;
- co->r = r;
- co->stopped = r > n ? 1 : 0;
-
- return (PyObject *)co;
+ combinationsobject *co;
+ Py_ssize_t n;
+ Py_ssize_t r;
+ PyObject *pool = NULL;
+ PyObject *iterable = NULL;
+ Py_ssize_t *indices = NULL;
+ Py_ssize_t i;
+ static char *kwargs[] = {"iterable", "r", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations", kwargs,
+ &iterable, &r))
+ return NULL;
+
+ pool = PySequence_Tuple(iterable);
+ if (pool == NULL)
+ goto error;
+ n = PyTuple_GET_SIZE(pool);
+ if (r < 0) {
+ PyErr_SetString(PyExc_ValueError, "r must be non-negative");
+ goto error;
+ }
+
+ indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+
+ for (i=0 ; i<r ; i++)
+ indices[i] = i;
+
+ /* create combinationsobject structure */
+ co = (combinationsobject *)type->tp_alloc(type, 0);
+ if (co == NULL)
+ goto error;
+
+ co->pool = pool;
+ co->indices = indices;
+ co->result = NULL;
+ co->r = r;
+ co->stopped = r > n ? 1 : 0;
+
+ return (PyObject *)co;
error:
- if (indices != NULL)
- PyMem_Free(indices);
- Py_XDECREF(pool);
- return NULL;
+ if (indices != NULL)
+ PyMem_Free(indices);
+ Py_XDECREF(pool);
+ return NULL;
}
static void
combinations_dealloc(combinationsobject *co)
{
- PyObject_GC_UnTrack(co);
- Py_XDECREF(co->pool);
- Py_XDECREF(co->result);
- if (co->indices != NULL)
- PyMem_Free(co->indices);
- Py_TYPE(co)->tp_free(co);
+ PyObject_GC_UnTrack(co);
+ Py_XDECREF(co->pool);
+ Py_XDECREF(co->result);
+ if (co->indices != NULL)
+ PyMem_Free(co->indices);
+ Py_TYPE(co)->tp_free(co);
}
static int
combinations_traverse(combinationsobject *co, visitproc visit, void *arg)
{
- Py_VISIT(co->pool);
- Py_VISIT(co->result);
- return 0;
+ Py_VISIT(co->pool);
+ Py_VISIT(co->result);
+ return 0;
}
static PyObject *
combinations_next(combinationsobject *co)
{
- PyObject *elem;
- PyObject *oldelem;
- PyObject *pool = co->pool;
- Py_ssize_t *indices = co->indices;
- PyObject *result = co->result;
- Py_ssize_t n = PyTuple_GET_SIZE(pool);
- Py_ssize_t r = co->r;
- Py_ssize_t i, j, index;
-
- if (co->stopped)
- return NULL;
-
- if (result == NULL) {
- /* On the first pass, initialize result tuple using the indices */
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- co->result = result;
- for (i=0; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- } else {
- /* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
- PyObject *old_result = result;
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- co->result = result;
- for (i=0; i<r ; i++) {
- elem = PyTuple_GET_ITEM(old_result, i);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- Py_DECREF(old_result);
- }
- /* Now, we've got the only copy so we can update it in-place
- * CPython's empty tuple is a singleton and cached in
- * PyTuple's freelist.
- */
- assert(r == 0 || Py_REFCNT(result) == 1);
-
- /* Scan indices right-to-left until finding one that is not
- at its maximum (i + n - r). */
- for (i=r-1 ; i >= 0 && indices[i] == i+n-r ; i--)
- ;
-
- /* If i is negative, then the indices are all at
- their maximum value and we're done. */
- if (i < 0)
- goto empty;
-
- /* Increment the current index which we know is not at its
- maximum. Then move back to the right setting each index
- to its lowest possible value (one higher than the index
- to its left -- this maintains the sort order invariant). */
- indices[i]++;
- for (j=i+1 ; j<r ; j++)
- indices[j] = indices[j-1] + 1;
-
- /* Update the result tuple for the new indices
- starting with i, the leftmost index that changed */
- for ( ; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- oldelem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, elem);
- Py_DECREF(oldelem);
- }
- }
-
- Py_INCREF(result);
- return result;
+ PyObject *elem;
+ PyObject *oldelem;
+ PyObject *pool = co->pool;
+ Py_ssize_t *indices = co->indices;
+ PyObject *result = co->result;
+ Py_ssize_t n = PyTuple_GET_SIZE(pool);
+ Py_ssize_t r = co->r;
+ Py_ssize_t i, j, index;
+
+ if (co->stopped)
+ return NULL;
+
+ if (result == NULL) {
+ /* On the first pass, initialize result tuple using the indices */
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ co->result = result;
+ for (i=0; i<r ; i++) {
+ index = indices[i];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ } else {
+ /* Copy the previous result tuple or re-use it if available */
+ if (Py_REFCNT(result) > 1) {
+ PyObject *old_result = result;
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ co->result = result;
+ for (i=0; i<r ; i++) {
+ elem = PyTuple_GET_ITEM(old_result, i);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ Py_DECREF(old_result);
+ }
+ /* Now, we've got the only copy so we can update it in-place
+ * CPython's empty tuple is a singleton and cached in
+ * PyTuple's freelist.
+ */
+ assert(r == 0 || Py_REFCNT(result) == 1);
+
+ /* Scan indices right-to-left until finding one that is not
+ at its maximum (i + n - r). */
+ for (i=r-1 ; i >= 0 && indices[i] == i+n-r ; i--)
+ ;
+
+ /* If i is negative, then the indices are all at
+ their maximum value and we're done. */
+ if (i < 0)
+ goto empty;
+
+ /* Increment the current index which we know is not at its
+ maximum. Then move back to the right setting each index
+ to its lowest possible value (one higher than the index
+ to its left -- this maintains the sort order invariant). */
+ indices[i]++;
+ for (j=i+1 ; j<r ; j++)
+ indices[j] = indices[j-1] + 1;
+
+ /* Update the result tuple for the new indices
+ starting with i, the leftmost index that changed */
+ for ( ; i<r ; i++) {
+ index = indices[i];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
+ }
+ }
+
+ Py_INCREF(result);
+ return result;
empty:
- co->stopped = 1;
- return NULL;
+ co->stopped = 1;
+ return NULL;
}
PyDoc_STRVAR(combinations_doc,
@@ -2023,47 +2023,47 @@ Return successive r-length combinations of elements in the iterable.\n\n\
combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
static PyTypeObject combinations_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.combinations", /* tp_name */
- sizeof(combinationsobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)combinations_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- combinations_doc, /* tp_doc */
- (traverseproc)combinations_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)combinations_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- combinations_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.combinations", /* tp_name */
+ sizeof(combinationsobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)combinations_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ combinations_doc, /* tp_doc */
+ (traverseproc)combinations_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)combinations_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ combinations_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -2071,37 +2071,37 @@ static PyTypeObject combinations_type = {
/* Equivalent to:
- def combinations_with_replacement(iterable, r):
- "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
- # number items returned: (n+r-1)! / r! / (n-1)!
- pool = tuple(iterable)
- n = len(pool)
- indices = [0] * r
- yield tuple(pool[i] for i in indices)
- while 1:
- for i in reversed(range(r)):
- if indices[i] != n - 1:
- break
- else:
- return
- indices[i:] = [indices[i] + 1] * (r - i)
- yield tuple(pool[i] for i in indices)
-
- def combinations_with_replacement2(iterable, r):
- 'Alternate version that filters from product()'
- pool = tuple(iterable)
- n = len(pool)
- for indices in product(range(n), repeat=r):
- if sorted(indices) == list(indices):
- yield tuple(pool[i] for i in indices)
+ def combinations_with_replacement(iterable, r):
+ "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
+ # number items returned: (n+r-1)! / r! / (n-1)!
+ pool = tuple(iterable)
+ n = len(pool)
+ indices = [0] * r
+ yield tuple(pool[i] for i in indices)
+ while 1:
+ for i in reversed(range(r)):
+ if indices[i] != n - 1:
+ break
+ else:
+ return
+ indices[i:] = [indices[i] + 1] * (r - i)
+ yield tuple(pool[i] for i in indices)
+
+ def combinations_with_replacement2(iterable, r):
+ 'Alternate version that filters from product()'
+ pool = tuple(iterable)
+ n = len(pool)
+ for indices in product(range(n), repeat=r):
+ if sorted(indices) == list(indices):
+ yield tuple(pool[i] for i in indices)
*/
typedef struct {
- PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
- Py_ssize_t *indices; /* one index per result element */
- PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the cwr iterator is exhausted */
+ PyObject_HEAD
+ PyObject *pool; /* input converted to a tuple */
+ Py_ssize_t *indices; /* one index per result element */
+ PyObject *result; /* most recently returned result tuple */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the cwr iterator is exhausted */
} cwrobject;
static PyTypeObject cwr_type;
@@ -2109,156 +2109,156 @@ static PyTypeObject cwr_type;
static PyObject *
cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- cwrobject *co;
- Py_ssize_t n;
- Py_ssize_t r;
- PyObject *pool = NULL;
- PyObject *iterable = NULL;
- Py_ssize_t *indices = NULL;
- Py_ssize_t i;
- static char *kwargs[] = {"iterable", "r", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations_with_replacement", kwargs,
- &iterable, &r))
- return NULL;
-
- pool = PySequence_Tuple(iterable);
- if (pool == NULL)
- goto error;
- n = PyTuple_GET_SIZE(pool);
- if (r < 0) {
- PyErr_SetString(PyExc_ValueError, "r must be non-negative");
- goto error;
- }
-
- indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
- if (indices == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- for (i=0 ; i<r ; i++)
- indices[i] = 0;
-
- /* create cwrobject structure */
- co = (cwrobject *)type->tp_alloc(type, 0);
- if (co == NULL)
- goto error;
-
- co->pool = pool;
- co->indices = indices;
- co->result = NULL;
- co->r = r;
- co->stopped = !n && r;
-
- return (PyObject *)co;
+ cwrobject *co;
+ Py_ssize_t n;
+ Py_ssize_t r;
+ PyObject *pool = NULL;
+ PyObject *iterable = NULL;
+ Py_ssize_t *indices = NULL;
+ Py_ssize_t i;
+ static char *kwargs[] = {"iterable", "r", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations_with_replacement", kwargs,
+ &iterable, &r))
+ return NULL;
+
+ pool = PySequence_Tuple(iterable);
+ if (pool == NULL)
+ goto error;
+ n = PyTuple_GET_SIZE(pool);
+ if (r < 0) {
+ PyErr_SetString(PyExc_ValueError, "r must be non-negative");
+ goto error;
+ }
+
+ indices = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+
+ for (i=0 ; i<r ; i++)
+ indices[i] = 0;
+
+ /* create cwrobject structure */
+ co = (cwrobject *)type->tp_alloc(type, 0);
+ if (co == NULL)
+ goto error;
+
+ co->pool = pool;
+ co->indices = indices;
+ co->result = NULL;
+ co->r = r;
+ co->stopped = !n && r;
+
+ return (PyObject *)co;
error:
- if (indices != NULL)
- PyMem_Free(indices);
- Py_XDECREF(pool);
- return NULL;
+ if (indices != NULL)
+ PyMem_Free(indices);
+ Py_XDECREF(pool);
+ return NULL;
}
static void
cwr_dealloc(cwrobject *co)
{
- PyObject_GC_UnTrack(co);
- Py_XDECREF(co->pool);
- Py_XDECREF(co->result);
- if (co->indices != NULL)
- PyMem_Free(co->indices);
- Py_TYPE(co)->tp_free(co);
+ PyObject_GC_UnTrack(co);
+ Py_XDECREF(co->pool);
+ Py_XDECREF(co->result);
+ if (co->indices != NULL)
+ PyMem_Free(co->indices);
+ Py_TYPE(co)->tp_free(co);
}
static int
cwr_traverse(cwrobject *co, visitproc visit, void *arg)
{
- Py_VISIT(co->pool);
- Py_VISIT(co->result);
- return 0;
+ Py_VISIT(co->pool);
+ Py_VISIT(co->result);
+ return 0;
}
static PyObject *
cwr_next(cwrobject *co)
{
- PyObject *elem;
- PyObject *oldelem;
- PyObject *pool = co->pool;
- Py_ssize_t *indices = co->indices;
- PyObject *result = co->result;
- Py_ssize_t n = PyTuple_GET_SIZE(pool);
- Py_ssize_t r = co->r;
- Py_ssize_t i, j, index;
-
- if (co->stopped)
- return NULL;
-
- if (result == NULL) {
- /* On the first pass, initialize result tuple using the indices */
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- co->result = result;
- for (i=0; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- } else {
- /* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
- PyObject *old_result = result;
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- co->result = result;
- for (i=0; i<r ; i++) {
- elem = PyTuple_GET_ITEM(old_result, i);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- Py_DECREF(old_result);
- }
- /* Now, we've got the only copy so we can update it in-place CPython's
- empty tuple is a singleton and cached in PyTuple's freelist. */
- assert(r == 0 || Py_REFCNT(result) == 1);
-
- /* Scan indices right-to-left until finding one that is not
- * at its maximum (n-1). */
- for (i=r-1 ; i >= 0 && indices[i] == n-1; i--)
- ;
+ PyObject *elem;
+ PyObject *oldelem;
+ PyObject *pool = co->pool;
+ Py_ssize_t *indices = co->indices;
+ PyObject *result = co->result;
+ Py_ssize_t n = PyTuple_GET_SIZE(pool);
+ Py_ssize_t r = co->r;
+ Py_ssize_t i, j, index;
+
+ if (co->stopped)
+ return NULL;
+
+ if (result == NULL) {
+ /* On the first pass, initialize result tuple using the indices */
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ co->result = result;
+ for (i=0; i<r ; i++) {
+ index = indices[i];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ } else {
+ /* Copy the previous result tuple or re-use it if available */
+ if (Py_REFCNT(result) > 1) {
+ PyObject *old_result = result;
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ co->result = result;
+ for (i=0; i<r ; i++) {
+ elem = PyTuple_GET_ITEM(old_result, i);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ Py_DECREF(old_result);
+ }
+ /* Now, we've got the only copy so we can update it in-place CPython's
+ empty tuple is a singleton and cached in PyTuple's freelist. */
+ assert(r == 0 || Py_REFCNT(result) == 1);
+
+ /* Scan indices right-to-left until finding one that is not
+ * at its maximum (n-1). */
+ for (i=r-1 ; i >= 0 && indices[i] == n-1; i--)
+ ;
+
+ /* If i is negative, then the indices are all at
+ their maximum value and we're done. */
+ if (i < 0)
+ goto empty;
+
+ /* Increment the current index which we know is not at its
+ maximum. Then set all to the right to the same value. */
+ indices[i]++;
+ for (j=i+1 ; j<r ; j++)
+ indices[j] = indices[j-1];
+
+ /* Update the result tuple for the new indices
+ starting with i, the leftmost index that changed */
+ for ( ; i<r ; i++) {
+ index = indices[i];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ oldelem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, elem);
+ Py_DECREF(oldelem);
+ }
+ }
- /* If i is negative, then the indices are all at
- their maximum value and we're done. */
- if (i < 0)
- goto empty;
-
- /* Increment the current index which we know is not at its
- maximum. Then set all to the right to the same value. */
- indices[i]++;
- for (j=i+1 ; j<r ; j++)
- indices[j] = indices[j-1];
-
- /* Update the result tuple for the new indices
- starting with i, the leftmost index that changed */
- for ( ; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- oldelem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, elem);
- Py_DECREF(oldelem);
- }
- }
-
- Py_INCREF(result);
- return result;
+ Py_INCREF(result);
+ return result;
empty:
- co->stopped = 1;
- return NULL;
+ co->stopped = 1;
+ return NULL;
}
PyDoc_STRVAR(cwr_doc,
@@ -2269,52 +2269,52 @@ allowing individual elements to have successive repeats.\n\
combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC");
static PyTypeObject cwr_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.combinations_with_replacement", /* tp_name */
- sizeof(cwrobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)cwr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- cwr_doc, /* tp_doc */
- (traverseproc)cwr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)cwr_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- cwr_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.combinations_with_replacement", /* tp_name */
+ sizeof(cwrobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)cwr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ cwr_doc, /* tp_doc */
+ (traverseproc)cwr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)cwr_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ cwr_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* permutations object ************************************************************
-
+
def permutations(iterable, r=None):
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
pool = tuple(iterable)
@@ -2324,28 +2324,28 @@ def permutations(iterable, r=None):
cycles = range(n-r+1, n+1)[::-1]
yield tuple(pool[i] for i in indices[:r])
while n:
- for i in reversed(range(r)):
- cycles[i] -= 1
- if cycles[i] == 0:
- indices[i:] = indices[i+1:] + indices[i:i+1]
- cycles[i] = n - i
- else:
- j = cycles[i]
- indices[i], indices[-j] = indices[-j], indices[i]
- yield tuple(pool[i] for i in indices[:r])
- break
+ for i in reversed(range(r)):
+ cycles[i] -= 1
+ if cycles[i] == 0:
+ indices[i:] = indices[i+1:] + indices[i:i+1]
+ cycles[i] = n - i
else:
- return
+ j = cycles[i]
+ indices[i], indices[-j] = indices[-j], indices[i]
+ yield tuple(pool[i] for i in indices[:r])
+ break
+ else:
+ return
*/
typedef struct {
- PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
- Py_ssize_t *indices; /* one index per element in the pool */
- Py_ssize_t *cycles; /* one rollover counter per element in the result */
- PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the permutations iterator is exhausted */
+ PyObject_HEAD
+ PyObject *pool; /* input converted to a tuple */
+ Py_ssize_t *indices; /* one index per element in the pool */
+ Py_ssize_t *cycles; /* one rollover counter per element in the result */
+ PyObject *result; /* most recently returned result tuple */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the permutations iterator is exhausted */
} permutationsobject;
static PyTypeObject permutations_type;
@@ -2353,184 +2353,184 @@ static PyTypeObject permutations_type;
static PyObject *
permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- permutationsobject *po;
- Py_ssize_t n;
- Py_ssize_t r;
- PyObject *robj = Py_None;
- PyObject *pool = NULL;
- PyObject *iterable = NULL;
- Py_ssize_t *indices = NULL;
- Py_ssize_t *cycles = NULL;
- Py_ssize_t i;
- static char *kwargs[] = {"iterable", "r", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:permutations", kwargs,
- &iterable, &robj))
- return NULL;
-
- pool = PySequence_Tuple(iterable);
- if (pool == NULL)
- goto error;
- n = PyTuple_GET_SIZE(pool);
-
- r = n;
- if (robj != Py_None) {
- if (!PyLong_Check(robj)) {
- PyErr_SetString(PyExc_TypeError, "Expected int as r");
- goto error;
- }
- r = PyLong_AsSsize_t(robj);
- if (r == -1 && PyErr_Occurred())
- goto error;
- }
- if (r < 0) {
- PyErr_SetString(PyExc_ValueError, "r must be non-negative");
- goto error;
- }
-
- indices = PyMem_Malloc(n * sizeof(Py_ssize_t));
- cycles = PyMem_Malloc(r * sizeof(Py_ssize_t));
- if (indices == NULL || cycles == NULL) {
- PyErr_NoMemory();
- goto error;
- }
-
- for (i=0 ; i<n ; i++)
- indices[i] = i;
- for (i=0 ; i<r ; i++)
- cycles[i] = n - i;
-
- /* create permutationsobject structure */
- po = (permutationsobject *)type->tp_alloc(type, 0);
- if (po == NULL)
- goto error;
-
- po->pool = pool;
- po->indices = indices;
- po->cycles = cycles;
- po->result = NULL;
- po->r = r;
- po->stopped = r > n ? 1 : 0;
-
- return (PyObject *)po;
+ permutationsobject *po;
+ Py_ssize_t n;
+ Py_ssize_t r;
+ PyObject *robj = Py_None;
+ PyObject *pool = NULL;
+ PyObject *iterable = NULL;
+ Py_ssize_t *indices = NULL;
+ Py_ssize_t *cycles = NULL;
+ Py_ssize_t i;
+ static char *kwargs[] = {"iterable", "r", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:permutations", kwargs,
+ &iterable, &robj))
+ return NULL;
+
+ pool = PySequence_Tuple(iterable);
+ if (pool == NULL)
+ goto error;
+ n = PyTuple_GET_SIZE(pool);
+
+ r = n;
+ if (robj != Py_None) {
+ if (!PyLong_Check(robj)) {
+ PyErr_SetString(PyExc_TypeError, "Expected int as r");
+ goto error;
+ }
+ r = PyLong_AsSsize_t(robj);
+ if (r == -1 && PyErr_Occurred())
+ goto error;
+ }
+ if (r < 0) {
+ PyErr_SetString(PyExc_ValueError, "r must be non-negative");
+ goto error;
+ }
+
+ indices = PyMem_Malloc(n * sizeof(Py_ssize_t));
+ cycles = PyMem_Malloc(r * sizeof(Py_ssize_t));
+ if (indices == NULL || cycles == NULL) {
+ PyErr_NoMemory();
+ goto error;
+ }
+
+ for (i=0 ; i<n ; i++)
+ indices[i] = i;
+ for (i=0 ; i<r ; i++)
+ cycles[i] = n - i;
+
+ /* create permutationsobject structure */
+ po = (permutationsobject *)type->tp_alloc(type, 0);
+ if (po == NULL)
+ goto error;
+
+ po->pool = pool;
+ po->indices = indices;
+ po->cycles = cycles;
+ po->result = NULL;
+ po->r = r;
+ po->stopped = r > n ? 1 : 0;
+
+ return (PyObject *)po;
error:
- if (indices != NULL)
- PyMem_Free(indices);
- if (cycles != NULL)
- PyMem_Free(cycles);
- Py_XDECREF(pool);
- return NULL;
+ if (indices != NULL)
+ PyMem_Free(indices);
+ if (cycles != NULL)
+ PyMem_Free(cycles);
+ Py_XDECREF(pool);
+ return NULL;
}
static void
permutations_dealloc(permutationsobject *po)
{
- PyObject_GC_UnTrack(po);
- Py_XDECREF(po->pool);
- Py_XDECREF(po->result);
- PyMem_Free(po->indices);
- PyMem_Free(po->cycles);
- Py_TYPE(po)->tp_free(po);
+ PyObject_GC_UnTrack(po);
+ Py_XDECREF(po->pool);
+ Py_XDECREF(po->result);
+ PyMem_Free(po->indices);
+ PyMem_Free(po->cycles);
+ Py_TYPE(po)->tp_free(po);
}
static int
permutations_traverse(permutationsobject *po, visitproc visit, void *arg)
{
- Py_VISIT(po->pool);
- Py_VISIT(po->result);
- return 0;
+ Py_VISIT(po->pool);
+ Py_VISIT(po->result);
+ return 0;
}
static PyObject *
permutations_next(permutationsobject *po)
{
- PyObject *elem;
- PyObject *oldelem;
- PyObject *pool = po->pool;
- Py_ssize_t *indices = po->indices;
- Py_ssize_t *cycles = po->cycles;
- PyObject *result = po->result;
- Py_ssize_t n = PyTuple_GET_SIZE(pool);
- Py_ssize_t r = po->r;
- Py_ssize_t i, j, k, index;
-
- if (po->stopped)
- return NULL;
-
- if (result == NULL) {
- /* On the first pass, initialize result tuple using the indices */
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- po->result = result;
- for (i=0; i<r ; i++) {
- index = indices[i];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- } else {
- if (n == 0)
- goto empty;
-
- /* Copy the previous result tuple or re-use it if available */
- if (Py_REFCNT(result) > 1) {
- PyObject *old_result = result;
- result = PyTuple_New(r);
- if (result == NULL)
- goto empty;
- po->result = result;
- for (i=0; i<r ; i++) {
- elem = PyTuple_GET_ITEM(old_result, i);
- Py_INCREF(elem);
- PyTuple_SET_ITEM(result, i, elem);
- }
- Py_DECREF(old_result);
- }
- /* Now, we've got the only copy so we can update it in-place */
- assert(r == 0 || Py_REFCNT(result) == 1);
-
- /* Decrement rightmost cycle, moving leftward upon zero rollover */
- for (i=r-1 ; i>=0 ; i--) {
- cycles[i] -= 1;
- if (cycles[i] == 0) {
- /* rotatation: indices[i:] = indices[i+1:] + indices[i:i+1] */
- index = indices[i];
- for (j=i ; j<n-1 ; j++)
- indices[j] = indices[j+1];
- indices[n-1] = index;
- cycles[i] = n - i;
- } else {
- j = cycles[i];
- index = indices[i];
- indices[i] = indices[n-j];
- indices[n-j] = index;
-
- for (k=i; k<r ; k++) {
- /* start with i, the leftmost element that changed */
- /* yield tuple(pool[k] for k in indices[:r]) */
- index = indices[k];
- elem = PyTuple_GET_ITEM(pool, index);
- Py_INCREF(elem);
- oldelem = PyTuple_GET_ITEM(result, k);
- PyTuple_SET_ITEM(result, k, elem);
- Py_DECREF(oldelem);
- }
- break;
- }
- }
- /* If i is negative, then the cycles have all
- rolled-over and we're done. */
- if (i < 0)
- goto empty;
- }
- Py_INCREF(result);
- return result;
+ PyObject *elem;
+ PyObject *oldelem;
+ PyObject *pool = po->pool;
+ Py_ssize_t *indices = po->indices;
+ Py_ssize_t *cycles = po->cycles;
+ PyObject *result = po->result;
+ Py_ssize_t n = PyTuple_GET_SIZE(pool);
+ Py_ssize_t r = po->r;
+ Py_ssize_t i, j, k, index;
+
+ if (po->stopped)
+ return NULL;
+
+ if (result == NULL) {
+ /* On the first pass, initialize result tuple using the indices */
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ po->result = result;
+ for (i=0; i<r ; i++) {
+ index = indices[i];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ } else {
+ if (n == 0)
+ goto empty;
+
+ /* Copy the previous result tuple or re-use it if available */
+ if (Py_REFCNT(result) > 1) {
+ PyObject *old_result = result;
+ result = PyTuple_New(r);
+ if (result == NULL)
+ goto empty;
+ po->result = result;
+ for (i=0; i<r ; i++) {
+ elem = PyTuple_GET_ITEM(old_result, i);
+ Py_INCREF(elem);
+ PyTuple_SET_ITEM(result, i, elem);
+ }
+ Py_DECREF(old_result);
+ }
+ /* Now, we've got the only copy so we can update it in-place */
+ assert(r == 0 || Py_REFCNT(result) == 1);
+
+ /* Decrement rightmost cycle, moving leftward upon zero rollover */
+ for (i=r-1 ; i>=0 ; i--) {
+ cycles[i] -= 1;
+ if (cycles[i] == 0) {
+ /* rotatation: indices[i:] = indices[i+1:] + indices[i:i+1] */
+ index = indices[i];
+ for (j=i ; j<n-1 ; j++)
+ indices[j] = indices[j+1];
+ indices[n-1] = index;
+ cycles[i] = n - i;
+ } else {
+ j = cycles[i];
+ index = indices[i];
+ indices[i] = indices[n-j];
+ indices[n-j] = index;
+
+ for (k=i; k<r ; k++) {
+ /* start with i, the leftmost element that changed */
+ /* yield tuple(pool[k] for k in indices[:r]) */
+ index = indices[k];
+ elem = PyTuple_GET_ITEM(pool, index);
+ Py_INCREF(elem);
+ oldelem = PyTuple_GET_ITEM(result, k);
+ PyTuple_SET_ITEM(result, k, elem);
+ Py_DECREF(oldelem);
+ }
+ break;
+ }
+ }
+ /* If i is negative, then the cycles have all
+ rolled-over and we're done. */
+ if (i < 0)
+ goto empty;
+ }
+ Py_INCREF(result);
+ return result;
empty:
- po->stopped = 1;
- return NULL;
+ po->stopped = 1;
+ return NULL;
}
PyDoc_STRVAR(permutations_doc,
@@ -2540,47 +2540,47 @@ Return successive r-length permutations of elements in the iterable.\n\n\
permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
static PyTypeObject permutations_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.permutations", /* tp_name */
- sizeof(permutationsobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)permutations_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- permutations_doc, /* tp_doc */
- (traverseproc)permutations_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)permutations_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- permutations_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.permutations", /* tp_name */
+ sizeof(permutationsobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)permutations_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ permutations_doc, /* tp_doc */
+ (traverseproc)permutations_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)permutations_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ permutations_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -2588,15 +2588,15 @@ static PyTypeObject permutations_type = {
/* Equivalent to:
- def compress(data, selectors):
- "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
- return (d for d, s in zip(data, selectors) if s)
+ def compress(data, selectors):
+ "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
+ return (d for d, s in zip(data, selectors) if s)
*/
typedef struct {
- PyObject_HEAD
- PyObject *data;
- PyObject *selectors;
+ PyObject_HEAD
+ PyObject *data;
+ PyObject *selectors;
} compressobject;
static PyTypeObject compress_type;
@@ -2604,86 +2604,86 @@ static PyTypeObject compress_type;
static PyObject *
compress_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *seq1, *seq2;
- PyObject *data=NULL, *selectors=NULL;
- compressobject *lz;
- static char *kwargs[] = {"data", "selectors", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:compress", kwargs, &seq1, &seq2))
- return NULL;
-
- data = PyObject_GetIter(seq1);
- if (data == NULL)
- goto fail;
- selectors = PyObject_GetIter(seq2);
- if (selectors == NULL)
- goto fail;
-
- /* create compressobject structure */
- lz = (compressobject *)type->tp_alloc(type, 0);
- if (lz == NULL)
- goto fail;
- lz->data = data;
- lz->selectors = selectors;
- return (PyObject *)lz;
+ PyObject *seq1, *seq2;
+ PyObject *data=NULL, *selectors=NULL;
+ compressobject *lz;
+ static char *kwargs[] = {"data", "selectors", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:compress", kwargs, &seq1, &seq2))
+ return NULL;
+
+ data = PyObject_GetIter(seq1);
+ if (data == NULL)
+ goto fail;
+ selectors = PyObject_GetIter(seq2);
+ if (selectors == NULL)
+ goto fail;
+
+ /* create compressobject structure */
+ lz = (compressobject *)type->tp_alloc(type, 0);
+ if (lz == NULL)
+ goto fail;
+ lz->data = data;
+ lz->selectors = selectors;
+ return (PyObject *)lz;
fail:
- Py_XDECREF(data);
- Py_XDECREF(selectors);
- return NULL;
+ Py_XDECREF(data);
+ Py_XDECREF(selectors);
+ return NULL;
}
static void
compress_dealloc(compressobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->data);
- Py_XDECREF(lz->selectors);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->data);
+ Py_XDECREF(lz->selectors);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
compress_traverse(compressobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->data);
- Py_VISIT(lz->selectors);
- return 0;
+ Py_VISIT(lz->data);
+ Py_VISIT(lz->selectors);
+ return 0;
}
static PyObject *
compress_next(compressobject *lz)
{
- PyObject *data = lz->data, *selectors = lz->selectors;
- PyObject *datum, *selector;
- PyObject *(*datanext)(PyObject *) = *Py_TYPE(data)->tp_iternext;
- PyObject *(*selectornext)(PyObject *) = *Py_TYPE(selectors)->tp_iternext;
- int ok;
-
- while (1) {
- /* Steps: get datum, get selector, evaluate selector.
- Order is important (to match the pure python version
- in terms of which input gets a chance to raise an
- exception first).
- */
-
- datum = datanext(data);
- if (datum == NULL)
- return NULL;
-
- selector = selectornext(selectors);
- if (selector == NULL) {
- Py_DECREF(datum);
- return NULL;
- }
-
- ok = PyObject_IsTrue(selector);
- Py_DECREF(selector);
- if (ok == 1)
- return datum;
- Py_DECREF(datum);
- if (ok == -1)
- return NULL;
- }
+ PyObject *data = lz->data, *selectors = lz->selectors;
+ PyObject *datum, *selector;
+ PyObject *(*datanext)(PyObject *) = *Py_TYPE(data)->tp_iternext;
+ PyObject *(*selectornext)(PyObject *) = *Py_TYPE(selectors)->tp_iternext;
+ int ok;
+
+ while (1) {
+ /* Steps: get datum, get selector, evaluate selector.
+ Order is important (to match the pure python version
+ in terms of which input gets a chance to raise an
+ exception first).
+ */
+
+ datum = datanext(data);
+ if (datum == NULL)
+ return NULL;
+
+ selector = selectornext(selectors);
+ if (selector == NULL) {
+ Py_DECREF(datum);
+ return NULL;
+ }
+
+ ok = PyObject_IsTrue(selector);
+ Py_DECREF(selector);
+ if (ok == 1)
+ return datum;
+ Py_DECREF(datum);
+ if (ok == -1)
+ return NULL;
+ }
}
PyDoc_STRVAR(compress_doc,
@@ -2694,56 +2694,56 @@ Forms a shorter iterator from selected data elements using the\n\
selectors to choose the data elements.");
static PyTypeObject compress_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.compress", /* tp_name */
- sizeof(compressobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)compress_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- compress_doc, /* tp_doc */
- (traverseproc)compress_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)compress_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- compress_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.compress", /* tp_name */
+ sizeof(compressobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)compress_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ compress_doc, /* tp_doc */
+ (traverseproc)compress_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)compress_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ compress_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* filterfalse object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
+ PyObject_HEAD
+ PyObject *func;
+ PyObject *it;
} filterfalseobject;
static PyTypeObject filterfalse_type;
@@ -2751,83 +2751,83 @@ static PyTypeObject filterfalse_type;
static PyObject *
filterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *func, *seq;
- PyObject *it;
- filterfalseobject *lz;
+ PyObject *func, *seq;
+ PyObject *it;
+ filterfalseobject *lz;
- if (type == &filterfalse_type &&
- !_PyArg_NoKeywords("filterfalse()", kwds))
- return NULL;
+ if (type == &filterfalse_type &&
+ !_PyArg_NoKeywords("filterfalse()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "filterfalse", 2, 2, &func, &seq))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "filterfalse", 2, 2, &func, &seq))
+ return NULL;
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
- /* create filterfalseobject structure */
- lz = (filterfalseobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
+ /* create filterfalseobject structure */
+ lz = (filterfalseobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_INCREF(func);
+ lz->func = func;
+ lz->it = it;
- return (PyObject *)lz;
+ return (PyObject *)lz;
}
static void
filterfalse_dealloc(filterfalseobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->func);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
filterfalse_traverse(filterfalseobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
filterfalse_next(filterfalseobject *lz)
{
- PyObject *item;
- PyObject *it = lz->it;
- long ok;
- PyObject *(*iternext)(PyObject *);
-
- iternext = *Py_TYPE(it)->tp_iternext;
- for (;;) {
- item = iternext(it);
- if (item == NULL)
- return NULL;
-
- if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
- ok = PyObject_IsTrue(item);
- } else {
- PyObject *good;
- good = PyObject_CallFunctionObjArgs(lz->func,
- item, NULL);
- if (good == NULL) {
- Py_DECREF(item);
- return NULL;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- }
- if (!ok)
- return item;
- Py_DECREF(item);
- }
+ PyObject *item;
+ PyObject *it = lz->it;
+ long ok;
+ PyObject *(*iternext)(PyObject *);
+
+ iternext = *Py_TYPE(it)->tp_iternext;
+ for (;;) {
+ item = iternext(it);
+ if (item == NULL)
+ return NULL;
+
+ if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
+ ok = PyObject_IsTrue(item);
+ } else {
+ PyObject *good;
+ good = PyObject_CallFunctionObjArgs(lz->func,
+ item, NULL);
+ if (good == NULL) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ }
+ if (!ok)
+ return item;
+ Py_DECREF(item);
+ }
}
PyDoc_STRVAR(filterfalse_doc,
@@ -2837,74 +2837,74 @@ Return those items of sequence for which function(item) is false.\n\
If function is None, return the items that are false.");
static PyTypeObject filterfalse_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.filterfalse", /* tp_name */
- sizeof(filterfalseobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)filterfalse_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- filterfalse_doc, /* tp_doc */
- (traverseproc)filterfalse_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)filterfalse_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- filterfalse_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.filterfalse", /* tp_name */
+ sizeof(filterfalseobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)filterfalse_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ filterfalse_doc, /* tp_doc */
+ (traverseproc)filterfalse_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)filterfalse_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ filterfalse_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* count object ************************************************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t cnt;
- PyObject *long_cnt;
- PyObject *long_step;
+ PyObject_HEAD
+ Py_ssize_t cnt;
+ PyObject *long_cnt;
+ PyObject *long_step;
} countobject;
/* Counting logic and invariants:
fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
- assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1));
- Advances with: cnt += 1
- When count hits Y_SSIZE_T_MAX, switch to slow_mode.
+ assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1));
+ Advances with: cnt += 1
+ When count hits Y_SSIZE_T_MAX, switch to slow_mode.
slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float.
- assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL);
- All counting is done with python objects (no overflows or underflows).
- Advances with: long_cnt += long_step
- Step may be zero -- effectively a slow version of repeat(cnt).
- Either long_cnt or long_step may be a float, Fraction, or Decimal.
+ assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL);
+ All counting is done with python objects (no overflows or underflows).
+ Advances with: long_cnt += long_step
+ Step may be zero -- effectively a slow version of repeat(cnt).
+ Either long_cnt or long_step may be a float, Fraction, or Decimal.
*/
static PyTypeObject count_type;
@@ -2912,221 +2912,221 @@ static PyTypeObject count_type;
static PyObject *
count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- countobject *lz;
- int slow_mode = 0;
- Py_ssize_t cnt = 0;
- PyObject *long_cnt = NULL;
- PyObject *long_step = NULL;
- static char *kwlist[] = {"start", "step", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count",
- kwlist, &long_cnt, &long_step))
- return NULL;
-
- if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) ||
- (long_step != NULL && !PyNumber_Check(long_step))) {
- PyErr_SetString(PyExc_TypeError, "a number is required");
- return NULL;
- }
-
- if (long_cnt != NULL) {
- cnt = PyLong_AsSsize_t(long_cnt);
- if ((cnt == -1 && PyErr_Occurred()) || !PyLong_Check(long_cnt)) {
- PyErr_Clear();
- slow_mode = 1;
- }
- Py_INCREF(long_cnt);
- } else {
- cnt = 0;
- long_cnt = PyLong_FromLong(0);
- }
-
- /* If not specified, step defaults to 1 */
- if (long_step == NULL) {
- long_step = PyLong_FromLong(1);
- if (long_step == NULL) {
- Py_DECREF(long_cnt);
- return NULL;
- }
- } else
- Py_INCREF(long_step);
-
- assert(long_cnt != NULL && long_step != NULL);
-
- /* Fast mode only works when the step is 1 */
- if (!PyLong_Check(long_step) ||
- PyLong_AS_LONG(long_step) != 1) {
- slow_mode = 1;
- }
-
- if (slow_mode)
- cnt = PY_SSIZE_T_MAX;
- else
- Py_CLEAR(long_cnt);
-
- assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && !slow_mode) ||
- (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && slow_mode));
- assert(slow_mode ||
- (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1));
-
- /* create countobject structure */
- lz = (countobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_XDECREF(long_cnt);
- return NULL;
- }
- lz->cnt = cnt;
- lz->long_cnt = long_cnt;
- lz->long_step = long_step;
-
- return (PyObject *)lz;
+ countobject *lz;
+ int slow_mode = 0;
+ Py_ssize_t cnt = 0;
+ PyObject *long_cnt = NULL;
+ PyObject *long_step = NULL;
+ static char *kwlist[] = {"start", "step", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count",
+ kwlist, &long_cnt, &long_step))
+ return NULL;
+
+ if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) ||
+ (long_step != NULL && !PyNumber_Check(long_step))) {
+ PyErr_SetString(PyExc_TypeError, "a number is required");
+ return NULL;
+ }
+
+ if (long_cnt != NULL) {
+ cnt = PyLong_AsSsize_t(long_cnt);
+ if ((cnt == -1 && PyErr_Occurred()) || !PyLong_Check(long_cnt)) {
+ PyErr_Clear();
+ slow_mode = 1;
+ }
+ Py_INCREF(long_cnt);
+ } else {
+ cnt = 0;
+ long_cnt = PyLong_FromLong(0);
+ }
+
+ /* If not specified, step defaults to 1 */
+ if (long_step == NULL) {
+ long_step = PyLong_FromLong(1);
+ if (long_step == NULL) {
+ Py_DECREF(long_cnt);
+ return NULL;
+ }
+ } else
+ Py_INCREF(long_step);
+
+ assert(long_cnt != NULL && long_step != NULL);
+
+ /* Fast mode only works when the step is 1 */
+ if (!PyLong_Check(long_step) ||
+ PyLong_AS_LONG(long_step) != 1) {
+ slow_mode = 1;
+ }
+
+ if (slow_mode)
+ cnt = PY_SSIZE_T_MAX;
+ else
+ Py_CLEAR(long_cnt);
+
+ assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && !slow_mode) ||
+ (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && slow_mode));
+ assert(slow_mode ||
+ (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1));
+
+ /* create countobject structure */
+ lz = (countobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_XDECREF(long_cnt);
+ return NULL;
+ }
+ lz->cnt = cnt;
+ lz->long_cnt = long_cnt;
+ lz->long_step = long_step;
+
+ return (PyObject *)lz;
}
static void
count_dealloc(countobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->long_cnt);
- Py_XDECREF(lz->long_step);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->long_cnt);
+ Py_XDECREF(lz->long_step);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
count_traverse(countobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->long_cnt);
- Py_VISIT(lz->long_step);
- return 0;
+ Py_VISIT(lz->long_cnt);
+ Py_VISIT(lz->long_step);
+ return 0;
}
static PyObject *
count_nextlong(countobject *lz)
{
- PyObject *long_cnt;
- PyObject *stepped_up;
+ PyObject *long_cnt;
+ PyObject *stepped_up;
- long_cnt = lz->long_cnt;
- if (long_cnt == NULL) {
- /* Switch to slow_mode */
- long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
- if (long_cnt == NULL)
- return NULL;
- }
- assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL);
+ long_cnt = lz->long_cnt;
+ if (long_cnt == NULL) {
+ /* Switch to slow_mode */
+ long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
+ if (long_cnt == NULL)
+ return NULL;
+ }
+ assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL);
- stepped_up = PyNumber_Add(long_cnt, lz->long_step);
- if (stepped_up == NULL)
- return NULL;
- lz->long_cnt = stepped_up;
- return long_cnt;
+ stepped_up = PyNumber_Add(long_cnt, lz->long_step);
+ if (stepped_up == NULL)
+ return NULL;
+ lz->long_cnt = stepped_up;
+ return long_cnt;
}
static PyObject *
count_next(countobject *lz)
{
- if (lz->cnt == PY_SSIZE_T_MAX)
- return count_nextlong(lz);
- return PyLong_FromSsize_t(lz->cnt++);
+ if (lz->cnt == PY_SSIZE_T_MAX)
+ return count_nextlong(lz);
+ return PyLong_FromSsize_t(lz->cnt++);
}
static PyObject *
count_repr(countobject *lz)
{
- if (lz->cnt != PY_SSIZE_T_MAX)
- return PyUnicode_FromFormat("count(%zd)", lz->cnt);
+ if (lz->cnt != PY_SSIZE_T_MAX)
+ return PyUnicode_FromFormat("count(%zd)", lz->cnt);
- if (PyLong_Check(lz->long_step)) {
- long step = PyLong_AsLong(lz->long_step);
- if (step == -1 && PyErr_Occurred()) {
- PyErr_Clear();
- }
- if (step == 1) {
- /* Don't display step when it is an integer equal to 1 */
- return PyUnicode_FromFormat("count(%R)", lz->long_cnt);
- }
- }
- return PyUnicode_FromFormat("count(%R, %R)",
- lz->long_cnt, lz->long_step);
+ if (PyLong_Check(lz->long_step)) {
+ long step = PyLong_AsLong(lz->long_step);
+ if (step == -1 && PyErr_Occurred()) {
+ PyErr_Clear();
+ }
+ if (step == 1) {
+ /* Don't display step when it is an integer equal to 1 */
+ return PyUnicode_FromFormat("count(%R)", lz->long_cnt);
+ }
+ }
+ return PyUnicode_FromFormat("count(%R, %R)",
+ lz->long_cnt, lz->long_step);
}
static PyObject *
count_reduce(countobject *lz)
{
- if (lz->cnt == PY_SSIZE_T_MAX)
- return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step);
- return Py_BuildValue("O(n)", Py_TYPE(lz), lz->cnt);
+ if (lz->cnt == PY_SSIZE_T_MAX)
+ return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step);
+ return Py_BuildValue("O(n)", Py_TYPE(lz), lz->cnt);
}
PyDoc_STRVAR(count_reduce_doc, "Return state information for pickling.");
static PyMethodDef count_methods[] = {
- {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS,
- count_reduce_doc},
- {NULL, NULL} /* sentinel */
+ {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS,
+ count_reduce_doc},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(count_doc,
- "count(start=0, step=1]) --> count object\n\
+ "count(start=0, step=1]) --> count object\n\
\n\
Return a count object whose .__next__() method returns consecutive values.\n\
Equivalent to:\n\n\
def count(firstval=0, step=1):\n\
- x = firstval\n\
- while 1:\n\
- yield x\n\
- x += step\n");
+ x = firstval\n\
+ while 1:\n\
+ yield x\n\
+ x += step\n");
static PyTypeObject count_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.count", /* tp_name */
- sizeof(countobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)count_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)count_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- count_doc, /* tp_doc */
- (traverseproc)count_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)count_next, /* tp_iternext */
- count_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- count_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.count", /* tp_name */
+ sizeof(countobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)count_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)count_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ count_doc, /* tp_doc */
+ (traverseproc)count_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)count_next, /* tp_iternext */
+ count_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ count_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* repeat object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *element;
- Py_ssize_t cnt;
+ PyObject_HEAD
+ PyObject *element;
+ Py_ssize_t cnt;
} repeatobject;
static PyTypeObject repeat_type;
@@ -3134,77 +3134,77 @@ static PyTypeObject repeat_type;
static PyObject *
repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- repeatobject *ro;
- PyObject *element;
- Py_ssize_t cnt = -1;
- static char *kwargs[] = {"object", "times", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
- &element, &cnt))
- return NULL;
+ repeatobject *ro;
+ PyObject *element;
+ Py_ssize_t cnt = -1;
+ static char *kwargs[] = {"object", "times", NULL};
- if (PyTuple_Size(args) == 2 && cnt < 0)
- cnt = 0;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
+ &element, &cnt))
+ return NULL;
- ro = (repeatobject *)type->tp_alloc(type, 0);
- if (ro == NULL)
- return NULL;
- Py_INCREF(element);
- ro->element = element;
- ro->cnt = cnt;
- return (PyObject *)ro;
+ if (PyTuple_Size(args) == 2 && cnt < 0)
+ cnt = 0;
+
+ ro = (repeatobject *)type->tp_alloc(type, 0);
+ if (ro == NULL)
+ return NULL;
+ Py_INCREF(element);
+ ro->element = element;
+ ro->cnt = cnt;
+ return (PyObject *)ro;
}
static void
repeat_dealloc(repeatobject *ro)
{
- PyObject_GC_UnTrack(ro);
- Py_XDECREF(ro->element);
- Py_TYPE(ro)->tp_free(ro);
+ PyObject_GC_UnTrack(ro);
+ Py_XDECREF(ro->element);
+ Py_TYPE(ro)->tp_free(ro);
}
static int
repeat_traverse(repeatobject *ro, visitproc visit, void *arg)
{
- Py_VISIT(ro->element);
- return 0;
+ Py_VISIT(ro->element);
+ return 0;
}
static PyObject *
repeat_next(repeatobject *ro)
{
- if (ro->cnt == 0)
- return NULL;
- if (ro->cnt > 0)
- ro->cnt--;
- Py_INCREF(ro->element);
- return ro->element;
+ if (ro->cnt == 0)
+ return NULL;
+ if (ro->cnt > 0)
+ ro->cnt--;
+ Py_INCREF(ro->element);
+ return ro->element;
}
static PyObject *
repeat_repr(repeatobject *ro)
{
- if (ro->cnt == -1)
- return PyUnicode_FromFormat("repeat(%R)", ro->element);
- else
- return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt);
-}
+ if (ro->cnt == -1)
+ return PyUnicode_FromFormat("repeat(%R)", ro->element);
+ else
+ return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt);
+}
static PyObject *
repeat_len(repeatobject *ro)
{
- if (ro->cnt == -1) {
- PyErr_SetString(PyExc_TypeError, "len() of unsized object");
- return NULL;
- }
- return PyLong_FromSize_t(ro->cnt);
+ if (ro->cnt == -1) {
+ PyErr_SetString(PyExc_TypeError, "len() of unsized object");
+ return NULL;
+ }
+ return PyLong_FromSize_t(ro->cnt);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef repeat_methods[] = {
- {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(repeat_doc,
@@ -3213,47 +3213,47 @@ for the specified number of times. If not specified, returns the object\n\
endlessly.");
static PyTypeObject repeat_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.repeat", /* tp_name */
- sizeof(repeatobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)repeat_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)repeat_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- repeat_doc, /* tp_doc */
- (traverseproc)repeat_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)repeat_next, /* tp_iternext */
- repeat_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- repeat_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.repeat", /* tp_name */
+ sizeof(repeatobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)repeat_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)repeat_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ repeat_doc, /* tp_doc */
+ (traverseproc)repeat_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)repeat_next, /* tp_iternext */
+ repeat_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ repeat_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* ziplongest object ************************************************************/
@@ -3261,12 +3261,12 @@ static PyTypeObject repeat_type = {
#include "Python.h"
typedef struct {
- PyObject_HEAD
- Py_ssize_t tuplesize;
- Py_ssize_t numactive;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
- PyObject *fillvalue;
+ PyObject_HEAD
+ Py_ssize_t tuplesize;
+ Py_ssize_t numactive;
+ PyObject *ittuple; /* tuple of iterators */
+ PyObject *result;
+ PyObject *fillvalue;
} ziplongestobject;
static PyTypeObject ziplongest_type;
@@ -3274,159 +3274,159 @@ static PyTypeObject ziplongest_type;
static PyObject *
zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- ziplongestobject *lz;
- Py_ssize_t i;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
- PyObject *fillvalue = Py_None;
- Py_ssize_t tuplesize = PySequence_Length(args);
-
- if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_Size(kwds) > 0) {
- fillvalue = PyDict_GetItemString(kwds, "fillvalue");
- if (fillvalue == NULL || PyDict_Size(kwds) > 1) {
- PyErr_SetString(PyExc_TypeError,
- "zip_longest() got an unexpected keyword argument");
- return NULL;
- }
+ ziplongestobject *lz;
+ Py_ssize_t i;
+ PyObject *ittuple; /* tuple of iterators */
+ PyObject *result;
+ PyObject *fillvalue = Py_None;
+ Py_ssize_t tuplesize = PySequence_Length(args);
+
+ if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_Size(kwds) > 0) {
+ fillvalue = PyDict_GetItemString(kwds, "fillvalue");
+ if (fillvalue == NULL || PyDict_Size(kwds) > 1) {
+ PyErr_SetString(PyExc_TypeError,
+ "zip_longest() got an unexpected keyword argument");
+ return NULL;
}
-
- /* args must be a tuple */
- assert(PyTuple_Check(args));
-
- /* obtain iterators */
- ittuple = PyTuple_New(tuplesize);
- if (ittuple == NULL)
- return NULL;
- for (i=0; i < tuplesize; ++i) {
- PyObject *item = PyTuple_GET_ITEM(args, i);
- PyObject *it = PyObject_GetIter(item);
- if (it == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError,
- "zip_longest argument #%zd must support iteration",
- i+1);
- Py_DECREF(ittuple);
- return NULL;
- }
- PyTuple_SET_ITEM(ittuple, i, it);
- }
-
- /* create a result holder */
- result = PyTuple_New(tuplesize);
- if (result == NULL) {
- Py_DECREF(ittuple);
- return NULL;
- }
- for (i=0 ; i < tuplesize ; i++) {
- Py_INCREF(Py_None);
- PyTuple_SET_ITEM(result, i, Py_None);
- }
-
- /* create ziplongestobject structure */
- lz = (ziplongestobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(ittuple);
- Py_DECREF(result);
- return NULL;
- }
- lz->ittuple = ittuple;
- lz->tuplesize = tuplesize;
- lz->numactive = tuplesize;
- lz->result = result;
- Py_INCREF(fillvalue);
- lz->fillvalue = fillvalue;
- return (PyObject *)lz;
+ }
+
+ /* args must be a tuple */
+ assert(PyTuple_Check(args));
+
+ /* obtain iterators */
+ ittuple = PyTuple_New(tuplesize);
+ if (ittuple == NULL)
+ return NULL;
+ for (i=0; i < tuplesize; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(args, i);
+ PyObject *it = PyObject_GetIter(item);
+ if (it == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "zip_longest argument #%zd must support iteration",
+ i+1);
+ Py_DECREF(ittuple);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(ittuple, i, it);
+ }
+
+ /* create a result holder */
+ result = PyTuple_New(tuplesize);
+ if (result == NULL) {
+ Py_DECREF(ittuple);
+ return NULL;
+ }
+ for (i=0 ; i < tuplesize ; i++) {
+ Py_INCREF(Py_None);
+ PyTuple_SET_ITEM(result, i, Py_None);
+ }
+
+ /* create ziplongestobject structure */
+ lz = (ziplongestobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(ittuple);
+ Py_DECREF(result);
+ return NULL;
+ }
+ lz->ittuple = ittuple;
+ lz->tuplesize = tuplesize;
+ lz->numactive = tuplesize;
+ lz->result = result;
+ Py_INCREF(fillvalue);
+ lz->fillvalue = fillvalue;
+ return (PyObject *)lz;
}
static void
zip_longest_dealloc(ziplongestobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->ittuple);
- Py_XDECREF(lz->result);
- Py_XDECREF(lz->fillvalue);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->ittuple);
+ Py_XDECREF(lz->result);
+ Py_XDECREF(lz->fillvalue);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
zip_longest_traverse(ziplongestobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->ittuple);
- Py_VISIT(lz->result);
- Py_VISIT(lz->fillvalue);
- return 0;
+ Py_VISIT(lz->ittuple);
+ Py_VISIT(lz->result);
+ Py_VISIT(lz->fillvalue);
+ return 0;
}
static PyObject *
zip_longest_next(ziplongestobject *lz)
{
- Py_ssize_t i;
- Py_ssize_t tuplesize = lz->tuplesize;
- PyObject *result = lz->result;
- PyObject *it;
- PyObject *item;
- PyObject *olditem;
-
- if (tuplesize == 0)
- return NULL;
- if (lz->numactive == 0)
- return NULL;
- if (Py_REFCNT(result) == 1) {
- Py_INCREF(result);
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- if (it == NULL) {
- Py_INCREF(lz->fillvalue);
- item = lz->fillvalue;
- } else {
- item = PyIter_Next(it);
- if (item == NULL) {
- lz->numactive -= 1;
- if (lz->numactive == 0 || PyErr_Occurred()) {
- lz->numactive = 0;
- Py_DECREF(result);
- return NULL;
- } else {
- Py_INCREF(lz->fillvalue);
- item = lz->fillvalue;
- PyTuple_SET_ITEM(lz->ittuple, i, NULL);
- Py_DECREF(it);
- }
- }
- }
- olditem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, item);
- Py_DECREF(olditem);
+ Py_ssize_t i;
+ Py_ssize_t tuplesize = lz->tuplesize;
+ PyObject *result = lz->result;
+ PyObject *it;
+ PyObject *item;
+ PyObject *olditem;
+
+ if (tuplesize == 0)
+ return NULL;
+ if (lz->numactive == 0)
+ return NULL;
+ if (Py_REFCNT(result) == 1) {
+ Py_INCREF(result);
+ for (i=0 ; i < tuplesize ; i++) {
+ it = PyTuple_GET_ITEM(lz->ittuple, i);
+ if (it == NULL) {
+ Py_INCREF(lz->fillvalue);
+ item = lz->fillvalue;
+ } else {
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ lz->numactive -= 1;
+ if (lz->numactive == 0 || PyErr_Occurred()) {
+ lz->numactive = 0;
+ Py_DECREF(result);
+ return NULL;
+ } else {
+ Py_INCREF(lz->fillvalue);
+ item = lz->fillvalue;
+ PyTuple_SET_ITEM(lz->ittuple, i, NULL);
+ Py_DECREF(it);
+ }
}
- } else {
- result = PyTuple_New(tuplesize);
- if (result == NULL)
+ }
+ olditem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, item);
+ Py_DECREF(olditem);
+ }
+ } else {
+ result = PyTuple_New(tuplesize);
+ if (result == NULL)
+ return NULL;
+ for (i=0 ; i < tuplesize ; i++) {
+ it = PyTuple_GET_ITEM(lz->ittuple, i);
+ if (it == NULL) {
+ Py_INCREF(lz->fillvalue);
+ item = lz->fillvalue;
+ } else {
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ lz->numactive -= 1;
+ if (lz->numactive == 0 || PyErr_Occurred()) {
+ lz->numactive = 0;
+ Py_DECREF(result);
return NULL;
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- if (it == NULL) {
- Py_INCREF(lz->fillvalue);
- item = lz->fillvalue;
- } else {
- item = PyIter_Next(it);
- if (item == NULL) {
- lz->numactive -= 1;
- if (lz->numactive == 0 || PyErr_Occurred()) {
- lz->numactive = 0;
- Py_DECREF(result);
- return NULL;
- } else {
- Py_INCREF(lz->fillvalue);
- item = lz->fillvalue;
- PyTuple_SET_ITEM(lz->ittuple, i, NULL);
- Py_DECREF(it);
- }
- }
- }
- PyTuple_SET_ITEM(result, i, item);
+ } else {
+ Py_INCREF(lz->fillvalue);
+ item = lz->fillvalue;
+ PyTuple_SET_ITEM(lz->ittuple, i, NULL);
+ Py_DECREF(it);
+ }
}
+ }
+ PyTuple_SET_ITEM(result, i, item);
}
- return result;
+ }
+ return result;
}
PyDoc_STRVAR(zip_longest_doc,
@@ -3441,47 +3441,47 @@ defaults to None or can be specified by a keyword argument.\n\
");
static PyTypeObject ziplongest_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.zip_longest", /* tp_name */
- sizeof(ziplongestobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)zip_longest_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- zip_longest_doc, /* tp_doc */
- (traverseproc)zip_longest_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)zip_longest_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- zip_longest_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "itertools.zip_longest", /* tp_name */
+ sizeof(ziplongestobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)zip_longest_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ zip_longest_doc, /* tp_doc */
+ (traverseproc)zip_longest_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)zip_longest_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ zip_longest_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* module level code ********************************************************/
@@ -3516,68 +3516,68 @@ combinations_with_replacement(p, r)\n\
static PyMethodDef module_methods[] = {
- {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc},
- {NULL, NULL} /* sentinel */
+ {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef itertoolsmodule = {
- PyModuleDef_HEAD_INIT,
- "itertools",
- module_doc,
- -1,
- module_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "itertools",
+ module_doc,
+ -1,
+ module_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_itertools(void)
{
- int i;
- PyObject *m;
- char *name;
- PyTypeObject *typelist[] = {
- &combinations_type,
- &cwr_type,
- &cycle_type,
- &dropwhile_type,
- &takewhile_type,
- &islice_type,
- &starmap_type,
- &chain_type,
- &compress_type,
- &filterfalse_type,
- &count_type,
- &ziplongest_type,
- &permutations_type,
- &product_type,
- &repeat_type,
- &groupby_type,
- NULL
- };
-
- Py_TYPE(&teedataobject_type) = &PyType_Type;
- m = PyModule_Create(&itertoolsmodule);
- if (m == NULL)
- return NULL;
-
- for (i=0 ; typelist[i] != NULL ; i++) {
- if (PyType_Ready(typelist[i]) < 0)
- return NULL;
- name = strchr(typelist[i]->tp_name, '.');
- assert (name != NULL);
- Py_INCREF(typelist[i]);
- PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
- }
-
- if (PyType_Ready(&teedataobject_type) < 0)
- return NULL;
- if (PyType_Ready(&tee_type) < 0)
- return NULL;
- if (PyType_Ready(&_grouper_type) < 0)
- return NULL;
- return m;
+ int i;
+ PyObject *m;
+ char *name;
+ PyTypeObject *typelist[] = {
+ &combinations_type,
+ &cwr_type,
+ &cycle_type,
+ &dropwhile_type,
+ &takewhile_type,
+ &islice_type,
+ &starmap_type,
+ &chain_type,
+ &compress_type,
+ &filterfalse_type,
+ &count_type,
+ &ziplongest_type,
+ &permutations_type,
+ &product_type,
+ &repeat_type,
+ &groupby_type,
+ NULL
+ };
+
+ Py_TYPE(&teedataobject_type) = &PyType_Type;
+ m = PyModule_Create(&itertoolsmodule);
+ if (m == NULL)
+ return NULL;
+
+ for (i=0 ; typelist[i] != NULL ; i++) {
+ if (PyType_Ready(typelist[i]) < 0)
+ return NULL;
+ name = strchr(typelist[i]->tp_name, '.');
+ assert (name != NULL);
+ Py_INCREF(typelist[i]);
+ PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
+ }
+
+ if (PyType_Ready(&teedataobject_type) < 0)
+ return NULL;
+ if (PyType_Ready(&tee_type) < 0)
+ return NULL;
+ if (PyType_Ready(&_grouper_type) < 0)
+ return NULL;
+ return m;
}
diff --git a/Modules/main.c b/Modules/main.c
index 7736d99..eb44aa9 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -104,20 +104,20 @@ PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
static FILE*
_wfopen(const wchar_t *path, const wchar_t *mode)
{
- char cpath[PATH_MAX];
- char cmode[10];
- size_t r;
- r = wcstombs(cpath, path, PATH_MAX);
- if (r == (size_t)-1 || r >= PATH_MAX) {
- errno = EINVAL;
- return NULL;
- }
- r = wcstombs(cmode, mode, 10);
- if (r == (size_t)-1 || r >= 10) {
- errno = EINVAL;
- return NULL;
- }
- return fopen(cpath, cmode);
+ char cpath[PATH_MAX];
+ char cmode[10];
+ size_t r;
+ r = wcstombs(cpath, path, PATH_MAX);
+ if (r == (size_t)-1 || r >= PATH_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ r = wcstombs(cmode, mode, 10);
+ if (r == (size_t)-1 || r >= 10) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return fopen(cpath, cmode);
}
#endif
@@ -125,131 +125,131 @@ _wfopen(const wchar_t *path, const wchar_t *mode)
static int
usage(int exitcode, wchar_t* program)
{
- FILE *f = exitcode ? stderr : stdout;
-
- fprintf(f, usage_line, program);
- if (exitcode)
- fprintf(f, "Try `python -h' for more information.\n");
- else {
- fputs(usage_1, f);
- fputs(usage_2, f);
- fputs(usage_3, f);
- fprintf(f, usage_4, DELIM);
- fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
- }
+ FILE *f = exitcode ? stderr : stdout;
+
+ fprintf(f, usage_line, program);
+ if (exitcode)
+ fprintf(f, "Try `python -h' for more information.\n");
+ else {
+ fputs(usage_1, f);
+ fputs(usage_2, f);
+ fputs(usage_3, f);
+ fprintf(f, usage_4, DELIM);
+ fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
+ }
#if defined(__VMS)
- if (exitcode == 0) {
- /* suppress 'error' message */
- return 1;
- }
- else {
- /* STS$M_INHIB_MSG + SS$_ABORT */
- return 0x1000002c;
- }
+ if (exitcode == 0) {
+ /* suppress 'error' message */
+ return 1;
+ }
+ else {
+ /* STS$M_INHIB_MSG + SS$_ABORT */
+ return 0x1000002c;
+ }
#else
- return exitcode;
+ return exitcode;
#endif
- /*NOTREACHED*/
+ /*NOTREACHED*/
}
static void RunStartupFile(PyCompilerFlags *cf)
{
- char *startup = Py_GETENV("PYTHONSTARTUP");
- if (startup != NULL && startup[0] != '\0') {
- FILE *fp = fopen(startup, "r");
- if (fp != NULL) {
- (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
- PyErr_Clear();
- fclose(fp);
- } else {
- int save_errno;
-
- save_errno = errno;
- PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
- errno = save_errno;
- PyErr_SetFromErrnoWithFilename(PyExc_IOError,
- startup);
- PyErr_Print();
- PyErr_Clear();
- }
- }
+ char *startup = Py_GETENV("PYTHONSTARTUP");
+ if (startup != NULL && startup[0] != '\0') {
+ FILE *fp = fopen(startup, "r");
+ if (fp != NULL) {
+ (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
+ PyErr_Clear();
+ fclose(fp);
+ } else {
+ int save_errno;
+
+ save_errno = errno;
+ PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
+ errno = save_errno;
+ PyErr_SetFromErrnoWithFilename(PyExc_IOError,
+ startup);
+ PyErr_Print();
+ PyErr_Clear();
+ }
+ }
}
static int RunModule(wchar_t *modname, int set_argv0)
{
- PyObject *module, *runpy, *runmodule, *runargs, *result;
- runpy = PyImport_ImportModule("runpy");
- if (runpy == NULL) {
- fprintf(stderr, "Could not import runpy module\n");
- return -1;
- }
- runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
- if (runmodule == NULL) {
- fprintf(stderr, "Could not access runpy._run_module_as_main\n");
- Py_DECREF(runpy);
- return -1;
- }
- module = PyUnicode_FromWideChar(modname, wcslen(modname));
- if (module == NULL) {
- fprintf(stderr, "Could not convert module name to unicode\n");
- Py_DECREF(runpy);
- Py_DECREF(runmodule);
- return -1;
- }
- runargs = Py_BuildValue("(Oi)", module, set_argv0);
- if (runargs == NULL) {
- fprintf(stderr,
- "Could not create arguments for runpy._run_module_as_main\n");
- Py_DECREF(runpy);
- Py_DECREF(runmodule);
- Py_DECREF(module);
- return -1;
- }
- result = PyObject_Call(runmodule, runargs, NULL);
- if (result == NULL) {
- PyErr_Print();
- }
- Py_DECREF(runpy);
- Py_DECREF(runmodule);
- Py_DECREF(module);
- Py_DECREF(runargs);
- if (result == NULL) {
- return -1;
- }
- Py_DECREF(result);
- return 0;
+ PyObject *module, *runpy, *runmodule, *runargs, *result;
+ runpy = PyImport_ImportModule("runpy");
+ if (runpy == NULL) {
+ fprintf(stderr, "Could not import runpy module\n");
+ return -1;
+ }
+ runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
+ if (runmodule == NULL) {
+ fprintf(stderr, "Could not access runpy._run_module_as_main\n");
+ Py_DECREF(runpy);
+ return -1;
+ }
+ module = PyUnicode_FromWideChar(modname, wcslen(modname));
+ if (module == NULL) {
+ fprintf(stderr, "Could not convert module name to unicode\n");
+ Py_DECREF(runpy);
+ Py_DECREF(runmodule);
+ return -1;
+ }
+ runargs = Py_BuildValue("(Oi)", module, set_argv0);
+ if (runargs == NULL) {
+ fprintf(stderr,
+ "Could not create arguments for runpy._run_module_as_main\n");
+ Py_DECREF(runpy);
+ Py_DECREF(runmodule);
+ Py_DECREF(module);
+ return -1;
+ }
+ result = PyObject_Call(runmodule, runargs, NULL);
+ if (result == NULL) {
+ PyErr_Print();
+ }
+ Py_DECREF(runpy);
+ Py_DECREF(runmodule);
+ Py_DECREF(module);
+ Py_DECREF(runargs);
+ if (result == NULL) {
+ return -1;
+ }
+ Py_DECREF(result);
+ return 0;
}
static int RunMainFromImporter(wchar_t *filename)
{
- PyObject *argv0 = NULL, *importer = NULL;
-
- if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
- (importer = PyImport_GetImporter(argv0)) &&
- (importer->ob_type != &PyNullImporter_Type))
- {
- /* argv0 is usable as an import source, so
- put it in sys.path[0] and import __main__ */
- PyObject *sys_path = NULL;
- if ((sys_path = PySys_GetObject("path")) &&
- !PyList_SetItem(sys_path, 0, argv0))
- {
- Py_INCREF(argv0);
- Py_DECREF(importer);
- sys_path = NULL;
- return RunModule(L"__main__", 0) != 0;
- }
- }
- Py_XDECREF(argv0);
- Py_XDECREF(importer);
- if (PyErr_Occurred()) {
- PyErr_Print();
- return 1;
+ PyObject *argv0 = NULL, *importer = NULL;
+
+ if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
+ (importer = PyImport_GetImporter(argv0)) &&
+ (importer->ob_type != &PyNullImporter_Type))
+ {
+ /* argv0 is usable as an import source, so
+ put it in sys.path[0] and import __main__ */
+ PyObject *sys_path = NULL;
+ if ((sys_path = PySys_GetObject("path")) &&
+ !PyList_SetItem(sys_path, 0, argv0))
+ {
+ Py_INCREF(argv0);
+ Py_DECREF(importer);
+ sys_path = NULL;
+ return RunModule(L"__main__", 0) != 0;
}
- else {
- return -1;
- }
+ }
+ Py_XDECREF(argv0);
+ Py_XDECREF(importer);
+ if (PyErr_Occurred()) {
+ PyErr_Print();
+ return 1;
+ }
+ else {
+ return -1;
+ }
}
@@ -258,391 +258,391 @@ static int RunMainFromImporter(wchar_t *filename)
int
Py_Main(int argc, wchar_t **argv)
{
- int c;
- int sts;
- wchar_t *command = NULL;
- wchar_t *filename = NULL;
- wchar_t *module = NULL;
- FILE *fp = stdin;
- char *p;
- int skipfirstline = 0;
- int stdin_is_interactive = 0;
- int help = 0;
- int version = 0;
- int saw_unbuffered_flag = 0;
- PyCompilerFlags cf;
-
- cf.cf_flags = 0;
-
- orig_argc = argc; /* For Py_GetArgcArgv() */
- orig_argv = argv;
-
- PySys_ResetWarnOptions();
-
- while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
- if (c == 'c') {
- size_t len;
- /* -c is the last option; following arguments
- that look like options are left for the
- command to interpret. */
-
- len = wcslen(_PyOS_optarg) + 1 + 1;
- command = (wchar_t *)malloc(sizeof(wchar_t) * len);
- if (command == NULL)
- Py_FatalError(
- "not enough memory to copy -c argument");
- wcscpy(command, _PyOS_optarg);
- command[len - 2] = '\n';
- command[len - 1] = 0;
- break;
- }
-
- if (c == 'm') {
- /* -m is the last option; following arguments
- that look like options are left for the
- module to interpret. */
- module = _PyOS_optarg;
- break;
- }
-
- switch (c) {
- case 'b':
- Py_BytesWarningFlag++;
- break;
-
- case 'd':
- Py_DebugFlag++;
- break;
-
- case 'i':
- Py_InspectFlag++;
- Py_InteractiveFlag++;
- break;
-
- /* case 'J': reserved for Jython */
-
- case 'O':
- Py_OptimizeFlag++;
- break;
-
- case 'B':
- Py_DontWriteBytecodeFlag++;
- break;
-
- case 's':
- Py_NoUserSiteDirectory++;
- break;
-
- case 'S':
- Py_NoSiteFlag++;
- break;
-
- case 'E':
- Py_IgnoreEnvironmentFlag++;
- break;
-
- case 't':
- /* ignored for backwards compatibility */
- break;
-
- case 'u':
- Py_UnbufferedStdioFlag = 1;
- saw_unbuffered_flag = 1;
- break;
-
- case 'v':
- Py_VerboseFlag++;
- break;
-
- case 'x':
- skipfirstline = 1;
- break;
-
- /* case 'X': reserved for implementation-specific arguments */
-
- case 'h':
- case '?':
- help++;
- break;
-
- case 'V':
- version++;
- break;
-
- case 'W':
- PySys_AddWarnOption(_PyOS_optarg);
- break;
-
- /* This space reserved for other options */
-
- default:
- return usage(2, argv[0]);
- /*NOTREACHED*/
-
- }
- }
-
- if (help)
- return usage(0, argv[0]);
-
- if (version) {
- fprintf(stderr, "Python %s\n", PY_VERSION);
- return 0;
- }
-
- if (!Py_InspectFlag &&
- (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
- Py_InspectFlag = 1;
- if (!saw_unbuffered_flag &&
- (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
- Py_UnbufferedStdioFlag = 1;
-
- if (!Py_NoUserSiteDirectory &&
- (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
- Py_NoUserSiteDirectory = 1;
-
- if (command == NULL && module == NULL && _PyOS_optind < argc &&
- wcscmp(argv[_PyOS_optind], L"-") != 0)
- {
+ int c;
+ int sts;
+ wchar_t *command = NULL;
+ wchar_t *filename = NULL;
+ wchar_t *module = NULL;
+ FILE *fp = stdin;
+ char *p;
+ int skipfirstline = 0;
+ int stdin_is_interactive = 0;
+ int help = 0;
+ int version = 0;
+ int saw_unbuffered_flag = 0;
+ PyCompilerFlags cf;
+
+ cf.cf_flags = 0;
+
+ orig_argc = argc; /* For Py_GetArgcArgv() */
+ orig_argv = argv;
+
+ PySys_ResetWarnOptions();
+
+ while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
+ if (c == 'c') {
+ size_t len;
+ /* -c is the last option; following arguments
+ that look like options are left for the
+ command to interpret. */
+
+ len = wcslen(_PyOS_optarg) + 1 + 1;
+ command = (wchar_t *)malloc(sizeof(wchar_t) * len);
+ if (command == NULL)
+ Py_FatalError(
+ "not enough memory to copy -c argument");
+ wcscpy(command, _PyOS_optarg);
+ command[len - 2] = '\n';
+ command[len - 1] = 0;
+ break;
+ }
+
+ if (c == 'm') {
+ /* -m is the last option; following arguments
+ that look like options are left for the
+ module to interpret. */
+ module = _PyOS_optarg;
+ break;
+ }
+
+ switch (c) {
+ case 'b':
+ Py_BytesWarningFlag++;
+ break;
+
+ case 'd':
+ Py_DebugFlag++;
+ break;
+
+ case 'i':
+ Py_InspectFlag++;
+ Py_InteractiveFlag++;
+ break;
+
+ /* case 'J': reserved for Jython */
+
+ case 'O':
+ Py_OptimizeFlag++;
+ break;
+
+ case 'B':
+ Py_DontWriteBytecodeFlag++;
+ break;
+
+ case 's':
+ Py_NoUserSiteDirectory++;
+ break;
+
+ case 'S':
+ Py_NoSiteFlag++;
+ break;
+
+ case 'E':
+ Py_IgnoreEnvironmentFlag++;
+ break;
+
+ case 't':
+ /* ignored for backwards compatibility */
+ break;
+
+ case 'u':
+ Py_UnbufferedStdioFlag = 1;
+ saw_unbuffered_flag = 1;
+ break;
+
+ case 'v':
+ Py_VerboseFlag++;
+ break;
+
+ case 'x':
+ skipfirstline = 1;
+ break;
+
+ /* case 'X': reserved for implementation-specific arguments */
+
+ case 'h':
+ case '?':
+ help++;
+ break;
+
+ case 'V':
+ version++;
+ break;
+
+ case 'W':
+ PySys_AddWarnOption(_PyOS_optarg);
+ break;
+
+ /* This space reserved for other options */
+
+ default:
+ return usage(2, argv[0]);
+ /*NOTREACHED*/
+
+ }
+ }
+
+ if (help)
+ return usage(0, argv[0]);
+
+ if (version) {
+ fprintf(stderr, "Python %s\n", PY_VERSION);
+ return 0;
+ }
+
+ if (!Py_InspectFlag &&
+ (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
+ Py_InspectFlag = 1;
+ if (!saw_unbuffered_flag &&
+ (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
+ Py_UnbufferedStdioFlag = 1;
+
+ if (!Py_NoUserSiteDirectory &&
+ (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
+ Py_NoUserSiteDirectory = 1;
+
+ if (command == NULL && module == NULL && _PyOS_optind < argc &&
+ wcscmp(argv[_PyOS_optind], L"-") != 0)
+ {
#ifdef __VMS
- filename = decc$translate_vms(argv[_PyOS_optind]);
- if (filename == (char *)0 || filename == (char *)-1)
- filename = argv[_PyOS_optind];
+ filename = decc$translate_vms(argv[_PyOS_optind]);
+ if (filename == (char *)0 || filename == (char *)-1)
+ filename = argv[_PyOS_optind];
#else
- filename = argv[_PyOS_optind];
+ filename = argv[_PyOS_optind];
#endif
- }
+ }
- stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
+ stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
- if (Py_UnbufferedStdioFlag) {
+ if (Py_UnbufferedStdioFlag) {
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
- _setmode(fileno(stdin), O_BINARY);
- _setmode(fileno(stdout), O_BINARY);
+ _setmode(fileno(stdin), O_BINARY);
+ _setmode(fileno(stdout), O_BINARY);
#endif
#ifdef HAVE_SETVBUF
- setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
- setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
- setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
+ setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
+ setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
+ setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
#else /* !HAVE_SETVBUF */
- setbuf(stdin, (char *)NULL);
- setbuf(stdout, (char *)NULL);
- setbuf(stderr, (char *)NULL);
+ setbuf(stdin, (char *)NULL);
+ setbuf(stdout, (char *)NULL);
+ setbuf(stderr, (char *)NULL);
#endif /* !HAVE_SETVBUF */
- }
- else if (Py_InteractiveFlag) {
+ }
+ else if (Py_InteractiveFlag) {
#ifdef MS_WINDOWS
- /* Doesn't have to have line-buffered -- use unbuffered */
- /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
- setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
+ /* Doesn't have to have line-buffered -- use unbuffered */
+ /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
+ setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
#else /* !MS_WINDOWS */
#ifdef HAVE_SETVBUF
- setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
- setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
+ setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
+ setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
#endif /* HAVE_SETVBUF */
#endif /* !MS_WINDOWS */
- /* Leave stderr alone - it should be unbuffered anyway. */
- }
+ /* Leave stderr alone - it should be unbuffered anyway. */
+ }
#ifdef __VMS
- else {
- setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
- }
+ else {
+ setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
+ }
#endif /* __VMS */
#ifdef __APPLE__
- /* On MacOS X, when the Python interpreter is embedded in an
- application bundle, it gets executed by a bootstrapping script
- that does os.execve() with an argv[0] that's different from the
- actual Python executable. This is needed to keep the Finder happy,
- or rather, to work around Apple's overly strict requirements of
- the process name. However, we still need a usable sys.executable,
- so the actual executable path is passed in an environment variable.
- See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
- script. */
- if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') {
- wchar_t* buffer;
- size_t len = strlen(p);
- size_t r;
-
- buffer = malloc(len * sizeof(wchar_t));
- if (buffer == NULL) {
- Py_FatalError(
- "not enough memory to copy PYTHONEXECUTABLE");
- }
-
- r = mbstowcs(buffer, p, len);
- Py_SetProgramName(buffer);
- /* buffer is now handed off - do not free */
- } else {
- Py_SetProgramName(argv[0]);
- }
+ /* On MacOS X, when the Python interpreter is embedded in an
+ application bundle, it gets executed by a bootstrapping script
+ that does os.execve() with an argv[0] that's different from the
+ actual Python executable. This is needed to keep the Finder happy,
+ or rather, to work around Apple's overly strict requirements of
+ the process name. However, we still need a usable sys.executable,
+ so the actual executable path is passed in an environment variable.
+ See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
+ script. */
+ if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') {
+ wchar_t* buffer;
+ size_t len = strlen(p);
+ size_t r;
+
+ buffer = malloc(len * sizeof(wchar_t));
+ if (buffer == NULL) {
+ Py_FatalError(
+ "not enough memory to copy PYTHONEXECUTABLE");
+ }
+
+ r = mbstowcs(buffer, p, len);
+ Py_SetProgramName(buffer);
+ /* buffer is now handed off - do not free */
+ } else {
+ Py_SetProgramName(argv[0]);
+ }
#else
- Py_SetProgramName(argv[0]);
+ Py_SetProgramName(argv[0]);
#endif
- Py_Initialize();
-
- if (Py_VerboseFlag ||
- (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
- fprintf(stderr, "Python %s on %s\n",
- Py_GetVersion(), Py_GetPlatform());
- if (!Py_NoSiteFlag)
- fprintf(stderr, "%s\n", COPYRIGHT);
- }
-
- if (command != NULL) {
- /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
- _PyOS_optind--;
- argv[_PyOS_optind] = L"-c";
- }
-
- if (module != NULL) {
- /* Backup _PyOS_optind and force sys.argv[0] = '-c'
- so that PySys_SetArgv correctly sets sys.path[0] to ''*/
- _PyOS_optind--;
- argv[_PyOS_optind] = L"-c";
- }
-
- PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
-
- if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
- isatty(fileno(stdin))) {
- PyObject *v;
- v = PyImport_ImportModule("readline");
- if (v == NULL)
- PyErr_Clear();
- else
- Py_DECREF(v);
- }
-
- if (command) {
- PyObject *commandObj = PyUnicode_FromWideChar(
- command, wcslen(command));
- free(command);
- if (commandObj != NULL) {
- sts = PyRun_SimpleStringFlags(
- _PyUnicode_AsString(commandObj), &cf) != 0;
- }
- else {
- PyErr_Print();
- sts = 1;
- }
- Py_DECREF(commandObj);
- } else if (module) {
- sts = RunModule(module, 1);
- }
- else {
-
- if (filename == NULL && stdin_is_interactive) {
- Py_InspectFlag = 0; /* do exit on SystemExit */
- RunStartupFile(&cf);
- }
- /* XXX */
-
- sts = -1; /* keep track of whether we've already run __main__ */
-
- if (filename != NULL) {
- sts = RunMainFromImporter(filename);
- }
-
- if (sts==-1 && filename!=NULL) {
- if ((fp = _wfopen(filename, L"r")) == NULL) {
- char cfilename[PATH_MAX];
- size_t r = wcstombs(cfilename, filename, PATH_MAX);
- if (r == PATH_MAX)
- /* cfilename is not null-terminated;
- * forcefully null-terminating it
- * might break the shift state */
- strcpy(cfilename, "<file name too long>");
- if (r == ((size_t)-1))
- strcpy(cfilename, "<unprintable file name>");
- fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
- argv[0], cfilename, errno, strerror(errno));
-
- return 2;
- }
- else if (skipfirstline) {
- int ch;
- /* Push back first newline so line numbers
- remain the same */
- while ((ch = getc(fp)) != EOF) {
- if (ch == '\n') {
- (void)ungetc(ch, fp);
- break;
- }
- }
- }
- {
- /* XXX: does this work on Win/Win64? (see posix_fstat) */
- struct stat sb;
- if (fstat(fileno(fp), &sb) == 0 &&
- S_ISDIR(sb.st_mode)) {
- fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
- fclose(fp);
- return 1;
- }
- }
- }
-
- if (sts==-1) {
- PyObject *filenameObj = NULL;
- char *p_cfilename = "<stdin>";
- if (filename) {
- filenameObj = PyUnicode_FromWideChar(
- filename, wcslen(filename));
- if (filenameObj != NULL)
- p_cfilename = _PyUnicode_AsString(filenameObj);
- else
- p_cfilename = "<decoding error>";
- }
- /* call pending calls like signal handlers (SIGINT) */
- if (Py_MakePendingCalls() == -1) {
- PyErr_Print();
- sts = 1;
- } else {
- sts = PyRun_AnyFileExFlags(
- fp,
- p_cfilename,
- filename != NULL, &cf) != 0;
- }
- Py_XDECREF(filenameObj);
- }
-
- }
-
- /* Check this environment variable at the end, to give programs the
- * opportunity to set it from Python.
- */
- if (!Py_InspectFlag &&
- (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
- {
- Py_InspectFlag = 1;
- }
-
- if (Py_InspectFlag && stdin_is_interactive &&
- (filename != NULL || command != NULL || module != NULL)) {
- Py_InspectFlag = 0;
- /* XXX */
- sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
- }
-
- Py_Finalize();
+ Py_Initialize();
+
+ if (Py_VerboseFlag ||
+ (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
+ fprintf(stderr, "Python %s on %s\n",
+ Py_GetVersion(), Py_GetPlatform());
+ if (!Py_NoSiteFlag)
+ fprintf(stderr, "%s\n", COPYRIGHT);
+ }
+
+ if (command != NULL) {
+ /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
+ _PyOS_optind--;
+ argv[_PyOS_optind] = L"-c";
+ }
+
+ if (module != NULL) {
+ /* Backup _PyOS_optind and force sys.argv[0] = '-c'
+ so that PySys_SetArgv correctly sets sys.path[0] to ''*/
+ _PyOS_optind--;
+ argv[_PyOS_optind] = L"-c";
+ }
+
+ PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
+
+ if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
+ isatty(fileno(stdin))) {
+ PyObject *v;
+ v = PyImport_ImportModule("readline");
+ if (v == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(v);
+ }
+
+ if (command) {
+ PyObject *commandObj = PyUnicode_FromWideChar(
+ command, wcslen(command));
+ free(command);
+ if (commandObj != NULL) {
+ sts = PyRun_SimpleStringFlags(
+ _PyUnicode_AsString(commandObj), &cf) != 0;
+ }
+ else {
+ PyErr_Print();
+ sts = 1;
+ }
+ Py_DECREF(commandObj);
+ } else if (module) {
+ sts = RunModule(module, 1);
+ }
+ else {
+
+ if (filename == NULL && stdin_is_interactive) {
+ Py_InspectFlag = 0; /* do exit on SystemExit */
+ RunStartupFile(&cf);
+ }
+ /* XXX */
+
+ sts = -1; /* keep track of whether we've already run __main__ */
+
+ if (filename != NULL) {
+ sts = RunMainFromImporter(filename);
+ }
+
+ if (sts==-1 && filename!=NULL) {
+ if ((fp = _wfopen(filename, L"r")) == NULL) {
+ char cfilename[PATH_MAX];
+ size_t r = wcstombs(cfilename, filename, PATH_MAX);
+ if (r == PATH_MAX)
+ /* cfilename is not null-terminated;
+ * forcefully null-terminating it
+ * might break the shift state */
+ strcpy(cfilename, "<file name too long>");
+ if (r == ((size_t)-1))
+ strcpy(cfilename, "<unprintable file name>");
+ fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
+ argv[0], cfilename, errno, strerror(errno));
+
+ return 2;
+ }
+ else if (skipfirstline) {
+ int ch;
+ /* Push back first newline so line numbers
+ remain the same */
+ while ((ch = getc(fp)) != EOF) {
+ if (ch == '\n') {
+ (void)ungetc(ch, fp);
+ break;
+ }
+ }
+ }
+ {
+ /* XXX: does this work on Win/Win64? (see posix_fstat) */
+ struct stat sb;
+ if (fstat(fileno(fp), &sb) == 0 &&
+ S_ISDIR(sb.st_mode)) {
+ fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
+ fclose(fp);
+ return 1;
+ }
+ }
+ }
+
+ if (sts==-1) {
+ PyObject *filenameObj = NULL;
+ char *p_cfilename = "<stdin>";
+ if (filename) {
+ filenameObj = PyUnicode_FromWideChar(
+ filename, wcslen(filename));
+ if (filenameObj != NULL)
+ p_cfilename = _PyUnicode_AsString(filenameObj);
+ else
+ p_cfilename = "<decoding error>";
+ }
+ /* call pending calls like signal handlers (SIGINT) */
+ if (Py_MakePendingCalls() == -1) {
+ PyErr_Print();
+ sts = 1;
+ } else {
+ sts = PyRun_AnyFileExFlags(
+ fp,
+ p_cfilename,
+ filename != NULL, &cf) != 0;
+ }
+ Py_XDECREF(filenameObj);
+ }
+
+ }
+
+ /* Check this environment variable at the end, to give programs the
+ * opportunity to set it from Python.
+ */
+ if (!Py_InspectFlag &&
+ (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
+ {
+ Py_InspectFlag = 1;
+ }
+
+ if (Py_InspectFlag && stdin_is_interactive &&
+ (filename != NULL || command != NULL || module != NULL)) {
+ Py_InspectFlag = 0;
+ /* XXX */
+ sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
+ }
+
+ Py_Finalize();
#ifdef __INSURE__
- /* Insure++ is a memory analysis tool that aids in discovering
- * memory leaks and other memory problems. On Python exit, the
- * interned string dictionaries are flagged as being in use at exit
- * (which it is). Under normal circumstances, this is fine because
- * the memory will be automatically reclaimed by the system. Under
- * memory debugging, it's a huge source of useless noise, so we
- * trade off slower shutdown for less distraction in the memory
- * reports. -baw
- */
- _Py_ReleaseInternedStrings();
- _Py_ReleaseInternedUnicodeStrings();
+ /* Insure++ is a memory analysis tool that aids in discovering
+ * memory leaks and other memory problems. On Python exit, the
+ * interned string dictionaries are flagged as being in use at exit
+ * (which it is). Under normal circumstances, this is fine because
+ * the memory will be automatically reclaimed by the system. Under
+ * memory debugging, it's a huge source of useless noise, so we
+ * trade off slower shutdown for less distraction in the memory
+ * reports. -baw
+ */
+ _Py_ReleaseInternedStrings();
+ _Py_ReleaseInternedUnicodeStrings();
#endif /* __INSURE__ */
- return sts;
+ return sts;
}
/* this is gonna seem *real weird*, but if you put some other code between
@@ -655,8 +655,8 @@ Py_Main(int argc, wchar_t **argv)
void
Py_GetArgcArgv(int *argc, wchar_t ***argv)
{
- *argc = orig_argc;
- *argv = orig_argv;
+ *argc = orig_argc;
+ *argv = orig_argv;
}
#ifdef __cplusplus
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 27ffcfc..50dd43e 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -67,37 +67,37 @@ extern double copysign(double, double);
static int
is_error(double x)
{
- int result = 1; /* presumption of guilt */
- assert(errno); /* non-zero errno is a precondition for calling */
- if (errno == EDOM)
- PyErr_SetString(PyExc_ValueError, "math domain error");
-
- else if (errno == ERANGE) {
- /* ANSI C generally requires libm functions to set ERANGE
- * on overflow, but also generally *allows* them to set
- * ERANGE on underflow too. There's no consistency about
- * the latter across platforms.
- * Alas, C99 never requires that errno be set.
- * Here we suppress the underflow errors (libm functions
- * should return a zero on underflow, and +- HUGE_VAL on
- * overflow, so testing the result for zero suffices to
- * distinguish the cases).
- *
- * On some platforms (Ubuntu/ia64) it seems that errno can be
- * set to ERANGE for subnormal results that do *not* underflow
- * to zero. So to be safe, we'll ignore ERANGE whenever the
- * function result is less than one in absolute value.
- */
- if (fabs(x) < 1.0)
- result = 0;
- else
- PyErr_SetString(PyExc_OverflowError,
- "math range error");
- }
- else
- /* Unexpected math error */
- PyErr_SetFromErrno(PyExc_ValueError);
- return result;
+ int result = 1; /* presumption of guilt */
+ assert(errno); /* non-zero errno is a precondition for calling */
+ if (errno == EDOM)
+ PyErr_SetString(PyExc_ValueError, "math domain error");
+
+ else if (errno == ERANGE) {
+ /* ANSI C generally requires libm functions to set ERANGE
+ * on overflow, but also generally *allows* them to set
+ * ERANGE on underflow too. There's no consistency about
+ * the latter across platforms.
+ * Alas, C99 never requires that errno be set.
+ * Here we suppress the underflow errors (libm functions
+ * should return a zero on underflow, and +- HUGE_VAL on
+ * overflow, so testing the result for zero suffices to
+ * distinguish the cases).
+ *
+ * On some platforms (Ubuntu/ia64) it seems that errno can be
+ * set to ERANGE for subnormal results that do *not* underflow
+ * to zero. So to be safe, we'll ignore ERANGE whenever the
+ * function result is less than one in absolute value.
+ */
+ if (fabs(x) < 1.0)
+ result = 0;
+ else
+ PyErr_SetString(PyExc_OverflowError,
+ "math range error");
+ }
+ else
+ /* Unexpected math error */
+ PyErr_SetFromErrno(PyExc_ValueError);
+ return result;
}
/*
@@ -111,29 +111,29 @@ is_error(double x)
static double
m_atan2(double y, double x)
{
- if (Py_IS_NAN(x) || Py_IS_NAN(y))
- return Py_NAN;
- if (Py_IS_INFINITY(y)) {
- if (Py_IS_INFINITY(x)) {
- if (copysign(1., x) == 1.)
- /* atan2(+-inf, +inf) == +-pi/4 */
- return copysign(0.25*Py_MATH_PI, y);
- else
- /* atan2(+-inf, -inf) == +-pi*3/4 */
- return copysign(0.75*Py_MATH_PI, y);
- }
- /* atan2(+-inf, x) == +-pi/2 for finite x */
- return copysign(0.5*Py_MATH_PI, y);
- }
- if (Py_IS_INFINITY(x) || y == 0.) {
- if (copysign(1., x) == 1.)
- /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
- return copysign(0., y);
- else
- /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
- return copysign(Py_MATH_PI, y);
- }
- return atan2(y, x);
+ if (Py_IS_NAN(x) || Py_IS_NAN(y))
+ return Py_NAN;
+ if (Py_IS_INFINITY(y)) {
+ if (Py_IS_INFINITY(x)) {
+ if (copysign(1., x) == 1.)
+ /* atan2(+-inf, +inf) == +-pi/4 */
+ return copysign(0.25*Py_MATH_PI, y);
+ else
+ /* atan2(+-inf, -inf) == +-pi*3/4 */
+ return copysign(0.75*Py_MATH_PI, y);
+ }
+ /* atan2(+-inf, x) == +-pi/2 for finite x */
+ return copysign(0.5*Py_MATH_PI, y);
+ }
+ if (Py_IS_INFINITY(x) || y == 0.) {
+ if (copysign(1., x) == 1.)
+ /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
+ return copysign(0., y);
+ else
+ /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
+ return copysign(Py_MATH_PI, y);
+ }
+ return atan2(y, x);
}
/*
@@ -146,45 +146,45 @@ m_atan2(double y, double x)
static double
m_log(double x)
{
- if (Py_IS_FINITE(x)) {
- if (x > 0.0)
- return log(x);
- errno = EDOM;
- if (x == 0.0)
- return -Py_HUGE_VAL; /* log(0) = -inf */
- else
- return Py_NAN; /* log(-ve) = nan */
- }
- else if (Py_IS_NAN(x))
- return x; /* log(nan) = nan */
- else if (x > 0.0)
- return x; /* log(inf) = inf */
- else {
- errno = EDOM;
- return Py_NAN; /* log(-inf) = nan */
- }
+ if (Py_IS_FINITE(x)) {
+ if (x > 0.0)
+ return log(x);
+ errno = EDOM;
+ if (x == 0.0)
+ return -Py_HUGE_VAL; /* log(0) = -inf */
+ else
+ return Py_NAN; /* log(-ve) = nan */
+ }
+ else if (Py_IS_NAN(x))
+ return x; /* log(nan) = nan */
+ else if (x > 0.0)
+ return x; /* log(inf) = inf */
+ else {
+ errno = EDOM;
+ return Py_NAN; /* log(-inf) = nan */
+ }
}
static double
m_log10(double x)
{
- if (Py_IS_FINITE(x)) {
- if (x > 0.0)
- return log10(x);
- errno = EDOM;
- if (x == 0.0)
- return -Py_HUGE_VAL; /* log10(0) = -inf */
- else
- return Py_NAN; /* log10(-ve) = nan */
- }
- else if (Py_IS_NAN(x))
- return x; /* log10(nan) = nan */
- else if (x > 0.0)
- return x; /* log10(inf) = inf */
- else {
- errno = EDOM;
- return Py_NAN; /* log10(-inf) = nan */
- }
+ if (Py_IS_FINITE(x)) {
+ if (x > 0.0)
+ return log10(x);
+ errno = EDOM;
+ if (x == 0.0)
+ return -Py_HUGE_VAL; /* log10(0) = -inf */
+ else
+ return Py_NAN; /* log10(-ve) = nan */
+ }
+ else if (Py_IS_NAN(x))
+ return x; /* log10(nan) = nan */
+ else if (x > 0.0)
+ return x; /* log10(inf) = inf */
+ else {
+ errno = EDOM;
+ return Py_NAN; /* log10(-inf) = nan */
+ }
}
@@ -223,33 +223,33 @@ math_1_to_whatever(PyObject *arg, double (*func) (double),
PyObject *(*from_double_func) (double),
int can_overflow)
{
- double x, r;
- x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- errno = 0;
- PyFPE_START_PROTECT("in math_1", return 0);
- r = (*func)(x);
- PyFPE_END_PROTECT(r);
- if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
- PyErr_SetString(PyExc_ValueError,
- "math domain error"); /* invalid arg */
- return NULL;
- }
- if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
- if (can_overflow)
- PyErr_SetString(PyExc_OverflowError,
- "math range error"); /* overflow */
- else
- PyErr_SetString(PyExc_ValueError,
- "math domain error"); /* singularity */
- return NULL;
- }
- if (Py_IS_FINITE(r) && errno && is_error(r))
- /* this branch unnecessary on most platforms */
- return NULL;
-
- return (*from_double_func)(r);
+ double x, r;
+ x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ errno = 0;
+ PyFPE_START_PROTECT("in math_1", return 0);
+ r = (*func)(x);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_NAN(r) && !Py_IS_NAN(x)) {
+ PyErr_SetString(PyExc_ValueError,
+ "math domain error"); /* invalid arg */
+ return NULL;
+ }
+ if (Py_IS_INFINITY(r) && Py_IS_FINITE(x)) {
+ if (can_overflow)
+ PyErr_SetString(PyExc_OverflowError,
+ "math range error"); /* overflow */
+ else
+ PyErr_SetString(PyExc_ValueError,
+ "math domain error"); /* singularity */
+ return NULL;
+ }
+ if (Py_IS_FINITE(r) && errno && is_error(r))
+ /* this branch unnecessary on most platforms */
+ return NULL;
+
+ return (*from_double_func)(r);
}
/*
@@ -282,59 +282,59 @@ math_1_to_whatever(PyObject *arg, double (*func) (double),
static PyObject *
math_1(PyObject *arg, double (*func) (double), int can_overflow)
{
- return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
+ return math_1_to_whatever(arg, func, PyFloat_FromDouble, can_overflow);
}
static PyObject *
math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow)
{
- return math_1_to_whatever(arg, func, PyLong_FromDouble, can_overflow);
+ return math_1_to_whatever(arg, func, PyLong_FromDouble, can_overflow);
}
static PyObject *
math_2(PyObject *args, double (*func) (double, double), char *funcname)
{
- PyObject *ox, *oy;
- double x, y, r;
- if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
- return NULL;
- x = PyFloat_AsDouble(ox);
- y = PyFloat_AsDouble(oy);
- if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
- return NULL;
- errno = 0;
- PyFPE_START_PROTECT("in math_2", return 0);
- r = (*func)(x, y);
- PyFPE_END_PROTECT(r);
- if (Py_IS_NAN(r)) {
- if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
- errno = EDOM;
- else
- errno = 0;
- }
- else if (Py_IS_INFINITY(r)) {
- if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
- errno = ERANGE;
- else
- errno = 0;
- }
- if (errno && is_error(r))
- return NULL;
- else
- return PyFloat_FromDouble(r);
+ PyObject *ox, *oy;
+ double x, y, r;
+ if (! PyArg_UnpackTuple(args, funcname, 2, 2, &ox, &oy))
+ return NULL;
+ x = PyFloat_AsDouble(ox);
+ y = PyFloat_AsDouble(oy);
+ if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
+ return NULL;
+ errno = 0;
+ PyFPE_START_PROTECT("in math_2", return 0);
+ r = (*func)(x, y);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_NAN(r)) {
+ if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
+ errno = EDOM;
+ else
+ errno = 0;
+ }
+ else if (Py_IS_INFINITY(r)) {
+ if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
+ errno = ERANGE;
+ else
+ errno = 0;
+ }
+ if (errno && is_error(r))
+ return NULL;
+ else
+ return PyFloat_FromDouble(r);
}
-#define FUNC1(funcname, func, can_overflow, docstring) \
- static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
- return math_1(args, func, can_overflow); \
- }\
- PyDoc_STRVAR(math_##funcname##_doc, docstring);
+#define FUNC1(funcname, func, can_overflow, docstring) \
+ static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
+ return math_1(args, func, can_overflow); \
+ }\
+ PyDoc_STRVAR(math_##funcname##_doc, docstring);
#define FUNC2(funcname, func, docstring) \
- static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
- return math_2(args, func, #funcname); \
- }\
- PyDoc_STRVAR(math_##funcname##_doc, docstring);
+ static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
+ return math_2(args, func, #funcname); \
+ }\
+ PyDoc_STRVAR(math_##funcname##_doc, docstring);
FUNC1(acos, acos, 0,
"acos(x)\n\nReturn the arc cosine (measured in radians) of x.")
@@ -353,25 +353,25 @@ FUNC1(atanh, atanh, 0,
"atanh(x)\n\nReturn the hyperbolic arc tangent (measured in radians) of x.")
static PyObject * math_ceil(PyObject *self, PyObject *number) {
- static PyObject *ceil_str = NULL;
- PyObject *method;
-
- if (ceil_str == NULL) {
- ceil_str = PyUnicode_InternFromString("__ceil__");
- if (ceil_str == NULL)
- return NULL;
- }
-
- method = _PyType_Lookup(Py_TYPE(number), ceil_str);
- if (method == NULL)
- return math_1_to_int(number, ceil, 0);
- else
- return PyObject_CallFunction(method, "O", number);
+ static PyObject *ceil_str = NULL;
+ PyObject *method;
+
+ if (ceil_str == NULL) {
+ ceil_str = PyUnicode_InternFromString("__ceil__");
+ if (ceil_str == NULL)
+ return NULL;
+ }
+
+ method = _PyType_Lookup(Py_TYPE(number), ceil_str);
+ if (method == NULL)
+ return math_1_to_int(number, ceil, 0);
+ else
+ return PyObject_CallFunction(method, "O", number);
}
PyDoc_STRVAR(math_ceil_doc,
- "ceil(x)\n\nReturn the ceiling of x as an int.\n"
- "This is the smallest integral value >= x.");
+ "ceil(x)\n\nReturn the ceiling of x as an int.\n"
+ "This is the smallest integral value >= x.");
FUNC2(copysign, copysign,
"copysign(x, y)\n\nReturn x with the sign of y.")
@@ -385,25 +385,25 @@ FUNC1(fabs, fabs, 0,
"fabs(x)\n\nReturn the absolute value of the float x.")
static PyObject * math_floor(PyObject *self, PyObject *number) {
- static PyObject *floor_str = NULL;
- PyObject *method;
-
- if (floor_str == NULL) {
- floor_str = PyUnicode_InternFromString("__floor__");
- if (floor_str == NULL)
- return NULL;
- }
-
- method = _PyType_Lookup(Py_TYPE(number), floor_str);
- if (method == NULL)
- return math_1_to_int(number, floor, 0);
- else
- return PyObject_CallFunction(method, "O", number);
+ static PyObject *floor_str = NULL;
+ PyObject *method;
+
+ if (floor_str == NULL) {
+ floor_str = PyUnicode_InternFromString("__floor__");
+ if (floor_str == NULL)
+ return NULL;
+ }
+
+ method = _PyType_Lookup(Py_TYPE(number), floor_str);
+ if (method == NULL)
+ return math_1_to_int(number, floor, 0);
+ else
+ return PyObject_CallFunction(method, "O", number);
}
PyDoc_STRVAR(math_floor_doc,
- "floor(x)\n\nReturn the floor of x as an int.\n"
- "This is the largest integral value <= x.");
+ "floor(x)\n\nReturn the floor of x as an int.\n"
+ "This is the largest integral value <= x.");
FUNC1(log1p, log1p, 1,
"log1p(x)\n\nReturn the natural logarithm of 1+x (base e).\n"
@@ -439,7 +439,7 @@ FUNC1(tanh, tanh, 0,
Also, the volatile declaration forces the values to be stored in memory as
regular doubles instead of extended long precision (80-bit) values. This
prevents double rounding because any addition or subtraction of two doubles
- can be resolved exactly into double-sized hi and lo values. As long as the
+ can be resolved exactly into double-sized hi and lo values. As long as the
hi value gets forced into a double before yr and lo are computed, the extra
bits in downstream extended precision operations (x87 for example) will be
exactly zero and therefore can be losslessly stored back into a double,
@@ -462,27 +462,27 @@ static int /* non-zero on error */
_fsum_realloc(double **p_ptr, Py_ssize_t n,
double *ps, Py_ssize_t *m_ptr)
{
- void *v = NULL;
- Py_ssize_t m = *m_ptr;
-
- m += m; /* double */
- if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
- double *p = *p_ptr;
- if (p == ps) {
- v = PyMem_Malloc(sizeof(double) * m);
- if (v != NULL)
- memcpy(v, ps, sizeof(double) * n);
- }
- else
- v = PyMem_Realloc(p, sizeof(double) * m);
- }
- if (v == NULL) { /* size overflow or no memory */
- PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
- return 1;
- }
- *p_ptr = (double*) v;
- *m_ptr = m;
- return 0;
+ void *v = NULL;
+ Py_ssize_t m = *m_ptr;
+
+ m += m; /* double */
+ if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
+ double *p = *p_ptr;
+ if (p == ps) {
+ v = PyMem_Malloc(sizeof(double) * m);
+ if (v != NULL)
+ memcpy(v, ps, sizeof(double) * n);
+ }
+ else
+ v = PyMem_Realloc(p, sizeof(double) * m);
+ }
+ if (v == NULL) { /* size overflow or no memory */
+ PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
+ return 1;
+ }
+ *p_ptr = (double*) v;
+ *m_ptr = m;
+ return 0;
}
/* Full precision summation of a sequence of floats.
@@ -490,17 +490,17 @@ _fsum_realloc(double **p_ptr, Py_ssize_t n,
def msum(iterable):
partials = [] # sorted, non-overlapping partial sums
for x in iterable:
- i = 0
- for y in partials:
- if abs(x) < abs(y):
- x, y = y, x
- hi = x + y
- lo = y - (hi - x)
- if lo:
- partials[i] = lo
- i += 1
- x = hi
- partials[i:] = [x]
+ i = 0
+ for y in partials:
+ if abs(x) < abs(y):
+ x, y = y, x
+ hi = x + y
+ lo = y - (hi - x)
+ if lo:
+ partials[i] = lo
+ i += 1
+ x = hi
+ partials[i:] = [x]
return sum_exact(partials)
Rounded x+y stored in hi with the roundoff stored in lo. Together hi+lo
@@ -518,119 +518,119 @@ _fsum_realloc(double **p_ptr, Py_ssize_t n,
static PyObject*
math_fsum(PyObject *self, PyObject *seq)
{
- PyObject *item, *iter, *sum = NULL;
- Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
- double x, y, t, ps[NUM_PARTIALS], *p = ps;
- double xsave, special_sum = 0.0, inf_sum = 0.0;
- volatile double hi, yr, lo;
-
- iter = PyObject_GetIter(seq);
- if (iter == NULL)
- return NULL;
-
- PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL)
-
- for(;;) { /* for x in iterable */
- assert(0 <= n && n <= m);
- assert((m == NUM_PARTIALS && p == ps) ||
- (m > NUM_PARTIALS && p != NULL));
-
- item = PyIter_Next(iter);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto _fsum_error;
- break;
- }
- x = PyFloat_AsDouble(item);
- Py_DECREF(item);
- if (PyErr_Occurred())
- goto _fsum_error;
-
- xsave = x;
- for (i = j = 0; j < n; j++) { /* for y in partials */
- y = p[j];
- if (fabs(x) < fabs(y)) {
- t = x; x = y; y = t;
- }
- hi = x + y;
- yr = hi - x;
- lo = y - yr;
- if (lo != 0.0)
- p[i++] = lo;
- x = hi;
- }
-
- n = i; /* ps[i:] = [x] */
- if (x != 0.0) {
- if (! Py_IS_FINITE(x)) {
- /* a nonfinite x could arise either as
- a result of intermediate overflow, or
- as a result of a nan or inf in the
- summands */
- if (Py_IS_FINITE(xsave)) {
- PyErr_SetString(PyExc_OverflowError,
- "intermediate overflow in fsum");
- goto _fsum_error;
- }
- if (Py_IS_INFINITY(xsave))
- inf_sum += xsave;
- special_sum += xsave;
- /* reset partials */
- n = 0;
- }
- else if (n >= m && _fsum_realloc(&p, n, ps, &m))
- goto _fsum_error;
- else
- p[n++] = x;
- }
- }
-
- if (special_sum != 0.0) {
- if (Py_IS_NAN(inf_sum))
- PyErr_SetString(PyExc_ValueError,
- "-inf + inf in fsum");
- else
- sum = PyFloat_FromDouble(special_sum);
- goto _fsum_error;
- }
-
- hi = 0.0;
- if (n > 0) {
- hi = p[--n];
- /* sum_exact(ps, hi) from the top, stop when the sum becomes
- inexact. */
- while (n > 0) {
- x = hi;
- y = p[--n];
- assert(fabs(y) < fabs(x));
- hi = x + y;
- yr = hi - x;
- lo = y - yr;
- if (lo != 0.0)
- break;
- }
- /* Make half-even rounding work across multiple partials.
- Needed so that sum([1e-16, 1, 1e16]) will round-up the last
- digit to two instead of down to zero (the 1e-16 makes the 1
- slightly closer to two). With a potential 1 ULP rounding
- error fixed-up, math.fsum() can guarantee commutativity. */
- if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
- (lo > 0.0 && p[n-1] > 0.0))) {
- y = lo * 2.0;
- x = hi + y;
- yr = x - hi;
- if (y == yr)
- hi = x;
- }
- }
- sum = PyFloat_FromDouble(hi);
+ PyObject *item, *iter, *sum = NULL;
+ Py_ssize_t i, j, n = 0, m = NUM_PARTIALS;
+ double x, y, t, ps[NUM_PARTIALS], *p = ps;
+ double xsave, special_sum = 0.0, inf_sum = 0.0;
+ volatile double hi, yr, lo;
+
+ iter = PyObject_GetIter(seq);
+ if (iter == NULL)
+ return NULL;
+
+ PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL)
+
+ for(;;) { /* for x in iterable */
+ assert(0 <= n && n <= m);
+ assert((m == NUM_PARTIALS && p == ps) ||
+ (m > NUM_PARTIALS && p != NULL));
+
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto _fsum_error;
+ break;
+ }
+ x = PyFloat_AsDouble(item);
+ Py_DECREF(item);
+ if (PyErr_Occurred())
+ goto _fsum_error;
+
+ xsave = x;
+ for (i = j = 0; j < n; j++) { /* for y in partials */
+ y = p[j];
+ if (fabs(x) < fabs(y)) {
+ t = x; x = y; y = t;
+ }
+ hi = x + y;
+ yr = hi - x;
+ lo = y - yr;
+ if (lo != 0.0)
+ p[i++] = lo;
+ x = hi;
+ }
+
+ n = i; /* ps[i:] = [x] */
+ if (x != 0.0) {
+ if (! Py_IS_FINITE(x)) {
+ /* a nonfinite x could arise either as
+ a result of intermediate overflow, or
+ as a result of a nan or inf in the
+ summands */
+ if (Py_IS_FINITE(xsave)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "intermediate overflow in fsum");
+ goto _fsum_error;
+ }
+ if (Py_IS_INFINITY(xsave))
+ inf_sum += xsave;
+ special_sum += xsave;
+ /* reset partials */
+ n = 0;
+ }
+ else if (n >= m && _fsum_realloc(&p, n, ps, &m))
+ goto _fsum_error;
+ else
+ p[n++] = x;
+ }
+ }
+
+ if (special_sum != 0.0) {
+ if (Py_IS_NAN(inf_sum))
+ PyErr_SetString(PyExc_ValueError,
+ "-inf + inf in fsum");
+ else
+ sum = PyFloat_FromDouble(special_sum);
+ goto _fsum_error;
+ }
+
+ hi = 0.0;
+ if (n > 0) {
+ hi = p[--n];
+ /* sum_exact(ps, hi) from the top, stop when the sum becomes
+ inexact. */
+ while (n > 0) {
+ x = hi;
+ y = p[--n];
+ assert(fabs(y) < fabs(x));
+ hi = x + y;
+ yr = hi - x;
+ lo = y - yr;
+ if (lo != 0.0)
+ break;
+ }
+ /* Make half-even rounding work across multiple partials.
+ Needed so that sum([1e-16, 1, 1e16]) will round-up the last
+ digit to two instead of down to zero (the 1e-16 makes the 1
+ slightly closer to two). With a potential 1 ULP rounding
+ error fixed-up, math.fsum() can guarantee commutativity. */
+ if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) ||
+ (lo > 0.0 && p[n-1] > 0.0))) {
+ y = lo * 2.0;
+ x = hi + y;
+ yr = x - hi;
+ if (y == yr)
+ hi = x;
+ }
+ }
+ sum = PyFloat_FromDouble(hi);
_fsum_error:
- PyFPE_END_PROTECT(hi)
- Py_DECREF(iter);
- if (p != ps)
- PyMem_Free(p);
- return sum;
+ PyFPE_END_PROTECT(hi)
+ Py_DECREF(iter);
+ if (p != ps)
+ PyMem_Free(p);
+ return sum;
}
#undef NUM_PARTIALS
@@ -643,46 +643,46 @@ Assumes IEEE-754 floating point arithmetic.");
static PyObject *
math_factorial(PyObject *self, PyObject *arg)
{
- long i, x;
- PyObject *result, *iobj, *newresult;
-
- if (PyFloat_Check(arg)) {
- double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
- if (dx != floor(dx)) {
- PyErr_SetString(PyExc_ValueError,
- "factorial() only accepts integral values");
- return NULL;
- }
- }
-
- x = PyLong_AsLong(arg);
- if (x == -1 && PyErr_Occurred())
- return NULL;
- if (x < 0) {
- PyErr_SetString(PyExc_ValueError,
- "factorial() not defined for negative values");
- return NULL;
- }
-
- result = (PyObject *)PyLong_FromLong(1);
- if (result == NULL)
- return NULL;
- for (i=1 ; i<=x ; i++) {
- iobj = (PyObject *)PyLong_FromLong(i);
- if (iobj == NULL)
- goto error;
- newresult = PyNumber_Multiply(result, iobj);
- Py_DECREF(iobj);
- if (newresult == NULL)
- goto error;
- Py_DECREF(result);
- result = newresult;
- }
- return result;
+ long i, x;
+ PyObject *result, *iobj, *newresult;
+
+ if (PyFloat_Check(arg)) {
+ double dx = PyFloat_AS_DOUBLE((PyFloatObject *)arg);
+ if (dx != floor(dx)) {
+ PyErr_SetString(PyExc_ValueError,
+ "factorial() only accepts integral values");
+ return NULL;
+ }
+ }
+
+ x = PyLong_AsLong(arg);
+ if (x == -1 && PyErr_Occurred())
+ return NULL;
+ if (x < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "factorial() not defined for negative values");
+ return NULL;
+ }
+
+ result = (PyObject *)PyLong_FromLong(1);
+ if (result == NULL)
+ return NULL;
+ for (i=1 ; i<=x ; i++) {
+ iobj = (PyObject *)PyLong_FromLong(i);
+ if (iobj == NULL)
+ goto error;
+ newresult = PyNumber_Multiply(result, iobj);
+ Py_DECREF(iobj);
+ if (newresult == NULL)
+ goto error;
+ Py_DECREF(result);
+ result = newresult;
+ }
+ return result;
error:
- Py_DECREF(result);
- return NULL;
+ Py_DECREF(result);
+ return NULL;
}
PyDoc_STRVAR(math_factorial_doc,
@@ -693,28 +693,28 @@ PyDoc_STRVAR(math_factorial_doc,
static PyObject *
math_trunc(PyObject *self, PyObject *number)
{
- static PyObject *trunc_str = NULL;
- PyObject *trunc;
-
- if (Py_TYPE(number)->tp_dict == NULL) {
- if (PyType_Ready(Py_TYPE(number)) < 0)
- return NULL;
- }
-
- if (trunc_str == NULL) {
- trunc_str = PyUnicode_InternFromString("__trunc__");
- if (trunc_str == NULL)
- return NULL;
- }
-
- trunc = _PyType_Lookup(Py_TYPE(number), trunc_str);
- if (trunc == NULL) {
- PyErr_Format(PyExc_TypeError,
- "type %.100s doesn't define __trunc__ method",
- Py_TYPE(number)->tp_name);
- return NULL;
- }
- return PyObject_CallFunctionObjArgs(trunc, number, NULL);
+ static PyObject *trunc_str = NULL;
+ PyObject *trunc;
+
+ if (Py_TYPE(number)->tp_dict == NULL) {
+ if (PyType_Ready(Py_TYPE(number)) < 0)
+ return NULL;
+ }
+
+ if (trunc_str == NULL) {
+ trunc_str = PyUnicode_InternFromString("__trunc__");
+ if (trunc_str == NULL)
+ return NULL;
+ }
+
+ trunc = _PyType_Lookup(Py_TYPE(number), trunc_str);
+ if (trunc == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "type %.100s doesn't define __trunc__ method",
+ Py_TYPE(number)->tp_name);
+ return NULL;
+ }
+ return PyObject_CallFunctionObjArgs(trunc, number, NULL);
}
PyDoc_STRVAR(math_trunc_doc,
@@ -725,21 +725,21 @@ PyDoc_STRVAR(math_trunc_doc,
static PyObject *
math_frexp(PyObject *self, PyObject *arg)
{
- int i;
- double x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- /* deal with special cases directly, to sidestep platform
- differences */
- if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
- i = 0;
- }
- else {
- PyFPE_START_PROTECT("in math_frexp", return 0);
- x = frexp(x, &i);
- PyFPE_END_PROTECT(x);
- }
- return Py_BuildValue("(di)", x, i);
+ int i;
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ /* deal with special cases directly, to sidestep platform
+ differences */
+ if (Py_IS_NAN(x) || Py_IS_INFINITY(x) || !x) {
+ i = 0;
+ }
+ else {
+ PyFPE_START_PROTECT("in math_frexp", return 0);
+ x = frexp(x, &i);
+ PyFPE_END_PROTECT(x);
+ }
+ return Py_BuildValue("(di)", x, i);
}
PyDoc_STRVAR(math_frexp_doc,
@@ -752,63 +752,63 @@ PyDoc_STRVAR(math_frexp_doc,
static PyObject *
math_ldexp(PyObject *self, PyObject *args)
{
- double x, r;
- PyObject *oexp;
- long exp;
- if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
- return NULL;
-
- if (PyLong_Check(oexp)) {
- /* on overflow, replace exponent with either LONG_MAX
- or LONG_MIN, depending on the sign. */
- exp = PyLong_AsLong(oexp);
- if (exp == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
- if (Py_SIZE(oexp) < 0) {
- exp = LONG_MIN;
- }
- else {
- exp = LONG_MAX;
- }
- PyErr_Clear();
- }
- else {
- /* propagate any unexpected exception */
- return NULL;
- }
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "Expected an int or long as second argument "
- "to ldexp.");
- return NULL;
- }
-
- if (x == 0. || !Py_IS_FINITE(x)) {
- /* NaNs, zeros and infinities are returned unchanged */
- r = x;
- errno = 0;
- } else if (exp > INT_MAX) {
- /* overflow */
- r = copysign(Py_HUGE_VAL, x);
- errno = ERANGE;
- } else if (exp < INT_MIN) {
- /* underflow to +-0 */
- r = copysign(0., x);
- errno = 0;
- } else {
- errno = 0;
- PyFPE_START_PROTECT("in math_ldexp", return 0);
- r = ldexp(x, (int)exp);
- PyFPE_END_PROTECT(r);
- if (Py_IS_INFINITY(r))
- errno = ERANGE;
- }
-
- if (errno && is_error(r))
- return NULL;
- return PyFloat_FromDouble(r);
+ double x, r;
+ PyObject *oexp;
+ long exp;
+ if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
+ return NULL;
+
+ if (PyLong_Check(oexp)) {
+ /* on overflow, replace exponent with either LONG_MAX
+ or LONG_MIN, depending on the sign. */
+ exp = PyLong_AsLong(oexp);
+ if (exp == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
+ if (Py_SIZE(oexp) < 0) {
+ exp = LONG_MIN;
+ }
+ else {
+ exp = LONG_MAX;
+ }
+ PyErr_Clear();
+ }
+ else {
+ /* propagate any unexpected exception */
+ return NULL;
+ }
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "Expected an int or long as second argument "
+ "to ldexp.");
+ return NULL;
+ }
+
+ if (x == 0. || !Py_IS_FINITE(x)) {
+ /* NaNs, zeros and infinities are returned unchanged */
+ r = x;
+ errno = 0;
+ } else if (exp > INT_MAX) {
+ /* overflow */
+ r = copysign(Py_HUGE_VAL, x);
+ errno = ERANGE;
+ } else if (exp < INT_MIN) {
+ /* underflow to +-0 */
+ r = copysign(0., x);
+ errno = 0;
+ } else {
+ errno = 0;
+ PyFPE_START_PROTECT("in math_ldexp", return 0);
+ r = ldexp(x, (int)exp);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_INFINITY(r))
+ errno = ERANGE;
+ }
+
+ if (errno && is_error(r))
+ return NULL;
+ return PyFloat_FromDouble(r);
}
PyDoc_STRVAR(math_ldexp_doc,
@@ -818,23 +818,23 @@ Return x * (2**i).");
static PyObject *
math_modf(PyObject *self, PyObject *arg)
{
- double y, x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- /* some platforms don't do the right thing for NaNs and
- infinities, so we take care of special cases directly. */
- if (!Py_IS_FINITE(x)) {
- if (Py_IS_INFINITY(x))
- return Py_BuildValue("(dd)", copysign(0., x), x);
- else if (Py_IS_NAN(x))
- return Py_BuildValue("(dd)", x, x);
- }
-
- errno = 0;
- PyFPE_START_PROTECT("in math_modf", return 0);
- x = modf(x, &y);
- PyFPE_END_PROTECT(x);
- return Py_BuildValue("(dd)", x, y);
+ double y, x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ /* some platforms don't do the right thing for NaNs and
+ infinities, so we take care of special cases directly. */
+ if (!Py_IS_FINITE(x)) {
+ if (Py_IS_INFINITY(x))
+ return Py_BuildValue("(dd)", copysign(0., x), x);
+ else if (Py_IS_NAN(x))
+ return Py_BuildValue("(dd)", x, x);
+ }
+
+ errno = 0;
+ PyFPE_START_PROTECT("in math_modf", return 0);
+ x = modf(x, &y);
+ PyFPE_END_PROTECT(x);
+ return Py_BuildValue("(dd)", x, y);
}
PyDoc_STRVAR(math_modf_doc,
@@ -854,53 +854,53 @@ PyDoc_STRVAR(math_modf_doc,
static PyObject*
loghelper(PyObject* arg, double (*func)(double), char *funcname)
{
- /* If it is long, do it ourselves. */
- if (PyLong_Check(arg)) {
- double x;
- int e;
- x = _PyLong_AsScaledDouble(arg, &e);
- if (x <= 0.0) {
- PyErr_SetString(PyExc_ValueError,
- "math domain error");
- return NULL;
- }
- /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
- log(x) + log(2) * e * PyLong_SHIFT.
- CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
- so force use of double. */
- x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
- return PyFloat_FromDouble(x);
- }
-
- /* Else let libm handle it by itself. */
- return math_1(arg, func, 0);
+ /* If it is long, do it ourselves. */
+ if (PyLong_Check(arg)) {
+ double x;
+ int e;
+ x = _PyLong_AsScaledDouble(arg, &e);
+ if (x <= 0.0) {
+ PyErr_SetString(PyExc_ValueError,
+ "math domain error");
+ return NULL;
+ }
+ /* Value is ~= x * 2**(e*PyLong_SHIFT), so the log ~=
+ log(x) + log(2) * e * PyLong_SHIFT.
+ CAUTION: e*PyLong_SHIFT may overflow using int arithmetic,
+ so force use of double. */
+ x = func(x) + (e * (double)PyLong_SHIFT) * func(2.0);
+ return PyFloat_FromDouble(x);
+ }
+
+ /* Else let libm handle it by itself. */
+ return math_1(arg, func, 0);
}
static PyObject *
math_log(PyObject *self, PyObject *args)
{
- PyObject *arg;
- PyObject *base = NULL;
- PyObject *num, *den;
- PyObject *ans;
-
- if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
- return NULL;
-
- num = loghelper(arg, m_log, "log");
- if (num == NULL || base == NULL)
- return num;
-
- den = loghelper(base, m_log, "log");
- if (den == NULL) {
- Py_DECREF(num);
- return NULL;
- }
-
- ans = PyNumber_TrueDivide(num, den);
- Py_DECREF(num);
- Py_DECREF(den);
- return ans;
+ PyObject *arg;
+ PyObject *base = NULL;
+ PyObject *num, *den;
+ PyObject *ans;
+
+ if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
+ return NULL;
+
+ num = loghelper(arg, m_log, "log");
+ if (num == NULL || base == NULL)
+ return num;
+
+ den = loghelper(base, m_log, "log");
+ if (den == NULL) {
+ Py_DECREF(num);
+ return NULL;
+ }
+
+ ans = PyNumber_TrueDivide(num, den);
+ Py_DECREF(num);
+ Py_DECREF(den);
+ return ans;
}
PyDoc_STRVAR(math_log_doc,
@@ -911,7 +911,7 @@ If the base not specified, returns the natural logarithm (base e) of x.");
static PyObject *
math_log10(PyObject *self, PyObject *arg)
{
- return loghelper(arg, m_log10, "log10");
+ return loghelper(arg, m_log10, "log10");
}
PyDoc_STRVAR(math_log10_doc,
@@ -920,31 +920,31 @@ PyDoc_STRVAR(math_log10_doc,
static PyObject *
math_fmod(PyObject *self, PyObject *args)
{
- PyObject *ox, *oy;
- double r, x, y;
- if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy))
- return NULL;
- x = PyFloat_AsDouble(ox);
- y = PyFloat_AsDouble(oy);
- if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
- return NULL;
- /* fmod(x, +/-Inf) returns x for finite x. */
- if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
- return PyFloat_FromDouble(x);
- errno = 0;
- PyFPE_START_PROTECT("in math_fmod", return 0);
- r = fmod(x, y);
- PyFPE_END_PROTECT(r);
- if (Py_IS_NAN(r)) {
- if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
- errno = EDOM;
- else
- errno = 0;
- }
- if (errno && is_error(r))
- return NULL;
- else
- return PyFloat_FromDouble(r);
+ PyObject *ox, *oy;
+ double r, x, y;
+ if (! PyArg_UnpackTuple(args, "fmod", 2, 2, &ox, &oy))
+ return NULL;
+ x = PyFloat_AsDouble(ox);
+ y = PyFloat_AsDouble(oy);
+ if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
+ return NULL;
+ /* fmod(x, +/-Inf) returns x for finite x. */
+ if (Py_IS_INFINITY(y) && Py_IS_FINITE(x))
+ return PyFloat_FromDouble(x);
+ errno = 0;
+ PyFPE_START_PROTECT("in math_fmod", return 0);
+ r = fmod(x, y);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_NAN(r)) {
+ if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
+ errno = EDOM;
+ else
+ errno = 0;
+ }
+ if (errno && is_error(r))
+ return NULL;
+ else
+ return PyFloat_FromDouble(r);
}
PyDoc_STRVAR(math_fmod_doc,
@@ -954,39 +954,39 @@ PyDoc_STRVAR(math_fmod_doc,
static PyObject *
math_hypot(PyObject *self, PyObject *args)
{
- PyObject *ox, *oy;
- double r, x, y;
- if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy))
- return NULL;
- x = PyFloat_AsDouble(ox);
- y = PyFloat_AsDouble(oy);
- if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
- return NULL;
- /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */
- if (Py_IS_INFINITY(x))
- return PyFloat_FromDouble(fabs(x));
- if (Py_IS_INFINITY(y))
- return PyFloat_FromDouble(fabs(y));
- errno = 0;
- PyFPE_START_PROTECT("in math_hypot", return 0);
- r = hypot(x, y);
- PyFPE_END_PROTECT(r);
- if (Py_IS_NAN(r)) {
- if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
- errno = EDOM;
- else
- errno = 0;
- }
- else if (Py_IS_INFINITY(r)) {
- if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
- errno = ERANGE;
- else
- errno = 0;
- }
- if (errno && is_error(r))
- return NULL;
- else
- return PyFloat_FromDouble(r);
+ PyObject *ox, *oy;
+ double r, x, y;
+ if (! PyArg_UnpackTuple(args, "hypot", 2, 2, &ox, &oy))
+ return NULL;
+ x = PyFloat_AsDouble(ox);
+ y = PyFloat_AsDouble(oy);
+ if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
+ return NULL;
+ /* hypot(x, +/-Inf) returns Inf, even if x is a NaN. */
+ if (Py_IS_INFINITY(x))
+ return PyFloat_FromDouble(fabs(x));
+ if (Py_IS_INFINITY(y))
+ return PyFloat_FromDouble(fabs(y));
+ errno = 0;
+ PyFPE_START_PROTECT("in math_hypot", return 0);
+ r = hypot(x, y);
+ PyFPE_END_PROTECT(r);
+ if (Py_IS_NAN(r)) {
+ if (!Py_IS_NAN(x) && !Py_IS_NAN(y))
+ errno = EDOM;
+ else
+ errno = 0;
+ }
+ else if (Py_IS_INFINITY(r)) {
+ if (Py_IS_FINITE(x) && Py_IS_FINITE(y))
+ errno = ERANGE;
+ else
+ errno = 0;
+ }
+ if (errno && is_error(r))
+ return NULL;
+ else
+ return PyFloat_FromDouble(r);
}
PyDoc_STRVAR(math_hypot_doc,
@@ -1001,79 +1001,79 @@ PyDoc_STRVAR(math_hypot_doc,
static PyObject *
math_pow(PyObject *self, PyObject *args)
{
- PyObject *ox, *oy;
- double r, x, y;
- int odd_y;
-
- if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
- return NULL;
- x = PyFloat_AsDouble(ox);
- y = PyFloat_AsDouble(oy);
- if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
- return NULL;
-
- /* deal directly with IEEE specials, to cope with problems on various
- platforms whose semantics don't exactly match C99 */
- r = 0.; /* silence compiler warning */
- if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
- errno = 0;
- if (Py_IS_NAN(x))
- r = y == 0. ? 1. : x; /* NaN**0 = 1 */
- else if (Py_IS_NAN(y))
- r = x == 1. ? 1. : y; /* 1**NaN = 1 */
- else if (Py_IS_INFINITY(x)) {
- odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
- if (y > 0.)
- r = odd_y ? x : fabs(x);
- else if (y == 0.)
- r = 1.;
- else /* y < 0. */
- r = odd_y ? copysign(0., x) : 0.;
- }
- else if (Py_IS_INFINITY(y)) {
- if (fabs(x) == 1.0)
- r = 1.;
- else if (y > 0. && fabs(x) > 1.0)
- r = y;
- else if (y < 0. && fabs(x) < 1.0) {
- r = -y; /* result is +inf */
- if (x == 0.) /* 0**-inf: divide-by-zero */
- errno = EDOM;
- }
- else
- r = 0.;
- }
- }
- else {
- /* let libm handle finite**finite */
- errno = 0;
- PyFPE_START_PROTECT("in math_pow", return 0);
- r = pow(x, y);
- PyFPE_END_PROTECT(r);
- /* a NaN result should arise only from (-ve)**(finite
- non-integer); in this case we want to raise ValueError. */
- if (!Py_IS_FINITE(r)) {
- if (Py_IS_NAN(r)) {
- errno = EDOM;
- }
- /*
- an infinite result here arises either from:
- (A) (+/-0.)**negative (-> divide-by-zero)
- (B) overflow of x**y with x and y finite
- */
- else if (Py_IS_INFINITY(r)) {
- if (x == 0.)
- errno = EDOM;
- else
- errno = ERANGE;
- }
- }
- }
-
- if (errno && is_error(r))
- return NULL;
- else
- return PyFloat_FromDouble(r);
+ PyObject *ox, *oy;
+ double r, x, y;
+ int odd_y;
+
+ if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
+ return NULL;
+ x = PyFloat_AsDouble(ox);
+ y = PyFloat_AsDouble(oy);
+ if ((x == -1.0 || y == -1.0) && PyErr_Occurred())
+ return NULL;
+
+ /* deal directly with IEEE specials, to cope with problems on various
+ platforms whose semantics don't exactly match C99 */
+ r = 0.; /* silence compiler warning */
+ if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) {
+ errno = 0;
+ if (Py_IS_NAN(x))
+ r = y == 0. ? 1. : x; /* NaN**0 = 1 */
+ else if (Py_IS_NAN(y))
+ r = x == 1. ? 1. : y; /* 1**NaN = 1 */
+ else if (Py_IS_INFINITY(x)) {
+ odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0;
+ if (y > 0.)
+ r = odd_y ? x : fabs(x);
+ else if (y == 0.)
+ r = 1.;
+ else /* y < 0. */
+ r = odd_y ? copysign(0., x) : 0.;
+ }
+ else if (Py_IS_INFINITY(y)) {
+ if (fabs(x) == 1.0)
+ r = 1.;
+ else if (y > 0. && fabs(x) > 1.0)
+ r = y;
+ else if (y < 0. && fabs(x) < 1.0) {
+ r = -y; /* result is +inf */
+ if (x == 0.) /* 0**-inf: divide-by-zero */
+ errno = EDOM;
+ }
+ else
+ r = 0.;
+ }
+ }
+ else {
+ /* let libm handle finite**finite */
+ errno = 0;
+ PyFPE_START_PROTECT("in math_pow", return 0);
+ r = pow(x, y);
+ PyFPE_END_PROTECT(r);
+ /* a NaN result should arise only from (-ve)**(finite
+ non-integer); in this case we want to raise ValueError. */
+ if (!Py_IS_FINITE(r)) {
+ if (Py_IS_NAN(r)) {
+ errno = EDOM;
+ }
+ /*
+ an infinite result here arises either from:
+ (A) (+/-0.)**negative (-> divide-by-zero)
+ (B) overflow of x**y with x and y finite
+ */
+ else if (Py_IS_INFINITY(r)) {
+ if (x == 0.)
+ errno = EDOM;
+ else
+ errno = ERANGE;
+ }
+ }
+ }
+
+ if (errno && is_error(r))
+ return NULL;
+ else
+ return PyFloat_FromDouble(r);
}
PyDoc_STRVAR(math_pow_doc,
@@ -1085,10 +1085,10 @@ static const double radToDeg = 180.0 / Py_MATH_PI;
static PyObject *
math_degrees(PyObject *self, PyObject *arg)
{
- double x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(x * radToDeg);
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(x * radToDeg);
}
PyDoc_STRVAR(math_degrees_doc,
@@ -1098,10 +1098,10 @@ Convert angle x from radians to degrees.");
static PyObject *
math_radians(PyObject *self, PyObject *arg)
{
- double x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(x * degToRad);
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(x * degToRad);
}
PyDoc_STRVAR(math_radians_doc,
@@ -1111,10 +1111,10 @@ Convert angle x from degrees to radians.");
static PyObject *
math_isnan(PyObject *self, PyObject *arg)
{
- double x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)Py_IS_NAN(x));
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_NAN(x));
}
PyDoc_STRVAR(math_isnan_doc,
@@ -1124,10 +1124,10 @@ Check if float x is not a number (NaN).");
static PyObject *
math_isinf(PyObject *self, PyObject *arg)
{
- double x = PyFloat_AsDouble(arg);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)Py_IS_INFINITY(x));
+ double x = PyFloat_AsDouble(arg);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_INFINITY(x));
}
PyDoc_STRVAR(math_isinf_doc,
@@ -1135,42 +1135,42 @@ PyDoc_STRVAR(math_isinf_doc,
Check if float x is infinite (positive or negative).");
static PyMethodDef math_methods[] = {
- {"acos", math_acos, METH_O, math_acos_doc},
- {"acosh", math_acosh, METH_O, math_acosh_doc},
- {"asin", math_asin, METH_O, math_asin_doc},
- {"asinh", math_asinh, METH_O, math_asinh_doc},
- {"atan", math_atan, METH_O, math_atan_doc},
- {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
- {"atanh", math_atanh, METH_O, math_atanh_doc},
- {"ceil", math_ceil, METH_O, math_ceil_doc},
- {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
- {"cos", math_cos, METH_O, math_cos_doc},
- {"cosh", math_cosh, METH_O, math_cosh_doc},
- {"degrees", math_degrees, METH_O, math_degrees_doc},
- {"exp", math_exp, METH_O, math_exp_doc},
- {"fabs", math_fabs, METH_O, math_fabs_doc},
- {"factorial", math_factorial, METH_O, math_factorial_doc},
- {"floor", math_floor, METH_O, math_floor_doc},
- {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
- {"frexp", math_frexp, METH_O, math_frexp_doc},
- {"fsum", math_fsum, METH_O, math_fsum_doc},
- {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
- {"isinf", math_isinf, METH_O, math_isinf_doc},
- {"isnan", math_isnan, METH_O, math_isnan_doc},
- {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
- {"log", math_log, METH_VARARGS, math_log_doc},
- {"log1p", math_log1p, METH_O, math_log1p_doc},
- {"log10", math_log10, METH_O, math_log10_doc},
- {"modf", math_modf, METH_O, math_modf_doc},
- {"pow", math_pow, METH_VARARGS, math_pow_doc},
- {"radians", math_radians, METH_O, math_radians_doc},
- {"sin", math_sin, METH_O, math_sin_doc},
- {"sinh", math_sinh, METH_O, math_sinh_doc},
- {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
- {"tan", math_tan, METH_O, math_tan_doc},
- {"tanh", math_tanh, METH_O, math_tanh_doc},
- {"trunc", math_trunc, METH_O, math_trunc_doc},
- {NULL, NULL} /* sentinel */
+ {"acos", math_acos, METH_O, math_acos_doc},
+ {"acosh", math_acosh, METH_O, math_acosh_doc},
+ {"asin", math_asin, METH_O, math_asin_doc},
+ {"asinh", math_asinh, METH_O, math_asinh_doc},
+ {"atan", math_atan, METH_O, math_atan_doc},
+ {"atan2", math_atan2, METH_VARARGS, math_atan2_doc},
+ {"atanh", math_atanh, METH_O, math_atanh_doc},
+ {"ceil", math_ceil, METH_O, math_ceil_doc},
+ {"copysign", math_copysign, METH_VARARGS, math_copysign_doc},
+ {"cos", math_cos, METH_O, math_cos_doc},
+ {"cosh", math_cosh, METH_O, math_cosh_doc},
+ {"degrees", math_degrees, METH_O, math_degrees_doc},
+ {"exp", math_exp, METH_O, math_exp_doc},
+ {"fabs", math_fabs, METH_O, math_fabs_doc},
+ {"factorial", math_factorial, METH_O, math_factorial_doc},
+ {"floor", math_floor, METH_O, math_floor_doc},
+ {"fmod", math_fmod, METH_VARARGS, math_fmod_doc},
+ {"frexp", math_frexp, METH_O, math_frexp_doc},
+ {"fsum", math_fsum, METH_O, math_fsum_doc},
+ {"hypot", math_hypot, METH_VARARGS, math_hypot_doc},
+ {"isinf", math_isinf, METH_O, math_isinf_doc},
+ {"isnan", math_isnan, METH_O, math_isnan_doc},
+ {"ldexp", math_ldexp, METH_VARARGS, math_ldexp_doc},
+ {"log", math_log, METH_VARARGS, math_log_doc},
+ {"log1p", math_log1p, METH_O, math_log1p_doc},
+ {"log10", math_log10, METH_O, math_log10_doc},
+ {"modf", math_modf, METH_O, math_modf_doc},
+ {"pow", math_pow, METH_VARARGS, math_pow_doc},
+ {"radians", math_radians, METH_O, math_radians_doc},
+ {"sin", math_sin, METH_O, math_sin_doc},
+ {"sinh", math_sinh, METH_O, math_sinh_doc},
+ {"sqrt", math_sqrt, METH_O, math_sqrt_doc},
+ {"tan", math_tan, METH_O, math_tan_doc},
+ {"tanh", math_tanh, METH_O, math_tanh_doc},
+ {"trunc", math_trunc, METH_O, math_trunc_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -1180,29 +1180,29 @@ PyDoc_STRVAR(module_doc,
static struct PyModuleDef mathmodule = {
- PyModuleDef_HEAD_INIT,
- "math",
- module_doc,
- -1,
- math_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "math",
+ module_doc,
+ -1,
+ math_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_math(void)
{
- PyObject *m;
+ PyObject *m;
- m = PyModule_Create(&mathmodule);
- if (m == NULL)
- goto finally;
+ m = PyModule_Create(&mathmodule);
+ if (m == NULL)
+ goto finally;
- PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
- PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
+ PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI));
+ PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E));
finally:
- return m;
+ return m;
}
diff --git a/Modules/md5module.c b/Modules/md5module.c
index 6476bb7..d217dc8 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -23,8 +23,8 @@
/* Some useful types */
#if SIZEOF_INT == 4
-typedef unsigned int MD5_INT32; /* 32-bit integer */
-typedef PY_LONG_LONG MD5_INT64; /* 64-bit integer */
+typedef unsigned int MD5_INT32; /* 32-bit integer */
+typedef PY_LONG_LONG MD5_INT64; /* 64-bit integer */
#else
/* not defined. compilation will die. */
#endif
@@ -127,7 +127,7 @@ static void md5_compress(struct md5_state *md5, unsigned char *buf)
for (i = 0; i < 16; i++) {
LOAD32L(W[i], buf + (4*i));
}
-
+
/* copy state */
a = md5->state[0];
b = md5->state[1];
@@ -386,21 +386,21 @@ MD5_hexdigest(MD5object *self, PyObject *unused)
/* Create a new string */
retval = PyUnicode_FromStringAndSize(NULL, MD5_DIGESTSIZE * 2);
if (!retval)
- return NULL;
+ return NULL;
hex_digest = PyUnicode_AS_UNICODE(retval);
if (!hex_digest) {
- Py_DECREF(retval);
- return NULL;
+ Py_DECREF(retval);
+ return NULL;
}
/* Make hex version of the digest */
for(i=j=0; i<MD5_DIGESTSIZE; i++) {
char c;
c = (digest[i] >> 4) & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xf);
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
}
return retval;
@@ -414,7 +414,7 @@ MD5_update(MD5object *self, PyObject *args)
{
PyObject *obj;
Py_buffer buf;
-
+
if (!PyArg_ParseTuple(args, "O:update", &obj))
return NULL;
@@ -428,11 +428,11 @@ MD5_update(MD5object *self, PyObject *args)
}
static PyMethodDef MD5_methods[] = {
- {"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__},
- {"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__},
+ {"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__},
+ {"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__},
{"hexdigest", (PyCFunction)MD5_hexdigest, METH_NOARGS, MD5_hexdigest__doc__},
- {"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__},
- {NULL, NULL} /* sentinel */
+ {"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -472,13 +472,13 @@ static PyGetSetDef MD5_getseters[] = {
static PyTypeObject MD5type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_md5.md5", /*tp_name*/
- sizeof(MD5object), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_md5.md5", /*tp_name*/
+ sizeof(MD5object), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- MD5_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ MD5_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -494,13 +494,13 @@ static PyTypeObject MD5type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- MD5_methods, /* tp_methods */
- NULL, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ MD5_methods, /* tp_methods */
+ NULL, /* tp_members */
MD5_getseters, /* tp_getset */
};
@@ -553,7 +553,7 @@ MD5_new(PyObject *self, PyObject *args, PyObject *kwdict)
static struct PyMethodDef MD5_functions[] = {
{"md5", (PyCFunction)MD5_new, METH_VARARGS|METH_KEYWORDS, MD5_new__doc__},
- {NULL, NULL} /* Sentinel */
+ {NULL, NULL} /* Sentinel */
};
@@ -563,15 +563,15 @@ static struct PyMethodDef MD5_functions[] = {
static struct PyModuleDef _md5module = {
- PyModuleDef_HEAD_INIT,
- "_md5",
- NULL,
- -1,
- MD5_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_md5",
+ NULL,
+ -1,
+ MD5_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 9d6d6af..9714ddf 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -30,18 +30,18 @@
static int
my_getpagesize(void)
{
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- return si.dwPageSize;
+ SYSTEM_INFO si;
+ GetSystemInfo(&si);
+ return si.dwPageSize;
}
static int
my_getallocationgranularity (void)
{
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- return si.dwAllocationGranularity;
+ SYSTEM_INFO si;
+ GetSystemInfo(&si);
+ return si.dwAllocationGranularity;
}
#endif
@@ -54,7 +54,7 @@ my_getallocationgranularity (void)
static int
my_getpagesize(void)
{
- return sysconf(_SC_PAGESIZE);
+ return sysconf(_SC_PAGESIZE);
}
#define my_getallocationgranularity my_getpagesize
@@ -79,31 +79,31 @@ static PyObject *mmap_module_error;
typedef enum
{
- ACCESS_DEFAULT,
- ACCESS_READ,
- ACCESS_WRITE,
- ACCESS_COPY
+ ACCESS_DEFAULT,
+ ACCESS_READ,
+ ACCESS_WRITE,
+ ACCESS_COPY
} access_mode;
typedef struct {
- PyObject_HEAD
- char * data;
- size_t size;
- size_t pos; /* relative to offset */
- size_t offset;
- int exports;
+ PyObject_HEAD
+ char * data;
+ size_t size;
+ size_t pos; /* relative to offset */
+ size_t offset;
+ int exports;
#ifdef MS_WINDOWS
- HANDLE map_handle;
- HANDLE file_handle;
- char * tagname;
+ HANDLE map_handle;
+ HANDLE file_handle;
+ char * tagname;
#endif
#ifdef UNIX
- int fd;
+ int fd;
#endif
- access_mode access;
+ access_mode access;
} mmap_object;
@@ -111,331 +111,331 @@ static void
mmap_object_dealloc(mmap_object *m_obj)
{
#ifdef MS_WINDOWS
- if (m_obj->data != NULL)
- UnmapViewOfFile (m_obj->data);
- if (m_obj->map_handle != NULL)
- CloseHandle (m_obj->map_handle);
- if (m_obj->file_handle != INVALID_HANDLE_VALUE)
- CloseHandle (m_obj->file_handle);
- if (m_obj->tagname)
- PyMem_Free(m_obj->tagname);
+ if (m_obj->data != NULL)
+ UnmapViewOfFile (m_obj->data);
+ if (m_obj->map_handle != NULL)
+ CloseHandle (m_obj->map_handle);
+ if (m_obj->file_handle != INVALID_HANDLE_VALUE)
+ CloseHandle (m_obj->file_handle);
+ if (m_obj->tagname)
+ PyMem_Free(m_obj->tagname);
#endif /* MS_WINDOWS */
#ifdef UNIX
- if (m_obj->fd >= 0)
- (void) close(m_obj->fd);
- if (m_obj->data!=NULL) {
- msync(m_obj->data, m_obj->size, MS_SYNC);
- munmap(m_obj->data, m_obj->size);
- }
+ if (m_obj->fd >= 0)
+ (void) close(m_obj->fd);
+ if (m_obj->data!=NULL) {
+ msync(m_obj->data, m_obj->size, MS_SYNC);
+ munmap(m_obj->data, m_obj->size);
+ }
#endif /* UNIX */
- Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
+ Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
}
static PyObject *
mmap_close_method(mmap_object *self, PyObject *unused)
{
- if (self->exports > 0) {
- PyErr_SetString(PyExc_BufferError, "cannot close "\
- "exported pointers exist");
- return NULL;
- }
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_BufferError, "cannot close "\
+ "exported pointers exist");
+ return NULL;
+ }
#ifdef MS_WINDOWS
- /* For each resource we maintain, we need to check
- the value is valid, and if so, free the resource
- and set the member value to an invalid value so
- the dealloc does not attempt to resource clearing
- again.
- TODO - should we check for errors in the close operations???
- */
- if (self->data != NULL) {
- UnmapViewOfFile(self->data);
- self->data = NULL;
- }
- if (self->map_handle != NULL) {
- CloseHandle(self->map_handle);
- self->map_handle = NULL;
- }
- if (self->file_handle != INVALID_HANDLE_VALUE) {
- CloseHandle(self->file_handle);
- self->file_handle = INVALID_HANDLE_VALUE;
- }
+ /* For each resource we maintain, we need to check
+ the value is valid, and if so, free the resource
+ and set the member value to an invalid value so
+ the dealloc does not attempt to resource clearing
+ again.
+ TODO - should we check for errors in the close operations???
+ */
+ if (self->data != NULL) {
+ UnmapViewOfFile(self->data);
+ self->data = NULL;
+ }
+ if (self->map_handle != NULL) {
+ CloseHandle(self->map_handle);
+ self->map_handle = NULL;
+ }
+ if (self->file_handle != INVALID_HANDLE_VALUE) {
+ CloseHandle(self->file_handle);
+ self->file_handle = INVALID_HANDLE_VALUE;
+ }
#endif /* MS_WINDOWS */
#ifdef UNIX
- if (0 <= self->fd)
- (void) close(self->fd);
- self->fd = -1;
- if (self->data != NULL) {
- munmap(self->data, self->size);
- self->data = NULL;
- }
+ if (0 <= self->fd)
+ (void) close(self->fd);
+ self->fd = -1;
+ if (self->data != NULL) {
+ munmap(self->data, self->size);
+ self->data = NULL;
+ }
#endif
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
#ifdef MS_WINDOWS
-#define CHECK_VALID(err) \
-do { \
- if (self->map_handle == NULL) { \
- PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
- return err; \
- } \
+#define CHECK_VALID(err) \
+do { \
+ if (self->map_handle == NULL) { \
+ PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
+ return err; \
+ } \
} while (0)
#endif /* MS_WINDOWS */
#ifdef UNIX
-#define CHECK_VALID(err) \
-do { \
- if (self->data == NULL) { \
- PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
- return err; \
- } \
+#define CHECK_VALID(err) \
+do { \
+ if (self->data == NULL) { \
+ PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
+ return err; \
+ } \
} while (0)
#endif /* UNIX */
static PyObject *
mmap_read_byte_method(mmap_object *self,
- PyObject *unused)
+ PyObject *unused)
{
- CHECK_VALID(NULL);
- if (self->pos < self->size) {
- char value = self->data[self->pos];
- self->pos += 1;
- return Py_BuildValue("b", value);
- } else {
- PyErr_SetString(PyExc_ValueError, "read byte out of range");
- return NULL;
- }
+ CHECK_VALID(NULL);
+ if (self->pos < self->size) {
+ char value = self->data[self->pos];
+ self->pos += 1;
+ return Py_BuildValue("b", value);
+ } else {
+ PyErr_SetString(PyExc_ValueError, "read byte out of range");
+ return NULL;
+ }
}
static PyObject *
mmap_read_line_method(mmap_object *self,
- PyObject *unused)
+ PyObject *unused)
{
- char *start = self->data+self->pos;
- char *eof = self->data+self->size;
- char *eol;
- PyObject *result;
-
- CHECK_VALID(NULL);
-
- eol = memchr(start, '\n', self->size - self->pos);
- if (!eol)
- eol = eof;
- else
- ++eol; /* we're interested in the position after the
- newline. */
- result = PyBytes_FromStringAndSize(start, (eol - start));
- self->pos += (eol - start);
- return result;
+ char *start = self->data+self->pos;
+ char *eof = self->data+self->size;
+ char *eol;
+ PyObject *result;
+
+ CHECK_VALID(NULL);
+
+ eol = memchr(start, '\n', self->size - self->pos);
+ if (!eol)
+ eol = eof;
+ else
+ ++eol; /* we're interested in the position after the
+ newline. */
+ result = PyBytes_FromStringAndSize(start, (eol - start));
+ self->pos += (eol - start);
+ return result;
}
static PyObject *
mmap_read_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- Py_ssize_t num_bytes, n;
- PyObject *result;
-
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
- return(NULL);
-
- /* silently 'adjust' out-of-range requests */
- assert(self->size >= self->pos);
- n = self->size - self->pos;
- /* The difference can overflow, only if self->size is greater than
- * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
- * because the mapped area and the returned string each need more
- * than half of the addressable memory. So we clip the size, and let
- * the code below raise MemoryError.
- */
- if (n < 0)
- n = PY_SSIZE_T_MAX;
- if (num_bytes < 0 || num_bytes > n) {
- num_bytes = n;
- }
- result = PyBytes_FromStringAndSize(self->data+self->pos, num_bytes);
- self->pos += num_bytes;
- return result;
+ Py_ssize_t num_bytes, n;
+ PyObject *result;
+
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
+ return(NULL);
+
+ /* silently 'adjust' out-of-range requests */
+ assert(self->size >= self->pos);
+ n = self->size - self->pos;
+ /* The difference can overflow, only if self->size is greater than
+ * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
+ * because the mapped area and the returned string each need more
+ * than half of the addressable memory. So we clip the size, and let
+ * the code below raise MemoryError.
+ */
+ if (n < 0)
+ n = PY_SSIZE_T_MAX;
+ if (num_bytes < 0 || num_bytes > n) {
+ num_bytes = n;
+ }
+ result = PyBytes_FromStringAndSize(self->data+self->pos, num_bytes);
+ self->pos += num_bytes;
+ return result;
}
static PyObject *
mmap_gfind(mmap_object *self,
- PyObject *args,
- int reverse)
+ PyObject *args,
+ int reverse)
{
- Py_ssize_t start = self->pos;
- Py_ssize_t end = self->size;
- const char *needle;
- Py_ssize_t len;
-
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find",
- &needle, &len, &start, &end)) {
- return NULL;
- } else {
- const char *p, *start_p, *end_p;
- int sign = reverse ? -1 : 1;
-
- if (start < 0)
- start += self->size;
- if (start < 0)
- start = 0;
- else if ((size_t)start > self->size)
- start = self->size;
-
- if (end < 0)
- end += self->size;
- if (end < 0)
- end = 0;
- else if ((size_t)end > self->size)
- end = self->size;
-
- start_p = self->data + start;
- end_p = self->data + end;
-
- for (p = (reverse ? end_p - len : start_p);
- (p >= start_p) && (p + len <= end_p); p += sign) {
- Py_ssize_t i;
- for (i = 0; i < len && needle[i] == p[i]; ++i)
- /* nothing */;
- if (i == len) {
- return PyLong_FromSsize_t(p - self->data);
- }
- }
- return PyLong_FromLong(-1);
- }
+ Py_ssize_t start = self->pos;
+ Py_ssize_t end = self->size;
+ const char *needle;
+ Py_ssize_t len;
+
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find",
+ &needle, &len, &start, &end)) {
+ return NULL;
+ } else {
+ const char *p, *start_p, *end_p;
+ int sign = reverse ? -1 : 1;
+
+ if (start < 0)
+ start += self->size;
+ if (start < 0)
+ start = 0;
+ else if ((size_t)start > self->size)
+ start = self->size;
+
+ if (end < 0)
+ end += self->size;
+ if (end < 0)
+ end = 0;
+ else if ((size_t)end > self->size)
+ end = self->size;
+
+ start_p = self->data + start;
+ end_p = self->data + end;
+
+ for (p = (reverse ? end_p - len : start_p);
+ (p >= start_p) && (p + len <= end_p); p += sign) {
+ Py_ssize_t i;
+ for (i = 0; i < len && needle[i] == p[i]; ++i)
+ /* nothing */;
+ if (i == len) {
+ return PyLong_FromSsize_t(p - self->data);
+ }
+ }
+ return PyLong_FromLong(-1);
+ }
}
static PyObject *
mmap_find_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- return mmap_gfind(self, args, 0);
+ return mmap_gfind(self, args, 0);
}
static PyObject *
mmap_rfind_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- return mmap_gfind(self, args, 1);
+ return mmap_gfind(self, args, 1);
}
static int
is_writable(mmap_object *self)
{
- if (self->access != ACCESS_READ)
- return 1;
- PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
- return 0;
+ if (self->access != ACCESS_READ)
+ return 1;
+ PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
+ return 0;
}
static int
is_resizeable(mmap_object *self)
{
- if (self->exports > 0) {
- PyErr_SetString(PyExc_BufferError,
- "mmap can't resize with extant buffers exported.");
- return 0;
- }
- if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
- return 1;
- PyErr_Format(PyExc_TypeError,
- "mmap can't resize a readonly or copy-on-write memory map.");
- return 0;
+ if (self->exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "mmap can't resize with extant buffers exported.");
+ return 0;
+ }
+ if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
+ return 1;
+ PyErr_Format(PyExc_TypeError,
+ "mmap can't resize a readonly or copy-on-write memory map.");
+ return 0;
}
static PyObject *
mmap_write_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- Py_ssize_t length;
- char *data;
-
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "y#:write", &data, &length))
- return(NULL);
-
- if (!is_writable(self))
- return NULL;
-
- if ((self->pos + length) > self->size) {
- PyErr_SetString(PyExc_ValueError, "data out of range");
- return NULL;
- }
- memcpy(self->data+self->pos, data, length);
- self->pos = self->pos+length;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_ssize_t length;
+ char *data;
+
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "y#:write", &data, &length))
+ return(NULL);
+
+ if (!is_writable(self))
+ return NULL;
+
+ if ((self->pos + length) > self->size) {
+ PyErr_SetString(PyExc_ValueError, "data out of range");
+ return NULL;
+ }
+ memcpy(self->data+self->pos, data, length);
+ self->pos = self->pos+length;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
mmap_write_byte_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- char value;
-
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "b:write_byte", &value))
- return(NULL);
-
- if (!is_writable(self))
- return NULL;
-
- if (self->pos < self->size) {
- *(self->data+self->pos) = value;
- self->pos += 1;
- Py_INCREF(Py_None);
- return Py_None;
- }
- else {
- PyErr_SetString(PyExc_ValueError, "write byte out of range");
- return NULL;
- }
+ char value;
+
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "b:write_byte", &value))
+ return(NULL);
+
+ if (!is_writable(self))
+ return NULL;
+
+ if (self->pos < self->size) {
+ *(self->data+self->pos) = value;
+ self->pos += 1;
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else {
+ PyErr_SetString(PyExc_ValueError, "write byte out of range");
+ return NULL;
+ }
}
static PyObject *
mmap_size_method(mmap_object *self,
- PyObject *unused)
+ PyObject *unused)
{
- CHECK_VALID(NULL);
+ CHECK_VALID(NULL);
#ifdef MS_WINDOWS
- if (self->file_handle != INVALID_HANDLE_VALUE) {
- DWORD low,high;
- PY_LONG_LONG size;
- low = GetFileSize(self->file_handle, &high);
- if (low == INVALID_FILE_SIZE) {
- /* It might be that the function appears to have failed,
- when indeed its size equals INVALID_FILE_SIZE */
- DWORD error = GetLastError();
- if (error != NO_ERROR)
- return PyErr_SetFromWindowsErr(error);
- }
- if (!high && low < LONG_MAX)
- return PyLong_FromLong((long)low);
- size = (((PY_LONG_LONG)high)<<32) + low;
- return PyLong_FromLongLong(size);
- } else {
- return PyLong_FromSsize_t(self->size);
- }
+ if (self->file_handle != INVALID_HANDLE_VALUE) {
+ DWORD low,high;
+ PY_LONG_LONG size;
+ low = GetFileSize(self->file_handle, &high);
+ if (low == INVALID_FILE_SIZE) {
+ /* It might be that the function appears to have failed,
+ when indeed its size equals INVALID_FILE_SIZE */
+ DWORD error = GetLastError();
+ if (error != NO_ERROR)
+ return PyErr_SetFromWindowsErr(error);
+ }
+ if (!high && low < LONG_MAX)
+ return PyLong_FromLong((long)low);
+ size = (((PY_LONG_LONG)high)<<32) + low;
+ return PyLong_FromLongLong(size);
+ } else {
+ return PyLong_FromSsize_t(self->size);
+ }
#endif /* MS_WINDOWS */
#ifdef UNIX
- {
- struct stat buf;
- if (-1 == fstat(self->fd, &buf)) {
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- return PyLong_FromSsize_t(buf.st_size);
- }
+ {
+ struct stat buf;
+ if (-1 == fstat(self->fd, &buf)) {
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ return PyLong_FromSsize_t(buf.st_size);
+ }
#endif /* UNIX */
}
@@ -450,223 +450,223 @@ mmap_size_method(mmap_object *self,
static PyObject *
mmap_resize_method(mmap_object *self,
- PyObject *args)
+ PyObject *args)
{
- Py_ssize_t new_size;
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
- !is_resizeable(self)) {
- return NULL;
+ Py_ssize_t new_size;
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
+ !is_resizeable(self)) {
+ return NULL;
#ifdef MS_WINDOWS
- } else {
- DWORD dwErrCode = 0;
- DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
- /* First, unmap the file view */
- UnmapViewOfFile(self->data);
- self->data = NULL;
- /* Close the mapping object */
- CloseHandle(self->map_handle);
- self->map_handle = NULL;
- /* Move to the desired EOF position */
+ } else {
+ DWORD dwErrCode = 0;
+ DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
+ /* First, unmap the file view */
+ UnmapViewOfFile(self->data);
+ self->data = NULL;
+ /* Close the mapping object */
+ CloseHandle(self->map_handle);
+ self->map_handle = NULL;
+ /* Move to the desired EOF position */
#if SIZEOF_SIZE_T > 4
- newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
- newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
- off_hi = (DWORD)(self->offset >> 32);
- off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
+ newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
+ newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
+ off_hi = (DWORD)(self->offset >> 32);
+ off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
#else
- newSizeHigh = 0;
- newSizeLow = (DWORD)(self->offset + new_size);
- off_hi = 0;
- off_lo = (DWORD)self->offset;
+ newSizeHigh = 0;
+ newSizeLow = (DWORD)(self->offset + new_size);
+ off_hi = 0;
+ off_lo = (DWORD)self->offset;
#endif
- SetFilePointer(self->file_handle,
- newSizeLow, &newSizeHigh, FILE_BEGIN);
- /* Change the size of the file */
- SetEndOfFile(self->file_handle);
- /* Create another mapping object and remap the file view */
- self->map_handle = CreateFileMapping(
- self->file_handle,
- NULL,
- PAGE_READWRITE,
- 0,
- 0,
- self->tagname);
- if (self->map_handle != NULL) {
- self->data = (char *) MapViewOfFile(self->map_handle,
- FILE_MAP_WRITE,
- off_hi,
- off_lo,
- new_size);
- if (self->data != NULL) {
- self->size = new_size;
- Py_INCREF(Py_None);
- return Py_None;
- } else {
- dwErrCode = GetLastError();
- CloseHandle(self->map_handle);
- self->map_handle = NULL;
- }
- } else {
- dwErrCode = GetLastError();
- }
- PyErr_SetFromWindowsErr(dwErrCode);
- return NULL;
+ SetFilePointer(self->file_handle,
+ newSizeLow, &newSizeHigh, FILE_BEGIN);
+ /* Change the size of the file */
+ SetEndOfFile(self->file_handle);
+ /* Create another mapping object and remap the file view */
+ self->map_handle = CreateFileMapping(
+ self->file_handle,
+ NULL,
+ PAGE_READWRITE,
+ 0,
+ 0,
+ self->tagname);
+ if (self->map_handle != NULL) {
+ self->data = (char *) MapViewOfFile(self->map_handle,
+ FILE_MAP_WRITE,
+ off_hi,
+ off_lo,
+ new_size);
+ if (self->data != NULL) {
+ self->size = new_size;
+ Py_INCREF(Py_None);
+ return Py_None;
+ } else {
+ dwErrCode = GetLastError();
+ CloseHandle(self->map_handle);
+ self->map_handle = NULL;
+ }
+ } else {
+ dwErrCode = GetLastError();
+ }
+ PyErr_SetFromWindowsErr(dwErrCode);
+ return NULL;
#endif /* MS_WINDOWS */
#ifdef UNIX
#ifndef HAVE_MREMAP
- } else {
- PyErr_SetString(PyExc_SystemError,
- "mmap: resizing not available--no mremap()");
- return NULL;
+ } else {
+ PyErr_SetString(PyExc_SystemError,
+ "mmap: resizing not available--no mremap()");
+ return NULL;
#else
- } else {
- void *newmap;
+ } else {
+ void *newmap;
- if (ftruncate(self->fd, self->offset + new_size) == -1) {
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
+ if (ftruncate(self->fd, self->offset + new_size) == -1) {
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
#ifdef MREMAP_MAYMOVE
- newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
+ newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
#else
- #if defined(__NetBSD__)
- newmap = mremap(self->data, self->size, self->data, new_size, 0);
- #else
- newmap = mremap(self->data, self->size, new_size, 0);
- #endif /* __NetBSD__ */
+ #if defined(__NetBSD__)
+ newmap = mremap(self->data, self->size, self->data, new_size, 0);
+ #else
+ newmap = mremap(self->data, self->size, new_size, 0);
+ #endif /* __NetBSD__ */
#endif
- if (newmap == (void *)-1)
- {
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- self->data = newmap;
- self->size = new_size;
- Py_INCREF(Py_None);
- return Py_None;
+ if (newmap == (void *)-1)
+ {
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ self->data = newmap;
+ self->size = new_size;
+ Py_INCREF(Py_None);
+ return Py_None;
#endif /* HAVE_MREMAP */
#endif /* UNIX */
- }
+ }
}
static PyObject *
mmap_tell_method(mmap_object *self, PyObject *unused)
{
- CHECK_VALID(NULL);
- return PyLong_FromSize_t(self->pos);
+ CHECK_VALID(NULL);
+ return PyLong_FromSize_t(self->pos);
}
static PyObject *
mmap_flush_method(mmap_object *self, PyObject *args)
{
- Py_ssize_t offset = 0;
- Py_ssize_t size = self->size;
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
- return NULL;
- if ((size_t)(offset + size) > self->size) {
- PyErr_SetString(PyExc_ValueError, "flush values out of range");
- return NULL;
- }
+ Py_ssize_t offset = 0;
+ Py_ssize_t size = self->size;
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
+ return NULL;
+ if ((size_t)(offset + size) > self->size) {
+ PyErr_SetString(PyExc_ValueError, "flush values out of range");
+ return NULL;
+ }
#ifdef MS_WINDOWS
- return PyLong_FromLong((long) FlushViewOfFile(self->data+offset, size));
+ return PyLong_FromLong((long) FlushViewOfFile(self->data+offset, size));
#elif defined(UNIX)
- /* XXX semantics of return value? */
- /* XXX flags for msync? */
- if (-1 == msync(self->data + offset, size, MS_SYNC)) {
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- return PyLong_FromLong(0);
+ /* XXX semantics of return value? */
+ /* XXX flags for msync? */
+ if (-1 == msync(self->data + offset, size, MS_SYNC)) {
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ return PyLong_FromLong(0);
#else
- PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
+ return NULL;
#endif
}
static PyObject *
mmap_seek_method(mmap_object *self, PyObject *args)
{
- Py_ssize_t dist;
- int how=0;
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
- return NULL;
- else {
- size_t where;
- switch (how) {
- case 0: /* relative to start */
- if (dist < 0)
- goto onoutofrange;
- where = dist;
- break;
- case 1: /* relative to current position */
- if ((Py_ssize_t)self->pos + dist < 0)
- goto onoutofrange;
- where = self->pos + dist;
- break;
- case 2: /* relative to end */
- if ((Py_ssize_t)self->size + dist < 0)
- goto onoutofrange;
- where = self->size + dist;
- break;
- default:
- PyErr_SetString(PyExc_ValueError, "unknown seek type");
- return NULL;
- }
- if (where > self->size)
- goto onoutofrange;
- self->pos = where;
- Py_INCREF(Py_None);
- return Py_None;
- }
+ Py_ssize_t dist;
+ int how=0;
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
+ return NULL;
+ else {
+ size_t where;
+ switch (how) {
+ case 0: /* relative to start */
+ if (dist < 0)
+ goto onoutofrange;
+ where = dist;
+ break;
+ case 1: /* relative to current position */
+ if ((Py_ssize_t)self->pos + dist < 0)
+ goto onoutofrange;
+ where = self->pos + dist;
+ break;
+ case 2: /* relative to end */
+ if ((Py_ssize_t)self->size + dist < 0)
+ goto onoutofrange;
+ where = self->size + dist;
+ break;
+ default:
+ PyErr_SetString(PyExc_ValueError, "unknown seek type");
+ return NULL;
+ }
+ if (where > self->size)
+ goto onoutofrange;
+ self->pos = where;
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
onoutofrange:
- PyErr_SetString(PyExc_ValueError, "seek out of range");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "seek out of range");
+ return NULL;
}
static PyObject *
mmap_move_method(mmap_object *self, PyObject *args)
{
- unsigned long dest, src, cnt;
- CHECK_VALID(NULL);
- if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) ||
- !is_writable(self)) {
- return NULL;
- } else {
- /* bounds check the values */
- if (cnt < 0 || (cnt + dest) < cnt || (cnt + src) < cnt ||
- src < 0 || src > self->size || (src + cnt) > self->size ||
- dest < 0 || dest > self->size || (dest + cnt) > self->size) {
- PyErr_SetString(PyExc_ValueError,
- "source, destination, or count out of range");
- return NULL;
- }
- memmove(self->data+dest, self->data+src, cnt);
- Py_INCREF(Py_None);
- return Py_None;
- }
+ unsigned long dest, src, cnt;
+ CHECK_VALID(NULL);
+ if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) ||
+ !is_writable(self)) {
+ return NULL;
+ } else {
+ /* bounds check the values */
+ if (cnt < 0 || (cnt + dest) < cnt || (cnt + src) < cnt ||
+ src < 0 || src > self->size || (src + cnt) > self->size ||
+ dest < 0 || dest > self->size || (dest + cnt) > self->size) {
+ PyErr_SetString(PyExc_ValueError,
+ "source, destination, or count out of range");
+ return NULL;
+ }
+ memmove(self->data+dest, self->data+src, cnt);
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
}
static struct PyMethodDef mmap_object_methods[] = {
- {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
- {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
- {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
- {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
- {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
- {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
- {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
- {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
- {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
- {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
- {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
- {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
- {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
- {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
+ {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
+ {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
+ {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
+ {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
+ {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
+ {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
+ {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
+ {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
+ {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
+ {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
+ {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
+ {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
+ {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
/* Functions for treating an mmap'ed file as a buffer */
@@ -674,247 +674,247 @@ static struct PyMethodDef mmap_object_methods[] = {
static int
mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags)
{
- CHECK_VALID(-1);
- if (PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->size,
- (self->access == ACCESS_READ), flags) < 0)
- return -1;
- self->exports++;
- return 0;
+ CHECK_VALID(-1);
+ if (PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->size,
+ (self->access == ACCESS_READ), flags) < 0)
+ return -1;
+ self->exports++;
+ return 0;
}
static void
mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view)
{
- self->exports--;
+ self->exports--;
}
static Py_ssize_t
mmap_length(mmap_object *self)
{
- CHECK_VALID(-1);
- return self->size;
+ CHECK_VALID(-1);
+ return self->size;
}
static PyObject *
mmap_item(mmap_object *self, Py_ssize_t i)
{
- CHECK_VALID(NULL);
- if (i < 0 || (size_t)i >= self->size) {
- PyErr_SetString(PyExc_IndexError, "mmap index out of range");
- return NULL;
- }
- return PyBytes_FromStringAndSize(self->data + i, 1);
+ CHECK_VALID(NULL);
+ if (i < 0 || (size_t)i >= self->size) {
+ PyErr_SetString(PyExc_IndexError, "mmap index out of range");
+ return NULL;
+ }
+ return PyBytes_FromStringAndSize(self->data + i, 1);
}
static PyObject *
mmap_subscript(mmap_object *self, PyObject *item)
{
- CHECK_VALID(NULL);
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- if (i < 0)
- i += self->size;
- if (i < 0 || (size_t)i >= self->size) {
- PyErr_SetString(PyExc_IndexError,
- "mmap index out of range");
- return NULL;
- }
- return PyLong_FromLong(Py_CHARMASK(self->data[i]));
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen;
-
- if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
- &start, &stop, &step, &slicelen) < 0) {
- return NULL;
- }
-
- if (slicelen <= 0)
- return PyBytes_FromStringAndSize("", 0);
- else if (step == 1)
- return PyBytes_FromStringAndSize(self->data + start,
- slicelen);
- else {
- char *result_buf = (char *)PyMem_Malloc(slicelen);
- Py_ssize_t cur, i;
- PyObject *result;
-
- if (result_buf == NULL)
- return PyErr_NoMemory();
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- result_buf[i] = self->data[cur];
- }
- result = PyBytes_FromStringAndSize(result_buf,
- slicelen);
- PyMem_Free(result_buf);
- return result;
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "mmap indices must be integers");
- return NULL;
- }
+ CHECK_VALID(NULL);
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += self->size;
+ if (i < 0 || (size_t)i >= self->size) {
+ PyErr_SetString(PyExc_IndexError,
+ "mmap index out of range");
+ return NULL;
+ }
+ return PyLong_FromLong(Py_CHARMASK(self->data[i]));
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelen;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
+ &start, &stop, &step, &slicelen) < 0) {
+ return NULL;
+ }
+
+ if (slicelen <= 0)
+ return PyBytes_FromStringAndSize("", 0);
+ else if (step == 1)
+ return PyBytes_FromStringAndSize(self->data + start,
+ slicelen);
+ else {
+ char *result_buf = (char *)PyMem_Malloc(slicelen);
+ Py_ssize_t cur, i;
+ PyObject *result;
+
+ if (result_buf == NULL)
+ return PyErr_NoMemory();
+ for (cur = start, i = 0; i < slicelen;
+ cur += step, i++) {
+ result_buf[i] = self->data[cur];
+ }
+ result = PyBytes_FromStringAndSize(result_buf,
+ slicelen);
+ PyMem_Free(result_buf);
+ return result;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap indices must be integers");
+ return NULL;
+ }
}
static PyObject *
mmap_concat(mmap_object *self, PyObject *bb)
{
- CHECK_VALID(NULL);
- PyErr_SetString(PyExc_SystemError,
- "mmaps don't support concatenation");
- return NULL;
+ CHECK_VALID(NULL);
+ PyErr_SetString(PyExc_SystemError,
+ "mmaps don't support concatenation");
+ return NULL;
}
static PyObject *
mmap_repeat(mmap_object *self, Py_ssize_t n)
{
- CHECK_VALID(NULL);
- PyErr_SetString(PyExc_SystemError,
- "mmaps don't support repeat operation");
- return NULL;
+ CHECK_VALID(NULL);
+ PyErr_SetString(PyExc_SystemError,
+ "mmaps don't support repeat operation");
+ return NULL;
}
static int
mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
{
- const char *buf;
-
- CHECK_VALID(-1);
- if (i < 0 || (size_t)i >= self->size) {
- PyErr_SetString(PyExc_IndexError, "mmap index out of range");
- return -1;
- }
- if (v == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "mmap object doesn't support item deletion");
- return -1;
- }
- if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
- PyErr_SetString(PyExc_IndexError,
- "mmap assignment must be length-1 bytes()");
- return -1;
- }
- if (!is_writable(self))
- return -1;
- buf = PyBytes_AsString(v);
- self->data[i] = buf[0];
- return 0;
+ const char *buf;
+
+ CHECK_VALID(-1);
+ if (i < 0 || (size_t)i >= self->size) {
+ PyErr_SetString(PyExc_IndexError, "mmap index out of range");
+ return -1;
+ }
+ if (v == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap object doesn't support item deletion");
+ return -1;
+ }
+ if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) {
+ PyErr_SetString(PyExc_IndexError,
+ "mmap assignment must be length-1 bytes()");
+ return -1;
+ }
+ if (!is_writable(self))
+ return -1;
+ buf = PyBytes_AsString(v);
+ self->data[i] = buf[0];
+ return 0;
}
static int
mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
{
- CHECK_VALID(-1);
-
- if (!is_writable(self))
- return -1;
-
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- Py_ssize_t v;
-
- if (i == -1 && PyErr_Occurred())
- return -1;
- if (i < 0)
- i += self->size;
- if (i < 0 || (size_t)i >= self->size) {
- PyErr_SetString(PyExc_IndexError,
- "mmap index out of range");
- return -1;
- }
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "mmap doesn't support item deletion");
- return -1;
- }
- if (!PyIndex_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "mmap item value must be an int");
- return -1;
- }
- v = PyNumber_AsSsize_t(value, PyExc_TypeError);
- if (v == -1 && PyErr_Occurred())
- return -1;
- if (v < 0 || v > 255) {
- PyErr_SetString(PyExc_ValueError,
- "mmap item value must be "
- "in range(0, 256)");
- return -1;
- }
- self->data[i] = v;
- return 0;
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen;
- Py_buffer vbuf;
-
- if (PySlice_GetIndicesEx((PySliceObject *)item,
- self->size, &start, &stop,
- &step, &slicelen) < 0) {
- return -1;
- }
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "mmap object doesn't support slice deletion");
- return -1;
- }
- if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0)
- return -1;
- if (vbuf.len != slicelen) {
- PyErr_SetString(PyExc_IndexError,
- "mmap slice assignment is wrong size");
- PyBuffer_Release(&vbuf);
- return -1;
- }
-
- if (slicelen == 0) {
- }
- else if (step == 1) {
- memcpy(self->data + start, vbuf.buf, slicelen);
- }
- else {
- Py_ssize_t cur, i;
-
- for (cur = start, i = 0;
- i < slicelen;
- cur += step, i++)
- {
- self->data[cur] = ((char *)vbuf.buf)[i];
- }
- }
- PyBuffer_Release(&vbuf);
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "mmap indices must be integer");
- return -1;
- }
+ CHECK_VALID(-1);
+
+ if (!is_writable(self))
+ return -1;
+
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ Py_ssize_t v;
+
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0)
+ i += self->size;
+ if (i < 0 || (size_t)i >= self->size) {
+ PyErr_SetString(PyExc_IndexError,
+ "mmap index out of range");
+ return -1;
+ }
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap doesn't support item deletion");
+ return -1;
+ }
+ if (!PyIndex_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap item value must be an int");
+ return -1;
+ }
+ v = PyNumber_AsSsize_t(value, PyExc_TypeError);
+ if (v == -1 && PyErr_Occurred())
+ return -1;
+ if (v < 0 || v > 255) {
+ PyErr_SetString(PyExc_ValueError,
+ "mmap item value must be "
+ "in range(0, 256)");
+ return -1;
+ }
+ self->data[i] = v;
+ return 0;
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelen;
+ Py_buffer vbuf;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item,
+ self->size, &start, &stop,
+ &step, &slicelen) < 0) {
+ return -1;
+ }
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap object doesn't support slice deletion");
+ return -1;
+ }
+ if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0)
+ return -1;
+ if (vbuf.len != slicelen) {
+ PyErr_SetString(PyExc_IndexError,
+ "mmap slice assignment is wrong size");
+ PyBuffer_Release(&vbuf);
+ return -1;
+ }
+
+ if (slicelen == 0) {
+ }
+ else if (step == 1) {
+ memcpy(self->data + start, vbuf.buf, slicelen);
+ }
+ else {
+ Py_ssize_t cur, i;
+
+ for (cur = start, i = 0;
+ i < slicelen;
+ cur += step, i++)
+ {
+ self->data[cur] = ((char *)vbuf.buf)[i];
+ }
+ }
+ PyBuffer_Release(&vbuf);
+ return 0;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "mmap indices must be integer");
+ return -1;
+ }
}
static PySequenceMethods mmap_as_sequence = {
- (lenfunc)mmap_length, /*sq_length*/
- (binaryfunc)mmap_concat, /*sq_concat*/
- (ssizeargfunc)mmap_repeat, /*sq_repeat*/
- (ssizeargfunc)mmap_item, /*sq_item*/
- 0, /*sq_slice*/
- (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
- 0, /*sq_ass_slice*/
+ (lenfunc)mmap_length, /*sq_length*/
+ (binaryfunc)mmap_concat, /*sq_concat*/
+ (ssizeargfunc)mmap_repeat, /*sq_repeat*/
+ (ssizeargfunc)mmap_item, /*sq_item*/
+ 0, /*sq_slice*/
+ (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
+ 0, /*sq_ass_slice*/
};
static PyMappingMethods mmap_as_mapping = {
- (lenfunc)mmap_length,
- (binaryfunc)mmap_subscript,
- (objobjargproc)mmap_ass_subscript,
+ (lenfunc)mmap_length,
+ (binaryfunc)mmap_subscript,
+ (objobjargproc)mmap_ass_subscript,
};
static PyBufferProcs mmap_as_buffer = {
- (getbufferproc)mmap_buffer_getbuf,
- (releasebufferproc)mmap_buffer_releasebuf,
+ (getbufferproc)mmap_buffer_getbuf,
+ (releasebufferproc)mmap_buffer_releasebuf,
};
static PyObject *
@@ -945,46 +945,46 @@ To map anonymous memory, pass -1 as the fileno (both versions).");
static PyTypeObject mmap_object_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "mmap.mmap", /* tp_name */
- sizeof(mmap_object), /* tp_size */
- 0, /* tp_itemsize */
- /* methods */
- (destructor) mmap_object_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &mmap_as_sequence, /*tp_as_sequence*/
- &mmap_as_mapping, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- PyObject_GenericGetAttr, /*tp_getattro*/
- 0, /*tp_setattro*/
- &mmap_as_buffer, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- mmap_doc, /*tp_doc*/
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- mmap_object_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- new_mmap_object, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "mmap.mmap", /* tp_name */
+ sizeof(mmap_object), /* tp_size */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor) mmap_object_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &mmap_as_sequence, /*tp_as_sequence*/
+ &mmap_as_mapping, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ PyObject_GenericGetAttr, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ &mmap_as_buffer, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ mmap_doc, /*tp_doc*/
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ mmap_object_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ new_mmap_object, /* tp_new */
+ PyObject_Del, /* tp_free */
};
@@ -995,23 +995,23 @@ static PyTypeObject mmap_object_type = {
static Py_ssize_t
_GetMapSize(PyObject *o, const char* param)
{
- if (o == NULL)
- return 0;
- if (PyIndex_Check(o)) {
- Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
- if (i==-1 && PyErr_Occurred())
- return -1;
- if (i < 0) {
- PyErr_Format(PyExc_OverflowError,
- "memory mapped %s must be positive",
- param);
- return -1;
- }
- return i;
- }
-
- PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
- return -1;
+ if (o == NULL)
+ return 0;
+ if (PyIndex_Check(o)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
+ if (i==-1 && PyErr_Occurred())
+ return -1;
+ if (i < 0) {
+ PyErr_Format(PyExc_OverflowError,
+ "memory mapped %s must be positive",
+ param);
+ return -1;
+ }
+ return i;
+ }
+
+ PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
+ return -1;
}
#ifdef UNIX
@@ -1019,125 +1019,125 @@ static PyObject *
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
{
#ifdef HAVE_FSTAT
- struct stat st;
+ struct stat st;
#endif
- mmap_object *m_obj;
- PyObject *map_size_obj = NULL, *offset_obj = NULL;
- Py_ssize_t map_size, offset;
- int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
- int devzero = -1;
- int access = (int)ACCESS_DEFAULT;
- static char *keywords[] = {"fileno", "length",
- "flags", "prot",
- "access", "offset", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
- &fd, &map_size_obj, &flags, &prot,
- &access, &offset_obj))
- return NULL;
- map_size = _GetMapSize(map_size_obj, "size");
- if (map_size < 0)
- return NULL;
- offset = _GetMapSize(offset_obj, "offset");
- if (offset < 0)
- return NULL;
-
- if ((access != (int)ACCESS_DEFAULT) &&
- ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
- return PyErr_Format(PyExc_ValueError,
- "mmap can't specify both access and flags, prot.");
- switch ((access_mode)access) {
- case ACCESS_READ:
- flags = MAP_SHARED;
- prot = PROT_READ;
- break;
- case ACCESS_WRITE:
- flags = MAP_SHARED;
- prot = PROT_READ | PROT_WRITE;
- break;
- case ACCESS_COPY:
- flags = MAP_PRIVATE;
- prot = PROT_READ | PROT_WRITE;
- break;
- case ACCESS_DEFAULT:
- /* use the specified or default values of flags and prot */
- break;
- default:
- return PyErr_Format(PyExc_ValueError,
- "mmap invalid access parameter.");
- }
+ mmap_object *m_obj;
+ PyObject *map_size_obj = NULL, *offset_obj = NULL;
+ Py_ssize_t map_size, offset;
+ int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
+ int devzero = -1;
+ int access = (int)ACCESS_DEFAULT;
+ static char *keywords[] = {"fileno", "length",
+ "flags", "prot",
+ "access", "offset", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
+ &fd, &map_size_obj, &flags, &prot,
+ &access, &offset_obj))
+ return NULL;
+ map_size = _GetMapSize(map_size_obj, "size");
+ if (map_size < 0)
+ return NULL;
+ offset = _GetMapSize(offset_obj, "offset");
+ if (offset < 0)
+ return NULL;
+
+ if ((access != (int)ACCESS_DEFAULT) &&
+ ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
+ return PyErr_Format(PyExc_ValueError,
+ "mmap can't specify both access and flags, prot.");
+ switch ((access_mode)access) {
+ case ACCESS_READ:
+ flags = MAP_SHARED;
+ prot = PROT_READ;
+ break;
+ case ACCESS_WRITE:
+ flags = MAP_SHARED;
+ prot = PROT_READ | PROT_WRITE;
+ break;
+ case ACCESS_COPY:
+ flags = MAP_PRIVATE;
+ prot = PROT_READ | PROT_WRITE;
+ break;
+ case ACCESS_DEFAULT:
+ /* use the specified or default values of flags and prot */
+ break;
+ default:
+ return PyErr_Format(PyExc_ValueError,
+ "mmap invalid access parameter.");
+ }
if (prot == PROT_READ) {
- access = ACCESS_READ;
+ access = ACCESS_READ;
}
#ifdef HAVE_FSTAT
# ifdef __VMS
- /* on OpenVMS we must ensure that all bytes are written to the file */
- if (fd != -1) {
- fsync(fd);
- }
+ /* on OpenVMS we must ensure that all bytes are written to the file */
+ if (fd != -1) {
+ fsync(fd);
+ }
# endif
- if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
- if (map_size == 0) {
- map_size = st.st_size;
- } else if ((size_t)offset + (size_t)map_size > st.st_size) {
- PyErr_SetString(PyExc_ValueError,
- "mmap length is greater than file size");
- return NULL;
- }
- }
+ if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
+ if (map_size == 0) {
+ map_size = st.st_size;
+ } else if ((size_t)offset + (size_t)map_size > st.st_size) {
+ PyErr_SetString(PyExc_ValueError,
+ "mmap length is greater than file size");
+ return NULL;
+ }
+ }
#endif
- m_obj = (mmap_object *)type->tp_alloc(type, 0);
- if (m_obj == NULL) {return NULL;}
- m_obj->data = NULL;
- m_obj->size = (size_t) map_size;
- m_obj->pos = (size_t) 0;
- m_obj->exports = 0;
- m_obj->offset = offset;
- if (fd == -1) {
- m_obj->fd = -1;
- /* Assume the caller wants to map anonymous memory.
- This is the same behaviour as Windows. mmap.mmap(-1, size)
- on both Windows and Unix map anonymous memory.
- */
+ m_obj = (mmap_object *)type->tp_alloc(type, 0);
+ if (m_obj == NULL) {return NULL;}
+ m_obj->data = NULL;
+ m_obj->size = (size_t) map_size;
+ m_obj->pos = (size_t) 0;
+ m_obj->exports = 0;
+ m_obj->offset = offset;
+ if (fd == -1) {
+ m_obj->fd = -1;
+ /* Assume the caller wants to map anonymous memory.
+ This is the same behaviour as Windows. mmap.mmap(-1, size)
+ on both Windows and Unix map anonymous memory.
+ */
#ifdef MAP_ANONYMOUS
- /* BSD way to map anonymous memory */
- flags |= MAP_ANONYMOUS;
+ /* BSD way to map anonymous memory */
+ flags |= MAP_ANONYMOUS;
#else
- /* SVR4 method to map anonymous memory is to open /dev/zero */
- fd = devzero = open("/dev/zero", O_RDWR);
- if (devzero == -1) {
- Py_DECREF(m_obj);
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
+ /* SVR4 method to map anonymous memory is to open /dev/zero */
+ fd = devzero = open("/dev/zero", O_RDWR);
+ if (devzero == -1) {
+ Py_DECREF(m_obj);
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
#endif
- } else {
- m_obj->fd = dup(fd);
- if (m_obj->fd == -1) {
- Py_DECREF(m_obj);
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- }
-
- m_obj->data = mmap(NULL, map_size,
- prot, flags,
- fd, offset);
-
- if (devzero != -1) {
- close(devzero);
- }
-
- if (m_obj->data == (char *)-1) {
- m_obj->data = NULL;
- Py_DECREF(m_obj);
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- m_obj->access = (access_mode)access;
- return (PyObject *)m_obj;
+ } else {
+ m_obj->fd = dup(fd);
+ if (m_obj->fd == -1) {
+ Py_DECREF(m_obj);
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ }
+
+ m_obj->data = mmap(NULL, map_size,
+ prot, flags,
+ fd, offset);
+
+ if (devzero != -1) {
+ close(devzero);
+ }
+
+ if (m_obj->data == (char *)-1) {
+ m_obj->data = NULL;
+ Py_DECREF(m_obj);
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ m_obj->access = (access_mode)access;
+ return (PyObject *)m_obj;
}
#endif /* UNIX */
@@ -1145,266 +1145,266 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
static PyObject *
new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
{
- mmap_object *m_obj;
- PyObject *map_size_obj = NULL, *offset_obj = NULL;
- Py_ssize_t map_size, offset;
- DWORD off_hi; /* upper 32 bits of offset */
- DWORD off_lo; /* lower 32 bits of offset */
- DWORD size_hi; /* upper 32 bits of size */
- DWORD size_lo; /* lower 32 bits of size */
- char *tagname = "";
- DWORD dwErr = 0;
- int fileno;
- HANDLE fh = 0;
- int access = (access_mode)ACCESS_DEFAULT;
- DWORD flProtect, dwDesiredAccess;
- static char *keywords[] = { "fileno", "length",
- "tagname",
- "access", "offset", NULL };
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
- &fileno, &map_size_obj,
- &tagname, &access, &offset_obj)) {
- return NULL;
- }
-
- switch((access_mode)access) {
- case ACCESS_READ:
- flProtect = PAGE_READONLY;
- dwDesiredAccess = FILE_MAP_READ;
- break;
- case ACCESS_DEFAULT: case ACCESS_WRITE:
- flProtect = PAGE_READWRITE;
- dwDesiredAccess = FILE_MAP_WRITE;
- break;
- case ACCESS_COPY:
- flProtect = PAGE_WRITECOPY;
- dwDesiredAccess = FILE_MAP_COPY;
- break;
- default:
- return PyErr_Format(PyExc_ValueError,
- "mmap invalid access parameter.");
- }
-
- map_size = _GetMapSize(map_size_obj, "size");
- if (map_size < 0)
- return NULL;
- offset = _GetMapSize(offset_obj, "offset");
- if (offset < 0)
- return NULL;
-
- /* assume -1 and 0 both mean invalid filedescriptor
- to 'anonymously' map memory.
- XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
- XXX: Should this code be added?
- if (fileno == 0)
- PyErr_WarnEx(PyExc_DeprecationWarning,
- "don't use 0 for anonymous memory",
- 1);
- */
- if (fileno != -1 && fileno != 0) {
- fh = (HANDLE)_get_osfhandle(fileno);
- if (fh==(HANDLE)-1) {
- PyErr_SetFromErrno(mmap_module_error);
- return NULL;
- }
- /* Win9x appears to need us seeked to zero */
- lseek(fileno, 0, SEEK_SET);
- }
-
- m_obj = (mmap_object *)type->tp_alloc(type, 0);
- if (m_obj == NULL)
- return NULL;
- /* Set every field to an invalid marker, so we can safely
- destruct the object in the face of failure */
- m_obj->data = NULL;
- m_obj->file_handle = INVALID_HANDLE_VALUE;
- m_obj->map_handle = NULL;
- m_obj->tagname = NULL;
- m_obj->offset = offset;
-
- if (fh) {
- /* It is necessary to duplicate the handle, so the
- Python code can close it on us */
- if (!DuplicateHandle(
- GetCurrentProcess(), /* source process handle */
- fh, /* handle to be duplicated */
- GetCurrentProcess(), /* target proc handle */
- (LPHANDLE)&m_obj->file_handle, /* result */
- 0, /* access - ignored due to options value */
- FALSE, /* inherited by child processes? */
- DUPLICATE_SAME_ACCESS)) { /* options */
- dwErr = GetLastError();
- Py_DECREF(m_obj);
- PyErr_SetFromWindowsErr(dwErr);
- return NULL;
- }
- if (!map_size) {
- DWORD low,high;
- low = GetFileSize(fh, &high);
- /* low might just happen to have the value INVALID_FILE_SIZE;
- so we need to check the last error also. */
- if (low == INVALID_FILE_SIZE &&
- (dwErr = GetLastError()) != NO_ERROR) {
- Py_DECREF(m_obj);
- return PyErr_SetFromWindowsErr(dwErr);
- }
+ mmap_object *m_obj;
+ PyObject *map_size_obj = NULL, *offset_obj = NULL;
+ Py_ssize_t map_size, offset;
+ DWORD off_hi; /* upper 32 bits of offset */
+ DWORD off_lo; /* lower 32 bits of offset */
+ DWORD size_hi; /* upper 32 bits of size */
+ DWORD size_lo; /* lower 32 bits of size */
+ char *tagname = "";
+ DWORD dwErr = 0;
+ int fileno;
+ HANDLE fh = 0;
+ int access = (access_mode)ACCESS_DEFAULT;
+ DWORD flProtect, dwDesiredAccess;
+ static char *keywords[] = { "fileno", "length",
+ "tagname",
+ "access", "offset", NULL };
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
+ &fileno, &map_size_obj,
+ &tagname, &access, &offset_obj)) {
+ return NULL;
+ }
+
+ switch((access_mode)access) {
+ case ACCESS_READ:
+ flProtect = PAGE_READONLY;
+ dwDesiredAccess = FILE_MAP_READ;
+ break;
+ case ACCESS_DEFAULT: case ACCESS_WRITE:
+ flProtect = PAGE_READWRITE;
+ dwDesiredAccess = FILE_MAP_WRITE;
+ break;
+ case ACCESS_COPY:
+ flProtect = PAGE_WRITECOPY;
+ dwDesiredAccess = FILE_MAP_COPY;
+ break;
+ default:
+ return PyErr_Format(PyExc_ValueError,
+ "mmap invalid access parameter.");
+ }
+
+ map_size = _GetMapSize(map_size_obj, "size");
+ if (map_size < 0)
+ return NULL;
+ offset = _GetMapSize(offset_obj, "offset");
+ if (offset < 0)
+ return NULL;
+
+ /* assume -1 and 0 both mean invalid filedescriptor
+ to 'anonymously' map memory.
+ XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
+ XXX: Should this code be added?
+ if (fileno == 0)
+ PyErr_WarnEx(PyExc_DeprecationWarning,
+ "don't use 0 for anonymous memory",
+ 1);
+ */
+ if (fileno != -1 && fileno != 0) {
+ fh = (HANDLE)_get_osfhandle(fileno);
+ if (fh==(HANDLE)-1) {
+ PyErr_SetFromErrno(mmap_module_error);
+ return NULL;
+ }
+ /* Win9x appears to need us seeked to zero */
+ lseek(fileno, 0, SEEK_SET);
+ }
+
+ m_obj = (mmap_object *)type->tp_alloc(type, 0);
+ if (m_obj == NULL)
+ return NULL;
+ /* Set every field to an invalid marker, so we can safely
+ destruct the object in the face of failure */
+ m_obj->data = NULL;
+ m_obj->file_handle = INVALID_HANDLE_VALUE;
+ m_obj->map_handle = NULL;
+ m_obj->tagname = NULL;
+ m_obj->offset = offset;
+
+ if (fh) {
+ /* It is necessary to duplicate the handle, so the
+ Python code can close it on us */
+ if (!DuplicateHandle(
+ GetCurrentProcess(), /* source process handle */
+ fh, /* handle to be duplicated */
+ GetCurrentProcess(), /* target proc handle */
+ (LPHANDLE)&m_obj->file_handle, /* result */
+ 0, /* access - ignored due to options value */
+ FALSE, /* inherited by child processes? */
+ DUPLICATE_SAME_ACCESS)) { /* options */
+ dwErr = GetLastError();
+ Py_DECREF(m_obj);
+ PyErr_SetFromWindowsErr(dwErr);
+ return NULL;
+ }
+ if (!map_size) {
+ DWORD low,high;
+ low = GetFileSize(fh, &high);
+ /* low might just happen to have the value INVALID_FILE_SIZE;
+ so we need to check the last error also. */
+ if (low == INVALID_FILE_SIZE &&
+ (dwErr = GetLastError()) != NO_ERROR) {
+ Py_DECREF(m_obj);
+ return PyErr_SetFromWindowsErr(dwErr);
+ }
#if SIZEOF_SIZE_T > 4
- m_obj->size = (((size_t)high)<<32) + low;
+ m_obj->size = (((size_t)high)<<32) + low;
#else
- if (high)
- /* File is too large to map completely */
- m_obj->size = (size_t)-1;
- else
- m_obj->size = low;
+ if (high)
+ /* File is too large to map completely */
+ m_obj->size = (size_t)-1;
+ else
+ m_obj->size = low;
#endif
- } else {
- m_obj->size = map_size;
- }
- }
- else {
- m_obj->size = map_size;
- }
-
- /* set the initial position */
- m_obj->pos = (size_t) 0;
-
- m_obj->exports = 0;
- /* set the tag name */
- if (tagname != NULL && *tagname != '\0') {
- m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
- if (m_obj->tagname == NULL) {
- PyErr_NoMemory();
- Py_DECREF(m_obj);
- return NULL;
- }
- strcpy(m_obj->tagname, tagname);
- }
- else
- m_obj->tagname = NULL;
-
- m_obj->access = (access_mode)access;
- /* DWORD is a 4-byte int. If we're on a box where size_t consumes
- * more than 4 bytes, we need to break it apart. Else (size_t
- * consumes 4 bytes), C doesn't define what happens if we shift
- * right by 32, so we need different code.
- */
+ } else {
+ m_obj->size = map_size;
+ }
+ }
+ else {
+ m_obj->size = map_size;
+ }
+
+ /* set the initial position */
+ m_obj->pos = (size_t) 0;
+
+ m_obj->exports = 0;
+ /* set the tag name */
+ if (tagname != NULL && *tagname != '\0') {
+ m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
+ if (m_obj->tagname == NULL) {
+ PyErr_NoMemory();
+ Py_DECREF(m_obj);
+ return NULL;
+ }
+ strcpy(m_obj->tagname, tagname);
+ }
+ else
+ m_obj->tagname = NULL;
+
+ m_obj->access = (access_mode)access;
+ /* DWORD is a 4-byte int. If we're on a box where size_t consumes
+ * more than 4 bytes, we need to break it apart. Else (size_t
+ * consumes 4 bytes), C doesn't define what happens if we shift
+ * right by 32, so we need different code.
+ */
#if SIZEOF_SIZE_T > 4
- size_hi = (DWORD)((offset + m_obj->size) >> 32);
- size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
- off_hi = (DWORD)(offset >> 32);
- off_lo = (DWORD)(offset & 0xFFFFFFFF);
+ size_hi = (DWORD)((offset + m_obj->size) >> 32);
+ size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
+ off_hi = (DWORD)(offset >> 32);
+ off_lo = (DWORD)(offset & 0xFFFFFFFF);
#else
- size_hi = 0;
- size_lo = (DWORD)(offset + m_obj->size);
- off_hi = 0;
- off_lo = (DWORD)offset;
+ size_hi = 0;
+ size_lo = (DWORD)(offset + m_obj->size);
+ off_hi = 0;
+ off_lo = (DWORD)offset;
#endif
- /* For files, it would be sufficient to pass 0 as size.
- For anonymous maps, we have to pass the size explicitly. */
- m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
- NULL,
- flProtect,
- size_hi,
- size_lo,
- m_obj->tagname);
- if (m_obj->map_handle != NULL) {
- m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
- dwDesiredAccess,
- off_hi,
- off_lo,
- m_obj->size);
- if (m_obj->data != NULL)
- return (PyObject *)m_obj;
- else {
- dwErr = GetLastError();
- CloseHandle(m_obj->map_handle);
- m_obj->map_handle = NULL;
- }
- } else
- dwErr = GetLastError();
- Py_DECREF(m_obj);
- PyErr_SetFromWindowsErr(dwErr);
- return NULL;
+ /* For files, it would be sufficient to pass 0 as size.
+ For anonymous maps, we have to pass the size explicitly. */
+ m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
+ NULL,
+ flProtect,
+ size_hi,
+ size_lo,
+ m_obj->tagname);
+ if (m_obj->map_handle != NULL) {
+ m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
+ dwDesiredAccess,
+ off_hi,
+ off_lo,
+ m_obj->size);
+ if (m_obj->data != NULL)
+ return (PyObject *)m_obj;
+ else {
+ dwErr = GetLastError();
+ CloseHandle(m_obj->map_handle);
+ m_obj->map_handle = NULL;
+ }
+ } else
+ dwErr = GetLastError();
+ Py_DECREF(m_obj);
+ PyErr_SetFromWindowsErr(dwErr);
+ return NULL;
}
#endif /* MS_WINDOWS */
static void
setint(PyObject *d, const char *name, long value)
{
- PyObject *o = PyLong_FromLong(value);
- if (o && PyDict_SetItemString(d, name, o) == 0) {
- Py_DECREF(o);
- }
+ PyObject *o = PyLong_FromLong(value);
+ if (o && PyDict_SetItemString(d, name, o) == 0) {
+ Py_DECREF(o);
+ }
}
static struct PyModuleDef mmapmodule = {
- PyModuleDef_HEAD_INIT,
- "mmap",
- NULL,
- -1,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "mmap",
+ NULL,
+ -1,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_mmap(void)
{
- PyObject *dict, *module;
-
- if (PyType_Ready(&mmap_object_type) < 0)
- return NULL;
-
- module = PyModule_Create(&mmapmodule);
- if (module == NULL)
- return NULL;
- dict = PyModule_GetDict(module);
- if (!dict)
- return NULL;
- mmap_module_error = PyErr_NewException("mmap.error",
- PyExc_EnvironmentError , NULL);
- if (mmap_module_error == NULL)
- return NULL;
- PyDict_SetItemString(dict, "error", mmap_module_error);
- PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type);
+ PyObject *dict, *module;
+
+ if (PyType_Ready(&mmap_object_type) < 0)
+ return NULL;
+
+ module = PyModule_Create(&mmapmodule);
+ if (module == NULL)
+ return NULL;
+ dict = PyModule_GetDict(module);
+ if (!dict)
+ return NULL;
+ mmap_module_error = PyErr_NewException("mmap.error",
+ PyExc_EnvironmentError , NULL);
+ if (mmap_module_error == NULL)
+ return NULL;
+ PyDict_SetItemString(dict, "error", mmap_module_error);
+ PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type);
#ifdef PROT_EXEC
- setint(dict, "PROT_EXEC", PROT_EXEC);
+ setint(dict, "PROT_EXEC", PROT_EXEC);
#endif
#ifdef PROT_READ
- setint(dict, "PROT_READ", PROT_READ);
+ setint(dict, "PROT_READ", PROT_READ);
#endif
#ifdef PROT_WRITE
- setint(dict, "PROT_WRITE", PROT_WRITE);
+ setint(dict, "PROT_WRITE", PROT_WRITE);
#endif
#ifdef MAP_SHARED
- setint(dict, "MAP_SHARED", MAP_SHARED);
+ setint(dict, "MAP_SHARED", MAP_SHARED);
#endif
#ifdef MAP_PRIVATE
- setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
+ setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
#endif
#ifdef MAP_DENYWRITE
- setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
+ setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
#endif
#ifdef MAP_EXECUTABLE
- setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
+ setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
#endif
#ifdef MAP_ANONYMOUS
- setint(dict, "MAP_ANON", MAP_ANONYMOUS);
- setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
+ setint(dict, "MAP_ANON", MAP_ANONYMOUS);
+ setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
#endif
- setint(dict, "PAGESIZE", (long)my_getpagesize());
+ setint(dict, "PAGESIZE", (long)my_getpagesize());
- setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
+ setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
- setint(dict, "ACCESS_READ", ACCESS_READ);
- setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
- setint(dict, "ACCESS_COPY", ACCESS_COPY);
- return module;
+ setint(dict, "ACCESS_READ", ACCESS_READ);
+ setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
+ setint(dict, "ACCESS_COPY", ACCESS_COPY);
+ return module;
}
diff --git a/Modules/nismodule.c b/Modules/nismodule.c
index 4be4657..16c7b81 100644
--- a/Modules/nismodule.c
+++ b/Modules/nismodule.c
@@ -1,11 +1,11 @@
/***********************************************************
Written by:
- Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
- B&O group,
- Faculteit der Informatica,
- Universiteit Twente,
- Enschede,
- the Netherlands.
+ Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
+ B&O group,
+ Faculteit der Informatica,
+ Universiteit Twente,
+ Enschede,
+ the Netherlands.
******************************************************************/
/* NIS module implementation */
@@ -23,7 +23,7 @@
extern int yp_get_default_domain(char **);
#endif
-PyDoc_STRVAR(get_default_domain__doc__,
+PyDoc_STRVAR(get_default_domain__doc__,
"get_default_domain() -> str\n\
Corresponds to the C library yp_get_default_domain() call, returning\n\
the default NIS domain.\n");
@@ -49,44 +49,44 @@ static PyObject *NisError;
static PyObject *
nis_error (int err)
{
- PyErr_SetString(NisError, yperr_string(err));
- return NULL;
+ PyErr_SetString(NisError, yperr_string(err));
+ return NULL;
}
static struct nis_map {
- char *alias;
- char *map;
- int fix;
+ char *alias;
+ char *map;
+ int fix;
} aliases [] = {
- {"passwd", "passwd.byname", 0},
- {"group", "group.byname", 0},
- {"networks", "networks.byaddr", 0},
- {"hosts", "hosts.byname", 0},
- {"protocols", "protocols.bynumber", 0},
- {"services", "services.byname", 0},
- {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */
- {"ethers", "ethers.byname", 0},
- {0L, 0L, 0}
+ {"passwd", "passwd.byname", 0},
+ {"group", "group.byname", 0},
+ {"networks", "networks.byaddr", 0},
+ {"hosts", "hosts.byname", 0},
+ {"protocols", "protocols.bynumber", 0},
+ {"services", "services.byname", 0},
+ {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */
+ {"ethers", "ethers.byname", 0},
+ {0L, 0L, 0}
};
static char *
nis_mapname (char *map, int *pfix)
{
- int i;
-
- *pfix = 0;
- for (i=0; aliases[i].alias != 0L; i++) {
- if (!strcmp (aliases[i].alias, map)) {
- *pfix = aliases[i].fix;
- return aliases[i].map;
- }
- if (!strcmp (aliases[i].map, map)) {
- *pfix = aliases[i].fix;
- return aliases[i].map;
- }
- }
-
- return map;
+ int i;
+
+ *pfix = 0;
+ for (i=0; aliases[i].alias != 0L; i++) {
+ if (!strcmp (aliases[i].alias, map)) {
+ *pfix = aliases[i].fix;
+ return aliases[i].map;
+ }
+ if (!strcmp (aliases[i].map, map)) {
+ *pfix = aliases[i].fix;
+ return aliases[i].map;
+ }
+ }
+
+ return map;
}
#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
@@ -96,168 +96,168 @@ typedef int (*foreachfunc)(int, char *, int, char *, int, char *);
#endif
struct ypcallback_data {
- PyObject *dict;
- int fix;
- PyThreadState *state;
+ PyObject *dict;
+ int fix;
+ PyThreadState *state;
};
static int
nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
int invallen, struct ypcallback_data *indata)
{
- if (instatus == YP_TRUE) {
- PyObject *key;
- PyObject *val;
- int err;
-
- PyEval_RestoreThread(indata->state);
- if (indata->fix) {
- if (inkeylen > 0 && inkey[inkeylen-1] == '\0')
- inkeylen--;
- if (invallen > 0 && inval[invallen-1] == '\0')
- invallen--;
- }
- key = PyUnicode_FromStringAndSize(inkey, inkeylen);
- val = PyUnicode_FromStringAndSize(inval, invallen);
- if (key == NULL || val == NULL) {
- /* XXX error -- don't know how to handle */
- PyErr_Clear();
- Py_XDECREF(key);
- Py_XDECREF(val);
- return 1;
- }
- err = PyDict_SetItem(indata->dict, key, val);
- Py_DECREF(key);
- Py_DECREF(val);
- if (err != 0)
- PyErr_Clear();
- indata->state = PyEval_SaveThread();
- if (err != 0)
- return 1;
- return 0;
- }
- return 1;
+ if (instatus == YP_TRUE) {
+ PyObject *key;
+ PyObject *val;
+ int err;
+
+ PyEval_RestoreThread(indata->state);
+ if (indata->fix) {
+ if (inkeylen > 0 && inkey[inkeylen-1] == '\0')
+ inkeylen--;
+ if (invallen > 0 && inval[invallen-1] == '\0')
+ invallen--;
+ }
+ key = PyUnicode_FromStringAndSize(inkey, inkeylen);
+ val = PyUnicode_FromStringAndSize(inval, invallen);
+ if (key == NULL || val == NULL) {
+ /* XXX error -- don't know how to handle */
+ PyErr_Clear();
+ Py_XDECREF(key);
+ Py_XDECREF(val);
+ return 1;
+ }
+ err = PyDict_SetItem(indata->dict, key, val);
+ Py_DECREF(key);
+ Py_DECREF(val);
+ if (err != 0)
+ PyErr_Clear();
+ indata->state = PyEval_SaveThread();
+ if (err != 0)
+ return 1;
+ return 0;
+ }
+ return 1;
}
static PyObject *
nis_get_default_domain (PyObject *self)
{
- char *domain;
- int err;
- PyObject *res;
+ char *domain;
+ int err;
+ PyObject *res;
- if ((err = yp_get_default_domain(&domain)) != 0)
- return nis_error(err);
+ if ((err = yp_get_default_domain(&domain)) != 0)
+ return nis_error(err);
- res = PyUnicode_FromStringAndSize (domain, strlen(domain));
- return res;
+ res = PyUnicode_FromStringAndSize (domain, strlen(domain));
+ return res;
}
static PyObject *
nis_match (PyObject *self, PyObject *args, PyObject *kwdict)
{
- char *match;
- char *domain = NULL;
- int keylen, len;
- char *key, *map;
- int err;
- PyObject *res;
- int fix;
- static char *kwlist[] = {"key", "map", "domain", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict,
- "s#s|s:match", kwlist,
- &key, &keylen, &map, &domain))
- return NULL;
- if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
- return nis_error(err);
- map = nis_mapname (map, &fix);
- if (fix)
- keylen++;
- Py_BEGIN_ALLOW_THREADS
- err = yp_match (domain, map, key, keylen, &match, &len);
- Py_END_ALLOW_THREADS
- if (fix)
- len--;
- if (err != 0)
- return nis_error(err);
- res = PyUnicode_FromStringAndSize (match, len);
- free (match);
- return res;
+ char *match;
+ char *domain = NULL;
+ int keylen, len;
+ char *key, *map;
+ int err;
+ PyObject *res;
+ int fix;
+ static char *kwlist[] = {"key", "map", "domain", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict,
+ "s#s|s:match", kwlist,
+ &key, &keylen, &map, &domain))
+ return NULL;
+ if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
+ return nis_error(err);
+ map = nis_mapname (map, &fix);
+ if (fix)
+ keylen++;
+ Py_BEGIN_ALLOW_THREADS
+ err = yp_match (domain, map, key, keylen, &match, &len);
+ Py_END_ALLOW_THREADS
+ if (fix)
+ len--;
+ if (err != 0)
+ return nis_error(err);
+ res = PyUnicode_FromStringAndSize (match, len);
+ free (match);
+ return res;
}
static PyObject *
nis_cat (PyObject *self, PyObject *args, PyObject *kwdict)
{
- char *domain = NULL;
- char *map;
- struct ypall_callback cb;
- struct ypcallback_data data;
- PyObject *dict;
- int err;
- static char *kwlist[] = {"map", "domain", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat",
- kwlist, &map, &domain))
- return NULL;
- if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
- return nis_error(err);
- dict = PyDict_New ();
- if (dict == NULL)
- return NULL;
- cb.foreach = (foreachfunc)nis_foreach;
- data.dict = dict;
- map = nis_mapname (map, &data.fix);
- cb.data = (char *)&data;
- data.state = PyEval_SaveThread();
- err = yp_all (domain, map, &cb);
- PyEval_RestoreThread(data.state);
- if (err != 0) {
- Py_DECREF(dict);
- return nis_error(err);
- }
- return dict;
+ char *domain = NULL;
+ char *map;
+ struct ypall_callback cb;
+ struct ypcallback_data data;
+ PyObject *dict;
+ int err;
+ static char *kwlist[] = {"map", "domain", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat",
+ kwlist, &map, &domain))
+ return NULL;
+ if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
+ return nis_error(err);
+ dict = PyDict_New ();
+ if (dict == NULL)
+ return NULL;
+ cb.foreach = (foreachfunc)nis_foreach;
+ data.dict = dict;
+ map = nis_mapname (map, &data.fix);
+ cb.data = (char *)&data;
+ data.state = PyEval_SaveThread();
+ err = yp_all (domain, map, &cb);
+ PyEval_RestoreThread(data.state);
+ if (err != 0) {
+ Py_DECREF(dict);
+ return nis_error(err);
+ }
+ return dict;
}
/* These should be u_long on Sun h/w but not on 64-bit h/w.
This is not portable to machines with 16-bit ints and no prototypes */
#ifndef YPPROC_MAPLIST
-#define YPPROC_MAPLIST 11
+#define YPPROC_MAPLIST 11
#endif
#ifndef YPPROG
-#define YPPROG 100004
+#define YPPROG 100004
#endif
#ifndef YPVERS
-#define YPVERS 2
+#define YPVERS 2
#endif
typedef char *domainname;
typedef char *mapname;
enum nisstat {
- NIS_TRUE = 1,
- NIS_NOMORE = 2,
- NIS_FALSE = 0,
- NIS_NOMAP = -1,
- NIS_NODOM = -2,
- NIS_NOKEY = -3,
- NIS_BADOP = -4,
- NIS_BADDB = -5,
- NIS_YPERR = -6,
- NIS_BADARGS = -7,
- NIS_VERS = -8
+ NIS_TRUE = 1,
+ NIS_NOMORE = 2,
+ NIS_FALSE = 0,
+ NIS_NOMAP = -1,
+ NIS_NODOM = -2,
+ NIS_NOKEY = -3,
+ NIS_BADOP = -4,
+ NIS_BADDB = -5,
+ NIS_YPERR = -6,
+ NIS_BADARGS = -7,
+ NIS_VERS = -8
};
typedef enum nisstat nisstat;
struct nismaplist {
- mapname map;
- struct nismaplist *next;
+ mapname map;
+ struct nismaplist *next;
};
typedef struct nismaplist nismaplist;
struct nisresp_maplist {
- nisstat stat;
- nismaplist *maps;
+ nisstat stat;
+ nismaplist *maps;
};
typedef struct nisresp_maplist nisresp_maplist;
@@ -267,45 +267,45 @@ static
bool_t
nis_xdr_domainname(XDR *xdrs, domainname *objp)
{
- if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
- return (FALSE);
- }
- return (TRUE);
+ if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
+ return (FALSE);
+ }
+ return (TRUE);
}
static
bool_t
nis_xdr_mapname(XDR *xdrs, mapname *objp)
{
- if (!xdr_string(xdrs, objp, YPMAXMAP)) {
- return (FALSE);
- }
- return (TRUE);
+ if (!xdr_string(xdrs, objp, YPMAXMAP)) {
+ return (FALSE);
+ }
+ return (TRUE);
}
static
bool_t
nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp)
{
- if (!nis_xdr_mapname(xdrs, &objp->map)) {
- return (FALSE);
- }
- if (!xdr_pointer(xdrs, (char **)&objp->next,
- sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
- {
- return (FALSE);
- }
- return (TRUE);
+ if (!nis_xdr_mapname(xdrs, &objp->map)) {
+ return (FALSE);
+ }
+ if (!xdr_pointer(xdrs, (char **)&objp->next,
+ sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
+ {
+ return (FALSE);
+ }
+ return (TRUE);
}
static
bool_t
nis_xdr_ypstat(XDR *xdrs, nisstat *objp)
{
- if (!xdr_enum(xdrs, (enum_t *)objp)) {
- return (FALSE);
- }
- return (TRUE);
+ if (!xdr_enum(xdrs, (enum_t *)objp)) {
+ return (FALSE);
+ }
+ return (TRUE);
}
@@ -313,15 +313,15 @@ static
bool_t
nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp)
{
- if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
- return (FALSE);
- }
- if (!xdr_pointer(xdrs, (char **)&objp->maps,
- sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
- {
- return (FALSE);
- }
- return (TRUE);
+ if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
+ return (FALSE);
+ }
+ if (!xdr_pointer(xdrs, (char **)&objp->maps,
+ sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
+ {
+ return (FALSE);
+ }
+ return (TRUE);
}
@@ -329,132 +329,132 @@ static
nisresp_maplist *
nisproc_maplist_2(domainname *argp, CLIENT *clnt)
{
- static nisresp_maplist res;
-
- memset(&res, 0, sizeof(res));
- if (clnt_call(clnt, YPPROC_MAPLIST,
- (xdrproc_t)nis_xdr_domainname, (caddr_t)argp,
- (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res,
- TIMEOUT) != RPC_SUCCESS)
- {
- return (NULL);
- }
- return (&res);
+ static nisresp_maplist res;
+
+ memset(&res, 0, sizeof(res));
+ if (clnt_call(clnt, YPPROC_MAPLIST,
+ (xdrproc_t)nis_xdr_domainname, (caddr_t)argp,
+ (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res,
+ TIMEOUT) != RPC_SUCCESS)
+ {
+ return (NULL);
+ }
+ return (&res);
}
static
nismaplist *
nis_maplist (char *dom)
{
- nisresp_maplist *list;
- CLIENT *cl;
- char *server = NULL;
- int mapi = 0;
-
- while (!server && aliases[mapi].map != 0L) {
- yp_master (dom, aliases[mapi].map, &server);
- mapi++;
- }
- if (!server) {
- PyErr_SetString(NisError, "No NIS master found for any map");
- return NULL;
- }
- cl = clnt_create(server, YPPROG, YPVERS, "tcp");
- if (cl == NULL) {
- PyErr_SetString(NisError, clnt_spcreateerror(server));
- goto finally;
- }
- list = nisproc_maplist_2 (&dom, cl);
- clnt_destroy(cl);
- if (list == NULL)
- goto finally;
- if (list->stat != NIS_TRUE)
- goto finally;
-
- free(server);
- return list->maps;
+ nisresp_maplist *list;
+ CLIENT *cl;
+ char *server = NULL;
+ int mapi = 0;
+
+ while (!server && aliases[mapi].map != 0L) {
+ yp_master (dom, aliases[mapi].map, &server);
+ mapi++;
+ }
+ if (!server) {
+ PyErr_SetString(NisError, "No NIS master found for any map");
+ return NULL;
+ }
+ cl = clnt_create(server, YPPROG, YPVERS, "tcp");
+ if (cl == NULL) {
+ PyErr_SetString(NisError, clnt_spcreateerror(server));
+ goto finally;
+ }
+ list = nisproc_maplist_2 (&dom, cl);
+ clnt_destroy(cl);
+ if (list == NULL)
+ goto finally;
+ if (list->stat != NIS_TRUE)
+ goto finally;
+
+ free(server);
+ return list->maps;
finally:
- free(server);
- return NULL;
+ free(server);
+ return NULL;
}
static PyObject *
nis_maps (PyObject *self, PyObject *args, PyObject *kwdict)
{
- char *domain = NULL;
- nismaplist *maps;
- PyObject *list;
- int err;
- static char *kwlist[] = {"domain", NULL};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwdict,
- "|s:maps", kwlist, &domain))
- return NULL;
- if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) {
- nis_error(err);
- return NULL;
- }
-
- if ((maps = nis_maplist (domain)) == NULL)
- return NULL;
- if ((list = PyList_New(0)) == NULL)
- return NULL;
- for (maps = maps; maps; maps = maps->next) {
- PyObject *str = PyUnicode_FromString(maps->map);
- if (!str || PyList_Append(list, str) < 0)
- {
- Py_DECREF(list);
- list = NULL;
- break;
- }
- Py_DECREF(str);
- }
- /* XXX Shouldn't we free the list of maps now? */
- return list;
+ char *domain = NULL;
+ nismaplist *maps;
+ PyObject *list;
+ int err;
+ static char *kwlist[] = {"domain", NULL};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict,
+ "|s:maps", kwlist, &domain))
+ return NULL;
+ if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) {
+ nis_error(err);
+ return NULL;
+ }
+
+ if ((maps = nis_maplist (domain)) == NULL)
+ return NULL;
+ if ((list = PyList_New(0)) == NULL)
+ return NULL;
+ for (maps = maps; maps; maps = maps->next) {
+ PyObject *str = PyUnicode_FromString(maps->map);
+ if (!str || PyList_Append(list, str) < 0)
+ {
+ Py_DECREF(list);
+ list = NULL;
+ break;
+ }
+ Py_DECREF(str);
+ }
+ /* XXX Shouldn't we free the list of maps now? */
+ return list;
}
static PyMethodDef nis_methods[] = {
- {"match", (PyCFunction)nis_match,
- METH_VARARGS | METH_KEYWORDS,
- match__doc__},
- {"cat", (PyCFunction)nis_cat,
- METH_VARARGS | METH_KEYWORDS,
- cat__doc__},
- {"maps", (PyCFunction)nis_maps,
- METH_VARARGS | METH_KEYWORDS,
- maps__doc__},
- {"get_default_domain", (PyCFunction)nis_get_default_domain,
- METH_NOARGS,
- get_default_domain__doc__},
- {NULL, NULL} /* Sentinel */
+ {"match", (PyCFunction)nis_match,
+ METH_VARARGS | METH_KEYWORDS,
+ match__doc__},
+ {"cat", (PyCFunction)nis_cat,
+ METH_VARARGS | METH_KEYWORDS,
+ cat__doc__},
+ {"maps", (PyCFunction)nis_maps,
+ METH_VARARGS | METH_KEYWORDS,
+ maps__doc__},
+ {"get_default_domain", (PyCFunction)nis_get_default_domain,
+ METH_NOARGS,
+ get_default_domain__doc__},
+ {NULL, NULL} /* Sentinel */
};
PyDoc_STRVAR(nis__doc__,
"This module contains functions for accessing NIS maps.\n");
static struct PyModuleDef nismodule = {
- PyModuleDef_HEAD_INIT,
- "nis",
- nis__doc__,
- -1,
- nis_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "nis",
+ nis__doc__,
+ -1,
+ nis_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyObject*
PyInit_nis (void)
{
- PyObject *m, *d;
- m = PyModule_Create(&nismodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
- NisError = PyErr_NewException("nis.error", NULL, NULL);
- if (NisError != NULL)
- PyDict_SetItemString(d, "error", NisError);
- return m;
+ PyObject *m, *d;
+ m = PyModule_Create(&nismodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+ NisError = PyErr_NewException("nis.error", NULL, NULL);
+ if (NisError != NULL)
+ PyDict_SetItemString(d, "error", NisError);
+ return m;
}
diff --git a/Modules/operator.c b/Modules/operator.c
index 85d662d..8ca4d2d 100644
--- a/Modules/operator.c
+++ b/Modules/operator.c
@@ -112,47 +112,47 @@ spamrc(op_ge , Py_GE)
static PyObject*
op_pow(PyObject *s, PyObject *a)
{
- PyObject *a1, *a2;
- if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
- return PyNumber_Power(a1, a2, Py_None);
- return NULL;
+ PyObject *a1, *a2;
+ if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
+ return PyNumber_Power(a1, a2, Py_None);
+ return NULL;
}
static PyObject*
op_ipow(PyObject *s, PyObject *a)
{
- PyObject *a1, *a2;
- if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
- return PyNumber_InPlacePower(a1, a2, Py_None);
- return NULL;
+ PyObject *a1, *a2;
+ if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
+ return PyNumber_InPlacePower(a1, a2, Py_None);
+ return NULL;
}
static PyObject *
op_index(PyObject *s, PyObject *a)
{
- return PyNumber_Index(a);
+ return PyNumber_Index(a);
}
static PyObject*
is_(PyObject *s, PyObject *a)
{
- PyObject *a1, *a2, *result = NULL;
- if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
- result = (a1 == a2) ? Py_True : Py_False;
- Py_INCREF(result);
- }
- return result;
+ PyObject *a1, *a2, *result = NULL;
+ if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
+ result = (a1 == a2) ? Py_True : Py_False;
+ Py_INCREF(result);
+ }
+ return result;
}
static PyObject*
is_not(PyObject *s, PyObject *a)
{
- PyObject *a1, *a2, *result = NULL;
- if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
- result = (a1 != a2) ? Py_True : Py_False;
- Py_INCREF(result);
- }
- return result;
+ PyObject *a1, *a2, *result = NULL;
+ if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
+ result = (a1 != a2) ? Py_True : Py_False;
+ Py_INCREF(result);
+ }
+ return result;
}
#undef spam1
@@ -161,10 +161,10 @@ is_not(PyObject *s, PyObject *a)
#undef spam1o
#define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
#define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
- {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
+ {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
#define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
#define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
- {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
+ {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
static struct PyMethodDef operator_methods[] = {
@@ -227,16 +227,16 @@ spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
/* itemgetter object **********************************************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t nitems;
- PyObject *item;
+ PyObject_HEAD
+ Py_ssize_t nitems;
+ PyObject *item;
} itemgetterobject;
static PyTypeObject itemgetter_type;
@@ -244,77 +244,77 @@ static PyTypeObject itemgetter_type;
static PyObject *
itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- itemgetterobject *ig;
- PyObject *item;
- Py_ssize_t nitems;
-
- if (!_PyArg_NoKeywords("itemgetter()", kwds))
- return NULL;
-
- nitems = PyTuple_GET_SIZE(args);
- if (nitems <= 1) {
- if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
- return NULL;
- } else
- item = args;
-
- /* create itemgetterobject structure */
- ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
- if (ig == NULL)
- return NULL;
-
- Py_INCREF(item);
- ig->item = item;
- ig->nitems = nitems;
-
- PyObject_GC_Track(ig);
- return (PyObject *)ig;
+ itemgetterobject *ig;
+ PyObject *item;
+ Py_ssize_t nitems;
+
+ if (!_PyArg_NoKeywords("itemgetter()", kwds))
+ return NULL;
+
+ nitems = PyTuple_GET_SIZE(args);
+ if (nitems <= 1) {
+ if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
+ return NULL;
+ } else
+ item = args;
+
+ /* create itemgetterobject structure */
+ ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
+ if (ig == NULL)
+ return NULL;
+
+ Py_INCREF(item);
+ ig->item = item;
+ ig->nitems = nitems;
+
+ PyObject_GC_Track(ig);
+ return (PyObject *)ig;
}
static void
itemgetter_dealloc(itemgetterobject *ig)
{
- PyObject_GC_UnTrack(ig);
- Py_XDECREF(ig->item);
- PyObject_GC_Del(ig);
+ PyObject_GC_UnTrack(ig);
+ Py_XDECREF(ig->item);
+ PyObject_GC_Del(ig);
}
static int
itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
{
- Py_VISIT(ig->item);
- return 0;
+ Py_VISIT(ig->item);
+ return 0;
}
static PyObject *
itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
{
- PyObject *obj, *result;
- Py_ssize_t i, nitems=ig->nitems;
-
- if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
- return NULL;
- if (nitems == 1)
- return PyObject_GetItem(obj, ig->item);
-
- assert(PyTuple_Check(ig->item));
- assert(PyTuple_GET_SIZE(ig->item) == nitems);
-
- result = PyTuple_New(nitems);
- if (result == NULL)
- return NULL;
-
- for (i=0 ; i < nitems ; i++) {
- PyObject *item, *val;
- item = PyTuple_GET_ITEM(ig->item, i);
- val = PyObject_GetItem(obj, item);
- if (val == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SET_ITEM(result, i, val);
- }
- return result;
+ PyObject *obj, *result;
+ Py_ssize_t i, nitems=ig->nitems;
+
+ if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
+ return NULL;
+ if (nitems == 1)
+ return PyObject_GetItem(obj, ig->item);
+
+ assert(PyTuple_Check(ig->item));
+ assert(PyTuple_GET_SIZE(ig->item) == nitems);
+
+ result = PyTuple_New(nitems);
+ if (result == NULL)
+ return NULL;
+
+ for (i=0 ; i < nitems ; i++) {
+ PyObject *item, *val;
+ item = PyTuple_GET_ITEM(ig->item, i);
+ val = PyObject_GetItem(obj, item);
+ if (val == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(result, i, val);
+ }
+ return result;
}
PyDoc_STRVAR(itemgetter_doc,
@@ -325,55 +325,55 @@ After, f=itemgetter(2), the call f(r) returns r[2].\n\
After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
static PyTypeObject itemgetter_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "operator.itemgetter", /* tp_name */
- sizeof(itemgetterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)itemgetter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)itemgetter_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- itemgetter_doc, /* tp_doc */
- (traverseproc)itemgetter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- itemgetter_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "operator.itemgetter", /* tp_name */
+ sizeof(itemgetterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)itemgetter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)itemgetter_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ itemgetter_doc, /* tp_doc */
+ (traverseproc)itemgetter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ itemgetter_new, /* tp_new */
+ 0, /* tp_free */
};
/* attrgetter object **********************************************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t nattrs;
- PyObject *attr;
+ PyObject_HEAD
+ Py_ssize_t nattrs;
+ PyObject *attr;
} attrgetterobject;
static PyTypeObject attrgetter_type;
@@ -381,112 +381,112 @@ static PyTypeObject attrgetter_type;
static PyObject *
attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- attrgetterobject *ag;
- PyObject *attr;
- Py_ssize_t nattrs;
-
- if (!_PyArg_NoKeywords("attrgetter()", kwds))
- return NULL;
-
- nattrs = PyTuple_GET_SIZE(args);
- if (nattrs <= 1) {
- if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
- return NULL;
- } else
- attr = args;
-
- /* create attrgetterobject structure */
- ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
- if (ag == NULL)
- return NULL;
-
- Py_INCREF(attr);
- ag->attr = attr;
- ag->nattrs = nattrs;
-
- PyObject_GC_Track(ag);
- return (PyObject *)ag;
+ attrgetterobject *ag;
+ PyObject *attr;
+ Py_ssize_t nattrs;
+
+ if (!_PyArg_NoKeywords("attrgetter()", kwds))
+ return NULL;
+
+ nattrs = PyTuple_GET_SIZE(args);
+ if (nattrs <= 1) {
+ if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
+ return NULL;
+ } else
+ attr = args;
+
+ /* create attrgetterobject structure */
+ ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
+ if (ag == NULL)
+ return NULL;
+
+ Py_INCREF(attr);
+ ag->attr = attr;
+ ag->nattrs = nattrs;
+
+ PyObject_GC_Track(ag);
+ return (PyObject *)ag;
}
static void
attrgetter_dealloc(attrgetterobject *ag)
{
- PyObject_GC_UnTrack(ag);
- Py_XDECREF(ag->attr);
- PyObject_GC_Del(ag);
+ PyObject_GC_UnTrack(ag);
+ Py_XDECREF(ag->attr);
+ PyObject_GC_Del(ag);
}
static int
attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
{
- Py_VISIT(ag->attr);
- return 0;
+ Py_VISIT(ag->attr);
+ return 0;
}
static PyObject *
dotted_getattr(PyObject *obj, PyObject *attr)
{
- char *s, *p;
-
- if (!PyUnicode_Check(attr)) {
- PyErr_SetString(PyExc_TypeError,
- "attribute name must be a string");
- return NULL;
- }
-
- s = _PyUnicode_AsString(attr);
- Py_INCREF(obj);
- for (;;) {
- PyObject *newobj, *str;
- p = strchr(s, '.');
- str = p ? PyUnicode_FromStringAndSize(s, (p-s)) :
- PyUnicode_FromString(s);
- if (str == NULL) {
- Py_DECREF(obj);
- return NULL;
- }
- newobj = PyObject_GetAttr(obj, str);
- Py_DECREF(str);
- Py_DECREF(obj);
- if (newobj == NULL)
- return NULL;
- obj = newobj;
- if (p == NULL) break;
- s = p+1;
- }
-
- return obj;
+ char *s, *p;
+
+ if (!PyUnicode_Check(attr)) {
+ PyErr_SetString(PyExc_TypeError,
+ "attribute name must be a string");
+ return NULL;
+ }
+
+ s = _PyUnicode_AsString(attr);
+ Py_INCREF(obj);
+ for (;;) {
+ PyObject *newobj, *str;
+ p = strchr(s, '.');
+ str = p ? PyUnicode_FromStringAndSize(s, (p-s)) :
+ PyUnicode_FromString(s);
+ if (str == NULL) {
+ Py_DECREF(obj);
+ return NULL;
+ }
+ newobj = PyObject_GetAttr(obj, str);
+ Py_DECREF(str);
+ Py_DECREF(obj);
+ if (newobj == NULL)
+ return NULL;
+ obj = newobj;
+ if (p == NULL) break;
+ s = p+1;
+ }
+
+ return obj;
}
static PyObject *
attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
{
- PyObject *obj, *result;
- Py_ssize_t i, nattrs=ag->nattrs;
-
- if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
- return NULL;
- if (ag->nattrs == 1)
- return dotted_getattr(obj, ag->attr);
-
- assert(PyTuple_Check(ag->attr));
- assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
-
- result = PyTuple_New(nattrs);
- if (result == NULL)
- return NULL;
-
- for (i=0 ; i < nattrs ; i++) {
- PyObject *attr, *val;
- attr = PyTuple_GET_ITEM(ag->attr, i);
- val = dotted_getattr(obj, attr);
- if (val == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SET_ITEM(result, i, val);
- }
- return result;
+ PyObject *obj, *result;
+ Py_ssize_t i, nattrs=ag->nattrs;
+
+ if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
+ return NULL;
+ if (ag->nattrs == 1)
+ return dotted_getattr(obj, ag->attr);
+
+ assert(PyTuple_Check(ag->attr));
+ assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
+
+ result = PyTuple_New(nattrs);
+ if (result == NULL)
+ return NULL;
+
+ for (i=0 ; i < nattrs ; i++) {
+ PyObject *attr, *val;
+ attr = PyTuple_GET_ITEM(ag->attr, i);
+ val = dotted_getattr(obj, attr);
+ if (val == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(result, i, val);
+ }
+ return result;
}
PyDoc_STRVAR(attrgetter_doc,
@@ -499,56 +499,56 @@ After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
(r.name.first, r.name.last).");
static PyTypeObject attrgetter_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "operator.attrgetter", /* tp_name */
- sizeof(attrgetterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)attrgetter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)attrgetter_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- attrgetter_doc, /* tp_doc */
- (traverseproc)attrgetter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- attrgetter_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "operator.attrgetter", /* tp_name */
+ sizeof(attrgetterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)attrgetter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)attrgetter_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ attrgetter_doc, /* tp_doc */
+ (traverseproc)attrgetter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ attrgetter_new, /* tp_new */
+ 0, /* tp_free */
};
/* methodcaller object **********************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *name;
- PyObject *args;
- PyObject *kwds;
+ PyObject_HEAD
+ PyObject *name;
+ PyObject *args;
+ PyObject *kwds;
} methodcallerobject;
static PyTypeObject methodcaller_type;
@@ -556,69 +556,69 @@ static PyTypeObject methodcaller_type;
static PyObject *
methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- methodcallerobject *mc;
- PyObject *name, *newargs;
-
- if (PyTuple_GET_SIZE(args) < 1) {
- PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
- "one argument, the method name");
- return NULL;
- }
-
- /* create methodcallerobject structure */
- mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
- if (mc == NULL)
- return NULL;
-
- newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
- if (newargs == NULL) {
- Py_DECREF(mc);
- return NULL;
- }
- mc->args = newargs;
-
- name = PyTuple_GET_ITEM(args, 0);
- Py_INCREF(name);
- mc->name = name;
-
- Py_XINCREF(kwds);
- mc->kwds = kwds;
-
- PyObject_GC_Track(mc);
- return (PyObject *)mc;
+ methodcallerobject *mc;
+ PyObject *name, *newargs;
+
+ if (PyTuple_GET_SIZE(args) < 1) {
+ PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
+ "one argument, the method name");
+ return NULL;
+ }
+
+ /* create methodcallerobject structure */
+ mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
+ if (mc == NULL)
+ return NULL;
+
+ newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
+ if (newargs == NULL) {
+ Py_DECREF(mc);
+ return NULL;
+ }
+ mc->args = newargs;
+
+ name = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(name);
+ mc->name = name;
+
+ Py_XINCREF(kwds);
+ mc->kwds = kwds;
+
+ PyObject_GC_Track(mc);
+ return (PyObject *)mc;
}
static void
methodcaller_dealloc(methodcallerobject *mc)
{
- PyObject_GC_UnTrack(mc);
- Py_XDECREF(mc->name);
- Py_XDECREF(mc->args);
- Py_XDECREF(mc->kwds);
- PyObject_GC_Del(mc);
+ PyObject_GC_UnTrack(mc);
+ Py_XDECREF(mc->name);
+ Py_XDECREF(mc->args);
+ Py_XDECREF(mc->kwds);
+ PyObject_GC_Del(mc);
}
static int
methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
{
- Py_VISIT(mc->args);
- Py_VISIT(mc->kwds);
- return 0;
+ Py_VISIT(mc->args);
+ Py_VISIT(mc->kwds);
+ return 0;
}
static PyObject *
methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
{
- PyObject *method, *obj, *result;
-
- if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
- return NULL;
- method = PyObject_GetAttr(obj, mc->name);
- if (method == NULL)
- return NULL;
- result = PyObject_Call(method, mc->args, mc->kwds);
- Py_DECREF(method);
- return result;
+ PyObject *method, *obj, *result;
+
+ if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
+ return NULL;
+ method = PyObject_GetAttr(obj, mc->name);
+ if (method == NULL)
+ return NULL;
+ result = PyObject_Call(method, mc->args, mc->kwds);
+ Py_DECREF(method);
+ return result;
}
PyDoc_STRVAR(methodcaller_doc,
@@ -630,46 +630,46 @@ After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
r.name('date', foo=1).");
static PyTypeObject methodcaller_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "operator.methodcaller", /* tp_name */
- sizeof(methodcallerobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)methodcaller_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)methodcaller_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- methodcaller_doc, /* tp_doc */
- (traverseproc)methodcaller_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- methodcaller_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "operator.methodcaller", /* tp_name */
+ sizeof(methodcallerobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)methodcaller_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)methodcaller_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ methodcaller_doc, /* tp_doc */
+ (traverseproc)methodcaller_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ methodcaller_new, /* tp_new */
+ 0, /* tp_free */
};
@@ -677,40 +677,40 @@ static PyTypeObject methodcaller_type = {
static struct PyModuleDef operatormodule = {
- PyModuleDef_HEAD_INIT,
- "operator",
- operator_doc,
- -1,
- operator_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "operator",
+ operator_doc,
+ -1,
+ operator_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_operator(void)
{
- PyObject *m;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&operatormodule);
- if (m == NULL)
- return NULL;
-
- if (PyType_Ready(&itemgetter_type) < 0)
- return NULL;
- Py_INCREF(&itemgetter_type);
- PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
-
- if (PyType_Ready(&attrgetter_type) < 0)
- return NULL;
- Py_INCREF(&attrgetter_type);
- PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
-
- if (PyType_Ready(&methodcaller_type) < 0)
- return NULL;
- Py_INCREF(&methodcaller_type);
- PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
- return m;
+ PyObject *m;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&operatormodule);
+ if (m == NULL)
+ return NULL;
+
+ if (PyType_Ready(&itemgetter_type) < 0)
+ return NULL;
+ Py_INCREF(&itemgetter_type);
+ PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
+
+ if (PyType_Ready(&attrgetter_type) < 0)
+ return NULL;
+ Py_INCREF(&attrgetter_type);
+ PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
+
+ if (PyType_Ready(&methodcaller_type) < 0)
+ return NULL;
+ Py_INCREF(&methodcaller_type);
+ PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
+ return m;
}
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index 6654d71..5c990ef 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -809,8 +809,8 @@ oss_getattro(oss_audio_t *self, PyObject *nameobj)
PyObject * rval = NULL;
if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
-
+ name = _PyUnicode_AsString(nameobj);
+
if (strcmp(name, "closed") == 0) {
rval = (self->fd == -1) ? Py_True : Py_False;
Py_INCREF(rval);
@@ -975,15 +975,15 @@ error1:
static struct PyModuleDef ossaudiodevmodule = {
- PyModuleDef_HEAD_INIT,
- "ossaudiodev",
- NULL,
- -1,
- ossaudiodev_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "ossaudiodev",
+ NULL,
+ -1,
+ ossaudiodev_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -999,10 +999,10 @@ PyInit_ossaudiodev(void)
m = PyModule_Create(&ossaudiodevmodule);
if (m == NULL)
- return NULL;
+ return NULL;
OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
- NULL, NULL);
+ NULL, NULL);
if (OSSAudioError) {
/* Each call to PyModule_AddObject decrefs it; compensate: */
Py_INCREF(OSSAudioError);
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index a16e69c..dce85c2 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -578,8 +578,8 @@ parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
? eval_input : file_input,
&err, &flags);
- if (n) {
- res = parser_newstobject(n, type);
+ if (n) {
+ res = parser_newstobject(n, type);
if (res)
((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
}
@@ -784,7 +784,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
PyErr_Format(parser_error,
"third item in terminal node must be an"
" integer, found %s",
- Py_TYPE(temp)->tp_name);
+ Py_TYPE(temp)->tp_name);
Py_DECREF(o);
Py_DECREF(temp);
Py_DECREF(elem);
@@ -1051,7 +1051,7 @@ validate_class(node *tree)
{
int nch = NCH(tree);
int res = (validate_ntype(tree, classdef) &&
- ((nch == 4) || (nch == 6) || (nch == 7)));
+ ((nch == 4) || (nch == 6) || (nch == 7)));
if (res) {
res = (validate_name(CHILD(tree, 0), "class")
@@ -1064,15 +1064,15 @@ validate_class(node *tree)
}
if (res) {
- if (nch == 7) {
- res = ((validate_lparen(CHILD(tree, 2)) &&
- validate_arglist(CHILD(tree, 3)) &&
- validate_rparen(CHILD(tree, 4))));
- }
- else if (nch == 6) {
- res = (validate_lparen(CHILD(tree,2)) &&
- validate_rparen(CHILD(tree,3)));
- }
+ if (nch == 7) {
+ res = ((validate_lparen(CHILD(tree, 2)) &&
+ validate_arglist(CHILD(tree, 3)) &&
+ validate_rparen(CHILD(tree, 4))));
+ }
+ else if (nch == 6) {
+ res = (validate_lparen(CHILD(tree,2)) &&
+ validate_rparen(CHILD(tree,3)));
+ }
}
return (res);
}
@@ -1245,10 +1245,10 @@ validate_varargslist_trailer(node *tree, int start)
else {
/* skip over vfpdef (',' vfpdef ['=' test])* */
i = start + 1;
- if (TYPE(CHILD(tree, i)) == vfpdef ||
- TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
- i += 1;
- }
+ if (TYPE(CHILD(tree, i)) == vfpdef ||
+ TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
+ i += 1;
+ }
while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
res = validate_comma(CHILD(tree, i));
if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
@@ -1518,10 +1518,10 @@ validate_compound_stmt(node *tree)
static int
validate_yield_or_testlist(node *tree)
{
- if (TYPE(tree) == yield_expr)
- return validate_yield_expr(tree);
- else
- return validate_testlist(tree);
+ if (TYPE(tree) == yield_expr)
+ return validate_yield_expr(tree);
+ else
+ return validate_testlist(tree);
}
static int
@@ -1536,7 +1536,7 @@ validate_expr_stmt(node *tree)
if (res && nch == 3
&& TYPE(CHILD(tree, 1)) == augassign) {
res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
- && validate_yield_or_testlist(CHILD(tree, 2));
+ && validate_yield_or_testlist(CHILD(tree, 2));
if (res) {
char *s = STR(CHILD(CHILD(tree, 1), 0));
@@ -1714,14 +1714,14 @@ validate_dotted_as_name(node *tree)
static int
validate_dotted_as_names(node *tree)
{
- int nch = NCH(tree);
- int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
- int i;
+ int nch = NCH(tree);
+ int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
+ int i;
- for (i = 1; res && (i < nch); i += 2)
- res = (validate_comma(CHILD(tree, i))
- && validate_dotted_as_name(CHILD(tree, i + 1)));
- return (res);
+ for (i = 1; res && (i < nch); i += 2)
+ res = (validate_comma(CHILD(tree, i))
+ && validate_dotted_as_name(CHILD(tree, i + 1)));
+ return (res);
}
@@ -1734,8 +1734,8 @@ validate_import_as_names(node *tree)
int i;
for (i = 1; res && (i + 1 < nch); i += 2)
- res = (validate_comma(CHILD(tree, i))
- && validate_import_as_name(CHILD(tree, i + 1)));
+ res = (validate_comma(CHILD(tree, i))
+ && validate_import_as_name(CHILD(tree, i + 1)));
return (res);
}
@@ -1744,10 +1744,10 @@ validate_import_as_names(node *tree)
static int
validate_import_name(node *tree)
{
- return (validate_ntype(tree, import_name)
- && validate_numnodes(tree, 2, "import_name")
- && validate_name(CHILD(tree, 0), "import")
- && validate_dotted_as_names(CHILD(tree, 1)));
+ return (validate_ntype(tree, import_name)
+ && validate_numnodes(tree, 2, "import_name")
+ && validate_name(CHILD(tree, 0), "import")
+ && validate_dotted_as_names(CHILD(tree, 1)));
}
/* Helper function to count the number of leading dots in
@@ -1758,8 +1758,8 @@ count_from_dots(node *tree)
{
int i;
for (i = 1; i < NCH(tree); i++)
- if (TYPE(CHILD(tree, i)) != DOT)
- break;
+ if (TYPE(CHILD(tree, i)) != DOT)
+ break;
return i-1;
}
@@ -1769,24 +1769,24 @@ count_from_dots(node *tree)
static int
validate_import_from(node *tree)
{
- int nch = NCH(tree);
- int ndots = count_from_dots(tree);
- int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
- int offset = ndots + havename;
- int res = validate_ntype(tree, import_from)
- && (nch >= 4 + ndots)
- && validate_name(CHILD(tree, 0), "from")
- && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
- && validate_name(CHILD(tree, offset + 1), "import");
-
- if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
- res = ((nch == offset + 5)
- && validate_lparen(CHILD(tree, offset + 2))
- && validate_import_as_names(CHILD(tree, offset + 3))
- && validate_rparen(CHILD(tree, offset + 4)));
- else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
- res = validate_import_as_names(CHILD(tree, offset + 2));
- return (res);
+ int nch = NCH(tree);
+ int ndots = count_from_dots(tree);
+ int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
+ int offset = ndots + havename;
+ int res = validate_ntype(tree, import_from)
+ && (nch >= 4 + ndots)
+ && validate_name(CHILD(tree, 0), "from")
+ && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
+ && validate_name(CHILD(tree, offset + 1), "import");
+
+ if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
+ res = ((nch == offset + 5)
+ && validate_lparen(CHILD(tree, offset + 2))
+ && validate_import_as_names(CHILD(tree, offset + 3))
+ && validate_rparen(CHILD(tree, offset + 4)));
+ else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
+ res = validate_import_as_names(CHILD(tree, offset + 2));
+ return (res);
}
@@ -1798,9 +1798,9 @@ validate_import_stmt(node *tree)
int res = validate_numnodes(tree, 1, "import_stmt");
if (res) {
- int ntype = TYPE(CHILD(tree, 0));
+ int ntype = TYPE(CHILD(tree, 0));
- if (ntype == import_name || ntype == import_from)
+ if (ntype == import_name || ntype == import_from)
res = validate_node(CHILD(tree, 0));
else {
res = 0;
@@ -2321,11 +2321,11 @@ validate_atom(node *tree)
&& (validate_rparen(CHILD(tree, nch - 1))));
if (res && (nch == 3)) {
- if (TYPE(CHILD(tree, 1))==yield_expr)
- res = validate_yield_expr(CHILD(tree, 1));
- else
- res = validate_testlist_comp(CHILD(tree, 1));
- }
+ if (TYPE(CHILD(tree, 1))==yield_expr)
+ res = validate_yield_expr(CHILD(tree, 1));
+ else
+ res = validate_testlist_comp(CHILD(tree, 1));
+ }
break;
case LSQB:
if (nch == 2)
@@ -2353,11 +2353,11 @@ validate_atom(node *tree)
for (pos = 1; res && (pos < nch); ++pos)
res = validate_ntype(CHILD(tree, pos), STRING);
break;
- case DOT:
- res = (nch == 3 &&
- validate_ntype(CHILD(tree, 1), DOT) &&
- validate_ntype(CHILD(tree, 2), DOT));
- break;
+ case DOT:
+ res = (nch == 3 &&
+ validate_ntype(CHILD(tree, 1), DOT) &&
+ validate_ntype(CHILD(tree, 2), DOT));
+ break;
default:
res = 0;
break;
@@ -2414,17 +2414,17 @@ validate_decorator(node *tree)
int ok;
int nch = NCH(tree);
ok = (validate_ntype(tree, decorator) &&
- (nch == 3 || nch == 5 || nch == 6) &&
- validate_at(CHILD(tree, 0)) &&
- validate_dotted_name(CHILD(tree, 1)) &&
- validate_newline(RCHILD(tree, -1)));
+ (nch == 3 || nch == 5 || nch == 6) &&
+ validate_at(CHILD(tree, 0)) &&
+ validate_dotted_name(CHILD(tree, 1)) &&
+ validate_newline(RCHILD(tree, -1)));
if (ok && nch != 3) {
- ok = (validate_lparen(CHILD(tree, 2)) &&
- validate_rparen(RCHILD(tree, -2)));
+ ok = (validate_lparen(CHILD(tree, 2)) &&
+ validate_rparen(RCHILD(tree, -2)));
- if (ok && nch == 6)
- ok = validate_arglist(CHILD(tree, 3));
+ if (ok && nch == 6)
+ ok = validate_arglist(CHILD(tree, 3));
}
return ok;
@@ -2441,7 +2441,7 @@ validate_decorators(node *tree)
ok = validate_ntype(tree, decorators) && nch >= 1;
for (i = 0; ok && i < nch; ++i)
- ok = validate_decorator(CHILD(tree, i));
+ ok = validate_decorator(CHILD(tree, i));
return ok;
}
@@ -2456,7 +2456,7 @@ validate_with_item(node *tree)
int ok = (validate_ntype(tree, with_item)
&& (nch == 1 || nch == 3)
&& validate_test(CHILD(tree, 0)));
- if (ok && nch == 3)
+ if (ok && nch == 3)
ok = (validate_name(CHILD(tree, 1), "as")
&& validate_expr(CHILD(tree, 2)));
return ok;
@@ -2491,12 +2491,12 @@ validate_funcdef(node *tree)
{
int nch = NCH(tree);
int ok = (validate_ntype(tree, funcdef)
- && (nch == 5)
- && validate_name(RCHILD(tree, -5), "def")
- && validate_ntype(RCHILD(tree, -4), NAME)
- && validate_colon(RCHILD(tree, -2))
- && validate_parameters(RCHILD(tree, -3))
- && validate_suite(RCHILD(tree, -1)));
+ && (nch == 5)
+ && validate_name(RCHILD(tree, -5), "def")
+ && validate_ntype(RCHILD(tree, -4), NAME)
+ && validate_colon(RCHILD(tree, -2))
+ && validate_parameters(RCHILD(tree, -3))
+ && validate_suite(RCHILD(tree, -1)));
return ok;
}
@@ -2509,11 +2509,11 @@ validate_decorated(node *tree)
{
int nch = NCH(tree);
int ok = (validate_ntype(tree, decorated)
- && (nch == 2)
- && validate_decorators(RCHILD(tree, -2))
- && (validate_funcdef(RCHILD(tree, -1))
- || validate_class(RCHILD(tree, -1)))
- );
+ && (nch == 2)
+ && validate_decorators(RCHILD(tree, -2))
+ && (validate_funcdef(RCHILD(tree, -1))
+ || validate_class(RCHILD(tree, -1)))
+ );
return ok;
}
@@ -2863,9 +2863,9 @@ validate_node(node *tree)
case classdef:
res = validate_class(tree);
break;
- case decorated:
- res = validate_decorated(tree);
- break;
+ case decorated:
+ res = validate_decorated(tree);
+ break;
/*
* "Trivial" parse tree nodes.
* (Why did I call these trivial?)
@@ -2934,12 +2934,12 @@ validate_node(node *tree)
case import_stmt:
res = validate_import_stmt(tree);
break;
- case import_name:
- res = validate_import_name(tree);
- break;
- case import_from:
- res = validate_import_from(tree);
- break;
+ case import_name:
+ res = validate_import_name(tree);
+ break;
+ case import_from:
+ res = validate_import_from(tree);
+ break;
case global_stmt:
res = validate_global_stmt(tree);
break;
@@ -3154,15 +3154,15 @@ static PyMethodDef parser_functions[] = {
static struct PyModuleDef parsermodule = {
- PyModuleDef_HEAD_INIT,
- "parser",
- NULL,
- -1,
- parser_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "parser",
+ NULL,
+ -1,
+ parser_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
@@ -3176,7 +3176,7 @@ PyInit_parser(void)
return NULL;
module = PyModule_Create(&parsermodule);
if (module == NULL)
- return NULL;
+ return NULL;
if (parser_error == 0)
parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index 1547cdf..f6d36742 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -8,14 +8,14 @@
#include <pwd.h>
static PyStructSequence_Field struct_pwd_type_fields[] = {
- {"pw_name", "user name"},
- {"pw_passwd", "password"},
- {"pw_uid", "user id"},
- {"pw_gid", "group id"},
- {"pw_gecos", "real name"},
- {"pw_dir", "home directory"},
- {"pw_shell", "shell program"},
- {0}
+ {"pw_name", "user name"},
+ {"pw_passwd", "password"},
+ {"pw_uid", "user id"},
+ {"pw_gid", "group id"},
+ {"pw_gecos", "real name"},
+ {"pw_dir", "home directory"},
+ {"pw_shell", "shell program"},
+ {0}
};
PyDoc_STRVAR(struct_passwd__doc__,
@@ -25,10 +25,10 @@ This object may be accessed either as a tuple of\n\
or via the object attributes as named in the above tuple.");
static PyStructSequence_Desc struct_pwd_type_desc = {
- "pwd.struct_passwd",
- struct_passwd__doc__,
- struct_pwd_type_fields,
- 7,
+ "pwd.struct_passwd",
+ struct_passwd__doc__,
+ struct_pwd_type_fields,
+ 7,
};
PyDoc_STRVAR(pwd__doc__,
@@ -41,7 +41,7 @@ pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
The uid and gid items are integers, all others are strings. An\n\
exception is raised if the entry asked for cannot be found.");
-
+
static int initialized;
static PyTypeObject StructPwdType;
@@ -49,53 +49,53 @@ static void
sets(PyObject *v, int i, const char* val)
{
if (val) {
- PyObject *o = PyUnicode_Decode(val, strlen(val),
- Py_FileSystemDefaultEncoding,
- "surrogateescape");
- PyStructSequence_SET_ITEM(v, i, o);
+ PyObject *o = PyUnicode_Decode(val, strlen(val),
+ Py_FileSystemDefaultEncoding,
+ "surrogateescape");
+ PyStructSequence_SET_ITEM(v, i, o);
}
else {
- PyStructSequence_SET_ITEM(v, i, Py_None);
- Py_INCREF(Py_None);
+ PyStructSequence_SET_ITEM(v, i, Py_None);
+ Py_INCREF(Py_None);
}
}
static PyObject *
mkpwent(struct passwd *p)
{
- int setIndex = 0;
- PyObject *v = PyStructSequence_New(&StructPwdType);
- if (v == NULL)
- return NULL;
+ int setIndex = 0;
+ PyObject *v = PyStructSequence_New(&StructPwdType);
+ if (v == NULL)
+ return NULL;
#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
#define SETS(i,val) sets(v, i, val)
- SETS(setIndex++, p->pw_name);
+ SETS(setIndex++, p->pw_name);
#ifdef __VMS
- SETS(setIndex++, "");
+ SETS(setIndex++, "");
#else
- SETS(setIndex++, p->pw_passwd);
+ SETS(setIndex++, p->pw_passwd);
#endif
- SETI(setIndex++, p->pw_uid);
- SETI(setIndex++, p->pw_gid);
+ SETI(setIndex++, p->pw_uid);
+ SETI(setIndex++, p->pw_gid);
#ifdef __VMS
- SETS(setIndex++, "");
+ SETS(setIndex++, "");
#else
- SETS(setIndex++, p->pw_gecos);
+ SETS(setIndex++, p->pw_gecos);
#endif
- SETS(setIndex++, p->pw_dir);
- SETS(setIndex++, p->pw_shell);
+ SETS(setIndex++, p->pw_dir);
+ SETS(setIndex++, p->pw_shell);
#undef SETS
#undef SETI
- if (PyErr_Occurred()) {
- Py_XDECREF(v);
- return NULL;
- }
+ if (PyErr_Occurred()) {
+ Py_XDECREF(v);
+ return NULL;
+ }
- return v;
+ return v;
}
PyDoc_STRVAR(pwd_getpwuid__doc__,
@@ -107,16 +107,16 @@ See pwd.__doc__ for more on password database entries.");
static PyObject *
pwd_getpwuid(PyObject *self, PyObject *args)
{
- unsigned int uid;
- struct passwd *p;
- if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
- return NULL;
- if ((p = getpwuid(uid)) == NULL) {
- PyErr_Format(PyExc_KeyError,
- "getpwuid(): uid not found: %d", uid);
- return NULL;
- }
- return mkpwent(p);
+ unsigned int uid;
+ struct passwd *p;
+ if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
+ return NULL;
+ if ((p = getpwuid(uid)) == NULL) {
+ PyErr_Format(PyExc_KeyError,
+ "getpwuid(): uid not found: %d", uid);
+ return NULL;
+ }
+ return mkpwent(p);
}
PyDoc_STRVAR(pwd_getpwnam__doc__,
@@ -128,27 +128,27 @@ See pwd.__doc__ for more on password database entries.");
static PyObject *
pwd_getpwnam(PyObject *self, PyObject *args)
{
- char *name;
- struct passwd *p;
- PyObject *arg, *bytes, *retval = NULL;
-
- if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
- return NULL;
- if ((bytes = PyUnicode_AsEncodedString(arg,
- Py_FileSystemDefaultEncoding,
- "surrogateescape")) == NULL)
- return NULL;
- if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
- goto out;
- if ((p = getpwnam(name)) == NULL) {
- PyErr_Format(PyExc_KeyError,
- "getpwnam(): name not found: %s", name);
- goto out;
- }
- retval = mkpwent(p);
+ char *name;
+ struct passwd *p;
+ PyObject *arg, *bytes, *retval = NULL;
+
+ if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
+ return NULL;
+ if ((bytes = PyUnicode_AsEncodedString(arg,
+ Py_FileSystemDefaultEncoding,
+ "surrogateescape")) == NULL)
+ return NULL;
+ if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
+ goto out;
+ if ((p = getpwnam(name)) == NULL) {
+ PyErr_Format(PyExc_KeyError,
+ "getpwnam(): name not found: %s", name);
+ goto out;
+ }
+ retval = mkpwent(p);
out:
- Py_DECREF(bytes);
- return retval;
+ Py_DECREF(bytes);
+ return retval;
}
#ifdef HAVE_GETPWENT
@@ -161,67 +161,67 @@ See pwd.__doc__ for more on password database entries.");
static PyObject *
pwd_getpwall(PyObject *self)
{
- PyObject *d;
- struct passwd *p;
- if ((d = PyList_New(0)) == NULL)
- return NULL;
+ PyObject *d;
+ struct passwd *p;
+ if ((d = PyList_New(0)) == NULL)
+ return NULL;
#if defined(PYOS_OS2) && defined(PYCC_GCC)
- if ((p = getpwuid(0)) != NULL) {
+ if ((p = getpwuid(0)) != NULL) {
#else
- setpwent();
- while ((p = getpwent()) != NULL) {
+ setpwent();
+ while ((p = getpwent()) != NULL) {
#endif
- PyObject *v = mkpwent(p);
- if (v == NULL || PyList_Append(d, v) != 0) {
- Py_XDECREF(v);
- Py_DECREF(d);
- endpwent();
- return NULL;
- }
- Py_DECREF(v);
- }
- endpwent();
- return d;
+ PyObject *v = mkpwent(p);
+ if (v == NULL || PyList_Append(d, v) != 0) {
+ Py_XDECREF(v);
+ Py_DECREF(d);
+ endpwent();
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ endpwent();
+ return d;
}
#endif
static PyMethodDef pwd_methods[] = {
- {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
- {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
+ {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
+ {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
#ifdef HAVE_GETPWENT
- {"getpwall", (PyCFunction)pwd_getpwall,
- METH_NOARGS, pwd_getpwall__doc__},
+ {"getpwall", (PyCFunction)pwd_getpwall,
+ METH_NOARGS, pwd_getpwall__doc__},
#endif
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef pwdmodule = {
- PyModuleDef_HEAD_INIT,
- "pwd",
- pwd__doc__,
- -1,
- pwd_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "pwd",
+ pwd__doc__,
+ -1,
+ pwd_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_pwd(void)
{
- PyObject *m;
- m = PyModule_Create(&pwdmodule);
- if (m == NULL)
- return NULL;
-
- if (!initialized) {
- PyStructSequence_InitType(&StructPwdType,
- &struct_pwd_type_desc);
- initialized = 1;
- }
- Py_INCREF((PyObject *) &StructPwdType);
- PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
- return m;
+ PyObject *m;
+ m = PyModule_Create(&pwdmodule);
+ if (m == NULL)
+ return NULL;
+
+ if (!initialized) {
+ PyStructSequence_InitType(&StructPwdType,
+ &struct_pwd_type_desc);
+ initialized = 1;
+ }
+ Py_INCREF((PyObject *) &StructPwdType);
+ PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
+ return m;
}
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 1b9ffca..389975d 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -193,7 +193,7 @@ error_external_entity_ref_handler(XML_Parser parser,
used only from the character data handler trampoline, and must be
used right after `flag_error()` is called. */
static void
-noop_character_data_handler(void *userData, const XML_Char *data, int len)
+noop_character_data_handler(void *userData, const XML_Char *data, int len)
{
/* Do nothing. */
}
@@ -226,23 +226,23 @@ getcode(enum HandlerTypes slot, char* func_name, int lineno)
goto failed;
filename = PyUnicode_DecodeFSDefault(__FILE__);
handler_info[slot].tb_code =
- PyCode_New(0, /* argcount */
+ PyCode_New(0, /* argcount */
0, /* kwonlyargcount */
- 0, /* nlocals */
- 0, /* stacksize */
- 0, /* flags */
- code, /* code */
- nulltuple, /* consts */
- nulltuple, /* names */
- nulltuple, /* varnames */
+ 0, /* nlocals */
+ 0, /* stacksize */
+ 0, /* flags */
+ code, /* code */
+ nulltuple, /* consts */
+ nulltuple, /* names */
+ nulltuple, /* varnames */
#if PYTHON_API_VERSION >= 1010
- nulltuple, /* freevars */
- nulltuple, /* cellvars */
+ nulltuple, /* freevars */
+ nulltuple, /* cellvars */
#endif
- filename, /* filename */
- name, /* name */
- lineno, /* firstlineno */
- code /* lnotab */
+ filename, /* filename */
+ name, /* name */
+ lineno, /* firstlineno */
+ code /* lnotab */
);
if (handler_info[slot].tb_code == NULL)
goto failed;
@@ -264,25 +264,25 @@ trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
{
int result = 0;
if (!tstate->use_tracing || tstate->tracing)
- return 0;
+ return 0;
if (tstate->c_profilefunc != NULL) {
- tstate->tracing++;
- result = tstate->c_profilefunc(tstate->c_profileobj,
- f, code , val);
- tstate->use_tracing = ((tstate->c_tracefunc != NULL)
- || (tstate->c_profilefunc != NULL));
- tstate->tracing--;
- if (result)
- return result;
+ tstate->tracing++;
+ result = tstate->c_profilefunc(tstate->c_profileobj,
+ f, code , val);
+ tstate->use_tracing = ((tstate->c_tracefunc != NULL)
+ || (tstate->c_profilefunc != NULL));
+ tstate->tracing--;
+ if (result)
+ return result;
}
if (tstate->c_tracefunc != NULL) {
- tstate->tracing++;
- result = tstate->c_tracefunc(tstate->c_traceobj,
- f, code , val);
- tstate->use_tracing = ((tstate->c_tracefunc != NULL)
- || (tstate->c_profilefunc != NULL));
- tstate->tracing--;
- }
+ tstate->tracing++;
+ result = tstate->c_tracefunc(tstate->c_traceobj,
+ f, code , val);
+ tstate->use_tracing = ((tstate->c_tracefunc != NULL)
+ || (tstate->c_profilefunc != NULL));
+ tstate->tracing--;
+ }
return result;
}
@@ -293,12 +293,12 @@ trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
int err;
if (tstate->c_tracefunc == NULL)
- return 0;
+ return 0;
PyErr_Fetch(&type, &value, &traceback);
if (value == NULL) {
- value = Py_None;
- Py_INCREF(value);
+ value = Py_None;
+ Py_INCREF(value);
}
#if PY_VERSION_HEX < 0x02040000
arg = Py_BuildValue("(OOO)", type, value, traceback);
@@ -306,17 +306,17 @@ trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
arg = PyTuple_Pack(3, type, value, traceback);
#endif
if (arg == NULL) {
- PyErr_Restore(type, value, traceback);
- return 0;
+ PyErr_Restore(type, value, traceback);
+ return 0;
}
err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
Py_DECREF(arg);
if (err == 0)
- PyErr_Restore(type, value, traceback);
+ PyErr_Restore(type, value, traceback);
else {
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(traceback);
+ Py_XDECREF(type);
+ Py_XDECREF(value);
+ Py_XDECREF(traceback);
}
return err;
}
@@ -332,31 +332,31 @@ call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
if (c == NULL)
return NULL;
-
+
f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
if (f == NULL)
return NULL;
tstate->frame = f;
#ifdef FIX_TRACE
if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
- return NULL;
+ return NULL;
}
#endif
res = PyEval_CallObject(func, args);
if (res == NULL) {
- if (tstate->curexc_traceback == NULL)
- PyTraceBack_Here(f);
+ if (tstate->curexc_traceback == NULL)
+ PyTraceBack_Here(f);
XML_StopParser(self->itself, XML_FALSE);
#ifdef FIX_TRACE
- if (trace_frame_exc(tstate, f) < 0) {
- return NULL;
- }
+ if (trace_frame_exc(tstate, f) < 0) {
+ return NULL;
+ }
}
else {
- if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
- Py_XDECREF(res);
- res = NULL;
- }
+ if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
+ Py_XDECREF(res);
+ res = NULL;
+ }
}
#else
}
@@ -373,12 +373,12 @@ string_intern(xmlparseobject *self, const char* str)
PyObject *value;
/* result can be NULL if the unicode conversion failed. */
if (!result)
- return result;
+ return result;
if (!self->intern)
- return result;
+ return result;
value = PyDict_GetItem(self->intern, result);
if (!value) {
- if (PyDict_SetItem(self->intern, result, result) == 0)
+ if (PyDict_SetItem(self->intern, result, result) == 0)
return result;
else
return NULL;
@@ -438,7 +438,7 @@ flush_character_buffer(xmlparseobject *self)
}
static void
-my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
+my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
{
xmlparseobject *self = (xmlparseobject *) userData;
if (self->buffer == NULL)
@@ -578,13 +578,13 @@ my_##NAME##Handler PARAMS {\
}
#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
- RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
- (xmlparseobject *)userData)
+ RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
+ (xmlparseobject *)userData)
#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
- RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
- rc = PyLong_AsLong(rv);, rc, \
- (xmlparseobject *)userData)
+ RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
+ rc = PyLong_AsLong(rv);, rc, \
+ (xmlparseobject *)userData)
VOID_HANDLER(EndElement,
(void *userData, const XML_Char *name),
@@ -729,25 +729,25 @@ VOID_HANDLER(SkippedEntity,
#endif
VOID_HANDLER(NotationDecl,
- (void *userData,
- const XML_Char *notationName,
- const XML_Char *base,
- const XML_Char *systemId,
- const XML_Char *publicId),
+ (void *userData,
+ const XML_Char *notationName,
+ const XML_Char *base,
+ const XML_Char *systemId,
+ const XML_Char *publicId),
("(NNNN)",
- string_intern(self, notationName), string_intern(self, base),
- string_intern(self, systemId), string_intern(self, publicId)))
+ string_intern(self, notationName), string_intern(self, base),
+ string_intern(self, systemId), string_intern(self, publicId)))
VOID_HANDLER(StartNamespaceDecl,
- (void *userData,
- const XML_Char *prefix,
- const XML_Char *uri),
+ (void *userData,
+ const XML_Char *prefix,
+ const XML_Char *uri),
("(NN)",
string_intern(self, prefix), string_intern(self, uri)))
VOID_HANDLER(EndNamespaceDecl,
- (void *userData,
- const XML_Char *prefix),
+ (void *userData,
+ const XML_Char *prefix),
("(N)", string_intern(self, prefix)))
VOID_HANDLER(Comment,
@@ -756,36 +756,36 @@ VOID_HANDLER(Comment,
VOID_HANDLER(StartCdataSection,
(void *userData),
- ("()"))
+ ("()"))
VOID_HANDLER(EndCdataSection,
(void *userData),
- ("()"))
+ ("()"))
VOID_HANDLER(Default,
- (void *userData, const XML_Char *s, int len),
- ("(N)", (conv_string_len_to_unicode(s,len))))
+ (void *userData, const XML_Char *s, int len),
+ ("(N)", (conv_string_len_to_unicode(s,len))))
VOID_HANDLER(DefaultHandlerExpand,
- (void *userData, const XML_Char *s, int len),
- ("(N)", (conv_string_len_to_unicode(s,len))))
+ (void *userData, const XML_Char *s, int len),
+ ("(N)", (conv_string_len_to_unicode(s,len))))
INT_HANDLER(NotStandalone,
- (void *userData),
- ("()"))
+ (void *userData),
+ ("()"))
RC_HANDLER(int, ExternalEntityRef,
- (XML_Parser parser,
- const XML_Char *context,
- const XML_Char *base,
- const XML_Char *systemId,
- const XML_Char *publicId),
- int rc=0;,
+ (XML_Parser parser,
+ const XML_Char *context,
+ const XML_Char *base,
+ const XML_Char *systemId,
+ const XML_Char *publicId),
+ int rc=0;,
("(O&NNN)",
- conv_string_to_unicode ,context, string_intern(self, base),
- string_intern(self, systemId), string_intern(self, publicId)),
- rc = PyLong_AsLong(rv);, rc,
- XML_GetUserData(parser))
+ conv_string_to_unicode ,context, string_intern(self, base),
+ string_intern(self, systemId), string_intern(self, publicId)),
+ rc = PyLong_AsLong(rv);, rc,
+ XML_GetUserData(parser))
/* XXX UnknownEncodingHandler */
@@ -957,7 +957,7 @@ xmlparse_SetBase(xmlparseobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "s:SetBase", &base))
return NULL;
if (!XML_SetBase(self->itself, base)) {
- return PyErr_NoMemory();
+ return PyErr_NoMemory();
}
Py_INCREF(Py_None);
return Py_None;
@@ -1047,7 +1047,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
new_parser->in_callback = 0;
new_parser->ns_prefixes = self->ns_prefixes;
new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
- encoding);
+ encoding);
new_parser->handlers = 0;
new_parser->intern = self->intern;
Py_XINCREF(new_parser->intern);
@@ -1138,26 +1138,26 @@ xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
static PyObject *xmlparse_dir(PyObject *self, PyObject* noargs);
static struct PyMethodDef xmlparse_methods[] = {
- {"Parse", (PyCFunction)xmlparse_Parse,
- METH_VARARGS, xmlparse_Parse__doc__},
+ {"Parse", (PyCFunction)xmlparse_Parse,
+ METH_VARARGS, xmlparse_Parse__doc__},
{"ParseFile", (PyCFunction)xmlparse_ParseFile,
- METH_O, xmlparse_ParseFile__doc__},
+ METH_O, xmlparse_ParseFile__doc__},
{"SetBase", (PyCFunction)xmlparse_SetBase,
- METH_VARARGS, xmlparse_SetBase__doc__},
+ METH_VARARGS, xmlparse_SetBase__doc__},
{"GetBase", (PyCFunction)xmlparse_GetBase,
- METH_NOARGS, xmlparse_GetBase__doc__},
+ METH_NOARGS, xmlparse_GetBase__doc__},
{"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
- METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
+ METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
{"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
- METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
+ METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
{"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
- METH_NOARGS, xmlparse_GetInputContext__doc__},
+ METH_NOARGS, xmlparse_GetInputContext__doc__},
#if XML_COMBINED_VERSION >= 19505
{"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
- METH_VARARGS, xmlparse_UseForeignDTD__doc__},
+ METH_VARARGS, xmlparse_UseForeignDTD__doc__},
#endif
{"__dir__", xmlparse_dir, METH_NOARGS},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
/* ---------- */
@@ -1175,7 +1175,7 @@ init_template_buffer(void)
{
int i;
for (i = 0; i < 256; i++) {
- template_buffer[i] = i;
+ template_buffer[i] = i;
}
template_buffer[256] = 0;
}
@@ -1194,15 +1194,15 @@ PyUnknownEncodingHandler(void *encodingHandlerData,
PyUnicode_Decode(template_buffer, 256, name, "replace");
if (_u_string == NULL)
- return result;
+ return result;
for (i = 0; i < 256; i++) {
- /* Stupid to access directly, but fast */
- Py_UNICODE c = _u_string->str[i];
- if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
- info->map[i] = -1;
- else
- info->map[i] = c;
+ /* Stupid to access directly, but fast */
+ Py_UNICODE c = _u_string->str[i];
+ if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
+ info->map[i] = -1;
+ else
+ info->map[i] = c;
}
info->data = NULL;
info->convert = NULL;
@@ -1337,8 +1337,8 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
int handlernum = -1;
if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
-
+ name = _PyUnicode_AsString(nameobj);
+
handlernum = handlername2int(name);
if (handlernum != -1) {
@@ -1404,18 +1404,18 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
static PyObject *
xmlparse_dir(PyObject *self, PyObject* noargs)
{
-#define APPEND(list, str) \
- do { \
- PyObject *o = PyUnicode_FromString(str); \
- if (o != NULL) \
- PyList_Append(list, o); \
- Py_XDECREF(o); \
+#define APPEND(list, str) \
+ do { \
+ PyObject *o = PyUnicode_FromString(str); \
+ if (o != NULL) \
+ PyList_Append(list, o); \
+ Py_XDECREF(o); \
} while (0)
int i;
PyObject *rc = PyList_New(0);
if (!rc)
- return NULL;
+ return NULL;
for (i = 0; handler_info[i].name != NULL; i++) {
PyObject *o = get_handler_name(&handler_info[i]);
if (o != NULL)
@@ -1536,42 +1536,42 @@ xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
if (strcmp(name, "buffer_size") == 0) {
long new_buffer_size;
if (!PyLong_Check(v)) {
- PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
- return -1;
+ PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
+ return -1;
}
new_buffer_size=PyLong_AS_LONG(v);
/* trivial case -- no change */
if (new_buffer_size == self->buffer_size) {
- return 0;
+ return 0;
}
if (new_buffer_size <= 0) {
- PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
- return -1;
+ PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
+ return -1;
}
/* check maximum */
if (new_buffer_size > INT_MAX) {
- char errmsg[100];
- sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
- PyErr_SetString(PyExc_ValueError, errmsg);
- return -1;
+ char errmsg[100];
+ sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
+ PyErr_SetString(PyExc_ValueError, errmsg);
+ return -1;
}
if (self->buffer != NULL) {
- /* there is already a buffer */
- if (self->buffer_used != 0) {
- flush_character_buffer(self);
- }
- /* free existing buffer */
- free(self->buffer);
+ /* there is already a buffer */
+ if (self->buffer_used != 0) {
+ flush_character_buffer(self);
+ }
+ /* free existing buffer */
+ free(self->buffer);
}
self->buffer = malloc(new_buffer_size);
if (self->buffer == NULL) {
- PyErr_NoMemory();
- return -1;
- }
+ PyErr_NoMemory();
+ return -1;
+ }
self->buffer_size = new_buffer_size;
return 0;
}
@@ -1612,39 +1612,39 @@ xmlparse_clear(xmlparseobject *op)
PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
static PyTypeObject Xmlparsetype = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "pyexpat.xmlparser", /*tp_name*/
- sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)xmlparse_dealloc, /*tp_dealloc*/
- (printfunc)0, /*tp_print*/
- 0, /*tp_getattr*/
- (setattrfunc)xmlparse_setattr, /*tp_setattr*/
- 0, /*tp_reserved*/
- (reprfunc)0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- (hashfunc)0, /*tp_hash*/
- (ternaryfunc)0, /*tp_call*/
- (reprfunc)0, /*tp_str*/
- (getattrofunc)xmlparse_getattro, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "pyexpat.xmlparser", /*tp_name*/
+ sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)xmlparse_dealloc, /*tp_dealloc*/
+ (printfunc)0, /*tp_print*/
+ 0, /*tp_getattr*/
+ (setattrfunc)xmlparse_setattr, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ (reprfunc)0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ (hashfunc)0, /*tp_hash*/
+ (ternaryfunc)0, /*tp_call*/
+ (reprfunc)0, /*tp_str*/
+ (getattrofunc)xmlparse_getattro, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
#ifdef Py_TPFLAGS_HAVE_GC
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
#else
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
#endif
- Xmlparsetype__doc__, /* tp_doc - Documentation string */
- (traverseproc)xmlparse_traverse, /* tp_traverse */
- (inquiry)xmlparse_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- xmlparse_methods, /* tp_methods */
+ Xmlparsetype__doc__, /* tp_doc - Documentation string */
+ (traverseproc)xmlparse_traverse, /* tp_traverse */
+ (inquiry)xmlparse_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ xmlparse_methods, /* tp_methods */
};
/* End of code for xmlparser objects */
@@ -1678,21 +1678,21 @@ pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
/* Explicitly passing None means no interning is desired.
Not passing anything means that a new dictionary is used. */
if (intern == Py_None)
- intern = NULL;
+ intern = NULL;
else if (intern == NULL) {
- intern = PyDict_New();
- if (!intern)
- return NULL;
- intern_decref = 1;
+ intern = PyDict_New();
+ if (!intern)
+ return NULL;
+ intern_decref = 1;
}
else if (!PyDict_Check(intern)) {
- PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
- return NULL;
+ PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
+ return NULL;
}
result = newxmlparseobject(encoding, namespace_separator, intern);
if (intern_decref) {
- Py_DECREF(intern);
+ Py_DECREF(intern);
}
return result;
}
@@ -1714,12 +1714,12 @@ pyexpat_ErrorString(PyObject *self, PyObject *args)
/* List of methods defined in the module */
static struct PyMethodDef pyexpat_methods[] = {
- {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
+ {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
- {"ErrorString", (PyCFunction)pyexpat_ErrorString,
- METH_VARARGS, pyexpat_ErrorString__doc__},
+ {"ErrorString", (PyCFunction)pyexpat_ErrorString,
+ METH_VARARGS, pyexpat_ErrorString__doc__},
- {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
+ {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
/* Module docstring */
@@ -1768,15 +1768,15 @@ get_version_string(void)
PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
static struct PyModuleDef pyexpatmodule = {
- PyModuleDef_HEAD_INIT,
- MODULE_NAME,
- pyexpat_module_documentation,
- -1,
- pyexpat_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ MODULE_NAME,
+ pyexpat_module_documentation,
+ -1,
+ pyexpat_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -1798,12 +1798,12 @@ MODULE_INITFUNC(void)
return NULL;
if (PyType_Ready(&Xmlparsetype) < 0)
- return NULL;
+ return NULL;
/* Create the module and add the functions */
m = PyModule_Create(&pyexpatmodule);
if (m == NULL)
- return NULL;
+ return NULL;
/* Add some symbolic constants to the module */
if (ErrorObject == NULL) {
@@ -1860,7 +1860,7 @@ MODULE_INITFUNC(void)
if (errors_module == NULL || model_module == NULL)
/* Don't core dump later! */
return NULL;
-
+
#if XML_COMBINED_VERSION > 19505
{
const XML_Feature *features = XML_GetFeatureList();
@@ -1985,7 +1985,7 @@ MODULE_INITFUNC(void)
capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
capi.SetUserData = XML_SetUserData;
-
+
/* export using capsule */
capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
if (capi_object)
@@ -2001,12 +2001,12 @@ clear_handlers(xmlparseobject *self, int initial)
for (; handler_info[i].name != NULL; i++) {
if (initial)
- self->handlers[i] = NULL;
- else {
+ self->handlers[i] = NULL;
+ else {
temp = self->handlers[i];
self->handlers[i] = NULL;
Py_XDECREF(temp);
- handler_info[i].setter(self->itself, NULL);
+ handler_info[i].setter(self->itself, NULL);
}
}
}
diff --git a/Modules/python.c b/Modules/python.c
index edd33f4..50ec5f0 100644
--- a/Modules/python.c
+++ b/Modules/python.c
@@ -11,150 +11,150 @@
int
wmain(int argc, wchar_t **argv)
{
- return Py_Main(argc, argv);
+ return Py_Main(argc, argv);
}
#else
static wchar_t*
char2wchar(char* arg)
{
- wchar_t *res;
+ wchar_t *res;
#ifdef HAVE_BROKEN_MBSTOWCS
- /* Some platforms have a broken implementation of
- * mbstowcs which does not count the characters that
- * would result from conversion. Use an upper bound.
- */
- size_t argsize = strlen(arg);
+ /* Some platforms have a broken implementation of
+ * mbstowcs which does not count the characters that
+ * would result from conversion. Use an upper bound.
+ */
+ size_t argsize = strlen(arg);
#else
- size_t argsize = mbstowcs(NULL, arg, 0);
+ size_t argsize = mbstowcs(NULL, arg, 0);
#endif
- size_t count;
- unsigned char *in;
- wchar_t *out;
+ size_t count;
+ unsigned char *in;
+ wchar_t *out;
#ifdef HAVE_MBRTOWC
- mbstate_t mbs;
+ mbstate_t mbs;
#endif
- if (argsize != (size_t)-1) {
- res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
- if (!res)
- goto oom;
- count = mbstowcs(res, arg, argsize+1);
- if (count != (size_t)-1) {
- wchar_t *tmp;
- /* Only use the result if it contains no
- surrogate characters. */
- for (tmp = res; *tmp != 0 &&
- (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
- ;
- if (*tmp == 0)
- return res;
- }
- PyMem_Free(res);
- }
- /* Conversion failed. Fall back to escaping with surrogateescape. */
+ if (argsize != (size_t)-1) {
+ res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
+ if (!res)
+ goto oom;
+ count = mbstowcs(res, arg, argsize+1);
+ if (count != (size_t)-1) {
+ wchar_t *tmp;
+ /* Only use the result if it contains no
+ surrogate characters. */
+ for (tmp = res; *tmp != 0 &&
+ (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
+ ;
+ if (*tmp == 0)
+ return res;
+ }
+ PyMem_Free(res);
+ }
+ /* Conversion failed. Fall back to escaping with surrogateescape. */
#ifdef HAVE_MBRTOWC
- /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
-
- /* Overallocate; as multi-byte characters are in the argument, the
- actual output could use less memory. */
- argsize = strlen(arg) + 1;
- res = PyMem_Malloc(argsize*sizeof(wchar_t));
- if (!res) goto oom;
- in = (unsigned char*)arg;
- out = res;
- memset(&mbs, 0, sizeof mbs);
- while (argsize) {
- size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
- if (converted == 0)
- /* Reached end of string; null char stored. */
- break;
- if (converted == (size_t)-2) {
- /* Incomplete character. This should never happen,
- since we provide everything that we have -
- unless there is a bug in the C library, or I
- misunderstood how mbrtowc works. */
- fprintf(stderr, "unexpected mbrtowc result -2\n");
- return NULL;
- }
- if (converted == (size_t)-1) {
- /* Conversion error. Escape as UTF-8b, and start over
- in the initial shift state. */
- *out++ = 0xdc00 + *in++;
- argsize--;
- memset(&mbs, 0, sizeof mbs);
- continue;
- }
- if (*out >= 0xd800 && *out <= 0xdfff) {
- /* Surrogate character. Escape the original
- byte sequence with surrogateescape. */
- argsize -= converted;
- while (converted--)
- *out++ = 0xdc00 + *in++;
- continue;
- }
- /* successfully converted some bytes */
- in += converted;
- argsize -= converted;
- out++;
- }
+ /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
+
+ /* Overallocate; as multi-byte characters are in the argument, the
+ actual output could use less memory. */
+ argsize = strlen(arg) + 1;
+ res = PyMem_Malloc(argsize*sizeof(wchar_t));
+ if (!res) goto oom;
+ in = (unsigned char*)arg;
+ out = res;
+ memset(&mbs, 0, sizeof mbs);
+ while (argsize) {
+ size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
+ if (converted == 0)
+ /* Reached end of string; null char stored. */
+ break;
+ if (converted == (size_t)-2) {
+ /* Incomplete character. This should never happen,
+ since we provide everything that we have -
+ unless there is a bug in the C library, or I
+ misunderstood how mbrtowc works. */
+ fprintf(stderr, "unexpected mbrtowc result -2\n");
+ return NULL;
+ }
+ if (converted == (size_t)-1) {
+ /* Conversion error. Escape as UTF-8b, and start over
+ in the initial shift state. */
+ *out++ = 0xdc00 + *in++;
+ argsize--;
+ memset(&mbs, 0, sizeof mbs);
+ continue;
+ }
+ if (*out >= 0xd800 && *out <= 0xdfff) {
+ /* Surrogate character. Escape the original
+ byte sequence with surrogateescape. */
+ argsize -= converted;
+ while (converted--)
+ *out++ = 0xdc00 + *in++;
+ continue;
+ }
+ /* successfully converted some bytes */
+ in += converted;
+ argsize -= converted;
+ out++;
+ }
#else
- /* Cannot use C locale for escaping; manually escape as if charset
- is ASCII (i.e. escape all bytes > 128. This will still roundtrip
- correctly in the locale's charset, which must be an ASCII superset. */
- res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
- if (!res) goto oom;
- in = (unsigned char*)arg;
- out = res;
- while(*in)
- if(*in < 128)
- *out++ = *in++;
- else
- *out++ = 0xdc00 + *in++;
- *out = 0;
+ /* Cannot use C locale for escaping; manually escape as if charset
+ is ASCII (i.e. escape all bytes > 128. This will still roundtrip
+ correctly in the locale's charset, which must be an ASCII superset. */
+ res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
+ if (!res) goto oom;
+ in = (unsigned char*)arg;
+ out = res;
+ while(*in)
+ if(*in < 128)
+ *out++ = *in++;
+ else
+ *out++ = 0xdc00 + *in++;
+ *out = 0;
#endif
- return res;
+ return res;
oom:
- fprintf(stderr, "out of memory\n");
- return NULL;
+ fprintf(stderr, "out of memory\n");
+ return NULL;
}
int
main(int argc, char **argv)
{
- wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
- /* We need a second copies, as Python might modify the first one. */
- wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
- int i, res;
- char *oldloc;
- /* 754 requires that FP exceptions run in "no stop" mode by default,
- * and until C vendors implement C99's ways to control FP exceptions,
- * Python requires non-stop mode. Alas, some platforms enable FP
- * exceptions by default. Here we disable them.
- */
+ wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
+ /* We need a second copies, as Python might modify the first one. */
+ wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
+ int i, res;
+ char *oldloc;
+ /* 754 requires that FP exceptions run in "no stop" mode by default,
+ * and until C vendors implement C99's ways to control FP exceptions,
+ * Python requires non-stop mode. Alas, some platforms enable FP
+ * exceptions by default. Here we disable them.
+ */
#ifdef __FreeBSD__
- fp_except_t m;
+ fp_except_t m;
- m = fpgetmask();
- fpsetmask(m & ~FP_X_OFL);
+ m = fpgetmask();
+ fpsetmask(m & ~FP_X_OFL);
#endif
- if (!argv_copy || !argv_copy2) {
- fprintf(stderr, "out of memory\n");
- return 1;
- }
- oldloc = strdup(setlocale(LC_ALL, NULL));
- setlocale(LC_ALL, "");
- for (i = 0; i < argc; i++) {
- argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
- if (!argv_copy[i])
- return 1;
- }
- setlocale(LC_ALL, oldloc);
- free(oldloc);
- res = Py_Main(argc, argv_copy);
- for (i = 0; i < argc; i++) {
- PyMem_Free(argv_copy2[i]);
- }
- PyMem_Free(argv_copy);
- PyMem_Free(argv_copy2);
- return res;
+ if (!argv_copy || !argv_copy2) {
+ fprintf(stderr, "out of memory\n");
+ return 1;
+ }
+ oldloc = strdup(setlocale(LC_ALL, NULL));
+ setlocale(LC_ALL, "");
+ for (i = 0; i < argc; i++) {
+ argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
+ if (!argv_copy[i])
+ return 1;
+ }
+ setlocale(LC_ALL, oldloc);
+ free(oldloc);
+ res = Py_Main(argc, argv_copy);
+ for (i = 0; i < argc; i++) {
+ PyMem_Free(argv_copy2[i]);
+ }
+ PyMem_Free(argv_copy);
+ PyMem_Free(argv_copy2);
+ return res;
}
#endif
diff --git a/Modules/readline.c b/Modules/readline.c
index 5529b66..e4a1d55 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -23,7 +23,7 @@
#ifdef SAVE_LOCALE
# define RESTORE_LOCALE(sl) { setlocale(LC_CTYPE, sl); free(sl); }
#else
-# define RESTORE_LOCALE(sl)
+# define RESTORE_LOCALE(sl)
#endif
/* GNU readline definitions */
@@ -33,7 +33,7 @@
#ifdef HAVE_RL_COMPLETION_MATCHES
#define completion_matches(x, y) \
- rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
+ rl_completion_matches((x), ((rl_compentry_func_t *)(y)))
#else
#if defined(_RL_FUNCTION_TYPEDEF)
extern char **completion_matches(char *, rl_compentry_func_t *);
@@ -44,7 +44,7 @@ extern char **completion_matches(char *, CPFunction *);
static void
on_completion_display_matches_hook(char **matches,
- int num_matches, int max_length);
+ int num_matches, int max_length);
/* Exported function to send one line to readline's init file parser */
@@ -52,18 +52,18 @@ on_completion_display_matches_hook(char **matches,
static PyObject *
parse_and_bind(PyObject *self, PyObject *args)
{
- char *s, *copy;
- if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
- return NULL;
- /* Make a copy -- rl_parse_and_bind() modifies its argument */
- /* Bernard Herzog */
- copy = malloc(1 + strlen(s));
- if (copy == NULL)
- return PyErr_NoMemory();
- strcpy(copy, s);
- rl_parse_and_bind(copy);
- free(copy); /* Free the copy */
- Py_RETURN_NONE;
+ char *s, *copy;
+ if (!PyArg_ParseTuple(args, "s:parse_and_bind", &s))
+ return NULL;
+ /* Make a copy -- rl_parse_and_bind() modifies its argument */
+ /* Bernard Herzog */
+ copy = malloc(1 + strlen(s));
+ if (copy == NULL)
+ return PyErr_NoMemory();
+ strcpy(copy, s);
+ rl_parse_and_bind(copy);
+ free(copy); /* Free the copy */
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_parse_and_bind,
@@ -76,13 +76,13 @@ Parse and execute single line of a readline init file.");
static PyObject *
read_init_file(PyObject *self, PyObject *args)
{
- char *s = NULL;
- if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
- return NULL;
- errno = rl_read_init_file(s);
- if (errno)
- return PyErr_SetFromErrno(PyExc_IOError);
- Py_RETURN_NONE;
+ char *s = NULL;
+ if (!PyArg_ParseTuple(args, "|z:read_init_file", &s))
+ return NULL;
+ errno = rl_read_init_file(s);
+ if (errno)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_read_init_file,
@@ -96,13 +96,13 @@ The default filename is the last filename used.");
static PyObject *
read_history_file(PyObject *self, PyObject *args)
{
- char *s = NULL;
- if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
- return NULL;
- errno = read_history(s);
- if (errno)
- return PyErr_SetFromErrno(PyExc_IOError);
- Py_RETURN_NONE;
+ char *s = NULL;
+ if (!PyArg_ParseTuple(args, "|z:read_history_file", &s))
+ return NULL;
+ errno = read_history(s);
+ if (errno)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_RETURN_NONE;
}
static int _history_length = -1; /* do not truncate history by default */
@@ -117,15 +117,15 @@ The default filename is ~/.history.");
static PyObject *
write_history_file(PyObject *self, PyObject *args)
{
- char *s = NULL;
- if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
- return NULL;
- errno = write_history(s);
- if (!errno && _history_length >= 0)
- history_truncate_file(s, _history_length);
- if (errno)
- return PyErr_SetFromErrno(PyExc_IOError);
- Py_RETURN_NONE;
+ char *s = NULL;
+ if (!PyArg_ParseTuple(args, "|z:write_history_file", &s))
+ return NULL;
+ errno = write_history(s);
+ if (!errno && _history_length >= 0)
+ history_truncate_file(s, _history_length);
+ if (errno)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_write_history_file,
@@ -139,11 +139,11 @@ The default filename is ~/.history.");
static PyObject*
set_history_length(PyObject *self, PyObject *args)
{
- int length = _history_length;
- if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
- return NULL;
- _history_length = length;
- Py_RETURN_NONE;
+ int length = _history_length;
+ if (!PyArg_ParseTuple(args, "i:set_history_length", &length))
+ return NULL;
+ _history_length = length;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(set_history_length_doc,
@@ -158,7 +158,7 @@ history truncation.");
static PyObject*
get_history_length(PyObject *self, PyObject *noarg)
{
- return PyLong_FromLong(_history_length);
+ return PyLong_FromLong(_history_length);
}
PyDoc_STRVAR(get_history_length_doc,
@@ -172,29 +172,29 @@ the history file.");
static PyObject *
set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
{
- PyObject *function = Py_None;
- char buf[80];
- PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
- if (!PyArg_ParseTuple(args, buf, &function))
- return NULL;
- if (function == Py_None) {
- Py_XDECREF(*hook_var);
- *hook_var = NULL;
- }
- else if (PyCallable_Check(function)) {
- PyObject *tmp = *hook_var;
- Py_INCREF(function);
- *hook_var = function;
- Py_XDECREF(tmp);
- }
- else {
- PyOS_snprintf(buf, sizeof(buf),
- "set_%.50s(func): argument not callable",
- funcname);
- PyErr_SetString(PyExc_TypeError, buf);
- return NULL;
- }
- Py_RETURN_NONE;
+ PyObject *function = Py_None;
+ char buf[80];
+ PyOS_snprintf(buf, sizeof(buf), "|O:set_%.50s", funcname);
+ if (!PyArg_ParseTuple(args, buf, &function))
+ return NULL;
+ if (function == Py_None) {
+ Py_XDECREF(*hook_var);
+ *hook_var = NULL;
+ }
+ else if (PyCallable_Check(function)) {
+ PyObject *tmp = *hook_var;
+ Py_INCREF(function);
+ *hook_var = function;
+ Py_XDECREF(tmp);
+ }
+ else {
+ PyOS_snprintf(buf, sizeof(buf),
+ "set_%.50s(func): argument not callable",
+ funcname);
+ PyErr_SetString(PyExc_TypeError, buf);
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
@@ -210,20 +210,20 @@ static PyObject *pre_input_hook = NULL;
static PyObject *
set_completion_display_matches_hook(PyObject *self, PyObject *args)
{
- PyObject *result = set_hook("completion_display_matches_hook",
- &completion_display_matches_hook, args);
+ PyObject *result = set_hook("completion_display_matches_hook",
+ &completion_display_matches_hook, args);
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
- /* We cannot set this hook globally, since it replaces the
- default completion display. */
- rl_completion_display_matches_hook =
- completion_display_matches_hook ?
+ /* We cannot set this hook globally, since it replaces the
+ default completion display. */
+ rl_completion_display_matches_hook =
+ completion_display_matches_hook ?
#if defined(_RL_FUNCTION_TYPEDEF)
- (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
+ (rl_compdisp_func_t *)on_completion_display_matches_hook : 0;
#else
- (VFunction *)on_completion_display_matches_hook : 0;
+ (VFunction *)on_completion_display_matches_hook : 0;
#endif
#endif
- return result;
+ return result;
}
@@ -237,7 +237,7 @@ once each time matches need to be displayed.");
static PyObject *
set_startup_hook(PyObject *self, PyObject *args)
{
- return set_hook("startup_hook", &startup_hook, args);
+ return set_hook("startup_hook", &startup_hook, args);
}
PyDoc_STRVAR(doc_set_startup_hook,
@@ -254,7 +254,7 @@ before readline prints the first prompt.");
static PyObject *
set_pre_input_hook(PyObject *self, PyObject *args)
{
- return set_hook("pre_input_hook", &pre_input_hook, args);
+ return set_hook("pre_input_hook", &pre_input_hook, args);
}
PyDoc_STRVAR(doc_set_pre_input_hook,
@@ -292,8 +292,8 @@ Get the type of completion being attempted.");
static PyObject *
get_begidx(PyObject *self, PyObject *noarg)
{
- Py_INCREF(begidx);
- return begidx;
+ Py_INCREF(begidx);
+ return begidx;
}
PyDoc_STRVAR(doc_get_begidx,
@@ -306,8 +306,8 @@ get the beginning index of the readline tab-completion scope");
static PyObject *
get_endidx(PyObject *self, PyObject *noarg)
{
- Py_INCREF(endidx);
- return endidx;
+ Py_INCREF(endidx);
+ return endidx;
}
PyDoc_STRVAR(doc_get_endidx,
@@ -320,14 +320,14 @@ get the ending index of the readline tab-completion scope");
static PyObject *
set_completer_delims(PyObject *self, PyObject *args)
{
- char *break_chars;
-
- if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
- return NULL;
- }
- free((void*)rl_completer_word_break_characters);
- rl_completer_word_break_characters = strdup(break_chars);
- Py_RETURN_NONE;
+ char *break_chars;
+
+ if(!PyArg_ParseTuple(args, "s:set_completer_delims", &break_chars)) {
+ return NULL;
+ }
+ free((void*)rl_completer_word_break_characters);
+ rl_completer_word_break_characters = strdup(break_chars);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_set_completer_delims,
@@ -337,31 +337,31 @@ set the readline word delimiters for tab-completion");
static PyObject *
py_remove_history(PyObject *self, PyObject *args)
{
- int entry_number;
- HIST_ENTRY *entry;
-
- if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
- return NULL;
- if (entry_number < 0) {
- PyErr_SetString(PyExc_ValueError,
- "History index cannot be negative");
- return NULL;
- }
- entry = remove_history(entry_number);
- if (!entry) {
- PyErr_Format(PyExc_ValueError,
- "No history item at position %d",
- entry_number);
- return NULL;
- }
- /* free memory allocated for the history entry */
- if (entry->line)
- free(entry->line);
- if (entry->data)
- free(entry->data);
- free(entry);
-
- Py_RETURN_NONE;
+ int entry_number;
+ HIST_ENTRY *entry;
+
+ if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
+ return NULL;
+ if (entry_number < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "History index cannot be negative");
+ return NULL;
+ }
+ entry = remove_history(entry_number);
+ if (!entry) {
+ PyErr_Format(PyExc_ValueError,
+ "No history item at position %d",
+ entry_number);
+ return NULL;
+ }
+ /* free memory allocated for the history entry */
+ if (entry->line)
+ free(entry->line);
+ if (entry->data)
+ free(entry->data);
+ free(entry);
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_remove_history,
@@ -371,34 +371,34 @@ remove history item given by its position");
static PyObject *
py_replace_history(PyObject *self, PyObject *args)
{
- int entry_number;
- char *line;
- HIST_ENTRY *old_entry;
-
- if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
- &line)) {
- return NULL;
- }
- if (entry_number < 0) {
- PyErr_SetString(PyExc_ValueError,
- "History index cannot be negative");
- return NULL;
- }
- old_entry = replace_history_entry(entry_number, line, (void *)NULL);
- if (!old_entry) {
- PyErr_Format(PyExc_ValueError,
- "No history item at position %d",
- entry_number);
- return NULL;
- }
- /* free memory allocated for the old history entry */
- if (old_entry->line)
- free(old_entry->line);
- if (old_entry->data)
- free(old_entry->data);
- free(old_entry);
-
- Py_RETURN_NONE;
+ int entry_number;
+ char *line;
+ HIST_ENTRY *old_entry;
+
+ if (!PyArg_ParseTuple(args, "is:replace_history", &entry_number,
+ &line)) {
+ return NULL;
+ }
+ if (entry_number < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "History index cannot be negative");
+ return NULL;
+ }
+ old_entry = replace_history_entry(entry_number, line, (void *)NULL);
+ if (!old_entry) {
+ PyErr_Format(PyExc_ValueError,
+ "No history item at position %d",
+ entry_number);
+ return NULL;
+ }
+ /* free memory allocated for the old history entry */
+ if (old_entry->line)
+ free(old_entry->line);
+ if (old_entry->data)
+ free(old_entry->data);
+ free(old_entry);
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_replace_history,
@@ -410,13 +410,13 @@ replaces history item given by its position with contents of line");
static PyObject *
py_add_history(PyObject *self, PyObject *args)
{
- char *line;
+ char *line;
- if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
- return NULL;
- }
- add_history(line);
- Py_RETURN_NONE;
+ if(!PyArg_ParseTuple(args, "s:add_history", &line)) {
+ return NULL;
+ }
+ add_history(line);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_add_history,
@@ -429,7 +429,7 @@ add a line to the history buffer");
static PyObject *
get_completer_delims(PyObject *self, PyObject *noarg)
{
- return PyUnicode_FromString(rl_completer_word_break_characters);
+ return PyUnicode_FromString(rl_completer_word_break_characters);
}
PyDoc_STRVAR(doc_get_completer_delims,
@@ -442,7 +442,7 @@ get the readline word delimiters for tab-completion");
static PyObject *
set_completer(PyObject *self, PyObject *args)
{
- return set_hook("completer", &completer, args);
+ return set_hook("completer", &completer, args);
}
PyDoc_STRVAR(doc_set_completer,
@@ -456,11 +456,11 @@ It should return the next possible completion starting with 'text'.");
static PyObject *
get_completer(PyObject *self, PyObject *noargs)
{
- if (completer == NULL) {
- Py_RETURN_NONE;
- }
- Py_INCREF(completer);
- return completer;
+ if (completer == NULL) {
+ Py_RETURN_NONE;
+ }
+ Py_INCREF(completer);
+ return completer;
}
PyDoc_STRVAR(doc_get_completer,
@@ -473,16 +473,16 @@ Returns current completer function.");
static PyObject *
get_history_item(PyObject *self, PyObject *args)
{
- int idx = 0;
- HIST_ENTRY *hist_ent;
-
- if (!PyArg_ParseTuple(args, "i:index", &idx))
- return NULL;
- if ((hist_ent = history_get(idx)))
- return PyUnicode_FromString(hist_ent->line);
- else {
- Py_RETURN_NONE;
- }
+ int idx = 0;
+ HIST_ENTRY *hist_ent;
+
+ if (!PyArg_ParseTuple(args, "i:index", &idx))
+ return NULL;
+ if ((hist_ent = history_get(idx)))
+ return PyUnicode_FromString(hist_ent->line);
+ else {
+ Py_RETURN_NONE;
+ }
}
PyDoc_STRVAR(doc_get_history_item,
@@ -495,10 +495,10 @@ return the current contents of history item at index.");
static PyObject *
get_current_history_length(PyObject *self, PyObject *noarg)
{
- HISTORY_STATE *hist_st;
+ HISTORY_STATE *hist_st;
- hist_st = history_get_history_state();
- return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
+ hist_st = history_get_history_state();
+ return PyLong_FromLong(hist_st ? (long) hist_st->length : (long) 0);
}
PyDoc_STRVAR(doc_get_current_history_length,
@@ -511,7 +511,7 @@ return the current (not the maximum) length of history.");
static PyObject *
get_line_buffer(PyObject *self, PyObject *noarg)
{
- return PyUnicode_FromString(rl_line_buffer);
+ return PyUnicode_FromString(rl_line_buffer);
}
PyDoc_STRVAR(doc_get_line_buffer,
@@ -526,8 +526,8 @@ return the current contents of the line buffer.");
static PyObject *
py_clear_history(PyObject *self, PyObject *noarg)
{
- clear_history();
- Py_RETURN_NONE;
+ clear_history();
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_clear_history,
@@ -541,11 +541,11 @@ Clear the current readline history.");
static PyObject *
insert_text(PyObject *self, PyObject *args)
{
- char *s;
- if (!PyArg_ParseTuple(args, "s:insert_text", &s))
- return NULL;
- rl_insert_text(s);
- Py_RETURN_NONE;
+ char *s;
+ if (!PyArg_ParseTuple(args, "s:insert_text", &s))
+ return NULL;
+ rl_insert_text(s);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_insert_text,
@@ -558,8 +558,8 @@ Insert text into the command line.");
static PyObject *
redisplay(PyObject *self, PyObject *noarg)
{
- rl_redisplay();
- Py_RETURN_NONE;
+ rl_redisplay();
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(doc_redisplay,
@@ -572,50 +572,50 @@ contents of the line buffer.");
static struct PyMethodDef readline_methods[] =
{
- {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
- {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
- {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
- {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
- {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
- {"read_history_file", read_history_file,
- METH_VARARGS, doc_read_history_file},
- {"write_history_file", write_history_file,
- METH_VARARGS, doc_write_history_file},
- {"get_history_item", get_history_item,
- METH_VARARGS, doc_get_history_item},
- {"get_current_history_length", (PyCFunction)get_current_history_length,
- METH_NOARGS, doc_get_current_history_length},
- {"set_history_length", set_history_length,
- METH_VARARGS, set_history_length_doc},
- {"get_history_length", get_history_length,
- METH_NOARGS, get_history_length_doc},
- {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
- {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
- {"get_completion_type", get_completion_type,
- METH_NOARGS, doc_get_completion_type},
- {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
- {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
-
- {"set_completer_delims", set_completer_delims,
- METH_VARARGS, doc_set_completer_delims},
- {"add_history", py_add_history, METH_VARARGS, doc_add_history},
- {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
- {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
- {"get_completer_delims", get_completer_delims,
- METH_NOARGS, doc_get_completer_delims},
-
- {"set_completion_display_matches_hook", set_completion_display_matches_hook,
- METH_VARARGS, doc_set_completion_display_matches_hook},
- {"set_startup_hook", set_startup_hook,
- METH_VARARGS, doc_set_startup_hook},
+ {"parse_and_bind", parse_and_bind, METH_VARARGS, doc_parse_and_bind},
+ {"get_line_buffer", get_line_buffer, METH_NOARGS, doc_get_line_buffer},
+ {"insert_text", insert_text, METH_VARARGS, doc_insert_text},
+ {"redisplay", redisplay, METH_NOARGS, doc_redisplay},
+ {"read_init_file", read_init_file, METH_VARARGS, doc_read_init_file},
+ {"read_history_file", read_history_file,
+ METH_VARARGS, doc_read_history_file},
+ {"write_history_file", write_history_file,
+ METH_VARARGS, doc_write_history_file},
+ {"get_history_item", get_history_item,
+ METH_VARARGS, doc_get_history_item},
+ {"get_current_history_length", (PyCFunction)get_current_history_length,
+ METH_NOARGS, doc_get_current_history_length},
+ {"set_history_length", set_history_length,
+ METH_VARARGS, set_history_length_doc},
+ {"get_history_length", get_history_length,
+ METH_NOARGS, get_history_length_doc},
+ {"set_completer", set_completer, METH_VARARGS, doc_set_completer},
+ {"get_completer", get_completer, METH_NOARGS, doc_get_completer},
+ {"get_completion_type", get_completion_type,
+ METH_NOARGS, doc_get_completion_type},
+ {"get_begidx", get_begidx, METH_NOARGS, doc_get_begidx},
+ {"get_endidx", get_endidx, METH_NOARGS, doc_get_endidx},
+
+ {"set_completer_delims", set_completer_delims,
+ METH_VARARGS, doc_set_completer_delims},
+ {"add_history", py_add_history, METH_VARARGS, doc_add_history},
+ {"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
+ {"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
+ {"get_completer_delims", get_completer_delims,
+ METH_NOARGS, doc_get_completer_delims},
+
+ {"set_completion_display_matches_hook", set_completion_display_matches_hook,
+ METH_VARARGS, doc_set_completion_display_matches_hook},
+ {"set_startup_hook", set_startup_hook,
+ METH_VARARGS, doc_set_startup_hook},
#ifdef HAVE_RL_PRE_INPUT_HOOK
- {"set_pre_input_hook", set_pre_input_hook,
- METH_VARARGS, doc_set_pre_input_hook},
+ {"set_pre_input_hook", set_pre_input_hook,
+ METH_VARARGS, doc_set_pre_input_hook},
#endif
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
- {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
+ {"clear_history", py_clear_history, METH_NOARGS, doc_clear_history},
#endif
- {0, 0}
+ {0, 0}
};
@@ -624,47 +624,47 @@ static struct PyMethodDef readline_methods[] =
static int
on_hook(PyObject *func)
{
- int result = 0;
- if (func != NULL) {
- PyObject *r;
+ int result = 0;
+ if (func != NULL) {
+ PyObject *r;
#ifdef WITH_THREAD
- PyGILState_STATE gilstate = PyGILState_Ensure();
+ PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
- r = PyObject_CallFunction(func, NULL);
- if (r == NULL)
- goto error;
- if (r == Py_None)
- result = 0;
- else {
- result = PyLong_AsLong(r);
- if (result == -1 && PyErr_Occurred())
- goto error;
- }
- Py_DECREF(r);
- goto done;
- error:
- PyErr_Clear();
- Py_XDECREF(r);
- done:
+ r = PyObject_CallFunction(func, NULL);
+ if (r == NULL)
+ goto error;
+ if (r == Py_None)
+ result = 0;
+ else {
+ result = PyLong_AsLong(r);
+ if (result == -1 && PyErr_Occurred())
+ goto error;
+ }
+ Py_DECREF(r);
+ goto done;
+ error:
+ PyErr_Clear();
+ Py_XDECREF(r);
+ done:
#ifdef WITH_THREAD
- PyGILState_Release(gilstate);
+ PyGILState_Release(gilstate);
#endif
- return result;
- }
- return result;
+ return result;
+ }
+ return result;
}
static int
on_startup_hook(void)
{
- return on_hook(startup_hook);
+ return on_hook(startup_hook);
}
#ifdef HAVE_RL_PRE_INPUT_HOOK
static int
on_pre_input_hook(void)
{
- return on_hook(pre_input_hook);
+ return on_hook(pre_input_hook);
}
#endif
@@ -673,42 +673,42 @@ on_pre_input_hook(void)
static void
on_completion_display_matches_hook(char **matches,
- int num_matches, int max_length)
+ int num_matches, int max_length)
{
- int i;
- PyObject *m=NULL, *s=NULL, *r=NULL;
+ int i;
+ PyObject *m=NULL, *s=NULL, *r=NULL;
#ifdef WITH_THREAD
- PyGILState_STATE gilstate = PyGILState_Ensure();
+ PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
- m = PyList_New(num_matches);
- if (m == NULL)
- goto error;
- for (i = 0; i < num_matches; i++) {
- s = PyUnicode_FromString(matches[i+1]);
- if (s == NULL)
- goto error;
- if (PyList_SetItem(m, i, s) == -1)
- goto error;
- }
- r = PyObject_CallFunction(completion_display_matches_hook,
- "sOi", matches[0], m, max_length);
-
- Py_DECREF(m); m=NULL;
-
- if (r == NULL ||
- (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
- goto error;
- }
- Py_XDECREF(r); r=NULL;
-
- if (0) {
- error:
- PyErr_Clear();
- Py_XDECREF(m);
- Py_XDECREF(r);
- }
+ m = PyList_New(num_matches);
+ if (m == NULL)
+ goto error;
+ for (i = 0; i < num_matches; i++) {
+ s = PyUnicode_FromString(matches[i+1]);
+ if (s == NULL)
+ goto error;
+ if (PyList_SetItem(m, i, s) == -1)
+ goto error;
+ }
+ r = PyObject_CallFunction(completion_display_matches_hook,
+ "sOi", matches[0], m, max_length);
+
+ Py_DECREF(m); m=NULL;
+
+ if (r == NULL ||
+ (r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
+ goto error;
+ }
+ Py_XDECREF(r); r=NULL;
+
+ if (0) {
+ error:
+ PyErr_Clear();
+ Py_XDECREF(m);
+ Py_XDECREF(r);
+ }
#ifdef WITH_THREAD
- PyGILState_Release(gilstate);
+ PyGILState_Release(gilstate);
#endif
}
@@ -718,37 +718,37 @@ on_completion_display_matches_hook(char **matches,
static char *
on_completion(const char *text, int state)
{
- char *result = NULL;
- if (completer != NULL) {
- PyObject *r;
+ char *result = NULL;
+ if (completer != NULL) {
+ PyObject *r;
#ifdef WITH_THREAD
- PyGILState_STATE gilstate = PyGILState_Ensure();
+ PyGILState_STATE gilstate = PyGILState_Ensure();
#endif
- rl_attempted_completion_over = 1;
- r = PyObject_CallFunction(completer, "si", text, state);
- if (r == NULL)
- goto error;
- if (r == Py_None) {
- result = NULL;
- }
- else {
- char *s = _PyUnicode_AsString(r);
- if (s == NULL)
- goto error;
- result = strdup(s);
- }
- Py_DECREF(r);
- goto done;
- error:
- PyErr_Clear();
- Py_XDECREF(r);
- done:
+ rl_attempted_completion_over = 1;
+ r = PyObject_CallFunction(completer, "si", text, state);
+ if (r == NULL)
+ goto error;
+ if (r == Py_None) {
+ result = NULL;
+ }
+ else {
+ char *s = _PyUnicode_AsString(r);
+ if (s == NULL)
+ goto error;
+ result = strdup(s);
+ }
+ Py_DECREF(r);
+ goto done;
+ error:
+ PyErr_Clear();
+ Py_XDECREF(r);
+ done:
#ifdef WITH_THREAD
- PyGILState_Release(gilstate);
+ PyGILState_Release(gilstate);
#endif
- return result;
- }
- return result;
+ return result;
+ }
+ return result;
}
@@ -759,16 +759,16 @@ static char **
flex_complete(char *text, int start, int end)
{
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
- rl_completion_append_character ='\0';
+ rl_completion_append_character ='\0';
#endif
#ifdef HAVE_RL_COMPLETION_SUPPRESS_APPEND
- rl_completion_suppress_append = 0;
+ rl_completion_suppress_append = 0;
#endif
- Py_XDECREF(begidx);
- Py_XDECREF(endidx);
- begidx = PyLong_FromLong((long) start);
- endidx = PyLong_FromLong((long) end);
- return completion_matches(text, *on_completion);
+ Py_XDECREF(begidx);
+ Py_XDECREF(endidx);
+ begidx = PyLong_FromLong((long) start);
+ endidx = PyLong_FromLong((long) end);
+ return completion_matches(text, *on_completion);
}
@@ -778,45 +778,45 @@ static void
setup_readline(void)
{
#ifdef SAVE_LOCALE
- char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
- if (!saved_locale)
- Py_FatalError("not enough memory to save locale");
+ char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
+ if (!saved_locale)
+ Py_FatalError("not enough memory to save locale");
#endif
- using_history();
+ using_history();
- rl_readline_name = "python";
+ rl_readline_name = "python";
#if defined(PYOS_OS2) && defined(PYCC_GCC)
- /* Allow $if term= in .inputrc to work */
- rl_terminal_name = getenv("TERM");
+ /* Allow $if term= in .inputrc to work */
+ rl_terminal_name = getenv("TERM");
#endif
- /* Force rebind of TAB to insert-tab */
- rl_bind_key('\t', rl_insert);
- /* Bind both ESC-TAB and ESC-ESC to the completion function */
- rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
- rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
- /* Set our hook functions */
- rl_startup_hook = (Function *)on_startup_hook;
+ /* Force rebind of TAB to insert-tab */
+ rl_bind_key('\t', rl_insert);
+ /* Bind both ESC-TAB and ESC-ESC to the completion function */
+ rl_bind_key_in_map ('\t', rl_complete, emacs_meta_keymap);
+ rl_bind_key_in_map ('\033', rl_complete, emacs_meta_keymap);
+ /* Set our hook functions */
+ rl_startup_hook = (Function *)on_startup_hook;
#ifdef HAVE_RL_PRE_INPUT_HOOK
- rl_pre_input_hook = (Function *)on_pre_input_hook;
+ rl_pre_input_hook = (Function *)on_pre_input_hook;
#endif
- /* Set our completion function */
- rl_attempted_completion_function = (CPPFunction *)flex_complete;
- /* Set Python word break characters */
- rl_completer_word_break_characters =
- strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
- /* All nonalphanums except '.' */
-
- begidx = PyLong_FromLong(0L);
- endidx = PyLong_FromLong(0L);
- /* Initialize (allows .inputrc to override)
- *
- * XXX: A bug in the readline-2.2 library causes a memory leak
- * inside this function. Nothing we can do about it.
- */
- rl_initialize();
-
- RESTORE_LOCALE(saved_locale)
+ /* Set our completion function */
+ rl_attempted_completion_function = (CPPFunction *)flex_complete;
+ /* Set Python word break characters */
+ rl_completer_word_break_characters =
+ strdup(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?");
+ /* All nonalphanums except '.' */
+
+ begidx = PyLong_FromLong(0L);
+ endidx = PyLong_FromLong(0L);
+ /* Initialize (allows .inputrc to override)
+ *
+ * XXX: A bug in the readline-2.2 library causes a memory leak
+ * inside this function. Nothing we can do about it.
+ */
+ rl_initialize();
+
+ RESTORE_LOCALE(saved_locale)
}
/* Wrapper around GNU readline that handles signals differently. */
@@ -824,12 +824,12 @@ setup_readline(void)
#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)
-static char *completed_input_string;
+static char *completed_input_string;
static void
rlhandler(char *text)
{
- completed_input_string = text;
- rl_callback_handler_remove();
+ completed_input_string = text;
+ rl_callback_handler_remove();
}
extern PyThreadState* _PyOS_ReadlineTState;
@@ -837,60 +837,60 @@ extern PyThreadState* _PyOS_ReadlineTState;
static char *
readline_until_enter_or_signal(char *prompt, int *signal)
{
- char * not_done_reading = "";
- fd_set selectset;
+ char * not_done_reading = "";
+ fd_set selectset;
- *signal = 0;
+ *signal = 0;
#ifdef HAVE_RL_CATCH_SIGNAL
- rl_catch_signals = 0;
+ rl_catch_signals = 0;
#endif
- rl_callback_handler_install (prompt, rlhandler);
- FD_ZERO(&selectset);
-
- completed_input_string = not_done_reading;
-
- while (completed_input_string == not_done_reading) {
- int has_input = 0;
-
- while (!has_input)
- { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
-
- /* [Bug #1552726] Only limit the pause if an input hook has been
- defined. */
- struct timeval *timeoutp = NULL;
- if (PyOS_InputHook)
- timeoutp = &timeout;
- FD_SET(fileno(rl_instream), &selectset);
- /* select resets selectset if no input was available */
- has_input = select(fileno(rl_instream) + 1, &selectset,
- NULL, NULL, timeoutp);
- if(PyOS_InputHook) PyOS_InputHook();
- }
-
- if(has_input > 0) {
- rl_callback_read_char();
- }
- else if (errno == EINTR) {
- int s;
+ rl_callback_handler_install (prompt, rlhandler);
+ FD_ZERO(&selectset);
+
+ completed_input_string = not_done_reading;
+
+ while (completed_input_string == not_done_reading) {
+ int has_input = 0;
+
+ while (!has_input)
+ { struct timeval timeout = {0, 100000}; /* 0.1 seconds */
+
+ /* [Bug #1552726] Only limit the pause if an input hook has been
+ defined. */
+ struct timeval *timeoutp = NULL;
+ if (PyOS_InputHook)
+ timeoutp = &timeout;
+ FD_SET(fileno(rl_instream), &selectset);
+ /* select resets selectset if no input was available */
+ has_input = select(fileno(rl_instream) + 1, &selectset,
+ NULL, NULL, timeoutp);
+ if(PyOS_InputHook) PyOS_InputHook();
+ }
+
+ if(has_input > 0) {
+ rl_callback_read_char();
+ }
+ else if (errno == EINTR) {
+ int s;
#ifdef WITH_THREAD
- PyEval_RestoreThread(_PyOS_ReadlineTState);
+ PyEval_RestoreThread(_PyOS_ReadlineTState);
#endif
- s = PyErr_CheckSignals();
+ s = PyErr_CheckSignals();
#ifdef WITH_THREAD
- PyEval_SaveThread();
+ PyEval_SaveThread();
#endif
- if (s < 0) {
- rl_free_line_state();
- rl_cleanup_after_signal();
- rl_callback_handler_remove();
- *signal = 1;
- completed_input_string = NULL;
- }
- }
- }
-
- return completed_input_string;
+ if (s < 0) {
+ rl_free_line_state();
+ rl_cleanup_after_signal();
+ rl_callback_handler_remove();
+ *signal = 1;
+ completed_input_string = NULL;
+ }
+ }
+ }
+
+ return completed_input_string;
}
@@ -904,31 +904,31 @@ static jmp_buf jbuf;
static void
onintr(int sig)
{
- longjmp(jbuf, 1);
+ longjmp(jbuf, 1);
}
static char *
readline_until_enter_or_signal(char *prompt, int *signal)
{
- PyOS_sighandler_t old_inthandler;
- char *p;
-
- *signal = 0;
+ PyOS_sighandler_t old_inthandler;
+ char *p;
- old_inthandler = PyOS_setsig(SIGINT, onintr);
- if (setjmp(jbuf)) {
+ *signal = 0;
+
+ old_inthandler = PyOS_setsig(SIGINT, onintr);
+ if (setjmp(jbuf)) {
#ifdef HAVE_SIGRELSE
- /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
- sigrelse(SIGINT);
+ /* This seems necessary on SunOS 4.1 (Rasmus Hahn) */
+ sigrelse(SIGINT);
#endif
- PyOS_setsig(SIGINT, old_inthandler);
- *signal = 1;
- return NULL;
- }
- rl_event_hook = PyOS_InputHook;
- p = readline(prompt);
- PyOS_setsig(SIGINT, old_inthandler);
+ PyOS_setsig(SIGINT, old_inthandler);
+ *signal = 1;
+ return NULL;
+ }
+ rl_event_hook = PyOS_InputHook;
+ p = readline(prompt);
+ PyOS_setsig(SIGINT, old_inthandler);
return p;
}
@@ -938,73 +938,73 @@ readline_until_enter_or_signal(char *prompt, int *signal)
static char *
call_readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
{
- size_t n;
- char *p, *q;
- int signal;
+ size_t n;
+ char *p, *q;
+ int signal;
#ifdef SAVE_LOCALE
- char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
- if (!saved_locale)
- Py_FatalError("not enough memory to save locale");
- setlocale(LC_CTYPE, "");
+ char *saved_locale = strdup(setlocale(LC_CTYPE, NULL));
+ if (!saved_locale)
+ Py_FatalError("not enough memory to save locale");
+ setlocale(LC_CTYPE, "");
#endif
- if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
- rl_instream = sys_stdin;
- rl_outstream = sys_stdout;
+ if (sys_stdin != rl_instream || sys_stdout != rl_outstream) {
+ rl_instream = sys_stdin;
+ rl_outstream = sys_stdout;
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
- rl_prep_terminal (1);
+ rl_prep_terminal (1);
#endif
- }
-
- p = readline_until_enter_or_signal(prompt, &signal);
-
- /* we got an interrupt signal */
- if (signal) {
- RESTORE_LOCALE(saved_locale)
- return NULL;
- }
-
- /* We got an EOF, return a empty string. */
- if (p == NULL) {
- p = PyMem_Malloc(1);
- if (p != NULL)
- *p = '\0';
- RESTORE_LOCALE(saved_locale)
- return p;
- }
-
- /* we have a valid line */
- n = strlen(p);
- if (n > 0) {
- char *line;
- HISTORY_STATE *state = history_get_history_state();
- if (state->length > 0)
- line = history_get(state->length)->line;
- else
- line = "";
- if (strcmp(p, line))
- add_history(p);
- /* the history docs don't say so, but the address of state
- changes each time history_get_history_state is called
- which makes me think it's freshly malloc'd memory...
- on the other hand, the address of the last line stays the
- same as long as history isn't extended, so it appears to
- be malloc'd but managed by the history package... */
- free(state);
- }
- /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
- release the original. */
- q = p;
- p = PyMem_Malloc(n+2);
- if (p != NULL) {
- strncpy(p, q, n);
- p[n] = '\n';
- p[n+1] = '\0';
- }
- free(q);
- RESTORE_LOCALE(saved_locale)
- return p;
+ }
+
+ p = readline_until_enter_or_signal(prompt, &signal);
+
+ /* we got an interrupt signal */
+ if (signal) {
+ RESTORE_LOCALE(saved_locale)
+ return NULL;
+ }
+
+ /* We got an EOF, return a empty string. */
+ if (p == NULL) {
+ p = PyMem_Malloc(1);
+ if (p != NULL)
+ *p = '\0';
+ RESTORE_LOCALE(saved_locale)
+ return p;
+ }
+
+ /* we have a valid line */
+ n = strlen(p);
+ if (n > 0) {
+ char *line;
+ HISTORY_STATE *state = history_get_history_state();
+ if (state->length > 0)
+ line = history_get(state->length)->line;
+ else
+ line = "";
+ if (strcmp(p, line))
+ add_history(p);
+ /* the history docs don't say so, but the address of state
+ changes each time history_get_history_state is called
+ which makes me think it's freshly malloc'd memory...
+ on the other hand, the address of the last line stays the
+ same as long as history isn't extended, so it appears to
+ be malloc'd but managed by the history package... */
+ free(state);
+ }
+ /* Copy the malloc'ed buffer into a PyMem_Malloc'ed one and
+ release the original. */
+ q = p;
+ p = PyMem_Malloc(n+2);
+ if (p != NULL) {
+ strncpy(p, q, n);
+ p[n] = '\n';
+ p[n+1] = '\0';
+ }
+ free(q);
+ RESTORE_LOCALE(saved_locale)
+ return p;
}
@@ -1015,27 +1015,27 @@ PyDoc_STRVAR(doc_module,
static struct PyModuleDef readlinemodule = {
- PyModuleDef_HEAD_INIT,
- "readline",
- doc_module,
- -1,
- readline_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "readline",
+ doc_module,
+ -1,
+ readline_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_readline(void)
{
- PyObject *m;
+ PyObject *m;
- m = PyModule_Create(&readlinemodule);
- if (m == NULL)
- return NULL;
+ m = PyModule_Create(&readlinemodule);
+ if (m == NULL)
+ return NULL;
- PyOS_ReadlineFunctionPointer = call_readline;
- setup_readline();
- return m;
+ PyOS_ReadlineFunctionPointer = call_readline;
+ setup_readline();
+ return m;
}
diff --git a/Modules/resource.c b/Modules/resource.c
index 22e177f..6d8bd57 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -29,30 +29,30 @@ PyDoc_STRVAR(struct_rusage__doc__,
"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
static PyStructSequence_Field struct_rusage_fields[] = {
- {"ru_utime", "user time used"},
- {"ru_stime", "system time used"},
- {"ru_maxrss", "max. resident set size"},
- {"ru_ixrss", "shared memory size"},
- {"ru_idrss", "unshared data size"},
- {"ru_isrss", "unshared stack size"},
- {"ru_minflt", "page faults not requiring I/O"},
- {"ru_majflt", "page faults requiring I/O"},
- {"ru_nswap", "number of swap outs"},
- {"ru_inblock", "block input operations"},
- {"ru_oublock", "block output operations"},
- {"ru_msgsnd", "IPC messages sent"},
- {"ru_msgrcv", "IPC messages received"},
- {"ru_nsignals", "signals received"},
- {"ru_nvcsw", "voluntary context switches"},
- {"ru_nivcsw", "involuntary context switches"},
- {0}
+ {"ru_utime", "user time used"},
+ {"ru_stime", "system time used"},
+ {"ru_maxrss", "max. resident set size"},
+ {"ru_ixrss", "shared memory size"},
+ {"ru_idrss", "unshared data size"},
+ {"ru_isrss", "unshared stack size"},
+ {"ru_minflt", "page faults not requiring I/O"},
+ {"ru_majflt", "page faults requiring I/O"},
+ {"ru_nswap", "number of swap outs"},
+ {"ru_inblock", "block input operations"},
+ {"ru_oublock", "block output operations"},
+ {"ru_msgsnd", "IPC messages sent"},
+ {"ru_msgrcv", "IPC messages received"},
+ {"ru_nsignals", "signals received"},
+ {"ru_nvcsw", "voluntary context switches"},
+ {"ru_nivcsw", "involuntary context switches"},
+ {0}
};
static PyStructSequence_Desc struct_rusage_desc = {
- "resource.struct_rusage", /* name */
- struct_rusage__doc__, /* doc */
- struct_rusage_fields, /* fields */
- 16 /* n_in_sequence */
+ "resource.struct_rusage", /* name */
+ struct_rusage__doc__, /* doc */
+ struct_rusage_fields, /* fields */
+ 16 /* n_in_sequence */
};
static int initialized;
@@ -61,151 +61,151 @@ static PyTypeObject StructRUsageType;
static PyObject *
resource_getrusage(PyObject *self, PyObject *args)
{
- int who;
- struct rusage ru;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args, "i:getrusage", &who))
- return NULL;
-
- if (getrusage(who, &ru) == -1) {
- if (errno == EINVAL) {
- PyErr_SetString(PyExc_ValueError,
- "invalid who parameter");
- return NULL;
- }
- PyErr_SetFromErrno(ResourceError);
- return NULL;
- }
-
- result = PyStructSequence_New(&StructRUsageType);
- if (!result)
- return NULL;
-
- PyStructSequence_SET_ITEM(result, 0,
- PyFloat_FromDouble(doubletime(ru.ru_utime)));
- PyStructSequence_SET_ITEM(result, 1,
- PyFloat_FromDouble(doubletime(ru.ru_stime)));
- PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss));
- PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss));
- PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss));
- PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss));
- PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt));
- PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt));
- PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap));
- PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock));
- PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock));
- PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd));
- PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv));
- PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals));
- PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw));
- PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw));
-
- if (PyErr_Occurred()) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ int who;
+ struct rusage ru;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "i:getrusage", &who))
+ return NULL;
+
+ if (getrusage(who, &ru) == -1) {
+ if (errno == EINVAL) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid who parameter");
+ return NULL;
+ }
+ PyErr_SetFromErrno(ResourceError);
+ return NULL;
+ }
+
+ result = PyStructSequence_New(&StructRUsageType);
+ if (!result)
+ return NULL;
+
+ PyStructSequence_SET_ITEM(result, 0,
+ PyFloat_FromDouble(doubletime(ru.ru_utime)));
+ PyStructSequence_SET_ITEM(result, 1,
+ PyFloat_FromDouble(doubletime(ru.ru_stime)));
+ PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss));
+ PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss));
+ PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss));
+ PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss));
+ PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt));
+ PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt));
+ PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap));
+ PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock));
+ PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock));
+ PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd));
+ PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv));
+ PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals));
+ PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw));
+ PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw));
+
+ if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ return result;
}
static PyObject *
resource_getrlimit(PyObject *self, PyObject *args)
{
- struct rlimit rl;
- int resource;
+ struct rlimit rl;
+ int resource;
- if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
+ return NULL;
- if (resource < 0 || resource >= RLIM_NLIMITS) {
- PyErr_SetString(PyExc_ValueError,
- "invalid resource specified");
- return NULL;
- }
+ if (resource < 0 || resource >= RLIM_NLIMITS) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid resource specified");
+ return NULL;
+ }
- if (getrlimit(resource, &rl) == -1) {
- PyErr_SetFromErrno(ResourceError);
- return NULL;
- }
+ if (getrlimit(resource, &rl) == -1) {
+ PyErr_SetFromErrno(ResourceError);
+ return NULL;
+ }
#if defined(HAVE_LONG_LONG)
- if (sizeof(rl.rlim_cur) > sizeof(long)) {
- return Py_BuildValue("LL",
- (PY_LONG_LONG) rl.rlim_cur,
- (PY_LONG_LONG) rl.rlim_max);
- }
+ if (sizeof(rl.rlim_cur) > sizeof(long)) {
+ return Py_BuildValue("LL",
+ (PY_LONG_LONG) rl.rlim_cur,
+ (PY_LONG_LONG) rl.rlim_max);
+ }
#endif
- return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
+ return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
}
static PyObject *
resource_setrlimit(PyObject *self, PyObject *args)
{
- struct rlimit rl;
- int resource;
- PyObject *curobj, *maxobj;
+ struct rlimit rl;
+ int resource;
+ PyObject *curobj, *maxobj;
- if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
- &resource, &curobj, &maxobj))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
+ &resource, &curobj, &maxobj))
+ return NULL;
- if (resource < 0 || resource >= RLIM_NLIMITS) {
- PyErr_SetString(PyExc_ValueError,
- "invalid resource specified");
- return NULL;
- }
+ if (resource < 0 || resource >= RLIM_NLIMITS) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid resource specified");
+ return NULL;
+ }
#if !defined(HAVE_LARGEFILE_SUPPORT)
- rl.rlim_cur = PyLong_AsLong(curobj);
- if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
- rl.rlim_max = PyLong_AsLong(maxobj);
- if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ rl.rlim_cur = PyLong_AsLong(curobj);
+ if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
+ return NULL;
+ rl.rlim_max = PyLong_AsLong(maxobj);
+ if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
+ return NULL;
#else
- /* The limits are probably bigger than a long */
- rl.rlim_cur = PyLong_AsLongLong(curobj);
- if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
- rl.rlim_max = PyLong_AsLongLong(maxobj);
- if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ /* The limits are probably bigger than a long */
+ rl.rlim_cur = PyLong_AsLongLong(curobj);
+ if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
+ return NULL;
+ rl.rlim_max = PyLong_AsLongLong(maxobj);
+ if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
+ return NULL;
#endif
- rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
- rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
- if (setrlimit(resource, &rl) == -1) {
- if (errno == EINVAL)
- PyErr_SetString(PyExc_ValueError,
- "current limit exceeds maximum limit");
- else if (errno == EPERM)
- PyErr_SetString(PyExc_ValueError,
- "not allowed to raise maximum limit");
- else
- PyErr_SetFromErrno(ResourceError);
- return NULL;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
+ rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
+ if (setrlimit(resource, &rl) == -1) {
+ if (errno == EINVAL)
+ PyErr_SetString(PyExc_ValueError,
+ "current limit exceeds maximum limit");
+ else if (errno == EPERM)
+ PyErr_SetString(PyExc_ValueError,
+ "not allowed to raise maximum limit");
+ else
+ PyErr_SetFromErrno(ResourceError);
+ return NULL;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
resource_getpagesize(PyObject *self, PyObject *unused)
{
- long pagesize = 0;
+ long pagesize = 0;
#if defined(HAVE_GETPAGESIZE)
- pagesize = getpagesize();
+ pagesize = getpagesize();
#elif defined(HAVE_SYSCONF)
#if defined(_SC_PAGE_SIZE)
- pagesize = sysconf(_SC_PAGE_SIZE);
+ pagesize = sysconf(_SC_PAGE_SIZE);
#else
- /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
- pagesize = sysconf(_SC_PAGESIZE);
+ /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
+ pagesize = sysconf(_SC_PAGESIZE);
#endif
#endif
- return Py_BuildValue("i", pagesize);
+ return Py_BuildValue("i", pagesize);
}
@@ -213,11 +213,11 @@ resource_getpagesize(PyObject *self, PyObject *unused)
static struct PyMethodDef
resource_methods[] = {
- {"getrusage", resource_getrusage, METH_VARARGS},
- {"getrlimit", resource_getrlimit, METH_VARARGS},
- {"setrlimit", resource_setrlimit, METH_VARARGS},
- {"getpagesize", resource_getpagesize, METH_NOARGS},
- {NULL, NULL} /* sentinel */
+ {"getrusage", resource_getrusage, METH_VARARGS},
+ {"getrlimit", resource_getrlimit, METH_VARARGS},
+ {"setrlimit", resource_setrlimit, METH_VARARGS},
+ {"getpagesize", resource_getpagesize, METH_NOARGS},
+ {NULL, NULL} /* sentinel */
};
@@ -225,117 +225,117 @@ resource_methods[] = {
static struct PyModuleDef resourcemodule = {
- PyModuleDef_HEAD_INIT,
- "resource",
- NULL,
- -1,
- resource_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "resource",
+ NULL,
+ -1,
+ resource_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_resource(void)
{
- PyObject *m, *v;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&resourcemodule);
- if (m == NULL)
- return NULL;
-
- /* Add some symbolic constants to the module */
- if (ResourceError == NULL) {
- ResourceError = PyErr_NewException("resource.error",
- NULL, NULL);
- }
- Py_INCREF(ResourceError);
- PyModule_AddObject(m, "error", ResourceError);
- if (!initialized)
- PyStructSequence_InitType(&StructRUsageType,
- &struct_rusage_desc);
- Py_INCREF(&StructRUsageType);
- PyModule_AddObject(m, "struct_rusage",
- (PyObject*) &StructRUsageType);
-
- /* insert constants */
+ PyObject *m, *v;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&resourcemodule);
+ if (m == NULL)
+ return NULL;
+
+ /* Add some symbolic constants to the module */
+ if (ResourceError == NULL) {
+ ResourceError = PyErr_NewException("resource.error",
+ NULL, NULL);
+ }
+ Py_INCREF(ResourceError);
+ PyModule_AddObject(m, "error", ResourceError);
+ if (!initialized)
+ PyStructSequence_InitType(&StructRUsageType,
+ &struct_rusage_desc);
+ Py_INCREF(&StructRUsageType);
+ PyModule_AddObject(m, "struct_rusage",
+ (PyObject*) &StructRUsageType);
+
+ /* insert constants */
#ifdef RLIMIT_CPU
- PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
+ PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
#endif
#ifdef RLIMIT_FSIZE
- PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
+ PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
#endif
#ifdef RLIMIT_DATA
- PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
+ PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
#endif
#ifdef RLIMIT_STACK
- PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
+ PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
#endif
#ifdef RLIMIT_CORE
- PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
+ PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
#endif
#ifdef RLIMIT_NOFILE
- PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
+ PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
#endif
#ifdef RLIMIT_OFILE
- PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
+ PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
#endif
#ifdef RLIMIT_VMEM
- PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
+ PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
#endif
#ifdef RLIMIT_AS
- PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
+ PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
#endif
#ifdef RLIMIT_RSS
- PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
+ PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
#endif
#ifdef RLIMIT_NPROC
- PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
+ PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
#endif
#ifdef RLIMIT_MEMLOCK
- PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
+ PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
#endif
#ifdef RLIMIT_SBSIZE
- PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
+ PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
#endif
#ifdef RUSAGE_SELF
- PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
+ PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
#endif
#ifdef RUSAGE_CHILDREN
- PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
+ PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
#endif
#ifdef RUSAGE_BOTH
- PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
+ PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
#endif
#if defined(HAVE_LONG_LONG)
- if (sizeof(RLIM_INFINITY) > sizeof(long)) {
- v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
- } else
+ if (sizeof(RLIM_INFINITY) > sizeof(long)) {
+ v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
+ } else
#endif
- {
- v = PyLong_FromLong((long) RLIM_INFINITY);
- }
- if (v) {
- PyModule_AddObject(m, "RLIM_INFINITY", v);
- }
- initialized = 1;
- return m;
+ {
+ v = PyLong_FromLong((long) RLIM_INFINITY);
+ }
+ if (v) {
+ PyModule_AddObject(m, "RLIM_INFINITY", v);
+ }
+ initialized = 1;
+ return m;
}
diff --git a/Modules/rotatingtree.c b/Modules/rotatingtree.c
index 39c6dd5..07e08bc 100644
--- a/Modules/rotatingtree.c
+++ b/Modules/rotatingtree.c
@@ -14,14 +14,14 @@ static unsigned int random_stream = 0;
static int
randombits(int bits)
{
- int result;
- if (random_stream < (1U << bits)) {
- random_value *= 1082527;
- random_stream = random_value;
- }
- result = random_stream & ((1<<bits)-1);
- random_stream >>= bits;
- return result;
+ int result;
+ if (random_stream < (1U << bits)) {
+ random_value *= 1082527;
+ random_stream = random_value;
+ }
+ result = random_stream & ((1<<bits)-1);
+ random_stream >>= bits;
+ return result;
}
@@ -30,15 +30,15 @@ randombits(int bits)
void
RotatingTree_Add(rotating_node_t **root, rotating_node_t *node)
{
- while (*root != NULL) {
- if (KEY_LOWER_THAN(node->key, (*root)->key))
- root = &((*root)->left);
- else
- root = &((*root)->right);
- }
- node->left = NULL;
- node->right = NULL;
- *root = node;
+ while (*root != NULL) {
+ if (KEY_LOWER_THAN(node->key, (*root)->key))
+ root = &((*root)->left);
+ else
+ root = &((*root)->right);
+ }
+ node->left = NULL;
+ node->right = NULL;
+ *root = node;
}
/* Locate the node with the given key. This is the most complicated
@@ -47,57 +47,57 @@ RotatingTree_Add(rotating_node_t **root, rotating_node_t *node)
rotating_node_t *
RotatingTree_Get(rotating_node_t **root, void *key)
{
- if (randombits(3) != 4) {
- /* Fast path, no rebalancing */
- rotating_node_t *node = *root;
- while (node != NULL) {
- if (node->key == key)
- return node;
- if (KEY_LOWER_THAN(key, node->key))
- node = node->left;
- else
- node = node->right;
- }
- return NULL;
- }
- else {
- rotating_node_t **pnode = root;
- rotating_node_t *node = *pnode;
- rotating_node_t *next;
- int rotate;
- if (node == NULL)
- return NULL;
- while (1) {
- if (node->key == key)
- return node;
- rotate = !randombits(1);
- if (KEY_LOWER_THAN(key, node->key)) {
- next = node->left;
- if (next == NULL)
- return NULL;
- if (rotate) {
- node->left = next->right;
- next->right = node;
- *pnode = next;
- }
- else
- pnode = &(node->left);
- }
- else {
- next = node->right;
- if (next == NULL)
- return NULL;
- if (rotate) {
- node->right = next->left;
- next->left = node;
- *pnode = next;
- }
- else
- pnode = &(node->right);
- }
- node = next;
- }
- }
+ if (randombits(3) != 4) {
+ /* Fast path, no rebalancing */
+ rotating_node_t *node = *root;
+ while (node != NULL) {
+ if (node->key == key)
+ return node;
+ if (KEY_LOWER_THAN(key, node->key))
+ node = node->left;
+ else
+ node = node->right;
+ }
+ return NULL;
+ }
+ else {
+ rotating_node_t **pnode = root;
+ rotating_node_t *node = *pnode;
+ rotating_node_t *next;
+ int rotate;
+ if (node == NULL)
+ return NULL;
+ while (1) {
+ if (node->key == key)
+ return node;
+ rotate = !randombits(1);
+ if (KEY_LOWER_THAN(key, node->key)) {
+ next = node->left;
+ if (next == NULL)
+ return NULL;
+ if (rotate) {
+ node->left = next->right;
+ next->right = node;
+ *pnode = next;
+ }
+ else
+ pnode = &(node->left);
+ }
+ else {
+ next = node->right;
+ if (next == NULL)
+ return NULL;
+ if (rotate) {
+ node->right = next->left;
+ next->left = node;
+ *pnode = next;
+ }
+ else
+ pnode = &(node->right);
+ }
+ node = next;
+ }
+ }
}
/* Enumerate all nodes in the tree. The callback enumfn() should return
@@ -105,17 +105,17 @@ RotatingTree_Get(rotating_node_t **root, void *key)
A non-zero value is directly returned by RotatingTree_Enum(). */
int
RotatingTree_Enum(rotating_node_t *root, rotating_tree_enum_fn enumfn,
- void *arg)
+ void *arg)
{
- int result;
- rotating_node_t *node;
- while (root != NULL) {
- result = RotatingTree_Enum(root->left, enumfn, arg);
- if (result != 0) return result;
- node = root->right;
- result = enumfn(root, arg);
- if (result != 0) return result;
- root = node;
- }
- return 0;
+ int result;
+ rotating_node_t *node;
+ while (root != NULL) {
+ result = RotatingTree_Enum(root->left, enumfn, arg);
+ if (result != 0) return result;
+ node = root->right;
+ result = enumfn(root, arg);
+ if (result != 0) return result;
+ root = node;
+ }
+ return 0;
}
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 0fd3c48..ebfbb9c 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -22,7 +22,7 @@
*/
#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
#define FD_SETSIZE 512
-#endif
+#endif
#if defined(HAVE_POLL_H)
#include <poll.h>
@@ -58,20 +58,20 @@ static PyObject *SelectError;
/* list of Python objects and their file descriptor */
typedef struct {
- PyObject *obj; /* owned reference */
- SOCKET fd;
- int sentinel; /* -1 == sentinel */
+ PyObject *obj; /* owned reference */
+ SOCKET fd;
+ int sentinel; /* -1 == sentinel */
} pylist;
static void
reap_obj(pylist fd2obj[FD_SETSIZE + 1])
{
- int i;
- for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
- Py_XDECREF(fd2obj[i].obj);
- fd2obj[i].obj = NULL;
- }
- fd2obj[0].sentinel = -1;
+ int i;
+ for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
+ Py_XDECREF(fd2obj[i].obj);
+ fd2obj[i].obj = NULL;
+ }
+ fd2obj[0].sentinel = -1;
}
@@ -81,106 +81,106 @@ reap_obj(pylist fd2obj[FD_SETSIZE + 1])
static int
seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
{
- int i;
- int max = -1;
- int index = 0;
- int len = -1;
- PyObject* fast_seq = NULL;
- PyObject* o = NULL;
-
- fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
- FD_ZERO(set);
-
- fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
- if (!fast_seq)
- return -1;
+ int i;
+ int max = -1;
+ int index = 0;
+ int len = -1;
+ PyObject* fast_seq = NULL;
+ PyObject* o = NULL;
+
+ fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
+ FD_ZERO(set);
+
+ fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
+ if (!fast_seq)
+ return -1;
- len = PySequence_Fast_GET_SIZE(fast_seq);
+ len = PySequence_Fast_GET_SIZE(fast_seq);
- for (i = 0; i < len; i++) {
- SOCKET v;
+ for (i = 0; i < len; i++) {
+ SOCKET v;
- /* any intervening fileno() calls could decr this refcnt */
- if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
- return -1;
+ /* any intervening fileno() calls could decr this refcnt */
+ if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
+ return -1;
- Py_INCREF(o);
- v = PyObject_AsFileDescriptor( o );
- if (v == -1) goto finally;
+ Py_INCREF(o);
+ v = PyObject_AsFileDescriptor( o );
+ if (v == -1) goto finally;
#if defined(_MSC_VER)
- max = 0; /* not used for Win32 */
+ max = 0; /* not used for Win32 */
#else /* !_MSC_VER */
- if (v < 0 || v >= FD_SETSIZE) {
- PyErr_SetString(PyExc_ValueError,
- "filedescriptor out of range in select()");
- goto finally;
- }
- if (v > max)
- max = v;
+ if (v < 0 || v >= FD_SETSIZE) {
+ PyErr_SetString(PyExc_ValueError,
+ "filedescriptor out of range in select()");
+ goto finally;
+ }
+ if (v > max)
+ max = v;
#endif /* _MSC_VER */
- FD_SET(v, set);
-
- /* add object and its file descriptor to the list */
- if (index >= FD_SETSIZE) {
- PyErr_SetString(PyExc_ValueError,
- "too many file descriptors in select()");
- goto finally;
- }
- fd2obj[index].obj = o;
- fd2obj[index].fd = v;
- fd2obj[index].sentinel = 0;
- fd2obj[++index].sentinel = -1;
- }
- Py_DECREF(fast_seq);
- return max+1;
+ FD_SET(v, set);
+
+ /* add object and its file descriptor to the list */
+ if (index >= FD_SETSIZE) {
+ PyErr_SetString(PyExc_ValueError,
+ "too many file descriptors in select()");
+ goto finally;
+ }
+ fd2obj[index].obj = o;
+ fd2obj[index].fd = v;
+ fd2obj[index].sentinel = 0;
+ fd2obj[++index].sentinel = -1;
+ }
+ Py_DECREF(fast_seq);
+ return max+1;
finally:
- Py_XDECREF(o);
- Py_DECREF(fast_seq);
- return -1;
+ Py_XDECREF(o);
+ Py_DECREF(fast_seq);
+ return -1;
}
/* returns NULL and sets the Python exception if an error occurred */
static PyObject *
set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
{
- int i, j, count=0;
- PyObject *list, *o;
- SOCKET fd;
-
- for (j = 0; fd2obj[j].sentinel >= 0; j++) {
- if (FD_ISSET(fd2obj[j].fd, set))
- count++;
- }
- list = PyList_New(count);
- if (!list)
- return NULL;
-
- i = 0;
- for (j = 0; fd2obj[j].sentinel >= 0; j++) {
- fd = fd2obj[j].fd;
- if (FD_ISSET(fd, set)) {
+ int i, j, count=0;
+ PyObject *list, *o;
+ SOCKET fd;
+
+ for (j = 0; fd2obj[j].sentinel >= 0; j++) {
+ if (FD_ISSET(fd2obj[j].fd, set))
+ count++;
+ }
+ list = PyList_New(count);
+ if (!list)
+ return NULL;
+
+ i = 0;
+ for (j = 0; fd2obj[j].sentinel >= 0; j++) {
+ fd = fd2obj[j].fd;
+ if (FD_ISSET(fd, set)) {
#ifndef _MSC_VER
- if (fd > FD_SETSIZE) {
- PyErr_SetString(PyExc_SystemError,
- "filedescriptor out of range returned in select()");
- goto finally;
- }
+ if (fd > FD_SETSIZE) {
+ PyErr_SetString(PyExc_SystemError,
+ "filedescriptor out of range returned in select()");
+ goto finally;
+ }
#endif
- o = fd2obj[j].obj;
- fd2obj[j].obj = NULL;
- /* transfer ownership */
- if (PyList_SetItem(list, i, o) < 0)
- goto finally;
-
- i++;
- }
- }
- return list;
+ o = fd2obj[j].obj;
+ fd2obj[j].obj = NULL;
+ /* transfer ownership */
+ if (PyList_SetItem(list, i, o) < 0)
+ goto finally;
+
+ i++;
+ }
+ }
+ return list;
finally:
- Py_DECREF(list);
- return NULL;
+ Py_DECREF(list);
+ return NULL;
}
#undef SELECT_USES_HEAP
@@ -192,170 +192,170 @@ static PyObject *
select_select(PyObject *self, PyObject *args)
{
#ifdef SELECT_USES_HEAP
- pylist *rfd2obj, *wfd2obj, *efd2obj;
+ pylist *rfd2obj, *wfd2obj, *efd2obj;
#else /* !SELECT_USES_HEAP */
- /* XXX: All this should probably be implemented as follows:
- * - find the highest descriptor we're interested in
- * - add one
- * - that's the size
- * See: Stevens, APitUE, $12.5.1
- */
- pylist rfd2obj[FD_SETSIZE + 1];
- pylist wfd2obj[FD_SETSIZE + 1];
- pylist efd2obj[FD_SETSIZE + 1];
+ /* XXX: All this should probably be implemented as follows:
+ * - find the highest descriptor we're interested in
+ * - add one
+ * - that's the size
+ * See: Stevens, APitUE, $12.5.1
+ */
+ pylist rfd2obj[FD_SETSIZE + 1];
+ pylist wfd2obj[FD_SETSIZE + 1];
+ pylist efd2obj[FD_SETSIZE + 1];
#endif /* SELECT_USES_HEAP */
- PyObject *ifdlist, *ofdlist, *efdlist;
- PyObject *ret = NULL;
- PyObject *tout = Py_None;
- fd_set ifdset, ofdset, efdset;
- double timeout;
- struct timeval tv, *tvp;
- long seconds;
- int imax, omax, emax, max;
- int n;
-
- /* convert arguments */
- if (!PyArg_UnpackTuple(args, "select", 3, 4,
- &ifdlist, &ofdlist, &efdlist, &tout))
- return NULL;
-
- if (tout == Py_None)
- tvp = (struct timeval *)0;
- else if (!PyNumber_Check(tout)) {
- PyErr_SetString(PyExc_TypeError,
- "timeout must be a float or None");
- return NULL;
- }
- else {
- timeout = PyFloat_AsDouble(tout);
- if (timeout == -1 && PyErr_Occurred())
- return NULL;
- if (timeout > (double)LONG_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "timeout period too long");
- return NULL;
- }
- seconds = (long)timeout;
- timeout = timeout - (double)seconds;
- tv.tv_sec = seconds;
- tv.tv_usec = (long)(timeout * 1E6);
- tvp = &tv;
- }
+ PyObject *ifdlist, *ofdlist, *efdlist;
+ PyObject *ret = NULL;
+ PyObject *tout = Py_None;
+ fd_set ifdset, ofdset, efdset;
+ double timeout;
+ struct timeval tv, *tvp;
+ long seconds;
+ int imax, omax, emax, max;
+ int n;
+
+ /* convert arguments */
+ if (!PyArg_UnpackTuple(args, "select", 3, 4,
+ &ifdlist, &ofdlist, &efdlist, &tout))
+ return NULL;
+
+ if (tout == Py_None)
+ tvp = (struct timeval *)0;
+ else if (!PyNumber_Check(tout)) {
+ PyErr_SetString(PyExc_TypeError,
+ "timeout must be a float or None");
+ return NULL;
+ }
+ else {
+ timeout = PyFloat_AsDouble(tout);
+ if (timeout == -1 && PyErr_Occurred())
+ return NULL;
+ if (timeout > (double)LONG_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "timeout period too long");
+ return NULL;
+ }
+ seconds = (long)timeout;
+ timeout = timeout - (double)seconds;
+ tv.tv_sec = seconds;
+ tv.tv_usec = (long)(timeout * 1E6);
+ tvp = &tv;
+ }
#ifdef SELECT_USES_HEAP
- /* Allocate memory for the lists */
- rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
- wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
- efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
- if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
- if (rfd2obj) PyMem_DEL(rfd2obj);
- if (wfd2obj) PyMem_DEL(wfd2obj);
- if (efd2obj) PyMem_DEL(efd2obj);
- return PyErr_NoMemory();
- }
+ /* Allocate memory for the lists */
+ rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
+ wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
+ efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
+ if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
+ if (rfd2obj) PyMem_DEL(rfd2obj);
+ if (wfd2obj) PyMem_DEL(wfd2obj);
+ if (efd2obj) PyMem_DEL(efd2obj);
+ return PyErr_NoMemory();
+ }
#endif /* SELECT_USES_HEAP */
- /* Convert sequences to fd_sets, and get maximum fd number
- * propagates the Python exception set in seq2set()
- */
- rfd2obj[0].sentinel = -1;
- wfd2obj[0].sentinel = -1;
- efd2obj[0].sentinel = -1;
- if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
- goto finally;
- if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
- goto finally;
- if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
- goto finally;
- max = imax;
- if (omax > max) max = omax;
- if (emax > max) max = emax;
-
- Py_BEGIN_ALLOW_THREADS
- n = select(max, &ifdset, &ofdset, &efdset, tvp);
- Py_END_ALLOW_THREADS
+ /* Convert sequences to fd_sets, and get maximum fd number
+ * propagates the Python exception set in seq2set()
+ */
+ rfd2obj[0].sentinel = -1;
+ wfd2obj[0].sentinel = -1;
+ efd2obj[0].sentinel = -1;
+ if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
+ goto finally;
+ if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
+ goto finally;
+ if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
+ goto finally;
+ max = imax;
+ if (omax > max) max = omax;
+ if (emax > max) max = emax;
+
+ Py_BEGIN_ALLOW_THREADS
+ n = select(max, &ifdset, &ofdset, &efdset, tvp);
+ Py_END_ALLOW_THREADS
#ifdef MS_WINDOWS
- if (n == SOCKET_ERROR) {
- PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
- }
+ if (n == SOCKET_ERROR) {
+ PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
+ }
#else
- if (n < 0) {
- PyErr_SetFromErrno(SelectError);
- }
+ if (n < 0) {
+ PyErr_SetFromErrno(SelectError);
+ }
#endif
- else {
- /* any of these three calls can raise an exception. it's more
- convenient to test for this after all three calls... but
- is that acceptable?
- */
- ifdlist = set2list(&ifdset, rfd2obj);
- ofdlist = set2list(&ofdset, wfd2obj);
- efdlist = set2list(&efdset, efd2obj);
- if (PyErr_Occurred())
- ret = NULL;
- else
- ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
-
- Py_DECREF(ifdlist);
- Py_DECREF(ofdlist);
- Py_DECREF(efdlist);
- }
-
+ else {
+ /* any of these three calls can raise an exception. it's more
+ convenient to test for this after all three calls... but
+ is that acceptable?
+ */
+ ifdlist = set2list(&ifdset, rfd2obj);
+ ofdlist = set2list(&ofdset, wfd2obj);
+ efdlist = set2list(&efdset, efd2obj);
+ if (PyErr_Occurred())
+ ret = NULL;
+ else
+ ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
+
+ Py_DECREF(ifdlist);
+ Py_DECREF(ofdlist);
+ Py_DECREF(efdlist);
+ }
+
finally:
- reap_obj(rfd2obj);
- reap_obj(wfd2obj);
- reap_obj(efd2obj);
+ reap_obj(rfd2obj);
+ reap_obj(wfd2obj);
+ reap_obj(efd2obj);
#ifdef SELECT_USES_HEAP
- PyMem_DEL(rfd2obj);
- PyMem_DEL(wfd2obj);
- PyMem_DEL(efd2obj);
+ PyMem_DEL(rfd2obj);
+ PyMem_DEL(wfd2obj);
+ PyMem_DEL(efd2obj);
#endif /* SELECT_USES_HEAP */
- return ret;
+ return ret;
}
#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
-/*
+/*
* poll() support
*/
typedef struct {
- PyObject_HEAD
- PyObject *dict;
- int ufd_uptodate;
- int ufd_len;
- struct pollfd *ufds;
+ PyObject_HEAD
+ PyObject *dict;
+ int ufd_uptodate;
+ int ufd_len;
+ struct pollfd *ufds;
} pollObject;
static PyTypeObject poll_Type;
-/* Update the malloc'ed array of pollfds to match the dictionary
+/* Update the malloc'ed array of pollfds to match the dictionary
contained within a pollObject. Return 1 on success, 0 on an error.
*/
static int
update_ufd_array(pollObject *self)
{
- Py_ssize_t i, pos;
- PyObject *key, *value;
- struct pollfd *old_ufds = self->ufds;
-
- self->ufd_len = PyDict_Size(self->dict);
- PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
- if (self->ufds == NULL) {
- self->ufds = old_ufds;
- PyErr_NoMemory();
- return 0;
- }
-
- i = pos = 0;
- while (PyDict_Next(self->dict, &pos, &key, &value)) {
- self->ufds[i].fd = PyLong_AsLong(key);
- self->ufds[i].events = (short)PyLong_AsLong(value);
- i++;
- }
- self->ufd_uptodate = 1;
- return 1;
+ Py_ssize_t i, pos;
+ PyObject *key, *value;
+ struct pollfd *old_ufds = self->ufds;
+
+ self->ufd_len = PyDict_Size(self->dict);
+ PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
+ if (self->ufds == NULL) {
+ self->ufds = old_ufds;
+ PyErr_NoMemory();
+ return 0;
+ }
+
+ i = pos = 0;
+ while (PyDict_Next(self->dict, &pos, &key, &value)) {
+ self->ufds[i].fd = PyLong_AsLong(key);
+ self->ufds[i].events = (short)PyLong_AsLong(value);
+ i++;
+ }
+ self->ufd_uptodate = 1;
+ return 1;
}
PyDoc_STRVAR(poll_register_doc,
@@ -366,39 +366,39 @@ fd -- either an integer, or an object with a fileno() method returning an\n\
events -- an optional bitmask describing the type of events to check for");
static PyObject *
-poll_register(pollObject *self, PyObject *args)
+poll_register(pollObject *self, PyObject *args)
{
- PyObject *o, *key, *value;
- int fd, events = POLLIN | POLLPRI | POLLOUT;
- int err;
-
- if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
- return NULL;
- }
-
- fd = PyObject_AsFileDescriptor(o);
- if (fd == -1) return NULL;
-
- /* Add entry to the internal dictionary: the key is the
- file descriptor, and the value is the event mask. */
- key = PyLong_FromLong(fd);
- if (key == NULL)
- return NULL;
- value = PyLong_FromLong(events);
- if (value == NULL) {
- Py_DECREF(key);
- return NULL;
- }
- err = PyDict_SetItem(self->dict, key, value);
- Py_DECREF(key);
- Py_DECREF(value);
- if (err < 0)
- return NULL;
-
- self->ufd_uptodate = 0;
-
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *o, *key, *value;
+ int fd, events = POLLIN | POLLPRI | POLLOUT;
+ int err;
+
+ if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
+ return NULL;
+ }
+
+ fd = PyObject_AsFileDescriptor(o);
+ if (fd == -1) return NULL;
+
+ /* Add entry to the internal dictionary: the key is the
+ file descriptor, and the value is the event mask. */
+ key = PyLong_FromLong(fd);
+ if (key == NULL)
+ return NULL;
+ value = PyLong_FromLong(events);
+ if (value == NULL) {
+ Py_DECREF(key);
+ return NULL;
+ }
+ err = PyDict_SetItem(self->dict, key, value);
+ Py_DECREF(key);
+ Py_DECREF(value);
+ if (err < 0)
+ return NULL;
+
+ self->ufd_uptodate = 0;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(poll_modify_doc,
@@ -411,41 +411,41 @@ events -- an optional bitmask describing the type of events to check for");
static PyObject *
poll_modify(pollObject *self, PyObject *args)
{
- PyObject *o, *key, *value;
- int fd, events;
- int err;
-
- if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
- return NULL;
- }
-
- fd = PyObject_AsFileDescriptor(o);
- if (fd == -1) return NULL;
-
- /* Modify registered fd */
- key = PyLong_FromLong(fd);
- if (key == NULL)
- return NULL;
- if (PyDict_GetItem(self->dict, key) == NULL) {
- errno = ENOENT;
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- value = PyLong_FromLong(events);
- if (value == NULL) {
- Py_DECREF(key);
- return NULL;
- }
- err = PyDict_SetItem(self->dict, key, value);
- Py_DECREF(key);
- Py_DECREF(value);
- if (err < 0)
- return NULL;
-
- self->ufd_uptodate = 0;
-
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *o, *key, *value;
+ int fd, events;
+ int err;
+
+ if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
+ return NULL;
+ }
+
+ fd = PyObject_AsFileDescriptor(o);
+ if (fd == -1) return NULL;
+
+ /* Modify registered fd */
+ key = PyLong_FromLong(fd);
+ if (key == NULL)
+ return NULL;
+ if (PyDict_GetItem(self->dict, key) == NULL) {
+ errno = ENOENT;
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ value = PyLong_FromLong(events);
+ if (value == NULL) {
+ Py_DECREF(key);
+ return NULL;
+ }
+ err = PyDict_SetItem(self->dict, key, value);
+ Py_DECREF(key);
+ Py_DECREF(value);
+ if (err < 0)
+ return NULL;
+
+ self->ufd_uptodate = 0;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
@@ -454,32 +454,32 @@ PyDoc_STRVAR(poll_unregister_doc,
Remove a file descriptor being tracked by the polling object.");
static PyObject *
-poll_unregister(pollObject *self, PyObject *o)
+poll_unregister(pollObject *self, PyObject *o)
{
- PyObject *key;
- int fd;
-
- fd = PyObject_AsFileDescriptor( o );
- if (fd == -1)
- return NULL;
-
- /* Check whether the fd is already in the array */
- key = PyLong_FromLong(fd);
- if (key == NULL)
- return NULL;
-
- if (PyDict_DelItem(self->dict, key) == -1) {
- Py_DECREF(key);
- /* This will simply raise the KeyError set by PyDict_DelItem
- if the file descriptor isn't registered. */
- return NULL;
- }
-
- Py_DECREF(key);
- self->ufd_uptodate = 0;
-
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *key;
+ int fd;
+
+ fd = PyObject_AsFileDescriptor( o );
+ if (fd == -1)
+ return NULL;
+
+ /* Check whether the fd is already in the array */
+ key = PyLong_FromLong(fd);
+ if (key == NULL)
+ return NULL;
+
+ if (PyDict_DelItem(self->dict, key) == -1) {
+ Py_DECREF(key);
+ /* This will simply raise the KeyError set by PyDict_DelItem
+ if the file descriptor isn't registered. */
+ return NULL;
+ }
+
+ Py_DECREF(key);
+ self->ufd_uptodate = 0;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(poll_poll_doc,
@@ -488,169 +488,169 @@ Polls the set of registered file descriptors, returning a list containing \n\
any descriptors that have events or errors to report.");
static PyObject *
-poll_poll(pollObject *self, PyObject *args)
+poll_poll(pollObject *self, PyObject *args)
{
- PyObject *result_list = NULL, *tout = NULL;
- int timeout = 0, poll_result, i, j;
- PyObject *value = NULL, *num = NULL;
-
- if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
- return NULL;
- }
-
- /* Check values for timeout */
- if (tout == NULL || tout == Py_None)
- timeout = -1;
- else if (!PyNumber_Check(tout)) {
- PyErr_SetString(PyExc_TypeError,
- "timeout must be an integer or None");
- return NULL;
- }
- else {
- tout = PyNumber_Long(tout);
- if (!tout)
- return NULL;
- timeout = PyLong_AsLong(tout);
- Py_DECREF(tout);
- if (timeout == -1 && PyErr_Occurred())
- return NULL;
- }
-
- /* Ensure the ufd array is up to date */
- if (!self->ufd_uptodate)
- if (update_ufd_array(self) == 0)
- return NULL;
-
- /* call poll() */
- Py_BEGIN_ALLOW_THREADS
- poll_result = poll(self->ufds, self->ufd_len, timeout);
- Py_END_ALLOW_THREADS
-
- if (poll_result < 0) {
- PyErr_SetFromErrno(SelectError);
- return NULL;
- }
-
- /* build the result list */
-
- result_list = PyList_New(poll_result);
- if (!result_list)
- return NULL;
- else {
- for (i = 0, j = 0; j < poll_result; j++) {
- /* skip to the next fired descriptor */
- while (!self->ufds[i].revents) {
- i++;
- }
- /* if we hit a NULL return, set value to NULL
- and break out of loop; code at end will
- clean up result_list */
- value = PyTuple_New(2);
- if (value == NULL)
- goto error;
- num = PyLong_FromLong(self->ufds[i].fd);
- if (num == NULL) {
- Py_DECREF(value);
- goto error;
- }
- PyTuple_SET_ITEM(value, 0, num);
-
- /* The &0xffff is a workaround for AIX. 'revents'
- is a 16-bit short, and IBM assigned POLLNVAL
- to be 0x8000, so the conversion to int results
- in a negative number. See SF bug #923315. */
- num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
- if (num == NULL) {
- Py_DECREF(value);
- goto error;
- }
- PyTuple_SET_ITEM(value, 1, num);
- if ((PyList_SetItem(result_list, j, value)) == -1) {
- Py_DECREF(value);
- goto error;
- }
- i++;
- }
- }
- return result_list;
+ PyObject *result_list = NULL, *tout = NULL;
+ int timeout = 0, poll_result, i, j;
+ PyObject *value = NULL, *num = NULL;
+
+ if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
+ return NULL;
+ }
+
+ /* Check values for timeout */
+ if (tout == NULL || tout == Py_None)
+ timeout = -1;
+ else if (!PyNumber_Check(tout)) {
+ PyErr_SetString(PyExc_TypeError,
+ "timeout must be an integer or None");
+ return NULL;
+ }
+ else {
+ tout = PyNumber_Long(tout);
+ if (!tout)
+ return NULL;
+ timeout = PyLong_AsLong(tout);
+ Py_DECREF(tout);
+ if (timeout == -1 && PyErr_Occurred())
+ return NULL;
+ }
+
+ /* Ensure the ufd array is up to date */
+ if (!self->ufd_uptodate)
+ if (update_ufd_array(self) == 0)
+ return NULL;
+
+ /* call poll() */
+ Py_BEGIN_ALLOW_THREADS
+ poll_result = poll(self->ufds, self->ufd_len, timeout);
+ Py_END_ALLOW_THREADS
+
+ if (poll_result < 0) {
+ PyErr_SetFromErrno(SelectError);
+ return NULL;
+ }
+
+ /* build the result list */
+
+ result_list = PyList_New(poll_result);
+ if (!result_list)
+ return NULL;
+ else {
+ for (i = 0, j = 0; j < poll_result; j++) {
+ /* skip to the next fired descriptor */
+ while (!self->ufds[i].revents) {
+ i++;
+ }
+ /* if we hit a NULL return, set value to NULL
+ and break out of loop; code at end will
+ clean up result_list */
+ value = PyTuple_New(2);
+ if (value == NULL)
+ goto error;
+ num = PyLong_FromLong(self->ufds[i].fd);
+ if (num == NULL) {
+ Py_DECREF(value);
+ goto error;
+ }
+ PyTuple_SET_ITEM(value, 0, num);
+
+ /* The &0xffff is a workaround for AIX. 'revents'
+ is a 16-bit short, and IBM assigned POLLNVAL
+ to be 0x8000, so the conversion to int results
+ in a negative number. See SF bug #923315. */
+ num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
+ if (num == NULL) {
+ Py_DECREF(value);
+ goto error;
+ }
+ PyTuple_SET_ITEM(value, 1, num);
+ if ((PyList_SetItem(result_list, j, value)) == -1) {
+ Py_DECREF(value);
+ goto error;
+ }
+ i++;
+ }
+ }
+ return result_list;
error:
- Py_DECREF(result_list);
- return NULL;
+ Py_DECREF(result_list);
+ return NULL;
}
static PyMethodDef poll_methods[] = {
- {"register", (PyCFunction)poll_register,
- METH_VARARGS, poll_register_doc},
- {"modify", (PyCFunction)poll_modify,
- METH_VARARGS, poll_modify_doc},
- {"unregister", (PyCFunction)poll_unregister,
- METH_O, poll_unregister_doc},
- {"poll", (PyCFunction)poll_poll,
- METH_VARARGS, poll_poll_doc},
- {NULL, NULL} /* sentinel */
+ {"register", (PyCFunction)poll_register,
+ METH_VARARGS, poll_register_doc},
+ {"modify", (PyCFunction)poll_modify,
+ METH_VARARGS, poll_modify_doc},
+ {"unregister", (PyCFunction)poll_unregister,
+ METH_O, poll_unregister_doc},
+ {"poll", (PyCFunction)poll_poll,
+ METH_VARARGS, poll_poll_doc},
+ {NULL, NULL} /* sentinel */
};
static pollObject *
newPollObject(void)
{
- pollObject *self;
- self = PyObject_New(pollObject, &poll_Type);
- if (self == NULL)
- return NULL;
- /* ufd_uptodate is a Boolean, denoting whether the
- array pointed to by ufds matches the contents of the dictionary. */
- self->ufd_uptodate = 0;
- self->ufds = NULL;
- self->dict = PyDict_New();
- if (self->dict == NULL) {
- Py_DECREF(self);
- return NULL;
- }
- return self;
+ pollObject *self;
+ self = PyObject_New(pollObject, &poll_Type);
+ if (self == NULL)
+ return NULL;
+ /* ufd_uptodate is a Boolean, denoting whether the
+ array pointed to by ufds matches the contents of the dictionary. */
+ self->ufd_uptodate = 0;
+ self->ufds = NULL;
+ self->dict = PyDict_New();
+ if (self->dict == NULL) {
+ Py_DECREF(self);
+ return NULL;
+ }
+ return self;
}
static void
poll_dealloc(pollObject *self)
{
- if (self->ufds != NULL)
- PyMem_DEL(self->ufds);
- Py_XDECREF(self->dict);
- PyObject_Del(self);
+ if (self->ufds != NULL)
+ PyMem_DEL(self->ufds);
+ Py_XDECREF(self->dict);
+ PyObject_Del(self);
}
static PyTypeObject poll_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "select.poll", /*tp_name*/
- sizeof(pollObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)poll_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- poll_methods, /*tp_methods*/
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "select.poll", /*tp_name*/
+ sizeof(pollObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)poll_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ poll_methods, /*tp_methods*/
};
PyDoc_STRVAR(poll_doc,
@@ -660,36 +660,36 @@ unregistering file descriptors, and then polling them for I/O events.");
static PyObject *
select_poll(PyObject *self, PyObject *unused)
{
- return (PyObject *)newPollObject();
+ return (PyObject *)newPollObject();
}
#ifdef __APPLE__
-/*
+/*
* On some systems poll() sets errno on invalid file descriptors. We test
* for this at runtime because this bug may be fixed or introduced between
* OS releases.
*/
static int select_have_broken_poll(void)
{
- int poll_test;
- int filedes[2];
-
- struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
-
- /* Create a file descriptor to make invalid */
- if (pipe(filedes) < 0) {
- return 1;
- }
- poll_struct.fd = filedes[0];
- close(filedes[0]);
- close(filedes[1]);
- poll_test = poll(&poll_struct, 1, 0);
- if (poll_test < 0) {
- return 1;
- } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
- return 1;
- }
- return 0;
+ int poll_test;
+ int filedes[2];
+
+ struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
+
+ /* Create a file descriptor to make invalid */
+ if (pipe(filedes) < 0) {
+ return 1;
+ }
+ poll_struct.fd = filedes[0];
+ close(filedes[0]);
+ close(filedes[1]);
+ poll_test = poll(&poll_struct, 1, 0);
+ if (poll_test < 0) {
+ return 1;
+ } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
+ return 1;
+ }
+ return 0;
}
#endif /* __APPLE__ */
@@ -708,8 +708,8 @@ static int select_have_broken_poll(void)
#endif
typedef struct {
- PyObject_HEAD
- SOCKET epfd; /* epoll control file descriptor */
+ PyObject_HEAD
+ SOCKET epfd; /* epoll control file descriptor */
} pyEpoll_Object;
static PyTypeObject pyEpoll_Type;
@@ -718,92 +718,92 @@ static PyTypeObject pyEpoll_Type;
static PyObject *
pyepoll_err_closed(void)
{
- PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
+ return NULL;
}
static int
pyepoll_internal_close(pyEpoll_Object *self)
{
- int save_errno = 0;
- if (self->epfd >= 0) {
- int epfd = self->epfd;
- self->epfd = -1;
- Py_BEGIN_ALLOW_THREADS
- if (close(epfd) < 0)
- save_errno = errno;
- Py_END_ALLOW_THREADS
- }
- return save_errno;
+ int save_errno = 0;
+ if (self->epfd >= 0) {
+ int epfd = self->epfd;
+ self->epfd = -1;
+ Py_BEGIN_ALLOW_THREADS
+ if (close(epfd) < 0)
+ save_errno = errno;
+ Py_END_ALLOW_THREADS
+ }
+ return save_errno;
}
static PyObject *
newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)
{
- pyEpoll_Object *self;
-
- if (sizehint == -1) {
- sizehint = FD_SETSIZE-1;
- }
- else if (sizehint < 1) {
- PyErr_Format(PyExc_ValueError,
- "sizehint must be greater zero, got %d",
- sizehint);
- return NULL;
- }
-
- assert(type != NULL && type->tp_alloc != NULL);
- self = (pyEpoll_Object *) type->tp_alloc(type, 0);
- if (self == NULL)
- return NULL;
-
- if (fd == -1) {
- Py_BEGIN_ALLOW_THREADS
- self->epfd = epoll_create(sizehint);
- Py_END_ALLOW_THREADS
- }
- else {
- self->epfd = fd;
- }
- if (self->epfd < 0) {
- Py_DECREF(self);
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return (PyObject *)self;
+ pyEpoll_Object *self;
+
+ if (sizehint == -1) {
+ sizehint = FD_SETSIZE-1;
+ }
+ else if (sizehint < 1) {
+ PyErr_Format(PyExc_ValueError,
+ "sizehint must be greater zero, got %d",
+ sizehint);
+ return NULL;
+ }
+
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = (pyEpoll_Object *) type->tp_alloc(type, 0);
+ if (self == NULL)
+ return NULL;
+
+ if (fd == -1) {
+ Py_BEGIN_ALLOW_THREADS
+ self->epfd = epoll_create(sizehint);
+ Py_END_ALLOW_THREADS
+ }
+ else {
+ self->epfd = fd;
+ }
+ if (self->epfd < 0) {
+ Py_DECREF(self);
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return (PyObject *)self;
}
static PyObject *
pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- int sizehint = -1;
- static char *kwlist[] = {"sizehint", NULL};
+ int sizehint = -1;
+ static char *kwlist[] = {"sizehint", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist,
- &sizehint))
- return NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist,
+ &sizehint))
+ return NULL;
- return newPyEpoll_Object(type, sizehint, -1);
+ return newPyEpoll_Object(type, sizehint, -1);
}
static void
pyepoll_dealloc(pyEpoll_Object *self)
{
- (void)pyepoll_internal_close(self);
- Py_TYPE(self)->tp_free(self);
+ (void)pyepoll_internal_close(self);
+ Py_TYPE(self)->tp_free(self);
}
static PyObject*
pyepoll_close(pyEpoll_Object *self)
{
- errno = pyepoll_internal_close(self);
- if (errno < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_RETURN_NONE;
+ errno = pyepoll_internal_close(self);
+ if (errno < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(pyepoll_close_doc,
@@ -815,18 +815,18 @@ object will raise an exception.");
static PyObject*
pyepoll_get_closed(pyEpoll_Object *self)
{
- if (self->epfd < 0)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
+ if (self->epfd < 0)
+ Py_RETURN_TRUE;
+ else
+ Py_RETURN_FALSE;
}
static PyObject*
pyepoll_fileno(pyEpoll_Object *self)
{
- if (self->epfd < 0)
- return pyepoll_err_closed();
- return PyLong_FromLong(self->epfd);
+ if (self->epfd < 0)
+ return pyepoll_err_closed();
+ return PyLong_FromLong(self->epfd);
}
PyDoc_STRVAR(pyepoll_fileno_doc,
@@ -837,12 +837,12 @@ Return the epoll control file descriptor.");
static PyObject*
pyepoll_fromfd(PyObject *cls, PyObject *args)
{
- SOCKET fd;
+ SOCKET fd;
- if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
+ return NULL;
- return newPyEpoll_Object((PyTypeObject*)cls, -1, fd);
+ return newPyEpoll_Object((PyTypeObject*)cls, -1, fd);
}
PyDoc_STRVAR(pyepoll_fromfd_doc,
@@ -853,65 +853,65 @@ Create an epoll object from a given control fd.");
static PyObject *
pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
{
- struct epoll_event ev;
- int result;
- int fd;
-
- if (epfd < 0)
- return pyepoll_err_closed();
-
- fd = PyObject_AsFileDescriptor(pfd);
- if (fd == -1) {
- return NULL;
- }
-
- switch(op) {
- case EPOLL_CTL_ADD:
- case EPOLL_CTL_MOD:
- ev.events = events;
- ev.data.fd = fd;
- Py_BEGIN_ALLOW_THREADS
- result = epoll_ctl(epfd, op, fd, &ev);
- Py_END_ALLOW_THREADS
- break;
- case EPOLL_CTL_DEL:
- /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
- * operation required a non-NULL pointer in event, even
- * though this argument is ignored. */
- Py_BEGIN_ALLOW_THREADS
- result = epoll_ctl(epfd, op, fd, &ev);
- if (errno == EBADF) {
- /* fd already closed */
- result = 0;
- errno = 0;
- }
- Py_END_ALLOW_THREADS
- break;
- default:
- result = -1;
- errno = EINVAL;
- }
-
- if (result < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_RETURN_NONE;
+ struct epoll_event ev;
+ int result;
+ int fd;
+
+ if (epfd < 0)
+ return pyepoll_err_closed();
+
+ fd = PyObject_AsFileDescriptor(pfd);
+ if (fd == -1) {
+ return NULL;
+ }
+
+ switch(op) {
+ case EPOLL_CTL_ADD:
+ case EPOLL_CTL_MOD:
+ ev.events = events;
+ ev.data.fd = fd;
+ Py_BEGIN_ALLOW_THREADS
+ result = epoll_ctl(epfd, op, fd, &ev);
+ Py_END_ALLOW_THREADS
+ break;
+ case EPOLL_CTL_DEL:
+ /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
+ * operation required a non-NULL pointer in event, even
+ * though this argument is ignored. */
+ Py_BEGIN_ALLOW_THREADS
+ result = epoll_ctl(epfd, op, fd, &ev);
+ if (errno == EBADF) {
+ /* fd already closed */
+ result = 0;
+ errno = 0;
+ }
+ Py_END_ALLOW_THREADS
+ break;
+ default:
+ result = -1;
+ errno = EINVAL;
+ }
+
+ if (result < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
static PyObject *
pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
{
- PyObject *pfd;
- unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
- static char *kwlist[] = {"fd", "eventmask", NULL};
+ PyObject *pfd;
+ unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
+ static char *kwlist[] = {"fd", "eventmask", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
- &pfd, &events)) {
- return NULL;
- }
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
+ &pfd, &events)) {
+ return NULL;
+ }
- return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
+ return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
}
PyDoc_STRVAR(pyepoll_register_doc,
@@ -928,16 +928,16 @@ The epoll interface supports all file descriptors that support poll.");
static PyObject *
pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
{
- PyObject *pfd;
- unsigned int events;
- static char *kwlist[] = {"fd", "eventmask", NULL};
+ PyObject *pfd;
+ unsigned int events;
+ static char *kwlist[] = {"fd", "eventmask", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
- &pfd, &events)) {
- return NULL;
- }
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
+ &pfd, &events)) {
+ return NULL;
+ }
- return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
+ return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
}
PyDoc_STRVAR(pyepoll_modify_doc,
@@ -949,15 +949,15 @@ events is a bit set composed of the various EPOLL constants");
static PyObject *
pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
{
- PyObject *pfd;
- static char *kwlist[] = {"fd", NULL};
+ PyObject *pfd;
+ static char *kwlist[] = {"fd", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
- &pfd)) {
- return NULL;
- }
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
+ &pfd)) {
+ return NULL;
+ }
- return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
+ return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
}
PyDoc_STRVAR(pyepoll_unregister_doc,
@@ -968,76 +968,76 @@ fd is the target file descriptor of the operation.");
static PyObject *
pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
{
- double dtimeout = -1.;
- int timeout;
- int maxevents = -1;
- int nfds, i;
- PyObject *elist = NULL, *etuple = NULL;
- struct epoll_event *evs = NULL;
- static char *kwlist[] = {"timeout", "maxevents", NULL};
-
- if (self->epfd < 0)
- return pyepoll_err_closed();
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
- &dtimeout, &maxevents)) {
- return NULL;
- }
-
- if (dtimeout < 0) {
- timeout = -1;
- }
- else if (dtimeout * 1000.0 > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "timeout is too large");
- return NULL;
- }
- else {
- timeout = (int)(dtimeout * 1000.0);
- }
-
- if (maxevents == -1) {
- maxevents = FD_SETSIZE-1;
- }
- else if (maxevents < 1) {
- PyErr_Format(PyExc_ValueError,
- "maxevents must be greater than 0, got %d",
- maxevents);
- return NULL;
- }
-
- evs = PyMem_New(struct epoll_event, maxevents);
- if (evs == NULL) {
- Py_DECREF(self);
- PyErr_NoMemory();
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
- Py_END_ALLOW_THREADS
- if (nfds < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- goto error;
- }
-
- elist = PyList_New(nfds);
- if (elist == NULL) {
- goto error;
- }
-
- for (i = 0; i < nfds; i++) {
- etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
- if (etuple == NULL) {
- Py_CLEAR(elist);
- goto error;
- }
- PyList_SET_ITEM(elist, i, etuple);
- }
+ double dtimeout = -1.;
+ int timeout;
+ int maxevents = -1;
+ int nfds, i;
+ PyObject *elist = NULL, *etuple = NULL;
+ struct epoll_event *evs = NULL;
+ static char *kwlist[] = {"timeout", "maxevents", NULL};
+
+ if (self->epfd < 0)
+ return pyepoll_err_closed();
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
+ &dtimeout, &maxevents)) {
+ return NULL;
+ }
+
+ if (dtimeout < 0) {
+ timeout = -1;
+ }
+ else if (dtimeout * 1000.0 > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "timeout is too large");
+ return NULL;
+ }
+ else {
+ timeout = (int)(dtimeout * 1000.0);
+ }
+
+ if (maxevents == -1) {
+ maxevents = FD_SETSIZE-1;
+ }
+ else if (maxevents < 1) {
+ PyErr_Format(PyExc_ValueError,
+ "maxevents must be greater than 0, got %d",
+ maxevents);
+ return NULL;
+ }
+
+ evs = PyMem_New(struct epoll_event, maxevents);
+ if (evs == NULL) {
+ Py_DECREF(self);
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
+ Py_END_ALLOW_THREADS
+ if (nfds < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ goto error;
+ }
+
+ elist = PyList_New(nfds);
+ if (elist == NULL) {
+ goto error;
+ }
+
+ for (i = 0; i < nfds; i++) {
+ etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
+ if (etuple == NULL) {
+ Py_CLEAR(elist);
+ goto error;
+ }
+ PyList_SET_ITEM(elist, i, etuple);
+ }
error:
- PyMem_Free(evs);
- return elist;
+ PyMem_Free(evs);
+ return elist;
}
PyDoc_STRVAR(pyepoll_poll_doc,
@@ -1048,27 +1048,27 @@ in seconds (as float). -1 makes poll wait indefinitely.\n\
Up to maxevents are returned to the caller.");
static PyMethodDef pyepoll_methods[] = {
- {"fromfd", (PyCFunction)pyepoll_fromfd,
- METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
- {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
- pyepoll_close_doc},
- {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
- pyepoll_fileno_doc},
- {"modify", (PyCFunction)pyepoll_modify,
- METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
- {"register", (PyCFunction)pyepoll_register,
- METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
- {"unregister", (PyCFunction)pyepoll_unregister,
- METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
- {"poll", (PyCFunction)pyepoll_poll,
- METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
- {NULL, NULL},
+ {"fromfd", (PyCFunction)pyepoll_fromfd,
+ METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
+ {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
+ pyepoll_close_doc},
+ {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
+ pyepoll_fileno_doc},
+ {"modify", (PyCFunction)pyepoll_modify,
+ METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
+ {"register", (PyCFunction)pyepoll_register,
+ METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
+ {"unregister", (PyCFunction)pyepoll_unregister,
+ METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
+ {"poll", (PyCFunction)pyepoll_poll,
+ METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
+ {NULL, NULL},
};
static PyGetSetDef pyepoll_getsetlist[] = {
- {"closed", (getter)pyepoll_get_closed, NULL,
- "True if the epoll handler is closed"},
- {0},
+ {"closed", (getter)pyepoll_get_closed, NULL,
+ "True if the epoll handler is closed"},
+ {0},
};
PyDoc_STRVAR(pyepoll_doc,
@@ -1081,45 +1081,45 @@ sizehint is used to optimize internal data structures. It doesn't limit\n\
the maximum number of monitored events.");
static PyTypeObject pyEpoll_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "select.epoll", /* tp_name */
- sizeof(pyEpoll_Object), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)pyepoll_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- pyepoll_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- pyepoll_methods, /* tp_methods */
- 0, /* tp_members */
- pyepoll_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- pyepoll_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "select.epoll", /* tp_name */
+ sizeof(pyEpoll_Object), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)pyepoll_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ pyepoll_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ pyepoll_methods, /* tp_methods */
+ 0, /* tp_members */
+ pyepoll_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ pyepoll_new, /* tp_new */
+ 0, /* tp_free */
};
#endif /* HAVE_EPOLL */
@@ -1174,8 +1174,8 @@ of. You could also set up a dictionary on the python side to store a\n\
udata->object mapping.");
typedef struct {
- PyObject_HEAD
- struct kevent e;
+ PyObject_HEAD
+ struct kevent e;
} kqueue_event_Object;
static PyTypeObject kqueue_event_Type;
@@ -1183,8 +1183,8 @@ static PyTypeObject kqueue_event_Type;
#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
typedef struct {
- PyObject_HEAD
- SOCKET kqfd; /* kqueue control fd */
+ PyObject_HEAD
+ SOCKET kqfd; /* kqueue control fd */
} kqueue_queue_Object;
static PyTypeObject kqueue_queue_Type;
@@ -1198,221 +1198,221 @@ static PyTypeObject kqueue_queue_Type;
#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
static struct PyMemberDef kqueue_event_members[] = {
- {"ident", T_UINT, KQ_OFF(e.ident)},
- {"filter", T_SHORT, KQ_OFF(e.filter)},
- {"flags", T_USHORT, KQ_OFF(e.flags)},
- {"fflags", T_UINT, KQ_OFF(e.fflags)},
- {"data", T_INT, KQ_OFF(e.data)},
- {"udata", T_INT, KQ_OFF(e.udata)},
- {NULL} /* Sentinel */
+ {"ident", T_UINT, KQ_OFF(e.ident)},
+ {"filter", T_SHORT, KQ_OFF(e.filter)},
+ {"flags", T_USHORT, KQ_OFF(e.flags)},
+ {"fflags", T_UINT, KQ_OFF(e.fflags)},
+ {"data", T_INT, KQ_OFF(e.data)},
+ {"udata", T_INT, KQ_OFF(e.udata)},
+ {NULL} /* Sentinel */
};
#undef KQ_OFF
static PyObject *
kqueue_event_repr(kqueue_event_Object *s)
{
- char buf[1024];
- PyOS_snprintf(
- buf, sizeof(buf),
- "<select.kevent ident=%lu filter=%d flags=0x%x fflags=0x%x "
- "data=0x%lx udata=%p>",
- (unsigned long)(s->e.ident), s->e.filter, s->e.flags,
- s->e.fflags, (long)(s->e.data), s->e.udata);
- return PyBytes_FromString(buf);
+ char buf[1024];
+ PyOS_snprintf(
+ buf, sizeof(buf),
+ "<select.kevent ident=%lu filter=%d flags=0x%x fflags=0x%x "
+ "data=0x%lx udata=%p>",
+ (unsigned long)(s->e.ident), s->e.filter, s->e.flags,
+ s->e.fflags, (long)(s->e.data), s->e.udata);
+ return PyBytes_FromString(buf);
}
static int
kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
{
- PyObject *pfd;
- static char *kwlist[] = {"ident", "filter", "flags", "fflags",
- "data", "udata", NULL};
-
- EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhiii:kevent", kwlist,
- &pfd, &(self->e.filter), &(self->e.flags),
- &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
- return -1;
- }
-
- self->e.ident = PyObject_AsFileDescriptor(pfd);
- if (self->e.ident == -1) {
- return -1;
- }
- return 0;
+ PyObject *pfd;
+ static char *kwlist[] = {"ident", "filter", "flags", "fflags",
+ "data", "udata", NULL};
+
+ EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhiii:kevent", kwlist,
+ &pfd, &(self->e.filter), &(self->e.flags),
+ &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
+ return -1;
+ }
+
+ self->e.ident = PyObject_AsFileDescriptor(pfd);
+ if (self->e.ident == -1) {
+ return -1;
+ }
+ return 0;
}
static PyObject *
kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
- int op)
+ int op)
{
- int result = 0;
-
- if (!kqueue_event_Check(o)) {
- if (op == Py_EQ || op == Py_NE) {
- PyObject *res = op == Py_EQ ? Py_False : Py_True;
- Py_INCREF(res);
- return res;
- }
- PyErr_Format(PyExc_TypeError,
- "can't compare %.200s to %.200s",
- Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
- return NULL;
- }
- if (((result = s->e.ident - o->e.ident) == 0) &&
- ((result = s->e.filter - o->e.filter) == 0) &&
- ((result = s->e.flags - o->e.flags) == 0) &&
- ((result = s->e.fflags - o->e.fflags) == 0) &&
- ((result = s->e.data - o->e.data) == 0) &&
- ((result = s->e.udata - o->e.udata) == 0)
- ) {
- result = 0;
- }
-
- switch (op) {
- case Py_EQ:
- result = (result == 0);
- break;
- case Py_NE:
- result = (result != 0);
- break;
- case Py_LE:
- result = (result <= 0);
- break;
- case Py_GE:
- result = (result >= 0);
- break;
- case Py_LT:
- result = (result < 0);
- break;
- case Py_GT:
- result = (result > 0);
- break;
- }
- return PyBool_FromLong(result);
+ int result = 0;
+
+ if (!kqueue_event_Check(o)) {
+ if (op == Py_EQ || op == Py_NE) {
+ PyObject *res = op == Py_EQ ? Py_False : Py_True;
+ Py_INCREF(res);
+ return res;
+ }
+ PyErr_Format(PyExc_TypeError,
+ "can't compare %.200s to %.200s",
+ Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
+ return NULL;
+ }
+ if (((result = s->e.ident - o->e.ident) == 0) &&
+ ((result = s->e.filter - o->e.filter) == 0) &&
+ ((result = s->e.flags - o->e.flags) == 0) &&
+ ((result = s->e.fflags - o->e.fflags) == 0) &&
+ ((result = s->e.data - o->e.data) == 0) &&
+ ((result = s->e.udata - o->e.udata) == 0)
+ ) {
+ result = 0;
+ }
+
+ switch (op) {
+ case Py_EQ:
+ result = (result == 0);
+ break;
+ case Py_NE:
+ result = (result != 0);
+ break;
+ case Py_LE:
+ result = (result <= 0);
+ break;
+ case Py_GE:
+ result = (result >= 0);
+ break;
+ case Py_LT:
+ result = (result < 0);
+ break;
+ case Py_GT:
+ result = (result > 0);
+ break;
+ }
+ return PyBool_FromLong(result);
}
static PyTypeObject kqueue_event_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "select.kevent", /* tp_name */
- sizeof(kqueue_event_Object), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)kqueue_event_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- kqueue_event_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- kqueue_event_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)kqueue_event_init, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "select.kevent", /* tp_name */
+ sizeof(kqueue_event_Object), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)kqueue_event_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ kqueue_event_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ kqueue_event_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)kqueue_event_init, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
};
static PyObject *
kqueue_queue_err_closed(void)
{
- PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
+ return NULL;
}
static int
kqueue_queue_internal_close(kqueue_queue_Object *self)
{
- int save_errno = 0;
- if (self->kqfd >= 0) {
- int kqfd = self->kqfd;
- self->kqfd = -1;
- Py_BEGIN_ALLOW_THREADS
- if (close(kqfd) < 0)
- save_errno = errno;
- Py_END_ALLOW_THREADS
- }
- return save_errno;
+ int save_errno = 0;
+ if (self->kqfd >= 0) {
+ int kqfd = self->kqfd;
+ self->kqfd = -1;
+ Py_BEGIN_ALLOW_THREADS
+ if (close(kqfd) < 0)
+ save_errno = errno;
+ Py_END_ALLOW_THREADS
+ }
+ return save_errno;
}
static PyObject *
newKqueue_Object(PyTypeObject *type, SOCKET fd)
{
- kqueue_queue_Object *self;
- assert(type != NULL && type->tp_alloc != NULL);
- self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
- if (self == NULL) {
- return NULL;
- }
-
- if (fd == -1) {
- Py_BEGIN_ALLOW_THREADS
- self->kqfd = kqueue();
- Py_END_ALLOW_THREADS
- }
- else {
- self->kqfd = fd;
- }
- if (self->kqfd < 0) {
- Py_DECREF(self);
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return (PyObject *)self;
+ kqueue_queue_Object *self;
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
+ if (self == NULL) {
+ return NULL;
+ }
+
+ if (fd == -1) {
+ Py_BEGIN_ALLOW_THREADS
+ self->kqfd = kqueue();
+ Py_END_ALLOW_THREADS
+ }
+ else {
+ self->kqfd = fd;
+ }
+ if (self->kqfd < 0) {
+ Py_DECREF(self);
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return (PyObject *)self;
}
static PyObject *
kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- if ((args != NULL && PyObject_Size(args)) ||
- (kwds != NULL && PyObject_Size(kwds))) {
- PyErr_SetString(PyExc_ValueError,
- "select.kqueue doesn't accept arguments");
- return NULL;
- }
+ if ((args != NULL && PyObject_Size(args)) ||
+ (kwds != NULL && PyObject_Size(kwds))) {
+ PyErr_SetString(PyExc_ValueError,
+ "select.kqueue doesn't accept arguments");
+ return NULL;
+ }
- return newKqueue_Object(type, -1);
+ return newKqueue_Object(type, -1);
}
static void
kqueue_queue_dealloc(kqueue_queue_Object *self)
{
- kqueue_queue_internal_close(self);
- Py_TYPE(self)->tp_free(self);
+ kqueue_queue_internal_close(self);
+ Py_TYPE(self)->tp_free(self);
}
static PyObject*
kqueue_queue_close(kqueue_queue_Object *self)
{
- errno = kqueue_queue_internal_close(self);
- if (errno < 0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- Py_RETURN_NONE;
+ errno = kqueue_queue_internal_close(self);
+ if (errno < 0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(kqueue_queue_close_doc,
@@ -1424,18 +1424,18 @@ object will raise an exception.");
static PyObject*
kqueue_queue_get_closed(kqueue_queue_Object *self)
{
- if (self->kqfd < 0)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
+ if (self->kqfd < 0)
+ Py_RETURN_TRUE;
+ else
+ Py_RETURN_FALSE;
}
static PyObject*
kqueue_queue_fileno(kqueue_queue_Object *self)
{
- if (self->kqfd < 0)
- return kqueue_queue_err_closed();
- return PyLong_FromLong(self->kqfd);
+ if (self->kqfd < 0)
+ return kqueue_queue_err_closed();
+ return PyLong_FromLong(self->kqfd);
}
PyDoc_STRVAR(kqueue_queue_fileno_doc,
@@ -1446,12 +1446,12 @@ Return the kqueue control file descriptor.");
static PyObject*
kqueue_queue_fromfd(PyObject *cls, PyObject *args)
{
- SOCKET fd;
+ SOCKET fd;
- if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
+ return NULL;
- return newKqueue_Object((PyTypeObject*)cls, fd);
+ return newKqueue_Object((PyTypeObject*)cls, fd);
}
PyDoc_STRVAR(kqueue_queue_fromfd_doc,
@@ -1462,146 +1462,146 @@ Create a kqueue object from a given control fd.");
static PyObject *
kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
{
- int nevents = 0;
- int gotevents = 0;
- int nchanges = 0;
- int i = 0;
- PyObject *otimeout = NULL;
- PyObject *ch = NULL;
- PyObject *it = NULL, *ei = NULL;
- PyObject *result = NULL;
- struct kevent *evl = NULL;
- struct kevent *chl = NULL;
- struct timespec timeoutspec;
- struct timespec *ptimeoutspec;
-
- if (self->kqfd < 0)
- return kqueue_queue_err_closed();
-
- if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
- return NULL;
-
- if (nevents < 0) {
- PyErr_Format(PyExc_ValueError,
- "Length of eventlist must be 0 or positive, got %d",
- nevents);
- return NULL;
- }
-
- if (ch != NULL && ch != Py_None) {
- it = PyObject_GetIter(ch);
- if (it == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "changelist is not iterable");
- return NULL;
- }
- nchanges = PyObject_Size(ch);
- if (nchanges < 0) {
- return NULL;
- }
- }
-
- if (otimeout == Py_None || otimeout == NULL) {
- ptimeoutspec = NULL;
- }
- else if (PyNumber_Check(otimeout)) {
- double timeout;
- long seconds;
-
- timeout = PyFloat_AsDouble(otimeout);
- if (timeout == -1 && PyErr_Occurred())
- return NULL;
- if (timeout > (double)LONG_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "timeout period too long");
- return NULL;
- }
- if (timeout < 0) {
- PyErr_SetString(PyExc_ValueError,
- "timeout must be positive or None");
- return NULL;
- }
-
- seconds = (long)timeout;
- timeout = timeout - (double)seconds;
- timeoutspec.tv_sec = seconds;
- timeoutspec.tv_nsec = (long)(timeout * 1E9);
- ptimeoutspec = &timeoutspec;
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "timeout argument must be an number "
- "or None, got %.200s",
- Py_TYPE(otimeout)->tp_name);
- return NULL;
- }
-
- if (nchanges) {
- chl = PyMem_New(struct kevent, nchanges);
- if (chl == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- i = 0;
- while ((ei = PyIter_Next(it)) != NULL) {
- if (!kqueue_event_Check(ei)) {
- Py_DECREF(ei);
- PyErr_SetString(PyExc_TypeError,
- "changelist must be an iterable of "
- "select.kevent objects");
- goto error;
- } else {
- chl[i++] = ((kqueue_event_Object *)ei)->e;
- }
- Py_DECREF(ei);
- }
- }
- Py_CLEAR(it);
-
- /* event list */
- if (nevents) {
- evl = PyMem_New(struct kevent, nevents);
- if (evl == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- }
-
- Py_BEGIN_ALLOW_THREADS
- gotevents = kevent(self->kqfd, chl, nchanges,
- evl, nevents, ptimeoutspec);
- Py_END_ALLOW_THREADS
-
- if (gotevents == -1) {
- PyErr_SetFromErrno(PyExc_OSError);
- goto error;
- }
-
- result = PyList_New(gotevents);
- if (result == NULL) {
- goto error;
- }
-
- for (i = 0; i < gotevents; i++) {
- kqueue_event_Object *ch;
-
- ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
- if (ch == NULL) {
- goto error;
- }
- ch->e = evl[i];
- PyList_SET_ITEM(result, i, (PyObject *)ch);
- }
- PyMem_Free(chl);
- PyMem_Free(evl);
- return result;
+ int nevents = 0;
+ int gotevents = 0;
+ int nchanges = 0;
+ int i = 0;
+ PyObject *otimeout = NULL;
+ PyObject *ch = NULL;
+ PyObject *it = NULL, *ei = NULL;
+ PyObject *result = NULL;
+ struct kevent *evl = NULL;
+ struct kevent *chl = NULL;
+ struct timespec timeoutspec;
+ struct timespec *ptimeoutspec;
+
+ if (self->kqfd < 0)
+ return kqueue_queue_err_closed();
+
+ if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
+ return NULL;
+
+ if (nevents < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "Length of eventlist must be 0 or positive, got %d",
+ nevents);
+ return NULL;
+ }
+
+ if (ch != NULL && ch != Py_None) {
+ it = PyObject_GetIter(ch);
+ if (it == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "changelist is not iterable");
+ return NULL;
+ }
+ nchanges = PyObject_Size(ch);
+ if (nchanges < 0) {
+ return NULL;
+ }
+ }
+
+ if (otimeout == Py_None || otimeout == NULL) {
+ ptimeoutspec = NULL;
+ }
+ else if (PyNumber_Check(otimeout)) {
+ double timeout;
+ long seconds;
+
+ timeout = PyFloat_AsDouble(otimeout);
+ if (timeout == -1 && PyErr_Occurred())
+ return NULL;
+ if (timeout > (double)LONG_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "timeout period too long");
+ return NULL;
+ }
+ if (timeout < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "timeout must be positive or None");
+ return NULL;
+ }
+
+ seconds = (long)timeout;
+ timeout = timeout - (double)seconds;
+ timeoutspec.tv_sec = seconds;
+ timeoutspec.tv_nsec = (long)(timeout * 1E9);
+ ptimeoutspec = &timeoutspec;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "timeout argument must be an number "
+ "or None, got %.200s",
+ Py_TYPE(otimeout)->tp_name);
+ return NULL;
+ }
+
+ if (nchanges) {
+ chl = PyMem_New(struct kevent, nchanges);
+ if (chl == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ i = 0;
+ while ((ei = PyIter_Next(it)) != NULL) {
+ if (!kqueue_event_Check(ei)) {
+ Py_DECREF(ei);
+ PyErr_SetString(PyExc_TypeError,
+ "changelist must be an iterable of "
+ "select.kevent objects");
+ goto error;
+ } else {
+ chl[i++] = ((kqueue_event_Object *)ei)->e;
+ }
+ Py_DECREF(ei);
+ }
+ }
+ Py_CLEAR(it);
+
+ /* event list */
+ if (nevents) {
+ evl = PyMem_New(struct kevent, nevents);
+ if (evl == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ gotevents = kevent(self->kqfd, chl, nchanges,
+ evl, nevents, ptimeoutspec);
+ Py_END_ALLOW_THREADS
+
+ if (gotevents == -1) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ goto error;
+ }
+
+ result = PyList_New(gotevents);
+ if (result == NULL) {
+ goto error;
+ }
+
+ for (i = 0; i < gotevents; i++) {
+ kqueue_event_Object *ch;
+
+ ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
+ if (ch == NULL) {
+ goto error;
+ }
+ ch->e = evl[i];
+ PyList_SET_ITEM(result, i, (PyObject *)ch);
+ }
+ PyMem_Free(chl);
+ PyMem_Free(evl);
+ return result;
error:
- PyMem_Free(chl);
- PyMem_Free(evl);
- Py_XDECREF(result);
- Py_XDECREF(it);
- return NULL;
+ PyMem_Free(chl);
+ PyMem_Free(evl);
+ Py_XDECREF(result);
+ Py_XDECREF(it);
+ return NULL;
}
PyDoc_STRVAR(kqueue_queue_control_doc,
@@ -1617,21 +1617,21 @@ Calls the kernel kevent function.\n\
static PyMethodDef kqueue_queue_methods[] = {
- {"fromfd", (PyCFunction)kqueue_queue_fromfd,
- METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
- {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
- kqueue_queue_close_doc},
- {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
- kqueue_queue_fileno_doc},
- {"control", (PyCFunction)kqueue_queue_control,
- METH_VARARGS , kqueue_queue_control_doc},
- {NULL, NULL},
+ {"fromfd", (PyCFunction)kqueue_queue_fromfd,
+ METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
+ {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
+ kqueue_queue_close_doc},
+ {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
+ kqueue_queue_fileno_doc},
+ {"control", (PyCFunction)kqueue_queue_control,
+ METH_VARARGS , kqueue_queue_control_doc},
+ {NULL, NULL},
};
static PyGetSetDef kqueue_queue_getsetlist[] = {
- {"closed", (getter)kqueue_queue_get_closed, NULL,
- "True if the kqueue handler is closed"},
- {0},
+ {"closed", (getter)kqueue_queue_get_closed, NULL,
+ "True if the kqueue handler is closed"},
+ {0},
};
PyDoc_STRVAR(kqueue_queue_doc,
@@ -1650,45 +1650,45 @@ To stop listening:\n\
>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
static PyTypeObject kqueue_queue_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "select.kqueue", /* tp_name */
- sizeof(kqueue_queue_Object), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)kqueue_queue_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- kqueue_queue_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- kqueue_queue_methods, /* tp_methods */
- 0, /* tp_members */
- kqueue_queue_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- kqueue_queue_new, /* tp_new */
- 0, /* tp_free */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "select.kqueue", /* tp_name */
+ sizeof(kqueue_queue_Object), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)kqueue_queue_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ kqueue_queue_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ kqueue_queue_methods, /* tp_methods */
+ 0, /* tp_members */
+ kqueue_queue_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ kqueue_queue_new, /* tp_new */
+ 0, /* tp_free */
};
#endif /* HAVE_KQUEUE */
@@ -1719,11 +1719,11 @@ On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
descriptors can be used.");
static PyMethodDef select_methods[] = {
- {"select", select_select, METH_VARARGS, select_doc},
+ {"select", select_select, METH_VARARGS, select_doc},
#ifdef HAVE_POLL
- {"poll", select_poll, METH_NOARGS, poll_doc},
+ {"poll", select_poll, METH_NOARGS, poll_doc},
#endif /* HAVE_POLL */
- {0, 0}, /* sentinel */
+ {0, 0}, /* sentinel */
};
PyDoc_STRVAR(module_doc,
@@ -1734,163 +1734,163 @@ On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptor
static struct PyModuleDef selectmodule = {
- PyModuleDef_HEAD_INIT,
- "select",
- module_doc,
- -1,
- select_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "select",
+ module_doc,
+ -1,
+ select_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_select(void)
{
- PyObject *m;
- m = PyModule_Create(&selectmodule);
- if (m == NULL)
- return NULL;
+ PyObject *m;
+ m = PyModule_Create(&selectmodule);
+ if (m == NULL)
+ return NULL;
- SelectError = PyErr_NewException("select.error", NULL, NULL);
- Py_INCREF(SelectError);
- PyModule_AddObject(m, "error", SelectError);
+ SelectError = PyErr_NewException("select.error", NULL, NULL);
+ Py_INCREF(SelectError);
+ PyModule_AddObject(m, "error", SelectError);
#if defined(HAVE_POLL)
#ifdef __APPLE__
- if (select_have_broken_poll()) {
- if (PyObject_DelAttrString(m, "poll") == -1) {
- PyErr_Clear();
- }
- } else {
+ if (select_have_broken_poll()) {
+ if (PyObject_DelAttrString(m, "poll") == -1) {
+ PyErr_Clear();
+ }
+ } else {
#else
- {
+ {
#endif
- if (PyType_Ready(&poll_Type) < 0)
- return NULL;
- PyModule_AddIntConstant(m, "POLLIN", POLLIN);
- PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
- PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
- PyModule_AddIntConstant(m, "POLLERR", POLLERR);
- PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
- PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
+ if (PyType_Ready(&poll_Type) < 0)
+ return NULL;
+ PyModule_AddIntConstant(m, "POLLIN", POLLIN);
+ PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
+ PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
+ PyModule_AddIntConstant(m, "POLLERR", POLLERR);
+ PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
+ PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
#ifdef POLLRDNORM
- PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
+ PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
#endif
#ifdef POLLRDBAND
- PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
+ PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
#endif
#ifdef POLLWRNORM
- PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
+ PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
#endif
#ifdef POLLWRBAND
- PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
+ PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
#endif
#ifdef POLLMSG
- PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
+ PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
#endif
- }
+ }
#endif /* HAVE_POLL */
#ifdef HAVE_EPOLL
- Py_TYPE(&pyEpoll_Type) = &PyType_Type;
- if (PyType_Ready(&pyEpoll_Type) < 0)
- return NULL;
-
- Py_INCREF(&pyEpoll_Type);
- PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
-
- PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
- PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
- PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
- PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
- PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
- PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
+ Py_TYPE(&pyEpoll_Type) = &PyType_Type;
+ if (PyType_Ready(&pyEpoll_Type) < 0)
+ return NULL;
+
+ Py_INCREF(&pyEpoll_Type);
+ PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
+
+ PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
+ PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
+ PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
+ PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
+ PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
+ PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
#ifdef EPOLLONESHOT
- /* Kernel 2.6.2+ */
- PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
+ /* Kernel 2.6.2+ */
+ PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
#endif
- /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
- PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
- PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
- PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
- PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
- PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
+ /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
+ PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
+ PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
+ PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
+ PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
+ PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
#endif /* HAVE_EPOLL */
#ifdef HAVE_KQUEUE
- kqueue_event_Type.tp_new = PyType_GenericNew;
- Py_TYPE(&kqueue_event_Type) = &PyType_Type;
- if(PyType_Ready(&kqueue_event_Type) < 0)
- return NULL;
-
- Py_INCREF(&kqueue_event_Type);
- PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
-
- Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
- if(PyType_Ready(&kqueue_queue_Type) < 0)
- return NULL;
- Py_INCREF(&kqueue_queue_Type);
- PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
-
- /* event filters */
- PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
- PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
- PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
- PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
- PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
+ kqueue_event_Type.tp_new = PyType_GenericNew;
+ Py_TYPE(&kqueue_event_Type) = &PyType_Type;
+ if(PyType_Ready(&kqueue_event_Type) < 0)
+ return NULL;
+
+ Py_INCREF(&kqueue_event_Type);
+ PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
+
+ Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
+ if(PyType_Ready(&kqueue_queue_Type) < 0)
+ return NULL;
+ Py_INCREF(&kqueue_queue_Type);
+ PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
+
+ /* event filters */
+ PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
+ PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
+ PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
+ PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
+ PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
#ifdef EVFILT_NETDEV
- PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
+ PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
#endif
- PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
- PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
-
- /* event flags */
- PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
- PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
- PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
- PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
- PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
- PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
-
- PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
- PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
-
- PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
- PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
-
- /* READ WRITE filter flag */
- PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
-
- /* VNODE filter flags */
- PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
- PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
- PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
- PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
- PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
- PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
- PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
-
- /* PROC filter flags */
- PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
- PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
- PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
- PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
- PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
-
- PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
- PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
- PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
-
- /* NETDEV filter flags */
+ PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
+ PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
+
+ /* event flags */
+ PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
+ PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
+ PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
+ PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
+ PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
+ PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
+
+ PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
+ PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
+
+ PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
+ PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
+
+ /* READ WRITE filter flag */
+ PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
+
+ /* VNODE filter flags */
+ PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
+ PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
+ PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
+ PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
+ PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
+ PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
+ PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
+
+ /* PROC filter flags */
+ PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
+ PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
+ PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
+ PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
+ PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
+
+ PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
+ PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
+ PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
+
+ /* NETDEV filter flags */
#ifdef EVFILT_NETDEV
- PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
- PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
- PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
+ PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
+ PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
+ PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
#endif
#endif /* HAVE_KQUEUE */
- return m;
+ return m;
}
diff --git a/Modules/sha1module.c b/Modules/sha1module.c
index 3e630eb..b8e8880 100644
--- a/Modules/sha1module.c
+++ b/Modules/sha1module.c
@@ -23,8 +23,8 @@
/* Some useful types */
#if SIZEOF_INT == 4
-typedef unsigned int SHA1_INT32; /* 32-bit integer */
-typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
+typedef unsigned int SHA1_INT32; /* 32-bit integer */
+typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
#else
/* not defined. compilation will die. */
#endif
@@ -122,7 +122,7 @@ static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
/* expand it */
for (i = 16; i < 80; i++) {
- W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
+ W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
}
/* compress */
@@ -131,7 +131,7 @@ static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
#define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
#define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
#define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
-
+
for (i = 0; i < 20; ) {
FF0(a,b,c,d,e,i++);
FF0(e,a,b,c,d,i++);
@@ -141,7 +141,7 @@ static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
}
/* round two */
- for (; i < 40; ) {
+ for (; i < 40; ) {
FF1(a,b,c,d,e,i++);
FF1(e,a,b,c,d,i++);
FF1(d,e,a,b,c,i++);
@@ -150,7 +150,7 @@ static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
}
/* round three */
- for (; i < 60; ) {
+ for (; i < 60; ) {
FF2(a,b,c,d,e,i++);
FF2(e,a,b,c,d,i++);
FF2(d,e,a,b,c,i++);
@@ -159,7 +159,7 @@ static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
}
/* round four */
- for (; i < 80; ) {
+ for (; i < 80; ) {
FF3(a,b,c,d,e,i++);
FF3(e,a,b,c,d,i++);
FF3(d,e,a,b,c,i++);
@@ -362,21 +362,21 @@ SHA1_hexdigest(SHA1object *self, PyObject *unused)
/* Create a new string */
retval = PyUnicode_FromStringAndSize(NULL, SHA1_DIGESTSIZE * 2);
if (!retval)
- return NULL;
+ return NULL;
hex_digest = PyUnicode_AS_UNICODE(retval);
if (!hex_digest) {
- Py_DECREF(retval);
- return NULL;
+ Py_DECREF(retval);
+ return NULL;
}
/* Make hex version of the digest */
for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
char c;
c = (digest[i] >> 4) & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xf);
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
}
return retval;
@@ -404,11 +404,11 @@ SHA1_update(SHA1object *self, PyObject *args)
}
static PyMethodDef SHA1_methods[] = {
- {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__},
- {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__},
+ {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__},
+ {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__},
{"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__},
- {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__},
- {NULL, NULL} /* sentinel */
+ {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -448,13 +448,13 @@ static PyGetSetDef SHA1_getseters[] = {
static PyTypeObject SHA1type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_sha1.sha1", /*tp_name*/
- sizeof(SHA1object), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_sha1.sha1", /*tp_name*/
+ sizeof(SHA1object), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- SHA1_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ SHA1_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -470,13 +470,13 @@ static PyTypeObject SHA1type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- SHA1_methods, /* tp_methods */
- NULL, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ SHA1_methods, /* tp_methods */
+ NULL, /* tp_members */
SHA1_getseters, /* tp_getset */
};
@@ -529,7 +529,7 @@ SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict)
static struct PyMethodDef SHA1_functions[] = {
{"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__},
- {NULL, NULL} /* Sentinel */
+ {NULL, NULL} /* Sentinel */
};
@@ -539,15 +539,15 @@ static struct PyMethodDef SHA1_functions[] = {
static struct PyModuleDef _sha1module = {
- PyModuleDef_HEAD_INIT,
- "_sha1",
- NULL,
- -1,
- SHA1_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_sha1",
+ NULL,
+ -1,
+ SHA1_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
diff --git a/Modules/sha256module.c b/Modules/sha256module.c
index a9ac748..705b31a 100644
--- a/Modules/sha256module.c
+++ b/Modules/sha256module.c
@@ -23,7 +23,7 @@
/* Endianness testing and definitions */
#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
- if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
+ if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
#define PCT_LITTLE_ENDIAN 1
#define PCT_BIG_ENDIAN 0
@@ -33,7 +33,7 @@
typedef unsigned char SHA_BYTE;
#if SIZEOF_INT == 4
-typedef unsigned int SHA_INT32; /* 32-bit integer */
+typedef unsigned int SHA_INT32; /* 32-bit integer */
#else
/* not defined. compilation will die. */
#endif
@@ -47,11 +47,11 @@ typedef unsigned int SHA_INT32; /* 32-bit integer */
typedef struct {
PyObject_HEAD
- SHA_INT32 digest[8]; /* Message digest */
- SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
- SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
+ SHA_INT32 digest[8]; /* Message digest */
+ SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
+ SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
int Endianness;
- int local; /* unprocessed amount in data */
+ int local; /* unprocessed amount in data */
int digestsize;
} SHAobject;
@@ -63,7 +63,7 @@ static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
SHA_INT32 value;
if ( Endianness == PCT_BIG_ENDIAN )
- return;
+ return;
byteCount /= sizeof(*buffer);
while (byteCount--) {
@@ -115,7 +115,7 @@ static void SHAcopy(SHAobject *src, SHAobject *dest)
( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
-#define Maj(x,y,z) (((x | y) & z) | (x & y))
+#define Maj(x,y,z) (((x | y) & z) | (x & y))
#define S(x, n) ROR((x),(n))
#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
@@ -128,13 +128,13 @@ static void
sha_transform(SHAobject *sha_info)
{
int i;
- SHA_INT32 S[8], W[64], t0, t1;
+ SHA_INT32 S[8], W[64], t0, t1;
memcpy(W, sha_info->data, sizeof(sha_info->data));
longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
for (i = 16; i < 64; ++i) {
- W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
+ W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
}
for (i = 0; i < 8; ++i) {
S[i] = sha_info->digest[i];
@@ -212,8 +212,8 @@ sha_transform(SHAobject *sha_info)
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
-#undef RND
-
+#undef RND
+
/* feedback */
for (i = 0; i < 8; i++) {
sha_info->digest[i] = sha_info->digest[i] + S[i];
@@ -315,14 +315,14 @@ sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
count = (int) ((lo_bit_count >> 3) & 0x3f);
((SHA_BYTE *) sha_info->data)[count++] = 0x80;
if (count > SHA_BLOCKSIZE - 8) {
- memset(((SHA_BYTE *) sha_info->data) + count, 0,
- SHA_BLOCKSIZE - count);
- sha_transform(sha_info);
- memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
+ memset(((SHA_BYTE *) sha_info->data) + count, 0,
+ SHA_BLOCKSIZE - count);
+ sha_transform(sha_info);
+ memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
}
else {
- memset(((SHA_BYTE *) sha_info->data) + count, 0,
- SHA_BLOCKSIZE - 8 - count);
+ memset(((SHA_BYTE *) sha_info->data) + count, 0,
+ SHA_BLOCKSIZE - 8 - count);
}
/* GJS: note that we add the hi/lo in big-endian. sha_transform will
@@ -455,21 +455,21 @@ SHA256_hexdigest(SHAobject *self, PyObject *unused)
/* Create a new string */
retval = PyUnicode_FromStringAndSize(NULL, self->digestsize * 2);
if (!retval)
- return NULL;
+ return NULL;
hex_digest = PyUnicode_AS_UNICODE(retval);
if (!hex_digest) {
- Py_DECREF(retval);
- return NULL;
+ Py_DECREF(retval);
+ return NULL;
}
/* Make hex version of the digest */
for(i=j=0; i<self->digestsize; i++) {
char c;
c = (digest[i] >> 4) & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xf);
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
}
return retval;
@@ -497,11 +497,11 @@ SHA256_update(SHAobject *self, PyObject *args)
}
static PyMethodDef SHA_methods[] = {
- {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__},
- {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__},
+ {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__},
+ {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__},
{"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest__doc__},
- {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__},
- {NULL, NULL} /* sentinel */
+ {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -538,13 +538,13 @@ static PyMemberDef SHA_members[] = {
static PyTypeObject SHA224type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_sha256.sha224", /*tp_name*/
- sizeof(SHAobject), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_sha256.sha224", /*tp_name*/
+ sizeof(SHAobject), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- SHA_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ SHA_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -560,25 +560,25 @@ static PyTypeObject SHA224type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- SHA_methods, /* tp_methods */
- SHA_members, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ SHA_methods, /* tp_methods */
+ SHA_members, /* tp_members */
SHA_getseters, /* tp_getset */
};
static PyTypeObject SHA256type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_sha256.sha256", /*tp_name*/
- sizeof(SHAobject), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_sha256.sha256", /*tp_name*/
+ sizeof(SHAobject), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- SHA_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ SHA_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -594,13 +594,13 @@ static PyTypeObject SHA256type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- SHA_methods, /* tp_methods */
- SHA_members, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ SHA_methods, /* tp_methods */
+ SHA_members, /* tp_members */
SHA_getseters, /* tp_getset */
};
@@ -695,7 +695,7 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
static struct PyMethodDef SHA_functions[] = {
{"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__doc__},
{"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__doc__},
- {NULL, NULL} /* Sentinel */
+ {NULL, NULL} /* Sentinel */
};
@@ -705,15 +705,15 @@ static struct PyMethodDef SHA_functions[] = {
static struct PyModuleDef _sha256module = {
- PyModuleDef_HEAD_INIT,
- "_sha256",
- NULL,
- -1,
- SHA_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_sha256",
+ NULL,
+ -1,
+ SHA_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
index e29ddec..c403ed6 100644
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -24,7 +24,7 @@
/* Endianness testing and definitions */
#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
- if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
+ if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
#define PCT_LITTLE_ENDIAN 1
#define PCT_BIG_ENDIAN 0
@@ -34,8 +34,8 @@
typedef unsigned char SHA_BYTE;
#if SIZEOF_INT == 4
-typedef unsigned int SHA_INT32; /* 32-bit integer */
-typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
+typedef unsigned int SHA_INT32; /* 32-bit integer */
+typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
#else
/* not defined. compilation will die. */
#endif
@@ -49,11 +49,11 @@ typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
typedef struct {
PyObject_HEAD
- SHA_INT64 digest[8]; /* Message digest */
- SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
- SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
+ SHA_INT64 digest[8]; /* Message digest */
+ SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
+ SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
int Endianness;
- int local; /* unprocessed amount in data */
+ int local; /* unprocessed amount in data */
int digestsize;
} SHAobject;
@@ -65,22 +65,22 @@ static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness)
SHA_INT64 value;
if ( Endianness == PCT_BIG_ENDIAN )
- return;
+ return;
byteCount /= sizeof(*buffer);
while (byteCount--) {
value = *buffer;
- ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
- ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
- ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
- ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
- ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
- ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
- ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
- ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
-
- buffer++;
+ ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
+ ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
+ ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
+ ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
+ ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
+ ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
+ ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
+ ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
+
+ buffer++;
}
}
@@ -125,7 +125,7 @@ static void SHAcopy(SHAobject *src, SHAobject *dest)
( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
-#define Maj(x,y,z) (((x | y) & z) | (x & y))
+#define Maj(x,y,z) (((x | y) & z) | (x & y))
#define S(x, n) ROR64((x),(n))
#define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
@@ -144,7 +144,7 @@ sha512_transform(SHAobject *sha_info)
longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
for (i = 16; i < 80; ++i) {
- W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
+ W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
}
for (i = 0; i < 8; ++i) {
S[i] = sha_info->digest[i];
@@ -238,8 +238,8 @@ sha512_transform(SHAobject *sha_info)
RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
-#undef RND
-
+#undef RND
+
/* feedback */
for (i = 0; i < 8; i++) {
sha_info->digest[i] = sha_info->digest[i] + S[i];
@@ -341,14 +341,14 @@ sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
count = (int) ((lo_bit_count >> 3) & 0x7f);
((SHA_BYTE *) sha_info->data)[count++] = 0x80;
if (count > SHA_BLOCKSIZE - 16) {
- memset(((SHA_BYTE *) sha_info->data) + count, 0,
- SHA_BLOCKSIZE - count);
- sha512_transform(sha_info);
- memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
+ memset(((SHA_BYTE *) sha_info->data) + count, 0,
+ SHA_BLOCKSIZE - count);
+ sha512_transform(sha_info);
+ memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
}
else {
- memset(((SHA_BYTE *) sha_info->data) + count, 0,
- SHA_BLOCKSIZE - 16 - count);
+ memset(((SHA_BYTE *) sha_info->data) + count, 0,
+ SHA_BLOCKSIZE - 16 - count);
}
/* GJS: note that we add the hi/lo in big-endian. sha512_transform will
@@ -521,21 +521,21 @@ SHA512_hexdigest(SHAobject *self, PyObject *unused)
/* Create a new string */
retval = PyUnicode_FromStringAndSize(NULL, self->digestsize * 2);
if (!retval)
- return NULL;
+ return NULL;
hex_digest = PyUnicode_AS_UNICODE(retval);
if (!hex_digest) {
- Py_DECREF(retval);
- return NULL;
+ Py_DECREF(retval);
+ return NULL;
}
/* Make hex version of the digest */
for (i=j=0; i<self->digestsize; i++) {
char c;
c = (digest[i] >> 4) & 0xf;
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xf);
- c = (c>9) ? c+'a'-10 : c + '0';
+ c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
}
return retval;
@@ -563,11 +563,11 @@ SHA512_update(SHAobject *self, PyObject *args)
}
static PyMethodDef SHA_methods[] = {
- {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__},
- {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__},
+ {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__},
+ {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__},
{"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__},
- {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__},
- {NULL, NULL} /* sentinel */
+ {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -604,13 +604,13 @@ static PyMemberDef SHA_members[] = {
static PyTypeObject SHA384type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_sha512.sha384", /*tp_name*/
- sizeof(SHAobject), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_sha512.sha384", /*tp_name*/
+ sizeof(SHAobject), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- SHA512_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ SHA512_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -626,25 +626,25 @@ static PyTypeObject SHA384type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- SHA_methods, /* tp_methods */
- SHA_members, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ SHA_methods, /* tp_methods */
+ SHA_members, /* tp_members */
SHA_getseters, /* tp_getset */
};
static PyTypeObject SHA512type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "_sha512.sha512", /*tp_name*/
- sizeof(SHAobject), /*tp_size*/
- 0, /*tp_itemsize*/
+ "_sha512.sha512", /*tp_name*/
+ sizeof(SHAobject), /*tp_size*/
+ 0, /*tp_itemsize*/
/* methods */
- SHA512_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
+ SHA512_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_reserved*/
0, /*tp_repr*/
@@ -660,13 +660,13 @@ static PyTypeObject SHA512type = {
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- SHA_methods, /* tp_methods */
- SHA_members, /* tp_members */
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ SHA_methods, /* tp_methods */
+ SHA_members, /* tp_members */
SHA_getseters, /* tp_getset */
};
@@ -761,7 +761,7 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
static struct PyMethodDef SHA_functions[] = {
{"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__},
{"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__},
- {NULL, NULL} /* Sentinel */
+ {NULL, NULL} /* Sentinel */
};
@@ -771,15 +771,15 @@ static struct PyMethodDef SHA_functions[] = {
static struct PyModuleDef _sha512module = {
- PyModuleDef_HEAD_INIT,
- "_sha512",
- NULL,
- -1,
- SHA_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_sha512",
+ NULL,
+ -1,
+ SHA_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 26a4e79..e420a1b 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -28,13 +28,13 @@
#ifndef NSIG
# if defined(_NSIG)
-# define NSIG _NSIG /* For BSD/SysV */
+# define NSIG _NSIG /* For BSD/SysV */
# elif defined(_SIGMAX)
-# define NSIG (_SIGMAX + 1) /* For QNX */
+# define NSIG (_SIGMAX + 1) /* For QNX */
# elif defined(SIGMAX)
-# define NSIG (SIGMAX + 1) /* For djgpp */
+# define NSIG (SIGMAX + 1) /* For djgpp */
# else
-# define NSIG 64 /* Use a reasonable default value */
+# define NSIG 64 /* Use a reasonable default value */
# endif
#endif
@@ -76,8 +76,8 @@ static pid_t main_pid;
#endif
static struct {
- int tripped;
- PyObject *func;
+ int tripped;
+ PyObject *func;
} Handlers[NSIG];
static sig_atomic_t wakeup_fd = -1;
@@ -120,18 +120,18 @@ itimer_retval(struct itimerval *iv)
r = PyTuple_New(2);
if (r == NULL)
- return NULL;
+ return NULL;
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
- Py_DECREF(r);
- return NULL;
+ Py_DECREF(r);
+ return NULL;
}
PyTuple_SET_ITEM(r, 0, v);
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
- Py_DECREF(r);
- return NULL;
+ Py_DECREF(r);
+ return NULL;
}
PyTuple_SET_ITEM(r, 1, v);
@@ -143,8 +143,8 @@ itimer_retval(struct itimerval *iv)
static PyObject *
signal_default_int_handler(PyObject *self, PyObject *args)
{
- PyErr_SetNone(PyExc_KeyboardInterrupt);
- return NULL;
+ PyErr_SetNone(PyExc_KeyboardInterrupt);
+ return NULL;
}
PyDoc_STRVAR(default_int_handler_doc,
@@ -157,7 +157,7 @@ It raises KeyboardInterrupt.");
static int
checksignals_witharg(void * unused)
{
- return PyErr_CheckSignals();
+ return PyErr_CheckSignals();
}
static void
@@ -165,38 +165,38 @@ signal_handler(int sig_num)
{
#ifdef WITH_THREAD
#ifdef WITH_PTH
- if (PyThread_get_thread_ident() != main_thread) {
- pth_raise(*(pth_t *) main_thread, sig_num);
- return;
- }
-#endif
- /* See NOTES section above */
- if (getpid() == main_pid) {
-#endif
- Handlers[sig_num].tripped = 1;
- /* Set is_tripped after setting .tripped, as it gets
- cleared in PyErr_CheckSignals() before .tripped. */
- is_tripped = 1;
- Py_AddPendingCall(checksignals_witharg, NULL);
- if (wakeup_fd != -1)
- write(wakeup_fd, "\0", 1);
+ if (PyThread_get_thread_ident() != main_thread) {
+ pth_raise(*(pth_t *) main_thread, sig_num);
+ return;
+ }
+#endif
+ /* See NOTES section above */
+ if (getpid() == main_pid) {
+#endif
+ Handlers[sig_num].tripped = 1;
+ /* Set is_tripped after setting .tripped, as it gets
+ cleared in PyErr_CheckSignals() before .tripped. */
+ is_tripped = 1;
+ Py_AddPendingCall(checksignals_witharg, NULL);
+ if (wakeup_fd != -1)
+ write(wakeup_fd, "\0", 1);
#ifdef WITH_THREAD
- }
+ }
#endif
#ifdef SIGCHLD
- if (sig_num == SIGCHLD) {
- /* To avoid infinite recursion, this signal remains
- reset until explicit re-instated.
- Don't clear the 'func' field as it is our pointer
- to the Python handler... */
- return;
- }
+ if (sig_num == SIGCHLD) {
+ /* To avoid infinite recursion, this signal remains
+ reset until explicit re-instated.
+ Don't clear the 'func' field as it is our pointer
+ to the Python handler... */
+ return;
+ }
#endif
#ifndef HAVE_SIGACTION
- /* If the handler was not set up with sigaction, reinstall it. See
- * Python/pythonrun.c for the implementation of PyOS_setsig which
- * makes this true. See also issue8354. */
- PyOS_setsig(sig_num, signal_handler);
+ /* If the handler was not set up with sigaction, reinstall it. See
+ * Python/pythonrun.c for the implementation of PyOS_setsig which
+ * makes this true. See also issue8354. */
+ PyOS_setsig(sig_num, signal_handler);
#endif
}
@@ -205,11 +205,11 @@ signal_handler(int sig_num)
static PyObject *
signal_alarm(PyObject *self, PyObject *args)
{
- int t;
- if (!PyArg_ParseTuple(args, "i:alarm", &t))
- return NULL;
- /* alarm() returns the number of seconds remaining */
- return PyLong_FromLong((long)alarm(t));
+ int t;
+ if (!PyArg_ParseTuple(args, "i:alarm", &t))
+ return NULL;
+ /* alarm() returns the number of seconds remaining */
+ return PyLong_FromLong((long)alarm(t));
}
PyDoc_STRVAR(alarm_doc,
@@ -222,17 +222,17 @@ Arrange for SIGALRM to arrive after the given number of seconds.");
static PyObject *
signal_pause(PyObject *self)
{
- Py_BEGIN_ALLOW_THREADS
- (void)pause();
- Py_END_ALLOW_THREADS
- /* make sure that any exceptions that got raised are propagated
- * back into Python
- */
- if (PyErr_CheckSignals())
- return NULL;
-
- Py_INCREF(Py_None);
- return Py_None;
+ Py_BEGIN_ALLOW_THREADS
+ (void)pause();
+ Py_END_ALLOW_THREADS
+ /* make sure that any exceptions that got raised are propagated
+ * back into Python
+ */
+ if (PyErr_CheckSignals())
+ return NULL;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(pause_doc,
"pause()\n\
@@ -245,44 +245,44 @@ Wait until a signal arrives.");
static PyObject *
signal_signal(PyObject *self, PyObject *args)
{
- PyObject *obj;
- int sig_num;
- PyObject *old_handler;
- void (*func)(int);
- if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
- return NULL;
+ PyObject *obj;
+ int sig_num;
+ PyObject *old_handler;
+ void (*func)(int);
+ if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
+ return NULL;
#ifdef WITH_THREAD
- if (PyThread_get_thread_ident() != main_thread) {
- PyErr_SetString(PyExc_ValueError,
- "signal only works in main thread");
- return NULL;
- }
-#endif
- if (sig_num < 1 || sig_num >= NSIG) {
- PyErr_SetString(PyExc_ValueError,
- "signal number out of range");
- return NULL;
- }
- if (obj == IgnoreHandler)
- func = SIG_IGN;
- else if (obj == DefaultHandler)
- func = SIG_DFL;
- else if (!PyCallable_Check(obj)) {
- PyErr_SetString(PyExc_TypeError,
+ if (PyThread_get_thread_ident() != main_thread) {
+ PyErr_SetString(PyExc_ValueError,
+ "signal only works in main thread");
+ return NULL;
+ }
+#endif
+ if (sig_num < 1 || sig_num >= NSIG) {
+ PyErr_SetString(PyExc_ValueError,
+ "signal number out of range");
+ return NULL;
+ }
+ if (obj == IgnoreHandler)
+ func = SIG_IGN;
+ else if (obj == DefaultHandler)
+ func = SIG_DFL;
+ else if (!PyCallable_Check(obj)) {
+ PyErr_SetString(PyExc_TypeError,
"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
- return NULL;
- }
- else
- func = signal_handler;
- if (PyOS_setsig(sig_num, func) == SIG_ERR) {
- PyErr_SetFromErrno(PyExc_RuntimeError);
- return NULL;
- }
- old_handler = Handlers[sig_num].func;
- Handlers[sig_num].tripped = 0;
- Py_INCREF(obj);
- Handlers[sig_num].func = obj;
- return old_handler;
+ return NULL;
+ }
+ else
+ func = signal_handler;
+ if (PyOS_setsig(sig_num, func) == SIG_ERR) {
+ PyErr_SetFromErrno(PyExc_RuntimeError);
+ return NULL;
+ }
+ old_handler = Handlers[sig_num].func;
+ Handlers[sig_num].tripped = 0;
+ Py_INCREF(obj);
+ Handlers[sig_num].func = obj;
+ return old_handler;
}
PyDoc_STRVAR(signal_doc,
@@ -300,18 +300,18 @@ the first is the signal number, the second is the interrupted stack frame.");
static PyObject *
signal_getsignal(PyObject *self, PyObject *args)
{
- int sig_num;
- PyObject *old_handler;
- if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
- return NULL;
- if (sig_num < 1 || sig_num >= NSIG) {
- PyErr_SetString(PyExc_ValueError,
- "signal number out of range");
- return NULL;
- }
- old_handler = Handlers[sig_num].func;
- Py_INCREF(old_handler);
- return old_handler;
+ int sig_num;
+ PyObject *old_handler;
+ if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
+ return NULL;
+ if (sig_num < 1 || sig_num >= NSIG) {
+ PyErr_SetString(PyExc_ValueError,
+ "signal number out of range");
+ return NULL;
+ }
+ old_handler = Handlers[sig_num].func;
+ Py_INCREF(old_handler);
+ return old_handler;
}
PyDoc_STRVAR(getsignal_doc,
@@ -333,23 +333,23 @@ will be interrupted.");
static PyObject *
signal_siginterrupt(PyObject *self, PyObject *args)
{
- int sig_num;
- int flag;
-
- if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
- return NULL;
- if (sig_num < 1 || sig_num >= NSIG) {
- PyErr_SetString(PyExc_ValueError,
- "signal number out of range");
- return NULL;
- }
- if (siginterrupt(sig_num, flag)<0) {
- PyErr_SetFromErrno(PyExc_RuntimeError);
- return NULL;
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ int sig_num;
+ int flag;
+
+ if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
+ return NULL;
+ if (sig_num < 1 || sig_num >= NSIG) {
+ PyErr_SetString(PyExc_ValueError,
+ "signal number out of range");
+ return NULL;
+ }
+ if (siginterrupt(sig_num, flag)<0) {
+ PyErr_SetFromErrno(PyExc_RuntimeError);
+ return NULL;
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
#endif
@@ -357,24 +357,24 @@ signal_siginterrupt(PyObject *self, PyObject *args)
static PyObject *
signal_set_wakeup_fd(PyObject *self, PyObject *args)
{
- struct stat buf;
- int fd, old_fd;
- if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
- return NULL;
+ struct stat buf;
+ int fd, old_fd;
+ if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
+ return NULL;
#ifdef WITH_THREAD
- if (PyThread_get_thread_ident() != main_thread) {
- PyErr_SetString(PyExc_ValueError,
- "set_wakeup_fd only works in main thread");
- return NULL;
- }
-#endif
- if (fd != -1 && fstat(fd, &buf) != 0) {
- PyErr_SetString(PyExc_ValueError, "invalid fd");
- return NULL;
- }
- old_fd = wakeup_fd;
- wakeup_fd = fd;
- return PyLong_FromLong(old_fd);
+ if (PyThread_get_thread_ident() != main_thread) {
+ PyErr_SetString(PyExc_ValueError,
+ "set_wakeup_fd only works in main thread");
+ return NULL;
+ }
+#endif
+ if (fd != -1 && fstat(fd, &buf) != 0) {
+ PyErr_SetString(PyExc_ValueError, "invalid fd");
+ return NULL;
+ }
+ old_fd = wakeup_fd;
+ wakeup_fd = fd;
+ return PyLong_FromLong(old_fd);
}
PyDoc_STRVAR(set_wakeup_fd_doc,
@@ -390,11 +390,11 @@ The fd must be non-blocking.");
int
PySignal_SetWakeupFd(int fd)
{
- int old_fd = wakeup_fd;
- if (fd < 0)
- fd = -1;
- wakeup_fd = fd;
- return old_fd;
+ int old_fd = wakeup_fd;
+ if (fd < 0)
+ fd = -1;
+ wakeup_fd = fd;
+ return old_fd;
}
@@ -408,14 +408,14 @@ signal_setitimer(PyObject *self, PyObject *args)
struct itimerval new, old;
if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
- return NULL;
+ return NULL;
timeval_from_double(first, &new.it_value);
timeval_from_double(interval, &new.it_interval);
/* Let OS check "which" value */
if (setitimer(which, &new, &old) != 0) {
- PyErr_SetFromErrno(ItimerError);
- return NULL;
+ PyErr_SetFromErrno(ItimerError);
+ return NULL;
}
return itimer_retval(&old);
@@ -441,11 +441,11 @@ signal_getitimer(PyObject *self, PyObject *args)
struct itimerval old;
if (!PyArg_ParseTuple(args, "i:getitimer", &which))
- return NULL;
+ return NULL;
if (getitimer(which, &old) != 0) {
- PyErr_SetFromErrno(ItimerError);
- return NULL;
+ PyErr_SetFromErrno(ItimerError);
+ return NULL;
}
return itimer_retval(&old);
@@ -461,27 +461,27 @@ Returns current value of given itimer.");
/* List of functions defined in the module */
static PyMethodDef signal_methods[] = {
#ifdef HAVE_ALARM
- {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
+ {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
#endif
#ifdef HAVE_SETITIMER
{"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
#endif
#ifdef HAVE_GETITIMER
- {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
+ {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
#endif
- {"signal", signal_signal, METH_VARARGS, signal_doc},
- {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
- {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
+ {"signal", signal_signal, METH_VARARGS, signal_doc},
+ {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
+ {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
#ifdef HAVE_SIGINTERRUPT
- {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
+ {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
#endif
#ifdef HAVE_PAUSE
- {"pause", (PyCFunction)signal_pause,
- METH_NOARGS,pause_doc},
+ {"pause", (PyCFunction)signal_pause,
+ METH_NOARGS,pause_doc},
#endif
- {"default_int_handler", signal_default_int_handler,
- METH_VARARGS, default_int_handler_doc},
- {NULL, NULL} /* sentinel */
+ {"default_int_handler", signal_default_int_handler,
+ METH_VARARGS, default_int_handler_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -522,264 +522,264 @@ A signal handler function is called with two arguments:\n\
the first is the signal number, the second is the interrupted stack frame.");
static struct PyModuleDef signalmodule = {
- PyModuleDef_HEAD_INIT,
- "signal",
- module_doc,
- -1,
- signal_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "signal",
+ module_doc,
+ -1,
+ signal_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_signal(void)
{
- PyObject *m, *d, *x;
- int i;
+ PyObject *m, *d, *x;
+ int i;
#ifdef WITH_THREAD
- main_thread = PyThread_get_thread_ident();
- main_pid = getpid();
-#endif
-
- /* Create the module and add the functions */
- m = PyModule_Create(&signalmodule);
- if (m == NULL)
- return NULL;
-
- /* Add some symbolic constants to the module */
- d = PyModule_GetDict(m);
-
- x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
- if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
- goto finally;
-
- x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
- if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
- goto finally;
-
- x = PyLong_FromLong((long)NSIG);
- if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
- goto finally;
- Py_DECREF(x);
-
- x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
- if (!x)
- goto finally;
- Py_INCREF(IntHandler);
-
- Handlers[0].tripped = 0;
- for (i = 1; i < NSIG; i++) {
- void (*t)(int);
- t = PyOS_getsig(i);
- Handlers[i].tripped = 0;
- if (t == SIG_DFL)
- Handlers[i].func = DefaultHandler;
- else if (t == SIG_IGN)
- Handlers[i].func = IgnoreHandler;
- else
- Handlers[i].func = Py_None; /* None of our business */
- Py_INCREF(Handlers[i].func);
- }
- if (Handlers[SIGINT].func == DefaultHandler) {
- /* Install default int handler */
- Py_INCREF(IntHandler);
- Py_DECREF(Handlers[SIGINT].func);
- Handlers[SIGINT].func = IntHandler;
- old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
- }
+ main_thread = PyThread_get_thread_ident();
+ main_pid = getpid();
+#endif
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&signalmodule);
+ if (m == NULL)
+ return NULL;
+
+ /* Add some symbolic constants to the module */
+ d = PyModule_GetDict(m);
+
+ x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
+ if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
+ goto finally;
+
+ x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
+ if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
+ goto finally;
+
+ x = PyLong_FromLong((long)NSIG);
+ if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
+ goto finally;
+ Py_DECREF(x);
+
+ x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
+ if (!x)
+ goto finally;
+ Py_INCREF(IntHandler);
+
+ Handlers[0].tripped = 0;
+ for (i = 1; i < NSIG; i++) {
+ void (*t)(int);
+ t = PyOS_getsig(i);
+ Handlers[i].tripped = 0;
+ if (t == SIG_DFL)
+ Handlers[i].func = DefaultHandler;
+ else if (t == SIG_IGN)
+ Handlers[i].func = IgnoreHandler;
+ else
+ Handlers[i].func = Py_None; /* None of our business */
+ Py_INCREF(Handlers[i].func);
+ }
+ if (Handlers[SIGINT].func == DefaultHandler) {
+ /* Install default int handler */
+ Py_INCREF(IntHandler);
+ Py_DECREF(Handlers[SIGINT].func);
+ Handlers[SIGINT].func = IntHandler;
+ old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
+ }
#ifdef SIGHUP
- x = PyLong_FromLong(SIGHUP);
- PyDict_SetItemString(d, "SIGHUP", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGHUP);
+ PyDict_SetItemString(d, "SIGHUP", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGINT
- x = PyLong_FromLong(SIGINT);
- PyDict_SetItemString(d, "SIGINT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGINT);
+ PyDict_SetItemString(d, "SIGINT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGBREAK
- x = PyLong_FromLong(SIGBREAK);
- PyDict_SetItemString(d, "SIGBREAK", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGBREAK);
+ PyDict_SetItemString(d, "SIGBREAK", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGQUIT
- x = PyLong_FromLong(SIGQUIT);
- PyDict_SetItemString(d, "SIGQUIT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGQUIT);
+ PyDict_SetItemString(d, "SIGQUIT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGILL
- x = PyLong_FromLong(SIGILL);
- PyDict_SetItemString(d, "SIGILL", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGILL);
+ PyDict_SetItemString(d, "SIGILL", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGTRAP
- x = PyLong_FromLong(SIGTRAP);
- PyDict_SetItemString(d, "SIGTRAP", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGTRAP);
+ PyDict_SetItemString(d, "SIGTRAP", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGIOT
- x = PyLong_FromLong(SIGIOT);
- PyDict_SetItemString(d, "SIGIOT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGIOT);
+ PyDict_SetItemString(d, "SIGIOT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGABRT
- x = PyLong_FromLong(SIGABRT);
- PyDict_SetItemString(d, "SIGABRT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGABRT);
+ PyDict_SetItemString(d, "SIGABRT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGEMT
- x = PyLong_FromLong(SIGEMT);
- PyDict_SetItemString(d, "SIGEMT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGEMT);
+ PyDict_SetItemString(d, "SIGEMT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGFPE
- x = PyLong_FromLong(SIGFPE);
- PyDict_SetItemString(d, "SIGFPE", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGFPE);
+ PyDict_SetItemString(d, "SIGFPE", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGKILL
- x = PyLong_FromLong(SIGKILL);
- PyDict_SetItemString(d, "SIGKILL", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGKILL);
+ PyDict_SetItemString(d, "SIGKILL", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGBUS
- x = PyLong_FromLong(SIGBUS);
- PyDict_SetItemString(d, "SIGBUS", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGBUS);
+ PyDict_SetItemString(d, "SIGBUS", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGSEGV
- x = PyLong_FromLong(SIGSEGV);
- PyDict_SetItemString(d, "SIGSEGV", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGSEGV);
+ PyDict_SetItemString(d, "SIGSEGV", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGSYS
- x = PyLong_FromLong(SIGSYS);
- PyDict_SetItemString(d, "SIGSYS", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGSYS);
+ PyDict_SetItemString(d, "SIGSYS", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGPIPE
- x = PyLong_FromLong(SIGPIPE);
- PyDict_SetItemString(d, "SIGPIPE", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGPIPE);
+ PyDict_SetItemString(d, "SIGPIPE", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGALRM
- x = PyLong_FromLong(SIGALRM);
- PyDict_SetItemString(d, "SIGALRM", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGALRM);
+ PyDict_SetItemString(d, "SIGALRM", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGTERM
- x = PyLong_FromLong(SIGTERM);
- PyDict_SetItemString(d, "SIGTERM", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGTERM);
+ PyDict_SetItemString(d, "SIGTERM", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGUSR1
- x = PyLong_FromLong(SIGUSR1);
- PyDict_SetItemString(d, "SIGUSR1", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGUSR1);
+ PyDict_SetItemString(d, "SIGUSR1", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGUSR2
- x = PyLong_FromLong(SIGUSR2);
- PyDict_SetItemString(d, "SIGUSR2", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGUSR2);
+ PyDict_SetItemString(d, "SIGUSR2", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGCLD
- x = PyLong_FromLong(SIGCLD);
- PyDict_SetItemString(d, "SIGCLD", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGCLD);
+ PyDict_SetItemString(d, "SIGCLD", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGCHLD
- x = PyLong_FromLong(SIGCHLD);
- PyDict_SetItemString(d, "SIGCHLD", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGCHLD);
+ PyDict_SetItemString(d, "SIGCHLD", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGPWR
- x = PyLong_FromLong(SIGPWR);
- PyDict_SetItemString(d, "SIGPWR", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGPWR);
+ PyDict_SetItemString(d, "SIGPWR", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGIO
- x = PyLong_FromLong(SIGIO);
- PyDict_SetItemString(d, "SIGIO", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGIO);
+ PyDict_SetItemString(d, "SIGIO", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGURG
- x = PyLong_FromLong(SIGURG);
- PyDict_SetItemString(d, "SIGURG", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGURG);
+ PyDict_SetItemString(d, "SIGURG", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGWINCH
- x = PyLong_FromLong(SIGWINCH);
- PyDict_SetItemString(d, "SIGWINCH", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGWINCH);
+ PyDict_SetItemString(d, "SIGWINCH", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGPOLL
- x = PyLong_FromLong(SIGPOLL);
- PyDict_SetItemString(d, "SIGPOLL", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGPOLL);
+ PyDict_SetItemString(d, "SIGPOLL", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGSTOP
- x = PyLong_FromLong(SIGSTOP);
- PyDict_SetItemString(d, "SIGSTOP", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGSTOP);
+ PyDict_SetItemString(d, "SIGSTOP", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGTSTP
- x = PyLong_FromLong(SIGTSTP);
- PyDict_SetItemString(d, "SIGTSTP", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGTSTP);
+ PyDict_SetItemString(d, "SIGTSTP", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGCONT
- x = PyLong_FromLong(SIGCONT);
- PyDict_SetItemString(d, "SIGCONT", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGCONT);
+ PyDict_SetItemString(d, "SIGCONT", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGTTIN
- x = PyLong_FromLong(SIGTTIN);
- PyDict_SetItemString(d, "SIGTTIN", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGTTIN);
+ PyDict_SetItemString(d, "SIGTTIN", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGTTOU
- x = PyLong_FromLong(SIGTTOU);
- PyDict_SetItemString(d, "SIGTTOU", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGTTOU);
+ PyDict_SetItemString(d, "SIGTTOU", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGVTALRM
- x = PyLong_FromLong(SIGVTALRM);
- PyDict_SetItemString(d, "SIGVTALRM", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGVTALRM);
+ PyDict_SetItemString(d, "SIGVTALRM", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGPROF
- x = PyLong_FromLong(SIGPROF);
- PyDict_SetItemString(d, "SIGPROF", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGPROF);
+ PyDict_SetItemString(d, "SIGPROF", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGXCPU
- x = PyLong_FromLong(SIGXCPU);
- PyDict_SetItemString(d, "SIGXCPU", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGXCPU);
+ PyDict_SetItemString(d, "SIGXCPU", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGXFSZ
- x = PyLong_FromLong(SIGXFSZ);
- PyDict_SetItemString(d, "SIGXFSZ", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGXFSZ);
+ PyDict_SetItemString(d, "SIGXFSZ", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGRTMIN
- x = PyLong_FromLong(SIGRTMIN);
- PyDict_SetItemString(d, "SIGRTMIN", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGRTMIN);
+ PyDict_SetItemString(d, "SIGRTMIN", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGRTMAX
- x = PyLong_FromLong(SIGRTMAX);
- PyDict_SetItemString(d, "SIGRTMAX", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGRTMAX);
+ PyDict_SetItemString(d, "SIGRTMAX", x);
+ Py_XDECREF(x);
#endif
#ifdef SIGINFO
- x = PyLong_FromLong(SIGINFO);
- PyDict_SetItemString(d, "SIGINFO", x);
- Py_XDECREF(x);
+ x = PyLong_FromLong(SIGINFO);
+ PyDict_SetItemString(d, "SIGINFO", x);
+ Py_XDECREF(x);
#endif
#ifdef ITIMER_REAL
@@ -799,15 +799,15 @@ PyInit_signal(void)
#endif
#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
- ItimerError = PyErr_NewException("signal.ItimerError",
- PyExc_IOError, NULL);
+ ItimerError = PyErr_NewException("signal.ItimerError",
+ PyExc_IOError, NULL);
if (ItimerError != NULL)
- PyDict_SetItemString(d, "ItimerError", ItimerError);
+ PyDict_SetItemString(d, "ItimerError", ItimerError);
#endif
if (PyErr_Occurred()) {
- Py_DECREF(m);
- m = NULL;
+ Py_DECREF(m);
+ m = NULL;
}
finally:
@@ -817,28 +817,28 @@ PyInit_signal(void)
static void
finisignal(void)
{
- int i;
- PyObject *func;
-
- PyOS_setsig(SIGINT, old_siginthandler);
- old_siginthandler = SIG_DFL;
-
- for (i = 1; i < NSIG; i++) {
- func = Handlers[i].func;
- Handlers[i].tripped = 0;
- Handlers[i].func = NULL;
- if (i != SIGINT && func != NULL && func != Py_None &&
- func != DefaultHandler && func != IgnoreHandler)
- PyOS_setsig(i, SIG_DFL);
- Py_XDECREF(func);
- }
-
- Py_XDECREF(IntHandler);
- IntHandler = NULL;
- Py_XDECREF(DefaultHandler);
- DefaultHandler = NULL;
- Py_XDECREF(IgnoreHandler);
- IgnoreHandler = NULL;
+ int i;
+ PyObject *func;
+
+ PyOS_setsig(SIGINT, old_siginthandler);
+ old_siginthandler = SIG_DFL;
+
+ for (i = 1; i < NSIG; i++) {
+ func = Handlers[i].func;
+ Handlers[i].tripped = 0;
+ Handlers[i].func = NULL;
+ if (i != SIGINT && func != NULL && func != Py_None &&
+ func != DefaultHandler && func != IgnoreHandler)
+ PyOS_setsig(i, SIG_DFL);
+ Py_XDECREF(func);
+ }
+
+ Py_XDECREF(IntHandler);
+ IntHandler = NULL;
+ Py_XDECREF(DefaultHandler);
+ DefaultHandler = NULL;
+ Py_XDECREF(IgnoreHandler);
+ IgnoreHandler = NULL;
}
@@ -846,55 +846,55 @@ finisignal(void)
int
PyErr_CheckSignals(void)
{
- int i;
- PyObject *f;
+ int i;
+ PyObject *f;
- if (!is_tripped)
- return 0;
+ if (!is_tripped)
+ return 0;
#ifdef WITH_THREAD
- if (PyThread_get_thread_ident() != main_thread)
- return 0;
-#endif
-
- /*
- * The is_tripped variable is meant to speed up the calls to
- * PyErr_CheckSignals (both directly or via pending calls) when no
- * signal has arrived. This variable is set to 1 when a signal arrives
- * and it is set to 0 here, when we know some signals arrived. This way
- * we can run the registered handlers with no signals blocked.
- *
- * NOTE: with this approach we can have a situation where is_tripped is
- * 1 but we have no more signals to handle (Handlers[i].tripped
- * is 0 for every signal i). This won't do us any harm (except
- * we're gonna spent some cycles for nothing). This happens when
- * we receive a signal i after we zero is_tripped and before we
- * check Handlers[i].tripped.
- */
- is_tripped = 0;
-
- if (!(f = (PyObject *)PyEval_GetFrame()))
- f = Py_None;
-
- for (i = 1; i < NSIG; i++) {
- if (Handlers[i].tripped) {
- PyObject *result = NULL;
- PyObject *arglist = Py_BuildValue("(iO)", i, f);
- Handlers[i].tripped = 0;
-
- if (arglist) {
- result = PyEval_CallObject(Handlers[i].func,
- arglist);
- Py_DECREF(arglist);
- }
- if (!result)
- return -1;
-
- Py_DECREF(result);
- }
- }
-
- return 0;
+ if (PyThread_get_thread_ident() != main_thread)
+ return 0;
+#endif
+
+ /*
+ * The is_tripped variable is meant to speed up the calls to
+ * PyErr_CheckSignals (both directly or via pending calls) when no
+ * signal has arrived. This variable is set to 1 when a signal arrives
+ * and it is set to 0 here, when we know some signals arrived. This way
+ * we can run the registered handlers with no signals blocked.
+ *
+ * NOTE: with this approach we can have a situation where is_tripped is
+ * 1 but we have no more signals to handle (Handlers[i].tripped
+ * is 0 for every signal i). This won't do us any harm (except
+ * we're gonna spent some cycles for nothing). This happens when
+ * we receive a signal i after we zero is_tripped and before we
+ * check Handlers[i].tripped.
+ */
+ is_tripped = 0;
+
+ if (!(f = (PyObject *)PyEval_GetFrame()))
+ f = Py_None;
+
+ for (i = 1; i < NSIG; i++) {
+ if (Handlers[i].tripped) {
+ PyObject *result = NULL;
+ PyObject *arglist = Py_BuildValue("(iO)", i, f);
+ Handlers[i].tripped = 0;
+
+ if (arglist) {
+ result = PyEval_CallObject(Handlers[i].func,
+ arglist);
+ Py_DECREF(arglist);
+ }
+ if (!result)
+ return -1;
+
+ Py_DECREF(result);
+ }
+ }
+
+ return 0;
}
@@ -904,49 +904,49 @@ PyErr_CheckSignals(void)
void
PyErr_SetInterrupt(void)
{
- is_tripped = 1;
- Handlers[SIGINT].tripped = 1;
- Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
+ is_tripped = 1;
+ Handlers[SIGINT].tripped = 1;
+ Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
}
void
PyOS_InitInterrupts(void)
{
- PyObject *m = PyInit_signal();
- if (m) {
- _PyImport_FixupExtension(m, "signal", "signal");
- Py_DECREF(m);
- }
+ PyObject *m = PyInit_signal();
+ if (m) {
+ _PyImport_FixupExtension(m, "signal", "signal");
+ Py_DECREF(m);
+ }
}
void
PyOS_FiniInterrupts(void)
{
- finisignal();
+ finisignal();
}
int
PyOS_InterruptOccurred(void)
{
- if (Handlers[SIGINT].tripped) {
+ if (Handlers[SIGINT].tripped) {
#ifdef WITH_THREAD
- if (PyThread_get_thread_ident() != main_thread)
- return 0;
+ if (PyThread_get_thread_ident() != main_thread)
+ return 0;
#endif
- Handlers[SIGINT].tripped = 0;
- return 1;
- }
- return 0;
+ Handlers[SIGINT].tripped = 0;
+ return 1;
+ }
+ return 0;
}
void
PyOS_AfterFork(void)
{
#ifdef WITH_THREAD
- PyEval_ReInitThreads();
- main_thread = PyThread_get_thread_ident();
- main_pid = getpid();
- _PyImport_ReInitLock();
- PyThread_ReInitTLS();
+ PyEval_ReInitThreads();
+ main_thread = PyThread_get_thread_ident();
+ main_pid = getpid();
+ _PyImport_ReInitLock();
+ PyThread_ReInitTLS();
#endif
}
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 636571c..d0d7f3c 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -17,9 +17,9 @@ Module interface:
- socket.error: exception raised for socket specific errors
- socket.gaierror: exception raised for getaddrinfo/getnameinfo errors,
- a subclass of socket.error
+ a subclass of socket.error
- socket.herror: exception raised for gethostby* errors,
- a subclass of socket.error
+ a subclass of socket.error
- socket.gethostbyname(hostname) --> host IP address (string: 'dd.dd.dd.dd')
- socket.gethostbyaddr(IP address) --> (hostname, [alias, ...], [IP addr, ...])
- socket.gethostname() --> host name (string: 'spam' or 'spam.domain.com')
@@ -27,14 +27,14 @@ Module interface:
- socket.getservbyname(servicename[, protocolname]) --> port number
- socket.getservbyport(portnumber[, protocolname]) --> service name
- socket.socket([family[, type [, proto, fileno]]]) --> new socket object
- (fileno specifies a pre-existing socket file descriptor)
+ (fileno specifies a pre-existing socket file descriptor)
- socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
- socket.ntohs(16 bit value) --> new int object
- socket.ntohl(32 bit value) --> new int object
- socket.htons(16 bit value) --> new int object
- socket.htonl(32 bit value) --> new int object
- socket.getaddrinfo(host, port [, family, socktype, proto, flags])
- --> List of (family, socktype, proto, canonname, sockaddr)
+ --> List of (family, socktype, proto, canonname, sockaddr)
- socket.getnameinfo(sockaddr, flags) --> (host, port)
- socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h>
- socket.has_ipv6: boolean value indicating if IPv6 is supported
@@ -54,22 +54,22 @@ Module interface:
specify packet-type and ha-type/addr.
- an AF_TIPC socket address is expressed as
(addr_type, v1, v2, v3 [, scope]); where addr_type can be one of:
- TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
+ TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, and TIPC_ADDR_ID;
and scope can be one of:
- TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
+ TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and TIPC_NODE_SCOPE.
The meaning of v1, v2 and v3 depends on the value of addr_type:
- if addr_type is TIPC_ADDR_NAME:
- v1 is the server type
- v2 is the port identifier
- v3 is ignored
- if addr_type is TIPC_ADDR_NAMESEQ:
- v1 is the server type
- v2 is the lower port number
- v3 is the upper port number
- if addr_type is TIPC_ADDR_ID:
- v1 is the node
- v2 is the ref
- v3 is ignored
+ if addr_type is TIPC_ADDR_NAME:
+ v1 is the server type
+ v2 is the port identifier
+ v3 is ignored
+ if addr_type is TIPC_ADDR_NAMESEQ:
+ v1 is the server type
+ v2 is the lower port number
+ v3 is the upper port number
+ if addr_type is TIPC_ADDR_ID:
+ v1 is the node
+ v2 is the ref
+ v3 is ignored
Local naming conventions:
@@ -283,7 +283,7 @@ typedef size_t socklen_t;
#include <stddef.h>
#ifndef offsetof
-# define offsetof(type, member) ((size_t)(&((type *)0)->member))
+# define offsetof(type, member) ((size_t)(&((type *)0)->member))
#endif
#ifndef O_NONBLOCK
@@ -351,16 +351,16 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
static SOCKET
dup_socket(SOCKET handle)
{
- HANDLE newhandle;
-
- if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
- GetCurrentProcess(), &newhandle,
- 0, FALSE, DUPLICATE_SAME_ACCESS))
- {
- WSASetLastError(GetLastError());
- return INVALID_SOCKET;
- }
- return (SOCKET)newhandle;
+ HANDLE newhandle;
+
+ if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
+ GetCurrentProcess(), &newhandle,
+ 0, FALSE, DUPLICATE_SAME_ACCESS))
+ {
+ WSASetLastError(GetLastError());
+ return INVALID_SOCKET;
+ }
+ return (SOCKET)newhandle;
}
#define SOCKETCLOSE closesocket
#else
@@ -418,7 +418,7 @@ dup_socket(SOCKET handle)
#define SEGMENT_SIZE (32 * 1024 -1)
#endif
-#define SAS2SA(x) ((struct sockaddr *)(x))
+#define SAS2SA(x) ((struct sockaddr *)(x))
/*
* Constants for getnameinfo()
@@ -473,8 +473,8 @@ static PyTypeObject sock_type;
static PyObject*
select_error(void)
{
- PyErr_SetString(socket_error, "unable to select on socket");
- return NULL;
+ PyErr_SetString(socket_error, "unable to select on socket");
+ return NULL;
}
/* Convenience function to raise an error according to errno
@@ -484,95 +484,95 @@ static PyObject *
set_error(void)
{
#ifdef MS_WINDOWS
- int err_no = WSAGetLastError();
- /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
- recognizes the error codes used by both GetLastError() and
- WSAGetLastError */
- if (err_no)
- return PyErr_SetExcFromWindowsErr(socket_error, err_no);
+ int err_no = WSAGetLastError();
+ /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
+ recognizes the error codes used by both GetLastError() and
+ WSAGetLastError */
+ if (err_no)
+ return PyErr_SetExcFromWindowsErr(socket_error, err_no);
#endif
#if defined(PYOS_OS2) && !defined(PYCC_GCC)
- if (sock_errno() != NO_ERROR) {
- APIRET rc;
- ULONG msglen;
- char outbuf[100];
- int myerrorcode = sock_errno();
-
- /* Retrieve socket-related error message from MPTN.MSG file */
- rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
- myerrorcode - SOCBASEERR + 26,
- "mptn.msg",
- &msglen);
- if (rc == NO_ERROR) {
- PyObject *v;
-
- /* OS/2 doesn't guarantee a terminator */
- outbuf[msglen] = '\0';
- if (strlen(outbuf) > 0) {
- /* If non-empty msg, trim CRLF */
- char *lastc = &outbuf[ strlen(outbuf)-1 ];
- while (lastc > outbuf &&
- isspace(Py_CHARMASK(*lastc))) {
- /* Trim trailing whitespace (CRLF) */
- *lastc-- = '\0';
- }
- }
- v = Py_BuildValue("(is)", myerrorcode, outbuf);
- if (v != NULL) {
- PyErr_SetObject(socket_error, v);
- Py_DECREF(v);
- }
- return NULL;
- }
- }
-#endif
-
- return PyErr_SetFromErrno(socket_error);
+ if (sock_errno() != NO_ERROR) {
+ APIRET rc;
+ ULONG msglen;
+ char outbuf[100];
+ int myerrorcode = sock_errno();
+
+ /* Retrieve socket-related error message from MPTN.MSG file */
+ rc = DosGetMessage(NULL, 0, outbuf, sizeof(outbuf),
+ myerrorcode - SOCBASEERR + 26,
+ "mptn.msg",
+ &msglen);
+ if (rc == NO_ERROR) {
+ PyObject *v;
+
+ /* OS/2 doesn't guarantee a terminator */
+ outbuf[msglen] = '\0';
+ if (strlen(outbuf) > 0) {
+ /* If non-empty msg, trim CRLF */
+ char *lastc = &outbuf[ strlen(outbuf)-1 ];
+ while (lastc > outbuf &&
+ isspace(Py_CHARMASK(*lastc))) {
+ /* Trim trailing whitespace (CRLF) */
+ *lastc-- = '\0';
+ }
+ }
+ v = Py_BuildValue("(is)", myerrorcode, outbuf);
+ if (v != NULL) {
+ PyErr_SetObject(socket_error, v);
+ Py_DECREF(v);
+ }
+ return NULL;
+ }
+ }
+#endif
+
+ return PyErr_SetFromErrno(socket_error);
}
static PyObject *
set_herror(int h_error)
{
- PyObject *v;
+ PyObject *v;
#ifdef HAVE_HSTRERROR
- v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
+ v = Py_BuildValue("(is)", h_error, (char *)hstrerror(h_error));
#else
- v = Py_BuildValue("(is)", h_error, "host not found");
+ v = Py_BuildValue("(is)", h_error, "host not found");
#endif
- if (v != NULL) {
- PyErr_SetObject(socket_herror, v);
- Py_DECREF(v);
- }
+ if (v != NULL) {
+ PyErr_SetObject(socket_herror, v);
+ Py_DECREF(v);
+ }
- return NULL;
+ return NULL;
}
static PyObject *
set_gaierror(int error)
{
- PyObject *v;
+ PyObject *v;
#ifdef EAI_SYSTEM
- /* EAI_SYSTEM is not available on Windows XP. */
- if (error == EAI_SYSTEM)
- return set_error();
+ /* EAI_SYSTEM is not available on Windows XP. */
+ if (error == EAI_SYSTEM)
+ return set_error();
#endif
#ifdef HAVE_GAI_STRERROR
- v = Py_BuildValue("(is)", error, gai_strerror(error));
+ v = Py_BuildValue("(is)", error, gai_strerror(error));
#else
- v = Py_BuildValue("(is)", error, "getaddrinfo failed");
+ v = Py_BuildValue("(is)", error, "getaddrinfo failed");
#endif
- if (v != NULL) {
- PyErr_SetObject(socket_gaierror, v);
- Py_DECREF(v);
- }
+ if (v != NULL) {
+ PyErr_SetObject(socket_gaierror, v);
+ Py_DECREF(v);
+ }
- return NULL;
+ return NULL;
}
#ifdef __VMS
@@ -580,22 +580,22 @@ set_gaierror(int error)
static int
sendsegmented(int sock_fd, char *buf, int len, int flags)
{
- int n = 0;
- int remaining = len;
-
- while (remaining > 0) {
- unsigned int segment;
-
- segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
- n = send(sock_fd, buf, segment, flags);
- if (n < 0) {
- return n;
- }
- remaining -= segment;
- buf += segment;
- } /* end while */
-
- return len;
+ int n = 0;
+ int remaining = len;
+
+ while (remaining > 0) {
+ unsigned int segment;
+
+ segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining);
+ n = send(sock_fd, buf, segment, flags);
+ if (n < 0) {
+ return n;
+ }
+ remaining -= segment;
+ buf += segment;
+ } /* end while */
+
+ return len;
}
#endif
@@ -605,33 +605,33 @@ static int
internal_setblocking(PySocketSockObject *s, int block)
{
#ifndef MS_WINDOWS
- int delay_flag;
+ int delay_flag;
#endif
- Py_BEGIN_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
#ifndef MS_WINDOWS
#if defined(PYOS_OS2) && !defined(PYCC_GCC)
- block = !block;
- ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
+ block = !block;
+ ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
#elif defined(__VMS)
- block = !block;
- ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
+ block = !block;
+ ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block);
#else /* !PYOS_OS2 && !__VMS */
- delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
- if (block)
- delay_flag &= (~O_NONBLOCK);
- else
- delay_flag |= O_NONBLOCK;
- fcntl(s->sock_fd, F_SETFL, delay_flag);
+ delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
+ if (block)
+ delay_flag &= (~O_NONBLOCK);
+ else
+ delay_flag |= O_NONBLOCK;
+ fcntl(s->sock_fd, F_SETFL, delay_flag);
#endif /* !PYOS_OS2 */
#else /* MS_WINDOWS */
- block = !block;
- ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
+ block = !block;
+ ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block);
#endif /* MS_WINDOWS */
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
- /* Since these don't return anything */
- return 1;
+ /* Since these don't return anything */
+ return 1;
}
/* Do a select()/poll() on the socket, if necessary (sock_timeout > 0).
@@ -642,53 +642,53 @@ internal_setblocking(PySocketSockObject *s, int block)
static int
internal_select(PySocketSockObject *s, int writing)
{
- int n;
+ int n;
- /* Nothing to do unless we're in timeout mode (not non-blocking) */
- if (s->sock_timeout <= 0.0)
- return 0;
+ /* Nothing to do unless we're in timeout mode (not non-blocking) */
+ if (s->sock_timeout <= 0.0)
+ return 0;
- /* Guard against closed socket */
- if (s->sock_fd < 0)
- return 0;
+ /* Guard against closed socket */
+ if (s->sock_fd < 0)
+ return 0;
- /* Prefer poll, if available, since you can poll() any fd
- * which can't be done with select(). */
+ /* Prefer poll, if available, since you can poll() any fd
+ * which can't be done with select(). */
#ifdef HAVE_POLL
- {
- struct pollfd pollfd;
- int timeout;
+ {
+ struct pollfd pollfd;
+ int timeout;
- pollfd.fd = s->sock_fd;
- pollfd.events = writing ? POLLOUT : POLLIN;
+ pollfd.fd = s->sock_fd;
+ pollfd.events = writing ? POLLOUT : POLLIN;
- /* s->sock_timeout is in seconds, timeout in ms */
- timeout = (int)(s->sock_timeout * 1000 + 0.5);
- n = poll(&pollfd, 1, timeout);
- }
+ /* s->sock_timeout is in seconds, timeout in ms */
+ timeout = (int)(s->sock_timeout * 1000 + 0.5);
+ n = poll(&pollfd, 1, timeout);
+ }
#else
- {
- /* Construct the arguments to select */
- fd_set fds;
- struct timeval tv;
- tv.tv_sec = (int)s->sock_timeout;
- tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
- FD_ZERO(&fds);
- FD_SET(s->sock_fd, &fds);
-
- /* See if the socket is ready */
- if (writing)
- n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
- else
- n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
- }
-#endif
-
- if (n < 0)
- return -1;
- if (n == 0)
- return 1;
- return 0;
+ {
+ /* Construct the arguments to select */
+ fd_set fds;
+ struct timeval tv;
+ tv.tv_sec = (int)s->sock_timeout;
+ tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
+ FD_ZERO(&fds);
+ FD_SET(s->sock_fd, &fds);
+
+ /* See if the socket is ready */
+ if (writing)
+ n = select(s->sock_fd+1, NULL, &fds, NULL, &tv);
+ else
+ n = select(s->sock_fd+1, &fds, NULL, NULL, &tv);
+ }
+#endif
+
+ if (n < 0)
+ return -1;
+ if (n == 0)
+ return 1;
+ return 0;
}
/* Initialize a new socket object. */
@@ -697,18 +697,18 @@ static double defaulttimeout = -1.0; /* Default timeout for new sockets */
static void
init_sockobject(PySocketSockObject *s,
- SOCKET_T fd, int family, int type, int proto)
+ SOCKET_T fd, int family, int type, int proto)
{
- s->sock_fd = fd;
- s->sock_family = family;
- s->sock_type = type;
- s->sock_proto = proto;
- s->sock_timeout = defaulttimeout;
+ s->sock_fd = fd;
+ s->sock_family = family;
+ s->sock_type = type;
+ s->sock_proto = proto;
+ s->sock_timeout = defaulttimeout;
- s->errorhandler = &set_error;
+ s->errorhandler = &set_error;
- if (defaulttimeout >= 0.0)
- internal_setblocking(s, 0);
+ if (defaulttimeout >= 0.0)
+ internal_setblocking(s, 0);
}
@@ -721,12 +721,12 @@ init_sockobject(PySocketSockObject *s,
static PySocketSockObject *
new_sockobject(SOCKET_T fd, int family, int type, int proto)
{
- PySocketSockObject *s;
- s = (PySocketSockObject *)
- PyType_GenericNew(&sock_type, NULL, NULL);
- if (s != NULL)
- init_sockobject(s, fd, family, type, proto);
- return s;
+ PySocketSockObject *s;
+ s = (PySocketSockObject *)
+ PyType_GenericNew(&sock_type, NULL, NULL);
+ if (s != NULL)
+ init_sockobject(s, fd, family, type, proto);
+ return s;
}
@@ -746,122 +746,122 @@ PyThread_type_lock netdb_lock;
static int
setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
{
- struct addrinfo hints, *res;
- int error;
- int d1, d2, d3, d4;
- char ch;
-
- memset((void *) addr_ret, '\0', sizeof(*addr_ret));
- if (name[0] == '\0') {
- int siz;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = af;
- hints.ai_socktype = SOCK_DGRAM; /*dummy*/
- hints.ai_flags = AI_PASSIVE;
- Py_BEGIN_ALLOW_THREADS
- ACQUIRE_GETADDRINFO_LOCK
- error = getaddrinfo(NULL, "0", &hints, &res);
- Py_END_ALLOW_THREADS
- /* We assume that those thread-unsafe getaddrinfo() versions
- *are* safe regarding their return value, ie. that a
- subsequent call to getaddrinfo() does not destroy the
- outcome of the first call. */
- RELEASE_GETADDRINFO_LOCK
- if (error) {
- set_gaierror(error);
- return -1;
- }
- switch (res->ai_family) {
- case AF_INET:
- siz = 4;
- break;
+ struct addrinfo hints, *res;
+ int error;
+ int d1, d2, d3, d4;
+ char ch;
+
+ memset((void *) addr_ret, '\0', sizeof(*addr_ret));
+ if (name[0] == '\0') {
+ int siz;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = af;
+ hints.ai_socktype = SOCK_DGRAM; /*dummy*/
+ hints.ai_flags = AI_PASSIVE;
+ Py_BEGIN_ALLOW_THREADS
+ ACQUIRE_GETADDRINFO_LOCK
+ error = getaddrinfo(NULL, "0", &hints, &res);
+ Py_END_ALLOW_THREADS
+ /* We assume that those thread-unsafe getaddrinfo() versions
+ *are* safe regarding their return value, ie. that a
+ subsequent call to getaddrinfo() does not destroy the
+ outcome of the first call. */
+ RELEASE_GETADDRINFO_LOCK
+ if (error) {
+ set_gaierror(error);
+ return -1;
+ }
+ switch (res->ai_family) {
+ case AF_INET:
+ siz = 4;
+ break;
#ifdef ENABLE_IPV6
- case AF_INET6:
- siz = 16;
- break;
-#endif
- default:
- freeaddrinfo(res);
- PyErr_SetString(socket_error,
- "unsupported address family");
- return -1;
- }
- if (res->ai_next) {
- freeaddrinfo(res);
- PyErr_SetString(socket_error,
- "wildcard resolved to multiple address");
- return -1;
- }
- if (res->ai_addrlen < addr_ret_size)
- addr_ret_size = res->ai_addrlen;
- memcpy(addr_ret, res->ai_addr, addr_ret_size);
- freeaddrinfo(res);
- return siz;
- }
- if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
- struct sockaddr_in *sin;
- if (af != AF_INET && af != AF_UNSPEC) {
- PyErr_SetString(socket_error,
- "address family mismatched");
- return -1;
- }
- sin = (struct sockaddr_in *)addr_ret;
- memset((void *) sin, '\0', sizeof(*sin));
- sin->sin_family = AF_INET;
+ case AF_INET6:
+ siz = 16;
+ break;
+#endif
+ default:
+ freeaddrinfo(res);
+ PyErr_SetString(socket_error,
+ "unsupported address family");
+ return -1;
+ }
+ if (res->ai_next) {
+ freeaddrinfo(res);
+ PyErr_SetString(socket_error,
+ "wildcard resolved to multiple address");
+ return -1;
+ }
+ if (res->ai_addrlen < addr_ret_size)
+ addr_ret_size = res->ai_addrlen;
+ memcpy(addr_ret, res->ai_addr, addr_ret_size);
+ freeaddrinfo(res);
+ return siz;
+ }
+ if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
+ struct sockaddr_in *sin;
+ if (af != AF_INET && af != AF_UNSPEC) {
+ PyErr_SetString(socket_error,
+ "address family mismatched");
+ return -1;
+ }
+ sin = (struct sockaddr_in *)addr_ret;
+ memset((void *) sin, '\0', sizeof(*sin));
+ sin->sin_family = AF_INET;
#ifdef HAVE_SOCKADDR_SA_LEN
- sin->sin_len = sizeof(*sin);
-#endif
- sin->sin_addr.s_addr = INADDR_BROADCAST;
- return sizeof(sin->sin_addr);
- }
- if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
- 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
- 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
- struct sockaddr_in *sin;
- sin = (struct sockaddr_in *)addr_ret;
- sin->sin_addr.s_addr = htonl(
- ((long) d1 << 24) | ((long) d2 << 16) |
- ((long) d3 << 8) | ((long) d4 << 0));
- sin->sin_family = AF_INET;
+ sin->sin_len = sizeof(*sin);
+#endif
+ sin->sin_addr.s_addr = INADDR_BROADCAST;
+ return sizeof(sin->sin_addr);
+ }
+ if (sscanf(name, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
+ 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
+ 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
+ struct sockaddr_in *sin;
+ sin = (struct sockaddr_in *)addr_ret;
+ sin->sin_addr.s_addr = htonl(
+ ((long) d1 << 24) | ((long) d2 << 16) |
+ ((long) d3 << 8) | ((long) d4 << 0));
+ sin->sin_family = AF_INET;
#ifdef HAVE_SOCKADDR_SA_LEN
- sin->sin_len = sizeof(*sin);
-#endif
- return 4;
- }
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = af;
- Py_BEGIN_ALLOW_THREADS
- ACQUIRE_GETADDRINFO_LOCK
- error = getaddrinfo(name, NULL, &hints, &res);
+ sin->sin_len = sizeof(*sin);
+#endif
+ return 4;
+ }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = af;
+ Py_BEGIN_ALLOW_THREADS
+ ACQUIRE_GETADDRINFO_LOCK
+ error = getaddrinfo(name, NULL, &hints, &res);
#if defined(__digital__) && defined(__unix__)
- if (error == EAI_NONAME && af == AF_UNSPEC) {
- /* On Tru64 V5.1, numeric-to-addr conversion fails
- if no address family is given. Assume IPv4 for now.*/
- hints.ai_family = AF_INET;
- error = getaddrinfo(name, NULL, &hints, &res);
- }
-#endif
- Py_END_ALLOW_THREADS
- RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
- if (error) {
- set_gaierror(error);
- return -1;
- }
- if (res->ai_addrlen < addr_ret_size)
- addr_ret_size = res->ai_addrlen;
- memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
- freeaddrinfo(res);
- switch (addr_ret->sa_family) {
- case AF_INET:
- return 4;
+ if (error == EAI_NONAME && af == AF_UNSPEC) {
+ /* On Tru64 V5.1, numeric-to-addr conversion fails
+ if no address family is given. Assume IPv4 for now.*/
+ hints.ai_family = AF_INET;
+ error = getaddrinfo(name, NULL, &hints, &res);
+ }
+#endif
+ Py_END_ALLOW_THREADS
+ RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
+ if (error) {
+ set_gaierror(error);
+ return -1;
+ }
+ if (res->ai_addrlen < addr_ret_size)
+ addr_ret_size = res->ai_addrlen;
+ memcpy((char *) addr_ret, res->ai_addr, addr_ret_size);
+ freeaddrinfo(res);
+ switch (addr_ret->sa_family) {
+ case AF_INET:
+ return 4;
#ifdef ENABLE_IPV6
- case AF_INET6:
- return 16;
+ case AF_INET6:
+ return 16;
#endif
- default:
- PyErr_SetString(socket_error, "unknown address family");
- return -1;
- }
+ default:
+ PyErr_SetString(socket_error, "unknown address family");
+ return -1;
+ }
}
@@ -872,16 +872,16 @@ setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
static PyObject *
makeipaddr(struct sockaddr *addr, int addrlen)
{
- char buf[NI_MAXHOST];
- int error;
-
- error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
- NI_NUMERICHOST);
- if (error) {
- set_gaierror(error);
- return NULL;
- }
- return PyUnicode_FromString(buf);
+ char buf[NI_MAXHOST];
+ int error;
+
+ error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
+ NI_NUMERICHOST);
+ if (error) {
+ set_gaierror(error);
+ return NULL;
+ }
+ return PyUnicode_FromString(buf);
}
@@ -893,24 +893,24 @@ makeipaddr(struct sockaddr *addr, int addrlen)
static int
setbdaddr(char *name, bdaddr_t *bdaddr)
{
- unsigned int b0, b1, b2, b3, b4, b5;
- char ch;
- int n;
-
- n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
- &b5, &b4, &b3, &b2, &b1, &b0, &ch);
- if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
- bdaddr->b[0] = b0;
- bdaddr->b[1] = b1;
- bdaddr->b[2] = b2;
- bdaddr->b[3] = b3;
- bdaddr->b[4] = b4;
- bdaddr->b[5] = b5;
- return 6;
- } else {
- PyErr_SetString(socket_error, "bad bluetooth address");
- return -1;
- }
+ unsigned int b0, b1, b2, b3, b4, b5;
+ char ch;
+ int n;
+
+ n = sscanf(name, "%X:%X:%X:%X:%X:%X%c",
+ &b5, &b4, &b3, &b2, &b1, &b0, &ch);
+ if (n == 6 && (b0 | b1 | b2 | b3 | b4 | b5) < 256) {
+ bdaddr->b[0] = b0;
+ bdaddr->b[1] = b1;
+ bdaddr->b[2] = b2;
+ bdaddr->b[3] = b3;
+ bdaddr->b[4] = b4;
+ bdaddr->b[5] = b5;
+ return 6;
+ } else {
+ PyErr_SetString(socket_error, "bad bluetooth address");
+ return -1;
+ }
}
/* Create a string representation of the Bluetooth address. This is always a
@@ -920,12 +920,12 @@ setbdaddr(char *name, bdaddr_t *bdaddr)
static PyObject *
makebdaddr(bdaddr_t *bdaddr)
{
- char buf[(6 * 2) + 5 + 1];
+ char buf[(6 * 2) + 5 + 1];
- sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
- bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
- bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
- return PyUnicode_FromString(buf);
+ sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
+ bdaddr->b[5], bdaddr->b[4], bdaddr->b[3],
+ bdaddr->b[2], bdaddr->b[1], bdaddr->b[0]);
+ return PyUnicode_FromString(buf);
}
#endif
@@ -939,193 +939,193 @@ makebdaddr(bdaddr_t *bdaddr)
static PyObject *
makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
{
- if (addrlen == 0) {
- /* No address -- may be recvfrom() from known socket */
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- switch (addr->sa_family) {
-
- case AF_INET:
- {
- struct sockaddr_in *a;
- PyObject *addrobj = makeipaddr(addr, sizeof(*a));
- PyObject *ret = NULL;
- if (addrobj) {
- a = (struct sockaddr_in *)addr;
- ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
- Py_DECREF(addrobj);
- }
- return ret;
- }
+ if (addrlen == 0) {
+ /* No address -- may be recvfrom() from known socket */
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ switch (addr->sa_family) {
+
+ case AF_INET:
+ {
+ struct sockaddr_in *a;
+ PyObject *addrobj = makeipaddr(addr, sizeof(*a));
+ PyObject *ret = NULL;
+ if (addrobj) {
+ a = (struct sockaddr_in *)addr;
+ ret = Py_BuildValue("Oi", addrobj, ntohs(a->sin_port));
+ Py_DECREF(addrobj);
+ }
+ return ret;
+ }
#if defined(AF_UNIX)
- case AF_UNIX:
- {
- struct sockaddr_un *a = (struct sockaddr_un *) addr;
+ case AF_UNIX:
+ {
+ struct sockaddr_un *a = (struct sockaddr_un *) addr;
#ifdef linux
- if (a->sun_path[0] == 0) { /* Linux abstract namespace */
- addrlen -= offsetof(struct sockaddr_un, sun_path);
- return PyBytes_FromStringAndSize(a->sun_path, addrlen);
- }
- else
+ if (a->sun_path[0] == 0) { /* Linux abstract namespace */
+ addrlen -= offsetof(struct sockaddr_un, sun_path);
+ return PyBytes_FromStringAndSize(a->sun_path, addrlen);
+ }
+ else
#endif /* linux */
- {
- /* regular NULL-terminated string */
- return PyUnicode_FromString(a->sun_path);
- }
- }
+ {
+ /* regular NULL-terminated string */
+ return PyUnicode_FromString(a->sun_path);
+ }
+ }
#endif /* AF_UNIX */
#if defined(AF_NETLINK)
case AF_NETLINK:
{
- struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
- return Py_BuildValue("II", a->nl_pid, a->nl_groups);
+ struct sockaddr_nl *a = (struct sockaddr_nl *) addr;
+ return Py_BuildValue("II", a->nl_pid, a->nl_groups);
}
#endif /* AF_NETLINK */
#ifdef ENABLE_IPV6
- case AF_INET6:
- {
- struct sockaddr_in6 *a;
- PyObject *addrobj = makeipaddr(addr, sizeof(*a));
- PyObject *ret = NULL;
- if (addrobj) {
- a = (struct sockaddr_in6 *)addr;
- ret = Py_BuildValue("Oiii",
- addrobj,
- ntohs(a->sin6_port),
- a->sin6_flowinfo,
- a->sin6_scope_id);
- Py_DECREF(addrobj);
- }
- return ret;
- }
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *a;
+ PyObject *addrobj = makeipaddr(addr, sizeof(*a));
+ PyObject *ret = NULL;
+ if (addrobj) {
+ a = (struct sockaddr_in6 *)addr;
+ ret = Py_BuildValue("Oiii",
+ addrobj,
+ ntohs(a->sin6_port),
+ a->sin6_flowinfo,
+ a->sin6_scope_id);
+ Py_DECREF(addrobj);
+ }
+ return ret;
+ }
#endif
#ifdef USE_BLUETOOTH
- case AF_BLUETOOTH:
- switch (proto) {
-
- case BTPROTO_L2CAP:
- {
- struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
- PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
- PyObject *ret = NULL;
- if (addrobj) {
- ret = Py_BuildValue("Oi",
- addrobj,
- _BT_L2_MEMB(a, psm));
- Py_DECREF(addrobj);
- }
- return ret;
- }
-
- case BTPROTO_RFCOMM:
- {
- struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
- PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
- PyObject *ret = NULL;
- if (addrobj) {
- ret = Py_BuildValue("Oi",
- addrobj,
- _BT_RC_MEMB(a, channel));
- Py_DECREF(addrobj);
- }
- return ret;
- }
-
- case BTPROTO_HCI:
- {
- struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
- PyObject *ret = NULL;
- ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
- return ret;
- }
+ case AF_BLUETOOTH:
+ switch (proto) {
+
+ case BTPROTO_L2CAP:
+ {
+ struct sockaddr_l2 *a = (struct sockaddr_l2 *) addr;
+ PyObject *addrobj = makebdaddr(&_BT_L2_MEMB(a, bdaddr));
+ PyObject *ret = NULL;
+ if (addrobj) {
+ ret = Py_BuildValue("Oi",
+ addrobj,
+ _BT_L2_MEMB(a, psm));
+ Py_DECREF(addrobj);
+ }
+ return ret;
+ }
+
+ case BTPROTO_RFCOMM:
+ {
+ struct sockaddr_rc *a = (struct sockaddr_rc *) addr;
+ PyObject *addrobj = makebdaddr(&_BT_RC_MEMB(a, bdaddr));
+ PyObject *ret = NULL;
+ if (addrobj) {
+ ret = Py_BuildValue("Oi",
+ addrobj,
+ _BT_RC_MEMB(a, channel));
+ Py_DECREF(addrobj);
+ }
+ return ret;
+ }
+
+ case BTPROTO_HCI:
+ {
+ struct sockaddr_hci *a = (struct sockaddr_hci *) addr;
+ PyObject *ret = NULL;
+ ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev));
+ return ret;
+ }
#if !defined(__FreeBSD__)
- case BTPROTO_SCO:
- {
- struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
- return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
- }
+ case BTPROTO_SCO:
+ {
+ struct sockaddr_sco *a = (struct sockaddr_sco *) addr;
+ return makebdaddr(&_BT_SCO_MEMB(a, bdaddr));
+ }
#endif
- default:
- PyErr_SetString(PyExc_ValueError,
- "Unknown Bluetooth protocol");
- return NULL;
- }
+ default:
+ PyErr_SetString(PyExc_ValueError,
+ "Unknown Bluetooth protocol");
+ return NULL;
+ }
#endif
#ifdef HAVE_NETPACKET_PACKET_H
- case AF_PACKET:
- {
- struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
- char *ifname = "";
- struct ifreq ifr;
- /* need to look up interface name give index */
- if (a->sll_ifindex) {
- ifr.ifr_ifindex = a->sll_ifindex;
- if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
- ifname = ifr.ifr_name;
- }
- return Py_BuildValue("shbhy#",
- ifname,
- ntohs(a->sll_protocol),
- a->sll_pkttype,
- a->sll_hatype,
- a->sll_addr,
- a->sll_halen);
- }
+ case AF_PACKET:
+ {
+ struct sockaddr_ll *a = (struct sockaddr_ll *)addr;
+ char *ifname = "";
+ struct ifreq ifr;
+ /* need to look up interface name give index */
+ if (a->sll_ifindex) {
+ ifr.ifr_ifindex = a->sll_ifindex;
+ if (ioctl(sockfd, SIOCGIFNAME, &ifr) == 0)
+ ifname = ifr.ifr_name;
+ }
+ return Py_BuildValue("shbhy#",
+ ifname,
+ ntohs(a->sll_protocol),
+ a->sll_pkttype,
+ a->sll_hatype,
+ a->sll_addr,
+ a->sll_halen);
+ }
#endif
#ifdef HAVE_LINUX_TIPC_H
- case AF_TIPC:
- {
- struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
- if (a->addrtype == TIPC_ADDR_NAMESEQ) {
- return Py_BuildValue("IIIII",
- a->addrtype,
- a->addr.nameseq.type,
- a->addr.nameseq.lower,
- a->addr.nameseq.upper,
- a->scope);
- } else if (a->addrtype == TIPC_ADDR_NAME) {
- return Py_BuildValue("IIIII",
- a->addrtype,
- a->addr.name.name.type,
- a->addr.name.name.instance,
- a->addr.name.name.instance,
- a->scope);
- } else if (a->addrtype == TIPC_ADDR_ID) {
- return Py_BuildValue("IIIII",
- a->addrtype,
- a->addr.id.node,
- a->addr.id.ref,
- 0,
- a->scope);
- } else {
- PyErr_SetString(PyExc_ValueError,
- "Invalid address type");
- return NULL;
- }
- }
-#endif
-
- /* More cases here... */
-
- default:
- /* If we don't know the address family, don't raise an
- exception -- return it as an (int, bytes) tuple. */
- return Py_BuildValue("iy#",
- addr->sa_family,
- addr->sa_data,
- sizeof(addr->sa_data));
-
- }
+ case AF_TIPC:
+ {
+ struct sockaddr_tipc *a = (struct sockaddr_tipc *) addr;
+ if (a->addrtype == TIPC_ADDR_NAMESEQ) {
+ return Py_BuildValue("IIIII",
+ a->addrtype,
+ a->addr.nameseq.type,
+ a->addr.nameseq.lower,
+ a->addr.nameseq.upper,
+ a->scope);
+ } else if (a->addrtype == TIPC_ADDR_NAME) {
+ return Py_BuildValue("IIIII",
+ a->addrtype,
+ a->addr.name.name.type,
+ a->addr.name.name.instance,
+ a->addr.name.name.instance,
+ a->scope);
+ } else if (a->addrtype == TIPC_ADDR_ID) {
+ return Py_BuildValue("IIIII",
+ a->addrtype,
+ a->addr.id.node,
+ a->addr.id.ref,
+ 0,
+ a->scope);
+ } else {
+ PyErr_SetString(PyExc_ValueError,
+ "Invalid address type");
+ return NULL;
+ }
+ }
+#endif
+
+ /* More cases here... */
+
+ default:
+ /* If we don't know the address family, don't raise an
+ exception -- return it as an (int, bytes) tuple. */
+ return Py_BuildValue("iy#",
+ addr->sa_family,
+ addr->sa_data,
+ sizeof(addr->sa_data));
+
+ }
}
@@ -1136,346 +1136,346 @@ makesockaddr(int sockfd, struct sockaddr *addr, int addrlen, int proto)
static int
getsockaddrarg(PySocketSockObject *s, PyObject *args,
- struct sockaddr *addr_ret, int *len_ret)
+ struct sockaddr *addr_ret, int *len_ret)
{
- switch (s->sock_family) {
+ switch (s->sock_family) {
#if defined(AF_UNIX)
- case AF_UNIX:
- {
- struct sockaddr_un* addr;
- char *path;
- int len;
- if (!PyArg_Parse(args, "s#", &path, &len))
- return 0;
-
- addr = (struct sockaddr_un*)addr_ret;
+ case AF_UNIX:
+ {
+ struct sockaddr_un* addr;
+ char *path;
+ int len;
+ if (!PyArg_Parse(args, "s#", &path, &len))
+ return 0;
+
+ addr = (struct sockaddr_un*)addr_ret;
#ifdef linux
- if (len > 0 && path[0] == 0) {
- /* Linux abstract namespace extension */
- if (len > sizeof addr->sun_path) {
- PyErr_SetString(socket_error,
- "AF_UNIX path too long");
- return 0;
- }
- }
- else
+ if (len > 0 && path[0] == 0) {
+ /* Linux abstract namespace extension */
+ if (len > sizeof addr->sun_path) {
+ PyErr_SetString(socket_error,
+ "AF_UNIX path too long");
+ return 0;
+ }
+ }
+ else
#endif /* linux */
- {
- /* regular NULL-terminated string */
- if (len >= sizeof addr->sun_path) {
- PyErr_SetString(socket_error,
- "AF_UNIX path too long");
- return 0;
- }
- addr->sun_path[len] = 0;
- }
- addr->sun_family = s->sock_family;
- memcpy(addr->sun_path, path, len);
+ {
+ /* regular NULL-terminated string */
+ if (len >= sizeof addr->sun_path) {
+ PyErr_SetString(socket_error,
+ "AF_UNIX path too long");
+ return 0;
+ }
+ addr->sun_path[len] = 0;
+ }
+ addr->sun_family = s->sock_family;
+ memcpy(addr->sun_path, path, len);
#if defined(PYOS_OS2)
- *len_ret = sizeof(*addr);
+ *len_ret = sizeof(*addr);
#else
- *len_ret = len + offsetof(struct sockaddr_un, sun_path);
+ *len_ret = len + offsetof(struct sockaddr_un, sun_path);
#endif
- return 1;
- }
+ return 1;
+ }
#endif /* AF_UNIX */
#if defined(AF_NETLINK)
- case AF_NETLINK:
- {
- struct sockaddr_nl* addr;
- int pid, groups;
- addr = (struct sockaddr_nl *)addr_ret;
- if (!PyTuple_Check(args)) {
- PyErr_Format(
- PyExc_TypeError,
- "getsockaddrarg: "
- "AF_NETLINK address must be tuple, not %.500s",
- Py_TYPE(args)->tp_name);
- return 0;
- }
- if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
- return 0;
- addr->nl_family = AF_NETLINK;
- addr->nl_pid = pid;
- addr->nl_groups = groups;
- *len_ret = sizeof(*addr);
- return 1;
- }
-#endif
-
- case AF_INET:
- {
- struct sockaddr_in* addr;
- char *host;
- int port, result;
- if (!PyTuple_Check(args)) {
- PyErr_Format(
- PyExc_TypeError,
- "getsockaddrarg: "
- "AF_INET address must be tuple, not %.500s",
- Py_TYPE(args)->tp_name);
- return 0;
- }
- if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
- "idna", &host, &port))
- return 0;
- addr=(struct sockaddr_in*)addr_ret;
- result = setipaddr(host, (struct sockaddr *)addr,
- sizeof(*addr), AF_INET);
- PyMem_Free(host);
- if (result < 0)
- return 0;
- if (port < 0 || port > 0xffff) {
- PyErr_SetString(
- PyExc_OverflowError,
- "getsockaddrarg: port must be 0-65535.");
- return 0;
- }
- addr->sin_family = AF_INET;
- addr->sin_port = htons((short)port);
- *len_ret = sizeof *addr;
- return 1;
- }
+ case AF_NETLINK:
+ {
+ struct sockaddr_nl* addr;
+ int pid, groups;
+ addr = (struct sockaddr_nl *)addr_ret;
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "getsockaddrarg: "
+ "AF_NETLINK address must be tuple, not %.500s",
+ Py_TYPE(args)->tp_name);
+ return 0;
+ }
+ if (!PyArg_ParseTuple(args, "II:getsockaddrarg", &pid, &groups))
+ return 0;
+ addr->nl_family = AF_NETLINK;
+ addr->nl_pid = pid;
+ addr->nl_groups = groups;
+ *len_ret = sizeof(*addr);
+ return 1;
+ }
+#endif
+
+ case AF_INET:
+ {
+ struct sockaddr_in* addr;
+ char *host;
+ int port, result;
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "getsockaddrarg: "
+ "AF_INET address must be tuple, not %.500s",
+ Py_TYPE(args)->tp_name);
+ return 0;
+ }
+ if (!PyArg_ParseTuple(args, "eti:getsockaddrarg",
+ "idna", &host, &port))
+ return 0;
+ addr=(struct sockaddr_in*)addr_ret;
+ result = setipaddr(host, (struct sockaddr *)addr,
+ sizeof(*addr), AF_INET);
+ PyMem_Free(host);
+ if (result < 0)
+ return 0;
+ if (port < 0 || port > 0xffff) {
+ PyErr_SetString(
+ PyExc_OverflowError,
+ "getsockaddrarg: port must be 0-65535.");
+ return 0;
+ }
+ addr->sin_family = AF_INET;
+ addr->sin_port = htons((short)port);
+ *len_ret = sizeof *addr;
+ return 1;
+ }
#ifdef ENABLE_IPV6
- case AF_INET6:
- {
- struct sockaddr_in6* addr;
- char *host;
- int port, flowinfo, scope_id, result;
- flowinfo = scope_id = 0;
- if (!PyTuple_Check(args)) {
- PyErr_Format(
- PyExc_TypeError,
- "getsockaddrarg: "
- "AF_INET6 address must be tuple, not %.500s",
- Py_TYPE(args)->tp_name);
- return 0;
- }
- if (!PyArg_ParseTuple(args, "eti|ii",
- "idna", &host, &port, &flowinfo,
- &scope_id)) {
- return 0;
- }
- addr = (struct sockaddr_in6*)addr_ret;
- result = setipaddr(host, (struct sockaddr *)addr,
- sizeof(*addr), AF_INET6);
- PyMem_Free(host);
- if (result < 0)
- return 0;
- if (port < 0 || port > 0xffff) {
- PyErr_SetString(
- PyExc_OverflowError,
- "getsockaddrarg: port must be 0-65535.");
- return 0;
- }
- addr->sin6_family = s->sock_family;
- addr->sin6_port = htons((short)port);
- addr->sin6_flowinfo = flowinfo;
- addr->sin6_scope_id = scope_id;
- *len_ret = sizeof *addr;
- return 1;
- }
+ case AF_INET6:
+ {
+ struct sockaddr_in6* addr;
+ char *host;
+ int port, flowinfo, scope_id, result;
+ flowinfo = scope_id = 0;
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "getsockaddrarg: "
+ "AF_INET6 address must be tuple, not %.500s",
+ Py_TYPE(args)->tp_name);
+ return 0;
+ }
+ if (!PyArg_ParseTuple(args, "eti|ii",
+ "idna", &host, &port, &flowinfo,
+ &scope_id)) {
+ return 0;
+ }
+ addr = (struct sockaddr_in6*)addr_ret;
+ result = setipaddr(host, (struct sockaddr *)addr,
+ sizeof(*addr), AF_INET6);
+ PyMem_Free(host);
+ if (result < 0)
+ return 0;
+ if (port < 0 || port > 0xffff) {
+ PyErr_SetString(
+ PyExc_OverflowError,
+ "getsockaddrarg: port must be 0-65535.");
+ return 0;
+ }
+ addr->sin6_family = s->sock_family;
+ addr->sin6_port = htons((short)port);
+ addr->sin6_flowinfo = flowinfo;
+ addr->sin6_scope_id = scope_id;
+ *len_ret = sizeof *addr;
+ return 1;
+ }
#endif
#ifdef USE_BLUETOOTH
- case AF_BLUETOOTH:
- {
- switch (s->sock_proto) {
- case BTPROTO_L2CAP:
- {
- struct sockaddr_l2 *addr;
- char *straddr;
-
- addr = (struct sockaddr_l2 *)addr_ret;
- memset(addr, 0, sizeof(struct sockaddr_l2));
- _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
- if (!PyArg_ParseTuple(args, "si", &straddr,
- &_BT_L2_MEMB(addr, psm))) {
- PyErr_SetString(socket_error, "getsockaddrarg: "
- "wrong format");
- return 0;
- }
- if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
- return 0;
-
- *len_ret = sizeof *addr;
- return 1;
- }
- case BTPROTO_RFCOMM:
- {
- struct sockaddr_rc *addr;
- char *straddr;
-
- addr = (struct sockaddr_rc *)addr_ret;
- _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
- if (!PyArg_ParseTuple(args, "si", &straddr,
- &_BT_RC_MEMB(addr, channel))) {
- PyErr_SetString(socket_error, "getsockaddrarg: "
- "wrong format");
- return 0;
- }
- if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
- return 0;
-
- *len_ret = sizeof *addr;
- return 1;
- }
- case BTPROTO_HCI:
- {
- struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
- _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
- if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
- PyErr_SetString(socket_error, "getsockaddrarg: "
- "wrong format");
- return 0;
- }
- *len_ret = sizeof *addr;
- return 1;
- }
+ case AF_BLUETOOTH:
+ {
+ switch (s->sock_proto) {
+ case BTPROTO_L2CAP:
+ {
+ struct sockaddr_l2 *addr;
+ char *straddr;
+
+ addr = (struct sockaddr_l2 *)addr_ret;
+ memset(addr, 0, sizeof(struct sockaddr_l2));
+ _BT_L2_MEMB(addr, family) = AF_BLUETOOTH;
+ if (!PyArg_ParseTuple(args, "si", &straddr,
+ &_BT_L2_MEMB(addr, psm))) {
+ PyErr_SetString(socket_error, "getsockaddrarg: "
+ "wrong format");
+ return 0;
+ }
+ if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0)
+ return 0;
+
+ *len_ret = sizeof *addr;
+ return 1;
+ }
+ case BTPROTO_RFCOMM:
+ {
+ struct sockaddr_rc *addr;
+ char *straddr;
+
+ addr = (struct sockaddr_rc *)addr_ret;
+ _BT_RC_MEMB(addr, family) = AF_BLUETOOTH;
+ if (!PyArg_ParseTuple(args, "si", &straddr,
+ &_BT_RC_MEMB(addr, channel))) {
+ PyErr_SetString(socket_error, "getsockaddrarg: "
+ "wrong format");
+ return 0;
+ }
+ if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0)
+ return 0;
+
+ *len_ret = sizeof *addr;
+ return 1;
+ }
+ case BTPROTO_HCI:
+ {
+ struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret;
+ _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH;
+ if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) {
+ PyErr_SetString(socket_error, "getsockaddrarg: "
+ "wrong format");
+ return 0;
+ }
+ *len_ret = sizeof *addr;
+ return 1;
+ }
#if !defined(__FreeBSD__)
- case BTPROTO_SCO:
- {
- struct sockaddr_sco *addr;
- char *straddr;
-
- addr = (struct sockaddr_sco *)addr_ret;
- _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
- if (!PyBytes_Check(args)) {
- PyErr_SetString(socket_error, "getsockaddrarg: "
- "wrong format");
- return 0;
- }
- straddr = PyBytes_AS_STRING(args);
- if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
- return 0;
-
- *len_ret = sizeof *addr;
- return 1;
- }
-#endif
- default:
- PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
- return 0;
- }
- }
+ case BTPROTO_SCO:
+ {
+ struct sockaddr_sco *addr;
+ char *straddr;
+
+ addr = (struct sockaddr_sco *)addr_ret;
+ _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
+ if (!PyBytes_Check(args)) {
+ PyErr_SetString(socket_error, "getsockaddrarg: "
+ "wrong format");
+ return 0;
+ }
+ straddr = PyBytes_AS_STRING(args);
+ if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
+ return 0;
+
+ *len_ret = sizeof *addr;
+ return 1;
+ }
+#endif
+ default:
+ PyErr_SetString(socket_error, "getsockaddrarg: unknown Bluetooth protocol");
+ return 0;
+ }
+ }
#endif
#ifdef HAVE_NETPACKET_PACKET_H
- case AF_PACKET:
- {
- struct sockaddr_ll* addr;
- struct ifreq ifr;
- char *interfaceName;
- int protoNumber;
- int hatype = 0;
- int pkttype = 0;
- char *haddr = NULL;
- unsigned int halen = 0;
-
- if (!PyTuple_Check(args)) {
- PyErr_Format(
- PyExc_TypeError,
- "getsockaddrarg: "
- "AF_PACKET address must be tuple, not %.500s",
- Py_TYPE(args)->tp_name);
- return 0;
- }
- if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
- &protoNumber, &pkttype, &hatype,
- &haddr, &halen))
- return 0;
- strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
- ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
- if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
- s->errorhandler();
- return 0;
- }
- if (halen > 8) {
- PyErr_SetString(PyExc_ValueError,
- "Hardware address must be 8 bytes or less");
- return 0;
- }
- if (protoNumber < 0 || protoNumber > 0xffff) {
- PyErr_SetString(
- PyExc_OverflowError,
- "getsockaddrarg: protoNumber must be 0-65535.");
- return 0;
- }
- addr = (struct sockaddr_ll*)addr_ret;
- addr->sll_family = AF_PACKET;
- addr->sll_protocol = htons((short)protoNumber);
- addr->sll_ifindex = ifr.ifr_ifindex;
- addr->sll_pkttype = pkttype;
- addr->sll_hatype = hatype;
- if (halen != 0) {
- memcpy(&addr->sll_addr, haddr, halen);
- }
- addr->sll_halen = halen;
- *len_ret = sizeof *addr;
- return 1;
- }
+ case AF_PACKET:
+ {
+ struct sockaddr_ll* addr;
+ struct ifreq ifr;
+ char *interfaceName;
+ int protoNumber;
+ int hatype = 0;
+ int pkttype = 0;
+ char *haddr = NULL;
+ unsigned int halen = 0;
+
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "getsockaddrarg: "
+ "AF_PACKET address must be tuple, not %.500s",
+ Py_TYPE(args)->tp_name);
+ return 0;
+ }
+ if (!PyArg_ParseTuple(args, "si|iiy#", &interfaceName,
+ &protoNumber, &pkttype, &hatype,
+ &haddr, &halen))
+ return 0;
+ strncpy(ifr.ifr_name, interfaceName, sizeof(ifr.ifr_name));
+ ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
+ if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
+ s->errorhandler();
+ return 0;
+ }
+ if (halen > 8) {
+ PyErr_SetString(PyExc_ValueError,
+ "Hardware address must be 8 bytes or less");
+ return 0;
+ }
+ if (protoNumber < 0 || protoNumber > 0xffff) {
+ PyErr_SetString(
+ PyExc_OverflowError,
+ "getsockaddrarg: protoNumber must be 0-65535.");
+ return 0;
+ }
+ addr = (struct sockaddr_ll*)addr_ret;
+ addr->sll_family = AF_PACKET;
+ addr->sll_protocol = htons((short)protoNumber);
+ addr->sll_ifindex = ifr.ifr_ifindex;
+ addr->sll_pkttype = pkttype;
+ addr->sll_hatype = hatype;
+ if (halen != 0) {
+ memcpy(&addr->sll_addr, haddr, halen);
+ }
+ addr->sll_halen = halen;
+ *len_ret = sizeof *addr;
+ return 1;
+ }
#endif
#ifdef HAVE_LINUX_TIPC_H
- case AF_TIPC:
- {
- unsigned int atype, v1, v2, v3;
- unsigned int scope = TIPC_CLUSTER_SCOPE;
- struct sockaddr_tipc *addr;
-
- if (!PyTuple_Check(args)) {
- PyErr_Format(
- PyExc_TypeError,
- "getsockaddrarg: "
- "AF_TIPC address must be tuple, not %.500s",
- Py_TYPE(args)->tp_name);
- return 0;
- }
-
- if (!PyArg_ParseTuple(args,
- "IIII|I;Invalid TIPC address format",
- &atype, &v1, &v2, &v3, &scope))
- return 0;
-
- addr = (struct sockaddr_tipc *) addr_ret;
- memset(addr, 0, sizeof(struct sockaddr_tipc));
-
- addr->family = AF_TIPC;
- addr->scope = scope;
- addr->addrtype = atype;
-
- if (atype == TIPC_ADDR_NAMESEQ) {
- addr->addr.nameseq.type = v1;
- addr->addr.nameseq.lower = v2;
- addr->addr.nameseq.upper = v3;
- } else if (atype == TIPC_ADDR_NAME) {
- addr->addr.name.name.type = v1;
- addr->addr.name.name.instance = v2;
- } else if (atype == TIPC_ADDR_ID) {
- addr->addr.id.node = v1;
- addr->addr.id.ref = v2;
- } else {
- /* Shouldn't happen */
- PyErr_SetString(PyExc_TypeError, "Invalid address type");
- return 0;
- }
-
- *len_ret = sizeof(*addr);
-
- return 1;
- }
-#endif
-
- /* More cases here... */
-
- default:
- PyErr_SetString(socket_error, "getsockaddrarg: bad family");
- return 0;
-
- }
+ case AF_TIPC:
+ {
+ unsigned int atype, v1, v2, v3;
+ unsigned int scope = TIPC_CLUSTER_SCOPE;
+ struct sockaddr_tipc *addr;
+
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "getsockaddrarg: "
+ "AF_TIPC address must be tuple, not %.500s",
+ Py_TYPE(args)->tp_name);
+ return 0;
+ }
+
+ if (!PyArg_ParseTuple(args,
+ "IIII|I;Invalid TIPC address format",
+ &atype, &v1, &v2, &v3, &scope))
+ return 0;
+
+ addr = (struct sockaddr_tipc *) addr_ret;
+ memset(addr, 0, sizeof(struct sockaddr_tipc));
+
+ addr->family = AF_TIPC;
+ addr->scope = scope;
+ addr->addrtype = atype;
+
+ if (atype == TIPC_ADDR_NAMESEQ) {
+ addr->addr.nameseq.type = v1;
+ addr->addr.nameseq.lower = v2;
+ addr->addr.nameseq.upper = v3;
+ } else if (atype == TIPC_ADDR_NAME) {
+ addr->addr.name.name.type = v1;
+ addr->addr.name.name.instance = v2;
+ } else if (atype == TIPC_ADDR_ID) {
+ addr->addr.id.node = v1;
+ addr->addr.id.ref = v2;
+ } else {
+ /* Shouldn't happen */
+ PyErr_SetString(PyExc_TypeError, "Invalid address type");
+ return 0;
+ }
+
+ *len_ret = sizeof(*addr);
+
+ return 1;
+ }
+#endif
+
+ /* More cases here... */
+
+ default:
+ PyErr_SetString(socket_error, "getsockaddrarg: bad family");
+ return 0;
+
+ }
}
@@ -1486,89 +1486,89 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
static int
getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
{
- switch (s->sock_family) {
+ switch (s->sock_family) {
#if defined(AF_UNIX)
- case AF_UNIX:
- {
- *len_ret = sizeof (struct sockaddr_un);
- return 1;
- }
+ case AF_UNIX:
+ {
+ *len_ret = sizeof (struct sockaddr_un);
+ return 1;
+ }
#endif /* AF_UNIX */
#if defined(AF_NETLINK)
case AF_NETLINK:
{
- *len_ret = sizeof (struct sockaddr_nl);
- return 1;
+ *len_ret = sizeof (struct sockaddr_nl);
+ return 1;
}
#endif
- case AF_INET:
- {
- *len_ret = sizeof (struct sockaddr_in);
- return 1;
- }
+ case AF_INET:
+ {
+ *len_ret = sizeof (struct sockaddr_in);
+ return 1;
+ }
#ifdef ENABLE_IPV6
- case AF_INET6:
- {
- *len_ret = sizeof (struct sockaddr_in6);
- return 1;
- }
+ case AF_INET6:
+ {
+ *len_ret = sizeof (struct sockaddr_in6);
+ return 1;
+ }
#endif
#ifdef USE_BLUETOOTH
- case AF_BLUETOOTH:
- {
- switch(s->sock_proto)
- {
-
- case BTPROTO_L2CAP:
- *len_ret = sizeof (struct sockaddr_l2);
- return 1;
- case BTPROTO_RFCOMM:
- *len_ret = sizeof (struct sockaddr_rc);
- return 1;
- case BTPROTO_HCI:
- *len_ret = sizeof (struct sockaddr_hci);
- return 1;
+ case AF_BLUETOOTH:
+ {
+ switch(s->sock_proto)
+ {
+
+ case BTPROTO_L2CAP:
+ *len_ret = sizeof (struct sockaddr_l2);
+ return 1;
+ case BTPROTO_RFCOMM:
+ *len_ret = sizeof (struct sockaddr_rc);
+ return 1;
+ case BTPROTO_HCI:
+ *len_ret = sizeof (struct sockaddr_hci);
+ return 1;
#if !defined(__FreeBSD__)
- case BTPROTO_SCO:
- *len_ret = sizeof (struct sockaddr_sco);
- return 1;
+ case BTPROTO_SCO:
+ *len_ret = sizeof (struct sockaddr_sco);
+ return 1;
#endif
- default:
- PyErr_SetString(socket_error, "getsockaddrlen: "
- "unknown BT protocol");
- return 0;
+ default:
+ PyErr_SetString(socket_error, "getsockaddrlen: "
+ "unknown BT protocol");
+ return 0;
- }
- }
+ }
+ }
#endif
#ifdef HAVE_NETPACKET_PACKET_H
- case AF_PACKET:
- {
- *len_ret = sizeof (struct sockaddr_ll);
- return 1;
- }
+ case AF_PACKET:
+ {
+ *len_ret = sizeof (struct sockaddr_ll);
+ return 1;
+ }
#endif
#ifdef HAVE_LINUX_TIPC_H
- case AF_TIPC:
- {
- *len_ret = sizeof (struct sockaddr_tipc);
- return 1;
- }
+ case AF_TIPC:
+ {
+ *len_ret = sizeof (struct sockaddr_tipc);
+ return 1;
+ }
#endif
- /* More cases here... */
+ /* More cases here... */
- default:
- PyErr_SetString(socket_error, "getsockaddrlen: bad family");
- return 0;
+ default:
+ PyErr_SetString(socket_error, "getsockaddrlen: bad family");
+ return 0;
- }
+ }
}
@@ -1577,52 +1577,52 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
static PyObject *
sock_accept(PySocketSockObject *s)
{
- sock_addr_t addrbuf;
- SOCKET_T newfd = INVALID_SOCKET;
- socklen_t addrlen;
- PyObject *sock = NULL;
- PyObject *addr = NULL;
- PyObject *res = NULL;
- int timeout;
-
- if (!getsockaddrlen(s, &addrlen))
- return NULL;
- memset(&addrbuf, 0, addrlen);
-
- if (!IS_SELECTABLE(s))
- return select_error();
-
- Py_BEGIN_ALLOW_THREADS
- timeout = internal_select(s, 0);
- if (!timeout)
- newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
- Py_END_ALLOW_THREADS
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return NULL;
- }
-
- if (newfd == INVALID_SOCKET)
- return s->errorhandler();
-
- sock = PyLong_FromSocket_t(newfd);
- if (sock == NULL) {
- SOCKETCLOSE(newfd);
- goto finally;
- }
-
- addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
- addrlen, s->sock_proto);
- if (addr == NULL)
- goto finally;
-
- res = PyTuple_Pack(2, sock, addr);
+ sock_addr_t addrbuf;
+ SOCKET_T newfd = INVALID_SOCKET;
+ socklen_t addrlen;
+ PyObject *sock = NULL;
+ PyObject *addr = NULL;
+ PyObject *res = NULL;
+ int timeout;
+
+ if (!getsockaddrlen(s, &addrlen))
+ return NULL;
+ memset(&addrbuf, 0, addrlen);
+
+ if (!IS_SELECTABLE(s))
+ return select_error();
+
+ Py_BEGIN_ALLOW_THREADS
+ timeout = internal_select(s, 0);
+ if (!timeout)
+ newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
+ Py_END_ALLOW_THREADS
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return NULL;
+ }
+
+ if (newfd == INVALID_SOCKET)
+ return s->errorhandler();
+
+ sock = PyLong_FromSocket_t(newfd);
+ if (sock == NULL) {
+ SOCKETCLOSE(newfd);
+ goto finally;
+ }
+
+ addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
+ addrlen, s->sock_proto);
+ if (addr == NULL)
+ goto finally;
+
+ res = PyTuple_Pack(2, sock, addr);
finally:
- Py_XDECREF(sock);
- Py_XDECREF(addr);
- return res;
+ Py_XDECREF(sock);
+ Py_XDECREF(addr);
+ return res;
}
PyDoc_STRVAR(accept_doc,
@@ -1640,17 +1640,17 @@ For IP sockets, the address info is a pair (hostaddr, port).");
static PyObject *
sock_setblocking(PySocketSockObject *s, PyObject *arg)
{
- int block;
+ int block;
- block = PyLong_AsLong(arg);
- if (block == -1 && PyErr_Occurred())
- return NULL;
+ block = PyLong_AsLong(arg);
+ if (block == -1 && PyErr_Occurred())
+ return NULL;
- s->sock_timeout = block ? -1.0 : 0.0;
- internal_setblocking(s, block);
+ s->sock_timeout = block ? -1.0 : 0.0;
+ internal_setblocking(s, block);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setblocking_doc,
@@ -1669,25 +1669,25 @@ setblocking(False) is equivalent to settimeout(0.0).");
static PyObject *
sock_settimeout(PySocketSockObject *s, PyObject *arg)
{
- double timeout;
-
- if (arg == Py_None)
- timeout = -1.0;
- else {
- timeout = PyFloat_AsDouble(arg);
- if (timeout < 0.0) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_ValueError,
- "Timeout value out of range");
- return NULL;
- }
- }
-
- s->sock_timeout = timeout;
- internal_setblocking(s, timeout < 0.0);
-
- Py_INCREF(Py_None);
- return Py_None;
+ double timeout;
+
+ if (arg == Py_None)
+ timeout = -1.0;
+ else {
+ timeout = PyFloat_AsDouble(arg);
+ if (timeout < 0.0) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_ValueError,
+ "Timeout value out of range");
+ return NULL;
+ }
+ }
+
+ s->sock_timeout = timeout;
+ internal_setblocking(s, timeout < 0.0);
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(settimeout_doc,
@@ -1703,12 +1703,12 @@ Setting a timeout of zero is the same as setblocking(0).");
static PyObject *
sock_gettimeout(PySocketSockObject *s)
{
- if (s->sock_timeout < 0.0) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- else
- return PyFloat_FromDouble(s->sock_timeout);
+ if (s->sock_timeout < 0.0) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else
+ return PyFloat_FromDouble(s->sock_timeout);
}
PyDoc_STRVAR(gettimeout_doc,
@@ -1726,29 +1726,29 @@ operations are disabled.");
static PyObject *
sock_setsockopt(PySocketSockObject *s, PyObject *args)
{
- int level;
- int optname;
- int res;
- char *buf;
- int buflen;
- int flag;
-
- if (PyArg_ParseTuple(args, "iii:setsockopt",
- &level, &optname, &flag)) {
- buf = (char *) &flag;
- buflen = sizeof flag;
- }
- else {
- PyErr_Clear();
- if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
- &level, &optname, &buf, &buflen))
- return NULL;
- }
- res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
- if (res < 0)
- return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ int level;
+ int optname;
+ int res;
+ char *buf;
+ int buflen;
+ int flag;
+
+ if (PyArg_ParseTuple(args, "iii:setsockopt",
+ &level, &optname, &flag)) {
+ buf = (char *) &flag;
+ buflen = sizeof flag;
+ }
+ else {
+ PyErr_Clear();
+ if (!PyArg_ParseTuple(args, "iiy#:setsockopt",
+ &level, &optname, &buf, &buflen))
+ return NULL;
+ }
+ res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);
+ if (res < 0)
+ return s->errorhandler();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setsockopt_doc,
@@ -1766,47 +1766,47 @@ The value argument can either be an integer or a string.");
static PyObject *
sock_getsockopt(PySocketSockObject *s, PyObject *args)
{
- int level;
- int optname;
- int res;
- PyObject *buf;
- socklen_t buflen = 0;
-
- if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
- &level, &optname, &buflen))
- return NULL;
-
- if (buflen == 0) {
- int flag = 0;
- socklen_t flagsize = sizeof flag;
- res = getsockopt(s->sock_fd, level, optname,
- (void *)&flag, &flagsize);
- if (res < 0)
- return s->errorhandler();
- return PyLong_FromLong(flag);
- }
+ int level;
+ int optname;
+ int res;
+ PyObject *buf;
+ socklen_t buflen = 0;
+
+ if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
+ &level, &optname, &buflen))
+ return NULL;
+
+ if (buflen == 0) {
+ int flag = 0;
+ socklen_t flagsize = sizeof flag;
+ res = getsockopt(s->sock_fd, level, optname,
+ (void *)&flag, &flagsize);
+ if (res < 0)
+ return s->errorhandler();
+ return PyLong_FromLong(flag);
+ }
#ifdef __VMS
- /* socklen_t is unsigned so no negative test is needed,
- test buflen == 0 is previously done */
- if (buflen > 1024) {
+ /* socklen_t is unsigned so no negative test is needed,
+ test buflen == 0 is previously done */
+ if (buflen > 1024) {
#else
- if (buflen <= 0 || buflen > 1024) {
-#endif
- PyErr_SetString(socket_error,
- "getsockopt buflen out of range");
- return NULL;
- }
- buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
- if (buf == NULL)
- return NULL;
- res = getsockopt(s->sock_fd, level, optname,
- (void *)PyBytes_AS_STRING(buf), &buflen);
- if (res < 0) {
- Py_DECREF(buf);
- return s->errorhandler();
- }
- _PyBytes_Resize(&buf, buflen);
- return buf;
+ if (buflen <= 0 || buflen > 1024) {
+#endif
+ PyErr_SetString(socket_error,
+ "getsockopt buflen out of range");
+ return NULL;
+ }
+ buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
+ if (buf == NULL)
+ return NULL;
+ res = getsockopt(s->sock_fd, level, optname,
+ (void *)PyBytes_AS_STRING(buf), &buflen);
+ if (res < 0) {
+ Py_DECREF(buf);
+ return s->errorhandler();
+ }
+ _PyBytes_Resize(&buf, buflen);
+ return buf;
}
PyDoc_STRVAR(getsockopt_doc,
@@ -1822,19 +1822,19 @@ string of that length; otherwise it is an integer.");
static PyObject *
sock_bind(PySocketSockObject *s, PyObject *addro)
{
- sock_addr_t addrbuf;
- int addrlen;
- int res;
-
- if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ sock_addr_t addrbuf;
+ int addrlen;
+ int res;
+
+ if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return s->errorhandler();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(bind_doc,
@@ -1852,16 +1852,16 @@ sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");
static PyObject *
sock_close(PySocketSockObject *s)
{
- SOCKET_T fd;
-
- if ((fd = s->sock_fd) != -1) {
- s->sock_fd = -1;
- Py_BEGIN_ALLOW_THREADS
- (void) SOCKETCLOSE(fd);
- Py_END_ALLOW_THREADS
- }
- Py_INCREF(Py_None);
- return Py_None;
+ SOCKET_T fd;
+
+ if ((fd = s->sock_fd) != -1) {
+ s->sock_fd = -1;
+ Py_BEGIN_ALLOW_THREADS
+ (void) SOCKETCLOSE(fd);
+ Py_END_ALLOW_THREADS
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(close_doc,
@@ -1871,90 +1871,90 @@ Close the socket. It cannot be used after this call.");
static int
internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
- int *timeoutp)
+ int *timeoutp)
{
- int res, timeout;
+ int res, timeout;
- timeout = 0;
- res = connect(s->sock_fd, addr, addrlen);
+ timeout = 0;
+ res = connect(s->sock_fd, addr, addrlen);
#ifdef MS_WINDOWS
- if (s->sock_timeout > 0.0) {
- if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
- IS_SELECTABLE(s)) {
- /* This is a mess. Best solution: trust select */
- fd_set fds;
- fd_set fds_exc;
- struct timeval tv;
- tv.tv_sec = (int)s->sock_timeout;
- tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
- FD_ZERO(&fds);
- FD_SET(s->sock_fd, &fds);
- FD_ZERO(&fds_exc);
- FD_SET(s->sock_fd, &fds_exc);
- res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
- if (res == 0) {
- res = WSAEWOULDBLOCK;
- timeout = 1;
- } else if (res > 0) {
- if (FD_ISSET(s->sock_fd, &fds))
- /* The socket is in the writable set - this
- means connected */
- res = 0;
- else {
- /* As per MS docs, we need to call getsockopt()
- to get the underlying error */
- int res_size = sizeof res;
- /* It must be in the exception set */
- assert(FD_ISSET(s->sock_fd, &fds_exc));
- if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
- (char *)&res, &res_size))
- /* getsockopt also clears WSAGetLastError,
- so reset it back. */
- WSASetLastError(res);
- else
- res = WSAGetLastError();
- }
- }
- /* else if (res < 0) an error occurred */
- }
- }
-
- if (res < 0)
- res = WSAGetLastError();
+ if (s->sock_timeout > 0.0) {
+ if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&
+ IS_SELECTABLE(s)) {
+ /* This is a mess. Best solution: trust select */
+ fd_set fds;
+ fd_set fds_exc;
+ struct timeval tv;
+ tv.tv_sec = (int)s->sock_timeout;
+ tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
+ FD_ZERO(&fds);
+ FD_SET(s->sock_fd, &fds);
+ FD_ZERO(&fds_exc);
+ FD_SET(s->sock_fd, &fds_exc);
+ res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);
+ if (res == 0) {
+ res = WSAEWOULDBLOCK;
+ timeout = 1;
+ } else if (res > 0) {
+ if (FD_ISSET(s->sock_fd, &fds))
+ /* The socket is in the writable set - this
+ means connected */
+ res = 0;
+ else {
+ /* As per MS docs, we need to call getsockopt()
+ to get the underlying error */
+ int res_size = sizeof res;
+ /* It must be in the exception set */
+ assert(FD_ISSET(s->sock_fd, &fds_exc));
+ if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,
+ (char *)&res, &res_size))
+ /* getsockopt also clears WSAGetLastError,
+ so reset it back. */
+ WSASetLastError(res);
+ else
+ res = WSAGetLastError();
+ }
+ }
+ /* else if (res < 0) an error occurred */
+ }
+ }
+
+ if (res < 0)
+ res = WSAGetLastError();
#else
- if (s->sock_timeout > 0.0) {
- if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
- timeout = internal_select(s, 1);
- if (timeout == 0) {
- /* Bug #1019808: in case of an EINPROGRESS,
- use getsockopt(SO_ERROR) to get the real
- error. */
- socklen_t res_size = sizeof res;
- (void)getsockopt(s->sock_fd, SOL_SOCKET,
- SO_ERROR, &res, &res_size);
- if (res == EISCONN)
- res = 0;
- errno = res;
- }
- else if (timeout == -1) {
- res = errno; /* had error */
- }
- else
- res = EWOULDBLOCK; /* timed out */
- }
- }
-
- if (res < 0)
- res = errno;
-
-#endif
- *timeoutp = timeout;
-
- return res;
+ if (s->sock_timeout > 0.0) {
+ if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {
+ timeout = internal_select(s, 1);
+ if (timeout == 0) {
+ /* Bug #1019808: in case of an EINPROGRESS,
+ use getsockopt(SO_ERROR) to get the real
+ error. */
+ socklen_t res_size = sizeof res;
+ (void)getsockopt(s->sock_fd, SOL_SOCKET,
+ SO_ERROR, &res, &res_size);
+ if (res == EISCONN)
+ res = 0;
+ errno = res;
+ }
+ else if (timeout == -1) {
+ res = errno; /* had error */
+ }
+ else
+ res = EWOULDBLOCK; /* timed out */
+ }
+ }
+
+ if (res < 0)
+ res = errno;
+
+#endif
+ *timeoutp = timeout;
+
+ return res;
}
/* s.connect(sockaddr) method */
@@ -1962,26 +1962,26 @@ internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
static PyObject *
sock_connect(PySocketSockObject *s, PyObject *addro)
{
- sock_addr_t addrbuf;
- int addrlen;
- int res;
- int timeout;
-
- if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
- Py_END_ALLOW_THREADS
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return NULL;
- }
- if (res != 0)
- return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ sock_addr_t addrbuf;
+ int addrlen;
+ int res;
+ int timeout;
+
+ if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
+ Py_END_ALLOW_THREADS
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return NULL;
+ }
+ if (res != 0)
+ return s->errorhandler();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(connect_doc,
@@ -1996,26 +1996,26 @@ is a pair (host, port).");
static PyObject *
sock_connect_ex(PySocketSockObject *s, PyObject *addro)
{
- sock_addr_t addrbuf;
- int addrlen;
- int res;
- int timeout;
+ sock_addr_t addrbuf;
+ int addrlen;
+ int res;
+ int timeout;
- if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
- return NULL;
+ if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);
+ Py_END_ALLOW_THREADS
- /* Signals are not errors (though they may raise exceptions). Adapted
- from PyErr_SetFromErrnoWithFilenameObject(). */
+ /* Signals are not errors (though they may raise exceptions). Adapted
+ from PyErr_SetFromErrnoWithFilenameObject(). */
#ifdef EINTR
- if (res == EINTR && PyErr_CheckSignals())
- return NULL;
+ if (res == EINTR && PyErr_CheckSignals())
+ return NULL;
#endif
- return PyLong_FromLong((long) res);
+ return PyLong_FromLong((long) res);
}
PyDoc_STRVAR(connect_ex_doc,
@@ -2030,7 +2030,7 @@ instead of raising an exception when an error occurs.");
static PyObject *
sock_fileno(PySocketSockObject *s)
{
- return PyLong_FromSocket_t(s->sock_fd);
+ return PyLong_FromSocket_t(s->sock_fd);
}
PyDoc_STRVAR(fileno_doc,
@@ -2044,20 +2044,20 @@ Return the integer file descriptor of the socket.");
static PyObject *
sock_getsockname(PySocketSockObject *s)
{
- sock_addr_t addrbuf;
- int res;
- socklen_t addrlen;
-
- if (!getsockaddrlen(s, &addrlen))
- return NULL;
- memset(&addrbuf, 0, addrlen);
- Py_BEGIN_ALLOW_THREADS
- res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return s->errorhandler();
- return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
- s->sock_proto);
+ sock_addr_t addrbuf;
+ int res;
+ socklen_t addrlen;
+
+ if (!getsockaddrlen(s, &addrlen))
+ return NULL;
+ memset(&addrbuf, 0, addrlen);
+ Py_BEGIN_ALLOW_THREADS
+ res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return s->errorhandler();
+ return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
+ s->sock_proto);
}
PyDoc_STRVAR(getsockname_doc,
@@ -2067,26 +2067,26 @@ Return the address of the local endpoint. For IP sockets, the address\n\
info is a pair (hostaddr, port).");
-#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
+#ifdef HAVE_GETPEERNAME /* Cray APP doesn't have this :-( */
/* s.getpeername() method */
static PyObject *
sock_getpeername(PySocketSockObject *s)
{
- sock_addr_t addrbuf;
- int res;
- socklen_t addrlen;
-
- if (!getsockaddrlen(s, &addrlen))
- return NULL;
- memset(&addrbuf, 0, addrlen);
- Py_BEGIN_ALLOW_THREADS
- res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return s->errorhandler();
- return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
- s->sock_proto);
+ sock_addr_t addrbuf;
+ int res;
+ socklen_t addrlen;
+
+ if (!getsockaddrlen(s, &addrlen))
+ return NULL;
+ memset(&addrbuf, 0, addrlen);
+ Py_BEGIN_ALLOW_THREADS
+ res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return s->errorhandler();
+ return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,
+ s->sock_proto);
}
PyDoc_STRVAR(getpeername_doc,
@@ -2103,21 +2103,21 @@ info is a pair (hostaddr, port).");
static PyObject *
sock_listen(PySocketSockObject *s, PyObject *arg)
{
- int backlog;
- int res;
-
- backlog = PyLong_AsLong(arg);
- if (backlog == -1 && PyErr_Occurred())
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- if (backlog < 1)
- backlog = 1;
- res = listen(s->sock_fd, backlog);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ int backlog;
+ int res;
+
+ backlog = PyLong_AsLong(arg);
+ if (backlog == -1 && PyErr_Occurred())
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ if (backlog < 1)
+ backlog = 1;
+ res = listen(s->sock_fd, backlog);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return s->errorhandler();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(listen_doc,
@@ -2139,80 +2139,80 @@ will allow before refusing new connections.");
static ssize_t
sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
{
- ssize_t outlen = -1;
- int timeout;
+ ssize_t outlen = -1;
+ int timeout;
#ifdef __VMS
- int remaining;
- char *read_buf;
+ int remaining;
+ char *read_buf;
#endif
- if (!IS_SELECTABLE(s)) {
- select_error();
- return -1;
- }
- if (len == 0) {
- /* If 0 bytes were requested, do nothing. */
- return 0;
- }
+ if (!IS_SELECTABLE(s)) {
+ select_error();
+ return -1;
+ }
+ if (len == 0) {
+ /* If 0 bytes were requested, do nothing. */
+ return 0;
+ }
#ifndef __VMS
- Py_BEGIN_ALLOW_THREADS
- timeout = internal_select(s, 0);
- if (!timeout)
- outlen = recv(s->sock_fd, cbuf, len, flags);
- Py_END_ALLOW_THREADS
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return -1;
- }
- if (outlen < 0) {
- /* Note: the call to errorhandler() ALWAYS indirectly returned
- NULL, so ignore its return value */
- s->errorhandler();
- return -1;
- }
+ Py_BEGIN_ALLOW_THREADS
+ timeout = internal_select(s, 0);
+ if (!timeout)
+ outlen = recv(s->sock_fd, cbuf, len, flags);
+ Py_END_ALLOW_THREADS
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return -1;
+ }
+ if (outlen < 0) {
+ /* Note: the call to errorhandler() ALWAYS indirectly returned
+ NULL, so ignore its return value */
+ s->errorhandler();
+ return -1;
+ }
#else
- read_buf = cbuf;
- remaining = len;
- while (remaining != 0) {
- unsigned int segment;
- int nread = -1;
-
- segment = remaining /SEGMENT_SIZE;
- if (segment != 0) {
- segment = SEGMENT_SIZE;
- }
- else {
- segment = remaining;
- }
-
- Py_BEGIN_ALLOW_THREADS
- timeout = internal_select(s, 0);
- if (!timeout)
- nread = recv(s->sock_fd, read_buf, segment, flags);
- Py_END_ALLOW_THREADS
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return -1;
- }
- if (nread < 0) {
- s->errorhandler();
- return -1;
- }
- if (nread != remaining) {
- read_buf += nread;
- break;
- }
-
- remaining -= segment;
- read_buf += segment;
- }
- outlen = read_buf - cbuf;
+ read_buf = cbuf;
+ remaining = len;
+ while (remaining != 0) {
+ unsigned int segment;
+ int nread = -1;
+
+ segment = remaining /SEGMENT_SIZE;
+ if (segment != 0) {
+ segment = SEGMENT_SIZE;
+ }
+ else {
+ segment = remaining;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ timeout = internal_select(s, 0);
+ if (!timeout)
+ nread = recv(s->sock_fd, read_buf, segment, flags);
+ Py_END_ALLOW_THREADS
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return -1;
+ }
+ if (nread < 0) {
+ s->errorhandler();
+ return -1;
+ }
+ if (nread != remaining) {
+ read_buf += nread;
+ break;
+ }
+
+ remaining -= segment;
+ read_buf += segment;
+ }
+ outlen = read_buf - cbuf;
#endif /* !__VMS */
- return outlen;
+ return outlen;
}
@@ -2221,39 +2221,39 @@ sock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags)
static PyObject *
sock_recv(PySocketSockObject *s, PyObject *args)
{
- int recvlen, flags = 0;
- ssize_t outlen;
- PyObject *buf;
-
- if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
- return NULL;
-
- if (recvlen < 0) {
- PyErr_SetString(PyExc_ValueError,
- "negative buffersize in recv");
- return NULL;
- }
-
- /* Allocate a new string. */
- buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
- if (buf == NULL)
- return NULL;
-
- /* Call the guts */
- outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
- if (outlen < 0) {
- /* An error occurred, release the string and return an
- error. */
- Py_DECREF(buf);
- return NULL;
- }
- if (outlen != recvlen) {
- /* We did not read as many bytes as we anticipated, resize the
- string if possible and be successful. */
- _PyBytes_Resize(&buf, outlen);
- }
-
- return buf;
+ int recvlen, flags = 0;
+ ssize_t outlen;
+ PyObject *buf;
+
+ if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))
+ return NULL;
+
+ if (recvlen < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative buffersize in recv");
+ return NULL;
+ }
+
+ /* Allocate a new string. */
+ buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
+ if (buf == NULL)
+ return NULL;
+
+ /* Call the guts */
+ outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
+ if (outlen < 0) {
+ /* An error occurred, release the string and return an
+ error. */
+ Py_DECREF(buf);
+ return NULL;
+ }
+ if (outlen != recvlen) {
+ /* We did not read as many bytes as we anticipated, resize the
+ string if possible and be successful. */
+ _PyBytes_Resize(&buf, outlen);
+ }
+
+ return buf;
}
PyDoc_STRVAR(recv_doc,
@@ -2270,52 +2270,52 @@ the remote end is closed and all data is read, return the empty string.");
static PyObject*
sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
-
- int recvlen = 0, flags = 0;
- ssize_t readlen;
- Py_buffer pbuf;
- char *buf;
- int buflen;
-
- /* Get the buffer's memory */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist,
- &pbuf, &recvlen, &flags))
- return NULL;
- buf = pbuf.buf;
- buflen = pbuf.len;
-
- if (recvlen < 0) {
- PyBuffer_Release(&pbuf);
- PyErr_SetString(PyExc_ValueError,
- "negative buffersize in recv_into");
- return NULL;
- }
- if (recvlen == 0) {
- /* If nbytes was not specified, use the buffer's length */
- recvlen = buflen;
- }
-
- /* Check if the buffer is large enough */
- if (buflen < recvlen) {
- PyBuffer_Release(&pbuf);
- PyErr_SetString(PyExc_ValueError,
- "buffer too small for requested bytes");
- return NULL;
- }
-
- /* Call the guts */
- readlen = sock_recv_guts(s, buf, recvlen, flags);
- if (readlen < 0) {
- /* Return an error. */
- PyBuffer_Release(&pbuf);
- return NULL;
- }
-
- PyBuffer_Release(&pbuf);
- /* Return the number of bytes read. Note that we do not do anything
- special here in the case that readlen < recvlen. */
- return PyLong_FromSsize_t(readlen);
+ static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
+
+ int recvlen = 0, flags = 0;
+ ssize_t readlen;
+ Py_buffer pbuf;
+ char *buf;
+ int buflen;
+
+ /* Get the buffer's memory */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recv_into", kwlist,
+ &pbuf, &recvlen, &flags))
+ return NULL;
+ buf = pbuf.buf;
+ buflen = pbuf.len;
+
+ if (recvlen < 0) {
+ PyBuffer_Release(&pbuf);
+ PyErr_SetString(PyExc_ValueError,
+ "negative buffersize in recv_into");
+ return NULL;
+ }
+ if (recvlen == 0) {
+ /* If nbytes was not specified, use the buffer's length */
+ recvlen = buflen;
+ }
+
+ /* Check if the buffer is large enough */
+ if (buflen < recvlen) {
+ PyBuffer_Release(&pbuf);
+ PyErr_SetString(PyExc_ValueError,
+ "buffer too small for requested bytes");
+ return NULL;
+ }
+
+ /* Call the guts */
+ readlen = sock_recv_guts(s, buf, recvlen, flags);
+ if (readlen < 0) {
+ /* Return an error. */
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
+
+ PyBuffer_Release(&pbuf);
+ /* Return the number of bytes read. Note that we do not do anything
+ special here in the case that readlen < recvlen. */
+ return PyLong_FromSsize_t(readlen);
}
PyDoc_STRVAR(recv_into_doc,
@@ -2341,56 +2341,56 @@ See recv() for documentation about the flags.");
*/
static ssize_t
sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
- PyObject** addr)
+ PyObject** addr)
{
- sock_addr_t addrbuf;
- int timeout;
- ssize_t n = -1;
- socklen_t addrlen;
+ sock_addr_t addrbuf;
+ int timeout;
+ ssize_t n = -1;
+ socklen_t addrlen;
- *addr = NULL;
+ *addr = NULL;
- if (!getsockaddrlen(s, &addrlen))
- return -1;
+ if (!getsockaddrlen(s, &addrlen))
+ return -1;
- if (!IS_SELECTABLE(s)) {
- select_error();
- return -1;
- }
+ if (!IS_SELECTABLE(s)) {
+ select_error();
+ return -1;
+ }
- Py_BEGIN_ALLOW_THREADS
- memset(&addrbuf, 0, addrlen);
- timeout = internal_select(s, 0);
- if (!timeout) {
+ Py_BEGIN_ALLOW_THREADS
+ memset(&addrbuf, 0, addrlen);
+ timeout = internal_select(s, 0);
+ if (!timeout) {
#ifndef MS_WINDOWS
#if defined(PYOS_OS2) && !defined(PYCC_GCC)
- n = recvfrom(s->sock_fd, cbuf, len, flags,
- SAS2SA(&addrbuf), &addrlen);
+ n = recvfrom(s->sock_fd, cbuf, len, flags,
+ SAS2SA(&addrbuf), &addrlen);
#else
- n = recvfrom(s->sock_fd, cbuf, len, flags,
- (void *) &addrbuf, &addrlen);
+ n = recvfrom(s->sock_fd, cbuf, len, flags,
+ (void *) &addrbuf, &addrlen);
#endif
#else
- n = recvfrom(s->sock_fd, cbuf, len, flags,
- SAS2SA(&addrbuf), &addrlen);
-#endif
- }
- Py_END_ALLOW_THREADS
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return -1;
- }
- if (n < 0) {
- s->errorhandler();
- return -1;
- }
-
- if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
- addrlen, s->sock_proto)))
- return -1;
-
- return n;
+ n = recvfrom(s->sock_fd, cbuf, len, flags,
+ SAS2SA(&addrbuf), &addrlen);
+#endif
+ }
+ Py_END_ALLOW_THREADS
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return -1;
+ }
+ if (n < 0) {
+ s->errorhandler();
+ return -1;
+ }
+
+ if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),
+ addrlen, s->sock_proto)))
+ return -1;
+
+ return n;
}
/* s.recvfrom(nbytes [,flags]) method */
@@ -2398,45 +2398,45 @@ sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,
static PyObject *
sock_recvfrom(PySocketSockObject *s, PyObject *args)
{
- PyObject *buf = NULL;
- PyObject *addr = NULL;
- PyObject *ret = NULL;
- int recvlen, flags = 0;
- ssize_t outlen;
-
- if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
- return NULL;
-
- if (recvlen < 0) {
- PyErr_SetString(PyExc_ValueError,
- "negative buffersize in recvfrom");
- return NULL;
- }
-
- buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
- if (buf == NULL)
- return NULL;
-
- outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
- recvlen, flags, &addr);
- if (outlen < 0) {
- goto finally;
- }
-
- if (outlen != recvlen) {
- /* We did not read as many bytes as we anticipated, resize the
- string if possible and be succesful. */
- if (_PyBytes_Resize(&buf, outlen) < 0)
- /* Oopsy, not so succesful after all. */
- goto finally;
- }
-
- ret = PyTuple_Pack(2, buf, addr);
+ PyObject *buf = NULL;
+ PyObject *addr = NULL;
+ PyObject *ret = NULL;
+ int recvlen, flags = 0;
+ ssize_t outlen;
+
+ if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))
+ return NULL;
+
+ if (recvlen < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative buffersize in recvfrom");
+ return NULL;
+ }
+
+ buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
+ if (buf == NULL)
+ return NULL;
+
+ outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
+ recvlen, flags, &addr);
+ if (outlen < 0) {
+ goto finally;
+ }
+
+ if (outlen != recvlen) {
+ /* We did not read as many bytes as we anticipated, resize the
+ string if possible and be succesful. */
+ if (_PyBytes_Resize(&buf, outlen) < 0)
+ /* Oopsy, not so succesful after all. */
+ goto finally;
+ }
+
+ ret = PyTuple_Pack(2, buf, addr);
finally:
- Py_XDECREF(buf);
- Py_XDECREF(addr);
- return ret;
+ Py_XDECREF(buf);
+ Py_XDECREF(addr);
+ return ret;
}
PyDoc_STRVAR(recvfrom_doc,
@@ -2450,47 +2450,47 @@ Like recv(buffersize, flags) but also return the sender's address info.");
static PyObject *
sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
{
- static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
-
- int recvlen = 0, flags = 0;
- ssize_t readlen;
- Py_buffer pbuf;
- char *buf;
- int buflen;
-
- PyObject *addr = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into",
- kwlist, &pbuf,
- &recvlen, &flags))
- return NULL;
- buf = pbuf.buf;
- buflen = pbuf.len;
- assert(buf != 0 && buflen > 0);
-
- if (recvlen < 0) {
- PyBuffer_Release(&pbuf);
- PyErr_SetString(PyExc_ValueError,
- "negative buffersize in recvfrom_into");
- return NULL;
- }
- if (recvlen == 0) {
- /* If nbytes was not specified, use the buffer's length */
- recvlen = buflen;
- }
-
- readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
- if (readlen < 0) {
- PyBuffer_Release(&pbuf);
- /* Return an error */
- Py_XDECREF(addr);
- return NULL;
- }
-
- PyBuffer_Release(&pbuf);
- /* Return the number of bytes read and the address. Note that we do
- not do anything special here in the case that readlen < recvlen. */
- return Py_BuildValue("lN", readlen, addr);
+ static char *kwlist[] = {"buffer", "nbytes", "flags", 0};
+
+ int recvlen = 0, flags = 0;
+ ssize_t readlen;
+ Py_buffer pbuf;
+ char *buf;
+ int buflen;
+
+ PyObject *addr = NULL;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "w*|ii:recvfrom_into",
+ kwlist, &pbuf,
+ &recvlen, &flags))
+ return NULL;
+ buf = pbuf.buf;
+ buflen = pbuf.len;
+ assert(buf != 0 && buflen > 0);
+
+ if (recvlen < 0) {
+ PyBuffer_Release(&pbuf);
+ PyErr_SetString(PyExc_ValueError,
+ "negative buffersize in recvfrom_into");
+ return NULL;
+ }
+ if (recvlen == 0) {
+ /* If nbytes was not specified, use the buffer's length */
+ recvlen = buflen;
+ }
+
+ readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);
+ if (readlen < 0) {
+ PyBuffer_Release(&pbuf);
+ /* Return an error */
+ Py_XDECREF(addr);
+ return NULL;
+ }
+
+ PyBuffer_Release(&pbuf);
+ /* Return the number of bytes read and the address. Note that we do
+ not do anything special here in the case that readlen < recvlen. */
+ return Py_BuildValue("lN", readlen, addr);
}
PyDoc_STRVAR(recvfrom_into_doc,
@@ -2504,39 +2504,39 @@ Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address i
static PyObject *
sock_send(PySocketSockObject *s, PyObject *args)
{
- char *buf;
- int len, n = -1, flags = 0, timeout;
- Py_buffer pbuf;
-
- if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
- return NULL;
-
- if (!IS_SELECTABLE(s)) {
- PyBuffer_Release(&pbuf);
- return select_error();
- }
- buf = pbuf.buf;
- len = pbuf.len;
-
- Py_BEGIN_ALLOW_THREADS
- timeout = internal_select(s, 1);
- if (!timeout)
+ char *buf;
+ int len, n = -1, flags = 0, timeout;
+ Py_buffer pbuf;
+
+ if (!PyArg_ParseTuple(args, "y*|i:send", &pbuf, &flags))
+ return NULL;
+
+ if (!IS_SELECTABLE(s)) {
+ PyBuffer_Release(&pbuf);
+ return select_error();
+ }
+ buf = pbuf.buf;
+ len = pbuf.len;
+
+ Py_BEGIN_ALLOW_THREADS
+ timeout = internal_select(s, 1);
+ if (!timeout)
#ifdef __VMS
- n = sendsegmented(s->sock_fd, buf, len, flags);
+ n = sendsegmented(s->sock_fd, buf, len, flags);
#else
- n = send(s->sock_fd, buf, len, flags);
+ n = send(s->sock_fd, buf, len, flags);
#endif
- Py_END_ALLOW_THREADS
+ Py_END_ALLOW_THREADS
- PyBuffer_Release(&pbuf);
+ PyBuffer_Release(&pbuf);
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return NULL;
- }
- if (n < 0)
- return s->errorhandler();
- return PyLong_FromLong((long)n);
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return NULL;
+ }
+ if (n < 0)
+ return s->errorhandler();
+ return PyLong_FromLong((long)n);
}
PyDoc_STRVAR(send_doc,
@@ -2552,48 +2552,48 @@ sent; this may be less than len(data) if the network is busy.");
static PyObject *
sock_sendall(PySocketSockObject *s, PyObject *args)
{
- char *buf;
- int len, n = -1, flags = 0, timeout;
- Py_buffer pbuf;
-
- if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
- return NULL;
- buf = pbuf.buf;
- len = pbuf.len;
-
- if (!IS_SELECTABLE(s)) {
- PyBuffer_Release(&pbuf);
- return select_error();
- }
-
- Py_BEGIN_ALLOW_THREADS
- do {
- timeout = internal_select(s, 1);
- n = -1;
- if (timeout)
- break;
+ char *buf;
+ int len, n = -1, flags = 0, timeout;
+ Py_buffer pbuf;
+
+ if (!PyArg_ParseTuple(args, "y*|i:sendall", &pbuf, &flags))
+ return NULL;
+ buf = pbuf.buf;
+ len = pbuf.len;
+
+ if (!IS_SELECTABLE(s)) {
+ PyBuffer_Release(&pbuf);
+ return select_error();
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ do {
+ timeout = internal_select(s, 1);
+ n = -1;
+ if (timeout)
+ break;
#ifdef __VMS
- n = sendsegmented(s->sock_fd, buf, len, flags);
+ n = sendsegmented(s->sock_fd, buf, len, flags);
#else
- n = send(s->sock_fd, buf, len, flags);
-#endif
- if (n < 0)
- break;
- buf += n;
- len -= n;
- } while (len > 0);
- Py_END_ALLOW_THREADS
- PyBuffer_Release(&pbuf);
-
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return NULL;
- }
- if (n < 0)
- return s->errorhandler();
-
- Py_INCREF(Py_None);
- return Py_None;
+ n = send(s->sock_fd, buf, len, flags);
+#endif
+ if (n < 0)
+ break;
+ buf += n;
+ len -= n;
+ } while (len > 0);
+ Py_END_ALLOW_THREADS
+ PyBuffer_Release(&pbuf);
+
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return NULL;
+ }
+ if (n < 0)
+ return s->errorhandler();
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(sendall_doc,
@@ -2610,47 +2610,47 @@ to tell how much data has been sent.");
static PyObject *
sock_sendto(PySocketSockObject *s, PyObject *args)
{
- Py_buffer pbuf;
- PyObject *addro;
- char *buf;
- Py_ssize_t len;
- sock_addr_t addrbuf;
- int addrlen, n = -1, flags, timeout;
-
- flags = 0;
- if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
- PyErr_Clear();
- if (!PyArg_ParseTuple(args, "y*iO:sendto",
- &pbuf, &flags, &addro))
- return NULL;
- }
- buf = pbuf.buf;
- len = pbuf.len;
-
- if (!IS_SELECTABLE(s)) {
- PyBuffer_Release(&pbuf);
- return select_error();
- }
-
- if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
- PyBuffer_Release(&pbuf);
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- timeout = internal_select(s, 1);
- if (!timeout)
- n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
- Py_END_ALLOW_THREADS
-
- PyBuffer_Release(&pbuf);
- if (timeout == 1) {
- PyErr_SetString(socket_timeout, "timed out");
- return NULL;
- }
- if (n < 0)
- return s->errorhandler();
- return PyLong_FromLong((long)n);
+ Py_buffer pbuf;
+ PyObject *addro;
+ char *buf;
+ Py_ssize_t len;
+ sock_addr_t addrbuf;
+ int addrlen, n = -1, flags, timeout;
+
+ flags = 0;
+ if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) {
+ PyErr_Clear();
+ if (!PyArg_ParseTuple(args, "y*iO:sendto",
+ &pbuf, &flags, &addro))
+ return NULL;
+ }
+ buf = pbuf.buf;
+ len = pbuf.len;
+
+ if (!IS_SELECTABLE(s)) {
+ PyBuffer_Release(&pbuf);
+ return select_error();
+ }
+
+ if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) {
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ timeout = internal_select(s, 1);
+ if (!timeout)
+ n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
+ Py_END_ALLOW_THREADS
+
+ PyBuffer_Release(&pbuf);
+ if (timeout == 1) {
+ PyErr_SetString(socket_timeout, "timed out");
+ return NULL;
+ }
+ if (n < 0)
+ return s->errorhandler();
+ return PyLong_FromLong((long)n);
}
PyDoc_STRVAR(sendto_doc,
@@ -2665,19 +2665,19 @@ For IP sockets, the address is a pair (hostaddr, port).");
static PyObject *
sock_shutdown(PySocketSockObject *s, PyObject *arg)
{
- int how;
- int res;
-
- how = PyLong_AsLong(arg);
- if (how == -1 && PyErr_Occurred())
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- res = shutdown(s->sock_fd, how);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ int how;
+ int res;
+
+ how = PyLong_AsLong(arg);
+ if (how == -1 && PyErr_Occurred())
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ res = shutdown(s->sock_fd, how);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return s->errorhandler();
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(shutdown_doc,
@@ -2690,18 +2690,18 @@ of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");
static PyObject*
sock_ioctl(PySocketSockObject *s, PyObject *arg)
{
- unsigned long cmd = SIO_RCVALL;
- unsigned int option = RCVALL_ON;
- DWORD recv;
-
- if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
- return NULL;
-
- if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
- NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
- return set_error();
- }
- return PyLong_FromUnsignedLong(recv);
+ unsigned long cmd = SIO_RCVALL;
+ unsigned int option = RCVALL_ON;
+ DWORD recv;
+
+ if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
+ return NULL;
+
+ if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
+ NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
+ return set_error();
+ }
+ return PyLong_FromUnsignedLong(recv);
}
PyDoc_STRVAR(sock_ioctl_doc,
"ioctl(cmd, option) -> long\n\
@@ -2715,57 +2715,57 @@ constants.");
/* List of methods for socket objects */
static PyMethodDef sock_methods[] = {
- {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
- accept_doc},
- {"bind", (PyCFunction)sock_bind, METH_O,
- bind_doc},
- {"close", (PyCFunction)sock_close, METH_NOARGS,
- close_doc},
- {"connect", (PyCFunction)sock_connect, METH_O,
- connect_doc},
- {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
- connect_ex_doc},
- {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
- fileno_doc},
+ {"_accept", (PyCFunction)sock_accept, METH_NOARGS,
+ accept_doc},
+ {"bind", (PyCFunction)sock_bind, METH_O,
+ bind_doc},
+ {"close", (PyCFunction)sock_close, METH_NOARGS,
+ close_doc},
+ {"connect", (PyCFunction)sock_connect, METH_O,
+ connect_doc},
+ {"connect_ex", (PyCFunction)sock_connect_ex, METH_O,
+ connect_ex_doc},
+ {"fileno", (PyCFunction)sock_fileno, METH_NOARGS,
+ fileno_doc},
#ifdef HAVE_GETPEERNAME
- {"getpeername", (PyCFunction)sock_getpeername,
- METH_NOARGS, getpeername_doc},
+ {"getpeername", (PyCFunction)sock_getpeername,
+ METH_NOARGS, getpeername_doc},
#endif
- {"getsockname", (PyCFunction)sock_getsockname,
- METH_NOARGS, getsockname_doc},
- {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
- getsockopt_doc},
+ {"getsockname", (PyCFunction)sock_getsockname,
+ METH_NOARGS, getsockname_doc},
+ {"getsockopt", (PyCFunction)sock_getsockopt, METH_VARARGS,
+ getsockopt_doc},
#if defined(MS_WINDOWS) && defined(SIO_RCVALL)
- {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
- sock_ioctl_doc},
-#endif
- {"listen", (PyCFunction)sock_listen, METH_O,
- listen_doc},
- {"recv", (PyCFunction)sock_recv, METH_VARARGS,
- recv_doc},
- {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
- recv_into_doc},
- {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
- recvfrom_doc},
- {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
- recvfrom_into_doc},
- {"send", (PyCFunction)sock_send, METH_VARARGS,
- send_doc},
- {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
- sendall_doc},
- {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
- sendto_doc},
- {"setblocking", (PyCFunction)sock_setblocking, METH_O,
- setblocking_doc},
- {"settimeout", (PyCFunction)sock_settimeout, METH_O,
- settimeout_doc},
- {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
- gettimeout_doc},
- {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
- setsockopt_doc},
- {"shutdown", (PyCFunction)sock_shutdown, METH_O,
- shutdown_doc},
- {NULL, NULL} /* sentinel */
+ {"ioctl", (PyCFunction)sock_ioctl, METH_VARARGS,
+ sock_ioctl_doc},
+#endif
+ {"listen", (PyCFunction)sock_listen, METH_O,
+ listen_doc},
+ {"recv", (PyCFunction)sock_recv, METH_VARARGS,
+ recv_doc},
+ {"recv_into", (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,
+ recv_into_doc},
+ {"recvfrom", (PyCFunction)sock_recvfrom, METH_VARARGS,
+ recvfrom_doc},
+ {"recvfrom_into", (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,
+ recvfrom_into_doc},
+ {"send", (PyCFunction)sock_send, METH_VARARGS,
+ send_doc},
+ {"sendall", (PyCFunction)sock_sendall, METH_VARARGS,
+ sendall_doc},
+ {"sendto", (PyCFunction)sock_sendto, METH_VARARGS,
+ sendto_doc},
+ {"setblocking", (PyCFunction)sock_setblocking, METH_O,
+ setblocking_doc},
+ {"settimeout", (PyCFunction)sock_settimeout, METH_O,
+ settimeout_doc},
+ {"gettimeout", (PyCFunction)sock_gettimeout, METH_NOARGS,
+ gettimeout_doc},
+ {"setsockopt", (PyCFunction)sock_setsockopt, METH_VARARGS,
+ setsockopt_doc},
+ {"shutdown", (PyCFunction)sock_shutdown, METH_O,
+ shutdown_doc},
+ {NULL, NULL} /* sentinel */
};
/* SockObject members */
@@ -2783,9 +2783,9 @@ static PyMemberDef sock_memberlist[] = {
static void
sock_dealloc(PySocketSockObject *s)
{
- if (s->sock_fd != -1)
- (void) SOCKETCLOSE(s->sock_fd);
- Py_TYPE(s)->tp_free((PyObject *)s);
+ if (s->sock_fd != -1)
+ (void) SOCKETCLOSE(s->sock_fd);
+ Py_TYPE(s)->tp_free((PyObject *)s);
}
@@ -2793,21 +2793,21 @@ static PyObject *
sock_repr(PySocketSockObject *s)
{
#if SIZEOF_SOCKET_T > SIZEOF_LONG
- if (s->sock_fd > LONG_MAX) {
- /* this can occur on Win64, and actually there is a special
- ugly printf formatter for decimal pointer length integer
- printing, only bother if necessary*/
- PyErr_SetString(PyExc_OverflowError,
- "no printf formatter to display "
- "the socket descriptor in decimal");
- return NULL;
- }
-#endif
- return PyUnicode_FromFormat(
- "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
- (long)s->sock_fd, s->sock_family,
- s->sock_type,
- s->sock_proto);
+ if (s->sock_fd > LONG_MAX) {
+ /* this can occur on Win64, and actually there is a special
+ ugly printf formatter for decimal pointer length integer
+ printing, only bother if necessary*/
+ PyErr_SetString(PyExc_OverflowError,
+ "no printf formatter to display "
+ "the socket descriptor in decimal");
+ return NULL;
+ }
+#endif
+ return PyUnicode_FromFormat(
+ "<socket object, fd=%ld, family=%d, type=%d, proto=%d>",
+ (long)s->sock_fd, s->sock_family,
+ s->sock_type,
+ s->sock_proto);
}
@@ -2816,15 +2816,15 @@ sock_repr(PySocketSockObject *s)
static PyObject *
sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *new;
-
- new = type->tp_alloc(type, 0);
- if (new != NULL) {
- ((PySocketSockObject *)new)->sock_fd = -1;
- ((PySocketSockObject *)new)->sock_timeout = -1.0;
- ((PySocketSockObject *)new)->errorhandler = &set_error;
- }
- return new;
+ PyObject *new;
+
+ new = type->tp_alloc(type, 0);
+ if (new != NULL) {
+ ((PySocketSockObject *)new)->sock_fd = -1;
+ ((PySocketSockObject *)new)->sock_timeout = -1.0;
+ ((PySocketSockObject *)new)->errorhandler = &set_error;
+ }
+ return new;
}
@@ -2834,40 +2834,40 @@ sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
{
- PySocketSockObject *s = (PySocketSockObject *)self;
- PyObject *fdobj = NULL;
- SOCKET_T fd = INVALID_SOCKET;
- int family = AF_INET, type = SOCK_STREAM, proto = 0;
- static char *keywords[] = {"family", "type", "proto", "fileno", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds,
- "|iiiO:socket", keywords,
- &family, &type, &proto, &fdobj))
- return -1;
-
- if (fdobj != NULL && fdobj != Py_None) {
- fd = PyLong_AsSocket_t(fdobj);
- if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
- return -1;
- if (fd == INVALID_SOCKET) {
- PyErr_SetString(PyExc_ValueError,
- "can't use invalid socket value");
- return -1;
- }
- }
- else {
- Py_BEGIN_ALLOW_THREADS
- fd = socket(family, type, proto);
- Py_END_ALLOW_THREADS
-
- if (fd == INVALID_SOCKET) {
- set_error();
- return -1;
- }
- }
- init_sockobject(s, fd, family, type, proto);
-
- return 0;
+ PySocketSockObject *s = (PySocketSockObject *)self;
+ PyObject *fdobj = NULL;
+ SOCKET_T fd = INVALID_SOCKET;
+ int family = AF_INET, type = SOCK_STREAM, proto = 0;
+ static char *keywords[] = {"family", "type", "proto", "fileno", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds,
+ "|iiiO:socket", keywords,
+ &family, &type, &proto, &fdobj))
+ return -1;
+
+ if (fdobj != NULL && fdobj != Py_None) {
+ fd = PyLong_AsSocket_t(fdobj);
+ if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
+ return -1;
+ if (fd == INVALID_SOCKET) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't use invalid socket value");
+ return -1;
+ }
+ }
+ else {
+ Py_BEGIN_ALLOW_THREADS
+ fd = socket(family, type, proto);
+ Py_END_ALLOW_THREADS
+
+ if (fd == INVALID_SOCKET) {
+ set_error();
+ return -1;
+ }
+ }
+ init_sockobject(s, fd, family, type, proto);
+
+ return 0;
}
@@ -2875,45 +2875,45 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
/* Type object for socket objects. */
static PyTypeObject sock_type = {
- PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
- "_socket.socket", /* tp_name */
- sizeof(PySocketSockObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)sock_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)sock_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- sock_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- sock_methods, /* tp_methods */
- sock_memberlist, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- sock_initobj, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- sock_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
+ "_socket.socket", /* tp_name */
+ sizeof(PySocketSockObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)sock_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)sock_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ sock_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ sock_methods, /* tp_methods */
+ sock_memberlist, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ sock_initobj, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ sock_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
@@ -2923,15 +2923,15 @@ static PyTypeObject sock_type = {
static PyObject *
socket_gethostname(PyObject *self, PyObject *unused)
{
- char buf[1024];
- int res;
- Py_BEGIN_ALLOW_THREADS
- res = gethostname(buf, (int) sizeof buf - 1);
- Py_END_ALLOW_THREADS
- if (res < 0)
- return set_error();
- buf[sizeof buf - 1] = '\0';
- return PyUnicode_FromString(buf);
+ char buf[1024];
+ int res;
+ Py_BEGIN_ALLOW_THREADS
+ res = gethostname(buf, (int) sizeof buf - 1);
+ Py_END_ALLOW_THREADS
+ if (res < 0)
+ return set_error();
+ buf[sizeof buf - 1] = '\0';
+ return PyUnicode_FromString(buf);
}
PyDoc_STRVAR(gethostname_doc,
@@ -2946,14 +2946,14 @@ Return the current host name.");
static PyObject *
socket_gethostbyname(PyObject *self, PyObject *args)
{
- char *name;
- sock_addr_t addrbuf;
-
- if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
- return NULL;
- if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
- return NULL;
- return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
+ char *name;
+ sock_addr_t addrbuf;
+
+ if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
+ return NULL;
+ if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
+ return NULL;
+ return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
}
PyDoc_STRVAR(gethostbyname_doc,
@@ -2967,126 +2967,126 @@ Return the IP address (a string of the form '255.255.255.255') for a host.");
static PyObject *
gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
{
- char **pch;
- PyObject *rtn_tuple = (PyObject *)NULL;
- PyObject *name_list = (PyObject *)NULL;
- PyObject *addr_list = (PyObject *)NULL;
- PyObject *tmp;
+ char **pch;
+ PyObject *rtn_tuple = (PyObject *)NULL;
+ PyObject *name_list = (PyObject *)NULL;
+ PyObject *addr_list = (PyObject *)NULL;
+ PyObject *tmp;
- if (h == NULL) {
- /* Let's get real error message to return */
- set_herror(h_errno);
- return NULL;
- }
+ if (h == NULL) {
+ /* Let's get real error message to return */
+ set_herror(h_errno);
+ return NULL;
+ }
- if (h->h_addrtype != af) {
- /* Let's get real error message to return */
- PyErr_SetString(socket_error,
- (char *)strerror(EAFNOSUPPORT));
+ if (h->h_addrtype != af) {
+ /* Let's get real error message to return */
+ PyErr_SetString(socket_error,
+ (char *)strerror(EAFNOSUPPORT));
- return NULL;
- }
+ return NULL;
+ }
- switch (af) {
+ switch (af) {
- case AF_INET:
- if (alen < sizeof(struct sockaddr_in))
- return NULL;
- break;
+ case AF_INET:
+ if (alen < sizeof(struct sockaddr_in))
+ return NULL;
+ break;
#ifdef ENABLE_IPV6
- case AF_INET6:
- if (alen < sizeof(struct sockaddr_in6))
- return NULL;
- break;
+ case AF_INET6:
+ if (alen < sizeof(struct sockaddr_in6))
+ return NULL;
+ break;
#endif
- }
+ }
- if ((name_list = PyList_New(0)) == NULL)
- goto err;
+ if ((name_list = PyList_New(0)) == NULL)
+ goto err;
- if ((addr_list = PyList_New(0)) == NULL)
- goto err;
+ if ((addr_list = PyList_New(0)) == NULL)
+ goto err;
- /* SF #1511317: h_aliases can be NULL */
- if (h->h_aliases) {
- for (pch = h->h_aliases; *pch != NULL; pch++) {
- int status;
- tmp = PyUnicode_FromString(*pch);
- if (tmp == NULL)
- goto err;
+ /* SF #1511317: h_aliases can be NULL */
+ if (h->h_aliases) {
+ for (pch = h->h_aliases; *pch != NULL; pch++) {
+ int status;
+ tmp = PyUnicode_FromString(*pch);
+ if (tmp == NULL)
+ goto err;
- status = PyList_Append(name_list, tmp);
- Py_DECREF(tmp);
+ status = PyList_Append(name_list, tmp);
+ Py_DECREF(tmp);
- if (status)
- goto err;
- }
- }
+ if (status)
+ goto err;
+ }
+ }
- for (pch = h->h_addr_list; *pch != NULL; pch++) {
- int status;
+ for (pch = h->h_addr_list; *pch != NULL; pch++) {
+ int status;
- switch (af) {
+ switch (af) {
- case AF_INET:
- {
- struct sockaddr_in sin;
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = af;
+ case AF_INET:
+ {
+ struct sockaddr_in sin;
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = af;
#ifdef HAVE_SOCKADDR_SA_LEN
- sin.sin_len = sizeof(sin);
+ sin.sin_len = sizeof(sin);
#endif
- memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
- tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
+ memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
+ tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin));
- if (pch == h->h_addr_list && alen >= sizeof(sin))
- memcpy((char *) addr, &sin, sizeof(sin));
- break;
- }
+ if (pch == h->h_addr_list && alen >= sizeof(sin))
+ memcpy((char *) addr, &sin, sizeof(sin));
+ break;
+ }
#ifdef ENABLE_IPV6
- case AF_INET6:
- {
- struct sockaddr_in6 sin6;
- memset(&sin6, 0, sizeof(sin6));
- sin6.sin6_family = af;
+ case AF_INET6:
+ {
+ struct sockaddr_in6 sin6;
+ memset(&sin6, 0, sizeof(sin6));
+ sin6.sin6_family = af;
#ifdef HAVE_SOCKADDR_SA_LEN
- sin6.sin6_len = sizeof(sin6);
+ sin6.sin6_len = sizeof(sin6);
#endif
- memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
- tmp = makeipaddr((struct sockaddr *)&sin6,
- sizeof(sin6));
+ memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
+ tmp = makeipaddr((struct sockaddr *)&sin6,
+ sizeof(sin6));
- if (pch == h->h_addr_list && alen >= sizeof(sin6))
- memcpy((char *) addr, &sin6, sizeof(sin6));
- break;
- }
+ if (pch == h->h_addr_list && alen >= sizeof(sin6))
+ memcpy((char *) addr, &sin6, sizeof(sin6));
+ break;
+ }
#endif
- default: /* can't happen */
- PyErr_SetString(socket_error,
- "unsupported address family");
- return NULL;
- }
+ default: /* can't happen */
+ PyErr_SetString(socket_error,
+ "unsupported address family");
+ return NULL;
+ }
- if (tmp == NULL)
- goto err;
+ if (tmp == NULL)
+ goto err;
- status = PyList_Append(addr_list, tmp);
- Py_DECREF(tmp);
+ status = PyList_Append(addr_list, tmp);
+ Py_DECREF(tmp);
- if (status)
- goto err;
- }
+ if (status)
+ goto err;
+ }
- rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
+ rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
err:
- Py_XDECREF(name_list);
- Py_XDECREF(addr_list);
- return rtn_tuple;
+ Py_XDECREF(name_list);
+ Py_XDECREF(addr_list);
+ return rtn_tuple;
}
@@ -3096,63 +3096,63 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
static PyObject *
socket_gethostbyname_ex(PyObject *self, PyObject *args)
{
- char *name;
- struct hostent *h;
+ char *name;
+ struct hostent *h;
#ifdef ENABLE_IPV6
- struct sockaddr_storage addr;
+ struct sockaddr_storage addr;
#else
- struct sockaddr_in addr;
+ struct sockaddr_in addr;
#endif
- struct sockaddr *sa;
- PyObject *ret;
+ struct sockaddr *sa;
+ PyObject *ret;
#ifdef HAVE_GETHOSTBYNAME_R
- struct hostent hp_allocated;
+ struct hostent hp_allocated;
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
- struct hostent_data data;
+ struct hostent_data data;
#else
- char buf[16384];
- int buf_len = (sizeof buf) - 1;
- int errnop;
+ char buf[16384];
+ int buf_len = (sizeof buf) - 1;
+ int errnop;
#endif
#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
- int result;
+ int result;
#endif
#endif /* HAVE_GETHOSTBYNAME_R */
- if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
- return NULL;
- if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
- return NULL;
- Py_BEGIN_ALLOW_THREADS
+ if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
+ return NULL;
+ if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
#ifdef HAVE_GETHOSTBYNAME_R
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
- result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
- &h, &errnop);
+ result = gethostbyname_r(name, &hp_allocated, buf, buf_len,
+ &h, &errnop);
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
- h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
+ h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
- memset((void *) &data, '\0', sizeof(data));
- result = gethostbyname_r(name, &hp_allocated, &data);
- h = (result != 0) ? NULL : &hp_allocated;
+ memset((void *) &data, '\0', sizeof(data));
+ result = gethostbyname_r(name, &hp_allocated, &data);
+ h = (result != 0) ? NULL : &hp_allocated;
#endif
#else /* not HAVE_GETHOSTBYNAME_R */
#ifdef USE_GETHOSTBYNAME_LOCK
- PyThread_acquire_lock(netdb_lock, 1);
+ PyThread_acquire_lock(netdb_lock, 1);
#endif
- h = gethostbyname(name);
+ h = gethostbyname(name);
#endif /* HAVE_GETHOSTBYNAME_R */
- Py_END_ALLOW_THREADS
- /* Some C libraries would require addr.__ss_family instead of
- addr.ss_family.
- Therefore, we cast the sockaddr_storage into sockaddr to
- access sa_family. */
- sa = (struct sockaddr*)&addr;
- ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
- sa->sa_family);
+ Py_END_ALLOW_THREADS
+ /* Some C libraries would require addr.__ss_family instead of
+ addr.ss_family.
+ Therefore, we cast the sockaddr_storage into sockaddr to
+ access sa_family. */
+ sa = (struct sockaddr*)&addr;
+ ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),
+ sa->sa_family);
#ifdef USE_GETHOSTBYNAME_LOCK
- PyThread_release_lock(netdb_lock);
+ PyThread_release_lock(netdb_lock);
#endif
- return ret;
+ return ret;
}
PyDoc_STRVAR(ghbn_ex_doc,
@@ -3169,84 +3169,84 @@ static PyObject *
socket_gethostbyaddr(PyObject *self, PyObject *args)
{
#ifdef ENABLE_IPV6
- struct sockaddr_storage addr;
+ struct sockaddr_storage addr;
#else
- struct sockaddr_in addr;
+ struct sockaddr_in addr;
#endif
- struct sockaddr *sa = (struct sockaddr *)&addr;
- char *ip_num;
- struct hostent *h;
- PyObject *ret;
+ struct sockaddr *sa = (struct sockaddr *)&addr;
+ char *ip_num;
+ struct hostent *h;
+ PyObject *ret;
#ifdef HAVE_GETHOSTBYNAME_R
- struct hostent hp_allocated;
+ struct hostent hp_allocated;
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
- struct hostent_data data;
+ struct hostent_data data;
#else
- /* glibcs up to 2.10 assume that the buf argument to
- gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
- does not ensure. The attribute below instructs the compiler
- to maintain this alignment. */
- char buf[16384] Py_ALIGNED(8);
- int buf_len = (sizeof buf) - 1;
- int errnop;
+ /* glibcs up to 2.10 assume that the buf argument to
+ gethostbyaddr_r is 8-byte aligned, which at least llvm-gcc
+ does not ensure. The attribute below instructs the compiler
+ to maintain this alignment. */
+ char buf[16384] Py_ALIGNED(8);
+ int buf_len = (sizeof buf) - 1;
+ int errnop;
#endif
#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)
- int result;
+ int result;
#endif
#endif /* HAVE_GETHOSTBYNAME_R */
- char *ap;
- int al;
- int af;
-
- if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
- return NULL;
- af = AF_UNSPEC;
- if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
- return NULL;
- af = sa->sa_family;
- ap = NULL;
- al = 0;
- switch (af) {
- case AF_INET:
- ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
- al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
- break;
+ char *ap;
+ int al;
+ int af;
+
+ if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
+ return NULL;
+ af = AF_UNSPEC;
+ if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
+ return NULL;
+ af = sa->sa_family;
+ ap = NULL;
+ al = 0;
+ switch (af) {
+ case AF_INET:
+ ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;
+ al = sizeof(((struct sockaddr_in *)sa)->sin_addr);
+ break;
#ifdef ENABLE_IPV6
- case AF_INET6:
- ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
- al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
- break;
-#endif
- default:
- PyErr_SetString(socket_error, "unsupported address family");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
+ case AF_INET6:
+ ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;
+ al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
+ break;
+#endif
+ default:
+ PyErr_SetString(socket_error, "unsupported address family");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
#ifdef HAVE_GETHOSTBYNAME_R
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
- result = gethostbyaddr_r(ap, al, af,
- &hp_allocated, buf, buf_len,
- &h, &errnop);
+ result = gethostbyaddr_r(ap, al, af,
+ &hp_allocated, buf, buf_len,
+ &h, &errnop);
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
- h = gethostbyaddr_r(ap, al, af,
- &hp_allocated, buf, buf_len, &errnop);
+ h = gethostbyaddr_r(ap, al, af,
+ &hp_allocated, buf, buf_len, &errnop);
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
- memset((void *) &data, '\0', sizeof(data));
- result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
- h = (result != 0) ? NULL : &hp_allocated;
+ memset((void *) &data, '\0', sizeof(data));
+ result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
+ h = (result != 0) ? NULL : &hp_allocated;
#endif
#else /* not HAVE_GETHOSTBYNAME_R */
#ifdef USE_GETHOSTBYNAME_LOCK
- PyThread_acquire_lock(netdb_lock, 1);
+ PyThread_acquire_lock(netdb_lock, 1);
#endif
- h = gethostbyaddr(ap, al, af);
+ h = gethostbyaddr(ap, al, af);
#endif /* HAVE_GETHOSTBYNAME_R */
- Py_END_ALLOW_THREADS
- ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
+ Py_END_ALLOW_THREADS
+ ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);
#ifdef USE_GETHOSTBYNAME_LOCK
- PyThread_release_lock(netdb_lock);
+ PyThread_release_lock(netdb_lock);
#endif
- return ret;
+ return ret;
}
PyDoc_STRVAR(gethostbyaddr_doc,
@@ -3264,18 +3264,18 @@ for a host. The host argument is a string giving a host name or IP number.");
static PyObject *
socket_getservbyname(PyObject *self, PyObject *args)
{
- char *name, *proto=NULL;
- struct servent *sp;
- if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- sp = getservbyname(name, proto);
- Py_END_ALLOW_THREADS
- if (sp == NULL) {
- PyErr_SetString(socket_error, "service/proto not found");
- return NULL;
- }
- return PyLong_FromLong((long) ntohs(sp->s_port));
+ char *name, *proto=NULL;
+ struct servent *sp;
+ if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ sp = getservbyname(name, proto);
+ Py_END_ALLOW_THREADS
+ if (sp == NULL) {
+ PyErr_SetString(socket_error, "service/proto not found");
+ return NULL;
+ }
+ return PyLong_FromLong((long) ntohs(sp->s_port));
}
PyDoc_STRVAR(getservbyname_doc,
@@ -3294,25 +3294,25 @@ otherwise any protocol will match.");
static PyObject *
socket_getservbyport(PyObject *self, PyObject *args)
{
- int port;
- char *proto=NULL;
- struct servent *sp;
- if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
- return NULL;
- if (port < 0 || port > 0xffff) {
- PyErr_SetString(
- PyExc_OverflowError,
- "getservbyport: port must be 0-65535.");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- sp = getservbyport(htons((short)port), proto);
- Py_END_ALLOW_THREADS
- if (sp == NULL) {
- PyErr_SetString(socket_error, "port/proto not found");
- return NULL;
- }
- return PyUnicode_FromString(sp->s_name);
+ int port;
+ char *proto=NULL;
+ struct servent *sp;
+ if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto))
+ return NULL;
+ if (port < 0 || port > 0xffff) {
+ PyErr_SetString(
+ PyExc_OverflowError,
+ "getservbyport: port must be 0-65535.");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ sp = getservbyport(htons((short)port), proto);
+ Py_END_ALLOW_THREADS
+ if (sp == NULL) {
+ PyErr_SetString(socket_error, "port/proto not found");
+ return NULL;
+ }
+ return PyUnicode_FromString(sp->s_name);
}
PyDoc_STRVAR(getservbyport_doc,
@@ -3330,18 +3330,18 @@ otherwise any protocol will match.");
static PyObject *
socket_getprotobyname(PyObject *self, PyObject *args)
{
- char *name;
- struct protoent *sp;
- if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- sp = getprotobyname(name);
- Py_END_ALLOW_THREADS
- if (sp == NULL) {
- PyErr_SetString(socket_error, "protocol not found");
- return NULL;
- }
- return PyLong_FromLong((long) sp->p_proto);
+ char *name;
+ struct protoent *sp;
+ if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ sp = getprotobyname(name);
+ Py_END_ALLOW_THREADS
+ if (sp == NULL) {
+ PyErr_SetString(socket_error, "protocol not found");
+ return NULL;
+ }
+ return PyLong_FromLong((long) sp->p_proto);
}
PyDoc_STRVAR(getprotobyname_doc,
@@ -3356,22 +3356,22 @@ Return the protocol number for the named protocol. (Rarely used.)");
static PyObject *
socket_dup(PyObject *self, PyObject *fdobj)
{
- SOCKET_T fd, newfd;
- PyObject *newfdobj;
+ SOCKET_T fd, newfd;
+ PyObject *newfdobj;
- fd = PyLong_AsSocket_t(fdobj);
- if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
- return NULL;
+ fd = PyLong_AsSocket_t(fdobj);
+ if (fd == (SOCKET_T)(-1) && PyErr_Occurred())
+ return NULL;
- newfd = dup_socket(fd);
- if (newfd == INVALID_SOCKET)
- return set_error();
+ newfd = dup_socket(fd);
+ if (newfd == INVALID_SOCKET)
+ return set_error();
- newfdobj = PyLong_FromSocket_t(newfd);
- if (newfdobj == NULL)
- SOCKETCLOSE(newfd);
- return newfdobj;
+ newfdobj = PyLong_FromSocket_t(newfd);
+ if (newfdobj == NULL)
+ SOCKETCLOSE(newfd);
+ return newfdobj;
}
PyDoc_STRVAR(dup_doc,
@@ -3391,40 +3391,40 @@ sockets; on some platforms os.dup() won't work for socket file descriptors.");
static PyObject *
socket_socketpair(PyObject *self, PyObject *args)
{
- PySocketSockObject *s0 = NULL, *s1 = NULL;
- SOCKET_T sv[2];
- int family, type = SOCK_STREAM, proto = 0;
- PyObject *res = NULL;
+ PySocketSockObject *s0 = NULL, *s1 = NULL;
+ SOCKET_T sv[2];
+ int family, type = SOCK_STREAM, proto = 0;
+ PyObject *res = NULL;
#if defined(AF_UNIX)
- family = AF_UNIX;
+ family = AF_UNIX;
#else
- family = AF_INET;
-#endif
- if (!PyArg_ParseTuple(args, "|iii:socketpair",
- &family, &type, &proto))
- return NULL;
- /* Create a pair of socket fds */
- if (socketpair(family, type, proto, sv) < 0)
- return set_error();
- s0 = new_sockobject(sv[0], family, type, proto);
- if (s0 == NULL)
- goto finally;
- s1 = new_sockobject(sv[1], family, type, proto);
- if (s1 == NULL)
- goto finally;
- res = PyTuple_Pack(2, s0, s1);
+ family = AF_INET;
+#endif
+ if (!PyArg_ParseTuple(args, "|iii:socketpair",
+ &family, &type, &proto))
+ return NULL;
+ /* Create a pair of socket fds */
+ if (socketpair(family, type, proto, sv) < 0)
+ return set_error();
+ s0 = new_sockobject(sv[0], family, type, proto);
+ if (s0 == NULL)
+ goto finally;
+ s1 = new_sockobject(sv[1], family, type, proto);
+ if (s1 == NULL)
+ goto finally;
+ res = PyTuple_Pack(2, s0, s1);
finally:
- if (res == NULL) {
- if (s0 == NULL)
- SOCKETCLOSE(sv[0]);
- if (s1 == NULL)
- SOCKETCLOSE(sv[1]);
- }
- Py_XDECREF(s0);
- Py_XDECREF(s1);
- return res;
+ if (res == NULL) {
+ if (s0 == NULL)
+ SOCKETCLOSE(sv[0]);
+ if (s1 == NULL)
+ SOCKETCLOSE(sv[1]);
+ }
+ Py_XDECREF(s0);
+ Py_XDECREF(s1);
+ return res;
}
PyDoc_STRVAR(socketpair_doc,
@@ -3441,18 +3441,18 @@ AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
static PyObject *
socket_ntohs(PyObject *self, PyObject *args)
{
- int x1, x2;
-
- if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
- return NULL;
- }
- if (x1 < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "can't convert negative number to unsigned long");
- return NULL;
- }
- x2 = (unsigned int)ntohs((unsigned short)x1);
- return PyLong_FromLong(x2);
+ int x1, x2;
+
+ if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
+ return NULL;
+ }
+ if (x1 < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
+ x2 = (unsigned int)ntohs((unsigned short)x1);
+ return PyLong_FromLong(x2);
}
PyDoc_STRVAR(ntohs_doc,
@@ -3464,31 +3464,31 @@ Convert a 16-bit integer from network to host byte order.");
static PyObject *
socket_ntohl(PyObject *self, PyObject *arg)
{
- unsigned long x;
+ unsigned long x;
- if (PyLong_Check(arg)) {
- x = PyLong_AsUnsignedLong(arg);
- if (x == (unsigned long) -1 && PyErr_Occurred())
- return NULL;
+ if (PyLong_Check(arg)) {
+ x = PyLong_AsUnsignedLong(arg);
+ if (x == (unsigned long) -1 && PyErr_Occurred())
+ return NULL;
#if SIZEOF_LONG > 4
- {
- unsigned long y;
- /* only want the trailing 32 bits */
- y = x & 0xFFFFFFFFUL;
- if (y ^ x)
- return PyErr_Format(PyExc_OverflowError,
- "long int larger than 32 bits");
- x = y;
- }
-#endif
- }
- else
- return PyErr_Format(PyExc_TypeError,
- "expected int/long, %s found",
- Py_TYPE(arg)->tp_name);
- if (x == (unsigned long) -1 && PyErr_Occurred())
- return NULL;
- return PyLong_FromUnsignedLong(ntohl(x));
+ {
+ unsigned long y;
+ /* only want the trailing 32 bits */
+ y = x & 0xFFFFFFFFUL;
+ if (y ^ x)
+ return PyErr_Format(PyExc_OverflowError,
+ "long int larger than 32 bits");
+ x = y;
+ }
+#endif
+ }
+ else
+ return PyErr_Format(PyExc_TypeError,
+ "expected int/long, %s found",
+ Py_TYPE(arg)->tp_name);
+ if (x == (unsigned long) -1 && PyErr_Occurred())
+ return NULL;
+ return PyLong_FromUnsignedLong(ntohl(x));
}
PyDoc_STRVAR(ntohl_doc,
@@ -3500,18 +3500,18 @@ Convert a 32-bit integer from network to host byte order.");
static PyObject *
socket_htons(PyObject *self, PyObject *args)
{
- int x1, x2;
-
- if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
- return NULL;
- }
- if (x1 < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "can't convert negative number to unsigned long");
- return NULL;
- }
- x2 = (unsigned int)htons((unsigned short)x1);
- return PyLong_FromLong(x2);
+ int x1, x2;
+
+ if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
+ return NULL;
+ }
+ if (x1 < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
+ x2 = (unsigned int)htons((unsigned short)x1);
+ return PyLong_FromLong(x2);
}
PyDoc_STRVAR(htons_doc,
@@ -3523,29 +3523,29 @@ Convert a 16-bit integer from host to network byte order.");
static PyObject *
socket_htonl(PyObject *self, PyObject *arg)
{
- unsigned long x;
+ unsigned long x;
- if (PyLong_Check(arg)) {
- x = PyLong_AsUnsignedLong(arg);
- if (x == (unsigned long) -1 && PyErr_Occurred())
- return NULL;
+ if (PyLong_Check(arg)) {
+ x = PyLong_AsUnsignedLong(arg);
+ if (x == (unsigned long) -1 && PyErr_Occurred())
+ return NULL;
#if SIZEOF_LONG > 4
- {
- unsigned long y;
- /* only want the trailing 32 bits */
- y = x & 0xFFFFFFFFUL;
- if (y ^ x)
- return PyErr_Format(PyExc_OverflowError,
- "long int larger than 32 bits");
- x = y;
- }
-#endif
- }
- else
- return PyErr_Format(PyExc_TypeError,
- "expected int/long, %s found",
- Py_TYPE(arg)->tp_name);
- return PyLong_FromUnsignedLong(htonl((unsigned long)x));
+ {
+ unsigned long y;
+ /* only want the trailing 32 bits */
+ y = x & 0xFFFFFFFFUL;
+ if (y ^ x)
+ return PyErr_Format(PyExc_OverflowError,
+ "long int larger than 32 bits");
+ x = y;
+ }
+#endif
+ }
+ else
+ return PyErr_Format(PyExc_TypeError,
+ "expected int/long, %s found",
+ Py_TYPE(arg)->tp_name);
+ return PyLong_FromUnsignedLong(htonl((unsigned long)x));
}
PyDoc_STRVAR(htonl_doc,
@@ -3568,20 +3568,20 @@ socket_inet_aton(PyObject *self, PyObject *args)
#define INADDR_NONE (-1)
#endif
#ifdef HAVE_INET_ATON
- struct in_addr buf;
+ struct in_addr buf;
#endif
#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
#if (SIZEOF_INT != 4)
#error "Not sure if in_addr_t exists and int is not 32-bits."
#endif
- /* Have to use inet_addr() instead */
- unsigned int packed_addr;
+ /* Have to use inet_addr() instead */
+ unsigned int packed_addr;
#endif
- char *ip_addr;
+ char *ip_addr;
- if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr))
+ return NULL;
#ifdef HAVE_INET_ATON
@@ -3589,13 +3589,13 @@ socket_inet_aton(PyObject *self, PyObject *args)
#ifdef USE_INET_ATON_WEAKLINK
if (inet_aton != NULL) {
#endif
- if (inet_aton(ip_addr, &buf))
- return PyBytes_FromStringAndSize((char *)(&buf),
- sizeof(buf));
+ if (inet_aton(ip_addr, &buf))
+ return PyBytes_FromStringAndSize((char *)(&buf),
+ sizeof(buf));
- PyErr_SetString(socket_error,
- "illegal IP address string passed to inet_aton");
- return NULL;
+ PyErr_SetString(socket_error,
+ "illegal IP address string passed to inet_aton");
+ return NULL;
#ifdef USE_INET_ATON_WEAKLINK
} else {
@@ -3605,22 +3605,22 @@ socket_inet_aton(PyObject *self, PyObject *args)
#if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK)
- /* special-case this address as inet_addr might return INADDR_NONE
- * for this */
- if (strcmp(ip_addr, "255.255.255.255") == 0) {
- packed_addr = 0xFFFFFFFF;
- } else {
+ /* special-case this address as inet_addr might return INADDR_NONE
+ * for this */
+ if (strcmp(ip_addr, "255.255.255.255") == 0) {
+ packed_addr = 0xFFFFFFFF;
+ } else {
- packed_addr = inet_addr(ip_addr);
+ packed_addr = inet_addr(ip_addr);
- if (packed_addr == INADDR_NONE) { /* invalid address */
- PyErr_SetString(socket_error,
- "illegal IP address string passed to inet_aton");
- return NULL;
- }
- }
- return PyBytes_FromStringAndSize((char *) &packed_addr,
- sizeof(packed_addr));
+ if (packed_addr == INADDR_NONE) { /* invalid address */
+ PyErr_SetString(socket_error,
+ "illegal IP address string passed to inet_aton");
+ return NULL;
+ }
+ }
+ return PyBytes_FromStringAndSize((char *) &packed_addr,
+ sizeof(packed_addr));
#ifdef USE_INET_ATON_WEAKLINK
}
@@ -3637,23 +3637,23 @@ Convert an IP address from 32-bit packed binary format to string format");
static PyObject*
socket_inet_ntoa(PyObject *self, PyObject *args)
{
- char *packed_str;
- int addr_len;
- struct in_addr packed_addr;
+ char *packed_str;
+ int addr_len;
+ struct in_addr packed_addr;
- if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "y#:inet_ntoa", &packed_str, &addr_len)) {
+ return NULL;
+ }
- if (addr_len != sizeof(packed_addr)) {
- PyErr_SetString(socket_error,
- "packed IP wrong length for inet_ntoa");
- return NULL;
- }
+ if (addr_len != sizeof(packed_addr)) {
+ PyErr_SetString(socket_error,
+ "packed IP wrong length for inet_ntoa");
+ return NULL;
+ }
- memcpy(&packed_addr, packed_str, addr_len);
+ memcpy(&packed_addr, packed_str, addr_len);
- return PyUnicode_FromString(inet_ntoa(packed_addr));
+ return PyUnicode_FromString(inet_ntoa(packed_addr));
}
#ifdef HAVE_INET_PTON
@@ -3667,46 +3667,46 @@ for use with low-level network functions.");
static PyObject *
socket_inet_pton(PyObject *self, PyObject *args)
{
- int af;
- char* ip;
- int retval;
+ int af;
+ char* ip;
+ int retval;
#ifdef ENABLE_IPV6
- char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
+ char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];
#else
- char packed[sizeof(struct in_addr)];
+ char packed[sizeof(struct in_addr)];
#endif
- if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) {
+ return NULL;
+ }
#if !defined(ENABLE_IPV6) && defined(AF_INET6)
- if(af == AF_INET6) {
- PyErr_SetString(socket_error,
- "can't use AF_INET6, IPv6 is disabled");
- return NULL;
- }
-#endif
-
- retval = inet_pton(af, ip, packed);
- if (retval < 0) {
- PyErr_SetFromErrno(socket_error);
- return NULL;
- } else if (retval == 0) {
- PyErr_SetString(socket_error,
- "illegal IP address string passed to inet_pton");
- return NULL;
- } else if (af == AF_INET) {
- return PyBytes_FromStringAndSize(packed,
- sizeof(struct in_addr));
+ if(af == AF_INET6) {
+ PyErr_SetString(socket_error,
+ "can't use AF_INET6, IPv6 is disabled");
+ return NULL;
+ }
+#endif
+
+ retval = inet_pton(af, ip, packed);
+ if (retval < 0) {
+ PyErr_SetFromErrno(socket_error);
+ return NULL;
+ } else if (retval == 0) {
+ PyErr_SetString(socket_error,
+ "illegal IP address string passed to inet_pton");
+ return NULL;
+ } else if (af == AF_INET) {
+ return PyBytes_FromStringAndSize(packed,
+ sizeof(struct in_addr));
#ifdef ENABLE_IPV6
- } else if (af == AF_INET6) {
- return PyBytes_FromStringAndSize(packed,
- sizeof(struct in6_addr));
-#endif
- } else {
- PyErr_SetString(socket_error, "unknown address family");
- return NULL;
- }
+ } else if (af == AF_INET6) {
+ return PyBytes_FromStringAndSize(packed,
+ sizeof(struct in6_addr));
+#endif
+ } else {
+ PyErr_SetString(socket_error, "unknown address family");
+ return NULL;
+ }
}
PyDoc_STRVAR(inet_ntop_doc,
@@ -3717,54 +3717,54 @@ Convert a packed IP address of the given family to string format.");
static PyObject *
socket_inet_ntop(PyObject *self, PyObject *args)
{
- int af;
- char* packed;
- int len;
- const char* retval;
+ int af;
+ char* packed;
+ int len;
+ const char* retval;
#ifdef ENABLE_IPV6
- char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
+ char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];
#else
- char ip[INET_ADDRSTRLEN + 1];
+ char ip[INET_ADDRSTRLEN + 1];
#endif
- /* Guarantee NUL-termination for PyUnicode_FromString() below */
- memset((void *) &ip[0], '\0', sizeof(ip));
+ /* Guarantee NUL-termination for PyUnicode_FromString() below */
+ memset((void *) &ip[0], '\0', sizeof(ip));
- if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "iy#:inet_ntop", &af, &packed, &len)) {
+ return NULL;
+ }
- if (af == AF_INET) {
- if (len != sizeof(struct in_addr)) {
- PyErr_SetString(PyExc_ValueError,
- "invalid length of packed IP address string");
- return NULL;
- }
+ if (af == AF_INET) {
+ if (len != sizeof(struct in_addr)) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid length of packed IP address string");
+ return NULL;
+ }
#ifdef ENABLE_IPV6
- } else if (af == AF_INET6) {
- if (len != sizeof(struct in6_addr)) {
- PyErr_SetString(PyExc_ValueError,
- "invalid length of packed IP address string");
- return NULL;
- }
-#endif
- } else {
- PyErr_Format(PyExc_ValueError,
- "unknown address family %d", af);
- return NULL;
- }
-
- retval = inet_ntop(af, packed, ip, sizeof(ip));
- if (!retval) {
- PyErr_SetFromErrno(socket_error);
- return NULL;
- } else {
- return PyUnicode_FromString(retval);
- }
-
- /* NOTREACHED */
- PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
- return NULL;
+ } else if (af == AF_INET6) {
+ if (len != sizeof(struct in6_addr)) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid length of packed IP address string");
+ return NULL;
+ }
+#endif
+ } else {
+ PyErr_Format(PyExc_ValueError,
+ "unknown address family %d", af);
+ return NULL;
+ }
+
+ retval = inet_ntop(af, packed, ip, sizeof(ip));
+ if (!retval) {
+ PyErr_SetFromErrno(socket_error);
+ return NULL;
+ } else {
+ return PyUnicode_FromString(retval);
+ }
+
+ /* NOTREACHED */
+ PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
+ return NULL;
}
#endif /* HAVE_INET_PTON */
@@ -3775,100 +3775,100 @@ socket_inet_ntop(PyObject *self, PyObject *args)
static PyObject *
socket_getaddrinfo(PyObject *self, PyObject *args)
{
- struct addrinfo hints, *res;
- struct addrinfo *res0 = NULL;
- PyObject *hobj = NULL;
- PyObject *pobj = (PyObject *)NULL;
- char pbuf[30];
- char *hptr, *pptr;
- int family, socktype, protocol, flags;
- int error;
- PyObject *all = (PyObject *)NULL;
- PyObject *idna = NULL;
-
- family = socktype = protocol = flags = 0;
- family = AF_UNSPEC;
- if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
- &hobj, &pobj, &family, &socktype,
- &protocol, &flags)) {
- return NULL;
- }
- if (hobj == Py_None) {
- hptr = NULL;
- } else if (PyUnicode_Check(hobj)) {
- idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
- if (!idna)
- return NULL;
- assert(PyBytes_Check(idna));
- hptr = PyBytes_AS_STRING(idna);
- } else if (PyBytes_Check(hobj)) {
- hptr = PyBytes_AsString(hobj);
- } else {
- PyErr_SetString(PyExc_TypeError,
- "getaddrinfo() argument 1 must be string or None");
- return NULL;
- }
- if (PyLong_CheckExact(pobj)) {
- long value = PyLong_AsLong(pobj);
- if (value == -1 && PyErr_Occurred())
- goto err;
- PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
- pptr = pbuf;
- } else if (PyUnicode_Check(pobj)) {
- pptr = _PyUnicode_AsString(pobj);
- } else if (PyBytes_Check(pobj)) {
- pptr = PyBytes_AsString(pobj);
- } else if (pobj == Py_None) {
- pptr = (char *)NULL;
- } else {
- PyErr_SetString(socket_error, "Int or String expected");
- goto err;
- }
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = family;
- hints.ai_socktype = socktype;
- hints.ai_protocol = protocol;
- hints.ai_flags = flags;
- Py_BEGIN_ALLOW_THREADS
- ACQUIRE_GETADDRINFO_LOCK
- error = getaddrinfo(hptr, pptr, &hints, &res0);
- Py_END_ALLOW_THREADS
- RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
- if (error) {
- set_gaierror(error);
- goto err;
- }
-
- if ((all = PyList_New(0)) == NULL)
- goto err;
- for (res = res0; res; res = res->ai_next) {
- PyObject *single;
- PyObject *addr =
- makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
- if (addr == NULL)
- goto err;
- single = Py_BuildValue("iiisO", res->ai_family,
- res->ai_socktype, res->ai_protocol,
- res->ai_canonname ? res->ai_canonname : "",
- addr);
- Py_DECREF(addr);
- if (single == NULL)
- goto err;
-
- if (PyList_Append(all, single))
- goto err;
- Py_XDECREF(single);
- }
- Py_XDECREF(idna);
- if (res0)
- freeaddrinfo(res0);
- return all;
+ struct addrinfo hints, *res;
+ struct addrinfo *res0 = NULL;
+ PyObject *hobj = NULL;
+ PyObject *pobj = (PyObject *)NULL;
+ char pbuf[30];
+ char *hptr, *pptr;
+ int family, socktype, protocol, flags;
+ int error;
+ PyObject *all = (PyObject *)NULL;
+ PyObject *idna = NULL;
+
+ family = socktype = protocol = flags = 0;
+ family = AF_UNSPEC;
+ if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo",
+ &hobj, &pobj, &family, &socktype,
+ &protocol, &flags)) {
+ return NULL;
+ }
+ if (hobj == Py_None) {
+ hptr = NULL;
+ } else if (PyUnicode_Check(hobj)) {
+ idna = PyObject_CallMethod(hobj, "encode", "s", "idna");
+ if (!idna)
+ return NULL;
+ assert(PyBytes_Check(idna));
+ hptr = PyBytes_AS_STRING(idna);
+ } else if (PyBytes_Check(hobj)) {
+ hptr = PyBytes_AsString(hobj);
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "getaddrinfo() argument 1 must be string or None");
+ return NULL;
+ }
+ if (PyLong_CheckExact(pobj)) {
+ long value = PyLong_AsLong(pobj);
+ if (value == -1 && PyErr_Occurred())
+ goto err;
+ PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value);
+ pptr = pbuf;
+ } else if (PyUnicode_Check(pobj)) {
+ pptr = _PyUnicode_AsString(pobj);
+ } else if (PyBytes_Check(pobj)) {
+ pptr = PyBytes_AsString(pobj);
+ } else if (pobj == Py_None) {
+ pptr = (char *)NULL;
+ } else {
+ PyErr_SetString(socket_error, "Int or String expected");
+ goto err;
+ }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_socktype = socktype;
+ hints.ai_protocol = protocol;
+ hints.ai_flags = flags;
+ Py_BEGIN_ALLOW_THREADS
+ ACQUIRE_GETADDRINFO_LOCK
+ error = getaddrinfo(hptr, pptr, &hints, &res0);
+ Py_END_ALLOW_THREADS
+ RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
+ if (error) {
+ set_gaierror(error);
+ goto err;
+ }
+
+ if ((all = PyList_New(0)) == NULL)
+ goto err;
+ for (res = res0; res; res = res->ai_next) {
+ PyObject *single;
+ PyObject *addr =
+ makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol);
+ if (addr == NULL)
+ goto err;
+ single = Py_BuildValue("iiisO", res->ai_family,
+ res->ai_socktype, res->ai_protocol,
+ res->ai_canonname ? res->ai_canonname : "",
+ addr);
+ Py_DECREF(addr);
+ if (single == NULL)
+ goto err;
+
+ if (PyList_Append(all, single))
+ goto err;
+ Py_XDECREF(single);
+ }
+ Py_XDECREF(idna);
+ if (res0)
+ freeaddrinfo(res0);
+ return all;
err:
- Py_XDECREF(all);
- Py_XDECREF(idna);
- if (res0)
- freeaddrinfo(res0);
- return (PyObject *)NULL;
+ Py_XDECREF(all);
+ Py_XDECREF(idna);
+ if (res0)
+ freeaddrinfo(res0);
+ return (PyObject *)NULL;
}
PyDoc_STRVAR(getaddrinfo_doc,
@@ -3883,77 +3883,77 @@ Resolve host and port into addrinfo struct.");
static PyObject *
socket_getnameinfo(PyObject *self, PyObject *args)
{
- PyObject *sa = (PyObject *)NULL;
- int flags;
- char *hostp;
- int port, flowinfo, scope_id;
- char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
- struct addrinfo hints, *res = NULL;
- int error;
- PyObject *ret = (PyObject *)NULL;
-
- flags = flowinfo = scope_id = 0;
- if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
- return NULL;
- if (!PyTuple_Check(sa)) {
- PyErr_SetString(PyExc_TypeError,
- "getnameinfo() argument 1 must be a tuple");
- return NULL;
- }
- if (!PyArg_ParseTuple(sa, "si|ii",
- &hostp, &port, &flowinfo, &scope_id))
- return NULL;
- PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
- Py_BEGIN_ALLOW_THREADS
- ACQUIRE_GETADDRINFO_LOCK
- error = getaddrinfo(hostp, pbuf, &hints, &res);
- Py_END_ALLOW_THREADS
- RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
- if (error) {
- set_gaierror(error);
- goto fail;
- }
- if (res->ai_next) {
- PyErr_SetString(socket_error,
- "sockaddr resolved to multiple addresses");
- goto fail;
- }
- switch (res->ai_family) {
- case AF_INET:
- {
- if (PyTuple_GET_SIZE(sa) != 2) {
- PyErr_SetString(socket_error,
- "IPv4 sockaddr must be 2 tuple");
- goto fail;
- }
- break;
- }
+ PyObject *sa = (PyObject *)NULL;
+ int flags;
+ char *hostp;
+ int port, flowinfo, scope_id;
+ char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
+ struct addrinfo hints, *res = NULL;
+ int error;
+ PyObject *ret = (PyObject *)NULL;
+
+ flags = flowinfo = scope_id = 0;
+ if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
+ return NULL;
+ if (!PyTuple_Check(sa)) {
+ PyErr_SetString(PyExc_TypeError,
+ "getnameinfo() argument 1 must be a tuple");
+ return NULL;
+ }
+ if (!PyArg_ParseTuple(sa, "si|ii",
+ &hostp, &port, &flowinfo, &scope_id))
+ return NULL;
+ PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */
+ Py_BEGIN_ALLOW_THREADS
+ ACQUIRE_GETADDRINFO_LOCK
+ error = getaddrinfo(hostp, pbuf, &hints, &res);
+ Py_END_ALLOW_THREADS
+ RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */
+ if (error) {
+ set_gaierror(error);
+ goto fail;
+ }
+ if (res->ai_next) {
+ PyErr_SetString(socket_error,
+ "sockaddr resolved to multiple addresses");
+ goto fail;
+ }
+ switch (res->ai_family) {
+ case AF_INET:
+ {
+ if (PyTuple_GET_SIZE(sa) != 2) {
+ PyErr_SetString(socket_error,
+ "IPv4 sockaddr must be 2 tuple");
+ goto fail;
+ }
+ break;
+ }
#ifdef ENABLE_IPV6
- case AF_INET6:
- {
- struct sockaddr_in6 *sin6;
- sin6 = (struct sockaddr_in6 *)res->ai_addr;
- sin6->sin6_flowinfo = flowinfo;
- sin6->sin6_scope_id = scope_id;
- break;
- }
-#endif
- }
- error = getnameinfo(res->ai_addr, res->ai_addrlen,
- hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
- if (error) {
- set_gaierror(error);
- goto fail;
- }
- ret = Py_BuildValue("ss", hbuf, pbuf);
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *sin6;
+ sin6 = (struct sockaddr_in6 *)res->ai_addr;
+ sin6->sin6_flowinfo = flowinfo;
+ sin6->sin6_scope_id = scope_id;
+ break;
+ }
+#endif
+ }
+ error = getnameinfo(res->ai_addr, res->ai_addrlen,
+ hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags);
+ if (error) {
+ set_gaierror(error);
+ goto fail;
+ }
+ ret = Py_BuildValue("ss", hbuf, pbuf);
fail:
- if (res)
- freeaddrinfo(res);
- return ret;
+ if (res)
+ freeaddrinfo(res);
+ return ret;
}
PyDoc_STRVAR(getnameinfo_doc,
@@ -3967,12 +3967,12 @@ Get host and port for a sockaddr.");
static PyObject *
socket_getdefaulttimeout(PyObject *self)
{
- if (defaulttimeout < 0.0) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- else
- return PyFloat_FromDouble(defaulttimeout);
+ if (defaulttimeout < 0.0) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else
+ return PyFloat_FromDouble(defaulttimeout);
}
PyDoc_STRVAR(getdefaulttimeout_doc,
@@ -3985,24 +3985,24 @@ When the socket module is first imported, the default is None.");
static PyObject *
socket_setdefaulttimeout(PyObject *self, PyObject *arg)
{
- double timeout;
-
- if (arg == Py_None)
- timeout = -1.0;
- else {
- timeout = PyFloat_AsDouble(arg);
- if (timeout < 0.0) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_ValueError,
- "Timeout value out of range");
- return NULL;
- }
- }
-
- defaulttimeout = timeout;
-
- Py_INCREF(Py_None);
- return Py_None;
+ double timeout;
+
+ if (arg == Py_None)
+ timeout = -1.0;
+ else {
+ timeout = PyFloat_AsDouble(arg);
+ if (timeout < 0.0) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_ValueError,
+ "Timeout value out of range");
+ return NULL;
+ }
+ }
+
+ defaulttimeout = timeout;
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setdefaulttimeout_doc,
@@ -4016,55 +4016,55 @@ When the socket module is first imported, the default is None.");
/* List of functions exported by this module. */
static PyMethodDef socket_methods[] = {
- {"gethostbyname", socket_gethostbyname,
- METH_VARARGS, gethostbyname_doc},
- {"gethostbyname_ex", socket_gethostbyname_ex,
- METH_VARARGS, ghbn_ex_doc},
- {"gethostbyaddr", socket_gethostbyaddr,
- METH_VARARGS, gethostbyaddr_doc},
- {"gethostname", socket_gethostname,
- METH_NOARGS, gethostname_doc},
- {"getservbyname", socket_getservbyname,
- METH_VARARGS, getservbyname_doc},
- {"getservbyport", socket_getservbyport,
- METH_VARARGS, getservbyport_doc},
- {"getprotobyname", socket_getprotobyname,
- METH_VARARGS, getprotobyname_doc},
+ {"gethostbyname", socket_gethostbyname,
+ METH_VARARGS, gethostbyname_doc},
+ {"gethostbyname_ex", socket_gethostbyname_ex,
+ METH_VARARGS, ghbn_ex_doc},
+ {"gethostbyaddr", socket_gethostbyaddr,
+ METH_VARARGS, gethostbyaddr_doc},
+ {"gethostname", socket_gethostname,
+ METH_NOARGS, gethostname_doc},
+ {"getservbyname", socket_getservbyname,
+ METH_VARARGS, getservbyname_doc},
+ {"getservbyport", socket_getservbyport,
+ METH_VARARGS, getservbyport_doc},
+ {"getprotobyname", socket_getprotobyname,
+ METH_VARARGS, getprotobyname_doc},
#ifndef NO_DUP
- {"dup", socket_dup,
- METH_O, dup_doc},
+ {"dup", socket_dup,
+ METH_O, dup_doc},
#endif
#ifdef HAVE_SOCKETPAIR
- {"socketpair", socket_socketpair,
- METH_VARARGS, socketpair_doc},
-#endif
- {"ntohs", socket_ntohs,
- METH_VARARGS, ntohs_doc},
- {"ntohl", socket_ntohl,
- METH_O, ntohl_doc},
- {"htons", socket_htons,
- METH_VARARGS, htons_doc},
- {"htonl", socket_htonl,
- METH_O, htonl_doc},
- {"inet_aton", socket_inet_aton,
- METH_VARARGS, inet_aton_doc},
- {"inet_ntoa", socket_inet_ntoa,
- METH_VARARGS, inet_ntoa_doc},
+ {"socketpair", socket_socketpair,
+ METH_VARARGS, socketpair_doc},
+#endif
+ {"ntohs", socket_ntohs,
+ METH_VARARGS, ntohs_doc},
+ {"ntohl", socket_ntohl,
+ METH_O, ntohl_doc},
+ {"htons", socket_htons,
+ METH_VARARGS, htons_doc},
+ {"htonl", socket_htonl,
+ METH_O, htonl_doc},
+ {"inet_aton", socket_inet_aton,
+ METH_VARARGS, inet_aton_doc},
+ {"inet_ntoa", socket_inet_ntoa,
+ METH_VARARGS, inet_ntoa_doc},
#ifdef HAVE_INET_PTON
- {"inet_pton", socket_inet_pton,
- METH_VARARGS, inet_pton_doc},
- {"inet_ntop", socket_inet_ntop,
- METH_VARARGS, inet_ntop_doc},
-#endif
- {"getaddrinfo", socket_getaddrinfo,
- METH_VARARGS, getaddrinfo_doc},
- {"getnameinfo", socket_getnameinfo,
- METH_VARARGS, getnameinfo_doc},
- {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
- METH_NOARGS, getdefaulttimeout_doc},
- {"setdefaulttimeout", socket_setdefaulttimeout,
- METH_O, setdefaulttimeout_doc},
- {NULL, NULL} /* Sentinel */
+ {"inet_pton", socket_inet_pton,
+ METH_VARARGS, inet_pton_doc},
+ {"inet_ntop", socket_inet_ntop,
+ METH_VARARGS, inet_ntop_doc},
+#endif
+ {"getaddrinfo", socket_getaddrinfo,
+ METH_VARARGS, getaddrinfo_doc},
+ {"getnameinfo", socket_getnameinfo,
+ METH_VARARGS, getnameinfo_doc},
+ {"getdefaulttimeout", (PyCFunction)socket_getdefaulttimeout,
+ METH_NOARGS, getdefaulttimeout_doc},
+ {"setdefaulttimeout", socket_setdefaulttimeout,
+ METH_O, setdefaulttimeout_doc},
+ {NULL, NULL} /* Sentinel */
};
@@ -4076,34 +4076,34 @@ static PyMethodDef socket_methods[] = {
static void
os_cleanup(void)
{
- WSACleanup();
+ WSACleanup();
}
static int
os_init(void)
{
- WSADATA WSAData;
- int ret;
- ret = WSAStartup(0x0101, &WSAData);
- switch (ret) {
- case 0: /* No error */
- Py_AtExit(os_cleanup);
- return 1; /* Success */
- case WSASYSNOTREADY:
- PyErr_SetString(PyExc_ImportError,
- "WSAStartup failed: network not ready");
- break;
- case WSAVERNOTSUPPORTED:
- case WSAEINVAL:
- PyErr_SetString(
- PyExc_ImportError,
- "WSAStartup failed: requested version not supported");
- break;
- default:
- PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
- break;
- }
- return 0; /* Failure */
+ WSADATA WSAData;
+ int ret;
+ ret = WSAStartup(0x0101, &WSAData);
+ switch (ret) {
+ case 0: /* No error */
+ Py_AtExit(os_cleanup);
+ return 1; /* Success */
+ case WSASYSNOTREADY:
+ PyErr_SetString(PyExc_ImportError,
+ "WSAStartup failed: network not ready");
+ break;
+ case WSAVERNOTSUPPORTED:
+ case WSAEINVAL:
+ PyErr_SetString(
+ PyExc_ImportError,
+ "WSAStartup failed: requested version not supported");
+ break;
+ default:
+ PyErr_Format(PyExc_ImportError, "WSAStartup failed: error code %d", ret);
+ break;
+ }
+ return 0; /* Failure */
}
#endif /* MS_WINDOWS */
@@ -4118,18 +4118,18 @@ static int
os_init(void)
{
#ifndef PYCC_GCC
- int rc = sock_init();
+ int rc = sock_init();
- if (rc == 0) {
- return 1; /* Success */
- }
+ if (rc == 0) {
+ return 1; /* Success */
+ }
- PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
+ PyErr_Format(PyExc_ImportError, "OS/2 TCP/IP Error# %d", sock_errno());
- return 0; /* Failure */
+ return 0; /* Failure */
#else
- /* No need to initialise sockets with GCC/EMX */
- return 1; /* Success */
+ /* No need to initialise sockets with GCC/EMX */
+ return 1; /* Success */
#endif
}
@@ -4140,7 +4140,7 @@ os_init(void)
static int
os_init(void)
{
- return 1; /* Success */
+ return 1; /* Success */
}
#endif
@@ -4150,8 +4150,8 @@ os_init(void)
static
PySocketModule_APIObject PySocketModuleAPI =
{
- &sock_type,
- NULL
+ &sock_type,
+ NULL
};
@@ -4171,923 +4171,923 @@ PyDoc_STRVAR(socket_doc,
See the socket module for documentation.");
static struct PyModuleDef socketmodule = {
- PyModuleDef_HEAD_INIT,
- PySocket_MODULE_NAME,
- socket_doc,
- -1,
- socket_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ PySocket_MODULE_NAME,
+ socket_doc,
+ -1,
+ socket_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__socket(void)
{
- PyObject *m, *has_ipv6;
-
- if (!os_init())
- return NULL;
-
- Py_TYPE(&sock_type) = &PyType_Type;
- m = PyModule_Create(&socketmodule);
- if (m == NULL)
- return NULL;
-
- socket_error = PyErr_NewException("socket.error",
- PyExc_IOError, NULL);
- if (socket_error == NULL)
- return NULL;
- PySocketModuleAPI.error = socket_error;
- Py_INCREF(socket_error);
- PyModule_AddObject(m, "error", socket_error);
- socket_herror = PyErr_NewException("socket.herror",
- socket_error, NULL);
- if (socket_herror == NULL)
- return NULL;
- Py_INCREF(socket_herror);
- PyModule_AddObject(m, "herror", socket_herror);
- socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
- NULL);
- if (socket_gaierror == NULL)
- return NULL;
- Py_INCREF(socket_gaierror);
- PyModule_AddObject(m, "gaierror", socket_gaierror);
- socket_timeout = PyErr_NewException("socket.timeout",
- socket_error, NULL);
- if (socket_timeout == NULL)
- return NULL;
- Py_INCREF(socket_timeout);
- PyModule_AddObject(m, "timeout", socket_timeout);
- Py_INCREF((PyObject *)&sock_type);
- if (PyModule_AddObject(m, "SocketType",
- (PyObject *)&sock_type) != 0)
- return NULL;
- Py_INCREF((PyObject *)&sock_type);
- if (PyModule_AddObject(m, "socket",
- (PyObject *)&sock_type) != 0)
- return NULL;
+ PyObject *m, *has_ipv6;
+
+ if (!os_init())
+ return NULL;
+
+ Py_TYPE(&sock_type) = &PyType_Type;
+ m = PyModule_Create(&socketmodule);
+ if (m == NULL)
+ return NULL;
+
+ socket_error = PyErr_NewException("socket.error",
+ PyExc_IOError, NULL);
+ if (socket_error == NULL)
+ return NULL;
+ PySocketModuleAPI.error = socket_error;
+ Py_INCREF(socket_error);
+ PyModule_AddObject(m, "error", socket_error);
+ socket_herror = PyErr_NewException("socket.herror",
+ socket_error, NULL);
+ if (socket_herror == NULL)
+ return NULL;
+ Py_INCREF(socket_herror);
+ PyModule_AddObject(m, "herror", socket_herror);
+ socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,
+ NULL);
+ if (socket_gaierror == NULL)
+ return NULL;
+ Py_INCREF(socket_gaierror);
+ PyModule_AddObject(m, "gaierror", socket_gaierror);
+ socket_timeout = PyErr_NewException("socket.timeout",
+ socket_error, NULL);
+ if (socket_timeout == NULL)
+ return NULL;
+ Py_INCREF(socket_timeout);
+ PyModule_AddObject(m, "timeout", socket_timeout);
+ Py_INCREF((PyObject *)&sock_type);
+ if (PyModule_AddObject(m, "SocketType",
+ (PyObject *)&sock_type) != 0)
+ return NULL;
+ Py_INCREF((PyObject *)&sock_type);
+ if (PyModule_AddObject(m, "socket",
+ (PyObject *)&sock_type) != 0)
+ return NULL;
#ifdef ENABLE_IPV6
- has_ipv6 = Py_True;
+ has_ipv6 = Py_True;
#else
- has_ipv6 = Py_False;
+ has_ipv6 = Py_False;
#endif
- Py_INCREF(has_ipv6);
- PyModule_AddObject(m, "has_ipv6", has_ipv6);
+ Py_INCREF(has_ipv6);
+ PyModule_AddObject(m, "has_ipv6", has_ipv6);
- /* Export C API */
- if (PyModule_AddObject(m, PySocket_CAPI_NAME,
- PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
- ) != 0)
- return NULL;
+ /* Export C API */
+ if (PyModule_AddObject(m, PySocket_CAPI_NAME,
+ PyCapsule_New(&PySocketModuleAPI, PySocket_CAPSULE_NAME, NULL)
+ ) != 0)
+ return NULL;
- /* Address families (we only support AF_INET and AF_UNIX) */
+ /* Address families (we only support AF_INET and AF_UNIX) */
#ifdef AF_UNSPEC
- PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
+ PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);
#endif
- PyModule_AddIntConstant(m, "AF_INET", AF_INET);
+ PyModule_AddIntConstant(m, "AF_INET", AF_INET);
#ifdef AF_INET6
- PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
+ PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);
#endif /* AF_INET6 */
#if defined(AF_UNIX)
- PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
+ PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);
#endif /* AF_UNIX */
#ifdef AF_AX25
- /* Amateur Radio AX.25 */
- PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
+ /* Amateur Radio AX.25 */
+ PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);
#endif
#ifdef AF_IPX
- PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
+ PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */
#endif
#ifdef AF_APPLETALK
- /* Appletalk DDP */
- PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
+ /* Appletalk DDP */
+ PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);
#endif
#ifdef AF_NETROM
- /* Amateur radio NetROM */
- PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
+ /* Amateur radio NetROM */
+ PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);
#endif
#ifdef AF_BRIDGE
- /* Multiprotocol bridge */
- PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
+ /* Multiprotocol bridge */
+ PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);
#endif
#ifdef AF_ATMPVC
- /* ATM PVCs */
- PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
+ /* ATM PVCs */
+ PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);
#endif
#ifdef AF_AAL5
- /* Reserved for Werner's ATM */
- PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
+ /* Reserved for Werner's ATM */
+ PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);
#endif
#ifdef AF_X25
- /* Reserved for X.25 project */
- PyModule_AddIntConstant(m, "AF_X25", AF_X25);
+ /* Reserved for X.25 project */
+ PyModule_AddIntConstant(m, "AF_X25", AF_X25);
#endif
#ifdef AF_INET6
- PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
+ PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */
#endif
#ifdef AF_ROSE
- /* Amateur Radio X.25 PLP */
- PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
+ /* Amateur Radio X.25 PLP */
+ PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);
#endif
#ifdef AF_DECnet
- /* Reserved for DECnet project */
- PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
+ /* Reserved for DECnet project */
+ PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);
#endif
#ifdef AF_NETBEUI
- /* Reserved for 802.2LLC project */
- PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
+ /* Reserved for 802.2LLC project */
+ PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);
#endif
#ifdef AF_SECURITY
- /* Security callback pseudo AF */
- PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
+ /* Security callback pseudo AF */
+ PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);
#endif
#ifdef AF_KEY
- /* PF_KEY key management API */
- PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
+ /* PF_KEY key management API */
+ PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);
#endif
#ifdef AF_NETLINK
- /* */
- PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
- PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
+ /* */
+ PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);
+ PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);
#ifdef NETLINK_SKIP
- PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
+ PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);
#endif
#ifdef NETLINK_W1
- PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
+ PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);
#endif
- PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
- PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
+ PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);
+ PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);
#ifdef NETLINK_TCPDIAG
- PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
+ PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);
#endif
#ifdef NETLINK_NFLOG
- PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
+ PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);
#endif
#ifdef NETLINK_XFRM
- PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
+ PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);
#endif
#ifdef NETLINK_ARPD
- PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
+ PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);
#endif
#ifdef NETLINK_ROUTE6
- PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
+ PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);
#endif
- PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
+ PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);
#ifdef NETLINK_DNRTMSG
- PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
+ PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);
#endif
#ifdef NETLINK_TAPBASE
- PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
+ PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);
#endif
#endif /* AF_NETLINK */
#ifdef AF_ROUTE
- /* Alias to emulate 4.4BSD */
- PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
+ /* Alias to emulate 4.4BSD */
+ PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);
#endif
#ifdef AF_ASH
- /* Ash */
- PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
+ /* Ash */
+ PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);
#endif
#ifdef AF_ECONET
- /* Acorn Econet */
- PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
+ /* Acorn Econet */
+ PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);
#endif
#ifdef AF_ATMSVC
- /* ATM SVCs */
- PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
+ /* ATM SVCs */
+ PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);
#endif
#ifdef AF_SNA
- /* Linux SNA Project (nutters!) */
- PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
+ /* Linux SNA Project (nutters!) */
+ PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);
#endif
#ifdef AF_IRDA
- /* IRDA sockets */
- PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
+ /* IRDA sockets */
+ PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);
#endif
#ifdef AF_PPPOX
- /* PPPoX sockets */
- PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
+ /* PPPoX sockets */
+ PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);
#endif
#ifdef AF_WANPIPE
- /* Wanpipe API Sockets */
- PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
+ /* Wanpipe API Sockets */
+ PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);
#endif
#ifdef AF_LLC
- /* Linux LLC */
- PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
+ /* Linux LLC */
+ PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);
#endif
#ifdef USE_BLUETOOTH
- PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
- PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
- PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
- PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
- PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
+ PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);
+ PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);
+ PyModule_AddIntConstant(m, "BTPROTO_HCI", BTPROTO_HCI);
+ PyModule_AddIntConstant(m, "SOL_HCI", SOL_HCI);
+ PyModule_AddIntConstant(m, "HCI_FILTER", HCI_FILTER);
#if !defined(__FreeBSD__)
- PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
- PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
- PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
+ PyModule_AddIntConstant(m, "HCI_TIME_STAMP", HCI_TIME_STAMP);
+ PyModule_AddIntConstant(m, "HCI_DATA_DIR", HCI_DATA_DIR);
+ PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);
#endif
- PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
- PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
- PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
+ PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);
+ PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");
+ PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");
#endif
#ifdef HAVE_NETPACKET_PACKET_H
- PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
- PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
- PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
- PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
- PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
- PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
- PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
- PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
- PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
+ PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);
+ PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);
+ PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);
+ PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);
+ PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);
+ PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);
+ PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);
+ PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);
+ PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);
#endif
#ifdef HAVE_LINUX_TIPC_H
- PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
-
- /* for addresses */
- PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
- PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
- PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
-
- PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
- PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
- PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
-
- /* for setsockopt() */
- PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
- PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
- PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
- PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
- TIPC_DEST_DROPPABLE);
- PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
-
- PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
- TIPC_LOW_IMPORTANCE);
- PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
- TIPC_MEDIUM_IMPORTANCE);
- PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
- TIPC_HIGH_IMPORTANCE);
- PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
- TIPC_CRITICAL_IMPORTANCE);
-
- /* for subscriptions */
- PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
- PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
+ PyModule_AddIntConstant(m, "AF_TIPC", AF_TIPC);
+
+ /* for addresses */
+ PyModule_AddIntConstant(m, "TIPC_ADDR_NAMESEQ", TIPC_ADDR_NAMESEQ);
+ PyModule_AddIntConstant(m, "TIPC_ADDR_NAME", TIPC_ADDR_NAME);
+ PyModule_AddIntConstant(m, "TIPC_ADDR_ID", TIPC_ADDR_ID);
+
+ PyModule_AddIntConstant(m, "TIPC_ZONE_SCOPE", TIPC_ZONE_SCOPE);
+ PyModule_AddIntConstant(m, "TIPC_CLUSTER_SCOPE", TIPC_CLUSTER_SCOPE);
+ PyModule_AddIntConstant(m, "TIPC_NODE_SCOPE", TIPC_NODE_SCOPE);
+
+ /* for setsockopt() */
+ PyModule_AddIntConstant(m, "SOL_TIPC", SOL_TIPC);
+ PyModule_AddIntConstant(m, "TIPC_IMPORTANCE", TIPC_IMPORTANCE);
+ PyModule_AddIntConstant(m, "TIPC_SRC_DROPPABLE", TIPC_SRC_DROPPABLE);
+ PyModule_AddIntConstant(m, "TIPC_DEST_DROPPABLE",
+ TIPC_DEST_DROPPABLE);
+ PyModule_AddIntConstant(m, "TIPC_CONN_TIMEOUT", TIPC_CONN_TIMEOUT);
+
+ PyModule_AddIntConstant(m, "TIPC_LOW_IMPORTANCE",
+ TIPC_LOW_IMPORTANCE);
+ PyModule_AddIntConstant(m, "TIPC_MEDIUM_IMPORTANCE",
+ TIPC_MEDIUM_IMPORTANCE);
+ PyModule_AddIntConstant(m, "TIPC_HIGH_IMPORTANCE",
+ TIPC_HIGH_IMPORTANCE);
+ PyModule_AddIntConstant(m, "TIPC_CRITICAL_IMPORTANCE",
+ TIPC_CRITICAL_IMPORTANCE);
+
+ /* for subscriptions */
+ PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
+ PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
#ifdef TIPC_SUB_CANCEL
- /* doesn't seem to be available everywhere */
- PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
+ /* doesn't seem to be available everywhere */
+ PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
#endif
- PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
- PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
- PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
- PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
- PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
- PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
+ PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
+ PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
+ PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
+ PyModule_AddIntConstant(m, "TIPC_SUBSCR_TIMEOUT", TIPC_SUBSCR_TIMEOUT);
+ PyModule_AddIntConstant(m, "TIPC_CFG_SRV", TIPC_CFG_SRV);
+ PyModule_AddIntConstant(m, "TIPC_TOP_SRV", TIPC_TOP_SRV);
#endif
- /* Socket types */
- PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
- PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
+ /* Socket types */
+ PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);
+ PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);
/* We have incomplete socket support. */
- PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
- PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
+ PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);
+ PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);
#if defined(SOCK_RDM)
- PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
+ PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);
#endif
-#ifdef SO_DEBUG
- PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
+#ifdef SO_DEBUG
+ PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);
#endif
-#ifdef SO_ACCEPTCONN
- PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
+#ifdef SO_ACCEPTCONN
+ PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);
#endif
-#ifdef SO_REUSEADDR
- PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
+#ifdef SO_REUSEADDR
+ PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);
#endif
#ifdef SO_EXCLUSIVEADDRUSE
- PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
+ PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);
#endif
-#ifdef SO_KEEPALIVE
- PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
+#ifdef SO_KEEPALIVE
+ PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);
#endif
-#ifdef SO_DONTROUTE
- PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
+#ifdef SO_DONTROUTE
+ PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);
#endif
-#ifdef SO_BROADCAST
- PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
+#ifdef SO_BROADCAST
+ PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);
#endif
-#ifdef SO_USELOOPBACK
- PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
+#ifdef SO_USELOOPBACK
+ PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);
#endif
-#ifdef SO_LINGER
- PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
+#ifdef SO_LINGER
+ PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);
#endif
-#ifdef SO_OOBINLINE
- PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
+#ifdef SO_OOBINLINE
+ PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);
#endif
-#ifdef SO_REUSEPORT
- PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
+#ifdef SO_REUSEPORT
+ PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);
#endif
-#ifdef SO_SNDBUF
- PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
+#ifdef SO_SNDBUF
+ PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);
#endif
-#ifdef SO_RCVBUF
- PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
+#ifdef SO_RCVBUF
+ PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);
#endif
-#ifdef SO_SNDLOWAT
- PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
+#ifdef SO_SNDLOWAT
+ PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);
#endif
-#ifdef SO_RCVLOWAT
- PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
+#ifdef SO_RCVLOWAT
+ PyModule_AddIntConstant(m, "SO_RCVLOWAT", SO_RCVLOWAT);
#endif
-#ifdef SO_SNDTIMEO
- PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
+#ifdef SO_SNDTIMEO
+ PyModule_AddIntConstant(m, "SO_SNDTIMEO", SO_SNDTIMEO);
#endif
-#ifdef SO_RCVTIMEO
- PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
+#ifdef SO_RCVTIMEO
+ PyModule_AddIntConstant(m, "SO_RCVTIMEO", SO_RCVTIMEO);
#endif
-#ifdef SO_ERROR
- PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
+#ifdef SO_ERROR
+ PyModule_AddIntConstant(m, "SO_ERROR", SO_ERROR);
#endif
-#ifdef SO_TYPE
- PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
+#ifdef SO_TYPE
+ PyModule_AddIntConstant(m, "SO_TYPE", SO_TYPE);
#endif
- /* Maximum number of connections for "listen" */
-#ifdef SOMAXCONN
- PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
+ /* Maximum number of connections for "listen" */
+#ifdef SOMAXCONN
+ PyModule_AddIntConstant(m, "SOMAXCONN", SOMAXCONN);
#else
- PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
+ PyModule_AddIntConstant(m, "SOMAXCONN", 5); /* Common value */
#endif
- /* Flags for send, recv */
-#ifdef MSG_OOB
- PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
+ /* Flags for send, recv */
+#ifdef MSG_OOB
+ PyModule_AddIntConstant(m, "MSG_OOB", MSG_OOB);
#endif
-#ifdef MSG_PEEK
- PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
+#ifdef MSG_PEEK
+ PyModule_AddIntConstant(m, "MSG_PEEK", MSG_PEEK);
#endif
-#ifdef MSG_DONTROUTE
- PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
+#ifdef MSG_DONTROUTE
+ PyModule_AddIntConstant(m, "MSG_DONTROUTE", MSG_DONTROUTE);
#endif
-#ifdef MSG_DONTWAIT
- PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
+#ifdef MSG_DONTWAIT
+ PyModule_AddIntConstant(m, "MSG_DONTWAIT", MSG_DONTWAIT);
#endif
-#ifdef MSG_EOR
- PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
+#ifdef MSG_EOR
+ PyModule_AddIntConstant(m, "MSG_EOR", MSG_EOR);
#endif
-#ifdef MSG_TRUNC
- PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
+#ifdef MSG_TRUNC
+ PyModule_AddIntConstant(m, "MSG_TRUNC", MSG_TRUNC);
#endif
-#ifdef MSG_CTRUNC
- PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
+#ifdef MSG_CTRUNC
+ PyModule_AddIntConstant(m, "MSG_CTRUNC", MSG_CTRUNC);
#endif
-#ifdef MSG_WAITALL
- PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
+#ifdef MSG_WAITALL
+ PyModule_AddIntConstant(m, "MSG_WAITALL", MSG_WAITALL);
#endif
-#ifdef MSG_BTAG
- PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
+#ifdef MSG_BTAG
+ PyModule_AddIntConstant(m, "MSG_BTAG", MSG_BTAG);
#endif
-#ifdef MSG_ETAG
- PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
+#ifdef MSG_ETAG
+ PyModule_AddIntConstant(m, "MSG_ETAG", MSG_ETAG);
#endif
- /* Protocol level and numbers, usable for [gs]etsockopt */
-#ifdef SOL_SOCKET
- PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
+ /* Protocol level and numbers, usable for [gs]etsockopt */
+#ifdef SOL_SOCKET
+ PyModule_AddIntConstant(m, "SOL_SOCKET", SOL_SOCKET);
#endif
-#ifdef SOL_IP
- PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
+#ifdef SOL_IP
+ PyModule_AddIntConstant(m, "SOL_IP", SOL_IP);
#else
- PyModule_AddIntConstant(m, "SOL_IP", 0);
+ PyModule_AddIntConstant(m, "SOL_IP", 0);
#endif
-#ifdef SOL_IPX
- PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
+#ifdef SOL_IPX
+ PyModule_AddIntConstant(m, "SOL_IPX", SOL_IPX);
#endif
-#ifdef SOL_AX25
- PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
+#ifdef SOL_AX25
+ PyModule_AddIntConstant(m, "SOL_AX25", SOL_AX25);
#endif
-#ifdef SOL_ATALK
- PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
+#ifdef SOL_ATALK
+ PyModule_AddIntConstant(m, "SOL_ATALK", SOL_ATALK);
#endif
-#ifdef SOL_NETROM
- PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
+#ifdef SOL_NETROM
+ PyModule_AddIntConstant(m, "SOL_NETROM", SOL_NETROM);
#endif
-#ifdef SOL_ROSE
- PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
+#ifdef SOL_ROSE
+ PyModule_AddIntConstant(m, "SOL_ROSE", SOL_ROSE);
#endif
-#ifdef SOL_TCP
- PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
+#ifdef SOL_TCP
+ PyModule_AddIntConstant(m, "SOL_TCP", SOL_TCP);
#else
- PyModule_AddIntConstant(m, "SOL_TCP", 6);
+ PyModule_AddIntConstant(m, "SOL_TCP", 6);
#endif
-#ifdef SOL_UDP
- PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
+#ifdef SOL_UDP
+ PyModule_AddIntConstant(m, "SOL_UDP", SOL_UDP);
#else
- PyModule_AddIntConstant(m, "SOL_UDP", 17);
+ PyModule_AddIntConstant(m, "SOL_UDP", 17);
#endif
-#ifdef IPPROTO_IP
- PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
+#ifdef IPPROTO_IP
+ PyModule_AddIntConstant(m, "IPPROTO_IP", IPPROTO_IP);
#else
- PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
+ PyModule_AddIntConstant(m, "IPPROTO_IP", 0);
#endif
-#ifdef IPPROTO_HOPOPTS
- PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
+#ifdef IPPROTO_HOPOPTS
+ PyModule_AddIntConstant(m, "IPPROTO_HOPOPTS", IPPROTO_HOPOPTS);
#endif
-#ifdef IPPROTO_ICMP
- PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
+#ifdef IPPROTO_ICMP
+ PyModule_AddIntConstant(m, "IPPROTO_ICMP", IPPROTO_ICMP);
#else
- PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
+ PyModule_AddIntConstant(m, "IPPROTO_ICMP", 1);
#endif
-#ifdef IPPROTO_IGMP
- PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
+#ifdef IPPROTO_IGMP
+ PyModule_AddIntConstant(m, "IPPROTO_IGMP", IPPROTO_IGMP);
#endif
-#ifdef IPPROTO_GGP
- PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
+#ifdef IPPROTO_GGP
+ PyModule_AddIntConstant(m, "IPPROTO_GGP", IPPROTO_GGP);
#endif
-#ifdef IPPROTO_IPV4
- PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
+#ifdef IPPROTO_IPV4
+ PyModule_AddIntConstant(m, "IPPROTO_IPV4", IPPROTO_IPV4);
#endif
-#ifdef IPPROTO_IPV6
- PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
+#ifdef IPPROTO_IPV6
+ PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
#endif
-#ifdef IPPROTO_IPIP
- PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
+#ifdef IPPROTO_IPIP
+ PyModule_AddIntConstant(m, "IPPROTO_IPIP", IPPROTO_IPIP);
#endif
-#ifdef IPPROTO_TCP
- PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
+#ifdef IPPROTO_TCP
+ PyModule_AddIntConstant(m, "IPPROTO_TCP", IPPROTO_TCP);
#else
- PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
+ PyModule_AddIntConstant(m, "IPPROTO_TCP", 6);
#endif
-#ifdef IPPROTO_EGP
- PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
+#ifdef IPPROTO_EGP
+ PyModule_AddIntConstant(m, "IPPROTO_EGP", IPPROTO_EGP);
#endif
-#ifdef IPPROTO_PUP
- PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
+#ifdef IPPROTO_PUP
+ PyModule_AddIntConstant(m, "IPPROTO_PUP", IPPROTO_PUP);
#endif
-#ifdef IPPROTO_UDP
- PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
+#ifdef IPPROTO_UDP
+ PyModule_AddIntConstant(m, "IPPROTO_UDP", IPPROTO_UDP);
#else
- PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
+ PyModule_AddIntConstant(m, "IPPROTO_UDP", 17);
#endif
-#ifdef IPPROTO_IDP
- PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
+#ifdef IPPROTO_IDP
+ PyModule_AddIntConstant(m, "IPPROTO_IDP", IPPROTO_IDP);
#endif
-#ifdef IPPROTO_HELLO
- PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
+#ifdef IPPROTO_HELLO
+ PyModule_AddIntConstant(m, "IPPROTO_HELLO", IPPROTO_HELLO);
#endif
-#ifdef IPPROTO_ND
- PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
+#ifdef IPPROTO_ND
+ PyModule_AddIntConstant(m, "IPPROTO_ND", IPPROTO_ND);
#endif
-#ifdef IPPROTO_TP
- PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
+#ifdef IPPROTO_TP
+ PyModule_AddIntConstant(m, "IPPROTO_TP", IPPROTO_TP);
#endif
-#ifdef IPPROTO_IPV6
- PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
+#ifdef IPPROTO_IPV6
+ PyModule_AddIntConstant(m, "IPPROTO_IPV6", IPPROTO_IPV6);
#endif
-#ifdef IPPROTO_ROUTING
- PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
+#ifdef IPPROTO_ROUTING
+ PyModule_AddIntConstant(m, "IPPROTO_ROUTING", IPPROTO_ROUTING);
#endif
-#ifdef IPPROTO_FRAGMENT
- PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
+#ifdef IPPROTO_FRAGMENT
+ PyModule_AddIntConstant(m, "IPPROTO_FRAGMENT", IPPROTO_FRAGMENT);
#endif
-#ifdef IPPROTO_RSVP
- PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
+#ifdef IPPROTO_RSVP
+ PyModule_AddIntConstant(m, "IPPROTO_RSVP", IPPROTO_RSVP);
#endif
-#ifdef IPPROTO_GRE
- PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
+#ifdef IPPROTO_GRE
+ PyModule_AddIntConstant(m, "IPPROTO_GRE", IPPROTO_GRE);
#endif
-#ifdef IPPROTO_ESP
- PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
+#ifdef IPPROTO_ESP
+ PyModule_AddIntConstant(m, "IPPROTO_ESP", IPPROTO_ESP);
#endif
-#ifdef IPPROTO_AH
- PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
+#ifdef IPPROTO_AH
+ PyModule_AddIntConstant(m, "IPPROTO_AH", IPPROTO_AH);
#endif
-#ifdef IPPROTO_MOBILE
- PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
+#ifdef IPPROTO_MOBILE
+ PyModule_AddIntConstant(m, "IPPROTO_MOBILE", IPPROTO_MOBILE);
#endif
-#ifdef IPPROTO_ICMPV6
- PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
+#ifdef IPPROTO_ICMPV6
+ PyModule_AddIntConstant(m, "IPPROTO_ICMPV6", IPPROTO_ICMPV6);
#endif
-#ifdef IPPROTO_NONE
- PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
+#ifdef IPPROTO_NONE
+ PyModule_AddIntConstant(m, "IPPROTO_NONE", IPPROTO_NONE);
#endif
-#ifdef IPPROTO_DSTOPTS
- PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
+#ifdef IPPROTO_DSTOPTS
+ PyModule_AddIntConstant(m, "IPPROTO_DSTOPTS", IPPROTO_DSTOPTS);
#endif
-#ifdef IPPROTO_XTP
- PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
+#ifdef IPPROTO_XTP
+ PyModule_AddIntConstant(m, "IPPROTO_XTP", IPPROTO_XTP);
#endif
-#ifdef IPPROTO_EON
- PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
+#ifdef IPPROTO_EON
+ PyModule_AddIntConstant(m, "IPPROTO_EON", IPPROTO_EON);
#endif
-#ifdef IPPROTO_PIM
- PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
+#ifdef IPPROTO_PIM
+ PyModule_AddIntConstant(m, "IPPROTO_PIM", IPPROTO_PIM);
#endif
-#ifdef IPPROTO_IPCOMP
- PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
+#ifdef IPPROTO_IPCOMP
+ PyModule_AddIntConstant(m, "IPPROTO_IPCOMP", IPPROTO_IPCOMP);
#endif
-#ifdef IPPROTO_VRRP
- PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
+#ifdef IPPROTO_VRRP
+ PyModule_AddIntConstant(m, "IPPROTO_VRRP", IPPROTO_VRRP);
#endif
-#ifdef IPPROTO_BIP
- PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
+#ifdef IPPROTO_BIP
+ PyModule_AddIntConstant(m, "IPPROTO_BIP", IPPROTO_BIP);
#endif
/**/
-#ifdef IPPROTO_RAW
- PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
+#ifdef IPPROTO_RAW
+ PyModule_AddIntConstant(m, "IPPROTO_RAW", IPPROTO_RAW);
#else
- PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
+ PyModule_AddIntConstant(m, "IPPROTO_RAW", 255);
#endif
-#ifdef IPPROTO_MAX
- PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
+#ifdef IPPROTO_MAX
+ PyModule_AddIntConstant(m, "IPPROTO_MAX", IPPROTO_MAX);
#endif
- /* Some port configuration */
-#ifdef IPPORT_RESERVED
- PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
+ /* Some port configuration */
+#ifdef IPPORT_RESERVED
+ PyModule_AddIntConstant(m, "IPPORT_RESERVED", IPPORT_RESERVED);
#else
- PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
+ PyModule_AddIntConstant(m, "IPPORT_RESERVED", 1024);
#endif
-#ifdef IPPORT_USERRESERVED
- PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
+#ifdef IPPORT_USERRESERVED
+ PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", IPPORT_USERRESERVED);
#else
- PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
+ PyModule_AddIntConstant(m, "IPPORT_USERRESERVED", 5000);
#endif
- /* Some reserved IP v.4 addresses */
-#ifdef INADDR_ANY
- PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
+ /* Some reserved IP v.4 addresses */
+#ifdef INADDR_ANY
+ PyModule_AddIntConstant(m, "INADDR_ANY", INADDR_ANY);
#else
- PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
+ PyModule_AddIntConstant(m, "INADDR_ANY", 0x00000000);
#endif
-#ifdef INADDR_BROADCAST
- PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
+#ifdef INADDR_BROADCAST
+ PyModule_AddIntConstant(m, "INADDR_BROADCAST", INADDR_BROADCAST);
#else
- PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
+ PyModule_AddIntConstant(m, "INADDR_BROADCAST", 0xffffffff);
#endif
-#ifdef INADDR_LOOPBACK
- PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
+#ifdef INADDR_LOOPBACK
+ PyModule_AddIntConstant(m, "INADDR_LOOPBACK", INADDR_LOOPBACK);
#else
- PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
+ PyModule_AddIntConstant(m, "INADDR_LOOPBACK", 0x7F000001);
#endif
-#ifdef INADDR_UNSPEC_GROUP
- PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
+#ifdef INADDR_UNSPEC_GROUP
+ PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", INADDR_UNSPEC_GROUP);
#else
- PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
+ PyModule_AddIntConstant(m, "INADDR_UNSPEC_GROUP", 0xe0000000);
#endif
-#ifdef INADDR_ALLHOSTS_GROUP
- PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
- INADDR_ALLHOSTS_GROUP);
+#ifdef INADDR_ALLHOSTS_GROUP
+ PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP",
+ INADDR_ALLHOSTS_GROUP);
#else
- PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
+ PyModule_AddIntConstant(m, "INADDR_ALLHOSTS_GROUP", 0xe0000001);
#endif
-#ifdef INADDR_MAX_LOCAL_GROUP
- PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
- INADDR_MAX_LOCAL_GROUP);
+#ifdef INADDR_MAX_LOCAL_GROUP
+ PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP",
+ INADDR_MAX_LOCAL_GROUP);
#else
- PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
+ PyModule_AddIntConstant(m, "INADDR_MAX_LOCAL_GROUP", 0xe00000ff);
#endif
-#ifdef INADDR_NONE
- PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
+#ifdef INADDR_NONE
+ PyModule_AddIntConstant(m, "INADDR_NONE", INADDR_NONE);
#else
- PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
+ PyModule_AddIntConstant(m, "INADDR_NONE", 0xffffffff);
#endif
- /* IPv4 [gs]etsockopt options */
-#ifdef IP_OPTIONS
- PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
+ /* IPv4 [gs]etsockopt options */
+#ifdef IP_OPTIONS
+ PyModule_AddIntConstant(m, "IP_OPTIONS", IP_OPTIONS);
#endif
-#ifdef IP_HDRINCL
- PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
+#ifdef IP_HDRINCL
+ PyModule_AddIntConstant(m, "IP_HDRINCL", IP_HDRINCL);
#endif
-#ifdef IP_TOS
- PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
+#ifdef IP_TOS
+ PyModule_AddIntConstant(m, "IP_TOS", IP_TOS);
#endif
-#ifdef IP_TTL
- PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
+#ifdef IP_TTL
+ PyModule_AddIntConstant(m, "IP_TTL", IP_TTL);
#endif
-#ifdef IP_RECVOPTS
- PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
+#ifdef IP_RECVOPTS
+ PyModule_AddIntConstant(m, "IP_RECVOPTS", IP_RECVOPTS);
#endif
-#ifdef IP_RECVRETOPTS
- PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
+#ifdef IP_RECVRETOPTS
+ PyModule_AddIntConstant(m, "IP_RECVRETOPTS", IP_RECVRETOPTS);
#endif
-#ifdef IP_RECVDSTADDR
- PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
+#ifdef IP_RECVDSTADDR
+ PyModule_AddIntConstant(m, "IP_RECVDSTADDR", IP_RECVDSTADDR);
#endif
-#ifdef IP_RETOPTS
- PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
+#ifdef IP_RETOPTS
+ PyModule_AddIntConstant(m, "IP_RETOPTS", IP_RETOPTS);
#endif
-#ifdef IP_MULTICAST_IF
- PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
+#ifdef IP_MULTICAST_IF
+ PyModule_AddIntConstant(m, "IP_MULTICAST_IF", IP_MULTICAST_IF);
#endif
-#ifdef IP_MULTICAST_TTL
- PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
+#ifdef IP_MULTICAST_TTL
+ PyModule_AddIntConstant(m, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);
#endif
-#ifdef IP_MULTICAST_LOOP
- PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
+#ifdef IP_MULTICAST_LOOP
+ PyModule_AddIntConstant(m, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
#endif
-#ifdef IP_ADD_MEMBERSHIP
- PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
+#ifdef IP_ADD_MEMBERSHIP
+ PyModule_AddIntConstant(m, "IP_ADD_MEMBERSHIP", IP_ADD_MEMBERSHIP);
#endif
-#ifdef IP_DROP_MEMBERSHIP
- PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
+#ifdef IP_DROP_MEMBERSHIP
+ PyModule_AddIntConstant(m, "IP_DROP_MEMBERSHIP", IP_DROP_MEMBERSHIP);
#endif
-#ifdef IP_DEFAULT_MULTICAST_TTL
- PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
- IP_DEFAULT_MULTICAST_TTL);
+#ifdef IP_DEFAULT_MULTICAST_TTL
+ PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_TTL",
+ IP_DEFAULT_MULTICAST_TTL);
#endif
-#ifdef IP_DEFAULT_MULTICAST_LOOP
- PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
- IP_DEFAULT_MULTICAST_LOOP);
+#ifdef IP_DEFAULT_MULTICAST_LOOP
+ PyModule_AddIntConstant(m, "IP_DEFAULT_MULTICAST_LOOP",
+ IP_DEFAULT_MULTICAST_LOOP);
#endif
-#ifdef IP_MAX_MEMBERSHIPS
- PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
+#ifdef IP_MAX_MEMBERSHIPS
+ PyModule_AddIntConstant(m, "IP_MAX_MEMBERSHIPS", IP_MAX_MEMBERSHIPS);
#endif
- /* IPv6 [gs]etsockopt options, defined in RFC2553 */
-#ifdef IPV6_JOIN_GROUP
- PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
+ /* IPv6 [gs]etsockopt options, defined in RFC2553 */
+#ifdef IPV6_JOIN_GROUP
+ PyModule_AddIntConstant(m, "IPV6_JOIN_GROUP", IPV6_JOIN_GROUP);
#endif
-#ifdef IPV6_LEAVE_GROUP
- PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
+#ifdef IPV6_LEAVE_GROUP
+ PyModule_AddIntConstant(m, "IPV6_LEAVE_GROUP", IPV6_LEAVE_GROUP);
#endif
-#ifdef IPV6_MULTICAST_HOPS
- PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
+#ifdef IPV6_MULTICAST_HOPS
+ PyModule_AddIntConstant(m, "IPV6_MULTICAST_HOPS", IPV6_MULTICAST_HOPS);
#endif
-#ifdef IPV6_MULTICAST_IF
- PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
+#ifdef IPV6_MULTICAST_IF
+ PyModule_AddIntConstant(m, "IPV6_MULTICAST_IF", IPV6_MULTICAST_IF);
#endif
-#ifdef IPV6_MULTICAST_LOOP
- PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
+#ifdef IPV6_MULTICAST_LOOP
+ PyModule_AddIntConstant(m, "IPV6_MULTICAST_LOOP", IPV6_MULTICAST_LOOP);
#endif
-#ifdef IPV6_UNICAST_HOPS
- PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
+#ifdef IPV6_UNICAST_HOPS
+ PyModule_AddIntConstant(m, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
#endif
- /* Additional IPV6 socket options, defined in RFC 3493 */
+ /* Additional IPV6 socket options, defined in RFC 3493 */
#ifdef IPV6_V6ONLY
- PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
+ PyModule_AddIntConstant(m, "IPV6_V6ONLY", IPV6_V6ONLY);
#endif
- /* Advanced IPV6 socket options, from RFC 3542 */
+ /* Advanced IPV6 socket options, from RFC 3542 */
#ifdef IPV6_CHECKSUM
- PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
+ PyModule_AddIntConstant(m, "IPV6_CHECKSUM", IPV6_CHECKSUM);
#endif
#ifdef IPV6_DONTFRAG
- PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
+ PyModule_AddIntConstant(m, "IPV6_DONTFRAG", IPV6_DONTFRAG);
#endif
#ifdef IPV6_DSTOPTS
- PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
+ PyModule_AddIntConstant(m, "IPV6_DSTOPTS", IPV6_DSTOPTS);
#endif
#ifdef IPV6_HOPLIMIT
- PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
+ PyModule_AddIntConstant(m, "IPV6_HOPLIMIT", IPV6_HOPLIMIT);
#endif
#ifdef IPV6_HOPOPTS
- PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
+ PyModule_AddIntConstant(m, "IPV6_HOPOPTS", IPV6_HOPOPTS);
#endif
#ifdef IPV6_NEXTHOP
- PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
+ PyModule_AddIntConstant(m, "IPV6_NEXTHOP", IPV6_NEXTHOP);
#endif
#ifdef IPV6_PATHMTU
- PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
+ PyModule_AddIntConstant(m, "IPV6_PATHMTU", IPV6_PATHMTU);
#endif
#ifdef IPV6_PKTINFO
- PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
+ PyModule_AddIntConstant(m, "IPV6_PKTINFO", IPV6_PKTINFO);
#endif
#ifdef IPV6_RECVDSTOPTS
- PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
+ PyModule_AddIntConstant(m, "IPV6_RECVDSTOPTS", IPV6_RECVDSTOPTS);
#endif
#ifdef IPV6_RECVHOPLIMIT
- PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
+ PyModule_AddIntConstant(m, "IPV6_RECVHOPLIMIT", IPV6_RECVHOPLIMIT);
#endif
#ifdef IPV6_RECVHOPOPTS
- PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
+ PyModule_AddIntConstant(m, "IPV6_RECVHOPOPTS", IPV6_RECVHOPOPTS);
#endif
#ifdef IPV6_RECVPKTINFO
- PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
+ PyModule_AddIntConstant(m, "IPV6_RECVPKTINFO", IPV6_RECVPKTINFO);
#endif
#ifdef IPV6_RECVRTHDR
- PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
+ PyModule_AddIntConstant(m, "IPV6_RECVRTHDR", IPV6_RECVRTHDR);
#endif
#ifdef IPV6_RECVTCLASS
- PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
+ PyModule_AddIntConstant(m, "IPV6_RECVTCLASS", IPV6_RECVTCLASS);
#endif
#ifdef IPV6_RTHDR
- PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
+ PyModule_AddIntConstant(m, "IPV6_RTHDR", IPV6_RTHDR);
#endif
#ifdef IPV6_RTHDRDSTOPTS
- PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
+ PyModule_AddIntConstant(m, "IPV6_RTHDRDSTOPTS", IPV6_RTHDRDSTOPTS);
#endif
#ifdef IPV6_RTHDR_TYPE_0
- PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
+ PyModule_AddIntConstant(m, "IPV6_RTHDR_TYPE_0", IPV6_RTHDR_TYPE_0);
#endif
#ifdef IPV6_RECVPATHMTU
- PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
+ PyModule_AddIntConstant(m, "IPV6_RECVPATHMTU", IPV6_RECVPATHMTU);
#endif
#ifdef IPV6_TCLASS
- PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
+ PyModule_AddIntConstant(m, "IPV6_TCLASS", IPV6_TCLASS);
#endif
#ifdef IPV6_USE_MIN_MTU
- PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
+ PyModule_AddIntConstant(m, "IPV6_USE_MIN_MTU", IPV6_USE_MIN_MTU);
#endif
- /* TCP options */
-#ifdef TCP_NODELAY
- PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
+ /* TCP options */
+#ifdef TCP_NODELAY
+ PyModule_AddIntConstant(m, "TCP_NODELAY", TCP_NODELAY);
#endif
-#ifdef TCP_MAXSEG
- PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
+#ifdef TCP_MAXSEG
+ PyModule_AddIntConstant(m, "TCP_MAXSEG", TCP_MAXSEG);
#endif
-#ifdef TCP_CORK
- PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
+#ifdef TCP_CORK
+ PyModule_AddIntConstant(m, "TCP_CORK", TCP_CORK);
#endif
-#ifdef TCP_KEEPIDLE
- PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
+#ifdef TCP_KEEPIDLE
+ PyModule_AddIntConstant(m, "TCP_KEEPIDLE", TCP_KEEPIDLE);
#endif
-#ifdef TCP_KEEPINTVL
- PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
+#ifdef TCP_KEEPINTVL
+ PyModule_AddIntConstant(m, "TCP_KEEPINTVL", TCP_KEEPINTVL);
#endif
-#ifdef TCP_KEEPCNT
- PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
+#ifdef TCP_KEEPCNT
+ PyModule_AddIntConstant(m, "TCP_KEEPCNT", TCP_KEEPCNT);
#endif
-#ifdef TCP_SYNCNT
- PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
+#ifdef TCP_SYNCNT
+ PyModule_AddIntConstant(m, "TCP_SYNCNT", TCP_SYNCNT);
#endif
-#ifdef TCP_LINGER2
- PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
+#ifdef TCP_LINGER2
+ PyModule_AddIntConstant(m, "TCP_LINGER2", TCP_LINGER2);
#endif
-#ifdef TCP_DEFER_ACCEPT
- PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
+#ifdef TCP_DEFER_ACCEPT
+ PyModule_AddIntConstant(m, "TCP_DEFER_ACCEPT", TCP_DEFER_ACCEPT);
#endif
-#ifdef TCP_WINDOW_CLAMP
- PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
+#ifdef TCP_WINDOW_CLAMP
+ PyModule_AddIntConstant(m, "TCP_WINDOW_CLAMP", TCP_WINDOW_CLAMP);
#endif
-#ifdef TCP_INFO
- PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
+#ifdef TCP_INFO
+ PyModule_AddIntConstant(m, "TCP_INFO", TCP_INFO);
#endif
-#ifdef TCP_QUICKACK
- PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
+#ifdef TCP_QUICKACK
+ PyModule_AddIntConstant(m, "TCP_QUICKACK", TCP_QUICKACK);
#endif
- /* IPX options */
-#ifdef IPX_TYPE
- PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
+ /* IPX options */
+#ifdef IPX_TYPE
+ PyModule_AddIntConstant(m, "IPX_TYPE", IPX_TYPE);
#endif
- /* get{addr,name}info parameters */
+ /* get{addr,name}info parameters */
#ifdef EAI_ADDRFAMILY
- PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
+ PyModule_AddIntConstant(m, "EAI_ADDRFAMILY", EAI_ADDRFAMILY);
#endif
#ifdef EAI_AGAIN
- PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
+ PyModule_AddIntConstant(m, "EAI_AGAIN", EAI_AGAIN);
#endif
#ifdef EAI_BADFLAGS
- PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
+ PyModule_AddIntConstant(m, "EAI_BADFLAGS", EAI_BADFLAGS);
#endif
#ifdef EAI_FAIL
- PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
+ PyModule_AddIntConstant(m, "EAI_FAIL", EAI_FAIL);
#endif
#ifdef EAI_FAMILY
- PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
+ PyModule_AddIntConstant(m, "EAI_FAMILY", EAI_FAMILY);
#endif
#ifdef EAI_MEMORY
- PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
+ PyModule_AddIntConstant(m, "EAI_MEMORY", EAI_MEMORY);
#endif
#ifdef EAI_NODATA
- PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
+ PyModule_AddIntConstant(m, "EAI_NODATA", EAI_NODATA);
#endif
#ifdef EAI_NONAME
- PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
+ PyModule_AddIntConstant(m, "EAI_NONAME", EAI_NONAME);
#endif
#ifdef EAI_OVERFLOW
- PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
+ PyModule_AddIntConstant(m, "EAI_OVERFLOW", EAI_OVERFLOW);
#endif
#ifdef EAI_SERVICE
- PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
+ PyModule_AddIntConstant(m, "EAI_SERVICE", EAI_SERVICE);
#endif
#ifdef EAI_SOCKTYPE
- PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
+ PyModule_AddIntConstant(m, "EAI_SOCKTYPE", EAI_SOCKTYPE);
#endif
#ifdef EAI_SYSTEM
- PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
+ PyModule_AddIntConstant(m, "EAI_SYSTEM", EAI_SYSTEM);
#endif
#ifdef EAI_BADHINTS
- PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
+ PyModule_AddIntConstant(m, "EAI_BADHINTS", EAI_BADHINTS);
#endif
#ifdef EAI_PROTOCOL
- PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
+ PyModule_AddIntConstant(m, "EAI_PROTOCOL", EAI_PROTOCOL);
#endif
#ifdef EAI_MAX
- PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
+ PyModule_AddIntConstant(m, "EAI_MAX", EAI_MAX);
#endif
#ifdef AI_PASSIVE
- PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
+ PyModule_AddIntConstant(m, "AI_PASSIVE", AI_PASSIVE);
#endif
#ifdef AI_CANONNAME
- PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
+ PyModule_AddIntConstant(m, "AI_CANONNAME", AI_CANONNAME);
#endif
#ifdef AI_NUMERICHOST
- PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
+ PyModule_AddIntConstant(m, "AI_NUMERICHOST", AI_NUMERICHOST);
#endif
#ifdef AI_NUMERICSERV
- PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
+ PyModule_AddIntConstant(m, "AI_NUMERICSERV", AI_NUMERICSERV);
#endif
#ifdef AI_MASK
- PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
+ PyModule_AddIntConstant(m, "AI_MASK", AI_MASK);
#endif
#ifdef AI_ALL
- PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
+ PyModule_AddIntConstant(m, "AI_ALL", AI_ALL);
#endif
#ifdef AI_V4MAPPED_CFG
- PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
+ PyModule_AddIntConstant(m, "AI_V4MAPPED_CFG", AI_V4MAPPED_CFG);
#endif
#ifdef AI_ADDRCONFIG
- PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
+ PyModule_AddIntConstant(m, "AI_ADDRCONFIG", AI_ADDRCONFIG);
#endif
#ifdef AI_V4MAPPED
- PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
+ PyModule_AddIntConstant(m, "AI_V4MAPPED", AI_V4MAPPED);
#endif
#ifdef AI_DEFAULT
- PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
+ PyModule_AddIntConstant(m, "AI_DEFAULT", AI_DEFAULT);
#endif
#ifdef NI_MAXHOST
- PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
+ PyModule_AddIntConstant(m, "NI_MAXHOST", NI_MAXHOST);
#endif
#ifdef NI_MAXSERV
- PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
+ PyModule_AddIntConstant(m, "NI_MAXSERV", NI_MAXSERV);
#endif
#ifdef NI_NOFQDN
- PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
+ PyModule_AddIntConstant(m, "NI_NOFQDN", NI_NOFQDN);
#endif
#ifdef NI_NUMERICHOST
- PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
+ PyModule_AddIntConstant(m, "NI_NUMERICHOST", NI_NUMERICHOST);
#endif
#ifdef NI_NAMEREQD
- PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
+ PyModule_AddIntConstant(m, "NI_NAMEREQD", NI_NAMEREQD);
#endif
#ifdef NI_NUMERICSERV
- PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
+ PyModule_AddIntConstant(m, "NI_NUMERICSERV", NI_NUMERICSERV);
#endif
#ifdef NI_DGRAM
- PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
+ PyModule_AddIntConstant(m, "NI_DGRAM", NI_DGRAM);
#endif
- /* shutdown() parameters */
+ /* shutdown() parameters */
#ifdef SHUT_RD
- PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
+ PyModule_AddIntConstant(m, "SHUT_RD", SHUT_RD);
#elif defined(SD_RECEIVE)
- PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
+ PyModule_AddIntConstant(m, "SHUT_RD", SD_RECEIVE);
#else
- PyModule_AddIntConstant(m, "SHUT_RD", 0);
+ PyModule_AddIntConstant(m, "SHUT_RD", 0);
#endif
#ifdef SHUT_WR
- PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
+ PyModule_AddIntConstant(m, "SHUT_WR", SHUT_WR);
#elif defined(SD_SEND)
- PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
+ PyModule_AddIntConstant(m, "SHUT_WR", SD_SEND);
#else
- PyModule_AddIntConstant(m, "SHUT_WR", 1);
+ PyModule_AddIntConstant(m, "SHUT_WR", 1);
#endif
#ifdef SHUT_RDWR
- PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
+ PyModule_AddIntConstant(m, "SHUT_RDWR", SHUT_RDWR);
#elif defined(SD_BOTH)
- PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
+ PyModule_AddIntConstant(m, "SHUT_RDWR", SD_BOTH);
#else
- PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
+ PyModule_AddIntConstant(m, "SHUT_RDWR", 2);
#endif
#ifdef SIO_RCVALL
- {
- PyObject *tmp;
- tmp = PyLong_FromUnsignedLong(SIO_RCVALL);
- if (tmp == NULL)
- return NULL;
- PyModule_AddObject(m, "SIO_RCVALL", tmp);
- }
- PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
- PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
- PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
+ {
+ PyObject *tmp;
+ tmp = PyLong_FromUnsignedLong(SIO_RCVALL);
+ if (tmp == NULL)
+ return NULL;
+ PyModule_AddObject(m, "SIO_RCVALL", tmp);
+ }
+ PyModule_AddIntConstant(m, "RCVALL_OFF", RCVALL_OFF);
+ PyModule_AddIntConstant(m, "RCVALL_ON", RCVALL_ON);
+ PyModule_AddIntConstant(m, "RCVALL_SOCKETLEVELONLY", RCVALL_SOCKETLEVELONLY);
#ifdef RCVALL_IPLEVEL
- PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
+ PyModule_AddIntConstant(m, "RCVALL_IPLEVEL", RCVALL_IPLEVEL);
#endif
#ifdef RCVALL_MAX
- PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
+ PyModule_AddIntConstant(m, "RCVALL_MAX", RCVALL_MAX);
#endif
#endif /* _MSTCPIP_ */
- /* Initialize gethostbyname lock */
+ /* Initialize gethostbyname lock */
#if defined(USE_GETHOSTBYNAME_LOCK) || defined(USE_GETADDRINFO_LOCK)
- netdb_lock = PyThread_allocate_lock();
+ netdb_lock = PyThread_allocate_lock();
#endif
- return m;
+ return m;
}
@@ -5100,34 +5100,34 @@ PyInit__socket(void)
int
inet_pton(int af, const char *src, void *dst)
{
- if (af == AF_INET) {
+ if (af == AF_INET) {
#if (SIZEOF_INT != 4)
#error "Not sure if in_addr_t exists and int is not 32-bits."
#endif
- unsigned int packed_addr;
- packed_addr = inet_addr(src);
- if (packed_addr == INADDR_NONE)
- return 0;
- memcpy(dst, &packed_addr, 4);
- return 1;
- }
- /* Should set errno to EAFNOSUPPORT */
- return -1;
+ unsigned int packed_addr;
+ packed_addr = inet_addr(src);
+ if (packed_addr == INADDR_NONE)
+ return 0;
+ memcpy(dst, &packed_addr, 4);
+ return 1;
+ }
+ /* Should set errno to EAFNOSUPPORT */
+ return -1;
}
const char *
inet_ntop(int af, const void *src, char *dst, socklen_t size)
{
- if (af == AF_INET) {
- struct in_addr packed_addr;
- if (size < 16)
- /* Should set errno to ENOSPC. */
- return NULL;
- memcpy(&packed_addr, src, sizeof(packed_addr));
- return strncpy(dst, inet_ntoa(packed_addr), size);
- }
- /* Should set errno to EAFNOSUPPORT */
- return NULL;
+ if (af == AF_INET) {
+ struct in_addr packed_addr;
+ if (size < 16)
+ /* Should set errno to ENOSPC. */
+ return NULL;
+ memcpy(&packed_addr, src, sizeof(packed_addr));
+ return strncpy(dst, inet_ntoa(packed_addr), size);
+ }
+ /* Should set errno to EAFNOSUPPORT */
+ return NULL;
}
#endif
diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h
index 65bc8ad..49ac9d0 100644
--- a/Modules/socketmodule.h
+++ b/Modules/socketmodule.h
@@ -17,7 +17,7 @@
# include <ws2tcpip.h>
/* VC6 is shipped with old platform headers, and does not have MSTcpIP.h
* Separate SDKs have all the functions we want, but older ones don't have
- * any version information.
+ * any version information.
* I use SIO_GET_MULTICAST_FILTER to detect a decent SDK.
*/
# ifdef SIO_GET_MULTICAST_FILTER
@@ -76,44 +76,44 @@ extern "C" {
#endif
/* Python module and C API name */
-#define PySocket_MODULE_NAME "_socket"
-#define PySocket_CAPI_NAME "CAPI"
-#define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
+#define PySocket_MODULE_NAME "_socket"
+#define PySocket_CAPI_NAME "CAPI"
+#define PySocket_CAPSULE_NAME PySocket_MODULE_NAME "." PySocket_CAPI_NAME
/* Abstract the socket file descriptor type */
#ifdef MS_WINDOWS
typedef SOCKET SOCKET_T;
-# ifdef MS_WIN64
-# define SIZEOF_SOCKET_T 8
-# else
-# define SIZEOF_SOCKET_T 4
-# endif
+# ifdef MS_WIN64
+# define SIZEOF_SOCKET_T 8
+# else
+# define SIZEOF_SOCKET_T 4
+# endif
#else
typedef int SOCKET_T;
-# define SIZEOF_SOCKET_T SIZEOF_INT
+# define SIZEOF_SOCKET_T SIZEOF_INT
#endif
/* Socket address */
typedef union sock_addr {
- struct sockaddr_in in;
+ struct sockaddr_in in;
#ifdef AF_UNIX
- struct sockaddr_un un;
+ struct sockaddr_un un;
#endif
#ifdef AF_NETLINK
- struct sockaddr_nl nl;
+ struct sockaddr_nl nl;
#endif
#ifdef ENABLE_IPV6
- struct sockaddr_in6 in6;
- struct sockaddr_storage storage;
+ struct sockaddr_in6 in6;
+ struct sockaddr_storage storage;
#endif
#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
- struct sockaddr_l2 bt_l2;
- struct sockaddr_rc bt_rc;
- struct sockaddr_sco bt_sco;
- struct sockaddr_hci bt_hci;
+ struct sockaddr_l2 bt_l2;
+ struct sockaddr_rc bt_rc;
+ struct sockaddr_sco bt_sco;
+ struct sockaddr_hci bt_hci;
#endif
#ifdef HAVE_NETPACKET_PACKET_H
- struct sockaddr_ll ll;
+ struct sockaddr_ll ll;
#endif
} sock_addr_t;
@@ -122,16 +122,16 @@ typedef union sock_addr {
arguments properly. */
typedef struct {
- PyObject_HEAD
- SOCKET_T sock_fd; /* Socket file descriptor */
- int sock_family; /* Address family, e.g., AF_INET */
- int sock_type; /* Socket type, e.g., SOCK_STREAM */
- int sock_proto; /* Protocol type, usually 0 */
- PyObject *(*errorhandler)(void); /* Error handler; checks
- errno, returns NULL and
- sets a Python exception */
- double sock_timeout; /* Operation timeout in seconds;
- 0.0 means non-blocking */
+ PyObject_HEAD
+ SOCKET_T sock_fd; /* Socket file descriptor */
+ int sock_family; /* Address family, e.g., AF_INET */
+ int sock_type; /* Socket type, e.g., SOCK_STREAM */
+ int sock_proto; /* Protocol type, usually 0 */
+ PyObject *(*errorhandler)(void); /* Error handler; checks
+ errno, returns NULL and
+ sets a Python exception */
+ double sock_timeout; /* Operation timeout in seconds;
+ 0.0 means non-blocking */
} PySocketSockObject;
/* --- C API ----------------------------------------------------*/
@@ -139,7 +139,7 @@ typedef struct {
/* Short explanation of what this C API export mechanism does
and how it works:
- The _ssl module needs access to the type object defined in
+ The _ssl module needs access to the type object defined in
the _socket module. Since cross-DLL linking introduces a lot of
problems on many platforms, the "trick" is to wrap the
C API of a module in a struct which then gets exported to
@@ -161,24 +161,24 @@ typedef struct {
Load _socket module and its C API; this sets up the global
PySocketModule:
-
- if (PySocketModule_ImportModuleAndAPI())
- return;
+
+ if (PySocketModule_ImportModuleAndAPI())
+ return;
Now use the C API as if it were defined in the using
module:
- if (!PyArg_ParseTuple(args, "O!|zz:ssl",
+ if (!PyArg_ParseTuple(args, "O!|zz:ssl",
- PySocketModule.Sock_Type,
+ PySocketModule.Sock_Type,
- (PyObject*)&Sock,
- &key_file, &cert_file))
- return NULL;
+ (PyObject*)&Sock,
+ &key_file, &cert_file))
+ return NULL;
Support could easily be extended to export more C APIs/symbols
- this way. Currently, only the type object is exported,
+ this way. Currently, only the type object is exported,
other candidates would be socket constructors and socket
access functions.
@@ -186,8 +186,8 @@ typedef struct {
/* C API for usage by other Python modules */
typedef struct {
- PyTypeObject *Sock_Type;
- PyObject *error;
+ PyTypeObject *Sock_Type;
+ PyObject *error;
} PySocketModule_APIObject;
#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)
diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c
index 230b57c..3a62fe8 100644
--- a/Modules/spwdmodule.c
+++ b/Modules/spwdmodule.c
@@ -27,16 +27,16 @@ You have to be root to be able to use this module.");
#if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
static PyStructSequence_Field struct_spwd_type_fields[] = {
- {"sp_nam", "login name"},
- {"sp_pwd", "encrypted password"},
- {"sp_lstchg", "date of last change"},
- {"sp_min", "min #days between changes"},
- {"sp_max", "max #days between changes"},
- {"sp_warn", "#days before pw expires to warn user about it"},
- {"sp_inact", "#days after pw expires until account is blocked"},
- {"sp_expire", "#days since 1970-01-01 until account is disabled"},
- {"sp_flag", "reserved"},
- {0}
+ {"sp_nam", "login name"},
+ {"sp_pwd", "encrypted password"},
+ {"sp_lstchg", "date of last change"},
+ {"sp_min", "min #days between changes"},
+ {"sp_max", "max #days between changes"},
+ {"sp_warn", "#days before pw expires to warn user about it"},
+ {"sp_inact", "#days after pw expires until account is blocked"},
+ {"sp_expire", "#days since 1970-01-01 until account is disabled"},
+ {"sp_flag", "reserved"},
+ {0}
};
PyDoc_STRVAR(struct_spwd__doc__,
@@ -46,10 +46,10 @@ This object may be accessed either as a 9-tuple of\n\
or via the object attributes as named in the above tuple.");
static PyStructSequence_Desc struct_spwd_type_desc = {
- "spwd.struct_spwd",
- struct_spwd__doc__,
- struct_spwd_type_fields,
- 9,
+ "spwd.struct_spwd",
+ struct_spwd__doc__,
+ struct_spwd_type_fields,
+ 9,
};
static int initialized;
@@ -60,45 +60,45 @@ static void
sets(PyObject *v, int i, const char* val)
{
if (val) {
- PyObject *o = PyUnicode_Decode(val, strlen(val),
- Py_FileSystemDefaultEncoding,
- "surrogateescape");
- PyStructSequence_SET_ITEM(v, i, o);
+ PyObject *o = PyUnicode_Decode(val, strlen(val),
+ Py_FileSystemDefaultEncoding,
+ "surrogateescape");
+ PyStructSequence_SET_ITEM(v, i, o);
} else {
- PyStructSequence_SET_ITEM(v, i, Py_None);
- Py_INCREF(Py_None);
+ PyStructSequence_SET_ITEM(v, i, Py_None);
+ Py_INCREF(Py_None);
}
}
static PyObject *mkspent(struct spwd *p)
{
- int setIndex = 0;
- PyObject *v = PyStructSequence_New(&StructSpwdType);
- if (v == NULL)
- return NULL;
+ int setIndex = 0;
+ PyObject *v = PyStructSequence_New(&StructSpwdType);
+ if (v == NULL)
+ return NULL;
#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
#define SETS(i,val) sets(v, i, val)
- SETS(setIndex++, p->sp_namp);
- SETS(setIndex++, p->sp_pwdp);
- SETI(setIndex++, p->sp_lstchg);
- SETI(setIndex++, p->sp_min);
- SETI(setIndex++, p->sp_max);
- SETI(setIndex++, p->sp_warn);
- SETI(setIndex++, p->sp_inact);
- SETI(setIndex++, p->sp_expire);
- SETI(setIndex++, p->sp_flag);
+ SETS(setIndex++, p->sp_namp);
+ SETS(setIndex++, p->sp_pwdp);
+ SETI(setIndex++, p->sp_lstchg);
+ SETI(setIndex++, p->sp_min);
+ SETI(setIndex++, p->sp_max);
+ SETI(setIndex++, p->sp_warn);
+ SETI(setIndex++, p->sp_inact);
+ SETI(setIndex++, p->sp_expire);
+ SETI(setIndex++, p->sp_flag);
#undef SETS
#undef SETI
- if (PyErr_Occurred()) {
- Py_DECREF(v);
- return NULL;
- }
+ if (PyErr_Occurred()) {
+ Py_DECREF(v);
+ return NULL;
+ }
- return v;
+ return v;
}
#endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
@@ -114,26 +114,26 @@ See spwd.__doc__ for more on shadow password database entries.");
static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
{
- char *name;
- struct spwd *p;
- PyObject *arg, *bytes, *retval = NULL;
-
- if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
- return NULL;
- if ((bytes = PyUnicode_AsEncodedString(arg,
- Py_FileSystemDefaultEncoding,
- "surrogateescape")) == NULL)
- return NULL;
- if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
- goto out;
- if ((p = getspnam(name)) == NULL) {
- PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
- goto out;
- }
- retval = mkspent(p);
+ char *name;
+ struct spwd *p;
+ PyObject *arg, *bytes, *retval = NULL;
+
+ if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
+ return NULL;
+ if ((bytes = PyUnicode_AsEncodedString(arg,
+ Py_FileSystemDefaultEncoding,
+ "surrogateescape")) == NULL)
+ return NULL;
+ if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
+ goto out;
+ if ((p = getspnam(name)) == NULL) {
+ PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
+ goto out;
+ }
+ retval = mkspent(p);
out:
- Py_DECREF(bytes);
- return retval;
+ Py_DECREF(bytes);
+ return retval;
}
#endif /* HAVE_GETSPNAM */
@@ -149,63 +149,63 @@ See spwd.__doc__ for more on shadow password database entries.");
static PyObject *
spwd_getspall(PyObject *self, PyObject *args)
{
- PyObject *d;
- struct spwd *p;
- if ((d = PyList_New(0)) == NULL)
- return NULL;
- setspent();
- while ((p = getspent()) != NULL) {
- PyObject *v = mkspent(p);
- if (v == NULL || PyList_Append(d, v) != 0) {
- Py_XDECREF(v);
- Py_DECREF(d);
- endspent();
- return NULL;
- }
- Py_DECREF(v);
- }
- endspent();
- return d;
+ PyObject *d;
+ struct spwd *p;
+ if ((d = PyList_New(0)) == NULL)
+ return NULL;
+ setspent();
+ while ((p = getspent()) != NULL) {
+ PyObject *v = mkspent(p);
+ if (v == NULL || PyList_Append(d, v) != 0) {
+ Py_XDECREF(v);
+ Py_DECREF(d);
+ endspent();
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ endspent();
+ return d;
}
#endif /* HAVE_GETSPENT */
static PyMethodDef spwd_methods[] = {
-#ifdef HAVE_GETSPNAM
- {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
+#ifdef HAVE_GETSPNAM
+ {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
#endif
#ifdef HAVE_GETSPENT
- {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
+ {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
#endif
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef spwdmodule = {
- PyModuleDef_HEAD_INIT,
- "spwd",
- spwd__doc__,
- -1,
- spwd_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "spwd",
+ spwd__doc__,
+ -1,
+ spwd_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_spwd(void)
{
- PyObject *m;
- m=PyModule_Create(&spwdmodule);
- if (m == NULL)
- return NULL;
- if (!initialized)
- PyStructSequence_InitType(&StructSpwdType,
- &struct_spwd_type_desc);
- Py_INCREF((PyObject *) &StructSpwdType);
- PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
- initialized = 1;
- return m;
+ PyObject *m;
+ m=PyModule_Create(&spwdmodule);
+ if (m == NULL)
+ return NULL;
+ if (!initialized)
+ PyStructSequence_InitType(&StructSpwdType,
+ &struct_spwd_type_desc);
+ Py_INCREF((PyObject *) &StructSpwdType);
+ PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
+ initialized = 1;
+ return m;
}
diff --git a/Modules/symtablemodule.c b/Modules/symtablemodule.c
index 3388d63..26e2768 100644
--- a/Modules/symtablemodule.c
+++ b/Modules/symtablemodule.c
@@ -8,97 +8,97 @@
static PyObject *
symtable_symtable(PyObject *self, PyObject *args)
{
- struct symtable *st;
- PyObject *t;
+ struct symtable *st;
+ PyObject *t;
- char *str;
- char *filename;
- char *startstr;
- int start;
+ char *str;
+ char *filename;
+ char *startstr;
+ int start;
- if (!PyArg_ParseTuple(args, "sss:symtable", &str, &filename,
- &startstr))
- return NULL;
- if (strcmp(startstr, "exec") == 0)
- start = Py_file_input;
- else if (strcmp(startstr, "eval") == 0)
- start = Py_eval_input;
- else if (strcmp(startstr, "single") == 0)
- start = Py_single_input;
- else {
- PyErr_SetString(PyExc_ValueError,
- "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
- return NULL;
- }
- st = Py_SymtableString(str, filename, start);
- if (st == NULL)
- return NULL;
- t = st->st_blocks;
- Py_INCREF(t);
- PyMem_Free((void *)st->st_future);
- PySymtable_Free(st);
- return t;
+ if (!PyArg_ParseTuple(args, "sss:symtable", &str, &filename,
+ &startstr))
+ return NULL;
+ if (strcmp(startstr, "exec") == 0)
+ start = Py_file_input;
+ else if (strcmp(startstr, "eval") == 0)
+ start = Py_eval_input;
+ else if (strcmp(startstr, "single") == 0)
+ start = Py_single_input;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
+ return NULL;
+ }
+ st = Py_SymtableString(str, filename, start);
+ if (st == NULL)
+ return NULL;
+ t = st->st_blocks;
+ Py_INCREF(t);
+ PyMem_Free((void *)st->st_future);
+ PySymtable_Free(st);
+ return t;
}
static PyMethodDef symtable_methods[] = {
- {"symtable", symtable_symtable, METH_VARARGS,
- PyDoc_STR("Return symbol and scope dictionaries"
- " used internally by compiler.")},
- {NULL, NULL} /* sentinel */
+ {"symtable", symtable_symtable, METH_VARARGS,
+ PyDoc_STR("Return symbol and scope dictionaries"
+ " used internally by compiler.")},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef symtablemodule = {
- PyModuleDef_HEAD_INIT,
- "_symtable",
- NULL,
- -1,
- symtable_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_symtable",
+ NULL,
+ -1,
+ symtable_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__symtable(void)
{
- PyObject *m;
+ PyObject *m;
- m = PyModule_Create(&symtablemodule);
- if (m == NULL)
- return NULL;
- PyModule_AddIntConstant(m, "USE", USE);
- PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
- PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
- PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM);
- PyModule_AddIntConstant(m, "DEF_STAR", DEF_STAR);
- PyModule_AddIntConstant(m, "DEF_DOUBLESTAR", DEF_DOUBLESTAR);
- PyModule_AddIntConstant(m, "DEF_INTUPLE", DEF_INTUPLE);
- PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE);
- PyModule_AddIntConstant(m, "DEF_FREE_GLOBAL", DEF_FREE_GLOBAL);
- PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS);
- PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT);
- PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND);
+ m = PyModule_Create(&symtablemodule);
+ if (m == NULL)
+ return NULL;
+ PyModule_AddIntConstant(m, "USE", USE);
+ PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
+ PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
+ PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM);
+ PyModule_AddIntConstant(m, "DEF_STAR", DEF_STAR);
+ PyModule_AddIntConstant(m, "DEF_DOUBLESTAR", DEF_DOUBLESTAR);
+ PyModule_AddIntConstant(m, "DEF_INTUPLE", DEF_INTUPLE);
+ PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE);
+ PyModule_AddIntConstant(m, "DEF_FREE_GLOBAL", DEF_FREE_GLOBAL);
+ PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS);
+ PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT);
+ PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND);
- PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
- PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
- PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
+ PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
+ PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
+ PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
- PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR);
- PyModule_AddIntConstant(m, "OPT_TOPLEVEL", OPT_TOPLEVEL);
+ PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR);
+ PyModule_AddIntConstant(m, "OPT_TOPLEVEL", OPT_TOPLEVEL);
- PyModule_AddIntConstant(m, "LOCAL", LOCAL);
- PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT);
- PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT);
- PyModule_AddIntConstant(m, "FREE", FREE);
- PyModule_AddIntConstant(m, "CELL", CELL);
+ PyModule_AddIntConstant(m, "LOCAL", LOCAL);
+ PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT);
+ PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT);
+ PyModule_AddIntConstant(m, "FREE", FREE);
+ PyModule_AddIntConstant(m, "CELL", CELL);
- PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
- PyModule_AddIntConstant(m, "SCOPE_MASK", SCOPE_MASK);
+ PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
+ PyModule_AddIntConstant(m, "SCOPE_MASK", SCOPE_MASK);
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- m = 0;
- }
- return m;
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ m = 0;
+ }
+ return m;
}
diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c
index c6a3b36..5532d03 100644
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -4,20 +4,20 @@ Cathedral City, California Republic, United States of America.
All Rights Reserved
-Permission to use, copy, modify, and distribute this software and its
-documentation for any purpose and without fee is hereby granted,
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
-both that copyright notice and this permission notice appear in
+both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Lance Ellinghouse
-not be used in advertising or publicity pertaining to distribution
+not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
-FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
-INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
-FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
-NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
+INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
@@ -49,198 +49,198 @@ Revision history:
#include <syslog.h>
/* only one instance, only one syslog, so globals should be ok */
-static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
+static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
-static PyObject *
+static PyObject *
syslog_openlog(PyObject * self, PyObject * args)
{
- long logopt = 0;
- long facility = LOG_USER;
- PyObject *new_S_ident_o;
- const char *ident;
-
- if (!PyArg_ParseTuple(args,
- "U|ll;ident string [, logoption [, facility]]",
- &new_S_ident_o, &logopt, &facility))
- return NULL;
-
- /* This is needed because openlog() does NOT make a copy
- * and syslog() later uses it.. cannot trash it.
- */
- Py_XDECREF(S_ident_o);
- S_ident_o = new_S_ident_o;
- Py_INCREF(S_ident_o);
-
- ident = _PyUnicode_AsString(S_ident_o);
- if (ident == NULL)
- return NULL;
- openlog(ident, logopt, facility);
-
- Py_INCREF(Py_None);
- return Py_None;
+ long logopt = 0;
+ long facility = LOG_USER;
+ PyObject *new_S_ident_o;
+ const char *ident;
+
+ if (!PyArg_ParseTuple(args,
+ "U|ll;ident string [, logoption [, facility]]",
+ &new_S_ident_o, &logopt, &facility))
+ return NULL;
+
+ /* This is needed because openlog() does NOT make a copy
+ * and syslog() later uses it.. cannot trash it.
+ */
+ Py_XDECREF(S_ident_o);
+ S_ident_o = new_S_ident_o;
+ Py_INCREF(S_ident_o);
+
+ ident = _PyUnicode_AsString(S_ident_o);
+ if (ident == NULL)
+ return NULL;
+ openlog(ident, logopt, facility);
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
-static PyObject *
+static PyObject *
syslog_syslog(PyObject * self, PyObject * args)
{
- PyObject *message_object;
- const char *message;
- int priority = LOG_INFO;
-
- if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
- &priority, &message_object)) {
- PyErr_Clear();
- if (!PyArg_ParseTuple(args, "U;[priority,] message string",
- &message_object))
- return NULL;
- }
-
- message = _PyUnicode_AsString(message_object);
- if (message == NULL)
- return NULL;
- Py_BEGIN_ALLOW_THREADS;
- syslog(priority, "%s", message);
- Py_END_ALLOW_THREADS;
- Py_RETURN_NONE;
+ PyObject *message_object;
+ const char *message;
+ int priority = LOG_INFO;
+
+ if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
+ &priority, &message_object)) {
+ PyErr_Clear();
+ if (!PyArg_ParseTuple(args, "U;[priority,] message string",
+ &message_object))
+ return NULL;
+ }
+
+ message = _PyUnicode_AsString(message_object);
+ if (message == NULL)
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS;
+ syslog(priority, "%s", message);
+ Py_END_ALLOW_THREADS;
+ Py_RETURN_NONE;
}
-static PyObject *
+static PyObject *
syslog_closelog(PyObject *self, PyObject *unused)
{
- closelog();
- Py_XDECREF(S_ident_o);
- S_ident_o = NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ closelog();
+ Py_XDECREF(S_ident_o);
+ S_ident_o = NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
-static PyObject *
+static PyObject *
syslog_setlogmask(PyObject *self, PyObject *args)
{
- long maskpri, omaskpri;
+ long maskpri, omaskpri;
- if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
- return NULL;
- omaskpri = setlogmask(maskpri);
- return PyLong_FromLong(omaskpri);
+ if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
+ return NULL;
+ omaskpri = setlogmask(maskpri);
+ return PyLong_FromLong(omaskpri);
}
-static PyObject *
+static PyObject *
syslog_log_mask(PyObject *self, PyObject *args)
{
- long mask;
- long pri;
- if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
- return NULL;
- mask = LOG_MASK(pri);
- return PyLong_FromLong(mask);
+ long mask;
+ long pri;
+ if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
+ return NULL;
+ mask = LOG_MASK(pri);
+ return PyLong_FromLong(mask);
}
-static PyObject *
+static PyObject *
syslog_log_upto(PyObject *self, PyObject *args)
{
- long mask;
- long pri;
- if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
- return NULL;
- mask = LOG_UPTO(pri);
- return PyLong_FromLong(mask);
+ long mask;
+ long pri;
+ if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
+ return NULL;
+ mask = LOG_UPTO(pri);
+ return PyLong_FromLong(mask);
}
/* List of functions defined in the module */
static PyMethodDef syslog_methods[] = {
- {"openlog", syslog_openlog, METH_VARARGS},
- {"closelog", syslog_closelog, METH_NOARGS},
- {"syslog", syslog_syslog, METH_VARARGS},
- {"setlogmask", syslog_setlogmask, METH_VARARGS},
- {"LOG_MASK", syslog_log_mask, METH_VARARGS},
- {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
- {NULL, NULL, 0}
+ {"openlog", syslog_openlog, METH_VARARGS},
+ {"closelog", syslog_closelog, METH_NOARGS},
+ {"syslog", syslog_syslog, METH_VARARGS},
+ {"setlogmask", syslog_setlogmask, METH_VARARGS},
+ {"LOG_MASK", syslog_log_mask, METH_VARARGS},
+ {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
+ {NULL, NULL, 0}
};
/* Initialization function for the module */
static struct PyModuleDef syslogmodule = {
- PyModuleDef_HEAD_INIT,
- "syslog",
- NULL,
- -1,
- syslog_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "syslog",
+ NULL,
+ -1,
+ syslog_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_syslog(void)
{
- PyObject *m;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&syslogmodule);
- if (m == NULL)
- return NULL;
-
- /* Add some symbolic constants to the module */
-
- /* Priorities */
- PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
- PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
- PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
- PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
- PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
- PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
- PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
- PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
-
- /* openlog() option flags */
- PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
- PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
- PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
+ PyObject *m;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&syslogmodule);
+ if (m == NULL)
+ return NULL;
+
+ /* Add some symbolic constants to the module */
+
+ /* Priorities */
+ PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
+ PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
+ PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
+ PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
+ PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
+ PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
+ PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
+ PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
+
+ /* openlog() option flags */
+ PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
+ PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
+ PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
#ifdef LOG_NOWAIT
- PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
+ PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
#endif
#ifdef LOG_PERROR
- PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
+ PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
#endif
- /* Facilities */
- PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
- PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
- PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
- PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
- PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
- PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
- PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
- PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
- PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
- PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
- PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
- PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
- PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
- PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
+ /* Facilities */
+ PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
+ PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
+ PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
+ PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
+ PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
+ PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
+ PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
+ PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
+ PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
+ PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
+ PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
+ PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
+ PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
+ PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
#ifndef LOG_SYSLOG
-#define LOG_SYSLOG LOG_DAEMON
+#define LOG_SYSLOG LOG_DAEMON
#endif
#ifndef LOG_NEWS
-#define LOG_NEWS LOG_MAIL
+#define LOG_NEWS LOG_MAIL
#endif
#ifndef LOG_UUCP
-#define LOG_UUCP LOG_MAIL
+#define LOG_UUCP LOG_MAIL
#endif
#ifndef LOG_CRON
-#define LOG_CRON LOG_DAEMON
+#define LOG_CRON LOG_DAEMON
#endif
- PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
- PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
- PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
- PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
- return m;
+ PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
+ PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
+ PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
+ PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
+ return m;
}
diff --git a/Modules/termios.c b/Modules/termios.c
index 9fccb2e..97a0da2 100644
--- a/Modules/termios.c
+++ b/Modules/termios.c
@@ -42,14 +42,14 @@ static PyObject *TermiosError;
static int fdconv(PyObject* obj, void* p)
{
- int fd;
-
- fd = PyObject_AsFileDescriptor(obj);
- if (fd >= 0) {
- *(int*)p = fd;
- return 1;
- }
- return 0;
+ int fd;
+
+ fd = PyObject_AsFileDescriptor(obj);
+ if (fd >= 0) {
+ *(int*)p = fd;
+ return 1;
+ }
+ return 0;
}
PyDoc_STRVAR(termios_tcgetattr__doc__,
@@ -66,67 +66,67 @@ in this module.");
static PyObject *
termios_tcgetattr(PyObject *self, PyObject *args)
{
- int fd;
- struct termios mode;
- PyObject *cc;
- speed_t ispeed, ospeed;
- PyObject *v;
- int i;
- char ch;
-
- if (!PyArg_ParseTuple(args, "O&:tcgetattr",
- fdconv, (void*)&fd))
- return NULL;
-
- if (tcgetattr(fd, &mode) == -1)
- return PyErr_SetFromErrno(TermiosError);
-
- ispeed = cfgetispeed(&mode);
- ospeed = cfgetospeed(&mode);
-
- cc = PyList_New(NCCS);
- if (cc == NULL)
- return NULL;
- for (i = 0; i < NCCS; i++) {
- ch = (char)mode.c_cc[i];
- v = PyBytes_FromStringAndSize(&ch, 1);
- if (v == NULL)
- goto err;
- PyList_SetItem(cc, i, v);
- }
-
- /* Convert the MIN and TIME slots to integer. On some systems, the
- MIN and TIME slots are the same as the EOF and EOL slots. So we
- only do this in noncanonical input mode. */
- if ((mode.c_lflag & ICANON) == 0) {
- v = PyLong_FromLong((long)mode.c_cc[VMIN]);
- if (v == NULL)
- goto err;
- PyList_SetItem(cc, VMIN, v);
- v = PyLong_FromLong((long)mode.c_cc[VTIME]);
- if (v == NULL)
- goto err;
- PyList_SetItem(cc, VTIME, v);
- }
-
- if (!(v = PyList_New(7)))
- goto err;
-
- PyList_SetItem(v, 0, PyLong_FromLong((long)mode.c_iflag));
- PyList_SetItem(v, 1, PyLong_FromLong((long)mode.c_oflag));
- PyList_SetItem(v, 2, PyLong_FromLong((long)mode.c_cflag));
- PyList_SetItem(v, 3, PyLong_FromLong((long)mode.c_lflag));
- PyList_SetItem(v, 4, PyLong_FromLong((long)ispeed));
- PyList_SetItem(v, 5, PyLong_FromLong((long)ospeed));
- PyList_SetItem(v, 6, cc);
- if (PyErr_Occurred()){
- Py_DECREF(v);
- goto err;
- }
- return v;
+ int fd;
+ struct termios mode;
+ PyObject *cc;
+ speed_t ispeed, ospeed;
+ PyObject *v;
+ int i;
+ char ch;
+
+ if (!PyArg_ParseTuple(args, "O&:tcgetattr",
+ fdconv, (void*)&fd))
+ return NULL;
+
+ if (tcgetattr(fd, &mode) == -1)
+ return PyErr_SetFromErrno(TermiosError);
+
+ ispeed = cfgetispeed(&mode);
+ ospeed = cfgetospeed(&mode);
+
+ cc = PyList_New(NCCS);
+ if (cc == NULL)
+ return NULL;
+ for (i = 0; i < NCCS; i++) {
+ ch = (char)mode.c_cc[i];
+ v = PyBytes_FromStringAndSize(&ch, 1);
+ if (v == NULL)
+ goto err;
+ PyList_SetItem(cc, i, v);
+ }
+
+ /* Convert the MIN and TIME slots to integer. On some systems, the
+ MIN and TIME slots are the same as the EOF and EOL slots. So we
+ only do this in noncanonical input mode. */
+ if ((mode.c_lflag & ICANON) == 0) {
+ v = PyLong_FromLong((long)mode.c_cc[VMIN]);
+ if (v == NULL)
+ goto err;
+ PyList_SetItem(cc, VMIN, v);
+ v = PyLong_FromLong((long)mode.c_cc[VTIME]);
+ if (v == NULL)
+ goto err;
+ PyList_SetItem(cc, VTIME, v);
+ }
+
+ if (!(v = PyList_New(7)))
+ goto err;
+
+ PyList_SetItem(v, 0, PyLong_FromLong((long)mode.c_iflag));
+ PyList_SetItem(v, 1, PyLong_FromLong((long)mode.c_oflag));
+ PyList_SetItem(v, 2, PyLong_FromLong((long)mode.c_cflag));
+ PyList_SetItem(v, 3, PyLong_FromLong((long)mode.c_lflag));
+ PyList_SetItem(v, 4, PyLong_FromLong((long)ispeed));
+ PyList_SetItem(v, 5, PyLong_FromLong((long)ospeed));
+ PyList_SetItem(v, 6, cc);
+ if (PyErr_Occurred()){
+ Py_DECREF(v);
+ goto err;
+ }
+ return v;
err:
- Py_DECREF(cc);
- return NULL;
+ Py_DECREF(cc);
+ return NULL;
}
PyDoc_STRVAR(termios_tcsetattr__doc__,
@@ -143,64 +143,64 @@ queued output and discarding all queued input. ");
static PyObject *
termios_tcsetattr(PyObject *self, PyObject *args)
{
- int fd, when;
- struct termios mode;
- speed_t ispeed, ospeed;
- PyObject *term, *cc, *v;
- int i;
-
- if (!PyArg_ParseTuple(args, "O&iO:tcsetattr",
- fdconv, &fd, &when, &term))
- return NULL;
- if (!PyList_Check(term) || PyList_Size(term) != 7) {
- PyErr_SetString(PyExc_TypeError,
- "tcsetattr, arg 3: must be 7 element list");
- return NULL;
- }
-
- /* Get the old mode, in case there are any hidden fields... */
- if (tcgetattr(fd, &mode) == -1)
- return PyErr_SetFromErrno(TermiosError);
- mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0));
- mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1));
- mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2));
- mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3));
- ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4));
- ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5));
- cc = PyList_GetItem(term, 6);
- if (PyErr_Occurred())
- return NULL;
-
- if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) {
- PyErr_Format(PyExc_TypeError,
- "tcsetattr: attributes[6] must be %d element list",
- NCCS);
- return NULL;
- }
-
- for (i = 0; i < NCCS; i++) {
- v = PyList_GetItem(cc, i);
-
- if (PyBytes_Check(v) && PyBytes_Size(v) == 1)
- mode.c_cc[i] = (cc_t) * PyBytes_AsString(v);
- else if (PyLong_Check(v))
- mode.c_cc[i] = (cc_t) PyLong_AsLong(v);
- else {
- PyErr_SetString(PyExc_TypeError,
+ int fd, when;
+ struct termios mode;
+ speed_t ispeed, ospeed;
+ PyObject *term, *cc, *v;
+ int i;
+
+ if (!PyArg_ParseTuple(args, "O&iO:tcsetattr",
+ fdconv, &fd, &when, &term))
+ return NULL;
+ if (!PyList_Check(term) || PyList_Size(term) != 7) {
+ PyErr_SetString(PyExc_TypeError,
+ "tcsetattr, arg 3: must be 7 element list");
+ return NULL;
+ }
+
+ /* Get the old mode, in case there are any hidden fields... */
+ if (tcgetattr(fd, &mode) == -1)
+ return PyErr_SetFromErrno(TermiosError);
+ mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0));
+ mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1));
+ mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2));
+ mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3));
+ ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4));
+ ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5));
+ cc = PyList_GetItem(term, 6);
+ if (PyErr_Occurred())
+ return NULL;
+
+ if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) {
+ PyErr_Format(PyExc_TypeError,
+ "tcsetattr: attributes[6] must be %d element list",
+ NCCS);
+ return NULL;
+ }
+
+ for (i = 0; i < NCCS; i++) {
+ v = PyList_GetItem(cc, i);
+
+ if (PyBytes_Check(v) && PyBytes_Size(v) == 1)
+ mode.c_cc[i] = (cc_t) * PyBytes_AsString(v);
+ else if (PyLong_Check(v))
+ mode.c_cc[i] = (cc_t) PyLong_AsLong(v);
+ else {
+ PyErr_SetString(PyExc_TypeError,
"tcsetattr: elements of attributes must be characters or integers");
- return NULL;
- }
- }
-
- if (cfsetispeed(&mode, (speed_t) ispeed) == -1)
- return PyErr_SetFromErrno(TermiosError);
- if (cfsetospeed(&mode, (speed_t) ospeed) == -1)
- return PyErr_SetFromErrno(TermiosError);
- if (tcsetattr(fd, when, &mode) == -1)
- return PyErr_SetFromErrno(TermiosError);
-
- Py_INCREF(Py_None);
- return Py_None;
+ return NULL;
+ }
+ }
+
+ if (cfsetispeed(&mode, (speed_t) ispeed) == -1)
+ return PyErr_SetFromErrno(TermiosError);
+ if (cfsetospeed(&mode, (speed_t) ospeed) == -1)
+ return PyErr_SetFromErrno(TermiosError);
+ if (tcsetattr(fd, when, &mode) == -1)
+ return PyErr_SetFromErrno(TermiosError);
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(termios_tcsendbreak__doc__,
@@ -213,16 +213,16 @@ has a system dependent meaning.");
static PyObject *
termios_tcsendbreak(PyObject *self, PyObject *args)
{
- int fd, duration;
+ int fd, duration;
- if (!PyArg_ParseTuple(args, "O&i:tcsendbreak",
- fdconv, &fd, &duration))
- return NULL;
- if (tcsendbreak(fd, duration) == -1)
- return PyErr_SetFromErrno(TermiosError);
+ if (!PyArg_ParseTuple(args, "O&i:tcsendbreak",
+ fdconv, &fd, &duration))
+ return NULL;
+ if (tcsendbreak(fd, duration) == -1)
+ return PyErr_SetFromErrno(TermiosError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(termios_tcdrain__doc__,
@@ -233,16 +233,16 @@ Wait until all output written to file descriptor fd has been transmitted.");
static PyObject *
termios_tcdrain(PyObject *self, PyObject *args)
{
- int fd;
+ int fd;
- if (!PyArg_ParseTuple(args, "O&:tcdrain",
- fdconv, &fd))
- return NULL;
- if (tcdrain(fd) == -1)
- return PyErr_SetFromErrno(TermiosError);
+ if (!PyArg_ParseTuple(args, "O&:tcdrain",
+ fdconv, &fd))
+ return NULL;
+ if (tcdrain(fd) == -1)
+ return PyErr_SetFromErrno(TermiosError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(termios_tcflush__doc__,
@@ -256,16 +256,16 @@ both queues. ");
static PyObject *
termios_tcflush(PyObject *self, PyObject *args)
{
- int fd, queue;
+ int fd, queue;
- if (!PyArg_ParseTuple(args, "O&i:tcflush",
- fdconv, &fd, &queue))
- return NULL;
- if (tcflush(fd, queue) == -1)
- return PyErr_SetFromErrno(TermiosError);
+ if (!PyArg_ParseTuple(args, "O&i:tcflush",
+ fdconv, &fd, &queue))
+ return NULL;
+ if (tcflush(fd, queue) == -1)
+ return PyErr_SetFromErrno(TermiosError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(termios_tcflow__doc__,
@@ -279,33 +279,33 @@ or termios.TCION to restart input.");
static PyObject *
termios_tcflow(PyObject *self, PyObject *args)
{
- int fd, action;
+ int fd, action;
- if (!PyArg_ParseTuple(args, "O&i:tcflow",
- fdconv, &fd, &action))
- return NULL;
- if (tcflow(fd, action) == -1)
- return PyErr_SetFromErrno(TermiosError);
+ if (!PyArg_ParseTuple(args, "O&i:tcflow",
+ fdconv, &fd, &action))
+ return NULL;
+ if (tcflow(fd, action) == -1)
+ return PyErr_SetFromErrno(TermiosError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyMethodDef termios_methods[] =
{
- {"tcgetattr", termios_tcgetattr,
- METH_VARARGS, termios_tcgetattr__doc__},
- {"tcsetattr", termios_tcsetattr,
- METH_VARARGS, termios_tcsetattr__doc__},
- {"tcsendbreak", termios_tcsendbreak,
- METH_VARARGS, termios_tcsendbreak__doc__},
- {"tcdrain", termios_tcdrain,
- METH_VARARGS, termios_tcdrain__doc__},
- {"tcflush", termios_tcflush,
- METH_VARARGS, termios_tcflush__doc__},
- {"tcflow", termios_tcflow,
- METH_VARARGS, termios_tcflow__doc__},
- {NULL, NULL}
+ {"tcgetattr", termios_tcgetattr,
+ METH_VARARGS, termios_tcgetattr__doc__},
+ {"tcsetattr", termios_tcsetattr,
+ METH_VARARGS, termios_tcsetattr__doc__},
+ {"tcsendbreak", termios_tcsendbreak,
+ METH_VARARGS, termios_tcsendbreak__doc__},
+ {"tcdrain", termios_tcdrain,
+ METH_VARARGS, termios_tcdrain__doc__},
+ {"tcflush", termios_tcflush,
+ METH_VARARGS, termios_tcflush__doc__},
+ {"tcflow", termios_tcflow,
+ METH_VARARGS, termios_tcflow__doc__},
+ {NULL, NULL}
};
@@ -318,619 +318,619 @@ static PyMethodDef termios_methods[] =
#endif
static struct constant {
- char *name;
- long value;
+ char *name;
+ long value;
} termios_constants[] = {
- /* cfgetospeed(), cfsetospeed() constants */
- {"B0", B0},
- {"B50", B50},
- {"B75", B75},
- {"B110", B110},
- {"B134", B134},
- {"B150", B150},
- {"B200", B200},
- {"B300", B300},
- {"B600", B600},
- {"B1200", B1200},
- {"B1800", B1800},
- {"B2400", B2400},
- {"B4800", B4800},
- {"B9600", B9600},
- {"B19200", B19200},
- {"B38400", B38400},
+ /* cfgetospeed(), cfsetospeed() constants */
+ {"B0", B0},
+ {"B50", B50},
+ {"B75", B75},
+ {"B110", B110},
+ {"B134", B134},
+ {"B150", B150},
+ {"B200", B200},
+ {"B300", B300},
+ {"B600", B600},
+ {"B1200", B1200},
+ {"B1800", B1800},
+ {"B2400", B2400},
+ {"B4800", B4800},
+ {"B9600", B9600},
+ {"B19200", B19200},
+ {"B38400", B38400},
#ifdef B57600
- {"B57600", B57600},
+ {"B57600", B57600},
#endif
#ifdef B115200
- {"B115200", B115200},
+ {"B115200", B115200},
#endif
#ifdef B230400
- {"B230400", B230400},
+ {"B230400", B230400},
#endif
#ifdef CBAUDEX
- {"CBAUDEX", CBAUDEX},
-#endif
-
- /* tcsetattr() constants */
- {"TCSANOW", TCSANOW},
- {"TCSADRAIN", TCSADRAIN},
- {"TCSAFLUSH", TCSAFLUSH},
-
- /* tcflush() constants */
- {"TCIFLUSH", TCIFLUSH},
- {"TCOFLUSH", TCOFLUSH},
- {"TCIOFLUSH", TCIOFLUSH},
-
- /* tcflow() constants */
- {"TCOOFF", TCOOFF},
- {"TCOON", TCOON},
- {"TCIOFF", TCIOFF},
- {"TCION", TCION},
-
- /* struct termios.c_iflag constants */
- {"IGNBRK", IGNBRK},
- {"BRKINT", BRKINT},
- {"IGNPAR", IGNPAR},
- {"PARMRK", PARMRK},
- {"INPCK", INPCK},
- {"ISTRIP", ISTRIP},
- {"INLCR", INLCR},
- {"IGNCR", IGNCR},
- {"ICRNL", ICRNL},
+ {"CBAUDEX", CBAUDEX},
+#endif
+
+ /* tcsetattr() constants */
+ {"TCSANOW", TCSANOW},
+ {"TCSADRAIN", TCSADRAIN},
+ {"TCSAFLUSH", TCSAFLUSH},
+
+ /* tcflush() constants */
+ {"TCIFLUSH", TCIFLUSH},
+ {"TCOFLUSH", TCOFLUSH},
+ {"TCIOFLUSH", TCIOFLUSH},
+
+ /* tcflow() constants */
+ {"TCOOFF", TCOOFF},
+ {"TCOON", TCOON},
+ {"TCIOFF", TCIOFF},
+ {"TCION", TCION},
+
+ /* struct termios.c_iflag constants */
+ {"IGNBRK", IGNBRK},
+ {"BRKINT", BRKINT},
+ {"IGNPAR", IGNPAR},
+ {"PARMRK", PARMRK},
+ {"INPCK", INPCK},
+ {"ISTRIP", ISTRIP},
+ {"INLCR", INLCR},
+ {"IGNCR", IGNCR},
+ {"ICRNL", ICRNL},
#ifdef IUCLC
- {"IUCLC", IUCLC},
+ {"IUCLC", IUCLC},
#endif
- {"IXON", IXON},
- {"IXANY", IXANY},
- {"IXOFF", IXOFF},
+ {"IXON", IXON},
+ {"IXANY", IXANY},
+ {"IXOFF", IXOFF},
#ifdef IMAXBEL
- {"IMAXBEL", IMAXBEL},
+ {"IMAXBEL", IMAXBEL},
#endif
- /* struct termios.c_oflag constants */
- {"OPOST", OPOST},
+ /* struct termios.c_oflag constants */
+ {"OPOST", OPOST},
#ifdef OLCUC
- {"OLCUC", OLCUC},
+ {"OLCUC", OLCUC},
#endif
#ifdef ONLCR
- {"ONLCR", ONLCR},
+ {"ONLCR", ONLCR},
#endif
#ifdef OCRNL
- {"OCRNL", OCRNL},
+ {"OCRNL", OCRNL},
#endif
#ifdef ONOCR
- {"ONOCR", ONOCR},
+ {"ONOCR", ONOCR},
#endif
#ifdef ONLRET
- {"ONLRET", ONLRET},
+ {"ONLRET", ONLRET},
#endif
#ifdef OFILL
- {"OFILL", OFILL},
+ {"OFILL", OFILL},
#endif
#ifdef OFDEL
- {"OFDEL", OFDEL},
+ {"OFDEL", OFDEL},
#endif
#ifdef NLDLY
- {"NLDLY", NLDLY},
+ {"NLDLY", NLDLY},
#endif
#ifdef CRDLY
- {"CRDLY", CRDLY},
+ {"CRDLY", CRDLY},
#endif
#ifdef TABDLY
- {"TABDLY", TABDLY},
+ {"TABDLY", TABDLY},
#endif
#ifdef BSDLY
- {"BSDLY", BSDLY},
+ {"BSDLY", BSDLY},
#endif
#ifdef VTDLY
- {"VTDLY", VTDLY},
+ {"VTDLY", VTDLY},
#endif
#ifdef FFDLY
- {"FFDLY", FFDLY},
+ {"FFDLY", FFDLY},
#endif
- /* struct termios.c_oflag-related values (delay mask) */
+ /* struct termios.c_oflag-related values (delay mask) */
#ifdef NL0
- {"NL0", NL0},
+ {"NL0", NL0},
#endif
#ifdef NL1
- {"NL1", NL1},
+ {"NL1", NL1},
#endif
#ifdef CR0
- {"CR0", CR0},
+ {"CR0", CR0},
#endif
#ifdef CR1
- {"CR1", CR1},
+ {"CR1", CR1},
#endif
#ifdef CR2
- {"CR2", CR2},
+ {"CR2", CR2},
#endif
#ifdef CR3
- {"CR3", CR3},
+ {"CR3", CR3},
#endif
#ifdef TAB0
- {"TAB0", TAB0},
+ {"TAB0", TAB0},
#endif
#ifdef TAB1
- {"TAB1", TAB1},
+ {"TAB1", TAB1},
#endif
#ifdef TAB2
- {"TAB2", TAB2},
+ {"TAB2", TAB2},
#endif
#ifdef TAB3
- {"TAB3", TAB3},
+ {"TAB3", TAB3},
#endif
#ifdef XTABS
- {"XTABS", XTABS},
+ {"XTABS", XTABS},
#endif
#ifdef BS0
- {"BS0", BS0},
+ {"BS0", BS0},
#endif
#ifdef BS1
- {"BS1", BS1},
+ {"BS1", BS1},
#endif
#ifdef VT0
- {"VT0", VT0},
+ {"VT0", VT0},
#endif
#ifdef VT1
- {"VT1", VT1},
+ {"VT1", VT1},
#endif
#ifdef FF0
- {"FF0", FF0},
+ {"FF0", FF0},
#endif
#ifdef FF1
- {"FF1", FF1},
+ {"FF1", FF1},
#endif
- /* struct termios.c_cflag constants */
- {"CSIZE", CSIZE},
- {"CSTOPB", CSTOPB},
- {"CREAD", CREAD},
- {"PARENB", PARENB},
- {"PARODD", PARODD},
- {"HUPCL", HUPCL},
- {"CLOCAL", CLOCAL},
+ /* struct termios.c_cflag constants */
+ {"CSIZE", CSIZE},
+ {"CSTOPB", CSTOPB},
+ {"CREAD", CREAD},
+ {"PARENB", PARENB},
+ {"PARODD", PARODD},
+ {"HUPCL", HUPCL},
+ {"CLOCAL", CLOCAL},
#ifdef CIBAUD
- {"CIBAUD", CIBAUD},
+ {"CIBAUD", CIBAUD},
#endif
#ifdef CRTSCTS
- {"CRTSCTS", (long)CRTSCTS},
+ {"CRTSCTS", (long)CRTSCTS},
#endif
- /* struct termios.c_cflag-related values (character size) */
- {"CS5", CS5},
- {"CS6", CS6},
- {"CS7", CS7},
- {"CS8", CS8},
+ /* struct termios.c_cflag-related values (character size) */
+ {"CS5", CS5},
+ {"CS6", CS6},
+ {"CS7", CS7},
+ {"CS8", CS8},
- /* struct termios.c_lflag constants */
- {"ISIG", ISIG},
- {"ICANON", ICANON},
+ /* struct termios.c_lflag constants */
+ {"ISIG", ISIG},
+ {"ICANON", ICANON},
#ifdef XCASE
- {"XCASE", XCASE},
+ {"XCASE", XCASE},
#endif
- {"ECHO", ECHO},
- {"ECHOE", ECHOE},
- {"ECHOK", ECHOK},
- {"ECHONL", ECHONL},
+ {"ECHO", ECHO},
+ {"ECHOE", ECHOE},
+ {"ECHOK", ECHOK},
+ {"ECHONL", ECHONL},
#ifdef ECHOCTL
- {"ECHOCTL", ECHOCTL},
+ {"ECHOCTL", ECHOCTL},
#endif
#ifdef ECHOPRT
- {"ECHOPRT", ECHOPRT},
+ {"ECHOPRT", ECHOPRT},
#endif
#ifdef ECHOKE
- {"ECHOKE", ECHOKE},
+ {"ECHOKE", ECHOKE},
#endif
#ifdef FLUSHO
- {"FLUSHO", FLUSHO},
+ {"FLUSHO", FLUSHO},
#endif
- {"NOFLSH", NOFLSH},
- {"TOSTOP", TOSTOP},
+ {"NOFLSH", NOFLSH},
+ {"TOSTOP", TOSTOP},
#ifdef PENDIN
- {"PENDIN", PENDIN},
-#endif
- {"IEXTEN", IEXTEN},
-
- /* indexes into the control chars array returned by tcgetattr() */
- {"VINTR", VINTR},
- {"VQUIT", VQUIT},
- {"VERASE", VERASE},
- {"VKILL", VKILL},
- {"VEOF", VEOF},
- {"VTIME", VTIME},
- {"VMIN", VMIN},
+ {"PENDIN", PENDIN},
+#endif
+ {"IEXTEN", IEXTEN},
+
+ /* indexes into the control chars array returned by tcgetattr() */
+ {"VINTR", VINTR},
+ {"VQUIT", VQUIT},
+ {"VERASE", VERASE},
+ {"VKILL", VKILL},
+ {"VEOF", VEOF},
+ {"VTIME", VTIME},
+ {"VMIN", VMIN},
#ifdef VSWTC
- /* The #defines above ensure that if either is defined, both are,
- * but both may be omitted by the system headers. ;-( */
- {"VSWTC", VSWTC},
- {"VSWTCH", VSWTCH},
-#endif
- {"VSTART", VSTART},
- {"VSTOP", VSTOP},
- {"VSUSP", VSUSP},
- {"VEOL", VEOL},
+ /* The #defines above ensure that if either is defined, both are,
+ * but both may be omitted by the system headers. ;-( */
+ {"VSWTC", VSWTC},
+ {"VSWTCH", VSWTCH},
+#endif
+ {"VSTART", VSTART},
+ {"VSTOP", VSTOP},
+ {"VSUSP", VSUSP},
+ {"VEOL", VEOL},
#ifdef VREPRINT
- {"VREPRINT", VREPRINT},
+ {"VREPRINT", VREPRINT},
#endif
#ifdef VDISCARD
- {"VDISCARD", VDISCARD},
+ {"VDISCARD", VDISCARD},
#endif
#ifdef VWERASE
- {"VWERASE", VWERASE},
+ {"VWERASE", VWERASE},
#endif
#ifdef VLNEXT
- {"VLNEXT", VLNEXT},
+ {"VLNEXT", VLNEXT},
#endif
#ifdef VEOL2
- {"VEOL2", VEOL2},
+ {"VEOL2", VEOL2},
#endif
#ifdef B460800
- {"B460800", B460800},
+ {"B460800", B460800},
#endif
#ifdef CBAUD
- {"CBAUD", CBAUD},
+ {"CBAUD", CBAUD},
#endif
#ifdef CDEL
- {"CDEL", CDEL},
+ {"CDEL", CDEL},
#endif
#ifdef CDSUSP
- {"CDSUSP", CDSUSP},
+ {"CDSUSP", CDSUSP},
#endif
#ifdef CEOF
- {"CEOF", CEOF},
+ {"CEOF", CEOF},
#endif
#ifdef CEOL
- {"CEOL", CEOL},
+ {"CEOL", CEOL},
#endif
#ifdef CEOL2
- {"CEOL2", CEOL2},
+ {"CEOL2", CEOL2},
#endif
#ifdef CEOT
- {"CEOT", CEOT},
+ {"CEOT", CEOT},
#endif
#ifdef CERASE
- {"CERASE", CERASE},
+ {"CERASE", CERASE},
#endif
#ifdef CESC
- {"CESC", CESC},
+ {"CESC", CESC},
#endif
#ifdef CFLUSH
- {"CFLUSH", CFLUSH},
+ {"CFLUSH", CFLUSH},
#endif
#ifdef CINTR
- {"CINTR", CINTR},
+ {"CINTR", CINTR},
#endif
#ifdef CKILL
- {"CKILL", CKILL},
+ {"CKILL", CKILL},
#endif
#ifdef CLNEXT
- {"CLNEXT", CLNEXT},
+ {"CLNEXT", CLNEXT},
#endif
#ifdef CNUL
- {"CNUL", CNUL},
+ {"CNUL", CNUL},
#endif
#ifdef COMMON
- {"COMMON", COMMON},
+ {"COMMON", COMMON},
#endif
#ifdef CQUIT
- {"CQUIT", CQUIT},
+ {"CQUIT", CQUIT},
#endif
#ifdef CRPRNT
- {"CRPRNT", CRPRNT},
+ {"CRPRNT", CRPRNT},
#endif
#ifdef CSTART
- {"CSTART", CSTART},
+ {"CSTART", CSTART},
#endif
#ifdef CSTOP
- {"CSTOP", CSTOP},
+ {"CSTOP", CSTOP},
#endif
#ifdef CSUSP
- {"CSUSP", CSUSP},
+ {"CSUSP", CSUSP},
#endif
#ifdef CSWTCH
- {"CSWTCH", CSWTCH},
+ {"CSWTCH", CSWTCH},
#endif
#ifdef CWERASE
- {"CWERASE", CWERASE},
+ {"CWERASE", CWERASE},
#endif
#ifdef EXTA
- {"EXTA", EXTA},
+ {"EXTA", EXTA},
#endif
#ifdef EXTB
- {"EXTB", EXTB},
+ {"EXTB", EXTB},
#endif
#ifdef FIOASYNC
- {"FIOASYNC", FIOASYNC},
+ {"FIOASYNC", FIOASYNC},
#endif
#ifdef FIOCLEX
- {"FIOCLEX", FIOCLEX},
+ {"FIOCLEX", FIOCLEX},
#endif
#ifdef FIONBIO
- {"FIONBIO", FIONBIO},
+ {"FIONBIO", FIONBIO},
#endif
#ifdef FIONCLEX
- {"FIONCLEX", FIONCLEX},
+ {"FIONCLEX", FIONCLEX},
#endif
#ifdef FIONREAD
- {"FIONREAD", FIONREAD},
+ {"FIONREAD", FIONREAD},
#endif
#ifdef IBSHIFT
- {"IBSHIFT", IBSHIFT},
+ {"IBSHIFT", IBSHIFT},
#endif
#ifdef INIT_C_CC
- {"INIT_C_CC", INIT_C_CC},
+ {"INIT_C_CC", INIT_C_CC},
#endif
#ifdef IOCSIZE_MASK
- {"IOCSIZE_MASK", IOCSIZE_MASK},
+ {"IOCSIZE_MASK", IOCSIZE_MASK},
#endif
#ifdef IOCSIZE_SHIFT
- {"IOCSIZE_SHIFT", IOCSIZE_SHIFT},
+ {"IOCSIZE_SHIFT", IOCSIZE_SHIFT},
#endif
#ifdef NCC
- {"NCC", NCC},
+ {"NCC", NCC},
#endif
#ifdef NCCS
- {"NCCS", NCCS},
+ {"NCCS", NCCS},
#endif
#ifdef NSWTCH
- {"NSWTCH", NSWTCH},
+ {"NSWTCH", NSWTCH},
#endif
#ifdef N_MOUSE
- {"N_MOUSE", N_MOUSE},
+ {"N_MOUSE", N_MOUSE},
#endif
#ifdef N_PPP
- {"N_PPP", N_PPP},
+ {"N_PPP", N_PPP},
#endif
#ifdef N_SLIP
- {"N_SLIP", N_SLIP},
+ {"N_SLIP", N_SLIP},
#endif
#ifdef N_STRIP
- {"N_STRIP", N_STRIP},
+ {"N_STRIP", N_STRIP},
#endif
#ifdef N_TTY
- {"N_TTY", N_TTY},
+ {"N_TTY", N_TTY},
#endif
#ifdef TCFLSH
- {"TCFLSH", TCFLSH},
+ {"TCFLSH", TCFLSH},
#endif
#ifdef TCGETA
- {"TCGETA", TCGETA},
+ {"TCGETA", TCGETA},
#endif
#ifdef TCGETS
- {"TCGETS", TCGETS},
+ {"TCGETS", TCGETS},
#endif
#ifdef TCSBRK
- {"TCSBRK", TCSBRK},
+ {"TCSBRK", TCSBRK},
#endif
#ifdef TCSBRKP
- {"TCSBRKP", TCSBRKP},
+ {"TCSBRKP", TCSBRKP},
#endif
#ifdef TCSETA
- {"TCSETA", TCSETA},
+ {"TCSETA", TCSETA},
#endif
#ifdef TCSETAF
- {"TCSETAF", TCSETAF},
+ {"TCSETAF", TCSETAF},
#endif
#ifdef TCSETAW
- {"TCSETAW", TCSETAW},
+ {"TCSETAW", TCSETAW},
#endif
#ifdef TCSETS
- {"TCSETS", TCSETS},
+ {"TCSETS", TCSETS},
#endif
#ifdef TCSETSF
- {"TCSETSF", TCSETSF},
+ {"TCSETSF", TCSETSF},
#endif
#ifdef TCSETSW
- {"TCSETSW", TCSETSW},
+ {"TCSETSW", TCSETSW},
#endif
#ifdef TCXONC
- {"TCXONC", TCXONC},
+ {"TCXONC", TCXONC},
#endif
#ifdef TIOCCONS
- {"TIOCCONS", TIOCCONS},
+ {"TIOCCONS", TIOCCONS},
#endif
#ifdef TIOCEXCL
- {"TIOCEXCL", TIOCEXCL},
+ {"TIOCEXCL", TIOCEXCL},
#endif
#ifdef TIOCGETD
- {"TIOCGETD", TIOCGETD},
+ {"TIOCGETD", TIOCGETD},
#endif
#ifdef TIOCGICOUNT
- {"TIOCGICOUNT", TIOCGICOUNT},
+ {"TIOCGICOUNT", TIOCGICOUNT},
#endif
#ifdef TIOCGLCKTRMIOS
- {"TIOCGLCKTRMIOS", TIOCGLCKTRMIOS},
+ {"TIOCGLCKTRMIOS", TIOCGLCKTRMIOS},
#endif
#ifdef TIOCGPGRP
- {"TIOCGPGRP", TIOCGPGRP},
+ {"TIOCGPGRP", TIOCGPGRP},
#endif
#ifdef TIOCGSERIAL
- {"TIOCGSERIAL", TIOCGSERIAL},
+ {"TIOCGSERIAL", TIOCGSERIAL},
#endif
#ifdef TIOCGSOFTCAR
- {"TIOCGSOFTCAR", TIOCGSOFTCAR},
+ {"TIOCGSOFTCAR", TIOCGSOFTCAR},
#endif
#ifdef TIOCGWINSZ
- {"TIOCGWINSZ", TIOCGWINSZ},
+ {"TIOCGWINSZ", TIOCGWINSZ},
#endif
#ifdef TIOCINQ
- {"TIOCINQ", TIOCINQ},
+ {"TIOCINQ", TIOCINQ},
#endif
#ifdef TIOCLINUX
- {"TIOCLINUX", TIOCLINUX},
+ {"TIOCLINUX", TIOCLINUX},
#endif
#ifdef TIOCMBIC
- {"TIOCMBIC", TIOCMBIC},
+ {"TIOCMBIC", TIOCMBIC},
#endif
#ifdef TIOCMBIS
- {"TIOCMBIS", TIOCMBIS},
+ {"TIOCMBIS", TIOCMBIS},
#endif
#ifdef TIOCMGET
- {"TIOCMGET", TIOCMGET},
+ {"TIOCMGET", TIOCMGET},
#endif
#ifdef TIOCMIWAIT
- {"TIOCMIWAIT", TIOCMIWAIT},
+ {"TIOCMIWAIT", TIOCMIWAIT},
#endif
#ifdef TIOCMSET
- {"TIOCMSET", TIOCMSET},
+ {"TIOCMSET", TIOCMSET},
#endif
#ifdef TIOCM_CAR
- {"TIOCM_CAR", TIOCM_CAR},
+ {"TIOCM_CAR", TIOCM_CAR},
#endif
#ifdef TIOCM_CD
- {"TIOCM_CD", TIOCM_CD},
+ {"TIOCM_CD", TIOCM_CD},
#endif
#ifdef TIOCM_CTS
- {"TIOCM_CTS", TIOCM_CTS},
+ {"TIOCM_CTS", TIOCM_CTS},
#endif
#ifdef TIOCM_DSR
- {"TIOCM_DSR", TIOCM_DSR},
+ {"TIOCM_DSR", TIOCM_DSR},
#endif
#ifdef TIOCM_DTR
- {"TIOCM_DTR", TIOCM_DTR},
+ {"TIOCM_DTR", TIOCM_DTR},
#endif
#ifdef TIOCM_LE
- {"TIOCM_LE", TIOCM_LE},
+ {"TIOCM_LE", TIOCM_LE},
#endif
#ifdef TIOCM_RI
- {"TIOCM_RI", TIOCM_RI},
+ {"TIOCM_RI", TIOCM_RI},
#endif
#ifdef TIOCM_RNG
- {"TIOCM_RNG", TIOCM_RNG},
+ {"TIOCM_RNG", TIOCM_RNG},
#endif
#ifdef TIOCM_RTS
- {"TIOCM_RTS", TIOCM_RTS},
+ {"TIOCM_RTS", TIOCM_RTS},
#endif
#ifdef TIOCM_SR
- {"TIOCM_SR", TIOCM_SR},
+ {"TIOCM_SR", TIOCM_SR},
#endif
#ifdef TIOCM_ST
- {"TIOCM_ST", TIOCM_ST},
+ {"TIOCM_ST", TIOCM_ST},
#endif
#ifdef TIOCNOTTY
- {"TIOCNOTTY", TIOCNOTTY},
+ {"TIOCNOTTY", TIOCNOTTY},
#endif
#ifdef TIOCNXCL
- {"TIOCNXCL", TIOCNXCL},
+ {"TIOCNXCL", TIOCNXCL},
#endif
#ifdef TIOCOUTQ
- {"TIOCOUTQ", TIOCOUTQ},
+ {"TIOCOUTQ", TIOCOUTQ},
#endif
#ifdef TIOCPKT
- {"TIOCPKT", TIOCPKT},
+ {"TIOCPKT", TIOCPKT},
#endif
#ifdef TIOCPKT_DATA
- {"TIOCPKT_DATA", TIOCPKT_DATA},
+ {"TIOCPKT_DATA", TIOCPKT_DATA},
#endif
#ifdef TIOCPKT_DOSTOP
- {"TIOCPKT_DOSTOP", TIOCPKT_DOSTOP},
+ {"TIOCPKT_DOSTOP", TIOCPKT_DOSTOP},
#endif
#ifdef TIOCPKT_FLUSHREAD
- {"TIOCPKT_FLUSHREAD", TIOCPKT_FLUSHREAD},
+ {"TIOCPKT_FLUSHREAD", TIOCPKT_FLUSHREAD},
#endif
#ifdef TIOCPKT_FLUSHWRITE
- {"TIOCPKT_FLUSHWRITE", TIOCPKT_FLUSHWRITE},
+ {"TIOCPKT_FLUSHWRITE", TIOCPKT_FLUSHWRITE},
#endif
#ifdef TIOCPKT_NOSTOP
- {"TIOCPKT_NOSTOP", TIOCPKT_NOSTOP},
+ {"TIOCPKT_NOSTOP", TIOCPKT_NOSTOP},
#endif
#ifdef TIOCPKT_START
- {"TIOCPKT_START", TIOCPKT_START},
+ {"TIOCPKT_START", TIOCPKT_START},
#endif
#ifdef TIOCPKT_STOP
- {"TIOCPKT_STOP", TIOCPKT_STOP},
+ {"TIOCPKT_STOP", TIOCPKT_STOP},
#endif
#ifdef TIOCSCTTY
- {"TIOCSCTTY", TIOCSCTTY},
+ {"TIOCSCTTY", TIOCSCTTY},
#endif
#ifdef TIOCSERCONFIG
- {"TIOCSERCONFIG", TIOCSERCONFIG},
+ {"TIOCSERCONFIG", TIOCSERCONFIG},
#endif
#ifdef TIOCSERGETLSR
- {"TIOCSERGETLSR", TIOCSERGETLSR},
+ {"TIOCSERGETLSR", TIOCSERGETLSR},
#endif
#ifdef TIOCSERGETMULTI
- {"TIOCSERGETMULTI", TIOCSERGETMULTI},
+ {"TIOCSERGETMULTI", TIOCSERGETMULTI},
#endif
#ifdef TIOCSERGSTRUCT
- {"TIOCSERGSTRUCT", TIOCSERGSTRUCT},
+ {"TIOCSERGSTRUCT", TIOCSERGSTRUCT},
#endif
#ifdef TIOCSERGWILD
- {"TIOCSERGWILD", TIOCSERGWILD},
+ {"TIOCSERGWILD", TIOCSERGWILD},
#endif
#ifdef TIOCSERSETMULTI
- {"TIOCSERSETMULTI", TIOCSERSETMULTI},
+ {"TIOCSERSETMULTI", TIOCSERSETMULTI},
#endif
#ifdef TIOCSERSWILD
- {"TIOCSERSWILD", TIOCSERSWILD},
+ {"TIOCSERSWILD", TIOCSERSWILD},
#endif
#ifdef TIOCSER_TEMT
- {"TIOCSER_TEMT", TIOCSER_TEMT},
+ {"TIOCSER_TEMT", TIOCSER_TEMT},
#endif
#ifdef TIOCSETD
- {"TIOCSETD", TIOCSETD},
+ {"TIOCSETD", TIOCSETD},
#endif
#ifdef TIOCSLCKTRMIOS
- {"TIOCSLCKTRMIOS", TIOCSLCKTRMIOS},
+ {"TIOCSLCKTRMIOS", TIOCSLCKTRMIOS},
#endif
#ifdef TIOCSPGRP
- {"TIOCSPGRP", TIOCSPGRP},
+ {"TIOCSPGRP", TIOCSPGRP},
#endif
#ifdef TIOCSSERIAL
- {"TIOCSSERIAL", TIOCSSERIAL},
+ {"TIOCSSERIAL", TIOCSSERIAL},
#endif
#ifdef TIOCSSOFTCAR
- {"TIOCSSOFTCAR", TIOCSSOFTCAR},
+ {"TIOCSSOFTCAR", TIOCSSOFTCAR},
#endif
#ifdef TIOCSTI
- {"TIOCSTI", TIOCSTI},
+ {"TIOCSTI", TIOCSTI},
#endif
#ifdef TIOCSWINSZ
- {"TIOCSWINSZ", TIOCSWINSZ},
+ {"TIOCSWINSZ", TIOCSWINSZ},
#endif
#ifdef TIOCTTYGSTRUCT
- {"TIOCTTYGSTRUCT", TIOCTTYGSTRUCT},
+ {"TIOCTTYGSTRUCT", TIOCTTYGSTRUCT},
#endif
- /* sentinel */
- {NULL, 0}
+ /* sentinel */
+ {NULL, 0}
};
static struct PyModuleDef termiosmodule = {
- PyModuleDef_HEAD_INIT,
- "termios",
- termios__doc__,
- -1,
- termios_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "termios",
+ termios__doc__,
+ -1,
+ termios_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_termios(void)
{
- PyObject *m;
- struct constant *constant = termios_constants;
-
- m = PyModule_Create(&termiosmodule);
- if (m == NULL)
- return NULL;
-
- if (TermiosError == NULL) {
- TermiosError = PyErr_NewException("termios.error", NULL, NULL);
- }
- Py_INCREF(TermiosError);
- PyModule_AddObject(m, "error", TermiosError);
-
- while (constant->name != NULL) {
- PyModule_AddIntConstant(m, constant->name, constant->value);
- ++constant;
- }
- return m;
+ PyObject *m;
+ struct constant *constant = termios_constants;
+
+ m = PyModule_Create(&termiosmodule);
+ if (m == NULL)
+ return NULL;
+
+ if (TermiosError == NULL) {
+ TermiosError = PyErr_NewException("termios.error", NULL, NULL);
+ }
+ Py_INCREF(TermiosError);
+ PyModule_AddObject(m, "error", TermiosError);
+
+ while (constant->name != NULL) {
+ PyModule_AddIntConstant(m, constant->name, constant->value);
+ ++constant;
+ }
+ return m;
}
diff --git a/Modules/testcapi_long.h b/Modules/testcapi_long.h
index 60ca326..fa94fd6 100644
--- a/Modules/testcapi_long.h
+++ b/Modules/testcapi_long.h
@@ -1,182 +1,182 @@
/* Poor-man's template. Macros used:
- TESTNAME name of the test (like test_long_api_inner)
- TYPENAME the signed type (like long)
- F_S_TO_PY convert signed to pylong; TYPENAME -> PyObject*
- F_PY_TO_S convert pylong to signed; PyObject* -> TYPENAME
- F_U_TO_PY convert unsigned to pylong; unsigned TYPENAME -> PyObject*
+ TESTNAME name of the test (like test_long_api_inner)
+ TYPENAME the signed type (like long)
+ F_S_TO_PY convert signed to pylong; TYPENAME -> PyObject*
+ F_PY_TO_S convert pylong to signed; PyObject* -> TYPENAME
+ F_U_TO_PY convert unsigned to pylong; unsigned TYPENAME -> PyObject*
F_PY_TO_U convert pylong to unsigned; PyObject* -> unsigned TYPENAME
*/
static PyObject *
TESTNAME(PyObject *error(const char*))
{
- const int NBITS = sizeof(TYPENAME) * 8;
- unsigned TYPENAME base;
- PyObject *pyresult;
- int i;
-
- /* Note: This test lets PyObjects leak if an error is raised. Since
- an error should never be raised, leaks are impossible <wink>. */
-
- /* Test native -> PyLong -> native roundtrip identity.
- * Generate all powers of 2, and test them and their negations,
- * plus the numbers +-1 off from them.
- */
- base = 1;
- for (i = 0;
- i < NBITS + 1; /* on last, base overflows to 0 */
- ++i, base <<= 1)
- {
- int j;
- for (j = 0; j < 6; ++j) {
- TYPENAME in, out;
- unsigned TYPENAME uin, uout;
-
- /* For 0, 1, 2 use base; for 3, 4, 5 use -base */
- uin = j < 3 ? base
- : (unsigned TYPENAME)(-(TYPENAME)base);
-
- /* For 0 & 3, subtract 1.
- * For 1 & 4, leave alone.
- * For 2 & 5, add 1.
- */
- uin += (unsigned TYPENAME)(TYPENAME)(j % 3 - 1);
-
- pyresult = F_U_TO_PY(uin);
- if (pyresult == NULL)
- return error(
- "unsigned unexpected null result");
-
- uout = F_PY_TO_U(pyresult);
- if (uout == (unsigned TYPENAME)-1 && PyErr_Occurred())
- return error(
- "unsigned unexpected -1 result");
- if (uout != uin)
- return error(
- "unsigned output != input");
- UNBIND(pyresult);
-
- in = (TYPENAME)uin;
- pyresult = F_S_TO_PY(in);
- if (pyresult == NULL)
- return error(
- "signed unexpected null result");
-
- out = F_PY_TO_S(pyresult);
- if (out == (TYPENAME)-1 && PyErr_Occurred())
- return error(
- "signed unexpected -1 result");
- if (out != in)
- return error(
- "signed output != input");
- UNBIND(pyresult);
- }
- }
-
- /* Overflow tests. The loop above ensured that all limit cases that
- * should not overflow don't overflow, so all we need to do here is
- * provoke one-over-the-limit cases (not exhaustive, but sharp).
- */
- {
- PyObject *one, *x, *y;
- TYPENAME out;
- unsigned TYPENAME uout;
-
- one = PyLong_FromLong(1);
- if (one == NULL)
- return error(
- "unexpected NULL from PyLong_FromLong");
-
- /* Unsigned complains about -1? */
- x = PyNumber_Negative(one);
- if (x == NULL)
- return error(
- "unexpected NULL from PyNumber_Negative");
-
- uout = F_PY_TO_U(x);
- if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
- return error(
- "PyLong_AsUnsignedXXX(-1) didn't complain");
- if (!PyErr_ExceptionMatches(PyExc_OverflowError))
- return error(
- "PyLong_AsUnsignedXXX(-1) raised "
- "something other than OverflowError");
- PyErr_Clear();
- UNBIND(x);
-
- /* Unsigned complains about 2**NBITS? */
- y = PyLong_FromLong((long)NBITS);
- if (y == NULL)
- return error(
- "unexpected NULL from PyLong_FromLong");
-
- x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
- UNBIND(y);
- if (x == NULL)
- return error(
- "unexpected NULL from PyNumber_Lshift");
-
- uout = F_PY_TO_U(x);
- if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
- return error(
- "PyLong_AsUnsignedXXX(2**NBITS) didn't "
- "complain");
- if (!PyErr_ExceptionMatches(PyExc_OverflowError))
- return error(
- "PyLong_AsUnsignedXXX(2**NBITS) raised "
- "something other than OverflowError");
- PyErr_Clear();
-
- /* Signed complains about 2**(NBITS-1)?
- x still has 2**NBITS. */
- y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
- UNBIND(x);
- if (y == NULL)
- return error(
- "unexpected NULL from PyNumber_Rshift");
-
- out = F_PY_TO_S(y);
- if (out != (TYPENAME)-1 || !PyErr_Occurred())
- return error(
- "PyLong_AsXXX(2**(NBITS-1)) didn't "
- "complain");
- if (!PyErr_ExceptionMatches(PyExc_OverflowError))
- return error(
- "PyLong_AsXXX(2**(NBITS-1)) raised "
- "something other than OverflowError");
- PyErr_Clear();
-
- /* Signed complains about -2**(NBITS-1)-1?;
- y still has 2**(NBITS-1). */
- x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
- UNBIND(y);
- if (x == NULL)
- return error(
- "unexpected NULL from PyNumber_Negative");
-
- y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
- UNBIND(x);
- if (y == NULL)
- return error(
- "unexpected NULL from PyNumber_Subtract");
-
- out = F_PY_TO_S(y);
- if (out != (TYPENAME)-1 || !PyErr_Occurred())
- return error(
- "PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
- "complain");
- if (!PyErr_ExceptionMatches(PyExc_OverflowError))
- return error(
- "PyLong_AsXXX(-2**(NBITS-1)-1) raised "
- "something other than OverflowError");
- PyErr_Clear();
- UNBIND(y);
-
- Py_XDECREF(x);
- Py_XDECREF(y);
- Py_DECREF(one);
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ const int NBITS = sizeof(TYPENAME) * 8;
+ unsigned TYPENAME base;
+ PyObject *pyresult;
+ int i;
+
+ /* Note: This test lets PyObjects leak if an error is raised. Since
+ an error should never be raised, leaks are impossible <wink>. */
+
+ /* Test native -> PyLong -> native roundtrip identity.
+ * Generate all powers of 2, and test them and their negations,
+ * plus the numbers +-1 off from them.
+ */
+ base = 1;
+ for (i = 0;
+ i < NBITS + 1; /* on last, base overflows to 0 */
+ ++i, base <<= 1)
+ {
+ int j;
+ for (j = 0; j < 6; ++j) {
+ TYPENAME in, out;
+ unsigned TYPENAME uin, uout;
+
+ /* For 0, 1, 2 use base; for 3, 4, 5 use -base */
+ uin = j < 3 ? base
+ : (unsigned TYPENAME)(-(TYPENAME)base);
+
+ /* For 0 & 3, subtract 1.
+ * For 1 & 4, leave alone.
+ * For 2 & 5, add 1.
+ */
+ uin += (unsigned TYPENAME)(TYPENAME)(j % 3 - 1);
+
+ pyresult = F_U_TO_PY(uin);
+ if (pyresult == NULL)
+ return error(
+ "unsigned unexpected null result");
+
+ uout = F_PY_TO_U(pyresult);
+ if (uout == (unsigned TYPENAME)-1 && PyErr_Occurred())
+ return error(
+ "unsigned unexpected -1 result");
+ if (uout != uin)
+ return error(
+ "unsigned output != input");
+ UNBIND(pyresult);
+
+ in = (TYPENAME)uin;
+ pyresult = F_S_TO_PY(in);
+ if (pyresult == NULL)
+ return error(
+ "signed unexpected null result");
+
+ out = F_PY_TO_S(pyresult);
+ if (out == (TYPENAME)-1 && PyErr_Occurred())
+ return error(
+ "signed unexpected -1 result");
+ if (out != in)
+ return error(
+ "signed output != input");
+ UNBIND(pyresult);
+ }
+ }
+
+ /* Overflow tests. The loop above ensured that all limit cases that
+ * should not overflow don't overflow, so all we need to do here is
+ * provoke one-over-the-limit cases (not exhaustive, but sharp).
+ */
+ {
+ PyObject *one, *x, *y;
+ TYPENAME out;
+ unsigned TYPENAME uout;
+
+ one = PyLong_FromLong(1);
+ if (one == NULL)
+ return error(
+ "unexpected NULL from PyLong_FromLong");
+
+ /* Unsigned complains about -1? */
+ x = PyNumber_Negative(one);
+ if (x == NULL)
+ return error(
+ "unexpected NULL from PyNumber_Negative");
+
+ uout = F_PY_TO_U(x);
+ if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
+ return error(
+ "PyLong_AsUnsignedXXX(-1) didn't complain");
+ if (!PyErr_ExceptionMatches(PyExc_OverflowError))
+ return error(
+ "PyLong_AsUnsignedXXX(-1) raised "
+ "something other than OverflowError");
+ PyErr_Clear();
+ UNBIND(x);
+
+ /* Unsigned complains about 2**NBITS? */
+ y = PyLong_FromLong((long)NBITS);
+ if (y == NULL)
+ return error(
+ "unexpected NULL from PyLong_FromLong");
+
+ x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
+ UNBIND(y);
+ if (x == NULL)
+ return error(
+ "unexpected NULL from PyNumber_Lshift");
+
+ uout = F_PY_TO_U(x);
+ if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
+ return error(
+ "PyLong_AsUnsignedXXX(2**NBITS) didn't "
+ "complain");
+ if (!PyErr_ExceptionMatches(PyExc_OverflowError))
+ return error(
+ "PyLong_AsUnsignedXXX(2**NBITS) raised "
+ "something other than OverflowError");
+ PyErr_Clear();
+
+ /* Signed complains about 2**(NBITS-1)?
+ x still has 2**NBITS. */
+ y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
+ UNBIND(x);
+ if (y == NULL)
+ return error(
+ "unexpected NULL from PyNumber_Rshift");
+
+ out = F_PY_TO_S(y);
+ if (out != (TYPENAME)-1 || !PyErr_Occurred())
+ return error(
+ "PyLong_AsXXX(2**(NBITS-1)) didn't "
+ "complain");
+ if (!PyErr_ExceptionMatches(PyExc_OverflowError))
+ return error(
+ "PyLong_AsXXX(2**(NBITS-1)) raised "
+ "something other than OverflowError");
+ PyErr_Clear();
+
+ /* Signed complains about -2**(NBITS-1)-1?;
+ y still has 2**(NBITS-1). */
+ x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
+ UNBIND(y);
+ if (x == NULL)
+ return error(
+ "unexpected NULL from PyNumber_Negative");
+
+ y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
+ UNBIND(x);
+ if (y == NULL)
+ return error(
+ "unexpected NULL from PyNumber_Subtract");
+
+ out = F_PY_TO_S(y);
+ if (out != (TYPENAME)-1 || !PyErr_Occurred())
+ return error(
+ "PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
+ "complain");
+ if (!PyErr_ExceptionMatches(PyExc_OverflowError))
+ return error(
+ "PyLong_AsXXX(-2**(NBITS-1)-1) raised "
+ "something other than OverflowError");
+ PyErr_Clear();
+ UNBIND(y);
+
+ Py_XDECREF(x);
+ Py_XDECREF(y);
+ Py_DECREF(one);
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index d8db409..abc43a2 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -48,12 +48,12 @@ extern int ftime(struct timeb *);
static HANDLE hInterruptEvent = NULL;
static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
{
- SetEvent(hInterruptEvent);
- /* allow other default handlers to be called.
- Default Python handler will setup the
- KeyboardInterrupt exception.
- */
- return FALSE;
+ SetEvent(hInterruptEvent);
+ /* allow other default handlers to be called.
+ Default Python handler will setup the
+ KeyboardInterrupt exception.
+ */
+ return FALSE;
}
static long main_thread;
@@ -94,38 +94,38 @@ static PyObject *moddict;
time_t
_PyTime_DoubleToTimet(double x)
{
- time_t result;
- double diff;
-
- result = (time_t)x;
- /* How much info did we lose? time_t may be an integral or
- * floating type, and we don't know which. If it's integral,
- * we don't know whether C truncates, rounds, returns the floor,
- * etc. If we lost a second or more, the C rounding is
- * unreasonable, or the input just doesn't fit in a time_t;
- * call it an error regardless. Note that the original cast to
- * time_t can cause a C error too, but nothing we can do to
- * worm around that.
- */
- diff = x - (double)result;
- if (diff <= -1.0 || diff >= 1.0) {
- PyErr_SetString(PyExc_ValueError,
- "timestamp out of range for platform time_t");
- result = (time_t)-1;
- }
- return result;
+ time_t result;
+ double diff;
+
+ result = (time_t)x;
+ /* How much info did we lose? time_t may be an integral or
+ * floating type, and we don't know which. If it's integral,
+ * we don't know whether C truncates, rounds, returns the floor,
+ * etc. If we lost a second or more, the C rounding is
+ * unreasonable, or the input just doesn't fit in a time_t;
+ * call it an error regardless. Note that the original cast to
+ * time_t can cause a C error too, but nothing we can do to
+ * worm around that.
+ */
+ diff = x - (double)result;
+ if (diff <= -1.0 || diff >= 1.0) {
+ PyErr_SetString(PyExc_ValueError,
+ "timestamp out of range for platform time_t");
+ result = (time_t)-1;
+ }
+ return result;
}
static PyObject *
time_time(PyObject *self, PyObject *unused)
{
- double secs;
- secs = floattime();
- if (secs == 0.0) {
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
- return PyFloat_FromDouble(secs);
+ double secs;
+ secs = floattime();
+ if (secs == 0.0) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+ return PyFloat_FromDouble(secs);
}
PyDoc_STRVAR(time_doc,
@@ -147,7 +147,7 @@ Fractions of a second may be present if the system clock provides them.");
static PyObject *
time_clock(PyObject *self, PyObject *unused)
{
- return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
+ return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
}
#endif /* HAVE_CLOCK */
@@ -156,25 +156,25 @@ time_clock(PyObject *self, PyObject *unused)
static PyObject *
time_clock(PyObject *self, PyObject *unused)
{
- static LARGE_INTEGER ctrStart;
- static double divisor = 0.0;
- LARGE_INTEGER now;
- double diff;
-
- if (divisor == 0.0) {
- LARGE_INTEGER freq;
- QueryPerformanceCounter(&ctrStart);
- if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
- /* Unlikely to happen - this works on all intel
- machines at least! Revert to clock() */
- return PyFloat_FromDouble(((double)clock()) /
- CLOCKS_PER_SEC);
- }
- divisor = (double)freq.QuadPart;
- }
- QueryPerformanceCounter(&now);
- diff = (double)(now.QuadPart - ctrStart.QuadPart);
- return PyFloat_FromDouble(diff / divisor);
+ static LARGE_INTEGER ctrStart;
+ static double divisor = 0.0;
+ LARGE_INTEGER now;
+ double diff;
+
+ if (divisor == 0.0) {
+ LARGE_INTEGER freq;
+ QueryPerformanceCounter(&ctrStart);
+ if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
+ /* Unlikely to happen - this works on all intel
+ machines at least! Revert to clock() */
+ return PyFloat_FromDouble(((double)clock()) /
+ CLOCKS_PER_SEC);
+ }
+ divisor = (double)freq.QuadPart;
+ }
+ QueryPerformanceCounter(&now);
+ diff = (double)(now.QuadPart - ctrStart.QuadPart);
+ return PyFloat_FromDouble(diff / divisor);
}
#define HAVE_CLOCK /* So it gets included in the methods */
@@ -192,13 +192,13 @@ records.");
static PyObject *
time_sleep(PyObject *self, PyObject *args)
{
- double secs;
- if (!PyArg_ParseTuple(args, "d:sleep", &secs))
- return NULL;
- if (floatsleep(secs) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ double secs;
+ if (!PyArg_ParseTuple(args, "d:sleep", &secs))
+ return NULL;
+ if (floatsleep(secs) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(sleep_doc,
@@ -208,23 +208,23 @@ Delay execution for a given number of seconds. The argument may be\n\
a floating point number for subsecond precision.");
static PyStructSequence_Field struct_time_type_fields[] = {
- {"tm_year", NULL},
- {"tm_mon", NULL},
- {"tm_mday", NULL},
- {"tm_hour", NULL},
- {"tm_min", NULL},
- {"tm_sec", NULL},
- {"tm_wday", NULL},
- {"tm_yday", NULL},
- {"tm_isdst", NULL},
- {0}
+ {"tm_year", NULL},
+ {"tm_mon", NULL},
+ {"tm_mday", NULL},
+ {"tm_hour", NULL},
+ {"tm_min", NULL},
+ {"tm_sec", NULL},
+ {"tm_wday", NULL},
+ {"tm_yday", NULL},
+ {"tm_isdst", NULL},
+ {0}
};
static PyStructSequence_Desc struct_time_type_desc = {
- "time.struct_time",
- NULL,
- struct_time_type_fields,
- 9,
+ "time.struct_time",
+ NULL,
+ struct_time_type_fields,
+ 9,
};
static int initialized;
@@ -233,71 +233,71 @@ static PyTypeObject StructTimeType;
static PyObject *
tmtotuple(struct tm *p)
{
- PyObject *v = PyStructSequence_New(&StructTimeType);
- if (v == NULL)
- return NULL;
+ PyObject *v = PyStructSequence_New(&StructTimeType);
+ if (v == NULL)
+ return NULL;
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
- SET(0, p->tm_year + 1900);
- SET(1, p->tm_mon + 1); /* Want January == 1 */
- SET(2, p->tm_mday);
- SET(3, p->tm_hour);
- SET(4, p->tm_min);
- SET(5, p->tm_sec);
- SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
- SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
- SET(8, p->tm_isdst);
+ SET(0, p->tm_year + 1900);
+ SET(1, p->tm_mon + 1); /* Want January == 1 */
+ SET(2, p->tm_mday);
+ SET(3, p->tm_hour);
+ SET(4, p->tm_min);
+ SET(5, p->tm_sec);
+ SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
+ SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
+ SET(8, p->tm_isdst);
#undef SET
- if (PyErr_Occurred()) {
- Py_XDECREF(v);
- return NULL;
- }
+ if (PyErr_Occurred()) {
+ Py_XDECREF(v);
+ return NULL;
+ }
- return v;
+ return v;
}
static PyObject *
structtime_totuple(PyObject *t)
{
- PyObject *x = NULL;
- unsigned int i;
- PyObject *v = PyTuple_New(9);
- if (v == NULL)
- return NULL;
-
- for (i=0; i<9; i++) {
- x = PyStructSequence_GET_ITEM(t, i);
- Py_INCREF(x);
- PyTuple_SET_ITEM(v, i, x);
- }
-
- if (PyErr_Occurred()) {
- Py_XDECREF(v);
- return NULL;
- }
-
- return v;
+ PyObject *x = NULL;
+ unsigned int i;
+ PyObject *v = PyTuple_New(9);
+ if (v == NULL)
+ return NULL;
+
+ for (i=0; i<9; i++) {
+ x = PyStructSequence_GET_ITEM(t, i);
+ Py_INCREF(x);
+ PyTuple_SET_ITEM(v, i, x);
+ }
+
+ if (PyErr_Occurred()) {
+ Py_XDECREF(v);
+ return NULL;
+ }
+
+ return v;
}
static PyObject *
time_convert(double when, struct tm * (*function)(const time_t *))
{
- struct tm *p;
- time_t whent = _PyTime_DoubleToTimet(when);
-
- if (whent == (time_t)-1 && PyErr_Occurred())
- return NULL;
- errno = 0;
- p = function(&whent);
- if (p == NULL) {
+ struct tm *p;
+ time_t whent = _PyTime_DoubleToTimet(when);
+
+ if (whent == (time_t)-1 && PyErr_Occurred())
+ return NULL;
+ errno = 0;
+ p = function(&whent);
+ if (p == NULL) {
#ifdef EINVAL
- if (errno == 0)
- errno = EINVAL;
+ if (errno == 0)
+ errno = EINVAL;
#endif
- return PyErr_SetFromErrno(PyExc_ValueError);
- }
- return tmtotuple(p);
+ return PyErr_SetFromErrno(PyExc_ValueError);
+ }
+ return tmtotuple(p);
}
/* Parse arg tuple that can contain an optional float-or-None value;
@@ -307,28 +307,28 @@ time_convert(double when, struct tm * (*function)(const time_t *))
static int
parse_time_double_args(PyObject *args, char *format, double *pwhen)
{
- PyObject *ot = NULL;
-
- if (!PyArg_ParseTuple(args, format, &ot))
- return 0;
- if (ot == NULL || ot == Py_None)
- *pwhen = floattime();
- else {
- double when = PyFloat_AsDouble(ot);
- if (PyErr_Occurred())
- return 0;
- *pwhen = when;
- }
- return 1;
+ PyObject *ot = NULL;
+
+ if (!PyArg_ParseTuple(args, format, &ot))
+ return 0;
+ if (ot == NULL || ot == Py_None)
+ *pwhen = floattime();
+ else {
+ double when = PyFloat_AsDouble(ot);
+ if (PyErr_Occurred())
+ return 0;
+ *pwhen = when;
+ }
+ return 1;
}
static PyObject *
time_gmtime(PyObject *self, PyObject *args)
{
- double when;
- if (!parse_time_double_args(args, "|O:gmtime", &when))
- return NULL;
- return time_convert(when, gmtime);
+ double when;
+ if (!parse_time_double_args(args, "|O:gmtime", &when))
+ return NULL;
+ return time_convert(when, gmtime);
}
PyDoc_STRVAR(gmtime_doc,
@@ -341,15 +341,15 @@ GMT). When 'seconds' is not passed in, convert the current time instead.");
static PyObject *
time_localtime(PyObject *self, PyObject *args)
{
- double when;
- if (!parse_time_double_args(args, "|O:localtime", &when))
- return NULL;
- return time_convert(when, localtime);
+ double when;
+ if (!parse_time_double_args(args, "|O:localtime", &when))
+ return NULL;
+ return time_convert(when, localtime);
}
PyDoc_STRVAR(localtime_doc,
"localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
- tm_sec,tm_wday,tm_yday,tm_isdst)\n\
+ tm_sec,tm_wday,tm_yday,tm_isdst)\n\
\n\
Convert seconds since the Epoch to a time tuple expressing local time.\n\
When 'seconds' is not passed in, convert the current time instead.");
@@ -357,63 +357,63 @@ When 'seconds' is not passed in, convert the current time instead.");
static int
gettmarg(PyObject *args, struct tm *p)
{
- int y;
- PyObject *t = NULL;
-
- memset((void *) p, '\0', sizeof(struct tm));
-
- if (PyTuple_Check(args)) {
- t = args;
- Py_INCREF(t);
- }
- else if (Py_TYPE(args) == &StructTimeType) {
- t = structtime_totuple(args);
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "Tuple or struct_time argument required");
- return 0;
- }
-
- if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
- &y,
- &p->tm_mon,
- &p->tm_mday,
- &p->tm_hour,
- &p->tm_min,
- &p->tm_sec,
- &p->tm_wday,
- &p->tm_yday,
- &p->tm_isdst)) {
- Py_XDECREF(t);
- return 0;
- }
- Py_DECREF(t);
-
- if (y < 1900) {
- PyObject *accept = PyDict_GetItemString(moddict,
- "accept2dyear");
- if (accept == NULL || !PyLong_CheckExact(accept) ||
- !PyObject_IsTrue(accept)) {
- PyErr_SetString(PyExc_ValueError,
- "year >= 1900 required");
- return 0;
- }
- if (69 <= y && y <= 99)
- y += 1900;
- else if (0 <= y && y <= 68)
- y += 2000;
- else {
- PyErr_SetString(PyExc_ValueError,
- "year out of range");
- return 0;
- }
- }
- p->tm_year = y - 1900;
- p->tm_mon--;
- p->tm_wday = (p->tm_wday + 1) % 7;
- p->tm_yday--;
- return 1;
+ int y;
+ PyObject *t = NULL;
+
+ memset((void *) p, '\0', sizeof(struct tm));
+
+ if (PyTuple_Check(args)) {
+ t = args;
+ Py_INCREF(t);
+ }
+ else if (Py_TYPE(args) == &StructTimeType) {
+ t = structtime_totuple(args);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "Tuple or struct_time argument required");
+ return 0;
+ }
+
+ if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
+ &y,
+ &p->tm_mon,
+ &p->tm_mday,
+ &p->tm_hour,
+ &p->tm_min,
+ &p->tm_sec,
+ &p->tm_wday,
+ &p->tm_yday,
+ &p->tm_isdst)) {
+ Py_XDECREF(t);
+ return 0;
+ }
+ Py_DECREF(t);
+
+ if (y < 1900) {
+ PyObject *accept = PyDict_GetItemString(moddict,
+ "accept2dyear");
+ if (accept == NULL || !PyLong_CheckExact(accept) ||
+ !PyObject_IsTrue(accept)) {
+ PyErr_SetString(PyExc_ValueError,
+ "year >= 1900 required");
+ return 0;
+ }
+ if (69 <= y && y <= 99)
+ y += 1900;
+ else if (0 <= y && y <= 68)
+ y += 2000;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "year out of range");
+ return 0;
+ }
+ }
+ p->tm_year = y - 1900;
+ p->tm_mon--;
+ p->tm_wday = (p->tm_wday + 1) % 7;
+ p->tm_yday--;
+ return 1;
}
#ifdef HAVE_STRFTIME
@@ -430,172 +430,172 @@ gettmarg(PyObject *args, struct tm *p)
static PyObject *
time_strftime(PyObject *self, PyObject *args)
{
- PyObject *tup = NULL;
- struct tm buf;
- const time_char *fmt;
- PyObject *format, *tmpfmt;
- size_t fmtlen, buflen;
- time_char *outbuf = 0;
- size_t i;
-
- memset((void *) &buf, '\0', sizeof(buf));
-
- /* Will always expect a unicode string to be passed as format.
- Given that there's no str type anymore in py3k this seems safe.
- */
- if (!PyArg_ParseTuple(args, "U|O:strftime", &format, &tup))
- return NULL;
-
- if (tup == NULL) {
- time_t tt = time(NULL);
- buf = *localtime(&tt);
- } else if (!gettmarg(tup, &buf))
- return NULL;
-
- /* Checks added to make sure strftime() does not crash Python by
- indexing blindly into some array for a textual representation
- by some bad index (fixes bug #897625).
-
- Also support values of zero from Python code for arguments in which
- that is out of range by forcing that value to the lowest value that
- is valid (fixed bug #1520914).
-
- Valid ranges based on what is allowed in struct tm:
-
- - tm_year: [0, max(int)] (1)
- - tm_mon: [0, 11] (2)
- - tm_mday: [1, 31]
- - tm_hour: [0, 23]
- - tm_min: [0, 59]
- - tm_sec: [0, 60]
- - tm_wday: [0, 6] (1)
- - tm_yday: [0, 365] (2)
- - tm_isdst: [-max(int), max(int)]
-
- (1) gettmarg() handles bounds-checking.
- (2) Python's acceptable range is one greater than the range in C,
- thus need to check against automatic decrement by gettmarg().
- */
- if (buf.tm_mon == -1)
- buf.tm_mon = 0;
- else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
- PyErr_SetString(PyExc_ValueError, "month out of range");
- return NULL;
- }
- if (buf.tm_mday == 0)
- buf.tm_mday = 1;
- else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
- PyErr_SetString(PyExc_ValueError, "day of month out of range");
- return NULL;
- }
- if (buf.tm_hour < 0 || buf.tm_hour > 23) {
- PyErr_SetString(PyExc_ValueError, "hour out of range");
- return NULL;
- }
- if (buf.tm_min < 0 || buf.tm_min > 59) {
- PyErr_SetString(PyExc_ValueError, "minute out of range");
- return NULL;
- }
- if (buf.tm_sec < 0 || buf.tm_sec > 61) {
- PyErr_SetString(PyExc_ValueError, "seconds out of range");
- return NULL;
- }
- /* tm_wday does not need checking of its upper-bound since taking
- ``% 7`` in gettmarg() automatically restricts the range. */
- if (buf.tm_wday < 0) {
- PyErr_SetString(PyExc_ValueError, "day of week out of range");
- return NULL;
- }
- if (buf.tm_yday == -1)
- buf.tm_yday = 0;
- else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
- PyErr_SetString(PyExc_ValueError, "day of year out of range");
- return NULL;
- }
- if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
- PyErr_SetString(PyExc_ValueError,
- "daylight savings flag out of range");
- return NULL;
- }
+ PyObject *tup = NULL;
+ struct tm buf;
+ const time_char *fmt;
+ PyObject *format, *tmpfmt;
+ size_t fmtlen, buflen;
+ time_char *outbuf = 0;
+ size_t i;
+
+ memset((void *) &buf, '\0', sizeof(buf));
+
+ /* Will always expect a unicode string to be passed as format.
+ Given that there's no str type anymore in py3k this seems safe.
+ */
+ if (!PyArg_ParseTuple(args, "U|O:strftime", &format, &tup))
+ return NULL;
+
+ if (tup == NULL) {
+ time_t tt = time(NULL);
+ buf = *localtime(&tt);
+ } else if (!gettmarg(tup, &buf))
+ return NULL;
+
+ /* Checks added to make sure strftime() does not crash Python by
+ indexing blindly into some array for a textual representation
+ by some bad index (fixes bug #897625).
+
+ Also support values of zero from Python code for arguments in which
+ that is out of range by forcing that value to the lowest value that
+ is valid (fixed bug #1520914).
+
+ Valid ranges based on what is allowed in struct tm:
+
+ - tm_year: [0, max(int)] (1)
+ - tm_mon: [0, 11] (2)
+ - tm_mday: [1, 31]
+ - tm_hour: [0, 23]
+ - tm_min: [0, 59]
+ - tm_sec: [0, 60]
+ - tm_wday: [0, 6] (1)
+ - tm_yday: [0, 365] (2)
+ - tm_isdst: [-max(int), max(int)]
+
+ (1) gettmarg() handles bounds-checking.
+ (2) Python's acceptable range is one greater than the range in C,
+ thus need to check against automatic decrement by gettmarg().
+ */
+ if (buf.tm_mon == -1)
+ buf.tm_mon = 0;
+ else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
+ PyErr_SetString(PyExc_ValueError, "month out of range");
+ return NULL;
+ }
+ if (buf.tm_mday == 0)
+ buf.tm_mday = 1;
+ else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
+ PyErr_SetString(PyExc_ValueError, "day of month out of range");
+ return NULL;
+ }
+ if (buf.tm_hour < 0 || buf.tm_hour > 23) {
+ PyErr_SetString(PyExc_ValueError, "hour out of range");
+ return NULL;
+ }
+ if (buf.tm_min < 0 || buf.tm_min > 59) {
+ PyErr_SetString(PyExc_ValueError, "minute out of range");
+ return NULL;
+ }
+ if (buf.tm_sec < 0 || buf.tm_sec > 61) {
+ PyErr_SetString(PyExc_ValueError, "seconds out of range");
+ return NULL;
+ }
+ /* tm_wday does not need checking of its upper-bound since taking
+ ``% 7`` in gettmarg() automatically restricts the range. */
+ if (buf.tm_wday < 0) {
+ PyErr_SetString(PyExc_ValueError, "day of week out of range");
+ return NULL;
+ }
+ if (buf.tm_yday == -1)
+ buf.tm_yday = 0;
+ else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
+ PyErr_SetString(PyExc_ValueError, "day of year out of range");
+ return NULL;
+ }
+ if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
+ PyErr_SetString(PyExc_ValueError,
+ "daylight savings flag out of range");
+ return NULL;
+ }
#ifdef HAVE_WCSFTIME
- tmpfmt = PyBytes_FromStringAndSize(NULL,
- sizeof(wchar_t) * (PyUnicode_GetSize(format)+1));
- if (!tmpfmt)
- return NULL;
- /* This assumes that PyUnicode_AsWideChar doesn't do any UTF-16
- expansion. */
- if (PyUnicode_AsWideChar((PyUnicodeObject*)format,
- (wchar_t*)PyBytes_AS_STRING(tmpfmt),
- PyUnicode_GetSize(format)+1) == (size_t)-1)
- /* This shouldn't fail. */
- Py_FatalError("PyUnicode_AsWideChar failed");
- format = tmpfmt;
- fmt = (wchar_t*)PyBytes_AS_STRING(format);
+ tmpfmt = PyBytes_FromStringAndSize(NULL,
+ sizeof(wchar_t) * (PyUnicode_GetSize(format)+1));
+ if (!tmpfmt)
+ return NULL;
+ /* This assumes that PyUnicode_AsWideChar doesn't do any UTF-16
+ expansion. */
+ if (PyUnicode_AsWideChar((PyUnicodeObject*)format,
+ (wchar_t*)PyBytes_AS_STRING(tmpfmt),
+ PyUnicode_GetSize(format)+1) == (size_t)-1)
+ /* This shouldn't fail. */
+ Py_FatalError("PyUnicode_AsWideChar failed");
+ format = tmpfmt;
+ fmt = (wchar_t*)PyBytes_AS_STRING(format);
#else
- /* Convert the unicode string to an ascii one */
- format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL);
- if (format == NULL)
- return NULL;
- fmt = PyBytes_AS_STRING(format);
+ /* Convert the unicode string to an ascii one */
+ format = PyUnicode_AsEncodedString(format, TZNAME_ENCODING, NULL);
+ if (format == NULL)
+ return NULL;
+ fmt = PyBytes_AS_STRING(format);
#endif
#if defined(MS_WINDOWS) && defined(HAVE_WCSFTIME)
- /* check that the format string contains only valid directives */
- for(outbuf = wcschr(fmt, L'%');
- outbuf != NULL;
- outbuf = wcschr(outbuf+2, L'%'))
- {
- if (outbuf[1]=='#')
- ++outbuf; /* not documented by python, */
- if (outbuf[1]=='\0' ||
- !wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1]))
- {
- PyErr_SetString(PyExc_ValueError, "Invalid format string");
- return 0;
- }
- }
+ /* check that the format string contains only valid directives */
+ for(outbuf = wcschr(fmt, L'%');
+ outbuf != NULL;
+ outbuf = wcschr(outbuf+2, L'%'))
+ {
+ if (outbuf[1]=='#')
+ ++outbuf; /* not documented by python, */
+ if (outbuf[1]=='\0' ||
+ !wcschr(L"aAbBcdfHIjmMpSUwWxXyYzZ%", outbuf[1]))
+ {
+ PyErr_SetString(PyExc_ValueError, "Invalid format string");
+ return 0;
+ }
+ }
#endif
- fmtlen = time_strlen(fmt);
-
- /* I hate these functions that presume you know how big the output
- * will be ahead of time...
- */
- for (i = 1024; ; i += i) {
- outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
- if (outbuf == NULL) {
- Py_DECREF(format);
- return PyErr_NoMemory();
- }
- buflen = format_time(outbuf, i, fmt, &buf);
- if (buflen > 0 || i >= 256 * fmtlen) {
- /* If the buffer is 256 times as long as the format,
- it's probably not failing for lack of room!
- More likely, the format yields an empty result,
- e.g. an empty format, or %Z when the timezone
- is unknown. */
- PyObject *ret;
+ fmtlen = time_strlen(fmt);
+
+ /* I hate these functions that presume you know how big the output
+ * will be ahead of time...
+ */
+ for (i = 1024; ; i += i) {
+ outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
+ if (outbuf == NULL) {
+ Py_DECREF(format);
+ return PyErr_NoMemory();
+ }
+ buflen = format_time(outbuf, i, fmt, &buf);
+ if (buflen > 0 || i >= 256 * fmtlen) {
+ /* If the buffer is 256 times as long as the format,
+ it's probably not failing for lack of room!
+ More likely, the format yields an empty result,
+ e.g. an empty format, or %Z when the timezone
+ is unknown. */
+ PyObject *ret;
#ifdef HAVE_WCSFTIME
- ret = PyUnicode_FromWideChar(outbuf, buflen);
+ ret = PyUnicode_FromWideChar(outbuf, buflen);
#else
- ret = PyUnicode_Decode(outbuf, buflen,
- TZNAME_ENCODING, NULL);
+ ret = PyUnicode_Decode(outbuf, buflen,
+ TZNAME_ENCODING, NULL);
#endif
- PyMem_Free(outbuf);
- Py_DECREF(format);
- return ret;
- }
- PyMem_Free(outbuf);
+ PyMem_Free(outbuf);
+ Py_DECREF(format);
+ return ret;
+ }
+ PyMem_Free(outbuf);
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
- /* VisualStudio .NET 2005 does this properly */
- if (buflen == 0 && errno == EINVAL) {
- PyErr_SetString(PyExc_ValueError, "Invalid format string");
- Py_DECREF(format);
- return 0;
- }
+ /* VisualStudio .NET 2005 does this properly */
+ if (buflen == 0 && errno == EINVAL) {
+ PyErr_SetString(PyExc_ValueError, "Invalid format string");
+ Py_DECREF(format);
+ return 0;
+ }
#endif
- }
+ }
}
#undef time_char
@@ -616,7 +616,7 @@ time_strptime(PyObject *self, PyObject *args)
PyObject *strptime_result;
if (!strptime_module)
- return NULL;
+ return NULL;
strptime_result = PyObject_CallMethod(strptime_module, "_strptime_time", "O", args);
Py_DECREF(strptime_module);
return strptime_result;
@@ -632,20 +632,20 @@ See the library reference manual for formatting codes (same as strftime()).");
static PyObject *
time_asctime(PyObject *self, PyObject *args)
{
- PyObject *tup = NULL;
- struct tm buf;
- char *p;
- if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
- return NULL;
- if (tup == NULL) {
- time_t tt = time(NULL);
- buf = *localtime(&tt);
- } else if (!gettmarg(tup, &buf))
- return NULL;
- p = asctime(&buf);
- if (p[24] == '\n')
- p[24] = '\0';
- return PyUnicode_FromString(p);
+ PyObject *tup = NULL;
+ struct tm buf;
+ char *p;
+ if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
+ return NULL;
+ if (tup == NULL) {
+ time_t tt = time(NULL);
+ buf = *localtime(&tt);
+ } else if (!gettmarg(tup, &buf))
+ return NULL;
+ p = asctime(&buf);
+ if (p[24] == '\n')
+ p[24] = '\0';
+ return PyUnicode_FromString(p);
}
PyDoc_STRVAR(asctime_doc,
@@ -658,30 +658,30 @@ is used.");
static PyObject *
time_ctime(PyObject *self, PyObject *args)
{
- PyObject *ot = NULL;
- time_t tt;
- char *p;
-
- if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
- return NULL;
- if (ot == NULL || ot == Py_None)
- tt = time(NULL);
- else {
- double dt = PyFloat_AsDouble(ot);
- if (PyErr_Occurred())
- return NULL;
- tt = _PyTime_DoubleToTimet(dt);
- if (tt == (time_t)-1 && PyErr_Occurred())
- return NULL;
- }
- p = ctime(&tt);
- if (p == NULL) {
- PyErr_SetString(PyExc_ValueError, "unconvertible time");
- return NULL;
- }
- if (p[24] == '\n')
- p[24] = '\0';
- return PyUnicode_FromString(p);
+ PyObject *ot = NULL;
+ time_t tt;
+ char *p;
+
+ if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
+ return NULL;
+ if (ot == NULL || ot == Py_None)
+ tt = time(NULL);
+ else {
+ double dt = PyFloat_AsDouble(ot);
+ if (PyErr_Occurred())
+ return NULL;
+ tt = _PyTime_DoubleToTimet(dt);
+ if (tt == (time_t)-1 && PyErr_Occurred())
+ return NULL;
+ }
+ p = ctime(&tt);
+ if (p == NULL) {
+ PyErr_SetString(PyExc_ValueError, "unconvertible time");
+ return NULL;
+ }
+ if (p[24] == '\n')
+ p[24] = '\0';
+ return PyUnicode_FromString(p);
}
PyDoc_STRVAR(ctime_doc,
@@ -695,17 +695,17 @@ not present, current time as returned by localtime() is used.");
static PyObject *
time_mktime(PyObject *self, PyObject *tup)
{
- struct tm buf;
- time_t tt;
- if (!gettmarg(tup, &buf))
- return NULL;
- tt = mktime(&buf);
- if (tt == (time_t)(-1)) {
- PyErr_SetString(PyExc_OverflowError,
- "mktime argument out of range");
- return NULL;
- }
- return PyFloat_FromDouble((double)tt);
+ struct tm buf;
+ time_t tt;
+ if (!gettmarg(tup, &buf))
+ return NULL;
+ tt = mktime(&buf);
+ if (tt == (time_t)(-1)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "mktime argument out of range");
+ return NULL;
+ }
+ return PyFloat_FromDouble((double)tt);
}
PyDoc_STRVAR(mktime_doc,
@@ -720,21 +720,21 @@ static void PyInit_timezone(PyObject *module);
static PyObject *
time_tzset(PyObject *self, PyObject *unused)
{
- PyObject* m;
+ PyObject* m;
- m = PyImport_ImportModuleNoBlock("time");
- if (m == NULL) {
- return NULL;
- }
+ m = PyImport_ImportModuleNoBlock("time");
+ if (m == NULL) {
+ return NULL;
+ }
- tzset();
+ tzset();
- /* Reset timezone, altzone, daylight and tzname */
- PyInit_timezone(m);
- Py_DECREF(m);
+ /* Reset timezone, altzone, daylight and tzname */
+ PyInit_timezone(m);
+ Py_DECREF(m);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(tzset_doc,
@@ -754,115 +754,115 @@ should not be relied on.");
static void
PyInit_timezone(PyObject *m) {
/* This code moved from PyInit_time wholesale to allow calling it from
- time_tzset. In the future, some parts of it can be moved back
- (for platforms that don't HAVE_WORKING_TZSET, when we know what they
- are), and the extraneous calls to tzset(3) should be removed.
- I haven't done this yet, as I don't want to change this code as
- little as possible when introducing the time.tzset and time.tzsetwall
- methods. This should simply be a method of doing the following once,
- at the top of this function and removing the call to tzset() from
- time_tzset():
-
- #ifdef HAVE_TZSET
- tzset()
- #endif
-
- And I'm lazy and hate C so nyer.
+ time_tzset. In the future, some parts of it can be moved back
+ (for platforms that don't HAVE_WORKING_TZSET, when we know what they
+ are), and the extraneous calls to tzset(3) should be removed.
+ I haven't done this yet, as I don't want to change this code as
+ little as possible when introducing the time.tzset and time.tzsetwall
+ methods. This should simply be a method of doing the following once,
+ at the top of this function and removing the call to tzset() from
+ time_tzset():
+
+ #ifdef HAVE_TZSET
+ tzset()
+ #endif
+
+ And I'm lazy and hate C so nyer.
*/
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
- PyObject *otz0, *otz1;
- tzset();
+ PyObject *otz0, *otz1;
+ tzset();
#ifdef PYOS_OS2
- PyModule_AddIntConstant(m, "timezone", _timezone);
+ PyModule_AddIntConstant(m, "timezone", _timezone);
#else /* !PYOS_OS2 */
- PyModule_AddIntConstant(m, "timezone", timezone);
+ PyModule_AddIntConstant(m, "timezone", timezone);
#endif /* PYOS_OS2 */
#ifdef HAVE_ALTZONE
- PyModule_AddIntConstant(m, "altzone", altzone);
+ PyModule_AddIntConstant(m, "altzone", altzone);
#else
#ifdef PYOS_OS2
- PyModule_AddIntConstant(m, "altzone", _timezone-3600);
+ PyModule_AddIntConstant(m, "altzone", _timezone-3600);
#else /* !PYOS_OS2 */
- PyModule_AddIntConstant(m, "altzone", timezone-3600);
+ PyModule_AddIntConstant(m, "altzone", timezone-3600);
#endif /* PYOS_OS2 */
#endif
- PyModule_AddIntConstant(m, "daylight", daylight);
- otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
- otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
- PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
+ PyModule_AddIntConstant(m, "daylight", daylight);
+ otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
+ otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
+ PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
#ifdef HAVE_STRUCT_TM_TM_ZONE
- {
+ {
#define YEAR ((time_t)((365 * 24 + 6) * 3600))
- time_t t;
- struct tm *p;
- long janzone, julyzone;
- char janname[10], julyname[10];
- t = (time((time_t *)0) / YEAR) * YEAR;
- p = localtime(&t);
- janzone = -p->tm_gmtoff;
- strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
- janname[9] = '\0';
- t += YEAR/2;
- p = localtime(&t);
- julyzone = -p->tm_gmtoff;
- strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
- julyname[9] = '\0';
-
- if( janzone < julyzone ) {
- /* DST is reversed in the southern hemisphere */
- PyModule_AddIntConstant(m, "timezone", julyzone);
- PyModule_AddIntConstant(m, "altzone", janzone);
- PyModule_AddIntConstant(m, "daylight",
- janzone != julyzone);
- PyModule_AddObject(m, "tzname",
- Py_BuildValue("(zz)",
- julyname, janname));
- } else {
- PyModule_AddIntConstant(m, "timezone", janzone);
- PyModule_AddIntConstant(m, "altzone", julyzone);
- PyModule_AddIntConstant(m, "daylight",
- janzone != julyzone);
- PyModule_AddObject(m, "tzname",
- Py_BuildValue("(zz)",
- janname, julyname));
- }
- }
+ time_t t;
+ struct tm *p;
+ long janzone, julyzone;
+ char janname[10], julyname[10];
+ t = (time((time_t *)0) / YEAR) * YEAR;
+ p = localtime(&t);
+ janzone = -p->tm_gmtoff;
+ strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
+ janname[9] = '\0';
+ t += YEAR/2;
+ p = localtime(&t);
+ julyzone = -p->tm_gmtoff;
+ strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
+ julyname[9] = '\0';
+
+ if( janzone < julyzone ) {
+ /* DST is reversed in the southern hemisphere */
+ PyModule_AddIntConstant(m, "timezone", julyzone);
+ PyModule_AddIntConstant(m, "altzone", janzone);
+ PyModule_AddIntConstant(m, "daylight",
+ janzone != julyzone);
+ PyModule_AddObject(m, "tzname",
+ Py_BuildValue("(zz)",
+ julyname, janname));
+ } else {
+ PyModule_AddIntConstant(m, "timezone", janzone);
+ PyModule_AddIntConstant(m, "altzone", julyzone);
+ PyModule_AddIntConstant(m, "daylight",
+ janzone != julyzone);
+ PyModule_AddObject(m, "tzname",
+ Py_BuildValue("(zz)",
+ janname, julyname));
+ }
+ }
#else
#endif /* HAVE_STRUCT_TM_TM_ZONE */
#ifdef __CYGWIN__
- tzset();
- PyModule_AddIntConstant(m, "timezone", _timezone);
- PyModule_AddIntConstant(m, "altzone", _timezone-3600);
- PyModule_AddIntConstant(m, "daylight", _daylight);
- PyModule_AddObject(m, "tzname",
- Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
+ tzset();
+ PyModule_AddIntConstant(m, "timezone", _timezone);
+ PyModule_AddIntConstant(m, "altzone", _timezone-3600);
+ PyModule_AddIntConstant(m, "daylight", _daylight);
+ PyModule_AddObject(m, "tzname",
+ Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#endif /* __CYGWIN__ */
#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
}
static PyMethodDef time_methods[] = {
- {"time", time_time, METH_NOARGS, time_doc},
+ {"time", time_time, METH_NOARGS, time_doc},
#ifdef HAVE_CLOCK
- {"clock", time_clock, METH_NOARGS, clock_doc},
+ {"clock", time_clock, METH_NOARGS, clock_doc},
#endif
- {"sleep", time_sleep, METH_VARARGS, sleep_doc},
- {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
- {"localtime", time_localtime, METH_VARARGS, localtime_doc},
- {"asctime", time_asctime, METH_VARARGS, asctime_doc},
- {"ctime", time_ctime, METH_VARARGS, ctime_doc},
+ {"sleep", time_sleep, METH_VARARGS, sleep_doc},
+ {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
+ {"localtime", time_localtime, METH_VARARGS, localtime_doc},
+ {"asctime", time_asctime, METH_VARARGS, asctime_doc},
+ {"ctime", time_ctime, METH_VARARGS, ctime_doc},
#ifdef HAVE_MKTIME
- {"mktime", time_mktime, METH_O, mktime_doc},
+ {"mktime", time_mktime, METH_O, mktime_doc},
#endif
#ifdef HAVE_STRFTIME
- {"strftime", time_strftime, METH_VARARGS, strftime_doc},
+ {"strftime", time_strftime, METH_VARARGS, strftime_doc},
#endif
- {"strptime", time_strptime, METH_VARARGS, strptime_doc},
+ {"strptime", time_strptime, METH_VARARGS, strptime_doc},
#ifdef HAVE_WORKING_TZSET
- {"tzset", time_tzset, METH_NOARGS, tzset_doc},
+ {"tzset", time_tzset, METH_NOARGS, tzset_doc},
#endif
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
@@ -914,53 +914,53 @@ tzset() -- change the local timezone");
static struct PyModuleDef timemodule = {
- PyModuleDef_HEAD_INIT,
- "time",
- module_doc,
- -1,
- time_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "time",
+ module_doc,
+ -1,
+ time_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_time(void)
{
- PyObject *m;
- char *p;
- m = PyModule_Create(&timemodule);
- if (m == NULL)
- return NULL;
-
- /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
- p = Py_GETENV("PYTHONY2K");
- PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
- /* Squirrel away the module's dictionary for the y2k check */
- moddict = PyModule_GetDict(m);
- Py_INCREF(moddict);
-
- /* Set, or reset, module variables like time.timezone */
- PyInit_timezone(m);
+ PyObject *m;
+ char *p;
+ m = PyModule_Create(&timemodule);
+ if (m == NULL)
+ return NULL;
+
+ /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
+ p = Py_GETENV("PYTHONY2K");
+ PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
+ /* Squirrel away the module's dictionary for the y2k check */
+ moddict = PyModule_GetDict(m);
+ Py_INCREF(moddict);
+
+ /* Set, or reset, module variables like time.timezone */
+ PyInit_timezone(m);
#ifdef MS_WINDOWS
- /* Helper to allow interrupts for Windows.
- If Ctrl+C event delivered while not sleeping
- it will be ignored.
- */
- main_thread = PyThread_get_thread_ident();
- hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
+ /* Helper to allow interrupts for Windows.
+ If Ctrl+C event delivered while not sleeping
+ it will be ignored.
+ */
+ main_thread = PyThread_get_thread_ident();
+ hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
#endif /* MS_WINDOWS */
- if (!initialized) {
- PyStructSequence_InitType(&StructTimeType,
- &struct_time_type_desc);
- }
- Py_INCREF(&StructTimeType);
- PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
- initialized = 1;
- return m;
+ if (!initialized) {
+ PyStructSequence_InitType(&StructTimeType,
+ &struct_time_type_desc);
+ }
+ Py_INCREF(&StructTimeType);
+ PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
+ initialized = 1;
+ return m;
}
@@ -969,38 +969,38 @@ PyInit_time(void)
static double
floattime(void)
{
- /* There are three ways to get the time:
- (1) gettimeofday() -- resolution in microseconds
- (2) ftime() -- resolution in milliseconds
- (3) time() -- resolution in seconds
- In all cases the return value is a float in seconds.
- Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
- fail, so we fall back on ftime() or time().
- Note: clock resolution does not imply clock accuracy! */
+ /* There are three ways to get the time:
+ (1) gettimeofday() -- resolution in microseconds
+ (2) ftime() -- resolution in milliseconds
+ (3) time() -- resolution in seconds
+ In all cases the return value is a float in seconds.
+ Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
+ fail, so we fall back on ftime() or time().
+ Note: clock resolution does not imply clock accuracy! */
#ifdef HAVE_GETTIMEOFDAY
- {
- struct timeval t;
+ {
+ struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
- if (gettimeofday(&t) == 0)
- return (double)t.tv_sec + t.tv_usec*0.000001;
+ if (gettimeofday(&t) == 0)
+ return (double)t.tv_sec + t.tv_usec*0.000001;
#else /* !GETTIMEOFDAY_NO_TZ */
- if (gettimeofday(&t, (struct timezone *)NULL) == 0)
- return (double)t.tv_sec + t.tv_usec*0.000001;
+ if (gettimeofday(&t, (struct timezone *)NULL) == 0)
+ return (double)t.tv_sec + t.tv_usec*0.000001;
#endif /* !GETTIMEOFDAY_NO_TZ */
- }
+ }
#endif /* !HAVE_GETTIMEOFDAY */
- {
+ {
#if defined(HAVE_FTIME)
- struct timeb t;
- ftime(&t);
- return (double)t.time + (double)t.millitm * (double)0.001;
+ struct timeb t;
+ ftime(&t);
+ return (double)t.time + (double)t.millitm * (double)0.001;
#else /* !HAVE_FTIME */
- time_t secs;
- time(&secs);
- return (double)secs;
+ time_t secs;
+ time(&secs);
+ return (double)secs;
#endif /* !HAVE_FTIME */
- }
+ }
}
@@ -1013,96 +1013,96 @@ floatsleep(double secs)
{
/* XXX Should test for MS_WINDOWS first! */
#if defined(HAVE_SELECT) && !defined(__EMX__)
- struct timeval t;
- double frac;
- frac = fmod(secs, 1.0);
- secs = floor(secs);
- t.tv_sec = (long)secs;
- t.tv_usec = (long)(frac*1000000.0);
- Py_BEGIN_ALLOW_THREADS
- if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
+ struct timeval t;
+ double frac;
+ frac = fmod(secs, 1.0);
+ secs = floor(secs);
+ t.tv_sec = (long)secs;
+ t.tv_usec = (long)(frac*1000000.0);
+ Py_BEGIN_ALLOW_THREADS
+ if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
#ifdef EINTR
- if (errno != EINTR) {
+ if (errno != EINTR) {
#else
- if (1) {
+ if (1) {
#endif
- Py_BLOCK_THREADS
- PyErr_SetFromErrno(PyExc_IOError);
- return -1;
- }
- }
- Py_END_ALLOW_THREADS
+ Py_BLOCK_THREADS
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ }
+ Py_END_ALLOW_THREADS
#elif defined(__WATCOMC__) && !defined(__QNX__)
- /* XXX Can't interrupt this sleep */
- Py_BEGIN_ALLOW_THREADS
- delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
- Py_END_ALLOW_THREADS
+ /* XXX Can't interrupt this sleep */
+ Py_BEGIN_ALLOW_THREADS
+ delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
+ Py_END_ALLOW_THREADS
#elif defined(MS_WINDOWS)
- {
- double millisecs = secs * 1000.0;
- unsigned long ul_millis;
-
- if (millisecs > (double)ULONG_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "sleep length is too large");
- return -1;
- }
- Py_BEGIN_ALLOW_THREADS
- /* Allow sleep(0) to maintain win32 semantics, and as decreed
- * by Guido, only the main thread can be interrupted.
- */
- ul_millis = (unsigned long)millisecs;
- if (ul_millis == 0 ||
- main_thread != PyThread_get_thread_ident())
- Sleep(ul_millis);
- else {
- DWORD rc;
- ResetEvent(hInterruptEvent);
- rc = WaitForSingleObject(hInterruptEvent, ul_millis);
- if (rc == WAIT_OBJECT_0) {
- /* Yield to make sure real Python signal
- * handler called.
- */
- Sleep(1);
- Py_BLOCK_THREADS
- errno = EINTR;
- PyErr_SetFromErrno(PyExc_IOError);
- return -1;
- }
- }
- Py_END_ALLOW_THREADS
- }
+ {
+ double millisecs = secs * 1000.0;
+ unsigned long ul_millis;
+
+ if (millisecs > (double)ULONG_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "sleep length is too large");
+ return -1;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ /* Allow sleep(0) to maintain win32 semantics, and as decreed
+ * by Guido, only the main thread can be interrupted.
+ */
+ ul_millis = (unsigned long)millisecs;
+ if (ul_millis == 0 ||
+ main_thread != PyThread_get_thread_ident())
+ Sleep(ul_millis);
+ else {
+ DWORD rc;
+ ResetEvent(hInterruptEvent);
+ rc = WaitForSingleObject(hInterruptEvent, ul_millis);
+ if (rc == WAIT_OBJECT_0) {
+ /* Yield to make sure real Python signal
+ * handler called.
+ */
+ Sleep(1);
+ Py_BLOCK_THREADS
+ errno = EINTR;
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ }
+ Py_END_ALLOW_THREADS
+ }
#elif defined(PYOS_OS2)
- /* This Sleep *IS* Interruptable by Exceptions */
- Py_BEGIN_ALLOW_THREADS
- if (DosSleep(secs * 1000) != NO_ERROR) {
- Py_BLOCK_THREADS
- PyErr_SetFromErrno(PyExc_IOError);
- return -1;
- }
- Py_END_ALLOW_THREADS
+ /* This Sleep *IS* Interruptable by Exceptions */
+ Py_BEGIN_ALLOW_THREADS
+ if (DosSleep(secs * 1000) != NO_ERROR) {
+ Py_BLOCK_THREADS
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ Py_END_ALLOW_THREADS
#elif defined(PLAN9)
- {
- double millisecs = secs * 1000.0;
- if (millisecs > (double)LONG_MAX) {
- PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
- return -1;
- }
- /* This sleep *CAN BE* interrupted. */
- Py_BEGIN_ALLOW_THREADS
- if(sleep((long)millisecs) < 0){
- Py_BLOCK_THREADS
- PyErr_SetFromErrno(PyExc_IOError);
- return -1;
- }
- Py_END_ALLOW_THREADS
- }
+ {
+ double millisecs = secs * 1000.0;
+ if (millisecs > (double)LONG_MAX) {
+ PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
+ return -1;
+ }
+ /* This sleep *CAN BE* interrupted. */
+ Py_BEGIN_ALLOW_THREADS
+ if(sleep((long)millisecs) < 0){
+ Py_BLOCK_THREADS
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ Py_END_ALLOW_THREADS
+ }
#else
- /* XXX Can't interrupt this sleep */
- Py_BEGIN_ALLOW_THREADS
- sleep((int)secs);
- Py_END_ALLOW_THREADS
+ /* XXX Can't interrupt this sleep */
+ Py_BEGIN_ALLOW_THREADS
+ sleep((int)secs);
+ Py_END_ALLOW_THREADS
#endif
- return 0;
+ return 0;
}
diff --git a/Modules/tkappinit.c b/Modules/tkappinit.c
index cfbd20c..c1f97b0 100644
--- a/Modules/tkappinit.c
+++ b/Modules/tkappinit.c
@@ -26,154 +26,154 @@ static int tk_load_failed;
int
Tcl_AppInit(Tcl_Interp *interp)
{
- Tk_Window main_window;
- const char *_tkinter_skip_tk_init;
+ Tk_Window main_window;
+ const char *_tkinter_skip_tk_init;
#ifdef TKINTER_PROTECT_LOADTK
- const char *_tkinter_tk_failed;
+ const char *_tkinter_tk_failed;
#endif
#ifdef TK_AQUA
#ifndef MAX_PATH_LEN
#define MAX_PATH_LEN 1024
#endif
- char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
- Tcl_Obj* pathPtr;
+ char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
+ Tcl_Obj* pathPtr;
- /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
- Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
- tclLibPath, MAX_PATH_LEN, 0);
+ /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
+ Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
+ tclLibPath, MAX_PATH_LEN, 0);
- if (tclLibPath[0] != '\0') {
- Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
- Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
- Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
- }
-
- if (tclLibPath[0] != '\0') {
- Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
- Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
- Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
- }
+ if (tclLibPath[0] != '\0') {
+ Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
+ Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
+ Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
+ }
+
+ if (tclLibPath[0] != '\0') {
+ Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
+ Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
+ Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
+ }
#endif
- if (Tcl_Init (interp) == TCL_ERROR)
- return TCL_ERROR;
+ if (Tcl_Init (interp) == TCL_ERROR)
+ return TCL_ERROR;
#ifdef TK_AQUA
- /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
- Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
- tkLibPath, MAX_PATH_LEN, 1);
+ /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
+ Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
+ tkLibPath, MAX_PATH_LEN, 1);
- if (tclLibPath[0] != '\0') {
- pathPtr = Tcl_NewStringObj(tclLibPath, -1);
- } else {
- Tcl_Obj *pathPtr = TclGetLibraryPath();
- }
+ if (tclLibPath[0] != '\0') {
+ pathPtr = Tcl_NewStringObj(tclLibPath, -1);
+ } else {
+ Tcl_Obj *pathPtr = TclGetLibraryPath();
+ }
- if (tkLibPath[0] != '\0') {
- Tcl_Obj *objPtr;
+ if (tkLibPath[0] != '\0') {
+ Tcl_Obj *objPtr;
- Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
- objPtr = Tcl_NewStringObj(tkLibPath, -1);
- Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
- }
+ Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
+ objPtr = Tcl_NewStringObj(tkLibPath, -1);
+ Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
+ }
- TclSetLibraryPath(pathPtr);
+ TclSetLibraryPath(pathPtr);
#endif
#ifdef WITH_XXX
- /* Initialize modules that don't require Tk */
+ /* Initialize modules that don't require Tk */
#endif
- _tkinter_skip_tk_init = Tcl_GetVar(interp,
- "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
- if (_tkinter_skip_tk_init != NULL &&
- strcmp(_tkinter_skip_tk_init, "1") == 0) {
- return TCL_OK;
- }
+ _tkinter_skip_tk_init = Tcl_GetVar(interp,
+ "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
+ if (_tkinter_skip_tk_init != NULL &&
+ strcmp(_tkinter_skip_tk_init, "1") == 0) {
+ return TCL_OK;
+ }
#ifdef TKINTER_PROTECT_LOADTK
- _tkinter_tk_failed = Tcl_GetVar(interp,
- "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
+ _tkinter_tk_failed = Tcl_GetVar(interp,
+ "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
- if (tk_load_failed || (
- _tkinter_tk_failed != NULL &&
- strcmp(_tkinter_tk_failed, "1") == 0)) {
- Tcl_SetResult(interp, TKINTER_LOADTK_ERRMSG, TCL_STATIC);
- return TCL_ERROR;
- }
+ if (tk_load_failed || (
+ _tkinter_tk_failed != NULL &&
+ strcmp(_tkinter_tk_failed, "1") == 0)) {
+ Tcl_SetResult(interp, TKINTER_LOADTK_ERRMSG, TCL_STATIC);
+ return TCL_ERROR;
+ }
#endif
- if (Tk_Init(interp) == TCL_ERROR) {
+ if (Tk_Init(interp) == TCL_ERROR) {
#ifdef TKINTER_PROTECT_LOADTK
- tk_load_failed = 1;
- Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
+ tk_load_failed = 1;
+ Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
#endif
- return TCL_ERROR;
- }
+ return TCL_ERROR;
+ }
- main_window = Tk_MainWindow(interp);
+ main_window = Tk_MainWindow(interp);
#ifdef TK_AQUA
- TkMacOSXInitAppleEvents(interp);
- TkMacOSXInitMenus(interp);
+ TkMacOSXInitAppleEvents(interp);
+ TkMacOSXInitMenus(interp);
#endif
-
+
#ifdef WITH_MOREBUTTONS
- {
- extern Tcl_CmdProc studButtonCmd;
- extern Tcl_CmdProc triButtonCmd;
+ {
+ extern Tcl_CmdProc studButtonCmd;
+ extern Tcl_CmdProc triButtonCmd;
- Tcl_CreateCommand(interp, "studbutton", studButtonCmd,
- (ClientData) main_window, NULL);
- Tcl_CreateCommand(interp, "tributton", triButtonCmd,
- (ClientData) main_window, NULL);
- }
+ Tcl_CreateCommand(interp, "studbutton", studButtonCmd,
+ (ClientData) main_window, NULL);
+ Tcl_CreateCommand(interp, "tributton", triButtonCmd,
+ (ClientData) main_window, NULL);
+ }
#endif
#ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */
- {
- extern void TkImaging_Init(Tcl_Interp *);
- TkImaging_Init(interp);
- /* XXX TkImaging_Init() doesn't have the right return type */
- /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
- }
+ {
+ extern void TkImaging_Init(Tcl_Interp *);
+ TkImaging_Init(interp);
+ /* XXX TkImaging_Init() doesn't have the right return type */
+ /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
+ }
#endif
#ifdef WITH_PIL_OLD /* 0.2b4 and earlier */
- {
- extern void TkImaging_Init(void);
- /* XXX TkImaging_Init() doesn't have the right prototype */
- /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
- }
+ {
+ extern void TkImaging_Init(void);
+ /* XXX TkImaging_Init() doesn't have the right prototype */
+ /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
+ }
#endif
#ifdef WITH_TIX
- {
- extern int Tix_Init(Tcl_Interp *interp);
- extern int Tix_SafeInit(Tcl_Interp *interp);
- Tcl_StaticPackage(NULL, "Tix", Tix_Init, Tix_SafeInit);
- }
+ {
+ extern int Tix_Init(Tcl_Interp *interp);
+ extern int Tix_SafeInit(Tcl_Interp *interp);
+ Tcl_StaticPackage(NULL, "Tix", Tix_Init, Tix_SafeInit);
+ }
#endif
#ifdef WITH_BLT
- {
- extern int Blt_Init(Tcl_Interp *);
- extern int Blt_SafeInit(Tcl_Interp *);
- Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit);
- }
+ {
+ extern int Blt_Init(Tcl_Interp *);
+ extern int Blt_SafeInit(Tcl_Interp *);
+ Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit);
+ }
#endif
#ifdef WITH_TOGL
- {
- /* XXX I've heard rumors that this doesn't work */
- extern int Togl_Init(Tcl_Interp *);
- /* XXX Is there no Togl_SafeInit? */
- Tcl_StaticPackage(NULL, "Togl", Togl_Init, NULL);
- }
+ {
+ /* XXX I've heard rumors that this doesn't work */
+ extern int Togl_Init(Tcl_Interp *);
+ /* XXX Is there no Togl_SafeInit? */
+ Tcl_StaticPackage(NULL, "Togl", Togl_Init, NULL);
+ }
#endif
#ifdef WITH_XXX
#endif
- return TCL_OK;
+ return TCL_OK;
}
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index f7c3292..0f726b8 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -19,14 +19,14 @@
/* character properties */
typedef struct {
- const unsigned char category; /* index into
- _PyUnicode_CategoryNames */
- const unsigned char combining; /* combining class value 0 - 255 */
- const unsigned char bidirectional; /* index into
- _PyUnicode_BidirectionalNames */
- const unsigned char mirrored; /* true if mirrored in bidir mode */
- const unsigned char east_asian_width; /* index into
- _PyUnicode_EastAsianWidth */
+ const unsigned char category; /* index into
+ _PyUnicode_CategoryNames */
+ const unsigned char combining; /* combining class value 0 - 255 */
+ const unsigned char bidirectional; /* index into
+ _PyUnicode_BidirectionalNames */
+ const unsigned char mirrored; /* true if mirrored in bidir mode */
+ const unsigned char east_asian_width; /* index into
+ _PyUnicode_EastAsianWidth */
const unsigned char normalization_quick_check; /* see is_normalized() */
} _PyUnicode_DatabaseRecord;
@@ -67,7 +67,7 @@ typedef struct previous_version {
#define get_old_record(self, v) ((((PreviousDBVersion*)self)->getrecord)(v))
static PyMemberDef DB_members[] = {
- {"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY},
+ {"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY},
{NULL}
};
@@ -79,14 +79,14 @@ static PyObject*
new_previous_version(const char*name, const change_record* (*getrecord)(Py_UCS4),
Py_UCS4 (*normalization)(Py_UCS4))
{
- PreviousDBVersion *self;
- self = PyObject_New(PreviousDBVersion, &UCD_Type);
- if (self == NULL)
- return NULL;
- self->name = name;
- self->getrecord = getrecord;
+ PreviousDBVersion *self;
+ self = PyObject_New(PreviousDBVersion, &UCD_Type);
+ if (self == NULL)
+ return NULL;
+ self->name = name;
+ self->getrecord = getrecord;
self->normalization = normalization;
- return (PyObject*)self;
+ return (PyObject*)self;
}
@@ -95,12 +95,12 @@ static Py_UCS4 getuchar(PyUnicodeObject *obj)
Py_UNICODE *v = PyUnicode_AS_UNICODE(obj);
if (PyUnicode_GET_SIZE(obj) == 1)
- return *v;
+ return *v;
#ifndef Py_UNICODE_WIDE
else if ((PyUnicode_GET_SIZE(obj) == 2) &&
(0xD800 <= v[0] && v[0] <= 0xDBFF) &&
(0xDC00 <= v[1] && v[1] <= 0xDFFF))
- return (((v[0] & 0x3FF)<<10) | (v[1] & 0x3FF)) + 0x10000;
+ return (((v[0] & 0x3FF)<<10) | (v[1] & 0x3FF)) + 0x10000;
#endif
PyErr_SetString(PyExc_TypeError,
"need a single Unicode character as parameter");
@@ -137,7 +137,7 @@ unicodedata_decimal(PyObject *self, PyObject *args)
/* unassigned */
have_old = 1;
rc = -1;
- }
+ }
else if (old->decimal_changed != 0xFF) {
have_old = 1;
rc = old->decimal_changed;
@@ -147,15 +147,15 @@ unicodedata_decimal(PyObject *self, PyObject *args)
if (!have_old)
rc = Py_UNICODE_TODECIMAL(c);
if (rc < 0) {
- if (defobj == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "not a decimal");
+ if (defobj == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "not a decimal");
return NULL;
- }
- else {
- Py_INCREF(defobj);
- return defobj;
- }
+ }
+ else {
+ Py_INCREF(defobj);
+ return defobj;
+ }
}
return PyLong_FromLong(rc);
}
@@ -182,14 +182,14 @@ unicodedata_digit(PyObject *self, PyObject *args)
return NULL;
rc = Py_UNICODE_TODIGIT(c);
if (rc < 0) {
- if (defobj == NULL) {
- PyErr_SetString(PyExc_ValueError, "not a digit");
+ if (defobj == NULL) {
+ PyErr_SetString(PyExc_ValueError, "not a digit");
return NULL;
- }
- else {
- Py_INCREF(defobj);
- return defobj;
- }
+ }
+ else {
+ Py_INCREF(defobj);
+ return defobj;
+ }
}
return PyLong_FromLong(rc);
}
@@ -222,7 +222,7 @@ unicodedata_numeric(PyObject *self, PyObject *args)
/* unassigned */
have_old = 1;
rc = -1.0;
- }
+ }
else if (old->decimal_changed != 0xFF) {
have_old = 1;
rc = old->decimal_changed;
@@ -232,14 +232,14 @@ unicodedata_numeric(PyObject *self, PyObject *args)
if (!have_old)
rc = Py_UNICODE_TONUMERIC(c);
if (rc == -1.0) {
- if (defobj == NULL) {
- PyErr_SetString(PyExc_ValueError, "not a numeric character");
- return NULL;
- }
- else {
- Py_INCREF(defobj);
- return defobj;
- }
+ if (defobj == NULL) {
+ PyErr_SetString(PyExc_ValueError, "not a numeric character");
+ return NULL;
+ }
+ else {
+ Py_INCREF(defobj);
+ return defobj;
+ }
}
return PyFloat_FromDouble(rc);
}
@@ -258,8 +258,8 @@ unicodedata_category(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:category",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -287,8 +287,8 @@ unicodedata_bidirectional(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:bidirectional",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -318,8 +318,8 @@ unicodedata_combining(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:combining",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -347,8 +347,8 @@ unicodedata_mirrored(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:mirrored",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -377,8 +377,8 @@ unicodedata_east_asian_width(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:east_asian_width",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -408,8 +408,8 @@ unicodedata_decomposition(PyObject *self, PyObject *args)
Py_UCS4 c;
if (!PyArg_ParseTuple(args, "O!:decomposition",
- &PyUnicode_Type, &v))
- return NULL;
+ &PyUnicode_Type, &v))
+ return NULL;
c = getuchar(v);
if (c == (Py_UCS4)-1)
return NULL;
@@ -455,7 +455,7 @@ unicodedata_decomposition(PyObject *self, PyObject *args)
decomp_data[++index]);
i += strlen(decomp + i);
}
-
+
decomp[i] = '\0';
return PyUnicode_FromString(decomp);
@@ -466,7 +466,7 @@ get_decomp_record(PyObject *self, Py_UCS4 code, int *index, int *prefix, int *co
{
if (code >= 0x110000) {
*index = 0;
- } else if (self && UCD_Check(self) &&
+ } else if (self && UCD_Check(self) &&
get_old_record(self, code)->category_changed==0) {
/* unassigned in old version */
*index = 0;
@@ -476,7 +476,7 @@ get_decomp_record(PyObject *self, Py_UCS4 code, int *index, int *prefix, int *co
*index = decomp_index2[(*index<<DECOMP_SHIFT)+
(code&((1<<DECOMP_SHIFT)-1))];
}
-
+
/* high byte is number of hex bytes (usually one or two), low byte
is prefix code (from*/
*count = decomp_data[*index] >> 8;
@@ -501,11 +501,11 @@ nfd_nfkd(PyObject *self, PyObject *input, int k)
PyObject *result;
Py_UNICODE *i, *end, *o;
/* Longest decomposition in Unicode 3.2: U+FDFA */
- Py_UNICODE stack[20];
+ Py_UNICODE stack[20];
Py_ssize_t space, isize;
int index, prefix, count, stackptr;
unsigned char prev, cur;
-
+
stackptr = 0;
isize = PyUnicode_GET_SIZE(input);
/* Overallocate atmost 10 characters. */
@@ -642,12 +642,12 @@ nfc_nfkc(PyObject *self, PyObject *input, int k)
i = PyUnicode_AS_UNICODE(result);
end = i + PyUnicode_GET_SIZE(result);
o = PyUnicode_AS_UNICODE(result);
-
+
again:
while (i < end) {
for (index = 0; index < cskipped; index++) {
if (skipped[index] == i) {
- /* *i character is skipped.
+ /* *i character is skipped.
Remove from list. */
skipped[index] = skipped[cskipped-1];
cskipped--;
@@ -658,7 +658,7 @@ nfc_nfkc(PyObject *self, PyObject *input, int k)
/* Hangul Composition. We don't need to check for <LV,T>
pairs, since we always have decomposed data. */
if (LBase <= *i && *i < (LBase+LCount) &&
- i + 1 < end &&
+ i + 1 < end &&
VBase <= i[1] && i[1] <= (VBase+VCount)) {
int LIndex, VIndex;
LIndex = i[0] - LBase;
@@ -707,7 +707,7 @@ nfc_nfkc(PyObject *self, PyObject *input, int k)
(index&((1<<COMP_SHIFT)-1))];
if (code == 0)
goto not_combinable;
-
+
/* Replace the original character. */
*i = code;
/* Mark the second character unused. */
@@ -891,29 +891,29 @@ _getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen)
if (old->category_changed == 0) {
/* unassigned */
return 0;
- }
+ }
}
if (SBase <= code && code < SBase+SCount) {
- /* Hangul syllable. */
- int SIndex = code - SBase;
- int L = SIndex / NCount;
- int V = (SIndex % NCount) / TCount;
- int T = SIndex % TCount;
-
- if (buflen < 27)
- /* Worst case: HANGUL SYLLABLE <10chars>. */
- return 0;
- strcpy(buffer, "HANGUL SYLLABLE ");
- buffer += 16;
- strcpy(buffer, hangul_syllables[L][0]);
- buffer += strlen(hangul_syllables[L][0]);
- strcpy(buffer, hangul_syllables[V][1]);
- buffer += strlen(hangul_syllables[V][1]);
- strcpy(buffer, hangul_syllables[T][2]);
- buffer += strlen(hangul_syllables[T][2]);
- *buffer = '\0';
- return 1;
+ /* Hangul syllable. */
+ int SIndex = code - SBase;
+ int L = SIndex / NCount;
+ int V = (SIndex % NCount) / TCount;
+ int T = SIndex % TCount;
+
+ if (buflen < 27)
+ /* Worst case: HANGUL SYLLABLE <10chars>. */
+ return 0;
+ strcpy(buffer, "HANGUL SYLLABLE ");
+ buffer += 16;
+ strcpy(buffer, hangul_syllables[L][0]);
+ buffer += strlen(hangul_syllables[L][0]);
+ strcpy(buffer, hangul_syllables[V][1]);
+ buffer += strlen(hangul_syllables[V][1]);
+ strcpy(buffer, hangul_syllables[T][2]);
+ buffer += strlen(hangul_syllables[T][2]);
+ *buffer = '\0';
+ return 1;
}
if (is_unified_ideograph(code)) {
@@ -980,23 +980,23 @@ _cmpname(PyObject *self, int code, const char* name, int namelen)
return buffer[namelen] == '\0';
}
-static void
+static void
find_syllable(const char *str, int *len, int *pos, int count, int column)
{
int i, len1;
*len = -1;
for (i = 0; i < count; i++) {
- char *s = hangul_syllables[i][column];
- len1 = strlen(s);
- if (len1 <= *len)
- continue;
- if (strncmp(str, s, len1) == 0) {
- *len = len1;
- *pos = i;
- }
+ char *s = hangul_syllables[i][column];
+ len1 = strlen(s);
+ if (len1 <= *len)
+ continue;
+ if (strncmp(str, s, len1) == 0) {
+ *len = len1;
+ *pos = i;
+ }
}
if (*len == -1) {
- *len = 0;
+ *len = 0;
}
}
@@ -1009,18 +1009,18 @@ _getcode(PyObject* self, const char* name, int namelen, Py_UCS4* code)
/* Check for hangul syllables. */
if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) {
- int len, L = -1, V = -1, T = -1;
- const char *pos = name + 16;
- find_syllable(pos, &len, &L, LCount, 0);
- pos += len;
- find_syllable(pos, &len, &V, VCount, 1);
- pos += len;
- find_syllable(pos, &len, &T, TCount, 2);
- pos += len;
- if (L != -1 && V != -1 && T != -1 && pos-name == namelen) {
- *code = SBase + (L*VCount+V)*TCount + T;
- return 1;
- }
+ int len, L = -1, V = -1, T = -1;
+ const char *pos = name + 16;
+ find_syllable(pos, &len, &L, LCount, 0);
+ pos += len;
+ find_syllable(pos, &len, &V, VCount, 1);
+ pos += len;
+ find_syllable(pos, &len, &T, TCount, 2);
+ pos += len;
+ if (L != -1 && V != -1 && T != -1 && pos-name == namelen) {
+ *code = SBase + (L*VCount+V)*TCount + T;
+ return 1;
+ }
/* Otherwise, it's an illegal syllable name. */
return 0;
}
@@ -1080,7 +1080,7 @@ _getcode(PyObject* self, const char* name, int namelen, Py_UCS4* code)
}
}
-static const _PyUnicode_Name_CAPI hashAPI =
+static const _PyUnicode_Name_CAPI hashAPI =
{
sizeof(_PyUnicode_Name_CAPI),
_getucname,
@@ -1112,14 +1112,14 @@ unicodedata_name(PyObject* self, PyObject* args)
return NULL;
if (!_getucname(self, c, name, sizeof(name))) {
- if (defobj == NULL) {
- PyErr_SetString(PyExc_ValueError, "no such name");
+ if (defobj == NULL) {
+ PyErr_SetString(PyExc_ValueError, "no such name");
return NULL;
- }
- else {
- Py_INCREF(defobj);
- return defobj;
- }
+ }
+ else {
+ Py_INCREF(defobj);
+ return defobj;
+ }
}
return PyUnicode_FromString(name);
@@ -1157,7 +1157,7 @@ unicodedata_lookup(PyObject* self, PyObject* args)
}
#endif
str[0] = (Py_UNICODE) code;
- return PyUnicode_FromUnicode(str, 1);
+ return PyUnicode_FromUnicode(str, 1);
}
/* XXX Add doc strings. */
@@ -1182,27 +1182,27 @@ static PyMethodDef unicodedata_functions[] = {
{"lookup", unicodedata_lookup, METH_VARARGS, unicodedata_lookup__doc__},
{"normalize", unicodedata_normalize, METH_VARARGS,
unicodedata_normalize__doc__},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject UCD_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "unicodedata.UCD", /*tp_name*/
- sizeof(PreviousDBVersion), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)PyObject_Del, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "unicodedata.UCD", /*tp_name*/
+ sizeof(PreviousDBVersion), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)PyObject_Del, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
PyObject_GenericGetAttr,/*tp_getattro*/
@@ -1243,15 +1243,15 @@ http://www.unicode.org/Public/5.1.0/ucd/UCD.html).");
static struct PyModuleDef unicodedatamodule = {
- PyModuleDef_HEAD_INIT,
- "unicodedata",
- unicodedata_docstring,
- -1,
- unicodedata_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "unicodedata",
+ unicodedata_docstring,
+ -1,
+ unicodedata_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -1281,7 +1281,7 @@ PyInit_unicodedata(void)
return m;
}
-/*
+/*
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c
index a31341c..92c7673 100644
--- a/Modules/xxmodule.c
+++ b/Modules/xxmodule.c
@@ -19,23 +19,23 @@
static PyObject *ErrorObject;
typedef struct {
- PyObject_HEAD
- PyObject *x_attr; /* Attributes dictionary */
+ PyObject_HEAD
+ PyObject *x_attr; /* Attributes dictionary */
} XxoObject;
static PyTypeObject Xxo_Type;
-#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
+#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
static XxoObject *
newXxoObject(PyObject *arg)
{
- XxoObject *self;
- self = PyObject_New(XxoObject, &Xxo_Type);
- if (self == NULL)
- return NULL;
- self->x_attr = NULL;
- return self;
+ XxoObject *self;
+ self = PyObject_New(XxoObject, &Xxo_Type);
+ if (self == NULL)
+ return NULL;
+ self->x_attr = NULL;
+ return self;
}
/* Xxo methods */
@@ -43,101 +43,101 @@ newXxoObject(PyObject *arg)
static void
Xxo_dealloc(XxoObject *self)
{
- Py_XDECREF(self->x_attr);
- PyObject_Del(self);
+ Py_XDECREF(self->x_attr);
+ PyObject_Del(self);
}
static PyObject *
Xxo_demo(XxoObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":demo"))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, ":demo"))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyMethodDef Xxo_methods[] = {
- {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
- PyDoc_STR("demo() -> None")},
- {NULL, NULL} /* sentinel */
+ {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
+ PyDoc_STR("demo() -> None")},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
Xxo_getattro(XxoObject *self, PyObject *name)
{
- if (self->x_attr != NULL) {
- PyObject *v = PyDict_GetItem(self->x_attr, name);
- if (v != NULL) {
- Py_INCREF(v);
- return v;
- }
- }
- return PyObject_GenericGetAttr((PyObject *)self, name);
+ if (self->x_attr != NULL) {
+ PyObject *v = PyDict_GetItem(self->x_attr, name);
+ if (v != NULL) {
+ Py_INCREF(v);
+ return v;
+ }
+ }
+ return PyObject_GenericGetAttr((PyObject *)self, name);
}
static int
Xxo_setattr(XxoObject *self, char *name, PyObject *v)
{
- if (self->x_attr == NULL) {
- self->x_attr = PyDict_New();
- if (self->x_attr == NULL)
- return -1;
- }
- if (v == NULL) {
- int rv = PyDict_DelItemString(self->x_attr, name);
- if (rv < 0)
- PyErr_SetString(PyExc_AttributeError,
- "delete non-existing Xxo attribute");
- return rv;
- }
- else
- return PyDict_SetItemString(self->x_attr, name, v);
+ if (self->x_attr == NULL) {
+ self->x_attr = PyDict_New();
+ if (self->x_attr == NULL)
+ return -1;
+ }
+ if (v == NULL) {
+ int rv = PyDict_DelItemString(self->x_attr, name);
+ if (rv < 0)
+ PyErr_SetString(PyExc_AttributeError,
+ "delete non-existing Xxo attribute");
+ return rv;
+ }
+ else
+ return PyDict_SetItemString(self->x_attr, name, v);
}
static PyTypeObject Xxo_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Xxo", /*tp_name*/
- sizeof(XxoObject), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)Xxo_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- (getattrfunc)0, /*tp_getattr*/
- (setattrfunc)Xxo_setattr, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- (getattrofunc)Xxo_getattro, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- Xxo_methods, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Xxo", /*tp_name*/
+ sizeof(XxoObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)Xxo_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ (getattrfunc)0, /*tp_getattr*/
+ (setattrfunc)Xxo_setattr, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ (getattrofunc)Xxo_getattro, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ Xxo_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
};
/* --------------------------------------------------------------------- */
@@ -151,12 +151,12 @@ Return the sum of i and j.");
static PyObject *
xx_foo(PyObject *self, PyObject *args)
{
- long i, j;
- long res;
- if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
- return NULL;
- res = i+j; /* XXX Do something here */
- return PyLong_FromLong(res);
+ long i, j;
+ long res;
+ if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
+ return NULL;
+ res = i+j; /* XXX Do something here */
+ return PyLong_FromLong(res);
}
@@ -165,14 +165,14 @@ xx_foo(PyObject *self, PyObject *args)
static PyObject *
xx_new(PyObject *self, PyObject *args)
{
- XxoObject *rv;
-
- if (!PyArg_ParseTuple(args, ":new"))
- return NULL;
- rv = newXxoObject(args);
- if (rv == NULL)
- return NULL;
- return (PyObject *)rv;
+ XxoObject *rv;
+
+ if (!PyArg_ParseTuple(args, ":new"))
+ return NULL;
+ rv = newXxoObject(args);
+ if (rv == NULL)
+ return NULL;
+ return (PyObject *)rv;
}
/* Example with subtle bug from extensions manual ("Thin Ice"). */
@@ -180,20 +180,20 @@ xx_new(PyObject *self, PyObject *args)
static PyObject *
xx_bug(PyObject *self, PyObject *args)
{
- PyObject *list, *item;
+ PyObject *list, *item;
- if (!PyArg_ParseTuple(args, "O:bug", &list))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O:bug", &list))
+ return NULL;
- item = PyList_GetItem(list, 0);
- /* Py_INCREF(item); */
- PyList_SetItem(list, 1, PyLong_FromLong(0L));
- PyObject_Print(item, stdout, 0);
- printf("\n");
- /* Py_DECREF(item); */
+ item = PyList_GetItem(list, 0);
+ /* Py_INCREF(item); */
+ PyList_SetItem(list, 1, PyLong_FromLong(0L));
+ PyObject_Print(item, stdout, 0);
+ printf("\n");
+ /* Py_DECREF(item); */
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* Test bad format character */
@@ -201,61 +201,61 @@ xx_bug(PyObject *self, PyObject *args)
static PyObject *
xx_roj(PyObject *self, PyObject *args)
{
- PyObject *a;
- long b;
- if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *a;
+ long b;
+ if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* ---------- */
static PyTypeObject Str_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Str", /*tp_name*/
- 0, /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- 0, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /* see PyInit_xx */ /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Str", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
};
/* ---------- */
@@ -263,54 +263,54 @@ static PyTypeObject Str_Type = {
static PyObject *
null_richcompare(PyObject *self, PyObject *other, int op)
{
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyTypeObject Null_Type = {
- /* The ob_type field must be initialized in the module init function
- * to be portable to Windows without using C++. */
- PyVarObject_HEAD_INIT(NULL, 0)
- "xxmodule.Null", /*tp_name*/
- 0, /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- null_richcompare, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- 0, /*tp_methods*/
- 0, /*tp_members*/
- 0, /*tp_getset*/
- 0, /* see PyInit_xx */ /*tp_base*/
- 0, /*tp_dict*/
- 0, /*tp_descr_get*/
- 0, /*tp_descr_set*/
- 0, /*tp_dictoffset*/
- 0, /*tp_init*/
- 0, /*tp_alloc*/
- 0, /* see PyInit_xx */ /*tp_new*/
- 0, /*tp_free*/
- 0, /*tp_is_gc*/
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Null", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ null_richcompare, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /* see PyInit_xx */ /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
};
@@ -320,15 +320,15 @@ static PyTypeObject Null_Type = {
/* List of functions defined in the module */
static PyMethodDef xx_methods[] = {
- {"roj", xx_roj, METH_VARARGS,
- PyDoc_STR("roj(a,b) -> None")},
- {"foo", xx_foo, METH_VARARGS,
- xx_foo_doc},
- {"new", xx_new, METH_VARARGS,
- PyDoc_STR("new() -> new Xx object")},
- {"bug", xx_bug, METH_VARARGS,
- PyDoc_STR("bug(o) -> None")},
- {NULL, NULL} /* sentinel */
+ {"roj", xx_roj, METH_VARARGS,
+ PyDoc_STR("roj(a,b) -> None")},
+ {"foo", xx_foo, METH_VARARGS,
+ xx_foo_doc},
+ {"new", xx_new, METH_VARARGS,
+ PyDoc_STR("new() -> new Xx object")},
+ {"bug", xx_bug, METH_VARARGS,
+ PyDoc_STR("bug(o) -> None")},
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(module_doc,
@@ -338,59 +338,59 @@ PyDoc_STRVAR(module_doc,
static struct PyModuleDef xxmodule = {
- PyModuleDef_HEAD_INIT,
- "xx",
- module_doc,
- -1,
- xx_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "xx",
+ module_doc,
+ -1,
+ xx_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_xx(void)
{
- PyObject *m = NULL;
-
- /* Due to cross platform compiler issues the slots must be filled
- * here. It's required for portability to Windows without requiring
- * C++. */
- Null_Type.tp_base = &PyBaseObject_Type;
- Null_Type.tp_new = PyType_GenericNew;
- Str_Type.tp_base = &PyUnicode_Type;
-
- /* Finalize the type object including setting type of the new type
- * object; doing it here is required for portability, too. */
- if (PyType_Ready(&Xxo_Type) < 0)
- goto fail;
-
- /* Create the module and add the functions */
- m = PyModule_Create(&xxmodule);
- if (m == NULL)
- goto fail;
-
- /* Add some symbolic constants to the module */
- if (ErrorObject == NULL) {
- ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
- if (ErrorObject == NULL)
- goto fail;
- }
- Py_INCREF(ErrorObject);
- PyModule_AddObject(m, "error", ErrorObject);
-
- /* Add Str */
- if (PyType_Ready(&Str_Type) < 0)
- goto fail;
- PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
-
- /* Add Null */
- if (PyType_Ready(&Null_Type) < 0)
- goto fail;
- PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
- return m;
+ PyObject *m = NULL;
+
+ /* Due to cross platform compiler issues the slots must be filled
+ * here. It's required for portability to Windows without requiring
+ * C++. */
+ Null_Type.tp_base = &PyBaseObject_Type;
+ Null_Type.tp_new = PyType_GenericNew;
+ Str_Type.tp_base = &PyUnicode_Type;
+
+ /* Finalize the type object including setting type of the new type
+ * object; doing it here is required for portability, too. */
+ if (PyType_Ready(&Xxo_Type) < 0)
+ goto fail;
+
+ /* Create the module and add the functions */
+ m = PyModule_Create(&xxmodule);
+ if (m == NULL)
+ goto fail;
+
+ /* Add some symbolic constants to the module */
+ if (ErrorObject == NULL) {
+ ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
+ if (ErrorObject == NULL)
+ goto fail;
+ }
+ Py_INCREF(ErrorObject);
+ PyModule_AddObject(m, "error", ErrorObject);
+
+ /* Add Str */
+ if (PyType_Ready(&Str_Type) < 0)
+ goto fail;
+ PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
+
+ /* Add Null */
+ if (PyType_Ready(&Null_Type) < 0)
+ goto fail;
+ PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
+ return m;
fail:
- Py_XDECREF(m);
- return NULL;
+ Py_XDECREF(m);
+ return NULL;
}
diff --git a/Modules/xxsubtype.c b/Modules/xxsubtype.c
index 91ba0f12..6944e37 100644
--- a/Modules/xxsubtype.c
+++ b/Modules/xxsubtype.c
@@ -19,291 +19,291 @@ PyDoc_STRVAR(xxsubtype__doc__,
/* spamlist -- a list subtype */
typedef struct {
- PyListObject list;
- int state;
+ PyListObject list;
+ int state;
} spamlistobject;
static PyObject *
spamlist_getstate(spamlistobject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":getstate"))
- return NULL;
- return PyLong_FromLong(self->state);
+ if (!PyArg_ParseTuple(args, ":getstate"))
+ return NULL;
+ return PyLong_FromLong(self->state);
}
static PyObject *
spamlist_setstate(spamlistobject *self, PyObject *args)
{
- int state;
+ int state;
- if (!PyArg_ParseTuple(args, "i:setstate", &state))
- return NULL;
- self->state = state;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, "i:setstate", &state))
+ return NULL;
+ self->state = state;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
{
- PyObject *result = PyTuple_New(3);
-
- if (result != NULL) {
- if (self == NULL)
- self = Py_None;
- if (kw == NULL)
- kw = Py_None;
- Py_INCREF(self);
- PyTuple_SET_ITEM(result, 0, self);
- Py_INCREF(args);
- PyTuple_SET_ITEM(result, 1, args);
- Py_INCREF(kw);
- PyTuple_SET_ITEM(result, 2, kw);
- }
- return result;
+ PyObject *result = PyTuple_New(3);
+
+ if (result != NULL) {
+ if (self == NULL)
+ self = Py_None;
+ if (kw == NULL)
+ kw = Py_None;
+ Py_INCREF(self);
+ PyTuple_SET_ITEM(result, 0, self);
+ Py_INCREF(args);
+ PyTuple_SET_ITEM(result, 1, args);
+ Py_INCREF(kw);
+ PyTuple_SET_ITEM(result, 2, kw);
+ }
+ return result;
}
static PyMethodDef spamlist_methods[] = {
- {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
- PyDoc_STR("getstate() -> state")},
- {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
- PyDoc_STR("setstate(state)")},
- /* These entries differ only in the flags; they are used by the tests
- in test.test_descr. */
- {"classmeth", (PyCFunction)spamlist_specialmeth,
- METH_VARARGS | METH_KEYWORDS | METH_CLASS,
- PyDoc_STR("classmeth(*args, **kw)")},
- {"staticmeth", (PyCFunction)spamlist_specialmeth,
- METH_VARARGS | METH_KEYWORDS | METH_STATIC,
- PyDoc_STR("staticmeth(*args, **kw)")},
- {NULL, NULL},
+ {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
+ PyDoc_STR("getstate() -> state")},
+ {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
+ PyDoc_STR("setstate(state)")},
+ /* These entries differ only in the flags; they are used by the tests
+ in test.test_descr. */
+ {"classmeth", (PyCFunction)spamlist_specialmeth,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+ PyDoc_STR("classmeth(*args, **kw)")},
+ {"staticmeth", (PyCFunction)spamlist_specialmeth,
+ METH_VARARGS | METH_KEYWORDS | METH_STATIC,
+ PyDoc_STR("staticmeth(*args, **kw)")},
+ {NULL, NULL},
};
static int
spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
{
- if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
- return -1;
- self->state = 0;
- return 0;
+ if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
+ return -1;
+ self->state = 0;
+ return 0;
}
static PyObject *
spamlist_state_get(spamlistobject *self)
{
- return PyLong_FromLong(self->state);
+ return PyLong_FromLong(self->state);
}
static PyGetSetDef spamlist_getsets[] = {
- {"state", (getter)spamlist_state_get, NULL,
- PyDoc_STR("an int variable for demonstration purposes")},
- {0}
+ {"state", (getter)spamlist_state_get, NULL,
+ PyDoc_STR("an int variable for demonstration purposes")},
+ {0}
};
static PyTypeObject spamlist_type = {
- PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
- "xxsubtype.spamlist",
- sizeof(spamlistobject),
- 0,
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- spamlist_methods, /* tp_methods */
- 0, /* tp_members */
- spamlist_getsets, /* tp_getset */
- DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)spamlist_init, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
+ PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
+ "xxsubtype.spamlist",
+ sizeof(spamlistobject),
+ 0,
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ spamlist_methods, /* tp_methods */
+ 0, /* tp_members */
+ spamlist_getsets, /* tp_getset */
+ DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)spamlist_init, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
};
/* spamdict -- a dict subtype */
typedef struct {
- PyDictObject dict;
- int state;
+ PyDictObject dict;
+ int state;
} spamdictobject;
static PyObject *
spamdict_getstate(spamdictobject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":getstate"))
- return NULL;
- return PyLong_FromLong(self->state);
+ if (!PyArg_ParseTuple(args, ":getstate"))
+ return NULL;
+ return PyLong_FromLong(self->state);
}
static PyObject *
spamdict_setstate(spamdictobject *self, PyObject *args)
{
- int state;
+ int state;
- if (!PyArg_ParseTuple(args, "i:setstate", &state))
- return NULL;
- self->state = state;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, "i:setstate", &state))
+ return NULL;
+ self->state = state;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyMethodDef spamdict_methods[] = {
- {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
- PyDoc_STR("getstate() -> state")},
- {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
- PyDoc_STR("setstate(state)")},
- {NULL, NULL},
+ {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
+ PyDoc_STR("getstate() -> state")},
+ {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
+ PyDoc_STR("setstate(state)")},
+ {NULL, NULL},
};
static int
spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
{
- if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
- return -1;
- self->state = 0;
- return 0;
+ if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
+ return -1;
+ self->state = 0;
+ return 0;
}
static PyMemberDef spamdict_members[] = {
- {"state", T_INT, offsetof(spamdictobject, state), READONLY,
- PyDoc_STR("an int variable for demonstration purposes")},
- {0}
+ {"state", T_INT, offsetof(spamdictobject, state), READONLY,
+ PyDoc_STR("an int variable for demonstration purposes")},
+ {0}
};
static PyTypeObject spamdict_type = {
- PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
- "xxsubtype.spamdict",
- sizeof(spamdictobject),
- 0,
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- spamdict_methods, /* tp_methods */
- spamdict_members, /* tp_members */
- 0, /* tp_getset */
- DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)spamdict_init, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
+ PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
+ "xxsubtype.spamdict",
+ sizeof(spamdictobject),
+ 0,
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ spamdict_methods, /* tp_methods */
+ spamdict_members, /* tp_members */
+ 0, /* tp_getset */
+ DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)spamdict_init, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
};
static PyObject *
spam_bench(PyObject *self, PyObject *args)
{
- PyObject *obj, *name, *res;
- int n = 1000;
- time_t t0, t1;
-
- if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
- return NULL;
- t0 = clock();
- while (--n >= 0) {
- res = PyObject_GetAttr(obj, name);
- if (res == NULL)
- return NULL;
- Py_DECREF(res);
- }
- t1 = clock();
- return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
+ PyObject *obj, *name, *res;
+ int n = 1000;
+ time_t t0, t1;
+
+ if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
+ return NULL;
+ t0 = clock();
+ while (--n >= 0) {
+ res = PyObject_GetAttr(obj, name);
+ if (res == NULL)
+ return NULL;
+ Py_DECREF(res);
+ }
+ t1 = clock();
+ return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
}
static PyMethodDef xxsubtype_functions[] = {
- {"bench", spam_bench, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"bench", spam_bench, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
static struct PyModuleDef xxsubtypemodule = {
- PyModuleDef_HEAD_INIT,
- "xxsubtype",
- xxsubtype__doc__,
- -1,
- xxsubtype_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "xxsubtype",
+ xxsubtype__doc__,
+ -1,
+ xxsubtype_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_xxsubtype(void)
{
- PyObject *m;
-
- /* Fill in deferred data addresses. This must be done before
- PyType_Ready() is called. Note that PyType_Ready() automatically
- initializes the ob.ob_type field to &PyType_Type if it's NULL,
- so it's not necessary to fill in ob_type first. */
- spamdict_type.tp_base = &PyDict_Type;
- if (PyType_Ready(&spamdict_type) < 0)
- return NULL;
-
- spamlist_type.tp_base = &PyList_Type;
- if (PyType_Ready(&spamlist_type) < 0)
- return NULL;
-
- m = PyModule_Create(&xxsubtypemodule);
- if (m == NULL)
- return NULL;
-
- if (PyType_Ready(&spamlist_type) < 0)
- return NULL;
- if (PyType_Ready(&spamdict_type) < 0)
- return NULL;
-
- Py_INCREF(&spamlist_type);
- if (PyModule_AddObject(m, "spamlist",
- (PyObject *) &spamlist_type) < 0)
- return NULL;
-
- Py_INCREF(&spamdict_type);
- if (PyModule_AddObject(m, "spamdict",
- (PyObject *) &spamdict_type) < 0)
- return NULL;
- return m;
+ PyObject *m;
+
+ /* Fill in deferred data addresses. This must be done before
+ PyType_Ready() is called. Note that PyType_Ready() automatically
+ initializes the ob.ob_type field to &PyType_Type if it's NULL,
+ so it's not necessary to fill in ob_type first. */
+ spamdict_type.tp_base = &PyDict_Type;
+ if (PyType_Ready(&spamdict_type) < 0)
+ return NULL;
+
+ spamlist_type.tp_base = &PyList_Type;
+ if (PyType_Ready(&spamlist_type) < 0)
+ return NULL;
+
+ m = PyModule_Create(&xxsubtypemodule);
+ if (m == NULL)
+ return NULL;
+
+ if (PyType_Ready(&spamlist_type) < 0)
+ return NULL;
+ if (PyType_Ready(&spamdict_type) < 0)
+ return NULL;
+
+ Py_INCREF(&spamlist_type);
+ if (PyModule_AddObject(m, "spamlist",
+ (PyObject *) &spamlist_type) < 0)
+ return NULL;
+
+ Py_INCREF(&spamdict_type);
+ if (PyModule_AddObject(m, "spamdict",
+ (PyObject *) &spamdict_type) < 0)
+ return NULL;
+ return m;
}
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index fed3e99..734445c 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -10,8 +10,8 @@
#define IS_PACKAGE 0x2
struct st_zip_searchorder {
- char suffix[14];
- int type;
+ char suffix[14];
+ int type;
};
/* zip_searchorder defines how we search for a module in the Zip
@@ -20,13 +20,13 @@ struct st_zip_searchorder {
are swapped by initzipimport() if we run in optimized mode. Also,
'/' is replaced by SEP there. */
static struct st_zip_searchorder zip_searchorder[] = {
- {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
- {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
- {"/__init__.py", IS_PACKAGE | IS_SOURCE},
- {".pyc", IS_BYTECODE},
- {".pyo", IS_BYTECODE},
- {".py", IS_SOURCE},
- {"", 0}
+ {"/__init__.pyc", IS_PACKAGE | IS_BYTECODE},
+ {"/__init__.pyo", IS_PACKAGE | IS_BYTECODE},
+ {"/__init__.py", IS_PACKAGE | IS_SOURCE},
+ {".pyc", IS_BYTECODE},
+ {".pyo", IS_BYTECODE},
+ {".py", IS_SOURCE},
+ {"", 0}
};
/* zipimporter object definition and support */
@@ -34,10 +34,10 @@ static struct st_zip_searchorder zip_searchorder[] = {
typedef struct _zipimporter ZipImporter;
struct _zipimporter {
- PyObject_HEAD
- PyObject *archive; /* pathname of the Zip archive */
- PyObject *prefix; /* file prefix: "a/sub/directory/" */
- PyObject *files; /* dict with file info {path: toc_entry} */
+ PyObject_HEAD
+ PyObject *archive; /* pathname of the Zip archive */
+ PyObject *prefix; /* file prefix: "a/sub/directory/" */
+ PyObject *files; /* dict with file info {path: toc_entry} */
};
static PyObject *ZipImportError;
@@ -47,7 +47,7 @@ static PyObject *zip_directory_cache = NULL;
static PyObject *read_directory(char *archive);
static PyObject *get_data(char *archive, PyObject *toc_entry);
static PyObject *get_module_code(ZipImporter *self, char *fullname,
- int *p_ispackage, char **p_modpath);
+ int *p_ispackage, char **p_modpath);
#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
@@ -60,147 +60,147 @@ static PyObject *get_module_code(ZipImporter *self, char *fullname,
static int
zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
{
- char *path, *p, *prefix, buf[MAXPATHLEN+2];
- size_t len;
-
- if (!_PyArg_NoKeywords("zipimporter()", kwds))
- return -1;
-
- if (!PyArg_ParseTuple(args, "s:zipimporter", &path))
- return -1;
-
- len = strlen(path);
- if (len == 0) {
- PyErr_SetString(ZipImportError, "archive path is empty");
- return -1;
- }
- if (len >= MAXPATHLEN) {
- PyErr_SetString(ZipImportError,
- "archive path too long");
- return -1;
- }
- strcpy(buf, path);
+ char *path, *p, *prefix, buf[MAXPATHLEN+2];
+ size_t len;
+
+ if (!_PyArg_NoKeywords("zipimporter()", kwds))
+ return -1;
+
+ if (!PyArg_ParseTuple(args, "s:zipimporter", &path))
+ return -1;
+
+ len = strlen(path);
+ if (len == 0) {
+ PyErr_SetString(ZipImportError, "archive path is empty");
+ return -1;
+ }
+ if (len >= MAXPATHLEN) {
+ PyErr_SetString(ZipImportError,
+ "archive path too long");
+ return -1;
+ }
+ strcpy(buf, path);
#ifdef ALTSEP
- for (p = buf; *p; p++) {
- if (*p == ALTSEP)
- *p = SEP;
- }
+ for (p = buf; *p; p++) {
+ if (*p == ALTSEP)
+ *p = SEP;
+ }
#endif
- path = NULL;
- prefix = NULL;
- for (;;) {
- struct stat statbuf;
- int rv;
-
- rv = stat(buf, &statbuf);
- if (rv == 0) {
- /* it exists */
- if (S_ISREG(statbuf.st_mode))
- /* it's a file */
- path = buf;
- break;
- }
- /* back up one path element */
- p = strrchr(buf, SEP);
- if (prefix != NULL)
- *prefix = SEP;
- if (p == NULL)
- break;
- *p = '\0';
- prefix = p;
- }
- if (path != NULL) {
- PyObject *files;
- files = PyDict_GetItemString(zip_directory_cache, path);
- if (files == NULL) {
- files = read_directory(buf);
- if (files == NULL)
- return -1;
- if (PyDict_SetItemString(zip_directory_cache, path,
- files) != 0)
- return -1;
- }
- else
- Py_INCREF(files);
- self->files = files;
- }
- else {
- PyErr_SetString(ZipImportError, "not a Zip file");
- return -1;
- }
-
- if (prefix == NULL)
- prefix = "";
- else {
- prefix++;
- len = strlen(prefix);
- if (prefix[len-1] != SEP) {
- /* add trailing SEP */
- prefix[len] = SEP;
- prefix[len + 1] = '\0';
- }
- }
-
- self->archive = PyUnicode_FromString(buf);
- if (self->archive == NULL)
- return -1;
-
- self->prefix = PyUnicode_FromString(prefix);
- if (self->prefix == NULL)
- return -1;
-
- return 0;
+ path = NULL;
+ prefix = NULL;
+ for (;;) {
+ struct stat statbuf;
+ int rv;
+
+ rv = stat(buf, &statbuf);
+ if (rv == 0) {
+ /* it exists */
+ if (S_ISREG(statbuf.st_mode))
+ /* it's a file */
+ path = buf;
+ break;
+ }
+ /* back up one path element */
+ p = strrchr(buf, SEP);
+ if (prefix != NULL)
+ *prefix = SEP;
+ if (p == NULL)
+ break;
+ *p = '\0';
+ prefix = p;
+ }
+ if (path != NULL) {
+ PyObject *files;
+ files = PyDict_GetItemString(zip_directory_cache, path);
+ if (files == NULL) {
+ files = read_directory(buf);
+ if (files == NULL)
+ return -1;
+ if (PyDict_SetItemString(zip_directory_cache, path,
+ files) != 0)
+ return -1;
+ }
+ else
+ Py_INCREF(files);
+ self->files = files;
+ }
+ else {
+ PyErr_SetString(ZipImportError, "not a Zip file");
+ return -1;
+ }
+
+ if (prefix == NULL)
+ prefix = "";
+ else {
+ prefix++;
+ len = strlen(prefix);
+ if (prefix[len-1] != SEP) {
+ /* add trailing SEP */
+ prefix[len] = SEP;
+ prefix[len + 1] = '\0';
+ }
+ }
+
+ self->archive = PyUnicode_FromString(buf);
+ if (self->archive == NULL)
+ return -1;
+
+ self->prefix = PyUnicode_FromString(prefix);
+ if (self->prefix == NULL)
+ return -1;
+
+ return 0;
}
/* GC support. */
static int
zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
{
- ZipImporter *self = (ZipImporter *)obj;
- Py_VISIT(self->files);
- return 0;
+ ZipImporter *self = (ZipImporter *)obj;
+ Py_VISIT(self->files);
+ return 0;
}
static void
zipimporter_dealloc(ZipImporter *self)
{
- PyObject_GC_UnTrack(self);
- Py_XDECREF(self->archive);
- Py_XDECREF(self->prefix);
- Py_XDECREF(self->files);
- Py_TYPE(self)->tp_free((PyObject *)self);
+ PyObject_GC_UnTrack(self);
+ Py_XDECREF(self->archive);
+ Py_XDECREF(self->prefix);
+ Py_XDECREF(self->files);
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *
zipimporter_repr(ZipImporter *self)
{
- char *archive = "???";
- char *prefix = "";
-
- if (self->archive != NULL && PyUnicode_Check(self->archive))
- archive = _PyUnicode_AsString(self->archive);
- if (self->prefix != NULL && PyUnicode_Check(self->prefix))
- prefix = _PyUnicode_AsString(self->prefix);
- if (prefix != NULL && *prefix)
- return PyUnicode_FromFormat("<zipimporter object \"%.300s%c%.150s\">",
- archive, SEP, prefix);
- else
- return PyUnicode_FromFormat("<zipimporter object \"%.300s\">",
- archive);
+ char *archive = "???";
+ char *prefix = "";
+
+ if (self->archive != NULL && PyUnicode_Check(self->archive))
+ archive = _PyUnicode_AsString(self->archive);
+ if (self->prefix != NULL && PyUnicode_Check(self->prefix))
+ prefix = _PyUnicode_AsString(self->prefix);
+ if (prefix != NULL && *prefix)
+ return PyUnicode_FromFormat("<zipimporter object \"%.300s%c%.150s\">",
+ archive, SEP, prefix);
+ else
+ return PyUnicode_FromFormat("<zipimporter object \"%.300s\">",
+ archive);
}
/* return fullname.split(".")[-1] */
static char *
get_subname(char *fullname)
{
- char *subname = strrchr(fullname, '.');
- if (subname == NULL)
- subname = fullname;
- else
- subname++;
- return subname;
+ char *subname = strrchr(fullname, '.');
+ if (subname == NULL)
+ subname = fullname;
+ else
+ subname++;
+ return subname;
}
/* Given a (sub)modulename, write the potential file path in the
@@ -209,59 +209,59 @@ get_subname(char *fullname)
static int
make_filename(char *prefix, char *name, char *path)
{
- size_t len;
- char *p;
-
- len = strlen(prefix);
-
- /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
- if (len + strlen(name) + 13 >= MAXPATHLEN) {
- PyErr_SetString(ZipImportError, "path too long");
- return -1;
- }
-
- strcpy(path, prefix);
- strcpy(path + len, name);
- for (p = path + len; *p; p++) {
- if (*p == '.')
- *p = SEP;
- }
- len += strlen(name);
- assert(len < INT_MAX);
- return (int)len;
+ size_t len;
+ char *p;
+
+ len = strlen(prefix);
+
+ /* self.prefix + name [+ SEP + "__init__"] + ".py[co]" */
+ if (len + strlen(name) + 13 >= MAXPATHLEN) {
+ PyErr_SetString(ZipImportError, "path too long");
+ return -1;
+ }
+
+ strcpy(path, prefix);
+ strcpy(path + len, name);
+ for (p = path + len; *p; p++) {
+ if (*p == '.')
+ *p = SEP;
+ }
+ len += strlen(name);
+ assert(len < INT_MAX);
+ return (int)len;
}
enum zi_module_info {
- MI_ERROR,
- MI_NOT_FOUND,
- MI_MODULE,
- MI_PACKAGE
+ MI_ERROR,
+ MI_NOT_FOUND,
+ MI_MODULE,
+ MI_PACKAGE
};
/* Return some information about a module. */
static enum zi_module_info
get_module_info(ZipImporter *self, char *fullname)
{
- char *subname, path[MAXPATHLEN + 1];
- int len;
- struct st_zip_searchorder *zso;
-
- subname = get_subname(fullname);
-
- len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
- if (len < 0)
- return MI_ERROR;
-
- for (zso = zip_searchorder; *zso->suffix; zso++) {
- strcpy(path + len, zso->suffix);
- if (PyDict_GetItemString(self->files, path) != NULL) {
- if (zso->type & IS_PACKAGE)
- return MI_PACKAGE;
- else
- return MI_MODULE;
- }
- }
- return MI_NOT_FOUND;
+ char *subname, path[MAXPATHLEN + 1];
+ int len;
+ struct st_zip_searchorder *zso;
+
+ subname = get_subname(fullname);
+
+ len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
+ if (len < 0)
+ return MI_ERROR;
+
+ for (zso = zip_searchorder; *zso->suffix; zso++) {
+ strcpy(path + len, zso->suffix);
+ if (PyDict_GetItemString(self->files, path) != NULL) {
+ if (zso->type & IS_PACKAGE)
+ return MI_PACKAGE;
+ else
+ return MI_MODULE;
+ }
+ }
+ return MI_NOT_FOUND;
}
/* Check whether we can satisfy the import of the module named by
@@ -269,86 +269,86 @@ get_module_info(ZipImporter *self, char *fullname)
static PyObject *
zipimporter_find_module(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- PyObject *path = NULL;
- char *fullname;
- enum zi_module_info mi;
-
- if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module",
- &fullname, &path))
- return NULL;
-
- mi = get_module_info(self, fullname);
- if (mi == MI_ERROR)
- return NULL;
- if (mi == MI_NOT_FOUND) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- Py_INCREF(self);
- return (PyObject *)self;
+ ZipImporter *self = (ZipImporter *)obj;
+ PyObject *path = NULL;
+ char *fullname;
+ enum zi_module_info mi;
+
+ if (!PyArg_ParseTuple(args, "s|O:zipimporter.find_module",
+ &fullname, &path))
+ return NULL;
+
+ mi = get_module_info(self, fullname);
+ if (mi == MI_ERROR)
+ return NULL;
+ if (mi == MI_NOT_FOUND) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ Py_INCREF(self);
+ return (PyObject *)self;
}
/* Load and return the module named by 'fullname'. */
static PyObject *
zipimporter_load_module(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- PyObject *code, *mod, *dict;
- char *fullname, *modpath;
- int ispackage;
-
- if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
- &fullname))
- return NULL;
-
- code = get_module_code(self, fullname, &ispackage, &modpath);
- if (code == NULL)
- return NULL;
-
- mod = PyImport_AddModule(fullname);
- if (mod == NULL) {
- Py_DECREF(code);
- return NULL;
- }
- dict = PyModule_GetDict(mod);
-
- /* mod.__loader__ = self */
- if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
- goto error;
-
- if (ispackage) {
- /* add __path__ to the module *before* the code gets
- executed */
- PyObject *pkgpath, *fullpath;
- char *subname = get_subname(fullname);
- int err;
-
- fullpath = PyUnicode_FromFormat("%U%c%U%s",
- self->archive, SEP,
- self->prefix, subname);
- if (fullpath == NULL)
- goto error;
-
- pkgpath = Py_BuildValue("[O]", fullpath);
- Py_DECREF(fullpath);
- if (pkgpath == NULL)
- goto error;
- err = PyDict_SetItemString(dict, "__path__", pkgpath);
- Py_DECREF(pkgpath);
- if (err != 0)
- goto error;
- }
- mod = PyImport_ExecCodeModuleEx(fullname, code, modpath);
- Py_DECREF(code);
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # loaded from Zip %s\n",
- fullname, modpath);
- return mod;
+ ZipImporter *self = (ZipImporter *)obj;
+ PyObject *code, *mod, *dict;
+ char *fullname, *modpath;
+ int ispackage;
+
+ if (!PyArg_ParseTuple(args, "s:zipimporter.load_module",
+ &fullname))
+ return NULL;
+
+ code = get_module_code(self, fullname, &ispackage, &modpath);
+ if (code == NULL)
+ return NULL;
+
+ mod = PyImport_AddModule(fullname);
+ if (mod == NULL) {
+ Py_DECREF(code);
+ return NULL;
+ }
+ dict = PyModule_GetDict(mod);
+
+ /* mod.__loader__ = self */
+ if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0)
+ goto error;
+
+ if (ispackage) {
+ /* add __path__ to the module *before* the code gets
+ executed */
+ PyObject *pkgpath, *fullpath;
+ char *subname = get_subname(fullname);
+ int err;
+
+ fullpath = PyUnicode_FromFormat("%U%c%U%s",
+ self->archive, SEP,
+ self->prefix, subname);
+ if (fullpath == NULL)
+ goto error;
+
+ pkgpath = Py_BuildValue("[O]", fullpath);
+ Py_DECREF(fullpath);
+ if (pkgpath == NULL)
+ goto error;
+ err = PyDict_SetItemString(dict, "__path__", pkgpath);
+ Py_DECREF(pkgpath);
+ if (err != 0)
+ goto error;
+ }
+ mod = PyImport_ExecCodeModuleEx(fullname, code, modpath);
+ Py_DECREF(code);
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # loaded from Zip %s\n",
+ fullname, modpath);
+ return mod;
error:
- Py_DECREF(code);
- Py_DECREF(mod);
- return NULL;
+ Py_DECREF(code);
+ Py_DECREF(mod);
+ return NULL;
}
/* Return a string matching __file__ for the named module */
@@ -362,13 +362,13 @@ zipimporter_get_filename(PyObject *obj, PyObject *args)
if (!PyArg_ParseTuple(args, "s:zipimporter.get_filename",
&fullname))
- return NULL;
+ return NULL;
/* Deciding the filename requires working out where the code
would come from if the module was actually loaded */
code = get_module_code(self, fullname, &ispackage, &modpath);
if (code == NULL)
- return NULL;
+ return NULL;
Py_DECREF(code); /* Only need the path info */
return PyUnicode_FromString(modpath);
@@ -378,123 +378,123 @@ zipimporter_get_filename(PyObject *obj, PyObject *args)
static PyObject *
zipimporter_is_package(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- char *fullname;
- enum zi_module_info mi;
-
- if (!PyArg_ParseTuple(args, "s:zipimporter.is_package",
- &fullname))
- return NULL;
-
- mi = get_module_info(self, fullname);
- if (mi == MI_ERROR)
- return NULL;
- if (mi == MI_NOT_FOUND) {
- PyErr_Format(ZipImportError, "can't find module '%.200s'",
- fullname);
- return NULL;
- }
- return PyBool_FromLong(mi == MI_PACKAGE);
+ ZipImporter *self = (ZipImporter *)obj;
+ char *fullname;
+ enum zi_module_info mi;
+
+ if (!PyArg_ParseTuple(args, "s:zipimporter.is_package",
+ &fullname))
+ return NULL;
+
+ mi = get_module_info(self, fullname);
+ if (mi == MI_ERROR)
+ return NULL;
+ if (mi == MI_NOT_FOUND) {
+ PyErr_Format(ZipImportError, "can't find module '%.200s'",
+ fullname);
+ return NULL;
+ }
+ return PyBool_FromLong(mi == MI_PACKAGE);
}
static PyObject *
zipimporter_get_data(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- char *path;
+ ZipImporter *self = (ZipImporter *)obj;
+ char *path;
#ifdef ALTSEP
- char *p, buf[MAXPATHLEN + 1];
+ char *p, buf[MAXPATHLEN + 1];
#endif
- PyObject *toc_entry;
- Py_ssize_t len;
- char *archive_str;
+ PyObject *toc_entry;
+ Py_ssize_t len;
+ char *archive_str;
- if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path))
+ return NULL;
#ifdef ALTSEP
- if (strlen(path) >= MAXPATHLEN) {
- PyErr_SetString(ZipImportError, "path too long");
- return NULL;
- }
- strcpy(buf, path);
- for (p = buf; *p; p++) {
- if (*p == ALTSEP)
- *p = SEP;
- }
- path = buf;
+ if (strlen(path) >= MAXPATHLEN) {
+ PyErr_SetString(ZipImportError, "path too long");
+ return NULL;
+ }
+ strcpy(buf, path);
+ for (p = buf; *p; p++) {
+ if (*p == ALTSEP)
+ *p = SEP;
+ }
+ path = buf;
#endif
- archive_str = _PyUnicode_AsStringAndSize(self->archive, &len);
- if ((size_t)len < strlen(path) &&
- strncmp(path, archive_str, len) == 0 &&
- path[len] == SEP) {
- path = path + len + 1;
- }
-
- toc_entry = PyDict_GetItemString(self->files, path);
- if (toc_entry == NULL) {
- PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
- return NULL;
- }
- return get_data(archive_str, toc_entry);
+ archive_str = _PyUnicode_AsStringAndSize(self->archive, &len);
+ if ((size_t)len < strlen(path) &&
+ strncmp(path, archive_str, len) == 0 &&
+ path[len] == SEP) {
+ path = path + len + 1;
+ }
+
+ toc_entry = PyDict_GetItemString(self->files, path);
+ if (toc_entry == NULL) {
+ PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
+ return NULL;
+ }
+ return get_data(archive_str, toc_entry);
}
static PyObject *
zipimporter_get_code(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- char *fullname;
+ ZipImporter *self = (ZipImporter *)obj;
+ char *fullname;
- if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
- return NULL;
+ if (!PyArg_ParseTuple(args, "s:zipimporter.get_code", &fullname))
+ return NULL;
- return get_module_code(self, fullname, NULL, NULL);
+ return get_module_code(self, fullname, NULL, NULL);
}
static PyObject *
zipimporter_get_source(PyObject *obj, PyObject *args)
{
- ZipImporter *self = (ZipImporter *)obj;
- PyObject *toc_entry;
- char *fullname, *subname, path[MAXPATHLEN+1];
- int len;
- enum zi_module_info mi;
-
- if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname))
- return NULL;
-
- mi = get_module_info(self, fullname);
- if (mi == MI_ERROR)
- return NULL;
- if (mi == MI_NOT_FOUND) {
- PyErr_Format(ZipImportError, "can't find module '%.200s'",
- fullname);
- return NULL;
- }
- subname = get_subname(fullname);
-
- len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
- if (len < 0)
- return NULL;
-
- if (mi == MI_PACKAGE) {
- path[len] = SEP;
- strcpy(path + len + 1, "__init__.py");
- }
- else
- strcpy(path + len, ".py");
-
- toc_entry = PyDict_GetItemString(self->files, path);
- if (toc_entry != NULL) {
- PyObject *bytes = get_data(_PyUnicode_AsString(self->archive), toc_entry);
- PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes));
- Py_XDECREF(bytes);
- return res;
- }
-
- /* we have the module, but no source */
- Py_INCREF(Py_None);
- return Py_None;
+ ZipImporter *self = (ZipImporter *)obj;
+ PyObject *toc_entry;
+ char *fullname, *subname, path[MAXPATHLEN+1];
+ int len;
+ enum zi_module_info mi;
+
+ if (!PyArg_ParseTuple(args, "s:zipimporter.get_source", &fullname))
+ return NULL;
+
+ mi = get_module_info(self, fullname);
+ if (mi == MI_ERROR)
+ return NULL;
+ if (mi == MI_NOT_FOUND) {
+ PyErr_Format(ZipImportError, "can't find module '%.200s'",
+ fullname);
+ return NULL;
+ }
+ subname = get_subname(fullname);
+
+ len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
+ if (len < 0)
+ return NULL;
+
+ if (mi == MI_PACKAGE) {
+ path[len] = SEP;
+ strcpy(path + len + 1, "__init__.py");
+ }
+ else
+ strcpy(path + len, ".py");
+
+ toc_entry = PyDict_GetItemString(self->files, path);
+ if (toc_entry != NULL) {
+ PyObject *bytes = get_data(_PyUnicode_AsString(self->archive), toc_entry);
+ PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes));
+ Py_XDECREF(bytes);
+ return res;
+ }
+
+ /* we have the module, but no source */
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(doc_find_module,
@@ -545,28 +545,28 @@ PyDoc_STRVAR(doc_get_filename,
Return the filename for the specified module.");
static PyMethodDef zipimporter_methods[] = {
- {"find_module", zipimporter_find_module, METH_VARARGS,
- doc_find_module},
- {"load_module", zipimporter_load_module, METH_VARARGS,
- doc_load_module},
- {"get_data", zipimporter_get_data, METH_VARARGS,
- doc_get_data},
- {"get_code", zipimporter_get_code, METH_VARARGS,
- doc_get_code},
- {"get_source", zipimporter_get_source, METH_VARARGS,
- doc_get_source},
- {"get_filename", zipimporter_get_filename, METH_VARARGS,
- doc_get_filename},
- {"is_package", zipimporter_is_package, METH_VARARGS,
- doc_is_package},
- {NULL, NULL} /* sentinel */
+ {"find_module", zipimporter_find_module, METH_VARARGS,
+ doc_find_module},
+ {"load_module", zipimporter_load_module, METH_VARARGS,
+ doc_load_module},
+ {"get_data", zipimporter_get_data, METH_VARARGS,
+ doc_get_data},
+ {"get_code", zipimporter_get_code, METH_VARARGS,
+ doc_get_code},
+ {"get_source", zipimporter_get_source, METH_VARARGS,
+ doc_get_source},
+ {"get_filename", zipimporter_get_filename, METH_VARARGS,
+ doc_get_filename},
+ {"is_package", zipimporter_is_package, METH_VARARGS,
+ doc_is_package},
+ {NULL, NULL} /* sentinel */
};
static PyMemberDef zipimporter_members[] = {
- {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
- {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
- {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
- {NULL}
+ {"archive", T_OBJECT, offsetof(ZipImporter, archive), READONLY},
+ {"prefix", T_OBJECT, offsetof(ZipImporter, prefix), READONLY},
+ {"_files", T_OBJECT, offsetof(ZipImporter, files), READONLY},
+ {NULL}
};
PyDoc_STRVAR(zipimporter_doc,
@@ -586,46 +586,46 @@ zipfile targeted.");
#define DEFERRED_ADDRESS(ADDR) 0
static PyTypeObject ZipImporter_Type = {
- PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
- "zipimport.zipimporter",
- sizeof(ZipImporter),
- 0, /* tp_itemsize */
- (destructor)zipimporter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)zipimporter_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_HAVE_GC, /* tp_flags */
- zipimporter_doc, /* tp_doc */
- zipimporter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- zipimporter_methods, /* tp_methods */
- zipimporter_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)zipimporter_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
+ "zipimport.zipimporter",
+ sizeof(ZipImporter),
+ 0, /* tp_itemsize */
+ (destructor)zipimporter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)zipimporter_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ zipimporter_doc, /* tp_doc */
+ zipimporter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ zipimporter_methods, /* tp_methods */
+ zipimporter_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)zipimporter_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -636,16 +636,16 @@ static PyTypeObject ZipImporter_Type = {
marshal.c:r_long() */
static long
get_long(unsigned char *buf) {
- long x;
- x = buf[0];
- x |= (long)buf[1] << 8;
- x |= (long)buf[2] << 16;
- x |= (long)buf[3] << 24;
+ long x;
+ x = buf[0];
+ x |= (long)buf[1] << 8;
+ x |= (long)buf[2] << 16;
+ x |= (long)buf[3] << 24;
#if SIZEOF_LONG > 4
- /* Sign extension for 64-bit machines */
- x |= -(x & 0x80000000L);
+ /* Sign extension for 64-bit machines */
+ x |= -(x & 0x80000000L);
#endif
- return x;
+ return x;
}
/*
@@ -657,13 +657,13 @@ get_long(unsigned char *buf) {
A toc_entry is a tuple:
(__file__, # value to use for __file__, available for all files
- compress, # compression kind; 0 for uncompressed
- data_size, # size of compressed data on disk
- file_size, # size of decompressed data
- file_offset, # offset of file header from start of archive
- time, # mod time of file (in dos format)
- date, # mod data of file (in dos format)
- crc, # crc checksum of the data
+ compress, # compression kind; 0 for uncompressed
+ data_size, # size of compressed data on disk
+ file_size, # size of decompressed data
+ file_offset, # offset of file header from start of archive
+ time, # mod time of file (in dos format)
+ date, # mod data of file (in dos format)
+ crc, # crc checksum of the data
)
Directories can be recognized by the trailing SEP in the name,
@@ -672,115 +672,115 @@ get_long(unsigned char *buf) {
static PyObject *
read_directory(char *archive)
{
- PyObject *files = NULL;
- FILE *fp;
- long compress, crc, data_size, file_size, file_offset, date, time;
- long header_offset, name_size, header_size, header_position;
- long i, l, count;
- size_t length;
- char path[MAXPATHLEN + 5];
- char name[MAXPATHLEN + 5];
- char *p, endof_central_dir[22];
- long arc_offset; /* offset from beginning of file to start of zip-archive */
-
- if (strlen(archive) > MAXPATHLEN) {
- PyErr_SetString(PyExc_OverflowError,
- "Zip path name is too long");
- return NULL;
- }
- strcpy(path, archive);
-
- fp = fopen(archive, "rb");
- if (fp == NULL) {
- PyErr_Format(ZipImportError, "can't open Zip file: "
- "'%.200s'", archive);
- return NULL;
- }
- fseek(fp, -22, SEEK_END);
- header_position = ftell(fp);
- if (fread(endof_central_dir, 1, 22, fp) != 22) {
- fclose(fp);
- PyErr_Format(ZipImportError, "can't read Zip file: "
- "'%.200s'", archive);
- return NULL;
- }
- if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
- /* Bad: End of Central Dir signature */
- fclose(fp);
- PyErr_Format(ZipImportError, "not a Zip file: "
- "'%.200s'", archive);
- return NULL;
- }
-
- header_size = get_long((unsigned char *)endof_central_dir + 12);
- header_offset = get_long((unsigned char *)endof_central_dir + 16);
- arc_offset = header_position - header_offset - header_size;
- header_offset += arc_offset;
-
- files = PyDict_New();
- if (files == NULL)
- goto error;
-
- length = (long)strlen(path);
- path[length] = SEP;
-
- /* Start of Central Directory */
- count = 0;
- for (;;) {
- PyObject *t;
- int err;
-
- fseek(fp, header_offset, 0); /* Start of file header */
- l = PyMarshal_ReadLongFromFile(fp);
- if (l != 0x02014B50)
- break; /* Bad: Central Dir File Header */
- fseek(fp, header_offset + 10, 0);
- compress = PyMarshal_ReadShortFromFile(fp);
- time = PyMarshal_ReadShortFromFile(fp);
- date = PyMarshal_ReadShortFromFile(fp);
- crc = PyMarshal_ReadLongFromFile(fp);
- data_size = PyMarshal_ReadLongFromFile(fp);
- file_size = PyMarshal_ReadLongFromFile(fp);
- name_size = PyMarshal_ReadShortFromFile(fp);
- header_size = 46 + name_size +
- PyMarshal_ReadShortFromFile(fp) +
- PyMarshal_ReadShortFromFile(fp);
- fseek(fp, header_offset + 42, 0);
- file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
- if (name_size > MAXPATHLEN)
- name_size = MAXPATHLEN;
-
- p = name;
- for (i = 0; i < name_size; i++) {
- *p = (char)getc(fp);
- if (*p == '/')
- *p = SEP;
- p++;
- }
- *p = 0; /* Add terminating null byte */
- header_offset += header_size;
-
- strncpy(path + length + 1, name, MAXPATHLEN - length - 1);
-
- t = Py_BuildValue("siiiiiii", path, compress, data_size,
- file_size, file_offset, time, date, crc);
- if (t == NULL)
- goto error;
- err = PyDict_SetItemString(files, name, t);
- Py_DECREF(t);
- if (err != 0)
- goto error;
- count++;
- }
- fclose(fp);
- if (Py_VerboseFlag)
- PySys_WriteStderr("# zipimport: found %ld names in %s\n",
- count, archive);
- return files;
+ PyObject *files = NULL;
+ FILE *fp;
+ long compress, crc, data_size, file_size, file_offset, date, time;
+ long header_offset, name_size, header_size, header_position;
+ long i, l, count;
+ size_t length;
+ char path[MAXPATHLEN + 5];
+ char name[MAXPATHLEN + 5];
+ char *p, endof_central_dir[22];
+ long arc_offset; /* offset from beginning of file to start of zip-archive */
+
+ if (strlen(archive) > MAXPATHLEN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Zip path name is too long");
+ return NULL;
+ }
+ strcpy(path, archive);
+
+ fp = fopen(archive, "rb");
+ if (fp == NULL) {
+ PyErr_Format(ZipImportError, "can't open Zip file: "
+ "'%.200s'", archive);
+ return NULL;
+ }
+ fseek(fp, -22, SEEK_END);
+ header_position = ftell(fp);
+ if (fread(endof_central_dir, 1, 22, fp) != 22) {
+ fclose(fp);
+ PyErr_Format(ZipImportError, "can't read Zip file: "
+ "'%.200s'", archive);
+ return NULL;
+ }
+ if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
+ /* Bad: End of Central Dir signature */
+ fclose(fp);
+ PyErr_Format(ZipImportError, "not a Zip file: "
+ "'%.200s'", archive);
+ return NULL;
+ }
+
+ header_size = get_long((unsigned char *)endof_central_dir + 12);
+ header_offset = get_long((unsigned char *)endof_central_dir + 16);
+ arc_offset = header_position - header_offset - header_size;
+ header_offset += arc_offset;
+
+ files = PyDict_New();
+ if (files == NULL)
+ goto error;
+
+ length = (long)strlen(path);
+ path[length] = SEP;
+
+ /* Start of Central Directory */
+ count = 0;
+ for (;;) {
+ PyObject *t;
+ int err;
+
+ fseek(fp, header_offset, 0); /* Start of file header */
+ l = PyMarshal_ReadLongFromFile(fp);
+ if (l != 0x02014B50)
+ break; /* Bad: Central Dir File Header */
+ fseek(fp, header_offset + 10, 0);
+ compress = PyMarshal_ReadShortFromFile(fp);
+ time = PyMarshal_ReadShortFromFile(fp);
+ date = PyMarshal_ReadShortFromFile(fp);
+ crc = PyMarshal_ReadLongFromFile(fp);
+ data_size = PyMarshal_ReadLongFromFile(fp);
+ file_size = PyMarshal_ReadLongFromFile(fp);
+ name_size = PyMarshal_ReadShortFromFile(fp);
+ header_size = 46 + name_size +
+ PyMarshal_ReadShortFromFile(fp) +
+ PyMarshal_ReadShortFromFile(fp);
+ fseek(fp, header_offset + 42, 0);
+ file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset;
+ if (name_size > MAXPATHLEN)
+ name_size = MAXPATHLEN;
+
+ p = name;
+ for (i = 0; i < name_size; i++) {
+ *p = (char)getc(fp);
+ if (*p == '/')
+ *p = SEP;
+ p++;
+ }
+ *p = 0; /* Add terminating null byte */
+ header_offset += header_size;
+
+ strncpy(path + length + 1, name, MAXPATHLEN - length - 1);
+
+ t = Py_BuildValue("siiiiiii", path, compress, data_size,
+ file_size, file_offset, time, date, crc);
+ if (t == NULL)
+ goto error;
+ err = PyDict_SetItemString(files, name, t);
+ Py_DECREF(t);
+ if (err != 0)
+ goto error;
+ count++;
+ }
+ fclose(fp);
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# zipimport: found %ld names in %s\n",
+ count, archive);
+ return files;
error:
- fclose(fp);
- Py_XDECREF(files);
- return NULL;
+ fclose(fp);
+ Py_XDECREF(files);
+ return NULL;
}
/* Return the zlib.decompress function object, or NULL if zlib couldn't
@@ -790,31 +790,31 @@ error:
static PyObject *
get_decompress_func(void)
{
- static PyObject *decompress = NULL;
-
- if (decompress == NULL) {
- PyObject *zlib;
- static int importing_zlib = 0;
-
- if (importing_zlib != 0)
- /* Someone has a zlib.py[co] in their Zip file;
- let's avoid a stack overflow. */
- return NULL;
- importing_zlib = 1;
- zlib = PyImport_ImportModuleNoBlock("zlib");
- importing_zlib = 0;
- if (zlib != NULL) {
- decompress = PyObject_GetAttrString(zlib,
- "decompress");
- Py_DECREF(zlib);
- }
- else
- PyErr_Clear();
- if (Py_VerboseFlag)
- PySys_WriteStderr("# zipimport: zlib %s\n",
- zlib != NULL ? "available": "UNAVAILABLE");
- }
- return decompress;
+ static PyObject *decompress = NULL;
+
+ if (decompress == NULL) {
+ PyObject *zlib;
+ static int importing_zlib = 0;
+
+ if (importing_zlib != 0)
+ /* Someone has a zlib.py[co] in their Zip file;
+ let's avoid a stack overflow. */
+ return NULL;
+ importing_zlib = 1;
+ zlib = PyImport_ImportModuleNoBlock("zlib");
+ importing_zlib = 0;
+ if (zlib != NULL) {
+ decompress = PyObject_GetAttrString(zlib,
+ "decompress");
+ Py_DECREF(zlib);
+ }
+ else
+ PyErr_Clear();
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# zipimport: zlib %s\n",
+ zlib != NULL ? "available": "UNAVAILABLE");
+ }
+ return decompress;
}
/* Given a path to a Zip file and a toc_entry, return the (uncompressed)
@@ -822,91 +822,91 @@ get_decompress_func(void)
static PyObject *
get_data(char *archive, PyObject *toc_entry)
{
- PyObject *raw_data, *data = NULL, *decompress;
- char *buf;
- FILE *fp;
- int err;
- Py_ssize_t bytes_read = 0;
- long l;
- char *datapath;
- long compress, data_size, file_size, file_offset, bytes_size;
- long time, date, crc;
-
- if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress,
- &data_size, &file_size, &file_offset, &time,
- &date, &crc)) {
- return NULL;
- }
-
- fp = fopen(archive, "rb");
- if (!fp) {
- PyErr_Format(PyExc_IOError,
- "zipimport: can not open file %s", archive);
- return NULL;
- }
-
- /* Check to make sure the local file header is correct */
- fseek(fp, file_offset, 0);
- l = PyMarshal_ReadLongFromFile(fp);
- if (l != 0x04034B50) {
- /* Bad: Local File Header */
- PyErr_Format(ZipImportError,
- "bad local file header in %s",
- archive);
- fclose(fp);
- return NULL;
- }
- fseek(fp, file_offset + 26, 0);
- l = 30 + PyMarshal_ReadShortFromFile(fp) +
- PyMarshal_ReadShortFromFile(fp); /* local header size */
- file_offset += l; /* Start of file data */
-
- bytes_size = compress == 0 ? data_size : data_size + 1;
- if (bytes_size == 0)
- bytes_size++;
- raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
-
- if (raw_data == NULL) {
- fclose(fp);
- return NULL;
- }
- buf = PyBytes_AsString(raw_data);
-
- err = fseek(fp, file_offset, 0);
- if (err == 0)
- bytes_read = fread(buf, 1, data_size, fp);
- fclose(fp);
- if (err || bytes_read != data_size) {
- PyErr_SetString(PyExc_IOError,
- "zipimport: can't read data");
- Py_DECREF(raw_data);
- return NULL;
- }
-
- if (compress != 0) {
- buf[data_size] = 'Z'; /* saw this in zipfile.py */
- data_size++;
- }
- buf[data_size] = '\0';
-
- if (compress == 0) { /* data is not compressed */
- data = PyBytes_FromStringAndSize(buf, data_size);
- Py_DECREF(raw_data);
- return data;
- }
-
- /* Decompress with zlib */
- decompress = get_decompress_func();
- if (decompress == NULL) {
- PyErr_SetString(ZipImportError,
- "can't decompress data; "
- "zlib not available");
- goto error;
- }
- data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
+ PyObject *raw_data, *data = NULL, *decompress;
+ char *buf;
+ FILE *fp;
+ int err;
+ Py_ssize_t bytes_read = 0;
+ long l;
+ char *datapath;
+ long compress, data_size, file_size, file_offset, bytes_size;
+ long time, date, crc;
+
+ if (!PyArg_ParseTuple(toc_entry, "slllllll", &datapath, &compress,
+ &data_size, &file_size, &file_offset, &time,
+ &date, &crc)) {
+ return NULL;
+ }
+
+ fp = fopen(archive, "rb");
+ if (!fp) {
+ PyErr_Format(PyExc_IOError,
+ "zipimport: can not open file %s", archive);
+ return NULL;
+ }
+
+ /* Check to make sure the local file header is correct */
+ fseek(fp, file_offset, 0);
+ l = PyMarshal_ReadLongFromFile(fp);
+ if (l != 0x04034B50) {
+ /* Bad: Local File Header */
+ PyErr_Format(ZipImportError,
+ "bad local file header in %s",
+ archive);
+ fclose(fp);
+ return NULL;
+ }
+ fseek(fp, file_offset + 26, 0);
+ l = 30 + PyMarshal_ReadShortFromFile(fp) +
+ PyMarshal_ReadShortFromFile(fp); /* local header size */
+ file_offset += l; /* Start of file data */
+
+ bytes_size = compress == 0 ? data_size : data_size + 1;
+ if (bytes_size == 0)
+ bytes_size++;
+ raw_data = PyBytes_FromStringAndSize((char *)NULL, bytes_size);
+
+ if (raw_data == NULL) {
+ fclose(fp);
+ return NULL;
+ }
+ buf = PyBytes_AsString(raw_data);
+
+ err = fseek(fp, file_offset, 0);
+ if (err == 0)
+ bytes_read = fread(buf, 1, data_size, fp);
+ fclose(fp);
+ if (err || bytes_read != data_size) {
+ PyErr_SetString(PyExc_IOError,
+ "zipimport: can't read data");
+ Py_DECREF(raw_data);
+ return NULL;
+ }
+
+ if (compress != 0) {
+ buf[data_size] = 'Z'; /* saw this in zipfile.py */
+ data_size++;
+ }
+ buf[data_size] = '\0';
+
+ if (compress == 0) { /* data is not compressed */
+ data = PyBytes_FromStringAndSize(buf, data_size);
+ Py_DECREF(raw_data);
+ return data;
+ }
+
+ /* Decompress with zlib */
+ decompress = get_decompress_func();
+ if (decompress == NULL) {
+ PyErr_SetString(ZipImportError,
+ "can't decompress data; "
+ "zlib not available");
+ goto error;
+ }
+ data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
error:
- Py_DECREF(raw_data);
- return data;
+ Py_DECREF(raw_data);
+ return data;
}
/* Lenient date/time comparison function. The precision of the mtime
@@ -915,11 +915,11 @@ error:
static int
eq_mtime(time_t t1, time_t t2)
{
- time_t d = t1 - t2;
- if (d < 0)
- d = -d;
- /* dostime only stores even seconds, so be lenient */
- return d <= 1;
+ time_t d = t1 - t2;
+ if (d < 0)
+ d = -d;
+ /* dostime only stores even seconds, so be lenient */
+ return d <= 1;
}
/* Given the contents of a .py[co] file in a buffer, unmarshal the data
@@ -930,44 +930,44 @@ eq_mtime(time_t t1, time_t t2)
static PyObject *
unmarshal_code(char *pathname, PyObject *data, time_t mtime)
{
- PyObject *code;
- char *buf = PyBytes_AsString(data);
- Py_ssize_t size = PyBytes_Size(data);
-
- if (size <= 9) {
- PyErr_SetString(ZipImportError,
- "bad pyc data");
- return NULL;
- }
-
- if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# %s has bad magic\n",
- pathname);
- Py_INCREF(Py_None);
- return Py_None; /* signal caller to try alternative */
- }
-
- if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
- mtime)) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# %s has bad mtime\n",
- pathname);
- Py_INCREF(Py_None);
- return Py_None; /* signal caller to try alternative */
- }
-
- code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
- if (code == NULL)
- return NULL;
- if (!PyCode_Check(code)) {
- Py_DECREF(code);
- PyErr_Format(PyExc_TypeError,
- "compiled module %.200s is not a code object",
- pathname);
- return NULL;
- }
- return code;
+ PyObject *code;
+ char *buf = PyBytes_AsString(data);
+ Py_ssize_t size = PyBytes_Size(data);
+
+ if (size <= 9) {
+ PyErr_SetString(ZipImportError,
+ "bad pyc data");
+ return NULL;
+ }
+
+ if (get_long((unsigned char *)buf) != PyImport_GetMagicNumber()) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# %s has bad magic\n",
+ pathname);
+ Py_INCREF(Py_None);
+ return Py_None; /* signal caller to try alternative */
+ }
+
+ if (mtime != 0 && !eq_mtime(get_long((unsigned char *)buf + 4),
+ mtime)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# %s has bad mtime\n",
+ pathname);
+ Py_INCREF(Py_None);
+ return Py_None; /* signal caller to try alternative */
+ }
+
+ code = PyMarshal_ReadObjectFromString(buf + 8, size - 8);
+ if (code == NULL)
+ return NULL;
+ if (!PyCode_Check(code)) {
+ Py_DECREF(code);
+ PyErr_Format(PyExc_TypeError,
+ "compiled module %.200s is not a code object",
+ pathname);
+ return NULL;
+ }
+ return code;
}
/* Replace any occurances of "\r\n?" in the input string with "\n".
@@ -977,38 +977,38 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime)
static PyObject *
normalize_line_endings(PyObject *source)
{
- char *buf, *q, *p = PyBytes_AsString(source);
- PyObject *fixed_source;
- int len = 0;
-
- if (!p) {
- return PyBytes_FromStringAndSize("\n\0", 2);
- }
-
- /* one char extra for trailing \n and one for terminating \0 */
- buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
- if (buf == NULL) {
- PyErr_SetString(PyExc_MemoryError,
- "zipimport: no memory to allocate "
- "source buffer");
- return NULL;
- }
- /* replace "\r\n?" by "\n" */
- for (q = buf; *p != '\0'; p++) {
- if (*p == '\r') {
- *q++ = '\n';
- if (*(p + 1) == '\n')
- p++;
- }
- else
- *q++ = *p;
- len++;
- }
- *q++ = '\n'; /* add trailing \n */
- *q = '\0';
- fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
- PyMem_Free(buf);
- return fixed_source;
+ char *buf, *q, *p = PyBytes_AsString(source);
+ PyObject *fixed_source;
+ int len = 0;
+
+ if (!p) {
+ return PyBytes_FromStringAndSize("\n\0", 2);
+ }
+
+ /* one char extra for trailing \n and one for terminating \0 */
+ buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
+ if (buf == NULL) {
+ PyErr_SetString(PyExc_MemoryError,
+ "zipimport: no memory to allocate "
+ "source buffer");
+ return NULL;
+ }
+ /* replace "\r\n?" by "\n" */
+ for (q = buf; *p != '\0'; p++) {
+ if (*p == '\r') {
+ *q++ = '\n';
+ if (*(p + 1) == '\n')
+ p++;
+ }
+ else
+ *q++ = *p;
+ len++;
+ }
+ *q++ = '\n'; /* add trailing \n */
+ *q = '\0';
+ fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
+ PyMem_Free(buf);
+ return fixed_source;
}
/* Given a string buffer containing Python source code, compile it
@@ -1016,16 +1016,16 @@ normalize_line_endings(PyObject *source)
static PyObject *
compile_source(char *pathname, PyObject *source)
{
- PyObject *code, *fixed_source;
+ PyObject *code, *fixed_source;
- fixed_source = normalize_line_endings(source);
- if (fixed_source == NULL)
- return NULL;
+ fixed_source = normalize_line_endings(source);
+ if (fixed_source == NULL)
+ return NULL;
- code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
- Py_file_input);
- Py_DECREF(fixed_source);
- return code;
+ code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
+ Py_file_input);
+ Py_DECREF(fixed_source);
+ return code;
}
/* Convert the date/time values found in the Zip archive to a value
@@ -1033,19 +1033,19 @@ compile_source(char *pathname, PyObject *source)
static time_t
parse_dostime(int dostime, int dosdate)
{
- struct tm stm;
+ struct tm stm;
- memset((void *) &stm, '\0', sizeof(stm));
+ memset((void *) &stm, '\0', sizeof(stm));
- stm.tm_sec = (dostime & 0x1f) * 2;
- stm.tm_min = (dostime >> 5) & 0x3f;
- stm.tm_hour = (dostime >> 11) & 0x1f;
- stm.tm_mday = dosdate & 0x1f;
- stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
- stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
- stm.tm_isdst = -1; /* wday/yday is ignored */
+ stm.tm_sec = (dostime & 0x1f) * 2;
+ stm.tm_min = (dostime >> 5) & 0x3f;
+ stm.tm_hour = (dostime >> 11) & 0x1f;
+ stm.tm_mday = dosdate & 0x1f;
+ stm.tm_mon = ((dosdate >> 5) & 0x0f) - 1;
+ stm.tm_year = ((dosdate >> 9) & 0x7f) + 80;
+ stm.tm_isdst = -1; /* wday/yday is ignored */
- return mktime(&stm);
+ return mktime(&stm);
}
/* Given a path to a .pyc or .pyo file in the archive, return the
@@ -1054,106 +1054,106 @@ parse_dostime(int dostime, int dosdate)
static time_t
get_mtime_of_source(ZipImporter *self, char *path)
{
- PyObject *toc_entry;
- time_t mtime = 0;
- Py_ssize_t lastchar = strlen(path) - 1;
- char savechar = path[lastchar];
- path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
- toc_entry = PyDict_GetItemString(self->files, path);
- if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
- PyTuple_Size(toc_entry) == 8) {
- /* fetch the time stamp of the .py file for comparison
- with an embedded pyc time stamp */
- int time, date;
- time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
- date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
- mtime = parse_dostime(time, date);
- }
- path[lastchar] = savechar;
- return mtime;
+ PyObject *toc_entry;
+ time_t mtime = 0;
+ Py_ssize_t lastchar = strlen(path) - 1;
+ char savechar = path[lastchar];
+ path[lastchar] = '\0'; /* strip 'c' or 'o' from *.py[co] */
+ toc_entry = PyDict_GetItemString(self->files, path);
+ if (toc_entry != NULL && PyTuple_Check(toc_entry) &&
+ PyTuple_Size(toc_entry) == 8) {
+ /* fetch the time stamp of the .py file for comparison
+ with an embedded pyc time stamp */
+ int time, date;
+ time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5));
+ date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6));
+ mtime = parse_dostime(time, date);
+ }
+ path[lastchar] = savechar;
+ return mtime;
}
/* Return the code object for the module named by 'fullname' from the
Zip archive as a new reference. */
static PyObject *
get_code_from_data(ZipImporter *self, int ispackage, int isbytecode,
- time_t mtime, PyObject *toc_entry)
+ time_t mtime, PyObject *toc_entry)
{
- PyObject *data, *code;
- char *modpath;
- char *archive = _PyUnicode_AsString(self->archive);
-
- if (archive == NULL)
- return NULL;
-
- data = get_data(archive, toc_entry);
- if (data == NULL)
- return NULL;
-
- modpath = _PyUnicode_AsString(PyTuple_GetItem(toc_entry, 0));
-
- if (isbytecode) {
- code = unmarshal_code(modpath, data, mtime);
- }
- else {
- code = compile_source(modpath, data);
- }
- Py_DECREF(data);
- return code;
+ PyObject *data, *code;
+ char *modpath;
+ char *archive = _PyUnicode_AsString(self->archive);
+
+ if (archive == NULL)
+ return NULL;
+
+ data = get_data(archive, toc_entry);
+ if (data == NULL)
+ return NULL;
+
+ modpath = _PyUnicode_AsString(PyTuple_GetItem(toc_entry, 0));
+
+ if (isbytecode) {
+ code = unmarshal_code(modpath, data, mtime);
+ }
+ else {
+ code = compile_source(modpath, data);
+ }
+ Py_DECREF(data);
+ return code;
}
/* Get the code object assoiciated with the module specified by
'fullname'. */
static PyObject *
get_module_code(ZipImporter *self, char *fullname,
- int *p_ispackage, char **p_modpath)
+ int *p_ispackage, char **p_modpath)
{
- PyObject *toc_entry;
- char *subname, path[MAXPATHLEN + 1];
- int len;
- struct st_zip_searchorder *zso;
-
- subname = get_subname(fullname);
-
- len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
- if (len < 0)
- return NULL;
-
- for (zso = zip_searchorder; *zso->suffix; zso++) {
- PyObject *code = NULL;
-
- strcpy(path + len, zso->suffix);
- if (Py_VerboseFlag > 1)
- PySys_WriteStderr("# trying %s%c%s\n",
- _PyUnicode_AsString(self->archive),
- (int)SEP, path);
- toc_entry = PyDict_GetItemString(self->files, path);
- if (toc_entry != NULL) {
- time_t mtime = 0;
- int ispackage = zso->type & IS_PACKAGE;
- int isbytecode = zso->type & IS_BYTECODE;
-
- if (isbytecode)
- mtime = get_mtime_of_source(self, path);
- if (p_ispackage != NULL)
- *p_ispackage = ispackage;
- code = get_code_from_data(self, ispackage,
- isbytecode, mtime,
- toc_entry);
- if (code == Py_None) {
- /* bad magic number or non-matching mtime
- in byte code, try next */
- Py_DECREF(code);
- continue;
- }
- if (code != NULL && p_modpath != NULL)
- *p_modpath = _PyUnicode_AsString(
- PyTuple_GetItem(toc_entry, 0));
- return code;
- }
- }
- PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
- return NULL;
+ PyObject *toc_entry;
+ char *subname, path[MAXPATHLEN + 1];
+ int len;
+ struct st_zip_searchorder *zso;
+
+ subname = get_subname(fullname);
+
+ len = make_filename(_PyUnicode_AsString(self->prefix), subname, path);
+ if (len < 0)
+ return NULL;
+
+ for (zso = zip_searchorder; *zso->suffix; zso++) {
+ PyObject *code = NULL;
+
+ strcpy(path + len, zso->suffix);
+ if (Py_VerboseFlag > 1)
+ PySys_WriteStderr("# trying %s%c%s\n",
+ _PyUnicode_AsString(self->archive),
+ (int)SEP, path);
+ toc_entry = PyDict_GetItemString(self->files, path);
+ if (toc_entry != NULL) {
+ time_t mtime = 0;
+ int ispackage = zso->type & IS_PACKAGE;
+ int isbytecode = zso->type & IS_BYTECODE;
+
+ if (isbytecode)
+ mtime = get_mtime_of_source(self, path);
+ if (p_ispackage != NULL)
+ *p_ispackage = ispackage;
+ code = get_code_from_data(self, ispackage,
+ isbytecode, mtime,
+ toc_entry);
+ if (code == Py_None) {
+ /* bad magic number or non-matching mtime
+ in byte code, try next */
+ Py_DECREF(code);
+ continue;
+ }
+ if (code != NULL && p_modpath != NULL)
+ *p_modpath = _PyUnicode_AsString(
+ PyTuple_GetItem(toc_entry, 0));
+ return code;
+ }
+ }
+ PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
+ return NULL;
}
@@ -1174,65 +1174,65 @@ used by the builtin import mechanism for sys.path items that are paths\n\
to Zip archives.");
static struct PyModuleDef zipimportmodule = {
- PyModuleDef_HEAD_INIT,
- "zipimport",
- zipimport_doc,
- -1,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "zipimport",
+ zipimport_doc,
+ -1,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_zipimport(void)
{
- PyObject *mod;
-
- if (PyType_Ready(&ZipImporter_Type) < 0)
- return NULL;
-
- /* Correct directory separator */
- zip_searchorder[0].suffix[0] = SEP;
- zip_searchorder[1].suffix[0] = SEP;
- zip_searchorder[2].suffix[0] = SEP;
- if (Py_OptimizeFlag) {
- /* Reverse *.pyc and *.pyo */
- struct st_zip_searchorder tmp;
- tmp = zip_searchorder[0];
- zip_searchorder[0] = zip_searchorder[1];
- zip_searchorder[1] = tmp;
- tmp = zip_searchorder[3];
- zip_searchorder[3] = zip_searchorder[4];
- zip_searchorder[4] = tmp;
- }
-
- mod = PyModule_Create(&zipimportmodule);
- if (mod == NULL)
- return NULL;
-
- ZipImportError = PyErr_NewException("zipimport.ZipImportError",
- PyExc_ImportError, NULL);
- if (ZipImportError == NULL)
- return NULL;
-
- Py_INCREF(ZipImportError);
- if (PyModule_AddObject(mod, "ZipImportError",
- ZipImportError) < 0)
- return NULL;
-
- Py_INCREF(&ZipImporter_Type);
- if (PyModule_AddObject(mod, "zipimporter",
- (PyObject *)&ZipImporter_Type) < 0)
- return NULL;
-
- zip_directory_cache = PyDict_New();
- if (zip_directory_cache == NULL)
- return NULL;
- Py_INCREF(zip_directory_cache);
- if (PyModule_AddObject(mod, "_zip_directory_cache",
- zip_directory_cache) < 0)
- return NULL;
- return mod;
+ PyObject *mod;
+
+ if (PyType_Ready(&ZipImporter_Type) < 0)
+ return NULL;
+
+ /* Correct directory separator */
+ zip_searchorder[0].suffix[0] = SEP;
+ zip_searchorder[1].suffix[0] = SEP;
+ zip_searchorder[2].suffix[0] = SEP;
+ if (Py_OptimizeFlag) {
+ /* Reverse *.pyc and *.pyo */
+ struct st_zip_searchorder tmp;
+ tmp = zip_searchorder[0];
+ zip_searchorder[0] = zip_searchorder[1];
+ zip_searchorder[1] = tmp;
+ tmp = zip_searchorder[3];
+ zip_searchorder[3] = zip_searchorder[4];
+ zip_searchorder[4] = tmp;
+ }
+
+ mod = PyModule_Create(&zipimportmodule);
+ if (mod == NULL)
+ return NULL;
+
+ ZipImportError = PyErr_NewException("zipimport.ZipImportError",
+ PyExc_ImportError, NULL);
+ if (ZipImportError == NULL)
+ return NULL;
+
+ Py_INCREF(ZipImportError);
+ if (PyModule_AddObject(mod, "ZipImportError",
+ ZipImportError) < 0)
+ return NULL;
+
+ Py_INCREF(&ZipImporter_Type);
+ if (PyModule_AddObject(mod, "zipimporter",
+ (PyObject *)&ZipImporter_Type) < 0)
+ return NULL;
+
+ zip_directory_cache = PyDict_New();
+ if (zip_directory_cache == NULL)
+ return NULL;
+ Py_INCREF(zip_directory_cache);
+ if (PyModule_AddObject(mod, "_zip_directory_cache",
+ zip_directory_cache) < 0)
+ return NULL;
+ return mod;
}
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 353d11a..1f263ab 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -53,9 +53,9 @@ static void
zlib_error(z_stream zst, int err, char *msg)
{
if (zst.msg == Z_NULL)
- PyErr_Format(ZlibError, "Error %d %s", err, msg);
+ PyErr_Format(ZlibError, "Error %d %s", err, msg);
else
- PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
+ PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
}
PyDoc_STRVAR(compressobj__doc__,
@@ -74,17 +74,17 @@ newcompobject(PyTypeObject *type)
compobject *self;
self = PyObject_New(compobject, type);
if (self == NULL)
- return NULL;
+ return NULL;
self->is_initialised = 0;
self->unused_data = PyBytes_FromStringAndSize("", 0);
if (self->unused_data == NULL) {
- Py_DECREF(self);
- return NULL;
+ Py_DECREF(self);
+ return NULL;
}
self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
if (self->unconsumed_tail == NULL) {
- Py_DECREF(self);
- return NULL;
+ Py_DECREF(self);
+ return NULL;
}
#ifdef WITH_THREAD
self->lock = PyThread_allocate_lock();
@@ -108,7 +108,7 @@ PyZlib_compress(PyObject *self, PyObject *args)
/* require Python string object, optional 'level' arg */
if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
- return NULL;
+ return NULL;
input = pinput.buf;
length = pinput.len;
@@ -116,10 +116,10 @@ PyZlib_compress(PyObject *self, PyObject *args)
output = (Byte*)malloc(zst.avail_out);
if (output == NULL) {
- PyBuffer_Release(&pinput);
- PyErr_SetString(PyExc_MemoryError,
- "Can't allocate memory to compress data");
- return NULL;
+ PyBuffer_Release(&pinput);
+ PyErr_SetString(PyExc_MemoryError,
+ "Can't allocate memory to compress data");
+ return NULL;
}
/* Past the point of no return. From here on out, we need to make sure
@@ -134,19 +134,19 @@ PyZlib_compress(PyObject *self, PyObject *args)
switch(err) {
case(Z_OK):
- break;
+ break;
case(Z_MEM_ERROR):
- PyErr_SetString(PyExc_MemoryError,
- "Out of memory while compressing data");
- goto error;
+ PyErr_SetString(PyExc_MemoryError,
+ "Out of memory while compressing data");
+ goto error;
case(Z_STREAM_ERROR):
- PyErr_SetString(ZlibError,
- "Bad compression level");
- goto error;
+ PyErr_SetString(ZlibError,
+ "Bad compression level");
+ goto error;
default:
deflateEnd(&zst);
- zlib_error(zst, err, "while compressing data");
- goto error;
+ zlib_error(zst, err, "while compressing data");
+ goto error;
}
Py_BEGIN_ALLOW_THREADS;
@@ -154,17 +154,17 @@ PyZlib_compress(PyObject *self, PyObject *args)
Py_END_ALLOW_THREADS;
if (err != Z_STREAM_END) {
- zlib_error(zst, err, "while compressing data");
- deflateEnd(&zst);
- goto error;
+ zlib_error(zst, err, "while compressing data");
+ deflateEnd(&zst);
+ goto error;
}
err=deflateEnd(&zst);
if (err == Z_OK)
- ReturnVal = PyBytes_FromStringAndSize((char *)output,
+ ReturnVal = PyBytes_FromStringAndSize((char *)output,
zst.total_out);
else
- zlib_error(zst, err, "while finishing compression");
+ zlib_error(zst, err, "while finishing compression");
error:
PyBuffer_Release(&pinput);
@@ -191,20 +191,20 @@ PyZlib_decompress(PyObject *self, PyObject *args)
z_stream zst;
if (!PyArg_ParseTuple(args, "y*|in:decompress",
- &pinput, &wsize, &r_strlen))
- return NULL;
+ &pinput, &wsize, &r_strlen))
+ return NULL;
input = pinput.buf;
length = pinput.len;
if (r_strlen <= 0)
- r_strlen = 1;
+ r_strlen = 1;
zst.avail_in = length;
zst.avail_out = r_strlen;
if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
- PyBuffer_Release(&pinput);
- return NULL;
+ PyBuffer_Release(&pinput);
+ return NULL;
}
zst.zalloc = (alloc_func)NULL;
@@ -215,60 +215,60 @@ PyZlib_decompress(PyObject *self, PyObject *args)
switch(err) {
case(Z_OK):
- break;
+ break;
case(Z_MEM_ERROR):
- PyErr_SetString(PyExc_MemoryError,
- "Out of memory while decompressing data");
- goto error;
+ PyErr_SetString(PyExc_MemoryError,
+ "Out of memory while decompressing data");
+ goto error;
default:
inflateEnd(&zst);
- zlib_error(zst, err, "while preparing to decompress data");
- goto error;
+ zlib_error(zst, err, "while preparing to decompress data");
+ goto error;
}
do {
- Py_BEGIN_ALLOW_THREADS
- err=inflate(&zst, Z_FINISH);
- Py_END_ALLOW_THREADS
-
- switch(err) {
- case(Z_STREAM_END):
- break;
- case(Z_BUF_ERROR):
- /*
- * If there is at least 1 byte of room according to zst.avail_out
- * and we get this error, assume that it means zlib cannot
- * process the inflate call() due to an error in the data.
- */
- if (zst.avail_out > 0) {
- PyErr_Format(ZlibError, "Error %i while decompressing data",
- err);
- inflateEnd(&zst);
- goto error;
- }
- /* fall through */
- case(Z_OK):
- /* need more memory */
- if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
- inflateEnd(&zst);
- goto error;
- }
- zst.next_out =
+ Py_BEGIN_ALLOW_THREADS
+ err=inflate(&zst, Z_FINISH);
+ Py_END_ALLOW_THREADS
+
+ switch(err) {
+ case(Z_STREAM_END):
+ break;
+ case(Z_BUF_ERROR):
+ /*
+ * If there is at least 1 byte of room according to zst.avail_out
+ * and we get this error, assume that it means zlib cannot
+ * process the inflate call() due to an error in the data.
+ */
+ if (zst.avail_out > 0) {
+ PyErr_Format(ZlibError, "Error %i while decompressing data",
+ err);
+ inflateEnd(&zst);
+ goto error;
+ }
+ /* fall through */
+ case(Z_OK):
+ /* need more memory */
+ if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
+ inflateEnd(&zst);
+ goto error;
+ }
+ zst.next_out =
(unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
- zst.avail_out = r_strlen;
- r_strlen = r_strlen << 1;
- break;
- default:
- inflateEnd(&zst);
- zlib_error(zst, err, "while decompressing data");
- goto error;
- }
+ zst.avail_out = r_strlen;
+ r_strlen = r_strlen << 1;
+ break;
+ default:
+ inflateEnd(&zst);
+ zlib_error(zst, err, "while decompressing data");
+ goto error;
+ }
} while (err != Z_STREAM_END);
err = inflateEnd(&zst);
if (err != Z_OK) {
- zlib_error(zst, err, "while finishing data decompression");
- goto error;
+ zlib_error(zst, err, "while finishing data decompression");
+ goto error;
}
if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
@@ -291,12 +291,12 @@ PyZlib_compressobj(PyObject *selfptr, PyObject *args)
int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
- &memLevel, &strategy))
- return NULL;
+ &memLevel, &strategy))
+ return NULL;
self = newcompobject(&Comptype);
if (self==NULL)
- return(NULL);
+ return(NULL);
self->zst.zalloc = (alloc_func)NULL;
self->zst.zfree = (free_func)Z_NULL;
self->zst.next_in = NULL;
@@ -304,21 +304,21 @@ PyZlib_compressobj(PyObject *selfptr, PyObject *args)
err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
switch(err) {
case (Z_OK):
- self->is_initialised = 1;
- return (PyObject*)self;
+ self->is_initialised = 1;
+ return (PyObject*)self;
case (Z_MEM_ERROR):
- Py_DECREF(self);
- PyErr_SetString(PyExc_MemoryError,
- "Can't allocate memory for compression object");
- return NULL;
+ Py_DECREF(self);
+ PyErr_SetString(PyExc_MemoryError,
+ "Can't allocate memory for compression object");
+ return NULL;
case(Z_STREAM_ERROR):
- Py_DECREF(self);
- PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
- return NULL;
+ Py_DECREF(self);
+ PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
+ return NULL;
default:
- zlib_error(self->zst, err, "while creating compression object");
+ zlib_error(self->zst, err, "while creating compression object");
Py_DECREF(self);
- return NULL;
+ return NULL;
}
}
@@ -328,11 +328,11 @@ PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
int wbits=DEF_WBITS, err;
compobject *self;
if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
- return NULL;
+ return NULL;
self = newcompobject(&Decomptype);
if (self == NULL)
- return(NULL);
+ return(NULL);
self->zst.zalloc = (alloc_func)NULL;
self->zst.zfree = (free_func)Z_NULL;
self->zst.next_in = NULL;
@@ -340,21 +340,21 @@ PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
err = inflateInit2(&self->zst, wbits);
switch(err) {
case (Z_OK):
- self->is_initialised = 1;
- return (PyObject*)self;
+ self->is_initialised = 1;
+ return (PyObject*)self;
case(Z_STREAM_ERROR):
- Py_DECREF(self);
- PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
- return NULL;
+ Py_DECREF(self);
+ PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
+ return NULL;
case (Z_MEM_ERROR):
- Py_DECREF(self);
- PyErr_SetString(PyExc_MemoryError,
- "Can't allocate memory for decompression object");
- return NULL;
+ Py_DECREF(self);
+ PyErr_SetString(PyExc_MemoryError,
+ "Can't allocate memory for decompression object");
+ return NULL;
default:
- zlib_error(self->zst, err, "while creating decompression object");
+ zlib_error(self->zst, err, "while creating decompression object");
Py_DECREF(self);
- return NULL;
+ return NULL;
}
}
@@ -404,13 +404,13 @@ PyZlib_objcompress(compobject *self, PyObject *args)
unsigned long start_total_out;
if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
- return NULL;
+ return NULL;
input = pinput.buf;
inplen = pinput.len;
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
- PyBuffer_Release(&pinput);
- return NULL;
+ PyBuffer_Release(&pinput);
+ return NULL;
}
ENTER_ZLIB(self);
@@ -428,19 +428,19 @@ PyZlib_objcompress(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
- if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
+ if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
- goto error;
+ goto error;
}
- self->zst.next_out =
+ self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
- self->zst.avail_out = length;
- length = length << 1;
+ self->zst.avail_out = length;
+ length = length << 1;
- Py_BEGIN_ALLOW_THREADS
- err = deflate(&(self->zst), Z_NO_FLUSH);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ err = deflate(&(self->zst), Z_NO_FLUSH);
+ Py_END_ALLOW_THREADS
}
/* We will only get Z_BUF_ERROR if the output buffer was full but
there wasn't more output when we tried again, so it is not an error
@@ -448,10 +448,10 @@ PyZlib_objcompress(compobject *self, PyObject *args)
*/
if (err != Z_OK && err != Z_BUF_ERROR) {
- zlib_error(self->zst, err, "while compressing");
- Py_DECREF(RetVal);
- RetVal = NULL;
- goto error;
+ zlib_error(self->zst, err, "while compressing");
+ Py_DECREF(RetVal);
+ RetVal = NULL;
+ goto error;
}
if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(RetVal);
@@ -486,23 +486,23 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
unsigned long start_total_out;
if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
- &max_length))
- return NULL;
+ &max_length))
+ return NULL;
input = pinput.buf;
inplen = pinput.len;
if (max_length < 0) {
- PyBuffer_Release(&pinput);
- PyErr_SetString(PyExc_ValueError,
- "max_length must be greater than zero");
- return NULL;
+ PyBuffer_Release(&pinput);
+ PyErr_SetString(PyExc_ValueError,
+ "max_length must be greater than zero");
+ return NULL;
}
/* limit amount of data allocated to max_length */
if (max_length && length > max_length)
- length = max_length;
+ length = max_length;
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
- PyBuffer_Release(&pinput);
- return NULL;
+ PyBuffer_Release(&pinput);
+ return NULL;
}
ENTER_ZLIB(self);
@@ -521,43 +521,43 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
So extend the output buffer and try again.
*/
while (err == Z_OK && self->zst.avail_out == 0) {
- /* If max_length set, don't continue decompressing if we've already
- reached the limit.
- */
- if (max_length && length >= max_length)
- break;
-
- /* otherwise, ... */
- old_length = length;
- length = length << 1;
- if (max_length && length > max_length)
- length = max_length;
-
- if (_PyBytes_Resize(&RetVal, length) < 0) {
+ /* If max_length set, don't continue decompressing if we've already
+ reached the limit.
+ */
+ if (max_length && length >= max_length)
+ break;
+
+ /* otherwise, ... */
+ old_length = length;
+ length = length << 1;
+ if (max_length && length > max_length)
+ length = max_length;
+
+ if (_PyBytes_Resize(&RetVal, length) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
- goto error;
+ goto error;
}
- self->zst.next_out =
+ self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
- self->zst.avail_out = length - old_length;
+ self->zst.avail_out = length - old_length;
- Py_BEGIN_ALLOW_THREADS
- err = inflate(&(self->zst), Z_SYNC_FLUSH);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ err = inflate(&(self->zst), Z_SYNC_FLUSH);
+ Py_END_ALLOW_THREADS
}
/* Not all of the compressed data could be accommodated in the output buffer
of specified size. Return the unconsumed tail in an attribute.*/
if(max_length) {
- Py_DECREF(self->unconsumed_tail);
- self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
- self->zst.avail_in);
- if(!self->unconsumed_tail) {
- Py_DECREF(RetVal);
- RetVal = NULL;
- goto error;
- }
+ Py_DECREF(self->unconsumed_tail);
+ self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
+ self->zst.avail_in);
+ if(!self->unconsumed_tail) {
+ Py_DECREF(RetVal);
+ RetVal = NULL;
+ goto error;
+ }
}
/* The end of the compressed data has been reached, so set the
@@ -567,22 +567,22 @@ PyZlib_objdecompress(compobject *self, PyObject *args)
preserved.
*/
if (err == Z_STREAM_END) {
- Py_XDECREF(self->unused_data); /* Free original empty string */
- self->unused_data = PyBytes_FromStringAndSize(
- (char *)self->zst.next_in, self->zst.avail_in);
- if (self->unused_data == NULL) {
- Py_DECREF(RetVal);
- goto error;
- }
- /* We will only get Z_BUF_ERROR if the output buffer was full
- but there wasn't more output when we tried again, so it is
- not an error condition.
- */
+ Py_XDECREF(self->unused_data); /* Free original empty string */
+ self->unused_data = PyBytes_FromStringAndSize(
+ (char *)self->zst.next_in, self->zst.avail_in);
+ if (self->unused_data == NULL) {
+ Py_DECREF(RetVal);
+ goto error;
+ }
+ /* We will only get Z_BUF_ERROR if the output buffer was full
+ but there wasn't more output when we tried again, so it is
+ not an error condition.
+ */
} else if (err != Z_OK && err != Z_BUF_ERROR) {
- zlib_error(self->zst, err, "while decompressing");
- Py_DECREF(RetVal);
- RetVal = NULL;
- goto error;
+ zlib_error(self->zst, err, "while decompressing");
+ Py_DECREF(RetVal);
+ RetVal = NULL;
+ goto error;
}
if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
@@ -613,16 +613,16 @@ PyZlib_flush(compobject *self, PyObject *args)
unsigned long start_total_out;
if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
- return NULL;
+ return NULL;
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
doing any work at all; just return an empty string. */
if (flushmode == Z_NO_FLUSH) {
- return PyBytes_FromStringAndSize(NULL, 0);
+ return PyBytes_FromStringAndSize(NULL, 0);
}
if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
- return NULL;
+ return NULL;
ENTER_ZLIB(self);
@@ -638,44 +638,44 @@ PyZlib_flush(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while (err == Z_OK && self->zst.avail_out == 0) {
- if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
+ if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Py_DECREF(RetVal);
RetVal = NULL;
- goto error;
+ goto error;
}
- self->zst.next_out =
+ self->zst.next_out =
(unsigned char *)PyBytes_AS_STRING(RetVal) + length;
- self->zst.avail_out = length;
- length = length << 1;
+ self->zst.avail_out = length;
+ length = length << 1;
- Py_BEGIN_ALLOW_THREADS
- err = deflate(&(self->zst), flushmode);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ err = deflate(&(self->zst), flushmode);
+ Py_END_ALLOW_THREADS
}
/* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
various data structures. Note we should only get Z_STREAM_END when
flushmode is Z_FINISH, but checking both for safety*/
if (err == Z_STREAM_END && flushmode == Z_FINISH) {
- err = deflateEnd(&(self->zst));
- if (err != Z_OK) {
- zlib_error(self->zst, err, "from deflateEnd()");
- Py_DECREF(RetVal);
- RetVal = NULL;
- goto error;
- }
- else
- self->is_initialised = 0;
-
- /* We will only get Z_BUF_ERROR if the output buffer was full
- but there wasn't more output when we tried again, so it is
- not an error condition.
- */
+ err = deflateEnd(&(self->zst));
+ if (err != Z_OK) {
+ zlib_error(self->zst, err, "from deflateEnd()");
+ Py_DECREF(RetVal);
+ RetVal = NULL;
+ goto error;
+ }
+ else
+ self->is_initialised = 0;
+
+ /* We will only get Z_BUF_ERROR if the output buffer was full
+ but there wasn't more output when we tried again, so it is
+ not an error condition.
+ */
} else if (err!=Z_OK && err!=Z_BUF_ERROR) {
- zlib_error(self->zst, err, "while flushing");
- Py_DECREF(RetVal);
- RetVal = NULL;
- goto error;
+ zlib_error(self->zst, err, "while flushing");
+ Py_DECREF(RetVal);
+ RetVal = NULL;
+ goto error;
}
if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
@@ -807,13 +807,13 @@ PyZlib_unflush(compobject *self, PyObject *args)
unsigned long start_total_out;
if (!PyArg_ParseTuple(args, "|i:flush", &length))
- return NULL;
+ return NULL;
if (length <= 0) {
- PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
- return NULL;
+ PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
+ return NULL;
}
if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
- return NULL;
+ return NULL;
ENTER_ZLIB(self);
@@ -829,32 +829,32 @@ PyZlib_unflush(compobject *self, PyObject *args)
/* while Z_OK and the output buffer is full, there might be more output,
so extend the output buffer and try again */
while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
- if (_PyBytes_Resize(&retval, length << 1) < 0) {
+ if (_PyBytes_Resize(&retval, length << 1) < 0) {
Py_DECREF(retval);
retval = NULL;
- goto error;
+ goto error;
}
- self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
- self->zst.avail_out = length;
- length = length << 1;
+ self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
+ self->zst.avail_out = length;
+ length = length << 1;
- Py_BEGIN_ALLOW_THREADS
- err = inflate(&(self->zst), Z_FINISH);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ err = inflate(&(self->zst), Z_FINISH);
+ Py_END_ALLOW_THREADS
}
/* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
various data structures. Note we should only get Z_STREAM_END when
flushmode is Z_FINISH */
if (err == Z_STREAM_END) {
- err = inflateEnd(&(self->zst));
+ err = inflateEnd(&(self->zst));
self->is_initialised = 0;
- if (err != Z_OK) {
- zlib_error(self->zst, err, "from inflateEnd()");
- Py_DECREF(retval);
- retval = NULL;
- goto error;
- }
+ if (err != Z_OK) {
+ zlib_error(self->zst, err, "from inflateEnd()");
+ Py_DECREF(retval);
+ retval = NULL;
+ goto error;
+ }
}
if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Py_DECREF(retval);
@@ -942,7 +942,7 @@ PyZlib_crc32(PyObject *self, PyObject *args)
int signed_val;
if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
- return NULL;
+ return NULL;
/* Releasing the GIL for very small buffers is inefficient
and may lower performance */
if (pbuf.len > 1024*5) {
@@ -950,7 +950,7 @@ PyZlib_crc32(PyObject *self, PyObject *args)
signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Py_END_ALLOW_THREADS
} else {
- signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
+ signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
}
PyBuffer_Release(&pbuf);
return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
@@ -1053,15 +1053,15 @@ PyDoc_STRVAR(zlib_module_documentation,
"objects support decompress() and flush().");
static struct PyModuleDef zlibmodule = {
- PyModuleDef_HEAD_INIT,
- "zlib",
- zlib_module_documentation,
- -1,
- zlib_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "zlib",
+ zlib_module_documentation,
+ -1,
+ zlib_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -1069,17 +1069,17 @@ PyInit_zlib(void)
{
PyObject *m, *ver;
if (PyType_Ready(&Comptype) < 0)
- return NULL;
+ return NULL;
if (PyType_Ready(&Decomptype) < 0)
- return NULL;
+ return NULL;
m = PyModule_Create(&zlibmodule);
if (m == NULL)
- return NULL;
+ return NULL;
ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL) {
Py_INCREF(ZlibError);
- PyModule_AddObject(m, "error", ZlibError);
+ PyModule_AddObject(m, "error", ZlibError);
}
PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
@@ -1098,7 +1098,7 @@ PyInit_zlib(void)
ver = PyUnicode_FromString(ZLIB_VERSION);
if (ver != NULL)
- PyModule_AddObject(m, "ZLIB_VERSION", ver);
+ PyModule_AddObject(m, "ZLIB_VERSION", ver);
PyModule_AddStringConstant(m, "__version__", "1.0");
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 5247824..2d85104 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -12,17 +12,17 @@
static PyObject *
type_error(const char *msg, PyObject *obj)
{
- PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
- return NULL;
+ PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name);
+ return NULL;
}
static PyObject *
null_error(void)
{
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_SystemError,
- "null argument to internal routine");
- return NULL;
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError,
+ "null argument to internal routine");
+ return NULL;
}
/* Operations on any object */
@@ -30,37 +30,37 @@ null_error(void)
PyObject *
PyObject_Type(PyObject *o)
{
- PyObject *v;
+ PyObject *v;
- if (o == NULL)
- return null_error();
- v = (PyObject *)o->ob_type;
- Py_INCREF(v);
- return v;
+ if (o == NULL)
+ return null_error();
+ v = (PyObject *)o->ob_type;
+ Py_INCREF(v);
+ return v;
}
Py_ssize_t
PyObject_Size(PyObject *o)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (o == NULL) {
- null_error();
- return -1;
- }
+ if (o == NULL) {
+ null_error();
+ return -1;
+ }
- m = o->ob_type->tp_as_sequence;
- if (m && m->sq_length)
- return m->sq_length(o);
+ m = o->ob_type->tp_as_sequence;
+ if (m && m->sq_length)
+ return m->sq_length(o);
- return PyMapping_Size(o);
+ return PyMapping_Size(o);
}
#undef PyObject_Length
Py_ssize_t
PyObject_Length(PyObject *o)
{
- return PyObject_Size(o);
+ return PyObject_Size(o);
}
#define PyObject_Length PyObject_Size
@@ -74,149 +74,149 @@ PyObject_Length(PyObject *o)
Py_ssize_t
_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
{
- static PyObject *hintstrobj = NULL;
- PyObject *ro, *hintmeth;
- Py_ssize_t rv;
-
- /* try o.__len__() */
- rv = PyObject_Size(o);
- if (rv >= 0)
- return rv;
- if (PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(PyExc_TypeError))
- return -1;
- PyErr_Clear();
- }
-
- /* try o.__length_hint__() */
- hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
- if (hintmeth == NULL) {
- if (PyErr_Occurred())
- return -1;
- else
- return defaultvalue;
- }
- ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
- Py_DECREF(hintmeth);
- if (ro == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_TypeError))
- return -1;
- PyErr_Clear();
- return defaultvalue;
- }
- rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
- Py_DECREF(ro);
- return rv;
+ static PyObject *hintstrobj = NULL;
+ PyObject *ro, *hintmeth;
+ Py_ssize_t rv;
+
+ /* try o.__len__() */
+ rv = PyObject_Size(o);
+ if (rv >= 0)
+ return rv;
+ if (PyErr_Occurred()) {
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return -1;
+ PyErr_Clear();
+ }
+
+ /* try o.__length_hint__() */
+ hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj);
+ if (hintmeth == NULL) {
+ if (PyErr_Occurred())
+ return -1;
+ else
+ return defaultvalue;
+ }
+ ro = PyObject_CallFunctionObjArgs(hintmeth, NULL);
+ Py_DECREF(hintmeth);
+ if (ro == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
+ return -1;
+ PyErr_Clear();
+ return defaultvalue;
+ }
+ rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
+ Py_DECREF(ro);
+ return rv;
}
PyObject *
PyObject_GetItem(PyObject *o, PyObject *key)
{
- PyMappingMethods *m;
-
- if (o == NULL || key == NULL)
- return null_error();
-
- m = o->ob_type->tp_as_mapping;
- if (m && m->mp_subscript)
- return m->mp_subscript(o, key);
-
- if (o->ob_type->tp_as_sequence) {
- if (PyIndex_Check(key)) {
- Py_ssize_t key_value;
- key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
- if (key_value == -1 && PyErr_Occurred())
- return NULL;
- return PySequence_GetItem(o, key_value);
- }
- else if (o->ob_type->tp_as_sequence->sq_item)
- return type_error("sequence index must "
- "be integer, not '%.200s'", key);
- }
+ PyMappingMethods *m;
+
+ if (o == NULL || key == NULL)
+ return null_error();
+
+ m = o->ob_type->tp_as_mapping;
+ if (m && m->mp_subscript)
+ return m->mp_subscript(o, key);
+
+ if (o->ob_type->tp_as_sequence) {
+ if (PyIndex_Check(key)) {
+ Py_ssize_t key_value;
+ key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
+ if (key_value == -1 && PyErr_Occurred())
+ return NULL;
+ return PySequence_GetItem(o, key_value);
+ }
+ else if (o->ob_type->tp_as_sequence->sq_item)
+ return type_error("sequence index must "
+ "be integer, not '%.200s'", key);
+ }
- return type_error("'%.200s' object is not subscriptable", o);
+ return type_error("'%.200s' object is not subscriptable", o);
}
int
PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
{
- PyMappingMethods *m;
-
- if (o == NULL || key == NULL || value == NULL) {
- null_error();
- return -1;
- }
- m = o->ob_type->tp_as_mapping;
- if (m && m->mp_ass_subscript)
- return m->mp_ass_subscript(o, key, value);
-
- if (o->ob_type->tp_as_sequence) {
- if (PyIndex_Check(key)) {
- Py_ssize_t key_value;
- key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
- if (key_value == -1 && PyErr_Occurred())
- return -1;
- return PySequence_SetItem(o, key_value, value);
- }
- else if (o->ob_type->tp_as_sequence->sq_ass_item) {
- type_error("sequence index must be "
- "integer, not '%.200s'", key);
- return -1;
- }
- }
-
- type_error("'%.200s' object does not support item assignment", o);
- return -1;
+ PyMappingMethods *m;
+
+ if (o == NULL || key == NULL || value == NULL) {
+ null_error();
+ return -1;
+ }
+ m = o->ob_type->tp_as_mapping;
+ if (m && m->mp_ass_subscript)
+ return m->mp_ass_subscript(o, key, value);
+
+ if (o->ob_type->tp_as_sequence) {
+ if (PyIndex_Check(key)) {
+ Py_ssize_t key_value;
+ key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
+ if (key_value == -1 && PyErr_Occurred())
+ return -1;
+ return PySequence_SetItem(o, key_value, value);
+ }
+ else if (o->ob_type->tp_as_sequence->sq_ass_item) {
+ type_error("sequence index must be "
+ "integer, not '%.200s'", key);
+ return -1;
+ }
+ }
+
+ type_error("'%.200s' object does not support item assignment", o);
+ return -1;
}
int
PyObject_DelItem(PyObject *o, PyObject *key)
{
- PyMappingMethods *m;
-
- if (o == NULL || key == NULL) {
- null_error();
- return -1;
- }
- m = o->ob_type->tp_as_mapping;
- if (m && m->mp_ass_subscript)
- return m->mp_ass_subscript(o, key, (PyObject*)NULL);
-
- if (o->ob_type->tp_as_sequence) {
- if (PyIndex_Check(key)) {
- Py_ssize_t key_value;
- key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
- if (key_value == -1 && PyErr_Occurred())
- return -1;
- return PySequence_DelItem(o, key_value);
- }
- else if (o->ob_type->tp_as_sequence->sq_ass_item) {
- type_error("sequence index must be "
- "integer, not '%.200s'", key);
- return -1;
- }
- }
-
- type_error("'%.200s' object does not support item deletion", o);
- return -1;
+ PyMappingMethods *m;
+
+ if (o == NULL || key == NULL) {
+ null_error();
+ return -1;
+ }
+ m = o->ob_type->tp_as_mapping;
+ if (m && m->mp_ass_subscript)
+ return m->mp_ass_subscript(o, key, (PyObject*)NULL);
+
+ if (o->ob_type->tp_as_sequence) {
+ if (PyIndex_Check(key)) {
+ Py_ssize_t key_value;
+ key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
+ if (key_value == -1 && PyErr_Occurred())
+ return -1;
+ return PySequence_DelItem(o, key_value);
+ }
+ else if (o->ob_type->tp_as_sequence->sq_ass_item) {
+ type_error("sequence index must be "
+ "integer, not '%.200s'", key);
+ return -1;
+ }
+ }
+
+ type_error("'%.200s' object does not support item deletion", o);
+ return -1;
}
int
PyObject_DelItemString(PyObject *o, char *key)
{
- PyObject *okey;
- int ret;
+ PyObject *okey;
+ int ret;
- if (o == NULL || key == NULL) {
- null_error();
- return -1;
- }
- okey = PyUnicode_FromString(key);
- if (okey == NULL)
- return -1;
- ret = PyObject_DelItem(o, okey);
- Py_DECREF(okey);
- return ret;
+ if (o == NULL || key == NULL) {
+ null_error();
+ return -1;
+ }
+ okey = PyUnicode_FromString(key);
+ if (okey == NULL)
+ return -1;
+ ret = PyObject_DelItem(o, okey);
+ Py_DECREF(okey);
+ return ret;
}
/* We release the buffer right after use of this function which could
@@ -224,104 +224,104 @@ PyObject_DelItemString(PyObject *o, char *key)
*/
int
PyObject_AsCharBuffer(PyObject *obj,
- const char **buffer,
- Py_ssize_t *buffer_len)
-{
- PyBufferProcs *pb;
- Py_buffer view;
-
- if (obj == NULL || buffer == NULL || buffer_len == NULL) {
- null_error();
- return -1;
- }
- pb = obj->ob_type->tp_as_buffer;
- if (pb == NULL || pb->bf_getbuffer == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "expected an object with the buffer interface");
- return -1;
- }
- if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
-
- *buffer = view.buf;
- *buffer_len = view.len;
- if (pb->bf_releasebuffer != NULL)
- (*pb->bf_releasebuffer)(obj, &view);
- Py_XDECREF(view.obj);
- return 0;
+ const char **buffer,
+ Py_ssize_t *buffer_len)
+{
+ PyBufferProcs *pb;
+ Py_buffer view;
+
+ if (obj == NULL || buffer == NULL || buffer_len == NULL) {
+ null_error();
+ return -1;
+ }
+ pb = obj->ob_type->tp_as_buffer;
+ if (pb == NULL || pb->bf_getbuffer == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected an object with the buffer interface");
+ return -1;
+ }
+ if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
+
+ *buffer = view.buf;
+ *buffer_len = view.len;
+ if (pb->bf_releasebuffer != NULL)
+ (*pb->bf_releasebuffer)(obj, &view);
+ Py_XDECREF(view.obj);
+ return 0;
}
int
PyObject_CheckReadBuffer(PyObject *obj)
{
- PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
- Py_buffer view;
+ PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
+ Py_buffer view;
- if (pb == NULL ||
- pb->bf_getbuffer == NULL)
- return 0;
- if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
- PyErr_Clear();
- return 0;
- }
- PyBuffer_Release(&view);
- return 1;
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL)
+ return 0;
+ if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE) == -1) {
+ PyErr_Clear();
+ return 0;
+ }
+ PyBuffer_Release(&view);
+ return 1;
}
int PyObject_AsReadBuffer(PyObject *obj,
- const void **buffer,
- Py_ssize_t *buffer_len)
-{
- PyBufferProcs *pb;
- Py_buffer view;
-
- if (obj == NULL || buffer == NULL || buffer_len == NULL) {
- null_error();
- return -1;
- }
- pb = obj->ob_type->tp_as_buffer;
- if (pb == NULL ||
- pb->bf_getbuffer == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "expected an object with a buffer interface");
- return -1;
- }
-
- if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
-
- *buffer = view.buf;
- *buffer_len = view.len;
- if (pb->bf_releasebuffer != NULL)
- (*pb->bf_releasebuffer)(obj, &view);
- Py_XDECREF(view.obj);
- return 0;
+ const void **buffer,
+ Py_ssize_t *buffer_len)
+{
+ PyBufferProcs *pb;
+ Py_buffer view;
+
+ if (obj == NULL || buffer == NULL || buffer_len == NULL) {
+ null_error();
+ return -1;
+ }
+ pb = obj->ob_type->tp_as_buffer;
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected an object with a buffer interface");
+ return -1;
+ }
+
+ if ((*pb->bf_getbuffer)(obj, &view, PyBUF_SIMPLE)) return -1;
+
+ *buffer = view.buf;
+ *buffer_len = view.len;
+ if (pb->bf_releasebuffer != NULL)
+ (*pb->bf_releasebuffer)(obj, &view);
+ Py_XDECREF(view.obj);
+ return 0;
}
int PyObject_AsWriteBuffer(PyObject *obj,
- void **buffer,
- Py_ssize_t *buffer_len)
-{
- PyBufferProcs *pb;
- Py_buffer view;
-
- if (obj == NULL || buffer == NULL || buffer_len == NULL) {
- null_error();
- return -1;
- }
- pb = obj->ob_type->tp_as_buffer;
- if (pb == NULL ||
- pb->bf_getbuffer == NULL ||
- ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
- PyErr_SetString(PyExc_TypeError,
- "expected an object with a writable buffer interface");
- return -1;
- }
-
- *buffer = view.buf;
- *buffer_len = view.len;
- if (pb->bf_releasebuffer != NULL)
- (*pb->bf_releasebuffer)(obj, &view);
- Py_XDECREF(view.obj);
- return 0;
+ void **buffer,
+ Py_ssize_t *buffer_len)
+{
+ PyBufferProcs *pb;
+ Py_buffer view;
+
+ if (obj == NULL || buffer == NULL || buffer_len == NULL) {
+ null_error();
+ return -1;
+ }
+ pb = obj->ob_type->tp_as_buffer;
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL ||
+ ((*pb->bf_getbuffer)(obj, &view, PyBUF_WRITABLE) != 0)) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected an object with a writable buffer interface");
+ return -1;
+ }
+
+ *buffer = view.buf;
+ *buffer_len = view.len;
+ if (pb->bf_releasebuffer != NULL)
+ (*pb->bf_releasebuffer)(obj, &view);
+ Py_XDECREF(view.obj);
+ return 0;
}
/* Buffer C-API for Python 3.0 */
@@ -329,119 +329,119 @@ int PyObject_AsWriteBuffer(PyObject *obj,
int
PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
{
- if (!PyObject_CheckBuffer(obj)) {
- PyErr_Format(PyExc_TypeError,
- "'%100s' does not support the buffer interface",
- Py_TYPE(obj)->tp_name);
- return -1;
- }
- return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
+ if (!PyObject_CheckBuffer(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "'%100s' does not support the buffer interface",
+ Py_TYPE(obj)->tp_name);
+ return -1;
+ }
+ return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags);
}
static int
_IsFortranContiguous(Py_buffer *view)
{
- Py_ssize_t sd, dim;
- int i;
+ Py_ssize_t sd, dim;
+ int i;
- if (view->ndim == 0) return 1;
- if (view->strides == NULL) return (view->ndim == 1);
+ if (view->ndim == 0) return 1;
+ if (view->strides == NULL) return (view->ndim == 1);
- sd = view->itemsize;
- if (view->ndim == 1) return (view->shape[0] == 1 ||
- sd == view->strides[0]);
- for (i=0; i<view->ndim; i++) {
- dim = view->shape[i];
- if (dim == 0) return 1;
- if (view->strides[i] != sd) return 0;
- sd *= dim;
- }
- return 1;
+ sd = view->itemsize;
+ if (view->ndim == 1) return (view->shape[0] == 1 ||
+ sd == view->strides[0]);
+ for (i=0; i<view->ndim; i++) {
+ dim = view->shape[i];
+ if (dim == 0) return 1;
+ if (view->strides[i] != sd) return 0;
+ sd *= dim;
+ }
+ return 1;
}
static int
_IsCContiguous(Py_buffer *view)
{
- Py_ssize_t sd, dim;
- int i;
+ Py_ssize_t sd, dim;
+ int i;
- if (view->ndim == 0) return 1;
- if (view->strides == NULL) return 1;
+ if (view->ndim == 0) return 1;
+ if (view->strides == NULL) return 1;
- sd = view->itemsize;
- if (view->ndim == 1) return (view->shape[0] == 1 ||
- sd == view->strides[0]);
- for (i=view->ndim-1; i>=0; i--) {
- dim = view->shape[i];
- if (dim == 0) return 1;
- if (view->strides[i] != sd) return 0;
- sd *= dim;
- }
- return 1;
+ sd = view->itemsize;
+ if (view->ndim == 1) return (view->shape[0] == 1 ||
+ sd == view->strides[0]);
+ for (i=view->ndim-1; i>=0; i--) {
+ dim = view->shape[i];
+ if (dim == 0) return 1;
+ if (view->strides[i] != sd) return 0;
+ sd *= dim;
+ }
+ return 1;
}
int
PyBuffer_IsContiguous(Py_buffer *view, char fort)
{
- if (view->suboffsets != NULL) return 0;
+ if (view->suboffsets != NULL) return 0;
- if (fort == 'C')
- return _IsCContiguous(view);
- else if (fort == 'F')
- return _IsFortranContiguous(view);
- else if (fort == 'A')
- return (_IsCContiguous(view) || _IsFortranContiguous(view));
- return 0;
+ if (fort == 'C')
+ return _IsCContiguous(view);
+ else if (fort == 'F')
+ return _IsFortranContiguous(view);
+ else if (fort == 'A')
+ return (_IsCContiguous(view) || _IsFortranContiguous(view));
+ return 0;
}
void*
PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
{
- char* pointer;
- int i;
- pointer = (char *)view->buf;
- for (i = 0; i < view->ndim; i++) {
- pointer += view->strides[i]*indices[i];
- if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
- pointer = *((char**)pointer) + view->suboffsets[i];
- }
- }
- return (void*)pointer;
+ char* pointer;
+ int i;
+ pointer = (char *)view->buf;
+ for (i = 0; i < view->ndim; i++) {
+ pointer += view->strides[i]*indices[i];
+ if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) {
+ pointer = *((char**)pointer) + view->suboffsets[i];
+ }
+ }
+ return (void*)pointer;
}
void
_add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape)
{
- int k;
+ int k;
- for (k=0; k<nd; k++) {
- if (index[k] < shape[k]-1) {
- index[k]++;
- break;
- }
- else {
- index[k] = 0;
- }
- }
+ for (k=0; k<nd; k++) {
+ if (index[k] < shape[k]-1) {
+ index[k]++;
+ break;
+ }
+ else {
+ index[k] = 0;
+ }
+ }
}
void
_add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
{
- int k;
+ int k;
- for (k=nd-1; k>=0; k--) {
- if (index[k] < shape[k]-1) {
- index[k]++;
- break;
- }
- else {
- index[k] = 0;
- }
- }
+ for (k=nd-1; k>=0; k--) {
+ if (index[k] < shape[k]-1) {
+ index[k]++;
+ break;
+ }
+ else {
+ index[k] = 0;
+ }
+ }
}
/* view is not checked for consistency in either of these. It is
@@ -452,242 +452,242 @@ _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape)
int
PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
{
- int k;
- void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
- Py_ssize_t *indices, elements;
- char *dest, *ptr;
-
- if (len > view->len) {
- len = view->len;
- }
-
- if (PyBuffer_IsContiguous(view, fort)) {
- /* simplest copy is all that is needed */
- memcpy(buf, view->buf, len);
- return 0;
- }
-
- /* Otherwise a more elaborate scheme is needed */
-
- /* XXX(nnorwitz): need to check for overflow! */
- indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
- if (indices == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- for (k=0; k<view->ndim;k++) {
- indices[k] = 0;
- }
-
- if (fort == 'F') {
- addone = _add_one_to_index_F;
- }
- else {
- addone = _add_one_to_index_C;
- }
- dest = buf;
- /* XXX : This is not going to be the fastest code in the world
- several optimizations are possible.
- */
- elements = len / view->itemsize;
- while (elements--) {
- addone(view->ndim, indices, view->shape);
- ptr = PyBuffer_GetPointer(view, indices);
- memcpy(dest, ptr, view->itemsize);
- dest += view->itemsize;
- }
- PyMem_Free(indices);
- return 0;
+ int k;
+ void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
+ Py_ssize_t *indices, elements;
+ char *dest, *ptr;
+
+ if (len > view->len) {
+ len = view->len;
+ }
+
+ if (PyBuffer_IsContiguous(view, fort)) {
+ /* simplest copy is all that is needed */
+ memcpy(buf, view->buf, len);
+ return 0;
+ }
+
+ /* Otherwise a more elaborate scheme is needed */
+
+ /* XXX(nnorwitz): need to check for overflow! */
+ indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ for (k=0; k<view->ndim;k++) {
+ indices[k] = 0;
+ }
+
+ if (fort == 'F') {
+ addone = _add_one_to_index_F;
+ }
+ else {
+ addone = _add_one_to_index_C;
+ }
+ dest = buf;
+ /* XXX : This is not going to be the fastest code in the world
+ several optimizations are possible.
+ */
+ elements = len / view->itemsize;
+ while (elements--) {
+ addone(view->ndim, indices, view->shape);
+ ptr = PyBuffer_GetPointer(view, indices);
+ memcpy(dest, ptr, view->itemsize);
+ dest += view->itemsize;
+ }
+ PyMem_Free(indices);
+ return 0;
}
int
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
{
- int k;
- void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
- Py_ssize_t *indices, elements;
- char *src, *ptr;
-
- if (len > view->len) {
- len = view->len;
- }
-
- if (PyBuffer_IsContiguous(view, fort)) {
- /* simplest copy is all that is needed */
- memcpy(view->buf, buf, len);
- return 0;
- }
-
- /* Otherwise a more elaborate scheme is needed */
-
- /* XXX(nnorwitz): need to check for overflow! */
- indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
- if (indices == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- for (k=0; k<view->ndim;k++) {
- indices[k] = 0;
- }
-
- if (fort == 'F') {
- addone = _add_one_to_index_F;
- }
- else {
- addone = _add_one_to_index_C;
- }
- src = buf;
- /* XXX : This is not going to be the fastest code in the world
- several optimizations are possible.
- */
- elements = len / view->itemsize;
- while (elements--) {
- addone(view->ndim, indices, view->shape);
- ptr = PyBuffer_GetPointer(view, indices);
- memcpy(ptr, src, view->itemsize);
- src += view->itemsize;
- }
-
- PyMem_Free(indices);
- return 0;
+ int k;
+ void (*addone)(int, Py_ssize_t *, Py_ssize_t *);
+ Py_ssize_t *indices, elements;
+ char *src, *ptr;
+
+ if (len > view->len) {
+ len = view->len;
+ }
+
+ if (PyBuffer_IsContiguous(view, fort)) {
+ /* simplest copy is all that is needed */
+ memcpy(view->buf, buf, len);
+ return 0;
+ }
+
+ /* Otherwise a more elaborate scheme is needed */
+
+ /* XXX(nnorwitz): need to check for overflow! */
+ indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim));
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ for (k=0; k<view->ndim;k++) {
+ indices[k] = 0;
+ }
+
+ if (fort == 'F') {
+ addone = _add_one_to_index_F;
+ }
+ else {
+ addone = _add_one_to_index_C;
+ }
+ src = buf;
+ /* XXX : This is not going to be the fastest code in the world
+ several optimizations are possible.
+ */
+ elements = len / view->itemsize;
+ while (elements--) {
+ addone(view->ndim, indices, view->shape);
+ ptr = PyBuffer_GetPointer(view, indices);
+ memcpy(ptr, src, view->itemsize);
+ src += view->itemsize;
+ }
+
+ PyMem_Free(indices);
+ return 0;
}
int PyObject_CopyData(PyObject *dest, PyObject *src)
{
- Py_buffer view_dest, view_src;
- int k;
- Py_ssize_t *indices, elements;
- char *dptr, *sptr;
-
- if (!PyObject_CheckBuffer(dest) ||
- !PyObject_CheckBuffer(src)) {
- PyErr_SetString(PyExc_TypeError,
- "both destination and source must have the "\
- "buffer interface");
- return -1;
- }
-
- if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
- if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
- PyBuffer_Release(&view_dest);
- return -1;
- }
-
- if (view_dest.len < view_src.len) {
- PyErr_SetString(PyExc_BufferError,
- "destination is too small to receive data from source");
- PyBuffer_Release(&view_dest);
- PyBuffer_Release(&view_src);
- return -1;
- }
-
- if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
- PyBuffer_IsContiguous(&view_src, 'C')) ||
- (PyBuffer_IsContiguous(&view_dest, 'F') &&
- PyBuffer_IsContiguous(&view_src, 'F'))) {
- /* simplest copy is all that is needed */
- memcpy(view_dest.buf, view_src.buf, view_src.len);
- PyBuffer_Release(&view_dest);
- PyBuffer_Release(&view_src);
- return 0;
- }
-
- /* Otherwise a more elaborate copy scheme is needed */
-
- /* XXX(nnorwitz): need to check for overflow! */
- indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
- if (indices == NULL) {
- PyErr_NoMemory();
- PyBuffer_Release(&view_dest);
- PyBuffer_Release(&view_src);
- return -1;
- }
- for (k=0; k<view_src.ndim;k++) {
- indices[k] = 0;
- }
- elements = 1;
- for (k=0; k<view_src.ndim; k++) {
- /* XXX(nnorwitz): can this overflow? */
- elements *= view_src.shape[k];
- }
- while (elements--) {
- _add_one_to_index_C(view_src.ndim, indices, view_src.shape);
- dptr = PyBuffer_GetPointer(&view_dest, indices);
- sptr = PyBuffer_GetPointer(&view_src, indices);
- memcpy(dptr, sptr, view_src.itemsize);
- }
- PyMem_Free(indices);
- PyBuffer_Release(&view_dest);
- PyBuffer_Release(&view_src);
- return 0;
+ Py_buffer view_dest, view_src;
+ int k;
+ Py_ssize_t *indices, elements;
+ char *dptr, *sptr;
+
+ if (!PyObject_CheckBuffer(dest) ||
+ !PyObject_CheckBuffer(src)) {
+ PyErr_SetString(PyExc_TypeError,
+ "both destination and source must have the "\
+ "buffer interface");
+ return -1;
+ }
+
+ if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1;
+ if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) {
+ PyBuffer_Release(&view_dest);
+ return -1;
+ }
+
+ if (view_dest.len < view_src.len) {
+ PyErr_SetString(PyExc_BufferError,
+ "destination is too small to receive data from source");
+ PyBuffer_Release(&view_dest);
+ PyBuffer_Release(&view_src);
+ return -1;
+ }
+
+ if ((PyBuffer_IsContiguous(&view_dest, 'C') &&
+ PyBuffer_IsContiguous(&view_src, 'C')) ||
+ (PyBuffer_IsContiguous(&view_dest, 'F') &&
+ PyBuffer_IsContiguous(&view_src, 'F'))) {
+ /* simplest copy is all that is needed */
+ memcpy(view_dest.buf, view_src.buf, view_src.len);
+ PyBuffer_Release(&view_dest);
+ PyBuffer_Release(&view_src);
+ return 0;
+ }
+
+ /* Otherwise a more elaborate copy scheme is needed */
+
+ /* XXX(nnorwitz): need to check for overflow! */
+ indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim);
+ if (indices == NULL) {
+ PyErr_NoMemory();
+ PyBuffer_Release(&view_dest);
+ PyBuffer_Release(&view_src);
+ return -1;
+ }
+ for (k=0; k<view_src.ndim;k++) {
+ indices[k] = 0;
+ }
+ elements = 1;
+ for (k=0; k<view_src.ndim; k++) {
+ /* XXX(nnorwitz): can this overflow? */
+ elements *= view_src.shape[k];
+ }
+ while (elements--) {
+ _add_one_to_index_C(view_src.ndim, indices, view_src.shape);
+ dptr = PyBuffer_GetPointer(&view_dest, indices);
+ sptr = PyBuffer_GetPointer(&view_src, indices);
+ memcpy(dptr, sptr, view_src.itemsize);
+ }
+ PyMem_Free(indices);
+ PyBuffer_Release(&view_dest);
+ PyBuffer_Release(&view_src);
+ return 0;
}
void
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape,
- Py_ssize_t *strides, int itemsize,
- char fort)
-{
- int k;
- Py_ssize_t sd;
-
- sd = itemsize;
- if (fort == 'F') {
- for (k=0; k<nd; k++) {
- strides[k] = sd;
- sd *= shape[k];
- }
- }
- else {
- for (k=nd-1; k>=0; k--) {
- strides[k] = sd;
- sd *= shape[k];
- }
- }
- return;
+ Py_ssize_t *strides, int itemsize,
+ char fort)
+{
+ int k;
+ Py_ssize_t sd;
+
+ sd = itemsize;
+ if (fort == 'F') {
+ for (k=0; k<nd; k++) {
+ strides[k] = sd;
+ sd *= shape[k];
+ }
+ }
+ else {
+ for (k=nd-1; k>=0; k--) {
+ strides[k] = sd;
+ sd *= shape[k];
+ }
+ }
+ return;
}
int
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
- int readonly, int flags)
-{
- if (view == NULL) return 0;
- if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
- (readonly == 1)) {
- PyErr_SetString(PyExc_BufferError,
- "Object is not writable.");
- return -1;
- }
-
- view->obj = obj;
- if (obj)
- Py_INCREF(obj);
- view->buf = buf;
- view->len = len;
- view->readonly = readonly;
- view->itemsize = 1;
- view->format = NULL;
- if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
- view->format = "B";
- view->ndim = 1;
- view->shape = NULL;
- if ((flags & PyBUF_ND) == PyBUF_ND)
- view->shape = &(view->len);
- view->strides = NULL;
- if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
- view->strides = &(view->itemsize);
- view->suboffsets = NULL;
- view->internal = NULL;
- return 0;
+ int readonly, int flags)
+{
+ if (view == NULL) return 0;
+ if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) &&
+ (readonly == 1)) {
+ PyErr_SetString(PyExc_BufferError,
+ "Object is not writable.");
+ return -1;
+ }
+
+ view->obj = obj;
+ if (obj)
+ Py_INCREF(obj);
+ view->buf = buf;
+ view->len = len;
+ view->readonly = readonly;
+ view->itemsize = 1;
+ view->format = NULL;
+ if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
+ view->format = "B";
+ view->ndim = 1;
+ view->shape = NULL;
+ if ((flags & PyBUF_ND) == PyBUF_ND)
+ view->shape = &(view->len);
+ view->strides = NULL;
+ if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
+ view->strides = &(view->itemsize);
+ view->suboffsets = NULL;
+ view->internal = NULL;
+ return 0;
}
void
PyBuffer_Release(Py_buffer *view)
{
- PyObject *obj = view->obj;
- if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
- Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
- Py_XDECREF(obj);
- view->obj = NULL;
+ PyObject *obj = view->obj;
+ if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer)
+ Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view);
+ Py_XDECREF(obj);
+ view->obj = NULL;
}
PyObject *
@@ -700,41 +700,41 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
/* Initialize cached value */
if (str__format__ == NULL) {
- /* Initialize static variable needed by _PyType_Lookup */
- str__format__ = PyUnicode_FromString("__format__");
- if (str__format__ == NULL)
- goto done;
+ /* Initialize static variable needed by _PyType_Lookup */
+ str__format__ = PyUnicode_FromString("__format__");
+ if (str__format__ == NULL)
+ goto done;
}
/* If no format_spec is provided, use an empty string */
if (format_spec == NULL) {
- empty = PyUnicode_FromUnicode(NULL, 0);
- format_spec = empty;
+ empty = PyUnicode_FromUnicode(NULL, 0);
+ format_spec = empty;
}
/* Make sure the type is initialized. float gets initialized late */
if (Py_TYPE(obj)->tp_dict == NULL)
- if (PyType_Ready(Py_TYPE(obj)) < 0)
- goto done;
+ if (PyType_Ready(Py_TYPE(obj)) < 0)
+ goto done;
/* Find the (unbound!) __format__ method (a borrowed reference) */
meth = _PyType_Lookup(Py_TYPE(obj), str__format__);
if (meth == NULL) {
- PyErr_Format(PyExc_TypeError,
- "Type %.100s doesn't define __format__",
- Py_TYPE(obj)->tp_name);
- goto done;
+ PyErr_Format(PyExc_TypeError,
+ "Type %.100s doesn't define __format__",
+ Py_TYPE(obj)->tp_name);
+ goto done;
}
/* And call it, binding it to the value */
result = PyObject_CallFunctionObjArgs(meth, obj, format_spec, NULL);
if (result && !PyUnicode_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "__format__ method did not return string");
- Py_DECREF(result);
- result = NULL;
- goto done;
+ PyErr_SetString(PyExc_TypeError,
+ "__format__ method did not return string");
+ Py_DECREF(result);
+ result = NULL;
+ goto done;
}
done:
@@ -746,24 +746,24 @@ done:
int
PyNumber_Check(PyObject *o)
{
- return o && o->ob_type->tp_as_number &&
- (o->ob_type->tp_as_number->nb_int ||
- o->ob_type->tp_as_number->nb_float);
+ return o && o->ob_type->tp_as_number &&
+ (o->ob_type->tp_as_number->nb_int ||
+ o->ob_type->tp_as_number->nb_float);
}
/* Binary operators */
#define NB_SLOT(x) offsetof(PyNumberMethods, x)
#define NB_BINOP(nb_methods, slot) \
- (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
+ (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
#define NB_TERNOP(nb_methods, slot) \
- (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
+ (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
/*
Calling scheme used for binary operations:
Order operations are tried until either a valid result or error:
- w.op(v,w)[*], v.op(v,w), w.op(v,w)
+ w.op(v,w)[*], v.op(v,w), w.op(v,w)
[*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
v->ob_type
@@ -772,62 +772,62 @@ PyNumber_Check(PyObject *o)
static PyObject *
binary_op1(PyObject *v, PyObject *w, const int op_slot)
{
- PyObject *x;
- binaryfunc slotv = NULL;
- binaryfunc slotw = NULL;
-
- if (v->ob_type->tp_as_number != NULL)
- slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
- if (w->ob_type != v->ob_type &&
- w->ob_type->tp_as_number != NULL) {
- slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
- if (slotw == slotv)
- slotw = NULL;
- }
- if (slotv) {
- if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
- x = slotw(v, w);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- slotw = NULL;
- }
- x = slotv(v, w);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- }
- if (slotw) {
- x = slotw(v, w);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ PyObject *x;
+ binaryfunc slotv = NULL;
+ binaryfunc slotw = NULL;
+
+ if (v->ob_type->tp_as_number != NULL)
+ slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
+ if (w->ob_type != v->ob_type &&
+ w->ob_type->tp_as_number != NULL) {
+ slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
+ if (slotw == slotv)
+ slotw = NULL;
+ }
+ if (slotv) {
+ if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
+ x = slotw(v, w);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ slotw = NULL;
+ }
+ x = slotv(v, w);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ }
+ if (slotw) {
+ x = slotw(v, w);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyObject *
binop_type_error(PyObject *v, PyObject *w, const char *op_name)
{
- PyErr_Format(PyExc_TypeError,
- "unsupported operand type(s) for %.100s: "
- "'%.100s' and '%.100s'",
- op_name,
- v->ob_type->tp_name,
- w->ob_type->tp_name);
- return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "unsupported operand type(s) for %.100s: "
+ "'%.100s' and '%.100s'",
+ op_name,
+ v->ob_type->tp_name,
+ w->ob_type->tp_name);
+ return NULL;
}
static PyObject *
binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
{
- PyObject *result = binary_op1(v, w, op_slot);
- if (result == Py_NotImplemented) {
- Py_DECREF(result);
- return binop_type_error(v, w, op_name);
- }
- return result;
+ PyObject *result = binary_op1(v, w, op_slot);
+ if (result == Py_NotImplemented) {
+ Py_DECREF(result);
+ return binop_type_error(v, w, op_name);
+ }
+ return result;
}
@@ -835,86 +835,86 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
Calling scheme used for ternary operations:
Order operations are tried until either a valid result or error:
- v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
+ v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
*/
static PyObject *
ternary_op(PyObject *v,
- PyObject *w,
- PyObject *z,
- const int op_slot,
- const char *op_name)
-{
- PyNumberMethods *mv, *mw, *mz;
- PyObject *x = NULL;
- ternaryfunc slotv = NULL;
- ternaryfunc slotw = NULL;
- ternaryfunc slotz = NULL;
-
- mv = v->ob_type->tp_as_number;
- mw = w->ob_type->tp_as_number;
- if (mv != NULL)
- slotv = NB_TERNOP(mv, op_slot);
- if (w->ob_type != v->ob_type &&
- mw != NULL) {
- slotw = NB_TERNOP(mw, op_slot);
- if (slotw == slotv)
- slotw = NULL;
- }
- if (slotv) {
- if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
- x = slotw(v, w, z);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- slotw = NULL;
- }
- x = slotv(v, w, z);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- }
- if (slotw) {
- x = slotw(v, w, z);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- }
- mz = z->ob_type->tp_as_number;
- if (mz != NULL) {
- slotz = NB_TERNOP(mz, op_slot);
- if (slotz == slotv || slotz == slotw)
- slotz = NULL;
- if (slotz) {
- x = slotz(v, w, z);
- if (x != Py_NotImplemented)
- return x;
- Py_DECREF(x); /* can't do it */
- }
- }
-
- if (z == Py_None)
- PyErr_Format(
- PyExc_TypeError,
- "unsupported operand type(s) for ** or pow(): "
- "'%.100s' and '%.100s'",
- v->ob_type->tp_name,
- w->ob_type->tp_name);
- else
- PyErr_Format(
- PyExc_TypeError,
- "unsupported operand type(s) for pow(): "
- "'%.100s', '%.100s', '%.100s'",
- v->ob_type->tp_name,
- w->ob_type->tp_name,
- z->ob_type->tp_name);
- return NULL;
+ PyObject *w,
+ PyObject *z,
+ const int op_slot,
+ const char *op_name)
+{
+ PyNumberMethods *mv, *mw, *mz;
+ PyObject *x = NULL;
+ ternaryfunc slotv = NULL;
+ ternaryfunc slotw = NULL;
+ ternaryfunc slotz = NULL;
+
+ mv = v->ob_type->tp_as_number;
+ mw = w->ob_type->tp_as_number;
+ if (mv != NULL)
+ slotv = NB_TERNOP(mv, op_slot);
+ if (w->ob_type != v->ob_type &&
+ mw != NULL) {
+ slotw = NB_TERNOP(mw, op_slot);
+ if (slotw == slotv)
+ slotw = NULL;
+ }
+ if (slotv) {
+ if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
+ x = slotw(v, w, z);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ slotw = NULL;
+ }
+ x = slotv(v, w, z);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ }
+ if (slotw) {
+ x = slotw(v, w, z);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ }
+ mz = z->ob_type->tp_as_number;
+ if (mz != NULL) {
+ slotz = NB_TERNOP(mz, op_slot);
+ if (slotz == slotv || slotz == slotw)
+ slotz = NULL;
+ if (slotz) {
+ x = slotz(v, w, z);
+ if (x != Py_NotImplemented)
+ return x;
+ Py_DECREF(x); /* can't do it */
+ }
+ }
+
+ if (z == Py_None)
+ PyErr_Format(
+ PyExc_TypeError,
+ "unsupported operand type(s) for ** or pow(): "
+ "'%.100s' and '%.100s'",
+ v->ob_type->tp_name,
+ w->ob_type->tp_name);
+ else
+ PyErr_Format(
+ PyExc_TypeError,
+ "unsupported operand type(s) for pow(): "
+ "'%.100s', '%.100s', '%.100s'",
+ v->ob_type->tp_name,
+ w->ob_type->tp_name,
+ z->ob_type->tp_name);
+ return NULL;
}
#define BINARY_FUNC(func, op, op_name) \
PyObject * \
func(PyObject *v, PyObject *w) { \
- return binary_op(v, w, NB_SLOT(op), op_name); \
+ return binary_op(v, w, NB_SLOT(op), op_name); \
}
BINARY_FUNC(PyNumber_Or, nb_or, "|")
@@ -928,75 +928,75 @@ BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
PyObject *
PyNumber_Add(PyObject *v, PyObject *w)
{
- PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
- if (result == Py_NotImplemented) {
- PySequenceMethods *m = v->ob_type->tp_as_sequence;
- Py_DECREF(result);
- if (m && m->sq_concat) {
- return (*m->sq_concat)(v, w);
- }
- result = binop_type_error(v, w, "+");
- }
- return result;
+ PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
+ if (result == Py_NotImplemented) {
+ PySequenceMethods *m = v->ob_type->tp_as_sequence;
+ Py_DECREF(result);
+ if (m && m->sq_concat) {
+ return (*m->sq_concat)(v, w);
+ }
+ result = binop_type_error(v, w, "+");
+ }
+ return result;
}
static PyObject *
sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
{
- Py_ssize_t count;
- if (PyIndex_Check(n)) {
- count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
- if (count == -1 && PyErr_Occurred())
- return NULL;
- }
- else {
- return type_error("can't multiply sequence by "
- "non-int of type '%.200s'", n);
- }
- return (*repeatfunc)(seq, count);
+ Py_ssize_t count;
+ if (PyIndex_Check(n)) {
+ count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
+ if (count == -1 && PyErr_Occurred())
+ return NULL;
+ }
+ else {
+ return type_error("can't multiply sequence by "
+ "non-int of type '%.200s'", n);
+ }
+ return (*repeatfunc)(seq, count);
}
PyObject *
PyNumber_Multiply(PyObject *v, PyObject *w)
{
- PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
- if (result == Py_NotImplemented) {
- PySequenceMethods *mv = v->ob_type->tp_as_sequence;
- PySequenceMethods *mw = w->ob_type->tp_as_sequence;
- Py_DECREF(result);
- if (mv && mv->sq_repeat) {
- return sequence_repeat(mv->sq_repeat, v, w);
- }
- else if (mw && mw->sq_repeat) {
- return sequence_repeat(mw->sq_repeat, w, v);
- }
- result = binop_type_error(v, w, "*");
- }
- return result;
+ PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
+ if (result == Py_NotImplemented) {
+ PySequenceMethods *mv = v->ob_type->tp_as_sequence;
+ PySequenceMethods *mw = w->ob_type->tp_as_sequence;
+ Py_DECREF(result);
+ if (mv && mv->sq_repeat) {
+ return sequence_repeat(mv->sq_repeat, v, w);
+ }
+ else if (mw && mw->sq_repeat) {
+ return sequence_repeat(mw->sq_repeat, w, v);
+ }
+ result = binop_type_error(v, w, "*");
+ }
+ return result;
}
PyObject *
PyNumber_FloorDivide(PyObject *v, PyObject *w)
{
- return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
+ return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
}
PyObject *
PyNumber_TrueDivide(PyObject *v, PyObject *w)
{
- return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
+ return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
}
PyObject *
PyNumber_Remainder(PyObject *v, PyObject *w)
{
- return binary_op(v, w, NB_SLOT(nb_remainder), "%");
+ return binary_op(v, w, NB_SLOT(nb_remainder), "%");
}
PyObject *
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
{
- return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
+ return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
}
/* Binary in-place operators */
@@ -1018,37 +1018,37 @@ PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
static PyObject *
binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
{
- PyNumberMethods *mv = v->ob_type->tp_as_number;
- if (mv != NULL) {
- binaryfunc slot = NB_BINOP(mv, iop_slot);
- if (slot) {
- PyObject *x = (slot)(v, w);
- if (x != Py_NotImplemented) {
- return x;
- }
- Py_DECREF(x);
- }
- }
- return binary_op1(v, w, op_slot);
+ PyNumberMethods *mv = v->ob_type->tp_as_number;
+ if (mv != NULL) {
+ binaryfunc slot = NB_BINOP(mv, iop_slot);
+ if (slot) {
+ PyObject *x = (slot)(v, w);
+ if (x != Py_NotImplemented) {
+ return x;
+ }
+ Py_DECREF(x);
+ }
+ }
+ return binary_op1(v, w, op_slot);
}
static PyObject *
binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
- const char *op_name)
+ const char *op_name)
{
- PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
- if (result == Py_NotImplemented) {
- Py_DECREF(result);
- return binop_type_error(v, w, op_name);
- }
- return result;
+ PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
+ if (result == Py_NotImplemented) {
+ Py_DECREF(result);
+ return binop_type_error(v, w, op_name);
+ }
+ return result;
}
#define INPLACE_BINOP(func, iop, op, op_name) \
- PyObject * \
- func(PyObject *v, PyObject *w) { \
- return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
- }
+ PyObject * \
+ func(PyObject *v, PyObject *w) { \
+ return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
+ }
INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
@@ -1060,84 +1060,84 @@ INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
PyObject *
PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
{
- return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
- NB_SLOT(nb_floor_divide), "//=");
+ return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
+ NB_SLOT(nb_floor_divide), "//=");
}
PyObject *
PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
{
- return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
- NB_SLOT(nb_true_divide), "/=");
+ return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
+ NB_SLOT(nb_true_divide), "/=");
}
PyObject *
PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
{
- PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
- NB_SLOT(nb_add));
- if (result == Py_NotImplemented) {
- PySequenceMethods *m = v->ob_type->tp_as_sequence;
- Py_DECREF(result);
- if (m != NULL) {
- binaryfunc f = NULL;
- f = m->sq_inplace_concat;
- if (f == NULL)
- f = m->sq_concat;
- if (f != NULL)
- return (*f)(v, w);
- }
- result = binop_type_error(v, w, "+=");
- }
- return result;
+ PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
+ NB_SLOT(nb_add));
+ if (result == Py_NotImplemented) {
+ PySequenceMethods *m = v->ob_type->tp_as_sequence;
+ Py_DECREF(result);
+ if (m != NULL) {
+ binaryfunc f = NULL;
+ f = m->sq_inplace_concat;
+ if (f == NULL)
+ f = m->sq_concat;
+ if (f != NULL)
+ return (*f)(v, w);
+ }
+ result = binop_type_error(v, w, "+=");
+ }
+ return result;
}
PyObject *
PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
{
- PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
- NB_SLOT(nb_multiply));
- if (result == Py_NotImplemented) {
- ssizeargfunc f = NULL;
- PySequenceMethods *mv = v->ob_type->tp_as_sequence;
- PySequenceMethods *mw = w->ob_type->tp_as_sequence;
- Py_DECREF(result);
- if (mv != NULL) {
- f = mv->sq_inplace_repeat;
- if (f == NULL)
- f = mv->sq_repeat;
- if (f != NULL)
- return sequence_repeat(f, v, w);
- }
- else if (mw != NULL) {
- /* Note that the right hand operand should not be
- * mutated in this case so sq_inplace_repeat is not
- * used. */
- if (mw->sq_repeat)
- return sequence_repeat(mw->sq_repeat, w, v);
- }
- result = binop_type_error(v, w, "*=");
- }
- return result;
+ PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
+ NB_SLOT(nb_multiply));
+ if (result == Py_NotImplemented) {
+ ssizeargfunc f = NULL;
+ PySequenceMethods *mv = v->ob_type->tp_as_sequence;
+ PySequenceMethods *mw = w->ob_type->tp_as_sequence;
+ Py_DECREF(result);
+ if (mv != NULL) {
+ f = mv->sq_inplace_repeat;
+ if (f == NULL)
+ f = mv->sq_repeat;
+ if (f != NULL)
+ return sequence_repeat(f, v, w);
+ }
+ else if (mw != NULL) {
+ /* Note that the right hand operand should not be
+ * mutated in this case so sq_inplace_repeat is not
+ * used. */
+ if (mw->sq_repeat)
+ return sequence_repeat(mw->sq_repeat, w, v);
+ }
+ result = binop_type_error(v, w, "*=");
+ }
+ return result;
}
PyObject *
PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
{
- return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
- NB_SLOT(nb_remainder), "%=");
+ return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
+ NB_SLOT(nb_remainder), "%=");
}
PyObject *
PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
{
- if (v->ob_type->tp_as_number &&
- v->ob_type->tp_as_number->nb_inplace_power != NULL) {
- return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
- }
- else {
- return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
- }
+ if (v->ob_type->tp_as_number &&
+ v->ob_type->tp_as_number->nb_inplace_power != NULL) {
+ return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
+ }
+ else {
+ return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
+ }
}
@@ -1146,57 +1146,57 @@ PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
PyObject *
PyNumber_Negative(PyObject *o)
{
- PyNumberMethods *m;
+ PyNumberMethods *m;
- if (o == NULL)
- return null_error();
- m = o->ob_type->tp_as_number;
- if (m && m->nb_negative)
- return (*m->nb_negative)(o);
+ if (o == NULL)
+ return null_error();
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_negative)
+ return (*m->nb_negative)(o);
- return type_error("bad operand type for unary -: '%.200s'", o);
+ return type_error("bad operand type for unary -: '%.200s'", o);
}
PyObject *
PyNumber_Positive(PyObject *o)
{
- PyNumberMethods *m;
+ PyNumberMethods *m;
- if (o == NULL)
- return null_error();
- m = o->ob_type->tp_as_number;
- if (m && m->nb_positive)
- return (*m->nb_positive)(o);
+ if (o == NULL)
+ return null_error();
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_positive)
+ return (*m->nb_positive)(o);
- return type_error("bad operand type for unary +: '%.200s'", o);
+ return type_error("bad operand type for unary +: '%.200s'", o);
}
PyObject *
PyNumber_Invert(PyObject *o)
{
- PyNumberMethods *m;
+ PyNumberMethods *m;
- if (o == NULL)
- return null_error();
- m = o->ob_type->tp_as_number;
- if (m && m->nb_invert)
- return (*m->nb_invert)(o);
+ if (o == NULL)
+ return null_error();
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_invert)
+ return (*m->nb_invert)(o);
- return type_error("bad operand type for unary ~: '%.200s'", o);
+ return type_error("bad operand type for unary ~: '%.200s'", o);
}
PyObject *
PyNumber_Absolute(PyObject *o)
{
- PyNumberMethods *m;
+ PyNumberMethods *m;
- if (o == NULL)
- return null_error();
- m = o->ob_type->tp_as_number;
- if (m && m->nb_absolute)
- return m->nb_absolute(o);
+ if (o == NULL)
+ return null_error();
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_absolute)
+ return m->nb_absolute(o);
- return type_error("bad operand type for abs(): '%.200s'", o);
+ return type_error("bad operand type for abs(): '%.200s'", o);
}
/* Return a Python Int or Long from the object item
@@ -1206,30 +1206,30 @@ PyNumber_Absolute(PyObject *o)
PyObject *
PyNumber_Index(PyObject *item)
{
- PyObject *result = NULL;
- if (item == NULL)
- return null_error();
- if (PyLong_Check(item)) {
- Py_INCREF(item);
- return item;
- }
- if (PyIndex_Check(item)) {
- result = item->ob_type->tp_as_number->nb_index(item);
- if (result && !PyLong_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "__index__ returned non-int "
- "(type %.200s)",
- result->ob_type->tp_name);
- Py_DECREF(result);
- return NULL;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "'%.200s' object cannot be interpreted "
- "as an integer", item->ob_type->tp_name);
- }
- return result;
+ PyObject *result = NULL;
+ if (item == NULL)
+ return null_error();
+ if (PyLong_Check(item)) {
+ Py_INCREF(item);
+ return item;
+ }
+ if (PyIndex_Check(item)) {
+ result = item->ob_type->tp_as_number->nb_index(item);
+ if (result && !PyLong_Check(result)) {
+ PyErr_Format(PyExc_TypeError,
+ "__index__ returned non-int "
+ "(type %.200s)",
+ result->ob_type->tp_name);
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object cannot be interpreted "
+ "as an integer", item->ob_type->tp_name);
+ }
+ return result;
}
/* Return an error on Overflow only if err is not NULL*/
@@ -1237,79 +1237,79 @@ PyNumber_Index(PyObject *item)
Py_ssize_t
PyNumber_AsSsize_t(PyObject *item, PyObject *err)
{
- Py_ssize_t result;
- PyObject *runerr;
- PyObject *value = PyNumber_Index(item);
- if (value == NULL)
- return -1;
-
- /* We're done if PyLong_AsSsize_t() returns without error. */
- result = PyLong_AsSsize_t(value);
- if (result != -1 || !(runerr = PyErr_Occurred()))
- goto finish;
-
- /* Error handling code -- only manage OverflowError differently */
- if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
- goto finish;
-
- PyErr_Clear();
- /* If no error-handling desired then the default clipping
- is sufficient.
- */
- if (!err) {
- assert(PyLong_Check(value));
- /* Whether or not it is less than or equal to
- zero is determined by the sign of ob_size
- */
- if (_PyLong_Sign(value) < 0)
- result = PY_SSIZE_T_MIN;
- else
- result = PY_SSIZE_T_MAX;
- }
- else {
- /* Otherwise replace the error with caller's error object. */
- PyErr_Format(err,
- "cannot fit '%.200s' into an index-sized integer",
- item->ob_type->tp_name);
- }
+ Py_ssize_t result;
+ PyObject *runerr;
+ PyObject *value = PyNumber_Index(item);
+ if (value == NULL)
+ return -1;
+
+ /* We're done if PyLong_AsSsize_t() returns without error. */
+ result = PyLong_AsSsize_t(value);
+ if (result != -1 || !(runerr = PyErr_Occurred()))
+ goto finish;
+
+ /* Error handling code -- only manage OverflowError differently */
+ if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
+ goto finish;
+
+ PyErr_Clear();
+ /* If no error-handling desired then the default clipping
+ is sufficient.
+ */
+ if (!err) {
+ assert(PyLong_Check(value));
+ /* Whether or not it is less than or equal to
+ zero is determined by the sign of ob_size
+ */
+ if (_PyLong_Sign(value) < 0)
+ result = PY_SSIZE_T_MIN;
+ else
+ result = PY_SSIZE_T_MAX;
+ }
+ else {
+ /* Otherwise replace the error with caller's error object. */
+ PyErr_Format(err,
+ "cannot fit '%.200s' into an index-sized integer",
+ item->ob_type->tp_name);
+ }
finish:
- Py_DECREF(value);
- return result;
+ Py_DECREF(value);
+ return result;
}
PyObject *
_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format)
{
- static PyObject *int_name = NULL;
- if (int_name == NULL) {
- int_name = PyUnicode_InternFromString("__int__");
- if (int_name == NULL)
- return NULL;
- }
-
- if (integral && !PyLong_Check(integral)) {
- /* Don't go through tp_as_number->nb_int to avoid
- hitting the classic class fallback to __trunc__. */
- PyObject *int_func = PyObject_GetAttr(integral, int_name);
- if (int_func == NULL) {
- PyErr_Clear(); /* Raise a different error. */
- goto non_integral_error;
- }
- Py_DECREF(integral);
- integral = PyEval_CallObject(int_func, NULL);
- Py_DECREF(int_func);
- if (integral && !PyLong_Check(integral)) {
- goto non_integral_error;
- }
- }
- return integral;
+ static PyObject *int_name = NULL;
+ if (int_name == NULL) {
+ int_name = PyUnicode_InternFromString("__int__");
+ if (int_name == NULL)
+ return NULL;
+ }
+
+ if (integral && !PyLong_Check(integral)) {
+ /* Don't go through tp_as_number->nb_int to avoid
+ hitting the classic class fallback to __trunc__. */
+ PyObject *int_func = PyObject_GetAttr(integral, int_name);
+ if (int_func == NULL) {
+ PyErr_Clear(); /* Raise a different error. */
+ goto non_integral_error;
+ }
+ Py_DECREF(integral);
+ integral = PyEval_CallObject(int_func, NULL);
+ Py_DECREF(int_func);
+ if (integral && !PyLong_Check(integral)) {
+ goto non_integral_error;
+ }
+ }
+ return integral;
non_integral_error:
- PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
- Py_DECREF(integral);
- return NULL;
+ PyErr_Format(PyExc_TypeError, error_format, Py_TYPE(integral)->tp_name);
+ Py_DECREF(integral);
+ return NULL;
}
@@ -1317,134 +1317,134 @@ non_integral_error:
static PyObject *
long_from_string(const char *s, Py_ssize_t len)
{
- char *end;
- PyObject *x;
-
- x = PyLong_FromString((char*)s, &end, 10);
- if (x == NULL)
- return NULL;
- if (end != s + len) {
- PyErr_SetString(PyExc_ValueError,
- "null byte in argument for int()");
- Py_DECREF(x);
- return NULL;
- }
- return x;
+ char *end;
+ PyObject *x;
+
+ x = PyLong_FromString((char*)s, &end, 10);
+ if (x == NULL)
+ return NULL;
+ if (end != s + len) {
+ PyErr_SetString(PyExc_ValueError,
+ "null byte in argument for int()");
+ Py_DECREF(x);
+ return NULL;
+ }
+ return x;
}
PyObject *
PyNumber_Long(PyObject *o)
{
- PyNumberMethods *m;
- static PyObject *trunc_name = NULL;
- PyObject *trunc_func;
- const char *buffer;
- Py_ssize_t buffer_len;
-
- if (trunc_name == NULL) {
- trunc_name = PyUnicode_InternFromString("__trunc__");
- if (trunc_name == NULL)
- return NULL;
- }
-
- if (o == NULL)
- return null_error();
- if (PyLong_CheckExact(o)) {
- Py_INCREF(o);
- return o;
- }
- m = o->ob_type->tp_as_number;
- if (m && m->nb_int) { /* This should include subclasses of int */
- PyObject *res = m->nb_int(o);
- if (res && !PyLong_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "__int__ returned non-int (type %.200s)",
- res->ob_type->tp_name);
- Py_DECREF(res);
- return NULL;
- }
- return res;
- }
- if (PyLong_Check(o)) /* An int subclass without nb_int */
- return _PyLong_Copy((PyLongObject *)o);
- trunc_func = PyObject_GetAttr(o, trunc_name);
- if (trunc_func) {
- PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
- PyObject *int_instance;
- Py_DECREF(trunc_func);
- /* __trunc__ is specified to return an Integral type,
- but long() needs to return a long. */
- int_instance = _PyNumber_ConvertIntegralToInt(
- truncated,
- "__trunc__ returned non-Integral (type %.200s)");
- return int_instance;
- }
- PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
-
- if (PyBytes_Check(o))
- /* need to do extra error checking that PyLong_FromString()
- * doesn't do. In particular long('9.5') must raise an
- * exception, not truncate the float.
- */
- return long_from_string(PyBytes_AS_STRING(o),
- PyBytes_GET_SIZE(o));
- if (PyUnicode_Check(o))
- /* The above check is done in PyLong_FromUnicode(). */
- return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
- PyUnicode_GET_SIZE(o),
- 10);
- if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
- return long_from_string(buffer, buffer_len);
-
- return type_error("int() argument must be a string or a "
- "number, not '%.200s'", o);
+ PyNumberMethods *m;
+ static PyObject *trunc_name = NULL;
+ PyObject *trunc_func;
+ const char *buffer;
+ Py_ssize_t buffer_len;
+
+ if (trunc_name == NULL) {
+ trunc_name = PyUnicode_InternFromString("__trunc__");
+ if (trunc_name == NULL)
+ return NULL;
+ }
+
+ if (o == NULL)
+ return null_error();
+ if (PyLong_CheckExact(o)) {
+ Py_INCREF(o);
+ return o;
+ }
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_int) { /* This should include subclasses of int */
+ PyObject *res = m->nb_int(o);
+ if (res && !PyLong_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__int__ returned non-int (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
+ }
+ if (PyLong_Check(o)) /* An int subclass without nb_int */
+ return _PyLong_Copy((PyLongObject *)o);
+ trunc_func = PyObject_GetAttr(o, trunc_name);
+ if (trunc_func) {
+ PyObject *truncated = PyEval_CallObject(trunc_func, NULL);
+ PyObject *int_instance;
+ Py_DECREF(trunc_func);
+ /* __trunc__ is specified to return an Integral type,
+ but long() needs to return a long. */
+ int_instance = _PyNumber_ConvertIntegralToInt(
+ truncated,
+ "__trunc__ returned non-Integral (type %.200s)");
+ return int_instance;
+ }
+ PyErr_Clear(); /* It's not an error if o.__trunc__ doesn't exist. */
+
+ if (PyBytes_Check(o))
+ /* need to do extra error checking that PyLong_FromString()
+ * doesn't do. In particular long('9.5') must raise an
+ * exception, not truncate the float.
+ */
+ return long_from_string(PyBytes_AS_STRING(o),
+ PyBytes_GET_SIZE(o));
+ if (PyUnicode_Check(o))
+ /* The above check is done in PyLong_FromUnicode(). */
+ return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
+ PyUnicode_GET_SIZE(o),
+ 10);
+ if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
+ return long_from_string(buffer, buffer_len);
+
+ return type_error("int() argument must be a string or a "
+ "number, not '%.200s'", o);
}
PyObject *
PyNumber_Float(PyObject *o)
{
- PyNumberMethods *m;
-
- if (o == NULL)
- return null_error();
- m = o->ob_type->tp_as_number;
- if (m && m->nb_float) { /* This should include subclasses of float */
- PyObject *res = m->nb_float(o);
- if (res && !PyFloat_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "__float__ returned non-float (type %.200s)",
- res->ob_type->tp_name);
- Py_DECREF(res);
- return NULL;
- }
- return res;
- }
- if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
- PyFloatObject *po = (PyFloatObject *)o;
- return PyFloat_FromDouble(po->ob_fval);
- }
- return PyFloat_FromString(o);
+ PyNumberMethods *m;
+
+ if (o == NULL)
+ return null_error();
+ m = o->ob_type->tp_as_number;
+ if (m && m->nb_float) { /* This should include subclasses of float */
+ PyObject *res = m->nb_float(o);
+ if (res && !PyFloat_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__float__ returned non-float (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
+ }
+ if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
+ PyFloatObject *po = (PyFloatObject *)o;
+ return PyFloat_FromDouble(po->ob_fval);
+ }
+ return PyFloat_FromString(o);
}
PyObject *
PyNumber_ToBase(PyObject *n, int base)
{
- PyObject *res = NULL;
- PyObject *index = PyNumber_Index(n);
+ PyObject *res = NULL;
+ PyObject *index = PyNumber_Index(n);
- if (!index)
- return NULL;
- if (PyLong_Check(index))
- res = _PyLong_Format(index, base);
- else
- /* It should not be possible to get here, as
- PyNumber_Index already has a check for the same
- condition */
- PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
- "int or long");
- Py_DECREF(index);
- return res;
+ if (!index)
+ return NULL;
+ if (PyLong_Check(index))
+ res = _PyLong_Format(index, base);
+ else
+ /* It should not be possible to get here, as
+ PyNumber_Index already has a check for the same
+ condition */
+ PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not "
+ "int or long");
+ Py_DECREF(index);
+ return res;
}
@@ -1453,507 +1453,507 @@ PyNumber_ToBase(PyObject *n, int base)
int
PySequence_Check(PyObject *s)
{
- if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
- return 0;
- return s != NULL && s->ob_type->tp_as_sequence &&
- s->ob_type->tp_as_sequence->sq_item != NULL;
+ if (PyObject_IsInstance(s, (PyObject *)&PyDict_Type))
+ return 0;
+ return s != NULL && s->ob_type->tp_as_sequence &&
+ s->ob_type->tp_as_sequence->sq_item != NULL;
}
Py_ssize_t
PySequence_Size(PyObject *s)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (s == NULL) {
- null_error();
- return -1;
- }
+ if (s == NULL) {
+ null_error();
+ return -1;
+ }
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_length)
- return m->sq_length(s);
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_length)
+ return m->sq_length(s);
- type_error("object of type '%.200s' has no len()", s);
- return -1;
+ type_error("object of type '%.200s' has no len()", s);
+ return -1;
}
#undef PySequence_Length
Py_ssize_t
PySequence_Length(PyObject *s)
{
- return PySequence_Size(s);
+ return PySequence_Size(s);
}
#define PySequence_Length PySequence_Size
PyObject *
PySequence_Concat(PyObject *s, PyObject *o)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (s == NULL || o == NULL)
- return null_error();
+ if (s == NULL || o == NULL)
+ return null_error();
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_concat)
- return m->sq_concat(s, o);
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_concat)
+ return m->sq_concat(s, o);
- /* Instances of user classes defining an __add__() method only
- have an nb_add slot, not an sq_concat slot. So we fall back
- to nb_add if both arguments appear to be sequences. */
- if (PySequence_Check(s) && PySequence_Check(o)) {
- PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
- if (result != Py_NotImplemented)
- return result;
- Py_DECREF(result);
- }
- return type_error("'%.200s' object can't be concatenated", s);
+ /* Instances of user classes defining an __add__() method only
+ have an nb_add slot, not an sq_concat slot. So we fall back
+ to nb_add if both arguments appear to be sequences. */
+ if (PySequence_Check(s) && PySequence_Check(o)) {
+ PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
+ if (result != Py_NotImplemented)
+ return result;
+ Py_DECREF(result);
+ }
+ return type_error("'%.200s' object can't be concatenated", s);
}
PyObject *
PySequence_Repeat(PyObject *o, Py_ssize_t count)
{
- PySequenceMethods *m;
-
- if (o == NULL)
- return null_error();
-
- m = o->ob_type->tp_as_sequence;
- if (m && m->sq_repeat)
- return m->sq_repeat(o, count);
-
- /* Instances of user classes defining a __mul__() method only
- have an nb_multiply slot, not an sq_repeat slot. so we fall back
- to nb_multiply if o appears to be a sequence. */
- if (PySequence_Check(o)) {
- PyObject *n, *result;
- n = PyLong_FromSsize_t(count);
- if (n == NULL)
- return NULL;
- result = binary_op1(o, n, NB_SLOT(nb_multiply));
- Py_DECREF(n);
- if (result != Py_NotImplemented)
- return result;
- Py_DECREF(result);
- }
- return type_error("'%.200s' object can't be repeated", o);
+ PySequenceMethods *m;
+
+ if (o == NULL)
+ return null_error();
+
+ m = o->ob_type->tp_as_sequence;
+ if (m && m->sq_repeat)
+ return m->sq_repeat(o, count);
+
+ /* Instances of user classes defining a __mul__() method only
+ have an nb_multiply slot, not an sq_repeat slot. so we fall back
+ to nb_multiply if o appears to be a sequence. */
+ if (PySequence_Check(o)) {
+ PyObject *n, *result;
+ n = PyLong_FromSsize_t(count);
+ if (n == NULL)
+ return NULL;
+ result = binary_op1(o, n, NB_SLOT(nb_multiply));
+ Py_DECREF(n);
+ if (result != Py_NotImplemented)
+ return result;
+ Py_DECREF(result);
+ }
+ return type_error("'%.200s' object can't be repeated", o);
}
PyObject *
PySequence_InPlaceConcat(PyObject *s, PyObject *o)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (s == NULL || o == NULL)
- return null_error();
+ if (s == NULL || o == NULL)
+ return null_error();
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_inplace_concat)
- return m->sq_inplace_concat(s, o);
- if (m && m->sq_concat)
- return m->sq_concat(s, o);
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_inplace_concat)
+ return m->sq_inplace_concat(s, o);
+ if (m && m->sq_concat)
+ return m->sq_concat(s, o);
- if (PySequence_Check(s) && PySequence_Check(o)) {
- PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
- NB_SLOT(nb_add));
- if (result != Py_NotImplemented)
- return result;
- Py_DECREF(result);
- }
- return type_error("'%.200s' object can't be concatenated", s);
+ if (PySequence_Check(s) && PySequence_Check(o)) {
+ PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
+ NB_SLOT(nb_add));
+ if (result != Py_NotImplemented)
+ return result;
+ Py_DECREF(result);
+ }
+ return type_error("'%.200s' object can't be concatenated", s);
}
PyObject *
PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
{
- PySequenceMethods *m;
-
- if (o == NULL)
- return null_error();
-
- m = o->ob_type->tp_as_sequence;
- if (m && m->sq_inplace_repeat)
- return m->sq_inplace_repeat(o, count);
- if (m && m->sq_repeat)
- return m->sq_repeat(o, count);
-
- if (PySequence_Check(o)) {
- PyObject *n, *result;
- n = PyLong_FromSsize_t(count);
- if (n == NULL)
- return NULL;
- result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
- NB_SLOT(nb_multiply));
- Py_DECREF(n);
- if (result != Py_NotImplemented)
- return result;
- Py_DECREF(result);
- }
- return type_error("'%.200s' object can't be repeated", o);
+ PySequenceMethods *m;
+
+ if (o == NULL)
+ return null_error();
+
+ m = o->ob_type->tp_as_sequence;
+ if (m && m->sq_inplace_repeat)
+ return m->sq_inplace_repeat(o, count);
+ if (m && m->sq_repeat)
+ return m->sq_repeat(o, count);
+
+ if (PySequence_Check(o)) {
+ PyObject *n, *result;
+ n = PyLong_FromSsize_t(count);
+ if (n == NULL)
+ return NULL;
+ result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
+ NB_SLOT(nb_multiply));
+ Py_DECREF(n);
+ if (result != Py_NotImplemented)
+ return result;
+ Py_DECREF(result);
+ }
+ return type_error("'%.200s' object can't be repeated", o);
}
PyObject *
PySequence_GetItem(PyObject *s, Py_ssize_t i)
{
- PySequenceMethods *m;
-
- if (s == NULL)
- return null_error();
-
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_item) {
- if (i < 0) {
- if (m->sq_length) {
- Py_ssize_t l = (*m->sq_length)(s);
- if (l < 0)
- return NULL;
- i += l;
- }
- }
- return m->sq_item(s, i);
- }
+ PySequenceMethods *m;
+
+ if (s == NULL)
+ return null_error();
+
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_item) {
+ if (i < 0) {
+ if (m->sq_length) {
+ Py_ssize_t l = (*m->sq_length)(s);
+ if (l < 0)
+ return NULL;
+ i += l;
+ }
+ }
+ return m->sq_item(s, i);
+ }
- return type_error("'%.200s' object does not support indexing", s);
+ return type_error("'%.200s' object does not support indexing", s);
}
PyObject *
PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
{
- PyMappingMethods *mp;
+ PyMappingMethods *mp;
- if (!s) return null_error();
+ if (!s) return null_error();
- mp = s->ob_type->tp_as_mapping;
- if (mp->mp_subscript) {
- PyObject *res;
- PyObject *slice = _PySlice_FromIndices(i1, i2);
- if (!slice)
- return NULL;
- res = mp->mp_subscript(s, slice);
- Py_DECREF(slice);
- return res;
- }
+ mp = s->ob_type->tp_as_mapping;
+ if (mp->mp_subscript) {
+ PyObject *res;
+ PyObject *slice = _PySlice_FromIndices(i1, i2);
+ if (!slice)
+ return NULL;
+ res = mp->mp_subscript(s, slice);
+ Py_DECREF(slice);
+ return res;
+ }
- return type_error("'%.200s' object is unsliceable", s);
+ return type_error("'%.200s' object is unsliceable", s);
}
int
PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (s == NULL) {
- null_error();
- return -1;
- }
+ if (s == NULL) {
+ null_error();
+ return -1;
+ }
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_ass_item) {
- if (i < 0) {
- if (m->sq_length) {
- Py_ssize_t l = (*m->sq_length)(s);
- if (l < 0)
- return -1;
- i += l;
- }
- }
- return m->sq_ass_item(s, i, o);
- }
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_ass_item) {
+ if (i < 0) {
+ if (m->sq_length) {
+ Py_ssize_t l = (*m->sq_length)(s);
+ if (l < 0)
+ return -1;
+ i += l;
+ }
+ }
+ return m->sq_ass_item(s, i, o);
+ }
- type_error("'%.200s' object does not support item assignment", s);
- return -1;
+ type_error("'%.200s' object does not support item assignment", s);
+ return -1;
}
int
PySequence_DelItem(PyObject *s, Py_ssize_t i)
{
- PySequenceMethods *m;
+ PySequenceMethods *m;
- if (s == NULL) {
- null_error();
- return -1;
- }
+ if (s == NULL) {
+ null_error();
+ return -1;
+ }
- m = s->ob_type->tp_as_sequence;
- if (m && m->sq_ass_item) {
- if (i < 0) {
- if (m->sq_length) {
- Py_ssize_t l = (*m->sq_length)(s);
- if (l < 0)
- return -1;
- i += l;
- }
- }
- return m->sq_ass_item(s, i, (PyObject *)NULL);
- }
+ m = s->ob_type->tp_as_sequence;
+ if (m && m->sq_ass_item) {
+ if (i < 0) {
+ if (m->sq_length) {
+ Py_ssize_t l = (*m->sq_length)(s);
+ if (l < 0)
+ return -1;
+ i += l;
+ }
+ }
+ return m->sq_ass_item(s, i, (PyObject *)NULL);
+ }
- type_error("'%.200s' object doesn't support item deletion", s);
- return -1;
+ type_error("'%.200s' object doesn't support item deletion", s);
+ return -1;
}
int
PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
{
- PyMappingMethods *mp;
+ PyMappingMethods *mp;
- if (s == NULL) {
- null_error();
- return -1;
- }
+ if (s == NULL) {
+ null_error();
+ return -1;
+ }
- mp = s->ob_type->tp_as_mapping;
- if (mp->mp_ass_subscript) {
- int res;
- PyObject *slice = _PySlice_FromIndices(i1, i2);
- if (!slice)
- return -1;
- res = mp->mp_ass_subscript(s, slice, o);
- Py_DECREF(slice);
- return res;
- }
+ mp = s->ob_type->tp_as_mapping;
+ if (mp->mp_ass_subscript) {
+ int res;
+ PyObject *slice = _PySlice_FromIndices(i1, i2);
+ if (!slice)
+ return -1;
+ res = mp->mp_ass_subscript(s, slice, o);
+ Py_DECREF(slice);
+ return res;
+ }
- type_error("'%.200s' object doesn't support slice assignment", s);
- return -1;
+ type_error("'%.200s' object doesn't support slice assignment", s);
+ return -1;
}
int
PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
{
- PyMappingMethods *mp;
+ PyMappingMethods *mp;
- if (s == NULL) {
- null_error();
- return -1;
- }
+ if (s == NULL) {
+ null_error();
+ return -1;
+ }
- mp = s->ob_type->tp_as_mapping;
- if (mp->mp_ass_subscript) {
- int res;
- PyObject *slice = _PySlice_FromIndices(i1, i2);
- if (!slice)
- return -1;
- res = mp->mp_ass_subscript(s, slice, NULL);
- Py_DECREF(slice);
- return res;
- }
- type_error("'%.200s' object doesn't support slice deletion", s);
- return -1;
+ mp = s->ob_type->tp_as_mapping;
+ if (mp->mp_ass_subscript) {
+ int res;
+ PyObject *slice = _PySlice_FromIndices(i1, i2);
+ if (!slice)
+ return -1;
+ res = mp->mp_ass_subscript(s, slice, NULL);
+ Py_DECREF(slice);
+ return res;
+ }
+ type_error("'%.200s' object doesn't support slice deletion", s);
+ return -1;
}
PyObject *
PySequence_Tuple(PyObject *v)
{
- PyObject *it; /* iter(v) */
- Py_ssize_t n; /* guess for result tuple size */
- PyObject *result = NULL;
- Py_ssize_t j;
-
- if (v == NULL)
- return null_error();
-
- /* Special-case the common tuple and list cases, for efficiency. */
- if (PyTuple_CheckExact(v)) {
- /* Note that we can't know whether it's safe to return
- a tuple *subclass* instance as-is, hence the restriction
- to exact tuples here. In contrast, lists always make
- a copy, so there's no need for exactness below. */
- Py_INCREF(v);
- return v;
- }
- if (PyList_Check(v))
- return PyList_AsTuple(v);
-
- /* Get iterator. */
- it = PyObject_GetIter(v);
- if (it == NULL)
- return NULL;
-
- /* Guess result size and allocate space. */
- n = _PyObject_LengthHint(v, 10);
- if (n == -1)
- goto Fail;
- result = PyTuple_New(n);
- if (result == NULL)
- goto Fail;
-
- /* Fill the tuple. */
- for (j = 0; ; ++j) {
- PyObject *item = PyIter_Next(it);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto Fail;
- break;
- }
- if (j >= n) {
- Py_ssize_t oldn = n;
- /* The over-allocation strategy can grow a bit faster
- than for lists because unlike lists the
- over-allocation isn't permanent -- we reclaim
- the excess before the end of this routine.
- So, grow by ten and then add 25%.
- */
- n += 10;
- n += n >> 2;
- if (n < oldn) {
- /* Check for overflow */
- PyErr_NoMemory();
- Py_DECREF(item);
- goto Fail;
- }
- if (_PyTuple_Resize(&result, n) != 0) {
- Py_DECREF(item);
- goto Fail;
- }
- }
- PyTuple_SET_ITEM(result, j, item);
- }
-
- /* Cut tuple back if guess was too large. */
- if (j < n &&
- _PyTuple_Resize(&result, j) != 0)
- goto Fail;
-
- Py_DECREF(it);
- return result;
+ PyObject *it; /* iter(v) */
+ Py_ssize_t n; /* guess for result tuple size */
+ PyObject *result = NULL;
+ Py_ssize_t j;
+
+ if (v == NULL)
+ return null_error();
+
+ /* Special-case the common tuple and list cases, for efficiency. */
+ if (PyTuple_CheckExact(v)) {
+ /* Note that we can't know whether it's safe to return
+ a tuple *subclass* instance as-is, hence the restriction
+ to exact tuples here. In contrast, lists always make
+ a copy, so there's no need for exactness below. */
+ Py_INCREF(v);
+ return v;
+ }
+ if (PyList_Check(v))
+ return PyList_AsTuple(v);
+
+ /* Get iterator. */
+ it = PyObject_GetIter(v);
+ if (it == NULL)
+ return NULL;
+
+ /* Guess result size and allocate space. */
+ n = _PyObject_LengthHint(v, 10);
+ if (n == -1)
+ goto Fail;
+ result = PyTuple_New(n);
+ if (result == NULL)
+ goto Fail;
+
+ /* Fill the tuple. */
+ for (j = 0; ; ++j) {
+ PyObject *item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto Fail;
+ break;
+ }
+ if (j >= n) {
+ Py_ssize_t oldn = n;
+ /* The over-allocation strategy can grow a bit faster
+ than for lists because unlike lists the
+ over-allocation isn't permanent -- we reclaim
+ the excess before the end of this routine.
+ So, grow by ten and then add 25%.
+ */
+ n += 10;
+ n += n >> 2;
+ if (n < oldn) {
+ /* Check for overflow */
+ PyErr_NoMemory();
+ Py_DECREF(item);
+ goto Fail;
+ }
+ if (_PyTuple_Resize(&result, n) != 0) {
+ Py_DECREF(item);
+ goto Fail;
+ }
+ }
+ PyTuple_SET_ITEM(result, j, item);
+ }
+
+ /* Cut tuple back if guess was too large. */
+ if (j < n &&
+ _PyTuple_Resize(&result, j) != 0)
+ goto Fail;
+
+ Py_DECREF(it);
+ return result;
Fail:
- Py_XDECREF(result);
- Py_DECREF(it);
- return NULL;
+ Py_XDECREF(result);
+ Py_DECREF(it);
+ return NULL;
}
PyObject *
PySequence_List(PyObject *v)
{
- PyObject *result; /* result list */
- PyObject *rv; /* return value from PyList_Extend */
+ PyObject *result; /* result list */
+ PyObject *rv; /* return value from PyList_Extend */
- if (v == NULL)
- return null_error();
+ if (v == NULL)
+ return null_error();
- result = PyList_New(0);
- if (result == NULL)
- return NULL;
+ result = PyList_New(0);
+ if (result == NULL)
+ return NULL;
- rv = _PyList_Extend((PyListObject *)result, v);
- if (rv == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(rv);
- return result;
+ rv = _PyList_Extend((PyListObject *)result, v);
+ if (rv == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(rv);
+ return result;
}
PyObject *
PySequence_Fast(PyObject *v, const char *m)
{
- PyObject *it;
+ PyObject *it;
- if (v == NULL)
- return null_error();
+ if (v == NULL)
+ return null_error();
- if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
- Py_INCREF(v);
- return v;
- }
+ if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
+ Py_INCREF(v);
+ return v;
+ }
- it = PyObject_GetIter(v);
- if (it == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_SetString(PyExc_TypeError, m);
- return NULL;
- }
+ it = PyObject_GetIter(v);
+ if (it == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_SetString(PyExc_TypeError, m);
+ return NULL;
+ }
- v = PySequence_List(it);
- Py_DECREF(it);
+ v = PySequence_List(it);
+ Py_DECREF(it);
- return v;
+ return v;
}
/* Iterate over seq. Result depends on the operation:
- PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
- PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
- set ValueError and return -1 if none found; also return -1 on error.
+ PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
+ PY_ITERSEARCH_INDEX: 0-based index of first occurrence of obj in seq;
+ set ValueError and return -1 if none found; also return -1 on error.
Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
*/
Py_ssize_t
_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
{
- Py_ssize_t n;
- int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
- PyObject *it; /* iter(seq) */
-
- if (seq == NULL || obj == NULL) {
- null_error();
- return -1;
- }
-
- it = PyObject_GetIter(seq);
- if (it == NULL) {
- type_error("argument of type '%.200s' is not iterable", seq);
- return -1;
- }
-
- n = wrapped = 0;
- for (;;) {
- int cmp;
- PyObject *item = PyIter_Next(it);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto Fail;
- break;
- }
-
- cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
- Py_DECREF(item);
- if (cmp < 0)
- goto Fail;
- if (cmp > 0) {
- switch (operation) {
- case PY_ITERSEARCH_COUNT:
- if (n == PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "count exceeds C integer size");
- goto Fail;
- }
- ++n;
- break;
-
- case PY_ITERSEARCH_INDEX:
- if (wrapped) {
- PyErr_SetString(PyExc_OverflowError,
- "index exceeds C integer size");
- goto Fail;
- }
- goto Done;
-
- case PY_ITERSEARCH_CONTAINS:
- n = 1;
- goto Done;
-
- default:
- assert(!"unknown operation");
- }
- }
-
- if (operation == PY_ITERSEARCH_INDEX) {
- if (n == PY_SSIZE_T_MAX)
- wrapped = 1;
- ++n;
- }
- }
-
- if (operation != PY_ITERSEARCH_INDEX)
- goto Done;
-
- PyErr_SetString(PyExc_ValueError,
- "sequence.index(x): x not in sequence");
- /* fall into failure code */
+ Py_ssize_t n;
+ int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
+ PyObject *it; /* iter(seq) */
+
+ if (seq == NULL || obj == NULL) {
+ null_error();
+ return -1;
+ }
+
+ it = PyObject_GetIter(seq);
+ if (it == NULL) {
+ type_error("argument of type '%.200s' is not iterable", seq);
+ return -1;
+ }
+
+ n = wrapped = 0;
+ for (;;) {
+ int cmp;
+ PyObject *item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto Fail;
+ break;
+ }
+
+ cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
+ Py_DECREF(item);
+ if (cmp < 0)
+ goto Fail;
+ if (cmp > 0) {
+ switch (operation) {
+ case PY_ITERSEARCH_COUNT:
+ if (n == PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "count exceeds C integer size");
+ goto Fail;
+ }
+ ++n;
+ break;
+
+ case PY_ITERSEARCH_INDEX:
+ if (wrapped) {
+ PyErr_SetString(PyExc_OverflowError,
+ "index exceeds C integer size");
+ goto Fail;
+ }
+ goto Done;
+
+ case PY_ITERSEARCH_CONTAINS:
+ n = 1;
+ goto Done;
+
+ default:
+ assert(!"unknown operation");
+ }
+ }
+
+ if (operation == PY_ITERSEARCH_INDEX) {
+ if (n == PY_SSIZE_T_MAX)
+ wrapped = 1;
+ ++n;
+ }
+ }
+
+ if (operation != PY_ITERSEARCH_INDEX)
+ goto Done;
+
+ PyErr_SetString(PyExc_ValueError,
+ "sequence.index(x): x not in sequence");
+ /* fall into failure code */
Fail:
- n = -1;
- /* fall through */
+ n = -1;
+ /* fall through */
Done:
- Py_DECREF(it);
- return n;
+ Py_DECREF(it);
+ return n;
}
@@ -1961,7 +1961,7 @@ Done:
Py_ssize_t
PySequence_Count(PyObject *s, PyObject *o)
{
- return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
+ return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
}
/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
@@ -1970,12 +1970,12 @@ PySequence_Count(PyObject *s, PyObject *o)
int
PySequence_Contains(PyObject *seq, PyObject *ob)
{
- Py_ssize_t result;
- PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
- if (sqm != NULL && sqm->sq_contains != NULL)
- return (*sqm->sq_contains)(seq, ob);
- result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
- return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
+ Py_ssize_t result;
+ PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
+ if (sqm != NULL && sqm->sq_contains != NULL)
+ return (*sqm->sq_contains)(seq, ob);
+ result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
+ return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
}
/* Backwards compatibility */
@@ -1983,13 +1983,13 @@ PySequence_Contains(PyObject *seq, PyObject *ob)
int
PySequence_In(PyObject *w, PyObject *v)
{
- return PySequence_Contains(w, v);
+ return PySequence_Contains(w, v);
}
Py_ssize_t
PySequence_Index(PyObject *s, PyObject *o)
{
- return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
+ return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
}
/* Operations on mappings */
@@ -1997,145 +1997,145 @@ PySequence_Index(PyObject *s, PyObject *o)
int
PyMapping_Check(PyObject *o)
{
- return o && o->ob_type->tp_as_mapping &&
- o->ob_type->tp_as_mapping->mp_subscript;
+ return o && o->ob_type->tp_as_mapping &&
+ o->ob_type->tp_as_mapping->mp_subscript;
}
Py_ssize_t
PyMapping_Size(PyObject *o)
{
- PyMappingMethods *m;
+ PyMappingMethods *m;
- if (o == NULL) {
- null_error();
- return -1;
- }
+ if (o == NULL) {
+ null_error();
+ return -1;
+ }
- m = o->ob_type->tp_as_mapping;
- if (m && m->mp_length)
- return m->mp_length(o);
+ m = o->ob_type->tp_as_mapping;
+ if (m && m->mp_length)
+ return m->mp_length(o);
- type_error("object of type '%.200s' has no len()", o);
- return -1;
+ type_error("object of type '%.200s' has no len()", o);
+ return -1;
}
#undef PyMapping_Length
Py_ssize_t
PyMapping_Length(PyObject *o)
{
- return PyMapping_Size(o);
+ return PyMapping_Size(o);
}
#define PyMapping_Length PyMapping_Size
PyObject *
PyMapping_GetItemString(PyObject *o, char *key)
{
- PyObject *okey, *r;
+ PyObject *okey, *r;
- if (key == NULL)
- return null_error();
+ if (key == NULL)
+ return null_error();
- okey = PyUnicode_FromString(key);
- if (okey == NULL)
- return NULL;
- r = PyObject_GetItem(o, okey);
- Py_DECREF(okey);
- return r;
+ okey = PyUnicode_FromString(key);
+ if (okey == NULL)
+ return NULL;
+ r = PyObject_GetItem(o, okey);
+ Py_DECREF(okey);
+ return r;
}
int
PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
{
- PyObject *okey;
- int r;
+ PyObject *okey;
+ int r;
- if (key == NULL) {
- null_error();
- return -1;
- }
+ if (key == NULL) {
+ null_error();
+ return -1;
+ }
- okey = PyUnicode_FromString(key);
- if (okey == NULL)
- return -1;
- r = PyObject_SetItem(o, okey, value);
- Py_DECREF(okey);
- return r;
+ okey = PyUnicode_FromString(key);
+ if (okey == NULL)
+ return -1;
+ r = PyObject_SetItem(o, okey, value);
+ Py_DECREF(okey);
+ return r;
}
int
PyMapping_HasKeyString(PyObject *o, char *key)
{
- PyObject *v;
+ PyObject *v;
- v = PyMapping_GetItemString(o, key);
- if (v) {
- Py_DECREF(v);
- return 1;
- }
- PyErr_Clear();
- return 0;
+ v = PyMapping_GetItemString(o, key);
+ if (v) {
+ Py_DECREF(v);
+ return 1;
+ }
+ PyErr_Clear();
+ return 0;
}
int
PyMapping_HasKey(PyObject *o, PyObject *key)
{
- PyObject *v;
+ PyObject *v;
- v = PyObject_GetItem(o, key);
- if (v) {
- Py_DECREF(v);
- return 1;
- }
- PyErr_Clear();
- return 0;
+ v = PyObject_GetItem(o, key);
+ if (v) {
+ Py_DECREF(v);
+ return 1;
+ }
+ PyErr_Clear();
+ return 0;
}
PyObject *
PyMapping_Keys(PyObject *o)
{
- PyObject *keys;
- PyObject *fast;
+ PyObject *keys;
+ PyObject *fast;
- if (PyDict_CheckExact(o))
- return PyDict_Keys(o);
- keys = PyObject_CallMethod(o, "keys", NULL);
- if (keys == NULL)
- return NULL;
- fast = PySequence_Fast(keys, "o.keys() are not iterable");
- Py_DECREF(keys);
- return fast;
+ if (PyDict_CheckExact(o))
+ return PyDict_Keys(o);
+ keys = PyObject_CallMethod(o, "keys", NULL);
+ if (keys == NULL)
+ return NULL;
+ fast = PySequence_Fast(keys, "o.keys() are not iterable");
+ Py_DECREF(keys);
+ return fast;
}
PyObject *
PyMapping_Items(PyObject *o)
{
- PyObject *items;
- PyObject *fast;
+ PyObject *items;
+ PyObject *fast;
- if (PyDict_CheckExact(o))
- return PyDict_Items(o);
- items = PyObject_CallMethod(o, "items", NULL);
- if (items == NULL)
- return NULL;
- fast = PySequence_Fast(items, "o.items() are not iterable");
- Py_DECREF(items);
- return fast;
+ if (PyDict_CheckExact(o))
+ return PyDict_Items(o);
+ items = PyObject_CallMethod(o, "items", NULL);
+ if (items == NULL)
+ return NULL;
+ fast = PySequence_Fast(items, "o.items() are not iterable");
+ Py_DECREF(items);
+ return fast;
}
PyObject *
PyMapping_Values(PyObject *o)
{
- PyObject *values;
- PyObject *fast;
+ PyObject *values;
+ PyObject *fast;
- if (PyDict_CheckExact(o))
- return PyDict_Values(o);
- values = PyObject_CallMethod(o, "values", NULL);
- if (values == NULL)
- return NULL;
- fast = PySequence_Fast(values, "o.values() are not iterable");
- Py_DECREF(values);
- return fast;
+ if (PyDict_CheckExact(o))
+ return PyDict_Values(o);
+ values = PyObject_CallMethod(o, "values", NULL);
+ if (values == NULL)
+ return NULL;
+ fast = PySequence_Fast(values, "o.values() are not iterable");
+ Py_DECREF(values);
+ return fast;
}
/* Operations on callable objects */
@@ -2145,253 +2145,253 @@ PyMapping_Values(PyObject *o)
PyObject *
PyObject_CallObject(PyObject *o, PyObject *a)
{
- return PyEval_CallObjectWithKeywords(o, a, NULL);
+ return PyEval_CallObjectWithKeywords(o, a, NULL);
}
PyObject *
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
{
- ternaryfunc call;
-
- if ((call = func->ob_type->tp_call) != NULL) {
- PyObject *result;
- if (Py_EnterRecursiveCall(" while calling a Python object"))
- return NULL;
- result = (*call)(func, arg, kw);
- Py_LeaveRecursiveCall();
- if (result == NULL && !PyErr_Occurred())
- PyErr_SetString(
- PyExc_SystemError,
- "NULL result without error in PyObject_Call");
- return result;
- }
- PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
- func->ob_type->tp_name);
- return NULL;
+ ternaryfunc call;
+
+ if ((call = func->ob_type->tp_call) != NULL) {
+ PyObject *result;
+ if (Py_EnterRecursiveCall(" while calling a Python object"))
+ return NULL;
+ result = (*call)(func, arg, kw);
+ Py_LeaveRecursiveCall();
+ if (result == NULL && !PyErr_Occurred())
+ PyErr_SetString(
+ PyExc_SystemError,
+ "NULL result without error in PyObject_Call");
+ return result;
+ }
+ PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
+ func->ob_type->tp_name);
+ return NULL;
}
static PyObject*
call_function_tail(PyObject *callable, PyObject *args)
{
- PyObject *retval;
+ PyObject *retval;
- if (args == NULL)
- return NULL;
+ if (args == NULL)
+ return NULL;
- if (!PyTuple_Check(args)) {
- PyObject *a;
+ if (!PyTuple_Check(args)) {
+ PyObject *a;
- a = PyTuple_New(1);
- if (a == NULL) {
- Py_DECREF(args);
- return NULL;
- }
- PyTuple_SET_ITEM(a, 0, args);
- args = a;
- }
- retval = PyObject_Call(callable, args, NULL);
+ a = PyTuple_New(1);
+ if (a == NULL) {
+ Py_DECREF(args);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(a, 0, args);
+ args = a;
+ }
+ retval = PyObject_Call(callable, args, NULL);
- Py_DECREF(args);
+ Py_DECREF(args);
- return retval;
+ return retval;
}
PyObject *
PyObject_CallFunction(PyObject *callable, char *format, ...)
{
- va_list va;
- PyObject *args;
+ va_list va;
+ PyObject *args;
- if (callable == NULL)
- return null_error();
+ if (callable == NULL)
+ return null_error();
- if (format && *format) {
- va_start(va, format);
- args = Py_VaBuildValue(format, va);
- va_end(va);
- }
- else
- args = PyTuple_New(0);
+ if (format && *format) {
+ va_start(va, format);
+ args = Py_VaBuildValue(format, va);
+ va_end(va);
+ }
+ else
+ args = PyTuple_New(0);
- return call_function_tail(callable, args);
+ return call_function_tail(callable, args);
}
PyObject *
_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
{
- va_list va;
- PyObject *args;
+ va_list va;
+ PyObject *args;
- if (callable == NULL)
- return null_error();
+ if (callable == NULL)
+ return null_error();
- if (format && *format) {
- va_start(va, format);
- args = _Py_VaBuildValue_SizeT(format, va);
- va_end(va);
- }
- else
- args = PyTuple_New(0);
+ if (format && *format) {
+ va_start(va, format);
+ args = _Py_VaBuildValue_SizeT(format, va);
+ va_end(va);
+ }
+ else
+ args = PyTuple_New(0);
- return call_function_tail(callable, args);
+ return call_function_tail(callable, args);
}
PyObject *
PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
{
- va_list va;
- PyObject *args;
- PyObject *func = NULL;
- PyObject *retval = NULL;
+ va_list va;
+ PyObject *args;
+ PyObject *func = NULL;
+ PyObject *retval = NULL;
- if (o == NULL || name == NULL)
- return null_error();
+ if (o == NULL || name == NULL)
+ return null_error();
- func = PyObject_GetAttrString(o, name);
- if (func == NULL) {
- PyErr_SetString(PyExc_AttributeError, name);
- return 0;
- }
+ func = PyObject_GetAttrString(o, name);
+ if (func == NULL) {
+ PyErr_SetString(PyExc_AttributeError, name);
+ return 0;
+ }
- if (!PyCallable_Check(func)) {
- type_error("attribute of type '%.200s' is not callable", func);
- goto exit;
- }
+ if (!PyCallable_Check(func)) {
+ type_error("attribute of type '%.200s' is not callable", func);
+ goto exit;
+ }
- if (format && *format) {
- va_start(va, format);
- args = Py_VaBuildValue(format, va);
- va_end(va);
- }
- else
- args = PyTuple_New(0);
+ if (format && *format) {
+ va_start(va, format);
+ args = Py_VaBuildValue(format, va);
+ va_end(va);
+ }
+ else
+ args = PyTuple_New(0);
- retval = call_function_tail(func, args);
+ retval = call_function_tail(func, args);
exit:
- /* args gets consumed in call_function_tail */
- Py_XDECREF(func);
+ /* args gets consumed in call_function_tail */
+ Py_XDECREF(func);
- return retval;
+ return retval;
}
PyObject *
_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
{
- va_list va;
- PyObject *args;
- PyObject *func = NULL;
- PyObject *retval = NULL;
+ va_list va;
+ PyObject *args;
+ PyObject *func = NULL;
+ PyObject *retval = NULL;
- if (o == NULL || name == NULL)
- return null_error();
+ if (o == NULL || name == NULL)
+ return null_error();
- func = PyObject_GetAttrString(o, name);
- if (func == NULL) {
- PyErr_SetString(PyExc_AttributeError, name);
- return 0;
- }
+ func = PyObject_GetAttrString(o, name);
+ if (func == NULL) {
+ PyErr_SetString(PyExc_AttributeError, name);
+ return 0;
+ }
- if (!PyCallable_Check(func)) {
- type_error("attribute of type '%.200s' is not callable", func);
- goto exit;
- }
+ if (!PyCallable_Check(func)) {
+ type_error("attribute of type '%.200s' is not callable", func);
+ goto exit;
+ }
- if (format && *format) {
- va_start(va, format);
- args = _Py_VaBuildValue_SizeT(format, va);
- va_end(va);
- }
- else
- args = PyTuple_New(0);
+ if (format && *format) {
+ va_start(va, format);
+ args = _Py_VaBuildValue_SizeT(format, va);
+ va_end(va);
+ }
+ else
+ args = PyTuple_New(0);
- retval = call_function_tail(func, args);
+ retval = call_function_tail(func, args);
exit:
- /* args gets consumed in call_function_tail */
- Py_XDECREF(func);
+ /* args gets consumed in call_function_tail */
+ Py_XDECREF(func);
- return retval;
+ return retval;
}
static PyObject *
objargs_mktuple(va_list va)
{
- int i, n = 0;
- va_list countva;
- PyObject *result, *tmp;
+ int i, n = 0;
+ va_list countva;
+ PyObject *result, *tmp;
#ifdef VA_LIST_IS_ARRAY
- memcpy(countva, va, sizeof(va_list));
+ memcpy(countva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(countva, va);
+ __va_copy(countva, va);
#else
- countva = va;
+ countva = va;
#endif
#endif
- while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
- ++n;
- result = PyTuple_New(n);
- if (result != NULL && n > 0) {
- for (i = 0; i < n; ++i) {
- tmp = (PyObject *)va_arg(va, PyObject *);
- PyTuple_SET_ITEM(result, i, tmp);
- Py_INCREF(tmp);
- }
- }
- return result;
+ while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
+ ++n;
+ result = PyTuple_New(n);
+ if (result != NULL && n > 0) {
+ for (i = 0; i < n; ++i) {
+ tmp = (PyObject *)va_arg(va, PyObject *);
+ PyTuple_SET_ITEM(result, i, tmp);
+ Py_INCREF(tmp);
+ }
+ }
+ return result;
}
PyObject *
PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
{
- PyObject *args, *tmp;
- va_list vargs;
+ PyObject *args, *tmp;
+ va_list vargs;
- if (callable == NULL || name == NULL)
- return null_error();
+ if (callable == NULL || name == NULL)
+ return null_error();
- callable = PyObject_GetAttr(callable, name);
- if (callable == NULL)
- return NULL;
+ callable = PyObject_GetAttr(callable, name);
+ if (callable == NULL)
+ return NULL;
- /* count the args */
- va_start(vargs, name);
- args = objargs_mktuple(vargs);
- va_end(vargs);
- if (args == NULL) {
- Py_DECREF(callable);
- return NULL;
- }
- tmp = PyObject_Call(callable, args, NULL);
- Py_DECREF(args);
- Py_DECREF(callable);
+ /* count the args */
+ va_start(vargs, name);
+ args = objargs_mktuple(vargs);
+ va_end(vargs);
+ if (args == NULL) {
+ Py_DECREF(callable);
+ return NULL;
+ }
+ tmp = PyObject_Call(callable, args, NULL);
+ Py_DECREF(args);
+ Py_DECREF(callable);
- return tmp;
+ return tmp;
}
PyObject *
PyObject_CallFunctionObjArgs(PyObject *callable, ...)
{
- PyObject *args, *tmp;
- va_list vargs;
+ PyObject *args, *tmp;
+ va_list vargs;
- if (callable == NULL)
- return null_error();
+ if (callable == NULL)
+ return null_error();
- /* count the args */
- va_start(vargs, callable);
- args = objargs_mktuple(vargs);
- va_end(vargs);
- if (args == NULL)
- return NULL;
- tmp = PyObject_Call(callable, args, NULL);
- Py_DECREF(args);
+ /* count the args */
+ va_start(vargs, callable);
+ args = objargs_mktuple(vargs);
+ va_end(vargs);
+ if (args == NULL)
+ return NULL;
+ tmp = PyObject_Call(callable, args, NULL);
+ Py_DECREF(args);
- return tmp;
+ return tmp;
}
@@ -2415,7 +2415,7 @@ PyObject_CallFunctionObjArgs(PyObject *callable, ...)
* produce exactly the same results: NULL is returned and no error is set.
*
* If some exception other than AttributeError is raised, then NULL is also
- * returned, but the exception is not cleared. That's because we want the
+ * returned, but the exception is not cleared. That's because we want the
* exception to be propagated along.
*
* Callers are expected to test for PyErr_Occurred() when the return value
@@ -2426,282 +2426,282 @@ PyObject_CallFunctionObjArgs(PyObject *callable, ...)
static PyObject *
abstract_get_bases(PyObject *cls)
{
- static PyObject *__bases__ = NULL;
- PyObject *bases;
-
- if (__bases__ == NULL) {
- __bases__ = PyUnicode_InternFromString("__bases__");
- if (__bases__ == NULL)
- return NULL;
- }
- Py_ALLOW_RECURSION
- bases = PyObject_GetAttr(cls, __bases__);
- Py_END_ALLOW_RECURSION
- if (bases == NULL) {
- if (PyErr_ExceptionMatches(PyExc_AttributeError))
- PyErr_Clear();
- return NULL;
- }
- if (!PyTuple_Check(bases)) {
- Py_DECREF(bases);
- return NULL;
- }
- return bases;
+ static PyObject *__bases__ = NULL;
+ PyObject *bases;
+
+ if (__bases__ == NULL) {
+ __bases__ = PyUnicode_InternFromString("__bases__");
+ if (__bases__ == NULL)
+ return NULL;
+ }
+ Py_ALLOW_RECURSION
+ bases = PyObject_GetAttr(cls, __bases__);
+ Py_END_ALLOW_RECURSION
+ if (bases == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_AttributeError))
+ PyErr_Clear();
+ return NULL;
+ }
+ if (!PyTuple_Check(bases)) {
+ Py_DECREF(bases);
+ return NULL;
+ }
+ return bases;
}
static int
abstract_issubclass(PyObject *derived, PyObject *cls)
{
- PyObject *bases = NULL;
- Py_ssize_t i, n;
- int r = 0;
-
- while (1) {
- if (derived == cls)
- return 1;
- bases = abstract_get_bases(derived);
- if (bases == NULL) {
- if (PyErr_Occurred())
- return -1;
- return 0;
- }
- n = PyTuple_GET_SIZE(bases);
- if (n == 0) {
- Py_DECREF(bases);
- return 0;
- }
- /* Avoid recursivity in the single inheritance case */
- if (n == 1) {
- derived = PyTuple_GET_ITEM(bases, 0);
- Py_DECREF(bases);
- continue;
- }
- for (i = 0; i < n; i++) {
- r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
- if (r != 0)
- break;
- }
- Py_DECREF(bases);
- return r;
- }
+ PyObject *bases = NULL;
+ Py_ssize_t i, n;
+ int r = 0;
+
+ while (1) {
+ if (derived == cls)
+ return 1;
+ bases = abstract_get_bases(derived);
+ if (bases == NULL) {
+ if (PyErr_Occurred())
+ return -1;
+ return 0;
+ }
+ n = PyTuple_GET_SIZE(bases);
+ if (n == 0) {
+ Py_DECREF(bases);
+ return 0;
+ }
+ /* Avoid recursivity in the single inheritance case */
+ if (n == 1) {
+ derived = PyTuple_GET_ITEM(bases, 0);
+ Py_DECREF(bases);
+ continue;
+ }
+ for (i = 0; i < n; i++) {
+ r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
+ if (r != 0)
+ break;
+ }
+ Py_DECREF(bases);
+ return r;
+ }
}
static int
check_class(PyObject *cls, const char *error)
{
- PyObject *bases = abstract_get_bases(cls);
- if (bases == NULL) {
- /* Do not mask errors. */
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError, error);
- return 0;
- }
- Py_DECREF(bases);
- return -1;
+ PyObject *bases = abstract_get_bases(cls);
+ if (bases == NULL) {
+ /* Do not mask errors. */
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError, error);
+ return 0;
+ }
+ Py_DECREF(bases);
+ return -1;
}
static int
recursive_isinstance(PyObject *inst, PyObject *cls)
{
- PyObject *icls;
- static PyObject *__class__ = NULL;
- int retval = 0;
-
- if (__class__ == NULL) {
- __class__ = PyUnicode_InternFromString("__class__");
- if (__class__ == NULL)
- return -1;
- }
-
- if (PyType_Check(cls)) {
- retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
- if (retval == 0) {
- PyObject *c = PyObject_GetAttr(inst, __class__);
- if (c == NULL) {
- PyErr_Clear();
- }
- else {
- if (c != (PyObject *)(inst->ob_type) &&
- PyType_Check(c))
- retval = PyType_IsSubtype(
- (PyTypeObject *)c,
- (PyTypeObject *)cls);
- Py_DECREF(c);
- }
- }
- }
- else {
- if (!check_class(cls,
- "isinstance() arg 2 must be a class, type,"
- " or tuple of classes and types"))
- return -1;
- icls = PyObject_GetAttr(inst, __class__);
- if (icls == NULL) {
- PyErr_Clear();
- retval = 0;
- }
- else {
- retval = abstract_issubclass(icls, cls);
- Py_DECREF(icls);
- }
- }
-
- return retval;
+ PyObject *icls;
+ static PyObject *__class__ = NULL;
+ int retval = 0;
+
+ if (__class__ == NULL) {
+ __class__ = PyUnicode_InternFromString("__class__");
+ if (__class__ == NULL)
+ return -1;
+ }
+
+ if (PyType_Check(cls)) {
+ retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
+ if (retval == 0) {
+ PyObject *c = PyObject_GetAttr(inst, __class__);
+ if (c == NULL) {
+ PyErr_Clear();
+ }
+ else {
+ if (c != (PyObject *)(inst->ob_type) &&
+ PyType_Check(c))
+ retval = PyType_IsSubtype(
+ (PyTypeObject *)c,
+ (PyTypeObject *)cls);
+ Py_DECREF(c);
+ }
+ }
+ }
+ else {
+ if (!check_class(cls,
+ "isinstance() arg 2 must be a class, type,"
+ " or tuple of classes and types"))
+ return -1;
+ icls = PyObject_GetAttr(inst, __class__);
+ if (icls == NULL) {
+ PyErr_Clear();
+ retval = 0;
+ }
+ else {
+ retval = abstract_issubclass(icls, cls);
+ Py_DECREF(icls);
+ }
+ }
+
+ return retval;
}
int
PyObject_IsInstance(PyObject *inst, PyObject *cls)
{
- static PyObject *name = NULL;
- PyObject *checker;
-
- /* Quick test for an exact match */
- if (Py_TYPE(inst) == (PyTypeObject *)cls)
- return 1;
-
- if (PyTuple_Check(cls)) {
- Py_ssize_t i;
- Py_ssize_t n;
- int r = 0;
-
- if (Py_EnterRecursiveCall(" in __instancecheck__"))
- return -1;
- n = PyTuple_GET_SIZE(cls);
- for (i = 0; i < n; ++i) {
- PyObject *item = PyTuple_GET_ITEM(cls, i);
- r = PyObject_IsInstance(inst, item);
- if (r != 0)
- /* either found it, or got an error */
- break;
- }
- Py_LeaveRecursiveCall();
- return r;
- }
-
- checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
- if (checker != NULL) {
- PyObject *res;
- int ok = -1;
- if (Py_EnterRecursiveCall(" in __instancecheck__")) {
- Py_DECREF(checker);
- return ok;
- }
- res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
- Py_LeaveRecursiveCall();
- Py_DECREF(checker);
- if (res != NULL) {
- ok = PyObject_IsTrue(res);
- Py_DECREF(res);
- }
- return ok;
- }
- else if (PyErr_Occurred())
- return -1;
- return recursive_isinstance(inst, cls);
-}
-
-static int
+ static PyObject *name = NULL;
+ PyObject *checker;
+
+ /* Quick test for an exact match */
+ if (Py_TYPE(inst) == (PyTypeObject *)cls)
+ return 1;
+
+ if (PyTuple_Check(cls)) {
+ Py_ssize_t i;
+ Py_ssize_t n;
+ int r = 0;
+
+ if (Py_EnterRecursiveCall(" in __instancecheck__"))
+ return -1;
+ n = PyTuple_GET_SIZE(cls);
+ for (i = 0; i < n; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(cls, i);
+ r = PyObject_IsInstance(inst, item);
+ if (r != 0)
+ /* either found it, or got an error */
+ break;
+ }
+ Py_LeaveRecursiveCall();
+ return r;
+ }
+
+ checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
+ if (checker != NULL) {
+ PyObject *res;
+ int ok = -1;
+ if (Py_EnterRecursiveCall(" in __instancecheck__")) {
+ Py_DECREF(checker);
+ return ok;
+ }
+ res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
+ Py_LeaveRecursiveCall();
+ Py_DECREF(checker);
+ if (res != NULL) {
+ ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ }
+ return ok;
+ }
+ else if (PyErr_Occurred())
+ return -1;
+ return recursive_isinstance(inst, cls);
+}
+
+static int
recursive_issubclass(PyObject *derived, PyObject *cls)
{
- if (PyType_Check(cls) && PyType_Check(derived)) {
- /* Fast path (non-recursive) */
- return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
- }
- if (!check_class(derived,
- "issubclass() arg 1 must be a class"))
- return -1;
- if (!check_class(cls,
- "issubclass() arg 2 must be a class"
- " or tuple of classes"))
- return -1;
+ if (PyType_Check(cls) && PyType_Check(derived)) {
+ /* Fast path (non-recursive) */
+ return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
+ }
+ if (!check_class(derived,
+ "issubclass() arg 1 must be a class"))
+ return -1;
+ if (!check_class(cls,
+ "issubclass() arg 2 must be a class"
+ " or tuple of classes"))
+ return -1;
- return abstract_issubclass(derived, cls);
+ return abstract_issubclass(derived, cls);
}
int
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
{
- static PyObject *name = NULL;
- PyObject *checker;
-
- if (PyTuple_Check(cls)) {
- Py_ssize_t i;
- Py_ssize_t n;
- int r = 0;
-
- if (Py_EnterRecursiveCall(" in __subclasscheck__"))
- return -1;
- n = PyTuple_GET_SIZE(cls);
- for (i = 0; i < n; ++i) {
- PyObject *item = PyTuple_GET_ITEM(cls, i);
- r = PyObject_IsSubclass(derived, item);
- if (r != 0)
- /* either found it, or got an error */
- break;
- }
- Py_LeaveRecursiveCall();
- return r;
- }
-
- checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
- if (checker != NULL) {
- PyObject *res;
- int ok = -1;
- if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
- Py_DECREF(checker);
- return ok;
- }
- res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
- Py_LeaveRecursiveCall();
- Py_DECREF(checker);
- if (res != NULL) {
- ok = PyObject_IsTrue(res);
- Py_DECREF(res);
- }
- return ok;
- }
- else if (PyErr_Occurred())
- return -1;
- return recursive_issubclass(derived, cls);
+ static PyObject *name = NULL;
+ PyObject *checker;
+
+ if (PyTuple_Check(cls)) {
+ Py_ssize_t i;
+ Py_ssize_t n;
+ int r = 0;
+
+ if (Py_EnterRecursiveCall(" in __subclasscheck__"))
+ return -1;
+ n = PyTuple_GET_SIZE(cls);
+ for (i = 0; i < n; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(cls, i);
+ r = PyObject_IsSubclass(derived, item);
+ if (r != 0)
+ /* either found it, or got an error */
+ break;
+ }
+ Py_LeaveRecursiveCall();
+ return r;
+ }
+
+ checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
+ if (checker != NULL) {
+ PyObject *res;
+ int ok = -1;
+ if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
+ Py_DECREF(checker);
+ return ok;
+ }
+ res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
+ Py_LeaveRecursiveCall();
+ Py_DECREF(checker);
+ if (res != NULL) {
+ ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ }
+ return ok;
+ }
+ else if (PyErr_Occurred())
+ return -1;
+ return recursive_issubclass(derived, cls);
}
int
_PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
{
- return recursive_isinstance(inst, cls);
+ return recursive_isinstance(inst, cls);
}
int
_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
{
- return recursive_issubclass(derived, cls);
+ return recursive_issubclass(derived, cls);
}
PyObject *
PyObject_GetIter(PyObject *o)
{
- PyTypeObject *t = o->ob_type;
- getiterfunc f = NULL;
- f = t->tp_iter;
- if (f == NULL) {
- if (PySequence_Check(o))
- return PySeqIter_New(o);
- return type_error("'%.200s' object is not iterable", o);
- }
- else {
- PyObject *res = (*f)(o);
- if (res != NULL && !PyIter_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "iter() returned non-iterator "
- "of type '%.100s'",
- res->ob_type->tp_name);
- Py_DECREF(res);
- res = NULL;
- }
- return res;
- }
+ PyTypeObject *t = o->ob_type;
+ getiterfunc f = NULL;
+ f = t->tp_iter;
+ if (f == NULL) {
+ if (PySequence_Check(o))
+ return PySeqIter_New(o);
+ return type_error("'%.200s' object is not iterable", o);
+ }
+ else {
+ PyObject *res = (*f)(o);
+ if (res != NULL && !PyIter_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "iter() returned non-iterator "
+ "of type '%.100s'",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ res = NULL;
+ }
+ return res;
+ }
}
/* Return next item.
@@ -2709,16 +2709,16 @@ PyObject_GetIter(PyObject *o)
* If the iteration terminates normally, return NULL and clear the
* PyExc_StopIteration exception (if it was set). PyErr_Occurred()
* will be false.
- * Else return the next object. PyErr_Occurred() will be false.
+ * Else return the next object. PyErr_Occurred() will be false.
*/
PyObject *
PyIter_Next(PyObject *iter)
{
- PyObject *result;
- result = (*iter->ob_type->tp_iternext)(iter);
- if (result == NULL &&
- PyErr_Occurred() &&
- PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- return result;
+ PyObject *result;
+ result = (*iter->ob_type->tp_iternext)(iter);
+ if (result == NULL &&
+ PyErr_Occurred() &&
+ PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ return result;
}
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index 3d55543..31510bb 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -11,30 +11,30 @@ static PyObject *true_str = NULL;
static PyObject *
bool_repr(PyObject *self)
{
- PyObject *s;
-
- if (self == Py_True)
- s = true_str ? true_str :
- (true_str = PyUnicode_InternFromString("True"));
- else
- s = false_str ? false_str :
- (false_str = PyUnicode_InternFromString("False"));
- Py_XINCREF(s);
- return s;
+ PyObject *s;
+
+ if (self == Py_True)
+ s = true_str ? true_str :
+ (true_str = PyUnicode_InternFromString("True"));
+ else
+ s = false_str ? false_str :
+ (false_str = PyUnicode_InternFromString("False"));
+ Py_XINCREF(s);
+ return s;
}
/* Function to return a bool from a C long */
PyObject *PyBool_FromLong(long ok)
{
- PyObject *result;
-
- if (ok)
- result = Py_True;
- else
- result = Py_False;
- Py_INCREF(result);
- return result;
+ PyObject *result;
+
+ if (ok)
+ result = Py_True;
+ else
+ result = Py_False;
+ Py_INCREF(result);
+ return result;
}
/* We define bool_new to always return either Py_True or Py_False */
@@ -42,16 +42,16 @@ PyObject *PyBool_FromLong(long ok)
static PyObject *
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"x", 0};
- PyObject *x = Py_False;
- long ok;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
- return NULL;
- ok = PyObject_IsTrue(x);
- if (ok < 0)
- return NULL;
- return PyBool_FromLong(ok);
+ static char *kwlist[] = {"x", 0};
+ PyObject *x = Py_False;
+ long ok;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
+ return NULL;
+ ok = PyObject_IsTrue(x);
+ if (ok < 0)
+ return NULL;
+ return PyBool_FromLong(ok);
}
/* Arithmetic operations redefined to return bool if both args are bool. */
@@ -59,25 +59,25 @@ bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
bool_and(PyObject *a, PyObject *b)
{
- if (!PyBool_Check(a) || !PyBool_Check(b))
- return PyLong_Type.tp_as_number->nb_and(a, b);
- return PyBool_FromLong((a == Py_True) & (b == Py_True));
+ if (!PyBool_Check(a) || !PyBool_Check(b))
+ return PyLong_Type.tp_as_number->nb_and(a, b);
+ return PyBool_FromLong((a == Py_True) & (b == Py_True));
}
static PyObject *
bool_or(PyObject *a, PyObject *b)
{
- if (!PyBool_Check(a) || !PyBool_Check(b))
- return PyLong_Type.tp_as_number->nb_or(a, b);
- return PyBool_FromLong((a == Py_True) | (b == Py_True));
+ if (!PyBool_Check(a) || !PyBool_Check(b))
+ return PyLong_Type.tp_as_number->nb_or(a, b);
+ return PyBool_FromLong((a == Py_True) | (b == Py_True));
}
static PyObject *
bool_xor(PyObject *a, PyObject *b)
{
- if (!PyBool_Check(a) || !PyBool_Check(b))
- return PyLong_Type.tp_as_number->nb_xor(a, b);
- return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
+ if (!PyBool_Check(a) || !PyBool_Check(b))
+ return PyLong_Type.tp_as_number->nb_xor(a, b);
+ return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
}
/* Doc string */
@@ -92,93 +92,93 @@ The class bool is a subclass of the class int, and cannot be subclassed.");
/* Arithmetic methods -- only so we can override &, |, ^. */
static PyNumberMethods bool_as_number = {
- 0, /* nb_add */
- 0, /* nb_subtract */
- 0, /* nb_multiply */
- 0, /* nb_remainder */
- 0, /* nb_divmod */
- 0, /* nb_power */
- 0, /* nb_negative */
- 0, /* nb_positive */
- 0, /* nb_absolute */
- 0, /* nb_bool */
- 0, /* nb_invert */
- 0, /* nb_lshift */
- 0, /* nb_rshift */
- bool_and, /* nb_and */
- bool_xor, /* nb_xor */
- bool_or, /* nb_or */
- 0, /* nb_int */
- 0, /* nb_reserved */
- 0, /* nb_float */
- 0, /* nb_inplace_add */
- 0, /* nb_inplace_subtract */
- 0, /* nb_inplace_multiply */
- 0, /* nb_inplace_remainder */
- 0, /* nb_inplace_power */
- 0, /* nb_inplace_lshift */
- 0, /* nb_inplace_rshift */
- 0, /* nb_inplace_and */
- 0, /* nb_inplace_xor */
- 0, /* nb_inplace_or */
- 0, /* nb_floor_divide */
- 0, /* nb_true_divide */
- 0, /* nb_inplace_floor_divide */
- 0, /* nb_inplace_true_divide */
- 0, /* nb_index */
+ 0, /* nb_add */
+ 0, /* nb_subtract */
+ 0, /* nb_multiply */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
+ 0, /* nb_power */
+ 0, /* nb_negative */
+ 0, /* nb_positive */
+ 0, /* nb_absolute */
+ 0, /* nb_bool */
+ 0, /* nb_invert */
+ 0, /* nb_lshift */
+ 0, /* nb_rshift */
+ bool_and, /* nb_and */
+ bool_xor, /* nb_xor */
+ bool_or, /* nb_or */
+ 0, /* nb_int */
+ 0, /* nb_reserved */
+ 0, /* nb_float */
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply */
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ 0, /* nb_floor_divide */
+ 0, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
+ 0, /* nb_index */
};
/* The type object for bool. Note that this cannot be subclassed! */
PyTypeObject PyBool_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "bool",
- sizeof(struct _longobject),
- 0,
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- bool_repr, /* tp_repr */
- &bool_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- bool_repr, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- bool_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- &PyLong_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- bool_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "bool",
+ sizeof(struct _longobject),
+ 0,
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ bool_repr, /* tp_repr */
+ &bool_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ bool_repr, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ bool_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ &PyLong_Type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ bool_new, /* tp_new */
};
/* The objects representing bool values False and True */
struct _longobject _Py_FalseStruct = {
- PyVarObject_HEAD_INIT(&PyBool_Type, 0)
- { 0 }
+ PyVarObject_HEAD_INIT(&PyBool_Type, 0)
+ { 0 }
};
struct _longobject _Py_TrueStruct = {
- PyVarObject_HEAD_INIT(&PyBool_Type, 1)
- { 1 }
+ PyVarObject_HEAD_INIT(&PyBool_Type, 1)
+ { 1 }
};
diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c
index 6908eb8..215a1fa 100644
--- a/Objects/bytes_methods.c
+++ b/Objects/bytes_methods.c
@@ -24,7 +24,7 @@ _Py_bytes_isspace(const char *cptr, Py_ssize_t len)
e = p + len;
for (; p < e; p++) {
- if (!Py_ISSPACE(*p))
+ if (!Py_ISSPACE(*p))
Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
@@ -46,16 +46,16 @@ _Py_bytes_isalpha(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1 && Py_ISALPHA(*p))
- Py_RETURN_TRUE;
+ Py_RETURN_TRUE;
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
for (; p < e; p++) {
- if (!Py_ISALPHA(*p))
- Py_RETURN_FALSE;
+ if (!Py_ISALPHA(*p))
+ Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
@@ -76,16 +76,16 @@ _Py_bytes_isalnum(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1 && Py_ISALNUM(*p))
- Py_RETURN_TRUE;
+ Py_RETURN_TRUE;
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
for (; p < e; p++) {
- if (!Py_ISALNUM(*p))
- Py_RETURN_FALSE;
+ if (!Py_ISALNUM(*p))
+ Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
@@ -106,16 +106,16 @@ _Py_bytes_isdigit(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1 && Py_ISDIGIT(*p))
- Py_RETURN_TRUE;
+ Py_RETURN_TRUE;
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
for (; p < e; p++) {
- if (!Py_ISDIGIT(*p))
- Py_RETURN_FALSE;
+ if (!Py_ISDIGIT(*p))
+ Py_RETURN_FALSE;
}
Py_RETURN_TRUE;
}
@@ -137,19 +137,19 @@ _Py_bytes_islower(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1)
- return PyBool_FromLong(Py_ISLOWER(*p));
+ return PyBool_FromLong(Py_ISLOWER(*p));
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
cased = 0;
for (; p < e; p++) {
- if (Py_ISUPPER(*p))
- Py_RETURN_FALSE;
- else if (!cased && Py_ISLOWER(*p))
- cased = 1;
+ if (Py_ISUPPER(*p))
+ Py_RETURN_FALSE;
+ else if (!cased && Py_ISLOWER(*p))
+ cased = 1;
}
return PyBool_FromLong(cased);
}
@@ -171,19 +171,19 @@ _Py_bytes_isupper(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1)
- return PyBool_FromLong(Py_ISUPPER(*p));
+ return PyBool_FromLong(Py_ISUPPER(*p));
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
cased = 0;
for (; p < e; p++) {
- if (Py_ISLOWER(*p))
- Py_RETURN_FALSE;
- else if (!cased && Py_ISUPPER(*p))
- cased = 1;
+ if (Py_ISLOWER(*p))
+ Py_RETURN_FALSE;
+ else if (!cased && Py_ISUPPER(*p))
+ cased = 1;
}
return PyBool_FromLong(cased);
}
@@ -207,32 +207,32 @@ _Py_bytes_istitle(const char *cptr, Py_ssize_t len)
/* Shortcut for single character strings */
if (len == 1)
- return PyBool_FromLong(Py_ISUPPER(*p));
+ return PyBool_FromLong(Py_ISUPPER(*p));
/* Special case for empty strings */
if (len == 0)
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
e = p + len;
cased = 0;
previous_is_cased = 0;
for (; p < e; p++) {
- register const unsigned char ch = *p;
-
- if (Py_ISUPPER(ch)) {
- if (previous_is_cased)
- Py_RETURN_FALSE;
- previous_is_cased = 1;
- cased = 1;
- }
- else if (Py_ISLOWER(ch)) {
- if (!previous_is_cased)
- Py_RETURN_FALSE;
- previous_is_cased = 1;
- cased = 1;
- }
- else
- previous_is_cased = 0;
+ register const unsigned char ch = *p;
+
+ if (Py_ISUPPER(ch)) {
+ if (previous_is_cased)
+ Py_RETURN_FALSE;
+ previous_is_cased = 1;
+ cased = 1;
+ }
+ else if (Py_ISLOWER(ch)) {
+ if (!previous_is_cased)
+ Py_RETURN_FALSE;
+ previous_is_cased = 1;
+ cased = 1;
+ }
+ else
+ previous_is_cased = 0;
}
return PyBool_FromLong(cased);
}
@@ -246,23 +246,23 @@ Return a copy of B with all ASCII characters converted to lowercase.");
void
_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
/*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
+ newobj = PyBytes_FromStringAndSize(NULL, len);
+ if (!newobj)
+ return NULL;
- s = PyBytes_AS_STRING(newobj);
+ s = PyBytes_AS_STRING(newobj);
*/
- Py_MEMCPY(result, cptr, len);
+ Py_MEMCPY(result, cptr, len);
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(result[i]);
- if (Py_ISUPPER(c))
- result[i] = Py_TOLOWER(c);
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(result[i]);
+ if (Py_ISUPPER(c))
+ result[i] = Py_TOLOWER(c);
+ }
}
@@ -274,23 +274,23 @@ Return a copy of B with all ASCII characters converted to uppercase.");
void
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
/*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (!newobj)
- return NULL;
+ newobj = PyBytes_FromStringAndSize(NULL, len);
+ if (!newobj)
+ return NULL;
- s = PyBytes_AS_STRING(newobj);
+ s = PyBytes_AS_STRING(newobj);
*/
- Py_MEMCPY(result, cptr, len);
+ Py_MEMCPY(result, cptr, len);
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(result[i]);
- if (Py_ISLOWER(c))
- result[i] = Py_TOUPPER(c);
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(result[i]);
+ if (Py_ISLOWER(c))
+ result[i] = Py_TOUPPER(c);
+ }
}
@@ -303,29 +303,29 @@ characters, all remaining cased characters have lowercase.");
void
_Py_bytes_title(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
- int previous_is_cased = 0;
+ Py_ssize_t i;
+ int previous_is_cased = 0;
/*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_AsString(newobj);
+ newobj = PyBytes_FromStringAndSize(NULL, len);
+ if (newobj == NULL)
+ return NULL;
+ s_new = PyBytes_AsString(newobj);
*/
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c)) {
- if (!previous_is_cased)
- c = Py_TOUPPER(c);
- previous_is_cased = 1;
- } else if (Py_ISUPPER(c)) {
- if (previous_is_cased)
- c = Py_TOLOWER(c);
- previous_is_cased = 1;
- } else
- previous_is_cased = 0;
- *result++ = c;
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c)) {
+ if (!previous_is_cased)
+ c = Py_TOUPPER(c);
+ previous_is_cased = 1;
+ } else if (Py_ISUPPER(c)) {
+ if (previous_is_cased)
+ c = Py_TOLOWER(c);
+ previous_is_cased = 1;
+ } else
+ previous_is_cased = 0;
+ *result++ = c;
+ }
}
@@ -337,30 +337,30 @@ Return a copy of B with only its first character capitalized (ASCII).");
void
_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
/*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_AsString(newobj);
+ newobj = PyBytes_FromStringAndSize(NULL, len);
+ if (newobj == NULL)
+ return NULL;
+ s_new = PyBytes_AsString(newobj);
*/
- if (0 < len) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c))
- *result = Py_TOUPPER(c);
- else
- *result = c;
- result++;
- }
- for (i = 1; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISUPPER(c))
- *result = Py_TOLOWER(c);
- else
- *result = c;
- result++;
- }
+ if (0 < len) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c))
+ *result = Py_TOUPPER(c);
+ else
+ *result = c;
+ result++;
+ }
+ for (i = 1; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISUPPER(c))
+ *result = Py_TOLOWER(c);
+ else
+ *result = c;
+ result++;
+ }
}
@@ -373,26 +373,26 @@ to lowercase ASCII and vice versa.");
void
_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
{
- Py_ssize_t i;
+ Py_ssize_t i;
/*
- newobj = PyBytes_FromStringAndSize(NULL, len);
- if (newobj == NULL)
- return NULL;
- s_new = PyBytes_AsString(newobj);
+ newobj = PyBytes_FromStringAndSize(NULL, len);
+ if (newobj == NULL)
+ return NULL;
+ s_new = PyBytes_AsString(newobj);
*/
- for (i = 0; i < len; i++) {
- int c = Py_CHARMASK(*s++);
- if (Py_ISLOWER(c)) {
- *result = Py_TOUPPER(c);
- }
- else if (Py_ISUPPER(c)) {
- *result = Py_TOLOWER(c);
- }
- else
- *result = c;
- result++;
- }
+ for (i = 0; i < len; i++) {
+ int c = Py_CHARMASK(*s++);
+ if (Py_ISLOWER(c)) {
+ *result = Py_TOUPPER(c);
+ }
+ else if (Py_ISUPPER(c)) {
+ *result = Py_TOLOWER(c);
+ }
+ else
+ *result = c;
+ result++;
+ }
}
@@ -425,40 +425,40 @@ _getbuffer(PyObject *obj, Py_buffer *view)
PyObject *
_Py_bytes_maketrans(PyObject *args)
{
- PyObject *frm, *to, *res = NULL;
- Py_buffer bfrm, bto;
- Py_ssize_t i;
- char *p;
-
- bfrm.len = -1;
- bto.len = -1;
-
- if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to))
- return NULL;
- if (_getbuffer(frm, &bfrm) < 0)
- return NULL;
- if (_getbuffer(to, &bto) < 0)
- goto done;
- if (bfrm.len != bto.len) {
- PyErr_Format(PyExc_ValueError,
- "maketrans arguments must have same length");
- goto done;
- }
- res = PyBytes_FromStringAndSize(NULL, 256);
- if (!res) {
- goto done;
- }
- p = PyBytes_AS_STRING(res);
- for (i = 0; i < 256; i++)
- p[i] = i;
- for (i = 0; i < bfrm.len; i++) {
- p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
- }
+ PyObject *frm, *to, *res = NULL;
+ Py_buffer bfrm, bto;
+ Py_ssize_t i;
+ char *p;
+
+ bfrm.len = -1;
+ bto.len = -1;
+
+ if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to))
+ return NULL;
+ if (_getbuffer(frm, &bfrm) < 0)
+ return NULL;
+ if (_getbuffer(to, &bto) < 0)
+ goto done;
+ if (bfrm.len != bto.len) {
+ PyErr_Format(PyExc_ValueError,
+ "maketrans arguments must have same length");
+ goto done;
+ }
+ res = PyBytes_FromStringAndSize(NULL, 256);
+ if (!res) {
+ goto done;
+ }
+ p = PyBytes_AS_STRING(res);
+ for (i = 0; i < 256; i++)
+ p[i] = i;
+ for (i = 0; i < bfrm.len; i++) {
+ p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
+ }
done:
- if (bfrm.len != -1)
- PyBuffer_Release(&bfrm);
- if (bto.len != -1)
- PyBuffer_Release(&bto);
- return res;
+ if (bfrm.len != -1)
+ PyBuffer_Release(&bfrm);
+ if (bto.len != -1)
+ PyBuffer_Release(&bto);
+ return res;
}
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 36d71c0..ab72401 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -14,14 +14,14 @@ _getbuffer(PyObject *obj, Py_buffer *view)
if (buffer == NULL || buffer->bf_getbuffer == NULL)
{
- PyErr_Format(PyExc_TypeError,
- "Type %.100s doesn't support the buffer API",
- Py_TYPE(obj)->tp_name);
- return -1;
+ PyErr_Format(PyExc_TypeError,
+ "Type %.100s doesn't support the buffer API",
+ Py_TYPE(obj)->tp_name);
+ return -1;
}
if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
- return -1;
+ return -1;
return view->len;
}
@@ -69,305 +69,305 @@ static PyBytesObject *nullstring;
PyObject *
PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
{
- register PyBytesObject *op;
- if (size < 0) {
- PyErr_SetString(PyExc_SystemError,
- "Negative size passed to PyBytes_FromStringAndSize");
- return NULL;
- }
- if (size == 0 && (op = nullstring) != NULL) {
+ register PyBytesObject *op;
+ if (size < 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "Negative size passed to PyBytes_FromStringAndSize");
+ return NULL;
+ }
+ if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS
- null_strings++;
+ null_strings++;
#endif
- Py_INCREF(op);
- return (PyObject *)op;
- }
- if (size == 1 && str != NULL &&
- (op = characters[*str & UCHAR_MAX]) != NULL)
- {
+ Py_INCREF(op);
+ return (PyObject *)op;
+ }
+ if (size == 1 && str != NULL &&
+ (op = characters[*str & UCHAR_MAX]) != NULL)
+ {
#ifdef COUNT_ALLOCS
- one_strings++;
+ one_strings++;
#endif
- Py_INCREF(op);
- return (PyObject *)op;
- }
-
- if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
- PyErr_SetString(PyExc_OverflowError,
- "byte string is too large");
- return NULL;
- }
-
- /* Inline PyObject_NewVar */
- op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_INIT_VAR(op, &PyBytes_Type, size);
- op->ob_shash = -1;
- if (str != NULL)
- Py_MEMCPY(op->ob_sval, str, size);
- op->ob_sval[size] = '\0';
- /* share short strings */
- if (size == 0) {
- nullstring = op;
- Py_INCREF(op);
- } else if (size == 1 && str != NULL) {
- characters[*str & UCHAR_MAX] = op;
- Py_INCREF(op);
- }
- return (PyObject *) op;
+ Py_INCREF(op);
+ return (PyObject *)op;
+ }
+
+ if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
+ PyErr_SetString(PyExc_OverflowError,
+ "byte string is too large");
+ return NULL;
+ }
+
+ /* Inline PyObject_NewVar */
+ op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
+ if (op == NULL)
+ return PyErr_NoMemory();
+ PyObject_INIT_VAR(op, &PyBytes_Type, size);
+ op->ob_shash = -1;
+ if (str != NULL)
+ Py_MEMCPY(op->ob_sval, str, size);
+ op->ob_sval[size] = '\0';
+ /* share short strings */
+ if (size == 0) {
+ nullstring = op;
+ Py_INCREF(op);
+ } else if (size == 1 && str != NULL) {
+ characters[*str & UCHAR_MAX] = op;
+ Py_INCREF(op);
+ }
+ return (PyObject *) op;
}
PyObject *
PyBytes_FromString(const char *str)
{
- register size_t size;
- register PyBytesObject *op;
-
- assert(str != NULL);
- size = strlen(str);
- if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
- PyErr_SetString(PyExc_OverflowError,
- "byte string is too long");
- return NULL;
- }
- if (size == 0 && (op = nullstring) != NULL) {
+ register size_t size;
+ register PyBytesObject *op;
+
+ assert(str != NULL);
+ size = strlen(str);
+ if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
+ PyErr_SetString(PyExc_OverflowError,
+ "byte string is too long");
+ return NULL;
+ }
+ if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS
- null_strings++;
+ null_strings++;
#endif
- Py_INCREF(op);
- return (PyObject *)op;
- }
- if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
+ Py_INCREF(op);
+ return (PyObject *)op;
+ }
+ if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
#ifdef COUNT_ALLOCS
- one_strings++;
+ one_strings++;
#endif
- Py_INCREF(op);
- return (PyObject *)op;
- }
-
- /* Inline PyObject_NewVar */
- op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_INIT_VAR(op, &PyBytes_Type, size);
- op->ob_shash = -1;
- Py_MEMCPY(op->ob_sval, str, size+1);
- /* share short strings */
- if (size == 0) {
- nullstring = op;
- Py_INCREF(op);
- } else if (size == 1) {
- characters[*str & UCHAR_MAX] = op;
- Py_INCREF(op);
- }
- return (PyObject *) op;
+ Py_INCREF(op);
+ return (PyObject *)op;
+ }
+
+ /* Inline PyObject_NewVar */
+ op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size);
+ if (op == NULL)
+ return PyErr_NoMemory();
+ PyObject_INIT_VAR(op, &PyBytes_Type, size);
+ op->ob_shash = -1;
+ Py_MEMCPY(op->ob_sval, str, size+1);
+ /* share short strings */
+ if (size == 0) {
+ nullstring = op;
+ Py_INCREF(op);
+ } else if (size == 1) {
+ characters[*str & UCHAR_MAX] = op;
+ Py_INCREF(op);
+ }
+ return (PyObject *) op;
}
PyObject *
PyBytes_FromFormatV(const char *format, va_list vargs)
{
- va_list count;
- Py_ssize_t n = 0;
- const char* f;
- char *s;
- PyObject* string;
+ va_list count;
+ Py_ssize_t n = 0;
+ const char* f;
+ char *s;
+ PyObject* string;
#ifdef VA_LIST_IS_ARRAY
- Py_MEMCPY(count, vargs, sizeof(va_list));
+ Py_MEMCPY(count, vargs, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(count, vargs);
+ __va_copy(count, vargs);
#else
- count = vargs;
+ count = vargs;
#endif
#endif
- /* step 1: figure out how large a buffer we need */
- for (f = format; *f; f++) {
- if (*f == '%') {
- const char* p = f;
- while (*++f && *f != '%' && !ISALPHA(*f))
- ;
-
- /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
- * they don't affect the amount of space we reserve.
- */
- if ((*f == 'l' || *f == 'z') &&
- (f[1] == 'd' || f[1] == 'u'))
- ++f;
-
- switch (*f) {
- case 'c':
- (void)va_arg(count, int);
- /* fall through... */
- case '%':
- n++;
- break;
- case 'd': case 'u': case 'i': case 'x':
- (void) va_arg(count, int);
- /* 20 bytes is enough to hold a 64-bit
- integer. Decimal takes the most space.
- This isn't enough for octal. */
- n += 20;
- break;
- case 's':
- s = va_arg(count, char*);
- n += strlen(s);
- break;
- case 'p':
- (void) va_arg(count, int);
- /* maximum 64-bit pointer representation:
- * 0xffffffffffffffff
- * so 19 characters is enough.
- * XXX I count 18 -- what's the extra for?
- */
- n += 19;
- break;
- default:
- /* if we stumble upon an unknown
- formatting code, copy the rest of
- the format string to the output
- string. (we cannot just skip the
- code, since there's no way to know
- what's in the argument list) */
- n += strlen(p);
- goto expand;
- }
- } else
- n++;
- }
+ /* step 1: figure out how large a buffer we need */
+ for (f = format; *f; f++) {
+ if (*f == '%') {
+ const char* p = f;
+ while (*++f && *f != '%' && !ISALPHA(*f))
+ ;
+
+ /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
+ * they don't affect the amount of space we reserve.
+ */
+ if ((*f == 'l' || *f == 'z') &&
+ (f[1] == 'd' || f[1] == 'u'))
+ ++f;
+
+ switch (*f) {
+ case 'c':
+ (void)va_arg(count, int);
+ /* fall through... */
+ case '%':
+ n++;
+ break;
+ case 'd': case 'u': case 'i': case 'x':
+ (void) va_arg(count, int);
+ /* 20 bytes is enough to hold a 64-bit
+ integer. Decimal takes the most space.
+ This isn't enough for octal. */
+ n += 20;
+ break;
+ case 's':
+ s = va_arg(count, char*);
+ n += strlen(s);
+ break;
+ case 'p':
+ (void) va_arg(count, int);
+ /* maximum 64-bit pointer representation:
+ * 0xffffffffffffffff
+ * so 19 characters is enough.
+ * XXX I count 18 -- what's the extra for?
+ */
+ n += 19;
+ break;
+ default:
+ /* if we stumble upon an unknown
+ formatting code, copy the rest of
+ the format string to the output
+ string. (we cannot just skip the
+ code, since there's no way to know
+ what's in the argument list) */
+ n += strlen(p);
+ goto expand;
+ }
+ } else
+ n++;
+ }
expand:
- /* step 2: fill the buffer */
- /* Since we've analyzed how much space we need for the worst case,
- use sprintf directly instead of the slower PyOS_snprintf. */
- string = PyBytes_FromStringAndSize(NULL, n);
- if (!string)
- return NULL;
-
- s = PyBytes_AsString(string);
-
- for (f = format; *f; f++) {
- if (*f == '%') {
- const char* p = f++;
- Py_ssize_t i;
- int longflag = 0;
- int size_tflag = 0;
- /* parse the width.precision part (we're only
- interested in the precision value, if any) */
- n = 0;
- while (ISDIGIT(*f))
- n = (n*10) + *f++ - '0';
- if (*f == '.') {
- f++;
- n = 0;
- while (ISDIGIT(*f))
- n = (n*10) + *f++ - '0';
- }
- while (*f && *f != '%' && !ISALPHA(*f))
- f++;
- /* handle the long flag, but only for %ld and %lu.
- others can be added when necessary. */
- if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
- longflag = 1;
- ++f;
- }
- /* handle the size_t flag. */
- if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
- size_tflag = 1;
- ++f;
- }
-
- switch (*f) {
- case 'c':
- *s++ = va_arg(vargs, int);
- break;
- case 'd':
- if (longflag)
- sprintf(s, "%ld", va_arg(vargs, long));
- else if (size_tflag)
- sprintf(s, "%" PY_FORMAT_SIZE_T "d",
- va_arg(vargs, Py_ssize_t));
- else
- sprintf(s, "%d", va_arg(vargs, int));
- s += strlen(s);
- break;
- case 'u':
- if (longflag)
- sprintf(s, "%lu",
- va_arg(vargs, unsigned long));
- else if (size_tflag)
- sprintf(s, "%" PY_FORMAT_SIZE_T "u",
- va_arg(vargs, size_t));
- else
- sprintf(s, "%u",
- va_arg(vargs, unsigned int));
- s += strlen(s);
- break;
- case 'i':
- sprintf(s, "%i", va_arg(vargs, int));
- s += strlen(s);
- break;
- case 'x':
- sprintf(s, "%x", va_arg(vargs, int));
- s += strlen(s);
- break;
- case 's':
- p = va_arg(vargs, char*);
- i = strlen(p);
- if (n > 0 && i > n)
- i = n;
- Py_MEMCPY(s, p, i);
- s += i;
- break;
- case 'p':
- sprintf(s, "%p", va_arg(vargs, void*));
- /* %p is ill-defined: ensure leading 0x. */
- if (s[1] == 'X')
- s[1] = 'x';
- else if (s[1] != 'x') {
- memmove(s+2, s, strlen(s)+1);
- s[0] = '0';
- s[1] = 'x';
- }
- s += strlen(s);
- break;
- case '%':
- *s++ = '%';
- break;
- default:
- strcpy(s, p);
- s += strlen(s);
- goto end;
- }
- } else
- *s++ = *f;
- }
+ /* step 2: fill the buffer */
+ /* Since we've analyzed how much space we need for the worst case,
+ use sprintf directly instead of the slower PyOS_snprintf. */
+ string = PyBytes_FromStringAndSize(NULL, n);
+ if (!string)
+ return NULL;
+
+ s = PyBytes_AsString(string);
+
+ for (f = format; *f; f++) {
+ if (*f == '%') {
+ const char* p = f++;
+ Py_ssize_t i;
+ int longflag = 0;
+ int size_tflag = 0;
+ /* parse the width.precision part (we're only
+ interested in the precision value, if any) */
+ n = 0;
+ while (ISDIGIT(*f))
+ n = (n*10) + *f++ - '0';
+ if (*f == '.') {
+ f++;
+ n = 0;
+ while (ISDIGIT(*f))
+ n = (n*10) + *f++ - '0';
+ }
+ while (*f && *f != '%' && !ISALPHA(*f))
+ f++;
+ /* handle the long flag, but only for %ld and %lu.
+ others can be added when necessary. */
+ if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
+ longflag = 1;
+ ++f;
+ }
+ /* handle the size_t flag. */
+ if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
+ size_tflag = 1;
+ ++f;
+ }
+
+ switch (*f) {
+ case 'c':
+ *s++ = va_arg(vargs, int);
+ break;
+ case 'd':
+ if (longflag)
+ sprintf(s, "%ld", va_arg(vargs, long));
+ else if (size_tflag)
+ sprintf(s, "%" PY_FORMAT_SIZE_T "d",
+ va_arg(vargs, Py_ssize_t));
+ else
+ sprintf(s, "%d", va_arg(vargs, int));
+ s += strlen(s);
+ break;
+ case 'u':
+ if (longflag)
+ sprintf(s, "%lu",
+ va_arg(vargs, unsigned long));
+ else if (size_tflag)
+ sprintf(s, "%" PY_FORMAT_SIZE_T "u",
+ va_arg(vargs, size_t));
+ else
+ sprintf(s, "%u",
+ va_arg(vargs, unsigned int));
+ s += strlen(s);
+ break;
+ case 'i':
+ sprintf(s, "%i", va_arg(vargs, int));
+ s += strlen(s);
+ break;
+ case 'x':
+ sprintf(s, "%x", va_arg(vargs, int));
+ s += strlen(s);
+ break;
+ case 's':
+ p = va_arg(vargs, char*);
+ i = strlen(p);
+ if (n > 0 && i > n)
+ i = n;
+ Py_MEMCPY(s, p, i);
+ s += i;
+ break;
+ case 'p':
+ sprintf(s, "%p", va_arg(vargs, void*));
+ /* %p is ill-defined: ensure leading 0x. */
+ if (s[1] == 'X')
+ s[1] = 'x';
+ else if (s[1] != 'x') {
+ memmove(s+2, s, strlen(s)+1);
+ s[0] = '0';
+ s[1] = 'x';
+ }
+ s += strlen(s);
+ break;
+ case '%':
+ *s++ = '%';
+ break;
+ default:
+ strcpy(s, p);
+ s += strlen(s);
+ goto end;
+ }
+ } else
+ *s++ = *f;
+ }
end:
- _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string));
- return string;
+ _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string));
+ return string;
}
PyObject *
PyBytes_FromFormat(const char *format, ...)
{
- PyObject* ret;
- va_list vargs;
+ PyObject* ret;
+ va_list vargs;
#ifdef HAVE_STDARG_PROTOTYPES
- va_start(vargs, format);
+ va_start(vargs, format);
#else
- va_start(vargs);
+ va_start(vargs);
#endif
- ret = PyBytes_FromFormatV(format, vargs);
- va_end(vargs);
- return ret;
+ ret = PyBytes_FromFormatV(format, vargs);
+ va_end(vargs);
+ return ret;
}
static void
bytes_dealloc(PyObject *op)
{
- Py_TYPE(op)->tp_free(op);
+ Py_TYPE(op)->tp_free(op);
}
/* Unescape a backslash-escaped string. If unicode is non-zero,
@@ -376,135 +376,135 @@ bytes_dealloc(PyObject *op)
specified encoding. */
PyObject *PyBytes_DecodeEscape(const char *s,
- Py_ssize_t len,
- const char *errors,
- Py_ssize_t unicode,
- const char *recode_encoding)
-{
- int c;
- char *p, *buf;
- const char *end;
- PyObject *v;
- Py_ssize_t newlen = recode_encoding ? 4*len:len;
- v = PyBytes_FromStringAndSize((char *)NULL, newlen);
- if (v == NULL)
- return NULL;
- p = buf = PyBytes_AsString(v);
- end = s + len;
- while (s < end) {
- if (*s != '\\') {
- non_esc:
- if (recode_encoding && (*s & 0x80)) {
- PyObject *u, *w;
- char *r;
- const char* t;
- Py_ssize_t rn;
- t = s;
- /* Decode non-ASCII bytes as UTF-8. */
- while (t < end && (*t & 0x80)) t++;
- u = PyUnicode_DecodeUTF8(s, t - s, errors);
- if(!u) goto failed;
-
- /* Recode them in target encoding. */
- w = PyUnicode_AsEncodedString(
- u, recode_encoding, errors);
- Py_DECREF(u);
- if (!w) goto failed;
-
- /* Append bytes to output buffer. */
- assert(PyBytes_Check(w));
- r = PyBytes_AS_STRING(w);
- rn = PyBytes_GET_SIZE(w);
- Py_MEMCPY(p, r, rn);
- p += rn;
- Py_DECREF(w);
- s = t;
- } else {
- *p++ = *s++;
- }
- continue;
- }
- s++;
- if (s==end) {
- PyErr_SetString(PyExc_ValueError,
- "Trailing \\ in string");
- goto failed;
- }
- switch (*s++) {
- /* XXX This assumes ASCII! */
- case '\n': break;
- case '\\': *p++ = '\\'; break;
- case '\'': *p++ = '\''; break;
- case '\"': *p++ = '\"'; break;
- case 'b': *p++ = '\b'; break;
- case 'f': *p++ = '\014'; break; /* FF */
- case 't': *p++ = '\t'; break;
- case 'n': *p++ = '\n'; break;
- case 'r': *p++ = '\r'; break;
- case 'v': *p++ = '\013'; break; /* VT */
- case 'a': *p++ = '\007'; break; /* BEL, not classic C */
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- c = s[-1] - '0';
- if (s < end && '0' <= *s && *s <= '7') {
- c = (c<<3) + *s++ - '0';
- if (s < end && '0' <= *s && *s <= '7')
- c = (c<<3) + *s++ - '0';
- }
- *p++ = c;
- break;
- case 'x':
- if (s+1 < end && ISXDIGIT(s[0]) && ISXDIGIT(s[1])) {
- unsigned int x = 0;
- c = Py_CHARMASK(*s);
- s++;
- if (ISDIGIT(c))
- x = c - '0';
- else if (ISLOWER(c))
- x = 10 + c - 'a';
- else
- x = 10 + c - 'A';
- x = x << 4;
- c = Py_CHARMASK(*s);
- s++;
- if (ISDIGIT(c))
- x += c - '0';
- else if (ISLOWER(c))
- x += 10 + c - 'a';
- else
- x += 10 + c - 'A';
- *p++ = x;
- break;
- }
- if (!errors || strcmp(errors, "strict") == 0) {
- PyErr_SetString(PyExc_ValueError,
- "invalid \\x escape");
- goto failed;
- }
- if (strcmp(errors, "replace") == 0) {
- *p++ = '?';
- } else if (strcmp(errors, "ignore") == 0)
- /* do nothing */;
- else {
- PyErr_Format(PyExc_ValueError,
- "decoding error; unknown "
- "error handling code: %.400s",
- errors);
- goto failed;
- }
- default:
- *p++ = '\\';
- s--;
- goto non_esc; /* an arbitry number of unescaped
- UTF-8 bytes may follow. */
- }
- }
- if (p-buf < newlen)
- _PyBytes_Resize(&v, p - buf);
- return v;
+ Py_ssize_t len,
+ const char *errors,
+ Py_ssize_t unicode,
+ const char *recode_encoding)
+{
+ int c;
+ char *p, *buf;
+ const char *end;
+ PyObject *v;
+ Py_ssize_t newlen = recode_encoding ? 4*len:len;
+ v = PyBytes_FromStringAndSize((char *)NULL, newlen);
+ if (v == NULL)
+ return NULL;
+ p = buf = PyBytes_AsString(v);
+ end = s + len;
+ while (s < end) {
+ if (*s != '\\') {
+ non_esc:
+ if (recode_encoding && (*s & 0x80)) {
+ PyObject *u, *w;
+ char *r;
+ const char* t;
+ Py_ssize_t rn;
+ t = s;
+ /* Decode non-ASCII bytes as UTF-8. */
+ while (t < end && (*t & 0x80)) t++;
+ u = PyUnicode_DecodeUTF8(s, t - s, errors);
+ if(!u) goto failed;
+
+ /* Recode them in target encoding. */
+ w = PyUnicode_AsEncodedString(
+ u, recode_encoding, errors);
+ Py_DECREF(u);
+ if (!w) goto failed;
+
+ /* Append bytes to output buffer. */
+ assert(PyBytes_Check(w));
+ r = PyBytes_AS_STRING(w);
+ rn = PyBytes_GET_SIZE(w);
+ Py_MEMCPY(p, r, rn);
+ p += rn;
+ Py_DECREF(w);
+ s = t;
+ } else {
+ *p++ = *s++;
+ }
+ continue;
+ }
+ s++;
+ if (s==end) {
+ PyErr_SetString(PyExc_ValueError,
+ "Trailing \\ in string");
+ goto failed;
+ }
+ switch (*s++) {
+ /* XXX This assumes ASCII! */
+ case '\n': break;
+ case '\\': *p++ = '\\'; break;
+ case '\'': *p++ = '\''; break;
+ case '\"': *p++ = '\"'; break;
+ case 'b': *p++ = '\b'; break;
+ case 'f': *p++ = '\014'; break; /* FF */
+ case 't': *p++ = '\t'; break;
+ case 'n': *p++ = '\n'; break;
+ case 'r': *p++ = '\r'; break;
+ case 'v': *p++ = '\013'; break; /* VT */
+ case 'a': *p++ = '\007'; break; /* BEL, not classic C */
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ c = s[-1] - '0';
+ if (s < end && '0' <= *s && *s <= '7') {
+ c = (c<<3) + *s++ - '0';
+ if (s < end && '0' <= *s && *s <= '7')
+ c = (c<<3) + *s++ - '0';
+ }
+ *p++ = c;
+ break;
+ case 'x':
+ if (s+1 < end && ISXDIGIT(s[0]) && ISXDIGIT(s[1])) {
+ unsigned int x = 0;
+ c = Py_CHARMASK(*s);
+ s++;
+ if (ISDIGIT(c))
+ x = c - '0';
+ else if (ISLOWER(c))
+ x = 10 + c - 'a';
+ else
+ x = 10 + c - 'A';
+ x = x << 4;
+ c = Py_CHARMASK(*s);
+ s++;
+ if (ISDIGIT(c))
+ x += c - '0';
+ else if (ISLOWER(c))
+ x += 10 + c - 'a';
+ else
+ x += 10 + c - 'A';
+ *p++ = x;
+ break;
+ }
+ if (!errors || strcmp(errors, "strict") == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "invalid \\x escape");
+ goto failed;
+ }
+ if (strcmp(errors, "replace") == 0) {
+ *p++ = '?';
+ } else if (strcmp(errors, "ignore") == 0)
+ /* do nothing */;
+ else {
+ PyErr_Format(PyExc_ValueError,
+ "decoding error; unknown "
+ "error handling code: %.400s",
+ errors);
+ goto failed;
+ }
+ default:
+ *p++ = '\\';
+ s--;
+ goto non_esc; /* an arbitry number of unescaped
+ UTF-8 bytes may follow. */
+ }
+ }
+ if (p-buf < newlen)
+ _PyBytes_Resize(&v, p - buf);
+ return v;
failed:
- Py_DECREF(v);
- return NULL;
+ Py_DECREF(v);
+ return NULL;
}
/* -------------------------------------------------------------------- */
@@ -513,50 +513,50 @@ PyObject *PyBytes_DecodeEscape(const char *s,
Py_ssize_t
PyBytes_Size(register PyObject *op)
{
- if (!PyBytes_Check(op)) {
- PyErr_Format(PyExc_TypeError,
- "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
- return -1;
- }
- return Py_SIZE(op);
+ if (!PyBytes_Check(op)) {
+ PyErr_Format(PyExc_TypeError,
+ "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
+ return -1;
+ }
+ return Py_SIZE(op);
}
char *
PyBytes_AsString(register PyObject *op)
{
- if (!PyBytes_Check(op)) {
- PyErr_Format(PyExc_TypeError,
- "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
- return NULL;
- }
- return ((PyBytesObject *)op)->ob_sval;
+ if (!PyBytes_Check(op)) {
+ PyErr_Format(PyExc_TypeError,
+ "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
+ return NULL;
+ }
+ return ((PyBytesObject *)op)->ob_sval;
}
int
PyBytes_AsStringAndSize(register PyObject *obj,
- register char **s,
- register Py_ssize_t *len)
-{
- if (s == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
-
- if (!PyBytes_Check(obj)) {
- PyErr_Format(PyExc_TypeError,
- "expected bytes, %.200s found", Py_TYPE(obj)->tp_name);
- return -1;
- }
-
- *s = PyBytes_AS_STRING(obj);
- if (len != NULL)
- *len = PyBytes_GET_SIZE(obj);
- else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
- PyErr_SetString(PyExc_TypeError,
- "expected bytes with no null");
- return -1;
- }
- return 0;
+ register char **s,
+ register Py_ssize_t *len)
+{
+ if (s == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+
+ if (!PyBytes_Check(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "expected bytes, %.200s found", Py_TYPE(obj)->tp_name);
+ return -1;
+ }
+
+ *s = PyBytes_AS_STRING(obj);
+ if (len != NULL)
+ *len = PyBytes_GET_SIZE(obj);
+ else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected bytes with no null");
+ return -1;
+ }
+ return 0;
}
/* -------------------------------------------------------------------- */
@@ -590,199 +590,199 @@ PyBytes_AsStringAndSize(register PyObject *obj,
PyObject *
PyBytes_Repr(PyObject *obj, int smartquotes)
{
- static const char *hexdigits = "0123456789abcdef";
- register PyBytesObject* op = (PyBytesObject*) obj;
- Py_ssize_t length = Py_SIZE(op);
- size_t newsize = 3 + 4 * length;
- PyObject *v;
- if (newsize > PY_SSIZE_T_MAX || (newsize-3) / 4 != length) {
- PyErr_SetString(PyExc_OverflowError,
- "bytes object is too large to make repr");
- return NULL;
- }
- v = PyUnicode_FromUnicode(NULL, newsize);
- if (v == NULL) {
- return NULL;
- }
- else {
- register Py_ssize_t i;
- register Py_UNICODE c;
- register Py_UNICODE *p = PyUnicode_AS_UNICODE(v);
- int quote;
-
- /* Figure out which quote to use; single is preferred */
- quote = '\'';
- if (smartquotes) {
- char *test, *start;
- start = PyBytes_AS_STRING(op);
- for (test = start; test < start+length; ++test) {
- if (*test == '"') {
- quote = '\''; /* back to single */
- goto decided;
- }
- else if (*test == '\'')
- quote = '"';
- }
- decided:
- ;
- }
-
- *p++ = 'b', *p++ = quote;
- for (i = 0; i < length; i++) {
- /* There's at least enough room for a hex escape
- and a closing quote. */
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
- c = op->ob_sval[i];
- if (c == quote || c == '\\')
- *p++ = '\\', *p++ = c;
- else if (c == '\t')
- *p++ = '\\', *p++ = 't';
- else if (c == '\n')
- *p++ = '\\', *p++ = 'n';
- else if (c == '\r')
- *p++ = '\\', *p++ = 'r';
- else if (c < ' ' || c >= 0x7f) {
- *p++ = '\\';
- *p++ = 'x';
- *p++ = hexdigits[(c & 0xf0) >> 4];
- *p++ = hexdigits[c & 0xf];
- }
- else
- *p++ = c;
- }
- assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
- *p++ = quote;
- *p = '\0';
- if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
- Py_DECREF(v);
- return NULL;
- }
- return v;
- }
+ static const char *hexdigits = "0123456789abcdef";
+ register PyBytesObject* op = (PyBytesObject*) obj;
+ Py_ssize_t length = Py_SIZE(op);
+ size_t newsize = 3 + 4 * length;
+ PyObject *v;
+ if (newsize > PY_SSIZE_T_MAX || (newsize-3) / 4 != length) {
+ PyErr_SetString(PyExc_OverflowError,
+ "bytes object is too large to make repr");
+ return NULL;
+ }
+ v = PyUnicode_FromUnicode(NULL, newsize);
+ if (v == NULL) {
+ return NULL;
+ }
+ else {
+ register Py_ssize_t i;
+ register Py_UNICODE c;
+ register Py_UNICODE *p = PyUnicode_AS_UNICODE(v);
+ int quote;
+
+ /* Figure out which quote to use; single is preferred */
+ quote = '\'';
+ if (smartquotes) {
+ char *test, *start;
+ start = PyBytes_AS_STRING(op);
+ for (test = start; test < start+length; ++test) {
+ if (*test == '"') {
+ quote = '\''; /* back to single */
+ goto decided;
+ }
+ else if (*test == '\'')
+ quote = '"';
+ }
+ decided:
+ ;
+ }
+
+ *p++ = 'b', *p++ = quote;
+ for (i = 0; i < length; i++) {
+ /* There's at least enough room for a hex escape
+ and a closing quote. */
+ assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
+ c = op->ob_sval[i];
+ if (c == quote || c == '\\')
+ *p++ = '\\', *p++ = c;
+ else if (c == '\t')
+ *p++ = '\\', *p++ = 't';
+ else if (c == '\n')
+ *p++ = '\\', *p++ = 'n';
+ else if (c == '\r')
+ *p++ = '\\', *p++ = 'r';
+ else if (c < ' ' || c >= 0x7f) {
+ *p++ = '\\';
+ *p++ = 'x';
+ *p++ = hexdigits[(c & 0xf0) >> 4];
+ *p++ = hexdigits[c & 0xf];
+ }
+ else
+ *p++ = c;
+ }
+ assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
+ *p++ = quote;
+ *p = '\0';
+ if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ return v;
+ }
}
static PyObject *
bytes_repr(PyObject *op)
{
- return PyBytes_Repr(op, 1);
+ return PyBytes_Repr(op, 1);
}
static PyObject *
bytes_str(PyObject *op)
{
- if (Py_BytesWarningFlag) {
- if (PyErr_WarnEx(PyExc_BytesWarning,
- "str() on a bytes instance", 1))
- return NULL;
- }
- return bytes_repr(op);
+ if (Py_BytesWarningFlag) {
+ if (PyErr_WarnEx(PyExc_BytesWarning,
+ "str() on a bytes instance", 1))
+ return NULL;
+ }
+ return bytes_repr(op);
}
static Py_ssize_t
bytes_length(PyBytesObject *a)
{
- return Py_SIZE(a);
+ return Py_SIZE(a);
}
/* This is also used by PyBytes_Concat() */
static PyObject *
bytes_concat(PyObject *a, PyObject *b)
{
- Py_ssize_t size;
- Py_buffer va, vb;
- PyObject *result = NULL;
-
- va.len = -1;
- vb.len = -1;
- if (_getbuffer(a, &va) < 0 ||
- _getbuffer(b, &vb) < 0) {
- PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
- Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
- goto done;
- }
-
- /* Optimize end cases */
- if (va.len == 0 && PyBytes_CheckExact(b)) {
- result = b;
- Py_INCREF(result);
- goto done;
- }
- if (vb.len == 0 && PyBytes_CheckExact(a)) {
- result = a;
- Py_INCREF(result);
- goto done;
- }
-
- size = va.len + vb.len;
- if (size < 0) {
- PyErr_NoMemory();
- goto done;
- }
-
- result = PyBytes_FromStringAndSize(NULL, size);
- if (result != NULL) {
- memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
- memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
- }
+ Py_ssize_t size;
+ Py_buffer va, vb;
+ PyObject *result = NULL;
+
+ va.len = -1;
+ vb.len = -1;
+ if (_getbuffer(a, &va) < 0 ||
+ _getbuffer(b, &vb) < 0) {
+ PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
+ Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
+ goto done;
+ }
+
+ /* Optimize end cases */
+ if (va.len == 0 && PyBytes_CheckExact(b)) {
+ result = b;
+ Py_INCREF(result);
+ goto done;
+ }
+ if (vb.len == 0 && PyBytes_CheckExact(a)) {
+ result = a;
+ Py_INCREF(result);
+ goto done;
+ }
+
+ size = va.len + vb.len;
+ if (size < 0) {
+ PyErr_NoMemory();
+ goto done;
+ }
+
+ result = PyBytes_FromStringAndSize(NULL, size);
+ if (result != NULL) {
+ memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
+ memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
+ }
done:
- if (va.len != -1)
- PyBuffer_Release(&va);
- if (vb.len != -1)
- PyBuffer_Release(&vb);
- return result;
+ if (va.len != -1)
+ PyBuffer_Release(&va);
+ if (vb.len != -1)
+ PyBuffer_Release(&vb);
+ return result;
}
static PyObject *
bytes_repeat(register PyBytesObject *a, register Py_ssize_t n)
{
- register Py_ssize_t i;
- register Py_ssize_t j;
- register Py_ssize_t size;
- register PyBytesObject *op;
- size_t nbytes;
- if (n < 0)
- n = 0;
- /* watch out for overflows: the size can overflow int,
- * and the # of bytes needed can overflow size_t
- */
- size = Py_SIZE(a) * n;
- if (n && size / n != Py_SIZE(a)) {
- PyErr_SetString(PyExc_OverflowError,
- "repeated bytes are too long");
- return NULL;
- }
- if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
- Py_INCREF(a);
- return (PyObject *)a;
- }
- nbytes = (size_t)size;
- if (nbytes + PyBytesObject_SIZE <= nbytes) {
- PyErr_SetString(PyExc_OverflowError,
- "repeated bytes are too long");
- return NULL;
- }
- op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_INIT_VAR(op, &PyBytes_Type, size);
- op->ob_shash = -1;
- op->ob_sval[size] = '\0';
- if (Py_SIZE(a) == 1 && n > 0) {
- memset(op->ob_sval, a->ob_sval[0] , n);
- return (PyObject *) op;
- }
- i = 0;
- if (i < size) {
- Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
- i = Py_SIZE(a);
- }
- while (i < size) {
- j = (i <= size-i) ? i : size-i;
- Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
- i += j;
- }
- return (PyObject *) op;
+ register Py_ssize_t i;
+ register Py_ssize_t j;
+ register Py_ssize_t size;
+ register PyBytesObject *op;
+ size_t nbytes;
+ if (n < 0)
+ n = 0;
+ /* watch out for overflows: the size can overflow int,
+ * and the # of bytes needed can overflow size_t
+ */
+ size = Py_SIZE(a) * n;
+ if (n && size / n != Py_SIZE(a)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "repeated bytes are too long");
+ return NULL;
+ }
+ if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
+ Py_INCREF(a);
+ return (PyObject *)a;
+ }
+ nbytes = (size_t)size;
+ if (nbytes + PyBytesObject_SIZE <= nbytes) {
+ PyErr_SetString(PyExc_OverflowError,
+ "repeated bytes are too long");
+ return NULL;
+ }
+ op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes);
+ if (op == NULL)
+ return PyErr_NoMemory();
+ PyObject_INIT_VAR(op, &PyBytes_Type, size);
+ op->ob_shash = -1;
+ op->ob_sval[size] = '\0';
+ if (Py_SIZE(a) == 1 && n > 0) {
+ memset(op->ob_sval, a->ob_sval[0] , n);
+ return (PyObject *) op;
+ }
+ i = 0;
+ if (i < size) {
+ Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a));
+ i = Py_SIZE(a);
+ }
+ while (i < size) {
+ j = (i <= size-i) ? i : size-i;
+ Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
+ i += j;
+ }
+ return (PyObject *) op;
}
static int
@@ -790,19 +790,19 @@ bytes_contains(PyObject *self, PyObject *arg)
{
Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
if (ival == -1 && PyErr_Occurred()) {
- Py_buffer varg;
- int pos;
- PyErr_Clear();
- if (_getbuffer(arg, &varg) < 0)
- return -1;
- pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self),
- varg.buf, varg.len, 0);
- PyBuffer_Release(&varg);
- return pos >= 0;
+ Py_buffer varg;
+ int pos;
+ PyErr_Clear();
+ if (_getbuffer(arg, &varg) < 0)
+ return -1;
+ pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self),
+ varg.buf, varg.len, 0);
+ PyBuffer_Release(&varg);
+ return pos >= 0;
}
if (ival < 0 || ival >= 256) {
- PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
- return -1;
+ PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
+ return -1;
}
return memchr(PyBytes_AS_STRING(self), ival, Py_SIZE(self)) != NULL;
@@ -811,197 +811,197 @@ bytes_contains(PyObject *self, PyObject *arg)
static PyObject *
bytes_item(PyBytesObject *a, register Py_ssize_t i)
{
- if (i < 0 || i >= Py_SIZE(a)) {
- PyErr_SetString(PyExc_IndexError, "index out of range");
- return NULL;
- }
- return PyLong_FromLong((unsigned char)a->ob_sval[i]);
+ if (i < 0 || i >= Py_SIZE(a)) {
+ PyErr_SetString(PyExc_IndexError, "index out of range");
+ return NULL;
+ }
+ return PyLong_FromLong((unsigned char)a->ob_sval[i]);
}
static PyObject*
bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
{
- int c;
- Py_ssize_t len_a, len_b;
- Py_ssize_t min_len;
- PyObject *result;
-
- /* Make sure both arguments are strings. */
- if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
- if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) &&
- (PyObject_IsInstance((PyObject*)a,
- (PyObject*)&PyUnicode_Type) ||
- PyObject_IsInstance((PyObject*)b,
- (PyObject*)&PyUnicode_Type))) {
- if (PyErr_WarnEx(PyExc_BytesWarning,
- "Comparison between bytes and string", 1))
- return NULL;
- }
- result = Py_NotImplemented;
- goto out;
- }
- if (a == b) {
- switch (op) {
- case Py_EQ:case Py_LE:case Py_GE:
- result = Py_True;
- goto out;
- case Py_NE:case Py_LT:case Py_GT:
- result = Py_False;
- goto out;
- }
- }
- if (op == Py_EQ) {
- /* Supporting Py_NE here as well does not save
- much time, since Py_NE is rarely used. */
- if (Py_SIZE(a) == Py_SIZE(b)
- && (a->ob_sval[0] == b->ob_sval[0]
- && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
- result = Py_True;
- } else {
- result = Py_False;
- }
- goto out;
- }
- len_a = Py_SIZE(a); len_b = Py_SIZE(b);
- min_len = (len_a < len_b) ? len_a : len_b;
- if (min_len > 0) {
- c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
- if (c==0)
- c = memcmp(a->ob_sval, b->ob_sval, min_len);
- } else
- c = 0;
- if (c == 0)
- c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
- switch (op) {
- case Py_LT: c = c < 0; break;
- case Py_LE: c = c <= 0; break;
- case Py_EQ: assert(0); break; /* unreachable */
- case Py_NE: c = c != 0; break;
- case Py_GT: c = c > 0; break;
- case Py_GE: c = c >= 0; break;
- default:
- result = Py_NotImplemented;
- goto out;
- }
- result = c ? Py_True : Py_False;
+ int c;
+ Py_ssize_t len_a, len_b;
+ Py_ssize_t min_len;
+ PyObject *result;
+
+ /* Make sure both arguments are strings. */
+ if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
+ if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) &&
+ (PyObject_IsInstance((PyObject*)a,
+ (PyObject*)&PyUnicode_Type) ||
+ PyObject_IsInstance((PyObject*)b,
+ (PyObject*)&PyUnicode_Type))) {
+ if (PyErr_WarnEx(PyExc_BytesWarning,
+ "Comparison between bytes and string", 1))
+ return NULL;
+ }
+ result = Py_NotImplemented;
+ goto out;
+ }
+ if (a == b) {
+ switch (op) {
+ case Py_EQ:case Py_LE:case Py_GE:
+ result = Py_True;
+ goto out;
+ case Py_NE:case Py_LT:case Py_GT:
+ result = Py_False;
+ goto out;
+ }
+ }
+ if (op == Py_EQ) {
+ /* Supporting Py_NE here as well does not save
+ much time, since Py_NE is rarely used. */
+ if (Py_SIZE(a) == Py_SIZE(b)
+ && (a->ob_sval[0] == b->ob_sval[0]
+ && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
+ result = Py_True;
+ } else {
+ result = Py_False;
+ }
+ goto out;
+ }
+ len_a = Py_SIZE(a); len_b = Py_SIZE(b);
+ min_len = (len_a < len_b) ? len_a : len_b;
+ if (min_len > 0) {
+ c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
+ if (c==0)
+ c = memcmp(a->ob_sval, b->ob_sval, min_len);
+ } else
+ c = 0;
+ if (c == 0)
+ c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
+ switch (op) {
+ case Py_LT: c = c < 0; break;
+ case Py_LE: c = c <= 0; break;
+ case Py_EQ: assert(0); break; /* unreachable */
+ case Py_NE: c = c != 0; break;
+ case Py_GT: c = c > 0; break;
+ case Py_GE: c = c >= 0; break;
+ default:
+ result = Py_NotImplemented;
+ goto out;
+ }
+ result = c ? Py_True : Py_False;
out:
- Py_INCREF(result);
- return result;
+ Py_INCREF(result);
+ return result;
}
static long
bytes_hash(PyBytesObject *a)
{
- register Py_ssize_t len;
- register unsigned char *p;
- register long x;
+ register Py_ssize_t len;
+ register unsigned char *p;
+ register long x;
- if (a->ob_shash != -1)
- return a->ob_shash;
- len = Py_SIZE(a);
- p = (unsigned char *) a->ob_sval;
- x = *p << 7;
- while (--len >= 0)
- x = (1000003*x) ^ *p++;
- x ^= Py_SIZE(a);
- if (x == -1)
- x = -2;
- a->ob_shash = x;
- return x;
+ if (a->ob_shash != -1)
+ return a->ob_shash;
+ len = Py_SIZE(a);
+ p = (unsigned char *) a->ob_sval;
+ x = *p << 7;
+ while (--len >= 0)
+ x = (1000003*x) ^ *p++;
+ x ^= Py_SIZE(a);
+ if (x == -1)
+ x = -2;
+ a->ob_shash = x;
+ return x;
}
static PyObject*
bytes_subscript(PyBytesObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- if (i < 0)
- i += PyBytes_GET_SIZE(self);
- if (i < 0 || i >= PyBytes_GET_SIZE(self)) {
- PyErr_SetString(PyExc_IndexError,
- "index out of range");
- return NULL;
- }
- return PyLong_FromLong((unsigned char)self->ob_sval[i]);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelength, cur, i;
- char* source_buf;
- char* result_buf;
- PyObject* result;
-
- if (PySlice_GetIndicesEx((PySliceObject*)item,
- PyBytes_GET_SIZE(self),
- &start, &stop, &step, &slicelength) < 0) {
- return NULL;
- }
-
- if (slicelength <= 0) {
- return PyBytes_FromStringAndSize("", 0);
- }
- else if (start == 0 && step == 1 &&
- slicelength == PyBytes_GET_SIZE(self) &&
- PyBytes_CheckExact(self)) {
- Py_INCREF(self);
- return (PyObject *)self;
- }
- else if (step == 1) {
- return PyBytes_FromStringAndSize(
- PyBytes_AS_STRING(self) + start,
- slicelength);
- }
- else {
- source_buf = PyBytes_AS_STRING(self);
- result = PyBytes_FromStringAndSize(NULL, slicelength);
- if (result == NULL)
- return NULL;
-
- result_buf = PyBytes_AS_STRING(result);
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- result_buf[i] = source_buf[cur];
- }
-
- return result;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "byte indices must be integers, not %.200s",
- Py_TYPE(item)->tp_name);
- return NULL;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += PyBytes_GET_SIZE(self);
+ if (i < 0 || i >= PyBytes_GET_SIZE(self)) {
+ PyErr_SetString(PyExc_IndexError,
+ "index out of range");
+ return NULL;
+ }
+ return PyLong_FromLong((unsigned char)self->ob_sval[i]);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength, cur, i;
+ char* source_buf;
+ char* result_buf;
+ PyObject* result;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item,
+ PyBytes_GET_SIZE(self),
+ &start, &stop, &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ return PyBytes_FromStringAndSize("", 0);
+ }
+ else if (start == 0 && step == 1 &&
+ slicelength == PyBytes_GET_SIZE(self) &&
+ PyBytes_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
+ else if (step == 1) {
+ return PyBytes_FromStringAndSize(
+ PyBytes_AS_STRING(self) + start,
+ slicelength);
+ }
+ else {
+ source_buf = PyBytes_AS_STRING(self);
+ result = PyBytes_FromStringAndSize(NULL, slicelength);
+ if (result == NULL)
+ return NULL;
+
+ result_buf = PyBytes_AS_STRING(result);
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ result_buf[i] = source_buf[cur];
+ }
+
+ return result;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "byte indices must be integers, not %.200s",
+ Py_TYPE(item)->tp_name);
+ return NULL;
+ }
}
static int
bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags)
{
- return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self),
- 1, flags);
+ return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self),
+ 1, flags);
}
static PySequenceMethods bytes_as_sequence = {
- (lenfunc)bytes_length, /*sq_length*/
- (binaryfunc)bytes_concat, /*sq_concat*/
- (ssizeargfunc)bytes_repeat, /*sq_repeat*/
- (ssizeargfunc)bytes_item, /*sq_item*/
- 0, /*sq_slice*/
- 0, /*sq_ass_item*/
- 0, /*sq_ass_slice*/
- (objobjproc)bytes_contains /*sq_contains*/
+ (lenfunc)bytes_length, /*sq_length*/
+ (binaryfunc)bytes_concat, /*sq_concat*/
+ (ssizeargfunc)bytes_repeat, /*sq_repeat*/
+ (ssizeargfunc)bytes_item, /*sq_item*/
+ 0, /*sq_slice*/
+ 0, /*sq_ass_item*/
+ 0, /*sq_ass_slice*/
+ (objobjproc)bytes_contains /*sq_contains*/
};
static PyMappingMethods bytes_as_mapping = {
- (lenfunc)bytes_length,
- (binaryfunc)bytes_subscript,
- 0,
+ (lenfunc)bytes_length,
+ (binaryfunc)bytes_subscript,
+ 0,
};
static PyBufferProcs bytes_as_buffer = {
- (getbufferproc)bytes_buffer_getbuffer,
- NULL,
+ (getbufferproc)bytes_buffer_getbuffer,
+ NULL,
};
@@ -1016,9 +1016,9 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
/* Don't call if length < 2 */
-#define Py_STRING_MATCH(target, offset, pattern, length) \
- (target[offset] == pattern[0] && \
- target[offset+length-1] == pattern[length-1] && \
+#define Py_STRING_MATCH(target, offset, pattern, length) \
+ (target[offset] == pattern[0] && \
+ target[offset+length-1] == pattern[length-1] && \
!memcmp(target+offset+1, pattern+1, length-2) )
@@ -1033,24 +1033,24 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
/* 5 splits gives 6 elements */
#define PREALLOC_SIZE(maxsplit) \
- (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
-
-#define SPLIT_ADD(data, left, right) { \
- str = PyBytes_FromStringAndSize((data) + (left), \
- (right) - (left)); \
- if (str == NULL) \
- goto onError; \
- if (count < MAX_PREALLOC) { \
- PyList_SET_ITEM(list, count, str); \
- } else { \
- if (PyList_Append(list, str)) { \
- Py_DECREF(str); \
- goto onError; \
- } \
- else \
- Py_DECREF(str); \
- } \
- count++; }
+ (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
+
+#define SPLIT_ADD(data, left, right) { \
+ str = PyBytes_FromStringAndSize((data) + (left), \
+ (right) - (left)); \
+ if (str == NULL) \
+ goto onError; \
+ if (count < MAX_PREALLOC) { \
+ PyList_SET_ITEM(list, count, str); \
+ } else { \
+ if (PyList_Append(list, str)) { \
+ Py_DECREF(str); \
+ goto onError; \
+ } \
+ else \
+ Py_DECREF(str); \
+ } \
+ count++; }
/* Always force the list to the expected size. */
#define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count
@@ -1063,82 +1063,82 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
Py_LOCAL_INLINE(PyObject *)
split_whitespace(PyBytesObject *self, Py_ssize_t len, Py_ssize_t maxsplit)
{
- const char *s = PyBytes_AS_STRING(self);
- Py_ssize_t i, j, count=0;
- PyObject *str;
- PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
-
- if (list == NULL)
- return NULL;
-
- i = j = 0;
-
- while (maxsplit-- > 0) {
- SKIP_SPACE(s, i, len);
- if (i==len) break;
- j = i; i++;
- SKIP_NONSPACE(s, i, len);
- if (j == 0 && i == len && PyBytes_CheckExact(self)) {
- /* No whitespace in self, so just use it as list[0] */
- Py_INCREF(self);
- PyList_SET_ITEM(list, 0, (PyObject *)self);
- count++;
- break;
- }
- SPLIT_ADD(s, j, i);
- }
-
- if (i < len) {
- /* Only occurs when maxsplit was reached */
- /* Skip any remaining whitespace and copy to end of string */
- SKIP_SPACE(s, i, len);
- if (i != len)
- SPLIT_ADD(s, i, len);
- }
- FIX_PREALLOC_SIZE(list);
- return list;
+ const char *s = PyBytes_AS_STRING(self);
+ Py_ssize_t i, j, count=0;
+ PyObject *str;
+ PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
+
+ if (list == NULL)
+ return NULL;
+
+ i = j = 0;
+
+ while (maxsplit-- > 0) {
+ SKIP_SPACE(s, i, len);
+ if (i==len) break;
+ j = i; i++;
+ SKIP_NONSPACE(s, i, len);
+ if (j == 0 && i == len && PyBytes_CheckExact(self)) {
+ /* No whitespace in self, so just use it as list[0] */
+ Py_INCREF(self);
+ PyList_SET_ITEM(list, 0, (PyObject *)self);
+ count++;
+ break;
+ }
+ SPLIT_ADD(s, j, i);
+ }
+
+ if (i < len) {
+ /* Only occurs when maxsplit was reached */
+ /* Skip any remaining whitespace and copy to end of string */
+ SKIP_SPACE(s, i, len);
+ if (i != len)
+ SPLIT_ADD(s, i, len);
+ }
+ FIX_PREALLOC_SIZE(list);
+ return list;
onError:
- Py_DECREF(list);
- return NULL;
+ Py_DECREF(list);
+ return NULL;
}
Py_LOCAL_INLINE(PyObject *)
split_char(PyBytesObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount)
{
- const char *s = PyBytes_AS_STRING(self);
- register Py_ssize_t i, j, count=0;
- PyObject *str;
- PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
-
- if (list == NULL)
- return NULL;
-
- i = j = 0;
- while ((j < len) && (maxcount-- > 0)) {
- for(; j<len; j++) {
- /* I found that using memchr makes no difference */
- if (s[j] == ch) {
- SPLIT_ADD(s, i, j);
- i = j = j + 1;
- break;
- }
- }
- }
- if (i == 0 && count == 0 && PyBytes_CheckExact(self)) {
- /* ch not in self, so just use self as list[0] */
- Py_INCREF(self);
- PyList_SET_ITEM(list, 0, (PyObject *)self);
- count++;
- }
- else if (i <= len) {
- SPLIT_ADD(s, i, len);
- }
- FIX_PREALLOC_SIZE(list);
- return list;
+ const char *s = PyBytes_AS_STRING(self);
+ register Py_ssize_t i, j, count=0;
+ PyObject *str;
+ PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
+
+ if (list == NULL)
+ return NULL;
+
+ i = j = 0;
+ while ((j < len) && (maxcount-- > 0)) {
+ for(; j<len; j++) {
+ /* I found that using memchr makes no difference */
+ if (s[j] == ch) {
+ SPLIT_ADD(s, i, j);
+ i = j = j + 1;
+ break;
+ }
+ }
+ }
+ if (i == 0 && count == 0 && PyBytes_CheckExact(self)) {
+ /* ch not in self, so just use self as list[0] */
+ Py_INCREF(self);
+ PyList_SET_ITEM(list, 0, (PyObject *)self);
+ count++;
+ }
+ else if (i <= len) {
+ SPLIT_ADD(s, i, len);
+ }
+ FIX_PREALLOC_SIZE(list);
+ return list;
onError:
- Py_DECREF(list);
- return NULL;
+ Py_DECREF(list);
+ return NULL;
}
PyDoc_STRVAR(split__doc__,
@@ -1152,74 +1152,74 @@ If maxsplit is given, at most maxsplit splits are done.");
static PyObject *
bytes_split(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
- Py_ssize_t maxsplit = -1, count=0;
- const char *s = PyBytes_AS_STRING(self), *sub;
- Py_buffer vsub;
- PyObject *list, *str, *subobj = Py_None;
+ Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
+ Py_ssize_t maxsplit = -1, count=0;
+ const char *s = PyBytes_AS_STRING(self), *sub;
+ Py_buffer vsub;
+ PyObject *list, *str, *subobj = Py_None;
#ifdef USE_FAST
- Py_ssize_t pos;
+ Py_ssize_t pos;
#endif
- if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
- return NULL;
- if (maxsplit < 0)
- maxsplit = PY_SSIZE_T_MAX;
- if (subobj == Py_None)
- return split_whitespace(self, len, maxsplit);
- if (_getbuffer(subobj, &vsub) < 0)
- return NULL;
- sub = vsub.buf;
- n = vsub.len;
-
- if (n == 0) {
- PyErr_SetString(PyExc_ValueError, "empty separator");
- PyBuffer_Release(&vsub);
- return NULL;
- }
- else if (n == 1) {
- list = split_char(self, len, sub[0], maxsplit);
- PyBuffer_Release(&vsub);
- return list;
- }
-
- list = PyList_New(PREALLOC_SIZE(maxsplit));
- if (list == NULL) {
- PyBuffer_Release(&vsub);
- return NULL;
- }
+ if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
+ return NULL;
+ if (maxsplit < 0)
+ maxsplit = PY_SSIZE_T_MAX;
+ if (subobj == Py_None)
+ return split_whitespace(self, len, maxsplit);
+ if (_getbuffer(subobj, &vsub) < 0)
+ return NULL;
+ sub = vsub.buf;
+ n = vsub.len;
+
+ if (n == 0) {
+ PyErr_SetString(PyExc_ValueError, "empty separator");
+ PyBuffer_Release(&vsub);
+ return NULL;
+ }
+ else if (n == 1) {
+ list = split_char(self, len, sub[0], maxsplit);
+ PyBuffer_Release(&vsub);
+ return list;
+ }
+
+ list = PyList_New(PREALLOC_SIZE(maxsplit));
+ if (list == NULL) {
+ PyBuffer_Release(&vsub);
+ return NULL;
+ }
#ifdef USE_FAST
- i = j = 0;
- while (maxsplit-- > 0) {
- pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
- if (pos < 0)
- break;
- j = i+pos;
- SPLIT_ADD(s, i, j);
- i = j + n;
- }
+ i = j = 0;
+ while (maxsplit-- > 0) {
+ pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
+ if (pos < 0)
+ break;
+ j = i+pos;
+ SPLIT_ADD(s, i, j);
+ i = j + n;
+ }
#else
- i = j = 0;
- while ((j+n <= len) && (maxsplit-- > 0)) {
- for (; j+n <= len; j++) {
- if (Py_STRING_MATCH(s, j, sub, n)) {
- SPLIT_ADD(s, i, j);
- i = j = j + n;
- break;
- }
- }
- }
+ i = j = 0;
+ while ((j+n <= len) && (maxsplit-- > 0)) {
+ for (; j+n <= len; j++) {
+ if (Py_STRING_MATCH(s, j, sub, n)) {
+ SPLIT_ADD(s, i, j);
+ i = j = j + n;
+ break;
+ }
+ }
+ }
#endif
- SPLIT_ADD(s, i, len);
- FIX_PREALLOC_SIZE(list);
- PyBuffer_Release(&vsub);
- return list;
+ SPLIT_ADD(s, i, len);
+ FIX_PREALLOC_SIZE(list);
+ PyBuffer_Release(&vsub);
+ return list;
onError:
- Py_DECREF(list);
- PyBuffer_Release(&vsub);
- return NULL;
+ Py_DECREF(list);
+ PyBuffer_Release(&vsub);
+ return NULL;
}
PyDoc_STRVAR(partition__doc__,
@@ -1232,21 +1232,21 @@ found, returns B and two empty bytes objects.");
static PyObject *
bytes_partition(PyBytesObject *self, PyObject *sep_obj)
{
- const char *sep;
- Py_ssize_t sep_len;
+ const char *sep;
+ Py_ssize_t sep_len;
- if (PyBytes_Check(sep_obj)) {
- sep = PyBytes_AS_STRING(sep_obj);
- sep_len = PyBytes_GET_SIZE(sep_obj);
- }
- else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
- return NULL;
+ if (PyBytes_Check(sep_obj)) {
+ sep = PyBytes_AS_STRING(sep_obj);
+ sep_len = PyBytes_GET_SIZE(sep_obj);
+ }
+ else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
+ return NULL;
- return stringlib_partition(
- (PyObject*) self,
- PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
- sep_obj, sep, sep_len
- );
+ return stringlib_partition(
+ (PyObject*) self,
+ PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
+ sep_obj, sep, sep_len
+ );
}
PyDoc_STRVAR(rpartition__doc__,
@@ -1260,105 +1260,105 @@ bytes objects and B.");
static PyObject *
bytes_rpartition(PyBytesObject *self, PyObject *sep_obj)
{
- const char *sep;
- Py_ssize_t sep_len;
+ const char *sep;
+ Py_ssize_t sep_len;
- if (PyBytes_Check(sep_obj)) {
- sep = PyBytes_AS_STRING(sep_obj);
- sep_len = PyBytes_GET_SIZE(sep_obj);
- }
- else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
- return NULL;
+ if (PyBytes_Check(sep_obj)) {
+ sep = PyBytes_AS_STRING(sep_obj);
+ sep_len = PyBytes_GET_SIZE(sep_obj);
+ }
+ else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
+ return NULL;
- return stringlib_rpartition(
- (PyObject*) self,
- PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
- sep_obj, sep, sep_len
- );
+ return stringlib_rpartition(
+ (PyObject*) self,
+ PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
+ sep_obj, sep, sep_len
+ );
}
Py_LOCAL_INLINE(PyObject *)
rsplit_whitespace(PyBytesObject *self, Py_ssize_t len, Py_ssize_t maxsplit)
{
- const char *s = PyBytes_AS_STRING(self);
- Py_ssize_t i, j, count=0;
- PyObject *str;
- PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
-
- if (list == NULL)
- return NULL;
-
- i = j = len-1;
-
- while (maxsplit-- > 0) {
- RSKIP_SPACE(s, i);
- if (i<0) break;
- j = i; i--;
- RSKIP_NONSPACE(s, i);
- if (j == len-1 && i < 0 && PyBytes_CheckExact(self)) {
- /* No whitespace in self, so just use it as list[0] */
- Py_INCREF(self);
- PyList_SET_ITEM(list, 0, (PyObject *)self);
- count++;
- break;
- }
- SPLIT_ADD(s, i + 1, j + 1);
- }
- if (i >= 0) {
- /* Only occurs when maxsplit was reached. Skip any remaining
- whitespace and copy to beginning of string. */
- RSKIP_SPACE(s, i);
- if (i >= 0)
- SPLIT_ADD(s, 0, i + 1);
-
- }
- FIX_PREALLOC_SIZE(list);
- if (PyList_Reverse(list) < 0)
- goto onError;
- return list;
+ const char *s = PyBytes_AS_STRING(self);
+ Py_ssize_t i, j, count=0;
+ PyObject *str;
+ PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
+
+ if (list == NULL)
+ return NULL;
+
+ i = j = len-1;
+
+ while (maxsplit-- > 0) {
+ RSKIP_SPACE(s, i);
+ if (i<0) break;
+ j = i; i--;
+ RSKIP_NONSPACE(s, i);
+ if (j == len-1 && i < 0 && PyBytes_CheckExact(self)) {
+ /* No whitespace in self, so just use it as list[0] */
+ Py_INCREF(self);
+ PyList_SET_ITEM(list, 0, (PyObject *)self);
+ count++;
+ break;
+ }
+ SPLIT_ADD(s, i + 1, j + 1);
+ }
+ if (i >= 0) {
+ /* Only occurs when maxsplit was reached. Skip any remaining
+ whitespace and copy to beginning of string. */
+ RSKIP_SPACE(s, i);
+ if (i >= 0)
+ SPLIT_ADD(s, 0, i + 1);
+
+ }
+ FIX_PREALLOC_SIZE(list);
+ if (PyList_Reverse(list) < 0)
+ goto onError;
+ return list;
onError:
- Py_DECREF(list);
- return NULL;
+ Py_DECREF(list);
+ return NULL;
}
Py_LOCAL_INLINE(PyObject *)
rsplit_char(PyBytesObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount)
{
- const char *s = PyBytes_AS_STRING(self);
- register Py_ssize_t i, j, count=0;
- PyObject *str;
- PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
-
- if (list == NULL)
- return NULL;
-
- i = j = len - 1;
- while ((i >= 0) && (maxcount-- > 0)) {
- for (; i >= 0; i--) {
- if (s[i] == ch) {
- SPLIT_ADD(s, i + 1, j + 1);
- j = i = i - 1;
- break;
- }
- }
- }
- if (i < 0 && count == 0 && PyBytes_CheckExact(self)) {
- /* ch not in self, so just use self as list[0] */
- Py_INCREF(self);
- PyList_SET_ITEM(list, 0, (PyObject *)self);
- count++;
- }
- else if (j >= -1) {
- SPLIT_ADD(s, 0, j + 1);
- }
- FIX_PREALLOC_SIZE(list);
- if (PyList_Reverse(list) < 0)
- goto onError;
- return list;
+ const char *s = PyBytes_AS_STRING(self);
+ register Py_ssize_t i, j, count=0;
+ PyObject *str;
+ PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
+
+ if (list == NULL)
+ return NULL;
+
+ i = j = len - 1;
+ while ((i >= 0) && (maxcount-- > 0)) {
+ for (; i >= 0; i--) {
+ if (s[i] == ch) {
+ SPLIT_ADD(s, i + 1, j + 1);
+ j = i = i - 1;
+ break;
+ }
+ }
+ }
+ if (i < 0 && count == 0 && PyBytes_CheckExact(self)) {
+ /* ch not in self, so just use self as list[0] */
+ Py_INCREF(self);
+ PyList_SET_ITEM(list, 0, (PyObject *)self);
+ count++;
+ }
+ else if (j >= -1) {
+ SPLIT_ADD(s, 0, j + 1);
+ }
+ FIX_PREALLOC_SIZE(list);
+ if (PyList_Reverse(list) < 0)
+ goto onError;
+ return list;
onError:
- Py_DECREF(list);
- return NULL;
+ Py_DECREF(list);
+ return NULL;
}
PyDoc_STRVAR(rsplit__doc__,
@@ -1374,65 +1374,65 @@ If maxsplit is given, at most maxsplit splits are done.");
static PyObject *
bytes_rsplit(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
- Py_ssize_t maxsplit = -1, count=0;
- const char *s, *sub;
- Py_buffer vsub;
- PyObject *list, *str, *subobj = Py_None;
-
- if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
- return NULL;
- if (maxsplit < 0)
- maxsplit = PY_SSIZE_T_MAX;
- if (subobj == Py_None)
- return rsplit_whitespace(self, len, maxsplit);
- if (_getbuffer(subobj, &vsub) < 0)
- return NULL;
- sub = vsub.buf;
- n = vsub.len;
-
- if (n == 0) {
- PyErr_SetString(PyExc_ValueError, "empty separator");
- PyBuffer_Release(&vsub);
- return NULL;
- }
- else if (n == 1) {
- list = rsplit_char(self, len, sub[0], maxsplit);
- PyBuffer_Release(&vsub);
- return list;
- }
-
- list = PyList_New(PREALLOC_SIZE(maxsplit));
- if (list == NULL) {
- PyBuffer_Release(&vsub);
- return NULL;
- }
-
- j = len;
- i = j - n;
-
- s = PyBytes_AS_STRING(self);
- while ( (i >= 0) && (maxsplit-- > 0) ) {
- for (; i>=0; i--) {
- if (Py_STRING_MATCH(s, i, sub, n)) {
- SPLIT_ADD(s, i + n, j);
- j = i;
- i -= n;
- break;
- }
- }
- }
- SPLIT_ADD(s, 0, j);
- FIX_PREALLOC_SIZE(list);
- if (PyList_Reverse(list) < 0)
- goto onError;
- PyBuffer_Release(&vsub);
- return list;
+ Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
+ Py_ssize_t maxsplit = -1, count=0;
+ const char *s, *sub;
+ Py_buffer vsub;
+ PyObject *list, *str, *subobj = Py_None;
+
+ if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
+ return NULL;
+ if (maxsplit < 0)
+ maxsplit = PY_SSIZE_T_MAX;
+ if (subobj == Py_None)
+ return rsplit_whitespace(self, len, maxsplit);
+ if (_getbuffer(subobj, &vsub) < 0)
+ return NULL;
+ sub = vsub.buf;
+ n = vsub.len;
+
+ if (n == 0) {
+ PyErr_SetString(PyExc_ValueError, "empty separator");
+ PyBuffer_Release(&vsub);
+ return NULL;
+ }
+ else if (n == 1) {
+ list = rsplit_char(self, len, sub[0], maxsplit);
+ PyBuffer_Release(&vsub);
+ return list;
+ }
+
+ list = PyList_New(PREALLOC_SIZE(maxsplit));
+ if (list == NULL) {
+ PyBuffer_Release(&vsub);
+ return NULL;
+ }
+
+ j = len;
+ i = j - n;
+
+ s = PyBytes_AS_STRING(self);
+ while ( (i >= 0) && (maxsplit-- > 0) ) {
+ for (; i>=0; i--) {
+ if (Py_STRING_MATCH(s, i, sub, n)) {
+ SPLIT_ADD(s, i + n, j);
+ j = i;
+ i -= n;
+ break;
+ }
+ }
+ }
+ SPLIT_ADD(s, 0, j);
+ FIX_PREALLOC_SIZE(list);
+ if (PyList_Reverse(list) < 0)
+ goto onError;
+ PyBuffer_Release(&vsub);
+ return list;
onError:
- Py_DECREF(list);
- PyBuffer_Release(&vsub);
- return NULL;
+ Py_DECREF(list);
+ PyBuffer_Release(&vsub);
+ return NULL;
}
#undef SPLIT_ADD
@@ -1449,156 +1449,156 @@ Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'.");
static PyObject *
bytes_join(PyObject *self, PyObject *orig)
{
- char *sep = PyBytes_AS_STRING(self);
- const Py_ssize_t seplen = PyBytes_GET_SIZE(self);
- PyObject *res = NULL;
- char *p;
- Py_ssize_t seqlen = 0;
- size_t sz = 0;
- Py_ssize_t i;
- PyObject *seq, *item;
-
- seq = PySequence_Fast(orig, "");
- if (seq == NULL) {
- return NULL;
- }
-
- seqlen = PySequence_Size(seq);
- if (seqlen == 0) {
- Py_DECREF(seq);
- return PyBytes_FromString("");
- }
- if (seqlen == 1) {
- item = PySequence_Fast_GET_ITEM(seq, 0);
- if (PyBytes_CheckExact(item)) {
- Py_INCREF(item);
- Py_DECREF(seq);
- return item;
- }
- }
-
- /* There are at least two things to join, or else we have a subclass
- * of the builtin types in the sequence.
- * Do a pre-pass to figure out the total amount of space we'll
- * need (sz), and see whether all argument are bytes.
- */
- /* XXX Shouldn't we use _getbuffer() on these items instead? */
- for (i = 0; i < seqlen; i++) {
- const size_t old_sz = sz;
- item = PySequence_Fast_GET_ITEM(seq, i);
- if (!PyBytes_Check(item) && !PyByteArray_Check(item)) {
- PyErr_Format(PyExc_TypeError,
- "sequence item %zd: expected bytes,"
- " %.80s found",
- i, Py_TYPE(item)->tp_name);
- Py_DECREF(seq);
- return NULL;
- }
- sz += Py_SIZE(item);
- if (i != 0)
- sz += seplen;
- if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "join() result is too long for bytes");
- Py_DECREF(seq);
- return NULL;
- }
- }
-
- /* Allocate result space. */
- res = PyBytes_FromStringAndSize((char*)NULL, sz);
- if (res == NULL) {
- Py_DECREF(seq);
- return NULL;
- }
-
- /* Catenate everything. */
- /* I'm not worried about a PyByteArray item growing because there's
- nowhere in this function where we release the GIL. */
- p = PyBytes_AS_STRING(res);
- for (i = 0; i < seqlen; ++i) {
- size_t n;
- char *q;
- if (i) {
- Py_MEMCPY(p, sep, seplen);
- p += seplen;
- }
- item = PySequence_Fast_GET_ITEM(seq, i);
- n = Py_SIZE(item);
- if (PyBytes_Check(item))
- q = PyBytes_AS_STRING(item);
- else
- q = PyByteArray_AS_STRING(item);
- Py_MEMCPY(p, q, n);
- p += n;
- }
-
- Py_DECREF(seq);
- return res;
+ char *sep = PyBytes_AS_STRING(self);
+ const Py_ssize_t seplen = PyBytes_GET_SIZE(self);
+ PyObject *res = NULL;
+ char *p;
+ Py_ssize_t seqlen = 0;
+ size_t sz = 0;
+ Py_ssize_t i;
+ PyObject *seq, *item;
+
+ seq = PySequence_Fast(orig, "");
+ if (seq == NULL) {
+ return NULL;
+ }
+
+ seqlen = PySequence_Size(seq);
+ if (seqlen == 0) {
+ Py_DECREF(seq);
+ return PyBytes_FromString("");
+ }
+ if (seqlen == 1) {
+ item = PySequence_Fast_GET_ITEM(seq, 0);
+ if (PyBytes_CheckExact(item)) {
+ Py_INCREF(item);
+ Py_DECREF(seq);
+ return item;
+ }
+ }
+
+ /* There are at least two things to join, or else we have a subclass
+ * of the builtin types in the sequence.
+ * Do a pre-pass to figure out the total amount of space we'll
+ * need (sz), and see whether all argument are bytes.
+ */
+ /* XXX Shouldn't we use _getbuffer() on these items instead? */
+ for (i = 0; i < seqlen; i++) {
+ const size_t old_sz = sz;
+ item = PySequence_Fast_GET_ITEM(seq, i);
+ if (!PyBytes_Check(item) && !PyByteArray_Check(item)) {
+ PyErr_Format(PyExc_TypeError,
+ "sequence item %zd: expected bytes,"
+ " %.80s found",
+ i, Py_TYPE(item)->tp_name);
+ Py_DECREF(seq);
+ return NULL;
+ }
+ sz += Py_SIZE(item);
+ if (i != 0)
+ sz += seplen;
+ if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "join() result is too long for bytes");
+ Py_DECREF(seq);
+ return NULL;
+ }
+ }
+
+ /* Allocate result space. */
+ res = PyBytes_FromStringAndSize((char*)NULL, sz);
+ if (res == NULL) {
+ Py_DECREF(seq);
+ return NULL;
+ }
+
+ /* Catenate everything. */
+ /* I'm not worried about a PyByteArray item growing because there's
+ nowhere in this function where we release the GIL. */
+ p = PyBytes_AS_STRING(res);
+ for (i = 0; i < seqlen; ++i) {
+ size_t n;
+ char *q;
+ if (i) {
+ Py_MEMCPY(p, sep, seplen);
+ p += seplen;
+ }
+ item = PySequence_Fast_GET_ITEM(seq, i);
+ n = Py_SIZE(item);
+ if (PyBytes_Check(item))
+ q = PyBytes_AS_STRING(item);
+ else
+ q = PyByteArray_AS_STRING(item);
+ Py_MEMCPY(p, q, n);
+ p += n;
+ }
+
+ Py_DECREF(seq);
+ return res;
}
PyObject *
_PyBytes_Join(PyObject *sep, PyObject *x)
{
- assert(sep != NULL && PyBytes_Check(sep));
- assert(x != NULL);
- return bytes_join(sep, x);
+ assert(sep != NULL && PyBytes_Check(sep));
+ assert(x != NULL);
+ return bytes_join(sep, x);
}
Py_LOCAL_INLINE(void)
bytes_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
{
- if (*end > len)
- *end = len;
- else if (*end < 0)
- *end += len;
- if (*end < 0)
- *end = 0;
- if (*start < 0)
- *start += len;
- if (*start < 0)
- *start = 0;
+ if (*end > len)
+ *end = len;
+ else if (*end < 0)
+ *end += len;
+ if (*end < 0)
+ *end = 0;
+ if (*start < 0)
+ *start += len;
+ if (*start < 0)
+ *start = 0;
}
Py_LOCAL_INLINE(Py_ssize_t)
bytes_find_internal(PyBytesObject *self, PyObject *args, int dir)
{
- PyObject *subobj;
- const char *sub;
- Py_ssize_t sub_len;
- Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
- PyObject *obj_start=Py_None, *obj_end=Py_None;
-
- if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj,
- &obj_start, &obj_end))
- return -2;
- /* To support None in "start" and "end" arguments, meaning
- the same as if they were not passed.
- */
- if (obj_start != Py_None)
- if (!_PyEval_SliceIndex(obj_start, &start))
- return -2;
- if (obj_end != Py_None)
- if (!_PyEval_SliceIndex(obj_end, &end))
- return -2;
-
- if (PyBytes_Check(subobj)) {
- sub = PyBytes_AS_STRING(subobj);
- sub_len = PyBytes_GET_SIZE(subobj);
- }
- else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
- /* XXX - the "expected a character buffer object" is pretty
- confusing for a non-expert. remap to something else ? */
- return -2;
-
- if (dir > 0)
- return stringlib_find_slice(
- PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
- sub, sub_len, start, end);
- else
- return stringlib_rfind_slice(
- PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
- sub, sub_len, start, end);
+ PyObject *subobj;
+ const char *sub;
+ Py_ssize_t sub_len;
+ Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
+ PyObject *obj_start=Py_None, *obj_end=Py_None;
+
+ if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj,
+ &obj_start, &obj_end))
+ return -2;
+ /* To support None in "start" and "end" arguments, meaning
+ the same as if they were not passed.
+ */
+ if (obj_start != Py_None)
+ if (!_PyEval_SliceIndex(obj_start, &start))
+ return -2;
+ if (obj_end != Py_None)
+ if (!_PyEval_SliceIndex(obj_end, &end))
+ return -2;
+
+ if (PyBytes_Check(subobj)) {
+ sub = PyBytes_AS_STRING(subobj);
+ sub_len = PyBytes_GET_SIZE(subobj);
+ }
+ else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
+ /* XXX - the "expected a character buffer object" is pretty
+ confusing for a non-expert. remap to something else ? */
+ return -2;
+
+ if (dir > 0)
+ return stringlib_find_slice(
+ PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
+ sub, sub_len, start, end);
+ else
+ return stringlib_rfind_slice(
+ PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
+ sub, sub_len, start, end);
}
@@ -1614,10 +1614,10 @@ Return -1 on failure.");
static PyObject *
bytes_find(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t result = bytes_find_internal(self, args, +1);
- if (result == -2)
- return NULL;
- return PyLong_FromSsize_t(result);
+ Py_ssize_t result = bytes_find_internal(self, args, +1);
+ if (result == -2)
+ return NULL;
+ return PyLong_FromSsize_t(result);
}
@@ -1629,15 +1629,15 @@ Like B.find() but raise ValueError when the substring is not found.");
static PyObject *
bytes_index(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t result = bytes_find_internal(self, args, +1);
- if (result == -2)
- return NULL;
- if (result == -1) {
- PyErr_SetString(PyExc_ValueError,
- "substring not found");
- return NULL;
- }
- return PyLong_FromSsize_t(result);
+ Py_ssize_t result = bytes_find_internal(self, args, +1);
+ if (result == -2)
+ return NULL;
+ if (result == -1) {
+ PyErr_SetString(PyExc_ValueError,
+ "substring not found");
+ return NULL;
+ }
+ return PyLong_FromSsize_t(result);
}
@@ -1653,10 +1653,10 @@ Return -1 on failure.");
static PyObject *
bytes_rfind(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t result = bytes_find_internal(self, args, -1);
- if (result == -2)
- return NULL;
- return PyLong_FromSsize_t(result);
+ Py_ssize_t result = bytes_find_internal(self, args, -1);
+ if (result == -2)
+ return NULL;
+ return PyLong_FromSsize_t(result);
}
@@ -1668,101 +1668,101 @@ Like B.rfind() but raise ValueError when the substring is not found.");
static PyObject *
bytes_rindex(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t result = bytes_find_internal(self, args, -1);
- if (result == -2)
- return NULL;
- if (result == -1) {
- PyErr_SetString(PyExc_ValueError,
- "substring not found");
- return NULL;
- }
- return PyLong_FromSsize_t(result);
+ Py_ssize_t result = bytes_find_internal(self, args, -1);
+ if (result == -2)
+ return NULL;
+ if (result == -1) {
+ PyErr_SetString(PyExc_ValueError,
+ "substring not found");
+ return NULL;
+ }
+ return PyLong_FromSsize_t(result);
}
Py_LOCAL_INLINE(PyObject *)
do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
{
- Py_buffer vsep;
- char *s = PyBytes_AS_STRING(self);
- Py_ssize_t len = PyBytes_GET_SIZE(self);
- char *sep;
- Py_ssize_t seplen;
- Py_ssize_t i, j;
-
- if (_getbuffer(sepobj, &vsep) < 0)
- return NULL;
- sep = vsep.buf;
- seplen = vsep.len;
-
- i = 0;
- if (striptype != RIGHTSTRIP) {
- while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
- i++;
- }
- }
+ Py_buffer vsep;
+ char *s = PyBytes_AS_STRING(self);
+ Py_ssize_t len = PyBytes_GET_SIZE(self);
+ char *sep;
+ Py_ssize_t seplen;
+ Py_ssize_t i, j;
+
+ if (_getbuffer(sepobj, &vsep) < 0)
+ return NULL;
+ sep = vsep.buf;
+ seplen = vsep.len;
+
+ i = 0;
+ if (striptype != RIGHTSTRIP) {
+ while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
+ i++;
+ }
+ }
- j = len;
- if (striptype != LEFTSTRIP) {
- do {
- j--;
- } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
- j++;
- }
+ j = len;
+ if (striptype != LEFTSTRIP) {
+ do {
+ j--;
+ } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
+ j++;
+ }
- PyBuffer_Release(&vsep);
+ PyBuffer_Release(&vsep);
- if (i == 0 && j == len && PyBytes_CheckExact(self)) {
- Py_INCREF(self);
- return (PyObject*)self;
- }
- else
- return PyBytes_FromStringAndSize(s+i, j-i);
+ if (i == 0 && j == len && PyBytes_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject*)self;
+ }
+ else
+ return PyBytes_FromStringAndSize(s+i, j-i);
}
Py_LOCAL_INLINE(PyObject *)
do_strip(PyBytesObject *self, int striptype)
{
- char *s = PyBytes_AS_STRING(self);
- Py_ssize_t len = PyBytes_GET_SIZE(self), i, j;
+ char *s = PyBytes_AS_STRING(self);
+ Py_ssize_t len = PyBytes_GET_SIZE(self), i, j;
- i = 0;
- if (striptype != RIGHTSTRIP) {
- while (i < len && ISSPACE(s[i])) {
- i++;
- }
- }
+ i = 0;
+ if (striptype != RIGHTSTRIP) {
+ while (i < len && ISSPACE(s[i])) {
+ i++;
+ }
+ }
- j = len;
- if (striptype != LEFTSTRIP) {
- do {
- j--;
- } while (j >= i && ISSPACE(s[j]));
- j++;
- }
+ j = len;
+ if (striptype != LEFTSTRIP) {
+ do {
+ j--;
+ } while (j >= i && ISSPACE(s[j]));
+ j++;
+ }
- if (i == 0 && j == len && PyBytes_CheckExact(self)) {
- Py_INCREF(self);
- return (PyObject*)self;
- }
- else
- return PyBytes_FromStringAndSize(s+i, j-i);
+ if (i == 0 && j == len && PyBytes_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject*)self;
+ }
+ else
+ return PyBytes_FromStringAndSize(s+i, j-i);
}
Py_LOCAL_INLINE(PyObject *)
do_argstrip(PyBytesObject *self, int striptype, PyObject *args)
{
- PyObject *sep = NULL;
+ PyObject *sep = NULL;
- if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
- return NULL;
+ if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
+ return NULL;
- if (sep != NULL && sep != Py_None) {
- return do_xstrip(self, striptype, sep);
- }
- return do_strip(self, striptype);
+ if (sep != NULL && sep != Py_None) {
+ return do_xstrip(self, striptype, sep);
+ }
+ return do_strip(self, striptype);
}
@@ -1774,10 +1774,10 @@ If the argument is omitted, strip trailing ASCII whitespace.");
static PyObject *
bytes_strip(PyBytesObject *self, PyObject *args)
{
- if (PyTuple_GET_SIZE(args) == 0)
- return do_strip(self, BOTHSTRIP); /* Common case */
- else
- return do_argstrip(self, BOTHSTRIP, args);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return do_strip(self, BOTHSTRIP); /* Common case */
+ else
+ return do_argstrip(self, BOTHSTRIP, args);
}
@@ -1789,10 +1789,10 @@ If the argument is omitted, strip leading ASCII whitespace.");
static PyObject *
bytes_lstrip(PyBytesObject *self, PyObject *args)
{
- if (PyTuple_GET_SIZE(args) == 0)
- return do_strip(self, LEFTSTRIP); /* Common case */
- else
- return do_argstrip(self, LEFTSTRIP, args);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return do_strip(self, LEFTSTRIP); /* Common case */
+ else
+ return do_argstrip(self, LEFTSTRIP, args);
}
@@ -1804,10 +1804,10 @@ If the argument is omitted, strip trailing ASCII whitespace.");
static PyObject *
bytes_rstrip(PyBytesObject *self, PyObject *args)
{
- if (PyTuple_GET_SIZE(args) == 0)
- return do_strip(self, RIGHTSTRIP); /* Common case */
- else
- return do_argstrip(self, RIGHTSTRIP, args);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return do_strip(self, RIGHTSTRIP); /* Common case */
+ else
+ return do_argstrip(self, RIGHTSTRIP, args);
}
@@ -1821,27 +1821,27 @@ as in slice notation.");
static PyObject *
bytes_count(PyBytesObject *self, PyObject *args)
{
- PyObject *sub_obj;
- const char *str = PyBytes_AS_STRING(self), *sub;
- Py_ssize_t sub_len;
- Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
+ PyObject *sub_obj;
+ const char *str = PyBytes_AS_STRING(self), *sub;
+ Py_ssize_t sub_len;
+ Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
- if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
- _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
- return NULL;
+ if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
+ _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
+ return NULL;
- if (PyBytes_Check(sub_obj)) {
- sub = PyBytes_AS_STRING(sub_obj);
- sub_len = PyBytes_GET_SIZE(sub_obj);
- }
- else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
- return NULL;
+ if (PyBytes_Check(sub_obj)) {
+ sub = PyBytes_AS_STRING(sub_obj);
+ sub_len = PyBytes_GET_SIZE(sub_obj);
+ }
+ else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
+ return NULL;
- bytes_adjust_indices(&start, &end, PyBytes_GET_SIZE(self));
+ bytes_adjust_indices(&start, &end, PyBytes_GET_SIZE(self));
- return PyLong_FromSsize_t(
- stringlib_count(str + start, end - start, sub, sub_len)
- );
+ return PyLong_FromSsize_t(
+ stringlib_count(str + start, end - start, sub, sub_len)
+ );
}
@@ -1856,105 +1856,105 @@ table, which must be a bytes object of length 256.");
static PyObject *
bytes_translate(PyBytesObject *self, PyObject *args)
{
- register char *input, *output;
- const char *table;
- register Py_ssize_t i, c, changed = 0;
- PyObject *input_obj = (PyObject*)self;
- const char *output_start, *del_table=NULL;
- Py_ssize_t inlen, tablen, dellen = 0;
- PyObject *result;
- int trans_table[256];
- PyObject *tableobj, *delobj = NULL;
-
- if (!PyArg_UnpackTuple(args, "translate", 1, 2,
- &tableobj, &delobj))
- return NULL;
-
- if (PyBytes_Check(tableobj)) {
- table = PyBytes_AS_STRING(tableobj);
- tablen = PyBytes_GET_SIZE(tableobj);
- }
- else if (tableobj == Py_None) {
- table = NULL;
- tablen = 256;
- }
- else if (PyObject_AsCharBuffer(tableobj, &table, &tablen))
- return NULL;
-
- if (tablen != 256) {
- PyErr_SetString(PyExc_ValueError,
- "translation table must be 256 characters long");
- return NULL;
- }
-
- if (delobj != NULL) {
- if (PyBytes_Check(delobj)) {
- del_table = PyBytes_AS_STRING(delobj);
- dellen = PyBytes_GET_SIZE(delobj);
- }
- else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
- return NULL;
- }
- else {
- del_table = NULL;
- dellen = 0;
- }
-
- inlen = PyBytes_GET_SIZE(input_obj);
- result = PyBytes_FromStringAndSize((char *)NULL, inlen);
- if (result == NULL)
- return NULL;
- output_start = output = PyBytes_AsString(result);
- input = PyBytes_AS_STRING(input_obj);
-
- if (dellen == 0 && table != NULL) {
- /* If no deletions are required, use faster code */
- for (i = inlen; --i >= 0; ) {
- c = Py_CHARMASK(*input++);
- if (Py_CHARMASK((*output++ = table[c])) != c)
- changed = 1;
- }
- if (changed || !PyBytes_CheckExact(input_obj))
- return result;
- Py_DECREF(result);
- Py_INCREF(input_obj);
- return input_obj;
- }
-
- if (table == NULL) {
- for (i = 0; i < 256; i++)
- trans_table[i] = Py_CHARMASK(i);
- } else {
- for (i = 0; i < 256; i++)
- trans_table[i] = Py_CHARMASK(table[i]);
- }
-
- for (i = 0; i < dellen; i++)
- trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
-
- for (i = inlen; --i >= 0; ) {
- c = Py_CHARMASK(*input++);
- if (trans_table[c] != -1)
- if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
- continue;
- changed = 1;
- }
- if (!changed && PyBytes_CheckExact(input_obj)) {
- Py_DECREF(result);
- Py_INCREF(input_obj);
- return input_obj;
- }
- /* Fix the size of the resulting string */
- if (inlen > 0)
- _PyBytes_Resize(&result, output - output_start);
- return result;
+ register char *input, *output;
+ const char *table;
+ register Py_ssize_t i, c, changed = 0;
+ PyObject *input_obj = (PyObject*)self;
+ const char *output_start, *del_table=NULL;
+ Py_ssize_t inlen, tablen, dellen = 0;
+ PyObject *result;
+ int trans_table[256];
+ PyObject *tableobj, *delobj = NULL;
+
+ if (!PyArg_UnpackTuple(args, "translate", 1, 2,
+ &tableobj, &delobj))
+ return NULL;
+
+ if (PyBytes_Check(tableobj)) {
+ table = PyBytes_AS_STRING(tableobj);
+ tablen = PyBytes_GET_SIZE(tableobj);
+ }
+ else if (tableobj == Py_None) {
+ table = NULL;
+ tablen = 256;
+ }
+ else if (PyObject_AsCharBuffer(tableobj, &table, &tablen))
+ return NULL;
+
+ if (tablen != 256) {
+ PyErr_SetString(PyExc_ValueError,
+ "translation table must be 256 characters long");
+ return NULL;
+ }
+
+ if (delobj != NULL) {
+ if (PyBytes_Check(delobj)) {
+ del_table = PyBytes_AS_STRING(delobj);
+ dellen = PyBytes_GET_SIZE(delobj);
+ }
+ else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
+ return NULL;
+ }
+ else {
+ del_table = NULL;
+ dellen = 0;
+ }
+
+ inlen = PyBytes_GET_SIZE(input_obj);
+ result = PyBytes_FromStringAndSize((char *)NULL, inlen);
+ if (result == NULL)
+ return NULL;
+ output_start = output = PyBytes_AsString(result);
+ input = PyBytes_AS_STRING(input_obj);
+
+ if (dellen == 0 && table != NULL) {
+ /* If no deletions are required, use faster code */
+ for (i = inlen; --i >= 0; ) {
+ c = Py_CHARMASK(*input++);
+ if (Py_CHARMASK((*output++ = table[c])) != c)
+ changed = 1;
+ }
+ if (changed || !PyBytes_CheckExact(input_obj))
+ return result;
+ Py_DECREF(result);
+ Py_INCREF(input_obj);
+ return input_obj;
+ }
+
+ if (table == NULL) {
+ for (i = 0; i < 256; i++)
+ trans_table[i] = Py_CHARMASK(i);
+ } else {
+ for (i = 0; i < 256; i++)
+ trans_table[i] = Py_CHARMASK(table[i]);
+ }
+
+ for (i = 0; i < dellen; i++)
+ trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
+
+ for (i = inlen; --i >= 0; ) {
+ c = Py_CHARMASK(*input++);
+ if (trans_table[c] != -1)
+ if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
+ continue;
+ changed = 1;
+ }
+ if (!changed && PyBytes_CheckExact(input_obj)) {
+ Py_DECREF(result);
+ Py_INCREF(input_obj);
+ return input_obj;
+ }
+ /* Fix the size of the resulting string */
+ if (inlen > 0)
+ _PyBytes_Resize(&result, output - output_start);
+ return result;
}
static PyObject *
bytes_maketrans(PyObject *null, PyObject *args)
{
- return _Py_bytes_maketrans(args);
+ return _Py_bytes_maketrans(args);
}
#define FORWARD 1
@@ -1962,7 +1962,7 @@ bytes_maketrans(PyObject *null, PyObject *args)
/* find and count characters and substrings */
-#define findchar(target, target_len, c) \
+#define findchar(target, target_len, c) \
((char *)memchr((const void *)(target), c, target_len))
/* String ops must return a string. */
@@ -1970,117 +1970,117 @@ bytes_maketrans(PyObject *null, PyObject *args)
Py_LOCAL(PyBytesObject *)
return_self(PyBytesObject *self)
{
- if (PyBytes_CheckExact(self)) {
- Py_INCREF(self);
- return self;
- }
- return (PyBytesObject *)PyBytes_FromStringAndSize(
- PyBytes_AS_STRING(self),
- PyBytes_GET_SIZE(self));
+ if (PyBytes_CheckExact(self)) {
+ Py_INCREF(self);
+ return self;
+ }
+ return (PyBytesObject *)PyBytes_FromStringAndSize(
+ PyBytes_AS_STRING(self),
+ PyBytes_GET_SIZE(self));
}
Py_LOCAL_INLINE(Py_ssize_t)
countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
{
- Py_ssize_t count=0;
- const char *start=target;
- const char *end=target+target_len;
+ Py_ssize_t count=0;
+ const char *start=target;
+ const char *end=target+target_len;
- while ( (start=findchar(start, end-start, c)) != NULL ) {
- count++;
- if (count >= maxcount)
- break;
- start += 1;
- }
- return count;
+ while ( (start=findchar(start, end-start, c)) != NULL ) {
+ count++;
+ if (count >= maxcount)
+ break;
+ start += 1;
+ }
+ return count;
}
Py_LOCAL(Py_ssize_t)
findstring(const char *target, Py_ssize_t target_len,
- const char *pattern, Py_ssize_t pattern_len,
- Py_ssize_t start,
- Py_ssize_t end,
- int direction)
-{
- if (start < 0) {
- start += target_len;
- if (start < 0)
- start = 0;
- }
- if (end > target_len) {
- end = target_len;
- } else if (end < 0) {
- end += target_len;
- if (end < 0)
- end = 0;
- }
-
- /* zero-length substrings always match at the first attempt */
- if (pattern_len == 0)
- return (direction > 0) ? start : end;
-
- end -= pattern_len;
-
- if (direction < 0) {
- for (; end >= start; end--)
- if (Py_STRING_MATCH(target, end, pattern, pattern_len))
- return end;
- } else {
- for (; start <= end; start++)
- if (Py_STRING_MATCH(target, start,pattern,pattern_len))
- return start;
- }
- return -1;
+ const char *pattern, Py_ssize_t pattern_len,
+ Py_ssize_t start,
+ Py_ssize_t end,
+ int direction)
+{
+ if (start < 0) {
+ start += target_len;
+ if (start < 0)
+ start = 0;
+ }
+ if (end > target_len) {
+ end = target_len;
+ } else if (end < 0) {
+ end += target_len;
+ if (end < 0)
+ end = 0;
+ }
+
+ /* zero-length substrings always match at the first attempt */
+ if (pattern_len == 0)
+ return (direction > 0) ? start : end;
+
+ end -= pattern_len;
+
+ if (direction < 0) {
+ for (; end >= start; end--)
+ if (Py_STRING_MATCH(target, end, pattern, pattern_len))
+ return end;
+ } else {
+ for (; start <= end; start++)
+ if (Py_STRING_MATCH(target, start,pattern,pattern_len))
+ return start;
+ }
+ return -1;
}
Py_LOCAL_INLINE(Py_ssize_t)
countstring(const char *target, Py_ssize_t target_len,
- const char *pattern, Py_ssize_t pattern_len,
- Py_ssize_t start,
- Py_ssize_t end,
- int direction, Py_ssize_t maxcount)
-{
- Py_ssize_t count=0;
-
- if (start < 0) {
- start += target_len;
- if (start < 0)
- start = 0;
- }
- if (end > target_len) {
- end = target_len;
- } else if (end < 0) {
- end += target_len;
- if (end < 0)
- end = 0;
- }
-
- /* zero-length substrings match everywhere */
- if (pattern_len == 0 || maxcount == 0) {
- if (target_len+1 < maxcount)
- return target_len+1;
- return maxcount;
- }
-
- end -= pattern_len;
- if (direction < 0) {
- for (; (end >= start); end--)
- if (Py_STRING_MATCH(target, end,pattern,pattern_len)) {
- count++;
- if (--maxcount <= 0) break;
- end -= pattern_len-1;
- }
- } else {
- for (; (start <= end); start++)
- if (Py_STRING_MATCH(target, start,
- pattern, pattern_len)) {
- count++;
- if (--maxcount <= 0)
- break;
- start += pattern_len-1;
- }
- }
- return count;
+ const char *pattern, Py_ssize_t pattern_len,
+ Py_ssize_t start,
+ Py_ssize_t end,
+ int direction, Py_ssize_t maxcount)
+{
+ Py_ssize_t count=0;
+
+ if (start < 0) {
+ start += target_len;
+ if (start < 0)
+ start = 0;
+ }
+ if (end > target_len) {
+ end = target_len;
+ } else if (end < 0) {
+ end += target_len;
+ if (end < 0)
+ end = 0;
+ }
+
+ /* zero-length substrings match everywhere */
+ if (pattern_len == 0 || maxcount == 0) {
+ if (target_len+1 < maxcount)
+ return target_len+1;
+ return maxcount;
+ }
+
+ end -= pattern_len;
+ if (direction < 0) {
+ for (; (end >= start); end--)
+ if (Py_STRING_MATCH(target, end,pattern,pattern_len)) {
+ count++;
+ if (--maxcount <= 0) break;
+ end -= pattern_len-1;
+ }
+ } else {
+ for (; (start <= end); start++)
+ if (Py_STRING_MATCH(target, start,
+ pattern, pattern_len)) {
+ count++;
+ if (--maxcount <= 0)
+ break;
+ start += pattern_len-1;
+ }
+ }
+ return count;
}
@@ -2089,467 +2089,467 @@ countstring(const char *target, Py_ssize_t target_len,
/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_interleave(PyBytesObject *self,
- const char *to_s, Py_ssize_t to_len,
- Py_ssize_t maxcount)
-{
- char *self_s, *result_s;
- Py_ssize_t self_len, result_len;
- Py_ssize_t count, i, product;
- PyBytesObject *result;
-
- self_len = PyBytes_GET_SIZE(self);
-
- /* 1 at the end plus 1 after every character */
- count = self_len+1;
- if (maxcount < count)
- count = maxcount;
-
- /* Check for overflow */
- /* result_len = count * to_len + self_len; */
- product = count * to_len;
- if (product / to_len != count) {
- PyErr_SetString(PyExc_OverflowError,
- "replacement bytes are too long");
- return NULL;
- }
- result_len = product + self_len;
- if (result_len < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "replacement bytes are too long");
- return NULL;
- }
-
- if (! (result = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, result_len)) )
- return NULL;
-
- self_s = PyBytes_AS_STRING(self);
- result_s = PyBytes_AS_STRING(result);
-
- /* TODO: special case single character, which doesn't need memcpy */
-
- /* Lay the first one down (guaranteed this will occur) */
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- count -= 1;
-
- for (i=0; i<count; i++) {
- *result_s++ = *self_s++;
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- }
-
- /* Copy the rest of the original string */
- Py_MEMCPY(result_s, self_s, self_len-i);
-
- return result;
+ const char *to_s, Py_ssize_t to_len,
+ Py_ssize_t maxcount)
+{
+ char *self_s, *result_s;
+ Py_ssize_t self_len, result_len;
+ Py_ssize_t count, i, product;
+ PyBytesObject *result;
+
+ self_len = PyBytes_GET_SIZE(self);
+
+ /* 1 at the end plus 1 after every character */
+ count = self_len+1;
+ if (maxcount < count)
+ count = maxcount;
+
+ /* Check for overflow */
+ /* result_len = count * to_len + self_len; */
+ product = count * to_len;
+ if (product / to_len != count) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacement bytes are too long");
+ return NULL;
+ }
+ result_len = product + self_len;
+ if (result_len < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacement bytes are too long");
+ return NULL;
+ }
+
+ if (! (result = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, result_len)) )
+ return NULL;
+
+ self_s = PyBytes_AS_STRING(self);
+ result_s = PyBytes_AS_STRING(result);
+
+ /* TODO: special case single character, which doesn't need memcpy */
+
+ /* Lay the first one down (guaranteed this will occur) */
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ count -= 1;
+
+ for (i=0; i<count; i++) {
+ *result_s++ = *self_s++;
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ }
+
+ /* Copy the rest of the original string */
+ Py_MEMCPY(result_s, self_s, self_len-i);
+
+ return result;
}
/* Special case for deleting a single character */
/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_delete_single_character(PyBytesObject *self,
- char from_c, Py_ssize_t maxcount)
+ char from_c, Py_ssize_t maxcount)
{
- char *self_s, *result_s;
- char *start, *next, *end;
- Py_ssize_t self_len, result_len;
- Py_ssize_t count;
- PyBytesObject *result;
+ char *self_s, *result_s;
+ char *start, *next, *end;
+ Py_ssize_t self_len, result_len;
+ Py_ssize_t count;
+ PyBytesObject *result;
- self_len = PyBytes_GET_SIZE(self);
- self_s = PyBytes_AS_STRING(self);
+ self_len = PyBytes_GET_SIZE(self);
+ self_s = PyBytes_AS_STRING(self);
- count = countchar(self_s, self_len, from_c, maxcount);
- if (count == 0) {
- return return_self(self);
- }
-
- result_len = self_len - count; /* from_len == 1 */
- assert(result_len>=0);
-
- if ( (result = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
- return NULL;
- result_s = PyBytes_AS_STRING(result);
+ count = countchar(self_s, self_len, from_c, maxcount);
+ if (count == 0) {
+ return return_self(self);
+ }
- start = self_s;
- end = self_s + self_len;
- while (count-- > 0) {
- next = findchar(start, end-start, from_c);
- if (next == NULL)
- break;
- Py_MEMCPY(result_s, start, next-start);
- result_s += (next-start);
- start = next+1;
- }
- Py_MEMCPY(result_s, start, end-start);
+ result_len = self_len - count; /* from_len == 1 */
+ assert(result_len>=0);
+
+ if ( (result = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
+ return NULL;
+ result_s = PyBytes_AS_STRING(result);
+
+ start = self_s;
+ end = self_s + self_len;
+ while (count-- > 0) {
+ next = findchar(start, end-start, from_c);
+ if (next == NULL)
+ break;
+ Py_MEMCPY(result_s, start, next-start);
+ result_s += (next-start);
+ start = next+1;
+ }
+ Py_MEMCPY(result_s, start, end-start);
- return result;
+ return result;
}
/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_delete_substring(PyBytesObject *self,
- const char *from_s, Py_ssize_t from_len,
- Py_ssize_t maxcount) {
- char *self_s, *result_s;
- char *start, *next, *end;
- Py_ssize_t self_len, result_len;
- Py_ssize_t count, offset;
- PyBytesObject *result;
-
- self_len = PyBytes_GET_SIZE(self);
- self_s = PyBytes_AS_STRING(self);
-
- count = countstring(self_s, self_len,
- from_s, from_len,
- 0, self_len, 1,
- maxcount);
-
- if (count == 0) {
- /* no matches */
- return return_self(self);
- }
-
- result_len = self_len - (count * from_len);
- assert (result_len>=0);
-
- if ( (result = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, result_len)) == NULL )
- return NULL;
-
- result_s = PyBytes_AS_STRING(result);
-
- start = self_s;
- end = self_s + self_len;
- while (count-- > 0) {
- offset = findstring(start, end-start,
- from_s, from_len,
- 0, end-start, FORWARD);
- if (offset == -1)
- break;
- next = start + offset;
-
- Py_MEMCPY(result_s, start, next-start);
-
- result_s += (next-start);
- start = next+from_len;
- }
- Py_MEMCPY(result_s, start, end-start);
- return result;
+ const char *from_s, Py_ssize_t from_len,
+ Py_ssize_t maxcount) {
+ char *self_s, *result_s;
+ char *start, *next, *end;
+ Py_ssize_t self_len, result_len;
+ Py_ssize_t count, offset;
+ PyBytesObject *result;
+
+ self_len = PyBytes_GET_SIZE(self);
+ self_s = PyBytes_AS_STRING(self);
+
+ count = countstring(self_s, self_len,
+ from_s, from_len,
+ 0, self_len, 1,
+ maxcount);
+
+ if (count == 0) {
+ /* no matches */
+ return return_self(self);
+ }
+
+ result_len = self_len - (count * from_len);
+ assert (result_len>=0);
+
+ if ( (result = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, result_len)) == NULL )
+ return NULL;
+
+ result_s = PyBytes_AS_STRING(result);
+
+ start = self_s;
+ end = self_s + self_len;
+ while (count-- > 0) {
+ offset = findstring(start, end-start,
+ from_s, from_len,
+ 0, end-start, FORWARD);
+ if (offset == -1)
+ break;
+ next = start + offset;
+
+ Py_MEMCPY(result_s, start, next-start);
+
+ result_s += (next-start);
+ start = next+from_len;
+ }
+ Py_MEMCPY(result_s, start, end-start);
+ return result;
}
/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_single_character_in_place(PyBytesObject *self,
- char from_c, char to_c,
- Py_ssize_t maxcount)
+ char from_c, char to_c,
+ Py_ssize_t maxcount)
{
- char *self_s, *result_s, *start, *end, *next;
- Py_ssize_t self_len;
- PyBytesObject *result;
-
- /* The result string will be the same size */
- self_s = PyBytes_AS_STRING(self);
- self_len = PyBytes_GET_SIZE(self);
+ char *self_s, *result_s, *start, *end, *next;
+ Py_ssize_t self_len;
+ PyBytesObject *result;
- next = findchar(self_s, self_len, from_c);
+ /* The result string will be the same size */
+ self_s = PyBytes_AS_STRING(self);
+ self_len = PyBytes_GET_SIZE(self);
- if (next == NULL) {
- /* No matches; return the original string */
- return return_self(self);
- }
+ next = findchar(self_s, self_len, from_c);
- /* Need to make a new string */
- result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
- if (result == NULL)
- return NULL;
- result_s = PyBytes_AS_STRING(result);
- Py_MEMCPY(result_s, self_s, self_len);
-
- /* change everything in-place, starting with this one */
- start = result_s + (next-self_s);
- *start = to_c;
- start++;
- end = result_s + self_len;
+ if (next == NULL) {
+ /* No matches; return the original string */
+ return return_self(self);
+ }
- while (--maxcount > 0) {
- next = findchar(start, end-start, from_c);
- if (next == NULL)
- break;
- *next = to_c;
- start = next+1;
- }
+ /* Need to make a new string */
+ result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
+ if (result == NULL)
+ return NULL;
+ result_s = PyBytes_AS_STRING(result);
+ Py_MEMCPY(result_s, self_s, self_len);
+
+ /* change everything in-place, starting with this one */
+ start = result_s + (next-self_s);
+ *start = to_c;
+ start++;
+ end = result_s + self_len;
+
+ while (--maxcount > 0) {
+ next = findchar(start, end-start, from_c);
+ if (next == NULL)
+ break;
+ *next = to_c;
+ start = next+1;
+ }
- return result;
+ return result;
}
/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_substring_in_place(PyBytesObject *self,
- const char *from_s, Py_ssize_t from_len,
- const char *to_s, Py_ssize_t to_len,
- Py_ssize_t maxcount)
-{
- char *result_s, *start, *end;
- char *self_s;
- Py_ssize_t self_len, offset;
- PyBytesObject *result;
-
- /* The result string will be the same size */
-
- self_s = PyBytes_AS_STRING(self);
- self_len = PyBytes_GET_SIZE(self);
-
- offset = findstring(self_s, self_len,
- from_s, from_len,
- 0, self_len, FORWARD);
- if (offset == -1) {
- /* No matches; return the original string */
- return return_self(self);
- }
-
- /* Need to make a new string */
- result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
- if (result == NULL)
- return NULL;
- result_s = PyBytes_AS_STRING(result);
- Py_MEMCPY(result_s, self_s, self_len);
-
- /* change everything in-place, starting with this one */
- start = result_s + offset;
- Py_MEMCPY(start, to_s, from_len);
- start += from_len;
- end = result_s + self_len;
-
- while ( --maxcount > 0) {
- offset = findstring(start, end-start,
- from_s, from_len,
- 0, end-start, FORWARD);
- if (offset==-1)
- break;
- Py_MEMCPY(start+offset, to_s, from_len);
- start += offset+from_len;
- }
-
- return result;
+ const char *from_s, Py_ssize_t from_len,
+ const char *to_s, Py_ssize_t to_len,
+ Py_ssize_t maxcount)
+{
+ char *result_s, *start, *end;
+ char *self_s;
+ Py_ssize_t self_len, offset;
+ PyBytesObject *result;
+
+ /* The result string will be the same size */
+
+ self_s = PyBytes_AS_STRING(self);
+ self_len = PyBytes_GET_SIZE(self);
+
+ offset = findstring(self_s, self_len,
+ from_s, from_len,
+ 0, self_len, FORWARD);
+ if (offset == -1) {
+ /* No matches; return the original string */
+ return return_self(self);
+ }
+
+ /* Need to make a new string */
+ result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
+ if (result == NULL)
+ return NULL;
+ result_s = PyBytes_AS_STRING(result);
+ Py_MEMCPY(result_s, self_s, self_len);
+
+ /* change everything in-place, starting with this one */
+ start = result_s + offset;
+ Py_MEMCPY(start, to_s, from_len);
+ start += from_len;
+ end = result_s + self_len;
+
+ while ( --maxcount > 0) {
+ offset = findstring(start, end-start,
+ from_s, from_len,
+ 0, end-start, FORWARD);
+ if (offset==-1)
+ break;
+ Py_MEMCPY(start+offset, to_s, from_len);
+ start += offset+from_len;
+ }
+
+ return result;
}
/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_single_character(PyBytesObject *self,
- char from_c,
- const char *to_s, Py_ssize_t to_len,
- Py_ssize_t maxcount)
-{
- char *self_s, *result_s;
- char *start, *next, *end;
- Py_ssize_t self_len, result_len;
- Py_ssize_t count, product;
- PyBytesObject *result;
-
- self_s = PyBytes_AS_STRING(self);
- self_len = PyBytes_GET_SIZE(self);
-
- count = countchar(self_s, self_len, from_c, maxcount);
- if (count == 0) {
- /* no matches, return unchanged */
- return return_self(self);
- }
-
- /* use the difference between current and new, hence the "-1" */
- /* result_len = self_len + count * (to_len-1) */
- product = count * (to_len-1);
- if (product / (to_len-1) != count) {
- PyErr_SetString(PyExc_OverflowError,
- "replacement bytes are too long");
- return NULL;
- }
- result_len = self_len + product;
- if (result_len < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "replacment bytes are too long");
- return NULL;
- }
-
- if ( (result = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
- return NULL;
- result_s = PyBytes_AS_STRING(result);
-
- start = self_s;
- end = self_s + self_len;
- while (count-- > 0) {
- next = findchar(start, end-start, from_c);
- if (next == NULL)
- break;
-
- if (next == start) {
- /* replace with the 'to' */
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- start += 1;
- } else {
- /* copy the unchanged old then the 'to' */
- Py_MEMCPY(result_s, start, next-start);
- result_s += (next-start);
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- start = next+1;
- }
- }
- /* Copy the remainder of the remaining string */
- Py_MEMCPY(result_s, start, end-start);
-
- return result;
+ char from_c,
+ const char *to_s, Py_ssize_t to_len,
+ Py_ssize_t maxcount)
+{
+ char *self_s, *result_s;
+ char *start, *next, *end;
+ Py_ssize_t self_len, result_len;
+ Py_ssize_t count, product;
+ PyBytesObject *result;
+
+ self_s = PyBytes_AS_STRING(self);
+ self_len = PyBytes_GET_SIZE(self);
+
+ count = countchar(self_s, self_len, from_c, maxcount);
+ if (count == 0) {
+ /* no matches, return unchanged */
+ return return_self(self);
+ }
+
+ /* use the difference between current and new, hence the "-1" */
+ /* result_len = self_len + count * (to_len-1) */
+ product = count * (to_len-1);
+ if (product / (to_len-1) != count) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacement bytes are too long");
+ return NULL;
+ }
+ result_len = self_len + product;
+ if (result_len < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacment bytes are too long");
+ return NULL;
+ }
+
+ if ( (result = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
+ return NULL;
+ result_s = PyBytes_AS_STRING(result);
+
+ start = self_s;
+ end = self_s + self_len;
+ while (count-- > 0) {
+ next = findchar(start, end-start, from_c);
+ if (next == NULL)
+ break;
+
+ if (next == start) {
+ /* replace with the 'to' */
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ start += 1;
+ } else {
+ /* copy the unchanged old then the 'to' */
+ Py_MEMCPY(result_s, start, next-start);
+ result_s += (next-start);
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ start = next+1;
+ }
+ }
+ /* Copy the remainder of the remaining string */
+ Py_MEMCPY(result_s, start, end-start);
+
+ return result;
}
/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
Py_LOCAL(PyBytesObject *)
replace_substring(PyBytesObject *self,
- const char *from_s, Py_ssize_t from_len,
- const char *to_s, Py_ssize_t to_len,
- Py_ssize_t maxcount) {
- char *self_s, *result_s;
- char *start, *next, *end;
- Py_ssize_t self_len, result_len;
- Py_ssize_t count, offset, product;
- PyBytesObject *result;
-
- self_s = PyBytes_AS_STRING(self);
- self_len = PyBytes_GET_SIZE(self);
-
- count = countstring(self_s, self_len,
- from_s, from_len,
- 0, self_len, FORWARD, maxcount);
- if (count == 0) {
- /* no matches, return unchanged */
- return return_self(self);
- }
-
- /* Check for overflow */
- /* result_len = self_len + count * (to_len-from_len) */
- product = count * (to_len-from_len);
- if (product / (to_len-from_len) != count) {
- PyErr_SetString(PyExc_OverflowError,
- "replacement bytes are too long");
- return NULL;
- }
- result_len = self_len + product;
- if (result_len < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "replacement bytes are too long");
- return NULL;
- }
-
- if ( (result = (PyBytesObject *)
- PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
- return NULL;
- result_s = PyBytes_AS_STRING(result);
-
- start = self_s;
- end = self_s + self_len;
- while (count-- > 0) {
- offset = findstring(start, end-start,
- from_s, from_len,
- 0, end-start, FORWARD);
- if (offset == -1)
- break;
- next = start+offset;
- if (next == start) {
- /* replace with the 'to' */
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- start += from_len;
- } else {
- /* copy the unchanged old then the 'to' */
- Py_MEMCPY(result_s, start, next-start);
- result_s += (next-start);
- Py_MEMCPY(result_s, to_s, to_len);
- result_s += to_len;
- start = next+from_len;
- }
- }
- /* Copy the remainder of the remaining string */
- Py_MEMCPY(result_s, start, end-start);
-
- return result;
+ const char *from_s, Py_ssize_t from_len,
+ const char *to_s, Py_ssize_t to_len,
+ Py_ssize_t maxcount) {
+ char *self_s, *result_s;
+ char *start, *next, *end;
+ Py_ssize_t self_len, result_len;
+ Py_ssize_t count, offset, product;
+ PyBytesObject *result;
+
+ self_s = PyBytes_AS_STRING(self);
+ self_len = PyBytes_GET_SIZE(self);
+
+ count = countstring(self_s, self_len,
+ from_s, from_len,
+ 0, self_len, FORWARD, maxcount);
+ if (count == 0) {
+ /* no matches, return unchanged */
+ return return_self(self);
+ }
+
+ /* Check for overflow */
+ /* result_len = self_len + count * (to_len-from_len) */
+ product = count * (to_len-from_len);
+ if (product / (to_len-from_len) != count) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacement bytes are too long");
+ return NULL;
+ }
+ result_len = self_len + product;
+ if (result_len < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "replacement bytes are too long");
+ return NULL;
+ }
+
+ if ( (result = (PyBytesObject *)
+ PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
+ return NULL;
+ result_s = PyBytes_AS_STRING(result);
+
+ start = self_s;
+ end = self_s + self_len;
+ while (count-- > 0) {
+ offset = findstring(start, end-start,
+ from_s, from_len,
+ 0, end-start, FORWARD);
+ if (offset == -1)
+ break;
+ next = start+offset;
+ if (next == start) {
+ /* replace with the 'to' */
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ start += from_len;
+ } else {
+ /* copy the unchanged old then the 'to' */
+ Py_MEMCPY(result_s, start, next-start);
+ result_s += (next-start);
+ Py_MEMCPY(result_s, to_s, to_len);
+ result_s += to_len;
+ start = next+from_len;
+ }
+ }
+ /* Copy the remainder of the remaining string */
+ Py_MEMCPY(result_s, start, end-start);
+
+ return result;
}
Py_LOCAL(PyBytesObject *)
replace(PyBytesObject *self,
- const char *from_s, Py_ssize_t from_len,
- const char *to_s, Py_ssize_t to_len,
- Py_ssize_t maxcount)
-{
- if (maxcount < 0) {
- maxcount = PY_SSIZE_T_MAX;
- } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) {
- /* nothing to do; return the original string */
- return return_self(self);
- }
-
- if (maxcount == 0 ||
- (from_len == 0 && to_len == 0)) {
- /* nothing to do; return the original string */
- return return_self(self);
- }
-
- /* Handle zero-length special cases */
-
- if (from_len == 0) {
- /* insert the 'to' string everywhere. */
- /* >>> "Python".replace("", ".") */
- /* '.P.y.t.h.o.n.' */
- return replace_interleave(self, to_s, to_len, maxcount);
- }
-
- /* Except for "".replace("", "A") == "A" there is no way beyond this */
- /* point for an empty self string to generate a non-empty string */
- /* Special case so the remaining code always gets a non-empty string */
- if (PyBytes_GET_SIZE(self) == 0) {
- return return_self(self);
- }
-
- if (to_len == 0) {
- /* delete all occurrences of 'from' string */
- if (from_len == 1) {
- return replace_delete_single_character(
- self, from_s[0], maxcount);
- } else {
- return replace_delete_substring(self, from_s,
- from_len, maxcount);
- }
- }
-
- /* Handle special case where both strings have the same length */
-
- if (from_len == to_len) {
- if (from_len == 1) {
- return replace_single_character_in_place(
- self,
- from_s[0],
- to_s[0],
- maxcount);
- } else {
- return replace_substring_in_place(
- self, from_s, from_len, to_s, to_len,
- maxcount);
- }
- }
-
- /* Otherwise use the more generic algorithms */
- if (from_len == 1) {
- return replace_single_character(self, from_s[0],
- to_s, to_len, maxcount);
- } else {
- /* len('from')>=2, len('to')>=1 */
- return replace_substring(self, from_s, from_len, to_s, to_len,
- maxcount);
- }
+ const char *from_s, Py_ssize_t from_len,
+ const char *to_s, Py_ssize_t to_len,
+ Py_ssize_t maxcount)
+{
+ if (maxcount < 0) {
+ maxcount = PY_SSIZE_T_MAX;
+ } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) {
+ /* nothing to do; return the original string */
+ return return_self(self);
+ }
+
+ if (maxcount == 0 ||
+ (from_len == 0 && to_len == 0)) {
+ /* nothing to do; return the original string */
+ return return_self(self);
+ }
+
+ /* Handle zero-length special cases */
+
+ if (from_len == 0) {
+ /* insert the 'to' string everywhere. */
+ /* >>> "Python".replace("", ".") */
+ /* '.P.y.t.h.o.n.' */
+ return replace_interleave(self, to_s, to_len, maxcount);
+ }
+
+ /* Except for "".replace("", "A") == "A" there is no way beyond this */
+ /* point for an empty self string to generate a non-empty string */
+ /* Special case so the remaining code always gets a non-empty string */
+ if (PyBytes_GET_SIZE(self) == 0) {
+ return return_self(self);
+ }
+
+ if (to_len == 0) {
+ /* delete all occurrences of 'from' string */
+ if (from_len == 1) {
+ return replace_delete_single_character(
+ self, from_s[0], maxcount);
+ } else {
+ return replace_delete_substring(self, from_s,
+ from_len, maxcount);
+ }
+ }
+
+ /* Handle special case where both strings have the same length */
+
+ if (from_len == to_len) {
+ if (from_len == 1) {
+ return replace_single_character_in_place(
+ self,
+ from_s[0],
+ to_s[0],
+ maxcount);
+ } else {
+ return replace_substring_in_place(
+ self, from_s, from_len, to_s, to_len,
+ maxcount);
+ }
+ }
+
+ /* Otherwise use the more generic algorithms */
+ if (from_len == 1) {
+ return replace_single_character(self, from_s[0],
+ to_s, to_len, maxcount);
+ } else {
+ /* len('from')>=2, len('to')>=1 */
+ return replace_substring(self, from_s, from_len, to_s, to_len,
+ maxcount);
+ }
}
PyDoc_STRVAR(replace__doc__,
@@ -2562,31 +2562,31 @@ given, only the first count occurrences are replaced.");
static PyObject *
bytes_replace(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t count = -1;
- PyObject *from, *to;
- const char *from_s, *to_s;
- Py_ssize_t from_len, to_len;
+ Py_ssize_t count = -1;
+ PyObject *from, *to;
+ const char *from_s, *to_s;
+ Py_ssize_t from_len, to_len;
- if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
- return NULL;
+ if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
+ return NULL;
- if (PyBytes_Check(from)) {
- from_s = PyBytes_AS_STRING(from);
- from_len = PyBytes_GET_SIZE(from);
- }
- else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
- return NULL;
+ if (PyBytes_Check(from)) {
+ from_s = PyBytes_AS_STRING(from);
+ from_len = PyBytes_GET_SIZE(from);
+ }
+ else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
+ return NULL;
- if (PyBytes_Check(to)) {
- to_s = PyBytes_AS_STRING(to);
- to_len = PyBytes_GET_SIZE(to);
- }
- else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
- return NULL;
+ if (PyBytes_Check(to)) {
+ to_s = PyBytes_AS_STRING(to);
+ to_len = PyBytes_GET_SIZE(to);
+ }
+ else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
+ return NULL;
- return (PyObject *)replace((PyBytesObject *) self,
- from_s, from_len,
- to_s, to_len, count);
+ return (PyObject *)replace((PyBytesObject *) self,
+ from_s, from_len,
+ to_s, to_len, count);
}
/** End DALKE **/
@@ -2597,38 +2597,38 @@ bytes_replace(PyBytesObject *self, PyObject *args)
*/
Py_LOCAL(int)
_bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start,
- Py_ssize_t end, int direction)
+ Py_ssize_t end, int direction)
{
- Py_ssize_t len = PyBytes_GET_SIZE(self);
- Py_ssize_t slen;
- const char* sub;
- const char* str;
+ Py_ssize_t len = PyBytes_GET_SIZE(self);
+ Py_ssize_t slen;
+ const char* sub;
+ const char* str;
- if (PyBytes_Check(substr)) {
- sub = PyBytes_AS_STRING(substr);
- slen = PyBytes_GET_SIZE(substr);
- }
- else if (PyObject_AsCharBuffer(substr, &sub, &slen))
- return -1;
- str = PyBytes_AS_STRING(self);
+ if (PyBytes_Check(substr)) {
+ sub = PyBytes_AS_STRING(substr);
+ slen = PyBytes_GET_SIZE(substr);
+ }
+ else if (PyObject_AsCharBuffer(substr, &sub, &slen))
+ return -1;
+ str = PyBytes_AS_STRING(self);
- bytes_adjust_indices(&start, &end, len);
+ bytes_adjust_indices(&start, &end, len);
- if (direction < 0) {
- /* startswith */
- if (start+slen > len)
- return 0;
- } else {
- /* endswith */
- if (end-start < slen || start > len)
- return 0;
+ if (direction < 0) {
+ /* startswith */
+ if (start+slen > len)
+ return 0;
+ } else {
+ /* endswith */
+ if (end-start < slen || start > len)
+ return 0;
- if (end-slen > start)
- start = end - slen;
- }
- if (end-start >= slen)
- return ! memcmp(str+start, sub, slen);
- return 0;
+ if (end-slen > start)
+ start = end - slen;
+ }
+ if (end-start >= slen)
+ return ! memcmp(str+start, sub, slen);
+ return 0;
}
@@ -2643,33 +2643,33 @@ prefix can also be a tuple of bytes to try.");
static PyObject *
bytes_startswith(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t start = 0;
- Py_ssize_t end = PY_SSIZE_T_MAX;
- PyObject *subobj;
- int result;
-
- if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
- _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
- return NULL;
- if (PyTuple_Check(subobj)) {
- Py_ssize_t i;
- for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
- result = _bytes_tailmatch(self,
- PyTuple_GET_ITEM(subobj, i),
- start, end, -1);
- if (result == -1)
- return NULL;
- else if (result) {
- Py_RETURN_TRUE;
- }
- }
- Py_RETURN_FALSE;
- }
- result = _bytes_tailmatch(self, subobj, start, end, -1);
- if (result == -1)
- return NULL;
- else
- return PyBool_FromLong(result);
+ Py_ssize_t start = 0;
+ Py_ssize_t end = PY_SSIZE_T_MAX;
+ PyObject *subobj;
+ int result;
+
+ if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
+ _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
+ return NULL;
+ if (PyTuple_Check(subobj)) {
+ Py_ssize_t i;
+ for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
+ result = _bytes_tailmatch(self,
+ PyTuple_GET_ITEM(subobj, i),
+ start, end, -1);
+ if (result == -1)
+ return NULL;
+ else if (result) {
+ Py_RETURN_TRUE;
+ }
+ }
+ Py_RETURN_FALSE;
+ }
+ result = _bytes_tailmatch(self, subobj, start, end, -1);
+ if (result == -1)
+ return NULL;
+ else
+ return PyBool_FromLong(result);
}
@@ -2684,33 +2684,33 @@ suffix can also be a tuple of bytes to try.");
static PyObject *
bytes_endswith(PyBytesObject *self, PyObject *args)
{
- Py_ssize_t start = 0;
- Py_ssize_t end = PY_SSIZE_T_MAX;
- PyObject *subobj;
- int result;
-
- if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
- _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
- return NULL;
- if (PyTuple_Check(subobj)) {
- Py_ssize_t i;
- for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
- result = _bytes_tailmatch(self,
- PyTuple_GET_ITEM(subobj, i),
- start, end, +1);
- if (result == -1)
- return NULL;
- else if (result) {
- Py_RETURN_TRUE;
- }
- }
- Py_RETURN_FALSE;
- }
- result = _bytes_tailmatch(self, subobj, start, end, +1);
- if (result == -1)
- return NULL;
- else
- return PyBool_FromLong(result);
+ Py_ssize_t start = 0;
+ Py_ssize_t end = PY_SSIZE_T_MAX;
+ PyObject *subobj;
+ int result;
+
+ if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
+ _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
+ return NULL;
+ if (PyTuple_Check(subobj)) {
+ Py_ssize_t i;
+ for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
+ result = _bytes_tailmatch(self,
+ PyTuple_GET_ITEM(subobj, i),
+ start, end, +1);
+ if (result == -1)
+ return NULL;
+ else if (result) {
+ Py_RETURN_TRUE;
+ }
+ }
+ Py_RETURN_FALSE;
+ }
+ result = _bytes_tailmatch(self, subobj, start, end, +1);
+ if (result == -1)
+ return NULL;
+ else
+ return PyBool_FromLong(result);
}
@@ -2727,14 +2727,14 @@ able to handle UnicodeDecodeErrors.");
static PyObject *
bytes_decode(PyObject *self, PyObject *args)
{
- const char *encoding = NULL;
- const char *errors = NULL;
+ const char *encoding = NULL;
+ const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
- return NULL;
- if (encoding == NULL)
- encoding = PyUnicode_GetDefaultEncoding();
- return PyUnicode_FromEncodedObject(self, encoding, errors);
+ if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
+ return NULL;
+ if (encoding == NULL)
+ encoding = PyUnicode_GetDefaultEncoding();
+ return PyUnicode_FromEncodedObject(self, encoding, errors);
}
@@ -2748,61 +2748,61 @@ Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'.");
static int
hex_digit_to_int(Py_UNICODE c)
{
- if (c >= 128)
- return -1;
- if (ISDIGIT(c))
- return c - '0';
- else {
- if (ISUPPER(c))
- c = TOLOWER(c);
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- }
- return -1;
+ if (c >= 128)
+ return -1;
+ if (ISDIGIT(c))
+ return c - '0';
+ else {
+ if (ISUPPER(c))
+ c = TOLOWER(c);
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ }
+ return -1;
}
static PyObject *
bytes_fromhex(PyObject *cls, PyObject *args)
{
- PyObject *newstring, *hexobj;
- char *buf;
- Py_UNICODE *hex;
- Py_ssize_t hexlen, byteslen, i, j;
- int top, bot;
-
- if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
- return NULL;
- assert(PyUnicode_Check(hexobj));
- hexlen = PyUnicode_GET_SIZE(hexobj);
- hex = PyUnicode_AS_UNICODE(hexobj);
- byteslen = hexlen/2; /* This overestimates if there are spaces */
- newstring = PyBytes_FromStringAndSize(NULL, byteslen);
- if (!newstring)
- return NULL;
- buf = PyBytes_AS_STRING(newstring);
- for (i = j = 0; i < hexlen; i += 2) {
- /* skip over spaces in the input */
- while (hex[i] == ' ')
- i++;
- if (i >= hexlen)
- break;
- top = hex_digit_to_int(hex[i]);
- bot = hex_digit_to_int(hex[i+1]);
- if (top == -1 || bot == -1) {
- PyErr_Format(PyExc_ValueError,
- "non-hexadecimal number found in "
- "fromhex() arg at position %zd", i);
- goto error;
- }
- buf[j++] = (top << 4) + bot;
- }
- if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0)
- goto error;
- return newstring;
+ PyObject *newstring, *hexobj;
+ char *buf;
+ Py_UNICODE *hex;
+ Py_ssize_t hexlen, byteslen, i, j;
+ int top, bot;
+
+ if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj))
+ return NULL;
+ assert(PyUnicode_Check(hexobj));
+ hexlen = PyUnicode_GET_SIZE(hexobj);
+ hex = PyUnicode_AS_UNICODE(hexobj);
+ byteslen = hexlen/2; /* This overestimates if there are spaces */
+ newstring = PyBytes_FromStringAndSize(NULL, byteslen);
+ if (!newstring)
+ return NULL;
+ buf = PyBytes_AS_STRING(newstring);
+ for (i = j = 0; i < hexlen; i += 2) {
+ /* skip over spaces in the input */
+ while (hex[i] == ' ')
+ i++;
+ if (i >= hexlen)
+ break;
+ top = hex_digit_to_int(hex[i]);
+ bot = hex_digit_to_int(hex[i+1]);
+ if (top == -1 || bot == -1) {
+ PyErr_Format(PyExc_ValueError,
+ "non-hexadecimal number found in "
+ "fromhex() arg at position %zd", i);
+ goto error;
+ }
+ buf[j++] = (top << 4) + bot;
+ }
+ if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0)
+ goto error;
+ return newstring;
error:
- Py_XDECREF(newstring);
- return NULL;
+ Py_XDECREF(newstring);
+ return NULL;
}
PyDoc_STRVAR(sizeof__doc__,
@@ -2811,80 +2811,80 @@ PyDoc_STRVAR(sizeof__doc__,
static PyObject *
bytes_sizeof(PyBytesObject *v)
{
- Py_ssize_t res;
- res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize;
- return PyLong_FromSsize_t(res);
+ Py_ssize_t res;
+ res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize;
+ return PyLong_FromSsize_t(res);
}
static PyObject *
bytes_getnewargs(PyBytesObject *v)
{
- return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v));
+ return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v));
}
static PyMethodDef
bytes_methods[] = {
- {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
- {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
- _Py_capitalize__doc__},
- {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
- {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
- {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode__doc__},
- {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
- endswith__doc__},
- {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS,
- expandtabs__doc__},
- {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
- {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
- fromhex_doc},
- {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
- {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
- _Py_isalnum__doc__},
- {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
- _Py_isalpha__doc__},
- {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
- _Py_isdigit__doc__},
- {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
- _Py_islower__doc__},
- {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
- _Py_isspace__doc__},
- {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
- _Py_istitle__doc__},
- {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
- _Py_isupper__doc__},
- {"join", (PyCFunction)bytes_join, METH_O, join__doc__},
- {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
- {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
- {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__},
- {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC,
- _Py_maketrans__doc__},
- {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
- {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
- {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
- {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
- {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
- {"rpartition", (PyCFunction)bytes_rpartition, METH_O,
- rpartition__doc__},
- {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
- {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__},
- {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
- {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS,
- splitlines__doc__},
- {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
- startswith__doc__},
- {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__},
- {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
- _Py_swapcase__doc__},
- {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
- {"translate", (PyCFunction)bytes_translate, METH_VARARGS,
- translate__doc__},
- {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
- {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
- {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS,
- sizeof__doc__},
- {NULL, NULL} /* sentinel */
+ {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
+ {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
+ _Py_capitalize__doc__},
+ {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
+ {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
+ {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode__doc__},
+ {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS,
+ endswith__doc__},
+ {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS,
+ expandtabs__doc__},
+ {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
+ {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
+ fromhex_doc},
+ {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
+ {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
+ _Py_isalnum__doc__},
+ {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
+ _Py_isalpha__doc__},
+ {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
+ _Py_isdigit__doc__},
+ {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
+ _Py_islower__doc__},
+ {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
+ _Py_isspace__doc__},
+ {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
+ _Py_istitle__doc__},
+ {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
+ _Py_isupper__doc__},
+ {"join", (PyCFunction)bytes_join, METH_O, join__doc__},
+ {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
+ {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
+ {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__},
+ {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC,
+ _Py_maketrans__doc__},
+ {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
+ {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
+ {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
+ {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
+ {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
+ {"rpartition", (PyCFunction)bytes_rpartition, METH_O,
+ rpartition__doc__},
+ {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
+ {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__},
+ {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
+ {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS,
+ splitlines__doc__},
+ {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
+ startswith__doc__},
+ {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__},
+ {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
+ _Py_swapcase__doc__},
+ {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
+ {"translate", (PyCFunction)bytes_translate, METH_VARARGS,
+ translate__doc__},
+ {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
+ {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
+ {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS,
+ sizeof__doc__},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
@@ -2893,187 +2893,187 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *x = NULL;
- const char *encoding = NULL;
- const char *errors = NULL;
- PyObject *new = NULL;
- static char *kwlist[] = {"source", "encoding", "errors", 0};
-
- if (type != &PyBytes_Type)
- return str_subtype_new(type, args, kwds);
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x,
- &encoding, &errors))
- return NULL;
- if (x == NULL) {
- if (encoding != NULL || errors != NULL) {
- PyErr_SetString(PyExc_TypeError,
- "encoding or errors without sequence "
- "argument");
- return NULL;
- }
- return PyBytes_FromString("");
- }
-
- if (PyUnicode_Check(x)) {
- /* Encode via the codec registry */
- if (encoding == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "string argument without an encoding");
- return NULL;
- }
- new = PyUnicode_AsEncodedString(x, encoding, errors);
- if (new == NULL)
- return NULL;
- assert(PyBytes_Check(new));
- return new;
- }
-
- /* If it's not unicode, there can't be encoding or errors */
- if (encoding != NULL || errors != NULL) {
- PyErr_SetString(PyExc_TypeError,
- "encoding or errors without a string argument");
- return NULL;
- }
- return PyObject_Bytes(x);
+ PyObject *x = NULL;
+ const char *encoding = NULL;
+ const char *errors = NULL;
+ PyObject *new = NULL;
+ static char *kwlist[] = {"source", "encoding", "errors", 0};
+
+ if (type != &PyBytes_Type)
+ return str_subtype_new(type, args, kwds);
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x,
+ &encoding, &errors))
+ return NULL;
+ if (x == NULL) {
+ if (encoding != NULL || errors != NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "encoding or errors without sequence "
+ "argument");
+ return NULL;
+ }
+ return PyBytes_FromString("");
+ }
+
+ if (PyUnicode_Check(x)) {
+ /* Encode via the codec registry */
+ if (encoding == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "string argument without an encoding");
+ return NULL;
+ }
+ new = PyUnicode_AsEncodedString(x, encoding, errors);
+ if (new == NULL)
+ return NULL;
+ assert(PyBytes_Check(new));
+ return new;
+ }
+
+ /* If it's not unicode, there can't be encoding or errors */
+ if (encoding != NULL || errors != NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "encoding or errors without a string argument");
+ return NULL;
+ }
+ return PyObject_Bytes(x);
}
PyObject *
PyBytes_FromObject(PyObject *x)
{
- PyObject *new, *it;
- Py_ssize_t i, size;
-
- if (x == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- /* Is it an int? */
- size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
- if (size == -1 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- return NULL;
- PyErr_Clear();
- }
- else if (size < 0) {
- PyErr_SetString(PyExc_ValueError, "negative count");
- return NULL;
- }
- else {
- new = PyBytes_FromStringAndSize(NULL, size);
- if (new == NULL) {
- return NULL;
- }
- if (size > 0) {
- memset(((PyBytesObject*)new)->ob_sval, 0, size);
- }
- return new;
- }
-
- /* Use the modern buffer interface */
- if (PyObject_CheckBuffer(x)) {
- Py_buffer view;
- if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0)
- return NULL;
- new = PyBytes_FromStringAndSize(NULL, view.len);
- if (!new)
- goto fail;
- /* XXX(brett.cannon): Better way to get to internal buffer? */
- if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval,
- &view, view.len, 'C') < 0)
- goto fail;
- PyBuffer_Release(&view);
- return new;
- fail:
- Py_XDECREF(new);
- PyBuffer_Release(&view);
- return NULL;
- }
-
- /* For iterator version, create a string object and resize as needed */
- /* XXX(gb): is 64 a good value? also, optimize if length is known */
- /* XXX(guido): perhaps use Pysequence_Fast() -- I can't imagine the
- input being a truly long iterator. */
- size = 64;
- new = PyBytes_FromStringAndSize(NULL, size);
- if (new == NULL)
- return NULL;
-
- /* XXX Optimize this if the arguments is a list, tuple */
-
- /* Get the iterator */
- it = PyObject_GetIter(x);
- if (it == NULL)
- goto error;
-
- /* Run the iterator to exhaustion */
- for (i = 0; ; i++) {
- PyObject *item;
- Py_ssize_t value;
-
- /* Get the next item */
- item = PyIter_Next(it);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto error;
- break;
- }
-
- /* Interpret it as an int (__index__) */
- value = PyNumber_AsSsize_t(item, PyExc_ValueError);
- Py_DECREF(item);
- if (value == -1 && PyErr_Occurred())
- goto error;
-
- /* Range check */
- if (value < 0 || value >= 256) {
- PyErr_SetString(PyExc_ValueError,
- "bytes must be in range(0, 256)");
- goto error;
- }
-
- /* Append the byte */
- if (i >= size) {
- size *= 2;
- if (_PyBytes_Resize(&new, size) < 0)
- goto error;
- }
- ((PyBytesObject *)new)->ob_sval[i] = value;
- }
- _PyBytes_Resize(&new, i);
-
- /* Clean up and return success */
- Py_DECREF(it);
- return new;
+ PyObject *new, *it;
+ Py_ssize_t i, size;
+
+ if (x == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ /* Is it an int? */
+ size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
+ if (size == -1 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_OverflowError))
+ return NULL;
+ PyErr_Clear();
+ }
+ else if (size < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative count");
+ return NULL;
+ }
+ else {
+ new = PyBytes_FromStringAndSize(NULL, size);
+ if (new == NULL) {
+ return NULL;
+ }
+ if (size > 0) {
+ memset(((PyBytesObject*)new)->ob_sval, 0, size);
+ }
+ return new;
+ }
+
+ /* Use the modern buffer interface */
+ if (PyObject_CheckBuffer(x)) {
+ Py_buffer view;
+ if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0)
+ return NULL;
+ new = PyBytes_FromStringAndSize(NULL, view.len);
+ if (!new)
+ goto fail;
+ /* XXX(brett.cannon): Better way to get to internal buffer? */
+ if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval,
+ &view, view.len, 'C') < 0)
+ goto fail;
+ PyBuffer_Release(&view);
+ return new;
+ fail:
+ Py_XDECREF(new);
+ PyBuffer_Release(&view);
+ return NULL;
+ }
+
+ /* For iterator version, create a string object and resize as needed */
+ /* XXX(gb): is 64 a good value? also, optimize if length is known */
+ /* XXX(guido): perhaps use Pysequence_Fast() -- I can't imagine the
+ input being a truly long iterator. */
+ size = 64;
+ new = PyBytes_FromStringAndSize(NULL, size);
+ if (new == NULL)
+ return NULL;
+
+ /* XXX Optimize this if the arguments is a list, tuple */
+
+ /* Get the iterator */
+ it = PyObject_GetIter(x);
+ if (it == NULL)
+ goto error;
+
+ /* Run the iterator to exhaustion */
+ for (i = 0; ; i++) {
+ PyObject *item;
+ Py_ssize_t value;
+
+ /* Get the next item */
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto error;
+ break;
+ }
+
+ /* Interpret it as an int (__index__) */
+ value = PyNumber_AsSsize_t(item, PyExc_ValueError);
+ Py_DECREF(item);
+ if (value == -1 && PyErr_Occurred())
+ goto error;
+
+ /* Range check */
+ if (value < 0 || value >= 256) {
+ PyErr_SetString(PyExc_ValueError,
+ "bytes must be in range(0, 256)");
+ goto error;
+ }
+
+ /* Append the byte */
+ if (i >= size) {
+ size *= 2;
+ if (_PyBytes_Resize(&new, size) < 0)
+ goto error;
+ }
+ ((PyBytesObject *)new)->ob_sval[i] = value;
+ }
+ _PyBytes_Resize(&new, i);
+
+ /* Clean up and return success */
+ Py_DECREF(it);
+ return new;
error:
- /* Error handling when new != NULL */
- Py_XDECREF(it);
- Py_DECREF(new);
- return NULL;
+ /* Error handling when new != NULL */
+ Py_XDECREF(it);
+ Py_DECREF(new);
+ return NULL;
}
static PyObject *
str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *tmp, *pnew;
- Py_ssize_t n;
-
- assert(PyType_IsSubtype(type, &PyBytes_Type));
- tmp = bytes_new(&PyBytes_Type, args, kwds);
- if (tmp == NULL)
- return NULL;
- assert(PyBytes_CheckExact(tmp));
- n = PyBytes_GET_SIZE(tmp);
- pnew = type->tp_alloc(type, n);
- if (pnew != NULL) {
- Py_MEMCPY(PyBytes_AS_STRING(pnew),
- PyBytes_AS_STRING(tmp), n+1);
- ((PyBytesObject *)pnew)->ob_shash =
- ((PyBytesObject *)tmp)->ob_shash;
- }
- Py_DECREF(tmp);
- return pnew;
+ PyObject *tmp, *pnew;
+ Py_ssize_t n;
+
+ assert(PyType_IsSubtype(type, &PyBytes_Type));
+ tmp = bytes_new(&PyBytes_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyBytes_CheckExact(tmp));
+ n = PyBytes_GET_SIZE(tmp);
+ pnew = type->tp_alloc(type, n);
+ if (pnew != NULL) {
+ Py_MEMCPY(PyBytes_AS_STRING(pnew),
+ PyBytes_AS_STRING(tmp), n+1);
+ ((PyBytesObject *)pnew)->ob_shash =
+ ((PyBytesObject *)tmp)->ob_shash;
+ }
+ Py_DECREF(tmp);
+ return pnew;
}
PyDoc_STRVAR(bytes_doc,
@@ -3091,70 +3091,70 @@ Construct an immutable array of bytes from:\n\
static PyObject *bytes_iter(PyObject *seq);
PyTypeObject PyBytes_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "bytes",
- PyBytesObject_SIZE,
- sizeof(char),
- bytes_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)bytes_repr, /* tp_repr */
- 0, /* tp_as_number */
- &bytes_as_sequence, /* tp_as_sequence */
- &bytes_as_mapping, /* tp_as_mapping */
- (hashfunc)bytes_hash, /* tp_hash */
- 0, /* tp_call */
- bytes_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- &bytes_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */
- bytes_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- (richcmpfunc)bytes_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- bytes_iter, /* tp_iter */
- 0, /* tp_iternext */
- bytes_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- &PyBaseObject_Type, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- bytes_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "bytes",
+ PyBytesObject_SIZE,
+ sizeof(char),
+ bytes_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)bytes_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &bytes_as_sequence, /* tp_as_sequence */
+ &bytes_as_mapping, /* tp_as_mapping */
+ (hashfunc)bytes_hash, /* tp_hash */
+ 0, /* tp_call */
+ bytes_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ &bytes_as_buffer, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */
+ bytes_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ (richcmpfunc)bytes_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ bytes_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ bytes_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ &PyBaseObject_Type, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ bytes_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
void
PyBytes_Concat(register PyObject **pv, register PyObject *w)
{
- register PyObject *v;
- assert(pv != NULL);
- if (*pv == NULL)
- return;
- if (w == NULL) {
- Py_DECREF(*pv);
- *pv = NULL;
- return;
- }
- v = bytes_concat(*pv, w);
- Py_DECREF(*pv);
- *pv = v;
+ register PyObject *v;
+ assert(pv != NULL);
+ if (*pv == NULL)
+ return;
+ if (w == NULL) {
+ Py_DECREF(*pv);
+ *pv = NULL;
+ return;
+ }
+ v = bytes_concat(*pv, w);
+ Py_DECREF(*pv);
+ *pv = v;
}
void
PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w)
{
- PyBytes_Concat(pv, w);
- Py_XDECREF(w);
+ PyBytes_Concat(pv, w);
+ Py_XDECREF(w);
}
@@ -3175,31 +3175,31 @@ PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w)
int
_PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
{
- register PyObject *v;
- register PyBytesObject *sv;
- v = *pv;
- if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
- *pv = 0;
- Py_DECREF(v);
- PyErr_BadInternalCall();
- return -1;
- }
- /* XXX UNREF/NEWREF interface should be more symmetrical */
- _Py_DEC_REFTOTAL;
- _Py_ForgetReference(v);
- *pv = (PyObject *)
- PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize);
- if (*pv == NULL) {
- PyObject_Del(v);
- PyErr_NoMemory();
- return -1;
- }
- _Py_NewReference(*pv);
- sv = (PyBytesObject *) *pv;
- Py_SIZE(sv) = newsize;
- sv->ob_sval[newsize] = '\0';
- sv->ob_shash = -1; /* invalidate cached hash value */
- return 0;
+ register PyObject *v;
+ register PyBytesObject *sv;
+ v = *pv;
+ if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) {
+ *pv = 0;
+ Py_DECREF(v);
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ /* XXX UNREF/NEWREF interface should be more symmetrical */
+ _Py_DEC_REFTOTAL;
+ _Py_ForgetReference(v);
+ *pv = (PyObject *)
+ PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize);
+ if (*pv == NULL) {
+ PyObject_Del(v);
+ PyErr_NoMemory();
+ return -1;
+ }
+ _Py_NewReference(*pv);
+ sv = (PyBytesObject *) *pv;
+ Py_SIZE(sv) = newsize;
+ sv->ob_sval[newsize] = '\0';
+ sv->ob_shash = -1; /* invalidate cached hash value */
+ return 0;
}
/* _PyBytes_FormatLong emulates the format codes d, u, o, x and X, and
@@ -3215,262 +3215,262 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
* set in flags. The case of hex digits will be correct,
* There will be at least prec digits, zero-filled on the left if
* necessary to get that many.
- * val object to be converted
- * flags bitmask of format flags; only F_ALT is looked at
- * prec minimum number of digits; 0-fill on left if needed
- * type a character in [duoxX]; u acts the same as d
+ * val object to be converted
+ * flags bitmask of format flags; only F_ALT is looked at
+ * prec minimum number of digits; 0-fill on left if needed
+ * type a character in [duoxX]; u acts the same as d
*
* CAUTION: o, x and X conversions on regular ints can never
* produce a '-' sign, but can for Python's unbounded ints.
*/
PyObject*
_PyBytes_FormatLong(PyObject *val, int flags, int prec, int type,
- char **pbuf, int *plen)
-{
- PyObject *result = NULL;
- char *buf;
- Py_ssize_t i;
- int sign; /* 1 if '-', else 0 */
- int len; /* number of characters */
- Py_ssize_t llen;
- int numdigits; /* len == numnondigits + numdigits */
- int numnondigits = 0;
-
- /* Avoid exceeding SSIZE_T_MAX */
- if (prec > INT_MAX-3) {
- PyErr_SetString(PyExc_OverflowError,
- "precision too large");
- return NULL;
- }
-
- switch (type) {
- case 'd':
- case 'u':
- /* Special-case boolean: we want 0/1 */
- if (PyBool_Check(val))
- result = PyNumber_ToBase(val, 10);
- else
- result = Py_TYPE(val)->tp_str(val);
- break;
- case 'o':
- numnondigits = 2;
- result = PyNumber_ToBase(val, 8);
- break;
- case 'x':
- case 'X':
- numnondigits = 2;
- result = PyNumber_ToBase(val, 16);
- break;
- default:
- assert(!"'type' not in [duoxX]");
- }
- if (!result)
- return NULL;
-
- buf = _PyUnicode_AsString(result);
- if (!buf) {
- Py_DECREF(result);
- return NULL;
- }
-
- /* To modify the string in-place, there can only be one reference. */
- if (Py_REFCNT(result) != 1) {
- PyErr_BadInternalCall();
- return NULL;
- }
- llen = PyUnicode_GetSize(result);
- if (llen > INT_MAX) {
- PyErr_SetString(PyExc_ValueError,
- "string too large in _PyBytes_FormatLong");
- return NULL;
- }
- len = (int)llen;
- if (buf[len-1] == 'L') {
- --len;
- buf[len] = '\0';
- }
- sign = buf[0] == '-';
- numnondigits += sign;
- numdigits = len - numnondigits;
- assert(numdigits > 0);
-
- /* Get rid of base marker unless F_ALT */
- if (((flags & F_ALT) == 0 &&
- (type == 'o' || type == 'x' || type == 'X'))) {
- assert(buf[sign] == '0');
- assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' ||
- buf[sign+1] == 'o');
- numnondigits -= 2;
- buf += 2;
- len -= 2;
- if (sign)
- buf[0] = '-';
- assert(len == numnondigits + numdigits);
- assert(numdigits > 0);
- }
-
- /* Fill with leading zeroes to meet minimum width. */
- if (prec > numdigits) {
- PyObject *r1 = PyBytes_FromStringAndSize(NULL,
- numnondigits + prec);
- char *b1;
- if (!r1) {
- Py_DECREF(result);
- return NULL;
- }
- b1 = PyBytes_AS_STRING(r1);
- for (i = 0; i < numnondigits; ++i)
- *b1++ = *buf++;
- for (i = 0; i < prec - numdigits; i++)
- *b1++ = '0';
- for (i = 0; i < numdigits; i++)
- *b1++ = *buf++;
- *b1 = '\0';
- Py_DECREF(result);
- result = r1;
- buf = PyBytes_AS_STRING(result);
- len = numnondigits + prec;
- }
-
- /* Fix up case for hex conversions. */
- if (type == 'X') {
- /* Need to convert all lower case letters to upper case.
- and need to convert 0x to 0X (and -0x to -0X). */
- for (i = 0; i < len; i++)
- if (buf[i] >= 'a' && buf[i] <= 'x')
- buf[i] -= 'a'-'A';
- }
- *pbuf = buf;
- *plen = len;
- return result;
+ char **pbuf, int *plen)
+{
+ PyObject *result = NULL;
+ char *buf;
+ Py_ssize_t i;
+ int sign; /* 1 if '-', else 0 */
+ int len; /* number of characters */
+ Py_ssize_t llen;
+ int numdigits; /* len == numnondigits + numdigits */
+ int numnondigits = 0;
+
+ /* Avoid exceeding SSIZE_T_MAX */
+ if (prec > INT_MAX-3) {
+ PyErr_SetString(PyExc_OverflowError,
+ "precision too large");
+ return NULL;
+ }
+
+ switch (type) {
+ case 'd':
+ case 'u':
+ /* Special-case boolean: we want 0/1 */
+ if (PyBool_Check(val))
+ result = PyNumber_ToBase(val, 10);
+ else
+ result = Py_TYPE(val)->tp_str(val);
+ break;
+ case 'o':
+ numnondigits = 2;
+ result = PyNumber_ToBase(val, 8);
+ break;
+ case 'x':
+ case 'X':
+ numnondigits = 2;
+ result = PyNumber_ToBase(val, 16);
+ break;
+ default:
+ assert(!"'type' not in [duoxX]");
+ }
+ if (!result)
+ return NULL;
+
+ buf = _PyUnicode_AsString(result);
+ if (!buf) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ /* To modify the string in-place, there can only be one reference. */
+ if (Py_REFCNT(result) != 1) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ llen = PyUnicode_GetSize(result);
+ if (llen > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError,
+ "string too large in _PyBytes_FormatLong");
+ return NULL;
+ }
+ len = (int)llen;
+ if (buf[len-1] == 'L') {
+ --len;
+ buf[len] = '\0';
+ }
+ sign = buf[0] == '-';
+ numnondigits += sign;
+ numdigits = len - numnondigits;
+ assert(numdigits > 0);
+
+ /* Get rid of base marker unless F_ALT */
+ if (((flags & F_ALT) == 0 &&
+ (type == 'o' || type == 'x' || type == 'X'))) {
+ assert(buf[sign] == '0');
+ assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' ||
+ buf[sign+1] == 'o');
+ numnondigits -= 2;
+ buf += 2;
+ len -= 2;
+ if (sign)
+ buf[0] = '-';
+ assert(len == numnondigits + numdigits);
+ assert(numdigits > 0);
+ }
+
+ /* Fill with leading zeroes to meet minimum width. */
+ if (prec > numdigits) {
+ PyObject *r1 = PyBytes_FromStringAndSize(NULL,
+ numnondigits + prec);
+ char *b1;
+ if (!r1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ b1 = PyBytes_AS_STRING(r1);
+ for (i = 0; i < numnondigits; ++i)
+ *b1++ = *buf++;
+ for (i = 0; i < prec - numdigits; i++)
+ *b1++ = '0';
+ for (i = 0; i < numdigits; i++)
+ *b1++ = *buf++;
+ *b1 = '\0';
+ Py_DECREF(result);
+ result = r1;
+ buf = PyBytes_AS_STRING(result);
+ len = numnondigits + prec;
+ }
+
+ /* Fix up case for hex conversions. */
+ if (type == 'X') {
+ /* Need to convert all lower case letters to upper case.
+ and need to convert 0x to 0X (and -0x to -0X). */
+ for (i = 0; i < len; i++)
+ if (buf[i] >= 'a' && buf[i] <= 'x')
+ buf[i] -= 'a'-'A';
+ }
+ *pbuf = buf;
+ *plen = len;
+ return result;
}
void
PyBytes_Fini(void)
{
- int i;
- for (i = 0; i < UCHAR_MAX + 1; i++) {
- Py_XDECREF(characters[i]);
- characters[i] = NULL;
- }
- Py_XDECREF(nullstring);
- nullstring = NULL;
+ int i;
+ for (i = 0; i < UCHAR_MAX + 1; i++) {
+ Py_XDECREF(characters[i]);
+ characters[i] = NULL;
+ }
+ Py_XDECREF(nullstring);
+ nullstring = NULL;
}
/*********************** Bytes Iterator ****************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t it_index;
- PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ Py_ssize_t it_index;
+ PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */
} striterobject;
static void
striter_dealloc(striterobject *it)
{
- _PyObject_GC_UNTRACK(it);
- Py_XDECREF(it->it_seq);
- PyObject_GC_Del(it);
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
}
static int
striter_traverse(striterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_seq);
- return 0;
+ Py_VISIT(it->it_seq);
+ return 0;
}
static PyObject *
striter_next(striterobject *it)
{
- PyBytesObject *seq;
- PyObject *item;
-
- assert(it != NULL);
- seq = it->it_seq;
- if (seq == NULL)
- return NULL;
- assert(PyBytes_Check(seq));
-
- if (it->it_index < PyBytes_GET_SIZE(seq)) {
- item = PyLong_FromLong(
- (unsigned char)seq->ob_sval[it->it_index]);
- if (item != NULL)
- ++it->it_index;
- return item;
- }
+ PyBytesObject *seq;
+ PyObject *item;
+
+ assert(it != NULL);
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+ assert(PyBytes_Check(seq));
+
+ if (it->it_index < PyBytes_GET_SIZE(seq)) {
+ item = PyLong_FromLong(
+ (unsigned char)seq->ob_sval[it->it_index]);
+ if (item != NULL)
+ ++it->it_index;
+ return item;
+ }
- Py_DECREF(seq);
- it->it_seq = NULL;
- return NULL;
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ return NULL;
}
static PyObject *
striter_len(striterobject *it)
{
- Py_ssize_t len = 0;
- if (it->it_seq)
- len = PyBytes_GET_SIZE(it->it_seq) - it->it_index;
- return PyLong_FromSsize_t(len);
+ Py_ssize_t len = 0;
+ if (it->it_seq)
+ len = PyBytes_GET_SIZE(it->it_seq) - it->it_index;
+ return PyLong_FromSsize_t(len);
}
PyDoc_STRVAR(length_hint_doc,
- "Private method returning an estimate of len(list(it)).");
+ "Private method returning an estimate of len(list(it)).");
static PyMethodDef striter_methods[] = {
- {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS,
- length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS,
+ length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyBytesIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "bytes_iterator", /* tp_name */
- sizeof(striterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)striter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)striter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)striter_next, /* tp_iternext */
- striter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "bytes_iterator", /* tp_name */
+ sizeof(striterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)striter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)striter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)striter_next, /* tp_iternext */
+ striter_methods, /* tp_methods */
+ 0,
};
static PyObject *
bytes_iter(PyObject *seq)
{
- striterobject *it;
-
- if (!PyBytes_Check(seq)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- it = PyObject_GC_New(striterobject, &PyBytesIter_Type);
- if (it == NULL)
- return NULL;
- it->it_index = 0;
- Py_INCREF(seq);
- it->it_seq = (PyBytesObject *)seq;
- _PyObject_GC_TRACK(it);
- return (PyObject *)it;
+ striterobject *it;
+
+ if (!PyBytes_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(striterobject, &PyBytesIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = (PyBytesObject *)seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
}
diff --git a/Objects/cellobject.c b/Objects/cellobject.c
index d7908de..eb5ad98 100644
--- a/Objects/cellobject.c
+++ b/Objects/cellobject.c
@@ -5,50 +5,50 @@
PyObject *
PyCell_New(PyObject *obj)
{
- PyCellObject *op;
+ PyCellObject *op;
- op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
- if (op == NULL)
- return NULL;
- op->ob_ref = obj;
- Py_XINCREF(obj);
+ op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
+ if (op == NULL)
+ return NULL;
+ op->ob_ref = obj;
+ Py_XINCREF(obj);
- _PyObject_GC_TRACK(op);
- return (PyObject *)op;
+ _PyObject_GC_TRACK(op);
+ return (PyObject *)op;
}
PyObject *
PyCell_Get(PyObject *op)
{
- if (!PyCell_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- Py_XINCREF(((PyCellObject*)op)->ob_ref);
- return PyCell_GET(op);
+ if (!PyCell_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ Py_XINCREF(((PyCellObject*)op)->ob_ref);
+ return PyCell_GET(op);
}
int
PyCell_Set(PyObject *op, PyObject *obj)
{
- PyObject* oldobj;
- if (!PyCell_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- oldobj = PyCell_GET(op);
- Py_XINCREF(obj);
- PyCell_SET(op, obj);
- Py_XDECREF(oldobj);
- return 0;
+ PyObject* oldobj;
+ if (!PyCell_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ oldobj = PyCell_GET(op);
+ Py_XINCREF(obj);
+ PyCell_SET(op, obj);
+ Py_XDECREF(oldobj);
+ return 0;
}
static void
cell_dealloc(PyCellObject *op)
{
- _PyObject_GC_UNTRACK(op);
- Py_XDECREF(op->ob_ref);
- PyObject_GC_Del(op);
+ _PyObject_GC_UNTRACK(op);
+ Py_XDECREF(op->ob_ref);
+ PyObject_GC_Del(op);
}
#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
@@ -56,124 +56,124 @@ cell_dealloc(PyCellObject *op)
static PyObject *
cell_richcompare(PyObject *a, PyObject *b, int op)
{
- int result;
- PyObject *v;
-
- /* neither argument should be NULL, unless something's gone wrong */
- assert(a != NULL && b != NULL);
-
- /* both arguments should be instances of PyCellObject */
- if (!PyCell_Check(a) || !PyCell_Check(b)) {
- v = Py_NotImplemented;
- Py_INCREF(v);
- return v;
- }
-
- /* compare cells by contents; empty cells come before anything else */
- a = ((PyCellObject *)a)->ob_ref;
- b = ((PyCellObject *)b)->ob_ref;
- if (a != NULL && b != NULL)
- return PyObject_RichCompare(a, b, op);
-
- result = (b == NULL) - (a == NULL);
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- case Py_LE:
- v = TEST_COND(result <= 0);
- break;
- case Py_GE:
- v = TEST_COND(result >= 0);
- break;
- case Py_LT:
- v = TEST_COND(result < 0);
- break;
- case Py_GT:
- v = TEST_COND(result > 0);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
- Py_INCREF(v);
- return v;
+ int result;
+ PyObject *v;
+
+ /* neither argument should be NULL, unless something's gone wrong */
+ assert(a != NULL && b != NULL);
+
+ /* both arguments should be instances of PyCellObject */
+ if (!PyCell_Check(a) || !PyCell_Check(b)) {
+ v = Py_NotImplemented;
+ Py_INCREF(v);
+ return v;
+ }
+
+ /* compare cells by contents; empty cells come before anything else */
+ a = ((PyCellObject *)a)->ob_ref;
+ b = ((PyCellObject *)b)->ob_ref;
+ if (a != NULL && b != NULL)
+ return PyObject_RichCompare(a, b, op);
+
+ result = (b == NULL) - (a == NULL);
+ switch (op) {
+ case Py_EQ:
+ v = TEST_COND(result == 0);
+ break;
+ case Py_NE:
+ v = TEST_COND(result != 0);
+ break;
+ case Py_LE:
+ v = TEST_COND(result <= 0);
+ break;
+ case Py_GE:
+ v = TEST_COND(result >= 0);
+ break;
+ case Py_LT:
+ v = TEST_COND(result < 0);
+ break;
+ case Py_GT:
+ v = TEST_COND(result > 0);
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
+ Py_INCREF(v);
+ return v;
}
static PyObject *
cell_repr(PyCellObject *op)
{
- if (op->ob_ref == NULL)
- return PyUnicode_FromFormat("<cell at %p: empty>", op);
+ if (op->ob_ref == NULL)
+ return PyUnicode_FromFormat("<cell at %p: empty>", op);
- return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
- op, op->ob_ref->ob_type->tp_name,
- op->ob_ref);
+ return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
+ op, op->ob_ref->ob_type->tp_name,
+ op->ob_ref);
}
static int
cell_traverse(PyCellObject *op, visitproc visit, void *arg)
{
- Py_VISIT(op->ob_ref);
- return 0;
+ Py_VISIT(op->ob_ref);
+ return 0;
}
static int
cell_clear(PyCellObject *op)
{
- Py_CLEAR(op->ob_ref);
- return 0;
+ Py_CLEAR(op->ob_ref);
+ return 0;
}
static PyObject *
cell_get_contents(PyCellObject *op, void *closure)
{
- if (op->ob_ref == NULL)
- {
- PyErr_SetString(PyExc_ValueError, "Cell is empty");
- return NULL;
- }
- Py_INCREF(op->ob_ref);
- return op->ob_ref;
+ if (op->ob_ref == NULL)
+ {
+ PyErr_SetString(PyExc_ValueError, "Cell is empty");
+ return NULL;
+ }
+ Py_INCREF(op->ob_ref);
+ return op->ob_ref;
}
static PyGetSetDef cell_getsetlist[] = {
- {"cell_contents", (getter)cell_get_contents, NULL},
- {NULL} /* sentinel */
+ {"cell_contents", (getter)cell_get_contents, NULL},
+ {NULL} /* sentinel */
};
PyTypeObject PyCell_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "cell",
- sizeof(PyCellObject),
- 0,
- (destructor)cell_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)cell_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)cell_traverse, /* tp_traverse */
- (inquiry)cell_clear, /* tp_clear */
- cell_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- cell_getsetlist, /* tp_getset */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "cell",
+ sizeof(PyCellObject),
+ 0,
+ (destructor)cell_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)cell_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)cell_traverse, /* tp_traverse */
+ (inquiry)cell_clear, /* tp_clear */
+ cell_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ cell_getsetlist, /* tp_getset */
};
diff --git a/Objects/classobject.c b/Objects/classobject.c
index cb78d15..afd4ec3 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -17,21 +17,21 @@ static int numfree = 0;
PyObject *
PyMethod_Function(PyObject *im)
{
- if (!PyMethod_Check(im)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyMethodObject *)im)->im_func;
+ if (!PyMethod_Check(im)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyMethodObject *)im)->im_func;
}
PyObject *
PyMethod_Self(PyObject *im)
{
- if (!PyMethod_Check(im)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyMethodObject *)im)->im_self;
+ if (!PyMethod_Check(im)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyMethodObject *)im)->im_self;
}
/* Method objects are used for bound instance methods returned by
@@ -42,29 +42,29 @@ PyMethod_Self(PyObject *im)
PyObject *
PyMethod_New(PyObject *func, PyObject *self)
{
- register PyMethodObject *im;
- if (self == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
- im = free_list;
- if (im != NULL) {
- free_list = (PyMethodObject *)(im->im_self);
- PyObject_INIT(im, &PyMethod_Type);
- numfree--;
- }
- else {
- im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
- if (im == NULL)
- return NULL;
- }
- im->im_weakreflist = NULL;
- Py_INCREF(func);
- im->im_func = func;
- Py_XINCREF(self);
- im->im_self = self;
- _PyObject_GC_TRACK(im);
- return (PyObject *)im;
+ register PyMethodObject *im;
+ if (self == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ im = free_list;
+ if (im != NULL) {
+ free_list = (PyMethodObject *)(im->im_self);
+ PyObject_INIT(im, &PyMethod_Type);
+ numfree--;
+ }
+ else {
+ im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
+ if (im == NULL)
+ return NULL;
+ }
+ im->im_weakreflist = NULL;
+ Py_INCREF(func);
+ im->im_func = func;
+ Py_XINCREF(self);
+ im->im_self = self;
+ _PyObject_GC_TRACK(im);
+ return (PyObject *)im;
}
/* Descriptors for PyMethod attributes */
@@ -74,11 +74,11 @@ PyMethod_New(PyObject *func, PyObject *self)
#define MO_OFF(x) offsetof(PyMethodObject, x)
static PyMemberDef method_memberlist[] = {
- {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
- "the function (or other callable) implementing a method"},
- {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
- "the instance to which a method is bound"},
- {NULL} /* Sentinel */
+ {"__func__", T_OBJECT, MO_OFF(im_func), READONLY|RESTRICTED,
+ "the function (or other callable) implementing a method"},
+ {"__self__", T_OBJECT, MO_OFF(im_self), READONLY|RESTRICTED,
+ "the instance to which a method is bound"},
+ {NULL} /* Sentinel */
};
/* Christian Tismer argued convincingly that method attributes should
@@ -89,46 +89,46 @@ static PyMemberDef method_memberlist[] = {
static PyObject *
method_get_doc(PyMethodObject *im, void *context)
{
- static PyObject *docstr;
- if (docstr == NULL) {
- docstr= PyUnicode_InternFromString("__doc__");
- if (docstr == NULL)
- return NULL;
- }
- return PyObject_GetAttr(im->im_func, docstr);
+ static PyObject *docstr;
+ if (docstr == NULL) {
+ docstr= PyUnicode_InternFromString("__doc__");
+ if (docstr == NULL)
+ return NULL;
+ }
+ return PyObject_GetAttr(im->im_func, docstr);
}
static PyGetSetDef method_getset[] = {
- {"__doc__", (getter)method_get_doc, NULL, NULL},
- {0}
+ {"__doc__", (getter)method_get_doc, NULL, NULL},
+ {0}
};
static PyObject *
method_getattro(PyObject *obj, PyObject *name)
{
- PyMethodObject *im = (PyMethodObject *)obj;
- PyTypeObject *tp = obj->ob_type;
- PyObject *descr = NULL;
-
- {
- if (tp->tp_dict == NULL) {
- if (PyType_Ready(tp) < 0)
- return NULL;
- }
- descr = _PyType_Lookup(tp, name);
- }
-
- if (descr != NULL) {
- descrgetfunc f = TP_DESCR_GET(descr->ob_type);
- if (f != NULL)
- return f(descr, obj, (PyObject *)obj->ob_type);
- else {
- Py_INCREF(descr);
- return descr;
- }
- }
-
- return PyObject_GetAttr(im->im_func, name);
+ PyMethodObject *im = (PyMethodObject *)obj;
+ PyTypeObject *tp = obj->ob_type;
+ PyObject *descr = NULL;
+
+ {
+ if (tp->tp_dict == NULL) {
+ if (PyType_Ready(tp) < 0)
+ return NULL;
+ }
+ descr = _PyType_Lookup(tp, name);
+ }
+
+ if (descr != NULL) {
+ descrgetfunc f = TP_DESCR_GET(descr->ob_type);
+ if (f != NULL)
+ return f(descr, obj, (PyObject *)obj->ob_type);
+ else {
+ Py_INCREF(descr);
+ return descr;
+ }
+ }
+
+ return PyObject_GetAttr(im->im_func, name);
}
PyDoc_STRVAR(method_doc,
@@ -139,241 +139,241 @@ Create a bound instance method object.");
static PyObject *
method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
{
- PyObject *func;
- PyObject *self;
-
- if (!_PyArg_NoKeywords("method", kw))
- return NULL;
- if (!PyArg_UnpackTuple(args, "method", 2, 2,
- &func, &self))
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError,
- "first argument must be callable");
- return NULL;
- }
- if (self == NULL || self == Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "self must not be None");
- return NULL;
- }
-
- return PyMethod_New(func, self);
+ PyObject *func;
+ PyObject *self;
+
+ if (!_PyArg_NoKeywords("method", kw))
+ return NULL;
+ if (!PyArg_UnpackTuple(args, "method", 2, 2,
+ &func, &self))
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError,
+ "first argument must be callable");
+ return NULL;
+ }
+ if (self == NULL || self == Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "self must not be None");
+ return NULL;
+ }
+
+ return PyMethod_New(func, self);
}
static void
method_dealloc(register PyMethodObject *im)
{
- _PyObject_GC_UNTRACK(im);
- if (im->im_weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *)im);
- Py_DECREF(im->im_func);
- Py_XDECREF(im->im_self);
- if (numfree < PyMethod_MAXFREELIST) {
- im->im_self = (PyObject *)free_list;
- free_list = im;
- numfree++;
- }
- else {
- PyObject_GC_Del(im);
- }
+ _PyObject_GC_UNTRACK(im);
+ if (im->im_weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *)im);
+ Py_DECREF(im->im_func);
+ Py_XDECREF(im->im_self);
+ if (numfree < PyMethod_MAXFREELIST) {
+ im->im_self = (PyObject *)free_list;
+ free_list = im;
+ numfree++;
+ }
+ else {
+ PyObject_GC_Del(im);
+ }
}
static PyObject *
method_richcompare(PyObject *self, PyObject *other, int op)
{
- PyMethodObject *a, *b;
- PyObject *res;
- int eq;
-
- if ((op != Py_EQ && op != Py_NE) ||
- !PyMethod_Check(self) ||
- !PyMethod_Check(other))
- {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- a = (PyMethodObject *)self;
- b = (PyMethodObject *)other;
- eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
- if (eq == 1) {
- if (a->im_self == NULL || b->im_self == NULL)
- eq = a->im_self == b->im_self;
- else
- eq = PyObject_RichCompareBool(a->im_self, b->im_self,
- Py_EQ);
- }
- if (eq < 0)
- return NULL;
- if (op == Py_EQ)
- res = eq ? Py_True : Py_False;
- else
- res = eq ? Py_False : Py_True;
- Py_INCREF(res);
- return res;
+ PyMethodObject *a, *b;
+ PyObject *res;
+ int eq;
+
+ if ((op != Py_EQ && op != Py_NE) ||
+ !PyMethod_Check(self) ||
+ !PyMethod_Check(other))
+ {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ a = (PyMethodObject *)self;
+ b = (PyMethodObject *)other;
+ eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
+ if (eq == 1) {
+ if (a->im_self == NULL || b->im_self == NULL)
+ eq = a->im_self == b->im_self;
+ else
+ eq = PyObject_RichCompareBool(a->im_self, b->im_self,
+ Py_EQ);
+ }
+ if (eq < 0)
+ return NULL;
+ if (op == Py_EQ)
+ res = eq ? Py_True : Py_False;
+ else
+ res = eq ? Py_False : Py_True;
+ Py_INCREF(res);
+ return res;
}
static PyObject *
method_repr(PyMethodObject *a)
{
- PyObject *self = a->im_self;
- PyObject *func = a->im_func;
- PyObject *klass = (PyObject*)Py_TYPE(self);
- PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
- char *defname = "?";
-
- if (self == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- funcname = PyObject_GetAttrString(func, "__name__");
- if (funcname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- }
- else if (!PyUnicode_Check(funcname)) {
- Py_DECREF(funcname);
- funcname = NULL;
- }
-
- if (klass == NULL)
- klassname = NULL;
- else {
- klassname = PyObject_GetAttrString(klass, "__name__");
- if (klassname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- }
- else if (!PyUnicode_Check(klassname)) {
- Py_DECREF(klassname);
- klassname = NULL;
- }
- }
-
- /* XXX Shouldn't use repr()/%R here! */
- result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
- klassname, defname,
- funcname, defname, self);
-
- Py_XDECREF(funcname);
- Py_XDECREF(klassname);
- return result;
+ PyObject *self = a->im_self;
+ PyObject *func = a->im_func;
+ PyObject *klass = (PyObject*)Py_TYPE(self);
+ PyObject *funcname = NULL ,*klassname = NULL, *result = NULL;
+ char *defname = "?";
+
+ if (self == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ funcname = PyObject_GetAttrString(func, "__name__");
+ if (funcname == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return NULL;
+ PyErr_Clear();
+ }
+ else if (!PyUnicode_Check(funcname)) {
+ Py_DECREF(funcname);
+ funcname = NULL;
+ }
+
+ if (klass == NULL)
+ klassname = NULL;
+ else {
+ klassname = PyObject_GetAttrString(klass, "__name__");
+ if (klassname == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return NULL;
+ PyErr_Clear();
+ }
+ else if (!PyUnicode_Check(klassname)) {
+ Py_DECREF(klassname);
+ klassname = NULL;
+ }
+ }
+
+ /* XXX Shouldn't use repr()/%R here! */
+ result = PyUnicode_FromFormat("<bound method %V.%V of %R>",
+ klassname, defname,
+ funcname, defname, self);
+
+ Py_XDECREF(funcname);
+ Py_XDECREF(klassname);
+ return result;
}
static long
method_hash(PyMethodObject *a)
{
- long x, y;
- if (a->im_self == NULL)
- x = PyObject_Hash(Py_None);
- else
- x = PyObject_Hash(a->im_self);
- if (x == -1)
- return -1;
- y = PyObject_Hash(a->im_func);
- if (y == -1)
- return -1;
- x = x ^ y;
- if (x == -1)
- x = -2;
- return x;
+ long x, y;
+ if (a->im_self == NULL)
+ x = PyObject_Hash(Py_None);
+ else
+ x = PyObject_Hash(a->im_self);
+ if (x == -1)
+ return -1;
+ y = PyObject_Hash(a->im_func);
+ if (y == -1)
+ return -1;
+ x = x ^ y;
+ if (x == -1)
+ x = -2;
+ return x;
}
static int
method_traverse(PyMethodObject *im, visitproc visit, void *arg)
{
- Py_VISIT(im->im_func);
- Py_VISIT(im->im_self);
- return 0;
+ Py_VISIT(im->im_func);
+ Py_VISIT(im->im_self);
+ return 0;
}
static PyObject *
method_call(PyObject *func, PyObject *arg, PyObject *kw)
{
- PyObject *self = PyMethod_GET_SELF(func);
- PyObject *result;
-
- func = PyMethod_GET_FUNCTION(func);
- if (self == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
- else {
- Py_ssize_t argcount = PyTuple_Size(arg);
- PyObject *newarg = PyTuple_New(argcount + 1);
- int i;
- if (newarg == NULL)
- return NULL;
- Py_INCREF(self);
- PyTuple_SET_ITEM(newarg, 0, self);
- for (i = 0; i < argcount; i++) {
- PyObject *v = PyTuple_GET_ITEM(arg, i);
- Py_XINCREF(v);
- PyTuple_SET_ITEM(newarg, i+1, v);
- }
- arg = newarg;
- }
- result = PyObject_Call((PyObject *)func, arg, kw);
- Py_DECREF(arg);
- return result;
+ PyObject *self = PyMethod_GET_SELF(func);
+ PyObject *result;
+
+ func = PyMethod_GET_FUNCTION(func);
+ if (self == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ else {
+ Py_ssize_t argcount = PyTuple_Size(arg);
+ PyObject *newarg = PyTuple_New(argcount + 1);
+ int i;
+ if (newarg == NULL)
+ return NULL;
+ Py_INCREF(self);
+ PyTuple_SET_ITEM(newarg, 0, self);
+ for (i = 0; i < argcount; i++) {
+ PyObject *v = PyTuple_GET_ITEM(arg, i);
+ Py_XINCREF(v);
+ PyTuple_SET_ITEM(newarg, i+1, v);
+ }
+ arg = newarg;
+ }
+ result = PyObject_Call((PyObject *)func, arg, kw);
+ Py_DECREF(arg);
+ return result;
}
static PyObject *
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
{
- /* Don't rebind an already bound method of a class that's not a base
- class of cls. */
- if (PyMethod_GET_SELF(meth) != NULL) {
- /* Already bound */
- Py_INCREF(meth);
- return meth;
- }
- /* Bind it to obj */
- return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
+ /* Don't rebind an already bound method of a class that's not a base
+ class of cls. */
+ if (PyMethod_GET_SELF(meth) != NULL) {
+ /* Already bound */
+ Py_INCREF(meth);
+ return meth;
+ }
+ /* Bind it to obj */
+ return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj);
}
PyTypeObject PyMethod_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "method",
- sizeof(PyMethodObject),
- 0,
- (destructor)method_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)method_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)method_hash, /* tp_hash */
- method_call, /* tp_call */
- 0, /* tp_str */
- method_getattro, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- method_doc, /* tp_doc */
- (traverseproc)method_traverse, /* tp_traverse */
- 0, /* tp_clear */
- method_richcompare, /* tp_richcompare */
- offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- method_memberlist, /* tp_members */
- method_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- method_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- method_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "method",
+ sizeof(PyMethodObject),
+ 0,
+ (destructor)method_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)method_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)method_hash, /* tp_hash */
+ method_call, /* tp_call */
+ 0, /* tp_str */
+ method_getattro, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ method_doc, /* tp_doc */
+ (traverseproc)method_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ method_richcompare, /* tp_richcompare */
+ offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ method_memberlist, /* tp_members */
+ method_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ method_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ method_new, /* tp_new */
};
/* Clear out the free list */
@@ -381,22 +381,22 @@ PyTypeObject PyMethod_Type = {
int
PyMethod_ClearFreeList(void)
{
- int freelist_size = numfree;
-
- while (free_list) {
- PyMethodObject *im = free_list;
- free_list = (PyMethodObject *)(im->im_self);
- PyObject_GC_Del(im);
- numfree--;
- }
- assert(numfree == 0);
- return freelist_size;
+ int freelist_size = numfree;
+
+ while (free_list) {
+ PyMethodObject *im = free_list;
+ free_list = (PyMethodObject *)(im->im_self);
+ PyObject_GC_Del(im);
+ numfree--;
+ }
+ assert(numfree == 0);
+ return freelist_size;
}
void
PyMethod_Fini(void)
{
- (void)PyMethod_ClearFreeList();
+ (void)PyMethod_ClearFreeList();
}
/* ------------------------------------------------------------------------
@@ -405,176 +405,176 @@ PyMethod_Fini(void)
PyObject *
PyInstanceMethod_New(PyObject *func) {
- PyInstanceMethodObject *method;
- method = PyObject_GC_New(PyInstanceMethodObject,
- &PyInstanceMethod_Type);
- if (method == NULL) return NULL;
- Py_INCREF(func);
- method->func = func;
- _PyObject_GC_TRACK(method);
- return (PyObject *)method;
+ PyInstanceMethodObject *method;
+ method = PyObject_GC_New(PyInstanceMethodObject,
+ &PyInstanceMethod_Type);
+ if (method == NULL) return NULL;
+ Py_INCREF(func);
+ method->func = func;
+ _PyObject_GC_TRACK(method);
+ return (PyObject *)method;
}
PyObject *
PyInstanceMethod_Function(PyObject *im)
{
- if (!PyInstanceMethod_Check(im)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return PyInstanceMethod_GET_FUNCTION(im);
+ if (!PyInstanceMethod_Check(im)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return PyInstanceMethod_GET_FUNCTION(im);
}
#define IMO_OFF(x) offsetof(PyInstanceMethodObject, x)
static PyMemberDef instancemethod_memberlist[] = {
- {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
- "the function (or other callable) implementing a method"},
- {NULL} /* Sentinel */
+ {"__func__", T_OBJECT, IMO_OFF(func), READONLY|RESTRICTED,
+ "the function (or other callable) implementing a method"},
+ {NULL} /* Sentinel */
};
static PyObject *
instancemethod_get_doc(PyObject *self, void *context)
{
- static PyObject *docstr;
- if (docstr == NULL) {
- docstr = PyUnicode_InternFromString("__doc__");
- if (docstr == NULL)
- return NULL;
- }
- return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
+ static PyObject *docstr;
+ if (docstr == NULL) {
+ docstr = PyUnicode_InternFromString("__doc__");
+ if (docstr == NULL)
+ return NULL;
+ }
+ return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), docstr);
}
static PyGetSetDef instancemethod_getset[] = {
- {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
- {0}
+ {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
+ {0}
};
static PyObject *
instancemethod_getattro(PyObject *self, PyObject *name)
{
- PyTypeObject *tp = self->ob_type;
- PyObject *descr = NULL;
-
- if (tp->tp_dict == NULL) {
- if (PyType_Ready(tp) < 0)
- return NULL;
- }
- descr = _PyType_Lookup(tp, name);
-
- if (descr != NULL) {
- descrgetfunc f = TP_DESCR_GET(descr->ob_type);
- if (f != NULL)
- return f(descr, self, (PyObject *)self->ob_type);
- else {
- Py_INCREF(descr);
- return descr;
- }
- }
-
- return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
+ PyTypeObject *tp = self->ob_type;
+ PyObject *descr = NULL;
+
+ if (tp->tp_dict == NULL) {
+ if (PyType_Ready(tp) < 0)
+ return NULL;
+ }
+ descr = _PyType_Lookup(tp, name);
+
+ if (descr != NULL) {
+ descrgetfunc f = TP_DESCR_GET(descr->ob_type);
+ if (f != NULL)
+ return f(descr, self, (PyObject *)self->ob_type);
+ else {
+ Py_INCREF(descr);
+ return descr;
+ }
+ }
+
+ return PyObject_GetAttr(PyInstanceMethod_GET_FUNCTION(self), name);
}
static void
instancemethod_dealloc(PyObject *self) {
- _PyObject_GC_UNTRACK(self);
- Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
- PyObject_GC_Del(self);
+ _PyObject_GC_UNTRACK(self);
+ Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
+ PyObject_GC_Del(self);
}
static int
instancemethod_traverse(PyObject *self, visitproc visit, void *arg) {
- Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
- return 0;
+ Py_VISIT(PyInstanceMethod_GET_FUNCTION(self));
+ return 0;
}
static PyObject *
instancemethod_call(PyObject *self, PyObject *arg, PyObject *kw)
{
- return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
+ return PyObject_Call(PyMethod_GET_FUNCTION(self), arg, kw);
}
static PyObject *
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
- register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
- if (obj == NULL) {
- Py_INCREF(func);
- return func;
- }
- else
- return PyMethod_New(func, obj);
+ register PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
+ if (obj == NULL) {
+ Py_INCREF(func);
+ return func;
+ }
+ else
+ return PyMethod_New(func, obj);
}
static PyObject *
instancemethod_richcompare(PyObject *self, PyObject *other, int op)
{
- PyInstanceMethodObject *a, *b;
- PyObject *res;
- int eq;
-
- if ((op != Py_EQ && op != Py_NE) ||
- !PyInstanceMethod_Check(self) ||
- !PyInstanceMethod_Check(other))
- {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- a = (PyInstanceMethodObject *)self;
- b = (PyInstanceMethodObject *)other;
- eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
- if (eq < 0)
- return NULL;
- if (op == Py_EQ)
- res = eq ? Py_True : Py_False;
- else
- res = eq ? Py_False : Py_True;
- Py_INCREF(res);
- return res;
+ PyInstanceMethodObject *a, *b;
+ PyObject *res;
+ int eq;
+
+ if ((op != Py_EQ && op != Py_NE) ||
+ !PyInstanceMethod_Check(self) ||
+ !PyInstanceMethod_Check(other))
+ {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ a = (PyInstanceMethodObject *)self;
+ b = (PyInstanceMethodObject *)other;
+ eq = PyObject_RichCompareBool(a->func, b->func, Py_EQ);
+ if (eq < 0)
+ return NULL;
+ if (op == Py_EQ)
+ res = eq ? Py_True : Py_False;
+ else
+ res = eq ? Py_False : Py_True;
+ Py_INCREF(res);
+ return res;
}
static PyObject *
instancemethod_repr(PyObject *self)
{
- PyObject *func = PyInstanceMethod_Function(self);
- PyObject *funcname = NULL , *result = NULL;
- char *defname = "?";
-
- if (func == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- funcname = PyObject_GetAttrString(func, "__name__");
- if (funcname == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return NULL;
- PyErr_Clear();
- }
- else if (!PyUnicode_Check(funcname)) {
- Py_DECREF(funcname);
- funcname = NULL;
- }
-
- result = PyUnicode_FromFormat("<instancemethod %V at %p>",
- funcname, defname, self);
-
- Py_XDECREF(funcname);
- return result;
+ PyObject *func = PyInstanceMethod_Function(self);
+ PyObject *funcname = NULL , *result = NULL;
+ char *defname = "?";
+
+ if (func == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ funcname = PyObject_GetAttrString(func, "__name__");
+ if (funcname == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return NULL;
+ PyErr_Clear();
+ }
+ else if (!PyUnicode_Check(funcname)) {
+ Py_DECREF(funcname);
+ funcname = NULL;
+ }
+
+ result = PyUnicode_FromFormat("<instancemethod %V at %p>",
+ funcname, defname, self);
+
+ Py_XDECREF(funcname);
+ return result;
}
/*
static long
instancemethod_hash(PyObject *self)
{
- long x, y;
- x = (long)self;
- y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
- if (y == -1)
- return -1;
- x = x ^ y;
- if (x == -1)
- x = -2;
- return x;
+ long x, y;
+ x = (long)self;
+ y = PyObject_Hash(PyInstanceMethod_GET_FUNCTION(self));
+ if (y == -1)
+ return -1;
+ x = x ^ y;
+ if (x == -1)
+ x = -2;
+ return x;
}
*/
@@ -586,59 +586,59 @@ Bind a function to a class.");
static PyObject *
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
{
- PyObject *func;
-
- if (!_PyArg_NoKeywords("instancemethod", kw))
- return NULL;
- if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
- return NULL;
- if (!PyCallable_Check(func)) {
- PyErr_SetString(PyExc_TypeError,
- "first argument must be callable");
- return NULL;
- }
-
- return PyInstanceMethod_New(func);
+ PyObject *func;
+
+ if (!_PyArg_NoKeywords("instancemethod", kw))
+ return NULL;
+ if (!PyArg_UnpackTuple(args, "instancemethod", 1, 1, &func))
+ return NULL;
+ if (!PyCallable_Check(func)) {
+ PyErr_SetString(PyExc_TypeError,
+ "first argument must be callable");
+ return NULL;
+ }
+
+ return PyInstanceMethod_New(func);
}
PyTypeObject PyInstanceMethod_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "instancemethod", /* tp_name */
- sizeof(PyInstanceMethodObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- instancemethod_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)instancemethod_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /*(hashfunc)instancemethod_hash, tp_hash */
- instancemethod_call, /* tp_call */
- 0, /* tp_str */
- instancemethod_getattro, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT
- | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- instancemethod_doc, /* tp_doc */
- instancemethod_traverse, /* tp_traverse */
- 0, /* tp_clear */
- instancemethod_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- instancemethod_memberlist, /* tp_members */
- instancemethod_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- instancemethod_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- instancemethod_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "instancemethod", /* tp_name */
+ sizeof(PyInstanceMethodObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ instancemethod_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)instancemethod_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /*(hashfunc)instancemethod_hash, tp_hash */
+ instancemethod_call, /* tp_call */
+ 0, /* tp_str */
+ instancemethod_getattro, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT
+ | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ instancemethod_doc, /* tp_doc */
+ instancemethod_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ instancemethod_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ instancemethod_memberlist, /* tp_members */
+ instancemethod_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ instancemethod_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ instancemethod_new, /* tp_new */
};
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 832b4e9..e3eccf7 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -3,135 +3,135 @@
#include "structmember.h"
#define NAME_CHARS \
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
static int
all_name_chars(Py_UNICODE *s)
{
- static char ok_name_char[256];
- static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
-
- if (ok_name_char[*name_chars] == 0) {
- unsigned char *p;
- for (p = name_chars; *p; p++)
- ok_name_char[*p] = 1;
- }
- while (*s) {
- if (*s >= 128)
- return 0;
- if (ok_name_char[*s++] == 0)
- return 0;
- }
- return 1;
+ static char ok_name_char[256];
+ static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
+
+ if (ok_name_char[*name_chars] == 0) {
+ unsigned char *p;
+ for (p = name_chars; *p; p++)
+ ok_name_char[*p] = 1;
+ }
+ while (*s) {
+ if (*s >= 128)
+ return 0;
+ if (ok_name_char[*s++] == 0)
+ return 0;
+ }
+ return 1;
}
static void
intern_strings(PyObject *tuple)
{
- Py_ssize_t i;
-
- for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
- PyObject *v = PyTuple_GET_ITEM(tuple, i);
- if (v == NULL || !PyUnicode_CheckExact(v)) {
- Py_FatalError("non-string found in code slot");
- }
- PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
- }
+ Py_ssize_t i;
+
+ for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
+ PyObject *v = PyTuple_GET_ITEM(tuple, i);
+ if (v == NULL || !PyUnicode_CheckExact(v)) {
+ Py_FatalError("non-string found in code slot");
+ }
+ PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
+ }
}
PyCodeObject *
PyCode_New(int argcount, int kwonlyargcount,
- int nlocals, int stacksize, int flags,
- PyObject *code, PyObject *consts, PyObject *names,
- PyObject *varnames, PyObject *freevars, PyObject *cellvars,
- PyObject *filename, PyObject *name, int firstlineno,
- PyObject *lnotab)
+ int nlocals, int stacksize, int flags,
+ PyObject *code, PyObject *consts, PyObject *names,
+ PyObject *varnames, PyObject *freevars, PyObject *cellvars,
+ PyObject *filename, PyObject *name, int firstlineno,
+ PyObject *lnotab)
{
- PyCodeObject *co;
- Py_ssize_t i;
-
- /* Check argument types */
- if (argcount < 0 || nlocals < 0 ||
- code == NULL ||
- consts == NULL || !PyTuple_Check(consts) ||
- names == NULL || !PyTuple_Check(names) ||
- varnames == NULL || !PyTuple_Check(varnames) ||
- freevars == NULL || !PyTuple_Check(freevars) ||
- cellvars == NULL || !PyTuple_Check(cellvars) ||
- name == NULL || !PyUnicode_Check(name) ||
- filename == NULL || !PyUnicode_Check(filename) ||
- lnotab == NULL || !PyBytes_Check(lnotab) ||
- !PyObject_CheckReadBuffer(code)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- intern_strings(names);
- intern_strings(varnames);
- intern_strings(freevars);
- intern_strings(cellvars);
- /* Intern selected string constants */
- for (i = PyTuple_Size(consts); --i >= 0; ) {
- PyObject *v = PyTuple_GetItem(consts, i);
- if (!PyUnicode_Check(v))
- continue;
- if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
- continue;
- PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
- }
- co = PyObject_NEW(PyCodeObject, &PyCode_Type);
- if (co != NULL) {
- co->co_argcount = argcount;
- co->co_kwonlyargcount = kwonlyargcount;
- co->co_nlocals = nlocals;
- co->co_stacksize = stacksize;
- co->co_flags = flags;
- Py_INCREF(code);
- co->co_code = code;
- Py_INCREF(consts);
- co->co_consts = consts;
- Py_INCREF(names);
- co->co_names = names;
- Py_INCREF(varnames);
- co->co_varnames = varnames;
- Py_INCREF(freevars);
- co->co_freevars = freevars;
- Py_INCREF(cellvars);
- co->co_cellvars = cellvars;
- Py_INCREF(filename);
- co->co_filename = filename;
- Py_INCREF(name);
- co->co_name = name;
- co->co_firstlineno = firstlineno;
- Py_INCREF(lnotab);
- co->co_lnotab = lnotab;
- co->co_zombieframe = NULL;
- }
- return co;
+ PyCodeObject *co;
+ Py_ssize_t i;
+
+ /* Check argument types */
+ if (argcount < 0 || nlocals < 0 ||
+ code == NULL ||
+ consts == NULL || !PyTuple_Check(consts) ||
+ names == NULL || !PyTuple_Check(names) ||
+ varnames == NULL || !PyTuple_Check(varnames) ||
+ freevars == NULL || !PyTuple_Check(freevars) ||
+ cellvars == NULL || !PyTuple_Check(cellvars) ||
+ name == NULL || !PyUnicode_Check(name) ||
+ filename == NULL || !PyUnicode_Check(filename) ||
+ lnotab == NULL || !PyBytes_Check(lnotab) ||
+ !PyObject_CheckReadBuffer(code)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ intern_strings(names);
+ intern_strings(varnames);
+ intern_strings(freevars);
+ intern_strings(cellvars);
+ /* Intern selected string constants */
+ for (i = PyTuple_Size(consts); --i >= 0; ) {
+ PyObject *v = PyTuple_GetItem(consts, i);
+ if (!PyUnicode_Check(v))
+ continue;
+ if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
+ continue;
+ PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
+ }
+ co = PyObject_NEW(PyCodeObject, &PyCode_Type);
+ if (co != NULL) {
+ co->co_argcount = argcount;
+ co->co_kwonlyargcount = kwonlyargcount;
+ co->co_nlocals = nlocals;
+ co->co_stacksize = stacksize;
+ co->co_flags = flags;
+ Py_INCREF(code);
+ co->co_code = code;
+ Py_INCREF(consts);
+ co->co_consts = consts;
+ Py_INCREF(names);
+ co->co_names = names;
+ Py_INCREF(varnames);
+ co->co_varnames = varnames;
+ Py_INCREF(freevars);
+ co->co_freevars = freevars;
+ Py_INCREF(cellvars);
+ co->co_cellvars = cellvars;
+ Py_INCREF(filename);
+ co->co_filename = filename;
+ Py_INCREF(name);
+ co->co_name = name;
+ co->co_firstlineno = firstlineno;
+ Py_INCREF(lnotab);
+ co->co_lnotab = lnotab;
+ co->co_zombieframe = NULL;
+ }
+ return co;
}
#define OFF(x) offsetof(PyCodeObject, x)
static PyMemberDef code_memberlist[] = {
- {"co_argcount", T_INT, OFF(co_argcount), READONLY},
- {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
- {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
- {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
- {"co_flags", T_INT, OFF(co_flags), READONLY},
- {"co_code", T_OBJECT, OFF(co_code), READONLY},
- {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
- {"co_names", T_OBJECT, OFF(co_names), READONLY},
- {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
- {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
- {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
- {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
- {"co_name", T_OBJECT, OFF(co_name), READONLY},
- {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
- {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
- {NULL} /* Sentinel */
+ {"co_argcount", T_INT, OFF(co_argcount), READONLY},
+ {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
+ {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
+ {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
+ {"co_flags", T_INT, OFF(co_flags), READONLY},
+ {"co_code", T_OBJECT, OFF(co_code), READONLY},
+ {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
+ {"co_names", T_OBJECT, OFF(co_names), READONLY},
+ {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
+ {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
+ {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
+ {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
+ {"co_name", T_OBJECT, OFF(co_name), READONLY},
+ {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
+ {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
+ {NULL} /* Sentinel */
};
/* Helper for code_new: return a shallow copy of a tuple that is
@@ -140,42 +140,42 @@ static PyMemberDef code_memberlist[] = {
static PyObject*
validate_and_copy_tuple(PyObject *tup)
{
- PyObject *newtuple;
- PyObject *item;
- Py_ssize_t i, len;
-
- len = PyTuple_GET_SIZE(tup);
- newtuple = PyTuple_New(len);
- if (newtuple == NULL)
- return NULL;
-
- for (i = 0; i < len; i++) {
- item = PyTuple_GET_ITEM(tup, i);
- if (PyUnicode_CheckExact(item)) {
- Py_INCREF(item);
- }
- else if (!PyUnicode_Check(item)) {
- PyErr_Format(
- PyExc_TypeError,
- "name tuples must contain only "
- "strings, not '%.500s'",
- item->ob_type->tp_name);
- Py_DECREF(newtuple);
- return NULL;
- }
- else {
- item = PyUnicode_FromUnicode(
- PyUnicode_AS_UNICODE(item),
- PyUnicode_GET_SIZE(item));
- if (item == NULL) {
- Py_DECREF(newtuple);
- return NULL;
- }
- }
- PyTuple_SET_ITEM(newtuple, i, item);
- }
-
- return newtuple;
+ PyObject *newtuple;
+ PyObject *item;
+ Py_ssize_t i, len;
+
+ len = PyTuple_GET_SIZE(tup);
+ newtuple = PyTuple_New(len);
+ if (newtuple == NULL)
+ return NULL;
+
+ for (i = 0; i < len; i++) {
+ item = PyTuple_GET_ITEM(tup, i);
+ if (PyUnicode_CheckExact(item)) {
+ Py_INCREF(item);
+ }
+ else if (!PyUnicode_Check(item)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "name tuples must contain only "
+ "strings, not '%.500s'",
+ item->ob_type->tp_name);
+ Py_DECREF(newtuple);
+ return NULL;
+ }
+ else {
+ item = PyUnicode_FromUnicode(
+ PyUnicode_AS_UNICODE(item),
+ PyUnicode_GET_SIZE(item));
+ if (item == NULL) {
+ Py_DECREF(newtuple);
+ return NULL;
+ }
+ }
+ PyTuple_SET_ITEM(newtuple, i, item);
+ }
+
+ return newtuple;
}
PyDoc_STRVAR(code_doc,
@@ -188,247 +188,247 @@ Create a code object. Not for the faint of heart.");
static PyObject *
code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- int argcount;
- int kwonlyargcount;
- int nlocals;
- int stacksize;
- int flags;
- PyObject *co = NULL;
- PyObject *code;
- PyObject *consts;
- PyObject *names, *ournames = NULL;
- PyObject *varnames, *ourvarnames = NULL;
- PyObject *freevars = NULL, *ourfreevars = NULL;
- PyObject *cellvars = NULL, *ourcellvars = NULL;
- PyObject *filename;
- PyObject *name;
- int firstlineno;
- PyObject *lnotab;
-
- if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
- &argcount, &kwonlyargcount,
- &nlocals, &stacksize, &flags,
- &code,
- &PyTuple_Type, &consts,
- &PyTuple_Type, &names,
- &PyTuple_Type, &varnames,
- &filename, &name,
- &firstlineno, &lnotab,
- &PyTuple_Type, &freevars,
- &PyTuple_Type, &cellvars))
- return NULL;
-
- if (argcount < 0) {
- PyErr_SetString(
- PyExc_ValueError,
- "code: argcount must not be negative");
- goto cleanup;
- }
-
- if (kwonlyargcount < 0) {
- PyErr_SetString(
- PyExc_ValueError,
- "code: kwonlyargcount must not be negative");
- goto cleanup;
- }
- if (nlocals < 0) {
- PyErr_SetString(
- PyExc_ValueError,
- "code: nlocals must not be negative");
- goto cleanup;
- }
-
- ournames = validate_and_copy_tuple(names);
- if (ournames == NULL)
- goto cleanup;
- ourvarnames = validate_and_copy_tuple(varnames);
- if (ourvarnames == NULL)
- goto cleanup;
- if (freevars)
- ourfreevars = validate_and_copy_tuple(freevars);
- else
- ourfreevars = PyTuple_New(0);
- if (ourfreevars == NULL)
- goto cleanup;
- if (cellvars)
- ourcellvars = validate_and_copy_tuple(cellvars);
- else
- ourcellvars = PyTuple_New(0);
- if (ourcellvars == NULL)
- goto cleanup;
-
- co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
- nlocals, stacksize, flags,
- code, consts, ournames, ourvarnames,
- ourfreevars, ourcellvars, filename,
- name, firstlineno, lnotab);
+ int argcount;
+ int kwonlyargcount;
+ int nlocals;
+ int stacksize;
+ int flags;
+ PyObject *co = NULL;
+ PyObject *code;
+ PyObject *consts;
+ PyObject *names, *ournames = NULL;
+ PyObject *varnames, *ourvarnames = NULL;
+ PyObject *freevars = NULL, *ourfreevars = NULL;
+ PyObject *cellvars = NULL, *ourcellvars = NULL;
+ PyObject *filename;
+ PyObject *name;
+ int firstlineno;
+ PyObject *lnotab;
+
+ if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
+ &argcount, &kwonlyargcount,
+ &nlocals, &stacksize, &flags,
+ &code,
+ &PyTuple_Type, &consts,
+ &PyTuple_Type, &names,
+ &PyTuple_Type, &varnames,
+ &filename, &name,
+ &firstlineno, &lnotab,
+ &PyTuple_Type, &freevars,
+ &PyTuple_Type, &cellvars))
+ return NULL;
+
+ if (argcount < 0) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "code: argcount must not be negative");
+ goto cleanup;
+ }
+
+ if (kwonlyargcount < 0) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "code: kwonlyargcount must not be negative");
+ goto cleanup;
+ }
+ if (nlocals < 0) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "code: nlocals must not be negative");
+ goto cleanup;
+ }
+
+ ournames = validate_and_copy_tuple(names);
+ if (ournames == NULL)
+ goto cleanup;
+ ourvarnames = validate_and_copy_tuple(varnames);
+ if (ourvarnames == NULL)
+ goto cleanup;
+ if (freevars)
+ ourfreevars = validate_and_copy_tuple(freevars);
+ else
+ ourfreevars = PyTuple_New(0);
+ if (ourfreevars == NULL)
+ goto cleanup;
+ if (cellvars)
+ ourcellvars = validate_and_copy_tuple(cellvars);
+ else
+ ourcellvars = PyTuple_New(0);
+ if (ourcellvars == NULL)
+ goto cleanup;
+
+ co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
+ nlocals, stacksize, flags,
+ code, consts, ournames, ourvarnames,
+ ourfreevars, ourcellvars, filename,
+ name, firstlineno, lnotab);
cleanup:
- Py_XDECREF(ournames);
- Py_XDECREF(ourvarnames);
- Py_XDECREF(ourfreevars);
- Py_XDECREF(ourcellvars);
- return co;
+ Py_XDECREF(ournames);
+ Py_XDECREF(ourvarnames);
+ Py_XDECREF(ourfreevars);
+ Py_XDECREF(ourcellvars);
+ return co;
}
static void
code_dealloc(PyCodeObject *co)
{
- Py_XDECREF(co->co_code);
- Py_XDECREF(co->co_consts);
- Py_XDECREF(co->co_names);
- Py_XDECREF(co->co_varnames);
- Py_XDECREF(co->co_freevars);
- Py_XDECREF(co->co_cellvars);
- Py_XDECREF(co->co_filename);
- Py_XDECREF(co->co_name);
- Py_XDECREF(co->co_lnotab);
- if (co->co_zombieframe != NULL)
- PyObject_GC_Del(co->co_zombieframe);
- PyObject_DEL(co);
+ Py_XDECREF(co->co_code);
+ Py_XDECREF(co->co_consts);
+ Py_XDECREF(co->co_names);
+ Py_XDECREF(co->co_varnames);
+ Py_XDECREF(co->co_freevars);
+ Py_XDECREF(co->co_cellvars);
+ Py_XDECREF(co->co_filename);
+ Py_XDECREF(co->co_name);
+ Py_XDECREF(co->co_lnotab);
+ if (co->co_zombieframe != NULL)
+ PyObject_GC_Del(co->co_zombieframe);
+ PyObject_DEL(co);
}
static PyObject *
code_repr(PyCodeObject *co)
{
- int lineno = -1;
- char *filename = "???";
-
- if (co->co_firstlineno != 0)
- lineno = co->co_firstlineno;
- if (co->co_filename && PyUnicode_Check(co->co_filename))
- filename = _PyUnicode_AsString(co->co_filename);
- return PyUnicode_FromFormat(
- "<code object %.100U at %p, file \"%.300s\", line %d>",
- co->co_name, co, filename, lineno);
+ int lineno = -1;
+ char *filename = "???";
+
+ if (co->co_firstlineno != 0)
+ lineno = co->co_firstlineno;
+ if (co->co_filename && PyUnicode_Check(co->co_filename))
+ filename = _PyUnicode_AsString(co->co_filename);
+ return PyUnicode_FromFormat(
+ "<code object %.100U at %p, file \"%.300s\", line %d>",
+ co->co_name, co, filename, lineno);
}
static PyObject *
code_richcompare(PyObject *self, PyObject *other, int op)
{
- PyCodeObject *co, *cp;
- int eq;
- PyObject *res;
-
- if ((op != Py_EQ && op != Py_NE) ||
- !PyCode_Check(self) ||
- !PyCode_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- co = (PyCodeObject *)self;
- cp = (PyCodeObject *)other;
-
- eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = co->co_argcount == cp->co_argcount;
- if (!eq) goto unequal;
- eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
- if (!eq) goto unequal;
- eq = co->co_nlocals == cp->co_nlocals;
- if (!eq) goto unequal;
- eq = co->co_flags == cp->co_flags;
- if (!eq) goto unequal;
- eq = co->co_firstlineno == cp->co_firstlineno;
- if (!eq) goto unequal;
- eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
- if (eq <= 0) goto unequal;
- eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
- if (eq <= 0) goto unequal;
-
- if (op == Py_EQ)
- res = Py_True;
- else
- res = Py_False;
- goto done;
+ PyCodeObject *co, *cp;
+ int eq;
+ PyObject *res;
+
+ if ((op != Py_EQ && op != Py_NE) ||
+ !PyCode_Check(self) ||
+ !PyCode_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ co = (PyCodeObject *)self;
+ cp = (PyCodeObject *)other;
+
+ eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = co->co_argcount == cp->co_argcount;
+ if (!eq) goto unequal;
+ eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
+ if (!eq) goto unequal;
+ eq = co->co_nlocals == cp->co_nlocals;
+ if (!eq) goto unequal;
+ eq = co->co_flags == cp->co_flags;
+ if (!eq) goto unequal;
+ eq = co->co_firstlineno == cp->co_firstlineno;
+ if (!eq) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
+ if (eq <= 0) goto unequal;
+ eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
+ if (eq <= 0) goto unequal;
+
+ if (op == Py_EQ)
+ res = Py_True;
+ else
+ res = Py_False;
+ goto done;
unequal:
- if (eq < 0)
- return NULL;
- if (op == Py_NE)
- res = Py_True;
- else
- res = Py_False;
+ if (eq < 0)
+ return NULL;
+ if (op == Py_NE)
+ res = Py_True;
+ else
+ res = Py_False;
done:
- Py_INCREF(res);
- return res;
+ Py_INCREF(res);
+ return res;
}
static long
code_hash(PyCodeObject *co)
{
- long h, h0, h1, h2, h3, h4, h5, h6;
- h0 = PyObject_Hash(co->co_name);
- if (h0 == -1) return -1;
- h1 = PyObject_Hash(co->co_code);
- if (h1 == -1) return -1;
- h2 = PyObject_Hash(co->co_consts);
- if (h2 == -1) return -1;
- h3 = PyObject_Hash(co->co_names);
- if (h3 == -1) return -1;
- h4 = PyObject_Hash(co->co_varnames);
- if (h4 == -1) return -1;
- h5 = PyObject_Hash(co->co_freevars);
- if (h5 == -1) return -1;
- h6 = PyObject_Hash(co->co_cellvars);
- if (h6 == -1) return -1;
- h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
- co->co_argcount ^ co->co_kwonlyargcount ^
- co->co_nlocals ^ co->co_flags;
- if (h == -1) h = -2;
- return h;
+ long h, h0, h1, h2, h3, h4, h5, h6;
+ h0 = PyObject_Hash(co->co_name);
+ if (h0 == -1) return -1;
+ h1 = PyObject_Hash(co->co_code);
+ if (h1 == -1) return -1;
+ h2 = PyObject_Hash(co->co_consts);
+ if (h2 == -1) return -1;
+ h3 = PyObject_Hash(co->co_names);
+ if (h3 == -1) return -1;
+ h4 = PyObject_Hash(co->co_varnames);
+ if (h4 == -1) return -1;
+ h5 = PyObject_Hash(co->co_freevars);
+ if (h5 == -1) return -1;
+ h6 = PyObject_Hash(co->co_cellvars);
+ if (h6 == -1) return -1;
+ h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
+ co->co_argcount ^ co->co_kwonlyargcount ^
+ co->co_nlocals ^ co->co_flags;
+ if (h == -1) h = -2;
+ return h;
}
/* XXX code objects need to participate in GC? */
PyTypeObject PyCode_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "code",
- sizeof(PyCodeObject),
- 0,
- (destructor)code_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)code_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)code_hash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- code_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- code_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- code_memberlist, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- code_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "code",
+ sizeof(PyCodeObject),
+ 0,
+ (destructor)code_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)code_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)code_hash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ code_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ code_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ code_memberlist, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ code_new, /* tp_new */
};
/* All about c_lnotab.
@@ -441,9 +441,9 @@ The array is conceptually a list of
pairs. The details are important and delicate, best illustrated by example:
byte code offset source code line number
- 0 1
- 6 2
- 50 7
+ 0 1
+ 6 2
+ 50 7
350 307
361 308
@@ -463,10 +463,10 @@ corresponding to a bytecode address A should do something like this
lineno = addr = 0
for addr_incr, line_incr in c_lnotab:
- addr += addr_incr
- if addr > A:
- return lineno
- lineno += line_incr
+ addr += addr_incr
+ if addr > A:
+ return lineno
+ lineno += line_incr
In order for this to work, when the addr field increments by more than 255,
the line # increment in each pair generated must be 0 until the remaining addr
@@ -478,149 +478,149 @@ was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
int
PyCode_Addr2Line(PyCodeObject *co, int addrq)
{
- int size = PyBytes_Size(co->co_lnotab) / 2;
- unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
- int line = co->co_firstlineno;
- int addr = 0;
- while (--size >= 0) {
- addr += *p++;
- if (addr > addrq)
- break;
- line += *p++;
- }
- return line;
+ int size = PyBytes_Size(co->co_lnotab) / 2;
+ unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
+ int line = co->co_firstlineno;
+ int addr = 0;
+ while (--size >= 0) {
+ addr += *p++;
+ if (addr > addrq)
+ break;
+ line += *p++;
+ }
+ return line;
}
-/*
+/*
Check whether the current instruction is at the start of a line.
*/
- /* The theory of SET_LINENO-less tracing.
+ /* The theory of SET_LINENO-less tracing.
- In a nutshell, we use the co_lnotab field of the code object
- to tell when execution has moved onto a different line.
+ In a nutshell, we use the co_lnotab field of the code object
+ to tell when execution has moved onto a different line.
- As mentioned above, the basic idea is so set things up so
- that
+ As mentioned above, the basic idea is so set things up so
+ that
- *instr_lb <= frame->f_lasti < *instr_ub
+ *instr_lb <= frame->f_lasti < *instr_ub
- is true so long as execution does not change lines.
+ is true so long as execution does not change lines.
- This is all fairly simple. Digging the information out of
- co_lnotab takes some work, but is conceptually clear.
+ This is all fairly simple. Digging the information out of
+ co_lnotab takes some work, but is conceptually clear.
- Somewhat harder to explain is why we don't *always* call the
- line trace function when the above test fails.
+ Somewhat harder to explain is why we don't *always* call the
+ line trace function when the above test fails.
- Consider this code:
+ Consider this code:
- 1: def f(a):
- 2: if a:
- 3: print 1
- 4: else:
- 5: print 2
+ 1: def f(a):
+ 2: if a:
+ 3: print 1
+ 4: else:
+ 5: print 2
- which compiles to this:
+ which compiles to this:
- 2 0 LOAD_FAST 0 (a)
- 3 JUMP_IF_FALSE 9 (to 15)
- 6 POP_TOP
+ 2 0 LOAD_FAST 0 (a)
+ 3 JUMP_IF_FALSE 9 (to 15)
+ 6 POP_TOP
- 3 7 LOAD_CONST 1 (1)
- 10 PRINT_ITEM
- 11 PRINT_NEWLINE
- 12 JUMP_FORWARD 6 (to 21)
- >> 15 POP_TOP
+ 3 7 LOAD_CONST 1 (1)
+ 10 PRINT_ITEM
+ 11 PRINT_NEWLINE
+ 12 JUMP_FORWARD 6 (to 21)
+ >> 15 POP_TOP
- 5 16 LOAD_CONST 2 (2)
- 19 PRINT_ITEM
- 20 PRINT_NEWLINE
- >> 21 LOAD_CONST 0 (None)
- 24 RETURN_VALUE
+ 5 16 LOAD_CONST 2 (2)
+ 19 PRINT_ITEM
+ 20 PRINT_NEWLINE
+ >> 21 LOAD_CONST 0 (None)
+ 24 RETURN_VALUE
- If 'a' is false, execution will jump to instruction at offset
- 15 and the co_lnotab will claim that execution has moved to
- line 3. This is at best misleading. In this case we could
- associate the POP_TOP with line 4, but that doesn't make
- sense in all cases (I think).
+ If 'a' is false, execution will jump to instruction at offset
+ 15 and the co_lnotab will claim that execution has moved to
+ line 3. This is at best misleading. In this case we could
+ associate the POP_TOP with line 4, but that doesn't make
+ sense in all cases (I think).
- What we do is only call the line trace function if the co_lnotab
- indicates we have jumped to the *start* of a line, i.e. if the
- current instruction offset matches the offset given for the
- start of a line by the co_lnotab.
+ What we do is only call the line trace function if the co_lnotab
+ indicates we have jumped to the *start* of a line, i.e. if the
+ current instruction offset matches the offset given for the
+ start of a line by the co_lnotab.
- This also takes care of the situation where 'a' is true.
- Execution will jump from instruction offset 12 to offset 21.
- Then the co_lnotab would imply that execution has moved to line
- 5, which is again misleading.
+ This also takes care of the situation where 'a' is true.
+ Execution will jump from instruction offset 12 to offset 21.
+ Then the co_lnotab would imply that execution has moved to line
+ 5, which is again misleading.
- Why do we set f_lineno when tracing? Well, consider the code
- above when 'a' is true. If stepping through this with 'n' in
- pdb, you would stop at line 1 with a "call" type event, then
- line events on lines 2 and 3, then a "return" type event -- but
- you would be shown line 5 during this event. This is a change
- from the behaviour in 2.2 and before, and I've found it
- confusing in practice. By setting and using f_lineno when
- tracing, one can report a line number different from that
- suggested by f_lasti on this one occasion where it's desirable.
- */
+ Why do we set f_lineno when tracing? Well, consider the code
+ above when 'a' is true. If stepping through this with 'n' in
+ pdb, you would stop at line 1 with a "call" type event, then
+ line events on lines 2 and 3, then a "return" type event -- but
+ you would be shown line 5 during this event. This is a change
+ from the behaviour in 2.2 and before, and I've found it
+ confusing in practice. By setting and using f_lineno when
+ tracing, one can report a line number different from that
+ suggested by f_lasti on this one occasion where it's desirable.
+ */
-int
+int
PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
{
- int size, addr, line;
- unsigned char* p;
-
- p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
- size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
-
- addr = 0;
- line = co->co_firstlineno;
- assert(line > 0);
-
- /* possible optimization: if f->f_lasti == instr_ub
- (likely to be a common case) then we already know
- instr_lb -- if we stored the matching value of p
- somwhere we could skip the first while loop. */
-
- /* see comments in compile.c for the description of
- co_lnotab. A point to remember: increments to p
- should come in pairs -- although we don't care about
- the line increments here, treating them as byte
- increments gets confusing, to say the least. */
-
- bounds->ap_lower = 0;
- while (size > 0) {
- if (addr + *p > lasti)
- break;
- addr += *p++;
- if (*p)
- bounds->ap_lower = addr;
- line += *p++;
- --size;
- }
-
- /* If lasti and addr don't match exactly, we don't want to
- change the lineno slot on the frame or execute a trace
- function. Return -1 instead.
- */
- if (addr != lasti)
- line = -1;
-
- if (size > 0) {
- while (--size >= 0) {
- addr += *p++;
- if (*p++)
- break;
- }
- bounds->ap_upper = addr;
- }
- else {
- bounds->ap_upper = INT_MAX;
+ int size, addr, line;
+ unsigned char* p;
+
+ p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
+ size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
+
+ addr = 0;
+ line = co->co_firstlineno;
+ assert(line > 0);
+
+ /* possible optimization: if f->f_lasti == instr_ub
+ (likely to be a common case) then we already know
+ instr_lb -- if we stored the matching value of p
+ somwhere we could skip the first while loop. */
+
+ /* see comments in compile.c for the description of
+ co_lnotab. A point to remember: increments to p
+ should come in pairs -- although we don't care about
+ the line increments here, treating them as byte
+ increments gets confusing, to say the least. */
+
+ bounds->ap_lower = 0;
+ while (size > 0) {
+ if (addr + *p > lasti)
+ break;
+ addr += *p++;
+ if (*p)
+ bounds->ap_lower = addr;
+ line += *p++;
+ --size;
+ }
+
+ /* If lasti and addr don't match exactly, we don't want to
+ change the lineno slot on the frame or execute a trace
+ function. Return -1 instead.
+ */
+ if (addr != lasti)
+ line = -1;
+
+ if (size > 0) {
+ while (--size >= 0) {
+ addr += *p++;
+ if (*p++)
+ break;
}
+ bounds->ap_upper = addr;
+ }
+ else {
+ bounds->ap_upper = INT_MAX;
+ }
- return line;
+ return line;
}
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 30d8b52..300398e 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -21,375 +21,375 @@ static Py_complex c_1 = {1., 0.};
Py_complex
c_sum(Py_complex a, Py_complex b)
{
- Py_complex r;
- r.real = a.real + b.real;
- r.imag = a.imag + b.imag;
- return r;
+ Py_complex r;
+ r.real = a.real + b.real;
+ r.imag = a.imag + b.imag;
+ return r;
}
Py_complex
c_diff(Py_complex a, Py_complex b)
{
- Py_complex r;
- r.real = a.real - b.real;
- r.imag = a.imag - b.imag;
- return r;
+ Py_complex r;
+ r.real = a.real - b.real;
+ r.imag = a.imag - b.imag;
+ return r;
}
Py_complex
c_neg(Py_complex a)
{
- Py_complex r;
- r.real = -a.real;
- r.imag = -a.imag;
- return r;
+ Py_complex r;
+ r.real = -a.real;
+ r.imag = -a.imag;
+ return r;
}
Py_complex
c_prod(Py_complex a, Py_complex b)
{
- Py_complex r;
- r.real = a.real*b.real - a.imag*b.imag;
- r.imag = a.real*b.imag + a.imag*b.real;
- return r;
+ Py_complex r;
+ r.real = a.real*b.real - a.imag*b.imag;
+ r.imag = a.real*b.imag + a.imag*b.real;
+ return r;
}
Py_complex
c_quot(Py_complex a, Py_complex b)
{
- /******************************************************************
- This was the original algorithm. It's grossly prone to spurious
- overflow and underflow errors. It also merrily divides by 0 despite
- checking for that(!). The code still serves a doc purpose here, as
- the algorithm following is a simple by-cases transformation of this
- one:
-
- Py_complex r;
- double d = b.real*b.real + b.imag*b.imag;
- if (d == 0.)
- errno = EDOM;
- r.real = (a.real*b.real + a.imag*b.imag)/d;
- r.imag = (a.imag*b.real - a.real*b.imag)/d;
- return r;
- ******************************************************************/
-
- /* This algorithm is better, and is pretty obvious: first divide the
- * numerators and denominator by whichever of {b.real, b.imag} has
- * larger magnitude. The earliest reference I found was to CACM
- * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
- * University). As usual, though, we're still ignoring all IEEE
- * endcases.
- */
- Py_complex r; /* the result */
- const double abs_breal = b.real < 0 ? -b.real : b.real;
- const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
-
- if (abs_breal >= abs_bimag) {
- /* divide tops and bottom by b.real */
- if (abs_breal == 0.0) {
- errno = EDOM;
- r.real = r.imag = 0.0;
- }
- else {
- const double ratio = b.imag / b.real;
- const double denom = b.real + b.imag * ratio;
- r.real = (a.real + a.imag * ratio) / denom;
- r.imag = (a.imag - a.real * ratio) / denom;
- }
- }
- else {
- /* divide tops and bottom by b.imag */
- const double ratio = b.real / b.imag;
- const double denom = b.real * ratio + b.imag;
- assert(b.imag != 0.0);
- r.real = (a.real * ratio + a.imag) / denom;
- r.imag = (a.imag * ratio - a.real) / denom;
- }
- return r;
+ /******************************************************************
+ This was the original algorithm. It's grossly prone to spurious
+ overflow and underflow errors. It also merrily divides by 0 despite
+ checking for that(!). The code still serves a doc purpose here, as
+ the algorithm following is a simple by-cases transformation of this
+ one:
+
+ Py_complex r;
+ double d = b.real*b.real + b.imag*b.imag;
+ if (d == 0.)
+ errno = EDOM;
+ r.real = (a.real*b.real + a.imag*b.imag)/d;
+ r.imag = (a.imag*b.real - a.real*b.imag)/d;
+ return r;
+ ******************************************************************/
+
+ /* This algorithm is better, and is pretty obvious: first divide the
+ * numerators and denominator by whichever of {b.real, b.imag} has
+ * larger magnitude. The earliest reference I found was to CACM
+ * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
+ * University). As usual, though, we're still ignoring all IEEE
+ * endcases.
+ */
+ Py_complex r; /* the result */
+ const double abs_breal = b.real < 0 ? -b.real : b.real;
+ const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
+
+ if (abs_breal >= abs_bimag) {
+ /* divide tops and bottom by b.real */
+ if (abs_breal == 0.0) {
+ errno = EDOM;
+ r.real = r.imag = 0.0;
+ }
+ else {
+ const double ratio = b.imag / b.real;
+ const double denom = b.real + b.imag * ratio;
+ r.real = (a.real + a.imag * ratio) / denom;
+ r.imag = (a.imag - a.real * ratio) / denom;
+ }
+ }
+ else {
+ /* divide tops and bottom by b.imag */
+ const double ratio = b.real / b.imag;
+ const double denom = b.real * ratio + b.imag;
+ assert(b.imag != 0.0);
+ r.real = (a.real * ratio + a.imag) / denom;
+ r.imag = (a.imag * ratio - a.real) / denom;
+ }
+ return r;
}
Py_complex
c_pow(Py_complex a, Py_complex b)
{
- Py_complex r;
- double vabs,len,at,phase;
- if (b.real == 0. && b.imag == 0.) {
- r.real = 1.;
- r.imag = 0.;
- }
- else if (a.real == 0. && a.imag == 0.) {
- if (b.imag != 0. || b.real < 0.)
- errno = EDOM;
- r.real = 0.;
- r.imag = 0.;
- }
- else {
- vabs = hypot(a.real,a.imag);
- len = pow(vabs,b.real);
- at = atan2(a.imag, a.real);
- phase = at*b.real;
- if (b.imag != 0.0) {
- len /= exp(at*b.imag);
- phase += b.imag*log(vabs);
- }
- r.real = len*cos(phase);
- r.imag = len*sin(phase);
- }
- return r;
+ Py_complex r;
+ double vabs,len,at,phase;
+ if (b.real == 0. && b.imag == 0.) {
+ r.real = 1.;
+ r.imag = 0.;
+ }
+ else if (a.real == 0. && a.imag == 0.) {
+ if (b.imag != 0. || b.real < 0.)
+ errno = EDOM;
+ r.real = 0.;
+ r.imag = 0.;
+ }
+ else {
+ vabs = hypot(a.real,a.imag);
+ len = pow(vabs,b.real);
+ at = atan2(a.imag, a.real);
+ phase = at*b.real;
+ if (b.imag != 0.0) {
+ len /= exp(at*b.imag);
+ phase += b.imag*log(vabs);
+ }
+ r.real = len*cos(phase);
+ r.imag = len*sin(phase);
+ }
+ return r;
}
static Py_complex
c_powu(Py_complex x, long n)
{
- Py_complex r, p;
- long mask = 1;
- r = c_1;
- p = x;
- while (mask > 0 && n >= mask) {
- if (n & mask)
- r = c_prod(r,p);
- mask <<= 1;
- p = c_prod(p,p);
- }
- return r;
+ Py_complex r, p;
+ long mask = 1;
+ r = c_1;
+ p = x;
+ while (mask > 0 && n >= mask) {
+ if (n & mask)
+ r = c_prod(r,p);
+ mask <<= 1;
+ p = c_prod(p,p);
+ }
+ return r;
}
static Py_complex
c_powi(Py_complex x, long n)
{
- Py_complex cn;
-
- if (n > 100 || n < -100) {
- cn.real = (double) n;
- cn.imag = 0.;
- return c_pow(x,cn);
- }
- else if (n > 0)
- return c_powu(x,n);
- else
- return c_quot(c_1,c_powu(x,-n));
+ Py_complex cn;
+
+ if (n > 100 || n < -100) {
+ cn.real = (double) n;
+ cn.imag = 0.;
+ return c_pow(x,cn);
+ }
+ else if (n > 0)
+ return c_powu(x,n);
+ else
+ return c_quot(c_1,c_powu(x,-n));
}
double
c_abs(Py_complex z)
{
- /* sets errno = ERANGE on overflow; otherwise errno = 0 */
- double result;
-
- if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
- /* C99 rules: if either the real or the imaginary part is an
- infinity, return infinity, even if the other part is a
- NaN. */
- if (Py_IS_INFINITY(z.real)) {
- result = fabs(z.real);
- errno = 0;
- return result;
- }
- if (Py_IS_INFINITY(z.imag)) {
- result = fabs(z.imag);
- errno = 0;
- return result;
- }
- /* either the real or imaginary part is a NaN,
- and neither is infinite. Result should be NaN. */
- return Py_NAN;
- }
- result = hypot(z.real, z.imag);
- if (!Py_IS_FINITE(result))
- errno = ERANGE;
- else
- errno = 0;
- return result;
+ /* sets errno = ERANGE on overflow; otherwise errno = 0 */
+ double result;
+
+ if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
+ /* C99 rules: if either the real or the imaginary part is an
+ infinity, return infinity, even if the other part is a
+ NaN. */
+ if (Py_IS_INFINITY(z.real)) {
+ result = fabs(z.real);
+ errno = 0;
+ return result;
+ }
+ if (Py_IS_INFINITY(z.imag)) {
+ result = fabs(z.imag);
+ errno = 0;
+ return result;
+ }
+ /* either the real or imaginary part is a NaN,
+ and neither is infinite. Result should be NaN. */
+ return Py_NAN;
+ }
+ result = hypot(z.real, z.imag);
+ if (!Py_IS_FINITE(result))
+ errno = ERANGE;
+ else
+ errno = 0;
+ return result;
}
static PyObject *
complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
{
- PyObject *op;
+ PyObject *op;
- op = type->tp_alloc(type, 0);
- if (op != NULL)
- ((PyComplexObject *)op)->cval = cval;
- return op;
+ op = type->tp_alloc(type, 0);
+ if (op != NULL)
+ ((PyComplexObject *)op)->cval = cval;
+ return op;
}
PyObject *
PyComplex_FromCComplex(Py_complex cval)
{
- register PyComplexObject *op;
-
- /* Inline PyObject_New */
- op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_INIT(op, &PyComplex_Type);
- op->cval = cval;
- return (PyObject *) op;
+ register PyComplexObject *op;
+
+ /* Inline PyObject_New */
+ op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
+ if (op == NULL)
+ return PyErr_NoMemory();
+ PyObject_INIT(op, &PyComplex_Type);
+ op->cval = cval;
+ return (PyObject *) op;
}
static PyObject *
complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
{
- Py_complex c;
- c.real = real;
- c.imag = imag;
- return complex_subtype_from_c_complex(type, c);
+ Py_complex c;
+ c.real = real;
+ c.imag = imag;
+ return complex_subtype_from_c_complex(type, c);
}
PyObject *
PyComplex_FromDoubles(double real, double imag)
{
- Py_complex c;
- c.real = real;
- c.imag = imag;
- return PyComplex_FromCComplex(c);
+ Py_complex c;
+ c.real = real;
+ c.imag = imag;
+ return PyComplex_FromCComplex(c);
}
double
PyComplex_RealAsDouble(PyObject *op)
{
- if (PyComplex_Check(op)) {
- return ((PyComplexObject *)op)->cval.real;
- }
- else {
- return PyFloat_AsDouble(op);
- }
+ if (PyComplex_Check(op)) {
+ return ((PyComplexObject *)op)->cval.real;
+ }
+ else {
+ return PyFloat_AsDouble(op);
+ }
}
double
PyComplex_ImagAsDouble(PyObject *op)
{
- if (PyComplex_Check(op)) {
- return ((PyComplexObject *)op)->cval.imag;
- }
- else {
- return 0.0;
- }
+ if (PyComplex_Check(op)) {
+ return ((PyComplexObject *)op)->cval.imag;
+ }
+ else {
+ return 0.0;
+ }
}
Py_complex
PyComplex_AsCComplex(PyObject *op)
{
- Py_complex cv;
- PyObject *newop = NULL;
- static PyObject *complex_str = NULL;
-
- assert(op);
- /* If op is already of type PyComplex_Type, return its value */
- if (PyComplex_Check(op)) {
- return ((PyComplexObject *)op)->cval;
- }
- /* If not, use op's __complex__ method, if it exists */
-
- /* return -1 on failure */
- cv.real = -1.;
- cv.imag = 0.;
-
- if (complex_str == NULL) {
- if (!(complex_str = PyUnicode_FromString("__complex__")))
- return cv;
- }
-
- {
- PyObject *complexfunc;
- complexfunc = _PyType_Lookup(op->ob_type, complex_str);
- /* complexfunc is a borrowed reference */
- if (complexfunc) {
- newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
- if (!newop)
- return cv;
- }
- }
-
- if (newop) {
- if (!PyComplex_Check(newop)) {
- PyErr_SetString(PyExc_TypeError,
- "__complex__ should return a complex object");
- Py_DECREF(newop);
- return cv;
- }
- cv = ((PyComplexObject *)newop)->cval;
- Py_DECREF(newop);
- return cv;
- }
- /* If neither of the above works, interpret op as a float giving the
- real part of the result, and fill in the imaginary part as 0. */
- else {
- /* PyFloat_AsDouble will return -1 on failure */
- cv.real = PyFloat_AsDouble(op);
- return cv;
- }
+ Py_complex cv;
+ PyObject *newop = NULL;
+ static PyObject *complex_str = NULL;
+
+ assert(op);
+ /* If op is already of type PyComplex_Type, return its value */
+ if (PyComplex_Check(op)) {
+ return ((PyComplexObject *)op)->cval;
+ }
+ /* If not, use op's __complex__ method, if it exists */
+
+ /* return -1 on failure */
+ cv.real = -1.;
+ cv.imag = 0.;
+
+ if (complex_str == NULL) {
+ if (!(complex_str = PyUnicode_FromString("__complex__")))
+ return cv;
+ }
+
+ {
+ PyObject *complexfunc;
+ complexfunc = _PyType_Lookup(op->ob_type, complex_str);
+ /* complexfunc is a borrowed reference */
+ if (complexfunc) {
+ newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
+ if (!newop)
+ return cv;
+ }
+ }
+
+ if (newop) {
+ if (!PyComplex_Check(newop)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__complex__ should return a complex object");
+ Py_DECREF(newop);
+ return cv;
+ }
+ cv = ((PyComplexObject *)newop)->cval;
+ Py_DECREF(newop);
+ return cv;
+ }
+ /* If neither of the above works, interpret op as a float giving the
+ real part of the result, and fill in the imaginary part as 0. */
+ else {
+ /* PyFloat_AsDouble will return -1 on failure */
+ cv.real = PyFloat_AsDouble(op);
+ return cv;
+ }
}
static void
complex_dealloc(PyObject *op)
{
- op->ob_type->tp_free(op);
+ op->ob_type->tp_free(op);
}
static PyObject *
complex_format(PyComplexObject *v, int precision, char format_code)
{
- PyObject *result = NULL;
- Py_ssize_t len;
-
- /* If these are non-NULL, they'll need to be freed. */
- char *pre = NULL;
- char *im = NULL;
- char *buf = NULL;
-
- /* These do not need to be freed. re is either an alias
- for pre or a pointer to a constant. lead and tail
- are pointers to constants. */
- char *re = NULL;
- char *lead = "";
- char *tail = "";
-
- if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
- re = "";
- im = PyOS_double_to_string(v->cval.imag, format_code,
- precision, 0, NULL);
- if (!im) {
- PyErr_NoMemory();
- goto done;
- }
- } else {
- /* Format imaginary part with sign, real part without */
- pre = PyOS_double_to_string(v->cval.real, format_code,
- precision, 0, NULL);
- if (!pre) {
- PyErr_NoMemory();
- goto done;
- }
- re = pre;
-
- im = PyOS_double_to_string(v->cval.imag, format_code,
- precision, Py_DTSF_SIGN, NULL);
- if (!im) {
- PyErr_NoMemory();
- goto done;
- }
- lead = "(";
- tail = ")";
- }
- /* Alloc the final buffer. Add one for the "j" in the format string,
- and one for the trailing zero. */
- len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
- buf = PyMem_Malloc(len);
- if (!buf) {
- PyErr_NoMemory();
- goto done;
- }
- PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
- result = PyUnicode_FromString(buf);
+ PyObject *result = NULL;
+ Py_ssize_t len;
+
+ /* If these are non-NULL, they'll need to be freed. */
+ char *pre = NULL;
+ char *im = NULL;
+ char *buf = NULL;
+
+ /* These do not need to be freed. re is either an alias
+ for pre or a pointer to a constant. lead and tail
+ are pointers to constants. */
+ char *re = NULL;
+ char *lead = "";
+ char *tail = "";
+
+ if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
+ re = "";
+ im = PyOS_double_to_string(v->cval.imag, format_code,
+ precision, 0, NULL);
+ if (!im) {
+ PyErr_NoMemory();
+ goto done;
+ }
+ } else {
+ /* Format imaginary part with sign, real part without */
+ pre = PyOS_double_to_string(v->cval.real, format_code,
+ precision, 0, NULL);
+ if (!pre) {
+ PyErr_NoMemory();
+ goto done;
+ }
+ re = pre;
+
+ im = PyOS_double_to_string(v->cval.imag, format_code,
+ precision, Py_DTSF_SIGN, NULL);
+ if (!im) {
+ PyErr_NoMemory();
+ goto done;
+ }
+ lead = "(";
+ tail = ")";
+ }
+ /* Alloc the final buffer. Add one for the "j" in the format string,
+ and one for the trailing zero. */
+ len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
+ buf = PyMem_Malloc(len);
+ if (!buf) {
+ PyErr_NoMemory();
+ goto done;
+ }
+ PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
+ result = PyUnicode_FromString(buf);
done:
- PyMem_Free(im);
- PyMem_Free(pre);
- PyMem_Free(buf);
+ PyMem_Free(im);
+ PyMem_Free(pre);
+ PyMem_Free(buf);
- return result;
+ return result;
}
static PyObject *
@@ -407,266 +407,266 @@ complex_str(PyComplexObject *v)
static long
complex_hash(PyComplexObject *v)
{
- long hashreal, hashimag, combined;
- hashreal = _Py_HashDouble(v->cval.real);
- if (hashreal == -1)
- return -1;
- hashimag = _Py_HashDouble(v->cval.imag);
- if (hashimag == -1)
- return -1;
- /* Note: if the imaginary part is 0, hashimag is 0 now,
- * so the following returns hashreal unchanged. This is
- * important because numbers of different types that
- * compare equal must have the same hash value, so that
- * hash(x + 0*j) must equal hash(x).
- */
- combined = hashreal + 1000003 * hashimag;
- if (combined == -1)
- combined = -2;
- return combined;
+ long hashreal, hashimag, combined;
+ hashreal = _Py_HashDouble(v->cval.real);
+ if (hashreal == -1)
+ return -1;
+ hashimag = _Py_HashDouble(v->cval.imag);
+ if (hashimag == -1)
+ return -1;
+ /* Note: if the imaginary part is 0, hashimag is 0 now,
+ * so the following returns hashreal unchanged. This is
+ * important because numbers of different types that
+ * compare equal must have the same hash value, so that
+ * hash(x + 0*j) must equal hash(x).
+ */
+ combined = hashreal + 1000003 * hashimag;
+ if (combined == -1)
+ combined = -2;
+ return combined;
}
/* This macro may return! */
#define TO_COMPLEX(obj, c) \
- if (PyComplex_Check(obj)) \
- c = ((PyComplexObject *)(obj))->cval; \
- else if (to_complex(&(obj), &(c)) < 0) \
- return (obj)
+ if (PyComplex_Check(obj)) \
+ c = ((PyComplexObject *)(obj))->cval; \
+ else if (to_complex(&(obj), &(c)) < 0) \
+ return (obj)
static int
to_complex(PyObject **pobj, Py_complex *pc)
{
- PyObject *obj = *pobj;
-
- pc->real = pc->imag = 0.0;
- if (PyLong_Check(obj)) {
- pc->real = PyLong_AsDouble(obj);
- if (pc->real == -1.0 && PyErr_Occurred()) {
- *pobj = NULL;
- return -1;
- }
- return 0;
- }
- if (PyFloat_Check(obj)) {
- pc->real = PyFloat_AsDouble(obj);
- return 0;
- }
- Py_INCREF(Py_NotImplemented);
- *pobj = Py_NotImplemented;
- return -1;
+ PyObject *obj = *pobj;
+
+ pc->real = pc->imag = 0.0;
+ if (PyLong_Check(obj)) {
+ pc->real = PyLong_AsDouble(obj);
+ if (pc->real == -1.0 && PyErr_Occurred()) {
+ *pobj = NULL;
+ return -1;
+ }
+ return 0;
+ }
+ if (PyFloat_Check(obj)) {
+ pc->real = PyFloat_AsDouble(obj);
+ return 0;
+ }
+ Py_INCREF(Py_NotImplemented);
+ *pobj = Py_NotImplemented;
+ return -1;
}
-
+
static PyObject *
complex_add(PyObject *v, PyObject *w)
{
- Py_complex result;
- Py_complex a, b;
- TO_COMPLEX(v, a);
- TO_COMPLEX(w, b);
- PyFPE_START_PROTECT("complex_add", return 0)
- result = c_sum(a, b);
- PyFPE_END_PROTECT(result)
- return PyComplex_FromCComplex(result);
+ Py_complex result;
+ Py_complex a, b;
+ TO_COMPLEX(v, a);
+ TO_COMPLEX(w, b);
+ PyFPE_START_PROTECT("complex_add", return 0)
+ result = c_sum(a, b);
+ PyFPE_END_PROTECT(result)
+ return PyComplex_FromCComplex(result);
}
static PyObject *
complex_sub(PyObject *v, PyObject *w)
{
- Py_complex result;
- Py_complex a, b;
- TO_COMPLEX(v, a);
- TO_COMPLEX(w, b);
- PyFPE_START_PROTECT("complex_sub", return 0)
- result = c_diff(a, b);
- PyFPE_END_PROTECT(result)
- return PyComplex_FromCComplex(result);
+ Py_complex result;
+ Py_complex a, b;
+ TO_COMPLEX(v, a);
+ TO_COMPLEX(w, b);
+ PyFPE_START_PROTECT("complex_sub", return 0)
+ result = c_diff(a, b);
+ PyFPE_END_PROTECT(result)
+ return PyComplex_FromCComplex(result);
}
static PyObject *
complex_mul(PyObject *v, PyObject *w)
{
- Py_complex result;
- Py_complex a, b;
- TO_COMPLEX(v, a);
- TO_COMPLEX(w, b);
- PyFPE_START_PROTECT("complex_mul", return 0)
- result = c_prod(a, b);
- PyFPE_END_PROTECT(result)
- return PyComplex_FromCComplex(result);
+ Py_complex result;
+ Py_complex a, b;
+ TO_COMPLEX(v, a);
+ TO_COMPLEX(w, b);
+ PyFPE_START_PROTECT("complex_mul", return 0)
+ result = c_prod(a, b);
+ PyFPE_END_PROTECT(result)
+ return PyComplex_FromCComplex(result);
}
static PyObject *
complex_div(PyObject *v, PyObject *w)
{
- Py_complex quot;
- Py_complex a, b;
- TO_COMPLEX(v, a);
- TO_COMPLEX(w, b);
- PyFPE_START_PROTECT("complex_div", return 0)
- errno = 0;
- quot = c_quot(a, b);
- PyFPE_END_PROTECT(quot)
- if (errno == EDOM) {
- PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
- return NULL;
- }
- return PyComplex_FromCComplex(quot);
+ Py_complex quot;
+ Py_complex a, b;
+ TO_COMPLEX(v, a);
+ TO_COMPLEX(w, b);
+ PyFPE_START_PROTECT("complex_div", return 0)
+ errno = 0;
+ quot = c_quot(a, b);
+ PyFPE_END_PROTECT(quot)
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
+ return NULL;
+ }
+ return PyComplex_FromCComplex(quot);
}
static PyObject *
complex_remainder(PyObject *v, PyObject *w)
{
- PyErr_SetString(PyExc_TypeError,
- "can't mod complex numbers.");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "can't mod complex numbers.");
+ return NULL;
}
static PyObject *
complex_divmod(PyObject *v, PyObject *w)
{
- PyErr_SetString(PyExc_TypeError,
- "can't take floor or mod of complex number.");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "can't take floor or mod of complex number.");
+ return NULL;
}
static PyObject *
complex_pow(PyObject *v, PyObject *w, PyObject *z)
{
- Py_complex p;
- Py_complex exponent;
- long int_exponent;
- Py_complex a, b;
- TO_COMPLEX(v, a);
- TO_COMPLEX(w, b);
-
- if (z != Py_None) {
- PyErr_SetString(PyExc_ValueError, "complex modulo");
- return NULL;
- }
- PyFPE_START_PROTECT("complex_pow", return 0)
- errno = 0;
- exponent = b;
- int_exponent = (long)exponent.real;
- if (exponent.imag == 0. && exponent.real == int_exponent)
- p = c_powi(a, int_exponent);
- else
- p = c_pow(a, exponent);
-
- PyFPE_END_PROTECT(p)
- Py_ADJUST_ERANGE2(p.real, p.imag);
- if (errno == EDOM) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "0.0 to a negative or complex power");
- return NULL;
- }
- else if (errno == ERANGE) {
- PyErr_SetString(PyExc_OverflowError,
- "complex exponentiation");
- return NULL;
- }
- return PyComplex_FromCComplex(p);
+ Py_complex p;
+ Py_complex exponent;
+ long int_exponent;
+ Py_complex a, b;
+ TO_COMPLEX(v, a);
+ TO_COMPLEX(w, b);
+
+ if (z != Py_None) {
+ PyErr_SetString(PyExc_ValueError, "complex modulo");
+ return NULL;
+ }
+ PyFPE_START_PROTECT("complex_pow", return 0)
+ errno = 0;
+ exponent = b;
+ int_exponent = (long)exponent.real;
+ if (exponent.imag == 0. && exponent.real == int_exponent)
+ p = c_powi(a, int_exponent);
+ else
+ p = c_pow(a, exponent);
+
+ PyFPE_END_PROTECT(p)
+ Py_ADJUST_ERANGE2(p.real, p.imag);
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "0.0 to a negative or complex power");
+ return NULL;
+ }
+ else if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError,
+ "complex exponentiation");
+ return NULL;
+ }
+ return PyComplex_FromCComplex(p);
}
static PyObject *
complex_int_div(PyObject *v, PyObject *w)
{
- PyErr_SetString(PyExc_TypeError,
- "can't take floor of complex number.");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "can't take floor of complex number.");
+ return NULL;
}
static PyObject *
complex_neg(PyComplexObject *v)
{
- Py_complex neg;
- neg.real = -v->cval.real;
- neg.imag = -v->cval.imag;
- return PyComplex_FromCComplex(neg);
+ Py_complex neg;
+ neg.real = -v->cval.real;
+ neg.imag = -v->cval.imag;
+ return PyComplex_FromCComplex(neg);
}
static PyObject *
complex_pos(PyComplexObject *v)
{
- if (PyComplex_CheckExact(v)) {
- Py_INCREF(v);
- return (PyObject *)v;
- }
- else
- return PyComplex_FromCComplex(v->cval);
+ if (PyComplex_CheckExact(v)) {
+ Py_INCREF(v);
+ return (PyObject *)v;
+ }
+ else
+ return PyComplex_FromCComplex(v->cval);
}
static PyObject *
complex_abs(PyComplexObject *v)
{
- double result;
-
- PyFPE_START_PROTECT("complex_abs", return 0)
- result = c_abs(v->cval);
- PyFPE_END_PROTECT(result)
-
- if (errno == ERANGE) {
- PyErr_SetString(PyExc_OverflowError,
- "absolute value too large");
- return NULL;
- }
- return PyFloat_FromDouble(result);
+ double result;
+
+ PyFPE_START_PROTECT("complex_abs", return 0)
+ result = c_abs(v->cval);
+ PyFPE_END_PROTECT(result)
+
+ if (errno == ERANGE) {
+ PyErr_SetString(PyExc_OverflowError,
+ "absolute value too large");
+ return NULL;
+ }
+ return PyFloat_FromDouble(result);
}
static int
complex_bool(PyComplexObject *v)
{
- return v->cval.real != 0.0 || v->cval.imag != 0.0;
+ return v->cval.real != 0.0 || v->cval.imag != 0.0;
}
static PyObject *
complex_richcompare(PyObject *v, PyObject *w, int op)
{
- PyObject *res;
- Py_complex i, j;
- TO_COMPLEX(v, i);
- TO_COMPLEX(w, j);
-
- if (op != Py_EQ && op != Py_NE) {
- /* XXX Should eventually return NotImplemented */
- PyErr_SetString(PyExc_TypeError,
- "no ordering relation is defined for complex numbers");
- return NULL;
- }
-
- if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
- res = Py_True;
- else
- res = Py_False;
-
- Py_INCREF(res);
- return res;
+ PyObject *res;
+ Py_complex i, j;
+ TO_COMPLEX(v, i);
+ TO_COMPLEX(w, j);
+
+ if (op != Py_EQ && op != Py_NE) {
+ /* XXX Should eventually return NotImplemented */
+ PyErr_SetString(PyExc_TypeError,
+ "no ordering relation is defined for complex numbers");
+ return NULL;
+ }
+
+ if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
+ res = Py_True;
+ else
+ res = Py_False;
+
+ Py_INCREF(res);
+ return res;
}
static PyObject *
complex_int(PyObject *v)
{
- PyErr_SetString(PyExc_TypeError,
- "can't convert complex to int");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "can't convert complex to int");
+ return NULL;
}
static PyObject *
complex_float(PyObject *v)
{
- PyErr_SetString(PyExc_TypeError,
- "can't convert complex to float");
- return NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "can't convert complex to float");
+ return NULL;
}
static PyObject *
complex_conjugate(PyObject *self)
{
- Py_complex c;
- c = ((PyComplexObject *)self)->cval;
- c.imag = -c.imag;
- return PyComplex_FromCComplex(c);
+ Py_complex c;
+ c = ((PyComplexObject *)self)->cval;
+ c.imag = -c.imag;
+ return PyComplex_FromCComplex(c);
}
PyDoc_STRVAR(complex_conjugate_doc,
@@ -677,8 +677,8 @@ PyDoc_STRVAR(complex_conjugate_doc,
static PyObject *
complex_getnewargs(PyComplexObject *v)
{
- Py_complex c = v->cval;
- return Py_BuildValue("(dd)", c.real, c.imag);
+ Py_complex c = v->cval;
+ return Py_BuildValue("(dd)", c.real, c.imag);
}
PyDoc_STRVAR(complex__format__doc,
@@ -692,7 +692,7 @@ complex__format__(PyObject* self, PyObject* args)
PyObject *format_spec;
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
+ return NULL;
return _PyComplex_FormatAdvanced(self,
PyUnicode_AS_UNICODE(format_spec),
PyUnicode_GET_SIZE(format_spec));
@@ -702,10 +702,10 @@ complex__format__(PyObject* self, PyObject* args)
static PyObject *
complex_is_finite(PyObject *self)
{
- Py_complex c;
- c = ((PyComplexObject *)self)->cval;
- return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
- Py_IS_FINITE(c.imag)));
+ Py_complex c;
+ c = ((PyComplexObject *)self)->cval;
+ return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
+ Py_IS_FINITE(c.imag)));
}
PyDoc_STRVAR(complex_is_finite_doc,
@@ -715,314 +715,314 @@ PyDoc_STRVAR(complex_is_finite_doc,
#endif
static PyMethodDef complex_methods[] = {
- {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
- complex_conjugate_doc},
+ {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
+ complex_conjugate_doc},
#if 0
- {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
- complex_is_finite_doc},
+ {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
+ complex_is_finite_doc},
#endif
- {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
- {"__format__", (PyCFunction)complex__format__,
- METH_VARARGS, complex__format__doc},
- {NULL, NULL} /* sentinel */
+ {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
+ {"__format__", (PyCFunction)complex__format__,
+ METH_VARARGS, complex__format__doc},
+ {NULL, NULL} /* sentinel */
};
static PyMemberDef complex_members[] = {
- {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
- "the real part of a complex number"},
- {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
- "the imaginary part of a complex number"},
- {0},
+ {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
+ "the real part of a complex number"},
+ {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
+ "the imaginary part of a complex number"},
+ {0},
};
static PyObject *
complex_subtype_from_string(PyTypeObject *type, PyObject *v)
{
- const char *s, *start;
- char *end;
- double x=0.0, y=0.0, z;
- int got_bracket=0;
- char s_buffer[256];
- Py_ssize_t len;
-
- if (PyUnicode_Check(v)) {
- if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
- PyErr_SetString(PyExc_ValueError,
- "complex() literal too large to convert");
- return NULL;
- }
- if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
- PyUnicode_GET_SIZE(v),
- s_buffer,
- NULL))
- return NULL;
- s = s_buffer;
- len = strlen(s);
- }
- else if (PyObject_AsCharBuffer(v, &s, &len)) {
- PyErr_SetString(PyExc_TypeError,
- "complex() arg is not a string");
- return NULL;
- }
-
- /* position on first nonblank */
- start = s;
- while (Py_ISSPACE(*s))
- s++;
- if (*s == '(') {
- /* Skip over possible bracket from repr(). */
- got_bracket = 1;
- s++;
- while (Py_ISSPACE(*s))
- s++;
- }
-
- /* a valid complex string usually takes one of the three forms:
-
- <float> - real part only
- <float>j - imaginary part only
- <float><signed-float>j - real and imaginary parts
-
- where <float> represents any numeric string that's accepted by the
- float constructor (including 'nan', 'inf', 'infinity', etc.), and
- <signed-float> is any string of the form <float> whose first
- character is '+' or '-'.
-
- For backwards compatibility, the extra forms
-
- <float><sign>j
- <sign>j
- j
-
- are also accepted, though support for these forms may be removed from
- a future version of Python.
- */
-
- /* first look for forms starting with <float> */
- z = PyOS_string_to_double(s, &end, NULL);
- if (z == -1.0 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_ValueError))
- PyErr_Clear();
- else
- return NULL;
- }
- if (end != s) {
- /* all 4 forms starting with <float> land here */
- s = end;
- if (*s == '+' || *s == '-') {
- /* <float><signed-float>j | <float><sign>j */
- x = z;
- y = PyOS_string_to_double(s, &end, NULL);
- if (y == -1.0 && PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_ValueError))
- PyErr_Clear();
- else
- return NULL;
- }
- if (end != s)
- /* <float><signed-float>j */
- s = end;
- else {
- /* <float><sign>j */
- y = *s == '+' ? 1.0 : -1.0;
- s++;
- }
- if (!(*s == 'j' || *s == 'J'))
- goto parse_error;
- s++;
- }
- else if (*s == 'j' || *s == 'J') {
- /* <float>j */
- s++;
- y = z;
- }
- else
- /* <float> */
- x = z;
- }
- else {
- /* not starting with <float>; must be <sign>j or j */
- if (*s == '+' || *s == '-') {
- /* <sign>j */
- y = *s == '+' ? 1.0 : -1.0;
- s++;
- }
- else
- /* j */
- y = 1.0;
- if (!(*s == 'j' || *s == 'J'))
- goto parse_error;
- s++;
- }
-
- /* trailing whitespace and closing bracket */
- while (Py_ISSPACE(*s))
- s++;
- if (got_bracket) {
- /* if there was an opening parenthesis, then the corresponding
- closing parenthesis should be right here */
- if (*s != ')')
- goto parse_error;
- s++;
- while (Py_ISSPACE(*s))
- s++;
- }
-
- /* we should now be at the end of the string */
- if (s-start != len)
- goto parse_error;
-
- return complex_subtype_from_doubles(type, x, y);
+ const char *s, *start;
+ char *end;
+ double x=0.0, y=0.0, z;
+ int got_bracket=0;
+ char s_buffer[256];
+ Py_ssize_t len;
+
+ if (PyUnicode_Check(v)) {
+ if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
+ PyErr_SetString(PyExc_ValueError,
+ "complex() literal too large to convert");
+ return NULL;
+ }
+ if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
+ PyUnicode_GET_SIZE(v),
+ s_buffer,
+ NULL))
+ return NULL;
+ s = s_buffer;
+ len = strlen(s);
+ }
+ else if (PyObject_AsCharBuffer(v, &s, &len)) {
+ PyErr_SetString(PyExc_TypeError,
+ "complex() arg is not a string");
+ return NULL;
+ }
+
+ /* position on first nonblank */
+ start = s;
+ while (Py_ISSPACE(*s))
+ s++;
+ if (*s == '(') {
+ /* Skip over possible bracket from repr(). */
+ got_bracket = 1;
+ s++;
+ while (Py_ISSPACE(*s))
+ s++;
+ }
+
+ /* a valid complex string usually takes one of the three forms:
+
+ <float> - real part only
+ <float>j - imaginary part only
+ <float><signed-float>j - real and imaginary parts
+
+ where <float> represents any numeric string that's accepted by the
+ float constructor (including 'nan', 'inf', 'infinity', etc.), and
+ <signed-float> is any string of the form <float> whose first
+ character is '+' or '-'.
+
+ For backwards compatibility, the extra forms
+
+ <float><sign>j
+ <sign>j
+ j
+
+ are also accepted, though support for these forms may be removed from
+ a future version of Python.
+ */
+
+ /* first look for forms starting with <float> */
+ z = PyOS_string_to_double(s, &end, NULL);
+ if (z == -1.0 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_ValueError))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ if (end != s) {
+ /* all 4 forms starting with <float> land here */
+ s = end;
+ if (*s == '+' || *s == '-') {
+ /* <float><signed-float>j | <float><sign>j */
+ x = z;
+ y = PyOS_string_to_double(s, &end, NULL);
+ if (y == -1.0 && PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_ValueError))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ if (end != s)
+ /* <float><signed-float>j */
+ s = end;
+ else {
+ /* <float><sign>j */
+ y = *s == '+' ? 1.0 : -1.0;
+ s++;
+ }
+ if (!(*s == 'j' || *s == 'J'))
+ goto parse_error;
+ s++;
+ }
+ else if (*s == 'j' || *s == 'J') {
+ /* <float>j */
+ s++;
+ y = z;
+ }
+ else
+ /* <float> */
+ x = z;
+ }
+ else {
+ /* not starting with <float>; must be <sign>j or j */
+ if (*s == '+' || *s == '-') {
+ /* <sign>j */
+ y = *s == '+' ? 1.0 : -1.0;
+ s++;
+ }
+ else
+ /* j */
+ y = 1.0;
+ if (!(*s == 'j' || *s == 'J'))
+ goto parse_error;
+ s++;
+ }
+
+ /* trailing whitespace and closing bracket */
+ while (Py_ISSPACE(*s))
+ s++;
+ if (got_bracket) {
+ /* if there was an opening parenthesis, then the corresponding
+ closing parenthesis should be right here */
+ if (*s != ')')
+ goto parse_error;
+ s++;
+ while (Py_ISSPACE(*s))
+ s++;
+ }
+
+ /* we should now be at the end of the string */
+ if (s-start != len)
+ goto parse_error;
+
+ return complex_subtype_from_doubles(type, x, y);
parse_error:
- PyErr_SetString(PyExc_ValueError,
- "complex() arg is a malformed string");
- return NULL;
+ PyErr_SetString(PyExc_ValueError,
+ "complex() arg is a malformed string");
+ return NULL;
}
static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *r, *i, *tmp, *f;
- PyNumberMethods *nbr, *nbi = NULL;
- Py_complex cr, ci;
- int own_r = 0;
- int cr_is_complex = 0;
- int ci_is_complex = 0;
- static PyObject *complexstr;
- static char *kwlist[] = {"real", "imag", 0};
-
- r = Py_False;
- i = NULL;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
- &r, &i))
- return NULL;
-
- /* Special-case for a single argument when type(arg) is complex. */
- if (PyComplex_CheckExact(r) && i == NULL &&
- type == &PyComplex_Type) {
- /* Note that we can't know whether it's safe to return
- a complex *subclass* instance as-is, hence the restriction
- to exact complexes here. If either the input or the
- output is a complex subclass, it will be handled below
- as a non-orthogonal vector. */
- Py_INCREF(r);
- return r;
- }
- if (PyUnicode_Check(r)) {
- if (i != NULL) {
- PyErr_SetString(PyExc_TypeError,
- "complex() can't take second arg"
- " if first is a string");
- return NULL;
- }
- return complex_subtype_from_string(type, r);
- }
- if (i != NULL && PyUnicode_Check(i)) {
- PyErr_SetString(PyExc_TypeError,
- "complex() second arg can't be a string");
- return NULL;
- }
-
- /* XXX Hack to support classes with __complex__ method */
- if (complexstr == NULL) {
- complexstr = PyUnicode_InternFromString("__complex__");
- if (complexstr == NULL)
- return NULL;
- }
- f = PyObject_GetAttr(r, complexstr);
- if (f == NULL)
- PyErr_Clear();
- else {
- PyObject *args = PyTuple_New(0);
- if (args == NULL)
- return NULL;
- r = PyEval_CallObject(f, args);
- Py_DECREF(args);
- Py_DECREF(f);
- if (r == NULL)
- return NULL;
- own_r = 1;
- }
- nbr = r->ob_type->tp_as_number;
- if (i != NULL)
- nbi = i->ob_type->tp_as_number;
- if (nbr == NULL || nbr->nb_float == NULL ||
- ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
- PyErr_SetString(PyExc_TypeError,
- "complex() argument must be a string or a number");
- if (own_r) {
- Py_DECREF(r);
- }
- return NULL;
- }
-
- /* If we get this far, then the "real" and "imag" parts should
- both be treated as numbers, and the constructor should return a
- complex number equal to (real + imag*1j).
-
- Note that we do NOT assume the input to already be in canonical
- form; the "real" and "imag" parts might themselves be complex
- numbers, which slightly complicates the code below. */
- if (PyComplex_Check(r)) {
- /* Note that if r is of a complex subtype, we're only
- retaining its real & imag parts here, and the return
- value is (properly) of the builtin complex type. */
- cr = ((PyComplexObject*)r)->cval;
- cr_is_complex = 1;
- if (own_r) {
- Py_DECREF(r);
- }
- }
- else {
- /* The "real" part really is entirely real, and contributes
- nothing in the imaginary direction.
- Just treat it as a double. */
- tmp = PyNumber_Float(r);
- if (own_r) {
- /* r was a newly created complex number, rather
- than the original "real" argument. */
- Py_DECREF(r);
- }
- if (tmp == NULL)
- return NULL;
- if (!PyFloat_Check(tmp)) {
- PyErr_SetString(PyExc_TypeError,
- "float(r) didn't return a float");
- Py_DECREF(tmp);
- return NULL;
- }
- cr.real = PyFloat_AsDouble(tmp);
- cr.imag = 0.0; /* Shut up compiler warning */
- Py_DECREF(tmp);
- }
- if (i == NULL) {
- ci.real = 0.0;
- }
- else if (PyComplex_Check(i)) {
- ci = ((PyComplexObject*)i)->cval;
- ci_is_complex = 1;
- } else {
- /* The "imag" part really is entirely imaginary, and
- contributes nothing in the real direction.
- Just treat it as a double. */
- tmp = (*nbi->nb_float)(i);
- if (tmp == NULL)
- return NULL;
- ci.real = PyFloat_AsDouble(tmp);
- Py_DECREF(tmp);
- }
- /* If the input was in canonical form, then the "real" and "imag"
- parts are real numbers, so that ci.imag and cr.imag are zero.
- We need this correction in case they were not real numbers. */
-
- if (ci_is_complex) {
- cr.real -= ci.imag;
- }
- if (cr_is_complex) {
- ci.real += cr.imag;
- }
- return complex_subtype_from_doubles(type, cr.real, ci.real);
+ PyObject *r, *i, *tmp, *f;
+ PyNumberMethods *nbr, *nbi = NULL;
+ Py_complex cr, ci;
+ int own_r = 0;
+ int cr_is_complex = 0;
+ int ci_is_complex = 0;
+ static PyObject *complexstr;
+ static char *kwlist[] = {"real", "imag", 0};
+
+ r = Py_False;
+ i = NULL;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
+ &r, &i))
+ return NULL;
+
+ /* Special-case for a single argument when type(arg) is complex. */
+ if (PyComplex_CheckExact(r) && i == NULL &&
+ type == &PyComplex_Type) {
+ /* Note that we can't know whether it's safe to return
+ a complex *subclass* instance as-is, hence the restriction
+ to exact complexes here. If either the input or the
+ output is a complex subclass, it will be handled below
+ as a non-orthogonal vector. */
+ Py_INCREF(r);
+ return r;
+ }
+ if (PyUnicode_Check(r)) {
+ if (i != NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "complex() can't take second arg"
+ " if first is a string");
+ return NULL;
+ }
+ return complex_subtype_from_string(type, r);
+ }
+ if (i != NULL && PyUnicode_Check(i)) {
+ PyErr_SetString(PyExc_TypeError,
+ "complex() second arg can't be a string");
+ return NULL;
+ }
+
+ /* XXX Hack to support classes with __complex__ method */
+ if (complexstr == NULL) {
+ complexstr = PyUnicode_InternFromString("__complex__");
+ if (complexstr == NULL)
+ return NULL;
+ }
+ f = PyObject_GetAttr(r, complexstr);
+ if (f == NULL)
+ PyErr_Clear();
+ else {
+ PyObject *args = PyTuple_New(0);
+ if (args == NULL)
+ return NULL;
+ r = PyEval_CallObject(f, args);
+ Py_DECREF(args);
+ Py_DECREF(f);
+ if (r == NULL)
+ return NULL;
+ own_r = 1;
+ }
+ nbr = r->ob_type->tp_as_number;
+ if (i != NULL)
+ nbi = i->ob_type->tp_as_number;
+ if (nbr == NULL || nbr->nb_float == NULL ||
+ ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
+ PyErr_SetString(PyExc_TypeError,
+ "complex() argument must be a string or a number");
+ if (own_r) {
+ Py_DECREF(r);
+ }
+ return NULL;
+ }
+
+ /* If we get this far, then the "real" and "imag" parts should
+ both be treated as numbers, and the constructor should return a
+ complex number equal to (real + imag*1j).
+
+ Note that we do NOT assume the input to already be in canonical
+ form; the "real" and "imag" parts might themselves be complex
+ numbers, which slightly complicates the code below. */
+ if (PyComplex_Check(r)) {
+ /* Note that if r is of a complex subtype, we're only
+ retaining its real & imag parts here, and the return
+ value is (properly) of the builtin complex type. */
+ cr = ((PyComplexObject*)r)->cval;
+ cr_is_complex = 1;
+ if (own_r) {
+ Py_DECREF(r);
+ }
+ }
+ else {
+ /* The "real" part really is entirely real, and contributes
+ nothing in the imaginary direction.
+ Just treat it as a double. */
+ tmp = PyNumber_Float(r);
+ if (own_r) {
+ /* r was a newly created complex number, rather
+ than the original "real" argument. */
+ Py_DECREF(r);
+ }
+ if (tmp == NULL)
+ return NULL;
+ if (!PyFloat_Check(tmp)) {
+ PyErr_SetString(PyExc_TypeError,
+ "float(r) didn't return a float");
+ Py_DECREF(tmp);
+ return NULL;
+ }
+ cr.real = PyFloat_AsDouble(tmp);
+ cr.imag = 0.0; /* Shut up compiler warning */
+ Py_DECREF(tmp);
+ }
+ if (i == NULL) {
+ ci.real = 0.0;
+ }
+ else if (PyComplex_Check(i)) {
+ ci = ((PyComplexObject*)i)->cval;
+ ci_is_complex = 1;
+ } else {
+ /* The "imag" part really is entirely imaginary, and
+ contributes nothing in the real direction.
+ Just treat it as a double. */
+ tmp = (*nbi->nb_float)(i);
+ if (tmp == NULL)
+ return NULL;
+ ci.real = PyFloat_AsDouble(tmp);
+ Py_DECREF(tmp);
+ }
+ /* If the input was in canonical form, then the "real" and "imag"
+ parts are real numbers, so that ci.imag and cr.imag are zero.
+ We need this correction in case they were not real numbers. */
+
+ if (ci_is_complex) {
+ cr.real -= ci.imag;
+ }
+ if (cr_is_complex) {
+ ci.real += cr.imag;
+ }
+ return complex_subtype_from_doubles(type, cr.real, ci.real);
}
PyDoc_STRVAR(complex_doc,
@@ -1032,81 +1032,81 @@ PyDoc_STRVAR(complex_doc,
"This is equivalent to (real + imag*1j) where imag defaults to 0.");
static PyNumberMethods complex_as_number = {
- (binaryfunc)complex_add, /* nb_add */
- (binaryfunc)complex_sub, /* nb_subtract */
- (binaryfunc)complex_mul, /* nb_multiply */
- (binaryfunc)complex_remainder, /* nb_remainder */
- (binaryfunc)complex_divmod, /* nb_divmod */
- (ternaryfunc)complex_pow, /* nb_power */
- (unaryfunc)complex_neg, /* nb_negative */
- (unaryfunc)complex_pos, /* nb_positive */
- (unaryfunc)complex_abs, /* nb_absolute */
- (inquiry)complex_bool, /* nb_bool */
- 0, /* nb_invert */
- 0, /* nb_lshift */
- 0, /* nb_rshift */
- 0, /* nb_and */
- 0, /* nb_xor */
- 0, /* nb_or */
- complex_int, /* nb_int */
- 0, /* nb_reserved */
- complex_float, /* nb_float */
- 0, /* nb_inplace_add */
- 0, /* nb_inplace_subtract */
- 0, /* nb_inplace_multiply*/
- 0, /* nb_inplace_remainder */
- 0, /* nb_inplace_power */
- 0, /* nb_inplace_lshift */
- 0, /* nb_inplace_rshift */
- 0, /* nb_inplace_and */
- 0, /* nb_inplace_xor */
- 0, /* nb_inplace_or */
- (binaryfunc)complex_int_div, /* nb_floor_divide */
- (binaryfunc)complex_div, /* nb_true_divide */
- 0, /* nb_inplace_floor_divide */
- 0, /* nb_inplace_true_divide */
+ (binaryfunc)complex_add, /* nb_add */
+ (binaryfunc)complex_sub, /* nb_subtract */
+ (binaryfunc)complex_mul, /* nb_multiply */
+ (binaryfunc)complex_remainder, /* nb_remainder */
+ (binaryfunc)complex_divmod, /* nb_divmod */
+ (ternaryfunc)complex_pow, /* nb_power */
+ (unaryfunc)complex_neg, /* nb_negative */
+ (unaryfunc)complex_pos, /* nb_positive */
+ (unaryfunc)complex_abs, /* nb_absolute */
+ (inquiry)complex_bool, /* nb_bool */
+ 0, /* nb_invert */
+ 0, /* nb_lshift */
+ 0, /* nb_rshift */
+ 0, /* nb_and */
+ 0, /* nb_xor */
+ 0, /* nb_or */
+ complex_int, /* nb_int */
+ 0, /* nb_reserved */
+ complex_float, /* nb_float */
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply*/
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ (binaryfunc)complex_int_div, /* nb_floor_divide */
+ (binaryfunc)complex_div, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
};
PyTypeObject PyComplex_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "complex",
- sizeof(PyComplexObject),
- 0,
- complex_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)complex_repr, /* tp_repr */
- &complex_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)complex_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)complex_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- complex_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- complex_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- complex_methods, /* tp_methods */
- complex_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- complex_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "complex",
+ sizeof(PyComplexObject),
+ 0,
+ complex_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)complex_repr, /* tp_repr */
+ &complex_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)complex_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)complex_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ complex_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ complex_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ complex_methods, /* tp_methods */
+ complex_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ complex_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
#endif
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 81e6be9..5e59271 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -6,647 +6,647 @@
static void
descr_dealloc(PyDescrObject *descr)
{
- _PyObject_GC_UNTRACK(descr);
- Py_XDECREF(descr->d_type);
- Py_XDECREF(descr->d_name);
- PyObject_GC_Del(descr);
+ _PyObject_GC_UNTRACK(descr);
+ Py_XDECREF(descr->d_type);
+ Py_XDECREF(descr->d_name);
+ PyObject_GC_Del(descr);
}
static PyObject *
descr_name(PyDescrObject *descr)
{
- if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
- return descr->d_name;
- return NULL;
+ if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
+ return descr->d_name;
+ return NULL;
}
static PyObject *
descr_repr(PyDescrObject *descr, char *format)
{
- PyObject *name = NULL;
- if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
- name = descr->d_name;
+ PyObject *name = NULL;
+ if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
+ name = descr->d_name;
- return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
+ return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
}
static PyObject *
method_repr(PyMethodDescrObject *descr)
{
- return descr_repr((PyDescrObject *)descr,
- "<method '%V' of '%s' objects>");
+ return descr_repr((PyDescrObject *)descr,
+ "<method '%V' of '%s' objects>");
}
static PyObject *
member_repr(PyMemberDescrObject *descr)
{
- return descr_repr((PyDescrObject *)descr,
- "<member '%V' of '%s' objects>");
+ return descr_repr((PyDescrObject *)descr,
+ "<member '%V' of '%s' objects>");
}
static PyObject *
getset_repr(PyGetSetDescrObject *descr)
{
- return descr_repr((PyDescrObject *)descr,
- "<attribute '%V' of '%s' objects>");
+ return descr_repr((PyDescrObject *)descr,
+ "<attribute '%V' of '%s' objects>");
}
static PyObject *
wrapperdescr_repr(PyWrapperDescrObject *descr)
{
- return descr_repr((PyDescrObject *)descr,
- "<slot wrapper '%V' of '%s' objects>");
+ return descr_repr((PyDescrObject *)descr,
+ "<slot wrapper '%V' of '%s' objects>");
}
static int
descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
{
- if (obj == NULL) {
- Py_INCREF(descr);
- *pres = (PyObject *)descr;
- return 1;
- }
- if (!PyObject_TypeCheck(obj, descr->d_type)) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' for '%s' objects "
- "doesn't apply to '%s' object",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name,
- obj->ob_type->tp_name);
- *pres = NULL;
- return 1;
- }
- return 0;
+ if (obj == NULL) {
+ Py_INCREF(descr);
+ *pres = (PyObject *)descr;
+ return 1;
+ }
+ if (!PyObject_TypeCheck(obj, descr->d_type)) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' for '%s' objects "
+ "doesn't apply to '%s' object",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name,
+ obj->ob_type->tp_name);
+ *pres = NULL;
+ return 1;
+ }
+ return 0;
}
static PyObject *
classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
{
- /* Ensure a valid type. Class methods ignore obj. */
- if (type == NULL) {
- if (obj != NULL)
- type = (PyObject *)obj->ob_type;
- else {
- /* Wot - no type?! */
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' for type '%s' "
- "needs either an object or a type",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name);
- return NULL;
- }
- }
- if (!PyType_Check(type)) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' for type '%s' "
- "needs a type, not a '%s' as arg 2",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name,
- type->ob_type->tp_name);
- return NULL;
- }
- if (!PyType_IsSubtype((PyTypeObject *)type, descr->d_type)) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' for type '%s' "
- "doesn't apply to type '%s'",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name,
- ((PyTypeObject *)type)->tp_name);
- return NULL;
- }
- return PyCFunction_New(descr->d_method, type);
+ /* Ensure a valid type. Class methods ignore obj. */
+ if (type == NULL) {
+ if (obj != NULL)
+ type = (PyObject *)obj->ob_type;
+ else {
+ /* Wot - no type?! */
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' for type '%s' "
+ "needs either an object or a type",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name);
+ return NULL;
+ }
+ }
+ if (!PyType_Check(type)) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' for type '%s' "
+ "needs a type, not a '%s' as arg 2",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name,
+ type->ob_type->tp_name);
+ return NULL;
+ }
+ if (!PyType_IsSubtype((PyTypeObject *)type, descr->d_type)) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' for type '%s' "
+ "doesn't apply to type '%s'",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name,
+ ((PyTypeObject *)type)->tp_name);
+ return NULL;
+ }
+ return PyCFunction_New(descr->d_method, type);
}
static PyObject *
method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
+ PyObject *res;
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
- return PyCFunction_New(descr->d_method, obj);
+ if (descr_check((PyDescrObject *)descr, obj, &res))
+ return res;
+ return PyCFunction_New(descr->d_method, obj);
}
static PyObject *
member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
+ PyObject *res;
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
- return PyMember_GetOne((char *)obj, descr->d_member);
+ if (descr_check((PyDescrObject *)descr, obj, &res))
+ return res;
+ return PyMember_GetOne((char *)obj, descr->d_member);
}
static PyObject *
getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
-
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
- if (descr->d_getset->get != NULL)
- return descr->d_getset->get(obj, descr->d_getset->closure);
- PyErr_Format(PyExc_AttributeError,
- "attribute '%V' of '%.100s' objects is not readable",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name);
- return NULL;
+ PyObject *res;
+
+ if (descr_check((PyDescrObject *)descr, obj, &res))
+ return res;
+ if (descr->d_getset->get != NULL)
+ return descr->d_getset->get(obj, descr->d_getset->closure);
+ PyErr_Format(PyExc_AttributeError,
+ "attribute '%V' of '%.100s' objects is not readable",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name);
+ return NULL;
}
static PyObject *
wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
{
- PyObject *res;
+ PyObject *res;
- if (descr_check((PyDescrObject *)descr, obj, &res))
- return res;
- return PyWrapper_New((PyObject *)descr, obj);
+ if (descr_check((PyDescrObject *)descr, obj, &res))
+ return res;
+ return PyWrapper_New((PyObject *)descr, obj);
}
static int
descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
- int *pres)
+ int *pres)
{
- assert(obj != NULL);
- if (!PyObject_TypeCheck(obj, descr->d_type)) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' for '%.100s' objects "
- "doesn't apply to '%.100s' object",
- descr_name(descr), "?",
- descr->d_type->tp_name,
- obj->ob_type->tp_name);
- *pres = -1;
- return 1;
- }
- return 0;
+ assert(obj != NULL);
+ if (!PyObject_TypeCheck(obj, descr->d_type)) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' for '%.100s' objects "
+ "doesn't apply to '%.100s' object",
+ descr_name(descr), "?",
+ descr->d_type->tp_name,
+ obj->ob_type->tp_name);
+ *pres = -1;
+ return 1;
+ }
+ return 0;
}
static int
member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
{
- int res;
+ int res;
- if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
- return res;
- return PyMember_SetOne((char *)obj, descr->d_member, value);
+ if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
+ return res;
+ return PyMember_SetOne((char *)obj, descr->d_member, value);
}
static int
getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
{
- int res;
-
- if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
- return res;
- if (descr->d_getset->set != NULL)
- return descr->d_getset->set(obj, value,
- descr->d_getset->closure);
- PyErr_Format(PyExc_AttributeError,
- "attribute '%V' of '%.100s' objects is not writable",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name);
- return -1;
+ int res;
+
+ if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
+ return res;
+ if (descr->d_getset->set != NULL)
+ return descr->d_getset->set(obj, value,
+ descr->d_getset->closure);
+ PyErr_Format(PyExc_AttributeError,
+ "attribute '%V' of '%.100s' objects is not writable",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name);
+ return -1;
}
static PyObject *
methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
{
- Py_ssize_t argc;
- PyObject *self, *func, *result;
-
- /* Make sure that the first argument is acceptable as 'self' */
- assert(PyTuple_Check(args));
- argc = PyTuple_GET_SIZE(args);
- if (argc < 1) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' of '%.100s' "
- "object needs an argument",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name);
- return NULL;
- }
- self = PyTuple_GET_ITEM(args, 0);
- if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' "
- "requires a '%.100s' object "
- "but received a '%.100s'",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name,
- self->ob_type->tp_name);
- return NULL;
- }
-
- func = PyCFunction_New(descr->d_method, self);
- if (func == NULL)
- return NULL;
- args = PyTuple_GetSlice(args, 1, argc);
- if (args == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- result = PyEval_CallObjectWithKeywords(func, args, kwds);
- Py_DECREF(args);
- Py_DECREF(func);
- return result;
+ Py_ssize_t argc;
+ PyObject *self, *func, *result;
+
+ /* Make sure that the first argument is acceptable as 'self' */
+ assert(PyTuple_Check(args));
+ argc = PyTuple_GET_SIZE(args);
+ if (argc < 1) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' of '%.100s' "
+ "object needs an argument",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name);
+ return NULL;
+ }
+ self = PyTuple_GET_ITEM(args, 0);
+ if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' "
+ "requires a '%.100s' object "
+ "but received a '%.100s'",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name,
+ self->ob_type->tp_name);
+ return NULL;
+ }
+
+ func = PyCFunction_New(descr->d_method, self);
+ if (func == NULL)
+ return NULL;
+ args = PyTuple_GetSlice(args, 1, argc);
+ if (args == NULL) {
+ Py_DECREF(func);
+ return NULL;
+ }
+ result = PyEval_CallObjectWithKeywords(func, args, kwds);
+ Py_DECREF(args);
+ Py_DECREF(func);
+ return result;
}
static PyObject *
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
- PyObject *kwds)
+ PyObject *kwds)
{
- PyObject *func, *result;
+ PyObject *func, *result;
- func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
- if (func == NULL)
- return NULL;
+ func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
+ if (func == NULL)
+ return NULL;
- result = PyEval_CallObjectWithKeywords(func, args, kwds);
- Py_DECREF(func);
- return result;
+ result = PyEval_CallObjectWithKeywords(func, args, kwds);
+ Py_DECREF(func);
+ return result;
}
static PyObject *
wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
{
- Py_ssize_t argc;
- PyObject *self, *func, *result;
-
- /* Make sure that the first argument is acceptable as 'self' */
- assert(PyTuple_Check(args));
- argc = PyTuple_GET_SIZE(args);
- if (argc < 1) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' of '%.100s' "
- "object needs an argument",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name);
- return NULL;
- }
- self = PyTuple_GET_ITEM(args, 0);
- if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
- PyErr_Format(PyExc_TypeError,
- "descriptor '%V' "
- "requires a '%.100s' object "
- "but received a '%.100s'",
- descr_name((PyDescrObject *)descr), "?",
- descr->d_type->tp_name,
- self->ob_type->tp_name);
- return NULL;
- }
-
- func = PyWrapper_New((PyObject *)descr, self);
- if (func == NULL)
- return NULL;
- args = PyTuple_GetSlice(args, 1, argc);
- if (args == NULL) {
- Py_DECREF(func);
- return NULL;
- }
- result = PyEval_CallObjectWithKeywords(func, args, kwds);
- Py_DECREF(args);
- Py_DECREF(func);
- return result;
+ Py_ssize_t argc;
+ PyObject *self, *func, *result;
+
+ /* Make sure that the first argument is acceptable as 'self' */
+ assert(PyTuple_Check(args));
+ argc = PyTuple_GET_SIZE(args);
+ if (argc < 1) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' of '%.100s' "
+ "object needs an argument",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name);
+ return NULL;
+ }
+ self = PyTuple_GET_ITEM(args, 0);
+ if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
+ PyErr_Format(PyExc_TypeError,
+ "descriptor '%V' "
+ "requires a '%.100s' object "
+ "but received a '%.100s'",
+ descr_name((PyDescrObject *)descr), "?",
+ descr->d_type->tp_name,
+ self->ob_type->tp_name);
+ return NULL;
+ }
+
+ func = PyWrapper_New((PyObject *)descr, self);
+ if (func == NULL)
+ return NULL;
+ args = PyTuple_GetSlice(args, 1, argc);
+ if (args == NULL) {
+ Py_DECREF(func);
+ return NULL;
+ }
+ result = PyEval_CallObjectWithKeywords(func, args, kwds);
+ Py_DECREF(args);
+ Py_DECREF(func);
+ return result;
}
static PyObject *
method_get_doc(PyMethodDescrObject *descr, void *closure)
{
- if (descr->d_method->ml_doc == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyUnicode_FromString(descr->d_method->ml_doc);
+ if (descr->d_method->ml_doc == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyUnicode_FromString(descr->d_method->ml_doc);
}
static PyMemberDef descr_members[] = {
- {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
- {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
- {0}
+ {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
+ {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
+ {0}
};
static PyGetSetDef method_getset[] = {
- {"__doc__", (getter)method_get_doc},
- {0}
+ {"__doc__", (getter)method_get_doc},
+ {0}
};
static PyObject *
member_get_doc(PyMemberDescrObject *descr, void *closure)
{
- if (descr->d_member->doc == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyUnicode_FromString(descr->d_member->doc);
+ if (descr->d_member->doc == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyUnicode_FromString(descr->d_member->doc);
}
static PyGetSetDef member_getset[] = {
- {"__doc__", (getter)member_get_doc},
- {0}
+ {"__doc__", (getter)member_get_doc},
+ {0}
};
static PyObject *
getset_get_doc(PyGetSetDescrObject *descr, void *closure)
{
- if (descr->d_getset->doc == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyUnicode_FromString(descr->d_getset->doc);
+ if (descr->d_getset->doc == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyUnicode_FromString(descr->d_getset->doc);
}
static PyGetSetDef getset_getset[] = {
- {"__doc__", (getter)getset_get_doc},
- {0}
+ {"__doc__", (getter)getset_get_doc},
+ {0}
};
static PyObject *
wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
{
- if (descr->d_base->doc == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyUnicode_FromString(descr->d_base->doc);
+ if (descr->d_base->doc == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyUnicode_FromString(descr->d_base->doc);
}
static PyGetSetDef wrapperdescr_getset[] = {
- {"__doc__", (getter)wrapperdescr_get_doc},
- {0}
+ {"__doc__", (getter)wrapperdescr_get_doc},
+ {0}
};
static int
descr_traverse(PyObject *self, visitproc visit, void *arg)
{
- PyDescrObject *descr = (PyDescrObject *)self;
- Py_VISIT(descr->d_type);
- return 0;
+ PyDescrObject *descr = (PyDescrObject *)self;
+ Py_VISIT(descr->d_type);
+ return 0;
}
PyTypeObject PyMethodDescr_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "method_descriptor",
- sizeof(PyMethodDescrObject),
- 0,
- (destructor)descr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)method_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)methoddescr_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- descr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- descr_members, /* tp_members */
- method_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)method_get, /* tp_descr_get */
- 0, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "method_descriptor",
+ sizeof(PyMethodDescrObject),
+ 0,
+ (destructor)descr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)method_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)methoddescr_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ descr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ descr_members, /* tp_members */
+ method_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)method_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
};
/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
PyTypeObject PyClassMethodDescr_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "classmethod_descriptor",
- sizeof(PyMethodDescrObject),
- 0,
- (destructor)descr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)method_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)classmethoddescr_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- descr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- descr_members, /* tp_members */
- method_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)classmethod_get, /* tp_descr_get */
- 0, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "classmethod_descriptor",
+ sizeof(PyMethodDescrObject),
+ 0,
+ (destructor)descr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)method_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)classmethoddescr_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ descr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ descr_members, /* tp_members */
+ method_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)classmethod_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
};
PyTypeObject PyMemberDescr_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "member_descriptor",
- sizeof(PyMemberDescrObject),
- 0,
- (destructor)descr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)member_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- descr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- descr_members, /* tp_members */
- member_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)member_get, /* tp_descr_get */
- (descrsetfunc)member_set, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "member_descriptor",
+ sizeof(PyMemberDescrObject),
+ 0,
+ (destructor)descr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)member_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ descr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ descr_members, /* tp_members */
+ member_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)member_get, /* tp_descr_get */
+ (descrsetfunc)member_set, /* tp_descr_set */
};
PyTypeObject PyGetSetDescr_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "getset_descriptor",
- sizeof(PyGetSetDescrObject),
- 0,
- (destructor)descr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)getset_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- descr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- descr_members, /* tp_members */
- getset_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)getset_get, /* tp_descr_get */
- (descrsetfunc)getset_set, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "getset_descriptor",
+ sizeof(PyGetSetDescrObject),
+ 0,
+ (destructor)descr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)getset_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ descr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ descr_members, /* tp_members */
+ getset_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)getset_get, /* tp_descr_get */
+ (descrsetfunc)getset_set, /* tp_descr_set */
};
PyTypeObject PyWrapperDescr_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "wrapper_descriptor",
- sizeof(PyWrapperDescrObject),
- 0,
- (destructor)descr_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)wrapperdescr_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)wrapperdescr_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- descr_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- descr_members, /* tp_members */
- wrapperdescr_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "wrapper_descriptor",
+ sizeof(PyWrapperDescrObject),
+ 0,
+ (destructor)descr_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)wrapperdescr_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)wrapperdescr_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ descr_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ descr_members, /* tp_members */
+ wrapperdescr_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
};
static PyDescrObject *
descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
{
- PyDescrObject *descr;
-
- descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
- if (descr != NULL) {
- Py_XINCREF(type);
- descr->d_type = type;
- descr->d_name = PyUnicode_InternFromString(name);
- if (descr->d_name == NULL) {
- Py_DECREF(descr);
- descr = NULL;
- }
- }
- return descr;
+ PyDescrObject *descr;
+
+ descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
+ if (descr != NULL) {
+ Py_XINCREF(type);
+ descr->d_type = type;
+ descr->d_name = PyUnicode_InternFromString(name);
+ if (descr->d_name == NULL) {
+ Py_DECREF(descr);
+ descr = NULL;
+ }
+ }
+ return descr;
}
PyObject *
PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
{
- PyMethodDescrObject *descr;
+ PyMethodDescrObject *descr;
- descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
- type, method->ml_name);
- if (descr != NULL)
- descr->d_method = method;
- return (PyObject *)descr;
+ descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
+ type, method->ml_name);
+ if (descr != NULL)
+ descr->d_method = method;
+ return (PyObject *)descr;
}
PyObject *
PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
{
- PyMethodDescrObject *descr;
+ PyMethodDescrObject *descr;
- descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
- type, method->ml_name);
- if (descr != NULL)
- descr->d_method = method;
- return (PyObject *)descr;
+ descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
+ type, method->ml_name);
+ if (descr != NULL)
+ descr->d_method = method;
+ return (PyObject *)descr;
}
PyObject *
PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
{
- PyMemberDescrObject *descr;
+ PyMemberDescrObject *descr;
- descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
- type, member->name);
- if (descr != NULL)
- descr->d_member = member;
- return (PyObject *)descr;
+ descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
+ type, member->name);
+ if (descr != NULL)
+ descr->d_member = member;
+ return (PyObject *)descr;
}
PyObject *
PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
{
- PyGetSetDescrObject *descr;
+ PyGetSetDescrObject *descr;
- descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
- type, getset->name);
- if (descr != NULL)
- descr->d_getset = getset;
- return (PyObject *)descr;
+ descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
+ type, getset->name);
+ if (descr != NULL)
+ descr->d_getset = getset;
+ return (PyObject *)descr;
}
PyObject *
PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
{
- PyWrapperDescrObject *descr;
-
- descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
- type, base->name);
- if (descr != NULL) {
- descr->d_base = base;
- descr->d_wrapped = wrapped;
- }
- return (PyObject *)descr;
+ PyWrapperDescrObject *descr;
+
+ descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
+ type, base->name);
+ if (descr != NULL) {
+ descr->d_base = base;
+ descr->d_wrapped = wrapped;
+ }
+ return (PyObject *)descr;
}
@@ -656,180 +656,180 @@ PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
bit of a pain */
typedef struct {
- PyObject_HEAD
- PyObject *dict;
+ PyObject_HEAD
+ PyObject *dict;
} proxyobject;
static Py_ssize_t
proxy_len(proxyobject *pp)
{
- return PyObject_Size(pp->dict);
+ return PyObject_Size(pp->dict);
}
static PyObject *
proxy_getitem(proxyobject *pp, PyObject *key)
{
- return PyObject_GetItem(pp->dict, key);
+ return PyObject_GetItem(pp->dict, key);
}
static PyMappingMethods proxy_as_mapping = {
- (lenfunc)proxy_len, /* mp_length */
- (binaryfunc)proxy_getitem, /* mp_subscript */
- 0, /* mp_ass_subscript */
+ (lenfunc)proxy_len, /* mp_length */
+ (binaryfunc)proxy_getitem, /* mp_subscript */
+ 0, /* mp_ass_subscript */
};
static int
proxy_contains(proxyobject *pp, PyObject *key)
{
- return PyDict_Contains(pp->dict, key);
+ return PyDict_Contains(pp->dict, key);
}
static PySequenceMethods proxy_as_sequence = {
- 0, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)proxy_contains, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)proxy_contains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
};
static PyObject *
proxy_get(proxyobject *pp, PyObject *args)
{
- PyObject *key, *def = Py_None;
+ PyObject *key, *def = Py_None;
- if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
- return NULL;
- return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
+ if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
+ return NULL;
+ return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
}
static PyObject *
proxy_keys(proxyobject *pp)
{
- return PyMapping_Keys(pp->dict);
+ return PyMapping_Keys(pp->dict);
}
static PyObject *
proxy_values(proxyobject *pp)
{
- return PyMapping_Values(pp->dict);
+ return PyMapping_Values(pp->dict);
}
static PyObject *
proxy_items(proxyobject *pp)
{
- return PyMapping_Items(pp->dict);
+ return PyMapping_Items(pp->dict);
}
static PyObject *
proxy_copy(proxyobject *pp)
{
- return PyObject_CallMethod(pp->dict, "copy", NULL);
+ return PyObject_CallMethod(pp->dict, "copy", NULL);
}
static PyMethodDef proxy_methods[] = {
- {"get", (PyCFunction)proxy_get, METH_VARARGS,
- PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
- " d defaults to None.")},
- {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
- PyDoc_STR("D.keys() -> list of D's keys")},
- {"values", (PyCFunction)proxy_values, METH_NOARGS,
- PyDoc_STR("D.values() -> list of D's values")},
- {"items", (PyCFunction)proxy_items, METH_NOARGS,
- PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
- {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
- PyDoc_STR("D.copy() -> a shallow copy of D")},
- {0}
+ {"get", (PyCFunction)proxy_get, METH_VARARGS,
+ PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
+ " d defaults to None.")},
+ {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
+ PyDoc_STR("D.keys() -> list of D's keys")},
+ {"values", (PyCFunction)proxy_values, METH_NOARGS,
+ PyDoc_STR("D.values() -> list of D's values")},
+ {"items", (PyCFunction)proxy_items, METH_NOARGS,
+ PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
+ {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
+ PyDoc_STR("D.copy() -> a shallow copy of D")},
+ {0}
};
static void
proxy_dealloc(proxyobject *pp)
{
- _PyObject_GC_UNTRACK(pp);
- Py_DECREF(pp->dict);
- PyObject_GC_Del(pp);
+ _PyObject_GC_UNTRACK(pp);
+ Py_DECREF(pp->dict);
+ PyObject_GC_Del(pp);
}
static PyObject *
proxy_getiter(proxyobject *pp)
{
- return PyObject_GetIter(pp->dict);
+ return PyObject_GetIter(pp->dict);
}
static PyObject *
proxy_str(proxyobject *pp)
{
- return PyObject_Str(pp->dict);
+ return PyObject_Str(pp->dict);
}
static int
proxy_traverse(PyObject *self, visitproc visit, void *arg)
{
- proxyobject *pp = (proxyobject *)self;
- Py_VISIT(pp->dict);
- return 0;
+ proxyobject *pp = (proxyobject *)self;
+ Py_VISIT(pp->dict);
+ return 0;
}
static PyObject *
proxy_richcompare(proxyobject *v, PyObject *w, int op)
{
- return PyObject_RichCompare(v->dict, w, op);
+ return PyObject_RichCompare(v->dict, w, op);
}
PyTypeObject PyDictProxy_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_proxy", /* tp_name */
- sizeof(proxyobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)proxy_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- &proxy_as_sequence, /* tp_as_sequence */
- &proxy_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)proxy_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- proxy_traverse, /* tp_traverse */
- 0, /* tp_clear */
- (richcmpfunc)proxy_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)proxy_getiter, /* tp_iter */
- 0, /* tp_iternext */
- proxy_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_proxy", /* tp_name */
+ sizeof(proxyobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)proxy_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ &proxy_as_sequence, /* tp_as_sequence */
+ &proxy_as_mapping, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)proxy_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ proxy_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ (richcmpfunc)proxy_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ (getiterfunc)proxy_getiter, /* tp_iter */
+ 0, /* tp_iternext */
+ proxy_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
};
PyObject *
PyDictProxy_New(PyObject *dict)
{
- proxyobject *pp;
-
- pp = PyObject_GC_New(proxyobject, &PyDictProxy_Type);
- if (pp != NULL) {
- Py_INCREF(dict);
- pp->dict = dict;
- _PyObject_GC_TRACK(pp);
- }
- return (PyObject *)pp;
+ proxyobject *pp;
+
+ pp = PyObject_GC_New(proxyobject, &PyDictProxy_Type);
+ if (pp != NULL) {
+ Py_INCREF(dict);
+ pp->dict = dict;
+ _PyObject_GC_TRACK(pp);
+ }
+ return (PyObject *)pp;
}
@@ -842,9 +842,9 @@ PyDictProxy_New(PyObject *dict)
static PyTypeObject wrappertype;
typedef struct {
- PyObject_HEAD
- PyWrapperDescrObject *descr;
- PyObject *self;
+ PyObject_HEAD
+ PyWrapperDescrObject *descr;
+ PyObject *self;
} wrapperobject;
#define Wrapper_Check(v) (Py_TYPE(v) == &wrappertype)
@@ -852,12 +852,12 @@ typedef struct {
static void
wrapper_dealloc(wrapperobject *wp)
{
- PyObject_GC_UnTrack(wp);
- Py_TRASHCAN_SAFE_BEGIN(wp)
- Py_XDECREF(wp->descr);
- Py_XDECREF(wp->self);
- PyObject_GC_Del(wp);
- Py_TRASHCAN_SAFE_END(wp)
+ PyObject_GC_UnTrack(wp);
+ Py_TRASHCAN_SAFE_BEGIN(wp)
+ Py_XDECREF(wp->descr);
+ Py_XDECREF(wp->self);
+ PyObject_GC_Del(wp);
+ Py_TRASHCAN_SAFE_END(wp)
}
#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
@@ -865,211 +865,211 @@ wrapper_dealloc(wrapperobject *wp)
static PyObject *
wrapper_richcompare(PyObject *a, PyObject *b, int op)
{
- int result;
- PyObject *v;
- PyWrapperDescrObject *a_descr, *b_descr;
-
- assert(a != NULL && b != NULL);
-
- /* both arguments should be wrapperobjects */
- if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
- v = Py_NotImplemented;
- Py_INCREF(v);
- return v;
- }
-
- /* compare by descriptor address; if the descriptors are the same,
- compare by the objects they're bound to */
- a_descr = ((wrapperobject *)a)->descr;
- b_descr = ((wrapperobject *)b)->descr;
- if (a_descr == b_descr) {
- a = ((wrapperobject *)a)->self;
- b = ((wrapperobject *)b)->self;
- return PyObject_RichCompare(a, b, op);
- }
-
- result = a_descr - b_descr;
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- case Py_LE:
- v = TEST_COND(result <= 0);
- break;
- case Py_GE:
- v = TEST_COND(result >= 0);
- break;
- case Py_LT:
- v = TEST_COND(result < 0);
- break;
- case Py_GT:
- v = TEST_COND(result > 0);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
- Py_INCREF(v);
- return v;
+ int result;
+ PyObject *v;
+ PyWrapperDescrObject *a_descr, *b_descr;
+
+ assert(a != NULL && b != NULL);
+
+ /* both arguments should be wrapperobjects */
+ if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
+ v = Py_NotImplemented;
+ Py_INCREF(v);
+ return v;
+ }
+
+ /* compare by descriptor address; if the descriptors are the same,
+ compare by the objects they're bound to */
+ a_descr = ((wrapperobject *)a)->descr;
+ b_descr = ((wrapperobject *)b)->descr;
+ if (a_descr == b_descr) {
+ a = ((wrapperobject *)a)->self;
+ b = ((wrapperobject *)b)->self;
+ return PyObject_RichCompare(a, b, op);
+ }
+
+ result = a_descr - b_descr;
+ switch (op) {
+ case Py_EQ:
+ v = TEST_COND(result == 0);
+ break;
+ case Py_NE:
+ v = TEST_COND(result != 0);
+ break;
+ case Py_LE:
+ v = TEST_COND(result <= 0);
+ break;
+ case Py_GE:
+ v = TEST_COND(result >= 0);
+ break;
+ case Py_LT:
+ v = TEST_COND(result < 0);
+ break;
+ case Py_GT:
+ v = TEST_COND(result > 0);
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
+ Py_INCREF(v);
+ return v;
}
static long
wrapper_hash(wrapperobject *wp)
{
- int x, y;
- x = _Py_HashPointer(wp->descr);
- if (x == -1)
- return -1;
- y = PyObject_Hash(wp->self);
- if (y == -1)
- return -1;
- x = x ^ y;
- if (x == -1)
- x = -2;
- return x;
+ int x, y;
+ x = _Py_HashPointer(wp->descr);
+ if (x == -1)
+ return -1;
+ y = PyObject_Hash(wp->self);
+ if (y == -1)
+ return -1;
+ x = x ^ y;
+ if (x == -1)
+ x = -2;
+ return x;
}
static PyObject *
wrapper_repr(wrapperobject *wp)
{
- return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
- wp->descr->d_base->name,
- wp->self->ob_type->tp_name,
- wp->self);
+ return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
+ wp->descr->d_base->name,
+ wp->self->ob_type->tp_name,
+ wp->self);
}
static PyMemberDef wrapper_members[] = {
- {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
- {0}
+ {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
+ {0}
};
static PyObject *
wrapper_objclass(wrapperobject *wp)
{
- PyObject *c = (PyObject *)wp->descr->d_type;
+ PyObject *c = (PyObject *)wp->descr->d_type;
- Py_INCREF(c);
- return c;
+ Py_INCREF(c);
+ return c;
}
static PyObject *
wrapper_name(wrapperobject *wp)
{
- const char *s = wp->descr->d_base->name;
+ const char *s = wp->descr->d_base->name;
- return PyUnicode_FromString(s);
+ return PyUnicode_FromString(s);
}
static PyObject *
wrapper_doc(wrapperobject *wp)
{
- const char *s = wp->descr->d_base->doc;
-
- if (s == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- else {
- return PyUnicode_FromString(s);
- }
+ const char *s = wp->descr->d_base->doc;
+
+ if (s == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ else {
+ return PyUnicode_FromString(s);
+ }
}
static PyGetSetDef wrapper_getsets[] = {
- {"__objclass__", (getter)wrapper_objclass},
- {"__name__", (getter)wrapper_name},
- {"__doc__", (getter)wrapper_doc},
- {0}
+ {"__objclass__", (getter)wrapper_objclass},
+ {"__name__", (getter)wrapper_name},
+ {"__doc__", (getter)wrapper_doc},
+ {0}
};
static PyObject *
wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
{
- wrapperfunc wrapper = wp->descr->d_base->wrapper;
- PyObject *self = wp->self;
-
- if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
- wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
- return (*wk)(self, args, wp->descr->d_wrapped, kwds);
- }
-
- if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
- PyErr_Format(PyExc_TypeError,
- "wrapper %s doesn't take keyword arguments",
- wp->descr->d_base->name);
- return NULL;
- }
- return (*wrapper)(self, args, wp->descr->d_wrapped);
+ wrapperfunc wrapper = wp->descr->d_base->wrapper;
+ PyObject *self = wp->self;
+
+ if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
+ wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
+ return (*wk)(self, args, wp->descr->d_wrapped, kwds);
+ }
+
+ if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
+ PyErr_Format(PyExc_TypeError,
+ "wrapper %s doesn't take keyword arguments",
+ wp->descr->d_base->name);
+ return NULL;
+ }
+ return (*wrapper)(self, args, wp->descr->d_wrapped);
}
static int
wrapper_traverse(PyObject *self, visitproc visit, void *arg)
{
- wrapperobject *wp = (wrapperobject *)self;
- Py_VISIT(wp->descr);
- Py_VISIT(wp->self);
- return 0;
+ wrapperobject *wp = (wrapperobject *)self;
+ Py_VISIT(wp->descr);
+ Py_VISIT(wp->self);
+ return 0;
}
static PyTypeObject wrappertype = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "method-wrapper", /* tp_name */
- sizeof(wrapperobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)wrapper_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)wrapper_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)wrapper_hash, /* tp_hash */
- (ternaryfunc)wrapper_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- 0, /* tp_doc */
- wrapper_traverse, /* tp_traverse */
- 0, /* tp_clear */
- wrapper_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- wrapper_members, /* tp_members */
- wrapper_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "method-wrapper", /* tp_name */
+ sizeof(wrapperobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)wrapper_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)wrapper_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)wrapper_hash, /* tp_hash */
+ (ternaryfunc)wrapper_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ 0, /* tp_doc */
+ wrapper_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ wrapper_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ wrapper_members, /* tp_members */
+ wrapper_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
};
PyObject *
PyWrapper_New(PyObject *d, PyObject *self)
{
- wrapperobject *wp;
- PyWrapperDescrObject *descr;
-
- assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
- descr = (PyWrapperDescrObject *)d;
- assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
-
- wp = PyObject_GC_New(wrapperobject, &wrappertype);
- if (wp != NULL) {
- Py_INCREF(descr);
- wp->descr = descr;
- Py_INCREF(self);
- wp->self = self;
- _PyObject_GC_TRACK(wp);
- }
- return (PyObject *)wp;
+ wrapperobject *wp;
+ PyWrapperDescrObject *descr;
+
+ assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
+ descr = (PyWrapperDescrObject *)d;
+ assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
+
+ wp = PyObject_GC_New(wrapperobject, &wrappertype);
+ if (wp != NULL) {
+ Py_INCREF(descr);
+ wp->descr = descr;
+ Py_INCREF(self);
+ wp->self = self;
+ _PyObject_GC_TRACK(wp);
+ }
+ return (PyObject *)wp;
}
@@ -1078,247 +1078,247 @@ PyWrapper_New(PyObject *d, PyObject *self)
/*
class property(object):
- def __init__(self, fget=None, fset=None, fdel=None, doc=None):
- if doc is None and fget is not None and hasattr(fget, "__doc__"):
- doc = fget.__doc__
- self.__get = fget
- self.__set = fset
- self.__del = fdel
- self.__doc__ = doc
-
- def __get__(self, inst, type=None):
- if inst is None:
- return self
- if self.__get is None:
- raise AttributeError, "unreadable attribute"
- return self.__get(inst)
-
- def __set__(self, inst, value):
- if self.__set is None:
- raise AttributeError, "can't set attribute"
- return self.__set(inst, value)
-
- def __delete__(self, inst):
- if self.__del is None:
- raise AttributeError, "can't delete attribute"
- return self.__del(inst)
+ def __init__(self, fget=None, fset=None, fdel=None, doc=None):
+ if doc is None and fget is not None and hasattr(fget, "__doc__"):
+ doc = fget.__doc__
+ self.__get = fget
+ self.__set = fset
+ self.__del = fdel
+ self.__doc__ = doc
+
+ def __get__(self, inst, type=None):
+ if inst is None:
+ return self
+ if self.__get is None:
+ raise AttributeError, "unreadable attribute"
+ return self.__get(inst)
+
+ def __set__(self, inst, value):
+ if self.__set is None:
+ raise AttributeError, "can't set attribute"
+ return self.__set(inst, value)
+
+ def __delete__(self, inst):
+ if self.__del is None:
+ raise AttributeError, "can't delete attribute"
+ return self.__del(inst)
*/
typedef struct {
- PyObject_HEAD
- PyObject *prop_get;
- PyObject *prop_set;
- PyObject *prop_del;
- PyObject *prop_doc;
- int getter_doc;
+ PyObject_HEAD
+ PyObject *prop_get;
+ PyObject *prop_set;
+ PyObject *prop_del;
+ PyObject *prop_doc;
+ int getter_doc;
} propertyobject;
static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
- PyObject *, PyObject *);
+ PyObject *, PyObject *);
static PyMemberDef property_members[] = {
- {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
- {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
- {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
- {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
- {0}
+ {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
+ {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
+ {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
+ {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
+ {0}
};
PyDoc_STRVAR(getter_doc,
- "Descriptor to change the getter on a property.");
+ "Descriptor to change the getter on a property.");
static PyObject *
property_getter(PyObject *self, PyObject *getter)
{
- return property_copy(self, getter, NULL, NULL, NULL);
+ return property_copy(self, getter, NULL, NULL, NULL);
}
PyDoc_STRVAR(setter_doc,
- "Descriptor to change the setter on a property.");
+ "Descriptor to change the setter on a property.");
static PyObject *
property_setter(PyObject *self, PyObject *setter)
{
- return property_copy(self, NULL, setter, NULL, NULL);
+ return property_copy(self, NULL, setter, NULL, NULL);
}
PyDoc_STRVAR(deleter_doc,
- "Descriptor to change the deleter on a property.");
+ "Descriptor to change the deleter on a property.");
static PyObject *
property_deleter(PyObject *self, PyObject *deleter)
{
- return property_copy(self, NULL, NULL, deleter, NULL);
+ return property_copy(self, NULL, NULL, deleter, NULL);
}
static PyMethodDef property_methods[] = {
- {"getter", property_getter, METH_O, getter_doc},
- {"setter", property_setter, METH_O, setter_doc},
- {"deleter", property_deleter, METH_O, deleter_doc},
- {0}
+ {"getter", property_getter, METH_O, getter_doc},
+ {"setter", property_setter, METH_O, setter_doc},
+ {"deleter", property_deleter, METH_O, deleter_doc},
+ {0}
};
static void
property_dealloc(PyObject *self)
{
- propertyobject *gs = (propertyobject *)self;
-
- _PyObject_GC_UNTRACK(self);
- Py_XDECREF(gs->prop_get);
- Py_XDECREF(gs->prop_set);
- Py_XDECREF(gs->prop_del);
- Py_XDECREF(gs->prop_doc);
- self->ob_type->tp_free(self);
+ propertyobject *gs = (propertyobject *)self;
+
+ _PyObject_GC_UNTRACK(self);
+ Py_XDECREF(gs->prop_get);
+ Py_XDECREF(gs->prop_set);
+ Py_XDECREF(gs->prop_del);
+ Py_XDECREF(gs->prop_doc);
+ self->ob_type->tp_free(self);
}
static PyObject *
property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- propertyobject *gs = (propertyobject *)self;
-
- if (obj == NULL || obj == Py_None) {
- Py_INCREF(self);
- return self;
- }
- if (gs->prop_get == NULL) {
- PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
- return NULL;
- }
- return PyObject_CallFunction(gs->prop_get, "(O)", obj);
+ propertyobject *gs = (propertyobject *)self;
+
+ if (obj == NULL || obj == Py_None) {
+ Py_INCREF(self);
+ return self;
+ }
+ if (gs->prop_get == NULL) {
+ PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
+ return NULL;
+ }
+ return PyObject_CallFunction(gs->prop_get, "(O)", obj);
}
static int
property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
{
- propertyobject *gs = (propertyobject *)self;
- PyObject *func, *res;
-
- if (value == NULL)
- func = gs->prop_del;
- else
- func = gs->prop_set;
- if (func == NULL) {
- PyErr_SetString(PyExc_AttributeError,
- value == NULL ?
- "can't delete attribute" :
- "can't set attribute");
- return -1;
- }
- if (value == NULL)
- res = PyObject_CallFunction(func, "(O)", obj);
- else
- res = PyObject_CallFunction(func, "(OO)", obj, value);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
+ propertyobject *gs = (propertyobject *)self;
+ PyObject *func, *res;
+
+ if (value == NULL)
+ func = gs->prop_del;
+ else
+ func = gs->prop_set;
+ if (func == NULL) {
+ PyErr_SetString(PyExc_AttributeError,
+ value == NULL ?
+ "can't delete attribute" :
+ "can't set attribute");
+ return -1;
+ }
+ if (value == NULL)
+ res = PyObject_CallFunction(func, "(O)", obj);
+ else
+ res = PyObject_CallFunction(func, "(OO)", obj, value);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
}
static PyObject *
property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del,
- PyObject *doc)
+ PyObject *doc)
{
- propertyobject *pold = (propertyobject *)old;
- PyObject *new, *type;
-
- type = PyObject_Type(old);
- if (type == NULL)
- return NULL;
-
- if (get == NULL || get == Py_None) {
- Py_XDECREF(get);
- get = pold->prop_get ? pold->prop_get : Py_None;
- }
- if (set == NULL || set == Py_None) {
- Py_XDECREF(set);
- set = pold->prop_set ? pold->prop_set : Py_None;
- }
- if (del == NULL || del == Py_None) {
- Py_XDECREF(del);
- del = pold->prop_del ? pold->prop_del : Py_None;
- }
- if (doc == NULL || doc == Py_None) {
- Py_XDECREF(doc);
- if (pold->getter_doc && get != Py_None) {
- /* make _init use __doc__ from getter */
- doc = Py_None;
- }
- else {
- doc = pold->prop_doc ? pold->prop_doc : Py_None;
- }
- }
-
- new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
- Py_DECREF(type);
- if (new == NULL)
- return NULL;
- return new;
+ propertyobject *pold = (propertyobject *)old;
+ PyObject *new, *type;
+
+ type = PyObject_Type(old);
+ if (type == NULL)
+ return NULL;
+
+ if (get == NULL || get == Py_None) {
+ Py_XDECREF(get);
+ get = pold->prop_get ? pold->prop_get : Py_None;
+ }
+ if (set == NULL || set == Py_None) {
+ Py_XDECREF(set);
+ set = pold->prop_set ? pold->prop_set : Py_None;
+ }
+ if (del == NULL || del == Py_None) {
+ Py_XDECREF(del);
+ del = pold->prop_del ? pold->prop_del : Py_None;
+ }
+ if (doc == NULL || doc == Py_None) {
+ Py_XDECREF(doc);
+ if (pold->getter_doc && get != Py_None) {
+ /* make _init use __doc__ from getter */
+ doc = Py_None;
+ }
+ else {
+ doc = pold->prop_doc ? pold->prop_doc : Py_None;
+ }
+ }
+
+ new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
+ Py_DECREF(type);
+ if (new == NULL)
+ return NULL;
+ return new;
}
static int
property_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
- static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
- propertyobject *prop = (propertyobject *)self;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
- kwlist, &get, &set, &del, &doc))
- return -1;
-
- if (get == Py_None)
- get = NULL;
- if (set == Py_None)
- set = NULL;
- if (del == Py_None)
- del = NULL;
-
- Py_XINCREF(get);
- Py_XINCREF(set);
- Py_XINCREF(del);
- Py_XINCREF(doc);
-
- prop->prop_get = get;
- prop->prop_set = set;
- prop->prop_del = del;
- prop->prop_doc = doc;
- prop->getter_doc = 0;
-
- /* if no docstring given and the getter has one, use that one */
- if ((doc == NULL || doc == Py_None) && get != NULL) {
- PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
- if (get_doc) {
- if (Py_TYPE(self) == &PyProperty_Type) {
- Py_XDECREF(prop->prop_doc);
- prop->prop_doc = get_doc;
- }
- else {
- /* If this is a property subclass, put __doc__
- in dict of the subclass instance instead,
- otherwise it gets shadowed by __doc__ in the
- class's dict. */
- int err = PyObject_SetAttrString(self, "__doc__", get_doc);
- Py_DECREF(get_doc);
- if (err < 0)
- return -1;
- }
- prop->getter_doc = 1;
- }
- else if (PyErr_ExceptionMatches(PyExc_Exception)) {
- PyErr_Clear();
- }
- else {
- return -1;
- }
- }
-
- return 0;
+ PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
+ static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
+ propertyobject *prop = (propertyobject *)self;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
+ kwlist, &get, &set, &del, &doc))
+ return -1;
+
+ if (get == Py_None)
+ get = NULL;
+ if (set == Py_None)
+ set = NULL;
+ if (del == Py_None)
+ del = NULL;
+
+ Py_XINCREF(get);
+ Py_XINCREF(set);
+ Py_XINCREF(del);
+ Py_XINCREF(doc);
+
+ prop->prop_get = get;
+ prop->prop_set = set;
+ prop->prop_del = del;
+ prop->prop_doc = doc;
+ prop->getter_doc = 0;
+
+ /* if no docstring given and the getter has one, use that one */
+ if ((doc == NULL || doc == Py_None) && get != NULL) {
+ PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
+ if (get_doc) {
+ if (Py_TYPE(self) == &PyProperty_Type) {
+ Py_XDECREF(prop->prop_doc);
+ prop->prop_doc = get_doc;
+ }
+ else {
+ /* If this is a property subclass, put __doc__
+ in dict of the subclass instance instead,
+ otherwise it gets shadowed by __doc__ in the
+ class's dict. */
+ int err = PyObject_SetAttrString(self, "__doc__", get_doc);
+ Py_DECREF(get_doc);
+ if (err < 0)
+ return -1;
+ }
+ prop->getter_doc = 1;
+ }
+ else if (PyErr_ExceptionMatches(PyExc_Exception)) {
+ PyErr_Clear();
+ }
+ else {
+ return -1;
+ }
+ }
+
+ return 0;
}
PyDoc_STRVAR(property_doc,
@@ -1346,54 +1346,54 @@ PyDoc_STRVAR(property_doc,
static int
property_traverse(PyObject *self, visitproc visit, void *arg)
{
- propertyobject *pp = (propertyobject *)self;
- Py_VISIT(pp->prop_get);
- Py_VISIT(pp->prop_set);
- Py_VISIT(pp->prop_del);
- Py_VISIT(pp->prop_doc);
- return 0;
+ propertyobject *pp = (propertyobject *)self;
+ Py_VISIT(pp->prop_get);
+ Py_VISIT(pp->prop_set);
+ Py_VISIT(pp->prop_del);
+ Py_VISIT(pp->prop_doc);
+ return 0;
}
PyTypeObject PyProperty_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "property", /* tp_name */
- sizeof(propertyobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- property_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- property_doc, /* tp_doc */
- property_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- property_methods, /* tp_methods */
- property_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- property_descr_get, /* tp_descr_get */
- property_descr_set, /* tp_descr_set */
- 0, /* tp_dictoffset */
- property_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "property", /* tp_name */
+ sizeof(propertyobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ property_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ property_doc, /* tp_doc */
+ property_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ property_methods, /* tp_methods */
+ property_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ property_descr_get, /* tp_descr_get */
+ property_descr_set, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ property_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 1d36e1d..8d0e6a4 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -17,12 +17,12 @@
static void
set_key_error(PyObject *arg)
{
- PyObject *tup;
- tup = PyTuple_Pack(1, arg);
- if (!tup)
- return; /* caller will expect error to be set anyway */
- PyErr_SetObject(PyExc_KeyError, tup);
- Py_DECREF(tup);
+ PyObject *tup;
+ tup = PyTuple_Pack(1, arg);
+ if (!tup)
+ return; /* caller will expect error to be set anyway */
+ PyErr_SetObject(PyExc_KeyError, tup);
+ Py_DECREF(tup);
}
/* Define this out if you don't want conversion statistics on exit. */
@@ -52,7 +52,7 @@ gives better-than-random behavior in common cases, and that's very desirable.
OTOH, when collisions occur, the tendency to fill contiguous slices of the
hash table makes a good collision resolution strategy crucial. Taking only
the last i bits of the hash code is also vulnerable: for example, consider
-the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
+the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
of every hash code are all 0: they *all* map to the same table index.
@@ -142,7 +142,7 @@ static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject()
PyObject *
_PyDict_Dummy(void)
{
- return dummy;
+ return dummy;
}
#endif
@@ -157,9 +157,9 @@ static long converted = 0L;
static void
show_counts(void)
{
- fprintf(stderr, "created %ld string dicts\n", created);
- fprintf(stderr, "converted %ld to normal dicts\n", converted);
- fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
+ fprintf(stderr, "created %ld string dicts\n", created);
+ fprintf(stderr, "converted %ld to normal dicts\n", converted);
+ fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
}
#endif
@@ -172,12 +172,12 @@ static size_t count_reuse = 0;
static void
show_alloc(void)
{
- fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
- count_alloc);
- fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
- "d\n", count_reuse);
- fprintf(stderr, "%.2f%% reuse rate\n\n",
- (100.0*count_reuse/(count_alloc+count_reuse)));
+ fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
+ count_alloc);
+ fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
+ "d\n", count_reuse);
+ fprintf(stderr, "%.2f%% reuse rate\n\n",
+ (100.0*count_reuse/(count_alloc+count_reuse)));
}
#endif
@@ -189,12 +189,12 @@ static Py_ssize_t count_tracked = 0;
static void
show_track(void)
{
- fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
- count_tracked + count_untracked);
- fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
- "d\n", count_tracked);
- fprintf(stderr, "%.2f%% dict tracking rate\n\n",
- (100.0*count_tracked/(count_untracked+count_tracked)));
+ fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
+ count_tracked + count_untracked);
+ fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
+ "d\n", count_tracked);
+ fprintf(stderr, "%.2f%% dict tracking rate\n\n",
+ (100.0*count_tracked/(count_untracked+count_tracked)));
}
#endif
@@ -208,15 +208,15 @@ show_track(void)
an excellent reason not to).
*/
-#define INIT_NONZERO_DICT_SLOTS(mp) do { \
- (mp)->ma_table = (mp)->ma_smalltable; \
- (mp)->ma_mask = PyDict_MINSIZE - 1; \
+#define INIT_NONZERO_DICT_SLOTS(mp) do { \
+ (mp)->ma_table = (mp)->ma_smalltable; \
+ (mp)->ma_mask = PyDict_MINSIZE - 1; \
} while(0)
-#define EMPTY_TO_MINSIZE(mp) do { \
- memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
- (mp)->ma_used = (mp)->ma_fill = 0; \
- INIT_NONZERO_DICT_SLOTS(mp); \
+#define EMPTY_TO_MINSIZE(mp) do { \
+ memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
+ (mp)->ma_used = (mp)->ma_fill = 0; \
+ INIT_NONZERO_DICT_SLOTS(mp); \
} while(0)
/* Dictionary reuse scheme to save calls to malloc, free, and memset */
@@ -229,68 +229,68 @@ static int numfree = 0;
void
PyDict_Fini(void)
{
- PyDictObject *op;
+ PyDictObject *op;
- while (numfree) {
- op = free_list[--numfree];
- assert(PyDict_CheckExact(op));
- PyObject_GC_Del(op);
- }
+ while (numfree) {
+ op = free_list[--numfree];
+ assert(PyDict_CheckExact(op));
+ PyObject_GC_Del(op);
+ }
}
PyObject *
PyDict_New(void)
{
- register PyDictObject *mp;
- if (dummy == NULL) { /* Auto-initialize dummy */
- dummy = PyUnicode_FromString("<dummy key>");
- if (dummy == NULL)
- return NULL;
+ register PyDictObject *mp;
+ if (dummy == NULL) { /* Auto-initialize dummy */
+ dummy = PyUnicode_FromString("<dummy key>");
+ if (dummy == NULL)
+ return NULL;
#ifdef SHOW_CONVERSION_COUNTS
- Py_AtExit(show_counts);
+ Py_AtExit(show_counts);
#endif
#ifdef SHOW_ALLOC_COUNT
- Py_AtExit(show_alloc);
+ Py_AtExit(show_alloc);
#endif
#ifdef SHOW_TRACK_COUNT
- Py_AtExit(show_track);
+ Py_AtExit(show_track);
#endif
- }
- if (numfree) {
- mp = free_list[--numfree];
- assert (mp != NULL);
- assert (Py_TYPE(mp) == &PyDict_Type);
- _Py_NewReference((PyObject *)mp);
- if (mp->ma_fill) {
- EMPTY_TO_MINSIZE(mp);
- } else {
- /* At least set ma_table and ma_mask; these are wrong
- if an empty but presized dict is added to freelist */
- INIT_NONZERO_DICT_SLOTS(mp);
- }
- assert (mp->ma_used == 0);
- assert (mp->ma_table == mp->ma_smalltable);
- assert (mp->ma_mask == PyDict_MINSIZE - 1);
+ }
+ if (numfree) {
+ mp = free_list[--numfree];
+ assert (mp != NULL);
+ assert (Py_TYPE(mp) == &PyDict_Type);
+ _Py_NewReference((PyObject *)mp);
+ if (mp->ma_fill) {
+ EMPTY_TO_MINSIZE(mp);
+ } else {
+ /* At least set ma_table and ma_mask; these are wrong
+ if an empty but presized dict is added to freelist */
+ INIT_NONZERO_DICT_SLOTS(mp);
+ }
+ assert (mp->ma_used == 0);
+ assert (mp->ma_table == mp->ma_smalltable);
+ assert (mp->ma_mask == PyDict_MINSIZE - 1);
#ifdef SHOW_ALLOC_COUNT
- count_reuse++;
+ count_reuse++;
#endif
- } else {
- mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
- if (mp == NULL)
- return NULL;
- EMPTY_TO_MINSIZE(mp);
+ } else {
+ mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
+ if (mp == NULL)
+ return NULL;
+ EMPTY_TO_MINSIZE(mp);
#ifdef SHOW_ALLOC_COUNT
- count_alloc++;
+ count_alloc++;
#endif
- }
- mp->ma_lookup = lookdict_unicode;
+ }
+ mp->ma_lookup = lookdict_unicode;
#ifdef SHOW_TRACK_COUNT
- count_untracked++;
+ count_untracked++;
#endif
#ifdef SHOW_CONVERSION_COUNTS
- ++created;
+ ++created;
#endif
- return (PyObject *)mp;
+ return (PyObject *)mp;
}
/*
@@ -320,80 +320,80 @@ PyDictEntry*.
static PyDictEntry *
lookdict(PyDictObject *mp, PyObject *key, register long hash)
{
- register size_t i;
- register size_t perturb;
- register PyDictEntry *freeslot;
- register size_t mask = (size_t)mp->ma_mask;
- PyDictEntry *ep0 = mp->ma_table;
- register PyDictEntry *ep;
- register int cmp;
- PyObject *startkey;
-
- i = (size_t)hash & mask;
- ep = &ep0[i];
- if (ep->me_key == NULL || ep->me_key == key)
- return ep;
-
- if (ep->me_key == dummy)
- freeslot = ep;
- else {
- if (ep->me_hash == hash) {
- startkey = ep->me_key;
- Py_INCREF(startkey);
- cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
- Py_DECREF(startkey);
- if (cmp < 0)
- return NULL;
- if (ep0 == mp->ma_table && ep->me_key == startkey) {
- if (cmp > 0)
- return ep;
- }
- else {
- /* The compare did major nasty stuff to the
- * dict: start over.
- * XXX A clever adversary could prevent this
- * XXX from terminating.
- */
- return lookdict(mp, key, hash);
- }
- }
- freeslot = NULL;
- }
-
- /* In the loop, me_key == dummy is by far (factor of 100s) the
- least likely outcome, so test for that last. */
- for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- ep = &ep0[i & mask];
- if (ep->me_key == NULL)
- return freeslot == NULL ? ep : freeslot;
- if (ep->me_key == key)
- return ep;
- if (ep->me_hash == hash && ep->me_key != dummy) {
- startkey = ep->me_key;
- Py_INCREF(startkey);
- cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
- Py_DECREF(startkey);
- if (cmp < 0)
- return NULL;
- if (ep0 == mp->ma_table && ep->me_key == startkey) {
- if (cmp > 0)
- return ep;
- }
- else {
- /* The compare did major nasty stuff to the
- * dict: start over.
- * XXX A clever adversary could prevent this
- * XXX from terminating.
- */
- return lookdict(mp, key, hash);
- }
- }
- else if (ep->me_key == dummy && freeslot == NULL)
- freeslot = ep;
- }
- assert(0); /* NOT REACHED */
- return 0;
+ register size_t i;
+ register size_t perturb;
+ register PyDictEntry *freeslot;
+ register size_t mask = (size_t)mp->ma_mask;
+ PyDictEntry *ep0 = mp->ma_table;
+ register PyDictEntry *ep;
+ register int cmp;
+ PyObject *startkey;
+
+ i = (size_t)hash & mask;
+ ep = &ep0[i];
+ if (ep->me_key == NULL || ep->me_key == key)
+ return ep;
+
+ if (ep->me_key == dummy)
+ freeslot = ep;
+ else {
+ if (ep->me_hash == hash) {
+ startkey = ep->me_key;
+ Py_INCREF(startkey);
+ cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
+ Py_DECREF(startkey);
+ if (cmp < 0)
+ return NULL;
+ if (ep0 == mp->ma_table && ep->me_key == startkey) {
+ if (cmp > 0)
+ return ep;
+ }
+ else {
+ /* The compare did major nasty stuff to the
+ * dict: start over.
+ * XXX A clever adversary could prevent this
+ * XXX from terminating.
+ */
+ return lookdict(mp, key, hash);
+ }
+ }
+ freeslot = NULL;
+ }
+
+ /* In the loop, me_key == dummy is by far (factor of 100s) the
+ least likely outcome, so test for that last. */
+ for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ ep = &ep0[i & mask];
+ if (ep->me_key == NULL)
+ return freeslot == NULL ? ep : freeslot;
+ if (ep->me_key == key)
+ return ep;
+ if (ep->me_hash == hash && ep->me_key != dummy) {
+ startkey = ep->me_key;
+ Py_INCREF(startkey);
+ cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
+ Py_DECREF(startkey);
+ if (cmp < 0)
+ return NULL;
+ if (ep0 == mp->ma_table && ep->me_key == startkey) {
+ if (cmp > 0)
+ return ep;
+ }
+ else {
+ /* The compare did major nasty stuff to the
+ * dict: start over.
+ * XXX A clever adversary could prevent this
+ * XXX from terminating.
+ */
+ return lookdict(mp, key, hash);
+ }
+ }
+ else if (ep->me_key == dummy && freeslot == NULL)
+ freeslot = ep;
+ }
+ assert(0); /* NOT REACHED */
+ return 0;
}
/*
@@ -409,99 +409,99 @@ lookdict(PyDictObject *mp, PyObject *key, register long hash)
static PyDictEntry *
lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
{
- register size_t i;
- register size_t perturb;
- register PyDictEntry *freeslot;
- register size_t mask = (size_t)mp->ma_mask;
- PyDictEntry *ep0 = mp->ma_table;
- register PyDictEntry *ep;
-
- /* Make sure this function doesn't have to handle non-unicode keys,
- including subclasses of str; e.g., one reason to subclass
- unicodes is to override __eq__, and for speed we don't cater to
- that here. */
- if (!PyUnicode_CheckExact(key)) {
+ register size_t i;
+ register size_t perturb;
+ register PyDictEntry *freeslot;
+ register size_t mask = (size_t)mp->ma_mask;
+ PyDictEntry *ep0 = mp->ma_table;
+ register PyDictEntry *ep;
+
+ /* Make sure this function doesn't have to handle non-unicode keys,
+ including subclasses of str; e.g., one reason to subclass
+ unicodes is to override __eq__, and for speed we don't cater to
+ that here. */
+ if (!PyUnicode_CheckExact(key)) {
#ifdef SHOW_CONVERSION_COUNTS
- ++converted;
+ ++converted;
#endif
- mp->ma_lookup = lookdict;
- return lookdict(mp, key, hash);
- }
- i = hash & mask;
- ep = &ep0[i];
- if (ep->me_key == NULL || ep->me_key == key)
- return ep;
- if (ep->me_key == dummy)
- freeslot = ep;
- else {
- if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
- return ep;
- freeslot = NULL;
- }
-
- /* In the loop, me_key == dummy is by far (factor of 100s) the
- least likely outcome, so test for that last. */
- for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- ep = &ep0[i & mask];
- if (ep->me_key == NULL)
- return freeslot == NULL ? ep : freeslot;
- if (ep->me_key == key
- || (ep->me_hash == hash
- && ep->me_key != dummy
- && unicode_eq(ep->me_key, key)))
- return ep;
- if (ep->me_key == dummy && freeslot == NULL)
- freeslot = ep;
- }
- assert(0); /* NOT REACHED */
- return 0;
+ mp->ma_lookup = lookdict;
+ return lookdict(mp, key, hash);
+ }
+ i = hash & mask;
+ ep = &ep0[i];
+ if (ep->me_key == NULL || ep->me_key == key)
+ return ep;
+ if (ep->me_key == dummy)
+ freeslot = ep;
+ else {
+ if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
+ return ep;
+ freeslot = NULL;
+ }
+
+ /* In the loop, me_key == dummy is by far (factor of 100s) the
+ least likely outcome, so test for that last. */
+ for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ ep = &ep0[i & mask];
+ if (ep->me_key == NULL)
+ return freeslot == NULL ? ep : freeslot;
+ if (ep->me_key == key
+ || (ep->me_hash == hash
+ && ep->me_key != dummy
+ && unicode_eq(ep->me_key, key)))
+ return ep;
+ if (ep->me_key == dummy && freeslot == NULL)
+ freeslot = ep;
+ }
+ assert(0); /* NOT REACHED */
+ return 0;
}
#ifdef SHOW_TRACK_COUNT
#define INCREASE_TRACK_COUNT \
- (count_tracked++, count_untracked--);
+ (count_tracked++, count_untracked--);
#define DECREASE_TRACK_COUNT \
- (count_tracked--, count_untracked++);
+ (count_tracked--, count_untracked++);
#else
#define INCREASE_TRACK_COUNT
#define DECREASE_TRACK_COUNT
#endif
#define MAINTAIN_TRACKING(mp, key, value) \
- do { \
- if (!_PyObject_GC_IS_TRACKED(mp)) { \
- if (_PyObject_GC_MAY_BE_TRACKED(key) || \
- _PyObject_GC_MAY_BE_TRACKED(value)) { \
- _PyObject_GC_TRACK(mp); \
- INCREASE_TRACK_COUNT \
- } \
- } \
- } while(0)
+ do { \
+ if (!_PyObject_GC_IS_TRACKED(mp)) { \
+ if (_PyObject_GC_MAY_BE_TRACKED(key) || \
+ _PyObject_GC_MAY_BE_TRACKED(value)) { \
+ _PyObject_GC_TRACK(mp); \
+ INCREASE_TRACK_COUNT \
+ } \
+ } \
+ } while(0)
void
_PyDict_MaybeUntrack(PyObject *op)
{
- PyDictObject *mp;
- PyObject *value;
- Py_ssize_t mask, i;
- PyDictEntry *ep;
-
- if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
- return;
-
- mp = (PyDictObject *) op;
- ep = mp->ma_table;
- mask = mp->ma_mask;
- for (i = 0; i <= mask; i++) {
- if ((value = ep[i].me_value) == NULL)
- continue;
- if (_PyObject_GC_MAY_BE_TRACKED(value) ||
- _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
- return;
- }
- DECREASE_TRACK_COUNT
- _PyObject_GC_UNTRACK(op);
+ PyDictObject *mp;
+ PyObject *value;
+ Py_ssize_t mask, i;
+ PyDictEntry *ep;
+
+ if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
+ return;
+
+ mp = (PyDictObject *) op;
+ ep = mp->ma_table;
+ mask = mp->ma_mask;
+ for (i = 0; i <= mask; i++) {
+ if ((value = ep[i].me_value) == NULL)
+ continue;
+ if (_PyObject_GC_MAY_BE_TRACKED(value) ||
+ _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
+ return;
+ }
+ DECREASE_TRACK_COUNT
+ _PyObject_GC_UNTRACK(op);
}
@@ -514,37 +514,37 @@ Returns -1 if an error occurred, or 0 on success.
static int
insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
{
- PyObject *old_value;
- register PyDictEntry *ep;
- typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
-
- assert(mp->ma_lookup != NULL);
- ep = mp->ma_lookup(mp, key, hash);
- if (ep == NULL) {
- Py_DECREF(key);
- Py_DECREF(value);
- return -1;
- }
- MAINTAIN_TRACKING(mp, key, value);
- if (ep->me_value != NULL) {
- old_value = ep->me_value;
- ep->me_value = value;
- Py_DECREF(old_value); /* which **CAN** re-enter */
- Py_DECREF(key);
- }
- else {
- if (ep->me_key == NULL)
- mp->ma_fill++;
- else {
- assert(ep->me_key == dummy);
- Py_DECREF(dummy);
- }
- ep->me_key = key;
- ep->me_hash = (Py_ssize_t)hash;
- ep->me_value = value;
- mp->ma_used++;
- }
- return 0;
+ PyObject *old_value;
+ register PyDictEntry *ep;
+ typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
+
+ assert(mp->ma_lookup != NULL);
+ ep = mp->ma_lookup(mp, key, hash);
+ if (ep == NULL) {
+ Py_DECREF(key);
+ Py_DECREF(value);
+ return -1;
+ }
+ MAINTAIN_TRACKING(mp, key, value);
+ if (ep->me_value != NULL) {
+ old_value = ep->me_value;
+ ep->me_value = value;
+ Py_DECREF(old_value); /* which **CAN** re-enter */
+ Py_DECREF(key);
+ }
+ else {
+ if (ep->me_key == NULL)
+ mp->ma_fill++;
+ else {
+ assert(ep->me_key == dummy);
+ Py_DECREF(dummy);
+ }
+ ep->me_key = key;
+ ep->me_hash = (Py_ssize_t)hash;
+ ep->me_value = value;
+ mp->ma_used++;
+ }
+ return 0;
}
/*
@@ -557,27 +557,27 @@ is responsible for incref'ing `key` and `value`.
*/
static void
insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
- PyObject *value)
-{
- register size_t i;
- register size_t perturb;
- register size_t mask = (size_t)mp->ma_mask;
- PyDictEntry *ep0 = mp->ma_table;
- register PyDictEntry *ep;
-
- MAINTAIN_TRACKING(mp, key, value);
- i = hash & mask;
- ep = &ep0[i];
- for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- ep = &ep0[i & mask];
- }
- assert(ep->me_value == NULL);
- mp->ma_fill++;
- ep->me_key = key;
- ep->me_hash = (Py_ssize_t)hash;
- ep->me_value = value;
- mp->ma_used++;
+ PyObject *value)
+{
+ register size_t i;
+ register size_t perturb;
+ register size_t mask = (size_t)mp->ma_mask;
+ PyDictEntry *ep0 = mp->ma_table;
+ register PyDictEntry *ep;
+
+ MAINTAIN_TRACKING(mp, key, value);
+ i = hash & mask;
+ ep = &ep0[i];
+ for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ ep = &ep0[i & mask];
+ }
+ assert(ep->me_value == NULL);
+ mp->ma_fill++;
+ ep->me_key = key;
+ ep->me_hash = (Py_ssize_t)hash;
+ ep->me_value = value;
+ mp->ma_used++;
}
/*
@@ -588,84 +588,84 @@ actually be smaller than the old one.
static int
dictresize(PyDictObject *mp, Py_ssize_t minused)
{
- Py_ssize_t newsize;
- PyDictEntry *oldtable, *newtable, *ep;
- Py_ssize_t i;
- int is_oldtable_malloced;
- PyDictEntry small_copy[PyDict_MINSIZE];
-
- assert(minused >= 0);
-
- /* Find the smallest table size > minused. */
- for (newsize = PyDict_MINSIZE;
- newsize <= minused && newsize > 0;
- newsize <<= 1)
- ;
- if (newsize <= 0) {
- PyErr_NoMemory();
- return -1;
- }
-
- /* Get space for a new table. */
- oldtable = mp->ma_table;
- assert(oldtable != NULL);
- is_oldtable_malloced = oldtable != mp->ma_smalltable;
-
- if (newsize == PyDict_MINSIZE) {
- /* A large table is shrinking, or we can't get any smaller. */
- newtable = mp->ma_smalltable;
- if (newtable == oldtable) {
- if (mp->ma_fill == mp->ma_used) {
- /* No dummies, so no point doing anything. */
- return 0;
- }
- /* We're not going to resize it, but rebuild the
- table anyway to purge old dummy entries.
- Subtle: This is *necessary* if fill==size,
- as lookdict needs at least one virgin slot to
- terminate failing searches. If fill < size, it's
- merely desirable, as dummies slow searches. */
- assert(mp->ma_fill > mp->ma_used);
- memcpy(small_copy, oldtable, sizeof(small_copy));
- oldtable = small_copy;
- }
- }
- else {
- newtable = PyMem_NEW(PyDictEntry, newsize);
- if (newtable == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- }
-
- /* Make the dict empty, using the new table. */
- assert(newtable != oldtable);
- mp->ma_table = newtable;
- mp->ma_mask = newsize - 1;
- memset(newtable, 0, sizeof(PyDictEntry) * newsize);
- mp->ma_used = 0;
- i = mp->ma_fill;
- mp->ma_fill = 0;
-
- /* Copy the data over; this is refcount-neutral for active entries;
- dummy entries aren't copied over, of course */
- for (ep = oldtable; i > 0; ep++) {
- if (ep->me_value != NULL) { /* active entry */
- --i;
- insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
- ep->me_value);
- }
- else if (ep->me_key != NULL) { /* dummy entry */
- --i;
- assert(ep->me_key == dummy);
- Py_DECREF(ep->me_key);
- }
- /* else key == value == NULL: nothing to do */
- }
-
- if (is_oldtable_malloced)
- PyMem_DEL(oldtable);
- return 0;
+ Py_ssize_t newsize;
+ PyDictEntry *oldtable, *newtable, *ep;
+ Py_ssize_t i;
+ int is_oldtable_malloced;
+ PyDictEntry small_copy[PyDict_MINSIZE];
+
+ assert(minused >= 0);
+
+ /* Find the smallest table size > minused. */
+ for (newsize = PyDict_MINSIZE;
+ newsize <= minused && newsize > 0;
+ newsize <<= 1)
+ ;
+ if (newsize <= 0) {
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ /* Get space for a new table. */
+ oldtable = mp->ma_table;
+ assert(oldtable != NULL);
+ is_oldtable_malloced = oldtable != mp->ma_smalltable;
+
+ if (newsize == PyDict_MINSIZE) {
+ /* A large table is shrinking, or we can't get any smaller. */
+ newtable = mp->ma_smalltable;
+ if (newtable == oldtable) {
+ if (mp->ma_fill == mp->ma_used) {
+ /* No dummies, so no point doing anything. */
+ return 0;
+ }
+ /* We're not going to resize it, but rebuild the
+ table anyway to purge old dummy entries.
+ Subtle: This is *necessary* if fill==size,
+ as lookdict needs at least one virgin slot to
+ terminate failing searches. If fill < size, it's
+ merely desirable, as dummies slow searches. */
+ assert(mp->ma_fill > mp->ma_used);
+ memcpy(small_copy, oldtable, sizeof(small_copy));
+ oldtable = small_copy;
+ }
+ }
+ else {
+ newtable = PyMem_NEW(PyDictEntry, newsize);
+ if (newtable == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ }
+
+ /* Make the dict empty, using the new table. */
+ assert(newtable != oldtable);
+ mp->ma_table = newtable;
+ mp->ma_mask = newsize - 1;
+ memset(newtable, 0, sizeof(PyDictEntry) * newsize);
+ mp->ma_used = 0;
+ i = mp->ma_fill;
+ mp->ma_fill = 0;
+
+ /* Copy the data over; this is refcount-neutral for active entries;
+ dummy entries aren't copied over, of course */
+ for (ep = oldtable; i > 0; ep++) {
+ if (ep->me_value != NULL) { /* active entry */
+ --i;
+ insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
+ ep->me_value);
+ }
+ else if (ep->me_key != NULL) { /* dummy entry */
+ --i;
+ assert(ep->me_key == dummy);
+ Py_DECREF(ep->me_key);
+ }
+ /* else key == value == NULL: nothing to do */
+ }
+
+ if (is_oldtable_malloced)
+ PyMem_DEL(oldtable);
+ return 0;
}
/* Create a new dictionary pre-sized to hold an estimated number of elements.
@@ -676,13 +676,13 @@ dictresize(PyDictObject *mp, Py_ssize_t minused)
PyObject *
_PyDict_NewPresized(Py_ssize_t minused)
{
- PyObject *op = PyDict_New();
+ PyObject *op = PyDict_New();
- if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
- Py_DECREF(op);
- return NULL;
- }
- return op;
+ if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
+ Py_DECREF(op);
+ return NULL;
+ }
+ return op;
}
/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
@@ -698,46 +698,46 @@ _PyDict_NewPresized(Py_ssize_t minused)
PyObject *
PyDict_GetItem(PyObject *op, PyObject *key)
{
- long hash;
- PyDictObject *mp = (PyDictObject *)op;
- PyDictEntry *ep;
- PyThreadState *tstate;
- if (!PyDict_Check(op))
- return NULL;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
- {
- hash = PyObject_Hash(key);
- if (hash == -1) {
- PyErr_Clear();
- return NULL;
- }
- }
-
- /* We can arrive here with a NULL tstate during initialization: try
- running "python -Wi" for an example related to string interning.
- Let's just hope that no exception occurs then... This must be
- _PyThreadState_Current and not PyThreadState_GET() because in debug
- mode, the latter complains if tstate is NULL. */
- tstate = _PyThreadState_Current;
- if (tstate != NULL && tstate->curexc_type != NULL) {
- /* preserve the existing exception */
- PyObject *err_type, *err_value, *err_tb;
- PyErr_Fetch(&err_type, &err_value, &err_tb);
- ep = (mp->ma_lookup)(mp, key, hash);
- /* ignore errors */
- PyErr_Restore(err_type, err_value, err_tb);
- if (ep == NULL)
- return NULL;
- }
- else {
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL) {
- PyErr_Clear();
- return NULL;
- }
- }
- return ep->me_value;
+ long hash;
+ PyDictObject *mp = (PyDictObject *)op;
+ PyDictEntry *ep;
+ PyThreadState *tstate;
+ if (!PyDict_Check(op))
+ return NULL;
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ {
+ hash = PyObject_Hash(key);
+ if (hash == -1) {
+ PyErr_Clear();
+ return NULL;
+ }
+ }
+
+ /* We can arrive here with a NULL tstate during initialization: try
+ running "python -Wi" for an example related to string interning.
+ Let's just hope that no exception occurs then... This must be
+ _PyThreadState_Current and not PyThreadState_GET() because in debug
+ mode, the latter complains if tstate is NULL. */
+ tstate = _PyThreadState_Current;
+ if (tstate != NULL && tstate->curexc_type != NULL) {
+ /* preserve the existing exception */
+ PyObject *err_type, *err_value, *err_tb;
+ PyErr_Fetch(&err_type, &err_value, &err_tb);
+ ep = (mp->ma_lookup)(mp, key, hash);
+ /* ignore errors */
+ PyErr_Restore(err_type, err_value, err_tb);
+ if (ep == NULL)
+ return NULL;
+ }
+ else {
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL) {
+ PyErr_Clear();
+ return NULL;
+ }
+ }
+ return ep->me_value;
}
/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
@@ -747,27 +747,27 @@ PyDict_GetItem(PyObject *op, PyObject *key)
PyObject *
PyDict_GetItemWithError(PyObject *op, PyObject *key)
{
- long hash;
- PyDictObject*mp = (PyDictObject *)op;
- PyDictEntry *ep;
-
- if (!PyDict_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
- {
- hash = PyObject_Hash(key);
- if (hash == -1) {
- return NULL;
- }
- }
-
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- return ep->me_value;
+ long hash;
+ PyDictObject*mp = (PyDictObject *)op;
+ PyDictEntry *ep;
+
+ if (!PyDict_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ {
+ hash = PyObject_Hash(key);
+ if (hash == -1) {
+ return NULL;
+ }
+ }
+
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ return ep->me_value;
}
/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
@@ -779,154 +779,154 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key)
int
PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
{
- register PyDictObject *mp;
- register long hash;
- register Py_ssize_t n_used;
-
- if (!PyDict_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- assert(key);
- assert(value);
- mp = (PyDictObject *)op;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1)
- {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
- n_used = mp->ma_used;
- Py_INCREF(value);
- Py_INCREF(key);
- if (insertdict(mp, key, hash, value) != 0)
- return -1;
- /* If we added a key, we can safely resize. Otherwise just return!
- * If fill >= 2/3 size, adjust size. Normally, this doubles or
- * quaduples the size, but it's also possible for the dict to shrink
- * (if ma_fill is much larger than ma_used, meaning a lot of dict
- * keys have been * deleted).
- *
- * Quadrupling the size improves average dictionary sparseness
- * (reducing collisions) at the cost of some memory and iteration
- * speed (which loops over every possible entry). It also halves
- * the number of expensive resize operations in a growing dictionary.
- *
- * Very large dictionaries (over 50K items) use doubling instead.
- * This may help applications with severe memory constraints.
- */
- if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
- return 0;
- return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
+ register PyDictObject *mp;
+ register long hash;
+ register Py_ssize_t n_used;
+
+ if (!PyDict_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ assert(key);
+ assert(value);
+ mp = (PyDictObject *)op;
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1)
+ {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
+ n_used = mp->ma_used;
+ Py_INCREF(value);
+ Py_INCREF(key);
+ if (insertdict(mp, key, hash, value) != 0)
+ return -1;
+ /* If we added a key, we can safely resize. Otherwise just return!
+ * If fill >= 2/3 size, adjust size. Normally, this doubles or
+ * quaduples the size, but it's also possible for the dict to shrink
+ * (if ma_fill is much larger than ma_used, meaning a lot of dict
+ * keys have been * deleted).
+ *
+ * Quadrupling the size improves average dictionary sparseness
+ * (reducing collisions) at the cost of some memory and iteration
+ * speed (which loops over every possible entry). It also halves
+ * the number of expensive resize operations in a growing dictionary.
+ *
+ * Very large dictionaries (over 50K items) use doubling instead.
+ * This may help applications with severe memory constraints.
+ */
+ if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
+ return 0;
+ return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
}
int
PyDict_DelItem(PyObject *op, PyObject *key)
{
- register PyDictObject *mp;
- register long hash;
- register PyDictEntry *ep;
- PyObject *old_value, *old_key;
-
- if (!PyDict_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- assert(key);
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- mp = (PyDictObject *)op;
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return -1;
- if (ep->me_value == NULL) {
- set_key_error(key);
- return -1;
- }
- old_key = ep->me_key;
- Py_INCREF(dummy);
- ep->me_key = dummy;
- old_value = ep->me_value;
- ep->me_value = NULL;
- mp->ma_used--;
- Py_DECREF(old_value);
- Py_DECREF(old_key);
- return 0;
+ register PyDictObject *mp;
+ register long hash;
+ register PyDictEntry *ep;
+ PyObject *old_value, *old_key;
+
+ if (!PyDict_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ assert(key);
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ mp = (PyDictObject *)op;
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return -1;
+ if (ep->me_value == NULL) {
+ set_key_error(key);
+ return -1;
+ }
+ old_key = ep->me_key;
+ Py_INCREF(dummy);
+ ep->me_key = dummy;
+ old_value = ep->me_value;
+ ep->me_value = NULL;
+ mp->ma_used--;
+ Py_DECREF(old_value);
+ Py_DECREF(old_key);
+ return 0;
}
void
PyDict_Clear(PyObject *op)
{
- PyDictObject *mp;
- PyDictEntry *ep, *table;
- int table_is_malloced;
- Py_ssize_t fill;
- PyDictEntry small_copy[PyDict_MINSIZE];
+ PyDictObject *mp;
+ PyDictEntry *ep, *table;
+ int table_is_malloced;
+ Py_ssize_t fill;
+ PyDictEntry small_copy[PyDict_MINSIZE];
#ifdef Py_DEBUG
- Py_ssize_t i, n;
+ Py_ssize_t i, n;
#endif
- if (!PyDict_Check(op))
- return;
- mp = (PyDictObject *)op;
+ if (!PyDict_Check(op))
+ return;
+ mp = (PyDictObject *)op;
#ifdef Py_DEBUG
- n = mp->ma_mask + 1;
- i = 0;
+ n = mp->ma_mask + 1;
+ i = 0;
#endif
- table = mp->ma_table;
- assert(table != NULL);
- table_is_malloced = table != mp->ma_smalltable;
-
- /* This is delicate. During the process of clearing the dict,
- * decrefs can cause the dict to mutate. To avoid fatal confusion
- * (voice of experience), we have to make the dict empty before
- * clearing the slots, and never refer to anything via mp->xxx while
- * clearing.
- */
- fill = mp->ma_fill;
- if (table_is_malloced)
- EMPTY_TO_MINSIZE(mp);
-
- else if (fill > 0) {
- /* It's a small table with something that needs to be cleared.
- * Afraid the only safe way is to copy the dict entries into
- * another small table first.
- */
- memcpy(small_copy, table, sizeof(small_copy));
- table = small_copy;
- EMPTY_TO_MINSIZE(mp);
- }
- /* else it's a small table that's already empty */
-
- /* Now we can finally clear things. If C had refcounts, we could
- * assert that the refcount on table is 1 now, i.e. that this function
- * has unique access to it, so decref side-effects can't alter it.
- */
- for (ep = table; fill > 0; ++ep) {
+ table = mp->ma_table;
+ assert(table != NULL);
+ table_is_malloced = table != mp->ma_smalltable;
+
+ /* This is delicate. During the process of clearing the dict,
+ * decrefs can cause the dict to mutate. To avoid fatal confusion
+ * (voice of experience), we have to make the dict empty before
+ * clearing the slots, and never refer to anything via mp->xxx while
+ * clearing.
+ */
+ fill = mp->ma_fill;
+ if (table_is_malloced)
+ EMPTY_TO_MINSIZE(mp);
+
+ else if (fill > 0) {
+ /* It's a small table with something that needs to be cleared.
+ * Afraid the only safe way is to copy the dict entries into
+ * another small table first.
+ */
+ memcpy(small_copy, table, sizeof(small_copy));
+ table = small_copy;
+ EMPTY_TO_MINSIZE(mp);
+ }
+ /* else it's a small table that's already empty */
+
+ /* Now we can finally clear things. If C had refcounts, we could
+ * assert that the refcount on table is 1 now, i.e. that this function
+ * has unique access to it, so decref side-effects can't alter it.
+ */
+ for (ep = table; fill > 0; ++ep) {
#ifdef Py_DEBUG
- assert(i < n);
- ++i;
+ assert(i < n);
+ ++i;
#endif
- if (ep->me_key) {
- --fill;
- Py_DECREF(ep->me_key);
- Py_XDECREF(ep->me_value);
- }
+ if (ep->me_key) {
+ --fill;
+ Py_DECREF(ep->me_key);
+ Py_XDECREF(ep->me_value);
+ }
#ifdef Py_DEBUG
- else
- assert(ep->me_value == NULL);
+ else
+ assert(ep->me_value == NULL);
#endif
- }
+ }
- if (table_is_malloced)
- PyMem_DEL(table);
+ if (table_is_malloced)
+ PyMem_DEL(table);
}
/*
@@ -947,55 +947,55 @@ PyDict_Clear(PyObject *op)
int
PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
{
- register Py_ssize_t i;
- register Py_ssize_t mask;
- register PyDictEntry *ep;
-
- if (!PyDict_Check(op))
- return 0;
- i = *ppos;
- if (i < 0)
- return 0;
- ep = ((PyDictObject *)op)->ma_table;
- mask = ((PyDictObject *)op)->ma_mask;
- while (i <= mask && ep[i].me_value == NULL)
- i++;
- *ppos = i+1;
- if (i > mask)
- return 0;
- if (pkey)
- *pkey = ep[i].me_key;
- if (pvalue)
- *pvalue = ep[i].me_value;
- return 1;
+ register Py_ssize_t i;
+ register Py_ssize_t mask;
+ register PyDictEntry *ep;
+
+ if (!PyDict_Check(op))
+ return 0;
+ i = *ppos;
+ if (i < 0)
+ return 0;
+ ep = ((PyDictObject *)op)->ma_table;
+ mask = ((PyDictObject *)op)->ma_mask;
+ while (i <= mask && ep[i].me_value == NULL)
+ i++;
+ *ppos = i+1;
+ if (i > mask)
+ return 0;
+ if (pkey)
+ *pkey = ep[i].me_key;
+ if (pvalue)
+ *pvalue = ep[i].me_value;
+ return 1;
}
/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
int
_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
{
- register Py_ssize_t i;
- register Py_ssize_t mask;
- register PyDictEntry *ep;
-
- if (!PyDict_Check(op))
- return 0;
- i = *ppos;
- if (i < 0)
- return 0;
- ep = ((PyDictObject *)op)->ma_table;
- mask = ((PyDictObject *)op)->ma_mask;
- while (i <= mask && ep[i].me_value == NULL)
- i++;
- *ppos = i+1;
- if (i > mask)
- return 0;
- *phash = (long)(ep[i].me_hash);
- if (pkey)
- *pkey = ep[i].me_key;
- if (pvalue)
- *pvalue = ep[i].me_value;
- return 1;
+ register Py_ssize_t i;
+ register Py_ssize_t mask;
+ register PyDictEntry *ep;
+
+ if (!PyDict_Check(op))
+ return 0;
+ i = *ppos;
+ if (i < 0)
+ return 0;
+ ep = ((PyDictObject *)op)->ma_table;
+ mask = ((PyDictObject *)op)->ma_mask;
+ while (i <= mask && ep[i].me_value == NULL)
+ i++;
+ *ppos = i+1;
+ if (i > mask)
+ return 0;
+ *phash = (long)(ep[i].me_hash);
+ if (pkey)
+ *pkey = ep[i].me_key;
+ if (pvalue)
+ *pvalue = ep[i].me_value;
+ return 1;
}
/* Methods */
@@ -1003,400 +1003,400 @@ _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue,
static void
dict_dealloc(register PyDictObject *mp)
{
- register PyDictEntry *ep;
- Py_ssize_t fill = mp->ma_fill;
- PyObject_GC_UnTrack(mp);
- Py_TRASHCAN_SAFE_BEGIN(mp)
- for (ep = mp->ma_table; fill > 0; ep++) {
- if (ep->me_key) {
- --fill;
- Py_DECREF(ep->me_key);
- Py_XDECREF(ep->me_value);
- }
- }
- if (mp->ma_table != mp->ma_smalltable)
- PyMem_DEL(mp->ma_table);
- if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
- free_list[numfree++] = mp;
- else
- Py_TYPE(mp)->tp_free((PyObject *)mp);
- Py_TRASHCAN_SAFE_END(mp)
+ register PyDictEntry *ep;
+ Py_ssize_t fill = mp->ma_fill;
+ PyObject_GC_UnTrack(mp);
+ Py_TRASHCAN_SAFE_BEGIN(mp)
+ for (ep = mp->ma_table; fill > 0; ep++) {
+ if (ep->me_key) {
+ --fill;
+ Py_DECREF(ep->me_key);
+ Py_XDECREF(ep->me_value);
+ }
+ }
+ if (mp->ma_table != mp->ma_smalltable)
+ PyMem_DEL(mp->ma_table);
+ if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
+ free_list[numfree++] = mp;
+ else
+ Py_TYPE(mp)->tp_free((PyObject *)mp);
+ Py_TRASHCAN_SAFE_END(mp)
}
static PyObject *
dict_repr(PyDictObject *mp)
{
- Py_ssize_t i;
- PyObject *s, *temp, *colon = NULL;
- PyObject *pieces = NULL, *result = NULL;
- PyObject *key, *value;
-
- i = Py_ReprEnter((PyObject *)mp);
- if (i != 0) {
- return i > 0 ? PyUnicode_FromString("{...}") : NULL;
- }
-
- if (mp->ma_used == 0) {
- result = PyUnicode_FromString("{}");
- goto Done;
- }
-
- pieces = PyList_New(0);
- if (pieces == NULL)
- goto Done;
-
- colon = PyUnicode_FromString(": ");
- if (colon == NULL)
- goto Done;
-
- /* Do repr() on each key+value pair, and insert ": " between them.
- Note that repr may mutate the dict. */
- i = 0;
- while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
- int status;
- /* Prevent repr from deleting value during key format. */
- Py_INCREF(value);
- s = PyObject_Repr(key);
- PyUnicode_Append(&s, colon);
- PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
- Py_DECREF(value);
- if (s == NULL)
- goto Done;
- status = PyList_Append(pieces, s);
- Py_DECREF(s); /* append created a new ref */
- if (status < 0)
- goto Done;
- }
-
- /* Add "{}" decorations to the first and last items. */
- assert(PyList_GET_SIZE(pieces) > 0);
- s = PyUnicode_FromString("{");
- if (s == NULL)
- goto Done;
- temp = PyList_GET_ITEM(pieces, 0);
- PyUnicode_AppendAndDel(&s, temp);
- PyList_SET_ITEM(pieces, 0, s);
- if (s == NULL)
- goto Done;
-
- s = PyUnicode_FromString("}");
- if (s == NULL)
- goto Done;
- temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
- PyUnicode_AppendAndDel(&temp, s);
- PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
- if (temp == NULL)
- goto Done;
-
- /* Paste them all together with ", " between. */
- s = PyUnicode_FromString(", ");
- if (s == NULL)
- goto Done;
- result = PyUnicode_Join(s, pieces);
- Py_DECREF(s);
+ Py_ssize_t i;
+ PyObject *s, *temp, *colon = NULL;
+ PyObject *pieces = NULL, *result = NULL;
+ PyObject *key, *value;
+
+ i = Py_ReprEnter((PyObject *)mp);
+ if (i != 0) {
+ return i > 0 ? PyUnicode_FromString("{...}") : NULL;
+ }
+
+ if (mp->ma_used == 0) {
+ result = PyUnicode_FromString("{}");
+ goto Done;
+ }
+
+ pieces = PyList_New(0);
+ if (pieces == NULL)
+ goto Done;
+
+ colon = PyUnicode_FromString(": ");
+ if (colon == NULL)
+ goto Done;
+
+ /* Do repr() on each key+value pair, and insert ": " between them.
+ Note that repr may mutate the dict. */
+ i = 0;
+ while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
+ int status;
+ /* Prevent repr from deleting value during key format. */
+ Py_INCREF(value);
+ s = PyObject_Repr(key);
+ PyUnicode_Append(&s, colon);
+ PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
+ Py_DECREF(value);
+ if (s == NULL)
+ goto Done;
+ status = PyList_Append(pieces, s);
+ Py_DECREF(s); /* append created a new ref */
+ if (status < 0)
+ goto Done;
+ }
+
+ /* Add "{}" decorations to the first and last items. */
+ assert(PyList_GET_SIZE(pieces) > 0);
+ s = PyUnicode_FromString("{");
+ if (s == NULL)
+ goto Done;
+ temp = PyList_GET_ITEM(pieces, 0);
+ PyUnicode_AppendAndDel(&s, temp);
+ PyList_SET_ITEM(pieces, 0, s);
+ if (s == NULL)
+ goto Done;
+
+ s = PyUnicode_FromString("}");
+ if (s == NULL)
+ goto Done;
+ temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
+ PyUnicode_AppendAndDel(&temp, s);
+ PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
+ if (temp == NULL)
+ goto Done;
+
+ /* Paste them all together with ", " between. */
+ s = PyUnicode_FromString(", ");
+ if (s == NULL)
+ goto Done;
+ result = PyUnicode_Join(s, pieces);
+ Py_DECREF(s);
Done:
- Py_XDECREF(pieces);
- Py_XDECREF(colon);
- Py_ReprLeave((PyObject *)mp);
- return result;
+ Py_XDECREF(pieces);
+ Py_XDECREF(colon);
+ Py_ReprLeave((PyObject *)mp);
+ return result;
}
static Py_ssize_t
dict_length(PyDictObject *mp)
{
- return mp->ma_used;
+ return mp->ma_used;
}
static PyObject *
dict_subscript(PyDictObject *mp, register PyObject *key)
{
- PyObject *v;
- long hash;
- PyDictEntry *ep;
- assert(mp->ma_table != NULL);
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return NULL;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- v = ep->me_value;
- if (v == NULL) {
- if (!PyDict_CheckExact(mp)) {
- /* Look up __missing__ method if we're a subclass. */
- PyObject *missing, *res;
- static PyObject *missing_str = NULL;
- missing = _PyObject_LookupSpecial((PyObject *)mp,
- "__missing__",
- &missing_str);
- if (missing != NULL) {
- res = PyObject_CallFunctionObjArgs(missing,
- key, NULL);
- Py_DECREF(missing);
- return res;
- }
- else if (PyErr_Occurred())
- return NULL;
- }
- set_key_error(key);
- return NULL;
- }
- else
- Py_INCREF(v);
- return v;
+ PyObject *v;
+ long hash;
+ PyDictEntry *ep;
+ assert(mp->ma_table != NULL);
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ v = ep->me_value;
+ if (v == NULL) {
+ if (!PyDict_CheckExact(mp)) {
+ /* Look up __missing__ method if we're a subclass. */
+ PyObject *missing, *res;
+ static PyObject *missing_str = NULL;
+ missing = _PyObject_LookupSpecial((PyObject *)mp,
+ "__missing__",
+ &missing_str);
+ if (missing != NULL) {
+ res = PyObject_CallFunctionObjArgs(missing,
+ key, NULL);
+ Py_DECREF(missing);
+ return res;
+ }
+ else if (PyErr_Occurred())
+ return NULL;
+ }
+ set_key_error(key);
+ return NULL;
+ }
+ else
+ Py_INCREF(v);
+ return v;
}
static int
dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
{
- if (w == NULL)
- return PyDict_DelItem((PyObject *)mp, v);
- else
- return PyDict_SetItem((PyObject *)mp, v, w);
+ if (w == NULL)
+ return PyDict_DelItem((PyObject *)mp, v);
+ else
+ return PyDict_SetItem((PyObject *)mp, v, w);
}
static PyMappingMethods dict_as_mapping = {
- (lenfunc)dict_length, /*mp_length*/
- (binaryfunc)dict_subscript, /*mp_subscript*/
- (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
+ (lenfunc)dict_length, /*mp_length*/
+ (binaryfunc)dict_subscript, /*mp_subscript*/
+ (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
};
static PyObject *
dict_keys(register PyDictObject *mp)
{
- register PyObject *v;
- register Py_ssize_t i, j;
- PyDictEntry *ep;
- Py_ssize_t mask, n;
+ register PyObject *v;
+ register Py_ssize_t i, j;
+ PyDictEntry *ep;
+ Py_ssize_t mask, n;
again:
- n = mp->ma_used;
- v = PyList_New(n);
- if (v == NULL)
- return NULL;
- if (n != mp->ma_used) {
- /* Durnit. The allocations caused the dict to resize.
- * Just start over, this shouldn't normally happen.
- */
- Py_DECREF(v);
- goto again;
- }
- ep = mp->ma_table;
- mask = mp->ma_mask;
- for (i = 0, j = 0; i <= mask; i++) {
- if (ep[i].me_value != NULL) {
- PyObject *key = ep[i].me_key;
- Py_INCREF(key);
- PyList_SET_ITEM(v, j, key);
- j++;
- }
- }
- assert(j == n);
- return v;
+ n = mp->ma_used;
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ if (n != mp->ma_used) {
+ /* Durnit. The allocations caused the dict to resize.
+ * Just start over, this shouldn't normally happen.
+ */
+ Py_DECREF(v);
+ goto again;
+ }
+ ep = mp->ma_table;
+ mask = mp->ma_mask;
+ for (i = 0, j = 0; i <= mask; i++) {
+ if (ep[i].me_value != NULL) {
+ PyObject *key = ep[i].me_key;
+ Py_INCREF(key);
+ PyList_SET_ITEM(v, j, key);
+ j++;
+ }
+ }
+ assert(j == n);
+ return v;
}
static PyObject *
dict_values(register PyDictObject *mp)
{
- register PyObject *v;
- register Py_ssize_t i, j;
- PyDictEntry *ep;
- Py_ssize_t mask, n;
+ register PyObject *v;
+ register Py_ssize_t i, j;
+ PyDictEntry *ep;
+ Py_ssize_t mask, n;
again:
- n = mp->ma_used;
- v = PyList_New(n);
- if (v == NULL)
- return NULL;
- if (n != mp->ma_used) {
- /* Durnit. The allocations caused the dict to resize.
- * Just start over, this shouldn't normally happen.
- */
- Py_DECREF(v);
- goto again;
- }
- ep = mp->ma_table;
- mask = mp->ma_mask;
- for (i = 0, j = 0; i <= mask; i++) {
- if (ep[i].me_value != NULL) {
- PyObject *value = ep[i].me_value;
- Py_INCREF(value);
- PyList_SET_ITEM(v, j, value);
- j++;
- }
- }
- assert(j == n);
- return v;
+ n = mp->ma_used;
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ if (n != mp->ma_used) {
+ /* Durnit. The allocations caused the dict to resize.
+ * Just start over, this shouldn't normally happen.
+ */
+ Py_DECREF(v);
+ goto again;
+ }
+ ep = mp->ma_table;
+ mask = mp->ma_mask;
+ for (i = 0, j = 0; i <= mask; i++) {
+ if (ep[i].me_value != NULL) {
+ PyObject *value = ep[i].me_value;
+ Py_INCREF(value);
+ PyList_SET_ITEM(v, j, value);
+ j++;
+ }
+ }
+ assert(j == n);
+ return v;
}
static PyObject *
dict_items(register PyDictObject *mp)
{
- register PyObject *v;
- register Py_ssize_t i, j, n;
- Py_ssize_t mask;
- PyObject *item, *key, *value;
- PyDictEntry *ep;
+ register PyObject *v;
+ register Py_ssize_t i, j, n;
+ Py_ssize_t mask;
+ PyObject *item, *key, *value;
+ PyDictEntry *ep;
- /* Preallocate the list of tuples, to avoid allocations during
- * the loop over the items, which could trigger GC, which
- * could resize the dict. :-(
- */
+ /* Preallocate the list of tuples, to avoid allocations during
+ * the loop over the items, which could trigger GC, which
+ * could resize the dict. :-(
+ */
again:
- n = mp->ma_used;
- v = PyList_New(n);
- if (v == NULL)
- return NULL;
- for (i = 0; i < n; i++) {
- item = PyTuple_New(2);
- if (item == NULL) {
- Py_DECREF(v);
- return NULL;
- }
- PyList_SET_ITEM(v, i, item);
- }
- if (n != mp->ma_used) {
- /* Durnit. The allocations caused the dict to resize.
- * Just start over, this shouldn't normally happen.
- */
- Py_DECREF(v);
- goto again;
- }
- /* Nothing we do below makes any function calls. */
- ep = mp->ma_table;
- mask = mp->ma_mask;
- for (i = 0, j = 0; i <= mask; i++) {
- if ((value=ep[i].me_value) != NULL) {
- key = ep[i].me_key;
- item = PyList_GET_ITEM(v, j);
- Py_INCREF(key);
- PyTuple_SET_ITEM(item, 0, key);
- Py_INCREF(value);
- PyTuple_SET_ITEM(item, 1, value);
- j++;
- }
- }
- assert(j == n);
- return v;
+ n = mp->ma_used;
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ for (i = 0; i < n; i++) {
+ item = PyTuple_New(2);
+ if (item == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyList_SET_ITEM(v, i, item);
+ }
+ if (n != mp->ma_used) {
+ /* Durnit. The allocations caused the dict to resize.
+ * Just start over, this shouldn't normally happen.
+ */
+ Py_DECREF(v);
+ goto again;
+ }
+ /* Nothing we do below makes any function calls. */
+ ep = mp->ma_table;
+ mask = mp->ma_mask;
+ for (i = 0, j = 0; i <= mask; i++) {
+ if ((value=ep[i].me_value) != NULL) {
+ key = ep[i].me_key;
+ item = PyList_GET_ITEM(v, j);
+ Py_INCREF(key);
+ PyTuple_SET_ITEM(item, 0, key);
+ Py_INCREF(value);
+ PyTuple_SET_ITEM(item, 1, value);
+ j++;
+ }
+ }
+ assert(j == n);
+ return v;
}
static PyObject *
dict_fromkeys(PyObject *cls, PyObject *args)
{
- PyObject *seq;
- PyObject *value = Py_None;
- PyObject *it; /* iter(seq) */
- PyObject *key;
- PyObject *d;
- int status;
-
- if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
- return NULL;
-
- d = PyObject_CallObject(cls, NULL);
- if (d == NULL)
- return NULL;
-
- if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
- PyDictObject *mp = (PyDictObject *)d;
- PyObject *oldvalue;
- Py_ssize_t pos = 0;
- PyObject *key;
- long hash;
-
- if (dictresize(mp, Py_SIZE(seq)))
- return NULL;
-
- while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
- Py_INCREF(key);
- Py_INCREF(value);
- if (insertdict(mp, key, hash, value))
- return NULL;
- }
- return d;
- }
-
- if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
- PyDictObject *mp = (PyDictObject *)d;
- Py_ssize_t pos = 0;
- PyObject *key;
- long hash;
-
- if (dictresize(mp, PySet_GET_SIZE(seq)))
- return NULL;
-
- while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
- Py_INCREF(key);
- Py_INCREF(value);
- if (insertdict(mp, key, hash, value))
- return NULL;
- }
- return d;
- }
-
- it = PyObject_GetIter(seq);
- if (it == NULL){
- Py_DECREF(d);
- return NULL;
- }
-
- if (PyDict_CheckExact(d)) {
- while ((key = PyIter_Next(it)) != NULL) {
- status = PyDict_SetItem(d, key, value);
- Py_DECREF(key);
- if (status < 0)
- goto Fail;
- }
- } else {
- while ((key = PyIter_Next(it)) != NULL) {
- status = PyObject_SetItem(d, key, value);
- Py_DECREF(key);
- if (status < 0)
- goto Fail;
- }
- }
-
- if (PyErr_Occurred())
- goto Fail;
- Py_DECREF(it);
- return d;
+ PyObject *seq;
+ PyObject *value = Py_None;
+ PyObject *it; /* iter(seq) */
+ PyObject *key;
+ PyObject *d;
+ int status;
+
+ if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
+ return NULL;
+
+ d = PyObject_CallObject(cls, NULL);
+ if (d == NULL)
+ return NULL;
+
+ if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
+ PyDictObject *mp = (PyDictObject *)d;
+ PyObject *oldvalue;
+ Py_ssize_t pos = 0;
+ PyObject *key;
+ long hash;
+
+ if (dictresize(mp, Py_SIZE(seq)))
+ return NULL;
+
+ while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
+ Py_INCREF(key);
+ Py_INCREF(value);
+ if (insertdict(mp, key, hash, value))
+ return NULL;
+ }
+ return d;
+ }
+
+ if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
+ PyDictObject *mp = (PyDictObject *)d;
+ Py_ssize_t pos = 0;
+ PyObject *key;
+ long hash;
+
+ if (dictresize(mp, PySet_GET_SIZE(seq)))
+ return NULL;
+
+ while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
+ Py_INCREF(key);
+ Py_INCREF(value);
+ if (insertdict(mp, key, hash, value))
+ return NULL;
+ }
+ return d;
+ }
+
+ it = PyObject_GetIter(seq);
+ if (it == NULL){
+ Py_DECREF(d);
+ return NULL;
+ }
+
+ if (PyDict_CheckExact(d)) {
+ while ((key = PyIter_Next(it)) != NULL) {
+ status = PyDict_SetItem(d, key, value);
+ Py_DECREF(key);
+ if (status < 0)
+ goto Fail;
+ }
+ } else {
+ while ((key = PyIter_Next(it)) != NULL) {
+ status = PyObject_SetItem(d, key, value);
+ Py_DECREF(key);
+ if (status < 0)
+ goto Fail;
+ }
+ }
+
+ if (PyErr_Occurred())
+ goto Fail;
+ Py_DECREF(it);
+ return d;
Fail:
- Py_DECREF(it);
- Py_DECREF(d);
- return NULL;
+ Py_DECREF(it);
+ Py_DECREF(d);
+ return NULL;
}
static int
dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
{
- PyObject *arg = NULL;
- int result = 0;
+ PyObject *arg = NULL;
+ int result = 0;
- if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
- result = -1;
+ if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
+ result = -1;
- else if (arg != NULL) {
- if (PyObject_HasAttrString(arg, "keys"))
- result = PyDict_Merge(self, arg, 1);
- else
- result = PyDict_MergeFromSeq2(self, arg, 1);
- }
- if (result == 0 && kwds != NULL)
- result = PyDict_Merge(self, kwds, 1);
- return result;
+ else if (arg != NULL) {
+ if (PyObject_HasAttrString(arg, "keys"))
+ result = PyDict_Merge(self, arg, 1);
+ else
+ result = PyDict_MergeFromSeq2(self, arg, 1);
+ }
+ if (result == 0 && kwds != NULL)
+ result = PyDict_Merge(self, kwds, 1);
+ return result;
}
static PyObject *
dict_update(PyObject *self, PyObject *args, PyObject *kwds)
{
- if (dict_update_common(self, args, kwds, "update") != -1)
- Py_RETURN_NONE;
- return NULL;
+ if (dict_update_common(self, args, kwds, "update") != -1)
+ Py_RETURN_NONE;
+ return NULL;
}
/* Update unconditionally replaces existing items.
@@ -1412,238 +1412,238 @@ dict_update(PyObject *self, PyObject *args, PyObject *kwds)
int
PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
{
- PyObject *it; /* iter(seq2) */
- Py_ssize_t i; /* index into seq2 of current element */
- PyObject *item; /* seq2[i] */
- PyObject *fast; /* item as a 2-tuple or 2-list */
-
- assert(d != NULL);
- assert(PyDict_Check(d));
- assert(seq2 != NULL);
-
- it = PyObject_GetIter(seq2);
- if (it == NULL)
- return -1;
-
- for (i = 0; ; ++i) {
- PyObject *key, *value;
- Py_ssize_t n;
-
- fast = NULL;
- item = PyIter_Next(it);
- if (item == NULL) {
- if (PyErr_Occurred())
- goto Fail;
- break;
- }
-
- /* Convert item to sequence, and verify length 2. */
- fast = PySequence_Fast(item, "");
- if (fast == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError,
- "cannot convert dictionary update "
- "sequence element #%zd to a sequence",
- i);
- goto Fail;
- }
- n = PySequence_Fast_GET_SIZE(fast);
- if (n != 2) {
- PyErr_Format(PyExc_ValueError,
- "dictionary update sequence element #%zd "
- "has length %zd; 2 is required",
- i, n);
- goto Fail;
- }
-
- /* Update/merge with this (key, value) pair. */
- key = PySequence_Fast_GET_ITEM(fast, 0);
- value = PySequence_Fast_GET_ITEM(fast, 1);
- if (override || PyDict_GetItem(d, key) == NULL) {
- int status = PyDict_SetItem(d, key, value);
- if (status < 0)
- goto Fail;
- }
- Py_DECREF(fast);
- Py_DECREF(item);
- }
-
- i = 0;
- goto Return;
+ PyObject *it; /* iter(seq2) */
+ Py_ssize_t i; /* index into seq2 of current element */
+ PyObject *item; /* seq2[i] */
+ PyObject *fast; /* item as a 2-tuple or 2-list */
+
+ assert(d != NULL);
+ assert(PyDict_Check(d));
+ assert(seq2 != NULL);
+
+ it = PyObject_GetIter(seq2);
+ if (it == NULL)
+ return -1;
+
+ for (i = 0; ; ++i) {
+ PyObject *key, *value;
+ Py_ssize_t n;
+
+ fast = NULL;
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto Fail;
+ break;
+ }
+
+ /* Convert item to sequence, and verify length 2. */
+ fast = PySequence_Fast(item, "");
+ if (fast == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "cannot convert dictionary update "
+ "sequence element #%zd to a sequence",
+ i);
+ goto Fail;
+ }
+ n = PySequence_Fast_GET_SIZE(fast);
+ if (n != 2) {
+ PyErr_Format(PyExc_ValueError,
+ "dictionary update sequence element #%zd "
+ "has length %zd; 2 is required",
+ i, n);
+ goto Fail;
+ }
+
+ /* Update/merge with this (key, value) pair. */
+ key = PySequence_Fast_GET_ITEM(fast, 0);
+ value = PySequence_Fast_GET_ITEM(fast, 1);
+ if (override || PyDict_GetItem(d, key) == NULL) {
+ int status = PyDict_SetItem(d, key, value);
+ if (status < 0)
+ goto Fail;
+ }
+ Py_DECREF(fast);
+ Py_DECREF(item);
+ }
+
+ i = 0;
+ goto Return;
Fail:
- Py_XDECREF(item);
- Py_XDECREF(fast);
- i = -1;
+ Py_XDECREF(item);
+ Py_XDECREF(fast);
+ i = -1;
Return:
- Py_DECREF(it);
- return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
+ Py_DECREF(it);
+ return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
}
int
PyDict_Update(PyObject *a, PyObject *b)
{
- return PyDict_Merge(a, b, 1);
+ return PyDict_Merge(a, b, 1);
}
int
PyDict_Merge(PyObject *a, PyObject *b, int override)
{
- register PyDictObject *mp, *other;
- register Py_ssize_t i;
- PyDictEntry *entry;
-
- /* We accept for the argument either a concrete dictionary object,
- * or an abstract "mapping" object. For the former, we can do
- * things quite efficiently. For the latter, we only require that
- * PyMapping_Keys() and PyObject_GetItem() be supported.
- */
- if (a == NULL || !PyDict_Check(a) || b == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
- mp = (PyDictObject*)a;
- if (PyDict_Check(b)) {
- other = (PyDictObject*)b;
- if (other == mp || other->ma_used == 0)
- /* a.update(a) or a.update({}); nothing to do */
- return 0;
- if (mp->ma_used == 0)
- /* Since the target dict is empty, PyDict_GetItem()
- * always returns NULL. Setting override to 1
- * skips the unnecessary test.
- */
- override = 1;
- /* Do one big resize at the start, rather than
- * incrementally resizing as we insert new items. Expect
- * that there will be no (or few) overlapping keys.
- */
- if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
- if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
- return -1;
- }
- for (i = 0; i <= other->ma_mask; i++) {
- entry = &other->ma_table[i];
- if (entry->me_value != NULL &&
- (override ||
- PyDict_GetItem(a, entry->me_key) == NULL)) {
- Py_INCREF(entry->me_key);
- Py_INCREF(entry->me_value);
- if (insertdict(mp, entry->me_key,
- (long)entry->me_hash,
- entry->me_value) != 0)
- return -1;
- }
- }
- }
- else {
- /* Do it the generic, slower way */
- PyObject *keys = PyMapping_Keys(b);
- PyObject *iter;
- PyObject *key, *value;
- int status;
-
- if (keys == NULL)
- /* Docstring says this is equivalent to E.keys() so
- * if E doesn't have a .keys() method we want
- * AttributeError to percolate up. Might as well
- * do the same for any other error.
- */
- return -1;
-
- iter = PyObject_GetIter(keys);
- Py_DECREF(keys);
- if (iter == NULL)
- return -1;
-
- for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
- if (!override && PyDict_GetItem(a, key) != NULL) {
- Py_DECREF(key);
- continue;
- }
- value = PyObject_GetItem(b, key);
- if (value == NULL) {
- Py_DECREF(iter);
- Py_DECREF(key);
- return -1;
- }
- status = PyDict_SetItem(a, key, value);
- Py_DECREF(key);
- Py_DECREF(value);
- if (status < 0) {
- Py_DECREF(iter);
- return -1;
- }
- }
- Py_DECREF(iter);
- if (PyErr_Occurred())
- /* Iterator completed, via error */
- return -1;
- }
- return 0;
+ register PyDictObject *mp, *other;
+ register Py_ssize_t i;
+ PyDictEntry *entry;
+
+ /* We accept for the argument either a concrete dictionary object,
+ * or an abstract "mapping" object. For the former, we can do
+ * things quite efficiently. For the latter, we only require that
+ * PyMapping_Keys() and PyObject_GetItem() be supported.
+ */
+ if (a == NULL || !PyDict_Check(a) || b == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ mp = (PyDictObject*)a;
+ if (PyDict_Check(b)) {
+ other = (PyDictObject*)b;
+ if (other == mp || other->ma_used == 0)
+ /* a.update(a) or a.update({}); nothing to do */
+ return 0;
+ if (mp->ma_used == 0)
+ /* Since the target dict is empty, PyDict_GetItem()
+ * always returns NULL. Setting override to 1
+ * skips the unnecessary test.
+ */
+ override = 1;
+ /* Do one big resize at the start, rather than
+ * incrementally resizing as we insert new items. Expect
+ * that there will be no (or few) overlapping keys.
+ */
+ if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
+ if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
+ return -1;
+ }
+ for (i = 0; i <= other->ma_mask; i++) {
+ entry = &other->ma_table[i];
+ if (entry->me_value != NULL &&
+ (override ||
+ PyDict_GetItem(a, entry->me_key) == NULL)) {
+ Py_INCREF(entry->me_key);
+ Py_INCREF(entry->me_value);
+ if (insertdict(mp, entry->me_key,
+ (long)entry->me_hash,
+ entry->me_value) != 0)
+ return -1;
+ }
+ }
+ }
+ else {
+ /* Do it the generic, slower way */
+ PyObject *keys = PyMapping_Keys(b);
+ PyObject *iter;
+ PyObject *key, *value;
+ int status;
+
+ if (keys == NULL)
+ /* Docstring says this is equivalent to E.keys() so
+ * if E doesn't have a .keys() method we want
+ * AttributeError to percolate up. Might as well
+ * do the same for any other error.
+ */
+ return -1;
+
+ iter = PyObject_GetIter(keys);
+ Py_DECREF(keys);
+ if (iter == NULL)
+ return -1;
+
+ for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
+ if (!override && PyDict_GetItem(a, key) != NULL) {
+ Py_DECREF(key);
+ continue;
+ }
+ value = PyObject_GetItem(b, key);
+ if (value == NULL) {
+ Py_DECREF(iter);
+ Py_DECREF(key);
+ return -1;
+ }
+ status = PyDict_SetItem(a, key, value);
+ Py_DECREF(key);
+ Py_DECREF(value);
+ if (status < 0) {
+ Py_DECREF(iter);
+ return -1;
+ }
+ }
+ Py_DECREF(iter);
+ if (PyErr_Occurred())
+ /* Iterator completed, via error */
+ return -1;
+ }
+ return 0;
}
static PyObject *
dict_copy(register PyDictObject *mp)
{
- return PyDict_Copy((PyObject*)mp);
+ return PyDict_Copy((PyObject*)mp);
}
PyObject *
PyDict_Copy(PyObject *o)
{
- PyObject *copy;
+ PyObject *copy;
- if (o == NULL || !PyDict_Check(o)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- copy = PyDict_New();
- if (copy == NULL)
- return NULL;
- if (PyDict_Merge(copy, o, 1) == 0)
- return copy;
- Py_DECREF(copy);
- return NULL;
+ if (o == NULL || !PyDict_Check(o)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ copy = PyDict_New();
+ if (copy == NULL)
+ return NULL;
+ if (PyDict_Merge(copy, o, 1) == 0)
+ return copy;
+ Py_DECREF(copy);
+ return NULL;
}
Py_ssize_t
PyDict_Size(PyObject *mp)
{
- if (mp == NULL || !PyDict_Check(mp)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return ((PyDictObject *)mp)->ma_used;
+ if (mp == NULL || !PyDict_Check(mp)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return ((PyDictObject *)mp)->ma_used;
}
PyObject *
PyDict_Keys(PyObject *mp)
{
- if (mp == NULL || !PyDict_Check(mp)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return dict_keys((PyDictObject *)mp);
+ if (mp == NULL || !PyDict_Check(mp)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return dict_keys((PyDictObject *)mp);
}
PyObject *
PyDict_Values(PyObject *mp)
{
- if (mp == NULL || !PyDict_Check(mp)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return dict_values((PyDictObject *)mp);
+ if (mp == NULL || !PyDict_Check(mp)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return dict_values((PyDictObject *)mp);
}
PyObject *
PyDict_Items(PyObject *mp)
{
- if (mp == NULL || !PyDict_Check(mp)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return dict_items((PyDictObject *)mp);
+ if (mp == NULL || !PyDict_Check(mp)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return dict_items((PyDictObject *)mp);
}
/* Return 1 if dicts equal, 0 if not, -1 if error.
@@ -1653,271 +1653,271 @@ PyDict_Items(PyObject *mp)
static int
dict_equal(PyDictObject *a, PyDictObject *b)
{
- Py_ssize_t i;
-
- if (a->ma_used != b->ma_used)
- /* can't be equal if # of entries differ */
- return 0;
-
- /* Same # of entries -- check all of 'em. Exit early on any diff. */
- for (i = 0; i <= a->ma_mask; i++) {
- PyObject *aval = a->ma_table[i].me_value;
- if (aval != NULL) {
- int cmp;
- PyObject *bval;
- PyObject *key = a->ma_table[i].me_key;
- /* temporarily bump aval's refcount to ensure it stays
- alive until we're done with it */
- Py_INCREF(aval);
- /* ditto for key */
- Py_INCREF(key);
- bval = PyDict_GetItemWithError((PyObject *)b, key);
- Py_DECREF(key);
- if (bval == NULL) {
- Py_DECREF(aval);
- if (PyErr_Occurred())
- return -1;
- return 0;
- }
- cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
- Py_DECREF(aval);
- if (cmp <= 0) /* error or not equal */
- return cmp;
- }
- }
- return 1;
+ Py_ssize_t i;
+
+ if (a->ma_used != b->ma_used)
+ /* can't be equal if # of entries differ */
+ return 0;
+
+ /* Same # of entries -- check all of 'em. Exit early on any diff. */
+ for (i = 0; i <= a->ma_mask; i++) {
+ PyObject *aval = a->ma_table[i].me_value;
+ if (aval != NULL) {
+ int cmp;
+ PyObject *bval;
+ PyObject *key = a->ma_table[i].me_key;
+ /* temporarily bump aval's refcount to ensure it stays
+ alive until we're done with it */
+ Py_INCREF(aval);
+ /* ditto for key */
+ Py_INCREF(key);
+ bval = PyDict_GetItemWithError((PyObject *)b, key);
+ Py_DECREF(key);
+ if (bval == NULL) {
+ Py_DECREF(aval);
+ if (PyErr_Occurred())
+ return -1;
+ return 0;
+ }
+ cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
+ Py_DECREF(aval);
+ if (cmp <= 0) /* error or not equal */
+ return cmp;
+ }
+ }
+ return 1;
}
static PyObject *
dict_richcompare(PyObject *v, PyObject *w, int op)
{
- int cmp;
- PyObject *res;
-
- if (!PyDict_Check(v) || !PyDict_Check(w)) {
- res = Py_NotImplemented;
- }
- else if (op == Py_EQ || op == Py_NE) {
- cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
- if (cmp < 0)
- return NULL;
- res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
- }
- else
- res = Py_NotImplemented;
- Py_INCREF(res);
- return res;
+ int cmp;
+ PyObject *res;
+
+ if (!PyDict_Check(v) || !PyDict_Check(w)) {
+ res = Py_NotImplemented;
+ }
+ else if (op == Py_EQ || op == Py_NE) {
+ cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
+ if (cmp < 0)
+ return NULL;
+ res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
+ }
+ else
+ res = Py_NotImplemented;
+ Py_INCREF(res);
+ return res;
}
static PyObject *
dict_contains(register PyDictObject *mp, PyObject *key)
{
- long hash;
- PyDictEntry *ep;
+ long hash;
+ PyDictEntry *ep;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return NULL;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- return PyBool_FromLong(ep->me_value != NULL);
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ return PyBool_FromLong(ep->me_value != NULL);
}
static PyObject *
dict_get(register PyDictObject *mp, PyObject *args)
{
- PyObject *key;
- PyObject *failobj = Py_None;
- PyObject *val = NULL;
- long hash;
- PyDictEntry *ep;
+ PyObject *key;
+ PyObject *failobj = Py_None;
+ PyObject *val = NULL;
+ long hash;
+ PyDictEntry *ep;
- if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
+ return NULL;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return NULL;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- val = ep->me_value;
- if (val == NULL)
- val = failobj;
- Py_INCREF(val);
- return val;
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ val = ep->me_value;
+ if (val == NULL)
+ val = failobj;
+ Py_INCREF(val);
+ return val;
}
static PyObject *
dict_setdefault(register PyDictObject *mp, PyObject *args)
{
- PyObject *key;
- PyObject *failobj = Py_None;
- PyObject *val = NULL;
- long hash;
- PyDictEntry *ep;
-
- if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
- return NULL;
-
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return NULL;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- val = ep->me_value;
- if (val == NULL) {
- val = failobj;
- if (PyDict_SetItem((PyObject*)mp, key, failobj))
- val = NULL;
- }
- Py_XINCREF(val);
- return val;
+ PyObject *key;
+ PyObject *failobj = Py_None;
+ PyObject *val = NULL;
+ long hash;
+ PyDictEntry *ep;
+
+ if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
+ return NULL;
+
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ val = ep->me_value;
+ if (val == NULL) {
+ val = failobj;
+ if (PyDict_SetItem((PyObject*)mp, key, failobj))
+ val = NULL;
+ }
+ Py_XINCREF(val);
+ return val;
}
static PyObject *
dict_clear(register PyDictObject *mp)
{
- PyDict_Clear((PyObject *)mp);
- Py_RETURN_NONE;
+ PyDict_Clear((PyObject *)mp);
+ Py_RETURN_NONE;
}
static PyObject *
dict_pop(PyDictObject *mp, PyObject *args)
{
- long hash;
- PyDictEntry *ep;
- PyObject *old_value, *old_key;
- PyObject *key, *deflt = NULL;
-
- if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
- return NULL;
- if (mp->ma_used == 0) {
- if (deflt) {
- Py_INCREF(deflt);
- return deflt;
- }
- PyErr_SetString(PyExc_KeyError,
- "pop(): dictionary is empty");
- return NULL;
- }
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return NULL;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- if (ep == NULL)
- return NULL;
- if (ep->me_value == NULL) {
- if (deflt) {
- Py_INCREF(deflt);
- return deflt;
- }
- set_key_error(key);
- return NULL;
- }
- old_key = ep->me_key;
- Py_INCREF(dummy);
- ep->me_key = dummy;
- old_value = ep->me_value;
- ep->me_value = NULL;
- mp->ma_used--;
- Py_DECREF(old_key);
- return old_value;
+ long hash;
+ PyDictEntry *ep;
+ PyObject *old_value, *old_key;
+ PyObject *key, *deflt = NULL;
+
+ if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
+ return NULL;
+ if (mp->ma_used == 0) {
+ if (deflt) {
+ Py_INCREF(deflt);
+ return deflt;
+ }
+ PyErr_SetString(PyExc_KeyError,
+ "pop(): dictionary is empty");
+ return NULL;
+ }
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return NULL;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ if (ep == NULL)
+ return NULL;
+ if (ep->me_value == NULL) {
+ if (deflt) {
+ Py_INCREF(deflt);
+ return deflt;
+ }
+ set_key_error(key);
+ return NULL;
+ }
+ old_key = ep->me_key;
+ Py_INCREF(dummy);
+ ep->me_key = dummy;
+ old_value = ep->me_value;
+ ep->me_value = NULL;
+ mp->ma_used--;
+ Py_DECREF(old_key);
+ return old_value;
}
static PyObject *
dict_popitem(PyDictObject *mp)
{
- Py_ssize_t i = 0;
- PyDictEntry *ep;
- PyObject *res;
-
- /* Allocate the result tuple before checking the size. Believe it
- * or not, this allocation could trigger a garbage collection which
- * could empty the dict, so if we checked the size first and that
- * happened, the result would be an infinite loop (searching for an
- * entry that no longer exists). Note that the usual popitem()
- * idiom is "while d: k, v = d.popitem()". so needing to throw the
- * tuple away if the dict *is* empty isn't a significant
- * inefficiency -- possible, but unlikely in practice.
- */
- res = PyTuple_New(2);
- if (res == NULL)
- return NULL;
- if (mp->ma_used == 0) {
- Py_DECREF(res);
- PyErr_SetString(PyExc_KeyError,
- "popitem(): dictionary is empty");
- return NULL;
- }
- /* Set ep to "the first" dict entry with a value. We abuse the hash
- * field of slot 0 to hold a search finger:
- * If slot 0 has a value, use slot 0.
- * Else slot 0 is being used to hold a search finger,
- * and we use its hash value as the first index to look.
- */
- ep = &mp->ma_table[0];
- if (ep->me_value == NULL) {
- i = ep->me_hash;
- /* The hash field may be a real hash value, or it may be a
- * legit search finger, or it may be a once-legit search
- * finger that's out of bounds now because it wrapped around
- * or the table shrunk -- simply make sure it's in bounds now.
- */
- if (i > mp->ma_mask || i < 1)
- i = 1; /* skip slot 0 */
- while ((ep = &mp->ma_table[i])->me_value == NULL) {
- i++;
- if (i > mp->ma_mask)
- i = 1;
- }
- }
- PyTuple_SET_ITEM(res, 0, ep->me_key);
- PyTuple_SET_ITEM(res, 1, ep->me_value);
- Py_INCREF(dummy);
- ep->me_key = dummy;
- ep->me_value = NULL;
- mp->ma_used--;
- assert(mp->ma_table[0].me_value == NULL);
- mp->ma_table[0].me_hash = i + 1; /* next place to start */
- return res;
+ Py_ssize_t i = 0;
+ PyDictEntry *ep;
+ PyObject *res;
+
+ /* Allocate the result tuple before checking the size. Believe it
+ * or not, this allocation could trigger a garbage collection which
+ * could empty the dict, so if we checked the size first and that
+ * happened, the result would be an infinite loop (searching for an
+ * entry that no longer exists). Note that the usual popitem()
+ * idiom is "while d: k, v = d.popitem()". so needing to throw the
+ * tuple away if the dict *is* empty isn't a significant
+ * inefficiency -- possible, but unlikely in practice.
+ */
+ res = PyTuple_New(2);
+ if (res == NULL)
+ return NULL;
+ if (mp->ma_used == 0) {
+ Py_DECREF(res);
+ PyErr_SetString(PyExc_KeyError,
+ "popitem(): dictionary is empty");
+ return NULL;
+ }
+ /* Set ep to "the first" dict entry with a value. We abuse the hash
+ * field of slot 0 to hold a search finger:
+ * If slot 0 has a value, use slot 0.
+ * Else slot 0 is being used to hold a search finger,
+ * and we use its hash value as the first index to look.
+ */
+ ep = &mp->ma_table[0];
+ if (ep->me_value == NULL) {
+ i = ep->me_hash;
+ /* The hash field may be a real hash value, or it may be a
+ * legit search finger, or it may be a once-legit search
+ * finger that's out of bounds now because it wrapped around
+ * or the table shrunk -- simply make sure it's in bounds now.
+ */
+ if (i > mp->ma_mask || i < 1)
+ i = 1; /* skip slot 0 */
+ while ((ep = &mp->ma_table[i])->me_value == NULL) {
+ i++;
+ if (i > mp->ma_mask)
+ i = 1;
+ }
+ }
+ PyTuple_SET_ITEM(res, 0, ep->me_key);
+ PyTuple_SET_ITEM(res, 1, ep->me_value);
+ Py_INCREF(dummy);
+ ep->me_key = dummy;
+ ep->me_value = NULL;
+ mp->ma_used--;
+ assert(mp->ma_table[0].me_value == NULL);
+ mp->ma_table[0].me_hash = i + 1; /* next place to start */
+ return res;
}
static int
dict_traverse(PyObject *op, visitproc visit, void *arg)
{
- Py_ssize_t i = 0;
- PyObject *pk;
- PyObject *pv;
+ Py_ssize_t i = 0;
+ PyObject *pk;
+ PyObject *pv;
- while (PyDict_Next(op, &i, &pk, &pv)) {
- Py_VISIT(pk);
- Py_VISIT(pv);
- }
- return 0;
+ while (PyDict_Next(op, &i, &pk, &pv)) {
+ Py_VISIT(pk);
+ Py_VISIT(pv);
+ }
+ return 0;
}
static int
dict_tp_clear(PyObject *op)
{
- PyDict_Clear(op);
- return 0;
+ PyDict_Clear(op);
+ return 0;
}
static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
@@ -1925,12 +1925,12 @@ static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
static PyObject *
dict_sizeof(PyDictObject *mp)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = sizeof(PyDictObject);
- if (mp->ma_table != mp->ma_smalltable)
- res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
- return PyLong_FromSsize_t(res);
+ res = sizeof(PyDictObject);
+ if (mp->ma_table != mp->ma_smalltable)
+ res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
+ return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(contains__doc__,
@@ -1977,126 +1977,126 @@ static PyObject *dictitems_new(PyObject *);
static PyObject *dictvalues_new(PyObject *);
PyDoc_STRVAR(keys__doc__,
- "D.keys() -> a set-like object providing a view on D's keys");
+ "D.keys() -> a set-like object providing a view on D's keys");
PyDoc_STRVAR(items__doc__,
- "D.items() -> a set-like object providing a view on D's items");
+ "D.items() -> a set-like object providing a view on D's items");
PyDoc_STRVAR(values__doc__,
- "D.values() -> an object providing a view on D's values");
+ "D.values() -> an object providing a view on D's values");
static PyMethodDef mapp_methods[] = {
- {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
- contains__doc__},
- {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
- getitem__doc__},
- {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
- sizeof__doc__},
- {"get", (PyCFunction)dict_get, METH_VARARGS,
- get__doc__},
- {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
- setdefault_doc__},
- {"pop", (PyCFunction)dict_pop, METH_VARARGS,
- pop__doc__},
- {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
- popitem__doc__},
- {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
- keys__doc__},
- {"items", (PyCFunction)dictitems_new, METH_NOARGS,
- items__doc__},
- {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
- values__doc__},
- {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
- update__doc__},
- {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
- fromkeys__doc__},
- {"clear", (PyCFunction)dict_clear, METH_NOARGS,
- clear__doc__},
- {"copy", (PyCFunction)dict_copy, METH_NOARGS,
- copy__doc__},
- {NULL, NULL} /* sentinel */
+ {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
+ contains__doc__},
+ {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
+ getitem__doc__},
+ {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
+ sizeof__doc__},
+ {"get", (PyCFunction)dict_get, METH_VARARGS,
+ get__doc__},
+ {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
+ setdefault_doc__},
+ {"pop", (PyCFunction)dict_pop, METH_VARARGS,
+ pop__doc__},
+ {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
+ popitem__doc__},
+ {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
+ keys__doc__},
+ {"items", (PyCFunction)dictitems_new, METH_NOARGS,
+ items__doc__},
+ {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
+ values__doc__},
+ {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
+ update__doc__},
+ {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
+ fromkeys__doc__},
+ {"clear", (PyCFunction)dict_clear, METH_NOARGS,
+ clear__doc__},
+ {"copy", (PyCFunction)dict_copy, METH_NOARGS,
+ copy__doc__},
+ {NULL, NULL} /* sentinel */
};
/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
int
PyDict_Contains(PyObject *op, PyObject *key)
{
- long hash;
- PyDictObject *mp = (PyDictObject *)op;
- PyDictEntry *ep;
+ long hash;
+ PyDictObject *mp = (PyDictObject *)op;
+ PyDictEntry *ep;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- ep = (mp->ma_lookup)(mp, key, hash);
- return ep == NULL ? -1 : (ep->me_value != NULL);
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ ep = (mp->ma_lookup)(mp, key, hash);
+ return ep == NULL ? -1 : (ep->me_value != NULL);
}
/* Internal version of PyDict_Contains used when the hash value is already known */
int
_PyDict_Contains(PyObject *op, PyObject *key, long hash)
{
- PyDictObject *mp = (PyDictObject *)op;
- PyDictEntry *ep;
+ PyDictObject *mp = (PyDictObject *)op;
+ PyDictEntry *ep;
- ep = (mp->ma_lookup)(mp, key, hash);
- return ep == NULL ? -1 : (ep->me_value != NULL);
+ ep = (mp->ma_lookup)(mp, key, hash);
+ return ep == NULL ? -1 : (ep->me_value != NULL);
}
/* Hack to implement "key in dict" */
static PySequenceMethods dict_as_sequence = {
- 0, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- PyDict_Contains, /* sq_contains */
- 0, /* sq_inplace_concat */
- 0, /* sq_inplace_repeat */
+ 0, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ PyDict_Contains, /* sq_contains */
+ 0, /* sq_inplace_concat */
+ 0, /* sq_inplace_repeat */
};
static PyObject *
dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *self;
-
- assert(type != NULL && type->tp_alloc != NULL);
- self = type->tp_alloc(type, 0);
- if (self != NULL) {
- PyDictObject *d = (PyDictObject *)self;
- /* It's guaranteed that tp->alloc zeroed out the struct. */
- assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
- INIT_NONZERO_DICT_SLOTS(d);
- d->ma_lookup = lookdict_unicode;
- /* The object has been implicitely tracked by tp_alloc */
- if (type == &PyDict_Type)
- _PyObject_GC_UNTRACK(d);
+ PyObject *self;
+
+ assert(type != NULL && type->tp_alloc != NULL);
+ self = type->tp_alloc(type, 0);
+ if (self != NULL) {
+ PyDictObject *d = (PyDictObject *)self;
+ /* It's guaranteed that tp->alloc zeroed out the struct. */
+ assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
+ INIT_NONZERO_DICT_SLOTS(d);
+ d->ma_lookup = lookdict_unicode;
+ /* The object has been implicitely tracked by tp_alloc */
+ if (type == &PyDict_Type)
+ _PyObject_GC_UNTRACK(d);
#ifdef SHOW_CONVERSION_COUNTS
- ++created;
+ ++created;
#endif
#ifdef SHOW_TRACK_COUNT
- if (_PyObject_GC_IS_TRACKED(d))
- count_tracked++;
- else
- count_untracked++;
+ if (_PyObject_GC_IS_TRACKED(d))
+ count_tracked++;
+ else
+ count_untracked++;
#endif
- }
- return self;
+ }
+ return self;
}
static int
dict_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- return dict_update_common(self, args, kwds, "dict");
+ return dict_update_common(self, args, kwds, "dict");
}
static PyObject *
dict_iter(PyDictObject *dict)
{
- return dictiter_new(dict, &PyDictIterKey_Type);
+ return dictiter_new(dict, &PyDictIterKey_Type);
}
PyDoc_STRVAR(dictionary_doc,
@@ -2111,46 +2111,46 @@ PyDoc_STRVAR(dictionary_doc,
" in the keyword argument list. For example: dict(one=1, two=2)");
PyTypeObject PyDict_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict",
- sizeof(PyDictObject),
- 0,
- (destructor)dict_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)dict_repr, /* tp_repr */
- 0, /* tp_as_number */
- &dict_as_sequence, /* tp_as_sequence */
- &dict_as_mapping, /* tp_as_mapping */
- (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
- dictionary_doc, /* tp_doc */
- dict_traverse, /* tp_traverse */
- dict_tp_clear, /* tp_clear */
- dict_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)dict_iter, /* tp_iter */
- 0, /* tp_iternext */
- mapp_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- dict_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- dict_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict",
+ sizeof(PyDictObject),
+ 0,
+ (destructor)dict_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)dict_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &dict_as_sequence, /* tp_as_sequence */
+ &dict_as_mapping, /* tp_as_mapping */
+ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
+ dictionary_doc, /* tp_doc */
+ dict_traverse, /* tp_traverse */
+ dict_tp_clear, /* tp_clear */
+ dict_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ (getiterfunc)dict_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ mapp_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ dict_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ dict_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* For backward compatibility with old dictionary interface */
@@ -2158,340 +2158,340 @@ PyTypeObject PyDict_Type = {
PyObject *
PyDict_GetItemString(PyObject *v, const char *key)
{
- PyObject *kv, *rv;
- kv = PyUnicode_FromString(key);
- if (kv == NULL)
- return NULL;
- rv = PyDict_GetItem(v, kv);
- Py_DECREF(kv);
- return rv;
+ PyObject *kv, *rv;
+ kv = PyUnicode_FromString(key);
+ if (kv == NULL)
+ return NULL;
+ rv = PyDict_GetItem(v, kv);
+ Py_DECREF(kv);
+ return rv;
}
int
PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
{
- PyObject *kv;
- int err;
- kv = PyUnicode_FromString(key);
- if (kv == NULL)
- return -1;
- PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
- err = PyDict_SetItem(v, kv, item);
- Py_DECREF(kv);
- return err;
+ PyObject *kv;
+ int err;
+ kv = PyUnicode_FromString(key);
+ if (kv == NULL)
+ return -1;
+ PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
+ err = PyDict_SetItem(v, kv, item);
+ Py_DECREF(kv);
+ return err;
}
int
PyDict_DelItemString(PyObject *v, const char *key)
{
- PyObject *kv;
- int err;
- kv = PyUnicode_FromString(key);
- if (kv == NULL)
- return -1;
- err = PyDict_DelItem(v, kv);
- Py_DECREF(kv);
- return err;
+ PyObject *kv;
+ int err;
+ kv = PyUnicode_FromString(key);
+ if (kv == NULL)
+ return -1;
+ err = PyDict_DelItem(v, kv);
+ Py_DECREF(kv);
+ return err;
}
/* Dictionary iterator types */
typedef struct {
- PyObject_HEAD
- PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
- Py_ssize_t di_used;
- Py_ssize_t di_pos;
- PyObject* di_result; /* reusable result tuple for iteritems */
- Py_ssize_t len;
+ PyObject_HEAD
+ PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
+ Py_ssize_t di_used;
+ Py_ssize_t di_pos;
+ PyObject* di_result; /* reusable result tuple for iteritems */
+ Py_ssize_t len;
} dictiterobject;
static PyObject *
dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
{
- dictiterobject *di;
- di = PyObject_GC_New(dictiterobject, itertype);
- if (di == NULL)
- return NULL;
- Py_INCREF(dict);
- di->di_dict = dict;
- di->di_used = dict->ma_used;
- di->di_pos = 0;
- di->len = dict->ma_used;
- if (itertype == &PyDictIterItem_Type) {
- di->di_result = PyTuple_Pack(2, Py_None, Py_None);
- if (di->di_result == NULL) {
- Py_DECREF(di);
- return NULL;
- }
- }
- else
- di->di_result = NULL;
- _PyObject_GC_TRACK(di);
- return (PyObject *)di;
+ dictiterobject *di;
+ di = PyObject_GC_New(dictiterobject, itertype);
+ if (di == NULL)
+ return NULL;
+ Py_INCREF(dict);
+ di->di_dict = dict;
+ di->di_used = dict->ma_used;
+ di->di_pos = 0;
+ di->len = dict->ma_used;
+ if (itertype == &PyDictIterItem_Type) {
+ di->di_result = PyTuple_Pack(2, Py_None, Py_None);
+ if (di->di_result == NULL) {
+ Py_DECREF(di);
+ return NULL;
+ }
+ }
+ else
+ di->di_result = NULL;
+ _PyObject_GC_TRACK(di);
+ return (PyObject *)di;
}
static void
dictiter_dealloc(dictiterobject *di)
{
- Py_XDECREF(di->di_dict);
- Py_XDECREF(di->di_result);
- PyObject_GC_Del(di);
+ Py_XDECREF(di->di_dict);
+ Py_XDECREF(di->di_result);
+ PyObject_GC_Del(di);
}
static int
dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
{
- Py_VISIT(di->di_dict);
- Py_VISIT(di->di_result);
- return 0;
+ Py_VISIT(di->di_dict);
+ Py_VISIT(di->di_result);
+ return 0;
}
static PyObject *
dictiter_len(dictiterobject *di)
{
- Py_ssize_t len = 0;
- if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
- len = di->len;
- return PyLong_FromSize_t(len);
+ Py_ssize_t len = 0;
+ if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
+ len = di->len;
+ return PyLong_FromSize_t(len);
}
PyDoc_STRVAR(length_hint_doc,
"Private method returning an estimate of len(list(it)).");
static PyMethodDef dictiter_methods[] = {
- {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
- length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
+ length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
static PyObject *dictiter_iternextkey(dictiterobject *di)
{
- PyObject *key;
- register Py_ssize_t i, mask;
- register PyDictEntry *ep;
- PyDictObject *d = di->di_dict;
-
- if (d == NULL)
- return NULL;
- assert (PyDict_Check(d));
-
- if (di->di_used != d->ma_used) {
- PyErr_SetString(PyExc_RuntimeError,
- "dictionary changed size during iteration");
- di->di_used = -1; /* Make this state sticky */
- return NULL;
- }
-
- i = di->di_pos;
- if (i < 0)
- goto fail;
- ep = d->ma_table;
- mask = d->ma_mask;
- while (i <= mask && ep[i].me_value == NULL)
- i++;
- di->di_pos = i+1;
- if (i > mask)
- goto fail;
- di->len--;
- key = ep[i].me_key;
- Py_INCREF(key);
- return key;
+ PyObject *key;
+ register Py_ssize_t i, mask;
+ register PyDictEntry *ep;
+ PyDictObject *d = di->di_dict;
+
+ if (d == NULL)
+ return NULL;
+ assert (PyDict_Check(d));
+
+ if (di->di_used != d->ma_used) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dictionary changed size during iteration");
+ di->di_used = -1; /* Make this state sticky */
+ return NULL;
+ }
+
+ i = di->di_pos;
+ if (i < 0)
+ goto fail;
+ ep = d->ma_table;
+ mask = d->ma_mask;
+ while (i <= mask && ep[i].me_value == NULL)
+ i++;
+ di->di_pos = i+1;
+ if (i > mask)
+ goto fail;
+ di->len--;
+ key = ep[i].me_key;
+ Py_INCREF(key);
+ return key;
fail:
- Py_DECREF(d);
- di->di_dict = NULL;
- return NULL;
+ Py_DECREF(d);
+ di->di_dict = NULL;
+ return NULL;
}
PyTypeObject PyDictIterKey_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_keyiterator", /* tp_name */
- sizeof(dictiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dictiter_iternextkey, /* tp_iternext */
- dictiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_keyiterator", /* tp_name */
+ sizeof(dictiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dictiter_iternextkey, /* tp_iternext */
+ dictiter_methods, /* tp_methods */
+ 0,
};
static PyObject *dictiter_iternextvalue(dictiterobject *di)
{
- PyObject *value;
- register Py_ssize_t i, mask;
- register PyDictEntry *ep;
- PyDictObject *d = di->di_dict;
-
- if (d == NULL)
- return NULL;
- assert (PyDict_Check(d));
-
- if (di->di_used != d->ma_used) {
- PyErr_SetString(PyExc_RuntimeError,
- "dictionary changed size during iteration");
- di->di_used = -1; /* Make this state sticky */
- return NULL;
- }
-
- i = di->di_pos;
- mask = d->ma_mask;
- if (i < 0 || i > mask)
- goto fail;
- ep = d->ma_table;
- while ((value=ep[i].me_value) == NULL) {
- i++;
- if (i > mask)
- goto fail;
- }
- di->di_pos = i+1;
- di->len--;
- Py_INCREF(value);
- return value;
+ PyObject *value;
+ register Py_ssize_t i, mask;
+ register PyDictEntry *ep;
+ PyDictObject *d = di->di_dict;
+
+ if (d == NULL)
+ return NULL;
+ assert (PyDict_Check(d));
+
+ if (di->di_used != d->ma_used) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dictionary changed size during iteration");
+ di->di_used = -1; /* Make this state sticky */
+ return NULL;
+ }
+
+ i = di->di_pos;
+ mask = d->ma_mask;
+ if (i < 0 || i > mask)
+ goto fail;
+ ep = d->ma_table;
+ while ((value=ep[i].me_value) == NULL) {
+ i++;
+ if (i > mask)
+ goto fail;
+ }
+ di->di_pos = i+1;
+ di->len--;
+ Py_INCREF(value);
+ return value;
fail:
- Py_DECREF(d);
- di->di_dict = NULL;
- return NULL;
+ Py_DECREF(d);
+ di->di_dict = NULL;
+ return NULL;
}
PyTypeObject PyDictIterValue_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_valueiterator", /* tp_name */
- sizeof(dictiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
- dictiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_valueiterator", /* tp_name */
+ sizeof(dictiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
+ dictiter_methods, /* tp_methods */
+ 0,
};
static PyObject *dictiter_iternextitem(dictiterobject *di)
{
- PyObject *key, *value, *result = di->di_result;
- register Py_ssize_t i, mask;
- register PyDictEntry *ep;
- PyDictObject *d = di->di_dict;
-
- if (d == NULL)
- return NULL;
- assert (PyDict_Check(d));
-
- if (di->di_used != d->ma_used) {
- PyErr_SetString(PyExc_RuntimeError,
- "dictionary changed size during iteration");
- di->di_used = -1; /* Make this state sticky */
- return NULL;
- }
-
- i = di->di_pos;
- if (i < 0)
- goto fail;
- ep = d->ma_table;
- mask = d->ma_mask;
- while (i <= mask && ep[i].me_value == NULL)
- i++;
- di->di_pos = i+1;
- if (i > mask)
- goto fail;
-
- if (result->ob_refcnt == 1) {
- Py_INCREF(result);
- Py_DECREF(PyTuple_GET_ITEM(result, 0));
- Py_DECREF(PyTuple_GET_ITEM(result, 1));
- } else {
- result = PyTuple_New(2);
- if (result == NULL)
- return NULL;
- }
- di->len--;
- key = ep[i].me_key;
- value = ep[i].me_value;
- Py_INCREF(key);
- Py_INCREF(value);
- PyTuple_SET_ITEM(result, 0, key);
- PyTuple_SET_ITEM(result, 1, value);
- return result;
+ PyObject *key, *value, *result = di->di_result;
+ register Py_ssize_t i, mask;
+ register PyDictEntry *ep;
+ PyDictObject *d = di->di_dict;
+
+ if (d == NULL)
+ return NULL;
+ assert (PyDict_Check(d));
+
+ if (di->di_used != d->ma_used) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dictionary changed size during iteration");
+ di->di_used = -1; /* Make this state sticky */
+ return NULL;
+ }
+
+ i = di->di_pos;
+ if (i < 0)
+ goto fail;
+ ep = d->ma_table;
+ mask = d->ma_mask;
+ while (i <= mask && ep[i].me_value == NULL)
+ i++;
+ di->di_pos = i+1;
+ if (i > mask)
+ goto fail;
+
+ if (result->ob_refcnt == 1) {
+ Py_INCREF(result);
+ Py_DECREF(PyTuple_GET_ITEM(result, 0));
+ Py_DECREF(PyTuple_GET_ITEM(result, 1));
+ } else {
+ result = PyTuple_New(2);
+ if (result == NULL)
+ return NULL;
+ }
+ di->len--;
+ key = ep[i].me_key;
+ value = ep[i].me_value;
+ Py_INCREF(key);
+ Py_INCREF(value);
+ PyTuple_SET_ITEM(result, 0, key);
+ PyTuple_SET_ITEM(result, 1, value);
+ return result;
fail:
- Py_DECREF(d);
- di->di_dict = NULL;
- return NULL;
+ Py_DECREF(d);
+ di->di_dict = NULL;
+ return NULL;
}
PyTypeObject PyDictIterItem_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_itemiterator", /* tp_name */
- sizeof(dictiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)dictiter_iternextitem, /* tp_iternext */
- dictiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_itemiterator", /* tp_name */
+ sizeof(dictiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)dictiter_iternextitem, /* tp_iternext */
+ dictiter_methods, /* tp_methods */
+ 0,
};
@@ -2502,56 +2502,56 @@ PyTypeObject PyDictIterItem_Type = {
/* The instance lay-out is the same for all three; but the type differs. */
typedef struct {
- PyObject_HEAD
- PyDictObject *dv_dict;
+ PyObject_HEAD
+ PyDictObject *dv_dict;
} dictviewobject;
static void
dictview_dealloc(dictviewobject *dv)
{
- Py_XDECREF(dv->dv_dict);
- PyObject_GC_Del(dv);
+ Py_XDECREF(dv->dv_dict);
+ PyObject_GC_Del(dv);
}
static int
dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
{
- Py_VISIT(dv->dv_dict);
- return 0;
+ Py_VISIT(dv->dv_dict);
+ return 0;
}
static Py_ssize_t
dictview_len(dictviewobject *dv)
{
- Py_ssize_t len = 0;
- if (dv->dv_dict != NULL)
- len = dv->dv_dict->ma_used;
- return len;
+ Py_ssize_t len = 0;
+ if (dv->dv_dict != NULL)
+ len = dv->dv_dict->ma_used;
+ return len;
}
static PyObject *
dictview_new(PyObject *dict, PyTypeObject *type)
{
- dictviewobject *dv;
- if (dict == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (!PyDict_Check(dict)) {
- /* XXX Get rid of this restriction later */
- PyErr_Format(PyExc_TypeError,
- "%s() requires a dict argument, not '%s'",
- type->tp_name, dict->ob_type->tp_name);
- return NULL;
- }
- dv = PyObject_GC_New(dictviewobject, type);
- if (dv == NULL)
- return NULL;
- Py_INCREF(dict);
- dv->dv_dict = (PyDictObject *)dict;
- _PyObject_GC_TRACK(dv);
- return (PyObject *)dv;
+ dictviewobject *dv;
+ if (dict == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (!PyDict_Check(dict)) {
+ /* XXX Get rid of this restriction later */
+ PyErr_Format(PyExc_TypeError,
+ "%s() requires a dict argument, not '%s'",
+ type->tp_name, dict->ob_type->tp_name);
+ return NULL;
+ }
+ dv = PyObject_GC_New(dictviewobject, type);
+ if (dv == NULL)
+ return NULL;
+ Py_INCREF(dict);
+ dv->dv_dict = (PyDictObject *)dict;
+ _PyObject_GC_TRACK(dv);
+ return (PyObject *)dv;
}
/* TODO(guido): The views objects are not complete:
@@ -2567,102 +2567,102 @@ dictview_new(PyObject *dict, PyTypeObject *type)
static int
all_contained_in(PyObject *self, PyObject *other)
{
- PyObject *iter = PyObject_GetIter(self);
- int ok = 1;
-
- if (iter == NULL)
- return -1;
- for (;;) {
- PyObject *next = PyIter_Next(iter);
- if (next == NULL) {
- if (PyErr_Occurred())
- ok = -1;
- break;
- }
- ok = PySequence_Contains(other, next);
- Py_DECREF(next);
- if (ok <= 0)
- break;
- }
- Py_DECREF(iter);
- return ok;
+ PyObject *iter = PyObject_GetIter(self);
+ int ok = 1;
+
+ if (iter == NULL)
+ return -1;
+ for (;;) {
+ PyObject *next = PyIter_Next(iter);
+ if (next == NULL) {
+ if (PyErr_Occurred())
+ ok = -1;
+ break;
+ }
+ ok = PySequence_Contains(other, next);
+ Py_DECREF(next);
+ if (ok <= 0)
+ break;
+ }
+ Py_DECREF(iter);
+ return ok;
}
static PyObject *
dictview_richcompare(PyObject *self, PyObject *other, int op)
{
- Py_ssize_t len_self, len_other;
- int ok;
- PyObject *result;
-
- assert(self != NULL);
- assert(PyDictViewSet_Check(self));
- assert(other != NULL);
-
- if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- len_self = PyObject_Size(self);
- if (len_self < 0)
- return NULL;
- len_other = PyObject_Size(other);
- if (len_other < 0)
- return NULL;
-
- ok = 0;
- switch(op) {
-
- case Py_NE:
- case Py_EQ:
- if (len_self == len_other)
- ok = all_contained_in(self, other);
- if (op == Py_NE && ok >= 0)
- ok = !ok;
- break;
-
- case Py_LT:
- if (len_self < len_other)
- ok = all_contained_in(self, other);
- break;
-
- case Py_LE:
- if (len_self <= len_other)
- ok = all_contained_in(self, other);
- break;
-
- case Py_GT:
- if (len_self > len_other)
- ok = all_contained_in(other, self);
- break;
-
- case Py_GE:
- if (len_self >= len_other)
- ok = all_contained_in(other, self);
- break;
-
- }
- if (ok < 0)
- return NULL;
- result = ok ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
+ Py_ssize_t len_self, len_other;
+ int ok;
+ PyObject *result;
+
+ assert(self != NULL);
+ assert(PyDictViewSet_Check(self));
+ assert(other != NULL);
+
+ if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ len_self = PyObject_Size(self);
+ if (len_self < 0)
+ return NULL;
+ len_other = PyObject_Size(other);
+ if (len_other < 0)
+ return NULL;
+
+ ok = 0;
+ switch(op) {
+
+ case Py_NE:
+ case Py_EQ:
+ if (len_self == len_other)
+ ok = all_contained_in(self, other);
+ if (op == Py_NE && ok >= 0)
+ ok = !ok;
+ break;
+
+ case Py_LT:
+ if (len_self < len_other)
+ ok = all_contained_in(self, other);
+ break;
+
+ case Py_LE:
+ if (len_self <= len_other)
+ ok = all_contained_in(self, other);
+ break;
+
+ case Py_GT:
+ if (len_self > len_other)
+ ok = all_contained_in(other, self);
+ break;
+
+ case Py_GE:
+ if (len_self >= len_other)
+ ok = all_contained_in(other, self);
+ break;
+
+ }
+ if (ok < 0)
+ return NULL;
+ result = ok ? Py_True : Py_False;
+ Py_INCREF(result);
+ return result;
}
static PyObject *
dictview_repr(dictviewobject *dv)
{
- PyObject *seq;
- PyObject *result;
-
- seq = PySequence_List((PyObject *)dv);
- if (seq == NULL)
- return NULL;
+ PyObject *seq;
+ PyObject *result;
+
+ seq = PySequence_List((PyObject *)dv);
+ if (seq == NULL)
+ return NULL;
- result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
- Py_DECREF(seq);
- return result;
+ result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
+ Py_DECREF(seq);
+ return result;
}
/*** dict_keys ***/
@@ -2670,164 +2670,164 @@ dictview_repr(dictviewobject *dv)
static PyObject *
dictkeys_iter(dictviewobject *dv)
{
- if (dv->dv_dict == NULL) {
- Py_RETURN_NONE;
- }
- return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
+ if (dv->dv_dict == NULL) {
+ Py_RETURN_NONE;
+ }
+ return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
}
static int
dictkeys_contains(dictviewobject *dv, PyObject *obj)
{
- if (dv->dv_dict == NULL)
- return 0;
- return PyDict_Contains((PyObject *)dv->dv_dict, obj);
+ if (dv->dv_dict == NULL)
+ return 0;
+ return PyDict_Contains((PyObject *)dv->dv_dict, obj);
}
static PySequenceMethods dictkeys_as_sequence = {
- (lenfunc)dictview_len, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)dictkeys_contains, /* sq_contains */
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)dictkeys_contains, /* sq_contains */
};
static PyObject*
dictviews_sub(PyObject* self, PyObject *other)
{
- PyObject *result = PySet_New(self);
- PyObject *tmp;
- if (result == NULL)
- return NULL;
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
- tmp = PyObject_CallMethod(result, "difference_update", "O", other);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
+ tmp = PyObject_CallMethod(result, "difference_update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
- Py_DECREF(tmp);
- return result;
+ Py_DECREF(tmp);
+ return result;
}
static PyObject*
dictviews_and(PyObject* self, PyObject *other)
{
- PyObject *result = PySet_New(self);
- PyObject *tmp;
- if (result == NULL)
- return NULL;
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
- tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
+ tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
- Py_DECREF(tmp);
- return result;
+ Py_DECREF(tmp);
+ return result;
}
static PyObject*
dictviews_or(PyObject* self, PyObject *other)
{
- PyObject *result = PySet_New(self);
- PyObject *tmp;
- if (result == NULL)
- return NULL;
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
- tmp = PyObject_CallMethod(result, "update", "O", other);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
+ tmp = PyObject_CallMethod(result, "update", "O", other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
- Py_DECREF(tmp);
- return result;
+ Py_DECREF(tmp);
+ return result;
}
static PyObject*
dictviews_xor(PyObject* self, PyObject *other)
{
- PyObject *result = PySet_New(self);
- PyObject *tmp;
- if (result == NULL)
- return NULL;
+ PyObject *result = PySet_New(self);
+ PyObject *tmp;
+ if (result == NULL)
+ return NULL;
- tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
- other);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
+ tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
+ other);
+ if (tmp == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
- Py_DECREF(tmp);
- return result;
+ Py_DECREF(tmp);
+ return result;
}
static PyNumberMethods dictviews_as_number = {
- 0, /*nb_add*/
- (binaryfunc)dictviews_sub, /*nb_subtract*/
- 0, /*nb_multiply*/
- 0, /*nb_remainder*/
- 0, /*nb_divmod*/
- 0, /*nb_power*/
- 0, /*nb_negative*/
- 0, /*nb_positive*/
- 0, /*nb_absolute*/
- 0, /*nb_bool*/
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- (binaryfunc)dictviews_and, /*nb_and*/
- (binaryfunc)dictviews_xor, /*nb_xor*/
- (binaryfunc)dictviews_or, /*nb_or*/
+ 0, /*nb_add*/
+ (binaryfunc)dictviews_sub, /*nb_subtract*/
+ 0, /*nb_multiply*/
+ 0, /*nb_remainder*/
+ 0, /*nb_divmod*/
+ 0, /*nb_power*/
+ 0, /*nb_negative*/
+ 0, /*nb_positive*/
+ 0, /*nb_absolute*/
+ 0, /*nb_bool*/
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ (binaryfunc)dictviews_and, /*nb_and*/
+ (binaryfunc)dictviews_xor, /*nb_xor*/
+ (binaryfunc)dictviews_or, /*nb_or*/
};
static PyMethodDef dictkeys_methods[] = {
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyDictKeys_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_keys", /* tp_name */
- sizeof(dictviewobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictview_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)dictview_repr, /* tp_repr */
- &dictviews_as_number, /* tp_as_number */
- &dictkeys_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictview_traverse, /* tp_traverse */
- 0, /* tp_clear */
- dictview_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)dictkeys_iter, /* tp_iter */
- 0, /* tp_iternext */
- dictkeys_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_keys", /* tp_name */
+ sizeof(dictviewobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictview_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)dictview_repr, /* tp_repr */
+ &dictviews_as_number, /* tp_as_number */
+ &dictkeys_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictview_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ dictview_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ (getiterfunc)dictkeys_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ dictkeys_methods, /* tp_methods */
+ 0,
};
static PyObject *
dictkeys_new(PyObject *dict)
{
- return dictview_new(dict, &PyDictKeys_Type);
+ return dictview_new(dict, &PyDictKeys_Type);
}
/*** dict_items ***/
@@ -2835,83 +2835,83 @@ dictkeys_new(PyObject *dict)
static PyObject *
dictitems_iter(dictviewobject *dv)
{
- if (dv->dv_dict == NULL) {
- Py_RETURN_NONE;
- }
- return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
+ if (dv->dv_dict == NULL) {
+ Py_RETURN_NONE;
+ }
+ return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
}
static int
dictitems_contains(dictviewobject *dv, PyObject *obj)
{
- PyObject *key, *value, *found;
- if (dv->dv_dict == NULL)
- return 0;
- if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
- return 0;
- key = PyTuple_GET_ITEM(obj, 0);
- value = PyTuple_GET_ITEM(obj, 1);
- found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
- if (found == NULL) {
- if (PyErr_Occurred())
- return -1;
- return 0;
- }
- return PyObject_RichCompareBool(value, found, Py_EQ);
+ PyObject *key, *value, *found;
+ if (dv->dv_dict == NULL)
+ return 0;
+ if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
+ return 0;
+ key = PyTuple_GET_ITEM(obj, 0);
+ value = PyTuple_GET_ITEM(obj, 1);
+ found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
+ if (found == NULL) {
+ if (PyErr_Occurred())
+ return -1;
+ return 0;
+ }
+ return PyObject_RichCompareBool(value, found, Py_EQ);
}
static PySequenceMethods dictitems_as_sequence = {
- (lenfunc)dictview_len, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)dictitems_contains, /* sq_contains */
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)dictitems_contains, /* sq_contains */
};
static PyMethodDef dictitems_methods[] = {
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyDictItems_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_items", /* tp_name */
- sizeof(dictviewobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictview_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)dictview_repr, /* tp_repr */
- &dictviews_as_number, /* tp_as_number */
- &dictitems_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictview_traverse, /* tp_traverse */
- 0, /* tp_clear */
- dictview_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)dictitems_iter, /* tp_iter */
- 0, /* tp_iternext */
- dictitems_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_items", /* tp_name */
+ sizeof(dictviewobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictview_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)dictview_repr, /* tp_repr */
+ &dictviews_as_number, /* tp_as_number */
+ &dictitems_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictview_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ dictview_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ (getiterfunc)dictitems_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ dictitems_methods, /* tp_methods */
+ 0,
};
static PyObject *
dictitems_new(PyObject *dict)
{
- return dictview_new(dict, &PyDictItems_Type);
+ return dictview_new(dict, &PyDictItems_Type);
}
/*** dict_values ***/
@@ -2919,62 +2919,62 @@ dictitems_new(PyObject *dict)
static PyObject *
dictvalues_iter(dictviewobject *dv)
{
- if (dv->dv_dict == NULL) {
- Py_RETURN_NONE;
- }
- return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
+ if (dv->dv_dict == NULL) {
+ Py_RETURN_NONE;
+ }
+ return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
}
static PySequenceMethods dictvalues_as_sequence = {
- (lenfunc)dictview_len, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)0, /* sq_contains */
+ (lenfunc)dictview_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)0, /* sq_contains */
};
static PyMethodDef dictvalues_methods[] = {
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyDictValues_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "dict_values", /* tp_name */
- sizeof(dictviewobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)dictview_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)dictview_repr, /* tp_repr */
- 0, /* tp_as_number */
- &dictvalues_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)dictview_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)dictvalues_iter, /* tp_iter */
- 0, /* tp_iternext */
- dictvalues_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "dict_values", /* tp_name */
+ sizeof(dictviewobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)dictview_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)dictview_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &dictvalues_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)dictview_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ (getiterfunc)dictvalues_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ dictvalues_methods, /* tp_methods */
+ 0,
};
static PyObject *
dictvalues_new(PyObject *dict)
{
- return dictview_new(dict, &PyDictValues_Type);
+ return dictview_new(dict, &PyDictValues_Type);
}
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 3d9bfad..b579f96 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -3,159 +3,159 @@
#include "Python.h"
typedef struct {
- PyObject_HEAD
- Py_ssize_t en_index; /* current index of enumeration */
- PyObject* en_sit; /* secondary iterator of enumeration */
- PyObject* en_result; /* result tuple */
- PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */
+ PyObject_HEAD
+ Py_ssize_t en_index; /* current index of enumeration */
+ PyObject* en_sit; /* secondary iterator of enumeration */
+ PyObject* en_result; /* result tuple */
+ PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */
} enumobject;
static PyObject *
enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- enumobject *en;
- PyObject *seq = NULL;
- PyObject *start = NULL;
- static char *kwlist[] = {"iterable", "start", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
- &seq, &start))
- return NULL;
-
- en = (enumobject *)type->tp_alloc(type, 0);
- if (en == NULL)
- return NULL;
- if (start != NULL) {
- start = PyNumber_Index(start);
- if (start == NULL) {
- Py_DECREF(en);
- return NULL;
- }
- assert(PyLong_Check(start));
- en->en_index = PyLong_AsSsize_t(start);
- if (en->en_index == -1 && PyErr_Occurred()) {
- PyErr_Clear();
- en->en_index = PY_SSIZE_T_MAX;
- en->en_longindex = start;
- } else {
- en->en_longindex = NULL;
- Py_DECREF(start);
- }
- } else {
- en->en_index = 0;
- en->en_longindex = NULL;
- }
- en->en_sit = PyObject_GetIter(seq);
- if (en->en_sit == NULL) {
- Py_DECREF(en);
- return NULL;
- }
- en->en_result = PyTuple_Pack(2, Py_None, Py_None);
- if (en->en_result == NULL) {
- Py_DECREF(en);
- return NULL;
- }
- return (PyObject *)en;
+ enumobject *en;
+ PyObject *seq = NULL;
+ PyObject *start = NULL;
+ static char *kwlist[] = {"iterable", "start", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
+ &seq, &start))
+ return NULL;
+
+ en = (enumobject *)type->tp_alloc(type, 0);
+ if (en == NULL)
+ return NULL;
+ if (start != NULL) {
+ start = PyNumber_Index(start);
+ if (start == NULL) {
+ Py_DECREF(en);
+ return NULL;
+ }
+ assert(PyLong_Check(start));
+ en->en_index = PyLong_AsSsize_t(start);
+ if (en->en_index == -1 && PyErr_Occurred()) {
+ PyErr_Clear();
+ en->en_index = PY_SSIZE_T_MAX;
+ en->en_longindex = start;
+ } else {
+ en->en_longindex = NULL;
+ Py_DECREF(start);
+ }
+ } else {
+ en->en_index = 0;
+ en->en_longindex = NULL;
+ }
+ en->en_sit = PyObject_GetIter(seq);
+ if (en->en_sit == NULL) {
+ Py_DECREF(en);
+ return NULL;
+ }
+ en->en_result = PyTuple_Pack(2, Py_None, Py_None);
+ if (en->en_result == NULL) {
+ Py_DECREF(en);
+ return NULL;
+ }
+ return (PyObject *)en;
}
static void
enum_dealloc(enumobject *en)
{
- PyObject_GC_UnTrack(en);
- Py_XDECREF(en->en_sit);
- Py_XDECREF(en->en_result);
- Py_XDECREF(en->en_longindex);
- Py_TYPE(en)->tp_free(en);
+ PyObject_GC_UnTrack(en);
+ Py_XDECREF(en->en_sit);
+ Py_XDECREF(en->en_result);
+ Py_XDECREF(en->en_longindex);
+ Py_TYPE(en)->tp_free(en);
}
static int
enum_traverse(enumobject *en, visitproc visit, void *arg)
{
- Py_VISIT(en->en_sit);
- Py_VISIT(en->en_result);
- Py_VISIT(en->en_longindex);
- return 0;
+ Py_VISIT(en->en_sit);
+ Py_VISIT(en->en_result);
+ Py_VISIT(en->en_longindex);
+ return 0;
}
static PyObject *
enum_next_long(enumobject *en, PyObject* next_item)
{
- static PyObject *one = NULL;
- PyObject *result = en->en_result;
- PyObject *next_index;
- PyObject *stepped_up;
-
- if (en->en_longindex == NULL) {
- en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
- if (en->en_longindex == NULL)
- return NULL;
- }
- if (one == NULL) {
- one = PyLong_FromLong(1);
- if (one == NULL)
- return NULL;
- }
- next_index = en->en_longindex;
- assert(next_index != NULL);
- stepped_up = PyNumber_Add(next_index, one);
- if (stepped_up == NULL)
- return NULL;
- en->en_longindex = stepped_up;
-
- if (result->ob_refcnt == 1) {
- Py_INCREF(result);
- Py_DECREF(PyTuple_GET_ITEM(result, 0));
- Py_DECREF(PyTuple_GET_ITEM(result, 1));
- } else {
- result = PyTuple_New(2);
- if (result == NULL) {
- Py_DECREF(next_index);
- Py_DECREF(next_item);
- return NULL;
- }
- }
- PyTuple_SET_ITEM(result, 0, next_index);
- PyTuple_SET_ITEM(result, 1, next_item);
- return result;
+ static PyObject *one = NULL;
+ PyObject *result = en->en_result;
+ PyObject *next_index;
+ PyObject *stepped_up;
+
+ if (en->en_longindex == NULL) {
+ en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
+ if (en->en_longindex == NULL)
+ return NULL;
+ }
+ if (one == NULL) {
+ one = PyLong_FromLong(1);
+ if (one == NULL)
+ return NULL;
+ }
+ next_index = en->en_longindex;
+ assert(next_index != NULL);
+ stepped_up = PyNumber_Add(next_index, one);
+ if (stepped_up == NULL)
+ return NULL;
+ en->en_longindex = stepped_up;
+
+ if (result->ob_refcnt == 1) {
+ Py_INCREF(result);
+ Py_DECREF(PyTuple_GET_ITEM(result, 0));
+ Py_DECREF(PyTuple_GET_ITEM(result, 1));
+ } else {
+ result = PyTuple_New(2);
+ if (result == NULL) {
+ Py_DECREF(next_index);
+ Py_DECREF(next_item);
+ return NULL;
+ }
+ }
+ PyTuple_SET_ITEM(result, 0, next_index);
+ PyTuple_SET_ITEM(result, 1, next_item);
+ return result;
}
static PyObject *
enum_next(enumobject *en)
{
- PyObject *next_index;
- PyObject *next_item;
- PyObject *result = en->en_result;
- PyObject *it = en->en_sit;
-
- next_item = (*Py_TYPE(it)->tp_iternext)(it);
- if (next_item == NULL)
- return NULL;
-
- if (en->en_index == PY_SSIZE_T_MAX)
- return enum_next_long(en, next_item);
-
- next_index = PyLong_FromSsize_t(en->en_index);
- if (next_index == NULL) {
- Py_DECREF(next_item);
- return NULL;
- }
- en->en_index++;
-
- if (result->ob_refcnt == 1) {
- Py_INCREF(result);
- Py_DECREF(PyTuple_GET_ITEM(result, 0));
- Py_DECREF(PyTuple_GET_ITEM(result, 1));
- } else {
- result = PyTuple_New(2);
- if (result == NULL) {
- Py_DECREF(next_index);
- Py_DECREF(next_item);
- return NULL;
- }
- }
- PyTuple_SET_ITEM(result, 0, next_index);
- PyTuple_SET_ITEM(result, 1, next_item);
- return result;
+ PyObject *next_index;
+ PyObject *next_item;
+ PyObject *result = en->en_result;
+ PyObject *it = en->en_sit;
+
+ next_item = (*Py_TYPE(it)->tp_iternext)(it);
+ if (next_item == NULL)
+ return NULL;
+
+ if (en->en_index == PY_SSIZE_T_MAX)
+ return enum_next_long(en, next_item);
+
+ next_index = PyLong_FromSsize_t(en->en_index);
+ if (next_index == NULL) {
+ Py_DECREF(next_item);
+ return NULL;
+ }
+ en->en_index++;
+
+ if (result->ob_refcnt == 1) {
+ Py_INCREF(result);
+ Py_DECREF(PyTuple_GET_ITEM(result, 0));
+ Py_DECREF(PyTuple_GET_ITEM(result, 1));
+ } else {
+ result = PyTuple_New(2);
+ if (result == NULL) {
+ Py_DECREF(next_index);
+ Py_DECREF(next_item);
+ return NULL;
+ }
+ }
+ PyTuple_SET_ITEM(result, 0, next_index);
+ PyTuple_SET_ITEM(result, 1, next_item);
+ return result;
}
PyDoc_STRVAR(enum_doc,
@@ -167,134 +167,134 @@ PyDoc_STRVAR(enum_doc,
"for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
PyTypeObject PyEnum_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "enumerate", /* tp_name */
- sizeof(enumobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)enum_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- enum_doc, /* tp_doc */
- (traverseproc)enum_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)enum_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- enum_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "enumerate", /* tp_name */
+ sizeof(enumobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)enum_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ enum_doc, /* tp_doc */
+ (traverseproc)enum_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)enum_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ enum_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* Reversed Object ***************************************************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t index;
- PyObject* seq;
+ PyObject_HEAD
+ Py_ssize_t index;
+ PyObject* seq;
} reversedobject;
static PyObject *
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- Py_ssize_t n;
- PyObject *seq, *reversed_meth;
- static PyObject *reversed_cache = NULL;
- reversedobject *ro;
-
- if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
- return NULL;
-
- if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
- return NULL;
-
- reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
- if (reversed_meth != NULL) {
- PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
- Py_DECREF(reversed_meth);
- return res;
- }
- else if (PyErr_Occurred())
- return NULL;
-
- if (!PySequence_Check(seq)) {
- PyErr_SetString(PyExc_TypeError,
- "argument to reversed() must be a sequence");
- return NULL;
- }
-
- n = PySequence_Size(seq);
- if (n == -1)
- return NULL;
-
- ro = (reversedobject *)type->tp_alloc(type, 0);
- if (ro == NULL)
- return NULL;
-
- ro->index = n-1;
- Py_INCREF(seq);
- ro->seq = seq;
- return (PyObject *)ro;
+ Py_ssize_t n;
+ PyObject *seq, *reversed_meth;
+ static PyObject *reversed_cache = NULL;
+ reversedobject *ro;
+
+ if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
+ return NULL;
+
+ if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
+ return NULL;
+
+ reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
+ if (reversed_meth != NULL) {
+ PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
+ Py_DECREF(reversed_meth);
+ return res;
+ }
+ else if (PyErr_Occurred())
+ return NULL;
+
+ if (!PySequence_Check(seq)) {
+ PyErr_SetString(PyExc_TypeError,
+ "argument to reversed() must be a sequence");
+ return NULL;
+ }
+
+ n = PySequence_Size(seq);
+ if (n == -1)
+ return NULL;
+
+ ro = (reversedobject *)type->tp_alloc(type, 0);
+ if (ro == NULL)
+ return NULL;
+
+ ro->index = n-1;
+ Py_INCREF(seq);
+ ro->seq = seq;
+ return (PyObject *)ro;
}
static void
reversed_dealloc(reversedobject *ro)
{
- PyObject_GC_UnTrack(ro);
- Py_XDECREF(ro->seq);
- Py_TYPE(ro)->tp_free(ro);
+ PyObject_GC_UnTrack(ro);
+ Py_XDECREF(ro->seq);
+ Py_TYPE(ro)->tp_free(ro);
}
static int
reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
{
- Py_VISIT(ro->seq);
- return 0;
+ Py_VISIT(ro->seq);
+ return 0;
}
static PyObject *
reversed_next(reversedobject *ro)
{
- PyObject *item;
- Py_ssize_t index = ro->index;
-
- if (index >= 0) {
- item = PySequence_GetItem(ro->seq, index);
- if (item != NULL) {
- ro->index--;
- return item;
- }
- if (PyErr_ExceptionMatches(PyExc_IndexError) ||
- PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- }
- ro->index = -1;
- Py_CLEAR(ro->seq);
- return NULL;
+ PyObject *item;
+ Py_ssize_t index = ro->index;
+
+ if (index >= 0) {
+ item = PySequence_GetItem(ro->seq, index);
+ if (item != NULL) {
+ ro->index--;
+ return item;
+ }
+ if (PyErr_ExceptionMatches(PyExc_IndexError) ||
+ PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ }
+ ro->index = -1;
+ Py_CLEAR(ro->seq);
+ return NULL;
}
PyDoc_STRVAR(reversed_doc,
@@ -305,64 +305,64 @@ PyDoc_STRVAR(reversed_doc,
static PyObject *
reversed_len(reversedobject *ro)
{
- Py_ssize_t position, seqsize;
-
- if (ro->seq == NULL)
- return PyLong_FromLong(0);
- seqsize = PySequence_Size(ro->seq);
- if (seqsize == -1)
- return NULL;
- position = ro->index + 1;
- return PyLong_FromSsize_t((seqsize < position) ? 0 : position);
+ Py_ssize_t position, seqsize;
+
+ if (ro->seq == NULL)
+ return PyLong_FromLong(0);
+ seqsize = PySequence_Size(ro->seq);
+ if (seqsize == -1)
+ return NULL;
+ position = ro->index + 1;
+ return PyLong_FromSsize_t((seqsize < position) ? 0 : position);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef reversediter_methods[] = {
- {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyReversed_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "reversed", /* tp_name */
- sizeof(reversedobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)reversed_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- reversed_doc, /* tp_doc */
- (traverseproc)reversed_traverse,/* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)reversed_next, /* tp_iternext */
- reversediter_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- reversed_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "reversed", /* tp_name */
+ sizeof(reversedobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)reversed_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ reversed_doc, /* tp_doc */
+ (traverseproc)reversed_traverse,/* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)reversed_next, /* tp_iternext */
+ reversediter_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ reversed_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 57fe9d7..234922c 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -270,7 +270,7 @@ BaseException_set_context(PyObject *self, PyObject *arg) {
} else {
/* PyException_SetContext steals this reference */
Py_INCREF(arg);
- }
+ }
PyException_SetContext(self, arg);
return 0;
}
@@ -296,7 +296,7 @@ BaseException_set_cause(PyObject *self, PyObject *arg) {
} else {
/* PyException_SetCause steals this reference */
Py_INCREF(arg);
- }
+ }
PyException_SetCause(self, arg);
return 0;
}
@@ -379,7 +379,7 @@ static PyTypeObject _PyExc_BaseException = {
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*/
+ 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 */
@@ -990,7 +990,7 @@ SyntaxError_str(PySyntaxErrorObject *self)
lineno here */
if (self->filename && PyUnicode_Check(self->filename)) {
- filename = _PyUnicode_AsString(self->filename);
+ filename = _PyUnicode_AsString(self->filename);
}
have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
@@ -1001,7 +1001,7 @@ SyntaxError_str(PySyntaxErrorObject *self)
return PyUnicode_FromFormat("%S (%s, line %ld)",
self->msg ? self->msg : Py_None,
my_basename(filename),
- PyLong_AsLongAndOverflow(self->lineno, &overflow));
+ PyLong_AsLongAndOverflow(self->lineno, &overflow));
else if (filename)
return PyUnicode_FromFormat("%S (%s)",
self->msg ? self->msg : Py_None,
@@ -1997,25 +1997,25 @@ _PyExc_Init(void)
PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
if (!PyExc_RecursionErrorInst)
- Py_FatalError("Cannot pre-allocate RuntimeError instance for "
- "recursion errors");
+ Py_FatalError("Cannot pre-allocate RuntimeError instance for "
+ "recursion errors");
else {
- PyBaseExceptionObject *err_inst =
- (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
- PyObject *args_tuple;
- PyObject *exc_message;
- exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
- if (!exc_message)
- Py_FatalError("cannot allocate argument for RuntimeError "
- "pre-allocation");
- args_tuple = PyTuple_Pack(1, exc_message);
- if (!args_tuple)
- Py_FatalError("cannot allocate tuple for RuntimeError "
- "pre-allocation");
- Py_DECREF(exc_message);
- if (BaseException_init(err_inst, args_tuple, NULL))
- Py_FatalError("init of pre-allocated RuntimeError failed");
- Py_DECREF(args_tuple);
+ PyBaseExceptionObject *err_inst =
+ (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
+ PyObject *args_tuple;
+ PyObject *exc_message;
+ exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
+ if (!exc_message)
+ Py_FatalError("cannot allocate argument for RuntimeError "
+ "pre-allocation");
+ args_tuple = PyTuple_Pack(1, exc_message);
+ if (!args_tuple)
+ Py_FatalError("cannot allocate tuple for RuntimeError "
+ "pre-allocation");
+ Py_DECREF(exc_message);
+ if (BaseException_init(err_inst, args_tuple, NULL))
+ Py_FatalError("init of pre-allocated RuntimeError failed");
+ Py_DECREF(args_tuple);
}
Py_DECREF(bltinmod);
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 85e87d7..b36aa02 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -14,10 +14,10 @@
#endif
/* Newline flags */
-#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
-#define NEWLINE_CR 1 /* \r newline seen */
-#define NEWLINE_LF 2 /* \n newline seen */
-#define NEWLINE_CRLF 4 /* \r\n newline seen */
+#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
+#define NEWLINE_CR 1 /* \r newline seen */
+#define NEWLINE_LF 2 /* \n newline seen */
+#define NEWLINE_CRLF 4 /* \r\n newline seen */
#ifdef __cplusplus
extern "C" {
@@ -27,110 +27,110 @@ extern "C" {
PyObject *
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
- char *errors, char *newline, int closefd)
+ char *errors, char *newline, int closefd)
{
- PyObject *io, *stream, *nameobj = NULL;
-
- io = PyImport_ImportModule("io");
- if (io == NULL)
- return NULL;
- stream = PyObject_CallMethod(io, "open", "isisssi", fd, mode,
- buffering, encoding, errors,
- newline, closefd);
- Py_DECREF(io);
- if (stream == NULL)
- return NULL;
- if (name != NULL) {
- nameobj = PyUnicode_FromString(name);
- if (nameobj == NULL)
- PyErr_Clear();
- else {
- if (PyObject_SetAttrString(stream, "name", nameobj) < 0)
- PyErr_Clear();
- Py_DECREF(nameobj);
- }
- }
- return stream;
+ PyObject *io, *stream, *nameobj = NULL;
+
+ io = PyImport_ImportModule("io");
+ if (io == NULL)
+ return NULL;
+ stream = PyObject_CallMethod(io, "open", "isisssi", fd, mode,
+ buffering, encoding, errors,
+ newline, closefd);
+ Py_DECREF(io);
+ if (stream == NULL)
+ return NULL;
+ if (name != NULL) {
+ nameobj = PyUnicode_FromString(name);
+ if (nameobj == NULL)
+ PyErr_Clear();
+ else {
+ if (PyObject_SetAttrString(stream, "name", nameobj) < 0)
+ PyErr_Clear();
+ Py_DECREF(nameobj);
+ }
+ }
+ return stream;
}
PyObject *
PyFile_GetLine(PyObject *f, int n)
{
- PyObject *result;
-
- if (f == NULL) {
- PyErr_BadInternalCall();
- return NULL;
- }
-
- {
- PyObject *reader;
- PyObject *args;
-
- reader = PyObject_GetAttrString(f, "readline");
- if (reader == NULL)
- return NULL;
- if (n <= 0)
- args = PyTuple_New(0);
- else
- args = Py_BuildValue("(i)", n);
- if (args == NULL) {
- Py_DECREF(reader);
- return NULL;
- }
- result = PyEval_CallObject(reader, args);
- Py_DECREF(reader);
- Py_DECREF(args);
- if (result != NULL && !PyBytes_Check(result) &&
- !PyUnicode_Check(result)) {
- Py_DECREF(result);
- result = NULL;
- PyErr_SetString(PyExc_TypeError,
- "object.readline() returned non-string");
- }
- }
-
- if (n < 0 && result != NULL && PyBytes_Check(result)) {
- char *s = PyBytes_AS_STRING(result);
- Py_ssize_t len = PyBytes_GET_SIZE(result);
- if (len == 0) {
- Py_DECREF(result);
- result = NULL;
- PyErr_SetString(PyExc_EOFError,
- "EOF when reading a line");
- }
- else if (s[len-1] == '\n') {
- if (result->ob_refcnt == 1)
- _PyBytes_Resize(&result, len-1);
- else {
- PyObject *v;
- v = PyBytes_FromStringAndSize(s, len-1);
- Py_DECREF(result);
- result = v;
- }
- }
- }
- if (n < 0 && result != NULL && PyUnicode_Check(result)) {
- Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
- Py_ssize_t len = PyUnicode_GET_SIZE(result);
- if (len == 0) {
- Py_DECREF(result);
- result = NULL;
- PyErr_SetString(PyExc_EOFError,
- "EOF when reading a line");
- }
- else if (s[len-1] == '\n') {
- if (result->ob_refcnt == 1)
- PyUnicode_Resize(&result, len-1);
- else {
- PyObject *v;
- v = PyUnicode_FromUnicode(s, len-1);
- Py_DECREF(result);
- result = v;
- }
- }
- }
- return result;
+ PyObject *result;
+
+ if (f == NULL) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ {
+ PyObject *reader;
+ PyObject *args;
+
+ reader = PyObject_GetAttrString(f, "readline");
+ if (reader == NULL)
+ return NULL;
+ if (n <= 0)
+ args = PyTuple_New(0);
+ else
+ args = Py_BuildValue("(i)", n);
+ if (args == NULL) {
+ Py_DECREF(reader);
+ return NULL;
+ }
+ result = PyEval_CallObject(reader, args);
+ Py_DECREF(reader);
+ Py_DECREF(args);
+ if (result != NULL && !PyBytes_Check(result) &&
+ !PyUnicode_Check(result)) {
+ Py_DECREF(result);
+ result = NULL;
+ PyErr_SetString(PyExc_TypeError,
+ "object.readline() returned non-string");
+ }
+ }
+
+ if (n < 0 && result != NULL && PyBytes_Check(result)) {
+ char *s = PyBytes_AS_STRING(result);
+ Py_ssize_t len = PyBytes_GET_SIZE(result);
+ if (len == 0) {
+ Py_DECREF(result);
+ result = NULL;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF when reading a line");
+ }
+ else if (s[len-1] == '\n') {
+ if (result->ob_refcnt == 1)
+ _PyBytes_Resize(&result, len-1);
+ else {
+ PyObject *v;
+ v = PyBytes_FromStringAndSize(s, len-1);
+ Py_DECREF(result);
+ result = v;
+ }
+ }
+ }
+ if (n < 0 && result != NULL && PyUnicode_Check(result)) {
+ Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
+ Py_ssize_t len = PyUnicode_GET_SIZE(result);
+ if (len == 0) {
+ Py_DECREF(result);
+ result = NULL;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF when reading a line");
+ }
+ else if (s[len-1] == '\n') {
+ if (result->ob_refcnt == 1)
+ PyUnicode_Resize(&result, len-1);
+ else {
+ PyObject *v;
+ v = PyUnicode_FromUnicode(s, len-1);
+ Py_DECREF(result);
+ result = v;
+ }
+ }
+ }
+ return result;
}
/* Interfaces to write objects/strings to file-like objects */
@@ -138,60 +138,60 @@ PyFile_GetLine(PyObject *f, int n)
int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
- PyObject *writer, *value, *args, *result;
- if (f == NULL) {
- PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
- return -1;
- }
- writer = PyObject_GetAttrString(f, "write");
- if (writer == NULL)
- return -1;
- if (flags & Py_PRINT_RAW) {
- value = PyObject_Str(v);
- }
- else
- value = PyObject_Repr(v);
- if (value == NULL) {
- Py_DECREF(writer);
- return -1;
- }
- args = PyTuple_Pack(1, value);
- if (args == NULL) {
- Py_DECREF(value);
- Py_DECREF(writer);
- return -1;
- }
- result = PyEval_CallObject(writer, args);
- Py_DECREF(args);
- Py_DECREF(value);
- Py_DECREF(writer);
- if (result == NULL)
- return -1;
- Py_DECREF(result);
- return 0;
+ PyObject *writer, *value, *args, *result;
+ if (f == NULL) {
+ PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
+ return -1;
+ }
+ writer = PyObject_GetAttrString(f, "write");
+ if (writer == NULL)
+ return -1;
+ if (flags & Py_PRINT_RAW) {
+ value = PyObject_Str(v);
+ }
+ else
+ value = PyObject_Repr(v);
+ if (value == NULL) {
+ Py_DECREF(writer);
+ return -1;
+ }
+ args = PyTuple_Pack(1, value);
+ if (args == NULL) {
+ Py_DECREF(value);
+ Py_DECREF(writer);
+ return -1;
+ }
+ result = PyEval_CallObject(writer, args);
+ Py_DECREF(args);
+ Py_DECREF(value);
+ Py_DECREF(writer);
+ if (result == NULL)
+ return -1;
+ Py_DECREF(result);
+ return 0;
}
int
PyFile_WriteString(const char *s, PyObject *f)
{
- if (f == NULL) {
- /* Should be caused by a pre-existing error */
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_SystemError,
- "null file for PyFile_WriteString");
- return -1;
- }
- else if (!PyErr_Occurred()) {
- PyObject *v = PyUnicode_FromString(s);
- int err;
- if (v == NULL)
- return -1;
- err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
- Py_DECREF(v);
- return err;
- }
- else
- return -1;
+ if (f == NULL) {
+ /* Should be caused by a pre-existing error */
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError,
+ "null file for PyFile_WriteString");
+ return -1;
+ }
+ else if (!PyErr_Occurred()) {
+ PyObject *v = PyUnicode_FromString(s);
+ int err;
+ if (v == NULL)
+ return -1;
+ err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
+ Py_DECREF(v);
+ return err;
+ }
+ else
+ return -1;
}
/* Try to get a file-descriptor from a Python object. If the object
@@ -204,45 +204,45 @@ PyFile_WriteString(const char *s, PyObject *f)
int
PyObject_AsFileDescriptor(PyObject *o)
{
- int fd;
- PyObject *meth;
-
- if (PyLong_Check(o)) {
- fd = PyLong_AsLong(o);
- }
- else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
- {
- PyObject *fno = PyEval_CallObject(meth, NULL);
- Py_DECREF(meth);
- if (fno == NULL)
- return -1;
-
- if (PyLong_Check(fno)) {
- fd = PyLong_AsLong(fno);
- Py_DECREF(fno);
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "fileno() returned a non-integer");
- Py_DECREF(fno);
- return -1;
- }
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "argument must be an int, or have a fileno() method.");
- return -1;
- }
-
- if (fd == -1 && PyErr_Occurred())
- return -1;
- if (fd < 0) {
- PyErr_Format(PyExc_ValueError,
- "file descriptor cannot be a negative integer (%i)",
- fd);
- return -1;
- }
- return fd;
+ int fd;
+ PyObject *meth;
+
+ if (PyLong_Check(o)) {
+ fd = PyLong_AsLong(o);
+ }
+ else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
+ {
+ PyObject *fno = PyEval_CallObject(meth, NULL);
+ Py_DECREF(meth);
+ if (fno == NULL)
+ return -1;
+
+ if (PyLong_Check(fno)) {
+ fd = PyLong_AsLong(fno);
+ Py_DECREF(fno);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "fileno() returned a non-integer");
+ Py_DECREF(fno);
+ return -1;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "argument must be an int, or have a fileno() method.");
+ return -1;
+ }
+
+ if (fd == -1 && PyErr_Occurred())
+ return -1;
+ if (fd < 0) {
+ PyErr_Format(PyExc_ValueError,
+ "file descriptor cannot be a negative integer (%i)",
+ fd);
+ return -1;
+ }
+ return fd;
}
/*
@@ -262,68 +262,68 @@ PyObject_AsFileDescriptor(PyObject *o)
char *
Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
{
- char *p = buf;
- int c;
- int newlinetypes = 0;
- int skipnextlf = 0;
-
- if (fobj) {
- errno = ENXIO; /* What can you do... */
- return NULL;
- }
- FLOCKFILE(stream);
- c = 'x'; /* Shut up gcc warning */
- while (--n > 0 && (c = GETC(stream)) != EOF ) {
- if (skipnextlf ) {
- skipnextlf = 0;
- if (c == '\n') {
- /* Seeing a \n here with skipnextlf true
- ** means we saw a \r before.
- */
- newlinetypes |= NEWLINE_CRLF;
- c = GETC(stream);
- if (c == EOF) break;
- } else {
- /*
- ** Note that c == EOF also brings us here,
- ** so we're okay if the last char in the file
- ** is a CR.
- */
- newlinetypes |= NEWLINE_CR;
- }
- }
- if (c == '\r') {
- /* A \r is translated into a \n, and we skip
- ** an adjacent \n, if any. We don't set the
- ** newlinetypes flag until we've seen the next char.
- */
- skipnextlf = 1;
- c = '\n';
- } else if ( c == '\n') {
- newlinetypes |= NEWLINE_LF;
- }
- *p++ = c;
- if (c == '\n') break;
- }
- if ( c == EOF && skipnextlf )
- newlinetypes |= NEWLINE_CR;
- FUNLOCKFILE(stream);
- *p = '\0';
- if ( skipnextlf ) {
- /* If we have no file object we cannot save the
- ** skipnextlf flag. We have to readahead, which
- ** will cause a pause if we're reading from an
- ** interactive stream, but that is very unlikely
- ** unless we're doing something silly like
- ** exec(open("/dev/tty").read()).
- */
- c = GETC(stream);
- if ( c != '\n' )
- ungetc(c, stream);
- }
- if (p == buf)
- return NULL;
- return buf;
+ char *p = buf;
+ int c;
+ int newlinetypes = 0;
+ int skipnextlf = 0;
+
+ if (fobj) {
+ errno = ENXIO; /* What can you do... */
+ return NULL;
+ }
+ FLOCKFILE(stream);
+ c = 'x'; /* Shut up gcc warning */
+ while (--n > 0 && (c = GETC(stream)) != EOF ) {
+ if (skipnextlf ) {
+ skipnextlf = 0;
+ if (c == '\n') {
+ /* Seeing a \n here with skipnextlf true
+ ** means we saw a \r before.
+ */
+ newlinetypes |= NEWLINE_CRLF;
+ c = GETC(stream);
+ if (c == EOF) break;
+ } else {
+ /*
+ ** Note that c == EOF also brings us here,
+ ** so we're okay if the last char in the file
+ ** is a CR.
+ */
+ newlinetypes |= NEWLINE_CR;
+ }
+ }
+ if (c == '\r') {
+ /* A \r is translated into a \n, and we skip
+ ** an adjacent \n, if any. We don't set the
+ ** newlinetypes flag until we've seen the next char.
+ */
+ skipnextlf = 1;
+ c = '\n';
+ } else if ( c == '\n') {
+ newlinetypes |= NEWLINE_LF;
+ }
+ *p++ = c;
+ if (c == '\n') break;
+ }
+ if ( c == EOF && skipnextlf )
+ newlinetypes |= NEWLINE_CR;
+ FUNLOCKFILE(stream);
+ *p = '\0';
+ if ( skipnextlf ) {
+ /* If we have no file object we cannot save the
+ ** skipnextlf flag. We have to readahead, which
+ ** will cause a pause if we're reading from an
+ ** interactive stream, but that is very unlikely
+ ** unless we're doing something silly like
+ ** exec(open("/dev/tty").read()).
+ */
+ c = GETC(stream);
+ if ( c != '\n' )
+ ungetc(c, stream);
+ }
+ if (p == buf)
+ return NULL;
+ return buf;
}
/* **************************** std printer ****************************
@@ -332,195 +332,195 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
*/
typedef struct {
- PyObject_HEAD
- int fd;
+ PyObject_HEAD
+ int fd;
} PyStdPrinter_Object;
static PyObject *
stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
{
- PyStdPrinter_Object *self;
+ PyStdPrinter_Object *self;
- assert(type != NULL && type->tp_alloc != NULL);
+ assert(type != NULL && type->tp_alloc != NULL);
- self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
- if (self != NULL) {
- self->fd = -1;
- }
+ self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
+ if (self != NULL) {
+ self->fd = -1;
+ }
- return (PyObject *) self;
+ return (PyObject *) self;
}
static int
fileio_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyErr_SetString(PyExc_TypeError,
- "cannot create 'stderrprinter' instances");
- return -1;
+ PyErr_SetString(PyExc_TypeError,
+ "cannot create 'stderrprinter' instances");
+ return -1;
}
PyObject *
PyFile_NewStdPrinter(int fd)
{
- PyStdPrinter_Object *self;
-
- if (fd != fileno(stdout) && fd != fileno(stderr)) {
- /* not enough infrastructure for PyErr_BadInternalCall() */
- return NULL;
- }
-
- self = PyObject_New(PyStdPrinter_Object,
- &PyStdPrinter_Type);
- if (self != NULL) {
- self->fd = fd;
- }
- return (PyObject*)self;
+ PyStdPrinter_Object *self;
+
+ if (fd != fileno(stdout) && fd != fileno(stderr)) {
+ /* not enough infrastructure for PyErr_BadInternalCall() */
+ return NULL;
+ }
+
+ self = PyObject_New(PyStdPrinter_Object,
+ &PyStdPrinter_Type);
+ if (self != NULL) {
+ self->fd = fd;
+ }
+ return (PyObject*)self;
}
static PyObject *
stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
{
- char *c;
- Py_ssize_t n;
-
- if (self->fd < 0) {
- /* fd might be invalid on Windows
- * I can't raise an exception here. It may lead to an
- * unlimited recursion in the case stderr is invalid.
- */
- Py_RETURN_NONE;
- }
-
- if (!PyArg_ParseTuple(args, "s", &c)) {
- return NULL;
- }
- n = strlen(c);
-
- Py_BEGIN_ALLOW_THREADS
- errno = 0;
- n = write(self->fd, c, n);
- Py_END_ALLOW_THREADS
-
- if (n < 0) {
- if (errno == EAGAIN)
- Py_RETURN_NONE;
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
- }
-
- return PyLong_FromSsize_t(n);
+ char *c;
+ Py_ssize_t n;
+
+ if (self->fd < 0) {
+ /* fd might be invalid on Windows
+ * I can't raise an exception here. It may lead to an
+ * unlimited recursion in the case stderr is invalid.
+ */
+ Py_RETURN_NONE;
+ }
+
+ if (!PyArg_ParseTuple(args, "s", &c)) {
+ return NULL;
+ }
+ n = strlen(c);
+
+ Py_BEGIN_ALLOW_THREADS
+ errno = 0;
+ n = write(self->fd, c, n);
+ Py_END_ALLOW_THREADS
+
+ if (n < 0) {
+ if (errno == EAGAIN)
+ Py_RETURN_NONE;
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
+ }
+
+ return PyLong_FromSsize_t(n);
}
static PyObject *
stdprinter_fileno(PyStdPrinter_Object *self)
{
- return PyLong_FromLong((long) self->fd);
+ return PyLong_FromLong((long) self->fd);
}
static PyObject *
stdprinter_repr(PyStdPrinter_Object *self)
{
- return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
- self->fd, self);
+ return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
+ self->fd, self);
}
static PyObject *
stdprinter_noop(PyStdPrinter_Object *self)
{
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyObject *
stdprinter_isatty(PyStdPrinter_Object *self)
{
- long res;
- if (self->fd < 0) {
- Py_RETURN_FALSE;
- }
+ long res;
+ if (self->fd < 0) {
+ Py_RETURN_FALSE;
+ }
- Py_BEGIN_ALLOW_THREADS
- res = isatty(self->fd);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ res = isatty(self->fd);
+ Py_END_ALLOW_THREADS
- return PyBool_FromLong(res);
+ return PyBool_FromLong(res);
}
static PyMethodDef stdprinter_methods[] = {
- {"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
- {"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
- {"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""},
- {"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""},
- {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""},
- {NULL, NULL} /*sentinel */
+ {"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
+ {"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
+ {"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""},
+ {"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""},
+ {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""},
+ {NULL, NULL} /*sentinel */
};
static PyObject *
get_closed(PyStdPrinter_Object *self, void *closure)
{
- Py_INCREF(Py_False);
- return Py_False;
+ Py_INCREF(Py_False);
+ return Py_False;
}
static PyObject *
get_mode(PyStdPrinter_Object *self, void *closure)
{
- return PyUnicode_FromString("w");
+ return PyUnicode_FromString("w");
}
static PyObject *
get_encoding(PyStdPrinter_Object *self, void *closure)
{
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyGetSetDef stdprinter_getsetlist[] = {
- {"closed", (getter)get_closed, NULL, "True if the file is closed"},
- {"encoding", (getter)get_encoding, NULL, "Encoding of the file"},
- {"mode", (getter)get_mode, NULL, "String giving the file mode"},
- {0},
+ {"closed", (getter)get_closed, NULL, "True if the file is closed"},
+ {"encoding", (getter)get_encoding, NULL, "Encoding of the file"},
+ {"mode", (getter)get_mode, NULL, "String giving the file mode"},
+ {0},
};
PyTypeObject PyStdPrinter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "stderrprinter", /* tp_name */
- sizeof(PyStdPrinter_Object), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- 0, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)stdprinter_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- stdprinter_methods, /* tp_methods */
- 0, /* tp_members */
- stdprinter_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- fileio_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- stdprinter_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "stderrprinter", /* tp_name */
+ sizeof(PyStdPrinter_Object), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ 0, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)stdprinter_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ stdprinter_methods, /* tp_methods */
+ 0, /* tp_members */
+ stdprinter_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ fileio_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ stdprinter_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 341072e..959196d 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -26,13 +26,13 @@ extern int finite(double);
#endif
/* Special free list -- see comments for same code in intobject.c. */
-#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
-#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
-#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
+#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
+#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
+#define N_FLOATOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyFloatObject))
struct _floatblock {
- struct _floatblock *next;
- PyFloatObject objects[N_FLOATOBJECTS];
+ struct _floatblock *next;
+ PyFloatObject objects[N_FLOATOBJECTS];
};
typedef struct _floatblock PyFloatBlock;
@@ -43,31 +43,31 @@ static PyFloatObject *free_list = NULL;
static PyFloatObject *
fill_free_list(void)
{
- PyFloatObject *p, *q;
- /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
- p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
- if (p == NULL)
- return (PyFloatObject *) PyErr_NoMemory();
- ((PyFloatBlock *)p)->next = block_list;
- block_list = (PyFloatBlock *)p;
- p = &((PyFloatBlock *)p)->objects[0];
- q = p + N_FLOATOBJECTS;
- while (--q > p)
- Py_TYPE(q) = (struct _typeobject *)(q-1);
- Py_TYPE(q) = NULL;
- return p + N_FLOATOBJECTS - 1;
+ PyFloatObject *p, *q;
+ /* XXX Float blocks escape the object heap. Use PyObject_MALLOC ??? */
+ p = (PyFloatObject *) PyMem_MALLOC(sizeof(PyFloatBlock));
+ if (p == NULL)
+ return (PyFloatObject *) PyErr_NoMemory();
+ ((PyFloatBlock *)p)->next = block_list;
+ block_list = (PyFloatBlock *)p;
+ p = &((PyFloatBlock *)p)->objects[0];
+ q = p + N_FLOATOBJECTS;
+ while (--q > p)
+ Py_TYPE(q) = (struct _typeobject *)(q-1);
+ Py_TYPE(q) = NULL;
+ return p + N_FLOATOBJECTS - 1;
}
double
PyFloat_GetMax(void)
{
- return DBL_MAX;
+ return DBL_MAX;
}
double
PyFloat_GetMin(void)
{
- return DBL_MIN;
+ return DBL_MIN;
}
static PyTypeObject FloatInfoType;
@@ -80,183 +80,183 @@ information about the precision and internal representation. Please study\n\
your system's :file:`float.h` for more information.");
static PyStructSequence_Field floatinfo_fields[] = {
- {"max", "DBL_MAX -- maximum representable finite float"},
- {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
- "is representable"},
- {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
- "is representable"},
- {"min", "DBL_MIN -- Minimum positive normalizer float"},
- {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
- "is a normalized float"},
- {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
- "a normalized"},
- {"dig", "DBL_DIG -- digits"},
- {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
- {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
- "representable float"},
- {"radix", "FLT_RADIX -- radix of exponent"},
- {"rounds", "FLT_ROUNDS -- addition rounds"},
- {0}
+ {"max", "DBL_MAX -- maximum representable finite float"},
+ {"max_exp", "DBL_MAX_EXP -- maximum int e such that radix**(e-1) "
+ "is representable"},
+ {"max_10_exp", "DBL_MAX_10_EXP -- maximum int e such that 10**e "
+ "is representable"},
+ {"min", "DBL_MIN -- Minimum positive normalizer float"},
+ {"min_exp", "DBL_MIN_EXP -- minimum int e such that radix**(e-1) "
+ "is a normalized float"},
+ {"min_10_exp", "DBL_MIN_10_EXP -- minimum int e such that 10**e is "
+ "a normalized"},
+ {"dig", "DBL_DIG -- digits"},
+ {"mant_dig", "DBL_MANT_DIG -- mantissa digits"},
+ {"epsilon", "DBL_EPSILON -- Difference between 1 and the next "
+ "representable float"},
+ {"radix", "FLT_RADIX -- radix of exponent"},
+ {"rounds", "FLT_ROUNDS -- addition rounds"},
+ {0}
};
static PyStructSequence_Desc floatinfo_desc = {
- "sys.floatinfo", /* name */
- floatinfo__doc__, /* doc */
- floatinfo_fields, /* fields */
- 11
+ "sys.floatinfo", /* name */
+ floatinfo__doc__, /* doc */
+ floatinfo_fields, /* fields */
+ 11
};
PyObject *
PyFloat_GetInfo(void)
{
- PyObject* floatinfo;
- int pos = 0;
+ PyObject* floatinfo;
+ int pos = 0;
- floatinfo = PyStructSequence_New(&FloatInfoType);
- if (floatinfo == NULL) {
- return NULL;
- }
+ floatinfo = PyStructSequence_New(&FloatInfoType);
+ if (floatinfo == NULL) {
+ return NULL;
+ }
#define SetIntFlag(flag) \
- PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
+ PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag))
#define SetDblFlag(flag) \
- PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
-
- SetDblFlag(DBL_MAX);
- SetIntFlag(DBL_MAX_EXP);
- SetIntFlag(DBL_MAX_10_EXP);
- SetDblFlag(DBL_MIN);
- SetIntFlag(DBL_MIN_EXP);
- SetIntFlag(DBL_MIN_10_EXP);
- SetIntFlag(DBL_DIG);
- SetIntFlag(DBL_MANT_DIG);
- SetDblFlag(DBL_EPSILON);
- SetIntFlag(FLT_RADIX);
- SetIntFlag(FLT_ROUNDS);
+ PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag))
+
+ SetDblFlag(DBL_MAX);
+ SetIntFlag(DBL_MAX_EXP);
+ SetIntFlag(DBL_MAX_10_EXP);
+ SetDblFlag(DBL_MIN);
+ SetIntFlag(DBL_MIN_EXP);
+ SetIntFlag(DBL_MIN_10_EXP);
+ SetIntFlag(DBL_DIG);
+ SetIntFlag(DBL_MANT_DIG);
+ SetDblFlag(DBL_EPSILON);
+ SetIntFlag(FLT_RADIX);
+ SetIntFlag(FLT_ROUNDS);
#undef SetIntFlag
#undef SetDblFlag
-
- if (PyErr_Occurred()) {
- Py_CLEAR(floatinfo);
- return NULL;
- }
- return floatinfo;
+
+ if (PyErr_Occurred()) {
+ Py_CLEAR(floatinfo);
+ return NULL;
+ }
+ return floatinfo;
}
PyObject *
PyFloat_FromDouble(double fval)
{
- register PyFloatObject *op;
- if (free_list == NULL) {
- if ((free_list = fill_free_list()) == NULL)
- return NULL;
- }
- /* Inline PyObject_New */
- op = free_list;
- free_list = (PyFloatObject *)Py_TYPE(op);
- PyObject_INIT(op, &PyFloat_Type);
- op->ob_fval = fval;
- return (PyObject *) op;
+ register PyFloatObject *op;
+ if (free_list == NULL) {
+ if ((free_list = fill_free_list()) == NULL)
+ return NULL;
+ }
+ /* Inline PyObject_New */
+ op = free_list;
+ free_list = (PyFloatObject *)Py_TYPE(op);
+ PyObject_INIT(op, &PyFloat_Type);
+ op->ob_fval = fval;
+ return (PyObject *) op;
}
PyObject *
PyFloat_FromString(PyObject *v)
{
- const char *s, *last, *end;
- double x;
- char buffer[256]; /* for errors */
- char *s_buffer = NULL;
- Py_ssize_t len;
- PyObject *result = NULL;
-
- if (PyUnicode_Check(v)) {
- s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
- if (s_buffer == NULL)
- return PyErr_NoMemory();
- if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
- PyUnicode_GET_SIZE(v),
- s_buffer,
- NULL))
- goto error;
- s = s_buffer;
- len = strlen(s);
- }
- else if (PyObject_AsCharBuffer(v, &s, &len)) {
- PyErr_SetString(PyExc_TypeError,
- "float() argument must be a string or a number");
- return NULL;
- }
- last = s + len;
-
- while (Py_ISSPACE(*s))
- s++;
- /* We don't care about overflow or underflow. If the platform
- * supports them, infinities and signed zeroes (on underflow) are
- * fine. */
- x = PyOS_string_to_double(s, (char **)&end, NULL);
- if (x == -1.0 && PyErr_Occurred())
- goto error;
- while (Py_ISSPACE(*end))
- end++;
- if (end == last)
- result = PyFloat_FromDouble(x);
- else {
- PyOS_snprintf(buffer, sizeof(buffer),
- "invalid literal for float(): %.200s", s);
- PyErr_SetString(PyExc_ValueError, buffer);
- result = NULL;
- }
+ const char *s, *last, *end;
+ double x;
+ char buffer[256]; /* for errors */
+ char *s_buffer = NULL;
+ Py_ssize_t len;
+ PyObject *result = NULL;
+
+ if (PyUnicode_Check(v)) {
+ s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
+ if (s_buffer == NULL)
+ return PyErr_NoMemory();
+ if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
+ PyUnicode_GET_SIZE(v),
+ s_buffer,
+ NULL))
+ goto error;
+ s = s_buffer;
+ len = strlen(s);
+ }
+ else if (PyObject_AsCharBuffer(v, &s, &len)) {
+ PyErr_SetString(PyExc_TypeError,
+ "float() argument must be a string or a number");
+ return NULL;
+ }
+ last = s + len;
+
+ while (Py_ISSPACE(*s))
+ s++;
+ /* We don't care about overflow or underflow. If the platform
+ * supports them, infinities and signed zeroes (on underflow) are
+ * fine. */
+ x = PyOS_string_to_double(s, (char **)&end, NULL);
+ if (x == -1.0 && PyErr_Occurred())
+ goto error;
+ while (Py_ISSPACE(*end))
+ end++;
+ if (end == last)
+ result = PyFloat_FromDouble(x);
+ else {
+ PyOS_snprintf(buffer, sizeof(buffer),
+ "invalid literal for float(): %.200s", s);
+ PyErr_SetString(PyExc_ValueError, buffer);
+ result = NULL;
+ }
error:
- if (s_buffer)
- PyMem_FREE(s_buffer);
- return result;
+ if (s_buffer)
+ PyMem_FREE(s_buffer);
+ return result;
}
static void
float_dealloc(PyFloatObject *op)
{
- if (PyFloat_CheckExact(op)) {
- Py_TYPE(op) = (struct _typeobject *)free_list;
- free_list = op;
- }
- else
- Py_TYPE(op)->tp_free((PyObject *)op);
+ if (PyFloat_CheckExact(op)) {
+ Py_TYPE(op) = (struct _typeobject *)free_list;
+ free_list = op;
+ }
+ else
+ Py_TYPE(op)->tp_free((PyObject *)op);
}
double
PyFloat_AsDouble(PyObject *op)
{
- PyNumberMethods *nb;
- PyFloatObject *fo;
- double val;
-
- if (op && PyFloat_Check(op))
- return PyFloat_AS_DOUBLE((PyFloatObject*) op);
-
- if (op == NULL) {
- PyErr_BadArgument();
- return -1;
- }
-
- if ((nb = Py_TYPE(op)->tp_as_number) == NULL || nb->nb_float == NULL) {
- PyErr_SetString(PyExc_TypeError, "a float is required");
- return -1;
- }
-
- fo = (PyFloatObject*) (*nb->nb_float) (op);
- if (fo == NULL)
- return -1;
- if (!PyFloat_Check(fo)) {
- PyErr_SetString(PyExc_TypeError,
- "nb_float should return float object");
- return -1;
- }
-
- val = PyFloat_AS_DOUBLE(fo);
- Py_DECREF(fo);
-
- return val;
+ PyNumberMethods *nb;
+ PyFloatObject *fo;
+ double val;
+
+ if (op && PyFloat_Check(op))
+ return PyFloat_AS_DOUBLE((PyFloatObject*) op);
+
+ if (op == NULL) {
+ PyErr_BadArgument();
+ return -1;
+ }
+
+ if ((nb = Py_TYPE(op)->tp_as_number) == NULL || nb->nb_float == NULL) {
+ PyErr_SetString(PyExc_TypeError, "a float is required");
+ return -1;
+ }
+
+ fo = (PyFloatObject*) (*nb->nb_float) (op);
+ if (fo == NULL)
+ return -1;
+ if (!PyFloat_Check(fo)) {
+ PyErr_SetString(PyExc_TypeError,
+ "nb_float should return float object");
+ return -1;
+ }
+
+ val = PyFloat_AS_DOUBLE(fo);
+ Py_DECREF(fo);
+
+ return val;
}
/* Macro and helper that convert PyObject obj to a C double and store
@@ -265,32 +265,32 @@ PyFloat_AsDouble(PyObject *op)
obj is not of float, int or long type, Py_NotImplemented is incref'ed,
stored in obj, and returned from the function invoking this macro.
*/
-#define CONVERT_TO_DOUBLE(obj, dbl) \
- if (PyFloat_Check(obj)) \
- dbl = PyFloat_AS_DOUBLE(obj); \
- else if (convert_to_double(&(obj), &(dbl)) < 0) \
- return obj;
+#define CONVERT_TO_DOUBLE(obj, dbl) \
+ if (PyFloat_Check(obj)) \
+ dbl = PyFloat_AS_DOUBLE(obj); \
+ else if (convert_to_double(&(obj), &(dbl)) < 0) \
+ return obj;
/* Methods */
static int
convert_to_double(PyObject **v, double *dbl)
{
- register PyObject *obj = *v;
-
- if (PyLong_Check(obj)) {
- *dbl = PyLong_AsDouble(obj);
- if (*dbl == -1.0 && PyErr_Occurred()) {
- *v = NULL;
- return -1;
- }
- }
- else {
- Py_INCREF(Py_NotImplemented);
- *v = Py_NotImplemented;
- return -1;
- }
- return 0;
+ register PyObject *obj = *v;
+
+ if (PyLong_Check(obj)) {
+ *dbl = PyLong_AsDouble(obj);
+ if (*dbl == -1.0 && PyErr_Occurred()) {
+ *v = NULL;
+ return -1;
+ }
+ }
+ else {
+ Py_INCREF(Py_NotImplemented);
+ *v = Py_NotImplemented;
+ return -1;
+ }
+ return 0;
}
static PyObject *
@@ -302,7 +302,7 @@ float_str_or_repr(PyFloatObject *v, int precision, char format_code)
Py_DTSF_ADD_DOT_0,
NULL);
if (!buf)
- return PyErr_NoMemory();
+ return PyErr_NoMemory();
result = PyUnicode_FromString(buf);
PyMem_Free(buf);
return result;
@@ -338,514 +338,514 @@ float_str(PyFloatObject *v)
static PyObject*
float_richcompare(PyObject *v, PyObject *w, int op)
{
- double i, j;
- int r = 0;
-
- assert(PyFloat_Check(v));
- i = PyFloat_AS_DOUBLE(v);
-
- /* Switch on the type of w. Set i and j to doubles to be compared,
- * and op to the richcomp to use.
- */
- if (PyFloat_Check(w))
- j = PyFloat_AS_DOUBLE(w);
-
- else if (!Py_IS_FINITE(i)) {
- if (PyLong_Check(w))
- /* If i is an infinity, its magnitude exceeds any
- * finite integer, so it doesn't matter which int we
- * compare i with. If i is a NaN, similarly.
- */
- j = 0.0;
- else
- goto Unimplemented;
- }
-
- else if (PyLong_Check(w)) {
- int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
- int wsign = _PyLong_Sign(w);
- size_t nbits;
- int exponent;
-
- if (vsign != wsign) {
- /* Magnitudes are irrelevant -- the signs alone
- * determine the outcome.
- */
- i = (double)vsign;
- j = (double)wsign;
- goto Compare;
- }
- /* The signs are the same. */
- /* Convert w to a double if it fits. In particular, 0 fits. */
- nbits = _PyLong_NumBits(w);
- if (nbits == (size_t)-1 && PyErr_Occurred()) {
- /* This long is so large that size_t isn't big enough
- * to hold the # of bits. Replace with little doubles
- * that give the same outcome -- w is so large that
- * its magnitude must exceed the magnitude of any
- * finite float.
- */
- PyErr_Clear();
- i = (double)vsign;
- assert(wsign != 0);
- j = wsign * 2.0;
- goto Compare;
- }
- if (nbits <= 48) {
- j = PyLong_AsDouble(w);
- /* It's impossible that <= 48 bits overflowed. */
- assert(j != -1.0 || ! PyErr_Occurred());
- goto Compare;
- }
- assert(wsign != 0); /* else nbits was 0 */
- assert(vsign != 0); /* if vsign were 0, then since wsign is
- * not 0, we would have taken the
- * vsign != wsign branch at the start */
- /* We want to work with non-negative numbers. */
- if (vsign < 0) {
- /* "Multiply both sides" by -1; this also swaps the
- * comparator.
- */
- i = -i;
- op = _Py_SwappedOp[op];
- }
- assert(i > 0.0);
- (void) frexp(i, &exponent);
- /* exponent is the # of bits in v before the radix point;
- * we know that nbits (the # of bits in w) > 48 at this point
- */
- if (exponent < 0 || (size_t)exponent < nbits) {
- i = 1.0;
- j = 2.0;
- goto Compare;
- }
- if ((size_t)exponent > nbits) {
- i = 2.0;
- j = 1.0;
- goto Compare;
- }
- /* v and w have the same number of bits before the radix
- * point. Construct two longs that have the same comparison
- * outcome.
- */
- {
- double fracpart;
- double intpart;
- PyObject *result = NULL;
- PyObject *one = NULL;
- PyObject *vv = NULL;
- PyObject *ww = w;
-
- if (wsign < 0) {
- ww = PyNumber_Negative(w);
- if (ww == NULL)
- goto Error;
- }
- else
- Py_INCREF(ww);
-
- fracpart = modf(i, &intpart);
- vv = PyLong_FromDouble(intpart);
- if (vv == NULL)
- goto Error;
-
- if (fracpart != 0.0) {
- /* Shift left, and or a 1 bit into vv
- * to represent the lost fraction.
- */
- PyObject *temp;
-
- one = PyLong_FromLong(1);
- if (one == NULL)
- goto Error;
-
- temp = PyNumber_Lshift(ww, one);
- if (temp == NULL)
- goto Error;
- Py_DECREF(ww);
- ww = temp;
-
- temp = PyNumber_Lshift(vv, one);
- if (temp == NULL)
- goto Error;
- Py_DECREF(vv);
- vv = temp;
-
- temp = PyNumber_Or(vv, one);
- if (temp == NULL)
- goto Error;
- Py_DECREF(vv);
- vv = temp;
- }
-
- r = PyObject_RichCompareBool(vv, ww, op);
- if (r < 0)
- goto Error;
- result = PyBool_FromLong(r);
- Error:
- Py_XDECREF(vv);
- Py_XDECREF(ww);
- Py_XDECREF(one);
- return result;
- }
- } /* else if (PyLong_Check(w)) */
-
- else /* w isn't float, int, or long */
- goto Unimplemented;
+ double i, j;
+ int r = 0;
+
+ assert(PyFloat_Check(v));
+ i = PyFloat_AS_DOUBLE(v);
+
+ /* Switch on the type of w. Set i and j to doubles to be compared,
+ * and op to the richcomp to use.
+ */
+ if (PyFloat_Check(w))
+ j = PyFloat_AS_DOUBLE(w);
+
+ else if (!Py_IS_FINITE(i)) {
+ if (PyLong_Check(w))
+ /* If i is an infinity, its magnitude exceeds any
+ * finite integer, so it doesn't matter which int we
+ * compare i with. If i is a NaN, similarly.
+ */
+ j = 0.0;
+ else
+ goto Unimplemented;
+ }
+
+ else if (PyLong_Check(w)) {
+ int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
+ int wsign = _PyLong_Sign(w);
+ size_t nbits;
+ int exponent;
+
+ if (vsign != wsign) {
+ /* Magnitudes are irrelevant -- the signs alone
+ * determine the outcome.
+ */
+ i = (double)vsign;
+ j = (double)wsign;
+ goto Compare;
+ }
+ /* The signs are the same. */
+ /* Convert w to a double if it fits. In particular, 0 fits. */
+ nbits = _PyLong_NumBits(w);
+ if (nbits == (size_t)-1 && PyErr_Occurred()) {
+ /* This long is so large that size_t isn't big enough
+ * to hold the # of bits. Replace with little doubles
+ * that give the same outcome -- w is so large that
+ * its magnitude must exceed the magnitude of any
+ * finite float.
+ */
+ PyErr_Clear();
+ i = (double)vsign;
+ assert(wsign != 0);
+ j = wsign * 2.0;
+ goto Compare;
+ }
+ if (nbits <= 48) {
+ j = PyLong_AsDouble(w);
+ /* It's impossible that <= 48 bits overflowed. */
+ assert(j != -1.0 || ! PyErr_Occurred());
+ goto Compare;
+ }
+ assert(wsign != 0); /* else nbits was 0 */
+ assert(vsign != 0); /* if vsign were 0, then since wsign is
+ * not 0, we would have taken the
+ * vsign != wsign branch at the start */
+ /* We want to work with non-negative numbers. */
+ if (vsign < 0) {
+ /* "Multiply both sides" by -1; this also swaps the
+ * comparator.
+ */
+ i = -i;
+ op = _Py_SwappedOp[op];
+ }
+ assert(i > 0.0);
+ (void) frexp(i, &exponent);
+ /* exponent is the # of bits in v before the radix point;
+ * we know that nbits (the # of bits in w) > 48 at this point
+ */
+ if (exponent < 0 || (size_t)exponent < nbits) {
+ i = 1.0;
+ j = 2.0;
+ goto Compare;
+ }
+ if ((size_t)exponent > nbits) {
+ i = 2.0;
+ j = 1.0;
+ goto Compare;
+ }
+ /* v and w have the same number of bits before the radix
+ * point. Construct two longs that have the same comparison
+ * outcome.
+ */
+ {
+ double fracpart;
+ double intpart;
+ PyObject *result = NULL;
+ PyObject *one = NULL;
+ PyObject *vv = NULL;
+ PyObject *ww = w;
+
+ if (wsign < 0) {
+ ww = PyNumber_Negative(w);
+ if (ww == NULL)
+ goto Error;
+ }
+ else
+ Py_INCREF(ww);
+
+ fracpart = modf(i, &intpart);
+ vv = PyLong_FromDouble(intpart);
+ if (vv == NULL)
+ goto Error;
+
+ if (fracpart != 0.0) {
+ /* Shift left, and or a 1 bit into vv
+ * to represent the lost fraction.
+ */
+ PyObject *temp;
+
+ one = PyLong_FromLong(1);
+ if (one == NULL)
+ goto Error;
+
+ temp = PyNumber_Lshift(ww, one);
+ if (temp == NULL)
+ goto Error;
+ Py_DECREF(ww);
+ ww = temp;
+
+ temp = PyNumber_Lshift(vv, one);
+ if (temp == NULL)
+ goto Error;
+ Py_DECREF(vv);
+ vv = temp;
+
+ temp = PyNumber_Or(vv, one);
+ if (temp == NULL)
+ goto Error;
+ Py_DECREF(vv);
+ vv = temp;
+ }
+
+ r = PyObject_RichCompareBool(vv, ww, op);
+ if (r < 0)
+ goto Error;
+ result = PyBool_FromLong(r);
+ Error:
+ Py_XDECREF(vv);
+ Py_XDECREF(ww);
+ Py_XDECREF(one);
+ return result;
+ }
+ } /* else if (PyLong_Check(w)) */
+
+ else /* w isn't float, int, or long */
+ goto Unimplemented;
Compare:
- PyFPE_START_PROTECT("richcompare", return NULL)
- switch (op) {
- case Py_EQ:
- r = i == j;
- break;
- case Py_NE:
- r = i != j;
- break;
- case Py_LE:
- r = i <= j;
- break;
- case Py_GE:
- r = i >= j;
- break;
- case Py_LT:
- r = i < j;
- break;
- case Py_GT:
- r = i > j;
- break;
- }
- PyFPE_END_PROTECT(r)
- return PyBool_FromLong(r);
+ PyFPE_START_PROTECT("richcompare", return NULL)
+ switch (op) {
+ case Py_EQ:
+ r = i == j;
+ break;
+ case Py_NE:
+ r = i != j;
+ break;
+ case Py_LE:
+ r = i <= j;
+ break;
+ case Py_GE:
+ r = i >= j;
+ break;
+ case Py_LT:
+ r = i < j;
+ break;
+ case Py_GT:
+ r = i > j;
+ break;
+ }
+ PyFPE_END_PROTECT(r)
+ return PyBool_FromLong(r);
Unimplemented:
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static long
float_hash(PyFloatObject *v)
{
- return _Py_HashDouble(v->ob_fval);
+ return _Py_HashDouble(v->ob_fval);
}
static PyObject *
float_add(PyObject *v, PyObject *w)
{
- double a,b;
- CONVERT_TO_DOUBLE(v, a);
- CONVERT_TO_DOUBLE(w, b);
- PyFPE_START_PROTECT("add", return 0)
- a = a + b;
- PyFPE_END_PROTECT(a)
- return PyFloat_FromDouble(a);
+ double a,b;
+ CONVERT_TO_DOUBLE(v, a);
+ CONVERT_TO_DOUBLE(w, b);
+ PyFPE_START_PROTECT("add", return 0)
+ a = a + b;
+ PyFPE_END_PROTECT(a)
+ return PyFloat_FromDouble(a);
}
static PyObject *
float_sub(PyObject *v, PyObject *w)
{
- double a,b;
- CONVERT_TO_DOUBLE(v, a);
- CONVERT_TO_DOUBLE(w, b);
- PyFPE_START_PROTECT("subtract", return 0)
- a = a - b;
- PyFPE_END_PROTECT(a)
- return PyFloat_FromDouble(a);
+ double a,b;
+ CONVERT_TO_DOUBLE(v, a);
+ CONVERT_TO_DOUBLE(w, b);
+ PyFPE_START_PROTECT("subtract", return 0)
+ a = a - b;
+ PyFPE_END_PROTECT(a)
+ return PyFloat_FromDouble(a);
}
static PyObject *
float_mul(PyObject *v, PyObject *w)
{
- double a,b;
- CONVERT_TO_DOUBLE(v, a);
- CONVERT_TO_DOUBLE(w, b);
- PyFPE_START_PROTECT("multiply", return 0)
- a = a * b;
- PyFPE_END_PROTECT(a)
- return PyFloat_FromDouble(a);
+ double a,b;
+ CONVERT_TO_DOUBLE(v, a);
+ CONVERT_TO_DOUBLE(w, b);
+ PyFPE_START_PROTECT("multiply", return 0)
+ a = a * b;
+ PyFPE_END_PROTECT(a)
+ return PyFloat_FromDouble(a);
}
static PyObject *
float_div(PyObject *v, PyObject *w)
{
- double a,b;
- CONVERT_TO_DOUBLE(v, a);
- CONVERT_TO_DOUBLE(w, b);
+ double a,b;
+ CONVERT_TO_DOUBLE(v, a);
+ CONVERT_TO_DOUBLE(w, b);
#ifdef Py_NAN
- if (b == 0.0) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "float division");
- return NULL;
- }
+ if (b == 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "float division");
+ return NULL;
+ }
#endif
- PyFPE_START_PROTECT("divide", return 0)
- a = a / b;
- PyFPE_END_PROTECT(a)
- return PyFloat_FromDouble(a);
+ PyFPE_START_PROTECT("divide", return 0)
+ a = a / b;
+ PyFPE_END_PROTECT(a)
+ return PyFloat_FromDouble(a);
}
static PyObject *
float_rem(PyObject *v, PyObject *w)
{
- double vx, wx;
- double mod;
- CONVERT_TO_DOUBLE(v, vx);
- CONVERT_TO_DOUBLE(w, wx);
+ double vx, wx;
+ double mod;
+ CONVERT_TO_DOUBLE(v, vx);
+ CONVERT_TO_DOUBLE(w, wx);
#ifdef Py_NAN
- if (wx == 0.0) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "float modulo");
- return NULL;
- }
+ if (wx == 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "float modulo");
+ return NULL;
+ }
#endif
- PyFPE_START_PROTECT("modulo", return 0)
- mod = fmod(vx, wx);
- /* note: checking mod*wx < 0 is incorrect -- underflows to
- 0 if wx < sqrt(smallest nonzero double) */
- if (mod && ((wx < 0) != (mod < 0))) {
- mod += wx;
- }
- PyFPE_END_PROTECT(mod)
- return PyFloat_FromDouble(mod);
+ PyFPE_START_PROTECT("modulo", return 0)
+ mod = fmod(vx, wx);
+ /* note: checking mod*wx < 0 is incorrect -- underflows to
+ 0 if wx < sqrt(smallest nonzero double) */
+ if (mod && ((wx < 0) != (mod < 0))) {
+ mod += wx;
+ }
+ PyFPE_END_PROTECT(mod)
+ return PyFloat_FromDouble(mod);
}
static PyObject *
float_divmod(PyObject *v, PyObject *w)
{
- double vx, wx;
- double div, mod, floordiv;
- CONVERT_TO_DOUBLE(v, vx);
- CONVERT_TO_DOUBLE(w, wx);
- if (wx == 0.0) {
- PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
- return NULL;
- }
- PyFPE_START_PROTECT("divmod", return 0)
- mod = fmod(vx, wx);
- /* fmod is typically exact, so vx-mod is *mathematically* an
- exact multiple of wx. But this is fp arithmetic, and fp
- vx - mod is an approximation; the result is that div may
- not be an exact integral value after the division, although
- it will always be very close to one.
- */
- div = (vx - mod) / wx;
- if (mod) {
- /* ensure the remainder has the same sign as the denominator */
- if ((wx < 0) != (mod < 0)) {
- mod += wx;
- div -= 1.0;
- }
- }
- else {
- /* the remainder is zero, and in the presence of signed zeroes
- fmod returns different results across platforms; ensure
- it has the same sign as the denominator; we'd like to do
- "mod = wx * 0.0", but that may get optimized away */
- mod *= mod; /* hide "mod = +0" from optimizer */
- if (wx < 0.0)
- mod = -mod;
- }
- /* snap quotient to nearest integral value */
- if (div) {
- floordiv = floor(div);
- if (div - floordiv > 0.5)
- floordiv += 1.0;
- }
- else {
- /* div is zero - get the same sign as the true quotient */
- div *= div; /* hide "div = +0" from optimizers */
- floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
- }
- PyFPE_END_PROTECT(floordiv)
- return Py_BuildValue("(dd)", floordiv, mod);
+ double vx, wx;
+ double div, mod, floordiv;
+ CONVERT_TO_DOUBLE(v, vx);
+ CONVERT_TO_DOUBLE(w, wx);
+ if (wx == 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");
+ return NULL;
+ }
+ PyFPE_START_PROTECT("divmod", return 0)
+ mod = fmod(vx, wx);
+ /* fmod is typically exact, so vx-mod is *mathematically* an
+ exact multiple of wx. But this is fp arithmetic, and fp
+ vx - mod is an approximation; the result is that div may
+ not be an exact integral value after the division, although
+ it will always be very close to one.
+ */
+ div = (vx - mod) / wx;
+ if (mod) {
+ /* ensure the remainder has the same sign as the denominator */
+ if ((wx < 0) != (mod < 0)) {
+ mod += wx;
+ div -= 1.0;
+ }
+ }
+ else {
+ /* the remainder is zero, and in the presence of signed zeroes
+ fmod returns different results across platforms; ensure
+ it has the same sign as the denominator; we'd like to do
+ "mod = wx * 0.0", but that may get optimized away */
+ mod *= mod; /* hide "mod = +0" from optimizer */
+ if (wx < 0.0)
+ mod = -mod;
+ }
+ /* snap quotient to nearest integral value */
+ if (div) {
+ floordiv = floor(div);
+ if (div - floordiv > 0.5)
+ floordiv += 1.0;
+ }
+ else {
+ /* div is zero - get the same sign as the true quotient */
+ div *= div; /* hide "div = +0" from optimizers */
+ floordiv = div * vx / wx; /* zero w/ sign of vx/wx */
+ }
+ PyFPE_END_PROTECT(floordiv)
+ return Py_BuildValue("(dd)", floordiv, mod);
}
static PyObject *
float_floor_div(PyObject *v, PyObject *w)
{
- PyObject *t, *r;
-
- t = float_divmod(v, w);
- if (t == NULL || t == Py_NotImplemented)
- return t;
- assert(PyTuple_CheckExact(t));
- r = PyTuple_GET_ITEM(t, 0);
- Py_INCREF(r);
- Py_DECREF(t);
- return r;
+ PyObject *t, *r;
+
+ t = float_divmod(v, w);
+ if (t == NULL || t == Py_NotImplemented)
+ return t;
+ assert(PyTuple_CheckExact(t));
+ r = PyTuple_GET_ITEM(t, 0);
+ Py_INCREF(r);
+ Py_DECREF(t);
+ return r;
}
static PyObject *
float_pow(PyObject *v, PyObject *w, PyObject *z)
{
- double iv, iw, ix;
-
- if ((PyObject *)z != Py_None) {
- PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
- "allowed unless all arguments are integers");
- return NULL;
- }
-
- CONVERT_TO_DOUBLE(v, iv);
- CONVERT_TO_DOUBLE(w, iw);
-
- /* Sort out special cases here instead of relying on pow() */
- if (iw == 0) { /* v**0 is 1, even 0**0 */
- return PyFloat_FromDouble(1.0);
- }
- if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
- if (iw < 0.0) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "0.0 cannot be raised to a negative power");
- return NULL;
- }
- return PyFloat_FromDouble(0.0);
- }
- if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */
- return PyFloat_FromDouble(1.0);
- }
- if (iv < 0.0) {
- /* Whether this is an error is a mess, and bumps into libm
- * bugs so we have to figure it out ourselves.
- */
- if (iw != floor(iw)) {
- /* Negative numbers raised to fractional powers
- * become complex.
- */
- return PyComplex_Type.tp_as_number->nb_power(v, w, z);
- }
- /* iw is an exact integer, albeit perhaps a very large one.
- * -1 raised to an exact integer should never be exceptional.
- * Alas, some libms (chiefly glibc as of early 2003) return
- * NaN and set EDOM on pow(-1, large_int) if the int doesn't
- * happen to be representable in a *C* integer. That's a
- * bug; we let that slide in math.pow() (which currently
- * reflects all platform accidents), but not for Python's **.
- */
- if (iv == -1.0 && Py_IS_FINITE(iw)) {
- /* Return 1 if iw is even, -1 if iw is odd; there's
- * no guarantee that any C integral type is big
- * enough to hold iw, so we have to check this
- * indirectly.
- */
- ix = floor(iw * 0.5) * 2.0;
- return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
- }
- /* Else iv != -1.0, and overflow or underflow are possible.
- * Unless we're to write pow() ourselves, we have to trust
- * the platform to do this correctly.
- */
- }
- errno = 0;
- PyFPE_START_PROTECT("pow", return NULL)
- ix = pow(iv, iw);
- PyFPE_END_PROTECT(ix)
- Py_ADJUST_ERANGE1(ix);
- if (errno != 0) {
- /* We don't expect any errno value other than ERANGE, but
- * the range of libm bugs appears unbounded.
- */
- PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
- PyExc_ValueError);
- return NULL;
- }
- return PyFloat_FromDouble(ix);
+ double iv, iw, ix;
+
+ if ((PyObject *)z != Py_None) {
+ PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "
+ "allowed unless all arguments are integers");
+ return NULL;
+ }
+
+ CONVERT_TO_DOUBLE(v, iv);
+ CONVERT_TO_DOUBLE(w, iw);
+
+ /* Sort out special cases here instead of relying on pow() */
+ if (iw == 0) { /* v**0 is 1, even 0**0 */
+ return PyFloat_FromDouble(1.0);
+ }
+ if (iv == 0.0) { /* 0**w is error if w<0, else 1 */
+ if (iw < 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "0.0 cannot be raised to a negative power");
+ return NULL;
+ }
+ return PyFloat_FromDouble(0.0);
+ }
+ if (iv == 1.0) { /* 1**w is 1, even 1**inf and 1**nan */
+ return PyFloat_FromDouble(1.0);
+ }
+ if (iv < 0.0) {
+ /* Whether this is an error is a mess, and bumps into libm
+ * bugs so we have to figure it out ourselves.
+ */
+ if (iw != floor(iw)) {
+ /* Negative numbers raised to fractional powers
+ * become complex.
+ */
+ return PyComplex_Type.tp_as_number->nb_power(v, w, z);
+ }
+ /* iw is an exact integer, albeit perhaps a very large one.
+ * -1 raised to an exact integer should never be exceptional.
+ * Alas, some libms (chiefly glibc as of early 2003) return
+ * NaN and set EDOM on pow(-1, large_int) if the int doesn't
+ * happen to be representable in a *C* integer. That's a
+ * bug; we let that slide in math.pow() (which currently
+ * reflects all platform accidents), but not for Python's **.
+ */
+ if (iv == -1.0 && Py_IS_FINITE(iw)) {
+ /* Return 1 if iw is even, -1 if iw is odd; there's
+ * no guarantee that any C integral type is big
+ * enough to hold iw, so we have to check this
+ * indirectly.
+ */
+ ix = floor(iw * 0.5) * 2.0;
+ return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);
+ }
+ /* Else iv != -1.0, and overflow or underflow are possible.
+ * Unless we're to write pow() ourselves, we have to trust
+ * the platform to do this correctly.
+ */
+ }
+ errno = 0;
+ PyFPE_START_PROTECT("pow", return NULL)
+ ix = pow(iv, iw);
+ PyFPE_END_PROTECT(ix)
+ Py_ADJUST_ERANGE1(ix);
+ if (errno != 0) {
+ /* We don't expect any errno value other than ERANGE, but
+ * the range of libm bugs appears unbounded.
+ */
+ PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
+ PyExc_ValueError);
+ return NULL;
+ }
+ return PyFloat_FromDouble(ix);
}
static PyObject *
float_neg(PyFloatObject *v)
{
- return PyFloat_FromDouble(-v->ob_fval);
+ return PyFloat_FromDouble(-v->ob_fval);
}
static PyObject *
float_abs(PyFloatObject *v)
{
- return PyFloat_FromDouble(fabs(v->ob_fval));
+ return PyFloat_FromDouble(fabs(v->ob_fval));
}
static int
float_bool(PyFloatObject *v)
{
- return v->ob_fval != 0.0;
+ return v->ob_fval != 0.0;
}
static PyObject *
float_is_integer(PyObject *v)
{
- double x = PyFloat_AsDouble(v);
- PyObject *o;
-
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- if (!Py_IS_FINITE(x))
- Py_RETURN_FALSE;
- errno = 0;
- PyFPE_START_PROTECT("is_integer", return NULL)
- o = (floor(x) == x) ? Py_True : Py_False;
- PyFPE_END_PROTECT(x)
- if (errno != 0) {
- PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
- PyExc_ValueError);
- return NULL;
- }
- Py_INCREF(o);
- return o;
+ double x = PyFloat_AsDouble(v);
+ PyObject *o;
+
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ if (!Py_IS_FINITE(x))
+ Py_RETURN_FALSE;
+ errno = 0;
+ PyFPE_START_PROTECT("is_integer", return NULL)
+ o = (floor(x) == x) ? Py_True : Py_False;
+ PyFPE_END_PROTECT(x)
+ if (errno != 0) {
+ PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
+ PyExc_ValueError);
+ return NULL;
+ }
+ Py_INCREF(o);
+ return o;
}
#if 0
static PyObject *
float_is_inf(PyObject *v)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)Py_IS_INFINITY(x));
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_INFINITY(x));
}
static PyObject *
float_is_nan(PyObject *v)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)Py_IS_NAN(x));
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_NAN(x));
}
static PyObject *
float_is_finite(PyObject *v)
{
- double x = PyFloat_AsDouble(v);
- if (x == -1.0 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)Py_IS_FINITE(x));
+ double x = PyFloat_AsDouble(v);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)Py_IS_FINITE(x));
}
#endif
static PyObject *
float_trunc(PyObject *v)
{
- double x = PyFloat_AsDouble(v);
- double wholepart; /* integral portion of x, rounded toward 0 */
-
- (void)modf(x, &wholepart);
- /* Try to get out cheap if this fits in a Python int. The attempt
- * to cast to long must be protected, as C doesn't define what
- * happens if the double is too big to fit in a long. Some rare
- * systems raise an exception then (RISCOS was mentioned as one,
- * and someone using a non-default option on Sun also bumped into
- * that). Note that checking for >= and <= LONG_{MIN,MAX} would
- * still be vulnerable: if a long has more bits of precision than
- * a double, casting MIN/MAX to double may yield an approximation,
- * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
- * yield true from the C expression wholepart<=LONG_MAX, despite
- * that wholepart is actually greater than LONG_MAX.
- */
- if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
- const long aslong = (long)wholepart;
- return PyLong_FromLong(aslong);
- }
- return PyLong_FromDouble(wholepart);
+ double x = PyFloat_AsDouble(v);
+ double wholepart; /* integral portion of x, rounded toward 0 */
+
+ (void)modf(x, &wholepart);
+ /* Try to get out cheap if this fits in a Python int. The attempt
+ * to cast to long must be protected, as C doesn't define what
+ * happens if the double is too big to fit in a long. Some rare
+ * systems raise an exception then (RISCOS was mentioned as one,
+ * and someone using a non-default option on Sun also bumped into
+ * that). Note that checking for >= and <= LONG_{MIN,MAX} would
+ * still be vulnerable: if a long has more bits of precision than
+ * a double, casting MIN/MAX to double may yield an approximation,
+ * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would
+ * yield true from the C expression wholepart<=LONG_MAX, despite
+ * that wholepart is actually greater than LONG_MAX.
+ */
+ if (LONG_MIN < wholepart && wholepart < LONG_MAX) {
+ const long aslong = (long)wholepart;
+ return PyLong_FromLong(aslong);
+ }
+ return PyLong_FromDouble(wholepart);
}
/* double_round: rounds a finite double to the closest multiple of
@@ -860,49 +860,49 @@ float_trunc(PyObject *v)
static PyObject *
double_round(double x, int ndigits) {
- double rounded;
- Py_ssize_t buflen, mybuflen=100;
- char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf;
- int decpt, sign;
- PyObject *result = NULL;
-
- /* round to a decimal string */
- buf = _Py_dg_dtoa(x, 3, ndigits, &decpt, &sign, &buf_end);
- if (buf == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- /* Get new buffer if shortbuf is too small. Space needed <= buf_end -
- buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */
- buflen = buf_end - buf;
- if (buflen + 8 > mybuflen) {
- mybuflen = buflen+8;
- mybuf = (char *)PyMem_Malloc(mybuflen);
- if (mybuf == NULL) {
- PyErr_NoMemory();
- goto exit;
- }
- }
- /* copy buf to mybuf, adding exponent, sign and leading 0 */
- PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""),
- buf, decpt - (int)buflen);
-
- /* and convert the resulting string back to a double */
- errno = 0;
- rounded = _Py_dg_strtod(mybuf, NULL);
- if (errno == ERANGE && fabs(rounded) >= 1.)
- PyErr_SetString(PyExc_OverflowError,
- "rounded value too large to represent");
- else
- result = PyFloat_FromDouble(rounded);
-
- /* done computing value; now clean up */
- if (mybuf != shortbuf)
- PyMem_Free(mybuf);
+ double rounded;
+ Py_ssize_t buflen, mybuflen=100;
+ char *buf, *buf_end, shortbuf[100], *mybuf=shortbuf;
+ int decpt, sign;
+ PyObject *result = NULL;
+
+ /* round to a decimal string */
+ buf = _Py_dg_dtoa(x, 3, ndigits, &decpt, &sign, &buf_end);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ /* Get new buffer if shortbuf is too small. Space needed <= buf_end -
+ buf + 8: (1 extra for '0', 1 for sign, 5 for exp, 1 for '\0'). */
+ buflen = buf_end - buf;
+ if (buflen + 8 > mybuflen) {
+ mybuflen = buflen+8;
+ mybuf = (char *)PyMem_Malloc(mybuflen);
+ if (mybuf == NULL) {
+ PyErr_NoMemory();
+ goto exit;
+ }
+ }
+ /* copy buf to mybuf, adding exponent, sign and leading 0 */
+ PyOS_snprintf(mybuf, mybuflen, "%s0%se%d", (sign ? "-" : ""),
+ buf, decpt - (int)buflen);
+
+ /* and convert the resulting string back to a double */
+ errno = 0;
+ rounded = _Py_dg_strtod(mybuf, NULL);
+ if (errno == ERANGE && fabs(rounded) >= 1.)
+ PyErr_SetString(PyExc_OverflowError,
+ "rounded value too large to represent");
+ else
+ result = PyFloat_FromDouble(rounded);
+
+ /* done computing value; now clean up */
+ if (mybuf != shortbuf)
+ PyMem_Free(mybuf);
exit:
- _Py_dg_freedtoa(buf);
- return result;
+ _Py_dg_freedtoa(buf);
+ return result;
}
#else /* PY_NO_SHORT_FLOAT_REPR */
@@ -912,47 +912,47 @@ double_round(double x, int ndigits) {
static PyObject *
double_round(double x, int ndigits) {
- double pow1, pow2, y, z;
- if (ndigits >= 0) {
- if (ndigits > 22) {
- /* pow1 and pow2 are each safe from overflow, but
- pow1*pow2 ~= pow(10.0, ndigits) might overflow */
- pow1 = pow(10.0, (double)(ndigits-22));
- pow2 = 1e22;
- }
- else {
- pow1 = pow(10.0, (double)ndigits);
- pow2 = 1.0;
- }
- y = (x*pow1)*pow2;
- /* if y overflows, then rounded value is exactly x */
- if (!Py_IS_FINITE(y))
- return PyFloat_FromDouble(x);
- }
- else {
- pow1 = pow(10.0, (double)-ndigits);
- pow2 = 1.0; /* unused; silences a gcc compiler warning */
- y = x / pow1;
- }
-
- z = round(y);
- if (fabs(y-z) == 0.5)
- /* halfway between two integers; use round-half-even */
- z = 2.0*round(y/2.0);
-
- if (ndigits >= 0)
- z = (z / pow2) / pow1;
- else
- z *= pow1;
-
- /* if computation resulted in overflow, raise OverflowError */
- if (!Py_IS_FINITE(z)) {
- PyErr_SetString(PyExc_OverflowError,
- "overflow occurred during round");
- return NULL;
- }
-
- return PyFloat_FromDouble(z);
+ double pow1, pow2, y, z;
+ if (ndigits >= 0) {
+ if (ndigits > 22) {
+ /* pow1 and pow2 are each safe from overflow, but
+ pow1*pow2 ~= pow(10.0, ndigits) might overflow */
+ pow1 = pow(10.0, (double)(ndigits-22));
+ pow2 = 1e22;
+ }
+ else {
+ pow1 = pow(10.0, (double)ndigits);
+ pow2 = 1.0;
+ }
+ y = (x*pow1)*pow2;
+ /* if y overflows, then rounded value is exactly x */
+ if (!Py_IS_FINITE(y))
+ return PyFloat_FromDouble(x);
+ }
+ else {
+ pow1 = pow(10.0, (double)-ndigits);
+ pow2 = 1.0; /* unused; silences a gcc compiler warning */
+ y = x / pow1;
+ }
+
+ z = round(y);
+ if (fabs(y-z) == 0.5)
+ /* halfway between two integers; use round-half-even */
+ z = 2.0*round(y/2.0);
+
+ if (ndigits >= 0)
+ z = (z / pow2) / pow1;
+ else
+ z *= pow1;
+
+ /* if computation resulted in overflow, raise OverflowError */
+ if (!Py_IS_FINITE(z)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "overflow occurred during round");
+ return NULL;
+ }
+
+ return PyFloat_FromDouble(z);
}
#endif /* PY_NO_SHORT_FLOAT_REPR */
@@ -962,45 +962,45 @@ double_round(double x, int ndigits) {
static PyObject *
float_round(PyObject *v, PyObject *args)
{
- double x, rounded;
- PyObject *o_ndigits = NULL;
- Py_ssize_t ndigits;
-
- x = PyFloat_AsDouble(v);
- if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
- return NULL;
- if (o_ndigits == NULL) {
- /* single-argument round: round to nearest integer */
- rounded = round(x);
- if (fabs(x-rounded) == 0.5)
- /* halfway case: round to even */
- rounded = 2.0*round(x/2.0);
- return PyLong_FromDouble(rounded);
- }
-
- /* interpret second argument as a Py_ssize_t; clips on overflow */
- ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
- if (ndigits == -1 && PyErr_Occurred())
- return NULL;
-
- /* nans and infinities round to themselves */
- if (!Py_IS_FINITE(x))
- return PyFloat_FromDouble(x);
-
- /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
- always rounds to itself. For ndigits < NDIGITS_MIN, x always
- rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */
+ double x, rounded;
+ PyObject *o_ndigits = NULL;
+ Py_ssize_t ndigits;
+
+ x = PyFloat_AsDouble(v);
+ if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
+ return NULL;
+ if (o_ndigits == NULL) {
+ /* single-argument round: round to nearest integer */
+ rounded = round(x);
+ if (fabs(x-rounded) == 0.5)
+ /* halfway case: round to even */
+ rounded = 2.0*round(x/2.0);
+ return PyLong_FromDouble(rounded);
+ }
+
+ /* interpret second argument as a Py_ssize_t; clips on overflow */
+ ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
+ if (ndigits == -1 && PyErr_Occurred())
+ return NULL;
+
+ /* nans and infinities round to themselves */
+ if (!Py_IS_FINITE(x))
+ return PyFloat_FromDouble(x);
+
+ /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
+ always rounds to itself. For ndigits < NDIGITS_MIN, x always
+ rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */
#define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103))
#define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103))
- if (ndigits > NDIGITS_MAX)
- /* return x */
- return PyFloat_FromDouble(x);
- else if (ndigits < NDIGITS_MIN)
- /* return 0.0, but with sign of x */
- return PyFloat_FromDouble(0.0*x);
- else
- /* finite x, and ndigits is not unreasonably large */
- return double_round(x, (int)ndigits);
+ if (ndigits > NDIGITS_MAX)
+ /* return x */
+ return PyFloat_FromDouble(x);
+ else if (ndigits < NDIGITS_MIN)
+ /* return 0.0, but with sign of x */
+ return PyFloat_FromDouble(0.0*x);
+ else
+ /* finite x, and ndigits is not unreasonably large */
+ return double_round(x, (int)ndigits);
#undef NDIGITS_MAX
#undef NDIGITS_MIN
}
@@ -1008,11 +1008,11 @@ float_round(PyObject *v, PyObject *args)
static PyObject *
float_float(PyObject *v)
{
- if (PyFloat_CheckExact(v))
- Py_INCREF(v);
- else
- v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
- return v;
+ if (PyFloat_CheckExact(v))
+ Py_INCREF(v);
+ else
+ v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
+ return v;
}
/* turn ASCII hex characters into integer values and vice versa */
@@ -1020,73 +1020,73 @@ float_float(PyObject *v)
static char
char_from_hex(int x)
{
- assert(0 <= x && x < 16);
- return "0123456789abcdef"[x];
+ assert(0 <= x && x < 16);
+ return "0123456789abcdef"[x];
}
static int
hex_from_char(char c) {
- int x;
- switch(c) {
- case '0':
- x = 0;
- break;
- case '1':
- x = 1;
- break;
- case '2':
- x = 2;
- break;
- case '3':
- x = 3;
- break;
- case '4':
- x = 4;
- break;
- case '5':
- x = 5;
- break;
- case '6':
- x = 6;
- break;
- case '7':
- x = 7;
- break;
- case '8':
- x = 8;
- break;
- case '9':
- x = 9;
- break;
- case 'a':
- case 'A':
- x = 10;
- break;
- case 'b':
- case 'B':
- x = 11;
- break;
- case 'c':
- case 'C':
- x = 12;
- break;
- case 'd':
- case 'D':
- x = 13;
- break;
- case 'e':
- case 'E':
- x = 14;
- break;
- case 'f':
- case 'F':
- x = 15;
- break;
- default:
- x = -1;
- break;
- }
- return x;
+ int x;
+ switch(c) {
+ case '0':
+ x = 0;
+ break;
+ case '1':
+ x = 1;
+ break;
+ case '2':
+ x = 2;
+ break;
+ case '3':
+ x = 3;
+ break;
+ case '4':
+ x = 4;
+ break;
+ case '5':
+ x = 5;
+ break;
+ case '6':
+ x = 6;
+ break;
+ case '7':
+ x = 7;
+ break;
+ case '8':
+ x = 8;
+ break;
+ case '9':
+ x = 9;
+ break;
+ case 'a':
+ case 'A':
+ x = 10;
+ break;
+ case 'b':
+ case 'B':
+ x = 11;
+ break;
+ case 'c':
+ case 'C':
+ x = 12;
+ break;
+ case 'd':
+ case 'D':
+ x = 13;
+ break;
+ case 'e':
+ case 'E':
+ x = 14;
+ break;
+ case 'f':
+ case 'F':
+ x = 15;
+ break;
+ default:
+ x = -1;
+ break;
+ }
+ return x;
}
/* convert a float to a hexadecimal string */
@@ -1098,54 +1098,54 @@ hex_from_char(char c) {
static PyObject *
float_hex(PyObject *v)
{
- double x, m;
- int e, shift, i, si, esign;
- /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
- trailing NUL byte. */
- char s[(TOHEX_NBITS-1)/4+3];
-
- CONVERT_TO_DOUBLE(v, x);
-
- if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
- return float_str((PyFloatObject *)v);
-
- if (x == 0.0) {
- if(copysign(1.0, x) == -1.0)
- return PyUnicode_FromString("-0x0.0p+0");
- else
- return PyUnicode_FromString("0x0.0p+0");
- }
-
- m = frexp(fabs(x), &e);
- shift = 1 - MAX(DBL_MIN_EXP - e, 0);
- m = ldexp(m, shift);
- e -= shift;
-
- si = 0;
- s[si] = char_from_hex((int)m);
- si++;
- m -= (int)m;
- s[si] = '.';
- si++;
- for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
- m *= 16.0;
- s[si] = char_from_hex((int)m);
- si++;
- m -= (int)m;
- }
- s[si] = '\0';
-
- if (e < 0) {
- esign = (int)'-';
- e = -e;
- }
- else
- esign = (int)'+';
-
- if (x < 0.0)
- return PyUnicode_FromFormat("-0x%sp%c%d", s, esign, e);
- else
- return PyUnicode_FromFormat("0x%sp%c%d", s, esign, e);
+ double x, m;
+ int e, shift, i, si, esign;
+ /* Space for 1+(TOHEX_NBITS-1)/4 digits, a decimal point, and the
+ trailing NUL byte. */
+ char s[(TOHEX_NBITS-1)/4+3];
+
+ CONVERT_TO_DOUBLE(v, x);
+
+ if (Py_IS_NAN(x) || Py_IS_INFINITY(x))
+ return float_str((PyFloatObject *)v);
+
+ if (x == 0.0) {
+ if(copysign(1.0, x) == -1.0)
+ return PyUnicode_FromString("-0x0.0p+0");
+ else
+ return PyUnicode_FromString("0x0.0p+0");
+ }
+
+ m = frexp(fabs(x), &e);
+ shift = 1 - MAX(DBL_MIN_EXP - e, 0);
+ m = ldexp(m, shift);
+ e -= shift;
+
+ si = 0;
+ s[si] = char_from_hex((int)m);
+ si++;
+ m -= (int)m;
+ s[si] = '.';
+ si++;
+ for (i=0; i < (TOHEX_NBITS-1)/4; i++) {
+ m *= 16.0;
+ s[si] = char_from_hex((int)m);
+ si++;
+ m -= (int)m;
+ }
+ s[si] = '\0';
+
+ if (e < 0) {
+ esign = (int)'-';
+ e = -e;
+ }
+ else
+ esign = (int)'+';
+
+ if (x < 0.0)
+ return PyUnicode_FromFormat("-0x%sp%c%d", s, esign, e);
+ else
+ return PyUnicode_FromFormat("0x%sp%c%d", s, esign, e);
}
PyDoc_STRVAR(float_hex_doc,
@@ -1162,242 +1162,242 @@ Return a hexadecimal representation of a floating-point number.\n\
static PyObject *
float_fromhex(PyObject *cls, PyObject *arg)
{
- PyObject *result_as_float, *result;
- double x;
- long exp, top_exp, lsb, key_digit;
- char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
- int half_eps, digit, round_up, negate=0;
- Py_ssize_t length, ndigits, fdigits, i;
-
- /*
- * For the sake of simplicity and correctness, we impose an artificial
- * limit on ndigits, the total number of hex digits in the coefficient
- * The limit is chosen to ensure that, writing exp for the exponent,
- *
- * (1) if exp > LONG_MAX/2 then the value of the hex string is
- * guaranteed to overflow (provided it's nonzero)
- *
- * (2) if exp < LONG_MIN/2 then the value of the hex string is
- * guaranteed to underflow to 0.
- *
- * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of
- * overflow in the calculation of exp and top_exp below.
- *
- * More specifically, ndigits is assumed to satisfy the following
- * inequalities:
- *
- * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2
- * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP
- *
- * If either of these inequalities is not satisfied, a ValueError is
- * raised. Otherwise, write x for the value of the hex string, and
- * assume x is nonzero. Then
- *
- * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits).
- *
- * Now if exp > LONG_MAX/2 then:
- *
- * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP)
- * = DBL_MAX_EXP
- *
- * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C
- * double, so overflows. If exp < LONG_MIN/2, then
- *
- * exp + 4*ndigits <= LONG_MIN/2 - 1 + (
- * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2)
- * = DBL_MIN_EXP - DBL_MANT_DIG - 1
- *
- * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0
- * when converted to a C double.
- *
- * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both
- * exp+4*ndigits and exp-4*ndigits are within the range of a long.
- */
-
- s = _PyUnicode_AsStringAndSize(arg, &length);
- if (s == NULL)
- return NULL;
- s_end = s + length;
-
- /********************
- * Parse the string *
- ********************/
-
- /* leading whitespace */
- while (Py_ISSPACE(*s))
- s++;
-
- /* infinities and nans */
- x = _Py_parse_inf_or_nan(s, &coeff_end);
- if (coeff_end != s) {
- s = coeff_end;
- goto finished;
- }
-
- /* optional sign */
- if (*s == '-') {
- s++;
- negate = 1;
- }
- else if (*s == '+')
- s++;
-
- /* [0x] */
- s_store = s;
- if (*s == '0') {
- s++;
- if (*s == 'x' || *s == 'X')
- s++;
- else
- s = s_store;
- }
-
- /* coefficient: <integer> [. <fraction>] */
- coeff_start = s;
- while (hex_from_char(*s) >= 0)
- s++;
- s_store = s;
- if (*s == '.') {
- s++;
- while (hex_from_char(*s) >= 0)
- s++;
- coeff_end = s-1;
- }
- else
- coeff_end = s;
-
- /* ndigits = total # of hex digits; fdigits = # after point */
- ndigits = coeff_end - coeff_start;
- fdigits = coeff_end - s_store;
- if (ndigits == 0)
- goto parse_error;
- if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
- LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
- goto insane_length_error;
-
- /* [p <exponent>] */
- if (*s == 'p' || *s == 'P') {
- s++;
- exp_start = s;
- if (*s == '-' || *s == '+')
- s++;
- if (!('0' <= *s && *s <= '9'))
- goto parse_error;
- s++;
- while ('0' <= *s && *s <= '9')
- s++;
- exp = strtol(exp_start, NULL, 10);
- }
- else
- exp = 0;
+ PyObject *result_as_float, *result;
+ double x;
+ long exp, top_exp, lsb, key_digit;
+ char *s, *coeff_start, *s_store, *coeff_end, *exp_start, *s_end;
+ int half_eps, digit, round_up, negate=0;
+ Py_ssize_t length, ndigits, fdigits, i;
+
+ /*
+ * For the sake of simplicity and correctness, we impose an artificial
+ * limit on ndigits, the total number of hex digits in the coefficient
+ * The limit is chosen to ensure that, writing exp for the exponent,
+ *
+ * (1) if exp > LONG_MAX/2 then the value of the hex string is
+ * guaranteed to overflow (provided it's nonzero)
+ *
+ * (2) if exp < LONG_MIN/2 then the value of the hex string is
+ * guaranteed to underflow to 0.
+ *
+ * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of
+ * overflow in the calculation of exp and top_exp below.
+ *
+ * More specifically, ndigits is assumed to satisfy the following
+ * inequalities:
+ *
+ * 4*ndigits <= DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2
+ * 4*ndigits <= LONG_MAX/2 + 1 - DBL_MAX_EXP
+ *
+ * If either of these inequalities is not satisfied, a ValueError is
+ * raised. Otherwise, write x for the value of the hex string, and
+ * assume x is nonzero. Then
+ *
+ * 2**(exp-4*ndigits) <= |x| < 2**(exp+4*ndigits).
+ *
+ * Now if exp > LONG_MAX/2 then:
+ *
+ * exp - 4*ndigits >= LONG_MAX/2 + 1 - (LONG_MAX/2 + 1 - DBL_MAX_EXP)
+ * = DBL_MAX_EXP
+ *
+ * so |x| >= 2**DBL_MAX_EXP, which is too large to be stored in C
+ * double, so overflows. If exp < LONG_MIN/2, then
+ *
+ * exp + 4*ndigits <= LONG_MIN/2 - 1 + (
+ * DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2)
+ * = DBL_MIN_EXP - DBL_MANT_DIG - 1
+ *
+ * and so |x| < 2**(DBL_MIN_EXP-DBL_MANT_DIG-1), hence underflows to 0
+ * when converted to a C double.
+ *
+ * It's easy to show that if LONG_MIN/2 <= exp <= LONG_MAX/2 then both
+ * exp+4*ndigits and exp-4*ndigits are within the range of a long.
+ */
+
+ s = _PyUnicode_AsStringAndSize(arg, &length);
+ if (s == NULL)
+ return NULL;
+ s_end = s + length;
+
+ /********************
+ * Parse the string *
+ ********************/
+
+ /* leading whitespace */
+ while (Py_ISSPACE(*s))
+ s++;
+
+ /* infinities and nans */
+ x = _Py_parse_inf_or_nan(s, &coeff_end);
+ if (coeff_end != s) {
+ s = coeff_end;
+ goto finished;
+ }
+
+ /* optional sign */
+ if (*s == '-') {
+ s++;
+ negate = 1;
+ }
+ else if (*s == '+')
+ s++;
+
+ /* [0x] */
+ s_store = s;
+ if (*s == '0') {
+ s++;
+ if (*s == 'x' || *s == 'X')
+ s++;
+ else
+ s = s_store;
+ }
+
+ /* coefficient: <integer> [. <fraction>] */
+ coeff_start = s;
+ while (hex_from_char(*s) >= 0)
+ s++;
+ s_store = s;
+ if (*s == '.') {
+ s++;
+ while (hex_from_char(*s) >= 0)
+ s++;
+ coeff_end = s-1;
+ }
+ else
+ coeff_end = s;
+
+ /* ndigits = total # of hex digits; fdigits = # after point */
+ ndigits = coeff_end - coeff_start;
+ fdigits = coeff_end - s_store;
+ if (ndigits == 0)
+ goto parse_error;
+ if (ndigits > MIN(DBL_MIN_EXP - DBL_MANT_DIG - LONG_MIN/2,
+ LONG_MAX/2 + 1 - DBL_MAX_EXP)/4)
+ goto insane_length_error;
+
+ /* [p <exponent>] */
+ if (*s == 'p' || *s == 'P') {
+ s++;
+ exp_start = s;
+ if (*s == '-' || *s == '+')
+ s++;
+ if (!('0' <= *s && *s <= '9'))
+ goto parse_error;
+ s++;
+ while ('0' <= *s && *s <= '9')
+ s++;
+ exp = strtol(exp_start, NULL, 10);
+ }
+ else
+ exp = 0;
/* for 0 <= j < ndigits, HEX_DIGIT(j) gives the jth most significant digit */
-#define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \
- coeff_end-(j) : \
- coeff_end-1-(j)))
-
- /*******************************************
- * Compute rounded value of the hex string *
- *******************************************/
-
- /* Discard leading zeros, and catch extreme overflow and underflow */
- while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
- ndigits--;
- if (ndigits == 0 || exp < LONG_MIN/2) {
- x = 0.0;
- goto finished;
- }
- if (exp > LONG_MAX/2)
- goto overflow_error;
-
- /* Adjust exponent for fractional part. */
- exp = exp - 4*((long)fdigits);
-
- /* top_exp = 1 more than exponent of most sig. bit of coefficient */
- top_exp = exp + 4*((long)ndigits - 1);
- for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
- top_exp++;
-
- /* catch almost all nonextreme cases of overflow and underflow here */
- if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
- x = 0.0;
- goto finished;
- }
- if (top_exp > DBL_MAX_EXP)
- goto overflow_error;
-
- /* lsb = exponent of least significant bit of the *rounded* value.
- This is top_exp - DBL_MANT_DIG unless result is subnormal. */
- lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
-
- x = 0.0;
- if (exp >= lsb) {
- /* no rounding required */
- for (i = ndigits-1; i >= 0; i--)
- x = 16.0*x + HEX_DIGIT(i);
- x = ldexp(x, (int)(exp));
- goto finished;
- }
- /* rounding required. key_digit is the index of the hex digit
- containing the first bit to be rounded away. */
- half_eps = 1 << (int)((lsb - exp - 1) % 4);
- key_digit = (lsb - exp - 1) / 4;
- for (i = ndigits-1; i > key_digit; i--)
- x = 16.0*x + HEX_DIGIT(i);
- digit = HEX_DIGIT(key_digit);
- x = 16.0*x + (double)(digit & (16-2*half_eps));
-
- /* round-half-even: round up if bit lsb-1 is 1 and at least one of
- bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
- if ((digit & half_eps) != 0) {
- round_up = 0;
- if ((digit & (3*half_eps-1)) != 0 ||
- (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0))
- round_up = 1;
- else
- for (i = key_digit-1; i >= 0; i--)
- if (HEX_DIGIT(i) != 0) {
- round_up = 1;
- break;
- }
- if (round_up == 1) {
- x += 2*half_eps;
- if (top_exp == DBL_MAX_EXP &&
- x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
- /* overflow corner case: pre-rounded value <
- 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */
- goto overflow_error;
- }
- }
- x = ldexp(x, (int)(exp+4*key_digit));
+#define HEX_DIGIT(j) hex_from_char(*((j) < fdigits ? \
+ coeff_end-(j) : \
+ coeff_end-1-(j)))
+
+ /*******************************************
+ * Compute rounded value of the hex string *
+ *******************************************/
+
+ /* Discard leading zeros, and catch extreme overflow and underflow */
+ while (ndigits > 0 && HEX_DIGIT(ndigits-1) == 0)
+ ndigits--;
+ if (ndigits == 0 || exp < LONG_MIN/2) {
+ x = 0.0;
+ goto finished;
+ }
+ if (exp > LONG_MAX/2)
+ goto overflow_error;
+
+ /* Adjust exponent for fractional part. */
+ exp = exp - 4*((long)fdigits);
+
+ /* top_exp = 1 more than exponent of most sig. bit of coefficient */
+ top_exp = exp + 4*((long)ndigits - 1);
+ for (digit = HEX_DIGIT(ndigits-1); digit != 0; digit /= 2)
+ top_exp++;
+
+ /* catch almost all nonextreme cases of overflow and underflow here */
+ if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) {
+ x = 0.0;
+ goto finished;
+ }
+ if (top_exp > DBL_MAX_EXP)
+ goto overflow_error;
+
+ /* lsb = exponent of least significant bit of the *rounded* value.
+ This is top_exp - DBL_MANT_DIG unless result is subnormal. */
+ lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG;
+
+ x = 0.0;
+ if (exp >= lsb) {
+ /* no rounding required */
+ for (i = ndigits-1; i >= 0; i--)
+ x = 16.0*x + HEX_DIGIT(i);
+ x = ldexp(x, (int)(exp));
+ goto finished;
+ }
+ /* rounding required. key_digit is the index of the hex digit
+ containing the first bit to be rounded away. */
+ half_eps = 1 << (int)((lsb - exp - 1) % 4);
+ key_digit = (lsb - exp - 1) / 4;
+ for (i = ndigits-1; i > key_digit; i--)
+ x = 16.0*x + HEX_DIGIT(i);
+ digit = HEX_DIGIT(key_digit);
+ x = 16.0*x + (double)(digit & (16-2*half_eps));
+
+ /* round-half-even: round up if bit lsb-1 is 1 and at least one of
+ bits lsb, lsb-2, lsb-3, lsb-4, ... is 1. */
+ if ((digit & half_eps) != 0) {
+ round_up = 0;
+ if ((digit & (3*half_eps-1)) != 0 ||
+ (half_eps == 8 && (HEX_DIGIT(key_digit+1) & 1) != 0))
+ round_up = 1;
+ else
+ for (i = key_digit-1; i >= 0; i--)
+ if (HEX_DIGIT(i) != 0) {
+ round_up = 1;
+ break;
+ }
+ if (round_up == 1) {
+ x += 2*half_eps;
+ if (top_exp == DBL_MAX_EXP &&
+ x == ldexp((double)(2*half_eps), DBL_MANT_DIG))
+ /* overflow corner case: pre-rounded value <
+ 2**DBL_MAX_EXP; rounded=2**DBL_MAX_EXP. */
+ goto overflow_error;
+ }
+ }
+ x = ldexp(x, (int)(exp+4*key_digit));
finished:
- /* optional trailing whitespace leading to the end of the string */
- while (Py_ISSPACE(*s))
- s++;
- if (s != s_end)
- goto parse_error;
- result_as_float = Py_BuildValue("(d)", negate ? -x : x);
- if (result_as_float == NULL)
- return NULL;
- result = PyObject_CallObject(cls, result_as_float);
- Py_DECREF(result_as_float);
- return result;
+ /* optional trailing whitespace leading to the end of the string */
+ while (Py_ISSPACE(*s))
+ s++;
+ if (s != s_end)
+ goto parse_error;
+ result_as_float = Py_BuildValue("(d)", negate ? -x : x);
+ if (result_as_float == NULL)
+ return NULL;
+ result = PyObject_CallObject(cls, result_as_float);
+ Py_DECREF(result_as_float);
+ return result;
overflow_error:
- PyErr_SetString(PyExc_OverflowError,
- "hexadecimal value too large to represent as a float");
- return NULL;
+ PyErr_SetString(PyExc_OverflowError,
+ "hexadecimal value too large to represent as a float");
+ return NULL;
parse_error:
- PyErr_SetString(PyExc_ValueError,
- "invalid hexadecimal floating-point string");
- return NULL;
+ PyErr_SetString(PyExc_ValueError,
+ "invalid hexadecimal floating-point string");
+ return NULL;
insane_length_error:
- PyErr_SetString(PyExc_ValueError,
- "hexadecimal string too long to convert");
- return NULL;
+ PyErr_SetString(PyExc_ValueError,
+ "hexadecimal string too long to convert");
+ return NULL;
}
PyDoc_STRVAR(float_fromhex_doc,
@@ -1413,79 +1413,79 @@ Create a floating-point number from a hexadecimal string.\n\
static PyObject *
float_as_integer_ratio(PyObject *v, PyObject *unused)
{
- double self;
- double float_part;
- int exponent;
- int i;
-
- PyObject *prev;
- PyObject *py_exponent = NULL;
- PyObject *numerator = NULL;
- PyObject *denominator = NULL;
- PyObject *result_pair = NULL;
- PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
+ double self;
+ double float_part;
+ int exponent;
+ int i;
+
+ PyObject *prev;
+ PyObject *py_exponent = NULL;
+ PyObject *numerator = NULL;
+ PyObject *denominator = NULL;
+ PyObject *result_pair = NULL;
+ PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
#define INPLACE_UPDATE(obj, call) \
- prev = obj; \
- obj = call; \
- Py_DECREF(prev); \
+ prev = obj; \
+ obj = call; \
+ Py_DECREF(prev); \
- CONVERT_TO_DOUBLE(v, self);
+ CONVERT_TO_DOUBLE(v, self);
- if (Py_IS_INFINITY(self)) {
- PyErr_SetString(PyExc_OverflowError,
- "Cannot pass infinity to float.as_integer_ratio.");
- return NULL;
- }
+ if (Py_IS_INFINITY(self)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Cannot pass infinity to float.as_integer_ratio.");
+ return NULL;
+ }
#ifdef Py_NAN
- if (Py_IS_NAN(self)) {
- PyErr_SetString(PyExc_ValueError,
- "Cannot pass NaN to float.as_integer_ratio.");
- return NULL;
- }
+ if (Py_IS_NAN(self)) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot pass NaN to float.as_integer_ratio.");
+ return NULL;
+ }
#endif
- PyFPE_START_PROTECT("as_integer_ratio", goto error);
- float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
- PyFPE_END_PROTECT(float_part);
-
- for (i=0; i<300 && float_part != floor(float_part) ; i++) {
- float_part *= 2.0;
- exponent--;
- }
- /* self == float_part * 2**exponent exactly and float_part is integral.
- If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
- to be truncated by PyLong_FromDouble(). */
-
- numerator = PyLong_FromDouble(float_part);
- if (numerator == NULL) goto error;
-
- /* fold in 2**exponent */
- denominator = PyLong_FromLong(1);
- py_exponent = PyLong_FromLong(labs((long)exponent));
- if (py_exponent == NULL) goto error;
- INPLACE_UPDATE(py_exponent,
- long_methods->nb_lshift(denominator, py_exponent));
- if (py_exponent == NULL) goto error;
- if (exponent > 0) {
- INPLACE_UPDATE(numerator,
- long_methods->nb_multiply(numerator, py_exponent));
- if (numerator == NULL) goto error;
- }
- else {
- Py_DECREF(denominator);
- denominator = py_exponent;
- py_exponent = NULL;
- }
-
- result_pair = PyTuple_Pack(2, numerator, denominator);
+ PyFPE_START_PROTECT("as_integer_ratio", goto error);
+ float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
+ PyFPE_END_PROTECT(float_part);
+
+ for (i=0; i<300 && float_part != floor(float_part) ; i++) {
+ float_part *= 2.0;
+ exponent--;
+ }
+ /* self == float_part * 2**exponent exactly and float_part is integral.
+ If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
+ to be truncated by PyLong_FromDouble(). */
+
+ numerator = PyLong_FromDouble(float_part);
+ if (numerator == NULL) goto error;
+
+ /* fold in 2**exponent */
+ denominator = PyLong_FromLong(1);
+ py_exponent = PyLong_FromLong(labs((long)exponent));
+ if (py_exponent == NULL) goto error;
+ INPLACE_UPDATE(py_exponent,
+ long_methods->nb_lshift(denominator, py_exponent));
+ if (py_exponent == NULL) goto error;
+ if (exponent > 0) {
+ INPLACE_UPDATE(numerator,
+ long_methods->nb_multiply(numerator, py_exponent));
+ if (numerator == NULL) goto error;
+ }
+ else {
+ Py_DECREF(denominator);
+ denominator = py_exponent;
+ py_exponent = NULL;
+ }
+
+ result_pair = PyTuple_Pack(2, numerator, denominator);
#undef INPLACE_UPDATE
error:
- Py_XDECREF(py_exponent);
- Py_XDECREF(denominator);
- Py_XDECREF(numerator);
- return result_pair;
+ Py_XDECREF(py_exponent);
+ Py_XDECREF(denominator);
+ Py_XDECREF(numerator);
+ return result_pair;
}
PyDoc_STRVAR(float_as_integer_ratio_doc,
@@ -1509,18 +1509,18 @@ float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *x = Py_False; /* Integer zero */
- static char *kwlist[] = {"x", 0};
-
- if (type != &PyFloat_Type)
- return float_subtype_new(type, args, kwds); /* Wimp out */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
- return NULL;
- /* If it's a string, but not a string subclass, use
- PyFloat_FromString. */
- if (PyUnicode_CheckExact(x))
- return PyFloat_FromString(x);
- return PyNumber_Float(x);
+ PyObject *x = Py_False; /* Integer zero */
+ static char *kwlist[] = {"x", 0};
+
+ if (type != &PyFloat_Type)
+ return float_subtype_new(type, args, kwds); /* Wimp out */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
+ return NULL;
+ /* If it's a string, but not a string subclass, use
+ PyFloat_FromString. */
+ if (PyUnicode_CheckExact(x))
+ return PyFloat_FromString(x);
+ return PyNumber_Float(x);
}
/* Wimpy, slow approach to tp_new calls for subtypes of float:
@@ -1531,33 +1531,33 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *tmp, *newobj;
-
- assert(PyType_IsSubtype(type, &PyFloat_Type));
- tmp = float_new(&PyFloat_Type, args, kwds);
- if (tmp == NULL)
- return NULL;
- assert(PyFloat_CheckExact(tmp));
- newobj = type->tp_alloc(type, 0);
- if (newobj == NULL) {
- Py_DECREF(tmp);
- return NULL;
- }
- ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
- Py_DECREF(tmp);
- return newobj;
+ PyObject *tmp, *newobj;
+
+ assert(PyType_IsSubtype(type, &PyFloat_Type));
+ tmp = float_new(&PyFloat_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyFloat_CheckExact(tmp));
+ newobj = type->tp_alloc(type, 0);
+ if (newobj == NULL) {
+ Py_DECREF(tmp);
+ return NULL;
+ }
+ ((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
+ Py_DECREF(tmp);
+ return newobj;
}
static PyObject *
float_getnewargs(PyFloatObject *v)
{
- return Py_BuildValue("(d)", v->ob_fval);
+ return Py_BuildValue("(d)", v->ob_fval);
}
/* this is for the benefit of the pack/unpack routines below */
typedef enum {
- unknown_format, ieee_big_endian_format, ieee_little_endian_format
+ unknown_format, ieee_big_endian_format, ieee_little_endian_format
} float_format_type;
static float_format_type double_format, float_format;
@@ -1566,42 +1566,42 @@ static float_format_type detected_double_format, detected_float_format;
static PyObject *
float_getformat(PyTypeObject *v, PyObject* arg)
{
- char* s;
- float_format_type r;
-
- if (!PyUnicode_Check(arg)) {
- PyErr_Format(PyExc_TypeError,
- "__getformat__() argument must be string, not %.500s",
- Py_TYPE(arg)->tp_name);
- return NULL;
- }
- s = _PyUnicode_AsString(arg);
- if (s == NULL)
- return NULL;
- if (strcmp(s, "double") == 0) {
- r = double_format;
- }
- else if (strcmp(s, "float") == 0) {
- r = float_format;
- }
- else {
- PyErr_SetString(PyExc_ValueError,
- "__getformat__() argument 1 must be "
- "'double' or 'float'");
- return NULL;
- }
-
- switch (r) {
- case unknown_format:
- return PyUnicode_FromString("unknown");
- case ieee_little_endian_format:
- return PyUnicode_FromString("IEEE, little-endian");
- case ieee_big_endian_format:
- return PyUnicode_FromString("IEEE, big-endian");
- default:
- Py_FatalError("insane float_format or double_format");
- return NULL;
- }
+ char* s;
+ float_format_type r;
+
+ if (!PyUnicode_Check(arg)) {
+ PyErr_Format(PyExc_TypeError,
+ "__getformat__() argument must be string, not %.500s",
+ Py_TYPE(arg)->tp_name);
+ return NULL;
+ }
+ s = _PyUnicode_AsString(arg);
+ if (s == NULL)
+ return NULL;
+ if (strcmp(s, "double") == 0) {
+ r = double_format;
+ }
+ else if (strcmp(s, "float") == 0) {
+ r = float_format;
+ }
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "__getformat__() argument 1 must be "
+ "'double' or 'float'");
+ return NULL;
+ }
+
+ switch (r) {
+ case unknown_format:
+ return PyUnicode_FromString("unknown");
+ case ieee_little_endian_format:
+ return PyUnicode_FromString("IEEE, little-endian");
+ case ieee_big_endian_format:
+ return PyUnicode_FromString("IEEE, big-endian");
+ default:
+ Py_FatalError("insane float_format or double_format");
+ return NULL;
+ }
}
PyDoc_STRVAR(float_getformat_doc,
@@ -1617,57 +1617,57 @@ PyDoc_STRVAR(float_getformat_doc,
static PyObject *
float_setformat(PyTypeObject *v, PyObject* args)
{
- char* typestr;
- char* format;
- float_format_type f;
- float_format_type detected;
- float_format_type *p;
-
- if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
- return NULL;
-
- if (strcmp(typestr, "double") == 0) {
- p = &double_format;
- detected = detected_double_format;
- }
- else if (strcmp(typestr, "float") == 0) {
- p = &float_format;
- detected = detected_float_format;
- }
- else {
- PyErr_SetString(PyExc_ValueError,
- "__setformat__() argument 1 must "
- "be 'double' or 'float'");
- return NULL;
- }
-
- if (strcmp(format, "unknown") == 0) {
- f = unknown_format;
- }
- else if (strcmp(format, "IEEE, little-endian") == 0) {
- f = ieee_little_endian_format;
- }
- else if (strcmp(format, "IEEE, big-endian") == 0) {
- f = ieee_big_endian_format;
- }
- else {
- PyErr_SetString(PyExc_ValueError,
- "__setformat__() argument 2 must be "
- "'unknown', 'IEEE, little-endian' or "
- "'IEEE, big-endian'");
- return NULL;
-
- }
-
- if (f != unknown_format && f != detected) {
- PyErr_Format(PyExc_ValueError,
- "can only set %s format to 'unknown' or the "
- "detected platform value", typestr);
- return NULL;
- }
-
- *p = f;
- Py_RETURN_NONE;
+ char* typestr;
+ char* format;
+ float_format_type f;
+ float_format_type detected;
+ float_format_type *p;
+
+ if (!PyArg_ParseTuple(args, "ss:__setformat__", &typestr, &format))
+ return NULL;
+
+ if (strcmp(typestr, "double") == 0) {
+ p = &double_format;
+ detected = detected_double_format;
+ }
+ else if (strcmp(typestr, "float") == 0) {
+ p = &float_format;
+ detected = detected_float_format;
+ }
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "__setformat__() argument 1 must "
+ "be 'double' or 'float'");
+ return NULL;
+ }
+
+ if (strcmp(format, "unknown") == 0) {
+ f = unknown_format;
+ }
+ else if (strcmp(format, "IEEE, little-endian") == 0) {
+ f = ieee_little_endian_format;
+ }
+ else if (strcmp(format, "IEEE, big-endian") == 0) {
+ f = ieee_big_endian_format;
+ }
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "__setformat__() argument 2 must be "
+ "'unknown', 'IEEE, little-endian' or "
+ "'IEEE, big-endian'");
+ return NULL;
+
+ }
+
+ if (f != unknown_format && f != detected) {
+ PyErr_Format(PyExc_ValueError,
+ "can only set %s format to 'unknown' or the "
+ "detected platform value", typestr);
+ return NULL;
+ }
+
+ *p = f;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(float_setformat_doc,
@@ -1686,19 +1686,19 @@ PyDoc_STRVAR(float_setformat_doc,
static PyObject *
float_getzero(PyObject *v, void *closure)
{
- return PyFloat_FromDouble(0.0);
+ return PyFloat_FromDouble(0.0);
}
static PyObject *
float__format__(PyObject *self, PyObject *args)
{
- PyObject *format_spec;
+ PyObject *format_spec;
- if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
- return _PyFloat_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
+ return NULL;
+ return _PyFloat_FormatAdvanced(self,
+ PyUnicode_AS_UNICODE(format_spec),
+ PyUnicode_GET_SIZE(format_spec));
}
PyDoc_STRVAR(float__format__doc,
@@ -1708,45 +1708,45 @@ PyDoc_STRVAR(float__format__doc,
static PyMethodDef float_methods[] = {
- {"conjugate", (PyCFunction)float_float, METH_NOARGS,
- "Returns self, the complex conjugate of any float."},
- {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS,
- "Returns the Integral closest to x between 0 and x."},
- {"__round__", (PyCFunction)float_round, METH_VARARGS,
- "Returns the Integral closest to x, rounding half toward even.\n"
- "When an argument is passed, works like built-in round(x, ndigits)."},
- {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS,
- float_as_integer_ratio_doc},
- {"fromhex", (PyCFunction)float_fromhex,
- METH_O|METH_CLASS, float_fromhex_doc},
- {"hex", (PyCFunction)float_hex,
- METH_NOARGS, float_hex_doc},
- {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS,
- "Returns True if the float is an integer."},
+ {"conjugate", (PyCFunction)float_float, METH_NOARGS,
+ "Returns self, the complex conjugate of any float."},
+ {"__trunc__", (PyCFunction)float_trunc, METH_NOARGS,
+ "Returns the Integral closest to x between 0 and x."},
+ {"__round__", (PyCFunction)float_round, METH_VARARGS,
+ "Returns the Integral closest to x, rounding half toward even.\n"
+ "When an argument is passed, works like built-in round(x, ndigits)."},
+ {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS,
+ float_as_integer_ratio_doc},
+ {"fromhex", (PyCFunction)float_fromhex,
+ METH_O|METH_CLASS, float_fromhex_doc},
+ {"hex", (PyCFunction)float_hex,
+ METH_NOARGS, float_hex_doc},
+ {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS,
+ "Returns True if the float is an integer."},
#if 0
- {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS,
- "Returns True if the float is positive or negative infinite."},
- {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS,
- "Returns True if the float is finite, neither infinite nor NaN."},
- {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS,
- "Returns True if the float is not a number (NaN)."},
+ {"is_inf", (PyCFunction)float_is_inf, METH_NOARGS,
+ "Returns True if the float is positive or negative infinite."},
+ {"is_finite", (PyCFunction)float_is_finite, METH_NOARGS,
+ "Returns True if the float is finite, neither infinite nor NaN."},
+ {"is_nan", (PyCFunction)float_is_nan, METH_NOARGS,
+ "Returns True if the float is not a number (NaN)."},
#endif
- {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
- {"__getformat__", (PyCFunction)float_getformat,
- METH_O|METH_CLASS, float_getformat_doc},
- {"__setformat__", (PyCFunction)float_setformat,
- METH_VARARGS|METH_CLASS, float_setformat_doc},
- {"__format__", (PyCFunction)float__format__,
- METH_VARARGS, float__format__doc},
- {NULL, NULL} /* sentinel */
+ {"__getnewargs__", (PyCFunction)float_getnewargs, METH_NOARGS},
+ {"__getformat__", (PyCFunction)float_getformat,
+ METH_O|METH_CLASS, float_getformat_doc},
+ {"__setformat__", (PyCFunction)float_setformat,
+ METH_VARARGS|METH_CLASS, float_setformat_doc},
+ {"__format__", (PyCFunction)float__format__,
+ METH_VARARGS, float__format__doc},
+ {NULL, NULL} /* sentinel */
};
static PyGetSetDef float_getset[] = {
- {"real",
+ {"real",
(getter)float_float, (setter)NULL,
"the real part of a complex number",
NULL},
- {"imag",
+ {"imag",
(getter)float_getzero, (setter)NULL,
"the imaginary part of a complex number",
NULL},
@@ -1760,229 +1760,229 @@ Convert a string or number to a floating point number, if possible.");
static PyNumberMethods float_as_number = {
- float_add, /*nb_add*/
- float_sub, /*nb_subtract*/
- float_mul, /*nb_multiply*/
- float_rem, /*nb_remainder*/
- float_divmod, /*nb_divmod*/
- float_pow, /*nb_power*/
- (unaryfunc)float_neg, /*nb_negative*/
- (unaryfunc)float_float, /*nb_positive*/
- (unaryfunc)float_abs, /*nb_absolute*/
- (inquiry)float_bool, /*nb_bool*/
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- 0, /*nb_and*/
- 0, /*nb_xor*/
- 0, /*nb_or*/
- float_trunc, /*nb_int*/
- 0, /*nb_reserved*/
- float_float, /*nb_float*/
- 0, /* nb_inplace_add */
- 0, /* nb_inplace_subtract */
- 0, /* nb_inplace_multiply */
- 0, /* nb_inplace_remainder */
- 0, /* nb_inplace_power */
- 0, /* nb_inplace_lshift */
- 0, /* nb_inplace_rshift */
- 0, /* nb_inplace_and */
- 0, /* nb_inplace_xor */
- 0, /* nb_inplace_or */
- float_floor_div, /* nb_floor_divide */
- float_div, /* nb_true_divide */
- 0, /* nb_inplace_floor_divide */
- 0, /* nb_inplace_true_divide */
+ float_add, /*nb_add*/
+ float_sub, /*nb_subtract*/
+ float_mul, /*nb_multiply*/
+ float_rem, /*nb_remainder*/
+ float_divmod, /*nb_divmod*/
+ float_pow, /*nb_power*/
+ (unaryfunc)float_neg, /*nb_negative*/
+ (unaryfunc)float_float, /*nb_positive*/
+ (unaryfunc)float_abs, /*nb_absolute*/
+ (inquiry)float_bool, /*nb_bool*/
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ 0, /*nb_and*/
+ 0, /*nb_xor*/
+ 0, /*nb_or*/
+ float_trunc, /*nb_int*/
+ 0, /*nb_reserved*/
+ float_float, /*nb_float*/
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply */
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ float_floor_div, /* nb_floor_divide */
+ float_div, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
};
PyTypeObject PyFloat_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "float",
- sizeof(PyFloatObject),
- 0,
- (destructor)float_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)float_repr, /* tp_repr */
- &float_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)float_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc)float_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- float_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- float_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- float_methods, /* tp_methods */
- 0, /* tp_members */
- float_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- float_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "float",
+ sizeof(PyFloatObject),
+ 0,
+ (destructor)float_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)float_repr, /* tp_repr */
+ &float_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)float_hash, /* tp_hash */
+ 0, /* tp_call */
+ (reprfunc)float_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ float_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ float_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ float_methods, /* tp_methods */
+ 0, /* tp_members */
+ float_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ float_new, /* tp_new */
};
void
_PyFloat_Init(void)
{
- /* We attempt to determine if this machine is using IEEE
- floating point formats by peering at the bits of some
- carefully chosen values. If it looks like we are on an
- IEEE platform, the float packing/unpacking routines can
- just copy bits, if not they resort to arithmetic & shifts
- and masks. The shifts & masks approach works on all finite
- values, but what happens to infinities, NaNs and signed
- zeroes on packing is an accident, and attempting to unpack
- a NaN or an infinity will raise an exception.
-
- Note that if we're on some whacked-out platform which uses
- IEEE formats but isn't strictly little-endian or big-
- endian, we will fall back to the portable shifts & masks
- method. */
+ /* We attempt to determine if this machine is using IEEE
+ floating point formats by peering at the bits of some
+ carefully chosen values. If it looks like we are on an
+ IEEE platform, the float packing/unpacking routines can
+ just copy bits, if not they resort to arithmetic & shifts
+ and masks. The shifts & masks approach works on all finite
+ values, but what happens to infinities, NaNs and signed
+ zeroes on packing is an accident, and attempting to unpack
+ a NaN or an infinity will raise an exception.
+
+ Note that if we're on some whacked-out platform which uses
+ IEEE formats but isn't strictly little-endian or big-
+ endian, we will fall back to the portable shifts & masks
+ method. */
#if SIZEOF_DOUBLE == 8
- {
- double x = 9006104071832581.0;
- if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
- detected_double_format = ieee_big_endian_format;
- else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
- detected_double_format = ieee_little_endian_format;
- else
- detected_double_format = unknown_format;
- }
+ {
+ double x = 9006104071832581.0;
+ if (memcmp(&x, "\x43\x3f\xff\x01\x02\x03\x04\x05", 8) == 0)
+ detected_double_format = ieee_big_endian_format;
+ else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
+ detected_double_format = ieee_little_endian_format;
+ else
+ detected_double_format = unknown_format;
+ }
#else
- detected_double_format = unknown_format;
+ detected_double_format = unknown_format;
#endif
#if SIZEOF_FLOAT == 4
- {
- float y = 16711938.0;
- if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
- detected_float_format = ieee_big_endian_format;
- else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
- detected_float_format = ieee_little_endian_format;
- else
- detected_float_format = unknown_format;
- }
+ {
+ float y = 16711938.0;
+ if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
+ detected_float_format = ieee_big_endian_format;
+ else if (memcmp(&y, "\x02\x01\x7f\x4b", 4) == 0)
+ detected_float_format = ieee_little_endian_format;
+ else
+ detected_float_format = unknown_format;
+ }
#else
- detected_float_format = unknown_format;
+ detected_float_format = unknown_format;
#endif
- double_format = detected_double_format;
- float_format = detected_float_format;
+ double_format = detected_double_format;
+ float_format = detected_float_format;
- /* Init float info */
- if (FloatInfoType.tp_name == 0)
- PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
+ /* Init float info */
+ if (FloatInfoType.tp_name == 0)
+ PyStructSequence_InitType(&FloatInfoType, &floatinfo_desc);
}
int
PyFloat_ClearFreeList(void)
{
- PyFloatObject *p;
- PyFloatBlock *list, *next;
- int i;
- int u; /* remaining unfreed floats per block */
- int freelist_size = 0;
-
- list = block_list;
- block_list = NULL;
- free_list = NULL;
- while (list != NULL) {
- u = 0;
- for (i = 0, p = &list->objects[0];
- i < N_FLOATOBJECTS;
- i++, p++) {
- if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0)
- u++;
- }
- next = list->next;
- if (u) {
- list->next = block_list;
- block_list = list;
- for (i = 0, p = &list->objects[0];
- i < N_FLOATOBJECTS;
- i++, p++) {
- if (!PyFloat_CheckExact(p) ||
- Py_REFCNT(p) == 0) {
- Py_TYPE(p) = (struct _typeobject *)
- free_list;
- free_list = p;
- }
- }
- }
- else {
- PyMem_FREE(list);
- }
- freelist_size += u;
- list = next;
- }
- return freelist_size;
+ PyFloatObject *p;
+ PyFloatBlock *list, *next;
+ int i;
+ int u; /* remaining unfreed floats per block */
+ int freelist_size = 0;
+
+ list = block_list;
+ block_list = NULL;
+ free_list = NULL;
+ while (list != NULL) {
+ u = 0;
+ for (i = 0, p = &list->objects[0];
+ i < N_FLOATOBJECTS;
+ i++, p++) {
+ if (PyFloat_CheckExact(p) && Py_REFCNT(p) != 0)
+ u++;
+ }
+ next = list->next;
+ if (u) {
+ list->next = block_list;
+ block_list = list;
+ for (i = 0, p = &list->objects[0];
+ i < N_FLOATOBJECTS;
+ i++, p++) {
+ if (!PyFloat_CheckExact(p) ||
+ Py_REFCNT(p) == 0) {
+ Py_TYPE(p) = (struct _typeobject *)
+ free_list;
+ free_list = p;
+ }
+ }
+ }
+ else {
+ PyMem_FREE(list);
+ }
+ freelist_size += u;
+ list = next;
+ }
+ return freelist_size;
}
void
PyFloat_Fini(void)
{
- PyFloatObject *p;
- PyFloatBlock *list;
- int i;
- int u; /* total unfreed floats per block */
-
- u = PyFloat_ClearFreeList();
-
- if (!Py_VerboseFlag)
- return;
- fprintf(stderr, "# cleanup floats");
- if (!u) {
- fprintf(stderr, "\n");
- }
- else {
- fprintf(stderr,
- ": %d unfreed float%s\n",
- u, u == 1 ? "" : "s");
- }
- if (Py_VerboseFlag > 1) {
- list = block_list;
- while (list != NULL) {
- for (i = 0, p = &list->objects[0];
- i < N_FLOATOBJECTS;
- i++, p++) {
- if (PyFloat_CheckExact(p) &&
- Py_REFCNT(p) != 0) {
- char *buf = PyOS_double_to_string(
- PyFloat_AS_DOUBLE(p), 'r',
- 0, 0, NULL);
- if (buf) {
- /* XXX(twouters) cast
- refcount to long
- until %zd is
- universally
- available
- */
- fprintf(stderr,
- "# <float at %p, refcnt=%ld, val=%s>\n",
- p, (long)Py_REFCNT(p), buf);
- PyMem_Free(buf);
- }
- }
- }
- list = list->next;
- }
- }
+ PyFloatObject *p;
+ PyFloatBlock *list;
+ int i;
+ int u; /* total unfreed floats per block */
+
+ u = PyFloat_ClearFreeList();
+
+ if (!Py_VerboseFlag)
+ return;
+ fprintf(stderr, "# cleanup floats");
+ if (!u) {
+ fprintf(stderr, "\n");
+ }
+ else {
+ fprintf(stderr,
+ ": %d unfreed float%s\n",
+ u, u == 1 ? "" : "s");
+ }
+ if (Py_VerboseFlag > 1) {
+ list = block_list;
+ while (list != NULL) {
+ for (i = 0, p = &list->objects[0];
+ i < N_FLOATOBJECTS;
+ i++, p++) {
+ if (PyFloat_CheckExact(p) &&
+ Py_REFCNT(p) != 0) {
+ char *buf = PyOS_double_to_string(
+ PyFloat_AS_DOUBLE(p), 'r',
+ 0, 0, NULL);
+ if (buf) {
+ /* XXX(twouters) cast
+ refcount to long
+ until %zd is
+ universally
+ available
+ */
+ fprintf(stderr,
+ "# <float at %p, refcnt=%ld, val=%s>\n",
+ p, (long)Py_REFCNT(p), buf);
+ PyMem_Free(buf);
+ }
+ }
+ }
+ list = list->next;
+ }
+ }
}
/*----------------------------------------------------------------------------
@@ -1991,406 +1991,406 @@ PyFloat_Fini(void)
int
_PyFloat_Pack4(double x, unsigned char *p, int le)
{
- if (float_format == unknown_format) {
- unsigned char sign;
- int e;
- double f;
- unsigned int fbits;
- int incr = 1;
-
- if (le) {
- p += 3;
- incr = -1;
- }
-
- if (x < 0) {
- sign = 1;
- x = -x;
- }
- else
- sign = 0;
-
- f = frexp(x, &e);
-
- /* Normalize f to be in the range [1.0, 2.0) */
- if (0.5 <= f && f < 1.0) {
- f *= 2.0;
- e--;
- }
- else if (f == 0.0)
- e = 0;
- else {
- PyErr_SetString(PyExc_SystemError,
- "frexp() result out of range");
- return -1;
- }
-
- if (e >= 128)
- goto Overflow;
- else if (e < -126) {
- /* Gradual underflow */
- f = ldexp(f, 126 + e);
- e = 0;
- }
- else if (!(e == 0 && f == 0.0)) {
- e += 127;
- f -= 1.0; /* Get rid of leading 1 */
- }
-
- f *= 8388608.0; /* 2**23 */
- fbits = (unsigned int)(f + 0.5); /* Round */
- assert(fbits <= 8388608);
- if (fbits >> 23) {
- /* The carry propagated out of a string of 23 1 bits. */
- fbits = 0;
- ++e;
- if (e >= 255)
- goto Overflow;
- }
-
- /* First byte */
- *p = (sign << 7) | (e >> 1);
- p += incr;
-
- /* Second byte */
- *p = (char) (((e & 1) << 7) | (fbits >> 16));
- p += incr;
-
- /* Third byte */
- *p = (fbits >> 8) & 0xFF;
- p += incr;
-
- /* Fourth byte */
- *p = fbits & 0xFF;
-
- /* Done */
- return 0;
-
- }
- else {
- float y = (float)x;
- const char *s = (char*)&y;
- int i, incr = 1;
-
- if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
- goto Overflow;
-
- if ((float_format == ieee_little_endian_format && !le)
- || (float_format == ieee_big_endian_format && le)) {
- p += 3;
- incr = -1;
- }
-
- for (i = 0; i < 4; i++) {
- *p = *s++;
- p += incr;
- }
- return 0;
- }
+ if (float_format == unknown_format) {
+ unsigned char sign;
+ int e;
+ double f;
+ unsigned int fbits;
+ int incr = 1;
+
+ if (le) {
+ p += 3;
+ incr = -1;
+ }
+
+ if (x < 0) {
+ sign = 1;
+ x = -x;
+ }
+ else
+ sign = 0;
+
+ f = frexp(x, &e);
+
+ /* Normalize f to be in the range [1.0, 2.0) */
+ if (0.5 <= f && f < 1.0) {
+ f *= 2.0;
+ e--;
+ }
+ else if (f == 0.0)
+ e = 0;
+ else {
+ PyErr_SetString(PyExc_SystemError,
+ "frexp() result out of range");
+ return -1;
+ }
+
+ if (e >= 128)
+ goto Overflow;
+ else if (e < -126) {
+ /* Gradual underflow */
+ f = ldexp(f, 126 + e);
+ e = 0;
+ }
+ else if (!(e == 0 && f == 0.0)) {
+ e += 127;
+ f -= 1.0; /* Get rid of leading 1 */
+ }
+
+ f *= 8388608.0; /* 2**23 */
+ fbits = (unsigned int)(f + 0.5); /* Round */
+ assert(fbits <= 8388608);
+ if (fbits >> 23) {
+ /* The carry propagated out of a string of 23 1 bits. */
+ fbits = 0;
+ ++e;
+ if (e >= 255)
+ goto Overflow;
+ }
+
+ /* First byte */
+ *p = (sign << 7) | (e >> 1);
+ p += incr;
+
+ /* Second byte */
+ *p = (char) (((e & 1) << 7) | (fbits >> 16));
+ p += incr;
+
+ /* Third byte */
+ *p = (fbits >> 8) & 0xFF;
+ p += incr;
+
+ /* Fourth byte */
+ *p = fbits & 0xFF;
+
+ /* Done */
+ return 0;
+
+ }
+ else {
+ float y = (float)x;
+ const char *s = (char*)&y;
+ int i, incr = 1;
+
+ if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
+ goto Overflow;
+
+ if ((float_format == ieee_little_endian_format && !le)
+ || (float_format == ieee_big_endian_format && le)) {
+ p += 3;
+ incr = -1;
+ }
+
+ for (i = 0; i < 4; i++) {
+ *p = *s++;
+ p += incr;
+ }
+ return 0;
+ }
Overflow:
- PyErr_SetString(PyExc_OverflowError,
- "float too large to pack with f format");
- return -1;
+ PyErr_SetString(PyExc_OverflowError,
+ "float too large to pack with f format");
+ return -1;
}
int
_PyFloat_Pack8(double x, unsigned char *p, int le)
{
- if (double_format == unknown_format) {
- unsigned char sign;
- int e;
- double f;
- unsigned int fhi, flo;
- int incr = 1;
-
- if (le) {
- p += 7;
- incr = -1;
- }
-
- if (x < 0) {
- sign = 1;
- x = -x;
- }
- else
- sign = 0;
-
- f = frexp(x, &e);
-
- /* Normalize f to be in the range [1.0, 2.0) */
- if (0.5 <= f && f < 1.0) {
- f *= 2.0;
- e--;
- }
- else if (f == 0.0)
- e = 0;
- else {
- PyErr_SetString(PyExc_SystemError,
- "frexp() result out of range");
- return -1;
- }
-
- if (e >= 1024)
- goto Overflow;
- else if (e < -1022) {
- /* Gradual underflow */
- f = ldexp(f, 1022 + e);
- e = 0;
- }
- else if (!(e == 0 && f == 0.0)) {
- e += 1023;
- f -= 1.0; /* Get rid of leading 1 */
- }
-
- /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
- f *= 268435456.0; /* 2**28 */
- fhi = (unsigned int)f; /* Truncate */
- assert(fhi < 268435456);
-
- f -= (double)fhi;
- f *= 16777216.0; /* 2**24 */
- flo = (unsigned int)(f + 0.5); /* Round */
- assert(flo <= 16777216);
- if (flo >> 24) {
- /* The carry propagated out of a string of 24 1 bits. */
- flo = 0;
- ++fhi;
- if (fhi >> 28) {
- /* And it also progagated out of the next 28 bits. */
- fhi = 0;
- ++e;
- if (e >= 2047)
- goto Overflow;
- }
- }
-
- /* First byte */
- *p = (sign << 7) | (e >> 4);
- p += incr;
-
- /* Second byte */
- *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
- p += incr;
-
- /* Third byte */
- *p = (fhi >> 16) & 0xFF;
- p += incr;
-
- /* Fourth byte */
- *p = (fhi >> 8) & 0xFF;
- p += incr;
-
- /* Fifth byte */
- *p = fhi & 0xFF;
- p += incr;
-
- /* Sixth byte */
- *p = (flo >> 16) & 0xFF;
- p += incr;
-
- /* Seventh byte */
- *p = (flo >> 8) & 0xFF;
- p += incr;
-
- /* Eighth byte */
- *p = flo & 0xFF;
- p += incr;
-
- /* Done */
- return 0;
-
- Overflow:
- PyErr_SetString(PyExc_OverflowError,
- "float too large to pack with d format");
- return -1;
- }
- else {
- const char *s = (char*)&x;
- int i, incr = 1;
-
- if ((double_format == ieee_little_endian_format && !le)
- || (double_format == ieee_big_endian_format && le)) {
- p += 7;
- incr = -1;
- }
-
- for (i = 0; i < 8; i++) {
- *p = *s++;
- p += incr;
- }
- return 0;
- }
+ if (double_format == unknown_format) {
+ unsigned char sign;
+ int e;
+ double f;
+ unsigned int fhi, flo;
+ int incr = 1;
+
+ if (le) {
+ p += 7;
+ incr = -1;
+ }
+
+ if (x < 0) {
+ sign = 1;
+ x = -x;
+ }
+ else
+ sign = 0;
+
+ f = frexp(x, &e);
+
+ /* Normalize f to be in the range [1.0, 2.0) */
+ if (0.5 <= f && f < 1.0) {
+ f *= 2.0;
+ e--;
+ }
+ else if (f == 0.0)
+ e = 0;
+ else {
+ PyErr_SetString(PyExc_SystemError,
+ "frexp() result out of range");
+ return -1;
+ }
+
+ if (e >= 1024)
+ goto Overflow;
+ else if (e < -1022) {
+ /* Gradual underflow */
+ f = ldexp(f, 1022 + e);
+ e = 0;
+ }
+ else if (!(e == 0 && f == 0.0)) {
+ e += 1023;
+ f -= 1.0; /* Get rid of leading 1 */
+ }
+
+ /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
+ f *= 268435456.0; /* 2**28 */
+ fhi = (unsigned int)f; /* Truncate */
+ assert(fhi < 268435456);
+
+ f -= (double)fhi;
+ f *= 16777216.0; /* 2**24 */
+ flo = (unsigned int)(f + 0.5); /* Round */
+ assert(flo <= 16777216);
+ if (flo >> 24) {
+ /* The carry propagated out of a string of 24 1 bits. */
+ flo = 0;
+ ++fhi;
+ if (fhi >> 28) {
+ /* And it also progagated out of the next 28 bits. */
+ fhi = 0;
+ ++e;
+ if (e >= 2047)
+ goto Overflow;
+ }
+ }
+
+ /* First byte */
+ *p = (sign << 7) | (e >> 4);
+ p += incr;
+
+ /* Second byte */
+ *p = (unsigned char) (((e & 0xF) << 4) | (fhi >> 24));
+ p += incr;
+
+ /* Third byte */
+ *p = (fhi >> 16) & 0xFF;
+ p += incr;
+
+ /* Fourth byte */
+ *p = (fhi >> 8) & 0xFF;
+ p += incr;
+
+ /* Fifth byte */
+ *p = fhi & 0xFF;
+ p += incr;
+
+ /* Sixth byte */
+ *p = (flo >> 16) & 0xFF;
+ p += incr;
+
+ /* Seventh byte */
+ *p = (flo >> 8) & 0xFF;
+ p += incr;
+
+ /* Eighth byte */
+ *p = flo & 0xFF;
+ p += incr;
+
+ /* Done */
+ return 0;
+
+ Overflow:
+ PyErr_SetString(PyExc_OverflowError,
+ "float too large to pack with d format");
+ return -1;
+ }
+ else {
+ const char *s = (char*)&x;
+ int i, incr = 1;
+
+ if ((double_format == ieee_little_endian_format && !le)
+ || (double_format == ieee_big_endian_format && le)) {
+ p += 7;
+ incr = -1;
+ }
+
+ for (i = 0; i < 8; i++) {
+ *p = *s++;
+ p += incr;
+ }
+ return 0;
+ }
}
double
_PyFloat_Unpack4(const unsigned char *p, int le)
{
- if (float_format == unknown_format) {
- unsigned char sign;
- int e;
- unsigned int f;
- double x;
- int incr = 1;
-
- if (le) {
- p += 3;
- incr = -1;
- }
-
- /* First byte */
- sign = (*p >> 7) & 1;
- e = (*p & 0x7F) << 1;
- p += incr;
-
- /* Second byte */
- e |= (*p >> 7) & 1;
- f = (*p & 0x7F) << 16;
- p += incr;
-
- if (e == 255) {
- PyErr_SetString(
- PyExc_ValueError,
- "can't unpack IEEE 754 special value "
- "on non-IEEE platform");
- return -1;
- }
-
- /* Third byte */
- f |= *p << 8;
- p += incr;
-
- /* Fourth byte */
- f |= *p;
-
- x = (double)f / 8388608.0;
-
- /* XXX This sadly ignores Inf/NaN issues */
- if (e == 0)
- e = -126;
- else {
- x += 1.0;
- e -= 127;
- }
- x = ldexp(x, e);
-
- if (sign)
- x = -x;
-
- return x;
- }
- else {
- float x;
-
- if ((float_format == ieee_little_endian_format && !le)
- || (float_format == ieee_big_endian_format && le)) {
- char buf[4];
- char *d = &buf[3];
- int i;
-
- for (i = 0; i < 4; i++) {
- *d-- = *p++;
- }
- memcpy(&x, buf, 4);
- }
- else {
- memcpy(&x, p, 4);
- }
-
- return x;
- }
+ if (float_format == unknown_format) {
+ unsigned char sign;
+ int e;
+ unsigned int f;
+ double x;
+ int incr = 1;
+
+ if (le) {
+ p += 3;
+ incr = -1;
+ }
+
+ /* First byte */
+ sign = (*p >> 7) & 1;
+ e = (*p & 0x7F) << 1;
+ p += incr;
+
+ /* Second byte */
+ e |= (*p >> 7) & 1;
+ f = (*p & 0x7F) << 16;
+ p += incr;
+
+ if (e == 255) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "can't unpack IEEE 754 special value "
+ "on non-IEEE platform");
+ return -1;
+ }
+
+ /* Third byte */
+ f |= *p << 8;
+ p += incr;
+
+ /* Fourth byte */
+ f |= *p;
+
+ x = (double)f / 8388608.0;
+
+ /* XXX This sadly ignores Inf/NaN issues */
+ if (e == 0)
+ e = -126;
+ else {
+ x += 1.0;
+ e -= 127;
+ }
+ x = ldexp(x, e);
+
+ if (sign)
+ x = -x;
+
+ return x;
+ }
+ else {
+ float x;
+
+ if ((float_format == ieee_little_endian_format && !le)
+ || (float_format == ieee_big_endian_format && le)) {
+ char buf[4];
+ char *d = &buf[3];
+ int i;
+
+ for (i = 0; i < 4; i++) {
+ *d-- = *p++;
+ }
+ memcpy(&x, buf, 4);
+ }
+ else {
+ memcpy(&x, p, 4);
+ }
+
+ return x;
+ }
}
double
_PyFloat_Unpack8(const unsigned char *p, int le)
{
- if (double_format == unknown_format) {
- unsigned char sign;
- int e;
- unsigned int fhi, flo;
- double x;
- int incr = 1;
-
- if (le) {
- p += 7;
- incr = -1;
- }
-
- /* First byte */
- sign = (*p >> 7) & 1;
- e = (*p & 0x7F) << 4;
-
- p += incr;
-
- /* Second byte */
- e |= (*p >> 4) & 0xF;
- fhi = (*p & 0xF) << 24;
- p += incr;
-
- if (e == 2047) {
- PyErr_SetString(
- PyExc_ValueError,
- "can't unpack IEEE 754 special value "
- "on non-IEEE platform");
- return -1.0;
- }
-
- /* Third byte */
- fhi |= *p << 16;
- p += incr;
-
- /* Fourth byte */
- fhi |= *p << 8;
- p += incr;
-
- /* Fifth byte */
- fhi |= *p;
- p += incr;
-
- /* Sixth byte */
- flo = *p << 16;
- p += incr;
-
- /* Seventh byte */
- flo |= *p << 8;
- p += incr;
-
- /* Eighth byte */
- flo |= *p;
-
- x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
- x /= 268435456.0; /* 2**28 */
-
- if (e == 0)
- e = -1022;
- else {
- x += 1.0;
- e -= 1023;
- }
- x = ldexp(x, e);
-
- if (sign)
- x = -x;
-
- return x;
- }
- else {
- double x;
-
- if ((double_format == ieee_little_endian_format && !le)
- || (double_format == ieee_big_endian_format && le)) {
- char buf[8];
- char *d = &buf[7];
- int i;
-
- for (i = 0; i < 8; i++) {
- *d-- = *p++;
- }
- memcpy(&x, buf, 8);
- }
- else {
- memcpy(&x, p, 8);
- }
-
- return x;
- }
+ if (double_format == unknown_format) {
+ unsigned char sign;
+ int e;
+ unsigned int fhi, flo;
+ double x;
+ int incr = 1;
+
+ if (le) {
+ p += 7;
+ incr = -1;
+ }
+
+ /* First byte */
+ sign = (*p >> 7) & 1;
+ e = (*p & 0x7F) << 4;
+
+ p += incr;
+
+ /* Second byte */
+ e |= (*p >> 4) & 0xF;
+ fhi = (*p & 0xF) << 24;
+ p += incr;
+
+ if (e == 2047) {
+ PyErr_SetString(
+ PyExc_ValueError,
+ "can't unpack IEEE 754 special value "
+ "on non-IEEE platform");
+ return -1.0;
+ }
+
+ /* Third byte */
+ fhi |= *p << 16;
+ p += incr;
+
+ /* Fourth byte */
+ fhi |= *p << 8;
+ p += incr;
+
+ /* Fifth byte */
+ fhi |= *p;
+ p += incr;
+
+ /* Sixth byte */
+ flo = *p << 16;
+ p += incr;
+
+ /* Seventh byte */
+ flo |= *p << 8;
+ p += incr;
+
+ /* Eighth byte */
+ flo |= *p;
+
+ x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
+ x /= 268435456.0; /* 2**28 */
+
+ if (e == 0)
+ e = -1022;
+ else {
+ x += 1.0;
+ e -= 1023;
+ }
+ x = ldexp(x, e);
+
+ if (sign)
+ x = -x;
+
+ return x;
+ }
+ else {
+ double x;
+
+ if ((double_format == ieee_little_endian_format && !le)
+ || (double_format == ieee_big_endian_format && le)) {
+ char buf[8];
+ char *d = &buf[7];
+ int i;
+
+ for (i = 0; i < 8; i++) {
+ *d-- = *p++;
+ }
+ memcpy(&x, buf, 8);
+ }
+ else {
+ memcpy(&x, p, 8);
+ }
+
+ return x;
+ }
}
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 664dc2b..2cf2cdf 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -15,37 +15,37 @@
#define OFF(x) offsetof(PyFrameObject, x)
static PyMemberDef frame_memberlist[] = {
- {"f_back", T_OBJECT, OFF(f_back), READONLY},
- {"f_code", T_OBJECT, OFF(f_code), READONLY},
- {"f_builtins", T_OBJECT, OFF(f_builtins),READONLY},
- {"f_globals", T_OBJECT, OFF(f_globals), READONLY},
- {"f_lasti", T_INT, OFF(f_lasti), READONLY},
- {NULL} /* Sentinel */
+ {"f_back", T_OBJECT, OFF(f_back), READONLY},
+ {"f_code", T_OBJECT, OFF(f_code), READONLY},
+ {"f_builtins", T_OBJECT, OFF(f_builtins),READONLY},
+ {"f_globals", T_OBJECT, OFF(f_globals), READONLY},
+ {"f_lasti", T_INT, OFF(f_lasti), READONLY},
+ {NULL} /* Sentinel */
};
static PyObject *
frame_getlocals(PyFrameObject *f, void *closure)
{
- PyFrame_FastToLocals(f);
- Py_INCREF(f->f_locals);
- return f->f_locals;
+ PyFrame_FastToLocals(f);
+ Py_INCREF(f->f_locals);
+ return f->f_locals;
}
static PyObject *
frame_getlineno(PyFrameObject *f, void *closure)
{
- int lineno;
+ int lineno;
- if (f->f_trace)
- lineno = f->f_lineno;
- else
- lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+ if (f->f_trace)
+ lineno = f->f_lineno;
+ else
+ lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
- return PyLong_FromLong(lineno);
+ return PyLong_FromLong(lineno);
}
/* Setter for f_lineno - you can set f_lineno from within a trace function in
- * order to jump to a given line of code, subject to some restrictions. Most
+ * order to jump to a given line of code, subject to some restrictions. Most
* lines are OK to jump to because they don't make any assumptions about the
* state of the stack (obvious because you could remove the line and the code
* would still work without any stack errors), but there are some constructs
@@ -62,311 +62,311 @@ frame_getlineno(PyFrameObject *f, void *closure)
static int
frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
{
- int new_lineno = 0; /* The new value of f_lineno */
- long l_new_lineno;
- int overflow;
- int new_lasti = 0; /* The new value of f_lasti */
- int new_iblock = 0; /* The new value of f_iblock */
- unsigned char *code = NULL; /* The bytecode for the frame... */
- Py_ssize_t code_len = 0; /* ...and its length */
- unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
- Py_ssize_t lnotab_len = 0; /* (ditto) */
- int offset = 0; /* (ditto) */
- int line = 0; /* (ditto) */
- int addr = 0; /* (ditto) */
- int min_addr = 0; /* Scanning the SETUPs and POPs */
- int max_addr = 0; /* (ditto) */
- int delta_iblock = 0; /* (ditto) */
- int min_delta_iblock = 0; /* (ditto) */
- int min_iblock = 0; /* (ditto) */
- int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
- int new_lasti_setup_addr = 0; /* (ditto) */
- int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
- int in_finally[CO_MAXBLOCKS]; /* (ditto) */
- int blockstack_top = 0; /* (ditto) */
- unsigned char setup_op = 0; /* (ditto) */
-
- /* f_lineno must be an integer. */
- if (!PyLong_CheckExact(p_new_lineno)) {
- PyErr_SetString(PyExc_ValueError,
- "lineno must be an integer");
- return -1;
- }
-
- /* You can only do this from within a trace function, not via
- * _getframe or similar hackery. */
- if (!f->f_trace)
- {
- PyErr_Format(PyExc_ValueError,
- "f_lineno can only be set by a"
- " line trace function");
- return -1;
- }
-
- /* Fail if the line comes before the start of the code block. */
- l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
- if (overflow
+ int new_lineno = 0; /* The new value of f_lineno */
+ long l_new_lineno;
+ int overflow;
+ int new_lasti = 0; /* The new value of f_lasti */
+ int new_iblock = 0; /* The new value of f_iblock */
+ unsigned char *code = NULL; /* The bytecode for the frame... */
+ Py_ssize_t code_len = 0; /* ...and its length */
+ unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
+ Py_ssize_t lnotab_len = 0; /* (ditto) */
+ int offset = 0; /* (ditto) */
+ int line = 0; /* (ditto) */
+ int addr = 0; /* (ditto) */
+ int min_addr = 0; /* Scanning the SETUPs and POPs */
+ int max_addr = 0; /* (ditto) */
+ int delta_iblock = 0; /* (ditto) */
+ int min_delta_iblock = 0; /* (ditto) */
+ int min_iblock = 0; /* (ditto) */
+ int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
+ int new_lasti_setup_addr = 0; /* (ditto) */
+ int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
+ int in_finally[CO_MAXBLOCKS]; /* (ditto) */
+ int blockstack_top = 0; /* (ditto) */
+ unsigned char setup_op = 0; /* (ditto) */
+
+ /* f_lineno must be an integer. */
+ if (!PyLong_CheckExact(p_new_lineno)) {
+ PyErr_SetString(PyExc_ValueError,
+ "lineno must be an integer");
+ return -1;
+ }
+
+ /* You can only do this from within a trace function, not via
+ * _getframe or similar hackery. */
+ if (!f->f_trace)
+ {
+ PyErr_Format(PyExc_ValueError,
+ "f_lineno can only be set by a"
+ " line trace function");
+ return -1;
+ }
+
+ /* Fail if the line comes before the start of the code block. */
+ l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
+ if (overflow
#if SIZEOF_LONG > SIZEOF_INT
- || l_new_lineno > INT_MAX
- || l_new_lineno < INT_MIN
+ || l_new_lineno > INT_MAX
+ || l_new_lineno < INT_MIN
#endif
- ) {
- PyErr_SetString(PyExc_ValueError,
- "lineno out of range");
- return -1;
- }
- new_lineno = (int)l_new_lineno;
-
- if (new_lineno < f->f_code->co_firstlineno) {
- PyErr_Format(PyExc_ValueError,
- "line %d comes before the current code block",
- new_lineno);
- return -1;
- }
- else if (new_lineno == f->f_code->co_firstlineno) {
- new_lasti = 0;
- new_lineno = f->f_code->co_firstlineno;
- }
- else {
- /* Find the bytecode offset for the start of the given
- * line, or the first code-owning line after it. */
- char *tmp;
- PyBytes_AsStringAndSize(f->f_code->co_lnotab,
- &tmp, &lnotab_len);
- lnotab = (unsigned char *) tmp;
- addr = 0;
- line = f->f_code->co_firstlineno;
- new_lasti = -1;
- for (offset = 0; offset < lnotab_len; offset += 2) {
- addr += lnotab[offset];
- line += lnotab[offset+1];
- if (line >= new_lineno) {
- new_lasti = addr;
- new_lineno = line;
- break;
- }
- }
- }
-
- /* If we didn't reach the requested line, return an error. */
- if (new_lasti == -1) {
- PyErr_Format(PyExc_ValueError,
- "line %d comes after the current code block",
- new_lineno);
- return -1;
- }
-
- /* We're now ready to look at the bytecode. */
- PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
- min_addr = MIN(new_lasti, f->f_lasti);
- max_addr = MAX(new_lasti, f->f_lasti);
-
- /* You can't jump onto a line with an 'except' statement on it -
- * they expect to have an exception on the top of the stack, which
- * won't be true if you jump to them. They always start with code
- * that either pops the exception using POP_TOP (plain 'except:'
- * lines do this) or duplicates the exception on the stack using
- * DUP_TOP (if there's an exception type specified). See compile.c,
- * 'com_try_except' for the full details. There aren't any other
- * cases (AFAIK) where a line's code can start with DUP_TOP or
- * POP_TOP, but if any ever appear, they'll be subject to the same
- * restriction (but with a different error message). */
- if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
- PyErr_SetString(PyExc_ValueError,
- "can't jump to 'except' line as there's no exception");
- return -1;
- }
-
- /* You can't jump into or out of a 'finally' block because the 'try'
- * block leaves something on the stack for the END_FINALLY to clean
- * up. So we walk the bytecode, maintaining a simulated blockstack.
- * When we reach the old or new address and it's in a 'finally' block
- * we note the address of the corresponding SETUP_FINALLY. The jump
- * is only legal if neither address is in a 'finally' block or
- * they're both in the same one. 'blockstack' is a stack of the
- * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
- * whether we're in a 'finally' block at each blockstack level. */
- f_lasti_setup_addr = -1;
- new_lasti_setup_addr = -1;
- memset(blockstack, '\0', sizeof(blockstack));
- memset(in_finally, '\0', sizeof(in_finally));
- blockstack_top = 0;
- for (addr = 0; addr < code_len; addr++) {
- unsigned char op = code[addr];
- switch (op) {
- case SETUP_LOOP:
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- blockstack[blockstack_top++] = addr;
- in_finally[blockstack_top-1] = 0;
- break;
-
- case POP_BLOCK:
- assert(blockstack_top > 0);
- setup_op = code[blockstack[blockstack_top-1]];
- if (setup_op == SETUP_FINALLY) {
- in_finally[blockstack_top-1] = 1;
- }
- else {
- blockstack_top--;
- }
- break;
-
- case END_FINALLY:
- /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
- * in the bytecode but don't correspond to an actual
- * 'finally' block. (If blockstack_top is 0, we must
- * be seeing such an END_FINALLY.) */
- if (blockstack_top > 0) {
- setup_op = code[blockstack[blockstack_top-1]];
- if (setup_op == SETUP_FINALLY) {
- blockstack_top--;
- }
- }
- break;
- }
-
- /* For the addresses we're interested in, see whether they're
- * within a 'finally' block and if so, remember the address
- * of the SETUP_FINALLY. */
- if (addr == new_lasti || addr == f->f_lasti) {
- int i = 0;
- int setup_addr = -1;
- for (i = blockstack_top-1; i >= 0; i--) {
- if (in_finally[i]) {
- setup_addr = blockstack[i];
- break;
- }
- }
-
- if (setup_addr != -1) {
- if (addr == new_lasti) {
- new_lasti_setup_addr = setup_addr;
- }
-
- if (addr == f->f_lasti) {
- f_lasti_setup_addr = setup_addr;
- }
- }
- }
-
- if (op >= HAVE_ARGUMENT) {
- addr += 2;
- }
- }
-
- /* Verify that the blockstack tracking code didn't get lost. */
- assert(blockstack_top == 0);
-
- /* After all that, are we jumping into / out of a 'finally' block? */
- if (new_lasti_setup_addr != f_lasti_setup_addr) {
- PyErr_SetString(PyExc_ValueError,
- "can't jump into or out of a 'finally' block");
- return -1;
- }
-
-
- /* Police block-jumping (you can't jump into the middle of a block)
- * and ensure that the blockstack finishes up in a sensible state (by
- * popping any blocks we're jumping out of). We look at all the
- * blockstack operations between the current position and the new
- * one, and keep track of how many blocks we drop out of on the way.
- * By also keeping track of the lowest blockstack position we see, we
- * can tell whether the jump goes into any blocks without coming out
- * again - in that case we raise an exception below. */
- delta_iblock = 0;
- for (addr = min_addr; addr < max_addr; addr++) {
- unsigned char op = code[addr];
- switch (op) {
- case SETUP_LOOP:
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- delta_iblock++;
- break;
-
- case POP_BLOCK:
- delta_iblock--;
- break;
- }
-
- min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
-
- if (op >= HAVE_ARGUMENT) {
- addr += 2;
- }
- }
-
- /* Derive the absolute iblock values from the deltas. */
- min_iblock = f->f_iblock + min_delta_iblock;
- if (new_lasti > f->f_lasti) {
- /* Forwards jump. */
- new_iblock = f->f_iblock + delta_iblock;
- }
- else {
- /* Backwards jump. */
- new_iblock = f->f_iblock - delta_iblock;
- }
-
- /* Are we jumping into a block? */
- if (new_iblock > min_iblock) {
- PyErr_SetString(PyExc_ValueError,
- "can't jump into the middle of a block");
- return -1;
- }
-
- /* Pop any blocks that we're jumping out of. */
- while (f->f_iblock > new_iblock) {
- PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
- while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
- PyObject *v = (*--f->f_stacktop);
- Py_DECREF(v);
- }
- }
-
- /* Finally set the new f_lineno and f_lasti and return OK. */
- f->f_lineno = new_lineno;
- f->f_lasti = new_lasti;
- return 0;
+ ) {
+ PyErr_SetString(PyExc_ValueError,
+ "lineno out of range");
+ return -1;
+ }
+ new_lineno = (int)l_new_lineno;
+
+ if (new_lineno < f->f_code->co_firstlineno) {
+ PyErr_Format(PyExc_ValueError,
+ "line %d comes before the current code block",
+ new_lineno);
+ return -1;
+ }
+ else if (new_lineno == f->f_code->co_firstlineno) {
+ new_lasti = 0;
+ new_lineno = f->f_code->co_firstlineno;
+ }
+ else {
+ /* Find the bytecode offset for the start of the given
+ * line, or the first code-owning line after it. */
+ char *tmp;
+ PyBytes_AsStringAndSize(f->f_code->co_lnotab,
+ &tmp, &lnotab_len);
+ lnotab = (unsigned char *) tmp;
+ addr = 0;
+ line = f->f_code->co_firstlineno;
+ new_lasti = -1;
+ for (offset = 0; offset < lnotab_len; offset += 2) {
+ addr += lnotab[offset];
+ line += lnotab[offset+1];
+ if (line >= new_lineno) {
+ new_lasti = addr;
+ new_lineno = line;
+ break;
+ }
+ }
+ }
+
+ /* If we didn't reach the requested line, return an error. */
+ if (new_lasti == -1) {
+ PyErr_Format(PyExc_ValueError,
+ "line %d comes after the current code block",
+ new_lineno);
+ return -1;
+ }
+
+ /* We're now ready to look at the bytecode. */
+ PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
+ min_addr = MIN(new_lasti, f->f_lasti);
+ max_addr = MAX(new_lasti, f->f_lasti);
+
+ /* You can't jump onto a line with an 'except' statement on it -
+ * they expect to have an exception on the top of the stack, which
+ * won't be true if you jump to them. They always start with code
+ * that either pops the exception using POP_TOP (plain 'except:'
+ * lines do this) or duplicates the exception on the stack using
+ * DUP_TOP (if there's an exception type specified). See compile.c,
+ * 'com_try_except' for the full details. There aren't any other
+ * cases (AFAIK) where a line's code can start with DUP_TOP or
+ * POP_TOP, but if any ever appear, they'll be subject to the same
+ * restriction (but with a different error message). */
+ if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't jump to 'except' line as there's no exception");
+ return -1;
+ }
+
+ /* You can't jump into or out of a 'finally' block because the 'try'
+ * block leaves something on the stack for the END_FINALLY to clean
+ * up. So we walk the bytecode, maintaining a simulated blockstack.
+ * When we reach the old or new address and it's in a 'finally' block
+ * we note the address of the corresponding SETUP_FINALLY. The jump
+ * is only legal if neither address is in a 'finally' block or
+ * they're both in the same one. 'blockstack' is a stack of the
+ * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
+ * whether we're in a 'finally' block at each blockstack level. */
+ f_lasti_setup_addr = -1;
+ new_lasti_setup_addr = -1;
+ memset(blockstack, '\0', sizeof(blockstack));
+ memset(in_finally, '\0', sizeof(in_finally));
+ blockstack_top = 0;
+ for (addr = 0; addr < code_len; addr++) {
+ unsigned char op = code[addr];
+ switch (op) {
+ case SETUP_LOOP:
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ blockstack[blockstack_top++] = addr;
+ in_finally[blockstack_top-1] = 0;
+ break;
+
+ case POP_BLOCK:
+ assert(blockstack_top > 0);
+ setup_op = code[blockstack[blockstack_top-1]];
+ if (setup_op == SETUP_FINALLY) {
+ in_finally[blockstack_top-1] = 1;
+ }
+ else {
+ blockstack_top--;
+ }
+ break;
+
+ case END_FINALLY:
+ /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
+ * in the bytecode but don't correspond to an actual
+ * 'finally' block. (If blockstack_top is 0, we must
+ * be seeing such an END_FINALLY.) */
+ if (blockstack_top > 0) {
+ setup_op = code[blockstack[blockstack_top-1]];
+ if (setup_op == SETUP_FINALLY) {
+ blockstack_top--;
+ }
+ }
+ break;
+ }
+
+ /* For the addresses we're interested in, see whether they're
+ * within a 'finally' block and if so, remember the address
+ * of the SETUP_FINALLY. */
+ if (addr == new_lasti || addr == f->f_lasti) {
+ int i = 0;
+ int setup_addr = -1;
+ for (i = blockstack_top-1; i >= 0; i--) {
+ if (in_finally[i]) {
+ setup_addr = blockstack[i];
+ break;
+ }
+ }
+
+ if (setup_addr != -1) {
+ if (addr == new_lasti) {
+ new_lasti_setup_addr = setup_addr;
+ }
+
+ if (addr == f->f_lasti) {
+ f_lasti_setup_addr = setup_addr;
+ }
+ }
+ }
+
+ if (op >= HAVE_ARGUMENT) {
+ addr += 2;
+ }
+ }
+
+ /* Verify that the blockstack tracking code didn't get lost. */
+ assert(blockstack_top == 0);
+
+ /* After all that, are we jumping into / out of a 'finally' block? */
+ if (new_lasti_setup_addr != f_lasti_setup_addr) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't jump into or out of a 'finally' block");
+ return -1;
+ }
+
+
+ /* Police block-jumping (you can't jump into the middle of a block)
+ * and ensure that the blockstack finishes up in a sensible state (by
+ * popping any blocks we're jumping out of). We look at all the
+ * blockstack operations between the current position and the new
+ * one, and keep track of how many blocks we drop out of on the way.
+ * By also keeping track of the lowest blockstack position we see, we
+ * can tell whether the jump goes into any blocks without coming out
+ * again - in that case we raise an exception below. */
+ delta_iblock = 0;
+ for (addr = min_addr; addr < max_addr; addr++) {
+ unsigned char op = code[addr];
+ switch (op) {
+ case SETUP_LOOP:
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ delta_iblock++;
+ break;
+
+ case POP_BLOCK:
+ delta_iblock--;
+ break;
+ }
+
+ min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
+
+ if (op >= HAVE_ARGUMENT) {
+ addr += 2;
+ }
+ }
+
+ /* Derive the absolute iblock values from the deltas. */
+ min_iblock = f->f_iblock + min_delta_iblock;
+ if (new_lasti > f->f_lasti) {
+ /* Forwards jump. */
+ new_iblock = f->f_iblock + delta_iblock;
+ }
+ else {
+ /* Backwards jump. */
+ new_iblock = f->f_iblock - delta_iblock;
+ }
+
+ /* Are we jumping into a block? */
+ if (new_iblock > min_iblock) {
+ PyErr_SetString(PyExc_ValueError,
+ "can't jump into the middle of a block");
+ return -1;
+ }
+
+ /* Pop any blocks that we're jumping out of. */
+ while (f->f_iblock > new_iblock) {
+ PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
+ while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
+ PyObject *v = (*--f->f_stacktop);
+ Py_DECREF(v);
+ }
+ }
+
+ /* Finally set the new f_lineno and f_lasti and return OK. */
+ f->f_lineno = new_lineno;
+ f->f_lasti = new_lasti;
+ return 0;
}
static PyObject *
frame_gettrace(PyFrameObject *f, void *closure)
{
- PyObject* trace = f->f_trace;
+ PyObject* trace = f->f_trace;
- if (trace == NULL)
- trace = Py_None;
+ if (trace == NULL)
+ trace = Py_None;
- Py_INCREF(trace);
+ Py_INCREF(trace);
- return trace;
+ return trace;
}
static int
frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
{
- /* We rely on f_lineno being accurate when f_trace is set. */
+ /* We rely on f_lineno being accurate when f_trace is set. */
- PyObject* old_value = f->f_trace;
+ PyObject* old_value = f->f_trace;
- Py_XINCREF(v);
- f->f_trace = v;
+ Py_XINCREF(v);
+ f->f_trace = v;
- if (v != NULL)
- f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+ if (v != NULL)
+ f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
- Py_XDECREF(old_value);
+ Py_XDECREF(old_value);
- return 0;
+ return 0;
}
static PyGetSetDef frame_getsetlist[] = {
- {"f_locals", (getter)frame_getlocals, NULL, NULL},
- {"f_lineno", (getter)frame_getlineno,
- (setter)frame_setlineno, NULL},
- {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
- {0}
+ {"f_locals", (getter)frame_getlocals, NULL, NULL},
+ {"f_lineno", (getter)frame_getlineno,
+ (setter)frame_setlineno, NULL},
+ {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
+ {0}
};
/* Stack frames are allocated and deallocated at a considerable rate.
@@ -383,7 +383,7 @@ static PyGetSetDef frame_getsetlist[] = {
the following fields are still valid:
* ob_type, ob_size, f_code, f_valuestack;
-
+
* f_locals, f_trace,
f_exc_type, f_exc_value, f_exc_traceback are NULL;
@@ -394,10 +394,10 @@ static PyGetSetDef frame_getsetlist[] = {
integers are allocated in a special way -- see intobject.c). When
a stack frame is on the free list, only the following members have
a meaning:
- ob_type == &Frametype
- f_back next item on free list, or NULL
- f_stacksize size of value stack
- ob_size size of localsplus
+ ob_type == &Frametype
+ f_back next item on free list, or NULL
+ f_stacksize size of value stack
+ ob_size size of localsplus
Note that the value and block stacks are preserved -- this can save
another malloc() call or two (and two free() calls as well!).
Also note that, unlike for integers, each frame object is a
@@ -413,307 +413,307 @@ static PyGetSetDef frame_getsetlist[] = {
*/
static PyFrameObject *free_list = NULL;
-static int numfree = 0; /* number of frames currently in free_list */
+static int numfree = 0; /* number of frames currently in free_list */
/* max value for numfree */
-#define PyFrame_MAXFREELIST 200
+#define PyFrame_MAXFREELIST 200
static void
frame_dealloc(PyFrameObject *f)
{
- PyObject **p, **valuestack;
- PyCodeObject *co;
-
- PyObject_GC_UnTrack(f);
- Py_TRASHCAN_SAFE_BEGIN(f)
- /* Kill all local variables */
- valuestack = f->f_valuestack;
- for (p = f->f_localsplus; p < valuestack; p++)
- Py_CLEAR(*p);
-
- /* Free stack */
- if (f->f_stacktop != NULL) {
- for (p = valuestack; p < f->f_stacktop; p++)
- Py_XDECREF(*p);
- }
-
- Py_XDECREF(f->f_back);
- Py_DECREF(f->f_builtins);
- Py_DECREF(f->f_globals);
- Py_CLEAR(f->f_locals);
- Py_CLEAR(f->f_trace);
- Py_CLEAR(f->f_exc_type);
- Py_CLEAR(f->f_exc_value);
- Py_CLEAR(f->f_exc_traceback);
-
- co = f->f_code;
- if (co->co_zombieframe == NULL)
- co->co_zombieframe = f;
- else if (numfree < PyFrame_MAXFREELIST) {
- ++numfree;
- f->f_back = free_list;
- free_list = f;
- }
- else
- PyObject_GC_Del(f);
-
- Py_DECREF(co);
- Py_TRASHCAN_SAFE_END(f)
+ PyObject **p, **valuestack;
+ PyCodeObject *co;
+
+ PyObject_GC_UnTrack(f);
+ Py_TRASHCAN_SAFE_BEGIN(f)
+ /* Kill all local variables */
+ valuestack = f->f_valuestack;
+ for (p = f->f_localsplus; p < valuestack; p++)
+ Py_CLEAR(*p);
+
+ /* Free stack */
+ if (f->f_stacktop != NULL) {
+ for (p = valuestack; p < f->f_stacktop; p++)
+ Py_XDECREF(*p);
+ }
+
+ Py_XDECREF(f->f_back);
+ Py_DECREF(f->f_builtins);
+ Py_DECREF(f->f_globals);
+ Py_CLEAR(f->f_locals);
+ Py_CLEAR(f->f_trace);
+ Py_CLEAR(f->f_exc_type);
+ Py_CLEAR(f->f_exc_value);
+ Py_CLEAR(f->f_exc_traceback);
+
+ co = f->f_code;
+ if (co->co_zombieframe == NULL)
+ co->co_zombieframe = f;
+ else if (numfree < PyFrame_MAXFREELIST) {
+ ++numfree;
+ f->f_back = free_list;
+ free_list = f;
+ }
+ else
+ PyObject_GC_Del(f);
+
+ Py_DECREF(co);
+ Py_TRASHCAN_SAFE_END(f)
}
static int
frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
{
- PyObject **fastlocals, **p;
- int i, slots;
-
- Py_VISIT(f->f_back);
- Py_VISIT(f->f_code);
- Py_VISIT(f->f_builtins);
- Py_VISIT(f->f_globals);
- Py_VISIT(f->f_locals);
- Py_VISIT(f->f_trace);
- Py_VISIT(f->f_exc_type);
- Py_VISIT(f->f_exc_value);
- Py_VISIT(f->f_exc_traceback);
-
- /* locals */
- slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
- fastlocals = f->f_localsplus;
- for (i = slots; --i >= 0; ++fastlocals)
- Py_VISIT(*fastlocals);
-
- /* stack */
- if (f->f_stacktop != NULL) {
- for (p = f->f_valuestack; p < f->f_stacktop; p++)
- Py_VISIT(*p);
- }
- return 0;
+ PyObject **fastlocals, **p;
+ int i, slots;
+
+ Py_VISIT(f->f_back);
+ Py_VISIT(f->f_code);
+ Py_VISIT(f->f_builtins);
+ Py_VISIT(f->f_globals);
+ Py_VISIT(f->f_locals);
+ Py_VISIT(f->f_trace);
+ Py_VISIT(f->f_exc_type);
+ Py_VISIT(f->f_exc_value);
+ Py_VISIT(f->f_exc_traceback);
+
+ /* locals */
+ slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
+ fastlocals = f->f_localsplus;
+ for (i = slots; --i >= 0; ++fastlocals)
+ Py_VISIT(*fastlocals);
+
+ /* stack */
+ if (f->f_stacktop != NULL) {
+ for (p = f->f_valuestack; p < f->f_stacktop; p++)
+ Py_VISIT(*p);
+ }
+ return 0;
}
static void
frame_clear(PyFrameObject *f)
{
- PyObject **fastlocals, **p, **oldtop;
- int i, slots;
-
- /* Before anything else, make sure that this frame is clearly marked
- * as being defunct! Else, e.g., a generator reachable from this
- * frame may also point to this frame, believe itself to still be
- * active, and try cleaning up this frame again.
- */
- oldtop = f->f_stacktop;
- f->f_stacktop = NULL;
-
- Py_CLEAR(f->f_exc_type);
- Py_CLEAR(f->f_exc_value);
- Py_CLEAR(f->f_exc_traceback);
- Py_CLEAR(f->f_trace);
-
- /* locals */
- slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
- fastlocals = f->f_localsplus;
- for (i = slots; --i >= 0; ++fastlocals)
- Py_CLEAR(*fastlocals);
-
- /* stack */
- if (oldtop != NULL) {
- for (p = f->f_valuestack; p < oldtop; p++)
- Py_CLEAR(*p);
- }
+ PyObject **fastlocals, **p, **oldtop;
+ int i, slots;
+
+ /* Before anything else, make sure that this frame is clearly marked
+ * as being defunct! Else, e.g., a generator reachable from this
+ * frame may also point to this frame, believe itself to still be
+ * active, and try cleaning up this frame again.
+ */
+ oldtop = f->f_stacktop;
+ f->f_stacktop = NULL;
+
+ Py_CLEAR(f->f_exc_type);
+ Py_CLEAR(f->f_exc_value);
+ Py_CLEAR(f->f_exc_traceback);
+ Py_CLEAR(f->f_trace);
+
+ /* locals */
+ slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
+ fastlocals = f->f_localsplus;
+ for (i = slots; --i >= 0; ++fastlocals)
+ Py_CLEAR(*fastlocals);
+
+ /* stack */
+ if (oldtop != NULL) {
+ for (p = f->f_valuestack; p < oldtop; p++)
+ Py_CLEAR(*p);
+ }
}
static PyObject *
frame_sizeof(PyFrameObject *f)
{
- Py_ssize_t res, extras, ncells, nfrees;
+ Py_ssize_t res, extras, ncells, nfrees;
- ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
- nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
- extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
- ncells + nfrees;
- /* subtract one as it is already included in PyFrameObject */
- res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
+ ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
+ nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
+ extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
+ ncells + nfrees;
+ /* subtract one as it is already included in PyFrameObject */
+ res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
- return PyLong_FromSsize_t(res);
+ return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(sizeof__doc__,
"F.__sizeof__() -> size of F in memory, in bytes");
static PyMethodDef frame_methods[] = {
- {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
- sizeof__doc__},
- {NULL, NULL} /* sentinel */
+ {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
+ sizeof__doc__},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyFrame_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "frame",
- sizeof(PyFrameObject),
- sizeof(PyObject *),
- (destructor)frame_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)frame_traverse, /* tp_traverse */
- (inquiry)frame_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- frame_methods, /* tp_methods */
- frame_memberlist, /* tp_members */
- frame_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "frame",
+ sizeof(PyFrameObject),
+ sizeof(PyObject *),
+ (destructor)frame_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)frame_traverse, /* tp_traverse */
+ (inquiry)frame_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ frame_methods, /* tp_methods */
+ frame_memberlist, /* tp_members */
+ frame_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
};
static PyObject *builtin_object;
int _PyFrame_Init()
{
- builtin_object = PyUnicode_InternFromString("__builtins__");
- if (builtin_object == NULL)
- return 0;
- return 1;
+ builtin_object = PyUnicode_InternFromString("__builtins__");
+ if (builtin_object == NULL)
+ return 0;
+ return 1;
}
PyFrameObject *
PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
- PyObject *locals)
+ PyObject *locals)
{
- PyFrameObject *back = tstate->frame;
- PyFrameObject *f;
- PyObject *builtins;
- Py_ssize_t i;
+ PyFrameObject *back = tstate->frame;
+ PyFrameObject *f;
+ PyObject *builtins;
+ Py_ssize_t i;
#ifdef Py_DEBUG
- if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
- (locals != NULL && !PyMapping_Check(locals))) {
- PyErr_BadInternalCall();
- return NULL;
- }
+ if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
+ (locals != NULL && !PyMapping_Check(locals))) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
#endif
- if (back == NULL || back->f_globals != globals) {
- builtins = PyDict_GetItem(globals, builtin_object);
- if (builtins) {
- if (PyModule_Check(builtins)) {
- builtins = PyModule_GetDict(builtins);
- assert(!builtins || PyDict_Check(builtins));
- }
- else if (!PyDict_Check(builtins))
- builtins = NULL;
- }
- if (builtins == NULL) {
- /* No builtins! Make up a minimal one
- Give them 'None', at least. */
- builtins = PyDict_New();
- if (builtins == NULL ||
- PyDict_SetItemString(
- builtins, "None", Py_None) < 0)
- return NULL;
- }
- else
- Py_INCREF(builtins);
-
- }
- else {
- /* If we share the globals, we share the builtins.
- Save a lookup and a call. */
- builtins = back->f_builtins;
- assert(builtins != NULL && PyDict_Check(builtins));
- Py_INCREF(builtins);
- }
- if (code->co_zombieframe != NULL) {
- f = code->co_zombieframe;
- code->co_zombieframe = NULL;
- _Py_NewReference((PyObject *)f);
- assert(f->f_code == code);
- }
- else {
- Py_ssize_t extras, ncells, nfrees;
- ncells = PyTuple_GET_SIZE(code->co_cellvars);
- nfrees = PyTuple_GET_SIZE(code->co_freevars);
- extras = code->co_stacksize + code->co_nlocals + ncells +
- nfrees;
- if (free_list == NULL) {
- f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
- extras);
- if (f == NULL) {
- Py_DECREF(builtins);
- return NULL;
- }
- }
- else {
- assert(numfree > 0);
- --numfree;
- f = free_list;
- free_list = free_list->f_back;
- if (Py_SIZE(f) < extras) {
- f = PyObject_GC_Resize(PyFrameObject, f, extras);
- if (f == NULL) {
- Py_DECREF(builtins);
- return NULL;
- }
- }
- _Py_NewReference((PyObject *)f);
- }
-
- f->f_code = code;
- extras = code->co_nlocals + ncells + nfrees;
- f->f_valuestack = f->f_localsplus + extras;
- for (i=0; i<extras; i++)
- f->f_localsplus[i] = NULL;
- f->f_locals = NULL;
- f->f_trace = NULL;
- f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
- }
- f->f_stacktop = f->f_valuestack;
- f->f_builtins = builtins;
- Py_XINCREF(back);
- f->f_back = back;
- Py_INCREF(code);
- Py_INCREF(globals);
- f->f_globals = globals;
- /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
- if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
- (CO_NEWLOCALS | CO_OPTIMIZED))
- ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
- else if (code->co_flags & CO_NEWLOCALS) {
- locals = PyDict_New();
- if (locals == NULL) {
- Py_DECREF(f);
- return NULL;
- }
- f->f_locals = locals;
- }
- else {
- if (locals == NULL)
- locals = globals;
- Py_INCREF(locals);
- f->f_locals = locals;
- }
- f->f_tstate = tstate;
-
- f->f_lasti = -1;
- f->f_lineno = code->co_firstlineno;
- f->f_iblock = 0;
-
- _PyObject_GC_TRACK(f);
- return f;
+ if (back == NULL || back->f_globals != globals) {
+ builtins = PyDict_GetItem(globals, builtin_object);
+ if (builtins) {
+ if (PyModule_Check(builtins)) {
+ builtins = PyModule_GetDict(builtins);
+ assert(!builtins || PyDict_Check(builtins));
+ }
+ else if (!PyDict_Check(builtins))
+ builtins = NULL;
+ }
+ if (builtins == NULL) {
+ /* No builtins! Make up a minimal one
+ Give them 'None', at least. */
+ builtins = PyDict_New();
+ if (builtins == NULL ||
+ PyDict_SetItemString(
+ builtins, "None", Py_None) < 0)
+ return NULL;
+ }
+ else
+ Py_INCREF(builtins);
+
+ }
+ else {
+ /* If we share the globals, we share the builtins.
+ Save a lookup and a call. */
+ builtins = back->f_builtins;
+ assert(builtins != NULL && PyDict_Check(builtins));
+ Py_INCREF(builtins);
+ }
+ if (code->co_zombieframe != NULL) {
+ f = code->co_zombieframe;
+ code->co_zombieframe = NULL;
+ _Py_NewReference((PyObject *)f);
+ assert(f->f_code == code);
+ }
+ else {
+ Py_ssize_t extras, ncells, nfrees;
+ ncells = PyTuple_GET_SIZE(code->co_cellvars);
+ nfrees = PyTuple_GET_SIZE(code->co_freevars);
+ extras = code->co_stacksize + code->co_nlocals + ncells +
+ nfrees;
+ if (free_list == NULL) {
+ f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
+ extras);
+ if (f == NULL) {
+ Py_DECREF(builtins);
+ return NULL;
+ }
+ }
+ else {
+ assert(numfree > 0);
+ --numfree;
+ f = free_list;
+ free_list = free_list->f_back;
+ if (Py_SIZE(f) < extras) {
+ f = PyObject_GC_Resize(PyFrameObject, f, extras);
+ if (f == NULL) {
+ Py_DECREF(builtins);
+ return NULL;
+ }
+ }
+ _Py_NewReference((PyObject *)f);
+ }
+
+ f->f_code = code;
+ extras = code->co_nlocals + ncells + nfrees;
+ f->f_valuestack = f->f_localsplus + extras;
+ for (i=0; i<extras; i++)
+ f->f_localsplus[i] = NULL;
+ f->f_locals = NULL;
+ f->f_trace = NULL;
+ f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
+ }
+ f->f_stacktop = f->f_valuestack;
+ f->f_builtins = builtins;
+ Py_XINCREF(back);
+ f->f_back = back;
+ Py_INCREF(code);
+ Py_INCREF(globals);
+ f->f_globals = globals;
+ /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
+ if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
+ (CO_NEWLOCALS | CO_OPTIMIZED))
+ ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
+ else if (code->co_flags & CO_NEWLOCALS) {
+ locals = PyDict_New();
+ if (locals == NULL) {
+ Py_DECREF(f);
+ return NULL;
+ }
+ f->f_locals = locals;
+ }
+ else {
+ if (locals == NULL)
+ locals = globals;
+ Py_INCREF(locals);
+ f->f_locals = locals;
+ }
+ f->f_tstate = tstate;
+
+ f->f_lasti = -1;
+ f->f_lineno = code->co_firstlineno;
+ f->f_iblock = 0;
+
+ _PyObject_GC_TRACK(f);
+ return f;
}
/* Block management */
@@ -721,28 +721,28 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
void
PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
{
- PyTryBlock *b;
- if (f->f_iblock >= CO_MAXBLOCKS)
- Py_FatalError("XXX block stack overflow");
- b = &f->f_blockstack[f->f_iblock++];
- b->b_type = type;
- b->b_level = level;
- b->b_handler = handler;
+ PyTryBlock *b;
+ if (f->f_iblock >= CO_MAXBLOCKS)
+ Py_FatalError("XXX block stack overflow");
+ b = &f->f_blockstack[f->f_iblock++];
+ b->b_type = type;
+ b->b_level = level;
+ b->b_handler = handler;
}
PyTryBlock *
PyFrame_BlockPop(PyFrameObject *f)
{
- PyTryBlock *b;
- if (f->f_iblock <= 0)
- Py_FatalError("XXX block stack underflow");
- b = &f->f_blockstack[--f->f_iblock];
- return b;
+ PyTryBlock *b;
+ if (f->f_iblock <= 0)
+ Py_FatalError("XXX block stack underflow");
+ b = &f->f_blockstack[--f->f_iblock];
+ return b;
}
/* Convert between "fast" version of locals and dictionary version.
-
- map and values are input arguments. map is a tuple of strings.
+
+ map and values are input arguments. map is a tuple of strings.
values is an array of PyObject*. At index i, map[i] is the name of
the variable with value values[i]. The function copies the first
nmap variable from map/values into dict. If values[i] is NULL,
@@ -758,29 +758,29 @@ PyFrame_BlockPop(PyFrameObject *f)
static void
map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
- int deref)
+ int deref)
{
- Py_ssize_t j;
- assert(PyTuple_Check(map));
- assert(PyDict_Check(dict));
- assert(PyTuple_Size(map) >= nmap);
- for (j = nmap; --j >= 0; ) {
- PyObject *key = PyTuple_GET_ITEM(map, j);
- PyObject *value = values[j];
- assert(PyUnicode_Check(key));
- if (deref) {
- assert(PyCell_Check(value));
- value = PyCell_GET(value);
- }
- if (value == NULL) {
- if (PyObject_DelItem(dict, key) != 0)
- PyErr_Clear();
- }
- else {
- if (PyObject_SetItem(dict, key, value) != 0)
- PyErr_Clear();
- }
- }
+ Py_ssize_t j;
+ assert(PyTuple_Check(map));
+ assert(PyDict_Check(dict));
+ assert(PyTuple_Size(map) >= nmap);
+ for (j = nmap; --j >= 0; ) {
+ PyObject *key = PyTuple_GET_ITEM(map, j);
+ PyObject *value = values[j];
+ assert(PyUnicode_Check(key));
+ if (deref) {
+ assert(PyCell_Check(value));
+ value = PyCell_GET(value);
+ }
+ if (value == NULL) {
+ if (PyObject_DelItem(dict, key) != 0)
+ PyErr_Clear();
+ }
+ else {
+ if (PyObject_SetItem(dict, key, value) != 0)
+ PyErr_Clear();
+ }
+ }
}
/* Copy values from the "locals" dict into the fast locals.
@@ -788,7 +788,7 @@ map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
dict is an input argument containing string keys representing
variables names and arbitrary PyObject* as values.
- map and values are input arguments. map is a tuple of strings.
+ map and values are input arguments. map is a tuple of strings.
values is an array of PyObject*. At index i, map[i] is the name of
the variable with value values[i]. The function copies the first
nmap variable from map/values into dict. If values[i] is NULL,
@@ -806,150 +806,150 @@ map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
static void
dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
- int deref, int clear)
+ int deref, int clear)
{
- Py_ssize_t j;
- assert(PyTuple_Check(map));
- assert(PyDict_Check(dict));
- assert(PyTuple_Size(map) >= nmap);
- for (j = nmap; --j >= 0; ) {
- PyObject *key = PyTuple_GET_ITEM(map, j);
- PyObject *value = PyObject_GetItem(dict, key);
- assert(PyUnicode_Check(key));
- /* We only care about NULLs if clear is true. */
- if (value == NULL) {
- PyErr_Clear();
- if (!clear)
- continue;
- }
- if (deref) {
- assert(PyCell_Check(values[j]));
- if (PyCell_GET(values[j]) != value) {
- if (PyCell_Set(values[j], value) < 0)
- PyErr_Clear();
- }
- } else if (values[j] != value) {
- Py_XINCREF(value);
- Py_XDECREF(values[j]);
- values[j] = value;
- }
- Py_XDECREF(value);
- }
+ Py_ssize_t j;
+ assert(PyTuple_Check(map));
+ assert(PyDict_Check(dict));
+ assert(PyTuple_Size(map) >= nmap);
+ for (j = nmap; --j >= 0; ) {
+ PyObject *key = PyTuple_GET_ITEM(map, j);
+ PyObject *value = PyObject_GetItem(dict, key);
+ assert(PyUnicode_Check(key));
+ /* We only care about NULLs if clear is true. */
+ if (value == NULL) {
+ PyErr_Clear();
+ if (!clear)
+ continue;
+ }
+ if (deref) {
+ assert(PyCell_Check(values[j]));
+ if (PyCell_GET(values[j]) != value) {
+ if (PyCell_Set(values[j], value) < 0)
+ PyErr_Clear();
+ }
+ } else if (values[j] != value) {
+ Py_XINCREF(value);
+ Py_XDECREF(values[j]);
+ values[j] = value;
+ }
+ Py_XDECREF(value);
+ }
}
void
PyFrame_FastToLocals(PyFrameObject *f)
{
- /* Merge fast locals into f->f_locals */
- PyObject *locals, *map;
- PyObject **fast;
- PyObject *error_type, *error_value, *error_traceback;
- PyCodeObject *co;
- Py_ssize_t j;
- int ncells, nfreevars;
- if (f == NULL)
- return;
- locals = f->f_locals;
- if (locals == NULL) {
- locals = f->f_locals = PyDict_New();
- if (locals == NULL) {
- PyErr_Clear(); /* Can't report it :-( */
- return;
- }
- }
- co = f->f_code;
- map = co->co_varnames;
- if (!PyTuple_Check(map))
- return;
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
- fast = f->f_localsplus;
- j = PyTuple_GET_SIZE(map);
- if (j > co->co_nlocals)
- j = co->co_nlocals;
- if (co->co_nlocals)
- map_to_dict(map, j, locals, fast, 0);
- ncells = PyTuple_GET_SIZE(co->co_cellvars);
- nfreevars = PyTuple_GET_SIZE(co->co_freevars);
- if (ncells || nfreevars) {
- map_to_dict(co->co_cellvars, ncells,
- locals, fast + co->co_nlocals, 1);
- /* If the namespace is unoptimized, then one of the
- following cases applies:
- 1. It does not contain free variables, because it
- uses import * or is a top-level namespace.
- 2. It is a class namespace.
- We don't want to accidentally copy free variables
- into the locals dict used by the class.
- */
- if (co->co_flags & CO_OPTIMIZED) {
- map_to_dict(co->co_freevars, nfreevars,
- locals, fast + co->co_nlocals + ncells, 1);
- }
- }
- PyErr_Restore(error_type, error_value, error_traceback);
+ /* Merge fast locals into f->f_locals */
+ PyObject *locals, *map;
+ PyObject **fast;
+ PyObject *error_type, *error_value, *error_traceback;
+ PyCodeObject *co;
+ Py_ssize_t j;
+ int ncells, nfreevars;
+ if (f == NULL)
+ return;
+ locals = f->f_locals;
+ if (locals == NULL) {
+ locals = f->f_locals = PyDict_New();
+ if (locals == NULL) {
+ PyErr_Clear(); /* Can't report it :-( */
+ return;
+ }
+ }
+ co = f->f_code;
+ map = co->co_varnames;
+ if (!PyTuple_Check(map))
+ return;
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+ fast = f->f_localsplus;
+ j = PyTuple_GET_SIZE(map);
+ if (j > co->co_nlocals)
+ j = co->co_nlocals;
+ if (co->co_nlocals)
+ map_to_dict(map, j, locals, fast, 0);
+ ncells = PyTuple_GET_SIZE(co->co_cellvars);
+ nfreevars = PyTuple_GET_SIZE(co->co_freevars);
+ if (ncells || nfreevars) {
+ map_to_dict(co->co_cellvars, ncells,
+ locals, fast + co->co_nlocals, 1);
+ /* If the namespace is unoptimized, then one of the
+ following cases applies:
+ 1. It does not contain free variables, because it
+ uses import * or is a top-level namespace.
+ 2. It is a class namespace.
+ We don't want to accidentally copy free variables
+ into the locals dict used by the class.
+ */
+ if (co->co_flags & CO_OPTIMIZED) {
+ map_to_dict(co->co_freevars, nfreevars,
+ locals, fast + co->co_nlocals + ncells, 1);
+ }
+ }
+ PyErr_Restore(error_type, error_value, error_traceback);
}
void
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
{
- /* Merge f->f_locals into fast locals */
- PyObject *locals, *map;
- PyObject **fast;
- PyObject *error_type, *error_value, *error_traceback;
- PyCodeObject *co;
- Py_ssize_t j;
- int ncells, nfreevars;
- if (f == NULL)
- return;
- locals = f->f_locals;
- co = f->f_code;
- map = co->co_varnames;
- if (locals == NULL)
- return;
- if (!PyTuple_Check(map))
- return;
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
- fast = f->f_localsplus;
- j = PyTuple_GET_SIZE(map);
- if (j > co->co_nlocals)
- j = co->co_nlocals;
- if (co->co_nlocals)
- dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
- ncells = PyTuple_GET_SIZE(co->co_cellvars);
- nfreevars = PyTuple_GET_SIZE(co->co_freevars);
- if (ncells || nfreevars) {
- dict_to_map(co->co_cellvars, ncells,
- locals, fast + co->co_nlocals, 1, clear);
- /* Same test as in PyFrame_FastToLocals() above. */
- if (co->co_flags & CO_OPTIMIZED) {
- dict_to_map(co->co_freevars, nfreevars,
- locals, fast + co->co_nlocals + ncells, 1,
- clear);
- }
- }
- PyErr_Restore(error_type, error_value, error_traceback);
+ /* Merge f->f_locals into fast locals */
+ PyObject *locals, *map;
+ PyObject **fast;
+ PyObject *error_type, *error_value, *error_traceback;
+ PyCodeObject *co;
+ Py_ssize_t j;
+ int ncells, nfreevars;
+ if (f == NULL)
+ return;
+ locals = f->f_locals;
+ co = f->f_code;
+ map = co->co_varnames;
+ if (locals == NULL)
+ return;
+ if (!PyTuple_Check(map))
+ return;
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+ fast = f->f_localsplus;
+ j = PyTuple_GET_SIZE(map);
+ if (j > co->co_nlocals)
+ j = co->co_nlocals;
+ if (co->co_nlocals)
+ dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
+ ncells = PyTuple_GET_SIZE(co->co_cellvars);
+ nfreevars = PyTuple_GET_SIZE(co->co_freevars);
+ if (ncells || nfreevars) {
+ dict_to_map(co->co_cellvars, ncells,
+ locals, fast + co->co_nlocals, 1, clear);
+ /* Same test as in PyFrame_FastToLocals() above. */
+ if (co->co_flags & CO_OPTIMIZED) {
+ dict_to_map(co->co_freevars, nfreevars,
+ locals, fast + co->co_nlocals + ncells, 1,
+ clear);
+ }
+ }
+ PyErr_Restore(error_type, error_value, error_traceback);
}
/* Clear out the free list */
int
PyFrame_ClearFreeList(void)
{
- int freelist_size = numfree;
-
- while (free_list != NULL) {
- PyFrameObject *f = free_list;
- free_list = free_list->f_back;
- PyObject_GC_Del(f);
- --numfree;
- }
- assert(numfree == 0);
- return freelist_size;
+ int freelist_size = numfree;
+
+ while (free_list != NULL) {
+ PyFrameObject *f = free_list;
+ free_list = free_list->f_back;
+ PyObject_GC_Del(f);
+ --numfree;
+ }
+ assert(numfree == 0);
+ return freelist_size;
}
void
PyFrame_Fini(void)
{
- (void)PyFrame_ClearFreeList();
- Py_XDECREF(builtin_object);
- builtin_object = NULL;
+ (void)PyFrame_ClearFreeList();
+ Py_XDECREF(builtin_object);
+ builtin_object = NULL;
}
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 35fc32d..c2ad964 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -9,215 +9,215 @@
PyObject *
PyFunction_New(PyObject *code, PyObject *globals)
{
- PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
- &PyFunction_Type);
- static PyObject *__name__ = 0;
- if (op != NULL) {
- PyObject *doc;
- PyObject *consts;
- PyObject *module;
- op->func_weakreflist = NULL;
- Py_INCREF(code);
- op->func_code = code;
- Py_INCREF(globals);
- op->func_globals = globals;
- op->func_name = ((PyCodeObject *)code)->co_name;
- Py_INCREF(op->func_name);
- op->func_defaults = NULL; /* No default arguments */
- op->func_kwdefaults = NULL; /* No keyword only defaults */
- op->func_closure = NULL;
- consts = ((PyCodeObject *)code)->co_consts;
- if (PyTuple_Size(consts) >= 1) {
- doc = PyTuple_GetItem(consts, 0);
- if (!PyUnicode_Check(doc))
- doc = Py_None;
- }
- else
- doc = Py_None;
- Py_INCREF(doc);
- op->func_doc = doc;
- op->func_dict = NULL;
- op->func_module = NULL;
- op->func_annotations = NULL;
-
- /* __module__: If module name is in globals, use it.
- Otherwise, use None.
- */
- if (!__name__) {
- __name__ = PyUnicode_InternFromString("__name__");
- if (!__name__) {
- Py_DECREF(op);
- return NULL;
- }
- }
- module = PyDict_GetItem(globals, __name__);
- if (module) {
- Py_INCREF(module);
- op->func_module = module;
- }
- }
- else
- return NULL;
- _PyObject_GC_TRACK(op);
- return (PyObject *)op;
+ PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,
+ &PyFunction_Type);
+ static PyObject *__name__ = 0;
+ if (op != NULL) {
+ PyObject *doc;
+ PyObject *consts;
+ PyObject *module;
+ op->func_weakreflist = NULL;
+ Py_INCREF(code);
+ op->func_code = code;
+ Py_INCREF(globals);
+ op->func_globals = globals;
+ op->func_name = ((PyCodeObject *)code)->co_name;
+ Py_INCREF(op->func_name);
+ op->func_defaults = NULL; /* No default arguments */
+ op->func_kwdefaults = NULL; /* No keyword only defaults */
+ op->func_closure = NULL;
+ consts = ((PyCodeObject *)code)->co_consts;
+ if (PyTuple_Size(consts) >= 1) {
+ doc = PyTuple_GetItem(consts, 0);
+ if (!PyUnicode_Check(doc))
+ doc = Py_None;
+ }
+ else
+ doc = Py_None;
+ Py_INCREF(doc);
+ op->func_doc = doc;
+ op->func_dict = NULL;
+ op->func_module = NULL;
+ op->func_annotations = NULL;
+
+ /* __module__: If module name is in globals, use it.
+ Otherwise, use None.
+ */
+ if (!__name__) {
+ __name__ = PyUnicode_InternFromString("__name__");
+ if (!__name__) {
+ Py_DECREF(op);
+ return NULL;
+ }
+ }
+ module = PyDict_GetItem(globals, __name__);
+ if (module) {
+ Py_INCREF(module);
+ op->func_module = module;
+ }
+ }
+ else
+ return NULL;
+ _PyObject_GC_TRACK(op);
+ return (PyObject *)op;
}
PyObject *
PyFunction_GetCode(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_code;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_code;
}
PyObject *
PyFunction_GetGlobals(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_globals;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_globals;
}
PyObject *
PyFunction_GetModule(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_module;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_module;
}
PyObject *
PyFunction_GetDefaults(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_defaults;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_defaults;
}
int
PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (defaults == Py_None)
- defaults = NULL;
- else if (defaults && PyTuple_Check(defaults)) {
- Py_INCREF(defaults);
- }
- else {
- PyErr_SetString(PyExc_SystemError, "non-tuple default args");
- return -1;
- }
- Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
- ((PyFunctionObject *) op) -> func_defaults = defaults;
- return 0;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (defaults == Py_None)
+ defaults = NULL;
+ else if (defaults && PyTuple_Check(defaults)) {
+ Py_INCREF(defaults);
+ }
+ else {
+ PyErr_SetString(PyExc_SystemError, "non-tuple default args");
+ return -1;
+ }
+ Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);
+ ((PyFunctionObject *) op) -> func_defaults = defaults;
+ return 0;
}
PyObject *
PyFunction_GetKwDefaults(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_kwdefaults;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_kwdefaults;
}
int
PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (defaults == Py_None)
- defaults = NULL;
- else if (defaults && PyDict_Check(defaults)) {
- Py_INCREF(defaults);
- }
- else {
- PyErr_SetString(PyExc_SystemError,
- "non-dict keyword only default args");
- return -1;
- }
- Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
- ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
- return 0;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (defaults == Py_None)
+ defaults = NULL;
+ else if (defaults && PyDict_Check(defaults)) {
+ Py_INCREF(defaults);
+ }
+ else {
+ PyErr_SetString(PyExc_SystemError,
+ "non-dict keyword only default args");
+ return -1;
+ }
+ Py_XDECREF(((PyFunctionObject *)op) -> func_kwdefaults);
+ ((PyFunctionObject *) op) -> func_kwdefaults = defaults;
+ return 0;
}
PyObject *
PyFunction_GetClosure(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_closure;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_closure;
}
int
PyFunction_SetClosure(PyObject *op, PyObject *closure)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (closure == Py_None)
- closure = NULL;
- else if (PyTuple_Check(closure)) {
- Py_INCREF(closure);
- }
- else {
- PyErr_Format(PyExc_SystemError,
- "expected tuple for closure, got '%.100s'",
- closure->ob_type->tp_name);
- return -1;
- }
- Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
- ((PyFunctionObject *) op) -> func_closure = closure;
- return 0;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (closure == Py_None)
+ closure = NULL;
+ else if (PyTuple_Check(closure)) {
+ Py_INCREF(closure);
+ }
+ else {
+ PyErr_Format(PyExc_SystemError,
+ "expected tuple for closure, got '%.100s'",
+ closure->ob_type->tp_name);
+ return -1;
+ }
+ Py_XDECREF(((PyFunctionObject *) op) -> func_closure);
+ ((PyFunctionObject *) op) -> func_closure = closure;
+ return 0;
}
PyObject *
PyFunction_GetAnnotations(PyObject *op)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyFunctionObject *) op) -> func_annotations;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyFunctionObject *) op) -> func_annotations;
}
int
PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
{
- if (!PyFunction_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (annotations == Py_None)
- annotations = NULL;
- else if (annotations && PyDict_Check(annotations)) {
- Py_INCREF(annotations);
- }
- else {
- PyErr_SetString(PyExc_SystemError,
- "non-dict annotations");
- return -1;
- }
- Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
- ((PyFunctionObject *) op) -> func_annotations = annotations;
- return 0;
+ if (!PyFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (annotations == Py_None)
+ annotations = NULL;
+ else if (annotations && PyDict_Check(annotations)) {
+ Py_INCREF(annotations);
+ }
+ else {
+ PyErr_SetString(PyExc_SystemError,
+ "non-dict annotations");
+ return -1;
+ }
+ Py_XDECREF(((PyFunctionObject *)op) -> func_annotations);
+ ((PyFunctionObject *) op) -> func_annotations = annotations;
+ return 0;
}
/* Methods */
@@ -225,224 +225,224 @@ PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
#define OFF(x) offsetof(PyFunctionObject, x)
static PyMemberDef func_memberlist[] = {
- {"__closure__", T_OBJECT, OFF(func_closure),
- RESTRICTED|READONLY},
- {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
- {"__globals__", T_OBJECT, OFF(func_globals),
- RESTRICTED|READONLY},
- {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
- {NULL} /* Sentinel */
+ {"__closure__", T_OBJECT, OFF(func_closure),
+ RESTRICTED|READONLY},
+ {"__doc__", T_OBJECT, OFF(func_doc), PY_WRITE_RESTRICTED},
+ {"__globals__", T_OBJECT, OFF(func_globals),
+ RESTRICTED|READONLY},
+ {"__module__", T_OBJECT, OFF(func_module), PY_WRITE_RESTRICTED},
+ {NULL} /* Sentinel */
};
static PyObject *
func_get_dict(PyFunctionObject *op)
{
- if (op->func_dict == NULL) {
- op->func_dict = PyDict_New();
- if (op->func_dict == NULL)
- return NULL;
- }
- Py_INCREF(op->func_dict);
- return op->func_dict;
+ if (op->func_dict == NULL) {
+ op->func_dict = PyDict_New();
+ if (op->func_dict == NULL)
+ return NULL;
+ }
+ Py_INCREF(op->func_dict);
+ return op->func_dict;
}
static int
func_set_dict(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
-
- /* It is illegal to del f.func_dict */
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "function's dictionary may not be deleted");
- return -1;
- }
- /* Can only set func_dict to a dictionary */
- if (!PyDict_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "setting function's dictionary to a non-dict");
- return -1;
- }
- tmp = op->func_dict;
- Py_INCREF(value);
- op->func_dict = value;
- Py_XDECREF(tmp);
- return 0;
+ PyObject *tmp;
+
+ /* It is illegal to del f.func_dict */
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "function's dictionary may not be deleted");
+ return -1;
+ }
+ /* Can only set func_dict to a dictionary */
+ if (!PyDict_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "setting function's dictionary to a non-dict");
+ return -1;
+ }
+ tmp = op->func_dict;
+ Py_INCREF(value);
+ op->func_dict = value;
+ Py_XDECREF(tmp);
+ return 0;
}
static PyObject *
func_get_code(PyFunctionObject *op)
{
- Py_INCREF(op->func_code);
- return op->func_code;
+ Py_INCREF(op->func_code);
+ return op->func_code;
}
static int
func_set_code(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
- Py_ssize_t nfree, nclosure;
-
- /* Not legal to del f.func_code or to set it to anything
- * other than a code object. */
- if (value == NULL || !PyCode_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "__code__ must be set to a code object");
- return -1;
- }
- nfree = PyCode_GetNumFree((PyCodeObject *)value);
- nclosure = (op->func_closure == NULL ? 0 :
- PyTuple_GET_SIZE(op->func_closure));
- if (nclosure != nfree) {
- PyErr_Format(PyExc_ValueError,
- "%U() requires a code object with %zd free vars,"
- " not %zd",
- op->func_name,
- nclosure, nfree);
- return -1;
- }
- tmp = op->func_code;
- Py_INCREF(value);
- op->func_code = value;
- Py_DECREF(tmp);
- return 0;
+ PyObject *tmp;
+ Py_ssize_t nfree, nclosure;
+
+ /* Not legal to del f.func_code or to set it to anything
+ * other than a code object. */
+ if (value == NULL || !PyCode_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__code__ must be set to a code object");
+ return -1;
+ }
+ nfree = PyCode_GetNumFree((PyCodeObject *)value);
+ nclosure = (op->func_closure == NULL ? 0 :
+ PyTuple_GET_SIZE(op->func_closure));
+ if (nclosure != nfree) {
+ PyErr_Format(PyExc_ValueError,
+ "%U() requires a code object with %zd free vars,"
+ " not %zd",
+ op->func_name,
+ nclosure, nfree);
+ return -1;
+ }
+ tmp = op->func_code;
+ Py_INCREF(value);
+ op->func_code = value;
+ Py_DECREF(tmp);
+ return 0;
}
static PyObject *
func_get_name(PyFunctionObject *op)
{
- Py_INCREF(op->func_name);
- return op->func_name;
+ Py_INCREF(op->func_name);
+ return op->func_name;
}
static int
func_set_name(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
+ PyObject *tmp;
- /* Not legal to del f.func_name or to set it to anything
- * other than a string object. */
- if (value == NULL || !PyUnicode_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "__name__ must be set to a string object");
- return -1;
- }
- tmp = op->func_name;
- Py_INCREF(value);
- op->func_name = value;
- Py_DECREF(tmp);
- return 0;
+ /* Not legal to del f.func_name or to set it to anything
+ * other than a string object. */
+ if (value == NULL || !PyUnicode_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__name__ must be set to a string object");
+ return -1;
+ }
+ tmp = op->func_name;
+ Py_INCREF(value);
+ op->func_name = value;
+ Py_DECREF(tmp);
+ return 0;
}
static PyObject *
func_get_defaults(PyFunctionObject *op)
{
- if (op->func_defaults == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- Py_INCREF(op->func_defaults);
- return op->func_defaults;
+ if (op->func_defaults == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ Py_INCREF(op->func_defaults);
+ return op->func_defaults;
}
static int
func_set_defaults(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
+ PyObject *tmp;
- /* Legal to del f.func_defaults.
- * Can only set func_defaults to NULL or a tuple. */
- if (value == Py_None)
- value = NULL;
- if (value != NULL && !PyTuple_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "__defaults__ must be set to a tuple object");
- return -1;
- }
- tmp = op->func_defaults;
- Py_XINCREF(value);
- op->func_defaults = value;
- Py_XDECREF(tmp);
- return 0;
+ /* Legal to del f.func_defaults.
+ * Can only set func_defaults to NULL or a tuple. */
+ if (value == Py_None)
+ value = NULL;
+ if (value != NULL && !PyTuple_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__defaults__ must be set to a tuple object");
+ return -1;
+ }
+ tmp = op->func_defaults;
+ Py_XINCREF(value);
+ op->func_defaults = value;
+ Py_XDECREF(tmp);
+ return 0;
}
static PyObject *
func_get_kwdefaults(PyFunctionObject *op)
{
- if (op->func_kwdefaults == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- Py_INCREF(op->func_kwdefaults);
- return op->func_kwdefaults;
+ if (op->func_kwdefaults == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ Py_INCREF(op->func_kwdefaults);
+ return op->func_kwdefaults;
}
static int
func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
+ PyObject *tmp;
- if (value == Py_None)
- value = NULL;
- /* Legal to del f.func_kwdefaults.
- * Can only set func_kwdefaults to NULL or a dict. */
- if (value != NULL && !PyDict_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "__kwdefaults__ must be set to a dict object");
- return -1;
- }
- tmp = op->func_kwdefaults;
- Py_XINCREF(value);
- op->func_kwdefaults = value;
- Py_XDECREF(tmp);
- return 0;
+ if (value == Py_None)
+ value = NULL;
+ /* Legal to del f.func_kwdefaults.
+ * Can only set func_kwdefaults to NULL or a dict. */
+ if (value != NULL && !PyDict_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__kwdefaults__ must be set to a dict object");
+ return -1;
+ }
+ tmp = op->func_kwdefaults;
+ Py_XINCREF(value);
+ op->func_kwdefaults = value;
+ Py_XDECREF(tmp);
+ return 0;
}
static PyObject *
func_get_annotations(PyFunctionObject *op)
{
- if (op->func_annotations == NULL) {
- op->func_annotations = PyDict_New();
- if (op->func_annotations == NULL)
- return NULL;
- }
- Py_INCREF(op->func_annotations);
- return op->func_annotations;
+ if (op->func_annotations == NULL) {
+ op->func_annotations = PyDict_New();
+ if (op->func_annotations == NULL)
+ return NULL;
+ }
+ Py_INCREF(op->func_annotations);
+ return op->func_annotations;
}
static int
func_set_annotations(PyFunctionObject *op, PyObject *value)
{
- PyObject *tmp;
-
- if (value == Py_None)
- value = NULL;
- /* Legal to del f.func_annotations.
- * Can only set func_annotations to NULL (through C api)
- * or a dict. */
- if (value != NULL && !PyDict_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "__annotations__ must be set to a dict object");
- return -1;
- }
- tmp = op->func_annotations;
- Py_XINCREF(value);
- op->func_annotations = value;
- Py_XDECREF(tmp);
- return 0;
+ PyObject *tmp;
+
+ if (value == Py_None)
+ value = NULL;
+ /* Legal to del f.func_annotations.
+ * Can only set func_annotations to NULL (through C api)
+ * or a dict. */
+ if (value != NULL && !PyDict_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__annotations__ must be set to a dict object");
+ return -1;
+ }
+ tmp = op->func_annotations;
+ Py_XINCREF(value);
+ op->func_annotations = value;
+ Py_XDECREF(tmp);
+ return 0;
}
static PyGetSetDef func_getsetlist[] = {
- {"__code__", (getter)func_get_code, (setter)func_set_code},
- {"__defaults__", (getter)func_get_defaults,
- (setter)func_set_defaults},
- {"__kwdefaults__", (getter)func_get_kwdefaults,
- (setter)func_set_kwdefaults},
- {"__annotations__", (getter)func_get_annotations,
- (setter)func_set_annotations},
- {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
- {"__name__", (getter)func_get_name, (setter)func_set_name},
- {NULL} /* Sentinel */
+ {"__code__", (getter)func_get_code, (setter)func_set_code},
+ {"__defaults__", (getter)func_get_defaults,
+ (setter)func_set_defaults},
+ {"__kwdefaults__", (getter)func_get_kwdefaults,
+ (setter)func_set_kwdefaults},
+ {"__annotations__", (getter)func_get_annotations,
+ (setter)func_set_annotations},
+ {"__dict__", (getter)func_get_dict, (setter)func_set_dict},
+ {"__name__", (getter)func_get_name, (setter)func_set_name},
+ {NULL} /* Sentinel */
};
PyDoc_STRVAR(func_doc,
@@ -455,241 +455,241 @@ The optional closure tuple supplies the bindings for free variables.");
/* func_new() maintains the following invariants for closures. The
closure must correspond to the free variables of the code object.
-
- if len(code.co_freevars) == 0:
- closure = NULL
+
+ if len(code.co_freevars) == 0:
+ closure = NULL
else:
- len(closure) == len(code.co_freevars)
+ len(closure) == len(code.co_freevars)
for every elt in closure, type(elt) == cell
*/
static PyObject *
func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
{
- PyCodeObject *code;
- PyObject *globals;
- PyObject *name = Py_None;
- PyObject *defaults = Py_None;
- PyObject *closure = Py_None;
- PyFunctionObject *newfunc;
- Py_ssize_t nfree, nclosure;
- static char *kwlist[] = {"code", "globals", "name",
- "argdefs", "closure", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
- kwlist,
- &PyCode_Type, &code,
- &PyDict_Type, &globals,
- &name, &defaults, &closure))
- return NULL;
- if (name != Py_None && !PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "arg 3 (name) must be None or string");
- return NULL;
- }
- if (defaults != Py_None && !PyTuple_Check(defaults)) {
- PyErr_SetString(PyExc_TypeError,
- "arg 4 (defaults) must be None or tuple");
- return NULL;
- }
- nfree = PyTuple_GET_SIZE(code->co_freevars);
- if (!PyTuple_Check(closure)) {
- if (nfree && closure == Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "arg 5 (closure) must be tuple");
- return NULL;
- }
- else if (closure != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "arg 5 (closure) must be None or tuple");
- return NULL;
- }
- }
-
- /* check that the closure is well-formed */
- nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
- if (nfree != nclosure)
- return PyErr_Format(PyExc_ValueError,
- "%U requires closure of length %zd, not %zd",
- code->co_name, nfree, nclosure);
- if (nclosure) {
- Py_ssize_t i;
- for (i = 0; i < nclosure; i++) {
- PyObject *o = PyTuple_GET_ITEM(closure, i);
- if (!PyCell_Check(o)) {
- return PyErr_Format(PyExc_TypeError,
- "arg 5 (closure) expected cell, found %s",
- o->ob_type->tp_name);
- }
- }
- }
-
- newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
- globals);
- if (newfunc == NULL)
- return NULL;
-
- if (name != Py_None) {
- Py_INCREF(name);
- Py_DECREF(newfunc->func_name);
- newfunc->func_name = name;
- }
- if (defaults != Py_None) {
- Py_INCREF(defaults);
- newfunc->func_defaults = defaults;
- }
- if (closure != Py_None) {
- Py_INCREF(closure);
- newfunc->func_closure = closure;
- }
-
- return (PyObject *)newfunc;
+ PyCodeObject *code;
+ PyObject *globals;
+ PyObject *name = Py_None;
+ PyObject *defaults = Py_None;
+ PyObject *closure = Py_None;
+ PyFunctionObject *newfunc;
+ Py_ssize_t nfree, nclosure;
+ static char *kwlist[] = {"code", "globals", "name",
+ "argdefs", "closure", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",
+ kwlist,
+ &PyCode_Type, &code,
+ &PyDict_Type, &globals,
+ &name, &defaults, &closure))
+ return NULL;
+ if (name != Py_None && !PyUnicode_Check(name)) {
+ PyErr_SetString(PyExc_TypeError,
+ "arg 3 (name) must be None or string");
+ return NULL;
+ }
+ if (defaults != Py_None && !PyTuple_Check(defaults)) {
+ PyErr_SetString(PyExc_TypeError,
+ "arg 4 (defaults) must be None or tuple");
+ return NULL;
+ }
+ nfree = PyTuple_GET_SIZE(code->co_freevars);
+ if (!PyTuple_Check(closure)) {
+ if (nfree && closure == Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "arg 5 (closure) must be tuple");
+ return NULL;
+ }
+ else if (closure != Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "arg 5 (closure) must be None or tuple");
+ return NULL;
+ }
+ }
+
+ /* check that the closure is well-formed */
+ nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
+ if (nfree != nclosure)
+ return PyErr_Format(PyExc_ValueError,
+ "%U requires closure of length %zd, not %zd",
+ code->co_name, nfree, nclosure);
+ if (nclosure) {
+ Py_ssize_t i;
+ for (i = 0; i < nclosure; i++) {
+ PyObject *o = PyTuple_GET_ITEM(closure, i);
+ if (!PyCell_Check(o)) {
+ return PyErr_Format(PyExc_TypeError,
+ "arg 5 (closure) expected cell, found %s",
+ o->ob_type->tp_name);
+ }
+ }
+ }
+
+ newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
+ globals);
+ if (newfunc == NULL)
+ return NULL;
+
+ if (name != Py_None) {
+ Py_INCREF(name);
+ Py_DECREF(newfunc->func_name);
+ newfunc->func_name = name;
+ }
+ if (defaults != Py_None) {
+ Py_INCREF(defaults);
+ newfunc->func_defaults = defaults;
+ }
+ if (closure != Py_None) {
+ Py_INCREF(closure);
+ newfunc->func_closure = closure;
+ }
+
+ return (PyObject *)newfunc;
}
static void
func_dealloc(PyFunctionObject *op)
{
- _PyObject_GC_UNTRACK(op);
- if (op->func_weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) op);
- Py_DECREF(op->func_code);
- Py_DECREF(op->func_globals);
- Py_XDECREF(op->func_module);
- Py_DECREF(op->func_name);
- Py_XDECREF(op->func_defaults);
- Py_XDECREF(op->func_kwdefaults);
- Py_XDECREF(op->func_doc);
- Py_XDECREF(op->func_dict);
- Py_XDECREF(op->func_closure);
- Py_XDECREF(op->func_annotations);
- PyObject_GC_Del(op);
+ _PyObject_GC_UNTRACK(op);
+ if (op->func_weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) op);
+ Py_DECREF(op->func_code);
+ Py_DECREF(op->func_globals);
+ Py_XDECREF(op->func_module);
+ Py_DECREF(op->func_name);
+ Py_XDECREF(op->func_defaults);
+ Py_XDECREF(op->func_kwdefaults);
+ Py_XDECREF(op->func_doc);
+ Py_XDECREF(op->func_dict);
+ Py_XDECREF(op->func_closure);
+ Py_XDECREF(op->func_annotations);
+ PyObject_GC_Del(op);
}
static PyObject*
func_repr(PyFunctionObject *op)
{
- return PyUnicode_FromFormat("<function %U at %p>",
- op->func_name, op);
+ return PyUnicode_FromFormat("<function %U at %p>",
+ op->func_name, op);
}
static int
func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
{
- Py_VISIT(f->func_code);
- Py_VISIT(f->func_globals);
- Py_VISIT(f->func_module);
- Py_VISIT(f->func_defaults);
- Py_VISIT(f->func_kwdefaults);
- Py_VISIT(f->func_doc);
- Py_VISIT(f->func_name);
- Py_VISIT(f->func_dict);
- Py_VISIT(f->func_closure);
- Py_VISIT(f->func_annotations);
- return 0;
+ Py_VISIT(f->func_code);
+ Py_VISIT(f->func_globals);
+ Py_VISIT(f->func_module);
+ Py_VISIT(f->func_defaults);
+ Py_VISIT(f->func_kwdefaults);
+ Py_VISIT(f->func_doc);
+ Py_VISIT(f->func_name);
+ Py_VISIT(f->func_dict);
+ Py_VISIT(f->func_closure);
+ Py_VISIT(f->func_annotations);
+ return 0;
}
static PyObject *
function_call(PyObject *func, PyObject *arg, PyObject *kw)
{
- PyObject *result;
- PyObject *argdefs;
- PyObject *kwtuple = NULL;
- PyObject **d, **k;
- Py_ssize_t nk, nd;
-
- argdefs = PyFunction_GET_DEFAULTS(func);
- if (argdefs != NULL && PyTuple_Check(argdefs)) {
- d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
- nd = PyTuple_GET_SIZE(argdefs);
- }
- else {
- d = NULL;
- nd = 0;
- }
-
- if (kw != NULL && PyDict_Check(kw)) {
- Py_ssize_t pos, i;
- nk = PyDict_Size(kw);
- kwtuple = PyTuple_New(2*nk);
- if (kwtuple == NULL)
- return NULL;
- k = &PyTuple_GET_ITEM(kwtuple, 0);
- pos = i = 0;
- while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
- Py_INCREF(k[i]);
- Py_INCREF(k[i+1]);
- i += 2;
- }
- nk = i/2;
- }
- else {
- k = NULL;
- nk = 0;
- }
-
- result = PyEval_EvalCodeEx(
- (PyCodeObject *)PyFunction_GET_CODE(func),
- PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
- &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
- k, nk, d, nd,
- PyFunction_GET_KW_DEFAULTS(func),
- PyFunction_GET_CLOSURE(func));
-
- Py_XDECREF(kwtuple);
-
- return result;
+ PyObject *result;
+ PyObject *argdefs;
+ PyObject *kwtuple = NULL;
+ PyObject **d, **k;
+ Py_ssize_t nk, nd;
+
+ argdefs = PyFunction_GET_DEFAULTS(func);
+ if (argdefs != NULL && PyTuple_Check(argdefs)) {
+ d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
+ nd = PyTuple_GET_SIZE(argdefs);
+ }
+ else {
+ d = NULL;
+ nd = 0;
+ }
+
+ if (kw != NULL && PyDict_Check(kw)) {
+ Py_ssize_t pos, i;
+ nk = PyDict_Size(kw);
+ kwtuple = PyTuple_New(2*nk);
+ if (kwtuple == NULL)
+ return NULL;
+ k = &PyTuple_GET_ITEM(kwtuple, 0);
+ pos = i = 0;
+ while (PyDict_Next(kw, &pos, &k[i], &k[i+1])) {
+ Py_INCREF(k[i]);
+ Py_INCREF(k[i+1]);
+ i += 2;
+ }
+ nk = i/2;
+ }
+ else {
+ k = NULL;
+ nk = 0;
+ }
+
+ result = PyEval_EvalCodeEx(
+ (PyCodeObject *)PyFunction_GET_CODE(func),
+ PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
+ &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg),
+ k, nk, d, nd,
+ PyFunction_GET_KW_DEFAULTS(func),
+ PyFunction_GET_CLOSURE(func));
+
+ Py_XDECREF(kwtuple);
+
+ return result;
}
/* Bind a function to an object */
static PyObject *
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
{
- if (obj == Py_None || obj == NULL) {
- Py_INCREF(func);
- return func;
- }
- return PyMethod_New(func, obj);
+ if (obj == Py_None || obj == NULL) {
+ Py_INCREF(func);
+ return func;
+ }
+ return PyMethod_New(func, obj);
}
PyTypeObject PyFunction_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "function",
- sizeof(PyFunctionObject),
- 0,
- (destructor)func_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)func_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- function_call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- func_doc, /* tp_doc */
- (traverseproc)func_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- func_memberlist, /* tp_members */
- func_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- func_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- func_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "function",
+ sizeof(PyFunctionObject),
+ 0,
+ (destructor)func_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)func_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ function_call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ func_doc, /* tp_doc */
+ (traverseproc)func_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ func_memberlist, /* tp_members */
+ func_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ func_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ func_new, /* tp_new */
};
@@ -700,9 +700,9 @@ PyTypeObject PyFunction_Type = {
To declare a class method, use this idiom:
class C:
- def f(cls, arg1, arg2, ...): ...
- f = classmethod(f)
-
+ def f(cls, arg1, arg2, ...): ...
+ f = classmethod(f)
+
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()); the instance is ignored except for its class.
If a class method is called for a derived class, the derived class
@@ -713,66 +713,66 @@ PyTypeObject PyFunction_Type = {
*/
typedef struct {
- PyObject_HEAD
- PyObject *cm_callable;
+ PyObject_HEAD
+ PyObject *cm_callable;
} classmethod;
static void
cm_dealloc(classmethod *cm)
{
- _PyObject_GC_UNTRACK((PyObject *)cm);
- Py_XDECREF(cm->cm_callable);
- Py_TYPE(cm)->tp_free((PyObject *)cm);
+ _PyObject_GC_UNTRACK((PyObject *)cm);
+ Py_XDECREF(cm->cm_callable);
+ Py_TYPE(cm)->tp_free((PyObject *)cm);
}
static int
cm_traverse(classmethod *cm, visitproc visit, void *arg)
{
- Py_VISIT(cm->cm_callable);
- return 0;
+ Py_VISIT(cm->cm_callable);
+ return 0;
}
static int
cm_clear(classmethod *cm)
{
- Py_CLEAR(cm->cm_callable);
- return 0;
+ Py_CLEAR(cm->cm_callable);
+ return 0;
}
static PyObject *
cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- classmethod *cm = (classmethod *)self;
+ classmethod *cm = (classmethod *)self;
- if (cm->cm_callable == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "uninitialized classmethod object");
- return NULL;
- }
- if (type == NULL)
- type = (PyObject *)(Py_TYPE(obj));
- return PyMethod_New(cm->cm_callable, type);
+ if (cm->cm_callable == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "uninitialized classmethod object");
+ return NULL;
+ }
+ if (type == NULL)
+ type = (PyObject *)(Py_TYPE(obj));
+ return PyMethod_New(cm->cm_callable, type);
}
static int
cm_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- classmethod *cm = (classmethod *)self;
- PyObject *callable;
+ classmethod *cm = (classmethod *)self;
+ PyObject *callable;
- if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
- return -1;
- if (!_PyArg_NoKeywords("classmethod", kwds))
- return -1;
- Py_INCREF(callable);
- cm->cm_callable = callable;
- return 0;
+ if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
+ return -1;
+ if (!_PyArg_NoKeywords("classmethod", kwds))
+ return -1;
+ Py_INCREF(callable);
+ cm->cm_callable = callable;
+ return 0;
}
static PyMemberDef cm_memberlist[] = {
- {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
- {NULL} /* Sentinel */
+ {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
+ {NULL} /* Sentinel */
};
PyDoc_STRVAR(classmethod_doc,
@@ -797,57 +797,57 @@ Class methods are different than C++ or Java static methods.\n\
If you want those, see the staticmethod builtin.");
PyTypeObject PyClassMethod_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "classmethod",
- sizeof(classmethod),
- 0,
- (destructor)cm_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- classmethod_doc, /* tp_doc */
- (traverseproc)cm_traverse, /* tp_traverse */
- (inquiry)cm_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- cm_memberlist, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- cm_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- cm_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "classmethod",
+ sizeof(classmethod),
+ 0,
+ (destructor)cm_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ classmethod_doc, /* tp_doc */
+ (traverseproc)cm_traverse, /* tp_traverse */
+ (inquiry)cm_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ cm_memberlist, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ cm_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ cm_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
PyObject *
PyClassMethod_New(PyObject *callable)
{
- classmethod *cm = (classmethod *)
- PyType_GenericAlloc(&PyClassMethod_Type, 0);
- if (cm != NULL) {
- Py_INCREF(callable);
- cm->cm_callable = callable;
- }
- return (PyObject *)cm;
+ classmethod *cm = (classmethod *)
+ PyType_GenericAlloc(&PyClassMethod_Type, 0);
+ if (cm != NULL) {
+ Py_INCREF(callable);
+ cm->cm_callable = callable;
+ }
+ return (PyObject *)cm;
}
@@ -857,8 +857,8 @@ PyClassMethod_New(PyObject *callable)
To declare a static method, use this idiom:
class C:
- def f(arg1, arg2, ...): ...
- f = staticmethod(f)
+ def f(arg1, arg2, ...): ...
+ f = staticmethod(f)
It can be called either on the class (e.g. C.f()) or on an instance
(e.g. C().f()); the instance is ignored except for its class.
@@ -868,66 +868,66 @@ PyClassMethod_New(PyObject *callable)
*/
typedef struct {
- PyObject_HEAD
- PyObject *sm_callable;
+ PyObject_HEAD
+ PyObject *sm_callable;
} staticmethod;
static void
sm_dealloc(staticmethod *sm)
{
- _PyObject_GC_UNTRACK((PyObject *)sm);
- Py_XDECREF(sm->sm_callable);
- Py_TYPE(sm)->tp_free((PyObject *)sm);
+ _PyObject_GC_UNTRACK((PyObject *)sm);
+ Py_XDECREF(sm->sm_callable);
+ Py_TYPE(sm)->tp_free((PyObject *)sm);
}
static int
sm_traverse(staticmethod *sm, visitproc visit, void *arg)
{
- Py_VISIT(sm->sm_callable);
- return 0;
+ Py_VISIT(sm->sm_callable);
+ return 0;
}
static int
sm_clear(staticmethod *sm)
{
- Py_XDECREF(sm->sm_callable);
- sm->sm_callable = NULL;
+ Py_XDECREF(sm->sm_callable);
+ sm->sm_callable = NULL;
- return 0;
+ return 0;
}
static PyObject *
sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- staticmethod *sm = (staticmethod *)self;
+ staticmethod *sm = (staticmethod *)self;
- if (sm->sm_callable == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "uninitialized staticmethod object");
- return NULL;
- }
- Py_INCREF(sm->sm_callable);
- return sm->sm_callable;
+ if (sm->sm_callable == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "uninitialized staticmethod object");
+ return NULL;
+ }
+ Py_INCREF(sm->sm_callable);
+ return sm->sm_callable;
}
static int
sm_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- staticmethod *sm = (staticmethod *)self;
- PyObject *callable;
+ staticmethod *sm = (staticmethod *)self;
+ PyObject *callable;
- if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
- return -1;
- if (!_PyArg_NoKeywords("staticmethod", kwds))
- return -1;
- Py_INCREF(callable);
- sm->sm_callable = callable;
- return 0;
+ if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
+ return -1;
+ if (!_PyArg_NoKeywords("staticmethod", kwds))
+ return -1;
+ Py_INCREF(callable);
+ sm->sm_callable = callable;
+ return 0;
}
static PyMemberDef sm_memberlist[] = {
- {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
- {NULL} /* Sentinel */
+ {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
+ {NULL} /* Sentinel */
};
PyDoc_STRVAR(staticmethod_doc,
@@ -939,8 +939,8 @@ A static method does not receive an implicit first argument.\n\
To declare a static method, use this idiom:\n\
\n\
class C:\n\
- def f(arg1, arg2, ...): ...\n\
- f = staticmethod(f)\n\
+ def f(arg1, arg2, ...): ...\n\
+ f = staticmethod(f)\n\
\n\
It can be called either on the class (e.g. C.f()) or on an instance\n\
(e.g. C().f()). The instance is ignored except for its class.\n\
@@ -949,55 +949,55 @@ Static methods in Python are similar to those found in Java or C++.\n\
For a more advanced concept, see the classmethod builtin.");
PyTypeObject PyStaticMethod_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "staticmethod",
- sizeof(staticmethod),
- 0,
- (destructor)sm_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
- staticmethod_doc, /* tp_doc */
- (traverseproc)sm_traverse, /* tp_traverse */
- (inquiry)sm_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- sm_memberlist, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- sm_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- sm_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "staticmethod",
+ sizeof(staticmethod),
+ 0,
+ (destructor)sm_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ staticmethod_doc, /* tp_doc */
+ (traverseproc)sm_traverse, /* tp_traverse */
+ (inquiry)sm_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ sm_memberlist, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ sm_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ sm_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
PyObject *
PyStaticMethod_New(PyObject *callable)
{
- staticmethod *sm = (staticmethod *)
- PyType_GenericAlloc(&PyStaticMethod_Type, 0);
- if (sm != NULL) {
- Py_INCREF(callable);
- sm->sm_callable = callable;
- }
- return (PyObject *)sm;
+ staticmethod *sm = (staticmethod *)
+ PyType_GenericAlloc(&PyStaticMethod_Type, 0);
+ if (sm != NULL) {
+ Py_INCREF(callable);
+ sm->sm_callable = callable;
+ }
+ return (PyObject *)sm;
}
diff --git a/Objects/genobject.c b/Objects/genobject.c
index fb8415f..34ee7dc 100644
--- a/Objects/genobject.c
+++ b/Objects/genobject.c
@@ -10,103 +10,103 @@
static int
gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
{
- Py_VISIT((PyObject *)gen->gi_frame);
- Py_VISIT(gen->gi_code);
- return 0;
+ Py_VISIT((PyObject *)gen->gi_frame);
+ Py_VISIT(gen->gi_code);
+ return 0;
}
static void
gen_dealloc(PyGenObject *gen)
{
- PyObject *self = (PyObject *) gen;
+ PyObject *self = (PyObject *) gen;
- _PyObject_GC_UNTRACK(gen);
+ _PyObject_GC_UNTRACK(gen);
- if (gen->gi_weakreflist != NULL)
- PyObject_ClearWeakRefs(self);
+ if (gen->gi_weakreflist != NULL)
+ PyObject_ClearWeakRefs(self);
- _PyObject_GC_TRACK(self);
+ _PyObject_GC_TRACK(self);
- if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
- /* Generator is paused, so we need to close */
- Py_TYPE(gen)->tp_del(self);
- if (self->ob_refcnt > 0)
- return; /* resurrected. :( */
- }
+ if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
+ /* Generator is paused, so we need to close */
+ Py_TYPE(gen)->tp_del(self);
+ if (self->ob_refcnt > 0)
+ return; /* resurrected. :( */
+ }
- _PyObject_GC_UNTRACK(self);
- Py_CLEAR(gen->gi_frame);
- Py_CLEAR(gen->gi_code);
- PyObject_GC_Del(gen);
+ _PyObject_GC_UNTRACK(self);
+ Py_CLEAR(gen->gi_frame);
+ Py_CLEAR(gen->gi_code);
+ PyObject_GC_Del(gen);
}
static PyObject *
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyFrameObject *f = gen->gi_frame;
- PyObject *result;
-
- if (gen->gi_running) {
- PyErr_SetString(PyExc_ValueError,
- "generator already executing");
- return NULL;
- }
- if (f==NULL || f->f_stacktop == NULL) {
- /* Only set exception if called from send() */
- if (arg && !exc)
- PyErr_SetNone(PyExc_StopIteration);
- return NULL;
- }
-
- if (f->f_lasti == -1) {
- if (arg && arg != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "can't send non-None value to a "
- "just-started generator");
- return NULL;
- }
- } else {
- /* Push arg onto the frame's value stack */
- result = arg ? arg : Py_None;
- Py_INCREF(result);
- *(f->f_stacktop++) = result;
- }
-
- /* Generators always return to their most recent caller, not
- * necessarily their creator. */
- Py_XINCREF(tstate->frame);
- assert(f->f_back == NULL);
- f->f_back = tstate->frame;
-
- gen->gi_running = 1;
- result = PyEval_EvalFrameEx(f, exc);
- gen->gi_running = 0;
-
- /* Don't keep the reference to f_back any longer than necessary. It
- * may keep a chain of frames alive or it could create a reference
- * cycle. */
- assert(f->f_back == tstate->frame);
- Py_CLEAR(f->f_back);
-
- /* If the generator just returned (as opposed to yielding), signal
- * that the generator is exhausted. */
- if (result == Py_None && f->f_stacktop == NULL) {
- Py_DECREF(result);
- result = NULL;
- /* Set exception if not called by gen_iternext() */
- if (arg)
- PyErr_SetNone(PyExc_StopIteration);
- }
-
- if (!result || f->f_stacktop == NULL) {
- /* generator can't be rerun, so release the frame */
- Py_DECREF(f);
- gen->gi_frame = NULL;
- }
-
- return result;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyFrameObject *f = gen->gi_frame;
+ PyObject *result;
+
+ if (gen->gi_running) {
+ PyErr_SetString(PyExc_ValueError,
+ "generator already executing");
+ return NULL;
+ }
+ if (f==NULL || f->f_stacktop == NULL) {
+ /* Only set exception if called from send() */
+ if (arg && !exc)
+ PyErr_SetNone(PyExc_StopIteration);
+ return NULL;
+ }
+
+ if (f->f_lasti == -1) {
+ if (arg && arg != Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't send non-None value to a "
+ "just-started generator");
+ return NULL;
+ }
+ } else {
+ /* Push arg onto the frame's value stack */
+ result = arg ? arg : Py_None;
+ Py_INCREF(result);
+ *(f->f_stacktop++) = result;
+ }
+
+ /* Generators always return to their most recent caller, not
+ * necessarily their creator. */
+ Py_XINCREF(tstate->frame);
+ assert(f->f_back == NULL);
+ f->f_back = tstate->frame;
+
+ gen->gi_running = 1;
+ result = PyEval_EvalFrameEx(f, exc);
+ gen->gi_running = 0;
+
+ /* Don't keep the reference to f_back any longer than necessary. It
+ * may keep a chain of frames alive or it could create a reference
+ * cycle. */
+ assert(f->f_back == tstate->frame);
+ Py_CLEAR(f->f_back);
+
+ /* If the generator just returned (as opposed to yielding), signal
+ * that the generator is exhausted. */
+ if (result == Py_None && f->f_stacktop == NULL) {
+ Py_DECREF(result);
+ result = NULL;
+ /* Set exception if not called by gen_iternext() */
+ if (arg)
+ PyErr_SetNone(PyExc_StopIteration);
+ }
+
+ if (!result || f->f_stacktop == NULL) {
+ /* generator can't be rerun, so release the frame */
+ Py_DECREF(f);
+ gen->gi_frame = NULL;
+ }
+
+ return result;
}
PyDoc_STRVAR(send_doc,
@@ -116,7 +116,7 @@ return next yielded value or raise StopIteration.");
static PyObject *
gen_send(PyGenObject *gen, PyObject *arg)
{
- return gen_send_ex(gen, arg, 0);
+ return gen_send_ex(gen, arg, 0);
}
PyDoc_STRVAR(close_doc,
@@ -125,83 +125,83 @@ PyDoc_STRVAR(close_doc,
static PyObject *
gen_close(PyGenObject *gen, PyObject *args)
{
- PyObject *retval;
- PyErr_SetNone(PyExc_GeneratorExit);
- retval = gen_send_ex(gen, Py_None, 1);
- if (retval) {
- Py_DECREF(retval);
- PyErr_SetString(PyExc_RuntimeError,
- "generator ignored GeneratorExit");
- return NULL;
- }
- if (PyErr_ExceptionMatches(PyExc_StopIteration)
- || PyErr_ExceptionMatches(PyExc_GeneratorExit))
- {
- PyErr_Clear(); /* ignore these errors */
- Py_INCREF(Py_None);
- return Py_None;
- }
- return NULL;
+ PyObject *retval;
+ PyErr_SetNone(PyExc_GeneratorExit);
+ retval = gen_send_ex(gen, Py_None, 1);
+ if (retval) {
+ Py_DECREF(retval);
+ PyErr_SetString(PyExc_RuntimeError,
+ "generator ignored GeneratorExit");
+ return NULL;
+ }
+ if (PyErr_ExceptionMatches(PyExc_StopIteration)
+ || PyErr_ExceptionMatches(PyExc_GeneratorExit))
+ {
+ PyErr_Clear(); /* ignore these errors */
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return NULL;
}
static void
gen_del(PyObject *self)
{
- PyObject *res;
- PyObject *error_type, *error_value, *error_traceback;
- PyGenObject *gen = (PyGenObject *)self;
-
- if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
- /* Generator isn't paused, so no need to close */
- return;
-
- /* Temporarily resurrect the object. */
- assert(self->ob_refcnt == 0);
- self->ob_refcnt = 1;
-
- /* Save the current exception, if any. */
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
-
- res = gen_close(gen, NULL);
-
- if (res == NULL)
- PyErr_WriteUnraisable(self);
- else
- Py_DECREF(res);
-
- /* Restore the saved exception. */
- PyErr_Restore(error_type, error_value, error_traceback);
-
- /* Undo the temporary resurrection; can't use DECREF here, it would
- * cause a recursive call.
- */
- assert(self->ob_refcnt > 0);
- if (--self->ob_refcnt == 0)
- return; /* this is the normal path out */
-
- /* close() resurrected it! Make it look like the original Py_DECREF
- * never happened.
- */
- {
- Py_ssize_t refcnt = self->ob_refcnt;
- _Py_NewReference(self);
- self->ob_refcnt = refcnt;
- }
- assert(PyType_IS_GC(self->ob_type) &&
- _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
-
- /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
- * we need to undo that. */
- _Py_DEC_REFTOTAL;
- /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
- * chain, so no more to do there.
- * If COUNT_ALLOCS, the original decref bumped tp_frees, and
- * _Py_NewReference bumped tp_allocs: both of those need to be
- * undone.
- */
+ PyObject *res;
+ PyObject *error_type, *error_value, *error_traceback;
+ PyGenObject *gen = (PyGenObject *)self;
+
+ if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
+ /* Generator isn't paused, so no need to close */
+ return;
+
+ /* Temporarily resurrect the object. */
+ assert(self->ob_refcnt == 0);
+ self->ob_refcnt = 1;
+
+ /* Save the current exception, if any. */
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+ res = gen_close(gen, NULL);
+
+ if (res == NULL)
+ PyErr_WriteUnraisable(self);
+ else
+ Py_DECREF(res);
+
+ /* Restore the saved exception. */
+ PyErr_Restore(error_type, error_value, error_traceback);
+
+ /* Undo the temporary resurrection; can't use DECREF here, it would
+ * cause a recursive call.
+ */
+ assert(self->ob_refcnt > 0);
+ if (--self->ob_refcnt == 0)
+ return; /* this is the normal path out */
+
+ /* close() resurrected it! Make it look like the original Py_DECREF
+ * never happened.
+ */
+ {
+ Py_ssize_t refcnt = self->ob_refcnt;
+ _Py_NewReference(self);
+ self->ob_refcnt = refcnt;
+ }
+ assert(PyType_IS_GC(self->ob_type) &&
+ _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
+
+ /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
+ * we need to undo that. */
+ _Py_DEC_REFTOTAL;
+ /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
+ * chain, so no more to do there.
+ * If COUNT_ALLOCS, the original decref bumped tp_frees, and
+ * _Py_NewReference bumped tp_allocs: both of those need to be
+ * undone.
+ */
#ifdef COUNT_ALLOCS
- --self->ob_type->tp_frees;
- --self->ob_type->tp_allocs;
+ --self->ob_type->tp_frees;
+ --self->ob_type->tp_allocs;
#endif
}
@@ -214,89 +214,89 @@ return next yielded value or raise StopIteration.");
static PyObject *
gen_throw(PyGenObject *gen, PyObject *args)
{
- PyObject *typ;
- PyObject *tb = NULL;
- PyObject *val = NULL;
-
- if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
- return NULL;
-
- /* First, check the traceback argument, replacing None with
- NULL. */
- if (tb == Py_None)
- tb = NULL;
- else if (tb != NULL && !PyTraceBack_Check(tb)) {
- PyErr_SetString(PyExc_TypeError,
- "throw() third argument must be a traceback object");
- return NULL;
- }
-
- Py_INCREF(typ);
- Py_XINCREF(val);
- Py_XINCREF(tb);
-
- if (PyExceptionClass_Check(typ)) {
- PyErr_NormalizeException(&typ, &val, &tb);
- }
-
- else if (PyExceptionInstance_Check(typ)) {
- /* Raising an instance. The value should be a dummy. */
- if (val && val != Py_None) {
- PyErr_SetString(PyExc_TypeError,
- "instance exception may not have a separate value");
- goto failed_throw;
- }
- else {
- /* Normalize to raise <class>, <instance> */
- Py_XDECREF(val);
- val = typ;
- typ = PyExceptionInstance_Class(typ);
- Py_INCREF(typ);
- }
- }
- else {
- /* Not something you can raise. throw() fails. */
- PyErr_Format(PyExc_TypeError,
- "exceptions must be classes or instances "
- "deriving from BaseException, not %s",
- typ->ob_type->tp_name);
- goto failed_throw;
- }
-
- PyErr_Restore(typ, val, tb);
- return gen_send_ex(gen, Py_None, 1);
+ PyObject *typ;
+ PyObject *tb = NULL;
+ PyObject *val = NULL;
+
+ if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb))
+ return NULL;
+
+ /* First, check the traceback argument, replacing None with
+ NULL. */
+ if (tb == Py_None)
+ tb = NULL;
+ else if (tb != NULL && !PyTraceBack_Check(tb)) {
+ PyErr_SetString(PyExc_TypeError,
+ "throw() third argument must be a traceback object");
+ return NULL;
+ }
+
+ Py_INCREF(typ);
+ Py_XINCREF(val);
+ Py_XINCREF(tb);
+
+ if (PyExceptionClass_Check(typ)) {
+ PyErr_NormalizeException(&typ, &val, &tb);
+ }
+
+ else if (PyExceptionInstance_Check(typ)) {
+ /* Raising an instance. The value should be a dummy. */
+ if (val && val != Py_None) {
+ PyErr_SetString(PyExc_TypeError,
+ "instance exception may not have a separate value");
+ goto failed_throw;
+ }
+ else {
+ /* Normalize to raise <class>, <instance> */
+ Py_XDECREF(val);
+ val = typ;
+ typ = PyExceptionInstance_Class(typ);
+ Py_INCREF(typ);
+ }
+ }
+ else {
+ /* Not something you can raise. throw() fails. */
+ PyErr_Format(PyExc_TypeError,
+ "exceptions must be classes or instances "
+ "deriving from BaseException, not %s",
+ typ->ob_type->tp_name);
+ goto failed_throw;
+ }
+
+ PyErr_Restore(typ, val, tb);
+ return gen_send_ex(gen, Py_None, 1);
failed_throw:
- /* Didn't use our arguments, so restore their original refcounts */
- Py_DECREF(typ);
- Py_XDECREF(val);
- Py_XDECREF(tb);
- return NULL;
+ /* Didn't use our arguments, so restore their original refcounts */
+ Py_DECREF(typ);
+ Py_XDECREF(val);
+ Py_XDECREF(tb);
+ return NULL;
}
static PyObject *
gen_iternext(PyGenObject *gen)
{
- return gen_send_ex(gen, NULL, 0);
+ return gen_send_ex(gen, NULL, 0);
}
static PyObject *
gen_repr(PyGenObject *gen)
{
- return PyUnicode_FromFormat("<generator object %S at %p>",
- ((PyCodeObject *)gen->gi_code)->co_name,
- gen);
+ return PyUnicode_FromFormat("<generator object %S at %p>",
+ ((PyCodeObject *)gen->gi_code)->co_name,
+ gen);
}
static PyObject *
gen_get_name(PyGenObject *gen)
{
- PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
- Py_INCREF(name);
- return name;
+ PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
+ Py_INCREF(name);
+ return name;
}
@@ -304,109 +304,109 @@ PyDoc_STRVAR(gen__name__doc__,
"Return the name of the generator's associated code object.");
static PyGetSetDef gen_getsetlist[] = {
- {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
- {NULL}
+ {"__name__", (getter)gen_get_name, NULL, gen__name__doc__},
+ {NULL}
};
static PyMemberDef gen_memberlist[] = {
- {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
- {"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY},
- {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
- {NULL} /* Sentinel */
+ {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), READONLY},
+ {"gi_running", T_INT, offsetof(PyGenObject, gi_running), READONLY},
+ {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), READONLY},
+ {NULL} /* Sentinel */
};
static PyMethodDef gen_methods[] = {
- {"send",(PyCFunction)gen_send, METH_O, send_doc},
- {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
- {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
- {NULL, NULL} /* Sentinel */
+ {"send",(PyCFunction)gen_send, METH_O, send_doc},
+ {"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
+ {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
+ {NULL, NULL} /* Sentinel */
};
PyTypeObject PyGen_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "generator", /* tp_name */
- sizeof(PyGenObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)gen_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)gen_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)gen_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)gen_iternext, /* tp_iternext */
- gen_methods, /* tp_methods */
- gen_memberlist, /* tp_members */
- gen_getsetlist, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
-
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
- 0, /* tp_free */
- 0, /* tp_is_gc */
- 0, /* tp_bases */
- 0, /* tp_mro */
- 0, /* tp_cache */
- 0, /* tp_subclasses */
- 0, /* tp_weaklist */
- gen_del, /* tp_del */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "generator", /* tp_name */
+ sizeof(PyGenObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)gen_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)gen_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)gen_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyGenObject, gi_weakreflist), /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)gen_iternext, /* tp_iternext */
+ gen_methods, /* tp_methods */
+ gen_memberlist, /* tp_members */
+ gen_getsetlist, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
+ 0, /* tp_is_gc */
+ 0, /* tp_bases */
+ 0, /* tp_mro */
+ 0, /* tp_cache */
+ 0, /* tp_subclasses */
+ 0, /* tp_weaklist */
+ gen_del, /* tp_del */
};
PyObject *
PyGen_New(PyFrameObject *f)
{
- PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
- if (gen == NULL) {
- Py_DECREF(f);
- return NULL;
- }
- gen->gi_frame = f;
- Py_INCREF(f->f_code);
- gen->gi_code = (PyObject *)(f->f_code);
- gen->gi_running = 0;
- gen->gi_weakreflist = NULL;
- _PyObject_GC_TRACK(gen);
- return (PyObject *)gen;
+ PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type);
+ if (gen == NULL) {
+ Py_DECREF(f);
+ return NULL;
+ }
+ gen->gi_frame = f;
+ Py_INCREF(f->f_code);
+ gen->gi_code = (PyObject *)(f->f_code);
+ gen->gi_running = 0;
+ gen->gi_weakreflist = NULL;
+ _PyObject_GC_TRACK(gen);
+ return (PyObject *)gen;
}
int
PyGen_NeedsFinalizing(PyGenObject *gen)
{
- int i;
- PyFrameObject *f = gen->gi_frame;
+ int i;
+ PyFrameObject *f = gen->gi_frame;
- if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
- return 0; /* no frame or empty blockstack == no finalization */
+ if (f == NULL || f->f_stacktop == NULL || f->f_iblock <= 0)
+ return 0; /* no frame or empty blockstack == no finalization */
- /* Any block type besides a loop requires cleanup. */
- i = f->f_iblock;
- while (--i >= 0) {
- if (f->f_blockstack[i].b_type != SETUP_LOOP)
- return 1;
- }
+ /* Any block type besides a loop requires cleanup. */
+ i = f->f_iblock;
+ while (--i >= 0) {
+ if (f->f_blockstack[i].b_type != SETUP_LOOP)
+ return 1;
+ }
- /* No blocks except loops, it's safe to skip finalization. */
- return 0;
+ /* No blocks except loops, it's safe to skip finalization. */
+ return 0;
}
diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index 174bd98..b534b4a 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -3,230 +3,230 @@
#include "Python.h"
typedef struct {
- PyObject_HEAD
- long it_index;
- PyObject *it_seq; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ long it_index;
+ PyObject *it_seq; /* Set to NULL when iterator is exhausted */
} seqiterobject;
PyObject *
PySeqIter_New(PyObject *seq)
{
- seqiterobject *it;
-
- if (!PySequence_Check(seq)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
- if (it == NULL)
- return NULL;
- it->it_index = 0;
- Py_INCREF(seq);
- it->it_seq = seq;
- _PyObject_GC_TRACK(it);
- return (PyObject *)it;
+ seqiterobject *it;
+
+ if (!PySequence_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
}
static void
iter_dealloc(seqiterobject *it)
{
- _PyObject_GC_UNTRACK(it);
- Py_XDECREF(it->it_seq);
- PyObject_GC_Del(it);
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
}
static int
iter_traverse(seqiterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_seq);
- return 0;
+ Py_VISIT(it->it_seq);
+ return 0;
}
static PyObject *
iter_iternext(PyObject *iterator)
{
- seqiterobject *it;
- PyObject *seq;
- PyObject *result;
-
- assert(PySeqIter_Check(iterator));
- it = (seqiterobject *)iterator;
- seq = it->it_seq;
- if (seq == NULL)
- return NULL;
-
- result = PySequence_GetItem(seq, it->it_index);
- if (result != NULL) {
- it->it_index++;
- return result;
- }
- if (PyErr_ExceptionMatches(PyExc_IndexError) ||
- PyErr_ExceptionMatches(PyExc_StopIteration))
- {
- PyErr_Clear();
- Py_DECREF(seq);
- it->it_seq = NULL;
- }
- return NULL;
+ seqiterobject *it;
+ PyObject *seq;
+ PyObject *result;
+
+ assert(PySeqIter_Check(iterator));
+ it = (seqiterobject *)iterator;
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+
+ result = PySequence_GetItem(seq, it->it_index);
+ if (result != NULL) {
+ it->it_index++;
+ return result;
+ }
+ if (PyErr_ExceptionMatches(PyExc_IndexError) ||
+ PyErr_ExceptionMatches(PyExc_StopIteration))
+ {
+ PyErr_Clear();
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ }
+ return NULL;
}
static PyObject *
iter_len(seqiterobject *it)
{
- Py_ssize_t seqsize, len;
-
- if (it->it_seq) {
- seqsize = PySequence_Size(it->it_seq);
- if (seqsize == -1)
- return NULL;
- len = seqsize - it->it_index;
- if (len >= 0)
- return PyLong_FromSsize_t(len);
- }
- return PyLong_FromLong(0);
+ Py_ssize_t seqsize, len;
+
+ if (it->it_seq) {
+ seqsize = PySequence_Size(it->it_seq);
+ if (seqsize == -1)
+ return NULL;
+ len = seqsize - it->it_index;
+ if (len >= 0)
+ return PyLong_FromSsize_t(len);
+ }
+ return PyLong_FromLong(0);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef seqiter_methods[] = {
- {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PySeqIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "iterator", /* tp_name */
- sizeof(seqiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)iter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)iter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- iter_iternext, /* tp_iternext */
- seqiter_methods, /* tp_methods */
- 0, /* tp_members */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "iterator", /* tp_name */
+ sizeof(seqiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)iter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)iter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ iter_iternext, /* tp_iternext */
+ seqiter_methods, /* tp_methods */
+ 0, /* tp_members */
};
/* -------------------------------------- */
typedef struct {
- PyObject_HEAD
- PyObject *it_callable; /* Set to NULL when iterator is exhausted */
- PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ PyObject *it_callable; /* Set to NULL when iterator is exhausted */
+ PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
} calliterobject;
PyObject *
PyCallIter_New(PyObject *callable, PyObject *sentinel)
{
- calliterobject *it;
- it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
- if (it == NULL)
- return NULL;
- Py_INCREF(callable);
- it->it_callable = callable;
- Py_INCREF(sentinel);
- it->it_sentinel = sentinel;
- _PyObject_GC_TRACK(it);
- return (PyObject *)it;
+ calliterobject *it;
+ it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
+ if (it == NULL)
+ return NULL;
+ Py_INCREF(callable);
+ it->it_callable = callable;
+ Py_INCREF(sentinel);
+ it->it_sentinel = sentinel;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
}
static void
calliter_dealloc(calliterobject *it)
{
- _PyObject_GC_UNTRACK(it);
- Py_XDECREF(it->it_callable);
- Py_XDECREF(it->it_sentinel);
- PyObject_GC_Del(it);
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_callable);
+ Py_XDECREF(it->it_sentinel);
+ PyObject_GC_Del(it);
}
static int
calliter_traverse(calliterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_callable);
- Py_VISIT(it->it_sentinel);
- return 0;
+ Py_VISIT(it->it_callable);
+ Py_VISIT(it->it_sentinel);
+ return 0;
}
static PyObject *
calliter_iternext(calliterobject *it)
{
- if (it->it_callable != NULL) {
- PyObject *args = PyTuple_New(0);
- PyObject *result;
- if (args == NULL)
- return NULL;
- result = PyObject_Call(it->it_callable, args, NULL);
- Py_DECREF(args);
- if (result != NULL) {
- int ok;
- ok = PyObject_RichCompareBool(result,
- it->it_sentinel,
- Py_EQ);
- if (ok == 0)
- return result; /* Common case, fast path */
- Py_DECREF(result);
- if (ok > 0) {
- Py_CLEAR(it->it_callable);
- Py_CLEAR(it->it_sentinel);
- }
- }
- else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
- PyErr_Clear();
- Py_CLEAR(it->it_callable);
- Py_CLEAR(it->it_sentinel);
- }
- }
- return NULL;
+ if (it->it_callable != NULL) {
+ PyObject *args = PyTuple_New(0);
+ PyObject *result;
+ if (args == NULL)
+ return NULL;
+ result = PyObject_Call(it->it_callable, args, NULL);
+ Py_DECREF(args);
+ if (result != NULL) {
+ int ok;
+ ok = PyObject_RichCompareBool(result,
+ it->it_sentinel,
+ Py_EQ);
+ if (ok == 0)
+ return result; /* Common case, fast path */
+ Py_DECREF(result);
+ if (ok > 0) {
+ Py_CLEAR(it->it_callable);
+ Py_CLEAR(it->it_sentinel);
+ }
+ }
+ else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
+ PyErr_Clear();
+ Py_CLEAR(it->it_callable);
+ Py_CLEAR(it->it_sentinel);
+ }
+ }
+ return NULL;
}
PyTypeObject PyCallIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "callable_iterator", /* tp_name */
- sizeof(calliterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)calliter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)calliter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)calliter_iternext, /* tp_iternext */
- 0, /* tp_methods */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "callable_iterator", /* tp_name */
+ sizeof(calliterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)calliter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)calliter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)calliter_iternext, /* tp_iternext */
+ 0, /* tp_methods */
};
diff --git a/Objects/listobject.c b/Objects/listobject.c
index d3c997a..e8b21f1 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -5,7 +5,7 @@
#ifdef STDC_HEADERS
#include <stddef.h>
#else
-#include <sys/types.h> /* For size_t */
+#include <sys/types.h> /* For size_t */
#endif
/* Ensure ob_item has room for at least newsize elements, and set
@@ -24,52 +24,52 @@
static int
list_resize(PyListObject *self, Py_ssize_t newsize)
{
- PyObject **items;
- size_t new_allocated;
- Py_ssize_t allocated = self->allocated;
-
- /* Bypass realloc() when a previous overallocation is large enough
- to accommodate the newsize. If the newsize falls lower than half
- the allocated size, then proceed with the realloc() to shrink the list.
- */
- if (allocated >= newsize && newsize >= (allocated >> 1)) {
- assert(self->ob_item != NULL || newsize == 0);
- Py_SIZE(self) = newsize;
- return 0;
- }
-
- /* This over-allocates proportional to the list size, making room
- * for additional growth. The over-allocation is mild, but is
- * enough to give linear-time amortized behavior over a long
- * sequence of appends() in the presence of a poorly-performing
- * system realloc().
- * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
- */
- new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
-
- /* check for integer overflow */
- if (new_allocated > PY_SIZE_MAX - newsize) {
- PyErr_NoMemory();
- return -1;
- } else {
- new_allocated += newsize;
- }
-
- if (newsize == 0)
- new_allocated = 0;
- items = self->ob_item;
- if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
- PyMem_RESIZE(items, PyObject *, new_allocated);
- else
- items = NULL;
- if (items == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- self->ob_item = items;
- Py_SIZE(self) = newsize;
- self->allocated = new_allocated;
- return 0;
+ PyObject **items;
+ size_t new_allocated;
+ Py_ssize_t allocated = self->allocated;
+
+ /* Bypass realloc() when a previous overallocation is large enough
+ to accommodate the newsize. If the newsize falls lower than half
+ the allocated size, then proceed with the realloc() to shrink the list.
+ */
+ if (allocated >= newsize && newsize >= (allocated >> 1)) {
+ assert(self->ob_item != NULL || newsize == 0);
+ Py_SIZE(self) = newsize;
+ return 0;
+ }
+
+ /* This over-allocates proportional to the list size, making room
+ * for additional growth. The over-allocation is mild, but is
+ * enough to give linear-time amortized behavior over a long
+ * sequence of appends() in the presence of a poorly-performing
+ * system realloc().
+ * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
+ */
+ new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
+
+ /* check for integer overflow */
+ if (new_allocated > PY_SIZE_MAX - newsize) {
+ PyErr_NoMemory();
+ return -1;
+ } else {
+ new_allocated += newsize;
+ }
+
+ if (newsize == 0)
+ new_allocated = 0;
+ items = self->ob_item;
+ if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
+ PyMem_RESIZE(items, PyObject *, new_allocated);
+ else
+ items = NULL;
+ if (items == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ self->ob_item = items;
+ Py_SIZE(self) = newsize;
+ self->allocated = new_allocated;
+ return 0;
}
/* Debug statistic to compare allocations with reuse through the free list */
@@ -81,12 +81,12 @@ static size_t count_reuse = 0;
static void
show_alloc(void)
{
- fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
- count_alloc);
- fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
- "d\n", count_reuse);
- fprintf(stderr, "%.2f%% reuse rate\n\n",
- (100.0*count_reuse/(count_alloc+count_reuse)));
+ fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
+ count_alloc);
+ fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
+ "d\n", count_reuse);
+ fprintf(stderr, "%.2f%% reuse rate\n\n",
+ (100.0*count_reuse/(count_alloc+count_reuse)));
}
#endif
@@ -100,77 +100,77 @@ static int numfree = 0;
void
PyList_Fini(void)
{
- PyListObject *op;
+ PyListObject *op;
- while (numfree) {
- op = free_list[--numfree];
- assert(PyList_CheckExact(op));
- PyObject_GC_Del(op);
- }
+ while (numfree) {
+ op = free_list[--numfree];
+ assert(PyList_CheckExact(op));
+ PyObject_GC_Del(op);
+ }
}
PyObject *
PyList_New(Py_ssize_t size)
{
- PyListObject *op;
- size_t nbytes;
+ PyListObject *op;
+ size_t nbytes;
#ifdef SHOW_ALLOC_COUNT
- static int initialized = 0;
- if (!initialized) {
- Py_AtExit(show_alloc);
- initialized = 1;
- }
+ static int initialized = 0;
+ if (!initialized) {
+ Py_AtExit(show_alloc);
+ initialized = 1;
+ }
#endif
- if (size < 0) {
- PyErr_BadInternalCall();
- return NULL;
- }
- /* Check for overflow without an actual overflow,
- * which can cause compiler to optimise out */
- if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
- return PyErr_NoMemory();
- nbytes = size * sizeof(PyObject *);
- if (numfree) {
- numfree--;
- op = free_list[numfree];
- _Py_NewReference((PyObject *)op);
+ if (size < 0) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ /* Check for overflow without an actual overflow,
+ * which can cause compiler to optimise out */
+ if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
+ return PyErr_NoMemory();
+ nbytes = size * sizeof(PyObject *);
+ if (numfree) {
+ numfree--;
+ op = free_list[numfree];
+ _Py_NewReference((PyObject *)op);
#ifdef SHOW_ALLOC_COUNT
- count_reuse++;
+ count_reuse++;
#endif
- } else {
- op = PyObject_GC_New(PyListObject, &PyList_Type);
- if (op == NULL)
- return NULL;
+ } else {
+ op = PyObject_GC_New(PyListObject, &PyList_Type);
+ if (op == NULL)
+ return NULL;
#ifdef SHOW_ALLOC_COUNT
- count_alloc++;
+ count_alloc++;
#endif
- }
- if (size <= 0)
- op->ob_item = NULL;
- else {
- op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
- if (op->ob_item == NULL) {
- Py_DECREF(op);
- return PyErr_NoMemory();
- }
- memset(op->ob_item, 0, nbytes);
- }
- Py_SIZE(op) = size;
- op->allocated = size;
- _PyObject_GC_TRACK(op);
- return (PyObject *) op;
+ }
+ if (size <= 0)
+ op->ob_item = NULL;
+ else {
+ op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
+ if (op->ob_item == NULL) {
+ Py_DECREF(op);
+ return PyErr_NoMemory();
+ }
+ memset(op->ob_item, 0, nbytes);
+ }
+ Py_SIZE(op) = size;
+ op->allocated = size;
+ _PyObject_GC_TRACK(op);
+ return (PyObject *) op;
}
Py_ssize_t
PyList_Size(PyObject *op)
{
- if (!PyList_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- else
- return Py_SIZE(op);
+ if (!PyList_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ else
+ return Py_SIZE(op);
}
static PyObject *indexerr = NULL;
@@ -178,117 +178,117 @@ static PyObject *indexerr = NULL;
PyObject *
PyList_GetItem(PyObject *op, Py_ssize_t i)
{
- if (!PyList_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (i < 0 || i >= Py_SIZE(op)) {
- if (indexerr == NULL) {
- indexerr = PyUnicode_FromString(
- "list index out of range");
- if (indexerr == NULL)
- return NULL;
- }
- PyErr_SetObject(PyExc_IndexError, indexerr);
- return NULL;
- }
- return ((PyListObject *)op) -> ob_item[i];
+ if (!PyList_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (i < 0 || i >= Py_SIZE(op)) {
+ if (indexerr == NULL) {
+ indexerr = PyUnicode_FromString(
+ "list index out of range");
+ if (indexerr == NULL)
+ return NULL;
+ }
+ PyErr_SetObject(PyExc_IndexError, indexerr);
+ return NULL;
+ }
+ return ((PyListObject *)op) -> ob_item[i];
}
int
PyList_SetItem(register PyObject *op, register Py_ssize_t i,
register PyObject *newitem)
{
- register PyObject *olditem;
- register PyObject **p;
- if (!PyList_Check(op)) {
- Py_XDECREF(newitem);
- PyErr_BadInternalCall();
- return -1;
- }
- if (i < 0 || i >= Py_SIZE(op)) {
- Py_XDECREF(newitem);
- PyErr_SetString(PyExc_IndexError,
- "list assignment index out of range");
- return -1;
- }
- p = ((PyListObject *)op) -> ob_item + i;
- olditem = *p;
- *p = newitem;
- Py_XDECREF(olditem);
- return 0;
+ register PyObject *olditem;
+ register PyObject **p;
+ if (!PyList_Check(op)) {
+ Py_XDECREF(newitem);
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (i < 0 || i >= Py_SIZE(op)) {
+ Py_XDECREF(newitem);
+ PyErr_SetString(PyExc_IndexError,
+ "list assignment index out of range");
+ return -1;
+ }
+ p = ((PyListObject *)op) -> ob_item + i;
+ olditem = *p;
+ *p = newitem;
+ Py_XDECREF(olditem);
+ return 0;
}
static int
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
{
- Py_ssize_t i, n = Py_SIZE(self);
- PyObject **items;
- if (v == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (n == PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "cannot add more objects to list");
- return -1;
- }
-
- if (list_resize(self, n+1) == -1)
- return -1;
-
- if (where < 0) {
- where += n;
- if (where < 0)
- where = 0;
- }
- if (where > n)
- where = n;
- items = self->ob_item;
- for (i = n; --i >= where; )
- items[i+1] = items[i];
- Py_INCREF(v);
- items[where] = v;
- return 0;
+ Py_ssize_t i, n = Py_SIZE(self);
+ PyObject **items;
+ if (v == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (n == PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "cannot add more objects to list");
+ return -1;
+ }
+
+ if (list_resize(self, n+1) == -1)
+ return -1;
+
+ if (where < 0) {
+ where += n;
+ if (where < 0)
+ where = 0;
+ }
+ if (where > n)
+ where = n;
+ items = self->ob_item;
+ for (i = n; --i >= where; )
+ items[i+1] = items[i];
+ Py_INCREF(v);
+ items[where] = v;
+ return 0;
}
int
PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
{
- if (!PyList_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return ins1((PyListObject *)op, where, newitem);
+ if (!PyList_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return ins1((PyListObject *)op, where, newitem);
}
static int
app1(PyListObject *self, PyObject *v)
{
- Py_ssize_t n = PyList_GET_SIZE(self);
+ Py_ssize_t n = PyList_GET_SIZE(self);
- assert (v != NULL);
- if (n == PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "cannot add more objects to list");
- return -1;
- }
+ assert (v != NULL);
+ if (n == PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "cannot add more objects to list");
+ return -1;
+ }
- if (list_resize(self, n+1) == -1)
- return -1;
+ if (list_resize(self, n+1) == -1)
+ return -1;
- Py_INCREF(v);
- PyList_SET_ITEM(self, n, v);
- return 0;
+ Py_INCREF(v);
+ PyList_SET_ITEM(self, n, v);
+ return 0;
}
int
PyList_Append(PyObject *op, PyObject *newitem)
{
- if (PyList_Check(op) && (newitem != NULL))
- return app1((PyListObject *)op, newitem);
- PyErr_BadInternalCall();
- return -1;
+ if (PyList_Check(op) && (newitem != NULL))
+ return app1((PyListObject *)op, newitem);
+ PyErr_BadInternalCall();
+ return -1;
}
/* Methods */
@@ -296,271 +296,271 @@ PyList_Append(PyObject *op, PyObject *newitem)
static void
list_dealloc(PyListObject *op)
{
- Py_ssize_t i;
- PyObject_GC_UnTrack(op);
- Py_TRASHCAN_SAFE_BEGIN(op)
- if (op->ob_item != NULL) {
- /* Do it backwards, for Christian Tismer.
- There's a simple test case where somehow this reduces
- thrashing when a *very* large list is created and
- immediately deleted. */
- i = Py_SIZE(op);
- while (--i >= 0) {
- Py_XDECREF(op->ob_item[i]);
- }
- PyMem_FREE(op->ob_item);
- }
- if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
- free_list[numfree++] = op;
- else
- Py_TYPE(op)->tp_free((PyObject *)op);
- Py_TRASHCAN_SAFE_END(op)
+ Py_ssize_t i;
+ PyObject_GC_UnTrack(op);
+ Py_TRASHCAN_SAFE_BEGIN(op)
+ if (op->ob_item != NULL) {
+ /* Do it backwards, for Christian Tismer.
+ There's a simple test case where somehow this reduces
+ thrashing when a *very* large list is created and
+ immediately deleted. */
+ i = Py_SIZE(op);
+ while (--i >= 0) {
+ Py_XDECREF(op->ob_item[i]);
+ }
+ PyMem_FREE(op->ob_item);
+ }
+ if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
+ free_list[numfree++] = op;
+ else
+ Py_TYPE(op)->tp_free((PyObject *)op);
+ Py_TRASHCAN_SAFE_END(op)
}
static PyObject *
list_repr(PyListObject *v)
{
- Py_ssize_t i;
- PyObject *s, *temp;
- PyObject *pieces = NULL, *result = NULL;
-
- i = Py_ReprEnter((PyObject*)v);
- if (i != 0) {
- return i > 0 ? PyUnicode_FromString("[...]") : NULL;
- }
-
- if (Py_SIZE(v) == 0) {
- result = PyUnicode_FromString("[]");
- goto Done;
- }
-
- pieces = PyList_New(0);
- if (pieces == NULL)
- goto Done;
-
- /* Do repr() on each element. Note that this may mutate the list,
- so must refetch the list size on each iteration. */
- for (i = 0; i < Py_SIZE(v); ++i) {
- int status;
- if (Py_EnterRecursiveCall(" while getting the repr of a list"))
- goto Done;
- s = PyObject_Repr(v->ob_item[i]);
- Py_LeaveRecursiveCall();
- if (s == NULL)
- goto Done;
- status = PyList_Append(pieces, s);
- Py_DECREF(s); /* append created a new ref */
- if (status < 0)
- goto Done;
- }
-
- /* Add "[]" decorations to the first and last items. */
- assert(PyList_GET_SIZE(pieces) > 0);
- s = PyUnicode_FromString("[");
- if (s == NULL)
- goto Done;
- temp = PyList_GET_ITEM(pieces, 0);
- PyUnicode_AppendAndDel(&s, temp);
- PyList_SET_ITEM(pieces, 0, s);
- if (s == NULL)
- goto Done;
-
- s = PyUnicode_FromString("]");
- if (s == NULL)
- goto Done;
- temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
- PyUnicode_AppendAndDel(&temp, s);
- PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
- if (temp == NULL)
- goto Done;
-
- /* Paste them all together with ", " between. */
- s = PyUnicode_FromString(", ");
- if (s == NULL)
- goto Done;
- result = PyUnicode_Join(s, pieces);
- Py_DECREF(s);
+ Py_ssize_t i;
+ PyObject *s, *temp;
+ PyObject *pieces = NULL, *result = NULL;
+
+ i = Py_ReprEnter((PyObject*)v);
+ if (i != 0) {
+ return i > 0 ? PyUnicode_FromString("[...]") : NULL;
+ }
+
+ if (Py_SIZE(v) == 0) {
+ result = PyUnicode_FromString("[]");
+ goto Done;
+ }
+
+ pieces = PyList_New(0);
+ if (pieces == NULL)
+ goto Done;
+
+ /* Do repr() on each element. Note that this may mutate the list,
+ so must refetch the list size on each iteration. */
+ for (i = 0; i < Py_SIZE(v); ++i) {
+ int status;
+ if (Py_EnterRecursiveCall(" while getting the repr of a list"))
+ goto Done;
+ s = PyObject_Repr(v->ob_item[i]);
+ Py_LeaveRecursiveCall();
+ if (s == NULL)
+ goto Done;
+ status = PyList_Append(pieces, s);
+ Py_DECREF(s); /* append created a new ref */
+ if (status < 0)
+ goto Done;
+ }
+
+ /* Add "[]" decorations to the first and last items. */
+ assert(PyList_GET_SIZE(pieces) > 0);
+ s = PyUnicode_FromString("[");
+ if (s == NULL)
+ goto Done;
+ temp = PyList_GET_ITEM(pieces, 0);
+ PyUnicode_AppendAndDel(&s, temp);
+ PyList_SET_ITEM(pieces, 0, s);
+ if (s == NULL)
+ goto Done;
+
+ s = PyUnicode_FromString("]");
+ if (s == NULL)
+ goto Done;
+ temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
+ PyUnicode_AppendAndDel(&temp, s);
+ PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
+ if (temp == NULL)
+ goto Done;
+
+ /* Paste them all together with ", " between. */
+ s = PyUnicode_FromString(", ");
+ if (s == NULL)
+ goto Done;
+ result = PyUnicode_Join(s, pieces);
+ Py_DECREF(s);
Done:
- Py_XDECREF(pieces);
- Py_ReprLeave((PyObject *)v);
- return result;
+ Py_XDECREF(pieces);
+ Py_ReprLeave((PyObject *)v);
+ return result;
}
static Py_ssize_t
list_length(PyListObject *a)
{
- return Py_SIZE(a);
+ return Py_SIZE(a);
}
static int
list_contains(PyListObject *a, PyObject *el)
{
- Py_ssize_t i;
- int cmp;
+ Py_ssize_t i;
+ int cmp;
- for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
- cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
- Py_EQ);
- return cmp;
+ for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
+ cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
+ Py_EQ);
+ return cmp;
}
static PyObject *
list_item(PyListObject *a, Py_ssize_t i)
{
- if (i < 0 || i >= Py_SIZE(a)) {
- if (indexerr == NULL) {
- indexerr = PyUnicode_FromString(
- "list index out of range");
- if (indexerr == NULL)
- return NULL;
- }
- PyErr_SetObject(PyExc_IndexError, indexerr);
- return NULL;
- }
- Py_INCREF(a->ob_item[i]);
- return a->ob_item[i];
+ if (i < 0 || i >= Py_SIZE(a)) {
+ if (indexerr == NULL) {
+ indexerr = PyUnicode_FromString(
+ "list index out of range");
+ if (indexerr == NULL)
+ return NULL;
+ }
+ PyErr_SetObject(PyExc_IndexError, indexerr);
+ return NULL;
+ }
+ Py_INCREF(a->ob_item[i]);
+ return a->ob_item[i];
}
static PyObject *
list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
{
- PyListObject *np;
- PyObject **src, **dest;
- Py_ssize_t i, len;
- if (ilow < 0)
- ilow = 0;
- else if (ilow > Py_SIZE(a))
- ilow = Py_SIZE(a);
- if (ihigh < ilow)
- ihigh = ilow;
- else if (ihigh > Py_SIZE(a))
- ihigh = Py_SIZE(a);
- len = ihigh - ilow;
- np = (PyListObject *) PyList_New(len);
- if (np == NULL)
- return NULL;
-
- src = a->ob_item + ilow;
- dest = np->ob_item;
- for (i = 0; i < len; i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- return (PyObject *)np;
+ PyListObject *np;
+ PyObject **src, **dest;
+ Py_ssize_t i, len;
+ if (ilow < 0)
+ ilow = 0;
+ else if (ilow > Py_SIZE(a))
+ ilow = Py_SIZE(a);
+ if (ihigh < ilow)
+ ihigh = ilow;
+ else if (ihigh > Py_SIZE(a))
+ ihigh = Py_SIZE(a);
+ len = ihigh - ilow;
+ np = (PyListObject *) PyList_New(len);
+ if (np == NULL)
+ return NULL;
+
+ src = a->ob_item + ilow;
+ dest = np->ob_item;
+ for (i = 0; i < len; i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ return (PyObject *)np;
}
PyObject *
PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
{
- if (!PyList_Check(a)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return list_slice((PyListObject *)a, ilow, ihigh);
+ if (!PyList_Check(a)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return list_slice((PyListObject *)a, ilow, ihigh);
}
static PyObject *
list_concat(PyListObject *a, PyObject *bb)
{
- Py_ssize_t size;
- Py_ssize_t i;
- PyObject **src, **dest;
- PyListObject *np;
- if (!PyList_Check(bb)) {
- PyErr_Format(PyExc_TypeError,
- "can only concatenate list (not \"%.200s\") to list",
- bb->ob_type->tp_name);
- return NULL;
- }
+ Py_ssize_t size;
+ Py_ssize_t i;
+ PyObject **src, **dest;
+ PyListObject *np;
+ if (!PyList_Check(bb)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only concatenate list (not \"%.200s\") to list",
+ bb->ob_type->tp_name);
+ return NULL;
+ }
#define b ((PyListObject *)bb)
- size = Py_SIZE(a) + Py_SIZE(b);
- if (size < 0)
- return PyErr_NoMemory();
- np = (PyListObject *) PyList_New(size);
- if (np == NULL) {
- return NULL;
- }
- src = a->ob_item;
- dest = np->ob_item;
- for (i = 0; i < Py_SIZE(a); i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- src = b->ob_item;
- dest = np->ob_item + Py_SIZE(a);
- for (i = 0; i < Py_SIZE(b); i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- return (PyObject *)np;
+ size = Py_SIZE(a) + Py_SIZE(b);
+ if (size < 0)
+ return PyErr_NoMemory();
+ np = (PyListObject *) PyList_New(size);
+ if (np == NULL) {
+ return NULL;
+ }
+ src = a->ob_item;
+ dest = np->ob_item;
+ for (i = 0; i < Py_SIZE(a); i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ src = b->ob_item;
+ dest = np->ob_item + Py_SIZE(a);
+ for (i = 0; i < Py_SIZE(b); i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ return (PyObject *)np;
#undef b
}
static PyObject *
list_repeat(PyListObject *a, Py_ssize_t n)
{
- Py_ssize_t i, j;
- Py_ssize_t size;
- PyListObject *np;
- PyObject **p, **items;
- PyObject *elem;
- if (n < 0)
- n = 0;
- size = Py_SIZE(a) * n;
- if (n && size/n != Py_SIZE(a))
- return PyErr_NoMemory();
- if (size == 0)
- return PyList_New(0);
- np = (PyListObject *) PyList_New(size);
- if (np == NULL)
- return NULL;
-
- items = np->ob_item;
- if (Py_SIZE(a) == 1) {
- elem = a->ob_item[0];
- for (i = 0; i < n; i++) {
- items[i] = elem;
- Py_INCREF(elem);
- }
- return (PyObject *) np;
- }
- p = np->ob_item;
- items = a->ob_item;
- for (i = 0; i < n; i++) {
- for (j = 0; j < Py_SIZE(a); j++) {
- *p = items[j];
- Py_INCREF(*p);
- p++;
- }
- }
- return (PyObject *) np;
+ Py_ssize_t i, j;
+ Py_ssize_t size;
+ PyListObject *np;
+ PyObject **p, **items;
+ PyObject *elem;
+ if (n < 0)
+ n = 0;
+ size = Py_SIZE(a) * n;
+ if (n && size/n != Py_SIZE(a))
+ return PyErr_NoMemory();
+ if (size == 0)
+ return PyList_New(0);
+ np = (PyListObject *) PyList_New(size);
+ if (np == NULL)
+ return NULL;
+
+ items = np->ob_item;
+ if (Py_SIZE(a) == 1) {
+ elem = a->ob_item[0];
+ for (i = 0; i < n; i++) {
+ items[i] = elem;
+ Py_INCREF(elem);
+ }
+ return (PyObject *) np;
+ }
+ p = np->ob_item;
+ items = a->ob_item;
+ for (i = 0; i < n; i++) {
+ for (j = 0; j < Py_SIZE(a); j++) {
+ *p = items[j];
+ Py_INCREF(*p);
+ p++;
+ }
+ }
+ return (PyObject *) np;
}
static int
list_clear(PyListObject *a)
{
- Py_ssize_t i;
- PyObject **item = a->ob_item;
- if (item != NULL) {
- /* Because XDECREF can recursively invoke operations on
- this list, we make it empty first. */
- i = Py_SIZE(a);
- Py_SIZE(a) = 0;
- a->ob_item = NULL;
- a->allocated = 0;
- while (--i >= 0) {
- Py_XDECREF(item[i]);
- }
- PyMem_FREE(item);
- }
- /* Never fails; the return value can be ignored.
- Note that there is no guarantee that the list is actually empty
- at this point, because XDECREF may have populated it again! */
- return 0;
+ Py_ssize_t i;
+ PyObject **item = a->ob_item;
+ if (item != NULL) {
+ /* Because XDECREF can recursively invoke operations on
+ this list, we make it empty first. */
+ i = Py_SIZE(a);
+ Py_SIZE(a) = 0;
+ a->ob_item = NULL;
+ a->allocated = 0;
+ while (--i >= 0) {
+ Py_XDECREF(item[i]);
+ }
+ PyMem_FREE(item);
+ }
+ /* Never fails; the return value can be ignored.
+ Note that there is no guarantee that the list is actually empty
+ at this point, because XDECREF may have populated it again! */
+ return 0;
}
/* a[ilow:ihigh] = v if v != NULL.
@@ -572,368 +572,368 @@ list_clear(PyListObject *a)
static int
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
- /* Because [X]DECREF can recursively invoke list operations on
- this list, we must postpone all [X]DECREF activity until
- after the list is back in its canonical shape. Therefore
- we must allocate an additional array, 'recycle', into which
- we temporarily copy the items that are deleted from the
- list. :-( */
- PyObject *recycle_on_stack[8];
- PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
- PyObject **item;
- PyObject **vitem = NULL;
- PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
- Py_ssize_t n; /* # of elements in replacement list */
- Py_ssize_t norig; /* # of elements in list getting replaced */
- Py_ssize_t d; /* Change in size */
- Py_ssize_t k;
- size_t s;
- int result = -1; /* guilty until proved innocent */
+ /* Because [X]DECREF can recursively invoke list operations on
+ this list, we must postpone all [X]DECREF activity until
+ after the list is back in its canonical shape. Therefore
+ we must allocate an additional array, 'recycle', into which
+ we temporarily copy the items that are deleted from the
+ list. :-( */
+ PyObject *recycle_on_stack[8];
+ PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
+ PyObject **item;
+ PyObject **vitem = NULL;
+ PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
+ Py_ssize_t n; /* # of elements in replacement list */
+ Py_ssize_t norig; /* # of elements in list getting replaced */
+ Py_ssize_t d; /* Change in size */
+ Py_ssize_t k;
+ size_t s;
+ int result = -1; /* guilty until proved innocent */
#define b ((PyListObject *)v)
- if (v == NULL)
- n = 0;
- else {
- if (a == b) {
- /* Special case "a[i:j] = a" -- copy b first */
- v = list_slice(b, 0, Py_SIZE(b));
- if (v == NULL)
- return result;
- result = list_ass_slice(a, ilow, ihigh, v);
- Py_DECREF(v);
- return result;
- }
- v_as_SF = PySequence_Fast(v, "can only assign an iterable");
- if(v_as_SF == NULL)
- goto Error;
- n = PySequence_Fast_GET_SIZE(v_as_SF);
- vitem = PySequence_Fast_ITEMS(v_as_SF);
- }
- if (ilow < 0)
- ilow = 0;
- else if (ilow > Py_SIZE(a))
- ilow = Py_SIZE(a);
-
- if (ihigh < ilow)
- ihigh = ilow;
- else if (ihigh > Py_SIZE(a))
- ihigh = Py_SIZE(a);
-
- norig = ihigh - ilow;
- assert(norig >= 0);
- d = n - norig;
- if (Py_SIZE(a) + d == 0) {
- Py_XDECREF(v_as_SF);
- return list_clear(a);
- }
- item = a->ob_item;
- /* recycle the items that we are about to remove */
- s = norig * sizeof(PyObject *);
- if (s > sizeof(recycle_on_stack)) {
- recycle = (PyObject **)PyMem_MALLOC(s);
- if (recycle == NULL) {
- PyErr_NoMemory();
- goto Error;
- }
- }
- memcpy(recycle, &item[ilow], s);
-
- if (d < 0) { /* Delete -d items */
- memmove(&item[ihigh+d], &item[ihigh],
- (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
- list_resize(a, Py_SIZE(a) + d);
- item = a->ob_item;
- }
- else if (d > 0) { /* Insert d items */
- k = Py_SIZE(a);
- if (list_resize(a, k+d) < 0)
- goto Error;
- item = a->ob_item;
- memmove(&item[ihigh+d], &item[ihigh],
- (k - ihigh)*sizeof(PyObject *));
- }
- for (k = 0; k < n; k++, ilow++) {
- PyObject *w = vitem[k];
- Py_XINCREF(w);
- item[ilow] = w;
- }
- for (k = norig - 1; k >= 0; --k)
- Py_XDECREF(recycle[k]);
- result = 0;
+ if (v == NULL)
+ n = 0;
+ else {
+ if (a == b) {
+ /* Special case "a[i:j] = a" -- copy b first */
+ v = list_slice(b, 0, Py_SIZE(b));
+ if (v == NULL)
+ return result;
+ result = list_ass_slice(a, ilow, ihigh, v);
+ Py_DECREF(v);
+ return result;
+ }
+ v_as_SF = PySequence_Fast(v, "can only assign an iterable");
+ if(v_as_SF == NULL)
+ goto Error;
+ n = PySequence_Fast_GET_SIZE(v_as_SF);
+ vitem = PySequence_Fast_ITEMS(v_as_SF);
+ }
+ if (ilow < 0)
+ ilow = 0;
+ else if (ilow > Py_SIZE(a))
+ ilow = Py_SIZE(a);
+
+ if (ihigh < ilow)
+ ihigh = ilow;
+ else if (ihigh > Py_SIZE(a))
+ ihigh = Py_SIZE(a);
+
+ norig = ihigh - ilow;
+ assert(norig >= 0);
+ d = n - norig;
+ if (Py_SIZE(a) + d == 0) {
+ Py_XDECREF(v_as_SF);
+ return list_clear(a);
+ }
+ item = a->ob_item;
+ /* recycle the items that we are about to remove */
+ s = norig * sizeof(PyObject *);
+ if (s > sizeof(recycle_on_stack)) {
+ recycle = (PyObject **)PyMem_MALLOC(s);
+ if (recycle == NULL) {
+ PyErr_NoMemory();
+ goto Error;
+ }
+ }
+ memcpy(recycle, &item[ilow], s);
+
+ if (d < 0) { /* Delete -d items */
+ memmove(&item[ihigh+d], &item[ihigh],
+ (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
+ list_resize(a, Py_SIZE(a) + d);
+ item = a->ob_item;
+ }
+ else if (d > 0) { /* Insert d items */
+ k = Py_SIZE(a);
+ if (list_resize(a, k+d) < 0)
+ goto Error;
+ item = a->ob_item;
+ memmove(&item[ihigh+d], &item[ihigh],
+ (k - ihigh)*sizeof(PyObject *));
+ }
+ for (k = 0; k < n; k++, ilow++) {
+ PyObject *w = vitem[k];
+ Py_XINCREF(w);
+ item[ilow] = w;
+ }
+ for (k = norig - 1; k >= 0; --k)
+ Py_XDECREF(recycle[k]);
+ result = 0;
Error:
- if (recycle != recycle_on_stack)
- PyMem_FREE(recycle);
- Py_XDECREF(v_as_SF);
- return result;
+ if (recycle != recycle_on_stack)
+ PyMem_FREE(recycle);
+ Py_XDECREF(v_as_SF);
+ return result;
#undef b
}
int
PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
- if (!PyList_Check(a)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
+ if (!PyList_Check(a)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
}
static PyObject *
list_inplace_repeat(PyListObject *self, Py_ssize_t n)
{
- PyObject **items;
- Py_ssize_t size, i, j, p;
+ PyObject **items;
+ Py_ssize_t size, i, j, p;
- size = PyList_GET_SIZE(self);
- if (size == 0 || n == 1) {
- Py_INCREF(self);
- return (PyObject *)self;
- }
+ size = PyList_GET_SIZE(self);
+ if (size == 0 || n == 1) {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
- if (n < 1) {
- (void)list_clear(self);
- Py_INCREF(self);
- return (PyObject *)self;
- }
+ if (n < 1) {
+ (void)list_clear(self);
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
- if (size > PY_SSIZE_T_MAX / n) {
- return PyErr_NoMemory();
- }
+ if (size > PY_SSIZE_T_MAX / n) {
+ return PyErr_NoMemory();
+ }
- if (list_resize(self, size*n) == -1)
- return NULL;
+ if (list_resize(self, size*n) == -1)
+ return NULL;
- p = size;
- items = self->ob_item;
- for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
- for (j = 0; j < size; j++) {
- PyObject *o = items[j];
- Py_INCREF(o);
- items[p++] = o;
- }
- }
- Py_INCREF(self);
- return (PyObject *)self;
+ p = size;
+ items = self->ob_item;
+ for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
+ for (j = 0; j < size; j++) {
+ PyObject *o = items[j];
+ Py_INCREF(o);
+ items[p++] = o;
+ }
+ }
+ Py_INCREF(self);
+ return (PyObject *)self;
}
static int
list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
{
- PyObject *old_value;
- if (i < 0 || i >= Py_SIZE(a)) {
- PyErr_SetString(PyExc_IndexError,
- "list assignment index out of range");
- return -1;
- }
- if (v == NULL)
- return list_ass_slice(a, i, i+1, v);
- Py_INCREF(v);
- old_value = a->ob_item[i];
- a->ob_item[i] = v;
- Py_DECREF(old_value);
- return 0;
+ PyObject *old_value;
+ if (i < 0 || i >= Py_SIZE(a)) {
+ PyErr_SetString(PyExc_IndexError,
+ "list assignment index out of range");
+ return -1;
+ }
+ if (v == NULL)
+ return list_ass_slice(a, i, i+1, v);
+ Py_INCREF(v);
+ old_value = a->ob_item[i];
+ a->ob_item[i] = v;
+ Py_DECREF(old_value);
+ return 0;
}
static PyObject *
listinsert(PyListObject *self, PyObject *args)
{
- Py_ssize_t i;
- PyObject *v;
- if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
- return NULL;
- if (ins1(self, i, v) == 0)
- Py_RETURN_NONE;
- return NULL;
+ Py_ssize_t i;
+ PyObject *v;
+ if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
+ return NULL;
+ if (ins1(self, i, v) == 0)
+ Py_RETURN_NONE;
+ return NULL;
}
static PyObject *
listappend(PyListObject *self, PyObject *v)
{
- if (app1(self, v) == 0)
- Py_RETURN_NONE;
- return NULL;
+ if (app1(self, v) == 0)
+ Py_RETURN_NONE;
+ return NULL;
}
static PyObject *
listextend(PyListObject *self, PyObject *b)
{
- PyObject *it; /* iter(v) */
- Py_ssize_t m; /* size of self */
- Py_ssize_t n; /* guess for size of b */
- Py_ssize_t mn; /* m + n */
- Py_ssize_t i;
- PyObject *(*iternext)(PyObject *);
-
- /* Special cases:
- 1) lists and tuples which can use PySequence_Fast ops
- 2) extending self to self requires making a copy first
- */
- if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
- PyObject **src, **dest;
- b = PySequence_Fast(b, "argument must be iterable");
- if (!b)
- return NULL;
- n = PySequence_Fast_GET_SIZE(b);
- if (n == 0) {
- /* short circuit when b is empty */
- Py_DECREF(b);
- Py_RETURN_NONE;
- }
- m = Py_SIZE(self);
- if (list_resize(self, m + n) == -1) {
- Py_DECREF(b);
- return NULL;
- }
- /* note that we may still have self == b here for the
- * situation a.extend(a), but the following code works
- * in that case too. Just make sure to resize self
- * before calling PySequence_Fast_ITEMS.
- */
- /* populate the end of self with b's items */
- src = PySequence_Fast_ITEMS(b);
- dest = self->ob_item + m;
- for (i = 0; i < n; i++) {
- PyObject *o = src[i];
- Py_INCREF(o);
- dest[i] = o;
- }
- Py_DECREF(b);
- Py_RETURN_NONE;
- }
-
- it = PyObject_GetIter(b);
- if (it == NULL)
- return NULL;
- iternext = *it->ob_type->tp_iternext;
-
- /* Guess a result list size. */
- n = _PyObject_LengthHint(b, 8);
- if (n == -1) {
- Py_DECREF(it);
- return NULL;
- }
- m = Py_SIZE(self);
- mn = m + n;
- if (mn >= m) {
- /* Make room. */
- if (list_resize(self, mn) == -1)
- goto error;
- /* Make the list sane again. */
- Py_SIZE(self) = m;
- }
- /* Else m + n overflowed; on the chance that n lied, and there really
- * is enough room, ignore it. If n was telling the truth, we'll
- * eventually run out of memory during the loop.
- */
-
- /* Run iterator to exhaustion. */
- for (;;) {
- PyObject *item = iternext(it);
- if (item == NULL) {
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- goto error;
- }
- break;
- }
- if (Py_SIZE(self) < self->allocated) {
- /* steals ref */
- PyList_SET_ITEM(self, Py_SIZE(self), item);
- ++Py_SIZE(self);
- }
- else {
- int status = app1(self, item);
- Py_DECREF(item); /* append creates a new ref */
- if (status < 0)
- goto error;
- }
- }
-
- /* Cut back result list if initial guess was too large. */
- if (Py_SIZE(self) < self->allocated)
- list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
-
- Py_DECREF(it);
- Py_RETURN_NONE;
+ PyObject *it; /* iter(v) */
+ Py_ssize_t m; /* size of self */
+ Py_ssize_t n; /* guess for size of b */
+ Py_ssize_t mn; /* m + n */
+ Py_ssize_t i;
+ PyObject *(*iternext)(PyObject *);
+
+ /* Special cases:
+ 1) lists and tuples which can use PySequence_Fast ops
+ 2) extending self to self requires making a copy first
+ */
+ if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
+ PyObject **src, **dest;
+ b = PySequence_Fast(b, "argument must be iterable");
+ if (!b)
+ return NULL;
+ n = PySequence_Fast_GET_SIZE(b);
+ if (n == 0) {
+ /* short circuit when b is empty */
+ Py_DECREF(b);
+ Py_RETURN_NONE;
+ }
+ m = Py_SIZE(self);
+ if (list_resize(self, m + n) == -1) {
+ Py_DECREF(b);
+ return NULL;
+ }
+ /* note that we may still have self == b here for the
+ * situation a.extend(a), but the following code works
+ * in that case too. Just make sure to resize self
+ * before calling PySequence_Fast_ITEMS.
+ */
+ /* populate the end of self with b's items */
+ src = PySequence_Fast_ITEMS(b);
+ dest = self->ob_item + m;
+ for (i = 0; i < n; i++) {
+ PyObject *o = src[i];
+ Py_INCREF(o);
+ dest[i] = o;
+ }
+ Py_DECREF(b);
+ Py_RETURN_NONE;
+ }
+
+ it = PyObject_GetIter(b);
+ if (it == NULL)
+ return NULL;
+ iternext = *it->ob_type->tp_iternext;
+
+ /* Guess a result list size. */
+ n = _PyObject_LengthHint(b, 8);
+ if (n == -1) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ m = Py_SIZE(self);
+ mn = m + n;
+ if (mn >= m) {
+ /* Make room. */
+ if (list_resize(self, mn) == -1)
+ goto error;
+ /* Make the list sane again. */
+ Py_SIZE(self) = m;
+ }
+ /* Else m + n overflowed; on the chance that n lied, and there really
+ * is enough room, ignore it. If n was telling the truth, we'll
+ * eventually run out of memory during the loop.
+ */
+
+ /* Run iterator to exhaustion. */
+ for (;;) {
+ PyObject *item = iternext(it);
+ if (item == NULL) {
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else
+ goto error;
+ }
+ break;
+ }
+ if (Py_SIZE(self) < self->allocated) {
+ /* steals ref */
+ PyList_SET_ITEM(self, Py_SIZE(self), item);
+ ++Py_SIZE(self);
+ }
+ else {
+ int status = app1(self, item);
+ Py_DECREF(item); /* append creates a new ref */
+ if (status < 0)
+ goto error;
+ }
+ }
+
+ /* Cut back result list if initial guess was too large. */
+ if (Py_SIZE(self) < self->allocated)
+ list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
+
+ Py_DECREF(it);
+ Py_RETURN_NONE;
error:
- Py_DECREF(it);
- return NULL;
+ Py_DECREF(it);
+ return NULL;
}
PyObject *
_PyList_Extend(PyListObject *self, PyObject *b)
{
- return listextend(self, b);
+ return listextend(self, b);
}
static PyObject *
list_inplace_concat(PyListObject *self, PyObject *other)
{
- PyObject *result;
+ PyObject *result;
- result = listextend(self, other);
- if (result == NULL)
- return result;
- Py_DECREF(result);
- Py_INCREF(self);
- return (PyObject *)self;
+ result = listextend(self, other);
+ if (result == NULL)
+ return result;
+ Py_DECREF(result);
+ Py_INCREF(self);
+ return (PyObject *)self;
}
static PyObject *
listpop(PyListObject *self, PyObject *args)
{
- Py_ssize_t i = -1;
- PyObject *v;
- int status;
-
- if (!PyArg_ParseTuple(args, "|n:pop", &i))
- return NULL;
-
- if (Py_SIZE(self) == 0) {
- /* Special-case most common failure cause */
- PyErr_SetString(PyExc_IndexError, "pop from empty list");
- return NULL;
- }
- if (i < 0)
- i += Py_SIZE(self);
- if (i < 0 || i >= Py_SIZE(self)) {
- PyErr_SetString(PyExc_IndexError, "pop index out of range");
- return NULL;
- }
- v = self->ob_item[i];
- if (i == Py_SIZE(self) - 1) {
- status = list_resize(self, Py_SIZE(self) - 1);
- assert(status >= 0);
- return v; /* and v now owns the reference the list had */
- }
- Py_INCREF(v);
- status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
- assert(status >= 0);
- /* Use status, so that in a release build compilers don't
- * complain about the unused name.
- */
- (void) status;
-
- return v;
+ Py_ssize_t i = -1;
+ PyObject *v;
+ int status;
+
+ if (!PyArg_ParseTuple(args, "|n:pop", &i))
+ return NULL;
+
+ if (Py_SIZE(self) == 0) {
+ /* Special-case most common failure cause */
+ PyErr_SetString(PyExc_IndexError, "pop from empty list");
+ return NULL;
+ }
+ if (i < 0)
+ i += Py_SIZE(self);
+ if (i < 0 || i >= Py_SIZE(self)) {
+ PyErr_SetString(PyExc_IndexError, "pop index out of range");
+ return NULL;
+ }
+ v = self->ob_item[i];
+ if (i == Py_SIZE(self) - 1) {
+ status = list_resize(self, Py_SIZE(self) - 1);
+ assert(status >= 0);
+ return v; /* and v now owns the reference the list had */
+ }
+ Py_INCREF(v);
+ status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
+ assert(status >= 0);
+ /* Use status, so that in a release build compilers don't
+ * complain about the unused name.
+ */
+ (void) status;
+
+ return v;
}
/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
static void
reverse_slice(PyObject **lo, PyObject **hi)
{
- assert(lo && hi);
+ assert(lo && hi);
- --hi;
- while (lo < hi) {
- PyObject *t = *lo;
- *lo = *hi;
- *hi = t;
- ++lo;
- --hi;
- }
+ --hi;
+ while (lo < hi) {
+ PyObject *t = *lo;
+ *lo = *hi;
+ *hi = t;
+ ++lo;
+ --hi;
+ }
}
/* Lots of code for an adaptive, stable, natural mergesort. There are many
@@ -951,7 +951,7 @@ reverse_slice(PyObject **lo, PyObject **hi)
started. It makes more sense in context <wink>. X and Y are PyObject*s.
*/
#define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
- if (k)
+ if (k)
/* binarysort is the best method for sorting small arrays: it does
few compares, but can do data movement quadratic in the number of
@@ -967,48 +967,48 @@ reverse_slice(PyObject **lo, PyObject **hi)
static int
binarysort(PyObject **lo, PyObject **hi, PyObject **start)
{
- register Py_ssize_t k;
- register PyObject **l, **p, **r;
- register PyObject *pivot;
-
- assert(lo <= start && start <= hi);
- /* assert [lo, start) is sorted */
- if (lo == start)
- ++start;
- for (; start < hi; ++start) {
- /* set l to where *start belongs */
- l = lo;
- r = start;
- pivot = *r;
- /* Invariants:
- * pivot >= all in [lo, l).
- * pivot < all in [r, start).
- * The second is vacuously true at the start.
- */
- assert(l < r);
- do {
- p = l + ((r - l) >> 1);
- IFLT(pivot, *p)
- r = p;
- else
- l = p+1;
- } while (l < r);
- assert(l == r);
- /* The invariants still hold, so pivot >= all in [lo, l) and
- pivot < all in [l, start), so pivot belongs at l. Note
- that if there are elements equal to pivot, l points to the
- first slot after them -- that's why this sort is stable.
- Slide over to make room.
- Caution: using memmove is much slower under MSVC 5;
- we're not usually moving many slots. */
- for (p = start; p > l; --p)
- *p = *(p-1);
- *l = pivot;
- }
- return 0;
+ register Py_ssize_t k;
+ register PyObject **l, **p, **r;
+ register PyObject *pivot;
+
+ assert(lo <= start && start <= hi);
+ /* assert [lo, start) is sorted */
+ if (lo == start)
+ ++start;
+ for (; start < hi; ++start) {
+ /* set l to where *start belongs */
+ l = lo;
+ r = start;
+ pivot = *r;
+ /* Invariants:
+ * pivot >= all in [lo, l).
+ * pivot < all in [r, start).
+ * The second is vacuously true at the start.
+ */
+ assert(l < r);
+ do {
+ p = l + ((r - l) >> 1);
+ IFLT(pivot, *p)
+ r = p;
+ else
+ l = p+1;
+ } while (l < r);
+ assert(l == r);
+ /* The invariants still hold, so pivot >= all in [lo, l) and
+ pivot < all in [l, start), so pivot belongs at l. Note
+ that if there are elements equal to pivot, l points to the
+ first slot after them -- that's why this sort is stable.
+ Slide over to make room.
+ Caution: using memmove is much slower under MSVC 5;
+ we're not usually moving many slots. */
+ for (p = start; p > l; --p)
+ *p = *(p-1);
+ *l = pivot;
+ }
+ return 0;
fail:
- return -1;
+ return -1;
}
/*
@@ -1032,35 +1032,35 @@ Returns -1 in case of error.
static Py_ssize_t
count_run(PyObject **lo, PyObject **hi, int *descending)
{
- Py_ssize_t k;
- Py_ssize_t n;
-
- assert(lo < hi);
- *descending = 0;
- ++lo;
- if (lo == hi)
- return 1;
-
- n = 2;
- IFLT(*lo, *(lo-1)) {
- *descending = 1;
- for (lo = lo+1; lo < hi; ++lo, ++n) {
- IFLT(*lo, *(lo-1))
- ;
- else
- break;
- }
- }
- else {
- for (lo = lo+1; lo < hi; ++lo, ++n) {
- IFLT(*lo, *(lo-1))
- break;
- }
- }
-
- return n;
+ Py_ssize_t k;
+ Py_ssize_t n;
+
+ assert(lo < hi);
+ *descending = 0;
+ ++lo;
+ if (lo == hi)
+ return 1;
+
+ n = 2;
+ IFLT(*lo, *(lo-1)) {
+ *descending = 1;
+ for (lo = lo+1; lo < hi; ++lo, ++n) {
+ IFLT(*lo, *(lo-1))
+ ;
+ else
+ break;
+ }
+ }
+ else {
+ for (lo = lo+1; lo < hi; ++lo, ++n) {
+ IFLT(*lo, *(lo-1))
+ break;
+ }
+ }
+
+ return n;
fail:
- return -1;
+ return -1;
}
/*
@@ -1087,78 +1087,78 @@ Returns -1 on error. See listsort.txt for info on the method.
static Py_ssize_t
gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
{
- Py_ssize_t ofs;
- Py_ssize_t lastofs;
- Py_ssize_t k;
-
- assert(key && a && n > 0 && hint >= 0 && hint < n);
-
- a += hint;
- lastofs = 0;
- ofs = 1;
- IFLT(*a, key) {
- /* a[hint] < key -- gallop right, until
- * a[hint + lastofs] < key <= a[hint + ofs]
- */
- const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
- while (ofs < maxofs) {
- IFLT(a[ofs], key) {
- lastofs = ofs;
- ofs = (ofs << 1) + 1;
- if (ofs <= 0) /* int overflow */
- ofs = maxofs;
- }
- else /* key <= a[hint + ofs] */
- break;
- }
- if (ofs > maxofs)
- ofs = maxofs;
- /* Translate back to offsets relative to &a[0]. */
- lastofs += hint;
- ofs += hint;
- }
- else {
- /* key <= a[hint] -- gallop left, until
- * a[hint - ofs] < key <= a[hint - lastofs]
- */
- const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
- while (ofs < maxofs) {
- IFLT(*(a-ofs), key)
- break;
- /* key <= a[hint - ofs] */
- lastofs = ofs;
- ofs = (ofs << 1) + 1;
- if (ofs <= 0) /* int overflow */
- ofs = maxofs;
- }
- if (ofs > maxofs)
- ofs = maxofs;
- /* Translate back to positive offsets relative to &a[0]. */
- k = lastofs;
- lastofs = hint - ofs;
- ofs = hint - k;
- }
- a -= hint;
-
- assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
- /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
- * right of lastofs but no farther right than ofs. Do a binary
- * search, with invariant a[lastofs-1] < key <= a[ofs].
- */
- ++lastofs;
- while (lastofs < ofs) {
- Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
-
- IFLT(a[m], key)
- lastofs = m+1; /* a[m] < key */
- else
- ofs = m; /* key <= a[m] */
- }
- assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
- return ofs;
+ Py_ssize_t ofs;
+ Py_ssize_t lastofs;
+ Py_ssize_t k;
+
+ assert(key && a && n > 0 && hint >= 0 && hint < n);
+
+ a += hint;
+ lastofs = 0;
+ ofs = 1;
+ IFLT(*a, key) {
+ /* a[hint] < key -- gallop right, until
+ * a[hint + lastofs] < key <= a[hint + ofs]
+ */
+ const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
+ while (ofs < maxofs) {
+ IFLT(a[ofs], key) {
+ lastofs = ofs;
+ ofs = (ofs << 1) + 1;
+ if (ofs <= 0) /* int overflow */
+ ofs = maxofs;
+ }
+ else /* key <= a[hint + ofs] */
+ break;
+ }
+ if (ofs > maxofs)
+ ofs = maxofs;
+ /* Translate back to offsets relative to &a[0]. */
+ lastofs += hint;
+ ofs += hint;
+ }
+ else {
+ /* key <= a[hint] -- gallop left, until
+ * a[hint - ofs] < key <= a[hint - lastofs]
+ */
+ const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
+ while (ofs < maxofs) {
+ IFLT(*(a-ofs), key)
+ break;
+ /* key <= a[hint - ofs] */
+ lastofs = ofs;
+ ofs = (ofs << 1) + 1;
+ if (ofs <= 0) /* int overflow */
+ ofs = maxofs;
+ }
+ if (ofs > maxofs)
+ ofs = maxofs;
+ /* Translate back to positive offsets relative to &a[0]. */
+ k = lastofs;
+ lastofs = hint - ofs;
+ ofs = hint - k;
+ }
+ a -= hint;
+
+ assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
+ /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
+ * right of lastofs but no farther right than ofs. Do a binary
+ * search, with invariant a[lastofs-1] < key <= a[ofs].
+ */
+ ++lastofs;
+ while (lastofs < ofs) {
+ Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
+
+ IFLT(a[m], key)
+ lastofs = m+1; /* a[m] < key */
+ else
+ ofs = m; /* key <= a[m] */
+ }
+ assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
+ return ofs;
fail:
- return -1;
+ return -1;
}
/*
@@ -1178,78 +1178,78 @@ written as one routine with yet another "left or right?" flag.
static Py_ssize_t
gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
{
- Py_ssize_t ofs;
- Py_ssize_t lastofs;
- Py_ssize_t k;
-
- assert(key && a && n > 0 && hint >= 0 && hint < n);
-
- a += hint;
- lastofs = 0;
- ofs = 1;
- IFLT(key, *a) {
- /* key < a[hint] -- gallop left, until
- * a[hint - ofs] <= key < a[hint - lastofs]
- */
- const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
- while (ofs < maxofs) {
- IFLT(key, *(a-ofs)) {
- lastofs = ofs;
- ofs = (ofs << 1) + 1;
- if (ofs <= 0) /* int overflow */
- ofs = maxofs;
- }
- else /* a[hint - ofs] <= key */
- break;
- }
- if (ofs > maxofs)
- ofs = maxofs;
- /* Translate back to positive offsets relative to &a[0]. */
- k = lastofs;
- lastofs = hint - ofs;
- ofs = hint - k;
- }
- else {
- /* a[hint] <= key -- gallop right, until
- * a[hint + lastofs] <= key < a[hint + ofs]
- */
- const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
- while (ofs < maxofs) {
- IFLT(key, a[ofs])
- break;
- /* a[hint + ofs] <= key */
- lastofs = ofs;
- ofs = (ofs << 1) + 1;
- if (ofs <= 0) /* int overflow */
- ofs = maxofs;
- }
- if (ofs > maxofs)
- ofs = maxofs;
- /* Translate back to offsets relative to &a[0]. */
- lastofs += hint;
- ofs += hint;
- }
- a -= hint;
-
- assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
- /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
- * right of lastofs but no farther right than ofs. Do a binary
- * search, with invariant a[lastofs-1] <= key < a[ofs].
- */
- ++lastofs;
- while (lastofs < ofs) {
- Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
-
- IFLT(key, a[m])
- ofs = m; /* key < a[m] */
- else
- lastofs = m+1; /* a[m] <= key */
- }
- assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
- return ofs;
+ Py_ssize_t ofs;
+ Py_ssize_t lastofs;
+ Py_ssize_t k;
+
+ assert(key && a && n > 0 && hint >= 0 && hint < n);
+
+ a += hint;
+ lastofs = 0;
+ ofs = 1;
+ IFLT(key, *a) {
+ /* key < a[hint] -- gallop left, until
+ * a[hint - ofs] <= key < a[hint - lastofs]
+ */
+ const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
+ while (ofs < maxofs) {
+ IFLT(key, *(a-ofs)) {
+ lastofs = ofs;
+ ofs = (ofs << 1) + 1;
+ if (ofs <= 0) /* int overflow */
+ ofs = maxofs;
+ }
+ else /* a[hint - ofs] <= key */
+ break;
+ }
+ if (ofs > maxofs)
+ ofs = maxofs;
+ /* Translate back to positive offsets relative to &a[0]. */
+ k = lastofs;
+ lastofs = hint - ofs;
+ ofs = hint - k;
+ }
+ else {
+ /* a[hint] <= key -- gallop right, until
+ * a[hint + lastofs] <= key < a[hint + ofs]
+ */
+ const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
+ while (ofs < maxofs) {
+ IFLT(key, a[ofs])
+ break;
+ /* a[hint + ofs] <= key */
+ lastofs = ofs;
+ ofs = (ofs << 1) + 1;
+ if (ofs <= 0) /* int overflow */
+ ofs = maxofs;
+ }
+ if (ofs > maxofs)
+ ofs = maxofs;
+ /* Translate back to offsets relative to &a[0]. */
+ lastofs += hint;
+ ofs += hint;
+ }
+ a -= hint;
+
+ assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
+ /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
+ * right of lastofs but no farther right than ofs. Do a binary
+ * search, with invariant a[lastofs-1] <= key < a[ofs].
+ */
+ ++lastofs;
+ while (lastofs < ofs) {
+ Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
+
+ IFLT(key, a[m])
+ ofs = m; /* key < a[m] */
+ else
+ lastofs = m+1; /* a[m] <= key */
+ }
+ assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
+ return ofs;
fail:
- return -1;
+ return -1;
}
/* The maximum number of entries in a MergeState's pending-runs stack.
@@ -1272,48 +1272,48 @@ fail:
* a convenient way to pass state around among the helper functions.
*/
struct s_slice {
- PyObject **base;
- Py_ssize_t len;
+ PyObject **base;
+ Py_ssize_t len;
};
typedef struct s_MergeState {
- /* This controls when we get *into* galloping mode. It's initialized
- * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
- * random data, and lower for highly structured data.
- */
- Py_ssize_t min_gallop;
-
- /* 'a' is temp storage to help with merges. It contains room for
- * alloced entries.
- */
- PyObject **a; /* may point to temparray below */
- Py_ssize_t alloced;
-
- /* A stack of n pending runs yet to be merged. Run #i starts at
- * address base[i] and extends for len[i] elements. It's always
- * true (so long as the indices are in bounds) that
- *
- * pending[i].base + pending[i].len == pending[i+1].base
- *
- * so we could cut the storage for this, but it's a minor amount,
- * and keeping all the info explicit simplifies the code.
- */
- int n;
- struct s_slice pending[MAX_MERGE_PENDING];
-
- /* 'a' points to this when possible, rather than muck with malloc. */
- PyObject *temparray[MERGESTATE_TEMP_SIZE];
+ /* This controls when we get *into* galloping mode. It's initialized
+ * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
+ * random data, and lower for highly structured data.
+ */
+ Py_ssize_t min_gallop;
+
+ /* 'a' is temp storage to help with merges. It contains room for
+ * alloced entries.
+ */
+ PyObject **a; /* may point to temparray below */
+ Py_ssize_t alloced;
+
+ /* A stack of n pending runs yet to be merged. Run #i starts at
+ * address base[i] and extends for len[i] elements. It's always
+ * true (so long as the indices are in bounds) that
+ *
+ * pending[i].base + pending[i].len == pending[i+1].base
+ *
+ * so we could cut the storage for this, but it's a minor amount,
+ * and keeping all the info explicit simplifies the code.
+ */
+ int n;
+ struct s_slice pending[MAX_MERGE_PENDING];
+
+ /* 'a' points to this when possible, rather than muck with malloc. */
+ PyObject *temparray[MERGESTATE_TEMP_SIZE];
} MergeState;
/* Conceptually a MergeState's constructor. */
static void
merge_init(MergeState *ms)
{
- assert(ms != NULL);
- ms->a = ms->temparray;
- ms->alloced = MERGESTATE_TEMP_SIZE;
- ms->n = 0;
- ms->min_gallop = MIN_GALLOP;
+ assert(ms != NULL);
+ ms->a = ms->temparray;
+ ms->alloced = MERGESTATE_TEMP_SIZE;
+ ms->n = 0;
+ ms->min_gallop = MIN_GALLOP;
}
/* Free all the temp memory owned by the MergeState. This must be called
@@ -1323,11 +1323,11 @@ merge_init(MergeState *ms)
static void
merge_freemem(MergeState *ms)
{
- assert(ms != NULL);
- if (ms->a != ms->temparray)
- PyMem_Free(ms->a);
- ms->a = ms->temparray;
- ms->alloced = MERGESTATE_TEMP_SIZE;
+ assert(ms != NULL);
+ if (ms->a != ms->temparray)
+ PyMem_Free(ms->a);
+ ms->a = ms->temparray;
+ ms->alloced = MERGESTATE_TEMP_SIZE;
}
/* Ensure enough temp memory for 'need' array slots is available.
@@ -1336,28 +1336,28 @@ merge_freemem(MergeState *ms)
static int
merge_getmem(MergeState *ms, Py_ssize_t need)
{
- assert(ms != NULL);
- if (need <= ms->alloced)
- return 0;
- /* Don't realloc! That can cost cycles to copy the old data, but
- * we don't care what's in the block.
- */
- merge_freemem(ms);
- if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
- PyErr_NoMemory();
- return -1;
- }
- ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
- if (ms->a) {
- ms->alloced = need;
- return 0;
- }
- PyErr_NoMemory();
- merge_freemem(ms); /* reset to sane state */
- return -1;
-}
-#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
- merge_getmem(MS, NEED))
+ assert(ms != NULL);
+ if (need <= ms->alloced)
+ return 0;
+ /* Don't realloc! That can cost cycles to copy the old data, but
+ * we don't care what's in the block.
+ */
+ merge_freemem(ms);
+ if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
+ if (ms->a) {
+ ms->alloced = need;
+ return 0;
+ }
+ PyErr_NoMemory();
+ merge_freemem(ms); /* reset to sane state */
+ return -1;
+}
+#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
+ merge_getmem(MS, NEED))
/* Merge the na elements starting at pa with the nb elements starting at pb
* in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
@@ -1369,125 +1369,125 @@ static Py_ssize_t
merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na,
PyObject **pb, Py_ssize_t nb)
{
- Py_ssize_t k;
- PyObject **dest;
- int result = -1; /* guilty until proved innocent */
- Py_ssize_t min_gallop;
-
- assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
- if (MERGE_GETMEM(ms, na) < 0)
- return -1;
- memcpy(ms->a, pa, na * sizeof(PyObject*));
- dest = pa;
- pa = ms->a;
-
- *dest++ = *pb++;
- --nb;
- if (nb == 0)
- goto Succeed;
- if (na == 1)
- goto CopyB;
-
- min_gallop = ms->min_gallop;
- for (;;) {
- Py_ssize_t acount = 0; /* # of times A won in a row */
- Py_ssize_t bcount = 0; /* # of times B won in a row */
-
- /* Do the straightforward thing until (if ever) one run
- * appears to win consistently.
- */
- for (;;) {
- assert(na > 1 && nb > 0);
- k = ISLT(*pb, *pa);
- if (k) {
- if (k < 0)
- goto Fail;
- *dest++ = *pb++;
- ++bcount;
- acount = 0;
- --nb;
- if (nb == 0)
- goto Succeed;
- if (bcount >= min_gallop)
- break;
- }
- else {
- *dest++ = *pa++;
- ++acount;
- bcount = 0;
- --na;
- if (na == 1)
- goto CopyB;
- if (acount >= min_gallop)
- break;
- }
- }
-
- /* One run is winning so consistently that galloping may
- * be a huge win. So try that, and continue galloping until
- * (if ever) neither run appears to be winning consistently
- * anymore.
- */
- ++min_gallop;
- do {
- assert(na > 1 && nb > 0);
- min_gallop -= min_gallop > 1;
- ms->min_gallop = min_gallop;
- k = gallop_right(*pb, pa, na, 0);
- acount = k;
- if (k) {
- if (k < 0)
- goto Fail;
- memcpy(dest, pa, k * sizeof(PyObject *));
- dest += k;
- pa += k;
- na -= k;
- if (na == 1)
- goto CopyB;
- /* na==0 is impossible now if the comparison
- * function is consistent, but we can't assume
- * that it is.
- */
- if (na == 0)
- goto Succeed;
- }
- *dest++ = *pb++;
- --nb;
- if (nb == 0)
- goto Succeed;
-
- k = gallop_left(*pa, pb, nb, 0);
- bcount = k;
- if (k) {
- if (k < 0)
- goto Fail;
- memmove(dest, pb, k * sizeof(PyObject *));
- dest += k;
- pb += k;
- nb -= k;
- if (nb == 0)
- goto Succeed;
- }
- *dest++ = *pa++;
- --na;
- if (na == 1)
- goto CopyB;
- } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
- ++min_gallop; /* penalize it for leaving galloping mode */
- ms->min_gallop = min_gallop;
- }
+ Py_ssize_t k;
+ PyObject **dest;
+ int result = -1; /* guilty until proved innocent */
+ Py_ssize_t min_gallop;
+
+ assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
+ if (MERGE_GETMEM(ms, na) < 0)
+ return -1;
+ memcpy(ms->a, pa, na * sizeof(PyObject*));
+ dest = pa;
+ pa = ms->a;
+
+ *dest++ = *pb++;
+ --nb;
+ if (nb == 0)
+ goto Succeed;
+ if (na == 1)
+ goto CopyB;
+
+ min_gallop = ms->min_gallop;
+ for (;;) {
+ Py_ssize_t acount = 0; /* # of times A won in a row */
+ Py_ssize_t bcount = 0; /* # of times B won in a row */
+
+ /* Do the straightforward thing until (if ever) one run
+ * appears to win consistently.
+ */
+ for (;;) {
+ assert(na > 1 && nb > 0);
+ k = ISLT(*pb, *pa);
+ if (k) {
+ if (k < 0)
+ goto Fail;
+ *dest++ = *pb++;
+ ++bcount;
+ acount = 0;
+ --nb;
+ if (nb == 0)
+ goto Succeed;
+ if (bcount >= min_gallop)
+ break;
+ }
+ else {
+ *dest++ = *pa++;
+ ++acount;
+ bcount = 0;
+ --na;
+ if (na == 1)
+ goto CopyB;
+ if (acount >= min_gallop)
+ break;
+ }
+ }
+
+ /* One run is winning so consistently that galloping may
+ * be a huge win. So try that, and continue galloping until
+ * (if ever) neither run appears to be winning consistently
+ * anymore.
+ */
+ ++min_gallop;
+ do {
+ assert(na > 1 && nb > 0);
+ min_gallop -= min_gallop > 1;
+ ms->min_gallop = min_gallop;
+ k = gallop_right(*pb, pa, na, 0);
+ acount = k;
+ if (k) {
+ if (k < 0)
+ goto Fail;
+ memcpy(dest, pa, k * sizeof(PyObject *));
+ dest += k;
+ pa += k;
+ na -= k;
+ if (na == 1)
+ goto CopyB;
+ /* na==0 is impossible now if the comparison
+ * function is consistent, but we can't assume
+ * that it is.
+ */
+ if (na == 0)
+ goto Succeed;
+ }
+ *dest++ = *pb++;
+ --nb;
+ if (nb == 0)
+ goto Succeed;
+
+ k = gallop_left(*pa, pb, nb, 0);
+ bcount = k;
+ if (k) {
+ if (k < 0)
+ goto Fail;
+ memmove(dest, pb, k * sizeof(PyObject *));
+ dest += k;
+ pb += k;
+ nb -= k;
+ if (nb == 0)
+ goto Succeed;
+ }
+ *dest++ = *pa++;
+ --na;
+ if (na == 1)
+ goto CopyB;
+ } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
+ ++min_gallop; /* penalize it for leaving galloping mode */
+ ms->min_gallop = min_gallop;
+ }
Succeed:
- result = 0;
+ result = 0;
Fail:
- if (na)
- memcpy(dest, pa, na * sizeof(PyObject*));
- return result;
+ if (na)
+ memcpy(dest, pa, na * sizeof(PyObject*));
+ return result;
CopyB:
- assert(na == 1 && nb > 0);
- /* The last element of pa belongs at the end of the merge. */
- memmove(dest, pb, nb * sizeof(PyObject *));
- dest[nb] = *pa;
- return 0;
+ assert(na == 1 && nb > 0);
+ /* The last element of pa belongs at the end of the merge. */
+ memmove(dest, pb, nb * sizeof(PyObject *));
+ dest[nb] = *pa;
+ return 0;
}
/* Merge the na elements starting at pa with the nb elements starting at pb
@@ -1499,134 +1499,134 @@ CopyB:
static Py_ssize_t
merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
{
- Py_ssize_t k;
- PyObject **dest;
- int result = -1; /* guilty until proved innocent */
- PyObject **basea;
- PyObject **baseb;
- Py_ssize_t min_gallop;
-
- assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
- if (MERGE_GETMEM(ms, nb) < 0)
- return -1;
- dest = pb + nb - 1;
- memcpy(ms->a, pb, nb * sizeof(PyObject*));
- basea = pa;
- baseb = ms->a;
- pb = ms->a + nb - 1;
- pa += na - 1;
-
- *dest-- = *pa--;
- --na;
- if (na == 0)
- goto Succeed;
- if (nb == 1)
- goto CopyA;
-
- min_gallop = ms->min_gallop;
- for (;;) {
- Py_ssize_t acount = 0; /* # of times A won in a row */
- Py_ssize_t bcount = 0; /* # of times B won in a row */
-
- /* Do the straightforward thing until (if ever) one run
- * appears to win consistently.
- */
- for (;;) {
- assert(na > 0 && nb > 1);
- k = ISLT(*pb, *pa);
- if (k) {
- if (k < 0)
- goto Fail;
- *dest-- = *pa--;
- ++acount;
- bcount = 0;
- --na;
- if (na == 0)
- goto Succeed;
- if (acount >= min_gallop)
- break;
- }
- else {
- *dest-- = *pb--;
- ++bcount;
- acount = 0;
- --nb;
- if (nb == 1)
- goto CopyA;
- if (bcount >= min_gallop)
- break;
- }
- }
-
- /* One run is winning so consistently that galloping may
- * be a huge win. So try that, and continue galloping until
- * (if ever) neither run appears to be winning consistently
- * anymore.
- */
- ++min_gallop;
- do {
- assert(na > 0 && nb > 1);
- min_gallop -= min_gallop > 1;
- ms->min_gallop = min_gallop;
- k = gallop_right(*pb, basea, na, na-1);
- if (k < 0)
- goto Fail;
- k = na - k;
- acount = k;
- if (k) {
- dest -= k;
- pa -= k;
- memmove(dest+1, pa+1, k * sizeof(PyObject *));
- na -= k;
- if (na == 0)
- goto Succeed;
- }
- *dest-- = *pb--;
- --nb;
- if (nb == 1)
- goto CopyA;
-
- k = gallop_left(*pa, baseb, nb, nb-1);
- if (k < 0)
- goto Fail;
- k = nb - k;
- bcount = k;
- if (k) {
- dest -= k;
- pb -= k;
- memcpy(dest+1, pb+1, k * sizeof(PyObject *));
- nb -= k;
- if (nb == 1)
- goto CopyA;
- /* nb==0 is impossible now if the comparison
- * function is consistent, but we can't assume
- * that it is.
- */
- if (nb == 0)
- goto Succeed;
- }
- *dest-- = *pa--;
- --na;
- if (na == 0)
- goto Succeed;
- } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
- ++min_gallop; /* penalize it for leaving galloping mode */
- ms->min_gallop = min_gallop;
- }
+ Py_ssize_t k;
+ PyObject **dest;
+ int result = -1; /* guilty until proved innocent */
+ PyObject **basea;
+ PyObject **baseb;
+ Py_ssize_t min_gallop;
+
+ assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
+ if (MERGE_GETMEM(ms, nb) < 0)
+ return -1;
+ dest = pb + nb - 1;
+ memcpy(ms->a, pb, nb * sizeof(PyObject*));
+ basea = pa;
+ baseb = ms->a;
+ pb = ms->a + nb - 1;
+ pa += na - 1;
+
+ *dest-- = *pa--;
+ --na;
+ if (na == 0)
+ goto Succeed;
+ if (nb == 1)
+ goto CopyA;
+
+ min_gallop = ms->min_gallop;
+ for (;;) {
+ Py_ssize_t acount = 0; /* # of times A won in a row */
+ Py_ssize_t bcount = 0; /* # of times B won in a row */
+
+ /* Do the straightforward thing until (if ever) one run
+ * appears to win consistently.
+ */
+ for (;;) {
+ assert(na > 0 && nb > 1);
+ k = ISLT(*pb, *pa);
+ if (k) {
+ if (k < 0)
+ goto Fail;
+ *dest-- = *pa--;
+ ++acount;
+ bcount = 0;
+ --na;
+ if (na == 0)
+ goto Succeed;
+ if (acount >= min_gallop)
+ break;
+ }
+ else {
+ *dest-- = *pb--;
+ ++bcount;
+ acount = 0;
+ --nb;
+ if (nb == 1)
+ goto CopyA;
+ if (bcount >= min_gallop)
+ break;
+ }
+ }
+
+ /* One run is winning so consistently that galloping may
+ * be a huge win. So try that, and continue galloping until
+ * (if ever) neither run appears to be winning consistently
+ * anymore.
+ */
+ ++min_gallop;
+ do {
+ assert(na > 0 && nb > 1);
+ min_gallop -= min_gallop > 1;
+ ms->min_gallop = min_gallop;
+ k = gallop_right(*pb, basea, na, na-1);
+ if (k < 0)
+ goto Fail;
+ k = na - k;
+ acount = k;
+ if (k) {
+ dest -= k;
+ pa -= k;
+ memmove(dest+1, pa+1, k * sizeof(PyObject *));
+ na -= k;
+ if (na == 0)
+ goto Succeed;
+ }
+ *dest-- = *pb--;
+ --nb;
+ if (nb == 1)
+ goto CopyA;
+
+ k = gallop_left(*pa, baseb, nb, nb-1);
+ if (k < 0)
+ goto Fail;
+ k = nb - k;
+ bcount = k;
+ if (k) {
+ dest -= k;
+ pb -= k;
+ memcpy(dest+1, pb+1, k * sizeof(PyObject *));
+ nb -= k;
+ if (nb == 1)
+ goto CopyA;
+ /* nb==0 is impossible now if the comparison
+ * function is consistent, but we can't assume
+ * that it is.
+ */
+ if (nb == 0)
+ goto Succeed;
+ }
+ *dest-- = *pa--;
+ --na;
+ if (na == 0)
+ goto Succeed;
+ } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
+ ++min_gallop; /* penalize it for leaving galloping mode */
+ ms->min_gallop = min_gallop;
+ }
Succeed:
- result = 0;
+ result = 0;
Fail:
- if (nb)
- memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
- return result;
+ if (nb)
+ memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
+ return result;
CopyA:
- assert(nb == 1 && na > 0);
- /* The first element of pb belongs at the front of the merge. */
- dest -= na;
- pa -= na;
- memmove(dest+1, pa+1, na * sizeof(PyObject *));
- *dest = *pb;
- return 0;
+ assert(nb == 1 && na > 0);
+ /* The first element of pb belongs at the front of the merge. */
+ dest -= na;
+ pa -= na;
+ memmove(dest+1, pa+1, na * sizeof(PyObject *));
+ *dest = *pb;
+ return 0;
}
/* Merge the two runs at stack indices i and i+1.
@@ -1635,56 +1635,56 @@ CopyA:
static Py_ssize_t
merge_at(MergeState *ms, Py_ssize_t i)
{
- PyObject **pa, **pb;
- Py_ssize_t na, nb;
- Py_ssize_t k;
-
- assert(ms != NULL);
- assert(ms->n >= 2);
- assert(i >= 0);
- assert(i == ms->n - 2 || i == ms->n - 3);
-
- pa = ms->pending[i].base;
- na = ms->pending[i].len;
- pb = ms->pending[i+1].base;
- nb = ms->pending[i+1].len;
- assert(na > 0 && nb > 0);
- assert(pa + na == pb);
-
- /* Record the length of the combined runs; if i is the 3rd-last
- * run now, also slide over the last run (which isn't involved
- * in this merge). The current run i+1 goes away in any case.
- */
- ms->pending[i].len = na + nb;
- if (i == ms->n - 3)
- ms->pending[i+1] = ms->pending[i+2];
- --ms->n;
-
- /* Where does b start in a? Elements in a before that can be
- * ignored (already in place).
- */
- k = gallop_right(*pb, pa, na, 0);
- if (k < 0)
- return -1;
- pa += k;
- na -= k;
- if (na == 0)
- return 0;
-
- /* Where does a end in b? Elements in b after that can be
- * ignored (already in place).
- */
- nb = gallop_left(pa[na-1], pb, nb, nb-1);
- if (nb <= 0)
- return nb;
-
- /* Merge what remains of the runs, using a temp array with
- * min(na, nb) elements.
- */
- if (na <= nb)
- return merge_lo(ms, pa, na, pb, nb);
- else
- return merge_hi(ms, pa, na, pb, nb);
+ PyObject **pa, **pb;
+ Py_ssize_t na, nb;
+ Py_ssize_t k;
+
+ assert(ms != NULL);
+ assert(ms->n >= 2);
+ assert(i >= 0);
+ assert(i == ms->n - 2 || i == ms->n - 3);
+
+ pa = ms->pending[i].base;
+ na = ms->pending[i].len;
+ pb = ms->pending[i+1].base;
+ nb = ms->pending[i+1].len;
+ assert(na > 0 && nb > 0);
+ assert(pa + na == pb);
+
+ /* Record the length of the combined runs; if i is the 3rd-last
+ * run now, also slide over the last run (which isn't involved
+ * in this merge). The current run i+1 goes away in any case.
+ */
+ ms->pending[i].len = na + nb;
+ if (i == ms->n - 3)
+ ms->pending[i+1] = ms->pending[i+2];
+ --ms->n;
+
+ /* Where does b start in a? Elements in a before that can be
+ * ignored (already in place).
+ */
+ k = gallop_right(*pb, pa, na, 0);
+ if (k < 0)
+ return -1;
+ pa += k;
+ na -= k;
+ if (na == 0)
+ return 0;
+
+ /* Where does a end in b? Elements in b after that can be
+ * ignored (already in place).
+ */
+ nb = gallop_left(pa[na-1], pb, nb, nb-1);
+ if (nb <= 0)
+ return nb;
+
+ /* Merge what remains of the runs, using a temp array with
+ * min(na, nb) elements.
+ */
+ if (na <= nb)
+ return merge_lo(ms, pa, na, pb, nb);
+ else
+ return merge_hi(ms, pa, na, pb, nb);
}
/* Examine the stack of runs waiting to be merged, merging adjacent runs
@@ -1700,25 +1700,25 @@ merge_at(MergeState *ms, Py_ssize_t i)
static int
merge_collapse(MergeState *ms)
{
- struct s_slice *p = ms->pending;
-
- assert(ms);
- while (ms->n > 1) {
- Py_ssize_t n = ms->n - 2;
- if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
- if (p[n-1].len < p[n+1].len)
- --n;
- if (merge_at(ms, n) < 0)
- return -1;
- }
- else if (p[n].len <= p[n+1].len) {
- if (merge_at(ms, n) < 0)
- return -1;
- }
- else
- break;
- }
- return 0;
+ struct s_slice *p = ms->pending;
+
+ assert(ms);
+ while (ms->n > 1) {
+ Py_ssize_t n = ms->n - 2;
+ if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
+ if (p[n-1].len < p[n+1].len)
+ --n;
+ if (merge_at(ms, n) < 0)
+ return -1;
+ }
+ else if (p[n].len <= p[n+1].len) {
+ if (merge_at(ms, n) < 0)
+ return -1;
+ }
+ else
+ break;
+ }
+ return 0;
}
/* Regardless of invariants, merge all runs on the stack until only one
@@ -1729,17 +1729,17 @@ merge_collapse(MergeState *ms)
static int
merge_force_collapse(MergeState *ms)
{
- struct s_slice *p = ms->pending;
+ struct s_slice *p = ms->pending;
- assert(ms);
- while (ms->n > 1) {
- Py_ssize_t n = ms->n - 2;
- if (n > 0 && p[n-1].len < p[n+1].len)
- --n;
- if (merge_at(ms, n) < 0)
- return -1;
- }
- return 0;
+ assert(ms);
+ while (ms->n > 1) {
+ Py_ssize_t n = ms->n - 2;
+ if (n > 0 && p[n-1].len < p[n+1].len)
+ --n;
+ if (merge_at(ms, n) < 0)
+ return -1;
+ }
+ return 0;
}
/* Compute a good value for the minimum run length; natural runs shorter
@@ -1755,14 +1755,14 @@ merge_force_collapse(MergeState *ms)
static Py_ssize_t
merge_compute_minrun(Py_ssize_t n)
{
- Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
+ Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
- assert(n >= 0);
- while (n >= 64) {
- r |= n & 1;
- n >>= 1;
- }
- return n + r;
+ assert(n >= 0);
+ while (n >= 64) {
+ r |= n & 1;
+ n >>= 1;
+ }
+ return n + r;
}
/* Special wrapper to support stable sorting using the decorate-sort-undecorate
@@ -1773,9 +1773,9 @@ merge_compute_minrun(Py_ssize_t n)
a full record. */
typedef struct {
- PyObject_HEAD
- PyObject *key;
- PyObject *value;
+ PyObject_HEAD
+ PyObject *key;
+ PyObject *value;
} sortwrapperobject;
PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
@@ -1785,51 +1785,51 @@ static void
sortwrapper_dealloc(sortwrapperobject *);
PyTypeObject PySortWrapper_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "sortwrapper", /* tp_name */
- sizeof(sortwrapperobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)sortwrapper_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- sortwrapper_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "sortwrapper", /* tp_name */
+ sizeof(sortwrapperobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)sortwrapper_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ sortwrapper_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
};
static PyObject *
sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
{
- if (!PyObject_TypeCheck(b, &PySortWrapper_Type)) {
- PyErr_SetString(PyExc_TypeError,
- "expected a sortwrapperobject");
- return NULL;
- }
- return PyObject_RichCompare(a->key, b->key, op);
+ if (!PyObject_TypeCheck(b, &PySortWrapper_Type)) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected a sortwrapperobject");
+ return NULL;
+ }
+ return PyObject_RichCompare(a->key, b->key, op);
}
static void
sortwrapper_dealloc(sortwrapperobject *so)
{
- Py_XDECREF(so->key);
- Py_XDECREF(so->value);
- PyObject_Del(so);
+ Py_XDECREF(so->key);
+ Py_XDECREF(so->value);
+ PyObject_Del(so);
}
/* Returns a new reference to a sortwrapper.
@@ -1838,30 +1838,30 @@ sortwrapper_dealloc(sortwrapperobject *so)
static PyObject *
build_sortwrapper(PyObject *key, PyObject *value)
{
- sortwrapperobject *so;
+ sortwrapperobject *so;
- so = PyObject_New(sortwrapperobject, &PySortWrapper_Type);
- if (so == NULL)
- return NULL;
- so->key = key;
- so->value = value;
- return (PyObject *)so;
+ so = PyObject_New(sortwrapperobject, &PySortWrapper_Type);
+ if (so == NULL)
+ return NULL;
+ so->key = key;
+ so->value = value;
+ return (PyObject *)so;
}
/* Returns a new reference to the value underlying the wrapper. */
static PyObject *
sortwrapper_getvalue(PyObject *so)
{
- PyObject *value;
+ PyObject *value;
- if (!PyObject_TypeCheck(so, &PySortWrapper_Type)) {
- PyErr_SetString(PyExc_TypeError,
- "expected a sortwrapperobject");
- return NULL;
- }
- value = ((sortwrapperobject *)so)->value;
- Py_INCREF(value);
- return value;
+ if (!PyObject_TypeCheck(so, &PySortWrapper_Type)) {
+ PyErr_SetString(PyExc_TypeError,
+ "expected a sortwrapperobject");
+ return NULL;
+ }
+ value = ((sortwrapperobject *)so)->value;
+ Py_INCREF(value);
+ return value;
}
/* An adaptive, stable, natural mergesort. See listsort.txt.
@@ -1872,163 +1872,163 @@ sortwrapper_getvalue(PyObject *so)
static PyObject *
listsort(PyListObject *self, PyObject *args, PyObject *kwds)
{
- MergeState ms;
- PyObject **lo, **hi;
- Py_ssize_t nremaining;
- Py_ssize_t minrun;
- Py_ssize_t saved_ob_size, saved_allocated;
- PyObject **saved_ob_item;
- PyObject **final_ob_item;
- PyObject *result = NULL; /* guilty until proved innocent */
- int reverse = 0;
- PyObject *keyfunc = NULL;
- Py_ssize_t i;
- PyObject *key, *value, *kvpair;
- static char *kwlist[] = {"key", "reverse", 0};
-
- assert(self != NULL);
- assert (PyList_Check(self));
- if (args != NULL) {
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
- kwlist, &keyfunc, &reverse))
- return NULL;
- if (Py_SIZE(args) > 0) {
- PyErr_SetString(PyExc_TypeError,
- "must use keyword argument for key function");
- return NULL;
- }
- }
- if (keyfunc == Py_None)
- keyfunc = NULL;
-
- /* The list is temporarily made empty, so that mutations performed
- * by comparison functions can't affect the slice of memory we're
- * sorting (allowing mutations during sorting is a core-dump
- * factory, since ob_item may change).
- */
- saved_ob_size = Py_SIZE(self);
- saved_ob_item = self->ob_item;
- saved_allocated = self->allocated;
- Py_SIZE(self) = 0;
- self->ob_item = NULL;
- self->allocated = -1; /* any operation will reset it to >= 0 */
-
- if (keyfunc != NULL) {
- for (i=0 ; i < saved_ob_size ; i++) {
- value = saved_ob_item[i];
- key = PyObject_CallFunctionObjArgs(keyfunc, value,
- NULL);
- if (key == NULL) {
- for (i=i-1 ; i>=0 ; i--) {
- kvpair = saved_ob_item[i];
- value = sortwrapper_getvalue(kvpair);
- saved_ob_item[i] = value;
- Py_DECREF(kvpair);
- }
- goto dsu_fail;
- }
- kvpair = build_sortwrapper(key, value);
- if (kvpair == NULL)
- goto dsu_fail;
- saved_ob_item[i] = kvpair;
- }
- }
-
- /* Reverse sort stability achieved by initially reversing the list,
- applying a stable forward sort, then reversing the final result. */
- if (reverse && saved_ob_size > 1)
- reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
-
- merge_init(&ms);
-
- nremaining = saved_ob_size;
- if (nremaining < 2)
- goto succeed;
-
- /* March over the array once, left to right, finding natural runs,
- * and extending short natural runs to minrun elements.
- */
- lo = saved_ob_item;
- hi = lo + nremaining;
- minrun = merge_compute_minrun(nremaining);
- do {
- int descending;
- Py_ssize_t n;
-
- /* Identify next run. */
- n = count_run(lo, hi, &descending);
- if (n < 0)
- goto fail;
- if (descending)
- reverse_slice(lo, lo + n);
- /* If short, extend to min(minrun, nremaining). */
- if (n < minrun) {
- const Py_ssize_t force = nremaining <= minrun ?
- nremaining : minrun;
- if (binarysort(lo, lo + force, lo + n) < 0)
- goto fail;
- n = force;
- }
- /* Push run onto pending-runs stack, and maybe merge. */
- assert(ms.n < MAX_MERGE_PENDING);
- ms.pending[ms.n].base = lo;
- ms.pending[ms.n].len = n;
- ++ms.n;
- if (merge_collapse(&ms) < 0)
- goto fail;
- /* Advance to find next run. */
- lo += n;
- nremaining -= n;
- } while (nremaining);
- assert(lo == hi);
-
- if (merge_force_collapse(&ms) < 0)
- goto fail;
- assert(ms.n == 1);
- assert(ms.pending[0].base == saved_ob_item);
- assert(ms.pending[0].len == saved_ob_size);
+ MergeState ms;
+ PyObject **lo, **hi;
+ Py_ssize_t nremaining;
+ Py_ssize_t minrun;
+ Py_ssize_t saved_ob_size, saved_allocated;
+ PyObject **saved_ob_item;
+ PyObject **final_ob_item;
+ PyObject *result = NULL; /* guilty until proved innocent */
+ int reverse = 0;
+ PyObject *keyfunc = NULL;
+ Py_ssize_t i;
+ PyObject *key, *value, *kvpair;
+ static char *kwlist[] = {"key", "reverse", 0};
+
+ assert(self != NULL);
+ assert (PyList_Check(self));
+ if (args != NULL) {
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
+ kwlist, &keyfunc, &reverse))
+ return NULL;
+ if (Py_SIZE(args) > 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "must use keyword argument for key function");
+ return NULL;
+ }
+ }
+ if (keyfunc == Py_None)
+ keyfunc = NULL;
+
+ /* The list is temporarily made empty, so that mutations performed
+ * by comparison functions can't affect the slice of memory we're
+ * sorting (allowing mutations during sorting is a core-dump
+ * factory, since ob_item may change).
+ */
+ saved_ob_size = Py_SIZE(self);
+ saved_ob_item = self->ob_item;
+ saved_allocated = self->allocated;
+ Py_SIZE(self) = 0;
+ self->ob_item = NULL;
+ self->allocated = -1; /* any operation will reset it to >= 0 */
+
+ if (keyfunc != NULL) {
+ for (i=0 ; i < saved_ob_size ; i++) {
+ value = saved_ob_item[i];
+ key = PyObject_CallFunctionObjArgs(keyfunc, value,
+ NULL);
+ if (key == NULL) {
+ for (i=i-1 ; i>=0 ; i--) {
+ kvpair = saved_ob_item[i];
+ value = sortwrapper_getvalue(kvpair);
+ saved_ob_item[i] = value;
+ Py_DECREF(kvpair);
+ }
+ goto dsu_fail;
+ }
+ kvpair = build_sortwrapper(key, value);
+ if (kvpair == NULL)
+ goto dsu_fail;
+ saved_ob_item[i] = kvpair;
+ }
+ }
+
+ /* Reverse sort stability achieved by initially reversing the list,
+ applying a stable forward sort, then reversing the final result. */
+ if (reverse && saved_ob_size > 1)
+ reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
+
+ merge_init(&ms);
+
+ nremaining = saved_ob_size;
+ if (nremaining < 2)
+ goto succeed;
+
+ /* March over the array once, left to right, finding natural runs,
+ * and extending short natural runs to minrun elements.
+ */
+ lo = saved_ob_item;
+ hi = lo + nremaining;
+ minrun = merge_compute_minrun(nremaining);
+ do {
+ int descending;
+ Py_ssize_t n;
+
+ /* Identify next run. */
+ n = count_run(lo, hi, &descending);
+ if (n < 0)
+ goto fail;
+ if (descending)
+ reverse_slice(lo, lo + n);
+ /* If short, extend to min(minrun, nremaining). */
+ if (n < minrun) {
+ const Py_ssize_t force = nremaining <= minrun ?
+ nremaining : minrun;
+ if (binarysort(lo, lo + force, lo + n) < 0)
+ goto fail;
+ n = force;
+ }
+ /* Push run onto pending-runs stack, and maybe merge. */
+ assert(ms.n < MAX_MERGE_PENDING);
+ ms.pending[ms.n].base = lo;
+ ms.pending[ms.n].len = n;
+ ++ms.n;
+ if (merge_collapse(&ms) < 0)
+ goto fail;
+ /* Advance to find next run. */
+ lo += n;
+ nremaining -= n;
+ } while (nremaining);
+ assert(lo == hi);
+
+ if (merge_force_collapse(&ms) < 0)
+ goto fail;
+ assert(ms.n == 1);
+ assert(ms.pending[0].base == saved_ob_item);
+ assert(ms.pending[0].len == saved_ob_size);
succeed:
- result = Py_None;
+ result = Py_None;
fail:
- if (keyfunc != NULL) {
- for (i=0 ; i < saved_ob_size ; i++) {
- kvpair = saved_ob_item[i];
- value = sortwrapper_getvalue(kvpair);
- saved_ob_item[i] = value;
- Py_DECREF(kvpair);
- }
- }
-
- if (self->allocated != -1 && result != NULL) {
- /* The user mucked with the list during the sort,
- * and we don't already have another error to report.
- */
- PyErr_SetString(PyExc_ValueError, "list modified during sort");
- result = NULL;
- }
-
- if (reverse && saved_ob_size > 1)
- reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
-
- merge_freemem(&ms);
+ if (keyfunc != NULL) {
+ for (i=0 ; i < saved_ob_size ; i++) {
+ kvpair = saved_ob_item[i];
+ value = sortwrapper_getvalue(kvpair);
+ saved_ob_item[i] = value;
+ Py_DECREF(kvpair);
+ }
+ }
+
+ if (self->allocated != -1 && result != NULL) {
+ /* The user mucked with the list during the sort,
+ * and we don't already have another error to report.
+ */
+ PyErr_SetString(PyExc_ValueError, "list modified during sort");
+ result = NULL;
+ }
+
+ if (reverse && saved_ob_size > 1)
+ reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
+
+ merge_freemem(&ms);
dsu_fail:
- final_ob_item = self->ob_item;
- i = Py_SIZE(self);
- Py_SIZE(self) = saved_ob_size;
- self->ob_item = saved_ob_item;
- self->allocated = saved_allocated;
- if (final_ob_item != NULL) {
- /* we cannot use list_clear() for this because it does not
- guarantee that the list is really empty when it returns */
- while (--i >= 0) {
- Py_XDECREF(final_ob_item[i]);
- }
- PyMem_FREE(final_ob_item);
- }
- Py_XINCREF(result);
- return result;
+ final_ob_item = self->ob_item;
+ i = Py_SIZE(self);
+ Py_SIZE(self) = saved_ob_size;
+ self->ob_item = saved_ob_item;
+ self->allocated = saved_allocated;
+ if (final_ob_item != NULL) {
+ /* we cannot use list_clear() for this because it does not
+ guarantee that the list is really empty when it returns */
+ while (--i >= 0) {
+ Py_XDECREF(final_ob_item[i]);
+ }
+ PyMem_FREE(final_ob_item);
+ }
+ Py_XINCREF(result);
+ return result;
}
#undef IFLT
#undef ISLT
@@ -2036,248 +2036,248 @@ dsu_fail:
int
PyList_Sort(PyObject *v)
{
- if (v == NULL || !PyList_Check(v)) {
- PyErr_BadInternalCall();
- return -1;
- }
- v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
- if (v == NULL)
- return -1;
- Py_DECREF(v);
- return 0;
+ if (v == NULL || !PyList_Check(v)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
+ if (v == NULL)
+ return -1;
+ Py_DECREF(v);
+ return 0;
}
static PyObject *
listreverse(PyListObject *self)
{
- if (Py_SIZE(self) > 1)
- reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
- Py_RETURN_NONE;
+ if (Py_SIZE(self) > 1)
+ reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
+ Py_RETURN_NONE;
}
int
PyList_Reverse(PyObject *v)
{
- PyListObject *self = (PyListObject *)v;
+ PyListObject *self = (PyListObject *)v;
- if (v == NULL || !PyList_Check(v)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (Py_SIZE(self) > 1)
- reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
- return 0;
+ if (v == NULL || !PyList_Check(v)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (Py_SIZE(self) > 1)
+ reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
+ return 0;
}
PyObject *
PyList_AsTuple(PyObject *v)
{
- PyObject *w;
- PyObject **p, **q;
- Py_ssize_t n;
- if (v == NULL || !PyList_Check(v)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- n = Py_SIZE(v);
- w = PyTuple_New(n);
- if (w == NULL)
- return NULL;
- p = ((PyTupleObject *)w)->ob_item;
- q = ((PyListObject *)v)->ob_item;
- while (--n >= 0) {
- Py_INCREF(*q);
- *p = *q;
- p++;
- q++;
- }
- return w;
+ PyObject *w;
+ PyObject **p, **q;
+ Py_ssize_t n;
+ if (v == NULL || !PyList_Check(v)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ n = Py_SIZE(v);
+ w = PyTuple_New(n);
+ if (w == NULL)
+ return NULL;
+ p = ((PyTupleObject *)w)->ob_item;
+ q = ((PyListObject *)v)->ob_item;
+ while (--n >= 0) {
+ Py_INCREF(*q);
+ *p = *q;
+ p++;
+ q++;
+ }
+ return w;
}
static PyObject *
listindex(PyListObject *self, PyObject *args)
{
- Py_ssize_t i, start=0, stop=Py_SIZE(self);
- PyObject *v;
-
- if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
- _PyEval_SliceIndex, &start,
- _PyEval_SliceIndex, &stop))
- return NULL;
- if (start < 0) {
- start += Py_SIZE(self);
- if (start < 0)
- start = 0;
- }
- if (stop < 0) {
- stop += Py_SIZE(self);
- if (stop < 0)
- stop = 0;
- }
- for (i = start; i < stop && i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
- if (cmp > 0)
- return PyLong_FromSsize_t(i);
- else if (cmp < 0)
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
- return NULL;
+ Py_ssize_t i, start=0, stop=Py_SIZE(self);
+ PyObject *v;
+
+ if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
+ _PyEval_SliceIndex, &start,
+ _PyEval_SliceIndex, &stop))
+ return NULL;
+ if (start < 0) {
+ start += Py_SIZE(self);
+ if (start < 0)
+ start = 0;
+ }
+ if (stop < 0) {
+ stop += Py_SIZE(self);
+ if (stop < 0)
+ stop = 0;
+ }
+ for (i = start; i < stop && i < Py_SIZE(self); i++) {
+ int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
+ if (cmp > 0)
+ return PyLong_FromSsize_t(i);
+ else if (cmp < 0)
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
+ return NULL;
}
static PyObject *
listcount(PyListObject *self, PyObject *v)
{
- Py_ssize_t count = 0;
- Py_ssize_t i;
+ Py_ssize_t count = 0;
+ Py_ssize_t i;
- for (i = 0; i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
- if (cmp > 0)
- count++;
- else if (cmp < 0)
- return NULL;
- }
- return PyLong_FromSsize_t(count);
+ for (i = 0; i < Py_SIZE(self); i++) {
+ int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
+ if (cmp > 0)
+ count++;
+ else if (cmp < 0)
+ return NULL;
+ }
+ return PyLong_FromSsize_t(count);
}
static PyObject *
listremove(PyListObject *self, PyObject *v)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i = 0; i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
- if (cmp > 0) {
- if (list_ass_slice(self, i, i+1,
- (PyObject *)NULL) == 0)
- Py_RETURN_NONE;
- return NULL;
- }
- else if (cmp < 0)
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
- return NULL;
+ for (i = 0; i < Py_SIZE(self); i++) {
+ int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
+ if (cmp > 0) {
+ if (list_ass_slice(self, i, i+1,
+ (PyObject *)NULL) == 0)
+ Py_RETURN_NONE;
+ return NULL;
+ }
+ else if (cmp < 0)
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
+ return NULL;
}
static int
list_traverse(PyListObject *o, visitproc visit, void *arg)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i = Py_SIZE(o); --i >= 0; )
- Py_VISIT(o->ob_item[i]);
- return 0;
+ for (i = Py_SIZE(o); --i >= 0; )
+ Py_VISIT(o->ob_item[i]);
+ return 0;
}
static PyObject *
list_richcompare(PyObject *v, PyObject *w, int op)
{
- PyListObject *vl, *wl;
- Py_ssize_t i;
-
- if (!PyList_Check(v) || !PyList_Check(w)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- vl = (PyListObject *)v;
- wl = (PyListObject *)w;
-
- if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
- /* Shortcut: if the lengths differ, the lists differ */
- PyObject *res;
- if (op == Py_EQ)
- res = Py_False;
- else
- res = Py_True;
- Py_INCREF(res);
- return res;
- }
-
- /* Search for the first index where items are different */
- for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
- int k = PyObject_RichCompareBool(vl->ob_item[i],
- wl->ob_item[i], Py_EQ);
- if (k < 0)
- return NULL;
- if (!k)
- break;
- }
-
- if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
- /* No more items to compare -- compare sizes */
- Py_ssize_t vs = Py_SIZE(vl);
- Py_ssize_t ws = Py_SIZE(wl);
- int cmp;
- PyObject *res;
- switch (op) {
- case Py_LT: cmp = vs < ws; break;
- case Py_LE: cmp = vs <= ws; break;
- case Py_EQ: cmp = vs == ws; break;
- case Py_NE: cmp = vs != ws; break;
- case Py_GT: cmp = vs > ws; break;
- case Py_GE: cmp = vs >= ws; break;
- default: return NULL; /* cannot happen */
- }
- if (cmp)
- res = Py_True;
- else
- res = Py_False;
- Py_INCREF(res);
- return res;
- }
-
- /* We have an item that differs -- shortcuts for EQ/NE */
- if (op == Py_EQ) {
- Py_INCREF(Py_False);
- return Py_False;
- }
- if (op == Py_NE) {
- Py_INCREF(Py_True);
- return Py_True;
- }
-
- /* Compare the final item again using the proper operator */
- return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
+ PyListObject *vl, *wl;
+ Py_ssize_t i;
+
+ if (!PyList_Check(v) || !PyList_Check(w)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ vl = (PyListObject *)v;
+ wl = (PyListObject *)w;
+
+ if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
+ /* Shortcut: if the lengths differ, the lists differ */
+ PyObject *res;
+ if (op == Py_EQ)
+ res = Py_False;
+ else
+ res = Py_True;
+ Py_INCREF(res);
+ return res;
+ }
+
+ /* Search for the first index where items are different */
+ for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
+ int k = PyObject_RichCompareBool(vl->ob_item[i],
+ wl->ob_item[i], Py_EQ);
+ if (k < 0)
+ return NULL;
+ if (!k)
+ break;
+ }
+
+ if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
+ /* No more items to compare -- compare sizes */
+ Py_ssize_t vs = Py_SIZE(vl);
+ Py_ssize_t ws = Py_SIZE(wl);
+ int cmp;
+ PyObject *res;
+ switch (op) {
+ case Py_LT: cmp = vs < ws; break;
+ case Py_LE: cmp = vs <= ws; break;
+ case Py_EQ: cmp = vs == ws; break;
+ case Py_NE: cmp = vs != ws; break;
+ case Py_GT: cmp = vs > ws; break;
+ case Py_GE: cmp = vs >= ws; break;
+ default: return NULL; /* cannot happen */
+ }
+ if (cmp)
+ res = Py_True;
+ else
+ res = Py_False;
+ Py_INCREF(res);
+ return res;
+ }
+
+ /* We have an item that differs -- shortcuts for EQ/NE */
+ if (op == Py_EQ) {
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
+ if (op == Py_NE) {
+ Py_INCREF(Py_True);
+ return Py_True;
+ }
+
+ /* Compare the final item again using the proper operator */
+ return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
}
static int
list_init(PyListObject *self, PyObject *args, PyObject *kw)
{
- PyObject *arg = NULL;
- static char *kwlist[] = {"sequence", 0};
+ PyObject *arg = NULL;
+ static char *kwlist[] = {"sequence", 0};
- if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
- return -1;
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
+ return -1;
- /* Verify list invariants established by PyType_GenericAlloc() */
- assert(0 <= Py_SIZE(self));
- assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
- assert(self->ob_item != NULL ||
- self->allocated == 0 || self->allocated == -1);
+ /* Verify list invariants established by PyType_GenericAlloc() */
+ assert(0 <= Py_SIZE(self));
+ assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
+ assert(self->ob_item != NULL ||
+ self->allocated == 0 || self->allocated == -1);
- /* Empty previous contents */
- if (self->ob_item != NULL) {
- (void)list_clear(self);
- }
- if (arg != NULL) {
- PyObject *rv = listextend(self, arg);
- if (rv == NULL)
- return -1;
- Py_DECREF(rv);
- }
- return 0;
+ /* Empty previous contents */
+ if (self->ob_item != NULL) {
+ (void)list_clear(self);
+ }
+ if (arg != NULL) {
+ PyObject *rv = listextend(self, arg);
+ if (rv == NULL)
+ return -1;
+ Py_DECREF(rv);
+ }
+ return 0;
}
static PyObject *
list_sizeof(PyListObject *self)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = sizeof(PyListObject) + self->allocated * sizeof(void*);
- return PyLong_FromSsize_t(res);
+ res = sizeof(PyListObject) + self->allocated * sizeof(void*);
+ return PyLong_FromSsize_t(res);
}
static PyObject *list_iter(PyObject *seq);
@@ -2314,32 +2314,32 @@ PyDoc_STRVAR(sort_doc,
static PyObject *list_subscript(PyListObject*, PyObject*);
static PyMethodDef list_methods[] = {
- {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
- {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
- {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
- {"append", (PyCFunction)listappend, METH_O, append_doc},
- {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
- {"extend", (PyCFunction)listextend, METH_O, extend_doc},
- {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
- {"remove", (PyCFunction)listremove, METH_O, remove_doc},
- {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
- {"count", (PyCFunction)listcount, METH_O, count_doc},
- {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
- {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
- {NULL, NULL} /* sentinel */
+ {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
+ {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
+ {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
+ {"append", (PyCFunction)listappend, METH_O, append_doc},
+ {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
+ {"extend", (PyCFunction)listextend, METH_O, extend_doc},
+ {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
+ {"remove", (PyCFunction)listremove, METH_O, remove_doc},
+ {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
+ {"count", (PyCFunction)listcount, METH_O, count_doc},
+ {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
+ {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
+ {NULL, NULL} /* sentinel */
};
static PySequenceMethods list_as_sequence = {
- (lenfunc)list_length, /* sq_length */
- (binaryfunc)list_concat, /* sq_concat */
- (ssizeargfunc)list_repeat, /* sq_repeat */
- (ssizeargfunc)list_item, /* sq_item */
- 0, /* sq_slice */
- (ssizeobjargproc)list_ass_item, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)list_contains, /* sq_contains */
- (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
- (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
+ (lenfunc)list_length, /* sq_length */
+ (binaryfunc)list_concat, /* sq_concat */
+ (ssizeargfunc)list_repeat, /* sq_repeat */
+ (ssizeargfunc)list_item, /* sq_item */
+ 0, /* sq_slice */
+ (ssizeobjargproc)list_ass_item, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)list_contains, /* sq_contains */
+ (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
+ (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
};
PyDoc_STRVAR(list_doc,
@@ -2349,275 +2349,275 @@ PyDoc_STRVAR(list_doc,
static PyObject *
list_subscript(PyListObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i;
- i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- if (i < 0)
- i += PyList_GET_SIZE(self);
- return list_item(self, i);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelength, cur, i;
- PyObject* result;
- PyObject* it;
- PyObject **src, **dest;
-
- if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
- &start, &stop, &step, &slicelength) < 0) {
- return NULL;
- }
-
- if (slicelength <= 0) {
- return PyList_New(0);
- }
- else if (step == 1) {
- return list_slice(self, start, stop);
- }
- else {
- result = PyList_New(slicelength);
- if (!result) return NULL;
-
- src = self->ob_item;
- dest = ((PyListObject *)result)->ob_item;
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- it = src[cur];
- Py_INCREF(it);
- dest[i] = it;
- }
-
- return result;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "list indices must be integers, not %.200s",
- item->ob_type->tp_name);
- return NULL;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i;
+ i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += PyList_GET_SIZE(self);
+ return list_item(self, i);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength, cur, i;
+ PyObject* result;
+ PyObject* it;
+ PyObject **src, **dest;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
+ &start, &stop, &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ return PyList_New(0);
+ }
+ else if (step == 1) {
+ return list_slice(self, start, stop);
+ }
+ else {
+ result = PyList_New(slicelength);
+ if (!result) return NULL;
+
+ src = self->ob_item;
+ dest = ((PyListObject *)result)->ob_item;
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ it = src[cur];
+ Py_INCREF(it);
+ dest[i] = it;
+ }
+
+ return result;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "list indices must be integers, not %.200s",
+ item->ob_type->tp_name);
+ return NULL;
+ }
}
static int
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return -1;
- if (i < 0)
- i += PyList_GET_SIZE(self);
- return list_ass_item(self, i, value);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelength;
-
- if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
- &start, &stop, &step, &slicelength) < 0) {
- return -1;
- }
-
- if (step == 1)
- return list_ass_slice(self, start, stop, value);
-
- /* Make sure s[5:2] = [..] inserts at the right place:
- before 5, not before 2. */
- if ((step < 0 && start < stop) ||
- (step > 0 && start > stop))
- stop = start;
-
- if (value == NULL) {
- /* delete slice */
- PyObject **garbage;
- size_t cur;
- Py_ssize_t i;
-
- if (slicelength <= 0)
- return 0;
-
- if (step < 0) {
- stop = start + 1;
- start = stop + step*(slicelength - 1) - 1;
- step = -step;
- }
-
- assert((size_t)slicelength <=
- PY_SIZE_MAX / sizeof(PyObject*));
-
- garbage = (PyObject**)
- PyMem_MALLOC(slicelength*sizeof(PyObject*));
- if (!garbage) {
- PyErr_NoMemory();
- return -1;
- }
-
- /* drawing pictures might help understand these for
- loops. Basically, we memmove the parts of the
- list that are *not* part of the slice: step-1
- items for each item that is part of the slice,
- and then tail end of the list that was not
- covered by the slice */
- for (cur = start, i = 0;
- cur < (size_t)stop;
- cur += step, i++) {
- Py_ssize_t lim = step - 1;
-
- garbage[i] = PyList_GET_ITEM(self, cur);
-
- if (cur + step >= (size_t)Py_SIZE(self)) {
- lim = Py_SIZE(self) - cur - 1;
- }
-
- memmove(self->ob_item + cur - i,
- self->ob_item + cur + 1,
- lim * sizeof(PyObject *));
- }
- cur = start + slicelength*step;
- if (cur < (size_t)Py_SIZE(self)) {
- memmove(self->ob_item + cur - slicelength,
- self->ob_item + cur,
- (Py_SIZE(self) - cur) *
- sizeof(PyObject *));
- }
-
- Py_SIZE(self) -= slicelength;
- list_resize(self, Py_SIZE(self));
-
- for (i = 0; i < slicelength; i++) {
- Py_DECREF(garbage[i]);
- }
- PyMem_FREE(garbage);
-
- return 0;
- }
- else {
- /* assign slice */
- PyObject *ins, *seq;
- PyObject **garbage, **seqitems, **selfitems;
- Py_ssize_t cur, i;
-
- /* protect against a[::-1] = a */
- if (self == (PyListObject*)value) {
- seq = list_slice((PyListObject*)value, 0,
- PyList_GET_SIZE(value));
- }
- else {
- seq = PySequence_Fast(value,
- "must assign iterable "
- "to extended slice");
- }
- if (!seq)
- return -1;
-
- if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
- PyErr_Format(PyExc_ValueError,
- "attempt to assign sequence of "
- "size %zd to extended slice of "
- "size %zd",
- PySequence_Fast_GET_SIZE(seq),
- slicelength);
- Py_DECREF(seq);
- return -1;
- }
-
- if (!slicelength) {
- Py_DECREF(seq);
- return 0;
- }
-
- garbage = (PyObject**)
- PyMem_MALLOC(slicelength*sizeof(PyObject*));
- if (!garbage) {
- Py_DECREF(seq);
- PyErr_NoMemory();
- return -1;
- }
-
- selfitems = self->ob_item;
- seqitems = PySequence_Fast_ITEMS(seq);
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- garbage[i] = selfitems[cur];
- ins = seqitems[i];
- Py_INCREF(ins);
- selfitems[cur] = ins;
- }
-
- for (i = 0; i < slicelength; i++) {
- Py_DECREF(garbage[i]);
- }
-
- PyMem_FREE(garbage);
- Py_DECREF(seq);
-
- return 0;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "list indices must be integers, not %.200s",
- item->ob_type->tp_name);
- return -1;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0)
+ i += PyList_GET_SIZE(self);
+ return list_ass_item(self, i, value);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
+ &start, &stop, &step, &slicelength) < 0) {
+ return -1;
+ }
+
+ if (step == 1)
+ return list_ass_slice(self, start, stop, value);
+
+ /* Make sure s[5:2] = [..] inserts at the right place:
+ before 5, not before 2. */
+ if ((step < 0 && start < stop) ||
+ (step > 0 && start > stop))
+ stop = start;
+
+ if (value == NULL) {
+ /* delete slice */
+ PyObject **garbage;
+ size_t cur;
+ Py_ssize_t i;
+
+ if (slicelength <= 0)
+ return 0;
+
+ if (step < 0) {
+ stop = start + 1;
+ start = stop + step*(slicelength - 1) - 1;
+ step = -step;
+ }
+
+ assert((size_t)slicelength <=
+ PY_SIZE_MAX / sizeof(PyObject*));
+
+ garbage = (PyObject**)
+ PyMem_MALLOC(slicelength*sizeof(PyObject*));
+ if (!garbage) {
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ /* drawing pictures might help understand these for
+ loops. Basically, we memmove the parts of the
+ list that are *not* part of the slice: step-1
+ items for each item that is part of the slice,
+ and then tail end of the list that was not
+ covered by the slice */
+ for (cur = start, i = 0;
+ cur < (size_t)stop;
+ cur += step, i++) {
+ Py_ssize_t lim = step - 1;
+
+ garbage[i] = PyList_GET_ITEM(self, cur);
+
+ if (cur + step >= (size_t)Py_SIZE(self)) {
+ lim = Py_SIZE(self) - cur - 1;
+ }
+
+ memmove(self->ob_item + cur - i,
+ self->ob_item + cur + 1,
+ lim * sizeof(PyObject *));
+ }
+ cur = start + slicelength*step;
+ if (cur < (size_t)Py_SIZE(self)) {
+ memmove(self->ob_item + cur - slicelength,
+ self->ob_item + cur,
+ (Py_SIZE(self) - cur) *
+ sizeof(PyObject *));
+ }
+
+ Py_SIZE(self) -= slicelength;
+ list_resize(self, Py_SIZE(self));
+
+ for (i = 0; i < slicelength; i++) {
+ Py_DECREF(garbage[i]);
+ }
+ PyMem_FREE(garbage);
+
+ return 0;
+ }
+ else {
+ /* assign slice */
+ PyObject *ins, *seq;
+ PyObject **garbage, **seqitems, **selfitems;
+ Py_ssize_t cur, i;
+
+ /* protect against a[::-1] = a */
+ if (self == (PyListObject*)value) {
+ seq = list_slice((PyListObject*)value, 0,
+ PyList_GET_SIZE(value));
+ }
+ else {
+ seq = PySequence_Fast(value,
+ "must assign iterable "
+ "to extended slice");
+ }
+ if (!seq)
+ return -1;
+
+ if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
+ PyErr_Format(PyExc_ValueError,
+ "attempt to assign sequence of "
+ "size %zd to extended slice of "
+ "size %zd",
+ PySequence_Fast_GET_SIZE(seq),
+ slicelength);
+ Py_DECREF(seq);
+ return -1;
+ }
+
+ if (!slicelength) {
+ Py_DECREF(seq);
+ return 0;
+ }
+
+ garbage = (PyObject**)
+ PyMem_MALLOC(slicelength*sizeof(PyObject*));
+ if (!garbage) {
+ Py_DECREF(seq);
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ selfitems = self->ob_item;
+ seqitems = PySequence_Fast_ITEMS(seq);
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ garbage[i] = selfitems[cur];
+ ins = seqitems[i];
+ Py_INCREF(ins);
+ selfitems[cur] = ins;
+ }
+
+ for (i = 0; i < slicelength; i++) {
+ Py_DECREF(garbage[i]);
+ }
+
+ PyMem_FREE(garbage);
+ Py_DECREF(seq);
+
+ return 0;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "list indices must be integers, not %.200s",
+ item->ob_type->tp_name);
+ return -1;
+ }
}
static PyMappingMethods list_as_mapping = {
- (lenfunc)list_length,
- (binaryfunc)list_subscript,
- (objobjargproc)list_ass_subscript
+ (lenfunc)list_length,
+ (binaryfunc)list_subscript,
+ (objobjargproc)list_ass_subscript
};
PyTypeObject PyList_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "list",
- sizeof(PyListObject),
- 0,
- (destructor)list_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)list_repr, /* tp_repr */
- 0, /* tp_as_number */
- &list_as_sequence, /* tp_as_sequence */
- &list_as_mapping, /* tp_as_mapping */
- (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
- list_doc, /* tp_doc */
- (traverseproc)list_traverse, /* tp_traverse */
- (inquiry)list_clear, /* tp_clear */
- list_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- list_iter, /* tp_iter */
- 0, /* tp_iternext */
- list_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)list_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "list",
+ sizeof(PyListObject),
+ 0,
+ (destructor)list_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)list_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &list_as_sequence, /* tp_as_sequence */
+ &list_as_mapping, /* tp_as_mapping */
+ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
+ list_doc, /* tp_doc */
+ (traverseproc)list_traverse, /* tp_traverse */
+ (inquiry)list_clear, /* tp_clear */
+ list_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ list_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ list_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)list_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/*********************** List Iterator **************************/
typedef struct {
- PyObject_HEAD
- long it_index;
- PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ long it_index;
+ PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listiterobject;
static PyObject *list_iter(PyObject *);
@@ -2629,119 +2629,119 @@ static PyObject *listiter_len(listiterobject *);
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef listiter_methods[] = {
- {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyListIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "list_iterator", /* tp_name */
- sizeof(listiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)listiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)listiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)listiter_next, /* tp_iternext */
- listiter_methods, /* tp_methods */
- 0, /* tp_members */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "list_iterator", /* tp_name */
+ sizeof(listiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)listiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)listiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)listiter_next, /* tp_iternext */
+ listiter_methods, /* tp_methods */
+ 0, /* tp_members */
};
static PyObject *
list_iter(PyObject *seq)
{
- listiterobject *it;
+ listiterobject *it;
- if (!PyList_Check(seq)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- it = PyObject_GC_New(listiterobject, &PyListIter_Type);
- if (it == NULL)
- return NULL;
- it->it_index = 0;
- Py_INCREF(seq);
- it->it_seq = (PyListObject *)seq;
- _PyObject_GC_TRACK(it);
- return (PyObject *)it;
+ if (!PyList_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(listiterobject, &PyListIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = (PyListObject *)seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
}
static void
listiter_dealloc(listiterobject *it)
{
- _PyObject_GC_UNTRACK(it);
- Py_XDECREF(it->it_seq);
- PyObject_GC_Del(it);
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
}
static int
listiter_traverse(listiterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_seq);
- return 0;
+ Py_VISIT(it->it_seq);
+ return 0;
}
static PyObject *
listiter_next(listiterobject *it)
{
- PyListObject *seq;
- PyObject *item;
+ PyListObject *seq;
+ PyObject *item;
- assert(it != NULL);
- seq = it->it_seq;
- if (seq == NULL)
- return NULL;
- assert(PyList_Check(seq));
+ assert(it != NULL);
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+ assert(PyList_Check(seq));
- if (it->it_index < PyList_GET_SIZE(seq)) {
- item = PyList_GET_ITEM(seq, it->it_index);
- ++it->it_index;
- Py_INCREF(item);
- return item;
- }
+ if (it->it_index < PyList_GET_SIZE(seq)) {
+ item = PyList_GET_ITEM(seq, it->it_index);
+ ++it->it_index;
+ Py_INCREF(item);
+ return item;
+ }
- Py_DECREF(seq);
- it->it_seq = NULL;
- return NULL;
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ return NULL;
}
static PyObject *
listiter_len(listiterobject *it)
{
- Py_ssize_t len;
- if (it->it_seq) {
- len = PyList_GET_SIZE(it->it_seq) - it->it_index;
- if (len >= 0)
- return PyLong_FromSsize_t(len);
- }
- return PyLong_FromLong(0);
+ Py_ssize_t len;
+ if (it->it_seq) {
+ len = PyList_GET_SIZE(it->it_seq) - it->it_index;
+ if (len >= 0)
+ return PyLong_FromSsize_t(len);
+ }
+ return PyLong_FromLong(0);
}
/*********************** List Reverse Iterator **************************/
typedef struct {
- PyObject_HEAD
- Py_ssize_t it_index;
- PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ Py_ssize_t it_index;
+ PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listreviterobject;
static PyObject *list_reversed(PyListObject *, PyObject *);
@@ -2751,101 +2751,101 @@ static PyObject *listreviter_next(listreviterobject *);
static PyObject *listreviter_len(listreviterobject *);
static PyMethodDef listreviter_methods[] = {
- {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyListRevIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "list_reverseiterator", /* tp_name */
- sizeof(listreviterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)listreviter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)listreviter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)listreviter_next, /* tp_iternext */
- listreviter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "list_reverseiterator", /* tp_name */
+ sizeof(listreviterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)listreviter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)listreviter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)listreviter_next, /* tp_iternext */
+ listreviter_methods, /* tp_methods */
+ 0,
};
static PyObject *
list_reversed(PyListObject *seq, PyObject *unused)
{
- listreviterobject *it;
+ listreviterobject *it;
- it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
- if (it == NULL)
- return NULL;
- assert(PyList_Check(seq));
- it->it_index = PyList_GET_SIZE(seq) - 1;
- Py_INCREF(seq);
- it->it_seq = seq;
- PyObject_GC_Track(it);
- return (PyObject *)it;
+ it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
+ if (it == NULL)
+ return NULL;
+ assert(PyList_Check(seq));
+ it->it_index = PyList_GET_SIZE(seq) - 1;
+ Py_INCREF(seq);
+ it->it_seq = seq;
+ PyObject_GC_Track(it);
+ return (PyObject *)it;
}
static void
listreviter_dealloc(listreviterobject *it)
{
- PyObject_GC_UnTrack(it);
- Py_XDECREF(it->it_seq);
- PyObject_GC_Del(it);
+ PyObject_GC_UnTrack(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
}
static int
listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_seq);
- return 0;
+ Py_VISIT(it->it_seq);
+ return 0;
}
static PyObject *
listreviter_next(listreviterobject *it)
{
- PyObject *item;
- Py_ssize_t index = it->it_index;
- PyListObject *seq = it->it_seq;
+ PyObject *item;
+ Py_ssize_t index = it->it_index;
+ PyListObject *seq = it->it_seq;
- if (index>=0 && index < PyList_GET_SIZE(seq)) {
- item = PyList_GET_ITEM(seq, index);
- it->it_index--;
- Py_INCREF(item);
- return item;
- }
- it->it_index = -1;
- if (seq != NULL) {
- it->it_seq = NULL;
- Py_DECREF(seq);
- }
- return NULL;
+ if (index>=0 && index < PyList_GET_SIZE(seq)) {
+ item = PyList_GET_ITEM(seq, index);
+ it->it_index--;
+ Py_INCREF(item);
+ return item;
+ }
+ it->it_index = -1;
+ if (seq != NULL) {
+ it->it_seq = NULL;
+ Py_DECREF(seq);
+ }
+ return NULL;
}
static PyObject *
listreviter_len(listreviterobject *it)
{
- Py_ssize_t len = it->it_index + 1;
- if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
- len = 0;
- return PyLong_FromSsize_t(len);
+ Py_ssize_t len = it->it_index + 1;
+ if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
+ len = 0;
+ return PyLong_FromSsize_t(len);
}
diff --git a/Objects/longobject.c b/Objects/longobject.c
index e41d06a..f7c2e3a 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -11,16 +11,16 @@
#include <stddef.h>
#ifndef NSMALLPOSINTS
-#define NSMALLPOSINTS 257
+#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
-#define NSMALLNEGINTS 5
+#define NSMALLNEGINTS 5
#endif
/* convert a PyLong of size 1, 0 or -1 to an sdigit */
-#define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \
- (Py_SIZE(x) == 0 ? (sdigit)0 : \
- (sdigit)(x)->ob_digit[0]))
+#define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \
+ (Py_SIZE(x) == 0 ? (sdigit)0 : \
+ (sdigit)(x)->ob_digit[0]))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
@@ -37,32 +37,32 @@ int quick_int_allocs, quick_neg_int_allocs;
static PyObject *
get_small_int(sdigit ival)
{
- PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
- Py_INCREF(v);
+ PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
+ Py_INCREF(v);
#ifdef COUNT_ALLOCS
- if (ival >= 0)
- quick_int_allocs++;
- else
- quick_neg_int_allocs++;
+ if (ival >= 0)
+ quick_int_allocs++;
+ else
+ quick_neg_int_allocs++;
#endif
- return v;
+ return v;
}
#define CHECK_SMALL_INT(ival) \
- do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
- return get_small_int((sdigit)ival); \
- } while(0)
+ do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
+ return get_small_int((sdigit)ival); \
+ } while(0)
-static PyLongObject *
+static PyLongObject *
maybe_small_long(PyLongObject *v)
{
- if (v && ABS(Py_SIZE(v)) <= 1) {
- sdigit ival = MEDIUM_VALUE(v);
- if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
- Py_DECREF(v);
- return (PyLongObject *)get_small_int(ival);
- }
- }
- return v;
+ if (v && ABS(Py_SIZE(v)) <= 1) {
+ sdigit ival = MEDIUM_VALUE(v);
+ if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
+ Py_DECREF(v);
+ return (PyLongObject *)get_small_int(ival);
+ }
+ }
+ return v;
}
#else
#define CHECK_SMALL_INT(ival)
@@ -72,10 +72,10 @@ maybe_small_long(PyLongObject *v)
/* If a freshly-allocated long is already shared, it must
be a small integer, so negating it must go to PyLong_FromLong */
#define NEGATE(x) \
- do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \
- else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \
- Py_DECREF(x); (x) = (PyLongObject*)tmp; } \
- while(0)
+ do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \
+ else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \
+ Py_DECREF(x); (x) = (PyLongObject*)tmp; } \
+ while(0)
/* For long multiplication, use the O(N**2) school algorithm unless
* both operands contain more than KARATSUBA_CUTOFF digits (this
* being an internal Python long digit, in base BASE).
@@ -96,10 +96,10 @@ maybe_small_long(PyLongObject *v)
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#define SIGCHECK(PyTryBlock) \
- if (--_Py_Ticker < 0) { \
- _Py_Ticker = _Py_CheckInterval; \
- if (PyErr_CheckSignals()) PyTryBlock \
- }
+ if (--_Py_Ticker < 0) { \
+ _Py_Ticker = _Py_CheckInterval; \
+ if (PyErr_CheckSignals()) PyTryBlock \
+ }
/* forward declaration */
static int bits_in_digit(digit d);
@@ -111,68 +111,68 @@ static int bits_in_digit(digit d);
static PyLongObject *
long_normalize(register PyLongObject *v)
{
- Py_ssize_t j = ABS(Py_SIZE(v));
- Py_ssize_t i = j;
-
- while (i > 0 && v->ob_digit[i-1] == 0)
- --i;
- if (i != j)
- Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
- return v;
+ Py_ssize_t j = ABS(Py_SIZE(v));
+ Py_ssize_t i = j;
+
+ while (i > 0 && v->ob_digit[i-1] == 0)
+ --i;
+ if (i != j)
+ Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
+ return v;
}
/* Allocate a new long int object with size digits.
Return NULL and set exception if we run out of memory. */
#define MAX_LONG_DIGITS \
- ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
+ ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
PyLongObject *
_PyLong_New(Py_ssize_t size)
{
- PyLongObject *result;
- /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
- sizeof(digit)*size. Previous incarnations of this code used
- sizeof(PyVarObject) instead of the offsetof, but this risks being
- incorrect in the presence of padding between the PyVarObject header
- and the digits. */
- if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
- PyErr_SetString(PyExc_OverflowError,
- "too many digits in integer");
- return NULL;
- }
- result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) +
- size*sizeof(digit));
- if (!result) {
- PyErr_NoMemory();
- return NULL;
- }
- return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
+ PyLongObject *result;
+ /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
+ sizeof(digit)*size. Previous incarnations of this code used
+ sizeof(PyVarObject) instead of the offsetof, but this risks being
+ incorrect in the presence of padding between the PyVarObject header
+ and the digits. */
+ if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
+ PyErr_SetString(PyExc_OverflowError,
+ "too many digits in integer");
+ return NULL;
+ }
+ result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) +
+ size*sizeof(digit));
+ if (!result) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
}
PyObject *
_PyLong_Copy(PyLongObject *src)
{
- PyLongObject *result;
- Py_ssize_t i;
-
- assert(src != NULL);
- i = Py_SIZE(src);
- if (i < 0)
- i = -(i);
- if (i < 2) {
- sdigit ival = src->ob_digit[0];
- if (Py_SIZE(src) < 0)
- ival = -ival;
- CHECK_SMALL_INT(ival);
- }
- result = _PyLong_New(i);
- if (result != NULL) {
- Py_SIZE(result) = Py_SIZE(src);
- while (--i >= 0)
- result->ob_digit[i] = src->ob_digit[i];
- }
- return (PyObject *)result;
+ PyLongObject *result;
+ Py_ssize_t i;
+
+ assert(src != NULL);
+ i = Py_SIZE(src);
+ if (i < 0)
+ i = -(i);
+ if (i < 2) {
+ sdigit ival = src->ob_digit[0];
+ if (Py_SIZE(src) < 0)
+ ival = -ival;
+ CHECK_SMALL_INT(ival);
+ }
+ result = _PyLong_New(i);
+ if (result != NULL) {
+ Py_SIZE(result) = Py_SIZE(src);
+ while (--i >= 0)
+ result->ob_digit[i] = src->ob_digit[i];
+ }
+ return (PyObject *)result;
}
/* Create a new long int object from a C long int */
@@ -180,68 +180,68 @@ _PyLong_Copy(PyLongObject *src)
PyObject *
PyLong_FromLong(long ival)
{
- PyLongObject *v;
- unsigned long abs_ival;
- unsigned long t; /* unsigned so >> doesn't propagate sign bit */
- int ndigits = 0;
- int sign = 1;
-
- CHECK_SMALL_INT(ival);
-
- if (ival < 0) {
- /* negate: can't write this as abs_ival = -ival since that
- invokes undefined behaviour when ival is LONG_MIN */
- abs_ival = 0U-(unsigned long)ival;
- sign = -1;
- }
- else {
- abs_ival = (unsigned long)ival;
- }
-
- /* Fast path for single-digit ints */
- if (!(abs_ival >> PyLong_SHIFT)) {
- v = _PyLong_New(1);
- if (v) {
- Py_SIZE(v) = sign;
- v->ob_digit[0] = Py_SAFE_DOWNCAST(
- abs_ival, unsigned long, digit);
- }
- return (PyObject*)v;
- }
+ PyLongObject *v;
+ unsigned long abs_ival;
+ unsigned long t; /* unsigned so >> doesn't propagate sign bit */
+ int ndigits = 0;
+ int sign = 1;
+
+ CHECK_SMALL_INT(ival);
+
+ if (ival < 0) {
+ /* negate: can't write this as abs_ival = -ival since that
+ invokes undefined behaviour when ival is LONG_MIN */
+ abs_ival = 0U-(unsigned long)ival;
+ sign = -1;
+ }
+ else {
+ abs_ival = (unsigned long)ival;
+ }
+
+ /* Fast path for single-digit ints */
+ if (!(abs_ival >> PyLong_SHIFT)) {
+ v = _PyLong_New(1);
+ if (v) {
+ Py_SIZE(v) = sign;
+ v->ob_digit[0] = Py_SAFE_DOWNCAST(
+ abs_ival, unsigned long, digit);
+ }
+ return (PyObject*)v;
+ }
#if PyLong_SHIFT==15
- /* 2 digits */
- if (!(abs_ival >> 2*PyLong_SHIFT)) {
- v = _PyLong_New(2);
- if (v) {
- Py_SIZE(v) = 2*sign;
- v->ob_digit[0] = Py_SAFE_DOWNCAST(
- abs_ival & PyLong_MASK, unsigned long, digit);
- v->ob_digit[1] = Py_SAFE_DOWNCAST(
- abs_ival >> PyLong_SHIFT, unsigned long, digit);
- }
- return (PyObject*)v;
- }
+ /* 2 digits */
+ if (!(abs_ival >> 2*PyLong_SHIFT)) {
+ v = _PyLong_New(2);
+ if (v) {
+ Py_SIZE(v) = 2*sign;
+ v->ob_digit[0] = Py_SAFE_DOWNCAST(
+ abs_ival & PyLong_MASK, unsigned long, digit);
+ v->ob_digit[1] = Py_SAFE_DOWNCAST(
+ abs_ival >> PyLong_SHIFT, unsigned long, digit);
+ }
+ return (PyObject*)v;
+ }
#endif
- /* Larger numbers: loop to determine number of digits */
- t = abs_ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = ndigits*sign;
- t = abs_ival;
- while (t) {
- *p++ = Py_SAFE_DOWNCAST(
- t & PyLong_MASK, unsigned long, digit);
- t >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ /* Larger numbers: loop to determine number of digits */
+ t = abs_ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = ndigits*sign;
+ t = abs_ival;
+ while (t) {
+ *p++ = Py_SAFE_DOWNCAST(
+ t & PyLong_MASK, unsigned long, digit);
+ t >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Create a new long int object from a C unsigned long int */
@@ -249,28 +249,28 @@ PyLong_FromLong(long ival)
PyObject *
PyLong_FromUnsignedLong(unsigned long ival)
{
- PyLongObject *v;
- unsigned long t;
- int ndigits = 0;
-
- if (ival < PyLong_BASE)
- return PyLong_FromLong(ival);
- /* Count the number of Python digits. */
- t = (unsigned long)ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = ndigits;
- while (ival) {
- *p++ = (digit)(ival & PyLong_MASK);
- ival >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ PyLongObject *v;
+ unsigned long t;
+ int ndigits = 0;
+
+ if (ival < PyLong_BASE)
+ return PyLong_FromLong(ival);
+ /* Count the number of Python digits. */
+ t = (unsigned long)ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = ndigits;
+ while (ival) {
+ *p++ = (digit)(ival & PyLong_MASK);
+ ival >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Create a new long int object from a C double */
@@ -278,41 +278,41 @@ PyLong_FromUnsignedLong(unsigned long ival)
PyObject *
PyLong_FromDouble(double dval)
{
- PyLongObject *v;
- double frac;
- int i, ndig, expo, neg;
- neg = 0;
- if (Py_IS_INFINITY(dval)) {
- PyErr_SetString(PyExc_OverflowError,
- "cannot convert float infinity to integer");
- return NULL;
- }
- if (Py_IS_NAN(dval)) {
- PyErr_SetString(PyExc_ValueError,
- "cannot convert float NaN to integer");
- return NULL;
- }
- if (dval < 0.0) {
- neg = 1;
- dval = -dval;
- }
- frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
- if (expo <= 0)
- return PyLong_FromLong(0L);
- ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
- v = _PyLong_New(ndig);
- if (v == NULL)
- return NULL;
- frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
- for (i = ndig; --i >= 0; ) {
- digit bits = (digit)frac;
- v->ob_digit[i] = bits;
- frac = frac - (double)bits;
- frac = ldexp(frac, PyLong_SHIFT);
- }
- if (neg)
- Py_SIZE(v) = -(Py_SIZE(v));
- return (PyObject *)v;
+ PyLongObject *v;
+ double frac;
+ int i, ndig, expo, neg;
+ neg = 0;
+ if (Py_IS_INFINITY(dval)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "cannot convert float infinity to integer");
+ return NULL;
+ }
+ if (Py_IS_NAN(dval)) {
+ PyErr_SetString(PyExc_ValueError,
+ "cannot convert float NaN to integer");
+ return NULL;
+ }
+ if (dval < 0.0) {
+ neg = 1;
+ dval = -dval;
+ }
+ frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
+ if (expo <= 0)
+ return PyLong_FromLong(0L);
+ ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
+ v = _PyLong_New(ndig);
+ if (v == NULL)
+ return NULL;
+ frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
+ for (i = ndig; --i >= 0; ) {
+ digit bits = (digit)frac;
+ v->ob_digit[i] = bits;
+ frac = frac - (double)bits;
+ frac = ldexp(frac, PyLong_SHIFT);
+ }
+ if (neg)
+ Py_SIZE(v) = -(Py_SIZE(v));
+ return (PyObject *)v;
}
/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
@@ -324,8 +324,8 @@ PyLong_FromDouble(double dval)
* However, some other compilers warn about applying unary minus to an
* unsigned operand. Hence the weird "0-".
*/
-#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
-#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
+#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
+#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
/* Get a C long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
@@ -333,101 +333,101 @@ PyLong_FromDouble(double dval)
long
PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
{
- /* This version by Tim Peters */
- register PyLongObject *v;
- unsigned long x, prev;
- long res;
- Py_ssize_t i;
- int sign;
- int do_decref = 0; /* if nb_int was called */
-
- *overflow = 0;
- if (vv == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
-
- if (!PyLong_Check(vv)) {
- PyNumberMethods *nb;
- if ((nb = vv->ob_type->tp_as_number) == NULL ||
- nb->nb_int == NULL) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return -1;
- }
- vv = (*nb->nb_int) (vv);
- if (vv == NULL)
- return -1;
- do_decref = 1;
- if (!PyLong_Check(vv)) {
- Py_DECREF(vv);
- PyErr_SetString(PyExc_TypeError,
- "nb_int should return int object");
- return -1;
- }
- }
-
- res = -1;
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
-
- switch (i) {
- case -1:
- res = -(sdigit)v->ob_digit[0];
- break;
- case 0:
- res = 0;
- break;
- case 1:
- res = v->ob_digit[0];
- break;
- default:
- sign = 1;
- x = 0;
- if (i < 0) {
- sign = -1;
- i = -(i);
- }
- while (--i >= 0) {
- prev = x;
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- if ((x >> PyLong_SHIFT) != prev) {
- *overflow = Py_SIZE(v) > 0 ? 1 : -1;
- goto exit;
- }
- }
- /* Haven't lost any bits, but casting to long requires extra care
- * (see comment above).
- */
- if (x <= (unsigned long)LONG_MAX) {
- res = (long)x * sign;
- }
- else if (sign < 0 && x == PY_ABS_LONG_MIN) {
- res = LONG_MIN;
- }
- else {
- *overflow = Py_SIZE(v) > 0 ? 1 : -1;
- /* res is already set to -1 */
- }
- }
+ /* This version by Tim Peters */
+ register PyLongObject *v;
+ unsigned long x, prev;
+ long res;
+ Py_ssize_t i;
+ int sign;
+ int do_decref = 0; /* if nb_int was called */
+
+ *overflow = 0;
+ if (vv == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+
+ if (!PyLong_Check(vv)) {
+ PyNumberMethods *nb;
+ if ((nb = vv->ob_type->tp_as_number) == NULL ||
+ nb->nb_int == NULL) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return -1;
+ }
+ vv = (*nb->nb_int) (vv);
+ if (vv == NULL)
+ return -1;
+ do_decref = 1;
+ if (!PyLong_Check(vv)) {
+ Py_DECREF(vv);
+ PyErr_SetString(PyExc_TypeError,
+ "nb_int should return int object");
+ return -1;
+ }
+ }
+
+ res = -1;
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+
+ switch (i) {
+ case -1:
+ res = -(sdigit)v->ob_digit[0];
+ break;
+ case 0:
+ res = 0;
+ break;
+ case 1:
+ res = v->ob_digit[0];
+ break;
+ default:
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -(i);
+ }
+ while (--i >= 0) {
+ prev = x;
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ if ((x >> PyLong_SHIFT) != prev) {
+ *overflow = Py_SIZE(v) > 0 ? 1 : -1;
+ goto exit;
+ }
+ }
+ /* Haven't lost any bits, but casting to long requires extra care
+ * (see comment above).
+ */
+ if (x <= (unsigned long)LONG_MAX) {
+ res = (long)x * sign;
+ }
+ else if (sign < 0 && x == PY_ABS_LONG_MIN) {
+ res = LONG_MIN;
+ }
+ else {
+ *overflow = Py_SIZE(v) > 0 ? 1 : -1;
+ /* res is already set to -1 */
+ }
+ }
exit:
- if (do_decref) {
- Py_DECREF(vv);
- }
- return res;
+ if (do_decref) {
+ Py_DECREF(vv);
+ }
+ return res;
}
-long
+long
PyLong_AsLong(PyObject *obj)
{
- int overflow;
- long result = PyLong_AsLongAndOverflow(obj, &overflow);
- if (overflow) {
- /* XXX: could be cute and give a different
- message for overflow == -1 */
- PyErr_SetString(PyExc_OverflowError,
- "Python int too large to convert to C long");
- }
- return result;
+ int overflow;
+ long result = PyLong_AsLongAndOverflow(obj, &overflow);
+ if (overflow) {
+ /* XXX: could be cute and give a different
+ message for overflow == -1 */
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C long");
+ }
+ return result;
}
/* Get a Py_ssize_t from a long int object.
@@ -435,54 +435,54 @@ PyLong_AsLong(PyObject *obj)
Py_ssize_t
PyLong_AsSsize_t(PyObject *vv) {
- register PyLongObject *v;
- size_t x, prev;
- Py_ssize_t i;
- int sign;
-
- if (vv == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (!PyLong_Check(vv)) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return -1;
- }
-
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
- switch (i) {
- case -1: return -(sdigit)v->ob_digit[0];
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- sign = 1;
- x = 0;
- if (i < 0) {
- sign = -1;
- i = -(i);
- }
- while (--i >= 0) {
- prev = x;
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- if ((x >> PyLong_SHIFT) != prev)
- goto overflow;
- }
- /* Haven't lost any bits, but casting to a signed type requires
- * extra care (see comment above).
- */
- if (x <= (size_t)PY_SSIZE_T_MAX) {
- return (Py_ssize_t)x * sign;
- }
- else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
- return PY_SSIZE_T_MIN;
- }
- /* else overflow */
+ register PyLongObject *v;
+ size_t x, prev;
+ Py_ssize_t i;
+ int sign;
+
+ if (vv == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (!PyLong_Check(vv)) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return -1;
+ }
+
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+ switch (i) {
+ case -1: return -(sdigit)v->ob_digit[0];
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -(i);
+ }
+ while (--i >= 0) {
+ prev = x;
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ if ((x >> PyLong_SHIFT) != prev)
+ goto overflow;
+ }
+ /* Haven't lost any bits, but casting to a signed type requires
+ * extra care (see comment above).
+ */
+ if (x <= (size_t)PY_SSIZE_T_MAX) {
+ return (Py_ssize_t)x * sign;
+ }
+ else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
+ return PY_SSIZE_T_MIN;
+ }
+ /* else overflow */
overflow:
- PyErr_SetString(PyExc_OverflowError,
- "Python int too large to convert to C ssize_t");
- return -1;
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C ssize_t");
+ return -1;
}
/* Get a C unsigned long int from a long int object.
@@ -491,41 +491,41 @@ PyLong_AsSsize_t(PyObject *vv) {
unsigned long
PyLong_AsUnsignedLong(PyObject *vv)
{
- register PyLongObject *v;
- unsigned long x, prev;
- Py_ssize_t i;
-
- if (vv == NULL) {
- PyErr_BadInternalCall();
- return (unsigned long)-1;
- }
- if (!PyLong_Check(vv)) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return (unsigned long)-1;
- }
-
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
- x = 0;
- if (i < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "can't convert negative value to unsigned int");
- return (unsigned long) -1;
- }
- switch (i) {
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- while (--i >= 0) {
- prev = x;
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- if ((x >> PyLong_SHIFT) != prev) {
- PyErr_SetString(PyExc_OverflowError,
- "python int too large to convert to C unsigned long");
- return (unsigned long) -1;
- }
- }
- return x;
+ register PyLongObject *v;
+ unsigned long x, prev;
+ Py_ssize_t i;
+
+ if (vv == NULL) {
+ PyErr_BadInternalCall();
+ return (unsigned long)-1;
+ }
+ if (!PyLong_Check(vv)) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return (unsigned long)-1;
+ }
+
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+ x = 0;
+ if (i < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative value to unsigned int");
+ return (unsigned long) -1;
+ }
+ switch (i) {
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ while (--i >= 0) {
+ prev = x;
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ if ((x >> PyLong_SHIFT) != prev) {
+ PyErr_SetString(PyExc_OverflowError,
+ "python int too large to convert to C unsigned long");
+ return (unsigned long) -1;
+ }
+ }
+ return x;
}
/* Get a C unsigned long int from a long int object.
@@ -534,41 +534,41 @@ PyLong_AsUnsignedLong(PyObject *vv)
size_t
PyLong_AsSize_t(PyObject *vv)
{
- register PyLongObject *v;
- size_t x, prev;
- Py_ssize_t i;
-
- if (vv == NULL) {
- PyErr_BadInternalCall();
- return (size_t) -1;
- }
- if (!PyLong_Check(vv)) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return (size_t)-1;
- }
-
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
- x = 0;
- if (i < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "can't convert negative value to size_t");
- return (size_t) -1;
- }
- switch (i) {
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- while (--i >= 0) {
- prev = x;
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- if ((x >> PyLong_SHIFT) != prev) {
- PyErr_SetString(PyExc_OverflowError,
- "Python int too large to convert to C size_t");
- return (unsigned long) -1;
- }
- }
- return x;
+ register PyLongObject *v;
+ size_t x, prev;
+ Py_ssize_t i;
+
+ if (vv == NULL) {
+ PyErr_BadInternalCall();
+ return (size_t) -1;
+ }
+ if (!PyLong_Check(vv)) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return (size_t)-1;
+ }
+
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+ x = 0;
+ if (i < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative value to size_t");
+ return (size_t) -1;
+ }
+ switch (i) {
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ while (--i >= 0) {
+ prev = x;
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ if ((x >> PyLong_SHIFT) != prev) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C size_t");
+ return (unsigned long) -1;
+ }
+ }
+ return x;
}
/* Get a C unsigned long int from a long int object, ignoring the high bits.
@@ -577,354 +577,354 @@ PyLong_AsSize_t(PyObject *vv)
static unsigned long
_PyLong_AsUnsignedLongMask(PyObject *vv)
{
- register PyLongObject *v;
- unsigned long x;
- Py_ssize_t i;
- int sign;
-
- if (vv == NULL || !PyLong_Check(vv)) {
- PyErr_BadInternalCall();
- return (unsigned long) -1;
- }
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
- switch (i) {
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- sign = 1;
- x = 0;
- if (i < 0) {
- sign = -1;
- i = -i;
- }
- while (--i >= 0) {
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- }
- return x * sign;
+ register PyLongObject *v;
+ unsigned long x;
+ Py_ssize_t i;
+ int sign;
+
+ if (vv == NULL || !PyLong_Check(vv)) {
+ PyErr_BadInternalCall();
+ return (unsigned long) -1;
+ }
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+ switch (i) {
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -i;
+ }
+ while (--i >= 0) {
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ }
+ return x * sign;
}
unsigned long
PyLong_AsUnsignedLongMask(register PyObject *op)
{
- PyNumberMethods *nb;
- PyLongObject *lo;
- unsigned long val;
-
- if (op && PyLong_Check(op))
- return _PyLong_AsUnsignedLongMask(op);
-
- if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
- nb->nb_int == NULL) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return (unsigned long)-1;
- }
-
- lo = (PyLongObject*) (*nb->nb_int) (op);
- if (lo == NULL)
- return (unsigned long)-1;
- if (PyLong_Check(lo)) {
- val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
- Py_DECREF(lo);
- if (PyErr_Occurred())
- return (unsigned long)-1;
- return val;
- }
- else
- {
- Py_DECREF(lo);
- PyErr_SetString(PyExc_TypeError,
- "nb_int should return int object");
- return (unsigned long)-1;
- }
+ PyNumberMethods *nb;
+ PyLongObject *lo;
+ unsigned long val;
+
+ if (op && PyLong_Check(op))
+ return _PyLong_AsUnsignedLongMask(op);
+
+ if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
+ nb->nb_int == NULL) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return (unsigned long)-1;
+ }
+
+ lo = (PyLongObject*) (*nb->nb_int) (op);
+ if (lo == NULL)
+ return (unsigned long)-1;
+ if (PyLong_Check(lo)) {
+ val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
+ Py_DECREF(lo);
+ if (PyErr_Occurred())
+ return (unsigned long)-1;
+ return val;
+ }
+ else
+ {
+ Py_DECREF(lo);
+ PyErr_SetString(PyExc_TypeError,
+ "nb_int should return int object");
+ return (unsigned long)-1;
+ }
}
int
_PyLong_Sign(PyObject *vv)
{
- PyLongObject *v = (PyLongObject *)vv;
+ PyLongObject *v = (PyLongObject *)vv;
- assert(v != NULL);
- assert(PyLong_Check(v));
+ assert(v != NULL);
+ assert(PyLong_Check(v));
- return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
+ return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
}
size_t
_PyLong_NumBits(PyObject *vv)
{
- PyLongObject *v = (PyLongObject *)vv;
- size_t result = 0;
- Py_ssize_t ndigits;
-
- assert(v != NULL);
- assert(PyLong_Check(v));
- ndigits = ABS(Py_SIZE(v));
- assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
- if (ndigits > 0) {
- digit msd = v->ob_digit[ndigits - 1];
-
- result = (ndigits - 1) * PyLong_SHIFT;
- if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
- goto Overflow;
- do {
- ++result;
- if (result == 0)
- goto Overflow;
- msd >>= 1;
- } while (msd);
- }
- return result;
+ PyLongObject *v = (PyLongObject *)vv;
+ size_t result = 0;
+ Py_ssize_t ndigits;
+
+ assert(v != NULL);
+ assert(PyLong_Check(v));
+ ndigits = ABS(Py_SIZE(v));
+ assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
+ if (ndigits > 0) {
+ digit msd = v->ob_digit[ndigits - 1];
+
+ result = (ndigits - 1) * PyLong_SHIFT;
+ if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
+ goto Overflow;
+ do {
+ ++result;
+ if (result == 0)
+ goto Overflow;
+ msd >>= 1;
+ } while (msd);
+ }
+ return result;
Overflow:
- PyErr_SetString(PyExc_OverflowError, "int has too many bits "
- "to express in a platform size_t");
- return (size_t)-1;
+ PyErr_SetString(PyExc_OverflowError, "int has too many bits "
+ "to express in a platform size_t");
+ return (size_t)-1;
}
PyObject *
_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
- int little_endian, int is_signed)
+ int little_endian, int is_signed)
{
- const unsigned char* pstartbyte;/* LSB of bytes */
- int incr; /* direction to move pstartbyte */
- const unsigned char* pendbyte; /* MSB of bytes */
- size_t numsignificantbytes; /* number of bytes that matter */
- Py_ssize_t ndigits; /* number of Python long digits */
- PyLongObject* v; /* result */
- Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
-
- if (n == 0)
- return PyLong_FromLong(0L);
-
- if (little_endian) {
- pstartbyte = bytes;
- pendbyte = bytes + n - 1;
- incr = 1;
- }
- else {
- pstartbyte = bytes + n - 1;
- pendbyte = bytes;
- incr = -1;
- }
-
- if (is_signed)
- is_signed = *pendbyte >= 0x80;
-
- /* Compute numsignificantbytes. This consists of finding the most
- significant byte. Leading 0 bytes are insignficant if the number
- is positive, and leading 0xff bytes if negative. */
- {
- size_t i;
- const unsigned char* p = pendbyte;
- const int pincr = -incr; /* search MSB to LSB */
- const unsigned char insignficant = is_signed ? 0xff : 0x00;
-
- for (i = 0; i < n; ++i, p += pincr) {
- if (*p != insignficant)
- break;
- }
- numsignificantbytes = n - i;
- /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
- actually has 2 significant bytes. OTOH, 0xff0001 ==
- -0x00ffff, so we wouldn't *need* to bump it there; but we
- do for 0xffff = -0x0001. To be safe without bothering to
- check every case, bump it regardless. */
- if (is_signed && numsignificantbytes < n)
- ++numsignificantbytes;
- }
-
- /* How many Python long digits do we need? We have
- 8*numsignificantbytes bits, and each Python long digit has
- PyLong_SHIFT bits, so it's the ceiling of the quotient. */
- /* catch overflow before it happens */
- if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
- PyErr_SetString(PyExc_OverflowError,
- "byte array too long to convert to int");
- return NULL;
- }
- ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
- v = _PyLong_New(ndigits);
- if (v == NULL)
- return NULL;
-
- /* Copy the bits over. The tricky parts are computing 2's-comp on
- the fly for signed numbers, and dealing with the mismatch between
- 8-bit bytes and (probably) 15-bit Python digits.*/
- {
- size_t i;
- twodigits carry = 1; /* for 2's-comp calculation */
- twodigits accum = 0; /* sliding register */
- unsigned int accumbits = 0; /* number of bits in accum */
- const unsigned char* p = pstartbyte;
-
- for (i = 0; i < numsignificantbytes; ++i, p += incr) {
- twodigits thisbyte = *p;
- /* Compute correction for 2's comp, if needed. */
- if (is_signed) {
- thisbyte = (0xff ^ thisbyte) + carry;
- carry = thisbyte >> 8;
- thisbyte &= 0xff;
- }
- /* Because we're going LSB to MSB, thisbyte is
- more significant than what's already in accum,
- so needs to be prepended to accum. */
- accum |= (twodigits)thisbyte << accumbits;
- accumbits += 8;
- if (accumbits >= PyLong_SHIFT) {
- /* There's enough to fill a Python digit. */
- assert(idigit < ndigits);
- v->ob_digit[idigit] = (digit)(accum &
- PyLong_MASK);
- ++idigit;
- accum >>= PyLong_SHIFT;
- accumbits -= PyLong_SHIFT;
- assert(accumbits < PyLong_SHIFT);
- }
- }
- assert(accumbits < PyLong_SHIFT);
- if (accumbits) {
- assert(idigit < ndigits);
- v->ob_digit[idigit] = (digit)accum;
- ++idigit;
- }
- }
-
- Py_SIZE(v) = is_signed ? -idigit : idigit;
- return (PyObject *)long_normalize(v);
+ const unsigned char* pstartbyte;/* LSB of bytes */
+ int incr; /* direction to move pstartbyte */
+ const unsigned char* pendbyte; /* MSB of bytes */
+ size_t numsignificantbytes; /* number of bytes that matter */
+ Py_ssize_t ndigits; /* number of Python long digits */
+ PyLongObject* v; /* result */
+ Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
+
+ if (n == 0)
+ return PyLong_FromLong(0L);
+
+ if (little_endian) {
+ pstartbyte = bytes;
+ pendbyte = bytes + n - 1;
+ incr = 1;
+ }
+ else {
+ pstartbyte = bytes + n - 1;
+ pendbyte = bytes;
+ incr = -1;
+ }
+
+ if (is_signed)
+ is_signed = *pendbyte >= 0x80;
+
+ /* Compute numsignificantbytes. This consists of finding the most
+ significant byte. Leading 0 bytes are insignficant if the number
+ is positive, and leading 0xff bytes if negative. */
+ {
+ size_t i;
+ const unsigned char* p = pendbyte;
+ const int pincr = -incr; /* search MSB to LSB */
+ const unsigned char insignficant = is_signed ? 0xff : 0x00;
+
+ for (i = 0; i < n; ++i, p += pincr) {
+ if (*p != insignficant)
+ break;
+ }
+ numsignificantbytes = n - i;
+ /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
+ actually has 2 significant bytes. OTOH, 0xff0001 ==
+ -0x00ffff, so we wouldn't *need* to bump it there; but we
+ do for 0xffff = -0x0001. To be safe without bothering to
+ check every case, bump it regardless. */
+ if (is_signed && numsignificantbytes < n)
+ ++numsignificantbytes;
+ }
+
+ /* How many Python long digits do we need? We have
+ 8*numsignificantbytes bits, and each Python long digit has
+ PyLong_SHIFT bits, so it's the ceiling of the quotient. */
+ /* catch overflow before it happens */
+ if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
+ PyErr_SetString(PyExc_OverflowError,
+ "byte array too long to convert to int");
+ return NULL;
+ }
+ ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
+ v = _PyLong_New(ndigits);
+ if (v == NULL)
+ return NULL;
+
+ /* Copy the bits over. The tricky parts are computing 2's-comp on
+ the fly for signed numbers, and dealing with the mismatch between
+ 8-bit bytes and (probably) 15-bit Python digits.*/
+ {
+ size_t i;
+ twodigits carry = 1; /* for 2's-comp calculation */
+ twodigits accum = 0; /* sliding register */
+ unsigned int accumbits = 0; /* number of bits in accum */
+ const unsigned char* p = pstartbyte;
+
+ for (i = 0; i < numsignificantbytes; ++i, p += incr) {
+ twodigits thisbyte = *p;
+ /* Compute correction for 2's comp, if needed. */
+ if (is_signed) {
+ thisbyte = (0xff ^ thisbyte) + carry;
+ carry = thisbyte >> 8;
+ thisbyte &= 0xff;
+ }
+ /* Because we're going LSB to MSB, thisbyte is
+ more significant than what's already in accum,
+ so needs to be prepended to accum. */
+ accum |= (twodigits)thisbyte << accumbits;
+ accumbits += 8;
+ if (accumbits >= PyLong_SHIFT) {
+ /* There's enough to fill a Python digit. */
+ assert(idigit < ndigits);
+ v->ob_digit[idigit] = (digit)(accum &
+ PyLong_MASK);
+ ++idigit;
+ accum >>= PyLong_SHIFT;
+ accumbits -= PyLong_SHIFT;
+ assert(accumbits < PyLong_SHIFT);
+ }
+ }
+ assert(accumbits < PyLong_SHIFT);
+ if (accumbits) {
+ assert(idigit < ndigits);
+ v->ob_digit[idigit] = (digit)accum;
+ ++idigit;
+ }
+ }
+
+ Py_SIZE(v) = is_signed ? -idigit : idigit;
+ return (PyObject *)long_normalize(v);
}
int
_PyLong_AsByteArray(PyLongObject* v,
- unsigned char* bytes, size_t n,
- int little_endian, int is_signed)
+ unsigned char* bytes, size_t n,
+ int little_endian, int is_signed)
{
- Py_ssize_t i; /* index into v->ob_digit */
- Py_ssize_t ndigits; /* |v->ob_size| */
- twodigits accum; /* sliding register */
- unsigned int accumbits; /* # bits in accum */
- int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
- digit carry; /* for computing 2's-comp */
- size_t j; /* # bytes filled */
- unsigned char* p; /* pointer to next byte in bytes */
- int pincr; /* direction to move p */
-
- assert(v != NULL && PyLong_Check(v));
-
- if (Py_SIZE(v) < 0) {
- ndigits = -(Py_SIZE(v));
- if (!is_signed) {
- PyErr_SetString(PyExc_OverflowError,
- "can't convert negative int to unsigned");
- return -1;
- }
- do_twos_comp = 1;
- }
- else {
- ndigits = Py_SIZE(v);
- do_twos_comp = 0;
- }
-
- if (little_endian) {
- p = bytes;
- pincr = 1;
- }
- else {
- p = bytes + n - 1;
- pincr = -1;
- }
-
- /* Copy over all the Python digits.
- It's crucial that every Python digit except for the MSD contribute
- exactly PyLong_SHIFT bits to the total, so first assert that the long is
- normalized. */
- assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
- j = 0;
- accum = 0;
- accumbits = 0;
- carry = do_twos_comp ? 1 : 0;
- for (i = 0; i < ndigits; ++i) {
- digit thisdigit = v->ob_digit[i];
- if (do_twos_comp) {
- thisdigit = (thisdigit ^ PyLong_MASK) + carry;
- carry = thisdigit >> PyLong_SHIFT;
- thisdigit &= PyLong_MASK;
- }
- /* Because we're going LSB to MSB, thisdigit is more
- significant than what's already in accum, so needs to be
- prepended to accum. */
- accum |= (twodigits)thisdigit << accumbits;
-
- /* The most-significant digit may be (probably is) at least
- partly empty. */
- if (i == ndigits - 1) {
- /* Count # of sign bits -- they needn't be stored,
- * although for signed conversion we need later to
- * make sure at least one sign bit gets stored. */
- digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
- thisdigit;
- while (s != 0) {
- s >>= 1;
- accumbits++;
- }
- }
- else
- accumbits += PyLong_SHIFT;
-
- /* Store as many bytes as possible. */
- while (accumbits >= 8) {
- if (j >= n)
- goto Overflow;
- ++j;
- *p = (unsigned char)(accum & 0xff);
- p += pincr;
- accumbits -= 8;
- accum >>= 8;
- }
- }
-
- /* Store the straggler (if any). */
- assert(accumbits < 8);
- assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
- if (accumbits > 0) {
- if (j >= n)
- goto Overflow;
- ++j;
- if (do_twos_comp) {
- /* Fill leading bits of the byte with sign bits
- (appropriately pretending that the long had an
- infinite supply of sign bits). */
- accum |= (~(twodigits)0) << accumbits;
- }
- *p = (unsigned char)(accum & 0xff);
- p += pincr;
- }
- else if (j == n && n > 0 && is_signed) {
- /* The main loop filled the byte array exactly, so the code
- just above didn't get to ensure there's a sign bit, and the
- loop below wouldn't add one either. Make sure a sign bit
- exists. */
- unsigned char msb = *(p - pincr);
- int sign_bit_set = msb >= 0x80;
- assert(accumbits == 0);
- if (sign_bit_set == do_twos_comp)
- return 0;
- else
- goto Overflow;
- }
-
- /* Fill remaining bytes with copies of the sign bit. */
- {
- unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
- for ( ; j < n; ++j, p += pincr)
- *p = signbyte;
- }
-
- return 0;
+ Py_ssize_t i; /* index into v->ob_digit */
+ Py_ssize_t ndigits; /* |v->ob_size| */
+ twodigits accum; /* sliding register */
+ unsigned int accumbits; /* # bits in accum */
+ int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
+ digit carry; /* for computing 2's-comp */
+ size_t j; /* # bytes filled */
+ unsigned char* p; /* pointer to next byte in bytes */
+ int pincr; /* direction to move p */
+
+ assert(v != NULL && PyLong_Check(v));
+
+ if (Py_SIZE(v) < 0) {
+ ndigits = -(Py_SIZE(v));
+ if (!is_signed) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative int to unsigned");
+ return -1;
+ }
+ do_twos_comp = 1;
+ }
+ else {
+ ndigits = Py_SIZE(v);
+ do_twos_comp = 0;
+ }
+
+ if (little_endian) {
+ p = bytes;
+ pincr = 1;
+ }
+ else {
+ p = bytes + n - 1;
+ pincr = -1;
+ }
+
+ /* Copy over all the Python digits.
+ It's crucial that every Python digit except for the MSD contribute
+ exactly PyLong_SHIFT bits to the total, so first assert that the long is
+ normalized. */
+ assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
+ j = 0;
+ accum = 0;
+ accumbits = 0;
+ carry = do_twos_comp ? 1 : 0;
+ for (i = 0; i < ndigits; ++i) {
+ digit thisdigit = v->ob_digit[i];
+ if (do_twos_comp) {
+ thisdigit = (thisdigit ^ PyLong_MASK) + carry;
+ carry = thisdigit >> PyLong_SHIFT;
+ thisdigit &= PyLong_MASK;
+ }
+ /* Because we're going LSB to MSB, thisdigit is more
+ significant than what's already in accum, so needs to be
+ prepended to accum. */
+ accum |= (twodigits)thisdigit << accumbits;
+
+ /* The most-significant digit may be (probably is) at least
+ partly empty. */
+ if (i == ndigits - 1) {
+ /* Count # of sign bits -- they needn't be stored,
+ * although for signed conversion we need later to
+ * make sure at least one sign bit gets stored. */
+ digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
+ thisdigit;
+ while (s != 0) {
+ s >>= 1;
+ accumbits++;
+ }
+ }
+ else
+ accumbits += PyLong_SHIFT;
+
+ /* Store as many bytes as possible. */
+ while (accumbits >= 8) {
+ if (j >= n)
+ goto Overflow;
+ ++j;
+ *p = (unsigned char)(accum & 0xff);
+ p += pincr;
+ accumbits -= 8;
+ accum >>= 8;
+ }
+ }
+
+ /* Store the straggler (if any). */
+ assert(accumbits < 8);
+ assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
+ if (accumbits > 0) {
+ if (j >= n)
+ goto Overflow;
+ ++j;
+ if (do_twos_comp) {
+ /* Fill leading bits of the byte with sign bits
+ (appropriately pretending that the long had an
+ infinite supply of sign bits). */
+ accum |= (~(twodigits)0) << accumbits;
+ }
+ *p = (unsigned char)(accum & 0xff);
+ p += pincr;
+ }
+ else if (j == n && n > 0 && is_signed) {
+ /* The main loop filled the byte array exactly, so the code
+ just above didn't get to ensure there's a sign bit, and the
+ loop below wouldn't add one either. Make sure a sign bit
+ exists. */
+ unsigned char msb = *(p - pincr);
+ int sign_bit_set = msb >= 0x80;
+ assert(accumbits == 0);
+ if (sign_bit_set == do_twos_comp)
+ return 0;
+ else
+ goto Overflow;
+ }
+
+ /* Fill remaining bytes with copies of the sign bit. */
+ {
+ unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
+ for ( ; j < n; ++j, p += pincr)
+ *p = signbyte;
+ }
+
+ return 0;
Overflow:
- PyErr_SetString(PyExc_OverflowError, "int too big to convert");
- return -1;
+ PyErr_SetString(PyExc_OverflowError, "int too big to convert");
+ return -1;
}
@@ -942,42 +942,42 @@ _PyLong_AsScaledDouble(PyObject *vv, int *exponent)
least one round bit to stand in for the ignored least-significant bits.
*/
#define NBITS_WANTED 57
- PyLongObject *v;
- double x;
- const double multiplier = (double)(1L << PyLong_SHIFT);
- Py_ssize_t i;
- int sign;
- int nbitsneeded;
-
- if (vv == NULL || !PyLong_Check(vv)) {
- PyErr_BadInternalCall();
- return -1;
- }
- v = (PyLongObject *)vv;
- i = Py_SIZE(v);
- sign = 1;
- if (i < 0) {
- sign = -1;
- i = -(i);
- }
- else if (i == 0) {
- *exponent = 0;
- return 0.0;
- }
- --i;
- x = (double)v->ob_digit[i];
- nbitsneeded = NBITS_WANTED - 1;
- /* Invariant: i Python digits remain unaccounted for. */
- while (i > 0 && nbitsneeded > 0) {
- --i;
- x = x * multiplier + (double)v->ob_digit[i];
- nbitsneeded -= PyLong_SHIFT;
- }
- /* There are i digits we didn't shift in. Pretending they're all
- zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
- *exponent = i;
- assert(x > 0.0);
- return x * sign;
+ PyLongObject *v;
+ double x;
+ const double multiplier = (double)(1L << PyLong_SHIFT);
+ Py_ssize_t i;
+ int sign;
+ int nbitsneeded;
+
+ if (vv == NULL || !PyLong_Check(vv)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ v = (PyLongObject *)vv;
+ i = Py_SIZE(v);
+ sign = 1;
+ if (i < 0) {
+ sign = -1;
+ i = -(i);
+ }
+ else if (i == 0) {
+ *exponent = 0;
+ return 0.0;
+ }
+ --i;
+ x = (double)v->ob_digit[i];
+ nbitsneeded = NBITS_WANTED - 1;
+ /* Invariant: i Python digits remain unaccounted for. */
+ while (i > 0 && nbitsneeded > 0) {
+ --i;
+ x = x * multiplier + (double)v->ob_digit[i];
+ nbitsneeded -= PyLong_SHIFT;
+ }
+ /* There are i digits we didn't shift in. Pretending they're all
+ zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
+ *exponent = i;
+ assert(x > 0.0);
+ return x * sign;
#undef NBITS_WANTED
}
@@ -987,163 +987,163 @@ _PyLong_AsScaledDouble(PyObject *vv, int *exponent)
double
PyLong_AsDouble(PyObject *vv)
{
- PyLongObject *v = (PyLongObject *)vv;
- Py_ssize_t rnd_digit, rnd_bit, m, n;
- digit lsb, *d;
- int round_up = 0;
- double x;
-
- if (vv == NULL || !PyLong_Check(vv)) {
- PyErr_BadInternalCall();
- return -1.0;
- }
-
- /* Notes on the method: for simplicity, assume v is positive and >=
- 2**DBL_MANT_DIG. (For negative v we just ignore the sign until the
- end; for small v no rounding is necessary.) Write n for the number
- of bits in v, so that 2**(n-1) <= v < 2**n, and n > DBL_MANT_DIG.
-
- Some terminology: the *rounding bit* of v is the 1st bit of v that
- will be rounded away (bit n - DBL_MANT_DIG - 1); the *parity bit*
- is the bit immediately above. The round-half-to-even rule says
- that we round up if the rounding bit is set, unless v is exactly
- halfway between two floats and the parity bit is zero.
-
- Write d[0] ... d[m] for the digits of v, least to most significant.
- Let rnd_bit be the index of the rounding bit, and rnd_digit the
- index of the PyLong digit containing the rounding bit. Then the
- bits of the digit d[rnd_digit] look something like:
-
- rounding bit
- |
- v
- msb -> sssssrttttttttt <- lsb
- ^
- |
- parity bit
-
- where 's' represents a 'significant bit' that will be included in
- the mantissa of the result, 'r' is the rounding bit, and 't'
- represents a 'trailing bit' following the rounding bit. Note that
- if the rounding bit is at the top of d[rnd_digit] then the parity
- bit will be the lsb of d[rnd_digit+1]. If we set
-
- lsb = 1 << (rnd_bit % PyLong_SHIFT)
-
- then d[rnd_digit] & (PyLong_BASE - 2*lsb) selects just the
- significant bits of d[rnd_digit], d[rnd_digit] & (lsb-1) gets the
- trailing bits, and d[rnd_digit] & lsb gives the rounding bit.
-
- We initialize the double x to the integer given by digits
- d[rnd_digit:m-1], but with the rounding bit and trailing bits of
- d[rnd_digit] masked out. So the value of x comes from the top
- DBL_MANT_DIG bits of v, multiplied by 2*lsb. Note that in the loop
- that produces x, all floating-point operations are exact (assuming
- that FLT_RADIX==2). Now if we're rounding down, the value we want
- to return is simply
-
- x * 2**(PyLong_SHIFT * rnd_digit).
-
- and if we're rounding up, it's
-
- (x + 2*lsb) * 2**(PyLong_SHIFT * rnd_digit).
-
- Under the round-half-to-even rule, we round up if, and only
- if, the rounding bit is set *and* at least one of the
- following three conditions is satisfied:
-
- (1) the parity bit is set, or
- (2) at least one of the trailing bits of d[rnd_digit] is set, or
- (3) at least one of the digits d[i], 0 <= i < rnd_digit
- is nonzero.
-
- Finally, we have to worry about overflow. If v >= 2**DBL_MAX_EXP,
- or equivalently n > DBL_MAX_EXP, then overflow occurs. If v <
- 2**DBL_MAX_EXP then we're usually safe, but there's a corner case
- to consider: if v is very close to 2**DBL_MAX_EXP then it's
- possible that v is rounded up to exactly 2**DBL_MAX_EXP, and then
- again overflow occurs.
- */
-
- if (Py_SIZE(v) == 0)
- return 0.0;
- m = ABS(Py_SIZE(v)) - 1;
- d = v->ob_digit;
- assert(d[m]); /* v should be normalized */
-
- /* fast path for case where 0 < abs(v) < 2**DBL_MANT_DIG */
- if (m < DBL_MANT_DIG / PyLong_SHIFT ||
- (m == DBL_MANT_DIG / PyLong_SHIFT &&
- d[m] < (digit)1 << DBL_MANT_DIG%PyLong_SHIFT)) {
- x = d[m];
- while (--m >= 0)
- x = x*PyLong_BASE + d[m];
- return Py_SIZE(v) < 0 ? -x : x;
- }
-
- /* if m is huge then overflow immediately; otherwise, compute the
- number of bits n in v. The condition below implies n (= #bits) >=
- m * PyLong_SHIFT + 1 > DBL_MAX_EXP, hence v >= 2**DBL_MAX_EXP. */
- if (m > (DBL_MAX_EXP-1)/PyLong_SHIFT)
- goto overflow;
- n = m * PyLong_SHIFT + bits_in_digit(d[m]);
- if (n > DBL_MAX_EXP)
- goto overflow;
-
- /* find location of rounding bit */
- assert(n > DBL_MANT_DIG); /* dealt with |v| < 2**DBL_MANT_DIG above */
- rnd_bit = n - DBL_MANT_DIG - 1;
- rnd_digit = rnd_bit/PyLong_SHIFT;
- lsb = (digit)1 << (rnd_bit%PyLong_SHIFT);
-
- /* Get top DBL_MANT_DIG bits of v. Assumes PyLong_SHIFT <
- DBL_MANT_DIG, so we'll need bits from at least 2 digits of v. */
- x = d[m];
- assert(m > rnd_digit);
- while (--m > rnd_digit)
- x = x*PyLong_BASE + d[m];
- x = x*PyLong_BASE + (d[m] & (PyLong_BASE-2*lsb));
-
- /* decide whether to round up, using round-half-to-even */
- assert(m == rnd_digit);
- if (d[m] & lsb) { /* if (rounding bit is set) */
- digit parity_bit;
- if (lsb == PyLong_BASE/2)
- parity_bit = d[m+1] & 1;
- else
- parity_bit = d[m] & 2*lsb;
- if (parity_bit)
- round_up = 1;
- else if (d[m] & (lsb-1))
- round_up = 1;
- else {
- while (--m >= 0) {
- if (d[m]) {
- round_up = 1;
- break;
- }
- }
- }
- }
-
- /* and round up if necessary */
- if (round_up) {
- x += 2*lsb;
- if (n == DBL_MAX_EXP &&
- x == ldexp((double)(2*lsb), DBL_MANT_DIG)) {
- /* overflow corner case */
- goto overflow;
- }
- }
-
- /* shift, adjust for sign, and return */
- x = ldexp(x, rnd_digit*PyLong_SHIFT);
- return Py_SIZE(v) < 0 ? -x : x;
+ PyLongObject *v = (PyLongObject *)vv;
+ Py_ssize_t rnd_digit, rnd_bit, m, n;
+ digit lsb, *d;
+ int round_up = 0;
+ double x;
+
+ if (vv == NULL || !PyLong_Check(vv)) {
+ PyErr_BadInternalCall();
+ return -1.0;
+ }
+
+ /* Notes on the method: for simplicity, assume v is positive and >=
+ 2**DBL_MANT_DIG. (For negative v we just ignore the sign until the
+ end; for small v no rounding is necessary.) Write n for the number
+ of bits in v, so that 2**(n-1) <= v < 2**n, and n > DBL_MANT_DIG.
+
+ Some terminology: the *rounding bit* of v is the 1st bit of v that
+ will be rounded away (bit n - DBL_MANT_DIG - 1); the *parity bit*
+ is the bit immediately above. The round-half-to-even rule says
+ that we round up if the rounding bit is set, unless v is exactly
+ halfway between two floats and the parity bit is zero.
+
+ Write d[0] ... d[m] for the digits of v, least to most significant.
+ Let rnd_bit be the index of the rounding bit, and rnd_digit the
+ index of the PyLong digit containing the rounding bit. Then the
+ bits of the digit d[rnd_digit] look something like:
+
+ rounding bit
+ |
+ v
+ msb -> sssssrttttttttt <- lsb
+ ^
+ |
+ parity bit
+
+ where 's' represents a 'significant bit' that will be included in
+ the mantissa of the result, 'r' is the rounding bit, and 't'
+ represents a 'trailing bit' following the rounding bit. Note that
+ if the rounding bit is at the top of d[rnd_digit] then the parity
+ bit will be the lsb of d[rnd_digit+1]. If we set
+
+ lsb = 1 << (rnd_bit % PyLong_SHIFT)
+
+ then d[rnd_digit] & (PyLong_BASE - 2*lsb) selects just the
+ significant bits of d[rnd_digit], d[rnd_digit] & (lsb-1) gets the
+ trailing bits, and d[rnd_digit] & lsb gives the rounding bit.
+
+ We initialize the double x to the integer given by digits
+ d[rnd_digit:m-1], but with the rounding bit and trailing bits of
+ d[rnd_digit] masked out. So the value of x comes from the top
+ DBL_MANT_DIG bits of v, multiplied by 2*lsb. Note that in the loop
+ that produces x, all floating-point operations are exact (assuming
+ that FLT_RADIX==2). Now if we're rounding down, the value we want
+ to return is simply
+
+ x * 2**(PyLong_SHIFT * rnd_digit).
+
+ and if we're rounding up, it's
+
+ (x + 2*lsb) * 2**(PyLong_SHIFT * rnd_digit).
+
+ Under the round-half-to-even rule, we round up if, and only
+ if, the rounding bit is set *and* at least one of the
+ following three conditions is satisfied:
+
+ (1) the parity bit is set, or
+ (2) at least one of the trailing bits of d[rnd_digit] is set, or
+ (3) at least one of the digits d[i], 0 <= i < rnd_digit
+ is nonzero.
+
+ Finally, we have to worry about overflow. If v >= 2**DBL_MAX_EXP,
+ or equivalently n > DBL_MAX_EXP, then overflow occurs. If v <
+ 2**DBL_MAX_EXP then we're usually safe, but there's a corner case
+ to consider: if v is very close to 2**DBL_MAX_EXP then it's
+ possible that v is rounded up to exactly 2**DBL_MAX_EXP, and then
+ again overflow occurs.
+ */
+
+ if (Py_SIZE(v) == 0)
+ return 0.0;
+ m = ABS(Py_SIZE(v)) - 1;
+ d = v->ob_digit;
+ assert(d[m]); /* v should be normalized */
+
+ /* fast path for case where 0 < abs(v) < 2**DBL_MANT_DIG */
+ if (m < DBL_MANT_DIG / PyLong_SHIFT ||
+ (m == DBL_MANT_DIG / PyLong_SHIFT &&
+ d[m] < (digit)1 << DBL_MANT_DIG%PyLong_SHIFT)) {
+ x = d[m];
+ while (--m >= 0)
+ x = x*PyLong_BASE + d[m];
+ return Py_SIZE(v) < 0 ? -x : x;
+ }
+
+ /* if m is huge then overflow immediately; otherwise, compute the
+ number of bits n in v. The condition below implies n (= #bits) >=
+ m * PyLong_SHIFT + 1 > DBL_MAX_EXP, hence v >= 2**DBL_MAX_EXP. */
+ if (m > (DBL_MAX_EXP-1)/PyLong_SHIFT)
+ goto overflow;
+ n = m * PyLong_SHIFT + bits_in_digit(d[m]);
+ if (n > DBL_MAX_EXP)
+ goto overflow;
+
+ /* find location of rounding bit */
+ assert(n > DBL_MANT_DIG); /* dealt with |v| < 2**DBL_MANT_DIG above */
+ rnd_bit = n - DBL_MANT_DIG - 1;
+ rnd_digit = rnd_bit/PyLong_SHIFT;
+ lsb = (digit)1 << (rnd_bit%PyLong_SHIFT);
+
+ /* Get top DBL_MANT_DIG bits of v. Assumes PyLong_SHIFT <
+ DBL_MANT_DIG, so we'll need bits from at least 2 digits of v. */
+ x = d[m];
+ assert(m > rnd_digit);
+ while (--m > rnd_digit)
+ x = x*PyLong_BASE + d[m];
+ x = x*PyLong_BASE + (d[m] & (PyLong_BASE-2*lsb));
+
+ /* decide whether to round up, using round-half-to-even */
+ assert(m == rnd_digit);
+ if (d[m] & lsb) { /* if (rounding bit is set) */
+ digit parity_bit;
+ if (lsb == PyLong_BASE/2)
+ parity_bit = d[m+1] & 1;
+ else
+ parity_bit = d[m] & 2*lsb;
+ if (parity_bit)
+ round_up = 1;
+ else if (d[m] & (lsb-1))
+ round_up = 1;
+ else {
+ while (--m >= 0) {
+ if (d[m]) {
+ round_up = 1;
+ break;
+ }
+ }
+ }
+ }
+
+ /* and round up if necessary */
+ if (round_up) {
+ x += 2*lsb;
+ if (n == DBL_MAX_EXP &&
+ x == ldexp((double)(2*lsb), DBL_MANT_DIG)) {
+ /* overflow corner case */
+ goto overflow;
+ }
+ }
+
+ /* shift, adjust for sign, and return */
+ x = ldexp(x, rnd_digit*PyLong_SHIFT);
+ return Py_SIZE(v) < 0 ? -x : x;
overflow:
- PyErr_SetString(PyExc_OverflowError,
- "Python int too large to convert to C double");
- return -1.0;
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C double");
+ return -1.0;
}
/* Create a new long (or int) object from a C pointer */
@@ -1157,10 +1157,10 @@ PyLong_FromVoidPtr(void *p)
#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
# error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
- /* special-case null pointer */
- if (!p)
- return PyLong_FromLong(0);
- return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
+ /* special-case null pointer */
+ if (!p)
+ return PyLong_FromLong(0);
+ return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
}
@@ -1169,17 +1169,17 @@ PyLong_FromVoidPtr(void *p)
void *
PyLong_AsVoidPtr(PyObject *vv)
{
- /* This function will allow int or long objects. If vv is neither,
- then the PyLong_AsLong*() functions will raise the exception:
- PyExc_SystemError, "bad argument to internal function"
- */
+ /* This function will allow int or long objects. If vv is neither,
+ then the PyLong_AsLong*() functions will raise the exception:
+ PyExc_SystemError, "bad argument to internal function"
+ */
#if SIZEOF_VOID_P <= SIZEOF_LONG
- long x;
+ long x;
- if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
- x = PyLong_AsLong(vv);
- else
- x = PyLong_AsUnsignedLong(vv);
+ if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
+ x = PyLong_AsLong(vv);
+ else
+ x = PyLong_AsUnsignedLong(vv);
#else
#ifndef HAVE_LONG_LONG
@@ -1188,18 +1188,18 @@ PyLong_AsVoidPtr(PyObject *vv)
#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
# error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
- PY_LONG_LONG x;
+ PY_LONG_LONG x;
- if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
- x = PyLong_AsLongLong(vv);
- else
- x = PyLong_AsUnsignedLongLong(vv);
+ if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
+ x = PyLong_AsLongLong(vv);
+ else
+ x = PyLong_AsUnsignedLongLong(vv);
#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
- if (x == -1 && PyErr_Occurred())
- return NULL;
- return (void *)x;
+ if (x == -1 && PyErr_Occurred())
+ return NULL;
+ return (void *)x;
}
#ifdef HAVE_LONG_LONG
@@ -1215,43 +1215,43 @@ PyLong_AsVoidPtr(PyObject *vv)
PyObject *
PyLong_FromLongLong(PY_LONG_LONG ival)
{
- PyLongObject *v;
- unsigned PY_LONG_LONG abs_ival;
- unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
- int ndigits = 0;
- int negative = 0;
-
- CHECK_SMALL_INT(ival);
- if (ival < 0) {
- /* avoid signed overflow on negation; see comments
- in PyLong_FromLong above. */
- abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
- negative = 1;
- }
- else {
- abs_ival = (unsigned PY_LONG_LONG)ival;
- }
-
- /* Count the number of Python digits.
- We used to pick 5 ("big enough for anything"), but that's a
- waste of time and space given that 5*15 = 75 bits are rarely
- needed. */
- t = abs_ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = negative ? -ndigits : ndigits;
- t = abs_ival;
- while (t) {
- *p++ = (digit)(t & PyLong_MASK);
- t >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ PyLongObject *v;
+ unsigned PY_LONG_LONG abs_ival;
+ unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
+ int ndigits = 0;
+ int negative = 0;
+
+ CHECK_SMALL_INT(ival);
+ if (ival < 0) {
+ /* avoid signed overflow on negation; see comments
+ in PyLong_FromLong above. */
+ abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
+ negative = 1;
+ }
+ else {
+ abs_ival = (unsigned PY_LONG_LONG)ival;
+ }
+
+ /* Count the number of Python digits.
+ We used to pick 5 ("big enough for anything"), but that's a
+ waste of time and space given that 5*15 = 75 bits are rarely
+ needed. */
+ t = abs_ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = negative ? -ndigits : ndigits;
+ t = abs_ival;
+ while (t) {
+ *p++ = (digit)(t & PyLong_MASK);
+ t >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
@@ -1259,28 +1259,28 @@ PyLong_FromLongLong(PY_LONG_LONG ival)
PyObject *
PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
{
- PyLongObject *v;
- unsigned PY_LONG_LONG t;
- int ndigits = 0;
-
- if (ival < PyLong_BASE)
- return PyLong_FromLong((long)ival);
- /* Count the number of Python digits. */
- t = (unsigned PY_LONG_LONG)ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = ndigits;
- while (ival) {
- *p++ = (digit)(ival & PyLong_MASK);
- ival >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ PyLongObject *v;
+ unsigned PY_LONG_LONG t;
+ int ndigits = 0;
+
+ if (ival < PyLong_BASE)
+ return PyLong_FromLong((long)ival);
+ /* Count the number of Python digits. */
+ t = (unsigned PY_LONG_LONG)ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = ndigits;
+ while (ival) {
+ *p++ = (digit)(ival & PyLong_MASK);
+ ival >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Create a new long int object from a C Py_ssize_t. */
@@ -1288,39 +1288,39 @@ PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
PyObject *
PyLong_FromSsize_t(Py_ssize_t ival)
{
- PyLongObject *v;
- size_t abs_ival;
- size_t t; /* unsigned so >> doesn't propagate sign bit */
- int ndigits = 0;
- int negative = 0;
-
- CHECK_SMALL_INT(ival);
- if (ival < 0) {
- /* avoid signed overflow when ival = SIZE_T_MIN */
- abs_ival = (size_t)(-1-ival)+1;
- negative = 1;
- }
- else {
- abs_ival = (size_t)ival;
- }
-
- /* Count the number of Python digits. */
- t = abs_ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = negative ? -ndigits : ndigits;
- t = abs_ival;
- while (t) {
- *p++ = (digit)(t & PyLong_MASK);
- t >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ PyLongObject *v;
+ size_t abs_ival;
+ size_t t; /* unsigned so >> doesn't propagate sign bit */
+ int ndigits = 0;
+ int negative = 0;
+
+ CHECK_SMALL_INT(ival);
+ if (ival < 0) {
+ /* avoid signed overflow when ival = SIZE_T_MIN */
+ abs_ival = (size_t)(-1-ival)+1;
+ negative = 1;
+ }
+ else {
+ abs_ival = (size_t)ival;
+ }
+
+ /* Count the number of Python digits. */
+ t = abs_ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = negative ? -ndigits : ndigits;
+ t = abs_ival;
+ while (t) {
+ *p++ = (digit)(t & PyLong_MASK);
+ t >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Create a new long int object from a C size_t. */
@@ -1328,28 +1328,28 @@ PyLong_FromSsize_t(Py_ssize_t ival)
PyObject *
PyLong_FromSize_t(size_t ival)
{
- PyLongObject *v;
- size_t t;
- int ndigits = 0;
-
- if (ival < PyLong_BASE)
- return PyLong_FromLong((long)ival);
- /* Count the number of Python digits. */
- t = ival;
- while (t) {
- ++ndigits;
- t >>= PyLong_SHIFT;
- }
- v = _PyLong_New(ndigits);
- if (v != NULL) {
- digit *p = v->ob_digit;
- Py_SIZE(v) = ndigits;
- while (ival) {
- *p++ = (digit)(ival & PyLong_MASK);
- ival >>= PyLong_SHIFT;
- }
- }
- return (PyObject *)v;
+ PyLongObject *v;
+ size_t t;
+ int ndigits = 0;
+
+ if (ival < PyLong_BASE)
+ return PyLong_FromLong((long)ival);
+ /* Count the number of Python digits. */
+ t = ival;
+ while (t) {
+ ++ndigits;
+ t >>= PyLong_SHIFT;
+ }
+ v = _PyLong_New(ndigits);
+ if (v != NULL) {
+ digit *p = v->ob_digit;
+ Py_SIZE(v) = ndigits;
+ while (ival) {
+ *p++ = (digit)(ival & PyLong_MASK);
+ ival >>= PyLong_SHIFT;
+ }
+ }
+ return (PyObject *)v;
}
/* Get a C PY_LONG_LONG int from a long int object.
@@ -1358,51 +1358,51 @@ PyLong_FromSize_t(size_t ival)
PY_LONG_LONG
PyLong_AsLongLong(PyObject *vv)
{
- PyLongObject *v;
- PY_LONG_LONG bytes;
- int one = 1;
- int res;
-
- if (vv == NULL) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (!PyLong_Check(vv)) {
- PyNumberMethods *nb;
- PyObject *io;
- if ((nb = vv->ob_type->tp_as_number) == NULL ||
- nb->nb_int == NULL) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return -1;
- }
- io = (*nb->nb_int) (vv);
- if (io == NULL)
- return -1;
- if (PyLong_Check(io)) {
- bytes = PyLong_AsLongLong(io);
- Py_DECREF(io);
- return bytes;
- }
- Py_DECREF(io);
- PyErr_SetString(PyExc_TypeError, "integer conversion failed");
- return -1;
- }
-
- v = (PyLongObject*)vv;
- switch(Py_SIZE(v)) {
- case -1: return -(sdigit)v->ob_digit[0];
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- res = _PyLong_AsByteArray(
- (PyLongObject *)vv, (unsigned char *)&bytes,
- SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
-
- /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
- if (res < 0)
- return (PY_LONG_LONG)-1;
- else
- return bytes;
+ PyLongObject *v;
+ PY_LONG_LONG bytes;
+ int one = 1;
+ int res;
+
+ if (vv == NULL) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (!PyLong_Check(vv)) {
+ PyNumberMethods *nb;
+ PyObject *io;
+ if ((nb = vv->ob_type->tp_as_number) == NULL ||
+ nb->nb_int == NULL) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return -1;
+ }
+ io = (*nb->nb_int) (vv);
+ if (io == NULL)
+ return -1;
+ if (PyLong_Check(io)) {
+ bytes = PyLong_AsLongLong(io);
+ Py_DECREF(io);
+ return bytes;
+ }
+ Py_DECREF(io);
+ PyErr_SetString(PyExc_TypeError, "integer conversion failed");
+ return -1;
+ }
+
+ v = (PyLongObject*)vv;
+ switch(Py_SIZE(v)) {
+ case -1: return -(sdigit)v->ob_digit[0];
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ res = _PyLong_AsByteArray(
+ (PyLongObject *)vv, (unsigned char *)&bytes,
+ SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
+
+ /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
+ if (res < 0)
+ return (PY_LONG_LONG)-1;
+ else
+ return bytes;
}
/* Get a C unsigned PY_LONG_LONG int from a long int object.
@@ -1411,31 +1411,31 @@ PyLong_AsLongLong(PyObject *vv)
unsigned PY_LONG_LONG
PyLong_AsUnsignedLongLong(PyObject *vv)
{
- PyLongObject *v;
- unsigned PY_LONG_LONG bytes;
- int one = 1;
- int res;
-
- if (vv == NULL || !PyLong_Check(vv)) {
- PyErr_BadInternalCall();
- return (unsigned PY_LONG_LONG)-1;
- }
-
- v = (PyLongObject*)vv;
- switch(Py_SIZE(v)) {
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
-
- res = _PyLong_AsByteArray(
- (PyLongObject *)vv, (unsigned char *)&bytes,
- SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
-
- /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
- if (res < 0)
- return (unsigned PY_LONG_LONG)res;
- else
- return bytes;
+ PyLongObject *v;
+ unsigned PY_LONG_LONG bytes;
+ int one = 1;
+ int res;
+
+ if (vv == NULL || !PyLong_Check(vv)) {
+ PyErr_BadInternalCall();
+ return (unsigned PY_LONG_LONG)-1;
+ }
+
+ v = (PyLongObject*)vv;
+ switch(Py_SIZE(v)) {
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+
+ res = _PyLong_AsByteArray(
+ (PyLongObject *)vv, (unsigned char *)&bytes,
+ SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
+
+ /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
+ if (res < 0)
+ return (unsigned PY_LONG_LONG)res;
+ else
+ return bytes;
}
/* Get a C unsigned long int from a long int object, ignoring the high bits.
@@ -1444,95 +1444,95 @@ PyLong_AsUnsignedLongLong(PyObject *vv)
static unsigned PY_LONG_LONG
_PyLong_AsUnsignedLongLongMask(PyObject *vv)
{
- register PyLongObject *v;
- unsigned PY_LONG_LONG x;
- Py_ssize_t i;
- int sign;
-
- if (vv == NULL || !PyLong_Check(vv)) {
- PyErr_BadInternalCall();
- return (unsigned long) -1;
- }
- v = (PyLongObject *)vv;
- switch(Py_SIZE(v)) {
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- i = Py_SIZE(v);
- sign = 1;
- x = 0;
- if (i < 0) {
- sign = -1;
- i = -i;
- }
- while (--i >= 0) {
- x = (x << PyLong_SHIFT) + v->ob_digit[i];
- }
- return x * sign;
+ register PyLongObject *v;
+ unsigned PY_LONG_LONG x;
+ Py_ssize_t i;
+ int sign;
+
+ if (vv == NULL || !PyLong_Check(vv)) {
+ PyErr_BadInternalCall();
+ return (unsigned long) -1;
+ }
+ v = (PyLongObject *)vv;
+ switch(Py_SIZE(v)) {
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ i = Py_SIZE(v);
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -i;
+ }
+ while (--i >= 0) {
+ x = (x << PyLong_SHIFT) + v->ob_digit[i];
+ }
+ return x * sign;
}
unsigned PY_LONG_LONG
PyLong_AsUnsignedLongLongMask(register PyObject *op)
{
- PyNumberMethods *nb;
- PyLongObject *lo;
- unsigned PY_LONG_LONG val;
-
- if (op && PyLong_Check(op))
- return _PyLong_AsUnsignedLongLongMask(op);
-
- if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
- nb->nb_int == NULL) {
- PyErr_SetString(PyExc_TypeError, "an integer is required");
- return (unsigned PY_LONG_LONG)-1;
- }
-
- lo = (PyLongObject*) (*nb->nb_int) (op);
- if (lo == NULL)
- return (unsigned PY_LONG_LONG)-1;
- if (PyLong_Check(lo)) {
- val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
- Py_DECREF(lo);
- if (PyErr_Occurred())
- return (unsigned PY_LONG_LONG)-1;
- return val;
- }
- else
- {
- Py_DECREF(lo);
- PyErr_SetString(PyExc_TypeError,
- "nb_int should return int object");
- return (unsigned PY_LONG_LONG)-1;
- }
+ PyNumberMethods *nb;
+ PyLongObject *lo;
+ unsigned PY_LONG_LONG val;
+
+ if (op && PyLong_Check(op))
+ return _PyLong_AsUnsignedLongLongMask(op);
+
+ if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
+ nb->nb_int == NULL) {
+ PyErr_SetString(PyExc_TypeError, "an integer is required");
+ return (unsigned PY_LONG_LONG)-1;
+ }
+
+ lo = (PyLongObject*) (*nb->nb_int) (op);
+ if (lo == NULL)
+ return (unsigned PY_LONG_LONG)-1;
+ if (PyLong_Check(lo)) {
+ val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
+ Py_DECREF(lo);
+ if (PyErr_Occurred())
+ return (unsigned PY_LONG_LONG)-1;
+ return val;
+ }
+ else
+ {
+ Py_DECREF(lo);
+ PyErr_SetString(PyExc_TypeError,
+ "nb_int should return int object");
+ return (unsigned PY_LONG_LONG)-1;
+ }
}
#undef IS_LITTLE_ENDIAN
#endif /* HAVE_LONG_LONG */
#define CHECK_BINOP(v,w) \
- if (!PyLong_Check(v) || !PyLong_Check(w)) { \
- Py_INCREF(Py_NotImplemented); \
- return Py_NotImplemented; \
- }
+ if (!PyLong_Check(v) || !PyLong_Check(w)) { \
+ Py_INCREF(Py_NotImplemented); \
+ return Py_NotImplemented; \
+ }
/* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
2**k if d is nonzero, else 0. */
static const unsigned char BitLengthTable[32] = {
- 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
+ 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
};
static int
bits_in_digit(digit d)
{
- int d_bits = 0;
- while (d >= 32) {
- d_bits += 6;
- d >>= 6;
- }
- d_bits += (int)BitLengthTable[d];
- return d_bits;
+ int d_bits = 0;
+ while (d >= 32) {
+ d_bits += 6;
+ d >>= 6;
+ }
+ d_bits += (int)BitLengthTable[d];
+ return d_bits;
}
/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
@@ -1542,23 +1542,23 @@ bits_in_digit(digit d)
static digit
v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
{
- Py_ssize_t i;
- digit carry = 0;
-
- assert(m >= n);
- for (i = 0; i < n; ++i) {
- carry += x[i] + y[i];
- x[i] = carry & PyLong_MASK;
- carry >>= PyLong_SHIFT;
- assert((carry & 1) == carry);
- }
- for (; carry && i < m; ++i) {
- carry += x[i];
- x[i] = carry & PyLong_MASK;
- carry >>= PyLong_SHIFT;
- assert((carry & 1) == carry);
- }
- return carry;
+ Py_ssize_t i;
+ digit carry = 0;
+
+ assert(m >= n);
+ for (i = 0; i < n; ++i) {
+ carry += x[i] + y[i];
+ x[i] = carry & PyLong_MASK;
+ carry >>= PyLong_SHIFT;
+ assert((carry & 1) == carry);
+ }
+ for (; carry && i < m; ++i) {
+ carry += x[i];
+ x[i] = carry & PyLong_MASK;
+ carry >>= PyLong_SHIFT;
+ assert((carry & 1) == carry);
+ }
+ return carry;
}
/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
@@ -1568,23 +1568,23 @@ v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
static digit
v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
{
- Py_ssize_t i;
- digit borrow = 0;
-
- assert(m >= n);
- for (i = 0; i < n; ++i) {
- borrow = x[i] - y[i] - borrow;
- x[i] = borrow & PyLong_MASK;
- borrow >>= PyLong_SHIFT;
- borrow &= 1; /* keep only 1 sign bit */
- }
- for (; borrow && i < m; ++i) {
- borrow = x[i] - borrow;
- x[i] = borrow & PyLong_MASK;
- borrow >>= PyLong_SHIFT;
- borrow &= 1;
- }
- return borrow;
+ Py_ssize_t i;
+ digit borrow = 0;
+
+ assert(m >= n);
+ for (i = 0; i < n; ++i) {
+ borrow = x[i] - y[i] - borrow;
+ x[i] = borrow & PyLong_MASK;
+ borrow >>= PyLong_SHIFT;
+ borrow &= 1; /* keep only 1 sign bit */
+ }
+ for (; borrow && i < m; ++i) {
+ borrow = x[i] - borrow;
+ x[i] = borrow & PyLong_MASK;
+ borrow >>= PyLong_SHIFT;
+ borrow &= 1;
+ }
+ return borrow;
}
/* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put
@@ -1593,16 +1593,16 @@ v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
static digit
v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
{
- Py_ssize_t i;
- digit carry = 0;
-
- assert(0 <= d && d < PyLong_SHIFT);
- for (i=0; i < m; i++) {
- twodigits acc = (twodigits)a[i] << d | carry;
- z[i] = (digit)acc & PyLong_MASK;
- carry = (digit)(acc >> PyLong_SHIFT);
- }
- return carry;
+ Py_ssize_t i;
+ digit carry = 0;
+
+ assert(0 <= d && d < PyLong_SHIFT);
+ for (i=0; i < m; i++) {
+ twodigits acc = (twodigits)a[i] << d | carry;
+ z[i] = (digit)acc & PyLong_MASK;
+ carry = (digit)(acc >> PyLong_SHIFT);
+ }
+ return carry;
}
/* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put
@@ -1611,17 +1611,17 @@ v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
static digit
v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
{
- Py_ssize_t i;
- digit carry = 0;
- digit mask = ((digit)1 << d) - 1U;
-
- assert(0 <= d && d < PyLong_SHIFT);
- for (i=m; i-- > 0;) {
- twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
- carry = (digit)acc & mask;
- z[i] = (digit)(acc >> d);
- }
- return carry;
+ Py_ssize_t i;
+ digit carry = 0;
+ digit mask = ((digit)1 << d) - 1U;
+
+ assert(0 <= d && d < PyLong_SHIFT);
+ for (i=m; i-- > 0;) {
+ twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
+ carry = (digit)acc & mask;
+ z[i] = (digit)(acc >> d);
+ }
+ return carry;
}
/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
@@ -1633,18 +1633,18 @@ v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
static digit
inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
{
- twodigits rem = 0;
-
- assert(n > 0 && n <= PyLong_MASK);
- pin += size;
- pout += size;
- while (--size >= 0) {
- digit hi;
- rem = (rem << PyLong_SHIFT) + *--pin;
- *--pout = hi = (digit)(rem / n);
- rem -= (twodigits)hi * n;
- }
- return (digit)rem;
+ twodigits rem = 0;
+
+ assert(n > 0 && n <= PyLong_MASK);
+ pin += size;
+ pout += size;
+ while (--size >= 0) {
+ digit hi;
+ rem = (rem << PyLong_SHIFT) + *--pin;
+ *--pout = hi = (digit)(rem / n);
+ rem -= (twodigits)hi * n;
+ }
+ return (digit)rem;
}
/* Divide a long integer by a digit, returning both the quotient
@@ -1654,15 +1654,15 @@ inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
static PyLongObject *
divrem1(PyLongObject *a, digit n, digit *prem)
{
- const Py_ssize_t size = ABS(Py_SIZE(a));
- PyLongObject *z;
-
- assert(n > 0 && n <= PyLong_MASK);
- z = _PyLong_New(size);
- if (z == NULL)
- return NULL;
- *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
- return long_normalize(z);
+ const Py_ssize_t size = ABS(Py_SIZE(a));
+ PyLongObject *z;
+
+ assert(n > 0 && n <= PyLong_MASK);
+ z = _PyLong_New(size);
+ if (z == NULL)
+ return NULL;
+ *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
+ return long_normalize(z);
}
/* Convert a long int object to a string, using a given conversion base.
@@ -1672,163 +1672,163 @@ divrem1(PyLongObject *a, digit n, digit *prem)
PyObject *
_PyLong_Format(PyObject *aa, int base)
{
- register PyLongObject *a = (PyLongObject *)aa;
- PyObject *str;
- Py_ssize_t i, sz;
- Py_ssize_t size_a;
- Py_UNICODE *p;
- int bits;
- char sign = '\0';
-
- if (a == NULL || !PyLong_Check(a)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- assert(base >= 2 && base <= 36);
- size_a = ABS(Py_SIZE(a));
-
- /* Compute a rough upper bound for the length of the string */
- i = base;
- bits = 0;
- while (i > 1) {
- ++bits;
- i >>= 1;
- }
- i = 5;
- /* ensure we don't get signed overflow in sz calculation */
- if (size_a > (PY_SSIZE_T_MAX - i) / PyLong_SHIFT) {
- PyErr_SetString(PyExc_OverflowError,
- "int is too large to format");
- return NULL;
- }
- sz = i + 1 + (size_a * PyLong_SHIFT - 1) / bits;
- assert(sz >= 0);
- str = PyUnicode_FromUnicode(NULL, sz);
- if (str == NULL)
- return NULL;
- p = PyUnicode_AS_UNICODE(str) + sz;
- *p = '\0';
- if (Py_SIZE(a) < 0)
- sign = '-';
-
- if (Py_SIZE(a) == 0) {
- *--p = '0';
- }
- else if ((base & (base - 1)) == 0) {
- /* JRH: special case for power-of-2 bases */
- twodigits accum = 0;
- int accumbits = 0; /* # of bits in accum */
- int basebits = 1; /* # of bits in base-1 */
- i = base;
- while ((i >>= 1) > 1)
- ++basebits;
-
- for (i = 0; i < size_a; ++i) {
- accum |= (twodigits)a->ob_digit[i] << accumbits;
- accumbits += PyLong_SHIFT;
- assert(accumbits >= basebits);
- do {
- char cdigit = (char)(accum & (base - 1));
- cdigit += (cdigit < 10) ? '0' : 'a'-10;
- assert(p > PyUnicode_AS_UNICODE(str));
- *--p = cdigit;
- accumbits -= basebits;
- accum >>= basebits;
- } while (i < size_a-1 ? accumbits >= basebits :
- accum > 0);
- }
- }
- else {
- /* Not 0, and base not a power of 2. Divide repeatedly by
- base, but for speed use the highest power of base that
- fits in a digit. */
- Py_ssize_t size = size_a;
- digit *pin = a->ob_digit;
- PyLongObject *scratch;
- /* powbasw <- largest power of base that fits in a digit. */
- digit powbase = base; /* powbase == base ** power */
- int power = 1;
- for (;;) {
- twodigits newpow = powbase * (twodigits)base;
- if (newpow >> PyLong_SHIFT)
- /* doesn't fit in a digit */
- break;
- powbase = (digit)newpow;
- ++power;
- }
-
- /* Get a scratch area for repeated division. */
- scratch = _PyLong_New(size);
- if (scratch == NULL) {
- Py_DECREF(str);
- return NULL;
- }
-
- /* Repeatedly divide by powbase. */
- do {
- int ntostore = power;
- digit rem = inplace_divrem1(scratch->ob_digit,
- pin, size, powbase);
- pin = scratch->ob_digit; /* no need to use a again */
- if (pin[size - 1] == 0)
- --size;
- SIGCHECK({
- Py_DECREF(scratch);
- Py_DECREF(str);
- return NULL;
- })
-
- /* Break rem into digits. */
- assert(ntostore > 0);
- do {
- digit nextrem = (digit)(rem / base);
- char c = (char)(rem - nextrem * base);
- assert(p > PyUnicode_AS_UNICODE(str));
- c += (c < 10) ? '0' : 'a'-10;
- *--p = c;
- rem = nextrem;
- --ntostore;
- /* Termination is a bit delicate: must not
- store leading zeroes, so must get out if
- remaining quotient and rem are both 0. */
- } while (ntostore && (size || rem));
- } while (size != 0);
- Py_DECREF(scratch);
- }
-
- if (base == 16) {
- *--p = 'x';
- *--p = '0';
- }
- else if (base == 8) {
- *--p = 'o';
- *--p = '0';
- }
- else if (base == 2) {
- *--p = 'b';
- *--p = '0';
- }
- else if (base != 10) {
- *--p = '#';
- *--p = '0' + base%10;
- if (base > 10)
- *--p = '0' + base/10;
- }
- if (sign)
- *--p = sign;
- if (p != PyUnicode_AS_UNICODE(str)) {
- Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
- assert(p > q);
- do {
- } while ((*q++ = *p++) != '\0');
- q--;
- if (PyUnicode_Resize(&str,(Py_ssize_t) (q -
- PyUnicode_AS_UNICODE(str)))) {
- Py_DECREF(str);
- return NULL;
- }
- }
- return (PyObject *)str;
+ register PyLongObject *a = (PyLongObject *)aa;
+ PyObject *str;
+ Py_ssize_t i, sz;
+ Py_ssize_t size_a;
+ Py_UNICODE *p;
+ int bits;
+ char sign = '\0';
+
+ if (a == NULL || !PyLong_Check(a)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ assert(base >= 2 && base <= 36);
+ size_a = ABS(Py_SIZE(a));
+
+ /* Compute a rough upper bound for the length of the string */
+ i = base;
+ bits = 0;
+ while (i > 1) {
+ ++bits;
+ i >>= 1;
+ }
+ i = 5;
+ /* ensure we don't get signed overflow in sz calculation */
+ if (size_a > (PY_SSIZE_T_MAX - i) / PyLong_SHIFT) {
+ PyErr_SetString(PyExc_OverflowError,
+ "int is too large to format");
+ return NULL;
+ }
+ sz = i + 1 + (size_a * PyLong_SHIFT - 1) / bits;
+ assert(sz >= 0);
+ str = PyUnicode_FromUnicode(NULL, sz);
+ if (str == NULL)
+ return NULL;
+ p = PyUnicode_AS_UNICODE(str) + sz;
+ *p = '\0';
+ if (Py_SIZE(a) < 0)
+ sign = '-';
+
+ if (Py_SIZE(a) == 0) {
+ *--p = '0';
+ }
+ else if ((base & (base - 1)) == 0) {
+ /* JRH: special case for power-of-2 bases */
+ twodigits accum = 0;
+ int accumbits = 0; /* # of bits in accum */
+ int basebits = 1; /* # of bits in base-1 */
+ i = base;
+ while ((i >>= 1) > 1)
+ ++basebits;
+
+ for (i = 0; i < size_a; ++i) {
+ accum |= (twodigits)a->ob_digit[i] << accumbits;
+ accumbits += PyLong_SHIFT;
+ assert(accumbits >= basebits);
+ do {
+ char cdigit = (char)(accum & (base - 1));
+ cdigit += (cdigit < 10) ? '0' : 'a'-10;
+ assert(p > PyUnicode_AS_UNICODE(str));
+ *--p = cdigit;
+ accumbits -= basebits;
+ accum >>= basebits;
+ } while (i < size_a-1 ? accumbits >= basebits :
+ accum > 0);
+ }
+ }
+ else {
+ /* Not 0, and base not a power of 2. Divide repeatedly by
+ base, but for speed use the highest power of base that
+ fits in a digit. */
+ Py_ssize_t size = size_a;
+ digit *pin = a->ob_digit;
+ PyLongObject *scratch;
+ /* powbasw <- largest power of base that fits in a digit. */
+ digit powbase = base; /* powbase == base ** power */
+ int power = 1;
+ for (;;) {
+ twodigits newpow = powbase * (twodigits)base;
+ if (newpow >> PyLong_SHIFT)
+ /* doesn't fit in a digit */
+ break;
+ powbase = (digit)newpow;
+ ++power;
+ }
+
+ /* Get a scratch area for repeated division. */
+ scratch = _PyLong_New(size);
+ if (scratch == NULL) {
+ Py_DECREF(str);
+ return NULL;
+ }
+
+ /* Repeatedly divide by powbase. */
+ do {
+ int ntostore = power;
+ digit rem = inplace_divrem1(scratch->ob_digit,
+ pin, size, powbase);
+ pin = scratch->ob_digit; /* no need to use a again */
+ if (pin[size - 1] == 0)
+ --size;
+ SIGCHECK({
+ Py_DECREF(scratch);
+ Py_DECREF(str);
+ return NULL;
+ })
+
+ /* Break rem into digits. */
+ assert(ntostore > 0);
+ do {
+ digit nextrem = (digit)(rem / base);
+ char c = (char)(rem - nextrem * base);
+ assert(p > PyUnicode_AS_UNICODE(str));
+ c += (c < 10) ? '0' : 'a'-10;
+ *--p = c;
+ rem = nextrem;
+ --ntostore;
+ /* Termination is a bit delicate: must not
+ store leading zeroes, so must get out if
+ remaining quotient and rem are both 0. */
+ } while (ntostore && (size || rem));
+ } while (size != 0);
+ Py_DECREF(scratch);
+ }
+
+ if (base == 16) {
+ *--p = 'x';
+ *--p = '0';
+ }
+ else if (base == 8) {
+ *--p = 'o';
+ *--p = '0';
+ }
+ else if (base == 2) {
+ *--p = 'b';
+ *--p = '0';
+ }
+ else if (base != 10) {
+ *--p = '#';
+ *--p = '0' + base%10;
+ if (base > 10)
+ *--p = '0' + base/10;
+ }
+ if (sign)
+ *--p = sign;
+ if (p != PyUnicode_AS_UNICODE(str)) {
+ Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
+ assert(p > q);
+ do {
+ } while ((*q++ = *p++) != '\0');
+ q--;
+ if (PyUnicode_Resize(&str,(Py_ssize_t) (q -
+ PyUnicode_AS_UNICODE(str)))) {
+ Py_DECREF(str);
+ return NULL;
+ }
+ }
+ return (PyObject *)str;
}
/* Table of digit values for 8-bit string -> integer conversion.
@@ -1839,22 +1839,22 @@ _PyLong_Format(PyObject *aa, int base)
* base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
*/
unsigned char _PyLong_DigitValue[256] = {
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
- 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
- 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
- 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
- 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
+ 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
+ 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
+ 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
};
/* *str points to the first digit in a string of base `base` digits. base
@@ -1866,111 +1866,111 @@ unsigned char _PyLong_DigitValue[256] = {
static PyLongObject *
long_from_binary_base(char **str, int base)
{
- char *p = *str;
- char *start = p;
- int bits_per_char;
- Py_ssize_t n;
- PyLongObject *z;
- twodigits accum;
- int bits_in_accum;
- digit *pdigit;
-
- assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
- n = base;
- for (bits_per_char = -1; n; ++bits_per_char)
- n >>= 1;
- /* n <- total # of bits needed, while setting p to end-of-string */
- while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
- ++p;
- *str = p;
- /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
- n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
- if (n / bits_per_char < p - start) {
- PyErr_SetString(PyExc_ValueError,
- "int string too large to convert");
- return NULL;
- }
- n = n / PyLong_SHIFT;
- z = _PyLong_New(n);
- if (z == NULL)
- return NULL;
- /* Read string from right, and fill in long from left; i.e.,
- * from least to most significant in both.
- */
- accum = 0;
- bits_in_accum = 0;
- pdigit = z->ob_digit;
- while (--p >= start) {
- int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)];
- assert(k >= 0 && k < base);
- accum |= (twodigits)k << bits_in_accum;
- bits_in_accum += bits_per_char;
- if (bits_in_accum >= PyLong_SHIFT) {
- *pdigit++ = (digit)(accum & PyLong_MASK);
- assert(pdigit - z->ob_digit <= n);
- accum >>= PyLong_SHIFT;
- bits_in_accum -= PyLong_SHIFT;
- assert(bits_in_accum < PyLong_SHIFT);
- }
- }
- if (bits_in_accum) {
- assert(bits_in_accum <= PyLong_SHIFT);
- *pdigit++ = (digit)accum;
- assert(pdigit - z->ob_digit <= n);
- }
- while (pdigit - z->ob_digit < n)
- *pdigit++ = 0;
- return long_normalize(z);
+ char *p = *str;
+ char *start = p;
+ int bits_per_char;
+ Py_ssize_t n;
+ PyLongObject *z;
+ twodigits accum;
+ int bits_in_accum;
+ digit *pdigit;
+
+ assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
+ n = base;
+ for (bits_per_char = -1; n; ++bits_per_char)
+ n >>= 1;
+ /* n <- total # of bits needed, while setting p to end-of-string */
+ while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
+ ++p;
+ *str = p;
+ /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
+ n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
+ if (n / bits_per_char < p - start) {
+ PyErr_SetString(PyExc_ValueError,
+ "int string too large to convert");
+ return NULL;
+ }
+ n = n / PyLong_SHIFT;
+ z = _PyLong_New(n);
+ if (z == NULL)
+ return NULL;
+ /* Read string from right, and fill in long from left; i.e.,
+ * from least to most significant in both.
+ */
+ accum = 0;
+ bits_in_accum = 0;
+ pdigit = z->ob_digit;
+ while (--p >= start) {
+ int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)];
+ assert(k >= 0 && k < base);
+ accum |= (twodigits)k << bits_in_accum;
+ bits_in_accum += bits_per_char;
+ if (bits_in_accum >= PyLong_SHIFT) {
+ *pdigit++ = (digit)(accum & PyLong_MASK);
+ assert(pdigit - z->ob_digit <= n);
+ accum >>= PyLong_SHIFT;
+ bits_in_accum -= PyLong_SHIFT;
+ assert(bits_in_accum < PyLong_SHIFT);
+ }
+ }
+ if (bits_in_accum) {
+ assert(bits_in_accum <= PyLong_SHIFT);
+ *pdigit++ = (digit)accum;
+ assert(pdigit - z->ob_digit <= n);
+ }
+ while (pdigit - z->ob_digit < n)
+ *pdigit++ = 0;
+ return long_normalize(z);
}
PyObject *
PyLong_FromString(char *str, char **pend, int base)
{
- int sign = 1, error_if_nonzero = 0;
- char *start, *orig_str = str;
- PyLongObject *z = NULL;
- PyObject *strobj;
- Py_ssize_t slen;
-
- if ((base != 0 && base < 2) || base > 36) {
- PyErr_SetString(PyExc_ValueError,
- "int() arg 2 must be >= 2 and <= 36");
- return NULL;
- }
- while (*str != '\0' && isspace(Py_CHARMASK(*str)))
- str++;
- if (*str == '+')
- ++str;
- else if (*str == '-') {
- ++str;
- sign = -1;
- }
- if (base == 0) {
- if (str[0] != '0')
- base = 10;
- else if (str[1] == 'x' || str[1] == 'X')
- base = 16;
- else if (str[1] == 'o' || str[1] == 'O')
- base = 8;
- else if (str[1] == 'b' || str[1] == 'B')
- base = 2;
- else {
- /* "old" (C-style) octal literal, now invalid.
- it might still be zero though */
- error_if_nonzero = 1;
- base = 10;
- }
- }
- if (str[0] == '0' &&
- ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
- (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
- (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
- str += 2;
-
- start = str;
- if ((base & (base - 1)) == 0)
- z = long_from_binary_base(&str, base);
- else {
+ int sign = 1, error_if_nonzero = 0;
+ char *start, *orig_str = str;
+ PyLongObject *z = NULL;
+ PyObject *strobj;
+ Py_ssize_t slen;
+
+ if ((base != 0 && base < 2) || base > 36) {
+ PyErr_SetString(PyExc_ValueError,
+ "int() arg 2 must be >= 2 and <= 36");
+ return NULL;
+ }
+ while (*str != '\0' && isspace(Py_CHARMASK(*str)))
+ str++;
+ if (*str == '+')
+ ++str;
+ else if (*str == '-') {
+ ++str;
+ sign = -1;
+ }
+ if (base == 0) {
+ if (str[0] != '0')
+ base = 10;
+ else if (str[1] == 'x' || str[1] == 'X')
+ base = 16;
+ else if (str[1] == 'o' || str[1] == 'O')
+ base = 8;
+ else if (str[1] == 'b' || str[1] == 'B')
+ base = 2;
+ else {
+ /* "old" (C-style) octal literal, now invalid.
+ it might still be zero though */
+ error_if_nonzero = 1;
+ base = 10;
+ }
+ }
+ if (str[0] == '0' &&
+ ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
+ (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
+ (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
+ str += 2;
+
+ start = str;
+ if ((base & (base - 1)) == 0)
+ z = long_from_binary_base(&str, base);
+ else {
/***
Binary bases can be converted in time linear in the number of digits, because
Python's representation base is binary. Other bases (including decimal!) use
@@ -2056,227 +2056,227 @@ that triggers it(!). Instead the code was tested by artificially allocating
just 1 digit at the start, so that the copying code was exercised for every
digit beyond the first.
***/
- register twodigits c; /* current input character */
- Py_ssize_t size_z;
- int i;
- int convwidth;
- twodigits convmultmax, convmult;
- digit *pz, *pzstop;
- char* scan;
-
- static double log_base_BASE[37] = {0.0e0,};
- static int convwidth_base[37] = {0,};
- static twodigits convmultmax_base[37] = {0,};
-
- if (log_base_BASE[base] == 0.0) {
- twodigits convmax = base;
- int i = 1;
-
- log_base_BASE[base] = log((double)base) /
- log((double)PyLong_BASE);
- for (;;) {
- twodigits next = convmax * base;
- if (next > PyLong_BASE)
- break;
- convmax = next;
- ++i;
- }
- convmultmax_base[base] = convmax;
- assert(i > 0);
- convwidth_base[base] = i;
- }
-
- /* Find length of the string of numeric characters. */
- scan = str;
- while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
- ++scan;
-
- /* Create a long object that can contain the largest possible
- * integer with this base and length. Note that there's no
- * need to initialize z->ob_digit -- no slot is read up before
- * being stored into.
- */
- size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
- /* Uncomment next line to test exceedingly rare copy code */
- /* size_z = 1; */
- assert(size_z > 0);
- z = _PyLong_New(size_z);
- if (z == NULL)
- return NULL;
- Py_SIZE(z) = 0;
-
- /* `convwidth` consecutive input digits are treated as a single
- * digit in base `convmultmax`.
- */
- convwidth = convwidth_base[base];
- convmultmax = convmultmax_base[base];
-
- /* Work ;-) */
- while (str < scan) {
- /* grab up to convwidth digits from the input string */
- c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
- for (i = 1; i < convwidth && str != scan; ++i, ++str) {
- c = (twodigits)(c * base +
- (int)_PyLong_DigitValue[Py_CHARMASK(*str)]);
- assert(c < PyLong_BASE);
- }
-
- convmult = convmultmax;
- /* Calculate the shift only if we couldn't get
- * convwidth digits.
- */
- if (i != convwidth) {
- convmult = base;
- for ( ; i > 1; --i)
- convmult *= base;
- }
-
- /* Multiply z by convmult, and add c. */
- pz = z->ob_digit;
- pzstop = pz + Py_SIZE(z);
- for (; pz < pzstop; ++pz) {
- c += (twodigits)*pz * convmult;
- *pz = (digit)(c & PyLong_MASK);
- c >>= PyLong_SHIFT;
- }
- /* carry off the current end? */
- if (c) {
- assert(c < PyLong_BASE);
- if (Py_SIZE(z) < size_z) {
- *pz = (digit)c;
- ++Py_SIZE(z);
- }
- else {
- PyLongObject *tmp;
- /* Extremely rare. Get more space. */
- assert(Py_SIZE(z) == size_z);
- tmp = _PyLong_New(size_z + 1);
- if (tmp == NULL) {
- Py_DECREF(z);
- return NULL;
- }
- memcpy(tmp->ob_digit,
- z->ob_digit,
- sizeof(digit) * size_z);
- Py_DECREF(z);
- z = tmp;
- z->ob_digit[size_z] = (digit)c;
- ++size_z;
- }
- }
- }
- }
- if (z == NULL)
- return NULL;
- if (error_if_nonzero) {
- /* reset the base to 0, else the exception message
- doesn't make too much sense */
- base = 0;
- if (Py_SIZE(z) != 0)
- goto onError;
- /* there might still be other problems, therefore base
- remains zero here for the same reason */
- }
- if (str == start)
- goto onError;
- if (sign < 0)
- Py_SIZE(z) = -(Py_SIZE(z));
- while (*str && isspace(Py_CHARMASK(*str)))
- str++;
- if (*str != '\0')
- goto onError;
- if (pend)
- *pend = str;
- long_normalize(z);
- return (PyObject *) maybe_small_long(z);
+ register twodigits c; /* current input character */
+ Py_ssize_t size_z;
+ int i;
+ int convwidth;
+ twodigits convmultmax, convmult;
+ digit *pz, *pzstop;
+ char* scan;
+
+ static double log_base_BASE[37] = {0.0e0,};
+ static int convwidth_base[37] = {0,};
+ static twodigits convmultmax_base[37] = {0,};
+
+ if (log_base_BASE[base] == 0.0) {
+ twodigits convmax = base;
+ int i = 1;
+
+ log_base_BASE[base] = log((double)base) /
+ log((double)PyLong_BASE);
+ for (;;) {
+ twodigits next = convmax * base;
+ if (next > PyLong_BASE)
+ break;
+ convmax = next;
+ ++i;
+ }
+ convmultmax_base[base] = convmax;
+ assert(i > 0);
+ convwidth_base[base] = i;
+ }
+
+ /* Find length of the string of numeric characters. */
+ scan = str;
+ while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
+ ++scan;
+
+ /* Create a long object that can contain the largest possible
+ * integer with this base and length. Note that there's no
+ * need to initialize z->ob_digit -- no slot is read up before
+ * being stored into.
+ */
+ size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
+ /* Uncomment next line to test exceedingly rare copy code */
+ /* size_z = 1; */
+ assert(size_z > 0);
+ z = _PyLong_New(size_z);
+ if (z == NULL)
+ return NULL;
+ Py_SIZE(z) = 0;
+
+ /* `convwidth` consecutive input digits are treated as a single
+ * digit in base `convmultmax`.
+ */
+ convwidth = convwidth_base[base];
+ convmultmax = convmultmax_base[base];
+
+ /* Work ;-) */
+ while (str < scan) {
+ /* grab up to convwidth digits from the input string */
+ c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
+ for (i = 1; i < convwidth && str != scan; ++i, ++str) {
+ c = (twodigits)(c * base +
+ (int)_PyLong_DigitValue[Py_CHARMASK(*str)]);
+ assert(c < PyLong_BASE);
+ }
+
+ convmult = convmultmax;
+ /* Calculate the shift only if we couldn't get
+ * convwidth digits.
+ */
+ if (i != convwidth) {
+ convmult = base;
+ for ( ; i > 1; --i)
+ convmult *= base;
+ }
+
+ /* Multiply z by convmult, and add c. */
+ pz = z->ob_digit;
+ pzstop = pz + Py_SIZE(z);
+ for (; pz < pzstop; ++pz) {
+ c += (twodigits)*pz * convmult;
+ *pz = (digit)(c & PyLong_MASK);
+ c >>= PyLong_SHIFT;
+ }
+ /* carry off the current end? */
+ if (c) {
+ assert(c < PyLong_BASE);
+ if (Py_SIZE(z) < size_z) {
+ *pz = (digit)c;
+ ++Py_SIZE(z);
+ }
+ else {
+ PyLongObject *tmp;
+ /* Extremely rare. Get more space. */
+ assert(Py_SIZE(z) == size_z);
+ tmp = _PyLong_New(size_z + 1);
+ if (tmp == NULL) {
+ Py_DECREF(z);
+ return NULL;
+ }
+ memcpy(tmp->ob_digit,
+ z->ob_digit,
+ sizeof(digit) * size_z);
+ Py_DECREF(z);
+ z = tmp;
+ z->ob_digit[size_z] = (digit)c;
+ ++size_z;
+ }
+ }
+ }
+ }
+ if (z == NULL)
+ return NULL;
+ if (error_if_nonzero) {
+ /* reset the base to 0, else the exception message
+ doesn't make too much sense */
+ base = 0;
+ if (Py_SIZE(z) != 0)
+ goto onError;
+ /* there might still be other problems, therefore base
+ remains zero here for the same reason */
+ }
+ if (str == start)
+ goto onError;
+ if (sign < 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
+ while (*str && isspace(Py_CHARMASK(*str)))
+ str++;
+ if (*str != '\0')
+ goto onError;
+ if (pend)
+ *pend = str;
+ long_normalize(z);
+ return (PyObject *) maybe_small_long(z);
onError:
- Py_XDECREF(z);
- slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
- strobj = PyUnicode_FromStringAndSize(orig_str, slen);
- if (strobj == NULL)
- return NULL;
- PyErr_Format(PyExc_ValueError,
- "invalid literal for int() with base %d: %R",
- base, strobj);
- Py_DECREF(strobj);
- return NULL;
+ Py_XDECREF(z);
+ slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
+ strobj = PyUnicode_FromStringAndSize(orig_str, slen);
+ if (strobj == NULL)
+ return NULL;
+ PyErr_Format(PyExc_ValueError,
+ "invalid literal for int() with base %d: %R",
+ base, strobj);
+ Py_DECREF(strobj);
+ return NULL;
}
PyObject *
PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
{
- PyObject *result;
- char *buffer = (char *)PyMem_MALLOC(length+1);
-
- if (buffer == NULL)
- return NULL;
-
- if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
- PyMem_FREE(buffer);
- return NULL;
- }
- result = PyLong_FromString(buffer, NULL, base);
- PyMem_FREE(buffer);
- return result;
+ PyObject *result;
+ char *buffer = (char *)PyMem_MALLOC(length+1);
+
+ if (buffer == NULL)
+ return NULL;
+
+ if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
+ PyMem_FREE(buffer);
+ return NULL;
+ }
+ result = PyLong_FromString(buffer, NULL, base);
+ PyMem_FREE(buffer);
+ return result;
}
/* forward */
static PyLongObject *x_divrem
- (PyLongObject *, PyLongObject *, PyLongObject **);
+ (PyLongObject *, PyLongObject *, PyLongObject **);
static PyObject *long_long(PyObject *v);
/* Long division with remainder, top-level routine */
static int
long_divrem(PyLongObject *a, PyLongObject *b,
- PyLongObject **pdiv, PyLongObject **prem)
+ PyLongObject **pdiv, PyLongObject **prem)
{
- Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
- PyLongObject *z;
-
- if (size_b == 0) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "integer division or modulo by zero");
- return -1;
- }
- if (size_a < size_b ||
- (size_a == size_b &&
- a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
- /* |a| < |b|. */
- *pdiv = (PyLongObject*)PyLong_FromLong(0);
- if (*pdiv == NULL)
- return -1;
- Py_INCREF(a);
- *prem = (PyLongObject *) a;
- return 0;
- }
- if (size_b == 1) {
- digit rem = 0;
- z = divrem1(a, b->ob_digit[0], &rem);
- if (z == NULL)
- return -1;
- *prem = (PyLongObject *) PyLong_FromLong((long)rem);
- if (*prem == NULL) {
- Py_DECREF(z);
- return -1;
- }
- }
- else {
- z = x_divrem(a, b, prem);
- if (z == NULL)
- return -1;
- }
- /* Set the signs.
- The quotient z has the sign of a*b;
- the remainder r has the sign of a,
- so a = b*z + r. */
- if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
- NEGATE(z);
- if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
- NEGATE(*prem);
- *pdiv = maybe_small_long(z);
- return 0;
+ Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
+ PyLongObject *z;
+
+ if (size_b == 0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "integer division or modulo by zero");
+ return -1;
+ }
+ if (size_a < size_b ||
+ (size_a == size_b &&
+ a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
+ /* |a| < |b|. */
+ *pdiv = (PyLongObject*)PyLong_FromLong(0);
+ if (*pdiv == NULL)
+ return -1;
+ Py_INCREF(a);
+ *prem = (PyLongObject *) a;
+ return 0;
+ }
+ if (size_b == 1) {
+ digit rem = 0;
+ z = divrem1(a, b->ob_digit[0], &rem);
+ if (z == NULL)
+ return -1;
+ *prem = (PyLongObject *) PyLong_FromLong((long)rem);
+ if (*prem == NULL) {
+ Py_DECREF(z);
+ return -1;
+ }
+ }
+ else {
+ z = x_divrem(a, b, prem);
+ if (z == NULL)
+ return -1;
+ }
+ /* Set the signs.
+ The quotient z has the sign of a*b;
+ the remainder r has the sign of a,
+ so a = b*z + r. */
+ if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
+ NEGATE(z);
+ if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
+ NEGATE(*prem);
+ *pdiv = maybe_small_long(z);
+ return 0;
}
/* Unsigned long division with remainder -- the algorithm. The arguments v1
@@ -2285,125 +2285,125 @@ long_divrem(PyLongObject *a, PyLongObject *b,
static PyLongObject *
x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
{
- PyLongObject *v, *w, *a;
- Py_ssize_t i, k, size_v, size_w;
- int d;
- digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
- twodigits vv;
- sdigit zhi;
- stwodigits z;
-
- /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
- edn.), section 4.3.1, Algorithm D], except that we don't explicitly
- handle the special case when the initial estimate q for a quotient
- digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
- that won't overflow a digit. */
-
- /* allocate space; w will also be used to hold the final remainder */
- size_v = ABS(Py_SIZE(v1));
- size_w = ABS(Py_SIZE(w1));
- assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
- v = _PyLong_New(size_v+1);
- if (v == NULL) {
- *prem = NULL;
- return NULL;
- }
- w = _PyLong_New(size_w);
- if (w == NULL) {
- Py_DECREF(v);
- *prem = NULL;
- return NULL;
- }
-
- /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
- shift v1 left by the same amount. Results go into w and v. */
- d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
- carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
- assert(carry == 0);
- carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
- if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
- v->ob_digit[size_v] = carry;
- size_v++;
- }
-
- /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
- at most (and usually exactly) k = size_v - size_w digits. */
- k = size_v - size_w;
- assert(k >= 0);
- a = _PyLong_New(k);
- if (a == NULL) {
- Py_DECREF(w);
- Py_DECREF(v);
- *prem = NULL;
- return NULL;
- }
- v0 = v->ob_digit;
- w0 = w->ob_digit;
- wm1 = w0[size_w-1];
- wm2 = w0[size_w-2];
- for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
- /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
- single-digit quotient q, remainder in vk[0:size_w]. */
-
- SIGCHECK({
- Py_DECREF(a);
- Py_DECREF(w);
- Py_DECREF(v);
- *prem = NULL;
- return NULL;
- })
-
- /* estimate quotient digit q; may overestimate by 1 (rare) */
- vtop = vk[size_w];
- assert(vtop <= wm1);
- vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
- q = (digit)(vv / wm1);
- r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
- while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
- | vk[size_w-2])) {
- --q;
- r += wm1;
- if (r >= PyLong_BASE)
- break;
- }
- assert(q <= PyLong_BASE);
-
- /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
- zhi = 0;
- for (i = 0; i < size_w; ++i) {
- /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
- -PyLong_BASE * q <= z < PyLong_BASE */
- z = (sdigit)vk[i] + zhi -
- (stwodigits)q * (stwodigits)w0[i];
- vk[i] = (digit)z & PyLong_MASK;
- zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
- z, PyLong_SHIFT);
- }
-
- /* add w back if q was too large (this branch taken rarely) */
- assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
- if ((sdigit)vtop + zhi < 0) {
- carry = 0;
- for (i = 0; i < size_w; ++i) {
- carry += vk[i] + w0[i];
- vk[i] = carry & PyLong_MASK;
- carry >>= PyLong_SHIFT;
- }
- --q;
- }
-
- /* store quotient digit */
- assert(q < PyLong_BASE);
- *--ak = q;
- }
-
- /* unshift remainder; we reuse w to store the result */
- carry = v_rshift(w0, v0, size_w, d);
- assert(carry==0);
- Py_DECREF(v);
-
- *prem = long_normalize(w);
- return long_normalize(a);
+ PyLongObject *v, *w, *a;
+ Py_ssize_t i, k, size_v, size_w;
+ int d;
+ digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
+ twodigits vv;
+ sdigit zhi;
+ stwodigits z;
+
+ /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
+ edn.), section 4.3.1, Algorithm D], except that we don't explicitly
+ handle the special case when the initial estimate q for a quotient
+ digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
+ that won't overflow a digit. */
+
+ /* allocate space; w will also be used to hold the final remainder */
+ size_v = ABS(Py_SIZE(v1));
+ size_w = ABS(Py_SIZE(w1));
+ assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
+ v = _PyLong_New(size_v+1);
+ if (v == NULL) {
+ *prem = NULL;
+ return NULL;
+ }
+ w = _PyLong_New(size_w);
+ if (w == NULL) {
+ Py_DECREF(v);
+ *prem = NULL;
+ return NULL;
+ }
+
+ /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
+ shift v1 left by the same amount. Results go into w and v. */
+ d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
+ carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
+ assert(carry == 0);
+ carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
+ if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
+ v->ob_digit[size_v] = carry;
+ size_v++;
+ }
+
+ /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
+ at most (and usually exactly) k = size_v - size_w digits. */
+ k = size_v - size_w;
+ assert(k >= 0);
+ a = _PyLong_New(k);
+ if (a == NULL) {
+ Py_DECREF(w);
+ Py_DECREF(v);
+ *prem = NULL;
+ return NULL;
+ }
+ v0 = v->ob_digit;
+ w0 = w->ob_digit;
+ wm1 = w0[size_w-1];
+ wm2 = w0[size_w-2];
+ for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
+ /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
+ single-digit quotient q, remainder in vk[0:size_w]. */
+
+ SIGCHECK({
+ Py_DECREF(a);
+ Py_DECREF(w);
+ Py_DECREF(v);
+ *prem = NULL;
+ return NULL;
+ })
+
+ /* estimate quotient digit q; may overestimate by 1 (rare) */
+ vtop = vk[size_w];
+ assert(vtop <= wm1);
+ vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
+ q = (digit)(vv / wm1);
+ r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
+ while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
+ | vk[size_w-2])) {
+ --q;
+ r += wm1;
+ if (r >= PyLong_BASE)
+ break;
+ }
+ assert(q <= PyLong_BASE);
+
+ /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
+ zhi = 0;
+ for (i = 0; i < size_w; ++i) {
+ /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
+ -PyLong_BASE * q <= z < PyLong_BASE */
+ z = (sdigit)vk[i] + zhi -
+ (stwodigits)q * (stwodigits)w0[i];
+ vk[i] = (digit)z & PyLong_MASK;
+ zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
+ z, PyLong_SHIFT);
+ }
+
+ /* add w back if q was too large (this branch taken rarely) */
+ assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
+ if ((sdigit)vtop + zhi < 0) {
+ carry = 0;
+ for (i = 0; i < size_w; ++i) {
+ carry += vk[i] + w0[i];
+ vk[i] = carry & PyLong_MASK;
+ carry >>= PyLong_SHIFT;
+ }
+ --q;
+ }
+
+ /* store quotient digit */
+ assert(q < PyLong_BASE);
+ *--ak = q;
+ }
+
+ /* unshift remainder; we reuse w to store the result */
+ carry = v_rshift(w0, v0, size_w, d);
+ assert(carry==0);
+ Py_DECREF(v);
+
+ *prem = long_normalize(w);
+ return long_normalize(a);
}
/* Methods */
@@ -2411,121 +2411,121 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
static void
long_dealloc(PyObject *v)
{
- Py_TYPE(v)->tp_free(v);
+ Py_TYPE(v)->tp_free(v);
}
static PyObject *
long_repr(PyObject *v)
{
- return _PyLong_Format(v, 10);
+ return _PyLong_Format(v, 10);
}
static int
long_compare(PyLongObject *a, PyLongObject *b)
{
- Py_ssize_t sign;
-
- if (Py_SIZE(a) != Py_SIZE(b)) {
- if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
- sign = 0;
- else
- sign = Py_SIZE(a) - Py_SIZE(b);
- }
- else {
- Py_ssize_t i = ABS(Py_SIZE(a));
- while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
- ;
- if (i < 0)
- sign = 0;
- else {
- sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
- if (Py_SIZE(a) < 0)
- sign = -sign;
- }
- }
- return sign < 0 ? -1 : sign > 0 ? 1 : 0;
+ Py_ssize_t sign;
+
+ if (Py_SIZE(a) != Py_SIZE(b)) {
+ if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
+ sign = 0;
+ else
+ sign = Py_SIZE(a) - Py_SIZE(b);
+ }
+ else {
+ Py_ssize_t i = ABS(Py_SIZE(a));
+ while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
+ ;
+ if (i < 0)
+ sign = 0;
+ else {
+ sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
+ if (Py_SIZE(a) < 0)
+ sign = -sign;
+ }
+ }
+ return sign < 0 ? -1 : sign > 0 ? 1 : 0;
}
#define TEST_COND(cond) \
- ((cond) ? Py_True : Py_False)
+ ((cond) ? Py_True : Py_False)
static PyObject *
long_richcompare(PyObject *self, PyObject *other, int op)
{
- int result;
- PyObject *v;
- CHECK_BINOP(self, other);
- if (self == other)
- result = 0;
- else
- result = long_compare((PyLongObject*)self, (PyLongObject*)other);
- /* Convert the return value to a Boolean */
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- case Py_LE:
- v = TEST_COND(result <= 0);
- break;
- case Py_GE:
- v = TEST_COND(result >= 0);
- break;
- case Py_LT:
- v = TEST_COND(result == -1);
- break;
- case Py_GT:
- v = TEST_COND(result == 1);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
- Py_INCREF(v);
- return v;
+ int result;
+ PyObject *v;
+ CHECK_BINOP(self, other);
+ if (self == other)
+ result = 0;
+ else
+ result = long_compare((PyLongObject*)self, (PyLongObject*)other);
+ /* Convert the return value to a Boolean */
+ switch (op) {
+ case Py_EQ:
+ v = TEST_COND(result == 0);
+ break;
+ case Py_NE:
+ v = TEST_COND(result != 0);
+ break;
+ case Py_LE:
+ v = TEST_COND(result <= 0);
+ break;
+ case Py_GE:
+ v = TEST_COND(result >= 0);
+ break;
+ case Py_LT:
+ v = TEST_COND(result == -1);
+ break;
+ case Py_GT:
+ v = TEST_COND(result == 1);
+ break;
+ default:
+ PyErr_BadArgument();
+ return NULL;
+ }
+ Py_INCREF(v);
+ return v;
}
static long
long_hash(PyLongObject *v)
{
- unsigned long x;
- Py_ssize_t i;
- int sign;
-
- /* This is designed so that Python ints and longs with the
- same value hash to the same value, otherwise comparisons
- of mapping keys will turn out weird */
- i = Py_SIZE(v);
- switch(i) {
- case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
- case 0: return 0;
- case 1: return v->ob_digit[0];
- }
- sign = 1;
- x = 0;
- if (i < 0) {
- sign = -1;
- i = -(i);
- }
- /* The following loop produces a C unsigned long x such that x is
- congruent to the absolute value of v modulo ULONG_MAX. The
- resulting x is nonzero if and only if v is. */
- while (--i >= 0) {
- /* Force a native long #-bits (32 or 64) circular shift */
- x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
- x += v->ob_digit[i];
- /* If the addition above overflowed we compensate by
- incrementing. This preserves the value modulo
- ULONG_MAX. */
- if (x < v->ob_digit[i])
- x++;
- }
- x = x * sign;
- if (x == (unsigned long)-1)
- x = (unsigned long)-2;
- return (long)x;
+ unsigned long x;
+ Py_ssize_t i;
+ int sign;
+
+ /* This is designed so that Python ints and longs with the
+ same value hash to the same value, otherwise comparisons
+ of mapping keys will turn out weird */
+ i = Py_SIZE(v);
+ switch(i) {
+ case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
+ case 0: return 0;
+ case 1: return v->ob_digit[0];
+ }
+ sign = 1;
+ x = 0;
+ if (i < 0) {
+ sign = -1;
+ i = -(i);
+ }
+ /* The following loop produces a C unsigned long x such that x is
+ congruent to the absolute value of v modulo ULONG_MAX. The
+ resulting x is nonzero if and only if v is. */
+ while (--i >= 0) {
+ /* Force a native long #-bits (32 or 64) circular shift */
+ x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
+ x += v->ob_digit[i];
+ /* If the addition above overflowed we compensate by
+ incrementing. This preserves the value modulo
+ ULONG_MAX. */
+ if (x < v->ob_digit[i])
+ x++;
+ }
+ x = x * sign;
+ if (x == (unsigned long)-1)
+ x = (unsigned long)-2;
+ return (long)x;
}
@@ -2534,33 +2534,33 @@ long_hash(PyLongObject *v)
static PyLongObject *
x_add(PyLongObject *a, PyLongObject *b)
{
- Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
- PyLongObject *z;
- Py_ssize_t i;
- digit carry = 0;
-
- /* Ensure a is the larger of the two: */
- if (size_a < size_b) {
- { PyLongObject *temp = a; a = b; b = temp; }
- { Py_ssize_t size_temp = size_a;
- size_a = size_b;
- size_b = size_temp; }
- }
- z = _PyLong_New(size_a+1);
- if (z == NULL)
- return NULL;
- for (i = 0; i < size_b; ++i) {
- carry += a->ob_digit[i] + b->ob_digit[i];
- z->ob_digit[i] = carry & PyLong_MASK;
- carry >>= PyLong_SHIFT;
- }
- for (; i < size_a; ++i) {
- carry += a->ob_digit[i];
- z->ob_digit[i] = carry & PyLong_MASK;
- carry >>= PyLong_SHIFT;
- }
- z->ob_digit[i] = carry;
- return long_normalize(z);
+ Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
+ PyLongObject *z;
+ Py_ssize_t i;
+ digit carry = 0;
+
+ /* Ensure a is the larger of the two: */
+ if (size_a < size_b) {
+ { PyLongObject *temp = a; a = b; b = temp; }
+ { Py_ssize_t size_temp = size_a;
+ size_a = size_b;
+ size_b = size_temp; }
+ }
+ z = _PyLong_New(size_a+1);
+ if (z == NULL)
+ return NULL;
+ for (i = 0; i < size_b; ++i) {
+ carry += a->ob_digit[i] + b->ob_digit[i];
+ z->ob_digit[i] = carry & PyLong_MASK;
+ carry >>= PyLong_SHIFT;
+ }
+ for (; i < size_a; ++i) {
+ carry += a->ob_digit[i];
+ z->ob_digit[i] = carry & PyLong_MASK;
+ carry >>= PyLong_SHIFT;
+ }
+ z->ob_digit[i] = carry;
+ return long_normalize(z);
}
/* Subtract the absolute values of two integers. */
@@ -2568,113 +2568,113 @@ x_add(PyLongObject *a, PyLongObject *b)
static PyLongObject *
x_sub(PyLongObject *a, PyLongObject *b)
{
- Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
- PyLongObject *z;
- Py_ssize_t i;
- int sign = 1;
- digit borrow = 0;
-
- /* Ensure a is the larger of the two: */
- if (size_a < size_b) {
- sign = -1;
- { PyLongObject *temp = a; a = b; b = temp; }
- { Py_ssize_t size_temp = size_a;
- size_a = size_b;
- size_b = size_temp; }
- }
- else if (size_a == size_b) {
- /* Find highest digit where a and b differ: */
- i = size_a;
- while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
- ;
- if (i < 0)
- return (PyLongObject *)PyLong_FromLong(0);
- if (a->ob_digit[i] < b->ob_digit[i]) {
- sign = -1;
- { PyLongObject *temp = a; a = b; b = temp; }
- }
- size_a = size_b = i+1;
- }
- z = _PyLong_New(size_a);
- if (z == NULL)
- return NULL;
- for (i = 0; i < size_b; ++i) {
- /* The following assumes unsigned arithmetic
- works module 2**N for some N>PyLong_SHIFT. */
- borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
- z->ob_digit[i] = borrow & PyLong_MASK;
- borrow >>= PyLong_SHIFT;
- borrow &= 1; /* Keep only one sign bit */
- }
- for (; i < size_a; ++i) {
- borrow = a->ob_digit[i] - borrow;
- z->ob_digit[i] = borrow & PyLong_MASK;
- borrow >>= PyLong_SHIFT;
- borrow &= 1; /* Keep only one sign bit */
- }
- assert(borrow == 0);
- if (sign < 0)
- NEGATE(z);
- return long_normalize(z);
+ Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
+ PyLongObject *z;
+ Py_ssize_t i;
+ int sign = 1;
+ digit borrow = 0;
+
+ /* Ensure a is the larger of the two: */
+ if (size_a < size_b) {
+ sign = -1;
+ { PyLongObject *temp = a; a = b; b = temp; }
+ { Py_ssize_t size_temp = size_a;
+ size_a = size_b;
+ size_b = size_temp; }
+ }
+ else if (size_a == size_b) {
+ /* Find highest digit where a and b differ: */
+ i = size_a;
+ while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
+ ;
+ if (i < 0)
+ return (PyLongObject *)PyLong_FromLong(0);
+ if (a->ob_digit[i] < b->ob_digit[i]) {
+ sign = -1;
+ { PyLongObject *temp = a; a = b; b = temp; }
+ }
+ size_a = size_b = i+1;
+ }
+ z = _PyLong_New(size_a);
+ if (z == NULL)
+ return NULL;
+ for (i = 0; i < size_b; ++i) {
+ /* The following assumes unsigned arithmetic
+ works module 2**N for some N>PyLong_SHIFT. */
+ borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
+ z->ob_digit[i] = borrow & PyLong_MASK;
+ borrow >>= PyLong_SHIFT;
+ borrow &= 1; /* Keep only one sign bit */
+ }
+ for (; i < size_a; ++i) {
+ borrow = a->ob_digit[i] - borrow;
+ z->ob_digit[i] = borrow & PyLong_MASK;
+ borrow >>= PyLong_SHIFT;
+ borrow &= 1; /* Keep only one sign bit */
+ }
+ assert(borrow == 0);
+ if (sign < 0)
+ NEGATE(z);
+ return long_normalize(z);
}
static PyObject *
long_add(PyLongObject *a, PyLongObject *b)
{
- PyLongObject *z;
-
- CHECK_BINOP(a, b);
-
- if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
- PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) +
- MEDIUM_VALUE(b));
- return result;
- }
- if (Py_SIZE(a) < 0) {
- if (Py_SIZE(b) < 0) {
- z = x_add(a, b);
- if (z != NULL && Py_SIZE(z) != 0)
- Py_SIZE(z) = -(Py_SIZE(z));
- }
- else
- z = x_sub(b, a);
- }
- else {
- if (Py_SIZE(b) < 0)
- z = x_sub(a, b);
- else
- z = x_add(a, b);
- }
- return (PyObject *)z;
+ PyLongObject *z;
+
+ CHECK_BINOP(a, b);
+
+ if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
+ PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) +
+ MEDIUM_VALUE(b));
+ return result;
+ }
+ if (Py_SIZE(a) < 0) {
+ if (Py_SIZE(b) < 0) {
+ z = x_add(a, b);
+ if (z != NULL && Py_SIZE(z) != 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
+ }
+ else
+ z = x_sub(b, a);
+ }
+ else {
+ if (Py_SIZE(b) < 0)
+ z = x_sub(a, b);
+ else
+ z = x_add(a, b);
+ }
+ return (PyObject *)z;
}
static PyObject *
long_sub(PyLongObject *a, PyLongObject *b)
{
- PyLongObject *z;
-
- CHECK_BINOP(a, b);
-
- if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
- PyObject* r;
- r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b));
- return r;
- }
- if (Py_SIZE(a) < 0) {
- if (Py_SIZE(b) < 0)
- z = x_sub(a, b);
- else
- z = x_add(a, b);
- if (z != NULL && Py_SIZE(z) != 0)
- Py_SIZE(z) = -(Py_SIZE(z));
- }
- else {
- if (Py_SIZE(b) < 0)
- z = x_add(a, b);
- else
- z = x_sub(a, b);
- }
- return (PyObject *)z;
+ PyLongObject *z;
+
+ CHECK_BINOP(a, b);
+
+ if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
+ PyObject* r;
+ r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b));
+ return r;
+ }
+ if (Py_SIZE(a) < 0) {
+ if (Py_SIZE(b) < 0)
+ z = x_sub(a, b);
+ else
+ z = x_add(a, b);
+ if (z != NULL && Py_SIZE(z) != 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
+ }
+ else {
+ if (Py_SIZE(b) < 0)
+ z = x_add(a, b);
+ else
+ z = x_sub(a, b);
+ }
+ return (PyObject *)z;
}
/* Grade school multiplication, ignoring the signs.
@@ -2683,85 +2683,85 @@ long_sub(PyLongObject *a, PyLongObject *b)
static PyLongObject *
x_mul(PyLongObject *a, PyLongObject *b)
{
- PyLongObject *z;
- Py_ssize_t size_a = ABS(Py_SIZE(a));
- Py_ssize_t size_b = ABS(Py_SIZE(b));
- Py_ssize_t i;
-
- z = _PyLong_New(size_a + size_b);
- if (z == NULL)
- return NULL;
-
- memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
- if (a == b) {
- /* Efficient squaring per HAC, Algorithm 14.16:
- * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
- * Gives slightly less than a 2x speedup when a == b,
- * via exploiting that each entry in the multiplication
- * pyramid appears twice (except for the size_a squares).
- */
- for (i = 0; i < size_a; ++i) {
- twodigits carry;
- twodigits f = a->ob_digit[i];
- digit *pz = z->ob_digit + (i << 1);
- digit *pa = a->ob_digit + i + 1;
- digit *paend = a->ob_digit + size_a;
-
- SIGCHECK({
- Py_DECREF(z);
- return NULL;
- })
-
- carry = *pz + f * f;
- *pz++ = (digit)(carry & PyLong_MASK);
- carry >>= PyLong_SHIFT;
- assert(carry <= PyLong_MASK);
-
- /* Now f is added in twice in each column of the
- * pyramid it appears. Same as adding f<<1 once.
- */
- f <<= 1;
- while (pa < paend) {
- carry += *pz + *pa++ * f;
- *pz++ = (digit)(carry & PyLong_MASK);
- carry >>= PyLong_SHIFT;
- assert(carry <= (PyLong_MASK << 1));
- }
- if (carry) {
- carry += *pz;
- *pz++ = (digit)(carry & PyLong_MASK);
- carry >>= PyLong_SHIFT;
- }
- if (carry)
- *pz += (digit)(carry & PyLong_MASK);
- assert((carry >> PyLong_SHIFT) == 0);
- }
- }
- else { /* a is not the same as b -- gradeschool long mult */
- for (i = 0; i < size_a; ++i) {
- twodigits carry = 0;
- twodigits f = a->ob_digit[i];
- digit *pz = z->ob_digit + i;
- digit *pb = b->ob_digit;
- digit *pbend = b->ob_digit + size_b;
-
- SIGCHECK({
- Py_DECREF(z);
- return NULL;
- })
-
- while (pb < pbend) {
- carry += *pz + *pb++ * f;
- *pz++ = (digit)(carry & PyLong_MASK);
- carry >>= PyLong_SHIFT;
- assert(carry <= PyLong_MASK);
- }
- if (carry)
- *pz += (digit)(carry & PyLong_MASK);
- assert((carry >> PyLong_SHIFT) == 0);
- }
- }
- return long_normalize(z);
+ PyLongObject *z;
+ Py_ssize_t size_a = ABS(Py_SIZE(a));
+ Py_ssize_t size_b = ABS(Py_SIZE(b));
+ Py_ssize_t i;
+
+ z = _PyLong_New(size_a + size_b);
+ if (z == NULL)
+ return NULL;
+
+ memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
+ if (a == b) {
+ /* Efficient squaring per HAC, Algorithm 14.16:
+ * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
+ * Gives slightly less than a 2x speedup when a == b,
+ * via exploiting that each entry in the multiplication
+ * pyramid appears twice (except for the size_a squares).
+ */
+ for (i = 0; i < size_a; ++i) {
+ twodigits carry;
+ twodigits f = a->ob_digit[i];
+ digit *pz = z->ob_digit + (i << 1);
+ digit *pa = a->ob_digit + i + 1;
+ digit *paend = a->ob_digit + size_a;
+
+ SIGCHECK({
+ Py_DECREF(z);
+ return NULL;
+ })
+
+ carry = *pz + f * f;
+ *pz++ = (digit)(carry & PyLong_MASK);
+ carry >>= PyLong_SHIFT;
+ assert(carry <= PyLong_MASK);
+
+ /* Now f is added in twice in each column of the
+ * pyramid it appears. Same as adding f<<1 once.
+ */
+ f <<= 1;
+ while (pa < paend) {
+ carry += *pz + *pa++ * f;
+ *pz++ = (digit)(carry & PyLong_MASK);
+ carry >>= PyLong_SHIFT;
+ assert(carry <= (PyLong_MASK << 1));
+ }
+ if (carry) {
+ carry += *pz;
+ *pz++ = (digit)(carry & PyLong_MASK);
+ carry >>= PyLong_SHIFT;
+ }
+ if (carry)
+ *pz += (digit)(carry & PyLong_MASK);
+ assert((carry >> PyLong_SHIFT) == 0);
+ }
+ }
+ else { /* a is not the same as b -- gradeschool long mult */
+ for (i = 0; i < size_a; ++i) {
+ twodigits carry = 0;
+ twodigits f = a->ob_digit[i];
+ digit *pz = z->ob_digit + i;
+ digit *pb = b->ob_digit;
+ digit *pbend = b->ob_digit + size_b;
+
+ SIGCHECK({
+ Py_DECREF(z);
+ return NULL;
+ })
+
+ while (pb < pbend) {
+ carry += *pz + *pb++ * f;
+ *pz++ = (digit)(carry & PyLong_MASK);
+ carry >>= PyLong_SHIFT;
+ assert(carry <= PyLong_MASK);
+ }
+ if (carry)
+ *pz += (digit)(carry & PyLong_MASK);
+ assert((carry >> PyLong_SHIFT) == 0);
+ }
+ }
+ return long_normalize(z);
}
/* A helper for Karatsuba multiplication (k_mul).
@@ -2774,26 +2774,26 @@ x_mul(PyLongObject *a, PyLongObject *b)
static int
kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
{
- PyLongObject *hi, *lo;
- Py_ssize_t size_lo, size_hi;
- const Py_ssize_t size_n = ABS(Py_SIZE(n));
-
- size_lo = MIN(size_n, size);
- size_hi = size_n - size_lo;
-
- if ((hi = _PyLong_New(size_hi)) == NULL)
- return -1;
- if ((lo = _PyLong_New(size_lo)) == NULL) {
- Py_DECREF(hi);
- return -1;
- }
-
- memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
- memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
-
- *high = long_normalize(hi);
- *low = long_normalize(lo);
- return 0;
+ PyLongObject *hi, *lo;
+ Py_ssize_t size_lo, size_hi;
+ const Py_ssize_t size_n = ABS(Py_SIZE(n));
+
+ size_lo = MIN(size_n, size);
+ size_hi = size_n - size_lo;
+
+ if ((hi = _PyLong_New(size_hi)) == NULL)
+ return -1;
+ if ((lo = _PyLong_New(size_lo)) == NULL) {
+ Py_DECREF(hi);
+ return -1;
+ }
+
+ memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
+ memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
+
+ *high = long_normalize(hi);
+ *low = long_normalize(lo);
+ return 0;
}
static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
@@ -2805,169 +2805,169 @@ static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
static PyLongObject *
k_mul(PyLongObject *a, PyLongObject *b)
{
- Py_ssize_t asize = ABS(Py_SIZE(a));
- Py_ssize_t bsize = ABS(Py_SIZE(b));
- PyLongObject *ah = NULL;
- PyLongObject *al = NULL;
- PyLongObject *bh = NULL;
- PyLongObject *bl = NULL;
- PyLongObject *ret = NULL;
- PyLongObject *t1, *t2, *t3;
- Py_ssize_t shift; /* the number of digits we split off */
- Py_ssize_t i;
-
- /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
- * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
- * Then the original product is
- * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
- * By picking X to be a power of 2, "*X" is just shifting, and it's
- * been reduced to 3 multiplies on numbers half the size.
- */
-
- /* We want to split based on the larger number; fiddle so that b
- * is largest.
- */
- if (asize > bsize) {
- t1 = a;
- a = b;
- b = t1;
-
- i = asize;
- asize = bsize;
- bsize = i;
- }
-
- /* Use gradeschool math when either number is too small. */
- i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
- if (asize <= i) {
- if (asize == 0)
- return (PyLongObject *)PyLong_FromLong(0);
- else
- return x_mul(a, b);
- }
-
- /* If a is small compared to b, splitting on b gives a degenerate
- * case with ah==0, and Karatsuba may be (even much) less efficient
- * than "grade school" then. However, we can still win, by viewing
- * b as a string of "big digits", each of width a->ob_size. That
- * leads to a sequence of balanced calls to k_mul.
- */
- if (2 * asize <= bsize)
- return k_lopsided_mul(a, b);
-
- /* Split a & b into hi & lo pieces. */
- shift = bsize >> 1;
- if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
- assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
-
- if (a == b) {
- bh = ah;
- bl = al;
- Py_INCREF(bh);
- Py_INCREF(bl);
- }
- else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
-
- /* The plan:
- * 1. Allocate result space (asize + bsize digits: that's always
- * enough).
- * 2. Compute ah*bh, and copy into result at 2*shift.
- * 3. Compute al*bl, and copy into result at 0. Note that this
- * can't overlap with #2.
- * 4. Subtract al*bl from the result, starting at shift. This may
- * underflow (borrow out of the high digit), but we don't care:
- * we're effectively doing unsigned arithmetic mod
- * BASE**(sizea + sizeb), and so long as the *final* result fits,
- * borrows and carries out of the high digit can be ignored.
- * 5. Subtract ah*bh from the result, starting at shift.
- * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
- * at shift.
- */
-
- /* 1. Allocate result space. */
- ret = _PyLong_New(asize + bsize);
- if (ret == NULL) goto fail;
+ Py_ssize_t asize = ABS(Py_SIZE(a));
+ Py_ssize_t bsize = ABS(Py_SIZE(b));
+ PyLongObject *ah = NULL;
+ PyLongObject *al = NULL;
+ PyLongObject *bh = NULL;
+ PyLongObject *bl = NULL;
+ PyLongObject *ret = NULL;
+ PyLongObject *t1, *t2, *t3;
+ Py_ssize_t shift; /* the number of digits we split off */
+ Py_ssize_t i;
+
+ /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
+ * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
+ * Then the original product is
+ * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
+ * By picking X to be a power of 2, "*X" is just shifting, and it's
+ * been reduced to 3 multiplies on numbers half the size.
+ */
+
+ /* We want to split based on the larger number; fiddle so that b
+ * is largest.
+ */
+ if (asize > bsize) {
+ t1 = a;
+ a = b;
+ b = t1;
+
+ i = asize;
+ asize = bsize;
+ bsize = i;
+ }
+
+ /* Use gradeschool math when either number is too small. */
+ i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
+ if (asize <= i) {
+ if (asize == 0)
+ return (PyLongObject *)PyLong_FromLong(0);
+ else
+ return x_mul(a, b);
+ }
+
+ /* If a is small compared to b, splitting on b gives a degenerate
+ * case with ah==0, and Karatsuba may be (even much) less efficient
+ * than "grade school" then. However, we can still win, by viewing
+ * b as a string of "big digits", each of width a->ob_size. That
+ * leads to a sequence of balanced calls to k_mul.
+ */
+ if (2 * asize <= bsize)
+ return k_lopsided_mul(a, b);
+
+ /* Split a & b into hi & lo pieces. */
+ shift = bsize >> 1;
+ if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
+ assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
+
+ if (a == b) {
+ bh = ah;
+ bl = al;
+ Py_INCREF(bh);
+ Py_INCREF(bl);
+ }
+ else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
+
+ /* The plan:
+ * 1. Allocate result space (asize + bsize digits: that's always
+ * enough).
+ * 2. Compute ah*bh, and copy into result at 2*shift.
+ * 3. Compute al*bl, and copy into result at 0. Note that this
+ * can't overlap with #2.
+ * 4. Subtract al*bl from the result, starting at shift. This may
+ * underflow (borrow out of the high digit), but we don't care:
+ * we're effectively doing unsigned arithmetic mod
+ * BASE**(sizea + sizeb), and so long as the *final* result fits,
+ * borrows and carries out of the high digit can be ignored.
+ * 5. Subtract ah*bh from the result, starting at shift.
+ * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
+ * at shift.
+ */
+
+ /* 1. Allocate result space. */
+ ret = _PyLong_New(asize + bsize);
+ if (ret == NULL) goto fail;
#ifdef Py_DEBUG
- /* Fill with trash, to catch reference to uninitialized digits. */
- memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
+ /* Fill with trash, to catch reference to uninitialized digits. */
+ memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
#endif
- /* 2. t1 <- ah*bh, and copy into high digits of result. */
- if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
- assert(Py_SIZE(t1) >= 0);
- assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
- memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
- Py_SIZE(t1) * sizeof(digit));
-
- /* Zero-out the digits higher than the ah*bh copy. */
- i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
- if (i)
- memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
- i * sizeof(digit));
-
- /* 3. t2 <- al*bl, and copy into the low digits. */
- if ((t2 = k_mul(al, bl)) == NULL) {
- Py_DECREF(t1);
- goto fail;
- }
- assert(Py_SIZE(t2) >= 0);
- assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
- memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
-
- /* Zero out remaining digits. */
- i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
- if (i)
- memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
-
- /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
- * because it's fresher in cache.
- */
- i = Py_SIZE(ret) - shift; /* # digits after shift */
- (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
- Py_DECREF(t2);
-
- (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
- Py_DECREF(t1);
-
- /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
- if ((t1 = x_add(ah, al)) == NULL) goto fail;
- Py_DECREF(ah);
- Py_DECREF(al);
- ah = al = NULL;
-
- if (a == b) {
- t2 = t1;
- Py_INCREF(t2);
- }
- else if ((t2 = x_add(bh, bl)) == NULL) {
- Py_DECREF(t1);
- goto fail;
- }
- Py_DECREF(bh);
- Py_DECREF(bl);
- bh = bl = NULL;
-
- t3 = k_mul(t1, t2);
- Py_DECREF(t1);
- Py_DECREF(t2);
- if (t3 == NULL) goto fail;
- assert(Py_SIZE(t3) >= 0);
-
- /* Add t3. It's not obvious why we can't run out of room here.
- * See the (*) comment after this function.
- */
- (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
- Py_DECREF(t3);
-
- return long_normalize(ret);
+ /* 2. t1 <- ah*bh, and copy into high digits of result. */
+ if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
+ assert(Py_SIZE(t1) >= 0);
+ assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
+ memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
+ Py_SIZE(t1) * sizeof(digit));
+
+ /* Zero-out the digits higher than the ah*bh copy. */
+ i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
+ if (i)
+ memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
+ i * sizeof(digit));
+
+ /* 3. t2 <- al*bl, and copy into the low digits. */
+ if ((t2 = k_mul(al, bl)) == NULL) {
+ Py_DECREF(t1);
+ goto fail;
+ }
+ assert(Py_SIZE(t2) >= 0);
+ assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
+ memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
+
+ /* Zero out remaining digits. */
+ i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
+ if (i)
+ memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
+
+ /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
+ * because it's fresher in cache.
+ */
+ i = Py_SIZE(ret) - shift; /* # digits after shift */
+ (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
+ Py_DECREF(t2);
+
+ (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
+ Py_DECREF(t1);
+
+ /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
+ if ((t1 = x_add(ah, al)) == NULL) goto fail;
+ Py_DECREF(ah);
+ Py_DECREF(al);
+ ah = al = NULL;
+
+ if (a == b) {
+ t2 = t1;
+ Py_INCREF(t2);
+ }
+ else if ((t2 = x_add(bh, bl)) == NULL) {
+ Py_DECREF(t1);
+ goto fail;
+ }
+ Py_DECREF(bh);
+ Py_DECREF(bl);
+ bh = bl = NULL;
+
+ t3 = k_mul(t1, t2);
+ Py_DECREF(t1);
+ Py_DECREF(t2);
+ if (t3 == NULL) goto fail;
+ assert(Py_SIZE(t3) >= 0);
+
+ /* Add t3. It's not obvious why we can't run out of room here.
+ * See the (*) comment after this function.
+ */
+ (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
+ Py_DECREF(t3);
+
+ return long_normalize(ret);
fail:
- Py_XDECREF(ret);
- Py_XDECREF(ah);
- Py_XDECREF(al);
- Py_XDECREF(bh);
- Py_XDECREF(bl);
- return NULL;
+ Py_XDECREF(ret);
+ Py_XDECREF(ah);
+ Py_XDECREF(al);
+ Py_XDECREF(bh);
+ Py_XDECREF(bl);
+ return NULL;
}
/* (*) Why adding t3 can't "run out of room" above.
@@ -3026,85 +3026,85 @@ ah*bh and al*bl too.
static PyLongObject *
k_lopsided_mul(PyLongObject *a, PyLongObject *b)
{
- const Py_ssize_t asize = ABS(Py_SIZE(a));
- Py_ssize_t bsize = ABS(Py_SIZE(b));
- Py_ssize_t nbdone; /* # of b digits already multiplied */
- PyLongObject *ret;
- PyLongObject *bslice = NULL;
-
- assert(asize > KARATSUBA_CUTOFF);
- assert(2 * asize <= bsize);
-
- /* Allocate result space, and zero it out. */
- ret = _PyLong_New(asize + bsize);
- if (ret == NULL)
- return NULL;
- memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
-
- /* Successive slices of b are copied into bslice. */
- bslice = _PyLong_New(asize);
- if (bslice == NULL)
- goto fail;
-
- nbdone = 0;
- while (bsize > 0) {
- PyLongObject *product;
- const Py_ssize_t nbtouse = MIN(bsize, asize);
-
- /* Multiply the next slice of b by a. */
- memcpy(bslice->ob_digit, b->ob_digit + nbdone,
- nbtouse * sizeof(digit));
- Py_SIZE(bslice) = nbtouse;
- product = k_mul(a, bslice);
- if (product == NULL)
- goto fail;
-
- /* Add into result. */
- (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
- product->ob_digit, Py_SIZE(product));
- Py_DECREF(product);
-
- bsize -= nbtouse;
- nbdone += nbtouse;
- }
-
- Py_DECREF(bslice);
- return long_normalize(ret);
+ const Py_ssize_t asize = ABS(Py_SIZE(a));
+ Py_ssize_t bsize = ABS(Py_SIZE(b));
+ Py_ssize_t nbdone; /* # of b digits already multiplied */
+ PyLongObject *ret;
+ PyLongObject *bslice = NULL;
+
+ assert(asize > KARATSUBA_CUTOFF);
+ assert(2 * asize <= bsize);
+
+ /* Allocate result space, and zero it out. */
+ ret = _PyLong_New(asize + bsize);
+ if (ret == NULL)
+ return NULL;
+ memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
+
+ /* Successive slices of b are copied into bslice. */
+ bslice = _PyLong_New(asize);
+ if (bslice == NULL)
+ goto fail;
+
+ nbdone = 0;
+ while (bsize > 0) {
+ PyLongObject *product;
+ const Py_ssize_t nbtouse = MIN(bsize, asize);
+
+ /* Multiply the next slice of b by a. */
+ memcpy(bslice->ob_digit, b->ob_digit + nbdone,
+ nbtouse * sizeof(digit));
+ Py_SIZE(bslice) = nbtouse;
+ product = k_mul(a, bslice);
+ if (product == NULL)
+ goto fail;
+
+ /* Add into result. */
+ (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
+ product->ob_digit, Py_SIZE(product));
+ Py_DECREF(product);
+
+ bsize -= nbtouse;
+ nbdone += nbtouse;
+ }
+
+ Py_DECREF(bslice);
+ return long_normalize(ret);
fail:
- Py_DECREF(ret);
- Py_XDECREF(bslice);
- return NULL;
+ Py_DECREF(ret);
+ Py_XDECREF(bslice);
+ return NULL;
}
static PyObject *
long_mul(PyLongObject *a, PyLongObject *b)
{
- PyLongObject *z;
+ PyLongObject *z;
- CHECK_BINOP(a, b);
+ CHECK_BINOP(a, b);
- /* fast path for single-digit multiplication */
- if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
- stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b);
+ /* fast path for single-digit multiplication */
+ if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
+ stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b);
#ifdef HAVE_LONG_LONG
- return PyLong_FromLongLong((PY_LONG_LONG)v);
+ return PyLong_FromLongLong((PY_LONG_LONG)v);
#else
- /* if we don't have long long then we're almost certainly
- using 15-bit digits, so v will fit in a long. In the
- unlikely event that we're using 30-bit digits on a platform
- without long long, a large v will just cause us to fall
- through to the general multiplication code below. */
- if (v >= LONG_MIN && v <= LONG_MAX)
- return PyLong_FromLong((long)v);
+ /* if we don't have long long then we're almost certainly
+ using 15-bit digits, so v will fit in a long. In the
+ unlikely event that we're using 30-bit digits on a platform
+ without long long, a large v will just cause us to fall
+ through to the general multiplication code below. */
+ if (v >= LONG_MIN && v <= LONG_MAX)
+ return PyLong_FromLong((long)v);
#endif
- }
+ }
- z = k_mul(a, b);
- /* Negate if exactly one of the inputs is negative. */
- if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
- NEGATE(z);
- return (PyObject *)z;
+ z = k_mul(a, b);
+ /* Negate if exactly one of the inputs is negative. */
+ if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
+ NEGATE(z);
+ return (PyObject *)z;
}
/* The / and % operators are now defined in terms of divmod().
@@ -3113,11 +3113,11 @@ long_mul(PyLongObject *a, PyLongObject *b)
|a| by |b|, with the sign of a. This is also expressed
as a - b*trunc(a/b), if trunc truncates towards zero.
Some examples:
- a b a rem b a mod b
- 13 10 3 3
- -13 10 -3 7
- 13 -10 3 -7
- -13 -10 -3 -3
+ a b a rem b a mod b
+ 13 10 3 3
+ -13 10 -3 7
+ 13 -10 3 -7
+ -13 -10 -3 -3
So, to get from rem to mod, we have to add b if a and b
have different signs. We then subtract one from the 'div'
part of the outcome to keep the invariant intact. */
@@ -3130,480 +3130,480 @@ long_mul(PyLongObject *a, PyLongObject *b)
*/
static int
l_divmod(PyLongObject *v, PyLongObject *w,
- PyLongObject **pdiv, PyLongObject **pmod)
+ PyLongObject **pdiv, PyLongObject **pmod)
{
- PyLongObject *div, *mod;
-
- if (long_divrem(v, w, &div, &mod) < 0)
- return -1;
- if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
- (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
- PyLongObject *temp;
- PyLongObject *one;
- temp = (PyLongObject *) long_add(mod, w);
- Py_DECREF(mod);
- mod = temp;
- if (mod == NULL) {
- Py_DECREF(div);
- return -1;
- }
- one = (PyLongObject *) PyLong_FromLong(1L);
- if (one == NULL ||
- (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
- Py_DECREF(mod);
- Py_DECREF(div);
- Py_XDECREF(one);
- return -1;
- }
- Py_DECREF(one);
- Py_DECREF(div);
- div = temp;
- }
- if (pdiv != NULL)
- *pdiv = div;
- else
- Py_DECREF(div);
-
- if (pmod != NULL)
- *pmod = mod;
- else
- Py_DECREF(mod);
-
- return 0;
+ PyLongObject *div, *mod;
+
+ if (long_divrem(v, w, &div, &mod) < 0)
+ return -1;
+ if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
+ (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
+ PyLongObject *temp;
+ PyLongObject *one;
+ temp = (PyLongObject *) long_add(mod, w);
+ Py_DECREF(mod);
+ mod = temp;
+ if (mod == NULL) {
+ Py_DECREF(div);
+ return -1;
+ }
+ one = (PyLongObject *) PyLong_FromLong(1L);
+ if (one == NULL ||
+ (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
+ Py_DECREF(mod);
+ Py_DECREF(div);
+ Py_XDECREF(one);
+ return -1;
+ }
+ Py_DECREF(one);
+ Py_DECREF(div);
+ div = temp;
+ }
+ if (pdiv != NULL)
+ *pdiv = div;
+ else
+ Py_DECREF(div);
+
+ if (pmod != NULL)
+ *pmod = mod;
+ else
+ Py_DECREF(mod);
+
+ return 0;
}
static PyObject *
long_div(PyObject *a, PyObject *b)
{
- PyLongObject *div;
+ PyLongObject *div;
- CHECK_BINOP(a, b);
- if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
- div = NULL;
- return (PyObject *)div;
+ CHECK_BINOP(a, b);
+ if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
+ div = NULL;
+ return (PyObject *)div;
}
static PyObject *
long_true_divide(PyObject *a, PyObject *b)
{
- double ad, bd;
- int failed, aexp = -1, bexp = -1;
-
- CHECK_BINOP(a, b);
- ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
- bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
- failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
- if (failed)
- return NULL;
- /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
- but should really be set correctly after sucessful calls to
- _PyLong_AsScaledDouble() */
- assert(aexp >= 0 && bexp >= 0);
-
- if (bd == 0.0) {
- PyErr_SetString(PyExc_ZeroDivisionError,
- "int division or modulo by zero");
- return NULL;
- }
-
- /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
- ad /= bd; /* overflow/underflow impossible here */
- aexp -= bexp;
- if (aexp > INT_MAX / PyLong_SHIFT)
- goto overflow;
- else if (aexp < -(INT_MAX / PyLong_SHIFT))
- return PyFloat_FromDouble(0.0); /* underflow to 0 */
- errno = 0;
- ad = ldexp(ad, aexp * PyLong_SHIFT);
- if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
- goto overflow;
- return PyFloat_FromDouble(ad);
+ double ad, bd;
+ int failed, aexp = -1, bexp = -1;
+
+ CHECK_BINOP(a, b);
+ ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
+ bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
+ failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
+ if (failed)
+ return NULL;
+ /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
+ but should really be set correctly after sucessful calls to
+ _PyLong_AsScaledDouble() */
+ assert(aexp >= 0 && bexp >= 0);
+
+ if (bd == 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError,
+ "int division or modulo by zero");
+ return NULL;
+ }
+
+ /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
+ ad /= bd; /* overflow/underflow impossible here */
+ aexp -= bexp;
+ if (aexp > INT_MAX / PyLong_SHIFT)
+ goto overflow;
+ else if (aexp < -(INT_MAX / PyLong_SHIFT))
+ return PyFloat_FromDouble(0.0); /* underflow to 0 */
+ errno = 0;
+ ad = ldexp(ad, aexp * PyLong_SHIFT);
+ if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
+ goto overflow;
+ return PyFloat_FromDouble(ad);
overflow:
- PyErr_SetString(PyExc_OverflowError,
- "int/int too large for a float");
- return NULL;
+ PyErr_SetString(PyExc_OverflowError,
+ "int/int too large for a float");
+ return NULL;
}
static PyObject *
long_mod(PyObject *a, PyObject *b)
{
- PyLongObject *mod;
-
- CHECK_BINOP(a, b);
+ PyLongObject *mod;
+
+ CHECK_BINOP(a, b);
- if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
- mod = NULL;
- return (PyObject *)mod;
+ if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
+ mod = NULL;
+ return (PyObject *)mod;
}
static PyObject *
long_divmod(PyObject *a, PyObject *b)
{
- PyLongObject *div, *mod;
- PyObject *z;
-
- CHECK_BINOP(a, b);
-
- if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
- return NULL;
- }
- z = PyTuple_New(2);
- if (z != NULL) {
- PyTuple_SetItem(z, 0, (PyObject *) div);
- PyTuple_SetItem(z, 1, (PyObject *) mod);
- }
- else {
- Py_DECREF(div);
- Py_DECREF(mod);
- }
- return z;
+ PyLongObject *div, *mod;
+ PyObject *z;
+
+ CHECK_BINOP(a, b);
+
+ if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
+ return NULL;
+ }
+ z = PyTuple_New(2);
+ if (z != NULL) {
+ PyTuple_SetItem(z, 0, (PyObject *) div);
+ PyTuple_SetItem(z, 1, (PyObject *) mod);
+ }
+ else {
+ Py_DECREF(div);
+ Py_DECREF(mod);
+ }
+ return z;
}
/* pow(v, w, x) */
static PyObject *
long_pow(PyObject *v, PyObject *w, PyObject *x)
{
- PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
- int negativeOutput = 0; /* if x<0 return negative output */
-
- PyLongObject *z = NULL; /* accumulated result */
- Py_ssize_t i, j, k; /* counters */
- PyLongObject *temp = NULL;
-
- /* 5-ary values. If the exponent is large enough, table is
- * precomputed so that table[i] == a**i % c for i in range(32).
- */
- PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
-
- /* a, b, c = v, w, x */
- CHECK_BINOP(v, w);
- a = (PyLongObject*)v; Py_INCREF(a);
- b = (PyLongObject*)w; Py_INCREF(b);
- if (PyLong_Check(x)) {
- c = (PyLongObject *)x;
- Py_INCREF(x);
- }
- else if (x == Py_None)
- c = NULL;
- else {
- Py_DECREF(a);
- Py_DECREF(b);
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- if (Py_SIZE(b) < 0) { /* if exponent is negative */
- if (c) {
- PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
- "cannot be negative when 3rd argument specified");
- goto Error;
- }
- else {
- /* else return a float. This works because we know
- that this calls float_pow() which converts its
- arguments to double. */
- Py_DECREF(a);
- Py_DECREF(b);
- return PyFloat_Type.tp_as_number->nb_power(v, w, x);
- }
- }
-
- if (c) {
- /* if modulus == 0:
- raise ValueError() */
- if (Py_SIZE(c) == 0) {
- PyErr_SetString(PyExc_ValueError,
- "pow() 3rd argument cannot be 0");
- goto Error;
- }
-
- /* if modulus < 0:
- negativeOutput = True
- modulus = -modulus */
- if (Py_SIZE(c) < 0) {
- negativeOutput = 1;
- temp = (PyLongObject *)_PyLong_Copy(c);
- if (temp == NULL)
- goto Error;
- Py_DECREF(c);
- c = temp;
- temp = NULL;
- NEGATE(c);
- }
-
- /* if modulus == 1:
- return 0 */
- if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
- z = (PyLongObject *)PyLong_FromLong(0L);
- goto Done;
- }
-
- /* if base < 0:
- base = base % modulus
- Having the base positive just makes things easier. */
- if (Py_SIZE(a) < 0) {
- if (l_divmod(a, c, NULL, &temp) < 0)
- goto Error;
- Py_DECREF(a);
- a = temp;
- temp = NULL;
- }
- }
-
- /* At this point a, b, and c are guaranteed non-negative UNLESS
- c is NULL, in which case a may be negative. */
-
- z = (PyLongObject *)PyLong_FromLong(1L);
- if (z == NULL)
- goto Error;
-
- /* Perform a modular reduction, X = X % c, but leave X alone if c
- * is NULL.
- */
-#define REDUCE(X) \
- if (c != NULL) { \
- if (l_divmod(X, c, NULL, &temp) < 0) \
- goto Error; \
- Py_XDECREF(X); \
- X = temp; \
- temp = NULL; \
- }
-
- /* Multiply two values, then reduce the result:
- result = X*Y % c. If c is NULL, skip the mod. */
-#define MULT(X, Y, result) \
-{ \
- temp = (PyLongObject *)long_mul(X, Y); \
- if (temp == NULL) \
- goto Error; \
- Py_XDECREF(result); \
- result = temp; \
- temp = NULL; \
- REDUCE(result) \
+ PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
+ int negativeOutput = 0; /* if x<0 return negative output */
+
+ PyLongObject *z = NULL; /* accumulated result */
+ Py_ssize_t i, j, k; /* counters */
+ PyLongObject *temp = NULL;
+
+ /* 5-ary values. If the exponent is large enough, table is
+ * precomputed so that table[i] == a**i % c for i in range(32).
+ */
+ PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+
+ /* a, b, c = v, w, x */
+ CHECK_BINOP(v, w);
+ a = (PyLongObject*)v; Py_INCREF(a);
+ b = (PyLongObject*)w; Py_INCREF(b);
+ if (PyLong_Check(x)) {
+ c = (PyLongObject *)x;
+ Py_INCREF(x);
+ }
+ else if (x == Py_None)
+ c = NULL;
+ else {
+ Py_DECREF(a);
+ Py_DECREF(b);
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ if (Py_SIZE(b) < 0) { /* if exponent is negative */
+ if (c) {
+ PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
+ "cannot be negative when 3rd argument specified");
+ goto Error;
+ }
+ else {
+ /* else return a float. This works because we know
+ that this calls float_pow() which converts its
+ arguments to double. */
+ Py_DECREF(a);
+ Py_DECREF(b);
+ return PyFloat_Type.tp_as_number->nb_power(v, w, x);
+ }
+ }
+
+ if (c) {
+ /* if modulus == 0:
+ raise ValueError() */
+ if (Py_SIZE(c) == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "pow() 3rd argument cannot be 0");
+ goto Error;
+ }
+
+ /* if modulus < 0:
+ negativeOutput = True
+ modulus = -modulus */
+ if (Py_SIZE(c) < 0) {
+ negativeOutput = 1;
+ temp = (PyLongObject *)_PyLong_Copy(c);
+ if (temp == NULL)
+ goto Error;
+ Py_DECREF(c);
+ c = temp;
+ temp = NULL;
+ NEGATE(c);
+ }
+
+ /* if modulus == 1:
+ return 0 */
+ if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
+ z = (PyLongObject *)PyLong_FromLong(0L);
+ goto Done;
+ }
+
+ /* if base < 0:
+ base = base % modulus
+ Having the base positive just makes things easier. */
+ if (Py_SIZE(a) < 0) {
+ if (l_divmod(a, c, NULL, &temp) < 0)
+ goto Error;
+ Py_DECREF(a);
+ a = temp;
+ temp = NULL;
+ }
+ }
+
+ /* At this point a, b, and c are guaranteed non-negative UNLESS
+ c is NULL, in which case a may be negative. */
+
+ z = (PyLongObject *)PyLong_FromLong(1L);
+ if (z == NULL)
+ goto Error;
+
+ /* Perform a modular reduction, X = X % c, but leave X alone if c
+ * is NULL.
+ */
+#define REDUCE(X) \
+ if (c != NULL) { \
+ if (l_divmod(X, c, NULL, &temp) < 0) \
+ goto Error; \
+ Py_XDECREF(X); \
+ X = temp; \
+ temp = NULL; \
+ }
+
+ /* Multiply two values, then reduce the result:
+ result = X*Y % c. If c is NULL, skip the mod. */
+#define MULT(X, Y, result) \
+{ \
+ temp = (PyLongObject *)long_mul(X, Y); \
+ if (temp == NULL) \
+ goto Error; \
+ Py_XDECREF(result); \
+ result = temp; \
+ temp = NULL; \
+ REDUCE(result) \
}
- if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
- /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
- /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
- for (i = Py_SIZE(b) - 1; i >= 0; --i) {
- digit bi = b->ob_digit[i];
-
- for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
- MULT(z, z, z)
- if (bi & j)
- MULT(z, a, z)
- }
- }
- }
- else {
- /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
- Py_INCREF(z); /* still holds 1L */
- table[0] = z;
- for (i = 1; i < 32; ++i)
- MULT(table[i-1], a, table[i])
-
- for (i = Py_SIZE(b) - 1; i >= 0; --i) {
- const digit bi = b->ob_digit[i];
-
- for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
- const int index = (bi >> j) & 0x1f;
- for (k = 0; k < 5; ++k)
- MULT(z, z, z)
- if (index)
- MULT(z, table[index], z)
- }
- }
- }
-
- if (negativeOutput && (Py_SIZE(z) != 0)) {
- temp = (PyLongObject *)long_sub(z, c);
- if (temp == NULL)
- goto Error;
- Py_DECREF(z);
- z = temp;
- temp = NULL;
- }
- goto Done;
+ if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
+ /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
+ /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
+ for (i = Py_SIZE(b) - 1; i >= 0; --i) {
+ digit bi = b->ob_digit[i];
+
+ for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
+ MULT(z, z, z)
+ if (bi & j)
+ MULT(z, a, z)
+ }
+ }
+ }
+ else {
+ /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
+ Py_INCREF(z); /* still holds 1L */
+ table[0] = z;
+ for (i = 1; i < 32; ++i)
+ MULT(table[i-1], a, table[i])
+
+ for (i = Py_SIZE(b) - 1; i >= 0; --i) {
+ const digit bi = b->ob_digit[i];
+
+ for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
+ const int index = (bi >> j) & 0x1f;
+ for (k = 0; k < 5; ++k)
+ MULT(z, z, z)
+ if (index)
+ MULT(z, table[index], z)
+ }
+ }
+ }
+
+ if (negativeOutput && (Py_SIZE(z) != 0)) {
+ temp = (PyLongObject *)long_sub(z, c);
+ if (temp == NULL)
+ goto Error;
+ Py_DECREF(z);
+ z = temp;
+ temp = NULL;
+ }
+ goto Done;
Error:
- if (z != NULL) {
- Py_DECREF(z);
- z = NULL;
- }
- /* fall through */
+ if (z != NULL) {
+ Py_DECREF(z);
+ z = NULL;
+ }
+ /* fall through */
Done:
- if (Py_SIZE(b) > FIVEARY_CUTOFF) {
- for (i = 0; i < 32; ++i)
- Py_XDECREF(table[i]);
- }
- Py_DECREF(a);
- Py_DECREF(b);
- Py_XDECREF(c);
- Py_XDECREF(temp);
- return (PyObject *)z;
+ if (Py_SIZE(b) > FIVEARY_CUTOFF) {
+ for (i = 0; i < 32; ++i)
+ Py_XDECREF(table[i]);
+ }
+ Py_DECREF(a);
+ Py_DECREF(b);
+ Py_XDECREF(c);
+ Py_XDECREF(temp);
+ return (PyObject *)z;
}
static PyObject *
long_invert(PyLongObject *v)
{
- /* Implement ~x as -(x+1) */
- PyLongObject *x;
- PyLongObject *w;
- if (ABS(Py_SIZE(v)) <=1)
- return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
- w = (PyLongObject *)PyLong_FromLong(1L);
- if (w == NULL)
- return NULL;
- x = (PyLongObject *) long_add(v, w);
- Py_DECREF(w);
- if (x == NULL)
- return NULL;
- Py_SIZE(x) = -(Py_SIZE(x));
- return (PyObject *)maybe_small_long(x);
+ /* Implement ~x as -(x+1) */
+ PyLongObject *x;
+ PyLongObject *w;
+ if (ABS(Py_SIZE(v)) <=1)
+ return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
+ w = (PyLongObject *)PyLong_FromLong(1L);
+ if (w == NULL)
+ return NULL;
+ x = (PyLongObject *) long_add(v, w);
+ Py_DECREF(w);
+ if (x == NULL)
+ return NULL;
+ Py_SIZE(x) = -(Py_SIZE(x));
+ return (PyObject *)maybe_small_long(x);
}
static PyObject *
long_neg(PyLongObject *v)
{
- PyLongObject *z;
- if (ABS(Py_SIZE(v)) <= 1)
- return PyLong_FromLong(-MEDIUM_VALUE(v));
- z = (PyLongObject *)_PyLong_Copy(v);
- if (z != NULL)
- Py_SIZE(z) = -(Py_SIZE(v));
- return (PyObject *)z;
+ PyLongObject *z;
+ if (ABS(Py_SIZE(v)) <= 1)
+ return PyLong_FromLong(-MEDIUM_VALUE(v));
+ z = (PyLongObject *)_PyLong_Copy(v);
+ if (z != NULL)
+ Py_SIZE(z) = -(Py_SIZE(v));
+ return (PyObject *)z;
}
static PyObject *
long_abs(PyLongObject *v)
{
- if (Py_SIZE(v) < 0)
- return long_neg(v);
- else
- return long_long((PyObject *)v);
+ if (Py_SIZE(v) < 0)
+ return long_neg(v);
+ else
+ return long_long((PyObject *)v);
}
static int
long_bool(PyLongObject *v)
{
- return ABS(Py_SIZE(v)) != 0;
+ return ABS(Py_SIZE(v)) != 0;
}
static PyObject *
long_rshift(PyLongObject *a, PyLongObject *b)
{
- PyLongObject *z = NULL;
- long shiftby;
- Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
- digit lomask, himask;
-
- CHECK_BINOP(a, b);
-
- if (Py_SIZE(a) < 0) {
- /* Right shifting negative numbers is harder */
- PyLongObject *a1, *a2;
- a1 = (PyLongObject *) long_invert(a);
- if (a1 == NULL)
- goto rshift_error;
- a2 = (PyLongObject *) long_rshift(a1, b);
- Py_DECREF(a1);
- if (a2 == NULL)
- goto rshift_error;
- z = (PyLongObject *) long_invert(a2);
- Py_DECREF(a2);
- }
- else {
-
- shiftby = PyLong_AsLong((PyObject *)b);
- if (shiftby == -1L && PyErr_Occurred())
- goto rshift_error;
- if (shiftby < 0) {
- PyErr_SetString(PyExc_ValueError,
- "negative shift count");
- goto rshift_error;
- }
- wordshift = shiftby / PyLong_SHIFT;
- newsize = ABS(Py_SIZE(a)) - wordshift;
- if (newsize <= 0)
- return PyLong_FromLong(0);
- loshift = shiftby % PyLong_SHIFT;
- hishift = PyLong_SHIFT - loshift;
- lomask = ((digit)1 << hishift) - 1;
- himask = PyLong_MASK ^ lomask;
- z = _PyLong_New(newsize);
- if (z == NULL)
- goto rshift_error;
- if (Py_SIZE(a) < 0)
- Py_SIZE(z) = -(Py_SIZE(z));
- for (i = 0, j = wordshift; i < newsize; i++, j++) {
- z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
- if (i+1 < newsize)
- z->ob_digit[i] |=
- (a->ob_digit[j+1] << hishift) & himask;
- }
- z = long_normalize(z);
- }
+ PyLongObject *z = NULL;
+ long shiftby;
+ Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
+ digit lomask, himask;
+
+ CHECK_BINOP(a, b);
+
+ if (Py_SIZE(a) < 0) {
+ /* Right shifting negative numbers is harder */
+ PyLongObject *a1, *a2;
+ a1 = (PyLongObject *) long_invert(a);
+ if (a1 == NULL)
+ goto rshift_error;
+ a2 = (PyLongObject *) long_rshift(a1, b);
+ Py_DECREF(a1);
+ if (a2 == NULL)
+ goto rshift_error;
+ z = (PyLongObject *) long_invert(a2);
+ Py_DECREF(a2);
+ }
+ else {
+
+ shiftby = PyLong_AsLong((PyObject *)b);
+ if (shiftby == -1L && PyErr_Occurred())
+ goto rshift_error;
+ if (shiftby < 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "negative shift count");
+ goto rshift_error;
+ }
+ wordshift = shiftby / PyLong_SHIFT;
+ newsize = ABS(Py_SIZE(a)) - wordshift;
+ if (newsize <= 0)
+ return PyLong_FromLong(0);
+ loshift = shiftby % PyLong_SHIFT;
+ hishift = PyLong_SHIFT - loshift;
+ lomask = ((digit)1 << hishift) - 1;
+ himask = PyLong_MASK ^ lomask;
+ z = _PyLong_New(newsize);
+ if (z == NULL)
+ goto rshift_error;
+ if (Py_SIZE(a) < 0)
+ Py_SIZE(z) = -(Py_SIZE(z));
+ for (i = 0, j = wordshift; i < newsize; i++, j++) {
+ z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
+ if (i+1 < newsize)
+ z->ob_digit[i] |=
+ (a->ob_digit[j+1] << hishift) & himask;
+ }
+ z = long_normalize(z);
+ }
rshift_error:
- return (PyObject *) maybe_small_long(z);
+ return (PyObject *) maybe_small_long(z);
}
static PyObject *
long_lshift(PyObject *v, PyObject *w)
{
- /* This version due to Tim Peters */
- PyLongObject *a = (PyLongObject*)v;
- PyLongObject *b = (PyLongObject*)w;
- PyLongObject *z = NULL;
- long shiftby;
- Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
- twodigits accum;
-
- CHECK_BINOP(a, b);
-
- shiftby = PyLong_AsLong((PyObject *)b);
- if (shiftby == -1L && PyErr_Occurred())
- goto lshift_error;
- if (shiftby < 0) {
- PyErr_SetString(PyExc_ValueError, "negative shift count");
- goto lshift_error;
- }
- if ((long)(int)shiftby != shiftby) {
- PyErr_SetString(PyExc_ValueError,
- "outrageous left shift count");
- goto lshift_error;
- }
- /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
- wordshift = (int)shiftby / PyLong_SHIFT;
- remshift = (int)shiftby - wordshift * PyLong_SHIFT;
-
- oldsize = ABS(Py_SIZE(a));
- newsize = oldsize + wordshift;
- if (remshift)
- ++newsize;
- z = _PyLong_New(newsize);
- if (z == NULL)
- goto lshift_error;
- if (Py_SIZE(a) < 0)
- NEGATE(z);
- for (i = 0; i < wordshift; i++)
- z->ob_digit[i] = 0;
- accum = 0;
- for (i = wordshift, j = 0; j < oldsize; i++, j++) {
- accum |= (twodigits)a->ob_digit[j] << remshift;
- z->ob_digit[i] = (digit)(accum & PyLong_MASK);
- accum >>= PyLong_SHIFT;
- }
- if (remshift)
- z->ob_digit[newsize-1] = (digit)accum;
- else
- assert(!accum);
- z = long_normalize(z);
+ /* This version due to Tim Peters */
+ PyLongObject *a = (PyLongObject*)v;
+ PyLongObject *b = (PyLongObject*)w;
+ PyLongObject *z = NULL;
+ long shiftby;
+ Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
+ twodigits accum;
+
+ CHECK_BINOP(a, b);
+
+ shiftby = PyLong_AsLong((PyObject *)b);
+ if (shiftby == -1L && PyErr_Occurred())
+ goto lshift_error;
+ if (shiftby < 0) {
+ PyErr_SetString(PyExc_ValueError, "negative shift count");
+ goto lshift_error;
+ }
+ if ((long)(int)shiftby != shiftby) {
+ PyErr_SetString(PyExc_ValueError,
+ "outrageous left shift count");
+ goto lshift_error;
+ }
+ /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
+ wordshift = (int)shiftby / PyLong_SHIFT;
+ remshift = (int)shiftby - wordshift * PyLong_SHIFT;
+
+ oldsize = ABS(Py_SIZE(a));
+ newsize = oldsize + wordshift;
+ if (remshift)
+ ++newsize;
+ z = _PyLong_New(newsize);
+ if (z == NULL)
+ goto lshift_error;
+ if (Py_SIZE(a) < 0)
+ NEGATE(z);
+ for (i = 0; i < wordshift; i++)
+ z->ob_digit[i] = 0;
+ accum = 0;
+ for (i = wordshift, j = 0; j < oldsize; i++, j++) {
+ accum |= (twodigits)a->ob_digit[j] << remshift;
+ z->ob_digit[i] = (digit)(accum & PyLong_MASK);
+ accum >>= PyLong_SHIFT;
+ }
+ if (remshift)
+ z->ob_digit[newsize-1] = (digit)accum;
+ else
+ assert(!accum);
+ z = long_normalize(z);
lshift_error:
- return (PyObject *) maybe_small_long(z);
+ return (PyObject *) maybe_small_long(z);
}
@@ -3611,154 +3611,154 @@ lshift_error:
static PyObject *
long_bitwise(PyLongObject *a,
- int op, /* '&', '|', '^' */
- PyLongObject *b)
+ int op, /* '&', '|', '^' */
+ PyLongObject *b)
{
- digit maska, maskb; /* 0 or PyLong_MASK */
- int negz;
- Py_ssize_t size_a, size_b, size_z, i;
- PyLongObject *z;
- digit diga, digb;
- PyObject *v;
-
- if (Py_SIZE(a) < 0) {
- a = (PyLongObject *) long_invert(a);
- if (a == NULL)
- return NULL;
- maska = PyLong_MASK;
- }
- else {
- Py_INCREF(a);
- maska = 0;
- }
- if (Py_SIZE(b) < 0) {
- b = (PyLongObject *) long_invert(b);
- if (b == NULL) {
- Py_DECREF(a);
- return NULL;
- }
- maskb = PyLong_MASK;
- }
- else {
- Py_INCREF(b);
- maskb = 0;
- }
-
- negz = 0;
- switch (op) {
- case '^':
- if (maska != maskb) {
- maska ^= PyLong_MASK;
- negz = -1;
- }
- break;
- case '&':
- if (maska && maskb) {
- op = '|';
- maska ^= PyLong_MASK;
- maskb ^= PyLong_MASK;
- negz = -1;
- }
- break;
- case '|':
- if (maska || maskb) {
- op = '&';
- maska ^= PyLong_MASK;
- maskb ^= PyLong_MASK;
- negz = -1;
- }
- break;
- }
-
- /* JRH: The original logic here was to allocate the result value (z)
- as the longer of the two operands. However, there are some cases
- where the result is guaranteed to be shorter than that: AND of two
- positives, OR of two negatives: use the shorter number. AND with
- mixed signs: use the positive number. OR with mixed signs: use the
- negative number. After the transformations above, op will be '&'
- iff one of these cases applies, and mask will be non-0 for operands
- whose length should be ignored.
- */
-
- size_a = Py_SIZE(a);
- size_b = Py_SIZE(b);
- size_z = op == '&'
- ? (maska
- ? size_b
- : (maskb ? size_a : MIN(size_a, size_b)))
- : MAX(size_a, size_b);
- z = _PyLong_New(size_z);
- if (z == NULL) {
- Py_DECREF(a);
- Py_DECREF(b);
- return NULL;
- }
-
- for (i = 0; i < size_z; ++i) {
- diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
- digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
- switch (op) {
- case '&': z->ob_digit[i] = diga & digb; break;
- case '|': z->ob_digit[i] = diga | digb; break;
- case '^': z->ob_digit[i] = diga ^ digb; break;
- }
- }
-
- Py_DECREF(a);
- Py_DECREF(b);
- z = long_normalize(z);
- if (negz == 0)
- return (PyObject *) maybe_small_long(z);
- v = long_invert(z);
- Py_DECREF(z);
- return v;
+ digit maska, maskb; /* 0 or PyLong_MASK */
+ int negz;
+ Py_ssize_t size_a, size_b, size_z, i;
+ PyLongObject *z;
+ digit diga, digb;
+ PyObject *v;
+
+ if (Py_SIZE(a) < 0) {
+ a = (PyLongObject *) long_invert(a);
+ if (a == NULL)
+ return NULL;
+ maska = PyLong_MASK;
+ }
+ else {
+ Py_INCREF(a);
+ maska = 0;
+ }
+ if (Py_SIZE(b) < 0) {
+ b = (PyLongObject *) long_invert(b);
+ if (b == NULL) {
+ Py_DECREF(a);
+ return NULL;
+ }
+ maskb = PyLong_MASK;
+ }
+ else {
+ Py_INCREF(b);
+ maskb = 0;
+ }
+
+ negz = 0;
+ switch (op) {
+ case '^':
+ if (maska != maskb) {
+ maska ^= PyLong_MASK;
+ negz = -1;
+ }
+ break;
+ case '&':
+ if (maska && maskb) {
+ op = '|';
+ maska ^= PyLong_MASK;
+ maskb ^= PyLong_MASK;
+ negz = -1;
+ }
+ break;
+ case '|':
+ if (maska || maskb) {
+ op = '&';
+ maska ^= PyLong_MASK;
+ maskb ^= PyLong_MASK;
+ negz = -1;
+ }
+ break;
+ }
+
+ /* JRH: The original logic here was to allocate the result value (z)
+ as the longer of the two operands. However, there are some cases
+ where the result is guaranteed to be shorter than that: AND of two
+ positives, OR of two negatives: use the shorter number. AND with
+ mixed signs: use the positive number. OR with mixed signs: use the
+ negative number. After the transformations above, op will be '&'
+ iff one of these cases applies, and mask will be non-0 for operands
+ whose length should be ignored.
+ */
+
+ size_a = Py_SIZE(a);
+ size_b = Py_SIZE(b);
+ size_z = op == '&'
+ ? (maska
+ ? size_b
+ : (maskb ? size_a : MIN(size_a, size_b)))
+ : MAX(size_a, size_b);
+ z = _PyLong_New(size_z);
+ if (z == NULL) {
+ Py_DECREF(a);
+ Py_DECREF(b);
+ return NULL;
+ }
+
+ for (i = 0; i < size_z; ++i) {
+ diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
+ digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
+ switch (op) {
+ case '&': z->ob_digit[i] = diga & digb; break;
+ case '|': z->ob_digit[i] = diga | digb; break;
+ case '^': z->ob_digit[i] = diga ^ digb; break;
+ }
+ }
+
+ Py_DECREF(a);
+ Py_DECREF(b);
+ z = long_normalize(z);
+ if (negz == 0)
+ return (PyObject *) maybe_small_long(z);
+ v = long_invert(z);
+ Py_DECREF(z);
+ return v;
}
static PyObject *
long_and(PyObject *a, PyObject *b)
{
- PyObject *c;
- CHECK_BINOP(a, b);
- c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
- return c;
+ PyObject *c;
+ CHECK_BINOP(a, b);
+ c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
+ return c;
}
static PyObject *
long_xor(PyObject *a, PyObject *b)
{
- PyObject *c;
- CHECK_BINOP(a, b);
- c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
- return c;
+ PyObject *c;
+ CHECK_BINOP(a, b);
+ c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
+ return c;
}
static PyObject *
long_or(PyObject *a, PyObject *b)
{
- PyObject *c;
- CHECK_BINOP(a, b);
- c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
- return c;
+ PyObject *c;
+ CHECK_BINOP(a, b);
+ c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
+ return c;
}
static PyObject *
long_long(PyObject *v)
{
- if (PyLong_CheckExact(v))
- Py_INCREF(v);
- else
- v = _PyLong_Copy((PyLongObject *)v);
- return v;
+ if (PyLong_CheckExact(v))
+ Py_INCREF(v);
+ else
+ v = _PyLong_Copy((PyLongObject *)v);
+ return v;
}
static PyObject *
long_float(PyObject *v)
{
- double result;
- result = PyLong_AsDouble(v);
- if (result == -1.0 && PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(result);
+ double result;
+ result = PyLong_AsDouble(v);
+ if (result == -1.0 && PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(result);
}
static PyObject *
@@ -3767,47 +3767,47 @@ long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *x = NULL;
- int base = -909; /* unlikely! */
- static char *kwlist[] = {"x", "base", 0};
-
- if (type != &PyLong_Type)
- return long_subtype_new(type, args, kwds); /* Wimp out */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
- &x, &base))
- return NULL;
- if (x == NULL)
- return PyLong_FromLong(0L);
- if (base == -909)
- return PyNumber_Long(x);
- else if (PyUnicode_Check(x))
- return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
- PyUnicode_GET_SIZE(x),
- base);
- else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
- /* Since PyLong_FromString doesn't have a length parameter,
- * check here for possible NULs in the string. */
- char *string;
- Py_ssize_t size = Py_SIZE(x);
- if (PyByteArray_Check(x))
- string = PyByteArray_AS_STRING(x);
- else
- string = PyBytes_AS_STRING(x);
- if (strlen(string) != (size_t)size) {
- /* We only see this if there's a null byte in x,
- x is a bytes or buffer, *and* a base is given. */
- PyErr_Format(PyExc_ValueError,
- "invalid literal for int() with base %d: %R",
- base, x);
- return NULL;
- }
- return PyLong_FromString(string, NULL, base);
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "int() can't convert non-string with explicit base");
- return NULL;
- }
+ PyObject *x = NULL;
+ int base = -909; /* unlikely! */
+ static char *kwlist[] = {"x", "base", 0};
+
+ if (type != &PyLong_Type)
+ return long_subtype_new(type, args, kwds); /* Wimp out */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
+ &x, &base))
+ return NULL;
+ if (x == NULL)
+ return PyLong_FromLong(0L);
+ if (base == -909)
+ return PyNumber_Long(x);
+ else if (PyUnicode_Check(x))
+ return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
+ PyUnicode_GET_SIZE(x),
+ base);
+ else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
+ /* Since PyLong_FromString doesn't have a length parameter,
+ * check here for possible NULs in the string. */
+ char *string;
+ Py_ssize_t size = Py_SIZE(x);
+ if (PyByteArray_Check(x))
+ string = PyByteArray_AS_STRING(x);
+ else
+ string = PyBytes_AS_STRING(x);
+ if (strlen(string) != (size_t)size) {
+ /* We only see this if there's a null byte in x,
+ x is a bytes or buffer, *and* a base is given. */
+ PyErr_Format(PyExc_ValueError,
+ "invalid literal for int() with base %d: %R",
+ base, x);
+ return NULL;
+ }
+ return PyLong_FromString(string, NULL, base);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "int() can't convert non-string with explicit base");
+ return NULL;
+ }
}
/* Wimpy, slow approach to tp_new calls for subtypes of long:
@@ -3818,256 +3818,256 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject *
long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyLongObject *tmp, *newobj;
- Py_ssize_t i, n;
-
- assert(PyType_IsSubtype(type, &PyLong_Type));
- tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
- if (tmp == NULL)
- return NULL;
- assert(PyLong_CheckExact(tmp));
- n = Py_SIZE(tmp);
- if (n < 0)
- n = -n;
- newobj = (PyLongObject *)type->tp_alloc(type, n);
- if (newobj == NULL) {
- Py_DECREF(tmp);
- return NULL;
- }
- assert(PyLong_Check(newobj));
- Py_SIZE(newobj) = Py_SIZE(tmp);
- for (i = 0; i < n; i++)
- newobj->ob_digit[i] = tmp->ob_digit[i];
- Py_DECREF(tmp);
- return (PyObject *)newobj;
+ PyLongObject *tmp, *newobj;
+ Py_ssize_t i, n;
+
+ assert(PyType_IsSubtype(type, &PyLong_Type));
+ tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyLong_CheckExact(tmp));
+ n = Py_SIZE(tmp);
+ if (n < 0)
+ n = -n;
+ newobj = (PyLongObject *)type->tp_alloc(type, n);
+ if (newobj == NULL) {
+ Py_DECREF(tmp);
+ return NULL;
+ }
+ assert(PyLong_Check(newobj));
+ Py_SIZE(newobj) = Py_SIZE(tmp);
+ for (i = 0; i < n; i++)
+ newobj->ob_digit[i] = tmp->ob_digit[i];
+ Py_DECREF(tmp);
+ return (PyObject *)newobj;
}
static PyObject *
long_getnewargs(PyLongObject *v)
{
- return Py_BuildValue("(N)", _PyLong_Copy(v));
+ return Py_BuildValue("(N)", _PyLong_Copy(v));
}
static PyObject *
long_get0(PyLongObject *v, void *context) {
- return PyLong_FromLong(0L);
+ return PyLong_FromLong(0L);
}
static PyObject *
long_get1(PyLongObject *v, void *context) {
- return PyLong_FromLong(1L);
+ return PyLong_FromLong(1L);
}
static PyObject *
long__format__(PyObject *self, PyObject *args)
{
- PyObject *format_spec;
+ PyObject *format_spec;
- if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
- return _PyLong_FormatAdvanced(self,
- PyUnicode_AS_UNICODE(format_spec),
- PyUnicode_GET_SIZE(format_spec));
+ if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
+ return NULL;
+ return _PyLong_FormatAdvanced(self,
+ PyUnicode_AS_UNICODE(format_spec),
+ PyUnicode_GET_SIZE(format_spec));
}
static PyObject *
long_round(PyObject *self, PyObject *args)
{
- PyObject *o_ndigits=NULL, *temp;
- PyLongObject *pow=NULL, *q=NULL, *r=NULL, *ndigits=NULL, *one;
- int errcode;
- digit q_mod_4;
-
- /* Notes on the algorithm: to round to the nearest 10**n (n positive),
- the straightforward method is:
-
- (1) divide by 10**n
- (2) round to nearest integer (round to even in case of tie)
- (3) multiply result by 10**n.
-
- But the rounding step involves examining the fractional part of the
- quotient to see whether it's greater than 0.5 or not. Since we
- want to do the whole calculation in integer arithmetic, it's
- simpler to do:
-
- (1) divide by (10**n)/2
- (2) round to nearest multiple of 2 (multiple of 4 in case of tie)
- (3) multiply result by (10**n)/2.
-
- Then all we need to know about the fractional part of the quotient
- arising in step (2) is whether it's zero or not.
-
- Doing both a multiplication and division is wasteful, and is easily
- avoided if we just figure out how much to adjust the original input
- by to do the rounding.
-
- Here's the whole algorithm expressed in Python.
-
- def round(self, ndigits = None):
- """round(int, int) -> int"""
- if ndigits is None or ndigits >= 0:
- return self
- pow = 10**-ndigits >> 1
- q, r = divmod(self, pow)
- self -= r
- if (q & 1 != 0):
- if (q & 2 == r == 0):
- self -= pow
- else:
- self += pow
- return self
-
- */
- if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
- return NULL;
- if (o_ndigits == NULL)
- return long_long(self);
-
- ndigits = (PyLongObject *)PyNumber_Index(o_ndigits);
- if (ndigits == NULL)
- return NULL;
-
- if (Py_SIZE(ndigits) >= 0) {
- Py_DECREF(ndigits);
- return long_long(self);
- }
-
- Py_INCREF(self); /* to keep refcounting simple */
- /* we now own references to self, ndigits */
-
- /* pow = 10 ** -ndigits >> 1 */
- pow = (PyLongObject *)PyLong_FromLong(10L);
- if (pow == NULL)
- goto error;
- temp = long_neg(ndigits);
- Py_DECREF(ndigits);
- ndigits = (PyLongObject *)temp;
- if (ndigits == NULL)
- goto error;
- temp = long_pow((PyObject *)pow, (PyObject *)ndigits, Py_None);
- Py_DECREF(pow);
- pow = (PyLongObject *)temp;
- if (pow == NULL)
- goto error;
- assert(PyLong_Check(pow)); /* check long_pow returned a long */
- one = (PyLongObject *)PyLong_FromLong(1L);
- if (one == NULL)
- goto error;
- temp = long_rshift(pow, one);
- Py_DECREF(one);
- Py_DECREF(pow);
- pow = (PyLongObject *)temp;
- if (pow == NULL)
- goto error;
-
- /* q, r = divmod(self, pow) */
- errcode = l_divmod((PyLongObject *)self, pow, &q, &r);
- if (errcode == -1)
- goto error;
-
- /* self -= r */
- temp = long_sub((PyLongObject *)self, r);
- Py_DECREF(self);
- self = temp;
- if (self == NULL)
- goto error;
-
- /* get value of quotient modulo 4 */
- if (Py_SIZE(q) == 0)
- q_mod_4 = 0;
- else if (Py_SIZE(q) > 0)
- q_mod_4 = q->ob_digit[0] & 3;
- else
- q_mod_4 = (PyLong_BASE-q->ob_digit[0]) & 3;
-
- if ((q_mod_4 & 1) == 1) {
- /* q is odd; round self up or down by adding or subtracting pow */
- if (q_mod_4 == 1 && Py_SIZE(r) == 0)
- temp = (PyObject *)long_sub((PyLongObject *)self, pow);
- else
- temp = (PyObject *)long_add((PyLongObject *)self, pow);
- Py_DECREF(self);
- self = temp;
- if (self == NULL)
- goto error;
- }
- Py_DECREF(q);
- Py_DECREF(r);
- Py_DECREF(pow);
- Py_DECREF(ndigits);
- return self;
+ PyObject *o_ndigits=NULL, *temp;
+ PyLongObject *pow=NULL, *q=NULL, *r=NULL, *ndigits=NULL, *one;
+ int errcode;
+ digit q_mod_4;
+
+ /* Notes on the algorithm: to round to the nearest 10**n (n positive),
+ the straightforward method is:
+
+ (1) divide by 10**n
+ (2) round to nearest integer (round to even in case of tie)
+ (3) multiply result by 10**n.
+
+ But the rounding step involves examining the fractional part of the
+ quotient to see whether it's greater than 0.5 or not. Since we
+ want to do the whole calculation in integer arithmetic, it's
+ simpler to do:
+
+ (1) divide by (10**n)/2
+ (2) round to nearest multiple of 2 (multiple of 4 in case of tie)
+ (3) multiply result by (10**n)/2.
+
+ Then all we need to know about the fractional part of the quotient
+ arising in step (2) is whether it's zero or not.
+
+ Doing both a multiplication and division is wasteful, and is easily
+ avoided if we just figure out how much to adjust the original input
+ by to do the rounding.
+
+ Here's the whole algorithm expressed in Python.
+
+ def round(self, ndigits = None):
+ """round(int, int) -> int"""
+ if ndigits is None or ndigits >= 0:
+ return self
+ pow = 10**-ndigits >> 1
+ q, r = divmod(self, pow)
+ self -= r
+ if (q & 1 != 0):
+ if (q & 2 == r == 0):
+ self -= pow
+ else:
+ self += pow
+ return self
+
+ */
+ if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
+ return NULL;
+ if (o_ndigits == NULL)
+ return long_long(self);
+
+ ndigits = (PyLongObject *)PyNumber_Index(o_ndigits);
+ if (ndigits == NULL)
+ return NULL;
+
+ if (Py_SIZE(ndigits) >= 0) {
+ Py_DECREF(ndigits);
+ return long_long(self);
+ }
+
+ Py_INCREF(self); /* to keep refcounting simple */
+ /* we now own references to self, ndigits */
+
+ /* pow = 10 ** -ndigits >> 1 */
+ pow = (PyLongObject *)PyLong_FromLong(10L);
+ if (pow == NULL)
+ goto error;
+ temp = long_neg(ndigits);
+ Py_DECREF(ndigits);
+ ndigits = (PyLongObject *)temp;
+ if (ndigits == NULL)
+ goto error;
+ temp = long_pow((PyObject *)pow, (PyObject *)ndigits, Py_None);
+ Py_DECREF(pow);
+ pow = (PyLongObject *)temp;
+ if (pow == NULL)
+ goto error;
+ assert(PyLong_Check(pow)); /* check long_pow returned a long */
+ one = (PyLongObject *)PyLong_FromLong(1L);
+ if (one == NULL)
+ goto error;
+ temp = long_rshift(pow, one);
+ Py_DECREF(one);
+ Py_DECREF(pow);
+ pow = (PyLongObject *)temp;
+ if (pow == NULL)
+ goto error;
+
+ /* q, r = divmod(self, pow) */
+ errcode = l_divmod((PyLongObject *)self, pow, &q, &r);
+ if (errcode == -1)
+ goto error;
+
+ /* self -= r */
+ temp = long_sub((PyLongObject *)self, r);
+ Py_DECREF(self);
+ self = temp;
+ if (self == NULL)
+ goto error;
+
+ /* get value of quotient modulo 4 */
+ if (Py_SIZE(q) == 0)
+ q_mod_4 = 0;
+ else if (Py_SIZE(q) > 0)
+ q_mod_4 = q->ob_digit[0] & 3;
+ else
+ q_mod_4 = (PyLong_BASE-q->ob_digit[0]) & 3;
+
+ if ((q_mod_4 & 1) == 1) {
+ /* q is odd; round self up or down by adding or subtracting pow */
+ if (q_mod_4 == 1 && Py_SIZE(r) == 0)
+ temp = (PyObject *)long_sub((PyLongObject *)self, pow);
+ else
+ temp = (PyObject *)long_add((PyLongObject *)self, pow);
+ Py_DECREF(self);
+ self = temp;
+ if (self == NULL)
+ goto error;
+ }
+ Py_DECREF(q);
+ Py_DECREF(r);
+ Py_DECREF(pow);
+ Py_DECREF(ndigits);
+ return self;
error:
- Py_XDECREF(q);
- Py_XDECREF(r);
- Py_XDECREF(pow);
- Py_XDECREF(self);
- Py_XDECREF(ndigits);
- return NULL;
+ Py_XDECREF(q);
+ Py_XDECREF(r);
+ Py_XDECREF(pow);
+ Py_XDECREF(self);
+ Py_XDECREF(ndigits);
+ return NULL;
}
static PyObject *
long_sizeof(PyLongObject *v)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit);
- return PyLong_FromSsize_t(res);
+ res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit);
+ return PyLong_FromSsize_t(res);
}
static PyObject *
long_bit_length(PyLongObject *v)
{
- PyLongObject *result, *x, *y;
- Py_ssize_t ndigits, msd_bits = 0;
- digit msd;
-
- assert(v != NULL);
- assert(PyLong_Check(v));
-
- ndigits = ABS(Py_SIZE(v));
- if (ndigits == 0)
- return PyLong_FromLong(0);
-
- msd = v->ob_digit[ndigits-1];
- while (msd >= 32) {
- msd_bits += 6;
- msd >>= 6;
- }
- msd_bits += (long)(BitLengthTable[msd]);
-
- if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
- return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
-
- /* expression above may overflow; use Python integers instead */
- result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
- if (result == NULL)
- return NULL;
- x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
- if (x == NULL)
- goto error;
- y = (PyLongObject *)long_mul(result, x);
- Py_DECREF(x);
- if (y == NULL)
- goto error;
- Py_DECREF(result);
- result = y;
-
- x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
- if (x == NULL)
- goto error;
- y = (PyLongObject *)long_add(result, x);
- Py_DECREF(x);
- if (y == NULL)
- goto error;
- Py_DECREF(result);
- result = y;
-
- return (PyObject *)result;
+ PyLongObject *result, *x, *y;
+ Py_ssize_t ndigits, msd_bits = 0;
+ digit msd;
+
+ assert(v != NULL);
+ assert(PyLong_Check(v));
+
+ ndigits = ABS(Py_SIZE(v));
+ if (ndigits == 0)
+ return PyLong_FromLong(0);
+
+ msd = v->ob_digit[ndigits-1];
+ while (msd >= 32) {
+ msd_bits += 6;
+ msd >>= 6;
+ }
+ msd_bits += (long)(BitLengthTable[msd]);
+
+ if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
+ return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
+
+ /* expression above may overflow; use Python integers instead */
+ result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
+ if (result == NULL)
+ return NULL;
+ x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
+ if (x == NULL)
+ goto error;
+ y = (PyLongObject *)long_mul(result, x);
+ Py_DECREF(x);
+ if (y == NULL)
+ goto error;
+ Py_DECREF(result);
+ result = y;
+
+ x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
+ if (x == NULL)
+ goto error;
+ y = (PyLongObject *)long_add(result, x);
+ Py_DECREF(x);
+ if (y == NULL)
+ goto error;
+ Py_DECREF(result);
+ result = y;
+
+ return (PyObject *)result;
error:
- Py_DECREF(result);
- return NULL;
+ Py_DECREF(result);
+ return NULL;
}
PyDoc_STRVAR(long_bit_length_doc,
@@ -4083,33 +4083,33 @@ Number of bits necessary to represent self in binary.\n\
static PyObject *
long_is_finite(PyObject *v)
{
- Py_RETURN_TRUE;
+ Py_RETURN_TRUE;
}
#endif
static PyMethodDef long_methods[] = {
- {"conjugate", (PyCFunction)long_long, METH_NOARGS,
- "Returns self, the complex conjugate of any int."},
- {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS,
- long_bit_length_doc},
+ {"conjugate", (PyCFunction)long_long, METH_NOARGS,
+ "Returns self, the complex conjugate of any int."},
+ {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS,
+ long_bit_length_doc},
#if 0
- {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
- "Returns always True."},
+ {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
+ "Returns always True."},
#endif
- {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
- "Truncating an Integral returns itself."},
- {"__floor__", (PyCFunction)long_long, METH_NOARGS,
- "Flooring an Integral returns itself."},
- {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
- "Ceiling of an Integral returns itself."},
- {"__round__", (PyCFunction)long_round, METH_VARARGS,
- "Rounding an Integral returns itself.\n"
- "Rounding with an ndigits argument also returns an integer."},
- {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
- {"__format__", (PyCFunction)long__format__, METH_VARARGS},
- {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
- "Returns size in memory, in bytes"},
- {NULL, NULL} /* sentinel */
+ {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
+ "Truncating an Integral returns itself."},
+ {"__floor__", (PyCFunction)long_long, METH_NOARGS,
+ "Flooring an Integral returns itself."},
+ {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
+ "Ceiling of an Integral returns itself."},
+ {"__round__", (PyCFunction)long_round, METH_VARARGS,
+ "Rounding an Integral returns itself.\n"
+ "Rounding with an ndigits argument also returns an integer."},
+ {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
+ {"__format__", (PyCFunction)long__format__, METH_VARARGS},
+ {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
+ "Returns size in memory, in bytes"},
+ {NULL, NULL} /* sentinel */
};
static PyGetSetDef long_getset[] = {
@@ -4142,83 +4142,83 @@ string, use the optional base. It is an error to supply a base when\n\
converting a non-string.");
static PyNumberMethods long_as_number = {
- (binaryfunc) long_add, /*nb_add*/
- (binaryfunc) long_sub, /*nb_subtract*/
- (binaryfunc) long_mul, /*nb_multiply*/
- long_mod, /*nb_remainder*/
- long_divmod, /*nb_divmod*/
- long_pow, /*nb_power*/
- (unaryfunc) long_neg, /*nb_negative*/
- (unaryfunc) long_long, /*tp_positive*/
- (unaryfunc) long_abs, /*tp_absolute*/
- (inquiry) long_bool, /*tp_bool*/
- (unaryfunc) long_invert, /*nb_invert*/
- long_lshift, /*nb_lshift*/
- (binaryfunc) long_rshift, /*nb_rshift*/
- long_and, /*nb_and*/
- long_xor, /*nb_xor*/
- long_or, /*nb_or*/
- long_long, /*nb_int*/
- 0, /*nb_reserved*/
- long_float, /*nb_float*/
- 0, /* nb_inplace_add */
- 0, /* nb_inplace_subtract */
- 0, /* nb_inplace_multiply */
- 0, /* nb_inplace_remainder */
- 0, /* nb_inplace_power */
- 0, /* nb_inplace_lshift */
- 0, /* nb_inplace_rshift */
- 0, /* nb_inplace_and */
- 0, /* nb_inplace_xor */
- 0, /* nb_inplace_or */
- long_div, /* nb_floor_divide */
- long_true_divide, /* nb_true_divide */
- 0, /* nb_inplace_floor_divide */
- 0, /* nb_inplace_true_divide */
- long_long, /* nb_index */
+ (binaryfunc) long_add, /*nb_add*/
+ (binaryfunc) long_sub, /*nb_subtract*/
+ (binaryfunc) long_mul, /*nb_multiply*/
+ long_mod, /*nb_remainder*/
+ long_divmod, /*nb_divmod*/
+ long_pow, /*nb_power*/
+ (unaryfunc) long_neg, /*nb_negative*/
+ (unaryfunc) long_long, /*tp_positive*/
+ (unaryfunc) long_abs, /*tp_absolute*/
+ (inquiry) long_bool, /*tp_bool*/
+ (unaryfunc) long_invert, /*nb_invert*/
+ long_lshift, /*nb_lshift*/
+ (binaryfunc) long_rshift, /*nb_rshift*/
+ long_and, /*nb_and*/
+ long_xor, /*nb_xor*/
+ long_or, /*nb_or*/
+ long_long, /*nb_int*/
+ 0, /*nb_reserved*/
+ long_float, /*nb_float*/
+ 0, /* nb_inplace_add */
+ 0, /* nb_inplace_subtract */
+ 0, /* nb_inplace_multiply */
+ 0, /* nb_inplace_remainder */
+ 0, /* nb_inplace_power */
+ 0, /* nb_inplace_lshift */
+ 0, /* nb_inplace_rshift */
+ 0, /* nb_inplace_and */
+ 0, /* nb_inplace_xor */
+ 0, /* nb_inplace_or */
+ long_div, /* nb_floor_divide */
+ long_true_divide, /* nb_true_divide */
+ 0, /* nb_inplace_floor_divide */
+ 0, /* nb_inplace_true_divide */
+ long_long, /* nb_index */
};
PyTypeObject PyLong_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "int", /* tp_name */
- offsetof(PyLongObject, ob_digit), /* tp_basicsize */
- sizeof(digit), /* tp_itemsize */
- long_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- long_repr, /* tp_repr */
- &long_as_number, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)long_hash, /* tp_hash */
- 0, /* tp_call */
- long_repr, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
- long_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- long_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- long_methods, /* tp_methods */
- 0, /* tp_members */
- long_getset, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- long_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "int", /* tp_name */
+ offsetof(PyLongObject, ob_digit), /* tp_basicsize */
+ sizeof(digit), /* tp_itemsize */
+ long_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ long_repr, /* tp_repr */
+ &long_as_number, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)long_hash, /* tp_hash */
+ 0, /* tp_call */
+ long_repr, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
+ long_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ long_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ long_methods, /* tp_methods */
+ 0, /* tp_members */
+ long_getset, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ long_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
static PyTypeObject Int_InfoType;
@@ -4230,89 +4230,89 @@ A struct sequence that holds information about Python's\n\
internal representation of integers. The attributes are read only.");
static PyStructSequence_Field int_info_fields[] = {
- {"bits_per_digit", "size of a digit in bits"},
- {"sizeof_digit", "size in bytes of the C type used to "
- "represent a digit"},
- {NULL, NULL}
+ {"bits_per_digit", "size of a digit in bits"},
+ {"sizeof_digit", "size in bytes of the C type used to "
+ "represent a digit"},
+ {NULL, NULL}
};
static PyStructSequence_Desc int_info_desc = {
- "sys.int_info", /* name */
- int_info__doc__, /* doc */
- int_info_fields, /* fields */
- 2 /* number of fields */
+ "sys.int_info", /* name */
+ int_info__doc__, /* doc */
+ int_info_fields, /* fields */
+ 2 /* number of fields */
};
PyObject *
PyLong_GetInfo(void)
{
- PyObject* int_info;
- int field = 0;
- int_info = PyStructSequence_New(&Int_InfoType);
- if (int_info == NULL)
- return NULL;
- PyStructSequence_SET_ITEM(int_info, field++,
- PyLong_FromLong(PyLong_SHIFT));
- PyStructSequence_SET_ITEM(int_info, field++,
- PyLong_FromLong(sizeof(digit)));
- if (PyErr_Occurred()) {
- Py_CLEAR(int_info);
- return NULL;
- }
- return int_info;
+ PyObject* int_info;
+ int field = 0;
+ int_info = PyStructSequence_New(&Int_InfoType);
+ if (int_info == NULL)
+ return NULL;
+ PyStructSequence_SET_ITEM(int_info, field++,
+ PyLong_FromLong(PyLong_SHIFT));
+ PyStructSequence_SET_ITEM(int_info, field++,
+ PyLong_FromLong(sizeof(digit)));
+ if (PyErr_Occurred()) {
+ Py_CLEAR(int_info);
+ return NULL;
+ }
+ return int_info;
}
int
_PyLong_Init(void)
{
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
- int ival, size;
- PyLongObject *v = small_ints;
-
- for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
- size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
- if (Py_TYPE(v) == &PyLong_Type) {
- /* The element is already initialized, most likely
- * the Python interpreter was initialized before.
- */
- Py_ssize_t refcnt;
- PyObject* op = (PyObject*)v;
-
- refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
- _Py_NewReference(op);
- /* _Py_NewReference sets the ref count to 1 but
- * the ref count might be larger. Set the refcnt
- * to the original refcnt + 1 */
- Py_REFCNT(op) = refcnt + 1;
- assert(Py_SIZE(op) == size);
- assert(v->ob_digit[0] == abs(ival));
- }
- else {
- PyObject_INIT(v, &PyLong_Type);
- }
- Py_SIZE(v) = size;
- v->ob_digit[0] = abs(ival);
- }
+ int ival, size;
+ PyLongObject *v = small_ints;
+
+ for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
+ size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
+ if (Py_TYPE(v) == &PyLong_Type) {
+ /* The element is already initialized, most likely
+ * the Python interpreter was initialized before.
+ */
+ Py_ssize_t refcnt;
+ PyObject* op = (PyObject*)v;
+
+ refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
+ _Py_NewReference(op);
+ /* _Py_NewReference sets the ref count to 1 but
+ * the ref count might be larger. Set the refcnt
+ * to the original refcnt + 1 */
+ Py_REFCNT(op) = refcnt + 1;
+ assert(Py_SIZE(op) == size);
+ assert(v->ob_digit[0] == abs(ival));
+ }
+ else {
+ PyObject_INIT(v, &PyLong_Type);
+ }
+ Py_SIZE(v) = size;
+ v->ob_digit[0] = abs(ival);
+ }
#endif
- /* initialize int_info */
- if (Int_InfoType.tp_name == 0)
- PyStructSequence_InitType(&Int_InfoType, &int_info_desc);
+ /* initialize int_info */
+ if (Int_InfoType.tp_name == 0)
+ PyStructSequence_InitType(&Int_InfoType, &int_info_desc);
- return 1;
+ return 1;
}
void
PyLong_Fini(void)
{
- /* Integers are currently statically allocated. Py_DECREF is not
- needed, but Python must forget about the reference or multiple
- reinitializations will fail. */
+ /* Integers are currently statically allocated. Py_DECREF is not
+ needed, but Python must forget about the reference or multiple
+ reinitializations will fail. */
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
- int i;
- PyLongObject *v = small_ints;
- for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
- _Py_DEC_REFTOTAL;
- _Py_ForgetReference((PyObject*)v);
- }
+ int i;
+ PyLongObject *v = small_ints;
+ for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
+ _Py_DEC_REFTOTAL;
+ _Py_ForgetReference((PyObject*)v);
+ }
#endif
}
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index b734b0f..356d161 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -16,104 +16,104 @@ static int numfree = 0;
PyObject *
PyCFunction_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module)
{
- PyCFunctionObject *op;
- op = free_list;
- if (op != NULL) {
- free_list = (PyCFunctionObject *)(op->m_self);
- PyObject_INIT(op, &PyCFunction_Type);
- numfree--;
- }
- else {
- op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
- if (op == NULL)
- return NULL;
- }
- op->m_ml = ml;
- Py_XINCREF(self);
- op->m_self = self;
- Py_XINCREF(module);
- op->m_module = module;
- _PyObject_GC_TRACK(op);
- return (PyObject *)op;
+ PyCFunctionObject *op;
+ op = free_list;
+ if (op != NULL) {
+ free_list = (PyCFunctionObject *)(op->m_self);
+ PyObject_INIT(op, &PyCFunction_Type);
+ numfree--;
+ }
+ else {
+ op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type);
+ if (op == NULL)
+ return NULL;
+ }
+ op->m_ml = ml;
+ Py_XINCREF(self);
+ op->m_self = self;
+ Py_XINCREF(module);
+ op->m_module = module;
+ _PyObject_GC_TRACK(op);
+ return (PyObject *)op;
}
PyCFunction
PyCFunction_GetFunction(PyObject *op)
{
- if (!PyCFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
+ if (!PyCFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyCFunctionObject *)op) -> m_ml -> ml_meth;
}
PyObject *
PyCFunction_GetSelf(PyObject *op)
{
- if (!PyCFunction_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return ((PyCFunctionObject *)op) -> m_self;
+ if (!PyCFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return ((PyCFunctionObject *)op) -> m_self;
}
int
PyCFunction_GetFlags(PyObject *op)
{
- if (!PyCFunction_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
+ if (!PyCFunction_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return ((PyCFunctionObject *)op) -> m_ml -> ml_flags;
}
PyObject *
PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
{
- PyCFunctionObject* f = (PyCFunctionObject*)func;
- PyCFunction meth = PyCFunction_GET_FUNCTION(func);
- PyObject *self = PyCFunction_GET_SELF(func);
- Py_ssize_t size;
-
- switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
- case METH_VARARGS:
- if (kw == NULL || PyDict_Size(kw) == 0)
- return (*meth)(self, arg);
- break;
- case METH_VARARGS | METH_KEYWORDS:
- return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
- case METH_NOARGS:
- if (kw == NULL || PyDict_Size(kw) == 0) {
- size = PyTuple_GET_SIZE(arg);
- if (size == 0)
- return (*meth)(self, NULL);
- PyErr_Format(PyExc_TypeError,
- "%.200s() takes no arguments (%zd given)",
- f->m_ml->ml_name, size);
- return NULL;
- }
- break;
- case METH_O:
- if (kw == NULL || PyDict_Size(kw) == 0) {
- size = PyTuple_GET_SIZE(arg);
- if (size == 1)
- return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
- PyErr_Format(PyExc_TypeError,
- "%.200s() takes exactly one argument (%zd given)",
- f->m_ml->ml_name, size);
- return NULL;
- }
- break;
- default:
- PyErr_SetString(PyExc_SystemError, "Bad call flags in "
- "PyCFunction_Call. METH_OLDARGS is no "
- "longer supported!");
-
- return NULL;
- }
- PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
- f->m_ml->ml_name);
- return NULL;
+ PyCFunctionObject* f = (PyCFunctionObject*)func;
+ PyCFunction meth = PyCFunction_GET_FUNCTION(func);
+ PyObject *self = PyCFunction_GET_SELF(func);
+ Py_ssize_t size;
+
+ switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
+ case METH_VARARGS:
+ if (kw == NULL || PyDict_Size(kw) == 0)
+ return (*meth)(self, arg);
+ break;
+ case METH_VARARGS | METH_KEYWORDS:
+ return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
+ case METH_NOARGS:
+ if (kw == NULL || PyDict_Size(kw) == 0) {
+ size = PyTuple_GET_SIZE(arg);
+ if (size == 0)
+ return (*meth)(self, NULL);
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() takes no arguments (%zd given)",
+ f->m_ml->ml_name, size);
+ return NULL;
+ }
+ break;
+ case METH_O:
+ if (kw == NULL || PyDict_Size(kw) == 0) {
+ size = PyTuple_GET_SIZE(arg);
+ if (size == 1)
+ return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() takes exactly one argument (%zd given)",
+ f->m_ml->ml_name, size);
+ return NULL;
+ }
+ break;
+ default:
+ PyErr_SetString(PyExc_SystemError, "Bad call flags in "
+ "PyCFunction_Call. METH_OLDARGS is no "
+ "longer supported!");
+
+ return NULL;
+ }
+ PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
+ f->m_ml->ml_name);
+ return NULL;
}
/* Methods (the standard built-in methods, that is) */
@@ -121,163 +121,163 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
static void
meth_dealloc(PyCFunctionObject *m)
{
- _PyObject_GC_UNTRACK(m);
- Py_XDECREF(m->m_self);
- Py_XDECREF(m->m_module);
- if (numfree < PyCFunction_MAXFREELIST) {
- m->m_self = (PyObject *)free_list;
- free_list = m;
- numfree++;
- }
- else {
- PyObject_GC_Del(m);
- }
+ _PyObject_GC_UNTRACK(m);
+ Py_XDECREF(m->m_self);
+ Py_XDECREF(m->m_module);
+ if (numfree < PyCFunction_MAXFREELIST) {
+ m->m_self = (PyObject *)free_list;
+ free_list = m;
+ numfree++;
+ }
+ else {
+ PyObject_GC_Del(m);
+ }
}
static PyObject *
meth_get__doc__(PyCFunctionObject *m, void *closure)
{
- const char *doc = m->m_ml->ml_doc;
+ const char *doc = m->m_ml->ml_doc;
- if (doc != NULL)
- return PyUnicode_FromString(doc);
- Py_INCREF(Py_None);
- return Py_None;
+ if (doc != NULL)
+ return PyUnicode_FromString(doc);
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
meth_get__name__(PyCFunctionObject *m, void *closure)
{
- return PyUnicode_FromString(m->m_ml->ml_name);
+ return PyUnicode_FromString(m->m_ml->ml_name);
}
static int
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
{
- Py_VISIT(m->m_self);
- Py_VISIT(m->m_module);
- return 0;
+ Py_VISIT(m->m_self);
+ Py_VISIT(m->m_module);
+ return 0;
}
static PyObject *
meth_get__self__(PyCFunctionObject *m, void *closure)
{
- PyObject *self;
+ PyObject *self;
- self = m->m_self;
- if (self == NULL)
- self = Py_None;
- Py_INCREF(self);
- return self;
+ self = m->m_self;
+ if (self == NULL)
+ self = Py_None;
+ Py_INCREF(self);
+ return self;
}
static PyGetSetDef meth_getsets [] = {
- {"__doc__", (getter)meth_get__doc__, NULL, NULL},
- {"__name__", (getter)meth_get__name__, NULL, NULL},
- {"__self__", (getter)meth_get__self__, NULL, NULL},
- {0}
+ {"__doc__", (getter)meth_get__doc__, NULL, NULL},
+ {"__name__", (getter)meth_get__name__, NULL, NULL},
+ {"__self__", (getter)meth_get__self__, NULL, NULL},
+ {0}
};
#define OFF(x) offsetof(PyCFunctionObject, x)
static PyMemberDef meth_members[] = {
- {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
- {NULL}
+ {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
+ {NULL}
};
static PyObject *
meth_repr(PyCFunctionObject *m)
{
- if (m->m_self == NULL || PyModule_Check(m->m_self))
- return PyUnicode_FromFormat("<built-in function %s>",
- m->m_ml->ml_name);
- return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
- m->m_ml->ml_name,
- m->m_self->ob_type->tp_name,
- m->m_self);
+ if (m->m_self == NULL || PyModule_Check(m->m_self))
+ return PyUnicode_FromFormat("<built-in function %s>",
+ m->m_ml->ml_name);
+ return PyUnicode_FromFormat("<built-in method %s of %s object at %p>",
+ m->m_ml->ml_name,
+ m->m_self->ob_type->tp_name,
+ m->m_self);
}
static PyObject *
meth_richcompare(PyObject *self, PyObject *other, int op)
{
- PyCFunctionObject *a, *b;
- PyObject *res;
- int eq;
-
- if ((op != Py_EQ && op != Py_NE) ||
- !PyCFunction_Check(self) ||
- !PyCFunction_Check(other))
- {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- a = (PyCFunctionObject *)self;
- b = (PyCFunctionObject *)other;
- eq = a->m_self == b->m_self;
- if (eq)
- eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
- if (op == Py_EQ)
- res = eq ? Py_True : Py_False;
- else
- res = eq ? Py_False : Py_True;
- Py_INCREF(res);
- return res;
+ PyCFunctionObject *a, *b;
+ PyObject *res;
+ int eq;
+
+ if ((op != Py_EQ && op != Py_NE) ||
+ !PyCFunction_Check(self) ||
+ !PyCFunction_Check(other))
+ {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ a = (PyCFunctionObject *)self;
+ b = (PyCFunctionObject *)other;
+ eq = a->m_self == b->m_self;
+ if (eq)
+ eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
+ if (op == Py_EQ)
+ res = eq ? Py_True : Py_False;
+ else
+ res = eq ? Py_False : Py_True;
+ Py_INCREF(res);
+ return res;
}
static long
meth_hash(PyCFunctionObject *a)
{
- long x,y;
- if (a->m_self == NULL)
- x = 0;
- else {
- x = PyObject_Hash(a->m_self);
- if (x == -1)
- return -1;
- }
- y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
- if (y == -1)
- return -1;
- x ^= y;
- if (x == -1)
- x = -2;
- return x;
+ long x,y;
+ if (a->m_self == NULL)
+ x = 0;
+ else {
+ x = PyObject_Hash(a->m_self);
+ if (x == -1)
+ return -1;
+ }
+ y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
+ if (y == -1)
+ return -1;
+ x ^= y;
+ if (x == -1)
+ x = -2;
+ return x;
}
PyTypeObject PyCFunction_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "builtin_function_or_method",
- sizeof(PyCFunctionObject),
- 0,
- (destructor)meth_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)meth_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)meth_hash, /* tp_hash */
- PyCFunction_Call, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)meth_traverse, /* tp_traverse */
- 0, /* tp_clear */
- meth_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- meth_members, /* tp_members */
- meth_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "builtin_function_or_method",
+ sizeof(PyCFunctionObject),
+ 0,
+ (destructor)meth_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)meth_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)meth_hash, /* tp_hash */
+ PyCFunction_Call, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)meth_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ meth_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ meth_members, /* tp_members */
+ meth_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
};
/* Clear out the free list */
@@ -285,22 +285,22 @@ PyTypeObject PyCFunction_Type = {
int
PyCFunction_ClearFreeList(void)
{
- int freelist_size = numfree;
-
- while (free_list) {
- PyCFunctionObject *v = free_list;
- free_list = (PyCFunctionObject *)(v->m_self);
- PyObject_GC_Del(v);
- numfree--;
- }
- assert(numfree == 0);
- return freelist_size;
+ int freelist_size = numfree;
+
+ while (free_list) {
+ PyCFunctionObject *v = free_list;
+ free_list = (PyCFunctionObject *)(v->m_self);
+ PyObject_GC_Del(v);
+ numfree--;
+ }
+ assert(numfree == 0);
+ return freelist_size;
}
void
PyCFunction_Fini(void)
{
- (void)PyCFunction_ClearFreeList();
+ (void)PyCFunction_ClearFreeList();
}
/* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
@@ -314,5 +314,5 @@ PyAPI_FUNC(PyObject *) PyCFunction_New(PyMethodDef *, PyObject *);
PyObject *
PyCFunction_New(PyMethodDef *ml, PyObject *self)
{
- return PyCFunction_NewEx(ml, self, NULL);
+ return PyCFunction_NewEx(ml, self, NULL);
}
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index c1dd228..def3781 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -7,53 +7,53 @@
static Py_ssize_t max_module_number;
typedef struct {
- PyObject_HEAD
- PyObject *md_dict;
- struct PyModuleDef *md_def;
- void *md_state;
+ PyObject_HEAD
+ PyObject *md_dict;
+ struct PyModuleDef *md_def;
+ void *md_state;
} PyModuleObject;
static PyMemberDef module_members[] = {
- {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
- {0}
+ {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
+ {0}
};
static PyTypeObject moduledef_type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "moduledef", /* tp_name */
- sizeof(struct PyModuleDef), /* tp_size */
- 0, /* tp_itemsize */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "moduledef", /* tp_name */
+ sizeof(struct PyModuleDef), /* tp_size */
+ 0, /* tp_itemsize */
};
PyObject *
PyModule_New(const char *name)
{
- PyModuleObject *m;
- PyObject *nameobj;
- m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
- if (m == NULL)
- return NULL;
- m->md_def = NULL;
- m->md_state = NULL;
- nameobj = PyUnicode_FromString(name);
- m->md_dict = PyDict_New();
- if (m->md_dict == NULL || nameobj == NULL)
- goto fail;
- if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
- goto fail;
- if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
- goto fail;
- if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
- goto fail;
- Py_DECREF(nameobj);
- PyObject_GC_Track(m);
- return (PyObject *)m;
+ PyModuleObject *m;
+ PyObject *nameobj;
+ m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
+ if (m == NULL)
+ return NULL;
+ m->md_def = NULL;
+ m->md_state = NULL;
+ nameobj = PyUnicode_FromString(name);
+ m->md_dict = PyDict_New();
+ if (m->md_dict == NULL || nameobj == NULL)
+ goto fail;
+ if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
+ goto fail;
+ if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
+ goto fail;
+ if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
+ goto fail;
+ Py_DECREF(nameobj);
+ PyObject_GC_Track(m);
+ return (PyObject *)m;
fail:
- Py_XDECREF(nameobj);
- Py_DECREF(m);
- return NULL;
+ Py_XDECREF(nameobj);
+ Py_DECREF(m);
+ return NULL;
}
static char api_version_warning[] =
@@ -63,221 +63,221 @@ static char api_version_warning[] =
PyObject *
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
{
- PyObject *d, *v, *n;
- PyMethodDef *ml;
- const char* name;
- PyModuleObject *m;
- if (!Py_IsInitialized())
- Py_FatalError("Interpreter not initialized (version mismatch?)");
- if (PyType_Ready(&moduledef_type) < 0)
- return NULL;
- if (module->m_base.m_index == 0) {
- max_module_number++;
- Py_REFCNT(module) = 1;
- Py_TYPE(module) = &moduledef_type;
- module->m_base.m_index = max_module_number;
- }
- name = module->m_name;
- if (module_api_version != PYTHON_API_VERSION) {
- char message[512];
- PyOS_snprintf(message, sizeof(message),
- api_version_warning, name,
- PYTHON_API_VERSION, name,
- module_api_version);
- if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1))
- return NULL;
- }
- /* Make sure name is fully qualified.
-
- This is a bit of a hack: when the shared library is loaded,
- the module name is "package.module", but the module calls
- PyModule_Create*() with just "module" for the name. The shared
- library loader squirrels away the true name of the module in
- _Py_PackageContext, and PyModule_Create*() will substitute this
- (if the name actually matches).
- */
- if (_Py_PackageContext != NULL) {
- char *p = strrchr(_Py_PackageContext, '.');
- if (p != NULL && strcmp(module->m_name, p+1) == 0) {
- name = _Py_PackageContext;
- _Py_PackageContext = NULL;
- }
- }
- if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
- return NULL;
-
- if (module->m_size > 0) {
- m->md_state = PyMem_MALLOC(module->m_size);
- if (!m->md_state) {
- PyErr_NoMemory();
- Py_DECREF(m);
- return NULL;
- }
- memset(m->md_state, 0, module->m_size);
- }
-
- d = PyModule_GetDict((PyObject*)m);
- if (module->m_methods != NULL) {
- n = PyUnicode_FromString(name);
- if (n == NULL)
- return NULL;
- for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
- if ((ml->ml_flags & METH_CLASS) ||
- (ml->ml_flags & METH_STATIC)) {
- PyErr_SetString(PyExc_ValueError,
- "module functions cannot set"
- " METH_CLASS or METH_STATIC");
- Py_DECREF(n);
- return NULL;
- }
- v = PyCFunction_NewEx(ml, (PyObject*)m, n);
- if (v == NULL) {
- Py_DECREF(n);
- return NULL;
- }
- if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
- Py_DECREF(v);
- Py_DECREF(n);
- return NULL;
- }
- Py_DECREF(v);
- }
- Py_DECREF(n);
- }
- if (module->m_doc != NULL) {
- v = PyUnicode_FromString(module->m_doc);
- if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
- Py_XDECREF(v);
- return NULL;
- }
- Py_DECREF(v);
- }
- m->md_def = module;
- return (PyObject*)m;
+ PyObject *d, *v, *n;
+ PyMethodDef *ml;
+ const char* name;
+ PyModuleObject *m;
+ if (!Py_IsInitialized())
+ Py_FatalError("Interpreter not initialized (version mismatch?)");
+ if (PyType_Ready(&moduledef_type) < 0)
+ return NULL;
+ if (module->m_base.m_index == 0) {
+ max_module_number++;
+ Py_REFCNT(module) = 1;
+ Py_TYPE(module) = &moduledef_type;
+ module->m_base.m_index = max_module_number;
+ }
+ name = module->m_name;
+ if (module_api_version != PYTHON_API_VERSION) {
+ char message[512];
+ PyOS_snprintf(message, sizeof(message),
+ api_version_warning, name,
+ PYTHON_API_VERSION, name,
+ module_api_version);
+ if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1))
+ return NULL;
+ }
+ /* Make sure name is fully qualified.
+
+ This is a bit of a hack: when the shared library is loaded,
+ the module name is "package.module", but the module calls
+ PyModule_Create*() with just "module" for the name. The shared
+ library loader squirrels away the true name of the module in
+ _Py_PackageContext, and PyModule_Create*() will substitute this
+ (if the name actually matches).
+ */
+ if (_Py_PackageContext != NULL) {
+ char *p = strrchr(_Py_PackageContext, '.');
+ if (p != NULL && strcmp(module->m_name, p+1) == 0) {
+ name = _Py_PackageContext;
+ _Py_PackageContext = NULL;
+ }
+ }
+ if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
+ return NULL;
+
+ if (module->m_size > 0) {
+ m->md_state = PyMem_MALLOC(module->m_size);
+ if (!m->md_state) {
+ PyErr_NoMemory();
+ Py_DECREF(m);
+ return NULL;
+ }
+ memset(m->md_state, 0, module->m_size);
+ }
+
+ d = PyModule_GetDict((PyObject*)m);
+ if (module->m_methods != NULL) {
+ n = PyUnicode_FromString(name);
+ if (n == NULL)
+ return NULL;
+ for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
+ if ((ml->ml_flags & METH_CLASS) ||
+ (ml->ml_flags & METH_STATIC)) {
+ PyErr_SetString(PyExc_ValueError,
+ "module functions cannot set"
+ " METH_CLASS or METH_STATIC");
+ Py_DECREF(n);
+ return NULL;
+ }
+ v = PyCFunction_NewEx(ml, (PyObject*)m, n);
+ if (v == NULL) {
+ Py_DECREF(n);
+ return NULL;
+ }
+ if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
+ Py_DECREF(v);
+ Py_DECREF(n);
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ Py_DECREF(n);
+ }
+ if (module->m_doc != NULL) {
+ v = PyUnicode_FromString(module->m_doc);
+ if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
+ Py_XDECREF(v);
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ m->md_def = module;
+ return (PyObject*)m;
}
PyObject *
PyModule_GetDict(PyObject *m)
{
- PyObject *d;
- if (!PyModule_Check(m)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- d = ((PyModuleObject *)m) -> md_dict;
- if (d == NULL)
- ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
- return d;
+ PyObject *d;
+ if (!PyModule_Check(m)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ d = ((PyModuleObject *)m) -> md_dict;
+ if (d == NULL)
+ ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
+ return d;
}
const char *
PyModule_GetName(PyObject *m)
{
- PyObject *d;
- PyObject *nameobj;
- if (!PyModule_Check(m)) {
- PyErr_BadArgument();
- return NULL;
- }
- d = ((PyModuleObject *)m)->md_dict;
- if (d == NULL ||
- (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
- !PyUnicode_Check(nameobj))
- {
- PyErr_SetString(PyExc_SystemError, "nameless module");
- return NULL;
- }
- return _PyUnicode_AsString(nameobj);
+ PyObject *d;
+ PyObject *nameobj;
+ if (!PyModule_Check(m)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ d = ((PyModuleObject *)m)->md_dict;
+ if (d == NULL ||
+ (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
+ !PyUnicode_Check(nameobj))
+ {
+ PyErr_SetString(PyExc_SystemError, "nameless module");
+ return NULL;
+ }
+ return _PyUnicode_AsString(nameobj);
}
const char *
PyModule_GetFilename(PyObject *m)
{
- PyObject *d;
- PyObject *fileobj;
- if (!PyModule_Check(m)) {
- PyErr_BadArgument();
- return NULL;
- }
- d = ((PyModuleObject *)m)->md_dict;
- if (d == NULL ||
- (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
- !PyUnicode_Check(fileobj))
- {
- PyErr_SetString(PyExc_SystemError, "module filename missing");
- return NULL;
- }
- return _PyUnicode_AsString(fileobj);
+ PyObject *d;
+ PyObject *fileobj;
+ if (!PyModule_Check(m)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ d = ((PyModuleObject *)m)->md_dict;
+ if (d == NULL ||
+ (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
+ !PyUnicode_Check(fileobj))
+ {
+ PyErr_SetString(PyExc_SystemError, "module filename missing");
+ return NULL;
+ }
+ return _PyUnicode_AsString(fileobj);
}
PyModuleDef*
PyModule_GetDef(PyObject* m)
{
- if (!PyModule_Check(m)) {
- PyErr_BadArgument();
- return NULL;
- }
- return ((PyModuleObject *)m)->md_def;
+ if (!PyModule_Check(m)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ return ((PyModuleObject *)m)->md_def;
}
void*
PyModule_GetState(PyObject* m)
{
- if (!PyModule_Check(m)) {
- PyErr_BadArgument();
- return NULL;
- }
- return ((PyModuleObject *)m)->md_state;
+ if (!PyModule_Check(m)) {
+ PyErr_BadArgument();
+ return NULL;
+ }
+ return ((PyModuleObject *)m)->md_state;
}
void
_PyModule_Clear(PyObject *m)
{
- /* To make the execution order of destructors for global
- objects a bit more predictable, we first zap all objects
- whose name starts with a single underscore, before we clear
- the entire dictionary. We zap them by replacing them with
- None, rather than deleting them from the dictionary, to
- avoid rehashing the dictionary (to some extent). */
-
- Py_ssize_t pos;
- PyObject *key, *value;
- PyObject *d;
-
- d = ((PyModuleObject *)m)->md_dict;
- if (d == NULL)
- return;
-
- /* First, clear only names starting with a single underscore */
- pos = 0;
- while (PyDict_Next(d, &pos, &key, &value)) {
- if (value != Py_None && PyUnicode_Check(key)) {
- const char *s = _PyUnicode_AsString(key);
- if (s[0] == '_' && s[1] != '_') {
- if (Py_VerboseFlag > 1)
- PySys_WriteStderr("# clear[1] %s\n", s);
- PyDict_SetItem(d, key, Py_None);
- }
- }
- }
-
- /* Next, clear all names except for __builtins__ */
- pos = 0;
- while (PyDict_Next(d, &pos, &key, &value)) {
- if (value != Py_None && PyUnicode_Check(key)) {
- const char *s = _PyUnicode_AsString(key);
- if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
- if (Py_VerboseFlag > 1)
- PySys_WriteStderr("# clear[2] %s\n", s);
- PyDict_SetItem(d, key, Py_None);
- }
- }
- }
-
- /* Note: we leave __builtins__ in place, so that destructors
- of non-global objects defined in this module can still use
- builtins, in particularly 'None'. */
+ /* To make the execution order of destructors for global
+ objects a bit more predictable, we first zap all objects
+ whose name starts with a single underscore, before we clear
+ the entire dictionary. We zap them by replacing them with
+ None, rather than deleting them from the dictionary, to
+ avoid rehashing the dictionary (to some extent). */
+
+ Py_ssize_t pos;
+ PyObject *key, *value;
+ PyObject *d;
+
+ d = ((PyModuleObject *)m)->md_dict;
+ if (d == NULL)
+ return;
+
+ /* First, clear only names starting with a single underscore */
+ pos = 0;
+ while (PyDict_Next(d, &pos, &key, &value)) {
+ if (value != Py_None && PyUnicode_Check(key)) {
+ const char *s = _PyUnicode_AsString(key);
+ if (s[0] == '_' && s[1] != '_') {
+ if (Py_VerboseFlag > 1)
+ PySys_WriteStderr("# clear[1] %s\n", s);
+ PyDict_SetItem(d, key, Py_None);
+ }
+ }
+ }
+
+ /* Next, clear all names except for __builtins__ */
+ pos = 0;
+ while (PyDict_Next(d, &pos, &key, &value)) {
+ if (value != Py_None && PyUnicode_Check(key)) {
+ const char *s = _PyUnicode_AsString(key);
+ if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
+ if (Py_VerboseFlag > 1)
+ PySys_WriteStderr("# clear[2] %s\n", s);
+ PyDict_SetItem(d, key, Py_None);
+ }
+ }
+ }
+
+ /* Note: we leave __builtins__ in place, so that destructors
+ of non-global objects defined in this module can still use
+ builtins, in particularly 'None'. */
}
@@ -286,86 +286,86 @@ _PyModule_Clear(PyObject *m)
static int
module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"name", "doc", NULL};
- PyObject *dict, *name = Py_None, *doc = Py_None;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
- kwlist, &name, &doc))
- return -1;
- dict = m->md_dict;
- if (dict == NULL) {
- dict = PyDict_New();
- if (dict == NULL)
- return -1;
- m->md_dict = dict;
- }
- if (PyDict_SetItemString(dict, "__name__", name) < 0)
- return -1;
- if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
- return -1;
- return 0;
+ static char *kwlist[] = {"name", "doc", NULL};
+ PyObject *dict, *name = Py_None, *doc = Py_None;
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
+ kwlist, &name, &doc))
+ return -1;
+ dict = m->md_dict;
+ if (dict == NULL) {
+ dict = PyDict_New();
+ if (dict == NULL)
+ return -1;
+ m->md_dict = dict;
+ }
+ if (PyDict_SetItemString(dict, "__name__", name) < 0)
+ return -1;
+ if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
+ return -1;
+ return 0;
}
static void
module_dealloc(PyModuleObject *m)
{
- PyObject_GC_UnTrack(m);
- if (m->md_def && m->md_def->m_free)
- m->md_def->m_free(m);
- if (m->md_dict != NULL) {
- /* If we are the only ones holding a reference, we can clear
- the dictionary. */
- if (Py_REFCNT(m->md_dict) == 1)
- _PyModule_Clear((PyObject *)m);
- Py_DECREF(m->md_dict);
- }
- if (m->md_state != NULL)
- PyMem_FREE(m->md_state);
- Py_TYPE(m)->tp_free((PyObject *)m);
+ PyObject_GC_UnTrack(m);
+ if (m->md_def && m->md_def->m_free)
+ m->md_def->m_free(m);
+ if (m->md_dict != NULL) {
+ /* If we are the only ones holding a reference, we can clear
+ the dictionary. */
+ if (Py_REFCNT(m->md_dict) == 1)
+ _PyModule_Clear((PyObject *)m);
+ Py_DECREF(m->md_dict);
+ }
+ if (m->md_state != NULL)
+ PyMem_FREE(m->md_state);
+ Py_TYPE(m)->tp_free((PyObject *)m);
}
static PyObject *
module_repr(PyModuleObject *m)
{
- const char *name;
- const char *filename;
-
- name = PyModule_GetName((PyObject *)m);
- if (name == NULL) {
- PyErr_Clear();
- name = "?";
- }
- filename = PyModule_GetFilename((PyObject *)m);
- if (filename == NULL) {
- PyErr_Clear();
- return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
- }
- return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
+ const char *name;
+ const char *filename;
+
+ name = PyModule_GetName((PyObject *)m);
+ if (name == NULL) {
+ PyErr_Clear();
+ name = "?";
+ }
+ filename = PyModule_GetFilename((PyObject *)m);
+ if (filename == NULL) {
+ PyErr_Clear();
+ return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
+ }
+ return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
}
static int
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
{
- if (m->md_def && m->md_def->m_traverse) {
- int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
- if (res)
- return res;
- }
- Py_VISIT(m->md_dict);
- return 0;
+ if (m->md_def && m->md_def->m_traverse) {
+ int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
+ if (res)
+ return res;
+ }
+ Py_VISIT(m->md_dict);
+ return 0;
}
static int
module_clear(PyModuleObject *m)
{
- if (m->md_def && m->md_def->m_clear) {
- int res = m->md_def->m_clear((PyObject*)m);
- if (res)
- return res;
- }
- Py_CLEAR(m->md_dict);
- return 0;
+ if (m->md_def && m->md_def->m_clear) {
+ int res = m->md_def->m_clear((PyObject*)m);
+ if (res)
+ return res;
+ }
+ Py_CLEAR(m->md_dict);
+ return 0;
}
-
+
PyDoc_STRVAR(module_doc,
"module(name[, doc])\n\
@@ -374,44 +374,44 @@ Create a module object.\n\
The name must be a string; the optional doc argument can have any type.");
PyTypeObject PyModule_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "module", /* tp_name */
- sizeof(PyModuleObject), /* tp_size */
- 0, /* tp_itemsize */
- (destructor)module_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)module_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- module_doc, /* tp_doc */
- (traverseproc)module_traverse, /* tp_traverse */
- (inquiry)module_clear, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- module_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
- (initproc)module_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "module", /* tp_name */
+ sizeof(PyModuleObject), /* tp_size */
+ 0, /* tp_itemsize */
+ (destructor)module_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)module_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ module_doc, /* tp_doc */
+ (traverseproc)module_traverse, /* tp_traverse */
+ (inquiry)module_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ module_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
+ (initproc)module_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
diff --git a/Objects/object.c b/Objects/object.c
index eba061b..1b8d4e1 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -15,18 +15,18 @@ Py_ssize_t _Py_RefTotal;
Py_ssize_t
_Py_GetRefTotal(void)
{
- PyObject *o;
- Py_ssize_t total = _Py_RefTotal;
- /* ignore the references to the dummy object of the dicts and sets
- because they are not reliable and not useful (now that the
- hash table code is well-tested) */
- o = _PyDict_Dummy();
- if (o != NULL)
- total -= o->ob_refcnt;
- o = _PySet_Dummy();
- if (o != NULL)
- total -= o->ob_refcnt;
- return total;
+ PyObject *o;
+ Py_ssize_t total = _Py_RefTotal;
+ /* ignore the references to the dummy object of the dicts and sets
+ because they are not reliable and not useful (now that the
+ hash table code is well-tested) */
+ o = _PyDict_Dummy();
+ if (o != NULL)
+ total -= o->ob_refcnt;
+ o = _PySet_Dummy();
+ if (o != NULL)
+ total -= o->ob_refcnt;
+ return total;
}
#endif /* Py_REF_DEBUG */
@@ -58,21 +58,21 @@ void
_Py_AddToAllObjects(PyObject *op, int force)
{
#ifdef Py_DEBUG
- if (!force) {
- /* If it's initialized memory, op must be in or out of
- * the list unambiguously.
- */
- assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
- }
+ if (!force) {
+ /* If it's initialized memory, op must be in or out of
+ * the list unambiguously.
+ */
+ assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
+ }
#endif
- if (force || op->_ob_prev == NULL) {
- op->_ob_next = refchain._ob_next;
- op->_ob_prev = &refchain;
- refchain._ob_next->_ob_prev = op;
- refchain._ob_next = op;
- }
+ if (force || op->_ob_prev == NULL) {
+ op->_ob_next = refchain._ob_next;
+ op->_ob_prev = &refchain;
+ refchain._ob_next->_ob_prev = op;
+ refchain._ob_next = op;
+ }
}
-#endif /* Py_TRACE_REFS */
+#endif /* Py_TRACE_REFS */
#ifdef COUNT_ALLOCS
static PyTypeObject *type_list;
@@ -89,99 +89,99 @@ extern Py_ssize_t null_strings, one_strings;
void
dump_counts(FILE* f)
{
- PyTypeObject *tp;
-
- for (tp = type_list; tp; tp = tp->tp_next)
- fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
- "freed: %" PY_FORMAT_SIZE_T "d, "
- "max in use: %" PY_FORMAT_SIZE_T "d\n",
- tp->tp_name, tp->tp_allocs, tp->tp_frees,
- tp->tp_maxalloc);
- fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
- "empty: %" PY_FORMAT_SIZE_T "d\n",
- fast_tuple_allocs, tuple_zero_allocs);
- fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
- "neg: %" PY_FORMAT_SIZE_T "d\n",
- quick_int_allocs, quick_neg_int_allocs);
- fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
- "1-strings: %" PY_FORMAT_SIZE_T "d\n",
- null_strings, one_strings);
+ PyTypeObject *tp;
+
+ for (tp = type_list; tp; tp = tp->tp_next)
+ fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
+ "freed: %" PY_FORMAT_SIZE_T "d, "
+ "max in use: %" PY_FORMAT_SIZE_T "d\n",
+ tp->tp_name, tp->tp_allocs, tp->tp_frees,
+ tp->tp_maxalloc);
+ fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
+ "empty: %" PY_FORMAT_SIZE_T "d\n",
+ fast_tuple_allocs, tuple_zero_allocs);
+ fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
+ "neg: %" PY_FORMAT_SIZE_T "d\n",
+ quick_int_allocs, quick_neg_int_allocs);
+ fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
+ "1-strings: %" PY_FORMAT_SIZE_T "d\n",
+ null_strings, one_strings);
}
PyObject *
get_counts(void)
{
- PyTypeObject *tp;
- PyObject *result;
- PyObject *v;
-
- result = PyList_New(0);
- if (result == NULL)
- return NULL;
- for (tp = type_list; tp; tp = tp->tp_next) {
- v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
- tp->tp_frees, tp->tp_maxalloc);
- if (v == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- if (PyList_Append(result, v) < 0) {
- Py_DECREF(v);
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(v);
- }
- return result;
+ PyTypeObject *tp;
+ PyObject *result;
+ PyObject *v;
+
+ result = PyList_New(0);
+ if (result == NULL)
+ return NULL;
+ for (tp = type_list; tp; tp = tp->tp_next) {
+ v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
+ tp->tp_frees, tp->tp_maxalloc);
+ if (v == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ if (PyList_Append(result, v) < 0) {
+ Py_DECREF(v);
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(v);
+ }
+ return result;
}
void
inc_count(PyTypeObject *tp)
{
- if (tp->tp_next == NULL && tp->tp_prev == NULL) {
- /* first time; insert in linked list */
- if (tp->tp_next != NULL) /* sanity check */
- Py_FatalError("XXX inc_count sanity check");
- if (type_list)
- type_list->tp_prev = tp;
- tp->tp_next = type_list;
- /* Note that as of Python 2.2, heap-allocated type objects
- * can go away, but this code requires that they stay alive
- * until program exit. That's why we're careful with
- * refcounts here. type_list gets a new reference to tp,
- * while ownership of the reference type_list used to hold
- * (if any) was transferred to tp->tp_next in the line above.
- * tp is thus effectively immortal after this.
- */
- Py_INCREF(tp);
- type_list = tp;
+ if (tp->tp_next == NULL && tp->tp_prev == NULL) {
+ /* first time; insert in linked list */
+ if (tp->tp_next != NULL) /* sanity check */
+ Py_FatalError("XXX inc_count sanity check");
+ if (type_list)
+ type_list->tp_prev = tp;
+ tp->tp_next = type_list;
+ /* Note that as of Python 2.2, heap-allocated type objects
+ * can go away, but this code requires that they stay alive
+ * until program exit. That's why we're careful with
+ * refcounts here. type_list gets a new reference to tp,
+ * while ownership of the reference type_list used to hold
+ * (if any) was transferred to tp->tp_next in the line above.
+ * tp is thus effectively immortal after this.
+ */
+ Py_INCREF(tp);
+ type_list = tp;
#ifdef Py_TRACE_REFS
- /* Also insert in the doubly-linked list of all objects,
- * if not already there.
- */
- _Py_AddToAllObjects((PyObject *)tp, 0);
+ /* Also insert in the doubly-linked list of all objects,
+ * if not already there.
+ */
+ _Py_AddToAllObjects((PyObject *)tp, 0);
#endif
- }
- tp->tp_allocs++;
- if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
- tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
+ }
+ tp->tp_allocs++;
+ if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
+ tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
}
void dec_count(PyTypeObject *tp)
{
- tp->tp_frees++;
- if (unlist_types_without_objects &&
- tp->tp_allocs == tp->tp_frees) {
- /* unlink the type from type_list */
- if (tp->tp_prev)
- tp->tp_prev->tp_next = tp->tp_next;
- else
- type_list = tp->tp_next;
- if (tp->tp_next)
- tp->tp_next->tp_prev = tp->tp_prev;
- tp->tp_next = tp->tp_prev = NULL;
- Py_DECREF(tp);
- }
+ tp->tp_frees++;
+ if (unlist_types_without_objects &&
+ tp->tp_allocs == tp->tp_frees) {
+ /* unlink the type from type_list */
+ if (tp->tp_prev)
+ tp->tp_prev->tp_next = tp->tp_next;
+ else
+ type_list = tp->tp_next;
+ if (tp->tp_next)
+ tp->tp_next->tp_prev = tp->tp_prev;
+ tp->tp_next = tp->tp_prev = NULL;
+ Py_DECREF(tp);
+ }
}
#endif
@@ -191,13 +191,13 @@ void dec_count(PyTypeObject *tp)
void
_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
{
- char buf[300];
+ char buf[300];
- PyOS_snprintf(buf, sizeof(buf),
- "%s:%i object at %p has negative ref count "
- "%" PY_FORMAT_SIZE_T "d",
- fname, lineno, op, op->ob_refcnt);
- Py_FatalError(buf);
+ PyOS_snprintf(buf, sizeof(buf),
+ "%s:%i object at %p has negative ref count "
+ "%" PY_FORMAT_SIZE_T "d",
+ fname, lineno, op, op->ob_refcnt);
+ Py_FatalError(buf);
}
#endif /* Py_REF_DEBUG */
@@ -217,123 +217,123 @@ Py_DecRef(PyObject *o)
PyObject *
PyObject_Init(PyObject *op, PyTypeObject *tp)
{
- if (op == NULL)
- return PyErr_NoMemory();
- /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
- Py_TYPE(op) = tp;
- _Py_NewReference(op);
- return op;
+ if (op == NULL)
+ return PyErr_NoMemory();
+ /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
+ Py_TYPE(op) = tp;
+ _Py_NewReference(op);
+ return op;
}
PyVarObject *
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
{
- if (op == NULL)
- return (PyVarObject *) PyErr_NoMemory();
- /* Any changes should be reflected in PyObject_INIT_VAR */
- op->ob_size = size;
- Py_TYPE(op) = tp;
- _Py_NewReference((PyObject *)op);
- return op;
+ if (op == NULL)
+ return (PyVarObject *) PyErr_NoMemory();
+ /* Any changes should be reflected in PyObject_INIT_VAR */
+ op->ob_size = size;
+ Py_TYPE(op) = tp;
+ _Py_NewReference((PyObject *)op);
+ return op;
}
PyObject *
_PyObject_New(PyTypeObject *tp)
{
- PyObject *op;
- op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
- if (op == NULL)
- return PyErr_NoMemory();
- return PyObject_INIT(op, tp);
+ PyObject *op;
+ op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
+ if (op == NULL)
+ return PyErr_NoMemory();
+ return PyObject_INIT(op, tp);
}
PyVarObject *
_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
{
- PyVarObject *op;
- const size_t size = _PyObject_VAR_SIZE(tp, nitems);
- op = (PyVarObject *) PyObject_MALLOC(size);
- if (op == NULL)
- return (PyVarObject *)PyErr_NoMemory();
- return PyObject_INIT_VAR(op, tp, nitems);
+ PyVarObject *op;
+ const size_t size = _PyObject_VAR_SIZE(tp, nitems);
+ op = (PyVarObject *) PyObject_MALLOC(size);
+ if (op == NULL)
+ return (PyVarObject *)PyErr_NoMemory();
+ return PyObject_INIT_VAR(op, tp, nitems);
}
/* Implementation of PyObject_Print with recursion checking */
static int
internal_print(PyObject *op, FILE *fp, int flags, int nesting)
{
- int ret = 0;
- if (nesting > 10) {
- PyErr_SetString(PyExc_RuntimeError, "print recursion");
- return -1;
- }
- if (PyErr_CheckSignals())
- return -1;
+ int ret = 0;
+ if (nesting > 10) {
+ PyErr_SetString(PyExc_RuntimeError, "print recursion");
+ return -1;
+ }
+ if (PyErr_CheckSignals())
+ return -1;
#ifdef USE_STACKCHECK
- if (PyOS_CheckStack()) {
- PyErr_SetString(PyExc_MemoryError, "stack overflow");
- return -1;
- }
+ if (PyOS_CheckStack()) {
+ PyErr_SetString(PyExc_MemoryError, "stack overflow");
+ return -1;
+ }
#endif
- clearerr(fp); /* Clear any previous error condition */
- if (op == NULL) {
- Py_BEGIN_ALLOW_THREADS
- fprintf(fp, "<nil>");
- Py_END_ALLOW_THREADS
- }
- else {
- if (op->ob_refcnt <= 0)
- /* XXX(twouters) cast refcount to long until %zd is
- universally available */
- Py_BEGIN_ALLOW_THREADS
- fprintf(fp, "<refcnt %ld at %p>",
- (long)op->ob_refcnt, op);
- Py_END_ALLOW_THREADS
- else {
- PyObject *s;
- if (flags & Py_PRINT_RAW)
- s = PyObject_Str(op);
- else
- s = PyObject_Repr(op);
- if (s == NULL)
- ret = -1;
- else if (PyBytes_Check(s)) {
- fwrite(PyBytes_AS_STRING(s), 1,
- PyBytes_GET_SIZE(s), fp);
- }
- else if (PyUnicode_Check(s)) {
- PyObject *t;
- t = _PyUnicode_AsDefaultEncodedString(s, NULL);
- if (t == NULL)
- ret = 0;
- else {
- fwrite(PyBytes_AS_STRING(t), 1,
- PyBytes_GET_SIZE(t), fp);
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "str() or repr() returned '%.100s'",
- s->ob_type->tp_name);
- ret = -1;
- }
- Py_XDECREF(s);
- }
- }
- if (ret == 0) {
- if (ferror(fp)) {
- PyErr_SetFromErrno(PyExc_IOError);
- clearerr(fp);
- ret = -1;
- }
- }
- return ret;
+ clearerr(fp); /* Clear any previous error condition */
+ if (op == NULL) {
+ Py_BEGIN_ALLOW_THREADS
+ fprintf(fp, "<nil>");
+ Py_END_ALLOW_THREADS
+ }
+ else {
+ if (op->ob_refcnt <= 0)
+ /* XXX(twouters) cast refcount to long until %zd is
+ universally available */
+ Py_BEGIN_ALLOW_THREADS
+ fprintf(fp, "<refcnt %ld at %p>",
+ (long)op->ob_refcnt, op);
+ Py_END_ALLOW_THREADS
+ else {
+ PyObject *s;
+ if (flags & Py_PRINT_RAW)
+ s = PyObject_Str(op);
+ else
+ s = PyObject_Repr(op);
+ if (s == NULL)
+ ret = -1;
+ else if (PyBytes_Check(s)) {
+ fwrite(PyBytes_AS_STRING(s), 1,
+ PyBytes_GET_SIZE(s), fp);
+ }
+ else if (PyUnicode_Check(s)) {
+ PyObject *t;
+ t = _PyUnicode_AsDefaultEncodedString(s, NULL);
+ if (t == NULL)
+ ret = 0;
+ else {
+ fwrite(PyBytes_AS_STRING(t), 1,
+ PyBytes_GET_SIZE(t), fp);
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "str() or repr() returned '%.100s'",
+ s->ob_type->tp_name);
+ ret = -1;
+ }
+ Py_XDECREF(s);
+ }
+ }
+ if (ret == 0) {
+ if (ferror(fp)) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ clearerr(fp);
+ ret = -1;
+ }
+ }
+ return ret;
}
int
PyObject_Print(PyObject *op, FILE *fp, int flags)
{
- return internal_print(op, fp, flags, 0);
+ return internal_print(op, fp, flags, 0);
}
/* For debugging convenience. Set a breakpoint here and call it from your DLL */
@@ -347,159 +347,159 @@ _Py_BreakPoint(void)
void
_PyObject_Dump(PyObject* op)
{
- if (op == NULL)
- fprintf(stderr, "NULL\n");
- else {
+ if (op == NULL)
+ fprintf(stderr, "NULL\n");
+ else {
#ifdef WITH_THREAD
- PyGILState_STATE gil;
+ PyGILState_STATE gil;
#endif
- fprintf(stderr, "object : ");
+ fprintf(stderr, "object : ");
#ifdef WITH_THREAD
- gil = PyGILState_Ensure();
+ gil = PyGILState_Ensure();
#endif
- (void)PyObject_Print(op, stderr, 0);
+ (void)PyObject_Print(op, stderr, 0);
#ifdef WITH_THREAD
- PyGILState_Release(gil);
+ PyGILState_Release(gil);
#endif
- /* XXX(twouters) cast refcount to long until %zd is
- universally available */
- fprintf(stderr, "\n"
- "type : %s\n"
- "refcount: %ld\n"
- "address : %p\n",
- Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
- (long)op->ob_refcnt,
- op);
- }
+ /* XXX(twouters) cast refcount to long until %zd is
+ universally available */
+ fprintf(stderr, "\n"
+ "type : %s\n"
+ "refcount: %ld\n"
+ "address : %p\n",
+ Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
+ (long)op->ob_refcnt,
+ op);
+ }
}
PyObject *
PyObject_Repr(PyObject *v)
{
- PyObject *res;
- if (PyErr_CheckSignals())
- return NULL;
+ PyObject *res;
+ if (PyErr_CheckSignals())
+ return NULL;
#ifdef USE_STACKCHECK
- if (PyOS_CheckStack()) {
- PyErr_SetString(PyExc_MemoryError, "stack overflow");
- return NULL;
- }
+ if (PyOS_CheckStack()) {
+ PyErr_SetString(PyExc_MemoryError, "stack overflow");
+ return NULL;
+ }
#endif
- if (v == NULL)
- return PyUnicode_FromString("<NULL>");
- if (Py_TYPE(v)->tp_repr == NULL)
- return PyUnicode_FromFormat("<%s object at %p>",
- v->ob_type->tp_name, v);
- res = (*v->ob_type->tp_repr)(v);
- if (res != NULL && !PyUnicode_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "__repr__ returned non-string (type %.200s)",
- res->ob_type->tp_name);
- Py_DECREF(res);
- return NULL;
- }
- return res;
+ if (v == NULL)
+ return PyUnicode_FromString("<NULL>");
+ if (Py_TYPE(v)->tp_repr == NULL)
+ return PyUnicode_FromFormat("<%s object at %p>",
+ v->ob_type->tp_name, v);
+ res = (*v->ob_type->tp_repr)(v);
+ if (res != NULL && !PyUnicode_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__repr__ returned non-string (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
}
PyObject *
PyObject_Str(PyObject *v)
{
- PyObject *res;
- if (PyErr_CheckSignals())
- return NULL;
+ PyObject *res;
+ if (PyErr_CheckSignals())
+ return NULL;
#ifdef USE_STACKCHECK
- if (PyOS_CheckStack()) {
- PyErr_SetString(PyExc_MemoryError, "stack overflow");
- return NULL;
- }
+ if (PyOS_CheckStack()) {
+ PyErr_SetString(PyExc_MemoryError, "stack overflow");
+ return NULL;
+ }
#endif
- if (v == NULL)
- return PyUnicode_FromString("<NULL>");
- if (PyUnicode_CheckExact(v)) {
- Py_INCREF(v);
- return v;
- }
- if (Py_TYPE(v)->tp_str == NULL)
- return PyObject_Repr(v);
-
- /* It is possible for a type to have a tp_str representation that loops
- infinitely. */
- if (Py_EnterRecursiveCall(" while getting the str of an object"))
- return NULL;
- res = (*Py_TYPE(v)->tp_str)(v);
- Py_LeaveRecursiveCall();
- if (res == NULL)
- return NULL;
- if (!PyUnicode_Check(res)) {
- PyErr_Format(PyExc_TypeError,
- "__str__ returned non-string (type %.200s)",
- Py_TYPE(res)->tp_name);
- Py_DECREF(res);
- return NULL;
- }
- return res;
+ if (v == NULL)
+ return PyUnicode_FromString("<NULL>");
+ if (PyUnicode_CheckExact(v)) {
+ Py_INCREF(v);
+ return v;
+ }
+ if (Py_TYPE(v)->tp_str == NULL)
+ return PyObject_Repr(v);
+
+ /* It is possible for a type to have a tp_str representation that loops
+ infinitely. */
+ if (Py_EnterRecursiveCall(" while getting the str of an object"))
+ return NULL;
+ res = (*Py_TYPE(v)->tp_str)(v);
+ Py_LeaveRecursiveCall();
+ if (res == NULL)
+ return NULL;
+ if (!PyUnicode_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__str__ returned non-string (type %.200s)",
+ Py_TYPE(res)->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
}
PyObject *
PyObject_ASCII(PyObject *v)
{
- PyObject *repr, *ascii, *res;
-
- repr = PyObject_Repr(v);
- if (repr == NULL)
- return NULL;
+ PyObject *repr, *ascii, *res;
+
+ repr = PyObject_Repr(v);
+ if (repr == NULL)
+ return NULL;
- /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
- ascii = PyUnicode_EncodeASCII(
- PyUnicode_AS_UNICODE(repr),
- PyUnicode_GET_SIZE(repr),
- "backslashreplace");
+ /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
+ ascii = PyUnicode_EncodeASCII(
+ PyUnicode_AS_UNICODE(repr),
+ PyUnicode_GET_SIZE(repr),
+ "backslashreplace");
- Py_DECREF(repr);
- if (ascii == NULL)
- return NULL;
+ Py_DECREF(repr);
+ if (ascii == NULL)
+ return NULL;
- res = PyUnicode_DecodeASCII(
- PyBytes_AS_STRING(ascii),
- PyBytes_GET_SIZE(ascii),
- NULL);
+ res = PyUnicode_DecodeASCII(
+ PyBytes_AS_STRING(ascii),
+ PyBytes_GET_SIZE(ascii),
+ NULL);
- Py_DECREF(ascii);
- return res;
+ Py_DECREF(ascii);
+ return res;
}
PyObject *
PyObject_Bytes(PyObject *v)
{
- PyObject *result, *func;
- static PyObject *bytesstring = NULL;
-
- if (v == NULL)
- return PyBytes_FromString("<NULL>");
-
- if (PyBytes_CheckExact(v)) {
- Py_INCREF(v);
- return v;
- }
-
- func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
- if (func != NULL) {
- result = PyObject_CallFunctionObjArgs(func, NULL);
- Py_DECREF(func);
- if (result == NULL)
- return NULL;
- if (!PyBytes_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "__bytes__ returned non-bytes (type %.200s)",
- Py_TYPE(result)->tp_name);
- Py_DECREF(result);
- return NULL;
- }
- return result;
- }
- else if (PyErr_Occurred())
- return NULL;
- return PyBytes_FromObject(v);
+ PyObject *result, *func;
+ static PyObject *bytesstring = NULL;
+
+ if (v == NULL)
+ return PyBytes_FromString("<NULL>");
+
+ if (PyBytes_CheckExact(v)) {
+ Py_INCREF(v);
+ return v;
+ }
+
+ func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
+ if (func != NULL) {
+ result = PyObject_CallFunctionObjArgs(func, NULL);
+ Py_DECREF(func);
+ if (result == NULL)
+ return NULL;
+ if (!PyBytes_Check(result)) {
+ PyErr_Format(PyExc_TypeError,
+ "__bytes__ returned non-bytes (type %.200s)",
+ Py_TYPE(result)->tp_name);
+ Py_DECREF(result);
+ return NULL;
+ }
+ return result;
+ }
+ else if (PyErr_Occurred())
+ return NULL;
+ return PyBytes_FromObject(v);
}
/* For Python 3.0.1 and later, the old three-way comparison has been
@@ -542,49 +542,49 @@ static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
static PyObject *
do_richcompare(PyObject *v, PyObject *w, int op)
{
- richcmpfunc f;
- PyObject *res;
-
- if (v->ob_type != w->ob_type &&
- PyType_IsSubtype(w->ob_type, v->ob_type) &&
- (f = w->ob_type->tp_richcompare) != NULL) {
- res = (*f)(w, v, _Py_SwappedOp[op]);
- if (res != Py_NotImplemented)
- return res;
- Py_DECREF(res);
- }
- if ((f = v->ob_type->tp_richcompare) != NULL) {
- res = (*f)(v, w, op);
- if (res != Py_NotImplemented)
- return res;
- Py_DECREF(res);
- }
- if ((f = w->ob_type->tp_richcompare) != NULL) {
- res = (*f)(w, v, _Py_SwappedOp[op]);
- if (res != Py_NotImplemented)
- return res;
- Py_DECREF(res);
- }
- /* If neither object implements it, provide a sensible default
- for == and !=, but raise an exception for ordering. */
- switch (op) {
- case Py_EQ:
- res = (v == w) ? Py_True : Py_False;
- break;
- case Py_NE:
- res = (v != w) ? Py_True : Py_False;
- break;
- default:
- /* XXX Special-case None so it doesn't show as NoneType() */
- PyErr_Format(PyExc_TypeError,
- "unorderable types: %.100s() %s %.100s()",
- v->ob_type->tp_name,
- opstrings[op],
- w->ob_type->tp_name);
- return NULL;
- }
- Py_INCREF(res);
- return res;
+ richcmpfunc f;
+ PyObject *res;
+
+ if (v->ob_type != w->ob_type &&
+ PyType_IsSubtype(w->ob_type, v->ob_type) &&
+ (f = w->ob_type->tp_richcompare) != NULL) {
+ res = (*f)(w, v, _Py_SwappedOp[op]);
+ if (res != Py_NotImplemented)
+ return res;
+ Py_DECREF(res);
+ }
+ if ((f = v->ob_type->tp_richcompare) != NULL) {
+ res = (*f)(v, w, op);
+ if (res != Py_NotImplemented)
+ return res;
+ Py_DECREF(res);
+ }
+ if ((f = w->ob_type->tp_richcompare) != NULL) {
+ res = (*f)(w, v, _Py_SwappedOp[op]);
+ if (res != Py_NotImplemented)
+ return res;
+ Py_DECREF(res);
+ }
+ /* If neither object implements it, provide a sensible default
+ for == and !=, but raise an exception for ordering. */
+ switch (op) {
+ case Py_EQ:
+ res = (v == w) ? Py_True : Py_False;
+ break;
+ case Py_NE:
+ res = (v != w) ? Py_True : Py_False;
+ break;
+ default:
+ /* XXX Special-case None so it doesn't show as NoneType() */
+ PyErr_Format(PyExc_TypeError,
+ "unorderable types: %.100s() %s %.100s()",
+ v->ob_type->tp_name,
+ opstrings[op],
+ w->ob_type->tp_name);
+ return NULL;
+ }
+ Py_INCREF(res);
+ return res;
}
/* Perform a rich comparison with object result. This wraps do_richcompare()
@@ -593,19 +593,19 @@ do_richcompare(PyObject *v, PyObject *w, int op)
PyObject *
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
{
- PyObject *res;
+ PyObject *res;
- assert(Py_LT <= op && op <= Py_GE);
- if (v == NULL || w == NULL) {
- if (!PyErr_Occurred())
- PyErr_BadInternalCall();
- return NULL;
- }
- if (Py_EnterRecursiveCall(" in comparison"))
- return NULL;
- res = do_richcompare(v, w, op);
- Py_LeaveRecursiveCall();
- return res;
+ assert(Py_LT <= op && op <= Py_GE);
+ if (v == NULL || w == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (Py_EnterRecursiveCall(" in comparison"))
+ return NULL;
+ res = do_richcompare(v, w, op);
+ Py_LeaveRecursiveCall();
+ return res;
}
/* Perform a rich comparison with integer result. This wraps
@@ -613,31 +613,31 @@ PyObject_RichCompare(PyObject *v, PyObject *w, int op)
int
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
{
- PyObject *res;
- int ok;
-
- /* Quick result when objects are the same.
- Guarantees that identity implies equality. */
- if (v == w) {
- if (op == Py_EQ)
- return 1;
- else if (op == Py_NE)
- return 0;
- }
-
- res = PyObject_RichCompare(v, w, op);
- if (res == NULL)
- return -1;
- if (PyBool_Check(res))
- ok = (res == Py_True);
- else
- ok = PyObject_IsTrue(res);
- Py_DECREF(res);
- return ok;
+ PyObject *res;
+ int ok;
+
+ /* Quick result when objects are the same.
+ Guarantees that identity implies equality. */
+ if (v == w) {
+ if (op == Py_EQ)
+ return 1;
+ else if (op == Py_NE)
+ return 0;
+ }
+
+ res = PyObject_RichCompare(v, w, op);
+ if (res == NULL)
+ return -1;
+ if (PyBool_Check(res))
+ ok = (res == Py_True);
+ else
+ ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ return ok;
}
/* Set of hash utility functions to help maintaining the invariant that
- if a==b then hash(a)==hash(b)
+ if a==b then hash(a)==hash(b)
All the utility functions (_Py_Hash*()) return "-1" to signify an error.
*/
@@ -645,227 +645,227 @@ PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
long
_Py_HashDouble(double v)
{
- double intpart, fractpart;
- int expo;
- long hipart;
- long x; /* the final hash value */
- /* This is designed so that Python numbers of different types
- * that compare equal hash to the same value; otherwise comparisons
- * of mapping keys will turn out weird.
- */
-
- fractpart = modf(v, &intpart);
- if (fractpart == 0.0) {
- /* This must return the same hash as an equal int or long. */
- if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) {
- /* Convert to long and use its hash. */
- PyObject *plong; /* converted to Python long */
- if (Py_IS_INFINITY(intpart))
- /* can't convert to long int -- arbitrary */
- v = v < 0 ? -271828.0 : 314159.0;
- plong = PyLong_FromDouble(v);
- if (plong == NULL)
- return -1;
- x = PyObject_Hash(plong);
- Py_DECREF(plong);
- return x;
- }
- /* Fits in a C long == a Python int, so is its own hash. */
- x = (long)intpart;
- if (x == -1)
- x = -2;
- return x;
- }
- /* The fractional part is non-zero, so we don't have to worry about
- * making this match the hash of some other type.
- * Use frexp to get at the bits in the double.
- * Since the VAX D double format has 56 mantissa bits, which is the
- * most of any double format in use, each of these parts may have as
- * many as (but no more than) 56 significant bits.
- * So, assuming sizeof(long) >= 4, each part can be broken into two
- * longs; frexp and multiplication are used to do that.
- * Also, since the Cray double format has 15 exponent bits, which is
- * the most of any double format in use, shifting the exponent field
- * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
- */
- v = frexp(v, &expo);
- v *= 2147483648.0; /* 2**31 */
- hipart = (long)v; /* take the top 32 bits */
- v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
- x = hipart + (long)v + (expo << 15);
- if (x == -1)
- x = -2;
- return x;
+ double intpart, fractpart;
+ int expo;
+ long hipart;
+ long x; /* the final hash value */
+ /* This is designed so that Python numbers of different types
+ * that compare equal hash to the same value; otherwise comparisons
+ * of mapping keys will turn out weird.
+ */
+
+ fractpart = modf(v, &intpart);
+ if (fractpart == 0.0) {
+ /* This must return the same hash as an equal int or long. */
+ if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) {
+ /* Convert to long and use its hash. */
+ PyObject *plong; /* converted to Python long */
+ if (Py_IS_INFINITY(intpart))
+ /* can't convert to long int -- arbitrary */
+ v = v < 0 ? -271828.0 : 314159.0;
+ plong = PyLong_FromDouble(v);
+ if (plong == NULL)
+ return -1;
+ x = PyObject_Hash(plong);
+ Py_DECREF(plong);
+ return x;
+ }
+ /* Fits in a C long == a Python int, so is its own hash. */
+ x = (long)intpart;
+ if (x == -1)
+ x = -2;
+ return x;
+ }
+ /* The fractional part is non-zero, so we don't have to worry about
+ * making this match the hash of some other type.
+ * Use frexp to get at the bits in the double.
+ * Since the VAX D double format has 56 mantissa bits, which is the
+ * most of any double format in use, each of these parts may have as
+ * many as (but no more than) 56 significant bits.
+ * So, assuming sizeof(long) >= 4, each part can be broken into two
+ * longs; frexp and multiplication are used to do that.
+ * Also, since the Cray double format has 15 exponent bits, which is
+ * the most of any double format in use, shifting the exponent field
+ * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
+ */
+ v = frexp(v, &expo);
+ v *= 2147483648.0; /* 2**31 */
+ hipart = (long)v; /* take the top 32 bits */
+ v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
+ x = hipart + (long)v + (expo << 15);
+ if (x == -1)
+ x = -2;
+ return x;
}
long
_Py_HashPointer(void *p)
{
- long x;
- size_t y = (size_t)p;
- /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
- excessive hash collisions for dicts and sets */
- y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
- x = (long)y;
- if (x == -1)
- x = -2;
- return x;
+ long x;
+ size_t y = (size_t)p;
+ /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
+ excessive hash collisions for dicts and sets */
+ y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
+ x = (long)y;
+ if (x == -1)
+ x = -2;
+ return x;
}
long
PyObject_HashNotImplemented(PyObject *v)
{
- PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
- Py_TYPE(v)->tp_name);
- return -1;
+ PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
+ Py_TYPE(v)->tp_name);
+ return -1;
}
long
PyObject_Hash(PyObject *v)
{
- PyTypeObject *tp = Py_TYPE(v);
- if (tp->tp_hash != NULL)
- return (*tp->tp_hash)(v);
- /* To keep to the general practice that inheriting
- * solely from object in C code should work without
- * an explicit call to PyType_Ready, we implicitly call
- * PyType_Ready here and then check the tp_hash slot again
- */
- if (tp->tp_dict == NULL) {
- if (PyType_Ready(tp) < 0)
- return -1;
- if (tp->tp_hash != NULL)
- return (*tp->tp_hash)(v);
- }
- /* Otherwise, the object can't be hashed */
- return PyObject_HashNotImplemented(v);
+ PyTypeObject *tp = Py_TYPE(v);
+ if (tp->tp_hash != NULL)
+ return (*tp->tp_hash)(v);
+ /* To keep to the general practice that inheriting
+ * solely from object in C code should work without
+ * an explicit call to PyType_Ready, we implicitly call
+ * PyType_Ready here and then check the tp_hash slot again
+ */
+ if (tp->tp_dict == NULL) {
+ if (PyType_Ready(tp) < 0)
+ return -1;
+ if (tp->tp_hash != NULL)
+ return (*tp->tp_hash)(v);
+ }
+ /* Otherwise, the object can't be hashed */
+ return PyObject_HashNotImplemented(v);
}
PyObject *
PyObject_GetAttrString(PyObject *v, const char *name)
{
- PyObject *w, *res;
+ PyObject *w, *res;
- if (Py_TYPE(v)->tp_getattr != NULL)
- return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
- w = PyUnicode_InternFromString(name);
- if (w == NULL)
- return NULL;
- res = PyObject_GetAttr(v, w);
- Py_XDECREF(w);
- return res;
+ if (Py_TYPE(v)->tp_getattr != NULL)
+ return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
+ w = PyUnicode_InternFromString(name);
+ if (w == NULL)
+ return NULL;
+ res = PyObject_GetAttr(v, w);
+ Py_XDECREF(w);
+ return res;
}
int
PyObject_HasAttrString(PyObject *v, const char *name)
{
- PyObject *res = PyObject_GetAttrString(v, name);
- if (res != NULL) {
- Py_DECREF(res);
- return 1;
- }
- PyErr_Clear();
- return 0;
+ PyObject *res = PyObject_GetAttrString(v, name);
+ if (res != NULL) {
+ Py_DECREF(res);
+ return 1;
+ }
+ PyErr_Clear();
+ return 0;
}
int
PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
{
- PyObject *s;
- int res;
+ PyObject *s;
+ int res;
- if (Py_TYPE(v)->tp_setattr != NULL)
- return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
- s = PyUnicode_InternFromString(name);
- if (s == NULL)
- return -1;
- res = PyObject_SetAttr(v, s, w);
- Py_XDECREF(s);
- return res;
+ if (Py_TYPE(v)->tp_setattr != NULL)
+ return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
+ s = PyUnicode_InternFromString(name);
+ if (s == NULL)
+ return -1;
+ res = PyObject_SetAttr(v, s, w);
+ Py_XDECREF(s);
+ return res;
}
PyObject *
PyObject_GetAttr(PyObject *v, PyObject *name)
{
- PyTypeObject *tp = Py_TYPE(v);
-
- if (!PyUnicode_Check(name)) {
- PyErr_Format(PyExc_TypeError,
- "attribute name must be string, not '%.200s'",
- name->ob_type->tp_name);
- return NULL;
- }
- if (tp->tp_getattro != NULL)
- return (*tp->tp_getattro)(v, name);
- if (tp->tp_getattr != NULL) {
- char *name_str = _PyUnicode_AsString(name);
- if (name_str == NULL)
- return NULL;
- return (*tp->tp_getattr)(v, name_str);
- }
- PyErr_Format(PyExc_AttributeError,
- "'%.50s' object has no attribute '%U'",
- tp->tp_name, name);
- return NULL;
+ PyTypeObject *tp = Py_TYPE(v);
+
+ if (!PyUnicode_Check(name)) {
+ PyErr_Format(PyExc_TypeError,
+ "attribute name must be string, not '%.200s'",
+ name->ob_type->tp_name);
+ return NULL;
+ }
+ if (tp->tp_getattro != NULL)
+ return (*tp->tp_getattro)(v, name);
+ if (tp->tp_getattr != NULL) {
+ char *name_str = _PyUnicode_AsString(name);
+ if (name_str == NULL)
+ return NULL;
+ return (*tp->tp_getattr)(v, name_str);
+ }
+ PyErr_Format(PyExc_AttributeError,
+ "'%.50s' object has no attribute '%U'",
+ tp->tp_name, name);
+ return NULL;
}
int
PyObject_HasAttr(PyObject *v, PyObject *name)
{
- PyObject *res = PyObject_GetAttr(v, name);
- if (res != NULL) {
- Py_DECREF(res);
- return 1;
- }
- PyErr_Clear();
- return 0;
+ PyObject *res = PyObject_GetAttr(v, name);
+ if (res != NULL) {
+ Py_DECREF(res);
+ return 1;
+ }
+ PyErr_Clear();
+ return 0;
}
int
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
{
- PyTypeObject *tp = Py_TYPE(v);
- int err;
-
- if (!PyUnicode_Check(name)) {
- PyErr_Format(PyExc_TypeError,
- "attribute name must be string, not '%.200s'",
- name->ob_type->tp_name);
- return -1;
- }
- Py_INCREF(name);
-
- PyUnicode_InternInPlace(&name);
- if (tp->tp_setattro != NULL) {
- err = (*tp->tp_setattro)(v, name, value);
- Py_DECREF(name);
- return err;
- }
- if (tp->tp_setattr != NULL) {
- char *name_str = _PyUnicode_AsString(name);
- if (name_str == NULL)
- return -1;
- err = (*tp->tp_setattr)(v, name_str, value);
- Py_DECREF(name);
- return err;
- }
- Py_DECREF(name);
- assert(name->ob_refcnt >= 1);
- if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
- PyErr_Format(PyExc_TypeError,
- "'%.100s' object has no attributes "
- "(%s .%U)",
- tp->tp_name,
- value==NULL ? "del" : "assign to",
- name);
- else
- PyErr_Format(PyExc_TypeError,
- "'%.100s' object has only read-only attributes "
- "(%s .%U)",
- tp->tp_name,
- value==NULL ? "del" : "assign to",
- name);
- return -1;
+ PyTypeObject *tp = Py_TYPE(v);
+ int err;
+
+ if (!PyUnicode_Check(name)) {
+ PyErr_Format(PyExc_TypeError,
+ "attribute name must be string, not '%.200s'",
+ name->ob_type->tp_name);
+ return -1;
+ }
+ Py_INCREF(name);
+
+ PyUnicode_InternInPlace(&name);
+ if (tp->tp_setattro != NULL) {
+ err = (*tp->tp_setattro)(v, name, value);
+ Py_DECREF(name);
+ return err;
+ }
+ if (tp->tp_setattr != NULL) {
+ char *name_str = _PyUnicode_AsString(name);
+ if (name_str == NULL)
+ return -1;
+ err = (*tp->tp_setattr)(v, name_str, value);
+ Py_DECREF(name);
+ return err;
+ }
+ Py_DECREF(name);
+ assert(name->ob_refcnt >= 1);
+ if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
+ PyErr_Format(PyExc_TypeError,
+ "'%.100s' object has no attributes "
+ "(%s .%U)",
+ tp->tp_name,
+ value==NULL ? "del" : "assign to",
+ name);
+ else
+ PyErr_Format(PyExc_TypeError,
+ "'%.100s' object has only read-only attributes "
+ "(%s .%U)",
+ tp->tp_name,
+ value==NULL ? "del" : "assign to",
+ name);
+ return -1;
}
/* Helper to get a pointer to an object's __dict__ slot, if any */
@@ -873,33 +873,33 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
PyObject **
_PyObject_GetDictPtr(PyObject *obj)
{
- Py_ssize_t dictoffset;
- PyTypeObject *tp = Py_TYPE(obj);
+ Py_ssize_t dictoffset;
+ PyTypeObject *tp = Py_TYPE(obj);
- dictoffset = tp->tp_dictoffset;
- if (dictoffset == 0)
- return NULL;
- if (dictoffset < 0) {
- Py_ssize_t tsize;
- size_t size;
+ dictoffset = tp->tp_dictoffset;
+ if (dictoffset == 0)
+ return NULL;
+ if (dictoffset < 0) {
+ Py_ssize_t tsize;
+ size_t size;
- tsize = ((PyVarObject *)obj)->ob_size;
- if (tsize < 0)
- tsize = -tsize;
- size = _PyObject_VAR_SIZE(tp, tsize);
+ tsize = ((PyVarObject *)obj)->ob_size;
+ if (tsize < 0)
+ tsize = -tsize;
+ size = _PyObject_VAR_SIZE(tp, tsize);
- dictoffset += (long)size;
- assert(dictoffset > 0);
- assert(dictoffset % SIZEOF_VOID_P == 0);
- }
- return (PyObject **) ((char *)obj + dictoffset);
+ dictoffset += (long)size;
+ assert(dictoffset > 0);
+ assert(dictoffset % SIZEOF_VOID_P == 0);
+ }
+ return (PyObject **) ((char *)obj + dictoffset);
}
PyObject *
PyObject_SelfIter(PyObject *obj)
{
- Py_INCREF(obj);
- return obj;
+ Py_INCREF(obj);
+ return obj;
}
/* Helper used when the __next__ method is removed from a type:
@@ -910,10 +910,10 @@ PyObject_SelfIter(PyObject *obj)
PyObject *
_PyObject_NextNotImplemented(PyObject *self)
{
- PyErr_Format(PyExc_TypeError,
- "'%.200s' object is not iterable",
- Py_TYPE(self)->tp_name);
- return NULL;
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not iterable",
+ Py_TYPE(self)->tp_name);
+ return NULL;
}
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
@@ -921,189 +921,189 @@ _PyObject_NextNotImplemented(PyObject *self)
PyObject *
PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
{
- PyTypeObject *tp = Py_TYPE(obj);
- PyObject *descr = NULL;
- PyObject *res = NULL;
- descrgetfunc f;
- Py_ssize_t dictoffset;
- PyObject **dictptr;
-
- if (!PyUnicode_Check(name)){
- PyErr_Format(PyExc_TypeError,
- "attribute name must be string, not '%.200s'",
- name->ob_type->tp_name);
- return NULL;
- }
- else
- Py_INCREF(name);
-
- if (tp->tp_dict == NULL) {
- if (PyType_Ready(tp) < 0)
- goto done;
- }
+ PyTypeObject *tp = Py_TYPE(obj);
+ PyObject *descr = NULL;
+ PyObject *res = NULL;
+ descrgetfunc f;
+ Py_ssize_t dictoffset;
+ PyObject **dictptr;
+
+ if (!PyUnicode_Check(name)){
+ PyErr_Format(PyExc_TypeError,
+ "attribute name must be string, not '%.200s'",
+ name->ob_type->tp_name);
+ return NULL;
+ }
+ else
+ Py_INCREF(name);
+
+ if (tp->tp_dict == NULL) {
+ if (PyType_Ready(tp) < 0)
+ goto done;
+ }
#if 0 /* XXX this is not quite _PyType_Lookup anymore */
- /* Inline _PyType_Lookup */
- {
- Py_ssize_t i, n;
- PyObject *mro, *base, *dict;
-
- /* Look in tp_dict of types in MRO */
- mro = tp->tp_mro;
- assert(mro != NULL);
- assert(PyTuple_Check(mro));
- n = PyTuple_GET_SIZE(mro);
- for (i = 0; i < n; i++) {
- base = PyTuple_GET_ITEM(mro, i);
- assert(PyType_Check(base));
- dict = ((PyTypeObject *)base)->tp_dict;
- assert(dict && PyDict_Check(dict));
- descr = PyDict_GetItem(dict, name);
- if (descr != NULL)
- break;
- }
- }
+ /* Inline _PyType_Lookup */
+ {
+ Py_ssize_t i, n;
+ PyObject *mro, *base, *dict;
+
+ /* Look in tp_dict of types in MRO */
+ mro = tp->tp_mro;
+ assert(mro != NULL);
+ assert(PyTuple_Check(mro));
+ n = PyTuple_GET_SIZE(mro);
+ for (i = 0; i < n; i++) {
+ base = PyTuple_GET_ITEM(mro, i);
+ assert(PyType_Check(base));
+ dict = ((PyTypeObject *)base)->tp_dict;
+ assert(dict && PyDict_Check(dict));
+ descr = PyDict_GetItem(dict, name);
+ if (descr != NULL)
+ break;
+ }
+ }
#else
- descr = _PyType_Lookup(tp, name);
+ descr = _PyType_Lookup(tp, name);
#endif
- Py_XINCREF(descr);
-
- f = NULL;
- if (descr != NULL) {
- f = descr->ob_type->tp_descr_get;
- if (f != NULL && PyDescr_IsData(descr)) {
- res = f(descr, obj, (PyObject *)obj->ob_type);
- Py_DECREF(descr);
- goto done;
- }
- }
-
- /* Inline _PyObject_GetDictPtr */
- dictoffset = tp->tp_dictoffset;
- if (dictoffset != 0) {
- PyObject *dict;
- if (dictoffset < 0) {
- Py_ssize_t tsize;
- size_t size;
-
- tsize = ((PyVarObject *)obj)->ob_size;
- if (tsize < 0)
- tsize = -tsize;
- size = _PyObject_VAR_SIZE(tp, tsize);
-
- dictoffset += (long)size;
- assert(dictoffset > 0);
- assert(dictoffset % SIZEOF_VOID_P == 0);
- }
- dictptr = (PyObject **) ((char *)obj + dictoffset);
- dict = *dictptr;
- if (dict != NULL) {
- Py_INCREF(dict);
- res = PyDict_GetItem(dict, name);
- if (res != NULL) {
- Py_INCREF(res);
- Py_XDECREF(descr);
- Py_DECREF(dict);
- goto done;
- }
- Py_DECREF(dict);
- }
- }
-
- if (f != NULL) {
- res = f(descr, obj, (PyObject *)Py_TYPE(obj));
- Py_DECREF(descr);
- goto done;
- }
-
- if (descr != NULL) {
- res = descr;
- /* descr was already increfed above */
- goto done;
- }
-
- PyErr_Format(PyExc_AttributeError,
- "'%.50s' object has no attribute '%U'",
- tp->tp_name, name);
+ Py_XINCREF(descr);
+
+ f = NULL;
+ if (descr != NULL) {
+ f = descr->ob_type->tp_descr_get;
+ if (f != NULL && PyDescr_IsData(descr)) {
+ res = f(descr, obj, (PyObject *)obj->ob_type);
+ Py_DECREF(descr);
+ goto done;
+ }
+ }
+
+ /* Inline _PyObject_GetDictPtr */
+ dictoffset = tp->tp_dictoffset;
+ if (dictoffset != 0) {
+ PyObject *dict;
+ if (dictoffset < 0) {
+ Py_ssize_t tsize;
+ size_t size;
+
+ tsize = ((PyVarObject *)obj)->ob_size;
+ if (tsize < 0)
+ tsize = -tsize;
+ size = _PyObject_VAR_SIZE(tp, tsize);
+
+ dictoffset += (long)size;
+ assert(dictoffset > 0);
+ assert(dictoffset % SIZEOF_VOID_P == 0);
+ }
+ dictptr = (PyObject **) ((char *)obj + dictoffset);
+ dict = *dictptr;
+ if (dict != NULL) {
+ Py_INCREF(dict);
+ res = PyDict_GetItem(dict, name);
+ if (res != NULL) {
+ Py_INCREF(res);
+ Py_XDECREF(descr);
+ Py_DECREF(dict);
+ goto done;
+ }
+ Py_DECREF(dict);
+ }
+ }
+
+ if (f != NULL) {
+ res = f(descr, obj, (PyObject *)Py_TYPE(obj));
+ Py_DECREF(descr);
+ goto done;
+ }
+
+ if (descr != NULL) {
+ res = descr;
+ /* descr was already increfed above */
+ goto done;
+ }
+
+ PyErr_Format(PyExc_AttributeError,
+ "'%.50s' object has no attribute '%U'",
+ tp->tp_name, name);
done:
- Py_DECREF(name);
- return res;
+ Py_DECREF(name);
+ return res;
}
int
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
{
- PyTypeObject *tp = Py_TYPE(obj);
- PyObject *descr;
- descrsetfunc f;
- PyObject **dictptr;
- int res = -1;
-
- if (!PyUnicode_Check(name)){
- PyErr_Format(PyExc_TypeError,
- "attribute name must be string, not '%.200s'",
- name->ob_type->tp_name);
- return -1;
- }
- else
- Py_INCREF(name);
-
- if (tp->tp_dict == NULL) {
- if (PyType_Ready(tp) < 0)
- goto done;
- }
-
- descr = _PyType_Lookup(tp, name);
- f = NULL;
- if (descr != NULL) {
- f = descr->ob_type->tp_descr_set;
- if (f != NULL && PyDescr_IsData(descr)) {
- res = f(descr, obj, value);
- goto done;
- }
- }
-
- dictptr = _PyObject_GetDictPtr(obj);
- if (dictptr != NULL) {
- PyObject *dict = *dictptr;
- if (dict == NULL && value != NULL) {
- dict = PyDict_New();
- if (dict == NULL)
- goto done;
- *dictptr = dict;
- }
- if (dict != NULL) {
- Py_INCREF(dict);
- if (value == NULL)
- res = PyDict_DelItem(dict, name);
- else
- res = PyDict_SetItem(dict, name, value);
- if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
- PyErr_SetObject(PyExc_AttributeError, name);
- Py_DECREF(dict);
- goto done;
- }
- }
-
- if (f != NULL) {
- res = f(descr, obj, value);
- goto done;
- }
-
- if (descr == NULL) {
- PyErr_Format(PyExc_AttributeError,
- "'%.100s' object has no attribute '%U'",
- tp->tp_name, name);
- goto done;
- }
-
- PyErr_Format(PyExc_AttributeError,
- "'%.50s' object attribute '%U' is read-only",
- tp->tp_name, name);
+ PyTypeObject *tp = Py_TYPE(obj);
+ PyObject *descr;
+ descrsetfunc f;
+ PyObject **dictptr;
+ int res = -1;
+
+ if (!PyUnicode_Check(name)){
+ PyErr_Format(PyExc_TypeError,
+ "attribute name must be string, not '%.200s'",
+ name->ob_type->tp_name);
+ return -1;
+ }
+ else
+ Py_INCREF(name);
+
+ if (tp->tp_dict == NULL) {
+ if (PyType_Ready(tp) < 0)
+ goto done;
+ }
+
+ descr = _PyType_Lookup(tp, name);
+ f = NULL;
+ if (descr != NULL) {
+ f = descr->ob_type->tp_descr_set;
+ if (f != NULL && PyDescr_IsData(descr)) {
+ res = f(descr, obj, value);
+ goto done;
+ }
+ }
+
+ dictptr = _PyObject_GetDictPtr(obj);
+ if (dictptr != NULL) {
+ PyObject *dict = *dictptr;
+ if (dict == NULL && value != NULL) {
+ dict = PyDict_New();
+ if (dict == NULL)
+ goto done;
+ *dictptr = dict;
+ }
+ if (dict != NULL) {
+ Py_INCREF(dict);
+ if (value == NULL)
+ res = PyDict_DelItem(dict, name);
+ else
+ res = PyDict_SetItem(dict, name, value);
+ if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+ PyErr_SetObject(PyExc_AttributeError, name);
+ Py_DECREF(dict);
+ goto done;
+ }
+ }
+
+ if (f != NULL) {
+ res = f(descr, obj, value);
+ goto done;
+ }
+
+ if (descr == NULL) {
+ PyErr_Format(PyExc_AttributeError,
+ "'%.100s' object has no attribute '%U'",
+ tp->tp_name, name);
+ goto done;
+ }
+
+ PyErr_Format(PyExc_AttributeError,
+ "'%.50s' object attribute '%U' is read-only",
+ tp->tp_name, name);
done:
- Py_DECREF(name);
- return res;
+ Py_DECREF(name);
+ return res;
}
/* Test a value used as condition, e.g., in a for or if statement.
@@ -1112,26 +1112,26 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
int
PyObject_IsTrue(PyObject *v)
{
- Py_ssize_t res;
- if (v == Py_True)
- return 1;
- if (v == Py_False)
- return 0;
- if (v == Py_None)
- return 0;
- else if (v->ob_type->tp_as_number != NULL &&
- v->ob_type->tp_as_number->nb_bool != NULL)
- res = (*v->ob_type->tp_as_number->nb_bool)(v);
- else if (v->ob_type->tp_as_mapping != NULL &&
- v->ob_type->tp_as_mapping->mp_length != NULL)
- res = (*v->ob_type->tp_as_mapping->mp_length)(v);
- else if (v->ob_type->tp_as_sequence != NULL &&
- v->ob_type->tp_as_sequence->sq_length != NULL)
- res = (*v->ob_type->tp_as_sequence->sq_length)(v);
- else
- return 1;
- /* if it is negative, it should be either -1 or -2 */
- return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
+ Py_ssize_t res;
+ if (v == Py_True)
+ return 1;
+ if (v == Py_False)
+ return 0;
+ if (v == Py_None)
+ return 0;
+ else if (v->ob_type->tp_as_number != NULL &&
+ v->ob_type->tp_as_number->nb_bool != NULL)
+ res = (*v->ob_type->tp_as_number->nb_bool)(v);
+ else if (v->ob_type->tp_as_mapping != NULL &&
+ v->ob_type->tp_as_mapping->mp_length != NULL)
+ res = (*v->ob_type->tp_as_mapping->mp_length)(v);
+ else if (v->ob_type->tp_as_sequence != NULL &&
+ v->ob_type->tp_as_sequence->sq_length != NULL)
+ res = (*v->ob_type->tp_as_sequence->sq_length)(v);
+ else
+ return 1;
+ /* if it is negative, it should be either -1 or -2 */
+ return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
}
/* equivalent of 'not v'
@@ -1140,11 +1140,11 @@ PyObject_IsTrue(PyObject *v)
int
PyObject_Not(PyObject *v)
{
- int res;
- res = PyObject_IsTrue(v);
- if (res < 0)
- return res;
- return res == 0;
+ int res;
+ res = PyObject_IsTrue(v);
+ if (res < 0)
+ return res;
+ return res == 0;
}
/* Test whether an object can be called */
@@ -1152,9 +1152,9 @@ PyObject_Not(PyObject *v)
int
PyCallable_Check(PyObject *x)
{
- if (x == NULL)
- return 0;
- return x->ob_type->tp_call != NULL;
+ if (x == NULL)
+ return 0;
+ return x->ob_type->tp_call != NULL;
}
/* ------------------------- PyObject_Dir() helpers ------------------------- */
@@ -1170,78 +1170,78 @@ PyCallable_Check(PyObject *x)
static int
merge_class_dict(PyObject* dict, PyObject* aclass)
{
- PyObject *classdict;
- PyObject *bases;
-
- assert(PyDict_Check(dict));
- assert(aclass);
-
- /* Merge in the type's dict (if any). */
- classdict = PyObject_GetAttrString(aclass, "__dict__");
- if (classdict == NULL)
- PyErr_Clear();
- else {
- int status = PyDict_Update(dict, classdict);
- Py_DECREF(classdict);
- if (status < 0)
- return -1;
- }
-
- /* Recursively merge in the base types' (if any) dicts. */
- bases = PyObject_GetAttrString(aclass, "__bases__");
- if (bases == NULL)
- PyErr_Clear();
- else {
- /* We have no guarantee that bases is a real tuple */
- Py_ssize_t i, n;
- n = PySequence_Size(bases); /* This better be right */
- if (n < 0)
- PyErr_Clear();
- else {
- for (i = 0; i < n; i++) {
- int status;
- PyObject *base = PySequence_GetItem(bases, i);
- if (base == NULL) {
- Py_DECREF(bases);
- return -1;
- }
- status = merge_class_dict(dict, base);
- Py_DECREF(base);
- if (status < 0) {
- Py_DECREF(bases);
- return -1;
- }
- }
- }
- Py_DECREF(bases);
- }
- return 0;
+ PyObject *classdict;
+ PyObject *bases;
+
+ assert(PyDict_Check(dict));
+ assert(aclass);
+
+ /* Merge in the type's dict (if any). */
+ classdict = PyObject_GetAttrString(aclass, "__dict__");
+ if (classdict == NULL)
+ PyErr_Clear();
+ else {
+ int status = PyDict_Update(dict, classdict);
+ Py_DECREF(classdict);
+ if (status < 0)
+ return -1;
+ }
+
+ /* Recursively merge in the base types' (if any) dicts. */
+ bases = PyObject_GetAttrString(aclass, "__bases__");
+ if (bases == NULL)
+ PyErr_Clear();
+ else {
+ /* We have no guarantee that bases is a real tuple */
+ Py_ssize_t i, n;
+ n = PySequence_Size(bases); /* This better be right */
+ if (n < 0)
+ PyErr_Clear();
+ else {
+ for (i = 0; i < n; i++) {
+ int status;
+ PyObject *base = PySequence_GetItem(bases, i);
+ if (base == NULL) {
+ Py_DECREF(bases);
+ return -1;
+ }
+ status = merge_class_dict(dict, base);
+ Py_DECREF(base);
+ if (status < 0) {
+ Py_DECREF(bases);
+ return -1;
+ }
+ }
+ }
+ Py_DECREF(bases);
+ }
+ return 0;
}
/* Helper for PyObject_Dir without arguments: returns the local scope. */
static PyObject *
_dir_locals(void)
{
- PyObject *names;
- PyObject *locals = PyEval_GetLocals();
+ PyObject *names;
+ PyObject *locals = PyEval_GetLocals();
- if (locals == NULL) {
- PyErr_SetString(PyExc_SystemError, "frame does not exist");
- return NULL;
- }
+ if (locals == NULL) {
+ PyErr_SetString(PyExc_SystemError, "frame does not exist");
+ return NULL;
+ }
- names = PyMapping_Keys(locals);
- if (!names)
- return NULL;
- if (!PyList_Check(names)) {
- PyErr_Format(PyExc_TypeError,
- "dir(): expected keys() of locals to be a list, "
- "not '%.200s'", Py_TYPE(names)->tp_name);
- Py_DECREF(names);
- return NULL;
- }
- /* the locals don't need to be DECREF'd */
- return names;
+ names = PyMapping_Keys(locals);
+ if (!names)
+ return NULL;
+ if (!PyList_Check(names)) {
+ PyErr_Format(PyExc_TypeError,
+ "dir(): expected keys() of locals to be a list, "
+ "not '%.200s'", Py_TYPE(names)->tp_name);
+ Py_DECREF(names);
+ return NULL;
+ }
+ /* the locals don't need to be DECREF'd */
+ return names;
}
/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
@@ -1251,37 +1251,37 @@ _dir_locals(void)
static PyObject *
_specialized_dir_type(PyObject *obj)
{
- PyObject *result = NULL;
- PyObject *dict = PyDict_New();
+ PyObject *result = NULL;
+ PyObject *dict = PyDict_New();
- if (dict != NULL && merge_class_dict(dict, obj) == 0)
- result = PyDict_Keys(dict);
+ if (dict != NULL && merge_class_dict(dict, obj) == 0)
+ result = PyDict_Keys(dict);
- Py_XDECREF(dict);
- return result;
+ Py_XDECREF(dict);
+ return result;
}
/* Helper for PyObject_Dir of module objects: returns the module's __dict__. */
static PyObject *
_specialized_dir_module(PyObject *obj)
{
- PyObject *result = NULL;
- PyObject *dict = PyObject_GetAttrString(obj, "__dict__");
-
- if (dict != NULL) {
- if (PyDict_Check(dict))
- result = PyDict_Keys(dict);
- else {
- const char *name = PyModule_GetName(obj);
- if (name)
- PyErr_Format(PyExc_TypeError,
- "%.200s.__dict__ is not a dictionary",
- name);
- }
- }
+ PyObject *result = NULL;
+ PyObject *dict = PyObject_GetAttrString(obj, "__dict__");
+
+ if (dict != NULL) {
+ if (PyDict_Check(dict))
+ result = PyDict_Keys(dict);
+ else {
+ const char *name = PyModule_GetName(obj);
+ if (name)
+ PyErr_Format(PyExc_TypeError,
+ "%.200s.__dict__ is not a dictionary",
+ name);
+ }
+ }
- Py_XDECREF(dict);
- return result;
+ Py_XDECREF(dict);
+ return result;
}
/* Helper for PyObject_Dir of generic objects: returns __dict__, __class__,
@@ -1290,47 +1290,47 @@ _specialized_dir_module(PyObject *obj)
static PyObject *
_generic_dir(PyObject *obj)
{
- PyObject *result = NULL;
- PyObject *dict = NULL;
- PyObject *itsclass = NULL;
-
- /* Get __dict__ (which may or may not be a real dict...) */
- dict = PyObject_GetAttrString(obj, "__dict__");
- if (dict == NULL) {
- PyErr_Clear();
- dict = PyDict_New();
- }
- else if (!PyDict_Check(dict)) {
- Py_DECREF(dict);
- dict = PyDict_New();
- }
- else {
- /* Copy __dict__ to avoid mutating it. */
- PyObject *temp = PyDict_Copy(dict);
- Py_DECREF(dict);
- dict = temp;
- }
-
- if (dict == NULL)
- goto error;
-
- /* Merge in attrs reachable from its class. */
- itsclass = PyObject_GetAttrString(obj, "__class__");
- if (itsclass == NULL)
- /* XXX(tomer): Perhaps fall back to obj->ob_type if no
- __class__ exists? */
- PyErr_Clear();
- else {
- if (merge_class_dict(dict, itsclass) != 0)
- goto error;
- }
-
- result = PyDict_Keys(dict);
- /* fall through */
+ PyObject *result = NULL;
+ PyObject *dict = NULL;
+ PyObject *itsclass = NULL;
+
+ /* Get __dict__ (which may or may not be a real dict...) */
+ dict = PyObject_GetAttrString(obj, "__dict__");
+ if (dict == NULL) {
+ PyErr_Clear();
+ dict = PyDict_New();
+ }
+ else if (!PyDict_Check(dict)) {
+ Py_DECREF(dict);
+ dict = PyDict_New();
+ }
+ else {
+ /* Copy __dict__ to avoid mutating it. */
+ PyObject *temp = PyDict_Copy(dict);
+ Py_DECREF(dict);
+ dict = temp;
+ }
+
+ if (dict == NULL)
+ goto error;
+
+ /* Merge in attrs reachable from its class. */
+ itsclass = PyObject_GetAttrString(obj, "__class__");
+ if (itsclass == NULL)
+ /* XXX(tomer): Perhaps fall back to obj->ob_type if no
+ __class__ exists? */
+ PyErr_Clear();
+ else {
+ if (merge_class_dict(dict, itsclass) != 0)
+ goto error;
+ }
+
+ result = PyDict_Keys(dict);
+ /* fall through */
error:
- Py_XDECREF(itsclass);
- Py_XDECREF(dict);
- return result;
+ Py_XDECREF(itsclass);
+ Py_XDECREF(dict);
+ return result;
}
/* Helper for PyObject_Dir: object introspection.
@@ -1339,40 +1339,40 @@ error:
static PyObject *
_dir_object(PyObject *obj)
{
- PyObject * result = NULL;
- PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
- "__dir__");
-
- assert(obj);
- if (dirfunc == NULL) {
- /* use default implementation */
- PyErr_Clear();
- if (PyModule_Check(obj))
- result = _specialized_dir_module(obj);
- else if (PyType_Check(obj))
- result = _specialized_dir_type(obj);
- else
- result = _generic_dir(obj);
- }
- else {
- /* use __dir__ */
- result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
- Py_DECREF(dirfunc);
- if (result == NULL)
- return NULL;
-
- /* result must be a list */
- /* XXX(gbrandl): could also check if all items are strings */
- if (!PyList_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "__dir__() must return a list, not %.200s",
- Py_TYPE(result)->tp_name);
- Py_DECREF(result);
- result = NULL;
- }
- }
-
- return result;
+ PyObject * result = NULL;
+ PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
+ "__dir__");
+
+ assert(obj);
+ if (dirfunc == NULL) {
+ /* use default implementation */
+ PyErr_Clear();
+ if (PyModule_Check(obj))
+ result = _specialized_dir_module(obj);
+ else if (PyType_Check(obj))
+ result = _specialized_dir_type(obj);
+ else
+ result = _generic_dir(obj);
+ }
+ else {
+ /* use __dir__ */
+ result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
+ Py_DECREF(dirfunc);
+ if (result == NULL)
+ return NULL;
+
+ /* result must be a list */
+ /* XXX(gbrandl): could also check if all items are strings */
+ if (!PyList_Check(result)) {
+ PyErr_Format(PyExc_TypeError,
+ "__dir__() must return a list, not %.200s",
+ Py_TYPE(result)->tp_name);
+ Py_DECREF(result);
+ result = NULL;
+ }
+ }
+
+ return result;
}
/* Implementation of dir() -- if obj is NULL, returns the names in the current
@@ -1382,24 +1382,24 @@ _dir_object(PyObject *obj)
PyObject *
PyObject_Dir(PyObject *obj)
{
- PyObject * result;
+ PyObject * result;
- if (obj == NULL)
- /* no object -- introspect the locals */
- result = _dir_locals();
- else
- /* object -- introspect the object */
- result = _dir_object(obj);
+ if (obj == NULL)
+ /* no object -- introspect the locals */
+ result = _dir_locals();
+ else
+ /* object -- introspect the object */
+ result = _dir_object(obj);
- assert(result == NULL || PyList_Check(result));
+ assert(result == NULL || PyList_Check(result));
- if (result != NULL && PyList_Sort(result) != 0) {
- /* sorting the list failed */
- Py_DECREF(result);
- result = NULL;
- }
+ if (result != NULL && PyList_Sort(result) != 0) {
+ /* sorting the list failed */
+ Py_DECREF(result);
+ result = NULL;
+ }
- return result;
+ return result;
}
/*
@@ -1413,35 +1413,35 @@ so there is exactly one (which is indestructible, by the way).
static PyObject *
none_repr(PyObject *op)
{
- return PyUnicode_FromString("None");
+ return PyUnicode_FromString("None");
}
/* ARGUSED */
static void
none_dealloc(PyObject* ignore)
{
- /* This should never get called, but we also don't want to SEGV if
- * we accidentally decref None out of existence.
- */
- Py_FatalError("deallocating None");
+ /* This should never get called, but we also don't want to SEGV if
+ * we accidentally decref None out of existence.
+ */
+ Py_FatalError("deallocating None");
}
static PyTypeObject PyNone_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "NoneType",
- 0,
- 0,
- none_dealloc, /*tp_dealloc*/ /*never called*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- none_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "NoneType",
+ 0,
+ 0,
+ none_dealloc, /*tp_dealloc*/ /*never called*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ none_repr, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
};
PyObject _Py_NoneStruct = {
@@ -1455,166 +1455,166 @@ PyObject _Py_NoneStruct = {
static PyObject *
NotImplemented_repr(PyObject *op)
{
- return PyUnicode_FromString("NotImplemented");
+ return PyUnicode_FromString("NotImplemented");
}
static PyTypeObject PyNotImplemented_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "NotImplementedType",
- 0,
- 0,
- none_dealloc, /*tp_dealloc*/ /*never called*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- NotImplemented_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "NotImplementedType",
+ 0,
+ 0,
+ none_dealloc, /*tp_dealloc*/ /*never called*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ NotImplemented_repr, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
};
PyObject _Py_NotImplementedStruct = {
- _PyObject_EXTRA_INIT
- 1, &PyNotImplemented_Type
+ _PyObject_EXTRA_INIT
+ 1, &PyNotImplemented_Type
};
void
_Py_ReadyTypes(void)
{
- if (PyType_Ready(&PyType_Type) < 0)
- Py_FatalError("Can't initialize type type");
+ if (PyType_Ready(&PyType_Type) < 0)
+ Py_FatalError("Can't initialize type type");
- if (PyType_Ready(&_PyWeakref_RefType) < 0)
- Py_FatalError("Can't initialize weakref type");
+ if (PyType_Ready(&_PyWeakref_RefType) < 0)
+ Py_FatalError("Can't initialize weakref type");
- if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
- Py_FatalError("Can't initialize callable weakref proxy type");
+ if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
+ Py_FatalError("Can't initialize callable weakref proxy type");
- if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
- Py_FatalError("Can't initialize weakref proxy type");
+ if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
+ Py_FatalError("Can't initialize weakref proxy type");
- if (PyType_Ready(&PyBool_Type) < 0)
- Py_FatalError("Can't initialize bool type");
+ if (PyType_Ready(&PyBool_Type) < 0)
+ Py_FatalError("Can't initialize bool type");
- if (PyType_Ready(&PyByteArray_Type) < 0)
- Py_FatalError("Can't initialize bytearray type");
+ if (PyType_Ready(&PyByteArray_Type) < 0)
+ Py_FatalError("Can't initialize bytearray type");
- if (PyType_Ready(&PyBytes_Type) < 0)
- Py_FatalError("Can't initialize 'str'");
+ if (PyType_Ready(&PyBytes_Type) < 0)
+ Py_FatalError("Can't initialize 'str'");
- if (PyType_Ready(&PyList_Type) < 0)
- Py_FatalError("Can't initialize list type");
+ if (PyType_Ready(&PyList_Type) < 0)
+ Py_FatalError("Can't initialize list type");
- if (PyType_Ready(&PyNone_Type) < 0)
- Py_FatalError("Can't initialize None type");
+ if (PyType_Ready(&PyNone_Type) < 0)
+ Py_FatalError("Can't initialize None type");
- if (PyType_Ready(Py_Ellipsis->ob_type) < 0)
- Py_FatalError("Can't initialize type(Ellipsis)");
+ if (PyType_Ready(Py_Ellipsis->ob_type) < 0)
+ Py_FatalError("Can't initialize type(Ellipsis)");
- if (PyType_Ready(&PyNotImplemented_Type) < 0)
- Py_FatalError("Can't initialize NotImplemented type");
+ if (PyType_Ready(&PyNotImplemented_Type) < 0)
+ Py_FatalError("Can't initialize NotImplemented type");
- if (PyType_Ready(&PyTraceBack_Type) < 0)
- Py_FatalError("Can't initialize traceback type");
+ if (PyType_Ready(&PyTraceBack_Type) < 0)
+ Py_FatalError("Can't initialize traceback type");
- if (PyType_Ready(&PySuper_Type) < 0)
- Py_FatalError("Can't initialize super type");
+ if (PyType_Ready(&PySuper_Type) < 0)
+ Py_FatalError("Can't initialize super type");
- if (PyType_Ready(&PyBaseObject_Type) < 0)
- Py_FatalError("Can't initialize object type");
+ if (PyType_Ready(&PyBaseObject_Type) < 0)
+ Py_FatalError("Can't initialize object type");
- if (PyType_Ready(&PyRange_Type) < 0)
- Py_FatalError("Can't initialize range type");
+ if (PyType_Ready(&PyRange_Type) < 0)
+ Py_FatalError("Can't initialize range type");
- if (PyType_Ready(&PyDict_Type) < 0)
- Py_FatalError("Can't initialize dict type");
+ if (PyType_Ready(&PyDict_Type) < 0)
+ Py_FatalError("Can't initialize dict type");
- if (PyType_Ready(&PySet_Type) < 0)
- Py_FatalError("Can't initialize set type");
+ if (PyType_Ready(&PySet_Type) < 0)
+ Py_FatalError("Can't initialize set type");
- if (PyType_Ready(&PyUnicode_Type) < 0)
- Py_FatalError("Can't initialize str type");
+ if (PyType_Ready(&PyUnicode_Type) < 0)
+ Py_FatalError("Can't initialize str type");
- if (PyType_Ready(&PySlice_Type) < 0)
- Py_FatalError("Can't initialize slice type");
+ if (PyType_Ready(&PySlice_Type) < 0)
+ Py_FatalError("Can't initialize slice type");
- if (PyType_Ready(&PyStaticMethod_Type) < 0)
- Py_FatalError("Can't initialize static method type");
+ if (PyType_Ready(&PyStaticMethod_Type) < 0)
+ Py_FatalError("Can't initialize static method type");
#ifndef WITHOUT_COMPLEX
- if (PyType_Ready(&PyComplex_Type) < 0)
- Py_FatalError("Can't initialize complex type");
+ if (PyType_Ready(&PyComplex_Type) < 0)
+ Py_FatalError("Can't initialize complex type");
#endif
- if (PyType_Ready(&PyFloat_Type) < 0)
- Py_FatalError("Can't initialize float type");
+ if (PyType_Ready(&PyFloat_Type) < 0)
+ Py_FatalError("Can't initialize float type");
- if (PyType_Ready(&PyLong_Type) < 0)
- Py_FatalError("Can't initialize int type");
+ if (PyType_Ready(&PyLong_Type) < 0)
+ Py_FatalError("Can't initialize int type");
- if (PyType_Ready(&PyFrozenSet_Type) < 0)
- Py_FatalError("Can't initialize frozenset type");
+ if (PyType_Ready(&PyFrozenSet_Type) < 0)
+ Py_FatalError("Can't initialize frozenset type");
- if (PyType_Ready(&PyProperty_Type) < 0)
- Py_FatalError("Can't initialize property type");
+ if (PyType_Ready(&PyProperty_Type) < 0)
+ Py_FatalError("Can't initialize property type");
- if (PyType_Ready(&PyMemoryView_Type) < 0)
- Py_FatalError("Can't initialize memoryview type");
+ if (PyType_Ready(&PyMemoryView_Type) < 0)
+ Py_FatalError("Can't initialize memoryview type");
- if (PyType_Ready(&PyTuple_Type) < 0)
- Py_FatalError("Can't initialize tuple type");
+ if (PyType_Ready(&PyTuple_Type) < 0)
+ Py_FatalError("Can't initialize tuple type");
- if (PyType_Ready(&PyEnum_Type) < 0)
- Py_FatalError("Can't initialize enumerate type");
+ if (PyType_Ready(&PyEnum_Type) < 0)
+ Py_FatalError("Can't initialize enumerate type");
- if (PyType_Ready(&PyReversed_Type) < 0)
- Py_FatalError("Can't initialize reversed type");
+ if (PyType_Ready(&PyReversed_Type) < 0)
+ Py_FatalError("Can't initialize reversed type");
- if (PyType_Ready(&PyStdPrinter_Type) < 0)
- Py_FatalError("Can't initialize StdPrinter");
+ if (PyType_Ready(&PyStdPrinter_Type) < 0)
+ Py_FatalError("Can't initialize StdPrinter");
- if (PyType_Ready(&PyCode_Type) < 0)
- Py_FatalError("Can't initialize code type");
+ if (PyType_Ready(&PyCode_Type) < 0)
+ Py_FatalError("Can't initialize code type");
- if (PyType_Ready(&PyFrame_Type) < 0)
- Py_FatalError("Can't initialize frame type");
+ if (PyType_Ready(&PyFrame_Type) < 0)
+ Py_FatalError("Can't initialize frame type");
- if (PyType_Ready(&PyCFunction_Type) < 0)
- Py_FatalError("Can't initialize builtin function type");
+ if (PyType_Ready(&PyCFunction_Type) < 0)
+ Py_FatalError("Can't initialize builtin function type");
- if (PyType_Ready(&PyMethod_Type) < 0)
- Py_FatalError("Can't initialize method type");
+ if (PyType_Ready(&PyMethod_Type) < 0)
+ Py_FatalError("Can't initialize method type");
- if (PyType_Ready(&PyFunction_Type) < 0)
- Py_FatalError("Can't initialize function type");
+ if (PyType_Ready(&PyFunction_Type) < 0)
+ Py_FatalError("Can't initialize function type");
- if (PyType_Ready(&PyDictProxy_Type) < 0)
- Py_FatalError("Can't initialize dict proxy type");
+ if (PyType_Ready(&PyDictProxy_Type) < 0)
+ Py_FatalError("Can't initialize dict proxy type");
- if (PyType_Ready(&PyGen_Type) < 0)
- Py_FatalError("Can't initialize generator type");
+ if (PyType_Ready(&PyGen_Type) < 0)
+ Py_FatalError("Can't initialize generator type");
- if (PyType_Ready(&PyGetSetDescr_Type) < 0)
- Py_FatalError("Can't initialize get-set descriptor type");
+ if (PyType_Ready(&PyGetSetDescr_Type) < 0)
+ Py_FatalError("Can't initialize get-set descriptor type");
- if (PyType_Ready(&PyWrapperDescr_Type) < 0)
- Py_FatalError("Can't initialize wrapper type");
+ if (PyType_Ready(&PyWrapperDescr_Type) < 0)
+ Py_FatalError("Can't initialize wrapper type");
- if (PyType_Ready(&PyEllipsis_Type) < 0)
- Py_FatalError("Can't initialize ellipsis type");
+ if (PyType_Ready(&PyEllipsis_Type) < 0)
+ Py_FatalError("Can't initialize ellipsis type");
- if (PyType_Ready(&PyMemberDescr_Type) < 0)
- Py_FatalError("Can't initialize member descriptor type");
+ if (PyType_Ready(&PyMemberDescr_Type) < 0)
+ Py_FatalError("Can't initialize member descriptor type");
- if (PyType_Ready(&PyFilter_Type) < 0)
- Py_FatalError("Can't initialize filter type");
+ if (PyType_Ready(&PyFilter_Type) < 0)
+ Py_FatalError("Can't initialize filter type");
- if (PyType_Ready(&PyMap_Type) < 0)
- Py_FatalError("Can't initialize map type");
+ if (PyType_Ready(&PyMap_Type) < 0)
+ Py_FatalError("Can't initialize map type");
- if (PyType_Ready(&PyZip_Type) < 0)
- Py_FatalError("Can't initialize zip type");
+ if (PyType_Ready(&PyZip_Type) < 0)
+ Py_FatalError("Can't initialize zip type");
}
@@ -1623,50 +1623,50 @@ _Py_ReadyTypes(void)
void
_Py_NewReference(PyObject *op)
{
- _Py_INC_REFTOTAL;
- op->ob_refcnt = 1;
- _Py_AddToAllObjects(op, 1);
- _Py_INC_TPALLOCS(op);
+ _Py_INC_REFTOTAL;
+ op->ob_refcnt = 1;
+ _Py_AddToAllObjects(op, 1);
+ _Py_INC_TPALLOCS(op);
}
void
_Py_ForgetReference(register PyObject *op)
{
#ifdef SLOW_UNREF_CHECK
- register PyObject *p;
+ register PyObject *p;
#endif
- if (op->ob_refcnt < 0)
- Py_FatalError("UNREF negative refcnt");
- if (op == &refchain ||
- op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
- fprintf(stderr, "* ob\n");
- _PyObject_Dump(op);
- fprintf(stderr, "* op->_ob_prev->_ob_next\n");
- _PyObject_Dump(op->_ob_prev->_ob_next);
- fprintf(stderr, "* op->_ob_next->_ob_prev\n");
- _PyObject_Dump(op->_ob_next->_ob_prev);
- Py_FatalError("UNREF invalid object");
- }
+ if (op->ob_refcnt < 0)
+ Py_FatalError("UNREF negative refcnt");
+ if (op == &refchain ||
+ op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
+ fprintf(stderr, "* ob\n");
+ _PyObject_Dump(op);
+ fprintf(stderr, "* op->_ob_prev->_ob_next\n");
+ _PyObject_Dump(op->_ob_prev->_ob_next);
+ fprintf(stderr, "* op->_ob_next->_ob_prev\n");
+ _PyObject_Dump(op->_ob_next->_ob_prev);
+ Py_FatalError("UNREF invalid object");
+ }
#ifdef SLOW_UNREF_CHECK
- for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
- if (p == op)
- break;
- }
- if (p == &refchain) /* Not found */
- Py_FatalError("UNREF unknown object");
+ for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
+ if (p == op)
+ break;
+ }
+ if (p == &refchain) /* Not found */
+ Py_FatalError("UNREF unknown object");
#endif
- op->_ob_next->_ob_prev = op->_ob_prev;
- op->_ob_prev->_ob_next = op->_ob_next;
- op->_ob_next = op->_ob_prev = NULL;
- _Py_INC_TPFREES(op);
+ op->_ob_next->_ob_prev = op->_ob_prev;
+ op->_ob_prev->_ob_next = op->_ob_next;
+ op->_ob_next = op->_ob_prev = NULL;
+ _Py_INC_TPFREES(op);
}
void
_Py_Dealloc(PyObject *op)
{
- destructor dealloc = Py_TYPE(op)->tp_dealloc;
- _Py_ForgetReference(op);
- (*dealloc)(op);
+ destructor dealloc = Py_TYPE(op)->tp_dealloc;
+ _Py_ForgetReference(op);
+ (*dealloc)(op);
}
/* Print all live objects. Because PyObject_Print is called, the
@@ -1675,14 +1675,14 @@ _Py_Dealloc(PyObject *op)
void
_Py_PrintReferences(FILE *fp)
{
- PyObject *op;
- fprintf(fp, "Remaining objects:\n");
- for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
- fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
- if (PyObject_Print(op, fp, 0) != 0)
- PyErr_Clear();
- putc('\n', fp);
- }
+ PyObject *op;
+ fprintf(fp, "Remaining objects:\n");
+ for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
+ fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
+ if (PyObject_Print(op, fp, 0) != 0)
+ PyErr_Clear();
+ putc('\n', fp);
+ }
}
/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
@@ -1691,40 +1691,40 @@ _Py_PrintReferences(FILE *fp)
void
_Py_PrintReferenceAddresses(FILE *fp)
{
- PyObject *op;
- fprintf(fp, "Remaining object addresses:\n");
- for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
- fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
- op->ob_refcnt, Py_TYPE(op)->tp_name);
+ PyObject *op;
+ fprintf(fp, "Remaining object addresses:\n");
+ for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
+ fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
+ op->ob_refcnt, Py_TYPE(op)->tp_name);
}
PyObject *
_Py_GetObjects(PyObject *self, PyObject *args)
{
- int i, n;
- PyObject *t = NULL;
- PyObject *res, *op;
-
- if (!PyArg_ParseTuple(args, "i|O", &n, &t))
- return NULL;
- op = refchain._ob_next;
- res = PyList_New(0);
- if (res == NULL)
- return NULL;
- for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
- while (op == self || op == args || op == res || op == t ||
- (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
- op = op->_ob_next;
- if (op == &refchain)
- return res;
- }
- if (PyList_Append(res, op) < 0) {
- Py_DECREF(res);
- return NULL;
- }
- op = op->_ob_next;
- }
- return res;
+ int i, n;
+ PyObject *t = NULL;
+ PyObject *res, *op;
+
+ if (!PyArg_ParseTuple(args, "i|O", &n, &t))
+ return NULL;
+ op = refchain._ob_next;
+ res = PyList_New(0);
+ if (res == NULL)
+ return NULL;
+ for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
+ while (op == self || op == args || op == res || op == t ||
+ (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
+ op = op->_ob_next;
+ if (op == &refchain)
+ return res;
+ }
+ if (PyList_Append(res, op) < 0) {
+ Py_DECREF(res);
+ return NULL;
+ }
+ op = op->_ob_next;
+ }
+ return res;
}
#endif
@@ -1746,19 +1746,19 @@ Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
void *
PyMem_Malloc(size_t nbytes)
{
- return PyMem_MALLOC(nbytes);
+ return PyMem_MALLOC(nbytes);
}
void *
PyMem_Realloc(void *p, size_t nbytes)
{
- return PyMem_REALLOC(p, nbytes);
+ return PyMem_REALLOC(p, nbytes);
}
void
PyMem_Free(void *p)
{
- PyMem_FREE(p);
+ PyMem_FREE(p);
}
@@ -1779,52 +1779,52 @@ PyMem_Free(void *p)
int
Py_ReprEnter(PyObject *obj)
{
- PyObject *dict;
- PyObject *list;
- Py_ssize_t i;
-
- dict = PyThreadState_GetDict();
- if (dict == NULL)
- return 0;
- list = PyDict_GetItemString(dict, KEY);
- if (list == NULL) {
- list = PyList_New(0);
- if (list == NULL)
- return -1;
- if (PyDict_SetItemString(dict, KEY, list) < 0)
- return -1;
- Py_DECREF(list);
- }
- i = PyList_GET_SIZE(list);
- while (--i >= 0) {
- if (PyList_GET_ITEM(list, i) == obj)
- return 1;
- }
- PyList_Append(list, obj);
- return 0;
+ PyObject *dict;
+ PyObject *list;
+ Py_ssize_t i;
+
+ dict = PyThreadState_GetDict();
+ if (dict == NULL)
+ return 0;
+ list = PyDict_GetItemString(dict, KEY);
+ if (list == NULL) {
+ list = PyList_New(0);
+ if (list == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, KEY, list) < 0)
+ return -1;
+ Py_DECREF(list);
+ }
+ i = PyList_GET_SIZE(list);
+ while (--i >= 0) {
+ if (PyList_GET_ITEM(list, i) == obj)
+ return 1;
+ }
+ PyList_Append(list, obj);
+ return 0;
}
void
Py_ReprLeave(PyObject *obj)
{
- PyObject *dict;
- PyObject *list;
- Py_ssize_t i;
-
- dict = PyThreadState_GetDict();
- if (dict == NULL)
- return;
- list = PyDict_GetItemString(dict, KEY);
- if (list == NULL || !PyList_Check(list))
- return;
- i = PyList_GET_SIZE(list);
- /* Count backwards because we always expect obj to be list[-1] */
- while (--i >= 0) {
- if (PyList_GET_ITEM(list, i) == obj) {
- PyList_SetSlice(list, i, i + 1, NULL);
- break;
- }
- }
+ PyObject *dict;
+ PyObject *list;
+ Py_ssize_t i;
+
+ dict = PyThreadState_GetDict();
+ if (dict == NULL)
+ return;
+ list = PyDict_GetItemString(dict, KEY);
+ if (list == NULL || !PyList_Check(list))
+ return;
+ i = PyList_GET_SIZE(list);
+ /* Count backwards because we always expect obj to be list[-1] */
+ while (--i >= 0) {
+ if (PyList_GET_ITEM(list, i) == obj) {
+ PyList_SetSlice(list, i, i + 1, NULL);
+ break;
+ }
+ }
}
/* Trashcan support. */
@@ -1844,11 +1844,11 @@ PyObject *_PyTrash_delete_later = NULL;
void
_PyTrash_deposit_object(PyObject *op)
{
- assert(PyObject_IS_GC(op));
- assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
- assert(op->ob_refcnt == 0);
- _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
- _PyTrash_delete_later = op;
+ assert(PyObject_IS_GC(op));
+ assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
+ assert(op->ob_refcnt == 0);
+ _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
+ _PyTrash_delete_later = op;
}
/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
@@ -1857,24 +1857,24 @@ _PyTrash_deposit_object(PyObject *op)
void
_PyTrash_destroy_chain(void)
{
- while (_PyTrash_delete_later) {
- PyObject *op = _PyTrash_delete_later;
- destructor dealloc = Py_TYPE(op)->tp_dealloc;
-
- _PyTrash_delete_later =
- (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
-
- /* Call the deallocator directly. This used to try to
- * fool Py_DECREF into calling it indirectly, but
- * Py_DECREF was already called on this object, and in
- * assorted non-release builds calling Py_DECREF again ends
- * up distorting allocation statistics.
- */
- assert(op->ob_refcnt == 0);
- ++_PyTrash_delete_nesting;
- (*dealloc)(op);
- --_PyTrash_delete_nesting;
- }
+ while (_PyTrash_delete_later) {
+ PyObject *op = _PyTrash_delete_later;
+ destructor dealloc = Py_TYPE(op)->tp_dealloc;
+
+ _PyTrash_delete_later =
+ (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
+
+ /* Call the deallocator directly. This used to try to
+ * fool Py_DECREF into calling it indirectly, but
+ * Py_DECREF was already called on this object, and in
+ * assorted non-release builds calling Py_DECREF again ends
+ * up distorting allocation statistics.
+ */
+ assert(op->ob_refcnt == 0);
+ ++_PyTrash_delete_nesting;
+ (*dealloc)(op);
+ --_PyTrash_delete_nesting;
+ }
}
#ifdef __cplusplus
diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
index d1586c2..f3297f2 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -12,7 +12,7 @@
the cyclic garbage collector operates selectively on container objects.
- Object-specific allocators
+ Object-specific allocators
_____ ______ ______ ________
[ int ] [ dict ] [ list ] ... [ string ] Python core |
+3 | <----- Object-specific memory -----> | <-- Non-object memory --> |
@@ -52,7 +52,7 @@
* in Proc. 1995 Int'l. Workshop on Memory Management, September 1995.
*/
-/* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */
+/* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */
/*==========================================================================*/
@@ -80,22 +80,22 @@
*
* For small requests we have the following table:
*
- * Request in bytes Size of allocated block Size class idx
+ * Request in bytes Size of allocated block Size class idx
* ----------------------------------------------------------------
* 1-8 8 0
- * 9-16 16 1
- * 17-24 24 2
- * 25-32 32 3
- * 33-40 40 4
- * 41-48 48 5
- * 49-56 56 6
- * 57-64 64 7
- * 65-72 72 8
- * ... ... ...
- * 241-248 248 30
- * 249-256 256 31
+ * 9-16 16 1
+ * 17-24 24 2
+ * 25-32 32 3
+ * 33-40 40 4
+ * 41-48 48 5
+ * 49-56 56 6
+ * 57-64 64 7
+ * 65-72 72 8
+ * ... ... ...
+ * 241-248 248 30
+ * 249-256 256 31
*
- * 0, 257 and up: routed to the underlying allocator.
+ * 0, 257 and up: routed to the underlying allocator.
*/
/*==========================================================================*/
@@ -112,9 +112,9 @@
*
* You shouldn't change this unless you know what you are doing.
*/
-#define ALIGNMENT 8 /* must be 2^N */
-#define ALIGNMENT_SHIFT 3
-#define ALIGNMENT_MASK (ALIGNMENT - 1)
+#define ALIGNMENT 8 /* must be 2^N */
+#define ALIGNMENT_SHIFT 3
+#define ALIGNMENT_MASK (ALIGNMENT - 1)
/* Return the number of bytes in size class I, as a uint. */
#define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT)
@@ -125,14 +125,14 @@
* this value according to your application behaviour and memory needs.
*
* The following invariants must hold:
- * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
- * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
+ * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
+ * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
*
* Although not required, for better performance and space efficiency,
* it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
*/
-#define SMALL_REQUEST_THRESHOLD 256
-#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
+#define SMALL_REQUEST_THRESHOLD 256
+#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
/*
* The system's VMM page size can be obtained on most unices with a
@@ -144,15 +144,15 @@
* violation fault. 4K is apparently OK for all the platforms that python
* currently targets.
*/
-#define SYSTEM_PAGE_SIZE (4 * 1024)
-#define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1)
+#define SYSTEM_PAGE_SIZE (4 * 1024)
+#define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1)
/*
* Maximum amount of memory managed by the allocator for small requests.
*/
#ifdef WITH_MEMORY_LIMITS
#ifndef SMALL_MEMORY_LIMIT
-#define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */
+#define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */
#endif
#endif
@@ -169,18 +169,18 @@
* some address space wastage, but this is the most portable way to request
* memory from the system across various platforms.
*/
-#define ARENA_SIZE (256 << 10) /* 256KB */
+#define ARENA_SIZE (256 << 10) /* 256KB */
#ifdef WITH_MEMORY_LIMITS
-#define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE)
+#define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE)
#endif
/*
* Size of the pools used for small blocks. Should be a power of 2,
* between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k.
*/
-#define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */
-#define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK
+#define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */
+#define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK
/*
* -- End of tunable settings section --
@@ -206,92 +206,92 @@
/*
* Python's threads are serialized, so object malloc locking is disabled.
*/
-#define SIMPLELOCK_DECL(lock) /* simple lock declaration */
-#define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */
-#define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */
-#define SIMPLELOCK_LOCK(lock) /* acquire released lock */
-#define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */
+#define SIMPLELOCK_DECL(lock) /* simple lock declaration */
+#define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */
+#define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */
+#define SIMPLELOCK_LOCK(lock) /* acquire released lock */
+#define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */
/*
* Basic types
* I don't care if these are defined in <sys/types.h> or elsewhere. Axiom.
*/
#undef uchar
-#define uchar unsigned char /* assuming == 8 bits */
+#define uchar unsigned char /* assuming == 8 bits */
#undef uint
-#define uint unsigned int /* assuming >= 16 bits */
+#define uint unsigned int /* assuming >= 16 bits */
#undef ulong
-#define ulong unsigned long /* assuming >= 32 bits */
+#define ulong unsigned long /* assuming >= 32 bits */
#undef uptr
-#define uptr Py_uintptr_t
+#define uptr Py_uintptr_t
/* When you say memory, my mind reasons in terms of (pointers to) blocks */
typedef uchar block;
/* Pool for small blocks. */
struct pool_header {
- union { block *_padding;
- uint count; } ref; /* number of allocated blocks */
- block *freeblock; /* pool's free list head */
- struct pool_header *nextpool; /* next pool of this size class */
- struct pool_header *prevpool; /* previous pool "" */
- uint arenaindex; /* index into arenas of base adr */
- uint szidx; /* block size class index */
- uint nextoffset; /* bytes to virgin block */
- uint maxnextoffset; /* largest valid nextoffset */
+ union { block *_padding;
+ uint count; } ref; /* number of allocated blocks */
+ block *freeblock; /* pool's free list head */
+ struct pool_header *nextpool; /* next pool of this size class */
+ struct pool_header *prevpool; /* previous pool "" */
+ uint arenaindex; /* index into arenas of base adr */
+ uint szidx; /* block size class index */
+ uint nextoffset; /* bytes to virgin block */
+ uint maxnextoffset; /* largest valid nextoffset */
};
typedef struct pool_header *poolp;
/* Record keeping for arenas. */
struct arena_object {
- /* The address of the arena, as returned by malloc. Note that 0
- * will never be returned by a successful malloc, and is used
- * here to mark an arena_object that doesn't correspond to an
- * allocated arena.
- */
- uptr address;
-
- /* Pool-aligned pointer to the next pool to be carved off. */
- block* pool_address;
-
- /* The number of available pools in the arena: free pools + never-
- * allocated pools.
- */
- uint nfreepools;
-
- /* The total number of pools in the arena, whether or not available. */
- uint ntotalpools;
-
- /* Singly-linked list of available pools. */
- struct pool_header* freepools;
-
- /* Whenever this arena_object is not associated with an allocated
- * arena, the nextarena member is used to link all unassociated
- * arena_objects in the singly-linked `unused_arena_objects` list.
- * The prevarena member is unused in this case.
- *
- * When this arena_object is associated with an allocated arena
- * with at least one available pool, both members are used in the
- * doubly-linked `usable_arenas` list, which is maintained in
- * increasing order of `nfreepools` values.
- *
- * Else this arena_object is associated with an allocated arena
- * all of whose pools are in use. `nextarena` and `prevarena`
- * are both meaningless in this case.
- */
- struct arena_object* nextarena;
- struct arena_object* prevarena;
+ /* The address of the arena, as returned by malloc. Note that 0
+ * will never be returned by a successful malloc, and is used
+ * here to mark an arena_object that doesn't correspond to an
+ * allocated arena.
+ */
+ uptr address;
+
+ /* Pool-aligned pointer to the next pool to be carved off. */
+ block* pool_address;
+
+ /* The number of available pools in the arena: free pools + never-
+ * allocated pools.
+ */
+ uint nfreepools;
+
+ /* The total number of pools in the arena, whether or not available. */
+ uint ntotalpools;
+
+ /* Singly-linked list of available pools. */
+ struct pool_header* freepools;
+
+ /* Whenever this arena_object is not associated with an allocated
+ * arena, the nextarena member is used to link all unassociated
+ * arena_objects in the singly-linked `unused_arena_objects` list.
+ * The prevarena member is unused in this case.
+ *
+ * When this arena_object is associated with an allocated arena
+ * with at least one available pool, both members are used in the
+ * doubly-linked `usable_arenas` list, which is maintained in
+ * increasing order of `nfreepools` values.
+ *
+ * Else this arena_object is associated with an allocated arena
+ * all of whose pools are in use. `nextarena` and `prevarena`
+ * are both meaningless in this case.
+ */
+ struct arena_object* nextarena;
+ struct arena_object* prevarena;
};
#undef ROUNDUP
-#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
-#define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header))
+#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
+#define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header))
-#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
+#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
/* Round pointer P down to the closest pool-aligned address <= P, as a poolp */
#define POOL_ADDR(P) ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK))
@@ -305,10 +305,10 @@ struct arena_object {
* This malloc lock
*/
SIMPLELOCK_DECL(_malloc_lock)
-#define LOCK() SIMPLELOCK_LOCK(_malloc_lock)
-#define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock)
-#define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock)
-#define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock)
+#define LOCK() SIMPLELOCK_LOCK(_malloc_lock)
+#define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock)
+#define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock)
+#define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock)
/*
* Pool table -- headed, circular, doubly-linked lists of partially used pools.
@@ -388,9 +388,9 @@ nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is
compensating for that a pool_header's nextpool and prevpool members
immediately follow a pool_header's first two members:
- union { block *_padding;
- uint count; } ref;
- block *freeblock;
+ union { block *_padding;
+ uint count; } ref;
+ block *freeblock;
each of which consume sizeof(block *) bytes. So what usedpools[i+i] really
contains is a fudged-up pointer p such that *if* C believes it's a poolp
@@ -406,25 +406,25 @@ on that C doesn't insert any padding anywhere in a pool_header at or before
the prevpool member.
**************************************************************************** */
-#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
-#define PT(x) PTA(x), PTA(x)
+#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
+#define PT(x) PTA(x), PTA(x)
static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
- PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
+ PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
#if NB_SMALL_SIZE_CLASSES > 8
- , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
+ , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
#if NB_SMALL_SIZE_CLASSES > 16
- , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
+ , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
#if NB_SMALL_SIZE_CLASSES > 24
- , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
+ , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
#if NB_SMALL_SIZE_CLASSES > 32
- , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
+ , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
#if NB_SMALL_SIZE_CLASSES > 40
- , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
+ , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
#if NB_SMALL_SIZE_CLASSES > 48
- , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
+ , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
#if NB_SMALL_SIZE_CLASSES > 56
- , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
+ , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
#endif /* NB_SMALL_SIZE_CLASSES > 56 */
#endif /* NB_SMALL_SIZE_CLASSES > 48 */
#endif /* NB_SMALL_SIZE_CLASSES > 40 */
@@ -508,90 +508,90 @@ static size_t narenas_highwater = 0;
static struct arena_object*
new_arena(void)
{
- struct arena_object* arenaobj;
- uint excess; /* number of bytes above pool alignment */
+ struct arena_object* arenaobj;
+ uint excess; /* number of bytes above pool alignment */
#ifdef PYMALLOC_DEBUG
- if (Py_GETENV("PYTHONMALLOCSTATS"))
- _PyObject_DebugMallocStats();
+ if (Py_GETENV("PYTHONMALLOCSTATS"))
+ _PyObject_DebugMallocStats();
#endif
- if (unused_arena_objects == NULL) {
- uint i;
- uint numarenas;
- size_t nbytes;
-
- /* Double the number of arena objects on each allocation.
- * Note that it's possible for `numarenas` to overflow.
- */
- numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS;
- if (numarenas <= maxarenas)
- return NULL; /* overflow */
+ if (unused_arena_objects == NULL) {
+ uint i;
+ uint numarenas;
+ size_t nbytes;
+
+ /* Double the number of arena objects on each allocation.
+ * Note that it's possible for `numarenas` to overflow.
+ */
+ numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS;
+ if (numarenas <= maxarenas)
+ return NULL; /* overflow */
#if SIZEOF_SIZE_T <= SIZEOF_INT
- if (numarenas > PY_SIZE_MAX / sizeof(*arenas))
- return NULL; /* overflow */
+ if (numarenas > PY_SIZE_MAX / sizeof(*arenas))
+ return NULL; /* overflow */
#endif
- nbytes = numarenas * sizeof(*arenas);
- arenaobj = (struct arena_object *)realloc(arenas, nbytes);
- if (arenaobj == NULL)
- return NULL;
- arenas = arenaobj;
-
- /* We might need to fix pointers that were copied. However,
- * new_arena only gets called when all the pages in the
- * previous arenas are full. Thus, there are *no* pointers
- * into the old array. Thus, we don't have to worry about
- * invalid pointers. Just to be sure, some asserts:
- */
- assert(usable_arenas == NULL);
- assert(unused_arena_objects == NULL);
-
- /* Put the new arenas on the unused_arena_objects list. */
- for (i = maxarenas; i < numarenas; ++i) {
- arenas[i].address = 0; /* mark as unassociated */
- arenas[i].nextarena = i < numarenas - 1 ?
- &arenas[i+1] : NULL;
- }
-
- /* Update globals. */
- unused_arena_objects = &arenas[maxarenas];
- maxarenas = numarenas;
- }
-
- /* Take the next available arena object off the head of the list. */
- assert(unused_arena_objects != NULL);
- arenaobj = unused_arena_objects;
- unused_arena_objects = arenaobj->nextarena;
- assert(arenaobj->address == 0);
- arenaobj->address = (uptr)malloc(ARENA_SIZE);
- if (arenaobj->address == 0) {
- /* The allocation failed: return NULL after putting the
- * arenaobj back.
- */
- arenaobj->nextarena = unused_arena_objects;
- unused_arena_objects = arenaobj;
- return NULL;
- }
-
- ++narenas_currently_allocated;
+ nbytes = numarenas * sizeof(*arenas);
+ arenaobj = (struct arena_object *)realloc(arenas, nbytes);
+ if (arenaobj == NULL)
+ return NULL;
+ arenas = arenaobj;
+
+ /* We might need to fix pointers that were copied. However,
+ * new_arena only gets called when all the pages in the
+ * previous arenas are full. Thus, there are *no* pointers
+ * into the old array. Thus, we don't have to worry about
+ * invalid pointers. Just to be sure, some asserts:
+ */
+ assert(usable_arenas == NULL);
+ assert(unused_arena_objects == NULL);
+
+ /* Put the new arenas on the unused_arena_objects list. */
+ for (i = maxarenas; i < numarenas; ++i) {
+ arenas[i].address = 0; /* mark as unassociated */
+ arenas[i].nextarena = i < numarenas - 1 ?
+ &arenas[i+1] : NULL;
+ }
+
+ /* Update globals. */
+ unused_arena_objects = &arenas[maxarenas];
+ maxarenas = numarenas;
+ }
+
+ /* Take the next available arena object off the head of the list. */
+ assert(unused_arena_objects != NULL);
+ arenaobj = unused_arena_objects;
+ unused_arena_objects = arenaobj->nextarena;
+ assert(arenaobj->address == 0);
+ arenaobj->address = (uptr)malloc(ARENA_SIZE);
+ if (arenaobj->address == 0) {
+ /* The allocation failed: return NULL after putting the
+ * arenaobj back.
+ */
+ arenaobj->nextarena = unused_arena_objects;
+ unused_arena_objects = arenaobj;
+ return NULL;
+ }
+
+ ++narenas_currently_allocated;
#ifdef PYMALLOC_DEBUG
- ++ntimes_arena_allocated;
- if (narenas_currently_allocated > narenas_highwater)
- narenas_highwater = narenas_currently_allocated;
+ ++ntimes_arena_allocated;
+ if (narenas_currently_allocated > narenas_highwater)
+ narenas_highwater = narenas_currently_allocated;
#endif
- arenaobj->freepools = NULL;
- /* pool_address <- first pool-aligned address in the arena
- nfreepools <- number of whole pools that fit after alignment */
- arenaobj->pool_address = (block*)arenaobj->address;
- arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE;
- assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE);
- excess = (uint)(arenaobj->address & POOL_SIZE_MASK);
- if (excess != 0) {
- --arenaobj->nfreepools;
- arenaobj->pool_address += POOL_SIZE - excess;
- }
- arenaobj->ntotalpools = arenaobj->nfreepools;
-
- return arenaobj;
+ arenaobj->freepools = NULL;
+ /* pool_address <- first pool-aligned address in the arena
+ nfreepools <- number of whole pools that fit after alignment */
+ arenaobj->pool_address = (block*)arenaobj->address;
+ arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE;
+ assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE);
+ excess = (uint)(arenaobj->address & POOL_SIZE_MASK);
+ if (excess != 0) {
+ --arenaobj->nfreepools;
+ arenaobj->pool_address += POOL_SIZE - excess;
+ }
+ arenaobj->ntotalpools = arenaobj->nfreepools;
+
+ return arenaobj;
}
/*
@@ -607,11 +607,11 @@ called on every alloc/realloc/free, micro-efficiency is important here).
Tricky: Let B be the arena base address associated with the pool, B =
arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if
- B <= P < B + ARENA_SIZE
+ B <= P < B + ARENA_SIZE
Subtracting B throughout, this is true iff
- 0 <= P-B < ARENA_SIZE
+ 0 <= P-B < ARENA_SIZE
By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
@@ -645,7 +645,7 @@ Finally, if P is not controlled by obmalloc and AO corresponds to an unused
arena_object (one not currently associated with an allocated arena),
AO.address is 0, and the second test in the macro reduces to:
- P < ARENA_SIZE
+ P < ARENA_SIZE
If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes
that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part
@@ -668,10 +668,10 @@ obmalloc in a small constant time, independent of the number of arenas
obmalloc controls. Since this test is needed at every entry point, it's
extremely desirable that it be this fast.
*/
-#define Py_ADDRESS_IN_RANGE(P, POOL) \
- ((POOL)->arenaindex < maxarenas && \
- (uptr)(P) - arenas[(POOL)->arenaindex].address < (uptr)ARENA_SIZE && \
- arenas[(POOL)->arenaindex].address != 0)
+#define Py_ADDRESS_IN_RANGE(P, POOL) \
+ ((POOL)->arenaindex < maxarenas && \
+ (uptr)(P) - arenas[(POOL)->arenaindex].address < (uptr)ARENA_SIZE && \
+ arenas[(POOL)->arenaindex].address != 0)
/* This is only useful when running memory debuggers such as
@@ -694,7 +694,7 @@ extremely desirable that it be this fast.
#undef Py_ADDRESS_IN_RANGE
#if defined(__GNUC__) && ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) || \
- (__GNUC__ >= 4))
+ (__GNUC__ >= 4))
#define Py_NO_INLINE __attribute__((__noinline__))
#else
#define Py_NO_INLINE
@@ -723,194 +723,194 @@ int Py_ADDRESS_IN_RANGE(void *P, poolp pool) Py_NO_INLINE;
void *
PyObject_Malloc(size_t nbytes)
{
- block *bp;
- poolp pool;
- poolp next;
- uint size;
-
- /*
- * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
- * Most python internals blindly use a signed Py_ssize_t to track
- * things without checking for overflows or negatives.
- * As size_t is unsigned, checking for nbytes < 0 is not required.
- */
- if (nbytes > PY_SSIZE_T_MAX)
- return NULL;
-
- /*
- * This implicitly redirects malloc(0).
- */
- if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
- LOCK();
- /*
- * Most frequent paths first
- */
- size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
- pool = usedpools[size + size];
- if (pool != pool->nextpool) {
- /*
- * There is a used pool for this size class.
- * Pick up the head block of its free list.
- */
- ++pool->ref.count;
- bp = pool->freeblock;
- assert(bp != NULL);
- if ((pool->freeblock = *(block **)bp) != NULL) {
- UNLOCK();
- return (void *)bp;
- }
- /*
- * Reached the end of the free list, try to extend it.
- */
- if (pool->nextoffset <= pool->maxnextoffset) {
- /* There is room for another block. */
- pool->freeblock = (block*)pool +
- pool->nextoffset;
- pool->nextoffset += INDEX2SIZE(size);
- *(block **)(pool->freeblock) = NULL;
- UNLOCK();
- return (void *)bp;
- }
- /* Pool is full, unlink from used pools. */
- next = pool->nextpool;
- pool = pool->prevpool;
- next->prevpool = pool;
- pool->nextpool = next;
- UNLOCK();
- return (void *)bp;
- }
-
- /* There isn't a pool of the right size class immediately
- * available: use a free pool.
- */
- if (usable_arenas == NULL) {
- /* No arena has a free pool: allocate a new arena. */
+ block *bp;
+ poolp pool;
+ poolp next;
+ uint size;
+
+ /*
+ * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
+ * Most python internals blindly use a signed Py_ssize_t to track
+ * things without checking for overflows or negatives.
+ * As size_t is unsigned, checking for nbytes < 0 is not required.
+ */
+ if (nbytes > PY_SSIZE_T_MAX)
+ return NULL;
+
+ /*
+ * This implicitly redirects malloc(0).
+ */
+ if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
+ LOCK();
+ /*
+ * Most frequent paths first
+ */
+ size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
+ pool = usedpools[size + size];
+ if (pool != pool->nextpool) {
+ /*
+ * There is a used pool for this size class.
+ * Pick up the head block of its free list.
+ */
+ ++pool->ref.count;
+ bp = pool->freeblock;
+ assert(bp != NULL);
+ if ((pool->freeblock = *(block **)bp) != NULL) {
+ UNLOCK();
+ return (void *)bp;
+ }
+ /*
+ * Reached the end of the free list, try to extend it.
+ */
+ if (pool->nextoffset <= pool->maxnextoffset) {
+ /* There is room for another block. */
+ pool->freeblock = (block*)pool +
+ pool->nextoffset;
+ pool->nextoffset += INDEX2SIZE(size);
+ *(block **)(pool->freeblock) = NULL;
+ UNLOCK();
+ return (void *)bp;
+ }
+ /* Pool is full, unlink from used pools. */
+ next = pool->nextpool;
+ pool = pool->prevpool;
+ next->prevpool = pool;
+ pool->nextpool = next;
+ UNLOCK();
+ return (void *)bp;
+ }
+
+ /* There isn't a pool of the right size class immediately
+ * available: use a free pool.
+ */
+ if (usable_arenas == NULL) {
+ /* No arena has a free pool: allocate a new arena. */
#ifdef WITH_MEMORY_LIMITS
- if (narenas_currently_allocated >= MAX_ARENAS) {
- UNLOCK();
- goto redirect;
- }
+ if (narenas_currently_allocated >= MAX_ARENAS) {
+ UNLOCK();
+ goto redirect;
+ }
#endif
- usable_arenas = new_arena();
- if (usable_arenas == NULL) {
- UNLOCK();
- goto redirect;
- }
- usable_arenas->nextarena =
- usable_arenas->prevarena = NULL;
- }
- assert(usable_arenas->address != 0);
-
- /* Try to get a cached free pool. */
- pool = usable_arenas->freepools;
- if (pool != NULL) {
- /* Unlink from cached pools. */
- usable_arenas->freepools = pool->nextpool;
-
- /* This arena already had the smallest nfreepools
- * value, so decreasing nfreepools doesn't change
- * that, and we don't need to rearrange the
- * usable_arenas list. However, if the arena has
- * become wholly allocated, we need to remove its
- * arena_object from usable_arenas.
- */
- --usable_arenas->nfreepools;
- if (usable_arenas->nfreepools == 0) {
- /* Wholly allocated: remove. */
- assert(usable_arenas->freepools == NULL);
- assert(usable_arenas->nextarena == NULL ||
- usable_arenas->nextarena->prevarena ==
- usable_arenas);
-
- usable_arenas = usable_arenas->nextarena;
- if (usable_arenas != NULL) {
- usable_arenas->prevarena = NULL;
- assert(usable_arenas->address != 0);
- }
- }
- else {
- /* nfreepools > 0: it must be that freepools
- * isn't NULL, or that we haven't yet carved
- * off all the arena's pools for the first
- * time.
- */
- assert(usable_arenas->freepools != NULL ||
- usable_arenas->pool_address <=
- (block*)usable_arenas->address +
- ARENA_SIZE - POOL_SIZE);
- }
- init_pool:
- /* Frontlink to used pools. */
- next = usedpools[size + size]; /* == prev */
- pool->nextpool = next;
- pool->prevpool = next;
- next->nextpool = pool;
- next->prevpool = pool;
- pool->ref.count = 1;
- if (pool->szidx == size) {
- /* Luckily, this pool last contained blocks
- * of the same size class, so its header
- * and free list are already initialized.
- */
- bp = pool->freeblock;
- pool->freeblock = *(block **)bp;
- UNLOCK();
- return (void *)bp;
- }
- /*
- * Initialize the pool header, set up the free list to
- * contain just the second block, and return the first
- * block.
- */
- pool->szidx = size;
- size = INDEX2SIZE(size);
- bp = (block *)pool + POOL_OVERHEAD;
- pool->nextoffset = POOL_OVERHEAD + (size << 1);
- pool->maxnextoffset = POOL_SIZE - size;
- pool->freeblock = bp + size;
- *(block **)(pool->freeblock) = NULL;
- UNLOCK();
- return (void *)bp;
- }
-
- /* Carve off a new pool. */
- assert(usable_arenas->nfreepools > 0);
- assert(usable_arenas->freepools == NULL);
- pool = (poolp)usable_arenas->pool_address;
- assert((block*)pool <= (block*)usable_arenas->address +
- ARENA_SIZE - POOL_SIZE);
- pool->arenaindex = usable_arenas - arenas;
- assert(&arenas[pool->arenaindex] == usable_arenas);
- pool->szidx = DUMMY_SIZE_IDX;
- usable_arenas->pool_address += POOL_SIZE;
- --usable_arenas->nfreepools;
-
- if (usable_arenas->nfreepools == 0) {
- assert(usable_arenas->nextarena == NULL ||
- usable_arenas->nextarena->prevarena ==
- usable_arenas);
- /* Unlink the arena: it is completely allocated. */
- usable_arenas = usable_arenas->nextarena;
- if (usable_arenas != NULL) {
- usable_arenas->prevarena = NULL;
- assert(usable_arenas->address != 0);
- }
- }
-
- goto init_pool;
- }
-
- /* The small block allocator ends here. */
+ usable_arenas = new_arena();
+ if (usable_arenas == NULL) {
+ UNLOCK();
+ goto redirect;
+ }
+ usable_arenas->nextarena =
+ usable_arenas->prevarena = NULL;
+ }
+ assert(usable_arenas->address != 0);
+
+ /* Try to get a cached free pool. */
+ pool = usable_arenas->freepools;
+ if (pool != NULL) {
+ /* Unlink from cached pools. */
+ usable_arenas->freepools = pool->nextpool;
+
+ /* This arena already had the smallest nfreepools
+ * value, so decreasing nfreepools doesn't change
+ * that, and we don't need to rearrange the
+ * usable_arenas list. However, if the arena has
+ * become wholly allocated, we need to remove its
+ * arena_object from usable_arenas.
+ */
+ --usable_arenas->nfreepools;
+ if (usable_arenas->nfreepools == 0) {
+ /* Wholly allocated: remove. */
+ assert(usable_arenas->freepools == NULL);
+ assert(usable_arenas->nextarena == NULL ||
+ usable_arenas->nextarena->prevarena ==
+ usable_arenas);
+
+ usable_arenas = usable_arenas->nextarena;
+ if (usable_arenas != NULL) {
+ usable_arenas->prevarena = NULL;
+ assert(usable_arenas->address != 0);
+ }
+ }
+ else {
+ /* nfreepools > 0: it must be that freepools
+ * isn't NULL, or that we haven't yet carved
+ * off all the arena's pools for the first
+ * time.
+ */
+ assert(usable_arenas->freepools != NULL ||
+ usable_arenas->pool_address <=
+ (block*)usable_arenas->address +
+ ARENA_SIZE - POOL_SIZE);
+ }
+ init_pool:
+ /* Frontlink to used pools. */
+ next = usedpools[size + size]; /* == prev */
+ pool->nextpool = next;
+ pool->prevpool = next;
+ next->nextpool = pool;
+ next->prevpool = pool;
+ pool->ref.count = 1;
+ if (pool->szidx == size) {
+ /* Luckily, this pool last contained blocks
+ * of the same size class, so its header
+ * and free list are already initialized.
+ */
+ bp = pool->freeblock;
+ pool->freeblock = *(block **)bp;
+ UNLOCK();
+ return (void *)bp;
+ }
+ /*
+ * Initialize the pool header, set up the free list to
+ * contain just the second block, and return the first
+ * block.
+ */
+ pool->szidx = size;
+ size = INDEX2SIZE(size);
+ bp = (block *)pool + POOL_OVERHEAD;
+ pool->nextoffset = POOL_OVERHEAD + (size << 1);
+ pool->maxnextoffset = POOL_SIZE - size;
+ pool->freeblock = bp + size;
+ *(block **)(pool->freeblock) = NULL;
+ UNLOCK();
+ return (void *)bp;
+ }
+
+ /* Carve off a new pool. */
+ assert(usable_arenas->nfreepools > 0);
+ assert(usable_arenas->freepools == NULL);
+ pool = (poolp)usable_arenas->pool_address;
+ assert((block*)pool <= (block*)usable_arenas->address +
+ ARENA_SIZE - POOL_SIZE);
+ pool->arenaindex = usable_arenas - arenas;
+ assert(&arenas[pool->arenaindex] == usable_arenas);
+ pool->szidx = DUMMY_SIZE_IDX;
+ usable_arenas->pool_address += POOL_SIZE;
+ --usable_arenas->nfreepools;
+
+ if (usable_arenas->nfreepools == 0) {
+ assert(usable_arenas->nextarena == NULL ||
+ usable_arenas->nextarena->prevarena ==
+ usable_arenas);
+ /* Unlink the arena: it is completely allocated. */
+ usable_arenas = usable_arenas->nextarena;
+ if (usable_arenas != NULL) {
+ usable_arenas->prevarena = NULL;
+ assert(usable_arenas->address != 0);
+ }
+ }
+
+ goto init_pool;
+ }
+
+ /* The small block allocator ends here. */
redirect:
- /* Redirect the original request to the underlying (libc) allocator.
- * We jump here on bigger requests, on error in the code above (as a
- * last chance to serve the request) or when the max memory limit
- * has been reached.
- */
- if (nbytes == 0)
- nbytes = 1;
- return (void *)malloc(nbytes);
+ /* Redirect the original request to the underlying (libc) allocator.
+ * We jump here on bigger requests, on error in the code above (as a
+ * last chance to serve the request) or when the max memory limit
+ * has been reached.
+ */
+ if (nbytes == 0)
+ nbytes = 1;
+ return (void *)malloc(nbytes);
}
/* free */
@@ -919,210 +919,210 @@ redirect:
void
PyObject_Free(void *p)
{
- poolp pool;
- block *lastfree;
- poolp next, prev;
- uint size;
-
- if (p == NULL) /* free(NULL) has no effect */
- return;
-
- pool = POOL_ADDR(p);
- if (Py_ADDRESS_IN_RANGE(p, pool)) {
- /* We allocated this address. */
- LOCK();
- /* Link p to the start of the pool's freeblock list. Since
- * the pool had at least the p block outstanding, the pool
- * wasn't empty (so it's already in a usedpools[] list, or
- * was full and is in no list -- it's not in the freeblocks
- * list in any case).
- */
- assert(pool->ref.count > 0); /* else it was empty */
- *(block **)p = lastfree = pool->freeblock;
- pool->freeblock = (block *)p;
- if (lastfree) {
- struct arena_object* ao;
- uint nf; /* ao->nfreepools */
-
- /* freeblock wasn't NULL, so the pool wasn't full,
- * and the pool is in a usedpools[] list.
- */
- if (--pool->ref.count != 0) {
- /* pool isn't empty: leave it in usedpools */
- UNLOCK();
- return;
- }
- /* Pool is now empty: unlink from usedpools, and
- * link to the front of freepools. This ensures that
- * previously freed pools will be allocated later
- * (being not referenced, they are perhaps paged out).
- */
- next = pool->nextpool;
- prev = pool->prevpool;
- next->prevpool = prev;
- prev->nextpool = next;
-
- /* Link the pool to freepools. This is a singly-linked
- * list, and pool->prevpool isn't used there.
- */
- ao = &arenas[pool->arenaindex];
- pool->nextpool = ao->freepools;
- ao->freepools = pool;
- nf = ++ao->nfreepools;
-
- /* All the rest is arena management. We just freed
- * a pool, and there are 4 cases for arena mgmt:
- * 1. If all the pools are free, return the arena to
- * the system free().
- * 2. If this is the only free pool in the arena,
- * add the arena back to the `usable_arenas` list.
- * 3. If the "next" arena has a smaller count of free
- * pools, we have to "slide this arena right" to
- * restore that usable_arenas is sorted in order of
- * nfreepools.
- * 4. Else there's nothing more to do.
- */
- if (nf == ao->ntotalpools) {
- /* Case 1. First unlink ao from usable_arenas.
- */
- assert(ao->prevarena == NULL ||
- ao->prevarena->address != 0);
- assert(ao ->nextarena == NULL ||
- ao->nextarena->address != 0);
-
- /* Fix the pointer in the prevarena, or the
- * usable_arenas pointer.
- */
- if (ao->prevarena == NULL) {
- usable_arenas = ao->nextarena;
- assert(usable_arenas == NULL ||
- usable_arenas->address != 0);
- }
- else {
- assert(ao->prevarena->nextarena == ao);
- ao->prevarena->nextarena =
- ao->nextarena;
- }
- /* Fix the pointer in the nextarena. */
- if (ao->nextarena != NULL) {
- assert(ao->nextarena->prevarena == ao);
- ao->nextarena->prevarena =
- ao->prevarena;
- }
- /* Record that this arena_object slot is
- * available to be reused.
- */
- ao->nextarena = unused_arena_objects;
- unused_arena_objects = ao;
-
- /* Free the entire arena. */
- free((void *)ao->address);
- ao->address = 0; /* mark unassociated */
- --narenas_currently_allocated;
-
- UNLOCK();
- return;
- }
- if (nf == 1) {
- /* Case 2. Put ao at the head of
- * usable_arenas. Note that because
- * ao->nfreepools was 0 before, ao isn't
- * currently on the usable_arenas list.
- */
- ao->nextarena = usable_arenas;
- ao->prevarena = NULL;
- if (usable_arenas)
- usable_arenas->prevarena = ao;
- usable_arenas = ao;
- assert(usable_arenas->address != 0);
-
- UNLOCK();
- return;
- }
- /* If this arena is now out of order, we need to keep
- * the list sorted. The list is kept sorted so that
- * the "most full" arenas are used first, which allows
- * the nearly empty arenas to be completely freed. In
- * a few un-scientific tests, it seems like this
- * approach allowed a lot more memory to be freed.
- */
- if (ao->nextarena == NULL ||
- nf <= ao->nextarena->nfreepools) {
- /* Case 4. Nothing to do. */
- UNLOCK();
- return;
- }
- /* Case 3: We have to move the arena towards the end
- * of the list, because it has more free pools than
- * the arena to its right.
- * First unlink ao from usable_arenas.
- */
- if (ao->prevarena != NULL) {
- /* ao isn't at the head of the list */
- assert(ao->prevarena->nextarena == ao);
- ao->prevarena->nextarena = ao->nextarena;
- }
- else {
- /* ao is at the head of the list */
- assert(usable_arenas == ao);
- usable_arenas = ao->nextarena;
- }
- ao->nextarena->prevarena = ao->prevarena;
-
- /* Locate the new insertion point by iterating over
- * the list, using our nextarena pointer.
- */
- while (ao->nextarena != NULL &&
- nf > ao->nextarena->nfreepools) {
- ao->prevarena = ao->nextarena;
- ao->nextarena = ao->nextarena->nextarena;
- }
-
- /* Insert ao at this point. */
- assert(ao->nextarena == NULL ||
- ao->prevarena == ao->nextarena->prevarena);
- assert(ao->prevarena->nextarena == ao->nextarena);
-
- ao->prevarena->nextarena = ao;
- if (ao->nextarena != NULL)
- ao->nextarena->prevarena = ao;
-
- /* Verify that the swaps worked. */
- assert(ao->nextarena == NULL ||
- nf <= ao->nextarena->nfreepools);
- assert(ao->prevarena == NULL ||
- nf > ao->prevarena->nfreepools);
- assert(ao->nextarena == NULL ||
- ao->nextarena->prevarena == ao);
- assert((usable_arenas == ao &&
- ao->prevarena == NULL) ||
- ao->prevarena->nextarena == ao);
-
- UNLOCK();
- return;
- }
- /* Pool was full, so doesn't currently live in any list:
- * link it to the front of the appropriate usedpools[] list.
- * This mimics LRU pool usage for new allocations and
- * targets optimal filling when several pools contain
- * blocks of the same size class.
- */
- --pool->ref.count;
- assert(pool->ref.count > 0); /* else the pool is empty */
- size = pool->szidx;
- next = usedpools[size + size];
- prev = next->prevpool;
- /* insert pool before next: prev <-> pool <-> next */
- pool->nextpool = next;
- pool->prevpool = prev;
- next->prevpool = pool;
- prev->nextpool = pool;
- UNLOCK();
- return;
- }
-
- /* We didn't allocate this address. */
- free(p);
+ poolp pool;
+ block *lastfree;
+ poolp next, prev;
+ uint size;
+
+ if (p == NULL) /* free(NULL) has no effect */
+ return;
+
+ pool = POOL_ADDR(p);
+ if (Py_ADDRESS_IN_RANGE(p, pool)) {
+ /* We allocated this address. */
+ LOCK();
+ /* Link p to the start of the pool's freeblock list. Since
+ * the pool had at least the p block outstanding, the pool
+ * wasn't empty (so it's already in a usedpools[] list, or
+ * was full and is in no list -- it's not in the freeblocks
+ * list in any case).
+ */
+ assert(pool->ref.count > 0); /* else it was empty */
+ *(block **)p = lastfree = pool->freeblock;
+ pool->freeblock = (block *)p;
+ if (lastfree) {
+ struct arena_object* ao;
+ uint nf; /* ao->nfreepools */
+
+ /* freeblock wasn't NULL, so the pool wasn't full,
+ * and the pool is in a usedpools[] list.
+ */
+ if (--pool->ref.count != 0) {
+ /* pool isn't empty: leave it in usedpools */
+ UNLOCK();
+ return;
+ }
+ /* Pool is now empty: unlink from usedpools, and
+ * link to the front of freepools. This ensures that
+ * previously freed pools will be allocated later
+ * (being not referenced, they are perhaps paged out).
+ */
+ next = pool->nextpool;
+ prev = pool->prevpool;
+ next->prevpool = prev;
+ prev->nextpool = next;
+
+ /* Link the pool to freepools. This is a singly-linked
+ * list, and pool->prevpool isn't used there.
+ */
+ ao = &arenas[pool->arenaindex];
+ pool->nextpool = ao->freepools;
+ ao->freepools = pool;
+ nf = ++ao->nfreepools;
+
+ /* All the rest is arena management. We just freed
+ * a pool, and there are 4 cases for arena mgmt:
+ * 1. If all the pools are free, return the arena to
+ * the system free().
+ * 2. If this is the only free pool in the arena,
+ * add the arena back to the `usable_arenas` list.
+ * 3. If the "next" arena has a smaller count of free
+ * pools, we have to "slide this arena right" to
+ * restore that usable_arenas is sorted in order of
+ * nfreepools.
+ * 4. Else there's nothing more to do.
+ */
+ if (nf == ao->ntotalpools) {
+ /* Case 1. First unlink ao from usable_arenas.
+ */
+ assert(ao->prevarena == NULL ||
+ ao->prevarena->address != 0);
+ assert(ao ->nextarena == NULL ||
+ ao->nextarena->address != 0);
+
+ /* Fix the pointer in the prevarena, or the
+ * usable_arenas pointer.
+ */
+ if (ao->prevarena == NULL) {
+ usable_arenas = ao->nextarena;
+ assert(usable_arenas == NULL ||
+ usable_arenas->address != 0);
+ }
+ else {
+ assert(ao->prevarena->nextarena == ao);
+ ao->prevarena->nextarena =
+ ao->nextarena;
+ }
+ /* Fix the pointer in the nextarena. */
+ if (ao->nextarena != NULL) {
+ assert(ao->nextarena->prevarena == ao);
+ ao->nextarena->prevarena =
+ ao->prevarena;
+ }
+ /* Record that this arena_object slot is
+ * available to be reused.
+ */
+ ao->nextarena = unused_arena_objects;
+ unused_arena_objects = ao;
+
+ /* Free the entire arena. */
+ free((void *)ao->address);
+ ao->address = 0; /* mark unassociated */
+ --narenas_currently_allocated;
+
+ UNLOCK();
+ return;
+ }
+ if (nf == 1) {
+ /* Case 2. Put ao at the head of
+ * usable_arenas. Note that because
+ * ao->nfreepools was 0 before, ao isn't
+ * currently on the usable_arenas list.
+ */
+ ao->nextarena = usable_arenas;
+ ao->prevarena = NULL;
+ if (usable_arenas)
+ usable_arenas->prevarena = ao;
+ usable_arenas = ao;
+ assert(usable_arenas->address != 0);
+
+ UNLOCK();
+ return;
+ }
+ /* If this arena is now out of order, we need to keep
+ * the list sorted. The list is kept sorted so that
+ * the "most full" arenas are used first, which allows
+ * the nearly empty arenas to be completely freed. In
+ * a few un-scientific tests, it seems like this
+ * approach allowed a lot more memory to be freed.
+ */
+ if (ao->nextarena == NULL ||
+ nf <= ao->nextarena->nfreepools) {
+ /* Case 4. Nothing to do. */
+ UNLOCK();
+ return;
+ }
+ /* Case 3: We have to move the arena towards the end
+ * of the list, because it has more free pools than
+ * the arena to its right.
+ * First unlink ao from usable_arenas.
+ */
+ if (ao->prevarena != NULL) {
+ /* ao isn't at the head of the list */
+ assert(ao->prevarena->nextarena == ao);
+ ao->prevarena->nextarena = ao->nextarena;
+ }
+ else {
+ /* ao is at the head of the list */
+ assert(usable_arenas == ao);
+ usable_arenas = ao->nextarena;
+ }
+ ao->nextarena->prevarena = ao->prevarena;
+
+ /* Locate the new insertion point by iterating over
+ * the list, using our nextarena pointer.
+ */
+ while (ao->nextarena != NULL &&
+ nf > ao->nextarena->nfreepools) {
+ ao->prevarena = ao->nextarena;
+ ao->nextarena = ao->nextarena->nextarena;
+ }
+
+ /* Insert ao at this point. */
+ assert(ao->nextarena == NULL ||
+ ao->prevarena == ao->nextarena->prevarena);
+ assert(ao->prevarena->nextarena == ao->nextarena);
+
+ ao->prevarena->nextarena = ao;
+ if (ao->nextarena != NULL)
+ ao->nextarena->prevarena = ao;
+
+ /* Verify that the swaps worked. */
+ assert(ao->nextarena == NULL ||
+ nf <= ao->nextarena->nfreepools);
+ assert(ao->prevarena == NULL ||
+ nf > ao->prevarena->nfreepools);
+ assert(ao->nextarena == NULL ||
+ ao->nextarena->prevarena == ao);
+ assert((usable_arenas == ao &&
+ ao->prevarena == NULL) ||
+ ao->prevarena->nextarena == ao);
+
+ UNLOCK();
+ return;
+ }
+ /* Pool was full, so doesn't currently live in any list:
+ * link it to the front of the appropriate usedpools[] list.
+ * This mimics LRU pool usage for new allocations and
+ * targets optimal filling when several pools contain
+ * blocks of the same size class.
+ */
+ --pool->ref.count;
+ assert(pool->ref.count > 0); /* else the pool is empty */
+ size = pool->szidx;
+ next = usedpools[size + size];
+ prev = next->prevpool;
+ /* insert pool before next: prev <-> pool <-> next */
+ pool->nextpool = next;
+ pool->prevpool = prev;
+ next->prevpool = pool;
+ prev->nextpool = pool;
+ UNLOCK();
+ return;
+ }
+
+ /* We didn't allocate this address. */
+ free(p);
}
/* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0,
@@ -1134,72 +1134,72 @@ PyObject_Free(void *p)
void *
PyObject_Realloc(void *p, size_t nbytes)
{
- void *bp;
- poolp pool;
- size_t size;
-
- if (p == NULL)
- return PyObject_Malloc(nbytes);
-
- /*
- * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
- * Most python internals blindly use a signed Py_ssize_t to track
- * things without checking for overflows or negatives.
- * As size_t is unsigned, checking for nbytes < 0 is not required.
- */
- if (nbytes > PY_SSIZE_T_MAX)
- return NULL;
-
- pool = POOL_ADDR(p);
- if (Py_ADDRESS_IN_RANGE(p, pool)) {
- /* We're in charge of this block */
- size = INDEX2SIZE(pool->szidx);
- if (nbytes <= size) {
- /* The block is staying the same or shrinking. If
- * it's shrinking, there's a tradeoff: it costs
- * cycles to copy the block to a smaller size class,
- * but it wastes memory not to copy it. The
- * compromise here is to copy on shrink only if at
- * least 25% of size can be shaved off.
- */
- if (4 * nbytes > 3 * size) {
- /* It's the same,
- * or shrinking and new/old > 3/4.
- */
- return p;
- }
- size = nbytes;
- }
- bp = PyObject_Malloc(nbytes);
- if (bp != NULL) {
- memcpy(bp, p, size);
- PyObject_Free(p);
- }
- return bp;
- }
- /* We're not managing this block. If nbytes <=
- * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this
- * block. However, if we do, we need to copy the valid data from
- * the C-managed block to one of our blocks, and there's no portable
- * way to know how much of the memory space starting at p is valid.
- * As bug 1185883 pointed out the hard way, it's possible that the
- * C-managed block is "at the end" of allocated VM space, so that
- * a memory fault can occur if we try to copy nbytes bytes starting
- * at p. Instead we punt: let C continue to manage this block.
- */
- if (nbytes)
- return realloc(p, nbytes);
- /* C doesn't define the result of realloc(p, 0) (it may or may not
- * return NULL then), but Python's docs promise that nbytes==0 never
- * returns NULL. We don't pass 0 to realloc(), to avoid that endcase
- * to begin with. Even then, we can't be sure that realloc() won't
- * return NULL.
- */
- bp = realloc(p, 1);
- return bp ? bp : p;
+ void *bp;
+ poolp pool;
+ size_t size;
+
+ if (p == NULL)
+ return PyObject_Malloc(nbytes);
+
+ /*
+ * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
+ * Most python internals blindly use a signed Py_ssize_t to track
+ * things without checking for overflows or negatives.
+ * As size_t is unsigned, checking for nbytes < 0 is not required.
+ */
+ if (nbytes > PY_SSIZE_T_MAX)
+ return NULL;
+
+ pool = POOL_ADDR(p);
+ if (Py_ADDRESS_IN_RANGE(p, pool)) {
+ /* We're in charge of this block */
+ size = INDEX2SIZE(pool->szidx);
+ if (nbytes <= size) {
+ /* The block is staying the same or shrinking. If
+ * it's shrinking, there's a tradeoff: it costs
+ * cycles to copy the block to a smaller size class,
+ * but it wastes memory not to copy it. The
+ * compromise here is to copy on shrink only if at
+ * least 25% of size can be shaved off.
+ */
+ if (4 * nbytes > 3 * size) {
+ /* It's the same,
+ * or shrinking and new/old > 3/4.
+ */
+ return p;
+ }
+ size = nbytes;
+ }
+ bp = PyObject_Malloc(nbytes);
+ if (bp != NULL) {
+ memcpy(bp, p, size);
+ PyObject_Free(p);
+ }
+ return bp;
+ }
+ /* We're not managing this block. If nbytes <=
+ * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this
+ * block. However, if we do, we need to copy the valid data from
+ * the C-managed block to one of our blocks, and there's no portable
+ * way to know how much of the memory space starting at p is valid.
+ * As bug 1185883 pointed out the hard way, it's possible that the
+ * C-managed block is "at the end" of allocated VM space, so that
+ * a memory fault can occur if we try to copy nbytes bytes starting
+ * at p. Instead we punt: let C continue to manage this block.
+ */
+ if (nbytes)
+ return realloc(p, nbytes);
+ /* C doesn't define the result of realloc(p, 0) (it may or may not
+ * return NULL then), but Python's docs promise that nbytes==0 never
+ * returns NULL. We don't pass 0 to realloc(), to avoid that endcase
+ * to begin with. Even then, we can't be sure that realloc() won't
+ * return NULL.
+ */
+ bp = realloc(p, 1);
+ return bp ? bp : p;
}
-#else /* ! WITH_PYMALLOC */
+#else /* ! WITH_PYMALLOC */
/*==========================================================================*/
/* pymalloc not enabled: Redirect the entry points to malloc. These will
@@ -1208,19 +1208,19 @@ PyObject_Realloc(void *p, size_t nbytes)
void *
PyObject_Malloc(size_t n)
{
- return PyMem_MALLOC(n);
+ return PyMem_MALLOC(n);
}
void *
PyObject_Realloc(void *p, size_t n)
{
- return PyMem_REALLOC(p, n);
+ return PyMem_REALLOC(p, n);
}
void
PyObject_Free(void *p)
{
- PyMem_FREE(p);
+ PyMem_FREE(p);
}
#endif /* WITH_PYMALLOC */
@@ -1241,7 +1241,7 @@ PyObject_Free(void *p)
#define DEADBYTE 0xDB /* dead (newly freed) memory */
#define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */
-static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
+static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
/* serialno is always incremented via calling this routine. The point is
* to supply a single place to set a breakpoint.
@@ -1249,7 +1249,7 @@ static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
static void
bumpserialno(void)
{
- ++serialno;
+ ++serialno;
}
#define SST SIZEOF_SIZE_T
@@ -1258,13 +1258,13 @@ bumpserialno(void)
static size_t
read_size_t(const void *p)
{
- const uchar *q = (const uchar *)p;
- size_t result = *q++;
- int i;
+ const uchar *q = (const uchar *)p;
+ size_t result = *q++;
+ int i;
- for (i = SST; --i > 0; ++q)
- result = (result << 8) | *q;
- return result;
+ for (i = SST; --i > 0; ++q)
+ result = (result << 8) | *q;
+ return result;
}
/* Write n as a big-endian size_t, MSB at address p, LSB at
@@ -1273,13 +1273,13 @@ read_size_t(const void *p)
static void
write_size_t(void *p, size_t n)
{
- uchar *q = (uchar *)p + SST - 1;
- int i;
+ uchar *q = (uchar *)p + SST - 1;
+ int i;
- for (i = SST; --i >= 0; --q) {
- *q = (uchar)(n & 0xff);
- n >>= 8;
- }
+ for (i = SST; --i >= 0; --q) {
+ *q = (uchar)(n & 0xff);
+ n >>= 8;
+ }
}
#ifdef Py_DEBUG
@@ -1290,22 +1290,22 @@ write_size_t(void *p, size_t n)
static int
pool_is_in_list(const poolp target, poolp list)
{
- poolp origlist = list;
- assert(target != NULL);
- if (list == NULL)
- return 0;
- do {
- if (target == list)
- return 1;
- list = list->nextpool;
- } while (list != NULL && list != origlist);
- return 0;
+ poolp origlist = list;
+ assert(target != NULL);
+ if (list == NULL)
+ return 0;
+ do {
+ if (target == list)
+ return 1;
+ list = list->nextpool;
+ } while (list != NULL && list != origlist);
+ return 0;
}
#else
#define pool_is_in_list(X, Y) 1
-#endif /* Py_DEBUG */
+#endif /* Py_DEBUG */
/* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and
fills them with useful stuff, here calling the underlying malloc's result p:
@@ -1334,31 +1334,31 @@ p[2*S+n+S: 2*S+n+2*S]
void *
_PyObject_DebugMalloc(size_t nbytes)
{
- uchar *p; /* base address of malloc'ed block */
- uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */
- size_t total; /* nbytes + 4*SST */
+ uchar *p; /* base address of malloc'ed block */
+ uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */
+ size_t total; /* nbytes + 4*SST */
- bumpserialno();
- total = nbytes + 4*SST;
- if (total < nbytes)
- /* overflow: can't represent total as a size_t */
- return NULL;
+ bumpserialno();
+ total = nbytes + 4*SST;
+ if (total < nbytes)
+ /* overflow: can't represent total as a size_t */
+ return NULL;
- p = (uchar *)PyObject_Malloc(total);
- if (p == NULL)
- return NULL;
+ p = (uchar *)PyObject_Malloc(total);
+ if (p == NULL)
+ return NULL;
- write_size_t(p, nbytes);
- memset(p + SST, FORBIDDENBYTE, SST);
+ write_size_t(p, nbytes);
+ memset(p + SST, FORBIDDENBYTE, SST);
- if (nbytes > 0)
- memset(p + 2*SST, CLEANBYTE, nbytes);
+ if (nbytes > 0)
+ memset(p + 2*SST, CLEANBYTE, nbytes);
- tail = p + 2*SST + nbytes;
- memset(tail, FORBIDDENBYTE, SST);
- write_size_t(tail + SST, serialno);
+ tail = p + 2*SST + nbytes;
+ memset(tail, FORBIDDENBYTE, SST);
+ write_size_t(tail + SST, serialno);
- return p + 2*SST;
+ return p + 2*SST;
}
/* The debug free first checks the 2*SST bytes on each end for sanity (in
@@ -1369,63 +1369,63 @@ _PyObject_DebugMalloc(size_t nbytes)
void
_PyObject_DebugFree(void *p)
{
- uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */
- size_t nbytes;
-
- if (p == NULL)
- return;
- _PyObject_DebugCheckAddress(p);
- nbytes = read_size_t(q);
- if (nbytes > 0)
- memset(q, DEADBYTE, nbytes);
- PyObject_Free(q);
+ uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */
+ size_t nbytes;
+
+ if (p == NULL)
+ return;
+ _PyObject_DebugCheckAddress(p);
+ nbytes = read_size_t(q);
+ if (nbytes > 0)
+ memset(q, DEADBYTE, nbytes);
+ PyObject_Free(q);
}
void *
_PyObject_DebugRealloc(void *p, size_t nbytes)
{
- uchar *q = (uchar *)p;
- uchar *tail;
- size_t total; /* nbytes + 4*SST */
- size_t original_nbytes;
- int i;
-
- if (p == NULL)
- return _PyObject_DebugMalloc(nbytes);
-
- _PyObject_DebugCheckAddress(p);
- bumpserialno();
- original_nbytes = read_size_t(q - 2*SST);
- total = nbytes + 4*SST;
- if (total < nbytes)
- /* overflow: can't represent total as a size_t */
- return NULL;
-
- if (nbytes < original_nbytes) {
- /* shrinking: mark old extra memory dead */
- memset(q + nbytes, DEADBYTE, original_nbytes - nbytes);
- }
-
- /* Resize and add decorations. */
- q = (uchar *)PyObject_Realloc(q - 2*SST, total);
- if (q == NULL)
- return NULL;
-
- write_size_t(q, nbytes);
- for (i = 0; i < SST; ++i)
- assert(q[SST + i] == FORBIDDENBYTE);
- q += 2*SST;
- tail = q + nbytes;
- memset(tail, FORBIDDENBYTE, SST);
- write_size_t(tail + SST, serialno);
-
- if (nbytes > original_nbytes) {
- /* growing: mark new extra memory clean */
- memset(q + original_nbytes, CLEANBYTE,
- nbytes - original_nbytes);
- }
-
- return q;
+ uchar *q = (uchar *)p;
+ uchar *tail;
+ size_t total; /* nbytes + 4*SST */
+ size_t original_nbytes;
+ int i;
+
+ if (p == NULL)
+ return _PyObject_DebugMalloc(nbytes);
+
+ _PyObject_DebugCheckAddress(p);
+ bumpserialno();
+ original_nbytes = read_size_t(q - 2*SST);
+ total = nbytes + 4*SST;
+ if (total < nbytes)
+ /* overflow: can't represent total as a size_t */
+ return NULL;
+
+ if (nbytes < original_nbytes) {
+ /* shrinking: mark old extra memory dead */
+ memset(q + nbytes, DEADBYTE, original_nbytes - nbytes);
+ }
+
+ /* Resize and add decorations. */
+ q = (uchar *)PyObject_Realloc(q - 2*SST, total);
+ if (q == NULL)
+ return NULL;
+
+ write_size_t(q, nbytes);
+ for (i = 0; i < SST; ++i)
+ assert(q[SST + i] == FORBIDDENBYTE);
+ q += 2*SST;
+ tail = q + nbytes;
+ memset(tail, FORBIDDENBYTE, SST);
+ write_size_t(tail + SST, serialno);
+
+ if (nbytes > original_nbytes) {
+ /* growing: mark new extra memory clean */
+ memset(q + original_nbytes, CLEANBYTE,
+ nbytes - original_nbytes);
+ }
+
+ return q;
}
/* Check the forbidden bytes on both ends of the memory allocated for p.
@@ -1435,176 +1435,176 @@ _PyObject_DebugRealloc(void *p, size_t nbytes)
void
_PyObject_DebugCheckAddress(const void *p)
{
- const uchar *q = (const uchar *)p;
- char *msg;
- size_t nbytes;
- const uchar *tail;
- int i;
-
- if (p == NULL) {
- msg = "didn't expect a NULL pointer";
- goto error;
- }
-
- /* Check the stuff at the start of p first: if there's underwrite
- * corruption, the number-of-bytes field may be nuts, and checking
- * the tail could lead to a segfault then.
- */
- for (i = SST; i >= 1; --i) {
- if (*(q-i) != FORBIDDENBYTE) {
- msg = "bad leading pad byte";
- goto error;
- }
- }
-
- nbytes = read_size_t(q - 2*SST);
- tail = q + nbytes;
- for (i = 0; i < SST; ++i) {
- if (tail[i] != FORBIDDENBYTE) {
- msg = "bad trailing pad byte";
- goto error;
- }
- }
-
- return;
+ const uchar *q = (const uchar *)p;
+ char *msg;
+ size_t nbytes;
+ const uchar *tail;
+ int i;
+
+ if (p == NULL) {
+ msg = "didn't expect a NULL pointer";
+ goto error;
+ }
+
+ /* Check the stuff at the start of p first: if there's underwrite
+ * corruption, the number-of-bytes field may be nuts, and checking
+ * the tail could lead to a segfault then.
+ */
+ for (i = SST; i >= 1; --i) {
+ if (*(q-i) != FORBIDDENBYTE) {
+ msg = "bad leading pad byte";
+ goto error;
+ }
+ }
+
+ nbytes = read_size_t(q - 2*SST);
+ tail = q + nbytes;
+ for (i = 0; i < SST; ++i) {
+ if (tail[i] != FORBIDDENBYTE) {
+ msg = "bad trailing pad byte";
+ goto error;
+ }
+ }
+
+ return;
error:
- _PyObject_DebugDumpAddress(p);
- Py_FatalError(msg);
+ _PyObject_DebugDumpAddress(p);
+ Py_FatalError(msg);
}
/* Display info to stderr about the memory block at p. */
void
_PyObject_DebugDumpAddress(const void *p)
{
- const uchar *q = (const uchar *)p;
- const uchar *tail;
- size_t nbytes, serial;
- int i;
- int ok;
-
- fprintf(stderr, "Debug memory block at address p=%p:\n", p);
- if (p == NULL)
- return;
-
- nbytes = read_size_t(q - 2*SST);
- fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally "
- "requested\n", nbytes);
-
- /* In case this is nuts, check the leading pad bytes first. */
- fprintf(stderr, " The %d pad bytes at p-%d are ", SST, SST);
- ok = 1;
- for (i = 1; i <= SST; ++i) {
- if (*(q-i) != FORBIDDENBYTE) {
- ok = 0;
- break;
- }
- }
- if (ok)
- fputs("FORBIDDENBYTE, as expected.\n", stderr);
- else {
- fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
- FORBIDDENBYTE);
- for (i = SST; i >= 1; --i) {
- const uchar byte = *(q-i);
- fprintf(stderr, " at p-%d: 0x%02x", i, byte);
- if (byte != FORBIDDENBYTE)
- fputs(" *** OUCH", stderr);
- fputc('\n', stderr);
- }
-
- fputs(" Because memory is corrupted at the start, the "
- "count of bytes requested\n"
- " may be bogus, and checking the trailing pad "
- "bytes may segfault.\n", stderr);
- }
-
- tail = q + nbytes;
- fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail);
- ok = 1;
- for (i = 0; i < SST; ++i) {
- if (tail[i] != FORBIDDENBYTE) {
- ok = 0;
- break;
- }
- }
- if (ok)
- fputs("FORBIDDENBYTE, as expected.\n", stderr);
- else {
- fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
- FORBIDDENBYTE);
- for (i = 0; i < SST; ++i) {
- const uchar byte = tail[i];
- fprintf(stderr, " at tail+%d: 0x%02x",
- i, byte);
- if (byte != FORBIDDENBYTE)
- fputs(" *** OUCH", stderr);
- fputc('\n', stderr);
- }
- }
-
- serial = read_size_t(tail + SST);
- fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T
- "u to debug malloc/realloc.\n", serial);
-
- if (nbytes > 0) {
- i = 0;
- fputs(" Data at p:", stderr);
- /* print up to 8 bytes at the start */
- while (q < tail && i < 8) {
- fprintf(stderr, " %02x", *q);
- ++i;
- ++q;
- }
- /* and up to 8 at the end */
- if (q < tail) {
- if (tail - q > 8) {
- fputs(" ...", stderr);
- q = tail - 8;
- }
- while (q < tail) {
- fprintf(stderr, " %02x", *q);
- ++q;
- }
- }
- fputc('\n', stderr);
- }
+ const uchar *q = (const uchar *)p;
+ const uchar *tail;
+ size_t nbytes, serial;
+ int i;
+ int ok;
+
+ fprintf(stderr, "Debug memory block at address p=%p:\n", p);
+ if (p == NULL)
+ return;
+
+ nbytes = read_size_t(q - 2*SST);
+ fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally "
+ "requested\n", nbytes);
+
+ /* In case this is nuts, check the leading pad bytes first. */
+ fprintf(stderr, " The %d pad bytes at p-%d are ", SST, SST);
+ ok = 1;
+ for (i = 1; i <= SST; ++i) {
+ if (*(q-i) != FORBIDDENBYTE) {
+ ok = 0;
+ break;
+ }
+ }
+ if (ok)
+ fputs("FORBIDDENBYTE, as expected.\n", stderr);
+ else {
+ fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
+ FORBIDDENBYTE);
+ for (i = SST; i >= 1; --i) {
+ const uchar byte = *(q-i);
+ fprintf(stderr, " at p-%d: 0x%02x", i, byte);
+ if (byte != FORBIDDENBYTE)
+ fputs(" *** OUCH", stderr);
+ fputc('\n', stderr);
+ }
+
+ fputs(" Because memory is corrupted at the start, the "
+ "count of bytes requested\n"
+ " may be bogus, and checking the trailing pad "
+ "bytes may segfault.\n", stderr);
+ }
+
+ tail = q + nbytes;
+ fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail);
+ ok = 1;
+ for (i = 0; i < SST; ++i) {
+ if (tail[i] != FORBIDDENBYTE) {
+ ok = 0;
+ break;
+ }
+ }
+ if (ok)
+ fputs("FORBIDDENBYTE, as expected.\n", stderr);
+ else {
+ fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
+ FORBIDDENBYTE);
+ for (i = 0; i < SST; ++i) {
+ const uchar byte = tail[i];
+ fprintf(stderr, " at tail+%d: 0x%02x",
+ i, byte);
+ if (byte != FORBIDDENBYTE)
+ fputs(" *** OUCH", stderr);
+ fputc('\n', stderr);
+ }
+ }
+
+ serial = read_size_t(tail + SST);
+ fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T
+ "u to debug malloc/realloc.\n", serial);
+
+ if (nbytes > 0) {
+ i = 0;
+ fputs(" Data at p:", stderr);
+ /* print up to 8 bytes at the start */
+ while (q < tail && i < 8) {
+ fprintf(stderr, " %02x", *q);
+ ++i;
+ ++q;
+ }
+ /* and up to 8 at the end */
+ if (q < tail) {
+ if (tail - q > 8) {
+ fputs(" ...", stderr);
+ q = tail - 8;
+ }
+ while (q < tail) {
+ fprintf(stderr, " %02x", *q);
+ ++q;
+ }
+ }
+ fputc('\n', stderr);
+ }
}
static size_t
printone(const char* msg, size_t value)
{
- int i, k;
- char buf[100];
- size_t origvalue = value;
-
- fputs(msg, stderr);
- for (i = (int)strlen(msg); i < 35; ++i)
- fputc(' ', stderr);
- fputc('=', stderr);
-
- /* Write the value with commas. */
- i = 22;
- buf[i--] = '\0';
- buf[i--] = '\n';
- k = 3;
- do {
- size_t nextvalue = value / 10;
- uint digit = (uint)(value - nextvalue * 10);
- value = nextvalue;
- buf[i--] = (char)(digit + '0');
- --k;
- if (k == 0 && value && i >= 0) {
- k = 3;
- buf[i--] = ',';
- }
- } while (value && i >= 0);
-
- while (i >= 0)
- buf[i--] = ' ';
- fputs(buf, stderr);
-
- return origvalue;
+ int i, k;
+ char buf[100];
+ size_t origvalue = value;
+
+ fputs(msg, stderr);
+ for (i = (int)strlen(msg); i < 35; ++i)
+ fputc(' ', stderr);
+ fputc('=', stderr);
+
+ /* Write the value with commas. */
+ i = 22;
+ buf[i--] = '\0';
+ buf[i--] = '\n';
+ k = 3;
+ do {
+ size_t nextvalue = value / 10;
+ uint digit = (uint)(value - nextvalue * 10);
+ value = nextvalue;
+ buf[i--] = (char)(digit + '0');
+ --k;
+ if (k == 0 && value && i >= 0) {
+ k = 3;
+ buf[i--] = ',';
+ }
+ } while (value && i >= 0);
+
+ while (i >= 0)
+ buf[i--] = ' ';
+ fputs(buf, stderr);
+
+ return origvalue;
}
/* Print summary info to stderr about the state of pymalloc's structures.
@@ -1614,142 +1614,142 @@ printone(const char* msg, size_t value)
void
_PyObject_DebugMallocStats(void)
{
- uint i;
- const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
- /* # of pools, allocated blocks, and free blocks per class index */
- size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
- size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
- size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
- /* total # of allocated bytes in used and full pools */
- size_t allocated_bytes = 0;
- /* total # of available bytes in used pools */
- size_t available_bytes = 0;
- /* # of free pools + pools not yet carved out of current arena */
- uint numfreepools = 0;
- /* # of bytes for arena alignment padding */
- size_t arena_alignment = 0;
- /* # of bytes in used and full pools used for pool_headers */
- size_t pool_header_bytes = 0;
- /* # of bytes in used and full pools wasted due to quantization,
- * i.e. the necessarily leftover space at the ends of used and
- * full pools.
- */
- size_t quantization = 0;
- /* # of arenas actually allocated. */
- size_t narenas = 0;
- /* running total -- should equal narenas * ARENA_SIZE */
- size_t total;
- char buf[128];
-
- fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
- SMALL_REQUEST_THRESHOLD, numclasses);
-
- for (i = 0; i < numclasses; ++i)
- numpools[i] = numblocks[i] = numfreeblocks[i] = 0;
-
- /* Because full pools aren't linked to from anything, it's easiest
- * to march over all the arenas. If we're lucky, most of the memory
- * will be living in full pools -- would be a shame to miss them.
- */
- for (i = 0; i < maxarenas; ++i) {
- uint poolsinarena;
- uint j;
- uptr base = arenas[i].address;
-
- /* Skip arenas which are not allocated. */
- if (arenas[i].address == (uptr)NULL)
- continue;
- narenas += 1;
-
- poolsinarena = arenas[i].ntotalpools;
- numfreepools += arenas[i].nfreepools;
-
- /* round up to pool alignment */
- if (base & (uptr)POOL_SIZE_MASK) {
- arena_alignment += POOL_SIZE;
- base &= ~(uptr)POOL_SIZE_MASK;
- base += POOL_SIZE;
- }
-
- /* visit every pool in the arena */
- assert(base <= (uptr) arenas[i].pool_address);
- for (j = 0;
- base < (uptr) arenas[i].pool_address;
- ++j, base += POOL_SIZE) {
- poolp p = (poolp)base;
- const uint sz = p->szidx;
- uint freeblocks;
-
- if (p->ref.count == 0) {
- /* currently unused */
- assert(pool_is_in_list(p, arenas[i].freepools));
- continue;
- }
- ++numpools[sz];
- numblocks[sz] += p->ref.count;
- freeblocks = NUMBLOCKS(sz) - p->ref.count;
- numfreeblocks[sz] += freeblocks;
+ uint i;
+ const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
+ /* # of pools, allocated blocks, and free blocks per class index */
+ size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
+ size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
+ size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
+ /* total # of allocated bytes in used and full pools */
+ size_t allocated_bytes = 0;
+ /* total # of available bytes in used pools */
+ size_t available_bytes = 0;
+ /* # of free pools + pools not yet carved out of current arena */
+ uint numfreepools = 0;
+ /* # of bytes for arena alignment padding */
+ size_t arena_alignment = 0;
+ /* # of bytes in used and full pools used for pool_headers */
+ size_t pool_header_bytes = 0;
+ /* # of bytes in used and full pools wasted due to quantization,
+ * i.e. the necessarily leftover space at the ends of used and
+ * full pools.
+ */
+ size_t quantization = 0;
+ /* # of arenas actually allocated. */
+ size_t narenas = 0;
+ /* running total -- should equal narenas * ARENA_SIZE */
+ size_t total;
+ char buf[128];
+
+ fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
+ SMALL_REQUEST_THRESHOLD, numclasses);
+
+ for (i = 0; i < numclasses; ++i)
+ numpools[i] = numblocks[i] = numfreeblocks[i] = 0;
+
+ /* Because full pools aren't linked to from anything, it's easiest
+ * to march over all the arenas. If we're lucky, most of the memory
+ * will be living in full pools -- would be a shame to miss them.
+ */
+ for (i = 0; i < maxarenas; ++i) {
+ uint poolsinarena;
+ uint j;
+ uptr base = arenas[i].address;
+
+ /* Skip arenas which are not allocated. */
+ if (arenas[i].address == (uptr)NULL)
+ continue;
+ narenas += 1;
+
+ poolsinarena = arenas[i].ntotalpools;
+ numfreepools += arenas[i].nfreepools;
+
+ /* round up to pool alignment */
+ if (base & (uptr)POOL_SIZE_MASK) {
+ arena_alignment += POOL_SIZE;
+ base &= ~(uptr)POOL_SIZE_MASK;
+ base += POOL_SIZE;
+ }
+
+ /* visit every pool in the arena */
+ assert(base <= (uptr) arenas[i].pool_address);
+ for (j = 0;
+ base < (uptr) arenas[i].pool_address;
+ ++j, base += POOL_SIZE) {
+ poolp p = (poolp)base;
+ const uint sz = p->szidx;
+ uint freeblocks;
+
+ if (p->ref.count == 0) {
+ /* currently unused */
+ assert(pool_is_in_list(p, arenas[i].freepools));
+ continue;
+ }
+ ++numpools[sz];
+ numblocks[sz] += p->ref.count;
+ freeblocks = NUMBLOCKS(sz) - p->ref.count;
+ numfreeblocks[sz] += freeblocks;
#ifdef Py_DEBUG
- if (freeblocks > 0)
- assert(pool_is_in_list(p, usedpools[sz + sz]));
+ if (freeblocks > 0)
+ assert(pool_is_in_list(p, usedpools[sz + sz]));
#endif
- }
- }
- assert(narenas == narenas_currently_allocated);
-
- fputc('\n', stderr);
- fputs("class size num pools blocks in use avail blocks\n"
- "----- ---- --------- ------------- ------------\n",
- stderr);
-
- for (i = 0; i < numclasses; ++i) {
- size_t p = numpools[i];
- size_t b = numblocks[i];
- size_t f = numfreeblocks[i];
- uint size = INDEX2SIZE(i);
- if (p == 0) {
- assert(b == 0 && f == 0);
- continue;
- }
- fprintf(stderr, "%5u %6u "
- "%11" PY_FORMAT_SIZE_T "u "
- "%15" PY_FORMAT_SIZE_T "u "
- "%13" PY_FORMAT_SIZE_T "u\n",
- i, size, p, b, f);
- allocated_bytes += b * size;
- available_bytes += f * size;
- pool_header_bytes += p * POOL_OVERHEAD;
- quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
- }
- fputc('\n', stderr);
- (void)printone("# times object malloc called", serialno);
-
- (void)printone("# arenas allocated total", ntimes_arena_allocated);
- (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
- (void)printone("# arenas highwater mark", narenas_highwater);
- (void)printone("# arenas allocated current", narenas);
-
- PyOS_snprintf(buf, sizeof(buf),
- "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
- narenas, ARENA_SIZE);
- (void)printone(buf, narenas * ARENA_SIZE);
-
- fputc('\n', stderr);
-
- total = printone("# bytes in allocated blocks", allocated_bytes);
- total += printone("# bytes in available blocks", available_bytes);
-
- PyOS_snprintf(buf, sizeof(buf),
- "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
- total += printone(buf, (size_t)numfreepools * POOL_SIZE);
-
- total += printone("# bytes lost to pool headers", pool_header_bytes);
- total += printone("# bytes lost to quantization", quantization);
- total += printone("# bytes lost to arena alignment", arena_alignment);
- (void)printone("Total", total);
+ }
+ }
+ assert(narenas == narenas_currently_allocated);
+
+ fputc('\n', stderr);
+ fputs("class size num pools blocks in use avail blocks\n"
+ "----- ---- --------- ------------- ------------\n",
+ stderr);
+
+ for (i = 0; i < numclasses; ++i) {
+ size_t p = numpools[i];
+ size_t b = numblocks[i];
+ size_t f = numfreeblocks[i];
+ uint size = INDEX2SIZE(i);
+ if (p == 0) {
+ assert(b == 0 && f == 0);
+ continue;
+ }
+ fprintf(stderr, "%5u %6u "
+ "%11" PY_FORMAT_SIZE_T "u "
+ "%15" PY_FORMAT_SIZE_T "u "
+ "%13" PY_FORMAT_SIZE_T "u\n",
+ i, size, p, b, f);
+ allocated_bytes += b * size;
+ available_bytes += f * size;
+ pool_header_bytes += p * POOL_OVERHEAD;
+ quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
+ }
+ fputc('\n', stderr);
+ (void)printone("# times object malloc called", serialno);
+
+ (void)printone("# arenas allocated total", ntimes_arena_allocated);
+ (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
+ (void)printone("# arenas highwater mark", narenas_highwater);
+ (void)printone("# arenas allocated current", narenas);
+
+ PyOS_snprintf(buf, sizeof(buf),
+ "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
+ narenas, ARENA_SIZE);
+ (void)printone(buf, narenas * ARENA_SIZE);
+
+ fputc('\n', stderr);
+
+ total = printone("# bytes in allocated blocks", allocated_bytes);
+ total += printone("# bytes in available blocks", available_bytes);
+
+ PyOS_snprintf(buf, sizeof(buf),
+ "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
+ total += printone(buf, (size_t)numfreepools * POOL_SIZE);
+
+ total += printone("# bytes lost to pool headers", pool_header_bytes);
+ total += printone("# bytes lost to quantization", quantization);
+ total += printone("# bytes lost to arena alignment", arena_alignment);
+ (void)printone("Total", total);
}
-#endif /* PYMALLOC_DEBUG */
+#endif /* PYMALLOC_DEBUG */
#ifdef Py_USING_MEMORY_DEBUGGER
/* Make this function last so gcc won't inline it since the definition is
@@ -1758,8 +1758,8 @@ _PyObject_DebugMallocStats(void)
int
Py_ADDRESS_IN_RANGE(void *P, poolp pool)
{
- return pool->arenaindex < maxarenas &&
- (uptr)P - arenas[pool->arenaindex].address < (uptr)ARENA_SIZE &&
- arenas[pool->arenaindex].address != 0;
+ return pool->arenaindex < maxarenas &&
+ (uptr)P - arenas[pool->arenaindex].address < (uptr)ARENA_SIZE &&
+ arenas[pool->arenaindex].address != 0;
}
#endif
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index d7c2fb9..33a4d9d 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -17,7 +17,7 @@ typedef struct {
} rangeobject;
/* Helper function for validating step. Always returns a new reference or
- NULL on error.
+ NULL on error.
*/
static PyObject *
validate_step(PyObject *step)
@@ -269,16 +269,16 @@ range_repr(rangeobject *r)
static PyObject *
range_reduce(rangeobject *r, PyObject *args)
{
- return Py_BuildValue("(O(OOO))", Py_TYPE(r),
+ return Py_BuildValue("(O(OOO))", Py_TYPE(r),
r->start, r->stop, r->step);
}
static PySequenceMethods range_as_sequence = {
- (lenfunc)range_length, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
+ (lenfunc)range_length, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
(ssizeargfunc)range_item, /* sq_item */
- 0, /* sq_slice */
+ 0, /* sq_slice */
};
static PyObject * range_iter(PyObject *seq);
@@ -288,51 +288,51 @@ PyDoc_STRVAR(reverse_doc,
"Returns a reverse iterator.");
static PyMethodDef range_methods[] = {
- {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS,
- reverse_doc},
- {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS,
+ reverse_doc},
+ {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyRange_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "range", /* Name of this type */
- sizeof(rangeobject), /* Basic object size */
- 0, /* Item size for varobject */
- (destructor)range_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)range_repr, /* tp_repr */
- 0, /* tp_as_number */
- &range_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- range_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- range_iter, /* tp_iter */
- 0, /* tp_iternext */
- range_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- range_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "range", /* Name of this type */
+ sizeof(rangeobject), /* Basic object size */
+ 0, /* Item size for varobject */
+ (destructor)range_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)range_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &range_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ range_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ range_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ range_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ range_new, /* tp_new */
};
/*********************** range Iterator **************************/
@@ -343,18 +343,18 @@ PyTypeObject PyRange_Type = {
*/
typedef struct {
- PyObject_HEAD
- long index;
- long start;
- long step;
- long len;
+ PyObject_HEAD
+ long index;
+ long start;
+ long step;
+ long len;
} rangeiterobject;
static PyObject *
rangeiter_next(rangeiterobject *r)
{
if (r->index < r->len)
- /* cast to unsigned to avoid possible signed overflow
+ /* cast to unsigned to avoid possible signed overflow
in intermediate calculations. */
return PyLong_FromLong((long)(r->start +
(unsigned long)(r->index++) * r->step));
@@ -388,50 +388,50 @@ PyDoc_STRVAR(length_hint_doc,
static PyMethodDef rangeiter_methods[] = {
{"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
- length_hint_doc},
- {NULL, NULL} /* sentinel */
+ length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyRangeIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "range_iterator", /* tp_name */
- sizeof(rangeiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)PyObject_Del, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)rangeiter_next, /* tp_iternext */
- rangeiter_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- rangeiter_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "range_iterator", /* tp_name */
+ sizeof(rangeiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)PyObject_Del, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)rangeiter_next, /* tp_iternext */
+ rangeiter_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ rangeiter_new, /* tp_new */
};
/* Return number of items in range (lo, hi, step). step != 0
@@ -503,8 +503,8 @@ rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw)
static PyMethodDef longrangeiter_methods[] = {
{"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
- length_hint_doc},
- {NULL, NULL} /* sentinel */
+ length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
static void
@@ -553,36 +553,36 @@ longrangeiter_next(longrangeiterobject *r)
}
PyTypeObject PyLongRangeIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "longrange_iterator", /* tp_name */
- sizeof(longrangeiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)longrangeiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)longrangeiter_next, /* tp_iternext */
- longrangeiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "longrange_iterator", /* tp_name */
+ sizeof(longrangeiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)longrangeiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)longrangeiter_next, /* tp_iternext */
+ longrangeiter_methods, /* tp_methods */
+ 0,
};
static PyObject *
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 36afeb3..f0e1fcb 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1,5 +1,5 @@
-/* set object implementation
+/* set object implementation
Written and maintained by Raymond D. Hettinger <python@rcn.com>
Derived from Lib/sets.py and Objects/dictobject.c.
@@ -17,12 +17,12 @@
static void
set_key_error(PyObject *arg)
{
- PyObject *tup;
- tup = PyTuple_Pack(1, arg);
- if (!tup)
- return; /* caller will expect error to be set anyway */
- PyErr_SetObject(PyExc_KeyError, tup);
- Py_DECREF(tup);
+ PyObject *tup;
+ tup = PyTuple_Pack(1, arg);
+ if (!tup)
+ return; /* caller will expect error to be set anyway */
+ PyErr_SetObject(PyExc_KeyError, tup);
+ Py_DECREF(tup);
}
/* This must be >= 1. */
@@ -35,20 +35,20 @@ static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
PyObject *
_PySet_Dummy(void)
{
- return dummy;
+ return dummy;
}
#endif
-#define INIT_NONZERO_SET_SLOTS(so) do { \
- (so)->table = (so)->smalltable; \
- (so)->mask = PySet_MINSIZE - 1; \
- (so)->hash = -1; \
+#define INIT_NONZERO_SET_SLOTS(so) do { \
+ (so)->table = (so)->smalltable; \
+ (so)->mask = PySet_MINSIZE - 1; \
+ (so)->hash = -1; \
} while(0)
-#define EMPTY_TO_MINSIZE(so) do { \
- memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
- (so)->used = (so)->fill = 0; \
- INIT_NONZERO_SET_SLOTS(so); \
+#define EMPTY_TO_MINSIZE(so) do { \
+ memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
+ (so)->used = (so)->fill = 0; \
+ INIT_NONZERO_SET_SLOTS(so); \
} while(0)
/* Reuse scheme to save calls to malloc, free, and memset */
@@ -77,78 +77,78 @@ NULL if the rich comparison returns an error.
static setentry *
set_lookkey(PySetObject *so, PyObject *key, register long hash)
{
- register Py_ssize_t i;
- register size_t perturb;
- register setentry *freeslot;
- register size_t mask = so->mask;
- setentry *table = so->table;
- register setentry *entry;
- register int cmp;
- PyObject *startkey;
-
- i = hash & mask;
- entry = &table[i];
- if (entry->key == NULL || entry->key == key)
- return entry;
-
- if (entry->key == dummy)
- freeslot = entry;
- else {
- if (entry->hash == hash) {
- startkey = entry->key;
- Py_INCREF(startkey);
- cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
- Py_DECREF(startkey);
- if (cmp < 0)
- return NULL;
- if (table == so->table && entry->key == startkey) {
- if (cmp > 0)
- return entry;
- }
- else {
- /* The compare did major nasty stuff to the
- * set: start over.
- */
- return set_lookkey(so, key, hash);
- }
- }
- freeslot = NULL;
- }
-
- /* In the loop, key == dummy is by far (factor of 100s) the
- least likely outcome, so test for that last. */
- for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- entry = &table[i & mask];
- if (entry->key == NULL) {
- if (freeslot != NULL)
- entry = freeslot;
- break;
- }
- if (entry->key == key)
- break;
- if (entry->hash == hash && entry->key != dummy) {
- startkey = entry->key;
- Py_INCREF(startkey);
- cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
- Py_DECREF(startkey);
- if (cmp < 0)
- return NULL;
- if (table == so->table && entry->key == startkey) {
- if (cmp > 0)
- break;
- }
- else {
- /* The compare did major nasty stuff to the
- * set: start over.
- */
- return set_lookkey(so, key, hash);
- }
- }
- else if (entry->key == dummy && freeslot == NULL)
- freeslot = entry;
- }
- return entry;
+ register Py_ssize_t i;
+ register size_t perturb;
+ register setentry *freeslot;
+ register size_t mask = so->mask;
+ setentry *table = so->table;
+ register setentry *entry;
+ register int cmp;
+ PyObject *startkey;
+
+ i = hash & mask;
+ entry = &table[i];
+ if (entry->key == NULL || entry->key == key)
+ return entry;
+
+ if (entry->key == dummy)
+ freeslot = entry;
+ else {
+ if (entry->hash == hash) {
+ startkey = entry->key;
+ Py_INCREF(startkey);
+ cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
+ Py_DECREF(startkey);
+ if (cmp < 0)
+ return NULL;
+ if (table == so->table && entry->key == startkey) {
+ if (cmp > 0)
+ return entry;
+ }
+ else {
+ /* The compare did major nasty stuff to the
+ * set: start over.
+ */
+ return set_lookkey(so, key, hash);
+ }
+ }
+ freeslot = NULL;
+ }
+
+ /* In the loop, key == dummy is by far (factor of 100s) the
+ least likely outcome, so test for that last. */
+ for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ entry = &table[i & mask];
+ if (entry->key == NULL) {
+ if (freeslot != NULL)
+ entry = freeslot;
+ break;
+ }
+ if (entry->key == key)
+ break;
+ if (entry->hash == hash && entry->key != dummy) {
+ startkey = entry->key;
+ Py_INCREF(startkey);
+ cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
+ Py_DECREF(startkey);
+ if (cmp < 0)
+ return NULL;
+ if (table == so->table && entry->key == startkey) {
+ if (cmp > 0)
+ break;
+ }
+ else {
+ /* The compare did major nasty stuff to the
+ * set: start over.
+ */
+ return set_lookkey(so, key, hash);
+ }
+ }
+ else if (entry->key == dummy && freeslot == NULL)
+ freeslot = entry;
+ }
+ return entry;
}
/*
@@ -159,50 +159,50 @@ set_lookkey(PySetObject *so, PyObject *key, register long hash)
static setentry *
set_lookkey_unicode(PySetObject *so, PyObject *key, register long hash)
{
- register Py_ssize_t i;
- register size_t perturb;
- register setentry *freeslot;
- register size_t mask = so->mask;
- setentry *table = so->table;
- register setentry *entry;
-
- /* Make sure this function doesn't have to handle non-unicode keys,
- including subclasses of str; e.g., one reason to subclass
- strings is to override __eq__, and for speed we don't cater to
- that here. */
- if (!PyUnicode_CheckExact(key)) {
- so->lookup = set_lookkey;
- return set_lookkey(so, key, hash);
- }
- i = hash & mask;
- entry = &table[i];
- if (entry->key == NULL || entry->key == key)
- return entry;
- if (entry->key == dummy)
- freeslot = entry;
- else {
- if (entry->hash == hash && unicode_eq(entry->key, key))
- return entry;
- freeslot = NULL;
- }
-
- /* In the loop, key == dummy is by far (factor of 100s) the
- least likely outcome, so test for that last. */
- for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- entry = &table[i & mask];
- if (entry->key == NULL)
- return freeslot == NULL ? entry : freeslot;
- if (entry->key == key
- || (entry->hash == hash
- && entry->key != dummy
- && unicode_eq(entry->key, key)))
- return entry;
- if (entry->key == dummy && freeslot == NULL)
- freeslot = entry;
- }
- assert(0); /* NOT REACHED */
- return 0;
+ register Py_ssize_t i;
+ register size_t perturb;
+ register setentry *freeslot;
+ register size_t mask = so->mask;
+ setentry *table = so->table;
+ register setentry *entry;
+
+ /* Make sure this function doesn't have to handle non-unicode keys,
+ including subclasses of str; e.g., one reason to subclass
+ strings is to override __eq__, and for speed we don't cater to
+ that here. */
+ if (!PyUnicode_CheckExact(key)) {
+ so->lookup = set_lookkey;
+ return set_lookkey(so, key, hash);
+ }
+ i = hash & mask;
+ entry = &table[i];
+ if (entry->key == NULL || entry->key == key)
+ return entry;
+ if (entry->key == dummy)
+ freeslot = entry;
+ else {
+ if (entry->hash == hash && unicode_eq(entry->key, key))
+ return entry;
+ freeslot = NULL;
+ }
+
+ /* In the loop, key == dummy is by far (factor of 100s) the
+ least likely outcome, so test for that last. */
+ for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ entry = &table[i & mask];
+ if (entry->key == NULL)
+ return freeslot == NULL ? entry : freeslot;
+ if (entry->key == key
+ || (entry->hash == hash
+ && entry->key != dummy
+ && unicode_eq(entry->key, key)))
+ return entry;
+ if (entry->key == dummy && freeslot == NULL)
+ freeslot = entry;
+ }
+ assert(0); /* NOT REACHED */
+ return 0;
}
/*
@@ -213,30 +213,30 @@ Eats a reference to key.
static int
set_insert_key(register PySetObject *so, PyObject *key, long hash)
{
- register setentry *entry;
- typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
-
- assert(so->lookup != NULL);
- entry = so->lookup(so, key, hash);
- if (entry == NULL)
- return -1;
- if (entry->key == NULL) {
- /* UNUSED */
- so->fill++;
- entry->key = key;
- entry->hash = hash;
- so->used++;
- } else if (entry->key == dummy) {
- /* DUMMY */
- entry->key = key;
- entry->hash = hash;
- so->used++;
- Py_DECREF(dummy);
- } else {
- /* ACTIVE */
- Py_DECREF(key);
- }
- return 0;
+ register setentry *entry;
+ typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
+
+ assert(so->lookup != NULL);
+ entry = so->lookup(so, key, hash);
+ if (entry == NULL)
+ return -1;
+ if (entry->key == NULL) {
+ /* UNUSED */
+ so->fill++;
+ entry->key = key;
+ entry->hash = hash;
+ so->used++;
+ } else if (entry->key == dummy) {
+ /* DUMMY */
+ entry->key = key;
+ entry->hash = hash;
+ so->used++;
+ Py_DECREF(dummy);
+ } else {
+ /* ACTIVE */
+ Py_DECREF(key);
+ }
+ return 0;
}
/*
@@ -250,22 +250,22 @@ is responsible for incref'ing `key`.
static void
set_insert_clean(register PySetObject *so, PyObject *key, long hash)
{
- register size_t i;
- register size_t perturb;
- register size_t mask = (size_t)so->mask;
- setentry *table = so->table;
- register setentry *entry;
+ register size_t i;
+ register size_t perturb;
+ register size_t mask = (size_t)so->mask;
+ setentry *table = so->table;
+ register setentry *entry;
- i = hash & mask;
- entry = &table[i];
- for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
- i = (i << 2) + i + perturb + 1;
- entry = &table[i & mask];
- }
- so->fill++;
- entry->key = key;
- entry->hash = hash;
- so->used++;
+ i = hash & mask;
+ entry = &table[i];
+ for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ entry = &table[i & mask];
+ }
+ so->fill++;
+ entry->key = key;
+ entry->hash = hash;
+ so->used++;
}
/*
@@ -276,86 +276,86 @@ actually be smaller than the old one.
static int
set_table_resize(PySetObject *so, Py_ssize_t minused)
{
- Py_ssize_t newsize;
- setentry *oldtable, *newtable, *entry;
- Py_ssize_t i;
- int is_oldtable_malloced;
- setentry small_copy[PySet_MINSIZE];
-
- assert(minused >= 0);
-
- /* Find the smallest table size > minused. */
- for (newsize = PySet_MINSIZE;
- newsize <= minused && newsize > 0;
- newsize <<= 1)
- ;
- if (newsize <= 0) {
- PyErr_NoMemory();
- return -1;
- }
-
- /* Get space for a new table. */
- oldtable = so->table;
- assert(oldtable != NULL);
- is_oldtable_malloced = oldtable != so->smalltable;
-
- if (newsize == PySet_MINSIZE) {
- /* A large table is shrinking, or we can't get any smaller. */
- newtable = so->smalltable;
- if (newtable == oldtable) {
- if (so->fill == so->used) {
- /* No dummies, so no point doing anything. */
- return 0;
- }
- /* We're not going to resize it, but rebuild the
- table anyway to purge old dummy entries.
- Subtle: This is *necessary* if fill==size,
- as set_lookkey needs at least one virgin slot to
- terminate failing searches. If fill < size, it's
- merely desirable, as dummies slow searches. */
- assert(so->fill > so->used);
- memcpy(small_copy, oldtable, sizeof(small_copy));
- oldtable = small_copy;
- }
- }
- else {
- newtable = PyMem_NEW(setentry, newsize);
- if (newtable == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- }
-
- /* Make the set empty, using the new table. */
- assert(newtable != oldtable);
- so->table = newtable;
- so->mask = newsize - 1;
- memset(newtable, 0, sizeof(setentry) * newsize);
- so->used = 0;
- i = so->fill;
- so->fill = 0;
-
- /* Copy the data over; this is refcount-neutral for active entries;
- dummy entries aren't copied over, of course */
- for (entry = oldtable; i > 0; entry++) {
- if (entry->key == NULL) {
- /* UNUSED */
- ;
- } else if (entry->key == dummy) {
- /* DUMMY */
- --i;
- assert(entry->key == dummy);
- Py_DECREF(entry->key);
- } else {
- /* ACTIVE */
- --i;
- set_insert_clean(so, entry->key, entry->hash);
- }
- }
-
- if (is_oldtable_malloced)
- PyMem_DEL(oldtable);
- return 0;
+ Py_ssize_t newsize;
+ setentry *oldtable, *newtable, *entry;
+ Py_ssize_t i;
+ int is_oldtable_malloced;
+ setentry small_copy[PySet_MINSIZE];
+
+ assert(minused >= 0);
+
+ /* Find the smallest table size > minused. */
+ for (newsize = PySet_MINSIZE;
+ newsize <= minused && newsize > 0;
+ newsize <<= 1)
+ ;
+ if (newsize <= 0) {
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ /* Get space for a new table. */
+ oldtable = so->table;
+ assert(oldtable != NULL);
+ is_oldtable_malloced = oldtable != so->smalltable;
+
+ if (newsize == PySet_MINSIZE) {
+ /* A large table is shrinking, or we can't get any smaller. */
+ newtable = so->smalltable;
+ if (newtable == oldtable) {
+ if (so->fill == so->used) {
+ /* No dummies, so no point doing anything. */
+ return 0;
+ }
+ /* We're not going to resize it, but rebuild the
+ table anyway to purge old dummy entries.
+ Subtle: This is *necessary* if fill==size,
+ as set_lookkey needs at least one virgin slot to
+ terminate failing searches. If fill < size, it's
+ merely desirable, as dummies slow searches. */
+ assert(so->fill > so->used);
+ memcpy(small_copy, oldtable, sizeof(small_copy));
+ oldtable = small_copy;
+ }
+ }
+ else {
+ newtable = PyMem_NEW(setentry, newsize);
+ if (newtable == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ }
+
+ /* Make the set empty, using the new table. */
+ assert(newtable != oldtable);
+ so->table = newtable;
+ so->mask = newsize - 1;
+ memset(newtable, 0, sizeof(setentry) * newsize);
+ so->used = 0;
+ i = so->fill;
+ so->fill = 0;
+
+ /* Copy the data over; this is refcount-neutral for active entries;
+ dummy entries aren't copied over, of course */
+ for (entry = oldtable; i > 0; entry++) {
+ if (entry->key == NULL) {
+ /* UNUSED */
+ ;
+ } else if (entry->key == dummy) {
+ /* DUMMY */
+ --i;
+ assert(entry->key == dummy);
+ Py_DECREF(entry->key);
+ } else {
+ /* ACTIVE */
+ --i;
+ set_insert_clean(so, entry->key, entry->hash);
+ }
+ }
+
+ if (is_oldtable_malloced)
+ PyMem_DEL(oldtable);
+ return 0;
}
/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
@@ -363,42 +363,42 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
static int
set_add_entry(register PySetObject *so, setentry *entry)
{
- register Py_ssize_t n_used;
+ register Py_ssize_t n_used;
- assert(so->fill <= so->mask); /* at least one empty slot */
- n_used = so->used;
- Py_INCREF(entry->key);
- if (set_insert_key(so, entry->key, entry->hash) == -1) {
- Py_DECREF(entry->key);
- return -1;
- }
- if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
- return 0;
- return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
+ assert(so->fill <= so->mask); /* at least one empty slot */
+ n_used = so->used;
+ Py_INCREF(entry->key);
+ if (set_insert_key(so, entry->key, entry->hash) == -1) {
+ Py_DECREF(entry->key);
+ return -1;
+ }
+ if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
+ return 0;
+ return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
}
static int
set_add_key(register PySetObject *so, PyObject *key)
{
- register long hash;
- register Py_ssize_t n_used;
-
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- assert(so->fill <= so->mask); /* at least one empty slot */
- n_used = so->used;
- Py_INCREF(key);
- if (set_insert_key(so, key, hash) == -1) {
- Py_DECREF(key);
- return -1;
- }
- if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
- return 0;
- return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
+ register long hash;
+ register Py_ssize_t n_used;
+
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ assert(so->fill <= so->mask); /* at least one empty slot */
+ n_used = so->used;
+ Py_INCREF(key);
+ if (set_insert_key(so, key, hash) == -1) {
+ Py_DECREF(key);
+ return -1;
+ }
+ if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
+ return 0;
+ return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
}
#define DISCARD_NOTFOUND 0
@@ -406,112 +406,112 @@ set_add_key(register PySetObject *so, PyObject *key)
static int
set_discard_entry(PySetObject *so, setentry *oldentry)
-{ register setentry *entry;
- PyObject *old_key;
-
- entry = (so->lookup)(so, oldentry->key, oldentry->hash);
- if (entry == NULL)
- return -1;
- if (entry->key == NULL || entry->key == dummy)
- return DISCARD_NOTFOUND;
- old_key = entry->key;
- Py_INCREF(dummy);
- entry->key = dummy;
- so->used--;
- Py_DECREF(old_key);
- return DISCARD_FOUND;
+{ register setentry *entry;
+ PyObject *old_key;
+
+ entry = (so->lookup)(so, oldentry->key, oldentry->hash);
+ if (entry == NULL)
+ return -1;
+ if (entry->key == NULL || entry->key == dummy)
+ return DISCARD_NOTFOUND;
+ old_key = entry->key;
+ Py_INCREF(dummy);
+ entry->key = dummy;
+ so->used--;
+ Py_DECREF(old_key);
+ return DISCARD_FOUND;
}
static int
set_discard_key(PySetObject *so, PyObject *key)
{
- register long hash;
- register setentry *entry;
- PyObject *old_key;
-
- assert (PyAnySet_Check(so));
-
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- entry = (so->lookup)(so, key, hash);
- if (entry == NULL)
- return -1;
- if (entry->key == NULL || entry->key == dummy)
- return DISCARD_NOTFOUND;
- old_key = entry->key;
- Py_INCREF(dummy);
- entry->key = dummy;
- so->used--;
- Py_DECREF(old_key);
- return DISCARD_FOUND;
+ register long hash;
+ register setentry *entry;
+ PyObject *old_key;
+
+ assert (PyAnySet_Check(so));
+
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ entry = (so->lookup)(so, key, hash);
+ if (entry == NULL)
+ return -1;
+ if (entry->key == NULL || entry->key == dummy)
+ return DISCARD_NOTFOUND;
+ old_key = entry->key;
+ Py_INCREF(dummy);
+ entry->key = dummy;
+ so->used--;
+ Py_DECREF(old_key);
+ return DISCARD_FOUND;
}
static int
set_clear_internal(PySetObject *so)
{
- setentry *entry, *table;
- int table_is_malloced;
- Py_ssize_t fill;
- setentry small_copy[PySet_MINSIZE];
+ setentry *entry, *table;
+ int table_is_malloced;
+ Py_ssize_t fill;
+ setentry small_copy[PySet_MINSIZE];
#ifdef Py_DEBUG
- Py_ssize_t i, n;
- assert (PyAnySet_Check(so));
+ Py_ssize_t i, n;
+ assert (PyAnySet_Check(so));
- n = so->mask + 1;
- i = 0;
+ n = so->mask + 1;
+ i = 0;
#endif
- table = so->table;
- assert(table != NULL);
- table_is_malloced = table != so->smalltable;
-
- /* This is delicate. During the process of clearing the set,
- * decrefs can cause the set to mutate. To avoid fatal confusion
- * (voice of experience), we have to make the set empty before
- * clearing the slots, and never refer to anything via so->ref while
- * clearing.
- */
- fill = so->fill;
- if (table_is_malloced)
- EMPTY_TO_MINSIZE(so);
-
- else if (fill > 0) {
- /* It's a small table with something that needs to be cleared.
- * Afraid the only safe way is to copy the set entries into
- * another small table first.
- */
- memcpy(small_copy, table, sizeof(small_copy));
- table = small_copy;
- EMPTY_TO_MINSIZE(so);
- }
- /* else it's a small table that's already empty */
-
- /* Now we can finally clear things. If C had refcounts, we could
- * assert that the refcount on table is 1 now, i.e. that this function
- * has unique access to it, so decref side-effects can't alter it.
- */
- for (entry = table; fill > 0; ++entry) {
+ table = so->table;
+ assert(table != NULL);
+ table_is_malloced = table != so->smalltable;
+
+ /* This is delicate. During the process of clearing the set,
+ * decrefs can cause the set to mutate. To avoid fatal confusion
+ * (voice of experience), we have to make the set empty before
+ * clearing the slots, and never refer to anything via so->ref while
+ * clearing.
+ */
+ fill = so->fill;
+ if (table_is_malloced)
+ EMPTY_TO_MINSIZE(so);
+
+ else if (fill > 0) {
+ /* It's a small table with something that needs to be cleared.
+ * Afraid the only safe way is to copy the set entries into
+ * another small table first.
+ */
+ memcpy(small_copy, table, sizeof(small_copy));
+ table = small_copy;
+ EMPTY_TO_MINSIZE(so);
+ }
+ /* else it's a small table that's already empty */
+
+ /* Now we can finally clear things. If C had refcounts, we could
+ * assert that the refcount on table is 1 now, i.e. that this function
+ * has unique access to it, so decref side-effects can't alter it.
+ */
+ for (entry = table; fill > 0; ++entry) {
#ifdef Py_DEBUG
- assert(i < n);
- ++i;
+ assert(i < n);
+ ++i;
#endif
- if (entry->key) {
- --fill;
- Py_DECREF(entry->key);
- }
+ if (entry->key) {
+ --fill;
+ Py_DECREF(entry->key);
+ }
#ifdef Py_DEBUG
- else
- assert(entry->key == NULL);
+ else
+ assert(entry->key == NULL);
#endif
- }
+ }
- if (table_is_malloced)
- PyMem_DEL(table);
- return 0;
+ if (table_is_malloced)
+ PyMem_DEL(table);
+ return 0;
}
/*
@@ -525,223 +525,223 @@ set_clear_internal(PySetObject *so)
* }
*
* CAUTION: In general, it isn't safe to use set_next in a loop that
- * mutates the table.
+ * mutates the table.
*/
static int
set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
{
- Py_ssize_t i;
- Py_ssize_t mask;
- register setentry *table;
-
- assert (PyAnySet_Check(so));
- i = *pos_ptr;
- assert(i >= 0);
- table = so->table;
- mask = so->mask;
- while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
- i++;
- *pos_ptr = i+1;
- if (i > mask)
- return 0;
- assert(table[i].key != NULL);
- *entry_ptr = &table[i];
- return 1;
+ Py_ssize_t i;
+ Py_ssize_t mask;
+ register setentry *table;
+
+ assert (PyAnySet_Check(so));
+ i = *pos_ptr;
+ assert(i >= 0);
+ table = so->table;
+ mask = so->mask;
+ while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
+ i++;
+ *pos_ptr = i+1;
+ if (i > mask)
+ return 0;
+ assert(table[i].key != NULL);
+ *entry_ptr = &table[i];
+ return 1;
}
static void
set_dealloc(PySetObject *so)
{
- register setentry *entry;
- Py_ssize_t fill = so->fill;
- PyObject_GC_UnTrack(so);
- Py_TRASHCAN_SAFE_BEGIN(so)
- if (so->weakreflist != NULL)
- PyObject_ClearWeakRefs((PyObject *) so);
-
- for (entry = so->table; fill > 0; entry++) {
- if (entry->key) {
- --fill;
- Py_DECREF(entry->key);
- }
- }
- if (so->table != so->smalltable)
- PyMem_DEL(so->table);
- if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
- free_list[numfree++] = so;
- else
- Py_TYPE(so)->tp_free(so);
- Py_TRASHCAN_SAFE_END(so)
+ register setentry *entry;
+ Py_ssize_t fill = so->fill;
+ PyObject_GC_UnTrack(so);
+ Py_TRASHCAN_SAFE_BEGIN(so)
+ if (so->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) so);
+
+ for (entry = so->table; fill > 0; entry++) {
+ if (entry->key) {
+ --fill;
+ Py_DECREF(entry->key);
+ }
+ }
+ if (so->table != so->smalltable)
+ PyMem_DEL(so->table);
+ if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
+ free_list[numfree++] = so;
+ else
+ Py_TYPE(so)->tp_free(so);
+ Py_TRASHCAN_SAFE_END(so)
}
static PyObject *
set_repr(PySetObject *so)
{
- PyObject *keys, *result=NULL;
- Py_UNICODE *u;
- int status = Py_ReprEnter((PyObject*)so);
- PyObject *listrepr;
- Py_ssize_t newsize;
-
- if (status != 0) {
- if (status < 0)
- return NULL;
- return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
- }
-
- /* shortcut for the empty set */
- if (!so->used) {
- Py_ReprLeave((PyObject*)so);
- return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
- }
-
- keys = PySequence_List((PyObject *)so);
- if (keys == NULL)
- goto done;
-
- listrepr = PyObject_Repr(keys);
- Py_DECREF(keys);
- if (listrepr == NULL)
- goto done;
- newsize = PyUnicode_GET_SIZE(listrepr);
- result = PyUnicode_FromUnicode(NULL, newsize);
- if (result) {
- u = PyUnicode_AS_UNICODE(result);
- *u++ = '{';
- /* Omit the brackets from the listrepr */
- Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
- PyUnicode_GET_SIZE(listrepr)-2);
- u += newsize-2;
- *u++ = '}';
- }
- Py_DECREF(listrepr);
- if (Py_TYPE(so) != &PySet_Type) {
- PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
- Py_TYPE(so)->tp_name,
- result);
- Py_DECREF(result);
- result = tmp;
- }
+ PyObject *keys, *result=NULL;
+ Py_UNICODE *u;
+ int status = Py_ReprEnter((PyObject*)so);
+ PyObject *listrepr;
+ Py_ssize_t newsize;
+
+ if (status != 0) {
+ if (status < 0)
+ return NULL;
+ return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
+ }
+
+ /* shortcut for the empty set */
+ if (!so->used) {
+ Py_ReprLeave((PyObject*)so);
+ return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
+ }
+
+ keys = PySequence_List((PyObject *)so);
+ if (keys == NULL)
+ goto done;
+
+ listrepr = PyObject_Repr(keys);
+ Py_DECREF(keys);
+ if (listrepr == NULL)
+ goto done;
+ newsize = PyUnicode_GET_SIZE(listrepr);
+ result = PyUnicode_FromUnicode(NULL, newsize);
+ if (result) {
+ u = PyUnicode_AS_UNICODE(result);
+ *u++ = '{';
+ /* Omit the brackets from the listrepr */
+ Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,
+ PyUnicode_GET_SIZE(listrepr)-2);
+ u += newsize-2;
+ *u++ = '}';
+ }
+ Py_DECREF(listrepr);
+ if (Py_TYPE(so) != &PySet_Type) {
+ PyObject *tmp = PyUnicode_FromFormat("%s(%U)",
+ Py_TYPE(so)->tp_name,
+ result);
+ Py_DECREF(result);
+ result = tmp;
+ }
done:
- Py_ReprLeave((PyObject*)so);
- return result;
+ Py_ReprLeave((PyObject*)so);
+ return result;
}
static Py_ssize_t
set_len(PyObject *so)
{
- return ((PySetObject *)so)->used;
+ return ((PySetObject *)so)->used;
}
static int
set_merge(PySetObject *so, PyObject *otherset)
{
- PySetObject *other;
- register Py_ssize_t i;
- register setentry *entry;
-
- assert (PyAnySet_Check(so));
- assert (PyAnySet_Check(otherset));
-
- other = (PySetObject*)otherset;
- if (other == so || other->used == 0)
- /* a.update(a) or a.update({}); nothing to do */
- return 0;
- /* Do one big resize at the start, rather than
- * incrementally resizing as we insert new keys. Expect
- * that there will be no (or few) overlapping keys.
- */
- if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
- if (set_table_resize(so, (so->used + other->used)*2) != 0)
- return -1;
- }
- for (i = 0; i <= other->mask; i++) {
- entry = &other->table[i];
- if (entry->key != NULL &&
- entry->key != dummy) {
- Py_INCREF(entry->key);
- if (set_insert_key(so, entry->key, entry->hash) == -1) {
- Py_DECREF(entry->key);
- return -1;
- }
- }
- }
- return 0;
+ PySetObject *other;
+ register Py_ssize_t i;
+ register setentry *entry;
+
+ assert (PyAnySet_Check(so));
+ assert (PyAnySet_Check(otherset));
+
+ other = (PySetObject*)otherset;
+ if (other == so || other->used == 0)
+ /* a.update(a) or a.update({}); nothing to do */
+ return 0;
+ /* Do one big resize at the start, rather than
+ * incrementally resizing as we insert new keys. Expect
+ * that there will be no (or few) overlapping keys.
+ */
+ if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
+ if (set_table_resize(so, (so->used + other->used)*2) != 0)
+ return -1;
+ }
+ for (i = 0; i <= other->mask; i++) {
+ entry = &other->table[i];
+ if (entry->key != NULL &&
+ entry->key != dummy) {
+ Py_INCREF(entry->key);
+ if (set_insert_key(so, entry->key, entry->hash) == -1) {
+ Py_DECREF(entry->key);
+ return -1;
+ }
+ }
+ }
+ return 0;
}
static int
set_contains_key(PySetObject *so, PyObject *key)
{
- long hash;
- setentry *entry;
+ long hash;
+ setentry *entry;
- if (!PyUnicode_CheckExact(key) ||
- (hash = ((PyUnicodeObject *) key)->hash) == -1) {
- hash = PyObject_Hash(key);
- if (hash == -1)
- return -1;
- }
- entry = (so->lookup)(so, key, hash);
- if (entry == NULL)
- return -1;
- key = entry->key;
- return key != NULL && key != dummy;
+ if (!PyUnicode_CheckExact(key) ||
+ (hash = ((PyUnicodeObject *) key)->hash) == -1) {
+ hash = PyObject_Hash(key);
+ if (hash == -1)
+ return -1;
+ }
+ entry = (so->lookup)(so, key, hash);
+ if (entry == NULL)
+ return -1;
+ key = entry->key;
+ return key != NULL && key != dummy;
}
static int
set_contains_entry(PySetObject *so, setentry *entry)
{
- PyObject *key;
- setentry *lu_entry;
+ PyObject *key;
+ setentry *lu_entry;
- lu_entry = (so->lookup)(so, entry->key, entry->hash);
- if (lu_entry == NULL)
- return -1;
- key = lu_entry->key;
- return key != NULL && key != dummy;
+ lu_entry = (so->lookup)(so, entry->key, entry->hash);
+ if (lu_entry == NULL)
+ return -1;
+ key = lu_entry->key;
+ return key != NULL && key != dummy;
}
static PyObject *
set_pop(PySetObject *so)
{
- register Py_ssize_t i = 0;
- register setentry *entry;
- PyObject *key;
-
- assert (PyAnySet_Check(so));
- if (so->used == 0) {
- PyErr_SetString(PyExc_KeyError, "pop from an empty set");
- return NULL;
- }
-
- /* Set entry to "the first" unused or dummy set entry. We abuse
- * the hash field of slot 0 to hold a search finger:
- * If slot 0 has a value, use slot 0.
- * Else slot 0 is being used to hold a search finger,
- * and we use its hash value as the first index to look.
- */
- entry = &so->table[0];
- if (entry->key == NULL || entry->key == dummy) {
- i = entry->hash;
- /* The hash field may be a real hash value, or it may be a
- * legit search finger, or it may be a once-legit search
- * finger that's out of bounds now because it wrapped around
- * or the table shrunk -- simply make sure it's in bounds now.
- */
- if (i > so->mask || i < 1)
- i = 1; /* skip slot 0 */
- while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
- i++;
- if (i > so->mask)
- i = 1;
- }
- }
- key = entry->key;
- Py_INCREF(dummy);
- entry->key = dummy;
- so->used--;
- so->table[0].hash = i + 1; /* next place to start */
- return key;
+ register Py_ssize_t i = 0;
+ register setentry *entry;
+ PyObject *key;
+
+ assert (PyAnySet_Check(so));
+ if (so->used == 0) {
+ PyErr_SetString(PyExc_KeyError, "pop from an empty set");
+ return NULL;
+ }
+
+ /* Set entry to "the first" unused or dummy set entry. We abuse
+ * the hash field of slot 0 to hold a search finger:
+ * If slot 0 has a value, use slot 0.
+ * Else slot 0 is being used to hold a search finger,
+ * and we use its hash value as the first index to look.
+ */
+ entry = &so->table[0];
+ if (entry->key == NULL || entry->key == dummy) {
+ i = entry->hash;
+ /* The hash field may be a real hash value, or it may be a
+ * legit search finger, or it may be a once-legit search
+ * finger that's out of bounds now because it wrapped around
+ * or the table shrunk -- simply make sure it's in bounds now.
+ */
+ if (i > so->mask || i < 1)
+ i = 1; /* skip slot 0 */
+ while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
+ i++;
+ if (i > so->mask)
+ i = 1;
+ }
+ }
+ key = entry->key;
+ Py_INCREF(dummy);
+ entry->key = dummy;
+ so->used--;
+ so->table[0].hash = i + 1; /* next place to start */
+ return key;
}
PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\
@@ -750,289 +750,289 @@ Raises KeyError if the set is empty.");
static int
set_traverse(PySetObject *so, visitproc visit, void *arg)
{
- Py_ssize_t pos = 0;
- setentry *entry;
+ Py_ssize_t pos = 0;
+ setentry *entry;
- while (set_next(so, &pos, &entry))
- Py_VISIT(entry->key);
- return 0;
+ while (set_next(so, &pos, &entry))
+ Py_VISIT(entry->key);
+ return 0;
}
static long
frozenset_hash(PyObject *self)
{
- PySetObject *so = (PySetObject *)self;
- long h, hash = 1927868237L;
- setentry *entry;
- Py_ssize_t pos = 0;
-
- if (so->hash != -1)
- return so->hash;
-
- hash *= PySet_GET_SIZE(self) + 1;
- while (set_next(so, &pos, &entry)) {
- /* Work to increase the bit dispersion for closely spaced hash
- values. The is important because some use cases have many
- combinations of a small number of elements with nearby
- hashes so that many distinct combinations collapse to only
- a handful of distinct hash values. */
- h = entry->hash;
- hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
- }
- hash = hash * 69069L + 907133923L;
- if (hash == -1)
- hash = 590923713L;
- so->hash = hash;
- return hash;
+ PySetObject *so = (PySetObject *)self;
+ long h, hash = 1927868237L;
+ setentry *entry;
+ Py_ssize_t pos = 0;
+
+ if (so->hash != -1)
+ return so->hash;
+
+ hash *= PySet_GET_SIZE(self) + 1;
+ while (set_next(so, &pos, &entry)) {
+ /* Work to increase the bit dispersion for closely spaced hash
+ values. The is important because some use cases have many
+ combinations of a small number of elements with nearby
+ hashes so that many distinct combinations collapse to only
+ a handful of distinct hash values. */
+ h = entry->hash;
+ hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
+ }
+ hash = hash * 69069L + 907133923L;
+ if (hash == -1)
+ hash = 590923713L;
+ so->hash = hash;
+ return hash;
}
/***** Set iterator type ***********************************************/
typedef struct {
- PyObject_HEAD
- PySetObject *si_set; /* Set to NULL when iterator is exhausted */
- Py_ssize_t si_used;
- Py_ssize_t si_pos;
- Py_ssize_t len;
+ PyObject_HEAD
+ PySetObject *si_set; /* Set to NULL when iterator is exhausted */
+ Py_ssize_t si_used;
+ Py_ssize_t si_pos;
+ Py_ssize_t len;
} setiterobject;
static void
setiter_dealloc(setiterobject *si)
{
- Py_XDECREF(si->si_set);
- PyObject_GC_Del(si);
+ Py_XDECREF(si->si_set);
+ PyObject_GC_Del(si);
}
static int
setiter_traverse(setiterobject *si, visitproc visit, void *arg)
{
- Py_VISIT(si->si_set);
- return 0;
+ Py_VISIT(si->si_set);
+ return 0;
}
static PyObject *
setiter_len(setiterobject *si)
{
- Py_ssize_t len = 0;
- if (si->si_set != NULL && si->si_used == si->si_set->used)
- len = si->len;
- return PyLong_FromLong(len);
+ Py_ssize_t len = 0;
+ if (si->si_set != NULL && si->si_used == si->si_set->used)
+ len = si->len;
+ return PyLong_FromLong(len);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef setiter_methods[] = {
- {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
static PyObject *setiter_iternext(setiterobject *si)
{
- PyObject *key;
- register Py_ssize_t i, mask;
- register setentry *entry;
- PySetObject *so = si->si_set;
-
- if (so == NULL)
- return NULL;
- assert (PyAnySet_Check(so));
-
- if (si->si_used != so->used) {
- PyErr_SetString(PyExc_RuntimeError,
- "Set changed size during iteration");
- si->si_used = -1; /* Make this state sticky */
- return NULL;
- }
-
- i = si->si_pos;
- assert(i>=0);
- entry = so->table;
- mask = so->mask;
- while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
- i++;
- si->si_pos = i+1;
- if (i > mask)
- goto fail;
- si->len--;
- key = entry[i].key;
- Py_INCREF(key);
- return key;
+ PyObject *key;
+ register Py_ssize_t i, mask;
+ register setentry *entry;
+ PySetObject *so = si->si_set;
+
+ if (so == NULL)
+ return NULL;
+ assert (PyAnySet_Check(so));
+
+ if (si->si_used != so->used) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Set changed size during iteration");
+ si->si_used = -1; /* Make this state sticky */
+ return NULL;
+ }
+
+ i = si->si_pos;
+ assert(i>=0);
+ entry = so->table;
+ mask = so->mask;
+ while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
+ i++;
+ si->si_pos = i+1;
+ if (i > mask)
+ goto fail;
+ si->len--;
+ key = entry[i].key;
+ Py_INCREF(key);
+ return key;
fail:
- Py_DECREF(so);
- si->si_set = NULL;
- return NULL;
+ Py_DECREF(so);
+ si->si_set = NULL;
+ return NULL;
}
PyTypeObject PySetIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "set_iterator", /* tp_name */
- sizeof(setiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)setiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)setiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)setiter_iternext, /* tp_iternext */
- setiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "set_iterator", /* tp_name */
+ sizeof(setiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)setiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)setiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)setiter_iternext, /* tp_iternext */
+ setiter_methods, /* tp_methods */
+ 0,
};
static PyObject *
set_iter(PySetObject *so)
{
- setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
- if (si == NULL)
- return NULL;
- Py_INCREF(so);
- si->si_set = so;
- si->si_used = so->used;
- si->si_pos = 0;
- si->len = so->used;
- _PyObject_GC_TRACK(si);
- return (PyObject *)si;
+ setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
+ if (si == NULL)
+ return NULL;
+ Py_INCREF(so);
+ si->si_set = so;
+ si->si_used = so->used;
+ si->si_pos = 0;
+ si->len = so->used;
+ _PyObject_GC_TRACK(si);
+ return (PyObject *)si;
}
static int
set_update_internal(PySetObject *so, PyObject *other)
{
- PyObject *key, *it;
-
- if (PyAnySet_Check(other))
- return set_merge(so, other);
-
- if (PyDict_CheckExact(other)) {
- PyObject *value;
- Py_ssize_t pos = 0;
- long hash;
- Py_ssize_t dictsize = PyDict_Size(other);
-
- /* Do one big resize at the start, rather than
- * incrementally resizing as we insert new keys. Expect
- * that there will be no (or few) overlapping keys.
- */
- if (dictsize == -1)
- return -1;
- if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
- if (set_table_resize(so, (so->used + dictsize)*2) != 0)
- return -1;
- }
- while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
- setentry an_entry;
-
- an_entry.hash = hash;
- an_entry.key = key;
- if (set_add_entry(so, &an_entry) == -1)
- return -1;
- }
- return 0;
- }
-
- it = PyObject_GetIter(other);
- if (it == NULL)
- return -1;
-
- while ((key = PyIter_Next(it)) != NULL) {
- if (set_add_key(so, key) == -1) {
- Py_DECREF(it);
- Py_DECREF(key);
- return -1;
- }
- Py_DECREF(key);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return -1;
- return 0;
+ PyObject *key, *it;
+
+ if (PyAnySet_Check(other))
+ return set_merge(so, other);
+
+ if (PyDict_CheckExact(other)) {
+ PyObject *value;
+ Py_ssize_t pos = 0;
+ long hash;
+ Py_ssize_t dictsize = PyDict_Size(other);
+
+ /* Do one big resize at the start, rather than
+ * incrementally resizing as we insert new keys. Expect
+ * that there will be no (or few) overlapping keys.
+ */
+ if (dictsize == -1)
+ return -1;
+ if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
+ if (set_table_resize(so, (so->used + dictsize)*2) != 0)
+ return -1;
+ }
+ while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
+ setentry an_entry;
+
+ an_entry.hash = hash;
+ an_entry.key = key;
+ if (set_add_entry(so, &an_entry) == -1)
+ return -1;
+ }
+ return 0;
+ }
+
+ it = PyObject_GetIter(other);
+ if (it == NULL)
+ return -1;
+
+ while ((key = PyIter_Next(it)) != NULL) {
+ if (set_add_key(so, key) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(key);
+ return -1;
+ }
+ Py_DECREF(key);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return -1;
+ return 0;
}
static PyObject *
set_update(PySetObject *so, PyObject *args)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
- PyObject *other = PyTuple_GET_ITEM(args, i);
- if (set_update_internal(so, other) == -1)
- return NULL;
- }
- Py_RETURN_NONE;
+ for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
+ PyObject *other = PyTuple_GET_ITEM(args, i);
+ if (set_update_internal(so, other) == -1)
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
-PyDoc_STRVAR(update_doc,
+PyDoc_STRVAR(update_doc,
"Update a set with the union of itself and others.");
static PyObject *
make_new_set(PyTypeObject *type, PyObject *iterable)
{
- register PySetObject *so = NULL;
-
- if (dummy == NULL) { /* Auto-initialize dummy */
- dummy = PyUnicode_FromString("<dummy key>");
- if (dummy == NULL)
- return NULL;
- }
-
- /* create PySetObject structure */
- if (numfree &&
- (type == &PySet_Type || type == &PyFrozenSet_Type)) {
- so = free_list[--numfree];
- assert (so != NULL && PyAnySet_CheckExact(so));
- Py_TYPE(so) = type;
- _Py_NewReference((PyObject *)so);
- EMPTY_TO_MINSIZE(so);
- PyObject_GC_Track(so);
- } else {
- so = (PySetObject *)type->tp_alloc(type, 0);
- if (so == NULL)
- return NULL;
- /* tp_alloc has already zeroed the structure */
- assert(so->table == NULL && so->fill == 0 && so->used == 0);
- INIT_NONZERO_SET_SLOTS(so);
- }
-
- so->lookup = set_lookkey_unicode;
- so->weakreflist = NULL;
-
- if (iterable != NULL) {
- if (set_update_internal(so, iterable) == -1) {
- Py_DECREF(so);
- return NULL;
- }
- }
-
- return (PyObject *)so;
+ register PySetObject *so = NULL;
+
+ if (dummy == NULL) { /* Auto-initialize dummy */
+ dummy = PyUnicode_FromString("<dummy key>");
+ if (dummy == NULL)
+ return NULL;
+ }
+
+ /* create PySetObject structure */
+ if (numfree &&
+ (type == &PySet_Type || type == &PyFrozenSet_Type)) {
+ so = free_list[--numfree];
+ assert (so != NULL && PyAnySet_CheckExact(so));
+ Py_TYPE(so) = type;
+ _Py_NewReference((PyObject *)so);
+ EMPTY_TO_MINSIZE(so);
+ PyObject_GC_Track(so);
+ } else {
+ so = (PySetObject *)type->tp_alloc(type, 0);
+ if (so == NULL)
+ return NULL;
+ /* tp_alloc has already zeroed the structure */
+ assert(so->table == NULL && so->fill == 0 && so->used == 0);
+ INIT_NONZERO_SET_SLOTS(so);
+ }
+
+ so->lookup = set_lookkey_unicode;
+ so->weakreflist = NULL;
+
+ if (iterable != NULL) {
+ if (set_update_internal(so, iterable) == -1) {
+ Py_DECREF(so);
+ return NULL;
+ }
+ }
+
+ return (PyObject *)so;
}
static PyObject *
make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
{
- if (type != &PySet_Type && type != &PyFrozenSet_Type) {
- if (PyType_IsSubtype(type, &PySet_Type))
- type = &PySet_Type;
- else
- type = &PyFrozenSet_Type;
- }
- return make_new_set(type, iterable);
+ if (type != &PySet_Type && type != &PyFrozenSet_Type) {
+ if (PyType_IsSubtype(type, &PySet_Type))
+ type = &PySet_Type;
+ else
+ type = &PyFrozenSet_Type;
+ }
+ return make_new_set(type, iterable);
}
/* The empty frozenset is a singleton */
@@ -1041,56 +1041,56 @@ static PyObject *emptyfrozenset = NULL;
static PyObject *
frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *iterable = NULL, *result;
+ PyObject *iterable = NULL, *result;
- if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
- return NULL;
+ if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
+ return NULL;
- if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
- return NULL;
+ if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
+ return NULL;
- if (type != &PyFrozenSet_Type)
- return make_new_set(type, iterable);
+ if (type != &PyFrozenSet_Type)
+ return make_new_set(type, iterable);
- if (iterable != NULL) {
- /* frozenset(f) is idempotent */
- if (PyFrozenSet_CheckExact(iterable)) {
- Py_INCREF(iterable);
- return iterable;
- }
- result = make_new_set(type, iterable);
- if (result == NULL || PySet_GET_SIZE(result))
- return result;
- Py_DECREF(result);
- }
- /* The empty frozenset is a singleton */
- if (emptyfrozenset == NULL)
- emptyfrozenset = make_new_set(type, NULL);
- Py_XINCREF(emptyfrozenset);
- return emptyfrozenset;
+ if (iterable != NULL) {
+ /* frozenset(f) is idempotent */
+ if (PyFrozenSet_CheckExact(iterable)) {
+ Py_INCREF(iterable);
+ return iterable;
+ }
+ result = make_new_set(type, iterable);
+ if (result == NULL || PySet_GET_SIZE(result))
+ return result;
+ Py_DECREF(result);
+ }
+ /* The empty frozenset is a singleton */
+ if (emptyfrozenset == NULL)
+ emptyfrozenset = make_new_set(type, NULL);
+ Py_XINCREF(emptyfrozenset);
+ return emptyfrozenset;
}
void
PySet_Fini(void)
{
- PySetObject *so;
+ PySetObject *so;
- while (numfree) {
- numfree--;
- so = free_list[numfree];
- PyObject_GC_Del(so);
- }
- Py_CLEAR(dummy);
- Py_CLEAR(emptyfrozenset);
+ while (numfree) {
+ numfree--;
+ so = free_list[numfree];
+ PyObject_GC_Del(so);
+ }
+ Py_CLEAR(dummy);
+ Py_CLEAR(emptyfrozenset);
}
static PyObject *
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
- return NULL;
-
- return make_new_set(type, NULL);
+ if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
+ return NULL;
+
+ return make_new_set(type, NULL);
}
/* set_swap_bodies() switches the contents of any two sets by moving their
@@ -1100,64 +1100,64 @@ set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
The function always succeeds and it leaves both objects in a stable state.
- Useful for creating temporary frozensets from sets for membership testing
+ Useful for creating temporary frozensets from sets for membership testing
in __contains__(), discard(), and remove(). Also useful for operations
- that update in-place (by allowing an intermediate result to be swapped
+ that update in-place (by allowing an intermediate result to be swapped
into one of the original inputs).
*/
static void
set_swap_bodies(PySetObject *a, PySetObject *b)
{
- Py_ssize_t t;
- setentry *u;
- setentry *(*f)(PySetObject *so, PyObject *key, long hash);
- setentry tab[PySet_MINSIZE];
- long h;
-
- t = a->fill; a->fill = b->fill; b->fill = t;
- t = a->used; a->used = b->used; b->used = t;
- t = a->mask; a->mask = b->mask; b->mask = t;
-
- u = a->table;
- if (a->table == a->smalltable)
- u = b->smalltable;
- a->table = b->table;
- if (b->table == b->smalltable)
- a->table = a->smalltable;
- b->table = u;
-
- f = a->lookup; a->lookup = b->lookup; b->lookup = f;
-
- if (a->table == a->smalltable || b->table == b->smalltable) {
- memcpy(tab, a->smalltable, sizeof(tab));
- memcpy(a->smalltable, b->smalltable, sizeof(tab));
- memcpy(b->smalltable, tab, sizeof(tab));
- }
-
- if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
- PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
- h = a->hash; a->hash = b->hash; b->hash = h;
- } else {
- a->hash = -1;
- b->hash = -1;
- }
+ Py_ssize_t t;
+ setentry *u;
+ setentry *(*f)(PySetObject *so, PyObject *key, long hash);
+ setentry tab[PySet_MINSIZE];
+ long h;
+
+ t = a->fill; a->fill = b->fill; b->fill = t;
+ t = a->used; a->used = b->used; b->used = t;
+ t = a->mask; a->mask = b->mask; b->mask = t;
+
+ u = a->table;
+ if (a->table == a->smalltable)
+ u = b->smalltable;
+ a->table = b->table;
+ if (b->table == b->smalltable)
+ a->table = a->smalltable;
+ b->table = u;
+
+ f = a->lookup; a->lookup = b->lookup; b->lookup = f;
+
+ if (a->table == a->smalltable || b->table == b->smalltable) {
+ memcpy(tab, a->smalltable, sizeof(tab));
+ memcpy(a->smalltable, b->smalltable, sizeof(tab));
+ memcpy(b->smalltable, tab, sizeof(tab));
+ }
+
+ if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
+ PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
+ h = a->hash; a->hash = b->hash; b->hash = h;
+ } else {
+ a->hash = -1;
+ b->hash = -1;
+ }
}
static PyObject *
set_copy(PySetObject *so)
{
- return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
+ return make_new_set_basetype(Py_TYPE(so), (PyObject *)so);
}
static PyObject *
frozenset_copy(PySetObject *so)
{
- if (PyFrozenSet_CheckExact(so)) {
- Py_INCREF(so);
- return (PyObject *)so;
- }
- return set_copy(so);
+ if (PyFrozenSet_CheckExact(so)) {
+ Py_INCREF(so);
+ return (PyObject *)so;
+ }
+ return set_copy(so);
}
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
@@ -1165,8 +1165,8 @@ PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
static PyObject *
set_clear(PySetObject *so)
{
- set_clear_internal(so);
- Py_RETURN_NONE;
+ set_clear_internal(so);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
@@ -1174,24 +1174,24 @@ PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
static PyObject *
set_union(PySetObject *so, PyObject *args)
{
- PySetObject *result;
- PyObject *other;
- Py_ssize_t i;
+ PySetObject *result;
+ PyObject *other;
+ Py_ssize_t i;
- result = (PySetObject *)set_copy(so);
- if (result == NULL)
- return NULL;
+ result = (PySetObject *)set_copy(so);
+ if (result == NULL)
+ return NULL;
- for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
- other = PyTuple_GET_ITEM(args, i);
- if ((PyObject *)so == other)
- continue;
- if (set_update_internal(result, other) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- return (PyObject *)result;
+ for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
+ other = PyTuple_GET_ITEM(args, i);
+ if ((PyObject *)so == other)
+ continue;
+ if (set_update_internal(result, other) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ return (PyObject *)result;
}
PyDoc_STRVAR(union_doc,
@@ -1202,142 +1202,142 @@ PyDoc_STRVAR(union_doc,
static PyObject *
set_or(PySetObject *so, PyObject *other)
{
- PySetObject *result;
+ PySetObject *result;
- if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
- result = (PySetObject *)set_copy(so);
- if (result == NULL)
- return NULL;
- if ((PyObject *)so == other)
- return (PyObject *)result;
- if (set_update_internal(result, other) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
+ result = (PySetObject *)set_copy(so);
+ if (result == NULL)
+ return NULL;
+ if ((PyObject *)so == other)
+ return (PyObject *)result;
+ if (set_update_internal(result, other) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ return (PyObject *)result;
}
static PyObject *
set_ior(PySetObject *so, PyObject *other)
{
- if (!PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- if (set_update_internal(so, other) == -1)
- return NULL;
- Py_INCREF(so);
- return (PyObject *)so;
+ if (!PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ if (set_update_internal(so, other) == -1)
+ return NULL;
+ Py_INCREF(so);
+ return (PyObject *)so;
}
static PyObject *
set_intersection(PySetObject *so, PyObject *other)
{
- PySetObject *result;
- PyObject *key, *it, *tmp;
-
- if ((PyObject *)so == other)
- return set_copy(so);
-
- result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
- if (result == NULL)
- return NULL;
-
- if (PyAnySet_Check(other)) {
- Py_ssize_t pos = 0;
- setentry *entry;
-
- if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
- tmp = (PyObject *)so;
- so = (PySetObject *)other;
- other = tmp;
- }
-
- while (set_next((PySetObject *)other, &pos, &entry)) {
- int rv = set_contains_entry(so, entry);
- if (rv == -1) {
- Py_DECREF(result);
- return NULL;
- }
- if (rv) {
- if (set_add_entry(result, entry) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- }
- return (PyObject *)result;
- }
-
- it = PyObject_GetIter(other);
- if (it == NULL) {
- Py_DECREF(result);
- return NULL;
- }
-
- while ((key = PyIter_Next(it)) != NULL) {
- int rv;
- setentry entry;
- long hash = PyObject_Hash(key);
-
- if (hash == -1) {
- Py_DECREF(it);
- Py_DECREF(result);
- Py_DECREF(key);
- return NULL;
- }
- entry.hash = hash;
- entry.key = key;
- rv = set_contains_entry(so, &entry);
- if (rv == -1) {
- Py_DECREF(it);
- Py_DECREF(result);
- Py_DECREF(key);
- return NULL;
- }
- if (rv) {
- if (set_add_entry(result, &entry) == -1) {
- Py_DECREF(it);
- Py_DECREF(result);
- Py_DECREF(key);
- return NULL;
- }
- }
- Py_DECREF(key);
- }
- Py_DECREF(it);
- if (PyErr_Occurred()) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
+ PySetObject *result;
+ PyObject *key, *it, *tmp;
+
+ if ((PyObject *)so == other)
+ return set_copy(so);
+
+ result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
+ if (result == NULL)
+ return NULL;
+
+ if (PyAnySet_Check(other)) {
+ Py_ssize_t pos = 0;
+ setentry *entry;
+
+ if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
+ tmp = (PyObject *)so;
+ so = (PySetObject *)other;
+ other = tmp;
+ }
+
+ while (set_next((PySetObject *)other, &pos, &entry)) {
+ int rv = set_contains_entry(so, entry);
+ if (rv == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ if (rv) {
+ if (set_add_entry(result, entry) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ }
+ return (PyObject *)result;
+ }
+
+ it = PyObject_GetIter(other);
+ if (it == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ while ((key = PyIter_Next(it)) != NULL) {
+ int rv;
+ setentry entry;
+ long hash = PyObject_Hash(key);
+
+ if (hash == -1) {
+ Py_DECREF(it);
+ Py_DECREF(result);
+ Py_DECREF(key);
+ return NULL;
+ }
+ entry.hash = hash;
+ entry.key = key;
+ rv = set_contains_entry(so, &entry);
+ if (rv == -1) {
+ Py_DECREF(it);
+ Py_DECREF(result);
+ Py_DECREF(key);
+ return NULL;
+ }
+ if (rv) {
+ if (set_add_entry(result, &entry) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(result);
+ Py_DECREF(key);
+ return NULL;
+ }
+ }
+ Py_DECREF(key);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ return (PyObject *)result;
}
static PyObject *
set_intersection_multi(PySetObject *so, PyObject *args)
{
- Py_ssize_t i;
- PyObject *result = (PyObject *)so;
+ Py_ssize_t i;
+ PyObject *result = (PyObject *)so;
- if (PyTuple_GET_SIZE(args) == 0)
- return set_copy(so);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return set_copy(so);
- Py_INCREF(so);
- for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
- PyObject *other = PyTuple_GET_ITEM(args, i);
- PyObject *newresult = set_intersection((PySetObject *)result, other);
- if (newresult == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(result);
- result = newresult;
- }
- return result;
+ Py_INCREF(so);
+ for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
+ PyObject *other = PyTuple_GET_ITEM(args, i);
+ PyObject *newresult = set_intersection((PySetObject *)result, other);
+ if (newresult == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(result);
+ result = newresult;
+ }
+ return result;
}
PyDoc_STRVAR(intersection_doc,
@@ -1348,27 +1348,27 @@ PyDoc_STRVAR(intersection_doc,
static PyObject *
set_intersection_update(PySetObject *so, PyObject *other)
{
- PyObject *tmp;
+ PyObject *tmp;
- tmp = set_intersection(so, other);
- if (tmp == NULL)
- return NULL;
- set_swap_bodies(so, (PySetObject *)tmp);
- Py_DECREF(tmp);
- Py_RETURN_NONE;
+ tmp = set_intersection(so, other);
+ if (tmp == NULL)
+ return NULL;
+ set_swap_bodies(so, (PySetObject *)tmp);
+ Py_DECREF(tmp);
+ Py_RETURN_NONE;
}
static PyObject *
set_intersection_update_multi(PySetObject *so, PyObject *args)
{
- PyObject *tmp;
+ PyObject *tmp;
- tmp = set_intersection_multi(so, args);
- if (tmp == NULL)
- return NULL;
- set_swap_bodies(so, (PySetObject *)tmp);
- Py_DECREF(tmp);
- Py_RETURN_NONE;
+ tmp = set_intersection_multi(so, args);
+ if (tmp == NULL)
+ return NULL;
+ set_swap_bodies(so, (PySetObject *)tmp);
+ Py_DECREF(tmp);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(intersection_update_doc,
@@ -1377,92 +1377,92 @@ PyDoc_STRVAR(intersection_update_doc,
static PyObject *
set_and(PySetObject *so, PyObject *other)
{
- if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- return set_intersection(so, other);
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ return set_intersection(so, other);
}
static PyObject *
set_iand(PySetObject *so, PyObject *other)
{
- PyObject *result;
+ PyObject *result;
- if (!PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- result = set_intersection_update(so, other);
- if (result == NULL)
- return NULL;
- Py_DECREF(result);
- Py_INCREF(so);
- return (PyObject *)so;
+ if (!PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ result = set_intersection_update(so, other);
+ if (result == NULL)
+ return NULL;
+ Py_DECREF(result);
+ Py_INCREF(so);
+ return (PyObject *)so;
}
static PyObject *
set_isdisjoint(PySetObject *so, PyObject *other)
{
- PyObject *key, *it, *tmp;
-
- if ((PyObject *)so == other) {
- if (PySet_GET_SIZE(so) == 0)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
- }
-
- if (PyAnySet_CheckExact(other)) {
- Py_ssize_t pos = 0;
- setentry *entry;
-
- if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
- tmp = (PyObject *)so;
- so = (PySetObject *)other;
- other = tmp;
- }
- while (set_next((PySetObject *)other, &pos, &entry)) {
- int rv = set_contains_entry(so, entry);
- if (rv == -1)
- return NULL;
- if (rv)
- Py_RETURN_FALSE;
- }
- Py_RETURN_TRUE;
- }
-
- it = PyObject_GetIter(other);
- if (it == NULL)
- return NULL;
-
- while ((key = PyIter_Next(it)) != NULL) {
- int rv;
- setentry entry;
- long hash = PyObject_Hash(key);;
-
- if (hash == -1) {
- Py_DECREF(key);
- Py_DECREF(it);
- return NULL;
- }
- entry.hash = hash;
- entry.key = key;
- rv = set_contains_entry(so, &entry);
- Py_DECREF(key);
- if (rv == -1) {
- Py_DECREF(it);
- return NULL;
- }
- if (rv) {
- Py_DECREF(it);
- Py_RETURN_FALSE;
- }
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return NULL;
- Py_RETURN_TRUE;
+ PyObject *key, *it, *tmp;
+
+ if ((PyObject *)so == other) {
+ if (PySet_GET_SIZE(so) == 0)
+ Py_RETURN_TRUE;
+ else
+ Py_RETURN_FALSE;
+ }
+
+ if (PyAnySet_CheckExact(other)) {
+ Py_ssize_t pos = 0;
+ setentry *entry;
+
+ if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
+ tmp = (PyObject *)so;
+ so = (PySetObject *)other;
+ other = tmp;
+ }
+ while (set_next((PySetObject *)other, &pos, &entry)) {
+ int rv = set_contains_entry(so, entry);
+ if (rv == -1)
+ return NULL;
+ if (rv)
+ Py_RETURN_FALSE;
+ }
+ Py_RETURN_TRUE;
+ }
+
+ it = PyObject_GetIter(other);
+ if (it == NULL)
+ return NULL;
+
+ while ((key = PyIter_Next(it)) != NULL) {
+ int rv;
+ setentry entry;
+ long hash = PyObject_Hash(key);;
+
+ if (hash == -1) {
+ Py_DECREF(key);
+ Py_DECREF(it);
+ return NULL;
+ }
+ entry.hash = hash;
+ entry.key = key;
+ rv = set_contains_entry(so, &entry);
+ Py_DECREF(key);
+ if (rv == -1) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ if (rv) {
+ Py_DECREF(it);
+ Py_RETURN_FALSE;
+ }
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return NULL;
+ Py_RETURN_TRUE;
}
PyDoc_STRVAR(isdisjoint_doc,
@@ -1471,51 +1471,51 @@ PyDoc_STRVAR(isdisjoint_doc,
static int
set_difference_update_internal(PySetObject *so, PyObject *other)
{
- if ((PyObject *)so == other)
- return set_clear_internal(so);
-
- if (PyAnySet_Check(other)) {
- setentry *entry;
- Py_ssize_t pos = 0;
-
- while (set_next((PySetObject *)other, &pos, &entry))
- if (set_discard_entry(so, entry) == -1)
- return -1;
- } else {
- PyObject *key, *it;
- it = PyObject_GetIter(other);
- if (it == NULL)
- return -1;
-
- while ((key = PyIter_Next(it)) != NULL) {
- if (set_discard_key(so, key) == -1) {
- Py_DECREF(it);
- Py_DECREF(key);
- return -1;
- }
- Py_DECREF(key);
- }
- Py_DECREF(it);
- if (PyErr_Occurred())
- return -1;
- }
- /* If more than 1/5 are dummies, then resize them away. */
- if ((so->fill - so->used) * 5 < so->mask)
- return 0;
- return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
+ if ((PyObject *)so == other)
+ return set_clear_internal(so);
+
+ if (PyAnySet_Check(other)) {
+ setentry *entry;
+ Py_ssize_t pos = 0;
+
+ while (set_next((PySetObject *)other, &pos, &entry))
+ if (set_discard_entry(so, entry) == -1)
+ return -1;
+ } else {
+ PyObject *key, *it;
+ it = PyObject_GetIter(other);
+ if (it == NULL)
+ return -1;
+
+ while ((key = PyIter_Next(it)) != NULL) {
+ if (set_discard_key(so, key) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(key);
+ return -1;
+ }
+ Py_DECREF(key);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred())
+ return -1;
+ }
+ /* If more than 1/5 are dummies, then resize them away. */
+ if ((so->fill - so->used) * 5 < so->mask)
+ return 0;
+ return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
}
static PyObject *
set_difference_update(PySetObject *so, PyObject *args)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
- PyObject *other = PyTuple_GET_ITEM(args, i);
- if (set_difference_update_internal(so, other) == -1)
- return NULL;
- }
- Py_RETURN_NONE;
+ for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
+ PyObject *other = PyTuple_GET_ITEM(args, i);
+ if (set_difference_update_internal(so, other) == -1)
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(difference_update_doc,
@@ -1524,77 +1524,77 @@ PyDoc_STRVAR(difference_update_doc,
static PyObject *
set_difference(PySetObject *so, PyObject *other)
{
- PyObject *result;
- setentry *entry;
- Py_ssize_t pos = 0;
-
- if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
- result = set_copy(so);
- if (result == NULL)
- return NULL;
- if (set_difference_update_internal((PySetObject *)result, other) != -1)
- return result;
- Py_DECREF(result);
- return NULL;
- }
-
- result = make_new_set_basetype(Py_TYPE(so), NULL);
- if (result == NULL)
- return NULL;
-
- if (PyDict_CheckExact(other)) {
- while (set_next(so, &pos, &entry)) {
- setentry entrycopy;
- entrycopy.hash = entry->hash;
- entrycopy.key = entry->key;
- if (!_PyDict_Contains(other, entry->key, entry->hash)) {
- if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- }
- return result;
- }
-
- while (set_next(so, &pos, &entry)) {
- int rv = set_contains_entry((PySetObject *)other, entry);
- if (rv == -1) {
- Py_DECREF(result);
- return NULL;
- }
- if (!rv) {
- if (set_add_entry((PySetObject *)result, entry) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- }
- return result;
+ PyObject *result;
+ setentry *entry;
+ Py_ssize_t pos = 0;
+
+ if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
+ result = set_copy(so);
+ if (result == NULL)
+ return NULL;
+ if (set_difference_update_internal((PySetObject *)result, other) != -1)
+ return result;
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ result = make_new_set_basetype(Py_TYPE(so), NULL);
+ if (result == NULL)
+ return NULL;
+
+ if (PyDict_CheckExact(other)) {
+ while (set_next(so, &pos, &entry)) {
+ setentry entrycopy;
+ entrycopy.hash = entry->hash;
+ entrycopy.key = entry->key;
+ if (!_PyDict_Contains(other, entry->key, entry->hash)) {
+ if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ }
+ return result;
+ }
+
+ while (set_next(so, &pos, &entry)) {
+ int rv = set_contains_entry((PySetObject *)other, entry);
+ if (rv == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ if (!rv) {
+ if (set_add_entry((PySetObject *)result, entry) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ }
+ return result;
}
static PyObject *
set_difference_multi(PySetObject *so, PyObject *args)
{
- Py_ssize_t i;
- PyObject *result, *other;
+ Py_ssize_t i;
+ PyObject *result, *other;
- if (PyTuple_GET_SIZE(args) == 0)
- return set_copy(so);
+ if (PyTuple_GET_SIZE(args) == 0)
+ return set_copy(so);
- other = PyTuple_GET_ITEM(args, 0);
- result = set_difference(so, other);
- if (result == NULL)
- return NULL;
+ other = PyTuple_GET_ITEM(args, 0);
+ result = set_difference(so, other);
+ if (result == NULL)
+ return NULL;
- for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
- other = PyTuple_GET_ITEM(args, i);
- if (set_difference_update_internal((PySetObject *)result, other) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- }
- return result;
+ for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
+ other = PyTuple_GET_ITEM(args, i);
+ if (set_difference_update_internal((PySetObject *)result, other) == -1) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ }
+ return result;
}
PyDoc_STRVAR(difference_doc,
@@ -1604,81 +1604,81 @@ PyDoc_STRVAR(difference_doc,
static PyObject *
set_sub(PySetObject *so, PyObject *other)
{
- if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- return set_difference(so, other);
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ return set_difference(so, other);
}
static PyObject *
set_isub(PySetObject *so, PyObject *other)
{
- if (!PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- if (set_difference_update_internal(so, other) == -1)
- return NULL;
- Py_INCREF(so);
- return (PyObject *)so;
+ if (!PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ if (set_difference_update_internal(so, other) == -1)
+ return NULL;
+ Py_INCREF(so);
+ return (PyObject *)so;
}
static PyObject *
set_symmetric_difference_update(PySetObject *so, PyObject *other)
{
- PySetObject *otherset;
- PyObject *key;
- Py_ssize_t pos = 0;
- setentry *entry;
-
- if ((PyObject *)so == other)
- return set_clear(so);
-
- if (PyDict_CheckExact(other)) {
- PyObject *value;
- int rv;
- long hash;
- while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
- setentry an_entry;
-
- an_entry.hash = hash;
- an_entry.key = key;
- rv = set_discard_entry(so, &an_entry);
- if (rv == -1)
- return NULL;
- if (rv == DISCARD_NOTFOUND) {
- if (set_add_entry(so, &an_entry) == -1)
- return NULL;
- }
- }
- Py_RETURN_NONE;
- }
-
- if (PyAnySet_Check(other)) {
- Py_INCREF(other);
- otherset = (PySetObject *)other;
- } else {
- otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
- if (otherset == NULL)
- return NULL;
- }
-
- while (set_next(otherset, &pos, &entry)) {
- int rv = set_discard_entry(so, entry);
- if (rv == -1) {
- Py_DECREF(otherset);
- return NULL;
- }
- if (rv == DISCARD_NOTFOUND) {
- if (set_add_entry(so, entry) == -1) {
- Py_DECREF(otherset);
- return NULL;
- }
- }
- }
- Py_DECREF(otherset);
- Py_RETURN_NONE;
+ PySetObject *otherset;
+ PyObject *key;
+ Py_ssize_t pos = 0;
+ setentry *entry;
+
+ if ((PyObject *)so == other)
+ return set_clear(so);
+
+ if (PyDict_CheckExact(other)) {
+ PyObject *value;
+ int rv;
+ long hash;
+ while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
+ setentry an_entry;
+
+ an_entry.hash = hash;
+ an_entry.key = key;
+ rv = set_discard_entry(so, &an_entry);
+ if (rv == -1)
+ return NULL;
+ if (rv == DISCARD_NOTFOUND) {
+ if (set_add_entry(so, &an_entry) == -1)
+ return NULL;
+ }
+ }
+ Py_RETURN_NONE;
+ }
+
+ if (PyAnySet_Check(other)) {
+ Py_INCREF(other);
+ otherset = (PySetObject *)other;
+ } else {
+ otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
+ if (otherset == NULL)
+ return NULL;
+ }
+
+ while (set_next(otherset, &pos, &entry)) {
+ int rv = set_discard_entry(so, entry);
+ if (rv == -1) {
+ Py_DECREF(otherset);
+ return NULL;
+ }
+ if (rv == DISCARD_NOTFOUND) {
+ if (set_add_entry(so, entry) == -1) {
+ Py_DECREF(otherset);
+ return NULL;
+ }
+ }
+ }
+ Py_DECREF(otherset);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(symmetric_difference_update_doc,
@@ -1687,17 +1687,17 @@ PyDoc_STRVAR(symmetric_difference_update_doc,
static PyObject *
set_symmetric_difference(PySetObject *so, PyObject *other)
{
- PyObject *rv;
- PySetObject *otherset;
+ PyObject *rv;
+ PySetObject *otherset;
- otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
- if (otherset == NULL)
- return NULL;
- rv = set_symmetric_difference_update(otherset, (PyObject *)so);
- if (rv == NULL)
- return NULL;
- Py_DECREF(rv);
- return (PyObject *)otherset;
+ otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
+ if (otherset == NULL)
+ return NULL;
+ rv = set_symmetric_difference_update(otherset, (PyObject *)so);
+ if (rv == NULL)
+ return NULL;
+ Py_DECREF(rv);
+ return (PyObject *)otherset;
}
PyDoc_STRVAR(symmetric_difference_doc,
@@ -1708,56 +1708,56 @@ PyDoc_STRVAR(symmetric_difference_doc,
static PyObject *
set_xor(PySetObject *so, PyObject *other)
{
- if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- return set_symmetric_difference(so, other);
+ if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ return set_symmetric_difference(so, other);
}
static PyObject *
set_ixor(PySetObject *so, PyObject *other)
{
- PyObject *result;
+ PyObject *result;
- if (!PyAnySet_Check(other)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- result = set_symmetric_difference_update(so, other);
- if (result == NULL)
- return NULL;
- Py_DECREF(result);
- Py_INCREF(so);
- return (PyObject *)so;
+ if (!PyAnySet_Check(other)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ result = set_symmetric_difference_update(so, other);
+ if (result == NULL)
+ return NULL;
+ Py_DECREF(result);
+ Py_INCREF(so);
+ return (PyObject *)so;
}
static PyObject *
set_issubset(PySetObject *so, PyObject *other)
{
- setentry *entry;
- Py_ssize_t pos = 0;
-
- if (!PyAnySet_Check(other)) {
- PyObject *tmp, *result;
- tmp = make_new_set(&PySet_Type, other);
- if (tmp == NULL)
- return NULL;
- result = set_issubset(so, tmp);
- Py_DECREF(tmp);
- return result;
- }
- if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
- Py_RETURN_FALSE;
-
- while (set_next(so, &pos, &entry)) {
- int rv = set_contains_entry((PySetObject *)other, entry);
- if (rv == -1)
- return NULL;
- if (!rv)
- Py_RETURN_FALSE;
- }
- Py_RETURN_TRUE;
+ setentry *entry;
+ Py_ssize_t pos = 0;
+
+ if (!PyAnySet_Check(other)) {
+ PyObject *tmp, *result;
+ tmp = make_new_set(&PySet_Type, other);
+ if (tmp == NULL)
+ return NULL;
+ result = set_issubset(so, tmp);
+ Py_DECREF(tmp);
+ return result;
+ }
+ if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
+ Py_RETURN_FALSE;
+
+ while (set_next(so, &pos, &entry)) {
+ int rv = set_contains_entry((PySetObject *)other, entry);
+ if (rv == -1)
+ return NULL;
+ if (!rv)
+ Py_RETURN_FALSE;
+ }
+ Py_RETURN_TRUE;
}
PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
@@ -1765,17 +1765,17 @@ PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
static PyObject *
set_issuperset(PySetObject *so, PyObject *other)
{
- PyObject *tmp, *result;
+ PyObject *tmp, *result;
- if (!PyAnySet_Check(other)) {
- tmp = make_new_set(&PySet_Type, other);
- if (tmp == NULL)
- return NULL;
- result = set_issuperset(so, tmp);
- Py_DECREF(tmp);
- return result;
- }
- return set_issubset((PySetObject *)other, (PyObject *)so);
+ if (!PyAnySet_Check(other)) {
+ tmp = make_new_set(&PySet_Type, other);
+ if (tmp == NULL)
+ return NULL;
+ result = set_issuperset(so, tmp);
+ Py_DECREF(tmp);
+ return result;
+ }
+ return set_issubset((PySetObject *)other, (PyObject *)so);
}
PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
@@ -1783,54 +1783,54 @@ PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
static PyObject *
set_richcompare(PySetObject *v, PyObject *w, int op)
{
- PyObject *r1, *r2;
-
- if(!PyAnySet_Check(w)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- switch (op) {
- case Py_EQ:
- if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
- Py_RETURN_FALSE;
- if (v->hash != -1 &&
- ((PySetObject *)w)->hash != -1 &&
- v->hash != ((PySetObject *)w)->hash)
- Py_RETURN_FALSE;
- return set_issubset(v, w);
- case Py_NE:
- r1 = set_richcompare(v, w, Py_EQ);
- if (r1 == NULL)
- return NULL;
- r2 = PyBool_FromLong(PyObject_Not(r1));
- Py_DECREF(r1);
- return r2;
- case Py_LE:
- return set_issubset(v, w);
- case Py_GE:
- return set_issuperset(v, w);
- case Py_LT:
- if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
- Py_RETURN_FALSE;
- return set_issubset(v, w);
- case Py_GT:
- if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
- Py_RETURN_FALSE;
- return set_issuperset(v, w);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ PyObject *r1, *r2;
+
+ if(!PyAnySet_Check(w)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ switch (op) {
+ case Py_EQ:
+ if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
+ Py_RETURN_FALSE;
+ if (v->hash != -1 &&
+ ((PySetObject *)w)->hash != -1 &&
+ v->hash != ((PySetObject *)w)->hash)
+ Py_RETURN_FALSE;
+ return set_issubset(v, w);
+ case Py_NE:
+ r1 = set_richcompare(v, w, Py_EQ);
+ if (r1 == NULL)
+ return NULL;
+ r2 = PyBool_FromLong(PyObject_Not(r1));
+ Py_DECREF(r1);
+ return r2;
+ case Py_LE:
+ return set_issubset(v, w);
+ case Py_GE:
+ return set_issuperset(v, w);
+ case Py_LT:
+ if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
+ Py_RETURN_FALSE;
+ return set_issubset(v, w);
+ case Py_GT:
+ if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
+ Py_RETURN_FALSE;
+ return set_issuperset(v, w);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyObject *
set_add(PySetObject *so, PyObject *key)
{
- if (set_add_key(so, key) == -1)
- return NULL;
- Py_RETURN_NONE;
+ if (set_add_key(so, key) == -1)
+ return NULL;
+ Py_RETURN_NONE;
}
-PyDoc_STRVAR(add_doc,
+PyDoc_STRVAR(add_doc,
"Add an element to a set.\n\
\n\
This has no effect if the element is already present.");
@@ -1838,34 +1838,34 @@ This has no effect if the element is already present.");
static int
set_contains(PySetObject *so, PyObject *key)
{
- PyObject *tmpkey;
- int rv;
-
- rv = set_contains_key(so, key);
- if (rv == -1) {
- if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
- return -1;
- PyErr_Clear();
- tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
- if (tmpkey == NULL)
- return -1;
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- rv = set_contains(so, tmpkey);
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- Py_DECREF(tmpkey);
- }
- return rv;
+ PyObject *tmpkey;
+ int rv;
+
+ rv = set_contains_key(so, key);
+ if (rv == -1) {
+ if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
+ return -1;
+ PyErr_Clear();
+ tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
+ if (tmpkey == NULL)
+ return -1;
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ rv = set_contains(so, tmpkey);
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ Py_DECREF(tmpkey);
+ }
+ return rv;
}
static PyObject *
set_direct_contains(PySetObject *so, PyObject *key)
{
- long result;
+ long result;
- result = set_contains(so, key);
- if (result == -1)
- return NULL;
- return PyBool_FromLong(result);
+ result = set_contains(so, key);
+ if (result == -1)
+ return NULL;
+ return PyBool_FromLong(result);
}
PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
@@ -1873,30 +1873,30 @@ PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
static PyObject *
set_remove(PySetObject *so, PyObject *key)
{
- PyObject *tmpkey;
- int rv;
-
- rv = set_discard_key(so, key);
- if (rv == -1) {
- if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
- return NULL;
- PyErr_Clear();
- tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
- if (tmpkey == NULL)
- return NULL;
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- rv = set_discard_key(so, tmpkey);
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- Py_DECREF(tmpkey);
- if (rv == -1)
- return NULL;
- }
-
- if (rv == DISCARD_NOTFOUND) {
- set_key_error(key);
- return NULL;
- }
- Py_RETURN_NONE;
+ PyObject *tmpkey;
+ int rv;
+
+ rv = set_discard_key(so, key);
+ if (rv == -1) {
+ if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
+ return NULL;
+ PyErr_Clear();
+ tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
+ if (tmpkey == NULL)
+ return NULL;
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ rv = set_discard_key(so, tmpkey);
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ Py_DECREF(tmpkey);
+ if (rv == -1)
+ return NULL;
+ }
+
+ if (rv == DISCARD_NOTFOUND) {
+ set_key_error(key);
+ return NULL;
+ }
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(remove_doc,
@@ -1907,54 +1907,54 @@ If the element is not a member, raise a KeyError.");
static PyObject *
set_discard(PySetObject *so, PyObject *key)
{
- PyObject *tmpkey, *result;
- int rv;
-
- rv = set_discard_key(so, key);
- if (rv == -1) {
- if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
- return NULL;
- PyErr_Clear();
- tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
- if (tmpkey == NULL)
- return NULL;
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- result = set_discard(so, tmpkey);
- set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
- Py_DECREF(tmpkey);
- return result;
- }
- Py_RETURN_NONE;
+ PyObject *tmpkey, *result;
+ int rv;
+
+ rv = set_discard_key(so, key);
+ if (rv == -1) {
+ if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
+ return NULL;
+ PyErr_Clear();
+ tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
+ if (tmpkey == NULL)
+ return NULL;
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ result = set_discard(so, tmpkey);
+ set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
+ Py_DECREF(tmpkey);
+ return result;
+ }
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(discard_doc,
"Remove an element from a set if it is a member.\n\
\n\
-If the element is not a member, do nothing.");
+If the element is not a member, do nothing.");
static PyObject *
set_reduce(PySetObject *so)
{
- PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
-
- keys = PySequence_List((PyObject *)so);
- if (keys == NULL)
- goto done;
- args = PyTuple_Pack(1, keys);
- if (args == NULL)
- goto done;
- dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
- if (dict == NULL) {
- PyErr_Clear();
- dict = Py_None;
- Py_INCREF(dict);
- }
- result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
+ PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
+
+ keys = PySequence_List((PyObject *)so);
+ if (keys == NULL)
+ goto done;
+ args = PyTuple_Pack(1, keys);
+ if (args == NULL)
+ goto done;
+ dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
+ if (dict == NULL) {
+ PyErr_Clear();
+ dict = Py_None;
+ Py_INCREF(dict);
+ }
+ result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
done:
- Py_XDECREF(args);
- Py_XDECREF(keys);
- Py_XDECREF(dict);
- return result;
+ Py_XDECREF(args);
+ Py_XDECREF(keys);
+ Py_XDECREF(dict);
+ return result;
}
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
@@ -1962,42 +1962,42 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyObject *
set_sizeof(PySetObject *so)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = sizeof(PySetObject);
- if (so->table != so->smalltable)
- res = res + (so->mask + 1) * sizeof(setentry);
- return PyLong_FromSsize_t(res);
+ res = sizeof(PySetObject);
+ if (so->table != so->smalltable)
+ res = res + (so->mask + 1) * sizeof(setentry);
+ return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
static int
set_init(PySetObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *iterable = NULL;
+ PyObject *iterable = NULL;
- if (!PyAnySet_Check(self))
- return -1;
- if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
- return -1;
- if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
- return -1;
- set_clear_internal(self);
- self->hash = -1;
- if (iterable == NULL)
- return 0;
- return set_update_internal(self, iterable);
+ if (!PyAnySet_Check(self))
+ return -1;
+ if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
+ return -1;
+ if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
+ return -1;
+ set_clear_internal(self);
+ self->hash = -1;
+ if (iterable == NULL)
+ return 0;
+ return set_update_internal(self, iterable);
}
static PySequenceMethods set_as_sequence = {
- set_len, /* sq_length */
- 0, /* sq_concat */
- 0, /* sq_repeat */
- 0, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)set_contains, /* sq_contains */
+ set_len, /* sq_length */
+ 0, /* sq_concat */
+ 0, /* sq_repeat */
+ 0, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)set_contains, /* sq_contains */
};
/* set object ********************************************************/
@@ -2010,83 +2010,83 @@ All is well if assertions don't fail.");
#endif
static PyMethodDef set_methods[] = {
- {"add", (PyCFunction)set_add, METH_O,
- add_doc},
- {"clear", (PyCFunction)set_clear, METH_NOARGS,
- clear_doc},
- {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
- contains_doc},
- {"copy", (PyCFunction)set_copy, METH_NOARGS,
- copy_doc},
- {"discard", (PyCFunction)set_discard, METH_O,
- discard_doc},
- {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
- difference_doc},
- {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
- difference_update_doc},
- {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
- intersection_doc},
- {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
- intersection_update_doc},
- {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
- isdisjoint_doc},
- {"issubset", (PyCFunction)set_issubset, METH_O,
- issubset_doc},
- {"issuperset", (PyCFunction)set_issuperset, METH_O,
- issuperset_doc},
- {"pop", (PyCFunction)set_pop, METH_NOARGS,
- pop_doc},
- {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
- reduce_doc},
- {"remove", (PyCFunction)set_remove, METH_O,
- remove_doc},
- {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
- sizeof_doc},
- {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
- symmetric_difference_doc},
- {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
- symmetric_difference_update_doc},
+ {"add", (PyCFunction)set_add, METH_O,
+ add_doc},
+ {"clear", (PyCFunction)set_clear, METH_NOARGS,
+ clear_doc},
+ {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
+ contains_doc},
+ {"copy", (PyCFunction)set_copy, METH_NOARGS,
+ copy_doc},
+ {"discard", (PyCFunction)set_discard, METH_O,
+ discard_doc},
+ {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
+ difference_doc},
+ {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
+ difference_update_doc},
+ {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
+ intersection_doc},
+ {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
+ intersection_update_doc},
+ {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
+ isdisjoint_doc},
+ {"issubset", (PyCFunction)set_issubset, METH_O,
+ issubset_doc},
+ {"issuperset", (PyCFunction)set_issuperset, METH_O,
+ issuperset_doc},
+ {"pop", (PyCFunction)set_pop, METH_NOARGS,
+ pop_doc},
+ {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
+ reduce_doc},
+ {"remove", (PyCFunction)set_remove, METH_O,
+ remove_doc},
+ {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
+ sizeof_doc},
+ {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
+ symmetric_difference_doc},
+ {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
+ symmetric_difference_update_doc},
#ifdef Py_DEBUG
- {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
- test_c_api_doc},
+ {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
+ test_c_api_doc},
#endif
- {"union", (PyCFunction)set_union, METH_VARARGS,
- union_doc},
- {"update", (PyCFunction)set_update, METH_VARARGS,
- update_doc},
- {NULL, NULL} /* sentinel */
+ {"union", (PyCFunction)set_union, METH_VARARGS,
+ union_doc},
+ {"update", (PyCFunction)set_update, METH_VARARGS,
+ update_doc},
+ {NULL, NULL} /* sentinel */
};
static PyNumberMethods set_as_number = {
- 0, /*nb_add*/
- (binaryfunc)set_sub, /*nb_subtract*/
- 0, /*nb_multiply*/
- 0, /*nb_remainder*/
- 0, /*nb_divmod*/
- 0, /*nb_power*/
- 0, /*nb_negative*/
- 0, /*nb_positive*/
- 0, /*nb_absolute*/
- 0, /*nb_bool*/
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- (binaryfunc)set_and, /*nb_and*/
- (binaryfunc)set_xor, /*nb_xor*/
- (binaryfunc)set_or, /*nb_or*/
- 0, /*nb_int*/
- 0, /*nb_reserved*/
- 0, /*nb_float*/
- 0, /*nb_inplace_add*/
- (binaryfunc)set_isub, /*nb_inplace_subtract*/
- 0, /*nb_inplace_multiply*/
- 0, /*nb_inplace_remainder*/
- 0, /*nb_inplace_power*/
- 0, /*nb_inplace_lshift*/
- 0, /*nb_inplace_rshift*/
- (binaryfunc)set_iand, /*nb_inplace_and*/
- (binaryfunc)set_ixor, /*nb_inplace_xor*/
- (binaryfunc)set_ior, /*nb_inplace_or*/
+ 0, /*nb_add*/
+ (binaryfunc)set_sub, /*nb_subtract*/
+ 0, /*nb_multiply*/
+ 0, /*nb_remainder*/
+ 0, /*nb_divmod*/
+ 0, /*nb_power*/
+ 0, /*nb_negative*/
+ 0, /*nb_positive*/
+ 0, /*nb_absolute*/
+ 0, /*nb_bool*/
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ (binaryfunc)set_and, /*nb_and*/
+ (binaryfunc)set_xor, /*nb_xor*/
+ (binaryfunc)set_or, /*nb_or*/
+ 0, /*nb_int*/
+ 0, /*nb_reserved*/
+ 0, /*nb_float*/
+ 0, /*nb_inplace_add*/
+ (binaryfunc)set_isub, /*nb_inplace_subtract*/
+ 0, /*nb_inplace_multiply*/
+ 0, /*nb_inplace_remainder*/
+ 0, /*nb_inplace_power*/
+ 0, /*nb_inplace_lshift*/
+ 0, /*nb_inplace_rshift*/
+ (binaryfunc)set_iand, /*nb_inplace_and*/
+ (binaryfunc)set_ixor, /*nb_inplace_xor*/
+ (binaryfunc)set_ior, /*nb_inplace_or*/
};
PyDoc_STRVAR(set_doc,
@@ -2096,95 +2096,95 @@ set(iterable) -> new set object\n\
Build an unordered collection of unique elements.");
PyTypeObject PySet_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "set", /* tp_name */
- sizeof(PySetObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)set_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)set_repr, /* tp_repr */
- &set_as_number, /* tp_as_number */
- &set_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- set_doc, /* tp_doc */
- (traverseproc)set_traverse, /* tp_traverse */
- (inquiry)set_clear_internal, /* tp_clear */
- (richcmpfunc)set_richcompare, /* tp_richcompare */
- offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
- (getiterfunc)set_iter, /* tp_iter */
- 0, /* tp_iternext */
- set_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)set_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- set_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "set", /* tp_name */
+ sizeof(PySetObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)set_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)set_repr, /* tp_repr */
+ &set_as_number, /* tp_as_number */
+ &set_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ set_doc, /* tp_doc */
+ (traverseproc)set_traverse, /* tp_traverse */
+ (inquiry)set_clear_internal, /* tp_clear */
+ (richcmpfunc)set_richcompare, /* tp_richcompare */
+ offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
+ (getiterfunc)set_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ set_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)set_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ set_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* frozenset object ********************************************************/
static PyMethodDef frozenset_methods[] = {
- {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
- contains_doc},
- {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
- copy_doc},
- {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
- difference_doc},
- {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
- intersection_doc},
- {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
- isdisjoint_doc},
- {"issubset", (PyCFunction)set_issubset, METH_O,
- issubset_doc},
- {"issuperset", (PyCFunction)set_issuperset, METH_O,
- issuperset_doc},
- {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
- reduce_doc},
- {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
- sizeof_doc},
- {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
- symmetric_difference_doc},
- {"union", (PyCFunction)set_union, METH_VARARGS,
- union_doc},
- {NULL, NULL} /* sentinel */
+ {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
+ contains_doc},
+ {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
+ copy_doc},
+ {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
+ difference_doc},
+ {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
+ intersection_doc},
+ {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
+ isdisjoint_doc},
+ {"issubset", (PyCFunction)set_issubset, METH_O,
+ issubset_doc},
+ {"issuperset", (PyCFunction)set_issuperset, METH_O,
+ issuperset_doc},
+ {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
+ reduce_doc},
+ {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
+ sizeof_doc},
+ {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
+ symmetric_difference_doc},
+ {"union", (PyCFunction)set_union, METH_VARARGS,
+ union_doc},
+ {NULL, NULL} /* sentinel */
};
static PyNumberMethods frozenset_as_number = {
- 0, /*nb_add*/
- (binaryfunc)set_sub, /*nb_subtract*/
- 0, /*nb_multiply*/
- 0, /*nb_remainder*/
- 0, /*nb_divmod*/
- 0, /*nb_power*/
- 0, /*nb_negative*/
- 0, /*nb_positive*/
- 0, /*nb_absolute*/
- 0, /*nb_bool*/
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- (binaryfunc)set_and, /*nb_and*/
- (binaryfunc)set_xor, /*nb_xor*/
- (binaryfunc)set_or, /*nb_or*/
+ 0, /*nb_add*/
+ (binaryfunc)set_sub, /*nb_subtract*/
+ 0, /*nb_multiply*/
+ 0, /*nb_remainder*/
+ 0, /*nb_divmod*/
+ 0, /*nb_power*/
+ 0, /*nb_negative*/
+ 0, /*nb_positive*/
+ 0, /*nb_absolute*/
+ 0, /*nb_bool*/
+ 0, /*nb_invert*/
+ 0, /*nb_lshift*/
+ 0, /*nb_rshift*/
+ (binaryfunc)set_and, /*nb_and*/
+ (binaryfunc)set_xor, /*nb_xor*/
+ (binaryfunc)set_or, /*nb_or*/
};
PyDoc_STRVAR(frozenset_doc,
@@ -2194,47 +2194,47 @@ frozenset(iterable) -> frozenset object\n\
Build an immutable unordered collection of unique elements.");
PyTypeObject PyFrozenSet_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "frozenset", /* tp_name */
- sizeof(PySetObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)set_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)set_repr, /* tp_repr */
- &frozenset_as_number, /* tp_as_number */
- &set_as_sequence, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- frozenset_hash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- frozenset_doc, /* tp_doc */
- (traverseproc)set_traverse, /* tp_traverse */
- (inquiry)set_clear_internal, /* tp_clear */
- (richcmpfunc)set_richcompare, /* tp_richcompare */
- offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
- (getiterfunc)set_iter, /* tp_iter */
- 0, /* tp_iternext */
- frozenset_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- frozenset_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "frozenset", /* tp_name */
+ sizeof(PySetObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)set_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)set_repr, /* tp_repr */
+ &frozenset_as_number, /* tp_as_number */
+ &set_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ frozenset_hash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ frozenset_doc, /* tp_doc */
+ (traverseproc)set_traverse, /* tp_traverse */
+ (inquiry)set_clear_internal, /* tp_clear */
+ (richcmpfunc)set_richcompare, /* tp_richcompare */
+ offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
+ (getiterfunc)set_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ frozenset_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ frozenset_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -2243,224 +2243,224 @@ PyTypeObject PyFrozenSet_Type = {
PyObject *
PySet_New(PyObject *iterable)
{
- return make_new_set(&PySet_Type, iterable);
+ return make_new_set(&PySet_Type, iterable);
}
PyObject *
PyFrozenSet_New(PyObject *iterable)
{
- return make_new_set(&PyFrozenSet_Type, iterable);
+ return make_new_set(&PyFrozenSet_Type, iterable);
}
Py_ssize_t
PySet_Size(PyObject *anyset)
{
- if (!PyAnySet_Check(anyset)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return PySet_GET_SIZE(anyset);
+ if (!PyAnySet_Check(anyset)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return PySet_GET_SIZE(anyset);
}
int
PySet_Clear(PyObject *set)
{
- if (!PySet_Check(set)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return set_clear_internal((PySetObject *)set);
+ if (!PySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_clear_internal((PySetObject *)set);
}
int
PySet_Contains(PyObject *anyset, PyObject *key)
{
- if (!PyAnySet_Check(anyset)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return set_contains_key((PySetObject *)anyset, key);
+ if (!PyAnySet_Check(anyset)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_contains_key((PySetObject *)anyset, key);
}
int
PySet_Discard(PyObject *set, PyObject *key)
{
- if (!PySet_Check(set)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return set_discard_key((PySetObject *)set, key);
+ if (!PySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_discard_key((PySetObject *)set, key);
}
int
PySet_Add(PyObject *anyset, PyObject *key)
{
- if (!PySet_Check(anyset) &&
- (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return set_add_key((PySetObject *)anyset, key);
+ if (!PySet_Check(anyset) &&
+ (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_add_key((PySetObject *)anyset, key);
}
int
_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
{
- setentry *entry;
+ setentry *entry;
- if (!PyAnySet_Check(set)) {
- PyErr_BadInternalCall();
- return -1;
- }
- if (set_next((PySetObject *)set, pos, &entry) == 0)
- return 0;
- *key = entry->key;
- *hash = entry->hash;
- return 1;
+ if (!PyAnySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (set_next((PySetObject *)set, pos, &entry) == 0)
+ return 0;
+ *key = entry->key;
+ *hash = entry->hash;
+ return 1;
}
PyObject *
PySet_Pop(PyObject *set)
{
- if (!PySet_Check(set)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return set_pop((PySetObject *)set);
+ if (!PySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return set_pop((PySetObject *)set);
}
int
_PySet_Update(PyObject *set, PyObject *iterable)
{
- if (!PySet_Check(set)) {
- PyErr_BadInternalCall();
- return -1;
- }
- return set_update_internal((PySetObject *)set, iterable);
+ if (!PySet_Check(set)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ return set_update_internal((PySetObject *)set, iterable);
}
#ifdef Py_DEBUG
-/* Test code to be called with any three element set.
+/* Test code to be called with any three element set.
Returns True and original set is restored. */
-#define assertRaises(call_return_value, exception) \
- do { \
- assert(call_return_value); \
- assert(PyErr_ExceptionMatches(exception)); \
- PyErr_Clear(); \
- } while(0)
+#define assertRaises(call_return_value, exception) \
+ do { \
+ assert(call_return_value); \
+ assert(PyErr_ExceptionMatches(exception)); \
+ PyErr_Clear(); \
+ } while(0)
static PyObject *
test_c_api(PySetObject *so)
{
- Py_ssize_t count;
- char *s;
- Py_ssize_t i;
- PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
- PyObject *ob = (PyObject *)so;
- long hash;
-
- /* Verify preconditions and exercise type/size checks */
- assert(PyAnySet_Check(ob));
- assert(PyAnySet_CheckExact(ob));
- assert(!PyFrozenSet_CheckExact(ob));
- assert(PySet_Size(ob) == 3);
- assert(PySet_GET_SIZE(ob) == 3);
-
- /* Raise TypeError for non-iterable constructor arguments */
- assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
- assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
-
- /* Raise TypeError for unhashable key */
- dup = PySet_New(ob);
- assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
- assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
- assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
-
- /* Exercise successful pop, contains, add, and discard */
- elem = PySet_Pop(ob);
- assert(PySet_Contains(ob, elem) == 0);
- assert(PySet_GET_SIZE(ob) == 2);
- assert(PySet_Add(ob, elem) == 0);
- assert(PySet_Contains(ob, elem) == 1);
- assert(PySet_GET_SIZE(ob) == 3);
- assert(PySet_Discard(ob, elem) == 1);
- assert(PySet_GET_SIZE(ob) == 2);
- assert(PySet_Discard(ob, elem) == 0);
- assert(PySet_GET_SIZE(ob) == 2);
-
- /* Exercise clear */
- dup2 = PySet_New(dup);
- assert(PySet_Clear(dup2) == 0);
- assert(PySet_Size(dup2) == 0);
- Py_DECREF(dup2);
-
- /* Raise SystemError on clear or update of frozen set */
- f = PyFrozenSet_New(dup);
- assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
- assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
- assert(PySet_Add(f, elem) == 0);
- Py_INCREF(f);
- assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
- Py_DECREF(f);
- Py_DECREF(f);
-
- /* Exercise direct iteration */
- i = 0, count = 0;
- while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
- s = _PyUnicode_AsString(x);
- assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
- count++;
- }
- assert(count == 3);
-
- /* Exercise updates */
- dup2 = PySet_New(NULL);
- assert(_PySet_Update(dup2, dup) == 0);
- assert(PySet_Size(dup2) == 3);
- assert(_PySet_Update(dup2, dup) == 0);
- assert(PySet_Size(dup2) == 3);
- Py_DECREF(dup2);
-
- /* Raise SystemError when self argument is not a set or frozenset. */
- t = PyTuple_New(0);
- assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
- assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
- Py_DECREF(t);
-
- /* Raise SystemError when self argument is not a set. */
- f = PyFrozenSet_New(dup);
- assert(PySet_Size(f) == 3);
- assert(PyFrozenSet_CheckExact(f));
- assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
- assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
- Py_DECREF(f);
-
- /* Raise KeyError when popping from an empty set */
- assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
- Py_DECREF(ob);
- assert(PySet_GET_SIZE(ob) == 0);
- assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
-
- /* Restore the set from the copy using the PyNumber API */
- assert(PyNumber_InPlaceOr(ob, dup) == ob);
- Py_DECREF(ob);
-
- /* Verify constructors accept NULL arguments */
- f = PySet_New(NULL);
- assert(f != NULL);
- assert(PySet_GET_SIZE(f) == 0);
- Py_DECREF(f);
- f = PyFrozenSet_New(NULL);
- assert(f != NULL);
- assert(PyFrozenSet_CheckExact(f));
- assert(PySet_GET_SIZE(f) == 0);
- Py_DECREF(f);
-
- Py_DECREF(elem);
- Py_DECREF(dup);
- Py_RETURN_TRUE;
+ Py_ssize_t count;
+ char *s;
+ Py_ssize_t i;
+ PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
+ PyObject *ob = (PyObject *)so;
+ long hash;
+
+ /* Verify preconditions and exercise type/size checks */
+ assert(PyAnySet_Check(ob));
+ assert(PyAnySet_CheckExact(ob));
+ assert(!PyFrozenSet_CheckExact(ob));
+ assert(PySet_Size(ob) == 3);
+ assert(PySet_GET_SIZE(ob) == 3);
+
+ /* Raise TypeError for non-iterable constructor arguments */
+ assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
+ assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
+
+ /* Raise TypeError for unhashable key */
+ dup = PySet_New(ob);
+ assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
+ assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
+ assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
+
+ /* Exercise successful pop, contains, add, and discard */
+ elem = PySet_Pop(ob);
+ assert(PySet_Contains(ob, elem) == 0);
+ assert(PySet_GET_SIZE(ob) == 2);
+ assert(PySet_Add(ob, elem) == 0);
+ assert(PySet_Contains(ob, elem) == 1);
+ assert(PySet_GET_SIZE(ob) == 3);
+ assert(PySet_Discard(ob, elem) == 1);
+ assert(PySet_GET_SIZE(ob) == 2);
+ assert(PySet_Discard(ob, elem) == 0);
+ assert(PySet_GET_SIZE(ob) == 2);
+
+ /* Exercise clear */
+ dup2 = PySet_New(dup);
+ assert(PySet_Clear(dup2) == 0);
+ assert(PySet_Size(dup2) == 0);
+ Py_DECREF(dup2);
+
+ /* Raise SystemError on clear or update of frozen set */
+ f = PyFrozenSet_New(dup);
+ assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
+ assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
+ assert(PySet_Add(f, elem) == 0);
+ Py_INCREF(f);
+ assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
+ Py_DECREF(f);
+ Py_DECREF(f);
+
+ /* Exercise direct iteration */
+ i = 0, count = 0;
+ while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
+ s = _PyUnicode_AsString(x);
+ assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
+ count++;
+ }
+ assert(count == 3);
+
+ /* Exercise updates */
+ dup2 = PySet_New(NULL);
+ assert(_PySet_Update(dup2, dup) == 0);
+ assert(PySet_Size(dup2) == 3);
+ assert(_PySet_Update(dup2, dup) == 0);
+ assert(PySet_Size(dup2) == 3);
+ Py_DECREF(dup2);
+
+ /* Raise SystemError when self argument is not a set or frozenset. */
+ t = PyTuple_New(0);
+ assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
+ assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
+ Py_DECREF(t);
+
+ /* Raise SystemError when self argument is not a set. */
+ f = PyFrozenSet_New(dup);
+ assert(PySet_Size(f) == 3);
+ assert(PyFrozenSet_CheckExact(f));
+ assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
+ assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
+ Py_DECREF(f);
+
+ /* Raise KeyError when popping from an empty set */
+ assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
+ Py_DECREF(ob);
+ assert(PySet_GET_SIZE(ob) == 0);
+ assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
+
+ /* Restore the set from the copy using the PyNumber API */
+ assert(PyNumber_InPlaceOr(ob, dup) == ob);
+ Py_DECREF(ob);
+
+ /* Verify constructors accept NULL arguments */
+ f = PySet_New(NULL);
+ assert(f != NULL);
+ assert(PySet_GET_SIZE(f) == 0);
+ Py_DECREF(f);
+ f = PyFrozenSet_New(NULL);
+ assert(f != NULL);
+ assert(PyFrozenSet_CheckExact(f));
+ assert(PySet_GET_SIZE(f) == 0);
+ Py_DECREF(f);
+
+ Py_DECREF(elem);
+ Py_DECREF(dup);
+ Py_RETURN_TRUE;
}
#undef assertRaises
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 9acd7f7..ee89006 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -7,7 +7,7 @@ Guido, feel free to do whatever you want in the way of copyrights
for this file.
*/
-/*
+/*
Py_Ellipsis encodes the '...' rubber index token. It is similar to
the Py_NoneStruct in that there is no way to create other objects of
this type and there is exactly one in existence.
@@ -19,35 +19,35 @@ this type and there is exactly one in existence.
static PyObject *
ellipsis_repr(PyObject *op)
{
- return PyUnicode_FromString("Ellipsis");
+ return PyUnicode_FromString("Ellipsis");
}
PyTypeObject PyEllipsis_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "ellipsis", /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /*never called*/ /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- ellipsis_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "ellipsis", /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ 0, /*never called*/ /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ ellipsis_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
};
PyObject _Py_EllipsisObject = {
- _PyObject_EXTRA_INIT
- 1, &PyEllipsis_Type
+ _PyObject_EXTRA_INIT
+ 1, &PyEllipsis_Type
};
@@ -60,154 +60,154 @@ PyObject _Py_EllipsisObject = {
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
- PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
+ PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
- if (obj == NULL)
- return NULL;
+ if (obj == NULL)
+ return NULL;
- if (step == NULL) step = Py_None;
- Py_INCREF(step);
- if (start == NULL) start = Py_None;
- Py_INCREF(start);
- if (stop == NULL) stop = Py_None;
- Py_INCREF(stop);
+ if (step == NULL) step = Py_None;
+ Py_INCREF(step);
+ if (start == NULL) start = Py_None;
+ Py_INCREF(start);
+ if (stop == NULL) stop = Py_None;
+ Py_INCREF(stop);
- obj->step = step;
- obj->start = start;
- obj->stop = stop;
+ obj->step = step;
+ obj->start = start;
+ obj->stop = stop;
- return (PyObject *) obj;
+ return (PyObject *) obj;
}
PyObject *
_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
{
- PyObject *start, *end, *slice;
- start = PyLong_FromSsize_t(istart);
- if (!start)
- return NULL;
- end = PyLong_FromSsize_t(istop);
- if (!end) {
- Py_DECREF(start);
- return NULL;
- }
-
- slice = PySlice_New(start, end, NULL);
- Py_DECREF(start);
- Py_DECREF(end);
- return slice;
+ PyObject *start, *end, *slice;
+ start = PyLong_FromSsize_t(istart);
+ if (!start)
+ return NULL;
+ end = PyLong_FromSsize_t(istop);
+ if (!end) {
+ Py_DECREF(start);
+ return NULL;
+ }
+
+ slice = PySlice_New(start, end, NULL);
+ Py_DECREF(start);
+ Py_DECREF(end);
+ return slice;
}
int
PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
- /* XXX support long ints */
- if (r->step == Py_None) {
- *step = 1;
- } else {
- if (!PyLong_Check(r->step)) return -1;
- *step = PyLong_AsSsize_t(r->step);
- }
- if (r->start == Py_None) {
- *start = *step < 0 ? length-1 : 0;
- } else {
- if (!PyLong_Check(r->start)) return -1;
- *start = PyLong_AsSsize_t(r->start);
- if (*start < 0) *start += length;
- }
- if (r->stop == Py_None) {
- *stop = *step < 0 ? -1 : length;
- } else {
- if (!PyLong_Check(r->stop)) return -1;
- *stop = PyLong_AsSsize_t(r->stop);
- if (*stop < 0) *stop += length;
- }
- if (*stop > length) return -1;
- if (*start >= length) return -1;
- if (*step == 0) return -1;
- return 0;
+ /* XXX support long ints */
+ if (r->step == Py_None) {
+ *step = 1;
+ } else {
+ if (!PyLong_Check(r->step)) return -1;
+ *step = PyLong_AsSsize_t(r->step);
+ }
+ if (r->start == Py_None) {
+ *start = *step < 0 ? length-1 : 0;
+ } else {
+ if (!PyLong_Check(r->start)) return -1;
+ *start = PyLong_AsSsize_t(r->start);
+ if (*start < 0) *start += length;
+ }
+ if (r->stop == Py_None) {
+ *stop = *step < 0 ? -1 : length;
+ } else {
+ if (!PyLong_Check(r->stop)) return -1;
+ *stop = PyLong_AsSsize_t(r->stop);
+ if (*stop < 0) *stop += length;
+ }
+ if (*stop > length) return -1;
+ if (*start >= length) return -1;
+ if (*step == 0) return -1;
+ return 0;
}
int
PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length,
- Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
+ Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
{
- /* this is harder to get right than you might think */
-
- Py_ssize_t defstart, defstop;
-
- if (r->step == Py_None) {
- *step = 1;
- }
- else {
- if (!_PyEval_SliceIndex(r->step, step)) return -1;
- if (*step == 0) {
- PyErr_SetString(PyExc_ValueError,
- "slice step cannot be zero");
- return -1;
- }
- }
-
- defstart = *step < 0 ? length-1 : 0;
- defstop = *step < 0 ? -1 : length;
-
- if (r->start == Py_None) {
- *start = defstart;
- }
- else {
- if (!_PyEval_SliceIndex(r->start, start)) return -1;
- if (*start < 0) *start += length;
- if (*start < 0) *start = (*step < 0) ? -1 : 0;
- if (*start >= length)
- *start = (*step < 0) ? length - 1 : length;
- }
-
- if (r->stop == Py_None) {
- *stop = defstop;
- }
- else {
- if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
- if (*stop < 0) *stop += length;
- if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
- if (*stop >= length)
- *stop = (*step < 0) ? length - 1 : length;
- }
-
- if ((*step < 0 && *stop >= *start)
- || (*step > 0 && *start >= *stop)) {
- *slicelength = 0;
- }
- else if (*step < 0) {
- *slicelength = (*stop-*start+1)/(*step)+1;
- }
- else {
- *slicelength = (*stop-*start-1)/(*step)+1;
- }
-
- return 0;
+ /* this is harder to get right than you might think */
+
+ Py_ssize_t defstart, defstop;
+
+ if (r->step == Py_None) {
+ *step = 1;
+ }
+ else {
+ if (!_PyEval_SliceIndex(r->step, step)) return -1;
+ if (*step == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "slice step cannot be zero");
+ return -1;
+ }
+ }
+
+ defstart = *step < 0 ? length-1 : 0;
+ defstop = *step < 0 ? -1 : length;
+
+ if (r->start == Py_None) {
+ *start = defstart;
+ }
+ else {
+ if (!_PyEval_SliceIndex(r->start, start)) return -1;
+ if (*start < 0) *start += length;
+ if (*start < 0) *start = (*step < 0) ? -1 : 0;
+ if (*start >= length)
+ *start = (*step < 0) ? length - 1 : length;
+ }
+
+ if (r->stop == Py_None) {
+ *stop = defstop;
+ }
+ else {
+ if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
+ if (*stop < 0) *stop += length;
+ if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
+ if (*stop >= length)
+ *stop = (*step < 0) ? length - 1 : length;
+ }
+
+ if ((*step < 0 && *stop >= *start)
+ || (*step > 0 && *start >= *stop)) {
+ *slicelength = 0;
+ }
+ else if (*step < 0) {
+ *slicelength = (*stop-*start+1)/(*step)+1;
+ }
+ else {
+ *slicelength = (*stop-*start-1)/(*step)+1;
+ }
+
+ return 0;
}
static PyObject *
slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
- PyObject *start, *stop, *step;
+ PyObject *start, *stop, *step;
- start = stop = step = NULL;
+ start = stop = step = NULL;
- if (!_PyArg_NoKeywords("slice()", kw))
- return NULL;
+ if (!_PyArg_NoKeywords("slice()", kw))
+ return NULL;
- if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
+ return NULL;
- /* This swapping of stop and start is to maintain similarity with
- range(). */
- if (stop == NULL) {
- stop = start;
- start = NULL;
- }
- return PySlice_New(start, stop, step);
+ /* This swapping of stop and start is to maintain similarity with
+ range(). */
+ if (stop == NULL) {
+ stop = start;
+ start = NULL;
+ }
+ return PySlice_New(start, stop, step);
}
PyDoc_STRVAR(slice_doc,
@@ -218,42 +218,42 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
static void
slice_dealloc(PySliceObject *r)
{
- Py_DECREF(r->step);
- Py_DECREF(r->start);
- Py_DECREF(r->stop);
- PyObject_Del(r);
+ Py_DECREF(r->step);
+ Py_DECREF(r->start);
+ Py_DECREF(r->stop);
+ PyObject_Del(r);
}
static PyObject *
slice_repr(PySliceObject *r)
{
- return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
+ return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
}
static PyMemberDef slice_members[] = {
- {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
- {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
- {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
- {0}
+ {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
+ {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
+ {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
+ {0}
};
static PyObject*
slice_indices(PySliceObject* self, PyObject* len)
{
- Py_ssize_t ilen, start, stop, step, slicelength;
+ Py_ssize_t ilen, start, stop, step, slicelength;
- ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);
+ ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);
- if (ilen == -1 && PyErr_Occurred()) {
- return NULL;
- }
+ if (ilen == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
- if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
- &step, &slicelength) < 0) {
- return NULL;
- }
+ if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
+ &step, &slicelength) < 0) {
+ return NULL;
+ }
- return Py_BuildValue("(nnn)", start, stop, step);
+ return Py_BuildValue("(nnn)", start, stop, step);
}
PyDoc_STRVAR(slice_indices_doc,
@@ -267,119 +267,119 @@ handling of normal slices.");
static PyObject *
slice_reduce(PySliceObject* self)
{
- return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
+ return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
}
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyMethodDef slice_methods[] = {
- {"indices", (PyCFunction)slice_indices,
- METH_O, slice_indices_doc},
- {"__reduce__", (PyCFunction)slice_reduce,
- METH_NOARGS, reduce_doc},
- {NULL, NULL}
+ {"indices", (PyCFunction)slice_indices,
+ METH_O, slice_indices_doc},
+ {"__reduce__", (PyCFunction)slice_reduce,
+ METH_NOARGS, reduce_doc},
+ {NULL, NULL}
};
static PyObject *
slice_richcompare(PyObject *v, PyObject *w, int op)
{
- PyObject *t1;
- PyObject *t2;
- PyObject *res;
-
- if (!PySlice_Check(v) || !PySlice_Check(w)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- if (v == w) {
- /* XXX Do we really need this shortcut?
- There's a unit test for it, but is that fair? */
- switch (op) {
- case Py_EQ:
- case Py_LE:
- case Py_GE:
- res = Py_True;
- break;
- default:
- res = Py_False;
- break;
- }
- Py_INCREF(res);
- return res;
- }
-
- t1 = PyTuple_New(3);
- t2 = PyTuple_New(3);
- if (t1 == NULL || t2 == NULL)
- return NULL;
-
- PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
- PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
- PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
- PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
- PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
- PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
-
- res = PyObject_RichCompare(t1, t2, op);
-
- PyTuple_SET_ITEM(t1, 0, NULL);
- PyTuple_SET_ITEM(t1, 1, NULL);
- PyTuple_SET_ITEM(t1, 2, NULL);
- PyTuple_SET_ITEM(t2, 0, NULL);
- PyTuple_SET_ITEM(t2, 1, NULL);
- PyTuple_SET_ITEM(t2, 2, NULL);
-
- Py_DECREF(t1);
- Py_DECREF(t2);
-
- return res;
+ PyObject *t1;
+ PyObject *t2;
+ PyObject *res;
+
+ if (!PySlice_Check(v) || !PySlice_Check(w)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ if (v == w) {
+ /* XXX Do we really need this shortcut?
+ There's a unit test for it, but is that fair? */
+ switch (op) {
+ case Py_EQ:
+ case Py_LE:
+ case Py_GE:
+ res = Py_True;
+ break;
+ default:
+ res = Py_False;
+ break;
+ }
+ Py_INCREF(res);
+ return res;
+ }
+
+ t1 = PyTuple_New(3);
+ t2 = PyTuple_New(3);
+ if (t1 == NULL || t2 == NULL)
+ return NULL;
+
+ PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
+ PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
+ PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
+ PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
+ PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
+ PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
+
+ res = PyObject_RichCompare(t1, t2, op);
+
+ PyTuple_SET_ITEM(t1, 0, NULL);
+ PyTuple_SET_ITEM(t1, 1, NULL);
+ PyTuple_SET_ITEM(t1, 2, NULL);
+ PyTuple_SET_ITEM(t2, 0, NULL);
+ PyTuple_SET_ITEM(t2, 1, NULL);
+ PyTuple_SET_ITEM(t2, 2, NULL);
+
+ Py_DECREF(t1);
+ Py_DECREF(t2);
+
+ return res;
}
static long
slice_hash(PySliceObject *v)
{
- PyErr_SetString(PyExc_TypeError, "unhashable type");
- return -1L;
+ PyErr_SetString(PyExc_TypeError, "unhashable type");
+ return -1L;
}
PyTypeObject PySlice_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "slice", /* Name of this type */
- sizeof(PySliceObject), /* Basic object size */
- 0, /* Item size for varobject */
- (destructor)slice_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)slice_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)slice_hash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- slice_doc, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- slice_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- slice_methods, /* tp_methods */
- slice_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- slice_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "slice", /* Name of this type */
+ sizeof(PySliceObject), /* Basic object size */
+ 0, /* Item size for varobject */
+ (destructor)slice_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)slice_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)slice_hash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ slice_doc, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ slice_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ slice_methods, /* tp_methods */
+ slice_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ slice_new, /* tp_new */
};
diff --git a/Objects/stringlib/eq.h b/Objects/stringlib/eq.h
index 4e989dc..3e7f5e8 100644
--- a/Objects/stringlib/eq.h
+++ b/Objects/stringlib/eq.h
@@ -6,16 +6,16 @@
Py_LOCAL_INLINE(int)
unicode_eq(PyObject *aa, PyObject *bb)
{
- register PyUnicodeObject *a = (PyUnicodeObject *)aa;
- register PyUnicodeObject *b = (PyUnicodeObject *)bb;
+ register PyUnicodeObject *a = (PyUnicodeObject *)aa;
+ register PyUnicodeObject *b = (PyUnicodeObject *)bb;
- if (a->length != b->length)
- return 0;
- if (a->length == 0)
- return 1;
- if (a->str[0] != b->str[0])
- return 0;
- if (a->length == 1)
- return 1;
- return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
+ if (a->length != b->length)
+ return 0;
+ if (a->length == 0)
+ return 1;
+ if (a->str[0] != b->str[0])
+ return 0;
+ if (a->length == 1)
+ return 1;
+ return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
}
diff --git a/Objects/stringlib/partition.h b/Objects/stringlib/partition.h
index 105ba31..20c7507 100644
--- a/Objects/stringlib/partition.h
+++ b/Objects/stringlib/partition.h
@@ -18,23 +18,23 @@ stringlib_partition(
if (sep_len == 0) {
PyErr_SetString(PyExc_ValueError, "empty separator");
- return NULL;
+ return NULL;
}
out = PyTuple_New(3);
if (!out)
- return NULL;
+ return NULL;
pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH);
if (pos < 0) {
- Py_INCREF(str_obj);
- PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
- Py_INCREF(STRINGLIB_EMPTY);
- PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
- Py_INCREF(STRINGLIB_EMPTY);
- PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
- return out;
+ Py_INCREF(str_obj);
+ PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
+ Py_INCREF(STRINGLIB_EMPTY);
+ PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
+ Py_INCREF(STRINGLIB_EMPTY);
+ PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
+ return out;
}
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
@@ -44,8 +44,8 @@ stringlib_partition(
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
if (PyErr_Occurred()) {
- Py_DECREF(out);
- return NULL;
+ Py_DECREF(out);
+ return NULL;
}
return out;
@@ -62,29 +62,29 @@ stringlib_rpartition(
if (sep_len == 0) {
PyErr_SetString(PyExc_ValueError, "empty separator");
- return NULL;
+ return NULL;
}
out = PyTuple_New(3);
if (!out)
- return NULL;
+ return NULL;
/* XXX - create reversefastsearch helper! */
pos = -1;
- for (j = str_len - sep_len; j >= 0; --j)
+ for (j = str_len - sep_len; j >= 0; --j)
if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) {
pos = j;
break;
}
if (pos < 0) {
- Py_INCREF(STRINGLIB_EMPTY);
- PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
- Py_INCREF(STRINGLIB_EMPTY);
- PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
- Py_INCREF(str_obj);
- PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
- return out;
+ Py_INCREF(STRINGLIB_EMPTY);
+ PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
+ Py_INCREF(STRINGLIB_EMPTY);
+ PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
+ Py_INCREF(str_obj);
+ PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
+ return out;
}
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
@@ -94,8 +94,8 @@ stringlib_rpartition(
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
if (PyErr_Occurred()) {
- Py_DECREF(out);
- return NULL;
+ Py_DECREF(out);
+ return NULL;
}
return out;
diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/string_format.h
index 539feae..ecb00a9 100644
--- a/Objects/stringlib/string_format.h
+++ b/Objects/stringlib/string_format.h
@@ -563,36 +563,36 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
PyObject *format_spec_object = NULL;
PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;
STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
- format_spec->ptr : NULL;
+ format_spec->ptr : NULL;
Py_ssize_t format_spec_len = format_spec->ptr ?
- format_spec->end - format_spec->ptr : 0;
+ format_spec->end - format_spec->ptr : 0;
/* If we know the type exactly, skip the lookup of __format__ and just
call the formatter directly. */
if (PyUnicode_CheckExact(fieldobj))
- formatter = _PyUnicode_FormatAdvanced;
+ formatter = _PyUnicode_FormatAdvanced;
else if (PyLong_CheckExact(fieldobj))
- formatter =_PyLong_FormatAdvanced;
+ formatter =_PyLong_FormatAdvanced;
else if (PyFloat_CheckExact(fieldobj))
- formatter = _PyFloat_FormatAdvanced;
+ formatter = _PyFloat_FormatAdvanced;
/* XXX: for 2.6, convert format_spec to the appropriate type
(unicode, str) */
if (formatter) {
- /* we know exactly which formatter will be called when __format__ is
- looked up, so call it directly, instead. */
- result = formatter(fieldobj, format_spec_start, format_spec_len);
+ /* we know exactly which formatter will be called when __format__ is
+ looked up, so call it directly, instead. */
+ result = formatter(fieldobj, format_spec_start, format_spec_len);
}
else {
- /* We need to create an object out of the pointers we have, because
- __format__ takes a string/unicode object for format_spec. */
- format_spec_object = STRINGLIB_NEW(format_spec_start,
- format_spec_len);
- if (format_spec_object == NULL)
- goto done;
-
- result = PyObject_Format(fieldobj, format_spec_object);
+ /* We need to create an object out of the pointers we have, because
+ __format__ takes a string/unicode object for format_spec. */
+ format_spec_object = STRINGLIB_NEW(format_spec_start,
+ format_spec_len);
+ if (format_spec_object == NULL)
+ goto done;
+
+ result = PyObject_Format(fieldobj, format_spec_object);
}
if (result == NULL)
goto done;
@@ -605,11 +605,11 @@ render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
/* Convert result to our type. We could be str, and result could
be unicode */
{
- PyObject *tmp = STRINGLIB_TOSTR(result);
- if (tmp == NULL)
- goto done;
- Py_DECREF(result);
- result = tmp;
+ PyObject *tmp = STRINGLIB_TOSTR(result);
+ if (tmp == NULL)
+ goto done;
+ Py_DECREF(result);
+ result = tmp;
}
#endif
@@ -844,17 +844,17 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
return STRINGLIB_TOASCII(obj);
#endif
default:
- if (conversion > 32 && conversion < 127) {
- /* It's the ASCII subrange; casting to char is safe
- (assuming the execution character set is an ASCII
- superset). */
- PyErr_Format(PyExc_ValueError,
+ if (conversion > 32 && conversion < 127) {
+ /* It's the ASCII subrange; casting to char is safe
+ (assuming the execution character set is an ASCII
+ superset). */
+ PyErr_Format(PyExc_ValueError,
"Unknown conversion specifier %c",
(char)conversion);
- } else
- PyErr_Format(PyExc_ValueError,
- "Unknown conversion specifier \\x%x",
- (unsigned int)conversion);
+ } else
+ PyErr_Format(PyExc_ValueError,
+ "Unknown conversion specifier \\x%x",
+ (unsigned int)conversion);
return NULL;
}
}
@@ -1119,7 +1119,7 @@ formatteriter_next(formatteriterobject *it)
Py_INCREF(conversion_str);
}
else
- conversion_str = STRINGLIB_NEW(&conversion, 1);
+ conversion_str = STRINGLIB_NEW(&conversion, 1);
if (conversion_str == NULL)
goto done;
@@ -1135,39 +1135,39 @@ formatteriter_next(formatteriterobject *it)
}
static PyMethodDef formatteriter_methods[] = {
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject PyFormatterIter_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "formatteriterator", /* tp_name */
- sizeof(formatteriterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
+ "formatteriterator", /* tp_name */
+ sizeof(formatteriterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
/* methods */
- (destructor)formatteriter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)formatteriter_next, /* tp_iternext */
- formatteriter_methods, /* tp_methods */
+ (destructor)formatteriter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)formatteriter_next, /* tp_iternext */
+ formatteriter_methods, /* tp_methods */
0,
};
@@ -1268,39 +1268,39 @@ fieldnameiter_next(fieldnameiterobject *it)
}
static PyMethodDef fieldnameiter_methods[] = {
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject PyFieldNameIter_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "fieldnameiterator", /* tp_name */
- sizeof(fieldnameiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
+ "fieldnameiterator", /* tp_name */
+ sizeof(fieldnameiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
/* methods */
- (destructor)fieldnameiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)fieldnameiter_next, /* tp_iternext */
- fieldnameiter_methods, /* tp_methods */
+ (destructor)fieldnameiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)fieldnameiter_next, /* tp_iternext */
+ fieldnameiter_methods, /* tp_methods */
0};
/* unicode_formatter_field_name_split is used to implement
diff --git a/Objects/stringlib/transmogrify.h b/Objects/stringlib/transmogrify.h
index 7dc8177..4390e22 100644
--- a/Objects/stringlib/transmogrify.h
+++ b/Objects/stringlib/transmogrify.h
@@ -25,10 +25,10 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
size_t i, j;
PyObject *u;
int tabsize = 8;
-
+
if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
return NULL;
-
+
/* First pass: determine size of output string */
i = j = 0;
e = STRINGLIB_STR(self) + STRINGLIB_LEN(self);
@@ -55,20 +55,20 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
}
}
}
-
+
if ((i + j) > PY_SSIZE_T_MAX) {
PyErr_SetString(PyExc_OverflowError, "result is too long");
return NULL;
}
-
+
/* Second pass: create output string and fill it */
u = STRINGLIB_NEW(NULL, i + j);
if (!u)
return NULL;
-
+
j = 0;
q = STRINGLIB_STR(u);
-
+
for (p = STRINGLIB_STR(self); p < e; p++)
if (*p == '\t') {
if (tabsize > 0) {
@@ -84,7 +84,7 @@ stringlib_expandtabs(PyObject *self, PyObject *args)
if (*p == '\n' || *p == '\r')
j = 0;
}
-
+
return u;
}
@@ -110,16 +110,16 @@ pad(PyObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
}
u = STRINGLIB_NEW(NULL,
- left + STRINGLIB_LEN(self) + right);
+ left + STRINGLIB_LEN(self) + right);
if (u) {
if (left)
memset(STRINGLIB_STR(u), fill, left);
Py_MEMCPY(STRINGLIB_STR(u) + left,
- STRINGLIB_STR(self),
- STRINGLIB_LEN(self));
+ STRINGLIB_STR(self),
+ STRINGLIB_LEN(self));
if (right)
memset(STRINGLIB_STR(u) + left + STRINGLIB_LEN(self),
- fill, right);
+ fill, right);
}
return u;
@@ -271,17 +271,17 @@ stringlib_zfill(PyObject *self, PyObject *args)
}
-#define _STRINGLIB_SPLIT_APPEND(data, left, right) \
- str = STRINGLIB_NEW((data) + (left), \
- (right) - (left)); \
- if (str == NULL) \
- goto onError; \
- if (PyList_Append(list, str)) { \
- Py_DECREF(str); \
- goto onError; \
- } \
- else \
- Py_DECREF(str);
+#define _STRINGLIB_SPLIT_APPEND(data, left, right) \
+ str = STRINGLIB_NEW((data) + (left), \
+ (right) - (left)); \
+ if (str == NULL) \
+ goto onError; \
+ if (PyList_Append(list, str)) { \
+ Py_DECREF(str); \
+ goto onError; \
+ } \
+ else \
+ Py_DECREF(str);
PyDoc_STRVAR(splitlines__doc__,
"B.splitlines([keepends]) -> list of lines\n\
@@ -320,28 +320,28 @@ stringlib_splitlines(PyObject *self, PyObject *args)
goto onError;
for (i = j = 0; i < len; ) {
- Py_ssize_t eol;
-
- /* Find a line and append it */
- while (i < len && data[i] != '\n' && data[i] != '\r')
- i++;
-
- /* Skip the line break reading CRLF as one line break */
- eol = i;
- if (i < len) {
- if (data[i] == '\r' && i + 1 < len &&
- data[i+1] == '\n')
- i += 2;
- else
- i++;
- if (keepends)
- eol = i;
- }
- _STRINGLIB_SPLIT_APPEND(data, j, eol);
- j = i;
+ Py_ssize_t eol;
+
+ /* Find a line and append it */
+ while (i < len && data[i] != '\n' && data[i] != '\r')
+ i++;
+
+ /* Skip the line break reading CRLF as one line break */
+ eol = i;
+ if (i < len) {
+ if (data[i] == '\r' && i + 1 < len &&
+ data[i+1] == '\n')
+ i += 2;
+ else
+ i++;
+ if (keepends)
+ eol = i;
+ }
+ _STRINGLIB_SPLIT_APPEND(data, j, eol);
+ j = i;
}
if (j < len) {
- _STRINGLIB_SPLIT_APPEND(data, j, len);
+ _STRINGLIB_SPLIT_APPEND(data, j, len);
}
return list;
diff --git a/Objects/structseq.c b/Objects/structseq.c
index 54c8deb..7031225 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -9,7 +9,7 @@ static char visible_length_key[] = "n_sequence_fields";
static char real_length_key[] = "n_fields";
static char unnamed_fields_key[] = "n_unnamed_fields";
-/* Fields with this name have only a field index, not a field name.
+/* Fields with this name have only a field index, not a field name.
They are only allowed for indices < n_visible_fields. */
char *PyStructSequence_UnnamedField = "unnamed field";
@@ -29,511 +29,511 @@ char *PyStructSequence_UnnamedField = "unnamed field";
PyObject *
PyStructSequence_New(PyTypeObject *type)
{
- PyStructSequence *obj;
+ PyStructSequence *obj;
- obj = PyObject_New(PyStructSequence, type);
- if (obj == NULL)
- return NULL;
- Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
+ obj = PyObject_New(PyStructSequence, type);
+ if (obj == NULL)
+ return NULL;
+ Py_SIZE(obj) = VISIBLE_SIZE_TP(type);
- return (PyObject*) obj;
+ return (PyObject*) obj;
}
static void
structseq_dealloc(PyStructSequence *obj)
{
- Py_ssize_t i, size;
+ Py_ssize_t i, size;
- size = REAL_SIZE(obj);
- for (i = 0; i < size; ++i) {
- Py_XDECREF(obj->ob_item[i]);
- }
- PyObject_Del(obj);
+ size = REAL_SIZE(obj);
+ for (i = 0; i < size; ++i) {
+ Py_XDECREF(obj->ob_item[i]);
+ }
+ PyObject_Del(obj);
}
static Py_ssize_t
structseq_length(PyStructSequence *obj)
{
- return VISIBLE_SIZE(obj);
+ return VISIBLE_SIZE(obj);
}
static PyObject*
structseq_item(PyStructSequence *obj, Py_ssize_t i)
{
- if (i < 0 || i >= VISIBLE_SIZE(obj)) {
- PyErr_SetString(PyExc_IndexError, "tuple index out of range");
- return NULL;
- }
- Py_INCREF(obj->ob_item[i]);
- return obj->ob_item[i];
+ if (i < 0 || i >= VISIBLE_SIZE(obj)) {
+ PyErr_SetString(PyExc_IndexError, "tuple index out of range");
+ return NULL;
+ }
+ Py_INCREF(obj->ob_item[i]);
+ return obj->ob_item[i];
}
static PyObject*
structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high)
{
- PyTupleObject *np;
- Py_ssize_t i;
-
- if (low < 0)
- low = 0;
- if (high > VISIBLE_SIZE(obj))
- high = VISIBLE_SIZE(obj);
- if (high < low)
- high = low;
- np = (PyTupleObject *)PyTuple_New(high-low);
- if (np == NULL)
- return NULL;
- for(i = low; i < high; ++i) {
- PyObject *v = obj->ob_item[i];
- Py_INCREF(v);
- PyTuple_SET_ITEM(np, i-low, v);
- }
- return (PyObject *) np;
+ PyTupleObject *np;
+ Py_ssize_t i;
+
+ if (low < 0)
+ low = 0;
+ if (high > VISIBLE_SIZE(obj))
+ high = VISIBLE_SIZE(obj);
+ if (high < low)
+ high = low;
+ np = (PyTupleObject *)PyTuple_New(high-low);
+ if (np == NULL)
+ return NULL;
+ for(i = low; i < high; ++i) {
+ PyObject *v = obj->ob_item[i];
+ Py_INCREF(v);
+ PyTuple_SET_ITEM(np, i-low, v);
+ }
+ return (PyObject *) np;
}
static PyObject *
structseq_subscript(PyStructSequence *self, PyObject *item)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
-
- if (i < 0)
- i += VISIBLE_SIZE(self);
-
- if (i < 0 || i >= VISIBLE_SIZE(self)) {
- PyErr_SetString(PyExc_IndexError,
- "tuple index out of range");
- return NULL;
- }
- Py_INCREF(self->ob_item[i]);
- return self->ob_item[i];
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelen, cur, i;
- PyObject *result;
-
- if (PySlice_GetIndicesEx((PySliceObject *)item,
- VISIBLE_SIZE(self), &start, &stop,
- &step, &slicelen) < 0) {
- return NULL;
- }
- if (slicelen <= 0)
- return PyTuple_New(0);
- result = PyTuple_New(slicelen);
- if (result == NULL)
- return NULL;
- for (cur = start, i = 0; i < slicelen;
- cur += step, i++) {
- PyObject *v = self->ob_item[cur];
- Py_INCREF(v);
- PyTuple_SET_ITEM(result, i, v);
- }
- return result;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "structseq index must be integer");
- return NULL;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+
+ if (i < 0)
+ i += VISIBLE_SIZE(self);
+
+ if (i < 0 || i >= VISIBLE_SIZE(self)) {
+ PyErr_SetString(PyExc_IndexError,
+ "tuple index out of range");
+ return NULL;
+ }
+ Py_INCREF(self->ob_item[i]);
+ return self->ob_item[i];
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelen, cur, i;
+ PyObject *result;
+
+ if (PySlice_GetIndicesEx((PySliceObject *)item,
+ VISIBLE_SIZE(self), &start, &stop,
+ &step, &slicelen) < 0) {
+ return NULL;
+ }
+ if (slicelen <= 0)
+ return PyTuple_New(0);
+ result = PyTuple_New(slicelen);
+ if (result == NULL)
+ return NULL;
+ for (cur = start, i = 0; i < slicelen;
+ cur += step, i++) {
+ PyObject *v = self->ob_item[cur];
+ Py_INCREF(v);
+ PyTuple_SET_ITEM(result, i, v);
+ }
+ return result;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "structseq index must be integer");
+ return NULL;
+ }
}
static PyObject *
structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *arg = NULL;
- PyObject *dict = NULL;
- PyObject *ob;
- PyStructSequence *res = NULL;
- Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
- static char *kwlist[] = {"sequence", "dict", 0};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq",
- kwlist, &arg, &dict))
- return NULL;
-
- arg = PySequence_Fast(arg, "constructor requires a sequence");
-
- if (!arg) {
- return NULL;
- }
-
- if (dict && !PyDict_Check(dict)) {
- PyErr_Format(PyExc_TypeError,
- "%.500s() takes a dict as second arg, if any",
- type->tp_name);
- Py_DECREF(arg);
- return NULL;
- }
-
- len = PySequence_Fast_GET_SIZE(arg);
- min_len = VISIBLE_SIZE_TP(type);
- max_len = REAL_SIZE_TP(type);
- n_unnamed_fields = UNNAMED_FIELDS_TP(type);
-
- if (min_len != max_len) {
- if (len < min_len) {
- PyErr_Format(PyExc_TypeError,
- "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
- type->tp_name, min_len, len);
- Py_DECREF(arg);
- return NULL;
- }
-
- if (len > max_len) {
- PyErr_Format(PyExc_TypeError,
- "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
- type->tp_name, max_len, len);
- Py_DECREF(arg);
- return NULL;
- }
- }
- else {
- if (len != min_len) {
- PyErr_Format(PyExc_TypeError,
- "%.500s() takes a %zd-sequence (%zd-sequence given)",
- type->tp_name, min_len, len);
- Py_DECREF(arg);
- return NULL;
- }
- }
-
- res = (PyStructSequence*) PyStructSequence_New(type);
- if (res == NULL) {
- return NULL;
- }
- for (i = 0; i < len; ++i) {
- PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
- Py_INCREF(v);
- res->ob_item[i] = v;
- }
- for (; i < max_len; ++i) {
- if (dict && (ob = PyDict_GetItemString(
- dict, type->tp_members[i-n_unnamed_fields].name))) {
- }
- else {
- ob = Py_None;
- }
- Py_INCREF(ob);
- res->ob_item[i] = ob;
- }
-
- Py_DECREF(arg);
- return (PyObject*) res;
+ PyObject *arg = NULL;
+ PyObject *dict = NULL;
+ PyObject *ob;
+ PyStructSequence *res = NULL;
+ Py_ssize_t len, min_len, max_len, i, n_unnamed_fields;
+ static char *kwlist[] = {"sequence", "dict", 0};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq",
+ kwlist, &arg, &dict))
+ return NULL;
+
+ arg = PySequence_Fast(arg, "constructor requires a sequence");
+
+ if (!arg) {
+ return NULL;
+ }
+
+ if (dict && !PyDict_Check(dict)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.500s() takes a dict as second arg, if any",
+ type->tp_name);
+ Py_DECREF(arg);
+ return NULL;
+ }
+
+ len = PySequence_Fast_GET_SIZE(arg);
+ min_len = VISIBLE_SIZE_TP(type);
+ max_len = REAL_SIZE_TP(type);
+ n_unnamed_fields = UNNAMED_FIELDS_TP(type);
+
+ if (min_len != max_len) {
+ if (len < min_len) {
+ PyErr_Format(PyExc_TypeError,
+ "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
+ type->tp_name, min_len, len);
+ Py_DECREF(arg);
+ return NULL;
+ }
+
+ if (len > max_len) {
+ PyErr_Format(PyExc_TypeError,
+ "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
+ type->tp_name, max_len, len);
+ Py_DECREF(arg);
+ return NULL;
+ }
+ }
+ else {
+ if (len != min_len) {
+ PyErr_Format(PyExc_TypeError,
+ "%.500s() takes a %zd-sequence (%zd-sequence given)",
+ type->tp_name, min_len, len);
+ Py_DECREF(arg);
+ return NULL;
+ }
+ }
+
+ res = (PyStructSequence*) PyStructSequence_New(type);
+ if (res == NULL) {
+ return NULL;
+ }
+ for (i = 0; i < len; ++i) {
+ PyObject *v = PySequence_Fast_GET_ITEM(arg, i);
+ Py_INCREF(v);
+ res->ob_item[i] = v;
+ }
+ for (; i < max_len; ++i) {
+ if (dict && (ob = PyDict_GetItemString(
+ dict, type->tp_members[i-n_unnamed_fields].name))) {
+ }
+ else {
+ ob = Py_None;
+ }
+ Py_INCREF(ob);
+ res->ob_item[i] = ob;
+ }
+
+ Py_DECREF(arg);
+ return (PyObject*) res;
}
static PyObject *
make_tuple(PyStructSequence *obj)
{
- return structseq_slice(obj, 0, VISIBLE_SIZE(obj));
+ return structseq_slice(obj, 0, VISIBLE_SIZE(obj));
}
static PyObject *
structseq_repr(PyStructSequence *obj)
{
- /* buffer and type size were chosen well considered. */
+ /* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100
- PyObject *tup;
- PyTypeObject *typ = Py_TYPE(obj);
- int i, removelast = 0;
- Py_ssize_t len;
- char buf[REPR_BUFFER_SIZE];
- char *endofbuf, *pbuf = buf;
-
- /* pointer to end of writeable buffer; safes space for "...)\0" */
- endofbuf= &buf[REPR_BUFFER_SIZE-5];
-
- if ((tup = make_tuple(obj)) == NULL) {
- return NULL;
- }
-
- /* "typename(", limited to TYPE_MAXSIZE */
- len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
- strlen(typ->tp_name);
- strncpy(pbuf, typ->tp_name, len);
- pbuf += len;
- *pbuf++ = '(';
-
- for (i=0; i < VISIBLE_SIZE(obj); i++) {
- PyObject *val, *repr;
- char *cname, *crepr;
-
- cname = typ->tp_members[i].name;
-
- val = PyTuple_GetItem(tup, i);
- if (cname == NULL || val == NULL) {
- return NULL;
- }
- repr = PyObject_Repr(val);
- if (repr == NULL) {
- Py_DECREF(tup);
- return NULL;
- }
- crepr = _PyUnicode_AsString(repr);
- if (crepr == NULL) {
- Py_DECREF(tup);
- Py_DECREF(repr);
- return NULL;
- }
-
- /* + 3: keep space for "=" and ", " */
- len = strlen(cname) + strlen(crepr) + 3;
- if ((pbuf+len) <= endofbuf) {
- strcpy(pbuf, cname);
- pbuf += strlen(cname);
- *pbuf++ = '=';
- strcpy(pbuf, crepr);
- pbuf += strlen(crepr);
- *pbuf++ = ',';
- *pbuf++ = ' ';
- removelast = 1;
- Py_DECREF(repr);
- }
- else {
- strcpy(pbuf, "...");
- pbuf += 3;
- removelast = 0;
- Py_DECREF(repr);
- break;
- }
- }
- Py_DECREF(tup);
- if (removelast) {
- /* overwrite last ", " */
- pbuf-=2;
- }
- *pbuf++ = ')';
- *pbuf = '\0';
-
- return PyUnicode_FromString(buf);
+ PyObject *tup;
+ PyTypeObject *typ = Py_TYPE(obj);
+ int i, removelast = 0;
+ Py_ssize_t len;
+ char buf[REPR_BUFFER_SIZE];
+ char *endofbuf, *pbuf = buf;
+
+ /* pointer to end of writeable buffer; safes space for "...)\0" */
+ endofbuf= &buf[REPR_BUFFER_SIZE-5];
+
+ if ((tup = make_tuple(obj)) == NULL) {
+ return NULL;
+ }
+
+ /* "typename(", limited to TYPE_MAXSIZE */
+ len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
+ strlen(typ->tp_name);
+ strncpy(pbuf, typ->tp_name, len);
+ pbuf += len;
+ *pbuf++ = '(';
+
+ for (i=0; i < VISIBLE_SIZE(obj); i++) {
+ PyObject *val, *repr;
+ char *cname, *crepr;
+
+ cname = typ->tp_members[i].name;
+
+ val = PyTuple_GetItem(tup, i);
+ if (cname == NULL || val == NULL) {
+ return NULL;
+ }
+ repr = PyObject_Repr(val);
+ if (repr == NULL) {
+ Py_DECREF(tup);
+ return NULL;
+ }
+ crepr = _PyUnicode_AsString(repr);
+ if (crepr == NULL) {
+ Py_DECREF(tup);
+ Py_DECREF(repr);
+ return NULL;
+ }
+
+ /* + 3: keep space for "=" and ", " */
+ len = strlen(cname) + strlen(crepr) + 3;
+ if ((pbuf+len) <= endofbuf) {
+ strcpy(pbuf, cname);
+ pbuf += strlen(cname);
+ *pbuf++ = '=';
+ strcpy(pbuf, crepr);
+ pbuf += strlen(crepr);
+ *pbuf++ = ',';
+ *pbuf++ = ' ';
+ removelast = 1;
+ Py_DECREF(repr);
+ }
+ else {
+ strcpy(pbuf, "...");
+ pbuf += 3;
+ removelast = 0;
+ Py_DECREF(repr);
+ break;
+ }
+ }
+ Py_DECREF(tup);
+ if (removelast) {
+ /* overwrite last ", " */
+ pbuf-=2;
+ }
+ *pbuf++ = ')';
+ *pbuf = '\0';
+
+ return PyUnicode_FromString(buf);
}
static PyObject *
structseq_concat(PyStructSequence *obj, PyObject *b)
{
- PyObject *tup, *result;
- tup = make_tuple(obj);
- result = PySequence_Concat(tup, b);
- Py_DECREF(tup);
- return result;
+ PyObject *tup, *result;
+ tup = make_tuple(obj);
+ result = PySequence_Concat(tup, b);
+ Py_DECREF(tup);
+ return result;
}
static PyObject *
structseq_repeat(PyStructSequence *obj, Py_ssize_t n)
{
- PyObject *tup, *result;
- tup = make_tuple(obj);
- result = PySequence_Repeat(tup, n);
- Py_DECREF(tup);
- return result;
+ PyObject *tup, *result;
+ tup = make_tuple(obj);
+ result = PySequence_Repeat(tup, n);
+ Py_DECREF(tup);
+ return result;
}
static int
structseq_contains(PyStructSequence *obj, PyObject *o)
{
- PyObject *tup;
- int result;
- tup = make_tuple(obj);
- if (!tup)
- return -1;
- result = PySequence_Contains(tup, o);
- Py_DECREF(tup);
- return result;
+ PyObject *tup;
+ int result;
+ tup = make_tuple(obj);
+ if (!tup)
+ return -1;
+ result = PySequence_Contains(tup, o);
+ Py_DECREF(tup);
+ return result;
}
static long
structseq_hash(PyObject *obj)
{
- PyObject *tup;
- long result;
- tup = make_tuple((PyStructSequence*) obj);
- if (!tup)
- return -1;
- result = PyObject_Hash(tup);
- Py_DECREF(tup);
- return result;
+ PyObject *tup;
+ long result;
+ tup = make_tuple((PyStructSequence*) obj);
+ if (!tup)
+ return -1;
+ result = PyObject_Hash(tup);
+ Py_DECREF(tup);
+ return result;
}
static PyObject *
structseq_richcompare(PyObject *obj, PyObject *o2, int op)
{
- PyObject *tup, *result;
- tup = make_tuple((PyStructSequence*) obj);
- result = PyObject_RichCompare(tup, o2, op);
- Py_DECREF(tup);
- return result;
+ PyObject *tup, *result;
+ tup = make_tuple((PyStructSequence*) obj);
+ result = PyObject_RichCompare(tup, o2, op);
+ Py_DECREF(tup);
+ return result;
}
static PyObject *
structseq_reduce(PyStructSequence* self)
{
- PyObject* tup;
- PyObject* dict;
- PyObject* result;
- Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
- int i;
-
- n_fields = REAL_SIZE(self);
- n_visible_fields = VISIBLE_SIZE(self);
- n_unnamed_fields = UNNAMED_FIELDS(self);
- tup = PyTuple_New(n_visible_fields);
- if (!tup) {
- return NULL;
- }
-
- dict = PyDict_New();
- if (!dict) {
- Py_DECREF(tup);
- return NULL;
- }
-
- for (i = 0; i < n_visible_fields; i++) {
- Py_INCREF(self->ob_item[i]);
- PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
- }
-
- for (; i < n_fields; i++) {
- char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
- PyDict_SetItemString(dict, n,
- self->ob_item[i]);
- }
-
- result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
-
- Py_DECREF(tup);
- Py_DECREF(dict);
-
- return result;
+ PyObject* tup;
+ PyObject* dict;
+ PyObject* result;
+ Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
+ int i;
+
+ n_fields = REAL_SIZE(self);
+ n_visible_fields = VISIBLE_SIZE(self);
+ n_unnamed_fields = UNNAMED_FIELDS(self);
+ tup = PyTuple_New(n_visible_fields);
+ if (!tup) {
+ return NULL;
+ }
+
+ dict = PyDict_New();
+ if (!dict) {
+ Py_DECREF(tup);
+ return NULL;
+ }
+
+ for (i = 0; i < n_visible_fields; i++) {
+ Py_INCREF(self->ob_item[i]);
+ PyTuple_SET_ITEM(tup, i, self->ob_item[i]);
+ }
+
+ for (; i < n_fields; i++) {
+ char *n = Py_TYPE(self)->tp_members[i-n_unnamed_fields].name;
+ PyDict_SetItemString(dict, n,
+ self->ob_item[i]);
+ }
+
+ result = Py_BuildValue("(O(OO))", Py_TYPE(self), tup, dict);
+
+ Py_DECREF(tup);
+ Py_DECREF(dict);
+
+ return result;
}
static PySequenceMethods structseq_as_sequence = {
- (lenfunc)structseq_length,
- (binaryfunc)structseq_concat, /* sq_concat */
- (ssizeargfunc)structseq_repeat, /* sq_repeat */
- (ssizeargfunc)structseq_item, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)structseq_contains, /* sq_contains */
+ (lenfunc)structseq_length,
+ (binaryfunc)structseq_concat, /* sq_concat */
+ (ssizeargfunc)structseq_repeat, /* sq_repeat */
+ (ssizeargfunc)structseq_item, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)structseq_contains, /* sq_contains */
};
static PyMappingMethods structseq_as_mapping = {
- (lenfunc)structseq_length,
- (binaryfunc)structseq_subscript,
+ (lenfunc)structseq_length,
+ (binaryfunc)structseq_subscript,
};
static PyMethodDef structseq_methods[] = {
- {"__reduce__", (PyCFunction)structseq_reduce,
- METH_NOARGS, NULL},
- {NULL, NULL}
+ {"__reduce__", (PyCFunction)structseq_reduce,
+ METH_NOARGS, NULL},
+ {NULL, NULL}
};
static PyTypeObject _struct_sequence_template = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- NULL, /* tp_name */
- 0, /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor)structseq_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)structseq_repr, /* tp_repr */
- 0, /* tp_as_number */
- &structseq_as_sequence, /* tp_as_sequence */
- &structseq_as_mapping, /* tp_as_mapping */
- structseq_hash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- NULL, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- structseq_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- structseq_methods, /* tp_methods */
- NULL, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- structseq_new, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ NULL, /* tp_name */
+ 0, /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)structseq_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)structseq_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ &structseq_as_sequence, /* tp_as_sequence */
+ &structseq_as_mapping, /* tp_as_mapping */
+ structseq_hash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ NULL, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ structseq_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ structseq_methods, /* tp_methods */
+ NULL, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ structseq_new, /* tp_new */
};
void
PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)
{
- PyObject *dict;
- PyMemberDef* members;
- int n_members, n_unnamed_members, i, k;
+ PyObject *dict;
+ PyMemberDef* members;
+ int n_members, n_unnamed_members, i, k;
#ifdef Py_TRACE_REFS
- /* if the type object was chained, unchain it first
- before overwriting its storage */
- if (type->ob_base.ob_base._ob_next) {
- _Py_ForgetReference((PyObject*)type);
- }
+ /* if the type object was chained, unchain it first
+ before overwriting its storage */
+ if (type->ob_base.ob_base._ob_next) {
+ _Py_ForgetReference((PyObject*)type);
+ }
#endif
- n_unnamed_members = 0;
- for (i = 0; desc->fields[i].name != NULL; ++i)
- if (desc->fields[i].name == PyStructSequence_UnnamedField)
- n_unnamed_members++;
- n_members = i;
-
- memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
- type->tp_name = desc->name;
- type->tp_doc = desc->doc;
- type->tp_basicsize = sizeof(PyStructSequence)+
- sizeof(PyObject*)*(n_members-1);
- type->tp_itemsize = 0;
-
- members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1);
- if (members == NULL)
- return;
-
- for (i = k = 0; i < n_members; ++i) {
- if (desc->fields[i].name == PyStructSequence_UnnamedField)
- continue;
- members[k].name = desc->fields[i].name;
- members[k].type = T_OBJECT;
- members[k].offset = offsetof(PyStructSequence, ob_item)
- + i * sizeof(PyObject*);
- members[k].flags = READONLY;
- members[k].doc = desc->fields[i].doc;
- k++;
- }
- members[k].name = NULL;
-
- type->tp_members = members;
-
- if (PyType_Ready(type) < 0)
- return;
- Py_INCREF(type);
-
- dict = type->tp_dict;
-#define SET_DICT_FROM_INT(key, value) \
- do { \
- PyObject *v = PyLong_FromLong((long) value); \
- if (v != NULL) { \
- PyDict_SetItemString(dict, key, v); \
- Py_DECREF(v); \
- } \
- } while (0)
-
- SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
- SET_DICT_FROM_INT(real_length_key, n_members);
- SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
+ n_unnamed_members = 0;
+ for (i = 0; desc->fields[i].name != NULL; ++i)
+ if (desc->fields[i].name == PyStructSequence_UnnamedField)
+ n_unnamed_members++;
+ n_members = i;
+
+ memcpy(type, &_struct_sequence_template, sizeof(PyTypeObject));
+ type->tp_name = desc->name;
+ type->tp_doc = desc->doc;
+ type->tp_basicsize = sizeof(PyStructSequence)+
+ sizeof(PyObject*)*(n_members-1);
+ type->tp_itemsize = 0;
+
+ members = PyMem_NEW(PyMemberDef, n_members-n_unnamed_members+1);
+ if (members == NULL)
+ return;
+
+ for (i = k = 0; i < n_members; ++i) {
+ if (desc->fields[i].name == PyStructSequence_UnnamedField)
+ continue;
+ members[k].name = desc->fields[i].name;
+ members[k].type = T_OBJECT;
+ members[k].offset = offsetof(PyStructSequence, ob_item)
+ + i * sizeof(PyObject*);
+ members[k].flags = READONLY;
+ members[k].doc = desc->fields[i].doc;
+ k++;
+ }
+ members[k].name = NULL;
+
+ type->tp_members = members;
+
+ if (PyType_Ready(type) < 0)
+ return;
+ Py_INCREF(type);
+
+ dict = type->tp_dict;
+#define SET_DICT_FROM_INT(key, value) \
+ do { \
+ PyObject *v = PyLong_FromLong((long) value); \
+ if (v != NULL) { \
+ PyDict_SetItemString(dict, key, v); \
+ Py_DECREF(v); \
+ } \
+ } while (0)
+
+ SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
+ SET_DICT_FROM_INT(real_length_key, n_members);
+ SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
}
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 9145b58..aa3be82 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -5,9 +5,9 @@
/* Speed optimization to avoid frequent malloc/free of small tuples */
#ifndef PyTuple_MAXSAVESIZE
-#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
+#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
#endif
-#ifndef PyTuple_MAXFREELIST
+#ifndef PyTuple_MAXFREELIST
#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
#endif
@@ -35,12 +35,12 @@ static Py_ssize_t count_tracked = 0;
static void
show_track(void)
{
- fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
- count_tracked + count_untracked);
- fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
- "d\n", count_tracked);
- fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
- (100.0*count_tracked/(count_untracked+count_tracked)));
+ fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
+ count_tracked + count_untracked);
+ fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
+ "d\n", count_tracked);
+ fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
+ (100.0*count_tracked/(count_untracked+count_tracked)));
}
#endif
@@ -48,161 +48,161 @@ show_track(void)
PyObject *
PyTuple_New(register Py_ssize_t size)
{
- register PyTupleObject *op;
- Py_ssize_t i;
- if (size < 0) {
- PyErr_BadInternalCall();
- return NULL;
- }
+ register PyTupleObject *op;
+ Py_ssize_t i;
+ if (size < 0) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
#if PyTuple_MAXSAVESIZE > 0
- if (size == 0 && free_list[0]) {
- op = free_list[0];
- Py_INCREF(op);
+ if (size == 0 && free_list[0]) {
+ op = free_list[0];
+ Py_INCREF(op);
#ifdef COUNT_ALLOCS
- tuple_zero_allocs++;
+ tuple_zero_allocs++;
#endif
- return (PyObject *) op;
- }
- if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
- free_list[size] = (PyTupleObject *) op->ob_item[0];
- numfree[size]--;
+ return (PyObject *) op;
+ }
+ if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
+ free_list[size] = (PyTupleObject *) op->ob_item[0];
+ numfree[size]--;
#ifdef COUNT_ALLOCS
- fast_tuple_allocs++;
+ fast_tuple_allocs++;
#endif
- /* Inline PyObject_InitVar */
+ /* Inline PyObject_InitVar */
#ifdef Py_TRACE_REFS
- Py_SIZE(op) = size;
- Py_TYPE(op) = &PyTuple_Type;
+ Py_SIZE(op) = size;
+ Py_TYPE(op) = &PyTuple_Type;
#endif
- _Py_NewReference((PyObject *)op);
- }
- else
+ _Py_NewReference((PyObject *)op);
+ }
+ else
#endif
- {
- Py_ssize_t nbytes = size * sizeof(PyObject *);
- /* Check for overflow */
- if (nbytes / sizeof(PyObject *) != (size_t)size ||
- (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
- {
- return PyErr_NoMemory();
- }
- nbytes += sizeof(PyTupleObject) - sizeof(PyObject *);
-
- op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
- if (op == NULL)
- return NULL;
- }
- for (i=0; i < size; i++)
- op->ob_item[i] = NULL;
+ {
+ Py_ssize_t nbytes = size * sizeof(PyObject *);
+ /* Check for overflow */
+ if (nbytes / sizeof(PyObject *) != (size_t)size ||
+ (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
+ {
+ return PyErr_NoMemory();
+ }
+ nbytes += sizeof(PyTupleObject) - sizeof(PyObject *);
+
+ op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
+ if (op == NULL)
+ return NULL;
+ }
+ for (i=0; i < size; i++)
+ op->ob_item[i] = NULL;
#if PyTuple_MAXSAVESIZE > 0
- if (size == 0) {
- free_list[0] = op;
- ++numfree[0];
- Py_INCREF(op); /* extra INCREF so that this is never freed */
- }
+ if (size == 0) {
+ free_list[0] = op;
+ ++numfree[0];
+ Py_INCREF(op); /* extra INCREF so that this is never freed */
+ }
#endif
#ifdef SHOW_TRACK_COUNT
- count_tracked++;
+ count_tracked++;
#endif
- _PyObject_GC_TRACK(op);
- return (PyObject *) op;
+ _PyObject_GC_TRACK(op);
+ return (PyObject *) op;
}
Py_ssize_t
PyTuple_Size(register PyObject *op)
{
- if (!PyTuple_Check(op)) {
- PyErr_BadInternalCall();
- return -1;
- }
- else
- return Py_SIZE(op);
+ if (!PyTuple_Check(op)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ else
+ return Py_SIZE(op);
}
PyObject *
PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
{
- if (!PyTuple_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- if (i < 0 || i >= Py_SIZE(op)) {
- PyErr_SetString(PyExc_IndexError, "tuple index out of range");
- return NULL;
- }
- return ((PyTupleObject *)op) -> ob_item[i];
+ if (!PyTuple_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ if (i < 0 || i >= Py_SIZE(op)) {
+ PyErr_SetString(PyExc_IndexError, "tuple index out of range");
+ return NULL;
+ }
+ return ((PyTupleObject *)op) -> ob_item[i];
}
int
PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
{
- register PyObject *olditem;
- register PyObject **p;
- if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
- Py_XDECREF(newitem);
- PyErr_BadInternalCall();
- return -1;
- }
- if (i < 0 || i >= Py_SIZE(op)) {
- Py_XDECREF(newitem);
- PyErr_SetString(PyExc_IndexError,
- "tuple assignment index out of range");
- return -1;
- }
- p = ((PyTupleObject *)op) -> ob_item + i;
- olditem = *p;
- *p = newitem;
- Py_XDECREF(olditem);
- return 0;
+ register PyObject *olditem;
+ register PyObject **p;
+ if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
+ Py_XDECREF(newitem);
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ if (i < 0 || i >= Py_SIZE(op)) {
+ Py_XDECREF(newitem);
+ PyErr_SetString(PyExc_IndexError,
+ "tuple assignment index out of range");
+ return -1;
+ }
+ p = ((PyTupleObject *)op) -> ob_item + i;
+ olditem = *p;
+ *p = newitem;
+ Py_XDECREF(olditem);
+ return 0;
}
void
_PyTuple_MaybeUntrack(PyObject *op)
{
- PyTupleObject *t;
- Py_ssize_t i, n;
-
- if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
- return;
- t = (PyTupleObject *) op;
- n = Py_SIZE(t);
- for (i = 0; i < n; i++) {
- PyObject *elt = PyTuple_GET_ITEM(t, i);
- /* Tuple with NULL elements aren't
- fully constructed, don't untrack
- them yet. */
- if (!elt ||
- _PyObject_GC_MAY_BE_TRACKED(elt))
- return;
- }
+ PyTupleObject *t;
+ Py_ssize_t i, n;
+
+ if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
+ return;
+ t = (PyTupleObject *) op;
+ n = Py_SIZE(t);
+ for (i = 0; i < n; i++) {
+ PyObject *elt = PyTuple_GET_ITEM(t, i);
+ /* Tuple with NULL elements aren't
+ fully constructed, don't untrack
+ them yet. */
+ if (!elt ||
+ _PyObject_GC_MAY_BE_TRACKED(elt))
+ return;
+ }
#ifdef SHOW_TRACK_COUNT
- count_tracked--;
- count_untracked++;
+ count_tracked--;
+ count_untracked++;
#endif
- _PyObject_GC_UNTRACK(op);
+ _PyObject_GC_UNTRACK(op);
}
PyObject *
PyTuple_Pack(Py_ssize_t n, ...)
{
- Py_ssize_t i;
- PyObject *o;
- PyObject *result;
- PyObject **items;
- va_list vargs;
-
- va_start(vargs, n);
- result = PyTuple_New(n);
- if (result == NULL)
- return NULL;
- items = ((PyTupleObject *)result)->ob_item;
- for (i = 0; i < n; i++) {
- o = va_arg(vargs, PyObject *);
- Py_INCREF(o);
- items[i] = o;
- }
- va_end(vargs);
- return result;
+ Py_ssize_t i;
+ PyObject *o;
+ PyObject *result;
+ PyObject **items;
+ va_list vargs;
+
+ va_start(vargs, n);
+ result = PyTuple_New(n);
+ if (result == NULL)
+ return NULL;
+ items = ((PyTupleObject *)result)->ob_item;
+ for (i = 0; i < n; i++) {
+ o = va_arg(vargs, PyObject *);
+ Py_INCREF(o);
+ items[i] = o;
+ }
+ va_end(vargs);
+ return result;
}
@@ -211,405 +211,405 @@ PyTuple_Pack(Py_ssize_t n, ...)
static void
tupledealloc(register PyTupleObject *op)
{
- register Py_ssize_t i;
- register Py_ssize_t len = Py_SIZE(op);
- PyObject_GC_UnTrack(op);
- Py_TRASHCAN_SAFE_BEGIN(op)
- if (len > 0) {
- i = len;
- while (--i >= 0)
- Py_XDECREF(op->ob_item[i]);
+ register Py_ssize_t i;
+ register Py_ssize_t len = Py_SIZE(op);
+ PyObject_GC_UnTrack(op);
+ Py_TRASHCAN_SAFE_BEGIN(op)
+ if (len > 0) {
+ i = len;
+ while (--i >= 0)
+ Py_XDECREF(op->ob_item[i]);
#if PyTuple_MAXSAVESIZE > 0
- if (len < PyTuple_MAXSAVESIZE &&
- numfree[len] < PyTuple_MAXFREELIST &&
- Py_TYPE(op) == &PyTuple_Type)
- {
- op->ob_item[0] = (PyObject *) free_list[len];
- numfree[len]++;
- free_list[len] = op;
- goto done; /* return */
- }
+ if (len < PyTuple_MAXSAVESIZE &&
+ numfree[len] < PyTuple_MAXFREELIST &&
+ Py_TYPE(op) == &PyTuple_Type)
+ {
+ op->ob_item[0] = (PyObject *) free_list[len];
+ numfree[len]++;
+ free_list[len] = op;
+ goto done; /* return */
+ }
#endif
- }
- Py_TYPE(op)->tp_free((PyObject *)op);
+ }
+ Py_TYPE(op)->tp_free((PyObject *)op);
done:
- Py_TRASHCAN_SAFE_END(op)
+ Py_TRASHCAN_SAFE_END(op)
}
static PyObject *
tuplerepr(PyTupleObject *v)
{
- Py_ssize_t i, n;
- PyObject *s, *temp;
- PyObject *pieces, *result = NULL;
-
- n = Py_SIZE(v);
- if (n == 0)
- return PyUnicode_FromString("()");
-
- /* While not mutable, it is still possible to end up with a cycle in a
- tuple through an object that stores itself within a tuple (and thus
- infinitely asks for the repr of itself). This should only be
- possible within a type. */
- i = Py_ReprEnter((PyObject *)v);
- if (i != 0) {
- return i > 0 ? PyUnicode_FromString("(...)") : NULL;
- }
-
- pieces = PyTuple_New(n);
- if (pieces == NULL)
- return NULL;
-
- /* Do repr() on each element. */
- for (i = 0; i < n; ++i) {
- if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
- goto Done;
- s = PyObject_Repr(v->ob_item[i]);
- Py_LeaveRecursiveCall();
- if (s == NULL)
- goto Done;
- PyTuple_SET_ITEM(pieces, i, s);
- }
-
- /* Add "()" decorations to the first and last items. */
- assert(n > 0);
- s = PyUnicode_FromString("(");
- if (s == NULL)
- goto Done;
- temp = PyTuple_GET_ITEM(pieces, 0);
- PyUnicode_AppendAndDel(&s, temp);
- PyTuple_SET_ITEM(pieces, 0, s);
- if (s == NULL)
- goto Done;
-
- s = PyUnicode_FromString(n == 1 ? ",)" : ")");
- if (s == NULL)
- goto Done;
- temp = PyTuple_GET_ITEM(pieces, n-1);
- PyUnicode_AppendAndDel(&temp, s);
- PyTuple_SET_ITEM(pieces, n-1, temp);
- if (temp == NULL)
- goto Done;
-
- /* Paste them all together with ", " between. */
- s = PyUnicode_FromString(", ");
- if (s == NULL)
- goto Done;
- result = PyUnicode_Join(s, pieces);
- Py_DECREF(s);
+ Py_ssize_t i, n;
+ PyObject *s, *temp;
+ PyObject *pieces, *result = NULL;
+
+ n = Py_SIZE(v);
+ if (n == 0)
+ return PyUnicode_FromString("()");
+
+ /* While not mutable, it is still possible to end up with a cycle in a
+ tuple through an object that stores itself within a tuple (and thus
+ infinitely asks for the repr of itself). This should only be
+ possible within a type. */
+ i = Py_ReprEnter((PyObject *)v);
+ if (i != 0) {
+ return i > 0 ? PyUnicode_FromString("(...)") : NULL;
+ }
+
+ pieces = PyTuple_New(n);
+ if (pieces == NULL)
+ return NULL;
+
+ /* Do repr() on each element. */
+ for (i = 0; i < n; ++i) {
+ if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
+ goto Done;
+ s = PyObject_Repr(v->ob_item[i]);
+ Py_LeaveRecursiveCall();
+ if (s == NULL)
+ goto Done;
+ PyTuple_SET_ITEM(pieces, i, s);
+ }
+
+ /* Add "()" decorations to the first and last items. */
+ assert(n > 0);
+ s = PyUnicode_FromString("(");
+ if (s == NULL)
+ goto Done;
+ temp = PyTuple_GET_ITEM(pieces, 0);
+ PyUnicode_AppendAndDel(&s, temp);
+ PyTuple_SET_ITEM(pieces, 0, s);
+ if (s == NULL)
+ goto Done;
+
+ s = PyUnicode_FromString(n == 1 ? ",)" : ")");
+ if (s == NULL)
+ goto Done;
+ temp = PyTuple_GET_ITEM(pieces, n-1);
+ PyUnicode_AppendAndDel(&temp, s);
+ PyTuple_SET_ITEM(pieces, n-1, temp);
+ if (temp == NULL)
+ goto Done;
+
+ /* Paste them all together with ", " between. */
+ s = PyUnicode_FromString(", ");
+ if (s == NULL)
+ goto Done;
+ result = PyUnicode_Join(s, pieces);
+ Py_DECREF(s);
Done:
- Py_DECREF(pieces);
- Py_ReprLeave((PyObject *)v);
- return result;
+ Py_DECREF(pieces);
+ Py_ReprLeave((PyObject *)v);
+ return result;
}
-/* The addend 82520, was selected from the range(0, 1000000) for
- generating the greatest number of prime multipliers for tuples
+/* The addend 82520, was selected from the range(0, 1000000) for
+ generating the greatest number of prime multipliers for tuples
upto length eight:
- 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
+ 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
1330111, 1412633, 1165069, 1247599, 1495177, 1577699
*/
static long
tuplehash(PyTupleObject *v)
{
- register long x, y;
- register Py_ssize_t len = Py_SIZE(v);
- register PyObject **p;
- long mult = 1000003L;
- x = 0x345678L;
- p = v->ob_item;
- while (--len >= 0) {
- y = PyObject_Hash(*p++);
- if (y == -1)
- return -1;
- x = (x ^ y) * mult;
- /* the cast might truncate len; that doesn't change hash stability */
- mult += (long)(82520L + len + len);
- }
- x += 97531L;
- if (x == -1)
- x = -2;
- return x;
+ register long x, y;
+ register Py_ssize_t len = Py_SIZE(v);
+ register PyObject **p;
+ long mult = 1000003L;
+ x = 0x345678L;
+ p = v->ob_item;
+ while (--len >= 0) {
+ y = PyObject_Hash(*p++);
+ if (y == -1)
+ return -1;
+ x = (x ^ y) * mult;
+ /* the cast might truncate len; that doesn't change hash stability */
+ mult += (long)(82520L + len + len);
+ }
+ x += 97531L;
+ if (x == -1)
+ x = -2;
+ return x;
}
static Py_ssize_t
tuplelength(PyTupleObject *a)
{
- return Py_SIZE(a);
+ return Py_SIZE(a);
}
static int
tuplecontains(PyTupleObject *a, PyObject *el)
{
- Py_ssize_t i;
- int cmp;
+ Py_ssize_t i;
+ int cmp;
- for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
- cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
- Py_EQ);
- return cmp;
+ for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
+ cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
+ Py_EQ);
+ return cmp;
}
static PyObject *
tupleitem(register PyTupleObject *a, register Py_ssize_t i)
{
- if (i < 0 || i >= Py_SIZE(a)) {
- PyErr_SetString(PyExc_IndexError, "tuple index out of range");
- return NULL;
- }
- Py_INCREF(a->ob_item[i]);
- return a->ob_item[i];
+ if (i < 0 || i >= Py_SIZE(a)) {
+ PyErr_SetString(PyExc_IndexError, "tuple index out of range");
+ return NULL;
+ }
+ Py_INCREF(a->ob_item[i]);
+ return a->ob_item[i];
}
static PyObject *
-tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
- register Py_ssize_t ihigh)
+tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
+ register Py_ssize_t ihigh)
{
- register PyTupleObject *np;
- PyObject **src, **dest;
- register Py_ssize_t i;
- Py_ssize_t len;
- if (ilow < 0)
- ilow = 0;
- if (ihigh > Py_SIZE(a))
- ihigh = Py_SIZE(a);
- if (ihigh < ilow)
- ihigh = ilow;
- if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
- Py_INCREF(a);
- return (PyObject *)a;
- }
- len = ihigh - ilow;
- np = (PyTupleObject *)PyTuple_New(len);
- if (np == NULL)
- return NULL;
- src = a->ob_item + ilow;
- dest = np->ob_item;
- for (i = 0; i < len; i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- return (PyObject *)np;
+ register PyTupleObject *np;
+ PyObject **src, **dest;
+ register Py_ssize_t i;
+ Py_ssize_t len;
+ if (ilow < 0)
+ ilow = 0;
+ if (ihigh > Py_SIZE(a))
+ ihigh = Py_SIZE(a);
+ if (ihigh < ilow)
+ ihigh = ilow;
+ if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
+ Py_INCREF(a);
+ return (PyObject *)a;
+ }
+ len = ihigh - ilow;
+ np = (PyTupleObject *)PyTuple_New(len);
+ if (np == NULL)
+ return NULL;
+ src = a->ob_item + ilow;
+ dest = np->ob_item;
+ for (i = 0; i < len; i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ return (PyObject *)np;
}
PyObject *
PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
{
- if (op == NULL || !PyTuple_Check(op)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- return tupleslice((PyTupleObject *)op, i, j);
+ if (op == NULL || !PyTuple_Check(op)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ return tupleslice((PyTupleObject *)op, i, j);
}
static PyObject *
tupleconcat(register PyTupleObject *a, register PyObject *bb)
{
- register Py_ssize_t size;
- register Py_ssize_t i;
- PyObject **src, **dest;
- PyTupleObject *np;
- if (!PyTuple_Check(bb)) {
- PyErr_Format(PyExc_TypeError,
- "can only concatenate tuple (not \"%.200s\") to tuple",
- Py_TYPE(bb)->tp_name);
- return NULL;
- }
+ register Py_ssize_t size;
+ register Py_ssize_t i;
+ PyObject **src, **dest;
+ PyTupleObject *np;
+ if (!PyTuple_Check(bb)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only concatenate tuple (not \"%.200s\") to tuple",
+ Py_TYPE(bb)->tp_name);
+ return NULL;
+ }
#define b ((PyTupleObject *)bb)
- size = Py_SIZE(a) + Py_SIZE(b);
- if (size < 0)
- return PyErr_NoMemory();
- np = (PyTupleObject *) PyTuple_New(size);
- if (np == NULL) {
- return NULL;
- }
- src = a->ob_item;
- dest = np->ob_item;
- for (i = 0; i < Py_SIZE(a); i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- src = b->ob_item;
- dest = np->ob_item + Py_SIZE(a);
- for (i = 0; i < Py_SIZE(b); i++) {
- PyObject *v = src[i];
- Py_INCREF(v);
- dest[i] = v;
- }
- return (PyObject *)np;
+ size = Py_SIZE(a) + Py_SIZE(b);
+ if (size < 0)
+ return PyErr_NoMemory();
+ np = (PyTupleObject *) PyTuple_New(size);
+ if (np == NULL) {
+ return NULL;
+ }
+ src = a->ob_item;
+ dest = np->ob_item;
+ for (i = 0; i < Py_SIZE(a); i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ src = b->ob_item;
+ dest = np->ob_item + Py_SIZE(a);
+ for (i = 0; i < Py_SIZE(b); i++) {
+ PyObject *v = src[i];
+ Py_INCREF(v);
+ dest[i] = v;
+ }
+ return (PyObject *)np;
#undef b
}
static PyObject *
tuplerepeat(PyTupleObject *a, Py_ssize_t n)
{
- Py_ssize_t i, j;
- Py_ssize_t size;
- PyTupleObject *np;
- PyObject **p, **items;
- if (n < 0)
- n = 0;
- if (Py_SIZE(a) == 0 || n == 1) {
- if (PyTuple_CheckExact(a)) {
- /* Since tuples are immutable, we can return a shared
- copy in this case */
- Py_INCREF(a);
- return (PyObject *)a;
- }
- if (Py_SIZE(a) == 0)
- return PyTuple_New(0);
- }
- size = Py_SIZE(a) * n;
- if (size/Py_SIZE(a) != n)
- return PyErr_NoMemory();
- np = (PyTupleObject *) PyTuple_New(size);
- if (np == NULL)
- return NULL;
- p = np->ob_item;
- items = a->ob_item;
- for (i = 0; i < n; i++) {
- for (j = 0; j < Py_SIZE(a); j++) {
- *p = items[j];
- Py_INCREF(*p);
- p++;
- }
- }
- return (PyObject *) np;
+ Py_ssize_t i, j;
+ Py_ssize_t size;
+ PyTupleObject *np;
+ PyObject **p, **items;
+ if (n < 0)
+ n = 0;
+ if (Py_SIZE(a) == 0 || n == 1) {
+ if (PyTuple_CheckExact(a)) {
+ /* Since tuples are immutable, we can return a shared
+ copy in this case */
+ Py_INCREF(a);
+ return (PyObject *)a;
+ }
+ if (Py_SIZE(a) == 0)
+ return PyTuple_New(0);
+ }
+ size = Py_SIZE(a) * n;
+ if (size/Py_SIZE(a) != n)
+ return PyErr_NoMemory();
+ np = (PyTupleObject *) PyTuple_New(size);
+ if (np == NULL)
+ return NULL;
+ p = np->ob_item;
+ items = a->ob_item;
+ for (i = 0; i < n; i++) {
+ for (j = 0; j < Py_SIZE(a); j++) {
+ *p = items[j];
+ Py_INCREF(*p);
+ p++;
+ }
+ }
+ return (PyObject *) np;
}
static PyObject *
tupleindex(PyTupleObject *self, PyObject *args)
{
- Py_ssize_t i, start=0, stop=Py_SIZE(self);
- PyObject *v;
-
- if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
- _PyEval_SliceIndex, &start,
- _PyEval_SliceIndex, &stop))
- return NULL;
- if (start < 0) {
- start += Py_SIZE(self);
- if (start < 0)
- start = 0;
- }
- if (stop < 0) {
- stop += Py_SIZE(self);
- if (stop < 0)
- stop = 0;
- }
- for (i = start; i < stop && i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
- if (cmp > 0)
- return PyLong_FromSsize_t(i);
- else if (cmp < 0)
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
- return NULL;
+ Py_ssize_t i, start=0, stop=Py_SIZE(self);
+ PyObject *v;
+
+ if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
+ _PyEval_SliceIndex, &start,
+ _PyEval_SliceIndex, &stop))
+ return NULL;
+ if (start < 0) {
+ start += Py_SIZE(self);
+ if (start < 0)
+ start = 0;
+ }
+ if (stop < 0) {
+ stop += Py_SIZE(self);
+ if (stop < 0)
+ stop = 0;
+ }
+ for (i = start; i < stop && i < Py_SIZE(self); i++) {
+ int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
+ if (cmp > 0)
+ return PyLong_FromSsize_t(i);
+ else if (cmp < 0)
+ return NULL;
+ }
+ PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
+ return NULL;
}
static PyObject *
tuplecount(PyTupleObject *self, PyObject *v)
{
- Py_ssize_t count = 0;
- Py_ssize_t i;
-
- for (i = 0; i < Py_SIZE(self); i++) {
- int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
- if (cmp > 0)
- count++;
- else if (cmp < 0)
- return NULL;
- }
- return PyLong_FromSsize_t(count);
+ Py_ssize_t count = 0;
+ Py_ssize_t i;
+
+ for (i = 0; i < Py_SIZE(self); i++) {
+ int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
+ if (cmp > 0)
+ count++;
+ else if (cmp < 0)
+ return NULL;
+ }
+ return PyLong_FromSsize_t(count);
}
static int
tupletraverse(PyTupleObject *o, visitproc visit, void *arg)
{
- Py_ssize_t i;
+ Py_ssize_t i;
- for (i = Py_SIZE(o); --i >= 0; )
- Py_VISIT(o->ob_item[i]);
- return 0;
+ for (i = Py_SIZE(o); --i >= 0; )
+ Py_VISIT(o->ob_item[i]);
+ return 0;
}
static PyObject *
tuplerichcompare(PyObject *v, PyObject *w, int op)
{
- PyTupleObject *vt, *wt;
- Py_ssize_t i;
- Py_ssize_t vlen, wlen;
-
- if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
-
- vt = (PyTupleObject *)v;
- wt = (PyTupleObject *)w;
-
- vlen = Py_SIZE(vt);
- wlen = Py_SIZE(wt);
-
- /* Note: the corresponding code for lists has an "early out" test
- * here when op is EQ or NE and the lengths differ. That pays there,
- * but Tim was unable to find any real code where EQ/NE tuple
- * compares don't have the same length, so testing for it here would
- * have cost without benefit.
- */
-
- /* Search for the first index where items are different.
- * Note that because tuples are immutable, it's safe to reuse
- * vlen and wlen across the comparison calls.
- */
- for (i = 0; i < vlen && i < wlen; i++) {
- int k = PyObject_RichCompareBool(vt->ob_item[i],
- wt->ob_item[i], Py_EQ);
- if (k < 0)
- return NULL;
- if (!k)
- break;
- }
-
- if (i >= vlen || i >= wlen) {
- /* No more items to compare -- compare sizes */
- int cmp;
- PyObject *res;
- switch (op) {
- case Py_LT: cmp = vlen < wlen; break;
- case Py_LE: cmp = vlen <= wlen; break;
- case Py_EQ: cmp = vlen == wlen; break;
- case Py_NE: cmp = vlen != wlen; break;
- case Py_GT: cmp = vlen > wlen; break;
- case Py_GE: cmp = vlen >= wlen; break;
- default: return NULL; /* cannot happen */
- }
- if (cmp)
- res = Py_True;
- else
- res = Py_False;
- Py_INCREF(res);
- return res;
- }
-
- /* We have an item that differs -- shortcuts for EQ/NE */
- if (op == Py_EQ) {
- Py_INCREF(Py_False);
- return Py_False;
- }
- if (op == Py_NE) {
- Py_INCREF(Py_True);
- return Py_True;
- }
-
- /* Compare the final item again using the proper operator */
- return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
+ PyTupleObject *vt, *wt;
+ Py_ssize_t i;
+ Py_ssize_t vlen, wlen;
+
+ if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+
+ vt = (PyTupleObject *)v;
+ wt = (PyTupleObject *)w;
+
+ vlen = Py_SIZE(vt);
+ wlen = Py_SIZE(wt);
+
+ /* Note: the corresponding code for lists has an "early out" test
+ * here when op is EQ or NE and the lengths differ. That pays there,
+ * but Tim was unable to find any real code where EQ/NE tuple
+ * compares don't have the same length, so testing for it here would
+ * have cost without benefit.
+ */
+
+ /* Search for the first index where items are different.
+ * Note that because tuples are immutable, it's safe to reuse
+ * vlen and wlen across the comparison calls.
+ */
+ for (i = 0; i < vlen && i < wlen; i++) {
+ int k = PyObject_RichCompareBool(vt->ob_item[i],
+ wt->ob_item[i], Py_EQ);
+ if (k < 0)
+ return NULL;
+ if (!k)
+ break;
+ }
+
+ if (i >= vlen || i >= wlen) {
+ /* No more items to compare -- compare sizes */
+ int cmp;
+ PyObject *res;
+ switch (op) {
+ case Py_LT: cmp = vlen < wlen; break;
+ case Py_LE: cmp = vlen <= wlen; break;
+ case Py_EQ: cmp = vlen == wlen; break;
+ case Py_NE: cmp = vlen != wlen; break;
+ case Py_GT: cmp = vlen > wlen; break;
+ case Py_GE: cmp = vlen >= wlen; break;
+ default: return NULL; /* cannot happen */
+ }
+ if (cmp)
+ res = Py_True;
+ else
+ res = Py_False;
+ Py_INCREF(res);
+ return res;
+ }
+
+ /* We have an item that differs -- shortcuts for EQ/NE */
+ if (op == Py_EQ) {
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
+ if (op == Py_NE) {
+ Py_INCREF(Py_True);
+ return Py_True;
+ }
+
+ /* Compare the final item again using the proper operator */
+ return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
}
static PyObject *
@@ -618,41 +618,41 @@ tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *arg = NULL;
- static char *kwlist[] = {"sequence", 0};
-
- if (type != &PyTuple_Type)
- return tuple_subtype_new(type, args, kwds);
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
- return NULL;
-
- if (arg == NULL)
- return PyTuple_New(0);
- else
- return PySequence_Tuple(arg);
+ PyObject *arg = NULL;
+ static char *kwlist[] = {"sequence", 0};
+
+ if (type != &PyTuple_Type)
+ return tuple_subtype_new(type, args, kwds);
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
+ return NULL;
+
+ if (arg == NULL)
+ return PyTuple_New(0);
+ else
+ return PySequence_Tuple(arg);
}
static PyObject *
tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *tmp, *newobj, *item;
- Py_ssize_t i, n;
-
- assert(PyType_IsSubtype(type, &PyTuple_Type));
- tmp = tuple_new(&PyTuple_Type, args, kwds);
- if (tmp == NULL)
- return NULL;
- assert(PyTuple_Check(tmp));
- newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
- if (newobj == NULL)
- return NULL;
- for (i = 0; i < n; i++) {
- item = PyTuple_GET_ITEM(tmp, i);
- Py_INCREF(item);
- PyTuple_SET_ITEM(newobj, i, item);
- }
- Py_DECREF(tmp);
- return newobj;
+ PyObject *tmp, *newobj, *item;
+ Py_ssize_t i, n;
+
+ assert(PyType_IsSubtype(type, &PyTuple_Type));
+ tmp = tuple_new(&PyTuple_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyTuple_Check(tmp));
+ newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
+ if (newobj == NULL)
+ return NULL;
+ for (i = 0; i < n; i++) {
+ item = PyTuple_GET_ITEM(tmp, i);
+ Py_INCREF(item);
+ PyTuple_SET_ITEM(newobj, i, item);
+ }
+ Py_DECREF(tmp);
+ return newobj;
}
PyDoc_STRVAR(tuple_doc,
@@ -662,86 +662,86 @@ tuple(iterable) -> tuple initialized from iterable's items\n\
If the argument is a tuple, the return value is the same object.");
static PySequenceMethods tuple_as_sequence = {
- (lenfunc)tuplelength, /* sq_length */
- (binaryfunc)tupleconcat, /* sq_concat */
- (ssizeargfunc)tuplerepeat, /* sq_repeat */
- (ssizeargfunc)tupleitem, /* sq_item */
- 0, /* sq_slice */
- 0, /* sq_ass_item */
- 0, /* sq_ass_slice */
- (objobjproc)tuplecontains, /* sq_contains */
+ (lenfunc)tuplelength, /* sq_length */
+ (binaryfunc)tupleconcat, /* sq_concat */
+ (ssizeargfunc)tuplerepeat, /* sq_repeat */
+ (ssizeargfunc)tupleitem, /* sq_item */
+ 0, /* sq_slice */
+ 0, /* sq_ass_item */
+ 0, /* sq_ass_slice */
+ (objobjproc)tuplecontains, /* sq_contains */
};
static PyObject*
tuplesubscript(PyTupleObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
- Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- if (i < 0)
- i += PyTuple_GET_SIZE(self);
- return tupleitem(self, i);
- }
- else if (PySlice_Check(item)) {
- Py_ssize_t start, stop, step, slicelength, cur, i;
- PyObject* result;
- PyObject* it;
- PyObject **src, **dest;
-
- if (PySlice_GetIndicesEx((PySliceObject*)item,
- PyTuple_GET_SIZE(self),
- &start, &stop, &step, &slicelength) < 0) {
- return NULL;
- }
-
- if (slicelength <= 0) {
- return PyTuple_New(0);
- }
- else if (start == 0 && step == 1 &&
- slicelength == PyTuple_GET_SIZE(self) &&
- PyTuple_CheckExact(self)) {
- Py_INCREF(self);
- return (PyObject *)self;
- }
- else {
- result = PyTuple_New(slicelength);
- if (!result) return NULL;
-
- src = self->ob_item;
- dest = ((PyTupleObject *)result)->ob_item;
- for (cur = start, i = 0; i < slicelength;
- cur += step, i++) {
- it = src[cur];
- Py_INCREF(it);
- dest[i] = it;
- }
-
- return result;
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "tuple indices must be integers, not %.200s",
- Py_TYPE(item)->tp_name);
- return NULL;
- }
+ if (PyIndex_Check(item)) {
+ Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ if (i < 0)
+ i += PyTuple_GET_SIZE(self);
+ return tupleitem(self, i);
+ }
+ else if (PySlice_Check(item)) {
+ Py_ssize_t start, stop, step, slicelength, cur, i;
+ PyObject* result;
+ PyObject* it;
+ PyObject **src, **dest;
+
+ if (PySlice_GetIndicesEx((PySliceObject*)item,
+ PyTuple_GET_SIZE(self),
+ &start, &stop, &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ if (slicelength <= 0) {
+ return PyTuple_New(0);
+ }
+ else if (start == 0 && step == 1 &&
+ slicelength == PyTuple_GET_SIZE(self) &&
+ PyTuple_CheckExact(self)) {
+ Py_INCREF(self);
+ return (PyObject *)self;
+ }
+ else {
+ result = PyTuple_New(slicelength);
+ if (!result) return NULL;
+
+ src = self->ob_item;
+ dest = ((PyTupleObject *)result)->ob_item;
+ for (cur = start, i = 0; i < slicelength;
+ cur += step, i++) {
+ it = src[cur];
+ Py_INCREF(it);
+ dest[i] = it;
+ }
+
+ return result;
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "tuple indices must be integers, not %.200s",
+ Py_TYPE(item)->tp_name);
+ return NULL;
+ }
}
static PyObject *
tuple_getnewargs(PyTupleObject *v)
{
- return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
-
+ return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
+
}
static PyObject *
tuple_sizeof(PyTupleObject *self)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
- return PyLong_FromSsize_t(res);
+ res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
+ return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(index_doc,
@@ -754,62 +754,62 @@ PyDoc_STRVAR(sizeof_doc,
"T.__sizeof__() -- size of T in memory, in bytes");
static PyMethodDef tuple_methods[] = {
- {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
- {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
- {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
- {"count", (PyCFunction)tuplecount, METH_O, count_doc},
- {NULL, NULL} /* sentinel */
+ {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
+ {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
+ {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
+ {"count", (PyCFunction)tuplecount, METH_O, count_doc},
+ {NULL, NULL} /* sentinel */
};
static PyMappingMethods tuple_as_mapping = {
- (lenfunc)tuplelength,
- (binaryfunc)tuplesubscript,
- 0
+ (lenfunc)tuplelength,
+ (binaryfunc)tuplesubscript,
+ 0
};
static PyObject *tuple_iter(PyObject *seq);
PyTypeObject PyTuple_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "tuple",
- sizeof(PyTupleObject) - sizeof(PyObject *),
- sizeof(PyObject *),
- (destructor)tupledealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)tuplerepr, /* tp_repr */
- 0, /* tp_as_number */
- &tuple_as_sequence, /* tp_as_sequence */
- &tuple_as_mapping, /* tp_as_mapping */
- (hashfunc)tuplehash, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
- tuple_doc, /* tp_doc */
- (traverseproc)tupletraverse, /* tp_traverse */
- 0, /* tp_clear */
- tuplerichcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- tuple_iter, /* tp_iter */
- 0, /* tp_iternext */
- tuple_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- tuple_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "tuple",
+ sizeof(PyTupleObject) - sizeof(PyObject *),
+ sizeof(PyObject *),
+ (destructor)tupledealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)tuplerepr, /* tp_repr */
+ 0, /* tp_as_number */
+ &tuple_as_sequence, /* tp_as_sequence */
+ &tuple_as_mapping, /* tp_as_mapping */
+ (hashfunc)tuplehash, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
+ tuple_doc, /* tp_doc */
+ (traverseproc)tupletraverse, /* tp_traverse */
+ 0, /* tp_clear */
+ tuplerichcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ tuple_iter, /* tp_iter */
+ 0, /* tp_iternext */
+ tuple_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ tuple_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
/* The following function breaks the notion that tuples are immutable:
@@ -822,207 +822,207 @@ PyTypeObject PyTuple_Type = {
int
_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
{
- register PyTupleObject *v;
- register PyTupleObject *sv;
- Py_ssize_t i;
- Py_ssize_t oldsize;
-
- v = (PyTupleObject *) *pv;
- if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
- (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
- *pv = 0;
- Py_XDECREF(v);
- PyErr_BadInternalCall();
- return -1;
- }
- oldsize = Py_SIZE(v);
- if (oldsize == newsize)
- return 0;
-
- if (oldsize == 0) {
- /* Empty tuples are often shared, so we should never
- resize them in-place even if we do own the only
- (current) reference */
- Py_DECREF(v);
- *pv = PyTuple_New(newsize);
- return *pv == NULL ? -1 : 0;
- }
-
- /* XXX UNREF/NEWREF interface should be more symmetrical */
- _Py_DEC_REFTOTAL;
- if (_PyObject_GC_IS_TRACKED(v))
- _PyObject_GC_UNTRACK(v);
- _Py_ForgetReference((PyObject *) v);
- /* DECREF items deleted by shrinkage */
- for (i = newsize; i < oldsize; i++) {
- Py_XDECREF(v->ob_item[i]);
- v->ob_item[i] = NULL;
- }
- sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
- if (sv == NULL) {
- *pv = NULL;
- PyObject_GC_Del(v);
- return -1;
- }
- _Py_NewReference((PyObject *) sv);
- /* Zero out items added by growing */
- if (newsize > oldsize)
- memset(&sv->ob_item[oldsize], 0,
- sizeof(*sv->ob_item) * (newsize - oldsize));
- *pv = (PyObject *) sv;
- _PyObject_GC_TRACK(sv);
- return 0;
+ register PyTupleObject *v;
+ register PyTupleObject *sv;
+ Py_ssize_t i;
+ Py_ssize_t oldsize;
+
+ v = (PyTupleObject *) *pv;
+ if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
+ (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
+ *pv = 0;
+ Py_XDECREF(v);
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ oldsize = Py_SIZE(v);
+ if (oldsize == newsize)
+ return 0;
+
+ if (oldsize == 0) {
+ /* Empty tuples are often shared, so we should never
+ resize them in-place even if we do own the only
+ (current) reference */
+ Py_DECREF(v);
+ *pv = PyTuple_New(newsize);
+ return *pv == NULL ? -1 : 0;
+ }
+
+ /* XXX UNREF/NEWREF interface should be more symmetrical */
+ _Py_DEC_REFTOTAL;
+ if (_PyObject_GC_IS_TRACKED(v))
+ _PyObject_GC_UNTRACK(v);
+ _Py_ForgetReference((PyObject *) v);
+ /* DECREF items deleted by shrinkage */
+ for (i = newsize; i < oldsize; i++) {
+ Py_XDECREF(v->ob_item[i]);
+ v->ob_item[i] = NULL;
+ }
+ sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
+ if (sv == NULL) {
+ *pv = NULL;
+ PyObject_GC_Del(v);
+ return -1;
+ }
+ _Py_NewReference((PyObject *) sv);
+ /* Zero out items added by growing */
+ if (newsize > oldsize)
+ memset(&sv->ob_item[oldsize], 0,
+ sizeof(*sv->ob_item) * (newsize - oldsize));
+ *pv = (PyObject *) sv;
+ _PyObject_GC_TRACK(sv);
+ return 0;
}
int
PyTuple_ClearFreeList(void)
{
- int freelist_size = 0;
+ int freelist_size = 0;
#if PyTuple_MAXSAVESIZE > 0
- int i;
- for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
- PyTupleObject *p, *q;
- p = free_list[i];
- freelist_size += numfree[i];
- free_list[i] = NULL;
- numfree[i] = 0;
- while (p) {
- q = p;
- p = (PyTupleObject *)(p->ob_item[0]);
- PyObject_GC_Del(q);
- }
- }
+ int i;
+ for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
+ PyTupleObject *p, *q;
+ p = free_list[i];
+ freelist_size += numfree[i];
+ free_list[i] = NULL;
+ numfree[i] = 0;
+ while (p) {
+ q = p;
+ p = (PyTupleObject *)(p->ob_item[0]);
+ PyObject_GC_Del(q);
+ }
+ }
#endif
- return freelist_size;
+ return freelist_size;
}
-
+
void
PyTuple_Fini(void)
{
#if PyTuple_MAXSAVESIZE > 0
- /* empty tuples are used all over the place and applications may
- * rely on the fact that an empty tuple is a singleton. */
- Py_XDECREF(free_list[0]);
- free_list[0] = NULL;
+ /* empty tuples are used all over the place and applications may
+ * rely on the fact that an empty tuple is a singleton. */
+ Py_XDECREF(free_list[0]);
+ free_list[0] = NULL;
- (void)PyTuple_ClearFreeList();
+ (void)PyTuple_ClearFreeList();
#endif
#ifdef SHOW_TRACK_COUNT
- show_track();
+ show_track();
#endif
}
/*********************** Tuple Iterator **************************/
typedef struct {
- PyObject_HEAD
- long it_index;
- PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
+ PyObject_HEAD
+ long it_index;
+ PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
} tupleiterobject;
static void
tupleiter_dealloc(tupleiterobject *it)
{
- _PyObject_GC_UNTRACK(it);
- Py_XDECREF(it->it_seq);
- PyObject_GC_Del(it);
+ _PyObject_GC_UNTRACK(it);
+ Py_XDECREF(it->it_seq);
+ PyObject_GC_Del(it);
}
static int
tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
{
- Py_VISIT(it->it_seq);
- return 0;
+ Py_VISIT(it->it_seq);
+ return 0;
}
static PyObject *
tupleiter_next(tupleiterobject *it)
{
- PyTupleObject *seq;
- PyObject *item;
-
- assert(it != NULL);
- seq = it->it_seq;
- if (seq == NULL)
- return NULL;
- assert(PyTuple_Check(seq));
-
- if (it->it_index < PyTuple_GET_SIZE(seq)) {
- item = PyTuple_GET_ITEM(seq, it->it_index);
- ++it->it_index;
- Py_INCREF(item);
- return item;
- }
-
- Py_DECREF(seq);
- it->it_seq = NULL;
- return NULL;
+ PyTupleObject *seq;
+ PyObject *item;
+
+ assert(it != NULL);
+ seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
+ assert(PyTuple_Check(seq));
+
+ if (it->it_index < PyTuple_GET_SIZE(seq)) {
+ item = PyTuple_GET_ITEM(seq, it->it_index);
+ ++it->it_index;
+ Py_INCREF(item);
+ return item;
+ }
+
+ Py_DECREF(seq);
+ it->it_seq = NULL;
+ return NULL;
}
static PyObject *
tupleiter_len(tupleiterobject *it)
{
- Py_ssize_t len = 0;
- if (it->it_seq)
- len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
- return PyLong_FromSsize_t(len);
+ Py_ssize_t len = 0;
+ if (it->it_seq)
+ len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
+ return PyLong_FromSsize_t(len);
}
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
static PyMethodDef tupleiter_methods[] = {
- {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
- {NULL, NULL} /* sentinel */
+ {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyTupleIter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "tuple_iterator", /* tp_name */
- sizeof(tupleiterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)tupleiter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
- 0, /* tp_doc */
- (traverseproc)tupleiter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)tupleiter_next, /* tp_iternext */
- tupleiter_methods, /* tp_methods */
- 0,
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "tuple_iterator", /* tp_name */
+ sizeof(tupleiterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)tupleiter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ 0, /* tp_doc */
+ (traverseproc)tupleiter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)tupleiter_next, /* tp_iternext */
+ tupleiter_methods, /* tp_methods */
+ 0,
};
static PyObject *
tuple_iter(PyObject *seq)
{
- tupleiterobject *it;
-
- if (!PyTuple_Check(seq)) {
- PyErr_BadInternalCall();
- return NULL;
- }
- it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
- if (it == NULL)
- return NULL;
- it->it_index = 0;
- Py_INCREF(seq);
- it->it_seq = (PyTupleObject *)seq;
- _PyObject_GC_TRACK(it);
- return (PyObject *)it;
+ tupleiterobject *it;
+
+ if (!PyTuple_Check(seq)) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
+ if (it == NULL)
+ return NULL;
+ it->it_index = 0;
+ Py_INCREF(seq);
+ it->it_seq = (PyTupleObject *)seq;
+ _PyObject_GC_TRACK(it);
+ return (PyObject *)it;
}
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 07a8cd9..a94508d 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -13,22 +13,22 @@
they normally would. This is why the maximum size is limited to
MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
strings are used as attribute names. */
-#define MCACHE_MAX_ATTR_SIZE 100
-#define MCACHE_SIZE_EXP 10
-#define MCACHE_HASH(version, name_hash) \
- (((unsigned int)(version) * (unsigned int)(name_hash)) \
- >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
+#define MCACHE_MAX_ATTR_SIZE 100
+#define MCACHE_SIZE_EXP 10
+#define MCACHE_HASH(version, name_hash) \
+ (((unsigned int)(version) * (unsigned int)(name_hash)) \
+ >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
#define MCACHE_HASH_METHOD(type, name) \
- MCACHE_HASH((type)->tp_version_tag, \
- ((PyUnicodeObject *)(name))->hash)
+ MCACHE_HASH((type)->tp_version_tag, \
+ ((PyUnicodeObject *)(name))->hash)
#define MCACHE_CACHEABLE_NAME(name) \
- PyUnicode_CheckExact(name) && \
- PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
+ PyUnicode_CheckExact(name) && \
+ PyUnicode_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
struct method_cache_entry {
- unsigned int version;
- PyObject *name; /* reference to exactly a str or None */
- PyObject *value; /* borrowed */
+ unsigned int version;
+ PyObject *name; /* reference to exactly a str or None */
+ PyObject *value; /* borrowed */
};
static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
@@ -37,325 +37,325 @@ static unsigned int next_version_tag = 0;
unsigned int
PyType_ClearCache(void)
{
- Py_ssize_t i;
- unsigned int cur_version_tag = next_version_tag - 1;
-
- for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
- method_cache[i].version = 0;
- Py_CLEAR(method_cache[i].name);
- method_cache[i].value = NULL;
- }
- next_version_tag = 0;
- /* mark all version tags as invalid */
- PyType_Modified(&PyBaseObject_Type);
- return cur_version_tag;
+ Py_ssize_t i;
+ unsigned int cur_version_tag = next_version_tag - 1;
+
+ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
+ method_cache[i].version = 0;
+ Py_CLEAR(method_cache[i].name);
+ method_cache[i].value = NULL;
+ }
+ next_version_tag = 0;
+ /* mark all version tags as invalid */
+ PyType_Modified(&PyBaseObject_Type);
+ return cur_version_tag;
}
void
PyType_Modified(PyTypeObject *type)
{
- /* Invalidate any cached data for the specified type and all
- subclasses. This function is called after the base
- classes, mro, or attributes of the type are altered.
-
- Invariants:
-
- - Py_TPFLAGS_VALID_VERSION_TAG is never set if
- Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
- objects coming from non-recompiled extension modules)
-
- - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
- it must first be set on all super types.
-
- This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
- type (so it must first clear it on all subclasses). The
- tp_version_tag value is meaningless unless this flag is set.
- We don't assign new version tags eagerly, but only as
- needed.
- */
- PyObject *raw, *ref;
- Py_ssize_t i, n;
-
- if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
- return;
-
- raw = type->tp_subclasses;
- if (raw != NULL) {
- n = PyList_GET_SIZE(raw);
- for (i = 0; i < n; i++) {
- ref = PyList_GET_ITEM(raw, i);
- ref = PyWeakref_GET_OBJECT(ref);
- if (ref != Py_None) {
- PyType_Modified((PyTypeObject *)ref);
- }
- }
- }
- type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
+ /* Invalidate any cached data for the specified type and all
+ subclasses. This function is called after the base
+ classes, mro, or attributes of the type are altered.
+
+ Invariants:
+
+ - Py_TPFLAGS_VALID_VERSION_TAG is never set if
+ Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
+ objects coming from non-recompiled extension modules)
+
+ - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
+ it must first be set on all super types.
+
+ This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
+ type (so it must first clear it on all subclasses). The
+ tp_version_tag value is meaningless unless this flag is set.
+ We don't assign new version tags eagerly, but only as
+ needed.
+ */
+ PyObject *raw, *ref;
+ Py_ssize_t i, n;
+
+ if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
+ return;
+
+ raw = type->tp_subclasses;
+ if (raw != NULL) {
+ n = PyList_GET_SIZE(raw);
+ for (i = 0; i < n; i++) {
+ ref = PyList_GET_ITEM(raw, i);
+ ref = PyWeakref_GET_OBJECT(ref);
+ if (ref != Py_None) {
+ PyType_Modified((PyTypeObject *)ref);
+ }
+ }
+ }
+ type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
}
static void
type_mro_modified(PyTypeObject *type, PyObject *bases) {
- /*
- Check that all base classes or elements of the mro of type are
- able to be cached. This function is called after the base
- classes or mro of the type are altered.
-
- Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
- inherits from an old-style class, either directly or if it
- appears in the MRO of a new-style class. No support either for
- custom MROs that include types that are not officially super
- types.
-
- Called from mro_internal, which will subsequently be called on
- each subclass when their mro is recursively updated.
- */
- Py_ssize_t i, n;
- int clear = 0;
-
- if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
- return;
-
- n = PyTuple_GET_SIZE(bases);
- for (i = 0; i < n; i++) {
- PyObject *b = PyTuple_GET_ITEM(bases, i);
- PyTypeObject *cls;
-
- if (!PyType_Check(b) ) {
- clear = 1;
- break;
- }
-
- cls = (PyTypeObject *)b;
-
- if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
- !PyType_IsSubtype(type, cls)) {
- clear = 1;
- break;
- }
- }
-
- if (clear)
- type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
- Py_TPFLAGS_VALID_VERSION_TAG);
+ /*
+ Check that all base classes or elements of the mro of type are
+ able to be cached. This function is called after the base
+ classes or mro of the type are altered.
+
+ Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
+ inherits from an old-style class, either directly or if it
+ appears in the MRO of a new-style class. No support either for
+ custom MROs that include types that are not officially super
+ types.
+
+ Called from mro_internal, which will subsequently be called on
+ each subclass when their mro is recursively updated.
+ */
+ Py_ssize_t i, n;
+ int clear = 0;
+
+ if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
+ return;
+
+ n = PyTuple_GET_SIZE(bases);
+ for (i = 0; i < n; i++) {
+ PyObject *b = PyTuple_GET_ITEM(bases, i);
+ PyTypeObject *cls;
+
+ if (!PyType_Check(b) ) {
+ clear = 1;
+ break;
+ }
+
+ cls = (PyTypeObject *)b;
+
+ if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
+ !PyType_IsSubtype(type, cls)) {
+ clear = 1;
+ break;
+ }
+ }
+
+ if (clear)
+ type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
+ Py_TPFLAGS_VALID_VERSION_TAG);
}
static int
assign_version_tag(PyTypeObject *type)
{
- /* Ensure that the tp_version_tag is valid and set
- Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
- must first be done on all super classes. Return 0 if this
- cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
- */
- Py_ssize_t i, n;
- PyObject *bases;
-
- if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
- return 1;
- if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
- return 0;
- if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
- return 0;
-
- type->tp_version_tag = next_version_tag++;
- /* for stress-testing: next_version_tag &= 0xFF; */
-
- if (type->tp_version_tag == 0) {
- /* wrap-around or just starting Python - clear the whole
- cache by filling names with references to Py_None.
- Values are also set to NULL for added protection, as they
- are borrowed reference */
- for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
- method_cache[i].value = NULL;
- Py_XDECREF(method_cache[i].name);
- method_cache[i].name = Py_None;
- Py_INCREF(Py_None);
- }
- /* mark all version tags as invalid */
- PyType_Modified(&PyBaseObject_Type);
- return 1;
- }
- bases = type->tp_bases;
- n = PyTuple_GET_SIZE(bases);
- for (i = 0; i < n; i++) {
- PyObject *b = PyTuple_GET_ITEM(bases, i);
- assert(PyType_Check(b));
- if (!assign_version_tag((PyTypeObject *)b))
- return 0;
- }
- type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
- return 1;
+ /* Ensure that the tp_version_tag is valid and set
+ Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
+ must first be done on all super classes. Return 0 if this
+ cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
+ */
+ Py_ssize_t i, n;
+ PyObject *bases;
+
+ if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
+ return 1;
+ if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
+ return 0;
+ if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
+ return 0;
+
+ type->tp_version_tag = next_version_tag++;
+ /* for stress-testing: next_version_tag &= 0xFF; */
+
+ if (type->tp_version_tag == 0) {
+ /* wrap-around or just starting Python - clear the whole
+ cache by filling names with references to Py_None.
+ Values are also set to NULL for added protection, as they
+ are borrowed reference */
+ for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
+ method_cache[i].value = NULL;
+ Py_XDECREF(method_cache[i].name);
+ method_cache[i].name = Py_None;
+ Py_INCREF(Py_None);
+ }
+ /* mark all version tags as invalid */
+ PyType_Modified(&PyBaseObject_Type);
+ return 1;
+ }
+ bases = type->tp_bases;
+ n = PyTuple_GET_SIZE(bases);
+ for (i = 0; i < n; i++) {
+ PyObject *b = PyTuple_GET_ITEM(bases, i);
+ assert(PyType_Check(b));
+ if (!assign_version_tag((PyTypeObject *)b))
+ return 0;
+ }
+ type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
+ return 1;
}
static PyMemberDef type_members[] = {
- {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
- {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
- {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
- {"__weakrefoffset__", T_LONG,
- offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
- {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
- {"__dictoffset__", T_LONG,
- offsetof(PyTypeObject, tp_dictoffset), READONLY},
- {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
- {0}
+ {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
+ {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
+ {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
+ {"__weakrefoffset__", T_LONG,
+ offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
+ {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
+ {"__dictoffset__", T_LONG,
+ offsetof(PyTypeObject, tp_dictoffset), READONLY},
+ {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
+ {0}
};
static PyObject *
type_name(PyTypeObject *type, void *context)
{
- const char *s;
+ const char *s;
- if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
- PyHeapTypeObject* et = (PyHeapTypeObject*)type;
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+ PyHeapTypeObject* et = (PyHeapTypeObject*)type;
- Py_INCREF(et->ht_name);
- return et->ht_name;
- }
- else {
- s = strrchr(type->tp_name, '.');
- if (s == NULL)
- s = type->tp_name;
- else
- s++;
- return PyUnicode_FromString(s);
- }
+ Py_INCREF(et->ht_name);
+ return et->ht_name;
+ }
+ else {
+ s = strrchr(type->tp_name, '.');
+ if (s == NULL)
+ s = type->tp_name;
+ else
+ s++;
+ return PyUnicode_FromString(s);
+ }
}
static int
type_set_name(PyTypeObject *type, PyObject *value, void *context)
{
- PyHeapTypeObject* et;
- char *tp_name;
- PyObject *tmp;
-
- if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
- PyErr_Format(PyExc_TypeError,
- "can't set %s.__name__", type->tp_name);
- return -1;
- }
- if (!value) {
- PyErr_Format(PyExc_TypeError,
- "can't delete %s.__name__", type->tp_name);
- return -1;
- }
- if (!PyUnicode_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "can only assign string to %s.__name__, not '%s'",
- type->tp_name, Py_TYPE(value)->tp_name);
- return -1;
- }
-
- /* Check absence of null characters */
- tmp = PyUnicode_FromStringAndSize("\0", 1);
- if (tmp == NULL)
- return -1;
- if (PyUnicode_Contains(value, tmp) != 0) {
- Py_DECREF(tmp);
- PyErr_Format(PyExc_ValueError,
- "__name__ must not contain null bytes");
- return -1;
- }
- Py_DECREF(tmp);
-
- tp_name = _PyUnicode_AsString(value);
- if (tp_name == NULL)
- return -1;
-
- et = (PyHeapTypeObject*)type;
-
- Py_INCREF(value);
-
- Py_DECREF(et->ht_name);
- et->ht_name = value;
-
- type->tp_name = tp_name;
-
- return 0;
+ PyHeapTypeObject* et;
+ char *tp_name;
+ PyObject *tmp;
+
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
+ PyErr_Format(PyExc_TypeError,
+ "can't set %s.__name__", type->tp_name);
+ return -1;
+ }
+ if (!value) {
+ PyErr_Format(PyExc_TypeError,
+ "can't delete %s.__name__", type->tp_name);
+ return -1;
+ }
+ if (!PyUnicode_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only assign string to %s.__name__, not '%s'",
+ type->tp_name, Py_TYPE(value)->tp_name);
+ return -1;
+ }
+
+ /* Check absence of null characters */
+ tmp = PyUnicode_FromStringAndSize("\0", 1);
+ if (tmp == NULL)
+ return -1;
+ if (PyUnicode_Contains(value, tmp) != 0) {
+ Py_DECREF(tmp);
+ PyErr_Format(PyExc_ValueError,
+ "__name__ must not contain null bytes");
+ return -1;
+ }
+ Py_DECREF(tmp);
+
+ tp_name = _PyUnicode_AsString(value);
+ if (tp_name == NULL)
+ return -1;
+
+ et = (PyHeapTypeObject*)type;
+
+ Py_INCREF(value);
+
+ Py_DECREF(et->ht_name);
+ et->ht_name = value;
+
+ type->tp_name = tp_name;
+
+ return 0;
}
static PyObject *
type_module(PyTypeObject *type, void *context)
{
- PyObject *mod;
- char *s;
-
- if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
- mod = PyDict_GetItemString(type->tp_dict, "__module__");
- if (!mod) {
- PyErr_Format(PyExc_AttributeError, "__module__");
- return 0;
- }
- Py_XINCREF(mod);
- return mod;
- }
- else {
- s = strrchr(type->tp_name, '.');
- if (s != NULL)
- return PyUnicode_FromStringAndSize(
- type->tp_name, (Py_ssize_t)(s - type->tp_name));
- return PyUnicode_FromString("builtins");
- }
+ PyObject *mod;
+ char *s;
+
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
+ mod = PyDict_GetItemString(type->tp_dict, "__module__");
+ if (!mod) {
+ PyErr_Format(PyExc_AttributeError, "__module__");
+ return 0;
+ }
+ Py_XINCREF(mod);
+ return mod;
+ }
+ else {
+ s = strrchr(type->tp_name, '.');
+ if (s != NULL)
+ return PyUnicode_FromStringAndSize(
+ type->tp_name, (Py_ssize_t)(s - type->tp_name));
+ return PyUnicode_FromString("builtins");
+ }
}
static int
type_set_module(PyTypeObject *type, PyObject *value, void *context)
{
- if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
- PyErr_Format(PyExc_TypeError,
- "can't set %s.__module__", type->tp_name);
- return -1;
- }
- if (!value) {
- PyErr_Format(PyExc_TypeError,
- "can't delete %s.__module__", type->tp_name);
- return -1;
- }
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
+ PyErr_Format(PyExc_TypeError,
+ "can't set %s.__module__", type->tp_name);
+ return -1;
+ }
+ if (!value) {
+ PyErr_Format(PyExc_TypeError,
+ "can't delete %s.__module__", type->tp_name);
+ return -1;
+ }
- PyType_Modified(type);
+ PyType_Modified(type);
- return PyDict_SetItemString(type->tp_dict, "__module__", value);
+ return PyDict_SetItemString(type->tp_dict, "__module__", value);
}
static PyObject *
type_abstractmethods(PyTypeObject *type, void *context)
{
- PyObject *mod = PyDict_GetItemString(type->tp_dict,
- "__abstractmethods__");
- if (!mod) {
- PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
- return NULL;
- }
- Py_XINCREF(mod);
- return mod;
+ PyObject *mod = PyDict_GetItemString(type->tp_dict,
+ "__abstractmethods__");
+ if (!mod) {
+ PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
+ return NULL;
+ }
+ Py_XINCREF(mod);
+ return mod;
}
static int
type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
{
- /* __abstractmethods__ should only be set once on a type, in
- abc.ABCMeta.__new__, so this function doesn't do anything
- special to update subclasses.
- */
- int res = PyDict_SetItemString(type->tp_dict,
- "__abstractmethods__", value);
- if (res == 0) {
- PyType_Modified(type);
- if (value && PyObject_IsTrue(value)) {
- type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
- }
- else {
- type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
- }
- }
- return res;
+ /* __abstractmethods__ should only be set once on a type, in
+ abc.ABCMeta.__new__, so this function doesn't do anything
+ special to update subclasses.
+ */
+ int res = PyDict_SetItemString(type->tp_dict,
+ "__abstractmethods__", value);
+ if (res == 0) {
+ PyType_Modified(type);
+ if (value && PyObject_IsTrue(value)) {
+ type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
+ }
+ else {
+ type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
+ }
+ }
+ return res;
}
static PyObject *
type_get_bases(PyTypeObject *type, void *context)
{
- Py_INCREF(type->tp_bases);
- return type->tp_bases;
+ Py_INCREF(type->tp_bases);
+ return type->tp_bases;
}
static PyTypeObject *best_base(PyObject *);
@@ -367,357 +367,357 @@ static void update_all_slots(PyTypeObject *);
typedef int (*update_callback)(PyTypeObject *, void *);
static int update_subclasses(PyTypeObject *type, PyObject *name,
- update_callback callback, void *data);
+ update_callback callback, void *data);
static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
- update_callback callback, void *data);
+ update_callback callback, void *data);
static int
mro_subclasses(PyTypeObject *type, PyObject* temp)
{
- PyTypeObject *subclass;
- PyObject *ref, *subclasses, *old_mro;
- Py_ssize_t i, n;
-
- subclasses = type->tp_subclasses;
- if (subclasses == NULL)
- return 0;
- assert(PyList_Check(subclasses));
- n = PyList_GET_SIZE(subclasses);
- for (i = 0; i < n; i++) {
- ref = PyList_GET_ITEM(subclasses, i);
- assert(PyWeakref_CheckRef(ref));
- subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
- assert(subclass != NULL);
- if ((PyObject *)subclass == Py_None)
- continue;
- assert(PyType_Check(subclass));
- old_mro = subclass->tp_mro;
- if (mro_internal(subclass) < 0) {
- subclass->tp_mro = old_mro;
- return -1;
- }
- else {
- PyObject* tuple;
- tuple = PyTuple_Pack(2, subclass, old_mro);
- Py_DECREF(old_mro);
- if (!tuple)
- return -1;
- if (PyList_Append(temp, tuple) < 0)
- return -1;
- Py_DECREF(tuple);
- }
- if (mro_subclasses(subclass, temp) < 0)
- return -1;
- }
- return 0;
+ PyTypeObject *subclass;
+ PyObject *ref, *subclasses, *old_mro;
+ Py_ssize_t i, n;
+
+ subclasses = type->tp_subclasses;
+ if (subclasses == NULL)
+ return 0;
+ assert(PyList_Check(subclasses));
+ n = PyList_GET_SIZE(subclasses);
+ for (i = 0; i < n; i++) {
+ ref = PyList_GET_ITEM(subclasses, i);
+ assert(PyWeakref_CheckRef(ref));
+ subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
+ assert(subclass != NULL);
+ if ((PyObject *)subclass == Py_None)
+ continue;
+ assert(PyType_Check(subclass));
+ old_mro = subclass->tp_mro;
+ if (mro_internal(subclass) < 0) {
+ subclass->tp_mro = old_mro;
+ return -1;
+ }
+ else {
+ PyObject* tuple;
+ tuple = PyTuple_Pack(2, subclass, old_mro);
+ Py_DECREF(old_mro);
+ if (!tuple)
+ return -1;
+ if (PyList_Append(temp, tuple) < 0)
+ return -1;
+ Py_DECREF(tuple);
+ }
+ if (mro_subclasses(subclass, temp) < 0)
+ return -1;
+ }
+ return 0;
}
static int
type_set_bases(PyTypeObject *type, PyObject *value, void *context)
{
- Py_ssize_t i;
- int r = 0;
- PyObject *ob, *temp;
- PyTypeObject *new_base, *old_base;
- PyObject *old_bases, *old_mro;
-
- if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
- PyErr_Format(PyExc_TypeError,
- "can't set %s.__bases__", type->tp_name);
- return -1;
- }
- if (!value) {
- PyErr_Format(PyExc_TypeError,
- "can't delete %s.__bases__", type->tp_name);
- return -1;
- }
- if (!PyTuple_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "can only assign tuple to %s.__bases__, not %s",
- type->tp_name, Py_TYPE(value)->tp_name);
- return -1;
- }
- if (PyTuple_GET_SIZE(value) == 0) {
- PyErr_Format(PyExc_TypeError,
- "can only assign non-empty tuple to %s.__bases__, not ()",
- type->tp_name);
- return -1;
- }
- for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
- ob = PyTuple_GET_ITEM(value, i);
- if (!PyType_Check(ob)) {
- PyErr_Format(
- PyExc_TypeError,
- "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
- type->tp_name, Py_TYPE(ob)->tp_name);
- return -1;
- }
- if (PyType_Check(ob)) {
- if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
- PyErr_SetString(PyExc_TypeError,
- "a __bases__ item causes an inheritance cycle");
- return -1;
- }
- }
- }
-
- new_base = best_base(value);
-
- if (!new_base) {
- return -1;
- }
-
- if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
- return -1;
-
- Py_INCREF(new_base);
- Py_INCREF(value);
-
- old_bases = type->tp_bases;
- old_base = type->tp_base;
- old_mro = type->tp_mro;
-
- type->tp_bases = value;
- type->tp_base = new_base;
-
- if (mro_internal(type) < 0) {
- goto bail;
- }
-
- temp = PyList_New(0);
- if (!temp)
- goto bail;
-
- r = mro_subclasses(type, temp);
-
- if (r < 0) {
- for (i = 0; i < PyList_Size(temp); i++) {
- PyTypeObject* cls;
- PyObject* mro;
- PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
- "", 2, 2, &cls, &mro);
- Py_INCREF(mro);
- ob = cls->tp_mro;
- cls->tp_mro = mro;
- Py_DECREF(ob);
- }
- Py_DECREF(temp);
- goto bail;
- }
-
- Py_DECREF(temp);
-
- /* any base that was in __bases__ but now isn't, we
- need to remove |type| from its tp_subclasses.
- conversely, any class now in __bases__ that wasn't
- needs to have |type| added to its subclasses. */
-
- /* for now, sod that: just remove from all old_bases,
- add to all new_bases */
-
- for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
- ob = PyTuple_GET_ITEM(old_bases, i);
- if (PyType_Check(ob)) {
- remove_subclass(
- (PyTypeObject*)ob, type);
- }
- }
-
- for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
- ob = PyTuple_GET_ITEM(value, i);
- if (PyType_Check(ob)) {
- if (add_subclass((PyTypeObject*)ob, type) < 0)
- r = -1;
- }
- }
-
- update_all_slots(type);
-
- Py_DECREF(old_bases);
- Py_DECREF(old_base);
- Py_DECREF(old_mro);
-
- return r;
+ Py_ssize_t i;
+ int r = 0;
+ PyObject *ob, *temp;
+ PyTypeObject *new_base, *old_base;
+ PyObject *old_bases, *old_mro;
+
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
+ PyErr_Format(PyExc_TypeError,
+ "can't set %s.__bases__", type->tp_name);
+ return -1;
+ }
+ if (!value) {
+ PyErr_Format(PyExc_TypeError,
+ "can't delete %s.__bases__", type->tp_name);
+ return -1;
+ }
+ if (!PyTuple_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "can only assign tuple to %s.__bases__, not %s",
+ type->tp_name, Py_TYPE(value)->tp_name);
+ return -1;
+ }
+ if (PyTuple_GET_SIZE(value) == 0) {
+ PyErr_Format(PyExc_TypeError,
+ "can only assign non-empty tuple to %s.__bases__, not ()",
+ type->tp_name);
+ return -1;
+ }
+ for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
+ ob = PyTuple_GET_ITEM(value, i);
+ if (!PyType_Check(ob)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
+ type->tp_name, Py_TYPE(ob)->tp_name);
+ return -1;
+ }
+ if (PyType_Check(ob)) {
+ if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
+ PyErr_SetString(PyExc_TypeError,
+ "a __bases__ item causes an inheritance cycle");
+ return -1;
+ }
+ }
+ }
+
+ new_base = best_base(value);
+
+ if (!new_base) {
+ return -1;
+ }
+
+ if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
+ return -1;
+
+ Py_INCREF(new_base);
+ Py_INCREF(value);
+
+ old_bases = type->tp_bases;
+ old_base = type->tp_base;
+ old_mro = type->tp_mro;
+
+ type->tp_bases = value;
+ type->tp_base = new_base;
+
+ if (mro_internal(type) < 0) {
+ goto bail;
+ }
+
+ temp = PyList_New(0);
+ if (!temp)
+ goto bail;
+
+ r = mro_subclasses(type, temp);
+
+ if (r < 0) {
+ for (i = 0; i < PyList_Size(temp); i++) {
+ PyTypeObject* cls;
+ PyObject* mro;
+ PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
+ "", 2, 2, &cls, &mro);
+ Py_INCREF(mro);
+ ob = cls->tp_mro;
+ cls->tp_mro = mro;
+ Py_DECREF(ob);
+ }
+ Py_DECREF(temp);
+ goto bail;
+ }
+
+ Py_DECREF(temp);
+
+ /* any base that was in __bases__ but now isn't, we
+ need to remove |type| from its tp_subclasses.
+ conversely, any class now in __bases__ that wasn't
+ needs to have |type| added to its subclasses. */
+
+ /* for now, sod that: just remove from all old_bases,
+ add to all new_bases */
+
+ for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
+ ob = PyTuple_GET_ITEM(old_bases, i);
+ if (PyType_Check(ob)) {
+ remove_subclass(
+ (PyTypeObject*)ob, type);
+ }
+ }
+
+ for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
+ ob = PyTuple_GET_ITEM(value, i);
+ if (PyType_Check(ob)) {
+ if (add_subclass((PyTypeObject*)ob, type) < 0)
+ r = -1;
+ }
+ }
+
+ update_all_slots(type);
+
+ Py_DECREF(old_bases);
+ Py_DECREF(old_base);
+ Py_DECREF(old_mro);
+
+ return r;
bail:
- Py_DECREF(type->tp_bases);
- Py_DECREF(type->tp_base);
- if (type->tp_mro != old_mro) {
- Py_DECREF(type->tp_mro);
- }
+ Py_DECREF(type->tp_bases);
+ Py_DECREF(type->tp_base);
+ if (type->tp_mro != old_mro) {
+ Py_DECREF(type->tp_mro);
+ }
- type->tp_bases = old_bases;
- type->tp_base = old_base;
- type->tp_mro = old_mro;
+ type->tp_bases = old_bases;
+ type->tp_base = old_base;
+ type->tp_mro = old_mro;
- return -1;
+ return -1;
}
static PyObject *
type_dict(PyTypeObject *type, void *context)
{
- if (type->tp_dict == NULL) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- return PyDictProxy_New(type->tp_dict);
+ if (type->tp_dict == NULL) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ return PyDictProxy_New(type->tp_dict);
}
static PyObject *
type_get_doc(PyTypeObject *type, void *context)
{
- PyObject *result;
- if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
- return PyUnicode_FromString(type->tp_doc);
- result = PyDict_GetItemString(type->tp_dict, "__doc__");
- if (result == NULL) {
- result = Py_None;
- Py_INCREF(result);
- }
- else if (Py_TYPE(result)->tp_descr_get) {
- result = Py_TYPE(result)->tp_descr_get(result, NULL,
- (PyObject *)type);
- }
- else {
- Py_INCREF(result);
- }
- return result;
+ PyObject *result;
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
+ return PyUnicode_FromString(type->tp_doc);
+ result = PyDict_GetItemString(type->tp_dict, "__doc__");
+ if (result == NULL) {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+ else if (Py_TYPE(result)->tp_descr_get) {
+ result = Py_TYPE(result)->tp_descr_get(result, NULL,
+ (PyObject *)type);
+ }
+ else {
+ Py_INCREF(result);
+ }
+ return result;
}
static PyObject *
type___instancecheck__(PyObject *type, PyObject *inst)
{
- switch (_PyObject_RealIsInstance(inst, type)) {
- case -1:
- return NULL;
- case 0:
- Py_RETURN_FALSE;
- default:
- Py_RETURN_TRUE;
- }
+ switch (_PyObject_RealIsInstance(inst, type)) {
+ case -1:
+ return NULL;
+ case 0:
+ Py_RETURN_FALSE;
+ default:
+ Py_RETURN_TRUE;
+ }
}
static PyObject *
type___subclasscheck__(PyObject *type, PyObject *inst)
{
- switch (_PyObject_RealIsSubclass(inst, type)) {
- case -1:
- return NULL;
- case 0:
- Py_RETURN_FALSE;
- default:
- Py_RETURN_TRUE;
- }
+ switch (_PyObject_RealIsSubclass(inst, type)) {
+ case -1:
+ return NULL;
+ case 0:
+ Py_RETURN_FALSE;
+ default:
+ Py_RETURN_TRUE;
+ }
}
static PyGetSetDef type_getsets[] = {
- {"__name__", (getter)type_name, (setter)type_set_name, NULL},
- {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
- {"__module__", (getter)type_module, (setter)type_set_module, NULL},
- {"__abstractmethods__", (getter)type_abstractmethods,
- (setter)type_set_abstractmethods, NULL},
- {"__dict__", (getter)type_dict, NULL, NULL},
- {"__doc__", (getter)type_get_doc, NULL, NULL},
- {0}
+ {"__name__", (getter)type_name, (setter)type_set_name, NULL},
+ {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
+ {"__module__", (getter)type_module, (setter)type_set_module, NULL},
+ {"__abstractmethods__", (getter)type_abstractmethods,
+ (setter)type_set_abstractmethods, NULL},
+ {"__dict__", (getter)type_dict, NULL, NULL},
+ {"__doc__", (getter)type_get_doc, NULL, NULL},
+ {0}
};
static PyObject *
type_repr(PyTypeObject *type)
{
- PyObject *mod, *name, *rtn;
+ PyObject *mod, *name, *rtn;
- mod = type_module(type, NULL);
- if (mod == NULL)
- PyErr_Clear();
- else if (!PyUnicode_Check(mod)) {
- Py_DECREF(mod);
- mod = NULL;
- }
- name = type_name(type, NULL);
- if (name == NULL)
- return NULL;
+ mod = type_module(type, NULL);
+ if (mod == NULL)
+ PyErr_Clear();
+ else if (!PyUnicode_Check(mod)) {
+ Py_DECREF(mod);
+ mod = NULL;
+ }
+ name = type_name(type, NULL);
+ if (name == NULL)
+ return NULL;
- if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
- rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
- else
- rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
+ if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
+ rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
+ else
+ rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
- Py_XDECREF(mod);
- Py_DECREF(name);
- return rtn;
+ Py_XDECREF(mod);
+ Py_DECREF(name);
+ return rtn;
}
static PyObject *
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *obj;
-
- if (type->tp_new == NULL) {
- PyErr_Format(PyExc_TypeError,
- "cannot create '%.100s' instances",
- type->tp_name);
- return NULL;
- }
-
- obj = type->tp_new(type, args, kwds);
- if (obj != NULL) {
- /* Ugly exception: when the call was type(something),
- don't call tp_init on the result. */
- if (type == &PyType_Type &&
- PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
- (kwds == NULL ||
- (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
- return obj;
- /* If the returned object is not an instance of type,
- it won't be initialized. */
- if (!PyType_IsSubtype(Py_TYPE(obj), type))
- return obj;
- type = Py_TYPE(obj);
- if (type->tp_init != NULL &&
- type->tp_init(obj, args, kwds) < 0) {
- Py_DECREF(obj);
- obj = NULL;
- }
- }
- return obj;
+ PyObject *obj;
+
+ if (type->tp_new == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "cannot create '%.100s' instances",
+ type->tp_name);
+ return NULL;
+ }
+
+ obj = type->tp_new(type, args, kwds);
+ if (obj != NULL) {
+ /* Ugly exception: when the call was type(something),
+ don't call tp_init on the result. */
+ if (type == &PyType_Type &&
+ PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
+ (kwds == NULL ||
+ (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
+ return obj;
+ /* If the returned object is not an instance of type,
+ it won't be initialized. */
+ if (!PyType_IsSubtype(Py_TYPE(obj), type))
+ return obj;
+ type = Py_TYPE(obj);
+ if (type->tp_init != NULL &&
+ type->tp_init(obj, args, kwds) < 0) {
+ Py_DECREF(obj);
+ obj = NULL;
+ }
+ }
+ return obj;
}
PyObject *
PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
{
- PyObject *obj;
- const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
- /* note that we need to add one, for the sentinel */
+ PyObject *obj;
+ const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
+ /* note that we need to add one, for the sentinel */
- if (PyType_IS_GC(type))
- obj = _PyObject_GC_Malloc(size);
- else
- obj = (PyObject *)PyObject_MALLOC(size);
+ if (PyType_IS_GC(type))
+ obj = _PyObject_GC_Malloc(size);
+ else
+ obj = (PyObject *)PyObject_MALLOC(size);
- if (obj == NULL)
- return PyErr_NoMemory();
+ if (obj == NULL)
+ return PyErr_NoMemory();
- memset(obj, '\0', size);
+ memset(obj, '\0', size);
- if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
- Py_INCREF(type);
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ Py_INCREF(type);
- if (type->tp_itemsize == 0)
- PyObject_INIT(obj, type);
- else
- (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
+ if (type->tp_itemsize == 0)
+ PyObject_INIT(obj, type);
+ else
+ (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
- if (PyType_IS_GC(type))
- _PyObject_GC_TRACK(obj);
- return obj;
+ if (PyType_IS_GC(type))
+ _PyObject_GC_TRACK(obj);
+ return obj;
}
PyObject *
PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- return type->tp_alloc(type, 0);
+ return type->tp_alloc(type, 0);
}
/* Helpers for subtyping */
@@ -725,341 +725,341 @@ PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
{
- Py_ssize_t i, n;
- PyMemberDef *mp;
-
- n = Py_SIZE(type);
- mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
- for (i = 0; i < n; i++, mp++) {
- if (mp->type == T_OBJECT_EX) {
- char *addr = (char *)self + mp->offset;
- PyObject *obj = *(PyObject **)addr;
- if (obj != NULL) {
- int err = visit(obj, arg);
- if (err)
- return err;
- }
- }
- }
- return 0;
+ Py_ssize_t i, n;
+ PyMemberDef *mp;
+
+ n = Py_SIZE(type);
+ mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
+ for (i = 0; i < n; i++, mp++) {
+ if (mp->type == T_OBJECT_EX) {
+ char *addr = (char *)self + mp->offset;
+ PyObject *obj = *(PyObject **)addr;
+ if (obj != NULL) {
+ int err = visit(obj, arg);
+ if (err)
+ return err;
+ }
+ }
+ }
+ return 0;
}
static int
subtype_traverse(PyObject *self, visitproc visit, void *arg)
{
- PyTypeObject *type, *base;
- traverseproc basetraverse;
-
- /* Find the nearest base with a different tp_traverse,
- and traverse slots while we're at it */
- type = Py_TYPE(self);
- base = type;
- while ((basetraverse = base->tp_traverse) == subtype_traverse) {
- if (Py_SIZE(base)) {
- int err = traverse_slots(base, self, visit, arg);
- if (err)
- return err;
- }
- base = base->tp_base;
- assert(base);
- }
-
- if (type->tp_dictoffset != base->tp_dictoffset) {
- PyObject **dictptr = _PyObject_GetDictPtr(self);
- if (dictptr && *dictptr)
- Py_VISIT(*dictptr);
- }
-
- if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
- /* For a heaptype, the instances count as references
- to the type. Traverse the type so the collector
- can find cycles involving this link. */
- Py_VISIT(type);
-
- if (basetraverse)
- return basetraverse(self, visit, arg);
- return 0;
+ PyTypeObject *type, *base;
+ traverseproc basetraverse;
+
+ /* Find the nearest base with a different tp_traverse,
+ and traverse slots while we're at it */
+ type = Py_TYPE(self);
+ base = type;
+ while ((basetraverse = base->tp_traverse) == subtype_traverse) {
+ if (Py_SIZE(base)) {
+ int err = traverse_slots(base, self, visit, arg);
+ if (err)
+ return err;
+ }
+ base = base->tp_base;
+ assert(base);
+ }
+
+ if (type->tp_dictoffset != base->tp_dictoffset) {
+ PyObject **dictptr = _PyObject_GetDictPtr(self);
+ if (dictptr && *dictptr)
+ Py_VISIT(*dictptr);
+ }
+
+ if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ /* For a heaptype, the instances count as references
+ to the type. Traverse the type so the collector
+ can find cycles involving this link. */
+ Py_VISIT(type);
+
+ if (basetraverse)
+ return basetraverse(self, visit, arg);
+ return 0;
}
static void
clear_slots(PyTypeObject *type, PyObject *self)
{
- Py_ssize_t i, n;
- PyMemberDef *mp;
-
- n = Py_SIZE(type);
- mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
- for (i = 0; i < n; i++, mp++) {
- if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
- char *addr = (char *)self + mp->offset;
- PyObject *obj = *(PyObject **)addr;
- if (obj != NULL) {
- *(PyObject **)addr = NULL;
- Py_DECREF(obj);
- }
- }
- }
+ Py_ssize_t i, n;
+ PyMemberDef *mp;
+
+ n = Py_SIZE(type);
+ mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
+ for (i = 0; i < n; i++, mp++) {
+ if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
+ char *addr = (char *)self + mp->offset;
+ PyObject *obj = *(PyObject **)addr;
+ if (obj != NULL) {
+ *(PyObject **)addr = NULL;
+ Py_DECREF(obj);
+ }
+ }
+ }
}
static int
subtype_clear(PyObject *self)
{
- PyTypeObject *type, *base;
- inquiry baseclear;
+ PyTypeObject *type, *base;
+ inquiry baseclear;
- /* Find the nearest base with a different tp_clear
- and clear slots while we're at it */
- type = Py_TYPE(self);
- base = type;
- while ((baseclear = base->tp_clear) == subtype_clear) {
- if (Py_SIZE(base))
- clear_slots(base, self);
- base = base->tp_base;
- assert(base);
- }
+ /* Find the nearest base with a different tp_clear
+ and clear slots while we're at it */
+ type = Py_TYPE(self);
+ base = type;
+ while ((baseclear = base->tp_clear) == subtype_clear) {
+ if (Py_SIZE(base))
+ clear_slots(base, self);
+ base = base->tp_base;
+ assert(base);
+ }
- /* There's no need to clear the instance dict (if any);
- the collector will call its tp_clear handler. */
+ /* There's no need to clear the instance dict (if any);
+ the collector will call its tp_clear handler. */
- if (baseclear)
- return baseclear(self);
- return 0;
+ if (baseclear)
+ return baseclear(self);
+ return 0;
}
static void
subtype_dealloc(PyObject *self)
{
- PyTypeObject *type, *base;
- destructor basedealloc;
-
- /* Extract the type; we expect it to be a heap type */
- type = Py_TYPE(self);
- assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
-
- /* Test whether the type has GC exactly once */
-
- if (!PyType_IS_GC(type)) {
- /* It's really rare to find a dynamic type that doesn't have
- GC; it can only happen when deriving from 'object' and not
- adding any slots or instance variables. This allows
- certain simplifications: there's no need to call
- clear_slots(), or DECREF the dict, or clear weakrefs. */
-
- /* Maybe call finalizer; exit early if resurrected */
- if (type->tp_del) {
- type->tp_del(self);
- if (self->ob_refcnt > 0)
- return;
- }
-
- /* Find the nearest base with a different tp_dealloc */
- base = type;
- while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
- assert(Py_SIZE(base) == 0);
- base = base->tp_base;
- assert(base);
- }
-
- /* Extract the type again; tp_del may have changed it */
- type = Py_TYPE(self);
-
- /* Call the base tp_dealloc() */
- assert(basedealloc);
- basedealloc(self);
-
- /* Can't reference self beyond this point */
- Py_DECREF(type);
-
- /* Done */
- return;
- }
-
- /* We get here only if the type has GC */
-
- /* UnTrack and re-Track around the trashcan macro, alas */
- /* See explanation at end of function for full disclosure */
- PyObject_GC_UnTrack(self);
- ++_PyTrash_delete_nesting;
- Py_TRASHCAN_SAFE_BEGIN(self);
- --_PyTrash_delete_nesting;
- /* DO NOT restore GC tracking at this point. weakref callbacks
- * (if any, and whether directly here or indirectly in something we
- * call) may trigger GC, and if self is tracked at that point, it
- * will look like trash to GC and GC will try to delete self again.
- */
-
- /* Find the nearest base with a different tp_dealloc */
- base = type;
- while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
- base = base->tp_base;
- assert(base);
- }
-
- /* If we added a weaklist, we clear it. Do this *before* calling
- the finalizer (__del__), clearing slots, or clearing the instance
- dict. */
-
- if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
- PyObject_ClearWeakRefs(self);
-
- /* Maybe call finalizer; exit early if resurrected */
- if (type->tp_del) {
- _PyObject_GC_TRACK(self);
- type->tp_del(self);
- if (self->ob_refcnt > 0)
- goto endlabel; /* resurrected */
- else
- _PyObject_GC_UNTRACK(self);
- /* New weakrefs could be created during the finalizer call.
- If this occurs, clear them out without calling their
- finalizers since they might rely on part of the object
- being finalized that has already been destroyed. */
- if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
- /* Modeled after GET_WEAKREFS_LISTPTR() */
- PyWeakReference **list = (PyWeakReference **) \
- PyObject_GET_WEAKREFS_LISTPTR(self);
- while (*list)
- _PyWeakref_ClearRef(*list);
- }
- }
-
- /* Clear slots up to the nearest base with a different tp_dealloc */
- base = type;
- while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
- if (Py_SIZE(base))
- clear_slots(base, self);
- base = base->tp_base;
- assert(base);
- }
-
- /* If we added a dict, DECREF it */
- if (type->tp_dictoffset && !base->tp_dictoffset) {
- PyObject **dictptr = _PyObject_GetDictPtr(self);
- if (dictptr != NULL) {
- PyObject *dict = *dictptr;
- if (dict != NULL) {
- Py_DECREF(dict);
- *dictptr = NULL;
- }
- }
- }
-
- /* Extract the type again; tp_del may have changed it */
- type = Py_TYPE(self);
-
- /* Call the base tp_dealloc(); first retrack self if
- * basedealloc knows about gc.
- */
- if (PyType_IS_GC(base))
- _PyObject_GC_TRACK(self);
- assert(basedealloc);
- basedealloc(self);
-
- /* Can't reference self beyond this point */
- Py_DECREF(type);
+ PyTypeObject *type, *base;
+ destructor basedealloc;
+
+ /* Extract the type; we expect it to be a heap type */
+ type = Py_TYPE(self);
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
+
+ /* Test whether the type has GC exactly once */
+
+ if (!PyType_IS_GC(type)) {
+ /* It's really rare to find a dynamic type that doesn't have
+ GC; it can only happen when deriving from 'object' and not
+ adding any slots or instance variables. This allows
+ certain simplifications: there's no need to call
+ clear_slots(), or DECREF the dict, or clear weakrefs. */
+
+ /* Maybe call finalizer; exit early if resurrected */
+ if (type->tp_del) {
+ type->tp_del(self);
+ if (self->ob_refcnt > 0)
+ return;
+ }
+
+ /* Find the nearest base with a different tp_dealloc */
+ base = type;
+ while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
+ assert(Py_SIZE(base) == 0);
+ base = base->tp_base;
+ assert(base);
+ }
+
+ /* Extract the type again; tp_del may have changed it */
+ type = Py_TYPE(self);
+
+ /* Call the base tp_dealloc() */
+ assert(basedealloc);
+ basedealloc(self);
+
+ /* Can't reference self beyond this point */
+ Py_DECREF(type);
+
+ /* Done */
+ return;
+ }
+
+ /* We get here only if the type has GC */
+
+ /* UnTrack and re-Track around the trashcan macro, alas */
+ /* See explanation at end of function for full disclosure */
+ PyObject_GC_UnTrack(self);
+ ++_PyTrash_delete_nesting;
+ Py_TRASHCAN_SAFE_BEGIN(self);
+ --_PyTrash_delete_nesting;
+ /* DO NOT restore GC tracking at this point. weakref callbacks
+ * (if any, and whether directly here or indirectly in something we
+ * call) may trigger GC, and if self is tracked at that point, it
+ * will look like trash to GC and GC will try to delete self again.
+ */
+
+ /* Find the nearest base with a different tp_dealloc */
+ base = type;
+ while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
+ base = base->tp_base;
+ assert(base);
+ }
+
+ /* If we added a weaklist, we clear it. Do this *before* calling
+ the finalizer (__del__), clearing slots, or clearing the instance
+ dict. */
+
+ if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
+ PyObject_ClearWeakRefs(self);
+
+ /* Maybe call finalizer; exit early if resurrected */
+ if (type->tp_del) {
+ _PyObject_GC_TRACK(self);
+ type->tp_del(self);
+ if (self->ob_refcnt > 0)
+ goto endlabel; /* resurrected */
+ else
+ _PyObject_GC_UNTRACK(self);
+ /* New weakrefs could be created during the finalizer call.
+ If this occurs, clear them out without calling their
+ finalizers since they might rely on part of the object
+ being finalized that has already been destroyed. */
+ if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
+ /* Modeled after GET_WEAKREFS_LISTPTR() */
+ PyWeakReference **list = (PyWeakReference **) \
+ PyObject_GET_WEAKREFS_LISTPTR(self);
+ while (*list)
+ _PyWeakref_ClearRef(*list);
+ }
+ }
+
+ /* Clear slots up to the nearest base with a different tp_dealloc */
+ base = type;
+ while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
+ if (Py_SIZE(base))
+ clear_slots(base, self);
+ base = base->tp_base;
+ assert(base);
+ }
+
+ /* If we added a dict, DECREF it */
+ if (type->tp_dictoffset && !base->tp_dictoffset) {
+ PyObject **dictptr = _PyObject_GetDictPtr(self);
+ if (dictptr != NULL) {
+ PyObject *dict = *dictptr;
+ if (dict != NULL) {
+ Py_DECREF(dict);
+ *dictptr = NULL;
+ }
+ }
+ }
+
+ /* Extract the type again; tp_del may have changed it */
+ type = Py_TYPE(self);
+
+ /* Call the base tp_dealloc(); first retrack self if
+ * basedealloc knows about gc.
+ */
+ if (PyType_IS_GC(base))
+ _PyObject_GC_TRACK(self);
+ assert(basedealloc);
+ basedealloc(self);
+
+ /* Can't reference self beyond this point */
+ Py_DECREF(type);
endlabel:
- ++_PyTrash_delete_nesting;
- Py_TRASHCAN_SAFE_END(self);
- --_PyTrash_delete_nesting;
+ ++_PyTrash_delete_nesting;
+ Py_TRASHCAN_SAFE_END(self);
+ --_PyTrash_delete_nesting;
- /* Explanation of the weirdness around the trashcan macros:
+ /* Explanation of the weirdness around the trashcan macros:
- Q. What do the trashcan macros do?
+ Q. What do the trashcan macros do?
- A. Read the comment titled "Trashcan mechanism" in object.h.
- For one, this explains why there must be a call to GC-untrack
- before the trashcan begin macro. Without understanding the
- trashcan code, the answers to the following questions don't make
- sense.
+ A. Read the comment titled "Trashcan mechanism" in object.h.
+ For one, this explains why there must be a call to GC-untrack
+ before the trashcan begin macro. Without understanding the
+ trashcan code, the answers to the following questions don't make
+ sense.
- Q. Why do we GC-untrack before the trashcan and then immediately
- GC-track again afterward?
+ Q. Why do we GC-untrack before the trashcan and then immediately
+ GC-track again afterward?
- A. In the case that the base class is GC-aware, the base class
- probably GC-untracks the object. If it does that using the
- UNTRACK macro, this will crash when the object is already
- untracked. Because we don't know what the base class does, the
- only safe thing is to make sure the object is tracked when we
- call the base class dealloc. But... The trashcan begin macro
- requires that the object is *untracked* before it is called. So
- the dance becomes:
+ A. In the case that the base class is GC-aware, the base class
+ probably GC-untracks the object. If it does that using the
+ UNTRACK macro, this will crash when the object is already
+ untracked. Because we don't know what the base class does, the
+ only safe thing is to make sure the object is tracked when we
+ call the base class dealloc. But... The trashcan begin macro
+ requires that the object is *untracked* before it is called. So
+ the dance becomes:
- GC untrack
- trashcan begin
- GC track
+ GC untrack
+ trashcan begin
+ GC track
- Q. Why did the last question say "immediately GC-track again"?
- It's nowhere near immediately.
+ Q. Why did the last question say "immediately GC-track again"?
+ It's nowhere near immediately.
- A. Because the code *used* to re-track immediately. Bad Idea.
- self has a refcount of 0, and if gc ever gets its hands on it
- (which can happen if any weakref callback gets invoked), it
- looks like trash to gc too, and gc also tries to delete self
- then. But we're already deleting self. Double dealloction is
- a subtle disaster.
+ A. Because the code *used* to re-track immediately. Bad Idea.
+ self has a refcount of 0, and if gc ever gets its hands on it
+ (which can happen if any weakref callback gets invoked), it
+ looks like trash to gc too, and gc also tries to delete self
+ then. But we're already deleting self. Double dealloction is
+ a subtle disaster.
- Q. Why the bizarre (net-zero) manipulation of
- _PyTrash_delete_nesting around the trashcan macros?
+ Q. Why the bizarre (net-zero) manipulation of
+ _PyTrash_delete_nesting around the trashcan macros?
- A. Some base classes (e.g. list) also use the trashcan mechanism.
- The following scenario used to be possible:
+ A. Some base classes (e.g. list) also use the trashcan mechanism.
+ The following scenario used to be possible:
- - suppose the trashcan level is one below the trashcan limit
+ - suppose the trashcan level is one below the trashcan limit
- - subtype_dealloc() is called
+ - subtype_dealloc() is called
- - the trashcan limit is not yet reached, so the trashcan level
- is incremented and the code between trashcan begin and end is
- executed
+ - the trashcan limit is not yet reached, so the trashcan level
+ is incremented and the code between trashcan begin and end is
+ executed
- - this destroys much of the object's contents, including its
- slots and __dict__
+ - this destroys much of the object's contents, including its
+ slots and __dict__
- - basedealloc() is called; this is really list_dealloc(), or
- some other type which also uses the trashcan macros
+ - basedealloc() is called; this is really list_dealloc(), or
+ some other type which also uses the trashcan macros
- - the trashcan limit is now reached, so the object is put on the
- trashcan's to-be-deleted-later list
+ - the trashcan limit is now reached, so the object is put on the
+ trashcan's to-be-deleted-later list
- - basedealloc() returns
+ - basedealloc() returns
- - subtype_dealloc() decrefs the object's type
+ - subtype_dealloc() decrefs the object's type
- - subtype_dealloc() returns
+ - subtype_dealloc() returns
- - later, the trashcan code starts deleting the objects from its
- to-be-deleted-later list
+ - later, the trashcan code starts deleting the objects from its
+ to-be-deleted-later list
- - subtype_dealloc() is called *AGAIN* for the same object
+ - subtype_dealloc() is called *AGAIN* for the same object
- - at the very least (if the destroyed slots and __dict__ don't
- cause problems) the object's type gets decref'ed a second
- time, which is *BAD*!!!
+ - at the very least (if the destroyed slots and __dict__ don't
+ cause problems) the object's type gets decref'ed a second
+ time, which is *BAD*!!!
- The remedy is to make sure that if the code between trashcan
- begin and end in subtype_dealloc() is called, the code between
- trashcan begin and end in basedealloc() will also be called.
- This is done by decrementing the level after passing into the
- trashcan block, and incrementing it just before leaving the
- block.
+ The remedy is to make sure that if the code between trashcan
+ begin and end in subtype_dealloc() is called, the code between
+ trashcan begin and end in basedealloc() will also be called.
+ This is done by decrementing the level after passing into the
+ trashcan block, and incrementing it just before leaving the
+ block.
- But now it's possible that a chain of objects consisting solely
- of objects whose deallocator is subtype_dealloc() will defeat
- the trashcan mechanism completely: the decremented level means
- that the effective level never reaches the limit. Therefore, we
- *increment* the level *before* entering the trashcan block, and
- matchingly decrement it after leaving. This means the trashcan
- code will trigger a little early, but that's no big deal.
+ But now it's possible that a chain of objects consisting solely
+ of objects whose deallocator is subtype_dealloc() will defeat
+ the trashcan mechanism completely: the decremented level means
+ that the effective level never reaches the limit. Therefore, we
+ *increment* the level *before* entering the trashcan block, and
+ matchingly decrement it after leaving. This means the trashcan
+ code will trigger a little early, but that's no big deal.
- Q. Are there any live examples of code in need of all this
- complexity?
+ Q. Are there any live examples of code in need of all this
+ complexity?
- A. Yes. See SF bug 668433 for code that crashed (when Python was
- compiled in debug mode) before the trashcan level manipulations
- were added. For more discussion, see SF patches 581742, 575073
- and bug 574207.
- */
+ A. Yes. See SF bug 668433 for code that crashed (when Python was
+ compiled in debug mode) before the trashcan level manipulations
+ were added. For more discussion, see SF patches 581742, 575073
+ and bug 574207.
+ */
}
static PyTypeObject *solid_base(PyTypeObject *type);
@@ -1069,36 +1069,36 @@ static PyTypeObject *solid_base(PyTypeObject *type);
int
PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
{
- PyObject *mro;
-
- mro = a->tp_mro;
- if (mro != NULL) {
- /* Deal with multiple inheritance without recursion
- by walking the MRO tuple */
- Py_ssize_t i, n;
- assert(PyTuple_Check(mro));
- n = PyTuple_GET_SIZE(mro);
- for (i = 0; i < n; i++) {
- if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
- return 1;
- }
- return 0;
- }
- else {
- /* a is not completely initilized yet; follow tp_base */
- do {
- if (a == b)
- return 1;
- a = a->tp_base;
- } while (a != NULL);
- return b == &PyBaseObject_Type;
- }
+ PyObject *mro;
+
+ mro = a->tp_mro;
+ if (mro != NULL) {
+ /* Deal with multiple inheritance without recursion
+ by walking the MRO tuple */
+ Py_ssize_t i, n;
+ assert(PyTuple_Check(mro));
+ n = PyTuple_GET_SIZE(mro);
+ for (i = 0; i < n; i++) {
+ if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
+ return 1;
+ }
+ return 0;
+ }
+ else {
+ /* a is not completely initilized yet; follow tp_base */
+ do {
+ if (a == b)
+ return 1;
+ a = a->tp_base;
+ } while (a != NULL);
+ return b == &PyBaseObject_Type;
+ }
}
/* Internal routines to do a method lookup in the type
without looking in the instance dictionary
(so we can't use PyObject_GetAttr) but still binding
- it to the instance. The arguments are the object,
+ it to the instance. The arguments are the object,
the method name as a C string, and the address of a
static variable used to cache the interned Python string.
@@ -1115,75 +1115,75 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
static PyObject *
lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
{
- PyObject *res;
+ PyObject *res;
- if (*attrobj == NULL) {
- *attrobj = PyUnicode_InternFromString(attrstr);
- if (*attrobj == NULL)
- return NULL;
- }
- res = _PyType_Lookup(Py_TYPE(self), *attrobj);
- if (res != NULL) {
- descrgetfunc f;
- if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
- Py_INCREF(res);
- else
- res = f(res, self, (PyObject *)(Py_TYPE(self)));
- }
- return res;
+ if (*attrobj == NULL) {
+ *attrobj = PyUnicode_InternFromString(attrstr);
+ if (*attrobj == NULL)
+ return NULL;
+ }
+ res = _PyType_Lookup(Py_TYPE(self), *attrobj);
+ if (res != NULL) {
+ descrgetfunc f;
+ if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
+ Py_INCREF(res);
+ else
+ res = f(res, self, (PyObject *)(Py_TYPE(self)));
+ }
+ return res;
}
static PyObject *
lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
{
- PyObject *res = lookup_maybe(self, attrstr, attrobj);
- if (res == NULL && !PyErr_Occurred())
- PyErr_SetObject(PyExc_AttributeError, *attrobj);
- return res;
+ PyObject *res = lookup_maybe(self, attrstr, attrobj);
+ if (res == NULL && !PyErr_Occurred())
+ PyErr_SetObject(PyExc_AttributeError, *attrobj);
+ return res;
}
PyObject *
_PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
{
- return lookup_maybe(self, attrstr, attrobj);
+ return lookup_maybe(self, attrstr, attrobj);
}
/* A variation of PyObject_CallMethod that uses lookup_method()
- instead of PyObject_GetAttrString(). This uses the same convention
+ instead of PyObject_GetAttrString(). This uses the same convention
as lookup_method to cache the interned name string object. */
static PyObject *
call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
{
- va_list va;
- PyObject *args, *func = 0, *retval;
- va_start(va, format);
+ va_list va;
+ PyObject *args, *func = 0, *retval;
+ va_start(va, format);
- func = lookup_maybe(o, name, nameobj);
- if (func == NULL) {
- va_end(va);
- if (!PyErr_Occurred())
- PyErr_SetObject(PyExc_AttributeError, *nameobj);
- return NULL;
- }
+ func = lookup_maybe(o, name, nameobj);
+ if (func == NULL) {
+ va_end(va);
+ if (!PyErr_Occurred())
+ PyErr_SetObject(PyExc_AttributeError, *nameobj);
+ return NULL;
+ }
- if (format && *format)
- args = Py_VaBuildValue(format, va);
- else
- args = PyTuple_New(0);
+ if (format && *format)
+ args = Py_VaBuildValue(format, va);
+ else
+ args = PyTuple_New(0);
- va_end(va);
+ va_end(va);
- if (args == NULL)
- return NULL;
+ if (args == NULL)
+ return NULL;
- assert(PyTuple_Check(args));
- retval = PyObject_Call(func, args, NULL);
+ assert(PyTuple_Check(args));
+ retval = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- Py_DECREF(func);
+ Py_DECREF(args);
+ Py_DECREF(func);
- return retval;
+ return retval;
}
/* Clone of call_method() that returns NotImplemented when the lookup fails. */
@@ -1191,37 +1191,37 @@ call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
static PyObject *
call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
{
- va_list va;
- PyObject *args, *func = 0, *retval;
- va_start(va, format);
+ va_list va;
+ PyObject *args, *func = 0, *retval;
+ va_start(va, format);
- func = lookup_maybe(o, name, nameobj);
- if (func == NULL) {
- va_end(va);
- if (!PyErr_Occurred()) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- return NULL;
- }
+ func = lookup_maybe(o, name, nameobj);
+ if (func == NULL) {
+ va_end(va);
+ if (!PyErr_Occurred()) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ return NULL;
+ }
- if (format && *format)
- args = Py_VaBuildValue(format, va);
- else
- args = PyTuple_New(0);
+ if (format && *format)
+ args = Py_VaBuildValue(format, va);
+ else
+ args = PyTuple_New(0);
- va_end(va);
+ va_end(va);
- if (args == NULL)
- return NULL;
+ if (args == NULL)
+ return NULL;
- assert(PyTuple_Check(args));
- retval = PyObject_Call(func, args, NULL);
+ assert(PyTuple_Check(args));
+ retval = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- Py_DECREF(func);
+ Py_DECREF(args);
+ Py_DECREF(func);
- return retval;
+ return retval;
}
/*
@@ -1254,68 +1254,68 @@ call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
static int
tail_contains(PyObject *list, int whence, PyObject *o) {
- Py_ssize_t j, size;
- size = PyList_GET_SIZE(list);
+ Py_ssize_t j, size;
+ size = PyList_GET_SIZE(list);
- for (j = whence+1; j < size; j++) {
- if (PyList_GET_ITEM(list, j) == o)
- return 1;
- }
- return 0;
+ for (j = whence+1; j < size; j++) {
+ if (PyList_GET_ITEM(list, j) == o)
+ return 1;
+ }
+ return 0;
}
static PyObject *
class_name(PyObject *cls)
{
- PyObject *name = PyObject_GetAttrString(cls, "__name__");
- if (name == NULL) {
- PyErr_Clear();
- Py_XDECREF(name);
- name = PyObject_Repr(cls);
- }
- if (name == NULL)
- return NULL;
- if (!PyUnicode_Check(name)) {
- Py_DECREF(name);
- return NULL;
- }
- return name;
+ PyObject *name = PyObject_GetAttrString(cls, "__name__");
+ if (name == NULL) {
+ PyErr_Clear();
+ Py_XDECREF(name);
+ name = PyObject_Repr(cls);
+ }
+ if (name == NULL)
+ return NULL;
+ if (!PyUnicode_Check(name)) {
+ Py_DECREF(name);
+ return NULL;
+ }
+ return name;
}
static int
check_duplicates(PyObject *list)
{
- Py_ssize_t i, j, n;
- /* Let's use a quadratic time algorithm,
- assuming that the bases lists is short.
- */
- n = PyList_GET_SIZE(list);
- for (i = 0; i < n; i++) {
- PyObject *o = PyList_GET_ITEM(list, i);
- for (j = i + 1; j < n; j++) {
- if (PyList_GET_ITEM(list, j) == o) {
- o = class_name(o);
- if (o != NULL) {
- PyErr_Format(PyExc_TypeError,
- "duplicate base class %U",
- o);
- Py_DECREF(o);
- } else {
- PyErr_SetString(PyExc_TypeError,
- "duplicate base class");
- }
- return -1;
- }
- }
- }
- return 0;
+ Py_ssize_t i, j, n;
+ /* Let's use a quadratic time algorithm,
+ assuming that the bases lists is short.
+ */
+ n = PyList_GET_SIZE(list);
+ for (i = 0; i < n; i++) {
+ PyObject *o = PyList_GET_ITEM(list, i);
+ for (j = i + 1; j < n; j++) {
+ if (PyList_GET_ITEM(list, j) == o) {
+ o = class_name(o);
+ if (o != NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "duplicate base class %U",
+ o);
+ Py_DECREF(o);
+ } else {
+ PyErr_SetString(PyExc_TypeError,
+ "duplicate base class");
+ }
+ return -1;
+ }
+ }
+ }
+ return 0;
}
/* Raise a TypeError for an MRO order disagreement.
It's hard to produce a good error message. In the absence of better
insight into error reporting, report the classes that were candidates
- to be put next into the MRO. There is some conflict between the
+ to be put next into the MRO. There is some conflict between the
order in which they should be put in the MRO, but it's hard to
diagnose what constraint can't be satisfied.
*/
@@ -1323,252 +1323,252 @@ check_duplicates(PyObject *list)
static void
set_mro_error(PyObject *to_merge, int *remain)
{
- Py_ssize_t i, n, off, to_merge_size;
- char buf[1000];
- PyObject *k, *v;
- PyObject *set = PyDict_New();
- if (!set) return;
-
- to_merge_size = PyList_GET_SIZE(to_merge);
- for (i = 0; i < to_merge_size; i++) {
- PyObject *L = PyList_GET_ITEM(to_merge, i);
- if (remain[i] < PyList_GET_SIZE(L)) {
- PyObject *c = PyList_GET_ITEM(L, remain[i]);
- if (PyDict_SetItem(set, c, Py_None) < 0) {
- Py_DECREF(set);
- return;
- }
- }
- }
- n = PyDict_Size(set);
-
- off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
+ Py_ssize_t i, n, off, to_merge_size;
+ char buf[1000];
+ PyObject *k, *v;
+ PyObject *set = PyDict_New();
+ if (!set) return;
+
+ to_merge_size = PyList_GET_SIZE(to_merge);
+ for (i = 0; i < to_merge_size; i++) {
+ PyObject *L = PyList_GET_ITEM(to_merge, i);
+ if (remain[i] < PyList_GET_SIZE(L)) {
+ PyObject *c = PyList_GET_ITEM(L, remain[i]);
+ if (PyDict_SetItem(set, c, Py_None) < 0) {
+ Py_DECREF(set);
+ return;
+ }
+ }
+ }
+ n = PyDict_Size(set);
+
+ off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
consistent method resolution\norder (MRO) for bases");
- i = 0;
- while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
- PyObject *name = class_name(k);
- off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
- name ? _PyUnicode_AsString(name) : "?");
- Py_XDECREF(name);
- if (--n && (size_t)(off+1) < sizeof(buf)) {
- buf[off++] = ',';
- buf[off] = '\0';
- }
- }
- PyErr_SetString(PyExc_TypeError, buf);
- Py_DECREF(set);
+ i = 0;
+ while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
+ PyObject *name = class_name(k);
+ off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
+ name ? _PyUnicode_AsString(name) : "?");
+ Py_XDECREF(name);
+ if (--n && (size_t)(off+1) < sizeof(buf)) {
+ buf[off++] = ',';
+ buf[off] = '\0';
+ }
+ }
+ PyErr_SetString(PyExc_TypeError, buf);
+ Py_DECREF(set);
}
static int
pmerge(PyObject *acc, PyObject* to_merge) {
- Py_ssize_t i, j, to_merge_size, empty_cnt;
- int *remain;
- int ok;
-
- to_merge_size = PyList_GET_SIZE(to_merge);
-
- /* remain stores an index into each sublist of to_merge.
- remain[i] is the index of the next base in to_merge[i]
- that is not included in acc.
- */
- remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
- if (remain == NULL)
- return -1;
- for (i = 0; i < to_merge_size; i++)
- remain[i] = 0;
+ Py_ssize_t i, j, to_merge_size, empty_cnt;
+ int *remain;
+ int ok;
+
+ to_merge_size = PyList_GET_SIZE(to_merge);
+
+ /* remain stores an index into each sublist of to_merge.
+ remain[i] is the index of the next base in to_merge[i]
+ that is not included in acc.
+ */
+ remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
+ if (remain == NULL)
+ return -1;
+ for (i = 0; i < to_merge_size; i++)
+ remain[i] = 0;
again:
- empty_cnt = 0;
- for (i = 0; i < to_merge_size; i++) {
- PyObject *candidate;
-
- PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
-
- if (remain[i] >= PyList_GET_SIZE(cur_list)) {
- empty_cnt++;
- continue;
- }
-
- /* Choose next candidate for MRO.
-
- The input sequences alone can determine the choice.
- If not, choose the class which appears in the MRO
- of the earliest direct superclass of the new class.
- */
-
- candidate = PyList_GET_ITEM(cur_list, remain[i]);
- for (j = 0; j < to_merge_size; j++) {
- PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
- if (tail_contains(j_lst, remain[j], candidate)) {
- goto skip; /* continue outer loop */
- }
- }
- ok = PyList_Append(acc, candidate);
- if (ok < 0) {
- PyMem_Free(remain);
- return -1;
- }
- for (j = 0; j < to_merge_size; j++) {
- PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
- if (remain[j] < PyList_GET_SIZE(j_lst) &&
- PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
- remain[j]++;
- }
- }
- goto again;
- skip: ;
- }
-
- if (empty_cnt == to_merge_size) {
- PyMem_FREE(remain);
- return 0;
- }
- set_mro_error(to_merge, remain);
- PyMem_FREE(remain);
- return -1;
+ empty_cnt = 0;
+ for (i = 0; i < to_merge_size; i++) {
+ PyObject *candidate;
+
+ PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
+
+ if (remain[i] >= PyList_GET_SIZE(cur_list)) {
+ empty_cnt++;
+ continue;
+ }
+
+ /* Choose next candidate for MRO.
+
+ The input sequences alone can determine the choice.
+ If not, choose the class which appears in the MRO
+ of the earliest direct superclass of the new class.
+ */
+
+ candidate = PyList_GET_ITEM(cur_list, remain[i]);
+ for (j = 0; j < to_merge_size; j++) {
+ PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
+ if (tail_contains(j_lst, remain[j], candidate)) {
+ goto skip; /* continue outer loop */
+ }
+ }
+ ok = PyList_Append(acc, candidate);
+ if (ok < 0) {
+ PyMem_Free(remain);
+ return -1;
+ }
+ for (j = 0; j < to_merge_size; j++) {
+ PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
+ if (remain[j] < PyList_GET_SIZE(j_lst) &&
+ PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
+ remain[j]++;
+ }
+ }
+ goto again;
+ skip: ;
+ }
+
+ if (empty_cnt == to_merge_size) {
+ PyMem_FREE(remain);
+ return 0;
+ }
+ set_mro_error(to_merge, remain);
+ PyMem_FREE(remain);
+ return -1;
}
static PyObject *
mro_implementation(PyTypeObject *type)
{
- Py_ssize_t i, n;
- int ok;
- PyObject *bases, *result;
- PyObject *to_merge, *bases_aslist;
-
- if (type->tp_dict == NULL) {
- if (PyType_Ready(type) < 0)
- return NULL;
- }
-
- /* Find a superclass linearization that honors the constraints
- of the explicit lists of bases and the constraints implied by
- each base class.
-
- to_merge is a list of lists, where each list is a superclass
- linearization implied by a base class. The last element of
- to_merge is the declared list of bases.
- */
-
- bases = type->tp_bases;
- n = PyTuple_GET_SIZE(bases);
-
- to_merge = PyList_New(n+1);
- if (to_merge == NULL)
- return NULL;
-
- for (i = 0; i < n; i++) {
- PyObject *base = PyTuple_GET_ITEM(bases, i);
- PyObject *parentMRO;
- parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
- if (parentMRO == NULL) {
- Py_DECREF(to_merge);
- return NULL;
- }
-
- PyList_SET_ITEM(to_merge, i, parentMRO);
- }
-
- bases_aslist = PySequence_List(bases);
- if (bases_aslist == NULL) {
- Py_DECREF(to_merge);
- return NULL;
- }
- /* This is just a basic sanity check. */
- if (check_duplicates(bases_aslist) < 0) {
- Py_DECREF(to_merge);
- Py_DECREF(bases_aslist);
- return NULL;
- }
- PyList_SET_ITEM(to_merge, n, bases_aslist);
-
- result = Py_BuildValue("[O]", (PyObject *)type);
- if (result == NULL) {
- Py_DECREF(to_merge);
- return NULL;
- }
-
- ok = pmerge(result, to_merge);
- Py_DECREF(to_merge);
- if (ok < 0) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ Py_ssize_t i, n;
+ int ok;
+ PyObject *bases, *result;
+ PyObject *to_merge, *bases_aslist;
+
+ if (type->tp_dict == NULL) {
+ if (PyType_Ready(type) < 0)
+ return NULL;
+ }
+
+ /* Find a superclass linearization that honors the constraints
+ of the explicit lists of bases and the constraints implied by
+ each base class.
+
+ to_merge is a list of lists, where each list is a superclass
+ linearization implied by a base class. The last element of
+ to_merge is the declared list of bases.
+ */
+
+ bases = type->tp_bases;
+ n = PyTuple_GET_SIZE(bases);
+
+ to_merge = PyList_New(n+1);
+ if (to_merge == NULL)
+ return NULL;
+
+ for (i = 0; i < n; i++) {
+ PyObject *base = PyTuple_GET_ITEM(bases, i);
+ PyObject *parentMRO;
+ parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
+ if (parentMRO == NULL) {
+ Py_DECREF(to_merge);
+ return NULL;
+ }
+
+ PyList_SET_ITEM(to_merge, i, parentMRO);
+ }
+
+ bases_aslist = PySequence_List(bases);
+ if (bases_aslist == NULL) {
+ Py_DECREF(to_merge);
+ return NULL;
+ }
+ /* This is just a basic sanity check. */
+ if (check_duplicates(bases_aslist) < 0) {
+ Py_DECREF(to_merge);
+ Py_DECREF(bases_aslist);
+ return NULL;
+ }
+ PyList_SET_ITEM(to_merge, n, bases_aslist);
+
+ result = Py_BuildValue("[O]", (PyObject *)type);
+ if (result == NULL) {
+ Py_DECREF(to_merge);
+ return NULL;
+ }
+
+ ok = pmerge(result, to_merge);
+ Py_DECREF(to_merge);
+ if (ok < 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ return result;
}
static PyObject *
mro_external(PyObject *self)
{
- PyTypeObject *type = (PyTypeObject *)self;
+ PyTypeObject *type = (PyTypeObject *)self;
- return mro_implementation(type);
+ return mro_implementation(type);
}
static int
mro_internal(PyTypeObject *type)
{
- PyObject *mro, *result, *tuple;
- int checkit = 0;
-
- if (Py_TYPE(type) == &PyType_Type) {
- result = mro_implementation(type);
- }
- else {
- static PyObject *mro_str;
- checkit = 1;
- mro = lookup_method((PyObject *)type, "mro", &mro_str);
- if (mro == NULL)
- return -1;
- result = PyObject_CallObject(mro, NULL);
- Py_DECREF(mro);
- }
- if (result == NULL)
- return -1;
- tuple = PySequence_Tuple(result);
- Py_DECREF(result);
- if (tuple == NULL)
- return -1;
- if (checkit) {
- Py_ssize_t i, len;
- PyObject *cls;
- PyTypeObject *solid;
-
- solid = solid_base(type);
-
- len = PyTuple_GET_SIZE(tuple);
-
- for (i = 0; i < len; i++) {
- PyTypeObject *t;
- cls = PyTuple_GET_ITEM(tuple, i);
- if (!PyType_Check(cls)) {
- PyErr_Format(PyExc_TypeError,
- "mro() returned a non-class ('%.500s')",
- Py_TYPE(cls)->tp_name);
- Py_DECREF(tuple);
- return -1;
- }
- t = (PyTypeObject*)cls;
- if (!PyType_IsSubtype(solid, solid_base(t))) {
- PyErr_Format(PyExc_TypeError,
- "mro() returned base with unsuitable layout ('%.500s')",
- t->tp_name);
- Py_DECREF(tuple);
- return -1;
- }
- }
- }
- type->tp_mro = tuple;
-
- type_mro_modified(type, type->tp_mro);
- /* corner case: the old-style super class might have been hidden
- from the custom MRO */
- type_mro_modified(type, type->tp_bases);
-
- PyType_Modified(type);
-
- return 0;
+ PyObject *mro, *result, *tuple;
+ int checkit = 0;
+
+ if (Py_TYPE(type) == &PyType_Type) {
+ result = mro_implementation(type);
+ }
+ else {
+ static PyObject *mro_str;
+ checkit = 1;
+ mro = lookup_method((PyObject *)type, "mro", &mro_str);
+ if (mro == NULL)
+ return -1;
+ result = PyObject_CallObject(mro, NULL);
+ Py_DECREF(mro);
+ }
+ if (result == NULL)
+ return -1;
+ tuple = PySequence_Tuple(result);
+ Py_DECREF(result);
+ if (tuple == NULL)
+ return -1;
+ if (checkit) {
+ Py_ssize_t i, len;
+ PyObject *cls;
+ PyTypeObject *solid;
+
+ solid = solid_base(type);
+
+ len = PyTuple_GET_SIZE(tuple);
+
+ for (i = 0; i < len; i++) {
+ PyTypeObject *t;
+ cls = PyTuple_GET_ITEM(tuple, i);
+ if (!PyType_Check(cls)) {
+ PyErr_Format(PyExc_TypeError,
+ "mro() returned a non-class ('%.500s')",
+ Py_TYPE(cls)->tp_name);
+ Py_DECREF(tuple);
+ return -1;
+ }
+ t = (PyTypeObject*)cls;
+ if (!PyType_IsSubtype(solid, solid_base(t))) {
+ PyErr_Format(PyExc_TypeError,
+ "mro() returned base with unsuitable layout ('%.500s')",
+ t->tp_name);
+ Py_DECREF(tuple);
+ return -1;
+ }
+ }
+ }
+ type->tp_mro = tuple;
+
+ type_mro_modified(type, type->tp_mro);
+ /* corner case: the old-style super class might have been hidden
+ from the custom MRO */
+ type_mro_modified(type, type->tp_bases);
+
+ PyType_Modified(type);
+
+ return 0;
}
@@ -1578,90 +1578,90 @@ mro_internal(PyTypeObject *type)
static PyTypeObject *
best_base(PyObject *bases)
{
- Py_ssize_t i, n;
- PyTypeObject *base, *winner, *candidate, *base_i;
- PyObject *base_proto;
-
- assert(PyTuple_Check(bases));
- n = PyTuple_GET_SIZE(bases);
- assert(n > 0);
- base = NULL;
- winner = NULL;
- for (i = 0; i < n; i++) {
- base_proto = PyTuple_GET_ITEM(bases, i);
- if (!PyType_Check(base_proto)) {
- PyErr_SetString(
- PyExc_TypeError,
- "bases must be types");
- return NULL;
- }
- base_i = (PyTypeObject *)base_proto;
- if (base_i->tp_dict == NULL) {
- if (PyType_Ready(base_i) < 0)
- return NULL;
- }
- candidate = solid_base(base_i);
- if (winner == NULL) {
- winner = candidate;
- base = base_i;
- }
- else if (PyType_IsSubtype(winner, candidate))
- ;
- else if (PyType_IsSubtype(candidate, winner)) {
- winner = candidate;
- base = base_i;
- }
- else {
- PyErr_SetString(
- PyExc_TypeError,
- "multiple bases have "
- "instance lay-out conflict");
- return NULL;
- }
- }
- if (base == NULL)
- PyErr_SetString(PyExc_TypeError,
- "a new-style class can't have only classic bases");
- return base;
+ Py_ssize_t i, n;
+ PyTypeObject *base, *winner, *candidate, *base_i;
+ PyObject *base_proto;
+
+ assert(PyTuple_Check(bases));
+ n = PyTuple_GET_SIZE(bases);
+ assert(n > 0);
+ base = NULL;
+ winner = NULL;
+ for (i = 0; i < n; i++) {
+ base_proto = PyTuple_GET_ITEM(bases, i);
+ if (!PyType_Check(base_proto)) {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "bases must be types");
+ return NULL;
+ }
+ base_i = (PyTypeObject *)base_proto;
+ if (base_i->tp_dict == NULL) {
+ if (PyType_Ready(base_i) < 0)
+ return NULL;
+ }
+ candidate = solid_base(base_i);
+ if (winner == NULL) {
+ winner = candidate;
+ base = base_i;
+ }
+ else if (PyType_IsSubtype(winner, candidate))
+ ;
+ else if (PyType_IsSubtype(candidate, winner)) {
+ winner = candidate;
+ base = base_i;
+ }
+ else {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "multiple bases have "
+ "instance lay-out conflict");
+ return NULL;
+ }
+ }
+ if (base == NULL)
+ PyErr_SetString(PyExc_TypeError,
+ "a new-style class can't have only classic bases");
+ return base;
}
static int
extra_ivars(PyTypeObject *type, PyTypeObject *base)
{
- size_t t_size = type->tp_basicsize;
- size_t b_size = base->tp_basicsize;
+ size_t t_size = type->tp_basicsize;
+ size_t b_size = base->tp_basicsize;
- assert(t_size >= b_size); /* Else type smaller than base! */
- if (type->tp_itemsize || base->tp_itemsize) {
- /* If itemsize is involved, stricter rules */
- return t_size != b_size ||
- type->tp_itemsize != base->tp_itemsize;
- }
- if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
- type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
- type->tp_flags & Py_TPFLAGS_HEAPTYPE)
- t_size -= sizeof(PyObject *);
- if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
- type->tp_dictoffset + sizeof(PyObject *) == t_size &&
- type->tp_flags & Py_TPFLAGS_HEAPTYPE)
- t_size -= sizeof(PyObject *);
+ assert(t_size >= b_size); /* Else type smaller than base! */
+ if (type->tp_itemsize || base->tp_itemsize) {
+ /* If itemsize is involved, stricter rules */
+ return t_size != b_size ||
+ type->tp_itemsize != base->tp_itemsize;
+ }
+ if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
+ type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
+ type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ t_size -= sizeof(PyObject *);
+ if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
+ type->tp_dictoffset + sizeof(PyObject *) == t_size &&
+ type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ t_size -= sizeof(PyObject *);
- return t_size != b_size;
+ return t_size != b_size;
}
static PyTypeObject *
solid_base(PyTypeObject *type)
{
- PyTypeObject *base;
+ PyTypeObject *base;
- if (type->tp_base)
- base = solid_base(type->tp_base);
- else
- base = &PyBaseObject_Type;
- if (extra_ivars(type, base))
- return type;
- else
- return base;
+ if (type->tp_base)
+ base = solid_base(type->tp_base);
+ else
+ base = &PyBaseObject_Type;
+ if (extra_ivars(type, base))
+ return type;
+ else
+ return base;
}
static void object_dealloc(PyObject *);
@@ -1677,180 +1677,180 @@ static void fixup_slot_dispatchers(PyTypeObject *);
static PyTypeObject *
get_builtin_base_with_dict(PyTypeObject *type)
{
- while (type->tp_base != NULL) {
- if (type->tp_dictoffset != 0 &&
- !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
- return type;
- type = type->tp_base;
- }
- return NULL;
+ while (type->tp_base != NULL) {
+ if (type->tp_dictoffset != 0 &&
+ !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
+ return type;
+ type = type->tp_base;
+ }
+ return NULL;
}
static PyObject *
get_dict_descriptor(PyTypeObject *type)
{
- static PyObject *dict_str;
- PyObject *descr;
+ static PyObject *dict_str;
+ PyObject *descr;
- if (dict_str == NULL) {
- dict_str = PyUnicode_InternFromString("__dict__");
- if (dict_str == NULL)
- return NULL;
- }
- descr = _PyType_Lookup(type, dict_str);
- if (descr == NULL || !PyDescr_IsData(descr))
- return NULL;
+ if (dict_str == NULL) {
+ dict_str = PyUnicode_InternFromString("__dict__");
+ if (dict_str == NULL)
+ return NULL;
+ }
+ descr = _PyType_Lookup(type, dict_str);
+ if (descr == NULL || !PyDescr_IsData(descr))
+ return NULL;
- return descr;
+ return descr;
}
static void
raise_dict_descr_error(PyObject *obj)
{
- PyErr_Format(PyExc_TypeError,
- "this __dict__ descriptor does not support "
- "'%.200s' objects", Py_TYPE(obj)->tp_name);
+ PyErr_Format(PyExc_TypeError,
+ "this __dict__ descriptor does not support "
+ "'%.200s' objects", Py_TYPE(obj)->tp_name);
}
static PyObject *
subtype_dict(PyObject *obj, void *context)
{
- PyObject **dictptr;
- PyObject *dict;
- PyTypeObject *base;
-
- base = get_builtin_base_with_dict(Py_TYPE(obj));
- if (base != NULL) {
- descrgetfunc func;
- PyObject *descr = get_dict_descriptor(base);
- if (descr == NULL) {
- raise_dict_descr_error(obj);
- return NULL;
- }
- func = Py_TYPE(descr)->tp_descr_get;
- if (func == NULL) {
- raise_dict_descr_error(obj);
- return NULL;
- }
- return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
- }
-
- dictptr = _PyObject_GetDictPtr(obj);
- if (dictptr == NULL) {
- PyErr_SetString(PyExc_AttributeError,
- "This object has no __dict__");
- return NULL;
- }
- dict = *dictptr;
- if (dict == NULL)
- *dictptr = dict = PyDict_New();
- Py_XINCREF(dict);
- return dict;
+ PyObject **dictptr;
+ PyObject *dict;
+ PyTypeObject *base;
+
+ base = get_builtin_base_with_dict(Py_TYPE(obj));
+ if (base != NULL) {
+ descrgetfunc func;
+ PyObject *descr = get_dict_descriptor(base);
+ if (descr == NULL) {
+ raise_dict_descr_error(obj);
+ return NULL;
+ }
+ func = Py_TYPE(descr)->tp_descr_get;
+ if (func == NULL) {
+ raise_dict_descr_error(obj);
+ return NULL;
+ }
+ return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
+ }
+
+ dictptr = _PyObject_GetDictPtr(obj);
+ if (dictptr == NULL) {
+ PyErr_SetString(PyExc_AttributeError,
+ "This object has no __dict__");
+ return NULL;
+ }
+ dict = *dictptr;
+ if (dict == NULL)
+ *dictptr = dict = PyDict_New();
+ Py_XINCREF(dict);
+ return dict;
}
static int
subtype_setdict(PyObject *obj, PyObject *value, void *context)
{
- PyObject **dictptr;
- PyObject *dict;
- PyTypeObject *base;
-
- base = get_builtin_base_with_dict(Py_TYPE(obj));
- if (base != NULL) {
- descrsetfunc func;
- PyObject *descr = get_dict_descriptor(base);
- if (descr == NULL) {
- raise_dict_descr_error(obj);
- return -1;
- }
- func = Py_TYPE(descr)->tp_descr_set;
- if (func == NULL) {
- raise_dict_descr_error(obj);
- return -1;
- }
- return func(descr, obj, value);
- }
-
- dictptr = _PyObject_GetDictPtr(obj);
- if (dictptr == NULL) {
- PyErr_SetString(PyExc_AttributeError,
- "This object has no __dict__");
- return -1;
- }
- if (value != NULL && !PyDict_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "__dict__ must be set to a dictionary, "
- "not a '%.200s'", Py_TYPE(value)->tp_name);
- return -1;
- }
- dict = *dictptr;
- Py_XINCREF(value);
- *dictptr = value;
- Py_XDECREF(dict);
- return 0;
+ PyObject **dictptr;
+ PyObject *dict;
+ PyTypeObject *base;
+
+ base = get_builtin_base_with_dict(Py_TYPE(obj));
+ if (base != NULL) {
+ descrsetfunc func;
+ PyObject *descr = get_dict_descriptor(base);
+ if (descr == NULL) {
+ raise_dict_descr_error(obj);
+ return -1;
+ }
+ func = Py_TYPE(descr)->tp_descr_set;
+ if (func == NULL) {
+ raise_dict_descr_error(obj);
+ return -1;
+ }
+ return func(descr, obj, value);
+ }
+
+ dictptr = _PyObject_GetDictPtr(obj);
+ if (dictptr == NULL) {
+ PyErr_SetString(PyExc_AttributeError,
+ "This object has no __dict__");
+ return -1;
+ }
+ if (value != NULL && !PyDict_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "__dict__ must be set to a dictionary, "
+ "not a '%.200s'", Py_TYPE(value)->tp_name);
+ return -1;
+ }
+ dict = *dictptr;
+ Py_XINCREF(value);
+ *dictptr = value;
+ Py_XDECREF(dict);
+ return 0;
}
static PyObject *
subtype_getweakref(PyObject *obj, void *context)
{
- PyObject **weaklistptr;
- PyObject *result;
-
- if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
- PyErr_SetString(PyExc_AttributeError,
- "This object has no __weakref__");
- return NULL;
- }
- assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
- assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
- (size_t)(Py_TYPE(obj)->tp_basicsize));
- weaklistptr = (PyObject **)
- ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
- if (*weaklistptr == NULL)
- result = Py_None;
- else
- result = *weaklistptr;
- Py_INCREF(result);
- return result;
+ PyObject **weaklistptr;
+ PyObject *result;
+
+ if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
+ PyErr_SetString(PyExc_AttributeError,
+ "This object has no __weakref__");
+ return NULL;
+ }
+ assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
+ assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
+ (size_t)(Py_TYPE(obj)->tp_basicsize));
+ weaklistptr = (PyObject **)
+ ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
+ if (*weaklistptr == NULL)
+ result = Py_None;
+ else
+ result = *weaklistptr;
+ Py_INCREF(result);
+ return result;
}
/* Three variants on the subtype_getsets list. */
static PyGetSetDef subtype_getsets_full[] = {
- {"__dict__", subtype_dict, subtype_setdict,
- PyDoc_STR("dictionary for instance variables (if defined)")},
- {"__weakref__", subtype_getweakref, NULL,
- PyDoc_STR("list of weak references to the object (if defined)")},
- {0}
+ {"__dict__", subtype_dict, subtype_setdict,
+ PyDoc_STR("dictionary for instance variables (if defined)")},
+ {"__weakref__", subtype_getweakref, NULL,
+ PyDoc_STR("list of weak references to the object (if defined)")},
+ {0}
};
static PyGetSetDef subtype_getsets_dict_only[] = {
- {"__dict__", subtype_dict, subtype_setdict,
- PyDoc_STR("dictionary for instance variables (if defined)")},
- {0}
+ {"__dict__", subtype_dict, subtype_setdict,
+ PyDoc_STR("dictionary for instance variables (if defined)")},
+ {0}
};
static PyGetSetDef subtype_getsets_weakref_only[] = {
- {"__weakref__", subtype_getweakref, NULL,
- PyDoc_STR("list of weak references to the object (if defined)")},
- {0}
+ {"__weakref__", subtype_getweakref, NULL,
+ PyDoc_STR("list of weak references to the object (if defined)")},
+ {0}
};
static int
valid_identifier(PyObject *s)
{
- if (!PyUnicode_Check(s)) {
- PyErr_Format(PyExc_TypeError,
- "__slots__ items must be strings, not '%.200s'",
- Py_TYPE(s)->tp_name);
- return 0;
- }
- if (!PyUnicode_IsIdentifier(s)) {
- PyErr_SetString(PyExc_TypeError,
- "__slots__ must be identifiers");
- return 0;
- }
- return 1;
+ if (!PyUnicode_Check(s)) {
+ PyErr_Format(PyExc_TypeError,
+ "__slots__ items must be strings, not '%.200s'",
+ Py_TYPE(s)->tp_name);
+ return 0;
+ }
+ if (!PyUnicode_IsIdentifier(s)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__slots__ must be identifiers");
+ return 0;
+ }
+ return 1;
}
/* Forward */
@@ -1860,435 +1860,435 @@ object_init(PyObject *self, PyObject *args, PyObject *kwds);
static int
type_init(PyObject *cls, PyObject *args, PyObject *kwds)
{
- int res;
+ int res;
- assert(args != NULL && PyTuple_Check(args));
- assert(kwds == NULL || PyDict_Check(kwds));
+ assert(args != NULL && PyTuple_Check(args));
+ assert(kwds == NULL || PyDict_Check(kwds));
- if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
- PyErr_SetString(PyExc_TypeError,
- "type.__init__() takes no keyword arguments");
- return -1;
- }
+ if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "type.__init__() takes no keyword arguments");
+ return -1;
+ }
- if (args != NULL && PyTuple_Check(args) &&
- (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
- PyErr_SetString(PyExc_TypeError,
- "type.__init__() takes 1 or 3 arguments");
- return -1;
- }
+ if (args != NULL && PyTuple_Check(args) &&
+ (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
+ PyErr_SetString(PyExc_TypeError,
+ "type.__init__() takes 1 or 3 arguments");
+ return -1;
+ }
- /* Call object.__init__(self) now. */
- /* XXX Could call super(type, cls).__init__() but what's the point? */
- args = PyTuple_GetSlice(args, 0, 0);
- res = object_init(cls, args, NULL);
- Py_DECREF(args);
- return res;
+ /* Call object.__init__(self) now. */
+ /* XXX Could call super(type, cls).__init__() but what's the point? */
+ args = PyTuple_GetSlice(args, 0, 0);
+ res = object_init(cls, args, NULL);
+ Py_DECREF(args);
+ return res;
}
static PyObject *
type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
{
- PyObject *name, *bases, *dict;
- static char *kwlist[] = {"name", "bases", "dict", 0};
- PyObject *slots, *tmp, *newslots;
- PyTypeObject *type, *base, *tmptype, *winner;
- PyHeapTypeObject *et;
- PyMemberDef *mp;
- Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
- int j, may_add_dict, may_add_weak;
-
- assert(args != NULL && PyTuple_Check(args));
- assert(kwds == NULL || PyDict_Check(kwds));
-
- /* Special case: type(x) should return x->ob_type */
- {
- const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
- const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
-
- if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
- PyObject *x = PyTuple_GET_ITEM(args, 0);
- Py_INCREF(Py_TYPE(x));
- return (PyObject *) Py_TYPE(x);
- }
-
- /* SF bug 475327 -- if that didn't trigger, we need 3
- arguments. but PyArg_ParseTupleAndKeywords below may give
- a msg saying type() needs exactly 3. */
- if (nargs + nkwds != 3) {
- PyErr_SetString(PyExc_TypeError,
- "type() takes 1 or 3 arguments");
- return NULL;
- }
- }
-
- /* Check arguments: (name, bases, dict) */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
- &name,
- &PyTuple_Type, &bases,
- &PyDict_Type, &dict))
- return NULL;
-
- /* Determine the proper metatype to deal with this,
- and check for metatype conflicts while we're at it.
- Note that if some other metatype wins to contract,
- it's possible that its instances are not types. */
- nbases = PyTuple_GET_SIZE(bases);
- winner = metatype;
- for (i = 0; i < nbases; i++) {
- tmp = PyTuple_GET_ITEM(bases, i);
- tmptype = Py_TYPE(tmp);
- if (PyType_IsSubtype(winner, tmptype))
- continue;
- if (PyType_IsSubtype(tmptype, winner)) {
- winner = tmptype;
- continue;
- }
- PyErr_SetString(PyExc_TypeError,
- "metaclass conflict: "
- "the metaclass of a derived class "
- "must be a (non-strict) subclass "
- "of the metaclasses of all its bases");
- return NULL;
- }
- if (winner != metatype) {
- if (winner->tp_new != type_new) /* Pass it to the winner */
- return winner->tp_new(winner, args, kwds);
- metatype = winner;
- }
-
- /* Adjust for empty tuple bases */
- if (nbases == 0) {
- bases = PyTuple_Pack(1, &PyBaseObject_Type);
- if (bases == NULL)
- return NULL;
- nbases = 1;
- }
- else
- Py_INCREF(bases);
-
- /* XXX From here until type is allocated, "return NULL" leaks bases! */
-
- /* Calculate best base, and check that all bases are type objects */
- base = best_base(bases);
- if (base == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
- if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
- PyErr_Format(PyExc_TypeError,
- "type '%.100s' is not an acceptable base type",
- base->tp_name);
- Py_DECREF(bases);
- return NULL;
- }
-
- /* Check for a __slots__ sequence variable in dict, and count it */
- slots = PyDict_GetItemString(dict, "__slots__");
- nslots = 0;
- add_dict = 0;
- add_weak = 0;
- may_add_dict = base->tp_dictoffset == 0;
- may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
- if (slots == NULL) {
- if (may_add_dict) {
- add_dict++;
- }
- if (may_add_weak) {
- add_weak++;
- }
- }
- else {
- /* Have slots */
-
- /* Make it into a tuple */
- if (PyUnicode_Check(slots))
- slots = PyTuple_Pack(1, slots);
- else
- slots = PySequence_Tuple(slots);
- if (slots == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
- assert(PyTuple_Check(slots));
-
- /* Are slots allowed? */
- nslots = PyTuple_GET_SIZE(slots);
- if (nslots > 0 && base->tp_itemsize != 0) {
- PyErr_Format(PyExc_TypeError,
- "nonempty __slots__ "
- "not supported for subtype of '%s'",
- base->tp_name);
- bad_slots:
- Py_DECREF(bases);
- Py_DECREF(slots);
- return NULL;
- }
-
- /* Check for valid slot names and two special cases */
- for (i = 0; i < nslots; i++) {
- PyObject *tmp = PyTuple_GET_ITEM(slots, i);
- if (!valid_identifier(tmp))
- goto bad_slots;
- assert(PyUnicode_Check(tmp));
- if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
- if (!may_add_dict || add_dict) {
- PyErr_SetString(PyExc_TypeError,
- "__dict__ slot disallowed: "
- "we already got one");
- goto bad_slots;
- }
- add_dict++;
- }
- if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
- if (!may_add_weak || add_weak) {
- PyErr_SetString(PyExc_TypeError,
- "__weakref__ slot disallowed: "
- "either we already got one, "
- "or __itemsize__ != 0");
- goto bad_slots;
- }
- add_weak++;
- }
- }
-
- /* Copy slots into a list, mangle names and sort them.
- Sorted names are needed for __class__ assignment.
- Convert them back to tuple at the end.
- */
- newslots = PyList_New(nslots - add_dict - add_weak);
- if (newslots == NULL)
- goto bad_slots;
- for (i = j = 0; i < nslots; i++) {
- tmp = PyTuple_GET_ITEM(slots, i);
- if ((add_dict &&
- PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
- (add_weak &&
- PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
- continue;
- tmp =_Py_Mangle(name, tmp);
- if (!tmp)
- goto bad_slots;
- PyList_SET_ITEM(newslots, j, tmp);
- j++;
- }
- assert(j == nslots - add_dict - add_weak);
- nslots = j;
- Py_DECREF(slots);
- if (PyList_Sort(newslots) == -1) {
- Py_DECREF(bases);
- Py_DECREF(newslots);
- return NULL;
- }
- slots = PyList_AsTuple(newslots);
- Py_DECREF(newslots);
- if (slots == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
-
- /* Secondary bases may provide weakrefs or dict */
- if (nbases > 1 &&
- ((may_add_dict && !add_dict) ||
- (may_add_weak && !add_weak))) {
- for (i = 0; i < nbases; i++) {
- tmp = PyTuple_GET_ITEM(bases, i);
- if (tmp == (PyObject *)base)
- continue; /* Skip primary base */
- assert(PyType_Check(tmp));
- tmptype = (PyTypeObject *)tmp;
- if (may_add_dict && !add_dict &&
- tmptype->tp_dictoffset != 0)
- add_dict++;
- if (may_add_weak && !add_weak &&
- tmptype->tp_weaklistoffset != 0)
- add_weak++;
- if (may_add_dict && !add_dict)
- continue;
- if (may_add_weak && !add_weak)
- continue;
- /* Nothing more to check */
- break;
- }
- }
- }
-
- /* XXX From here until type is safely allocated,
- "return NULL" may leak slots! */
-
- /* Allocate the type object */
- type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
- if (type == NULL) {
- Py_XDECREF(slots);
- Py_DECREF(bases);
- return NULL;
- }
-
- /* Keep name and slots alive in the extended type object */
- et = (PyHeapTypeObject *)type;
- Py_INCREF(name);
- et->ht_name = name;
- et->ht_slots = slots;
-
- /* Initialize tp_flags */
- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
- Py_TPFLAGS_BASETYPE;
- if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
- type->tp_flags |= Py_TPFLAGS_HAVE_GC;
-
- /* Initialize essential fields */
- type->tp_as_number = &et->as_number;
- type->tp_as_sequence = &et->as_sequence;
- type->tp_as_mapping = &et->as_mapping;
- type->tp_as_buffer = &et->as_buffer;
- type->tp_name = _PyUnicode_AsString(name);
- if (!type->tp_name) {
- Py_DECREF(type);
- return NULL;
- }
-
- /* Set tp_base and tp_bases */
- type->tp_bases = bases;
- Py_INCREF(base);
- type->tp_base = base;
-
- /* Initialize tp_dict from passed-in dict */
- type->tp_dict = dict = PyDict_Copy(dict);
- if (dict == NULL) {
- Py_DECREF(type);
- return NULL;
- }
-
- /* Set __module__ in the dict */
- if (PyDict_GetItemString(dict, "__module__") == NULL) {
- tmp = PyEval_GetGlobals();
- if (tmp != NULL) {
- tmp = PyDict_GetItemString(tmp, "__name__");
- if (tmp != NULL) {
- if (PyDict_SetItemString(dict, "__module__",
- tmp) < 0)
- return NULL;
- }
- }
- }
-
- /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
- and is a string. The __doc__ accessor will first look for tp_doc;
- if that fails, it will still look into __dict__.
- */
- {
- PyObject *doc = PyDict_GetItemString(dict, "__doc__");
- if (doc != NULL && PyUnicode_Check(doc)) {
- Py_ssize_t len;
- char *doc_str;
- char *tp_doc;
-
- doc_str = _PyUnicode_AsString(doc);
- if (doc_str == NULL) {
- Py_DECREF(type);
- return NULL;
- }
- /* Silently truncate the docstring if it contains null bytes. */
- len = strlen(doc_str);
- tp_doc = (char *)PyObject_MALLOC(len + 1);
- if (tp_doc == NULL) {
- Py_DECREF(type);
- return NULL;
- }
- memcpy(tp_doc, doc_str, len + 1);
- type->tp_doc = tp_doc;
- }
- }
-
- /* Special-case __new__: if it's a plain function,
- make it a static function */
- tmp = PyDict_GetItemString(dict, "__new__");
- if (tmp != NULL && PyFunction_Check(tmp)) {
- tmp = PyStaticMethod_New(tmp);
- if (tmp == NULL) {
- Py_DECREF(type);
- return NULL;
- }
- PyDict_SetItemString(dict, "__new__", tmp);
- Py_DECREF(tmp);
- }
-
- /* Add descriptors for custom slots from __slots__, or for __dict__ */
- mp = PyHeapType_GET_MEMBERS(et);
- slotoffset = base->tp_basicsize;
- if (slots != NULL) {
- for (i = 0; i < nslots; i++, mp++) {
- mp->name = _PyUnicode_AsString(
- PyTuple_GET_ITEM(slots, i));
- mp->type = T_OBJECT_EX;
- mp->offset = slotoffset;
-
- /* __dict__ and __weakref__ are already filtered out */
- assert(strcmp(mp->name, "__dict__") != 0);
- assert(strcmp(mp->name, "__weakref__") != 0);
-
- slotoffset += sizeof(PyObject *);
- }
- }
- if (add_dict) {
- if (base->tp_itemsize)
- type->tp_dictoffset = -(long)sizeof(PyObject *);
- else
- type->tp_dictoffset = slotoffset;
- slotoffset += sizeof(PyObject *);
- }
- if (add_weak) {
- assert(!base->tp_itemsize);
- type->tp_weaklistoffset = slotoffset;
- slotoffset += sizeof(PyObject *);
- }
- type->tp_basicsize = slotoffset;
- type->tp_itemsize = base->tp_itemsize;
- type->tp_members = PyHeapType_GET_MEMBERS(et);
-
- if (type->tp_weaklistoffset && type->tp_dictoffset)
- type->tp_getset = subtype_getsets_full;
- else if (type->tp_weaklistoffset && !type->tp_dictoffset)
- type->tp_getset = subtype_getsets_weakref_only;
- else if (!type->tp_weaklistoffset && type->tp_dictoffset)
- type->tp_getset = subtype_getsets_dict_only;
- else
- type->tp_getset = NULL;
-
- /* Special case some slots */
- if (type->tp_dictoffset != 0 || nslots > 0) {
- if (base->tp_getattr == NULL && base->tp_getattro == NULL)
- type->tp_getattro = PyObject_GenericGetAttr;
- if (base->tp_setattr == NULL && base->tp_setattro == NULL)
- type->tp_setattro = PyObject_GenericSetAttr;
- }
- type->tp_dealloc = subtype_dealloc;
-
- /* Enable GC unless there are really no instance variables possible */
- if (!(type->tp_basicsize == sizeof(PyObject) &&
- type->tp_itemsize == 0))
- type->tp_flags |= Py_TPFLAGS_HAVE_GC;
-
- /* Always override allocation strategy to use regular heap */
- type->tp_alloc = PyType_GenericAlloc;
- if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
- type->tp_free = PyObject_GC_Del;
- type->tp_traverse = subtype_traverse;
- type->tp_clear = subtype_clear;
- }
- else
- type->tp_free = PyObject_Del;
-
- /* Initialize the rest */
- if (PyType_Ready(type) < 0) {
- Py_DECREF(type);
- return NULL;
- }
-
- /* Put the proper slots in place */
- fixup_slot_dispatchers(type);
-
- return (PyObject *)type;
+ PyObject *name, *bases, *dict;
+ static char *kwlist[] = {"name", "bases", "dict", 0};
+ PyObject *slots, *tmp, *newslots;
+ PyTypeObject *type, *base, *tmptype, *winner;
+ PyHeapTypeObject *et;
+ PyMemberDef *mp;
+ Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
+ int j, may_add_dict, may_add_weak;
+
+ assert(args != NULL && PyTuple_Check(args));
+ assert(kwds == NULL || PyDict_Check(kwds));
+
+ /* Special case: type(x) should return x->ob_type */
+ {
+ const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
+ const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
+
+ if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
+ PyObject *x = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(Py_TYPE(x));
+ return (PyObject *) Py_TYPE(x);
+ }
+
+ /* SF bug 475327 -- if that didn't trigger, we need 3
+ arguments. but PyArg_ParseTupleAndKeywords below may give
+ a msg saying type() needs exactly 3. */
+ if (nargs + nkwds != 3) {
+ PyErr_SetString(PyExc_TypeError,
+ "type() takes 1 or 3 arguments");
+ return NULL;
+ }
+ }
+
+ /* Check arguments: (name, bases, dict) */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "UO!O!:type", kwlist,
+ &name,
+ &PyTuple_Type, &bases,
+ &PyDict_Type, &dict))
+ return NULL;
+
+ /* Determine the proper metatype to deal with this,
+ and check for metatype conflicts while we're at it.
+ Note that if some other metatype wins to contract,
+ it's possible that its instances are not types. */
+ nbases = PyTuple_GET_SIZE(bases);
+ winner = metatype;
+ for (i = 0; i < nbases; i++) {
+ tmp = PyTuple_GET_ITEM(bases, i);
+ tmptype = Py_TYPE(tmp);
+ if (PyType_IsSubtype(winner, tmptype))
+ continue;
+ if (PyType_IsSubtype(tmptype, winner)) {
+ winner = tmptype;
+ continue;
+ }
+ PyErr_SetString(PyExc_TypeError,
+ "metaclass conflict: "
+ "the metaclass of a derived class "
+ "must be a (non-strict) subclass "
+ "of the metaclasses of all its bases");
+ return NULL;
+ }
+ if (winner != metatype) {
+ if (winner->tp_new != type_new) /* Pass it to the winner */
+ return winner->tp_new(winner, args, kwds);
+ metatype = winner;
+ }
+
+ /* Adjust for empty tuple bases */
+ if (nbases == 0) {
+ bases = PyTuple_Pack(1, &PyBaseObject_Type);
+ if (bases == NULL)
+ return NULL;
+ nbases = 1;
+ }
+ else
+ Py_INCREF(bases);
+
+ /* XXX From here until type is allocated, "return NULL" leaks bases! */
+
+ /* Calculate best base, and check that all bases are type objects */
+ base = best_base(bases);
+ if (base == NULL) {
+ Py_DECREF(bases);
+ return NULL;
+ }
+ if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
+ PyErr_Format(PyExc_TypeError,
+ "type '%.100s' is not an acceptable base type",
+ base->tp_name);
+ Py_DECREF(bases);
+ return NULL;
+ }
+
+ /* Check for a __slots__ sequence variable in dict, and count it */
+ slots = PyDict_GetItemString(dict, "__slots__");
+ nslots = 0;
+ add_dict = 0;
+ add_weak = 0;
+ may_add_dict = base->tp_dictoffset == 0;
+ may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
+ if (slots == NULL) {
+ if (may_add_dict) {
+ add_dict++;
+ }
+ if (may_add_weak) {
+ add_weak++;
+ }
+ }
+ else {
+ /* Have slots */
+
+ /* Make it into a tuple */
+ if (PyUnicode_Check(slots))
+ slots = PyTuple_Pack(1, slots);
+ else
+ slots = PySequence_Tuple(slots);
+ if (slots == NULL) {
+ Py_DECREF(bases);
+ return NULL;
+ }
+ assert(PyTuple_Check(slots));
+
+ /* Are slots allowed? */
+ nslots = PyTuple_GET_SIZE(slots);
+ if (nslots > 0 && base->tp_itemsize != 0) {
+ PyErr_Format(PyExc_TypeError,
+ "nonempty __slots__ "
+ "not supported for subtype of '%s'",
+ base->tp_name);
+ bad_slots:
+ Py_DECREF(bases);
+ Py_DECREF(slots);
+ return NULL;
+ }
+
+ /* Check for valid slot names and two special cases */
+ for (i = 0; i < nslots; i++) {
+ PyObject *tmp = PyTuple_GET_ITEM(slots, i);
+ if (!valid_identifier(tmp))
+ goto bad_slots;
+ assert(PyUnicode_Check(tmp));
+ if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) {
+ if (!may_add_dict || add_dict) {
+ PyErr_SetString(PyExc_TypeError,
+ "__dict__ slot disallowed: "
+ "we already got one");
+ goto bad_slots;
+ }
+ add_dict++;
+ }
+ if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
+ if (!may_add_weak || add_weak) {
+ PyErr_SetString(PyExc_TypeError,
+ "__weakref__ slot disallowed: "
+ "either we already got one, "
+ "or __itemsize__ != 0");
+ goto bad_slots;
+ }
+ add_weak++;
+ }
+ }
+
+ /* Copy slots into a list, mangle names and sort them.
+ Sorted names are needed for __class__ assignment.
+ Convert them back to tuple at the end.
+ */
+ newslots = PyList_New(nslots - add_dict - add_weak);
+ if (newslots == NULL)
+ goto bad_slots;
+ for (i = j = 0; i < nslots; i++) {
+ tmp = PyTuple_GET_ITEM(slots, i);
+ if ((add_dict &&
+ PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) ||
+ (add_weak &&
+ PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
+ continue;
+ tmp =_Py_Mangle(name, tmp);
+ if (!tmp)
+ goto bad_slots;
+ PyList_SET_ITEM(newslots, j, tmp);
+ j++;
+ }
+ assert(j == nslots - add_dict - add_weak);
+ nslots = j;
+ Py_DECREF(slots);
+ if (PyList_Sort(newslots) == -1) {
+ Py_DECREF(bases);
+ Py_DECREF(newslots);
+ return NULL;
+ }
+ slots = PyList_AsTuple(newslots);
+ Py_DECREF(newslots);
+ if (slots == NULL) {
+ Py_DECREF(bases);
+ return NULL;
+ }
+
+ /* Secondary bases may provide weakrefs or dict */
+ if (nbases > 1 &&
+ ((may_add_dict && !add_dict) ||
+ (may_add_weak && !add_weak))) {
+ for (i = 0; i < nbases; i++) {
+ tmp = PyTuple_GET_ITEM(bases, i);
+ if (tmp == (PyObject *)base)
+ continue; /* Skip primary base */
+ assert(PyType_Check(tmp));
+ tmptype = (PyTypeObject *)tmp;
+ if (may_add_dict && !add_dict &&
+ tmptype->tp_dictoffset != 0)
+ add_dict++;
+ if (may_add_weak && !add_weak &&
+ tmptype->tp_weaklistoffset != 0)
+ add_weak++;
+ if (may_add_dict && !add_dict)
+ continue;
+ if (may_add_weak && !add_weak)
+ continue;
+ /* Nothing more to check */
+ break;
+ }
+ }
+ }
+
+ /* XXX From here until type is safely allocated,
+ "return NULL" may leak slots! */
+
+ /* Allocate the type object */
+ type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
+ if (type == NULL) {
+ Py_XDECREF(slots);
+ Py_DECREF(bases);
+ return NULL;
+ }
+
+ /* Keep name and slots alive in the extended type object */
+ et = (PyHeapTypeObject *)type;
+ Py_INCREF(name);
+ et->ht_name = name;
+ et->ht_slots = slots;
+
+ /* Initialize tp_flags */
+ type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
+ Py_TPFLAGS_BASETYPE;
+ if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+
+ /* Initialize essential fields */
+ type->tp_as_number = &et->as_number;
+ type->tp_as_sequence = &et->as_sequence;
+ type->tp_as_mapping = &et->as_mapping;
+ type->tp_as_buffer = &et->as_buffer;
+ type->tp_name = _PyUnicode_AsString(name);
+ if (!type->tp_name) {
+ Py_DECREF(type);
+ return NULL;
+ }
+
+ /* Set tp_base and tp_bases */
+ type->tp_bases = bases;
+ Py_INCREF(base);
+ type->tp_base = base;
+
+ /* Initialize tp_dict from passed-in dict */
+ type->tp_dict = dict = PyDict_Copy(dict);
+ if (dict == NULL) {
+ Py_DECREF(type);
+ return NULL;
+ }
+
+ /* Set __module__ in the dict */
+ if (PyDict_GetItemString(dict, "__module__") == NULL) {
+ tmp = PyEval_GetGlobals();
+ if (tmp != NULL) {
+ tmp = PyDict_GetItemString(tmp, "__name__");
+ if (tmp != NULL) {
+ if (PyDict_SetItemString(dict, "__module__",
+ tmp) < 0)
+ return NULL;
+ }
+ }
+ }
+
+ /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
+ and is a string. The __doc__ accessor will first look for tp_doc;
+ if that fails, it will still look into __dict__.
+ */
+ {
+ PyObject *doc = PyDict_GetItemString(dict, "__doc__");
+ if (doc != NULL && PyUnicode_Check(doc)) {
+ Py_ssize_t len;
+ char *doc_str;
+ char *tp_doc;
+
+ doc_str = _PyUnicode_AsString(doc);
+ if (doc_str == NULL) {
+ Py_DECREF(type);
+ return NULL;
+ }
+ /* Silently truncate the docstring if it contains null bytes. */
+ len = strlen(doc_str);
+ tp_doc = (char *)PyObject_MALLOC(len + 1);
+ if (tp_doc == NULL) {
+ Py_DECREF(type);
+ return NULL;
+ }
+ memcpy(tp_doc, doc_str, len + 1);
+ type->tp_doc = tp_doc;
+ }
+ }
+
+ /* Special-case __new__: if it's a plain function,
+ make it a static function */
+ tmp = PyDict_GetItemString(dict, "__new__");
+ if (tmp != NULL && PyFunction_Check(tmp)) {
+ tmp = PyStaticMethod_New(tmp);
+ if (tmp == NULL) {
+ Py_DECREF(type);
+ return NULL;
+ }
+ PyDict_SetItemString(dict, "__new__", tmp);
+ Py_DECREF(tmp);
+ }
+
+ /* Add descriptors for custom slots from __slots__, or for __dict__ */
+ mp = PyHeapType_GET_MEMBERS(et);
+ slotoffset = base->tp_basicsize;
+ if (slots != NULL) {
+ for (i = 0; i < nslots; i++, mp++) {
+ mp->name = _PyUnicode_AsString(
+ PyTuple_GET_ITEM(slots, i));
+ mp->type = T_OBJECT_EX;
+ mp->offset = slotoffset;
+
+ /* __dict__ and __weakref__ are already filtered out */
+ assert(strcmp(mp->name, "__dict__") != 0);
+ assert(strcmp(mp->name, "__weakref__") != 0);
+
+ slotoffset += sizeof(PyObject *);
+ }
+ }
+ if (add_dict) {
+ if (base->tp_itemsize)
+ type->tp_dictoffset = -(long)sizeof(PyObject *);
+ else
+ type->tp_dictoffset = slotoffset;
+ slotoffset += sizeof(PyObject *);
+ }
+ if (add_weak) {
+ assert(!base->tp_itemsize);
+ type->tp_weaklistoffset = slotoffset;
+ slotoffset += sizeof(PyObject *);
+ }
+ type->tp_basicsize = slotoffset;
+ type->tp_itemsize = base->tp_itemsize;
+ type->tp_members = PyHeapType_GET_MEMBERS(et);
+
+ if (type->tp_weaklistoffset && type->tp_dictoffset)
+ type->tp_getset = subtype_getsets_full;
+ else if (type->tp_weaklistoffset && !type->tp_dictoffset)
+ type->tp_getset = subtype_getsets_weakref_only;
+ else if (!type->tp_weaklistoffset && type->tp_dictoffset)
+ type->tp_getset = subtype_getsets_dict_only;
+ else
+ type->tp_getset = NULL;
+
+ /* Special case some slots */
+ if (type->tp_dictoffset != 0 || nslots > 0) {
+ if (base->tp_getattr == NULL && base->tp_getattro == NULL)
+ type->tp_getattro = PyObject_GenericGetAttr;
+ if (base->tp_setattr == NULL && base->tp_setattro == NULL)
+ type->tp_setattro = PyObject_GenericSetAttr;
+ }
+ type->tp_dealloc = subtype_dealloc;
+
+ /* Enable GC unless there are really no instance variables possible */
+ if (!(type->tp_basicsize == sizeof(PyObject) &&
+ type->tp_itemsize == 0))
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+
+ /* Always override allocation strategy to use regular heap */
+ type->tp_alloc = PyType_GenericAlloc;
+ if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
+ type->tp_free = PyObject_GC_Del;
+ type->tp_traverse = subtype_traverse;
+ type->tp_clear = subtype_clear;
+ }
+ else
+ type->tp_free = PyObject_Del;
+
+ /* Initialize the rest */
+ if (PyType_Ready(type) < 0) {
+ Py_DECREF(type);
+ return NULL;
+ }
+
+ /* Put the proper slots in place */
+ fixup_slot_dispatchers(type);
+
+ return (PyObject *)type;
}
/* Internal API to look for a name through the MRO.
@@ -2296,50 +2296,50 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
PyObject *
_PyType_Lookup(PyTypeObject *type, PyObject *name)
{
- Py_ssize_t i, n;
- PyObject *mro, *res, *base, *dict;
- unsigned int h;
-
- if (MCACHE_CACHEABLE_NAME(name) &&
- PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
- /* fast path */
- h = MCACHE_HASH_METHOD(type, name);
- if (method_cache[h].version == type->tp_version_tag &&
- method_cache[h].name == name)
- return method_cache[h].value;
- }
-
- /* Look in tp_dict of types in MRO */
- mro = type->tp_mro;
-
- /* If mro is NULL, the type is either not yet initialized
- by PyType_Ready(), or already cleared by type_clear().
- Either way the safest thing to do is to return NULL. */
- if (mro == NULL)
- return NULL;
-
- res = NULL;
- assert(PyTuple_Check(mro));
- n = PyTuple_GET_SIZE(mro);
- for (i = 0; i < n; i++) {
- base = PyTuple_GET_ITEM(mro, i);
- assert(PyType_Check(base));
- dict = ((PyTypeObject *)base)->tp_dict;
- assert(dict && PyDict_Check(dict));
- res = PyDict_GetItem(dict, name);
- if (res != NULL)
- break;
- }
-
- if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
- h = MCACHE_HASH_METHOD(type, name);
- method_cache[h].version = type->tp_version_tag;
- method_cache[h].value = res; /* borrowed */
- Py_INCREF(name);
- Py_DECREF(method_cache[h].name);
- method_cache[h].name = name;
- }
- return res;
+ Py_ssize_t i, n;
+ PyObject *mro, *res, *base, *dict;
+ unsigned int h;
+
+ if (MCACHE_CACHEABLE_NAME(name) &&
+ PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
+ /* fast path */
+ h = MCACHE_HASH_METHOD(type, name);
+ if (method_cache[h].version == type->tp_version_tag &&
+ method_cache[h].name == name)
+ return method_cache[h].value;
+ }
+
+ /* Look in tp_dict of types in MRO */
+ mro = type->tp_mro;
+
+ /* If mro is NULL, the type is either not yet initialized
+ by PyType_Ready(), or already cleared by type_clear().
+ Either way the safest thing to do is to return NULL. */
+ if (mro == NULL)
+ return NULL;
+
+ res = NULL;
+ assert(PyTuple_Check(mro));
+ n = PyTuple_GET_SIZE(mro);
+ for (i = 0; i < n; i++) {
+ base = PyTuple_GET_ITEM(mro, i);
+ assert(PyType_Check(base));
+ dict = ((PyTypeObject *)base)->tp_dict;
+ assert(dict && PyDict_Check(dict));
+ res = PyDict_GetItem(dict, name);
+ if (res != NULL)
+ break;
+ }
+
+ if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
+ h = MCACHE_HASH_METHOD(type, name);
+ method_cache[h].version = type->tp_version_tag;
+ method_cache[h].value = res; /* borrowed */
+ Py_INCREF(name);
+ Py_DECREF(method_cache[h].name);
+ method_cache[h].name = name;
+ }
+ return res;
}
/* This is similar to PyObject_GenericGetAttr(),
@@ -2347,166 +2347,166 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
static PyObject *
type_getattro(PyTypeObject *type, PyObject *name)
{
- PyTypeObject *metatype = Py_TYPE(type);
- PyObject *meta_attribute, *attribute;
- descrgetfunc meta_get;
-
- /* Initialize this type (we'll assume the metatype is initialized) */
- if (type->tp_dict == NULL) {
- if (PyType_Ready(type) < 0)
- return NULL;
- }
-
- /* No readable descriptor found yet */
- meta_get = NULL;
-
- /* Look for the attribute in the metatype */
- meta_attribute = _PyType_Lookup(metatype, name);
-
- if (meta_attribute != NULL) {
- meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
-
- if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
- /* Data descriptors implement tp_descr_set to intercept
- * writes. Assume the attribute is not overridden in
- * type's tp_dict (and bases): call the descriptor now.
- */
- return meta_get(meta_attribute, (PyObject *)type,
- (PyObject *)metatype);
- }
- Py_INCREF(meta_attribute);
- }
-
- /* No data descriptor found on metatype. Look in tp_dict of this
- * type and its bases */
- attribute = _PyType_Lookup(type, name);
- if (attribute != NULL) {
- /* Implement descriptor functionality, if any */
- descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
-
- Py_XDECREF(meta_attribute);
-
- if (local_get != NULL) {
- /* NULL 2nd argument indicates the descriptor was
- * found on the target object itself (or a base) */
- return local_get(attribute, (PyObject *)NULL,
- (PyObject *)type);
- }
-
- Py_INCREF(attribute);
- return attribute;
- }
-
- /* No attribute found in local __dict__ (or bases): use the
- * descriptor from the metatype, if any */
- if (meta_get != NULL) {
- PyObject *res;
- res = meta_get(meta_attribute, (PyObject *)type,
- (PyObject *)metatype);
- Py_DECREF(meta_attribute);
- return res;
- }
-
- /* If an ordinary attribute was found on the metatype, return it now */
- if (meta_attribute != NULL) {
- return meta_attribute;
- }
-
- /* Give up */
- PyErr_Format(PyExc_AttributeError,
- "type object '%.50s' has no attribute '%U'",
- type->tp_name, name);
- return NULL;
+ PyTypeObject *metatype = Py_TYPE(type);
+ PyObject *meta_attribute, *attribute;
+ descrgetfunc meta_get;
+
+ /* Initialize this type (we'll assume the metatype is initialized) */
+ if (type->tp_dict == NULL) {
+ if (PyType_Ready(type) < 0)
+ return NULL;
+ }
+
+ /* No readable descriptor found yet */
+ meta_get = NULL;
+
+ /* Look for the attribute in the metatype */
+ meta_attribute = _PyType_Lookup(metatype, name);
+
+ if (meta_attribute != NULL) {
+ meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
+
+ if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
+ /* Data descriptors implement tp_descr_set to intercept
+ * writes. Assume the attribute is not overridden in
+ * type's tp_dict (and bases): call the descriptor now.
+ */
+ return meta_get(meta_attribute, (PyObject *)type,
+ (PyObject *)metatype);
+ }
+ Py_INCREF(meta_attribute);
+ }
+
+ /* No data descriptor found on metatype. Look in tp_dict of this
+ * type and its bases */
+ attribute = _PyType_Lookup(type, name);
+ if (attribute != NULL) {
+ /* Implement descriptor functionality, if any */
+ descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
+
+ Py_XDECREF(meta_attribute);
+
+ if (local_get != NULL) {
+ /* NULL 2nd argument indicates the descriptor was
+ * found on the target object itself (or a base) */
+ return local_get(attribute, (PyObject *)NULL,
+ (PyObject *)type);
+ }
+
+ Py_INCREF(attribute);
+ return attribute;
+ }
+
+ /* No attribute found in local __dict__ (or bases): use the
+ * descriptor from the metatype, if any */
+ if (meta_get != NULL) {
+ PyObject *res;
+ res = meta_get(meta_attribute, (PyObject *)type,
+ (PyObject *)metatype);
+ Py_DECREF(meta_attribute);
+ return res;
+ }
+
+ /* If an ordinary attribute was found on the metatype, return it now */
+ if (meta_attribute != NULL) {
+ return meta_attribute;
+ }
+
+ /* Give up */
+ PyErr_Format(PyExc_AttributeError,
+ "type object '%.50s' has no attribute '%U'",
+ type->tp_name, name);
+ return NULL;
}
static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
- if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
- PyErr_Format(
- PyExc_TypeError,
- "can't set attributes of built-in/extension type '%s'",
- type->tp_name);
- return -1;
- }
- if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
- return -1;
- return update_slot(type, name);
+ if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
+ PyErr_Format(
+ PyExc_TypeError,
+ "can't set attributes of built-in/extension type '%s'",
+ type->tp_name);
+ return -1;
+ }
+ if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
+ return -1;
+ return update_slot(type, name);
}
static void
type_dealloc(PyTypeObject *type)
{
- PyHeapTypeObject *et;
-
- /* Assert this is a heap-allocated type object */
- assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
- _PyObject_GC_UNTRACK(type);
- PyObject_ClearWeakRefs((PyObject *)type);
- et = (PyHeapTypeObject *)type;
- Py_XDECREF(type->tp_base);
- Py_XDECREF(type->tp_dict);
- Py_XDECREF(type->tp_bases);
- Py_XDECREF(type->tp_mro);
- Py_XDECREF(type->tp_cache);
- Py_XDECREF(type->tp_subclasses);
- /* A type's tp_doc is heap allocated, unlike the tp_doc slots
- * of most other objects. It's okay to cast it to char *.
- */
- PyObject_Free((char *)type->tp_doc);
- Py_XDECREF(et->ht_name);
- Py_XDECREF(et->ht_slots);
- Py_TYPE(type)->tp_free((PyObject *)type);
+ PyHeapTypeObject *et;
+
+ /* Assert this is a heap-allocated type object */
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
+ _PyObject_GC_UNTRACK(type);
+ PyObject_ClearWeakRefs((PyObject *)type);
+ et = (PyHeapTypeObject *)type;
+ Py_XDECREF(type->tp_base);
+ Py_XDECREF(type->tp_dict);
+ Py_XDECREF(type->tp_bases);
+ Py_XDECREF(type->tp_mro);
+ Py_XDECREF(type->tp_cache);
+ Py_XDECREF(type->tp_subclasses);
+ /* A type's tp_doc is heap allocated, unlike the tp_doc slots
+ * of most other objects. It's okay to cast it to char *.
+ */
+ PyObject_Free((char *)type->tp_doc);
+ Py_XDECREF(et->ht_name);
+ Py_XDECREF(et->ht_slots);
+ Py_TYPE(type)->tp_free((PyObject *)type);
}
static PyObject *
type_subclasses(PyTypeObject *type, PyObject *args_ignored)
{
- PyObject *list, *raw, *ref;
- Py_ssize_t i, n;
-
- list = PyList_New(0);
- if (list == NULL)
- return NULL;
- raw = type->tp_subclasses;
- if (raw == NULL)
- return list;
- assert(PyList_Check(raw));
- n = PyList_GET_SIZE(raw);
- for (i = 0; i < n; i++) {
- ref = PyList_GET_ITEM(raw, i);
- assert(PyWeakref_CheckRef(ref));
- ref = PyWeakref_GET_OBJECT(ref);
- if (ref != Py_None) {
- if (PyList_Append(list, ref) < 0) {
- Py_DECREF(list);
- return NULL;
- }
- }
- }
- return list;
+ PyObject *list, *raw, *ref;
+ Py_ssize_t i, n;
+
+ list = PyList_New(0);
+ if (list == NULL)
+ return NULL;
+ raw = type->tp_subclasses;
+ if (raw == NULL)
+ return list;
+ assert(PyList_Check(raw));
+ n = PyList_GET_SIZE(raw);
+ for (i = 0; i < n; i++) {
+ ref = PyList_GET_ITEM(raw, i);
+ assert(PyWeakref_CheckRef(ref));
+ ref = PyWeakref_GET_OBJECT(ref);
+ if (ref != Py_None) {
+ if (PyList_Append(list, ref) < 0) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ }
+ }
+ return list;
}
static PyObject *
type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
{
- return PyDict_New();
+ return PyDict_New();
}
static PyMethodDef type_methods[] = {
- {"mro", (PyCFunction)mro_external, METH_NOARGS,
- PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
- {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
- PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
- {"__prepare__", (PyCFunction)type_prepare,
- METH_VARARGS | METH_KEYWORDS | METH_CLASS,
- PyDoc_STR("__prepare__() -> dict\n"
- "used to create the namespace for the class statement")},
- {"__instancecheck__", type___instancecheck__, METH_O,
- PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
- {"__subclasscheck__", type___subclasscheck__, METH_O,
- PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
- {0}
+ {"mro", (PyCFunction)mro_external, METH_NOARGS,
+ PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
+ {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
+ PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
+ {"__prepare__", (PyCFunction)type_prepare,
+ METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+ PyDoc_STR("__prepare__() -> dict\n"
+ "used to create the namespace for the class statement")},
+ {"__instancecheck__", type___instancecheck__, METH_O,
+ PyDoc_STR("__instancecheck__() -> check if an object is an instance")},
+ {"__subclasscheck__", type___subclasscheck__, METH_O,
+ PyDoc_STR("__subclasschck__ -> check if an class is a subclass")},
+ {0}
};
PyDoc_STRVAR(type_doc,
@@ -2516,109 +2516,109 @@ PyDoc_STRVAR(type_doc,
static int
type_traverse(PyTypeObject *type, visitproc visit, void *arg)
{
- /* Because of type_is_gc(), the collector only calls this
- for heaptypes. */
- assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
+ /* Because of type_is_gc(), the collector only calls this
+ for heaptypes. */
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
- Py_VISIT(type->tp_dict);
- Py_VISIT(type->tp_cache);
- Py_VISIT(type->tp_mro);
- Py_VISIT(type->tp_bases);
- Py_VISIT(type->tp_base);
+ Py_VISIT(type->tp_dict);
+ Py_VISIT(type->tp_cache);
+ Py_VISIT(type->tp_mro);
+ Py_VISIT(type->tp_bases);
+ Py_VISIT(type->tp_base);
- /* There's no need to visit type->tp_subclasses or
- ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
- in cycles; tp_subclasses is a list of weak references,
- and slots is a tuple of strings. */
+ /* There's no need to visit type->tp_subclasses or
+ ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
+ in cycles; tp_subclasses is a list of weak references,
+ and slots is a tuple of strings. */
- return 0;
+ return 0;
}
static int
type_clear(PyTypeObject *type)
{
- /* Because of type_is_gc(), the collector only calls this
- for heaptypes. */
- assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
+ /* Because of type_is_gc(), the collector only calls this
+ for heaptypes. */
+ assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
- /* The only field we need to clear is tp_mro, which is part of a
- hard cycle (its first element is the class itself) that won't
- be broken otherwise (it's a tuple and tuples don't have a
- tp_clear handler). None of the other fields need to be
- cleared, and here's why:
+ /* The only field we need to clear is tp_mro, which is part of a
+ hard cycle (its first element is the class itself) that won't
+ be broken otherwise (it's a tuple and tuples don't have a
+ tp_clear handler). None of the other fields need to be
+ cleared, and here's why:
- tp_dict:
- It is a dict, so the collector will call its tp_clear.
+ tp_dict:
+ It is a dict, so the collector will call its tp_clear.
- tp_cache:
- Not used; if it were, it would be a dict.
+ tp_cache:
+ Not used; if it were, it would be a dict.
- tp_bases, tp_base:
- If these are involved in a cycle, there must be at least
- one other, mutable object in the cycle, e.g. a base
- class's dict; the cycle will be broken that way.
+ tp_bases, tp_base:
+ If these are involved in a cycle, there must be at least
+ one other, mutable object in the cycle, e.g. a base
+ class's dict; the cycle will be broken that way.
- tp_subclasses:
- A list of weak references can't be part of a cycle; and
- lists have their own tp_clear.
+ tp_subclasses:
+ A list of weak references can't be part of a cycle; and
+ lists have their own tp_clear.
- slots (in PyHeapTypeObject):
- A tuple of strings can't be part of a cycle.
- */
+ slots (in PyHeapTypeObject):
+ A tuple of strings can't be part of a cycle.
+ */
- Py_CLEAR(type->tp_mro);
+ Py_CLEAR(type->tp_mro);
- return 0;
+ return 0;
}
static int
type_is_gc(PyTypeObject *type)
{
- return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
+ return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
}
PyTypeObject PyType_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "type", /* tp_name */
- sizeof(PyHeapTypeObject), /* tp_basicsize */
- sizeof(PyMemberDef), /* tp_itemsize */
- (destructor)type_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)type_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- (ternaryfunc)type_call, /* tp_call */
- 0, /* tp_str */
- (getattrofunc)type_getattro, /* tp_getattro */
- (setattrofunc)type_setattro, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
- type_doc, /* tp_doc */
- (traverseproc)type_traverse, /* tp_traverse */
- (inquiry)type_clear, /* tp_clear */
- 0, /* tp_richcompare */
- offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- type_methods, /* tp_methods */
- type_members, /* tp_members */
- type_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
- type_init, /* tp_init */
- 0, /* tp_alloc */
- type_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
- (inquiry)type_is_gc, /* tp_is_gc */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "type", /* tp_name */
+ sizeof(PyHeapTypeObject), /* tp_basicsize */
+ sizeof(PyMemberDef), /* tp_itemsize */
+ (destructor)type_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)type_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ (ternaryfunc)type_call, /* tp_call */
+ 0, /* tp_str */
+ (getattrofunc)type_getattro, /* tp_getattro */
+ (setattrofunc)type_setattro, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS, /* tp_flags */
+ type_doc, /* tp_doc */
+ (traverseproc)type_traverse, /* tp_traverse */
+ (inquiry)type_clear, /* tp_clear */
+ 0, /* tp_richcompare */
+ offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ type_methods, /* tp_methods */
+ type_members, /* tp_members */
+ type_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
+ type_init, /* tp_init */
+ 0, /* tp_alloc */
+ type_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
+ (inquiry)type_is_gc, /* tp_is_gc */
};
@@ -2671,318 +2671,318 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static int
excess_args(PyObject *args, PyObject *kwds)
{
- return PyTuple_GET_SIZE(args) ||
- (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
+ return PyTuple_GET_SIZE(args) ||
+ (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
}
static int
object_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- int err = 0;
- if (excess_args(args, kwds)) {
- PyTypeObject *type = Py_TYPE(self);
- if (type->tp_init != object_init &&
- type->tp_new != object_new)
- {
- err = PyErr_WarnEx(PyExc_DeprecationWarning,
- "object.__init__() takes no parameters",
- 1);
- }
- else if (type->tp_init != object_init ||
- type->tp_new == object_new)
- {
- PyErr_SetString(PyExc_TypeError,
- "object.__init__() takes no parameters");
- err = -1;
- }
- }
- return err;
+ int err = 0;
+ if (excess_args(args, kwds)) {
+ PyTypeObject *type = Py_TYPE(self);
+ if (type->tp_init != object_init &&
+ type->tp_new != object_new)
+ {
+ err = PyErr_WarnEx(PyExc_DeprecationWarning,
+ "object.__init__() takes no parameters",
+ 1);
+ }
+ else if (type->tp_init != object_init ||
+ type->tp_new == object_new)
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "object.__init__() takes no parameters");
+ err = -1;
+ }
+ }
+ return err;
}
static PyObject *
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- int err = 0;
- if (excess_args(args, kwds)) {
- if (type->tp_new != object_new &&
- type->tp_init != object_init)
- {
- err = PyErr_WarnEx(PyExc_DeprecationWarning,
- "object.__new__() takes no parameters",
- 1);
- }
- else if (type->tp_new != object_new ||
- type->tp_init == object_init)
- {
- PyErr_SetString(PyExc_TypeError,
- "object.__new__() takes no parameters");
- err = -1;
- }
- }
- if (err < 0)
- return NULL;
-
- if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
- static PyObject *comma = NULL;
- PyObject *abstract_methods = NULL;
- PyObject *builtins;
- PyObject *sorted;
- PyObject *sorted_methods = NULL;
- PyObject *joined = NULL;
-
- /* Compute ", ".join(sorted(type.__abstractmethods__))
- into joined. */
- abstract_methods = type_abstractmethods(type, NULL);
- if (abstract_methods == NULL)
- goto error;
- builtins = PyEval_GetBuiltins();
- if (builtins == NULL)
- goto error;
- sorted = PyDict_GetItemString(builtins, "sorted");
- if (sorted == NULL)
- goto error;
- sorted_methods = PyObject_CallFunctionObjArgs(sorted,
- abstract_methods,
- NULL);
- if (sorted_methods == NULL)
- goto error;
- if (comma == NULL) {
- comma = PyUnicode_InternFromString(", ");
- if (comma == NULL)
- goto error;
- }
- joined = PyObject_CallMethod(comma, "join",
- "O", sorted_methods);
- if (joined == NULL)
- goto error;
-
- PyErr_Format(PyExc_TypeError,
- "Can't instantiate abstract class %s "
- "with abstract methods %U",
- type->tp_name,
- joined);
- error:
- Py_XDECREF(joined);
- Py_XDECREF(sorted_methods);
- Py_XDECREF(abstract_methods);
- return NULL;
- }
- return type->tp_alloc(type, 0);
+ int err = 0;
+ if (excess_args(args, kwds)) {
+ if (type->tp_new != object_new &&
+ type->tp_init != object_init)
+ {
+ err = PyErr_WarnEx(PyExc_DeprecationWarning,
+ "object.__new__() takes no parameters",
+ 1);
+ }
+ else if (type->tp_new != object_new ||
+ type->tp_init == object_init)
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "object.__new__() takes no parameters");
+ err = -1;
+ }
+ }
+ if (err < 0)
+ return NULL;
+
+ if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
+ static PyObject *comma = NULL;
+ PyObject *abstract_methods = NULL;
+ PyObject *builtins;
+ PyObject *sorted;
+ PyObject *sorted_methods = NULL;
+ PyObject *joined = NULL;
+
+ /* Compute ", ".join(sorted(type.__abstractmethods__))
+ into joined. */
+ abstract_methods = type_abstractmethods(type, NULL);
+ if (abstract_methods == NULL)
+ goto error;
+ builtins = PyEval_GetBuiltins();
+ if (builtins == NULL)
+ goto error;
+ sorted = PyDict_GetItemString(builtins, "sorted");
+ if (sorted == NULL)
+ goto error;
+ sorted_methods = PyObject_CallFunctionObjArgs(sorted,
+ abstract_methods,
+ NULL);
+ if (sorted_methods == NULL)
+ goto error;
+ if (comma == NULL) {
+ comma = PyUnicode_InternFromString(", ");
+ if (comma == NULL)
+ goto error;
+ }
+ joined = PyObject_CallMethod(comma, "join",
+ "O", sorted_methods);
+ if (joined == NULL)
+ goto error;
+
+ PyErr_Format(PyExc_TypeError,
+ "Can't instantiate abstract class %s "
+ "with abstract methods %U",
+ type->tp_name,
+ joined);
+ error:
+ Py_XDECREF(joined);
+ Py_XDECREF(sorted_methods);
+ Py_XDECREF(abstract_methods);
+ return NULL;
+ }
+ return type->tp_alloc(type, 0);
}
static void
object_dealloc(PyObject *self)
{
- Py_TYPE(self)->tp_free(self);
+ Py_TYPE(self)->tp_free(self);
}
static PyObject *
object_repr(PyObject *self)
{
- PyTypeObject *type;
- PyObject *mod, *name, *rtn;
-
- type = Py_TYPE(self);
- mod = type_module(type, NULL);
- if (mod == NULL)
- PyErr_Clear();
- else if (!PyUnicode_Check(mod)) {
- Py_DECREF(mod);
- mod = NULL;
- }
- name = type_name(type, NULL);
- if (name == NULL)
- return NULL;
- if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
- rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
- else
- rtn = PyUnicode_FromFormat("<%s object at %p>",
- type->tp_name, self);
- Py_XDECREF(mod);
- Py_DECREF(name);
- return rtn;
+ PyTypeObject *type;
+ PyObject *mod, *name, *rtn;
+
+ type = Py_TYPE(self);
+ mod = type_module(type, NULL);
+ if (mod == NULL)
+ PyErr_Clear();
+ else if (!PyUnicode_Check(mod)) {
+ Py_DECREF(mod);
+ mod = NULL;
+ }
+ name = type_name(type, NULL);
+ if (name == NULL)
+ return NULL;
+ if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins"))
+ rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
+ else
+ rtn = PyUnicode_FromFormat("<%s object at %p>",
+ type->tp_name, self);
+ Py_XDECREF(mod);
+ Py_DECREF(name);
+ return rtn;
}
static PyObject *
object_str(PyObject *self)
{
- unaryfunc f;
+ unaryfunc f;
- f = Py_TYPE(self)->tp_repr;
- if (f == NULL)
- f = object_repr;
- return f(self);
+ f = Py_TYPE(self)->tp_repr;
+ if (f == NULL)
+ f = object_repr;
+ return f(self);
}
static PyObject *
object_richcompare(PyObject *self, PyObject *other, int op)
{
- PyObject *res;
-
- switch (op) {
-
- case Py_EQ:
- /* Return NotImplemented instead of False, so if two
- objects are compared, both get a chance at the
- comparison. See issue #1393. */
- res = (self == other) ? Py_True : Py_NotImplemented;
- Py_INCREF(res);
- break;
-
- case Py_NE:
- /* By default, != returns the opposite of ==,
- unless the latter returns NotImplemented. */
- res = PyObject_RichCompare(self, other, Py_EQ);
- if (res != NULL && res != Py_NotImplemented) {
- int ok = PyObject_IsTrue(res);
- Py_DECREF(res);
- if (ok < 0)
- res = NULL;
- else {
- if (ok)
- res = Py_False;
- else
- res = Py_True;
- Py_INCREF(res);
- }
- }
- break;
-
- default:
- res = Py_NotImplemented;
- Py_INCREF(res);
- break;
- }
-
- return res;
+ PyObject *res;
+
+ switch (op) {
+
+ case Py_EQ:
+ /* Return NotImplemented instead of False, so if two
+ objects are compared, both get a chance at the
+ comparison. See issue #1393. */
+ res = (self == other) ? Py_True : Py_NotImplemented;
+ Py_INCREF(res);
+ break;
+
+ case Py_NE:
+ /* By default, != returns the opposite of ==,
+ unless the latter returns NotImplemented. */
+ res = PyObject_RichCompare(self, other, Py_EQ);
+ if (res != NULL && res != Py_NotImplemented) {
+ int ok = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (ok < 0)
+ res = NULL;
+ else {
+ if (ok)
+ res = Py_False;
+ else
+ res = Py_True;
+ Py_INCREF(res);
+ }
+ }
+ break;
+
+ default:
+ res = Py_NotImplemented;
+ Py_INCREF(res);
+ break;
+ }
+
+ return res;
}
static PyObject *
object_get_class(PyObject *self, void *closure)
{
- Py_INCREF(Py_TYPE(self));
- return (PyObject *)(Py_TYPE(self));
+ Py_INCREF(Py_TYPE(self));
+ return (PyObject *)(Py_TYPE(self));
}
static int
equiv_structs(PyTypeObject *a, PyTypeObject *b)
{
- return a == b ||
- (a != NULL &&
- b != NULL &&
- a->tp_basicsize == b->tp_basicsize &&
- a->tp_itemsize == b->tp_itemsize &&
- a->tp_dictoffset == b->tp_dictoffset &&
- a->tp_weaklistoffset == b->tp_weaklistoffset &&
- ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
- (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
+ return a == b ||
+ (a != NULL &&
+ b != NULL &&
+ a->tp_basicsize == b->tp_basicsize &&
+ a->tp_itemsize == b->tp_itemsize &&
+ a->tp_dictoffset == b->tp_dictoffset &&
+ a->tp_weaklistoffset == b->tp_weaklistoffset &&
+ ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
+ (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
}
static int
same_slots_added(PyTypeObject *a, PyTypeObject *b)
{
- PyTypeObject *base = a->tp_base;
- Py_ssize_t size;
- PyObject *slots_a, *slots_b;
-
- if (base != b->tp_base)
- return 0;
- if (equiv_structs(a, base) && equiv_structs(b, base))
- return 1;
- size = base->tp_basicsize;
- if (a->tp_dictoffset == size && b->tp_dictoffset == size)
- size += sizeof(PyObject *);
- if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
- size += sizeof(PyObject *);
-
- /* Check slots compliance */
- slots_a = ((PyHeapTypeObject *)a)->ht_slots;
- slots_b = ((PyHeapTypeObject *)b)->ht_slots;
- if (slots_a && slots_b) {
- if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
- return 0;
- size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
- }
- return size == a->tp_basicsize && size == b->tp_basicsize;
+ PyTypeObject *base = a->tp_base;
+ Py_ssize_t size;
+ PyObject *slots_a, *slots_b;
+
+ if (base != b->tp_base)
+ return 0;
+ if (equiv_structs(a, base) && equiv_structs(b, base))
+ return 1;
+ size = base->tp_basicsize;
+ if (a->tp_dictoffset == size && b->tp_dictoffset == size)
+ size += sizeof(PyObject *);
+ if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
+ size += sizeof(PyObject *);
+
+ /* Check slots compliance */
+ slots_a = ((PyHeapTypeObject *)a)->ht_slots;
+ slots_b = ((PyHeapTypeObject *)b)->ht_slots;
+ if (slots_a && slots_b) {
+ if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
+ return 0;
+ size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
+ }
+ return size == a->tp_basicsize && size == b->tp_basicsize;
}
static int
compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
{
- PyTypeObject *newbase, *oldbase;
-
- if (newto->tp_dealloc != oldto->tp_dealloc ||
- newto->tp_free != oldto->tp_free)
- {
- PyErr_Format(PyExc_TypeError,
- "%s assignment: "
- "'%s' deallocator differs from '%s'",
- attr,
- newto->tp_name,
- oldto->tp_name);
- return 0;
- }
- newbase = newto;
- oldbase = oldto;
- while (equiv_structs(newbase, newbase->tp_base))
- newbase = newbase->tp_base;
- while (equiv_structs(oldbase, oldbase->tp_base))
- oldbase = oldbase->tp_base;
- if (newbase != oldbase &&
- (newbase->tp_base != oldbase->tp_base ||
- !same_slots_added(newbase, oldbase))) {
- PyErr_Format(PyExc_TypeError,
- "%s assignment: "
- "'%s' object layout differs from '%s'",
- attr,
- newto->tp_name,
- oldto->tp_name);
- return 0;
- }
-
- return 1;
+ PyTypeObject *newbase, *oldbase;
+
+ if (newto->tp_dealloc != oldto->tp_dealloc ||
+ newto->tp_free != oldto->tp_free)
+ {
+ PyErr_Format(PyExc_TypeError,
+ "%s assignment: "
+ "'%s' deallocator differs from '%s'",
+ attr,
+ newto->tp_name,
+ oldto->tp_name);
+ return 0;
+ }
+ newbase = newto;
+ oldbase = oldto;
+ while (equiv_structs(newbase, newbase->tp_base))
+ newbase = newbase->tp_base;
+ while (equiv_structs(oldbase, oldbase->tp_base))
+ oldbase = oldbase->tp_base;
+ if (newbase != oldbase &&
+ (newbase->tp_base != oldbase->tp_base ||
+ !same_slots_added(newbase, oldbase))) {
+ PyErr_Format(PyExc_TypeError,
+ "%s assignment: "
+ "'%s' object layout differs from '%s'",
+ attr,
+ newto->tp_name,
+ oldto->tp_name);
+ return 0;
+ }
+
+ return 1;
}
static int
object_set_class(PyObject *self, PyObject *value, void *closure)
{
- PyTypeObject *oldto = Py_TYPE(self);
- PyTypeObject *newto;
-
- if (value == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete __class__ attribute");
- return -1;
- }
- if (!PyType_Check(value)) {
- PyErr_Format(PyExc_TypeError,
- "__class__ must be set to new-style class, not '%s' object",
- Py_TYPE(value)->tp_name);
- return -1;
- }
- newto = (PyTypeObject *)value;
- if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
- !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
- {
- PyErr_Format(PyExc_TypeError,
- "__class__ assignment: only for heap types");
- return -1;
- }
- if (compatible_for_assignment(newto, oldto, "__class__")) {
- Py_INCREF(newto);
- Py_TYPE(self) = newto;
- Py_DECREF(oldto);
- return 0;
- }
- else {
- return -1;
- }
+ PyTypeObject *oldto = Py_TYPE(self);
+ PyTypeObject *newto;
+
+ if (value == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete __class__ attribute");
+ return -1;
+ }
+ if (!PyType_Check(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "__class__ must be set to new-style class, not '%s' object",
+ Py_TYPE(value)->tp_name);
+ return -1;
+ }
+ newto = (PyTypeObject *)value;
+ if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
+ !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
+ {
+ PyErr_Format(PyExc_TypeError,
+ "__class__ assignment: only for heap types");
+ return -1;
+ }
+ if (compatible_for_assignment(newto, oldto, "__class__")) {
+ Py_INCREF(newto);
+ Py_TYPE(self) = newto;
+ Py_DECREF(oldto);
+ return 0;
+ }
+ else {
+ return -1;
+ }
}
static PyGetSetDef object_getsets[] = {
- {"__class__", object_get_class, object_set_class,
- PyDoc_STR("the object's class")},
- {0}
+ {"__class__", object_get_class, object_set_class,
+ PyDoc_STR("the object's class")},
+ {0}
};
@@ -2996,194 +2996,194 @@ static PyGetSetDef object_getsets[] = {
static PyObject *
import_copyreg(void)
{
- static PyObject *copyreg_str;
+ static PyObject *copyreg_str;
- if (!copyreg_str) {
- copyreg_str = PyUnicode_InternFromString("copyreg");
- if (copyreg_str == NULL)
- return NULL;
- }
+ if (!copyreg_str) {
+ copyreg_str = PyUnicode_InternFromString("copyreg");
+ if (copyreg_str == NULL)
+ return NULL;
+ }
- return PyImport_Import(copyreg_str);
+ return PyImport_Import(copyreg_str);
}
static PyObject *
slotnames(PyObject *cls)
{
- PyObject *clsdict;
- PyObject *copyreg;
- PyObject *slotnames;
-
- if (!PyType_Check(cls)) {
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- clsdict = ((PyTypeObject *)cls)->tp_dict;
- slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
- if (slotnames != NULL && PyList_Check(slotnames)) {
- Py_INCREF(slotnames);
- return slotnames;
- }
-
- copyreg = import_copyreg();
- if (copyreg == NULL)
- return NULL;
-
- slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
- Py_DECREF(copyreg);
- if (slotnames != NULL &&
- slotnames != Py_None &&
- !PyList_Check(slotnames))
- {
- PyErr_SetString(PyExc_TypeError,
- "copyreg._slotnames didn't return a list or None");
- Py_DECREF(slotnames);
- slotnames = NULL;
- }
-
- return slotnames;
+ PyObject *clsdict;
+ PyObject *copyreg;
+ PyObject *slotnames;
+
+ if (!PyType_Check(cls)) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ clsdict = ((PyTypeObject *)cls)->tp_dict;
+ slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
+ if (slotnames != NULL && PyList_Check(slotnames)) {
+ Py_INCREF(slotnames);
+ return slotnames;
+ }
+
+ copyreg = import_copyreg();
+ if (copyreg == NULL)
+ return NULL;
+
+ slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
+ Py_DECREF(copyreg);
+ if (slotnames != NULL &&
+ slotnames != Py_None &&
+ !PyList_Check(slotnames))
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "copyreg._slotnames didn't return a list or None");
+ Py_DECREF(slotnames);
+ slotnames = NULL;
+ }
+
+ return slotnames;
}
static PyObject *
reduce_2(PyObject *obj)
{
- PyObject *cls, *getnewargs;
- PyObject *args = NULL, *args2 = NULL;
- PyObject *getstate = NULL, *state = NULL, *names = NULL;
- PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
- PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
- Py_ssize_t i, n;
-
- cls = PyObject_GetAttrString(obj, "__class__");
- if (cls == NULL)
- return NULL;
-
- getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
- if (getnewargs != NULL) {
- args = PyObject_CallObject(getnewargs, NULL);
- Py_DECREF(getnewargs);
- if (args != NULL && !PyTuple_Check(args)) {
- PyErr_Format(PyExc_TypeError,
- "__getnewargs__ should return a tuple, "
- "not '%.200s'", Py_TYPE(args)->tp_name);
- goto end;
- }
- }
- else {
- PyErr_Clear();
- args = PyTuple_New(0);
- }
- if (args == NULL)
- goto end;
-
- getstate = PyObject_GetAttrString(obj, "__getstate__");
- if (getstate != NULL) {
- state = PyObject_CallObject(getstate, NULL);
- Py_DECREF(getstate);
- if (state == NULL)
- goto end;
- }
- else {
- PyErr_Clear();
- state = PyObject_GetAttrString(obj, "__dict__");
- if (state == NULL) {
- PyErr_Clear();
- state = Py_None;
- Py_INCREF(state);
- }
- names = slotnames(cls);
- if (names == NULL)
- goto end;
- if (names != Py_None) {
- assert(PyList_Check(names));
- slots = PyDict_New();
- if (slots == NULL)
- goto end;
- n = 0;
- /* Can't pre-compute the list size; the list
- is stored on the class so accessible to other
- threads, which may be run by DECREF */
- for (i = 0; i < PyList_GET_SIZE(names); i++) {
- PyObject *name, *value;
- name = PyList_GET_ITEM(names, i);
- value = PyObject_GetAttr(obj, name);
- if (value == NULL)
- PyErr_Clear();
- else {
- int err = PyDict_SetItem(slots, name,
- value);
- Py_DECREF(value);
- if (err)
- goto end;
- n++;
- }
- }
- if (n) {
- state = Py_BuildValue("(NO)", state, slots);
- if (state == NULL)
- goto end;
- }
- }
- }
-
- if (!PyList_Check(obj)) {
- listitems = Py_None;
- Py_INCREF(listitems);
- }
- else {
- listitems = PyObject_GetIter(obj);
- if (listitems == NULL)
- goto end;
- }
-
- if (!PyDict_Check(obj)) {
- dictitems = Py_None;
- Py_INCREF(dictitems);
- }
- else {
- PyObject *items = PyObject_CallMethod(obj, "items", "");
- if (items == NULL)
- goto end;
- dictitems = PyObject_GetIter(items);
- Py_DECREF(items);
- if (dictitems == NULL)
- goto end;
- }
-
- copyreg = import_copyreg();
- if (copyreg == NULL)
- goto end;
- newobj = PyObject_GetAttrString(copyreg, "__newobj__");
- if (newobj == NULL)
- goto end;
-
- n = PyTuple_GET_SIZE(args);
- args2 = PyTuple_New(n+1);
- if (args2 == NULL)
- goto end;
- PyTuple_SET_ITEM(args2, 0, cls);
- cls = NULL;
- for (i = 0; i < n; i++) {
- PyObject *v = PyTuple_GET_ITEM(args, i);
- Py_INCREF(v);
- PyTuple_SET_ITEM(args2, i+1, v);
- }
-
- res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
+ PyObject *cls, *getnewargs;
+ PyObject *args = NULL, *args2 = NULL;
+ PyObject *getstate = NULL, *state = NULL, *names = NULL;
+ PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
+ PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
+ Py_ssize_t i, n;
+
+ cls = PyObject_GetAttrString(obj, "__class__");
+ if (cls == NULL)
+ return NULL;
+
+ getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
+ if (getnewargs != NULL) {
+ args = PyObject_CallObject(getnewargs, NULL);
+ Py_DECREF(getnewargs);
+ if (args != NULL && !PyTuple_Check(args)) {
+ PyErr_Format(PyExc_TypeError,
+ "__getnewargs__ should return a tuple, "
+ "not '%.200s'", Py_TYPE(args)->tp_name);
+ goto end;
+ }
+ }
+ else {
+ PyErr_Clear();
+ args = PyTuple_New(0);
+ }
+ if (args == NULL)
+ goto end;
+
+ getstate = PyObject_GetAttrString(obj, "__getstate__");
+ if (getstate != NULL) {
+ state = PyObject_CallObject(getstate, NULL);
+ Py_DECREF(getstate);
+ if (state == NULL)
+ goto end;
+ }
+ else {
+ PyErr_Clear();
+ state = PyObject_GetAttrString(obj, "__dict__");
+ if (state == NULL) {
+ PyErr_Clear();
+ state = Py_None;
+ Py_INCREF(state);
+ }
+ names = slotnames(cls);
+ if (names == NULL)
+ goto end;
+ if (names != Py_None) {
+ assert(PyList_Check(names));
+ slots = PyDict_New();
+ if (slots == NULL)
+ goto end;
+ n = 0;
+ /* Can't pre-compute the list size; the list
+ is stored on the class so accessible to other
+ threads, which may be run by DECREF */
+ for (i = 0; i < PyList_GET_SIZE(names); i++) {
+ PyObject *name, *value;
+ name = PyList_GET_ITEM(names, i);
+ value = PyObject_GetAttr(obj, name);
+ if (value == NULL)
+ PyErr_Clear();
+ else {
+ int err = PyDict_SetItem(slots, name,
+ value);
+ Py_DECREF(value);
+ if (err)
+ goto end;
+ n++;
+ }
+ }
+ if (n) {
+ state = Py_BuildValue("(NO)", state, slots);
+ if (state == NULL)
+ goto end;
+ }
+ }
+ }
+
+ if (!PyList_Check(obj)) {
+ listitems = Py_None;
+ Py_INCREF(listitems);
+ }
+ else {
+ listitems = PyObject_GetIter(obj);
+ if (listitems == NULL)
+ goto end;
+ }
+
+ if (!PyDict_Check(obj)) {
+ dictitems = Py_None;
+ Py_INCREF(dictitems);
+ }
+ else {
+ PyObject *items = PyObject_CallMethod(obj, "items", "");
+ if (items == NULL)
+ goto end;
+ dictitems = PyObject_GetIter(items);
+ Py_DECREF(items);
+ if (dictitems == NULL)
+ goto end;
+ }
+
+ copyreg = import_copyreg();
+ if (copyreg == NULL)
+ goto end;
+ newobj = PyObject_GetAttrString(copyreg, "__newobj__");
+ if (newobj == NULL)
+ goto end;
+
+ n = PyTuple_GET_SIZE(args);
+ args2 = PyTuple_New(n+1);
+ if (args2 == NULL)
+ goto end;
+ PyTuple_SET_ITEM(args2, 0, cls);
+ cls = NULL;
+ for (i = 0; i < n; i++) {
+ PyObject *v = PyTuple_GET_ITEM(args, i);
+ Py_INCREF(v);
+ PyTuple_SET_ITEM(args2, i+1, v);
+ }
+
+ res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
end:
- Py_XDECREF(cls);
- Py_XDECREF(args);
- Py_XDECREF(args2);
- Py_XDECREF(slots);
- Py_XDECREF(state);
- Py_XDECREF(names);
- Py_XDECREF(listitems);
- Py_XDECREF(dictitems);
- Py_XDECREF(copyreg);
- Py_XDECREF(newobj);
- return res;
+ Py_XDECREF(cls);
+ Py_XDECREF(args);
+ Py_XDECREF(args2);
+ Py_XDECREF(slots);
+ Py_XDECREF(state);
+ Py_XDECREF(names);
+ Py_XDECREF(listitems);
+ Py_XDECREF(dictitems);
+ Py_XDECREF(copyreg);
+ Py_XDECREF(newobj);
+ return res;
}
/*
@@ -3204,79 +3204,79 @@ reduce_2(PyObject *obj)
static PyObject *
_common_reduce(PyObject *self, int proto)
{
- PyObject *copyreg, *res;
+ PyObject *copyreg, *res;
- if (proto >= 2)
- return reduce_2(self);
+ if (proto >= 2)
+ return reduce_2(self);
- copyreg = import_copyreg();
- if (!copyreg)
- return NULL;
+ copyreg = import_copyreg();
+ if (!copyreg)
+ return NULL;
- res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
- Py_DECREF(copyreg);
+ res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
+ Py_DECREF(copyreg);
- return res;
+ return res;
}
static PyObject *
object_reduce(PyObject *self, PyObject *args)
{
- int proto = 0;
+ int proto = 0;
- if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
- return NULL;
+ if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
+ return NULL;
- return _common_reduce(self, proto);
+ return _common_reduce(self, proto);
}
static PyObject *
object_reduce_ex(PyObject *self, PyObject *args)
{
- PyObject *reduce, *res;
- int proto = 0;
-
- if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
- return NULL;
-
- reduce = PyObject_GetAttrString(self, "__reduce__");
- if (reduce == NULL)
- PyErr_Clear();
- else {
- PyObject *cls, *clsreduce, *objreduce;
- int override;
- cls = PyObject_GetAttrString(self, "__class__");
- if (cls == NULL) {
- Py_DECREF(reduce);
- return NULL;
- }
- clsreduce = PyObject_GetAttrString(cls, "__reduce__");
- Py_DECREF(cls);
- if (clsreduce == NULL) {
- Py_DECREF(reduce);
- return NULL;
- }
- objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
- "__reduce__");
- override = (clsreduce != objreduce);
- Py_DECREF(clsreduce);
- if (override) {
- res = PyObject_CallObject(reduce, NULL);
- Py_DECREF(reduce);
- return res;
- }
- else
- Py_DECREF(reduce);
- }
-
- return _common_reduce(self, proto);
+ PyObject *reduce, *res;
+ int proto = 0;
+
+ if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
+ return NULL;
+
+ reduce = PyObject_GetAttrString(self, "__reduce__");
+ if (reduce == NULL)
+ PyErr_Clear();
+ else {
+ PyObject *cls, *clsreduce, *objreduce;
+ int override;
+ cls = PyObject_GetAttrString(self, "__class__");
+ if (cls == NULL) {
+ Py_DECREF(reduce);
+ return NULL;
+ }
+ clsreduce = PyObject_GetAttrString(cls, "__reduce__");
+ Py_DECREF(cls);
+ if (clsreduce == NULL) {
+ Py_DECREF(reduce);
+ return NULL;
+ }
+ objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
+ "__reduce__");
+ override = (clsreduce != objreduce);
+ Py_DECREF(clsreduce);
+ if (override) {
+ res = PyObject_CallObject(reduce, NULL);
+ Py_DECREF(reduce);
+ return res;
+ }
+ else
+ Py_DECREF(reduce);
+ }
+
+ return _common_reduce(self, proto);
}
static PyObject *
object_subclasshook(PyObject *cls, PyObject *args)
{
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
PyDoc_STRVAR(object_subclasshook_doc,
@@ -3292,104 +3292,104 @@ PyDoc_STRVAR(object_subclasshook_doc,
class object:
def __format__(self, format_spec):
- return format(str(self), format_spec)
+ return format(str(self), format_spec)
*/
static PyObject *
object_format(PyObject *self, PyObject *args)
{
- PyObject *format_spec;
- PyObject *self_as_str = NULL;
- PyObject *result = NULL;
- PyObject *format_meth = NULL;
-
- if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
-
- self_as_str = PyObject_Str(self);
- if (self_as_str != NULL) {
- /* find the format function */
- format_meth = PyObject_GetAttrString(self_as_str, "__format__");
- if (format_meth != NULL) {
- /* and call it */
- result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
- }
+ PyObject *format_spec;
+ PyObject *self_as_str = NULL;
+ PyObject *result = NULL;
+ PyObject *format_meth = NULL;
+
+ if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
+ return NULL;
+
+ self_as_str = PyObject_Str(self);
+ if (self_as_str != NULL) {
+ /* find the format function */
+ format_meth = PyObject_GetAttrString(self_as_str, "__format__");
+ if (format_meth != NULL) {
+ /* and call it */
+ result = PyObject_CallFunctionObjArgs(format_meth, format_spec, NULL);
}
+ }
- Py_XDECREF(self_as_str);
- Py_XDECREF(format_meth);
+ Py_XDECREF(self_as_str);
+ Py_XDECREF(format_meth);
- return result;
+ return result;
}
static PyObject *
object_sizeof(PyObject *self, PyObject *args)
{
- Py_ssize_t res, isize;
+ Py_ssize_t res, isize;
- res = 0;
- isize = self->ob_type->tp_itemsize;
- if (isize > 0)
- res = Py_SIZE(self->ob_type) * isize;
- res += self->ob_type->tp_basicsize;
+ res = 0;
+ isize = self->ob_type->tp_itemsize;
+ if (isize > 0)
+ res = Py_SIZE(self->ob_type) * isize;
+ res += self->ob_type->tp_basicsize;
- return PyLong_FromSsize_t(res);
+ return PyLong_FromSsize_t(res);
}
static PyMethodDef object_methods[] = {
- {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
- PyDoc_STR("helper for pickle")},
- {"__reduce__", object_reduce, METH_VARARGS,
- PyDoc_STR("helper for pickle")},
- {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
- object_subclasshook_doc},
- {"__format__", object_format, METH_VARARGS,
- PyDoc_STR("default object formatter")},
- {"__sizeof__", object_sizeof, METH_NOARGS,
- PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
- {0}
+ {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
+ PyDoc_STR("helper for pickle")},
+ {"__reduce__", object_reduce, METH_VARARGS,
+ PyDoc_STR("helper for pickle")},
+ {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
+ object_subclasshook_doc},
+ {"__format__", object_format, METH_VARARGS,
+ PyDoc_STR("default object formatter")},
+ {"__sizeof__", object_sizeof, METH_NOARGS,
+ PyDoc_STR("__sizeof__() -> size of object in memory, in bytes")},
+ {0}
};
PyTypeObject PyBaseObject_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "object", /* tp_name */
- sizeof(PyObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- object_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- object_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc)_Py_HashPointer, /* tp_hash */
- 0, /* tp_call */
- object_str, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- PyObject_GenericSetAttr, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
- PyDoc_STR("The most base type"), /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- object_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- object_methods, /* tp_methods */
- 0, /* tp_members */
- object_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- object_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- object_new, /* tp_new */
- PyObject_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "object", /* tp_name */
+ sizeof(PyObject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ object_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ object_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ (hashfunc)_Py_HashPointer, /* tp_hash */
+ 0, /* tp_call */
+ object_str, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ PyObject_GenericSetAttr, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ PyDoc_STR("The most base type"), /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ object_richcompare, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ object_methods, /* tp_methods */
+ 0, /* tp_members */
+ object_getsets, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ object_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ object_new, /* tp_new */
+ PyObject_Del, /* tp_free */
};
@@ -3398,168 +3398,168 @@ PyTypeObject PyBaseObject_Type = {
static int
add_methods(PyTypeObject *type, PyMethodDef *meth)
{
- PyObject *dict = type->tp_dict;
-
- for (; meth->ml_name != NULL; meth++) {
- PyObject *descr;
- if (PyDict_GetItemString(dict, meth->ml_name) &&
- !(meth->ml_flags & METH_COEXIST))
- continue;
- if (meth->ml_flags & METH_CLASS) {
- if (meth->ml_flags & METH_STATIC) {
- PyErr_SetString(PyExc_ValueError,
- "method cannot be both class and static");
- return -1;
- }
- descr = PyDescr_NewClassMethod(type, meth);
- }
- else if (meth->ml_flags & METH_STATIC) {
- PyObject *cfunc = PyCFunction_New(meth, NULL);
- if (cfunc == NULL)
- return -1;
- descr = PyStaticMethod_New(cfunc);
- Py_DECREF(cfunc);
- }
- else {
- descr = PyDescr_NewMethod(type, meth);
- }
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ PyObject *dict = type->tp_dict;
+
+ for (; meth->ml_name != NULL; meth++) {
+ PyObject *descr;
+ if (PyDict_GetItemString(dict, meth->ml_name) &&
+ !(meth->ml_flags & METH_COEXIST))
+ continue;
+ if (meth->ml_flags & METH_CLASS) {
+ if (meth->ml_flags & METH_STATIC) {
+ PyErr_SetString(PyExc_ValueError,
+ "method cannot be both class and static");
+ return -1;
+ }
+ descr = PyDescr_NewClassMethod(type, meth);
+ }
+ else if (meth->ml_flags & METH_STATIC) {
+ PyObject *cfunc = PyCFunction_New(meth, NULL);
+ if (cfunc == NULL)
+ return -1;
+ descr = PyStaticMethod_New(cfunc);
+ Py_DECREF(cfunc);
+ }
+ else {
+ descr = PyDescr_NewMethod(type, meth);
+ }
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
static int
add_members(PyTypeObject *type, PyMemberDef *memb)
{
- PyObject *dict = type->tp_dict;
+ PyObject *dict = type->tp_dict;
- for (; memb->name != NULL; memb++) {
- PyObject *descr;
- if (PyDict_GetItemString(dict, memb->name))
- continue;
- descr = PyDescr_NewMember(type, memb);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict, memb->name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ for (; memb->name != NULL; memb++) {
+ PyObject *descr;
+ if (PyDict_GetItemString(dict, memb->name))
+ continue;
+ descr = PyDescr_NewMember(type, memb);
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, memb->name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
static int
add_getset(PyTypeObject *type, PyGetSetDef *gsp)
{
- PyObject *dict = type->tp_dict;
+ PyObject *dict = type->tp_dict;
- for (; gsp->name != NULL; gsp++) {
- PyObject *descr;
- if (PyDict_GetItemString(dict, gsp->name))
- continue;
- descr = PyDescr_NewGetSet(type, gsp);
+ for (; gsp->name != NULL; gsp++) {
+ PyObject *descr;
+ if (PyDict_GetItemString(dict, gsp->name))
+ continue;
+ descr = PyDescr_NewGetSet(type, gsp);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- return 0;
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ return 0;
}
static void
inherit_special(PyTypeObject *type, PyTypeObject *base)
{
- Py_ssize_t oldsize, newsize;
-
- /* Copying basicsize is connected to the GC flags */
- oldsize = base->tp_basicsize;
- newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
- if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
- (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
- (!type->tp_traverse && !type->tp_clear)) {
- type->tp_flags |= Py_TPFLAGS_HAVE_GC;
- if (type->tp_traverse == NULL)
- type->tp_traverse = base->tp_traverse;
- if (type->tp_clear == NULL)
- type->tp_clear = base->tp_clear;
- }
- {
- /* The condition below could use some explanation.
- It appears that tp_new is not inherited for static types
- whose base class is 'object'; this seems to be a precaution
- so that old extension types don't suddenly become
- callable (object.__new__ wouldn't insure the invariants
- that the extension type's own factory function ensures).
- Heap types, of course, are under our control, so they do
- inherit tp_new; static extension types that specify some
- other built-in type as the default are considered
- new-style-aware so they also inherit object.__new__. */
- if (base != &PyBaseObject_Type ||
- (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
- if (type->tp_new == NULL)
- type->tp_new = base->tp_new;
- }
- }
- type->tp_basicsize = newsize;
-
- /* Copy other non-function slots */
+ Py_ssize_t oldsize, newsize;
+
+ /* Copying basicsize is connected to the GC flags */
+ oldsize = base->tp_basicsize;
+ newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
+ if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
+ (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
+ (!type->tp_traverse && !type->tp_clear)) {
+ type->tp_flags |= Py_TPFLAGS_HAVE_GC;
+ if (type->tp_traverse == NULL)
+ type->tp_traverse = base->tp_traverse;
+ if (type->tp_clear == NULL)
+ type->tp_clear = base->tp_clear;
+ }
+ {
+ /* The condition below could use some explanation.
+ It appears that tp_new is not inherited for static types
+ whose base class is 'object'; this seems to be a precaution
+ so that old extension types don't suddenly become
+ callable (object.__new__ wouldn't insure the invariants
+ that the extension type's own factory function ensures).
+ Heap types, of course, are under our control, so they do
+ inherit tp_new; static extension types that specify some
+ other built-in type as the default are considered
+ new-style-aware so they also inherit object.__new__. */
+ if (base != &PyBaseObject_Type ||
+ (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
+ if (type->tp_new == NULL)
+ type->tp_new = base->tp_new;
+ }
+ }
+ type->tp_basicsize = newsize;
+
+ /* Copy other non-function slots */
#undef COPYVAL
#define COPYVAL(SLOT) \
- if (type->SLOT == 0) type->SLOT = base->SLOT
-
- COPYVAL(tp_itemsize);
- COPYVAL(tp_weaklistoffset);
- COPYVAL(tp_dictoffset);
-
- /* Setup fast subclass flags */
- if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
- type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyType_Type))
- type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyLong_Type))
- type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyBytes_Type))
- type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyUnicode_Type))
- type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyTuple_Type))
- type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyList_Type))
- type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
- else if (PyType_IsSubtype(base, &PyDict_Type))
- type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
+ if (type->SLOT == 0) type->SLOT = base->SLOT
+
+ COPYVAL(tp_itemsize);
+ COPYVAL(tp_weaklistoffset);
+ COPYVAL(tp_dictoffset);
+
+ /* Setup fast subclass flags */
+ if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
+ type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyType_Type))
+ type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyLong_Type))
+ type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyBytes_Type))
+ type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyUnicode_Type))
+ type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyTuple_Type))
+ type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyList_Type))
+ type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
+ else if (PyType_IsSubtype(base, &PyDict_Type))
+ type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
}
static char *hash_name_op[] = {
- "__eq__",
- "__hash__",
- NULL
+ "__eq__",
+ "__hash__",
+ NULL
};
static int
overrides_hash(PyTypeObject *type)
{
- char **p;
- PyObject *dict = type->tp_dict;
+ char **p;
+ PyObject *dict = type->tp_dict;
- assert(dict != NULL);
- for (p = hash_name_op; *p; p++) {
- if (PyDict_GetItemString(dict, *p) != NULL)
- return 1;
- }
- return 0;
+ assert(dict != NULL);
+ for (p = hash_name_op; *p; p++) {
+ if (PyDict_GetItemString(dict, *p) != NULL)
+ return 1;
+ }
+ return 0;
}
static void
inherit_slots(PyTypeObject *type, PyTypeObject *base)
{
- PyTypeObject *basebase;
+ PyTypeObject *basebase;
#undef SLOTDEFINED
#undef COPYSLOT
@@ -3569,147 +3569,147 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
#undef COPYBUF
#define SLOTDEFINED(SLOT) \
- (base->SLOT != 0 && \
- (basebase == NULL || base->SLOT != basebase->SLOT))
+ (base->SLOT != 0 && \
+ (basebase == NULL || base->SLOT != basebase->SLOT))
#define COPYSLOT(SLOT) \
- if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
+ if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
- /* This won't inherit indirect slots (from tp_as_number etc.)
- if type doesn't provide the space. */
-
- if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
- basebase = base->tp_base;
- if (basebase->tp_as_number == NULL)
- basebase = NULL;
- COPYNUM(nb_add);
- COPYNUM(nb_subtract);
- COPYNUM(nb_multiply);
- COPYNUM(nb_remainder);
- COPYNUM(nb_divmod);
- COPYNUM(nb_power);
- COPYNUM(nb_negative);
- COPYNUM(nb_positive);
- COPYNUM(nb_absolute);
- COPYNUM(nb_bool);
- COPYNUM(nb_invert);
- COPYNUM(nb_lshift);
- COPYNUM(nb_rshift);
- COPYNUM(nb_and);
- COPYNUM(nb_xor);
- COPYNUM(nb_or);
- COPYNUM(nb_int);
- COPYNUM(nb_float);
- COPYNUM(nb_inplace_add);
- COPYNUM(nb_inplace_subtract);
- COPYNUM(nb_inplace_multiply);
- COPYNUM(nb_inplace_remainder);
- COPYNUM(nb_inplace_power);
- COPYNUM(nb_inplace_lshift);
- COPYNUM(nb_inplace_rshift);
- COPYNUM(nb_inplace_and);
- COPYNUM(nb_inplace_xor);
- COPYNUM(nb_inplace_or);
- COPYNUM(nb_true_divide);
- COPYNUM(nb_floor_divide);
- COPYNUM(nb_inplace_true_divide);
- COPYNUM(nb_inplace_floor_divide);
- COPYNUM(nb_index);
- }
-
- if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
- basebase = base->tp_base;
- if (basebase->tp_as_sequence == NULL)
- basebase = NULL;
- COPYSEQ(sq_length);
- COPYSEQ(sq_concat);
- COPYSEQ(sq_repeat);
- COPYSEQ(sq_item);
- COPYSEQ(sq_ass_item);
- COPYSEQ(sq_contains);
- COPYSEQ(sq_inplace_concat);
- COPYSEQ(sq_inplace_repeat);
- }
-
- if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
- basebase = base->tp_base;
- if (basebase->tp_as_mapping == NULL)
- basebase = NULL;
- COPYMAP(mp_length);
- COPYMAP(mp_subscript);
- COPYMAP(mp_ass_subscript);
- }
-
- if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
- basebase = base->tp_base;
- if (basebase->tp_as_buffer == NULL)
- basebase = NULL;
- COPYBUF(bf_getbuffer);
- COPYBUF(bf_releasebuffer);
- }
-
- basebase = base->tp_base;
-
- COPYSLOT(tp_dealloc);
- if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
- type->tp_getattr = base->tp_getattr;
- type->tp_getattro = base->tp_getattro;
- }
- if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
- type->tp_setattr = base->tp_setattr;
- type->tp_setattro = base->tp_setattro;
- }
- /* tp_reserved is ignored */
- COPYSLOT(tp_repr);
- /* tp_hash see tp_richcompare */
- COPYSLOT(tp_call);
- COPYSLOT(tp_str);
- {
- /* Copy comparison-related slots only when
- not overriding them anywhere */
- if (type->tp_richcompare == NULL &&
- type->tp_hash == NULL &&
- !overrides_hash(type))
- {
- type->tp_richcompare = base->tp_richcompare;
- type->tp_hash = base->tp_hash;
- }
- }
- {
- COPYSLOT(tp_iter);
- COPYSLOT(tp_iternext);
- }
- {
- COPYSLOT(tp_descr_get);
- COPYSLOT(tp_descr_set);
- COPYSLOT(tp_dictoffset);
- COPYSLOT(tp_init);
- COPYSLOT(tp_alloc);
- COPYSLOT(tp_is_gc);
- if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
- (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
- /* They agree about gc. */
- COPYSLOT(tp_free);
- }
- else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
- type->tp_free == NULL &&
- base->tp_free == PyObject_Free) {
- /* A bit of magic to plug in the correct default
- * tp_free function when a derived class adds gc,
- * didn't define tp_free, and the base uses the
- * default non-gc tp_free.
- */
- type->tp_free = PyObject_GC_Del;
- }
- /* else they didn't agree about gc, and there isn't something
- * obvious to be done -- the type is on its own.
- */
- }
+ /* This won't inherit indirect slots (from tp_as_number etc.)
+ if type doesn't provide the space. */
+
+ if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
+ basebase = base->tp_base;
+ if (basebase->tp_as_number == NULL)
+ basebase = NULL;
+ COPYNUM(nb_add);
+ COPYNUM(nb_subtract);
+ COPYNUM(nb_multiply);
+ COPYNUM(nb_remainder);
+ COPYNUM(nb_divmod);
+ COPYNUM(nb_power);
+ COPYNUM(nb_negative);
+ COPYNUM(nb_positive);
+ COPYNUM(nb_absolute);
+ COPYNUM(nb_bool);
+ COPYNUM(nb_invert);
+ COPYNUM(nb_lshift);
+ COPYNUM(nb_rshift);
+ COPYNUM(nb_and);
+ COPYNUM(nb_xor);
+ COPYNUM(nb_or);
+ COPYNUM(nb_int);
+ COPYNUM(nb_float);
+ COPYNUM(nb_inplace_add);
+ COPYNUM(nb_inplace_subtract);
+ COPYNUM(nb_inplace_multiply);
+ COPYNUM(nb_inplace_remainder);
+ COPYNUM(nb_inplace_power);
+ COPYNUM(nb_inplace_lshift);
+ COPYNUM(nb_inplace_rshift);
+ COPYNUM(nb_inplace_and);
+ COPYNUM(nb_inplace_xor);
+ COPYNUM(nb_inplace_or);
+ COPYNUM(nb_true_divide);
+ COPYNUM(nb_floor_divide);
+ COPYNUM(nb_inplace_true_divide);
+ COPYNUM(nb_inplace_floor_divide);
+ COPYNUM(nb_index);
+ }
+
+ if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
+ basebase = base->tp_base;
+ if (basebase->tp_as_sequence == NULL)
+ basebase = NULL;
+ COPYSEQ(sq_length);
+ COPYSEQ(sq_concat);
+ COPYSEQ(sq_repeat);
+ COPYSEQ(sq_item);
+ COPYSEQ(sq_ass_item);
+ COPYSEQ(sq_contains);
+ COPYSEQ(sq_inplace_concat);
+ COPYSEQ(sq_inplace_repeat);
+ }
+
+ if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
+ basebase = base->tp_base;
+ if (basebase->tp_as_mapping == NULL)
+ basebase = NULL;
+ COPYMAP(mp_length);
+ COPYMAP(mp_subscript);
+ COPYMAP(mp_ass_subscript);
+ }
+
+ if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
+ basebase = base->tp_base;
+ if (basebase->tp_as_buffer == NULL)
+ basebase = NULL;
+ COPYBUF(bf_getbuffer);
+ COPYBUF(bf_releasebuffer);
+ }
+
+ basebase = base->tp_base;
+
+ COPYSLOT(tp_dealloc);
+ if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
+ type->tp_getattr = base->tp_getattr;
+ type->tp_getattro = base->tp_getattro;
+ }
+ if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
+ type->tp_setattr = base->tp_setattr;
+ type->tp_setattro = base->tp_setattro;
+ }
+ /* tp_reserved is ignored */
+ COPYSLOT(tp_repr);
+ /* tp_hash see tp_richcompare */
+ COPYSLOT(tp_call);
+ COPYSLOT(tp_str);
+ {
+ /* Copy comparison-related slots only when
+ not overriding them anywhere */
+ if (type->tp_richcompare == NULL &&
+ type->tp_hash == NULL &&
+ !overrides_hash(type))
+ {
+ type->tp_richcompare = base->tp_richcompare;
+ type->tp_hash = base->tp_hash;
+ }
+ }
+ {
+ COPYSLOT(tp_iter);
+ COPYSLOT(tp_iternext);
+ }
+ {
+ COPYSLOT(tp_descr_get);
+ COPYSLOT(tp_descr_set);
+ COPYSLOT(tp_dictoffset);
+ COPYSLOT(tp_init);
+ COPYSLOT(tp_alloc);
+ COPYSLOT(tp_is_gc);
+ if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
+ (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
+ /* They agree about gc. */
+ COPYSLOT(tp_free);
+ }
+ else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
+ type->tp_free == NULL &&
+ base->tp_free == PyObject_Free) {
+ /* A bit of magic to plug in the correct default
+ * tp_free function when a derived class adds gc,
+ * didn't define tp_free, and the base uses the
+ * default non-gc tp_free.
+ */
+ type->tp_free = PyObject_GC_Del;
+ }
+ /* else they didn't agree about gc, and there isn't something
+ * obvious to be done -- the type is on its own.
+ */
+ }
}
static int add_operators(PyTypeObject *);
@@ -3717,273 +3717,273 @@ static int add_operators(PyTypeObject *);
int
PyType_Ready(PyTypeObject *type)
{
- PyObject *dict, *bases;
- PyTypeObject *base;
- Py_ssize_t i, n;
+ PyObject *dict, *bases;
+ PyTypeObject *base;
+ Py_ssize_t i, n;
- if (type->tp_flags & Py_TPFLAGS_READY) {
- assert(type->tp_dict != NULL);
- return 0;
- }
- assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
+ if (type->tp_flags & Py_TPFLAGS_READY) {
+ assert(type->tp_dict != NULL);
+ return 0;
+ }
+ assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
- type->tp_flags |= Py_TPFLAGS_READYING;
+ type->tp_flags |= Py_TPFLAGS_READYING;
#ifdef Py_TRACE_REFS
- /* PyType_Ready is the closest thing we have to a choke point
- * for type objects, so is the best place I can think of to try
- * to get type objects into the doubly-linked list of all objects.
- * Still, not all type objects go thru PyType_Ready.
- */
- _Py_AddToAllObjects((PyObject *)type, 0);
+ /* PyType_Ready is the closest thing we have to a choke point
+ * for type objects, so is the best place I can think of to try
+ * to get type objects into the doubly-linked list of all objects.
+ * Still, not all type objects go thru PyType_Ready.
+ */
+ _Py_AddToAllObjects((PyObject *)type, 0);
#endif
- /* Initialize tp_base (defaults to BaseObject unless that's us) */
- base = type->tp_base;
- if (base == NULL && type != &PyBaseObject_Type) {
- base = type->tp_base = &PyBaseObject_Type;
- Py_INCREF(base);
- }
-
- /* Now the only way base can still be NULL is if type is
- * &PyBaseObject_Type.
- */
-
- /* Initialize the base class */
- if (base != NULL && base->tp_dict == NULL) {
- if (PyType_Ready(base) < 0)
- goto error;
- }
-
- /* Initialize ob_type if NULL. This means extensions that want to be
- compilable separately on Windows can call PyType_Ready() instead of
- initializing the ob_type field of their type objects. */
- /* The test for base != NULL is really unnecessary, since base is only
- NULL when type is &PyBaseObject_Type, and we know its ob_type is
- not NULL (it's initialized to &PyType_Type). But coverity doesn't
- know that. */
- if (Py_TYPE(type) == NULL && base != NULL)
- Py_TYPE(type) = Py_TYPE(base);
-
- /* Initialize tp_bases */
- bases = type->tp_bases;
- if (bases == NULL) {
- if (base == NULL)
- bases = PyTuple_New(0);
- else
- bases = PyTuple_Pack(1, base);
- if (bases == NULL)
- goto error;
- type->tp_bases = bases;
- }
-
- /* Initialize tp_dict */
- dict = type->tp_dict;
- if (dict == NULL) {
- dict = PyDict_New();
- if (dict == NULL)
- goto error;
- type->tp_dict = dict;
- }
-
- /* Add type-specific descriptors to tp_dict */
- if (add_operators(type) < 0)
- goto error;
- if (type->tp_methods != NULL) {
- if (add_methods(type, type->tp_methods) < 0)
- goto error;
- }
- if (type->tp_members != NULL) {
- if (add_members(type, type->tp_members) < 0)
- goto error;
- }
- if (type->tp_getset != NULL) {
- if (add_getset(type, type->tp_getset) < 0)
- goto error;
- }
-
- /* Calculate method resolution order */
- if (mro_internal(type) < 0) {
- goto error;
- }
-
- /* Inherit special flags from dominant base */
- if (type->tp_base != NULL)
- inherit_special(type, type->tp_base);
-
- /* Initialize tp_dict properly */
- bases = type->tp_mro;
- assert(bases != NULL);
- assert(PyTuple_Check(bases));
- n = PyTuple_GET_SIZE(bases);
- for (i = 1; i < n; i++) {
- PyObject *b = PyTuple_GET_ITEM(bases, i);
- if (PyType_Check(b))
- inherit_slots(type, (PyTypeObject *)b);
- }
-
- /* Sanity check for tp_free. */
- if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
- (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
- /* This base class needs to call tp_free, but doesn't have
- * one, or its tp_free is for non-gc'ed objects.
- */
- PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
- "gc and is a base type but has inappropriate "
- "tp_free slot",
- type->tp_name);
- goto error;
- }
-
- /* if the type dictionary doesn't contain a __doc__, set it from
- the tp_doc slot.
- */
- if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
- if (type->tp_doc != NULL) {
- PyObject *doc = PyUnicode_FromString(type->tp_doc);
- if (doc == NULL)
- goto error;
- PyDict_SetItemString(type->tp_dict, "__doc__", doc);
- Py_DECREF(doc);
- } else {
- PyDict_SetItemString(type->tp_dict,
- "__doc__", Py_None);
- }
- }
-
- /* Hack for tp_hash and __hash__.
- If after all that, tp_hash is still NULL, and __hash__ is not in
- tp_dict, set tp_hash to PyObject_HashNotImplemented and
- tp_dict['__hash__'] equal to None.
- This signals that __hash__ is not inherited.
- */
- if (type->tp_hash == NULL) {
- if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
- if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
- goto error;
- type->tp_hash = PyObject_HashNotImplemented;
- }
- }
-
- /* Some more special stuff */
- base = type->tp_base;
- if (base != NULL) {
- if (type->tp_as_number == NULL)
- type->tp_as_number = base->tp_as_number;
- if (type->tp_as_sequence == NULL)
- type->tp_as_sequence = base->tp_as_sequence;
- if (type->tp_as_mapping == NULL)
- type->tp_as_mapping = base->tp_as_mapping;
- if (type->tp_as_buffer == NULL)
- type->tp_as_buffer = base->tp_as_buffer;
- }
-
- /* Link into each base class's list of subclasses */
- bases = type->tp_bases;
- n = PyTuple_GET_SIZE(bases);
- for (i = 0; i < n; i++) {
- PyObject *b = PyTuple_GET_ITEM(bases, i);
- if (PyType_Check(b) &&
- add_subclass((PyTypeObject *)b, type) < 0)
- goto error;
- }
-
- /* Warn for a type that implements tp_compare (now known as
- tp_reserved) but not tp_richcompare. */
- if (type->tp_reserved && !type->tp_richcompare) {
- int error;
- char msg[240];
- PyOS_snprintf(msg, sizeof(msg),
- "Type %.100s defines tp_reserved (formerly "
- "tp_compare) but not tp_richcompare. "
- "Comparisons may not behave as intended.",
- type->tp_name);
- error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1);
- if (error == -1)
- goto error;
- }
-
- /* All done -- set the ready flag */
- assert(type->tp_dict != NULL);
- type->tp_flags =
- (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
- return 0;
+ /* Initialize tp_base (defaults to BaseObject unless that's us) */
+ base = type->tp_base;
+ if (base == NULL && type != &PyBaseObject_Type) {
+ base = type->tp_base = &PyBaseObject_Type;
+ Py_INCREF(base);
+ }
+
+ /* Now the only way base can still be NULL is if type is
+ * &PyBaseObject_Type.
+ */
+
+ /* Initialize the base class */
+ if (base != NULL && base->tp_dict == NULL) {
+ if (PyType_Ready(base) < 0)
+ goto error;
+ }
+
+ /* Initialize ob_type if NULL. This means extensions that want to be
+ compilable separately on Windows can call PyType_Ready() instead of
+ initializing the ob_type field of their type objects. */
+ /* The test for base != NULL is really unnecessary, since base is only
+ NULL when type is &PyBaseObject_Type, and we know its ob_type is
+ not NULL (it's initialized to &PyType_Type). But coverity doesn't
+ know that. */
+ if (Py_TYPE(type) == NULL && base != NULL)
+ Py_TYPE(type) = Py_TYPE(base);
+
+ /* Initialize tp_bases */
+ bases = type->tp_bases;
+ if (bases == NULL) {
+ if (base == NULL)
+ bases = PyTuple_New(0);
+ else
+ bases = PyTuple_Pack(1, base);
+ if (bases == NULL)
+ goto error;
+ type->tp_bases = bases;
+ }
+
+ /* Initialize tp_dict */
+ dict = type->tp_dict;
+ if (dict == NULL) {
+ dict = PyDict_New();
+ if (dict == NULL)
+ goto error;
+ type->tp_dict = dict;
+ }
+
+ /* Add type-specific descriptors to tp_dict */
+ if (add_operators(type) < 0)
+ goto error;
+ if (type->tp_methods != NULL) {
+ if (add_methods(type, type->tp_methods) < 0)
+ goto error;
+ }
+ if (type->tp_members != NULL) {
+ if (add_members(type, type->tp_members) < 0)
+ goto error;
+ }
+ if (type->tp_getset != NULL) {
+ if (add_getset(type, type->tp_getset) < 0)
+ goto error;
+ }
+
+ /* Calculate method resolution order */
+ if (mro_internal(type) < 0) {
+ goto error;
+ }
+
+ /* Inherit special flags from dominant base */
+ if (type->tp_base != NULL)
+ inherit_special(type, type->tp_base);
+
+ /* Initialize tp_dict properly */
+ bases = type->tp_mro;
+ assert(bases != NULL);
+ assert(PyTuple_Check(bases));
+ n = PyTuple_GET_SIZE(bases);
+ for (i = 1; i < n; i++) {
+ PyObject *b = PyTuple_GET_ITEM(bases, i);
+ if (PyType_Check(b))
+ inherit_slots(type, (PyTypeObject *)b);
+ }
+
+ /* Sanity check for tp_free. */
+ if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
+ (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
+ /* This base class needs to call tp_free, but doesn't have
+ * one, or its tp_free is for non-gc'ed objects.
+ */
+ PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
+ "gc and is a base type but has inappropriate "
+ "tp_free slot",
+ type->tp_name);
+ goto error;
+ }
+
+ /* if the type dictionary doesn't contain a __doc__, set it from
+ the tp_doc slot.
+ */
+ if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
+ if (type->tp_doc != NULL) {
+ PyObject *doc = PyUnicode_FromString(type->tp_doc);
+ if (doc == NULL)
+ goto error;
+ PyDict_SetItemString(type->tp_dict, "__doc__", doc);
+ Py_DECREF(doc);
+ } else {
+ PyDict_SetItemString(type->tp_dict,
+ "__doc__", Py_None);
+ }
+ }
+
+ /* Hack for tp_hash and __hash__.
+ If after all that, tp_hash is still NULL, and __hash__ is not in
+ tp_dict, set tp_hash to PyObject_HashNotImplemented and
+ tp_dict['__hash__'] equal to None.
+ This signals that __hash__ is not inherited.
+ */
+ if (type->tp_hash == NULL) {
+ if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
+ if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
+ goto error;
+ type->tp_hash = PyObject_HashNotImplemented;
+ }
+ }
+
+ /* Some more special stuff */
+ base = type->tp_base;
+ if (base != NULL) {
+ if (type->tp_as_number == NULL)
+ type->tp_as_number = base->tp_as_number;
+ if (type->tp_as_sequence == NULL)
+ type->tp_as_sequence = base->tp_as_sequence;
+ if (type->tp_as_mapping == NULL)
+ type->tp_as_mapping = base->tp_as_mapping;
+ if (type->tp_as_buffer == NULL)
+ type->tp_as_buffer = base->tp_as_buffer;
+ }
+
+ /* Link into each base class's list of subclasses */
+ bases = type->tp_bases;
+ n = PyTuple_GET_SIZE(bases);
+ for (i = 0; i < n; i++) {
+ PyObject *b = PyTuple_GET_ITEM(bases, i);
+ if (PyType_Check(b) &&
+ add_subclass((PyTypeObject *)b, type) < 0)
+ goto error;
+ }
+
+ /* Warn for a type that implements tp_compare (now known as
+ tp_reserved) but not tp_richcompare. */
+ if (type->tp_reserved && !type->tp_richcompare) {
+ int error;
+ char msg[240];
+ PyOS_snprintf(msg, sizeof(msg),
+ "Type %.100s defines tp_reserved (formerly "
+ "tp_compare) but not tp_richcompare. "
+ "Comparisons may not behave as intended.",
+ type->tp_name);
+ error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1);
+ if (error == -1)
+ goto error;
+ }
+
+ /* All done -- set the ready flag */
+ assert(type->tp_dict != NULL);
+ type->tp_flags =
+ (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
+ return 0;
error:
- type->tp_flags &= ~Py_TPFLAGS_READYING;
- return -1;
+ type->tp_flags &= ~Py_TPFLAGS_READYING;
+ return -1;
}
static int
add_subclass(PyTypeObject *base, PyTypeObject *type)
{
- Py_ssize_t i;
- int result;
- PyObject *list, *ref, *newobj;
-
- list = base->tp_subclasses;
- if (list == NULL) {
- base->tp_subclasses = list = PyList_New(0);
- if (list == NULL)
- return -1;
- }
- assert(PyList_Check(list));
- newobj = PyWeakref_NewRef((PyObject *)type, NULL);
- i = PyList_GET_SIZE(list);
- while (--i >= 0) {
- ref = PyList_GET_ITEM(list, i);
- assert(PyWeakref_CheckRef(ref));
- if (PyWeakref_GET_OBJECT(ref) == Py_None)
- return PyList_SetItem(list, i, newobj);
- }
- result = PyList_Append(list, newobj);
- Py_DECREF(newobj);
- return result;
+ Py_ssize_t i;
+ int result;
+ PyObject *list, *ref, *newobj;
+
+ list = base->tp_subclasses;
+ if (list == NULL) {
+ base->tp_subclasses = list = PyList_New(0);
+ if (list == NULL)
+ return -1;
+ }
+ assert(PyList_Check(list));
+ newobj = PyWeakref_NewRef((PyObject *)type, NULL);
+ i = PyList_GET_SIZE(list);
+ while (--i >= 0) {
+ ref = PyList_GET_ITEM(list, i);
+ assert(PyWeakref_CheckRef(ref));
+ if (PyWeakref_GET_OBJECT(ref) == Py_None)
+ return PyList_SetItem(list, i, newobj);
+ }
+ result = PyList_Append(list, newobj);
+ Py_DECREF(newobj);
+ return result;
}
static void
remove_subclass(PyTypeObject *base, PyTypeObject *type)
{
- Py_ssize_t i;
- PyObject *list, *ref;
-
- list = base->tp_subclasses;
- if (list == NULL) {
- return;
- }
- assert(PyList_Check(list));
- i = PyList_GET_SIZE(list);
- while (--i >= 0) {
- ref = PyList_GET_ITEM(list, i);
- assert(PyWeakref_CheckRef(ref));
- if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
- /* this can't fail, right? */
- PySequence_DelItem(list, i);
- return;
- }
- }
+ Py_ssize_t i;
+ PyObject *list, *ref;
+
+ list = base->tp_subclasses;
+ if (list == NULL) {
+ return;
+ }
+ assert(PyList_Check(list));
+ i = PyList_GET_SIZE(list);
+ while (--i >= 0) {
+ ref = PyList_GET_ITEM(list, i);
+ assert(PyWeakref_CheckRef(ref));
+ if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
+ /* this can't fail, right? */
+ PySequence_DelItem(list, i);
+ return;
+ }
+ }
}
static int
check_num_args(PyObject *ob, int n)
{
- if (!PyTuple_CheckExact(ob)) {
- PyErr_SetString(PyExc_SystemError,
- "PyArg_UnpackTuple() argument list is not a tuple");
- return 0;
- }
- if (n == PyTuple_GET_SIZE(ob))
- return 1;
- PyErr_Format(
- PyExc_TypeError,
- "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
- return 0;
+ if (!PyTuple_CheckExact(ob)) {
+ PyErr_SetString(PyExc_SystemError,
+ "PyArg_UnpackTuple() argument list is not a tuple");
+ return 0;
+ }
+ if (n == PyTuple_GET_SIZE(ob))
+ return 1;
+ PyErr_Format(
+ PyExc_TypeError,
+ "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
+ return 0;
}
/* Generic wrappers for overloadable 'operators' such as __getitem__ */
/* There's a wrapper *function* for each distinct function typedef used
- for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
+ for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
wrapper *table* for each distinct operation (e.g. __len__, __add__).
Most tables have only one entry; the tables for binary operators have two
entries, one regular and one with reversed arguments. */
@@ -3991,253 +3991,253 @@ check_num_args(PyObject *ob, int n)
static PyObject *
wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
{
- lenfunc func = (lenfunc)wrapped;
- Py_ssize_t res;
+ lenfunc func = (lenfunc)wrapped;
+ Py_ssize_t res;
- if (!check_num_args(args, 0))
- return NULL;
- res = (*func)(self);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- return PyLong_FromLong((long)res);
+ if (!check_num_args(args, 0))
+ return NULL;
+ res = (*func)(self);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ return PyLong_FromLong((long)res);
}
static PyObject *
wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
{
- inquiry func = (inquiry)wrapped;
- int res;
+ inquiry func = (inquiry)wrapped;
+ int res;
- if (!check_num_args(args, 0))
- return NULL;
- res = (*func)(self);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- return PyBool_FromLong((long)res);
+ if (!check_num_args(args, 0))
+ return NULL;
+ res = (*func)(self);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ return PyBool_FromLong((long)res);
}
static PyObject *
wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
{
- binaryfunc func = (binaryfunc)wrapped;
- PyObject *other;
+ binaryfunc func = (binaryfunc)wrapped;
+ PyObject *other;
- if (!check_num_args(args, 1))
- return NULL;
- other = PyTuple_GET_ITEM(args, 0);
- return (*func)(self, other);
+ if (!check_num_args(args, 1))
+ return NULL;
+ other = PyTuple_GET_ITEM(args, 0);
+ return (*func)(self, other);
}
static PyObject *
wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
{
- binaryfunc func = (binaryfunc)wrapped;
- PyObject *other;
+ binaryfunc func = (binaryfunc)wrapped;
+ PyObject *other;
- if (!check_num_args(args, 1))
- return NULL;
- other = PyTuple_GET_ITEM(args, 0);
- return (*func)(self, other);
+ if (!check_num_args(args, 1))
+ return NULL;
+ other = PyTuple_GET_ITEM(args, 0);
+ return (*func)(self, other);
}
static PyObject *
wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
{
- binaryfunc func = (binaryfunc)wrapped;
- PyObject *other;
+ binaryfunc func = (binaryfunc)wrapped;
+ PyObject *other;
- if (!check_num_args(args, 1))
- return NULL;
- other = PyTuple_GET_ITEM(args, 0);
- if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- return (*func)(other, self);
+ if (!check_num_args(args, 1))
+ return NULL;
+ other = PyTuple_GET_ITEM(args, 0);
+ if (!PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ return (*func)(other, self);
}
static PyObject *
wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
{
- ternaryfunc func = (ternaryfunc)wrapped;
- PyObject *other;
- PyObject *third = Py_None;
+ ternaryfunc func = (ternaryfunc)wrapped;
+ PyObject *other;
+ PyObject *third = Py_None;
- /* Note: This wrapper only works for __pow__() */
+ /* Note: This wrapper only works for __pow__() */
- if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
- return NULL;
- return (*func)(self, other, third);
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
+ return NULL;
+ return (*func)(self, other, third);
}
static PyObject *
wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
{
- ternaryfunc func = (ternaryfunc)wrapped;
- PyObject *other;
- PyObject *third = Py_None;
+ ternaryfunc func = (ternaryfunc)wrapped;
+ PyObject *other;
+ PyObject *third = Py_None;
- /* Note: This wrapper only works for __pow__() */
+ /* Note: This wrapper only works for __pow__() */
- if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
- return NULL;
- return (*func)(other, self, third);
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
+ return NULL;
+ return (*func)(other, self, third);
}
static PyObject *
wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
{
- unaryfunc func = (unaryfunc)wrapped;
+ unaryfunc func = (unaryfunc)wrapped;
- if (!check_num_args(args, 0))
- return NULL;
- return (*func)(self);
+ if (!check_num_args(args, 0))
+ return NULL;
+ return (*func)(self);
}
static PyObject *
wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
{
- ssizeargfunc func = (ssizeargfunc)wrapped;
- PyObject* o;
- Py_ssize_t i;
+ ssizeargfunc func = (ssizeargfunc)wrapped;
+ PyObject* o;
+ Py_ssize_t i;
- if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
- return NULL;
- i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- return (*func)(self, i);
+ if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
+ return NULL;
+ i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ return (*func)(self, i);
}
static Py_ssize_t
getindex(PyObject *self, PyObject *arg)
{
- Py_ssize_t i;
-
- i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
- if (i == -1 && PyErr_Occurred())
- return -1;
- if (i < 0) {
- PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
- if (sq && sq->sq_length) {
- Py_ssize_t n = (*sq->sq_length)(self);
- if (n < 0)
- return -1;
- i += n;
- }
- }
- return i;
+ Py_ssize_t i;
+
+ i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
+ if (i == -1 && PyErr_Occurred())
+ return -1;
+ if (i < 0) {
+ PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
+ if (sq && sq->sq_length) {
+ Py_ssize_t n = (*sq->sq_length)(self);
+ if (n < 0)
+ return -1;
+ i += n;
+ }
+ }
+ return i;
}
static PyObject *
wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
{
- ssizeargfunc func = (ssizeargfunc)wrapped;
- PyObject *arg;
- Py_ssize_t i;
+ ssizeargfunc func = (ssizeargfunc)wrapped;
+ PyObject *arg;
+ Py_ssize_t i;
- if (PyTuple_GET_SIZE(args) == 1) {
- arg = PyTuple_GET_ITEM(args, 0);
- i = getindex(self, arg);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- return (*func)(self, i);
- }
- check_num_args(args, 1);
- assert(PyErr_Occurred());
- return NULL;
+ if (PyTuple_GET_SIZE(args) == 1) {
+ arg = PyTuple_GET_ITEM(args, 0);
+ i = getindex(self, arg);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ return (*func)(self, i);
+ }
+ check_num_args(args, 1);
+ assert(PyErr_Occurred());
+ return NULL;
}
static PyObject *
wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
{
- ssizeobjargproc func = (ssizeobjargproc)wrapped;
- Py_ssize_t i;
- int res;
- PyObject *arg, *value;
+ ssizeobjargproc func = (ssizeobjargproc)wrapped;
+ Py_ssize_t i;
+ int res;
+ PyObject *arg, *value;
- if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
- return NULL;
- i = getindex(self, arg);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- res = (*func)(self, i, value);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
+ return NULL;
+ i = getindex(self, arg);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ res = (*func)(self, i, value);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
{
- ssizeobjargproc func = (ssizeobjargproc)wrapped;
- Py_ssize_t i;
- int res;
- PyObject *arg;
+ ssizeobjargproc func = (ssizeobjargproc)wrapped;
+ Py_ssize_t i;
+ int res;
+ PyObject *arg;
- if (!check_num_args(args, 1))
- return NULL;
- arg = PyTuple_GET_ITEM(args, 0);
- i = getindex(self, arg);
- if (i == -1 && PyErr_Occurred())
- return NULL;
- res = (*func)(self, i, NULL);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!check_num_args(args, 1))
+ return NULL;
+ arg = PyTuple_GET_ITEM(args, 0);
+ i = getindex(self, arg);
+ if (i == -1 && PyErr_Occurred())
+ return NULL;
+ res = (*func)(self, i, NULL);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* XXX objobjproc is a misnomer; should be objargpred */
static PyObject *
wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
{
- objobjproc func = (objobjproc)wrapped;
- int res;
- PyObject *value;
+ objobjproc func = (objobjproc)wrapped;
+ int res;
+ PyObject *value;
- if (!check_num_args(args, 1))
- return NULL;
- value = PyTuple_GET_ITEM(args, 0);
- res = (*func)(self, value);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- else
- return PyBool_FromLong(res);
+ if (!check_num_args(args, 1))
+ return NULL;
+ value = PyTuple_GET_ITEM(args, 0);
+ res = (*func)(self, value);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ else
+ return PyBool_FromLong(res);
}
static PyObject *
wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
{
- objobjargproc func = (objobjargproc)wrapped;
- int res;
- PyObject *key, *value;
+ objobjargproc func = (objobjargproc)wrapped;
+ int res;
+ PyObject *key, *value;
- if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
- return NULL;
- res = (*func)(self, key, value);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
+ return NULL;
+ res = (*func)(self, key, value);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
{
- objobjargproc func = (objobjargproc)wrapped;
- int res;
- PyObject *key;
+ objobjargproc func = (objobjargproc)wrapped;
+ int res;
+ PyObject *key;
- if (!check_num_args(args, 1))
- return NULL;
- key = PyTuple_GET_ITEM(args, 0);
- res = (*func)(self, key, NULL);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!check_num_args(args, 1))
+ return NULL;
+ key = PyTuple_GET_ITEM(args, 0);
+ res = (*func)(self, key, NULL);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
@@ -4245,90 +4245,90 @@ wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
static int
hackcheck(PyObject *self, setattrofunc func, char *what)
{
- PyTypeObject *type = Py_TYPE(self);
- while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
- type = type->tp_base;
- /* If type is NULL now, this is a really weird type.
- In the spirit of backwards compatibility (?), just shut up. */
- if (type && type->tp_setattro != func) {
- PyErr_Format(PyExc_TypeError,
- "can't apply this %s to %s object",
- what,
- type->tp_name);
- return 0;
- }
- return 1;
+ PyTypeObject *type = Py_TYPE(self);
+ while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
+ type = type->tp_base;
+ /* If type is NULL now, this is a really weird type.
+ In the spirit of backwards compatibility (?), just shut up. */
+ if (type && type->tp_setattro != func) {
+ PyErr_Format(PyExc_TypeError,
+ "can't apply this %s to %s object",
+ what,
+ type->tp_name);
+ return 0;
+ }
+ return 1;
}
static PyObject *
wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
{
- setattrofunc func = (setattrofunc)wrapped;
- int res;
- PyObject *name, *value;
+ setattrofunc func = (setattrofunc)wrapped;
+ int res;
+ PyObject *name, *value;
- if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
- return NULL;
- if (!hackcheck(self, func, "__setattr__"))
- return NULL;
- res = (*func)(self, name, value);
- if (res < 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
+ return NULL;
+ if (!hackcheck(self, func, "__setattr__"))
+ return NULL;
+ res = (*func)(self, name, value);
+ if (res < 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
{
- setattrofunc func = (setattrofunc)wrapped;
- int res;
- PyObject *name;
+ setattrofunc func = (setattrofunc)wrapped;
+ int res;
+ PyObject *name;
- if (!check_num_args(args, 1))
- return NULL;
- name = PyTuple_GET_ITEM(args, 0);
- if (!hackcheck(self, func, "__delattr__"))
- return NULL;
- res = (*func)(self, name, NULL);
- if (res < 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!check_num_args(args, 1))
+ return NULL;
+ name = PyTuple_GET_ITEM(args, 0);
+ if (!hackcheck(self, func, "__delattr__"))
+ return NULL;
+ res = (*func)(self, name, NULL);
+ if (res < 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
{
- hashfunc func = (hashfunc)wrapped;
- long res;
+ hashfunc func = (hashfunc)wrapped;
+ long res;
- if (!check_num_args(args, 0))
- return NULL;
- res = (*func)(self);
- if (res == -1 && PyErr_Occurred())
- return NULL;
- return PyLong_FromLong(res);
+ if (!check_num_args(args, 0))
+ return NULL;
+ res = (*func)(self);
+ if (res == -1 && PyErr_Occurred())
+ return NULL;
+ return PyLong_FromLong(res);
}
static PyObject *
wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
{
- ternaryfunc func = (ternaryfunc)wrapped;
+ ternaryfunc func = (ternaryfunc)wrapped;
- return (*func)(self, args, kwds);
+ return (*func)(self, args, kwds);
}
static PyObject *
wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
{
- richcmpfunc func = (richcmpfunc)wrapped;
- PyObject *other;
+ richcmpfunc func = (richcmpfunc)wrapped;
+ PyObject *other;
- if (!check_num_args(args, 1))
- return NULL;
- other = PyTuple_GET_ITEM(args, 0);
- return (*func)(self, other, op);
+ if (!check_num_args(args, 1))
+ return NULL;
+ other = PyTuple_GET_ITEM(args, 0);
+ return (*func)(self, other, op);
}
#undef RICHCMP_WRAPPER
@@ -4336,7 +4336,7 @@ wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
static PyObject * \
richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
{ \
- return wrap_richcmpfunc(self, args, wrapped, OP); \
+ return wrap_richcmpfunc(self, args, wrapped, OP); \
}
RICHCMP_WRAPPER(lt, Py_LT)
@@ -4349,164 +4349,164 @@ RICHCMP_WRAPPER(ge, Py_GE)
static PyObject *
wrap_next(PyObject *self, PyObject *args, void *wrapped)
{
- unaryfunc func = (unaryfunc)wrapped;
- PyObject *res;
+ unaryfunc func = (unaryfunc)wrapped;
+ PyObject *res;
- if (!check_num_args(args, 0))
- return NULL;
- res = (*func)(self);
- if (res == NULL && !PyErr_Occurred())
- PyErr_SetNone(PyExc_StopIteration);
- return res;
+ if (!check_num_args(args, 0))
+ return NULL;
+ res = (*func)(self);
+ if (res == NULL && !PyErr_Occurred())
+ PyErr_SetNone(PyExc_StopIteration);
+ return res;
}
static PyObject *
wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
{
- descrgetfunc func = (descrgetfunc)wrapped;
- PyObject *obj;
- PyObject *type = NULL;
+ descrgetfunc func = (descrgetfunc)wrapped;
+ PyObject *obj;
+ PyObject *type = NULL;
- if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
- return NULL;
- if (obj == Py_None)
- obj = NULL;
- if (type == Py_None)
- type = NULL;
- if (type == NULL &&obj == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "__get__(None, None) is invalid");
- return NULL;
- }
- return (*func)(self, obj, type);
+ if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
+ return NULL;
+ if (obj == Py_None)
+ obj = NULL;
+ if (type == Py_None)
+ type = NULL;
+ if (type == NULL &&obj == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "__get__(None, None) is invalid");
+ return NULL;
+ }
+ return (*func)(self, obj, type);
}
static PyObject *
wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
{
- descrsetfunc func = (descrsetfunc)wrapped;
- PyObject *obj, *value;
- int ret;
+ descrsetfunc func = (descrsetfunc)wrapped;
+ PyObject *obj, *value;
+ int ret;
- if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
- return NULL;
- ret = (*func)(self, obj, value);
- if (ret < 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
+ return NULL;
+ ret = (*func)(self, obj, value);
+ if (ret < 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
{
- descrsetfunc func = (descrsetfunc)wrapped;
- PyObject *obj;
- int ret;
+ descrsetfunc func = (descrsetfunc)wrapped;
+ PyObject *obj;
+ int ret;
- if (!check_num_args(args, 1))
- return NULL;
- obj = PyTuple_GET_ITEM(args, 0);
- ret = (*func)(self, obj, NULL);
- if (ret < 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!check_num_args(args, 1))
+ return NULL;
+ obj = PyTuple_GET_ITEM(args, 0);
+ ret = (*func)(self, obj, NULL);
+ if (ret < 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
{
- initproc func = (initproc)wrapped;
+ initproc func = (initproc)wrapped;
- if (func(self, args, kwds) < 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (func(self, args, kwds) < 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyTypeObject *type, *subtype, *staticbase;
- PyObject *arg0, *res;
-
- if (self == NULL || !PyType_Check(self))
- Py_FatalError("__new__() called with non-type 'self'");
- type = (PyTypeObject *)self;
- if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
- PyErr_Format(PyExc_TypeError,
- "%s.__new__(): not enough arguments",
- type->tp_name);
- return NULL;
- }
- arg0 = PyTuple_GET_ITEM(args, 0);
- if (!PyType_Check(arg0)) {
- PyErr_Format(PyExc_TypeError,
- "%s.__new__(X): X is not a type object (%s)",
- type->tp_name,
- Py_TYPE(arg0)->tp_name);
- return NULL;
- }
- subtype = (PyTypeObject *)arg0;
- if (!PyType_IsSubtype(subtype, type)) {
- PyErr_Format(PyExc_TypeError,
- "%s.__new__(%s): %s is not a subtype of %s",
- type->tp_name,
- subtype->tp_name,
- subtype->tp_name,
- type->tp_name);
- return NULL;
- }
-
- /* Check that the use doesn't do something silly and unsafe like
- object.__new__(dict). To do this, we check that the
- most derived base that's not a heap type is this type. */
- staticbase = subtype;
- while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
- staticbase = staticbase->tp_base;
- /* If staticbase is NULL now, it is a really weird type.
- In the spirit of backwards compatibility (?), just shut up. */
- if (staticbase && staticbase->tp_new != type->tp_new) {
- PyErr_Format(PyExc_TypeError,
- "%s.__new__(%s) is not safe, use %s.__new__()",
- type->tp_name,
- subtype->tp_name,
- staticbase == NULL ? "?" : staticbase->tp_name);
- return NULL;
- }
-
- args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
- if (args == NULL)
- return NULL;
- res = type->tp_new(subtype, args, kwds);
- Py_DECREF(args);
- return res;
+ PyTypeObject *type, *subtype, *staticbase;
+ PyObject *arg0, *res;
+
+ if (self == NULL || !PyType_Check(self))
+ Py_FatalError("__new__() called with non-type 'self'");
+ type = (PyTypeObject *)self;
+ if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
+ PyErr_Format(PyExc_TypeError,
+ "%s.__new__(): not enough arguments",
+ type->tp_name);
+ return NULL;
+ }
+ arg0 = PyTuple_GET_ITEM(args, 0);
+ if (!PyType_Check(arg0)) {
+ PyErr_Format(PyExc_TypeError,
+ "%s.__new__(X): X is not a type object (%s)",
+ type->tp_name,
+ Py_TYPE(arg0)->tp_name);
+ return NULL;
+ }
+ subtype = (PyTypeObject *)arg0;
+ if (!PyType_IsSubtype(subtype, type)) {
+ PyErr_Format(PyExc_TypeError,
+ "%s.__new__(%s): %s is not a subtype of %s",
+ type->tp_name,
+ subtype->tp_name,
+ subtype->tp_name,
+ type->tp_name);
+ return NULL;
+ }
+
+ /* Check that the use doesn't do something silly and unsafe like
+ object.__new__(dict). To do this, we check that the
+ most derived base that's not a heap type is this type. */
+ staticbase = subtype;
+ while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
+ staticbase = staticbase->tp_base;
+ /* If staticbase is NULL now, it is a really weird type.
+ In the spirit of backwards compatibility (?), just shut up. */
+ if (staticbase && staticbase->tp_new != type->tp_new) {
+ PyErr_Format(PyExc_TypeError,
+ "%s.__new__(%s) is not safe, use %s.__new__()",
+ type->tp_name,
+ subtype->tp_name,
+ staticbase == NULL ? "?" : staticbase->tp_name);
+ return NULL;
+ }
+
+ args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
+ if (args == NULL)
+ return NULL;
+ res = type->tp_new(subtype, args, kwds);
+ Py_DECREF(args);
+ return res;
}
static struct PyMethodDef tp_new_methoddef[] = {
- {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
- PyDoc_STR("T.__new__(S, ...) -> "
- "a new object with type S, a subtype of T")},
- {0}
+ {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
+ PyDoc_STR("T.__new__(S, ...) -> "
+ "a new object with type S, a subtype of T")},
+ {0}
};
static int
add_tp_new_wrapper(PyTypeObject *type)
{
- PyObject *func;
+ PyObject *func;
- if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
- return 0;
- func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
- if (func == NULL)
- return -1;
- if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
- Py_DECREF(func);
- return -1;
- }
- Py_DECREF(func);
- return 0;
+ if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
+ return 0;
+ func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
+ if (func == NULL)
+ return -1;
+ if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
+ Py_DECREF(func);
+ return -1;
+ }
+ Py_DECREF(func);
+ return 0;
}
/* Slot wrappers that call the corresponding __foo__ slot. See comments
@@ -4516,16 +4516,16 @@ add_tp_new_wrapper(PyTypeObject *type)
static PyObject * \
FUNCNAME(PyObject *self) \
{ \
- static PyObject *cache_str; \
- return call_method(self, OPSTR, &cache_str, "()"); \
+ static PyObject *cache_str; \
+ return call_method(self, OPSTR, &cache_str, "()"); \
}
#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
static PyObject * \
FUNCNAME(PyObject *self, ARG1TYPE arg1) \
{ \
- static PyObject *cache_str; \
- return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
+ static PyObject *cache_str; \
+ return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
}
/* Boolean helper for SLOT1BINFULL().
@@ -4533,33 +4533,33 @@ FUNCNAME(PyObject *self, ARG1TYPE arg1) \
static int
method_is_overloaded(PyObject *left, PyObject *right, char *name)
{
- PyObject *a, *b;
- int ok;
+ PyObject *a, *b;
+ int ok;
- b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
- if (b == NULL) {
- PyErr_Clear();
- /* If right doesn't have it, it's not overloaded */
- return 0;
- }
+ b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
+ if (b == NULL) {
+ PyErr_Clear();
+ /* If right doesn't have it, it's not overloaded */
+ return 0;
+ }
- a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
- if (a == NULL) {
- PyErr_Clear();
- Py_DECREF(b);
- /* If right has it but left doesn't, it's overloaded */
- return 1;
- }
+ a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
+ if (a == NULL) {
+ PyErr_Clear();
+ Py_DECREF(b);
+ /* If right has it but left doesn't, it's overloaded */
+ return 1;
+ }
- ok = PyObject_RichCompareBool(a, b, Py_NE);
- Py_DECREF(a);
- Py_DECREF(b);
- if (ok < 0) {
- PyErr_Clear();
- return 0;
- }
+ ok = PyObject_RichCompareBool(a, b, Py_NE);
+ Py_DECREF(a);
+ Py_DECREF(b);
+ if (ok < 0) {
+ PyErr_Clear();
+ return 0;
+ }
- return ok;
+ return ok;
}
@@ -4567,68 +4567,68 @@ method_is_overloaded(PyObject *left, PyObject *right, char *name)
static PyObject * \
FUNCNAME(PyObject *self, PyObject *other) \
{ \
- static PyObject *cache_str, *rcache_str; \
- int do_other = Py_TYPE(self) != Py_TYPE(other) && \
- Py_TYPE(other)->tp_as_number != NULL && \
- Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
- if (Py_TYPE(self)->tp_as_number != NULL && \
- Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
- PyObject *r; \
- if (do_other && \
- PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
- method_is_overloaded(self, other, ROPSTR)) { \
- r = call_maybe( \
- other, ROPSTR, &rcache_str, "(O)", self); \
- if (r != Py_NotImplemented) \
- return r; \
- Py_DECREF(r); \
- do_other = 0; \
- } \
- r = call_maybe( \
- self, OPSTR, &cache_str, "(O)", other); \
- if (r != Py_NotImplemented || \
- Py_TYPE(other) == Py_TYPE(self)) \
- return r; \
- Py_DECREF(r); \
- } \
- if (do_other) { \
- return call_maybe( \
- other, ROPSTR, &rcache_str, "(O)", self); \
- } \
- Py_INCREF(Py_NotImplemented); \
- return Py_NotImplemented; \
+ static PyObject *cache_str, *rcache_str; \
+ int do_other = Py_TYPE(self) != Py_TYPE(other) && \
+ Py_TYPE(other)->tp_as_number != NULL && \
+ Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
+ if (Py_TYPE(self)->tp_as_number != NULL && \
+ Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
+ PyObject *r; \
+ if (do_other && \
+ PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
+ method_is_overloaded(self, other, ROPSTR)) { \
+ r = call_maybe( \
+ other, ROPSTR, &rcache_str, "(O)", self); \
+ if (r != Py_NotImplemented) \
+ return r; \
+ Py_DECREF(r); \
+ do_other = 0; \
+ } \
+ r = call_maybe( \
+ self, OPSTR, &cache_str, "(O)", other); \
+ if (r != Py_NotImplemented || \
+ Py_TYPE(other) == Py_TYPE(self)) \
+ return r; \
+ Py_DECREF(r); \
+ } \
+ if (do_other) { \
+ return call_maybe( \
+ other, ROPSTR, &rcache_str, "(O)", self); \
+ } \
+ Py_INCREF(Py_NotImplemented); \
+ return Py_NotImplemented; \
}
#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
- SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
+ SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
static PyObject * \
FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
{ \
- static PyObject *cache_str; \
- return call_method(self, OPSTR, &cache_str, \
- "(" ARGCODES ")", arg1, arg2); \
+ static PyObject *cache_str; \
+ return call_method(self, OPSTR, &cache_str, \
+ "(" ARGCODES ")", arg1, arg2); \
}
static Py_ssize_t
slot_sq_length(PyObject *self)
{
- static PyObject *len_str;
- PyObject *res = call_method(self, "__len__", &len_str, "()");
- Py_ssize_t len;
+ static PyObject *len_str;
+ PyObject *res = call_method(self, "__len__", &len_str, "()");
+ Py_ssize_t len;
- if (res == NULL)
- return -1;
- len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
- Py_DECREF(res);
- if (len < 0) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_ValueError,
- "__len__() should return >= 0");
- return -1;
- }
- return len;
+ if (res == NULL)
+ return -1;
+ len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
+ Py_DECREF(res);
+ if (len < 0) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_ValueError,
+ "__len__() should return >= 0");
+ return -1;
+ }
+ return len;
}
/* Super-optimized version of slot_sq_item.
@@ -4636,93 +4636,93 @@ slot_sq_length(PyObject *self)
static PyObject *
slot_sq_item(PyObject *self, Py_ssize_t i)
{
- static PyObject *getitem_str;
- PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
- descrgetfunc f;
-
- if (getitem_str == NULL) {
- getitem_str = PyUnicode_InternFromString("__getitem__");
- if (getitem_str == NULL)
- return NULL;
- }
- func = _PyType_Lookup(Py_TYPE(self), getitem_str);
- if (func != NULL) {
- if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
- Py_INCREF(func);
- else {
- func = f(func, self, (PyObject *)(Py_TYPE(self)));
- if (func == NULL) {
- return NULL;
- }
- }
- ival = PyLong_FromSsize_t(i);
- if (ival != NULL) {
- args = PyTuple_New(1);
- if (args != NULL) {
- PyTuple_SET_ITEM(args, 0, ival);
- retval = PyObject_Call(func, args, NULL);
- Py_XDECREF(args);
- Py_XDECREF(func);
- return retval;
- }
- }
- }
- else {
- PyErr_SetObject(PyExc_AttributeError, getitem_str);
- }
- Py_XDECREF(args);
- Py_XDECREF(ival);
- Py_XDECREF(func);
- return NULL;
+ static PyObject *getitem_str;
+ PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
+ descrgetfunc f;
+
+ if (getitem_str == NULL) {
+ getitem_str = PyUnicode_InternFromString("__getitem__");
+ if (getitem_str == NULL)
+ return NULL;
+ }
+ func = _PyType_Lookup(Py_TYPE(self), getitem_str);
+ if (func != NULL) {
+ if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
+ Py_INCREF(func);
+ else {
+ func = f(func, self, (PyObject *)(Py_TYPE(self)));
+ if (func == NULL) {
+ return NULL;
+ }
+ }
+ ival = PyLong_FromSsize_t(i);
+ if (ival != NULL) {
+ args = PyTuple_New(1);
+ if (args != NULL) {
+ PyTuple_SET_ITEM(args, 0, ival);
+ retval = PyObject_Call(func, args, NULL);
+ Py_XDECREF(args);
+ Py_XDECREF(func);
+ return retval;
+ }
+ }
+ }
+ else {
+ PyErr_SetObject(PyExc_AttributeError, getitem_str);
+ }
+ Py_XDECREF(args);
+ Py_XDECREF(ival);
+ Py_XDECREF(func);
+ return NULL;
}
static int
slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
{
- PyObject *res;
- static PyObject *delitem_str, *setitem_str;
+ PyObject *res;
+ static PyObject *delitem_str, *setitem_str;
- if (value == NULL)
- res = call_method(self, "__delitem__", &delitem_str,
- "(n)", index);
- else
- res = call_method(self, "__setitem__", &setitem_str,
- "(nO)", index, value);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
+ if (value == NULL)
+ res = call_method(self, "__delitem__", &delitem_str,
+ "(n)", index);
+ else
+ res = call_method(self, "__setitem__", &setitem_str,
+ "(nO)", index, value);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
}
static int
slot_sq_contains(PyObject *self, PyObject *value)
{
- PyObject *func, *res, *args;
- int result = -1;
-
- static PyObject *contains_str;
-
- func = lookup_maybe(self, "__contains__", &contains_str);
- if (func != NULL) {
- args = PyTuple_Pack(1, value);
- if (args == NULL)
- res = NULL;
- else {
- res = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- }
- Py_DECREF(func);
- if (res != NULL) {
- result = PyObject_IsTrue(res);
- Py_DECREF(res);
- }
- }
- else if (! PyErr_Occurred()) {
- /* Possible results: -1 and 1 */
- result = (int)_PySequence_IterSearch(self, value,
- PY_ITERSEARCH_CONTAINS);
- }
- return result;
+ PyObject *func, *res, *args;
+ int result = -1;
+
+ static PyObject *contains_str;
+
+ func = lookup_maybe(self, "__contains__", &contains_str);
+ if (func != NULL) {
+ args = PyTuple_Pack(1, value);
+ if (args == NULL)
+ res = NULL;
+ else {
+ res = PyObject_Call(func, args, NULL);
+ Py_DECREF(args);
+ }
+ Py_DECREF(func);
+ if (res != NULL) {
+ result = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ }
+ }
+ else if (! PyErr_Occurred()) {
+ /* Possible results: -1 and 1 */
+ result = (int)_PySequence_IterSearch(self, value,
+ PY_ITERSEARCH_CONTAINS);
+ }
+ return result;
}
#define slot_mp_length slot_sq_length
@@ -4732,19 +4732,19 @@ SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
static int
slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
{
- PyObject *res;
- static PyObject *delitem_str, *setitem_str;
+ PyObject *res;
+ static PyObject *delitem_str, *setitem_str;
- if (value == NULL)
- res = call_method(self, "__delitem__", &delitem_str,
- "(O)", key);
- else
- res = call_method(self, "__setitem__", &setitem_str,
- "(OO)", key, value);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
+ if (value == NULL)
+ res = call_method(self, "__delitem__", &delitem_str,
+ "(O)", key);
+ else
+ res = call_method(self, "__setitem__", &setitem_str,
+ "(OO)", key, value);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
}
SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
@@ -4756,25 +4756,25 @@ SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
- nb_power, "__pow__", "__rpow__")
+ nb_power, "__pow__", "__rpow__")
static PyObject *
slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
{
- static PyObject *pow_str;
+ static PyObject *pow_str;
- if (modulus == Py_None)
- return slot_nb_power_binary(self, other);
- /* Three-arg power doesn't use __rpow__. But ternary_op
- can call this when the second argument's type uses
- slot_nb_power, so check before calling self.__pow__. */
- if (Py_TYPE(self)->tp_as_number != NULL &&
- Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
- return call_method(self, "__pow__", &pow_str,
- "(OO)", other, modulus);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ if (modulus == Py_None)
+ return slot_nb_power_binary(self, other);
+ /* Three-arg power doesn't use __rpow__. But ternary_op
+ can call this when the second argument's type uses
+ slot_nb_power, so check before calling self.__pow__. */
+ if (Py_TYPE(self)->tp_as_number != NULL &&
+ Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
+ return call_method(self, "__pow__", &pow_str,
+ "(OO)", other, modulus);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
SLOT0(slot_nb_negative, "__neg__")
@@ -4784,52 +4784,52 @@ SLOT0(slot_nb_absolute, "__abs__")
static int
slot_nb_bool(PyObject *self)
{
- PyObject *func, *args;
- static PyObject *bool_str, *len_str;
- int result = -1;
- int using_len = 0;
-
- func = lookup_maybe(self, "__bool__", &bool_str);
- if (func == NULL) {
- if (PyErr_Occurred())
- return -1;
- func = lookup_maybe(self, "__len__", &len_str);
- if (func == NULL)
- return PyErr_Occurred() ? -1 : 1;
- using_len = 1;
- }
- args = PyTuple_New(0);
- if (args != NULL) {
- PyObject *temp = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- if (temp != NULL) {
- if (using_len) {
- /* enforced by slot_nb_len */
- result = PyObject_IsTrue(temp);
- }
- else if (PyBool_Check(temp)) {
- result = PyObject_IsTrue(temp);
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "__bool__ should return "
- "bool, returned %s",
- Py_TYPE(temp)->tp_name);
- result = -1;
- }
- Py_DECREF(temp);
- }
- }
- Py_DECREF(func);
- return result;
+ PyObject *func, *args;
+ static PyObject *bool_str, *len_str;
+ int result = -1;
+ int using_len = 0;
+
+ func = lookup_maybe(self, "__bool__", &bool_str);
+ if (func == NULL) {
+ if (PyErr_Occurred())
+ return -1;
+ func = lookup_maybe(self, "__len__", &len_str);
+ if (func == NULL)
+ return PyErr_Occurred() ? -1 : 1;
+ using_len = 1;
+ }
+ args = PyTuple_New(0);
+ if (args != NULL) {
+ PyObject *temp = PyObject_Call(func, args, NULL);
+ Py_DECREF(args);
+ if (temp != NULL) {
+ if (using_len) {
+ /* enforced by slot_nb_len */
+ result = PyObject_IsTrue(temp);
+ }
+ else if (PyBool_Check(temp)) {
+ result = PyObject_IsTrue(temp);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "__bool__ should return "
+ "bool, returned %s",
+ Py_TYPE(temp)->tp_name);
+ result = -1;
+ }
+ Py_DECREF(temp);
+ }
+ }
+ Py_DECREF(func);
+ return result;
}
static PyObject *
slot_nb_index(PyObject *self)
{
- static PyObject *index_str;
- return call_method(self, "__index__", &index_str, "()");
+ static PyObject *index_str;
+ return call_method(self, "__index__", &index_str, "()");
}
@@ -4847,11 +4847,11 @@ SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
/* Can't use SLOT1 here, because nb_inplace_power is ternary */
-static PyObject *
-slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
-{
- static PyObject *cache_str;
- return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
+static PyObject *
+slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
+{
+ static PyObject *cache_str;
+ return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
}
SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
@@ -4859,7 +4859,7 @@ SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
- "__floordiv__", "__rfloordiv__")
+ "__floordiv__", "__rfloordiv__")
SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
@@ -4867,90 +4867,90 @@ SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
static PyObject *
slot_tp_repr(PyObject *self)
{
- PyObject *func, *res;
- static PyObject *repr_str;
+ PyObject *func, *res;
+ static PyObject *repr_str;
- func = lookup_method(self, "__repr__", &repr_str);
- if (func != NULL) {
- res = PyEval_CallObject(func, NULL);
- Py_DECREF(func);
- return res;
- }
- PyErr_Clear();
- return PyUnicode_FromFormat("<%s object at %p>",
- Py_TYPE(self)->tp_name, self);
+ func = lookup_method(self, "__repr__", &repr_str);
+ if (func != NULL) {
+ res = PyEval_CallObject(func, NULL);
+ Py_DECREF(func);
+ return res;
+ }
+ PyErr_Clear();
+ return PyUnicode_FromFormat("<%s object at %p>",
+ Py_TYPE(self)->tp_name, self);
}
static PyObject *
slot_tp_str(PyObject *self)
{
- PyObject *func, *res;
- static PyObject *str_str;
-
- func = lookup_method(self, "__str__", &str_str);
- if (func != NULL) {
- res = PyEval_CallObject(func, NULL);
- Py_DECREF(func);
- return res;
- }
- else {
- PyObject *ress;
- PyErr_Clear();
- res = slot_tp_repr(self);
- if (!res)
- return NULL;
- ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
- Py_DECREF(res);
- return ress;
- }
+ PyObject *func, *res;
+ static PyObject *str_str;
+
+ func = lookup_method(self, "__str__", &str_str);
+ if (func != NULL) {
+ res = PyEval_CallObject(func, NULL);
+ Py_DECREF(func);
+ return res;
+ }
+ else {
+ PyObject *ress;
+ PyErr_Clear();
+ res = slot_tp_repr(self);
+ if (!res)
+ return NULL;
+ ress = _PyUnicode_AsDefaultEncodedString(res, NULL);
+ Py_DECREF(res);
+ return ress;
+ }
}
static long
slot_tp_hash(PyObject *self)
{
- PyObject *func, *res;
- static PyObject *hash_str;
- long h;
+ PyObject *func, *res;
+ static PyObject *hash_str;
+ long h;
- func = lookup_method(self, "__hash__", &hash_str);
+ func = lookup_method(self, "__hash__", &hash_str);
- if (func == Py_None) {
- Py_DECREF(func);
- func = NULL;
- }
+ if (func == Py_None) {
+ Py_DECREF(func);
+ func = NULL;
+ }
- if (func == NULL) {
- return PyObject_HashNotImplemented(self);
- }
+ if (func == NULL) {
+ return PyObject_HashNotImplemented(self);
+ }
- res = PyEval_CallObject(func, NULL);
- Py_DECREF(func);
- if (res == NULL)
- return -1;
- if (PyLong_Check(res))
- h = PyLong_Type.tp_hash(res);
- else
- h = PyLong_AsLong(res);
- Py_DECREF(res);
- if (h == -1 && !PyErr_Occurred())
- h = -2;
- return h;
+ res = PyEval_CallObject(func, NULL);
+ Py_DECREF(func);
+ if (res == NULL)
+ return -1;
+ if (PyLong_Check(res))
+ h = PyLong_Type.tp_hash(res);
+ else
+ h = PyLong_AsLong(res);
+ Py_DECREF(res);
+ if (h == -1 && !PyErr_Occurred())
+ h = -2;
+ return h;
}
static PyObject *
slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
{
- static PyObject *call_str;
- PyObject *meth = lookup_method(self, "__call__", &call_str);
- PyObject *res;
+ static PyObject *call_str;
+ PyObject *meth = lookup_method(self, "__call__", &call_str);
+ PyObject *res;
- if (meth == NULL)
- return NULL;
+ if (meth == NULL)
+ return NULL;
- res = PyObject_Call(meth, args, kwds);
+ res = PyObject_Call(meth, args, kwds);
- Py_DECREF(meth);
- return res;
+ Py_DECREF(meth);
+ return res;
}
/* There are two slot dispatch functions for tp_getattro.
@@ -4967,100 +4967,100 @@ slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
static PyObject *
slot_tp_getattro(PyObject *self, PyObject *name)
{
- static PyObject *getattribute_str = NULL;
- return call_method(self, "__getattribute__", &getattribute_str,
- "(O)", name);
+ static PyObject *getattribute_str = NULL;
+ return call_method(self, "__getattribute__", &getattribute_str,
+ "(O)", name);
}
static PyObject *
call_attribute(PyObject *self, PyObject *attr, PyObject *name)
{
- PyObject *res, *descr = NULL;
- descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
+ PyObject *res, *descr = NULL;
+ descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
- if (f != NULL) {
- descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
- if (descr == NULL)
- return NULL;
- else
- attr = descr;
- }
- res = PyObject_CallFunctionObjArgs(attr, name, NULL);
- Py_XDECREF(descr);
- return res;
+ if (f != NULL) {
+ descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
+ if (descr == NULL)
+ return NULL;
+ else
+ attr = descr;
+ }
+ res = PyObject_CallFunctionObjArgs(attr, name, NULL);
+ Py_XDECREF(descr);
+ return res;
}
static PyObject *
slot_tp_getattr_hook(PyObject *self, PyObject *name)
{
- PyTypeObject *tp = Py_TYPE(self);
- PyObject *getattr, *getattribute, *res;
- static PyObject *getattribute_str = NULL;
- static PyObject *getattr_str = NULL;
-
- if (getattr_str == NULL) {
- getattr_str = PyUnicode_InternFromString("__getattr__");
- if (getattr_str == NULL)
- return NULL;
- }
- if (getattribute_str == NULL) {
- getattribute_str =
- PyUnicode_InternFromString("__getattribute__");
- if (getattribute_str == NULL)
- return NULL;
- }
- /* speed hack: we could use lookup_maybe, but that would resolve the
- method fully for each attribute lookup for classes with
- __getattr__, even when the attribute is present. So we use
- _PyType_Lookup and create the method only when needed, with
- call_attribute. */
- getattr = _PyType_Lookup(tp, getattr_str);
- if (getattr == NULL) {
- /* No __getattr__ hook: use a simpler dispatcher */
- tp->tp_getattro = slot_tp_getattro;
- return slot_tp_getattro(self, name);
- }
- Py_INCREF(getattr);
- /* speed hack: we could use lookup_maybe, but that would resolve the
- method fully for each attribute lookup for classes with
- __getattr__, even when self has the default __getattribute__
- method. So we use _PyType_Lookup and create the method only when
- needed, with call_attribute. */
- getattribute = _PyType_Lookup(tp, getattribute_str);
- if (getattribute == NULL ||
- (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
- ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
- (void *)PyObject_GenericGetAttr))
- res = PyObject_GenericGetAttr(self, name);
- else {
- Py_INCREF(getattribute);
- res = call_attribute(self, getattribute, name);
- Py_DECREF(getattribute);
- }
- if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_Clear();
- res = call_attribute(self, getattr, name);
- }
- Py_DECREF(getattr);
- return res;
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject *getattr, *getattribute, *res;
+ static PyObject *getattribute_str = NULL;
+ static PyObject *getattr_str = NULL;
+
+ if (getattr_str == NULL) {
+ getattr_str = PyUnicode_InternFromString("__getattr__");
+ if (getattr_str == NULL)
+ return NULL;
+ }
+ if (getattribute_str == NULL) {
+ getattribute_str =
+ PyUnicode_InternFromString("__getattribute__");
+ if (getattribute_str == NULL)
+ return NULL;
+ }
+ /* speed hack: we could use lookup_maybe, but that would resolve the
+ method fully for each attribute lookup for classes with
+ __getattr__, even when the attribute is present. So we use
+ _PyType_Lookup and create the method only when needed, with
+ call_attribute. */
+ getattr = _PyType_Lookup(tp, getattr_str);
+ if (getattr == NULL) {
+ /* No __getattr__ hook: use a simpler dispatcher */
+ tp->tp_getattro = slot_tp_getattro;
+ return slot_tp_getattro(self, name);
+ }
+ Py_INCREF(getattr);
+ /* speed hack: we could use lookup_maybe, but that would resolve the
+ method fully for each attribute lookup for classes with
+ __getattr__, even when self has the default __getattribute__
+ method. So we use _PyType_Lookup and create the method only when
+ needed, with call_attribute. */
+ getattribute = _PyType_Lookup(tp, getattribute_str);
+ if (getattribute == NULL ||
+ (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
+ ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
+ (void *)PyObject_GenericGetAttr))
+ res = PyObject_GenericGetAttr(self, name);
+ else {
+ Py_INCREF(getattribute);
+ res = call_attribute(self, getattribute, name);
+ Py_DECREF(getattribute);
+ }
+ if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Clear();
+ res = call_attribute(self, getattr, name);
+ }
+ Py_DECREF(getattr);
+ return res;
}
static int
slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
{
- PyObject *res;
- static PyObject *delattr_str, *setattr_str;
+ PyObject *res;
+ static PyObject *delattr_str, *setattr_str;
- if (value == NULL)
- res = call_method(self, "__delattr__", &delattr_str,
- "(O)", name);
- else
- res = call_method(self, "__setattr__", &setattr_str,
- "(OO)", name, value);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
+ if (value == NULL)
+ res = call_method(self, "__delattr__", &delattr_str,
+ "(O)", name);
+ else
+ res = call_method(self, "__setattr__", &setattr_str,
+ "(OO)", name, value);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
}
static char *name_op[] = {
@@ -5075,244 +5075,244 @@ static char *name_op[] = {
static PyObject *
half_richcompare(PyObject *self, PyObject *other, int op)
{
- PyObject *func, *args, *res;
- static PyObject *op_str[6];
-
- func = lookup_method(self, name_op[op], &op_str[op]);
- if (func == NULL) {
- PyErr_Clear();
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
- }
- args = PyTuple_Pack(1, other);
- if (args == NULL)
- res = NULL;
- else {
- res = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- }
- Py_DECREF(func);
- return res;
+ PyObject *func, *args, *res;
+ static PyObject *op_str[6];
+
+ func = lookup_method(self, name_op[op], &op_str[op]);
+ if (func == NULL) {
+ PyErr_Clear();
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+ }
+ args = PyTuple_Pack(1, other);
+ if (args == NULL)
+ res = NULL;
+ else {
+ res = PyObject_Call(func, args, NULL);
+ Py_DECREF(args);
+ }
+ Py_DECREF(func);
+ return res;
}
static PyObject *
slot_tp_richcompare(PyObject *self, PyObject *other, int op)
{
- PyObject *res;
-
- if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
- res = half_richcompare(self, other, op);
- if (res != Py_NotImplemented)
- return res;
- Py_DECREF(res);
- }
- if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
- res = half_richcompare(other, self, _Py_SwappedOp[op]);
- if (res != Py_NotImplemented) {
- return res;
- }
- Py_DECREF(res);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
+ PyObject *res;
+
+ if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
+ res = half_richcompare(self, other, op);
+ if (res != Py_NotImplemented)
+ return res;
+ Py_DECREF(res);
+ }
+ if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
+ res = half_richcompare(other, self, _Py_SwappedOp[op]);
+ if (res != Py_NotImplemented) {
+ return res;
+ }
+ Py_DECREF(res);
+ }
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
}
static PyObject *
slot_tp_iter(PyObject *self)
{
- PyObject *func, *res;
- static PyObject *iter_str, *getitem_str;
-
- func = lookup_method(self, "__iter__", &iter_str);
- if (func != NULL) {
- PyObject *args;
- args = res = PyTuple_New(0);
- if (args != NULL) {
- res = PyObject_Call(func, args, NULL);
- Py_DECREF(args);
- }
- Py_DECREF(func);
- return res;
- }
- PyErr_Clear();
- func = lookup_method(self, "__getitem__", &getitem_str);
- if (func == NULL) {
- PyErr_Format(PyExc_TypeError,
- "'%.200s' object is not iterable",
- Py_TYPE(self)->tp_name);
- return NULL;
- }
- Py_DECREF(func);
- return PySeqIter_New(self);
+ PyObject *func, *res;
+ static PyObject *iter_str, *getitem_str;
+
+ func = lookup_method(self, "__iter__", &iter_str);
+ if (func != NULL) {
+ PyObject *args;
+ args = res = PyTuple_New(0);
+ if (args != NULL) {
+ res = PyObject_Call(func, args, NULL);
+ Py_DECREF(args);
+ }
+ Py_DECREF(func);
+ return res;
+ }
+ PyErr_Clear();
+ func = lookup_method(self, "__getitem__", &getitem_str);
+ if (func == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "'%.200s' object is not iterable",
+ Py_TYPE(self)->tp_name);
+ return NULL;
+ }
+ Py_DECREF(func);
+ return PySeqIter_New(self);
}
static PyObject *
slot_tp_iternext(PyObject *self)
{
- static PyObject *next_str;
- return call_method(self, "__next__", &next_str, "()");
+ static PyObject *next_str;
+ return call_method(self, "__next__", &next_str, "()");
}
static PyObject *
slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- PyTypeObject *tp = Py_TYPE(self);
- PyObject *get;
- static PyObject *get_str = NULL;
-
- if (get_str == NULL) {
- get_str = PyUnicode_InternFromString("__get__");
- if (get_str == NULL)
- return NULL;
- }
- get = _PyType_Lookup(tp, get_str);
- if (get == NULL) {
- /* Avoid further slowdowns */
- if (tp->tp_descr_get == slot_tp_descr_get)
- tp->tp_descr_get = NULL;
- Py_INCREF(self);
- return self;
- }
- if (obj == NULL)
- obj = Py_None;
- if (type == NULL)
- type = Py_None;
- return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject *get;
+ static PyObject *get_str = NULL;
+
+ if (get_str == NULL) {
+ get_str = PyUnicode_InternFromString("__get__");
+ if (get_str == NULL)
+ return NULL;
+ }
+ get = _PyType_Lookup(tp, get_str);
+ if (get == NULL) {
+ /* Avoid further slowdowns */
+ if (tp->tp_descr_get == slot_tp_descr_get)
+ tp->tp_descr_get = NULL;
+ Py_INCREF(self);
+ return self;
+ }
+ if (obj == NULL)
+ obj = Py_None;
+ if (type == NULL)
+ type = Py_None;
+ return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
}
static int
slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
{
- PyObject *res;
- static PyObject *del_str, *set_str;
+ PyObject *res;
+ static PyObject *del_str, *set_str;
- if (value == NULL)
- res = call_method(self, "__delete__", &del_str,
- "(O)", target);
- else
- res = call_method(self, "__set__", &set_str,
- "(OO)", target, value);
- if (res == NULL)
- return -1;
- Py_DECREF(res);
- return 0;
+ if (value == NULL)
+ res = call_method(self, "__delete__", &del_str,
+ "(O)", target);
+ else
+ res = call_method(self, "__set__", &set_str,
+ "(OO)", target, value);
+ if (res == NULL)
+ return -1;
+ Py_DECREF(res);
+ return 0;
}
static int
slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- static PyObject *init_str;
- PyObject *meth = lookup_method(self, "__init__", &init_str);
- PyObject *res;
-
- if (meth == NULL)
- return -1;
- res = PyObject_Call(meth, args, kwds);
- Py_DECREF(meth);
- if (res == NULL)
- return -1;
- if (res != Py_None) {
- PyErr_Format(PyExc_TypeError,
- "__init__() should return None, not '%.200s'",
- Py_TYPE(res)->tp_name);
- Py_DECREF(res);
- return -1;
- }
- Py_DECREF(res);
- return 0;
+ static PyObject *init_str;
+ PyObject *meth = lookup_method(self, "__init__", &init_str);
+ PyObject *res;
+
+ if (meth == NULL)
+ return -1;
+ res = PyObject_Call(meth, args, kwds);
+ Py_DECREF(meth);
+ if (res == NULL)
+ return -1;
+ if (res != Py_None) {
+ PyErr_Format(PyExc_TypeError,
+ "__init__() should return None, not '%.200s'",
+ Py_TYPE(res)->tp_name);
+ Py_DECREF(res);
+ return -1;
+ }
+ Py_DECREF(res);
+ return 0;
}
static PyObject *
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- static PyObject *new_str;
- PyObject *func;
- PyObject *newargs, *x;
- Py_ssize_t i, n;
-
- if (new_str == NULL) {
- new_str = PyUnicode_InternFromString("__new__");
- if (new_str == NULL)
- return NULL;
- }
- func = PyObject_GetAttr((PyObject *)type, new_str);
- if (func == NULL)
- return NULL;
- assert(PyTuple_Check(args));
- n = PyTuple_GET_SIZE(args);
- newargs = PyTuple_New(n+1);
- if (newargs == NULL)
- return NULL;
- Py_INCREF(type);
- PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
- for (i = 0; i < n; i++) {
- x = PyTuple_GET_ITEM(args, i);
- Py_INCREF(x);
- PyTuple_SET_ITEM(newargs, i+1, x);
- }
- x = PyObject_Call(func, newargs, kwds);
- Py_DECREF(newargs);
- Py_DECREF(func);
- return x;
+ static PyObject *new_str;
+ PyObject *func;
+ PyObject *newargs, *x;
+ Py_ssize_t i, n;
+
+ if (new_str == NULL) {
+ new_str = PyUnicode_InternFromString("__new__");
+ if (new_str == NULL)
+ return NULL;
+ }
+ func = PyObject_GetAttr((PyObject *)type, new_str);
+ if (func == NULL)
+ return NULL;
+ assert(PyTuple_Check(args));
+ n = PyTuple_GET_SIZE(args);
+ newargs = PyTuple_New(n+1);
+ if (newargs == NULL)
+ return NULL;
+ Py_INCREF(type);
+ PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
+ for (i = 0; i < n; i++) {
+ x = PyTuple_GET_ITEM(args, i);
+ Py_INCREF(x);
+ PyTuple_SET_ITEM(newargs, i+1, x);
+ }
+ x = PyObject_Call(func, newargs, kwds);
+ Py_DECREF(newargs);
+ Py_DECREF(func);
+ return x;
}
static void
slot_tp_del(PyObject *self)
{
- static PyObject *del_str = NULL;
- PyObject *del, *res;
- PyObject *error_type, *error_value, *error_traceback;
-
- /* Temporarily resurrect the object. */
- assert(self->ob_refcnt == 0);
- self->ob_refcnt = 1;
-
- /* Save the current exception, if any. */
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
-
- /* Execute __del__ method, if any. */
- del = lookup_maybe(self, "__del__", &del_str);
- if (del != NULL) {
- res = PyEval_CallObject(del, NULL);
- if (res == NULL)
- PyErr_WriteUnraisable(del);
- else
- Py_DECREF(res);
- Py_DECREF(del);
- }
-
- /* Restore the saved exception. */
- PyErr_Restore(error_type, error_value, error_traceback);
-
- /* Undo the temporary resurrection; can't use DECREF here, it would
- * cause a recursive call.
- */
- assert(self->ob_refcnt > 0);
- if (--self->ob_refcnt == 0)
- return; /* this is the normal path out */
-
- /* __del__ resurrected it! Make it look like the original Py_DECREF
- * never happened.
- */
- {
- Py_ssize_t refcnt = self->ob_refcnt;
- _Py_NewReference(self);
- self->ob_refcnt = refcnt;
- }
- assert(!PyType_IS_GC(Py_TYPE(self)) ||
- _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
- /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
- * we need to undo that. */
- _Py_DEC_REFTOTAL;
- /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
- * chain, so no more to do there.
- * If COUNT_ALLOCS, the original decref bumped tp_frees, and
- * _Py_NewReference bumped tp_allocs: both of those need to be
- * undone.
- */
+ static PyObject *del_str = NULL;
+ PyObject *del, *res;
+ PyObject *error_type, *error_value, *error_traceback;
+
+ /* Temporarily resurrect the object. */
+ assert(self->ob_refcnt == 0);
+ self->ob_refcnt = 1;
+
+ /* Save the current exception, if any. */
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+ /* Execute __del__ method, if any. */
+ del = lookup_maybe(self, "__del__", &del_str);
+ if (del != NULL) {
+ res = PyEval_CallObject(del, NULL);
+ if (res == NULL)
+ PyErr_WriteUnraisable(del);
+ else
+ Py_DECREF(res);
+ Py_DECREF(del);
+ }
+
+ /* Restore the saved exception. */
+ PyErr_Restore(error_type, error_value, error_traceback);
+
+ /* Undo the temporary resurrection; can't use DECREF here, it would
+ * cause a recursive call.
+ */
+ assert(self->ob_refcnt > 0);
+ if (--self->ob_refcnt == 0)
+ return; /* this is the normal path out */
+
+ /* __del__ resurrected it! Make it look like the original Py_DECREF
+ * never happened.
+ */
+ {
+ Py_ssize_t refcnt = self->ob_refcnt;
+ _Py_NewReference(self);
+ self->ob_refcnt = refcnt;
+ }
+ assert(!PyType_IS_GC(Py_TYPE(self)) ||
+ _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
+ /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
+ * we need to undo that. */
+ _Py_DEC_REFTOTAL;
+ /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
+ * chain, so no more to do there.
+ * If COUNT_ALLOCS, the original decref bumped tp_frees, and
+ * _Py_NewReference bumped tp_allocs: both of those need to be
+ * undone.
+ */
#ifdef COUNT_ALLOCS
- --Py_TYPE(self)->tp_frees;
- --Py_TYPE(self)->tp_allocs;
+ --Py_TYPE(self)->tp_frees;
+ --Py_TYPE(self)->tp_allocs;
#endif
}
@@ -5341,236 +5341,236 @@ typedef struct wrapperbase slotdef;
#undef RBINSLOT
#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
- PyDoc_STR(DOC)}
+ {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
+ PyDoc_STR(DOC)}
#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
- {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
- PyDoc_STR(DOC), FLAGS}
+ {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
+ PyDoc_STR(DOC), FLAGS}
#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
- PyDoc_STR(DOC)}
+ {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
+ PyDoc_STR(DOC)}
#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
+ ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
+ ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
- "x." NAME "() <==> " DOC)
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
+ "x." NAME "() <==> " DOC)
#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
- "x." NAME "(y) <==> x" DOC "y")
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
+ "x." NAME "(y) <==> x" DOC "y")
#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
- "x." NAME "(y) <==> x" DOC "y")
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
+ "x." NAME "(y) <==> x" DOC "y")
#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
- "x." NAME "(y) <==> y" DOC "x")
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
+ "x." NAME "(y) <==> y" DOC "x")
#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
- "x." NAME "(y) <==> " DOC)
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
+ "x." NAME "(y) <==> " DOC)
#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
- ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
- "x." NAME "(y) <==> " DOC)
+ ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
+ "x." NAME "(y) <==> " DOC)
static slotdef slotdefs[] = {
- SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
- "x.__len__() <==> len(x)"),
- /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
- The logic in abstract.c always falls back to nb_add/nb_multiply in
- this case. Defining both the nb_* and the sq_* slots to call the
- user-defined methods has unexpected side-effects, as shown by
- test_descr.notimplemented() */
- SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
- "x.__add__(y) <==> x+y"),
- SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
- "x.__mul__(n) <==> x*n"),
- SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
- "x.__rmul__(n) <==> n*x"),
- SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
- "x.__getitem__(y) <==> x[y]"),
- SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
- "x.__setitem__(i, y) <==> x[i]=y"),
- SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
- "x.__delitem__(y) <==> del x[y]"),
- SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
- "x.__contains__(y) <==> y in x"),
- SQSLOT("__iadd__", sq_inplace_concat, NULL,
- wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
- SQSLOT("__imul__", sq_inplace_repeat, NULL,
- wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
-
- MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
- "x.__len__() <==> len(x)"),
- MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
- wrap_binaryfunc,
- "x.__getitem__(y) <==> x[y]"),
- MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
- wrap_objobjargproc,
- "x.__setitem__(i, y) <==> x[i]=y"),
- MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
- wrap_delitem,
- "x.__delitem__(y) <==> del x[y]"),
-
- BINSLOT("__add__", nb_add, slot_nb_add,
- "+"),
- RBINSLOT("__radd__", nb_add, slot_nb_add,
- "+"),
- BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
- "-"),
- RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
- "-"),
- BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
- "*"),
- RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
- "*"),
- BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
- "%"),
- RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
- "%"),
- BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
- "divmod(x, y)"),
- RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
- "divmod(y, x)"),
- NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
- "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
- NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
- "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
- UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
- UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
- UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
- "abs(x)"),
- UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
- "x != 0"),
- UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
- BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
- RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
- BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
- RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
- BINSLOT("__and__", nb_and, slot_nb_and, "&"),
- RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
- BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
- RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
- BINSLOT("__or__", nb_or, slot_nb_or, "|"),
- RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
- UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
- "int(x)"),
- UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
- "float(x)"),
- NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
- "x[y:z] <==> x[y.__index__():z.__index__()]"),
- IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
- wrap_binaryfunc, "+"),
- IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
- wrap_binaryfunc, "-"),
- IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
- wrap_binaryfunc, "*"),
- IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
- wrap_binaryfunc, "%"),
- IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
- wrap_binaryfunc, "**"),
- IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
- wrap_binaryfunc, "<<"),
- IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
- wrap_binaryfunc, ">>"),
- IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
- wrap_binaryfunc, "&"),
- IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
- wrap_binaryfunc, "^"),
- IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
- wrap_binaryfunc, "|"),
- BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
- RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
- BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
- RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
- IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
- slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
- IBSLOT("__itruediv__", nb_inplace_true_divide,
- slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
-
- TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
- "x.__str__() <==> str(x)"),
- TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
- "x.__repr__() <==> repr(x)"),
- TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
- "x.__hash__() <==> hash(x)"),
- FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
- "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
- TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
- wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
- TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
- TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
- TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
- TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
- "x.__setattr__('name', value) <==> x.name = value"),
- TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
- TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
- "x.__delattr__('name') <==> del x.name"),
- TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
- TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
- "x.__lt__(y) <==> x<y"),
- TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
- "x.__le__(y) <==> x<=y"),
- TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
- "x.__eq__(y) <==> x==y"),
- TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
- "x.__ne__(y) <==> x!=y"),
- TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
- "x.__gt__(y) <==> x>y"),
- TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
- "x.__ge__(y) <==> x>=y"),
- TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
- "x.__iter__() <==> iter(x)"),
- TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
- "x.__next__() <==> next(x)"),
- TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
- "descr.__get__(obj[, type]) -> value"),
- TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
- "descr.__set__(obj, value)"),
- TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
- wrap_descr_delete, "descr.__delete__(obj)"),
- FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
- "x.__init__(...) initializes x; "
- "see x.__class__.__doc__ for signature",
- PyWrapperFlag_KEYWORDS),
- TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
- TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
- {NULL}
+ SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
+ "x.__len__() <==> len(x)"),
+ /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
+ The logic in abstract.c always falls back to nb_add/nb_multiply in
+ this case. Defining both the nb_* and the sq_* slots to call the
+ user-defined methods has unexpected side-effects, as shown by
+ test_descr.notimplemented() */
+ SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
+ "x.__add__(y) <==> x+y"),
+ SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
+ "x.__mul__(n) <==> x*n"),
+ SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
+ "x.__rmul__(n) <==> n*x"),
+ SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
+ "x.__getitem__(y) <==> x[y]"),
+ SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
+ "x.__setitem__(i, y) <==> x[i]=y"),
+ SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
+ "x.__delitem__(y) <==> del x[y]"),
+ SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
+ "x.__contains__(y) <==> y in x"),
+ SQSLOT("__iadd__", sq_inplace_concat, NULL,
+ wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
+ SQSLOT("__imul__", sq_inplace_repeat, NULL,
+ wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
+
+ MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
+ "x.__len__() <==> len(x)"),
+ MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
+ wrap_binaryfunc,
+ "x.__getitem__(y) <==> x[y]"),
+ MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
+ wrap_objobjargproc,
+ "x.__setitem__(i, y) <==> x[i]=y"),
+ MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
+ wrap_delitem,
+ "x.__delitem__(y) <==> del x[y]"),
+
+ BINSLOT("__add__", nb_add, slot_nb_add,
+ "+"),
+ RBINSLOT("__radd__", nb_add, slot_nb_add,
+ "+"),
+ BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
+ "-"),
+ RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
+ "-"),
+ BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
+ "*"),
+ RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
+ "*"),
+ BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
+ "%"),
+ RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
+ "%"),
+ BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
+ "divmod(x, y)"),
+ RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
+ "divmod(y, x)"),
+ NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
+ "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
+ NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
+ "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
+ UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
+ UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
+ UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
+ "abs(x)"),
+ UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
+ "x != 0"),
+ UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
+ BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
+ RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
+ BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
+ RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
+ BINSLOT("__and__", nb_and, slot_nb_and, "&"),
+ RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
+ BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
+ RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
+ BINSLOT("__or__", nb_or, slot_nb_or, "|"),
+ RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
+ UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
+ "int(x)"),
+ UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
+ "float(x)"),
+ NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
+ "x[y:z] <==> x[y.__index__():z.__index__()]"),
+ IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
+ wrap_binaryfunc, "+"),
+ IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
+ wrap_binaryfunc, "-"),
+ IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
+ wrap_binaryfunc, "*"),
+ IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
+ wrap_binaryfunc, "%"),
+ IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
+ wrap_binaryfunc, "**"),
+ IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
+ wrap_binaryfunc, "<<"),
+ IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
+ wrap_binaryfunc, ">>"),
+ IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
+ wrap_binaryfunc, "&"),
+ IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
+ wrap_binaryfunc, "^"),
+ IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
+ wrap_binaryfunc, "|"),
+ BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
+ RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
+ BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
+ RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
+ IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
+ slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
+ IBSLOT("__itruediv__", nb_inplace_true_divide,
+ slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
+
+ TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
+ "x.__str__() <==> str(x)"),
+ TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
+ "x.__repr__() <==> repr(x)"),
+ TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
+ "x.__hash__() <==> hash(x)"),
+ FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
+ "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
+ TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
+ wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
+ TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
+ TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
+ TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
+ TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
+ "x.__setattr__('name', value) <==> x.name = value"),
+ TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
+ TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
+ "x.__delattr__('name') <==> del x.name"),
+ TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
+ TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
+ "x.__lt__(y) <==> x<y"),
+ TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
+ "x.__le__(y) <==> x<=y"),
+ TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
+ "x.__eq__(y) <==> x==y"),
+ TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
+ "x.__ne__(y) <==> x!=y"),
+ TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
+ "x.__gt__(y) <==> x>y"),
+ TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
+ "x.__ge__(y) <==> x>=y"),
+ TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
+ "x.__iter__() <==> iter(x)"),
+ TPSLOT("__next__", tp_iternext, slot_tp_iternext, wrap_next,
+ "x.__next__() <==> next(x)"),
+ TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
+ "descr.__get__(obj[, type]) -> value"),
+ TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
+ "descr.__set__(obj, value)"),
+ TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
+ wrap_descr_delete, "descr.__delete__(obj)"),
+ FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
+ "x.__init__(...) initializes x; "
+ "see x.__class__.__doc__ for signature",
+ PyWrapperFlag_KEYWORDS),
+ TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
+ TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
+ {NULL}
};
/* Given a type pointer and an offset gotten from a slotdef entry, return a
- pointer to the actual slot. This is not quite the same as simply adding
+ pointer to the actual slot. This is not quite the same as simply adding
the offset to the type pointer, since it takes care to indirect through the
proper indirection pointer (as_buffer, etc.); it returns NULL if the
indirection pointer is NULL. */
static void **
slotptr(PyTypeObject *type, int ioffset)
{
- char *ptr;
- long offset = ioffset;
-
- /* Note: this depends on the order of the members of PyHeapTypeObject! */
- assert(offset >= 0);
- assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
- if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
- ptr = (char *)type->tp_as_sequence;
- offset -= offsetof(PyHeapTypeObject, as_sequence);
- }
- else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
- ptr = (char *)type->tp_as_mapping;
- offset -= offsetof(PyHeapTypeObject, as_mapping);
- }
- else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
- ptr = (char *)type->tp_as_number;
- offset -= offsetof(PyHeapTypeObject, as_number);
- }
- else {
- ptr = (char *)type;
- }
- if (ptr != NULL)
- ptr += offset;
- return (void **)ptr;
+ char *ptr;
+ long offset = ioffset;
+
+ /* Note: this depends on the order of the members of PyHeapTypeObject! */
+ assert(offset >= 0);
+ assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
+ if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
+ ptr = (char *)type->tp_as_sequence;
+ offset -= offsetof(PyHeapTypeObject, as_sequence);
+ }
+ else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
+ ptr = (char *)type->tp_as_mapping;
+ offset -= offsetof(PyHeapTypeObject, as_mapping);
+ }
+ else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
+ ptr = (char *)type->tp_as_number;
+ offset -= offsetof(PyHeapTypeObject, as_number);
+ }
+ else {
+ ptr = (char *)type;
+ }
+ if (ptr != NULL)
+ ptr += offset;
+ return (void **)ptr;
}
/* Length of array of slotdef pointers used to store slots with the
@@ -5584,37 +5584,37 @@ slotptr(PyTypeObject *type, int ioffset)
static void **
resolve_slotdups(PyTypeObject *type, PyObject *name)
{
- /* XXX Maybe this could be optimized more -- but is it worth it? */
-
- /* pname and ptrs act as a little cache */
- static PyObject *pname;
- static slotdef *ptrs[MAX_EQUIV];
- slotdef *p, **pp;
- void **res, **ptr;
-
- if (pname != name) {
- /* Collect all slotdefs that match name into ptrs. */
- pname = name;
- pp = ptrs;
- for (p = slotdefs; p->name_strobj; p++) {
- if (p->name_strobj == name)
- *pp++ = p;
- }
- *pp = NULL;
- }
-
- /* Look in all matching slots of the type; if exactly one of these has
- a filled-in slot, return its value. Otherwise return NULL. */
- res = NULL;
- for (pp = ptrs; *pp; pp++) {
- ptr = slotptr(type, (*pp)->offset);
- if (ptr == NULL || *ptr == NULL)
- continue;
- if (res != NULL)
- return NULL;
- res = ptr;
- }
- return res;
+ /* XXX Maybe this could be optimized more -- but is it worth it? */
+
+ /* pname and ptrs act as a little cache */
+ static PyObject *pname;
+ static slotdef *ptrs[MAX_EQUIV];
+ slotdef *p, **pp;
+ void **res, **ptr;
+
+ if (pname != name) {
+ /* Collect all slotdefs that match name into ptrs. */
+ pname = name;
+ pp = ptrs;
+ for (p = slotdefs; p->name_strobj; p++) {
+ if (p->name_strobj == name)
+ *pp++ = p;
+ }
+ *pp = NULL;
+ }
+
+ /* Look in all matching slots of the type; if exactly one of these has
+ a filled-in slot, return its value. Otherwise return NULL. */
+ res = NULL;
+ for (pp = ptrs; *pp; pp++) {
+ ptr = slotptr(type, (*pp)->offset);
+ if (ptr == NULL || *ptr == NULL)
+ continue;
+ if (res != NULL)
+ return NULL;
+ res = ptr;
+ }
+ return res;
}
/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
@@ -5626,81 +5626,81 @@ resolve_slotdups(PyTypeObject *type, PyObject *name)
static slotdef *
update_one_slot(PyTypeObject *type, slotdef *p)
{
- PyObject *descr;
- PyWrapperDescrObject *d;
- void *generic = NULL, *specific = NULL;
- int use_generic = 0;
- int offset = p->offset;
- void **ptr = slotptr(type, offset);
-
- if (ptr == NULL) {
- do {
- ++p;
- } while (p->offset == offset);
- return p;
- }
- do {
- descr = _PyType_Lookup(type, p->name_strobj);
- if (descr == NULL) {
- if (ptr == (void**)&type->tp_iternext) {
- specific = _PyObject_NextNotImplemented;
- }
- continue;
- }
- if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
- void **tptr = resolve_slotdups(type, p->name_strobj);
- if (tptr == NULL || tptr == ptr)
- generic = p->function;
- d = (PyWrapperDescrObject *)descr;
- if (d->d_base->wrapper == p->wrapper &&
- PyType_IsSubtype(type, d->d_type))
- {
- if (specific == NULL ||
- specific == d->d_wrapped)
- specific = d->d_wrapped;
- else
- use_generic = 1;
- }
- }
- else if (Py_TYPE(descr) == &PyCFunction_Type &&
- PyCFunction_GET_FUNCTION(descr) ==
- (PyCFunction)tp_new_wrapper &&
- ptr == (void**)&type->tp_new)
- {
- /* The __new__ wrapper is not a wrapper descriptor,
- so must be special-cased differently.
- If we don't do this, creating an instance will
- always use slot_tp_new which will look up
- __new__ in the MRO which will call tp_new_wrapper
- which will look through the base classes looking
- for a static base and call its tp_new (usually
- PyType_GenericNew), after performing various
- sanity checks and constructing a new argument
- list. Cut all that nonsense short -- this speeds
- up instance creation tremendously. */
- specific = (void *)type->tp_new;
- /* XXX I'm not 100% sure that there isn't a hole
- in this reasoning that requires additional
- sanity checks. I'll buy the first person to
- point out a bug in this reasoning a beer. */
- }
- else if (descr == Py_None &&
- ptr == (void**)&type->tp_hash) {
- /* We specifically allow __hash__ to be set to None
- to prevent inheritance of the default
- implementation from object.__hash__ */
- specific = PyObject_HashNotImplemented;
- }
- else {
- use_generic = 1;
- generic = p->function;
- }
- } while ((++p)->offset == offset);
- if (specific && !use_generic)
- *ptr = specific;
- else
- *ptr = generic;
- return p;
+ PyObject *descr;
+ PyWrapperDescrObject *d;
+ void *generic = NULL, *specific = NULL;
+ int use_generic = 0;
+ int offset = p->offset;
+ void **ptr = slotptr(type, offset);
+
+ if (ptr == NULL) {
+ do {
+ ++p;
+ } while (p->offset == offset);
+ return p;
+ }
+ do {
+ descr = _PyType_Lookup(type, p->name_strobj);
+ if (descr == NULL) {
+ if (ptr == (void**)&type->tp_iternext) {
+ specific = _PyObject_NextNotImplemented;
+ }
+ continue;
+ }
+ if (Py_TYPE(descr) == &PyWrapperDescr_Type) {
+ void **tptr = resolve_slotdups(type, p->name_strobj);
+ if (tptr == NULL || tptr == ptr)
+ generic = p->function;
+ d = (PyWrapperDescrObject *)descr;
+ if (d->d_base->wrapper == p->wrapper &&
+ PyType_IsSubtype(type, d->d_type))
+ {
+ if (specific == NULL ||
+ specific == d->d_wrapped)
+ specific = d->d_wrapped;
+ else
+ use_generic = 1;
+ }
+ }
+ else if (Py_TYPE(descr) == &PyCFunction_Type &&
+ PyCFunction_GET_FUNCTION(descr) ==
+ (PyCFunction)tp_new_wrapper &&
+ ptr == (void**)&type->tp_new)
+ {
+ /* The __new__ wrapper is not a wrapper descriptor,
+ so must be special-cased differently.
+ If we don't do this, creating an instance will
+ always use slot_tp_new which will look up
+ __new__ in the MRO which will call tp_new_wrapper
+ which will look through the base classes looking
+ for a static base and call its tp_new (usually
+ PyType_GenericNew), after performing various
+ sanity checks and constructing a new argument
+ list. Cut all that nonsense short -- this speeds
+ up instance creation tremendously. */
+ specific = (void *)type->tp_new;
+ /* XXX I'm not 100% sure that there isn't a hole
+ in this reasoning that requires additional
+ sanity checks. I'll buy the first person to
+ point out a bug in this reasoning a beer. */
+ }
+ else if (descr == Py_None &&
+ ptr == (void**)&type->tp_hash) {
+ /* We specifically allow __hash__ to be set to None
+ to prevent inheritance of the default
+ implementation from object.__hash__ */
+ specific = PyObject_HashNotImplemented;
+ }
+ else {
+ use_generic = 1;
+ generic = p->function;
+ }
+ } while ((++p)->offset == offset);
+ if (specific && !use_generic)
+ *ptr = specific;
+ else
+ *ptr = generic;
+ return p;
}
/* In the type, update the slots whose slotdefs are gathered in the pp array.
@@ -5708,11 +5708,11 @@ update_one_slot(PyTypeObject *type, slotdef *p)
static int
update_slots_callback(PyTypeObject *type, void *data)
{
- slotdef **pp = (slotdef **)data;
+ slotdef **pp = (slotdef **)data;
- for (; *pp; pp++)
- update_one_slot(type, *pp);
- return 0;
+ for (; *pp; pp++)
+ update_one_slot(type, *pp);
+ return 0;
}
/* Comparison function for qsort() to compare slotdefs by their offset, and
@@ -5720,14 +5720,14 @@ update_slots_callback(PyTypeObject *type, void *data)
static int
slotdef_cmp(const void *aa, const void *bb)
{
- const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
- int c = a->offset - b->offset;
- if (c != 0)
- return c;
- else
- /* Cannot use a-b, as this gives off_t,
- which may lose precision when converted to int. */
- return (a > b) ? 1 : (a < b) ? -1 : 0;
+ const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
+ int c = a->offset - b->offset;
+ if (c != 0)
+ return c;
+ else
+ /* Cannot use a-b, as this gives off_t,
+ which may lose precision when converted to int. */
+ return (a > b) ? 1 : (a < b) ? -1 : 0;
}
/* Initialize the slotdefs table by adding interned string objects for the
@@ -5735,56 +5735,56 @@ slotdef_cmp(const void *aa, const void *bb)
static void
init_slotdefs(void)
{
- slotdef *p;
- static int initialized = 0;
+ slotdef *p;
+ static int initialized = 0;
- if (initialized)
- return;
- for (p = slotdefs; p->name; p++) {
- p->name_strobj = PyUnicode_InternFromString(p->name);
- if (!p->name_strobj)
- Py_FatalError("Out of memory interning slotdef names");
- }
- qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
- slotdef_cmp);
- initialized = 1;
+ if (initialized)
+ return;
+ for (p = slotdefs; p->name; p++) {
+ p->name_strobj = PyUnicode_InternFromString(p->name);
+ if (!p->name_strobj)
+ Py_FatalError("Out of memory interning slotdef names");
+ }
+ qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
+ slotdef_cmp);
+ initialized = 1;
}
/* Update the slots after assignment to a class (type) attribute. */
static int
update_slot(PyTypeObject *type, PyObject *name)
{
- slotdef *ptrs[MAX_EQUIV];
- slotdef *p;
- slotdef **pp;
- int offset;
-
- /* Clear the VALID_VERSION flag of 'type' and all its
- subclasses. This could possibly be unified with the
- update_subclasses() recursion below, but carefully:
- they each have their own conditions on which to stop
- recursing into subclasses. */
- PyType_Modified(type);
-
- init_slotdefs();
- pp = ptrs;
- for (p = slotdefs; p->name; p++) {
- /* XXX assume name is interned! */
- if (p->name_strobj == name)
- *pp++ = p;
- }
- *pp = NULL;
- for (pp = ptrs; *pp; pp++) {
- p = *pp;
- offset = p->offset;
- while (p > slotdefs && (p-1)->offset == offset)
- --p;
- *pp = p;
- }
- if (ptrs[0] == NULL)
- return 0; /* Not an attribute that affects any slots */
- return update_subclasses(type, name,
- update_slots_callback, (void *)ptrs);
+ slotdef *ptrs[MAX_EQUIV];
+ slotdef *p;
+ slotdef **pp;
+ int offset;
+
+ /* Clear the VALID_VERSION flag of 'type' and all its
+ subclasses. This could possibly be unified with the
+ update_subclasses() recursion below, but carefully:
+ they each have their own conditions on which to stop
+ recursing into subclasses. */
+ PyType_Modified(type);
+
+ init_slotdefs();
+ pp = ptrs;
+ for (p = slotdefs; p->name; p++) {
+ /* XXX assume name is interned! */
+ if (p->name_strobj == name)
+ *pp++ = p;
+ }
+ *pp = NULL;
+ for (pp = ptrs; *pp; pp++) {
+ p = *pp;
+ offset = p->offset;
+ while (p > slotdefs && (p-1)->offset == offset)
+ --p;
+ *pp = p;
+ }
+ if (ptrs[0] == NULL)
+ return 0; /* Not an attribute that affects any slots */
+ return update_subclasses(type, name,
+ update_slots_callback, (void *)ptrs);
}
/* Store the proper functions in the slot dispatches at class (type)
@@ -5793,23 +5793,23 @@ update_slot(PyTypeObject *type, PyObject *name)
static void
fixup_slot_dispatchers(PyTypeObject *type)
{
- slotdef *p;
+ slotdef *p;
- init_slotdefs();
- for (p = slotdefs; p->name; )
- p = update_one_slot(type, p);
+ init_slotdefs();
+ for (p = slotdefs; p->name; )
+ p = update_one_slot(type, p);
}
static void
update_all_slots(PyTypeObject* type)
{
- slotdef *p;
+ slotdef *p;
- init_slotdefs();
- for (p = slotdefs; p->name; p++) {
- /* update_slot returns int but can't actually fail */
- update_slot(type, p->name_strobj);
- }
+ init_slotdefs();
+ for (p = slotdefs; p->name; p++) {
+ /* update_slot returns int but can't actually fail */
+ update_slot(type, p->name_strobj);
+ }
}
/* recurse_down_subclasses() and update_subclasses() are mutually
@@ -5818,56 +5818,56 @@ update_all_slots(PyTypeObject* type)
static int
update_subclasses(PyTypeObject *type, PyObject *name,
- update_callback callback, void *data)
+ update_callback callback, void *data)
{
- if (callback(type, data) < 0)
- return -1;
- return recurse_down_subclasses(type, name, callback, data);
+ if (callback(type, data) < 0)
+ return -1;
+ return recurse_down_subclasses(type, name, callback, data);
}
static int
recurse_down_subclasses(PyTypeObject *type, PyObject *name,
- update_callback callback, void *data)
-{
- PyTypeObject *subclass;
- PyObject *ref, *subclasses, *dict;
- Py_ssize_t i, n;
-
- subclasses = type->tp_subclasses;
- if (subclasses == NULL)
- return 0;
- assert(PyList_Check(subclasses));
- n = PyList_GET_SIZE(subclasses);
- for (i = 0; i < n; i++) {
- ref = PyList_GET_ITEM(subclasses, i);
- assert(PyWeakref_CheckRef(ref));
- subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
- assert(subclass != NULL);
- if ((PyObject *)subclass == Py_None)
- continue;
- assert(PyType_Check(subclass));
- /* Avoid recursing down into unaffected classes */
- dict = subclass->tp_dict;
- if (dict != NULL && PyDict_Check(dict) &&
- PyDict_GetItem(dict, name) != NULL)
- continue;
- if (update_subclasses(subclass, name, callback, data) < 0)
- return -1;
- }
- return 0;
+ update_callback callback, void *data)
+{
+ PyTypeObject *subclass;
+ PyObject *ref, *subclasses, *dict;
+ Py_ssize_t i, n;
+
+ subclasses = type->tp_subclasses;
+ if (subclasses == NULL)
+ return 0;
+ assert(PyList_Check(subclasses));
+ n = PyList_GET_SIZE(subclasses);
+ for (i = 0; i < n; i++) {
+ ref = PyList_GET_ITEM(subclasses, i);
+ assert(PyWeakref_CheckRef(ref));
+ subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
+ assert(subclass != NULL);
+ if ((PyObject *)subclass == Py_None)
+ continue;
+ assert(PyType_Check(subclass));
+ /* Avoid recursing down into unaffected classes */
+ dict = subclass->tp_dict;
+ if (dict != NULL && PyDict_Check(dict) &&
+ PyDict_GetItem(dict, name) != NULL)
+ continue;
+ if (update_subclasses(subclass, name, callback, data) < 0)
+ return -1;
+ }
+ return 0;
}
/* This function is called by PyType_Ready() to populate the type's
dictionary with method descriptors for function slots. For each
function slot (like tp_repr) that's defined in the type, one or more
corresponding descriptors are added in the type's tp_dict dictionary
- under the appropriate name (like __repr__). Some function slots
+ under the appropriate name (like __repr__). Some function slots
cause more than one descriptor to be added (for example, the nb_add
slot adds both __add__ and __radd__ descriptors) and some function
slots compete for the same descriptor (for example both sq_item and
mp_subscript generate a __getitem__ descriptor).
- In the latter case, the first slotdef entry encoutered wins. Since
+ In the latter case, the first slotdef entry encoutered wins. Since
slotdef entries are sorted by the offset of the slot in the
PyHeapTypeObject, this gives us some control over disambiguating
between competing slots: the members of PyHeapTypeObject are listed
@@ -5890,344 +5890,344 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *name,
static int
add_operators(PyTypeObject *type)
{
- PyObject *dict = type->tp_dict;
- slotdef *p;
- PyObject *descr;
- void **ptr;
-
- init_slotdefs();
- for (p = slotdefs; p->name; p++) {
- if (p->wrapper == NULL)
- continue;
- ptr = slotptr(type, p->offset);
- if (!ptr || !*ptr)
- continue;
- if (PyDict_GetItem(dict, p->name_strobj))
- continue;
- if (*ptr == PyObject_HashNotImplemented) {
- /* Classes may prevent the inheritance of the tp_hash
- slot by storing PyObject_HashNotImplemented in it. Make it
- visible as a None value for the __hash__ attribute. */
- if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
- return -1;
- }
- else {
- descr = PyDescr_NewWrapper(type, p, *ptr);
- if (descr == NULL)
- return -1;
- if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
- return -1;
- Py_DECREF(descr);
- }
- }
- if (type->tp_new != NULL) {
- if (add_tp_new_wrapper(type) < 0)
- return -1;
- }
- return 0;
+ PyObject *dict = type->tp_dict;
+ slotdef *p;
+ PyObject *descr;
+ void **ptr;
+
+ init_slotdefs();
+ for (p = slotdefs; p->name; p++) {
+ if (p->wrapper == NULL)
+ continue;
+ ptr = slotptr(type, p->offset);
+ if (!ptr || !*ptr)
+ continue;
+ if (PyDict_GetItem(dict, p->name_strobj))
+ continue;
+ if (*ptr == PyObject_HashNotImplemented) {
+ /* Classes may prevent the inheritance of the tp_hash
+ slot by storing PyObject_HashNotImplemented in it. Make it
+ visible as a None value for the __hash__ attribute. */
+ if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
+ return -1;
+ }
+ else {
+ descr = PyDescr_NewWrapper(type, p, *ptr);
+ if (descr == NULL)
+ return -1;
+ if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
+ return -1;
+ Py_DECREF(descr);
+ }
+ }
+ if (type->tp_new != NULL) {
+ if (add_tp_new_wrapper(type) < 0)
+ return -1;
+ }
+ return 0;
}
/* Cooperative 'super' */
typedef struct {
- PyObject_HEAD
- PyTypeObject *type;
- PyObject *obj;
- PyTypeObject *obj_type;
+ PyObject_HEAD
+ PyTypeObject *type;
+ PyObject *obj;
+ PyTypeObject *obj_type;
} superobject;
static PyMemberDef super_members[] = {
- {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
- "the class invoking super()"},
- {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
- "the instance invoking super(); may be None"},
- {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
- "the type of the instance invoking super(); may be None"},
- {0}
+ {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
+ "the class invoking super()"},
+ {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
+ "the instance invoking super(); may be None"},
+ {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
+ "the type of the instance invoking super(); may be None"},
+ {0}
};
static void
super_dealloc(PyObject *self)
{
- superobject *su = (superobject *)self;
+ superobject *su = (superobject *)self;
- _PyObject_GC_UNTRACK(self);
- Py_XDECREF(su->obj);
- Py_XDECREF(su->type);
- Py_XDECREF(su->obj_type);
- Py_TYPE(self)->tp_free(self);
+ _PyObject_GC_UNTRACK(self);
+ Py_XDECREF(su->obj);
+ Py_XDECREF(su->type);
+ Py_XDECREF(su->obj_type);
+ Py_TYPE(self)->tp_free(self);
}
static PyObject *
super_repr(PyObject *self)
{
- superobject *su = (superobject *)self;
+ superobject *su = (superobject *)self;
- if (su->obj_type)
- return PyUnicode_FromFormat(
- "<super: <class '%s'>, <%s object>>",
- su->type ? su->type->tp_name : "NULL",
- su->obj_type->tp_name);
- else
- return PyUnicode_FromFormat(
- "<super: <class '%s'>, NULL>",
- su->type ? su->type->tp_name : "NULL");
+ if (su->obj_type)
+ return PyUnicode_FromFormat(
+ "<super: <class '%s'>, <%s object>>",
+ su->type ? su->type->tp_name : "NULL",
+ su->obj_type->tp_name);
+ else
+ return PyUnicode_FromFormat(
+ "<super: <class '%s'>, NULL>",
+ su->type ? su->type->tp_name : "NULL");
}
static PyObject *
super_getattro(PyObject *self, PyObject *name)
{
- superobject *su = (superobject *)self;
- int skip = su->obj_type == NULL;
-
- if (!skip) {
- /* We want __class__ to return the class of the super object
- (i.e. super, or a subclass), not the class of su->obj. */
- skip = (PyUnicode_Check(name) &&
- PyUnicode_GET_SIZE(name) == 9 &&
- PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
- }
-
- if (!skip) {
- PyObject *mro, *res, *tmp, *dict;
- PyTypeObject *starttype;
- descrgetfunc f;
- Py_ssize_t i, n;
-
- starttype = su->obj_type;
- mro = starttype->tp_mro;
-
- if (mro == NULL)
- n = 0;
- else {
- assert(PyTuple_Check(mro));
- n = PyTuple_GET_SIZE(mro);
- }
- for (i = 0; i < n; i++) {
- if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
- break;
- }
- i++;
- res = NULL;
- for (; i < n; i++) {
- tmp = PyTuple_GET_ITEM(mro, i);
- if (PyType_Check(tmp))
- dict = ((PyTypeObject *)tmp)->tp_dict;
- else
- continue;
- res = PyDict_GetItem(dict, name);
- if (res != NULL) {
- Py_INCREF(res);
- f = Py_TYPE(res)->tp_descr_get;
- if (f != NULL) {
- tmp = f(res,
- /* Only pass 'obj' param if
- this is instance-mode super
- (See SF ID #743627)
- */
- (su->obj == (PyObject *)
- su->obj_type
- ? (PyObject *)NULL
- : su->obj),
- (PyObject *)starttype);
- Py_DECREF(res);
- res = tmp;
- }
- return res;
- }
- }
- }
- return PyObject_GenericGetAttr(self, name);
+ superobject *su = (superobject *)self;
+ int skip = su->obj_type == NULL;
+
+ if (!skip) {
+ /* We want __class__ to return the class of the super object
+ (i.e. super, or a subclass), not the class of su->obj. */
+ skip = (PyUnicode_Check(name) &&
+ PyUnicode_GET_SIZE(name) == 9 &&
+ PyUnicode_CompareWithASCIIString(name, "__class__") == 0);
+ }
+
+ if (!skip) {
+ PyObject *mro, *res, *tmp, *dict;
+ PyTypeObject *starttype;
+ descrgetfunc f;
+ Py_ssize_t i, n;
+
+ starttype = su->obj_type;
+ mro = starttype->tp_mro;
+
+ if (mro == NULL)
+ n = 0;
+ else {
+ assert(PyTuple_Check(mro));
+ n = PyTuple_GET_SIZE(mro);
+ }
+ for (i = 0; i < n; i++) {
+ if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
+ break;
+ }
+ i++;
+ res = NULL;
+ for (; i < n; i++) {
+ tmp = PyTuple_GET_ITEM(mro, i);
+ if (PyType_Check(tmp))
+ dict = ((PyTypeObject *)tmp)->tp_dict;
+ else
+ continue;
+ res = PyDict_GetItem(dict, name);
+ if (res != NULL) {
+ Py_INCREF(res);
+ f = Py_TYPE(res)->tp_descr_get;
+ if (f != NULL) {
+ tmp = f(res,
+ /* Only pass 'obj' param if
+ this is instance-mode super
+ (See SF ID #743627)
+ */
+ (su->obj == (PyObject *)
+ su->obj_type
+ ? (PyObject *)NULL
+ : su->obj),
+ (PyObject *)starttype);
+ Py_DECREF(res);
+ res = tmp;
+ }
+ return res;
+ }
+ }
+ }
+ return PyObject_GenericGetAttr(self, name);
}
static PyTypeObject *
supercheck(PyTypeObject *type, PyObject *obj)
{
- /* Check that a super() call makes sense. Return a type object.
-
- obj can be a new-style class, or an instance of one:
-
- - If it is a class, it must be a subclass of 'type'. This case is
- used for class methods; the return value is obj.
-
- - If it is an instance, it must be an instance of 'type'. This is
- the normal case; the return value is obj.__class__.
-
- But... when obj is an instance, we want to allow for the case where
- Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
- This will allow using super() with a proxy for obj.
- */
-
- /* Check for first bullet above (special case) */
- if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
- Py_INCREF(obj);
- return (PyTypeObject *)obj;
- }
-
- /* Normal case */
- if (PyType_IsSubtype(Py_TYPE(obj), type)) {
- Py_INCREF(Py_TYPE(obj));
- return Py_TYPE(obj);
- }
- else {
- /* Try the slow way */
- static PyObject *class_str = NULL;
- PyObject *class_attr;
-
- if (class_str == NULL) {
- class_str = PyUnicode_FromString("__class__");
- if (class_str == NULL)
- return NULL;
- }
-
- class_attr = PyObject_GetAttr(obj, class_str);
-
- if (class_attr != NULL &&
- PyType_Check(class_attr) &&
- (PyTypeObject *)class_attr != Py_TYPE(obj))
- {
- int ok = PyType_IsSubtype(
- (PyTypeObject *)class_attr, type);
- if (ok)
- return (PyTypeObject *)class_attr;
- }
-
- if (class_attr == NULL)
- PyErr_Clear();
- else
- Py_DECREF(class_attr);
- }
-
- PyErr_SetString(PyExc_TypeError,
- "super(type, obj): "
- "obj must be an instance or subtype of type");
- return NULL;
+ /* Check that a super() call makes sense. Return a type object.
+
+ obj can be a new-style class, or an instance of one:
+
+ - If it is a class, it must be a subclass of 'type'. This case is
+ used for class methods; the return value is obj.
+
+ - If it is an instance, it must be an instance of 'type'. This is
+ the normal case; the return value is obj.__class__.
+
+ But... when obj is an instance, we want to allow for the case where
+ Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
+ This will allow using super() with a proxy for obj.
+ */
+
+ /* Check for first bullet above (special case) */
+ if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
+ Py_INCREF(obj);
+ return (PyTypeObject *)obj;
+ }
+
+ /* Normal case */
+ if (PyType_IsSubtype(Py_TYPE(obj), type)) {
+ Py_INCREF(Py_TYPE(obj));
+ return Py_TYPE(obj);
+ }
+ else {
+ /* Try the slow way */
+ static PyObject *class_str = NULL;
+ PyObject *class_attr;
+
+ if (class_str == NULL) {
+ class_str = PyUnicode_FromString("__class__");
+ if (class_str == NULL)
+ return NULL;
+ }
+
+ class_attr = PyObject_GetAttr(obj, class_str);
+
+ if (class_attr != NULL &&
+ PyType_Check(class_attr) &&
+ (PyTypeObject *)class_attr != Py_TYPE(obj))
+ {
+ int ok = PyType_IsSubtype(
+ (PyTypeObject *)class_attr, type);
+ if (ok)
+ return (PyTypeObject *)class_attr;
+ }
+
+ if (class_attr == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(class_attr);
+ }
+
+ PyErr_SetString(PyExc_TypeError,
+ "super(type, obj): "
+ "obj must be an instance or subtype of type");
+ return NULL;
}
static PyObject *
super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
- superobject *su = (superobject *)self;
- superobject *newobj;
-
- if (obj == NULL || obj == Py_None || su->obj != NULL) {
- /* Not binding to an object, or already bound */
- Py_INCREF(self);
- return self;
- }
- if (Py_TYPE(su) != &PySuper_Type)
- /* If su is an instance of a (strict) subclass of super,
- call its type */
- return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
- su->type, obj, NULL);
- else {
- /* Inline the common case */
- PyTypeObject *obj_type = supercheck(su->type, obj);
- if (obj_type == NULL)
- return NULL;
- newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
- NULL, NULL);
- if (newobj == NULL)
- return NULL;
- Py_INCREF(su->type);
- Py_INCREF(obj);
- newobj->type = su->type;
- newobj->obj = obj;
- newobj->obj_type = obj_type;
- return (PyObject *)newobj;
- }
+ superobject *su = (superobject *)self;
+ superobject *newobj;
+
+ if (obj == NULL || obj == Py_None || su->obj != NULL) {
+ /* Not binding to an object, or already bound */
+ Py_INCREF(self);
+ return self;
+ }
+ if (Py_TYPE(su) != &PySuper_Type)
+ /* If su is an instance of a (strict) subclass of super,
+ call its type */
+ return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
+ su->type, obj, NULL);
+ else {
+ /* Inline the common case */
+ PyTypeObject *obj_type = supercheck(su->type, obj);
+ if (obj_type == NULL)
+ return NULL;
+ newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
+ NULL, NULL);
+ if (newobj == NULL)
+ return NULL;
+ Py_INCREF(su->type);
+ Py_INCREF(obj);
+ newobj->type = su->type;
+ newobj->obj = obj;
+ newobj->obj_type = obj_type;
+ return (PyObject *)newobj;
+ }
}
static int
super_init(PyObject *self, PyObject *args, PyObject *kwds)
{
- superobject *su = (superobject *)self;
- PyTypeObject *type = NULL;
- PyObject *obj = NULL;
- PyTypeObject *obj_type = NULL;
-
- if (!_PyArg_NoKeywords("super", kwds))
- return -1;
- if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
- return -1;
-
+ superobject *su = (superobject *)self;
+ PyTypeObject *type = NULL;
+ PyObject *obj = NULL;
+ PyTypeObject *obj_type = NULL;
+
+ if (!_PyArg_NoKeywords("super", kwds))
+ return -1;
+ if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
+ return -1;
+
+ if (type == NULL) {
+ /* Call super(), without args -- fill in from __class__
+ and first local variable on the stack. */
+ PyFrameObject *f = PyThreadState_GET()->frame;
+ PyCodeObject *co = f->f_code;
+ int i, n;
+ if (co == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "super(): no code object");
+ return -1;
+ }
+ if (co->co_argcount == 0) {
+ PyErr_SetString(PyExc_SystemError,
+ "super(): no arguments");
+ return -1;
+ }
+ obj = f->f_localsplus[0];
+ if (obj == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "super(): arg[0] deleted");
+ return -1;
+ }
+ if (co->co_freevars == NULL)
+ n = 0;
+ else {
+ assert(PyTuple_Check(co->co_freevars));
+ n = PyTuple_GET_SIZE(co->co_freevars);
+ }
+ for (i = 0; i < n; i++) {
+ PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
+ assert(PyUnicode_Check(name));
+ if (!PyUnicode_CompareWithASCIIString(name,
+ "__class__")) {
+ Py_ssize_t index = co->co_nlocals +
+ PyTuple_GET_SIZE(co->co_cellvars) + i;
+ PyObject *cell = f->f_localsplus[index];
+ if (cell == NULL || !PyCell_Check(cell)) {
+ PyErr_SetString(PyExc_SystemError,
+ "super(): bad __class__ cell");
+ return -1;
+ }
+ type = (PyTypeObject *) PyCell_GET(cell);
+ if (type == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "super(): empty __class__ cell");
+ return -1;
+ }
+ if (!PyType_Check(type)) {
+ PyErr_Format(PyExc_SystemError,
+ "super(): __class__ is not a type (%s)",
+ Py_TYPE(type)->tp_name);
+ return -1;
+ }
+ break;
+ }
+ }
if (type == NULL) {
- /* Call super(), without args -- fill in from __class__
- and first local variable on the stack. */
- PyFrameObject *f = PyThreadState_GET()->frame;
- PyCodeObject *co = f->f_code;
- int i, n;
- if (co == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "super(): no code object");
- return -1;
- }
- if (co->co_argcount == 0) {
- PyErr_SetString(PyExc_SystemError,
- "super(): no arguments");
- return -1;
- }
- obj = f->f_localsplus[0];
- if (obj == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "super(): arg[0] deleted");
- return -1;
- }
- if (co->co_freevars == NULL)
- n = 0;
- else {
- assert(PyTuple_Check(co->co_freevars));
- n = PyTuple_GET_SIZE(co->co_freevars);
- }
- for (i = 0; i < n; i++) {
- PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
- assert(PyUnicode_Check(name));
- if (!PyUnicode_CompareWithASCIIString(name,
- "__class__")) {
- Py_ssize_t index = co->co_nlocals +
- PyTuple_GET_SIZE(co->co_cellvars) + i;
- PyObject *cell = f->f_localsplus[index];
- if (cell == NULL || !PyCell_Check(cell)) {
- PyErr_SetString(PyExc_SystemError,
- "super(): bad __class__ cell");
- return -1;
- }
- type = (PyTypeObject *) PyCell_GET(cell);
- if (type == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "super(): empty __class__ cell");
- return -1;
- }
- if (!PyType_Check(type)) {
- PyErr_Format(PyExc_SystemError,
- "super(): __class__ is not a type (%s)",
- Py_TYPE(type)->tp_name);
- return -1;
- }
- break;
- }
- }
- if (type == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "super(): __class__ cell not found");
- return -1;
- }
+ PyErr_SetString(PyExc_SystemError,
+ "super(): __class__ cell not found");
+ return -1;
}
-
- if (obj == Py_None)
- obj = NULL;
- if (obj != NULL) {
- obj_type = supercheck(type, obj);
- if (obj_type == NULL)
- return -1;
- Py_INCREF(obj);
- }
- Py_INCREF(type);
- su->type = type;
- su->obj = obj;
- su->obj_type = obj_type;
- return 0;
+ }
+
+ if (obj == Py_None)
+ obj = NULL;
+ if (obj != NULL) {
+ obj_type = supercheck(type, obj);
+ if (obj_type == NULL)
+ return -1;
+ Py_INCREF(obj);
+ }
+ Py_INCREF(type);
+ su->type = type;
+ su->obj = obj;
+ su->obj_type = obj_type;
+ return 0;
}
PyDoc_STRVAR(super_doc,
@@ -6238,65 +6238,65 @@ PyDoc_STRVAR(super_doc,
"Typical use to call a cooperative superclass method:\n"
"class C(B):\n"
" def meth(self, arg):\n"
-" super().meth(arg)\n"
+" super().meth(arg)\n"
"This works for class methods too:\n"
"class C(B):\n"
" @classmethod\n"
" def cmeth(cls, arg):\n"
-" super().cmeth(arg)\n");
+" super().cmeth(arg)\n");
static int
super_traverse(PyObject *self, visitproc visit, void *arg)
{
- superobject *su = (superobject *)self;
+ superobject *su = (superobject *)self;
- Py_VISIT(su->obj);
- Py_VISIT(su->type);
- Py_VISIT(su->obj_type);
+ Py_VISIT(su->obj);
+ Py_VISIT(su->type);
+ Py_VISIT(su->obj_type);
- return 0;
+ return 0;
}
PyTypeObject PySuper_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "super", /* tp_name */
- sizeof(superobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- super_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- super_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- super_getattro, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- super_doc, /* tp_doc */
- super_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- super_members, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- super_descr_get, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- super_init, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "super", /* tp_name */
+ sizeof(superobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ super_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ super_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ super_getattro, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ super_doc, /* tp_doc */
+ super_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ super_members, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ super_descr_get, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ super_init, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ PyType_GenericNew, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
diff --git a/Objects/unicodectype.c b/Objects/unicodectype.c
index 8c710e0..1597bbd 100644
--- a/Objects/unicodectype.c
+++ b/Objects/unicodectype.c
@@ -67,9 +67,9 @@ int _PyUnicode_IsLinebreak(register const Py_UNICODE ch)
case 0x0085: /* NEXT LINE */
case 0x2028: /* LINE SEPARATOR */
case 0x2029: /* PARAGRAPH SEPARATOR */
- return 1;
+ return 1;
default:
- return 0;
+ return 0;
}
}
@@ -82,10 +82,10 @@ Py_UNICODE _PyUnicode_ToTitlecase(register Py_UNICODE ch)
int delta = ctype->title;
if (ctype->flags & NODELTA_MASK)
- return delta;
+ return delta;
if (delta >= 32768)
- delta -= 65536;
+ delta -= 65536;
return ch + delta;
}
@@ -133,7 +133,7 @@ int _PyUnicode_ToDecimalDigit(Py_UNICODE ch)
int _PyUnicode_IsDecimalDigit(Py_UNICODE ch)
{
if (_PyUnicode_ToDecimalDigit(ch) < 0)
- return 0;
+ return 0;
return 1;
}
@@ -150,7 +150,7 @@ int _PyUnicode_ToDigit(Py_UNICODE ch)
int _PyUnicode_IsDigit(Py_UNICODE ch)
{
if (_PyUnicode_ToDigit(ch) < 0)
- return 0;
+ return 0;
return 1;
}
@@ -169,7 +169,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1018A:
#endif
- return (double) 0;
+ return (double) 0;
case 0x09F4:
case 0x17F1:
case 0x215F:
@@ -188,7 +188,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10320:
case 0x103D1:
#endif
- return (double) 1;
+ return (double) 1;
case 0x00BD:
case 0x0F2A:
case 0x2CFD:
@@ -197,20 +197,20 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10175:
case 0x10176:
#endif
- return (double) 1 / 2;
+ return (double) 1 / 2;
case 0x2153:
- return (double) 1 / 3;
+ return (double) 1 / 3;
case 0x00BC:
#ifdef Py_UNICODE_WIDE
case 0x10140:
#endif
- return (double) 1 / 4;
+ return (double) 1 / 4;
case 0x2155:
- return (double) 1 / 5;
+ return (double) 1 / 5;
case 0x2159:
- return (double) 1 / 6;
+ return (double) 1 / 6;
case 0x215B:
- return (double) 1 / 8;
+ return (double) 1 / 8;
case 0x0BF0:
case 0x1372:
case 0x2169:
@@ -239,7 +239,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x103D3:
case 0x10A44:
#endif
- return (double) 10;
+ return (double) 10;
case 0x0BF1:
case 0x137B:
case 0x216D:
@@ -252,7 +252,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x103D5:
case 0x10A46:
#endif
- return (double) 100;
+ return (double) 100;
case 0x0BF2:
case 0x216F:
case 0x217F:
@@ -264,21 +264,21 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10171:
case 0x10A47:
#endif
- return (double) 1000;
+ return (double) 1000;
case 0x137C:
case 0x2182:
#ifdef Py_UNICODE_WIDE
case 0x1012B:
case 0x10155:
#endif
- return (double) 10000;
+ return (double) 10000;
case 0x216A:
case 0x217A:
case 0x246A:
case 0x247E:
case 0x2492:
case 0x24EB:
- return (double) 11;
+ return (double) 11;
case 0x0F2F:
return (double) 11 / 2;
case 0x216B:
@@ -287,24 +287,24 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x247F:
case 0x2493:
case 0x24EC:
- return (double) 12;
+ return (double) 12;
case 0x246C:
case 0x2480:
case 0x2494:
case 0x24ED:
- return (double) 13;
+ return (double) 13;
case 0x0F30:
return (double) 13 / 2;
case 0x246D:
case 0x2481:
case 0x2495:
case 0x24EE:
- return (double) 14;
+ return (double) 14;
case 0x246E:
case 0x2482:
case 0x2496:
case 0x24EF:
- return (double) 15;
+ return (double) 15;
case 0x0F31:
return (double) 15 / 2;
case 0x09F9:
@@ -312,13 +312,13 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x2483:
case 0x2497:
case 0x24F0:
- return (double) 16;
+ return (double) 16;
case 0x16EE:
case 0x2470:
case 0x2484:
case 0x2498:
case 0x24F1:
- return (double) 17;
+ return (double) 17;
case 0x0F32:
return (double) 17 / 2;
case 0x16EF:
@@ -326,13 +326,13 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x2485:
case 0x2499:
case 0x24F2:
- return (double) 18;
+ return (double) 18;
case 0x16F0:
case 0x2472:
case 0x2486:
case 0x249A:
case 0x24F3:
- return (double) 19;
+ return (double) 19;
case 0x09F5:
case 0x17F2:
case 0x2161:
@@ -349,12 +349,12 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x1015E:
case 0x103D2:
#endif
- return (double) 2;
+ return (double) 2;
case 0x2154:
#ifdef Py_UNICODE_WIDE
case 0x10177:
#endif
- return (double) 2 / 3;
+ return (double) 2 / 3;
case 0x2156:
return (double) 2 / 5;
case 0x1373:
@@ -406,18 +406,18 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x10109:
#endif
- return (double) 3;
+ return (double) 3;
case 0x0F2B:
return (double) 3 / 2;
case 0x00BE:
#ifdef Py_UNICODE_WIDE
case 0x10178:
#endif
- return (double) 3 / 4;
+ return (double) 3 / 4;
case 0x2157:
- return (double) 3 / 5;
+ return (double) 3 / 5;
case 0x215C:
- return (double) 3 / 8;
+ return (double) 3 / 8;
case 0x1374:
case 0x303A:
case 0x325A:
@@ -425,7 +425,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10112:
case 0x10165:
#endif
- return (double) 30;
+ return (double) 30;
#ifdef Py_UNICODE_WIDE
case 0x1011B:
case 0x1016B:
@@ -464,9 +464,9 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1010A:
#endif
- return (double) 4;
+ return (double) 4;
case 0x2158:
- return (double) 4 / 5;
+ return (double) 4 / 5;
case 0x1375:
case 0x32B5:
#ifdef Py_UNICODE_WIDE
@@ -514,13 +514,13 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10173:
case 0x10321:
#endif
- return (double) 5;
+ return (double) 5;
case 0x0F2C:
return (double) 5 / 2;
case 0x215A:
- return (double) 5 / 6;
+ return (double) 5 / 6;
case 0x215D:
- return (double) 5 / 8;
+ return (double) 5 / 8;
case 0x1376:
case 0x216C:
case 0x217C:
@@ -537,7 +537,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x10174:
case 0x10323:
#endif
- return (double) 50;
+ return (double) 50;
case 0x216E:
case 0x217E:
#ifdef Py_UNICODE_WIDE
@@ -551,7 +551,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x1016F:
case 0x10170:
#endif
- return (double) 500;
+ return (double) 500;
case 0x2181:
#ifdef Py_UNICODE_WIDE
case 0x10126:
@@ -559,7 +559,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
case 0x1014E:
case 0x10172:
#endif
- return (double) 5000;
+ return (double) 5000;
#ifdef Py_UNICODE_WIDE
case 0x1012F:
case 0x10147:
@@ -575,12 +575,12 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1010C:
#endif
- return (double) 6;
+ return (double) 6;
case 0x1377:
#ifdef Py_UNICODE_WIDE
case 0x10115:
#endif
- return (double) 60;
+ return (double) 60;
#ifdef Py_UNICODE_WIDE
case 0x1011E:
return (double) 600;
@@ -598,16 +598,16 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1010D:
#endif
- return (double) 7;
+ return (double) 7;
case 0x0F2D:
return (double) 7 / 2;
case 0x215E:
- return (double) 7 / 8;
+ return (double) 7 / 8;
case 0x1378:
#ifdef Py_UNICODE_WIDE
case 0x10116:
#endif
- return (double) 70;
+ return (double) 70;
#ifdef Py_UNICODE_WIDE
case 0x1011F:
return (double) 700;
@@ -625,12 +625,12 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1010E:
#endif
- return (double) 8;
+ return (double) 8;
case 0x1379:
#ifdef Py_UNICODE_WIDE
case 0x10117:
#endif
- return (double) 80;
+ return (double) 80;
#ifdef Py_UNICODE_WIDE
case 0x10120:
return (double) 800;
@@ -648,14 +648,14 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
#ifdef Py_UNICODE_WIDE
case 0x1010F:
#endif
- return (double) 9;
+ return (double) 9;
case 0x0F2E:
return (double) 9 / 2;
case 0x137A:
#ifdef Py_UNICODE_WIDE
case 0x10118:
#endif
- return (double) 90;
+ return (double) 90;
#ifdef Py_UNICODE_WIDE
case 0x10121:
case 0x1034A:
@@ -666,7 +666,7 @@ double _PyUnicode_ToNumeric(Py_UNICODE ch)
return (double) 90000;
#endif
default:
- return (double) _PyUnicode_ToDigit(ch);
+ return (double) _PyUnicode_ToDigit(ch);
}
}
@@ -733,9 +733,9 @@ int _PyUnicode_IsWhitespace(register const Py_UNICODE ch)
case 0x202F: /* NARROW NO-BREAK SPACE */
case 0x205F: /* MEDIUM MATHEMATICAL SPACE */
case 0x3000: /* IDEOGRAPHIC SPACE */
- return 1;
+ return 1;
default:
- return 0;
+ return 0;
}
}
@@ -767,9 +767,9 @@ Py_UNICODE _PyUnicode_ToUppercase(Py_UNICODE ch)
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
int delta = ctype->upper;
if (ctype->flags & NODELTA_MASK)
- return delta;
+ return delta;
if (delta >= 32768)
- delta -= 65536;
+ delta -= 65536;
return ch + delta;
}
@@ -781,9 +781,9 @@ Py_UNICODE _PyUnicode_ToLowercase(Py_UNICODE ch)
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);
int delta = ctype->lower;
if (ctype->flags & NODELTA_MASK)
- return delta;
+ return delta;
if (delta >= 32768)
- delta -= 65536;
+ delta -= 65536;
return ch + delta;
}
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index 27f015d..f43b68d 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -57,9 +57,9 @@ clear_weakref(PyWeakReference *self)
PyWeakref_GET_OBJECT(self));
if (*list == self)
- /* If 'self' is the end of the list (and thus self->wr_next == NULL)
- then the weakref list itself (and thus the value of *list) will
- end up being set to NULL. */
+ /* If 'self' is the end of the list (and thus self->wr_next == NULL)
+ then the weakref list itself (and thus the value of *list) will
+ end up being set to NULL. */
*list = self->wr_next;
self->wr_object = Py_None;
if (self->wr_prev != NULL)
@@ -161,21 +161,21 @@ weakref_repr(PyWeakReference *self)
PyOS_snprintf(buffer, sizeof(buffer), "<weakref at %p; dead>", self);
}
else {
- char *name = NULL;
- PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
- "__name__");
- if (nameobj == NULL)
- PyErr_Clear();
- else if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+ char *name = NULL;
+ PyObject *nameobj = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self),
+ "__name__");
+ if (nameobj == NULL)
+ PyErr_Clear();
+ else if (PyUnicode_Check(nameobj))
+ name = _PyUnicode_AsString(nameobj);
PyOS_snprintf(buffer, sizeof(buffer),
- name ? "<weakref at %p; to '%.50s' at %p (%s)>"
- : "<weakref at %p; to '%.50s' at %p>",
- self,
- Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
- PyWeakref_GET_OBJECT(self),
- name);
- Py_XDECREF(nameobj);
+ name ? "<weakref at %p; to '%.50s' at %p (%s)>"
+ : "<weakref at %p; to '%.50s' at %p>",
+ self,
+ Py_TYPE(PyWeakref_GET_OBJECT(self))->tp_name,
+ PyWeakref_GET_OBJECT(self),
+ name);
+ Py_XDECREF(nameobj);
}
return PyUnicode_FromString(buffer);
}
@@ -188,8 +188,8 @@ static PyObject *
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
{
if ((op != Py_EQ && op != Py_NE) ||
- !PyWeakref_Check(self) ||
- !PyWeakref_Check(other)) {
+ !PyWeakref_Check(self) ||
+ !PyWeakref_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
@@ -339,10 +339,10 @@ _PyWeakref_RefType = {
sizeof(PyWeakReference),
0,
weakref_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
+ 0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
- 0, /*tp_reserved*/
+ 0, /*tp_reserved*/
(reprfunc)weakref_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
@@ -358,7 +358,7 @@ _PyWeakref_RefType = {
0, /*tp_doc*/
(traverseproc)gc_traverse, /*tp_traverse*/
(inquiry)gc_clear, /*tp_clear*/
- (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
+ (richcmpfunc)weakref_richcompare, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
@@ -438,9 +438,9 @@ proxy_checkref(PyWeakReference *proxy)
#define WRAP_METHOD(method, special) \
static PyObject * \
method(PyObject *proxy) { \
- UNWRAP(proxy); \
- return PyObject_CallMethod(proxy, special, ""); \
- }
+ UNWRAP(proxy); \
+ return PyObject_CallMethod(proxy, special, ""); \
+ }
/* direct slots */
@@ -454,9 +454,9 @@ proxy_repr(PyWeakReference *proxy)
{
char buf[160];
PyOS_snprintf(buf, sizeof(buf),
- "<weakproxy at %p to %.100s at %p>", proxy,
- Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
- PyWeakref_GET_OBJECT(proxy));
+ "<weakproxy at %p to %.100s at %p>", proxy,
+ Py_TYPE(PyWeakref_GET_OBJECT(proxy))->tp_name,
+ PyWeakref_GET_OBJECT(proxy));
return PyUnicode_FromString(buf);
}
@@ -587,8 +587,8 @@ WRAP_METHOD(proxy_bytes, "__bytes__");
static PyMethodDef proxy_methods[] = {
- {"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
- {NULL, NULL}
+ {"__bytes__", (PyCFunction)proxy_bytes, METH_NOARGS},
+ {NULL, NULL}
};
@@ -636,7 +636,7 @@ static PySequenceMethods proxy_as_sequence = {
0, /*sq_item*/
0, /*sq_slice*/
0, /*sq_ass_item*/
- 0, /*sq_ass_slice*/
+ 0, /*sq_ass_slice*/
(objobjproc)proxy_contains, /* sq_contains */
};
@@ -655,20 +655,20 @@ _PyWeakref_ProxyType = {
0,
/* methods */
(destructor)proxy_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)proxy_repr, /* tp_repr */
- &proxy_as_number, /* tp_as_number */
- &proxy_as_sequence, /* tp_as_sequence */
- &proxy_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)proxy_repr, /* tp_repr */
+ &proxy_as_number, /* tp_as_number */
+ &proxy_as_sequence, /* tp_as_sequence */
+ &proxy_as_mapping, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
proxy_str, /* tp_str */
proxy_getattr, /* tp_getattro */
(setattrofunc)proxy_setattr, /* tp_setattro */
- 0, /* tp_as_buffer */
+ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
(traverseproc)gc_traverse, /* tp_traverse */
@@ -677,7 +677,7 @@ _PyWeakref_ProxyType = {
0, /* tp_weaklistoffset */
(getiterfunc)proxy_iter, /* tp_iter */
(iternextfunc)proxy_iternext, /* tp_iternext */
- proxy_methods, /* tp_methods */
+ proxy_methods, /* tp_methods */
};
@@ -689,20 +689,20 @@ _PyWeakref_CallableProxyType = {
0,
/* methods */
(destructor)proxy_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (unaryfunc)proxy_repr, /* tp_repr */
- &proxy_as_number, /* tp_as_number */
- &proxy_as_sequence, /* tp_as_sequence */
- &proxy_as_mapping, /* tp_as_mapping */
- 0, /* tp_hash */
- proxy_call, /* tp_call */
- proxy_str, /* tp_str */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (unaryfunc)proxy_repr, /* tp_repr */
+ &proxy_as_number, /* tp_as_number */
+ &proxy_as_sequence, /* tp_as_sequence */
+ &proxy_as_mapping, /* tp_as_mapping */
+ 0, /* tp_hash */
+ proxy_call, /* tp_call */
+ proxy_str, /* tp_str */
proxy_getattr, /* tp_getattro */
(setattrofunc)proxy_setattr, /* tp_setattro */
- 0, /* tp_as_buffer */
+ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
(traverseproc)gc_traverse, /* tp_traverse */
@@ -724,7 +724,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
PyErr_Format(PyExc_TypeError,
- "cannot create weak reference to '%s' object",
+ "cannot create weak reference to '%s' object",
Py_TYPE(ob)->tp_name);
return NULL;
}
@@ -783,7 +783,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
if (!PyType_SUPPORTS_WEAKREFS(Py_TYPE(ob))) {
PyErr_Format(PyExc_TypeError,
- "cannot create weak reference to '%s' object",
+ "cannot create weak reference to '%s' object",
Py_TYPE(ob)->tp_name);
return NULL;
}
@@ -908,7 +908,7 @@ PyObject_ClearWeakRefs(PyObject *object)
else {
PyObject *tuple;
Py_ssize_t i = 0;
-
+
tuple = PyTuple_New(count * 2);
if (tuple == NULL) {
if (restore_error)
diff --git a/PC/VS7.1/make_buildinfo.c b/PC/VS7.1/make_buildinfo.c
index 27580bb..2aa2b66 100644
--- a/PC/VS7.1/make_buildinfo.c
+++ b/PC/VS7.1/make_buildinfo.c
@@ -21,72 +21,72 @@
int make_buildinfo2()
{
- struct _stat st;
- HKEY hTortoise;
- char command[500];
- DWORD type, size;
- if (_stat(".svn", &st) < 0)
- return 0;
- /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
- if (_stat("no_subwcrev", &st) == 0)
- return 0;
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
- RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
- /* Tortoise not installed */
- return 0;
- command[0] = '"'; /* quote the path to the executable */
- size = sizeof(command) - 1;
- if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
- type != REG_SZ)
- /* Registry corrupted */
- return 0;
- strcat(command, "bin\\subwcrev.exe");
- if (_stat(command+1, &st) < 0)
- /* subwcrev.exe not part of the release */
- return 0;
- strcat(command, "\" ..\\.. ..\\..\\Modules\\getbuildinfo.c getbuildinfo2.c");
- puts(command); fflush(stdout);
- if (system(command) < 0)
- return 0;
- return 1;
+ struct _stat st;
+ HKEY hTortoise;
+ char command[500];
+ DWORD type, size;
+ if (_stat(".svn", &st) < 0)
+ return 0;
+ /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
+ if (_stat("no_subwcrev", &st) == 0)
+ return 0;
+ if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
+ RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
+ /* Tortoise not installed */
+ return 0;
+ command[0] = '"'; /* quote the path to the executable */
+ size = sizeof(command) - 1;
+ if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
+ type != REG_SZ)
+ /* Registry corrupted */
+ return 0;
+ strcat(command, "bin\\subwcrev.exe");
+ if (_stat(command+1, &st) < 0)
+ /* subwcrev.exe not part of the release */
+ return 0;
+ strcat(command, "\" ..\\.. ..\\..\\Modules\\getbuildinfo.c getbuildinfo2.c");
+ puts(command); fflush(stdout);
+ if (system(command) < 0)
+ return 0;
+ return 1;
}
int main(int argc, char*argv[])
{
- char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
- int do_unlink, result;
- if (argc != 2) {
- fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
- return EXIT_FAILURE;
- }
- if (strcmp(argv[1], "Release") == 0) {
- strcat(command, "-MD ");
- }
- else if (strcmp(argv[1], "Debug") == 0) {
- strcat(command, "-D_DEBUG -MDd ");
- }
- else if (strcmp(argv[1], "ReleaseItanium") == 0) {
- strcat(command, "-MD /USECL:MS_ITANIUM ");
- }
- else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
- strcat(command, "-MD ");
- strcat(command, "-MD /USECL:MS_OPTERON ");
- }
- else {
- fprintf(stderr, "unsupported configuration %s\n", argv[1]);
- return EXIT_FAILURE;
- }
+ char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
+ int do_unlink, result;
+ if (argc != 2) {
+ fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
+ return EXIT_FAILURE;
+ }
+ if (strcmp(argv[1], "Release") == 0) {
+ strcat(command, "-MD ");
+ }
+ else if (strcmp(argv[1], "Debug") == 0) {
+ strcat(command, "-D_DEBUG -MDd ");
+ }
+ else if (strcmp(argv[1], "ReleaseItanium") == 0) {
+ strcat(command, "-MD /USECL:MS_ITANIUM ");
+ }
+ else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
+ strcat(command, "-MD ");
+ strcat(command, "-MD /USECL:MS_OPTERON ");
+ }
+ else {
+ fprintf(stderr, "unsupported configuration %s\n", argv[1]);
+ return EXIT_FAILURE;
+ }
- if ((do_unlink = make_buildinfo2()))
- strcat(command, "getbuildinfo2.c -DSUBWCREV ");
- else
- strcat(command, "..\\..\\Modules\\getbuildinfo.c");
- strcat(command, " -Fogetbuildinfo.o -I..\\..\\Include -I..\\..\\PC");
- puts(command); fflush(stdout);
- result = system(command);
- if (do_unlink)
- unlink("getbuildinfo2.c");
- if (result < 0)
- return EXIT_FAILURE;
- return 0;
+ if ((do_unlink = make_buildinfo2()))
+ strcat(command, "getbuildinfo2.c -DSUBWCREV ");
+ else
+ strcat(command, "..\\..\\Modules\\getbuildinfo.c");
+ strcat(command, " -Fogetbuildinfo.o -I..\\..\\Include -I..\\..\\PC");
+ puts(command); fflush(stdout);
+ result = system(command);
+ if (do_unlink)
+ unlink("getbuildinfo2.c");
+ if (result < 0)
+ return EXIT_FAILURE;
+ return 0;
}
diff --git a/PC/VS8.0/make_buildinfo.c b/PC/VS8.0/make_buildinfo.c
index 133cfee..8cc3da2 100644
--- a/PC/VS8.0/make_buildinfo.c
+++ b/PC/VS8.0/make_buildinfo.c
@@ -23,72 +23,72 @@
int make_buildinfo2()
{
- struct _stat st;
- HKEY hTortoise;
- char command[CMD_SIZE+1];
- DWORD type, size;
- if (_stat(".svn", &st) < 0)
- return 0;
- /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
- if (_stat("no_subwcrev", &st) == 0)
- return 0;
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
- RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
- /* Tortoise not installed */
- return 0;
- command[0] = '"'; /* quote the path to the executable */
- size = sizeof(command) - 1;
- if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
- type != REG_SZ)
- /* Registry corrupted */
- return 0;
- strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
- if (_stat(command+1, &st) < 0)
- /* subwcrev.exe not part of the release */
- return 0;
- strcat_s(command, CMD_SIZE, "\" ..\\.. ..\\..\\Modules\\getbuildinfo.c getbuildinfo2.c");
- puts(command); fflush(stdout);
- if (system(command) < 0)
- return 0;
- return 1;
+ struct _stat st;
+ HKEY hTortoise;
+ char command[CMD_SIZE+1];
+ DWORD type, size;
+ if (_stat(".svn", &st) < 0)
+ return 0;
+ /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
+ if (_stat("no_subwcrev", &st) == 0)
+ return 0;
+ if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
+ RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
+ /* Tortoise not installed */
+ return 0;
+ command[0] = '"'; /* quote the path to the executable */
+ size = sizeof(command) - 1;
+ if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
+ type != REG_SZ)
+ /* Registry corrupted */
+ return 0;
+ strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
+ if (_stat(command+1, &st) < 0)
+ /* subwcrev.exe not part of the release */
+ return 0;
+ strcat_s(command, CMD_SIZE, "\" ..\\.. ..\\..\\Modules\\getbuildinfo.c getbuildinfo2.c");
+ puts(command); fflush(stdout);
+ if (system(command) < 0)
+ return 0;
+ return 1;
}
int main(int argc, char*argv[])
{
- char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
- int do_unlink, result;
- if (argc != 2) {
- fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
- return EXIT_FAILURE;
- }
- if (strcmp(argv[1], "Release") == 0) {
- strcat_s(command, CMD_SIZE, "-MD ");
- }
- else if (strcmp(argv[1], "Debug") == 0) {
- strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
- }
- else if (strcmp(argv[1], "ReleaseItanium") == 0) {
- strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
- }
- else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
- strcat_s(command, CMD_SIZE, "-MD ");
- strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
- }
- else {
- fprintf(stderr, "unsupported configuration %s\n", argv[1]);
- return EXIT_FAILURE;
- }
+ char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
+ int do_unlink, result;
+ if (argc != 2) {
+ fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
+ return EXIT_FAILURE;
+ }
+ if (strcmp(argv[1], "Release") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD ");
+ }
+ else if (strcmp(argv[1], "Debug") == 0) {
+ strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
+ }
+ else if (strcmp(argv[1], "ReleaseItanium") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
+ }
+ else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD ");
+ strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
+ }
+ else {
+ fprintf(stderr, "unsupported configuration %s\n", argv[1]);
+ return EXIT_FAILURE;
+ }
- if ((do_unlink = make_buildinfo2()))
- strcat_s(command, CMD_SIZE, "getbuildinfo2.c -DSUBWCREV ");
- else
- strcat_s(command, CMD_SIZE, "..\\..\\Modules\\getbuildinfo.c");
- strcat_s(command, CMD_SIZE, " -Fogetbuildinfo.o -I..\\..\\Include -I..\\..\\PC");
- puts(command); fflush(stdout);
- result = system(command);
- if (do_unlink)
- _unlink("getbuildinfo2.c");
- if (result < 0)
- return EXIT_FAILURE;
- return 0;
-} \ No newline at end of file
+ if ((do_unlink = make_buildinfo2()))
+ strcat_s(command, CMD_SIZE, "getbuildinfo2.c -DSUBWCREV ");
+ else
+ strcat_s(command, CMD_SIZE, "..\\..\\Modules\\getbuildinfo.c");
+ strcat_s(command, CMD_SIZE, " -Fogetbuildinfo.o -I..\\..\\Include -I..\\..\\PC");
+ puts(command); fflush(stdout);
+ result = system(command);
+ if (do_unlink)
+ _unlink("getbuildinfo2.c");
+ if (result < 0)
+ return EXIT_FAILURE;
+ return 0;
+}
diff --git a/PC/_msi.c b/PC/_msi.c
index 0df2397..26f3f33 100644
--- a/PC/_msi.c
+++ b/PC/_msi.c
@@ -20,19 +20,19 @@ uuidcreate(PyObject* obj, PyObject*args)
UUID result;
unsigned short *cresult;
PyObject *oresult;
-
+
/* May return ok, local only, and no address.
For local only, the documentation says we still get a uuid.
For RPC_S_UUID_NO_ADDRESS, it's not clear whether we can
use the result. */
if (UuidCreate(&result) == RPC_S_UUID_NO_ADDRESS) {
- PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
- return NULL;
+ PyErr_SetString(PyExc_NotImplementedError, "processing 'no address' result");
+ return NULL;
}
if (UuidToStringW(&result, &cresult) == RPC_S_OUT_OF_MEMORY) {
- PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
- return NULL;
+ PyErr_SetString(PyExc_MemoryError, "out of memory in uuidgen");
+ return NULL;
}
oresult = PyUnicode_FromUnicode(cresult, wcslen(cresult));
@@ -57,7 +57,7 @@ static FNFCIOPEN(cb_open)
{
int result = _open(pszFile, oflag, pmode);
if (result == -1)
- *err = errno;
+ *err = errno;
return result;
}
@@ -65,7 +65,7 @@ static FNFCIREAD(cb_read)
{
UINT result = (UINT)_read(hf, memory, cb);
if (result != cb)
- *err = errno;
+ *err = errno;
return result;
}
@@ -73,7 +73,7 @@ static FNFCIWRITE(cb_write)
{
UINT result = (UINT)_write(hf, memory, cb);
if (result != cb)
- *err = errno;
+ *err = errno;
return result;
}
@@ -81,7 +81,7 @@ static FNFCICLOSE(cb_close)
{
int result = _close(hf);
if (result != 0)
- *err = errno;
+ *err = errno;
return result;
}
@@ -89,7 +89,7 @@ static FNFCISEEK(cb_seek)
{
long result = (long)_lseek(hf, dist, seektype);
if (result == -1)
- *err = errno;
+ *err = errno;
return result;
}
@@ -97,7 +97,7 @@ static FNFCIDELETE(cb_delete)
{
int result = remove(pszFile);
if (result != 0)
- *err = errno;
+ *err = errno;
return result;
}
@@ -110,9 +110,9 @@ static FNFCIGETTEMPFILE(cb_gettempfile)
{
char *name = _tempnam("", "tmp");
if ((name != NULL) && ((int)strlen(name) < cbTempName)) {
- strcpy(pszTempName, name);
- free(name);
- return TRUE;
+ strcpy(pszTempName, name);
+ free(name);
+ return TRUE;
}
if (name) free(name);
@@ -122,10 +122,10 @@ static FNFCIGETTEMPFILE(cb_gettempfile)
static FNFCISTATUS(cb_status)
{
if (pv) {
- PyObject *result = PyObject_CallMethod(pv, "status", "iii", typeStatus, cb1, cb2);
- if (result == NULL)
- return -1;
- Py_DECREF(result);
+ PyObject *result = PyObject_CallMethod(pv, "status", "iii", typeStatus, cb1, cb2);
+ if (result == NULL)
+ return -1;
+ Py_DECREF(result);
}
return 0;
}
@@ -133,18 +133,18 @@ static FNFCISTATUS(cb_status)
static FNFCIGETNEXTCABINET(cb_getnextcabinet)
{
if (pv) {
- PyObject *result = PyObject_CallMethod(pv, "getnextcabinet", "i", pccab->iCab);
- if (result == NULL)
- return -1;
- if (!PyBytes_Check(result)) {
- PyErr_Format(PyExc_TypeError,
- "Incorrect return type %s from getnextcabinet",
- result->ob_type->tp_name);
- Py_DECREF(result);
- return FALSE;
- }
- strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab));
- return TRUE;
+ PyObject *result = PyObject_CallMethod(pv, "getnextcabinet", "i", pccab->iCab);
+ if (result == NULL)
+ return -1;
+ if (!PyBytes_Check(result)) {
+ PyErr_Format(PyExc_TypeError,
+ "Incorrect return type %s from getnextcabinet",
+ result->ob_type->tp_name);
+ Py_DECREF(result);
+ return FALSE;
+ }
+ strncpy(pccab->szCab, PyBytes_AsString(result), sizeof(pccab->szCab));
+ return TRUE;
}
return FALSE;
}
@@ -157,21 +157,21 @@ static FNFCIGETOPENINFO(cb_getopeninfo)
/* Need Win32 handle to get time stamps */
handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
- return -1;
+ return -1;
if (GetFileInformationByHandle(handle, &bhfi) == FALSE)
{
- CloseHandle(handle);
- return -1;
+ CloseHandle(handle);
+ return -1;
}
FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime);
FileTimeToDosDateTime(&filetime, pdate, ptime);
- *pattribs = (int)(bhfi.dwFileAttributes &
- (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
+ *pattribs = (int)(bhfi.dwFileAttributes &
+ (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
CloseHandle(handle);
@@ -189,11 +189,11 @@ static PyObject* fcicreate(PyObject* obj, PyObject* args)
if (!PyArg_ParseTuple(args, "sO:FCICreate", &cabname, &files))
- return NULL;
+ return NULL;
if (!PyList_Check(files)) {
- PyErr_SetString(PyExc_TypeError, "FCICreate expects a list");
- return NULL;
+ PyErr_SetString(PyExc_TypeError, "FCICreate expects a list");
+ return NULL;
}
ccab.cb = INT_MAX; /* no need to split CAB into multiple media */
@@ -209,49 +209,49 @@ static PyObject* fcicreate(PyObject* obj, PyObject* args)
ccab.szDisk[0] = '\0';
for (i = 0, p = cabname; *p; p = CharNext(p))
- if (*p == '\\' || *p == '/')
- i = p - cabname + 1;
+ if (*p == '\\' || *p == '/')
+ i = p - cabname + 1;
if (i >= sizeof(ccab.szCabPath) ||
- strlen(cabname+i) >= sizeof(ccab.szCab)) {
- PyErr_SetString(PyExc_ValueError, "path name too long");
- return 0;
+ strlen(cabname+i) >= sizeof(ccab.szCab)) {
+ PyErr_SetString(PyExc_ValueError, "path name too long");
+ return 0;
}
if (i > 0) {
- memcpy(ccab.szCabPath, cabname, i);
- ccab.szCabPath[i] = '\0';
- strcpy(ccab.szCab, cabname+i);
+ memcpy(ccab.szCabPath, cabname, i);
+ ccab.szCabPath[i] = '\0';
+ strcpy(ccab.szCab, cabname+i);
} else {
- strcpy(ccab.szCabPath, ".\\");
- strcpy(ccab.szCab, cabname);
+ strcpy(ccab.szCabPath, ".\\");
+ strcpy(ccab.szCab, cabname);
}
hfci = FCICreate(&erf, cb_fileplaced, cb_alloc, cb_free,
- cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete,
- cb_gettempfile, &ccab, NULL);
+ cb_open, cb_read, cb_write, cb_close, cb_seek, cb_delete,
+ cb_gettempfile, &ccab, NULL);
if (hfci == NULL) {
- PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper);
- return NULL;
+ PyErr_Format(PyExc_ValueError, "FCI error %d", erf.erfOper);
+ return NULL;
}
for (i=0; i < PyList_GET_SIZE(files); i++) {
- PyObject *item = PyList_GET_ITEM(files, i);
- char *filename, *cabname;
- if (!PyArg_ParseTuple(item, "ss", &filename, &cabname))
- goto err;
- if (!FCIAddFile(hfci, filename, cabname, FALSE,
- cb_getnextcabinet, cb_status, cb_getopeninfo,
- tcompTYPE_MSZIP))
- goto err;
+ PyObject *item = PyList_GET_ITEM(files, i);
+ char *filename, *cabname;
+ if (!PyArg_ParseTuple(item, "ss", &filename, &cabname))
+ goto err;
+ if (!FCIAddFile(hfci, filename, cabname, FALSE,
+ cb_getnextcabinet, cb_status, cb_getopeninfo,
+ tcompTYPE_MSZIP))
+ goto err;
}
if (!FCIFlushCabinet(hfci, FALSE, cb_getnextcabinet, cb_status))
- goto err;
+ goto err;
if (!FCIDestroy(hfci))
- goto err;
+ goto err;
Py_INCREF(Py_None);
return Py_None;
@@ -266,7 +266,7 @@ typedef struct msiobj{
MSIHANDLE h;
}msiobj;
-static void
+static void
msiobj_dealloc(msiobj* msidb)
{
MsiCloseHandle(msidb->h);
@@ -292,41 +292,41 @@ msierror(int status)
MSIHANDLE err = MsiGetLastErrorRecord();
if (err == 0) {
- switch(status) {
- case ERROR_ACCESS_DENIED:
- PyErr_SetString(MSIError, "access denied");
- return NULL;
- case ERROR_FUNCTION_FAILED:
- PyErr_SetString(MSIError, "function failed");
- return NULL;
- case ERROR_INVALID_DATA:
- PyErr_SetString(MSIError, "invalid data");
- return NULL;
- case ERROR_INVALID_HANDLE:
- PyErr_SetString(MSIError, "invalid handle");
- return NULL;
- case ERROR_INVALID_STATE:
- PyErr_SetString(MSIError, "invalid state");
- return NULL;
- case ERROR_INVALID_PARAMETER:
- PyErr_SetString(MSIError, "invalid parameter");
- return NULL;
- default:
- PyErr_Format(MSIError, "unknown error %x", status);
- return NULL;
- }
+ switch(status) {
+ case ERROR_ACCESS_DENIED:
+ PyErr_SetString(MSIError, "access denied");
+ return NULL;
+ case ERROR_FUNCTION_FAILED:
+ PyErr_SetString(MSIError, "function failed");
+ return NULL;
+ case ERROR_INVALID_DATA:
+ PyErr_SetString(MSIError, "invalid data");
+ return NULL;
+ case ERROR_INVALID_HANDLE:
+ PyErr_SetString(MSIError, "invalid handle");
+ return NULL;
+ case ERROR_INVALID_STATE:
+ PyErr_SetString(MSIError, "invalid state");
+ return NULL;
+ case ERROR_INVALID_PARAMETER:
+ PyErr_SetString(MSIError, "invalid parameter");
+ return NULL;
+ default:
+ PyErr_Format(MSIError, "unknown error %x", status);
+ return NULL;
+ }
}
code = MsiRecordGetInteger(err, 1); /* XXX code */
if (MsiFormatRecord(0, err, res, &size) == ERROR_MORE_DATA) {
- res = malloc(size+1);
- MsiFormatRecord(0, err, res, &size);
- res[size]='\0';
+ res = malloc(size+1);
+ MsiFormatRecord(0, err, res, &size);
+ res[size]='\0';
}
MsiCloseHandle(err);
PyErr_SetString(MSIError, res);
if (res != buf)
- free(res);
+ free(res);
return NULL;
}
@@ -343,7 +343,7 @@ record_getinteger(msiobj* record, PyObject* args)
{
unsigned int field;
int status;
-
+
if (!PyArg_ParseTuple(args, "I:GetInteger", &field))
return NULL;
status = MsiRecordGetInteger(record->h, field);
@@ -363,7 +363,7 @@ record_getstring(msiobj* record, PyObject* args)
WCHAR *res = buf;
DWORD size = sizeof(buf);
PyObject* string;
-
+
if (!PyArg_ParseTuple(args, "I:GetString", &field))
return NULL;
status = MsiRecordGetStringW(record->h, field, res, &size);
@@ -386,7 +386,7 @@ record_cleardata(msiobj* record, PyObject *args)
{
int status = MsiRecordClearData(record->h);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -400,10 +400,10 @@ record_setstring(msiobj* record, PyObject *args)
Py_UNICODE *data;
if (!PyArg_ParseTuple(args, "iu:SetString", &field, &data))
- return NULL;
+ return NULL;
if ((status = MsiRecordSetStringW(record->h, field, data)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -417,10 +417,10 @@ record_setstream(msiobj* record, PyObject *args)
Py_UNICODE *data;
if (!PyArg_ParseTuple(args, "iu:SetStream", &field, &data))
- return NULL;
+ return NULL;
if ((status = MsiRecordSetStreamW(record->h, field, data)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -434,10 +434,10 @@ record_setinteger(msiobj* record, PyObject *args)
int data;
if (!PyArg_ParseTuple(args, "ii:SetInteger", &field, &data))
- return NULL;
+ return NULL;
if ((status = MsiRecordSetInteger(record->h, field, data)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -446,39 +446,39 @@ record_setinteger(msiobj* record, PyObject *args)
static PyMethodDef record_methods[] = {
- { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS,
- PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")},
+ { "GetFieldCount", (PyCFunction)record_getfieldcount, METH_NOARGS,
+ PyDoc_STR("GetFieldCount() -> int\nWraps MsiRecordGetFieldCount")},
{ "GetInteger", (PyCFunction)record_getinteger, METH_VARARGS,
PyDoc_STR("GetInteger(field) -> int\nWraps MsiRecordGetInteger")},
{ "GetString", (PyCFunction)record_getstring, METH_VARARGS,
PyDoc_STR("GetString(field) -> string\nWraps MsiRecordGetString")},
- { "SetString", (PyCFunction)record_setstring, METH_VARARGS,
- PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")},
- { "SetStream", (PyCFunction)record_setstream, METH_VARARGS,
- PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")},
- { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS,
- PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")},
- { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS,
- PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")},
+ { "SetString", (PyCFunction)record_setstring, METH_VARARGS,
+ PyDoc_STR("SetString(field,str) -> None\nWraps MsiRecordSetString")},
+ { "SetStream", (PyCFunction)record_setstream, METH_VARARGS,
+ PyDoc_STR("SetStream(field,filename) -> None\nWraps MsiRecordSetInteger")},
+ { "SetInteger", (PyCFunction)record_setinteger, METH_VARARGS,
+ PyDoc_STR("SetInteger(field,int) -> None\nWraps MsiRecordSetInteger")},
+ { "ClearData", (PyCFunction)record_cleardata, METH_NOARGS,
+ PyDoc_STR("ClearData() -> int\nWraps MsiRecordGClearData")},
{ NULL, NULL }
};
static PyTypeObject record_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_msi.Record", /*tp_name*/
- sizeof(msiobj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)msiobj_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_msi.Record", /*tp_name*/
+ sizeof(msiobj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)msiobj_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
PyObject_GenericGetAttr,/*tp_getattro*/
@@ -513,8 +513,8 @@ record_new(MSIHANDLE h)
msiobj *result = PyObject_NEW(struct msiobj, &record_Type);
if (!result) {
- MsiCloseHandle(h);
- return NULL;
+ MsiCloseHandle(h);
+ return NULL;
}
result->h = h;
@@ -537,27 +537,27 @@ summary_getproperty(msiobj* si, PyObject *args)
DWORD ssize = sizeof(sval);
if (!PyArg_ParseTuple(args, "i:GetProperty", &field))
- return NULL;
+ return NULL;
- status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
- &fval, sval, &ssize);
+ status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
+ &fval, sval, &ssize);
if (status == ERROR_MORE_DATA) {
- sval = malloc(ssize);
- status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
- &fval, sval, &ssize);
+ sval = malloc(ssize);
+ status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
+ &fval, sval, &ssize);
}
switch(type) {
- case VT_I2: case VT_I4:
- return PyLong_FromLong(ival);
- case VT_FILETIME:
- PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
- return NULL;
- case VT_LPSTR:
- result = PyBytes_FromStringAndSize(sval, ssize);
- if (sval != sbuf)
- free(sval);
- return result;
+ case VT_I2: case VT_I4:
+ return PyLong_FromLong(ival);
+ case VT_FILETIME:
+ PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
+ return NULL;
+ case VT_LPSTR:
+ result = PyBytes_FromStringAndSize(sval, ssize);
+ if (sval != sbuf)
+ free(sval);
+ return result;
}
PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
return NULL;
@@ -571,7 +571,7 @@ summary_getpropertycount(msiobj* si, PyObject *args)
status = MsiSummaryInfoGetPropertyCount(si->h, &result);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
return PyLong_FromLong(result);
}
@@ -584,25 +584,25 @@ summary_setproperty(msiobj* si, PyObject *args)
PyObject* data;
if (!PyArg_ParseTuple(args, "iO:SetProperty", &field, &data))
- return NULL;
+ return NULL;
if (PyUnicode_Check(data)) {
- status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR,
- 0, NULL, PyUnicode_AsUnicode(data));
+ status = MsiSummaryInfoSetPropertyW(si->h, field, VT_LPSTR,
+ 0, NULL, PyUnicode_AsUnicode(data));
} else if (PyLong_CheckExact(data)) {
- long value = PyLong_AsLong(data);
- if (value == -1 && PyErr_Occurred()) {
- return NULL;
- }
- status = MsiSummaryInfoSetProperty(si->h, field, VT_I4,
- value, NULL, NULL);
+ long value = PyLong_AsLong(data);
+ if (value == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+ status = MsiSummaryInfoSetProperty(si->h, field, VT_I4,
+ value, NULL, NULL);
} else {
- PyErr_SetString(PyExc_TypeError, "unsupported type");
- return NULL;
+ PyErr_SetString(PyExc_TypeError, "unsupported type");
+ return NULL;
}
-
+
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -616,39 +616,39 @@ summary_persist(msiobj* si, PyObject *args)
status = MsiSummaryInfoPersist(si->h);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef summary_methods[] = {
- { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS,
- PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")},
- { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS,
- PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")},
- { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS,
- PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")},
- { "Persist", (PyCFunction)summary_persist, METH_NOARGS,
- PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")},
+ { "GetProperty", (PyCFunction)summary_getproperty, METH_VARARGS,
+ PyDoc_STR("GetProperty(propid) -> value\nWraps MsiSummaryInfoGetProperty")},
+ { "GetPropertyCount", (PyCFunction)summary_getpropertycount, METH_NOARGS,
+ PyDoc_STR("GetProperty() -> int\nWraps MsiSummaryInfoGetPropertyCount")},
+ { "SetProperty", (PyCFunction)summary_setproperty, METH_VARARGS,
+ PyDoc_STR("SetProperty(value) -> None\nWraps MsiSummaryInfoProperty")},
+ { "Persist", (PyCFunction)summary_persist, METH_NOARGS,
+ PyDoc_STR("Persist() -> None\nWraps MsiSummaryInfoPersist")},
{ NULL, NULL }
};
static PyTypeObject summary_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_msi.SummaryInformation", /*tp_name*/
- sizeof(msiobj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)msiobj_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_msi.SummaryInformation", /*tp_name*/
+ sizeof(msiobj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)msiobj_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
PyObject_GenericGetAttr,/*tp_getattro*/
@@ -687,7 +687,7 @@ view_execute(msiobj *view, PyObject*args)
PyObject *oparams = Py_None;
if (!PyArg_ParseTuple(args, "O:Execute", &oparams))
- return NULL;
+ return NULL;
if (oparams != Py_None) {
if (oparams->ob_type != &record_Type) {
@@ -699,7 +699,7 @@ view_execute(msiobj *view, PyObject*args)
status = MsiViewExecute(view->h, params);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -712,7 +712,7 @@ view_fetch(msiobj *view, PyObject*args)
MSIHANDLE result;
if ((status = MsiViewFetch(view->h, &result)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
return record_new(result);
}
@@ -725,10 +725,10 @@ view_getcolumninfo(msiobj *view, PyObject *args)
MSIHANDLE result;
if (!PyArg_ParseTuple(args, "i:GetColumnInfo", &kind))
- return NULL;
+ return NULL;
if ((status = MsiViewGetColumnInfo(view->h, kind, &result)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
return record_new(result);
}
@@ -741,15 +741,15 @@ view_modify(msiobj *view, PyObject *args)
int status;
if (!PyArg_ParseTuple(args, "iO:Modify", &kind, &data))
- return NULL;
+ return NULL;
if (data->ob_type != &record_Type) {
- PyErr_SetString(PyExc_TypeError, "Modify expects a record object");
- return NULL;
+ PyErr_SetString(PyExc_TypeError, "Modify expects a record object");
+ return NULL;
}
if ((status = MsiViewModify(view->h, kind, ((msiobj*)data)->h)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -761,42 +761,42 @@ view_close(msiobj *view, PyObject*args)
int status;
if ((status = MsiViewClose(view->h)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef view_methods[] = {
- { "Execute", (PyCFunction)view_execute, METH_VARARGS,
- PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")},
+ { "Execute", (PyCFunction)view_execute, METH_VARARGS,
+ PyDoc_STR("Execute(params=None) -> None\nWraps MsiViewExecute")},
{ "GetColumnInfo", (PyCFunction)view_getcolumninfo, METH_VARARGS,
- PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")},
+ PyDoc_STR("GetColumnInfo() -> result\nWraps MsiGetColumnInfo")},
{ "Fetch", (PyCFunction)view_fetch, METH_NOARGS,
- PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")},
+ PyDoc_STR("Fetch() -> result\nWraps MsiViewFetch")},
{ "Modify", (PyCFunction)view_modify, METH_VARARGS,
- PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")},
+ PyDoc_STR("Modify(mode,record) -> None\nWraps MsiViewModify")},
{ "Close", (PyCFunction)view_close, METH_NOARGS,
- PyDoc_STR("Close() -> result\nWraps MsiViewClose")},
+ PyDoc_STR("Close() -> result\nWraps MsiViewClose")},
{ NULL, NULL }
};
static PyTypeObject msiview_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_msi.View", /*tp_name*/
- sizeof(msiobj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)msiobj_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_msi.View", /*tp_name*/
+ sizeof(msiobj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)msiobj_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
PyObject_GenericGetAttr,/*tp_getattro*/
@@ -836,15 +836,15 @@ msidb_openview(msiobj *msidb, PyObject *args)
msiobj *result;
if (!PyArg_ParseTuple(args, "s:OpenView", &sql))
- return NULL;
+ return NULL;
if ((status = MsiDatabaseOpenView(msidb->h, sql, &hView)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
result = PyObject_NEW(struct msiobj, &msiview_Type);
if (!result) {
- MsiCloseHandle(hView);
- return NULL;
+ MsiCloseHandle(hView);
+ return NULL;
}
result->h = hView;
@@ -857,7 +857,7 @@ msidb_commit(msiobj *msidb, PyObject *args)
int status;
if ((status = MsiDatabaseCommit(msidb->h)) != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
Py_INCREF(Py_None);
return Py_None;
@@ -872,16 +872,16 @@ msidb_getsummaryinformation(msiobj *db, PyObject *args)
msiobj *oresult;
if (!PyArg_ParseTuple(args, "i:GetSummaryInformation", &count))
- return NULL;
+ return NULL;
status = MsiGetSummaryInformation(db->h, NULL, count, &result);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
oresult = PyObject_NEW(struct msiobj, &summary_Type);
if (!result) {
- MsiCloseHandle(result);
- return NULL;
+ MsiCloseHandle(result);
+ return NULL;
}
oresult->h = result;
@@ -889,31 +889,31 @@ msidb_getsummaryinformation(msiobj *db, PyObject *args)
}
static PyMethodDef db_methods[] = {
- { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS,
- PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")},
+ { "OpenView", (PyCFunction)msidb_openview, METH_VARARGS,
+ PyDoc_STR("OpenView(sql) -> viewobj\nWraps MsiDatabaseOpenView")},
{ "Commit", (PyCFunction)msidb_commit, METH_NOARGS,
- PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")},
- { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS,
- PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")},
+ PyDoc_STR("Commit() -> None\nWraps MsiDatabaseCommit")},
+ { "GetSummaryInformation", (PyCFunction)msidb_getsummaryinformation, METH_VARARGS,
+ PyDoc_STR("GetSummaryInformation(updateCount) -> viewobj\nWraps MsiGetSummaryInformation")},
{ NULL, NULL }
};
static PyTypeObject msidb_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_msi.Database", /*tp_name*/
- sizeof(msiobj), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- /* methods */
- (destructor)msiobj_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_msi.Database", /*tp_name*/
+ sizeof(msiobj), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)msiobj_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
PyObject_GenericGetAttr,/*tp_getattro*/
@@ -949,18 +949,18 @@ static PyObject* msiopendb(PyObject *obj, PyObject *args)
int persist;
MSIHANDLE h;
msiobj *result;
-
+
if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist))
- return NULL;
+ return NULL;
- status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
+ status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
if (status != ERROR_SUCCESS)
- return msierror(status);
+ return msierror(status);
result = PyObject_NEW(struct msiobj, &msidb_Type);
if (!result) {
- MsiCloseHandle(h);
- return NULL;
+ MsiCloseHandle(h);
+ return NULL;
}
result->h = h;
return (PyObject*)result;
@@ -973,11 +973,11 @@ createrecord(PyObject *o, PyObject *args)
MSIHANDLE h;
if (!PyArg_ParseTuple(args, "i:CreateRecord", &count))
- return NULL;
-
+ return NULL;
+
h = MsiCreateRecord(count);
if (h == 0)
- return msierror(0);
+ return msierror(0);
return record_new(h);
}
@@ -985,29 +985,29 @@ createrecord(PyObject *o, PyObject *args)
static PyMethodDef msi_methods[] = {
{"UuidCreate", (PyCFunction)uuidcreate, METH_NOARGS,
- PyDoc_STR("UuidCreate() -> string")},
- {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS,
- PyDoc_STR("fcicreate(cabname,files) -> None")},
- {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS,
- PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")},
- {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS,
- PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")},
- {NULL, NULL} /* sentinel */
+ PyDoc_STR("UuidCreate() -> string")},
+ {"FCICreate", (PyCFunction)fcicreate, METH_VARARGS,
+ PyDoc_STR("fcicreate(cabname,files) -> None")},
+ {"OpenDatabase", (PyCFunction)msiopendb, METH_VARARGS,
+ PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiOpenDatabase")},
+ {"CreateRecord", (PyCFunction)createrecord, METH_VARARGS,
+ PyDoc_STR("OpenDatabase(name, flags) -> dbobj\nWraps MsiCreateRecord")},
+ {NULL, NULL} /* sentinel */
};
static char msi_doc[] = "Documentation";
static struct PyModuleDef _msimodule = {
- PyModuleDef_HEAD_INIT,
- "_msi",
- msi_doc,
- -1,
- msi_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_msi",
+ msi_doc,
+ -1,
+ msi_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
@@ -1017,7 +1017,7 @@ PyInit__msi(void)
m = PyModule_Create(&_msimodule);
if (m == NULL)
- return NULL;
+ return NULL;
PyModule_AddIntConstant(m, "MSIDBOPEN_CREATEDIRECT", (int)MSIDBOPEN_CREATEDIRECT);
PyModule_AddIntConstant(m, "MSIDBOPEN_CREATE", (int)MSIDBOPEN_CREATE);
@@ -1063,7 +1063,7 @@ PyInit__msi(void)
MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL);
if (!MSIError)
- return NULL;
+ return NULL;
PyModule_AddObject(m, "MSIError", MSIError);
return m;
}
diff --git a/PC/_subprocess.c b/PC/_subprocess.c
index 7a12d91..27d3dc6 100644
--- a/PC/_subprocess.c
+++ b/PC/_subprocess.c
@@ -46,8 +46,8 @@
the wrapper is used to provide Detach and Close methods */
typedef struct {
- PyObject_HEAD
- HANDLE handle;
+ PyObject_HEAD
+ HANDLE handle;
} sp_handle_object;
static PyTypeObject sp_handle_type;
@@ -55,104 +55,104 @@ static PyTypeObject sp_handle_type;
static PyObject*
sp_handle_new(HANDLE handle)
{
- sp_handle_object* self;
+ sp_handle_object* self;
- self = PyObject_NEW(sp_handle_object, &sp_handle_type);
- if (self == NULL)
- return NULL;
+ self = PyObject_NEW(sp_handle_object, &sp_handle_type);
+ if (self == NULL)
+ return NULL;
- self->handle = handle;
+ self->handle = handle;
- return (PyObject*) self;
+ return (PyObject*) self;
}
#if defined(MS_WIN32) && !defined(MS_WIN64)
-#define HANDLE_TO_PYNUM(handle) PyLong_FromLong((long) handle)
-#define PY_HANDLE_PARAM "l"
+#define HANDLE_TO_PYNUM(handle) PyLong_FromLong((long) handle)
+#define PY_HANDLE_PARAM "l"
#else
-#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
-#define PY_HANDLE_PARAM "L"
+#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
+#define PY_HANDLE_PARAM "L"
#endif
static PyObject*
sp_handle_detach(sp_handle_object* self, PyObject* args)
{
- HANDLE handle;
+ HANDLE handle;
- if (! PyArg_ParseTuple(args, ":Detach"))
- return NULL;
+ if (! PyArg_ParseTuple(args, ":Detach"))
+ return NULL;
- handle = self->handle;
+ handle = self->handle;
- self->handle = INVALID_HANDLE_VALUE;
+ self->handle = INVALID_HANDLE_VALUE;
- /* note: return the current handle, as an integer */
- return HANDLE_TO_PYNUM(handle);
+ /* note: return the current handle, as an integer */
+ return HANDLE_TO_PYNUM(handle);
}
static PyObject*
sp_handle_close(sp_handle_object* self, PyObject* args)
{
- if (! PyArg_ParseTuple(args, ":Close"))
- return NULL;
-
- if (self->handle != INVALID_HANDLE_VALUE) {
- CloseHandle(self->handle);
- self->handle = INVALID_HANDLE_VALUE;
- }
- Py_INCREF(Py_None);
- return Py_None;
+ if (! PyArg_ParseTuple(args, ":Close"))
+ return NULL;
+
+ if (self->handle != INVALID_HANDLE_VALUE) {
+ CloseHandle(self->handle);
+ self->handle = INVALID_HANDLE_VALUE;
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
}
static void
sp_handle_dealloc(sp_handle_object* self)
{
- if (self->handle != INVALID_HANDLE_VALUE)
- CloseHandle(self->handle);
- PyObject_FREE(self);
+ if (self->handle != INVALID_HANDLE_VALUE)
+ CloseHandle(self->handle);
+ PyObject_FREE(self);
}
static PyMethodDef sp_handle_methods[] = {
- {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
- {"Close", (PyCFunction) sp_handle_close, METH_VARARGS},
- {NULL, NULL}
+ {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
+ {"Close", (PyCFunction) sp_handle_close, METH_VARARGS},
+ {NULL, NULL}
};
static PyObject*
sp_handle_as_int(sp_handle_object* self)
{
- return HANDLE_TO_PYNUM(self->handle);
+ return HANDLE_TO_PYNUM(self->handle);
}
static PyNumberMethods sp_handle_as_number;
static PyTypeObject sp_handle_type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "_subprocess_handle", sizeof(sp_handle_object), 0,
- (destructor) sp_handle_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- &sp_handle_as_number, /*tp_as_number */
- 0, /*tp_as_sequence */
- 0, /*tp_as_mapping */
- 0, /*tp_hash*/
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- 0, /*tp_doc*/
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- sp_handle_methods, /*tp_methods*/
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "_subprocess_handle", sizeof(sp_handle_object), 0,
+ (destructor) sp_handle_dealloc, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ &sp_handle_as_number, /*tp_as_number */
+ 0, /*tp_as_sequence */
+ 0, /*tp_as_mapping */
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ sp_handle_methods, /*tp_methods*/
};
/* -------------------------------------------------------------------- */
@@ -168,26 +168,26 @@ The integer associated with the handle object is returned.");
static PyObject *
sp_GetStdHandle(PyObject* self, PyObject* args)
{
- HANDLE handle;
- int std_handle;
+ HANDLE handle;
+ int std_handle;
- if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
- return NULL;
+ if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- handle = GetStdHandle((DWORD) std_handle);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ handle = GetStdHandle((DWORD) std_handle);
+ Py_END_ALLOW_THREADS
- if (handle == INVALID_HANDLE_VALUE)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (handle == INVALID_HANDLE_VALUE)
+ return PyErr_SetFromWindowsErr(GetLastError());
- if (! handle) {
- Py_INCREF(Py_None);
- return Py_None;
- }
+ if (! handle) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
- /* note: returns integer, not handle object */
- return HANDLE_TO_PYNUM(handle);
+ /* note: returns integer, not handle object */
+ return HANDLE_TO_PYNUM(handle);
}
PyDoc_STRVAR(GetCurrentProcess_doc,
@@ -198,10 +198,10 @@ Return a handle object for the current process.");
static PyObject *
sp_GetCurrentProcess(PyObject* self, PyObject* args)
{
- if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
- return NULL;
+ if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
+ return NULL;
- return sp_handle_new(GetCurrentProcess());
+ return sp_handle_new(GetCurrentProcess());
}
PyDoc_STRVAR(DuplicateHandle_doc,
@@ -218,43 +218,43 @@ through both handles.");
static PyObject *
sp_DuplicateHandle(PyObject* self, PyObject* args)
{
- HANDLE target_handle;
- BOOL result;
-
- HANDLE source_process_handle;
- HANDLE source_handle;
- HANDLE target_process_handle;
- int desired_access;
- int inherit_handle;
- int options = 0;
-
- if (! PyArg_ParseTuple(args,
- PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
- "ii|i:DuplicateHandle",
- &source_process_handle,
- &source_handle,
- &target_process_handle,
- &desired_access,
- &inherit_handle,
- &options))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- result = DuplicateHandle(
- source_process_handle,
- source_handle,
- target_process_handle,
- &target_handle,
- desired_access,
- inherit_handle,
- options
- );
- Py_END_ALLOW_THREADS
-
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
-
- return sp_handle_new(target_handle);
+ HANDLE target_handle;
+ BOOL result;
+
+ HANDLE source_process_handle;
+ HANDLE source_handle;
+ HANDLE target_process_handle;
+ int desired_access;
+ int inherit_handle;
+ int options = 0;
+
+ if (! PyArg_ParseTuple(args,
+ PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
+ "ii|i:DuplicateHandle",
+ &source_process_handle,
+ &source_handle,
+ &target_process_handle,
+ &desired_access,
+ &inherit_handle,
+ &options))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ result = DuplicateHandle(
+ source_process_handle,
+ source_handle,
+ target_process_handle,
+ &target_handle,
+ desired_access,
+ inherit_handle,
+ options
+ );
+ Py_END_ALLOW_THREADS
+
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
+
+ return sp_handle_new(target_handle);
}
PyDoc_STRVAR(CreatePipe_doc,
@@ -268,25 +268,25 @@ pipe_attrs is ignored internally and can be None.");
static PyObject *
sp_CreatePipe(PyObject* self, PyObject* args)
{
- HANDLE read_pipe;
- HANDLE write_pipe;
- BOOL result;
+ HANDLE read_pipe;
+ HANDLE write_pipe;
+ BOOL result;
- PyObject* pipe_attributes; /* ignored */
- int size;
+ PyObject* pipe_attributes; /* ignored */
+ int size;
- if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
- return NULL;
+ if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
+ Py_END_ALLOW_THREADS
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
- return Py_BuildValue(
- "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
+ return Py_BuildValue(
+ "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
}
/* helpers for createprocess */
@@ -294,110 +294,110 @@ sp_CreatePipe(PyObject* self, PyObject* args)
static int
getint(PyObject* obj, char* name)
{
- PyObject* value;
- int ret;
-
- value = PyObject_GetAttrString(obj, name);
- if (! value) {
- PyErr_Clear(); /* FIXME: propagate error? */
- return 0;
- }
- ret = (int) PyLong_AsLong(value);
- Py_DECREF(value);
- return ret;
+ PyObject* value;
+ int ret;
+
+ value = PyObject_GetAttrString(obj, name);
+ if (! value) {
+ PyErr_Clear(); /* FIXME: propagate error? */
+ return 0;
+ }
+ ret = (int) PyLong_AsLong(value);
+ Py_DECREF(value);
+ return ret;
}
static HANDLE
gethandle(PyObject* obj, char* name)
{
- sp_handle_object* value;
- HANDLE ret;
-
- value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
- if (! value) {
- PyErr_Clear(); /* FIXME: propagate error? */
- return NULL;
- }
- if (Py_TYPE(value) != &sp_handle_type)
- ret = NULL;
- else
- ret = value->handle;
- Py_DECREF(value);
- return ret;
+ sp_handle_object* value;
+ HANDLE ret;
+
+ value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
+ if (! value) {
+ PyErr_Clear(); /* FIXME: propagate error? */
+ return NULL;
+ }
+ if (Py_TYPE(value) != &sp_handle_type)
+ ret = NULL;
+ else
+ ret = value->handle;
+ Py_DECREF(value);
+ return ret;
}
static PyObject*
getenvironment(PyObject* environment)
{
- int i, envsize;
- PyObject* out = NULL;
- PyObject* keys;
- PyObject* values;
- Py_UNICODE* p;
-
- /* convert environment dictionary to windows enviroment string */
- if (! PyMapping_Check(environment)) {
- PyErr_SetString(
- PyExc_TypeError, "environment must be dictionary or None");
- return NULL;
- }
-
- envsize = PyMapping_Length(environment);
-
- keys = PyMapping_Keys(environment);
- values = PyMapping_Values(environment);
- if (!keys || !values)
- goto error;
-
- out = PyUnicode_FromUnicode(NULL, 2048);
- if (! out)
- goto error;
-
- p = PyUnicode_AS_UNICODE(out);
-
- for (i = 0; i < envsize; i++) {
- int ksize, vsize, totalsize;
- PyObject* key = PyList_GET_ITEM(keys, i);
- PyObject* value = PyList_GET_ITEM(values, i);
-
- if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
- PyErr_SetString(PyExc_TypeError,
- "environment can only contain strings");
- goto error;
- }
- ksize = PyUnicode_GET_SIZE(key);
- vsize = PyUnicode_GET_SIZE(value);
- totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 +
- vsize + 1 + 1;
- if (totalsize > PyUnicode_GET_SIZE(out)) {
- int offset = p - PyUnicode_AS_UNICODE(out);
- PyUnicode_Resize(&out, totalsize + 1024);
- p = PyUnicode_AS_UNICODE(out) + offset;
- }
- Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize);
- p += ksize;
- *p++ = '=';
- Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize);
- p += vsize;
- *p++ = '\0';
- }
-
- /* add trailing null byte */
- *p++ = '\0';
- PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out));
-
- /* PyObject_Print(out, stdout, 0); */
-
- Py_XDECREF(keys);
- Py_XDECREF(values);
-
- return out;
+ int i, envsize;
+ PyObject* out = NULL;
+ PyObject* keys;
+ PyObject* values;
+ Py_UNICODE* p;
+
+ /* convert environment dictionary to windows enviroment string */
+ if (! PyMapping_Check(environment)) {
+ PyErr_SetString(
+ PyExc_TypeError, "environment must be dictionary or None");
+ return NULL;
+ }
+
+ envsize = PyMapping_Length(environment);
+
+ keys = PyMapping_Keys(environment);
+ values = PyMapping_Values(environment);
+ if (!keys || !values)
+ goto error;
+
+ out = PyUnicode_FromUnicode(NULL, 2048);
+ if (! out)
+ goto error;
+
+ p = PyUnicode_AS_UNICODE(out);
+
+ for (i = 0; i < envsize; i++) {
+ int ksize, vsize, totalsize;
+ PyObject* key = PyList_GET_ITEM(keys, i);
+ PyObject* value = PyList_GET_ITEM(values, i);
+
+ if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
+ PyErr_SetString(PyExc_TypeError,
+ "environment can only contain strings");
+ goto error;
+ }
+ ksize = PyUnicode_GET_SIZE(key);
+ vsize = PyUnicode_GET_SIZE(value);
+ totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 +
+ vsize + 1 + 1;
+ if (totalsize > PyUnicode_GET_SIZE(out)) {
+ int offset = p - PyUnicode_AS_UNICODE(out);
+ PyUnicode_Resize(&out, totalsize + 1024);
+ p = PyUnicode_AS_UNICODE(out) + offset;
+ }
+ Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize);
+ p += ksize;
+ *p++ = '=';
+ Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize);
+ p += vsize;
+ *p++ = '\0';
+ }
+
+ /* add trailing null byte */
+ *p++ = '\0';
+ PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out));
+
+ /* PyObject_Print(out, stdout, 0); */
+
+ Py_XDECREF(keys);
+ Py_XDECREF(values);
+
+ return out;
error:
- Py_XDECREF(out);
- Py_XDECREF(keys);
- Py_XDECREF(values);
- return NULL;
+ Py_XDECREF(out);
+ Py_XDECREF(keys);
+ Py_XDECREF(values);
+ return NULL;
}
PyDoc_STRVAR(CreateProcess_doc,
@@ -415,77 +415,77 @@ proc_attrs and thread_attrs are ignored internally and can be None.");
static PyObject *
sp_CreateProcess(PyObject* self, PyObject* args)
{
- BOOL result;
- PROCESS_INFORMATION pi;
- STARTUPINFOW si;
- PyObject* environment;
-
- Py_UNICODE* application_name;
- Py_UNICODE* command_line;
- PyObject* process_attributes; /* ignored */
- PyObject* thread_attributes; /* ignored */
- int inherit_handles;
- int creation_flags;
- PyObject* env_mapping;
- Py_UNICODE* current_directory;
- PyObject* startup_info;
-
- if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
- &application_name,
- &command_line,
- &process_attributes,
- &thread_attributes,
- &inherit_handles,
- &creation_flags,
- &env_mapping,
- &current_directory,
- &startup_info))
- return NULL;
-
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
-
- /* note: we only support a small subset of all SI attributes */
- si.dwFlags = getint(startup_info, "dwFlags");
- si.wShowWindow = getint(startup_info, "wShowWindow");
- si.hStdInput = gethandle(startup_info, "hStdInput");
- si.hStdOutput = gethandle(startup_info, "hStdOutput");
- si.hStdError = gethandle(startup_info, "hStdError");
-
- if (PyErr_Occurred())
- return NULL;
-
- if (env_mapping == Py_None)
- environment = NULL;
- else {
- environment = getenvironment(env_mapping);
- if (! environment)
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- result = CreateProcessW(application_name,
- command_line,
- NULL,
- NULL,
- inherit_handles,
- creation_flags | CREATE_UNICODE_ENVIRONMENT,
- environment ? PyUnicode_AS_UNICODE(environment) : NULL,
- current_directory,
- &si,
- &pi);
- Py_END_ALLOW_THREADS
-
- Py_XDECREF(environment);
-
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
-
- return Py_BuildValue("NNii",
- sp_handle_new(pi.hProcess),
- sp_handle_new(pi.hThread),
- pi.dwProcessId,
- pi.dwThreadId);
+ BOOL result;
+ PROCESS_INFORMATION pi;
+ STARTUPINFOW si;
+ PyObject* environment;
+
+ Py_UNICODE* application_name;
+ Py_UNICODE* command_line;
+ PyObject* process_attributes; /* ignored */
+ PyObject* thread_attributes; /* ignored */
+ int inherit_handles;
+ int creation_flags;
+ PyObject* env_mapping;
+ Py_UNICODE* current_directory;
+ PyObject* startup_info;
+
+ if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
+ &application_name,
+ &command_line,
+ &process_attributes,
+ &thread_attributes,
+ &inherit_handles,
+ &creation_flags,
+ &env_mapping,
+ &current_directory,
+ &startup_info))
+ return NULL;
+
+ ZeroMemory(&si, sizeof(si));
+ si.cb = sizeof(si);
+
+ /* note: we only support a small subset of all SI attributes */
+ si.dwFlags = getint(startup_info, "dwFlags");
+ si.wShowWindow = getint(startup_info, "wShowWindow");
+ si.hStdInput = gethandle(startup_info, "hStdInput");
+ si.hStdOutput = gethandle(startup_info, "hStdOutput");
+ si.hStdError = gethandle(startup_info, "hStdError");
+
+ if (PyErr_Occurred())
+ return NULL;
+
+ if (env_mapping == Py_None)
+ environment = NULL;
+ else {
+ environment = getenvironment(env_mapping);
+ if (! environment)
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ result = CreateProcessW(application_name,
+ command_line,
+ NULL,
+ NULL,
+ inherit_handles,
+ creation_flags | CREATE_UNICODE_ENVIRONMENT,
+ environment ? PyUnicode_AS_UNICODE(environment) : NULL,
+ current_directory,
+ &si,
+ &pi);
+ Py_END_ALLOW_THREADS
+
+ Py_XDECREF(environment);
+
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
+
+ return Py_BuildValue("NNii",
+ sp_handle_new(pi.hProcess),
+ sp_handle_new(pi.hThread),
+ pi.dwProcessId,
+ pi.dwThreadId);
}
PyDoc_STRVAR(TerminateProcess_doc,
@@ -496,21 +496,21 @@ Terminate the specified process and all of its threads.");
static PyObject *
sp_TerminateProcess(PyObject* self, PyObject* args)
{
- BOOL result;
+ BOOL result;
- HANDLE process;
- int exit_code;
- if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
- &process, &exit_code))
- return NULL;
+ HANDLE process;
+ int exit_code;
+ if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
+ &process, &exit_code))
+ return NULL;
- result = TerminateProcess(process, exit_code);
+ result = TerminateProcess(process, exit_code);
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(GetExitCodeProcess_doc,
@@ -521,19 +521,19 @@ Return the termination status of the specified process.");
static PyObject *
sp_GetExitCodeProcess(PyObject* self, PyObject* args)
{
- DWORD exit_code;
- BOOL result;
+ DWORD exit_code;
+ BOOL result;
- HANDLE process;
- if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
- return NULL;
+ HANDLE process;
+ if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
+ return NULL;
- result = GetExitCodeProcess(process, &exit_code);
+ result = GetExitCodeProcess(process, &exit_code);
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
- return PyLong_FromLong(exit_code);
+ return PyLong_FromLong(exit_code);
}
PyDoc_STRVAR(WaitForSingleObject_doc,
@@ -546,23 +546,23 @@ in milliseconds.");
static PyObject *
sp_WaitForSingleObject(PyObject* self, PyObject* args)
{
- DWORD result;
+ DWORD result;
- HANDLE handle;
- int milliseconds;
- if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
- &handle,
- &milliseconds))
- return NULL;
+ HANDLE handle;
+ int milliseconds;
+ if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
+ &handle,
+ &milliseconds))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- result = WaitForSingleObject(handle, (DWORD) milliseconds);
- Py_END_ALLOW_THREADS
+ Py_BEGIN_ALLOW_THREADS
+ result = WaitForSingleObject(handle, (DWORD) milliseconds);
+ Py_END_ALLOW_THREADS
- if (result == WAIT_FAILED)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (result == WAIT_FAILED)
+ return PyErr_SetFromWindowsErr(GetLastError());
- return PyLong_FromLong((int) result);
+ return PyLong_FromLong((int) result);
}
PyDoc_STRVAR(GetVersion_doc,
@@ -573,10 +573,10 @@ Return the version number of the current operating system.");
static PyObject *
sp_GetVersion(PyObject* self, PyObject* args)
{
- if (! PyArg_ParseTuple(args, ":GetVersion"))
- return NULL;
+ if (! PyArg_ParseTuple(args, ":GetVersion"))
+ return NULL;
- return PyLong_FromLong((int) GetVersion());
+ return PyLong_FromLong((int) GetVersion());
}
PyDoc_STRVAR(GetModuleFileName_doc,
@@ -594,41 +594,41 @@ of the current process.");
static PyObject *
sp_GetModuleFileName(PyObject* self, PyObject* args)
{
- BOOL result;
- HMODULE module;
- WCHAR filename[MAX_PATH];
+ BOOL result;
+ HMODULE module;
+ WCHAR filename[MAX_PATH];
- if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
- &module))
- return NULL;
+ if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
+ &module))
+ return NULL;
- result = GetModuleFileNameW(module, filename, MAX_PATH);
- filename[MAX_PATH-1] = '\0';
+ result = GetModuleFileNameW(module, filename, MAX_PATH);
+ filename[MAX_PATH-1] = '\0';
- if (! result)
- return PyErr_SetFromWindowsErr(GetLastError());
+ if (! result)
+ return PyErr_SetFromWindowsErr(GetLastError());
- return PyUnicode_FromUnicode(filename, Py_UNICODE_strlen(filename));
+ return PyUnicode_FromUnicode(filename, Py_UNICODE_strlen(filename));
}
static PyMethodDef sp_functions[] = {
- {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc},
- {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS,
- GetCurrentProcess_doc},
- {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS,
- DuplicateHandle_doc},
- {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc},
- {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc},
- {"TerminateProcess", sp_TerminateProcess, METH_VARARGS,
- TerminateProcess_doc},
- {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS,
- GetExitCodeProcess_doc},
- {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS,
- WaitForSingleObject_doc},
- {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc},
- {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS,
- GetModuleFileName_doc},
- {NULL, NULL}
+ {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc},
+ {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS,
+ GetCurrentProcess_doc},
+ {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS,
+ DuplicateHandle_doc},
+ {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc},
+ {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc},
+ {"TerminateProcess", sp_TerminateProcess, METH_VARARGS,
+ TerminateProcess_doc},
+ {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS,
+ GetExitCodeProcess_doc},
+ {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS,
+ WaitForSingleObject_doc},
+ {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc},
+ {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS,
+ GetModuleFileName_doc},
+ {NULL, NULL}
};
/* -------------------------------------------------------------------- */
@@ -636,51 +636,51 @@ static PyMethodDef sp_functions[] = {
static void
defint(PyObject* d, const char* name, int value)
{
- PyObject* v = PyLong_FromLong((long) value);
- if (v) {
- PyDict_SetItemString(d, (char*) name, v);
- Py_DECREF(v);
- }
+ PyObject* v = PyLong_FromLong((long) value);
+ if (v) {
+ PyDict_SetItemString(d, (char*) name, v);
+ Py_DECREF(v);
+ }
}
static struct PyModuleDef _subprocessmodule = {
- PyModuleDef_HEAD_INIT,
- "_subprocess",
- NULL,
- -1,
- sp_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "_subprocess",
+ NULL,
+ -1,
+ sp_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit__subprocess()
{
- PyObject *d;
- PyObject *m;
-
- /* patch up object descriptors */
- sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
- if (PyType_Ready(&sp_handle_type) < 0)
- return NULL;
-
- m = PyModule_Create(&_subprocessmodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
-
- /* constants */
- defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
- defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
- defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
- defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
- defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
- defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
- defint(d, "SW_HIDE", SW_HIDE);
- defint(d, "INFINITE", INFINITE);
- defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
- defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
- return m;
+ PyObject *d;
+ PyObject *m;
+
+ /* patch up object descriptors */
+ sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
+ if (PyType_Ready(&sp_handle_type) < 0)
+ return NULL;
+
+ m = PyModule_Create(&_subprocessmodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+
+ /* constants */
+ defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
+ defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
+ defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
+ defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
+ defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
+ defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
+ defint(d, "SW_HIDE", SW_HIDE);
+ defint(d, "INFINITE", INFINITE);
+ defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
+ defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
+ return m;
}
diff --git a/PC/bdist_wininst/archive.h b/PC/bdist_wininst/archive.h
index 35d2fd6..31a7805 100644
--- a/PC/bdist_wininst/archive.h
+++ b/PC/bdist_wininst/archive.h
@@ -14,55 +14,55 @@
*/
struct eof_cdir {
- long tag; /* must be 0x06054b50 */
- short disknum;
- short firstdisk;
- short nTotalCDirThis;
- short nTotalCDir;
- long nBytesCDir;
- long ofsCDir;
- short commentlen;
+ long tag; /* must be 0x06054b50 */
+ short disknum;
+ short firstdisk;
+ short nTotalCDirThis;
+ short nTotalCDir;
+ long nBytesCDir;
+ long ofsCDir;
+ short commentlen;
};
struct cdir {
- long tag; /* must be 0x02014b50 */
- short version_made;
- short version_extract;
- short gp_bitflag;
- short comp_method;
- short last_mod_file_time;
- short last_mod_file_date;
- long crc32;
- long comp_size;
- long uncomp_size;
- short fname_length;
- short extra_length;
- short comment_length;
- short disknum_start;
- short int_file_attr;
- long ext_file_attr;
- long ofs_local_header;
+ long tag; /* must be 0x02014b50 */
+ short version_made;
+ short version_extract;
+ short gp_bitflag;
+ short comp_method;
+ short last_mod_file_time;
+ short last_mod_file_date;
+ long crc32;
+ long comp_size;
+ long uncomp_size;
+ short fname_length;
+ short extra_length;
+ short comment_length;
+ short disknum_start;
+ short int_file_attr;
+ long ext_file_attr;
+ long ofs_local_header;
};
struct fhdr {
- long tag; /* must be 0x04034b50 */
- short version_needed;
- short flags;
- short method;
- short last_mod_file_time;
- short last_mod_file_date;
- long crc32;
- long comp_size;
- long uncomp_size;
- short fname_length;
- short extra_length;
+ long tag; /* must be 0x04034b50 */
+ short version_needed;
+ short flags;
+ short method;
+ short last_mod_file_time;
+ short last_mod_file_date;
+ long crc32;
+ long comp_size;
+ long uncomp_size;
+ short fname_length;
+ short extra_length;
};
struct meta_data_hdr {
- int tag;
- int uncomp_size;
- int bitmap_size;
+ int tag;
+ int uncomp_size;
+ int bitmap_size;
};
#pragma pack()
@@ -70,29 +70,29 @@ struct meta_data_hdr {
/* installation scheme */
typedef struct tagSCHEME {
- char *name;
- char *prefix;
+ char *name;
+ char *prefix;
} SCHEME;
typedef int (*NOTIFYPROC)(int code, LPSTR text, ...);
extern BOOL
extract_file(char *dst, char *src, int method, int comp_size,
- int uncomp_size, NOTIFYPROC notify);
+ int uncomp_size, NOTIFYPROC notify);
extern BOOL
unzip_archive(SCHEME *scheme, char *dirname, char *data,
- DWORD size, NOTIFYPROC notify);
+ DWORD size, NOTIFYPROC notify);
extern char *
map_new_file(DWORD flags, char *filename, char
- *pathname_part, int size,
- WORD wFatDate, WORD wFatTime,
- NOTIFYPROC callback);
+ *pathname_part, int size,
+ WORD wFatDate, WORD wFatTime,
+ NOTIFYPROC callback);
extern BOOL
ensure_directory (char *pathname, char *new_part,
- NOTIFYPROC callback);
+ NOTIFYPROC callback);
/* codes for NOITIFYPROC */
#define DIR_CREATED 1
diff --git a/PC/bdist_wininst/extract.c b/PC/bdist_wininst/extract.c
index b495e1c..fc2d2b9 100644
--- a/PC/bdist_wininst/extract.c
+++ b/PC/bdist_wininst/extract.c
@@ -19,181 +19,181 @@
/* Convert unix-path to dos-path */
static void normpath(char *path)
{
- while (path && *path) {
- if (*path == '/')
- *path = '\\';
- ++path;
- }
+ while (path && *path) {
+ if (*path == '/')
+ *path = '\\';
+ ++path;
+ }
}
BOOL ensure_directory(char *pathname, char *new_part, NOTIFYPROC notify)
{
- while (new_part && *new_part && (new_part = strchr(new_part, '\\'))) {
- DWORD attr;
- *new_part = '\0';
- attr = GetFileAttributes(pathname);
- if (attr == -1) {
- /* nothing found */
- if (!CreateDirectory(pathname, NULL) && notify)
- notify(SYSTEM_ERROR,
- "CreateDirectory (%s)", pathname);
- else
- notify(DIR_CREATED, pathname);
- }
- if (attr & FILE_ATTRIBUTE_DIRECTORY) {
- ;
- } else {
- SetLastError(183);
- if (notify)
- notify(SYSTEM_ERROR,
- "CreateDirectory (%s)", pathname);
- }
- *new_part = '\\';
- ++new_part;
- }
- return TRUE;
+ while (new_part && *new_part && (new_part = strchr(new_part, '\\'))) {
+ DWORD attr;
+ *new_part = '\0';
+ attr = GetFileAttributes(pathname);
+ if (attr == -1) {
+ /* nothing found */
+ if (!CreateDirectory(pathname, NULL) && notify)
+ notify(SYSTEM_ERROR,
+ "CreateDirectory (%s)", pathname);
+ else
+ notify(DIR_CREATED, pathname);
+ }
+ if (attr & FILE_ATTRIBUTE_DIRECTORY) {
+ ;
+ } else {
+ SetLastError(183);
+ if (notify)
+ notify(SYSTEM_ERROR,
+ "CreateDirectory (%s)", pathname);
+ }
+ *new_part = '\\';
+ ++new_part;
+ }
+ return TRUE;
}
/* XXX Should better explicitely specify
* uncomp_size and file_times instead of pfhdr!
*/
char *map_new_file(DWORD flags, char *filename,
- char *pathname_part, int size,
- WORD wFatDate, WORD wFatTime,
- NOTIFYPROC notify)
+ char *pathname_part, int size,
+ WORD wFatDate, WORD wFatTime,
+ NOTIFYPROC notify)
{
- HANDLE hFile, hFileMapping;
- char *dst;
- FILETIME ft;
+ HANDLE hFile, hFileMapping;
+ char *dst;
+ FILETIME ft;
try_again:
- if (!flags)
- flags = CREATE_NEW;
- hFile = CreateFile(filename,
- GENERIC_WRITE | GENERIC_READ,
- 0, NULL,
- flags,
- FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- DWORD x = GetLastError();
- switch (x) {
- case ERROR_FILE_EXISTS:
- if (notify && notify(CAN_OVERWRITE, filename))
- hFile = CreateFile(filename,
- GENERIC_WRITE|GENERIC_READ,
- 0, NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- else {
- if (notify)
- notify(FILE_OVERWRITTEN, filename);
- return NULL;
- }
- break;
- case ERROR_PATH_NOT_FOUND:
- if (ensure_directory(filename, pathname_part, notify))
- goto try_again;
- else
- return FALSE;
- break;
- default:
- SetLastError(x);
- break;
- }
- }
- if (hFile == INVALID_HANDLE_VALUE) {
- if (notify)
- notify (SYSTEM_ERROR, "CreateFile (%s)", filename);
- return NULL;
- }
-
- if (notify)
- notify(FILE_CREATED, filename);
-
- DosDateTimeToFileTime(wFatDate, wFatTime, &ft);
- SetFileTime(hFile, &ft, &ft, &ft);
-
-
- if (size == 0) {
- /* We cannot map a zero-length file (Also it makes
- no sense */
- CloseHandle(hFile);
- return NULL;
- }
-
- hFileMapping = CreateFileMapping(hFile,
- NULL, PAGE_READWRITE, 0, size, NULL);
-
- CloseHandle(hFile);
-
- if (hFileMapping == INVALID_HANDLE_VALUE) {
- if (notify)
- notify(SYSTEM_ERROR,
- "CreateFileMapping (%s)", filename);
- return NULL;
- }
-
- dst = MapViewOfFile(hFileMapping,
- FILE_MAP_WRITE, 0, 0, 0);
-
- CloseHandle(hFileMapping);
-
- if (!dst) {
- if (notify)
- notify(SYSTEM_ERROR, "MapViewOfFile (%s)", filename);
- return NULL;
- }
- return dst;
+ if (!flags)
+ flags = CREATE_NEW;
+ hFile = CreateFile(filename,
+ GENERIC_WRITE | GENERIC_READ,
+ 0, NULL,
+ flags,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ if (hFile == INVALID_HANDLE_VALUE) {
+ DWORD x = GetLastError();
+ switch (x) {
+ case ERROR_FILE_EXISTS:
+ if (notify && notify(CAN_OVERWRITE, filename))
+ hFile = CreateFile(filename,
+ GENERIC_WRITE|GENERIC_READ,
+ 0, NULL,
+ CREATE_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL);
+ else {
+ if (notify)
+ notify(FILE_OVERWRITTEN, filename);
+ return NULL;
+ }
+ break;
+ case ERROR_PATH_NOT_FOUND:
+ if (ensure_directory(filename, pathname_part, notify))
+ goto try_again;
+ else
+ return FALSE;
+ break;
+ default:
+ SetLastError(x);
+ break;
+ }
+ }
+ if (hFile == INVALID_HANDLE_VALUE) {
+ if (notify)
+ notify (SYSTEM_ERROR, "CreateFile (%s)", filename);
+ return NULL;
+ }
+
+ if (notify)
+ notify(FILE_CREATED, filename);
+
+ DosDateTimeToFileTime(wFatDate, wFatTime, &ft);
+ SetFileTime(hFile, &ft, &ft, &ft);
+
+
+ if (size == 0) {
+ /* We cannot map a zero-length file (Also it makes
+ no sense */
+ CloseHandle(hFile);
+ return NULL;
+ }
+
+ hFileMapping = CreateFileMapping(hFile,
+ NULL, PAGE_READWRITE, 0, size, NULL);
+
+ CloseHandle(hFile);
+
+ if (hFileMapping == INVALID_HANDLE_VALUE) {
+ if (notify)
+ notify(SYSTEM_ERROR,
+ "CreateFileMapping (%s)", filename);
+ return NULL;
+ }
+
+ dst = MapViewOfFile(hFileMapping,
+ FILE_MAP_WRITE, 0, 0, 0);
+
+ CloseHandle(hFileMapping);
+
+ if (!dst) {
+ if (notify)
+ notify(SYSTEM_ERROR, "MapViewOfFile (%s)", filename);
+ return NULL;
+ }
+ return dst;
}
BOOL
extract_file(char *dst, char *src, int method, int comp_size,
- int uncomp_size, NOTIFYPROC notify)
+ int uncomp_size, NOTIFYPROC notify)
{
- z_stream zstream;
- int result;
+ z_stream zstream;
+ int result;
- if (method == Z_DEFLATED) {
- int x;
- memset(&zstream, 0, sizeof(zstream));
- zstream.next_in = src;
- zstream.avail_in = comp_size+1;
- zstream.next_out = dst;
- zstream.avail_out = uncomp_size;
+ if (method == Z_DEFLATED) {
+ int x;
+ memset(&zstream, 0, sizeof(zstream));
+ zstream.next_in = src;
+ zstream.avail_in = comp_size+1;
+ zstream.next_out = dst;
+ zstream.avail_out = uncomp_size;
/* Apparently an undocumented feature of zlib: Set windowsize
to negative values to supress the gzip header and be compatible with
zip! */
- result = TRUE;
- if (Z_OK != (x = inflateInit2(&zstream, -15))) {
- if (notify)
- notify(ZLIB_ERROR,
- "inflateInit2 returns %d", x);
- result = FALSE;
- goto cleanup;
- }
- if (Z_STREAM_END != (x = inflate(&zstream, Z_FINISH))) {
- if (notify)
- notify(ZLIB_ERROR,
- "inflate returns %d", x);
- result = FALSE;
- }
- cleanup:
- if (Z_OK != (x = inflateEnd(&zstream))) {
- if (notify)
- notify (ZLIB_ERROR,
- "inflateEnd returns %d", x);
- result = FALSE;
- }
- } else if (method == 0) {
- memcpy(dst, src, uncomp_size);
- result = TRUE;
- } else
- result = FALSE;
- UnmapViewOfFile(dst);
- return result;
+ result = TRUE;
+ if (Z_OK != (x = inflateInit2(&zstream, -15))) {
+ if (notify)
+ notify(ZLIB_ERROR,
+ "inflateInit2 returns %d", x);
+ result = FALSE;
+ goto cleanup;
+ }
+ if (Z_STREAM_END != (x = inflate(&zstream, Z_FINISH))) {
+ if (notify)
+ notify(ZLIB_ERROR,
+ "inflate returns %d", x);
+ result = FALSE;
+ }
+ cleanup:
+ if (Z_OK != (x = inflateEnd(&zstream))) {
+ if (notify)
+ notify (ZLIB_ERROR,
+ "inflateEnd returns %d", x);
+ result = FALSE;
+ }
+ } else if (method == 0) {
+ memcpy(dst, src, uncomp_size);
+ result = TRUE;
+ } else
+ result = FALSE;
+ UnmapViewOfFile(dst);
+ return result;
}
/* Open a zip-compatible archive and extract all files
@@ -201,121 +201,121 @@ extract_file(char *dst, char *src, int method, int comp_size,
*/
BOOL
unzip_archive(SCHEME *scheme, char *dirname, char *data, DWORD size,
- NOTIFYPROC notify)
+ NOTIFYPROC notify)
{
- int n;
- char pathname[MAX_PATH];
- char *new_part;
-
- /* read the end of central directory record */
- struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof
- (struct eof_cdir)];
-
- int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir -
- pe->ofsCDir;
-
- /* set position to start of central directory */
- int pos = arc_start + pe->ofsCDir;
-
- /* make sure this is a zip file */
- if (pe->tag != 0x06054b50)
- return FALSE;
-
- /* Loop through the central directory, reading all entries */
- for (n = 0; n < pe->nTotalCDir; ++n) {
- int i;
- char *fname;
- char *pcomp;
- char *dst;
- struct cdir *pcdir;
- struct fhdr *pfhdr;
-
- pcdir = (struct cdir *)&data[pos];
- pfhdr = (struct fhdr *)&data[pcdir->ofs_local_header +
- arc_start];
-
- if (pcdir->tag != 0x02014b50)
- return FALSE;
- if (pfhdr->tag != 0x04034b50)
- return FALSE;
- pos += sizeof(struct cdir);
- fname = (char *)&data[pos]; /* This is not null terminated! */
- pos += pcdir->fname_length + pcdir->extra_length +
- pcdir->comment_length;
-
- pcomp = &data[pcdir->ofs_local_header
- + sizeof(struct fhdr)
- + arc_start
- + pfhdr->fname_length
- + pfhdr->extra_length];
-
- /* dirname is the Python home directory (prefix) */
- strcpy(pathname, dirname);
- if (pathname[strlen(pathname)-1] != '\\')
- strcat(pathname, "\\");
- new_part = &pathname[lstrlen(pathname)];
- /* we must now match the first part of the pathname
- * in the archive to a component in the installation
- * scheme (PURELIB, PLATLIB, HEADERS, SCRIPTS, or DATA)
- * and replace this part by the one in the scheme to use
- */
- for (i = 0; scheme[i].name; ++i) {
- if (0 == strnicmp(scheme[i].name, fname,
- strlen(scheme[i].name))) {
- char *rest;
- int len;
-
- /* length of the replaced part */
- int namelen = strlen(scheme[i].name);
-
- strcat(pathname, scheme[i].prefix);
-
- rest = fname + namelen;
- len = pfhdr->fname_length - namelen;
-
- if ((pathname[strlen(pathname)-1] != '\\')
- && (pathname[strlen(pathname)-1] != '/'))
- strcat(pathname, "\\");
- /* Now that pathname ends with a separator,
- * we must make sure rest does not start with
- * an additional one.
- */
- if ((rest[0] == '\\') || (rest[0] == '/')) {
- ++rest;
- --len;
- }
-
- strncat(pathname, rest, len);
- goto Done;
- }
- }
- /* no prefix to replace found, go unchanged */
- strncat(pathname, fname, pfhdr->fname_length);
- Done:
- normpath(pathname);
- if (pathname[strlen(pathname)-1] != '\\') {
- /*
- * The local file header (pfhdr) does not always
- * contain the compressed and uncompressed sizes of
- * the data depending on bit 3 of the flags field. So
- * it seems better to use the data from the central
- * directory (pcdir).
- */
- dst = map_new_file(0, pathname, new_part,
- pcdir->uncomp_size,
- pcdir->last_mod_file_date,
- pcdir->last_mod_file_time, notify);
- if (dst) {
- if (!extract_file(dst, pcomp, pfhdr->method,
- pcdir->comp_size,
- pcdir->uncomp_size,
- notify))
- return FALSE;
- } /* else ??? */
- }
- if (notify)
- notify(NUM_FILES, new_part, (int)pe->nTotalCDir,
- (int)n+1);
- }
- return TRUE;
+ int n;
+ char pathname[MAX_PATH];
+ char *new_part;
+
+ /* read the end of central directory record */
+ struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof
+ (struct eof_cdir)];
+
+ int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir -
+ pe->ofsCDir;
+
+ /* set position to start of central directory */
+ int pos = arc_start + pe->ofsCDir;
+
+ /* make sure this is a zip file */
+ if (pe->tag != 0x06054b50)
+ return FALSE;
+
+ /* Loop through the central directory, reading all entries */
+ for (n = 0; n < pe->nTotalCDir; ++n) {
+ int i;
+ char *fname;
+ char *pcomp;
+ char *dst;
+ struct cdir *pcdir;
+ struct fhdr *pfhdr;
+
+ pcdir = (struct cdir *)&data[pos];
+ pfhdr = (struct fhdr *)&data[pcdir->ofs_local_header +
+ arc_start];
+
+ if (pcdir->tag != 0x02014b50)
+ return FALSE;
+ if (pfhdr->tag != 0x04034b50)
+ return FALSE;
+ pos += sizeof(struct cdir);
+ fname = (char *)&data[pos]; /* This is not null terminated! */
+ pos += pcdir->fname_length + pcdir->extra_length +
+ pcdir->comment_length;
+
+ pcomp = &data[pcdir->ofs_local_header
+ + sizeof(struct fhdr)
+ + arc_start
+ + pfhdr->fname_length
+ + pfhdr->extra_length];
+
+ /* dirname is the Python home directory (prefix) */
+ strcpy(pathname, dirname);
+ if (pathname[strlen(pathname)-1] != '\\')
+ strcat(pathname, "\\");
+ new_part = &pathname[lstrlen(pathname)];
+ /* we must now match the first part of the pathname
+ * in the archive to a component in the installation
+ * scheme (PURELIB, PLATLIB, HEADERS, SCRIPTS, or DATA)
+ * and replace this part by the one in the scheme to use
+ */
+ for (i = 0; scheme[i].name; ++i) {
+ if (0 == strnicmp(scheme[i].name, fname,
+ strlen(scheme[i].name))) {
+ char *rest;
+ int len;
+
+ /* length of the replaced part */
+ int namelen = strlen(scheme[i].name);
+
+ strcat(pathname, scheme[i].prefix);
+
+ rest = fname + namelen;
+ len = pfhdr->fname_length - namelen;
+
+ if ((pathname[strlen(pathname)-1] != '\\')
+ && (pathname[strlen(pathname)-1] != '/'))
+ strcat(pathname, "\\");
+ /* Now that pathname ends with a separator,
+ * we must make sure rest does not start with
+ * an additional one.
+ */
+ if ((rest[0] == '\\') || (rest[0] == '/')) {
+ ++rest;
+ --len;
+ }
+
+ strncat(pathname, rest, len);
+ goto Done;
+ }
+ }
+ /* no prefix to replace found, go unchanged */
+ strncat(pathname, fname, pfhdr->fname_length);
+ Done:
+ normpath(pathname);
+ if (pathname[strlen(pathname)-1] != '\\') {
+ /*
+ * The local file header (pfhdr) does not always
+ * contain the compressed and uncompressed sizes of
+ * the data depending on bit 3 of the flags field. So
+ * it seems better to use the data from the central
+ * directory (pcdir).
+ */
+ dst = map_new_file(0, pathname, new_part,
+ pcdir->uncomp_size,
+ pcdir->last_mod_file_date,
+ pcdir->last_mod_file_time, notify);
+ if (dst) {
+ if (!extract_file(dst, pcomp, pfhdr->method,
+ pcdir->comp_size,
+ pcdir->uncomp_size,
+ notify))
+ return FALSE;
+ } /* else ??? */
+ }
+ if (notify)
+ notify(NUM_FILES, new_part, (int)pe->nTotalCDir,
+ (int)n+1);
+ }
+ return TRUE;
}
diff --git a/PC/bdist_wininst/install.c b/PC/bdist_wininst/install.c
index 53e6a51..09dd161 100644
--- a/PC/bdist_wininst/install.c
+++ b/PC/bdist_wininst/install.c
@@ -20,21 +20,21 @@
*
* At runtime, the exefile has appended:
* - compressed setup-data in ini-format, containing the following sections:
- * [metadata]
- * author=Greg Ward
- * author_email=gward@python.net
- * description=Python Distribution Utilities
- * licence=Python
- * name=Distutils
- * url=http://www.python.org/sigs/distutils-sig/
- * version=0.9pre
+ * [metadata]
+ * author=Greg Ward
+ * author_email=gward@python.net
+ * description=Python Distribution Utilities
+ * licence=Python
+ * name=Distutils
+ * url=http://www.python.org/sigs/distutils-sig/
+ * version=0.9pre
*
- * [Setup]
- * info= text to be displayed in the edit-box
- * title= to be displayed by this program
- * target_version = if present, python version required
- * pyc_compile = if 0, do not compile py to pyc
- * pyo_compile = if 0, do not compile py to pyo
+ * [Setup]
+ * info= text to be displayed in the edit-box
+ * title= to be displayed by this program
+ * target_version = if present, python version required
+ * pyc_compile = if 0, do not compile py to pyc
+ * pyo_compile = if 0, do not compile py to pyo
*
* - a struct meta_data_hdr, describing the above
* - a zip-file, containing the modules to be installed.
@@ -119,28 +119,28 @@ wchar_t wmodulename[MAX_PATH];
HWND hwndMain;
HWND hDialog;
-char *ini_file; /* Full pathname of ini-file */
+char *ini_file; /* Full pathname of ini-file */
/* From ini-file */
-char info[4096]; /* [Setup] info= */
-char title[80]; /* [Setup] title=, contains package name
- including version: "Distutils-1.0.1" */
-char target_version[10]; /* [Setup] target_version=, required python
- version or empty string */
-char build_info[80]; /* [Setup] build_info=, distutils version
- and build date */
-
-char meta_name[80]; /* package name without version like
- 'Distutils' */
+char info[4096]; /* [Setup] info= */
+char title[80]; /* [Setup] title=, contains package name
+ including version: "Distutils-1.0.1" */
+char target_version[10]; /* [Setup] target_version=, required python
+ version or empty string */
+char build_info[80]; /* [Setup] build_info=, distutils version
+ and build date */
+
+char meta_name[80]; /* package name without version like
+ 'Distutils' */
char install_script[MAX_PATH];
char *pre_install_script; /* run before we install a single file */
char user_access_control[10]; // one of 'auto', 'force', otherwise none.
-int py_major, py_minor; /* Python version selected for installation */
+int py_major, py_minor; /* Python version selected for installation */
-char *arc_data; /* memory mapped archive */
-DWORD arc_size; /* number of bytes in archive */
-int exe_size; /* number of bytes for exe-file portion */
+char *arc_data; /* memory mapped archive */
+DWORD arc_size; /* number of bytes in archive */
+int exe_size; /* number of bytes for exe-file portion */
char python_dir[MAX_PATH];
char pythondll[MAX_PATH];
BOOL pyc_compile, pyo_compile;
@@ -148,7 +148,7 @@ BOOL pyc_compile, pyo_compile;
the permissions of the current user. */
HKEY hkey_root = (HKEY)-1;
-BOOL success; /* Installation successfull? */
+BOOL success; /* Installation successfull? */
char *failure_reason = NULL;
HANDLE hBitmap;
@@ -166,128 +166,128 @@ static BOOL notify(int code, char *fmt, ...);
/* Note: If scheme.prefix is nonempty, it must end with a '\'! */
/* Note: purelib must be the FIRST entry! */
SCHEME old_scheme[] = {
- { "PURELIB", "" },
- { "PLATLIB", "" },
- { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */
- { "SCRIPTS", "Scripts\\" },
- { "DATA", "" },
- { NULL, NULL },
+ { "PURELIB", "" },
+ { "PLATLIB", "" },
+ { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */
+ { "SCRIPTS", "Scripts\\" },
+ { "DATA", "" },
+ { NULL, NULL },
};
SCHEME new_scheme[] = {
- { "PURELIB", "Lib\\site-packages\\" },
- { "PLATLIB", "Lib\\site-packages\\" },
- { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */
- { "SCRIPTS", "Scripts\\" },
- { "DATA", "" },
- { NULL, NULL },
+ { "PURELIB", "Lib\\site-packages\\" },
+ { "PLATLIB", "Lib\\site-packages\\" },
+ { "HEADERS", "" }, /* 'Include/dist_name' part already in archive */
+ { "SCRIPTS", "Scripts\\" },
+ { "DATA", "" },
+ { NULL, NULL },
};
static void unescape(char *dst, char *src, unsigned size)
{
- char *eon;
- char ch;
-
- while (src && *src && (size > 2)) {
- if (*src == '\\') {
- switch (*++src) {
- case 'n':
- ++src;
- *dst++ = '\r';
- *dst++ = '\n';
- size -= 2;
- break;
- case 'r':
- ++src;
- *dst++ = '\r';
- --size;
- break;
- case '0': case '1': case '2': case '3':
- ch = (char)strtol(src, &eon, 8);
- if (ch == '\n') {
- *dst++ = '\r';
- --size;
- }
- *dst++ = ch;
- --size;
- src = eon;
- }
- } else {
- *dst++ = *src++;
- --size;
- }
- }
- *dst = '\0';
+ char *eon;
+ char ch;
+
+ while (src && *src && (size > 2)) {
+ if (*src == '\\') {
+ switch (*++src) {
+ case 'n':
+ ++src;
+ *dst++ = '\r';
+ *dst++ = '\n';
+ size -= 2;
+ break;
+ case 'r':
+ ++src;
+ *dst++ = '\r';
+ --size;
+ break;
+ case '0': case '1': case '2': case '3':
+ ch = (char)strtol(src, &eon, 8);
+ if (ch == '\n') {
+ *dst++ = '\r';
+ --size;
+ }
+ *dst++ = ch;
+ --size;
+ src = eon;
+ }
+ } else {
+ *dst++ = *src++;
+ --size;
+ }
+ }
+ *dst = '\0';
}
static struct tagFile {
- char *path;
- struct tagFile *next;
+ char *path;
+ struct tagFile *next;
} *file_list = NULL;
static void set_failure_reason(char *reason)
{
if (failure_reason)
- free(failure_reason);
+ free(failure_reason);
failure_reason = strdup(reason);
success = FALSE;
}
static char *get_failure_reason()
{
if (!failure_reason)
- return "Installation failed.";
+ return "Installation failed.";
return failure_reason;
}
static void add_to_filelist(char *path)
{
- struct tagFile *p;
- p = (struct tagFile *)malloc(sizeof(struct tagFile));
- p->path = strdup(path);
- p->next = file_list;
- file_list = p;
+ struct tagFile *p;
+ p = (struct tagFile *)malloc(sizeof(struct tagFile));
+ p->path = strdup(path);
+ p->next = file_list;
+ file_list = p;
}
static int do_compile_files(int (__cdecl * PyRun_SimpleString)(char *),
- int optimize)
+ int optimize)
{
- struct tagFile *p;
- int total, n;
- char Buffer[MAX_PATH + 64];
- int errors = 0;
-
- total = 0;
- p = file_list;
- while (p) {
- ++total;
- p = p->next;
- }
- SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETRANGE, 0,
- MAKELPARAM(0, total));
- SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, 0, 0);
-
- n = 0;
- p = file_list;
- while (p) {
- ++n;
- wsprintf(Buffer,
- "import py_compile; py_compile.compile (r'%s')",
- p->path);
- if (PyRun_SimpleString(Buffer)) {
- ++errors;
- }
- /* We send the notification even if the files could not
- * be created so that the uninstaller will remove them
- * in case they are created later.
- */
- wsprintf(Buffer, "%s%c", p->path, optimize ? 'o' : 'c');
- notify(FILE_CREATED, Buffer);
-
- SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, n, 0);
- SetDlgItemText(hDialog, IDC_INFO, p->path);
- p = p->next;
- }
- return errors;
+ struct tagFile *p;
+ int total, n;
+ char Buffer[MAX_PATH + 64];
+ int errors = 0;
+
+ total = 0;
+ p = file_list;
+ while (p) {
+ ++total;
+ p = p->next;
+ }
+ SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETRANGE, 0,
+ MAKELPARAM(0, total));
+ SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, 0, 0);
+
+ n = 0;
+ p = file_list;
+ while (p) {
+ ++n;
+ wsprintf(Buffer,
+ "import py_compile; py_compile.compile (r'%s')",
+ p->path);
+ if (PyRun_SimpleString(Buffer)) {
+ ++errors;
+ }
+ /* We send the notification even if the files could not
+ * be created so that the uninstaller will remove them
+ * in case they are created later.
+ */
+ wsprintf(Buffer, "%s%c", p->path, optimize ? 'o' : 'c');
+ notify(FILE_CREATED, Buffer);
+
+ SendDlgItemMessage(hDialog, IDC_PROGRESS, PBM_SETPOS, n, 0);
+ SetDlgItemText(hDialog, IDC_INFO, p->path);
+ p = p->next;
+ }
+ return errors;
}
#define DECLPROC(dll, result, name, args)\
@@ -304,22 +304,22 @@ typedef void PyObject;
// Result string must be free'd
wchar_t *widen_string(char *src)
{
- wchar_t *result;
- DWORD dest_cch;
- int src_len = strlen(src) + 1; // include NULL term in all ops
- /* use MultiByteToWideChar() to see how much we need. */
- /* NOTE: this will include the null-term in the length */
- dest_cch = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
- // alloc the buffer
- result = (wchar_t *)malloc(dest_cch * sizeof(wchar_t));
- if (result==NULL)
- return NULL;
- /* do the conversion */
- if (0==MultiByteToWideChar(CP_ACP, 0, src, src_len, result, dest_cch)) {
- free(result);
- return NULL;
- }
- return result;
+ wchar_t *result;
+ DWORD dest_cch;
+ int src_len = strlen(src) + 1; // include NULL term in all ops
+ /* use MultiByteToWideChar() to see how much we need. */
+ /* NOTE: this will include the null-term in the length */
+ dest_cch = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
+ // alloc the buffer
+ result = (wchar_t *)malloc(dest_cch * sizeof(wchar_t));
+ if (result==NULL)
+ return NULL;
+ /* do the conversion */
+ if (0==MultiByteToWideChar(CP_ACP, 0, src, src_len, result, dest_cch)) {
+ free(result);
+ return NULL;
+ }
+ return result;
}
/*
@@ -328,47 +328,47 @@ wchar_t *widen_string(char *src)
*/
static int compile_filelist(HINSTANCE hPython, BOOL optimize_flag)
{
- DECLPROC(hPython, void, Py_Initialize, (void));
- DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
- DECLPROC(hPython, void, Py_Finalize, (void));
- DECLPROC(hPython, int, PyRun_SimpleString, (char *));
- DECLPROC(hPython, PyObject *, PySys_GetObject, (char *));
- DECLVAR(hPython, int, Py_OptimizeFlag);
+ DECLPROC(hPython, void, Py_Initialize, (void));
+ DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
+ DECLPROC(hPython, void, Py_Finalize, (void));
+ DECLPROC(hPython, int, PyRun_SimpleString, (char *));
+ DECLPROC(hPython, PyObject *, PySys_GetObject, (char *));
+ DECLVAR(hPython, int, Py_OptimizeFlag);
- int errors = 0;
- struct tagFile *p = file_list;
+ int errors = 0;
+ struct tagFile *p = file_list;
- if (!p)
- return 0;
+ if (!p)
+ return 0;
- if (!Py_Initialize || !Py_SetProgramName || !Py_Finalize)
- return -1;
+ if (!Py_Initialize || !Py_SetProgramName || !Py_Finalize)
+ return -1;
- if (!PyRun_SimpleString || !PySys_GetObject || !Py_OptimizeFlag)
- return -1;
+ if (!PyRun_SimpleString || !PySys_GetObject || !Py_OptimizeFlag)
+ return -1;
- *Py_OptimizeFlag = optimize_flag ? 1 : 0;
- Py_SetProgramName(wmodulename);
- Py_Initialize();
+ *Py_OptimizeFlag = optimize_flag ? 1 : 0;
+ Py_SetProgramName(wmodulename);
+ Py_Initialize();
- errors += do_compile_files(PyRun_SimpleString, optimize_flag);
- Py_Finalize();
+ errors += do_compile_files(PyRun_SimpleString, optimize_flag);
+ Py_Finalize();
- return errors;
+ return errors;
}
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
struct PyMethodDef {
- char *ml_name;
- PyCFunction ml_meth;
- int ml_flags;
- char *ml_doc;
+ char *ml_name;
+ PyCFunction ml_meth;
+ int ml_flags;
+ char *ml_doc;
};
typedef struct PyMethodDef PyMethodDef;
// XXX - all of these are potentially fragile! We load and unload
-// the Python DLL multiple times - so storing functions pointers
+// the Python DLL multiple times - so storing functions pointers
// is dangerous (although things *look* OK at present)
// Better might be to roll prepare_script_environment() into
// LoadPythonDll(), and create a new UnloadPythonDLL() which also
@@ -385,249 +385,249 @@ PyObject *(*g_PyErr_Format)(PyObject *, char *, ...);
#define DEF_CSIDL(name) { name, #name }
struct {
- int nFolder;
- char *name;
+ int nFolder;
+ char *name;
} csidl_names[] = {
- /* Startup menu for all users.
- NT only */
- DEF_CSIDL(CSIDL_COMMON_STARTMENU),
- /* Startup menu. */
- DEF_CSIDL(CSIDL_STARTMENU),
+ /* Startup menu for all users.
+ NT only */
+ DEF_CSIDL(CSIDL_COMMON_STARTMENU),
+ /* Startup menu. */
+ DEF_CSIDL(CSIDL_STARTMENU),
/* DEF_CSIDL(CSIDL_COMMON_APPDATA), */
/* DEF_CSIDL(CSIDL_LOCAL_APPDATA), */
- /* Repository for application-specific data.
- Needs Internet Explorer 4.0 */
- DEF_CSIDL(CSIDL_APPDATA),
-
- /* The desktop for all users.
- NT only */
- DEF_CSIDL(CSIDL_COMMON_DESKTOPDIRECTORY),
- /* The desktop. */
- DEF_CSIDL(CSIDL_DESKTOPDIRECTORY),
-
- /* Startup folder for all users.
- NT only */
- DEF_CSIDL(CSIDL_COMMON_STARTUP),
- /* Startup folder. */
- DEF_CSIDL(CSIDL_STARTUP),
-
- /* Programs item in the start menu for all users.
- NT only */
- DEF_CSIDL(CSIDL_COMMON_PROGRAMS),
- /* Program item in the user's start menu. */
- DEF_CSIDL(CSIDL_PROGRAMS),
+ /* Repository for application-specific data.
+ Needs Internet Explorer 4.0 */
+ DEF_CSIDL(CSIDL_APPDATA),
+
+ /* The desktop for all users.
+ NT only */
+ DEF_CSIDL(CSIDL_COMMON_DESKTOPDIRECTORY),
+ /* The desktop. */
+ DEF_CSIDL(CSIDL_DESKTOPDIRECTORY),
+
+ /* Startup folder for all users.
+ NT only */
+ DEF_CSIDL(CSIDL_COMMON_STARTUP),
+ /* Startup folder. */
+ DEF_CSIDL(CSIDL_STARTUP),
+
+ /* Programs item in the start menu for all users.
+ NT only */
+ DEF_CSIDL(CSIDL_COMMON_PROGRAMS),
+ /* Program item in the user's start menu. */
+ DEF_CSIDL(CSIDL_PROGRAMS),
/* DEF_CSIDL(CSIDL_PROGRAM_FILES_COMMON), */
/* DEF_CSIDL(CSIDL_PROGRAM_FILES), */
- /* Virtual folder containing fonts. */
- DEF_CSIDL(CSIDL_FONTS),
+ /* Virtual folder containing fonts. */
+ DEF_CSIDL(CSIDL_FONTS),
};
#define DIM(a) (sizeof(a) / sizeof((a)[0]))
static PyObject *FileCreated(PyObject *self, PyObject *args)
{
- char *path;
- if (!g_PyArg_ParseTuple(args, "s", &path))
- return NULL;
- notify(FILE_CREATED, path);
- return g_Py_BuildValue("");
+ char *path;
+ if (!g_PyArg_ParseTuple(args, "s", &path))
+ return NULL;
+ notify(FILE_CREATED, path);
+ return g_Py_BuildValue("");
}
static PyObject *DirectoryCreated(PyObject *self, PyObject *args)
{
- char *path;
- if (!g_PyArg_ParseTuple(args, "s", &path))
- return NULL;
- notify(DIR_CREATED, path);
- return g_Py_BuildValue("");
+ char *path;
+ if (!g_PyArg_ParseTuple(args, "s", &path))
+ return NULL;
+ notify(DIR_CREATED, path);
+ return g_Py_BuildValue("");
}
static PyObject *GetSpecialFolderPath(PyObject *self, PyObject *args)
{
- char *name;
- char lpszPath[MAX_PATH];
- int i;
- static HRESULT (WINAPI *My_SHGetSpecialFolderPath)(HWND hwnd,
- LPTSTR lpszPath,
- int nFolder,
- BOOL fCreate);
-
- if (!My_SHGetSpecialFolderPath) {
- HINSTANCE hLib = LoadLibrary("shell32.dll");
- if (!hLib) {
- g_PyErr_Format(g_PyExc_OSError,
- "function not available");
- return NULL;
- }
- My_SHGetSpecialFolderPath = (BOOL (WINAPI *)(HWND, LPTSTR,
- int, BOOL))
- GetProcAddress(hLib,
- "SHGetSpecialFolderPathA");
- }
-
- if (!g_PyArg_ParseTuple(args, "s", &name))
- return NULL;
-
- if (!My_SHGetSpecialFolderPath) {
- g_PyErr_Format(g_PyExc_OSError, "function not available");
- return NULL;
- }
-
- for (i = 0; i < DIM(csidl_names); ++i) {
- if (0 == strcmpi(csidl_names[i].name, name)) {
- int nFolder;
- nFolder = csidl_names[i].nFolder;
- if (My_SHGetSpecialFolderPath(NULL, lpszPath,
- nFolder, 0))
- return g_Py_BuildValue("s", lpszPath);
- else {
- g_PyErr_Format(g_PyExc_OSError,
- "no such folder (%s)", name);
- return NULL;
- }
-
- }
- };
- g_PyErr_Format(g_PyExc_ValueError, "unknown CSIDL (%s)", name);
- return NULL;
+ char *name;
+ char lpszPath[MAX_PATH];
+ int i;
+ static HRESULT (WINAPI *My_SHGetSpecialFolderPath)(HWND hwnd,
+ LPTSTR lpszPath,
+ int nFolder,
+ BOOL fCreate);
+
+ if (!My_SHGetSpecialFolderPath) {
+ HINSTANCE hLib = LoadLibrary("shell32.dll");
+ if (!hLib) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "function not available");
+ return NULL;
+ }
+ My_SHGetSpecialFolderPath = (BOOL (WINAPI *)(HWND, LPTSTR,
+ int, BOOL))
+ GetProcAddress(hLib,
+ "SHGetSpecialFolderPathA");
+ }
+
+ if (!g_PyArg_ParseTuple(args, "s", &name))
+ return NULL;
+
+ if (!My_SHGetSpecialFolderPath) {
+ g_PyErr_Format(g_PyExc_OSError, "function not available");
+ return NULL;
+ }
+
+ for (i = 0; i < DIM(csidl_names); ++i) {
+ if (0 == strcmpi(csidl_names[i].name, name)) {
+ int nFolder;
+ nFolder = csidl_names[i].nFolder;
+ if (My_SHGetSpecialFolderPath(NULL, lpszPath,
+ nFolder, 0))
+ return g_Py_BuildValue("s", lpszPath);
+ else {
+ g_PyErr_Format(g_PyExc_OSError,
+ "no such folder (%s)", name);
+ return NULL;
+ }
+
+ }
+ };
+ g_PyErr_Format(g_PyExc_ValueError, "unknown CSIDL (%s)", name);
+ return NULL;
}
static PyObject *CreateShortcut(PyObject *self, PyObject *args)
{
- char *path; /* path and filename */
- char *description;
- char *filename;
-
- char *arguments = NULL;
- char *iconpath = NULL;
- int iconindex = 0;
- char *workdir = NULL;
-
- WCHAR wszFilename[MAX_PATH];
-
- IShellLink *ps1 = NULL;
- IPersistFile *pPf = NULL;
-
- HRESULT hr;
-
- hr = CoInitialize(NULL);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "CoInitialize failed, error 0x%x", hr);
- goto error;
- }
-
- if (!g_PyArg_ParseTuple(args, "sss|sssi",
- &path, &description, &filename,
- &arguments, &workdir, &iconpath, &iconindex))
- return NULL;
-
- hr = CoCreateInstance(&CLSID_ShellLink,
- NULL,
- CLSCTX_INPROC_SERVER,
- &IID_IShellLink,
- &ps1);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "CoCreateInstance failed, error 0x%x", hr);
- goto error;
- }
-
- hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile,
- (void **)&pPf);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "QueryInterface(IPersistFile) error 0x%x", hr);
- goto error;
- }
-
-
- hr = ps1->lpVtbl->SetPath(ps1, path);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "SetPath() failed, error 0x%x", hr);
- goto error;
- }
-
- hr = ps1->lpVtbl->SetDescription(ps1, description);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "SetDescription() failed, error 0x%x", hr);
- goto error;
- }
-
- if (arguments) {
- hr = ps1->lpVtbl->SetArguments(ps1, arguments);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "SetArguments() error 0x%x", hr);
- goto error;
- }
- }
-
- if (iconpath) {
- hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "SetIconLocation() error 0x%x", hr);
- goto error;
- }
- }
-
- if (workdir) {
- hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "SetWorkingDirectory() error 0x%x", hr);
- goto error;
- }
- }
-
- MultiByteToWideChar(CP_ACP, 0,
- filename, -1,
- wszFilename, MAX_PATH);
-
- hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
- if (FAILED(hr)) {
- g_PyErr_Format(g_PyExc_OSError,
- "Failed to create shortcut '%s' - error 0x%x", filename, hr);
- goto error;
- }
-
- pPf->lpVtbl->Release(pPf);
- ps1->lpVtbl->Release(ps1);
- CoUninitialize();
- return g_Py_BuildValue("");
-
+ char *path; /* path and filename */
+ char *description;
+ char *filename;
+
+ char *arguments = NULL;
+ char *iconpath = NULL;
+ int iconindex = 0;
+ char *workdir = NULL;
+
+ WCHAR wszFilename[MAX_PATH];
+
+ IShellLink *ps1 = NULL;
+ IPersistFile *pPf = NULL;
+
+ HRESULT hr;
+
+ hr = CoInitialize(NULL);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "CoInitialize failed, error 0x%x", hr);
+ goto error;
+ }
+
+ if (!g_PyArg_ParseTuple(args, "sss|sssi",
+ &path, &description, &filename,
+ &arguments, &workdir, &iconpath, &iconindex))
+ return NULL;
+
+ hr = CoCreateInstance(&CLSID_ShellLink,
+ NULL,
+ CLSCTX_INPROC_SERVER,
+ &IID_IShellLink,
+ &ps1);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "CoCreateInstance failed, error 0x%x", hr);
+ goto error;
+ }
+
+ hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile,
+ (void **)&pPf);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "QueryInterface(IPersistFile) error 0x%x", hr);
+ goto error;
+ }
+
+
+ hr = ps1->lpVtbl->SetPath(ps1, path);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "SetPath() failed, error 0x%x", hr);
+ goto error;
+ }
+
+ hr = ps1->lpVtbl->SetDescription(ps1, description);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "SetDescription() failed, error 0x%x", hr);
+ goto error;
+ }
+
+ if (arguments) {
+ hr = ps1->lpVtbl->SetArguments(ps1, arguments);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "SetArguments() error 0x%x", hr);
+ goto error;
+ }
+ }
+
+ if (iconpath) {
+ hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "SetIconLocation() error 0x%x", hr);
+ goto error;
+ }
+ }
+
+ if (workdir) {
+ hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "SetWorkingDirectory() error 0x%x", hr);
+ goto error;
+ }
+ }
+
+ MultiByteToWideChar(CP_ACP, 0,
+ filename, -1,
+ wszFilename, MAX_PATH);
+
+ hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
+ if (FAILED(hr)) {
+ g_PyErr_Format(g_PyExc_OSError,
+ "Failed to create shortcut '%s' - error 0x%x", filename, hr);
+ goto error;
+ }
+
+ pPf->lpVtbl->Release(pPf);
+ ps1->lpVtbl->Release(ps1);
+ CoUninitialize();
+ return g_Py_BuildValue("");
+
error:
- if (pPf)
- pPf->lpVtbl->Release(pPf);
+ if (pPf)
+ pPf->lpVtbl->Release(pPf);
- if (ps1)
- ps1->lpVtbl->Release(ps1);
+ if (ps1)
+ ps1->lpVtbl->Release(ps1);
- CoUninitialize();
+ CoUninitialize();
- return NULL;
+ return NULL;
}
static PyObject *PyMessageBox(PyObject *self, PyObject *args)
{
- int rc;
- char *text, *caption;
- int flags;
- if (!g_PyArg_ParseTuple(args, "ssi", &text, &caption, &flags))
- return NULL;
- rc = MessageBox(GetFocus(), text, caption, flags);
- return g_Py_BuildValue("i", rc);
+ int rc;
+ char *text, *caption;
+ int flags;
+ if (!g_PyArg_ParseTuple(args, "ssi", &text, &caption, &flags))
+ return NULL;
+ rc = MessageBox(GetFocus(), text, caption, flags);
+ return g_Py_BuildValue("i", rc);
}
static PyObject *GetRootHKey(PyObject *self)
{
- return g_PyLong_FromVoidPtr(hkey_root);
+ return g_PyLong_FromVoidPtr(hkey_root);
}
#define METH_VARARGS 0x0001
@@ -635,74 +635,74 @@ static PyObject *GetRootHKey(PyObject *self)
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
PyMethodDef meth[] = {
- {"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
- {"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
- {"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL},
- {"file_created", FileCreated, METH_VARARGS, NULL},
- {"directory_created", DirectoryCreated, METH_VARARGS, NULL},
- {"message_box", PyMessageBox, METH_VARARGS, NULL},
+ {"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
+ {"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
+ {"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL},
+ {"file_created", FileCreated, METH_VARARGS, NULL},
+ {"directory_created", DirectoryCreated, METH_VARARGS, NULL},
+ {"message_box", PyMessageBox, METH_VARARGS, NULL},
};
static HINSTANCE LoadPythonDll(char *fname)
{
- char fullpath[_MAX_PATH];
- LONG size = sizeof(fullpath);
- char subkey_name[80];
- char buffer[260 + 12];
- HINSTANCE h;
-
- /* make sure PYTHONHOME is set, to that sys.path is initialized correctly */
- wsprintf(buffer, "PYTHONHOME=%s", python_dir);
- _putenv(buffer);
- h = LoadLibrary(fname);
- if (h)
- return h;
- wsprintf(subkey_name,
- "SOFTWARE\\Python\\PythonCore\\%d.%d\\InstallPath",
- py_major, py_minor);
- if (ERROR_SUCCESS != RegQueryValue(HKEY_CURRENT_USER, subkey_name,
- fullpath, &size) &&
- ERROR_SUCCESS != RegQueryValue(HKEY_LOCAL_MACHINE, subkey_name,
- fullpath, &size))
- return NULL;
- strcat(fullpath, "\\");
- strcat(fullpath, fname);
- return LoadLibrary(fullpath);
+ char fullpath[_MAX_PATH];
+ LONG size = sizeof(fullpath);
+ char subkey_name[80];
+ char buffer[260 + 12];
+ HINSTANCE h;
+
+ /* make sure PYTHONHOME is set, to that sys.path is initialized correctly */
+ wsprintf(buffer, "PYTHONHOME=%s", python_dir);
+ _putenv(buffer);
+ h = LoadLibrary(fname);
+ if (h)
+ return h;
+ wsprintf(subkey_name,
+ "SOFTWARE\\Python\\PythonCore\\%d.%d\\InstallPath",
+ py_major, py_minor);
+ if (ERROR_SUCCESS != RegQueryValue(HKEY_CURRENT_USER, subkey_name,
+ fullpath, &size) &&
+ ERROR_SUCCESS != RegQueryValue(HKEY_LOCAL_MACHINE, subkey_name,
+ fullpath, &size))
+ return NULL;
+ strcat(fullpath, "\\");
+ strcat(fullpath, fname);
+ return LoadLibrary(fullpath);
}
static int prepare_script_environment(HINSTANCE hPython)
{
- PyObject *mod;
- DECLPROC(hPython, PyObject *, PyImport_ImportModule, (char *));
- DECLPROC(hPython, int, PyObject_SetAttrString, (PyObject *, char *, PyObject *));
- DECLPROC(hPython, PyObject *, PyObject_GetAttrString, (PyObject *, char *));
- DECLPROC(hPython, PyObject *, PyCFunction_New, (PyMethodDef *, PyObject *));
- DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
- DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...));
- DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *));
- DECLPROC(hPython, PyObject *, PyLong_FromVoidPtr, (void *));
- if (!PyImport_ImportModule || !PyObject_GetAttrString ||
- !PyObject_SetAttrString || !PyCFunction_New)
- return 1;
- if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format)
- return 1;
-
- mod = PyImport_ImportModule("builtins");
- if (mod) {
- int i;
- g_PyExc_ValueError = PyObject_GetAttrString(mod, "ValueError");
- g_PyExc_OSError = PyObject_GetAttrString(mod, "OSError");
- for (i = 0; i < DIM(meth); ++i) {
- PyObject_SetAttrString(mod, meth[i].ml_name,
- PyCFunction_New(&meth[i], NULL));
- }
- }
- g_Py_BuildValue = Py_BuildValue;
- g_PyArg_ParseTuple = PyArg_ParseTuple;
- g_PyErr_Format = PyErr_Format;
- g_PyLong_FromVoidPtr = PyLong_FromVoidPtr;
-
- return 0;
+ PyObject *mod;
+ DECLPROC(hPython, PyObject *, PyImport_ImportModule, (char *));
+ DECLPROC(hPython, int, PyObject_SetAttrString, (PyObject *, char *, PyObject *));
+ DECLPROC(hPython, PyObject *, PyObject_GetAttrString, (PyObject *, char *));
+ DECLPROC(hPython, PyObject *, PyCFunction_New, (PyMethodDef *, PyObject *));
+ DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
+ DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...));
+ DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *));
+ DECLPROC(hPython, PyObject *, PyLong_FromVoidPtr, (void *));
+ if (!PyImport_ImportModule || !PyObject_GetAttrString ||
+ !PyObject_SetAttrString || !PyCFunction_New)
+ return 1;
+ if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format)
+ return 1;
+
+ mod = PyImport_ImportModule("builtins");
+ if (mod) {
+ int i;
+ g_PyExc_ValueError = PyObject_GetAttrString(mod, "ValueError");
+ g_PyExc_OSError = PyObject_GetAttrString(mod, "OSError");
+ for (i = 0; i < DIM(meth); ++i) {
+ PyObject_SetAttrString(mod, meth[i].ml_name,
+ PyCFunction_New(&meth[i], NULL));
+ }
+ }
+ g_Py_BuildValue = Py_BuildValue;
+ g_PyArg_ParseTuple = PyArg_ParseTuple;
+ g_PyErr_Format = PyErr_Format;
+ g_PyLong_FromVoidPtr = PyLong_FromVoidPtr;
+
+ return 0;
}
/*
@@ -718,483 +718,483 @@ static int prepare_script_environment(HINSTANCE hPython)
static int
do_run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
{
- int fh, result, i;
- static wchar_t *wargv[256];
- DECLPROC(hPython, void, Py_Initialize, (void));
- DECLPROC(hPython, int, PySys_SetArgv, (int, wchar_t **));
- DECLPROC(hPython, int, PyRun_SimpleString, (char *));
- DECLPROC(hPython, void, Py_Finalize, (void));
- DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
- DECLPROC(hPython, PyObject *, PyCFunction_New,
- (PyMethodDef *, PyObject *));
- DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...));
- DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *));
-
- if (!Py_Initialize || !PySys_SetArgv
- || !PyRun_SimpleString || !Py_Finalize)
- return 1;
-
- if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format)
- return 1;
-
- if (!PyCFunction_New || !PyArg_ParseTuple || !PyErr_Format)
- return 1;
-
- if (pathname == NULL || pathname[0] == '\0')
- return 2;
-
- fh = open(pathname, _O_RDONLY);
- if (-1 == fh) {
- fprintf(stderr, "Could not open postinstall-script %s\n",
- pathname);
- return 3;
- }
-
- SetDlgItemText(hDialog, IDC_INFO, "Running Script...");
-
- Py_Initialize();
-
- prepare_script_environment(hPython);
- // widen the argv array for py3k.
- memset(wargv, 0, sizeof(wargv));
- for (i=0;i<argc;i++)
- wargv[i] = argv[i] ? widen_string(argv[i]) : NULL;
- PySys_SetArgv(argc, wargv);
- // free the strings we just widened.
- for (i=0;i<argc;i++)
- if (wargv[i])
- free(wargv[i]);
-
- result = 3;
- {
- struct _stat statbuf;
- if(0 == _fstat(fh, &statbuf)) {
- char *script = alloca(statbuf.st_size + 5);
- int n = read(fh, script, statbuf.st_size);
- if (n > 0) {
- script[n] = '\n';
- script[n+1] = 0;
- result = PyRun_SimpleString(script);
- }
- }
- }
- Py_Finalize();
-
- close(fh);
- return result;
+ int fh, result, i;
+ static wchar_t *wargv[256];
+ DECLPROC(hPython, void, Py_Initialize, (void));
+ DECLPROC(hPython, int, PySys_SetArgv, (int, wchar_t **));
+ DECLPROC(hPython, int, PyRun_SimpleString, (char *));
+ DECLPROC(hPython, void, Py_Finalize, (void));
+ DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
+ DECLPROC(hPython, PyObject *, PyCFunction_New,
+ (PyMethodDef *, PyObject *));
+ DECLPROC(hPython, int, PyArg_ParseTuple, (PyObject *, char *, ...));
+ DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *));
+
+ if (!Py_Initialize || !PySys_SetArgv
+ || !PyRun_SimpleString || !Py_Finalize)
+ return 1;
+
+ if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format)
+ return 1;
+
+ if (!PyCFunction_New || !PyArg_ParseTuple || !PyErr_Format)
+ return 1;
+
+ if (pathname == NULL || pathname[0] == '\0')
+ return 2;
+
+ fh = open(pathname, _O_RDONLY);
+ if (-1 == fh) {
+ fprintf(stderr, "Could not open postinstall-script %s\n",
+ pathname);
+ return 3;
+ }
+
+ SetDlgItemText(hDialog, IDC_INFO, "Running Script...");
+
+ Py_Initialize();
+
+ prepare_script_environment(hPython);
+ // widen the argv array for py3k.
+ memset(wargv, 0, sizeof(wargv));
+ for (i=0;i<argc;i++)
+ wargv[i] = argv[i] ? widen_string(argv[i]) : NULL;
+ PySys_SetArgv(argc, wargv);
+ // free the strings we just widened.
+ for (i=0;i<argc;i++)
+ if (wargv[i])
+ free(wargv[i]);
+
+ result = 3;
+ {
+ struct _stat statbuf;
+ if(0 == _fstat(fh, &statbuf)) {
+ char *script = alloca(statbuf.st_size + 5);
+ int n = read(fh, script, statbuf.st_size);
+ if (n > 0) {
+ script[n] = '\n';
+ script[n+1] = 0;
+ result = PyRun_SimpleString(script);
+ }
+ }
+ }
+ Py_Finalize();
+
+ close(fh);
+ return result;
}
static int
run_installscript(char *pathname, int argc, char **argv, char **pOutput)
{
- HINSTANCE hPython;
- int result = 1;
- int out_buf_size;
- HANDLE redirected, old_stderr, old_stdout;
- char *tempname;
-
- *pOutput = NULL;
-
- tempname = tempnam(NULL, NULL);
- // We use a static CRT while the Python version we load uses
- // the CRT from one of various possibile DLLs. As a result we
- // need to redirect the standard handles using the API rather
- // than the CRT.
- redirected = CreateFile(
- tempname,
- GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
- NULL);
- old_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
- old_stderr = GetStdHandle(STD_ERROR_HANDLE);
- SetStdHandle(STD_OUTPUT_HANDLE, redirected);
- SetStdHandle(STD_ERROR_HANDLE, redirected);
-
- hPython = LoadPythonDll(pythondll);
- if (hPython) {
- result = do_run_installscript(hPython, pathname, argc, argv);
- FreeLibrary(hPython);
- } else {
- fprintf(stderr, "*** Could not load Python ***");
- }
- SetStdHandle(STD_OUTPUT_HANDLE, old_stdout);
- SetStdHandle(STD_ERROR_HANDLE, old_stderr);
- out_buf_size = min(GetFileSize(redirected, NULL), 4096);
- *pOutput = malloc(out_buf_size+1);
- if (*pOutput) {
- DWORD nread = 0;
- SetFilePointer(redirected, 0, 0, FILE_BEGIN);
- ReadFile(redirected, *pOutput, out_buf_size, &nread, NULL);
- (*pOutput)[nread] = '\0';
- }
- CloseHandle(redirected);
- DeleteFile(tempname);
- return result;
+ HINSTANCE hPython;
+ int result = 1;
+ int out_buf_size;
+ HANDLE redirected, old_stderr, old_stdout;
+ char *tempname;
+
+ *pOutput = NULL;
+
+ tempname = tempnam(NULL, NULL);
+ // We use a static CRT while the Python version we load uses
+ // the CRT from one of various possibile DLLs. As a result we
+ // need to redirect the standard handles using the API rather
+ // than the CRT.
+ redirected = CreateFile(
+ tempname,
+ GENERIC_WRITE | GENERIC_READ,
+ FILE_SHARE_READ,
+ NULL,
+ CREATE_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
+ NULL);
+ old_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+ old_stderr = GetStdHandle(STD_ERROR_HANDLE);
+ SetStdHandle(STD_OUTPUT_HANDLE, redirected);
+ SetStdHandle(STD_ERROR_HANDLE, redirected);
+
+ hPython = LoadPythonDll(pythondll);
+ if (hPython) {
+ result = do_run_installscript(hPython, pathname, argc, argv);
+ FreeLibrary(hPython);
+ } else {
+ fprintf(stderr, "*** Could not load Python ***");
+ }
+ SetStdHandle(STD_OUTPUT_HANDLE, old_stdout);
+ SetStdHandle(STD_ERROR_HANDLE, old_stderr);
+ out_buf_size = min(GetFileSize(redirected, NULL), 4096);
+ *pOutput = malloc(out_buf_size+1);
+ if (*pOutput) {
+ DWORD nread = 0;
+ SetFilePointer(redirected, 0, 0, FILE_BEGIN);
+ ReadFile(redirected, *pOutput, out_buf_size, &nread, NULL);
+ (*pOutput)[nread] = '\0';
+ }
+ CloseHandle(redirected);
+ DeleteFile(tempname);
+ return result;
}
static int do_run_simple_script(HINSTANCE hPython, char *script)
{
- int rc;
- DECLPROC(hPython, void, Py_Initialize, (void));
- DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
- DECLPROC(hPython, void, Py_Finalize, (void));
- DECLPROC(hPython, int, PyRun_SimpleString, (char *));
- DECLPROC(hPython, void, PyErr_Print, (void));
-
- if (!Py_Initialize || !Py_SetProgramName || !Py_Finalize ||
- !PyRun_SimpleString || !PyErr_Print)
- return -1;
-
- Py_SetProgramName(wmodulename);
- Py_Initialize();
- prepare_script_environment(hPython);
- rc = PyRun_SimpleString(script);
- if (rc)
- PyErr_Print();
- Py_Finalize();
- return rc;
+ int rc;
+ DECLPROC(hPython, void, Py_Initialize, (void));
+ DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
+ DECLPROC(hPython, void, Py_Finalize, (void));
+ DECLPROC(hPython, int, PyRun_SimpleString, (char *));
+ DECLPROC(hPython, void, PyErr_Print, (void));
+
+ if (!Py_Initialize || !Py_SetProgramName || !Py_Finalize ||
+ !PyRun_SimpleString || !PyErr_Print)
+ return -1;
+
+ Py_SetProgramName(wmodulename);
+ Py_Initialize();
+ prepare_script_environment(hPython);
+ rc = PyRun_SimpleString(script);
+ if (rc)
+ PyErr_Print();
+ Py_Finalize();
+ return rc;
}
static int run_simple_script(char *script)
{
- int rc;
- HINSTANCE hPython;
- char *tempname = tempnam(NULL, NULL);
- // Redirect output using win32 API - see comments above...
- HANDLE redirected = CreateFile(
- tempname,
- GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
- NULL);
- HANDLE old_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
- HANDLE old_stderr = GetStdHandle(STD_ERROR_HANDLE);
- SetStdHandle(STD_OUTPUT_HANDLE, redirected);
- SetStdHandle(STD_ERROR_HANDLE, redirected);
-
- hPython = LoadPythonDll(pythondll);
- if (!hPython) {
- char reason[128];
- wsprintf(reason, "Can't load Python for pre-install script (%d)", GetLastError());
- set_failure_reason(reason);
- return -1;
- }
- rc = do_run_simple_script(hPython, script);
- FreeLibrary(hPython);
- SetStdHandle(STD_OUTPUT_HANDLE, old_stdout);
- SetStdHandle(STD_ERROR_HANDLE, old_stderr);
- /* We only care about the output when we fail. If the script works
- OK, then we discard it
- */
- if (rc) {
- int err_buf_size;
- char *err_buf;
- const char *prefix = "Running the pre-installation script failed\r\n";
- int prefix_len = strlen(prefix);
- err_buf_size = GetFileSize(redirected, NULL);
- if (err_buf_size==INVALID_FILE_SIZE) // an error - let's try anyway...
- err_buf_size = 4096;
- err_buf = malloc(prefix_len + err_buf_size + 1);
- if (err_buf) {
- DWORD n = 0;
- strcpy(err_buf, prefix);
- SetFilePointer(redirected, 0, 0, FILE_BEGIN);
- ReadFile(redirected, err_buf+prefix_len, err_buf_size, &n, NULL);
- err_buf[prefix_len+n] = '\0';
- set_failure_reason(err_buf);
- free(err_buf);
- } else {
- set_failure_reason("Out of memory!");
- }
- }
- CloseHandle(redirected);
- DeleteFile(tempname);
- return rc;
+ int rc;
+ HINSTANCE hPython;
+ char *tempname = tempnam(NULL, NULL);
+ // Redirect output using win32 API - see comments above...
+ HANDLE redirected = CreateFile(
+ tempname,
+ GENERIC_WRITE | GENERIC_READ,
+ FILE_SHARE_READ,
+ NULL,
+ CREATE_ALWAYS,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
+ NULL);
+ HANDLE old_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
+ HANDLE old_stderr = GetStdHandle(STD_ERROR_HANDLE);
+ SetStdHandle(STD_OUTPUT_HANDLE, redirected);
+ SetStdHandle(STD_ERROR_HANDLE, redirected);
+
+ hPython = LoadPythonDll(pythondll);
+ if (!hPython) {
+ char reason[128];
+ wsprintf(reason, "Can't load Python for pre-install script (%d)", GetLastError());
+ set_failure_reason(reason);
+ return -1;
+ }
+ rc = do_run_simple_script(hPython, script);
+ FreeLibrary(hPython);
+ SetStdHandle(STD_OUTPUT_HANDLE, old_stdout);
+ SetStdHandle(STD_ERROR_HANDLE, old_stderr);
+ /* We only care about the output when we fail. If the script works
+ OK, then we discard it
+ */
+ if (rc) {
+ int err_buf_size;
+ char *err_buf;
+ const char *prefix = "Running the pre-installation script failed\r\n";
+ int prefix_len = strlen(prefix);
+ err_buf_size = GetFileSize(redirected, NULL);
+ if (err_buf_size==INVALID_FILE_SIZE) // an error - let's try anyway...
+ err_buf_size = 4096;
+ err_buf = malloc(prefix_len + err_buf_size + 1);
+ if (err_buf) {
+ DWORD n = 0;
+ strcpy(err_buf, prefix);
+ SetFilePointer(redirected, 0, 0, FILE_BEGIN);
+ ReadFile(redirected, err_buf+prefix_len, err_buf_size, &n, NULL);
+ err_buf[prefix_len+n] = '\0';
+ set_failure_reason(err_buf);
+ free(err_buf);
+ } else {
+ set_failure_reason("Out of memory!");
+ }
+ }
+ CloseHandle(redirected);
+ DeleteFile(tempname);
+ return rc;
}
static BOOL SystemError(int error, char *msg)
{
- char Buffer[1024];
- int n;
-
- if (error) {
- LPVOID lpMsgBuf;
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- error,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPSTR)&lpMsgBuf,
- 0,
- NULL
- );
- strncpy(Buffer, lpMsgBuf, sizeof(Buffer));
- LocalFree(lpMsgBuf);
- } else
- Buffer[0] = '\0';
- n = lstrlen(Buffer);
- _snprintf(Buffer+n, sizeof(Buffer)-n, msg);
- MessageBox(hwndMain, Buffer, "Runtime Error", MB_OK | MB_ICONSTOP);
- return FALSE;
+ char Buffer[1024];
+ int n;
+
+ if (error) {
+ LPVOID lpMsgBuf;
+ FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL,
+ error,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPSTR)&lpMsgBuf,
+ 0,
+ NULL
+ );
+ strncpy(Buffer, lpMsgBuf, sizeof(Buffer));
+ LocalFree(lpMsgBuf);
+ } else
+ Buffer[0] = '\0';
+ n = lstrlen(Buffer);
+ _snprintf(Buffer+n, sizeof(Buffer)-n, msg);
+ MessageBox(hwndMain, Buffer, "Runtime Error", MB_OK | MB_ICONSTOP);
+ return FALSE;
}
static BOOL notify (int code, char *fmt, ...)
{
- char Buffer[1024];
- va_list marker;
- BOOL result = TRUE;
- int a, b;
- char *cp;
+ char Buffer[1024];
+ va_list marker;
+ BOOL result = TRUE;
+ int a, b;
+ char *cp;
- va_start(marker, fmt);
- _vsnprintf(Buffer, sizeof(Buffer), fmt, marker);
+ va_start(marker, fmt);
+ _vsnprintf(Buffer, sizeof(Buffer), fmt, marker);
- switch (code) {
+ switch (code) {
/* Questions */
- case CAN_OVERWRITE:
- break;
+ case CAN_OVERWRITE:
+ break;
/* Information notification */
- case DIR_CREATED:
- if (logfile)
- fprintf(logfile, "100 Made Dir: %s\n", fmt);
- break;
-
- case FILE_CREATED:
- if (logfile)
- fprintf(logfile, "200 File Copy: %s\n", fmt);
- goto add_to_filelist_label;
- break;
-
- case FILE_OVERWRITTEN:
- if (logfile)
- fprintf(logfile, "200 File Overwrite: %s\n", fmt);
- add_to_filelist_label:
- if ((cp = strrchr(fmt, '.')) && (0 == strcmp (cp, ".py")))
- add_to_filelist(fmt);
- break;
+ case DIR_CREATED:
+ if (logfile)
+ fprintf(logfile, "100 Made Dir: %s\n", fmt);
+ break;
+
+ case FILE_CREATED:
+ if (logfile)
+ fprintf(logfile, "200 File Copy: %s\n", fmt);
+ goto add_to_filelist_label;
+ break;
+
+ case FILE_OVERWRITTEN:
+ if (logfile)
+ fprintf(logfile, "200 File Overwrite: %s\n", fmt);
+ add_to_filelist_label:
+ if ((cp = strrchr(fmt, '.')) && (0 == strcmp (cp, ".py")))
+ add_to_filelist(fmt);
+ break;
/* Error Messages */
- case ZLIB_ERROR:
- MessageBox(GetFocus(), Buffer, "Error",
- MB_OK | MB_ICONWARNING);
- break;
-
- case SYSTEM_ERROR:
- SystemError(GetLastError(), Buffer);
- break;
-
- case NUM_FILES:
- a = va_arg(marker, int);
- b = va_arg(marker, int);
- SendMessage(hDialog, WM_NUMFILES, 0, MAKELPARAM(0, a));
- SendMessage(hDialog, WM_NEXTFILE, b,(LPARAM)fmt);
- }
- va_end(marker);
-
- return result;
+ case ZLIB_ERROR:
+ MessageBox(GetFocus(), Buffer, "Error",
+ MB_OK | MB_ICONWARNING);
+ break;
+
+ case SYSTEM_ERROR:
+ SystemError(GetLastError(), Buffer);
+ break;
+
+ case NUM_FILES:
+ a = va_arg(marker, int);
+ b = va_arg(marker, int);
+ SendMessage(hDialog, WM_NUMFILES, 0, MAKELPARAM(0, a));
+ SendMessage(hDialog, WM_NEXTFILE, b,(LPARAM)fmt);
+ }
+ va_end(marker);
+
+ return result;
}
static char *MapExistingFile(char *pathname, DWORD *psize)
{
- HANDLE hFile, hFileMapping;
- DWORD nSizeLow, nSizeHigh;
- char *data;
-
- hFile = CreateFile(pathname,
- GENERIC_READ, FILE_SHARE_READ, NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- return NULL;
- nSizeLow = GetFileSize(hFile, &nSizeHigh);
- hFileMapping = CreateFileMapping(hFile,
- NULL, PAGE_READONLY, 0, 0, NULL);
- CloseHandle(hFile);
-
- if (hFileMapping == INVALID_HANDLE_VALUE)
- return NULL;
-
- data = MapViewOfFile(hFileMapping,
- FILE_MAP_READ, 0, 0, 0);
-
- CloseHandle(hFileMapping);
- *psize = nSizeLow;
- return data;
+ HANDLE hFile, hFileMapping;
+ DWORD nSizeLow, nSizeHigh;
+ char *data;
+
+ hFile = CreateFile(pathname,
+ GENERIC_READ, FILE_SHARE_READ, NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ if (hFile == INVALID_HANDLE_VALUE)
+ return NULL;
+ nSizeLow = GetFileSize(hFile, &nSizeHigh);
+ hFileMapping = CreateFileMapping(hFile,
+ NULL, PAGE_READONLY, 0, 0, NULL);
+ CloseHandle(hFile);
+
+ if (hFileMapping == INVALID_HANDLE_VALUE)
+ return NULL;
+
+ data = MapViewOfFile(hFileMapping,
+ FILE_MAP_READ, 0, 0, 0);
+
+ CloseHandle(hFileMapping);
+ *psize = nSizeLow;
+ return data;
}
static void create_bitmap(HWND hwnd)
{
- BITMAPFILEHEADER *bfh;
- BITMAPINFO *bi;
- HDC hdc;
+ BITMAPFILEHEADER *bfh;
+ BITMAPINFO *bi;
+ HDC hdc;
- if (!bitmap_bytes)
- return;
+ if (!bitmap_bytes)
+ return;
- if (hBitmap)
- return;
+ if (hBitmap)
+ return;
- hdc = GetDC(hwnd);
+ hdc = GetDC(hwnd);
- bfh = (BITMAPFILEHEADER *)bitmap_bytes;
- bi = (BITMAPINFO *)(bitmap_bytes + sizeof(BITMAPFILEHEADER));
+ bfh = (BITMAPFILEHEADER *)bitmap_bytes;
+ bi = (BITMAPINFO *)(bitmap_bytes + sizeof(BITMAPFILEHEADER));
- hBitmap = CreateDIBitmap(hdc,
- &bi->bmiHeader,
- CBM_INIT,
- bitmap_bytes + bfh->bfOffBits,
- bi,
- DIB_RGB_COLORS);
- ReleaseDC(hwnd, hdc);
+ hBitmap = CreateDIBitmap(hdc,
+ &bi->bmiHeader,
+ CBM_INIT,
+ bitmap_bytes + bfh->bfOffBits,
+ bi,
+ DIB_RGB_COLORS);
+ ReleaseDC(hwnd, hdc);
}
/* Extract everything we need to begin the installation. Currently this
is the INI filename with install data, and the raw pre-install script
*/
static BOOL ExtractInstallData(char *data, DWORD size, int *pexe_size,
- char **out_ini_file, char **out_preinstall_script)
+ char **out_ini_file, char **out_preinstall_script)
{
- /* read the end of central directory record */
- struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof
- (struct eof_cdir)];
-
- int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir -
- pe->ofsCDir;
-
- int ofs = arc_start - sizeof (struct meta_data_hdr);
-
- /* read meta_data info */
- struct meta_data_hdr *pmd = (struct meta_data_hdr *)&data[ofs];
- char *src, *dst;
- char *ini_file;
- char tempdir[MAX_PATH];
-
- /* ensure that if we fail, we don't have garbage out pointers */
- *out_ini_file = *out_preinstall_script = NULL;
-
- if (pe->tag != 0x06054b50) {
- return FALSE;
- }
-
- if (pmd->tag != 0x1234567B) {
- return SystemError(0,
- "Invalid cfgdata magic number (see bdist_wininst.py)");
- }
- if (ofs < 0) {
- return FALSE;
- }
-
- if (pmd->bitmap_size) {
- /* Store pointer to bitmap bytes */
- bitmap_bytes = (char *)pmd - pmd->uncomp_size - pmd->bitmap_size;
- }
-
- *pexe_size = ofs - pmd->uncomp_size - pmd->bitmap_size;
-
- src = ((char *)pmd) - pmd->uncomp_size;
- ini_file = malloc(MAX_PATH); /* will be returned, so do not free it */
- if (!ini_file)
- return FALSE;
- if (!GetTempPath(sizeof(tempdir), tempdir)
- || !GetTempFileName(tempdir, "~du", 0, ini_file)) {
- SystemError(GetLastError(),
- "Could not create temporary file");
- return FALSE;
- }
-
- dst = map_new_file(CREATE_ALWAYS, ini_file, NULL, pmd->uncomp_size,
- 0, 0, NULL/*notify*/);
- if (!dst)
- return FALSE;
- /* Up to the first \0 is the INI file data. */
- strncpy(dst, src, pmd->uncomp_size);
- src += strlen(dst) + 1;
- /* Up to next \0 is the pre-install script */
- *out_preinstall_script = strdup(src);
- *out_ini_file = ini_file;
- UnmapViewOfFile(dst);
- return TRUE;
+ /* read the end of central directory record */
+ struct eof_cdir *pe = (struct eof_cdir *)&data[size - sizeof
+ (struct eof_cdir)];
+
+ int arc_start = size - sizeof (struct eof_cdir) - pe->nBytesCDir -
+ pe->ofsCDir;
+
+ int ofs = arc_start - sizeof (struct meta_data_hdr);
+
+ /* read meta_data info */
+ struct meta_data_hdr *pmd = (struct meta_data_hdr *)&data[ofs];
+ char *src, *dst;
+ char *ini_file;
+ char tempdir[MAX_PATH];
+
+ /* ensure that if we fail, we don't have garbage out pointers */
+ *out_ini_file = *out_preinstall_script = NULL;
+
+ if (pe->tag != 0x06054b50) {
+ return FALSE;
+ }
+
+ if (pmd->tag != 0x1234567B) {
+ return SystemError(0,
+ "Invalid cfgdata magic number (see bdist_wininst.py)");
+ }
+ if (ofs < 0) {
+ return FALSE;
+ }
+
+ if (pmd->bitmap_size) {
+ /* Store pointer to bitmap bytes */
+ bitmap_bytes = (char *)pmd - pmd->uncomp_size - pmd->bitmap_size;
+ }
+
+ *pexe_size = ofs - pmd->uncomp_size - pmd->bitmap_size;
+
+ src = ((char *)pmd) - pmd->uncomp_size;
+ ini_file = malloc(MAX_PATH); /* will be returned, so do not free it */
+ if (!ini_file)
+ return FALSE;
+ if (!GetTempPath(sizeof(tempdir), tempdir)
+ || !GetTempFileName(tempdir, "~du", 0, ini_file)) {
+ SystemError(GetLastError(),
+ "Could not create temporary file");
+ return FALSE;
+ }
+
+ dst = map_new_file(CREATE_ALWAYS, ini_file, NULL, pmd->uncomp_size,
+ 0, 0, NULL/*notify*/);
+ if (!dst)
+ return FALSE;
+ /* Up to the first \0 is the INI file data. */
+ strncpy(dst, src, pmd->uncomp_size);
+ src += strlen(dst) + 1;
+ /* Up to next \0 is the pre-install script */
+ *out_preinstall_script = strdup(src);
+ *out_ini_file = ini_file;
+ UnmapViewOfFile(dst);
+ return TRUE;
}
static void PumpMessages(void)
{
- MSG msg;
- while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
+ MSG msg;
+ while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
}
LRESULT CALLBACK
WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- HDC hdc;
- HFONT hFont;
- int h;
- PAINTSTRUCT ps;
- switch (msg) {
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- h = GetSystemMetrics(SM_CYSCREEN) / 10;
- hFont = CreateFont(h, 0, 0, 0, 700, TRUE,
- 0, 0, 0, 0, 0, 0, 0, "Times Roman");
- hFont = SelectObject(hdc, hFont);
- SetBkMode(hdc, TRANSPARENT);
- TextOut(hdc, 15, 15, title, strlen(title));
- SetTextColor(hdc, RGB(255, 255, 255));
- TextOut(hdc, 10, 10, title, strlen(title));
- DeleteObject(SelectObject(hdc, hFont));
- EndPaint(hwnd, &ps);
- return 0;
- }
- return DefWindowProc(hwnd, msg, wParam, lParam);
+ HDC hdc;
+ HFONT hFont;
+ int h;
+ PAINTSTRUCT ps;
+ switch (msg) {
+ case WM_PAINT:
+ hdc = BeginPaint(hwnd, &ps);
+ h = GetSystemMetrics(SM_CYSCREEN) / 10;
+ hFont = CreateFont(h, 0, 0, 0, 700, TRUE,
+ 0, 0, 0, 0, 0, 0, 0, "Times Roman");
+ hFont = SelectObject(hdc, hFont);
+ SetBkMode(hdc, TRANSPARENT);
+ TextOut(hdc, 15, 15, title, strlen(title));
+ SetTextColor(hdc, RGB(255, 255, 255));
+ TextOut(hdc, 10, 10, title, strlen(title));
+ DeleteObject(SelectObject(hdc, hFont));
+ EndPaint(hwnd, &ps);
+ return 0;
+ }
+ return DefWindowProc(hwnd, msg, wParam, lParam);
}
static HWND CreateBackground(char *title)
{
- WNDCLASS wc;
- HWND hwnd;
- char buffer[4096];
-
- wc.style = CS_VREDRAW | CS_HREDRAW;
- wc.lpfnWndProc = WindowProc;
- wc.cbWndExtra = 0;
- wc.cbClsExtra = 0;
- wc.hInstance = GetModuleHandle(NULL);
- wc.hIcon = NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 128));
- wc.lpszMenuName = NULL;
- wc.lpszClassName = "SetupWindowClass";
-
- if (!RegisterClass(&wc))
- MessageBox(hwndMain,
- "Could not register window class",
- "Setup.exe", MB_OK);
-
- wsprintf(buffer, "Setup %s", title);
- hwnd = CreateWindow("SetupWindowClass",
- buffer,
- 0,
- 0, 0,
- GetSystemMetrics(SM_CXFULLSCREEN),
- GetSystemMetrics(SM_CYFULLSCREEN),
- NULL,
- NULL,
- GetModuleHandle(NULL),
- NULL);
- ShowWindow(hwnd, SW_SHOWMAXIMIZED);
- UpdateWindow(hwnd);
- return hwnd;
+ WNDCLASS wc;
+ HWND hwnd;
+ char buffer[4096];
+
+ wc.style = CS_VREDRAW | CS_HREDRAW;
+ wc.lpfnWndProc = WindowProc;
+ wc.cbWndExtra = 0;
+ wc.cbClsExtra = 0;
+ wc.hInstance = GetModuleHandle(NULL);
+ wc.hIcon = NULL;
+ wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+ wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 128));
+ wc.lpszMenuName = NULL;
+ wc.lpszClassName = "SetupWindowClass";
+
+ if (!RegisterClass(&wc))
+ MessageBox(hwndMain,
+ "Could not register window class",
+ "Setup.exe", MB_OK);
+
+ wsprintf(buffer, "Setup %s", title);
+ hwnd = CreateWindow("SetupWindowClass",
+ buffer,
+ 0,
+ 0, 0,
+ GetSystemMetrics(SM_CXFULLSCREEN),
+ GetSystemMetrics(SM_CYFULLSCREEN),
+ NULL,
+ NULL,
+ GetModuleHandle(NULL),
+ NULL);
+ ShowWindow(hwnd, SW_SHOWMAXIMIZED);
+ UpdateWindow(hwnd);
+ return hwnd;
}
/*
@@ -1202,16 +1202,16 @@ static HWND CreateBackground(char *title)
*/
static void CenterWindow(HWND hwnd)
{
- RECT rc;
- int w, h;
-
- GetWindowRect(hwnd, &rc);
- w = GetSystemMetrics(SM_CXSCREEN);
- h = GetSystemMetrics(SM_CYSCREEN);
- MoveWindow(hwnd,
- (w - (rc.right-rc.left))/2,
- (h - (rc.bottom-rc.top))/2,
- rc.right-rc.left, rc.bottom-rc.top, FALSE);
+ RECT rc;
+ int w, h;
+
+ GetWindowRect(hwnd, &rc);
+ w = GetSystemMetrics(SM_CXSCREEN);
+ h = GetSystemMetrics(SM_CYSCREEN);
+ MoveWindow(hwnd,
+ (w - (rc.right-rc.left))/2,
+ (h - (rc.bottom-rc.top))/2,
+ rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
#include <prsht.h>
@@ -1219,45 +1219,45 @@ static void CenterWindow(HWND hwnd)
BOOL CALLBACK
IntroDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- LPNMHDR lpnm;
- char Buffer[4096];
-
- switch (msg) {
- case WM_INITDIALOG:
- create_bitmap(hwnd);
- if(hBitmap)
- SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
- IMAGE_BITMAP, (LPARAM)hBitmap);
- CenterWindow(GetParent(hwnd));
- wsprintf(Buffer,
- "This Wizard will install %s on your computer. "
- "Click Next to continue "
- "or Cancel to exit the Setup Wizard.",
- meta_name);
- SetDlgItemText(hwnd, IDC_TITLE, Buffer);
- SetDlgItemText(hwnd, IDC_INTRO_TEXT, info);
- SetDlgItemText(hwnd, IDC_BUILD_INFO, build_info);
- return FALSE;
-
- case WM_NOTIFY:
- lpnm = (LPNMHDR) lParam;
-
- switch (lpnm->code) {
- case PSN_SETACTIVE:
- PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_NEXT);
- break;
-
- case PSN_WIZNEXT:
- break;
-
- case PSN_RESET:
- break;
-
- default:
- break;
- }
- }
- return FALSE;
+ LPNMHDR lpnm;
+ char Buffer[4096];
+
+ switch (msg) {
+ case WM_INITDIALOG:
+ create_bitmap(hwnd);
+ if(hBitmap)
+ SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
+ IMAGE_BITMAP, (LPARAM)hBitmap);
+ CenterWindow(GetParent(hwnd));
+ wsprintf(Buffer,
+ "This Wizard will install %s on your computer. "
+ "Click Next to continue "
+ "or Cancel to exit the Setup Wizard.",
+ meta_name);
+ SetDlgItemText(hwnd, IDC_TITLE, Buffer);
+ SetDlgItemText(hwnd, IDC_INTRO_TEXT, info);
+ SetDlgItemText(hwnd, IDC_BUILD_INFO, build_info);
+ return FALSE;
+
+ case WM_NOTIFY:
+ lpnm = (LPNMHDR) lParam;
+
+ switch (lpnm->code) {
+ case PSN_SETACTIVE:
+ PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_NEXT);
+ break;
+
+ case PSN_WIZNEXT:
+ break;
+
+ case PSN_RESET:
+ break;
+
+ default:
+ break;
+ }
+ }
+ return FALSE;
}
#ifdef USE_OTHER_PYTHON_VERSIONS
@@ -1269,133 +1269,133 @@ int bound_image_major;
int bound_image_minor;
static BOOL __stdcall StatusRoutine(IMAGEHLP_STATUS_REASON reason,
- PSTR ImageName,
- PSTR DllName,
- ULONG Va,
- ULONG Parameter)
+ PSTR ImageName,
+ PSTR DllName,
+ ULONG Va,
+ ULONG Parameter)
{
- char fname[_MAX_PATH];
- int int_version;
-
- switch(reason) {
- case BindOutOfMemory:
- case BindRvaToVaFailed:
- case BindNoRoomInImage:
- case BindImportProcedureFailed:
- break;
-
- case BindImportProcedure:
- case BindForwarder:
- case BindForwarderNOT:
- case BindImageModified:
- case BindExpandFileHeaders:
- case BindImageComplete:
- case BindSymbolsNotUpdated:
- case BindMismatchedSymbols:
- case BindImportModuleFailed:
- break;
-
- case BindImportModule:
- if (1 == sscanf(DllName, "python%d", &int_version)) {
- SearchPath(NULL, DllName, NULL, sizeof(fname),
- fname, NULL);
- strcpy(bound_image_dll, fname);
- bound_image_major = int_version / 10;
- bound_image_minor = int_version % 10;
- OutputDebugString("BOUND ");
- OutputDebugString(fname);
- OutputDebugString("\n");
- }
- break;
- }
- return TRUE;
+ char fname[_MAX_PATH];
+ int int_version;
+
+ switch(reason) {
+ case BindOutOfMemory:
+ case BindRvaToVaFailed:
+ case BindNoRoomInImage:
+ case BindImportProcedureFailed:
+ break;
+
+ case BindImportProcedure:
+ case BindForwarder:
+ case BindForwarderNOT:
+ case BindImageModified:
+ case BindExpandFileHeaders:
+ case BindImageComplete:
+ case BindSymbolsNotUpdated:
+ case BindMismatchedSymbols:
+ case BindImportModuleFailed:
+ break;
+
+ case BindImportModule:
+ if (1 == sscanf(DllName, "python%d", &int_version)) {
+ SearchPath(NULL, DllName, NULL, sizeof(fname),
+ fname, NULL);
+ strcpy(bound_image_dll, fname);
+ bound_image_major = int_version / 10;
+ bound_image_minor = int_version % 10;
+ OutputDebugString("BOUND ");
+ OutputDebugString(fname);
+ OutputDebugString("\n");
+ }
+ break;
+ }
+ return TRUE;
}
/*
*/
static LPSTR get_sys_prefix(LPSTR exe, LPSTR dll)
{
- void (__cdecl * Py_Initialize)(void);
- void (__cdecl * Py_SetProgramName)(char *);
- void (__cdecl * Py_Finalize)(void);
- void* (__cdecl * PySys_GetObject)(char *);
- void (__cdecl * PySys_SetArgv)(int, char **);
- char* (__cdecl * Py_GetPrefix)(void);
- char* (__cdecl * Py_GetPath)(void);
- HINSTANCE hPython;
- LPSTR prefix = NULL;
- int (__cdecl * PyRun_SimpleString)(char *);
-
- {
- char Buffer[256];
- wsprintf(Buffer, "PYTHONHOME=%s", exe);
- *strrchr(Buffer, '\\') = '\0';
-// MessageBox(GetFocus(), Buffer, "PYTHONHOME", MB_OK);
- _putenv(Buffer);
- _putenv("PYTHONPATH=");
- }
-
- hPython = LoadLibrary(dll);
- if (!hPython)
- return NULL;
- Py_Initialize = (void (*)(void))GetProcAddress
- (hPython,"Py_Initialize");
-
- PySys_SetArgv = (void (*)(int, char **))GetProcAddress
- (hPython,"PySys_SetArgv");
-
- PyRun_SimpleString = (int (*)(char *))GetProcAddress
- (hPython,"PyRun_SimpleString");
-
- Py_SetProgramName = (void (*)(char *))GetProcAddress
- (hPython,"Py_SetProgramName");
-
- PySys_GetObject = (void* (*)(char *))GetProcAddress
- (hPython,"PySys_GetObject");
-
- Py_GetPrefix = (char * (*)(void))GetProcAddress
- (hPython,"Py_GetPrefix");
-
- Py_GetPath = (char * (*)(void))GetProcAddress
- (hPython,"Py_GetPath");
-
- Py_Finalize = (void (*)(void))GetProcAddress(hPython,
- "Py_Finalize");
- Py_SetProgramName(exe);
- Py_Initialize();
- PySys_SetArgv(1, &exe);
-
- MessageBox(GetFocus(), Py_GetPrefix(), "PREFIX", MB_OK);
- MessageBox(GetFocus(), Py_GetPath(), "PATH", MB_OK);
-
- Py_Finalize();
- FreeLibrary(hPython);
-
- return prefix;
+ void (__cdecl * Py_Initialize)(void);
+ void (__cdecl * Py_SetProgramName)(char *);
+ void (__cdecl * Py_Finalize)(void);
+ void* (__cdecl * PySys_GetObject)(char *);
+ void (__cdecl * PySys_SetArgv)(int, char **);
+ char* (__cdecl * Py_GetPrefix)(void);
+ char* (__cdecl * Py_GetPath)(void);
+ HINSTANCE hPython;
+ LPSTR prefix = NULL;
+ int (__cdecl * PyRun_SimpleString)(char *);
+
+ {
+ char Buffer[256];
+ wsprintf(Buffer, "PYTHONHOME=%s", exe);
+ *strrchr(Buffer, '\\') = '\0';
+// MessageBox(GetFocus(), Buffer, "PYTHONHOME", MB_OK);
+ _putenv(Buffer);
+ _putenv("PYTHONPATH=");
+ }
+
+ hPython = LoadLibrary(dll);
+ if (!hPython)
+ return NULL;
+ Py_Initialize = (void (*)(void))GetProcAddress
+ (hPython,"Py_Initialize");
+
+ PySys_SetArgv = (void (*)(int, char **))GetProcAddress
+ (hPython,"PySys_SetArgv");
+
+ PyRun_SimpleString = (int (*)(char *))GetProcAddress
+ (hPython,"PyRun_SimpleString");
+
+ Py_SetProgramName = (void (*)(char *))GetProcAddress
+ (hPython,"Py_SetProgramName");
+
+ PySys_GetObject = (void* (*)(char *))GetProcAddress
+ (hPython,"PySys_GetObject");
+
+ Py_GetPrefix = (char * (*)(void))GetProcAddress
+ (hPython,"Py_GetPrefix");
+
+ Py_GetPath = (char * (*)(void))GetProcAddress
+ (hPython,"Py_GetPath");
+
+ Py_Finalize = (void (*)(void))GetProcAddress(hPython,
+ "Py_Finalize");
+ Py_SetProgramName(exe);
+ Py_Initialize();
+ PySys_SetArgv(1, &exe);
+
+ MessageBox(GetFocus(), Py_GetPrefix(), "PREFIX", MB_OK);
+ MessageBox(GetFocus(), Py_GetPath(), "PATH", MB_OK);
+
+ Py_Finalize();
+ FreeLibrary(hPython);
+
+ return prefix;
}
static BOOL
CheckPythonExe(LPSTR pathname, LPSTR version, int *pmajor, int *pminor)
{
- bound_image_dll[0] = '\0';
- if (!BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES,
- pathname,
- NULL,
- NULL,
- StatusRoutine))
- return SystemError(0, "Could not bind image");
- if (bound_image_dll[0] == '\0')
- return SystemError(0, "Does not seem to be a python executable");
- *pmajor = bound_image_major;
- *pminor = bound_image_minor;
- if (version && *version) {
- char core_version[12];
- wsprintf(core_version, "%d.%d", bound_image_major, bound_image_minor);
- if (strcmp(version, core_version))
- return SystemError(0, "Wrong Python version");
- }
- get_sys_prefix(pathname, bound_image_dll);
- return TRUE;
+ bound_image_dll[0] = '\0';
+ if (!BindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES,
+ pathname,
+ NULL,
+ NULL,
+ StatusRoutine))
+ return SystemError(0, "Could not bind image");
+ if (bound_image_dll[0] == '\0')
+ return SystemError(0, "Does not seem to be a python executable");
+ *pmajor = bound_image_major;
+ *pminor = bound_image_minor;
+ if (version && *version) {
+ char core_version[12];
+ wsprintf(core_version, "%d.%d", bound_image_major, bound_image_minor);
+ if (strcmp(version, core_version))
+ return SystemError(0, "Wrong Python version");
+ }
+ get_sys_prefix(pathname, bound_image_dll);
+ return TRUE;
}
/*
@@ -1404,48 +1404,48 @@ CheckPythonExe(LPSTR pathname, LPSTR version, int *pmajor, int *pminor)
*/
static BOOL GetOtherPythonVersion(HWND hwnd, LPSTR version)
{
- char vers_name[_MAX_PATH + 80];
- DWORD itemindex;
- OPENFILENAME of;
- char pathname[_MAX_PATH];
- DWORD result;
-
- strcpy(pathname, "python.exe");
-
- memset(&of, 0, sizeof(of));
- of.lStructSize = sizeof(OPENFILENAME);
- of.hwndOwner = GetParent(hwnd);
- of.hInstance = NULL;
- of.lpstrFilter = "python.exe\0python.exe\0";
- of.lpstrCustomFilter = NULL;
- of.nMaxCustFilter = 0;
- of.nFilterIndex = 1;
- of.lpstrFile = pathname;
- of.nMaxFile = sizeof(pathname);
- of.lpstrFileTitle = NULL;
- of.nMaxFileTitle = 0;
- of.lpstrInitialDir = NULL;
- of.lpstrTitle = "Python executable";
- of.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
- of.lpstrDefExt = "exe";
-
- result = GetOpenFileName(&of);
- if (result) {
- int major, minor;
- if (!CheckPythonExe(pathname, version, &major, &minor)) {
- return FALSE;
- }
- *strrchr(pathname, '\\') = '\0';
- wsprintf(vers_name, "Python Version %d.%d in %s",
- major, minor, pathname);
- itemindex = SendMessage(hwnd, LB_INSERTSTRING, -1,
- (LPARAM)(LPSTR)vers_name);
- SendMessage(hwnd, LB_SETCURSEL, itemindex, 0);
- SendMessage(hwnd, LB_SETITEMDATA, itemindex,
- (LPARAM)(LPSTR)strdup(pathname));
- return TRUE;
- }
- return FALSE;
+ char vers_name[_MAX_PATH + 80];
+ DWORD itemindex;
+ OPENFILENAME of;
+ char pathname[_MAX_PATH];
+ DWORD result;
+
+ strcpy(pathname, "python.exe");
+
+ memset(&of, 0, sizeof(of));
+ of.lStructSize = sizeof(OPENFILENAME);
+ of.hwndOwner = GetParent(hwnd);
+ of.hInstance = NULL;
+ of.lpstrFilter = "python.exe\0python.exe\0";
+ of.lpstrCustomFilter = NULL;
+ of.nMaxCustFilter = 0;
+ of.nFilterIndex = 1;
+ of.lpstrFile = pathname;
+ of.nMaxFile = sizeof(pathname);
+ of.lpstrFileTitle = NULL;
+ of.nMaxFileTitle = 0;
+ of.lpstrInitialDir = NULL;
+ of.lpstrTitle = "Python executable";
+ of.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
+ of.lpstrDefExt = "exe";
+
+ result = GetOpenFileName(&of);
+ if (result) {
+ int major, minor;
+ if (!CheckPythonExe(pathname, version, &major, &minor)) {
+ return FALSE;
+ }
+ *strrchr(pathname, '\\') = '\0';
+ wsprintf(vers_name, "Python Version %d.%d in %s",
+ major, minor, pathname);
+ itemindex = SendMessage(hwnd, LB_INSERTSTRING, -1,
+ (LPARAM)(LPSTR)vers_name);
+ SendMessage(hwnd, LB_SETCURSEL, itemindex, 0);
+ SendMessage(hwnd, LB_SETITEMDATA, itemindex,
+ (LPARAM)(LPSTR)strdup(pathname));
+ return TRUE;
+ }
+ return FALSE;
}
#endif /* USE_OTHER_PYTHON_VERSIONS */
@@ -1462,71 +1462,71 @@ typedef struct _InstalledVersionInfo {
*/
static BOOL GetPythonVersions(HWND hwnd, HKEY hkRoot, LPSTR version)
{
- DWORD index = 0;
- char core_version[80];
- HKEY hKey;
- BOOL result = TRUE;
- DWORD bufsize;
-
- if (ERROR_SUCCESS != RegOpenKeyEx(hkRoot,
- "Software\\Python\\PythonCore",
- 0, KEY_READ, &hKey))
- return FALSE;
- bufsize = sizeof(core_version);
- while (ERROR_SUCCESS == RegEnumKeyEx(hKey, index,
- core_version, &bufsize, NULL,
- NULL, NULL, NULL)) {
- char subkey_name[80], vers_name[80];
- int itemindex;
- DWORD value_size;
- HKEY hk;
-
- bufsize = sizeof(core_version);
- ++index;
- if (version && *version && strcmp(version, core_version))
- continue;
-
- wsprintf(vers_name, "Python Version %s (found in registry)",
- core_version);
- wsprintf(subkey_name,
- "Software\\Python\\PythonCore\\%s\\InstallPath",
- core_version);
- if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot, subkey_name, 0, KEY_READ, &hk)) {
- InstalledVersionInfo *ivi =
- (InstalledVersionInfo *)malloc(sizeof(InstalledVersionInfo));
- value_size = sizeof(ivi->prefix);
- if (ivi &&
- ERROR_SUCCESS == RegQueryValueEx(hk, NULL, NULL, NULL,
- ivi->prefix, &value_size)) {
- itemindex = SendMessage(hwnd, LB_ADDSTRING, 0,
- (LPARAM)(LPSTR)vers_name);
- ivi->hkey = hkRoot;
- SendMessage(hwnd, LB_SETITEMDATA, itemindex,
- (LPARAM)(LPSTR)ivi);
- }
- RegCloseKey(hk);
- }
- }
- RegCloseKey(hKey);
- return result;
+ DWORD index = 0;
+ char core_version[80];
+ HKEY hKey;
+ BOOL result = TRUE;
+ DWORD bufsize;
+
+ if (ERROR_SUCCESS != RegOpenKeyEx(hkRoot,
+ "Software\\Python\\PythonCore",
+ 0, KEY_READ, &hKey))
+ return FALSE;
+ bufsize = sizeof(core_version);
+ while (ERROR_SUCCESS == RegEnumKeyEx(hKey, index,
+ core_version, &bufsize, NULL,
+ NULL, NULL, NULL)) {
+ char subkey_name[80], vers_name[80];
+ int itemindex;
+ DWORD value_size;
+ HKEY hk;
+
+ bufsize = sizeof(core_version);
+ ++index;
+ if (version && *version && strcmp(version, core_version))
+ continue;
+
+ wsprintf(vers_name, "Python Version %s (found in registry)",
+ core_version);
+ wsprintf(subkey_name,
+ "Software\\Python\\PythonCore\\%s\\InstallPath",
+ core_version);
+ if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot, subkey_name, 0, KEY_READ, &hk)) {
+ InstalledVersionInfo *ivi =
+ (InstalledVersionInfo *)malloc(sizeof(InstalledVersionInfo));
+ value_size = sizeof(ivi->prefix);
+ if (ivi &&
+ ERROR_SUCCESS == RegQueryValueEx(hk, NULL, NULL, NULL,
+ ivi->prefix, &value_size)) {
+ itemindex = SendMessage(hwnd, LB_ADDSTRING, 0,
+ (LPARAM)(LPSTR)vers_name);
+ ivi->hkey = hkRoot;
+ SendMessage(hwnd, LB_SETITEMDATA, itemindex,
+ (LPARAM)(LPSTR)ivi);
+ }
+ RegCloseKey(hk);
+ }
+ }
+ RegCloseKey(hKey);
+ return result;
}
/* Determine if the current user can write to HKEY_LOCAL_MACHINE */
BOOL HasLocalMachinePrivs()
{
- HKEY hKey;
- DWORD result;
- static char KeyName[] =
- "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
-
- result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- KeyName,
- 0,
- KEY_CREATE_SUB_KEY,
- &hKey);
- if (result==0)
- RegCloseKey(hKey);
- return result==0;
+ HKEY hKey;
+ DWORD result;
+ static char KeyName[] =
+ "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
+
+ result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+ KeyName,
+ 0,
+ KEY_CREATE_SUB_KEY,
+ &hKey);
+ if (result==0)
+ RegCloseKey(hKey);
+ return result==0;
}
// Check the root registry key to use - either HKLM or HKCU.
@@ -1539,86 +1539,86 @@ BOOL HasLocalMachinePrivs()
// We assume hkey_root is already set to where Python itself is installed.
void CheckRootKey(HWND hwnd)
{
- if (hkey_root==HKEY_CURRENT_USER) {
- ; // as above, always install ourself in HKCU too.
- } else if (hkey_root==HKEY_LOCAL_MACHINE) {
- // Python in HKLM, but we may or may not have permissions there.
- // Open the uninstall key with 'create' permissions - if this fails,
- // we don't have permission.
- if (!HasLocalMachinePrivs())
- hkey_root = HKEY_CURRENT_USER;
- } else {
- MessageBox(hwnd, "Don't know Python's installation type",
- "Strange", MB_OK | MB_ICONSTOP);
- /* Default to wherever they can, but preferring HKLM */
- hkey_root = HasLocalMachinePrivs() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
- }
+ if (hkey_root==HKEY_CURRENT_USER) {
+ ; // as above, always install ourself in HKCU too.
+ } else if (hkey_root==HKEY_LOCAL_MACHINE) {
+ // Python in HKLM, but we may or may not have permissions there.
+ // Open the uninstall key with 'create' permissions - if this fails,
+ // we don't have permission.
+ if (!HasLocalMachinePrivs())
+ hkey_root = HKEY_CURRENT_USER;
+ } else {
+ MessageBox(hwnd, "Don't know Python's installation type",
+ "Strange", MB_OK | MB_ICONSTOP);
+ /* Default to wherever they can, but preferring HKLM */
+ hkey_root = HasLocalMachinePrivs() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
+ }
}
/* Return the installation scheme depending on Python version number */
SCHEME *GetScheme(int major, int minor)
{
- if (major > 2)
- return new_scheme;
- else if((major == 2) && (minor >= 2))
- return new_scheme;
- return old_scheme;
+ if (major > 2)
+ return new_scheme;
+ else if((major == 2) && (minor >= 2))
+ return new_scheme;
+ return old_scheme;
}
BOOL CALLBACK
SelectPythonDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- LPNMHDR lpnm;
-
- switch (msg) {
- case WM_INITDIALOG:
- if (hBitmap)
- SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
- IMAGE_BITMAP, (LPARAM)hBitmap);
- GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST),
- HKEY_LOCAL_MACHINE, target_version);
- GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST),
- HKEY_CURRENT_USER, target_version);
- { /* select the last entry which is the highest python
- version found */
- int count;
- count = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
- LB_GETCOUNT, 0, 0);
- if (count && count != LB_ERR)
- SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, LB_SETCURSEL,
- count-1, 0);
-
- /* If a specific Python version is required,
- * display a prominent notice showing this fact.
- */
- if (target_version && target_version[0]) {
- char buffer[4096];
- wsprintf(buffer,
- "Python %s is required for this package. "
- "Select installation to use:",
- target_version);
- SetDlgItemText(hwnd, IDC_TITLE, buffer);
- }
-
- if (count == 0) {
- char Buffer[4096];
- char *msg;
- if (target_version && target_version[0]) {
- wsprintf(Buffer,
- "Python version %s required, which was not found"
- " in the registry.", target_version);
- msg = Buffer;
- } else
- msg = "No Python installation found in the registry.";
- MessageBox(hwnd, msg, "Cannot install",
- MB_OK | MB_ICONSTOP);
- }
- }
- goto UpdateInstallDir;
- break;
-
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
+ LPNMHDR lpnm;
+
+ switch (msg) {
+ case WM_INITDIALOG:
+ if (hBitmap)
+ SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
+ IMAGE_BITMAP, (LPARAM)hBitmap);
+ GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST),
+ HKEY_LOCAL_MACHINE, target_version);
+ GetPythonVersions(GetDlgItem(hwnd, IDC_VERSIONS_LIST),
+ HKEY_CURRENT_USER, target_version);
+ { /* select the last entry which is the highest python
+ version found */
+ int count;
+ count = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
+ LB_GETCOUNT, 0, 0);
+ if (count && count != LB_ERR)
+ SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST, LB_SETCURSEL,
+ count-1, 0);
+
+ /* If a specific Python version is required,
+ * display a prominent notice showing this fact.
+ */
+ if (target_version && target_version[0]) {
+ char buffer[4096];
+ wsprintf(buffer,
+ "Python %s is required for this package. "
+ "Select installation to use:",
+ target_version);
+ SetDlgItemText(hwnd, IDC_TITLE, buffer);
+ }
+
+ if (count == 0) {
+ char Buffer[4096];
+ char *msg;
+ if (target_version && target_version[0]) {
+ wsprintf(Buffer,
+ "Python version %s required, which was not found"
+ " in the registry.", target_version);
+ msg = Buffer;
+ } else
+ msg = "No Python installation found in the registry.";
+ MessageBox(hwnd, msg, "Cannot install",
+ MB_OK | MB_ICONSTOP);
+ }
+ }
+ goto UpdateInstallDir;
+ break;
+
+ case WM_COMMAND:
+ switch (LOWORD(wParam)) {
/*
case IDC_OTHERPYTHON:
if (GetOtherPythonVersion(GetDlgItem(hwnd, IDC_VERSIONS_LIST),
@@ -1626,307 +1626,307 @@ SelectPythonDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
goto UpdateInstallDir;
break;
*/
- case IDC_VERSIONS_LIST:
- switch (HIWORD(wParam)) {
- int id;
- case LBN_SELCHANGE:
- UpdateInstallDir:
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK | PSWIZB_NEXT);
- id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
- LB_GETCURSEL, 0, 0);
- if (id == LB_ERR) {
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK);
- SetDlgItemText(hwnd, IDC_PATH, "");
- SetDlgItemText(hwnd, IDC_INSTALL_PATH, "");
- strcpy(python_dir, "");
- strcpy(pythondll, "");
- } else {
- char *pbuf;
- int result;
- InstalledVersionInfo *ivi;
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK | PSWIZB_NEXT);
- /* Get the python directory */
- ivi = (InstalledVersionInfo *)
- SendDlgItemMessage(hwnd,
- IDC_VERSIONS_LIST,
- LB_GETITEMDATA,
- id,
- 0);
- hkey_root = ivi->hkey;
- strcpy(python_dir, ivi->prefix);
- SetDlgItemText(hwnd, IDC_PATH, python_dir);
- /* retrieve the python version and pythondll to use */
- result = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
- LB_GETTEXTLEN, (WPARAM)id, 0);
- pbuf = (char *)malloc(result + 1);
- if (pbuf) {
- /* guess the name of the python-dll */
- SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
- LB_GETTEXT, (WPARAM)id,
- (LPARAM)pbuf);
- result = sscanf(pbuf, "Python Version %d.%d",
- &py_major, &py_minor);
- if (result == 2) {
+ case IDC_VERSIONS_LIST:
+ switch (HIWORD(wParam)) {
+ int id;
+ case LBN_SELCHANGE:
+ UpdateInstallDir:
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK | PSWIZB_NEXT);
+ id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
+ LB_GETCURSEL, 0, 0);
+ if (id == LB_ERR) {
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK);
+ SetDlgItemText(hwnd, IDC_PATH, "");
+ SetDlgItemText(hwnd, IDC_INSTALL_PATH, "");
+ strcpy(python_dir, "");
+ strcpy(pythondll, "");
+ } else {
+ char *pbuf;
+ int result;
+ InstalledVersionInfo *ivi;
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK | PSWIZB_NEXT);
+ /* Get the python directory */
+ ivi = (InstalledVersionInfo *)
+ SendDlgItemMessage(hwnd,
+ IDC_VERSIONS_LIST,
+ LB_GETITEMDATA,
+ id,
+ 0);
+ hkey_root = ivi->hkey;
+ strcpy(python_dir, ivi->prefix);
+ SetDlgItemText(hwnd, IDC_PATH, python_dir);
+ /* retrieve the python version and pythondll to use */
+ result = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
+ LB_GETTEXTLEN, (WPARAM)id, 0);
+ pbuf = (char *)malloc(result + 1);
+ if (pbuf) {
+ /* guess the name of the python-dll */
+ SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
+ LB_GETTEXT, (WPARAM)id,
+ (LPARAM)pbuf);
+ result = sscanf(pbuf, "Python Version %d.%d",
+ &py_major, &py_minor);
+ if (result == 2) {
#ifdef _DEBUG
- wsprintf(pythondll, "python%d%d_d.dll",
- py_major, py_minor);
+ wsprintf(pythondll, "python%d%d_d.dll",
+ py_major, py_minor);
#else
- wsprintf(pythondll, "python%d%d.dll",
- py_major, py_minor);
+ wsprintf(pythondll, "python%d%d.dll",
+ py_major, py_minor);
#endif
- }
- free(pbuf);
- } else
- strcpy(pythondll, "");
- /* retrieve the scheme for this version */
- {
- char install_path[_MAX_PATH];
- SCHEME *scheme = GetScheme(py_major, py_minor);
- strcpy(install_path, python_dir);
- if (install_path[strlen(install_path)-1] != '\\')
- strcat(install_path, "\\");
- strcat(install_path, scheme[0].prefix);
- SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
- }
- }
- }
- break;
- }
- return 0;
-
- case WM_NOTIFY:
- lpnm = (LPNMHDR) lParam;
-
- switch (lpnm->code) {
- int id;
- case PSN_SETACTIVE:
- id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
- LB_GETCURSEL, 0, 0);
- if (id == LB_ERR)
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK);
- else
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK | PSWIZB_NEXT);
- break;
-
- case PSN_WIZNEXT:
- break;
-
- case PSN_WIZFINISH:
- break;
-
- case PSN_RESET:
- break;
-
- default:
- break;
- }
- }
- return 0;
+ }
+ free(pbuf);
+ } else
+ strcpy(pythondll, "");
+ /* retrieve the scheme for this version */
+ {
+ char install_path[_MAX_PATH];
+ SCHEME *scheme = GetScheme(py_major, py_minor);
+ strcpy(install_path, python_dir);
+ if (install_path[strlen(install_path)-1] != '\\')
+ strcat(install_path, "\\");
+ strcat(install_path, scheme[0].prefix);
+ SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
+ }
+ }
+ }
+ break;
+ }
+ return 0;
+
+ case WM_NOTIFY:
+ lpnm = (LPNMHDR) lParam;
+
+ switch (lpnm->code) {
+ int id;
+ case PSN_SETACTIVE:
+ id = SendDlgItemMessage(hwnd, IDC_VERSIONS_LIST,
+ LB_GETCURSEL, 0, 0);
+ if (id == LB_ERR)
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK);
+ else
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK | PSWIZB_NEXT);
+ break;
+
+ case PSN_WIZNEXT:
+ break;
+
+ case PSN_WIZFINISH:
+ break;
+
+ case PSN_RESET:
+ break;
+
+ default:
+ break;
+ }
+ }
+ return 0;
}
static BOOL OpenLogfile(char *dir)
{
- char buffer[_MAX_PATH+1];
- time_t ltime;
- struct tm *now;
- long result;
- HKEY hKey, hSubkey;
- char subkey_name[256];
- static char KeyName[] =
- "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
- const char *root_name = (hkey_root==HKEY_LOCAL_MACHINE ?
- "HKEY_LOCAL_MACHINE" : "HKEY_CURRENT_USER");
- DWORD disposition;
-
- /* Use Create, as the Uninstall subkey may not exist under HKCU.
- Use CreateKeyEx, so we can specify a SAM specifying write access
- */
- result = RegCreateKeyEx(hkey_root,
- KeyName,
- 0, /* reserved */
- NULL, /* class */
- 0, /* options */
- KEY_CREATE_SUB_KEY, /* sam */
- NULL, /* security */
- &hKey, /* result key */
- NULL); /* disposition */
- if (result != ERROR_SUCCESS) {
- if (result == ERROR_ACCESS_DENIED) {
- /* This should no longer be able to happen - we have already
- checked if they have permissions in HKLM, and all users
- should have write access to HKCU.
- */
- MessageBox(GetFocus(),
- "You do not seem to have sufficient access rights\n"
- "on this machine to install this software",
- NULL,
- MB_OK | MB_ICONSTOP);
- return FALSE;
- } else {
- MessageBox(GetFocus(), KeyName, "Could not open key", MB_OK);
- }
- }
-
- sprintf(buffer, "%s\\%s-wininst.log", dir, meta_name);
- logfile = fopen(buffer, "a");
- time(&ltime);
- now = localtime(&ltime);
- strftime(buffer, sizeof(buffer),
- "*** Installation started %Y/%m/%d %H:%M ***\n",
- localtime(&ltime));
- fprintf(logfile, buffer);
- fprintf(logfile, "Source: %s\n", modulename);
-
- /* Root key must be first entry processed by uninstaller. */
- fprintf(logfile, "999 Root Key: %s\n", root_name);
-
- sprintf(subkey_name, "%s-py%d.%d", meta_name, py_major, py_minor);
-
- result = RegCreateKeyEx(hKey, subkey_name,
- 0, NULL, 0,
- KEY_WRITE,
- NULL,
- &hSubkey,
- &disposition);
-
- if (result != ERROR_SUCCESS)
- MessageBox(GetFocus(), subkey_name, "Could not create key", MB_OK);
-
- RegCloseKey(hKey);
-
- if (disposition == REG_CREATED_NEW_KEY)
- fprintf(logfile, "020 Reg DB Key: [%s]%s\n", KeyName, subkey_name);
-
- sprintf(buffer, "Python %d.%d %s", py_major, py_minor, title);
-
- result = RegSetValueEx(hSubkey, "DisplayName",
- 0,
- REG_SZ,
- buffer,
- strlen(buffer)+1);
-
- if (result != ERROR_SUCCESS)
- MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
-
- fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
- KeyName, subkey_name, "DisplayName", buffer);
-
- {
- FILE *fp;
- sprintf(buffer, "%s\\Remove%s.exe", dir, meta_name);
- fp = fopen(buffer, "wb");
- fwrite(arc_data, exe_size, 1, fp);
- fclose(fp);
-
- sprintf(buffer, "\"%s\\Remove%s.exe\" -u \"%s\\%s-wininst.log\"",
- dir, meta_name, dir, meta_name);
-
- result = RegSetValueEx(hSubkey, "UninstallString",
- 0,
- REG_SZ,
- buffer,
- strlen(buffer)+1);
-
- if (result != ERROR_SUCCESS)
- MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
-
- fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
- KeyName, subkey_name, "UninstallString", buffer);
- }
- return TRUE;
+ char buffer[_MAX_PATH+1];
+ time_t ltime;
+ struct tm *now;
+ long result;
+ HKEY hKey, hSubkey;
+ char subkey_name[256];
+ static char KeyName[] =
+ "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
+ const char *root_name = (hkey_root==HKEY_LOCAL_MACHINE ?
+ "HKEY_LOCAL_MACHINE" : "HKEY_CURRENT_USER");
+ DWORD disposition;
+
+ /* Use Create, as the Uninstall subkey may not exist under HKCU.
+ Use CreateKeyEx, so we can specify a SAM specifying write access
+ */
+ result = RegCreateKeyEx(hkey_root,
+ KeyName,
+ 0, /* reserved */
+ NULL, /* class */
+ 0, /* options */
+ KEY_CREATE_SUB_KEY, /* sam */
+ NULL, /* security */
+ &hKey, /* result key */
+ NULL); /* disposition */
+ if (result != ERROR_SUCCESS) {
+ if (result == ERROR_ACCESS_DENIED) {
+ /* This should no longer be able to happen - we have already
+ checked if they have permissions in HKLM, and all users
+ should have write access to HKCU.
+ */
+ MessageBox(GetFocus(),
+ "You do not seem to have sufficient access rights\n"
+ "on this machine to install this software",
+ NULL,
+ MB_OK | MB_ICONSTOP);
+ return FALSE;
+ } else {
+ MessageBox(GetFocus(), KeyName, "Could not open key", MB_OK);
+ }
+ }
+
+ sprintf(buffer, "%s\\%s-wininst.log", dir, meta_name);
+ logfile = fopen(buffer, "a");
+ time(&ltime);
+ now = localtime(&ltime);
+ strftime(buffer, sizeof(buffer),
+ "*** Installation started %Y/%m/%d %H:%M ***\n",
+ localtime(&ltime));
+ fprintf(logfile, buffer);
+ fprintf(logfile, "Source: %s\n", modulename);
+
+ /* Root key must be first entry processed by uninstaller. */
+ fprintf(logfile, "999 Root Key: %s\n", root_name);
+
+ sprintf(subkey_name, "%s-py%d.%d", meta_name, py_major, py_minor);
+
+ result = RegCreateKeyEx(hKey, subkey_name,
+ 0, NULL, 0,
+ KEY_WRITE,
+ NULL,
+ &hSubkey,
+ &disposition);
+
+ if (result != ERROR_SUCCESS)
+ MessageBox(GetFocus(), subkey_name, "Could not create key", MB_OK);
+
+ RegCloseKey(hKey);
+
+ if (disposition == REG_CREATED_NEW_KEY)
+ fprintf(logfile, "020 Reg DB Key: [%s]%s\n", KeyName, subkey_name);
+
+ sprintf(buffer, "Python %d.%d %s", py_major, py_minor, title);
+
+ result = RegSetValueEx(hSubkey, "DisplayName",
+ 0,
+ REG_SZ,
+ buffer,
+ strlen(buffer)+1);
+
+ if (result != ERROR_SUCCESS)
+ MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
+
+ fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
+ KeyName, subkey_name, "DisplayName", buffer);
+
+ {
+ FILE *fp;
+ sprintf(buffer, "%s\\Remove%s.exe", dir, meta_name);
+ fp = fopen(buffer, "wb");
+ fwrite(arc_data, exe_size, 1, fp);
+ fclose(fp);
+
+ sprintf(buffer, "\"%s\\Remove%s.exe\" -u \"%s\\%s-wininst.log\"",
+ dir, meta_name, dir, meta_name);
+
+ result = RegSetValueEx(hSubkey, "UninstallString",
+ 0,
+ REG_SZ,
+ buffer,
+ strlen(buffer)+1);
+
+ if (result != ERROR_SUCCESS)
+ MessageBox(GetFocus(), buffer, "Could not set key value", MB_OK);
+
+ fprintf(logfile, "040 Reg DB Value: [%s\\%s]%s=%s\n",
+ KeyName, subkey_name, "UninstallString", buffer);
+ }
+ return TRUE;
}
static void CloseLogfile(void)
{
- char buffer[_MAX_PATH+1];
- time_t ltime;
- struct tm *now;
-
- time(&ltime);
- now = localtime(&ltime);
- strftime(buffer, sizeof(buffer),
- "*** Installation finished %Y/%m/%d %H:%M ***\n",
- localtime(&ltime));
- fprintf(logfile, buffer);
- if (logfile)
- fclose(logfile);
+ char buffer[_MAX_PATH+1];
+ time_t ltime;
+ struct tm *now;
+
+ time(&ltime);
+ now = localtime(&ltime);
+ strftime(buffer, sizeof(buffer),
+ "*** Installation finished %Y/%m/%d %H:%M ***\n",
+ localtime(&ltime));
+ fprintf(logfile, buffer);
+ if (logfile)
+ fclose(logfile);
}
BOOL CALLBACK
InstallFilesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- LPNMHDR lpnm;
- char Buffer[4096];
- SCHEME *scheme;
-
- switch (msg) {
- case WM_INITDIALOG:
- if (hBitmap)
- SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
- IMAGE_BITMAP, (LPARAM)hBitmap);
- wsprintf(Buffer,
- "Click Next to begin the installation of %s. "
- "If you want to review or change any of your "
- " installation settings, click Back. "
- "Click Cancel to exit the wizard.",
- meta_name);
- SetDlgItemText(hwnd, IDC_TITLE, Buffer);
- SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
- break;
-
- case WM_NUMFILES:
- SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, lParam);
- PumpMessages();
- return TRUE;
-
- case WM_NEXTFILE:
- SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, wParam,
- 0);
- SetDlgItemText(hwnd, IDC_INFO, (LPSTR)lParam);
- PumpMessages();
- return TRUE;
-
- case WM_NOTIFY:
- lpnm = (LPNMHDR) lParam;
-
- switch (lpnm->code) {
- case PSN_SETACTIVE:
- PropSheet_SetWizButtons(GetParent(hwnd),
- PSWIZB_BACK | PSWIZB_NEXT);
- break;
-
- case PSN_WIZFINISH:
- break;
-
- case PSN_WIZNEXT:
- /* Handle a Next button click here */
- hDialog = hwnd;
- success = TRUE;
-
- /* Disable the buttons while we work. Sending CANCELTOCLOSE has
- the effect of disabling the cancel button, which is a) as we
- do everything synchronously we can't cancel, and b) the next
- step is 'finished', when it is too late to cancel anyway.
- The next step being 'Finished' means we also don't need to
- restore the button state back */
- PropSheet_SetWizButtons(GetParent(hwnd), 0);
- SendMessage(GetParent(hwnd), PSM_CANCELTOCLOSE, 0, 0);
- /* Make sure the installation directory name ends in a */
- /* backslash */
- if (python_dir[strlen(python_dir)-1] != '\\')
- strcat(python_dir, "\\");
- /* Strip the trailing backslash again */
- python_dir[strlen(python_dir)-1] = '\0';
-
- CheckRootKey(hwnd);
-
- if (!OpenLogfile(python_dir))
- break;
+ LPNMHDR lpnm;
+ char Buffer[4096];
+ SCHEME *scheme;
+
+ switch (msg) {
+ case WM_INITDIALOG:
+ if (hBitmap)
+ SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
+ IMAGE_BITMAP, (LPARAM)hBitmap);
+ wsprintf(Buffer,
+ "Click Next to begin the installation of %s. "
+ "If you want to review or change any of your "
+ " installation settings, click Back. "
+ "Click Cancel to exit the wizard.",
+ meta_name);
+ SetDlgItemText(hwnd, IDC_TITLE, Buffer);
+ SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
+ break;
+
+ case WM_NUMFILES:
+ SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, lParam);
+ PumpMessages();
+ return TRUE;
+
+ case WM_NEXTFILE:
+ SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, wParam,
+ 0);
+ SetDlgItemText(hwnd, IDC_INFO, (LPSTR)lParam);
+ PumpMessages();
+ return TRUE;
+
+ case WM_NOTIFY:
+ lpnm = (LPNMHDR) lParam;
+
+ switch (lpnm->code) {
+ case PSN_SETACTIVE:
+ PropSheet_SetWizButtons(GetParent(hwnd),
+ PSWIZB_BACK | PSWIZB_NEXT);
+ break;
+
+ case PSN_WIZFINISH:
+ break;
+
+ case PSN_WIZNEXT:
+ /* Handle a Next button click here */
+ hDialog = hwnd;
+ success = TRUE;
+
+ /* Disable the buttons while we work. Sending CANCELTOCLOSE has
+ the effect of disabling the cancel button, which is a) as we
+ do everything synchronously we can't cancel, and b) the next
+ step is 'finished', when it is too late to cancel anyway.
+ The next step being 'Finished' means we also don't need to
+ restore the button state back */
+ PropSheet_SetWizButtons(GetParent(hwnd), 0);
+ SendMessage(GetParent(hwnd), PSM_CANCELTOCLOSE, 0, 0);
+ /* Make sure the installation directory name ends in a */
+ /* backslash */
+ if (python_dir[strlen(python_dir)-1] != '\\')
+ strcat(python_dir, "\\");
+ /* Strip the trailing backslash again */
+ python_dir[strlen(python_dir)-1] = '\0';
+
+ CheckRootKey(hwnd);
+
+ if (!OpenLogfile(python_dir))
+ break;
/*
* The scheme we have to use depends on the Python version...
@@ -1947,202 +1947,202 @@ InstallFilesDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
'data' : '$base',
}
*/
- scheme = GetScheme(py_major, py_minor);
- /* Run the pre-install script. */
- if (pre_install_script && *pre_install_script) {
- SetDlgItemText (hwnd, IDC_TITLE,
- "Running pre-installation script");
- run_simple_script(pre_install_script);
- }
- if (!success) {
- break;
- }
- /* Extract all files from the archive */
- SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
- if (!unzip_archive (scheme,
- python_dir, arc_data,
- arc_size, notify))
- set_failure_reason("Failed to unzip installation files");
- /* Compile the py-files */
- if (success && pyc_compile) {
- int errors;
- HINSTANCE hPython;
- SetDlgItemText(hwnd, IDC_TITLE,
- "Compiling files to .pyc...");
-
- SetDlgItemText(hDialog, IDC_INFO, "Loading python...");
- hPython = LoadPythonDll(pythondll);
- if (hPython) {
- errors = compile_filelist(hPython, FALSE);
- FreeLibrary(hPython);
- }
- /* Compilation errors are intentionally ignored:
- * Python2.0 contains a bug which will result
- * in sys.path containing garbage under certain
- * circumstances, and an error message will only
- * confuse the user.
- */
- }
- if (success && pyo_compile) {
- int errors;
- HINSTANCE hPython;
- SetDlgItemText(hwnd, IDC_TITLE,
- "Compiling files to .pyo...");
-
- SetDlgItemText(hDialog, IDC_INFO, "Loading python...");
- hPython = LoadPythonDll(pythondll);
- if (hPython) {
- errors = compile_filelist(hPython, TRUE);
- FreeLibrary(hPython);
- }
- /* Errors ignored: see above */
- }
-
-
- break;
-
- case PSN_RESET:
- break;
-
- default:
- break;
- }
- }
- return 0;
+ scheme = GetScheme(py_major, py_minor);
+ /* Run the pre-install script. */
+ if (pre_install_script && *pre_install_script) {
+ SetDlgItemText (hwnd, IDC_TITLE,
+ "Running pre-installation script");
+ run_simple_script(pre_install_script);
+ }
+ if (!success) {
+ break;
+ }
+ /* Extract all files from the archive */
+ SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
+ if (!unzip_archive (scheme,
+ python_dir, arc_data,
+ arc_size, notify))
+ set_failure_reason("Failed to unzip installation files");
+ /* Compile the py-files */
+ if (success && pyc_compile) {
+ int errors;
+ HINSTANCE hPython;
+ SetDlgItemText(hwnd, IDC_TITLE,
+ "Compiling files to .pyc...");
+
+ SetDlgItemText(hDialog, IDC_INFO, "Loading python...");
+ hPython = LoadPythonDll(pythondll);
+ if (hPython) {
+ errors = compile_filelist(hPython, FALSE);
+ FreeLibrary(hPython);
+ }
+ /* Compilation errors are intentionally ignored:
+ * Python2.0 contains a bug which will result
+ * in sys.path containing garbage under certain
+ * circumstances, and an error message will only
+ * confuse the user.
+ */
+ }
+ if (success && pyo_compile) {
+ int errors;
+ HINSTANCE hPython;
+ SetDlgItemText(hwnd, IDC_TITLE,
+ "Compiling files to .pyo...");
+
+ SetDlgItemText(hDialog, IDC_INFO, "Loading python...");
+ hPython = LoadPythonDll(pythondll);
+ if (hPython) {
+ errors = compile_filelist(hPython, TRUE);
+ FreeLibrary(hPython);
+ }
+ /* Errors ignored: see above */
+ }
+
+
+ break;
+
+ case PSN_RESET:
+ break;
+
+ default:
+ break;
+ }
+ }
+ return 0;
}
BOOL CALLBACK
FinishedDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
- LPNMHDR lpnm;
-
- switch (msg) {
- case WM_INITDIALOG:
- if (hBitmap)
- SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
- IMAGE_BITMAP, (LPARAM)hBitmap);
- if (!success)
- SetDlgItemText(hwnd, IDC_INFO, get_failure_reason());
-
- /* async delay: will show the dialog box completely before
- the install_script is started */
- PostMessage(hwnd, WM_USER, 0, 0L);
- return TRUE;
-
- case WM_USER:
-
- if (success && install_script && install_script[0]) {
- char fname[MAX_PATH];
- char *buffer;
- HCURSOR hCursor;
- int result;
-
- char *argv[3] = {NULL, "-install", NULL};
-
- SetDlgItemText(hwnd, IDC_TITLE,
- "Please wait while running postinstall script...");
- strcpy(fname, python_dir);
- strcat(fname, "\\Scripts\\");
- strcat(fname, install_script);
-
- if (logfile)
- fprintf(logfile, "300 Run Script: [%s]%s\n", pythondll, fname);
-
- hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
-
- argv[0] = fname;
-
- result = run_installscript(fname, 2, argv, &buffer);
- if (0 != result) {
- fprintf(stderr, "*** run_installscript: internal error 0x%X ***\n", result);
- }
- if (buffer)
- SetDlgItemText(hwnd, IDC_INFO, buffer);
- SetDlgItemText(hwnd, IDC_TITLE,
- "Postinstall script finished.\n"
- "Click the Finish button to exit the Setup wizard.");
-
- free(buffer);
- SetCursor(hCursor);
- CloseLogfile();
- }
-
- return TRUE;
-
- case WM_NOTIFY:
- lpnm = (LPNMHDR) lParam;
-
- switch (lpnm->code) {
- case PSN_SETACTIVE: /* Enable the Finish button */
- PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_FINISH);
- break;
-
- case PSN_WIZNEXT:
- break;
-
- case PSN_WIZFINISH:
- break;
-
- case PSN_RESET:
- break;
-
- default:
- break;
- }
- }
- return 0;
+ LPNMHDR lpnm;
+
+ switch (msg) {
+ case WM_INITDIALOG:
+ if (hBitmap)
+ SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE,
+ IMAGE_BITMAP, (LPARAM)hBitmap);
+ if (!success)
+ SetDlgItemText(hwnd, IDC_INFO, get_failure_reason());
+
+ /* async delay: will show the dialog box completely before
+ the install_script is started */
+ PostMessage(hwnd, WM_USER, 0, 0L);
+ return TRUE;
+
+ case WM_USER:
+
+ if (success && install_script && install_script[0]) {
+ char fname[MAX_PATH];
+ char *buffer;
+ HCURSOR hCursor;
+ int result;
+
+ char *argv[3] = {NULL, "-install", NULL};
+
+ SetDlgItemText(hwnd, IDC_TITLE,
+ "Please wait while running postinstall script...");
+ strcpy(fname, python_dir);
+ strcat(fname, "\\Scripts\\");
+ strcat(fname, install_script);
+
+ if (logfile)
+ fprintf(logfile, "300 Run Script: [%s]%s\n", pythondll, fname);
+
+ hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
+
+ argv[0] = fname;
+
+ result = run_installscript(fname, 2, argv, &buffer);
+ if (0 != result) {
+ fprintf(stderr, "*** run_installscript: internal error 0x%X ***\n", result);
+ }
+ if (buffer)
+ SetDlgItemText(hwnd, IDC_INFO, buffer);
+ SetDlgItemText(hwnd, IDC_TITLE,
+ "Postinstall script finished.\n"
+ "Click the Finish button to exit the Setup wizard.");
+
+ free(buffer);
+ SetCursor(hCursor);
+ CloseLogfile();
+ }
+
+ return TRUE;
+
+ case WM_NOTIFY:
+ lpnm = (LPNMHDR) lParam;
+
+ switch (lpnm->code) {
+ case PSN_SETACTIVE: /* Enable the Finish button */
+ PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_FINISH);
+ break;
+
+ case PSN_WIZNEXT:
+ break;
+
+ case PSN_WIZFINISH:
+ break;
+
+ case PSN_RESET:
+ break;
+
+ default:
+ break;
+ }
+ }
+ return 0;
}
void RunWizard(HWND hwnd)
{
- PROPSHEETPAGE psp = {0};
- HPROPSHEETPAGE ahpsp[4] = {0};
- PROPSHEETHEADER psh = {0};
-
- /* Display module information */
- psp.dwSize = sizeof(psp);
- psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
- psp.hInstance = GetModuleHandle (NULL);
- psp.lParam = 0;
- psp.pfnDlgProc = IntroDlgProc;
- psp.pszTemplate = MAKEINTRESOURCE(IDD_INTRO);
-
- ahpsp[0] = CreatePropertySheetPage(&psp);
-
- /* Select python version to use */
- psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
- psp.pszTemplate = MAKEINTRESOURCE(IDD_SELECTPYTHON);
- psp.pfnDlgProc = SelectPythonDlgProc;
-
- ahpsp[1] = CreatePropertySheetPage(&psp);
-
- /* Install the files */
- psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
- psp.pszTemplate = MAKEINTRESOURCE(IDD_INSTALLFILES);
- psp.pfnDlgProc = InstallFilesDlgProc;
-
- ahpsp[2] = CreatePropertySheetPage(&psp);
-
- /* Show success or failure */
- psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
- psp.pszTemplate = MAKEINTRESOURCE(IDD_FINISHED);
- psp.pfnDlgProc = FinishedDlgProc;
-
- ahpsp[3] = CreatePropertySheetPage(&psp);
-
- /* Create the property sheet */
- psh.dwSize = sizeof(psh);
- psh.hInstance = GetModuleHandle(NULL);
- psh.hwndParent = hwnd;
- psh.phpage = ahpsp;
- psh.dwFlags = PSH_WIZARD/*97*//*|PSH_WATERMARK|PSH_HEADER*/;
- psh.pszbmWatermark = NULL;
- psh.pszbmHeader = NULL;
- psh.nStartPage = 0;
- psh.nPages = 4;
-
- PropertySheet(&psh);
+ PROPSHEETPAGE psp = {0};
+ HPROPSHEETPAGE ahpsp[4] = {0};
+ PROPSHEETHEADER psh = {0};
+
+ /* Display module information */
+ psp.dwSize = sizeof(psp);
+ psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
+ psp.hInstance = GetModuleHandle (NULL);
+ psp.lParam = 0;
+ psp.pfnDlgProc = IntroDlgProc;
+ psp.pszTemplate = MAKEINTRESOURCE(IDD_INTRO);
+
+ ahpsp[0] = CreatePropertySheetPage(&psp);
+
+ /* Select python version to use */
+ psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
+ psp.pszTemplate = MAKEINTRESOURCE(IDD_SELECTPYTHON);
+ psp.pfnDlgProc = SelectPythonDlgProc;
+
+ ahpsp[1] = CreatePropertySheetPage(&psp);
+
+ /* Install the files */
+ psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
+ psp.pszTemplate = MAKEINTRESOURCE(IDD_INSTALLFILES);
+ psp.pfnDlgProc = InstallFilesDlgProc;
+
+ ahpsp[2] = CreatePropertySheetPage(&psp);
+
+ /* Show success or failure */
+ psp.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
+ psp.pszTemplate = MAKEINTRESOURCE(IDD_FINISHED);
+ psp.pfnDlgProc = FinishedDlgProc;
+
+ ahpsp[3] = CreatePropertySheetPage(&psp);
+
+ /* Create the property sheet */
+ psh.dwSize = sizeof(psh);
+ psh.hInstance = GetModuleHandle(NULL);
+ psh.hwndParent = hwnd;
+ psh.phpage = ahpsp;
+ psh.dwFlags = PSH_WIZARD/*97*//*|PSH_WATERMARK|PSH_HEADER*/;
+ psh.pszbmWatermark = NULL;
+ psh.pszbmHeader = NULL;
+ psh.nStartPage = 0;
+ psh.nPages = 4;
+
+ PropertySheet(&psh);
}
// subtly different from HasLocalMachinePrivs(), in that after executing
@@ -2150,16 +2150,16 @@ void RunWizard(HWND hwnd)
// such implication for HasLocalMachinePrivs
BOOL MyIsUserAnAdmin()
{
- typedef BOOL (WINAPI *PFNIsUserAnAdmin)();
- static PFNIsUserAnAdmin pfnIsUserAnAdmin = NULL;
- HMODULE shell32;
- // This function isn't guaranteed to be available (and it can't hurt
- // to leave the library loaded)
- if (0 == (shell32=LoadLibrary("shell32.dll")))
- return FALSE;
- if (0 == (pfnIsUserAnAdmin=(PFNIsUserAnAdmin)GetProcAddress(shell32, "IsUserAnAdmin")))
- return FALSE;
- return (*pfnIsUserAnAdmin)();
+ typedef BOOL (WINAPI *PFNIsUserAnAdmin)();
+ static PFNIsUserAnAdmin pfnIsUserAnAdmin = NULL;
+ HMODULE shell32;
+ // This function isn't guaranteed to be available (and it can't hurt
+ // to leave the library loaded)
+ if (0 == (shell32=LoadLibrary("shell32.dll")))
+ return FALSE;
+ if (0 == (pfnIsUserAnAdmin=(PFNIsUserAnAdmin)GetProcAddress(shell32, "IsUserAnAdmin")))
+ return FALSE;
+ return (*pfnIsUserAnAdmin)();
}
// Some magic for Vista's UAC. If there is a target_version, and
@@ -2171,38 +2171,38 @@ BOOL MyIsUserAnAdmin()
// Returns TRUE if we should spawn an elevated child
BOOL NeedAutoUAC()
{
- HKEY hk;
- char key_name[80];
- // no Python version info == we can't know yet.
- if (target_version[0] == '\0')
- return FALSE;
- // see how python is current installed
- wsprintf(key_name,
- "Software\\Python\\PythonCore\\%s\\InstallPath",
- target_version);
- if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE,
- key_name, 0, KEY_READ, &hk))
- return FALSE;
- RegCloseKey(hk);
- // Python is installed in HKLM - we must elevate.
- return TRUE;
+ HKEY hk;
+ char key_name[80];
+ // no Python version info == we can't know yet.
+ if (target_version[0] == '\0')
+ return FALSE;
+ // see how python is current installed
+ wsprintf(key_name,
+ "Software\\Python\\PythonCore\\%s\\InstallPath",
+ target_version);
+ if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+ key_name, 0, KEY_READ, &hk))
+ return FALSE;
+ RegCloseKey(hk);
+ // Python is installed in HKLM - we must elevate.
+ return TRUE;
}
// Returns TRUE if the platform supports UAC.
BOOL PlatformSupportsUAC()
{
- // Note that win2k does seem to support ShellExecute with 'runas',
- // but does *not* support IsUserAnAdmin - so we just pretend things
- // only work on XP and later.
- BOOL bIsWindowsXPorLater;
- OSVERSIONINFO winverinfo;
- winverinfo.dwOSVersionInfoSize = sizeof(winverinfo);
- if (!GetVersionEx(&winverinfo))
- return FALSE; // something bad has gone wrong
- bIsWindowsXPorLater =
+ // Note that win2k does seem to support ShellExecute with 'runas',
+ // but does *not* support IsUserAnAdmin - so we just pretend things
+ // only work on XP and later.
+ BOOL bIsWindowsXPorLater;
+ OSVERSIONINFO winverinfo;
+ winverinfo.dwOSVersionInfoSize = sizeof(winverinfo);
+ if (!GetVersionEx(&winverinfo))
+ return FALSE; // something bad has gone wrong
+ bIsWindowsXPorLater =
( (winverinfo.dwMajorVersion > 5) ||
( (winverinfo.dwMajorVersion == 5) && (winverinfo.dwMinorVersion >= 1) ));
- return bIsWindowsXPorLater;
+ return bIsWindowsXPorLater;
}
// Spawn ourself as an elevated application. On failure, a message is
@@ -2210,97 +2210,97 @@ BOOL PlatformSupportsUAC()
// on error.
void SpawnUAC()
{
- // interesting failure scenario that has been seen: initial executable
- // runs from a network drive - but once elevated, that network share
- // isn't seen, and ShellExecute fails with SE_ERR_ACCESSDENIED.
- int ret = (int)ShellExecute(0, "runas", modulename, "", NULL,
- SW_SHOWNORMAL);
- if (ret <= 32) {
- char msg[128];
- wsprintf(msg, "Failed to start elevated process (ShellExecute returned %d)", ret);
- MessageBox(0, msg, "Setup", MB_OK | MB_ICONERROR);
- }
+ // interesting failure scenario that has been seen: initial executable
+ // runs from a network drive - but once elevated, that network share
+ // isn't seen, and ShellExecute fails with SE_ERR_ACCESSDENIED.
+ int ret = (int)ShellExecute(0, "runas", modulename, "", NULL,
+ SW_SHOWNORMAL);
+ if (ret <= 32) {
+ char msg[128];
+ wsprintf(msg, "Failed to start elevated process (ShellExecute returned %d)", ret);
+ MessageBox(0, msg, "Setup", MB_OK | MB_ICONERROR);
+ }
}
int DoInstall(void)
{
- char ini_buffer[4096];
-
- /* Read installation information */
- GetPrivateProfileString("Setup", "title", "", ini_buffer,
- sizeof(ini_buffer), ini_file);
- unescape(title, ini_buffer, sizeof(title));
-
- GetPrivateProfileString("Setup", "info", "", ini_buffer,
- sizeof(ini_buffer), ini_file);
- unescape(info, ini_buffer, sizeof(info));
-
- GetPrivateProfileString("Setup", "build_info", "", build_info,
- sizeof(build_info), ini_file);
-
- pyc_compile = GetPrivateProfileInt("Setup", "target_compile", 1,
- ini_file);
- pyo_compile = GetPrivateProfileInt("Setup", "target_optimize", 1,
- ini_file);
-
- GetPrivateProfileString("Setup", "target_version", "",
- target_version, sizeof(target_version),
- ini_file);
-
- GetPrivateProfileString("metadata", "name", "",
- meta_name, sizeof(meta_name),
- ini_file);
-
- GetPrivateProfileString("Setup", "install_script", "",
- install_script, sizeof(install_script),
- ini_file);
-
- GetPrivateProfileString("Setup", "user_access_control", "",
- user_access_control, sizeof(user_access_control), ini_file);
-
- // See if we need to do the Vista UAC magic.
- if (strcmp(user_access_control, "force")==0) {
- if (PlatformSupportsUAC() && !MyIsUserAnAdmin()) {
- SpawnUAC();
- return 0;
- }
- // already admin - keep going
- } else if (strcmp(user_access_control, "auto")==0) {
- // Check if it looks like we need UAC control, based
- // on how Python itself was installed.
- if (PlatformSupportsUAC() && !MyIsUserAnAdmin() && NeedAutoUAC()) {
- SpawnUAC();
- return 0;
- }
- } else {
- // display a warning about unknown values - only the developer
- // of the extension will see it (until they fix it!)
- if (user_access_control[0] && strcmp(user_access_control, "none") != 0) {
- MessageBox(GetFocus(), "Bad user_access_control value", "oops", MB_OK);
- // nothing to do.
- }
- }
-
- hwndMain = CreateBackground(title);
-
- RunWizard(hwndMain);
-
- /* Clean up */
- UnmapViewOfFile(arc_data);
- if (ini_file)
- DeleteFile(ini_file);
-
- if (hBitmap)
- DeleteObject(hBitmap);
-
- return 0;
+ char ini_buffer[4096];
+
+ /* Read installation information */
+ GetPrivateProfileString("Setup", "title", "", ini_buffer,
+ sizeof(ini_buffer), ini_file);
+ unescape(title, ini_buffer, sizeof(title));
+
+ GetPrivateProfileString("Setup", "info", "", ini_buffer,
+ sizeof(ini_buffer), ini_file);
+ unescape(info, ini_buffer, sizeof(info));
+
+ GetPrivateProfileString("Setup", "build_info", "", build_info,
+ sizeof(build_info), ini_file);
+
+ pyc_compile = GetPrivateProfileInt("Setup", "target_compile", 1,
+ ini_file);
+ pyo_compile = GetPrivateProfileInt("Setup", "target_optimize", 1,
+ ini_file);
+
+ GetPrivateProfileString("Setup", "target_version", "",
+ target_version, sizeof(target_version),
+ ini_file);
+
+ GetPrivateProfileString("metadata", "name", "",
+ meta_name, sizeof(meta_name),
+ ini_file);
+
+ GetPrivateProfileString("Setup", "install_script", "",
+ install_script, sizeof(install_script),
+ ini_file);
+
+ GetPrivateProfileString("Setup", "user_access_control", "",
+ user_access_control, sizeof(user_access_control), ini_file);
+
+ // See if we need to do the Vista UAC magic.
+ if (strcmp(user_access_control, "force")==0) {
+ if (PlatformSupportsUAC() && !MyIsUserAnAdmin()) {
+ SpawnUAC();
+ return 0;
+ }
+ // already admin - keep going
+ } else if (strcmp(user_access_control, "auto")==0) {
+ // Check if it looks like we need UAC control, based
+ // on how Python itself was installed.
+ if (PlatformSupportsUAC() && !MyIsUserAnAdmin() && NeedAutoUAC()) {
+ SpawnUAC();
+ return 0;
+ }
+ } else {
+ // display a warning about unknown values - only the developer
+ // of the extension will see it (until they fix it!)
+ if (user_access_control[0] && strcmp(user_access_control, "none") != 0) {
+ MessageBox(GetFocus(), "Bad user_access_control value", "oops", MB_OK);
+ // nothing to do.
+ }
+ }
+
+ hwndMain = CreateBackground(title);
+
+ RunWizard(hwndMain);
+
+ /* Clean up */
+ UnmapViewOfFile(arc_data);
+ if (ini_file)
+ DeleteFile(ini_file);
+
+ if (hBitmap)
+ DeleteObject(hBitmap);
+
+ return 0;
}
/*********************** uninstall section ******************************/
static int compare(const void *p1, const void *p2)
{
- return strcmp(*(char **)p2, *(char **)p1);
+ return strcmp(*(char **)p2, *(char **)p1);
}
/*
@@ -2314,380 +2314,380 @@ static int compare(const void *p1, const void *p2)
*/
void remove_exe(void)
{
- char exename[_MAX_PATH];
- char batname[_MAX_PATH];
- FILE *fp;
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
-
- GetModuleFileName(NULL, exename, sizeof(exename));
- sprintf(batname, "%s.bat", exename);
- fp = fopen(batname, "w");
- fprintf(fp, ":Repeat\n");
- fprintf(fp, "del \"%s\"\n", exename);
- fprintf(fp, "if exist \"%s\" goto Repeat\n", exename);
- fprintf(fp, "del \"%s\"\n", batname);
- fclose(fp);
-
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- if (CreateProcess(NULL,
- batname,
- NULL,
- NULL,
- FALSE,
- CREATE_SUSPENDED | IDLE_PRIORITY_CLASS,
- NULL,
- "\\",
- &si,
- &pi)) {
- SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE);
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
- SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
- CloseHandle(pi.hProcess);
- ResumeThread(pi.hThread);
- CloseHandle(pi.hThread);
- }
+ char exename[_MAX_PATH];
+ char batname[_MAX_PATH];
+ FILE *fp;
+ STARTUPINFO si;
+ PROCESS_INFORMATION pi;
+
+ GetModuleFileName(NULL, exename, sizeof(exename));
+ sprintf(batname, "%s.bat", exename);
+ fp = fopen(batname, "w");
+ fprintf(fp, ":Repeat\n");
+ fprintf(fp, "del \"%s\"\n", exename);
+ fprintf(fp, "if exist \"%s\" goto Repeat\n", exename);
+ fprintf(fp, "del \"%s\"\n", batname);
+ fclose(fp);
+
+ ZeroMemory(&si, sizeof(si));
+ si.cb = sizeof(si);
+ si.dwFlags = STARTF_USESHOWWINDOW;
+ si.wShowWindow = SW_HIDE;
+ if (CreateProcess(NULL,
+ batname,
+ NULL,
+ NULL,
+ FALSE,
+ CREATE_SUSPENDED | IDLE_PRIORITY_CLASS,
+ NULL,
+ "\\",
+ &si,
+ &pi)) {
+ SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE);
+ SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
+ SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
+ CloseHandle(pi.hProcess);
+ ResumeThread(pi.hThread);
+ CloseHandle(pi.hThread);
+ }
}
void DeleteRegistryKey(char *string)
{
- char *keyname;
- char *subkeyname;
- char *delim;
- HKEY hKey;
- long result;
- char *line;
-
- line = strdup(string); /* so we can change it */
-
- keyname = strchr(line, '[');
- if (!keyname)
- return;
- ++keyname;
-
- subkeyname = strchr(keyname, ']');
- if (!subkeyname)
- return;
- *subkeyname++='\0';
- delim = strchr(subkeyname, '\n');
- if (delim)
- *delim = '\0';
-
- result = RegOpenKeyEx(hkey_root,
- keyname,
- 0,
- KEY_WRITE,
- &hKey);
-
- if (result != ERROR_SUCCESS)
- MessageBox(GetFocus(), string, "Could not open key", MB_OK);
- else {
- result = RegDeleteKey(hKey, subkeyname);
- if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND)
- MessageBox(GetFocus(), string, "Could not delete key", MB_OK);
- RegCloseKey(hKey);
- }
- free(line);
+ char *keyname;
+ char *subkeyname;
+ char *delim;
+ HKEY hKey;
+ long result;
+ char *line;
+
+ line = strdup(string); /* so we can change it */
+
+ keyname = strchr(line, '[');
+ if (!keyname)
+ return;
+ ++keyname;
+
+ subkeyname = strchr(keyname, ']');
+ if (!subkeyname)
+ return;
+ *subkeyname++='\0';
+ delim = strchr(subkeyname, '\n');
+ if (delim)
+ *delim = '\0';
+
+ result = RegOpenKeyEx(hkey_root,
+ keyname,
+ 0,
+ KEY_WRITE,
+ &hKey);
+
+ if (result != ERROR_SUCCESS)
+ MessageBox(GetFocus(), string, "Could not open key", MB_OK);
+ else {
+ result = RegDeleteKey(hKey, subkeyname);
+ if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND)
+ MessageBox(GetFocus(), string, "Could not delete key", MB_OK);
+ RegCloseKey(hKey);
+ }
+ free(line);
}
void DeleteRegistryValue(char *string)
{
- char *keyname;
- char *valuename;
- char *value;
- HKEY hKey;
- long result;
- char *line;
+ char *keyname;
+ char *valuename;
+ char *value;
+ HKEY hKey;
+ long result;
+ char *line;
- line = strdup(string); /* so we can change it */
+ line = strdup(string); /* so we can change it */
/* Format is 'Reg DB Value: [key]name=value' */
- keyname = strchr(line, '[');
- if (!keyname)
- return;
- ++keyname;
- valuename = strchr(keyname, ']');
- if (!valuename)
- return;
- *valuename++ = '\0';
- value = strchr(valuename, '=');
- if (!value)
- return;
-
- *value++ = '\0';
-
- result = RegOpenKeyEx(hkey_root,
- keyname,
- 0,
- KEY_WRITE,
- &hKey);
- if (result != ERROR_SUCCESS)
- MessageBox(GetFocus(), string, "Could not open key", MB_OK);
- else {
- result = RegDeleteValue(hKey, valuename);
- if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND)
- MessageBox(GetFocus(), string, "Could not delete value", MB_OK);
- RegCloseKey(hKey);
- }
- free(line);
+ keyname = strchr(line, '[');
+ if (!keyname)
+ return;
+ ++keyname;
+ valuename = strchr(keyname, ']');
+ if (!valuename)
+ return;
+ *valuename++ = '\0';
+ value = strchr(valuename, '=');
+ if (!value)
+ return;
+
+ *value++ = '\0';
+
+ result = RegOpenKeyEx(hkey_root,
+ keyname,
+ 0,
+ KEY_WRITE,
+ &hKey);
+ if (result != ERROR_SUCCESS)
+ MessageBox(GetFocus(), string, "Could not open key", MB_OK);
+ else {
+ result = RegDeleteValue(hKey, valuename);
+ if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND)
+ MessageBox(GetFocus(), string, "Could not delete value", MB_OK);
+ RegCloseKey(hKey);
+ }
+ free(line);
}
BOOL MyDeleteFile(char *line)
{
- char *pathname = strchr(line, ':');
- if (!pathname)
- return FALSE;
- ++pathname;
- while (isspace(*pathname))
- ++pathname;
- return DeleteFile(pathname);
+ char *pathname = strchr(line, ':');
+ if (!pathname)
+ return FALSE;
+ ++pathname;
+ while (isspace(*pathname))
+ ++pathname;
+ return DeleteFile(pathname);
}
BOOL MyRemoveDirectory(char *line)
{
- char *pathname = strchr(line, ':');
- if (!pathname)
- return FALSE;
- ++pathname;
- while (isspace(*pathname))
- ++pathname;
- return RemoveDirectory(pathname);
+ char *pathname = strchr(line, ':');
+ if (!pathname)
+ return FALSE;
+ ++pathname;
+ while (isspace(*pathname))
+ ++pathname;
+ return RemoveDirectory(pathname);
}
BOOL Run_RemoveScript(char *line)
{
- char *dllname;
- char *scriptname;
- static char lastscript[MAX_PATH];
+ char *dllname;
+ char *scriptname;
+ static char lastscript[MAX_PATH];
/* Format is 'Run Scripts: [pythondll]scriptname' */
/* XXX Currently, pythondll carries no path!!! */
- dllname = strchr(line, '[');
- if (!dllname)
- return FALSE;
- ++dllname;
- scriptname = strchr(dllname, ']');
- if (!scriptname)
- return FALSE;
- *scriptname++ = '\0';
- /* this function may be called more than one time with the same
- script, only run it one time */
- if (strcmp(lastscript, scriptname)) {
- char *argv[3] = {NULL, "-remove", NULL};
- char *buffer = NULL;
-
- argv[0] = scriptname;
-
- if (0 != run_installscript(scriptname, 2, argv, &buffer))
- fprintf(stderr, "*** Could not run installation script ***");
-
- if (buffer && buffer[0])
- MessageBox(GetFocus(), buffer, "uninstall-script", MB_OK);
- free(buffer);
-
- strcpy(lastscript, scriptname);
- }
- return TRUE;
+ dllname = strchr(line, '[');
+ if (!dllname)
+ return FALSE;
+ ++dllname;
+ scriptname = strchr(dllname, ']');
+ if (!scriptname)
+ return FALSE;
+ *scriptname++ = '\0';
+ /* this function may be called more than one time with the same
+ script, only run it one time */
+ if (strcmp(lastscript, scriptname)) {
+ char *argv[3] = {NULL, "-remove", NULL};
+ char *buffer = NULL;
+
+ argv[0] = scriptname;
+
+ if (0 != run_installscript(scriptname, 2, argv, &buffer))
+ fprintf(stderr, "*** Could not run installation script ***");
+
+ if (buffer && buffer[0])
+ MessageBox(GetFocus(), buffer, "uninstall-script", MB_OK);
+ free(buffer);
+
+ strcpy(lastscript, scriptname);
+ }
+ return TRUE;
}
int DoUninstall(int argc, char **argv)
{
- FILE *logfile;
- char buffer[4096];
- int nLines = 0;
- int i;
- char *cp;
- int nFiles = 0;
- int nDirs = 0;
- int nErrors = 0;
- char **lines;
- int lines_buffer_size = 10;
-
- if (argc != 3) {
- MessageBox(NULL,
- "Wrong number of args",
- NULL,
- MB_OK);
- return 1; /* Error */
- }
- if (strcmp(argv[1], "-u")) {
- MessageBox(NULL,
- "2. arg is not -u",
- NULL,
- MB_OK);
- return 1; /* Error */
- }
-
- logfile = fopen(argv[2], "r");
- if (!logfile) {
- MessageBox(NULL,
- "could not open logfile",
- NULL,
- MB_OK);
- return 1; /* Error */
- }
-
- lines = (char **)malloc(sizeof(char *) * lines_buffer_size);
- if (!lines)
- return SystemError(0, "Out of memory");
-
- /* Read the whole logfile, realloacting the buffer */
- while (fgets(buffer, sizeof(buffer), logfile)) {
- int len = strlen(buffer);
- /* remove trailing white space */
- while (isspace(buffer[len-1]))
- len -= 1;
- buffer[len] = '\0';
- lines[nLines++] = strdup(buffer);
- if (nLines >= lines_buffer_size) {
- lines_buffer_size += 10;
- lines = (char **)realloc(lines,
- sizeof(char *) * lines_buffer_size);
- if (!lines)
- return SystemError(0, "Out of memory");
- }
- }
- fclose(logfile);
-
- /* Sort all the lines, so that highest 3-digit codes are first */
- qsort(&lines[0], nLines, sizeof(char *),
- compare);
-
- if (IDYES != MessageBox(NULL,
- "Are you sure you want to remove\n"
- "this package from your computer?",
- "Please confirm",
- MB_YESNO | MB_ICONQUESTION))
- return 0;
-
- hkey_root = HKEY_LOCAL_MACHINE;
- cp = "";
- for (i = 0; i < nLines; ++i) {
- /* Ignore duplicate lines */
- if (strcmp(cp, lines[i])) {
- int ign;
- cp = lines[i];
- /* Parse the lines */
- if (2 == sscanf(cp, "%d Root Key: %s", &ign, &buffer)) {
- if (strcmp(buffer, "HKEY_CURRENT_USER")==0)
- hkey_root = HKEY_CURRENT_USER;
- else {
- // HKLM - check they have permissions.
- if (!HasLocalMachinePrivs()) {
- MessageBox(GetFocus(),
- "You do not seem to have sufficient access rights\n"
- "on this machine to uninstall this software",
- NULL,
- MB_OK | MB_ICONSTOP);
- return 1; /* Error */
- }
- }
- } else if (2 == sscanf(cp, "%d Made Dir: %s", &ign, &buffer)) {
- if (MyRemoveDirectory(cp))
- ++nDirs;
- else {
- int code = GetLastError();
- if (code != 2 && code != 3) { /* file or path not found */
- ++nErrors;
- }
- }
- } else if (2 == sscanf(cp, "%d File Copy: %s", &ign, &buffer)) {
- if (MyDeleteFile(cp))
- ++nFiles;
- else {
- int code = GetLastError();
- if (code != 2 && code != 3) { /* file or path not found */
- ++nErrors;
- }
- }
- } else if (2 == sscanf(cp, "%d File Overwrite: %s", &ign, &buffer)) {
- if (MyDeleteFile(cp))
- ++nFiles;
- else {
- int code = GetLastError();
- if (code != 2 && code != 3) { /* file or path not found */
- ++nErrors;
- }
- }
- } else if (2 == sscanf(cp, "%d Reg DB Key: %s", &ign, &buffer)) {
- DeleteRegistryKey(cp);
- } else if (2 == sscanf(cp, "%d Reg DB Value: %s", &ign, &buffer)) {
- DeleteRegistryValue(cp);
- } else if (2 == sscanf(cp, "%d Run Script: %s", &ign, &buffer)) {
- Run_RemoveScript(cp);
- }
- }
- }
-
- if (DeleteFile(argv[2])) {
- ++nFiles;
- } else {
- ++nErrors;
- SystemError(GetLastError(), argv[2]);
- }
- if (nErrors)
- wsprintf(buffer,
- "%d files and %d directories removed\n"
- "%d files or directories could not be removed",
- nFiles, nDirs, nErrors);
- else
- wsprintf(buffer, "%d files and %d directories removed",
- nFiles, nDirs);
- MessageBox(NULL, buffer, "Uninstall Finished!",
- MB_OK | MB_ICONINFORMATION);
- remove_exe();
- return 0;
+ FILE *logfile;
+ char buffer[4096];
+ int nLines = 0;
+ int i;
+ char *cp;
+ int nFiles = 0;
+ int nDirs = 0;
+ int nErrors = 0;
+ char **lines;
+ int lines_buffer_size = 10;
+
+ if (argc != 3) {
+ MessageBox(NULL,
+ "Wrong number of args",
+ NULL,
+ MB_OK);
+ return 1; /* Error */
+ }
+ if (strcmp(argv[1], "-u")) {
+ MessageBox(NULL,
+ "2. arg is not -u",
+ NULL,
+ MB_OK);
+ return 1; /* Error */
+ }
+
+ logfile = fopen(argv[2], "r");
+ if (!logfile) {
+ MessageBox(NULL,
+ "could not open logfile",
+ NULL,
+ MB_OK);
+ return 1; /* Error */
+ }
+
+ lines = (char **)malloc(sizeof(char *) * lines_buffer_size);
+ if (!lines)
+ return SystemError(0, "Out of memory");
+
+ /* Read the whole logfile, realloacting the buffer */
+ while (fgets(buffer, sizeof(buffer), logfile)) {
+ int len = strlen(buffer);
+ /* remove trailing white space */
+ while (isspace(buffer[len-1]))
+ len -= 1;
+ buffer[len] = '\0';
+ lines[nLines++] = strdup(buffer);
+ if (nLines >= lines_buffer_size) {
+ lines_buffer_size += 10;
+ lines = (char **)realloc(lines,
+ sizeof(char *) * lines_buffer_size);
+ if (!lines)
+ return SystemError(0, "Out of memory");
+ }
+ }
+ fclose(logfile);
+
+ /* Sort all the lines, so that highest 3-digit codes are first */
+ qsort(&lines[0], nLines, sizeof(char *),
+ compare);
+
+ if (IDYES != MessageBox(NULL,
+ "Are you sure you want to remove\n"
+ "this package from your computer?",
+ "Please confirm",
+ MB_YESNO | MB_ICONQUESTION))
+ return 0;
+
+ hkey_root = HKEY_LOCAL_MACHINE;
+ cp = "";
+ for (i = 0; i < nLines; ++i) {
+ /* Ignore duplicate lines */
+ if (strcmp(cp, lines[i])) {
+ int ign;
+ cp = lines[i];
+ /* Parse the lines */
+ if (2 == sscanf(cp, "%d Root Key: %s", &ign, &buffer)) {
+ if (strcmp(buffer, "HKEY_CURRENT_USER")==0)
+ hkey_root = HKEY_CURRENT_USER;
+ else {
+ // HKLM - check they have permissions.
+ if (!HasLocalMachinePrivs()) {
+ MessageBox(GetFocus(),
+ "You do not seem to have sufficient access rights\n"
+ "on this machine to uninstall this software",
+ NULL,
+ MB_OK | MB_ICONSTOP);
+ return 1; /* Error */
+ }
+ }
+ } else if (2 == sscanf(cp, "%d Made Dir: %s", &ign, &buffer)) {
+ if (MyRemoveDirectory(cp))
+ ++nDirs;
+ else {
+ int code = GetLastError();
+ if (code != 2 && code != 3) { /* file or path not found */
+ ++nErrors;
+ }
+ }
+ } else if (2 == sscanf(cp, "%d File Copy: %s", &ign, &buffer)) {
+ if (MyDeleteFile(cp))
+ ++nFiles;
+ else {
+ int code = GetLastError();
+ if (code != 2 && code != 3) { /* file or path not found */
+ ++nErrors;
+ }
+ }
+ } else if (2 == sscanf(cp, "%d File Overwrite: %s", &ign, &buffer)) {
+ if (MyDeleteFile(cp))
+ ++nFiles;
+ else {
+ int code = GetLastError();
+ if (code != 2 && code != 3) { /* file or path not found */
+ ++nErrors;
+ }
+ }
+ } else if (2 == sscanf(cp, "%d Reg DB Key: %s", &ign, &buffer)) {
+ DeleteRegistryKey(cp);
+ } else if (2 == sscanf(cp, "%d Reg DB Value: %s", &ign, &buffer)) {
+ DeleteRegistryValue(cp);
+ } else if (2 == sscanf(cp, "%d Run Script: %s", &ign, &buffer)) {
+ Run_RemoveScript(cp);
+ }
+ }
+ }
+
+ if (DeleteFile(argv[2])) {
+ ++nFiles;
+ } else {
+ ++nErrors;
+ SystemError(GetLastError(), argv[2]);
+ }
+ if (nErrors)
+ wsprintf(buffer,
+ "%d files and %d directories removed\n"
+ "%d files or directories could not be removed",
+ nFiles, nDirs, nErrors);
+ else
+ wsprintf(buffer, "%d files and %d directories removed",
+ nFiles, nDirs);
+ MessageBox(NULL, buffer, "Uninstall Finished!",
+ MB_OK | MB_ICONINFORMATION);
+ remove_exe();
+ return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
- LPSTR lpszCmdLine, INT nCmdShow)
+ LPSTR lpszCmdLine, INT nCmdShow)
{
- extern int __argc;
- extern char **__argv;
- char *basename;
-
- GetModuleFileName(NULL, modulename, sizeof(modulename));
- GetModuleFileNameW(NULL, wmodulename, sizeof(wmodulename)/sizeof(wmodulename[0]));
-
- /* Map the executable file to memory */
- arc_data = MapExistingFile(modulename, &arc_size);
- if (!arc_data) {
- SystemError(GetLastError(), "Could not open archive");
- return 1;
- }
-
- /* OK. So this program can act as installer (self-extracting
- * zip-file, or as uninstaller when started with '-u logfile'
- * command line flags.
- *
- * The installer is usually started without command line flags,
- * and the uninstaller is usually started with the '-u logfile'
- * flag. What to do if some innocent user double-clicks the
- * exe-file?
- * The following implements a defensive strategy...
- */
-
- /* Try to extract the configuration data into a temporary file */
- if (ExtractInstallData(arc_data, arc_size, &exe_size,
- &ini_file, &pre_install_script))
- return DoInstall();
-
- if (!ini_file && __argc > 1) {
- return DoUninstall(__argc, __argv);
- }
-
-
- basename = strrchr(modulename, '\\');
- if (basename)
- ++basename;
-
- /* Last guess about the purpose of this program */
- if (basename && (0 == strncmp(basename, "Remove", 6)))
- SystemError(0, "This program is normally started by windows");
- else
- SystemError(0, "Setup program invalid or damaged");
- return 1;
+ extern int __argc;
+ extern char **__argv;
+ char *basename;
+
+ GetModuleFileName(NULL, modulename, sizeof(modulename));
+ GetModuleFileNameW(NULL, wmodulename, sizeof(wmodulename)/sizeof(wmodulename[0]));
+
+ /* Map the executable file to memory */
+ arc_data = MapExistingFile(modulename, &arc_size);
+ if (!arc_data) {
+ SystemError(GetLastError(), "Could not open archive");
+ return 1;
+ }
+
+ /* OK. So this program can act as installer (self-extracting
+ * zip-file, or as uninstaller when started with '-u logfile'
+ * command line flags.
+ *
+ * The installer is usually started without command line flags,
+ * and the uninstaller is usually started with the '-u logfile'
+ * flag. What to do if some innocent user double-clicks the
+ * exe-file?
+ * The following implements a defensive strategy...
+ */
+
+ /* Try to extract the configuration data into a temporary file */
+ if (ExtractInstallData(arc_data, arc_size, &exe_size,
+ &ini_file, &pre_install_script))
+ return DoInstall();
+
+ if (!ini_file && __argc > 1) {
+ return DoUninstall(__argc, __argv);
+ }
+
+
+ basename = strrchr(modulename, '\\');
+ if (basename)
+ ++basename;
+
+ /* Last guess about the purpose of this program */
+ if (basename && (0 == strncmp(basename, "Remove", 6)))
+ SystemError(0, "This program is normally started by windows");
+ else
+ SystemError(0, "Setup program invalid or damaged");
+ return 1;
}
diff --git a/PC/config.c b/PC/config.c
index 5f58f20..28d02b1 100644
--- a/PC/config.c
+++ b/PC/config.c
@@ -71,87 +71,87 @@ extern PyObject* PyInit_imp(void);
struct _inittab _PyImport_Inittab[] = {
- {"array", PyInit_array},
- {"_ast", PyInit__ast},
+ {"array", PyInit_array},
+ {"_ast", PyInit__ast},
#ifdef MS_WINDOWS
#ifndef MS_WINI64
- {"audioop", PyInit_audioop},
+ {"audioop", PyInit_audioop},
#endif
#endif
- {"binascii", PyInit_binascii},
- {"cmath", PyInit_cmath},
- {"errno", PyInit_errno},
- {"gc", PyInit_gc},
- {"math", PyInit_math},
- {"nt", PyInit_nt}, /* Use the NT os functions, not posix */
- {"operator", PyInit_operator},
- {"signal", PyInit_signal},
- {"_md5", PyInit__md5},
- {"_sha1", PyInit__sha1},
- {"_sha256", PyInit__sha256},
- {"_sha512", PyInit__sha512},
- {"time", PyInit_time},
+ {"binascii", PyInit_binascii},
+ {"cmath", PyInit_cmath},
+ {"errno", PyInit_errno},
+ {"gc", PyInit_gc},
+ {"math", PyInit_math},
+ {"nt", PyInit_nt}, /* Use the NT os functions, not posix */
+ {"operator", PyInit_operator},
+ {"signal", PyInit_signal},
+ {"_md5", PyInit__md5},
+ {"_sha1", PyInit__sha1},
+ {"_sha256", PyInit__sha256},
+ {"_sha512", PyInit__sha512},
+ {"time", PyInit_time},
#ifdef WITH_THREAD
- {"_thread", PyInit__thread},
+ {"_thread", PyInit__thread},
#endif
#ifdef WIN32
- {"msvcrt", PyInit_msvcrt},
- {"_locale", PyInit__locale},
+ {"msvcrt", PyInit_msvcrt},
+ {"_locale", PyInit__locale},
#endif
- /* XXX Should _subprocess go in a WIN32 block? not WIN64? */
- {"_subprocess", PyInit__subprocess},
-
- {"_codecs", PyInit__codecs},
- {"_weakref", PyInit__weakref},
- {"_random", PyInit__random},
- {"_bisect", PyInit__bisect},
- {"_heapq", PyInit__heapq},
- {"_lsprof", PyInit__lsprof},
- {"itertools", PyInit_itertools},
- {"_collections", PyInit__collections},
- {"_symtable", PyInit__symtable},
- {"mmap", PyInit_mmap},
- {"_csv", PyInit__csv},
- {"_sre", PyInit__sre},
- {"parser", PyInit_parser},
- {"winreg", PyInit_winreg},
- {"_struct", PyInit__struct},
- {"datetime", PyInit_datetime},
- {"_functools", PyInit__functools},
- {"_json", PyInit__json},
-
- {"xxsubtype", PyInit_xxsubtype},
- {"zipimport", PyInit_zipimport},
- {"zlib", PyInit_zlib},
-
- /* CJK codecs */
- {"_multibytecodec", PyInit__multibytecodec},
- {"_codecs_cn", PyInit__codecs_cn},
- {"_codecs_hk", PyInit__codecs_hk},
- {"_codecs_iso2022", PyInit__codecs_iso2022},
- {"_codecs_jp", PyInit__codecs_jp},
- {"_codecs_kr", PyInit__codecs_kr},
- {"_codecs_tw", PyInit__codecs_tw},
+ /* XXX Should _subprocess go in a WIN32 block? not WIN64? */
+ {"_subprocess", PyInit__subprocess},
+
+ {"_codecs", PyInit__codecs},
+ {"_weakref", PyInit__weakref},
+ {"_random", PyInit__random},
+ {"_bisect", PyInit__bisect},
+ {"_heapq", PyInit__heapq},
+ {"_lsprof", PyInit__lsprof},
+ {"itertools", PyInit_itertools},
+ {"_collections", PyInit__collections},
+ {"_symtable", PyInit__symtable},
+ {"mmap", PyInit_mmap},
+ {"_csv", PyInit__csv},
+ {"_sre", PyInit__sre},
+ {"parser", PyInit_parser},
+ {"winreg", PyInit_winreg},
+ {"_struct", PyInit__struct},
+ {"datetime", PyInit_datetime},
+ {"_functools", PyInit__functools},
+ {"_json", PyInit__json},
+
+ {"xxsubtype", PyInit_xxsubtype},
+ {"zipimport", PyInit_zipimport},
+ {"zlib", PyInit_zlib},
+
+ /* CJK codecs */
+ {"_multibytecodec", PyInit__multibytecodec},
+ {"_codecs_cn", PyInit__codecs_cn},
+ {"_codecs_hk", PyInit__codecs_hk},
+ {"_codecs_iso2022", PyInit__codecs_iso2022},
+ {"_codecs_jp", PyInit__codecs_jp},
+ {"_codecs_kr", PyInit__codecs_kr},
+ {"_codecs_tw", PyInit__codecs_tw},
/* tools/freeze/makeconfig.py marker for additional "_inittab" entries */
/* -- ADDMODULE MARKER 2 -- */
- /* This module "lives in" with marshal.c */
- {"marshal", PyMarshal_Init},
+ /* This module "lives in" with marshal.c */
+ {"marshal", PyMarshal_Init},
- /* This lives it with import.c */
- {"imp", PyInit_imp},
+ /* This lives it with import.c */
+ {"imp", PyInit_imp},
- /* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
- {"builtins", NULL},
- {"sys", NULL},
- {"_warnings", _PyWarnings_Init},
+ /* These entries are here for sys.builtin_module_names */
+ {"__main__", NULL},
+ {"builtins", NULL},
+ {"sys", NULL},
+ {"_warnings", _PyWarnings_Init},
- {"_io", PyInit__io},
- {"_pickle", PyInit__pickle},
- {"atexit", PyInit_atexit},
+ {"_io", PyInit__io},
+ {"_pickle", PyInit__pickle},
+ {"atexit", PyInit_atexit},
- /* Sentinel */
- {0, 0}
+ /* Sentinel */
+ {0, 0}
};
diff --git a/PC/dl_nt.c b/PC/dl_nt.c
index f8af802..ef1ce09 100644
--- a/PC/dl_nt.c
+++ b/PC/dl_nt.c
@@ -3,7 +3,7 @@
Entry point for the Windows NT DLL.
About the only reason for having this, is so initall() can automatically
-be called, removing that burden (and possible source of frustration if
+be called, removing that burden (and possible source of frustration if
forgotten) from the programmer.
*/
@@ -46,61 +46,61 @@ static PFN_RELEASEACTCTX pfnReleaseActCtx = NULL;
void _LoadActCtxPointers()
{
- HINSTANCE hKernel32 = GetModuleHandleW(L"kernel32.dll");
- if (hKernel32)
- pfnGetCurrentActCtx = (PFN_GETCURRENTACTCTX) GetProcAddress(hKernel32, "GetCurrentActCtx");
- // If we can't load GetCurrentActCtx (ie, pre XP) , don't bother with the rest.
- if (pfnGetCurrentActCtx) {
- pfnActivateActCtx = (PFN_ACTIVATEACTCTX) GetProcAddress(hKernel32, "ActivateActCtx");
- pfnDeactivateActCtx = (PFN_DEACTIVATEACTCTX) GetProcAddress(hKernel32, "DeactivateActCtx");
- pfnAddRefActCtx = (PFN_ADDREFACTCTX) GetProcAddress(hKernel32, "AddRefActCtx");
- pfnReleaseActCtx = (PFN_RELEASEACTCTX) GetProcAddress(hKernel32, "ReleaseActCtx");
- }
+ HINSTANCE hKernel32 = GetModuleHandleW(L"kernel32.dll");
+ if (hKernel32)
+ pfnGetCurrentActCtx = (PFN_GETCURRENTACTCTX) GetProcAddress(hKernel32, "GetCurrentActCtx");
+ // If we can't load GetCurrentActCtx (ie, pre XP) , don't bother with the rest.
+ if (pfnGetCurrentActCtx) {
+ pfnActivateActCtx = (PFN_ACTIVATEACTCTX) GetProcAddress(hKernel32, "ActivateActCtx");
+ pfnDeactivateActCtx = (PFN_DEACTIVATEACTCTX) GetProcAddress(hKernel32, "DeactivateActCtx");
+ pfnAddRefActCtx = (PFN_ADDREFACTCTX) GetProcAddress(hKernel32, "AddRefActCtx");
+ pfnReleaseActCtx = (PFN_RELEASEACTCTX) GetProcAddress(hKernel32, "ReleaseActCtx");
+ }
}
ULONG_PTR _Py_ActivateActCtx()
{
- ULONG_PTR ret = 0;
- if (PyWin_DLLhActivationContext && pfnActivateActCtx)
- if (!(*pfnActivateActCtx)(PyWin_DLLhActivationContext, &ret)) {
- OutputDebugString("Python failed to activate the activation context before loading a DLL\n");
- ret = 0; // no promise the failing function didn't change it!
- }
- return ret;
+ ULONG_PTR ret = 0;
+ if (PyWin_DLLhActivationContext && pfnActivateActCtx)
+ if (!(*pfnActivateActCtx)(PyWin_DLLhActivationContext, &ret)) {
+ OutputDebugString("Python failed to activate the activation context before loading a DLL\n");
+ ret = 0; // no promise the failing function didn't change it!
+ }
+ return ret;
}
void _Py_DeactivateActCtx(ULONG_PTR cookie)
{
- if (cookie && pfnDeactivateActCtx)
- if (!(*pfnDeactivateActCtx)(0, cookie))
- OutputDebugString("Python failed to de-activate the activation context\n");
+ if (cookie && pfnDeactivateActCtx)
+ if (!(*pfnDeactivateActCtx)(0, cookie))
+ OutputDebugString("Python failed to de-activate the activation context\n");
}
-BOOL WINAPI DllMain (HANDLE hInst,
- ULONG ul_reason_for_call,
- LPVOID lpReserved)
+BOOL WINAPI DllMain (HANDLE hInst,
+ ULONG ul_reason_for_call,
+ LPVOID lpReserved)
{
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- PyWin_DLLhModule = hInst;
- // 1000 is a magic number I picked out of the air. Could do with a #define, I spose...
- LoadString(hInst, 1000, dllVersionBuffer, sizeof(dllVersionBuffer));
-
- // and capture our activation context for use when loading extensions.
- _LoadActCtxPointers();
- if (pfnGetCurrentActCtx && pfnAddRefActCtx)
- if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext))
- if (!(*pfnAddRefActCtx)(PyWin_DLLhActivationContext))
- OutputDebugString("Python failed to load the default activation context\n");
- break;
-
- case DLL_PROCESS_DETACH:
- if (pfnReleaseActCtx)
- (*pfnReleaseActCtx)(PyWin_DLLhActivationContext);
- break;
- }
- return TRUE;
+ switch (ul_reason_for_call)
+ {
+ case DLL_PROCESS_ATTACH:
+ PyWin_DLLhModule = hInst;
+ // 1000 is a magic number I picked out of the air. Could do with a #define, I spose...
+ LoadString(hInst, 1000, dllVersionBuffer, sizeof(dllVersionBuffer));
+
+ // and capture our activation context for use when loading extensions.
+ _LoadActCtxPointers();
+ if (pfnGetCurrentActCtx && pfnAddRefActCtx)
+ if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext))
+ if (!(*pfnAddRefActCtx)(PyWin_DLLhActivationContext))
+ OutputDebugString("Python failed to load the default activation context\n");
+ break;
+
+ case DLL_PROCESS_DETACH:
+ if (pfnReleaseActCtx)
+ (*pfnReleaseActCtx)(PyWin_DLLhActivationContext);
+ break;
+ }
+ return TRUE;
}
#endif /* Py_ENABLE_SHARED */
diff --git a/PC/errmap.h b/PC/errmap.h
index 59aeea1..d225aa4 100644
--- a/PC/errmap.h
+++ b/PC/errmap.h
@@ -1,78 +1,78 @@
/* Generated file. Do not edit. */
int winerror_to_errno(int winerror)
{
- switch(winerror) {
- case 2: return 2;
- case 3: return 2;
- case 4: return 24;
- case 5: return 13;
- case 6: return 9;
- case 7: return 12;
- case 8: return 12;
- case 9: return 12;
- case 10: return 7;
- case 11: return 8;
- case 15: return 2;
- case 16: return 13;
- case 17: return 18;
- case 18: return 2;
- case 19: return 13;
- case 20: return 13;
- case 21: return 13;
- case 22: return 13;
- case 23: return 13;
- case 24: return 13;
- case 25: return 13;
- case 26: return 13;
- case 27: return 13;
- case 28: return 13;
- case 29: return 13;
- case 30: return 13;
- case 31: return 13;
- case 32: return 13;
- case 33: return 13;
- case 34: return 13;
- case 35: return 13;
- case 36: return 13;
- case 53: return 2;
- case 65: return 13;
- case 67: return 2;
- case 80: return 17;
- case 82: return 13;
- case 83: return 13;
- case 89: return 11;
- case 108: return 13;
- case 109: return 32;
- case 112: return 28;
- case 114: return 9;
- case 128: return 10;
- case 129: return 10;
- case 130: return 9;
- case 132: return 13;
- case 145: return 41;
- case 158: return 13;
- case 161: return 2;
- case 164: return 11;
- case 167: return 13;
- case 183: return 17;
- case 188: return 8;
- case 189: return 8;
- case 190: return 8;
- case 191: return 8;
- case 192: return 8;
- case 193: return 8;
- case 194: return 8;
- case 195: return 8;
- case 196: return 8;
- case 197: return 8;
- case 198: return 8;
- case 199: return 8;
- case 200: return 8;
- case 201: return 8;
- case 202: return 8;
- case 206: return 2;
- case 215: return 11;
- case 1816: return 12;
- default: return EINVAL;
- }
+ switch(winerror) {
+ case 2: return 2;
+ case 3: return 2;
+ case 4: return 24;
+ case 5: return 13;
+ case 6: return 9;
+ case 7: return 12;
+ case 8: return 12;
+ case 9: return 12;
+ case 10: return 7;
+ case 11: return 8;
+ case 15: return 2;
+ case 16: return 13;
+ case 17: return 18;
+ case 18: return 2;
+ case 19: return 13;
+ case 20: return 13;
+ case 21: return 13;
+ case 22: return 13;
+ case 23: return 13;
+ case 24: return 13;
+ case 25: return 13;
+ case 26: return 13;
+ case 27: return 13;
+ case 28: return 13;
+ case 29: return 13;
+ case 30: return 13;
+ case 31: return 13;
+ case 32: return 13;
+ case 33: return 13;
+ case 34: return 13;
+ case 35: return 13;
+ case 36: return 13;
+ case 53: return 2;
+ case 65: return 13;
+ case 67: return 2;
+ case 80: return 17;
+ case 82: return 13;
+ case 83: return 13;
+ case 89: return 11;
+ case 108: return 13;
+ case 109: return 32;
+ case 112: return 28;
+ case 114: return 9;
+ case 128: return 10;
+ case 129: return 10;
+ case 130: return 9;
+ case 132: return 13;
+ case 145: return 41;
+ case 158: return 13;
+ case 161: return 2;
+ case 164: return 11;
+ case 167: return 13;
+ case 183: return 17;
+ case 188: return 8;
+ case 189: return 8;
+ case 190: return 8;
+ case 191: return 8;
+ case 192: return 8;
+ case 193: return 8;
+ case 194: return 8;
+ case 195: return 8;
+ case 196: return 8;
+ case 197: return 8;
+ case 198: return 8;
+ case 199: return 8;
+ case 200: return 8;
+ case 201: return 8;
+ case 202: return 8;
+ case 206: return 2;
+ case 215: return 11;
+ case 1816: return 12;
+ default: return EINVAL;
+ }
}
diff --git a/PC/example_nt/example.c b/PC/example_nt/example.c
index d807311..669d11b 100644
--- a/PC/example_nt/example.c
+++ b/PC/example_nt/example.c
@@ -3,30 +3,30 @@
static PyObject *
ex_foo(PyObject *self, PyObject *args)
{
- printf("Hello, world\n");
- Py_INCREF(Py_None);
- return Py_None;
+ printf("Hello, world\n");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyMethodDef example_methods[] = {
- {"foo", ex_foo, METH_VARARGS, "foo() doc string"},
- {NULL, NULL}
+ {"foo", ex_foo, METH_VARARGS, "foo() doc string"},
+ {NULL, NULL}
};
static struct PyModuleDef examplemodule = {
- PyModuleDef_HEAD_INIT,
- "example",
- "example module doc string",
- -1,
- example_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "example",
+ "example module doc string",
+ -1,
+ example_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_example(void)
{
- return PyModule_Create(&examplemodule);
+ return PyModule_Create(&examplemodule);
}
diff --git a/PC/frozen_dllmain.c b/PC/frozen_dllmain.c
index fbb4713..a8cc885 100644
--- a/PC/frozen_dllmain.c
+++ b/PC/frozen_dllmain.c
@@ -9,7 +9,7 @@ a frozen application, this DLLMain symbol exists multiple times.
The solution is:
* Each module checks for a frozen build, and if so, defines its DLLMain
- function as "__declspec(dllexport) DllMain%module%"
+ function as "__declspec(dllexport) DllMain%module%"
(eg, DllMainpythoncom, or DllMainpywintypes)
* The frozen .EXE/.DLL links against this module, which provides
@@ -47,10 +47,10 @@ changed, here is a snippet from the pythoncom extension module.
#include "windows.h"
static char *possibleModules[] = {
- "pywintypes",
- "pythoncom",
- "win32ui",
- NULL,
+ "pywintypes",
+ "pythoncom",
+ "win32ui",
+ NULL,
};
BOOL CallModuleDllMain(char *modName, DWORD dwReason);
@@ -62,73 +62,73 @@ BOOL CallModuleDllMain(char *modName, DWORD dwReason);
*/
void PyWinFreeze_ExeInit(void)
{
- char **modName;
- for (modName = possibleModules;*modName;*modName++) {
-/* printf("Initialising '%s'\n", *modName); */
- CallModuleDllMain(*modName, DLL_PROCESS_ATTACH);
- }
+ char **modName;
+ for (modName = possibleModules;*modName;*modName++) {
+/* printf("Initialising '%s'\n", *modName); */
+ CallModuleDllMain(*modName, DLL_PROCESS_ATTACH);
+ }
}
/*
Called by a frozen .EXE only, so that built-in extension
- modules are cleaned up
+ modules are cleaned up
*/
void PyWinFreeze_ExeTerm(void)
{
- // Must go backwards
- char **modName;
- for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
- modName >= possibleModules;
- *modName--) {
-/* printf("Terminating '%s'\n", *modName);*/
- CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
- }
+ // Must go backwards
+ char **modName;
+ for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
+ modName >= possibleModules;
+ *modName--) {
+/* printf("Terminating '%s'\n", *modName);*/
+ CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
+ }
}
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
- BOOL ret = TRUE;
- switch (dwReason) {
- case DLL_PROCESS_ATTACH:
- {
- char **modName;
- for (modName = possibleModules;*modName;*modName++) {
- BOOL ok = CallModuleDllMain(*modName, dwReason);
- if (!ok)
- ret = FALSE;
- }
- break;
- }
- case DLL_PROCESS_DETACH:
- {
- // Must go backwards
- char **modName;
- for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
- modName >= possibleModules;
- *modName--)
- CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
- break;
- }
- }
- return ret;
+ BOOL ret = TRUE;
+ switch (dwReason) {
+ case DLL_PROCESS_ATTACH:
+ {
+ char **modName;
+ for (modName = possibleModules;*modName;*modName++) {
+ BOOL ok = CallModuleDllMain(*modName, dwReason);
+ if (!ok)
+ ret = FALSE;
+ }
+ break;
+ }
+ case DLL_PROCESS_DETACH:
+ {
+ // Must go backwards
+ char **modName;
+ for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
+ modName >= possibleModules;
+ *modName--)
+ CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
+ break;
+ }
+ }
+ return ret;
}
BOOL CallModuleDllMain(char *modName, DWORD dwReason)
{
- BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
-
- char funcName[255];
- HMODULE hmod = GetModuleHandle(NULL);
- strcpy(funcName, "_DllMain");
- strcat(funcName, modName);
- strcat(funcName, "@12"); // stdcall convention.
- pfndllmain = (BOOL (WINAPI *)(HINSTANCE, DWORD, LPVOID))GetProcAddress(hmod, funcName);
- if (pfndllmain==NULL) {
- /* No function by that name exported - then that module does
- not appear in our frozen program - return OK
- */
- return TRUE;
- }
- return (*pfndllmain)(hmod, dwReason, NULL);
+ BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
+
+ char funcName[255];
+ HMODULE hmod = GetModuleHandle(NULL);
+ strcpy(funcName, "_DllMain");
+ strcat(funcName, modName);
+ strcat(funcName, "@12"); // stdcall convention.
+ pfndllmain = (BOOL (WINAPI *)(HINSTANCE, DWORD, LPVOID))GetProcAddress(hmod, funcName);
+ if (pfndllmain==NULL) {
+ /* No function by that name exported - then that module does
+ not appear in our frozen program - return OK
+ */
+ return TRUE;
+ }
+ return (*pfndllmain)(hmod, dwReason, NULL);
}
diff --git a/PC/generrmap.c b/PC/generrmap.c
index 2b25063..bf1081b 100644
--- a/PC/generrmap.c
+++ b/PC/generrmap.c
@@ -5,16 +5,16 @@
int main()
{
- int i;
- printf("/* Generated file. Do not edit. */\n");
- printf("int winerror_to_errno(int winerror)\n");
- printf("{\n\tswitch(winerror) {\n");
- for(i=1; i < 65000; i++) {
- _dosmaperr(i);
- if (errno == EINVAL)
- continue;
- printf("\t\tcase %d: return %d;\n", i, errno);
- }
- printf("\t\tdefault: return EINVAL;\n");
- printf("\t}\n}\n");
+ int i;
+ printf("/* Generated file. Do not edit. */\n");
+ printf("int winerror_to_errno(int winerror)\n");
+ printf("{\n\tswitch(winerror) {\n");
+ for(i=1; i < 65000; i++) {
+ _dosmaperr(i);
+ if (errno == EINVAL)
+ continue;
+ printf("\t\tcase %d: return %d;\n", i, errno);
+ }
+ printf("\t\tdefault: return EINVAL;\n");
+ printf("\t}\n}\n");
}
diff --git a/PC/getpathp.c b/PC/getpathp.c
index 212f942..0fe04c7 100644
--- a/PC/getpathp.c
+++ b/PC/getpathp.c
@@ -4,8 +4,8 @@
/* ----------------------------------------------------------------
PATH RULES FOR WINDOWS:
- This describes how sys.path is formed on Windows. It describes the
- functionality, not the implementation (ie, the order in which these
+ This describes how sys.path is formed on Windows. It describes the
+ functionality, not the implementation (ie, the order in which these
are actually fetched is different)
* Python always adds an empty entry at the start, which corresponds
@@ -24,15 +24,15 @@
* We attempt to locate the "Python Home" - if the PYTHONHOME env var
is set, we believe it. Otherwise, we use the path of our host .EXE's
to try and locate our "landmark" (lib\\os.py) and deduce our home.
- - If we DO have a Python Home: The relevant sub-directories (Lib,
+ - If we DO have a Python Home: The relevant sub-directories (Lib,
plat-win, etc) are based on the Python Home
- If we DO NOT have a Python Home, the core Python Path is
- loaded from the registry. This is the main PythonPath key,
+ loaded from the registry. This is the main PythonPath key,
and both HKLM and HKCU are combined to form the path)
* Iff - we can not locate the Python Home, have not had a PYTHONPATH
specified, and can't locate any Registry entries (ie, we have _nothing_
- we can assume is a good path), a default path with relative entries is
+ we can assume is a good path), a default path with relative entries is
used (eg. .\Lib;.\plat-win, etc)
@@ -42,9 +42,9 @@
the core path is deduced, and the core paths in the registry are
ignored. Other "application paths" in the registry are always read.
- * When Python is hosted in another exe (different directory, embedded via
+ * When Python is hosted in another exe (different directory, embedded via
COM, etc), the Python Home will not be deduced, so the core path from
- the registry is used. Other "application paths" in the registry are
+ the registry is used. Other "application paths" in the registry are
always read.
* If Python can't find its home and there is no registry (eg, frozen
@@ -92,12 +92,12 @@ static wchar_t *module_search_path = NULL;
static int
-is_sep(wchar_t ch) /* determine if "ch" is a separator character */
+is_sep(wchar_t ch) /* determine if "ch" is a separator character */
{
#ifdef ALTSEP
- return ch == SEP || ch == ALTSEP;
+ return ch == SEP || ch == ALTSEP;
#else
- return ch == SEP;
+ return ch == SEP;
#endif
}
@@ -107,35 +107,35 @@ is_sep(wchar_t ch) /* determine if "ch" is a separator character */
static void
reduce(wchar_t *dir)
{
- size_t i = wcslen(dir);
- while (i > 0 && !is_sep(dir[i]))
- --i;
- dir[i] = '\0';
+ size_t i = wcslen(dir);
+ while (i > 0 && !is_sep(dir[i]))
+ --i;
+ dir[i] = '\0';
}
-
+
static int
exists(wchar_t *filename)
{
- return GetFileAttributesW(filename) != 0xFFFFFFFF;
+ return GetFileAttributesW(filename) != 0xFFFFFFFF;
}
-/* Assumes 'filename' MAXPATHLEN+1 bytes long -
+/* Assumes 'filename' MAXPATHLEN+1 bytes long -
may extend 'filename' by one character.
*/
static int
-ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
+ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
{
- if (exists(filename))
- return 1;
-
- /* Check for the compiled version of prefix. */
- if (wcslen(filename) < MAXPATHLEN) {
- wcscat(filename, Py_OptimizeFlag ? L"o" : L"c");
- if (exists(filename))
- return 1;
- }
- return 0;
+ if (exists(filename))
+ return 1;
+
+ /* Check for the compiled version of prefix. */
+ if (wcslen(filename) < MAXPATHLEN) {
+ wcscat(filename, Py_OptimizeFlag ? L"o" : L"c");
+ if (exists(filename))
+ return 1;
+ }
+ return 0;
}
/* Add a path component, by appending stuff to buffer.
@@ -150,21 +150,21 @@ ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
static void
join(wchar_t *buffer, wchar_t *stuff)
{
- size_t n, k;
- if (is_sep(stuff[0]))
- n = 0;
- else {
- n = wcslen(buffer);
- if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
- buffer[n++] = SEP;
- }
- if (n > MAXPATHLEN)
- Py_FatalError("buffer overflow in getpathp.c's joinpath()");
- k = wcslen(stuff);
- if (n + k > MAXPATHLEN)
- k = MAXPATHLEN - n;
- wcsncpy(buffer+n, stuff, k);
- buffer[n+k] = '\0';
+ size_t n, k;
+ if (is_sep(stuff[0]))
+ n = 0;
+ else {
+ n = wcslen(buffer);
+ if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
+ buffer[n++] = SEP;
+ }
+ if (n > MAXPATHLEN)
+ Py_FatalError("buffer overflow in getpathp.c's joinpath()");
+ k = wcslen(stuff);
+ if (n + k > MAXPATHLEN)
+ k = MAXPATHLEN - n;
+ wcsncpy(buffer+n, stuff, k);
+ buffer[n+k] = '\0';
}
/* gotlandmark only called by search_for_prefix, which ensures
@@ -174,29 +174,29 @@ join(wchar_t *buffer, wchar_t *stuff)
static int
gotlandmark(wchar_t *landmark)
{
- int ok;
- Py_ssize_t n;
-
- n = wcslen(prefix);
- join(prefix, landmark);
- ok = ismodule(prefix);
- prefix[n] = '\0';
- return ok;
+ int ok;
+ Py_ssize_t n;
+
+ n = wcslen(prefix);
+ join(prefix, landmark);
+ ok = ismodule(prefix);
+ prefix[n] = '\0';
+ return ok;
}
-/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
+/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
assumption provided by only caller, calculate_path() */
static int
search_for_prefix(wchar_t *argv0_path, wchar_t *landmark)
{
- /* Search from argv0_path, until landmark is found */
- wcscpy(prefix, argv0_path);
- do {
- if (gotlandmark(landmark))
- return 1;
- reduce(prefix);
- } while (prefix[0]);
- return 0;
+ /* Search from argv0_path, until landmark is found */
+ wcscpy(prefix, argv0_path);
+ do {
+ if (gotlandmark(landmark))
+ return 1;
+ reduce(prefix);
+ } while (prefix[0]);
+ return 0;
}
#ifdef MS_WINDOWS
@@ -222,133 +222,133 @@ extern const char *PyWin_DLLVersionString;
static wchar_t *
getpythonregpath(HKEY keyBase, int skipcore)
{
- HKEY newKey = 0;
- DWORD dataSize = 0;
- DWORD numKeys = 0;
- LONG rc;
- wchar_t *retval = NULL;
- WCHAR *dataBuf = NULL;
- static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
- static const WCHAR keySuffix[] = L"\\PythonPath";
- size_t versionLen;
- DWORD index;
- WCHAR *keyBuf = NULL;
- WCHAR *keyBufPtr;
- WCHAR **ppPaths = NULL;
-
- /* Tried to use sysget("winver") but here is too early :-( */
- versionLen = strlen(PyWin_DLLVersionString);
- /* Space for all the chars, plus one \0 */
- keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
- sizeof(WCHAR)*(versionLen-1) +
- sizeof(keySuffix));
- if (keyBuf==NULL) goto done;
-
- memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
- keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1;
- mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
- keyBufPtr += versionLen;
- /* NULL comes with this one! */
- memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
- /* Open the root Python key */
- rc=RegOpenKeyExW(keyBase,
- keyBuf, /* subkey */
- 0, /* reserved */
- KEY_READ,
- &newKey);
- if (rc!=ERROR_SUCCESS) goto done;
- /* Find out how big our core buffer is, and how many subkeys we have */
- rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
- NULL, NULL, &dataSize, NULL, NULL);
- if (rc!=ERROR_SUCCESS) goto done;
- if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
- /* Allocate a temp array of char buffers, so we only need to loop
- reading the registry once
- */
- ppPaths = malloc( sizeof(WCHAR *) * numKeys );
- if (ppPaths==NULL) goto done;
- memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
- /* Loop over all subkeys, allocating a temp sub-buffer. */
- for(index=0;index<numKeys;index++) {
- WCHAR keyBuf[MAX_PATH+1];
- HKEY subKey = 0;
- DWORD reqdSize = MAX_PATH+1;
- /* Get the sub-key name */
- DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
- NULL, NULL, NULL, NULL );
- if (rc!=ERROR_SUCCESS) goto done;
- /* Open the sub-key */
- rc=RegOpenKeyExW(newKey,
- keyBuf, /* subkey */
- 0, /* reserved */
- KEY_READ,
- &subKey);
- if (rc!=ERROR_SUCCESS) goto done;
- /* Find the value of the buffer size, malloc, then read it */
- RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
- if (reqdSize) {
- ppPaths[index] = malloc(reqdSize);
- if (ppPaths[index]) {
- RegQueryValueExW(subKey, NULL, 0, NULL,
- (LPBYTE)ppPaths[index],
- &reqdSize);
- dataSize += reqdSize + 1; /* 1 for the ";" */
- }
- }
- RegCloseKey(subKey);
- }
-
- /* return null if no path to return */
- if (dataSize == 0) goto done;
-
- /* original datasize from RegQueryInfo doesn't include the \0 */
- dataBuf = malloc((dataSize+1) * sizeof(WCHAR));
- if (dataBuf) {
- WCHAR *szCur = dataBuf;
- DWORD reqdSize = dataSize;
- /* Copy our collected strings */
- for (index=0;index<numKeys;index++) {
- if (index > 0) {
- *(szCur++) = L';';
- dataSize--;
- }
- if (ppPaths[index]) {
- Py_ssize_t len = wcslen(ppPaths[index]);
- wcsncpy(szCur, ppPaths[index], len);
- szCur += len;
- assert(dataSize > (DWORD)len);
- dataSize -= (DWORD)len;
- }
- }
- if (skipcore)
- *szCur = '\0';
- else {
- /* If we have no values, we dont need a ';' */
- if (numKeys) {
- *(szCur++) = L';';
- dataSize--;
- }
- /* Now append the core path entries -
- this will include the NULL
- */
- rc = RegQueryValueExW(newKey, NULL, 0, NULL,
- (LPBYTE)szCur, &dataSize);
- }
- /* And set the result - caller must free */
- retval = dataBuf;
- }
+ HKEY newKey = 0;
+ DWORD dataSize = 0;
+ DWORD numKeys = 0;
+ LONG rc;
+ wchar_t *retval = NULL;
+ WCHAR *dataBuf = NULL;
+ static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
+ static const WCHAR keySuffix[] = L"\\PythonPath";
+ size_t versionLen;
+ DWORD index;
+ WCHAR *keyBuf = NULL;
+ WCHAR *keyBufPtr;
+ WCHAR **ppPaths = NULL;
+
+ /* Tried to use sysget("winver") but here is too early :-( */
+ versionLen = strlen(PyWin_DLLVersionString);
+ /* Space for all the chars, plus one \0 */
+ keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
+ sizeof(WCHAR)*(versionLen-1) +
+ sizeof(keySuffix));
+ if (keyBuf==NULL) goto done;
+
+ memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
+ keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1;
+ mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
+ keyBufPtr += versionLen;
+ /* NULL comes with this one! */
+ memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
+ /* Open the root Python key */
+ rc=RegOpenKeyExW(keyBase,
+ keyBuf, /* subkey */
+ 0, /* reserved */
+ KEY_READ,
+ &newKey);
+ if (rc!=ERROR_SUCCESS) goto done;
+ /* Find out how big our core buffer is, and how many subkeys we have */
+ rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
+ NULL, NULL, &dataSize, NULL, NULL);
+ if (rc!=ERROR_SUCCESS) goto done;
+ if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
+ /* Allocate a temp array of char buffers, so we only need to loop
+ reading the registry once
+ */
+ ppPaths = malloc( sizeof(WCHAR *) * numKeys );
+ if (ppPaths==NULL) goto done;
+ memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
+ /* Loop over all subkeys, allocating a temp sub-buffer. */
+ for(index=0;index<numKeys;index++) {
+ WCHAR keyBuf[MAX_PATH+1];
+ HKEY subKey = 0;
+ DWORD reqdSize = MAX_PATH+1;
+ /* Get the sub-key name */
+ DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
+ NULL, NULL, NULL, NULL );
+ if (rc!=ERROR_SUCCESS) goto done;
+ /* Open the sub-key */
+ rc=RegOpenKeyExW(newKey,
+ keyBuf, /* subkey */
+ 0, /* reserved */
+ KEY_READ,
+ &subKey);
+ if (rc!=ERROR_SUCCESS) goto done;
+ /* Find the value of the buffer size, malloc, then read it */
+ RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
+ if (reqdSize) {
+ ppPaths[index] = malloc(reqdSize);
+ if (ppPaths[index]) {
+ RegQueryValueExW(subKey, NULL, 0, NULL,
+ (LPBYTE)ppPaths[index],
+ &reqdSize);
+ dataSize += reqdSize + 1; /* 1 for the ";" */
+ }
+ }
+ RegCloseKey(subKey);
+ }
+
+ /* return null if no path to return */
+ if (dataSize == 0) goto done;
+
+ /* original datasize from RegQueryInfo doesn't include the \0 */
+ dataBuf = malloc((dataSize+1) * sizeof(WCHAR));
+ if (dataBuf) {
+ WCHAR *szCur = dataBuf;
+ DWORD reqdSize = dataSize;
+ /* Copy our collected strings */
+ for (index=0;index<numKeys;index++) {
+ if (index > 0) {
+ *(szCur++) = L';';
+ dataSize--;
+ }
+ if (ppPaths[index]) {
+ Py_ssize_t len = wcslen(ppPaths[index]);
+ wcsncpy(szCur, ppPaths[index], len);
+ szCur += len;
+ assert(dataSize > (DWORD)len);
+ dataSize -= (DWORD)len;
+ }
+ }
+ if (skipcore)
+ *szCur = '\0';
+ else {
+ /* If we have no values, we dont need a ';' */
+ if (numKeys) {
+ *(szCur++) = L';';
+ dataSize--;
+ }
+ /* Now append the core path entries -
+ this will include the NULL
+ */
+ rc = RegQueryValueExW(newKey, NULL, 0, NULL,
+ (LPBYTE)szCur, &dataSize);
+ }
+ /* And set the result - caller must free */
+ retval = dataBuf;
+ }
done:
- /* Loop freeing my temp buffers */
- if (ppPaths) {
- for(index=0;index<numKeys;index++)
- if (ppPaths[index]) free(ppPaths[index]);
- free(ppPaths);
- }
- if (newKey)
- RegCloseKey(newKey);
- if (keyBuf)
- free(keyBuf);
- return retval;
+ /* Loop freeing my temp buffers */
+ if (ppPaths) {
+ for(index=0;index<numKeys;index++)
+ if (ppPaths[index]) free(ppPaths[index]);
+ free(ppPaths);
+ }
+ if (newKey)
+ RegCloseKey(newKey);
+ if (keyBuf)
+ free(keyBuf);
+ return retval;
}
#endif /* Py_ENABLE_SHARED */
#endif /* MS_WINDOWS */
@@ -356,299 +356,299 @@ done:
static void
get_progpath(void)
{
- extern wchar_t *Py_GetProgramName(void);
- wchar_t *path = _wgetenv(L"PATH");
- wchar_t *prog = Py_GetProgramName();
+ extern wchar_t *Py_GetProgramName(void);
+ wchar_t *path = _wgetenv(L"PATH");
+ wchar_t *prog = Py_GetProgramName();
#ifdef MS_WINDOWS
#ifdef Py_ENABLE_SHARED
- extern HANDLE PyWin_DLLhModule;
- /* static init of progpath ensures final char remains \0 */
- if (PyWin_DLLhModule)
- if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
- dllpath[0] = 0;
+ extern HANDLE PyWin_DLLhModule;
+ /* static init of progpath ensures final char remains \0 */
+ if (PyWin_DLLhModule)
+ if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
+ dllpath[0] = 0;
#else
- dllpath[0] = 0;
+ dllpath[0] = 0;
#endif
- if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
- return;
+ if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
+ return;
#endif
- if (prog == NULL || *prog == '\0')
- prog = L"python";
-
- /* If there is no slash in the argv0 path, then we have to
- * assume python is on the user's $PATH, since there's no
- * other way to find a directory to start the search from. If
- * $PATH isn't exported, you lose.
- */
+ if (prog == NULL || *prog == '\0')
+ prog = L"python";
+
+ /* If there is no slash in the argv0 path, then we have to
+ * assume python is on the user's $PATH, since there's no
+ * other way to find a directory to start the search from. If
+ * $PATH isn't exported, you lose.
+ */
#ifdef ALTSEP
- if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
+ if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
#else
- if (wcschr(prog, SEP))
+ if (wcschr(prog, SEP))
#endif
- wcsncpy(progpath, prog, MAXPATHLEN);
- else if (path) {
- while (1) {
- wchar_t *delim = wcschr(path, DELIM);
-
- if (delim) {
- size_t len = delim - path;
- /* ensure we can't overwrite buffer */
- len = min(MAXPATHLEN,len);
- wcsncpy(progpath, path, len);
- *(progpath + len) = '\0';
- }
- else
- wcsncpy(progpath, path, MAXPATHLEN);
-
- /* join() is safe for MAXPATHLEN+1 size buffer */
- join(progpath, prog);
- if (exists(progpath))
- break;
-
- if (!delim) {
- progpath[0] = '\0';
- break;
- }
- path = delim + 1;
- }
- }
- else
- progpath[0] = '\0';
+ wcsncpy(progpath, prog, MAXPATHLEN);
+ else if (path) {
+ while (1) {
+ wchar_t *delim = wcschr(path, DELIM);
+
+ if (delim) {
+ size_t len = delim - path;
+ /* ensure we can't overwrite buffer */
+ len = min(MAXPATHLEN,len);
+ wcsncpy(progpath, path, len);
+ *(progpath + len) = '\0';
+ }
+ else
+ wcsncpy(progpath, path, MAXPATHLEN);
+
+ /* join() is safe for MAXPATHLEN+1 size buffer */
+ join(progpath, prog);
+ if (exists(progpath))
+ break;
+
+ if (!delim) {
+ progpath[0] = '\0';
+ break;
+ }
+ path = delim + 1;
+ }
+ }
+ else
+ progpath[0] = '\0';
}
static void
calculate_path(void)
{
- wchar_t argv0_path[MAXPATHLEN+1];
- wchar_t *buf;
- size_t bufsz;
- wchar_t *pythonhome = Py_GetPythonHome();
- wchar_t *envpath = NULL;
+ wchar_t argv0_path[MAXPATHLEN+1];
+ wchar_t *buf;
+ size_t bufsz;
+ wchar_t *pythonhome = Py_GetPythonHome();
+ wchar_t *envpath = NULL;
#ifdef MS_WINDOWS
- int skiphome, skipdefault;
- wchar_t *machinepath = NULL;
- wchar_t *userpath = NULL;
- wchar_t zip_path[MAXPATHLEN+1];
- size_t len;
-
- if (!Py_IgnoreEnvironmentFlag) {
- envpath = _wgetenv(L"PYTHONPATH");
- }
+ int skiphome, skipdefault;
+ wchar_t *machinepath = NULL;
+ wchar_t *userpath = NULL;
+ wchar_t zip_path[MAXPATHLEN+1];
+ size_t len;
+
+ if (!Py_IgnoreEnvironmentFlag) {
+ envpath = _wgetenv(L"PYTHONPATH");
+ }
#else
- char *_envpath = Py_GETENV("PYTHONPATH");
- wchar_t wenvpath[MAXPATHLEN+1];
- if (_envpath) {
- size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
- envpath = wenvpath;
- if (r == (size_t)-1 || r >= MAXPATHLEN)
- envpath = NULL;
- }
+ char *_envpath = Py_GETENV("PYTHONPATH");
+ wchar_t wenvpath[MAXPATHLEN+1];
+ if (_envpath) {
+ size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
+ envpath = wenvpath;
+ if (r == (size_t)-1 || r >= MAXPATHLEN)
+ envpath = NULL;
+ }
#endif
- get_progpath();
- /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
- wcscpy(argv0_path, progpath);
- reduce(argv0_path);
- if (pythonhome == NULL || *pythonhome == '\0') {
- if (search_for_prefix(argv0_path, LANDMARK))
- pythonhome = prefix;
- else
- pythonhome = NULL;
- }
- else
- wcsncpy(prefix, pythonhome, MAXPATHLEN);
+ get_progpath();
+ /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
+ wcscpy(argv0_path, progpath);
+ reduce(argv0_path);
+ if (pythonhome == NULL || *pythonhome == '\0') {
+ if (search_for_prefix(argv0_path, LANDMARK))
+ pythonhome = prefix;
+ else
+ pythonhome = NULL;
+ }
+ else
+ wcsncpy(prefix, pythonhome, MAXPATHLEN);
- if (envpath && *envpath == '\0')
- envpath = NULL;
+ if (envpath && *envpath == '\0')
+ envpath = NULL;
#ifdef MS_WINDOWS
- /* Calculate zip archive path */
- if (dllpath[0]) /* use name of python DLL */
- wcsncpy(zip_path, dllpath, MAXPATHLEN);
- else /* use name of executable program */
- wcsncpy(zip_path, progpath, MAXPATHLEN);
- zip_path[MAXPATHLEN] = '\0';
- len = wcslen(zip_path);
- if (len > 4) {
- zip_path[len-3] = 'z'; /* change ending to "zip" */
- zip_path[len-2] = 'i';
- zip_path[len-1] = 'p';
- }
- else {
- zip_path[0] = 0;
- }
-
- skiphome = pythonhome==NULL ? 0 : 1;
+ /* Calculate zip archive path */
+ if (dllpath[0]) /* use name of python DLL */
+ wcsncpy(zip_path, dllpath, MAXPATHLEN);
+ else /* use name of executable program */
+ wcsncpy(zip_path, progpath, MAXPATHLEN);
+ zip_path[MAXPATHLEN] = '\0';
+ len = wcslen(zip_path);
+ if (len > 4) {
+ zip_path[len-3] = 'z'; /* change ending to "zip" */
+ zip_path[len-2] = 'i';
+ zip_path[len-1] = 'p';
+ }
+ else {
+ zip_path[0] = 0;
+ }
+
+ skiphome = pythonhome==NULL ? 0 : 1;
#ifdef Py_ENABLE_SHARED
- machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
- userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
+ machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
+ userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
#endif
- /* We only use the default relative PYTHONPATH if we havent
- anything better to use! */
- skipdefault = envpath!=NULL || pythonhome!=NULL || \
- machinepath!=NULL || userpath!=NULL;
+ /* We only use the default relative PYTHONPATH if we havent
+ anything better to use! */
+ skipdefault = envpath!=NULL || pythonhome!=NULL || \
+ machinepath!=NULL || userpath!=NULL;
#endif
- /* We need to construct a path from the following parts.
- (1) the PYTHONPATH environment variable, if set;
- (2) for Win32, the zip archive file path;
- (3) for Win32, the machinepath and userpath, if set;
- (4) the PYTHONPATH config macro, with the leading "."
- of each component replaced with pythonhome, if set;
- (5) the directory containing the executable (argv0_path).
- The length calculation calculates #4 first.
- Extra rules:
- - If PYTHONHOME is set (in any way) item (3) is ignored.
- - If registry values are used, (4) and (5) are ignored.
- */
-
- /* Calculate size of return buffer */
- if (pythonhome != NULL) {
- wchar_t *p;
- bufsz = 1;
- for (p = PYTHONPATH; *p; p++) {
- if (*p == DELIM)
- bufsz++; /* number of DELIM plus one */
- }
- bufsz *= wcslen(pythonhome);
- }
- else
- bufsz = 0;
- bufsz += wcslen(PYTHONPATH) + 1;
- bufsz += wcslen(argv0_path) + 1;
+ /* We need to construct a path from the following parts.
+ (1) the PYTHONPATH environment variable, if set;
+ (2) for Win32, the zip archive file path;
+ (3) for Win32, the machinepath and userpath, if set;
+ (4) the PYTHONPATH config macro, with the leading "."
+ of each component replaced with pythonhome, if set;
+ (5) the directory containing the executable (argv0_path).
+ The length calculation calculates #4 first.
+ Extra rules:
+ - If PYTHONHOME is set (in any way) item (3) is ignored.
+ - If registry values are used, (4) and (5) are ignored.
+ */
+
+ /* Calculate size of return buffer */
+ if (pythonhome != NULL) {
+ wchar_t *p;
+ bufsz = 1;
+ for (p = PYTHONPATH; *p; p++) {
+ if (*p == DELIM)
+ bufsz++; /* number of DELIM plus one */
+ }
+ bufsz *= wcslen(pythonhome);
+ }
+ else
+ bufsz = 0;
+ bufsz += wcslen(PYTHONPATH) + 1;
+ bufsz += wcslen(argv0_path) + 1;
#ifdef MS_WINDOWS
- if (userpath)
- bufsz += wcslen(userpath) + 1;
- if (machinepath)
- bufsz += wcslen(machinepath) + 1;
- bufsz += wcslen(zip_path) + 1;
+ if (userpath)
+ bufsz += wcslen(userpath) + 1;
+ if (machinepath)
+ bufsz += wcslen(machinepath) + 1;
+ bufsz += wcslen(zip_path) + 1;
#endif
- if (envpath != NULL)
- bufsz += wcslen(envpath) + 1;
-
- module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
- if (buf == NULL) {
- /* We can't exit, so print a warning and limp along */
- fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
- if (envpath) {
- fprintf(stderr, "Using environment $PYTHONPATH.\n");
- module_search_path = envpath;
- }
- else {
- fprintf(stderr, "Using default static path.\n");
- module_search_path = PYTHONPATH;
- }
+ if (envpath != NULL)
+ bufsz += wcslen(envpath) + 1;
+
+ module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
+ if (buf == NULL) {
+ /* We can't exit, so print a warning and limp along */
+ fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
+ if (envpath) {
+ fprintf(stderr, "Using environment $PYTHONPATH.\n");
+ module_search_path = envpath;
+ }
+ else {
+ fprintf(stderr, "Using default static path.\n");
+ module_search_path = PYTHONPATH;
+ }
#ifdef MS_WINDOWS
- if (machinepath)
- free(machinepath);
- if (userpath)
- free(userpath);
+ if (machinepath)
+ free(machinepath);
+ if (userpath)
+ free(userpath);
#endif /* MS_WINDOWS */
- return;
- }
-
- if (envpath) {
- wcscpy(buf, envpath);
- buf = wcschr(buf, L'\0');
- *buf++ = DELIM;
- }
+ return;
+ }
+
+ if (envpath) {
+ wcscpy(buf, envpath);
+ buf = wcschr(buf, L'\0');
+ *buf++ = DELIM;
+ }
#ifdef MS_WINDOWS
- if (zip_path[0]) {
- wcscpy(buf, zip_path);
- buf = wcschr(buf, L'\0');
- *buf++ = DELIM;
- }
- if (userpath) {
- wcscpy(buf, userpath);
- buf = wcschr(buf, L'\0');
- *buf++ = DELIM;
- free(userpath);
- }
- if (machinepath) {
- wcscpy(buf, machinepath);
- buf = wcschr(buf, L'\0');
- *buf++ = DELIM;
- free(machinepath);
- }
- if (pythonhome == NULL) {
- if (!skipdefault) {
- wcscpy(buf, PYTHONPATH);
- buf = wcschr(buf, L'\0');
- }
- }
+ if (zip_path[0]) {
+ wcscpy(buf, zip_path);
+ buf = wcschr(buf, L'\0');
+ *buf++ = DELIM;
+ }
+ if (userpath) {
+ wcscpy(buf, userpath);
+ buf = wcschr(buf, L'\0');
+ *buf++ = DELIM;
+ free(userpath);
+ }
+ if (machinepath) {
+ wcscpy(buf, machinepath);
+ buf = wcschr(buf, L'\0');
+ *buf++ = DELIM;
+ free(machinepath);
+ }
+ if (pythonhome == NULL) {
+ if (!skipdefault) {
+ wcscpy(buf, PYTHONPATH);
+ buf = wcschr(buf, L'\0');
+ }
+ }
#else
- if (pythonhome == NULL) {
- wcscpy(buf, PYTHONPATH);
- buf = wcschr(buf, L'\0');
- }
+ if (pythonhome == NULL) {
+ wcscpy(buf, PYTHONPATH);
+ buf = wcschr(buf, L'\0');
+ }
#endif /* MS_WINDOWS */
- else {
- wchar_t *p = PYTHONPATH;
- wchar_t *q;
- size_t n;
- for (;;) {
- q = wcschr(p, DELIM);
- if (q == NULL)
- n = wcslen(p);
- else
- n = q-p;
- if (p[0] == '.' && is_sep(p[1])) {
- wcscpy(buf, pythonhome);
- buf = wcschr(buf, L'\0');
- p++;
- n--;
- }
- wcsncpy(buf, p, n);
- buf += n;
- if (q == NULL)
- break;
- *buf++ = DELIM;
- p = q+1;
- }
- }
- if (argv0_path) {
- *buf++ = DELIM;
- wcscpy(buf, argv0_path);
- buf = wcschr(buf, L'\0');
- }
- *buf = L'\0';
- /* Now to pull one last hack/trick. If sys.prefix is
- empty, then try and find it somewhere on the paths
- we calculated. We scan backwards, as our general policy
- is that Python core directories are at the *end* of
- sys.path. We assume that our "lib" directory is
- on the path, and that our 'prefix' directory is
- the parent of that.
- */
- if (*prefix==L'\0') {
- wchar_t lookBuf[MAXPATHLEN+1];
- wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
- while (1) {
- Py_ssize_t nchars;
- wchar_t *lookEnd = look;
- /* 'look' will end up one character before the
- start of the path in question - even if this
- is one character before the start of the buffer
- */
- while (look >= module_search_path && *look != DELIM)
- look--;
- nchars = lookEnd-look;
- wcsncpy(lookBuf, look+1, nchars);
- lookBuf[nchars] = L'\0';
- /* Up one level to the parent */
- reduce(lookBuf);
- if (search_for_prefix(lookBuf, LANDMARK)) {
- break;
- }
- /* If we are out of paths to search - give up */
- if (look < module_search_path)
- break;
- look--;
- }
- }
+ else {
+ wchar_t *p = PYTHONPATH;
+ wchar_t *q;
+ size_t n;
+ for (;;) {
+ q = wcschr(p, DELIM);
+ if (q == NULL)
+ n = wcslen(p);
+ else
+ n = q-p;
+ if (p[0] == '.' && is_sep(p[1])) {
+ wcscpy(buf, pythonhome);
+ buf = wcschr(buf, L'\0');
+ p++;
+ n--;
+ }
+ wcsncpy(buf, p, n);
+ buf += n;
+ if (q == NULL)
+ break;
+ *buf++ = DELIM;
+ p = q+1;
+ }
+ }
+ if (argv0_path) {
+ *buf++ = DELIM;
+ wcscpy(buf, argv0_path);
+ buf = wcschr(buf, L'\0');
+ }
+ *buf = L'\0';
+ /* Now to pull one last hack/trick. If sys.prefix is
+ empty, then try and find it somewhere on the paths
+ we calculated. We scan backwards, as our general policy
+ is that Python core directories are at the *end* of
+ sys.path. We assume that our "lib" directory is
+ on the path, and that our 'prefix' directory is
+ the parent of that.
+ */
+ if (*prefix==L'\0') {
+ wchar_t lookBuf[MAXPATHLEN+1];
+ wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
+ while (1) {
+ Py_ssize_t nchars;
+ wchar_t *lookEnd = look;
+ /* 'look' will end up one character before the
+ start of the path in question - even if this
+ is one character before the start of the buffer
+ */
+ while (look >= module_search_path && *look != DELIM)
+ look--;
+ nchars = lookEnd-look;
+ wcsncpy(lookBuf, look+1, nchars);
+ lookBuf[nchars] = L'\0';
+ /* Up one level to the parent */
+ reduce(lookBuf);
+ if (search_for_prefix(lookBuf, LANDMARK)) {
+ break;
+ }
+ /* If we are out of paths to search - give up */
+ if (look < module_search_path)
+ break;
+ look--;
+ }
+ }
}
@@ -657,29 +657,29 @@ calculate_path(void)
wchar_t *
Py_GetPath(void)
{
- if (!module_search_path)
- calculate_path();
- return module_search_path;
+ if (!module_search_path)
+ calculate_path();
+ return module_search_path;
}
wchar_t *
Py_GetPrefix(void)
{
- if (!module_search_path)
- calculate_path();
- return prefix;
+ if (!module_search_path)
+ calculate_path();
+ return prefix;
}
wchar_t *
Py_GetExecPrefix(void)
{
- return Py_GetPrefix();
+ return Py_GetPrefix();
}
wchar_t *
Py_GetProgramFullPath(void)
{
- if (!module_search_path)
- calculate_path();
- return progpath;
+ if (!module_search_path)
+ calculate_path();
+ return progpath;
}
diff --git a/PC/import_nt.c b/PC/import_nt.c
index e7d152a..196a774 100644
--- a/PC/import_nt.c
+++ b/PC/import_nt.c
@@ -1,6 +1,6 @@
/********************************************************************
- import_nt.c
+ import_nt.c
Win32 specific import code.
@@ -16,71 +16,71 @@
extern const char *PyWin_DLLVersionString;
FILE *PyWin_FindRegisteredModule(const char *moduleName,
- struct filedescr **ppFileDesc,
- char *pathBuf,
- Py_ssize_t pathLen)
+ struct filedescr **ppFileDesc,
+ char *pathBuf,
+ Py_ssize_t pathLen)
{
- char *moduleKey;
- const char keyPrefix[] = "Software\\Python\\PythonCore\\";
- const char keySuffix[] = "\\Modules\\";
+ char *moduleKey;
+ const char keyPrefix[] = "Software\\Python\\PythonCore\\";
+ const char keySuffix[] = "\\Modules\\";
#ifdef _DEBUG
- /* In debugging builds, we _must_ have the debug version
- * registered.
- */
- const char debugString[] = "\\Debug";
+ /* In debugging builds, we _must_ have the debug version
+ * registered.
+ */
+ const char debugString[] = "\\Debug";
#else
- const char debugString[] = "";
+ const char debugString[] = "";
#endif
- struct filedescr *fdp = NULL;
- FILE *fp;
- HKEY keyBase = HKEY_CURRENT_USER;
- int modNameSize;
- long regStat;
+ struct filedescr *fdp = NULL;
+ FILE *fp;
+ HKEY keyBase = HKEY_CURRENT_USER;
+ int modNameSize;
+ long regStat;
- /* Calculate the size for the sprintf buffer.
- * Get the size of the chars only, plus 1 NULL.
- */
- size_t bufSize = sizeof(keyPrefix)-1 +
- strlen(PyWin_DLLVersionString) +
- sizeof(keySuffix) +
- strlen(moduleName) +
- sizeof(debugString) - 1;
- /* alloca == no free required, but memory only local to fn,
- * also no heap fragmentation!
- */
- moduleKey = alloca(bufSize);
- PyOS_snprintf(moduleKey, bufSize,
- "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
- PyWin_DLLVersionString, moduleName, debugString);
+ /* Calculate the size for the sprintf buffer.
+ * Get the size of the chars only, plus 1 NULL.
+ */
+ size_t bufSize = sizeof(keyPrefix)-1 +
+ strlen(PyWin_DLLVersionString) +
+ sizeof(keySuffix) +
+ strlen(moduleName) +
+ sizeof(debugString) - 1;
+ /* alloca == no free required, but memory only local to fn,
+ * also no heap fragmentation!
+ */
+ moduleKey = alloca(bufSize);
+ PyOS_snprintf(moduleKey, bufSize,
+ "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
+ PyWin_DLLVersionString, moduleName, debugString);
- assert(pathLen < INT_MAX);
- modNameSize = (int)pathLen;
- regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
- if (regStat != ERROR_SUCCESS) {
- /* No user setting - lookup in machine settings */
- keyBase = HKEY_LOCAL_MACHINE;
- /* be anal - failure may have reset size param */
- modNameSize = (int)pathLen;
- regStat = RegQueryValue(keyBase, moduleKey,
- pathBuf, &modNameSize);
+ assert(pathLen < INT_MAX);
+ modNameSize = (int)pathLen;
+ regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
+ if (regStat != ERROR_SUCCESS) {
+ /* No user setting - lookup in machine settings */
+ keyBase = HKEY_LOCAL_MACHINE;
+ /* be anal - failure may have reset size param */
+ modNameSize = (int)pathLen;
+ regStat = RegQueryValue(keyBase, moduleKey,
+ pathBuf, &modNameSize);
- if (regStat != ERROR_SUCCESS)
- return NULL;
- }
- /* use the file extension to locate the type entry. */
- for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
- size_t extLen = strlen(fdp->suffix);
- assert(modNameSize >= 0); /* else cast to size_t is wrong */
- if ((size_t)modNameSize > extLen &&
- strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
- fdp->suffix,
- extLen) == 0)
- break;
- }
- if (fdp->suffix == NULL)
- return NULL;
- fp = fopen(pathBuf, fdp->mode);
- if (fp != NULL)
- *ppFileDesc = fdp;
- return fp;
+ if (regStat != ERROR_SUCCESS)
+ return NULL;
+ }
+ /* use the file extension to locate the type entry. */
+ for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
+ size_t extLen = strlen(fdp->suffix);
+ assert(modNameSize >= 0); /* else cast to size_t is wrong */
+ if ((size_t)modNameSize > extLen &&
+ strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
+ fdp->suffix,
+ extLen) == 0)
+ break;
+ }
+ if (fdp->suffix == NULL)
+ return NULL;
+ fp = fopen(pathBuf, fdp->mode);
+ if (fp != NULL)
+ *ppFileDesc = fdp;
+ return fp;
}
diff --git a/PC/make_versioninfo.c b/PC/make_versioninfo.c
index b9f9b88..e969671 100644
--- a/PC/make_versioninfo.c
+++ b/PC/make_versioninfo.c
@@ -22,17 +22,17 @@
*/
int main(int argc, char **argv)
{
- printf("/* This file created by make_versioninfo.exe */\n");
- printf("#define FIELD3 %d\n",
- PY_MICRO_VERSION*1000 + PY_RELEASE_LEVEL*10 + PY_RELEASE_SERIAL);
- printf("#define MS_DLL_ID \"%d.%d\"\n",
- PY_MAJOR_VERSION, PY_MINOR_VERSION);
- printf("#ifndef _DEBUG\n");
- printf("#define PYTHON_DLL_NAME \"python%d%d.dll\"\n",
- PY_MAJOR_VERSION, PY_MINOR_VERSION);
- printf("#else\n");
- printf("#define PYTHON_DLL_NAME \"python%d%d_d.dll\"\n",
- PY_MAJOR_VERSION, PY_MINOR_VERSION);
- printf("#endif\n");
- return 0;
+ printf("/* This file created by make_versioninfo.exe */\n");
+ printf("#define FIELD3 %d\n",
+ PY_MICRO_VERSION*1000 + PY_RELEASE_LEVEL*10 + PY_RELEASE_SERIAL);
+ printf("#define MS_DLL_ID \"%d.%d\"\n",
+ PY_MAJOR_VERSION, PY_MINOR_VERSION);
+ printf("#ifndef _DEBUG\n");
+ printf("#define PYTHON_DLL_NAME \"python%d%d.dll\"\n",
+ PY_MAJOR_VERSION, PY_MINOR_VERSION);
+ printf("#else\n");
+ printf("#define PYTHON_DLL_NAME \"python%d%d_d.dll\"\n",
+ PY_MAJOR_VERSION, PY_MINOR_VERSION);
+ printf("#endif\n");
+ return 0;
}
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index 6c43278..9a86d5e 100755
--- a/PC/msvcrtmodule.c
+++ b/PC/msvcrtmodule.c
@@ -1,18 +1,18 @@
/*********************************************************
- msvcrtmodule.c
+ msvcrtmodule.c
- A Python interface to the Microsoft Visual C Runtime
- Library, providing access to those non-portable, but
- still useful routines.
+ A Python interface to the Microsoft Visual C Runtime
+ Library, providing access to those non-portable, but
+ still useful routines.
- Only ever compiled with an MS compiler, so no attempt
- has been made to avoid MS language extensions, etc...
+ Only ever compiled with an MS compiler, so no attempt
+ has been made to avoid MS language extensions, etc...
- This may only work on NT or 95...
+ This may only work on NT or 95...
- Author: Mark Hammond and Guido van Rossum.
- Maintenance: Guido van Rossum.
+ Author: Mark Hammond and Guido van Rossum.
+ Maintenance: Guido van Rossum.
***********************************************************/
@@ -35,14 +35,14 @@
static PyObject *
msvcrt_heapmin(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":heapmin"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":heapmin"))
+ return NULL;
- if (_heapmin() != 0)
- return PyErr_SetFromErrno(PyExc_IOError);
+ if (_heapmin() != 0)
+ return PyErr_SetFromErrno(PyExc_IOError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(heapmin_doc,
@@ -55,22 +55,22 @@ to the operating system. On failure, this raises IOError.");
static PyObject *
msvcrt_locking(PyObject *self, PyObject *args)
{
- int fd;
- int mode;
- long nbytes;
- int err;
-
- if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- err = _locking(fd, mode, nbytes);
- Py_END_ALLOW_THREADS
- if (err != 0)
- return PyErr_SetFromErrno(PyExc_IOError);
-
- Py_INCREF(Py_None);
- return Py_None;
+ int fd;
+ int mode;
+ long nbytes;
+ int err;
+
+ if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ err = _locking(fd, mode, nbytes);
+ Py_END_ALLOW_THREADS
+ if (err != 0)
+ return PyErr_SetFromErrno(PyExc_IOError);
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(locking_doc,
@@ -88,16 +88,16 @@ individually.");
static PyObject *
msvcrt_setmode(PyObject *self, PyObject *args)
{
- int fd;
- int flags;
- if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
- return NULL;
+ int fd;
+ int flags;
+ if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
+ return NULL;
- flags = _setmode(fd, flags);
- if (flags == -1)
- return PyErr_SetFromErrno(PyExc_IOError);
+ flags = _setmode(fd, flags);
+ if (flags == -1)
+ return PyErr_SetFromErrno(PyExc_IOError);
- return PyLong_FromLong(flags);
+ return PyLong_FromLong(flags);
}
PyDoc_STRVAR(setmode_doc,
@@ -111,18 +111,18 @@ os.O_BINARY.");
static PyObject *
msvcrt_open_osfhandle(PyObject *self, PyObject *args)
{
- long handle;
- int flags;
- int fd;
+ long handle;
+ int flags;
+ int fd;
- if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
- return NULL;
+ if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
+ return NULL;
- fd = _open_osfhandle(handle, flags);
- if (fd == -1)
- return PyErr_SetFromErrno(PyExc_IOError);
+ fd = _open_osfhandle(handle, flags);
+ if (fd == -1)
+ return PyErr_SetFromErrno(PyExc_IOError);
- return PyLong_FromLong(fd);
+ return PyLong_FromLong(fd);
}
PyDoc_STRVAR(open_osfhandle_doc,
@@ -137,20 +137,20 @@ to os.fdopen() to create a file object.");
static PyObject *
msvcrt_get_osfhandle(PyObject *self, PyObject *args)
{
- int fd;
- Py_intptr_t handle;
+ int fd;
+ Py_intptr_t handle;
- if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
- return NULL;
+ if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
+ return NULL;
- handle = _get_osfhandle(fd);
- if (handle == -1)
- return PyErr_SetFromErrno(PyExc_IOError);
+ handle = _get_osfhandle(fd);
+ if (handle == -1)
+ return PyErr_SetFromErrno(PyExc_IOError);
- /* technically 'handle' is not a pointer, but a integer as
- large as a pointer, Python's *VoidPtr interface is the
- most appropriate here */
- return PyLong_FromVoidPtr((void*)handle);
+ /* technically 'handle' is not a pointer, but a integer as
+ large as a pointer, Python's *VoidPtr interface is the
+ most appropriate here */
+ return PyLong_FromVoidPtr((void*)handle);
}
PyDoc_STRVAR(get_osfhandle_doc,
@@ -164,13 +164,13 @@ if fd is not recognized.");
static PyObject *
msvcrt_kbhit(PyObject *self, PyObject *args)
{
- int ok;
+ int ok;
- if (!PyArg_ParseTuple(args, ":kbhit"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":kbhit"))
+ return NULL;
- ok = _kbhit();
- return PyLong_FromLong(ok);
+ ok = _kbhit();
+ return PyLong_FromLong(ok);
}
PyDoc_STRVAR(kbhit_doc,
@@ -181,17 +181,17 @@ Return true if a keypress is waiting to be read.");
static PyObject *
msvcrt_getch(PyObject *self, PyObject *args)
{
- int ch;
- char s[1];
+ int ch;
+ char s[1];
- if (!PyArg_ParseTuple(args, ":getch"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":getch"))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- ch = _getch();
- Py_END_ALLOW_THREADS
- s[0] = ch;
- return PyBytes_FromStringAndSize(s, 1);
+ Py_BEGIN_ALLOW_THREADS
+ ch = _getch();
+ Py_END_ALLOW_THREADS
+ s[0] = ch;
+ return PyBytes_FromStringAndSize(s, 1);
}
PyDoc_STRVAR(getch_doc,
@@ -208,17 +208,17 @@ this function.");
static PyObject *
msvcrt_getwch(PyObject *self, PyObject *args)
{
- Py_UNICODE ch;
- Py_UNICODE u[1];
+ Py_UNICODE ch;
+ Py_UNICODE u[1];
- if (!PyArg_ParseTuple(args, ":getwch"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":getwch"))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- ch = _getwch();
- Py_END_ALLOW_THREADS
- u[0] = ch;
- return PyUnicode_FromUnicode(u, 1);
+ Py_BEGIN_ALLOW_THREADS
+ ch = _getwch();
+ Py_END_ALLOW_THREADS
+ u[0] = ch;
+ return PyUnicode_FromUnicode(u, 1);
}
PyDoc_STRVAR(getwch_doc,
@@ -230,17 +230,17 @@ Wide char variant of getch(), returning a Unicode value.");
static PyObject *
msvcrt_getche(PyObject *self, PyObject *args)
{
- int ch;
- char s[1];
+ int ch;
+ char s[1];
- if (!PyArg_ParseTuple(args, ":getche"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":getche"))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- ch = _getche();
- Py_END_ALLOW_THREADS
- s[0] = ch;
- return PyBytes_FromStringAndSize(s, 1);
+ Py_BEGIN_ALLOW_THREADS
+ ch = _getche();
+ Py_END_ALLOW_THREADS
+ s[0] = ch;
+ return PyBytes_FromStringAndSize(s, 1);
}
PyDoc_STRVAR(getche_doc,
@@ -253,17 +253,17 @@ a printable character.");
static PyObject *
msvcrt_getwche(PyObject *self, PyObject *args)
{
- Py_UNICODE ch;
- Py_UNICODE s[1];
+ Py_UNICODE ch;
+ Py_UNICODE s[1];
- if (!PyArg_ParseTuple(args, ":getwche"))
- return NULL;
+ if (!PyArg_ParseTuple(args, ":getwche"))
+ return NULL;
- Py_BEGIN_ALLOW_THREADS
- ch = _getwche();
- Py_END_ALLOW_THREADS
- s[0] = ch;
- return PyUnicode_FromUnicode(s, 1);
+ Py_BEGIN_ALLOW_THREADS
+ ch = _getwche();
+ Py_END_ALLOW_THREADS
+ s[0] = ch;
+ return PyUnicode_FromUnicode(s, 1);
}
PyDoc_STRVAR(getwche_doc,
@@ -275,14 +275,14 @@ Wide char variant of getche(), returning a Unicode value.");
static PyObject *
msvcrt_putch(PyObject *self, PyObject *args)
{
- char ch;
+ char ch;
- if (!PyArg_ParseTuple(args, "c:putch", &ch))
- return NULL;
+ if (!PyArg_ParseTuple(args, "c:putch", &ch))
+ return NULL;
- _putch(ch);
- Py_INCREF(Py_None);
- return Py_None;
+ _putch(ch);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(putch_doc,
@@ -294,13 +294,13 @@ Print the character char to the console without buffering.");
static PyObject *
msvcrt_putwch(PyObject *self, PyObject *args)
{
- int ch;
+ int ch;
- if (!PyArg_ParseTuple(args, "C:putwch", &ch))
- return NULL;
+ if (!PyArg_ParseTuple(args, "C:putwch", &ch))
+ return NULL;
- _putwch(ch);
- Py_RETURN_NONE;
+ _putwch(ch);
+ Py_RETURN_NONE;
}
@@ -313,15 +313,15 @@ Wide char variant of putch(), accepting a Unicode value.");
static PyObject *
msvcrt_ungetch(PyObject *self, PyObject *args)
{
- char ch;
+ char ch;
- if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
- return NULL;
+ if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
+ return NULL;
- if (_ungetch(ch) == EOF)
- return PyErr_SetFromErrno(PyExc_IOError);
- Py_INCREF(Py_None);
- return Py_None;
+ if (_ungetch(ch) == EOF)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(ungetch_doc,
@@ -334,15 +334,15 @@ it will be the next character read by getch() or getche().");
static PyObject *
msvcrt_ungetwch(PyObject *self, PyObject *args)
{
- int ch;
+ int ch;
- if (!PyArg_ParseTuple(args, "C:ungetwch", &ch))
- return NULL;
+ if (!PyArg_ParseTuple(args, "C:ungetwch", &ch))
+ return NULL;
- if (_ungetwch(ch) == WEOF)
- return PyErr_SetFromErrno(PyExc_IOError);
- Py_INCREF(Py_None);
- return Py_None;
+ if (_ungetwch(ch) == WEOF)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(ungetwch_doc,
@@ -354,15 +354,15 @@ Wide char variant of ungetch(), accepting a Unicode value.");
static void
insertint(PyObject *d, char *name, int value)
{
- PyObject *v = PyLong_FromLong((long) value);
- if (v == NULL) {
- /* Don't bother reporting this error */
- PyErr_Clear();
- }
- else {
- PyDict_SetItemString(d, name, v);
- Py_DECREF(v);
- }
+ PyObject *v = PyLong_FromLong((long) value);
+ if (v == NULL) {
+ /* Don't bother reporting this error */
+ PyErr_Clear();
+ }
+ else {
+ PyDict_SetItemString(d, name, v);
+ Py_DECREF(v);
+ }
}
#ifdef _DEBUG
@@ -370,40 +370,40 @@ insertint(PyObject *d, char *name, int value)
static PyObject*
msvcrt_setreportfile(PyObject *self, PyObject *args)
{
- int type, file;
- _HFILE res;
-
- if (!PyArg_ParseTuple(args, "ii", &type, &file))
- return NULL;
- res = _CrtSetReportFile(type, (_HFILE)file);
- return PyLong_FromLong((long)res);
- Py_INCREF(Py_None);
- return Py_None;
+ int type, file;
+ _HFILE res;
+
+ if (!PyArg_ParseTuple(args, "ii", &type, &file))
+ return NULL;
+ res = _CrtSetReportFile(type, (_HFILE)file);
+ return PyLong_FromLong((long)res);
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject*
msvcrt_setreportmode(PyObject *self, PyObject *args)
{
- int type, mode;
- int res;
-
- if (!PyArg_ParseTuple(args, "ii", &type, &mode))
- return NULL;
- res = _CrtSetReportMode(type, mode);
- if (res == -1)
- return PyErr_SetFromErrno(PyExc_IOError);
- return PyLong_FromLong(res);
+ int type, mode;
+ int res;
+
+ if (!PyArg_ParseTuple(args, "ii", &type, &mode))
+ return NULL;
+ res = _CrtSetReportMode(type, mode);
+ if (res == -1)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ return PyLong_FromLong(res);
}
static PyObject*
msvcrt_seterrormode(PyObject *self, PyObject *args)
{
- int mode, res;
+ int mode, res;
- if (!PyArg_ParseTuple(args, "i", &mode))
- return NULL;
- res = _set_error_mode(mode);
- return PyLong_FromLong(res);
+ if (!PyArg_ParseTuple(args, "i", &mode))
+ return NULL;
+ res = _set_error_mode(mode);
+ return PyLong_FromLong(res);
}
#endif
@@ -411,104 +411,104 @@ msvcrt_seterrormode(PyObject *self, PyObject *args)
static PyObject*
seterrormode(PyObject *self, PyObject *args)
{
- unsigned int mode, res;
+ unsigned int mode, res;
- if (!PyArg_ParseTuple(args, "I", &mode))
- return NULL;
- res = SetErrorMode(mode);
- return PyLong_FromUnsignedLong(res);
+ if (!PyArg_ParseTuple(args, "I", &mode))
+ return NULL;
+ res = SetErrorMode(mode);
+ return PyLong_FromUnsignedLong(res);
}
/* List of functions exported by this module */
static struct PyMethodDef msvcrt_functions[] = {
- {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc},
- {"locking", msvcrt_locking, METH_VARARGS, locking_doc},
- {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc},
- {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
- {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
- {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc},
- {"getch", msvcrt_getch, METH_VARARGS, getch_doc},
- {"getche", msvcrt_getche, METH_VARARGS, getche_doc},
- {"putch", msvcrt_putch, METH_VARARGS, putch_doc},
- {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc},
- {"SetErrorMode", seterrormode, METH_VARARGS},
+ {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc},
+ {"locking", msvcrt_locking, METH_VARARGS, locking_doc},
+ {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc},
+ {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
+ {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
+ {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc},
+ {"getch", msvcrt_getch, METH_VARARGS, getch_doc},
+ {"getche", msvcrt_getche, METH_VARARGS, getche_doc},
+ {"putch", msvcrt_putch, METH_VARARGS, putch_doc},
+ {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc},
+ {"SetErrorMode", seterrormode, METH_VARARGS},
#ifdef _DEBUG
- {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS},
- {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS},
- {"set_error_mode", msvcrt_seterrormode, METH_VARARGS},
+ {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS},
+ {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS},
+ {"set_error_mode", msvcrt_seterrormode, METH_VARARGS},
#endif
#ifdef _WCONIO_DEFINED
- {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc},
- {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc},
- {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc},
- {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
+ {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc},
+ {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc},
+ {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc},
+ {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
#endif
- {NULL, NULL}
+ {NULL, NULL}
};
static struct PyModuleDef msvcrtmodule = {
- PyModuleDef_HEAD_INIT,
- "msvcrt",
- NULL,
- -1,
- msvcrt_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "msvcrt",
+ NULL,
+ -1,
+ msvcrt_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_msvcrt(void)
{
- int st;
- PyObject *d;
- PyObject *m = PyModule_Create(&msvcrtmodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
-
- /* constants for the locking() function's mode argument */
- insertint(d, "LK_LOCK", _LK_LOCK);
- insertint(d, "LK_NBLCK", _LK_NBLCK);
- insertint(d, "LK_NBRLCK", _LK_NBRLCK);
- insertint(d, "LK_RLCK", _LK_RLCK);
- insertint(d, "LK_UNLCK", _LK_UNLCK);
- insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
- insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
- insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
- insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
+ int st;
+ PyObject *d;
+ PyObject *m = PyModule_Create(&msvcrtmodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+
+ /* constants for the locking() function's mode argument */
+ insertint(d, "LK_LOCK", _LK_LOCK);
+ insertint(d, "LK_NBLCK", _LK_NBLCK);
+ insertint(d, "LK_NBRLCK", _LK_NBRLCK);
+ insertint(d, "LK_RLCK", _LK_RLCK);
+ insertint(d, "LK_UNLCK", _LK_UNLCK);
+ insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
+ insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
+ insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
+ insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
#ifdef _DEBUG
- insertint(d, "CRT_WARN", _CRT_WARN);
- insertint(d, "CRT_ERROR", _CRT_ERROR);
- insertint(d, "CRT_ASSERT", _CRT_ASSERT);
- insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
- insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
- insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
- insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
- insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
- insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
- insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
+ insertint(d, "CRT_WARN", _CRT_WARN);
+ insertint(d, "CRT_ERROR", _CRT_ERROR);
+ insertint(d, "CRT_ASSERT", _CRT_ASSERT);
+ insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
+ insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
+ insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
+ insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
+ insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
+ insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
+ insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
#endif
- /* constants for the crt versions */
+ /* constants for the crt versions */
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
- st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
- _VC_ASSEMBLY_PUBLICKEYTOKEN);
- if (st < 0) return NULL;
+ st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
+ _VC_ASSEMBLY_PUBLICKEYTOKEN);
+ if (st < 0) return NULL;
#endif
#ifdef _CRT_ASSEMBLY_VERSION
- st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
- _CRT_ASSEMBLY_VERSION);
- if (st < 0) return NULL;
+ st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
+ _CRT_ASSEMBLY_VERSION);
+ if (st < 0) return NULL;
#endif
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
- st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
- __LIBRARIES_ASSEMBLY_NAME_PREFIX);
- if (st < 0) return NULL;
+ st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
+ __LIBRARIES_ASSEMBLY_NAME_PREFIX);
+ if (st < 0) return NULL;
#endif
- return m;
+ return m;
}
diff --git a/PC/os2emx/config.c b/PC/os2emx/config.c
index 88ece9d..218aa68 100644
--- a/PC/os2emx/config.c
+++ b/PC/os2emx/config.c
@@ -95,71 +95,71 @@ extern void initgc();
struct _inittab _PyImport_Inittab[] = {
- {"os2", initos2},
- {"signal", initsignal},
+ {"os2", initos2},
+ {"signal", initsignal},
#ifdef WITH_THREAD
- {"_thread", init_thread},
+ {"_thread", init_thread},
#endif
- {"_codecs", init_codecs},
- {"_csv", init_csv},
- {"_locale", init_locale},
- {"_random", init_random},
- {"_sre", init_sre},
- {"_symtable", init_symtable},
- {"_weakref", init_weakref},
- {"array", initarray},
- {"binascii", initbinascii},
- {"collections", initcollections},
- {"cmath", initcmath},
- {"datetime", initdatetime},
- {"dl", initdl},
- {"errno", initerrno},
- {"fcntl", initfcntl},
- {"_functools", init_functools},
- {"_heapq", init_heapq},
- {"imageop", initimageop},
- {"itertools", inititertools},
- {"math", initmath},
- {"operator", initoperator},
- {"_sha256", init_sha256},
- {"_sha512", init_sha512},
- {"_struct", init_struct},
- {"termios", inittermios},
- {"time", inittime},
- {"xxsubtype", initxxsubtype},
- {"zipimport", initzipimport},
+ {"_codecs", init_codecs},
+ {"_csv", init_csv},
+ {"_locale", init_locale},
+ {"_random", init_random},
+ {"_sre", init_sre},
+ {"_symtable", init_symtable},
+ {"_weakref", init_weakref},
+ {"array", initarray},
+ {"binascii", initbinascii},
+ {"collections", initcollections},
+ {"cmath", initcmath},
+ {"datetime", initdatetime},
+ {"dl", initdl},
+ {"errno", initerrno},
+ {"fcntl", initfcntl},
+ {"_functools", init_functools},
+ {"_heapq", init_heapq},
+ {"imageop", initimageop},
+ {"itertools", inititertools},
+ {"math", initmath},
+ {"operator", initoperator},
+ {"_sha256", init_sha256},
+ {"_sha512", init_sha512},
+ {"_struct", init_struct},
+ {"termios", inittermios},
+ {"time", inittime},
+ {"xxsubtype", initxxsubtype},
+ {"zipimport", initzipimport},
#if !HAVE_DYNAMIC_LOADING
- {"_curses", init_curses},
- {"_curses_panel", init_curses_panel},
- {"_testcapi", init_testcapi},
- {"bz2", initbz2},
- {"fpectl", initfpectl},
- {"fpetest", initfpetest},
- {"parser", initparser},
- {"pwd", initpwd},
- {"unicodedata", initunicodedata},
- {"zlib", initzlib},
+ {"_curses", init_curses},
+ {"_curses_panel", init_curses_panel},
+ {"_testcapi", init_testcapi},
+ {"bz2", initbz2},
+ {"fpectl", initfpectl},
+ {"fpetest", initfpetest},
+ {"parser", initparser},
+ {"pwd", initpwd},
+ {"unicodedata", initunicodedata},
+ {"zlib", initzlib},
#ifdef USE_SOCKET
- {"_socket", init_socket},
- {"select", initselect},
+ {"_socket", init_socket},
+ {"select", initselect},
#endif
#endif
/* -- ADDMODULE MARKER 2 -- */
- /* This module "lives in" with marshal.c */
- {"marshal", PyMarshal_Init},
+ /* This module "lives in" with marshal.c */
+ {"marshal", PyMarshal_Init},
- /* This lives it with import.c */
- {"imp", initimp},
+ /* This lives it with import.c */
+ {"imp", initimp},
- /* These entries are here for sys.builtin_module_names */
- {"__main__", NULL},
- {"builtins", NULL},
- {"sys", NULL},
+ /* These entries are here for sys.builtin_module_names */
+ {"__main__", NULL},
+ {"builtins", NULL},
+ {"sys", NULL},
- /* This lives in gcmodule.c */
- {"gc", initgc},
+ /* This lives in gcmodule.c */
+ {"gc", initgc},
- /* Sentinel */
- {0, 0}
+ /* Sentinel */
+ {0, 0}
};
diff --git a/PC/os2emx/dlfcn.c b/PC/os2emx/dlfcn.c
index 9e88d5e..47c046c 100644
--- a/PC/os2emx/dlfcn.c
+++ b/PC/os2emx/dlfcn.c
@@ -46,178 +46,178 @@ PERFORMANCE OF THIS SOFTWARE.
#include <malloc.h>
typedef struct _track_rec {
- char *name;
- HMODULE handle;
- void *id;
- struct _track_rec *next;
+ char *name;
+ HMODULE handle;
+ void *id;
+ struct _track_rec *next;
} tDLLchain, *DLLchain;
-static DLLchain dlload = NULL; /* A simple chained list of DLL names */
-static char dlerr [256]; /* last error text string */
+static DLLchain dlload = NULL; /* A simple chained list of DLL names */
+static char dlerr [256]; /* last error text string */
static void *last_id;
static DLLchain find_id(void *id)
{
- DLLchain tmp;
+ DLLchain tmp;
- for (tmp = dlload; tmp; tmp = tmp->next)
- if (id == tmp->id)
- return tmp;
+ for (tmp = dlload; tmp; tmp = tmp->next)
+ if (id == tmp->id)
+ return tmp;
- return NULL;
+ return NULL;
}
/* load a dynamic-link library and return handle */
void *dlopen(char *filename, int flags)
{
- HMODULE hm;
- DLLchain tmp;
- char err[256];
- char *errtxt;
- int rc = 0, set_chain = 0;
-
- for (tmp = dlload; tmp; tmp = tmp->next)
- if (strnicmp(tmp->name, filename, 999) == 0)
- break;
-
- if (!tmp)
- {
- tmp = (DLLchain) malloc(sizeof(tDLLchain));
- if (!tmp)
- goto nomem;
- tmp->name = strdup(filename);
- tmp->next = dlload;
- set_chain = 1;
- }
-
- switch (rc = DosLoadModule((PSZ)&err, sizeof(err), filename, &hm))
- {
- case NO_ERROR:
- tmp->handle = hm;
- if (set_chain)
- {
- do
- last_id++;
- while ((last_id == 0) || (find_id(last_id)));
- tmp->id = last_id;
- dlload = tmp;
- }
- return tmp->id;
- case ERROR_FILE_NOT_FOUND:
- case ERROR_PATH_NOT_FOUND:
- errtxt = "module `%s' not found";
- break;
- case ERROR_TOO_MANY_OPEN_FILES:
- case ERROR_NOT_ENOUGH_MEMORY:
- case ERROR_SHARING_BUFFER_EXCEEDED:
+ HMODULE hm;
+ DLLchain tmp;
+ char err[256];
+ char *errtxt;
+ int rc = 0, set_chain = 0;
+
+ for (tmp = dlload; tmp; tmp = tmp->next)
+ if (strnicmp(tmp->name, filename, 999) == 0)
+ break;
+
+ if (!tmp)
+ {
+ tmp = (DLLchain) malloc(sizeof(tDLLchain));
+ if (!tmp)
+ goto nomem;
+ tmp->name = strdup(filename);
+ tmp->next = dlload;
+ set_chain = 1;
+ }
+
+ switch (rc = DosLoadModule((PSZ)&err, sizeof(err), filename, &hm))
+ {
+ case NO_ERROR:
+ tmp->handle = hm;
+ if (set_chain)
+ {
+ do
+ last_id++;
+ while ((last_id == 0) || (find_id(last_id)));
+ tmp->id = last_id;
+ dlload = tmp;
+ }
+ return tmp->id;
+ case ERROR_FILE_NOT_FOUND:
+ case ERROR_PATH_NOT_FOUND:
+ errtxt = "module `%s' not found";
+ break;
+ case ERROR_TOO_MANY_OPEN_FILES:
+ case ERROR_NOT_ENOUGH_MEMORY:
+ case ERROR_SHARING_BUFFER_EXCEEDED:
nomem:
- errtxt = "out of system resources";
- break;
- case ERROR_ACCESS_DENIED:
- errtxt = "access denied";
- break;
- case ERROR_BAD_FORMAT:
- case ERROR_INVALID_SEGMENT_NUMBER:
- case ERROR_INVALID_ORDINAL:
- case ERROR_INVALID_MODULETYPE:
- case ERROR_INVALID_EXE_SIGNATURE:
- case ERROR_EXE_MARKED_INVALID:
- case ERROR_ITERATED_DATA_EXCEEDS_64K:
- case ERROR_INVALID_MINALLOCSIZE:
- case ERROR_INVALID_SEGDPL:
- case ERROR_AUTODATASEG_EXCEEDS_64K:
- case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT:
- errtxt = "invalid module format";
- break;
- case ERROR_INVALID_NAME:
- errtxt = "filename doesn't match module name";
- break;
- case ERROR_SHARING_VIOLATION:
- case ERROR_LOCK_VIOLATION:
- errtxt = "sharing violation";
- break;
- case ERROR_INIT_ROUTINE_FAILED:
- errtxt = "module initialization failed";
- break;
- default:
- errtxt = "cause `%s', error code = %d";
- break;
- }
- snprintf(dlerr, sizeof(dlerr), errtxt, &err, rc);
- if (tmp)
- {
- if (tmp->name)
- free(tmp->name);
- free(tmp);
- }
- return 0;
+ errtxt = "out of system resources";
+ break;
+ case ERROR_ACCESS_DENIED:
+ errtxt = "access denied";
+ break;
+ case ERROR_BAD_FORMAT:
+ case ERROR_INVALID_SEGMENT_NUMBER:
+ case ERROR_INVALID_ORDINAL:
+ case ERROR_INVALID_MODULETYPE:
+ case ERROR_INVALID_EXE_SIGNATURE:
+ case ERROR_EXE_MARKED_INVALID:
+ case ERROR_ITERATED_DATA_EXCEEDS_64K:
+ case ERROR_INVALID_MINALLOCSIZE:
+ case ERROR_INVALID_SEGDPL:
+ case ERROR_AUTODATASEG_EXCEEDS_64K:
+ case ERROR_RELOCSRC_CHAIN_EXCEEDS_SEGLIMIT:
+ errtxt = "invalid module format";
+ break;
+ case ERROR_INVALID_NAME:
+ errtxt = "filename doesn't match module name";
+ break;
+ case ERROR_SHARING_VIOLATION:
+ case ERROR_LOCK_VIOLATION:
+ errtxt = "sharing violation";
+ break;
+ case ERROR_INIT_ROUTINE_FAILED:
+ errtxt = "module initialization failed";
+ break;
+ default:
+ errtxt = "cause `%s', error code = %d";
+ break;
+ }
+ snprintf(dlerr, sizeof(dlerr), errtxt, &err, rc);
+ if (tmp)
+ {
+ if (tmp->name)
+ free(tmp->name);
+ free(tmp);
+ }
+ return 0;
}
/* return a pointer to the `symbol' in DLL */
void *dlsym(void *handle, char *symbol)
{
- int rc = 0;
- PFN addr;
- char *errtxt;
- int symord = 0;
- DLLchain tmp = find_id(handle);
-
- if (!tmp)
- goto inv_handle;
-
- if (*symbol == '#')
- symord = atoi(symbol + 1);
-
- switch (rc = DosQueryProcAddr(tmp->handle, symord, symbol, &addr))
- {
- case NO_ERROR:
- return (void *)addr;
- case ERROR_INVALID_HANDLE:
+ int rc = 0;
+ PFN addr;
+ char *errtxt;
+ int symord = 0;
+ DLLchain tmp = find_id(handle);
+
+ if (!tmp)
+ goto inv_handle;
+
+ if (*symbol == '#')
+ symord = atoi(symbol + 1);
+
+ switch (rc = DosQueryProcAddr(tmp->handle, symord, symbol, &addr))
+ {
+ case NO_ERROR:
+ return (void *)addr;
+ case ERROR_INVALID_HANDLE:
inv_handle:
- errtxt = "invalid module handle";
- break;
- case ERROR_PROC_NOT_FOUND:
- case ERROR_INVALID_NAME:
- errtxt = "no symbol `%s' in module";
- break;
- default:
- errtxt = "symbol `%s', error code = %d";
- break;
- }
- snprintf(dlerr, sizeof(dlerr), errtxt, symbol, rc);
- return NULL;
+ errtxt = "invalid module handle";
+ break;
+ case ERROR_PROC_NOT_FOUND:
+ case ERROR_INVALID_NAME:
+ errtxt = "no symbol `%s' in module";
+ break;
+ default:
+ errtxt = "symbol `%s', error code = %d";
+ break;
+ }
+ snprintf(dlerr, sizeof(dlerr), errtxt, symbol, rc);
+ return NULL;
}
/* free dynamicaly-linked library */
int dlclose(void *handle)
{
- int rc;
- DLLchain tmp = find_id(handle);
-
- if (!tmp)
- goto inv_handle;
-
- switch (rc = DosFreeModule(tmp->handle))
- {
- case NO_ERROR:
- free(tmp->name);
- dlload = tmp->next;
- free(tmp);
- return 0;
- case ERROR_INVALID_HANDLE:
+ int rc;
+ DLLchain tmp = find_id(handle);
+
+ if (!tmp)
+ goto inv_handle;
+
+ switch (rc = DosFreeModule(tmp->handle))
+ {
+ case NO_ERROR:
+ free(tmp->name);
+ dlload = tmp->next;
+ free(tmp);
+ return 0;
+ case ERROR_INVALID_HANDLE:
inv_handle:
- strcpy(dlerr, "invalid module handle");
- return -1;
- case ERROR_INVALID_ACCESS:
- strcpy(dlerr, "access denied");
- return -1;
- default:
- return -1;
- }
+ strcpy(dlerr, "invalid module handle");
+ return -1;
+ case ERROR_INVALID_ACCESS:
+ strcpy(dlerr, "access denied");
+ return -1;
+ default:
+ return -1;
+ }
}
/* return a string describing last occurred dl error */
char *dlerror()
{
- return dlerr;
+ return dlerr;
}
diff --git a/PC/os2emx/dllentry.c b/PC/os2emx/dllentry.c
index dbf9c98..9ccca1c 100644
--- a/PC/os2emx/dllentry.c
+++ b/PC/os2emx/dllentry.c
@@ -4,7 +4,7 @@
#define NULL 0
-#define REF(s) extern void s(); void *____ref_##s = &s;
+#define REF(s) extern void s(); void *____ref_##s = &s;
/* Make references to imported symbols to pull them from static library */
REF(Py_Main);
@@ -18,25 +18,25 @@ extern void __ctordtorTerm(void);
unsigned long _DLL_InitTerm(unsigned long mod_handle, unsigned long flag)
{
- switch (flag)
- {
- case 0:
- if (_CRT_init())
- return 0;
- __ctordtorInit();
-
- /* Ignore fatal signals */
- signal(SIGSEGV, SIG_IGN);
- signal(SIGFPE, SIG_IGN);
-
- return 1;
-
- case 1:
- __ctordtorTerm();
- _CRT_term();
- return 1;
-
- default:
- return 0;
- }
+ switch (flag)
+ {
+ case 0:
+ if (_CRT_init())
+ return 0;
+ __ctordtorInit();
+
+ /* Ignore fatal signals */
+ signal(SIGSEGV, SIG_IGN);
+ signal(SIGFPE, SIG_IGN);
+
+ return 1;
+
+ case 1:
+ __ctordtorTerm();
+ _CRT_term();
+ return 1;
+
+ default:
+ return 0;
+ }
}
diff --git a/PC/os2emx/getpathp.c b/PC/os2emx/getpathp.c
index 9e37f63..0d73774 100644
--- a/PC/os2emx/getpathp.c
+++ b/PC/os2emx/getpathp.c
@@ -4,8 +4,8 @@
/* ----------------------------------------------------------------
PATH RULES FOR OS/2+EMX:
- This describes how sys.path is formed on OS/2+EMX. It describes the
- functionality, not the implementation (ie, the order in which these
+ This describes how sys.path is formed on OS/2+EMX. It describes the
+ functionality, not the implementation (ie, the order in which these
are actually fetched is different)
* Python always adds an empty entry at the start, which corresponds
@@ -16,10 +16,10 @@
* We attempt to locate the "Python Home" - if the PYTHONHOME env var
is set, we believe it. Otherwise, we use the path of our host .EXE's
to try and locate our "landmark" (lib\\os.py) and deduce our home.
- - If we DO have a Python Home: The relevant sub-directories (Lib,
+ - If we DO have a Python Home: The relevant sub-directories (Lib,
plat-win, etc) are based on the Python Home
- If we DO NOT have a Python Home, the core Python Path is
- loaded from the registry. This is the main PythonPath key,
+ loaded from the registry. This is the main PythonPath key,
and both HKLM and HKCU are combined to form the path)
* Iff - we can not locate the Python Home, and have not had a PYTHONPATH
@@ -32,9 +32,9 @@
(either an installed version, or directly from the PCbuild directory),
the core path is deduced.
- * When Python is hosted in another exe (different directory, embedded via
+ * When Python is hosted in another exe (different directory, embedded via
COM, etc), the Python Home will not be deduced, so the core path from
- the registry is used. Other "application paths "in the registry are
+ the registry is used. Other "application paths "in the registry are
always read.
* If Python can't find its home and there is no registry (eg, frozen
@@ -85,12 +85,12 @@ static char *module_search_path = NULL;
static int
-is_sep(char ch) /* determine if "ch" is a separator character */
+is_sep(char ch) /* determine if "ch" is a separator character */
{
#ifdef ALTSEP
- return ch == SEP || ch == ALTSEP;
+ return ch == SEP || ch == ALTSEP;
#else
- return ch == SEP;
+ return ch == SEP;
#endif
}
@@ -100,36 +100,36 @@ is_sep(char ch) /* determine if "ch" is a separator character */
static void
reduce(char *dir)
{
- size_t i = strlen(dir);
- while (i > 0 && !is_sep(dir[i]))
- --i;
- dir[i] = '\0';
+ size_t i = strlen(dir);
+ while (i > 0 && !is_sep(dir[i]))
+ --i;
+ dir[i] = '\0';
}
-
+
static int
exists(char *filename)
{
- struct stat buf;
- return stat(filename, &buf) == 0;
+ struct stat buf;
+ return stat(filename, &buf) == 0;
}
/* Is module (check for .pyc/.pyo too)
- * Assumes 'filename' MAXPATHLEN+1 bytes long -
+ * Assumes 'filename' MAXPATHLEN+1 bytes long -
* may extend 'filename' by one character.
*/
static int
ismodule(char *filename)
{
- if (exists(filename))
- return 1;
-
- /* Check for the compiled version of prefix. */
- if (strlen(filename) < MAXPATHLEN) {
- strcat(filename, Py_OptimizeFlag ? "o" : "c");
- if (exists(filename))
- return 1;
- }
- return 0;
+ if (exists(filename))
+ return 1;
+
+ /* Check for the compiled version of prefix. */
+ if (strlen(filename) < MAXPATHLEN) {
+ strcat(filename, Py_OptimizeFlag ? "o" : "c");
+ if (exists(filename))
+ return 1;
+ }
+ return 0;
}
/* Add a path component, by appending stuff to buffer.
@@ -145,21 +145,21 @@ ismodule(char *filename)
static void
join(char *buffer, char *stuff)
{
- size_t n, k;
- if (is_sep(stuff[0]))
- n = 0;
- else {
- n = strlen(buffer);
- if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
- buffer[n++] = SEP;
- }
- if (n > MAXPATHLEN)
- Py_FatalError("buffer overflow in getpathp.c's joinpath()");
- k = strlen(stuff);
- if (n + k > MAXPATHLEN)
- k = MAXPATHLEN - n;
- strncpy(buffer+n, stuff, k);
- buffer[n+k] = '\0';
+ size_t n, k;
+ if (is_sep(stuff[0]))
+ n = 0;
+ else {
+ n = strlen(buffer);
+ if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
+ buffer[n++] = SEP;
+ }
+ if (n > MAXPATHLEN)
+ Py_FatalError("buffer overflow in getpathp.c's joinpath()");
+ k = strlen(stuff);
+ if (n + k > MAXPATHLEN)
+ k = MAXPATHLEN - n;
+ strncpy(buffer+n, stuff, k);
+ buffer[n+k] = '\0';
}
/* gotlandmark only called by search_for_prefix, which ensures
@@ -169,219 +169,219 @@ join(char *buffer, char *stuff)
static int
gotlandmark(char *landmark)
{
- int n, ok;
+ int n, ok;
- n = strlen(prefix);
- join(prefix, landmark);
- ok = ismodule(prefix);
- prefix[n] = '\0';
- return ok;
+ n = strlen(prefix);
+ join(prefix, landmark);
+ ok = ismodule(prefix);
+ prefix[n] = '\0';
+ return ok;
}
-/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
+/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
* assumption provided by only caller, calculate_path()
*/
static int
search_for_prefix(char *argv0_path, char *landmark)
{
- /* Search from argv0_path, until landmark is found */
- strcpy(prefix, argv0_path);
- do {
- if (gotlandmark(landmark))
- return 1;
- reduce(prefix);
- } while (prefix[0]);
- return 0;
+ /* Search from argv0_path, until landmark is found */
+ strcpy(prefix, argv0_path);
+ do {
+ if (gotlandmark(landmark))
+ return 1;
+ reduce(prefix);
+ } while (prefix[0]);
+ return 0;
}
static void
get_progpath(void)
{
- extern char *Py_GetProgramName(void);
- char *path = getenv("PATH");
- char *prog = Py_GetProgramName();
-
- PPIB pib;
- if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
- (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
- return;
-
- if (prog == NULL || *prog == '\0')
- prog = "python";
-
- /* If there is no slash in the argv0 path, then we have to
- * assume python is on the user's $PATH, since there's no
- * other way to find a directory to start the search from. If
- * $PATH isn't exported, you lose.
- */
+ extern char *Py_GetProgramName(void);
+ char *path = getenv("PATH");
+ char *prog = Py_GetProgramName();
+
+ PPIB pib;
+ if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
+ (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
+ return;
+
+ if (prog == NULL || *prog == '\0')
+ prog = "python";
+
+ /* If there is no slash in the argv0 path, then we have to
+ * assume python is on the user's $PATH, since there's no
+ * other way to find a directory to start the search from. If
+ * $PATH isn't exported, you lose.
+ */
#ifdef ALTSEP
- if (strchr(prog, SEP) || strchr(prog, ALTSEP))
+ if (strchr(prog, SEP) || strchr(prog, ALTSEP))
#else
- if (strchr(prog, SEP))
+ if (strchr(prog, SEP))
#endif
- strncpy(progpath, prog, MAXPATHLEN);
- else if (path) {
- while (1) {
- char *delim = strchr(path, DELIM);
-
- if (delim) {
- size_t len = delim - path;
- /* ensure we can't overwrite buffer */
+ strncpy(progpath, prog, MAXPATHLEN);
+ else if (path) {
+ while (1) {
+ char *delim = strchr(path, DELIM);
+
+ if (delim) {
+ size_t len = delim - path;
+ /* ensure we can't overwrite buffer */
#if !defined(PYCC_GCC)
- len = min(MAXPATHLEN,len);
+ len = min(MAXPATHLEN,len);
#else
- len = MAXPATHLEN < len ? MAXPATHLEN : len;
+ len = MAXPATHLEN < len ? MAXPATHLEN : len;
#endif
- strncpy(progpath, path, len);
- *(progpath + len) = '\0';
- }
- else
- strncpy(progpath, path, MAXPATHLEN);
-
- /* join() is safe for MAXPATHLEN+1 size buffer */
- join(progpath, prog);
- if (exists(progpath))
- break;
-
- if (!delim) {
- progpath[0] = '\0';
- break;
- }
- path = delim + 1;
- }
- }
- else
- progpath[0] = '\0';
+ strncpy(progpath, path, len);
+ *(progpath + len) = '\0';
+ }
+ else
+ strncpy(progpath, path, MAXPATHLEN);
+
+ /* join() is safe for MAXPATHLEN+1 size buffer */
+ join(progpath, prog);
+ if (exists(progpath))
+ break;
+
+ if (!delim) {
+ progpath[0] = '\0';
+ break;
+ }
+ path = delim + 1;
+ }
+ }
+ else
+ progpath[0] = '\0';
}
static void
calculate_path(void)
{
- char argv0_path[MAXPATHLEN+1];
- char *buf;
- size_t bufsz;
- char *pythonhome = Py_GetPythonHome();
- char *envpath = getenv("PYTHONPATH");
- char zip_path[MAXPATHLEN+1];
- size_t len;
-
- get_progpath();
- /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
- strcpy(argv0_path, progpath);
- reduce(argv0_path);
- if (pythonhome == NULL || *pythonhome == '\0') {
- if (search_for_prefix(argv0_path, LANDMARK))
- pythonhome = prefix;
- else
- pythonhome = NULL;
- }
- else
- strncpy(prefix, pythonhome, MAXPATHLEN);
-
- if (envpath && *envpath == '\0')
- envpath = NULL;
-
- /* Calculate zip archive path */
- strncpy(zip_path, progpath, MAXPATHLEN);
- zip_path[MAXPATHLEN] = '\0';
- len = strlen(zip_path);
- if (len > 4) {
- zip_path[len-3] = 'z'; /* change ending to "zip" */
- zip_path[len-2] = 'i';
- zip_path[len-1] = 'p';
- }
- else {
- zip_path[0] = 0;
- }
-
- /* We need to construct a path from the following parts.
- * (1) the PYTHONPATH environment variable, if set;
- * (2) the zip archive file path;
- * (3) the PYTHONPATH config macro, with the leading "."
- * of each component replaced with pythonhome, if set;
- * (4) the directory containing the executable (argv0_path).
- * The length calculation calculates #3 first.
- */
-
- /* Calculate size of return buffer */
- if (pythonhome != NULL) {
- char *p;
- bufsz = 1;
- for (p = PYTHONPATH; *p; p++) {
- if (*p == DELIM)
- bufsz++; /* number of DELIM plus one */
- }
- bufsz *= strlen(pythonhome);
- }
- else
- bufsz = 0;
- bufsz += strlen(PYTHONPATH) + 1;
- bufsz += strlen(argv0_path) + 1;
- bufsz += strlen(zip_path) + 1;
- if (envpath != NULL)
- bufsz += strlen(envpath) + 1;
-
- module_search_path = buf = malloc(bufsz);
- if (buf == NULL) {
- /* We can't exit, so print a warning and limp along */
- fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
- if (envpath) {
- fprintf(stderr, "Using environment $PYTHONPATH.\n");
- module_search_path = envpath;
- }
- else {
- fprintf(stderr, "Using default static path.\n");
- module_search_path = PYTHONPATH;
- }
- return;
- }
-
- if (envpath) {
- strcpy(buf, envpath);
- buf = strchr(buf, '\0');
- *buf++ = DELIM;
- }
- if (zip_path[0]) {
- strcpy(buf, zip_path);
- buf = strchr(buf, '\0');
- *buf++ = DELIM;
- }
-
- if (pythonhome == NULL) {
- strcpy(buf, PYTHONPATH);
- buf = strchr(buf, '\0');
- }
- else {
- char *p = PYTHONPATH;
- char *q;
- size_t n;
- for (;;) {
- q = strchr(p, DELIM);
- if (q == NULL)
- n = strlen(p);
- else
- n = q-p;
- if (p[0] == '.' && is_sep(p[1])) {
- strcpy(buf, pythonhome);
- buf = strchr(buf, '\0');
- p++;
- n--;
- }
- strncpy(buf, p, n);
- buf += n;
- if (q == NULL)
- break;
- *buf++ = DELIM;
- p = q+1;
- }
- }
- if (argv0_path) {
- *buf++ = DELIM;
- strcpy(buf, argv0_path);
- buf = strchr(buf, '\0');
- }
- *buf = '\0';
+ char argv0_path[MAXPATHLEN+1];
+ char *buf;
+ size_t bufsz;
+ char *pythonhome = Py_GetPythonHome();
+ char *envpath = getenv("PYTHONPATH");
+ char zip_path[MAXPATHLEN+1];
+ size_t len;
+
+ get_progpath();
+ /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
+ strcpy(argv0_path, progpath);
+ reduce(argv0_path);
+ if (pythonhome == NULL || *pythonhome == '\0') {
+ if (search_for_prefix(argv0_path, LANDMARK))
+ pythonhome = prefix;
+ else
+ pythonhome = NULL;
+ }
+ else
+ strncpy(prefix, pythonhome, MAXPATHLEN);
+
+ if (envpath && *envpath == '\0')
+ envpath = NULL;
+
+ /* Calculate zip archive path */
+ strncpy(zip_path, progpath, MAXPATHLEN);
+ zip_path[MAXPATHLEN] = '\0';
+ len = strlen(zip_path);
+ if (len > 4) {
+ zip_path[len-3] = 'z'; /* change ending to "zip" */
+ zip_path[len-2] = 'i';
+ zip_path[len-1] = 'p';
+ }
+ else {
+ zip_path[0] = 0;
+ }
+
+ /* We need to construct a path from the following parts.
+ * (1) the PYTHONPATH environment variable, if set;
+ * (2) the zip archive file path;
+ * (3) the PYTHONPATH config macro, with the leading "."
+ * of each component replaced with pythonhome, if set;
+ * (4) the directory containing the executable (argv0_path).
+ * The length calculation calculates #3 first.
+ */
+
+ /* Calculate size of return buffer */
+ if (pythonhome != NULL) {
+ char *p;
+ bufsz = 1;
+ for (p = PYTHONPATH; *p; p++) {
+ if (*p == DELIM)
+ bufsz++; /* number of DELIM plus one */
+ }
+ bufsz *= strlen(pythonhome);
+ }
+ else
+ bufsz = 0;
+ bufsz += strlen(PYTHONPATH) + 1;
+ bufsz += strlen(argv0_path) + 1;
+ bufsz += strlen(zip_path) + 1;
+ if (envpath != NULL)
+ bufsz += strlen(envpath) + 1;
+
+ module_search_path = buf = malloc(bufsz);
+ if (buf == NULL) {
+ /* We can't exit, so print a warning and limp along */
+ fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
+ if (envpath) {
+ fprintf(stderr, "Using environment $PYTHONPATH.\n");
+ module_search_path = envpath;
+ }
+ else {
+ fprintf(stderr, "Using default static path.\n");
+ module_search_path = PYTHONPATH;
+ }
+ return;
+ }
+
+ if (envpath) {
+ strcpy(buf, envpath);
+ buf = strchr(buf, '\0');
+ *buf++ = DELIM;
+ }
+ if (zip_path[0]) {
+ strcpy(buf, zip_path);
+ buf = strchr(buf, '\0');
+ *buf++ = DELIM;
+ }
+
+ if (pythonhome == NULL) {
+ strcpy(buf, PYTHONPATH);
+ buf = strchr(buf, '\0');
+ }
+ else {
+ char *p = PYTHONPATH;
+ char *q;
+ size_t n;
+ for (;;) {
+ q = strchr(p, DELIM);
+ if (q == NULL)
+ n = strlen(p);
+ else
+ n = q-p;
+ if (p[0] == '.' && is_sep(p[1])) {
+ strcpy(buf, pythonhome);
+ buf = strchr(buf, '\0');
+ p++;
+ n--;
+ }
+ strncpy(buf, p, n);
+ buf += n;
+ if (q == NULL)
+ break;
+ *buf++ = DELIM;
+ p = q+1;
+ }
+ }
+ if (argv0_path) {
+ *buf++ = DELIM;
+ strcpy(buf, argv0_path);
+ buf = strchr(buf, '\0');
+ }
+ *buf = '\0';
}
@@ -390,29 +390,29 @@ calculate_path(void)
char *
Py_GetPath(void)
{
- if (!module_search_path)
- calculate_path();
- return module_search_path;
+ if (!module_search_path)
+ calculate_path();
+ return module_search_path;
}
char *
Py_GetPrefix(void)
{
- if (!module_search_path)
- calculate_path();
- return prefix;
+ if (!module_search_path)
+ calculate_path();
+ return prefix;
}
char *
Py_GetExecPrefix(void)
{
- return Py_GetPrefix();
+ return Py_GetPrefix();
}
char *
Py_GetProgramFullPath(void)
{
- if (!module_search_path)
- calculate_path();
- return progpath;
+ if (!module_search_path)
+ calculate_path();
+ return progpath;
}
diff --git a/PC/os2emx/pythonpm.c b/PC/os2emx/pythonpm.c
index 5accbbd..ba47f4b 100644
--- a/PC/os2emx/pythonpm.c
+++ b/PC/os2emx/pythonpm.c
@@ -27,10 +27,10 @@
/* use structure to pass command line to Python thread */
typedef struct
{
- int argc;
- char **argv;
- HWND Frame;
- int running;
+ int argc;
+ char **argv;
+ HWND Frame;
+ int running;
} arglist;
/* make this a global to simplify access.
@@ -45,80 +45,80 @@ void PythonThread(void *);
int
main(int argc, char **argv)
{
- ULONG FrameFlags = FCF_TITLEBAR |
- FCF_SYSMENU |
- FCF_SIZEBORDER |
- FCF_HIDEBUTTON |
- FCF_SHELLPOSITION |
- FCF_TASKLIST;
- HAB hab;
- HMQ hmq;
- HWND Client;
- QMSG qmsg;
- arglist args;
- int python_tid;
-
- /* init PM and create message queue */
- hab = WinInitialize(0);
- hmq = WinCreateMsgQueue(hab, 0);
-
- /* create a (hidden) Window to house the window procedure */
- args.Frame = WinCreateStdWindow(HWND_DESKTOP,
- 0,
- &FrameFlags,
- NULL,
- "PythonPM",
- 0L,
- 0,
- 0,
- &Client);
-
- /* run Python interpreter in a thread */
- args.argc = argc;
- args.argv = argv;
- args.running = 0;
- if (-1 == (python_tid = _beginthread(PythonThread, NULL, 1024 * 1024, &args)))
- {
- /* couldn't start thread */
- WinAlarm(HWND_DESKTOP, WA_ERROR);
- PythonRC = 1;
- }
- else
- {
- /* process PM messages, until Python exits */
- while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
- WinDispatchMsg(hab, &qmsg);
- if (args.running > 0)
- DosKillThread(python_tid);
- }
-
- /* destroy window, shutdown message queue and PM */
- WinDestroyWindow(args.Frame);
- WinDestroyMsgQueue(hmq);
- WinTerminate(hab);
-
- return PythonRC;
+ ULONG FrameFlags = FCF_TITLEBAR |
+ FCF_SYSMENU |
+ FCF_SIZEBORDER |
+ FCF_HIDEBUTTON |
+ FCF_SHELLPOSITION |
+ FCF_TASKLIST;
+ HAB hab;
+ HMQ hmq;
+ HWND Client;
+ QMSG qmsg;
+ arglist args;
+ int python_tid;
+
+ /* init PM and create message queue */
+ hab = WinInitialize(0);
+ hmq = WinCreateMsgQueue(hab, 0);
+
+ /* create a (hidden) Window to house the window procedure */
+ args.Frame = WinCreateStdWindow(HWND_DESKTOP,
+ 0,
+ &FrameFlags,
+ NULL,
+ "PythonPM",
+ 0L,
+ 0,
+ 0,
+ &Client);
+
+ /* run Python interpreter in a thread */
+ args.argc = argc;
+ args.argv = argv;
+ args.running = 0;
+ if (-1 == (python_tid = _beginthread(PythonThread, NULL, 1024 * 1024, &args)))
+ {
+ /* couldn't start thread */
+ WinAlarm(HWND_DESKTOP, WA_ERROR);
+ PythonRC = 1;
+ }
+ else
+ {
+ /* process PM messages, until Python exits */
+ while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
+ WinDispatchMsg(hab, &qmsg);
+ if (args.running > 0)
+ DosKillThread(python_tid);
+ }
+
+ /* destroy window, shutdown message queue and PM */
+ WinDestroyWindow(args.Frame);
+ WinDestroyMsgQueue(hmq);
+ WinTerminate(hab);
+
+ return PythonRC;
}
void PythonThread(void *argl)
{
- HAB hab;
- arglist *args;
+ HAB hab;
+ arglist *args;
- /* PM initialisation */
- hab = WinInitialize(0);
+ /* PM initialisation */
+ hab = WinInitialize(0);
- /* start Python */
- args = (arglist *)argl;
- args->running = 1;
- PythonRC = Py_Main(args->argc, args->argv);
+ /* start Python */
+ args = (arglist *)argl;
+ args->running = 1;
+ PythonRC = Py_Main(args->argc, args->argv);
- /* enter a critical section and send the termination message */
- DosEnterCritSec();
- args->running = 0;
- WinPostMsg(args->Frame, WM_QUIT, NULL, NULL);
+ /* enter a critical section and send the termination message */
+ DosEnterCritSec();
+ args->running = 0;
+ WinPostMsg(args->Frame, WM_QUIT, NULL, NULL);
- /* shutdown PM and terminate thread */
- WinTerminate(hab);
- _endthread();
+ /* shutdown PM and terminate thread */
+ WinTerminate(hab);
+ _endthread();
}
diff --git a/PC/os2vacpp/getpathp.c b/PC/os2vacpp/getpathp.c
index 607f2a1..5bc2827 100644
--- a/PC/os2vacpp/getpathp.c
+++ b/PC/os2vacpp/getpathp.c
@@ -55,12 +55,12 @@ static char *module_search_path = NULL;
static int
-is_sep(char ch) /* determine if "ch" is a separator character */
+is_sep(char ch) /* determine if "ch" is a separator character */
{
#ifdef ALTSEP
- return ch == SEP || ch == ALTSEP;
+ return ch == SEP || ch == ALTSEP;
#else
- return ch == SEP;
+ return ch == SEP;
#endif
}
@@ -68,18 +68,18 @@ is_sep(char ch) /* determine if "ch" is a separator character */
static void
reduce(char *dir)
{
- int i = strlen(dir);
- while (i > 0 && !is_sep(dir[i]))
- --i;
- dir[i] = '\0';
+ int i = strlen(dir);
+ while (i > 0 && !is_sep(dir[i]))
+ --i;
+ dir[i] = '\0';
}
-
+
static int
exists(char *filename)
{
- struct stat buf;
- return stat(filename, &buf) == 0;
+ struct stat buf;
+ return stat(filename, &buf) == 0;
}
@@ -95,42 +95,42 @@ exists(char *filename)
static void
join(char *buffer, char *stuff)
{
- int n, k;
- if (is_sep(stuff[0]))
- n = 0;
- else {
- n = strlen(buffer);
- if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
- buffer[n++] = SEP;
- }
- if (n > MAXPATHLEN)
- Py_FatalError("buffer overflow in getpathp.c's joinpath()");
- k = strlen(stuff);
- if (n + k > MAXPATHLEN)
- k = MAXPATHLEN - n;
- strncpy(buffer+n, stuff, k);
- buffer[n+k] = '\0';
+ int n, k;
+ if (is_sep(stuff[0]))
+ n = 0;
+ else {
+ n = strlen(buffer);
+ if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
+ buffer[n++] = SEP;
+ }
+ if (n > MAXPATHLEN)
+ Py_FatalError("buffer overflow in getpathp.c's joinpath()");
+ k = strlen(stuff);
+ if (n + k > MAXPATHLEN)
+ k = MAXPATHLEN - n;
+ strncpy(buffer+n, stuff, k);
+ buffer[n+k] = '\0';
}
static int
search_for_prefix(char *argv0_path, char *landmark)
{
- int n;
-
- /* Search from argv0_path, until root is found */
- strcpy(prefix, argv0_path);
- do {
- n = strlen(prefix);
- join(prefix, landmark);
- if (exists(prefix)) {
- prefix[n] = '\0';
- return 1;
- }
- prefix[n] = '\0';
- reduce(prefix);
- } while (prefix[0]);
- return 0;
+ int n;
+
+ /* Search from argv0_path, until root is found */
+ strcpy(prefix, argv0_path);
+ do {
+ n = strlen(prefix);
+ join(prefix, landmark);
+ if (exists(prefix)) {
+ prefix[n] = '\0';
+ return 1;
+ }
+ prefix[n] = '\0';
+ reduce(prefix);
+ } while (prefix[0]);
+ return 0;
}
#ifdef MS_WIN32
@@ -147,299 +147,299 @@ extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at st
static char *
getpythonregpath(HKEY keyBase, BOOL bWin32s)
{
- HKEY newKey = 0;
- DWORD nameSize = 0;
- DWORD dataSize = 0;
- DWORD numEntries = 0;
- LONG rc;
- char *retval = NULL;
- char *dataBuf;
- const char keyPrefix[] = "Software\\Python\\PythonCore\\";
- const char keySuffix[] = "\\PythonPath";
- int versionLen;
- char *keyBuf;
-
- // Tried to use sysget("winver") but here is too early :-(
- versionLen = strlen(PyWin_DLLVersionString);
- // alloca == no free required, but memory only local to fn.
- // also no heap fragmentation! Am I being silly?
- keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
- // lots of constants here for the compiler to optimize away :-)
- memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
- memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
- memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
-
- rc=RegOpenKey(keyBase,
- keyBuf,
- &newKey);
- if (rc==ERROR_SUCCESS) {
- RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
- &numEntries, &nameSize, &dataSize, NULL, NULL);
- }
- if (bWin32s && numEntries==0 && dataSize==0) {
- /* must hardcode for Win32s */
- numEntries = 1;
- dataSize = 511;
- }
- if (numEntries) {
- /* Loop over all subkeys. */
- /* Win32s doesnt know how many subkeys, so we do
- it twice */
- char keyBuf[MAX_PATH+1];
- int index = 0;
- int off = 0;
- for(index=0;;index++) {
- long reqdSize = 0;
- DWORD rc = RegEnumKey(newKey,
- index, keyBuf, MAX_PATH+1);
- if (rc) break;
- rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
- if (rc) break;
- if (bWin32s && reqdSize==0) reqdSize = 512;
- dataSize += reqdSize + 1; /* 1 for the ";" */
- }
- dataBuf = malloc(dataSize+1);
- if (dataBuf==NULL)
- return NULL; /* pretty serious? Raise error? */
- /* Now loop over, grabbing the paths.
- Subkeys before main library */
- for(index=0;;index++) {
- int adjust;
- long reqdSize = dataSize;
- DWORD rc = RegEnumKey(newKey,
- index, keyBuf,MAX_PATH+1);
- if (rc) break;
- rc = RegQueryValue(newKey,
- keyBuf, dataBuf+off, &reqdSize);
- if (rc) break;
- if (reqdSize>1) {
- /* If Nothing, or only '\0' copied. */
- adjust = strlen(dataBuf+off);
- dataSize -= adjust;
- off += adjust;
- dataBuf[off++] = ';';
- dataBuf[off] = '\0';
- dataSize--;
- }
- }
- /* Additionally, win32s doesnt work as expected, so
- the specific strlen() is required for 3.1. */
- rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
- if (rc==ERROR_SUCCESS) {
- if (strlen(dataBuf)==0)
- free(dataBuf);
- else
- retval = dataBuf; /* caller will free */
- }
- else
- free(dataBuf);
- }
-
- if (newKey)
- RegCloseKey(newKey);
- return retval;
+ HKEY newKey = 0;
+ DWORD nameSize = 0;
+ DWORD dataSize = 0;
+ DWORD numEntries = 0;
+ LONG rc;
+ char *retval = NULL;
+ char *dataBuf;
+ const char keyPrefix[] = "Software\\Python\\PythonCore\\";
+ const char keySuffix[] = "\\PythonPath";
+ int versionLen;
+ char *keyBuf;
+
+ // Tried to use sysget("winver") but here is too early :-(
+ versionLen = strlen(PyWin_DLLVersionString);
+ // alloca == no free required, but memory only local to fn.
+ // also no heap fragmentation! Am I being silly?
+ keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
+ // lots of constants here for the compiler to optimize away :-)
+ memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
+ memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
+ memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
+
+ rc=RegOpenKey(keyBase,
+ keyBuf,
+ &newKey);
+ if (rc==ERROR_SUCCESS) {
+ RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
+ &numEntries, &nameSize, &dataSize, NULL, NULL);
+ }
+ if (bWin32s && numEntries==0 && dataSize==0) {
+ /* must hardcode for Win32s */
+ numEntries = 1;
+ dataSize = 511;
+ }
+ if (numEntries) {
+ /* Loop over all subkeys. */
+ /* Win32s doesnt know how many subkeys, so we do
+ it twice */
+ char keyBuf[MAX_PATH+1];
+ int index = 0;
+ int off = 0;
+ for(index=0;;index++) {
+ long reqdSize = 0;
+ DWORD rc = RegEnumKey(newKey,
+ index, keyBuf, MAX_PATH+1);
+ if (rc) break;
+ rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
+ if (rc) break;
+ if (bWin32s && reqdSize==0) reqdSize = 512;
+ dataSize += reqdSize + 1; /* 1 for the ";" */
+ }
+ dataBuf = malloc(dataSize+1);
+ if (dataBuf==NULL)
+ return NULL; /* pretty serious? Raise error? */
+ /* Now loop over, grabbing the paths.
+ Subkeys before main library */
+ for(index=0;;index++) {
+ int adjust;
+ long reqdSize = dataSize;
+ DWORD rc = RegEnumKey(newKey,
+ index, keyBuf,MAX_PATH+1);
+ if (rc) break;
+ rc = RegQueryValue(newKey,
+ keyBuf, dataBuf+off, &reqdSize);
+ if (rc) break;
+ if (reqdSize>1) {
+ /* If Nothing, or only '\0' copied. */
+ adjust = strlen(dataBuf+off);
+ dataSize -= adjust;
+ off += adjust;
+ dataBuf[off++] = ';';
+ dataBuf[off] = '\0';
+ dataSize--;
+ }
+ }
+ /* Additionally, win32s doesnt work as expected, so
+ the specific strlen() is required for 3.1. */
+ rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
+ if (rc==ERROR_SUCCESS) {
+ if (strlen(dataBuf)==0)
+ free(dataBuf);
+ else
+ retval = dataBuf; /* caller will free */
+ }
+ else
+ free(dataBuf);
+ }
+
+ if (newKey)
+ RegCloseKey(newKey);
+ return retval;
}
#endif /* MS_WIN32 */
static void
get_progpath(void)
{
- extern char *Py_GetProgramName(void);
- char *path = getenv("PATH");
- char *prog = Py_GetProgramName();
+ extern char *Py_GetProgramName(void);
+ char *path = getenv("PATH");
+ char *prog = Py_GetProgramName();
#ifdef MS_WIN32
- if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
- return;
+ if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
+ return;
#endif
- if (prog == NULL || *prog == '\0')
- prog = "python";
-
- /* If there is no slash in the argv0 path, then we have to
- * assume python is on the user's $PATH, since there's no
- * other way to find a directory to start the search from. If
- * $PATH isn't exported, you lose.
- */
+ if (prog == NULL || *prog == '\0')
+ prog = "python";
+
+ /* If there is no slash in the argv0 path, then we have to
+ * assume python is on the user's $PATH, since there's no
+ * other way to find a directory to start the search from. If
+ * $PATH isn't exported, you lose.
+ */
#ifdef ALTSEP
- if (strchr(prog, SEP) || strchr(prog, ALTSEP))
+ if (strchr(prog, SEP) || strchr(prog, ALTSEP))
#else
- if (strchr(prog, SEP))
+ if (strchr(prog, SEP))
#endif
- strcpy(progpath, prog);
- else if (path) {
- while (1) {
- char *delim = strchr(path, DELIM);
-
- if (delim) {
- int len = delim - path;
- strncpy(progpath, path, len);
- *(progpath + len) = '\0';
- }
- else
- strcpy(progpath, path);
-
- join(progpath, prog);
- if (exists(progpath))
- break;
-
- if (!delim) {
- progpath[0] = '\0';
- break;
- }
- path = delim + 1;
- }
- }
- else
- progpath[0] = '\0';
+ strcpy(progpath, prog);
+ else if (path) {
+ while (1) {
+ char *delim = strchr(path, DELIM);
+
+ if (delim) {
+ int len = delim - path;
+ strncpy(progpath, path, len);
+ *(progpath + len) = '\0';
+ }
+ else
+ strcpy(progpath, path);
+
+ join(progpath, prog);
+ if (exists(progpath))
+ break;
+
+ if (!delim) {
+ progpath[0] = '\0';
+ break;
+ }
+ path = delim + 1;
+ }
+ }
+ else
+ progpath[0] = '\0';
}
static void
calculate_path(void)
{
- char argv0_path[MAXPATHLEN+1];
- char *buf;
- int bufsz;
- char *pythonhome = Py_GetPythonHome();
- char *envpath = Py_GETENV("PYTHONPATH");
+ char argv0_path[MAXPATHLEN+1];
+ char *buf;
+ int bufsz;
+ char *pythonhome = Py_GetPythonHome();
+ char *envpath = Py_GETENV("PYTHONPATH");
#ifdef MS_WIN32
- char *machinepath, *userpath;
-
- /* Are we running under Windows 3.1(1) Win32s? */
- if (PyWin_IsWin32s()) {
- /* Only CLASSES_ROOT is supported */
- machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
- userpath = NULL;
- } else {
- machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
- userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
- }
+ char *machinepath, *userpath;
+
+ /* Are we running under Windows 3.1(1) Win32s? */
+ if (PyWin_IsWin32s()) {
+ /* Only CLASSES_ROOT is supported */
+ machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
+ userpath = NULL;
+ } else {
+ machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
+ userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
+ }
#endif
- get_progpath();
- strcpy(argv0_path, progpath);
- reduce(argv0_path);
- if (pythonhome == NULL || *pythonhome == '\0') {
- if (search_for_prefix(argv0_path, LANDMARK))
- pythonhome = prefix;
- else
- pythonhome = NULL;
- }
- else {
- char *delim;
-
- strcpy(prefix, pythonhome);
-
- /* Extract Any Optional Trailing EXEC_PREFIX */
- /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
- delim = strchr(prefix, DELIM);
- if (delim) {
- *delim = '\0';
- strcpy(exec_prefix, delim+1);
- } else
- strcpy(exec_prefix, EXEC_PREFIX);
+ get_progpath();
+ strcpy(argv0_path, progpath);
+ reduce(argv0_path);
+ if (pythonhome == NULL || *pythonhome == '\0') {
+ if (search_for_prefix(argv0_path, LANDMARK))
+ pythonhome = prefix;
+ else
+ pythonhome = NULL;
+ }
+ else {
+ char *delim;
+
+ strcpy(prefix, pythonhome);
+
+ /* Extract Any Optional Trailing EXEC_PREFIX */
+ /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
+ delim = strchr(prefix, DELIM);
+ if (delim) {
+ *delim = '\0';
+ strcpy(exec_prefix, delim+1);
+ } else
+ strcpy(exec_prefix, EXEC_PREFIX);
}
- if (envpath && *envpath == '\0')
- envpath = NULL;
-
- /* We need to construct a path from the following parts:
- (1) the PYTHONPATH environment variable, if set;
- (2) for Win32, the machinepath and userpath, if set;
- (3) the PYTHONPATH config macro, with the leading "."
- of each component replaced with pythonhome, if set;
- (4) the directory containing the executable (argv0_path).
- The length calculation calculates #3 first.
- */
-
- /* Calculate size of return buffer */
- if (pythonhome != NULL) {
- char *p;
- bufsz = 1;
- for (p = PYTHONPATH; *p; p++) {
- if (*p == DELIM)
- bufsz++; /* number of DELIM plus one */
- }
- bufsz *= strlen(pythonhome);
- }
- else
- bufsz = 0;
- bufsz += strlen(PYTHONPATH) + 1;
- if (envpath != NULL)
- bufsz += strlen(envpath) + 1;
- bufsz += strlen(argv0_path) + 1;
+ if (envpath && *envpath == '\0')
+ envpath = NULL;
+
+ /* We need to construct a path from the following parts:
+ (1) the PYTHONPATH environment variable, if set;
+ (2) for Win32, the machinepath and userpath, if set;
+ (3) the PYTHONPATH config macro, with the leading "."
+ of each component replaced with pythonhome, if set;
+ (4) the directory containing the executable (argv0_path).
+ The length calculation calculates #3 first.
+ */
+
+ /* Calculate size of return buffer */
+ if (pythonhome != NULL) {
+ char *p;
+ bufsz = 1;
+ for (p = PYTHONPATH; *p; p++) {
+ if (*p == DELIM)
+ bufsz++; /* number of DELIM plus one */
+ }
+ bufsz *= strlen(pythonhome);
+ }
+ else
+ bufsz = 0;
+ bufsz += strlen(PYTHONPATH) + 1;
+ if (envpath != NULL)
+ bufsz += strlen(envpath) + 1;
+ bufsz += strlen(argv0_path) + 1;
#ifdef MS_WIN32
- if (machinepath)
- bufsz += strlen(machinepath) + 1;
- if (userpath)
- bufsz += strlen(userpath) + 1;
+ if (machinepath)
+ bufsz += strlen(machinepath) + 1;
+ if (userpath)
+ bufsz += strlen(userpath) + 1;
#endif
- module_search_path = buf = malloc(bufsz);
- if (buf == NULL) {
- /* We can't exit, so print a warning and limp along */
- fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
- if (envpath) {
- fprintf(stderr, "Using default static $PYTHONPATH.\n");
- module_search_path = envpath;
- }
- else {
- fprintf(stderr, "Using environment $PYTHONPATH.\n");
- module_search_path = PYTHONPATH;
- }
- return;
- }
-
- if (envpath) {
- strcpy(buf, envpath);
- buf = strchr(buf, '\0');
- *buf++ = DELIM;
- }
+ module_search_path = buf = malloc(bufsz);
+ if (buf == NULL) {
+ /* We can't exit, so print a warning and limp along */
+ fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
+ if (envpath) {
+ fprintf(stderr, "Using default static $PYTHONPATH.\n");
+ module_search_path = envpath;
+ }
+ else {
+ fprintf(stderr, "Using environment $PYTHONPATH.\n");
+ module_search_path = PYTHONPATH;
+ }
+ return;
+ }
+
+ if (envpath) {
+ strcpy(buf, envpath);
+ buf = strchr(buf, '\0');
+ *buf++ = DELIM;
+ }
#ifdef MS_WIN32
- if (machinepath) {
- strcpy(buf, machinepath);
- buf = strchr(buf, '\0');
- *buf++ = DELIM;
- }
- if (userpath) {
- strcpy(buf, userpath);
- buf = strchr(buf, '\0');
- *buf++ = DELIM;
- }
+ if (machinepath) {
+ strcpy(buf, machinepath);
+ buf = strchr(buf, '\0');
+ *buf++ = DELIM;
+ }
+ if (userpath) {
+ strcpy(buf, userpath);
+ buf = strchr(buf, '\0');
+ *buf++ = DELIM;
+ }
#endif
- if (pythonhome == NULL) {
- strcpy(buf, PYTHONPATH);
- buf = strchr(buf, '\0');
- }
- else {
- char *p = PYTHONPATH;
- char *q;
- int n;
- for (;;) {
- q = strchr(p, DELIM);
- if (q == NULL)
- n = strlen(p);
- else
- n = q-p;
- if (p[0] == '.' && is_sep(p[1])) {
- strcpy(buf, pythonhome);
- buf = strchr(buf, '\0');
- p++;
- n--;
- }
- strncpy(buf, p, n);
- buf += n;
- if (q == NULL)
- break;
- *buf++ = DELIM;
- p = q+1;
- }
- }
- if (argv0_path) {
- *buf++ = DELIM;
- strcpy(buf, argv0_path);
- buf = strchr(buf, '\0');
- }
- *buf = '\0';
+ if (pythonhome == NULL) {
+ strcpy(buf, PYTHONPATH);
+ buf = strchr(buf, '\0');
+ }
+ else {
+ char *p = PYTHONPATH;
+ char *q;
+ int n;
+ for (;;) {
+ q = strchr(p, DELIM);
+ if (q == NULL)
+ n = strlen(p);
+ else
+ n = q-p;
+ if (p[0] == '.' && is_sep(p[1])) {
+ strcpy(buf, pythonhome);
+ buf = strchr(buf, '\0');
+ p++;
+ n--;
+ }
+ strncpy(buf, p, n);
+ buf += n;
+ if (q == NULL)
+ break;
+ *buf++ = DELIM;
+ p = q+1;
+ }
+ }
+ if (argv0_path) {
+ *buf++ = DELIM;
+ strcpy(buf, argv0_path);
+ buf = strchr(buf, '\0');
+ }
+ *buf = '\0';
}
@@ -448,35 +448,35 @@ calculate_path(void)
char *
Py_GetPath(void)
{
- if (!module_search_path)
- calculate_path();
+ if (!module_search_path)
+ calculate_path();
- return module_search_path;
+ return module_search_path;
}
char *
Py_GetPrefix(void)
{
- if (!module_search_path)
- calculate_path();
+ if (!module_search_path)
+ calculate_path();
- return prefix;
+ return prefix;
}
char *
Py_GetExecPrefix(void)
{
- if (!module_search_path)
- calculate_path();
+ if (!module_search_path)
+ calculate_path();
- return exec_prefix;
+ return exec_prefix;
}
char *
Py_GetProgramFullPath(void)
{
- if (!module_search_path)
- calculate_path();
+ if (!module_search_path)
+ calculate_path();
- return progpath;
+ return progpath;
}
diff --git a/PC/winreg.c b/PC/winreg.c
index 7e8ea10..e6d113d 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -4,7 +4,7 @@
Windows Registry access module for Python.
* Simple registry access written by Mark Hammond in win32api
- module circa 1995.
+ module circa 1995.
* Bill Tutt expanded the support significantly not long after.
* Numerous other people have submitted patches since then.
* Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
@@ -28,7 +28,7 @@ static char errNotAHandle[] = "Object is not a handle";
want to lose this info...
*/
#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
- PyErr_SetFromWindowsErr(rc)
+ PyErr_SetFromWindowsErr(rc)
/* Forward declares */
@@ -353,8 +353,8 @@ PyDoc_STRVAR(PyHKEY_Detach_doc,
************************************************************************/
typedef struct {
- PyObject_VAR_HEAD
- HKEY hkey;
+ PyObject_VAR_HEAD
+ HKEY hkey;
} PyHKEYObject;
#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
@@ -364,95 +364,95 @@ static char *failMsg = "bad operand type";
static PyObject *
PyHKEY_unaryFailureFunc(PyObject *ob)
{
- PyErr_SetString(PyExc_TypeError, failMsg);
- return NULL;
+ PyErr_SetString(PyExc_TypeError, failMsg);
+ return NULL;
}
static PyObject *
PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
{
- PyErr_SetString(PyExc_TypeError, failMsg);
- return NULL;
+ PyErr_SetString(PyExc_TypeError, failMsg);
+ return NULL;
}
static PyObject *
PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
{
- PyErr_SetString(PyExc_TypeError, failMsg);
- return NULL;
+ PyErr_SetString(PyExc_TypeError, failMsg);
+ return NULL;
}
static void
PyHKEY_deallocFunc(PyObject *ob)
{
- /* Can not call PyHKEY_Close, as the ob->tp_type
- has already been cleared, thus causing the type
- check to fail!
- */
- PyHKEYObject *obkey = (PyHKEYObject *)ob;
- if (obkey->hkey)
- RegCloseKey((HKEY)obkey->hkey);
- PyObject_DEL(ob);
+ /* Can not call PyHKEY_Close, as the ob->tp_type
+ has already been cleared, thus causing the type
+ check to fail!
+ */
+ PyHKEYObject *obkey = (PyHKEYObject *)ob;
+ if (obkey->hkey)
+ RegCloseKey((HKEY)obkey->hkey);
+ PyObject_DEL(ob);
}
static int
PyHKEY_boolFunc(PyObject *ob)
{
- return ((PyHKEYObject *)ob)->hkey != 0;
+ return ((PyHKEYObject *)ob)->hkey != 0;
}
static PyObject *
PyHKEY_intFunc(PyObject *ob)
{
- PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
- return PyLong_FromVoidPtr(pyhkey->hkey);
+ PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
+ return PyLong_FromVoidPtr(pyhkey->hkey);
}
static PyObject *
PyHKEY_strFunc(PyObject *ob)
{
- PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
- return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
+ PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
+ return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
}
static int
PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
{
- PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
- PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
- return pyhkey1 == pyhkey2 ? 0 :
- (pyhkey1 < pyhkey2 ? -1 : 1);
+ PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
+ PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
+ return pyhkey1 == pyhkey2 ? 0 :
+ (pyhkey1 < pyhkey2 ? -1 : 1);
}
static long
PyHKEY_hashFunc(PyObject *ob)
{
- /* Just use the address.
- XXX - should we use the handle value?
- */
- return _Py_HashPointer(ob);
+ /* Just use the address.
+ XXX - should we use the handle value?
+ */
+ return _Py_HashPointer(ob);
}
static PyNumberMethods PyHKEY_NumberMethods =
{
- PyHKEY_binaryFailureFunc, /* nb_add */
- PyHKEY_binaryFailureFunc, /* nb_subtract */
- PyHKEY_binaryFailureFunc, /* nb_multiply */
- PyHKEY_binaryFailureFunc, /* nb_remainder */
- PyHKEY_binaryFailureFunc, /* nb_divmod */
- PyHKEY_ternaryFailureFunc, /* nb_power */
- PyHKEY_unaryFailureFunc, /* nb_negative */
- PyHKEY_unaryFailureFunc, /* nb_positive */
- PyHKEY_unaryFailureFunc, /* nb_absolute */
- PyHKEY_boolFunc, /* nb_bool */
- PyHKEY_unaryFailureFunc, /* nb_invert */
- PyHKEY_binaryFailureFunc, /* nb_lshift */
- PyHKEY_binaryFailureFunc, /* nb_rshift */
- PyHKEY_binaryFailureFunc, /* nb_and */
- PyHKEY_binaryFailureFunc, /* nb_xor */
- PyHKEY_binaryFailureFunc, /* nb_or */
- PyHKEY_intFunc, /* nb_int */
- 0, /* nb_reserved */
- PyHKEY_unaryFailureFunc, /* nb_float */
+ PyHKEY_binaryFailureFunc, /* nb_add */
+ PyHKEY_binaryFailureFunc, /* nb_subtract */
+ PyHKEY_binaryFailureFunc, /* nb_multiply */
+ PyHKEY_binaryFailureFunc, /* nb_remainder */
+ PyHKEY_binaryFailureFunc, /* nb_divmod */
+ PyHKEY_ternaryFailureFunc, /* nb_power */
+ PyHKEY_unaryFailureFunc, /* nb_negative */
+ PyHKEY_unaryFailureFunc, /* nb_positive */
+ PyHKEY_unaryFailureFunc, /* nb_absolute */
+ PyHKEY_boolFunc, /* nb_bool */
+ PyHKEY_unaryFailureFunc, /* nb_invert */
+ PyHKEY_binaryFailureFunc, /* nb_lshift */
+ PyHKEY_binaryFailureFunc, /* nb_rshift */
+ PyHKEY_binaryFailureFunc, /* nb_and */
+ PyHKEY_binaryFailureFunc, /* nb_xor */
+ PyHKEY_binaryFailureFunc, /* nb_or */
+ PyHKEY_intFunc, /* nb_int */
+ 0, /* nb_reserved */
+ PyHKEY_unaryFailureFunc, /* nb_float */
};
static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
@@ -461,51 +461,51 @@ static PyObject *PyHKEY_Enter(PyObject *self);
static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
static struct PyMethodDef PyHKEY_methods[] = {
- {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
- {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
- {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
- {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
- {NULL}
+ {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
+ {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
+ {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
+ {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
+ {NULL}
};
#define OFF(e) offsetof(PyHKEYObject, e)
static PyMemberDef PyHKEY_memberlist[] = {
- {"handle", T_INT, OFF(hkey), READONLY},
- {NULL} /* Sentinel */
+ {"handle", T_INT, OFF(hkey), READONLY},
+ {NULL} /* Sentinel */
};
/* The type itself */
PyTypeObject PyHKEY_Type =
{
- PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
- "PyHKEY",
- sizeof(PyHKEYObject),
- 0,
- PyHKEY_deallocFunc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- &PyHKEY_NumberMethods, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- PyHKEY_hashFunc, /* tp_hash */
- 0, /* tp_call */
- PyHKEY_strFunc, /* tp_str */
- 0, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- 0, /* tp_flags */
- PyHKEY_doc, /* tp_doc */
- 0, /*tp_traverse*/
- 0, /*tp_clear*/
- 0, /*tp_richcompare*/
- 0, /*tp_weaklistoffset*/
- 0, /*tp_iter*/
- 0, /*tp_iternext*/
- PyHKEY_methods, /*tp_methods*/
- PyHKEY_memberlist, /*tp_members*/
+ PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
+ "PyHKEY",
+ sizeof(PyHKEYObject),
+ 0,
+ PyHKEY_deallocFunc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ &PyHKEY_NumberMethods, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ PyHKEY_hashFunc, /* tp_hash */
+ 0, /* tp_call */
+ PyHKEY_strFunc, /* tp_str */
+ 0, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ 0, /* tp_flags */
+ PyHKEY_doc, /* tp_doc */
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ PyHKEY_methods, /*tp_methods*/
+ PyHKEY_memberlist, /*tp_members*/
};
/************************************************************************
@@ -516,39 +516,39 @@ PyTypeObject PyHKEY_Type =
static PyObject *
PyHKEY_CloseMethod(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, ":Close"))
- return NULL;
- if (!PyHKEY_Close(self))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, ":Close"))
+ return NULL;
+ if (!PyHKEY_Close(self))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyHKEY_DetachMethod(PyObject *self, PyObject *args)
{
- void* ret;
- PyHKEYObject *pThis = (PyHKEYObject *)self;
- if (!PyArg_ParseTuple(args, ":Detach"))
- return NULL;
- ret = (void*)pThis->hkey;
- pThis->hkey = 0;
- return PyLong_FromVoidPtr(ret);
+ void* ret;
+ PyHKEYObject *pThis = (PyHKEYObject *)self;
+ if (!PyArg_ParseTuple(args, ":Detach"))
+ return NULL;
+ ret = (void*)pThis->hkey;
+ pThis->hkey = 0;
+ return PyLong_FromVoidPtr(ret);
}
static PyObject *
PyHKEY_Enter(PyObject *self)
{
- Py_XINCREF(self);
- return self;
+ Py_XINCREF(self);
+ return self;
}
static PyObject *
PyHKEY_Exit(PyObject *self, PyObject *args)
{
- if (!PyHKEY_Close(self))
- return NULL;
- Py_RETURN_NONE;
+ if (!PyHKEY_Close(self))
+ return NULL;
+ Py_RETURN_NONE;
}
@@ -558,74 +558,74 @@ PyHKEY_Exit(PyObject *self, PyObject *args)
PyObject *
PyHKEY_New(HKEY hInit)
{
- PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
- if (key)
- key->hkey = hInit;
- return (PyObject *)key;
+ PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
+ if (key)
+ key->hkey = hInit;
+ return (PyObject *)key;
}
BOOL
PyHKEY_Close(PyObject *ob_handle)
{
- LONG rc;
- PyHKEYObject *key;
-
- if (!PyHKEY_Check(ob_handle)) {
- PyErr_SetString(PyExc_TypeError, "bad operand type");
- return FALSE;
- }
- key = (PyHKEYObject *)ob_handle;
- rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
- key->hkey = 0;
- if (rc != ERROR_SUCCESS)
- PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
- return rc == ERROR_SUCCESS;
+ LONG rc;
+ PyHKEYObject *key;
+
+ if (!PyHKEY_Check(ob_handle)) {
+ PyErr_SetString(PyExc_TypeError, "bad operand type");
+ return FALSE;
+ }
+ key = (PyHKEYObject *)ob_handle;
+ rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
+ key->hkey = 0;
+ if (rc != ERROR_SUCCESS)
+ PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
+ return rc == ERROR_SUCCESS;
}
BOOL
PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
{
- if (ob == Py_None) {
- if (!bNoneOK) {
- PyErr_SetString(
- PyExc_TypeError,
- "None is not a valid HKEY in this context");
- return FALSE;
- }
- *pHANDLE = (HKEY)0;
- }
- else if (PyHKEY_Check(ob)) {
- PyHKEYObject *pH = (PyHKEYObject *)ob;
- *pHANDLE = pH->hkey;
- }
- else if (PyLong_Check(ob)) {
- /* We also support integers */
- PyErr_Clear();
- *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
- if (PyErr_Occurred())
- return FALSE;
- }
- else {
- PyErr_SetString(
- PyExc_TypeError,
- "The object is not a PyHKEY object");
- return FALSE;
- }
- return TRUE;
+ if (ob == Py_None) {
+ if (!bNoneOK) {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "None is not a valid HKEY in this context");
+ return FALSE;
+ }
+ *pHANDLE = (HKEY)0;
+ }
+ else if (PyHKEY_Check(ob)) {
+ PyHKEYObject *pH = (PyHKEYObject *)ob;
+ *pHANDLE = pH->hkey;
+ }
+ else if (PyLong_Check(ob)) {
+ /* We also support integers */
+ PyErr_Clear();
+ *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
+ if (PyErr_Occurred())
+ return FALSE;
+ }
+ else {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "The object is not a PyHKEY object");
+ return FALSE;
+ }
+ return TRUE;
}
PyObject *
PyHKEY_FromHKEY(HKEY h)
{
- PyHKEYObject *op;
-
- /* Inline PyObject_New */
- op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_INIT(op, &PyHKEY_Type);
- op->hkey = h;
- return (PyObject *)op;
+ PyHKEYObject *op;
+
+ /* Inline PyObject_New */
+ op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
+ if (op == NULL)
+ return PyErr_NoMemory();
+ PyObject_INIT(op, &PyHKEY_Type);
+ op->hkey = h;
+ return (PyObject *)op;
}
@@ -635,32 +635,32 @@ PyHKEY_FromHKEY(HKEY h)
BOOL
PyWinObject_CloseHKEY(PyObject *obHandle)
{
- BOOL ok;
- if (PyHKEY_Check(obHandle)) {
- ok = PyHKEY_Close(obHandle);
- }
+ BOOL ok;
+ if (PyHKEY_Check(obHandle)) {
+ ok = PyHKEY_Close(obHandle);
+ }
#if SIZEOF_LONG >= SIZEOF_HKEY
- else if (PyLong_Check(obHandle)) {
- long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
- ok = (rc == ERROR_SUCCESS);
- if (!ok)
- PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
- }
+ else if (PyLong_Check(obHandle)) {
+ long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
+ ok = (rc == ERROR_SUCCESS);
+ if (!ok)
+ PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
+ }
#else
- else if (PyLong_Check(obHandle)) {
- long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
- ok = (rc == ERROR_SUCCESS);
- if (!ok)
- PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
- }
+ else if (PyLong_Check(obHandle)) {
+ long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
+ ok = (rc == ERROR_SUCCESS);
+ if (!ok)
+ PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
+ }
#endif
- else {
- PyErr_SetString(
- PyExc_TypeError,
- "A handle must be a HKEY object or an integer");
- return FALSE;
- }
- return ok;
+ else {
+ PyErr_SetString(
+ PyExc_TypeError,
+ "A handle must be a HKEY object or an integer");
+ return FALSE;
+ }
+ return ok;
}
@@ -677,29 +677,29 @@ PyWinObject_CloseHKEY(PyObject *obHandle)
static void
fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
{
- wchar_t *P;
- int i;
- wchar_t *Q;
-
- Q = data + len;
- for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
- str[i] = P;
- for(; *P != '\0'; P++)
- ;
- }
+ wchar_t *P;
+ int i;
+ wchar_t *Q;
+
+ Q = data + len;
+ for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
+ str[i] = P;
+ for(; *P != '\0'; P++)
+ ;
+ }
}
static int
countStrings(wchar_t *data, int len)
{
- int strings;
- wchar_t *P;
- wchar_t *Q = data + len;
-
- for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
- for (; P < Q && *P != '\0'; P++)
- ;
- return strings;
+ int strings;
+ wchar_t *P;
+ wchar_t *Q = data + len;
+
+ for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
+ for (; P < Q && *P != '\0'; P++)
+ ;
+ return strings;
}
/* Convert PyObject into Registry data.
@@ -707,200 +707,200 @@ countStrings(wchar_t *data, int len)
static BOOL
Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
{
- Py_ssize_t i,j;
- switch (typ) {
- case REG_DWORD:
- if (value != Py_None && !PyLong_Check(value))
- return FALSE;
- *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
- if (*retDataBuf==NULL){
- PyErr_NoMemory();
- return FALSE;
- }
- *retDataSize = sizeof(DWORD);
- if (value == Py_None) {
- DWORD zero = 0;
- memcpy(*retDataBuf, &zero, sizeof(DWORD));
- }
- else {
- DWORD d = PyLong_AsLong(value);
- memcpy(*retDataBuf, &d, sizeof(DWORD));
- }
- break;
- case REG_SZ:
- case REG_EXPAND_SZ:
- {
- if (value == Py_None)
- *retDataSize = 1;
- else {
- if (!PyUnicode_Check(value))
- return FALSE;
-
- *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
- }
- *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
- if (*retDataBuf==NULL){
- PyErr_NoMemory();
- return FALSE;
- }
- if (value == Py_None)
- wcscpy((wchar_t *)*retDataBuf, L"");
- else
- wcscpy((wchar_t *)*retDataBuf,
- PyUnicode_AS_UNICODE(value));
- break;
- }
- case REG_MULTI_SZ:
- {
- DWORD size = 0;
- wchar_t *P;
-
- if (value == Py_None)
- i = 0;
- else {
- if (!PyList_Check(value))
- return FALSE;
- i = PyList_Size(value);
- }
- for (j = 0; j < i; j++)
- {
- PyObject *t;
- t = PyList_GET_ITEM(value, j);
- if (!PyUnicode_Check(t))
- return FALSE;
- size += 2 + PyUnicode_GET_DATA_SIZE(t);
- }
-
- *retDataSize = size + 2;
- *retDataBuf = (BYTE *)PyMem_NEW(char,
- *retDataSize);
- if (*retDataBuf==NULL){
- PyErr_NoMemory();
- return FALSE;
- }
- P = (wchar_t *)*retDataBuf;
-
- for (j = 0; j < i; j++)
- {
- PyObject *t;
- t = PyList_GET_ITEM(value, j);
- wcscpy(P, PyUnicode_AS_UNICODE(t));
- P += 1 + wcslen(
- PyUnicode_AS_UNICODE(t));
- }
- /* And doubly-terminate the list... */
- *P = '\0';
- break;
- }
- case REG_BINARY:
- /* ALSO handle ALL unknown data types here. Even if we can't
- support it natively, we should handle the bits. */
- default:
- if (value == Py_None)
- *retDataSize = 0;
- else {
- Py_buffer view;
-
- if (!PyObject_CheckBuffer(value)) {
- PyErr_Format(PyExc_TypeError,
- "Objects of type '%s' can not "
- "be used as binary registry values",
- value->ob_type->tp_name);
- return FALSE;
- }
-
- if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
- return FALSE;
-
- *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
- if (*retDataBuf==NULL){
- PyBuffer_Release(&view);
- PyErr_NoMemory();
- return FALSE;
- }
- *retDataSize = view.len;
- memcpy(*retDataBuf, view.buf, view.len);
- PyBuffer_Release(&view);
- }
- break;
- }
- return TRUE;
+ Py_ssize_t i,j;
+ switch (typ) {
+ case REG_DWORD:
+ if (value != Py_None && !PyLong_Check(value))
+ return FALSE;
+ *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
+ if (*retDataBuf==NULL){
+ PyErr_NoMemory();
+ return FALSE;
+ }
+ *retDataSize = sizeof(DWORD);
+ if (value == Py_None) {
+ DWORD zero = 0;
+ memcpy(*retDataBuf, &zero, sizeof(DWORD));
+ }
+ else {
+ DWORD d = PyLong_AsLong(value);
+ memcpy(*retDataBuf, &d, sizeof(DWORD));
+ }
+ break;
+ case REG_SZ:
+ case REG_EXPAND_SZ:
+ {
+ if (value == Py_None)
+ *retDataSize = 1;
+ else {
+ if (!PyUnicode_Check(value))
+ return FALSE;
+
+ *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
+ }
+ *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
+ if (*retDataBuf==NULL){
+ PyErr_NoMemory();
+ return FALSE;
+ }
+ if (value == Py_None)
+ wcscpy((wchar_t *)*retDataBuf, L"");
+ else
+ wcscpy((wchar_t *)*retDataBuf,
+ PyUnicode_AS_UNICODE(value));
+ break;
+ }
+ case REG_MULTI_SZ:
+ {
+ DWORD size = 0;
+ wchar_t *P;
+
+ if (value == Py_None)
+ i = 0;
+ else {
+ if (!PyList_Check(value))
+ return FALSE;
+ i = PyList_Size(value);
+ }
+ for (j = 0; j < i; j++)
+ {
+ PyObject *t;
+ t = PyList_GET_ITEM(value, j);
+ if (!PyUnicode_Check(t))
+ return FALSE;
+ size += 2 + PyUnicode_GET_DATA_SIZE(t);
+ }
+
+ *retDataSize = size + 2;
+ *retDataBuf = (BYTE *)PyMem_NEW(char,
+ *retDataSize);
+ if (*retDataBuf==NULL){
+ PyErr_NoMemory();
+ return FALSE;
+ }
+ P = (wchar_t *)*retDataBuf;
+
+ for (j = 0; j < i; j++)
+ {
+ PyObject *t;
+ t = PyList_GET_ITEM(value, j);
+ wcscpy(P, PyUnicode_AS_UNICODE(t));
+ P += 1 + wcslen(
+ PyUnicode_AS_UNICODE(t));
+ }
+ /* And doubly-terminate the list... */
+ *P = '\0';
+ break;
+ }
+ case REG_BINARY:
+ /* ALSO handle ALL unknown data types here. Even if we can't
+ support it natively, we should handle the bits. */
+ default:
+ if (value == Py_None)
+ *retDataSize = 0;
+ else {
+ Py_buffer view;
+
+ if (!PyObject_CheckBuffer(value)) {
+ PyErr_Format(PyExc_TypeError,
+ "Objects of type '%s' can not "
+ "be used as binary registry values",
+ value->ob_type->tp_name);
+ return FALSE;
+ }
+
+ if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
+ return FALSE;
+
+ *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
+ if (*retDataBuf==NULL){
+ PyBuffer_Release(&view);
+ PyErr_NoMemory();
+ return FALSE;
+ }
+ *retDataSize = view.len;
+ memcpy(*retDataBuf, view.buf, view.len);
+ PyBuffer_Release(&view);
+ }
+ break;
+ }
+ return TRUE;
}
/* Convert Registry data into PyObject*/
static PyObject *
Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
{
- PyObject *obData;
-
- switch (typ) {
- case REG_DWORD:
- if (retDataSize == 0)
- obData = PyLong_FromLong(0);
- else
- obData = PyLong_FromLong(*(int *)retDataBuf);
- break;
- case REG_SZ:
- case REG_EXPAND_SZ:
- {
- /* the buffer may or may not have a trailing NULL */
- wchar_t *data = (wchar_t *)retDataBuf;
- int len = retDataSize / 2;
- if (retDataSize && data[len-1] == '\0')
- retDataSize -= 2;
- if (retDataSize <= 0)
- data = L"";
- obData = PyUnicode_FromUnicode(data, retDataSize/2);
- break;
- }
- case REG_MULTI_SZ:
- if (retDataSize == 0)
- obData = PyList_New(0);
- else
- {
- int index = 0;
- wchar_t *data = (wchar_t *)retDataBuf;
- int len = retDataSize / 2;
- int s = countStrings(data, len);
- wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
- if (str == NULL)
- return PyErr_NoMemory();
-
- fixupMultiSZ(str, data, len);
- obData = PyList_New(s);
- if (obData == NULL)
- return NULL;
- for (index = 0; index < s; index++)
- {
- size_t len = wcslen(str[index]);
- if (len > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "registry string is too long for a Python string");
- Py_DECREF(obData);
- return NULL;
- }
- PyList_SetItem(obData,
- index,
- PyUnicode_FromUnicode(str[index], len));
- }
- free(str);
-
- break;
- }
- case REG_BINARY:
- /* ALSO handle ALL unknown data types here. Even if we can't
- support it natively, we should handle the bits. */
- default:
- if (retDataSize == 0) {
- Py_INCREF(Py_None);
- obData = Py_None;
- }
- else
- obData = PyBytes_FromStringAndSize(
- (char *)retDataBuf, retDataSize);
- break;
- }
- return obData;
+ PyObject *obData;
+
+ switch (typ) {
+ case REG_DWORD:
+ if (retDataSize == 0)
+ obData = PyLong_FromLong(0);
+ else
+ obData = PyLong_FromLong(*(int *)retDataBuf);
+ break;
+ case REG_SZ:
+ case REG_EXPAND_SZ:
+ {
+ /* the buffer may or may not have a trailing NULL */
+ wchar_t *data = (wchar_t *)retDataBuf;
+ int len = retDataSize / 2;
+ if (retDataSize && data[len-1] == '\0')
+ retDataSize -= 2;
+ if (retDataSize <= 0)
+ data = L"";
+ obData = PyUnicode_FromUnicode(data, retDataSize/2);
+ break;
+ }
+ case REG_MULTI_SZ:
+ if (retDataSize == 0)
+ obData = PyList_New(0);
+ else
+ {
+ int index = 0;
+ wchar_t *data = (wchar_t *)retDataBuf;
+ int len = retDataSize / 2;
+ int s = countStrings(data, len);
+ wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
+ if (str == NULL)
+ return PyErr_NoMemory();
+
+ fixupMultiSZ(str, data, len);
+ obData = PyList_New(s);
+ if (obData == NULL)
+ return NULL;
+ for (index = 0; index < s; index++)
+ {
+ size_t len = wcslen(str[index]);
+ if (len > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "registry string is too long for a Python string");
+ Py_DECREF(obData);
+ return NULL;
+ }
+ PyList_SetItem(obData,
+ index,
+ PyUnicode_FromUnicode(str[index], len));
+ }
+ free(str);
+
+ break;
+ }
+ case REG_BINARY:
+ /* ALSO handle ALL unknown data types here. Even if we can't
+ support it natively, we should handle the bits. */
+ default:
+ if (retDataSize == 0) {
+ Py_INCREF(Py_None);
+ obData = Py_None;
+ }
+ else
+ obData = PyBytes_FromStringAndSize(
+ (char *)retDataBuf, retDataSize);
+ break;
+ }
+ return obData;
}
/* The Python methods */
@@ -908,281 +908,281 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
static PyObject *
PyCloseKey(PyObject *self, PyObject *args)
{
- PyObject *obKey;
- if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
- return NULL;
- if (!PyHKEY_Close(obKey))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *obKey;
+ if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
+ return NULL;
+ if (!PyHKEY_Close(obKey))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyConnectRegistry(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *szCompName = NULL;
- HKEY retKey;
- long rc;
- if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- rc = RegConnectRegistryW(szCompName, hKey, &retKey);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "ConnectRegistry");
- return PyHKEY_FromHKEY(retKey);
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *szCompName = NULL;
+ HKEY retKey;
+ long rc;
+ if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegConnectRegistryW(szCompName, hKey, &retKey);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "ConnectRegistry");
+ return PyHKEY_FromHKEY(retKey);
}
static PyObject *
PyCreateKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- HKEY retKey;
- long rc;
- if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- rc = RegCreateKeyW(hKey, subKey, &retKey);
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
- return PyHKEY_FromHKEY(retKey);
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ HKEY retKey;
+ long rc;
+ if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ rc = RegCreateKeyW(hKey, subKey, &retKey);
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
+ return PyHKEY_FromHKEY(retKey);
}
static PyObject *
PyDeleteKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- long rc;
- if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- rc = RegDeleteKeyW(hKey, subKey );
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ long rc;
+ if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ rc = RegDeleteKeyW(hKey, subKey );
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyDeleteValue(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- long rc;
- if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- rc = RegDeleteValueW(hKey, subKey);
- Py_END_ALLOW_THREADS
- if (rc !=ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegDeleteValue");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ long rc;
+ if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegDeleteValueW(hKey, subKey);
+ Py_END_ALLOW_THREADS
+ if (rc !=ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegDeleteValue");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyEnumKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- int index;
- long rc;
- PyObject *retStr;
- wchar_t tmpbuf[256]; /* max key name length is 255 */
- DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
-
- if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
-
- retStr = PyUnicode_FromUnicode(tmpbuf, len);
- return retStr; /* can be NULL */
+ HKEY hKey;
+ PyObject *obKey;
+ int index;
+ long rc;
+ PyObject *retStr;
+ wchar_t tmpbuf[256]; /* max key name length is 255 */
+ DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
+
+ if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
+
+ retStr = PyUnicode_FromUnicode(tmpbuf, len);
+ return retStr; /* can be NULL */
}
static PyObject *
PyEnumValue(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- int index;
- long rc;
- wchar_t *retValueBuf;
- BYTE *retDataBuf;
- DWORD retValueSize;
- DWORD retDataSize;
- DWORD typ;
- PyObject *obData;
- PyObject *retVal;
-
- if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL,
- &retValueSize, &retDataSize, NULL, NULL))
- != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryInfoKey");
- ++retValueSize; /* include null terminators */
- ++retDataSize;
- retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
- if (retValueBuf == NULL)
- return PyErr_NoMemory();
- retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
- if (retDataBuf == NULL) {
- PyMem_Free(retValueBuf);
- return PyErr_NoMemory();
- }
-
- Py_BEGIN_ALLOW_THREADS
- rc = RegEnumValueW(hKey,
- index,
- retValueBuf,
- &retValueSize,
- NULL,
- &typ,
- retDataBuf,
- &retDataSize);
- Py_END_ALLOW_THREADS
-
- if (rc != ERROR_SUCCESS) {
- retVal = PyErr_SetFromWindowsErrWithFunction(rc,
- "PyRegEnumValue");
- goto fail;
- }
- obData = Reg2Py(retDataBuf, retDataSize, typ);
- if (obData == NULL) {
- retVal = NULL;
- goto fail;
- }
- retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
- Py_DECREF(obData);
+ HKEY hKey;
+ PyObject *obKey;
+ int index;
+ long rc;
+ wchar_t *retValueBuf;
+ BYTE *retDataBuf;
+ DWORD retValueSize;
+ DWORD retDataSize;
+ DWORD typ;
+ PyObject *obData;
+ PyObject *retVal;
+
+ if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
+ NULL,
+ &retValueSize, &retDataSize, NULL, NULL))
+ != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryInfoKey");
+ ++retValueSize; /* include null terminators */
+ ++retDataSize;
+ retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
+ if (retValueBuf == NULL)
+ return PyErr_NoMemory();
+ retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
+ if (retDataBuf == NULL) {
+ PyMem_Free(retValueBuf);
+ return PyErr_NoMemory();
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegEnumValueW(hKey,
+ index,
+ retValueBuf,
+ &retValueSize,
+ NULL,
+ &typ,
+ retDataBuf,
+ &retDataSize);
+ Py_END_ALLOW_THREADS
+
+ if (rc != ERROR_SUCCESS) {
+ retVal = PyErr_SetFromWindowsErrWithFunction(rc,
+ "PyRegEnumValue");
+ goto fail;
+ }
+ obData = Reg2Py(retDataBuf, retDataSize, typ);
+ if (obData == NULL) {
+ retVal = NULL;
+ goto fail;
+ }
+ retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
+ Py_DECREF(obData);
fail:
- PyMem_Free(retValueBuf);
- PyMem_Free(retDataBuf);
- return retVal;
+ PyMem_Free(retValueBuf);
+ PyMem_Free(retDataBuf);
+ return retVal;
}
static PyObject *
PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
{
- Py_UNICODE *retValue = NULL;
- Py_UNICODE *src;
- DWORD retValueSize;
- DWORD rc;
- PyObject *o;
-
- if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
- return NULL;
-
- retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
- if (retValueSize == 0) {
- return PyErr_SetFromWindowsErrWithFunction(retValueSize,
- "ExpandEnvironmentStrings");
- }
- retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
- if (retValue == NULL) {
- return PyErr_NoMemory();
- }
-
- rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
- if (rc == 0) {
- PyMem_Free(retValue);
- return PyErr_SetFromWindowsErrWithFunction(retValueSize,
- "ExpandEnvironmentStrings");
- }
- o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
- PyMem_Free(retValue);
- return o;
+ Py_UNICODE *retValue = NULL;
+ Py_UNICODE *src;
+ DWORD retValueSize;
+ DWORD rc;
+ PyObject *o;
+
+ if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
+ return NULL;
+
+ retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
+ if (retValueSize == 0) {
+ return PyErr_SetFromWindowsErrWithFunction(retValueSize,
+ "ExpandEnvironmentStrings");
+ }
+ retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
+ if (retValue == NULL) {
+ return PyErr_NoMemory();
+ }
+
+ rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
+ if (rc == 0) {
+ PyMem_Free(retValue);
+ return PyErr_SetFromWindowsErrWithFunction(retValueSize,
+ "ExpandEnvironmentStrings");
+ }
+ o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
+ PyMem_Free(retValue);
+ return o;
}
static PyObject *
PyFlushKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- long rc;
- if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- rc = RegFlushKey(hKey);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ long rc;
+ if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegFlushKey(hKey);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyLoadKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- wchar_t *fileName;
-
- long rc;
- if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- Py_BEGIN_ALLOW_THREADS
- rc = RegLoadKeyW(hKey, subKey, fileName );
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ wchar_t *fileName;
+
+ long rc;
+ if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegLoadKeyW(hKey, subKey, fileName );
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyOpenKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
-
- wchar_t *subKey;
- int res = 0;
- HKEY retKey;
- long rc;
- REGSAM sam = KEY_READ;
- if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
- &res, &sam))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- Py_BEGIN_ALLOW_THREADS
- rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
- return PyHKEY_FromHKEY(retKey);
+ HKEY hKey;
+ PyObject *obKey;
+
+ wchar_t *subKey;
+ int res = 0;
+ HKEY retKey;
+ long rc;
+ REGSAM sam = KEY_READ;
+ if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
+ &res, &sam))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
+ return PyHKEY_FromHKEY(retKey);
}
@@ -1198,18 +1198,18 @@ PyQueryInfoKey(PyObject *self, PyObject *args)
PyObject *l;
PyObject *ret;
if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
- return NULL;
+ return NULL;
if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
+ return NULL;
if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
- &nValues, NULL, NULL, NULL, &ft))
+ &nValues, NULL, NULL, NULL, &ft))
!= ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
l = PyLong_FromLongLong(li.QuadPart);
if (l == NULL)
- return NULL;
+ return NULL;
ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
Py_DECREF(l);
return ret;
@@ -1218,326 +1218,326 @@ PyQueryInfoKey(PyObject *self, PyObject *args)
static PyObject *
PyQueryValue(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- long rc;
- PyObject *retStr;
- wchar_t *retBuf;
- long bufSize = 0;
-
- if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
- return NULL;
-
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
- != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryValue");
- retBuf = (wchar_t *)PyMem_Malloc(bufSize);
- if (retBuf == NULL)
- return PyErr_NoMemory();
-
- if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
- != ERROR_SUCCESS) {
- PyMem_Free(retBuf);
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryValue");
- }
-
- retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
- PyMem_Free(retBuf);
- return retStr;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ long rc;
+ PyObject *retStr;
+ wchar_t *retBuf;
+ long bufSize = 0;
+
+ if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
+ return NULL;
+
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
+ != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryValue");
+ retBuf = (wchar_t *)PyMem_Malloc(bufSize);
+ if (retBuf == NULL)
+ return PyErr_NoMemory();
+
+ if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
+ != ERROR_SUCCESS) {
+ PyMem_Free(retBuf);
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryValue");
+ }
+
+ retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
+ PyMem_Free(retBuf);
+ return retStr;
}
static PyObject *
PyQueryValueEx(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *valueName;
-
- long rc;
- BYTE *retBuf;
- DWORD bufSize = 0;
- DWORD typ;
- PyObject *obData;
- PyObject *result;
-
- if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
- return NULL;
-
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- if ((rc = RegQueryValueExW(hKey, valueName,
- NULL, NULL, NULL,
- &bufSize))
- != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryValueEx");
- retBuf = (BYTE *)PyMem_Malloc(bufSize);
- if (retBuf == NULL)
- return PyErr_NoMemory();
- if ((rc = RegQueryValueExW(hKey, valueName, NULL,
- &typ, retBuf, &bufSize))
- != ERROR_SUCCESS) {
- PyMem_Free(retBuf);
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryValueEx");
- }
- obData = Reg2Py(retBuf, bufSize, typ);
- PyMem_Free(retBuf);
- if (obData == NULL)
- return NULL;
- result = Py_BuildValue("Oi", obData, typ);
- Py_DECREF(obData);
- return result;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *valueName;
+
+ long rc;
+ BYTE *retBuf;
+ DWORD bufSize = 0;
+ DWORD typ;
+ PyObject *obData;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
+ return NULL;
+
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ if ((rc = RegQueryValueExW(hKey, valueName,
+ NULL, NULL, NULL,
+ &bufSize))
+ != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryValueEx");
+ retBuf = (BYTE *)PyMem_Malloc(bufSize);
+ if (retBuf == NULL)
+ return PyErr_NoMemory();
+ if ((rc = RegQueryValueExW(hKey, valueName, NULL,
+ &typ, retBuf, &bufSize))
+ != ERROR_SUCCESS) {
+ PyMem_Free(retBuf);
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryValueEx");
+ }
+ obData = Reg2Py(retBuf, bufSize, typ);
+ PyMem_Free(retBuf);
+ if (obData == NULL)
+ return NULL;
+ result = Py_BuildValue("Oi", obData, typ);
+ Py_DECREF(obData);
+ return result;
}
static PyObject *
PySaveKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *fileName;
- LPSECURITY_ATTRIBUTES pSA = NULL;
-
- long rc;
- if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *fileName;
+ LPSECURITY_ATTRIBUTES pSA = NULL;
+
+ long rc;
+ if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
/* One day we may get security into the core?
- if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
- return NULL;
+ if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
+ return NULL;
*/
- Py_BEGIN_ALLOW_THREADS
- rc = RegSaveKeyW(hKey, fileName, pSA );
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
- Py_INCREF(Py_None);
- return Py_None;
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegSaveKeyW(hKey, fileName, pSA );
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PySetValue(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- wchar_t *subKey;
- wchar_t *str;
- DWORD typ;
- DWORD len;
- long rc;
- if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
- &obKey,
- &subKey,
- &typ,
- &str,
- &len))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- if (typ != REG_SZ) {
- PyErr_SetString(PyExc_TypeError,
- "Type must be winreg.REG_SZ");
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ wchar_t *subKey;
+ wchar_t *str;
+ DWORD typ;
+ DWORD len;
+ long rc;
+ if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
+ &obKey,
+ &subKey,
+ &typ,
+ &str,
+ &len))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ if (typ != REG_SZ) {
+ PyErr_SetString(PyExc_TypeError,
+ "Type must be winreg.REG_SZ");
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PySetValueEx(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- Py_UNICODE *valueName;
- PyObject *obRes;
- PyObject *value;
- BYTE *data;
- DWORD len;
- DWORD typ;
-
- LONG rc;
-
- if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
- &obKey,
- &valueName,
- &obRes,
- &typ,
- &value))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
- if (!Py2Reg(value, typ, &data, &len))
- {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_ValueError,
- "Could not convert the data to the specified type.");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
- Py_END_ALLOW_THREADS
- PyMem_DEL(data);
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegSetValueEx");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ Py_UNICODE *valueName;
+ PyObject *obRes;
+ PyObject *value;
+ BYTE *data;
+ DWORD len;
+ DWORD typ;
+
+ LONG rc;
+
+ if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
+ &obKey,
+ &valueName,
+ &obRes,
+ &typ,
+ &value))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+ if (!Py2Reg(value, typ, &data, &len))
+ {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_ValueError,
+ "Could not convert the data to the specified type.");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
+ Py_END_ALLOW_THREADS
+ PyMem_DEL(data);
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegSetValueEx");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyDisableReflectionKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- HMODULE hMod;
- typedef LONG (WINAPI *RDRKFunc)(HKEY);
- RDRKFunc pfn = NULL;
- LONG rc;
-
- if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- // Only available on 64bit platforms, so we must load it
- // dynamically.
- hMod = GetModuleHandle("advapi32.dll");
- if (hMod)
- pfn = (RDRKFunc)GetProcAddress(hMod,
- "RegDisableReflectionKey");
- if (!pfn) {
- PyErr_SetString(PyExc_NotImplementedError,
- "not implemented on this platform");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- rc = (*pfn)(hKey);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegDisableReflectionKey");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ HMODULE hMod;
+ typedef LONG (WINAPI *RDRKFunc)(HKEY);
+ RDRKFunc pfn = NULL;
+ LONG rc;
+
+ if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ // Only available on 64bit platforms, so we must load it
+ // dynamically.
+ hMod = GetModuleHandle("advapi32.dll");
+ if (hMod)
+ pfn = (RDRKFunc)GetProcAddress(hMod,
+ "RegDisableReflectionKey");
+ if (!pfn) {
+ PyErr_SetString(PyExc_NotImplementedError,
+ "not implemented on this platform");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ rc = (*pfn)(hKey);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegDisableReflectionKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyEnableReflectionKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- HMODULE hMod;
- typedef LONG (WINAPI *RERKFunc)(HKEY);
- RERKFunc pfn = NULL;
- LONG rc;
-
- if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- // Only available on 64bit platforms, so we must load it
- // dynamically.
- hMod = GetModuleHandle("advapi32.dll");
- if (hMod)
- pfn = (RERKFunc)GetProcAddress(hMod,
- "RegEnableReflectionKey");
- if (!pfn) {
- PyErr_SetString(PyExc_NotImplementedError,
- "not implemented on this platform");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- rc = (*pfn)(hKey);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegEnableReflectionKey");
- Py_INCREF(Py_None);
- return Py_None;
+ HKEY hKey;
+ PyObject *obKey;
+ HMODULE hMod;
+ typedef LONG (WINAPI *RERKFunc)(HKEY);
+ RERKFunc pfn = NULL;
+ LONG rc;
+
+ if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ // Only available on 64bit platforms, so we must load it
+ // dynamically.
+ hMod = GetModuleHandle("advapi32.dll");
+ if (hMod)
+ pfn = (RERKFunc)GetProcAddress(hMod,
+ "RegEnableReflectionKey");
+ if (!pfn) {
+ PyErr_SetString(PyExc_NotImplementedError,
+ "not implemented on this platform");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ rc = (*pfn)(hKey);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegEnableReflectionKey");
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
PyQueryReflectionKey(PyObject *self, PyObject *args)
{
- HKEY hKey;
- PyObject *obKey;
- HMODULE hMod;
- typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
- RQRKFunc pfn = NULL;
- BOOL result;
- LONG rc;
-
- if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
- return NULL;
- if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
- return NULL;
-
- // Only available on 64bit platforms, so we must load it
- // dynamically.
- hMod = GetModuleHandle("advapi32.dll");
- if (hMod)
- pfn = (RQRKFunc)GetProcAddress(hMod,
- "RegQueryReflectionKey");
- if (!pfn) {
- PyErr_SetString(PyExc_NotImplementedError,
- "not implemented on this platform");
- return NULL;
- }
- Py_BEGIN_ALLOW_THREADS
- rc = (*pfn)(hKey, &result);
- Py_END_ALLOW_THREADS
- if (rc != ERROR_SUCCESS)
- return PyErr_SetFromWindowsErrWithFunction(rc,
- "RegQueryReflectionKey");
- return PyBool_FromLong(rc);
+ HKEY hKey;
+ PyObject *obKey;
+ HMODULE hMod;
+ typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
+ RQRKFunc pfn = NULL;
+ BOOL result;
+ LONG rc;
+
+ if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
+ return NULL;
+ if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
+ return NULL;
+
+ // Only available on 64bit platforms, so we must load it
+ // dynamically.
+ hMod = GetModuleHandle("advapi32.dll");
+ if (hMod)
+ pfn = (RQRKFunc)GetProcAddress(hMod,
+ "RegQueryReflectionKey");
+ if (!pfn) {
+ PyErr_SetString(PyExc_NotImplementedError,
+ "not implemented on this platform");
+ return NULL;
+ }
+ Py_BEGIN_ALLOW_THREADS
+ rc = (*pfn)(hKey, &result);
+ Py_END_ALLOW_THREADS
+ if (rc != ERROR_SUCCESS)
+ return PyErr_SetFromWindowsErrWithFunction(rc,
+ "RegQueryReflectionKey");
+ return PyBool_FromLong(rc);
}
static struct PyMethodDef winreg_methods[] = {
- {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
- {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
- {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
- {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
- {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
- {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
- {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
- {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
- {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
- {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
- ExpandEnvironmentStrings_doc },
- {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
- {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
- {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
- {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
- {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
- {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
- {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
- {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
- {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
- {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
- {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
- NULL,
+ {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
+ {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
+ {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
+ {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
+ {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
+ {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
+ {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
+ {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
+ {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
+ {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
+ ExpandEnvironmentStrings_doc },
+ {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
+ {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
+ {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
+ {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
+ {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
+ {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
+ {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
+ {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
+ {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
+ {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
+ {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
+ NULL,
};
static void
insint(PyObject * d, char * name, long value)
{
- PyObject *v = PyLong_FromLong(value);
- if (!v || PyDict_SetItemString(d, name, v))
- PyErr_Clear();
- Py_XDECREF(v);
+ PyObject *v = PyLong_FromLong(value);
+ if (!v || PyDict_SetItemString(d, name, v))
+ PyErr_Clear();
+ Py_XDECREF(v);
}
#define ADD_INT(val) insint(d, #val, val)
@@ -1545,104 +1545,104 @@ insint(PyObject * d, char * name, long value)
static void
inskey(PyObject * d, char * name, HKEY key)
{
- PyObject *v = PyLong_FromVoidPtr(key);
- if (!v || PyDict_SetItemString(d, name, v))
- PyErr_Clear();
- Py_XDECREF(v);
+ PyObject *v = PyLong_FromVoidPtr(key);
+ if (!v || PyDict_SetItemString(d, name, v))
+ PyErr_Clear();
+ Py_XDECREF(v);
}
#define ADD_KEY(val) inskey(d, #val, val)
static struct PyModuleDef winregmodule = {
- PyModuleDef_HEAD_INIT,
- "winreg",
- module_doc,
- -1,
- winreg_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "winreg",
+ module_doc,
+ -1,
+ winreg_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC PyInit_winreg(void)
{
- PyObject *m, *d;
- m = PyModule_Create(&winregmodule);
- if (m == NULL)
- return NULL;
- d = PyModule_GetDict(m);
- PyHKEY_Type.tp_doc = PyHKEY_doc;
- if (PyType_Ready(&PyHKEY_Type) < 0)
- return NULL;
- Py_INCREF(&PyHKEY_Type);
- if (PyDict_SetItemString(d, "HKEYType",
- (PyObject *)&PyHKEY_Type) != 0)
- return NULL;
- Py_INCREF(PyExc_WindowsError);
- if (PyDict_SetItemString(d, "error",
- PyExc_WindowsError) != 0)
- return NULL;
-
- /* Add the relevant constants */
- ADD_KEY(HKEY_CLASSES_ROOT);
- ADD_KEY(HKEY_CURRENT_USER);
- ADD_KEY(HKEY_LOCAL_MACHINE);
- ADD_KEY(HKEY_USERS);
- ADD_KEY(HKEY_PERFORMANCE_DATA);
+ PyObject *m, *d;
+ m = PyModule_Create(&winregmodule);
+ if (m == NULL)
+ return NULL;
+ d = PyModule_GetDict(m);
+ PyHKEY_Type.tp_doc = PyHKEY_doc;
+ if (PyType_Ready(&PyHKEY_Type) < 0)
+ return NULL;
+ Py_INCREF(&PyHKEY_Type);
+ if (PyDict_SetItemString(d, "HKEYType",
+ (PyObject *)&PyHKEY_Type) != 0)
+ return NULL;
+ Py_INCREF(PyExc_WindowsError);
+ if (PyDict_SetItemString(d, "error",
+ PyExc_WindowsError) != 0)
+ return NULL;
+
+ /* Add the relevant constants */
+ ADD_KEY(HKEY_CLASSES_ROOT);
+ ADD_KEY(HKEY_CURRENT_USER);
+ ADD_KEY(HKEY_LOCAL_MACHINE);
+ ADD_KEY(HKEY_USERS);
+ ADD_KEY(HKEY_PERFORMANCE_DATA);
#ifdef HKEY_CURRENT_CONFIG
- ADD_KEY(HKEY_CURRENT_CONFIG);
+ ADD_KEY(HKEY_CURRENT_CONFIG);
#endif
#ifdef HKEY_DYN_DATA
- ADD_KEY(HKEY_DYN_DATA);
+ ADD_KEY(HKEY_DYN_DATA);
#endif
- ADD_INT(KEY_QUERY_VALUE);
- ADD_INT(KEY_SET_VALUE);
- ADD_INT(KEY_CREATE_SUB_KEY);
- ADD_INT(KEY_ENUMERATE_SUB_KEYS);
- ADD_INT(KEY_NOTIFY);
- ADD_INT(KEY_CREATE_LINK);
- ADD_INT(KEY_READ);
- ADD_INT(KEY_WRITE);
- ADD_INT(KEY_EXECUTE);
- ADD_INT(KEY_ALL_ACCESS);
+ ADD_INT(KEY_QUERY_VALUE);
+ ADD_INT(KEY_SET_VALUE);
+ ADD_INT(KEY_CREATE_SUB_KEY);
+ ADD_INT(KEY_ENUMERATE_SUB_KEYS);
+ ADD_INT(KEY_NOTIFY);
+ ADD_INT(KEY_CREATE_LINK);
+ ADD_INT(KEY_READ);
+ ADD_INT(KEY_WRITE);
+ ADD_INT(KEY_EXECUTE);
+ ADD_INT(KEY_ALL_ACCESS);
#ifdef KEY_WOW64_64KEY
- ADD_INT(KEY_WOW64_64KEY);
+ ADD_INT(KEY_WOW64_64KEY);
#endif
#ifdef KEY_WOW64_32KEY
- ADD_INT(KEY_WOW64_32KEY);
+ ADD_INT(KEY_WOW64_32KEY);
#endif
- ADD_INT(REG_OPTION_RESERVED);
- ADD_INT(REG_OPTION_NON_VOLATILE);
- ADD_INT(REG_OPTION_VOLATILE);
- ADD_INT(REG_OPTION_CREATE_LINK);
- ADD_INT(REG_OPTION_BACKUP_RESTORE);
- ADD_INT(REG_OPTION_OPEN_LINK);
- ADD_INT(REG_LEGAL_OPTION);
- ADD_INT(REG_CREATED_NEW_KEY);
- ADD_INT(REG_OPENED_EXISTING_KEY);
- ADD_INT(REG_WHOLE_HIVE_VOLATILE);
- ADD_INT(REG_REFRESH_HIVE);
- ADD_INT(REG_NO_LAZY_FLUSH);
- ADD_INT(REG_NOTIFY_CHANGE_NAME);
- ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
- ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
- ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
- ADD_INT(REG_LEGAL_CHANGE_FILTER);
- ADD_INT(REG_NONE);
- ADD_INT(REG_SZ);
- ADD_INT(REG_EXPAND_SZ);
- ADD_INT(REG_BINARY);
- ADD_INT(REG_DWORD);
- ADD_INT(REG_DWORD_LITTLE_ENDIAN);
- ADD_INT(REG_DWORD_BIG_ENDIAN);
- ADD_INT(REG_LINK);
- ADD_INT(REG_MULTI_SZ);
- ADD_INT(REG_RESOURCE_LIST);
- ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
- ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
- return m;
+ ADD_INT(REG_OPTION_RESERVED);
+ ADD_INT(REG_OPTION_NON_VOLATILE);
+ ADD_INT(REG_OPTION_VOLATILE);
+ ADD_INT(REG_OPTION_CREATE_LINK);
+ ADD_INT(REG_OPTION_BACKUP_RESTORE);
+ ADD_INT(REG_OPTION_OPEN_LINK);
+ ADD_INT(REG_LEGAL_OPTION);
+ ADD_INT(REG_CREATED_NEW_KEY);
+ ADD_INT(REG_OPENED_EXISTING_KEY);
+ ADD_INT(REG_WHOLE_HIVE_VOLATILE);
+ ADD_INT(REG_REFRESH_HIVE);
+ ADD_INT(REG_NO_LAZY_FLUSH);
+ ADD_INT(REG_NOTIFY_CHANGE_NAME);
+ ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
+ ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
+ ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
+ ADD_INT(REG_LEGAL_CHANGE_FILTER);
+ ADD_INT(REG_NONE);
+ ADD_INT(REG_SZ);
+ ADD_INT(REG_EXPAND_SZ);
+ ADD_INT(REG_BINARY);
+ ADD_INT(REG_DWORD);
+ ADD_INT(REG_DWORD_LITTLE_ENDIAN);
+ ADD_INT(REG_DWORD_BIG_ENDIAN);
+ ADD_INT(REG_LINK);
+ ADD_INT(REG_MULTI_SZ);
+ ADD_INT(REG_RESOURCE_LIST);
+ ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
+ ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
+ return m;
}
diff --git a/PC/winsound.c b/PC/winsound.c
index da5dea9..4edda3f 100644
--- a/PC/winsound.c
+++ b/PC/winsound.c
@@ -78,14 +78,14 @@ sound_playsound(PyObject *s, PyObject *args)
int ok;
if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags)) {
- return NULL;
+ return NULL;
}
if(flags&SND_ASYNC && flags &SND_MEMORY) {
- /* Sidestep reference counting headache; unfortunately this also
- prevent SND_LOOP from memory. */
- PyErr_SetString(PyExc_RuntimeError,"Cannot play asynchronously from memory");
- return NULL;
+ /* Sidestep reference counting headache; unfortunately this also
+ prevent SND_LOOP from memory. */
+ PyErr_SetString(PyExc_RuntimeError,"Cannot play asynchronously from memory");
+ return NULL;
}
Py_BEGIN_ALLOW_THREADS
@@ -93,8 +93,8 @@ sound_playsound(PyObject *s, PyObject *args)
Py_END_ALLOW_THREADS
if(!ok)
{
- PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
- return NULL;
+ PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
+ return NULL;
}
Py_INCREF(Py_None);
@@ -104,40 +104,40 @@ sound_playsound(PyObject *s, PyObject *args)
static PyObject *
sound_beep(PyObject *self, PyObject *args)
{
- int freq;
- int dur;
- BOOL ok;
-
- if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur))
- return NULL;
-
- if (freq < 37 || freq > 32767) {
- PyErr_SetString(PyExc_ValueError,
- "frequency must be in 37 thru 32767");
- return NULL;
- }
-
- Py_BEGIN_ALLOW_THREADS
- ok = Beep(freq, dur);
- Py_END_ALLOW_THREADS
- if (!ok) {
- PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
- return NULL;
- }
-
- Py_INCREF(Py_None);
- return Py_None;
+ int freq;
+ int dur;
+ BOOL ok;
+
+ if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur))
+ return NULL;
+
+ if (freq < 37 || freq > 32767) {
+ PyErr_SetString(PyExc_ValueError,
+ "frequency must be in 37 thru 32767");
+ return NULL;
+ }
+
+ Py_BEGIN_ALLOW_THREADS
+ ok = Beep(freq, dur);
+ Py_END_ALLOW_THREADS
+ if (!ok) {
+ PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
+ return NULL;
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
sound_msgbeep(PyObject *self, PyObject *args)
{
- int x = MB_OK;
- if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x))
- return NULL;
- MessageBeep(x);
- Py_INCREF(Py_None);
- return Py_None;
+ int x = MB_OK;
+ if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x))
+ return NULL;
+ MessageBeep(x);
+ Py_INCREF(Py_None);
+ return Py_None;
}
static struct PyMethodDef sound_methods[] =
@@ -155,7 +155,7 @@ add_define(PyObject *dict, const char *key, long value)
PyObject *v=PyLong_FromLong(value);
if(v&&k)
{
- PyDict_SetItem(dict,k,v);
+ PyDict_SetItem(dict,k,v);
}
Py_XDECREF(k);
Py_XDECREF(v);
@@ -165,41 +165,41 @@ add_define(PyObject *dict, const char *key, long value)
static struct PyModuleDef winsoundmodule = {
- PyModuleDef_HEAD_INIT,
- "winsound",
- sound_module_doc,
- -1,
- sound_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "winsound",
+ sound_module_doc,
+ -1,
+ sound_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_winsound(void)
{
- PyObject *dict;
- PyObject *module = PyModule_Create(&winsoundmodule);
- if (module == NULL)
- return NULL;
- dict = PyModule_GetDict(module);
-
- ADD_DEFINE(SND_ASYNC);
- ADD_DEFINE(SND_NODEFAULT);
- ADD_DEFINE(SND_NOSTOP);
- ADD_DEFINE(SND_NOWAIT);
- ADD_DEFINE(SND_ALIAS);
- ADD_DEFINE(SND_FILENAME);
- ADD_DEFINE(SND_MEMORY);
- ADD_DEFINE(SND_PURGE);
- ADD_DEFINE(SND_LOOP);
- ADD_DEFINE(SND_APPLICATION);
-
- ADD_DEFINE(MB_OK);
- ADD_DEFINE(MB_ICONASTERISK);
- ADD_DEFINE(MB_ICONEXCLAMATION);
- ADD_DEFINE(MB_ICONHAND);
- ADD_DEFINE(MB_ICONQUESTION);
- return module;
+ PyObject *dict;
+ PyObject *module = PyModule_Create(&winsoundmodule);
+ if (module == NULL)
+ return NULL;
+ dict = PyModule_GetDict(module);
+
+ ADD_DEFINE(SND_ASYNC);
+ ADD_DEFINE(SND_NODEFAULT);
+ ADD_DEFINE(SND_NOSTOP);
+ ADD_DEFINE(SND_NOWAIT);
+ ADD_DEFINE(SND_ALIAS);
+ ADD_DEFINE(SND_FILENAME);
+ ADD_DEFINE(SND_MEMORY);
+ ADD_DEFINE(SND_PURGE);
+ ADD_DEFINE(SND_LOOP);
+ ADD_DEFINE(SND_APPLICATION);
+
+ ADD_DEFINE(MB_OK);
+ ADD_DEFINE(MB_ICONASTERISK);
+ ADD_DEFINE(MB_ICONEXCLAMATION);
+ ADD_DEFINE(MB_ICONHAND);
+ ADD_DEFINE(MB_ICONQUESTION);
+ return module;
}
diff --git a/PCbuild/make_buildinfo.c b/PCbuild/make_buildinfo.c
index 22b9882..5a3d385 100644
--- a/PCbuild/make_buildinfo.c
+++ b/PCbuild/make_buildinfo.c
@@ -23,72 +23,72 @@
int make_buildinfo2()
{
- struct _stat st;
- HKEY hTortoise;
- char command[CMD_SIZE+1];
- DWORD type, size;
- if (_stat(".svn", &st) < 0)
- return 0;
- /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
- if (_stat("no_subwcrev", &st) == 0)
- return 0;
- if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
- RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
- /* Tortoise not installed */
- return 0;
- command[0] = '"'; /* quote the path to the executable */
- size = sizeof(command) - 1;
- if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
- type != REG_SZ)
- /* Registry corrupted */
- return 0;
- strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
- if (_stat(command+1, &st) < 0)
- /* subwcrev.exe not part of the release */
- return 0;
- strcat_s(command, CMD_SIZE, "\" .. ..\\Modules\\getbuildinfo.c getbuildinfo2.c");
- puts(command); fflush(stdout);
- if (system(command) < 0)
- return 0;
- return 1;
+ struct _stat st;
+ HKEY hTortoise;
+ char command[CMD_SIZE+1];
+ DWORD type, size;
+ if (_stat(".svn", &st) < 0)
+ return 0;
+ /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
+ if (_stat("no_subwcrev", &st) == 0)
+ return 0;
+ if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
+ RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
+ /* Tortoise not installed */
+ return 0;
+ command[0] = '"'; /* quote the path to the executable */
+ size = sizeof(command) - 1;
+ if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
+ type != REG_SZ)
+ /* Registry corrupted */
+ return 0;
+ strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
+ if (_stat(command+1, &st) < 0)
+ /* subwcrev.exe not part of the release */
+ return 0;
+ strcat_s(command, CMD_SIZE, "\" .. ..\\Modules\\getbuildinfo.c getbuildinfo2.c");
+ puts(command); fflush(stdout);
+ if (system(command) < 0)
+ return 0;
+ return 1;
}
int main(int argc, char*argv[])
{
- char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
- int do_unlink, result;
- if (argc != 2) {
- fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
- return EXIT_FAILURE;
- }
- if (strcmp(argv[1], "Release") == 0) {
- strcat_s(command, CMD_SIZE, "-MD ");
- }
- else if (strcmp(argv[1], "Debug") == 0) {
- strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
- }
- else if (strcmp(argv[1], "ReleaseItanium") == 0) {
- strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
- }
- else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
- strcat_s(command, CMD_SIZE, "-MD ");
- strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
- }
- else {
- fprintf(stderr, "unsupported configuration %s\n", argv[1]);
- return EXIT_FAILURE;
- }
+ char command[500] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
+ int do_unlink, result;
+ if (argc != 2) {
+ fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
+ return EXIT_FAILURE;
+ }
+ if (strcmp(argv[1], "Release") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD ");
+ }
+ else if (strcmp(argv[1], "Debug") == 0) {
+ strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
+ }
+ else if (strcmp(argv[1], "ReleaseItanium") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
+ }
+ else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
+ strcat_s(command, CMD_SIZE, "-MD ");
+ strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
+ }
+ else {
+ fprintf(stderr, "unsupported configuration %s\n", argv[1]);
+ return EXIT_FAILURE;
+ }
- if ((do_unlink = make_buildinfo2()))
- strcat_s(command, CMD_SIZE, "getbuildinfo2.c -DSUBWCREV ");
- else
- strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c");
- strcat_s(command, CMD_SIZE, " -Fogetbuildinfo.o -I..\\Include -I..\\PC");
- puts(command); fflush(stdout);
- result = system(command);
- if (do_unlink)
- _unlink("getbuildinfo2.c");
- if (result < 0)
- return EXIT_FAILURE;
- return 0;
-} \ No newline at end of file
+ if ((do_unlink = make_buildinfo2()))
+ strcat_s(command, CMD_SIZE, "getbuildinfo2.c -DSUBWCREV ");
+ else
+ strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c");
+ strcat_s(command, CMD_SIZE, " -Fogetbuildinfo.o -I..\\Include -I..\\PC");
+ puts(command); fflush(stdout);
+ result = system(command);
+ if (do_unlink)
+ _unlink("getbuildinfo2.c");
+ if (result < 0)
+ return EXIT_FAILURE;
+ return 0;
+}
diff --git a/Parser/acceler.c b/Parser/acceler.c
index b41b265..9b14263 100644
--- a/Parser/acceler.c
+++ b/Parser/acceler.c
@@ -23,103 +23,103 @@ static void fixstate(grammar *, state *);
void
PyGrammar_AddAccelerators(grammar *g)
{
- dfa *d;
- int i;
- d = g->g_dfa;
- for (i = g->g_ndfas; --i >= 0; d++)
- fixdfa(g, d);
- g->g_accel = 1;
+ dfa *d;
+ int i;
+ d = g->g_dfa;
+ for (i = g->g_ndfas; --i >= 0; d++)
+ fixdfa(g, d);
+ g->g_accel = 1;
}
void
PyGrammar_RemoveAccelerators(grammar *g)
{
- dfa *d;
- int i;
- g->g_accel = 0;
- d = g->g_dfa;
- for (i = g->g_ndfas; --i >= 0; d++) {
- state *s;
- int j;
- s = d->d_state;
- for (j = 0; j < d->d_nstates; j++, s++) {
- if (s->s_accel)
- PyObject_FREE(s->s_accel);
- s->s_accel = NULL;
- }
- }
+ dfa *d;
+ int i;
+ g->g_accel = 0;
+ d = g->g_dfa;
+ for (i = g->g_ndfas; --i >= 0; d++) {
+ state *s;
+ int j;
+ s = d->d_state;
+ for (j = 0; j < d->d_nstates; j++, s++) {
+ if (s->s_accel)
+ PyObject_FREE(s->s_accel);
+ s->s_accel = NULL;
+ }
+ }
}
static void
fixdfa(grammar *g, dfa *d)
{
- state *s;
- int j;
- s = d->d_state;
- for (j = 0; j < d->d_nstates; j++, s++)
- fixstate(g, s);
+ state *s;
+ int j;
+ s = d->d_state;
+ for (j = 0; j < d->d_nstates; j++, s++)
+ fixstate(g, s);
}
static void
fixstate(grammar *g, state *s)
{
- arc *a;
- int k;
- int *accel;
- int nl = g->g_ll.ll_nlabels;
- s->s_accept = 0;
- accel = (int *) PyObject_MALLOC(nl * sizeof(int));
- if (accel == NULL) {
- fprintf(stderr, "no mem to build parser accelerators\n");
- exit(1);
- }
- for (k = 0; k < nl; k++)
- accel[k] = -1;
- a = s->s_arc;
- for (k = s->s_narcs; --k >= 0; a++) {
- int lbl = a->a_lbl;
- label *l = &g->g_ll.ll_label[lbl];
- int type = l->lb_type;
- if (a->a_arrow >= (1 << 7)) {
- printf("XXX too many states!\n");
- continue;
- }
- if (ISNONTERMINAL(type)) {
- dfa *d1 = PyGrammar_FindDFA(g, type);
- int ibit;
- if (type - NT_OFFSET >= (1 << 7)) {
- printf("XXX too high nonterminal number!\n");
- continue;
- }
- for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
- if (testbit(d1->d_first, ibit)) {
- if (accel[ibit] != -1)
- printf("XXX ambiguity!\n");
- accel[ibit] = a->a_arrow | (1 << 7) |
- ((type - NT_OFFSET) << 8);
- }
- }
- }
- else if (lbl == EMPTY)
- s->s_accept = 1;
- else if (lbl >= 0 && lbl < nl)
- accel[lbl] = a->a_arrow;
- }
- while (nl > 0 && accel[nl-1] == -1)
- nl--;
- for (k = 0; k < nl && accel[k] == -1;)
- k++;
- if (k < nl) {
- int i;
- s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
- if (s->s_accel == NULL) {
- fprintf(stderr, "no mem to add parser accelerators\n");
- exit(1);
- }
- s->s_lower = k;
- s->s_upper = nl;
- for (i = 0; k < nl; i++, k++)
- s->s_accel[i] = accel[k];
- }
- PyObject_FREE(accel);
+ arc *a;
+ int k;
+ int *accel;
+ int nl = g->g_ll.ll_nlabels;
+ s->s_accept = 0;
+ accel = (int *) PyObject_MALLOC(nl * sizeof(int));
+ if (accel == NULL) {
+ fprintf(stderr, "no mem to build parser accelerators\n");
+ exit(1);
+ }
+ for (k = 0; k < nl; k++)
+ accel[k] = -1;
+ a = s->s_arc;
+ for (k = s->s_narcs; --k >= 0; a++) {
+ int lbl = a->a_lbl;
+ label *l = &g->g_ll.ll_label[lbl];
+ int type = l->lb_type;
+ if (a->a_arrow >= (1 << 7)) {
+ printf("XXX too many states!\n");
+ continue;
+ }
+ if (ISNONTERMINAL(type)) {
+ dfa *d1 = PyGrammar_FindDFA(g, type);
+ int ibit;
+ if (type - NT_OFFSET >= (1 << 7)) {
+ printf("XXX too high nonterminal number!\n");
+ continue;
+ }
+ for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
+ if (testbit(d1->d_first, ibit)) {
+ if (accel[ibit] != -1)
+ printf("XXX ambiguity!\n");
+ accel[ibit] = a->a_arrow | (1 << 7) |
+ ((type - NT_OFFSET) << 8);
+ }
+ }
+ }
+ else if (lbl == EMPTY)
+ s->s_accept = 1;
+ else if (lbl >= 0 && lbl < nl)
+ accel[lbl] = a->a_arrow;
+ }
+ while (nl > 0 && accel[nl-1] == -1)
+ nl--;
+ for (k = 0; k < nl && accel[k] == -1;)
+ k++;
+ if (k < nl) {
+ int i;
+ s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
+ if (s->s_accel == NULL) {
+ fprintf(stderr, "no mem to add parser accelerators\n");
+ exit(1);
+ }
+ s->s_lower = k;
+ s->s_upper = nl;
+ for (i = 0; k < nl; i++, k++)
+ s->s_accel[i] = accel[k];
+ }
+ PyObject_FREE(accel);
}
diff --git a/Parser/bitset.c b/Parser/bitset.c
index b5543b8..f5bfd41 100644
--- a/Parser/bitset.c
+++ b/Parser/bitset.c
@@ -7,60 +7,60 @@
bitset
newbitset(int nbits)
{
- int nbytes = NBYTES(nbits);
- bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) * nbytes);
-
- if (ss == NULL)
- Py_FatalError("no mem for bitset");
-
- ss += nbytes;
- while (--nbytes >= 0)
- *--ss = 0;
- return ss;
+ int nbytes = NBYTES(nbits);
+ bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) * nbytes);
+
+ if (ss == NULL)
+ Py_FatalError("no mem for bitset");
+
+ ss += nbytes;
+ while (--nbytes >= 0)
+ *--ss = 0;
+ return ss;
}
void
delbitset(bitset ss)
{
- PyObject_FREE(ss);
+ PyObject_FREE(ss);
}
int
addbit(bitset ss, int ibit)
{
- int ibyte = BIT2BYTE(ibit);
- BYTE mask = BIT2MASK(ibit);
-
- if (ss[ibyte] & mask)
- return 0; /* Bit already set */
- ss[ibyte] |= mask;
- return 1;
+ int ibyte = BIT2BYTE(ibit);
+ BYTE mask = BIT2MASK(ibit);
+
+ if (ss[ibyte] & mask)
+ return 0; /* Bit already set */
+ ss[ibyte] |= mask;
+ return 1;
}
#if 0 /* Now a macro */
int
testbit(bitset ss, int ibit)
{
- return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
+ return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
}
#endif
int
samebitset(bitset ss1, bitset ss2, int nbits)
{
- int i;
-
- for (i = NBYTES(nbits); --i >= 0; )
- if (*ss1++ != *ss2++)
- return 0;
- return 1;
+ int i;
+
+ for (i = NBYTES(nbits); --i >= 0; )
+ if (*ss1++ != *ss2++)
+ return 0;
+ return 1;
}
void
mergebitset(bitset ss1, bitset ss2, int nbits)
{
- int i;
-
- for (i = NBYTES(nbits); --i >= 0; )
- *ss1++ |= *ss2++;
+ int i;
+
+ for (i = NBYTES(nbits); --i >= 0; )
+ *ss1++ |= *ss2++;
}
diff --git a/Parser/firstsets.c b/Parser/firstsets.c
index 00467b3..ee75d1b 100644
--- a/Parser/firstsets.c
+++ b/Parser/firstsets.c
@@ -13,101 +13,101 @@ static void calcfirstset(grammar *, dfa *);
void
addfirstsets(grammar *g)
{
- int i;
- dfa *d;
+ int i;
+ dfa *d;
- if (Py_DebugFlag)
- printf("Adding FIRST sets ...\n");
- for (i = 0; i < g->g_ndfas; i++) {
- d = &g->g_dfa[i];
- if (d->d_first == NULL)
- calcfirstset(g, d);
- }
+ if (Py_DebugFlag)
+ printf("Adding FIRST sets ...\n");
+ for (i = 0; i < g->g_ndfas; i++) {
+ d = &g->g_dfa[i];
+ if (d->d_first == NULL)
+ calcfirstset(g, d);
+ }
}
static void
calcfirstset(grammar *g, dfa *d)
{
- int i, j;
- state *s;
- arc *a;
- int nsyms;
- int *sym;
- int nbits;
- static bitset dummy;
- bitset result;
- int type;
- dfa *d1;
- label *l0;
-
- if (Py_DebugFlag)
- printf("Calculate FIRST set for '%s'\n", d->d_name);
-
- if (dummy == NULL)
- dummy = newbitset(1);
- if (d->d_first == dummy) {
- fprintf(stderr, "Left-recursion for '%s'\n", d->d_name);
- return;
- }
- if (d->d_first != NULL) {
- fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n",
- d->d_name);
- }
- d->d_first = dummy;
-
- l0 = g->g_ll.ll_label;
- nbits = g->g_ll.ll_nlabels;
- result = newbitset(nbits);
-
- sym = (int *)PyObject_MALLOC(sizeof(int));
- if (sym == NULL)
- Py_FatalError("no mem for new sym in calcfirstset");
- nsyms = 1;
- sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL);
-
- s = &d->d_state[d->d_initial];
- for (i = 0; i < s->s_narcs; i++) {
- a = &s->s_arc[i];
- for (j = 0; j < nsyms; j++) {
- if (sym[j] == a->a_lbl)
- break;
- }
- if (j >= nsyms) { /* New label */
- sym = (int *)PyObject_REALLOC(sym,
- sizeof(int) * (nsyms + 1));
- if (sym == NULL)
- Py_FatalError(
- "no mem to resize sym in calcfirstset");
- sym[nsyms++] = a->a_lbl;
- type = l0[a->a_lbl].lb_type;
- if (ISNONTERMINAL(type)) {
- d1 = PyGrammar_FindDFA(g, type);
- if (d1->d_first == dummy) {
- fprintf(stderr,
- "Left-recursion below '%s'\n",
- d->d_name);
- }
- else {
- if (d1->d_first == NULL)
- calcfirstset(g, d1);
- mergebitset(result,
- d1->d_first, nbits);
- }
- }
- else if (ISTERMINAL(type)) {
- addbit(result, a->a_lbl);
- }
- }
- }
- d->d_first = result;
- if (Py_DebugFlag) {
- printf("FIRST set for '%s': {", d->d_name);
- for (i = 0; i < nbits; i++) {
- if (testbit(result, i))
- printf(" %s", PyGrammar_LabelRepr(&l0[i]));
- }
- printf(" }\n");
- }
+ int i, j;
+ state *s;
+ arc *a;
+ int nsyms;
+ int *sym;
+ int nbits;
+ static bitset dummy;
+ bitset result;
+ int type;
+ dfa *d1;
+ label *l0;
- PyObject_FREE(sym);
+ if (Py_DebugFlag)
+ printf("Calculate FIRST set for '%s'\n", d->d_name);
+
+ if (dummy == NULL)
+ dummy = newbitset(1);
+ if (d->d_first == dummy) {
+ fprintf(stderr, "Left-recursion for '%s'\n", d->d_name);
+ return;
+ }
+ if (d->d_first != NULL) {
+ fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n",
+ d->d_name);
+ }
+ d->d_first = dummy;
+
+ l0 = g->g_ll.ll_label;
+ nbits = g->g_ll.ll_nlabels;
+ result = newbitset(nbits);
+
+ sym = (int *)PyObject_MALLOC(sizeof(int));
+ if (sym == NULL)
+ Py_FatalError("no mem for new sym in calcfirstset");
+ nsyms = 1;
+ sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL);
+
+ s = &d->d_state[d->d_initial];
+ for (i = 0; i < s->s_narcs; i++) {
+ a = &s->s_arc[i];
+ for (j = 0; j < nsyms; j++) {
+ if (sym[j] == a->a_lbl)
+ break;
+ }
+ if (j >= nsyms) { /* New label */
+ sym = (int *)PyObject_REALLOC(sym,
+ sizeof(int) * (nsyms + 1));
+ if (sym == NULL)
+ Py_FatalError(
+ "no mem to resize sym in calcfirstset");
+ sym[nsyms++] = a->a_lbl;
+ type = l0[a->a_lbl].lb_type;
+ if (ISNONTERMINAL(type)) {
+ d1 = PyGrammar_FindDFA(g, type);
+ if (d1->d_first == dummy) {
+ fprintf(stderr,
+ "Left-recursion below '%s'\n",
+ d->d_name);
+ }
+ else {
+ if (d1->d_first == NULL)
+ calcfirstset(g, d1);
+ mergebitset(result,
+ d1->d_first, nbits);
+ }
+ }
+ else if (ISTERMINAL(type)) {
+ addbit(result, a->a_lbl);
+ }
+ }
+ }
+ d->d_first = result;
+ if (Py_DebugFlag) {
+ printf("FIRST set for '%s': {", d->d_name);
+ for (i = 0; i < nbits; i++) {
+ if (testbit(result, i))
+ printf(" %s", PyGrammar_LabelRepr(&l0[i]));
+ }
+ printf(" }\n");
+ }
+
+ PyObject_FREE(sym);
}
diff --git a/Parser/grammar.c b/Parser/grammar.c
index fa27300..f2a25ca 100644
--- a/Parser/grammar.c
+++ b/Parser/grammar.c
@@ -14,98 +14,98 @@ extern int Py_DebugFlag;
grammar *
newgrammar(int start)
{
- grammar *g;
-
- g = (grammar *)PyObject_MALLOC(sizeof(grammar));
- if (g == NULL)
- Py_FatalError("no mem for new grammar");
- g->g_ndfas = 0;
- g->g_dfa = NULL;
- g->g_start = start;
- g->g_ll.ll_nlabels = 0;
- g->g_ll.ll_label = NULL;
- g->g_accel = 0;
- return g;
+ grammar *g;
+
+ g = (grammar *)PyObject_MALLOC(sizeof(grammar));
+ if (g == NULL)
+ Py_FatalError("no mem for new grammar");
+ g->g_ndfas = 0;
+ g->g_dfa = NULL;
+ g->g_start = start;
+ g->g_ll.ll_nlabels = 0;
+ g->g_ll.ll_label = NULL;
+ g->g_accel = 0;
+ return g;
}
dfa *
adddfa(grammar *g, int type, char *name)
{
- dfa *d;
-
- g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
- sizeof(dfa) * (g->g_ndfas + 1));
- if (g->g_dfa == NULL)
- Py_FatalError("no mem to resize dfa in adddfa");
- d = &g->g_dfa[g->g_ndfas++];
- d->d_type = type;
- d->d_name = strdup(name);
- d->d_nstates = 0;
- d->d_state = NULL;
- d->d_initial = -1;
- d->d_first = NULL;
- return d; /* Only use while fresh! */
+ dfa *d;
+
+ g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
+ sizeof(dfa) * (g->g_ndfas + 1));
+ if (g->g_dfa == NULL)
+ Py_FatalError("no mem to resize dfa in adddfa");
+ d = &g->g_dfa[g->g_ndfas++];
+ d->d_type = type;
+ d->d_name = strdup(name);
+ d->d_nstates = 0;
+ d->d_state = NULL;
+ d->d_initial = -1;
+ d->d_first = NULL;
+ return d; /* Only use while fresh! */
}
int
addstate(dfa *d)
{
- state *s;
-
- d->d_state = (state *)PyObject_REALLOC(d->d_state,
- sizeof(state) * (d->d_nstates + 1));
- if (d->d_state == NULL)
- Py_FatalError("no mem to resize state in addstate");
- s = &d->d_state[d->d_nstates++];
- s->s_narcs = 0;
- s->s_arc = NULL;
- s->s_lower = 0;
- s->s_upper = 0;
- s->s_accel = NULL;
- s->s_accept = 0;
- return s - d->d_state;
+ state *s;
+
+ d->d_state = (state *)PyObject_REALLOC(d->d_state,
+ sizeof(state) * (d->d_nstates + 1));
+ if (d->d_state == NULL)
+ Py_FatalError("no mem to resize state in addstate");
+ s = &d->d_state[d->d_nstates++];
+ s->s_narcs = 0;
+ s->s_arc = NULL;
+ s->s_lower = 0;
+ s->s_upper = 0;
+ s->s_accel = NULL;
+ s->s_accept = 0;
+ return s - d->d_state;
}
void
addarc(dfa *d, int from, int to, int lbl)
{
- state *s;
- arc *a;
-
- assert(0 <= from && from < d->d_nstates);
- assert(0 <= to && to < d->d_nstates);
-
- s = &d->d_state[from];
- s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
- if (s->s_arc == NULL)
- Py_FatalError("no mem to resize arc list in addarc");
- a = &s->s_arc[s->s_narcs++];
- a->a_lbl = lbl;
- a->a_arrow = to;
+ state *s;
+ arc *a;
+
+ assert(0 <= from && from < d->d_nstates);
+ assert(0 <= to && to < d->d_nstates);
+
+ s = &d->d_state[from];
+ s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
+ if (s->s_arc == NULL)
+ Py_FatalError("no mem to resize arc list in addarc");
+ a = &s->s_arc[s->s_narcs++];
+ a->a_lbl = lbl;
+ a->a_arrow = to;
}
int
addlabel(labellist *ll, int type, char *str)
{
- int i;
- label *lb;
-
- for (i = 0; i < ll->ll_nlabels; i++) {
- if (ll->ll_label[i].lb_type == type &&
- strcmp(ll->ll_label[i].lb_str, str) == 0)
- return i;
- }
- ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
- sizeof(label) * (ll->ll_nlabels + 1));
- if (ll->ll_label == NULL)
- Py_FatalError("no mem to resize labellist in addlabel");
- lb = &ll->ll_label[ll->ll_nlabels++];
- lb->lb_type = type;
- lb->lb_str = strdup(str);
- if (Py_DebugFlag)
- printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels,
- PyGrammar_LabelRepr(lb));
- return lb - ll->ll_label;
+ int i;
+ label *lb;
+
+ for (i = 0; i < ll->ll_nlabels; i++) {
+ if (ll->ll_label[i].lb_type == type &&
+ strcmp(ll->ll_label[i].lb_str, str) == 0)
+ return i;
+ }
+ ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
+ sizeof(label) * (ll->ll_nlabels + 1));
+ if (ll->ll_label == NULL)
+ Py_FatalError("no mem to resize labellist in addlabel");
+ lb = &ll->ll_label[ll->ll_nlabels++];
+ lb->lb_type = type;
+ lb->lb_str = strdup(str);
+ if (Py_DebugFlag)
+ printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels,
+ PyGrammar_LabelRepr(lb));
+ return lb - ll->ll_label;
}
/* Same, but rather dies than adds */
@@ -113,16 +113,16 @@ addlabel(labellist *ll, int type, char *str)
int
findlabel(labellist *ll, int type, char *str)
{
- int i;
-
- for (i = 0; i < ll->ll_nlabels; i++) {
- if (ll->ll_label[i].lb_type == type /*&&
- strcmp(ll->ll_label[i].lb_str, str) == 0*/)
- return i;
- }
- fprintf(stderr, "Label %d/'%s' not found\n", type, str);
- Py_FatalError("grammar.c:findlabel()");
- return 0; /* Make gcc -Wall happy */
+ int i;
+
+ for (i = 0; i < ll->ll_nlabels; i++) {
+ if (ll->ll_label[i].lb_type == type /*&&
+ strcmp(ll->ll_label[i].lb_str, str) == 0*/)
+ return i;
+ }
+ fprintf(stderr, "Label %d/'%s' not found\n", type, str);
+ Py_FatalError("grammar.c:findlabel()");
+ return 0; /* Make gcc -Wall happy */
}
/* Forward */
@@ -131,120 +131,120 @@ static void translabel(grammar *, label *);
void
translatelabels(grammar *g)
{
- int i;
+ int i;
#ifdef Py_DEBUG
- printf("Translating labels ...\n");
+ printf("Translating labels ...\n");
#endif
- /* Don't translate EMPTY */
- for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++)
- translabel(g, &g->g_ll.ll_label[i]);
+ /* Don't translate EMPTY */
+ for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++)
+ translabel(g, &g->g_ll.ll_label[i]);
}
static void
translabel(grammar *g, label *lb)
{
- int i;
-
- if (Py_DebugFlag)
- printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
-
- if (lb->lb_type == NAME) {
- for (i = 0; i < g->g_ndfas; i++) {
- if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) {
- if (Py_DebugFlag)
- printf(
- "Label %s is non-terminal %d.\n",
- lb->lb_str,
- g->g_dfa[i].d_type);
- lb->lb_type = g->g_dfa[i].d_type;
- free(lb->lb_str);
- lb->lb_str = NULL;
- return;
- }
- }
- for (i = 0; i < (int)N_TOKENS; i++) {
- if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
- if (Py_DebugFlag)
- printf("Label %s is terminal %d.\n",
- lb->lb_str, i);
- lb->lb_type = i;
- free(lb->lb_str);
- lb->lb_str = NULL;
- return;
- }
- }
- printf("Can't translate NAME label '%s'\n", lb->lb_str);
- return;
- }
-
- if (lb->lb_type == STRING) {
- if (isalpha(Py_CHARMASK(lb->lb_str[1])) ||
- lb->lb_str[1] == '_') {
- char *p;
- char *src;
- char *dest;
- size_t name_len;
- if (Py_DebugFlag)
- printf("Label %s is a keyword\n", lb->lb_str);
- lb->lb_type = NAME;
- src = lb->lb_str + 1;
- p = strchr(src, '\'');
- if (p)
- name_len = p - src;
- else
- name_len = strlen(src);
- dest = (char *)malloc(name_len + 1);
- if (!dest) {
- printf("Can't alloc dest '%s'\n", src);
- return;
- }
- strncpy(dest, src, name_len);
- dest[name_len] = '\0';
- free(lb->lb_str);
- lb->lb_str = dest;
- }
- else if (lb->lb_str[2] == lb->lb_str[0]) {
- int type = (int) PyToken_OneChar(lb->lb_str[1]);
- if (type != OP) {
- lb->lb_type = type;
- free(lb->lb_str);
- lb->lb_str = NULL;
- }
- else
- printf("Unknown OP label %s\n",
- lb->lb_str);
- }
- else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) {
- int type = (int) PyToken_TwoChars(lb->lb_str[1],
- lb->lb_str[2]);
- if (type != OP) {
- lb->lb_type = type;
- free(lb->lb_str);
- lb->lb_str = NULL;
- }
- else
- printf("Unknown OP label %s\n",
- lb->lb_str);
- }
- else if (lb->lb_str[2] && lb->lb_str[3] && lb->lb_str[4] == lb->lb_str[0]) {
- int type = (int) PyToken_ThreeChars(lb->lb_str[1],
- lb->lb_str[2],
- lb->lb_str[3]);
- if (type != OP) {
- lb->lb_type = type;
- free(lb->lb_str);
- lb->lb_str = NULL;
- }
- else
- printf("Unknown OP label %s\n",
- lb->lb_str);
- }
- else
- printf("Can't translate STRING label %s\n",
- lb->lb_str);
- }
- else
- printf("Can't translate label '%s'\n",
- PyGrammar_LabelRepr(lb));
+ int i;
+
+ if (Py_DebugFlag)
+ printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
+
+ if (lb->lb_type == NAME) {
+ for (i = 0; i < g->g_ndfas; i++) {
+ if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) {
+ if (Py_DebugFlag)
+ printf(
+ "Label %s is non-terminal %d.\n",
+ lb->lb_str,
+ g->g_dfa[i].d_type);
+ lb->lb_type = g->g_dfa[i].d_type;
+ free(lb->lb_str);
+ lb->lb_str = NULL;
+ return;
+ }
+ }
+ for (i = 0; i < (int)N_TOKENS; i++) {
+ if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
+ if (Py_DebugFlag)
+ printf("Label %s is terminal %d.\n",
+ lb->lb_str, i);
+ lb->lb_type = i;
+ free(lb->lb_str);
+ lb->lb_str = NULL;
+ return;
+ }
+ }
+ printf("Can't translate NAME label '%s'\n", lb->lb_str);
+ return;
+ }
+
+ if (lb->lb_type == STRING) {
+ if (isalpha(Py_CHARMASK(lb->lb_str[1])) ||
+ lb->lb_str[1] == '_') {
+ char *p;
+ char *src;
+ char *dest;
+ size_t name_len;
+ if (Py_DebugFlag)
+ printf("Label %s is a keyword\n", lb->lb_str);
+ lb->lb_type = NAME;
+ src = lb->lb_str + 1;
+ p = strchr(src, '\'');
+ if (p)
+ name_len = p - src;
+ else
+ name_len = strlen(src);
+ dest = (char *)malloc(name_len + 1);
+ if (!dest) {
+ printf("Can't alloc dest '%s'\n", src);
+ return;
+ }
+ strncpy(dest, src, name_len);
+ dest[name_len] = '\0';
+ free(lb->lb_str);
+ lb->lb_str = dest;
+ }
+ else if (lb->lb_str[2] == lb->lb_str[0]) {
+ int type = (int) PyToken_OneChar(lb->lb_str[1]);
+ if (type != OP) {
+ lb->lb_type = type;
+ free(lb->lb_str);
+ lb->lb_str = NULL;
+ }
+ else
+ printf("Unknown OP label %s\n",
+ lb->lb_str);
+ }
+ else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) {
+ int type = (int) PyToken_TwoChars(lb->lb_str[1],
+ lb->lb_str[2]);
+ if (type != OP) {
+ lb->lb_type = type;
+ free(lb->lb_str);
+ lb->lb_str = NULL;
+ }
+ else
+ printf("Unknown OP label %s\n",
+ lb->lb_str);
+ }
+ else if (lb->lb_str[2] && lb->lb_str[3] && lb->lb_str[4] == lb->lb_str[0]) {
+ int type = (int) PyToken_ThreeChars(lb->lb_str[1],
+ lb->lb_str[2],
+ lb->lb_str[3]);
+ if (type != OP) {
+ lb->lb_type = type;
+ free(lb->lb_str);
+ lb->lb_str = NULL;
+ }
+ else
+ printf("Unknown OP label %s\n",
+ lb->lb_str);
+ }
+ else
+ printf("Can't translate STRING label %s\n",
+ lb->lb_str);
+ }
+ else
+ printf("Can't translate label '%s'\n",
+ PyGrammar_LabelRepr(lb));
}
diff --git a/Parser/grammar1.c b/Parser/grammar1.c
index b76719a..1f7d264 100644
--- a/Parser/grammar1.c
+++ b/Parser/grammar1.c
@@ -11,47 +11,47 @@
dfa *
PyGrammar_FindDFA(grammar *g, register int type)
{
- register dfa *d;
+ register dfa *d;
#if 1
- /* Massive speed-up */
- d = &g->g_dfa[type - NT_OFFSET];
- assert(d->d_type == type);
- return d;
+ /* Massive speed-up */
+ d = &g->g_dfa[type - NT_OFFSET];
+ assert(d->d_type == type);
+ return d;
#else
- /* Old, slow version */
- register int i;
-
- for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
- if (d->d_type == type)
- return d;
- }
- assert(0);
- /* NOTREACHED */
+ /* Old, slow version */
+ register int i;
+
+ for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
+ if (d->d_type == type)
+ return d;
+ }
+ assert(0);
+ /* NOTREACHED */
#endif
}
char *
PyGrammar_LabelRepr(label *lb)
{
- static char buf[100];
-
- if (lb->lb_type == ENDMARKER)
- return "EMPTY";
- else if (ISNONTERMINAL(lb->lb_type)) {
- if (lb->lb_str == NULL) {
- PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
- return buf;
- }
- else
- return lb->lb_str;
- }
- else {
- if (lb->lb_str == NULL)
- return _PyParser_TokenNames[lb->lb_type];
- else {
- PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
- _PyParser_TokenNames[lb->lb_type], lb->lb_str);
- return buf;
- }
- }
+ static char buf[100];
+
+ if (lb->lb_type == ENDMARKER)
+ return "EMPTY";
+ else if (ISNONTERMINAL(lb->lb_type)) {
+ if (lb->lb_str == NULL) {
+ PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
+ return buf;
+ }
+ else
+ return lb->lb_str;
+ }
+ else {
+ if (lb->lb_str == NULL)
+ return _PyParser_TokenNames[lb->lb_type];
+ else {
+ PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
+ _PyParser_TokenNames[lb->lb_type], lb->lb_str);
+ return buf;
+ }
+ }
}
diff --git a/Parser/intrcheck.c b/Parser/intrcheck.c
index 06b5840..4439864 100644
--- a/Parser/intrcheck.c
+++ b/Parser/intrcheck.c
@@ -21,7 +21,7 @@ PyOS_FiniInterrupts(void)
int
PyOS_InterruptOccurred(void)
{
- _wyield();
+ _wyield();
}
#define OK
@@ -47,7 +47,7 @@ PyOS_InterruptOccurred(void)
void
PyOS_InitInterrupts(void)
{
- _go32_want_ctrl_break(1 /* TRUE */);
+ _go32_want_ctrl_break(1 /* TRUE */);
}
void
@@ -58,7 +58,7 @@ PyOS_FiniInterrupts(void)
int
PyOS_InterruptOccurred(void)
{
- return _go32_was_ctrl_break_hit();
+ return _go32_was_ctrl_break_hit();
}
#else /* !__GNUC__ */
@@ -78,12 +78,12 @@ PyOS_FiniInterrupts(void)
int
PyOS_InterruptOccurred(void)
{
- int interrupted = 0;
- while (kbhit()) {
- if (getch() == '\003')
- interrupted = 1;
- }
- return interrupted;
+ int interrupted = 0;
+ while (kbhit()) {
+ if (getch() == '\003')
+ interrupted = 1;
+ }
+ return interrupted;
}
#endif /* __GNUC__ */
@@ -106,7 +106,7 @@ static int interrupted;
void
PyErr_SetInterrupt(void)
{
- interrupted = 1;
+ interrupted = 1;
}
extern int PyErr_CheckSignals(void);
@@ -114,28 +114,28 @@ extern int PyErr_CheckSignals(void);
static int
checksignals_witharg(void * arg)
{
- return PyErr_CheckSignals();
+ return PyErr_CheckSignals();
}
static void
intcatcher(int sig)
{
- extern void Py_Exit(int);
- static char message[] =
+ extern void Py_Exit(int);
+ static char message[] =
"python: to interrupt a truly hanging Python program, interrupt once more.\n";
- switch (interrupted++) {
- case 0:
- break;
- case 1:
- write(2, message, strlen(message));
- break;
- case 2:
- interrupted = 0;
- Py_Exit(1);
- break;
- }
- PyOS_setsig(SIGINT, intcatcher);
- Py_AddPendingCall(checksignals_witharg, NULL);
+ switch (interrupted++) {
+ case 0:
+ break;
+ case 1:
+ write(2, message, strlen(message));
+ break;
+ case 2:
+ interrupted = 0;
+ Py_Exit(1);
+ break;
+ }
+ PyOS_setsig(SIGINT, intcatcher);
+ Py_AddPendingCall(checksignals_witharg, NULL);
}
static void (*old_siginthandler)(int) = SIG_DFL;
@@ -143,23 +143,23 @@ static void (*old_siginthandler)(int) = SIG_DFL;
void
PyOS_InitInterrupts(void)
{
- if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
- PyOS_setsig(SIGINT, intcatcher);
+ if ((old_siginthandler = PyOS_setsig(SIGINT, SIG_IGN)) != SIG_IGN)
+ PyOS_setsig(SIGINT, intcatcher);
}
void
PyOS_FiniInterrupts(void)
{
- PyOS_setsig(SIGINT, old_siginthandler);
+ PyOS_setsig(SIGINT, old_siginthandler);
}
int
PyOS_InterruptOccurred(void)
{
- if (!interrupted)
- return 0;
- interrupted = 0;
- return 1;
+ if (!interrupted)
+ return 0;
+ interrupted = 0;
+ return 1;
}
#endif /* !OK */
@@ -168,7 +168,7 @@ void
PyOS_AfterFork(void)
{
#ifdef WITH_THREAD
- PyEval_ReInitThreads();
- PyThread_ReInitTLS();
+ PyEval_ReInitThreads();
+ PyThread_ReInitTLS();
#endif
}
diff --git a/Parser/listnode.c b/Parser/listnode.c
index c0b3b66..b5f8ad2 100644
--- a/Parser/listnode.c
+++ b/Parser/listnode.c
@@ -12,7 +12,7 @@ static void listnode(FILE *, node *);
void
PyNode_ListTree(node *n)
{
- listnode(stdout, n);
+ listnode(stdout, n);
}
static int level, atbol;
@@ -20,47 +20,47 @@ static int level, atbol;
static void
listnode(FILE *fp, node *n)
{
- level = 0;
- atbol = 1;
- list1node(fp, n);
+ level = 0;
+ atbol = 1;
+ list1node(fp, n);
}
static void
list1node(FILE *fp, node *n)
{
- if (n == 0)
- return;
- if (ISNONTERMINAL(TYPE(n))) {
- int i;
- for (i = 0; i < NCH(n); i++)
- list1node(fp, CHILD(n, i));
- }
- else if (ISTERMINAL(TYPE(n))) {
- switch (TYPE(n)) {
- case INDENT:
- ++level;
- break;
- case DEDENT:
- --level;
- break;
- default:
- if (atbol) {
- int i;
- for (i = 0; i < level; ++i)
- fprintf(fp, "\t");
- atbol = 0;
- }
- if (TYPE(n) == NEWLINE) {
- if (STR(n) != NULL)
- fprintf(fp, "%s", STR(n));
- fprintf(fp, "\n");
- atbol = 1;
- }
- else
- fprintf(fp, "%s ", STR(n));
- break;
- }
- }
- else
- fprintf(fp, "? ");
+ if (n == 0)
+ return;
+ if (ISNONTERMINAL(TYPE(n))) {
+ int i;
+ for (i = 0; i < NCH(n); i++)
+ list1node(fp, CHILD(n, i));
+ }
+ else if (ISTERMINAL(TYPE(n))) {
+ switch (TYPE(n)) {
+ case INDENT:
+ ++level;
+ break;
+ case DEDENT:
+ --level;
+ break;
+ default:
+ if (atbol) {
+ int i;
+ for (i = 0; i < level; ++i)
+ fprintf(fp, "\t");
+ atbol = 0;
+ }
+ if (TYPE(n) == NEWLINE) {
+ if (STR(n) != NULL)
+ fprintf(fp, "%s", STR(n));
+ fprintf(fp, "\n");
+ atbol = 1;
+ }
+ else
+ fprintf(fp, "%s ", STR(n));
+ break;
+ }
+ }
+ else
+ fprintf(fp, "? ");
}
diff --git a/Parser/metagrammar.c b/Parser/metagrammar.c
index b61bc6d..53810b8 100644
--- a/Parser/metagrammar.c
+++ b/Parser/metagrammar.c
@@ -4,152 +4,152 @@
#include "grammar.h"
#include "pgen.h"
static arc arcs_0_0[3] = {
- {2, 0},
- {3, 0},
- {4, 1},
+ {2, 0},
+ {3, 0},
+ {4, 1},
};
static arc arcs_0_1[1] = {
- {0, 1},
+ {0, 1},
};
static state states_0[2] = {
- {3, arcs_0_0},
- {1, arcs_0_1},
+ {3, arcs_0_0},
+ {1, arcs_0_1},
};
static arc arcs_1_0[1] = {
- {5, 1},
+ {5, 1},
};
static arc arcs_1_1[1] = {
- {6, 2},
+ {6, 2},
};
static arc arcs_1_2[1] = {
- {7, 3},
+ {7, 3},
};
static arc arcs_1_3[1] = {
- {3, 4},
+ {3, 4},
};
static arc arcs_1_4[1] = {
- {0, 4},
+ {0, 4},
};
static state states_1[5] = {
- {1, arcs_1_0},
- {1, arcs_1_1},
- {1, arcs_1_2},
- {1, arcs_1_3},
- {1, arcs_1_4},
+ {1, arcs_1_0},
+ {1, arcs_1_1},
+ {1, arcs_1_2},
+ {1, arcs_1_3},
+ {1, arcs_1_4},
};
static arc arcs_2_0[1] = {
- {8, 1},
+ {8, 1},
};
static arc arcs_2_1[2] = {
- {9, 0},
- {0, 1},
+ {9, 0},
+ {0, 1},
};
static state states_2[2] = {
- {1, arcs_2_0},
- {2, arcs_2_1},
+ {1, arcs_2_0},
+ {2, arcs_2_1},
};
static arc arcs_3_0[1] = {
- {10, 1},
+ {10, 1},
};
static arc arcs_3_1[2] = {
- {10, 1},
- {0, 1},
+ {10, 1},
+ {0, 1},
};
static state states_3[2] = {
- {1, arcs_3_0},
- {2, arcs_3_1},
+ {1, arcs_3_0},
+ {2, arcs_3_1},
};
static arc arcs_4_0[2] = {
- {11, 1},
- {13, 2},
+ {11, 1},
+ {13, 2},
};
static arc arcs_4_1[1] = {
- {7, 3},
+ {7, 3},
};
static arc arcs_4_2[3] = {
- {14, 4},
- {15, 4},
- {0, 2},
+ {14, 4},
+ {15, 4},
+ {0, 2},
};
static arc arcs_4_3[1] = {
- {12, 4},
+ {12, 4},
};
static arc arcs_4_4[1] = {
- {0, 4},
+ {0, 4},
};
static state states_4[5] = {
- {2, arcs_4_0},
- {1, arcs_4_1},
- {3, arcs_4_2},
- {1, arcs_4_3},
- {1, arcs_4_4},
+ {2, arcs_4_0},
+ {1, arcs_4_1},
+ {3, arcs_4_2},
+ {1, arcs_4_3},
+ {1, arcs_4_4},
};
static arc arcs_5_0[3] = {
- {5, 1},
- {16, 1},
- {17, 2},
+ {5, 1},
+ {16, 1},
+ {17, 2},
};
static arc arcs_5_1[1] = {
- {0, 1},
+ {0, 1},
};
static arc arcs_5_2[1] = {
- {7, 3},
+ {7, 3},
};
static arc arcs_5_3[1] = {
- {18, 1},
+ {18, 1},
};
static state states_5[4] = {
- {3, arcs_5_0},
- {1, arcs_5_1},
- {1, arcs_5_2},
- {1, arcs_5_3},
+ {3, arcs_5_0},
+ {1, arcs_5_1},
+ {1, arcs_5_2},
+ {1, arcs_5_3},
};
static dfa dfas[6] = {
- {256, "MSTART", 0, 2, states_0,
- "\070\000\000"},
- {257, "RULE", 0, 5, states_1,
- "\040\000\000"},
- {258, "RHS", 0, 2, states_2,
- "\040\010\003"},
- {259, "ALT", 0, 2, states_3,
- "\040\010\003"},
- {260, "ITEM", 0, 5, states_4,
- "\040\010\003"},
- {261, "ATOM", 0, 4, states_5,
- "\040\000\003"},
+ {256, "MSTART", 0, 2, states_0,
+ "\070\000\000"},
+ {257, "RULE", 0, 5, states_1,
+ "\040\000\000"},
+ {258, "RHS", 0, 2, states_2,
+ "\040\010\003"},
+ {259, "ALT", 0, 2, states_3,
+ "\040\010\003"},
+ {260, "ITEM", 0, 5, states_4,
+ "\040\010\003"},
+ {261, "ATOM", 0, 4, states_5,
+ "\040\000\003"},
};
static label labels[19] = {
- {0, "EMPTY"},
- {256, 0},
- {257, 0},
- {4, 0},
- {0, 0},
- {1, 0},
- {11, 0},
- {258, 0},
- {259, 0},
- {18, 0},
- {260, 0},
- {9, 0},
- {10, 0},
- {261, 0},
- {16, 0},
- {14, 0},
- {3, 0},
- {7, 0},
- {8, 0},
+ {0, "EMPTY"},
+ {256, 0},
+ {257, 0},
+ {4, 0},
+ {0, 0},
+ {1, 0},
+ {11, 0},
+ {258, 0},
+ {259, 0},
+ {18, 0},
+ {260, 0},
+ {9, 0},
+ {10, 0},
+ {261, 0},
+ {16, 0},
+ {14, 0},
+ {3, 0},
+ {7, 0},
+ {8, 0},
};
static grammar _PyParser_Grammar = {
- 6,
- dfas,
- {19, labels},
- 256
+ 6,
+ dfas,
+ {19, labels},
+ 256
};
grammar *
meta_grammar(void)
{
- return &_PyParser_Grammar;
+ return &_PyParser_Grammar;
}
grammar *
diff --git a/Parser/myreadline.c b/Parser/myreadline.c
index a144850..a62e208 100644
--- a/Parser/myreadline.c
+++ b/Parser/myreadline.c
@@ -35,67 +35,67 @@ int (*PyOS_InputHook)(void) = NULL;
static int
my_fgets(char *buf, int len, FILE *fp)
{
- char *p;
- for (;;) {
- if (PyOS_InputHook != NULL)
- (void)(PyOS_InputHook)();
- errno = 0;
- p = fgets(buf, len, fp);
- if (p != NULL)
- return 0; /* No error */
+ char *p;
+ for (;;) {
+ if (PyOS_InputHook != NULL)
+ (void)(PyOS_InputHook)();
+ errno = 0;
+ p = fgets(buf, len, fp);
+ if (p != NULL)
+ return 0; /* No error */
#ifdef MS_WINDOWS
- /* In the case of a Ctrl+C or some other external event
- interrupting the operation:
- Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
- error code (and feof() returns TRUE).
- Win9x: Ctrl+C seems to have no effect on fgets() returning
- early - the signal handler is called, but the fgets()
- only returns "normally" (ie, when Enter hit or feof())
- */
- if (GetLastError()==ERROR_OPERATION_ABORTED) {
- /* Signals come asynchronously, so we sleep a brief
- moment before checking if the handler has been
- triggered (we cant just return 1 before the
- signal handler has been called, as the later
- signal may be treated as a separate interrupt).
- */
- Sleep(1);
- if (PyOS_InterruptOccurred()) {
- return 1; /* Interrupt */
- }
- /* Either the sleep wasn't long enough (need a
- short loop retrying?) or not interrupted at all
- (in which case we should revisit the whole thing!)
- Logging some warning would be nice. assert is not
- viable as under the debugger, the various dialogs
- mean the condition is not true.
- */
- }
+ /* In the case of a Ctrl+C or some other external event
+ interrupting the operation:
+ Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
+ error code (and feof() returns TRUE).
+ Win9x: Ctrl+C seems to have no effect on fgets() returning
+ early - the signal handler is called, but the fgets()
+ only returns "normally" (ie, when Enter hit or feof())
+ */
+ if (GetLastError()==ERROR_OPERATION_ABORTED) {
+ /* Signals come asynchronously, so we sleep a brief
+ moment before checking if the handler has been
+ triggered (we cant just return 1 before the
+ signal handler has been called, as the later
+ signal may be treated as a separate interrupt).
+ */
+ Sleep(1);
+ if (PyOS_InterruptOccurred()) {
+ return 1; /* Interrupt */
+ }
+ /* Either the sleep wasn't long enough (need a
+ short loop retrying?) or not interrupted at all
+ (in which case we should revisit the whole thing!)
+ Logging some warning would be nice. assert is not
+ viable as under the debugger, the various dialogs
+ mean the condition is not true.
+ */
+ }
#endif /* MS_WINDOWS */
- if (feof(fp)) {
- return -1; /* EOF */
- }
+ if (feof(fp)) {
+ return -1; /* EOF */
+ }
#ifdef EINTR
- if (errno == EINTR) {
- int s;
+ if (errno == EINTR) {
+ int s;
#ifdef WITH_THREAD
- PyEval_RestoreThread(_PyOS_ReadlineTState);
+ PyEval_RestoreThread(_PyOS_ReadlineTState);
#endif
- s = PyErr_CheckSignals();
+ s = PyErr_CheckSignals();
#ifdef WITH_THREAD
- PyEval_SaveThread();
+ PyEval_SaveThread();
#endif
- if (s < 0) {
- return 1;
- }
- }
+ if (s < 0) {
+ return 1;
+ }
+ }
#endif
- if (PyOS_InterruptOccurred()) {
- return 1; /* Interrupt */
- }
- return -2; /* Error */
- }
- /* NOTREACHED */
+ if (PyOS_InterruptOccurred()) {
+ return 1; /* Interrupt */
+ }
+ return -2; /* Error */
+ }
+ /* NOTREACHED */
}
@@ -104,41 +104,41 @@ my_fgets(char *buf, int len, FILE *fp)
char *
PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
{
- size_t n;
- char *p;
- n = 100;
- if ((p = (char *)PyMem_MALLOC(n)) == NULL)
- return NULL;
- fflush(sys_stdout);
- if (prompt)
- fprintf(stderr, "%s", prompt);
- fflush(stderr);
- switch (my_fgets(p, (int)n, sys_stdin)) {
- case 0: /* Normal case */
- break;
- case 1: /* Interrupt */
- PyMem_FREE(p);
- return NULL;
- case -1: /* EOF */
- case -2: /* Error */
- default: /* Shouldn't happen */
- *p = '\0';
- break;
- }
- n = strlen(p);
- while (n > 0 && p[n-1] != '\n') {
- size_t incr = n+2;
- p = (char *)PyMem_REALLOC(p, n + incr);
- if (p == NULL)
- return NULL;
- if (incr > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError, "input line too long");
- }
- if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
- break;
- n += strlen(p+n);
- }
- return (char *)PyMem_REALLOC(p, n+1);
+ size_t n;
+ char *p;
+ n = 100;
+ if ((p = (char *)PyMem_MALLOC(n)) == NULL)
+ return NULL;
+ fflush(sys_stdout);
+ if (prompt)
+ fprintf(stderr, "%s", prompt);
+ fflush(stderr);
+ switch (my_fgets(p, (int)n, sys_stdin)) {
+ case 0: /* Normal case */
+ break;
+ case 1: /* Interrupt */
+ PyMem_FREE(p);
+ return NULL;
+ case -1: /* EOF */
+ case -2: /* Error */
+ default: /* Shouldn't happen */
+ *p = '\0';
+ break;
+ }
+ n = strlen(p);
+ while (n > 0 && p[n-1] != '\n') {
+ size_t incr = n+2;
+ p = (char *)PyMem_REALLOC(p, n + incr);
+ if (p == NULL)
+ return NULL;
+ if (incr > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError, "input line too long");
+ }
+ if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
+ break;
+ n += strlen(p+n);
+ }
+ return (char *)PyMem_REALLOC(p, n+1);
}
@@ -155,52 +155,52 @@ char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
char *
PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
{
- char *rv;
+ char *rv;
- if (_PyOS_ReadlineTState == PyThreadState_GET()) {
- PyErr_SetString(PyExc_RuntimeError,
- "can't re-enter readline");
- return NULL;
- }
-
+ if (_PyOS_ReadlineTState == PyThreadState_GET()) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "can't re-enter readline");
+ return NULL;
+ }
- if (PyOS_ReadlineFunctionPointer == NULL) {
+
+ if (PyOS_ReadlineFunctionPointer == NULL) {
#ifdef __VMS
- PyOS_ReadlineFunctionPointer = vms__StdioReadline;
+ PyOS_ReadlineFunctionPointer = vms__StdioReadline;
#else
- PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
+ PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
#endif
- }
-
+ }
+
#ifdef WITH_THREAD
- if (_PyOS_ReadlineLock == NULL) {
- _PyOS_ReadlineLock = PyThread_allocate_lock();
- }
+ if (_PyOS_ReadlineLock == NULL) {
+ _PyOS_ReadlineLock = PyThread_allocate_lock();
+ }
#endif
- _PyOS_ReadlineTState = PyThreadState_GET();
- Py_BEGIN_ALLOW_THREADS
+ _PyOS_ReadlineTState = PyThreadState_GET();
+ Py_BEGIN_ALLOW_THREADS
#ifdef WITH_THREAD
- PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
+ PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
#endif
- /* This is needed to handle the unlikely case that the
- * interpreter is in interactive mode *and* stdin/out are not
- * a tty. This can happen, for example if python is run like
- * this: python -i < test1.py
- */
- if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
- rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
- else
- rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
- prompt);
- Py_END_ALLOW_THREADS
+ /* This is needed to handle the unlikely case that the
+ * interpreter is in interactive mode *and* stdin/out are not
+ * a tty. This can happen, for example if python is run like
+ * this: python -i < test1.py
+ */
+ if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
+ rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
+ else
+ rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
+ prompt);
+ Py_END_ALLOW_THREADS
#ifdef WITH_THREAD
- PyThread_release_lock(_PyOS_ReadlineLock);
+ PyThread_release_lock(_PyOS_ReadlineLock);
#endif
- _PyOS_ReadlineTState = NULL;
+ _PyOS_ReadlineTState = NULL;
- return rv;
+ return rv;
}
diff --git a/Parser/node.c b/Parser/node.c
index f4c86cb..9eba76b 100644
--- a/Parser/node.c
+++ b/Parser/node.c
@@ -7,30 +7,30 @@
node *
PyNode_New(int type)
{
- node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
- if (n == NULL)
- return NULL;
- n->n_type = type;
- n->n_str = NULL;
- n->n_lineno = 0;
- n->n_nchildren = 0;
- n->n_child = NULL;
- return n;
+ node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
+ if (n == NULL)
+ return NULL;
+ n->n_type = type;
+ n->n_str = NULL;
+ n->n_lineno = 0;
+ n->n_nchildren = 0;
+ n->n_child = NULL;
+ return n;
}
/* See comments at XXXROUNDUP below. Returns -1 on overflow. */
static int
fancy_roundup(int n)
{
- /* Round up to the closest power of 2 >= n. */
- int result = 256;
- assert(n > 128);
- while (result < n) {
- result <<= 1;
- if (result <= 0)
- return -1;
- }
- return result;
+ /* Round up to the closest power of 2 >= n. */
+ int result = 256;
+ assert(n > 128);
+ while (result < n) {
+ result <<= 1;
+ if (result <= 0)
+ return -1;
+ }
+ return result;
}
/* A gimmick to make massive numbers of reallocs quicker. The result is
@@ -70,46 +70,46 @@ fancy_roundup(int n)
* Note that this would be straightforward if a node stored its current
* capacity. The code is tricky to avoid that.
*/
-#define XXXROUNDUP(n) ((n) <= 1 ? (n) : \
- (n) <= 128 ? (((n) + 3) & ~3) : \
- fancy_roundup(n))
+#define XXXROUNDUP(n) ((n) <= 1 ? (n) : \
+ (n) <= 128 ? (((n) + 3) & ~3) : \
+ fancy_roundup(n))
int
PyNode_AddChild(register node *n1, int type, char *str, int lineno, int col_offset)
{
- const int nch = n1->n_nchildren;
- int current_capacity;
- int required_capacity;
- node *n;
+ const int nch = n1->n_nchildren;
+ int current_capacity;
+ int required_capacity;
+ node *n;
- if (nch == INT_MAX || nch < 0)
- return E_OVERFLOW;
+ if (nch == INT_MAX || nch < 0)
+ return E_OVERFLOW;
- current_capacity = XXXROUNDUP(nch);
- required_capacity = XXXROUNDUP(nch + 1);
- if (current_capacity < 0 || required_capacity < 0)
- return E_OVERFLOW;
- if (current_capacity < required_capacity) {
- if (required_capacity > PY_SIZE_MAX / sizeof(node)) {
- return E_NOMEM;
- }
- n = n1->n_child;
- n = (node *) PyObject_REALLOC(n,
- required_capacity * sizeof(node));
- if (n == NULL)
- return E_NOMEM;
- n1->n_child = n;
- }
+ current_capacity = XXXROUNDUP(nch);
+ required_capacity = XXXROUNDUP(nch + 1);
+ if (current_capacity < 0 || required_capacity < 0)
+ return E_OVERFLOW;
+ if (current_capacity < required_capacity) {
+ if (required_capacity > PY_SIZE_MAX / sizeof(node)) {
+ return E_NOMEM;
+ }
+ n = n1->n_child;
+ n = (node *) PyObject_REALLOC(n,
+ required_capacity * sizeof(node));
+ if (n == NULL)
+ return E_NOMEM;
+ n1->n_child = n;
+ }
- n = &n1->n_child[n1->n_nchildren++];
- n->n_type = type;
- n->n_str = str;
- n->n_lineno = lineno;
- n->n_col_offset = col_offset;
- n->n_nchildren = 0;
- n->n_child = NULL;
- return 0;
+ n = &n1->n_child[n1->n_nchildren++];
+ n->n_type = type;
+ n->n_str = str;
+ n->n_lineno = lineno;
+ n->n_col_offset = col_offset;
+ n->n_nchildren = 0;
+ n->n_child = NULL;
+ return 0;
}
/* Forward */
@@ -119,20 +119,20 @@ static void freechildren(node *);
void
PyNode_Free(node *n)
{
- if (n != NULL) {
- freechildren(n);
- PyObject_FREE(n);
- }
+ if (n != NULL) {
+ freechildren(n);
+ PyObject_FREE(n);
+ }
}
static void
freechildren(node *n)
{
- int i;
- for (i = NCH(n); --i >= 0; )
- freechildren(CHILD(n, i));
- if (n->n_child != NULL)
- PyObject_FREE(n->n_child);
- if (STR(n) != NULL)
- PyObject_FREE(STR(n));
+ int i;
+ for (i = NCH(n); --i >= 0; )
+ freechildren(CHILD(n, i));
+ if (n->n_child != NULL)
+ PyObject_FREE(n->n_child);
+ if (STR(n) != NULL)
+ PyObject_FREE(STR(n));
}
diff --git a/Parser/parser.c b/Parser/parser.c
index 83e5e6d..b505fe0 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -29,7 +29,7 @@ static void s_reset(stack *);
static void
s_reset(stack *s)
{
- s->s_top = &s->s_base[MAXSTACK];
+ s->s_top = &s->s_base[MAXSTACK];
}
#define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
@@ -37,16 +37,16 @@ s_reset(stack *s)
static int
s_push(register stack *s, dfa *d, node *parent)
{
- register stackentry *top;
- if (s->s_top == s->s_base) {
- fprintf(stderr, "s_push: parser stack overflow\n");
- return E_NOMEM;
- }
- top = --s->s_top;
- top->s_dfa = d;
- top->s_parent = parent;
- top->s_state = 0;
- return 0;
+ register stackentry *top;
+ if (s->s_top == s->s_base) {
+ fprintf(stderr, "s_push: parser stack overflow\n");
+ return E_NOMEM;
+ }
+ top = --s->s_top;
+ top->s_dfa = d;
+ top->s_parent = parent;
+ top->s_state = 0;
+ return 0;
}
#ifdef Py_DEBUG
@@ -54,9 +54,9 @@ s_push(register stack *s, dfa *d, node *parent)
static void
s_pop(register stack *s)
{
- if (s_empty(s))
- Py_FatalError("s_pop: parser stack underflow -- FATAL");
- s->s_top++;
+ if (s_empty(s))
+ Py_FatalError("s_pop: parser stack underflow -- FATAL");
+ s->s_top++;
}
#else /* !Py_DEBUG */
@@ -71,34 +71,34 @@ s_pop(register stack *s)
parser_state *
PyParser_New(grammar *g, int start)
{
- parser_state *ps;
-
- if (!g->g_accel)
- PyGrammar_AddAccelerators(g);
- ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
- if (ps == NULL)
- return NULL;
- ps->p_grammar = g;
+ parser_state *ps;
+
+ if (!g->g_accel)
+ PyGrammar_AddAccelerators(g);
+ ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
+ if (ps == NULL)
+ return NULL;
+ ps->p_grammar = g;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
- ps->p_flags = 0;
+ ps->p_flags = 0;
#endif
- ps->p_tree = PyNode_New(start);
- if (ps->p_tree == NULL) {
- PyMem_FREE(ps);
- return NULL;
- }
- s_reset(&ps->p_stack);
- (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
- return ps;
+ ps->p_tree = PyNode_New(start);
+ if (ps->p_tree == NULL) {
+ PyMem_FREE(ps);
+ return NULL;
+ }
+ s_reset(&ps->p_stack);
+ (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
+ return ps;
}
void
PyParser_Delete(parser_state *ps)
{
- /* NB If you want to save the parse tree,
- you must set p_tree to NULL before calling delparser! */
- PyNode_Free(ps->p_tree);
- PyMem_FREE(ps);
+ /* NB If you want to save the parse tree,
+ you must set p_tree to NULL before calling delparser! */
+ PyNode_Free(ps->p_tree);
+ PyMem_FREE(ps);
}
@@ -107,27 +107,27 @@ PyParser_Delete(parser_state *ps)
static int
shift(register stack *s, int type, char *str, int newstate, int lineno, int col_offset)
{
- int err;
- assert(!s_empty(s));
- err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
- if (err)
- return err;
- s->s_top->s_state = newstate;
- return 0;
+ int err;
+ assert(!s_empty(s));
+ err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
+ if (err)
+ return err;
+ s->s_top->s_state = newstate;
+ return 0;
}
static int
push(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
{
- int err;
- register node *n;
- n = s->s_top->s_parent;
- assert(!s_empty(s));
- err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
- if (err)
- return err;
- s->s_top->s_state = newstate;
- return s_push(s, d, CHILD(n, NCH(n)-1));
+ int err;
+ register node *n;
+ n = s->s_top->s_parent;
+ assert(!s_empty(s));
+ err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
+ if (err)
+ return err;
+ s->s_top->s_state = newstate;
+ return s_push(s, d, CHILD(n, NCH(n)-1));
}
@@ -136,47 +136,47 @@ push(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offs
static int
classify(parser_state *ps, int type, char *str)
{
- grammar *g = ps->p_grammar;
- register int n = g->g_ll.ll_nlabels;
-
- if (type == NAME) {
- register char *s = str;
- register label *l = g->g_ll.ll_label;
- register int i;
- for (i = n; i > 0; i--, l++) {
- if (l->lb_type != NAME || l->lb_str == NULL ||
- l->lb_str[0] != s[0] ||
- strcmp(l->lb_str, s) != 0)
- continue;
+ grammar *g = ps->p_grammar;
+ register int n = g->g_ll.ll_nlabels;
+
+ if (type == NAME) {
+ register char *s = str;
+ register label *l = g->g_ll.ll_label;
+ register int i;
+ for (i = n; i > 0; i--, l++) {
+ if (l->lb_type != NAME || l->lb_str == NULL ||
+ l->lb_str[0] != s[0] ||
+ strcmp(l->lb_str, s) != 0)
+ continue;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
- /* Leaving this in as an example */
- if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
- if (s[0] == 'w' && strcmp(s, "with") == 0)
- break; /* not a keyword yet */
- else if (s[0] == 'a' && strcmp(s, "as") == 0)
- break; /* not a keyword yet */
- }
+ /* Leaving this in as an example */
+ if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
+ if (s[0] == 'w' && strcmp(s, "with") == 0)
+ break; /* not a keyword yet */
+ else if (s[0] == 'a' && strcmp(s, "as") == 0)
+ break; /* not a keyword yet */
+ }
#endif
#endif
- D(printf("It's a keyword\n"));
- return n - i;
- }
- }
-
- {
- register label *l = g->g_ll.ll_label;
- register int i;
- for (i = n; i > 0; i--, l++) {
- if (l->lb_type == type && l->lb_str == NULL) {
- D(printf("It's a token we know\n"));
- return n - i;
- }
- }
- }
-
- D(printf("Illegal token\n"));
- return -1;
+ D(printf("It's a keyword\n"));
+ return n - i;
+ }
+ }
+
+ {
+ register label *l = g->g_ll.ll_label;
+ register int i;
+ for (i = n; i > 0; i--, l++) {
+ if (l->lb_type == type && l->lb_str == NULL) {
+ D(printf("It's a token we know\n"));
+ return n - i;
+ }
+ }
+ }
+
+ D(printf("Illegal token\n"));
+ return -1;
}
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
@@ -185,152 +185,152 @@ classify(parser_state *ps, int type, char *str)
static void
future_hack(parser_state *ps)
{
- node *n = ps->p_stack.s_top->s_parent;
- node *ch, *cch;
- int i;
-
- /* from __future__ import ..., must have at least 4 children */
- n = CHILD(n, 0);
- if (NCH(n) < 4)
- return;
- ch = CHILD(n, 0);
- if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0)
- return;
- ch = CHILD(n, 1);
- if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
- strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
- return;
- ch = CHILD(n, 3);
- /* ch can be a star, a parenthesis or import_as_names */
- if (TYPE(ch) == STAR)
- return;
- if (TYPE(ch) == LPAR)
- ch = CHILD(n, 4);
-
- for (i = 0; i < NCH(ch); i += 2) {
- cch = CHILD(ch, i);
- if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) {
- char *str_ch = STR(CHILD(cch, 0));
- if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) {
- ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
- } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) {
- ps->p_flags |= CO_FUTURE_PRINT_FUNCTION;
- } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) {
- ps->p_flags |= CO_FUTURE_UNICODE_LITERALS;
- }
- }
- }
+ node *n = ps->p_stack.s_top->s_parent;
+ node *ch, *cch;
+ int i;
+
+ /* from __future__ import ..., must have at least 4 children */
+ n = CHILD(n, 0);
+ if (NCH(n) < 4)
+ return;
+ ch = CHILD(n, 0);
+ if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0)
+ return;
+ ch = CHILD(n, 1);
+ if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
+ strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
+ return;
+ ch = CHILD(n, 3);
+ /* ch can be a star, a parenthesis or import_as_names */
+ if (TYPE(ch) == STAR)
+ return;
+ if (TYPE(ch) == LPAR)
+ ch = CHILD(n, 4);
+
+ for (i = 0; i < NCH(ch); i += 2) {
+ cch = CHILD(ch, i);
+ if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) {
+ char *str_ch = STR(CHILD(cch, 0));
+ if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) {
+ ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
+ } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) {
+ ps->p_flags |= CO_FUTURE_PRINT_FUNCTION;
+ } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) {
+ ps->p_flags |= CO_FUTURE_UNICODE_LITERALS;
+ }
+ }
+ }
}
#endif
#endif /* future keyword */
int
PyParser_AddToken(register parser_state *ps, register int type, char *str,
- int lineno, int col_offset, int *expected_ret)
+ int lineno, int col_offset, int *expected_ret)
{
- register int ilabel;
- int err;
-
- D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
-
- /* Find out which label this token is */
- ilabel = classify(ps, type, str);
- if (ilabel < 0)
- return E_SYNTAX;
-
- /* Loop until the token is shifted or an error occurred */
- for (;;) {
- /* Fetch the current dfa and state */
- register dfa *d = ps->p_stack.s_top->s_dfa;
- register state *s = &d->d_state[ps->p_stack.s_top->s_state];
-
- D(printf(" DFA '%s', state %d:",
- d->d_name, ps->p_stack.s_top->s_state));
-
- /* Check accelerator */
- if (s->s_lower <= ilabel && ilabel < s->s_upper) {
- register int x = s->s_accel[ilabel - s->s_lower];
- if (x != -1) {
- if (x & (1<<7)) {
- /* Push non-terminal */
- int nt = (x >> 8) + NT_OFFSET;
- int arrow = x & ((1<<7)-1);
- dfa *d1 = PyGrammar_FindDFA(
- ps->p_grammar, nt);
- if ((err = push(&ps->p_stack, nt, d1,
- arrow, lineno, col_offset)) > 0) {
- D(printf(" MemError: push\n"));
- return err;
- }
- D(printf(" Push ...\n"));
- continue;
- }
-
- /* Shift the token */
- if ((err = shift(&ps->p_stack, type, str,
- x, lineno, col_offset)) > 0) {
- D(printf(" MemError: shift.\n"));
- return err;
- }
- D(printf(" Shift.\n"));
- /* Pop while we are in an accept-only state */
- while (s = &d->d_state
- [ps->p_stack.s_top->s_state],
- s->s_accept && s->s_narcs == 1) {
- D(printf(" DFA '%s', state %d: "
- "Direct pop.\n",
- d->d_name,
- ps->p_stack.s_top->s_state));
+ register int ilabel;
+ int err;
+
+ D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
+
+ /* Find out which label this token is */
+ ilabel = classify(ps, type, str);
+ if (ilabel < 0)
+ return E_SYNTAX;
+
+ /* Loop until the token is shifted or an error occurred */
+ for (;;) {
+ /* Fetch the current dfa and state */
+ register dfa *d = ps->p_stack.s_top->s_dfa;
+ register state *s = &d->d_state[ps->p_stack.s_top->s_state];
+
+ D(printf(" DFA '%s', state %d:",
+ d->d_name, ps->p_stack.s_top->s_state));
+
+ /* Check accelerator */
+ if (s->s_lower <= ilabel && ilabel < s->s_upper) {
+ register int x = s->s_accel[ilabel - s->s_lower];
+ if (x != -1) {
+ if (x & (1<<7)) {
+ /* Push non-terminal */
+ int nt = (x >> 8) + NT_OFFSET;
+ int arrow = x & ((1<<7)-1);
+ dfa *d1 = PyGrammar_FindDFA(
+ ps->p_grammar, nt);
+ if ((err = push(&ps->p_stack, nt, d1,
+ arrow, lineno, col_offset)) > 0) {
+ D(printf(" MemError: push\n"));
+ return err;
+ }
+ D(printf(" Push ...\n"));
+ continue;
+ }
+
+ /* Shift the token */
+ if ((err = shift(&ps->p_stack, type, str,
+ x, lineno, col_offset)) > 0) {
+ D(printf(" MemError: shift.\n"));
+ return err;
+ }
+ D(printf(" Shift.\n"));
+ /* Pop while we are in an accept-only state */
+ while (s = &d->d_state
+ [ps->p_stack.s_top->s_state],
+ s->s_accept && s->s_narcs == 1) {
+ D(printf(" DFA '%s', state %d: "
+ "Direct pop.\n",
+ d->d_name,
+ ps->p_stack.s_top->s_state));
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
- if (d->d_name[0] == 'i' &&
- strcmp(d->d_name,
- "import_stmt") == 0)
- future_hack(ps);
+ if (d->d_name[0] == 'i' &&
+ strcmp(d->d_name,
+ "import_stmt") == 0)
+ future_hack(ps);
#endif
#endif
- s_pop(&ps->p_stack);
- if (s_empty(&ps->p_stack)) {
- D(printf(" ACCEPT.\n"));
- return E_DONE;
- }
- d = ps->p_stack.s_top->s_dfa;
- }
- return E_OK;
- }
- }
-
- if (s->s_accept) {
+ s_pop(&ps->p_stack);
+ if (s_empty(&ps->p_stack)) {
+ D(printf(" ACCEPT.\n"));
+ return E_DONE;
+ }
+ d = ps->p_stack.s_top->s_dfa;
+ }
+ return E_OK;
+ }
+ }
+
+ if (s->s_accept) {
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
#if 0
- if (d->d_name[0] == 'i' &&
- strcmp(d->d_name, "import_stmt") == 0)
- future_hack(ps);
+ if (d->d_name[0] == 'i' &&
+ strcmp(d->d_name, "import_stmt") == 0)
+ future_hack(ps);
#endif
#endif
- /* Pop this dfa and try again */
- s_pop(&ps->p_stack);
- D(printf(" Pop ...\n"));
- if (s_empty(&ps->p_stack)) {
- D(printf(" Error: bottom of stack.\n"));
- return E_SYNTAX;
- }
- continue;
- }
-
- /* Stuck, report syntax error */
- D(printf(" Error.\n"));
- if (expected_ret) {
- if (s->s_lower == s->s_upper - 1) {
- /* Only one possible expected token */
- *expected_ret = ps->p_grammar->
- g_ll.ll_label[s->s_lower].lb_type;
- }
- else
- *expected_ret = -1;
- }
- return E_SYNTAX;
- }
+ /* Pop this dfa and try again */
+ s_pop(&ps->p_stack);
+ D(printf(" Pop ...\n"));
+ if (s_empty(&ps->p_stack)) {
+ D(printf(" Error: bottom of stack.\n"));
+ return E_SYNTAX;
+ }
+ continue;
+ }
+
+ /* Stuck, report syntax error */
+ D(printf(" Error.\n"));
+ if (expected_ret) {
+ if (s->s_lower == s->s_upper - 1) {
+ /* Only one possible expected token */
+ *expected_ret = ps->p_grammar->
+ g_ll.ll_label[s->s_lower].lb_type;
+ }
+ else
+ *expected_ret = -1;
+ }
+ return E_SYNTAX;
+ }
}
@@ -341,62 +341,62 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
void
dumptree(grammar *g, node *n)
{
- int i;
-
- if (n == NULL)
- printf("NIL");
- else {
- label l;
- l.lb_type = TYPE(n);
- l.lb_str = STR(n);
- printf("%s", PyGrammar_LabelRepr(&l));
- if (ISNONTERMINAL(TYPE(n))) {
- printf("(");
- for (i = 0; i < NCH(n); i++) {
- if (i > 0)
- printf(",");
- dumptree(g, CHILD(n, i));
- }
- printf(")");
- }
- }
+ int i;
+
+ if (n == NULL)
+ printf("NIL");
+ else {
+ label l;
+ l.lb_type = TYPE(n);
+ l.lb_str = STR(n);
+ printf("%s", PyGrammar_LabelRepr(&l));
+ if (ISNONTERMINAL(TYPE(n))) {
+ printf("(");
+ for (i = 0; i < NCH(n); i++) {
+ if (i > 0)
+ printf(",");
+ dumptree(g, CHILD(n, i));
+ }
+ printf(")");
+ }
+ }
}
void
showtree(grammar *g, node *n)
{
- int i;
-
- if (n == NULL)
- return;
- if (ISNONTERMINAL(TYPE(n))) {
- for (i = 0; i < NCH(n); i++)
- showtree(g, CHILD(n, i));
- }
- else if (ISTERMINAL(TYPE(n))) {
- printf("%s", _PyParser_TokenNames[TYPE(n)]);
- if (TYPE(n) == NUMBER || TYPE(n) == NAME)
- printf("(%s)", STR(n));
- printf(" ");
- }
- else
- printf("? ");
+ int i;
+
+ if (n == NULL)
+ return;
+ if (ISNONTERMINAL(TYPE(n))) {
+ for (i = 0; i < NCH(n); i++)
+ showtree(g, CHILD(n, i));
+ }
+ else if (ISTERMINAL(TYPE(n))) {
+ printf("%s", _PyParser_TokenNames[TYPE(n)]);
+ if (TYPE(n) == NUMBER || TYPE(n) == NAME)
+ printf("(%s)", STR(n));
+ printf(" ");
+ }
+ else
+ printf("? ");
}
void
printtree(parser_state *ps)
{
- if (Py_DebugFlag) {
- printf("Parse tree:\n");
- dumptree(ps->p_grammar, ps->p_tree);
- printf("\n");
- printf("Tokens:\n");
- showtree(ps->p_grammar, ps->p_tree);
- printf("\n");
- }
- printf("Listing:\n");
- PyNode_ListTree(ps->p_tree);
- printf("\n");
+ if (Py_DebugFlag) {
+ printf("Parse tree:\n");
+ dumptree(ps->p_grammar, ps->p_tree);
+ printf("\n");
+ printf("Tokens:\n");
+ showtree(ps->p_grammar, ps->p_tree);
+ printf("\n");
+ }
+ printf("Listing:\n");
+ PyNode_ListTree(ps->p_tree);
+ printf("\n");
}
#endif /* Py_DEBUG */
@@ -431,15 +431,15 @@ symbol.
As an example, consider this grammar:
-expr: term (OP term)*
-term: CONSTANT | '(' expr ')'
+expr: term (OP term)*
+term: CONSTANT | '(' expr ')'
The DFA corresponding to the rule for expr is:
------->.---term-->.------->
- ^ |
- | |
- \----OP----/
+ ^ |
+ | |
+ \----OP----/
The parse tree generated for the input a+b is:
diff --git a/Parser/parsetok.c b/Parser/parsetok.c
index 1470327..16cf5cb 100644
--- a/Parser/parsetok.c
+++ b/Parser/parsetok.c
@@ -19,84 +19,84 @@ static void initerr(perrdetail *err_ret, const char* filename);
node *
PyParser_ParseString(const char *s, grammar *g, int start, perrdetail *err_ret)
{
- return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0);
+ return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0);
}
node *
PyParser_ParseStringFlags(const char *s, grammar *g, int start,
- perrdetail *err_ret, int flags)
+ perrdetail *err_ret, int flags)
{
- return PyParser_ParseStringFlagsFilename(s, NULL,
- g, start, err_ret, flags);
+ return PyParser_ParseStringFlagsFilename(s, NULL,
+ g, start, err_ret, flags);
}
node *
PyParser_ParseStringFlagsFilename(const char *s, const char *filename,
- grammar *g, int start,
- perrdetail *err_ret, int flags)
+ grammar *g, int start,
+ perrdetail *err_ret, int flags)
{
- int iflags = flags;
- return PyParser_ParseStringFlagsFilenameEx(s, filename, g, start,
- err_ret, &iflags);
+ int iflags = flags;
+ return PyParser_ParseStringFlagsFilenameEx(s, filename, g, start,
+ err_ret, &iflags);
}
node *
PyParser_ParseStringFlagsFilenameEx(const char *s, const char *filename,
- grammar *g, int start,
- perrdetail *err_ret, int *flags)
+ grammar *g, int start,
+ perrdetail *err_ret, int *flags)
{
- struct tok_state *tok;
+ struct tok_state *tok;
- initerr(err_ret, filename);
+ initerr(err_ret, filename);
- if (*flags & PyPARSE_IGNORE_COOKIE)
- tok = PyTokenizer_FromUTF8(s);
- else
- tok = PyTokenizer_FromString(s);
- if (tok == NULL) {
- err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
- return NULL;
- }
+ if (*flags & PyPARSE_IGNORE_COOKIE)
+ tok = PyTokenizer_FromUTF8(s);
+ else
+ tok = PyTokenizer_FromString(s);
+ if (tok == NULL) {
+ err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
+ return NULL;
+ }
- tok->filename = filename ? filename : "<string>";
- return parsetok(tok, g, start, err_ret, flags);
+ tok->filename = filename ? filename : "<string>";
+ return parsetok(tok, g, start, err_ret, flags);
}
/* Parse input coming from a file. Return error code, print some errors. */
node *
PyParser_ParseFile(FILE *fp, const char *filename, grammar *g, int start,
- char *ps1, char *ps2, perrdetail *err_ret)
+ char *ps1, char *ps2, perrdetail *err_ret)
{
- return PyParser_ParseFileFlags(fp, filename, NULL,
- g, start, ps1, ps2, err_ret, 0);
+ return PyParser_ParseFileFlags(fp, filename, NULL,
+ g, start, ps1, ps2, err_ret, 0);
}
node *
PyParser_ParseFileFlags(FILE *fp, const char *filename, const char *enc,
- grammar *g, int start,
- char *ps1, char *ps2, perrdetail *err_ret, int flags)
+ grammar *g, int start,
+ char *ps1, char *ps2, perrdetail *err_ret, int flags)
{
- int iflags = flags;
- return PyParser_ParseFileFlagsEx(fp, filename, enc, g, start, ps1,
- ps2, err_ret, &iflags);
+ int iflags = flags;
+ return PyParser_ParseFileFlagsEx(fp, filename, enc, g, start, ps1,
+ ps2, err_ret, &iflags);
}
node *
-PyParser_ParseFileFlagsEx(FILE *fp, const char *filename,
- const char *enc, grammar *g, int start,
- char *ps1, char *ps2, perrdetail *err_ret, int *flags)
+PyParser_ParseFileFlagsEx(FILE *fp, const char *filename,
+ const char *enc, grammar *g, int start,
+ char *ps1, char *ps2, perrdetail *err_ret, int *flags)
{
- struct tok_state *tok;
+ struct tok_state *tok;
- initerr(err_ret, filename);
+ initerr(err_ret, filename);
- if ((tok = PyTokenizer_FromFile(fp, (char *)enc, ps1, ps2)) == NULL) {
- err_ret->error = E_NOMEM;
- return NULL;
- }
- tok->filename = filename;
- return parsetok(tok, g, start, err_ret, flags);
+ if ((tok = PyTokenizer_FromFile(fp, (char *)enc, ps1, ps2)) == NULL) {
+ err_ret->error = E_NOMEM;
+ return NULL;
+ }
+ tok->filename = filename;
+ return parsetok(tok, g, start, err_ret, flags);
}
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
@@ -110,9 +110,9 @@ static char as_msg[] =
static void
warn(const char *msg, const char *filename, int lineno)
{
- if (filename == NULL)
- filename = "<string>";
- PySys_WriteStderr(msg, filename, lineno);
+ if (filename == NULL)
+ filename = "<string>";
+ PySys_WriteStderr(msg, filename, lineno);
}
#endif
#endif
@@ -122,151 +122,151 @@ warn(const char *msg, const char *filename, int lineno)
static node *
parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
- int *flags)
+ int *flags)
{
- parser_state *ps;
- node *n;
- int started = 0, handling_import = 0, handling_with = 0;
-
- if ((ps = PyParser_New(g, start)) == NULL) {
- fprintf(stderr, "no mem for new parser\n");
- err_ret->error = E_NOMEM;
- PyTokenizer_Free(tok);
- return NULL;
- }
+ parser_state *ps;
+ node *n;
+ int started = 0, handling_import = 0, handling_with = 0;
+
+ if ((ps = PyParser_New(g, start)) == NULL) {
+ fprintf(stderr, "no mem for new parser\n");
+ err_ret->error = E_NOMEM;
+ PyTokenizer_Free(tok);
+ return NULL;
+ }
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
- if (*flags & PyPARSE_BARRY_AS_BDFL)
- ps->p_flags |= CO_FUTURE_BARRY_AS_BDFL;
+ if (*flags & PyPARSE_BARRY_AS_BDFL)
+ ps->p_flags |= CO_FUTURE_BARRY_AS_BDFL;
#endif
- for (;;) {
- char *a, *b;
- int type;
- size_t len;
- char *str;
- int col_offset;
-
- type = PyTokenizer_Get(tok, &a, &b);
- if (type == ERRORTOKEN) {
- err_ret->error = tok->done;
- break;
- }
- if (type == ENDMARKER && started) {
- type = NEWLINE; /* Add an extra newline */
- handling_with = handling_import = 0;
- started = 0;
- /* Add the right number of dedent tokens,
- except if a certain flag is given --
- codeop.py uses this. */
- if (tok->indent &&
- !(*flags & PyPARSE_DONT_IMPLY_DEDENT))
- {
- tok->pendin = -tok->indent;
- tok->indent = 0;
- }
- }
- else
- started = 1;
- len = b - a; /* XXX this may compute NULL - NULL */
- str = (char *) PyObject_MALLOC(len + 1);
- if (str == NULL) {
- fprintf(stderr, "no mem for next token\n");
- err_ret->error = E_NOMEM;
- break;
- }
- if (len > 0)
- strncpy(str, a, len);
- str[len] = '\0';
+ for (;;) {
+ char *a, *b;
+ int type;
+ size_t len;
+ char *str;
+ int col_offset;
+
+ type = PyTokenizer_Get(tok, &a, &b);
+ if (type == ERRORTOKEN) {
+ err_ret->error = tok->done;
+ break;
+ }
+ if (type == ENDMARKER && started) {
+ type = NEWLINE; /* Add an extra newline */
+ handling_with = handling_import = 0;
+ started = 0;
+ /* Add the right number of dedent tokens,
+ except if a certain flag is given --
+ codeop.py uses this. */
+ if (tok->indent &&
+ !(*flags & PyPARSE_DONT_IMPLY_DEDENT))
+ {
+ tok->pendin = -tok->indent;
+ tok->indent = 0;
+ }
+ }
+ else
+ started = 1;
+ len = b - a; /* XXX this may compute NULL - NULL */
+ str = (char *) PyObject_MALLOC(len + 1);
+ if (str == NULL) {
+ fprintf(stderr, "no mem for next token\n");
+ err_ret->error = E_NOMEM;
+ break;
+ }
+ if (len > 0)
+ strncpy(str, a, len);
+ str[len] = '\0';
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
- if (type == NOTEQUAL) {
- if (!(ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
- strcmp(str, "!=")) {
- err_ret->error = E_SYNTAX;
- break;
- }
- else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
- strcmp(str, "<>")) {
- err_ret->text = "with Barry as BDFL, use '<>' "
- "instead of '!='";
- err_ret->error = E_SYNTAX;
- break;
- }
- }
+ if (type == NOTEQUAL) {
+ if (!(ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
+ strcmp(str, "!=")) {
+ err_ret->error = E_SYNTAX;
+ break;
+ }
+ else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
+ strcmp(str, "<>")) {
+ err_ret->text = "with Barry as BDFL, use '<>' "
+ "instead of '!='";
+ err_ret->error = E_SYNTAX;
+ break;
+ }
+ }
#endif
- if (a >= tok->line_start)
- col_offset = a - tok->line_start;
- else
- col_offset = -1;
-
- if ((err_ret->error =
- PyParser_AddToken(ps, (int)type, str,
- tok->lineno, col_offset,
- &(err_ret->expected))) != E_OK) {
- if (err_ret->error != E_DONE) {
- PyObject_FREE(str);
- err_ret->token = type;
- }
- break;
- }
- }
-
- if (err_ret->error == E_DONE) {
- n = ps->p_tree;
- ps->p_tree = NULL;
- }
- else
- n = NULL;
+ if (a >= tok->line_start)
+ col_offset = a - tok->line_start;
+ else
+ col_offset = -1;
+
+ if ((err_ret->error =
+ PyParser_AddToken(ps, (int)type, str,
+ tok->lineno, col_offset,
+ &(err_ret->expected))) != E_OK) {
+ if (err_ret->error != E_DONE) {
+ PyObject_FREE(str);
+ err_ret->token = type;
+ }
+ break;
+ }
+ }
+
+ if (err_ret->error == E_DONE) {
+ n = ps->p_tree;
+ ps->p_tree = NULL;
+ }
+ else
+ n = NULL;
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
- *flags = ps->p_flags;
+ *flags = ps->p_flags;
#endif
- PyParser_Delete(ps);
-
- if (n == NULL) {
- if (tok->lineno <= 1 && tok->done == E_EOF)
- err_ret->error = E_EOF;
- err_ret->lineno = tok->lineno;
- if (tok->buf != NULL) {
- size_t len;
- assert(tok->cur - tok->buf < INT_MAX);
- err_ret->offset = (int)(tok->cur - tok->buf);
- len = tok->inp - tok->buf;
- err_ret->text = (char *) PyObject_MALLOC(len + 1);
- if (err_ret->text != NULL) {
- if (len > 0)
- strncpy(err_ret->text, tok->buf, len);
- err_ret->text[len] = '\0';
- }
- }
- } else if (tok->encoding != NULL) {
- node* r = PyNode_New(encoding_decl);
- if (!r) {
- err_ret->error = E_NOMEM;
- n = NULL;
- goto done;
- }
- r->n_str = tok->encoding;
- r->n_nchildren = 1;
- r->n_child = n;
- tok->encoding = NULL;
- n = r;
- }
+ PyParser_Delete(ps);
+
+ if (n == NULL) {
+ if (tok->lineno <= 1 && tok->done == E_EOF)
+ err_ret->error = E_EOF;
+ err_ret->lineno = tok->lineno;
+ if (tok->buf != NULL) {
+ size_t len;
+ assert(tok->cur - tok->buf < INT_MAX);
+ err_ret->offset = (int)(tok->cur - tok->buf);
+ len = tok->inp - tok->buf;
+ err_ret->text = (char *) PyObject_MALLOC(len + 1);
+ if (err_ret->text != NULL) {
+ if (len > 0)
+ strncpy(err_ret->text, tok->buf, len);
+ err_ret->text[len] = '\0';
+ }
+ }
+ } else if (tok->encoding != NULL) {
+ node* r = PyNode_New(encoding_decl);
+ if (!r) {
+ err_ret->error = E_NOMEM;
+ n = NULL;
+ goto done;
+ }
+ r->n_str = tok->encoding;
+ r->n_nchildren = 1;
+ r->n_child = n;
+ tok->encoding = NULL;
+ n = r;
+ }
done:
- PyTokenizer_Free(tok);
+ PyTokenizer_Free(tok);
- return n;
+ return n;
}
static void
initerr(perrdetail *err_ret, const char *filename)
{
- err_ret->error = E_OK;
- err_ret->filename = filename;
- err_ret->lineno = 0;
- err_ret->offset = 0;
- err_ret->text = NULL;
- err_ret->token = -1;
- err_ret->expected = -1;
+ err_ret->error = E_OK;
+ err_ret->filename = filename;
+ err_ret->lineno = 0;
+ err_ret->offset = 0;
+ err_ret->text = NULL;
+ err_ret->token = -1;
+ err_ret->expected = -1;
}
diff --git a/Parser/pgen.c b/Parser/pgen.c
index 959a5d3..beaf53b 100644
--- a/Parser/pgen.c
+++ b/Parser/pgen.c
@@ -17,85 +17,85 @@ extern int Py_IgnoreEnvironmentFlag; /* needed by Py_GETENV */
/* PART ONE -- CONSTRUCT NFA -- Cf. Algorithm 3.2 from [Aho&Ullman 77] */
typedef struct _nfaarc {
- int ar_label;
- int ar_arrow;
+ int ar_label;
+ int ar_arrow;
} nfaarc;
typedef struct _nfastate {
- int st_narcs;
- nfaarc *st_arc;
+ int st_narcs;
+ nfaarc *st_arc;
} nfastate;
typedef struct _nfa {
- int nf_type;
- char *nf_name;
- int nf_nstates;
- nfastate *nf_state;
- int nf_start, nf_finish;
+ int nf_type;
+ char *nf_name;
+ int nf_nstates;
+ nfastate *nf_state;
+ int nf_start, nf_finish;
} nfa;
/* Forward */
static void compile_rhs(labellist *ll,
- nfa *nf, node *n, int *pa, int *pb);
+ nfa *nf, node *n, int *pa, int *pb);
static void compile_alt(labellist *ll,
- nfa *nf, node *n, int *pa, int *pb);
+ nfa *nf, node *n, int *pa, int *pb);
static void compile_item(labellist *ll,
- nfa *nf, node *n, int *pa, int *pb);
+ nfa *nf, node *n, int *pa, int *pb);
static void compile_atom(labellist *ll,
- nfa *nf, node *n, int *pa, int *pb);
+ nfa *nf, node *n, int *pa, int *pb);
static int
addnfastate(nfa *nf)
{
- nfastate *st;
-
- nf->nf_state = (nfastate *)PyObject_REALLOC(nf->nf_state,
- sizeof(nfastate) * (nf->nf_nstates + 1));
- if (nf->nf_state == NULL)
- Py_FatalError("out of mem");
- st = &nf->nf_state[nf->nf_nstates++];
- st->st_narcs = 0;
- st->st_arc = NULL;
- return st - nf->nf_state;
+ nfastate *st;
+
+ nf->nf_state = (nfastate *)PyObject_REALLOC(nf->nf_state,
+ sizeof(nfastate) * (nf->nf_nstates + 1));
+ if (nf->nf_state == NULL)
+ Py_FatalError("out of mem");
+ st = &nf->nf_state[nf->nf_nstates++];
+ st->st_narcs = 0;
+ st->st_arc = NULL;
+ return st - nf->nf_state;
}
static void
addnfaarc(nfa *nf, int from, int to, int lbl)
{
- nfastate *st;
- nfaarc *ar;
-
- st = &nf->nf_state[from];
- st->st_arc = (nfaarc *)PyObject_REALLOC(st->st_arc,
- sizeof(nfaarc) * (st->st_narcs + 1));
- if (st->st_arc == NULL)
- Py_FatalError("out of mem");
- ar = &st->st_arc[st->st_narcs++];
- ar->ar_label = lbl;
- ar->ar_arrow = to;
+ nfastate *st;
+ nfaarc *ar;
+
+ st = &nf->nf_state[from];
+ st->st_arc = (nfaarc *)PyObject_REALLOC(st->st_arc,
+ sizeof(nfaarc) * (st->st_narcs + 1));
+ if (st->st_arc == NULL)
+ Py_FatalError("out of mem");
+ ar = &st->st_arc[st->st_narcs++];
+ ar->ar_label = lbl;
+ ar->ar_arrow = to;
}
static nfa *
newnfa(char *name)
{
- nfa *nf;
- static int type = NT_OFFSET; /* All types will be disjunct */
-
- nf = (nfa *)PyObject_MALLOC(sizeof(nfa));
- if (nf == NULL)
- Py_FatalError("no mem for new nfa");
- nf->nf_type = type++;
- nf->nf_name = name; /* XXX strdup(name) ??? */
- nf->nf_nstates = 0;
- nf->nf_state = NULL;
- nf->nf_start = nf->nf_finish = -1;
- return nf;
+ nfa *nf;
+ static int type = NT_OFFSET; /* All types will be disjunct */
+
+ nf = (nfa *)PyObject_MALLOC(sizeof(nfa));
+ if (nf == NULL)
+ Py_FatalError("no mem for new nfa");
+ nf->nf_type = type++;
+ nf->nf_name = name; /* XXX strdup(name) ??? */
+ nf->nf_nstates = 0;
+ nf->nf_state = NULL;
+ nf->nf_start = nf->nf_finish = -1;
+ return nf;
}
typedef struct _nfagrammar {
- int gr_nnfas;
- nfa **gr_nfa;
- labellist gr_ll;
+ int gr_nnfas;
+ nfa **gr_nfa;
+ labellist gr_ll;
} nfagrammar;
/* Forward */
@@ -104,32 +104,32 @@ static void compile_rule(nfagrammar *gr, node *n);
static nfagrammar *
newnfagrammar(void)
{
- nfagrammar *gr;
-
- gr = (nfagrammar *)PyObject_MALLOC(sizeof(nfagrammar));
- if (gr == NULL)
- Py_FatalError("no mem for new nfa grammar");
- gr->gr_nnfas = 0;
- gr->gr_nfa = NULL;
- gr->gr_ll.ll_nlabels = 0;
- gr->gr_ll.ll_label = NULL;
- addlabel(&gr->gr_ll, ENDMARKER, "EMPTY");
- return gr;
+ nfagrammar *gr;
+
+ gr = (nfagrammar *)PyObject_MALLOC(sizeof(nfagrammar));
+ if (gr == NULL)
+ Py_FatalError("no mem for new nfa grammar");
+ gr->gr_nnfas = 0;
+ gr->gr_nfa = NULL;
+ gr->gr_ll.ll_nlabels = 0;
+ gr->gr_ll.ll_label = NULL;
+ addlabel(&gr->gr_ll, ENDMARKER, "EMPTY");
+ return gr;
}
static nfa *
addnfa(nfagrammar *gr, char *name)
{
- nfa *nf;
-
- nf = newnfa(name);
- gr->gr_nfa = (nfa **)PyObject_REALLOC(gr->gr_nfa,
- sizeof(nfa*) * (gr->gr_nnfas + 1));
- if (gr->gr_nfa == NULL)
- Py_FatalError("out of mem");
- gr->gr_nfa[gr->gr_nnfas++] = nf;
- addlabel(&gr->gr_ll, NAME, nf->nf_name);
- return nf;
+ nfa *nf;
+
+ nf = newnfa(name);
+ gr->gr_nfa = (nfa **)PyObject_REALLOC(gr->gr_nfa,
+ sizeof(nfa*) * (gr->gr_nnfas + 1));
+ if (gr->gr_nfa == NULL)
+ Py_FatalError("out of mem");
+ gr->gr_nfa[gr->gr_nnfas++] = nf;
+ addlabel(&gr->gr_ll, NAME, nf->nf_name);
+ return nf;
}
#ifdef Py_DEBUG
@@ -137,203 +137,203 @@ addnfa(nfagrammar *gr, char *name)
static char REQNFMT[] = "metacompile: less than %d children\n";
#define REQN(i, count) \
- if (i < count) { \
- fprintf(stderr, REQNFMT, count); \
- Py_FatalError("REQN"); \
- } else
+ if (i < count) { \
+ fprintf(stderr, REQNFMT, count); \
+ Py_FatalError("REQN"); \
+ } else
#else
-#define REQN(i, count) /* empty */
+#define REQN(i, count) /* empty */
#endif
static nfagrammar *
metacompile(node *n)
{
- nfagrammar *gr;
- int i;
-
- if (Py_DebugFlag)
- printf("Compiling (meta-) parse tree into NFA grammar\n");
- gr = newnfagrammar();
- REQ(n, MSTART);
- i = n->n_nchildren - 1; /* Last child is ENDMARKER */
- n = n->n_child;
- for (; --i >= 0; n++) {
- if (n->n_type != NEWLINE)
- compile_rule(gr, n);
- }
- return gr;
+ nfagrammar *gr;
+ int i;
+
+ if (Py_DebugFlag)
+ printf("Compiling (meta-) parse tree into NFA grammar\n");
+ gr = newnfagrammar();
+ REQ(n, MSTART);
+ i = n->n_nchildren - 1; /* Last child is ENDMARKER */
+ n = n->n_child;
+ for (; --i >= 0; n++) {
+ if (n->n_type != NEWLINE)
+ compile_rule(gr, n);
+ }
+ return gr;
}
static void
compile_rule(nfagrammar *gr, node *n)
{
- nfa *nf;
-
- REQ(n, RULE);
- REQN(n->n_nchildren, 4);
- n = n->n_child;
- REQ(n, NAME);
- nf = addnfa(gr, n->n_str);
- n++;
- REQ(n, COLON);
- n++;
- REQ(n, RHS);
- compile_rhs(&gr->gr_ll, nf, n, &nf->nf_start, &nf->nf_finish);
- n++;
- REQ(n, NEWLINE);
+ nfa *nf;
+
+ REQ(n, RULE);
+ REQN(n->n_nchildren, 4);
+ n = n->n_child;
+ REQ(n, NAME);
+ nf = addnfa(gr, n->n_str);
+ n++;
+ REQ(n, COLON);
+ n++;
+ REQ(n, RHS);
+ compile_rhs(&gr->gr_ll, nf, n, &nf->nf_start, &nf->nf_finish);
+ n++;
+ REQ(n, NEWLINE);
}
static void
compile_rhs(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
{
- int i;
- int a, b;
-
- REQ(n, RHS);
- i = n->n_nchildren;
- REQN(i, 1);
- n = n->n_child;
- REQ(n, ALT);
- compile_alt(ll, nf, n, pa, pb);
- if (--i <= 0)
- return;
- n++;
- a = *pa;
- b = *pb;
- *pa = addnfastate(nf);
- *pb = addnfastate(nf);
- addnfaarc(nf, *pa, a, EMPTY);
- addnfaarc(nf, b, *pb, EMPTY);
- for (; --i >= 0; n++) {
- REQ(n, VBAR);
- REQN(i, 1);
- --i;
- n++;
- REQ(n, ALT);
- compile_alt(ll, nf, n, &a, &b);
- addnfaarc(nf, *pa, a, EMPTY);
- addnfaarc(nf, b, *pb, EMPTY);
- }
+ int i;
+ int a, b;
+
+ REQ(n, RHS);
+ i = n->n_nchildren;
+ REQN(i, 1);
+ n = n->n_child;
+ REQ(n, ALT);
+ compile_alt(ll, nf, n, pa, pb);
+ if (--i <= 0)
+ return;
+ n++;
+ a = *pa;
+ b = *pb;
+ *pa = addnfastate(nf);
+ *pb = addnfastate(nf);
+ addnfaarc(nf, *pa, a, EMPTY);
+ addnfaarc(nf, b, *pb, EMPTY);
+ for (; --i >= 0; n++) {
+ REQ(n, VBAR);
+ REQN(i, 1);
+ --i;
+ n++;
+ REQ(n, ALT);
+ compile_alt(ll, nf, n, &a, &b);
+ addnfaarc(nf, *pa, a, EMPTY);
+ addnfaarc(nf, b, *pb, EMPTY);
+ }
}
static void
compile_alt(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
{
- int i;
- int a, b;
-
- REQ(n, ALT);
- i = n->n_nchildren;
- REQN(i, 1);
- n = n->n_child;
- REQ(n, ITEM);
- compile_item(ll, nf, n, pa, pb);
- --i;
- n++;
- for (; --i >= 0; n++) {
- REQ(n, ITEM);
- compile_item(ll, nf, n, &a, &b);
- addnfaarc(nf, *pb, a, EMPTY);
- *pb = b;
- }
+ int i;
+ int a, b;
+
+ REQ(n, ALT);
+ i = n->n_nchildren;
+ REQN(i, 1);
+ n = n->n_child;
+ REQ(n, ITEM);
+ compile_item(ll, nf, n, pa, pb);
+ --i;
+ n++;
+ for (; --i >= 0; n++) {
+ REQ(n, ITEM);
+ compile_item(ll, nf, n, &a, &b);
+ addnfaarc(nf, *pb, a, EMPTY);
+ *pb = b;
+ }
}
static void
compile_item(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
{
- int i;
- int a, b;
-
- REQ(n, ITEM);
- i = n->n_nchildren;
- REQN(i, 1);
- n = n->n_child;
- if (n->n_type == LSQB) {
- REQN(i, 3);
- n++;
- REQ(n, RHS);
- *pa = addnfastate(nf);
- *pb = addnfastate(nf);
- addnfaarc(nf, *pa, *pb, EMPTY);
- compile_rhs(ll, nf, n, &a, &b);
- addnfaarc(nf, *pa, a, EMPTY);
- addnfaarc(nf, b, *pb, EMPTY);
- REQN(i, 1);
- n++;
- REQ(n, RSQB);
- }
- else {
- compile_atom(ll, nf, n, pa, pb);
- if (--i <= 0)
- return;
- n++;
- addnfaarc(nf, *pb, *pa, EMPTY);
- if (n->n_type == STAR)
- *pb = *pa;
- else
- REQ(n, PLUS);
- }
+ int i;
+ int a, b;
+
+ REQ(n, ITEM);
+ i = n->n_nchildren;
+ REQN(i, 1);
+ n = n->n_child;
+ if (n->n_type == LSQB) {
+ REQN(i, 3);
+ n++;
+ REQ(n, RHS);
+ *pa = addnfastate(nf);
+ *pb = addnfastate(nf);
+ addnfaarc(nf, *pa, *pb, EMPTY);
+ compile_rhs(ll, nf, n, &a, &b);
+ addnfaarc(nf, *pa, a, EMPTY);
+ addnfaarc(nf, b, *pb, EMPTY);
+ REQN(i, 1);
+ n++;
+ REQ(n, RSQB);
+ }
+ else {
+ compile_atom(ll, nf, n, pa, pb);
+ if (--i <= 0)
+ return;
+ n++;
+ addnfaarc(nf, *pb, *pa, EMPTY);
+ if (n->n_type == STAR)
+ *pb = *pa;
+ else
+ REQ(n, PLUS);
+ }
}
static void
compile_atom(labellist *ll, nfa *nf, node *n, int *pa, int *pb)
{
- int i;
-
- REQ(n, ATOM);
- i = n->n_nchildren;
- REQN(i, 1);
- n = n->n_child;
- if (n->n_type == LPAR) {
- REQN(i, 3);
- n++;
- REQ(n, RHS);
- compile_rhs(ll, nf, n, pa, pb);
- n++;
- REQ(n, RPAR);
- }
- else if (n->n_type == NAME || n->n_type == STRING) {
- *pa = addnfastate(nf);
- *pb = addnfastate(nf);
- addnfaarc(nf, *pa, *pb, addlabel(ll, n->n_type, n->n_str));
- }
- else
- REQ(n, NAME);
+ int i;
+
+ REQ(n, ATOM);
+ i = n->n_nchildren;
+ REQN(i, 1);
+ n = n->n_child;
+ if (n->n_type == LPAR) {
+ REQN(i, 3);
+ n++;
+ REQ(n, RHS);
+ compile_rhs(ll, nf, n, pa, pb);
+ n++;
+ REQ(n, RPAR);
+ }
+ else if (n->n_type == NAME || n->n_type == STRING) {
+ *pa = addnfastate(nf);
+ *pb = addnfastate(nf);
+ addnfaarc(nf, *pa, *pb, addlabel(ll, n->n_type, n->n_str));
+ }
+ else
+ REQ(n, NAME);
}
static void
dumpstate(labellist *ll, nfa *nf, int istate)
{
- nfastate *st;
- int i;
- nfaarc *ar;
-
- printf("%c%2d%c",
- istate == nf->nf_start ? '*' : ' ',
- istate,
- istate == nf->nf_finish ? '.' : ' ');
- st = &nf->nf_state[istate];
- ar = st->st_arc;
- for (i = 0; i < st->st_narcs; i++) {
- if (i > 0)
- printf("\n ");
- printf("-> %2d %s", ar->ar_arrow,
- PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label]));
- ar++;
- }
- printf("\n");
+ nfastate *st;
+ int i;
+ nfaarc *ar;
+
+ printf("%c%2d%c",
+ istate == nf->nf_start ? '*' : ' ',
+ istate,
+ istate == nf->nf_finish ? '.' : ' ');
+ st = &nf->nf_state[istate];
+ ar = st->st_arc;
+ for (i = 0; i < st->st_narcs; i++) {
+ if (i > 0)
+ printf("\n ");
+ printf("-> %2d %s", ar->ar_arrow,
+ PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label]));
+ ar++;
+ }
+ printf("\n");
}
static void
dumpnfa(labellist *ll, nfa *nf)
{
- int i;
-
- printf("NFA '%s' has %d states; start %d, finish %d\n",
- nf->nf_name, nf->nf_nstates, nf->nf_start, nf->nf_finish);
- for (i = 0; i < nf->nf_nstates; i++)
- dumpstate(ll, nf, i);
+ int i;
+
+ printf("NFA '%s' has %d states; start %d, finish %d\n",
+ nf->nf_name, nf->nf_nstates, nf->nf_start, nf->nf_finish);
+ for (i = 0; i < nf->nf_nstates; i++)
+ dumpstate(ll, nf, i);
}
@@ -342,184 +342,184 @@ dumpnfa(labellist *ll, nfa *nf)
static void
addclosure(bitset ss, nfa *nf, int istate)
{
- if (addbit(ss, istate)) {
- nfastate *st = &nf->nf_state[istate];
- nfaarc *ar = st->st_arc;
- int i;
-
- for (i = st->st_narcs; --i >= 0; ) {
- if (ar->ar_label == EMPTY)
- addclosure(ss, nf, ar->ar_arrow);
- ar++;
- }
- }
+ if (addbit(ss, istate)) {
+ nfastate *st = &nf->nf_state[istate];
+ nfaarc *ar = st->st_arc;
+ int i;
+
+ for (i = st->st_narcs; --i >= 0; ) {
+ if (ar->ar_label == EMPTY)
+ addclosure(ss, nf, ar->ar_arrow);
+ ar++;
+ }
+ }
}
typedef struct _ss_arc {
- bitset sa_bitset;
- int sa_arrow;
- int sa_label;
+ bitset sa_bitset;
+ int sa_arrow;
+ int sa_label;
} ss_arc;
typedef struct _ss_state {
- bitset ss_ss;
- int ss_narcs;
- struct _ss_arc *ss_arc;
- int ss_deleted;
- int ss_finish;
- int ss_rename;
+ bitset ss_ss;
+ int ss_narcs;
+ struct _ss_arc *ss_arc;
+ int ss_deleted;
+ int ss_finish;
+ int ss_rename;
} ss_state;
typedef struct _ss_dfa {
- int sd_nstates;
- ss_state *sd_state;
+ int sd_nstates;
+ ss_state *sd_state;
} ss_dfa;
/* Forward */
static void printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
- labellist *ll, char *msg);
+ labellist *ll, char *msg);
static void simplify(int xx_nstates, ss_state *xx_state);
static void convert(dfa *d, int xx_nstates, ss_state *xx_state);
static void
makedfa(nfagrammar *gr, nfa *nf, dfa *d)
{
- int nbits = nf->nf_nstates;
- bitset ss;
- int xx_nstates;
- ss_state *xx_state, *yy;
- ss_arc *zz;
- int istate, jstate, iarc, jarc, ibit;
- nfastate *st;
- nfaarc *ar;
-
- ss = newbitset(nbits);
- addclosure(ss, nf, nf->nf_start);
- xx_state = (ss_state *)PyObject_MALLOC(sizeof(ss_state));
- if (xx_state == NULL)
- Py_FatalError("no mem for xx_state in makedfa");
- xx_nstates = 1;
- yy = &xx_state[0];
- yy->ss_ss = ss;
- yy->ss_narcs = 0;
- yy->ss_arc = NULL;
- yy->ss_deleted = 0;
- yy->ss_finish = testbit(ss, nf->nf_finish);
- if (yy->ss_finish)
- printf("Error: nonterminal '%s' may produce empty.\n",
- nf->nf_name);
-
- /* This algorithm is from a book written before
- the invention of structured programming... */
-
- /* For each unmarked state... */
- for (istate = 0; istate < xx_nstates; ++istate) {
- size_t size;
- yy = &xx_state[istate];
- ss = yy->ss_ss;
- /* For all its states... */
- for (ibit = 0; ibit < nf->nf_nstates; ++ibit) {
- if (!testbit(ss, ibit))
- continue;
- st = &nf->nf_state[ibit];
- /* For all non-empty arcs from this state... */
- for (iarc = 0; iarc < st->st_narcs; iarc++) {
- ar = &st->st_arc[iarc];
- if (ar->ar_label == EMPTY)
- continue;
- /* Look up in list of arcs from this state */
- for (jarc = 0; jarc < yy->ss_narcs; ++jarc) {
- zz = &yy->ss_arc[jarc];
- if (ar->ar_label == zz->sa_label)
- goto found;
- }
- /* Add new arc for this state */
- size = sizeof(ss_arc) * (yy->ss_narcs + 1);
- yy->ss_arc = (ss_arc *)PyObject_REALLOC(
- yy->ss_arc, size);
- if (yy->ss_arc == NULL)
- Py_FatalError("out of mem");
- zz = &yy->ss_arc[yy->ss_narcs++];
- zz->sa_label = ar->ar_label;
- zz->sa_bitset = newbitset(nbits);
- zz->sa_arrow = -1;
- found: ;
- /* Add destination */
- addclosure(zz->sa_bitset, nf, ar->ar_arrow);
- }
- }
- /* Now look up all the arrow states */
- for (jarc = 0; jarc < xx_state[istate].ss_narcs; jarc++) {
- zz = &xx_state[istate].ss_arc[jarc];
- for (jstate = 0; jstate < xx_nstates; jstate++) {
- if (samebitset(zz->sa_bitset,
- xx_state[jstate].ss_ss, nbits)) {
- zz->sa_arrow = jstate;
- goto done;
- }
- }
- size = sizeof(ss_state) * (xx_nstates + 1);
- xx_state = (ss_state *)PyObject_REALLOC(xx_state,
- size);
- if (xx_state == NULL)
- Py_FatalError("out of mem");
- zz->sa_arrow = xx_nstates;
- yy = &xx_state[xx_nstates++];
- yy->ss_ss = zz->sa_bitset;
- yy->ss_narcs = 0;
- yy->ss_arc = NULL;
- yy->ss_deleted = 0;
- yy->ss_finish = testbit(yy->ss_ss, nf->nf_finish);
- done: ;
- }
- }
-
- if (Py_DebugFlag)
- printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
- "before minimizing");
-
- simplify(xx_nstates, xx_state);
-
- if (Py_DebugFlag)
- printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
- "after minimizing");
-
- convert(d, xx_nstates, xx_state);
-
- /* XXX cleanup */
- PyObject_FREE(xx_state);
+ int nbits = nf->nf_nstates;
+ bitset ss;
+ int xx_nstates;
+ ss_state *xx_state, *yy;
+ ss_arc *zz;
+ int istate, jstate, iarc, jarc, ibit;
+ nfastate *st;
+ nfaarc *ar;
+
+ ss = newbitset(nbits);
+ addclosure(ss, nf, nf->nf_start);
+ xx_state = (ss_state *)PyObject_MALLOC(sizeof(ss_state));
+ if (xx_state == NULL)
+ Py_FatalError("no mem for xx_state in makedfa");
+ xx_nstates = 1;
+ yy = &xx_state[0];
+ yy->ss_ss = ss;
+ yy->ss_narcs = 0;
+ yy->ss_arc = NULL;
+ yy->ss_deleted = 0;
+ yy->ss_finish = testbit(ss, nf->nf_finish);
+ if (yy->ss_finish)
+ printf("Error: nonterminal '%s' may produce empty.\n",
+ nf->nf_name);
+
+ /* This algorithm is from a book written before
+ the invention of structured programming... */
+
+ /* For each unmarked state... */
+ for (istate = 0; istate < xx_nstates; ++istate) {
+ size_t size;
+ yy = &xx_state[istate];
+ ss = yy->ss_ss;
+ /* For all its states... */
+ for (ibit = 0; ibit < nf->nf_nstates; ++ibit) {
+ if (!testbit(ss, ibit))
+ continue;
+ st = &nf->nf_state[ibit];
+ /* For all non-empty arcs from this state... */
+ for (iarc = 0; iarc < st->st_narcs; iarc++) {
+ ar = &st->st_arc[iarc];
+ if (ar->ar_label == EMPTY)
+ continue;
+ /* Look up in list of arcs from this state */
+ for (jarc = 0; jarc < yy->ss_narcs; ++jarc) {
+ zz = &yy->ss_arc[jarc];
+ if (ar->ar_label == zz->sa_label)
+ goto found;
+ }
+ /* Add new arc for this state */
+ size = sizeof(ss_arc) * (yy->ss_narcs + 1);
+ yy->ss_arc = (ss_arc *)PyObject_REALLOC(
+ yy->ss_arc, size);
+ if (yy->ss_arc == NULL)
+ Py_FatalError("out of mem");
+ zz = &yy->ss_arc[yy->ss_narcs++];
+ zz->sa_label = ar->ar_label;
+ zz->sa_bitset = newbitset(nbits);
+ zz->sa_arrow = -1;
+ found: ;
+ /* Add destination */
+ addclosure(zz->sa_bitset, nf, ar->ar_arrow);
+ }
+ }
+ /* Now look up all the arrow states */
+ for (jarc = 0; jarc < xx_state[istate].ss_narcs; jarc++) {
+ zz = &xx_state[istate].ss_arc[jarc];
+ for (jstate = 0; jstate < xx_nstates; jstate++) {
+ if (samebitset(zz->sa_bitset,
+ xx_state[jstate].ss_ss, nbits)) {
+ zz->sa_arrow = jstate;
+ goto done;
+ }
+ }
+ size = sizeof(ss_state) * (xx_nstates + 1);
+ xx_state = (ss_state *)PyObject_REALLOC(xx_state,
+ size);
+ if (xx_state == NULL)
+ Py_FatalError("out of mem");
+ zz->sa_arrow = xx_nstates;
+ yy = &xx_state[xx_nstates++];
+ yy->ss_ss = zz->sa_bitset;
+ yy->ss_narcs = 0;
+ yy->ss_arc = NULL;
+ yy->ss_deleted = 0;
+ yy->ss_finish = testbit(yy->ss_ss, nf->nf_finish);
+ done: ;
+ }
+ }
+
+ if (Py_DebugFlag)
+ printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
+ "before minimizing");
+
+ simplify(xx_nstates, xx_state);
+
+ if (Py_DebugFlag)
+ printssdfa(xx_nstates, xx_state, nbits, &gr->gr_ll,
+ "after minimizing");
+
+ convert(d, xx_nstates, xx_state);
+
+ /* XXX cleanup */
+ PyObject_FREE(xx_state);
}
static void
printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
- labellist *ll, char *msg)
+ labellist *ll, char *msg)
{
- int i, ibit, iarc;
- ss_state *yy;
- ss_arc *zz;
-
- printf("Subset DFA %s\n", msg);
- for (i = 0; i < xx_nstates; i++) {
- yy = &xx_state[i];
- if (yy->ss_deleted)
- continue;
- printf(" Subset %d", i);
- if (yy->ss_finish)
- printf(" (finish)");
- printf(" { ");
- for (ibit = 0; ibit < nbits; ibit++) {
- if (testbit(yy->ss_ss, ibit))
- printf("%d ", ibit);
- }
- printf("}\n");
- for (iarc = 0; iarc < yy->ss_narcs; iarc++) {
- zz = &yy->ss_arc[iarc];
- printf(" Arc to state %d, label %s\n",
- zz->sa_arrow,
- PyGrammar_LabelRepr(
- &ll->ll_label[zz->sa_label]));
- }
- }
+ int i, ibit, iarc;
+ ss_state *yy;
+ ss_arc *zz;
+
+ printf("Subset DFA %s\n", msg);
+ for (i = 0; i < xx_nstates; i++) {
+ yy = &xx_state[i];
+ if (yy->ss_deleted)
+ continue;
+ printf(" Subset %d", i);
+ if (yy->ss_finish)
+ printf(" (finish)");
+ printf(" { ");
+ for (ibit = 0; ibit < nbits; ibit++) {
+ if (testbit(yy->ss_ss, ibit))
+ printf("%d ", ibit);
+ }
+ printf("}\n");
+ for (iarc = 0; iarc < yy->ss_narcs; iarc++) {
+ zz = &yy->ss_arc[iarc];
+ printf(" Arc to state %d, label %s\n",
+ zz->sa_arrow,
+ PyGrammar_LabelRepr(
+ &ll->ll_label[zz->sa_label]));
+ }
+ }
}
@@ -535,59 +535,59 @@ printssdfa(int xx_nstates, ss_state *xx_state, int nbits,
static int
samestate(ss_state *s1, ss_state *s2)
{
- int i;
-
- if (s1->ss_narcs != s2->ss_narcs || s1->ss_finish != s2->ss_finish)
- return 0;
- for (i = 0; i < s1->ss_narcs; i++) {
- if (s1->ss_arc[i].sa_arrow != s2->ss_arc[i].sa_arrow ||
- s1->ss_arc[i].sa_label != s2->ss_arc[i].sa_label)
- return 0;
- }
- return 1;
+ int i;
+
+ if (s1->ss_narcs != s2->ss_narcs || s1->ss_finish != s2->ss_finish)
+ return 0;
+ for (i = 0; i < s1->ss_narcs; i++) {
+ if (s1->ss_arc[i].sa_arrow != s2->ss_arc[i].sa_arrow ||
+ s1->ss_arc[i].sa_label != s2->ss_arc[i].sa_label)
+ return 0;
+ }
+ return 1;
}
static void
renamestates(int xx_nstates, ss_state *xx_state, int from, int to)
{
- int i, j;
-
- if (Py_DebugFlag)
- printf("Rename state %d to %d.\n", from, to);
- for (i = 0; i < xx_nstates; i++) {
- if (xx_state[i].ss_deleted)
- continue;
- for (j = 0; j < xx_state[i].ss_narcs; j++) {
- if (xx_state[i].ss_arc[j].sa_arrow == from)
- xx_state[i].ss_arc[j].sa_arrow = to;
- }
- }
+ int i, j;
+
+ if (Py_DebugFlag)
+ printf("Rename state %d to %d.\n", from, to);
+ for (i = 0; i < xx_nstates; i++) {
+ if (xx_state[i].ss_deleted)
+ continue;
+ for (j = 0; j < xx_state[i].ss_narcs; j++) {
+ if (xx_state[i].ss_arc[j].sa_arrow == from)
+ xx_state[i].ss_arc[j].sa_arrow = to;
+ }
+ }
}
static void
simplify(int xx_nstates, ss_state *xx_state)
{
- int changes;
- int i, j;
-
- do {
- changes = 0;
- for (i = 1; i < xx_nstates; i++) {
- if (xx_state[i].ss_deleted)
- continue;
- for (j = 0; j < i; j++) {
- if (xx_state[j].ss_deleted)
- continue;
- if (samestate(&xx_state[i], &xx_state[j])) {
- xx_state[i].ss_deleted++;
- renamestates(xx_nstates, xx_state,
- i, j);
- changes++;
- break;
- }
- }
- }
- } while (changes);
+ int changes;
+ int i, j;
+
+ do {
+ changes = 0;
+ for (i = 1; i < xx_nstates; i++) {
+ if (xx_state[i].ss_deleted)
+ continue;
+ for (j = 0; j < i; j++) {
+ if (xx_state[j].ss_deleted)
+ continue;
+ if (samestate(&xx_state[i], &xx_state[j])) {
+ xx_state[i].ss_deleted++;
+ renamestates(xx_nstates, xx_state,
+ i, j);
+ changes++;
+ break;
+ }
+ }
+ }
+ } while (changes);
}
@@ -598,32 +598,32 @@ simplify(int xx_nstates, ss_state *xx_state)
static void
convert(dfa *d, int xx_nstates, ss_state *xx_state)
{
- int i, j;
- ss_state *yy;
- ss_arc *zz;
-
- for (i = 0; i < xx_nstates; i++) {
- yy = &xx_state[i];
- if (yy->ss_deleted)
- continue;
- yy->ss_rename = addstate(d);
- }
-
- for (i = 0; i < xx_nstates; i++) {
- yy = &xx_state[i];
- if (yy->ss_deleted)
- continue;
- for (j = 0; j < yy->ss_narcs; j++) {
- zz = &yy->ss_arc[j];
- addarc(d, yy->ss_rename,
- xx_state[zz->sa_arrow].ss_rename,
- zz->sa_label);
- }
- if (yy->ss_finish)
- addarc(d, yy->ss_rename, yy->ss_rename, 0);
- }
-
- d->d_initial = 0;
+ int i, j;
+ ss_state *yy;
+ ss_arc *zz;
+
+ for (i = 0; i < xx_nstates; i++) {
+ yy = &xx_state[i];
+ if (yy->ss_deleted)
+ continue;
+ yy->ss_rename = addstate(d);
+ }
+
+ for (i = 0; i < xx_nstates; i++) {
+ yy = &xx_state[i];
+ if (yy->ss_deleted)
+ continue;
+ for (j = 0; j < yy->ss_narcs; j++) {
+ zz = &yy->ss_arc[j];
+ addarc(d, yy->ss_rename,
+ xx_state[zz->sa_arrow].ss_rename,
+ zz->sa_label);
+ }
+ if (yy->ss_finish)
+ addarc(d, yy->ss_rename, yy->ss_rename, 0);
+ }
+
+ d->d_initial = 0;
}
@@ -632,43 +632,43 @@ convert(dfa *d, int xx_nstates, ss_state *xx_state)
static grammar *
maketables(nfagrammar *gr)
{
- int i;
- nfa *nf;
- dfa *d;
- grammar *g;
-
- if (gr->gr_nnfas == 0)
- return NULL;
- g = newgrammar(gr->gr_nfa[0]->nf_type);
- /* XXX first rule must be start rule */
- g->g_ll = gr->gr_ll;
-
- for (i = 0; i < gr->gr_nnfas; i++) {
- nf = gr->gr_nfa[i];
- if (Py_DebugFlag) {
- printf("Dump of NFA for '%s' ...\n", nf->nf_name);
- dumpnfa(&gr->gr_ll, nf);
- printf("Making DFA for '%s' ...\n", nf->nf_name);
- }
- d = adddfa(g, nf->nf_type, nf->nf_name);
- makedfa(gr, gr->gr_nfa[i], d);
- }
-
- return g;
+ int i;
+ nfa *nf;
+ dfa *d;
+ grammar *g;
+
+ if (gr->gr_nnfas == 0)
+ return NULL;
+ g = newgrammar(gr->gr_nfa[0]->nf_type);
+ /* XXX first rule must be start rule */
+ g->g_ll = gr->gr_ll;
+
+ for (i = 0; i < gr->gr_nnfas; i++) {
+ nf = gr->gr_nfa[i];
+ if (Py_DebugFlag) {
+ printf("Dump of NFA for '%s' ...\n", nf->nf_name);
+ dumpnfa(&gr->gr_ll, nf);
+ printf("Making DFA for '%s' ...\n", nf->nf_name);
+ }
+ d = adddfa(g, nf->nf_type, nf->nf_name);
+ makedfa(gr, gr->gr_nfa[i], d);
+ }
+
+ return g;
}
grammar *
pgen(node *n)
{
- nfagrammar *gr;
- grammar *g;
-
- gr = metacompile(n);
- g = maketables(gr);
- translatelabels(g);
- addfirstsets(g);
- PyObject_FREE(gr);
- return g;
+ nfagrammar *gr;
+ grammar *g;
+
+ gr = metacompile(n);
+ g = maketables(gr);
+ translatelabels(g);
+ addfirstsets(g);
+ PyObject_FREE(gr);
+ return g;
}
grammar *
@@ -702,7 +702,7 @@ Reference
---------
[Aho&Ullman 77]
- Aho&Ullman, Principles of Compiler Design, Addison-Wesley 1977
- (first edition)
+ Aho&Ullman, Principles of Compiler Design, Addison-Wesley 1977
+ (first edition)
*/
diff --git a/Parser/pgenmain.c b/Parser/pgenmain.c
index fc27a2c..88fa7f1 100644
--- a/Parser/pgenmain.c
+++ b/Parser/pgenmain.c
@@ -30,104 +30,104 @@ grammar *getgrammar(char *filename);
void
Py_Exit(int sts)
{
- exit(sts);
+ exit(sts);
}
int
main(int argc, char **argv)
{
- grammar *g;
- FILE *fp;
- char *filename, *graminit_h, *graminit_c;
-
- if (argc != 4) {
- fprintf(stderr,
- "usage: %s grammar graminit.h graminit.c\n", argv[0]);
- Py_Exit(2);
- }
- filename = argv[1];
- graminit_h = argv[2];
- graminit_c = argv[3];
- g = getgrammar(filename);
- fp = fopen(graminit_c, "w");
- if (fp == NULL) {
- perror(graminit_c);
- Py_Exit(1);
- }
- if (Py_DebugFlag)
- printf("Writing %s ...\n", graminit_c);
- printgrammar(g, fp);
- fclose(fp);
- fp = fopen(graminit_h, "w");
- if (fp == NULL) {
- perror(graminit_h);
- Py_Exit(1);
- }
- if (Py_DebugFlag)
- printf("Writing %s ...\n", graminit_h);
- printnonterminals(g, fp);
- fclose(fp);
- Py_Exit(0);
- return 0; /* Make gcc -Wall happy */
+ grammar *g;
+ FILE *fp;
+ char *filename, *graminit_h, *graminit_c;
+
+ if (argc != 4) {
+ fprintf(stderr,
+ "usage: %s grammar graminit.h graminit.c\n", argv[0]);
+ Py_Exit(2);
+ }
+ filename = argv[1];
+ graminit_h = argv[2];
+ graminit_c = argv[3];
+ g = getgrammar(filename);
+ fp = fopen(graminit_c, "w");
+ if (fp == NULL) {
+ perror(graminit_c);
+ Py_Exit(1);
+ }
+ if (Py_DebugFlag)
+ printf("Writing %s ...\n", graminit_c);
+ printgrammar(g, fp);
+ fclose(fp);
+ fp = fopen(graminit_h, "w");
+ if (fp == NULL) {
+ perror(graminit_h);
+ Py_Exit(1);
+ }
+ if (Py_DebugFlag)
+ printf("Writing %s ...\n", graminit_h);
+ printnonterminals(g, fp);
+ fclose(fp);
+ Py_Exit(0);
+ return 0; /* Make gcc -Wall happy */
}
grammar *
getgrammar(char *filename)
{
- FILE *fp;
- node *n;
- grammar *g0, *g;
- perrdetail err;
-
- fp = fopen(filename, "r");
- if (fp == NULL) {
- perror(filename);
- Py_Exit(1);
- }
- g0 = meta_grammar();
- n = PyParser_ParseFile(fp, filename, g0, g0->g_start,
- (char *)NULL, (char *)NULL, &err);
- fclose(fp);
- if (n == NULL) {
- fprintf(stderr, "Parsing error %d, line %d.\n",
- err.error, err.lineno);
- if (err.text != NULL) {
- size_t i;
- fprintf(stderr, "%s", err.text);
- i = strlen(err.text);
- if (i == 0 || err.text[i-1] != '\n')
- fprintf(stderr, "\n");
- for (i = 0; i < err.offset; i++) {
- if (err.text[i] == '\t')
- putc('\t', stderr);
- else
- putc(' ', stderr);
- }
- fprintf(stderr, "^\n");
- PyObject_FREE(err.text);
- }
- Py_Exit(1);
- }
- g = pgen(n);
- if (g == NULL) {
- printf("Bad grammar.\n");
- Py_Exit(1);
- }
- return g;
+ FILE *fp;
+ node *n;
+ grammar *g0, *g;
+ perrdetail err;
+
+ fp = fopen(filename, "r");
+ if (fp == NULL) {
+ perror(filename);
+ Py_Exit(1);
+ }
+ g0 = meta_grammar();
+ n = PyParser_ParseFile(fp, filename, g0, g0->g_start,
+ (char *)NULL, (char *)NULL, &err);
+ fclose(fp);
+ if (n == NULL) {
+ fprintf(stderr, "Parsing error %d, line %d.\n",
+ err.error, err.lineno);
+ if (err.text != NULL) {
+ size_t i;
+ fprintf(stderr, "%s", err.text);
+ i = strlen(err.text);
+ if (i == 0 || err.text[i-1] != '\n')
+ fprintf(stderr, "\n");
+ for (i = 0; i < err.offset; i++) {
+ if (err.text[i] == '\t')
+ putc('\t', stderr);
+ else
+ putc(' ', stderr);
+ }
+ fprintf(stderr, "^\n");
+ PyObject_FREE(err.text);
+ }
+ Py_Exit(1);
+ }
+ g = pgen(n);
+ if (g == NULL) {
+ printf("Bad grammar.\n");
+ Py_Exit(1);
+ }
+ return g;
}
/* Can't happen in pgen */
PyObject*
PyErr_Occurred()
{
- return 0;
+ return 0;
}
void
Py_FatalError(const char *msg)
{
- fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
- Py_Exit(1);
+ fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
+ Py_Exit(1);
}
/* No-nonsense my_readline() for tokenizer.c */
@@ -135,28 +135,28 @@ Py_FatalError(const char *msg)
char *
PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
{
- size_t n = 1000;
- char *p = (char *)PyMem_MALLOC(n);
- char *q;
- if (p == NULL)
- return NULL;
- fprintf(stderr, "%s", prompt);
- q = fgets(p, n, sys_stdin);
- if (q == NULL) {
- *p = '\0';
- return p;
- }
- n = strlen(p);
- if (n > 0 && p[n-1] != '\n')
- p[n-1] = '\n';
- return (char *)PyMem_REALLOC(p, n+1);
+ size_t n = 1000;
+ char *p = (char *)PyMem_MALLOC(n);
+ char *q;
+ if (p == NULL)
+ return NULL;
+ fprintf(stderr, "%s", prompt);
+ q = fgets(p, n, sys_stdin);
+ if (q == NULL) {
+ *p = '\0';
+ return p;
+ }
+ n = strlen(p);
+ if (n > 0 && p[n-1] != '\n')
+ p[n-1] = '\n';
+ return (char *)PyMem_REALLOC(p, n+1);
}
/* No-nonsense fgets */
char *
Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
{
- return fgets(buf, n, stream);
+ return fgets(buf, n, stream);
}
@@ -165,9 +165,9 @@ Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
void
PySys_WriteStderr(const char *format, ...)
{
- va_list va;
+ va_list va;
- va_start(va, format);
- vfprintf(stderr, format, va);
- va_end(va);
+ va_start(va, format);
+ vfprintf(stderr, format, va);
+ va_end(va);
}
diff --git a/Parser/printgrammar.c b/Parser/printgrammar.c
index ae180e1..3b54a05 100644
--- a/Parser/printgrammar.c
+++ b/Parser/printgrammar.c
@@ -13,105 +13,105 @@ static void printlabels(grammar *, FILE *);
void
printgrammar(grammar *g, FILE *fp)
{
- fprintf(fp, "/* Generated by Parser/pgen */\n\n");
- fprintf(fp, "#include \"pgenheaders.h\"\n");
- fprintf(fp, "#include \"grammar.h\"\n");
- fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
- printdfas(g, fp);
- printlabels(g, fp);
- fprintf(fp, "grammar _PyParser_Grammar = {\n");
- fprintf(fp, "\t%d,\n", g->g_ndfas);
- fprintf(fp, "\tdfas,\n");
- fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
- fprintf(fp, "\t%d\n", g->g_start);
- fprintf(fp, "};\n");
+ fprintf(fp, "/* Generated by Parser/pgen */\n\n");
+ fprintf(fp, "#include \"pgenheaders.h\"\n");
+ fprintf(fp, "#include \"grammar.h\"\n");
+ fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
+ printdfas(g, fp);
+ printlabels(g, fp);
+ fprintf(fp, "grammar _PyParser_Grammar = {\n");
+ fprintf(fp, "\t%d,\n", g->g_ndfas);
+ fprintf(fp, "\tdfas,\n");
+ fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
+ fprintf(fp, "\t%d\n", g->g_start);
+ fprintf(fp, "};\n");
}
void
printnonterminals(grammar *g, FILE *fp)
{
- dfa *d;
- int i;
-
- fprintf(fp, "/* Generated by Parser/pgen */\n\n");
-
- d = g->g_dfa;
- for (i = g->g_ndfas; --i >= 0; d++)
- fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
+ dfa *d;
+ int i;
+
+ fprintf(fp, "/* Generated by Parser/pgen */\n\n");
+
+ d = g->g_dfa;
+ for (i = g->g_ndfas; --i >= 0; d++)
+ fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
}
static void
printarcs(int i, dfa *d, FILE *fp)
{
- arc *a;
- state *s;
- int j, k;
-
- s = d->d_state;
- for (j = 0; j < d->d_nstates; j++, s++) {
- fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
- i, j, s->s_narcs);
- a = s->s_arc;
- for (k = 0; k < s->s_narcs; k++, a++)
- fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
- fprintf(fp, "};\n");
- }
+ arc *a;
+ state *s;
+ int j, k;
+
+ s = d->d_state;
+ for (j = 0; j < d->d_nstates; j++, s++) {
+ fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
+ i, j, s->s_narcs);
+ a = s->s_arc;
+ for (k = 0; k < s->s_narcs; k++, a++)
+ fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
+ fprintf(fp, "};\n");
+ }
}
static void
printstates(grammar *g, FILE *fp)
{
- state *s;
- dfa *d;
- int i, j;
-
- d = g->g_dfa;
- for (i = 0; i < g->g_ndfas; i++, d++) {
- printarcs(i, d, fp);
- fprintf(fp, "static state states_%d[%d] = {\n",
- i, d->d_nstates);
- s = d->d_state;
- for (j = 0; j < d->d_nstates; j++, s++)
- fprintf(fp, "\t{%d, arcs_%d_%d},\n",
- s->s_narcs, i, j);
- fprintf(fp, "};\n");
- }
+ state *s;
+ dfa *d;
+ int i, j;
+
+ d = g->g_dfa;
+ for (i = 0; i < g->g_ndfas; i++, d++) {
+ printarcs(i, d, fp);
+ fprintf(fp, "static state states_%d[%d] = {\n",
+ i, d->d_nstates);
+ s = d->d_state;
+ for (j = 0; j < d->d_nstates; j++, s++)
+ fprintf(fp, "\t{%d, arcs_%d_%d},\n",
+ s->s_narcs, i, j);
+ fprintf(fp, "};\n");
+ }
}
static void
printdfas(grammar *g, FILE *fp)
{
- dfa *d;
- int i, j;
-
- printstates(g, fp);
- fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
- d = g->g_dfa;
- for (i = 0; i < g->g_ndfas; i++, d++) {
- fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
- d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
- fprintf(fp, "\t \"");
- for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
- fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
- fprintf(fp, "\"},\n");
- }
- fprintf(fp, "};\n");
+ dfa *d;
+ int i, j;
+
+ printstates(g, fp);
+ fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
+ d = g->g_dfa;
+ for (i = 0; i < g->g_ndfas; i++, d++) {
+ fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
+ d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
+ fprintf(fp, "\t \"");
+ for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
+ fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
+ fprintf(fp, "\"},\n");
+ }
+ fprintf(fp, "};\n");
}
static void
printlabels(grammar *g, FILE *fp)
{
- label *l;
- int i;
-
- fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
- l = g->g_ll.ll_label;
- for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
- if (l->lb_str == NULL)
- fprintf(fp, "\t{%d, 0},\n", l->lb_type);
- else
- fprintf(fp, "\t{%d, \"%s\"},\n",
- l->lb_type, l->lb_str);
- }
- fprintf(fp, "};\n");
+ label *l;
+ int i;
+
+ fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
+ l = g->g_ll.ll_label;
+ for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
+ if (l->lb_str == NULL)
+ fprintf(fp, "\t{%d, 0},\n", l->lb_type);
+ else
+ fprintf(fp, "\t{%d, \"%s\"},\n",
+ l->lb_type, l->lb_str);
+ }
+ fprintf(fp, "};\n");
}
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index af99def..d985131 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -19,17 +19,17 @@
#endif /* PGEN */
#define is_potential_identifier_start(c) (\
- (c >= 'a' && c <= 'z')\
- || (c >= 'A' && c <= 'Z')\
- || c == '_'\
- || (c >= 128))
+ (c >= 'a' && c <= 'z')\
+ || (c >= 'A' && c <= 'Z')\
+ || c == '_'\
+ || (c >= 128))
#define is_potential_identifier_char(c) (\
- (c >= 'a' && c <= 'z')\
- || (c >= 'A' && c <= 'Z')\
- || (c >= '0' && c <= '9')\
- || c == '_'\
- || (c >= 128))
+ (c >= 'a' && c <= 'z')\
+ || (c >= 'A' && c <= 'Z')\
+ || (c >= '0' && c <= '9')\
+ || c == '_'\
+ || (c >= 128))
extern char *PyOS_Readline(FILE *, FILE *, char *);
/* Return malloc'ed string including trailing \n;
@@ -48,62 +48,62 @@ static void tok_backup(struct tok_state *tok, int c);
/* Token names */
char *_PyParser_TokenNames[] = {
- "ENDMARKER",
- "NAME",
- "NUMBER",
- "STRING",
- "NEWLINE",
- "INDENT",
- "DEDENT",
- "LPAR",
- "RPAR",
- "LSQB",
- "RSQB",
- "COLON",
- "COMMA",
- "SEMI",
- "PLUS",
- "MINUS",
- "STAR",
- "SLASH",
- "VBAR",
- "AMPER",
- "LESS",
- "GREATER",
- "EQUAL",
- "DOT",
- "PERCENT",
- "LBRACE",
- "RBRACE",
- "EQEQUAL",
- "NOTEQUAL",
- "LESSEQUAL",
- "GREATEREQUAL",
- "TILDE",
- "CIRCUMFLEX",
- "LEFTSHIFT",
- "RIGHTSHIFT",
- "DOUBLESTAR",
- "PLUSEQUAL",
- "MINEQUAL",
- "STAREQUAL",
- "SLASHEQUAL",
- "PERCENTEQUAL",
- "AMPEREQUAL",
- "VBAREQUAL",
- "CIRCUMFLEXEQUAL",
- "LEFTSHIFTEQUAL",
- "RIGHTSHIFTEQUAL",
- "DOUBLESTAREQUAL",
- "DOUBLESLASH",
- "DOUBLESLASHEQUAL",
- "AT",
- "RARROW",
- "ELLIPSIS",
- /* This table must match the #defines in token.h! */
- "OP",
- "<ERRORTOKEN>",
- "<N_TOKENS>"
+ "ENDMARKER",
+ "NAME",
+ "NUMBER",
+ "STRING",
+ "NEWLINE",
+ "INDENT",
+ "DEDENT",
+ "LPAR",
+ "RPAR",
+ "LSQB",
+ "RSQB",
+ "COLON",
+ "COMMA",
+ "SEMI",
+ "PLUS",
+ "MINUS",
+ "STAR",
+ "SLASH",
+ "VBAR",
+ "AMPER",
+ "LESS",
+ "GREATER",
+ "EQUAL",
+ "DOT",
+ "PERCENT",
+ "LBRACE",
+ "RBRACE",
+ "EQEQUAL",
+ "NOTEQUAL",
+ "LESSEQUAL",
+ "GREATEREQUAL",
+ "TILDE",
+ "CIRCUMFLEX",
+ "LEFTSHIFT",
+ "RIGHTSHIFT",
+ "DOUBLESTAR",
+ "PLUSEQUAL",
+ "MINEQUAL",
+ "STAREQUAL",
+ "SLASHEQUAL",
+ "PERCENTEQUAL",
+ "AMPEREQUAL",
+ "VBAREQUAL",
+ "CIRCUMFLEXEQUAL",
+ "LEFTSHIFTEQUAL",
+ "RIGHTSHIFTEQUAL",
+ "DOUBLESTAREQUAL",
+ "DOUBLESLASH",
+ "DOUBLESLASHEQUAL",
+ "AT",
+ "RARROW",
+ "ELLIPSIS",
+ /* This table must match the #defines in token.h! */
+ "OP",
+ "<ERRORTOKEN>",
+ "<N_TOKENS>"
};
@@ -112,37 +112,37 @@ char *_PyParser_TokenNames[] = {
static struct tok_state *
tok_new(void)
{
- struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
- sizeof(struct tok_state));
- if (tok == NULL)
- return NULL;
- tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
- tok->done = E_OK;
- tok->fp = NULL;
- tok->tabsize = TABSIZE;
- tok->indent = 0;
- tok->indstack[0] = 0;
- tok->atbol = 1;
- tok->pendin = 0;
- tok->prompt = tok->nextprompt = NULL;
- tok->lineno = 0;
- tok->level = 0;
- tok->filename = NULL;
- tok->altwarning = 1;
- tok->alterror = 1;
- tok->alttabsize = 1;
- tok->altindstack[0] = 0;
- tok->decoding_state = STATE_INIT;
- tok->decoding_erred = 0;
- tok->read_coding_spec = 0;
- tok->enc = NULL;
- tok->encoding = NULL;
- tok->cont_line = 0;
+ struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
+ sizeof(struct tok_state));
+ if (tok == NULL)
+ return NULL;
+ tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
+ tok->done = E_OK;
+ tok->fp = NULL;
+ tok->tabsize = TABSIZE;
+ tok->indent = 0;
+ tok->indstack[0] = 0;
+ tok->atbol = 1;
+ tok->pendin = 0;
+ tok->prompt = tok->nextprompt = NULL;
+ tok->lineno = 0;
+ tok->level = 0;
+ tok->filename = NULL;
+ tok->altwarning = 1;
+ tok->alterror = 1;
+ tok->alttabsize = 1;
+ tok->altindstack[0] = 0;
+ tok->decoding_state = STATE_INIT;
+ tok->decoding_erred = 0;
+ tok->read_coding_spec = 0;
+ tok->enc = NULL;
+ tok->encoding = NULL;
+ tok->cont_line = 0;
#ifndef PGEN
- tok->decoding_readline = NULL;
- tok->decoding_buffer = NULL;
+ tok->decoding_readline = NULL;
+ tok->decoding_buffer = NULL;
#endif
- return tok;
+ return tok;
}
#ifdef PGEN
@@ -150,19 +150,19 @@ tok_new(void)
static char *
decoding_fgets(char *s, int size, struct tok_state *tok)
{
- return fgets(s, size, tok->fp);
+ return fgets(s, size, tok->fp);
}
static int
decoding_feof(struct tok_state *tok)
{
- return feof(tok->fp);
+ return feof(tok->fp);
}
static const char *
decode_str(const char *str, struct tok_state *tok)
{
- return str;
+ return str;
}
#else /* PGEN */
@@ -170,51 +170,51 @@ decode_str(const char *str, struct tok_state *tok)
static char *
error_ret(struct tok_state *tok) /* XXX */
{
- tok->decoding_erred = 1;
- if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
- PyMem_FREE(tok->buf);
- tok->buf = NULL;
- return NULL; /* as if it were EOF */
+ tok->decoding_erred = 1;
+ if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
+ PyMem_FREE(tok->buf);
+ tok->buf = NULL;
+ return NULL; /* as if it were EOF */
}
static char *
new_string(const char *s, Py_ssize_t len)
{
- char* result = (char *)PyMem_MALLOC(len + 1);
- if (result != NULL) {
- memcpy(result, s, len);
- result[len] = '\0';
- }
- return result;
+ char* result = (char *)PyMem_MALLOC(len + 1);
+ if (result != NULL) {
+ memcpy(result, s, len);
+ result[len] = '\0';
+ }
+ return result;
}
static char *
-get_normal_name(char *s) /* for utf-8 and latin-1 */
+get_normal_name(char *s) /* for utf-8 and latin-1 */
{
- char buf[13];
- int i;
- for (i = 0; i < 12; i++) {
- int c = s[i];
- if (c == '\0')
- break;
- else if (c == '_')
- buf[i] = '-';
- else
- buf[i] = tolower(c);
- }
- buf[i] = '\0';
- if (strcmp(buf, "utf-8") == 0 ||
- strncmp(buf, "utf-8-", 6) == 0)
- return "utf-8";
- else if (strcmp(buf, "latin-1") == 0 ||
- strcmp(buf, "iso-8859-1") == 0 ||
- strcmp(buf, "iso-latin-1") == 0 ||
- strncmp(buf, "latin-1-", 8) == 0 ||
- strncmp(buf, "iso-8859-1-", 11) == 0 ||
- strncmp(buf, "iso-latin-1-", 12) == 0)
- return "iso-8859-1";
- else
- return s;
+ char buf[13];
+ int i;
+ for (i = 0; i < 12; i++) {
+ int c = s[i];
+ if (c == '\0')
+ break;
+ else if (c == '_')
+ buf[i] = '-';
+ else
+ buf[i] = tolower(c);
+ }
+ buf[i] = '\0';
+ if (strcmp(buf, "utf-8") == 0 ||
+ strncmp(buf, "utf-8-", 6) == 0)
+ return "utf-8";
+ else if (strcmp(buf, "latin-1") == 0 ||
+ strcmp(buf, "iso-8859-1") == 0 ||
+ strcmp(buf, "iso-latin-1") == 0 ||
+ strncmp(buf, "latin-1-", 8) == 0 ||
+ strncmp(buf, "iso-8859-1-", 11) == 0 ||
+ strncmp(buf, "iso-latin-1-", 12) == 0)
+ return "iso-8859-1";
+ else
+ return s;
}
/* Return the coding spec in S, or NULL if none is found. */
@@ -222,43 +222,43 @@ get_normal_name(char *s) /* for utf-8 and latin-1 */
static char *
get_coding_spec(const char *s, Py_ssize_t size)
{
- Py_ssize_t i;
- /* Coding spec must be in a comment, and that comment must be
- * the only statement on the source code line. */
- for (i = 0; i < size - 6; i++) {
- if (s[i] == '#')
- break;
- if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
- return NULL;
- }
- for (; i < size - 6; i++) { /* XXX inefficient search */
- const char* t = s + i;
- if (strncmp(t, "coding", 6) == 0) {
- const char* begin = NULL;
- t += 6;
- if (t[0] != ':' && t[0] != '=')
- continue;
- do {
- t++;
- } while (t[0] == '\x20' || t[0] == '\t');
-
- begin = t;
- while (isalnum(Py_CHARMASK(t[0])) ||
- t[0] == '-' || t[0] == '_' || t[0] == '.')
- t++;
-
- if (begin < t) {
- char* r = new_string(begin, t - begin);
- char* q = get_normal_name(r);
- if (r != q) {
- PyMem_FREE(r);
- r = new_string(q, strlen(q));
- }
- return r;
- }
- }
- }
- return NULL;
+ Py_ssize_t i;
+ /* Coding spec must be in a comment, and that comment must be
+ * the only statement on the source code line. */
+ for (i = 0; i < size - 6; i++) {
+ if (s[i] == '#')
+ break;
+ if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
+ return NULL;
+ }
+ for (; i < size - 6; i++) { /* XXX inefficient search */
+ const char* t = s + i;
+ if (strncmp(t, "coding", 6) == 0) {
+ const char* begin = NULL;
+ t += 6;
+ if (t[0] != ':' && t[0] != '=')
+ continue;
+ do {
+ t++;
+ } while (t[0] == '\x20' || t[0] == '\t');
+
+ begin = t;
+ while (isalnum(Py_CHARMASK(t[0])) ||
+ t[0] == '-' || t[0] == '_' || t[0] == '.')
+ t++;
+
+ if (begin < t) {
+ char* r = new_string(begin, t - begin);
+ char* q = get_normal_name(r);
+ if (r != q) {
+ PyMem_FREE(r);
+ r = new_string(q, strlen(q));
+ }
+ return r;
+ }
+ }
+ }
+ return NULL;
}
/* Check whether the line contains a coding spec. If it does,
@@ -268,42 +268,42 @@ get_coding_spec(const char *s, Py_ssize_t size)
static int
check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
- int set_readline(struct tok_state *, const char *))
+ int set_readline(struct tok_state *, const char *))
{
- char * cs;
- int r = 1;
-
- if (tok->cont_line)
- /* It's a continuation line, so it can't be a coding spec. */
- return 1;
- cs = get_coding_spec(line, size);
- if (cs != NULL) {
- tok->read_coding_spec = 1;
- if (tok->encoding == NULL) {
- assert(tok->decoding_state == STATE_RAW);
- if (strcmp(cs, "utf-8") == 0) {
- tok->encoding = cs;
- } else {
- r = set_readline(tok, cs);
- if (r) {
- tok->encoding = cs;
- tok->decoding_state = STATE_NORMAL;
- }
- else
- PyMem_FREE(cs);
- }
- } else { /* then, compare cs with BOM */
- r = (strcmp(tok->encoding, cs) == 0);
- PyMem_FREE(cs);
- }
- }
- if (!r) {
- cs = tok->encoding;
- if (!cs)
- cs = "with BOM";
- PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
- }
- return r;
+ char * cs;
+ int r = 1;
+
+ if (tok->cont_line)
+ /* It's a continuation line, so it can't be a coding spec. */
+ return 1;
+ cs = get_coding_spec(line, size);
+ if (cs != NULL) {
+ tok->read_coding_spec = 1;
+ if (tok->encoding == NULL) {
+ assert(tok->decoding_state == STATE_RAW);
+ if (strcmp(cs, "utf-8") == 0) {
+ tok->encoding = cs;
+ } else {
+ r = set_readline(tok, cs);
+ if (r) {
+ tok->encoding = cs;
+ tok->decoding_state = STATE_NORMAL;
+ }
+ else
+ PyMem_FREE(cs);
+ }
+ } else { /* then, compare cs with BOM */
+ r = (strcmp(tok->encoding, cs) == 0);
+ PyMem_FREE(cs);
+ }
+ }
+ if (!r) {
+ cs = tok->encoding;
+ if (!cs)
+ cs = "with BOM";
+ PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
+ }
+ return r;
}
/* See whether the file starts with a BOM. If it does,
@@ -312,62 +312,62 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
static int
check_bom(int get_char(struct tok_state *),
- void unget_char(int, struct tok_state *),
- int set_readline(struct tok_state *, const char *),
- struct tok_state *tok)
+ void unget_char(int, struct tok_state *),
+ int set_readline(struct tok_state *, const char *),
+ struct tok_state *tok)
{
- int ch1, ch2, ch3;
- ch1 = get_char(tok);
- tok->decoding_state = STATE_RAW;
- if (ch1 == EOF) {
- return 1;
- } else if (ch1 == 0xEF) {
- ch2 = get_char(tok);
- if (ch2 != 0xBB) {
- unget_char(ch2, tok);
- unget_char(ch1, tok);
- return 1;
- }
- ch3 = get_char(tok);
- if (ch3 != 0xBF) {
- unget_char(ch3, tok);
- unget_char(ch2, tok);
- unget_char(ch1, tok);
- return 1;
- }
+ int ch1, ch2, ch3;
+ ch1 = get_char(tok);
+ tok->decoding_state = STATE_RAW;
+ if (ch1 == EOF) {
+ return 1;
+ } else if (ch1 == 0xEF) {
+ ch2 = get_char(tok);
+ if (ch2 != 0xBB) {
+ unget_char(ch2, tok);
+ unget_char(ch1, tok);
+ return 1;
+ }
+ ch3 = get_char(tok);
+ if (ch3 != 0xBF) {
+ unget_char(ch3, tok);
+ unget_char(ch2, tok);
+ unget_char(ch1, tok);
+ return 1;
+ }
#if 0
- /* Disable support for UTF-16 BOMs until a decision
- is made whether this needs to be supported. */
- } else if (ch1 == 0xFE) {
- ch2 = get_char(tok);
- if (ch2 != 0xFF) {
- unget_char(ch2, tok);
- unget_char(ch1, tok);
- return 1;
- }
- if (!set_readline(tok, "utf-16-be"))
- return 0;
- tok->decoding_state = STATE_NORMAL;
- } else if (ch1 == 0xFF) {
- ch2 = get_char(tok);
- if (ch2 != 0xFE) {
- unget_char(ch2, tok);
- unget_char(ch1, tok);
- return 1;
- }
- if (!set_readline(tok, "utf-16-le"))
- return 0;
- tok->decoding_state = STATE_NORMAL;
+ /* Disable support for UTF-16 BOMs until a decision
+ is made whether this needs to be supported. */
+ } else if (ch1 == 0xFE) {
+ ch2 = get_char(tok);
+ if (ch2 != 0xFF) {
+ unget_char(ch2, tok);
+ unget_char(ch1, tok);
+ return 1;
+ }
+ if (!set_readline(tok, "utf-16-be"))
+ return 0;
+ tok->decoding_state = STATE_NORMAL;
+ } else if (ch1 == 0xFF) {
+ ch2 = get_char(tok);
+ if (ch2 != 0xFE) {
+ unget_char(ch2, tok);
+ unget_char(ch1, tok);
+ return 1;
+ }
+ if (!set_readline(tok, "utf-16-le"))
+ return 0;
+ tok->decoding_state = STATE_NORMAL;
#endif
- } else {
- unget_char(ch1, tok);
- return 1;
- }
- if (tok->encoding != NULL)
- PyMem_FREE(tok->encoding);
- tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
- /* No need to set_readline: input is already utf-8 */
- return 1;
+ } else {
+ unget_char(ch1, tok);
+ return 1;
+ }
+ if (tok->encoding != NULL)
+ PyMem_FREE(tok->encoding);
+ tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
+ /* No need to set_readline: input is already utf-8 */
+ return 1;
}
/* Read a line of text from TOK into S, using the stream in TOK.
@@ -376,74 +376,74 @@ check_bom(int get_char(struct tok_state *),
On entry, tok->decoding_buffer will be one of:
1) NULL: need to call tok->decoding_readline to get a new line
2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
- stored the result in tok->decoding_buffer
+ stored the result in tok->decoding_buffer
3) PyByteArrayObject *: previous call to fp_readl did not have enough room
- (in the s buffer) to copy entire contents of the line read
- by tok->decoding_readline. tok->decoding_buffer has the overflow.
- In this case, fp_readl is called in a loop (with an expanded buffer)
- until the buffer ends with a '\n' (or until the end of the file is
- reached): see tok_nextc and its calls to decoding_fgets.
+ (in the s buffer) to copy entire contents of the line read
+ by tok->decoding_readline. tok->decoding_buffer has the overflow.
+ In this case, fp_readl is called in a loop (with an expanded buffer)
+ until the buffer ends with a '\n' (or until the end of the file is
+ reached): see tok_nextc and its calls to decoding_fgets.
*/
static char *
fp_readl(char *s, int size, struct tok_state *tok)
{
- PyObject* bufobj;
- const char *buf;
- Py_ssize_t buflen;
-
- /* Ask for one less byte so we can terminate it */
- assert(size > 0);
- size--;
-
- if (tok->decoding_buffer) {
- bufobj = tok->decoding_buffer;
- Py_INCREF(bufobj);
- }
- else
- {
- bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
- if (bufobj == NULL)
- goto error;
- }
- if (PyUnicode_CheckExact(bufobj))
- {
- buf = _PyUnicode_AsStringAndSize(bufobj, &buflen);
- if (buf == NULL) {
- goto error;
- }
- }
- else
- {
- buf = PyByteArray_AsString(bufobj);
- if (buf == NULL) {
- goto error;
- }
- buflen = PyByteArray_GET_SIZE(bufobj);
- }
-
- Py_XDECREF(tok->decoding_buffer);
- if (buflen > size) {
- /* Too many chars, the rest goes into tok->decoding_buffer */
- tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
- buflen-size);
- if (tok->decoding_buffer == NULL)
- goto error;
- buflen = size;
- }
- else
- tok->decoding_buffer = NULL;
-
- memcpy(s, buf, buflen);
- s[buflen] = '\0';
- if (buflen == 0) /* EOF */
- s = NULL;
- Py_DECREF(bufobj);
- return s;
+ PyObject* bufobj;
+ const char *buf;
+ Py_ssize_t buflen;
+
+ /* Ask for one less byte so we can terminate it */
+ assert(size > 0);
+ size--;
+
+ if (tok->decoding_buffer) {
+ bufobj = tok->decoding_buffer;
+ Py_INCREF(bufobj);
+ }
+ else
+ {
+ bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
+ if (bufobj == NULL)
+ goto error;
+ }
+ if (PyUnicode_CheckExact(bufobj))
+ {
+ buf = _PyUnicode_AsStringAndSize(bufobj, &buflen);
+ if (buf == NULL) {
+ goto error;
+ }
+ }
+ else
+ {
+ buf = PyByteArray_AsString(bufobj);
+ if (buf == NULL) {
+ goto error;
+ }
+ buflen = PyByteArray_GET_SIZE(bufobj);
+ }
+
+ Py_XDECREF(tok->decoding_buffer);
+ if (buflen > size) {
+ /* Too many chars, the rest goes into tok->decoding_buffer */
+ tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
+ buflen-size);
+ if (tok->decoding_buffer == NULL)
+ goto error;
+ buflen = size;
+ }
+ else
+ tok->decoding_buffer = NULL;
+
+ memcpy(s, buf, buflen);
+ s[buflen] = '\0';
+ if (buflen == 0) /* EOF */
+ s = NULL;
+ Py_DECREF(bufobj);
+ return s;
error:
- Py_XDECREF(bufobj);
- return error_ret(tok);
+ Py_XDECREF(bufobj);
+ return error_ret(tok);
}
/* Set the readline function for TOK to a StreamReader's
@@ -459,49 +459,49 @@ error:
static int
fp_setreadl(struct tok_state *tok, const char* enc)
{
- PyObject *readline = NULL, *stream = NULL, *io = NULL;
-
- io = PyImport_ImportModuleNoBlock("io");
- if (io == NULL)
- goto cleanup;
-
- if (tok->filename)
- stream = PyObject_CallMethod(io, "open", "ssis",
- tok->filename, "r", -1, enc);
- else
- stream = PyObject_CallMethod(io, "open", "isisOOO",
- fileno(tok->fp), "r", -1, enc, Py_None, Py_None, Py_False);
- if (stream == NULL)
- goto cleanup;
-
- Py_XDECREF(tok->decoding_readline);
- readline = PyObject_GetAttrString(stream, "readline");
- tok->decoding_readline = readline;
-
- /* The file has been reopened; parsing will restart from
- * the beginning of the file, we have to reset the line number.
- * But this function has been called from inside tok_nextc() which
- * will increment lineno before it returns. So we set it -1 so that
- * the next call to tok_nextc() will start with tok->lineno == 0.
- */
- tok->lineno = -1;
+ PyObject *readline = NULL, *stream = NULL, *io = NULL;
+
+ io = PyImport_ImportModuleNoBlock("io");
+ if (io == NULL)
+ goto cleanup;
+
+ if (tok->filename)
+ stream = PyObject_CallMethod(io, "open", "ssis",
+ tok->filename, "r", -1, enc);
+ else
+ stream = PyObject_CallMethod(io, "open", "isisOOO",
+ fileno(tok->fp), "r", -1, enc, Py_None, Py_None, Py_False);
+ if (stream == NULL)
+ goto cleanup;
+
+ Py_XDECREF(tok->decoding_readline);
+ readline = PyObject_GetAttrString(stream, "readline");
+ tok->decoding_readline = readline;
+
+ /* The file has been reopened; parsing will restart from
+ * the beginning of the file, we have to reset the line number.
+ * But this function has been called from inside tok_nextc() which
+ * will increment lineno before it returns. So we set it -1 so that
+ * the next call to tok_nextc() will start with tok->lineno == 0.
+ */
+ tok->lineno = -1;
cleanup:
- Py_XDECREF(stream);
- Py_XDECREF(io);
- return readline != NULL;
+ Py_XDECREF(stream);
+ Py_XDECREF(io);
+ return readline != NULL;
}
/* Fetch the next byte from TOK. */
static int fp_getc(struct tok_state *tok) {
- return getc(tok->fp);
+ return getc(tok->fp);
}
/* Unfetch the last byte back into TOK. */
static void fp_ungetc(int c, struct tok_state *tok) {
- ungetc(c, tok->fp);
+ ungetc(c, tok->fp);
}
/* Check whether the characters at s start a valid
@@ -509,27 +509,27 @@ static void fp_ungetc(int c, struct tok_state *tok) {
the sequence if yes, 0 if not. */
static int valid_utf8(const unsigned char* s)
{
- int expected = 0;
- int length;
- if (*s < 0x80)
- /* single-byte code */
- return 1;
- if (*s < 0xc0)
- /* following byte */
- return 0;
- if (*s < 0xE0)
- expected = 1;
- else if (*s < 0xF0)
- expected = 2;
- else if (*s < 0xF8)
- expected = 3;
- else
- return 0;
- length = expected + 1;
- for (; expected; expected--)
- if (s[expected] < 0x80 || s[expected] >= 0xC0)
- return 0;
- return length;
+ int expected = 0;
+ int length;
+ if (*s < 0x80)
+ /* single-byte code */
+ return 1;
+ if (*s < 0xc0)
+ /* following byte */
+ return 0;
+ if (*s < 0xE0)
+ expected = 1;
+ else if (*s < 0xF0)
+ expected = 2;
+ else if (*s < 0xF8)
+ expected = 3;
+ else
+ return 0;
+ length = expected + 1;
+ for (; expected; expected--)
+ if (s[expected] < 0x80 || s[expected] >= 0xC0)
+ return 0;
+ return length;
}
/* Read a line of input from TOK. Determine encoding
@@ -538,95 +538,95 @@ static int valid_utf8(const unsigned char* s)
static char *
decoding_fgets(char *s, int size, struct tok_state *tok)
{
- char *line = NULL;
- int badchar = 0;
- for (;;) {
- if (tok->decoding_state == STATE_NORMAL) {
- /* We already have a codec associated with
- this input. */
- line = fp_readl(s, size, tok);
- break;
- } else if (tok->decoding_state == STATE_RAW) {
- /* We want a 'raw' read. */
- line = Py_UniversalNewlineFgets(s, size,
- tok->fp, NULL);
- break;
- } else {
- /* We have not yet determined the encoding.
- If an encoding is found, use the file-pointer
- reader functions from now on. */
- if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
- return error_ret(tok);
- assert(tok->decoding_state != STATE_INIT);
- }
- }
- if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
- if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
- return error_ret(tok);
- }
- }
+ char *line = NULL;
+ int badchar = 0;
+ for (;;) {
+ if (tok->decoding_state == STATE_NORMAL) {
+ /* We already have a codec associated with
+ this input. */
+ line = fp_readl(s, size, tok);
+ break;
+ } else if (tok->decoding_state == STATE_RAW) {
+ /* We want a 'raw' read. */
+ line = Py_UniversalNewlineFgets(s, size,
+ tok->fp, NULL);
+ break;
+ } else {
+ /* We have not yet determined the encoding.
+ If an encoding is found, use the file-pointer
+ reader functions from now on. */
+ if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
+ return error_ret(tok);
+ assert(tok->decoding_state != STATE_INIT);
+ }
+ }
+ if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
+ if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
+ return error_ret(tok);
+ }
+ }
#ifndef PGEN
- /* The default encoding is UTF-8, so make sure we don't have any
- non-UTF-8 sequences in it. */
- if (line && !tok->encoding) {
- unsigned char *c;
- int length;
- for (c = (unsigned char *)line; *c; c += length)
- if (!(length = valid_utf8(c))) {
- badchar = *c;
- break;
- }
- }
- if (badchar) {
- char buf[500];
- /* Need to add 1 to the line number, since this line
- has not been counted, yet. */
- sprintf(buf,
- "Non-UTF-8 code starting with '\\x%.2x' "
- "in file %.200s on line %i, "
- "but no encoding declared; "
- "see http://python.org/dev/peps/pep-0263/ for details",
- badchar, tok->filename, tok->lineno + 1);
- PyErr_SetString(PyExc_SyntaxError, buf);
- return error_ret(tok);
- }
+ /* The default encoding is UTF-8, so make sure we don't have any
+ non-UTF-8 sequences in it. */
+ if (line && !tok->encoding) {
+ unsigned char *c;
+ int length;
+ for (c = (unsigned char *)line; *c; c += length)
+ if (!(length = valid_utf8(c))) {
+ badchar = *c;
+ break;
+ }
+ }
+ if (badchar) {
+ char buf[500];
+ /* Need to add 1 to the line number, since this line
+ has not been counted, yet. */
+ sprintf(buf,
+ "Non-UTF-8 code starting with '\\x%.2x' "
+ "in file %.200s on line %i, "
+ "but no encoding declared; "
+ "see http://python.org/dev/peps/pep-0263/ for details",
+ badchar, tok->filename, tok->lineno + 1);
+ PyErr_SetString(PyExc_SyntaxError, buf);
+ return error_ret(tok);
+ }
#endif
- return line;
+ return line;
}
static int
decoding_feof(struct tok_state *tok)
{
- if (tok->decoding_state != STATE_NORMAL) {
- return feof(tok->fp);
- } else {
- PyObject* buf = tok->decoding_buffer;
- if (buf == NULL) {
- buf = PyObject_CallObject(tok->decoding_readline, NULL);
- if (buf == NULL) {
- error_ret(tok);
- return 1;
- } else {
- tok->decoding_buffer = buf;
- }
- }
- return PyObject_Length(buf) == 0;
- }
+ if (tok->decoding_state != STATE_NORMAL) {
+ return feof(tok->fp);
+ } else {
+ PyObject* buf = tok->decoding_buffer;
+ if (buf == NULL) {
+ buf = PyObject_CallObject(tok->decoding_readline, NULL);
+ if (buf == NULL) {
+ error_ret(tok);
+ return 1;
+ } else {
+ tok->decoding_buffer = buf;
+ }
+ }
+ return PyObject_Length(buf) == 0;
+ }
}
/* Fetch a byte from TOK, using the string buffer. */
static int
buf_getc(struct tok_state *tok) {
- return Py_CHARMASK(*tok->str++);
+ return Py_CHARMASK(*tok->str++);
}
/* Unfetch a byte from TOK, using the string buffer. */
static void
buf_ungetc(int c, struct tok_state *tok) {
- tok->str--;
- assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
+ tok->str--;
+ assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
}
/* Set the readline function for TOK to ENC. For the string-based
@@ -634,8 +634,8 @@ buf_ungetc(int c, struct tok_state *tok) {
static int
buf_setreadl(struct tok_state *tok, const char* enc) {
- tok->enc = enc;
- return 1;
+ tok->enc = enc;
+ return 1;
}
/* Return a UTF-8 encoding Python string object from the
@@ -643,13 +643,13 @@ buf_setreadl(struct tok_state *tok, const char* enc) {
static PyObject *
translate_into_utf8(const char* str, const char* enc) {
- PyObject *utf8;
- PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
- if (buf == NULL)
- return NULL;
- utf8 = PyUnicode_AsUTF8String(buf);
- Py_DECREF(buf);
- return utf8;
+ PyObject *utf8;
+ PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
+ if (buf == NULL)
+ return NULL;
+ utf8 = PyUnicode_AsUTF8String(buf);
+ Py_DECREF(buf);
+ return utf8;
}
/* Decode a byte string STR for use as the buffer of TOK.
@@ -659,53 +659,53 @@ translate_into_utf8(const char* str, const char* enc) {
static const char *
decode_str(const char *str, struct tok_state *tok)
{
- PyObject* utf8 = NULL;
- const char *s;
- const char *newl[2] = {NULL, NULL};
- int lineno = 0;
- tok->enc = NULL;
- tok->str = str;
- if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
- return error_ret(tok);
- str = tok->str; /* string after BOM if any */
- assert(str);
- if (tok->enc != NULL) {
- utf8 = translate_into_utf8(str, tok->enc);
- if (utf8 == NULL)
- return error_ret(tok);
- str = PyBytes_AsString(utf8);
- }
- for (s = str;; s++) {
- if (*s == '\0') break;
- else if (*s == '\n') {
- assert(lineno < 2);
- newl[lineno] = s;
- lineno++;
- if (lineno == 2) break;
- }
- }
- tok->enc = NULL;
- /* need to check line 1 and 2 separately since check_coding_spec
- assumes a single line as input */
- if (newl[0]) {
- if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
- return error_ret(tok);
- if (tok->enc == NULL && newl[1]) {
- if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
- tok, buf_setreadl))
- return error_ret(tok);
- }
- }
- if (tok->enc != NULL) {
- assert(utf8 == NULL);
- utf8 = translate_into_utf8(str, tok->enc);
- if (utf8 == NULL)
- return error_ret(tok);
- str = PyBytes_AS_STRING(utf8);
- }
- assert(tok->decoding_buffer == NULL);
- tok->decoding_buffer = utf8; /* CAUTION */
- return str;
+ PyObject* utf8 = NULL;
+ const char *s;
+ const char *newl[2] = {NULL, NULL};
+ int lineno = 0;
+ tok->enc = NULL;
+ tok->str = str;
+ if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
+ return error_ret(tok);
+ str = tok->str; /* string after BOM if any */
+ assert(str);
+ if (tok->enc != NULL) {
+ utf8 = translate_into_utf8(str, tok->enc);
+ if (utf8 == NULL)
+ return error_ret(tok);
+ str = PyBytes_AsString(utf8);
+ }
+ for (s = str;; s++) {
+ if (*s == '\0') break;
+ else if (*s == '\n') {
+ assert(lineno < 2);
+ newl[lineno] = s;
+ lineno++;
+ if (lineno == 2) break;
+ }
+ }
+ tok->enc = NULL;
+ /* need to check line 1 and 2 separately since check_coding_spec
+ assumes a single line as input */
+ if (newl[0]) {
+ if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
+ return error_ret(tok);
+ if (tok->enc == NULL && newl[1]) {
+ if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
+ tok, buf_setreadl))
+ return error_ret(tok);
+ }
+ }
+ if (tok->enc != NULL) {
+ assert(utf8 == NULL);
+ utf8 = translate_into_utf8(str, tok->enc);
+ if (utf8 == NULL)
+ return error_ret(tok);
+ str = PyBytes_AS_STRING(utf8);
+ }
+ assert(tok->decoding_buffer == NULL);
+ tok->decoding_buffer = utf8; /* CAUTION */
+ return str;
}
#endif /* PGEN */
@@ -715,40 +715,40 @@ decode_str(const char *str, struct tok_state *tok)
struct tok_state *
PyTokenizer_FromString(const char *str)
{
- struct tok_state *tok = tok_new();
- if (tok == NULL)
- return NULL;
- str = (char *)decode_str(str, tok);
- if (str == NULL) {
- PyTokenizer_Free(tok);
- return NULL;
- }
-
- /* XXX: constify members. */
- tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
- return tok;
+ struct tok_state *tok = tok_new();
+ if (tok == NULL)
+ return NULL;
+ str = (char *)decode_str(str, tok);
+ if (str == NULL) {
+ PyTokenizer_Free(tok);
+ return NULL;
+ }
+
+ /* XXX: constify members. */
+ tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
+ return tok;
}
struct tok_state *
PyTokenizer_FromUTF8(const char *str)
{
- struct tok_state *tok = tok_new();
- if (tok == NULL)
- return NULL;
- tok->decoding_state = STATE_RAW;
- tok->read_coding_spec = 1;
- tok->enc = NULL;
- tok->str = str;
- tok->encoding = (char *)PyMem_MALLOC(6);
- if (!tok->encoding) {
- PyTokenizer_Free(tok);
- return NULL;
- }
- strcpy(tok->encoding, "utf-8");
-
- /* XXX: constify members. */
- tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
- return tok;
+ struct tok_state *tok = tok_new();
+ if (tok == NULL)
+ return NULL;
+ tok->decoding_state = STATE_RAW;
+ tok->read_coding_spec = 1;
+ tok->enc = NULL;
+ tok->str = str;
+ tok->encoding = (char *)PyMem_MALLOC(6);
+ if (!tok->encoding) {
+ PyTokenizer_Free(tok);
+ return NULL;
+ }
+ strcpy(tok->encoding, "utf-8");
+
+ /* XXX: constify members. */
+ tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
+ return tok;
}
@@ -757,30 +757,30 @@ PyTokenizer_FromUTF8(const char *str)
struct tok_state *
PyTokenizer_FromFile(FILE *fp, char* enc, char *ps1, char *ps2)
{
- struct tok_state *tok = tok_new();
- if (tok == NULL)
- return NULL;
- if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
- PyTokenizer_Free(tok);
- return NULL;
- }
- tok->cur = tok->inp = tok->buf;
- tok->end = tok->buf + BUFSIZ;
- tok->fp = fp;
- tok->prompt = ps1;
- tok->nextprompt = ps2;
- if (enc != NULL) {
- /* Must copy encoding declaration since it
- gets copied into the parse tree. */
- tok->encoding = PyMem_MALLOC(strlen(enc)+1);
- if (!tok->encoding) {
- PyTokenizer_Free(tok);
- return NULL;
- }
- strcpy(tok->encoding, enc);
- tok->decoding_state = STATE_NORMAL;
- }
- return tok;
+ struct tok_state *tok = tok_new();
+ if (tok == NULL)
+ return NULL;
+ if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
+ PyTokenizer_Free(tok);
+ return NULL;
+ }
+ tok->cur = tok->inp = tok->buf;
+ tok->end = tok->buf + BUFSIZ;
+ tok->fp = fp;
+ tok->prompt = ps1;
+ tok->nextprompt = ps2;
+ if (enc != NULL) {
+ /* Must copy encoding declaration since it
+ gets copied into the parse tree. */
+ tok->encoding = PyMem_MALLOC(strlen(enc)+1);
+ if (!tok->encoding) {
+ PyTokenizer_Free(tok);
+ return NULL;
+ }
+ strcpy(tok->encoding, enc);
+ tok->decoding_state = STATE_NORMAL;
+ }
+ return tok;
}
@@ -789,15 +789,15 @@ PyTokenizer_FromFile(FILE *fp, char* enc, char *ps1, char *ps2)
void
PyTokenizer_Free(struct tok_state *tok)
{
- if (tok->encoding != NULL)
- PyMem_FREE(tok->encoding);
+ if (tok->encoding != NULL)
+ PyMem_FREE(tok->encoding);
#ifndef PGEN
- Py_XDECREF(tok->decoding_readline);
- Py_XDECREF(tok->decoding_buffer);
+ Py_XDECREF(tok->decoding_readline);
+ Py_XDECREF(tok->decoding_buffer);
#endif
- if (tok->fp != NULL && tok->buf != NULL)
- PyMem_FREE(tok->buf);
- PyMem_FREE(tok);
+ if (tok->fp != NULL && tok->buf != NULL)
+ PyMem_FREE(tok->buf);
+ PyMem_FREE(tok);
}
/* Get next char, updating state; error code goes into tok->done */
@@ -805,188 +805,188 @@ PyTokenizer_Free(struct tok_state *tok)
static int
tok_nextc(register struct tok_state *tok)
{
- for (;;) {
- if (tok->cur != tok->inp) {
- return Py_CHARMASK(*tok->cur++); /* Fast path */
- }
- if (tok->done != E_OK)
- return EOF;
- if (tok->fp == NULL) {
- char *end = strchr(tok->inp, '\n');
- if (end != NULL)
- end++;
- else {
- end = strchr(tok->inp, '\0');
- if (end == tok->inp) {
- tok->done = E_EOF;
- return EOF;
- }
- }
- if (tok->start == NULL)
- tok->buf = tok->cur;
- tok->line_start = tok->cur;
- tok->lineno++;
- tok->inp = end;
- return Py_CHARMASK(*tok->cur++);
- }
- if (tok->prompt != NULL) {
- char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
+ for (;;) {
+ if (tok->cur != tok->inp) {
+ return Py_CHARMASK(*tok->cur++); /* Fast path */
+ }
+ if (tok->done != E_OK)
+ return EOF;
+ if (tok->fp == NULL) {
+ char *end = strchr(tok->inp, '\n');
+ if (end != NULL)
+ end++;
+ else {
+ end = strchr(tok->inp, '\0');
+ if (end == tok->inp) {
+ tok->done = E_EOF;
+ return EOF;
+ }
+ }
+ if (tok->start == NULL)
+ tok->buf = tok->cur;
+ tok->line_start = tok->cur;
+ tok->lineno++;
+ tok->inp = end;
+ return Py_CHARMASK(*tok->cur++);
+ }
+ if (tok->prompt != NULL) {
+ char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
#ifndef PGEN
- if (tok->encoding && newtok && *newtok) {
- /* Recode to UTF-8 */
- Py_ssize_t buflen;
- const char* buf;
- PyObject *u = translate_into_utf8(newtok, tok->encoding);
- PyMem_FREE(newtok);
- if (!u) {
- tok->done = E_DECODE;
- return EOF;
- }
- buflen = PyBytes_GET_SIZE(u);
- buf = PyBytes_AS_STRING(u);
- if (!buf) {
- Py_DECREF(u);
- tok->done = E_DECODE;
- return EOF;
- }
- newtok = PyMem_MALLOC(buflen+1);
- strcpy(newtok, buf);
- Py_DECREF(u);
- }
+ if (tok->encoding && newtok && *newtok) {
+ /* Recode to UTF-8 */
+ Py_ssize_t buflen;
+ const char* buf;
+ PyObject *u = translate_into_utf8(newtok, tok->encoding);
+ PyMem_FREE(newtok);
+ if (!u) {
+ tok->done = E_DECODE;
+ return EOF;
+ }
+ buflen = PyBytes_GET_SIZE(u);
+ buf = PyBytes_AS_STRING(u);
+ if (!buf) {
+ Py_DECREF(u);
+ tok->done = E_DECODE;
+ return EOF;
+ }
+ newtok = PyMem_MALLOC(buflen+1);
+ strcpy(newtok, buf);
+ Py_DECREF(u);
+ }
#endif
- if (tok->nextprompt != NULL)
- tok->prompt = tok->nextprompt;
- if (newtok == NULL)
- tok->done = E_INTR;
- else if (*newtok == '\0') {
- PyMem_FREE(newtok);
- tok->done = E_EOF;
- }
- else if (tok->start != NULL) {
- size_t start = tok->start - tok->buf;
- size_t oldlen = tok->cur - tok->buf;
- size_t newlen = oldlen + strlen(newtok);
- char *buf = tok->buf;
- buf = (char *)PyMem_REALLOC(buf, newlen+1);
- tok->lineno++;
- if (buf == NULL) {
- PyMem_FREE(tok->buf);
- tok->buf = NULL;
- PyMem_FREE(newtok);
- tok->done = E_NOMEM;
- return EOF;
- }
- tok->buf = buf;
- tok->cur = tok->buf + oldlen;
- tok->line_start = tok->cur;
- strcpy(tok->buf + oldlen, newtok);
- PyMem_FREE(newtok);
- tok->inp = tok->buf + newlen;
- tok->end = tok->inp + 1;
- tok->start = tok->buf + start;
- }
- else {
- tok->lineno++;
- if (tok->buf != NULL)
- PyMem_FREE(tok->buf);
- tok->buf = newtok;
- tok->line_start = tok->buf;
- tok->cur = tok->buf;
- tok->line_start = tok->buf;
- tok->inp = strchr(tok->buf, '\0');
- tok->end = tok->inp + 1;
- }
- }
- else {
- int done = 0;
- Py_ssize_t cur = 0;
- char *pt;
- if (tok->start == NULL) {
- if (tok->buf == NULL) {
- tok->buf = (char *)
- PyMem_MALLOC(BUFSIZ);
- if (tok->buf == NULL) {
- tok->done = E_NOMEM;
- return EOF;
- }
- tok->end = tok->buf + BUFSIZ;
- }
- if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
- tok) == NULL) {
- tok->done = E_EOF;
- done = 1;
- }
- else {
- tok->done = E_OK;
- tok->inp = strchr(tok->buf, '\0');
- done = tok->inp[-1] == '\n';
- }
- }
- else {
- cur = tok->cur - tok->buf;
- if (decoding_feof(tok)) {
- tok->done = E_EOF;
- done = 1;
- }
- else
- tok->done = E_OK;
- }
- tok->lineno++;
- /* Read until '\n' or EOF */
- while (!done) {
- Py_ssize_t curstart = tok->start == NULL ? -1 :
- tok->start - tok->buf;
- Py_ssize_t curvalid = tok->inp - tok->buf;
- Py_ssize_t newsize = curvalid + BUFSIZ;
- char *newbuf = tok->buf;
- newbuf = (char *)PyMem_REALLOC(newbuf,
- newsize);
- if (newbuf == NULL) {
- tok->done = E_NOMEM;
- tok->cur = tok->inp;
- return EOF;
- }
- tok->buf = newbuf;
- tok->inp = tok->buf + curvalid;
- tok->end = tok->buf + newsize;
- tok->start = curstart < 0 ? NULL :
- tok->buf + curstart;
- if (decoding_fgets(tok->inp,
- (int)(tok->end - tok->inp),
- tok) == NULL) {
- /* Break out early on decoding
- errors, as tok->buf will be NULL
- */
- if (tok->decoding_erred)
- return EOF;
- /* Last line does not end in \n,
- fake one */
- strcpy(tok->inp, "\n");
- }
- tok->inp = strchr(tok->inp, '\0');
- done = tok->inp[-1] == '\n';
- }
- if (tok->buf != NULL) {
- tok->cur = tok->buf + cur;
- tok->line_start = tok->cur;
- /* replace "\r\n" with "\n" */
- /* For Mac leave the \r, giving a syntax error */
- pt = tok->inp - 2;
- if (pt >= tok->buf && *pt == '\r') {
- *pt++ = '\n';
- *pt = '\0';
- tok->inp = pt;
- }
- }
- }
- if (tok->done != E_OK) {
- if (tok->prompt != NULL)
- PySys_WriteStderr("\n");
- tok->cur = tok->inp;
- return EOF;
- }
- }
- /*NOTREACHED*/
+ if (tok->nextprompt != NULL)
+ tok->prompt = tok->nextprompt;
+ if (newtok == NULL)
+ tok->done = E_INTR;
+ else if (*newtok == '\0') {
+ PyMem_FREE(newtok);
+ tok->done = E_EOF;
+ }
+ else if (tok->start != NULL) {
+ size_t start = tok->start - tok->buf;
+ size_t oldlen = tok->cur - tok->buf;
+ size_t newlen = oldlen + strlen(newtok);
+ char *buf = tok->buf;
+ buf = (char *)PyMem_REALLOC(buf, newlen+1);
+ tok->lineno++;
+ if (buf == NULL) {
+ PyMem_FREE(tok->buf);
+ tok->buf = NULL;
+ PyMem_FREE(newtok);
+ tok->done = E_NOMEM;
+ return EOF;
+ }
+ tok->buf = buf;
+ tok->cur = tok->buf + oldlen;
+ tok->line_start = tok->cur;
+ strcpy(tok->buf + oldlen, newtok);
+ PyMem_FREE(newtok);
+ tok->inp = tok->buf + newlen;
+ tok->end = tok->inp + 1;
+ tok->start = tok->buf + start;
+ }
+ else {
+ tok->lineno++;
+ if (tok->buf != NULL)
+ PyMem_FREE(tok->buf);
+ tok->buf = newtok;
+ tok->line_start = tok->buf;
+ tok->cur = tok->buf;
+ tok->line_start = tok->buf;
+ tok->inp = strchr(tok->buf, '\0');
+ tok->end = tok->inp + 1;
+ }
+ }
+ else {
+ int done = 0;
+ Py_ssize_t cur = 0;
+ char *pt;
+ if (tok->start == NULL) {
+ if (tok->buf == NULL) {
+ tok->buf = (char *)
+ PyMem_MALLOC(BUFSIZ);
+ if (tok->buf == NULL) {
+ tok->done = E_NOMEM;
+ return EOF;
+ }
+ tok->end = tok->buf + BUFSIZ;
+ }
+ if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
+ tok) == NULL) {
+ tok->done = E_EOF;
+ done = 1;
+ }
+ else {
+ tok->done = E_OK;
+ tok->inp = strchr(tok->buf, '\0');
+ done = tok->inp[-1] == '\n';
+ }
+ }
+ else {
+ cur = tok->cur - tok->buf;
+ if (decoding_feof(tok)) {
+ tok->done = E_EOF;
+ done = 1;
+ }
+ else
+ tok->done = E_OK;
+ }
+ tok->lineno++;
+ /* Read until '\n' or EOF */
+ while (!done) {
+ Py_ssize_t curstart = tok->start == NULL ? -1 :
+ tok->start - tok->buf;
+ Py_ssize_t curvalid = tok->inp - tok->buf;
+ Py_ssize_t newsize = curvalid + BUFSIZ;
+ char *newbuf = tok->buf;
+ newbuf = (char *)PyMem_REALLOC(newbuf,
+ newsize);
+ if (newbuf == NULL) {
+ tok->done = E_NOMEM;
+ tok->cur = tok->inp;
+ return EOF;
+ }
+ tok->buf = newbuf;
+ tok->inp = tok->buf + curvalid;
+ tok->end = tok->buf + newsize;
+ tok->start = curstart < 0 ? NULL :
+ tok->buf + curstart;
+ if (decoding_fgets(tok->inp,
+ (int)(tok->end - tok->inp),
+ tok) == NULL) {
+ /* Break out early on decoding
+ errors, as tok->buf will be NULL
+ */
+ if (tok->decoding_erred)
+ return EOF;
+ /* Last line does not end in \n,
+ fake one */
+ strcpy(tok->inp, "\n");
+ }
+ tok->inp = strchr(tok->inp, '\0');
+ done = tok->inp[-1] == '\n';
+ }
+ if (tok->buf != NULL) {
+ tok->cur = tok->buf + cur;
+ tok->line_start = tok->cur;
+ /* replace "\r\n" with "\n" */
+ /* For Mac leave the \r, giving a syntax error */
+ pt = tok->inp - 2;
+ if (pt >= tok->buf && *pt == '\r') {
+ *pt++ = '\n';
+ *pt = '\0';
+ tok->inp = pt;
+ }
+ }
+ }
+ if (tok->done != E_OK) {
+ if (tok->prompt != NULL)
+ PySys_WriteStderr("\n");
+ tok->cur = tok->inp;
+ return EOF;
+ }
+ }
+ /*NOTREACHED*/
}
@@ -995,12 +995,12 @@ tok_nextc(register struct tok_state *tok)
static void
tok_backup(register struct tok_state *tok, register int c)
{
- if (c != EOF) {
- if (--tok->cur < tok->buf)
- Py_FatalError("tok_backup: beginning of buffer");
- if (*tok->cur != c)
- *tok->cur = c;
- }
+ if (c != EOF) {
+ if (--tok->cur < tok->buf)
+ Py_FatalError("tok_backup: beginning of buffer");
+ if (*tok->cur != c)
+ *tok->cur = c;
+ }
}
@@ -1009,181 +1009,181 @@ tok_backup(register struct tok_state *tok, register int c)
int
PyToken_OneChar(int c)
{
- switch (c) {
- case '(': return LPAR;
- case ')': return RPAR;
- case '[': return LSQB;
- case ']': return RSQB;
- case ':': return COLON;
- case ',': return COMMA;
- case ';': return SEMI;
- case '+': return PLUS;
- case '-': return MINUS;
- case '*': return STAR;
- case '/': return SLASH;
- case '|': return VBAR;
- case '&': return AMPER;
- case '<': return LESS;
- case '>': return GREATER;
- case '=': return EQUAL;
- case '.': return DOT;
- case '%': return PERCENT;
- case '{': return LBRACE;
- case '}': return RBRACE;
- case '^': return CIRCUMFLEX;
- case '~': return TILDE;
- case '@': return AT;
- default: return OP;
- }
+ switch (c) {
+ case '(': return LPAR;
+ case ')': return RPAR;
+ case '[': return LSQB;
+ case ']': return RSQB;
+ case ':': return COLON;
+ case ',': return COMMA;
+ case ';': return SEMI;
+ case '+': return PLUS;
+ case '-': return MINUS;
+ case '*': return STAR;
+ case '/': return SLASH;
+ case '|': return VBAR;
+ case '&': return AMPER;
+ case '<': return LESS;
+ case '>': return GREATER;
+ case '=': return EQUAL;
+ case '.': return DOT;
+ case '%': return PERCENT;
+ case '{': return LBRACE;
+ case '}': return RBRACE;
+ case '^': return CIRCUMFLEX;
+ case '~': return TILDE;
+ case '@': return AT;
+ default: return OP;
+ }
}
int
PyToken_TwoChars(int c1, int c2)
{
- switch (c1) {
- case '=':
- switch (c2) {
- case '=': return EQEQUAL;
- }
- break;
- case '!':
- switch (c2) {
- case '=': return NOTEQUAL;
- }
- break;
- case '<':
- switch (c2) {
- case '>': return NOTEQUAL;
- case '=': return LESSEQUAL;
- case '<': return LEFTSHIFT;
- }
- break;
- case '>':
- switch (c2) {
- case '=': return GREATEREQUAL;
- case '>': return RIGHTSHIFT;
- }
- break;
- case '+':
- switch (c2) {
- case '=': return PLUSEQUAL;
- }
- break;
- case '-':
- switch (c2) {
- case '=': return MINEQUAL;
- case '>': return RARROW;
- }
- break;
- case '*':
- switch (c2) {
- case '*': return DOUBLESTAR;
- case '=': return STAREQUAL;
- }
- break;
- case '/':
- switch (c2) {
- case '/': return DOUBLESLASH;
- case '=': return SLASHEQUAL;
- }
- break;
- case '|':
- switch (c2) {
- case '=': return VBAREQUAL;
- }
- break;
- case '%':
- switch (c2) {
- case '=': return PERCENTEQUAL;
- }
- break;
- case '&':
- switch (c2) {
- case '=': return AMPEREQUAL;
- }
- break;
- case '^':
- switch (c2) {
- case '=': return CIRCUMFLEXEQUAL;
- }
- break;
- }
- return OP;
+ switch (c1) {
+ case '=':
+ switch (c2) {
+ case '=': return EQEQUAL;
+ }
+ break;
+ case '!':
+ switch (c2) {
+ case '=': return NOTEQUAL;
+ }
+ break;
+ case '<':
+ switch (c2) {
+ case '>': return NOTEQUAL;
+ case '=': return LESSEQUAL;
+ case '<': return LEFTSHIFT;
+ }
+ break;
+ case '>':
+ switch (c2) {
+ case '=': return GREATEREQUAL;
+ case '>': return RIGHTSHIFT;
+ }
+ break;
+ case '+':
+ switch (c2) {
+ case '=': return PLUSEQUAL;
+ }
+ break;
+ case '-':
+ switch (c2) {
+ case '=': return MINEQUAL;
+ case '>': return RARROW;
+ }
+ break;
+ case '*':
+ switch (c2) {
+ case '*': return DOUBLESTAR;
+ case '=': return STAREQUAL;
+ }
+ break;
+ case '/':
+ switch (c2) {
+ case '/': return DOUBLESLASH;
+ case '=': return SLASHEQUAL;
+ }
+ break;
+ case '|':
+ switch (c2) {
+ case '=': return VBAREQUAL;
+ }
+ break;
+ case '%':
+ switch (c2) {
+ case '=': return PERCENTEQUAL;
+ }
+ break;
+ case '&':
+ switch (c2) {
+ case '=': return AMPEREQUAL;
+ }
+ break;
+ case '^':
+ switch (c2) {
+ case '=': return CIRCUMFLEXEQUAL;
+ }
+ break;
+ }
+ return OP;
}
int
PyToken_ThreeChars(int c1, int c2, int c3)
{
- switch (c1) {
- case '<':
- switch (c2) {
- case '<':
- switch (c3) {
- case '=':
- return LEFTSHIFTEQUAL;
- }
- break;
- }
- break;
- case '>':
- switch (c2) {
- case '>':
- switch (c3) {
- case '=':
- return RIGHTSHIFTEQUAL;
- }
- break;
- }
- break;
- case '*':
- switch (c2) {
- case '*':
- switch (c3) {
- case '=':
- return DOUBLESTAREQUAL;
- }
- break;
- }
- break;
- case '/':
- switch (c2) {
- case '/':
- switch (c3) {
- case '=':
- return DOUBLESLASHEQUAL;
- }
- break;
- }
- break;
+ switch (c1) {
+ case '<':
+ switch (c2) {
+ case '<':
+ switch (c3) {
+ case '=':
+ return LEFTSHIFTEQUAL;
+ }
+ break;
+ }
+ break;
+ case '>':
+ switch (c2) {
+ case '>':
+ switch (c3) {
+ case '=':
+ return RIGHTSHIFTEQUAL;
+ }
+ break;
+ }
+ break;
+ case '*':
+ switch (c2) {
+ case '*':
+ switch (c3) {
+ case '=':
+ return DOUBLESTAREQUAL;
+ }
+ break;
+ }
+ break;
+ case '/':
+ switch (c2) {
+ case '/':
+ switch (c3) {
+ case '=':
+ return DOUBLESLASHEQUAL;
+ }
+ break;
+ }
+ break;
+ case '.':
+ switch (c2) {
case '.':
- switch (c2) {
- case '.':
- switch (c3) {
- case '.':
- return ELLIPSIS;
- }
- break;
- }
- break;
- }
- return OP;
+ switch (c3) {
+ case '.':
+ return ELLIPSIS;
+ }
+ break;
+ }
+ break;
+ }
+ return OP;
}
static int
indenterror(struct tok_state *tok)
{
- if (tok->alterror) {
- tok->done = E_TABSPACE;
- tok->cur = tok->inp;
- return 1;
- }
- if (tok->altwarning) {
- PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
- "in indentation\n", tok->filename);
- tok->altwarning = 0;
- }
- return 0;
+ if (tok->alterror) {
+ tok->done = E_TABSPACE;
+ tok->cur = tok->inp;
+ return 1;
+ }
+ if (tok->altwarning) {
+ PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
+ "in indentation\n", tok->filename);
+ tok->altwarning = 0;
+ }
+ return 0;
}
#ifdef PGEN
@@ -1193,23 +1193,23 @@ indenterror(struct tok_state *tok)
static int
verify_identifier(struct tok_state *tok)
{
- PyObject *s;
- int result;
- s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
- if (s == NULL) {
- if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
- PyErr_Clear();
- tok->done = E_IDENTIFIER;
- } else {
- tok->done = E_ERROR;
- }
- return 0;
- }
- result = PyUnicode_IsIdentifier(s);
- Py_DECREF(s);
- if (result == 0)
- tok->done = E_IDENTIFIER;
- return result;
+ PyObject *s;
+ int result;
+ s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
+ if (s == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
+ PyErr_Clear();
+ tok->done = E_IDENTIFIER;
+ } else {
+ tok->done = E_ERROR;
+ }
+ return 0;
+ }
+ result = PyUnicode_IsIdentifier(s);
+ Py_DECREF(s);
+ if (result == 0)
+ tok->done = E_IDENTIFIER;
+ return result;
}
#endif
@@ -1218,413 +1218,413 @@ verify_identifier(struct tok_state *tok)
static int
tok_get(register struct tok_state *tok, char **p_start, char **p_end)
{
- register int c;
- int blankline, nonascii;
+ register int c;
+ int blankline, nonascii;
- *p_start = *p_end = NULL;
+ *p_start = *p_end = NULL;
nextline:
- tok->start = NULL;
- blankline = 0;
-
- /* Get indentation level */
- if (tok->atbol) {
- register int col = 0;
- register int altcol = 0;
- tok->atbol = 0;
- for (;;) {
- c = tok_nextc(tok);
- if (c == ' ')
- col++, altcol++;
- else if (c == '\t') {
- col = (col/tok->tabsize + 1) * tok->tabsize;
- altcol = (altcol/tok->alttabsize + 1)
- * tok->alttabsize;
- }
- else if (c == '\014') /* Control-L (formfeed) */
- col = altcol = 0; /* For Emacs users */
- else
- break;
- }
- tok_backup(tok, c);
- if (c == '#' || c == '\n') {
- /* Lines with only whitespace and/or comments
- shouldn't affect the indentation and are
- not passed to the parser as NEWLINE tokens,
- except *totally* empty lines in interactive
- mode, which signal the end of a command group. */
- if (col == 0 && c == '\n' && tok->prompt != NULL)
- blankline = 0; /* Let it through */
- else
- blankline = 1; /* Ignore completely */
- /* We can't jump back right here since we still
- may need to skip to the end of a comment */
- }
- if (!blankline && tok->level == 0) {
- if (col == tok->indstack[tok->indent]) {
- /* No change */
- if (altcol != tok->altindstack[tok->indent]) {
- if (indenterror(tok))
- return ERRORTOKEN;
- }
- }
- else if (col > tok->indstack[tok->indent]) {
- /* Indent -- always one */
- if (tok->indent+1 >= MAXINDENT) {
- tok->done = E_TOODEEP;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- if (altcol <= tok->altindstack[tok->indent]) {
- if (indenterror(tok))
- return ERRORTOKEN;
- }
- tok->pendin++;
- tok->indstack[++tok->indent] = col;
- tok->altindstack[tok->indent] = altcol;
- }
- else /* col < tok->indstack[tok->indent] */ {
- /* Dedent -- any number, must be consistent */
- while (tok->indent > 0 &&
- col < tok->indstack[tok->indent]) {
- tok->pendin--;
- tok->indent--;
- }
- if (col != tok->indstack[tok->indent]) {
- tok->done = E_DEDENT;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- if (altcol != tok->altindstack[tok->indent]) {
- if (indenterror(tok))
- return ERRORTOKEN;
- }
- }
- }
- }
-
- tok->start = tok->cur;
-
- /* Return pending indents/dedents */
- if (tok->pendin != 0) {
- if (tok->pendin < 0) {
- tok->pendin++;
- return DEDENT;
- }
- else {
- tok->pendin--;
- return INDENT;
- }
- }
+ tok->start = NULL;
+ blankline = 0;
+
+ /* Get indentation level */
+ if (tok->atbol) {
+ register int col = 0;
+ register int altcol = 0;
+ tok->atbol = 0;
+ for (;;) {
+ c = tok_nextc(tok);
+ if (c == ' ')
+ col++, altcol++;
+ else if (c == '\t') {
+ col = (col/tok->tabsize + 1) * tok->tabsize;
+ altcol = (altcol/tok->alttabsize + 1)
+ * tok->alttabsize;
+ }
+ else if (c == '\014') /* Control-L (formfeed) */
+ col = altcol = 0; /* For Emacs users */
+ else
+ break;
+ }
+ tok_backup(tok, c);
+ if (c == '#' || c == '\n') {
+ /* Lines with only whitespace and/or comments
+ shouldn't affect the indentation and are
+ not passed to the parser as NEWLINE tokens,
+ except *totally* empty lines in interactive
+ mode, which signal the end of a command group. */
+ if (col == 0 && c == '\n' && tok->prompt != NULL)
+ blankline = 0; /* Let it through */
+ else
+ blankline = 1; /* Ignore completely */
+ /* We can't jump back right here since we still
+ may need to skip to the end of a comment */
+ }
+ if (!blankline && tok->level == 0) {
+ if (col == tok->indstack[tok->indent]) {
+ /* No change */
+ if (altcol != tok->altindstack[tok->indent]) {
+ if (indenterror(tok))
+ return ERRORTOKEN;
+ }
+ }
+ else if (col > tok->indstack[tok->indent]) {
+ /* Indent -- always one */
+ if (tok->indent+1 >= MAXINDENT) {
+ tok->done = E_TOODEEP;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ if (altcol <= tok->altindstack[tok->indent]) {
+ if (indenterror(tok))
+ return ERRORTOKEN;
+ }
+ tok->pendin++;
+ tok->indstack[++tok->indent] = col;
+ tok->altindstack[tok->indent] = altcol;
+ }
+ else /* col < tok->indstack[tok->indent] */ {
+ /* Dedent -- any number, must be consistent */
+ while (tok->indent > 0 &&
+ col < tok->indstack[tok->indent]) {
+ tok->pendin--;
+ tok->indent--;
+ }
+ if (col != tok->indstack[tok->indent]) {
+ tok->done = E_DEDENT;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ if (altcol != tok->altindstack[tok->indent]) {
+ if (indenterror(tok))
+ return ERRORTOKEN;
+ }
+ }
+ }
+ }
+
+ tok->start = tok->cur;
+
+ /* Return pending indents/dedents */
+ if (tok->pendin != 0) {
+ if (tok->pendin < 0) {
+ tok->pendin++;
+ return DEDENT;
+ }
+ else {
+ tok->pendin--;
+ return INDENT;
+ }
+ }
again:
- tok->start = NULL;
- /* Skip spaces */
- do {
- c = tok_nextc(tok);
- } while (c == ' ' || c == '\t' || c == '\014');
-
- /* Set start of current token */
- tok->start = tok->cur - 1;
-
- /* Skip comment */
- if (c == '#')
- while (c != EOF && c != '\n')
- c = tok_nextc(tok);
-
- /* Check for EOF and errors now */
- if (c == EOF) {
- return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
- }
-
- /* Identifier (most frequent token!) */
- nonascii = 0;
- if (is_potential_identifier_start(c)) {
- /* Process b"", r"" and br"" */
- if (c == 'b' || c == 'B') {
- c = tok_nextc(tok);
- if (c == '"' || c == '\'')
- goto letter_quote;
- }
- if (c == 'r' || c == 'R') {
- c = tok_nextc(tok);
- if (c == '"' || c == '\'')
- goto letter_quote;
- }
- while (is_potential_identifier_char(c)) {
- if (c >= 128)
- nonascii = 1;
- c = tok_nextc(tok);
- }
- tok_backup(tok, c);
- if (nonascii &&
- !verify_identifier(tok)) {
- tok->done = E_IDENTIFIER;
- return ERRORTOKEN;
- }
- *p_start = tok->start;
- *p_end = tok->cur;
- return NAME;
- }
-
- /* Newline */
- if (c == '\n') {
- tok->atbol = 1;
- if (blankline || tok->level > 0)
- goto nextline;
- *p_start = tok->start;
- *p_end = tok->cur - 1; /* Leave '\n' out of the string */
- tok->cont_line = 0;
- return NEWLINE;
- }
-
- /* Period or number starting with period? */
- if (c == '.') {
- c = tok_nextc(tok);
- if (isdigit(c)) {
- goto fraction;
- } else if (c == '.') {
- c = tok_nextc(tok);
- if (c == '.') {
- *p_start = tok->start;
- *p_end = tok->cur;
- return ELLIPSIS;
- } else {
- tok_backup(tok, c);
- }
- tok_backup(tok, '.');
- } else {
- tok_backup(tok, c);
- }
- *p_start = tok->start;
- *p_end = tok->cur;
- return DOT;
- }
-
- /* Number */
- if (isdigit(c)) {
- if (c == '0') {
- /* Hex, octal or binary -- maybe. */
- c = tok_nextc(tok);
- if (c == '.')
- goto fraction;
+ tok->start = NULL;
+ /* Skip spaces */
+ do {
+ c = tok_nextc(tok);
+ } while (c == ' ' || c == '\t' || c == '\014');
+
+ /* Set start of current token */
+ tok->start = tok->cur - 1;
+
+ /* Skip comment */
+ if (c == '#')
+ while (c != EOF && c != '\n')
+ c = tok_nextc(tok);
+
+ /* Check for EOF and errors now */
+ if (c == EOF) {
+ return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
+ }
+
+ /* Identifier (most frequent token!) */
+ nonascii = 0;
+ if (is_potential_identifier_start(c)) {
+ /* Process b"", r"" and br"" */
+ if (c == 'b' || c == 'B') {
+ c = tok_nextc(tok);
+ if (c == '"' || c == '\'')
+ goto letter_quote;
+ }
+ if (c == 'r' || c == 'R') {
+ c = tok_nextc(tok);
+ if (c == '"' || c == '\'')
+ goto letter_quote;
+ }
+ while (is_potential_identifier_char(c)) {
+ if (c >= 128)
+ nonascii = 1;
+ c = tok_nextc(tok);
+ }
+ tok_backup(tok, c);
+ if (nonascii &&
+ !verify_identifier(tok)) {
+ tok->done = E_IDENTIFIER;
+ return ERRORTOKEN;
+ }
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return NAME;
+ }
+
+ /* Newline */
+ if (c == '\n') {
+ tok->atbol = 1;
+ if (blankline || tok->level > 0)
+ goto nextline;
+ *p_start = tok->start;
+ *p_end = tok->cur - 1; /* Leave '\n' out of the string */
+ tok->cont_line = 0;
+ return NEWLINE;
+ }
+
+ /* Period or number starting with period? */
+ if (c == '.') {
+ c = tok_nextc(tok);
+ if (isdigit(c)) {
+ goto fraction;
+ } else if (c == '.') {
+ c = tok_nextc(tok);
+ if (c == '.') {
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return ELLIPSIS;
+ } else {
+ tok_backup(tok, c);
+ }
+ tok_backup(tok, '.');
+ } else {
+ tok_backup(tok, c);
+ }
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return DOT;
+ }
+
+ /* Number */
+ if (isdigit(c)) {
+ if (c == '0') {
+ /* Hex, octal or binary -- maybe. */
+ c = tok_nextc(tok);
+ if (c == '.')
+ goto fraction;
#ifndef WITHOUT_COMPLEX
- if (c == 'j' || c == 'J')
- goto imaginary;
+ if (c == 'j' || c == 'J')
+ goto imaginary;
#endif
- if (c == 'x' || c == 'X') {
-
- /* Hex */
- c = tok_nextc(tok);
- if (!isxdigit(c)) {
- tok->done = E_TOKEN;
- tok_backup(tok, c);
- return ERRORTOKEN;
- }
- do {
- c = tok_nextc(tok);
- } while (isxdigit(c));
- }
- else if (c == 'o' || c == 'O') {
- /* Octal */
- c = tok_nextc(tok);
- if (c < '0' || c >= '8') {
- tok->done = E_TOKEN;
- tok_backup(tok, c);
- return ERRORTOKEN;
- }
- do {
- c = tok_nextc(tok);
- } while ('0' <= c && c < '8');
- }
- else if (c == 'b' || c == 'B') {
- /* Binary */
- c = tok_nextc(tok);
- if (c != '0' && c != '1') {
- tok->done = E_TOKEN;
- tok_backup(tok, c);
- return ERRORTOKEN;
- }
- do {
- c = tok_nextc(tok);
- } while (c == '0' || c == '1');
- }
- else {
- int nonzero = 0;
- /* maybe old-style octal; c is first char of it */
- /* in any case, allow '0' as a literal */
- while (c == '0')
- c = tok_nextc(tok);
- while (isdigit(c)) {
- nonzero = 1;
- c = tok_nextc(tok);
- }
- if (c == '.')
- goto fraction;
- else if (c == 'e' || c == 'E')
- goto exponent;
+ if (c == 'x' || c == 'X') {
+
+ /* Hex */
+ c = tok_nextc(tok);
+ if (!isxdigit(c)) {
+ tok->done = E_TOKEN;
+ tok_backup(tok, c);
+ return ERRORTOKEN;
+ }
+ do {
+ c = tok_nextc(tok);
+ } while (isxdigit(c));
+ }
+ else if (c == 'o' || c == 'O') {
+ /* Octal */
+ c = tok_nextc(tok);
+ if (c < '0' || c >= '8') {
+ tok->done = E_TOKEN;
+ tok_backup(tok, c);
+ return ERRORTOKEN;
+ }
+ do {
+ c = tok_nextc(tok);
+ } while ('0' <= c && c < '8');
+ }
+ else if (c == 'b' || c == 'B') {
+ /* Binary */
+ c = tok_nextc(tok);
+ if (c != '0' && c != '1') {
+ tok->done = E_TOKEN;
+ tok_backup(tok, c);
+ return ERRORTOKEN;
+ }
+ do {
+ c = tok_nextc(tok);
+ } while (c == '0' || c == '1');
+ }
+ else {
+ int nonzero = 0;
+ /* maybe old-style octal; c is first char of it */
+ /* in any case, allow '0' as a literal */
+ while (c == '0')
+ c = tok_nextc(tok);
+ while (isdigit(c)) {
+ nonzero = 1;
+ c = tok_nextc(tok);
+ }
+ if (c == '.')
+ goto fraction;
+ else if (c == 'e' || c == 'E')
+ goto exponent;
#ifndef WITHOUT_COMPLEX
- else if (c == 'j' || c == 'J')
- goto imaginary;
+ else if (c == 'j' || c == 'J')
+ goto imaginary;
#endif
- else if (nonzero) {
- tok->done = E_TOKEN;
- tok_backup(tok, c);
- return ERRORTOKEN;
- }
- }
- }
- else {
- /* Decimal */
- do {
- c = tok_nextc(tok);
- } while (isdigit(c));
- {
- /* Accept floating point numbers. */
- if (c == '.') {
- fraction:
- /* Fraction */
- do {
- c = tok_nextc(tok);
- } while (isdigit(c));
- }
- if (c == 'e' || c == 'E') {
- exponent:
- /* Exponent part */
- c = tok_nextc(tok);
- if (c == '+' || c == '-')
- c = tok_nextc(tok);
- if (!isdigit(c)) {
- tok->done = E_TOKEN;
- tok_backup(tok, c);
- return ERRORTOKEN;
- }
- do {
- c = tok_nextc(tok);
- } while (isdigit(c));
- }
+ else if (nonzero) {
+ tok->done = E_TOKEN;
+ tok_backup(tok, c);
+ return ERRORTOKEN;
+ }
+ }
+ }
+ else {
+ /* Decimal */
+ do {
+ c = tok_nextc(tok);
+ } while (isdigit(c));
+ {
+ /* Accept floating point numbers. */
+ if (c == '.') {
+ fraction:
+ /* Fraction */
+ do {
+ c = tok_nextc(tok);
+ } while (isdigit(c));
+ }
+ if (c == 'e' || c == 'E') {
+ exponent:
+ /* Exponent part */
+ c = tok_nextc(tok);
+ if (c == '+' || c == '-')
+ c = tok_nextc(tok);
+ if (!isdigit(c)) {
+ tok->done = E_TOKEN;
+ tok_backup(tok, c);
+ return ERRORTOKEN;
+ }
+ do {
+ c = tok_nextc(tok);
+ } while (isdigit(c));
+ }
#ifndef WITHOUT_COMPLEX
- if (c == 'j' || c == 'J')
- /* Imaginary part */
- imaginary:
- c = tok_nextc(tok);
+ if (c == 'j' || c == 'J')
+ /* Imaginary part */
+ imaginary:
+ c = tok_nextc(tok);
#endif
- }
- }
- tok_backup(tok, c);
- *p_start = tok->start;
- *p_end = tok->cur;
- return NUMBER;
- }
+ }
+ }
+ tok_backup(tok, c);
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return NUMBER;
+ }
letter_quote:
- /* String */
- if (c == '\'' || c == '"') {
- int quote = c;
- int quote_size = 1; /* 1 or 3 */
- int end_quote_size = 0;
-
- /* Find the quote size and start of string */
- c = tok_nextc(tok);
- if (c == quote) {
- c = tok_nextc(tok);
- if (c == quote)
- quote_size = 3;
- else
- end_quote_size = 1; /* empty string found */
- }
- if (c != quote)
- tok_backup(tok, c);
-
- /* Get rest of string */
- while (end_quote_size != quote_size) {
- c = tok_nextc(tok);
- if (c == EOF) {
- if (quote_size == 3)
- tok->done = E_EOFS;
- else
- tok->done = E_EOLS;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- if (quote_size == 1 && c == '\n') {
- tok->done = E_EOLS;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- if (c == quote)
- end_quote_size += 1;
- else {
- end_quote_size = 0;
- if (c == '\\')
- c = tok_nextc(tok); /* skip escaped char */
- }
- }
-
- *p_start = tok->start;
- *p_end = tok->cur;
- return STRING;
- }
-
- /* Line continuation */
- if (c == '\\') {
- c = tok_nextc(tok);
- if (c != '\n') {
- tok->done = E_LINECONT;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- tok->cont_line = 1;
- goto again; /* Read next line */
- }
-
- /* Check for two-character token */
- {
- int c2 = tok_nextc(tok);
- int token = PyToken_TwoChars(c, c2);
- if (token != OP) {
- int c3 = tok_nextc(tok);
- int token3 = PyToken_ThreeChars(c, c2, c3);
- if (token3 != OP) {
- token = token3;
- } else {
- tok_backup(tok, c3);
- }
- *p_start = tok->start;
- *p_end = tok->cur;
- return token;
- }
- tok_backup(tok, c2);
- }
-
- /* Keep track of parentheses nesting level */
- switch (c) {
- case '(':
- case '[':
- case '{':
- tok->level++;
- break;
- case ')':
- case ']':
- case '}':
- tok->level--;
- break;
- }
-
- /* Punctuation character */
- *p_start = tok->start;
- *p_end = tok->cur;
- return PyToken_OneChar(c);
+ /* String */
+ if (c == '\'' || c == '"') {
+ int quote = c;
+ int quote_size = 1; /* 1 or 3 */
+ int end_quote_size = 0;
+
+ /* Find the quote size and start of string */
+ c = tok_nextc(tok);
+ if (c == quote) {
+ c = tok_nextc(tok);
+ if (c == quote)
+ quote_size = 3;
+ else
+ end_quote_size = 1; /* empty string found */
+ }
+ if (c != quote)
+ tok_backup(tok, c);
+
+ /* Get rest of string */
+ while (end_quote_size != quote_size) {
+ c = tok_nextc(tok);
+ if (c == EOF) {
+ if (quote_size == 3)
+ tok->done = E_EOFS;
+ else
+ tok->done = E_EOLS;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ if (quote_size == 1 && c == '\n') {
+ tok->done = E_EOLS;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ if (c == quote)
+ end_quote_size += 1;
+ else {
+ end_quote_size = 0;
+ if (c == '\\')
+ c = tok_nextc(tok); /* skip escaped char */
+ }
+ }
+
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return STRING;
+ }
+
+ /* Line continuation */
+ if (c == '\\') {
+ c = tok_nextc(tok);
+ if (c != '\n') {
+ tok->done = E_LINECONT;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ tok->cont_line = 1;
+ goto again; /* Read next line */
+ }
+
+ /* Check for two-character token */
+ {
+ int c2 = tok_nextc(tok);
+ int token = PyToken_TwoChars(c, c2);
+ if (token != OP) {
+ int c3 = tok_nextc(tok);
+ int token3 = PyToken_ThreeChars(c, c2, c3);
+ if (token3 != OP) {
+ token = token3;
+ } else {
+ tok_backup(tok, c3);
+ }
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return token;
+ }
+ tok_backup(tok, c2);
+ }
+
+ /* Keep track of parentheses nesting level */
+ switch (c) {
+ case '(':
+ case '[':
+ case '{':
+ tok->level++;
+ break;
+ case ')':
+ case ']':
+ case '}':
+ tok->level--;
+ break;
+ }
+
+ /* Punctuation character */
+ *p_start = tok->start;
+ *p_end = tok->cur;
+ return PyToken_OneChar(c);
}
int
PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
{
- int result = tok_get(tok, p_start, p_end);
- if (tok->decoding_erred) {
- result = ERRORTOKEN;
- tok->done = E_DECODE;
- }
- return result;
+ int result = tok_get(tok, p_start, p_end);
+ if (tok->decoding_erred) {
+ result = ERRORTOKEN;
+ tok->done = E_DECODE;
+ }
+ return result;
}
/* Get -*- encoding -*- from a Python file.
@@ -1639,34 +1639,34 @@ PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
char *
PyTokenizer_FindEncoding(int fd)
{
- struct tok_state *tok;
- FILE *fp;
- char *p_start =NULL , *p_end =NULL , *encoding = NULL;
-
- fd = dup(fd);
- if (fd < 0) {
- return NULL;
- }
- fp = fdopen(fd, "r");
- if (fp == NULL) {
- return NULL;
- }
- tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL);
- if (tok == NULL) {
- fclose(fp);
- return NULL;
- }
- while (tok->lineno < 2 && tok->done == E_OK) {
- PyTokenizer_Get(tok, &p_start, &p_end);
- }
- fclose(fp);
- if (tok->encoding) {
- encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
- if (encoding)
- strcpy(encoding, tok->encoding);
- }
- PyTokenizer_Free(tok);
- return encoding;
+ struct tok_state *tok;
+ FILE *fp;
+ char *p_start =NULL , *p_end =NULL , *encoding = NULL;
+
+ fd = dup(fd);
+ if (fd < 0) {
+ return NULL;
+ }
+ fp = fdopen(fd, "r");
+ if (fp == NULL) {
+ return NULL;
+ }
+ tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL);
+ if (tok == NULL) {
+ fclose(fp);
+ return NULL;
+ }
+ while (tok->lineno < 2 && tok->done == E_OK) {
+ PyTokenizer_Get(tok, &p_start, &p_end);
+ }
+ fclose(fp);
+ if (tok->encoding) {
+ encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
+ if (encoding)
+ strcpy(encoding, tok->encoding);
+ }
+ PyTokenizer_Free(tok);
+ return encoding;
}
#ifdef Py_DEBUG
@@ -1674,9 +1674,9 @@ PyTokenizer_FindEncoding(int fd)
void
tok_dump(int type, char *start, char *end)
{
- printf("%s", _PyParser_TokenNames[type]);
- if (type == NAME || type == NUMBER || type == STRING || type == OP)
- printf("(%.*s)", (int)(end - start), start);
+ printf("%s", _PyParser_TokenNames[type]);
+ if (type == NAME || type == NUMBER || type == STRING || type == OP)
+ printf("(%.*s)", (int)(end - start), start);
}
#endif
diff --git a/Parser/tokenizer.h b/Parser/tokenizer.h
index e3328f1..5a6d060 100644
--- a/Parser/tokenizer.h
+++ b/Parser/tokenizer.h
@@ -8,66 +8,66 @@ extern "C" {
/* Tokenizer interface */
-#include "token.h" /* For token types */
+#include "token.h" /* For token types */
-#define MAXINDENT 100 /* Max indentation level */
+#define MAXINDENT 100 /* Max indentation level */
enum decoding_state {
- STATE_INIT,
- STATE_RAW,
- STATE_NORMAL, /* have a codec associated with input */
+ STATE_INIT,
+ STATE_RAW,
+ STATE_NORMAL, /* have a codec associated with input */
};
/* Tokenizer state */
struct tok_state {
- /* Input state; buf <= cur <= inp <= end */
- /* NB an entire line is held in the buffer */
- char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL */
- char *cur; /* Next character in buffer */
- char *inp; /* End of data in buffer */
- char *end; /* End of input buffer if buf != NULL */
- char *start; /* Start of current token if not NULL */
- int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
- /* NB If done != E_OK, cur must be == inp!!! */
- FILE *fp; /* Rest of input; NULL if tokenizing a string */
- int tabsize; /* Tab spacing */
- int indent; /* Current indentation index */
- int indstack[MAXINDENT]; /* Stack of indents */
- int atbol; /* Nonzero if at begin of new line */
- int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
- char *prompt, *nextprompt; /* For interactive prompting */
- int lineno; /* Current line number */
- int level; /* () [] {} Parentheses nesting level */
- /* Used to allow free continuations inside them */
- /* Stuff for checking on different tab sizes */
- const char *filename; /* For error messages */
- int altwarning; /* Issue warning if alternate tabs don't match */
- int alterror; /* Issue error if alternate tabs don't match */
- int alttabsize; /* Alternate tab spacing */
- int altindstack[MAXINDENT]; /* Stack of alternate indents */
- /* Stuff for PEP 0263 */
- enum decoding_state decoding_state;
- int decoding_erred; /* whether erred in decoding */
- int read_coding_spec; /* whether 'coding:...' has been read */
- char *encoding; /* Source encoding. */
- int cont_line; /* whether we are in a continuation line. */
- const char* line_start; /* pointer to start of current line */
+ /* Input state; buf <= cur <= inp <= end */
+ /* NB an entire line is held in the buffer */
+ char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL */
+ char *cur; /* Next character in buffer */
+ char *inp; /* End of data in buffer */
+ char *end; /* End of input buffer if buf != NULL */
+ char *start; /* Start of current token if not NULL */
+ int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
+ /* NB If done != E_OK, cur must be == inp!!! */
+ FILE *fp; /* Rest of input; NULL if tokenizing a string */
+ int tabsize; /* Tab spacing */
+ int indent; /* Current indentation index */
+ int indstack[MAXINDENT]; /* Stack of indents */
+ int atbol; /* Nonzero if at begin of new line */
+ int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
+ char *prompt, *nextprompt; /* For interactive prompting */
+ int lineno; /* Current line number */
+ int level; /* () [] {} Parentheses nesting level */
+ /* Used to allow free continuations inside them */
+ /* Stuff for checking on different tab sizes */
+ const char *filename; /* For error messages */
+ int altwarning; /* Issue warning if alternate tabs don't match */
+ int alterror; /* Issue error if alternate tabs don't match */
+ int alttabsize; /* Alternate tab spacing */
+ int altindstack[MAXINDENT]; /* Stack of alternate indents */
+ /* Stuff for PEP 0263 */
+ enum decoding_state decoding_state;
+ int decoding_erred; /* whether erred in decoding */
+ int read_coding_spec; /* whether 'coding:...' has been read */
+ char *encoding; /* Source encoding. */
+ int cont_line; /* whether we are in a continuation line. */
+ const char* line_start; /* pointer to start of current line */
#ifndef PGEN
- PyObject *decoding_readline; /* codecs.open(...).readline */
- PyObject *decoding_buffer;
+ PyObject *decoding_readline; /* codecs.open(...).readline */
+ PyObject *decoding_buffer;
#endif
- const char* enc; /* Encoding for the current str. */
- const char* str;
+ const char* enc; /* Encoding for the current str. */
+ const char* str;
};
extern struct tok_state *PyTokenizer_FromString(const char *);
extern struct tok_state *PyTokenizer_FromUTF8(const char *);
extern struct tok_state *PyTokenizer_FromFile(FILE *, char*,
- char *, char *);
+ char *, char *);
extern void PyTokenizer_Free(struct tok_state *);
extern int PyTokenizer_Get(struct tok_state *, char **, char **);
-extern char * PyTokenizer_RestoreEncoding(struct tok_state* tok,
- int len, int *offset);
+extern char * PyTokenizer_RestoreEncoding(struct tok_state* tok,
+ int len, int *offset);
extern char * PyTokenizer_FindEncoding(int);
#ifdef __cplusplus
diff --git a/Python/_warnings.c b/Python/_warnings.c
index ef7f373..f0e1e51 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -85,10 +85,10 @@ get_default_action(void)
default_action = get_warnings_attr("defaultaction");
if (default_action == NULL) {
- if (PyErr_Occurred()) {
- return NULL;
- }
- return _default_action;
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ return _default_action;
}
Py_DECREF(_default_action);
@@ -202,12 +202,12 @@ normalize_module(PyObject *filename)
mod_str = _PyUnicode_AsString(filename);
if (mod_str == NULL)
- return NULL;
+ return NULL;
len = PyUnicode_GetSize(filename);
if (len < 0)
return NULL;
if (len >= 3 &&
- strncmp(mod_str + (len - 3), ".py", 3) == 0) {
+ strncmp(mod_str + (len - 3), ".py", 3) == 0) {
module = PyUnicode_FromStringAndSize(mod_str, len-3);
}
else {
@@ -243,15 +243,15 @@ static void
show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
*category, PyObject *sourceline)
{
- PyObject *f_stderr;
- PyObject *name;
+ PyObject *f_stderr;
+ PyObject *name;
char lineno_str[128];
PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
name = PyObject_GetAttrString(category, "__name__");
if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
- return;
+ return;
f_stderr = PySys_GetObject("stderr");
if (f_stderr == NULL) {
@@ -272,8 +272,8 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
/* Print " source_line\n" */
if (sourceline) {
char *source_line_str = _PyUnicode_AsString(sourceline);
- if (source_line_str == NULL)
- return;
+ if (source_line_str == NULL)
+ return;
while (*source_line_str == ' ' || *source_line_str == '\t' ||
*source_line_str == '\014')
source_line_str++;
@@ -284,12 +284,12 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
else
if (_Py_DisplaySourceLine(f_stderr, _PyUnicode_AsString(filename),
lineno, 2) < 0)
- return;
+ return;
PyErr_Clear();
}
static PyObject *
-warn_explicit(PyObject *category, PyObject *message,
+warn_explicit(PyObject *category, PyObject *message,
PyObject *filename, int lineno,
PyObject *module, PyObject *registry, PyObject *sourceline)
{
@@ -297,7 +297,7 @@ warn_explicit(PyObject *category, PyObject *message,
PyObject *item = Py_None;
const char *action;
int rc;
-
+
if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
return NULL;
@@ -344,7 +344,7 @@ warn_explicit(PyObject *category, PyObject *message,
rc = already_warned(registry, key, 0);
if (rc == -1)
goto cleanup;
- else if (rc == 1)
+ else if (rc == 1)
goto return_none;
/* Else this warning hasn't been generated before. */
}
@@ -374,12 +374,12 @@ warn_explicit(PyObject *category, PyObject *message,
goto cleanup;
}
/* _once_registry[(text, category)] = 1 */
- rc = update_registry(registry, text, category, 0);
+ rc = update_registry(registry, text, category, 0);
}
else if (strcmp(action, "module") == 0) {
/* registry[(text, category, 0)] = 1 */
if (registry != NULL && registry != Py_None)
- rc = update_registry(registry, text, category, 0);
+ rc = update_registry(registry, text, category, 0);
}
else if (strcmp(action, "default") != 0) {
PyObject *to_str = PyObject_Str(item);
@@ -387,9 +387,9 @@ warn_explicit(PyObject *category, PyObject *message,
if (to_str != NULL) {
err_str = _PyUnicode_AsString(to_str);
- if (err_str == NULL)
- goto cleanup;
- }
+ if (err_str == NULL)
+ goto cleanup;
+ }
PyErr_Format(PyExc_RuntimeError,
"Unrecognized action (%s) in warnings.filters:\n %s",
action, err_str);
@@ -500,7 +500,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
if (*filename != NULL) {
Py_ssize_t len = PyUnicode_GetSize(*filename);
const char *file_str = _PyUnicode_AsString(*filename);
- if (file_str == NULL || (len < 0 && PyErr_Occurred()))
+ if (file_str == NULL || (len < 0 && PyErr_Occurred()))
goto handle_error;
/* if filename.lower().endswith((".pyc", ".pyo")): */
@@ -512,16 +512,16 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
tolower(file_str[len-1]) == 'o'))
{
*filename = PyUnicode_FromStringAndSize(file_str, len-1);
- if (*filename == NULL)
- goto handle_error;
- }
- else
+ if (*filename == NULL)
+ goto handle_error;
+ }
+ else
Py_INCREF(*filename);
}
else {
const char *module_str = _PyUnicode_AsString(*module);
- if (module_str == NULL)
- goto handle_error;
+ if (module_str == NULL)
+ goto handle_error;
if (strcmp(module_str, "__main__") == 0) {
PyObject *argv = PySys_GetObject("argv");
if (argv != NULL && PyList_Size(argv) > 0) {
@@ -544,8 +544,8 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
else {
/* embedded interpreters don't have sys.argv, see bug #839151 */
*filename = PyUnicode_FromString("__main__");
- if (*filename == NULL)
- goto handle_error;
+ if (*filename == NULL)
+ goto handle_error;
}
}
if (*filename == NULL) {
@@ -616,7 +616,7 @@ warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *message, *category = NULL;
Py_ssize_t stack_level = 1;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list,
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list,
&message, &category, &stack_level))
return NULL;
@@ -794,7 +794,7 @@ static PyMethodDef warnings_functions[] = {
METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
/* XXX(brett.cannon): add showwarning? */
/* XXX(brett.cannon): Reasonable to add formatwarning? */
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
@@ -875,15 +875,15 @@ init_filters(void)
}
static struct PyModuleDef warningsmodule = {
- PyModuleDef_HEAD_INIT,
- MODULE_NAME,
- warnings__doc__,
- 0,
- warnings_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ MODULE_NAME,
+ warnings__doc__,
+ 0,
+ warnings_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
diff --git a/Python/asdl.c b/Python/asdl.c
index 1105d3a..c30d7d20 100644
--- a/Python/asdl.c
+++ b/Python/asdl.c
@@ -4,61 +4,61 @@
asdl_seq *
asdl_seq_new(int size, PyArena *arena)
{
- asdl_seq *seq = NULL;
- size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
+ asdl_seq *seq = NULL;
+ size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
- /* check size is sane */
- if (size < 0 || size == INT_MIN ||
- (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
- PyErr_NoMemory();
- return NULL;
- }
+ /* check size is sane */
+ if (size < 0 || size == INT_MIN ||
+ (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
+ PyErr_NoMemory();
+ return NULL;
+ }
- /* check if size can be added safely */
- if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
- PyErr_NoMemory();
- return NULL;
- }
+ /* check if size can be added safely */
+ if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
+ PyErr_NoMemory();
+ return NULL;
+ }
- n += sizeof(asdl_seq);
+ n += sizeof(asdl_seq);
- seq = (asdl_seq *)PyArena_Malloc(arena, n);
- if (!seq) {
- PyErr_NoMemory();
- return NULL;
- }
- memset(seq, 0, n);
- seq->size = size;
- return seq;
+ seq = (asdl_seq *)PyArena_Malloc(arena, n);
+ if (!seq) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(seq, 0, n);
+ seq->size = size;
+ return seq;
}
asdl_int_seq *
asdl_int_seq_new(int size, PyArena *arena)
{
- asdl_int_seq *seq = NULL;
- size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
+ asdl_int_seq *seq = NULL;
+ size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
- /* check size is sane */
- if (size < 0 || size == INT_MIN ||
- (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
- PyErr_NoMemory();
- return NULL;
- }
+ /* check size is sane */
+ if (size < 0 || size == INT_MIN ||
+ (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
+ PyErr_NoMemory();
+ return NULL;
+ }
- /* check if size can be added safely */
- if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
- PyErr_NoMemory();
- return NULL;
- }
+ /* check if size can be added safely */
+ if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
+ PyErr_NoMemory();
+ return NULL;
+ }
- n += sizeof(asdl_seq);
+ n += sizeof(asdl_seq);
- seq = (asdl_int_seq *)PyArena_Malloc(arena, n);
- if (!seq) {
- PyErr_NoMemory();
- return NULL;
- }
- memset(seq, 0, n);
- seq->size = size;
- return seq;
+ seq = (asdl_int_seq *)PyArena_Malloc(arena, n);
+ if (!seq) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(seq, 0, n);
+ seq->size = size;
+ return seq;
}
diff --git a/Python/ast.c b/Python/ast.c
index 2a806af..b8cb565 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -58,19 +58,19 @@ new_identifier(const char* n, PyArena *arena)
/* Check whether there are non-ASCII characters in the
identifier; if so, normalize to NFKC. */
for (; *u; u++) {
- if (*u >= 128) {
- PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
- PyObject *id2;
- if (!m)
- return NULL;
- id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id);
- Py_DECREF(m);
- if (!id2)
- return NULL;
- Py_DECREF(id);
- id = id2;
- break;
- }
+ if (*u >= 128) {
+ PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
+ PyObject *id2;
+ if (!m)
+ return NULL;
+ id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id);
+ Py_DECREF(m);
+ if (!id2)
+ return NULL;
+ Py_DECREF(id);
+ id = id2;
+ break;
+ }
}
PyUnicode_InternInPlace(&id);
PyArena_AddPyObject(arena, id);
@@ -226,7 +226,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
c.c_encoding = STR(n);
n = CHILD(n, 0);
} else {
- /* PEP 3120 */
+ /* PEP 3120 */
c.c_encoding = "utf-8";
}
c.c_arena = arena;
@@ -424,7 +424,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
s = e->v.List.elts;
break;
case Tuple_kind:
- if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
+ if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
return ast_error(n, "can't assign to ()");
e->v.Tuple.ctx = ctx;
s = e->v.Tuple.elts;
@@ -471,8 +471,8 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
expr_name = "conditional expression";
break;
default:
- PyErr_Format(PyExc_SystemError,
- "unexpected expression in assignment %d (line %d)",
+ PyErr_Format(PyExc_SystemError,
+ "unexpected expression in assignment %d (line %d)",
e->kind, e->lineno);
return 0;
}
@@ -487,7 +487,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
}
/* If the LHS is a list or tuple, we need to set the assignment
- context for all the contained elements.
+ context for all the contained elements.
*/
if (s) {
int i;
@@ -709,7 +709,7 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
}
return i;
error:
- return -1;
+ return -1;
}
/* Create AST for argument list. */
@@ -722,12 +722,12 @@ ast_for_arguments(struct compiling *c, const node *n)
parameters: '(' [typedargslist] ')'
typedargslist: ((tfpdef ['=' test] ',')*
- ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
+ ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
| '**' tfpdef)
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
tfpdef: NAME [':' test]
varargslist: ((vfpdef ['=' test] ',')*
- ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
+ ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
| '**' vfpdef)
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
vfpdef: NAME
@@ -768,7 +768,7 @@ ast_for_arguments(struct compiling *c, const node *n)
if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
if (TYPE(ch) == EQUAL) nposdefaults++;
}
- /* count the number of keyword only args &
+ /* count the number of keyword only args &
defaults for keyword only args */
for ( ; i < NCH(n); ++i) {
ch = CHILD(n, i);
@@ -782,11 +782,11 @@ ast_for_arguments(struct compiling *c, const node *n)
asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
if (!kwonlyargs && nkwonlyargs)
goto error;
- posdefaults = (nposdefaults ?
+ posdefaults = (nposdefaults ?
asdl_seq_new(nposdefaults, c->c_arena) : NULL);
if (!posdefaults && nposdefaults)
goto error;
- /* The length of kwonlyargs and kwdefaults are same
+ /* The length of kwonlyargs and kwdefaults are same
since we set NULL as default for keyword only argument w/o default
- we have sequence data structure, but no dictionary */
kwdefaults = (nkwonlyargs ?
@@ -823,7 +823,7 @@ ast_for_arguments(struct compiling *c, const node *n)
found_default = 1;
}
else if (found_default) {
- ast_error(n,
+ ast_error(n,
"non-default argument follows default argument");
goto error;
}
@@ -835,7 +835,7 @@ ast_for_arguments(struct compiling *c, const node *n)
break;
case STAR:
if (i+1 >= NCH(n)) {
- ast_error(CHILD(n, i),
+ ast_error(CHILD(n, i),
"named arguments must follow bare *");
goto error;
}
@@ -932,15 +932,15 @@ ast_for_decorator(struct compiling *c, const node *n)
/* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
expr_ty d = NULL;
expr_ty name_expr;
-
+
REQ(n, decorator);
REQ(CHILD(n, 0), AT);
REQ(RCHILD(n, -1), NEWLINE);
-
+
name_expr = ast_for_dotted_name(c, CHILD(n, 1));
if (!name_expr)
return NULL;
-
+
if (NCH(n) == 3) { /* No arguments */
d = name_expr;
name_expr = NULL;
@@ -968,12 +968,12 @@ ast_for_decorators(struct compiling *c, const node *n)
asdl_seq* decorator_seq;
expr_ty d;
int i;
-
+
REQ(n, decorators);
decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
if (!decorator_seq)
return NULL;
-
+
for (i = 0; i < NCH(n); i++) {
d = ast_for_decorator(c, CHILD(n, i));
if (!d)
@@ -1029,7 +1029,7 @@ ast_for_decorated(struct compiling *c, const node *n)
return NULL;
assert(TYPE(CHILD(n, 1)) == funcdef ||
- TYPE(CHILD(n, 1)) == classdef);
+ TYPE(CHILD(n, 1)) == classdef);
if (TYPE(CHILD(n, 1)) == funcdef) {
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
@@ -1077,7 +1077,7 @@ ast_for_lambdef(struct compiling *c, const node *n)
static expr_ty
ast_for_ifexpr(struct compiling *c, const node *n)
{
- /* test: or_test 'if' or_test 'else' test */
+ /* test: or_test 'if' or_test 'else' test */
expr_ty expression, body, orelse;
assert(NCH(n) == 5);
@@ -1174,9 +1174,9 @@ ast_for_comprehension(struct compiling *c, const node *n)
asdl_seq *t;
expr_ty expression;
node *for_ch;
-
+
REQ(n, comp_for);
-
+
for_ch = CHILD(n, 1);
t = ast_for_exprlist(c, for_ch, Store);
if (!t)
@@ -1201,7 +1201,7 @@ ast_for_comprehension(struct compiling *c, const node *n)
if (NCH(n) == 5) {
int j, n_ifs;
asdl_seq *ifs;
-
+
n = CHILD(n, 4);
n_ifs = count_comp_ifs(c, n);
if (n_ifs == -1)
@@ -1215,7 +1215,7 @@ ast_for_comprehension(struct compiling *c, const node *n)
REQ(n, comp_iter);
n = CHILD(n, 0);
REQ(n, comp_if);
-
+
expression = ast_for_expr(c, CHILD(n, 1));
if (!expression)
return NULL;
@@ -1240,13 +1240,13 @@ ast_for_itercomp(struct compiling *c, const node *n, int type)
argument: [test '='] test [comp_for] # Really [keyword '='] test */
expr_ty elt;
asdl_seq *comps;
-
+
assert(NCH(n) > 1);
-
+
elt = ast_for_expr(c, CHILD(n, 0));
if (!elt)
return NULL;
-
+
comps = ast_for_comprehension(c, CHILD(n, 1));
if (!comps)
return NULL;
@@ -1267,10 +1267,10 @@ ast_for_dictcomp(struct compiling *c, const node *n)
{
expr_ty key, value;
asdl_seq *comps;
-
+
assert(NCH(n) > 3);
REQ(CHILD(n, 1), COLON);
-
+
key = ast_for_expr(c, CHILD(n, 0));
if (!key)
return NULL;
@@ -1278,11 +1278,11 @@ ast_for_dictcomp(struct compiling *c, const node *n)
value = ast_for_expr(c, CHILD(n, 2));
if (!value)
return NULL;
-
+
comps = ast_for_comprehension(c, CHILD(n, 3));
if (!comps)
return NULL;
-
+
return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
}
@@ -1317,7 +1317,7 @@ ast_for_atom(struct compiling *c, const node *n)
*/
node *ch = CHILD(n, 0);
int bytesmode = 0;
-
+
switch (TYPE(ch)) {
case NAME: {
/* All names start in Load context, but may later be
@@ -1368,24 +1368,24 @@ ast_for_atom(struct compiling *c, const node *n)
return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
case LPAR: /* some parenthesized expressions */
ch = CHILD(n, 1);
-
+
if (TYPE(ch) == RPAR)
return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
-
+
if (TYPE(ch) == yield_expr)
return ast_for_expr(c, ch);
- /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
+ /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
return ast_for_genexp(c, ch);
-
+
return ast_for_testlist(c, ch);
case LSQB: /* list (or list comprehension) */
ch = CHILD(n, 1);
-
+
if (TYPE(ch) == RSQB)
return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
-
+
REQ(ch, testlist_comp);
if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
asdl_seq *elts = seq_for_testlist(c, ch);
@@ -1432,14 +1432,14 @@ ast_for_atom(struct compiling *c, const node *n)
keys = asdl_seq_new(size, c->c_arena);
if (!keys)
return NULL;
-
+
values = asdl_seq_new(size, c->c_arena);
if (!values)
return NULL;
-
+
for (i = 0; i < NCH(ch); i += 4) {
expr_ty expression;
-
+
expression = ast_for_expr(c, CHILD(ch, i));
if (!expression)
return NULL;
@@ -1477,10 +1477,10 @@ ast_for_slice(struct compiling *c, const node *n)
if (NCH(n) == 1 && TYPE(ch) == test) {
/* 'step' variable hold no significance in terms of being used over
other vars */
- step = ast_for_expr(c, ch);
+ step = ast_for_expr(c, ch);
if (!step)
return NULL;
-
+
return Index(step, c->c_arena);
}
@@ -1537,7 +1537,7 @@ static expr_ty
ast_for_binop(struct compiling *c, const node *n)
{
/* Must account for a sequence of expressions.
- How should A op B op C by represented?
+ How should A op B op C by represented?
BinOp(BinOp(A, op, B), op, C).
*/
@@ -1575,10 +1575,10 @@ ast_for_binop(struct compiling *c, const node *n)
if (!tmp)
return NULL;
- tmp_result = BinOp(result, newoperator, tmp,
+ tmp_result = BinOp(result, newoperator, tmp,
LINENO(next_oper), next_oper->n_col_offset,
c->c_arena);
- if (!tmp_result)
+ if (!tmp_result)
return NULL;
result = tmp_result;
}
@@ -1588,7 +1588,7 @@ ast_for_binop(struct compiling *c, const node *n)
static expr_ty
ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
{
- /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
+ /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
*/
@@ -1619,7 +1619,7 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
c->c_arena);
}
else {
- /* The grammar is ambiguous here. The ambiguity is resolved
+ /* The grammar is ambiguous here. The ambiguity is resolved
by treating the sequence as a tuple literal if there are
no slice features.
*/
@@ -1772,7 +1772,7 @@ ast_for_expr(struct compiling *c, const node *n)
/* handle the full range of simple expressions
test: or_test ['if' or_test 'else' test] | lambdef
test_nocond: or_test | lambdef_nocond
- or_test: and_test ('or' and_test)*
+ or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
@@ -1860,7 +1860,7 @@ ast_for_expr(struct compiling *c, const node *n)
if (!expression) {
return NULL;
}
-
+
asdl_seq_SET(ops, i / 2, newoperator);
asdl_seq_SET(cmps, i / 2, expression);
}
@@ -1868,7 +1868,7 @@ ast_for_expr(struct compiling *c, const node *n)
if (!expression) {
return NULL;
}
-
+
return Compare(expression, ops, cmps, LINENO(n),
n->n_col_offset, c->c_arena);
}
@@ -1987,7 +1987,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
if (!e)
return NULL;
asdl_seq_SET(args, nargs++, e);
- }
+ }
else if (TYPE(CHILD(ch, 1)) == comp_for) {
e = ast_for_genexp(c, ch);
if (!e)
@@ -1999,7 +1999,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
identifier key, tmp;
int k;
- /* CHILD(ch, 0) is test, but must be an identifier? */
+ /* CHILD(ch, 0) is test, but must be an identifier? */
e = ast_for_expr(c, CHILD(ch, 0));
if (!e)
return NULL;
@@ -2015,8 +2015,8 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func)
ast_error(CHILD(ch, 0), "keyword can't be an expression");
return NULL;
} else if (forbidden_name(e, ch)) {
- return NULL;
- }
+ return NULL;
+ }
key = e->v.Name.id;
for (k = 0; k < nkeywords; k++) {
tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
@@ -2080,7 +2080,7 @@ static stmt_ty
ast_for_expr_stmt(struct compiling *c, const node *n)
{
REQ(n, expr_stmt);
- /* expr_stmt: testlist (augassign (yield_expr|testlist)
+ /* expr_stmt: testlist (augassign (yield_expr|testlist)
| ('=' (yield_expr|testlist))*)
testlist: test (',' test)* [',']
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
@@ -2154,7 +2154,7 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
e = ast_for_testlist(c, ch);
/* set context to assign */
- if (!e)
+ if (!e)
return NULL;
if (!set_context(c, e, Store, CHILD(n, i)))
@@ -2200,7 +2200,7 @@ static stmt_ty
ast_for_del_stmt(struct compiling *c, const node *n)
{
asdl_seq *expr_list;
-
+
/* del_stmt: 'del' exprlist */
REQ(n, del_stmt);
@@ -2323,7 +2323,7 @@ alias_for_import_name(struct compiling *c, const node *n)
int i;
size_t len;
char *s;
- PyObject *uni;
+ PyObject *uni;
len = 0;
for (i = 0; i < NCH(n); i += 2)
@@ -2344,13 +2344,13 @@ alias_for_import_name(struct compiling *c, const node *n)
}
--s;
*s = '\0';
- uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
- PyBytes_GET_SIZE(str),
- NULL);
- Py_DECREF(str);
- if (!uni)
- return NULL;
- str = uni;
+ uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
+ PyBytes_GET_SIZE(str),
+ NULL);
+ Py_DECREF(str);
+ if (!uni)
+ return NULL;
+ str = uni;
PyUnicode_InternInPlace(&str);
PyArena_AddPyObject(c->c_arena, str);
return alias(str, NULL, c->c_arena);
@@ -2407,7 +2407,7 @@ ast_for_import_stmt(struct compiling *c, const node *n)
int idx, ndots = 0;
alias_ty mod = NULL;
identifier modname;
-
+
/* Count the number of dots (for relative imports) and check for the
optional module name */
for (idx = 1; idx < NCH(n); idx++) {
@@ -2416,7 +2416,7 @@ ast_for_import_stmt(struct compiling *c, const node *n)
idx++;
break;
} else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
- /* three consecutive dots are tokenized as one ELLIPSIS */
+ /* three consecutive dots are tokenized as one ELLIPSIS */
ndots += 3;
continue;
} else if (TYPE(CHILD(n, idx)) != DOT) {
@@ -2545,7 +2545,7 @@ ast_for_assert_stmt(struct compiling *c, const node *n)
expr2 = ast_for_expr(c, CHILD(n, 3));
if (!expr2)
return NULL;
-
+
return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
}
PyErr_Format(PyExc_SystemError,
@@ -2572,7 +2572,7 @@ ast_for_suite(struct compiling *c, const node *n)
if (TYPE(CHILD(n, 0)) == simple_stmt) {
n = CHILD(n, 0);
/* simple_stmt always ends with a NEWLINE,
- and may have a trailing SEMI
+ and may have a trailing SEMI
*/
end = NCH(n) - 1;
if (TYPE(CHILD(n, end - 1)) == SEMI)
@@ -2637,10 +2637,10 @@ ast_for_if_stmt(struct compiling *c, const node *n)
expression = ast_for_expr(c, CHILD(n, 1));
if (!expression)
return NULL;
- suite_seq = ast_for_suite(c, CHILD(n, 3));
+ suite_seq = ast_for_suite(c, CHILD(n, 3));
if (!suite_seq)
return NULL;
-
+
return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
c->c_arena);
}
@@ -2698,8 +2698,8 @@ ast_for_if_stmt(struct compiling *c, const node *n)
if (!suite_seq2)
return NULL;
- asdl_seq_SET(orelse, 0,
- If(expression, suite_seq, suite_seq2,
+ asdl_seq_SET(orelse, 0,
+ If(expression, suite_seq, suite_seq2,
LINENO(CHILD(n, NCH(n) - 6)),
CHILD(n, NCH(n) - 6)->n_col_offset,
c->c_arena));
@@ -2720,7 +2720,7 @@ ast_for_if_stmt(struct compiling *c, const node *n)
return NULL;
asdl_seq_SET(newobj, 0,
- If(expression, suite_seq, orelse,
+ If(expression, suite_seq, orelse,
LINENO(CHILD(n, off)),
CHILD(n, off)->n_col_offset, c->c_arena));
orelse = newobj;
@@ -2914,7 +2914,7 @@ ast_for_try_stmt(struct compiling *c, const node *n)
ast_error(n, "malformed 'try' statement");
return NULL;
}
-
+
if (n_except > 0) {
int i;
stmt_ty except_st;
@@ -3125,8 +3125,8 @@ ast_for_stmt(struct compiling *c, const node *n)
return ast_for_funcdef(c, ch, NULL);
case classdef:
return ast_for_classdef(c, ch, NULL);
- case decorated:
- return ast_for_decorated(c, ch);
+ case decorated:
+ return ast_for_decorated(c, ch);
default:
PyErr_Format(PyExc_SystemError,
"unhandled small_stmt: TYPE=%d NCH=%d\n",
@@ -3288,7 +3288,7 @@ parsestr(struct compiling *c, const node *n, int *bytesmode)
if (quote == 'b' || quote == 'B') {
quote = *++s;
*bytesmode = 1;
- }
+ }
if (quote == 'r' || quote == 'R') {
quote = *++s;
rawmode = 1;
@@ -3301,7 +3301,7 @@ parsestr(struct compiling *c, const node *n, int *bytesmode)
s++;
len = strlen(s);
if (len > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
+ PyErr_SetString(PyExc_OverflowError,
"string to parse is too long");
return NULL;
}
@@ -3345,7 +3345,7 @@ parsestr(struct compiling *c, const node *n, int *bytesmode)
return PyBytes_FromStringAndSize(s, len);
} else if (strcmp(c->c_encoding, "utf-8") == 0) {
return PyUnicode_FromStringAndSize(s, len);
- } else {
+ } else {
return PyUnicode_DecodeLatin1(s, len, NULL);
}
}
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 10527c1..cd85156 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -29,138 +29,138 @@ int Py_HasFileSystemDefaultEncoding = 0;
int
_Py_SetFileSystemEncoding(PyObject *s)
{
- PyObject *defenc, *codec;
- if (!PyUnicode_Check(s)) {
- PyErr_BadInternalCall();
- return -1;
- }
- defenc = _PyUnicode_AsDefaultEncodedString(s, NULL);
- if (!defenc)
- return -1;
- codec = _PyCodec_Lookup(PyBytes_AsString(defenc));
- if (codec == NULL)
- return -1;
- Py_DECREF(codec);
- if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding)
- /* A file system encoding was set at run-time */
- free((char*)Py_FileSystemDefaultEncoding);
- Py_FileSystemDefaultEncoding = strdup(PyBytes_AsString(defenc));
- Py_HasFileSystemDefaultEncoding = 0;
- return 0;
+ PyObject *defenc, *codec;
+ if (!PyUnicode_Check(s)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ defenc = _PyUnicode_AsDefaultEncodedString(s, NULL);
+ if (!defenc)
+ return -1;
+ codec = _PyCodec_Lookup(PyBytes_AsString(defenc));
+ if (codec == NULL)
+ return -1;
+ Py_DECREF(codec);
+ if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding)
+ /* A file system encoding was set at run-time */
+ free((char*)Py_FileSystemDefaultEncoding);
+ Py_FileSystemDefaultEncoding = strdup(PyBytes_AsString(defenc));
+ Py_HasFileSystemDefaultEncoding = 0;
+ return 0;
}
static PyObject *
builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
- PyObject *cls = NULL;
- Py_ssize_t nargs, nbases;
-
- assert(args != NULL);
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: args is not a tuple");
- return NULL;
- }
- nargs = PyTuple_GET_SIZE(args);
- if (nargs < 2) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: not enough arguments");
- return NULL;
- }
- func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
- name = PyTuple_GET_ITEM(args, 1);
- if (!PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: name is not a string");
- return NULL;
- }
- bases = PyTuple_GetSlice(args, 2, nargs);
- if (bases == NULL)
- return NULL;
- nbases = nargs - 2;
-
- if (kwds == NULL) {
- meta = NULL;
- mkw = NULL;
+ PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
+ PyObject *cls = NULL;
+ Py_ssize_t nargs, nbases;
+
+ assert(args != NULL);
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__build_class__: args is not a tuple");
+ return NULL;
+ }
+ nargs = PyTuple_GET_SIZE(args);
+ if (nargs < 2) {
+ PyErr_SetString(PyExc_TypeError,
+ "__build_class__: not enough arguments");
+ return NULL;
+ }
+ func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
+ name = PyTuple_GET_ITEM(args, 1);
+ if (!PyUnicode_Check(name)) {
+ PyErr_SetString(PyExc_TypeError,
+ "__build_class__: name is not a string");
+ return NULL;
+ }
+ bases = PyTuple_GetSlice(args, 2, nargs);
+ if (bases == NULL)
+ return NULL;
+ nbases = nargs - 2;
+
+ if (kwds == NULL) {
+ meta = NULL;
+ mkw = NULL;
+ }
+ else {
+ mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
+ if (mkw == NULL) {
+ Py_DECREF(bases);
+ return NULL;
+ }
+ meta = PyDict_GetItemString(mkw, "metaclass");
+ if (meta != NULL) {
+ Py_INCREF(meta);
+ if (PyDict_DelItemString(mkw, "metaclass") < 0) {
+ Py_DECREF(meta);
+ Py_DECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
+ }
+ }
+ }
+ if (meta == NULL) {
+ if (PyTuple_GET_SIZE(bases) == 0)
+ meta = (PyObject *) (&PyType_Type);
+ else {
+ PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
+ meta = (PyObject *) (base0->ob_type);
+ }
+ Py_INCREF(meta);
+ }
+ prep = PyObject_GetAttrString(meta, "__prepare__");
+ if (prep == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Clear();
+ ns = PyDict_New();
}
- else {
- mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */
- if (mkw == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
- meta = PyDict_GetItemString(mkw, "metaclass");
- if (meta != NULL) {
- Py_INCREF(meta);
- if (PyDict_DelItemString(mkw, "metaclass") < 0) {
- Py_DECREF(meta);
- Py_DECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- }
- }
- if (meta == NULL) {
- if (PyTuple_GET_SIZE(bases) == 0)
- meta = (PyObject *) (&PyType_Type);
- else {
- PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
- meta = (PyObject *) (base0->ob_type);
- }
- Py_INCREF(meta);
- }
- prep = PyObject_GetAttrString(meta, "__prepare__");
- if (prep == NULL) {
- if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_Clear();
- ns = PyDict_New();
- }
- else {
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- }
- else {
- PyObject *pargs = PyTuple_Pack(2, name, bases);
- if (pargs == NULL) {
- Py_DECREF(prep);
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
- Py_DECREF(pargs);
- Py_DECREF(prep);
- }
- if (ns == NULL) {
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
- if (cell != NULL) {
- PyObject *margs;
- margs = PyTuple_Pack(3, name, bases, ns);
- if (margs != NULL) {
- cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
- Py_DECREF(margs);
- }
- if (cls != NULL && PyCell_Check(cell)) {
- Py_INCREF(cls);
- PyCell_SET(cell, cls);
- }
- Py_DECREF(cell);
- }
- Py_DECREF(ns);
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return cls;
+ else {
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
+ }
+ }
+ else {
+ PyObject *pargs = PyTuple_Pack(2, name, bases);
+ if (pargs == NULL) {
+ Py_DECREF(prep);
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
+ }
+ ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
+ Py_DECREF(pargs);
+ Py_DECREF(prep);
+ }
+ if (ns == NULL) {
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
+ }
+ cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
+ if (cell != NULL) {
+ PyObject *margs;
+ margs = PyTuple_Pack(3, name, bases, ns);
+ if (margs != NULL) {
+ cls = PyEval_CallObjectWithKeywords(meta, margs, mkw);
+ Py_DECREF(margs);
+ }
+ if (cls != NULL && PyCell_Check(cell)) {
+ Py_INCREF(cls);
+ PyCell_SET(cell, cls);
+ }
+ Py_DECREF(cell);
+ }
+ Py_DECREF(ns);
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return cls;
}
PyDoc_STRVAR(build_class_doc,
@@ -171,19 +171,19 @@ Internal helper function used by the class statement.");
static PyObject *
builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"name", "globals", "locals", "fromlist",
- "level", 0};
- char *name;
- PyObject *globals = NULL;
- PyObject *locals = NULL;
- PyObject *fromlist = NULL;
- int level = -1;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
- kwlist, &name, &globals, &locals, &fromlist, &level))
- return NULL;
- return PyImport_ImportModuleLevel(name, globals, locals,
- fromlist, level);
+ static char *kwlist[] = {"name", "globals", "locals", "fromlist",
+ "level", 0};
+ char *name;
+ PyObject *globals = NULL;
+ PyObject *locals = NULL;
+ PyObject *fromlist = NULL;
+ int level = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
+ kwlist, &name, &globals, &locals, &fromlist, &level))
+ return NULL;
+ return PyImport_ImportModuleLevel(name, globals, locals,
+ fromlist, level);
}
PyDoc_STRVAR(import_doc,
@@ -204,7 +204,7 @@ is the number of parent directories to search relative to the current module.");
static PyObject *
builtin_abs(PyObject *self, PyObject *v)
{
- return PyNumber_Absolute(v);
+ return PyNumber_Absolute(v);
}
PyDoc_STRVAR(abs_doc,
@@ -215,38 +215,38 @@ Return the absolute value of the argument.");
static PyObject *
builtin_all(PyObject *self, PyObject *v)
{
- PyObject *it, *item;
- PyObject *(*iternext)(PyObject *);
- int cmp;
-
- it = PyObject_GetIter(v);
- if (it == NULL)
- return NULL;
- iternext = *Py_TYPE(it)->tp_iternext;
-
- for (;;) {
- item = iternext(it);
- if (item == NULL)
- break;
- cmp = PyObject_IsTrue(item);
- Py_DECREF(item);
- if (cmp < 0) {
- Py_DECREF(it);
- return NULL;
- }
- if (cmp == 0) {
- Py_DECREF(it);
- Py_RETURN_FALSE;
- }
- }
- Py_DECREF(it);
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- return NULL;
- }
- Py_RETURN_TRUE;
+ PyObject *it, *item;
+ PyObject *(*iternext)(PyObject *);
+ int cmp;
+
+ it = PyObject_GetIter(v);
+ if (it == NULL)
+ return NULL;
+ iternext = *Py_TYPE(it)->tp_iternext;
+
+ for (;;) {
+ item = iternext(it);
+ if (item == NULL)
+ break;
+ cmp = PyObject_IsTrue(item);
+ Py_DECREF(item);
+ if (cmp < 0) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ if (cmp == 0) {
+ Py_DECREF(it);
+ Py_RETURN_FALSE;
+ }
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ Py_RETURN_TRUE;
}
PyDoc_STRVAR(all_doc,
@@ -257,38 +257,38 @@ Return True if bool(x) is True for all values x in the iterable.");
static PyObject *
builtin_any(PyObject *self, PyObject *v)
{
- PyObject *it, *item;
- PyObject *(*iternext)(PyObject *);
- int cmp;
-
- it = PyObject_GetIter(v);
- if (it == NULL)
- return NULL;
- iternext = *Py_TYPE(it)->tp_iternext;
-
- for (;;) {
- item = iternext(it);
- if (item == NULL)
- break;
- cmp = PyObject_IsTrue(item);
- Py_DECREF(item);
- if (cmp < 0) {
- Py_DECREF(it);
- return NULL;
- }
- if (cmp == 1) {
- Py_DECREF(it);
- Py_RETURN_TRUE;
- }
- }
- Py_DECREF(it);
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- return NULL;
- }
- Py_RETURN_FALSE;
+ PyObject *it, *item;
+ PyObject *(*iternext)(PyObject *);
+ int cmp;
+
+ it = PyObject_GetIter(v);
+ if (it == NULL)
+ return NULL;
+ iternext = *Py_TYPE(it)->tp_iternext;
+
+ for (;;) {
+ item = iternext(it);
+ if (item == NULL)
+ break;
+ cmp = PyObject_IsTrue(item);
+ Py_DECREF(item);
+ if (cmp < 0) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ if (cmp == 1) {
+ Py_DECREF(it);
+ Py_RETURN_TRUE;
+ }
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else
+ return NULL;
+ }
+ Py_RETURN_FALSE;
}
PyDoc_STRVAR(any_doc,
@@ -299,7 +299,7 @@ Return True if bool(x) is True for any x in the iterable.");
static PyObject *
builtin_ascii(PyObject *self, PyObject *v)
{
- return PyObject_ASCII(v);
+ return PyObject_ASCII(v);
}
PyDoc_STRVAR(ascii_doc,
@@ -314,7 +314,7 @@ to that returned by repr() in Python 2.");
static PyObject *
builtin_bin(PyObject *self, PyObject *v)
{
- return PyNumber_ToBase(v, 2);
+ return PyNumber_ToBase(v, 2);
}
PyDoc_STRVAR(bin_doc,
@@ -324,90 +324,90 @@ Return the binary representation of an integer or long integer.");
typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
+ PyObject_HEAD
+ PyObject *func;
+ PyObject *it;
} filterobject;
static PyObject *
filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *func, *seq;
- PyObject *it;
- filterobject *lz;
-
- if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter()", kwds))
- return NULL;
-
- if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
- return NULL;
-
- /* Get iterator. */
- it = PyObject_GetIter(seq);
- if (it == NULL)
- return NULL;
-
- /* create filterobject structure */
- lz = (filterobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
-
- return (PyObject *)lz;
+ PyObject *func, *seq;
+ PyObject *it;
+ filterobject *lz;
+
+ if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter()", kwds))
+ return NULL;
+
+ if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
+ return NULL;
+
+ /* Get iterator. */
+ it = PyObject_GetIter(seq);
+ if (it == NULL)
+ return NULL;
+
+ /* create filterobject structure */
+ lz = (filterobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_INCREF(func);
+ lz->func = func;
+ lz->it = it;
+
+ return (PyObject *)lz;
}
static void
filter_dealloc(filterobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->func);
+ Py_XDECREF(lz->it);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
filter_traverse(filterobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->it);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
filter_next(filterobject *lz)
{
- PyObject *item;
- PyObject *it = lz->it;
- long ok;
- PyObject *(*iternext)(PyObject *);
-
- iternext = *Py_TYPE(it)->tp_iternext;
- for (;;) {
- item = iternext(it);
- if (item == NULL)
- return NULL;
-
- if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
- ok = PyObject_IsTrue(item);
- } else {
- PyObject *good;
- good = PyObject_CallFunctionObjArgs(lz->func,
- item, NULL);
- if (good == NULL) {
- Py_DECREF(item);
- return NULL;
- }
- ok = PyObject_IsTrue(good);
- Py_DECREF(good);
- }
- if (ok)
- return item;
- Py_DECREF(item);
- }
+ PyObject *item;
+ PyObject *it = lz->it;
+ long ok;
+ PyObject *(*iternext)(PyObject *);
+
+ iternext = *Py_TYPE(it)->tp_iternext;
+ for (;;) {
+ item = iternext(it);
+ if (item == NULL)
+ return NULL;
+
+ if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) {
+ ok = PyObject_IsTrue(item);
+ } else {
+ PyObject *good;
+ good = PyObject_CallFunctionObjArgs(lz->func,
+ item, NULL);
+ if (good == NULL) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ }
+ if (ok)
+ return item;
+ Py_DECREF(item);
+ }
}
PyDoc_STRVAR(filter_doc,
@@ -417,47 +417,47 @@ Return an iterator yielding those items of iterable for which function(item)\n\
is true. If function is None, return the items that are true.");
PyTypeObject PyFilter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "filter", /* tp_name */
- sizeof(filterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)filter_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- filter_doc, /* tp_doc */
- (traverseproc)filter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)filter_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- filter_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "filter", /* tp_name */
+ sizeof(filterobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)filter_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ filter_doc, /* tp_doc */
+ (traverseproc)filter_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)filter_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ filter_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -468,7 +468,7 @@ builtin_format(PyObject *self, PyObject *args)
PyObject *format_spec = NULL;
if (!PyArg_ParseTuple(args, "O|U:format", &value, &format_spec))
- return NULL;
+ return NULL;
return PyObject_Format(value, format_spec);
}
@@ -482,12 +482,12 @@ format_spec defaults to \"\"");
static PyObject *
builtin_chr(PyObject *self, PyObject *args)
{
- int x;
+ int x;
- if (!PyArg_ParseTuple(args, "i:chr", &x))
- return NULL;
+ if (!PyArg_ParseTuple(args, "i:chr", &x))
+ return NULL;
- return PyUnicode_FromOrdinal(x);
+ return PyUnicode_FromOrdinal(x);
}
PyDoc_VAR(chr_doc) = PyDoc_STR(
@@ -506,111 +506,111 @@ PyDoc_STR(
static char *
source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)
{
- char *str;
- Py_ssize_t size;
-
- if (PyUnicode_Check(cmd)) {
- cf->cf_flags |= PyCF_IGNORE_COOKIE;
- cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
- if (cmd == NULL)
- return NULL;
- }
- else if (!PyObject_CheckReadBuffer(cmd)) {
- PyErr_Format(PyExc_TypeError,
- "%s() arg 1 must be a %s object",
- funcname, what);
- return NULL;
- }
- if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
- return NULL;
- }
- if (strlen(str) != size) {
- PyErr_SetString(PyExc_TypeError,
- "source code string cannot contain null bytes");
- return NULL;
- }
- return str;
+ char *str;
+ Py_ssize_t size;
+
+ if (PyUnicode_Check(cmd)) {
+ cf->cf_flags |= PyCF_IGNORE_COOKIE;
+ cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
+ if (cmd == NULL)
+ return NULL;
+ }
+ else if (!PyObject_CheckReadBuffer(cmd)) {
+ PyErr_Format(PyExc_TypeError,
+ "%s() arg 1 must be a %s object",
+ funcname, what);
+ return NULL;
+ }
+ if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) {
+ return NULL;
+ }
+ if (strlen(str) != size) {
+ PyErr_SetString(PyExc_TypeError,
+ "source code string cannot contain null bytes");
+ return NULL;
+ }
+ return str;
}
static PyObject *
builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
{
- char *str;
- char *filename;
- char *startstr;
- int mode = -1;
- int dont_inherit = 0;
- int supplied_flags = 0;
- int is_ast;
- PyCompilerFlags cf;
- PyObject *cmd;
- static char *kwlist[] = {"source", "filename", "mode", "flags",
- "dont_inherit", NULL};
- int start[] = {Py_file_input, Py_eval_input, Py_single_input};
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
- kwlist, &cmd, &filename, &startstr,
- &supplied_flags, &dont_inherit))
- return NULL;
-
- cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
-
- if (supplied_flags &
- ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
- {
- PyErr_SetString(PyExc_ValueError,
- "compile(): unrecognised flags");
- return NULL;
- }
- /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
-
- if (!dont_inherit) {
- PyEval_MergeCompilerFlags(&cf);
- }
-
- if (strcmp(startstr, "exec") == 0)
- mode = 0;
- else if (strcmp(startstr, "eval") == 0)
- mode = 1;
- else if (strcmp(startstr, "single") == 0)
- mode = 2;
- else {
- PyErr_SetString(PyExc_ValueError,
- "compile() arg 3 must be 'exec', 'eval' or 'single'");
- return NULL;
- }
-
- is_ast = PyAST_Check(cmd);
- if (is_ast == -1)
- return NULL;
- if (is_ast) {
- PyObject *result;
- if (supplied_flags & PyCF_ONLY_AST) {
- Py_INCREF(cmd);
- result = cmd;
- }
- else {
- PyArena *arena;
- mod_ty mod;
-
- arena = PyArena_New();
- mod = PyAST_obj2mod(cmd, arena, mode);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
- result = (PyObject*)PyAST_Compile(mod, filename,
- &cf, arena);
- PyArena_Free(arena);
- }
- return result;
- }
-
- str = source_as_string(cmd, "compile", "string, bytes, AST or code", &cf);
- if (str == NULL)
- return NULL;
-
- return Py_CompileStringFlags(str, filename, start[mode], &cf);
+ char *str;
+ char *filename;
+ char *startstr;
+ int mode = -1;
+ int dont_inherit = 0;
+ int supplied_flags = 0;
+ int is_ast;
+ PyCompilerFlags cf;
+ PyObject *cmd;
+ static char *kwlist[] = {"source", "filename", "mode", "flags",
+ "dont_inherit", NULL};
+ int start[] = {Py_file_input, Py_eval_input, Py_single_input};
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
+ kwlist, &cmd, &filename, &startstr,
+ &supplied_flags, &dont_inherit))
+ return NULL;
+
+ cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8;
+
+ if (supplied_flags &
+ ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
+ {
+ PyErr_SetString(PyExc_ValueError,
+ "compile(): unrecognised flags");
+ return NULL;
+ }
+ /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
+
+ if (!dont_inherit) {
+ PyEval_MergeCompilerFlags(&cf);
+ }
+
+ if (strcmp(startstr, "exec") == 0)
+ mode = 0;
+ else if (strcmp(startstr, "eval") == 0)
+ mode = 1;
+ else if (strcmp(startstr, "single") == 0)
+ mode = 2;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "compile() arg 3 must be 'exec', 'eval' or 'single'");
+ return NULL;
+ }
+
+ is_ast = PyAST_Check(cmd);
+ if (is_ast == -1)
+ return NULL;
+ if (is_ast) {
+ PyObject *result;
+ if (supplied_flags & PyCF_ONLY_AST) {
+ Py_INCREF(cmd);
+ result = cmd;
+ }
+ else {
+ PyArena *arena;
+ mod_ty mod;
+
+ arena = PyArena_New();
+ mod = PyAST_obj2mod(cmd, arena, mode);
+ if (mod == NULL) {
+ PyArena_Free(arena);
+ return NULL;
+ }
+ result = (PyObject*)PyAST_Compile(mod, filename,
+ &cf, arena);
+ PyArena_Free(arena);
+ }
+ return result;
+ }
+
+ str = source_as_string(cmd, "compile", "string, bytes, AST or code", &cf);
+ if (str == NULL)
+ return NULL;
+
+ return Py_CompileStringFlags(str, filename, start[mode], &cf);
}
PyDoc_STRVAR(compile_doc,
@@ -631,11 +631,11 @@ in addition to any features explicitly specified.");
static PyObject *
builtin_dir(PyObject *self, PyObject *args)
{
- PyObject *arg = NULL;
+ PyObject *arg = NULL;
- if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
- return NULL;
- return PyObject_Dir(arg);
+ if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
+ return NULL;
+ return PyObject_Dir(arg);
}
PyDoc_STRVAR(dir_doc,
@@ -655,11 +655,11 @@ PyDoc_STRVAR(dir_doc,
static PyObject *
builtin_divmod(PyObject *self, PyObject *args)
{
- PyObject *v, *w;
+ PyObject *v, *w;
- if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
- return NULL;
- return PyNumber_Divmod(v, w);
+ if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
+ return NULL;
+ return PyNumber_Divmod(v, w);
}
PyDoc_STRVAR(divmod_doc,
@@ -671,65 +671,65 @@ Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
static PyObject *
builtin_eval(PyObject *self, PyObject *args)
{
- PyObject *cmd, *result, *tmp = NULL;
- PyObject *globals = Py_None, *locals = Py_None;
- char *str;
- PyCompilerFlags cf;
-
- if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
- return NULL;
- if (locals != Py_None && !PyMapping_Check(locals)) {
- PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
- return NULL;
- }
- if (globals != Py_None && !PyDict_Check(globals)) {
- PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
- "globals must be a real dict; try eval(expr, {}, mapping)"
- : "globals must be a dict");
- return NULL;
- }
- if (globals == Py_None) {
- globals = PyEval_GetGlobals();
- if (locals == Py_None)
- locals = PyEval_GetLocals();
- }
- else if (locals == Py_None)
- locals = globals;
-
- if (globals == NULL || locals == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "eval must be given globals and locals "
- "when called without a frame");
- return NULL;
- }
-
- if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
- if (PyDict_SetItemString(globals, "__builtins__",
- PyEval_GetBuiltins()) != 0)
- return NULL;
- }
-
- if (PyCode_Check(cmd)) {
- if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
- PyErr_SetString(PyExc_TypeError,
- "code object passed to eval() may not contain free variables");
- return NULL;
- }
- return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
- }
-
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
- str = source_as_string(cmd, "eval", "string, bytes or code", &cf);
- if (str == NULL)
- return NULL;
-
- while (*str == ' ' || *str == '\t')
- str++;
-
- (void)PyEval_MergeCompilerFlags(&cf);
- result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
- Py_XDECREF(tmp);
- return result;
+ PyObject *cmd, *result, *tmp = NULL;
+ PyObject *globals = Py_None, *locals = Py_None;
+ char *str;
+ PyCompilerFlags cf;
+
+ if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
+ return NULL;
+ if (locals != Py_None && !PyMapping_Check(locals)) {
+ PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
+ return NULL;
+ }
+ if (globals != Py_None && !PyDict_Check(globals)) {
+ PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
+ "globals must be a real dict; try eval(expr, {}, mapping)"
+ : "globals must be a dict");
+ return NULL;
+ }
+ if (globals == Py_None) {
+ globals = PyEval_GetGlobals();
+ if (locals == Py_None)
+ locals = PyEval_GetLocals();
+ }
+ else if (locals == Py_None)
+ locals = globals;
+
+ if (globals == NULL || locals == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "eval must be given globals and locals "
+ "when called without a frame");
+ return NULL;
+ }
+
+ if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
+ return NULL;
+ }
+
+ if (PyCode_Check(cmd)) {
+ if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "code object passed to eval() may not contain free variables");
+ return NULL;
+ }
+ return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
+ }
+
+ cf.cf_flags = PyCF_SOURCE_IS_UTF8;
+ str = source_as_string(cmd, "eval", "string, bytes or code", &cf);
+ if (str == NULL)
+ return NULL;
+
+ while (*str == ' ' || *str == '\t')
+ str++;
+
+ (void)PyEval_MergeCompilerFlags(&cf);
+ result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
+ Py_XDECREF(tmp);
+ return result;
}
PyDoc_STRVAR(eval_doc,
@@ -745,72 +745,72 @@ If only globals is given, locals defaults to it.\n");
static PyObject *
builtin_exec(PyObject *self, PyObject *args)
{
- PyObject *v;
- PyObject *prog, *globals = Py_None, *locals = Py_None;
- int plain = 0;
-
- if (!PyArg_UnpackTuple(args, "exec", 1, 3, &prog, &globals, &locals))
- return NULL;
-
- if (globals == Py_None) {
- globals = PyEval_GetGlobals();
- if (locals == Py_None) {
- locals = PyEval_GetLocals();
- plain = 1;
- }
- if (!globals || !locals) {
- PyErr_SetString(PyExc_SystemError,
- "globals and locals cannot be NULL");
- return NULL;
- }
- }
- else if (locals == Py_None)
- locals = globals;
-
- if (!PyDict_Check(globals)) {
- PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
- globals->ob_type->tp_name);
- return NULL;
- }
- if (!PyMapping_Check(locals)) {
- PyErr_Format(PyExc_TypeError,
- "arg 3 must be a mapping or None, not %.100s",
- locals->ob_type->tp_name);
- return NULL;
- }
- if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
- if (PyDict_SetItemString(globals, "__builtins__",
- PyEval_GetBuiltins()) != 0)
- return NULL;
- }
-
- if (PyCode_Check(prog)) {
- if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
- PyErr_SetString(PyExc_TypeError,
- "code object passed to exec() may not "
- "contain free variables");
- return NULL;
- }
- v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
- }
- else {
- char *str;
- PyCompilerFlags cf;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
- str = source_as_string(prog, "exec",
- "string, bytes or code", &cf);
- if (str == NULL)
- return NULL;
- if (PyEval_MergeCompilerFlags(&cf))
- v = PyRun_StringFlags(str, Py_file_input, globals,
- locals, &cf);
- else
- v = PyRun_String(str, Py_file_input, globals, locals);
- }
- if (v == NULL)
- return NULL;
- Py_DECREF(v);
- Py_RETURN_NONE;
+ PyObject *v;
+ PyObject *prog, *globals = Py_None, *locals = Py_None;
+ int plain = 0;
+
+ if (!PyArg_UnpackTuple(args, "exec", 1, 3, &prog, &globals, &locals))
+ return NULL;
+
+ if (globals == Py_None) {
+ globals = PyEval_GetGlobals();
+ if (locals == Py_None) {
+ locals = PyEval_GetLocals();
+ plain = 1;
+ }
+ if (!globals || !locals) {
+ PyErr_SetString(PyExc_SystemError,
+ "globals and locals cannot be NULL");
+ return NULL;
+ }
+ }
+ else if (locals == Py_None)
+ locals = globals;
+
+ if (!PyDict_Check(globals)) {
+ PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s",
+ globals->ob_type->tp_name);
+ return NULL;
+ }
+ if (!PyMapping_Check(locals)) {
+ PyErr_Format(PyExc_TypeError,
+ "arg 3 must be a mapping or None, not %.100s",
+ locals->ob_type->tp_name);
+ return NULL;
+ }
+ if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
+ return NULL;
+ }
+
+ if (PyCode_Check(prog)) {
+ if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
+ PyErr_SetString(PyExc_TypeError,
+ "code object passed to exec() may not "
+ "contain free variables");
+ return NULL;
+ }
+ v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
+ }
+ else {
+ char *str;
+ PyCompilerFlags cf;
+ cf.cf_flags = PyCF_SOURCE_IS_UTF8;
+ str = source_as_string(prog, "exec",
+ "string, bytes or code", &cf);
+ if (str == NULL)
+ return NULL;
+ if (PyEval_MergeCompilerFlags(&cf))
+ v = PyRun_StringFlags(str, Py_file_input, globals,
+ locals, &cf);
+ else
+ v = PyRun_String(str, Py_file_input, globals, locals);
+ }
+ if (v == NULL)
+ return NULL;
+ Py_DECREF(v);
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(exec_doc,
@@ -825,26 +825,26 @@ globals and locals. If only globals is given, locals defaults to it.");
static PyObject *
builtin_getattr(PyObject *self, PyObject *args)
{
- PyObject *v, *result, *dflt = NULL;
- PyObject *name;
-
- if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
- return NULL;
-
- if (!PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "getattr(): attribute name must be string");
- return NULL;
- }
- result = PyObject_GetAttr(v, name);
- if (result == NULL && dflt != NULL &&
- PyErr_ExceptionMatches(PyExc_AttributeError))
- {
- PyErr_Clear();
- Py_INCREF(dflt);
- result = dflt;
- }
- return result;
+ PyObject *v, *result, *dflt = NULL;
+ PyObject *name;
+
+ if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
+ return NULL;
+
+ if (!PyUnicode_Check(name)) {
+ PyErr_SetString(PyExc_TypeError,
+ "getattr(): attribute name must be string");
+ return NULL;
+ }
+ result = PyObject_GetAttr(v, name);
+ if (result == NULL && dflt != NULL &&
+ PyErr_ExceptionMatches(PyExc_AttributeError))
+ {
+ PyErr_Clear();
+ Py_INCREF(dflt);
+ result = dflt;
+ }
+ return result;
}
PyDoc_STRVAR(getattr_doc,
@@ -858,11 +858,11 @@ exist; without it, an exception is raised in that case.");
static PyObject *
builtin_globals(PyObject *self)
{
- PyObject *d;
+ PyObject *d;
- d = PyEval_GetGlobals();
- Py_XINCREF(d);
- return d;
+ d = PyEval_GetGlobals();
+ Py_XINCREF(d);
+ return d;
}
PyDoc_STRVAR(globals_doc,
@@ -874,29 +874,29 @@ Return the dictionary containing the current scope's global variables.");
static PyObject *
builtin_hasattr(PyObject *self, PyObject *args)
{
- PyObject *v;
- PyObject *name;
-
- if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
- return NULL;
- if (!PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "hasattr(): attribute name must be string");
- return NULL;
- }
- v = PyObject_GetAttr(v, name);
- if (v == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_Exception))
- return NULL;
- else {
- PyErr_Clear();
- Py_INCREF(Py_False);
- return Py_False;
- }
- }
- Py_DECREF(v);
- Py_INCREF(Py_True);
- return Py_True;
+ PyObject *v;
+ PyObject *name;
+
+ if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
+ return NULL;
+ if (!PyUnicode_Check(name)) {
+ PyErr_SetString(PyExc_TypeError,
+ "hasattr(): attribute name must be string");
+ return NULL;
+ }
+ v = PyObject_GetAttr(v, name);
+ if (v == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_Exception))
+ return NULL;
+ else {
+ PyErr_Clear();
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
+ }
+ Py_DECREF(v);
+ Py_INCREF(Py_True);
+ return Py_True;
}
PyDoc_STRVAR(hasattr_doc,
@@ -909,7 +909,7 @@ Return whether the object has an attribute with the given name.\n\
static PyObject *
builtin_id(PyObject *self, PyObject *v)
{
- return PyLong_FromVoidPtr(v);
+ return PyLong_FromVoidPtr(v);
}
PyDoc_STRVAR(id_doc,
@@ -922,181 +922,181 @@ simultaneously existing objects. (Hint: it's the object's memory address.)");
/* map object ************************************************************/
typedef struct {
- PyObject_HEAD
- PyObject *iters;
- PyObject *func;
+ PyObject_HEAD
+ PyObject *iters;
+ PyObject *func;
} mapobject;
static PyObject *
map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *it, *iters, *func;
- mapobject *lz;
- Py_ssize_t numargs, i;
-
- if (type == &PyMap_Type && !_PyArg_NoKeywords("map()", kwds))
- return NULL;
-
- numargs = PyTuple_Size(args);
- if (numargs < 2) {
- PyErr_SetString(PyExc_TypeError,
- "map() must have at least two arguments.");
- return NULL;
- }
-
- iters = PyTuple_New(numargs-1);
- if (iters == NULL)
- return NULL;
-
- for (i=1 ; i<numargs ; i++) {
- /* Get iterator. */
- it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
- if (it == NULL) {
- Py_DECREF(iters);
- return NULL;
- }
- PyTuple_SET_ITEM(iters, i-1, it);
- }
-
- /* create mapobject structure */
- lz = (mapobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(iters);
- return NULL;
- }
- lz->iters = iters;
- func = PyTuple_GET_ITEM(args, 0);
- Py_INCREF(func);
- lz->func = func;
-
- return (PyObject *)lz;
+ PyObject *it, *iters, *func;
+ mapobject *lz;
+ Py_ssize_t numargs, i;
+
+ if (type == &PyMap_Type && !_PyArg_NoKeywords("map()", kwds))
+ return NULL;
+
+ numargs = PyTuple_Size(args);
+ if (numargs < 2) {
+ PyErr_SetString(PyExc_TypeError,
+ "map() must have at least two arguments.");
+ return NULL;
+ }
+
+ iters = PyTuple_New(numargs-1);
+ if (iters == NULL)
+ return NULL;
+
+ for (i=1 ; i<numargs ; i++) {
+ /* Get iterator. */
+ it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
+ if (it == NULL) {
+ Py_DECREF(iters);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(iters, i-1, it);
+ }
+
+ /* create mapobject structure */
+ lz = (mapobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(iters);
+ return NULL;
+ }
+ lz->iters = iters;
+ func = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(func);
+ lz->func = func;
+
+ return (PyObject *)lz;
}
static void
map_dealloc(mapobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->iters);
- Py_XDECREF(lz->func);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->iters);
+ Py_XDECREF(lz->func);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
map_traverse(mapobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->iters);
- Py_VISIT(lz->func);
- return 0;
+ Py_VISIT(lz->iters);
+ Py_VISIT(lz->func);
+ return 0;
}
static PyObject *
map_next(mapobject *lz)
{
- PyObject *val;
- PyObject *argtuple;
- PyObject *result;
- Py_ssize_t numargs, i;
-
- numargs = PyTuple_Size(lz->iters);
- argtuple = PyTuple_New(numargs);
- if (argtuple == NULL)
- return NULL;
-
- for (i=0 ; i<numargs ; i++) {
- val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
- if (val == NULL) {
- Py_DECREF(argtuple);
- return NULL;
- }
- PyTuple_SET_ITEM(argtuple, i, val);
- }
- result = PyObject_Call(lz->func, argtuple, NULL);
- Py_DECREF(argtuple);
- return result;
+ PyObject *val;
+ PyObject *argtuple;
+ PyObject *result;
+ Py_ssize_t numargs, i;
+
+ numargs = PyTuple_Size(lz->iters);
+ argtuple = PyTuple_New(numargs);
+ if (argtuple == NULL)
+ return NULL;
+
+ for (i=0 ; i<numargs ; i++) {
+ val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
+ if (val == NULL) {
+ Py_DECREF(argtuple);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(argtuple, i, val);
+ }
+ result = PyObject_Call(lz->func, argtuple, NULL);
+ Py_DECREF(argtuple);
+ return result;
}
PyDoc_STRVAR(map_doc,
"map(func, *iterables) --> map object\n\
\n\
Make an iterator that computes the function using arguments from\n\
-each of the iterables. Stops when the shortest iterable is exhausted.");
+each of the iterables. Stops when the shortest iterable is exhausted.");
PyTypeObject PyMap_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "map", /* tp_name */
- sizeof(mapobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)map_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- map_doc, /* tp_doc */
- (traverseproc)map_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)map_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- map_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "map", /* tp_name */
+ sizeof(mapobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)map_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ map_doc, /* tp_doc */
+ (traverseproc)map_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)map_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ map_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
static PyObject *
builtin_next(PyObject *self, PyObject *args)
{
- PyObject *it, *res;
- PyObject *def = NULL;
-
- if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
- return NULL;
- if (!PyIter_Check(it)) {
- PyErr_Format(PyExc_TypeError,
- "%.200s object is not an iterator",
- it->ob_type->tp_name);
- return NULL;
- }
-
- res = (*it->ob_type->tp_iternext)(it);
- if (res != NULL) {
- return res;
- } else if (def != NULL) {
- if (PyErr_Occurred()) {
- if(!PyErr_ExceptionMatches(PyExc_StopIteration))
- return NULL;
- PyErr_Clear();
- }
- Py_INCREF(def);
- return def;
- } else if (PyErr_Occurred()) {
- return NULL;
- } else {
- PyErr_SetNone(PyExc_StopIteration);
- return NULL;
- }
+ PyObject *it, *res;
+ PyObject *def = NULL;
+
+ if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
+ return NULL;
+ if (!PyIter_Check(it)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s object is not an iterator",
+ it->ob_type->tp_name);
+ return NULL;
+ }
+
+ res = (*it->ob_type->tp_iternext)(it);
+ if (res != NULL) {
+ return res;
+ } else if (def != NULL) {
+ if (PyErr_Occurred()) {
+ if(!PyErr_ExceptionMatches(PyExc_StopIteration))
+ return NULL;
+ PyErr_Clear();
+ }
+ Py_INCREF(def);
+ return def;
+ } else if (PyErr_Occurred()) {
+ return NULL;
+ } else {
+ PyErr_SetNone(PyExc_StopIteration);
+ return NULL;
+ }
}
PyDoc_STRVAR(next_doc,
@@ -1109,16 +1109,16 @@ is exhausted, it is returned instead of raising StopIteration.");
static PyObject *
builtin_setattr(PyObject *self, PyObject *args)
{
- PyObject *v;
- PyObject *name;
- PyObject *value;
-
- if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
- return NULL;
- if (PyObject_SetAttr(v, name, value) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *v;
+ PyObject *name;
+ PyObject *value;
+
+ if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
+ return NULL;
+ if (PyObject_SetAttr(v, name, value) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setattr_doc,
@@ -1131,15 +1131,15 @@ Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
static PyObject *
builtin_delattr(PyObject *self, PyObject *args)
{
- PyObject *v;
- PyObject *name;
-
- if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
- return NULL;
- if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *v;
+ PyObject *name;
+
+ if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
+ return NULL;
+ if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(delattr_doc,
@@ -1152,12 +1152,12 @@ Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
static PyObject *
builtin_hash(PyObject *self, PyObject *v)
{
- long x;
+ long x;
- x = PyObject_Hash(v);
- if (x == -1)
- return NULL;
- return PyLong_FromLong(x);
+ x = PyObject_Hash(v);
+ if (x == -1)
+ return NULL;
+ return PyLong_FromLong(x);
}
PyDoc_STRVAR(hash_doc,
@@ -1170,7 +1170,7 @@ the same hash value. The reverse is not necessarily true, but likely.");
static PyObject *
builtin_hex(PyObject *self, PyObject *v)
{
- return PyNumber_ToBase(v, 16);
+ return PyNumber_ToBase(v, 16);
}
PyDoc_STRVAR(hex_doc,
@@ -1182,18 +1182,18 @@ Return the hexadecimal representation of an integer or long integer.");
static PyObject *
builtin_iter(PyObject *self, PyObject *args)
{
- PyObject *v, *w = NULL;
-
- if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
- return NULL;
- if (w == NULL)
- return PyObject_GetIter(v);
- if (!PyCallable_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "iter(v, w): v must be callable");
- return NULL;
- }
- return PyCallIter_New(v, w);
+ PyObject *v, *w = NULL;
+
+ if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
+ return NULL;
+ if (w == NULL)
+ return PyObject_GetIter(v);
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "iter(v, w): v must be callable");
+ return NULL;
+ }
+ return PyCallIter_New(v, w);
}
PyDoc_STRVAR(iter_doc,
@@ -1208,12 +1208,12 @@ In the second form, the callable is called until it returns the sentinel.");
static PyObject *
builtin_len(PyObject *self, PyObject *v)
{
- Py_ssize_t res;
+ Py_ssize_t res;
- res = PyObject_Size(v);
- if (res < 0 && PyErr_Occurred())
- return NULL;
- return PyLong_FromSsize_t(res);
+ res = PyObject_Size(v);
+ if (res < 0 && PyErr_Occurred())
+ return NULL;
+ return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(len_doc,
@@ -1225,11 +1225,11 @@ Return the number of items of a sequence or mapping.");
static PyObject *
builtin_locals(PyObject *self)
{
- PyObject *d;
+ PyObject *d;
- d = PyEval_GetLocals();
- Py_XINCREF(d);
- return d;
+ d = PyEval_GetLocals();
+ Py_XINCREF(d);
+ return d;
}
PyDoc_STRVAR(locals_doc,
@@ -1241,96 +1241,96 @@ Update and return a dictionary containing the current scope's local variables.")
static PyObject *
min_max(PyObject *args, PyObject *kwds, int op)
{
- PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
- const char *name = op == Py_LT ? "min" : "max";
-
- if (PyTuple_Size(args) > 1)
- v = args;
- else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
- return NULL;
-
- if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
- keyfunc = PyDict_GetItemString(kwds, "key");
- if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
- PyErr_Format(PyExc_TypeError,
- "%s() got an unexpected keyword argument", name);
- return NULL;
- }
- Py_INCREF(keyfunc);
- }
-
- it = PyObject_GetIter(v);
- if (it == NULL) {
- Py_XDECREF(keyfunc);
- return NULL;
- }
-
- maxitem = NULL; /* the result */
- maxval = NULL; /* the value associated with the result */
- while (( item = PyIter_Next(it) )) {
- /* get the value from the key function */
- if (keyfunc != NULL) {
- val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
- if (val == NULL)
- goto Fail_it_item;
- }
- /* no key function; the value is the item */
- else {
- val = item;
- Py_INCREF(val);
- }
-
- /* maximum value and item are unset; set them */
- if (maxval == NULL) {
- maxitem = item;
- maxval = val;
- }
- /* maximum value and item are set; update them as necessary */
- else {
- int cmp = PyObject_RichCompareBool(val, maxval, op);
- if (cmp < 0)
- goto Fail_it_item_and_val;
- else if (cmp > 0) {
- Py_DECREF(maxval);
- Py_DECREF(maxitem);
- maxval = val;
- maxitem = item;
- }
- else {
- Py_DECREF(item);
- Py_DECREF(val);
- }
- }
- }
- if (PyErr_Occurred())
- goto Fail_it;
- if (maxval == NULL) {
- PyErr_Format(PyExc_ValueError,
- "%s() arg is an empty sequence", name);
- assert(maxitem == NULL);
- }
- else
- Py_DECREF(maxval);
- Py_DECREF(it);
- Py_XDECREF(keyfunc);
- return maxitem;
+ PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
+ const char *name = op == Py_LT ? "min" : "max";
+
+ if (PyTuple_Size(args) > 1)
+ v = args;
+ else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
+ return NULL;
+
+ if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
+ keyfunc = PyDict_GetItemString(kwds, "key");
+ if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%s() got an unexpected keyword argument", name);
+ return NULL;
+ }
+ Py_INCREF(keyfunc);
+ }
+
+ it = PyObject_GetIter(v);
+ if (it == NULL) {
+ Py_XDECREF(keyfunc);
+ return NULL;
+ }
+
+ maxitem = NULL; /* the result */
+ maxval = NULL; /* the value associated with the result */
+ while (( item = PyIter_Next(it) )) {
+ /* get the value from the key function */
+ if (keyfunc != NULL) {
+ val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
+ if (val == NULL)
+ goto Fail_it_item;
+ }
+ /* no key function; the value is the item */
+ else {
+ val = item;
+ Py_INCREF(val);
+ }
+
+ /* maximum value and item are unset; set them */
+ if (maxval == NULL) {
+ maxitem = item;
+ maxval = val;
+ }
+ /* maximum value and item are set; update them as necessary */
+ else {
+ int cmp = PyObject_RichCompareBool(val, maxval, op);
+ if (cmp < 0)
+ goto Fail_it_item_and_val;
+ else if (cmp > 0) {
+ Py_DECREF(maxval);
+ Py_DECREF(maxitem);
+ maxval = val;
+ maxitem = item;
+ }
+ else {
+ Py_DECREF(item);
+ Py_DECREF(val);
+ }
+ }
+ }
+ if (PyErr_Occurred())
+ goto Fail_it;
+ if (maxval == NULL) {
+ PyErr_Format(PyExc_ValueError,
+ "%s() arg is an empty sequence", name);
+ assert(maxitem == NULL);
+ }
+ else
+ Py_DECREF(maxval);
+ Py_DECREF(it);
+ Py_XDECREF(keyfunc);
+ return maxitem;
Fail_it_item_and_val:
- Py_DECREF(val);
+ Py_DECREF(val);
Fail_it_item:
- Py_DECREF(item);
+ Py_DECREF(item);
Fail_it:
- Py_XDECREF(maxval);
- Py_XDECREF(maxitem);
- Py_DECREF(it);
- Py_XDECREF(keyfunc);
- return NULL;
+ Py_XDECREF(maxval);
+ Py_XDECREF(maxitem);
+ Py_DECREF(it);
+ Py_XDECREF(keyfunc);
+ return NULL;
}
static PyObject *
builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
{
- return min_max(args, kwds, Py_LT);
+ return min_max(args, kwds, Py_LT);
}
PyDoc_STRVAR(min_doc,
@@ -1344,7 +1344,7 @@ With two or more arguments, return the smallest argument.");
static PyObject *
builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
{
- return min_max(args, kwds, Py_GT);
+ return min_max(args, kwds, Py_GT);
}
PyDoc_STRVAR(max_doc,
@@ -1358,7 +1358,7 @@ With two or more arguments, return the largest argument.");
static PyObject *
builtin_oct(PyObject *self, PyObject *v)
{
- return PyNumber_ToBase(v, 8);
+ return PyNumber_ToBase(v, 8);
}
PyDoc_STRVAR(oct_doc,
@@ -1370,56 +1370,56 @@ Return the octal representation of an integer or long integer.");
static PyObject *
builtin_ord(PyObject *self, PyObject* obj)
{
- long ord;
- Py_ssize_t size;
-
- if (PyBytes_Check(obj)) {
- size = PyBytes_GET_SIZE(obj);
- if (size == 1) {
- ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
- return PyLong_FromLong(ord);
- }
- }
- else if (PyUnicode_Check(obj)) {
- size = PyUnicode_GET_SIZE(obj);
- if (size == 1) {
- ord = (long)*PyUnicode_AS_UNICODE(obj);
- return PyLong_FromLong(ord);
- }
+ long ord;
+ Py_ssize_t size;
+
+ if (PyBytes_Check(obj)) {
+ size = PyBytes_GET_SIZE(obj);
+ if (size == 1) {
+ ord = (long)((unsigned char)*PyBytes_AS_STRING(obj));
+ return PyLong_FromLong(ord);
+ }
+ }
+ else if (PyUnicode_Check(obj)) {
+ size = PyUnicode_GET_SIZE(obj);
+ if (size == 1) {
+ ord = (long)*PyUnicode_AS_UNICODE(obj);
+ return PyLong_FromLong(ord);
+ }
#ifndef Py_UNICODE_WIDE
- if (size == 2) {
- /* Decode a valid surrogate pair */
- int c0 = PyUnicode_AS_UNICODE(obj)[0];
- int c1 = PyUnicode_AS_UNICODE(obj)[1];
- if (0xD800 <= c0 && c0 <= 0xDBFF &&
- 0xDC00 <= c1 && c1 <= 0xDFFF) {
- ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
- 0x00010000);
- return PyLong_FromLong(ord);
- }
- }
+ if (size == 2) {
+ /* Decode a valid surrogate pair */
+ int c0 = PyUnicode_AS_UNICODE(obj)[0];
+ int c1 = PyUnicode_AS_UNICODE(obj)[1];
+ if (0xD800 <= c0 && c0 <= 0xDBFF &&
+ 0xDC00 <= c1 && c1 <= 0xDFFF) {
+ ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) +
+ 0x00010000);
+ return PyLong_FromLong(ord);
+ }
+ }
#endif
- }
- else if (PyByteArray_Check(obj)) {
- /* XXX Hopefully this is temporary */
- size = PyByteArray_GET_SIZE(obj);
- if (size == 1) {
- ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
- return PyLong_FromLong(ord);
- }
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "ord() expected string of length 1, but " \
- "%.200s found", obj->ob_type->tp_name);
- return NULL;
- }
-
- PyErr_Format(PyExc_TypeError,
- "ord() expected a character, "
- "but string of length %zd found",
- size);
- return NULL;
+ }
+ else if (PyByteArray_Check(obj)) {
+ /* XXX Hopefully this is temporary */
+ size = PyByteArray_GET_SIZE(obj);
+ if (size == 1) {
+ ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
+ return PyLong_FromLong(ord);
+ }
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "ord() expected string of length 1, but " \
+ "%.200s found", obj->ob_type->tp_name);
+ return NULL;
+ }
+
+ PyErr_Format(PyExc_TypeError,
+ "ord() expected a character, "
+ "but string of length %zd found",
+ size);
+ return NULL;
}
PyDoc_VAR(ord_doc) = PyDoc_STR(
@@ -1438,11 +1438,11 @@ PyDoc_STR(
static PyObject *
builtin_pow(PyObject *self, PyObject *args)
{
- PyObject *v, *w, *z = Py_None;
+ PyObject *v, *w, *z = Py_None;
- if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
- return NULL;
- return PyNumber_Power(v, w, z);
+ if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
+ return NULL;
+ return PyNumber_Power(v, w, z);
}
PyDoc_STRVAR(pow_doc,
@@ -1456,62 +1456,62 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
static PyObject *
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"sep", "end", "file", 0};
- static PyObject *dummy_args;
- PyObject *sep = NULL, *end = NULL, *file = NULL;
- int i, err;
-
- if (dummy_args == NULL) {
- if (!(dummy_args = PyTuple_New(0)))
- return NULL;
- }
- if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
- kwlist, &sep, &end, &file))
- return NULL;
- if (file == NULL || file == Py_None) {
- file = PySys_GetObject("stdout");
- /* sys.stdout may be None when FILE* stdout isn't connected */
- if (file == Py_None)
- Py_RETURN_NONE;
- }
-
- if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
- PyErr_Format(PyExc_TypeError,
- "sep must be None or a string, not %.200s",
- sep->ob_type->tp_name);
- return NULL;
- }
- if (end && end != Py_None && !PyUnicode_Check(end)) {
- PyErr_Format(PyExc_TypeError,
- "end must be None or a string, not %.200s",
- end->ob_type->tp_name);
- return NULL;
- }
-
- for (i = 0; i < PyTuple_Size(args); i++) {
- if (i > 0) {
- if (sep == NULL || sep == Py_None)
- err = PyFile_WriteString(" ", file);
- else
- err = PyFile_WriteObject(sep, file,
- Py_PRINT_RAW);
- if (err)
- return NULL;
- }
- err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
- Py_PRINT_RAW);
- if (err)
- return NULL;
- }
-
- if (end == NULL || end == Py_None)
- err = PyFile_WriteString("\n", file);
- else
- err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
- if (err)
- return NULL;
-
- Py_RETURN_NONE;
+ static char *kwlist[] = {"sep", "end", "file", 0};
+ static PyObject *dummy_args;
+ PyObject *sep = NULL, *end = NULL, *file = NULL;
+ int i, err;
+
+ if (dummy_args == NULL) {
+ if (!(dummy_args = PyTuple_New(0)))
+ return NULL;
+ }
+ if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
+ kwlist, &sep, &end, &file))
+ return NULL;
+ if (file == NULL || file == Py_None) {
+ file = PySys_GetObject("stdout");
+ /* sys.stdout may be None when FILE* stdout isn't connected */
+ if (file == Py_None)
+ Py_RETURN_NONE;
+ }
+
+ if (sep && sep != Py_None && !PyUnicode_Check(sep)) {
+ PyErr_Format(PyExc_TypeError,
+ "sep must be None or a string, not %.200s",
+ sep->ob_type->tp_name);
+ return NULL;
+ }
+ if (end && end != Py_None && !PyUnicode_Check(end)) {
+ PyErr_Format(PyExc_TypeError,
+ "end must be None or a string, not %.200s",
+ end->ob_type->tp_name);
+ return NULL;
+ }
+
+ for (i = 0; i < PyTuple_Size(args); i++) {
+ if (i > 0) {
+ if (sep == NULL || sep == Py_None)
+ err = PyFile_WriteString(" ", file);
+ else
+ err = PyFile_WriteObject(sep, file,
+ Py_PRINT_RAW);
+ if (err)
+ return NULL;
+ }
+ err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
+ Py_PRINT_RAW);
+ if (err)
+ return NULL;
+ }
+
+ if (end == NULL || end == Py_None)
+ err = PyFile_WriteString("\n", file);
+ else
+ err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
+ if (err)
+ return NULL;
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(print_doc,
@@ -1527,164 +1527,164 @@ end: string appended after the last value, default a newline.");
static PyObject *
builtin_input(PyObject *self, PyObject *args)
{
- PyObject *promptarg = NULL;
- PyObject *fin = PySys_GetObject("stdin");
- PyObject *fout = PySys_GetObject("stdout");
- PyObject *ferr = PySys_GetObject("stderr");
- PyObject *tmp;
- long fd;
- int tty;
-
- /* Parse arguments */
- if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
- return NULL;
-
- /* Check that stdin/out/err are intact */
- if (fin == NULL || fin == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stdin");
- return NULL;
- }
- if (fout == NULL || fout == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stdout");
- return NULL;
- }
- if (ferr == NULL || ferr == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stderr");
- return NULL;
- }
-
- /* First of all, flush stderr */
- tmp = PyObject_CallMethod(ferr, "flush", "");
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
-
- /* We should only use (GNU) readline if Python's sys.stdin and
- sys.stdout are the same as C's stdin and stdout, because we
- need to pass it those. */
- tmp = PyObject_CallMethod(fin, "fileno", "");
- if (tmp == NULL) {
- PyErr_Clear();
- tty = 0;
- }
- else {
- fd = PyLong_AsLong(tmp);
- Py_DECREF(tmp);
- if (fd < 0 && PyErr_Occurred())
- return NULL;
- tty = fd == fileno(stdin) && isatty(fd);
- }
- if (tty) {
- tmp = PyObject_CallMethod(fout, "fileno", "");
- if (tmp == NULL)
- PyErr_Clear();
- else {
- fd = PyLong_AsLong(tmp);
- Py_DECREF(tmp);
- if (fd < 0 && PyErr_Occurred())
- return NULL;
- tty = fd == fileno(stdout) && isatty(fd);
- }
- }
-
- /* If we're interactive, use (GNU) readline */
- if (tty) {
- PyObject *po;
- char *prompt;
- char *s;
- PyObject *stdin_encoding;
- PyObject *result;
-
- stdin_encoding = PyObject_GetAttrString(fin, "encoding");
- if (!stdin_encoding)
- /* stdin is a text stream, so it must have an
- encoding. */
- return NULL;
- tmp = PyObject_CallMethod(fout, "flush", "");
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
- if (promptarg != NULL) {
- PyObject *stringpo;
- PyObject *stdout_encoding;
- stdout_encoding = PyObject_GetAttrString(fout,
- "encoding");
- if (stdout_encoding == NULL) {
- Py_DECREF(stdin_encoding);
- return NULL;
- }
- stringpo = PyObject_Str(promptarg);
- if (stringpo == NULL) {
- Py_DECREF(stdin_encoding);
- Py_DECREF(stdout_encoding);
- return NULL;
- }
- po = PyUnicode_AsEncodedString(stringpo,
- _PyUnicode_AsString(stdout_encoding), NULL);
- Py_DECREF(stdout_encoding);
- Py_DECREF(stringpo);
- if (po == NULL) {
- Py_DECREF(stdin_encoding);
- return NULL;
- }
- prompt = PyBytes_AsString(po);
- if (prompt == NULL) {
- Py_DECREF(stdin_encoding);
- Py_DECREF(po);
- return NULL;
- }
- }
- else {
- po = NULL;
- prompt = "";
- }
- s = PyOS_Readline(stdin, stdout, prompt);
- Py_XDECREF(po);
- if (s == NULL) {
- if (!PyErr_Occurred())
- PyErr_SetNone(PyExc_KeyboardInterrupt);
- Py_DECREF(stdin_encoding);
- return NULL;
- }
- if (*s == '\0') {
- PyErr_SetNone(PyExc_EOFError);
- result = NULL;
- }
- else { /* strip trailing '\n' */
- size_t len = strlen(s);
- if (len > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "input: input too long");
- result = NULL;
- }
- else {
- result = PyUnicode_Decode
- (s, len-1,
- _PyUnicode_AsString(stdin_encoding),
- NULL);
- }
- }
- Py_DECREF(stdin_encoding);
- PyMem_FREE(s);
- return result;
- }
-
- /* Fallback if we're not interactive */
- if (promptarg != NULL) {
- if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
- return NULL;
- }
- tmp = PyObject_CallMethod(fout, "flush", "");
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
- return PyFile_GetLine(fin, -1);
+ PyObject *promptarg = NULL;
+ PyObject *fin = PySys_GetObject("stdin");
+ PyObject *fout = PySys_GetObject("stdout");
+ PyObject *ferr = PySys_GetObject("stderr");
+ PyObject *tmp;
+ long fd;
+ int tty;
+
+ /* Parse arguments */
+ if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg))
+ return NULL;
+
+ /* Check that stdin/out/err are intact */
+ if (fin == NULL || fin == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "input(): lost sys.stdin");
+ return NULL;
+ }
+ if (fout == NULL || fout == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "input(): lost sys.stdout");
+ return NULL;
+ }
+ if (ferr == NULL || ferr == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "input(): lost sys.stderr");
+ return NULL;
+ }
+
+ /* First of all, flush stderr */
+ tmp = PyObject_CallMethod(ferr, "flush", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(tmp);
+
+ /* We should only use (GNU) readline if Python's sys.stdin and
+ sys.stdout are the same as C's stdin and stdout, because we
+ need to pass it those. */
+ tmp = PyObject_CallMethod(fin, "fileno", "");
+ if (tmp == NULL) {
+ PyErr_Clear();
+ tty = 0;
+ }
+ else {
+ fd = PyLong_AsLong(tmp);
+ Py_DECREF(tmp);
+ if (fd < 0 && PyErr_Occurred())
+ return NULL;
+ tty = fd == fileno(stdin) && isatty(fd);
+ }
+ if (tty) {
+ tmp = PyObject_CallMethod(fout, "fileno", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else {
+ fd = PyLong_AsLong(tmp);
+ Py_DECREF(tmp);
+ if (fd < 0 && PyErr_Occurred())
+ return NULL;
+ tty = fd == fileno(stdout) && isatty(fd);
+ }
+ }
+
+ /* If we're interactive, use (GNU) readline */
+ if (tty) {
+ PyObject *po;
+ char *prompt;
+ char *s;
+ PyObject *stdin_encoding;
+ PyObject *result;
+
+ stdin_encoding = PyObject_GetAttrString(fin, "encoding");
+ if (!stdin_encoding)
+ /* stdin is a text stream, so it must have an
+ encoding. */
+ return NULL;
+ tmp = PyObject_CallMethod(fout, "flush", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(tmp);
+ if (promptarg != NULL) {
+ PyObject *stringpo;
+ PyObject *stdout_encoding;
+ stdout_encoding = PyObject_GetAttrString(fout,
+ "encoding");
+ if (stdout_encoding == NULL) {
+ Py_DECREF(stdin_encoding);
+ return NULL;
+ }
+ stringpo = PyObject_Str(promptarg);
+ if (stringpo == NULL) {
+ Py_DECREF(stdin_encoding);
+ Py_DECREF(stdout_encoding);
+ return NULL;
+ }
+ po = PyUnicode_AsEncodedString(stringpo,
+ _PyUnicode_AsString(stdout_encoding), NULL);
+ Py_DECREF(stdout_encoding);
+ Py_DECREF(stringpo);
+ if (po == NULL) {
+ Py_DECREF(stdin_encoding);
+ return NULL;
+ }
+ prompt = PyBytes_AsString(po);
+ if (prompt == NULL) {
+ Py_DECREF(stdin_encoding);
+ Py_DECREF(po);
+ return NULL;
+ }
+ }
+ else {
+ po = NULL;
+ prompt = "";
+ }
+ s = PyOS_Readline(stdin, stdout, prompt);
+ Py_XDECREF(po);
+ if (s == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_SetNone(PyExc_KeyboardInterrupt);
+ Py_DECREF(stdin_encoding);
+ return NULL;
+ }
+ if (*s == '\0') {
+ PyErr_SetNone(PyExc_EOFError);
+ result = NULL;
+ }
+ else { /* strip trailing '\n' */
+ size_t len = strlen(s);
+ if (len > PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "input: input too long");
+ result = NULL;
+ }
+ else {
+ result = PyUnicode_Decode
+ (s, len-1,
+ _PyUnicode_AsString(stdin_encoding),
+ NULL);
+ }
+ }
+ Py_DECREF(stdin_encoding);
+ PyMem_FREE(s);
+ return result;
+ }
+
+ /* Fallback if we're not interactive */
+ if (promptarg != NULL) {
+ if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
+ return NULL;
+ }
+ tmp = PyObject_CallMethod(fout, "flush", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(tmp);
+ return PyFile_GetLine(fin, -1);
}
PyDoc_STRVAR(input_doc,
@@ -1699,7 +1699,7 @@ is printed without a trailing newline before reading.");
static PyObject *
builtin_repr(PyObject *self, PyObject *v)
{
- return PyObject_Repr(v);
+ return PyObject_Repr(v);
}
PyDoc_STRVAR(repr_doc,
@@ -1712,38 +1712,38 @@ For most object types, eval(repr(object)) == object.");
static PyObject *
builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
{
- static PyObject *round_str = NULL;
- PyObject *ndigits = NULL;
- static char *kwlist[] = {"number", "ndigits", 0};
- PyObject *number, *round;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round",
- kwlist, &number, &ndigits))
- return NULL;
-
- if (Py_TYPE(number)->tp_dict == NULL) {
- if (PyType_Ready(Py_TYPE(number)) < 0)
- return NULL;
- }
-
- if (round_str == NULL) {
- round_str = PyUnicode_InternFromString("__round__");
- if (round_str == NULL)
- return NULL;
- }
-
- round = _PyType_Lookup(Py_TYPE(number), round_str);
- if (round == NULL) {
- PyErr_Format(PyExc_TypeError,
- "type %.100s doesn't define __round__ method",
- Py_TYPE(number)->tp_name);
- return NULL;
- }
-
- if (ndigits == NULL)
- return PyObject_CallFunction(round, "O", number);
- else
- return PyObject_CallFunction(round, "OO", number, ndigits);
+ static PyObject *round_str = NULL;
+ PyObject *ndigits = NULL;
+ static char *kwlist[] = {"number", "ndigits", 0};
+ PyObject *number, *round;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round",
+ kwlist, &number, &ndigits))
+ return NULL;
+
+ if (Py_TYPE(number)->tp_dict == NULL) {
+ if (PyType_Ready(Py_TYPE(number)) < 0)
+ return NULL;
+ }
+
+ if (round_str == NULL) {
+ round_str = PyUnicode_InternFromString("__round__");
+ if (round_str == NULL)
+ return NULL;
+ }
+
+ round = _PyType_Lookup(Py_TYPE(number), round_str);
+ if (round == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "type %.100s doesn't define __round__ method",
+ Py_TYPE(number)->tp_name);
+ return NULL;
+ }
+
+ if (ndigits == NULL)
+ return PyObject_CallFunction(round, "O", number);
+ else
+ return PyObject_CallFunction(round, "OO", number, ndigits);
}
PyDoc_STRVAR(round_doc,
@@ -1757,42 +1757,42 @@ same type as the number. ndigits may be negative.");
static PyObject *
builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs;
- PyObject *callable;
- static char *kwlist[] = {"iterable", "key", "reverse", 0};
- int reverse;
-
- /* args 1-3 should match listsort in Objects/listobject.c */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
- kwlist, &seq, &keyfunc, &reverse))
- return NULL;
-
- newlist = PySequence_List(seq);
- if (newlist == NULL)
- return NULL;
-
- callable = PyObject_GetAttrString(newlist, "sort");
- if (callable == NULL) {
- Py_DECREF(newlist);
- return NULL;
- }
-
- newargs = PyTuple_GetSlice(args, 1, 4);
- if (newargs == NULL) {
- Py_DECREF(newlist);
- Py_DECREF(callable);
- return NULL;
- }
-
- v = PyObject_Call(callable, newargs, kwds);
- Py_DECREF(newargs);
- Py_DECREF(callable);
- if (v == NULL) {
- Py_DECREF(newlist);
- return NULL;
- }
- Py_DECREF(v);
- return newlist;
+ PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs;
+ PyObject *callable;
+ static char *kwlist[] = {"iterable", "key", "reverse", 0};
+ int reverse;
+
+ /* args 1-3 should match listsort in Objects/listobject.c */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
+ kwlist, &seq, &keyfunc, &reverse))
+ return NULL;
+
+ newlist = PySequence_List(seq);
+ if (newlist == NULL)
+ return NULL;
+
+ callable = PyObject_GetAttrString(newlist, "sort");
+ if (callable == NULL) {
+ Py_DECREF(newlist);
+ return NULL;
+ }
+
+ newargs = PyTuple_GetSlice(args, 1, 4);
+ if (newargs == NULL) {
+ Py_DECREF(newlist);
+ Py_DECREF(callable);
+ return NULL;
+ }
+
+ v = PyObject_Call(callable, newargs, kwds);
+ Py_DECREF(newargs);
+ Py_DECREF(callable);
+ if (v == NULL) {
+ Py_DECREF(newlist);
+ return NULL;
+ }
+ Py_DECREF(v);
+ return newlist;
}
PyDoc_STRVAR(sorted_doc,
@@ -1801,30 +1801,30 @@ PyDoc_STRVAR(sorted_doc,
static PyObject *
builtin_vars(PyObject *self, PyObject *args)
{
- PyObject *v = NULL;
- PyObject *d;
-
- if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
- return NULL;
- if (v == NULL) {
- d = PyEval_GetLocals();
- if (d == NULL) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_SystemError,
- "vars(): no locals!?");
- }
- else
- Py_INCREF(d);
- }
- else {
- d = PyObject_GetAttrString(v, "__dict__");
- if (d == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "vars() argument must have __dict__ attribute");
- return NULL;
- }
- }
- return d;
+ PyObject *v = NULL;
+ PyObject *d;
+
+ if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
+ return NULL;
+ if (v == NULL) {
+ d = PyEval_GetLocals();
+ if (d == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError,
+ "vars(): no locals!?");
+ }
+ else
+ Py_INCREF(d);
+ }
+ else {
+ d = PyObject_GetAttrString(v, "__dict__");
+ if (d == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "vars() argument must have __dict__ attribute");
+ return NULL;
+ }
+ }
+ return d;
}
PyDoc_STRVAR(vars_doc,
@@ -1836,147 +1836,147 @@ With an argument, equivalent to object.__dict__.");
static PyObject*
builtin_sum(PyObject *self, PyObject *args)
{
- PyObject *seq;
- PyObject *result = NULL;
- PyObject *temp, *item, *iter;
-
- if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
- return NULL;
-
- iter = PyObject_GetIter(seq);
- if (iter == NULL)
- return NULL;
-
- if (result == NULL) {
- result = PyLong_FromLong(0);
- if (result == NULL) {
- Py_DECREF(iter);
- return NULL;
- }
- } else {
- /* reject string values for 'start' parameter */
- if (PyUnicode_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "sum() can't sum strings [use ''.join(seq) instead]");
- Py_DECREF(iter);
- return NULL;
- }
- if (PyByteArray_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "sum() can't sum bytes [use b''.join(seq) instead]");
- Py_DECREF(iter);
- return NULL;
- }
-
- Py_INCREF(result);
- }
+ PyObject *seq;
+ PyObject *result = NULL;
+ PyObject *temp, *item, *iter;
+
+ if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
+ return NULL;
+
+ iter = PyObject_GetIter(seq);
+ if (iter == NULL)
+ return NULL;
+
+ if (result == NULL) {
+ result = PyLong_FromLong(0);
+ if (result == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ } else {
+ /* reject string values for 'start' parameter */
+ if (PyUnicode_Check(result)) {
+ PyErr_SetString(PyExc_TypeError,
+ "sum() can't sum strings [use ''.join(seq) instead]");
+ Py_DECREF(iter);
+ return NULL;
+ }
+ if (PyByteArray_Check(result)) {
+ PyErr_SetString(PyExc_TypeError,
+ "sum() can't sum bytes [use b''.join(seq) instead]");
+ Py_DECREF(iter);
+ return NULL;
+ }
+
+ Py_INCREF(result);
+ }
#ifndef SLOW_SUM
- /* Fast addition by keeping temporary sums in C instead of new Python objects.
- Assumes all inputs are the same type. If the assumption fails, default
- to the more general routine.
- */
- if (PyLong_CheckExact(result)) {
- int overflow;
- long i_result = PyLong_AsLongAndOverflow(result, &overflow);
- /* If this already overflowed, don't even enter the loop. */
- if (overflow == 0) {
- Py_DECREF(result);
- result = NULL;
- }
- while(result == NULL) {
- item = PyIter_Next(iter);
- if (item == NULL) {
- Py_DECREF(iter);
- if (PyErr_Occurred())
- return NULL;
- return PyLong_FromLong(i_result);
- }
- if (PyLong_CheckExact(item)) {
- long b = PyLong_AsLongAndOverflow(item, &overflow);
- long x = i_result + b;
- if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
- i_result = x;
- Py_DECREF(item);
- continue;
- }
- }
- /* Either overflowed or is not an int. Restore real objects and process normally */
- result = PyLong_FromLong(i_result);
- temp = PyNumber_Add(result, item);
- Py_DECREF(result);
- Py_DECREF(item);
- result = temp;
- if (result == NULL) {
- Py_DECREF(iter);
- return NULL;
- }
- }
- }
-
- if (PyFloat_CheckExact(result)) {
- double f_result = PyFloat_AS_DOUBLE(result);
- Py_DECREF(result);
- result = NULL;
- while(result == NULL) {
- item = PyIter_Next(iter);
- if (item == NULL) {
- Py_DECREF(iter);
- if (PyErr_Occurred())
- return NULL;
- return PyFloat_FromDouble(f_result);
- }
- if (PyFloat_CheckExact(item)) {
- PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
- f_result += PyFloat_AS_DOUBLE(item);
- PyFPE_END_PROTECT(f_result)
- Py_DECREF(item);
- continue;
- }
- if (PyLong_CheckExact(item)) {
- long value;
- int overflow;
- value = PyLong_AsLongAndOverflow(item, &overflow);
- if (!overflow) {
- PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
- f_result += (double)value;
- PyFPE_END_PROTECT(f_result)
- Py_DECREF(item);
- continue;
- }
- }
- result = PyFloat_FromDouble(f_result);
- temp = PyNumber_Add(result, item);
- Py_DECREF(result);
- Py_DECREF(item);
- result = temp;
- if (result == NULL) {
- Py_DECREF(iter);
- return NULL;
- }
- }
- }
+ /* Fast addition by keeping temporary sums in C instead of new Python objects.
+ Assumes all inputs are the same type. If the assumption fails, default
+ to the more general routine.
+ */
+ if (PyLong_CheckExact(result)) {
+ int overflow;
+ long i_result = PyLong_AsLongAndOverflow(result, &overflow);
+ /* If this already overflowed, don't even enter the loop. */
+ if (overflow == 0) {
+ Py_DECREF(result);
+ result = NULL;
+ }
+ while(result == NULL) {
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ Py_DECREF(iter);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyLong_FromLong(i_result);
+ }
+ if (PyLong_CheckExact(item)) {
+ long b = PyLong_AsLongAndOverflow(item, &overflow);
+ long x = i_result + b;
+ if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
+ i_result = x;
+ Py_DECREF(item);
+ continue;
+ }
+ }
+ /* Either overflowed or is not an int. Restore real objects and process normally */
+ result = PyLong_FromLong(i_result);
+ temp = PyNumber_Add(result, item);
+ Py_DECREF(result);
+ Py_DECREF(item);
+ result = temp;
+ if (result == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ }
+ }
+
+ if (PyFloat_CheckExact(result)) {
+ double f_result = PyFloat_AS_DOUBLE(result);
+ Py_DECREF(result);
+ result = NULL;
+ while(result == NULL) {
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ Py_DECREF(iter);
+ if (PyErr_Occurred())
+ return NULL;
+ return PyFloat_FromDouble(f_result);
+ }
+ if (PyFloat_CheckExact(item)) {
+ PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
+ f_result += PyFloat_AS_DOUBLE(item);
+ PyFPE_END_PROTECT(f_result)
+ Py_DECREF(item);
+ continue;
+ }
+ if (PyLong_CheckExact(item)) {
+ long value;
+ int overflow;
+ value = PyLong_AsLongAndOverflow(item, &overflow);
+ if (!overflow) {
+ PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
+ f_result += (double)value;
+ PyFPE_END_PROTECT(f_result)
+ Py_DECREF(item);
+ continue;
+ }
+ }
+ result = PyFloat_FromDouble(f_result);
+ temp = PyNumber_Add(result, item);
+ Py_DECREF(result);
+ Py_DECREF(item);
+ result = temp;
+ if (result == NULL) {
+ Py_DECREF(iter);
+ return NULL;
+ }
+ }
+ }
#endif
- for(;;) {
- item = PyIter_Next(iter);
- if (item == NULL) {
- /* error, or end-of-sequence */
- if (PyErr_Occurred()) {
- Py_DECREF(result);
- result = NULL;
- }
- break;
- }
- temp = PyNumber_Add(result, item);
- Py_DECREF(result);
- Py_DECREF(item);
- result = temp;
- if (result == NULL)
- break;
- }
- Py_DECREF(iter);
- return result;
+ for(;;) {
+ item = PyIter_Next(iter);
+ if (item == NULL) {
+ /* error, or end-of-sequence */
+ if (PyErr_Occurred()) {
+ Py_DECREF(result);
+ result = NULL;
+ }
+ break;
+ }
+ temp = PyNumber_Add(result, item);
+ Py_DECREF(result);
+ Py_DECREF(item);
+ result = temp;
+ if (result == NULL)
+ break;
+ }
+ Py_DECREF(iter);
+ return result;
}
PyDoc_STRVAR(sum_doc,
@@ -1990,17 +1990,17 @@ empty, returns start.");
static PyObject *
builtin_isinstance(PyObject *self, PyObject *args)
{
- PyObject *inst;
- PyObject *cls;
- int retval;
+ PyObject *inst;
+ PyObject *cls;
+ int retval;
- if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
+ return NULL;
- retval = PyObject_IsInstance(inst, cls);
- if (retval < 0)
- return NULL;
- return PyBool_FromLong(retval);
+ retval = PyObject_IsInstance(inst, cls);
+ if (retval < 0)
+ return NULL;
+ return PyBool_FromLong(retval);
}
PyDoc_STRVAR(isinstance_doc,
@@ -2015,17 +2015,17 @@ isinstance(x, A) or isinstance(x, B) or ... (etc.).");
static PyObject *
builtin_issubclass(PyObject *self, PyObject *args)
{
- PyObject *derived;
- PyObject *cls;
- int retval;
+ PyObject *derived;
+ PyObject *cls;
+ int retval;
- if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
- return NULL;
+ if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
+ return NULL;
- retval = PyObject_IsSubclass(derived, cls);
- if (retval < 0)
- return NULL;
- return PyBool_FromLong(retval);
+ retval = PyObject_IsSubclass(derived, cls);
+ if (retval < 0)
+ return NULL;
+ return PyBool_FromLong(retval);
}
PyDoc_STRVAR(issubclass_doc,
@@ -2037,127 +2037,127 @@ is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
typedef struct {
- PyObject_HEAD
- Py_ssize_t tuplesize;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
+ PyObject_HEAD
+ Py_ssize_t tuplesize;
+ PyObject *ittuple; /* tuple of iterators */
+ PyObject *result;
} zipobject;
static PyObject *
zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- zipobject *lz;
- Py_ssize_t i;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
- Py_ssize_t tuplesize = PySequence_Length(args);
-
- if (type == &PyZip_Type && !_PyArg_NoKeywords("zip()", kwds))
- return NULL;
-
- /* args must be a tuple */
- assert(PyTuple_Check(args));
-
- /* obtain iterators */
- ittuple = PyTuple_New(tuplesize);
- if (ittuple == NULL)
- return NULL;
- for (i=0; i < tuplesize; ++i) {
- PyObject *item = PyTuple_GET_ITEM(args, i);
- PyObject *it = PyObject_GetIter(item);
- if (it == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError,
- "zip argument #%zd must support iteration",
- i+1);
- Py_DECREF(ittuple);
- return NULL;
- }
- PyTuple_SET_ITEM(ittuple, i, it);
- }
-
- /* create a result holder */
- result = PyTuple_New(tuplesize);
- if (result == NULL) {
- Py_DECREF(ittuple);
- return NULL;
- }
- for (i=0 ; i < tuplesize ; i++) {
- Py_INCREF(Py_None);
- PyTuple_SET_ITEM(result, i, Py_None);
- }
-
- /* create zipobject structure */
- lz = (zipobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(ittuple);
- Py_DECREF(result);
- return NULL;
- }
- lz->ittuple = ittuple;
- lz->tuplesize = tuplesize;
- lz->result = result;
-
- return (PyObject *)lz;
+ zipobject *lz;
+ Py_ssize_t i;
+ PyObject *ittuple; /* tuple of iterators */
+ PyObject *result;
+ Py_ssize_t tuplesize = PySequence_Length(args);
+
+ if (type == &PyZip_Type && !_PyArg_NoKeywords("zip()", kwds))
+ return NULL;
+
+ /* args must be a tuple */
+ assert(PyTuple_Check(args));
+
+ /* obtain iterators */
+ ittuple = PyTuple_New(tuplesize);
+ if (ittuple == NULL)
+ return NULL;
+ for (i=0; i < tuplesize; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(args, i);
+ PyObject *it = PyObject_GetIter(item);
+ if (it == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "zip argument #%zd must support iteration",
+ i+1);
+ Py_DECREF(ittuple);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(ittuple, i, it);
+ }
+
+ /* create a result holder */
+ result = PyTuple_New(tuplesize);
+ if (result == NULL) {
+ Py_DECREF(ittuple);
+ return NULL;
+ }
+ for (i=0 ; i < tuplesize ; i++) {
+ Py_INCREF(Py_None);
+ PyTuple_SET_ITEM(result, i, Py_None);
+ }
+
+ /* create zipobject structure */
+ lz = (zipobject *)type->tp_alloc(type, 0);
+ if (lz == NULL) {
+ Py_DECREF(ittuple);
+ Py_DECREF(result);
+ return NULL;
+ }
+ lz->ittuple = ittuple;
+ lz->tuplesize = tuplesize;
+ lz->result = result;
+
+ return (PyObject *)lz;
}
static void
zip_dealloc(zipobject *lz)
{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->ittuple);
- Py_XDECREF(lz->result);
- Py_TYPE(lz)->tp_free(lz);
+ PyObject_GC_UnTrack(lz);
+ Py_XDECREF(lz->ittuple);
+ Py_XDECREF(lz->result);
+ Py_TYPE(lz)->tp_free(lz);
}
static int
zip_traverse(zipobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->ittuple);
- Py_VISIT(lz->result);
- return 0;
+ Py_VISIT(lz->ittuple);
+ Py_VISIT(lz->result);
+ return 0;
}
static PyObject *
zip_next(zipobject *lz)
{
- Py_ssize_t i;
- Py_ssize_t tuplesize = lz->tuplesize;
- PyObject *result = lz->result;
- PyObject *it;
- PyObject *item;
- PyObject *olditem;
-
- if (tuplesize == 0)
- return NULL;
- if (Py_REFCNT(result) == 1) {
- Py_INCREF(result);
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- item = (*Py_TYPE(it)->tp_iternext)(it);
- if (item == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- olditem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, item);
- Py_DECREF(olditem);
- }
- } else {
- result = PyTuple_New(tuplesize);
- if (result == NULL)
- return NULL;
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- item = (*Py_TYPE(it)->tp_iternext)(it);
- if (item == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SET_ITEM(result, i, item);
- }
- }
- return result;
+ Py_ssize_t i;
+ Py_ssize_t tuplesize = lz->tuplesize;
+ PyObject *result = lz->result;
+ PyObject *it;
+ PyObject *item;
+ PyObject *olditem;
+
+ if (tuplesize == 0)
+ return NULL;
+ if (Py_REFCNT(result) == 1) {
+ Py_INCREF(result);
+ for (i=0 ; i < tuplesize ; i++) {
+ it = PyTuple_GET_ITEM(lz->ittuple, i);
+ item = (*Py_TYPE(it)->tp_iternext)(it);
+ if (item == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ olditem = PyTuple_GET_ITEM(result, i);
+ PyTuple_SET_ITEM(result, i, item);
+ Py_DECREF(olditem);
+ }
+ } else {
+ result = PyTuple_New(tuplesize);
+ if (result == NULL)
+ return NULL;
+ for (i=0 ; i < tuplesize ; i++) {
+ it = PyTuple_GET_ITEM(lz->ittuple, i);
+ item = (*Py_TYPE(it)->tp_iternext)(it);
+ if (item == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(result, i, item);
+ }
+ }
+ return result;
}
PyDoc_STRVAR(zip_doc,
@@ -2169,93 +2169,93 @@ method continues until the shortest iterable in the argument sequence\n\
is exhausted and then it raises StopIteration.");
PyTypeObject PyZip_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "zip", /* tp_name */
- sizeof(zipobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)zip_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- zip_doc, /* tp_doc */
- (traverseproc)zip_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)zip_next, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- zip_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "zip", /* tp_name */
+ sizeof(zipobject), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ /* methods */
+ (destructor)zip_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ zip_doc, /* tp_doc */
+ (traverseproc)zip_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
+ (iternextfunc)zip_next, /* tp_iternext */
+ 0, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ PyType_GenericAlloc, /* tp_alloc */
+ zip_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
static PyMethodDef builtin_methods[] = {
- {"__build_class__", (PyCFunction)builtin___build_class__,
- METH_VARARGS | METH_KEYWORDS, build_class_doc},
- {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
- {"abs", builtin_abs, METH_O, abs_doc},
- {"all", builtin_all, METH_O, all_doc},
- {"any", builtin_any, METH_O, any_doc},
- {"ascii", builtin_ascii, METH_O, ascii_doc},
- {"bin", builtin_bin, METH_O, bin_doc},
- {"chr", builtin_chr, METH_VARARGS, chr_doc},
- {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
- {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
- {"dir", builtin_dir, METH_VARARGS, dir_doc},
- {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
- {"eval", builtin_eval, METH_VARARGS, eval_doc},
- {"exec", builtin_exec, METH_VARARGS, exec_doc},
- {"format", builtin_format, METH_VARARGS, format_doc},
- {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
- {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
- {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
- {"hash", builtin_hash, METH_O, hash_doc},
- {"hex", builtin_hex, METH_O, hex_doc},
- {"id", builtin_id, METH_O, id_doc},
- {"input", builtin_input, METH_VARARGS, input_doc},
- {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
- {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
- {"iter", builtin_iter, METH_VARARGS, iter_doc},
- {"len", builtin_len, METH_O, len_doc},
- {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
- {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
- {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
- {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
- {"oct", builtin_oct, METH_O, oct_doc},
- {"ord", builtin_ord, METH_O, ord_doc},
- {"pow", builtin_pow, METH_VARARGS, pow_doc},
- {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
- {"repr", builtin_repr, METH_O, repr_doc},
- {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
- {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
- {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
- {"sum", builtin_sum, METH_VARARGS, sum_doc},
- {"vars", builtin_vars, METH_VARARGS, vars_doc},
- {NULL, NULL},
+ {"__build_class__", (PyCFunction)builtin___build_class__,
+ METH_VARARGS | METH_KEYWORDS, build_class_doc},
+ {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
+ {"abs", builtin_abs, METH_O, abs_doc},
+ {"all", builtin_all, METH_O, all_doc},
+ {"any", builtin_any, METH_O, any_doc},
+ {"ascii", builtin_ascii, METH_O, ascii_doc},
+ {"bin", builtin_bin, METH_O, bin_doc},
+ {"chr", builtin_chr, METH_VARARGS, chr_doc},
+ {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
+ {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
+ {"dir", builtin_dir, METH_VARARGS, dir_doc},
+ {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
+ {"eval", builtin_eval, METH_VARARGS, eval_doc},
+ {"exec", builtin_exec, METH_VARARGS, exec_doc},
+ {"format", builtin_format, METH_VARARGS, format_doc},
+ {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
+ {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
+ {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
+ {"hash", builtin_hash, METH_O, hash_doc},
+ {"hex", builtin_hex, METH_O, hex_doc},
+ {"id", builtin_id, METH_O, id_doc},
+ {"input", builtin_input, METH_VARARGS, input_doc},
+ {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
+ {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
+ {"iter", builtin_iter, METH_VARARGS, iter_doc},
+ {"len", builtin_len, METH_O, len_doc},
+ {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
+ {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
+ {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
+ {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc},
+ {"oct", builtin_oct, METH_O, oct_doc},
+ {"ord", builtin_ord, METH_O, ord_doc},
+ {"pow", builtin_pow, METH_VARARGS, pow_doc},
+ {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
+ {"repr", builtin_repr, METH_O, repr_doc},
+ {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
+ {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
+ {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
+ {"sum", builtin_sum, METH_VARARGS, sum_doc},
+ {"vars", builtin_vars, METH_VARARGS, vars_doc},
+ {NULL, NULL},
};
PyDoc_STRVAR(builtin_doc,
@@ -2264,85 +2264,85 @@ PyDoc_STRVAR(builtin_doc,
Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
static struct PyModuleDef builtinsmodule = {
- PyModuleDef_HEAD_INIT,
- "builtins",
- builtin_doc,
- -1, /* multiple "initialization" just copies the module dict. */
- builtin_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "builtins",
+ builtin_doc,
+ -1, /* multiple "initialization" just copies the module dict. */
+ builtin_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyObject *
_PyBuiltin_Init(void)
{
- PyObject *mod, *dict, *debug;
- mod = PyModule_Create(&builtinsmodule);
- if (mod == NULL)
- return NULL;
- dict = PyModule_GetDict(mod);
+ PyObject *mod, *dict, *debug;
+ mod = PyModule_Create(&builtinsmodule);
+ if (mod == NULL)
+ return NULL;
+ dict = PyModule_GetDict(mod);
#ifdef Py_TRACE_REFS
- /* "builtins" exposes a number of statically allocated objects
- * that, before this code was added in 2.3, never showed up in
- * the list of "all objects" maintained by Py_TRACE_REFS. As a
- * result, programs leaking references to None and False (etc)
- * couldn't be diagnosed by examining sys.getobjects(0).
- */
+ /* "builtins" exposes a number of statically allocated objects
+ * that, before this code was added in 2.3, never showed up in
+ * the list of "all objects" maintained by Py_TRACE_REFS. As a
+ * result, programs leaking references to None and False (etc)
+ * couldn't be diagnosed by examining sys.getobjects(0).
+ */
#define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
#else
#define ADD_TO_ALL(OBJECT) (void)0
#endif
#define SETBUILTIN(NAME, OBJECT) \
- if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
- return NULL; \
- ADD_TO_ALL(OBJECT)
-
- SETBUILTIN("None", Py_None);
- SETBUILTIN("Ellipsis", Py_Ellipsis);
- SETBUILTIN("NotImplemented", Py_NotImplemented);
- SETBUILTIN("False", Py_False);
- SETBUILTIN("True", Py_True);
- SETBUILTIN("bool", &PyBool_Type);
- SETBUILTIN("memoryview", &PyMemoryView_Type);
- SETBUILTIN("bytearray", &PyByteArray_Type);
- SETBUILTIN("bytes", &PyBytes_Type);
- SETBUILTIN("classmethod", &PyClassMethod_Type);
+ if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
+ return NULL; \
+ ADD_TO_ALL(OBJECT)
+
+ SETBUILTIN("None", Py_None);
+ SETBUILTIN("Ellipsis", Py_Ellipsis);
+ SETBUILTIN("NotImplemented", Py_NotImplemented);
+ SETBUILTIN("False", Py_False);
+ SETBUILTIN("True", Py_True);
+ SETBUILTIN("bool", &PyBool_Type);
+ SETBUILTIN("memoryview", &PyMemoryView_Type);
+ SETBUILTIN("bytearray", &PyByteArray_Type);
+ SETBUILTIN("bytes", &PyBytes_Type);
+ SETBUILTIN("classmethod", &PyClassMethod_Type);
#ifndef WITHOUT_COMPLEX
- SETBUILTIN("complex", &PyComplex_Type);
+ SETBUILTIN("complex", &PyComplex_Type);
#endif
- SETBUILTIN("dict", &PyDict_Type);
- SETBUILTIN("enumerate", &PyEnum_Type);
- SETBUILTIN("filter", &PyFilter_Type);
- SETBUILTIN("float", &PyFloat_Type);
- SETBUILTIN("frozenset", &PyFrozenSet_Type);
- SETBUILTIN("property", &PyProperty_Type);
- SETBUILTIN("int", &PyLong_Type);
- SETBUILTIN("list", &PyList_Type);
- SETBUILTIN("map", &PyMap_Type);
- SETBUILTIN("object", &PyBaseObject_Type);
- SETBUILTIN("range", &PyRange_Type);
- SETBUILTIN("reversed", &PyReversed_Type);
- SETBUILTIN("set", &PySet_Type);
- SETBUILTIN("slice", &PySlice_Type);
- SETBUILTIN("staticmethod", &PyStaticMethod_Type);
- SETBUILTIN("str", &PyUnicode_Type);
- SETBUILTIN("super", &PySuper_Type);
- SETBUILTIN("tuple", &PyTuple_Type);
- SETBUILTIN("type", &PyType_Type);
- SETBUILTIN("zip", &PyZip_Type);
- debug = PyBool_FromLong(Py_OptimizeFlag == 0);
- if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
- Py_XDECREF(debug);
- return NULL;
- }
- Py_XDECREF(debug);
-
- return mod;
+ SETBUILTIN("dict", &PyDict_Type);
+ SETBUILTIN("enumerate", &PyEnum_Type);
+ SETBUILTIN("filter", &PyFilter_Type);
+ SETBUILTIN("float", &PyFloat_Type);
+ SETBUILTIN("frozenset", &PyFrozenSet_Type);
+ SETBUILTIN("property", &PyProperty_Type);
+ SETBUILTIN("int", &PyLong_Type);
+ SETBUILTIN("list", &PyList_Type);
+ SETBUILTIN("map", &PyMap_Type);
+ SETBUILTIN("object", &PyBaseObject_Type);
+ SETBUILTIN("range", &PyRange_Type);
+ SETBUILTIN("reversed", &PyReversed_Type);
+ SETBUILTIN("set", &PySet_Type);
+ SETBUILTIN("slice", &PySlice_Type);
+ SETBUILTIN("staticmethod", &PyStaticMethod_Type);
+ SETBUILTIN("str", &PyUnicode_Type);
+ SETBUILTIN("super", &PySuper_Type);
+ SETBUILTIN("tuple", &PyTuple_Type);
+ SETBUILTIN("type", &PyType_Type);
+ SETBUILTIN("zip", &PyZip_Type);
+ debug = PyBool_FromLong(Py_OptimizeFlag == 0);
+ if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
+ Py_XDECREF(debug);
+ return NULL;
+ }
+ Py_XDECREF(debug);
+
+ return mod;
#undef ADD_TO_ALL
#undef SETBUILTIN
}
diff --git a/Python/ceval.c b/Python/ceval.c
index c807ecc..4dfbfc8 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -28,27 +28,27 @@
typedef unsigned long long uint64;
#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
- section should work for GCC on any PowerPC
- platform, irrespective of OS.
- POWER? Who knows :-) */
+ section should work for GCC on any PowerPC
+ platform, irrespective of OS.
+ POWER? Who knows :-) */
#define READ_TIMESTAMP(var) ppc_getcounter(&var)
static void
ppc_getcounter(uint64 *v)
{
- register unsigned long tbu, tb, tbu2;
+ register unsigned long tbu, tb, tbu2;
loop:
- asm volatile ("mftbu %0" : "=r" (tbu) );
- asm volatile ("mftb %0" : "=r" (tb) );
- asm volatile ("mftbu %0" : "=r" (tbu2));
- if (__builtin_expect(tbu != tbu2, 0)) goto loop;
-
- /* The slightly peculiar way of writing the next lines is
- compiled better by GCC than any other way I tried. */
- ((long*)(v))[0] = tbu;
- ((long*)(v))[1] = tb;
+ asm volatile ("mftbu %0" : "=r" (tbu) );
+ asm volatile ("mftb %0" : "=r" (tb) );
+ asm volatile ("mftbu %0" : "=r" (tbu2));
+ if (__builtin_expect(tbu != tbu2, 0)) goto loop;
+
+ /* The slightly peculiar way of writing the next lines is
+ compiled better by GCC than any other way I tried. */
+ ((long*)(v))[0] = tbu;
+ ((long*)(v))[1] = tb;
}
#elif defined(__i386__)
@@ -77,17 +77,17 @@ ppc_getcounter(uint64 *v)
#endif
void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
- uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
+ uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
{
- uint64 intr, inst, loop;
- PyThreadState *tstate = PyThreadState_Get();
- if (!tstate->interp->tscdump)
- return;
- intr = intr1 - intr0;
- inst = inst1 - inst0 - intr;
- loop = loop1 - loop0 - intr;
- fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
- opcode, ticked, inst, loop);
+ uint64 intr, inst, loop;
+ PyThreadState *tstate = PyThreadState_Get();
+ if (!tstate->interp->tscdump)
+ return;
+ intr = intr1 - intr0;
+ inst = inst1 - inst0 - intr;
+ loop = loop1 - loop0 - intr;
+ fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
+ opcode, ticked, inst, loop);
}
#endif
@@ -97,8 +97,8 @@ void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
#ifdef Py_DEBUG
/* For debugging the interpreter: */
-#define LLTRACE 1 /* Low-level trace feature */
-#define CHECKEXC 1 /* Double-check exception checking */
+#define LLTRACE 1 /* Low-level trace feature */
+#define CHECKEXC 1 /* Double-check exception checking */
#endif
typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
@@ -113,7 +113,7 @@ static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
static PyObject * do_call(PyObject *, PyObject ***, int, int);
static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
- PyObject *);
+ PyObject *);
static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
static PyObject * load_args(PyObject ***, int);
#define CALL_FLAG_VAR 1
@@ -124,12 +124,12 @@ static int lltrace;
static int prtrace(PyObject *, char *);
#endif
static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
- int, PyObject *);
+ int, PyObject *);
static int call_trace_protected(Py_tracefunc, PyObject *,
- PyFrameObject *, int, PyObject *);
+ PyFrameObject *, int, PyObject *);
static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
static int maybe_call_line_trace(Py_tracefunc, PyObject *,
- PyFrameObject *, int *, int *, int *);
+ PyFrameObject *, int *, int *, int *);
static PyObject * cmp_outcome(int, PyObject *, PyObject *);
static PyObject * import_from(PyObject *, PyObject *);
@@ -139,14 +139,14 @@ static PyObject * unicode_concatenate(PyObject *, PyObject *,
PyFrameObject *, unsigned char *);
#define NAME_ERROR_MSG \
- "name '%.200s' is not defined"
+ "name '%.200s' is not defined"
#define GLOBAL_NAME_ERROR_MSG \
- "global name '%.200s' is not defined"
+ "global name '%.200s' is not defined"
#define UNBOUNDLOCAL_ERROR_MSG \
- "local variable '%.200s' referenced before assignment"
+ "local variable '%.200s' referenced before assignment"
#define UNBOUNDFREE_ERROR_MSG \
- "free variable '%.200s' referenced before assignment" \
- " in enclosing scope"
+ "free variable '%.200s' referenced before assignment" \
+ " in enclosing scope"
/* Dynamic execution profile */
#ifdef DYNAMIC_EXECUTION_PROFILE
@@ -198,10 +198,10 @@ static int pcall[PCALL_NUM];
PyObject *
PyEval_GetCallStats(PyObject *self)
{
- return Py_BuildValue("iiiiiiiiiii",
- pcall[0], pcall[1], pcall[2], pcall[3],
- pcall[4], pcall[5], pcall[6], pcall[7],
- pcall[8], pcall[9], pcall[10]);
+ return Py_BuildValue("iiiiiiiiiii",
+ pcall[0], pcall[1], pcall[2], pcall[3],
+ pcall[4], pcall[5], pcall[6], pcall[7],
+ pcall[8], pcall[9], pcall[10]);
}
#else
#define PCALL(O)
@@ -209,8 +209,8 @@ PyEval_GetCallStats(PyObject *self)
PyObject *
PyEval_GetCallStats(PyObject *self)
{
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
#endif
@@ -229,52 +229,52 @@ static long main_thread = 0;
int
PyEval_ThreadsInitialized(void)
{
- return interpreter_lock != 0;
+ return interpreter_lock != 0;
}
void
PyEval_InitThreads(void)
{
- if (interpreter_lock)
- return;
- interpreter_lock = PyThread_allocate_lock();
- PyThread_acquire_lock(interpreter_lock, 1);
- main_thread = PyThread_get_thread_ident();
+ if (interpreter_lock)
+ return;
+ interpreter_lock = PyThread_allocate_lock();
+ PyThread_acquire_lock(interpreter_lock, 1);
+ main_thread = PyThread_get_thread_ident();
}
void
PyEval_AcquireLock(void)
{
- PyThread_acquire_lock(interpreter_lock, 1);
+ PyThread_acquire_lock(interpreter_lock, 1);
}
void
PyEval_ReleaseLock(void)
{
- PyThread_release_lock(interpreter_lock);
+ PyThread_release_lock(interpreter_lock);
}
void
PyEval_AcquireThread(PyThreadState *tstate)
{
- if (tstate == NULL)
- Py_FatalError("PyEval_AcquireThread: NULL new thread state");
- /* Check someone has called PyEval_InitThreads() to create the lock */
- assert(interpreter_lock);
- PyThread_acquire_lock(interpreter_lock, 1);
- if (PyThreadState_Swap(tstate) != NULL)
- Py_FatalError(
- "PyEval_AcquireThread: non-NULL old thread state");
+ if (tstate == NULL)
+ Py_FatalError("PyEval_AcquireThread: NULL new thread state");
+ /* Check someone has called PyEval_InitThreads() to create the lock */
+ assert(interpreter_lock);
+ PyThread_acquire_lock(interpreter_lock, 1);
+ if (PyThreadState_Swap(tstate) != NULL)
+ Py_FatalError(
+ "PyEval_AcquireThread: non-NULL old thread state");
}
void
PyEval_ReleaseThread(PyThreadState *tstate)
{
- if (tstate == NULL)
- Py_FatalError("PyEval_ReleaseThread: NULL thread state");
- if (PyThreadState_Swap(NULL) != tstate)
- Py_FatalError("PyEval_ReleaseThread: wrong thread state");
- PyThread_release_lock(interpreter_lock);
+ if (tstate == NULL)
+ Py_FatalError("PyEval_ReleaseThread: NULL thread state");
+ if (PyThreadState_Swap(NULL) != tstate)
+ Py_FatalError("PyEval_ReleaseThread: wrong thread state");
+ PyThread_release_lock(interpreter_lock);
}
/* This function is called from PyOS_AfterFork to ensure that newly
@@ -285,36 +285,36 @@ PyEval_ReleaseThread(PyThreadState *tstate)
void
PyEval_ReInitThreads(void)
{
- PyObject *threading, *result;
- PyThreadState *tstate;
-
- if (!interpreter_lock)
- return;
- /*XXX Can't use PyThread_free_lock here because it does too
- much error-checking. Doing this cleanly would require
- adding a new function to each thread_*.h. Instead, just
- create a new lock and waste a little bit of memory */
- interpreter_lock = PyThread_allocate_lock();
- pending_lock = PyThread_allocate_lock();
- PyThread_acquire_lock(interpreter_lock, 1);
- main_thread = PyThread_get_thread_ident();
-
- /* Update the threading module with the new state.
- */
- tstate = PyThreadState_GET();
- threading = PyMapping_GetItemString(tstate->interp->modules,
- "threading");
- if (threading == NULL) {
- /* threading not imported */
- PyErr_Clear();
- return;
- }
- result = PyObject_CallMethod(threading, "_after_fork", NULL);
- if (result == NULL)
- PyErr_WriteUnraisable(threading);
- else
- Py_DECREF(result);
- Py_DECREF(threading);
+ PyObject *threading, *result;
+ PyThreadState *tstate;
+
+ if (!interpreter_lock)
+ return;
+ /*XXX Can't use PyThread_free_lock here because it does too
+ much error-checking. Doing this cleanly would require
+ adding a new function to each thread_*.h. Instead, just
+ create a new lock and waste a little bit of memory */
+ interpreter_lock = PyThread_allocate_lock();
+ pending_lock = PyThread_allocate_lock();
+ PyThread_acquire_lock(interpreter_lock, 1);
+ main_thread = PyThread_get_thread_ident();
+
+ /* Update the threading module with the new state.
+ */
+ tstate = PyThreadState_GET();
+ threading = PyMapping_GetItemString(tstate->interp->modules,
+ "threading");
+ if (threading == NULL) {
+ /* threading not imported */
+ PyErr_Clear();
+ return;
+ }
+ result = PyObject_CallMethod(threading, "_after_fork", NULL);
+ if (result == NULL)
+ PyErr_WriteUnraisable(threading);
+ else
+ Py_DECREF(result);
+ Py_DECREF(threading);
}
#endif
@@ -325,29 +325,29 @@ PyEval_ReInitThreads(void)
PyThreadState *
PyEval_SaveThread(void)
{
- PyThreadState *tstate = PyThreadState_Swap(NULL);
- if (tstate == NULL)
- Py_FatalError("PyEval_SaveThread: NULL tstate");
+ PyThreadState *tstate = PyThreadState_Swap(NULL);
+ if (tstate == NULL)
+ Py_FatalError("PyEval_SaveThread: NULL tstate");
#ifdef WITH_THREAD
- if (interpreter_lock)
- PyThread_release_lock(interpreter_lock);
+ if (interpreter_lock)
+ PyThread_release_lock(interpreter_lock);
#endif
- return tstate;
+ return tstate;
}
void
PyEval_RestoreThread(PyThreadState *tstate)
{
- if (tstate == NULL)
- Py_FatalError("PyEval_RestoreThread: NULL tstate");
+ if (tstate == NULL)
+ Py_FatalError("PyEval_RestoreThread: NULL tstate");
#ifdef WITH_THREAD
- if (interpreter_lock) {
- int err = errno;
- PyThread_acquire_lock(interpreter_lock, 1);
- errno = err;
- }
+ if (interpreter_lock) {
+ int err = errno;
+ PyThread_acquire_lock(interpreter_lock, 1);
+ errno = err;
+ }
#endif
- PyThreadState_Swap(tstate);
+ PyThreadState_Swap(tstate);
}
@@ -384,8 +384,8 @@ PyEval_RestoreThread(PyThreadState *tstate)
#define NPENDINGCALLS 32
static struct {
- int (*func)(void *);
- void *arg;
+ int (*func)(void *);
+ void *arg;
} pendingcalls[NPENDINGCALLS];
static int pendingfirst = 0;
static int pendinglast = 0;
@@ -395,93 +395,93 @@ static char pendingbusy = 0;
int
Py_AddPendingCall(int (*func)(void *), void *arg)
{
- int i, j, result=0;
- PyThread_type_lock lock = pending_lock;
-
- /* try a few times for the lock. Since this mechanism is used
- * for signal handling (on the main thread), there is a (slim)
- * chance that a signal is delivered on the same thread while we
- * hold the lock during the Py_MakePendingCalls() function.
- * This avoids a deadlock in that case.
- * Note that signals can be delivered on any thread. In particular,
- * on Windows, a SIGINT is delivered on a system-created worker
- * thread.
- * We also check for lock being NULL, in the unlikely case that
- * this function is called before any bytecode evaluation takes place.
- */
- if (lock != NULL) {
- for (i = 0; i<100; i++) {
- if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
- break;
- }
- if (i == 100)
- return -1;
- }
-
- i = pendinglast;
- j = (i + 1) % NPENDINGCALLS;
- if (j == pendingfirst) {
- result = -1; /* Queue full */
- } else {
- pendingcalls[i].func = func;
- pendingcalls[i].arg = arg;
- pendinglast = j;
- }
- /* signal main loop */
- _Py_Ticker = 0;
- pendingcalls_to_do = 1;
- if (lock != NULL)
- PyThread_release_lock(lock);
- return result;
+ int i, j, result=0;
+ PyThread_type_lock lock = pending_lock;
+
+ /* try a few times for the lock. Since this mechanism is used
+ * for signal handling (on the main thread), there is a (slim)
+ * chance that a signal is delivered on the same thread while we
+ * hold the lock during the Py_MakePendingCalls() function.
+ * This avoids a deadlock in that case.
+ * Note that signals can be delivered on any thread. In particular,
+ * on Windows, a SIGINT is delivered on a system-created worker
+ * thread.
+ * We also check for lock being NULL, in the unlikely case that
+ * this function is called before any bytecode evaluation takes place.
+ */
+ if (lock != NULL) {
+ for (i = 0; i<100; i++) {
+ if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
+ break;
+ }
+ if (i == 100)
+ return -1;
+ }
+
+ i = pendinglast;
+ j = (i + 1) % NPENDINGCALLS;
+ if (j == pendingfirst) {
+ result = -1; /* Queue full */
+ } else {
+ pendingcalls[i].func = func;
+ pendingcalls[i].arg = arg;
+ pendinglast = j;
+ }
+ /* signal main loop */
+ _Py_Ticker = 0;
+ pendingcalls_to_do = 1;
+ if (lock != NULL)
+ PyThread_release_lock(lock);
+ return result;
}
int
Py_MakePendingCalls(void)
{
- int i;
- int r = 0;
-
- if (!pending_lock) {
- /* initial allocation of the lock */
- pending_lock = PyThread_allocate_lock();
- if (pending_lock == NULL)
- return -1;
- }
-
- /* only service pending calls on main thread */
- if (main_thread && PyThread_get_thread_ident() != main_thread)
- return 0;
- /* don't perform recursive pending calls */
- if (pendingbusy)
- return 0;
- pendingbusy = 1;
- /* perform a bounded number of calls, in case of recursion */
- for (i=0; i<NPENDINGCALLS; i++) {
- int j;
- int (*func)(void *);
- void *arg = NULL;
-
- /* pop one item off the queue while holding the lock */
- PyThread_acquire_lock(pending_lock, WAIT_LOCK);
- j = pendingfirst;
- if (j == pendinglast) {
- func = NULL; /* Queue empty */
- } else {
- func = pendingcalls[j].func;
- arg = pendingcalls[j].arg;
- pendingfirst = (j + 1) % NPENDINGCALLS;
- }
- pendingcalls_to_do = pendingfirst != pendinglast;
- PyThread_release_lock(pending_lock);
- /* having released the lock, perform the callback */
- if (func == NULL)
- break;
- r = func(arg);
- if (r)
- break;
- }
- pendingbusy = 0;
- return r;
+ int i;
+ int r = 0;
+
+ if (!pending_lock) {
+ /* initial allocation of the lock */
+ pending_lock = PyThread_allocate_lock();
+ if (pending_lock == NULL)
+ return -1;
+ }
+
+ /* only service pending calls on main thread */
+ if (main_thread && PyThread_get_thread_ident() != main_thread)
+ return 0;
+ /* don't perform recursive pending calls */
+ if (pendingbusy)
+ return 0;
+ pendingbusy = 1;
+ /* perform a bounded number of calls, in case of recursion */
+ for (i=0; i<NPENDINGCALLS; i++) {
+ int j;
+ int (*func)(void *);
+ void *arg = NULL;
+
+ /* pop one item off the queue while holding the lock */
+ PyThread_acquire_lock(pending_lock, WAIT_LOCK);
+ j = pendingfirst;
+ if (j == pendinglast) {
+ func = NULL; /* Queue empty */
+ } else {
+ func = pendingcalls[j].func;
+ arg = pendingcalls[j].arg;
+ pendingfirst = (j + 1) % NPENDINGCALLS;
+ }
+ pendingcalls_to_do = pendingfirst != pendinglast;
+ PyThread_release_lock(pending_lock);
+ /* having released the lock, perform the callback */
+ if (func == NULL)
+ break;
+ r = func(arg);
+ if (r)
+ break;
+ }
+ pendingbusy = 0;
+ return r;
}
#else /* if ! defined WITH_THREAD */
@@ -492,11 +492,11 @@ Py_MakePendingCalls(void)
with WITH_THREAD.
Don't use this implementation when Py_AddPendingCalls() can happen
on a different thread!
-
+
There are two possible race conditions:
(1) nested asynchronous calls to Py_AddPendingCall()
(2) AddPendingCall() calls made while pending calls are being processed.
-
+
(1) is very unlikely because typically signal delivery
is blocked during signal handling. So it should be impossible.
(2) is a real possibility.
@@ -511,8 +511,8 @@ Py_MakePendingCalls(void)
#define NPENDINGCALLS 32
static struct {
- int (*func)(void *);
- void *arg;
+ int (*func)(void *);
+ void *arg;
} pendingcalls[NPENDINGCALLS];
static volatile int pendingfirst = 0;
static volatile int pendinglast = 0;
@@ -521,55 +521,55 @@ static volatile int pendingcalls_to_do = 0;
int
Py_AddPendingCall(int (*func)(void *), void *arg)
{
- static volatile int busy = 0;
- int i, j;
- /* XXX Begin critical section */
- if (busy)
- return -1;
- busy = 1;
- i = pendinglast;
- j = (i + 1) % NPENDINGCALLS;
- if (j == pendingfirst) {
- busy = 0;
- return -1; /* Queue full */
- }
- pendingcalls[i].func = func;
- pendingcalls[i].arg = arg;
- pendinglast = j;
-
- _Py_Ticker = 0;
- pendingcalls_to_do = 1; /* Signal main loop */
- busy = 0;
- /* XXX End critical section */
- return 0;
+ static volatile int busy = 0;
+ int i, j;
+ /* XXX Begin critical section */
+ if (busy)
+ return -1;
+ busy = 1;
+ i = pendinglast;
+ j = (i + 1) % NPENDINGCALLS;
+ if (j == pendingfirst) {
+ busy = 0;
+ return -1; /* Queue full */
+ }
+ pendingcalls[i].func = func;
+ pendingcalls[i].arg = arg;
+ pendinglast = j;
+
+ _Py_Ticker = 0;
+ pendingcalls_to_do = 1; /* Signal main loop */
+ busy = 0;
+ /* XXX End critical section */
+ return 0;
}
int
Py_MakePendingCalls(void)
{
- static int busy = 0;
- if (busy)
- return 0;
- busy = 1;
- pendingcalls_to_do = 0;
- for (;;) {
- int i;
- int (*func)(void *);
- void *arg;
- i = pendingfirst;
- if (i == pendinglast)
- break; /* Queue empty */
- func = pendingcalls[i].func;
- arg = pendingcalls[i].arg;
- pendingfirst = (i + 1) % NPENDINGCALLS;
- if (func(arg) < 0) {
- busy = 0;
- pendingcalls_to_do = 1; /* We're not done yet */
- return -1;
- }
- }
- busy = 0;
- return 0;
+ static int busy = 0;
+ if (busy)
+ return 0;
+ busy = 1;
+ pendingcalls_to_do = 0;
+ for (;;) {
+ int i;
+ int (*func)(void *);
+ void *arg;
+ i = pendingfirst;
+ if (i == pendinglast)
+ break; /* Queue empty */
+ func = pendingcalls[i].func;
+ arg = pendingcalls[i].arg;
+ pendingfirst = (i + 1) % NPENDINGCALLS;
+ if (func(arg) < 0) {
+ busy = 0;
+ pendingcalls_to_do = 1; /* We're not done yet */
+ return -1;
+ }
+ }
+ busy = 0;
+ return 0;
}
#endif /* WITH_THREAD */
@@ -586,14 +586,14 @@ int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
int
Py_GetRecursionLimit(void)
{
- return recursion_limit;
+ return recursion_limit;
}
void
Py_SetRecursionLimit(int new_limit)
{
- recursion_limit = new_limit;
- _Py_CheckRecursionLimit = recursion_limit;
+ recursion_limit = new_limit;
+ _Py_CheckRecursionLimit = recursion_limit;
}
/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
@@ -604,47 +604,47 @@ Py_SetRecursionLimit(int new_limit)
int
_Py_CheckRecursiveCall(char *where)
{
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = PyThreadState_GET();
#ifdef USE_STACKCHECK
- if (PyOS_CheckStack()) {
- --tstate->recursion_depth;
- PyErr_SetString(PyExc_MemoryError, "Stack overflow");
- return -1;
- }
+ if (PyOS_CheckStack()) {
+ --tstate->recursion_depth;
+ PyErr_SetString(PyExc_MemoryError, "Stack overflow");
+ return -1;
+ }
#endif
- _Py_CheckRecursionLimit = recursion_limit;
- if (tstate->recursion_critical)
- /* Somebody asked that we don't check for recursion. */
- return 0;
- if (tstate->overflowed) {
- if (tstate->recursion_depth > recursion_limit + 50) {
- /* Overflowing while handling an overflow. Give up. */
- Py_FatalError("Cannot recover from stack overflow.");
- }
- return 0;
- }
- if (tstate->recursion_depth > recursion_limit) {
- --tstate->recursion_depth;
- tstate->overflowed = 1;
- PyErr_Format(PyExc_RuntimeError,
- "maximum recursion depth exceeded%s",
- where);
- return -1;
- }
- return 0;
+ _Py_CheckRecursionLimit = recursion_limit;
+ if (tstate->recursion_critical)
+ /* Somebody asked that we don't check for recursion. */
+ return 0;
+ if (tstate->overflowed) {
+ if (tstate->recursion_depth > recursion_limit + 50) {
+ /* Overflowing while handling an overflow. Give up. */
+ Py_FatalError("Cannot recover from stack overflow.");
+ }
+ return 0;
+ }
+ if (tstate->recursion_depth > recursion_limit) {
+ --tstate->recursion_depth;
+ tstate->overflowed = 1;
+ PyErr_Format(PyExc_RuntimeError,
+ "maximum recursion depth exceeded%s",
+ where);
+ return -1;
+ }
+ return 0;
}
/* Status code for main loop (reason for stack unwind) */
enum why_code {
- WHY_NOT = 0x0001, /* No error */
- WHY_EXCEPTION = 0x0002, /* Exception occurred */
- WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
- WHY_RETURN = 0x0008, /* 'return' statement */
- WHY_BREAK = 0x0010, /* 'break' statement */
- WHY_CONTINUE = 0x0020, /* 'continue' statement */
- WHY_YIELD = 0x0040, /* 'yield' operator */
- WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
+ WHY_NOT = 0x0001, /* No error */
+ WHY_EXCEPTION = 0x0002, /* Exception occurred */
+ WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
+ WHY_RETURN = 0x0008, /* 'return' statement */
+ WHY_BREAK = 0x0010, /* 'break' statement */
+ WHY_CONTINUE = 0x0020, /* 'continue' statement */
+ WHY_YIELD = 0x0040, /* 'yield' operator */
+ WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
};
static enum why_code do_raise(PyObject *, PyObject *);
@@ -665,12 +665,12 @@ volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
PyObject *
PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
{
- return PyEval_EvalCodeEx(co,
- globals, locals,
- (PyObject **)NULL, 0,
- (PyObject **)NULL, 0,
- (PyObject **)NULL, 0,
- NULL, NULL);
+ return PyEval_EvalCodeEx(co,
+ globals, locals,
+ (PyObject **)NULL, 0,
+ (PyObject **)NULL, 0,
+ (PyObject **)NULL, 0,
+ NULL, NULL);
}
@@ -678,49 +678,49 @@ PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
PyObject *
PyEval_EvalFrame(PyFrameObject *f) {
- /* This is for backward compatibility with extension modules that
- used this API; core interpreter code should call
- PyEval_EvalFrameEx() */
- return PyEval_EvalFrameEx(f, 0);
+ /* This is for backward compatibility with extension modules that
+ used this API; core interpreter code should call
+ PyEval_EvalFrameEx() */
+ return PyEval_EvalFrameEx(f, 0);
}
PyObject *
PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
#ifdef DXPAIRS
- int lastopcode = 0;
+ int lastopcode = 0;
#endif
- register PyObject **stack_pointer; /* Next free slot in value stack */
- register unsigned char *next_instr;
- register int opcode; /* Current opcode */
- register int oparg; /* Current opcode argument, if any */
- register enum why_code why; /* Reason for block stack unwind */
- register int err; /* Error status -- nonzero if error */
- register PyObject *x; /* Result object -- NULL if error */
- register PyObject *v; /* Temporary objects popped off stack */
- register PyObject *w;
- register PyObject *u;
- register PyObject *t;
- register PyObject **fastlocals, **freevars;
- PyObject *retval = NULL; /* Return value */
- PyThreadState *tstate = PyThreadState_GET();
- PyCodeObject *co;
-
- /* when tracing we set things up so that
-
- not (instr_lb <= current_bytecode_offset < instr_ub)
-
- is true when the line being executed has changed. The
- initial values are such as to make this false the first
- time it is tested. */
- int instr_ub = -1, instr_lb = 0, instr_prev = -1;
-
- unsigned char *first_instr;
- PyObject *names;
- PyObject *consts;
+ register PyObject **stack_pointer; /* Next free slot in value stack */
+ register unsigned char *next_instr;
+ register int opcode; /* Current opcode */
+ register int oparg; /* Current opcode argument, if any */
+ register enum why_code why; /* Reason for block stack unwind */
+ register int err; /* Error status -- nonzero if error */
+ register PyObject *x; /* Result object -- NULL if error */
+ register PyObject *v; /* Temporary objects popped off stack */
+ register PyObject *w;
+ register PyObject *u;
+ register PyObject *t;
+ register PyObject **fastlocals, **freevars;
+ PyObject *retval = NULL; /* Return value */
+ PyThreadState *tstate = PyThreadState_GET();
+ PyCodeObject *co;
+
+ /* when tracing we set things up so that
+
+ not (instr_lb <= current_bytecode_offset < instr_ub)
+
+ is true when the line being executed has changed. The
+ initial values are such as to make this false the first
+ time it is tested. */
+ int instr_ub = -1, instr_lb = 0, instr_prev = -1;
+
+ unsigned char *first_instr;
+ PyObject *names;
+ PyObject *consts;
#if defined(Py_DEBUG) || defined(LLTRACE)
- /* Make it easier to find out where we are with a debugger */
- char *filename;
+ /* Make it easier to find out where we are with a debugger */
+ char *filename;
#endif
/* Computed GOTOs, or
@@ -729,7 +729,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
(http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
The traditional bytecode evaluation loop uses a "switch" statement, which
- decent compilers will optimize as a single indirect branch instruction
+ decent compilers will optimize as a single indirect branch instruction
combined with a lookup table of jump addresses. However, since the
indirect jump instruction is shared by all opcodes, the CPU will have a
hard time making the right prediction for where to jump next (actually,
@@ -744,7 +744,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
a much better chance to turn out valid, especially in small bytecode loops.
A mispredicted branch on a modern CPU flushes the whole pipeline and
- can cost several CPU cycles (depending on the pipeline depth),
+ can cost several CPU cycles (depending on the pipeline depth),
and potentially many more instructions (depending on the pipeline width).
A correctly predicted branch, however, is nearly free.
@@ -773,59 +773,59 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
/* This macro is used when several opcodes defer to the same implementation
(e.g. SETUP_LOOP, SETUP_FINALLY) */
#define TARGET_WITH_IMPL(op, impl) \
- TARGET_##op: \
- opcode = op; \
- if (HAS_ARG(op)) \
- oparg = NEXTARG(); \
- case op: \
- goto impl; \
+ TARGET_##op: \
+ opcode = op; \
+ if (HAS_ARG(op)) \
+ oparg = NEXTARG(); \
+ case op: \
+ goto impl; \
#define TARGET(op) \
- TARGET_##op: \
- opcode = op; \
- if (HAS_ARG(op)) \
- oparg = NEXTARG(); \
- case op:
+ TARGET_##op: \
+ opcode = op; \
+ if (HAS_ARG(op)) \
+ oparg = NEXTARG(); \
+ case op:
#define DISPATCH() \
- { \
- /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
- int _tick = _Py_Ticker - 1; \
- _Py_Ticker = _tick; \
- if (_tick >= 0) { \
- FAST_DISPATCH(); \
- } \
- continue; \
- }
+ { \
+ /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
+ int _tick = _Py_Ticker - 1; \
+ _Py_Ticker = _tick; \
+ if (_tick >= 0) { \
+ FAST_DISPATCH(); \
+ } \
+ continue; \
+ }
#ifdef LLTRACE
#define FAST_DISPATCH() \
- { \
- if (!lltrace && !_Py_TracingPossible) { \
- f->f_lasti = INSTR_OFFSET(); \
- goto *opcode_targets[*next_instr++]; \
- } \
- goto fast_next_opcode; \
- }
+ { \
+ if (!lltrace && !_Py_TracingPossible) { \
+ f->f_lasti = INSTR_OFFSET(); \
+ goto *opcode_targets[*next_instr++]; \
+ } \
+ goto fast_next_opcode; \
+ }
#else
#define FAST_DISPATCH() \
- { \
- if (!_Py_TracingPossible) { \
- f->f_lasti = INSTR_OFFSET(); \
- goto *opcode_targets[*next_instr++]; \
- } \
- goto fast_next_opcode; \
- }
+ { \
+ if (!_Py_TracingPossible) { \
+ f->f_lasti = INSTR_OFFSET(); \
+ goto *opcode_targets[*next_instr++]; \
+ } \
+ goto fast_next_opcode; \
+ }
#endif
#else
#define TARGET(op) \
- case op:
+ case op:
#define TARGET_WITH_IMPL(op, impl) \
- /* silence compiler warnings about `impl` unused */ \
- if (0) goto impl; \
- case op:
+ /* silence compiler warnings about `impl` unused */ \
+ if (0) goto impl; \
+ case op:
#define DISPATCH() continue
#define FAST_DISPATCH() goto fast_next_opcode
#endif
@@ -867,62 +867,62 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
CALL_FUNCTION (and friends)
*/
- uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
- int ticked = 0;
+ uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
+ int ticked = 0;
- READ_TIMESTAMP(inst0);
- READ_TIMESTAMP(inst1);
- READ_TIMESTAMP(loop0);
- READ_TIMESTAMP(loop1);
+ READ_TIMESTAMP(inst0);
+ READ_TIMESTAMP(inst1);
+ READ_TIMESTAMP(loop0);
+ READ_TIMESTAMP(loop1);
- /* shut up the compiler */
- opcode = 0;
+ /* shut up the compiler */
+ opcode = 0;
#endif
/* Code access macros */
-#define INSTR_OFFSET() ((int)(next_instr - first_instr))
-#define NEXTOP() (*next_instr++)
-#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
-#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
-#define JUMPTO(x) (next_instr = first_instr + (x))
-#define JUMPBY(x) (next_instr += (x))
+#define INSTR_OFFSET() ((int)(next_instr - first_instr))
+#define NEXTOP() (*next_instr++)
+#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
+#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
+#define JUMPTO(x) (next_instr = first_instr + (x))
+#define JUMPBY(x) (next_instr += (x))
/* OpCode prediction macros
- Some opcodes tend to come in pairs thus making it possible to
- predict the second code when the first is run. For example,
- COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
- those opcodes are often followed by a POP_TOP.
-
- Verifying the prediction costs a single high-speed test of a register
- variable against a constant. If the pairing was good, then the
- processor's own internal branch predication has a high likelihood of
- success, resulting in a nearly zero-overhead transition to the
- next opcode. A successful prediction saves a trip through the eval-loop
- including its two unpredictable branches, the HAS_ARG test and the
- switch-case. Combined with the processor's internal branch prediction,
- a successful PREDICT has the effect of making the two opcodes run as if
- they were a single new opcode with the bodies combined.
+ Some opcodes tend to come in pairs thus making it possible to
+ predict the second code when the first is run. For example,
+ COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
+ those opcodes are often followed by a POP_TOP.
+
+ Verifying the prediction costs a single high-speed test of a register
+ variable against a constant. If the pairing was good, then the
+ processor's own internal branch predication has a high likelihood of
+ success, resulting in a nearly zero-overhead transition to the
+ next opcode. A successful prediction saves a trip through the eval-loop
+ including its two unpredictable branches, the HAS_ARG test and the
+ switch-case. Combined with the processor's internal branch prediction,
+ a successful PREDICT has the effect of making the two opcodes run as if
+ they were a single new opcode with the bodies combined.
If collecting opcode statistics, your choices are to either keep the
- predictions turned-on and interpret the results as if some opcodes
- had been combined or turn-off predictions so that the opcode frequency
- counter updates for both opcodes.
+ predictions turned-on and interpret the results as if some opcodes
+ had been combined or turn-off predictions so that the opcode frequency
+ counter updates for both opcodes.
Opcode prediction is disabled with threaded code, since the latter allows
- the CPU to record separate branch prediction information for each
- opcode.
+ the CPU to record separate branch prediction information for each
+ opcode.
*/
#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
-#define PREDICT(op) if (0) goto PRED_##op
-#define PREDICTED(op) PRED_##op:
-#define PREDICTED_WITH_ARG(op) PRED_##op:
+#define PREDICT(op) if (0) goto PRED_##op
+#define PREDICTED(op) PRED_##op:
+#define PREDICTED_WITH_ARG(op) PRED_##op:
#else
-#define PREDICT(op) if (*next_instr == op) goto PRED_##op
-#define PREDICTED(op) PRED_##op: next_instr++
-#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
+#define PREDICT(op) if (*next_instr == op) goto PRED_##op
+#define PREDICTED(op) PRED_##op: next_instr++
+#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
#endif
@@ -930,42 +930,42 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
/* The stack can grow at most MAXINT deep, as co_nlocals and
co_stacksize are ints. */
-#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
-#define EMPTY() (STACK_LEVEL() == 0)
-#define TOP() (stack_pointer[-1])
-#define SECOND() (stack_pointer[-2])
-#define THIRD() (stack_pointer[-3])
-#define FOURTH() (stack_pointer[-4])
-#define SET_TOP(v) (stack_pointer[-1] = (v))
-#define SET_SECOND(v) (stack_pointer[-2] = (v))
-#define SET_THIRD(v) (stack_pointer[-3] = (v))
-#define SET_FOURTH(v) (stack_pointer[-4] = (v))
-#define BASIC_STACKADJ(n) (stack_pointer += n)
-#define BASIC_PUSH(v) (*stack_pointer++ = (v))
-#define BASIC_POP() (*--stack_pointer)
+#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
+#define EMPTY() (STACK_LEVEL() == 0)
+#define TOP() (stack_pointer[-1])
+#define SECOND() (stack_pointer[-2])
+#define THIRD() (stack_pointer[-3])
+#define FOURTH() (stack_pointer[-4])
+#define SET_TOP(v) (stack_pointer[-1] = (v))
+#define SET_SECOND(v) (stack_pointer[-2] = (v))
+#define SET_THIRD(v) (stack_pointer[-3] = (v))
+#define SET_FOURTH(v) (stack_pointer[-4] = (v))
+#define BASIC_STACKADJ(n) (stack_pointer += n)
+#define BASIC_PUSH(v) (*stack_pointer++ = (v))
+#define BASIC_POP() (*--stack_pointer)
#ifdef LLTRACE
-#define PUSH(v) { (void)(BASIC_PUSH(v), \
- lltrace && prtrace(TOP(), "push")); \
- assert(STACK_LEVEL() <= co->co_stacksize); }
-#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
- BASIC_POP())
-#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
- lltrace && prtrace(TOP(), "stackadj")); \
- assert(STACK_LEVEL() <= co->co_stacksize); }
+#define PUSH(v) { (void)(BASIC_PUSH(v), \
+ lltrace && prtrace(TOP(), "push")); \
+ assert(STACK_LEVEL() <= co->co_stacksize); }
+#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
+ BASIC_POP())
+#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
+ lltrace && prtrace(TOP(), "stackadj")); \
+ assert(STACK_LEVEL() <= co->co_stacksize); }
#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
- prtrace((STACK_POINTER)[-1], "ext_pop")), \
- *--(STACK_POINTER))
+ prtrace((STACK_POINTER)[-1], "ext_pop")), \
+ *--(STACK_POINTER))
#else
-#define PUSH(v) BASIC_PUSH(v)
-#define POP() BASIC_POP()
-#define STACKADJ(n) BASIC_STACKADJ(n)
+#define PUSH(v) BASIC_PUSH(v)
+#define POP() BASIC_POP()
+#define STACKADJ(n) BASIC_STACKADJ(n)
#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
#endif
/* Local variable macros */
-#define GETLOCAL(i) (fastlocals[i])
+#define GETLOCAL(i) (fastlocals[i])
/* The SETLOCAL() macro must not DECREF the local variable in-place and
then store the new value; it must copy the old value to a temporary
@@ -973,1962 +973,1962 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
This is because it is possible that during the DECREF the frame is
accessed by other code (e.g. a __del__ method or gc.collect()) and the
variable would be pointing to already-freed memory. */
-#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
- GETLOCAL(i) = value; \
- Py_XDECREF(tmp); } while (0)
+#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
+ GETLOCAL(i) = value; \
+ Py_XDECREF(tmp); } while (0)
#define UNWIND_BLOCK(b) \
- while (STACK_LEVEL() > (b)->b_level) { \
- PyObject *v = POP(); \
- Py_XDECREF(v); \
- }
+ while (STACK_LEVEL() > (b)->b_level) { \
+ PyObject *v = POP(); \
+ Py_XDECREF(v); \
+ }
#define UNWIND_EXCEPT_HANDLER(b) \
- { \
- PyObject *type, *value, *traceback; \
- assert(STACK_LEVEL() >= (b)->b_level + 3); \
- while (STACK_LEVEL() > (b)->b_level + 3) { \
- value = POP(); \
- Py_XDECREF(value); \
- } \
- type = tstate->exc_type; \
- value = tstate->exc_value; \
- traceback = tstate->exc_traceback; \
- tstate->exc_type = POP(); \
- tstate->exc_value = POP(); \
- tstate->exc_traceback = POP(); \
- Py_XDECREF(type); \
- Py_XDECREF(value); \
- Py_XDECREF(traceback); \
- }
+ { \
+ PyObject *type, *value, *traceback; \
+ assert(STACK_LEVEL() >= (b)->b_level + 3); \
+ while (STACK_LEVEL() > (b)->b_level + 3) { \
+ value = POP(); \
+ Py_XDECREF(value); \
+ } \
+ type = tstate->exc_type; \
+ value = tstate->exc_value; \
+ traceback = tstate->exc_traceback; \
+ tstate->exc_type = POP(); \
+ tstate->exc_value = POP(); \
+ tstate->exc_traceback = POP(); \
+ Py_XDECREF(type); \
+ Py_XDECREF(value); \
+ Py_XDECREF(traceback); \
+ }
#define SAVE_EXC_STATE() \
- { \
- PyObject *type, *value, *traceback; \
- Py_XINCREF(tstate->exc_type); \
- Py_XINCREF(tstate->exc_value); \
- Py_XINCREF(tstate->exc_traceback); \
- type = f->f_exc_type; \
- value = f->f_exc_value; \
- traceback = f->f_exc_traceback; \
- f->f_exc_type = tstate->exc_type; \
- f->f_exc_value = tstate->exc_value; \
- f->f_exc_traceback = tstate->exc_traceback; \
- Py_XDECREF(type); \
- Py_XDECREF(value); \
- Py_XDECREF(traceback); \
- }
+ { \
+ PyObject *type, *value, *traceback; \
+ Py_XINCREF(tstate->exc_type); \
+ Py_XINCREF(tstate->exc_value); \
+ Py_XINCREF(tstate->exc_traceback); \
+ type = f->f_exc_type; \
+ value = f->f_exc_value; \
+ traceback = f->f_exc_traceback; \
+ f->f_exc_type = tstate->exc_type; \
+ f->f_exc_value = tstate->exc_value; \
+ f->f_exc_traceback = tstate->exc_traceback; \
+ Py_XDECREF(type); \
+ Py_XDECREF(value); \
+ Py_XDECREF(traceback); \
+ }
#define SWAP_EXC_STATE() \
- { \
- PyObject *tmp; \
- tmp = tstate->exc_type; \
- tstate->exc_type = f->f_exc_type; \
- f->f_exc_type = tmp; \
- tmp = tstate->exc_value; \
- tstate->exc_value = f->f_exc_value; \
- f->f_exc_value = tmp; \
- tmp = tstate->exc_traceback; \
- tstate->exc_traceback = f->f_exc_traceback; \
- f->f_exc_traceback = tmp; \
- }
+ { \
+ PyObject *tmp; \
+ tmp = tstate->exc_type; \
+ tstate->exc_type = f->f_exc_type; \
+ f->f_exc_type = tmp; \
+ tmp = tstate->exc_value; \
+ tstate->exc_value = f->f_exc_value; \
+ f->f_exc_value = tmp; \
+ tmp = tstate->exc_traceback; \
+ tstate->exc_traceback = f->f_exc_traceback; \
+ f->f_exc_traceback = tmp; \
+ }
/* Start of code */
- if (f == NULL)
- return NULL;
-
- /* push frame */
- if (Py_EnterRecursiveCall(""))
- return NULL;
-
- tstate->frame = f;
-
- if (tstate->use_tracing) {
- if (tstate->c_tracefunc != NULL) {
- /* tstate->c_tracefunc, if defined, is a
- function that will be called on *every* entry
- to a code block. Its return value, if not
- None, is a function that will be called at
- the start of each executed line of code.
- (Actually, the function must return itself
- in order to continue tracing.) The trace
- functions are called with three arguments:
- a pointer to the current frame, a string
- indicating why the function is called, and
- an argument which depends on the situation.
- The global trace function is also called
- whenever an exception is detected. */
- if (call_trace_protected(tstate->c_tracefunc,
- tstate->c_traceobj,
- f, PyTrace_CALL, Py_None)) {
- /* Trace function raised an error */
- goto exit_eval_frame;
- }
- }
- if (tstate->c_profilefunc != NULL) {
- /* Similar for c_profilefunc, except it needn't
- return itself and isn't called for "line" events */
- if (call_trace_protected(tstate->c_profilefunc,
- tstate->c_profileobj,
- f, PyTrace_CALL, Py_None)) {
- /* Profile function raised an error */
- goto exit_eval_frame;
- }
- }
- }
-
- co = f->f_code;
- names = co->co_names;
- consts = co->co_consts;
- fastlocals = f->f_localsplus;
- freevars = f->f_localsplus + co->co_nlocals;
- first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
- /* An explanation is in order for the next line.
-
- f->f_lasti now refers to the index of the last instruction
- executed. You might think this was obvious from the name, but
- this wasn't always true before 2.3! PyFrame_New now sets
- f->f_lasti to -1 (i.e. the index *before* the first instruction)
- and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
- does work. Promise.
-
- When the PREDICT() macros are enabled, some opcode pairs follow in
- direct succession without updating f->f_lasti. A successful
- prediction effectively links the two codes together as if they
- were a single new opcode; accordingly,f->f_lasti will point to
- the first code in the pair (for instance, GET_ITER followed by
- FOR_ITER is effectively a single opcode and f->f_lasti will point
- at to the beginning of the combined pair.)
- */
- next_instr = first_instr + f->f_lasti + 1;
- stack_pointer = f->f_stacktop;
- assert(stack_pointer != NULL);
- f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
-
- if (co->co_flags & CO_GENERATOR && !throwflag) {
- if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
- /* We were in an except handler when we left,
- restore the exception state which was put aside
- (see YIELD_VALUE). */
- SWAP_EXC_STATE();
- }
- else {
- SAVE_EXC_STATE();
- }
- }
+ if (f == NULL)
+ return NULL;
+
+ /* push frame */
+ if (Py_EnterRecursiveCall(""))
+ return NULL;
+
+ tstate->frame = f;
+
+ if (tstate->use_tracing) {
+ if (tstate->c_tracefunc != NULL) {
+ /* tstate->c_tracefunc, if defined, is a
+ function that will be called on *every* entry
+ to a code block. Its return value, if not
+ None, is a function that will be called at
+ the start of each executed line of code.
+ (Actually, the function must return itself
+ in order to continue tracing.) The trace
+ functions are called with three arguments:
+ a pointer to the current frame, a string
+ indicating why the function is called, and
+ an argument which depends on the situation.
+ The global trace function is also called
+ whenever an exception is detected. */
+ if (call_trace_protected(tstate->c_tracefunc,
+ tstate->c_traceobj,
+ f, PyTrace_CALL, Py_None)) {
+ /* Trace function raised an error */
+ goto exit_eval_frame;
+ }
+ }
+ if (tstate->c_profilefunc != NULL) {
+ /* Similar for c_profilefunc, except it needn't
+ return itself and isn't called for "line" events */
+ if (call_trace_protected(tstate->c_profilefunc,
+ tstate->c_profileobj,
+ f, PyTrace_CALL, Py_None)) {
+ /* Profile function raised an error */
+ goto exit_eval_frame;
+ }
+ }
+ }
+
+ co = f->f_code;
+ names = co->co_names;
+ consts = co->co_consts;
+ fastlocals = f->f_localsplus;
+ freevars = f->f_localsplus + co->co_nlocals;
+ first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
+ /* An explanation is in order for the next line.
+
+ f->f_lasti now refers to the index of the last instruction
+ executed. You might think this was obvious from the name, but
+ this wasn't always true before 2.3! PyFrame_New now sets
+ f->f_lasti to -1 (i.e. the index *before* the first instruction)
+ and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
+ does work. Promise.
+
+ When the PREDICT() macros are enabled, some opcode pairs follow in
+ direct succession without updating f->f_lasti. A successful
+ prediction effectively links the two codes together as if they
+ were a single new opcode; accordingly,f->f_lasti will point to
+ the first code in the pair (for instance, GET_ITER followed by
+ FOR_ITER is effectively a single opcode and f->f_lasti will point
+ at to the beginning of the combined pair.)
+ */
+ next_instr = first_instr + f->f_lasti + 1;
+ stack_pointer = f->f_stacktop;
+ assert(stack_pointer != NULL);
+ f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
+
+ if (co->co_flags & CO_GENERATOR && !throwflag) {
+ if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
+ /* We were in an except handler when we left,
+ restore the exception state which was put aside
+ (see YIELD_VALUE). */
+ SWAP_EXC_STATE();
+ }
+ else {
+ SAVE_EXC_STATE();
+ }
+ }
#ifdef LLTRACE
- lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
+ lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
#endif
#if defined(Py_DEBUG) || defined(LLTRACE)
- filename = _PyUnicode_AsString(co->co_filename);
+ filename = _PyUnicode_AsString(co->co_filename);
#endif
- why = WHY_NOT;
- err = 0;
- x = Py_None; /* Not a reference, just anything non-NULL */
- w = NULL;
+ why = WHY_NOT;
+ err = 0;
+ x = Py_None; /* Not a reference, just anything non-NULL */
+ w = NULL;
- if (throwflag) { /* support for generator.throw() */
- why = WHY_EXCEPTION;
- goto on_error;
- }
+ if (throwflag) { /* support for generator.throw() */
+ why = WHY_EXCEPTION;
+ goto on_error;
+ }
- for (;;) {
+ for (;;) {
#ifdef WITH_TSC
- if (inst1 == 0) {
- /* Almost surely, the opcode executed a break
- or a continue, preventing inst1 from being set
- on the way out of the loop.
- */
- READ_TIMESTAMP(inst1);
- loop1 = inst1;
- }
- dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
- intr0, intr1);
- ticked = 0;
- inst1 = 0;
- intr0 = 0;
- intr1 = 0;
- READ_TIMESTAMP(loop0);
+ if (inst1 == 0) {
+ /* Almost surely, the opcode executed a break
+ or a continue, preventing inst1 from being set
+ on the way out of the loop.
+ */
+ READ_TIMESTAMP(inst1);
+ loop1 = inst1;
+ }
+ dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
+ intr0, intr1);
+ ticked = 0;
+ inst1 = 0;
+ intr0 = 0;
+ intr1 = 0;
+ READ_TIMESTAMP(loop0);
#endif
- assert(stack_pointer >= f->f_valuestack); /* else underflow */
- assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
-
- /* Do periodic things. Doing this every time through
- the loop would add too much overhead, so we do it
- only every Nth instruction. We also do it if
- ``pendingcalls_to_do'' is set, i.e. when an asynchronous
- event needs attention (e.g. a signal handler or
- async I/O handler); see Py_AddPendingCall() and
- Py_MakePendingCalls() above. */
-
- if (--_Py_Ticker < 0) {
- if (*next_instr == SETUP_FINALLY) {
- /* Make the last opcode before
- a try: finally: block uninterruptable. */
- goto fast_next_opcode;
- }
- _Py_Ticker = _Py_CheckInterval;
- tstate->tick_counter++;
+ assert(stack_pointer >= f->f_valuestack); /* else underflow */
+ assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
+
+ /* Do periodic things. Doing this every time through
+ the loop would add too much overhead, so we do it
+ only every Nth instruction. We also do it if
+ ``pendingcalls_to_do'' is set, i.e. when an asynchronous
+ event needs attention (e.g. a signal handler or
+ async I/O handler); see Py_AddPendingCall() and
+ Py_MakePendingCalls() above. */
+
+ if (--_Py_Ticker < 0) {
+ if (*next_instr == SETUP_FINALLY) {
+ /* Make the last opcode before
+ a try: finally: block uninterruptable. */
+ goto fast_next_opcode;
+ }
+ _Py_Ticker = _Py_CheckInterval;
+ tstate->tick_counter++;
#ifdef WITH_TSC
- ticked = 1;
+ ticked = 1;
#endif
- if (pendingcalls_to_do) {
- if (Py_MakePendingCalls() < 0) {
- why = WHY_EXCEPTION;
- goto on_error;
- }
- if (pendingcalls_to_do)
- /* MakePendingCalls() didn't succeed.
- Force early re-execution of this
- "periodic" code, possibly after
- a thread switch */
- _Py_Ticker = 0;
- }
+ if (pendingcalls_to_do) {
+ if (Py_MakePendingCalls() < 0) {
+ why = WHY_EXCEPTION;
+ goto on_error;
+ }
+ if (pendingcalls_to_do)
+ /* MakePendingCalls() didn't succeed.
+ Force early re-execution of this
+ "periodic" code, possibly after
+ a thread switch */
+ _Py_Ticker = 0;
+ }
#ifdef WITH_THREAD
- if (interpreter_lock) {
- /* Give another thread a chance */
-
- if (PyThreadState_Swap(NULL) != tstate)
- Py_FatalError("ceval: tstate mix-up");
- PyThread_release_lock(interpreter_lock);
-
- /* Other threads may run now */
-
- PyThread_acquire_lock(interpreter_lock, 1);
- if (PyThreadState_Swap(tstate) != NULL)
- Py_FatalError("ceval: orphan tstate");
-
- /* Check for thread interrupts */
-
- if (tstate->async_exc != NULL) {
- x = tstate->async_exc;
- tstate->async_exc = NULL;
- PyErr_SetNone(x);
- Py_DECREF(x);
- why = WHY_EXCEPTION;
- goto on_error;
- }
- }
+ if (interpreter_lock) {
+ /* Give another thread a chance */
+
+ if (PyThreadState_Swap(NULL) != tstate)
+ Py_FatalError("ceval: tstate mix-up");
+ PyThread_release_lock(interpreter_lock);
+
+ /* Other threads may run now */
+
+ PyThread_acquire_lock(interpreter_lock, 1);
+ if (PyThreadState_Swap(tstate) != NULL)
+ Py_FatalError("ceval: orphan tstate");
+
+ /* Check for thread interrupts */
+
+ if (tstate->async_exc != NULL) {
+ x = tstate->async_exc;
+ tstate->async_exc = NULL;
+ PyErr_SetNone(x);
+ Py_DECREF(x);
+ why = WHY_EXCEPTION;
+ goto on_error;
+ }
+ }
#endif
- }
-
- fast_next_opcode:
- f->f_lasti = INSTR_OFFSET();
-
- /* line-by-line tracing support */
-
- if (_Py_TracingPossible &&
- tstate->c_tracefunc != NULL && !tstate->tracing) {
- /* see maybe_call_line_trace
- for expository comments */
- f->f_stacktop = stack_pointer;
-
- err = maybe_call_line_trace(tstate->c_tracefunc,
- tstate->c_traceobj,
- f, &instr_lb, &instr_ub,
- &instr_prev);
- /* Reload possibly changed frame fields */
- JUMPTO(f->f_lasti);
- if (f->f_stacktop != NULL) {
- stack_pointer = f->f_stacktop;
- f->f_stacktop = NULL;
- }
- if (err) {
- /* trace function raised an exception */
- goto on_error;
- }
- }
-
- /* Extract opcode and argument */
-
- opcode = NEXTOP();
- oparg = 0; /* allows oparg to be stored in a register because
- it doesn't have to be remembered across a full loop */
- if (HAS_ARG(opcode))
- oparg = NEXTARG();
- dispatch_opcode:
+ }
+
+ fast_next_opcode:
+ f->f_lasti = INSTR_OFFSET();
+
+ /* line-by-line tracing support */
+
+ if (_Py_TracingPossible &&
+ tstate->c_tracefunc != NULL && !tstate->tracing) {
+ /* see maybe_call_line_trace
+ for expository comments */
+ f->f_stacktop = stack_pointer;
+
+ err = maybe_call_line_trace(tstate->c_tracefunc,
+ tstate->c_traceobj,
+ f, &instr_lb, &instr_ub,
+ &instr_prev);
+ /* Reload possibly changed frame fields */
+ JUMPTO(f->f_lasti);
+ if (f->f_stacktop != NULL) {
+ stack_pointer = f->f_stacktop;
+ f->f_stacktop = NULL;
+ }
+ if (err) {
+ /* trace function raised an exception */
+ goto on_error;
+ }
+ }
+
+ /* Extract opcode and argument */
+
+ opcode = NEXTOP();
+ oparg = 0; /* allows oparg to be stored in a register because
+ it doesn't have to be remembered across a full loop */
+ if (HAS_ARG(opcode))
+ oparg = NEXTARG();
+ dispatch_opcode:
#ifdef DYNAMIC_EXECUTION_PROFILE
#ifdef DXPAIRS
- dxpairs[lastopcode][opcode]++;
- lastopcode = opcode;
+ dxpairs[lastopcode][opcode]++;
+ lastopcode = opcode;
#endif
- dxp[opcode]++;
+ dxp[opcode]++;
#endif
#ifdef LLTRACE
- /* Instruction tracing */
-
- if (lltrace) {
- if (HAS_ARG(opcode)) {
- printf("%d: %d, %d\n",
- f->f_lasti, opcode, oparg);
- }
- else {
- printf("%d: %d\n",
- f->f_lasti, opcode);
- }
- }
+ /* Instruction tracing */
+
+ if (lltrace) {
+ if (HAS_ARG(opcode)) {
+ printf("%d: %d, %d\n",
+ f->f_lasti, opcode, oparg);
+ }
+ else {
+ printf("%d: %d\n",
+ f->f_lasti, opcode);
+ }
+ }
#endif
- /* Main switch on opcode */
- READ_TIMESTAMP(inst0);
-
- switch (opcode) {
-
- /* BEWARE!
- It is essential that any operation that fails sets either
- x to NULL, err to nonzero, or why to anything but WHY_NOT,
- and that no operation that succeeds does this! */
-
- /* case STOP_CODE: this is an error! */
-
- TARGET(NOP)
- FAST_DISPATCH();
-
- TARGET(LOAD_FAST)
- x = GETLOCAL(oparg);
- if (x != NULL) {
- Py_INCREF(x);
- PUSH(x);
- FAST_DISPATCH();
- }
- format_exc_check_arg(PyExc_UnboundLocalError,
- UNBOUNDLOCAL_ERROR_MSG,
- PyTuple_GetItem(co->co_varnames, oparg));
- break;
-
- TARGET(LOAD_CONST)
- x = GETITEM(consts, oparg);
- Py_INCREF(x);
- PUSH(x);
- FAST_DISPATCH();
-
- PREDICTED_WITH_ARG(STORE_FAST);
- TARGET(STORE_FAST)
- v = POP();
- SETLOCAL(oparg, v);
- FAST_DISPATCH();
-
- TARGET(POP_TOP)
- v = POP();
- Py_DECREF(v);
- FAST_DISPATCH();
-
- TARGET(ROT_TWO)
- v = TOP();
- w = SECOND();
- SET_TOP(w);
- SET_SECOND(v);
- FAST_DISPATCH();
-
- TARGET(ROT_THREE)
- v = TOP();
- w = SECOND();
- x = THIRD();
- SET_TOP(w);
- SET_SECOND(x);
- SET_THIRD(v);
- FAST_DISPATCH();
-
- TARGET(ROT_FOUR)
- u = TOP();
- v = SECOND();
- w = THIRD();
- x = FOURTH();
- SET_TOP(v);
- SET_SECOND(w);
- SET_THIRD(x);
- SET_FOURTH(u);
- FAST_DISPATCH();
-
- TARGET(DUP_TOP)
- v = TOP();
- Py_INCREF(v);
- PUSH(v);
- FAST_DISPATCH();
-
- TARGET(DUP_TOPX)
- if (oparg == 2) {
- x = TOP();
- Py_INCREF(x);
- w = SECOND();
- Py_INCREF(w);
- STACKADJ(2);
- SET_TOP(x);
- SET_SECOND(w);
- FAST_DISPATCH();
- } else if (oparg == 3) {
- x = TOP();
- Py_INCREF(x);
- w = SECOND();
- Py_INCREF(w);
- v = THIRD();
- Py_INCREF(v);
- STACKADJ(3);
- SET_TOP(x);
- SET_SECOND(w);
- SET_THIRD(v);
- FAST_DISPATCH();
- }
- Py_FatalError("invalid argument to DUP_TOPX"
- " (bytecode corruption?)");
- /* Never returns, so don't bother to set why. */
- break;
-
- TARGET(UNARY_POSITIVE)
- v = TOP();
- x = PyNumber_Positive(v);
- Py_DECREF(v);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(UNARY_NEGATIVE)
- v = TOP();
- x = PyNumber_Negative(v);
- Py_DECREF(v);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(UNARY_NOT)
- v = TOP();
- err = PyObject_IsTrue(v);
- Py_DECREF(v);
- if (err == 0) {
- Py_INCREF(Py_True);
- SET_TOP(Py_True);
- DISPATCH();
- }
- else if (err > 0) {
- Py_INCREF(Py_False);
- SET_TOP(Py_False);
- err = 0;
- DISPATCH();
- }
- STACKADJ(-1);
- break;
-
- TARGET(UNARY_INVERT)
- v = TOP();
- x = PyNumber_Invert(v);
- Py_DECREF(v);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_POWER)
- w = POP();
- v = TOP();
- x = PyNumber_Power(v, w, Py_None);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_MULTIPLY)
- w = POP();
- v = TOP();
- x = PyNumber_Multiply(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_TRUE_DIVIDE)
- w = POP();
- v = TOP();
- x = PyNumber_TrueDivide(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_FLOOR_DIVIDE)
- w = POP();
- v = TOP();
- x = PyNumber_FloorDivide(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_MODULO)
- w = POP();
- v = TOP();
- if (PyUnicode_CheckExact(v))
- x = PyUnicode_Format(v, w);
- else
- x = PyNumber_Remainder(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_ADD)
- w = POP();
- v = TOP();
- if (PyUnicode_CheckExact(v) &&
- PyUnicode_CheckExact(w)) {
- x = unicode_concatenate(v, w, f, next_instr);
- /* unicode_concatenate consumed the ref to v */
- goto skip_decref_vx;
- }
- else {
- x = PyNumber_Add(v, w);
- }
- Py_DECREF(v);
- skip_decref_vx:
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_SUBTRACT)
- w = POP();
- v = TOP();
- x = PyNumber_Subtract(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_SUBSCR)
- w = POP();
- v = TOP();
- x = PyObject_GetItem(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_LSHIFT)
- w = POP();
- v = TOP();
- x = PyNumber_Lshift(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_RSHIFT)
- w = POP();
- v = TOP();
- x = PyNumber_Rshift(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_AND)
- w = POP();
- v = TOP();
- x = PyNumber_And(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_XOR)
- w = POP();
- v = TOP();
- x = PyNumber_Xor(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(BINARY_OR)
- w = POP();
- v = TOP();
- x = PyNumber_Or(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(LIST_APPEND)
- w = POP();
- v = stack_pointer[-oparg];
- err = PyList_Append(v, w);
- Py_DECREF(w);
- if (err == 0) {
- PREDICT(JUMP_ABSOLUTE);
- DISPATCH();
- }
- break;
-
- TARGET(SET_ADD)
- w = POP();
- v = stack_pointer[-oparg];
- err = PySet_Add(v, w);
- Py_DECREF(w);
- if (err == 0) {
- PREDICT(JUMP_ABSOLUTE);
- DISPATCH();
- }
- break;
-
- TARGET(INPLACE_POWER)
- w = POP();
- v = TOP();
- x = PyNumber_InPlacePower(v, w, Py_None);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_MULTIPLY)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceMultiply(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_TRUE_DIVIDE)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceTrueDivide(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_FLOOR_DIVIDE)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceFloorDivide(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_MODULO)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceRemainder(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_ADD)
- w = POP();
- v = TOP();
- if (PyUnicode_CheckExact(v) &&
- PyUnicode_CheckExact(w)) {
- x = unicode_concatenate(v, w, f, next_instr);
- /* unicode_concatenate consumed the ref to v */
- goto skip_decref_v;
- }
- else {
- x = PyNumber_InPlaceAdd(v, w);
- }
- Py_DECREF(v);
- skip_decref_v:
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_SUBTRACT)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceSubtract(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_LSHIFT)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceLshift(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_RSHIFT)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceRshift(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_AND)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceAnd(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_XOR)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceXor(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(INPLACE_OR)
- w = POP();
- v = TOP();
- x = PyNumber_InPlaceOr(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(STORE_SUBSCR)
- w = TOP();
- v = SECOND();
- u = THIRD();
- STACKADJ(-3);
- /* v[w] = u */
- err = PyObject_SetItem(v, w, u);
- Py_DECREF(u);
- Py_DECREF(v);
- Py_DECREF(w);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(DELETE_SUBSCR)
- w = TOP();
- v = SECOND();
- STACKADJ(-2);
- /* del v[w] */
- err = PyObject_DelItem(v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(PRINT_EXPR)
- v = POP();
- w = PySys_GetObject("displayhook");
- if (w == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "lost sys.displayhook");
- err = -1;
- x = NULL;
- }
- if (err == 0) {
- x = PyTuple_Pack(1, v);
- if (x == NULL)
- err = -1;
- }
- if (err == 0) {
- w = PyEval_CallObject(w, x);
- Py_XDECREF(w);
- if (w == NULL)
- err = -1;
- }
- Py_DECREF(v);
- Py_XDECREF(x);
- break;
+ /* Main switch on opcode */
+ READ_TIMESTAMP(inst0);
+
+ switch (opcode) {
+
+ /* BEWARE!
+ It is essential that any operation that fails sets either
+ x to NULL, err to nonzero, or why to anything but WHY_NOT,
+ and that no operation that succeeds does this! */
+
+ /* case STOP_CODE: this is an error! */
+
+ TARGET(NOP)
+ FAST_DISPATCH();
+
+ TARGET(LOAD_FAST)
+ x = GETLOCAL(oparg);
+ if (x != NULL) {
+ Py_INCREF(x);
+ PUSH(x);
+ FAST_DISPATCH();
+ }
+ format_exc_check_arg(PyExc_UnboundLocalError,
+ UNBOUNDLOCAL_ERROR_MSG,
+ PyTuple_GetItem(co->co_varnames, oparg));
+ break;
+
+ TARGET(LOAD_CONST)
+ x = GETITEM(consts, oparg);
+ Py_INCREF(x);
+ PUSH(x);
+ FAST_DISPATCH();
+
+ PREDICTED_WITH_ARG(STORE_FAST);
+ TARGET(STORE_FAST)
+ v = POP();
+ SETLOCAL(oparg, v);
+ FAST_DISPATCH();
+
+ TARGET(POP_TOP)
+ v = POP();
+ Py_DECREF(v);
+ FAST_DISPATCH();
+
+ TARGET(ROT_TWO)
+ v = TOP();
+ w = SECOND();
+ SET_TOP(w);
+ SET_SECOND(v);
+ FAST_DISPATCH();
+
+ TARGET(ROT_THREE)
+ v = TOP();
+ w = SECOND();
+ x = THIRD();
+ SET_TOP(w);
+ SET_SECOND(x);
+ SET_THIRD(v);
+ FAST_DISPATCH();
+
+ TARGET(ROT_FOUR)
+ u = TOP();
+ v = SECOND();
+ w = THIRD();
+ x = FOURTH();
+ SET_TOP(v);
+ SET_SECOND(w);
+ SET_THIRD(x);
+ SET_FOURTH(u);
+ FAST_DISPATCH();
+
+ TARGET(DUP_TOP)
+ v = TOP();
+ Py_INCREF(v);
+ PUSH(v);
+ FAST_DISPATCH();
+
+ TARGET(DUP_TOPX)
+ if (oparg == 2) {
+ x = TOP();
+ Py_INCREF(x);
+ w = SECOND();
+ Py_INCREF(w);
+ STACKADJ(2);
+ SET_TOP(x);
+ SET_SECOND(w);
+ FAST_DISPATCH();
+ } else if (oparg == 3) {
+ x = TOP();
+ Py_INCREF(x);
+ w = SECOND();
+ Py_INCREF(w);
+ v = THIRD();
+ Py_INCREF(v);
+ STACKADJ(3);
+ SET_TOP(x);
+ SET_SECOND(w);
+ SET_THIRD(v);
+ FAST_DISPATCH();
+ }
+ Py_FatalError("invalid argument to DUP_TOPX"
+ " (bytecode corruption?)");
+ /* Never returns, so don't bother to set why. */
+ break;
+
+ TARGET(UNARY_POSITIVE)
+ v = TOP();
+ x = PyNumber_Positive(v);
+ Py_DECREF(v);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(UNARY_NEGATIVE)
+ v = TOP();
+ x = PyNumber_Negative(v);
+ Py_DECREF(v);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(UNARY_NOT)
+ v = TOP();
+ err = PyObject_IsTrue(v);
+ Py_DECREF(v);
+ if (err == 0) {
+ Py_INCREF(Py_True);
+ SET_TOP(Py_True);
+ DISPATCH();
+ }
+ else if (err > 0) {
+ Py_INCREF(Py_False);
+ SET_TOP(Py_False);
+ err = 0;
+ DISPATCH();
+ }
+ STACKADJ(-1);
+ break;
+
+ TARGET(UNARY_INVERT)
+ v = TOP();
+ x = PyNumber_Invert(v);
+ Py_DECREF(v);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_POWER)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Power(v, w, Py_None);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_MULTIPLY)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Multiply(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_TRUE_DIVIDE)
+ w = POP();
+ v = TOP();
+ x = PyNumber_TrueDivide(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_FLOOR_DIVIDE)
+ w = POP();
+ v = TOP();
+ x = PyNumber_FloorDivide(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_MODULO)
+ w = POP();
+ v = TOP();
+ if (PyUnicode_CheckExact(v))
+ x = PyUnicode_Format(v, w);
+ else
+ x = PyNumber_Remainder(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_ADD)
+ w = POP();
+ v = TOP();
+ if (PyUnicode_CheckExact(v) &&
+ PyUnicode_CheckExact(w)) {
+ x = unicode_concatenate(v, w, f, next_instr);
+ /* unicode_concatenate consumed the ref to v */
+ goto skip_decref_vx;
+ }
+ else {
+ x = PyNumber_Add(v, w);
+ }
+ Py_DECREF(v);
+ skip_decref_vx:
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_SUBTRACT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Subtract(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_SUBSCR)
+ w = POP();
+ v = TOP();
+ x = PyObject_GetItem(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_LSHIFT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Lshift(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_RSHIFT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Rshift(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_AND)
+ w = POP();
+ v = TOP();
+ x = PyNumber_And(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_XOR)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Xor(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(BINARY_OR)
+ w = POP();
+ v = TOP();
+ x = PyNumber_Or(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(LIST_APPEND)
+ w = POP();
+ v = stack_pointer[-oparg];
+ err = PyList_Append(v, w);
+ Py_DECREF(w);
+ if (err == 0) {
+ PREDICT(JUMP_ABSOLUTE);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(SET_ADD)
+ w = POP();
+ v = stack_pointer[-oparg];
+ err = PySet_Add(v, w);
+ Py_DECREF(w);
+ if (err == 0) {
+ PREDICT(JUMP_ABSOLUTE);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(INPLACE_POWER)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlacePower(v, w, Py_None);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_MULTIPLY)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceMultiply(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_TRUE_DIVIDE)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceTrueDivide(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_FLOOR_DIVIDE)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceFloorDivide(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_MODULO)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceRemainder(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_ADD)
+ w = POP();
+ v = TOP();
+ if (PyUnicode_CheckExact(v) &&
+ PyUnicode_CheckExact(w)) {
+ x = unicode_concatenate(v, w, f, next_instr);
+ /* unicode_concatenate consumed the ref to v */
+ goto skip_decref_v;
+ }
+ else {
+ x = PyNumber_InPlaceAdd(v, w);
+ }
+ Py_DECREF(v);
+ skip_decref_v:
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_SUBTRACT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceSubtract(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_LSHIFT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceLshift(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_RSHIFT)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceRshift(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_AND)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceAnd(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_XOR)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceXor(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(INPLACE_OR)
+ w = POP();
+ v = TOP();
+ x = PyNumber_InPlaceOr(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(STORE_SUBSCR)
+ w = TOP();
+ v = SECOND();
+ u = THIRD();
+ STACKADJ(-3);
+ /* v[w] = u */
+ err = PyObject_SetItem(v, w, u);
+ Py_DECREF(u);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(DELETE_SUBSCR)
+ w = TOP();
+ v = SECOND();
+ STACKADJ(-2);
+ /* del v[w] */
+ err = PyObject_DelItem(v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(PRINT_EXPR)
+ v = POP();
+ w = PySys_GetObject("displayhook");
+ if (w == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "lost sys.displayhook");
+ err = -1;
+ x = NULL;
+ }
+ if (err == 0) {
+ x = PyTuple_Pack(1, v);
+ if (x == NULL)
+ err = -1;
+ }
+ if (err == 0) {
+ w = PyEval_CallObject(w, x);
+ Py_XDECREF(w);
+ if (w == NULL)
+ err = -1;
+ }
+ Py_DECREF(v);
+ Py_XDECREF(x);
+ break;
#ifdef CASE_TOO_BIG
- default: switch (opcode) {
+ default: switch (opcode) {
#endif
- TARGET(RAISE_VARARGS)
- v = w = NULL;
- switch (oparg) {
- case 2:
- v = POP(); /* cause */
- case 1:
- w = POP(); /* exc */
- case 0: /* Fallthrough */
- why = do_raise(w, v);
- break;
- default:
- PyErr_SetString(PyExc_SystemError,
- "bad RAISE_VARARGS oparg");
- why = WHY_EXCEPTION;
- break;
- }
- break;
-
- TARGET(STORE_LOCALS)
- x = POP();
- v = f->f_locals;
- Py_XDECREF(v);
- f->f_locals = x;
- DISPATCH();
-
- TARGET(RETURN_VALUE)
- retval = POP();
- why = WHY_RETURN;
- goto fast_block_end;
-
- TARGET(YIELD_VALUE)
- retval = POP();
- f->f_stacktop = stack_pointer;
- why = WHY_YIELD;
- /* Put aside the current exception state and restore
- that of the calling frame. This only serves when
- "yield" is used inside an except handler. */
- SWAP_EXC_STATE();
- goto fast_yield;
-
- TARGET(POP_EXCEPT)
- {
- PyTryBlock *b = PyFrame_BlockPop(f);
- if (b->b_type != EXCEPT_HANDLER) {
- PyErr_SetString(PyExc_SystemError,
- "popped block is not an except handler");
- why = WHY_EXCEPTION;
- break;
- }
- UNWIND_EXCEPT_HANDLER(b);
- }
- DISPATCH();
-
- TARGET(POP_BLOCK)
- {
- PyTryBlock *b = PyFrame_BlockPop(f);
- UNWIND_BLOCK(b);
- }
- DISPATCH();
-
- PREDICTED(END_FINALLY);
- TARGET(END_FINALLY)
- v = POP();
- if (PyLong_Check(v)) {
- why = (enum why_code) PyLong_AS_LONG(v);
- assert(why != WHY_YIELD);
- if (why == WHY_RETURN ||
- why == WHY_CONTINUE)
- retval = POP();
- if (why == WHY_SILENCED) {
- /* An exception was silenced by 'with', we must
- manually unwind the EXCEPT_HANDLER block which was
- created when the exception was caught, otherwise
- the stack will be in an inconsistent state. */
- PyTryBlock *b = PyFrame_BlockPop(f);
- if (b->b_type != EXCEPT_HANDLER) {
- PyErr_SetString(PyExc_SystemError,
- "popped block is not an except handler");
- why = WHY_EXCEPTION;
- }
- else {
- UNWIND_EXCEPT_HANDLER(b);
- why = WHY_NOT;
- }
- }
- }
- else if (PyExceptionClass_Check(v)) {
- w = POP();
- u = POP();
- PyErr_Restore(v, w, u);
- why = WHY_RERAISE;
- break;
- }
- else if (v != Py_None) {
- PyErr_SetString(PyExc_SystemError,
- "'finally' pops bad exception");
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- break;
-
- TARGET(LOAD_BUILD_CLASS)
- x = PyDict_GetItemString(f->f_builtins,
- "__build_class__");
- if (x == NULL) {
- PyErr_SetString(PyExc_ImportError,
- "__build_class__ not found");
- break;
- }
- Py_INCREF(x);
- PUSH(x);
- break;
-
- TARGET(STORE_NAME)
- w = GETITEM(names, oparg);
- v = POP();
- if ((x = f->f_locals) != NULL) {
- if (PyDict_CheckExact(x))
- err = PyDict_SetItem(x, w, v);
- else
- err = PyObject_SetItem(x, w, v);
- Py_DECREF(v);
- if (err == 0) DISPATCH();
- break;
- }
- PyErr_Format(PyExc_SystemError,
- "no locals found when storing %R", w);
- break;
-
- TARGET(DELETE_NAME)
- w = GETITEM(names, oparg);
- if ((x = f->f_locals) != NULL) {
- if ((err = PyObject_DelItem(x, w)) != 0)
- format_exc_check_arg(PyExc_NameError,
- NAME_ERROR_MSG,
- w);
- break;
- }
- PyErr_Format(PyExc_SystemError,
- "no locals when deleting %R", w);
- break;
-
- PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
- TARGET(UNPACK_SEQUENCE)
- v = POP();
- if (PyTuple_CheckExact(v) &&
- PyTuple_GET_SIZE(v) == oparg) {
- PyObject **items = \
- ((PyTupleObject *)v)->ob_item;
- while (oparg--) {
- w = items[oparg];
- Py_INCREF(w);
- PUSH(w);
- }
- Py_DECREF(v);
- DISPATCH();
- } else if (PyList_CheckExact(v) &&
- PyList_GET_SIZE(v) == oparg) {
- PyObject **items = \
- ((PyListObject *)v)->ob_item;
- while (oparg--) {
- w = items[oparg];
- Py_INCREF(w);
- PUSH(w);
- }
- } else if (unpack_iterable(v, oparg, -1,
- stack_pointer + oparg)) {
- stack_pointer += oparg;
- } else {
- /* unpack_iterable() raised an exception */
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- break;
-
- TARGET(UNPACK_EX)
- {
- int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
- v = POP();
-
- if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
- stack_pointer + totalargs)) {
- stack_pointer += totalargs;
- } else {
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- break;
- }
-
- TARGET(STORE_ATTR)
- w = GETITEM(names, oparg);
- v = TOP();
- u = SECOND();
- STACKADJ(-2);
- err = PyObject_SetAttr(v, w, u); /* v.w = u */
- Py_DECREF(v);
- Py_DECREF(u);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(DELETE_ATTR)
- w = GETITEM(names, oparg);
- v = POP();
- err = PyObject_SetAttr(v, w, (PyObject *)NULL);
- /* del v.w */
- Py_DECREF(v);
- break;
-
- TARGET(STORE_GLOBAL)
- w = GETITEM(names, oparg);
- v = POP();
- err = PyDict_SetItem(f->f_globals, w, v);
- Py_DECREF(v);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(DELETE_GLOBAL)
- w = GETITEM(names, oparg);
- if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
- format_exc_check_arg(
- PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
- break;
-
- TARGET(LOAD_NAME)
- w = GETITEM(names, oparg);
- if ((v = f->f_locals) == NULL) {
- PyErr_Format(PyExc_SystemError,
- "no locals when loading %R", w);
- why = WHY_EXCEPTION;
- break;
- }
- if (PyDict_CheckExact(v)) {
- x = PyDict_GetItem(v, w);
- Py_XINCREF(x);
- }
- else {
- x = PyObject_GetItem(v, w);
- if (x == NULL && PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(
- PyExc_KeyError))
- break;
- PyErr_Clear();
- }
- }
- if (x == NULL) {
- x = PyDict_GetItem(f->f_globals, w);
- if (x == NULL) {
- x = PyDict_GetItem(f->f_builtins, w);
- if (x == NULL) {
- format_exc_check_arg(
- PyExc_NameError,
- NAME_ERROR_MSG, w);
- break;
- }
- }
- Py_INCREF(x);
- }
- PUSH(x);
- DISPATCH();
-
- TARGET(LOAD_GLOBAL)
- w = GETITEM(names, oparg);
- if (PyUnicode_CheckExact(w)) {
- /* Inline the PyDict_GetItem() calls.
- WARNING: this is an extreme speed hack.
- Do not try this at home. */
- long hash = ((PyUnicodeObject *)w)->hash;
- if (hash != -1) {
- PyDictObject *d;
- PyDictEntry *e;
- d = (PyDictObject *)(f->f_globals);
- e = d->ma_lookup(d, w, hash);
- if (e == NULL) {
- x = NULL;
- break;
- }
- x = e->me_value;
- if (x != NULL) {
- Py_INCREF(x);
- PUSH(x);
- DISPATCH();
- }
- d = (PyDictObject *)(f->f_builtins);
- e = d->ma_lookup(d, w, hash);
- if (e == NULL) {
- x = NULL;
- break;
- }
- x = e->me_value;
- if (x != NULL) {
- Py_INCREF(x);
- PUSH(x);
- DISPATCH();
- }
- goto load_global_error;
- }
- }
- /* This is the un-inlined version of the code above */
- x = PyDict_GetItem(f->f_globals, w);
- if (x == NULL) {
- x = PyDict_GetItem(f->f_builtins, w);
- if (x == NULL) {
- load_global_error:
- format_exc_check_arg(
- PyExc_NameError,
- GLOBAL_NAME_ERROR_MSG, w);
- break;
- }
- }
- Py_INCREF(x);
- PUSH(x);
- DISPATCH();
-
- TARGET(DELETE_FAST)
- x = GETLOCAL(oparg);
- if (x != NULL) {
- SETLOCAL(oparg, NULL);
- DISPATCH();
- }
- format_exc_check_arg(
- PyExc_UnboundLocalError,
- UNBOUNDLOCAL_ERROR_MSG,
- PyTuple_GetItem(co->co_varnames, oparg)
- );
- break;
-
- TARGET(LOAD_CLOSURE)
- x = freevars[oparg];
- Py_INCREF(x);
- PUSH(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(LOAD_DEREF)
- x = freevars[oparg];
- w = PyCell_Get(x);
- if (w != NULL) {
- PUSH(w);
- DISPATCH();
- }
- err = -1;
- /* Don't stomp existing exception */
- if (PyErr_Occurred())
- break;
- if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
- v = PyTuple_GET_ITEM(co->co_cellvars,
- oparg);
- format_exc_check_arg(
- PyExc_UnboundLocalError,
- UNBOUNDLOCAL_ERROR_MSG,
- v);
- } else {
- v = PyTuple_GET_ITEM(co->co_freevars, oparg -
- PyTuple_GET_SIZE(co->co_cellvars));
- format_exc_check_arg(PyExc_NameError,
- UNBOUNDFREE_ERROR_MSG, v);
- }
- break;
-
- TARGET(STORE_DEREF)
- w = POP();
- x = freevars[oparg];
- PyCell_Set(x, w);
- Py_DECREF(w);
- DISPATCH();
-
- TARGET(BUILD_TUPLE)
- x = PyTuple_New(oparg);
- if (x != NULL) {
- for (; --oparg >= 0;) {
- w = POP();
- PyTuple_SET_ITEM(x, oparg, w);
- }
- PUSH(x);
- DISPATCH();
- }
- break;
-
- TARGET(BUILD_LIST)
- x = PyList_New(oparg);
- if (x != NULL) {
- for (; --oparg >= 0;) {
- w = POP();
- PyList_SET_ITEM(x, oparg, w);
- }
- PUSH(x);
- DISPATCH();
- }
- break;
-
- TARGET(BUILD_SET)
- x = PySet_New(NULL);
- if (x != NULL) {
- for (; --oparg >= 0;) {
- w = POP();
- if (err == 0)
- err = PySet_Add(x, w);
- Py_DECREF(w);
- }
- if (err != 0) {
- Py_DECREF(x);
- break;
- }
- PUSH(x);
- DISPATCH();
- }
- break;
-
- TARGET(BUILD_MAP)
- x = _PyDict_NewPresized((Py_ssize_t)oparg);
- PUSH(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(STORE_MAP)
- w = TOP(); /* key */
- u = SECOND(); /* value */
- v = THIRD(); /* dict */
- STACKADJ(-2);
- assert (PyDict_CheckExact(v));
- err = PyDict_SetItem(v, w, u); /* v[w] = u */
- Py_DECREF(u);
- Py_DECREF(w);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(MAP_ADD)
- w = TOP(); /* key */
- u = SECOND(); /* value */
- STACKADJ(-2);
- v = stack_pointer[-oparg]; /* dict */
- assert (PyDict_CheckExact(v));
- err = PyDict_SetItem(v, w, u); /* v[w] = u */
- Py_DECREF(u);
- Py_DECREF(w);
- if (err == 0) {
- PREDICT(JUMP_ABSOLUTE);
- DISPATCH();
- }
- break;
-
- TARGET(LOAD_ATTR)
- w = GETITEM(names, oparg);
- v = TOP();
- x = PyObject_GetAttr(v, w);
- Py_DECREF(v);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(COMPARE_OP)
- w = POP();
- v = TOP();
- x = cmp_outcome(oparg, v, w);
- Py_DECREF(v);
- Py_DECREF(w);
- SET_TOP(x);
- if (x == NULL) break;
- PREDICT(POP_JUMP_IF_FALSE);
- PREDICT(POP_JUMP_IF_TRUE);
- DISPATCH();
-
- TARGET(IMPORT_NAME)
- w = GETITEM(names, oparg);
- x = PyDict_GetItemString(f->f_builtins, "__import__");
- if (x == NULL) {
- PyErr_SetString(PyExc_ImportError,
- "__import__ not found");
- break;
- }
- Py_INCREF(x);
- v = POP();
- u = TOP();
- if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
- w = PyTuple_Pack(5,
- w,
- f->f_globals,
- f->f_locals == NULL ?
- Py_None : f->f_locals,
- v,
- u);
- else
- w = PyTuple_Pack(4,
- w,
- f->f_globals,
- f->f_locals == NULL ?
- Py_None : f->f_locals,
- v);
- Py_DECREF(v);
- Py_DECREF(u);
- if (w == NULL) {
- u = POP();
- Py_DECREF(x);
- x = NULL;
- break;
- }
- READ_TIMESTAMP(intr0);
- v = x;
- x = PyEval_CallObject(v, w);
- Py_DECREF(v);
- READ_TIMESTAMP(intr1);
- Py_DECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(IMPORT_STAR)
- v = POP();
- PyFrame_FastToLocals(f);
- if ((x = f->f_locals) == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "no locals found during 'import *'");
- break;
- }
- READ_TIMESTAMP(intr0);
- err = import_all_from(x, v);
- READ_TIMESTAMP(intr1);
- PyFrame_LocalsToFast(f, 0);
- Py_DECREF(v);
- if (err == 0) DISPATCH();
- break;
-
- TARGET(IMPORT_FROM)
- w = GETITEM(names, oparg);
- v = TOP();
- READ_TIMESTAMP(intr0);
- x = import_from(v, w);
- READ_TIMESTAMP(intr1);
- PUSH(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(JUMP_FORWARD)
- JUMPBY(oparg);
- FAST_DISPATCH();
-
- PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
- TARGET(POP_JUMP_IF_FALSE)
- w = POP();
- if (w == Py_True) {
- Py_DECREF(w);
- FAST_DISPATCH();
- }
- if (w == Py_False) {
- Py_DECREF(w);
- JUMPTO(oparg);
- FAST_DISPATCH();
- }
- err = PyObject_IsTrue(w);
- Py_DECREF(w);
- if (err > 0)
- err = 0;
- else if (err == 0)
- JUMPTO(oparg);
- else
- break;
- DISPATCH();
-
- PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
- TARGET(POP_JUMP_IF_TRUE)
- w = POP();
- if (w == Py_False) {
- Py_DECREF(w);
- FAST_DISPATCH();
- }
- if (w == Py_True) {
- Py_DECREF(w);
- JUMPTO(oparg);
- FAST_DISPATCH();
- }
- err = PyObject_IsTrue(w);
- Py_DECREF(w);
- if (err > 0) {
- err = 0;
- JUMPTO(oparg);
- }
- else if (err == 0)
- ;
- else
- break;
- DISPATCH();
-
- TARGET(JUMP_IF_FALSE_OR_POP)
- w = TOP();
- if (w == Py_True) {
- STACKADJ(-1);
- Py_DECREF(w);
- FAST_DISPATCH();
- }
- if (w == Py_False) {
- JUMPTO(oparg);
- FAST_DISPATCH();
- }
- err = PyObject_IsTrue(w);
- if (err > 0) {
- STACKADJ(-1);
- Py_DECREF(w);
- err = 0;
- }
- else if (err == 0)
- JUMPTO(oparg);
- else
- break;
- DISPATCH();
-
- TARGET(JUMP_IF_TRUE_OR_POP)
- w = TOP();
- if (w == Py_False) {
- STACKADJ(-1);
- Py_DECREF(w);
- FAST_DISPATCH();
- }
- if (w == Py_True) {
- JUMPTO(oparg);
- FAST_DISPATCH();
- }
- err = PyObject_IsTrue(w);
- if (err > 0) {
- err = 0;
- JUMPTO(oparg);
- }
- else if (err == 0) {
- STACKADJ(-1);
- Py_DECREF(w);
- }
- else
- break;
- DISPATCH();
-
- PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
- TARGET(JUMP_ABSOLUTE)
- JUMPTO(oparg);
+ TARGET(RAISE_VARARGS)
+ v = w = NULL;
+ switch (oparg) {
+ case 2:
+ v = POP(); /* cause */
+ case 1:
+ w = POP(); /* exc */
+ case 0: /* Fallthrough */
+ why = do_raise(w, v);
+ break;
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "bad RAISE_VARARGS oparg");
+ why = WHY_EXCEPTION;
+ break;
+ }
+ break;
+
+ TARGET(STORE_LOCALS)
+ x = POP();
+ v = f->f_locals;
+ Py_XDECREF(v);
+ f->f_locals = x;
+ DISPATCH();
+
+ TARGET(RETURN_VALUE)
+ retval = POP();
+ why = WHY_RETURN;
+ goto fast_block_end;
+
+ TARGET(YIELD_VALUE)
+ retval = POP();
+ f->f_stacktop = stack_pointer;
+ why = WHY_YIELD;
+ /* Put aside the current exception state and restore
+ that of the calling frame. This only serves when
+ "yield" is used inside an except handler. */
+ SWAP_EXC_STATE();
+ goto fast_yield;
+
+ TARGET(POP_EXCEPT)
+ {
+ PyTryBlock *b = PyFrame_BlockPop(f);
+ if (b->b_type != EXCEPT_HANDLER) {
+ PyErr_SetString(PyExc_SystemError,
+ "popped block is not an except handler");
+ why = WHY_EXCEPTION;
+ break;
+ }
+ UNWIND_EXCEPT_HANDLER(b);
+ }
+ DISPATCH();
+
+ TARGET(POP_BLOCK)
+ {
+ PyTryBlock *b = PyFrame_BlockPop(f);
+ UNWIND_BLOCK(b);
+ }
+ DISPATCH();
+
+ PREDICTED(END_FINALLY);
+ TARGET(END_FINALLY)
+ v = POP();
+ if (PyLong_Check(v)) {
+ why = (enum why_code) PyLong_AS_LONG(v);
+ assert(why != WHY_YIELD);
+ if (why == WHY_RETURN ||
+ why == WHY_CONTINUE)
+ retval = POP();
+ if (why == WHY_SILENCED) {
+ /* An exception was silenced by 'with', we must
+ manually unwind the EXCEPT_HANDLER block which was
+ created when the exception was caught, otherwise
+ the stack will be in an inconsistent state. */
+ PyTryBlock *b = PyFrame_BlockPop(f);
+ if (b->b_type != EXCEPT_HANDLER) {
+ PyErr_SetString(PyExc_SystemError,
+ "popped block is not an except handler");
+ why = WHY_EXCEPTION;
+ }
+ else {
+ UNWIND_EXCEPT_HANDLER(b);
+ why = WHY_NOT;
+ }
+ }
+ }
+ else if (PyExceptionClass_Check(v)) {
+ w = POP();
+ u = POP();
+ PyErr_Restore(v, w, u);
+ why = WHY_RERAISE;
+ break;
+ }
+ else if (v != Py_None) {
+ PyErr_SetString(PyExc_SystemError,
+ "'finally' pops bad exception");
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ break;
+
+ TARGET(LOAD_BUILD_CLASS)
+ x = PyDict_GetItemString(f->f_builtins,
+ "__build_class__");
+ if (x == NULL) {
+ PyErr_SetString(PyExc_ImportError,
+ "__build_class__ not found");
+ break;
+ }
+ Py_INCREF(x);
+ PUSH(x);
+ break;
+
+ TARGET(STORE_NAME)
+ w = GETITEM(names, oparg);
+ v = POP();
+ if ((x = f->f_locals) != NULL) {
+ if (PyDict_CheckExact(x))
+ err = PyDict_SetItem(x, w, v);
+ else
+ err = PyObject_SetItem(x, w, v);
+ Py_DECREF(v);
+ if (err == 0) DISPATCH();
+ break;
+ }
+ PyErr_Format(PyExc_SystemError,
+ "no locals found when storing %R", w);
+ break;
+
+ TARGET(DELETE_NAME)
+ w = GETITEM(names, oparg);
+ if ((x = f->f_locals) != NULL) {
+ if ((err = PyObject_DelItem(x, w)) != 0)
+ format_exc_check_arg(PyExc_NameError,
+ NAME_ERROR_MSG,
+ w);
+ break;
+ }
+ PyErr_Format(PyExc_SystemError,
+ "no locals when deleting %R", w);
+ break;
+
+ PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
+ TARGET(UNPACK_SEQUENCE)
+ v = POP();
+ if (PyTuple_CheckExact(v) &&
+ PyTuple_GET_SIZE(v) == oparg) {
+ PyObject **items = \
+ ((PyTupleObject *)v)->ob_item;
+ while (oparg--) {
+ w = items[oparg];
+ Py_INCREF(w);
+ PUSH(w);
+ }
+ Py_DECREF(v);
+ DISPATCH();
+ } else if (PyList_CheckExact(v) &&
+ PyList_GET_SIZE(v) == oparg) {
+ PyObject **items = \
+ ((PyListObject *)v)->ob_item;
+ while (oparg--) {
+ w = items[oparg];
+ Py_INCREF(w);
+ PUSH(w);
+ }
+ } else if (unpack_iterable(v, oparg, -1,
+ stack_pointer + oparg)) {
+ stack_pointer += oparg;
+ } else {
+ /* unpack_iterable() raised an exception */
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ break;
+
+ TARGET(UNPACK_EX)
+ {
+ int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
+ v = POP();
+
+ if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
+ stack_pointer + totalargs)) {
+ stack_pointer += totalargs;
+ } else {
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ break;
+ }
+
+ TARGET(STORE_ATTR)
+ w = GETITEM(names, oparg);
+ v = TOP();
+ u = SECOND();
+ STACKADJ(-2);
+ err = PyObject_SetAttr(v, w, u); /* v.w = u */
+ Py_DECREF(v);
+ Py_DECREF(u);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(DELETE_ATTR)
+ w = GETITEM(names, oparg);
+ v = POP();
+ err = PyObject_SetAttr(v, w, (PyObject *)NULL);
+ /* del v.w */
+ Py_DECREF(v);
+ break;
+
+ TARGET(STORE_GLOBAL)
+ w = GETITEM(names, oparg);
+ v = POP();
+ err = PyDict_SetItem(f->f_globals, w, v);
+ Py_DECREF(v);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(DELETE_GLOBAL)
+ w = GETITEM(names, oparg);
+ if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
+ format_exc_check_arg(
+ PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
+ break;
+
+ TARGET(LOAD_NAME)
+ w = GETITEM(names, oparg);
+ if ((v = f->f_locals) == NULL) {
+ PyErr_Format(PyExc_SystemError,
+ "no locals when loading %R", w);
+ why = WHY_EXCEPTION;
+ break;
+ }
+ if (PyDict_CheckExact(v)) {
+ x = PyDict_GetItem(v, w);
+ Py_XINCREF(x);
+ }
+ else {
+ x = PyObject_GetItem(v, w);
+ if (x == NULL && PyErr_Occurred()) {
+ if (!PyErr_ExceptionMatches(
+ PyExc_KeyError))
+ break;
+ PyErr_Clear();
+ }
+ }
+ if (x == NULL) {
+ x = PyDict_GetItem(f->f_globals, w);
+ if (x == NULL) {
+ x = PyDict_GetItem(f->f_builtins, w);
+ if (x == NULL) {
+ format_exc_check_arg(
+ PyExc_NameError,
+ NAME_ERROR_MSG, w);
+ break;
+ }
+ }
+ Py_INCREF(x);
+ }
+ PUSH(x);
+ DISPATCH();
+
+ TARGET(LOAD_GLOBAL)
+ w = GETITEM(names, oparg);
+ if (PyUnicode_CheckExact(w)) {
+ /* Inline the PyDict_GetItem() calls.
+ WARNING: this is an extreme speed hack.
+ Do not try this at home. */
+ long hash = ((PyUnicodeObject *)w)->hash;
+ if (hash != -1) {
+ PyDictObject *d;
+ PyDictEntry *e;
+ d = (PyDictObject *)(f->f_globals);
+ e = d->ma_lookup(d, w, hash);
+ if (e == NULL) {
+ x = NULL;
+ break;
+ }
+ x = e->me_value;
+ if (x != NULL) {
+ Py_INCREF(x);
+ PUSH(x);
+ DISPATCH();
+ }
+ d = (PyDictObject *)(f->f_builtins);
+ e = d->ma_lookup(d, w, hash);
+ if (e == NULL) {
+ x = NULL;
+ break;
+ }
+ x = e->me_value;
+ if (x != NULL) {
+ Py_INCREF(x);
+ PUSH(x);
+ DISPATCH();
+ }
+ goto load_global_error;
+ }
+ }
+ /* This is the un-inlined version of the code above */
+ x = PyDict_GetItem(f->f_globals, w);
+ if (x == NULL) {
+ x = PyDict_GetItem(f->f_builtins, w);
+ if (x == NULL) {
+ load_global_error:
+ format_exc_check_arg(
+ PyExc_NameError,
+ GLOBAL_NAME_ERROR_MSG, w);
+ break;
+ }
+ }
+ Py_INCREF(x);
+ PUSH(x);
+ DISPATCH();
+
+ TARGET(DELETE_FAST)
+ x = GETLOCAL(oparg);
+ if (x != NULL) {
+ SETLOCAL(oparg, NULL);
+ DISPATCH();
+ }
+ format_exc_check_arg(
+ PyExc_UnboundLocalError,
+ UNBOUNDLOCAL_ERROR_MSG,
+ PyTuple_GetItem(co->co_varnames, oparg)
+ );
+ break;
+
+ TARGET(LOAD_CLOSURE)
+ x = freevars[oparg];
+ Py_INCREF(x);
+ PUSH(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(LOAD_DEREF)
+ x = freevars[oparg];
+ w = PyCell_Get(x);
+ if (w != NULL) {
+ PUSH(w);
+ DISPATCH();
+ }
+ err = -1;
+ /* Don't stomp existing exception */
+ if (PyErr_Occurred())
+ break;
+ if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
+ v = PyTuple_GET_ITEM(co->co_cellvars,
+ oparg);
+ format_exc_check_arg(
+ PyExc_UnboundLocalError,
+ UNBOUNDLOCAL_ERROR_MSG,
+ v);
+ } else {
+ v = PyTuple_GET_ITEM(co->co_freevars, oparg -
+ PyTuple_GET_SIZE(co->co_cellvars));
+ format_exc_check_arg(PyExc_NameError,
+ UNBOUNDFREE_ERROR_MSG, v);
+ }
+ break;
+
+ TARGET(STORE_DEREF)
+ w = POP();
+ x = freevars[oparg];
+ PyCell_Set(x, w);
+ Py_DECREF(w);
+ DISPATCH();
+
+ TARGET(BUILD_TUPLE)
+ x = PyTuple_New(oparg);
+ if (x != NULL) {
+ for (; --oparg >= 0;) {
+ w = POP();
+ PyTuple_SET_ITEM(x, oparg, w);
+ }
+ PUSH(x);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(BUILD_LIST)
+ x = PyList_New(oparg);
+ if (x != NULL) {
+ for (; --oparg >= 0;) {
+ w = POP();
+ PyList_SET_ITEM(x, oparg, w);
+ }
+ PUSH(x);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(BUILD_SET)
+ x = PySet_New(NULL);
+ if (x != NULL) {
+ for (; --oparg >= 0;) {
+ w = POP();
+ if (err == 0)
+ err = PySet_Add(x, w);
+ Py_DECREF(w);
+ }
+ if (err != 0) {
+ Py_DECREF(x);
+ break;
+ }
+ PUSH(x);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(BUILD_MAP)
+ x = _PyDict_NewPresized((Py_ssize_t)oparg);
+ PUSH(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(STORE_MAP)
+ w = TOP(); /* key */
+ u = SECOND(); /* value */
+ v = THIRD(); /* dict */
+ STACKADJ(-2);
+ assert (PyDict_CheckExact(v));
+ err = PyDict_SetItem(v, w, u); /* v[w] = u */
+ Py_DECREF(u);
+ Py_DECREF(w);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(MAP_ADD)
+ w = TOP(); /* key */
+ u = SECOND(); /* value */
+ STACKADJ(-2);
+ v = stack_pointer[-oparg]; /* dict */
+ assert (PyDict_CheckExact(v));
+ err = PyDict_SetItem(v, w, u); /* v[w] = u */
+ Py_DECREF(u);
+ Py_DECREF(w);
+ if (err == 0) {
+ PREDICT(JUMP_ABSOLUTE);
+ DISPATCH();
+ }
+ break;
+
+ TARGET(LOAD_ATTR)
+ w = GETITEM(names, oparg);
+ v = TOP();
+ x = PyObject_GetAttr(v, w);
+ Py_DECREF(v);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(COMPARE_OP)
+ w = POP();
+ v = TOP();
+ x = cmp_outcome(oparg, v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x == NULL) break;
+ PREDICT(POP_JUMP_IF_FALSE);
+ PREDICT(POP_JUMP_IF_TRUE);
+ DISPATCH();
+
+ TARGET(IMPORT_NAME)
+ w = GETITEM(names, oparg);
+ x = PyDict_GetItemString(f->f_builtins, "__import__");
+ if (x == NULL) {
+ PyErr_SetString(PyExc_ImportError,
+ "__import__ not found");
+ break;
+ }
+ Py_INCREF(x);
+ v = POP();
+ u = TOP();
+ if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
+ w = PyTuple_Pack(5,
+ w,
+ f->f_globals,
+ f->f_locals == NULL ?
+ Py_None : f->f_locals,
+ v,
+ u);
+ else
+ w = PyTuple_Pack(4,
+ w,
+ f->f_globals,
+ f->f_locals == NULL ?
+ Py_None : f->f_locals,
+ v);
+ Py_DECREF(v);
+ Py_DECREF(u);
+ if (w == NULL) {
+ u = POP();
+ Py_DECREF(x);
+ x = NULL;
+ break;
+ }
+ READ_TIMESTAMP(intr0);
+ v = x;
+ x = PyEval_CallObject(v, w);
+ Py_DECREF(v);
+ READ_TIMESTAMP(intr1);
+ Py_DECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(IMPORT_STAR)
+ v = POP();
+ PyFrame_FastToLocals(f);
+ if ((x = f->f_locals) == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "no locals found during 'import *'");
+ break;
+ }
+ READ_TIMESTAMP(intr0);
+ err = import_all_from(x, v);
+ READ_TIMESTAMP(intr1);
+ PyFrame_LocalsToFast(f, 0);
+ Py_DECREF(v);
+ if (err == 0) DISPATCH();
+ break;
+
+ TARGET(IMPORT_FROM)
+ w = GETITEM(names, oparg);
+ v = TOP();
+ READ_TIMESTAMP(intr0);
+ x = import_from(v, w);
+ READ_TIMESTAMP(intr1);
+ PUSH(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(JUMP_FORWARD)
+ JUMPBY(oparg);
+ FAST_DISPATCH();
+
+ PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
+ TARGET(POP_JUMP_IF_FALSE)
+ w = POP();
+ if (w == Py_True) {
+ Py_DECREF(w);
+ FAST_DISPATCH();
+ }
+ if (w == Py_False) {
+ Py_DECREF(w);
+ JUMPTO(oparg);
+ FAST_DISPATCH();
+ }
+ err = PyObject_IsTrue(w);
+ Py_DECREF(w);
+ if (err > 0)
+ err = 0;
+ else if (err == 0)
+ JUMPTO(oparg);
+ else
+ break;
+ DISPATCH();
+
+ PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
+ TARGET(POP_JUMP_IF_TRUE)
+ w = POP();
+ if (w == Py_False) {
+ Py_DECREF(w);
+ FAST_DISPATCH();
+ }
+ if (w == Py_True) {
+ Py_DECREF(w);
+ JUMPTO(oparg);
+ FAST_DISPATCH();
+ }
+ err = PyObject_IsTrue(w);
+ Py_DECREF(w);
+ if (err > 0) {
+ err = 0;
+ JUMPTO(oparg);
+ }
+ else if (err == 0)
+ ;
+ else
+ break;
+ DISPATCH();
+
+ TARGET(JUMP_IF_FALSE_OR_POP)
+ w = TOP();
+ if (w == Py_True) {
+ STACKADJ(-1);
+ Py_DECREF(w);
+ FAST_DISPATCH();
+ }
+ if (w == Py_False) {
+ JUMPTO(oparg);
+ FAST_DISPATCH();
+ }
+ err = PyObject_IsTrue(w);
+ if (err > 0) {
+ STACKADJ(-1);
+ Py_DECREF(w);
+ err = 0;
+ }
+ else if (err == 0)
+ JUMPTO(oparg);
+ else
+ break;
+ DISPATCH();
+
+ TARGET(JUMP_IF_TRUE_OR_POP)
+ w = TOP();
+ if (w == Py_False) {
+ STACKADJ(-1);
+ Py_DECREF(w);
+ FAST_DISPATCH();
+ }
+ if (w == Py_True) {
+ JUMPTO(oparg);
+ FAST_DISPATCH();
+ }
+ err = PyObject_IsTrue(w);
+ if (err > 0) {
+ err = 0;
+ JUMPTO(oparg);
+ }
+ else if (err == 0) {
+ STACKADJ(-1);
+ Py_DECREF(w);
+ }
+ else
+ break;
+ DISPATCH();
+
+ PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
+ TARGET(JUMP_ABSOLUTE)
+ JUMPTO(oparg);
#if FAST_LOOPS
- /* Enabling this path speeds-up all while and for-loops by bypassing
- the per-loop checks for signals. By default, this should be turned-off
- because it prevents detection of a control-break in tight loops like
- "while 1: pass". Compile with this option turned-on when you need
- the speed-up and do not need break checking inside tight loops (ones
- that contain only instructions ending with FAST_DISPATCH).
- */
- FAST_DISPATCH();
+ /* Enabling this path speeds-up all while and for-loops by bypassing
+ the per-loop checks for signals. By default, this should be turned-off
+ because it prevents detection of a control-break in tight loops like
+ "while 1: pass". Compile with this option turned-on when you need
+ the speed-up and do not need break checking inside tight loops (ones
+ that contain only instructions ending with FAST_DISPATCH).
+ */
+ FAST_DISPATCH();
#else
- DISPATCH();
+ DISPATCH();
#endif
- TARGET(GET_ITER)
- /* before: [obj]; after [getiter(obj)] */
- v = TOP();
- x = PyObject_GetIter(v);
- Py_DECREF(v);
- if (x != NULL) {
- SET_TOP(x);
- PREDICT(FOR_ITER);
- DISPATCH();
- }
- STACKADJ(-1);
- break;
-
- PREDICTED_WITH_ARG(FOR_ITER);
- TARGET(FOR_ITER)
- /* before: [iter]; after: [iter, iter()] *or* [] */
- v = TOP();
- x = (*v->ob_type->tp_iternext)(v);
- if (x != NULL) {
- PUSH(x);
- PREDICT(STORE_FAST);
- PREDICT(UNPACK_SEQUENCE);
- DISPATCH();
- }
- if (PyErr_Occurred()) {
- if (!PyErr_ExceptionMatches(
- PyExc_StopIteration))
- break;
- PyErr_Clear();
- }
- /* iterator ended normally */
- x = v = POP();
- Py_DECREF(v);
- JUMPBY(oparg);
- DISPATCH();
-
- TARGET(BREAK_LOOP)
- why = WHY_BREAK;
- goto fast_block_end;
-
- TARGET(CONTINUE_LOOP)
- retval = PyLong_FromLong(oparg);
- if (!retval) {
- x = NULL;
- break;
- }
- why = WHY_CONTINUE;
- goto fast_block_end;
-
- TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
- TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
- TARGET(SETUP_FINALLY)
- _setup_finally:
- /* NOTE: If you add any new block-setup opcodes that
- are not try/except/finally handlers, you may need
- to update the PyGen_NeedsFinalizing() function.
- */
-
- PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
- STACK_LEVEL());
- DISPATCH();
-
- TARGET(WITH_CLEANUP)
- {
- /* At the top of the stack are 1-3 values indicating
- how/why we entered the finally clause:
- - TOP = None
- - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
- - TOP = WHY_*; no retval below it
- - (TOP, SECOND, THIRD) = exc_info()
- Below them is EXIT, the context.__exit__ bound method.
- In the last case, we must call
- EXIT(TOP, SECOND, THIRD)
- otherwise we must call
- EXIT(None, None, None)
-
- In all cases, we remove EXIT from the stack, leaving
- the rest in the same order.
-
- In addition, if the stack represents an exception,
- *and* the function call returns a 'true' value, we
- "zap" this information, to prevent END_FINALLY from
- re-raising the exception. (But non-local gotos
- should still be resumed.)
- */
-
- PyObject *exit_func = POP();
- u = TOP();
- if (u == Py_None) {
- v = w = Py_None;
- }
- else if (PyLong_Check(u)) {
- u = v = w = Py_None;
- }
- else {
- v = SECOND();
- w = THIRD();
- }
- /* XXX Not the fastest way to call it... */
- x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
- NULL);
- Py_DECREF(exit_func);
- if (x == NULL)
- break; /* Go to error exit */
-
- if (u != Py_None)
- err = PyObject_IsTrue(x);
- else
- err = 0;
- Py_DECREF(x);
-
- if (err < 0)
- break; /* Go to error exit */
- else if (err > 0) {
- err = 0;
- /* There was an exception and a True return */
- STACKADJ(-2);
- SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
- Py_DECREF(u);
- Py_DECREF(v);
- Py_DECREF(w);
- }
- PREDICT(END_FINALLY);
- break;
- }
-
- TARGET(CALL_FUNCTION)
- {
- PyObject **sp;
- PCALL(PCALL_ALL);
- sp = stack_pointer;
+ TARGET(GET_ITER)
+ /* before: [obj]; after [getiter(obj)] */
+ v = TOP();
+ x = PyObject_GetIter(v);
+ Py_DECREF(v);
+ if (x != NULL) {
+ SET_TOP(x);
+ PREDICT(FOR_ITER);
+ DISPATCH();
+ }
+ STACKADJ(-1);
+ break;
+
+ PREDICTED_WITH_ARG(FOR_ITER);
+ TARGET(FOR_ITER)
+ /* before: [iter]; after: [iter, iter()] *or* [] */
+ v = TOP();
+ x = (*v->ob_type->tp_iternext)(v);
+ if (x != NULL) {
+ PUSH(x);
+ PREDICT(STORE_FAST);
+ PREDICT(UNPACK_SEQUENCE);
+ DISPATCH();
+ }
+ if (PyErr_Occurred()) {
+ if (!PyErr_ExceptionMatches(
+ PyExc_StopIteration))
+ break;
+ PyErr_Clear();
+ }
+ /* iterator ended normally */
+ x = v = POP();
+ Py_DECREF(v);
+ JUMPBY(oparg);
+ DISPATCH();
+
+ TARGET(BREAK_LOOP)
+ why = WHY_BREAK;
+ goto fast_block_end;
+
+ TARGET(CONTINUE_LOOP)
+ retval = PyLong_FromLong(oparg);
+ if (!retval) {
+ x = NULL;
+ break;
+ }
+ why = WHY_CONTINUE;
+ goto fast_block_end;
+
+ TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
+ TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
+ TARGET(SETUP_FINALLY)
+ _setup_finally:
+ /* NOTE: If you add any new block-setup opcodes that
+ are not try/except/finally handlers, you may need
+ to update the PyGen_NeedsFinalizing() function.
+ */
+
+ PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
+ STACK_LEVEL());
+ DISPATCH();
+
+ TARGET(WITH_CLEANUP)
+ {
+ /* At the top of the stack are 1-3 values indicating
+ how/why we entered the finally clause:
+ - TOP = None
+ - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
+ - TOP = WHY_*; no retval below it
+ - (TOP, SECOND, THIRD) = exc_info()
+ Below them is EXIT, the context.__exit__ bound method.
+ In the last case, we must call
+ EXIT(TOP, SECOND, THIRD)
+ otherwise we must call
+ EXIT(None, None, None)
+
+ In all cases, we remove EXIT from the stack, leaving
+ the rest in the same order.
+
+ In addition, if the stack represents an exception,
+ *and* the function call returns a 'true' value, we
+ "zap" this information, to prevent END_FINALLY from
+ re-raising the exception. (But non-local gotos
+ should still be resumed.)
+ */
+
+ PyObject *exit_func = POP();
+ u = TOP();
+ if (u == Py_None) {
+ v = w = Py_None;
+ }
+ else if (PyLong_Check(u)) {
+ u = v = w = Py_None;
+ }
+ else {
+ v = SECOND();
+ w = THIRD();
+ }
+ /* XXX Not the fastest way to call it... */
+ x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
+ NULL);
+ Py_DECREF(exit_func);
+ if (x == NULL)
+ break; /* Go to error exit */
+
+ if (u != Py_None)
+ err = PyObject_IsTrue(x);
+ else
+ err = 0;
+ Py_DECREF(x);
+
+ if (err < 0)
+ break; /* Go to error exit */
+ else if (err > 0) {
+ err = 0;
+ /* There was an exception and a True return */
+ STACKADJ(-2);
+ SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
+ Py_DECREF(u);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ }
+ PREDICT(END_FINALLY);
+ break;
+ }
+
+ TARGET(CALL_FUNCTION)
+ {
+ PyObject **sp;
+ PCALL(PCALL_ALL);
+ sp = stack_pointer;
#ifdef WITH_TSC
- x = call_function(&sp, oparg, &intr0, &intr1);
+ x = call_function(&sp, oparg, &intr0, &intr1);
#else
- x = call_function(&sp, oparg);
+ x = call_function(&sp, oparg);
#endif
- stack_pointer = sp;
- PUSH(x);
- if (x != NULL)
- DISPATCH();
- break;
- }
-
- TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
- TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
- TARGET(CALL_FUNCTION_VAR_KW)
- _call_function_var_kw:
- {
- int na = oparg & 0xff;
- int nk = (oparg>>8) & 0xff;
- int flags = (opcode - CALL_FUNCTION) & 3;
- int n = na + 2 * nk;
- PyObject **pfunc, *func, **sp;
- PCALL(PCALL_ALL);
- if (flags & CALL_FLAG_VAR)
- n++;
- if (flags & CALL_FLAG_KW)
- n++;
- pfunc = stack_pointer - n - 1;
- func = *pfunc;
-
- if (PyMethod_Check(func)
- && PyMethod_GET_SELF(func) != NULL) {
- PyObject *self = PyMethod_GET_SELF(func);
- Py_INCREF(self);
- func = PyMethod_GET_FUNCTION(func);
- Py_INCREF(func);
- Py_DECREF(*pfunc);
- *pfunc = self;
- na++;
- n++;
- } else
- Py_INCREF(func);
- sp = stack_pointer;
- READ_TIMESTAMP(intr0);
- x = ext_do_call(func, &sp, flags, na, nk);
- READ_TIMESTAMP(intr1);
- stack_pointer = sp;
- Py_DECREF(func);
-
- while (stack_pointer > pfunc) {
- w = POP();
- Py_DECREF(w);
- }
- PUSH(x);
- if (x != NULL)
- DISPATCH();
- break;
- }
-
- TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
- TARGET(MAKE_FUNCTION)
- _make_function:
- {
- int posdefaults = oparg & 0xff;
- int kwdefaults = (oparg>>8) & 0xff;
- int num_annotations = (oparg >> 16) & 0x7fff;
-
- v = POP(); /* code object */
- x = PyFunction_New(v, f->f_globals);
- Py_DECREF(v);
-
- if (x != NULL && opcode == MAKE_CLOSURE) {
- v = POP();
- if (PyFunction_SetClosure(x, v) != 0) {
- /* Can't happen unless bytecode is corrupt. */
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- }
-
- if (x != NULL && num_annotations > 0) {
- Py_ssize_t name_ix;
- u = POP(); /* names of args with annotations */
- v = PyDict_New();
- if (v == NULL) {
- Py_DECREF(x);
- x = NULL;
- break;
- }
- name_ix = PyTuple_Size(u);
- assert(num_annotations == name_ix+1);
- while (name_ix > 0) {
- --name_ix;
- t = PyTuple_GET_ITEM(u, name_ix);
- w = POP();
- /* XXX(nnorwitz): check for errors */
- PyDict_SetItem(v, t, w);
- Py_DECREF(w);
- }
-
- if (PyFunction_SetAnnotations(x, v) != 0) {
- /* Can't happen unless
- PyFunction_SetAnnotations changes. */
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- Py_DECREF(u);
- }
-
- /* XXX Maybe this should be a separate opcode? */
- if (x != NULL && posdefaults > 0) {
- v = PyTuple_New(posdefaults);
- if (v == NULL) {
- Py_DECREF(x);
- x = NULL;
- break;
- }
- while (--posdefaults >= 0) {
- w = POP();
- PyTuple_SET_ITEM(v, posdefaults, w);
- }
- if (PyFunction_SetDefaults(x, v) != 0) {
- /* Can't happen unless
- PyFunction_SetDefaults changes. */
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- }
- if (x != NULL && kwdefaults > 0) {
- v = PyDict_New();
- if (v == NULL) {
- Py_DECREF(x);
- x = NULL;
- break;
- }
- while (--kwdefaults >= 0) {
- w = POP(); /* default value */
- u = POP(); /* kw only arg name */
- /* XXX(nnorwitz): check for errors */
- PyDict_SetItem(v, u, w);
- Py_DECREF(w);
- Py_DECREF(u);
- }
- if (PyFunction_SetKwDefaults(x, v) != 0) {
- /* Can't happen unless
- PyFunction_SetKwDefaults changes. */
- why = WHY_EXCEPTION;
- }
- Py_DECREF(v);
- }
- PUSH(x);
- break;
- }
-
- TARGET(BUILD_SLICE)
- if (oparg == 3)
- w = POP();
- else
- w = NULL;
- v = POP();
- u = TOP();
- x = PySlice_New(u, v, w);
- Py_DECREF(u);
- Py_DECREF(v);
- Py_XDECREF(w);
- SET_TOP(x);
- if (x != NULL) DISPATCH();
- break;
-
- TARGET(EXTENDED_ARG)
- opcode = NEXTOP();
- oparg = oparg<<16 | NEXTARG();
- goto dispatch_opcode;
+ stack_pointer = sp;
+ PUSH(x);
+ if (x != NULL)
+ DISPATCH();
+ break;
+ }
+
+ TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
+ TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
+ TARGET(CALL_FUNCTION_VAR_KW)
+ _call_function_var_kw:
+ {
+ int na = oparg & 0xff;
+ int nk = (oparg>>8) & 0xff;
+ int flags = (opcode - CALL_FUNCTION) & 3;
+ int n = na + 2 * nk;
+ PyObject **pfunc, *func, **sp;
+ PCALL(PCALL_ALL);
+ if (flags & CALL_FLAG_VAR)
+ n++;
+ if (flags & CALL_FLAG_KW)
+ n++;
+ pfunc = stack_pointer - n - 1;
+ func = *pfunc;
+
+ if (PyMethod_Check(func)
+ && PyMethod_GET_SELF(func) != NULL) {
+ PyObject *self = PyMethod_GET_SELF(func);
+ Py_INCREF(self);
+ func = PyMethod_GET_FUNCTION(func);
+ Py_INCREF(func);
+ Py_DECREF(*pfunc);
+ *pfunc = self;
+ na++;
+ n++;
+ } else
+ Py_INCREF(func);
+ sp = stack_pointer;
+ READ_TIMESTAMP(intr0);
+ x = ext_do_call(func, &sp, flags, na, nk);
+ READ_TIMESTAMP(intr1);
+ stack_pointer = sp;
+ Py_DECREF(func);
+
+ while (stack_pointer > pfunc) {
+ w = POP();
+ Py_DECREF(w);
+ }
+ PUSH(x);
+ if (x != NULL)
+ DISPATCH();
+ break;
+ }
+
+ TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
+ TARGET(MAKE_FUNCTION)
+ _make_function:
+ {
+ int posdefaults = oparg & 0xff;
+ int kwdefaults = (oparg>>8) & 0xff;
+ int num_annotations = (oparg >> 16) & 0x7fff;
+
+ v = POP(); /* code object */
+ x = PyFunction_New(v, f->f_globals);
+ Py_DECREF(v);
+
+ if (x != NULL && opcode == MAKE_CLOSURE) {
+ v = POP();
+ if (PyFunction_SetClosure(x, v) != 0) {
+ /* Can't happen unless bytecode is corrupt. */
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ }
+
+ if (x != NULL && num_annotations > 0) {
+ Py_ssize_t name_ix;
+ u = POP(); /* names of args with annotations */
+ v = PyDict_New();
+ if (v == NULL) {
+ Py_DECREF(x);
+ x = NULL;
+ break;
+ }
+ name_ix = PyTuple_Size(u);
+ assert(num_annotations == name_ix+1);
+ while (name_ix > 0) {
+ --name_ix;
+ t = PyTuple_GET_ITEM(u, name_ix);
+ w = POP();
+ /* XXX(nnorwitz): check for errors */
+ PyDict_SetItem(v, t, w);
+ Py_DECREF(w);
+ }
+
+ if (PyFunction_SetAnnotations(x, v) != 0) {
+ /* Can't happen unless
+ PyFunction_SetAnnotations changes. */
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ Py_DECREF(u);
+ }
+
+ /* XXX Maybe this should be a separate opcode? */
+ if (x != NULL && posdefaults > 0) {
+ v = PyTuple_New(posdefaults);
+ if (v == NULL) {
+ Py_DECREF(x);
+ x = NULL;
+ break;
+ }
+ while (--posdefaults >= 0) {
+ w = POP();
+ PyTuple_SET_ITEM(v, posdefaults, w);
+ }
+ if (PyFunction_SetDefaults(x, v) != 0) {
+ /* Can't happen unless
+ PyFunction_SetDefaults changes. */
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ }
+ if (x != NULL && kwdefaults > 0) {
+ v = PyDict_New();
+ if (v == NULL) {
+ Py_DECREF(x);
+ x = NULL;
+ break;
+ }
+ while (--kwdefaults >= 0) {
+ w = POP(); /* default value */
+ u = POP(); /* kw only arg name */
+ /* XXX(nnorwitz): check for errors */
+ PyDict_SetItem(v, u, w);
+ Py_DECREF(w);
+ Py_DECREF(u);
+ }
+ if (PyFunction_SetKwDefaults(x, v) != 0) {
+ /* Can't happen unless
+ PyFunction_SetKwDefaults changes. */
+ why = WHY_EXCEPTION;
+ }
+ Py_DECREF(v);
+ }
+ PUSH(x);
+ break;
+ }
+
+ TARGET(BUILD_SLICE)
+ if (oparg == 3)
+ w = POP();
+ else
+ w = NULL;
+ v = POP();
+ u = TOP();
+ x = PySlice_New(u, v, w);
+ Py_DECREF(u);
+ Py_DECREF(v);
+ Py_XDECREF(w);
+ SET_TOP(x);
+ if (x != NULL) DISPATCH();
+ break;
+
+ TARGET(EXTENDED_ARG)
+ opcode = NEXTOP();
+ oparg = oparg<<16 | NEXTARG();
+ goto dispatch_opcode;
#ifdef USE_COMPUTED_GOTOS
- _unknown_opcode:
+ _unknown_opcode:
#endif
- default:
- fprintf(stderr,
- "XXX lineno: %d, opcode: %d\n",
- PyCode_Addr2Line(f->f_code, f->f_lasti),
- opcode);
- PyErr_SetString(PyExc_SystemError, "unknown opcode");
- why = WHY_EXCEPTION;
- break;
+ default:
+ fprintf(stderr,
+ "XXX lineno: %d, opcode: %d\n",
+ PyCode_Addr2Line(f->f_code, f->f_lasti),
+ opcode);
+ PyErr_SetString(PyExc_SystemError, "unknown opcode");
+ why = WHY_EXCEPTION;
+ break;
#ifdef CASE_TOO_BIG
- }
+ }
#endif
- } /* switch */
+ } /* switch */
- on_error:
+ on_error:
- READ_TIMESTAMP(inst1);
+ READ_TIMESTAMP(inst1);
- /* Quickly continue if no error occurred */
+ /* Quickly continue if no error occurred */
- if (why == WHY_NOT) {
- if (err == 0 && x != NULL) {
+ if (why == WHY_NOT) {
+ if (err == 0 && x != NULL) {
#ifdef CHECKEXC
- /* This check is expensive! */
- if (PyErr_Occurred())
- fprintf(stderr,
- "XXX undetected error\n");
- else {
+ /* This check is expensive! */
+ if (PyErr_Occurred())
+ fprintf(stderr,
+ "XXX undetected error\n");
+ else {
#endif
- READ_TIMESTAMP(loop1);
- continue; /* Normal, fast path */
+ READ_TIMESTAMP(loop1);
+ continue; /* Normal, fast path */
#ifdef CHECKEXC
- }
+ }
#endif
- }
- why = WHY_EXCEPTION;
- x = Py_None;
- err = 0;
- }
-
- /* Double-check exception status */
-
- if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_SystemError,
- "error return without exception set");
- why = WHY_EXCEPTION;
- }
- }
+ }
+ why = WHY_EXCEPTION;
+ x = Py_None;
+ err = 0;
+ }
+
+ /* Double-check exception status */
+
+ if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_SystemError,
+ "error return without exception set");
+ why = WHY_EXCEPTION;
+ }
+ }
#ifdef CHECKEXC
- else {
- /* This check is expensive! */
- if (PyErr_Occurred()) {
- char buf[128];
- sprintf(buf, "Stack unwind with exception "
- "set and why=%d", why);
- Py_FatalError(buf);
- }
- }
+ else {
+ /* This check is expensive! */
+ if (PyErr_Occurred()) {
+ char buf[128];
+ sprintf(buf, "Stack unwind with exception "
+ "set and why=%d", why);
+ Py_FatalError(buf);
+ }
+ }
#endif
- /* Log traceback info if this is a real exception */
+ /* Log traceback info if this is a real exception */
- if (why == WHY_EXCEPTION) {
- PyTraceBack_Here(f);
+ if (why == WHY_EXCEPTION) {
+ PyTraceBack_Here(f);
- if (tstate->c_tracefunc != NULL)
- call_exc_trace(tstate->c_tracefunc,
- tstate->c_traceobj, f);
- }
+ if (tstate->c_tracefunc != NULL)
+ call_exc_trace(tstate->c_tracefunc,
+ tstate->c_traceobj, f);
+ }
- /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
+ /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
- if (why == WHY_RERAISE)
- why = WHY_EXCEPTION;
+ if (why == WHY_RERAISE)
+ why = WHY_EXCEPTION;
- /* Unwind stacks if a (pseudo) exception occurred */
+ /* Unwind stacks if a (pseudo) exception occurred */
fast_block_end:
- while (why != WHY_NOT && f->f_iblock > 0) {
- PyTryBlock *b = PyFrame_BlockPop(f);
-
- assert(why != WHY_YIELD);
- if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
- /* For a continue inside a try block,
- don't pop the block for the loop. */
- PyFrame_BlockSetup(f, b->b_type, b->b_handler,
- b->b_level);
- why = WHY_NOT;
- JUMPTO(PyLong_AS_LONG(retval));
- Py_DECREF(retval);
- break;
- }
-
- if (b->b_type == EXCEPT_HANDLER) {
- UNWIND_EXCEPT_HANDLER(b);
- continue;
- }
- UNWIND_BLOCK(b);
- if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
- why = WHY_NOT;
- JUMPTO(b->b_handler);
- break;
- }
- if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
- || b->b_type == SETUP_FINALLY)) {
- PyObject *exc, *val, *tb;
- int handler = b->b_handler;
- /* Beware, this invalidates all b->b_* fields */
- PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
- PUSH(tstate->exc_traceback);
- PUSH(tstate->exc_value);
- if (tstate->exc_type != NULL) {
- PUSH(tstate->exc_type);
- }
- else {
- Py_INCREF(Py_None);
- PUSH(Py_None);
- }
- PyErr_Fetch(&exc, &val, &tb);
- /* Make the raw exception data
- available to the handler,
- so a program can emulate the
- Python main loop. */
- PyErr_NormalizeException(
- &exc, &val, &tb);
- PyException_SetTraceback(val, tb);
- Py_INCREF(exc);
- tstate->exc_type = exc;
- Py_INCREF(val);
- tstate->exc_value = val;
- tstate->exc_traceback = tb;
- if (tb == NULL)
- tb = Py_None;
- Py_INCREF(tb);
- PUSH(tb);
- PUSH(val);
- PUSH(exc);
- why = WHY_NOT;
- JUMPTO(handler);
- break;
- }
- if (b->b_type == SETUP_FINALLY) {
- if (why & (WHY_RETURN | WHY_CONTINUE))
- PUSH(retval);
- PUSH(PyLong_FromLong((long)why));
- why = WHY_NOT;
- JUMPTO(b->b_handler);
- break;
- }
- } /* unwind stack */
-
- /* End the loop if we still have an error (or return) */
-
- if (why != WHY_NOT)
- break;
- READ_TIMESTAMP(loop1);
-
- } /* main loop */
-
- assert(why != WHY_YIELD);
- /* Pop remaining stack entries. */
- while (!EMPTY()) {
- v = POP();
- Py_XDECREF(v);
- }
-
- if (why != WHY_RETURN)
- retval = NULL;
+ while (why != WHY_NOT && f->f_iblock > 0) {
+ PyTryBlock *b = PyFrame_BlockPop(f);
+
+ assert(why != WHY_YIELD);
+ if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
+ /* For a continue inside a try block,
+ don't pop the block for the loop. */
+ PyFrame_BlockSetup(f, b->b_type, b->b_handler,
+ b->b_level);
+ why = WHY_NOT;
+ JUMPTO(PyLong_AS_LONG(retval));
+ Py_DECREF(retval);
+ break;
+ }
+
+ if (b->b_type == EXCEPT_HANDLER) {
+ UNWIND_EXCEPT_HANDLER(b);
+ continue;
+ }
+ UNWIND_BLOCK(b);
+ if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
+ why = WHY_NOT;
+ JUMPTO(b->b_handler);
+ break;
+ }
+ if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
+ || b->b_type == SETUP_FINALLY)) {
+ PyObject *exc, *val, *tb;
+ int handler = b->b_handler;
+ /* Beware, this invalidates all b->b_* fields */
+ PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
+ PUSH(tstate->exc_traceback);
+ PUSH(tstate->exc_value);
+ if (tstate->exc_type != NULL) {
+ PUSH(tstate->exc_type);
+ }
+ else {
+ Py_INCREF(Py_None);
+ PUSH(Py_None);
+ }
+ PyErr_Fetch(&exc, &val, &tb);
+ /* Make the raw exception data
+ available to the handler,
+ so a program can emulate the
+ Python main loop. */
+ PyErr_NormalizeException(
+ &exc, &val, &tb);
+ PyException_SetTraceback(val, tb);
+ Py_INCREF(exc);
+ tstate->exc_type = exc;
+ Py_INCREF(val);
+ tstate->exc_value = val;
+ tstate->exc_traceback = tb;
+ if (tb == NULL)
+ tb = Py_None;
+ Py_INCREF(tb);
+ PUSH(tb);
+ PUSH(val);
+ PUSH(exc);
+ why = WHY_NOT;
+ JUMPTO(handler);
+ break;
+ }
+ if (b->b_type == SETUP_FINALLY) {
+ if (why & (WHY_RETURN | WHY_CONTINUE))
+ PUSH(retval);
+ PUSH(PyLong_FromLong((long)why));
+ why = WHY_NOT;
+ JUMPTO(b->b_handler);
+ break;
+ }
+ } /* unwind stack */
+
+ /* End the loop if we still have an error (or return) */
+
+ if (why != WHY_NOT)
+ break;
+ READ_TIMESTAMP(loop1);
+
+ } /* main loop */
+
+ assert(why != WHY_YIELD);
+ /* Pop remaining stack entries. */
+ while (!EMPTY()) {
+ v = POP();
+ Py_XDECREF(v);
+ }
+
+ if (why != WHY_RETURN)
+ retval = NULL;
fast_yield:
- if (tstate->use_tracing) {
- if (tstate->c_tracefunc) {
- if (why == WHY_RETURN || why == WHY_YIELD) {
- if (call_trace(tstate->c_tracefunc,
- tstate->c_traceobj, f,
- PyTrace_RETURN, retval)) {
- Py_XDECREF(retval);
- retval = NULL;
- why = WHY_EXCEPTION;
- }
- }
- else if (why == WHY_EXCEPTION) {
- call_trace_protected(tstate->c_tracefunc,
- tstate->c_traceobj, f,
- PyTrace_RETURN, NULL);
- }
- }
- if (tstate->c_profilefunc) {
- if (why == WHY_EXCEPTION)
- call_trace_protected(tstate->c_profilefunc,
- tstate->c_profileobj, f,
- PyTrace_RETURN, NULL);
- else if (call_trace(tstate->c_profilefunc,
- tstate->c_profileobj, f,
- PyTrace_RETURN, retval)) {
- Py_XDECREF(retval);
- retval = NULL;
- why = WHY_EXCEPTION;
- }
- }
- }
-
- /* pop frame */
+ if (tstate->use_tracing) {
+ if (tstate->c_tracefunc) {
+ if (why == WHY_RETURN || why == WHY_YIELD) {
+ if (call_trace(tstate->c_tracefunc,
+ tstate->c_traceobj, f,
+ PyTrace_RETURN, retval)) {
+ Py_XDECREF(retval);
+ retval = NULL;
+ why = WHY_EXCEPTION;
+ }
+ }
+ else if (why == WHY_EXCEPTION) {
+ call_trace_protected(tstate->c_tracefunc,
+ tstate->c_traceobj, f,
+ PyTrace_RETURN, NULL);
+ }
+ }
+ if (tstate->c_profilefunc) {
+ if (why == WHY_EXCEPTION)
+ call_trace_protected(tstate->c_profilefunc,
+ tstate->c_profileobj, f,
+ PyTrace_RETURN, NULL);
+ else if (call_trace(tstate->c_profilefunc,
+ tstate->c_profileobj, f,
+ PyTrace_RETURN, retval)) {
+ Py_XDECREF(retval);
+ retval = NULL;
+ why = WHY_EXCEPTION;
+ }
+ }
+ }
+
+ /* pop frame */
exit_eval_frame:
- Py_LeaveRecursiveCall();
- tstate->frame = f->f_back;
+ Py_LeaveRecursiveCall();
+ tstate->frame = f->f_back;
- return retval;
+ return retval;
}
/* This is gonna seem *real weird*, but if you put some other code between
@@ -2937,278 +2937,278 @@ exit_eval_frame:
PyObject *
PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
- PyObject **args, int argcount, PyObject **kws, int kwcount,
- PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
+ PyObject **args, int argcount, PyObject **kws, int kwcount,
+ PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
{
- register PyFrameObject *f;
- register PyObject *retval = NULL;
- register PyObject **fastlocals, **freevars;
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *x, *u;
-
- if (globals == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "PyEval_EvalCodeEx: NULL globals");
- return NULL;
- }
-
- assert(tstate != NULL);
- assert(globals != NULL);
- f = PyFrame_New(tstate, co, globals, locals);
- if (f == NULL)
- return NULL;
-
- fastlocals = f->f_localsplus;
- freevars = f->f_localsplus + co->co_nlocals;
-
- if (co->co_argcount > 0 ||
- co->co_kwonlyargcount > 0 ||
- co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
- int i;
- int n = argcount;
- PyObject *kwdict = NULL;
- if (co->co_flags & CO_VARKEYWORDS) {
- kwdict = PyDict_New();
- if (kwdict == NULL)
- goto fail;
- i = co->co_argcount + co->co_kwonlyargcount;
- if (co->co_flags & CO_VARARGS)
- i++;
- SETLOCAL(i, kwdict);
- }
- if (argcount > co->co_argcount) {
- if (!(co->co_flags & CO_VARARGS)) {
- PyErr_Format(PyExc_TypeError,
- "%U() takes %s %d "
- "%spositional argument%s (%d given)",
- co->co_name,
- defcount ? "at most" : "exactly",
- co->co_argcount,
- kwcount ? "non-keyword " : "",
- co->co_argcount == 1 ? "" : "s",
- argcount);
- goto fail;
- }
- n = co->co_argcount;
- }
- for (i = 0; i < n; i++) {
- x = args[i];
- Py_INCREF(x);
- SETLOCAL(i, x);
- }
- if (co->co_flags & CO_VARARGS) {
- u = PyTuple_New(argcount - n);
- if (u == NULL)
- goto fail;
- SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
- for (i = n; i < argcount; i++) {
- x = args[i];
- Py_INCREF(x);
- PyTuple_SET_ITEM(u, i-n, x);
- }
- }
- for (i = 0; i < kwcount; i++) {
- PyObject **co_varnames;
- PyObject *keyword = kws[2*i];
- PyObject *value = kws[2*i + 1];
- int j;
- if (keyword == NULL || !PyUnicode_Check(keyword)) {
- PyErr_Format(PyExc_TypeError,
- "%U() keywords must be strings",
- co->co_name);
- goto fail;
- }
- /* Speed hack: do raw pointer compares. As names are
- normally interned this should almost always hit. */
- co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
- for (j = 0;
- j < co->co_argcount + co->co_kwonlyargcount;
- j++) {
- PyObject *nm = co_varnames[j];
- if (nm == keyword)
- goto kw_found;
- }
- /* Slow fallback, just in case */
- for (j = 0;
- j < co->co_argcount + co->co_kwonlyargcount;
- j++) {
- PyObject *nm = co_varnames[j];
- int cmp = PyObject_RichCompareBool(
- keyword, nm, Py_EQ);
- if (cmp > 0)
- goto kw_found;
- else if (cmp < 0)
- goto fail;
- }
- /* Check errors from Compare */
- if (PyErr_Occurred())
- goto fail;
- if (j >= co->co_argcount + co->co_kwonlyargcount) {
- if (kwdict == NULL) {
- PyErr_Format(PyExc_TypeError,
- "%U() got an unexpected "
- "keyword argument '%S'",
- co->co_name,
- keyword);
- goto fail;
- }
- PyDict_SetItem(kwdict, keyword, value);
- continue;
- }
+ register PyFrameObject *f;
+ register PyObject *retval = NULL;
+ register PyObject **fastlocals, **freevars;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *x, *u;
+
+ if (globals == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "PyEval_EvalCodeEx: NULL globals");
+ return NULL;
+ }
+
+ assert(tstate != NULL);
+ assert(globals != NULL);
+ f = PyFrame_New(tstate, co, globals, locals);
+ if (f == NULL)
+ return NULL;
+
+ fastlocals = f->f_localsplus;
+ freevars = f->f_localsplus + co->co_nlocals;
+
+ if (co->co_argcount > 0 ||
+ co->co_kwonlyargcount > 0 ||
+ co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
+ int i;
+ int n = argcount;
+ PyObject *kwdict = NULL;
+ if (co->co_flags & CO_VARKEYWORDS) {
+ kwdict = PyDict_New();
+ if (kwdict == NULL)
+ goto fail;
+ i = co->co_argcount + co->co_kwonlyargcount;
+ if (co->co_flags & CO_VARARGS)
+ i++;
+ SETLOCAL(i, kwdict);
+ }
+ if (argcount > co->co_argcount) {
+ if (!(co->co_flags & CO_VARARGS)) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() takes %s %d "
+ "%spositional argument%s (%d given)",
+ co->co_name,
+ defcount ? "at most" : "exactly",
+ co->co_argcount,
+ kwcount ? "non-keyword " : "",
+ co->co_argcount == 1 ? "" : "s",
+ argcount);
+ goto fail;
+ }
+ n = co->co_argcount;
+ }
+ for (i = 0; i < n; i++) {
+ x = args[i];
+ Py_INCREF(x);
+ SETLOCAL(i, x);
+ }
+ if (co->co_flags & CO_VARARGS) {
+ u = PyTuple_New(argcount - n);
+ if (u == NULL)
+ goto fail;
+ SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
+ for (i = n; i < argcount; i++) {
+ x = args[i];
+ Py_INCREF(x);
+ PyTuple_SET_ITEM(u, i-n, x);
+ }
+ }
+ for (i = 0; i < kwcount; i++) {
+ PyObject **co_varnames;
+ PyObject *keyword = kws[2*i];
+ PyObject *value = kws[2*i + 1];
+ int j;
+ if (keyword == NULL || !PyUnicode_Check(keyword)) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() keywords must be strings",
+ co->co_name);
+ goto fail;
+ }
+ /* Speed hack: do raw pointer compares. As names are
+ normally interned this should almost always hit. */
+ co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
+ for (j = 0;
+ j < co->co_argcount + co->co_kwonlyargcount;
+ j++) {
+ PyObject *nm = co_varnames[j];
+ if (nm == keyword)
+ goto kw_found;
+ }
+ /* Slow fallback, just in case */
+ for (j = 0;
+ j < co->co_argcount + co->co_kwonlyargcount;
+ j++) {
+ PyObject *nm = co_varnames[j];
+ int cmp = PyObject_RichCompareBool(
+ keyword, nm, Py_EQ);
+ if (cmp > 0)
+ goto kw_found;
+ else if (cmp < 0)
+ goto fail;
+ }
+ /* Check errors from Compare */
+ if (PyErr_Occurred())
+ goto fail;
+ if (j >= co->co_argcount + co->co_kwonlyargcount) {
+ if (kwdict == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() got an unexpected "
+ "keyword argument '%S'",
+ co->co_name,
+ keyword);
+ goto fail;
+ }
+ PyDict_SetItem(kwdict, keyword, value);
+ continue;
+ }
kw_found:
- if (GETLOCAL(j) != NULL) {
- PyErr_Format(PyExc_TypeError,
- "%U() got multiple "
- "values for keyword "
- "argument '%S'",
- co->co_name,
- keyword);
- goto fail;
- }
- Py_INCREF(value);
- SETLOCAL(j, value);
- }
- if (co->co_kwonlyargcount > 0) {
- for (i = co->co_argcount;
- i < co->co_argcount + co->co_kwonlyargcount;
- i++) {
- PyObject *name, *def;
- if (GETLOCAL(i) != NULL)
- continue;
- name = PyTuple_GET_ITEM(co->co_varnames, i);
- def = NULL;
- if (kwdefs != NULL)
- def = PyDict_GetItem(kwdefs, name);
- if (def != NULL) {
- Py_INCREF(def);
- SETLOCAL(i, def);
- continue;
- }
- PyErr_Format(PyExc_TypeError,
- "%U() needs keyword-only argument %S",
- co->co_name, name);
- goto fail;
- }
- }
- if (argcount < co->co_argcount) {
- int m = co->co_argcount - defcount;
- for (i = argcount; i < m; i++) {
- if (GETLOCAL(i) == NULL) {
- PyErr_Format(PyExc_TypeError,
- "%U() takes %s %d "
- "%spositional argument%s "
- "(%d given)",
- co->co_name,
- ((co->co_flags & CO_VARARGS) ||
- defcount) ? "at least"
- : "exactly",
- m, kwcount ? "non-keyword " : "",
- m == 1 ? "" : "s", i);
- goto fail;
- }
- }
- if (n > m)
- i = n - m;
- else
- i = 0;
- for (; i < defcount; i++) {
- if (GETLOCAL(m+i) == NULL) {
- PyObject *def = defs[i];
- Py_INCREF(def);
- SETLOCAL(m+i, def);
- }
- }
- }
- }
- else {
- if (argcount > 0 || kwcount > 0) {
- PyErr_Format(PyExc_TypeError,
- "%U() takes no arguments (%d given)",
- co->co_name,
- argcount + kwcount);
- goto fail;
- }
- }
- /* Allocate and initialize storage for cell vars, and copy free
- vars into frame. This isn't too efficient right now. */
- if (PyTuple_GET_SIZE(co->co_cellvars)) {
- int i, j, nargs, found;
- Py_UNICODE *cellname, *argname;
- PyObject *c;
-
- nargs = co->co_argcount + co->co_kwonlyargcount;
- if (co->co_flags & CO_VARARGS)
- nargs++;
- if (co->co_flags & CO_VARKEYWORDS)
- nargs++;
-
- /* Initialize each cell var, taking into account
- cell vars that are initialized from arguments.
-
- Should arrange for the compiler to put cellvars
- that are arguments at the beginning of the cellvars
- list so that we can march over it more efficiently?
- */
- for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
- cellname = PyUnicode_AS_UNICODE(
- PyTuple_GET_ITEM(co->co_cellvars, i));
- found = 0;
- for (j = 0; j < nargs; j++) {
- argname = PyUnicode_AS_UNICODE(
- PyTuple_GET_ITEM(co->co_varnames, j));
- if (Py_UNICODE_strcmp(cellname, argname) == 0) {
- c = PyCell_New(GETLOCAL(j));
- if (c == NULL)
- goto fail;
- GETLOCAL(co->co_nlocals + i) = c;
- found = 1;
- break;
- }
- }
- if (found == 0) {
- c = PyCell_New(NULL);
- if (c == NULL)
- goto fail;
- SETLOCAL(co->co_nlocals + i, c);
- }
- }
- }
- if (PyTuple_GET_SIZE(co->co_freevars)) {
- int i;
- for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
- PyObject *o = PyTuple_GET_ITEM(closure, i);
- Py_INCREF(o);
- freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
- }
- }
-
- if (co->co_flags & CO_GENERATOR) {
- /* Don't need to keep the reference to f_back, it will be set
- * when the generator is resumed. */
- Py_XDECREF(f->f_back);
- f->f_back = NULL;
-
- PCALL(PCALL_GENERATOR);
-
- /* Create a new generator that owns the ready to run frame
- * and return that as the value. */
- return PyGen_New(f);
- }
-
- retval = PyEval_EvalFrameEx(f,0);
+ if (GETLOCAL(j) != NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() got multiple "
+ "values for keyword "
+ "argument '%S'",
+ co->co_name,
+ keyword);
+ goto fail;
+ }
+ Py_INCREF(value);
+ SETLOCAL(j, value);
+ }
+ if (co->co_kwonlyargcount > 0) {
+ for (i = co->co_argcount;
+ i < co->co_argcount + co->co_kwonlyargcount;
+ i++) {
+ PyObject *name, *def;
+ if (GETLOCAL(i) != NULL)
+ continue;
+ name = PyTuple_GET_ITEM(co->co_varnames, i);
+ def = NULL;
+ if (kwdefs != NULL)
+ def = PyDict_GetItem(kwdefs, name);
+ if (def != NULL) {
+ Py_INCREF(def);
+ SETLOCAL(i, def);
+ continue;
+ }
+ PyErr_Format(PyExc_TypeError,
+ "%U() needs keyword-only argument %S",
+ co->co_name, name);
+ goto fail;
+ }
+ }
+ if (argcount < co->co_argcount) {
+ int m = co->co_argcount - defcount;
+ for (i = argcount; i < m; i++) {
+ if (GETLOCAL(i) == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() takes %s %d "
+ "%spositional argument%s "
+ "(%d given)",
+ co->co_name,
+ ((co->co_flags & CO_VARARGS) ||
+ defcount) ? "at least"
+ : "exactly",
+ m, kwcount ? "non-keyword " : "",
+ m == 1 ? "" : "s", i);
+ goto fail;
+ }
+ }
+ if (n > m)
+ i = n - m;
+ else
+ i = 0;
+ for (; i < defcount; i++) {
+ if (GETLOCAL(m+i) == NULL) {
+ PyObject *def = defs[i];
+ Py_INCREF(def);
+ SETLOCAL(m+i, def);
+ }
+ }
+ }
+ }
+ else {
+ if (argcount > 0 || kwcount > 0) {
+ PyErr_Format(PyExc_TypeError,
+ "%U() takes no arguments (%d given)",
+ co->co_name,
+ argcount + kwcount);
+ goto fail;
+ }
+ }
+ /* Allocate and initialize storage for cell vars, and copy free
+ vars into frame. This isn't too efficient right now. */
+ if (PyTuple_GET_SIZE(co->co_cellvars)) {
+ int i, j, nargs, found;
+ Py_UNICODE *cellname, *argname;
+ PyObject *c;
+
+ nargs = co->co_argcount + co->co_kwonlyargcount;
+ if (co->co_flags & CO_VARARGS)
+ nargs++;
+ if (co->co_flags & CO_VARKEYWORDS)
+ nargs++;
+
+ /* Initialize each cell var, taking into account
+ cell vars that are initialized from arguments.
+
+ Should arrange for the compiler to put cellvars
+ that are arguments at the beginning of the cellvars
+ list so that we can march over it more efficiently?
+ */
+ for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
+ cellname = PyUnicode_AS_UNICODE(
+ PyTuple_GET_ITEM(co->co_cellvars, i));
+ found = 0;
+ for (j = 0; j < nargs; j++) {
+ argname = PyUnicode_AS_UNICODE(
+ PyTuple_GET_ITEM(co->co_varnames, j));
+ if (Py_UNICODE_strcmp(cellname, argname) == 0) {
+ c = PyCell_New(GETLOCAL(j));
+ if (c == NULL)
+ goto fail;
+ GETLOCAL(co->co_nlocals + i) = c;
+ found = 1;
+ break;
+ }
+ }
+ if (found == 0) {
+ c = PyCell_New(NULL);
+ if (c == NULL)
+ goto fail;
+ SETLOCAL(co->co_nlocals + i, c);
+ }
+ }
+ }
+ if (PyTuple_GET_SIZE(co->co_freevars)) {
+ int i;
+ for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
+ PyObject *o = PyTuple_GET_ITEM(closure, i);
+ Py_INCREF(o);
+ freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
+ }
+ }
+
+ if (co->co_flags & CO_GENERATOR) {
+ /* Don't need to keep the reference to f_back, it will be set
+ * when the generator is resumed. */
+ Py_XDECREF(f->f_back);
+ f->f_back = NULL;
+
+ PCALL(PCALL_GENERATOR);
+
+ /* Create a new generator that owns the ready to run frame
+ * and return that as the value. */
+ return PyGen_New(f);
+ }
+
+ retval = PyEval_EvalFrameEx(f,0);
fail: /* Jump here from prelude on failure */
- /* decref'ing the frame can cause __del__ methods to get invoked,
- which can call back into Python. While we're done with the
- current Python frame (f), the associated C stack is still in use,
- so recursion_depth must be boosted for the duration.
- */
- assert(tstate != NULL);
- ++tstate->recursion_depth;
- Py_DECREF(f);
- --tstate->recursion_depth;
- return retval;
+ /* decref'ing the frame can cause __del__ methods to get invoked,
+ which can call back into Python. While we're done with the
+ current Python frame (f), the associated C stack is still in use,
+ so recursion_depth must be boosted for the duration.
+ */
+ assert(tstate != NULL);
+ ++tstate->recursion_depth;
+ Py_DECREF(f);
+ --tstate->recursion_depth;
+ return retval;
}
@@ -3217,83 +3217,83 @@ fail: /* Jump here from prelude on failure */
static enum why_code
do_raise(PyObject *exc, PyObject *cause)
{
- PyObject *type = NULL, *value = NULL;
-
- if (exc == NULL) {
- /* Reraise */
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *tb;
- type = tstate->exc_type;
- value = tstate->exc_value;
- tb = tstate->exc_traceback;
- if (type == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active exception to reraise");
- return WHY_EXCEPTION;
- }
- Py_XINCREF(type);
- Py_XINCREF(value);
- Py_XINCREF(tb);
- PyErr_Restore(type, value, tb);
- return WHY_RERAISE;
- }
-
- /* We support the following forms of raise:
- raise
+ PyObject *type = NULL, *value = NULL;
+
+ if (exc == NULL) {
+ /* Reraise */
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *tb;
+ type = tstate->exc_type;
+ value = tstate->exc_value;
+ tb = tstate->exc_traceback;
+ if (type == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "No active exception to reraise");
+ return WHY_EXCEPTION;
+ }
+ Py_XINCREF(type);
+ Py_XINCREF(value);
+ Py_XINCREF(tb);
+ PyErr_Restore(type, value, tb);
+ return WHY_RERAISE;
+ }
+
+ /* We support the following forms of raise:
+ raise
raise <instance>
raise <type> */
- if (PyExceptionClass_Check(exc)) {
- type = exc;
- value = PyObject_CallObject(exc, NULL);
- if (value == NULL)
- goto raise_error;
- }
- else if (PyExceptionInstance_Check(exc)) {
- value = exc;
- type = PyExceptionInstance_Class(exc);
- Py_INCREF(type);
- }
- else {
- /* Not something you can raise. You get an exception
- anyway, just not what you specified :-) */
- Py_DECREF(exc);
- PyErr_SetString(PyExc_TypeError,
- "exceptions must derive from BaseException");
- goto raise_error;
- }
-
- if (cause) {
- PyObject *fixed_cause;
- if (PyExceptionClass_Check(cause)) {
- fixed_cause = PyObject_CallObject(cause, NULL);
- if (fixed_cause == NULL)
- goto raise_error;
- Py_DECREF(cause);
- }
- else if (PyExceptionInstance_Check(cause)) {
- fixed_cause = cause;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "exception causes must derive from "
- "BaseException");
- goto raise_error;
- }
- PyException_SetCause(value, fixed_cause);
- }
-
- PyErr_SetObject(type, value);
- /* PyErr_SetObject incref's its arguments */
- Py_XDECREF(value);
- Py_XDECREF(type);
- return WHY_EXCEPTION;
+ if (PyExceptionClass_Check(exc)) {
+ type = exc;
+ value = PyObject_CallObject(exc, NULL);
+ if (value == NULL)
+ goto raise_error;
+ }
+ else if (PyExceptionInstance_Check(exc)) {
+ value = exc;
+ type = PyExceptionInstance_Class(exc);
+ Py_INCREF(type);
+ }
+ else {
+ /* Not something you can raise. You get an exception
+ anyway, just not what you specified :-) */
+ Py_DECREF(exc);
+ PyErr_SetString(PyExc_TypeError,
+ "exceptions must derive from BaseException");
+ goto raise_error;
+ }
+
+ if (cause) {
+ PyObject *fixed_cause;
+ if (PyExceptionClass_Check(cause)) {
+ fixed_cause = PyObject_CallObject(cause, NULL);
+ if (fixed_cause == NULL)
+ goto raise_error;
+ Py_DECREF(cause);
+ }
+ else if (PyExceptionInstance_Check(cause)) {
+ fixed_cause = cause;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "exception causes must derive from "
+ "BaseException");
+ goto raise_error;
+ }
+ PyException_SetCause(value, fixed_cause);
+ }
+
+ PyErr_SetObject(type, value);
+ /* PyErr_SetObject incref's its arguments */
+ Py_XDECREF(value);
+ Py_XDECREF(type);
+ return WHY_EXCEPTION;
raise_error:
- Py_XDECREF(value);
- Py_XDECREF(type);
- Py_XDECREF(cause);
- return WHY_EXCEPTION;
+ Py_XDECREF(value);
+ Py_XDECREF(type);
+ Py_XDECREF(cause);
+ return WHY_EXCEPTION;
}
/* Iterate v argcnt times and store the results on the stack (via decreasing
@@ -3306,73 +3306,73 @@ raise_error:
static int
unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
{
- int i = 0, j = 0;
- Py_ssize_t ll = 0;
- PyObject *it; /* iter(v) */
- PyObject *w;
- PyObject *l = NULL; /* variable list */
-
- assert(v != NULL);
-
- it = PyObject_GetIter(v);
- if (it == NULL)
- goto Error;
-
- for (; i < argcnt; i++) {
- w = PyIter_Next(it);
- if (w == NULL) {
- /* Iterator done, via error or exhaustion. */
- if (!PyErr_Occurred()) {
- PyErr_Format(PyExc_ValueError,
- "need more than %d value%s to unpack",
- i, i == 1 ? "" : "s");
- }
- goto Error;
- }
- *--sp = w;
- }
-
- if (argcntafter == -1) {
- /* We better have exhausted the iterator now. */
- w = PyIter_Next(it);
- if (w == NULL) {
- if (PyErr_Occurred())
- goto Error;
- Py_DECREF(it);
- return 1;
- }
- Py_DECREF(w);
- PyErr_SetString(PyExc_ValueError, "too many values to unpack");
- goto Error;
- }
-
- l = PySequence_List(it);
- if (l == NULL)
- goto Error;
- *--sp = l;
- i++;
-
- ll = PyList_GET_SIZE(l);
- if (ll < argcntafter) {
- PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
- argcnt + ll);
- goto Error;
- }
-
- /* Pop the "after-variable" args off the list. */
- for (j = argcntafter; j > 0; j--, i++) {
- *--sp = PyList_GET_ITEM(l, ll - j);
- }
- /* Resize the list. */
- Py_SIZE(l) = ll - argcntafter;
- Py_DECREF(it);
- return 1;
+ int i = 0, j = 0;
+ Py_ssize_t ll = 0;
+ PyObject *it; /* iter(v) */
+ PyObject *w;
+ PyObject *l = NULL; /* variable list */
+
+ assert(v != NULL);
+
+ it = PyObject_GetIter(v);
+ if (it == NULL)
+ goto Error;
+
+ for (; i < argcnt; i++) {
+ w = PyIter_Next(it);
+ if (w == NULL) {
+ /* Iterator done, via error or exhaustion. */
+ if (!PyErr_Occurred()) {
+ PyErr_Format(PyExc_ValueError,
+ "need more than %d value%s to unpack",
+ i, i == 1 ? "" : "s");
+ }
+ goto Error;
+ }
+ *--sp = w;
+ }
+
+ if (argcntafter == -1) {
+ /* We better have exhausted the iterator now. */
+ w = PyIter_Next(it);
+ if (w == NULL) {
+ if (PyErr_Occurred())
+ goto Error;
+ Py_DECREF(it);
+ return 1;
+ }
+ Py_DECREF(w);
+ PyErr_SetString(PyExc_ValueError, "too many values to unpack");
+ goto Error;
+ }
+
+ l = PySequence_List(it);
+ if (l == NULL)
+ goto Error;
+ *--sp = l;
+ i++;
+
+ ll = PyList_GET_SIZE(l);
+ if (ll < argcntafter) {
+ PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
+ argcnt + ll);
+ goto Error;
+ }
+
+ /* Pop the "after-variable" args off the list. */
+ for (j = argcntafter; j > 0; j--, i++) {
+ *--sp = PyList_GET_ITEM(l, ll - j);
+ }
+ /* Resize the list. */
+ Py_SIZE(l) = ll - argcntafter;
+ Py_DECREF(it);
+ return 1;
Error:
- for (; i > 0; i--, sp++)
- Py_DECREF(*sp);
- Py_XDECREF(it);
- return 0;
+ for (; i > 0; i--, sp++)
+ Py_DECREF(*sp);
+ Py_XDECREF(it);
+ return 0;
}
@@ -3380,223 +3380,223 @@ Error:
static int
prtrace(PyObject *v, char *str)
{
- printf("%s ", str);
- if (PyObject_Print(v, stdout, 0) != 0)
- PyErr_Clear(); /* Don't know what else to do */
- printf("\n");
- return 1;
+ printf("%s ", str);
+ if (PyObject_Print(v, stdout, 0) != 0)
+ PyErr_Clear(); /* Don't know what else to do */
+ printf("\n");
+ return 1;
}
#endif
static void
call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
{
- PyObject *type, *value, *traceback, *arg;
- int err;
- PyErr_Fetch(&type, &value, &traceback);
- if (value == NULL) {
- value = Py_None;
- Py_INCREF(value);
- }
- arg = PyTuple_Pack(3, type, value, traceback);
- if (arg == NULL) {
- PyErr_Restore(type, value, traceback);
- return;
- }
- err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
- Py_DECREF(arg);
- if (err == 0)
- PyErr_Restore(type, value, traceback);
- else {
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(traceback);
- }
+ PyObject *type, *value, *traceback, *arg;
+ int err;
+ PyErr_Fetch(&type, &value, &traceback);
+ if (value == NULL) {
+ value = Py_None;
+ Py_INCREF(value);
+ }
+ arg = PyTuple_Pack(3, type, value, traceback);
+ if (arg == NULL) {
+ PyErr_Restore(type, value, traceback);
+ return;
+ }
+ err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
+ Py_DECREF(arg);
+ if (err == 0)
+ PyErr_Restore(type, value, traceback);
+ else {
+ Py_XDECREF(type);
+ Py_XDECREF(value);
+ Py_XDECREF(traceback);
+ }
}
static int
call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
- int what, PyObject *arg)
+ int what, PyObject *arg)
{
- PyObject *type, *value, *traceback;
- int err;
- PyErr_Fetch(&type, &value, &traceback);
- err = call_trace(func, obj, frame, what, arg);
- if (err == 0)
- {
- PyErr_Restore(type, value, traceback);
- return 0;
- }
- else {
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(traceback);
- return -1;
- }
+ PyObject *type, *value, *traceback;
+ int err;
+ PyErr_Fetch(&type, &value, &traceback);
+ err = call_trace(func, obj, frame, what, arg);
+ if (err == 0)
+ {
+ PyErr_Restore(type, value, traceback);
+ return 0;
+ }
+ else {
+ Py_XDECREF(type);
+ Py_XDECREF(value);
+ Py_XDECREF(traceback);
+ return -1;
+ }
}
static int
call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
- int what, PyObject *arg)
+ int what, PyObject *arg)
{
- register PyThreadState *tstate = frame->f_tstate;
- int result;
- if (tstate->tracing)
- return 0;
- tstate->tracing++;
- tstate->use_tracing = 0;
- result = func(obj, frame, what, arg);
- tstate->use_tracing = ((tstate->c_tracefunc != NULL)
- || (tstate->c_profilefunc != NULL));
- tstate->tracing--;
- return result;
+ register PyThreadState *tstate = frame->f_tstate;
+ int result;
+ if (tstate->tracing)
+ return 0;
+ tstate->tracing++;
+ tstate->use_tracing = 0;
+ result = func(obj, frame, what, arg);
+ tstate->use_tracing = ((tstate->c_tracefunc != NULL)
+ || (tstate->c_profilefunc != NULL));
+ tstate->tracing--;
+ return result;
}
PyObject *
_PyEval_CallTracing(PyObject *func, PyObject *args)
{
- PyFrameObject *frame = PyEval_GetFrame();
- PyThreadState *tstate = frame->f_tstate;
- int save_tracing = tstate->tracing;
- int save_use_tracing = tstate->use_tracing;
- PyObject *result;
-
- tstate->tracing = 0;
- tstate->use_tracing = ((tstate->c_tracefunc != NULL)
- || (tstate->c_profilefunc != NULL));
- result = PyObject_Call(func, args, NULL);
- tstate->tracing = save_tracing;
- tstate->use_tracing = save_use_tracing;
- return result;
+ PyFrameObject *frame = PyEval_GetFrame();
+ PyThreadState *tstate = frame->f_tstate;
+ int save_tracing = tstate->tracing;
+ int save_use_tracing = tstate->use_tracing;
+ PyObject *result;
+
+ tstate->tracing = 0;
+ tstate->use_tracing = ((tstate->c_tracefunc != NULL)
+ || (tstate->c_profilefunc != NULL));
+ result = PyObject_Call(func, args, NULL);
+ tstate->tracing = save_tracing;
+ tstate->use_tracing = save_use_tracing;
+ return result;
}
static int
maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
- PyFrameObject *frame, int *instr_lb, int *instr_ub,
- int *instr_prev)
+ PyFrameObject *frame, int *instr_lb, int *instr_ub,
+ int *instr_prev)
{
- int result = 0;
-
- /* If the last instruction executed isn't in the current
- instruction window, reset the window. If the last
- instruction happens to fall at the start of a line or if it
- represents a jump backwards, call the trace function.
- */
- if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
- int line;
- PyAddrPair bounds;
-
- line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
- &bounds);
- if (line >= 0) {
- frame->f_lineno = line;
- result = call_trace(func, obj, frame,
- PyTrace_LINE, Py_None);
- }
- *instr_lb = bounds.ap_lower;
- *instr_ub = bounds.ap_upper;
- }
- else if (frame->f_lasti <= *instr_prev) {
- result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
- }
- *instr_prev = frame->f_lasti;
- return result;
+ int result = 0;
+
+ /* If the last instruction executed isn't in the current
+ instruction window, reset the window. If the last
+ instruction happens to fall at the start of a line or if it
+ represents a jump backwards, call the trace function.
+ */
+ if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
+ int line;
+ PyAddrPair bounds;
+
+ line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
+ &bounds);
+ if (line >= 0) {
+ frame->f_lineno = line;
+ result = call_trace(func, obj, frame,
+ PyTrace_LINE, Py_None);
+ }
+ *instr_lb = bounds.ap_lower;
+ *instr_ub = bounds.ap_upper;
+ }
+ else if (frame->f_lasti <= *instr_prev) {
+ result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
+ }
+ *instr_prev = frame->f_lasti;
+ return result;
}
void
PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *temp = tstate->c_profileobj;
- Py_XINCREF(arg);
- tstate->c_profilefunc = NULL;
- tstate->c_profileobj = NULL;
- /* Must make sure that tracing is not ignored if 'temp' is freed */
- tstate->use_tracing = tstate->c_tracefunc != NULL;
- Py_XDECREF(temp);
- tstate->c_profilefunc = func;
- tstate->c_profileobj = arg;
- /* Flag that tracing or profiling is turned on */
- tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *temp = tstate->c_profileobj;
+ Py_XINCREF(arg);
+ tstate->c_profilefunc = NULL;
+ tstate->c_profileobj = NULL;
+ /* Must make sure that tracing is not ignored if 'temp' is freed */
+ tstate->use_tracing = tstate->c_tracefunc != NULL;
+ Py_XDECREF(temp);
+ tstate->c_profilefunc = func;
+ tstate->c_profileobj = arg;
+ /* Flag that tracing or profiling is turned on */
+ tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
}
void
PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *temp = tstate->c_traceobj;
- _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
- Py_XINCREF(arg);
- tstate->c_tracefunc = NULL;
- tstate->c_traceobj = NULL;
- /* Must make sure that profiling is not ignored if 'temp' is freed */
- tstate->use_tracing = tstate->c_profilefunc != NULL;
- Py_XDECREF(temp);
- tstate->c_tracefunc = func;
- tstate->c_traceobj = arg;
- /* Flag that tracing or profiling is turned on */
- tstate->use_tracing = ((func != NULL)
- || (tstate->c_profilefunc != NULL));
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *temp = tstate->c_traceobj;
+ _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
+ Py_XINCREF(arg);
+ tstate->c_tracefunc = NULL;
+ tstate->c_traceobj = NULL;
+ /* Must make sure that profiling is not ignored if 'temp' is freed */
+ tstate->use_tracing = tstate->c_profilefunc != NULL;
+ Py_XDECREF(temp);
+ tstate->c_tracefunc = func;
+ tstate->c_traceobj = arg;
+ /* Flag that tracing or profiling is turned on */
+ tstate->use_tracing = ((func != NULL)
+ || (tstate->c_profilefunc != NULL));
}
PyObject *
PyEval_GetBuiltins(void)
{
- PyFrameObject *current_frame = PyEval_GetFrame();
- if (current_frame == NULL)
- return PyThreadState_GET()->interp->builtins;
- else
- return current_frame->f_builtins;
+ PyFrameObject *current_frame = PyEval_GetFrame();
+ if (current_frame == NULL)
+ return PyThreadState_GET()->interp->builtins;
+ else
+ return current_frame->f_builtins;
}
PyObject *
PyEval_GetLocals(void)
{
- PyFrameObject *current_frame = PyEval_GetFrame();
- if (current_frame == NULL)
- return NULL;
- PyFrame_FastToLocals(current_frame);
- return current_frame->f_locals;
+ PyFrameObject *current_frame = PyEval_GetFrame();
+ if (current_frame == NULL)
+ return NULL;
+ PyFrame_FastToLocals(current_frame);
+ return current_frame->f_locals;
}
PyObject *
PyEval_GetGlobals(void)
{
- PyFrameObject *current_frame = PyEval_GetFrame();
- if (current_frame == NULL)
- return NULL;
- else
- return current_frame->f_globals;
+ PyFrameObject *current_frame = PyEval_GetFrame();
+ if (current_frame == NULL)
+ return NULL;
+ else
+ return current_frame->f_globals;
}
PyFrameObject *
PyEval_GetFrame(void)
{
- PyThreadState *tstate = PyThreadState_GET();
- return _PyThreadState_GetFrame(tstate);
+ PyThreadState *tstate = PyThreadState_GET();
+ return _PyThreadState_GetFrame(tstate);
}
int
PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
{
- PyFrameObject *current_frame = PyEval_GetFrame();
- int result = cf->cf_flags != 0;
-
- if (current_frame != NULL) {
- const int codeflags = current_frame->f_code->co_flags;
- const int compilerflags = codeflags & PyCF_MASK;
- if (compilerflags) {
- result = 1;
- cf->cf_flags |= compilerflags;
- }
+ PyFrameObject *current_frame = PyEval_GetFrame();
+ int result = cf->cf_flags != 0;
+
+ if (current_frame != NULL) {
+ const int codeflags = current_frame->f_code->co_flags;
+ const int compilerflags = codeflags & PyCF_MASK;
+ if (compilerflags) {
+ result = 1;
+ cf->cf_flags |= compilerflags;
+ }
#if 0 /* future keyword */
- if (codeflags & CO_GENERATOR_ALLOWED) {
- result = 1;
- cf->cf_flags |= CO_GENERATOR_ALLOWED;
- }
+ if (codeflags & CO_GENERATOR_ALLOWED) {
+ result = 1;
+ cf->cf_flags |= CO_GENERATOR_ALLOWED;
+ }
#endif
- }
- return result;
+ }
+ return result;
}
@@ -3609,194 +3609,194 @@ PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
PyObject *
PyEval_CallObject(PyObject *func, PyObject *arg)
{
- return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
+ return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
}
#define PyEval_CallObject(func,arg) \
- PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
+ PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
PyObject *
PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
{
- PyObject *result;
-
- if (arg == NULL) {
- arg = PyTuple_New(0);
- if (arg == NULL)
- return NULL;
- }
- else if (!PyTuple_Check(arg)) {
- PyErr_SetString(PyExc_TypeError,
- "argument list must be a tuple");
- return NULL;
- }
- else
- Py_INCREF(arg);
-
- if (kw != NULL && !PyDict_Check(kw)) {
- PyErr_SetString(PyExc_TypeError,
- "keyword list must be a dictionary");
- Py_DECREF(arg);
- return NULL;
- }
-
- result = PyObject_Call(func, arg, kw);
- Py_DECREF(arg);
- return result;
+ PyObject *result;
+
+ if (arg == NULL) {
+ arg = PyTuple_New(0);
+ if (arg == NULL)
+ return NULL;
+ }
+ else if (!PyTuple_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError,
+ "argument list must be a tuple");
+ return NULL;
+ }
+ else
+ Py_INCREF(arg);
+
+ if (kw != NULL && !PyDict_Check(kw)) {
+ PyErr_SetString(PyExc_TypeError,
+ "keyword list must be a dictionary");
+ Py_DECREF(arg);
+ return NULL;
+ }
+
+ result = PyObject_Call(func, arg, kw);
+ Py_DECREF(arg);
+ return result;
}
const char *
PyEval_GetFuncName(PyObject *func)
{
- if (PyMethod_Check(func))
- return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
- else if (PyFunction_Check(func))
- return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
- else if (PyCFunction_Check(func))
- return ((PyCFunctionObject*)func)->m_ml->ml_name;
- else
- return func->ob_type->tp_name;
+ if (PyMethod_Check(func))
+ return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
+ else if (PyFunction_Check(func))
+ return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
+ else if (PyCFunction_Check(func))
+ return ((PyCFunctionObject*)func)->m_ml->ml_name;
+ else
+ return func->ob_type->tp_name;
}
const char *
PyEval_GetFuncDesc(PyObject *func)
{
- if (PyMethod_Check(func))
- return "()";
- else if (PyFunction_Check(func))
- return "()";
- else if (PyCFunction_Check(func))
- return "()";
- else
- return " object";
+ if (PyMethod_Check(func))
+ return "()";
+ else if (PyFunction_Check(func))
+ return "()";
+ else if (PyCFunction_Check(func))
+ return "()";
+ else
+ return " object";
}
static void
err_args(PyObject *func, int flags, int nargs)
{
- if (flags & METH_NOARGS)
- PyErr_Format(PyExc_TypeError,
- "%.200s() takes no arguments (%d given)",
- ((PyCFunctionObject *)func)->m_ml->ml_name,
- nargs);
- else
- PyErr_Format(PyExc_TypeError,
- "%.200s() takes exactly one argument (%d given)",
- ((PyCFunctionObject *)func)->m_ml->ml_name,
- nargs);
+ if (flags & METH_NOARGS)
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() takes no arguments (%d given)",
+ ((PyCFunctionObject *)func)->m_ml->ml_name,
+ nargs);
+ else
+ PyErr_Format(PyExc_TypeError,
+ "%.200s() takes exactly one argument (%d given)",
+ ((PyCFunctionObject *)func)->m_ml->ml_name,
+ nargs);
}
#define C_TRACE(x, call) \
if (tstate->use_tracing && tstate->c_profilefunc) { \
- if (call_trace(tstate->c_profilefunc, \
- tstate->c_profileobj, \
- tstate->frame, PyTrace_C_CALL, \
- func)) { \
- x = NULL; \
- } \
- else { \
- x = call; \
- if (tstate->c_profilefunc != NULL) { \
- if (x == NULL) { \
- call_trace_protected(tstate->c_profilefunc, \
- tstate->c_profileobj, \
- tstate->frame, PyTrace_C_EXCEPTION, \
- func); \
- /* XXX should pass (type, value, tb) */ \
- } else { \
- if (call_trace(tstate->c_profilefunc, \
- tstate->c_profileobj, \
- tstate->frame, PyTrace_C_RETURN, \
- func)) { \
- Py_DECREF(x); \
- x = NULL; \
- } \
- } \
- } \
- } \
+ if (call_trace(tstate->c_profilefunc, \
+ tstate->c_profileobj, \
+ tstate->frame, PyTrace_C_CALL, \
+ func)) { \
+ x = NULL; \
+ } \
+ else { \
+ x = call; \
+ if (tstate->c_profilefunc != NULL) { \
+ if (x == NULL) { \
+ call_trace_protected(tstate->c_profilefunc, \
+ tstate->c_profileobj, \
+ tstate->frame, PyTrace_C_EXCEPTION, \
+ func); \
+ /* XXX should pass (type, value, tb) */ \
+ } else { \
+ if (call_trace(tstate->c_profilefunc, \
+ tstate->c_profileobj, \
+ tstate->frame, PyTrace_C_RETURN, \
+ func)) { \
+ Py_DECREF(x); \
+ x = NULL; \
+ } \
+ } \
+ } \
+ } \
} else { \
- x = call; \
- }
+ x = call; \
+ }
static PyObject *
call_function(PyObject ***pp_stack, int oparg
#ifdef WITH_TSC
- , uint64* pintr0, uint64* pintr1
+ , uint64* pintr0, uint64* pintr1
#endif
- )
+ )
{
- int na = oparg & 0xff;
- int nk = (oparg>>8) & 0xff;
- int n = na + 2 * nk;
- PyObject **pfunc = (*pp_stack) - n - 1;
- PyObject *func = *pfunc;
- PyObject *x, *w;
-
- /* Always dispatch PyCFunction first, because these are
- presumed to be the most frequent callable object.
- */
- if (PyCFunction_Check(func) && nk == 0) {
- int flags = PyCFunction_GET_FLAGS(func);
- PyThreadState *tstate = PyThreadState_GET();
-
- PCALL(PCALL_CFUNCTION);
- if (flags & (METH_NOARGS | METH_O)) {
- PyCFunction meth = PyCFunction_GET_FUNCTION(func);
- PyObject *self = PyCFunction_GET_SELF(func);
- if (flags & METH_NOARGS && na == 0) {
- C_TRACE(x, (*meth)(self,NULL));
- }
- else if (flags & METH_O && na == 1) {
- PyObject *arg = EXT_POP(*pp_stack);
- C_TRACE(x, (*meth)(self,arg));
- Py_DECREF(arg);
- }
- else {
- err_args(func, flags, na);
- x = NULL;
- }
- }
- else {
- PyObject *callargs;
- callargs = load_args(pp_stack, na);
- READ_TIMESTAMP(*pintr0);
- C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
- READ_TIMESTAMP(*pintr1);
- Py_XDECREF(callargs);
- }
- } else {
- if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
- /* optimize access to bound methods */
- PyObject *self = PyMethod_GET_SELF(func);
- PCALL(PCALL_METHOD);
- PCALL(PCALL_BOUND_METHOD);
- Py_INCREF(self);
- func = PyMethod_GET_FUNCTION(func);
- Py_INCREF(func);
- Py_DECREF(*pfunc);
- *pfunc = self;
- na++;
- n++;
- } else
- Py_INCREF(func);
- READ_TIMESTAMP(*pintr0);
- if (PyFunction_Check(func))
- x = fast_function(func, pp_stack, n, na, nk);
- else
- x = do_call(func, pp_stack, na, nk);
- READ_TIMESTAMP(*pintr1);
- Py_DECREF(func);
- }
-
- /* Clear the stack of the function object. Also removes
- the arguments in case they weren't consumed already
- (fast_function() and err_args() leave them on the stack).
- */
- while ((*pp_stack) > pfunc) {
- w = EXT_POP(*pp_stack);
- Py_DECREF(w);
- PCALL(PCALL_POP);
- }
- return x;
+ int na = oparg & 0xff;
+ int nk = (oparg>>8) & 0xff;
+ int n = na + 2 * nk;
+ PyObject **pfunc = (*pp_stack) - n - 1;
+ PyObject *func = *pfunc;
+ PyObject *x, *w;
+
+ /* Always dispatch PyCFunction first, because these are
+ presumed to be the most frequent callable object.
+ */
+ if (PyCFunction_Check(func) && nk == 0) {
+ int flags = PyCFunction_GET_FLAGS(func);
+ PyThreadState *tstate = PyThreadState_GET();
+
+ PCALL(PCALL_CFUNCTION);
+ if (flags & (METH_NOARGS | METH_O)) {
+ PyCFunction meth = PyCFunction_GET_FUNCTION(func);
+ PyObject *self = PyCFunction_GET_SELF(func);
+ if (flags & METH_NOARGS && na == 0) {
+ C_TRACE(x, (*meth)(self,NULL));
+ }
+ else if (flags & METH_O && na == 1) {
+ PyObject *arg = EXT_POP(*pp_stack);
+ C_TRACE(x, (*meth)(self,arg));
+ Py_DECREF(arg);
+ }
+ else {
+ err_args(func, flags, na);
+ x = NULL;
+ }
+ }
+ else {
+ PyObject *callargs;
+ callargs = load_args(pp_stack, na);
+ READ_TIMESTAMP(*pintr0);
+ C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
+ READ_TIMESTAMP(*pintr1);
+ Py_XDECREF(callargs);
+ }
+ } else {
+ if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
+ /* optimize access to bound methods */
+ PyObject *self = PyMethod_GET_SELF(func);
+ PCALL(PCALL_METHOD);
+ PCALL(PCALL_BOUND_METHOD);
+ Py_INCREF(self);
+ func = PyMethod_GET_FUNCTION(func);
+ Py_INCREF(func);
+ Py_DECREF(*pfunc);
+ *pfunc = self;
+ na++;
+ n++;
+ } else
+ Py_INCREF(func);
+ READ_TIMESTAMP(*pintr0);
+ if (PyFunction_Check(func))
+ x = fast_function(func, pp_stack, n, na, nk);
+ else
+ x = do_call(func, pp_stack, na, nk);
+ READ_TIMESTAMP(*pintr1);
+ Py_DECREF(func);
+ }
+
+ /* Clear the stack of the function object. Also removes
+ the arguments in case they weren't consumed already
+ (fast_function() and err_args() leave them on the stack).
+ */
+ while ((*pp_stack) > pfunc) {
+ w = EXT_POP(*pp_stack);
+ Py_DECREF(w);
+ PCALL(PCALL_POP);
+ }
+ return x;
}
/* The fast_function() function optimize calls for which no argument
@@ -3811,275 +3811,275 @@ call_function(PyObject ***pp_stack, int oparg
static PyObject *
fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
{
- PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
- PyObject *globals = PyFunction_GET_GLOBALS(func);
- PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
- PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
- PyObject **d = NULL;
- int nd = 0;
-
- PCALL(PCALL_FUNCTION);
- PCALL(PCALL_FAST_FUNCTION);
- if (argdefs == NULL && co->co_argcount == n &&
- co->co_kwonlyargcount == 0 && nk==0 &&
- co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
- PyFrameObject *f;
- PyObject *retval = NULL;
- PyThreadState *tstate = PyThreadState_GET();
- PyObject **fastlocals, **stack;
- int i;
-
- PCALL(PCALL_FASTER_FUNCTION);
- assert(globals != NULL);
- /* XXX Perhaps we should create a specialized
- PyFrame_New() that doesn't take locals, but does
- take builtins without sanity checking them.
- */
- assert(tstate != NULL);
- f = PyFrame_New(tstate, co, globals, NULL);
- if (f == NULL)
- return NULL;
-
- fastlocals = f->f_localsplus;
- stack = (*pp_stack) - n;
-
- for (i = 0; i < n; i++) {
- Py_INCREF(*stack);
- fastlocals[i] = *stack++;
- }
- retval = PyEval_EvalFrameEx(f,0);
- ++tstate->recursion_depth;
- Py_DECREF(f);
- --tstate->recursion_depth;
- return retval;
- }
- if (argdefs != NULL) {
- d = &PyTuple_GET_ITEM(argdefs, 0);
- nd = Py_SIZE(argdefs);
- }
- return PyEval_EvalCodeEx(co, globals,
- (PyObject *)NULL, (*pp_stack)-n, na,
- (*pp_stack)-2*nk, nk, d, nd, kwdefs,
- PyFunction_GET_CLOSURE(func));
+ PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
+ PyObject *globals = PyFunction_GET_GLOBALS(func);
+ PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
+ PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
+ PyObject **d = NULL;
+ int nd = 0;
+
+ PCALL(PCALL_FUNCTION);
+ PCALL(PCALL_FAST_FUNCTION);
+ if (argdefs == NULL && co->co_argcount == n &&
+ co->co_kwonlyargcount == 0 && nk==0 &&
+ co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
+ PyFrameObject *f;
+ PyObject *retval = NULL;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject **fastlocals, **stack;
+ int i;
+
+ PCALL(PCALL_FASTER_FUNCTION);
+ assert(globals != NULL);
+ /* XXX Perhaps we should create a specialized
+ PyFrame_New() that doesn't take locals, but does
+ take builtins without sanity checking them.
+ */
+ assert(tstate != NULL);
+ f = PyFrame_New(tstate, co, globals, NULL);
+ if (f == NULL)
+ return NULL;
+
+ fastlocals = f->f_localsplus;
+ stack = (*pp_stack) - n;
+
+ for (i = 0; i < n; i++) {
+ Py_INCREF(*stack);
+ fastlocals[i] = *stack++;
+ }
+ retval = PyEval_EvalFrameEx(f,0);
+ ++tstate->recursion_depth;
+ Py_DECREF(f);
+ --tstate->recursion_depth;
+ return retval;
+ }
+ if (argdefs != NULL) {
+ d = &PyTuple_GET_ITEM(argdefs, 0);
+ nd = Py_SIZE(argdefs);
+ }
+ return PyEval_EvalCodeEx(co, globals,
+ (PyObject *)NULL, (*pp_stack)-n, na,
+ (*pp_stack)-2*nk, nk, d, nd, kwdefs,
+ PyFunction_GET_CLOSURE(func));
}
static PyObject *
update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
PyObject *func)
{
- PyObject *kwdict = NULL;
- if (orig_kwdict == NULL)
- kwdict = PyDict_New();
- else {
- kwdict = PyDict_Copy(orig_kwdict);
- Py_DECREF(orig_kwdict);
- }
- if (kwdict == NULL)
- return NULL;
- while (--nk >= 0) {
- int err;
- PyObject *value = EXT_POP(*pp_stack);
- PyObject *key = EXT_POP(*pp_stack);
- if (PyDict_GetItem(kwdict, key) != NULL) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%s got multiple values "
- "for keyword argument '%U'",
- PyEval_GetFuncName(func),
- PyEval_GetFuncDesc(func),
- key);
- Py_DECREF(key);
- Py_DECREF(value);
- Py_DECREF(kwdict);
- return NULL;
- }
- err = PyDict_SetItem(kwdict, key, value);
- Py_DECREF(key);
- Py_DECREF(value);
- if (err) {
- Py_DECREF(kwdict);
- return NULL;
- }
- }
- return kwdict;
+ PyObject *kwdict = NULL;
+ if (orig_kwdict == NULL)
+ kwdict = PyDict_New();
+ else {
+ kwdict = PyDict_Copy(orig_kwdict);
+ Py_DECREF(orig_kwdict);
+ }
+ if (kwdict == NULL)
+ return NULL;
+ while (--nk >= 0) {
+ int err;
+ PyObject *value = EXT_POP(*pp_stack);
+ PyObject *key = EXT_POP(*pp_stack);
+ if (PyDict_GetItem(kwdict, key) != NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s%s got multiple values "
+ "for keyword argument '%U'",
+ PyEval_GetFuncName(func),
+ PyEval_GetFuncDesc(func),
+ key);
+ Py_DECREF(key);
+ Py_DECREF(value);
+ Py_DECREF(kwdict);
+ return NULL;
+ }
+ err = PyDict_SetItem(kwdict, key, value);
+ Py_DECREF(key);
+ Py_DECREF(value);
+ if (err) {
+ Py_DECREF(kwdict);
+ return NULL;
+ }
+ }
+ return kwdict;
}
static PyObject *
update_star_args(int nstack, int nstar, PyObject *stararg,
- PyObject ***pp_stack)
+ PyObject ***pp_stack)
{
- PyObject *callargs, *w;
-
- callargs = PyTuple_New(nstack + nstar);
- if (callargs == NULL) {
- return NULL;
- }
- if (nstar) {
- int i;
- for (i = 0; i < nstar; i++) {
- PyObject *a = PyTuple_GET_ITEM(stararg, i);
- Py_INCREF(a);
- PyTuple_SET_ITEM(callargs, nstack + i, a);
- }
- }
- while (--nstack >= 0) {
- w = EXT_POP(*pp_stack);
- PyTuple_SET_ITEM(callargs, nstack, w);
- }
- return callargs;
+ PyObject *callargs, *w;
+
+ callargs = PyTuple_New(nstack + nstar);
+ if (callargs == NULL) {
+ return NULL;
+ }
+ if (nstar) {
+ int i;
+ for (i = 0; i < nstar; i++) {
+ PyObject *a = PyTuple_GET_ITEM(stararg, i);
+ Py_INCREF(a);
+ PyTuple_SET_ITEM(callargs, nstack + i, a);
+ }
+ }
+ while (--nstack >= 0) {
+ w = EXT_POP(*pp_stack);
+ PyTuple_SET_ITEM(callargs, nstack, w);
+ }
+ return callargs;
}
static PyObject *
load_args(PyObject ***pp_stack, int na)
{
- PyObject *args = PyTuple_New(na);
- PyObject *w;
-
- if (args == NULL)
- return NULL;
- while (--na >= 0) {
- w = EXT_POP(*pp_stack);
- PyTuple_SET_ITEM(args, na, w);
- }
- return args;
+ PyObject *args = PyTuple_New(na);
+ PyObject *w;
+
+ if (args == NULL)
+ return NULL;
+ while (--na >= 0) {
+ w = EXT_POP(*pp_stack);
+ PyTuple_SET_ITEM(args, na, w);
+ }
+ return args;
}
static PyObject *
do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
{
- PyObject *callargs = NULL;
- PyObject *kwdict = NULL;
- PyObject *result = NULL;
-
- if (nk > 0) {
- kwdict = update_keyword_args(NULL, nk, pp_stack, func);
- if (kwdict == NULL)
- goto call_fail;
- }
- callargs = load_args(pp_stack, na);
- if (callargs == NULL)
- goto call_fail;
+ PyObject *callargs = NULL;
+ PyObject *kwdict = NULL;
+ PyObject *result = NULL;
+
+ if (nk > 0) {
+ kwdict = update_keyword_args(NULL, nk, pp_stack, func);
+ if (kwdict == NULL)
+ goto call_fail;
+ }
+ callargs = load_args(pp_stack, na);
+ if (callargs == NULL)
+ goto call_fail;
#ifdef CALL_PROFILE
- /* At this point, we have to look at the type of func to
- update the call stats properly. Do it here so as to avoid
- exposing the call stats machinery outside ceval.c
- */
- if (PyFunction_Check(func))
- PCALL(PCALL_FUNCTION);
- else if (PyMethod_Check(func))
- PCALL(PCALL_METHOD);
- else if (PyType_Check(func))
- PCALL(PCALL_TYPE);
- else if (PyCFunction_Check(func))
- PCALL(PCALL_CFUNCTION);
- else
- PCALL(PCALL_OTHER);
+ /* At this point, we have to look at the type of func to
+ update the call stats properly. Do it here so as to avoid
+ exposing the call stats machinery outside ceval.c
+ */
+ if (PyFunction_Check(func))
+ PCALL(PCALL_FUNCTION);
+ else if (PyMethod_Check(func))
+ PCALL(PCALL_METHOD);
+ else if (PyType_Check(func))
+ PCALL(PCALL_TYPE);
+ else if (PyCFunction_Check(func))
+ PCALL(PCALL_CFUNCTION);
+ else
+ PCALL(PCALL_OTHER);
#endif
- if (PyCFunction_Check(func)) {
- PyThreadState *tstate = PyThreadState_GET();
- C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
- }
- else
- result = PyObject_Call(func, callargs, kwdict);
+ if (PyCFunction_Check(func)) {
+ PyThreadState *tstate = PyThreadState_GET();
+ C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
+ }
+ else
+ result = PyObject_Call(func, callargs, kwdict);
call_fail:
- Py_XDECREF(callargs);
- Py_XDECREF(kwdict);
- return result;
+ Py_XDECREF(callargs);
+ Py_XDECREF(kwdict);
+ return result;
}
static PyObject *
ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
{
- int nstar = 0;
- PyObject *callargs = NULL;
- PyObject *stararg = NULL;
- PyObject *kwdict = NULL;
- PyObject *result = NULL;
-
- if (flags & CALL_FLAG_KW) {
- kwdict = EXT_POP(*pp_stack);
- if (!PyDict_Check(kwdict)) {
- PyObject *d;
- d = PyDict_New();
- if (d == NULL)
- goto ext_call_fail;
- if (PyDict_Update(d, kwdict) != 0) {
- Py_DECREF(d);
- /* PyDict_Update raises attribute
- * error (percolated from an attempt
- * to get 'keys' attribute) instead of
- * a type error if its second argument
- * is not a mapping.
- */
- if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%.200s argument after ** "
- "must be a mapping, not %.200s",
- PyEval_GetFuncName(func),
- PyEval_GetFuncDesc(func),
- kwdict->ob_type->tp_name);
- }
- goto ext_call_fail;
- }
- Py_DECREF(kwdict);
- kwdict = d;
- }
- }
- if (flags & CALL_FLAG_VAR) {
- stararg = EXT_POP(*pp_stack);
- if (!PyTuple_Check(stararg)) {
- PyObject *t = NULL;
- t = PySequence_Tuple(stararg);
- if (t == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError)) {
- PyErr_Format(PyExc_TypeError,
- "%.200s%.200s argument after * "
- "must be a sequence, not %200s",
- PyEval_GetFuncName(func),
- PyEval_GetFuncDesc(func),
- stararg->ob_type->tp_name);
- }
- goto ext_call_fail;
- }
- Py_DECREF(stararg);
- stararg = t;
- }
- nstar = PyTuple_GET_SIZE(stararg);
- }
- if (nk > 0) {
- kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
- if (kwdict == NULL)
- goto ext_call_fail;
- }
- callargs = update_star_args(na, nstar, stararg, pp_stack);
- if (callargs == NULL)
- goto ext_call_fail;
+ int nstar = 0;
+ PyObject *callargs = NULL;
+ PyObject *stararg = NULL;
+ PyObject *kwdict = NULL;
+ PyObject *result = NULL;
+
+ if (flags & CALL_FLAG_KW) {
+ kwdict = EXT_POP(*pp_stack);
+ if (!PyDict_Check(kwdict)) {
+ PyObject *d;
+ d = PyDict_New();
+ if (d == NULL)
+ goto ext_call_fail;
+ if (PyDict_Update(d, kwdict) != 0) {
+ Py_DECREF(d);
+ /* PyDict_Update raises attribute
+ * error (percolated from an attempt
+ * to get 'keys' attribute) instead of
+ * a type error if its second argument
+ * is not a mapping.
+ */
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s%.200s argument after ** "
+ "must be a mapping, not %.200s",
+ PyEval_GetFuncName(func),
+ PyEval_GetFuncDesc(func),
+ kwdict->ob_type->tp_name);
+ }
+ goto ext_call_fail;
+ }
+ Py_DECREF(kwdict);
+ kwdict = d;
+ }
+ }
+ if (flags & CALL_FLAG_VAR) {
+ stararg = EXT_POP(*pp_stack);
+ if (!PyTuple_Check(stararg)) {
+ PyObject *t = NULL;
+ t = PySequence_Tuple(stararg);
+ if (t == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s%.200s argument after * "
+ "must be a sequence, not %200s",
+ PyEval_GetFuncName(func),
+ PyEval_GetFuncDesc(func),
+ stararg->ob_type->tp_name);
+ }
+ goto ext_call_fail;
+ }
+ Py_DECREF(stararg);
+ stararg = t;
+ }
+ nstar = PyTuple_GET_SIZE(stararg);
+ }
+ if (nk > 0) {
+ kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
+ if (kwdict == NULL)
+ goto ext_call_fail;
+ }
+ callargs = update_star_args(na, nstar, stararg, pp_stack);
+ if (callargs == NULL)
+ goto ext_call_fail;
#ifdef CALL_PROFILE
- /* At this point, we have to look at the type of func to
- update the call stats properly. Do it here so as to avoid
- exposing the call stats machinery outside ceval.c
- */
- if (PyFunction_Check(func))
- PCALL(PCALL_FUNCTION);
- else if (PyMethod_Check(func))
- PCALL(PCALL_METHOD);
- else if (PyType_Check(func))
- PCALL(PCALL_TYPE);
- else if (PyCFunction_Check(func))
- PCALL(PCALL_CFUNCTION);
- else
- PCALL(PCALL_OTHER);
+ /* At this point, we have to look at the type of func to
+ update the call stats properly. Do it here so as to avoid
+ exposing the call stats machinery outside ceval.c
+ */
+ if (PyFunction_Check(func))
+ PCALL(PCALL_FUNCTION);
+ else if (PyMethod_Check(func))
+ PCALL(PCALL_METHOD);
+ else if (PyType_Check(func))
+ PCALL(PCALL_TYPE);
+ else if (PyCFunction_Check(func))
+ PCALL(PCALL_CFUNCTION);
+ else
+ PCALL(PCALL_OTHER);
#endif
- if (PyCFunction_Check(func)) {
- PyThreadState *tstate = PyThreadState_GET();
- C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
- }
- else
- result = PyObject_Call(func, callargs, kwdict);
+ if (PyCFunction_Check(func)) {
+ PyThreadState *tstate = PyThreadState_GET();
+ C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
+ }
+ else
+ result = PyObject_Call(func, callargs, kwdict);
ext_call_fail:
- Py_XDECREF(callargs);
- Py_XDECREF(kwdict);
- Py_XDECREF(stararg);
- return result;
+ Py_XDECREF(callargs);
+ Py_XDECREF(kwdict);
+ Py_XDECREF(stararg);
+ return result;
}
/* Extract a slice index from a PyInt or PyLong or an object with the
@@ -4095,245 +4095,245 @@ ext_call_fail:
int
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
{
- if (v != NULL) {
- Py_ssize_t x;
- if (PyIndex_Check(v)) {
- x = PyNumber_AsSsize_t(v, NULL);
- if (x == -1 && PyErr_Occurred())
- return 0;
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "slice indices must be integers or "
- "None or have an __index__ method");
- return 0;
- }
- *pi = x;
- }
- return 1;
+ if (v != NULL) {
+ Py_ssize_t x;
+ if (PyIndex_Check(v)) {
+ x = PyNumber_AsSsize_t(v, NULL);
+ if (x == -1 && PyErr_Occurred())
+ return 0;
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "slice indices must be integers or "
+ "None or have an __index__ method");
+ return 0;
+ }
+ *pi = x;
+ }
+ return 1;
}
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
- "BaseException is not allowed"
+ "BaseException is not allowed"
static PyObject *
cmp_outcome(int op, register PyObject *v, register PyObject *w)
{
- int res = 0;
- switch (op) {
- case PyCmp_IS:
- res = (v == w);
- break;
- case PyCmp_IS_NOT:
- res = (v != w);
- break;
- case PyCmp_IN:
- res = PySequence_Contains(w, v);
- if (res < 0)
- return NULL;
- break;
- case PyCmp_NOT_IN:
- res = PySequence_Contains(w, v);
- if (res < 0)
- return NULL;
- res = !res;
- break;
- case PyCmp_EXC_MATCH:
- if (PyTuple_Check(w)) {
- Py_ssize_t i, length;
- length = PyTuple_Size(w);
- for (i = 0; i < length; i += 1) {
- PyObject *exc = PyTuple_GET_ITEM(w, i);
- if (!PyExceptionClass_Check(exc)) {
- PyErr_SetString(PyExc_TypeError,
- CANNOT_CATCH_MSG);
- return NULL;
- }
- }
- }
- else {
- if (!PyExceptionClass_Check(w)) {
- PyErr_SetString(PyExc_TypeError,
- CANNOT_CATCH_MSG);
- return NULL;
- }
- }
- res = PyErr_GivenExceptionMatches(v, w);
- break;
- default:
- return PyObject_RichCompare(v, w, op);
- }
- v = res ? Py_True : Py_False;
- Py_INCREF(v);
- return v;
+ int res = 0;
+ switch (op) {
+ case PyCmp_IS:
+ res = (v == w);
+ break;
+ case PyCmp_IS_NOT:
+ res = (v != w);
+ break;
+ case PyCmp_IN:
+ res = PySequence_Contains(w, v);
+ if (res < 0)
+ return NULL;
+ break;
+ case PyCmp_NOT_IN:
+ res = PySequence_Contains(w, v);
+ if (res < 0)
+ return NULL;
+ res = !res;
+ break;
+ case PyCmp_EXC_MATCH:
+ if (PyTuple_Check(w)) {
+ Py_ssize_t i, length;
+ length = PyTuple_Size(w);
+ for (i = 0; i < length; i += 1) {
+ PyObject *exc = PyTuple_GET_ITEM(w, i);
+ if (!PyExceptionClass_Check(exc)) {
+ PyErr_SetString(PyExc_TypeError,
+ CANNOT_CATCH_MSG);
+ return NULL;
+ }
+ }
+ }
+ else {
+ if (!PyExceptionClass_Check(w)) {
+ PyErr_SetString(PyExc_TypeError,
+ CANNOT_CATCH_MSG);
+ return NULL;
+ }
+ }
+ res = PyErr_GivenExceptionMatches(v, w);
+ break;
+ default:
+ return PyObject_RichCompare(v, w, op);
+ }
+ v = res ? Py_True : Py_False;
+ Py_INCREF(v);
+ return v;
}
static PyObject *
import_from(PyObject *v, PyObject *name)
{
- PyObject *x;
+ PyObject *x;
- x = PyObject_GetAttr(v, name);
- if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
- PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
- }
- return x;
+ x = PyObject_GetAttr(v, name);
+ if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
+ }
+ return x;
}
static int
import_all_from(PyObject *locals, PyObject *v)
{
- PyObject *all = PyObject_GetAttrString(v, "__all__");
- PyObject *dict, *name, *value;
- int skip_leading_underscores = 0;
- int pos, err;
-
- if (all == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1; /* Unexpected error */
- PyErr_Clear();
- dict = PyObject_GetAttrString(v, "__dict__");
- if (dict == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_AttributeError))
- return -1;
- PyErr_SetString(PyExc_ImportError,
- "from-import-* object has no __dict__ and no __all__");
- return -1;
- }
- all = PyMapping_Keys(dict);
- Py_DECREF(dict);
- if (all == NULL)
- return -1;
- skip_leading_underscores = 1;
- }
-
- for (pos = 0, err = 0; ; pos++) {
- name = PySequence_GetItem(all, pos);
- if (name == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_IndexError))
- err = -1;
- else
- PyErr_Clear();
- break;
- }
- if (skip_leading_underscores &&
- PyUnicode_Check(name) &&
- PyUnicode_AS_UNICODE(name)[0] == '_')
- {
- Py_DECREF(name);
- continue;
- }
- value = PyObject_GetAttr(v, name);
- if (value == NULL)
- err = -1;
- else if (PyDict_CheckExact(locals))
- err = PyDict_SetItem(locals, name, value);
- else
- err = PyObject_SetItem(locals, name, value);
- Py_DECREF(name);
- Py_XDECREF(value);
- if (err != 0)
- break;
- }
- Py_DECREF(all);
- return err;
+ PyObject *all = PyObject_GetAttrString(v, "__all__");
+ PyObject *dict, *name, *value;
+ int skip_leading_underscores = 0;
+ int pos, err;
+
+ if (all == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return -1; /* Unexpected error */
+ PyErr_Clear();
+ dict = PyObject_GetAttrString(v, "__dict__");
+ if (dict == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return -1;
+ PyErr_SetString(PyExc_ImportError,
+ "from-import-* object has no __dict__ and no __all__");
+ return -1;
+ }
+ all = PyMapping_Keys(dict);
+ Py_DECREF(dict);
+ if (all == NULL)
+ return -1;
+ skip_leading_underscores = 1;
+ }
+
+ for (pos = 0, err = 0; ; pos++) {
+ name = PySequence_GetItem(all, pos);
+ if (name == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_IndexError))
+ err = -1;
+ else
+ PyErr_Clear();
+ break;
+ }
+ if (skip_leading_underscores &&
+ PyUnicode_Check(name) &&
+ PyUnicode_AS_UNICODE(name)[0] == '_')
+ {
+ Py_DECREF(name);
+ continue;
+ }
+ value = PyObject_GetAttr(v, name);
+ if (value == NULL)
+ err = -1;
+ else if (PyDict_CheckExact(locals))
+ err = PyDict_SetItem(locals, name, value);
+ else
+ err = PyObject_SetItem(locals, name, value);
+ Py_DECREF(name);
+ Py_XDECREF(value);
+ if (err != 0)
+ break;
+ }
+ Py_DECREF(all);
+ return err;
}
static void
format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
{
- const char *obj_str;
+ const char *obj_str;
- if (!obj)
- return;
+ if (!obj)
+ return;
- obj_str = _PyUnicode_AsString(obj);
- if (!obj_str)
- return;
+ obj_str = _PyUnicode_AsString(obj);
+ if (!obj_str)
+ return;
- PyErr_Format(exc, format_str, obj_str);
+ PyErr_Format(exc, format_str, obj_str);
}
static PyObject *
unicode_concatenate(PyObject *v, PyObject *w,
- PyFrameObject *f, unsigned char *next_instr)
+ PyFrameObject *f, unsigned char *next_instr)
{
- /* This function implements 'variable += expr' when both arguments
- are (Unicode) strings. */
- Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
- Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
- Py_ssize_t new_len = v_len + w_len;
- if (new_len < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "strings are too large to concat");
- return NULL;
- }
-
- if (v->ob_refcnt == 2) {
- /* In the common case, there are 2 references to the value
- * stored in 'variable' when the += is performed: one on the
- * value stack (in 'v') and one still stored in the
- * 'variable'. We try to delete the variable now to reduce
- * the refcnt to 1.
- */
- switch (*next_instr) {
- case STORE_FAST:
- {
- int oparg = PEEKARG();
- PyObject **fastlocals = f->f_localsplus;
- if (GETLOCAL(oparg) == v)
- SETLOCAL(oparg, NULL);
- break;
- }
- case STORE_DEREF:
- {
- PyObject **freevars = (f->f_localsplus +
- f->f_code->co_nlocals);
- PyObject *c = freevars[PEEKARG()];
- if (PyCell_GET(c) == v)
- PyCell_Set(c, NULL);
- break;
- }
- case STORE_NAME:
- {
- PyObject *names = f->f_code->co_names;
- PyObject *name = GETITEM(names, PEEKARG());
- PyObject *locals = f->f_locals;
- if (PyDict_CheckExact(locals) &&
- PyDict_GetItem(locals, name) == v) {
- if (PyDict_DelItem(locals, name) != 0) {
- PyErr_Clear();
- }
- }
- break;
- }
- }
- }
-
- if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
- /* Now we own the last reference to 'v', so we can resize it
- * in-place.
- */
- if (PyUnicode_Resize(&v, new_len) != 0) {
- /* XXX if PyUnicode_Resize() fails, 'v' has been
- * deallocated so it cannot be put back into
- * 'variable'. The MemoryError is raised when there
- * is no value in 'variable', which might (very
- * remotely) be a cause of incompatibilities.
- */
- return NULL;
- }
- /* copy 'w' into the newly allocated area of 'v' */
- memcpy(PyUnicode_AS_UNICODE(v) + v_len,
- PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
- return v;
- }
- else {
- /* When in-place resizing is not an option. */
- w = PyUnicode_Concat(v, w);
- Py_DECREF(v);
- return w;
- }
+ /* This function implements 'variable += expr' when both arguments
+ are (Unicode) strings. */
+ Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
+ Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
+ Py_ssize_t new_len = v_len + w_len;
+ if (new_len < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "strings are too large to concat");
+ return NULL;
+ }
+
+ if (v->ob_refcnt == 2) {
+ /* In the common case, there are 2 references to the value
+ * stored in 'variable' when the += is performed: one on the
+ * value stack (in 'v') and one still stored in the
+ * 'variable'. We try to delete the variable now to reduce
+ * the refcnt to 1.
+ */
+ switch (*next_instr) {
+ case STORE_FAST:
+ {
+ int oparg = PEEKARG();
+ PyObject **fastlocals = f->f_localsplus;
+ if (GETLOCAL(oparg) == v)
+ SETLOCAL(oparg, NULL);
+ break;
+ }
+ case STORE_DEREF:
+ {
+ PyObject **freevars = (f->f_localsplus +
+ f->f_code->co_nlocals);
+ PyObject *c = freevars[PEEKARG()];
+ if (PyCell_GET(c) == v)
+ PyCell_Set(c, NULL);
+ break;
+ }
+ case STORE_NAME:
+ {
+ PyObject *names = f->f_code->co_names;
+ PyObject *name = GETITEM(names, PEEKARG());
+ PyObject *locals = f->f_locals;
+ if (PyDict_CheckExact(locals) &&
+ PyDict_GetItem(locals, name) == v) {
+ if (PyDict_DelItem(locals, name) != 0) {
+ PyErr_Clear();
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
+ /* Now we own the last reference to 'v', so we can resize it
+ * in-place.
+ */
+ if (PyUnicode_Resize(&v, new_len) != 0) {
+ /* XXX if PyUnicode_Resize() fails, 'v' has been
+ * deallocated so it cannot be put back into
+ * 'variable'. The MemoryError is raised when there
+ * is no value in 'variable', which might (very
+ * remotely) be a cause of incompatibilities.
+ */
+ return NULL;
+ }
+ /* copy 'w' into the newly allocated area of 'v' */
+ memcpy(PyUnicode_AS_UNICODE(v) + v_len,
+ PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
+ return v;
+ }
+ else {
+ /* When in-place resizing is not an option. */
+ w = PyUnicode_Concat(v, w);
+ Py_DECREF(v);
+ return w;
+ }
}
#ifdef DYNAMIC_EXECUTION_PROFILE
@@ -4341,40 +4341,40 @@ unicode_concatenate(PyObject *v, PyObject *w,
static PyObject *
getarray(long a[256])
{
- int i;
- PyObject *l = PyList_New(256);
- if (l == NULL) return NULL;
- for (i = 0; i < 256; i++) {
- PyObject *x = PyLong_FromLong(a[i]);
- if (x == NULL) {
- Py_DECREF(l);
- return NULL;
- }
- PyList_SetItem(l, i, x);
- }
- for (i = 0; i < 256; i++)
- a[i] = 0;
- return l;
+ int i;
+ PyObject *l = PyList_New(256);
+ if (l == NULL) return NULL;
+ for (i = 0; i < 256; i++) {
+ PyObject *x = PyLong_FromLong(a[i]);
+ if (x == NULL) {
+ Py_DECREF(l);
+ return NULL;
+ }
+ PyList_SetItem(l, i, x);
+ }
+ for (i = 0; i < 256; i++)
+ a[i] = 0;
+ return l;
}
PyObject *
_Py_GetDXProfile(PyObject *self, PyObject *args)
{
#ifndef DXPAIRS
- return getarray(dxp);
+ return getarray(dxp);
#else
- int i;
- PyObject *l = PyList_New(257);
- if (l == NULL) return NULL;
- for (i = 0; i < 257; i++) {
- PyObject *x = getarray(dxpairs[i]);
- if (x == NULL) {
- Py_DECREF(l);
- return NULL;
- }
- PyList_SetItem(l, i, x);
- }
- return l;
+ int i;
+ PyObject *l = PyList_New(257);
+ if (l == NULL) return NULL;
+ for (i = 0; i < 257; i++) {
+ PyObject *x = getarray(dxpairs[i]);
+ if (x == NULL) {
+ Py_DECREF(l);
+ return NULL;
+ }
+ PyList_SetItem(l, i, x);
+ }
+ return l;
#endif
}
diff --git a/Python/codecs.c b/Python/codecs.c
index e6ffa0d..04487a2 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -30,14 +30,14 @@ int PyCodec_Register(PyObject *search_function)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
- goto onError;
+ goto onError;
if (search_function == NULL) {
- PyErr_BadArgument();
- goto onError;
+ PyErr_BadArgument();
+ goto onError;
}
if (!PyCallable_Check(search_function)) {
- PyErr_SetString(PyExc_TypeError, "argument must be callable");
- goto onError;
+ PyErr_SetString(PyExc_TypeError, "argument must be callable");
+ goto onError;
}
return PyList_Append(interp->codec_search_path, search_function);
@@ -57,8 +57,8 @@ PyObject *normalizestring(const char *string)
PyObject *v;
if (len > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError, "string is too large");
- return NULL;
+ PyErr_SetString(PyExc_OverflowError, "string is too large");
+ return NULL;
}
p = PyMem_Malloc(len + 1);
@@ -70,7 +70,7 @@ PyObject *normalizestring(const char *string)
ch = '-';
else
ch = tolower(Py_CHARMASK(ch));
- p[i] = ch;
+ p[i] = ch;
}
p[i] = '\0';
v = PyUnicode_FromString(p);
@@ -102,78 +102,78 @@ PyObject *_PyCodec_Lookup(const char *encoding)
Py_ssize_t i, len;
if (encoding == NULL) {
- PyErr_BadArgument();
- goto onError;
+ PyErr_BadArgument();
+ goto onError;
}
interp = PyThreadState_GET()->interp;
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
- goto onError;
+ goto onError;
/* Convert the encoding to a normalized Python string: all
characters are converted to lower case, spaces and hyphens are
replaced with underscores. */
v = normalizestring(encoding);
if (v == NULL)
- goto onError;
+ goto onError;
PyUnicode_InternInPlace(&v);
/* First, try to lookup the name in the registry dictionary */
result = PyDict_GetItem(interp->codec_search_cache, v);
if (result != NULL) {
- Py_INCREF(result);
- Py_DECREF(v);
- return result;
+ Py_INCREF(result);
+ Py_DECREF(v);
+ return result;
}
/* Next, scan the search functions in order of registration */
args = PyTuple_New(1);
if (args == NULL)
- goto onError;
+ goto onError;
PyTuple_SET_ITEM(args,0,v);
len = PyList_Size(interp->codec_search_path);
if (len < 0)
- goto onError;
+ goto onError;
if (len == 0) {
- PyErr_SetString(PyExc_LookupError,
- "no codec search functions registered: "
- "can't find encoding");
- goto onError;
+ PyErr_SetString(PyExc_LookupError,
+ "no codec search functions registered: "
+ "can't find encoding");
+ goto onError;
}
for (i = 0; i < len; i++) {
- PyObject *func;
-
- func = PyList_GetItem(interp->codec_search_path, i);
- if (func == NULL)
- goto onError;
- result = PyEval_CallObject(func, args);
- if (result == NULL)
- goto onError;
- if (result == Py_None) {
- Py_DECREF(result);
- continue;
- }
- if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
- PyErr_SetString(PyExc_TypeError,
- "codec search functions must return 4-tuples");
- Py_DECREF(result);
- goto onError;
- }
- break;
+ PyObject *func;
+
+ func = PyList_GetItem(interp->codec_search_path, i);
+ if (func == NULL)
+ goto onError;
+ result = PyEval_CallObject(func, args);
+ if (result == NULL)
+ goto onError;
+ if (result == Py_None) {
+ Py_DECREF(result);
+ continue;
+ }
+ if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
+ PyErr_SetString(PyExc_TypeError,
+ "codec search functions must return 4-tuples");
+ Py_DECREF(result);
+ goto onError;
+ }
+ break;
}
if (i == len) {
- /* XXX Perhaps we should cache misses too ? */
- PyErr_Format(PyExc_LookupError,
+ /* XXX Perhaps we should cache misses too ? */
+ PyErr_Format(PyExc_LookupError,
"unknown encoding: %s", encoding);
- goto onError;
+ goto onError;
}
/* Cache and return the result */
if (PyDict_SetItem(interp->codec_search_cache, v, result) < 0) {
- Py_DECREF(result);
- goto onError;
+ Py_DECREF(result);
+ goto onError;
}
Py_DECREF(args);
return result;
@@ -188,38 +188,38 @@ PyObject *_PyCodec_Lookup(const char *encoding)
int PyCodec_KnownEncoding(const char *encoding)
{
PyObject *codecs;
-
+
codecs = _PyCodec_Lookup(encoding);
if (!codecs) {
- PyErr_Clear();
- return 0;
+ PyErr_Clear();
+ return 0;
}
else {
- Py_DECREF(codecs);
- return 1;
+ Py_DECREF(codecs);
+ return 1;
}
}
static
PyObject *args_tuple(PyObject *object,
- const char *errors)
+ const char *errors)
{
PyObject *args;
args = PyTuple_New(1 + (errors != NULL));
if (args == NULL)
- return NULL;
+ return NULL;
Py_INCREF(object);
PyTuple_SET_ITEM(args,0,object);
if (errors) {
- PyObject *v;
-
- v = PyUnicode_FromString(errors);
- if (v == NULL) {
- Py_DECREF(args);
- return NULL;
- }
- PyTuple_SET_ITEM(args, 1, v);
+ PyObject *v;
+
+ v = PyUnicode_FromString(errors);
+ if (v == NULL) {
+ Py_DECREF(args);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(args, 1, v);
}
return args;
}
@@ -234,7 +234,7 @@ PyObject *codec_getitem(const char *encoding, int index)
codecs = _PyCodec_Lookup(encoding);
if (codecs == NULL)
- return NULL;
+ return NULL;
v = PyTuple_GET_ITEM(codecs, index);
Py_DECREF(codecs);
Py_INCREF(v);
@@ -245,22 +245,22 @@ PyObject *codec_getitem(const char *encoding, int index)
static
PyObject *codec_getincrementalcodec(const char *encoding,
- const char *errors,
- const char *attrname)
+ const char *errors,
+ const char *attrname)
{
PyObject *codecs, *ret, *inccodec;
codecs = _PyCodec_Lookup(encoding);
if (codecs == NULL)
- return NULL;
+ return NULL;
inccodec = PyObject_GetAttrString(codecs, attrname);
Py_DECREF(codecs);
if (inccodec == NULL)
- return NULL;
+ return NULL;
if (errors)
- ret = PyObject_CallFunction(inccodec, "s", errors);
+ ret = PyObject_CallFunction(inccodec, "s", errors);
else
- ret = PyObject_CallFunction(inccodec, NULL);
+ ret = PyObject_CallFunction(inccodec, NULL);
Py_DECREF(inccodec);
return ret;
}
@@ -269,21 +269,21 @@ PyObject *codec_getincrementalcodec(const char *encoding,
static
PyObject *codec_getstreamcodec(const char *encoding,
- PyObject *stream,
- const char *errors,
- const int index)
+ PyObject *stream,
+ const char *errors,
+ const int index)
{
PyObject *codecs, *streamcodec, *codeccls;
codecs = _PyCodec_Lookup(encoding);
if (codecs == NULL)
- return NULL;
+ return NULL;
codeccls = PyTuple_GET_ITEM(codecs, index);
if (errors != NULL)
- streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
+ streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
else
- streamcodec = PyObject_CallFunction(codeccls, "O", stream);
+ streamcodec = PyObject_CallFunction(codeccls, "O", stream);
Py_DECREF(codecs);
return streamcodec;
}
@@ -305,27 +305,27 @@ PyObject *PyCodec_Decoder(const char *encoding)
}
PyObject *PyCodec_IncrementalEncoder(const char *encoding,
- const char *errors)
+ const char *errors)
{
return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
}
PyObject *PyCodec_IncrementalDecoder(const char *encoding,
- const char *errors)
+ const char *errors)
{
return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
}
PyObject *PyCodec_StreamReader(const char *encoding,
- PyObject *stream,
- const char *errors)
+ PyObject *stream,
+ const char *errors)
{
return codec_getstreamcodec(encoding, stream, errors, 2);
}
PyObject *PyCodec_StreamWriter(const char *encoding,
- PyObject *stream,
- const char *errors)
+ PyObject *stream,
+ const char *errors)
{
return codec_getstreamcodec(encoding, stream, errors, 3);
}
@@ -336,8 +336,8 @@ PyObject *PyCodec_StreamWriter(const char *encoding,
errors is passed to the encoder factory as argument if non-NULL. */
PyObject *PyCodec_Encode(PyObject *object,
- const char *encoding,
- const char *errors)
+ const char *encoding,
+ const char *errors)
{
PyObject *encoder = NULL;
PyObject *args = NULL, *result = NULL;
@@ -345,21 +345,21 @@ PyObject *PyCodec_Encode(PyObject *object,
encoder = PyCodec_Encoder(encoding);
if (encoder == NULL)
- goto onError;
+ goto onError;
args = args_tuple(object, errors);
if (args == NULL)
- goto onError;
+ goto onError;
result = PyEval_CallObject(encoder, args);
if (result == NULL)
- goto onError;
+ goto onError;
if (!PyTuple_Check(result) ||
- PyTuple_GET_SIZE(result) != 2) {
- PyErr_SetString(PyExc_TypeError,
- "encoder must return a tuple (object, integer)");
- goto onError;
+ PyTuple_GET_SIZE(result) != 2) {
+ PyErr_SetString(PyExc_TypeError,
+ "encoder must return a tuple (object, integer)");
+ goto onError;
}
v = PyTuple_GET_ITEM(result,0);
Py_INCREF(v);
@@ -369,7 +369,7 @@ PyObject *PyCodec_Encode(PyObject *object,
Py_DECREF(encoder);
Py_DECREF(result);
return v;
-
+
onError:
Py_XDECREF(result);
Py_XDECREF(args);
@@ -383,8 +383,8 @@ PyObject *PyCodec_Encode(PyObject *object,
errors is passed to the decoder factory as argument if non-NULL. */
PyObject *PyCodec_Decode(PyObject *object,
- const char *encoding,
- const char *errors)
+ const char *encoding,
+ const char *errors)
{
PyObject *decoder = NULL;
PyObject *args = NULL, *result = NULL;
@@ -392,20 +392,20 @@ PyObject *PyCodec_Decode(PyObject *object,
decoder = PyCodec_Decoder(encoding);
if (decoder == NULL)
- goto onError;
+ goto onError;
args = args_tuple(object, errors);
if (args == NULL)
- goto onError;
+ goto onError;
result = PyEval_CallObject(decoder,args);
if (result == NULL)
- goto onError;
+ goto onError;
if (!PyTuple_Check(result) ||
- PyTuple_GET_SIZE(result) != 2) {
- PyErr_SetString(PyExc_TypeError,
- "decoder must return a tuple (object,integer)");
- goto onError;
+ PyTuple_GET_SIZE(result) != 2) {
+ PyErr_SetString(PyExc_TypeError,
+ "decoder must return a tuple (object,integer)");
+ goto onError;
}
v = PyTuple_GET_ITEM(result,0);
Py_INCREF(v);
@@ -433,13 +433,13 @@ int PyCodec_RegisterError(const char *name, PyObject *error)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
- return -1;
+ return -1;
if (!PyCallable_Check(error)) {
- PyErr_SetString(PyExc_TypeError, "handler must be callable");
- return -1;
+ PyErr_SetString(PyExc_TypeError, "handler must be callable");
+ return -1;
}
return PyDict_SetItemString(interp->codec_error_registry,
- (char *)name, error);
+ (char *)name, error);
}
/* Lookup the error handling callback function registered under the
@@ -451,15 +451,15 @@ PyObject *PyCodec_LookupError(const char *name)
PyInterpreterState *interp = PyThreadState_GET()->interp;
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
- return NULL;
+ return NULL;
if (name==NULL)
- name = "strict";
+ name = "strict";
handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
if (!handler)
- PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
+ PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
else
- Py_INCREF(handler);
+ Py_INCREF(handler);
return handler;
}
@@ -482,7 +482,7 @@ PyObject *PyCodec_StrictErrors(PyObject *exc)
if (PyExceptionInstance_Check(exc))
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
else
- PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
+ PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
return NULL;
}
@@ -491,20 +491,20 @@ PyObject *PyCodec_IgnoreErrors(PyObject *exc)
{
Py_ssize_t end;
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
- if (PyUnicodeDecodeError_GetEnd(exc, &end))
- return NULL;
+ if (PyUnicodeDecodeError_GetEnd(exc, &end))
+ return NULL;
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
- if (PyUnicodeTranslateError_GetEnd(exc, &end))
- return NULL;
+ if (PyUnicodeTranslateError_GetEnd(exc, &end))
+ return NULL;
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
/* ouch: passing NULL, 0, pos gives None instead of u'' */
return Py_BuildValue("(u#n)", &end, 0, end);
@@ -519,155 +519,155 @@ PyObject *PyCodec_ReplaceErrors(PyObject *exc)
Py_ssize_t i;
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- PyObject *res;
- Py_UNICODE *p;
- if (PyUnicodeEncodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
- res = PyUnicode_FromUnicode(NULL, end-start);
- if (res == NULL)
- return NULL;
- for (p = PyUnicode_AS_UNICODE(res), i = start;
- i<end; ++p, ++i)
- *p = '?';
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- return restuple;
+ PyObject *res;
+ Py_UNICODE *p;
+ if (PyUnicodeEncodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
+ res = PyUnicode_FromUnicode(NULL, end-start);
+ if (res == NULL)
+ return NULL;
+ for (p = PyUnicode_AS_UNICODE(res), i = start;
+ i<end; ++p, ++i)
+ *p = '?';
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ return restuple;
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
- Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
- if (PyUnicodeDecodeError_GetEnd(exc, &end))
- return NULL;
- return Py_BuildValue("(u#n)", &res, 1, end);
+ Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
+ if (PyUnicodeDecodeError_GetEnd(exc, &end))
+ return NULL;
+ return Py_BuildValue("(u#n)", &res, 1, end);
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
- PyObject *res;
- Py_UNICODE *p;
- if (PyUnicodeTranslateError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeTranslateError_GetEnd(exc, &end))
- return NULL;
- res = PyUnicode_FromUnicode(NULL, end-start);
- if (res == NULL)
- return NULL;
- for (p = PyUnicode_AS_UNICODE(res), i = start;
- i<end; ++p, ++i)
- *p = Py_UNICODE_REPLACEMENT_CHARACTER;
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- return restuple;
+ PyObject *res;
+ Py_UNICODE *p;
+ if (PyUnicodeTranslateError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeTranslateError_GetEnd(exc, &end))
+ return NULL;
+ res = PyUnicode_FromUnicode(NULL, end-start);
+ if (res == NULL)
+ return NULL;
+ for (p = PyUnicode_AS_UNICODE(res), i = start;
+ i<end; ++p, ++i)
+ *p = Py_UNICODE_REPLACEMENT_CHARACTER;
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ return restuple;
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
}
PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
{
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- PyObject *restuple;
- PyObject *object;
- Py_ssize_t start;
- Py_ssize_t end;
- PyObject *res;
- Py_UNICODE *p;
- Py_UNICODE *startp;
- Py_UNICODE *outp;
- int ressize;
- if (PyUnicodeEncodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))
- return NULL;
- startp = PyUnicode_AS_UNICODE(object);
- for (p = startp+start, ressize = 0; p < startp+end; ++p) {
- if (*p<10)
- ressize += 2+1+1;
- else if (*p<100)
- ressize += 2+2+1;
- else if (*p<1000)
- ressize += 2+3+1;
- else if (*p<10000)
- ressize += 2+4+1;
+ PyObject *restuple;
+ PyObject *object;
+ Py_ssize_t start;
+ Py_ssize_t end;
+ PyObject *res;
+ Py_UNICODE *p;
+ Py_UNICODE *startp;
+ Py_UNICODE *outp;
+ int ressize;
+ if (PyUnicodeEncodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
+ if (!(object = PyUnicodeEncodeError_GetObject(exc)))
+ return NULL;
+ startp = PyUnicode_AS_UNICODE(object);
+ for (p = startp+start, ressize = 0; p < startp+end; ++p) {
+ if (*p<10)
+ ressize += 2+1+1;
+ else if (*p<100)
+ ressize += 2+2+1;
+ else if (*p<1000)
+ ressize += 2+3+1;
+ else if (*p<10000)
+ ressize += 2+4+1;
#ifndef Py_UNICODE_WIDE
- else
- ressize += 2+5+1;
+ else
+ ressize += 2+5+1;
#else
- else if (*p<100000)
- ressize += 2+5+1;
- else if (*p<1000000)
- ressize += 2+6+1;
- else
- ressize += 2+7+1;
+ else if (*p<100000)
+ ressize += 2+5+1;
+ else if (*p<1000000)
+ ressize += 2+6+1;
+ else
+ ressize += 2+7+1;
#endif
- }
- /* allocate replacement */
- res = PyUnicode_FromUnicode(NULL, ressize);
- if (res == NULL) {
- Py_DECREF(object);
- return NULL;
- }
- /* generate replacement */
- for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
- p < startp+end; ++p) {
- Py_UNICODE c = *p;
- int digits;
- int base;
- *outp++ = '&';
- *outp++ = '#';
- if (*p<10) {
- digits = 1;
- base = 1;
- }
- else if (*p<100) {
- digits = 2;
- base = 10;
- }
- else if (*p<1000) {
- digits = 3;
- base = 100;
- }
- else if (*p<10000) {
- digits = 4;
- base = 1000;
- }
+ }
+ /* allocate replacement */
+ res = PyUnicode_FromUnicode(NULL, ressize);
+ if (res == NULL) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ /* generate replacement */
+ for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
+ p < startp+end; ++p) {
+ Py_UNICODE c = *p;
+ int digits;
+ int base;
+ *outp++ = '&';
+ *outp++ = '#';
+ if (*p<10) {
+ digits = 1;
+ base = 1;
+ }
+ else if (*p<100) {
+ digits = 2;
+ base = 10;
+ }
+ else if (*p<1000) {
+ digits = 3;
+ base = 100;
+ }
+ else if (*p<10000) {
+ digits = 4;
+ base = 1000;
+ }
#ifndef Py_UNICODE_WIDE
- else {
- digits = 5;
- base = 10000;
- }
+ else {
+ digits = 5;
+ base = 10000;
+ }
#else
- else if (*p<100000) {
- digits = 5;
- base = 10000;
- }
- else if (*p<1000000) {
- digits = 6;
- base = 100000;
- }
- else {
- digits = 7;
- base = 1000000;
- }
+ else if (*p<100000) {
+ digits = 5;
+ base = 10000;
+ }
+ else if (*p<1000000) {
+ digits = 6;
+ base = 100000;
+ }
+ else {
+ digits = 7;
+ base = 1000000;
+ }
#endif
- while (digits-->0) {
- *outp++ = '0' + c/base;
- c %= base;
- base /= 10;
- }
- *outp++ = ';';
- }
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- Py_DECREF(object);
- return restuple;
+ while (digits-->0) {
+ *outp++ = '0' + c/base;
+ c %= base;
+ base /= 10;
+ }
+ *outp++ = ';';
+ }
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return restuple;
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
}
@@ -679,72 +679,72 @@ static Py_UNICODE hexdigits[] = {
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
{
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- PyObject *restuple;
- PyObject *object;
- Py_ssize_t start;
- Py_ssize_t end;
- PyObject *res;
- Py_UNICODE *p;
- Py_UNICODE *startp;
- Py_UNICODE *outp;
- int ressize;
- if (PyUnicodeEncodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))
- return NULL;
- startp = PyUnicode_AS_UNICODE(object);
- for (p = startp+start, ressize = 0; p < startp+end; ++p) {
+ PyObject *restuple;
+ PyObject *object;
+ Py_ssize_t start;
+ Py_ssize_t end;
+ PyObject *res;
+ Py_UNICODE *p;
+ Py_UNICODE *startp;
+ Py_UNICODE *outp;
+ int ressize;
+ if (PyUnicodeEncodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
+ if (!(object = PyUnicodeEncodeError_GetObject(exc)))
+ return NULL;
+ startp = PyUnicode_AS_UNICODE(object);
+ for (p = startp+start, ressize = 0; p < startp+end; ++p) {
#ifdef Py_UNICODE_WIDE
- if (*p >= 0x00010000)
- ressize += 1+1+8;
- else
+ if (*p >= 0x00010000)
+ ressize += 1+1+8;
+ else
#endif
- if (*p >= 0x100) {
- ressize += 1+1+4;
- }
- else
- ressize += 1+1+2;
- }
- res = PyUnicode_FromUnicode(NULL, ressize);
- if (res==NULL)
- return NULL;
- for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
- p < startp+end; ++p) {
- Py_UNICODE c = *p;
- *outp++ = '\\';
+ if (*p >= 0x100) {
+ ressize += 1+1+4;
+ }
+ else
+ ressize += 1+1+2;
+ }
+ res = PyUnicode_FromUnicode(NULL, ressize);
+ if (res==NULL)
+ return NULL;
+ for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
+ p < startp+end; ++p) {
+ Py_UNICODE c = *p;
+ *outp++ = '\\';
#ifdef Py_UNICODE_WIDE
- if (c >= 0x00010000) {
- *outp++ = 'U';
- *outp++ = hexdigits[(c>>28)&0xf];
- *outp++ = hexdigits[(c>>24)&0xf];
- *outp++ = hexdigits[(c>>20)&0xf];
- *outp++ = hexdigits[(c>>16)&0xf];
- *outp++ = hexdigits[(c>>12)&0xf];
- *outp++ = hexdigits[(c>>8)&0xf];
- }
- else
+ if (c >= 0x00010000) {
+ *outp++ = 'U';
+ *outp++ = hexdigits[(c>>28)&0xf];
+ *outp++ = hexdigits[(c>>24)&0xf];
+ *outp++ = hexdigits[(c>>20)&0xf];
+ *outp++ = hexdigits[(c>>16)&0xf];
+ *outp++ = hexdigits[(c>>12)&0xf];
+ *outp++ = hexdigits[(c>>8)&0xf];
+ }
+ else
#endif
- if (c >= 0x100) {
- *outp++ = 'u';
- *outp++ = hexdigits[(c>>12)&0xf];
- *outp++ = hexdigits[(c>>8)&0xf];
- }
- else
- *outp++ = 'x';
- *outp++ = hexdigits[(c>>4)&0xf];
- *outp++ = hexdigits[c&0xf];
- }
-
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- Py_DECREF(object);
- return restuple;
+ if (c >= 0x100) {
+ *outp++ = 'u';
+ *outp++ = hexdigits[(c>>12)&0xf];
+ *outp++ = hexdigits[(c>>8)&0xf];
+ }
+ else
+ *outp++ = 'x';
+ *outp++ = hexdigits[(c>>4)&0xf];
+ *outp++ = hexdigits[c&0xf];
+ }
+
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return restuple;
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
}
@@ -759,73 +759,73 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
Py_ssize_t end;
PyObject *res;
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- Py_UNICODE *p;
- Py_UNICODE *startp;
- char *outp;
- if (PyUnicodeEncodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))
- return NULL;
- startp = PyUnicode_AS_UNICODE(object);
- res = PyBytes_FromStringAndSize(NULL, 3*(end-start));
- if (!res) {
- Py_DECREF(object);
- return NULL;
- }
- outp = PyBytes_AsString(res);
- for (p = startp+start; p < startp+end; p++) {
- Py_UNICODE ch = *p;
- if (ch < 0xd800 || ch > 0xdfff) {
- /* Not a surrogate, fail with original exception */
- PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
- Py_DECREF(res);
- Py_DECREF(object);
- return NULL;
- }
- *outp++ = (char)(0xe0 | (ch >> 12));
- *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f));
- *outp++ = (char)(0x80 | (ch & 0x3f));
- }
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- Py_DECREF(object);
- return restuple;
+ Py_UNICODE *p;
+ Py_UNICODE *startp;
+ char *outp;
+ if (PyUnicodeEncodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
+ if (!(object = PyUnicodeEncodeError_GetObject(exc)))
+ return NULL;
+ startp = PyUnicode_AS_UNICODE(object);
+ res = PyBytes_FromStringAndSize(NULL, 3*(end-start));
+ if (!res) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ outp = PyBytes_AsString(res);
+ for (p = startp+start; p < startp+end; p++) {
+ Py_UNICODE ch = *p;
+ if (ch < 0xd800 || ch > 0xdfff) {
+ /* Not a surrogate, fail with original exception */
+ PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return NULL;
+ }
+ *outp++ = (char)(0xe0 | (ch >> 12));
+ *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f));
+ *outp++ = (char)(0x80 | (ch & 0x3f));
+ }
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return restuple;
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
- unsigned char *p;
- Py_UNICODE ch = 0;
- if (PyUnicodeDecodeError_GetStart(exc, &start))
- return NULL;
- if (!(object = PyUnicodeDecodeError_GetObject(exc)))
- return NULL;
- if (!(p = (unsigned char*)PyBytes_AsString(object))) {
- Py_DECREF(object);
- return NULL;
- }
- /* Try decoding a single surrogate character. If
- there are more, let the codec call us again. */
- p += start;
- if ((p[0] & 0xf0) == 0xe0 ||
- (p[1] & 0xc0) == 0x80 ||
- (p[2] & 0xc0) == 0x80) {
- /* it's a three-byte code */
- ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f);
- if (ch < 0xd800 || ch > 0xdfff)
- /* it's not a surrogate - fail */
- ch = 0;
- }
- Py_DECREF(object);
- if (ch == 0) {
- PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
- return NULL;
- }
- return Py_BuildValue("(u#n)", &ch, 1, start+3);
+ unsigned char *p;
+ Py_UNICODE ch = 0;
+ if (PyUnicodeDecodeError_GetStart(exc, &start))
+ return NULL;
+ if (!(object = PyUnicodeDecodeError_GetObject(exc)))
+ return NULL;
+ if (!(p = (unsigned char*)PyBytes_AsString(object))) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ /* Try decoding a single surrogate character. If
+ there are more, let the codec call us again. */
+ p += start;
+ if ((p[0] & 0xf0) == 0xe0 ||
+ (p[1] & 0xc0) == 0x80 ||
+ (p[2] & 0xc0) == 0x80) {
+ /* it's a three-byte code */
+ ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f);
+ if (ch < 0xd800 || ch > 0xdfff)
+ /* it's not a surrogate - fail */
+ ch = 0;
+ }
+ Py_DECREF(object);
+ if (ch == 0) {
+ PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
+ return NULL;
+ }
+ return Py_BuildValue("(u#n)", &ch, 1, start+3);
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
}
@@ -838,74 +838,74 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
Py_ssize_t end;
PyObject *res;
if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
- Py_UNICODE *p;
- Py_UNICODE *startp;
- char *outp;
- if (PyUnicodeEncodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeEncodeError_GetEnd(exc, &end))
- return NULL;
- if (!(object = PyUnicodeEncodeError_GetObject(exc)))
- return NULL;
- startp = PyUnicode_AS_UNICODE(object);
- res = PyBytes_FromStringAndSize(NULL, end-start);
- if (!res) {
- Py_DECREF(object);
- return NULL;
- }
- outp = PyBytes_AsString(res);
- for (p = startp+start; p < startp+end; p++) {
- Py_UNICODE ch = *p;
- if (ch < 0xdc80 || ch > 0xdcff) {
- /* Not a UTF-8b surrogate, fail with original exception */
- PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
- Py_DECREF(res);
- Py_DECREF(object);
- return NULL;
- }
- *outp++ = ch - 0xdc00;
- }
- restuple = Py_BuildValue("(On)", res, end);
- Py_DECREF(res);
- Py_DECREF(object);
- return restuple;
+ Py_UNICODE *p;
+ Py_UNICODE *startp;
+ char *outp;
+ if (PyUnicodeEncodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeEncodeError_GetEnd(exc, &end))
+ return NULL;
+ if (!(object = PyUnicodeEncodeError_GetObject(exc)))
+ return NULL;
+ startp = PyUnicode_AS_UNICODE(object);
+ res = PyBytes_FromStringAndSize(NULL, end-start);
+ if (!res) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ outp = PyBytes_AsString(res);
+ for (p = startp+start; p < startp+end; p++) {
+ Py_UNICODE ch = *p;
+ if (ch < 0xdc80 || ch > 0xdcff) {
+ /* Not a UTF-8b surrogate, fail with original exception */
+ PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return NULL;
+ }
+ *outp++ = ch - 0xdc00;
+ }
+ restuple = Py_BuildValue("(On)", res, end);
+ Py_DECREF(res);
+ Py_DECREF(object);
+ return restuple;
}
else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
- unsigned char *p;
- Py_UNICODE ch[4]; /* decode up to 4 bad bytes. */
- int consumed = 0;
- if (PyUnicodeDecodeError_GetStart(exc, &start))
- return NULL;
- if (PyUnicodeDecodeError_GetEnd(exc, &end))
- return NULL;
- if (!(object = PyUnicodeDecodeError_GetObject(exc)))
- return NULL;
- if (!(p = (unsigned char*)PyBytes_AsString(object))) {
- Py_DECREF(object);
- return NULL;
- }
- while (consumed < 4 && consumed < end-start) {
- /* Refuse to escape ASCII bytes. */
- if (p[start+consumed] < 128)
- break;
- ch[consumed] = 0xdc00 + p[start+consumed];
- consumed++;
- }
- Py_DECREF(object);
- if (!consumed) {
- /* codec complained about ASCII byte. */
- PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
- return NULL;
- }
- return Py_BuildValue("(u#n)", ch, consumed, start+consumed);
+ unsigned char *p;
+ Py_UNICODE ch[4]; /* decode up to 4 bad bytes. */
+ int consumed = 0;
+ if (PyUnicodeDecodeError_GetStart(exc, &start))
+ return NULL;
+ if (PyUnicodeDecodeError_GetEnd(exc, &end))
+ return NULL;
+ if (!(object = PyUnicodeDecodeError_GetObject(exc)))
+ return NULL;
+ if (!(p = (unsigned char*)PyBytes_AsString(object))) {
+ Py_DECREF(object);
+ return NULL;
+ }
+ while (consumed < 4 && consumed < end-start) {
+ /* Refuse to escape ASCII bytes. */
+ if (p[start+consumed] < 128)
+ break;
+ ch[consumed] = 0xdc00 + p[start+consumed];
+ consumed++;
+ }
+ Py_DECREF(object);
+ if (!consumed) {
+ /* codec complained about ASCII byte. */
+ PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
+ return NULL;
+ }
+ return Py_BuildValue("(u#n)", ch, consumed, start+consumed);
}
else {
- wrong_exception_type(exc);
- return NULL;
+ wrong_exception_type(exc);
+ return NULL;
}
}
-
+
static PyObject *strict_errors(PyObject *self, PyObject *exc)
{
return PyCodec_StrictErrors(exc);
@@ -948,78 +948,78 @@ static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
static int _PyCodecRegistry_Init(void)
{
static struct {
- char *name;
- PyMethodDef def;
+ char *name;
+ PyMethodDef def;
} methods[] =
{
- {
- "strict",
- {
- "strict_errors",
- strict_errors,
- METH_O,
- PyDoc_STR("Implements the 'strict' error handling, which "
- "raises a UnicodeError on coding errors.")
- }
- },
- {
- "ignore",
- {
- "ignore_errors",
- ignore_errors,
- METH_O,
- PyDoc_STR("Implements the 'ignore' error handling, which "
- "ignores malformed data and continues.")
- }
- },
- {
- "replace",
- {
- "replace_errors",
- replace_errors,
- METH_O,
- PyDoc_STR("Implements the 'replace' error handling, which "
- "replaces malformed data with a replacement marker.")
- }
- },
- {
- "xmlcharrefreplace",
- {
- "xmlcharrefreplace_errors",
- xmlcharrefreplace_errors,
- METH_O,
- PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
- "which replaces an unencodable character with the "
- "appropriate XML character reference.")
- }
- },
- {
- "backslashreplace",
- {
- "backslashreplace_errors",
- backslashreplace_errors,
- METH_O,
- PyDoc_STR("Implements the 'backslashreplace' error handling, "
- "which replaces an unencodable character with a "
- "backslashed escape sequence.")
- }
- },
- {
- "surrogatepass",
- {
- "surrogatepass",
- surrogatepass_errors,
- METH_O
- }
- },
- {
- "surrogateescape",
- {
- "surrogateescape",
- surrogateescape_errors,
- METH_O
- }
- }
+ {
+ "strict",
+ {
+ "strict_errors",
+ strict_errors,
+ METH_O,
+ PyDoc_STR("Implements the 'strict' error handling, which "
+ "raises a UnicodeError on coding errors.")
+ }
+ },
+ {
+ "ignore",
+ {
+ "ignore_errors",
+ ignore_errors,
+ METH_O,
+ PyDoc_STR("Implements the 'ignore' error handling, which "
+ "ignores malformed data and continues.")
+ }
+ },
+ {
+ "replace",
+ {
+ "replace_errors",
+ replace_errors,
+ METH_O,
+ PyDoc_STR("Implements the 'replace' error handling, which "
+ "replaces malformed data with a replacement marker.")
+ }
+ },
+ {
+ "xmlcharrefreplace",
+ {
+ "xmlcharrefreplace_errors",
+ xmlcharrefreplace_errors,
+ METH_O,
+ PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
+ "which replaces an unencodable character with the "
+ "appropriate XML character reference.")
+ }
+ },
+ {
+ "backslashreplace",
+ {
+ "backslashreplace_errors",
+ backslashreplace_errors,
+ METH_O,
+ PyDoc_STR("Implements the 'backslashreplace' error handling, "
+ "which replaces an unencodable character with a "
+ "backslashed escape sequence.")
+ }
+ },
+ {
+ "surrogatepass",
+ {
+ "surrogatepass",
+ surrogatepass_errors,
+ METH_O
+ }
+ },
+ {
+ "surrogateescape",
+ {
+ "surrogateescape",
+ surrogateescape_errors,
+ METH_O
+ }
+ }
};
PyInterpreterState *interp = PyThreadState_GET()->interp;
@@ -1027,42 +1027,42 @@ static int _PyCodecRegistry_Init(void)
unsigned i;
if (interp->codec_search_path != NULL)
- return 0;
+ return 0;
interp->codec_search_path = PyList_New(0);
interp->codec_search_cache = PyDict_New();
interp->codec_error_registry = PyDict_New();
if (interp->codec_error_registry) {
- for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
- PyObject *func = PyCFunction_New(&methods[i].def, NULL);
- int res;
- if (!func)
- Py_FatalError("can't initialize codec error registry");
- res = PyCodec_RegisterError(methods[i].name, func);
- Py_DECREF(func);
- if (res)
- Py_FatalError("can't initialize codec error registry");
- }
+ for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
+ PyObject *func = PyCFunction_New(&methods[i].def, NULL);
+ int res;
+ if (!func)
+ Py_FatalError("can't initialize codec error registry");
+ res = PyCodec_RegisterError(methods[i].name, func);
+ Py_DECREF(func);
+ if (res)
+ Py_FatalError("can't initialize codec error registry");
+ }
}
if (interp->codec_search_path == NULL ||
- interp->codec_search_cache == NULL ||
- interp->codec_error_registry == NULL)
- Py_FatalError("can't initialize codec registry");
+ interp->codec_search_cache == NULL ||
+ interp->codec_error_registry == NULL)
+ Py_FatalError("can't initialize codec registry");
mod = PyImport_ImportModuleNoBlock("encodings");
if (mod == NULL) {
- if (PyErr_ExceptionMatches(PyExc_ImportError)) {
- /* Ignore ImportErrors... this is done so that
- distributions can disable the encodings package. Note
- that other errors are not masked, e.g. SystemErrors
- raised to inform the user of an error in the Python
- configuration are still reported back to the user. */
- PyErr_Clear();
- return 0;
- }
- return -1;
+ if (PyErr_ExceptionMatches(PyExc_ImportError)) {
+ /* Ignore ImportErrors... this is done so that
+ distributions can disable the encodings package. Note
+ that other errors are not masked, e.g. SystemErrors
+ raised to inform the user of an error in the Python
+ configuration are still reported back to the user. */
+ PyErr_Clear();
+ return 0;
+ }
+ return -1;
}
Py_DECREF(mod);
interp->codecs_initialized = 1;
diff --git a/Python/compile.c b/Python/compile.c
index ab54208..074b131 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -5,10 +5,10 @@
* PyCodeObject. The compiler makes several passes to build the code
* object:
* 1. Checks for future statements. See future.c
- * 2. Builds a symbol table. See symtable.c.
+ * 2. Builds a symbol table. See symtable.c.
* 3. Generate code for basic blocks. See compiler_mod() in this file.
* 4. Assemble the basic blocks into final code. See assemble() in
- * this file.
+ * this file.
* 5. Optimize the byte code (peephole optimizations). See peephole.c
*
* Note that compiler_mod() suggests module, but the module ast type
@@ -45,37 +45,37 @@ int Py_OptimizeFlag = 0;
#define COMP_DICTCOMP 3
struct instr {
- unsigned i_jabs : 1;
- unsigned i_jrel : 1;
- unsigned i_hasarg : 1;
- unsigned char i_opcode;
- int i_oparg;
- struct basicblock_ *i_target; /* target block (if jump instruction) */
- int i_lineno;
+ unsigned i_jabs : 1;
+ unsigned i_jrel : 1;
+ unsigned i_hasarg : 1;
+ unsigned char i_opcode;
+ int i_oparg;
+ struct basicblock_ *i_target; /* target block (if jump instruction) */
+ int i_lineno;
};
typedef struct basicblock_ {
/* Each basicblock in a compilation unit is linked via b_list in the
reverse order that the block are allocated. b_list points to the next
block, not to be confused with b_next, which is next by control flow. */
- struct basicblock_ *b_list;
- /* number of instructions used */
- int b_iused;
- /* length of instruction array (b_instr) */
- int b_ialloc;
- /* pointer to an array of instructions, initially NULL */
- struct instr *b_instr;
- /* If b_next is non-NULL, it is a pointer to the next
- block reached by normal control flow. */
- struct basicblock_ *b_next;
- /* b_seen is used to perform a DFS of basicblocks. */
- unsigned b_seen : 1;
- /* b_return is true if a RETURN_VALUE opcode is inserted. */
- unsigned b_return : 1;
- /* depth of stack upon entry of block, computed by stackdepth() */
- int b_startdepth;
- /* instruction offset for block, computed by assemble_jump_offsets() */
- int b_offset;
+ struct basicblock_ *b_list;
+ /* number of instructions used */
+ int b_iused;
+ /* length of instruction array (b_instr) */
+ int b_ialloc;
+ /* pointer to an array of instructions, initially NULL */
+ struct instr *b_instr;
+ /* If b_next is non-NULL, it is a pointer to the next
+ block reached by normal control flow. */
+ struct basicblock_ *b_next;
+ /* b_seen is used to perform a DFS of basicblocks. */
+ unsigned b_seen : 1;
+ /* b_return is true if a RETURN_VALUE opcode is inserted. */
+ unsigned b_return : 1;
+ /* depth of stack upon entry of block, computed by stackdepth() */
+ int b_startdepth;
+ /* instruction offset for block, computed by assemble_jump_offsets() */
+ int b_offset;
} basicblock;
/* fblockinfo tracks the current frame block.
@@ -88,66 +88,66 @@ compiler IR.
enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
struct fblockinfo {
- enum fblocktype fb_type;
- basicblock *fb_block;
+ enum fblocktype fb_type;
+ basicblock *fb_block;
};
/* The following items change on entry and exit of code blocks.
They must be saved and restored when returning to a block.
*/
struct compiler_unit {
- PySTEntryObject *u_ste;
-
- PyObject *u_name;
- /* The following fields are dicts that map objects to
- the index of them in co_XXX. The index is used as
- the argument for opcodes that refer to those collections.
- */
- PyObject *u_consts; /* all constants */
- PyObject *u_names; /* all names */
- PyObject *u_varnames; /* local variables */
- PyObject *u_cellvars; /* cell variables */
- PyObject *u_freevars; /* free variables */
-
- PyObject *u_private; /* for private name mangling */
-
- int u_argcount; /* number of arguments for block */
- int u_kwonlyargcount; /* number of keyword only arguments for block */
- /* Pointer to the most recently allocated block. By following b_list
- members, you can reach all early allocated blocks. */
- basicblock *u_blocks;
- basicblock *u_curblock; /* pointer to current block */
- int u_tmpname; /* temporary variables for list comps */
-
- int u_nfblocks;
- struct fblockinfo u_fblock[CO_MAXBLOCKS];
-
- int u_firstlineno; /* the first lineno of the block */
- int u_lineno; /* the lineno for the current stmt */
- int u_lineno_set; /* boolean to indicate whether instr
- has been generated with current lineno */
+ PySTEntryObject *u_ste;
+
+ PyObject *u_name;
+ /* The following fields are dicts that map objects to
+ the index of them in co_XXX. The index is used as
+ the argument for opcodes that refer to those collections.
+ */
+ PyObject *u_consts; /* all constants */
+ PyObject *u_names; /* all names */
+ PyObject *u_varnames; /* local variables */
+ PyObject *u_cellvars; /* cell variables */
+ PyObject *u_freevars; /* free variables */
+
+ PyObject *u_private; /* for private name mangling */
+
+ int u_argcount; /* number of arguments for block */
+ int u_kwonlyargcount; /* number of keyword only arguments for block */
+ /* Pointer to the most recently allocated block. By following b_list
+ members, you can reach all early allocated blocks. */
+ basicblock *u_blocks;
+ basicblock *u_curblock; /* pointer to current block */
+ int u_tmpname; /* temporary variables for list comps */
+
+ int u_nfblocks;
+ struct fblockinfo u_fblock[CO_MAXBLOCKS];
+
+ int u_firstlineno; /* the first lineno of the block */
+ int u_lineno; /* the lineno for the current stmt */
+ int u_lineno_set; /* boolean to indicate whether instr
+ has been generated with current lineno */
};
-/* This struct captures the global state of a compilation.
+/* This struct captures the global state of a compilation.
The u pointer points to the current compilation unit, while units
-for enclosing blocks are stored in c_stack. The u and c_stack are
+for enclosing blocks are stored in c_stack. The u and c_stack are
managed by compiler_enter_scope() and compiler_exit_scope().
*/
struct compiler {
- const char *c_filename;
- struct symtable *c_st;
- PyFutureFeatures *c_future; /* pointer to module's __future__ */
- PyCompilerFlags *c_flags;
-
- int c_interactive; /* true if in interactive mode */
- int c_nestlevel;
-
- struct compiler_unit *u; /* compiler state for current block */
- PyObject *c_stack; /* Python list holding compiler_unit ptrs */
- char *c_encoding; /* source encoding (a borrowed reference) */
- PyArena *c_arena; /* pointer to memory allocation arena */
+ const char *c_filename;
+ struct symtable *c_st;
+ PyFutureFeatures *c_future; /* pointer to module's __future__ */
+ PyCompilerFlags *c_flags;
+
+ int c_interactive; /* true if in interactive mode */
+ int c_nestlevel;
+
+ struct compiler_unit *u; /* compiler state for current block */
+ PyObject *c_stack; /* Python list holding compiler_unit ptrs */
+ char *c_encoding; /* source encoding (a borrowed reference) */
+ PyArena *c_arena; /* pointer to memory allocation arena */
};
static int compiler_enter_scope(struct compiler *, identifier, void *, int);
@@ -168,12 +168,12 @@ static int compiler_visit_keyword(struct compiler *, keyword_ty);
static int compiler_visit_expr(struct compiler *, expr_ty);
static int compiler_augassign(struct compiler *, stmt_ty);
static int compiler_visit_slice(struct compiler *, slice_ty,
- expr_context_ty);
+ expr_context_ty);
static int compiler_push_fblock(struct compiler *, enum fblocktype,
- basicblock *);
+ basicblock *);
static void compiler_pop_fblock(struct compiler *, enum fblocktype,
- basicblock *);
+ basicblock *);
/* Returns true if there is a loop on the fblock stack. */
static int compiler_in_loop(struct compiler *);
@@ -182,10 +182,10 @@ static int expr_constant(expr_ty e);
static int compiler_with(struct compiler *, stmt_ty);
static int compiler_call_helper(struct compiler *c, int n,
- asdl_seq *args,
- asdl_seq *keywords,
- expr_ty starargs,
- expr_ty kwargs);
+ asdl_seq *args,
+ asdl_seq *keywords,
+ expr_ty starargs,
+ expr_ty kwargs);
static PyCodeObject *assemble(struct compiler *, int addNone);
static PyObject *__doc__;
@@ -195,175 +195,175 @@ static PyObject *__doc__;
PyObject *
_Py_Mangle(PyObject *privateobj, PyObject *ident)
{
- /* Name mangling: __private becomes _classname__private.
- This is independent from how the name is used. */
- const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
- Py_UNICODE *buffer;
- size_t nlen, plen;
- if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
- name == NULL || name[0] != '_' || name[1] != '_') {
- Py_INCREF(ident);
- return ident;
- }
- p = PyUnicode_AS_UNICODE(privateobj);
- nlen = Py_UNICODE_strlen(name);
- /* Don't mangle __id__ or names with dots.
-
- The only time a name with a dot can occur is when
- we are compiling an import statement that has a
- package name.
-
- TODO(jhylton): Decide whether we want to support
- mangling of the module name, e.g. __M.X.
- */
- if ((name[nlen-1] == '_' && name[nlen-2] == '_')
- || Py_UNICODE_strchr(name, '.')) {
- Py_INCREF(ident);
- return ident; /* Don't mangle __whatever__ */
- }
- /* Strip leading underscores from class name */
- while (*p == '_')
- p++;
- if (*p == 0) {
- Py_INCREF(ident);
- return ident; /* Don't mangle if class is just underscores */
- }
- plen = Py_UNICODE_strlen(p);
-
- assert(1 <= PY_SSIZE_T_MAX - nlen);
- assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
-
- ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
- if (!ident)
- return 0;
- /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
- buffer = PyUnicode_AS_UNICODE(ident);
- buffer[0] = '_';
- Py_UNICODE_strncpy(buffer+1, p, plen);
- Py_UNICODE_strcpy(buffer+1+plen, name);
- return ident;
+ /* Name mangling: __private becomes _classname__private.
+ This is independent from how the name is used. */
+ const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
+ Py_UNICODE *buffer;
+ size_t nlen, plen;
+ if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
+ name == NULL || name[0] != '_' || name[1] != '_') {
+ Py_INCREF(ident);
+ return ident;
+ }
+ p = PyUnicode_AS_UNICODE(privateobj);
+ nlen = Py_UNICODE_strlen(name);
+ /* Don't mangle __id__ or names with dots.
+
+ The only time a name with a dot can occur is when
+ we are compiling an import statement that has a
+ package name.
+
+ TODO(jhylton): Decide whether we want to support
+ mangling of the module name, e.g. __M.X.
+ */
+ if ((name[nlen-1] == '_' && name[nlen-2] == '_')
+ || Py_UNICODE_strchr(name, '.')) {
+ Py_INCREF(ident);
+ return ident; /* Don't mangle __whatever__ */
+ }
+ /* Strip leading underscores from class name */
+ while (*p == '_')
+ p++;
+ if (*p == 0) {
+ Py_INCREF(ident);
+ return ident; /* Don't mangle if class is just underscores */
+ }
+ plen = Py_UNICODE_strlen(p);
+
+ assert(1 <= PY_SSIZE_T_MAX - nlen);
+ assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
+
+ ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
+ if (!ident)
+ return 0;
+ /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
+ buffer = PyUnicode_AS_UNICODE(ident);
+ buffer[0] = '_';
+ Py_UNICODE_strncpy(buffer+1, p, plen);
+ Py_UNICODE_strcpy(buffer+1+plen, name);
+ return ident;
}
static int
compiler_init(struct compiler *c)
{
- memset(c, 0, sizeof(struct compiler));
+ memset(c, 0, sizeof(struct compiler));
- c->c_stack = PyList_New(0);
- if (!c->c_stack)
- return 0;
+ c->c_stack = PyList_New(0);
+ if (!c->c_stack)
+ return 0;
- return 1;
+ return 1;
}
PyCodeObject *
PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
- PyArena *arena)
+ PyArena *arena)
{
- struct compiler c;
- PyCodeObject *co = NULL;
- PyCompilerFlags local_flags;
- int merged;
-
- if (!__doc__) {
- __doc__ = PyUnicode_InternFromString("__doc__");
- if (!__doc__)
- return NULL;
- }
-
- if (!compiler_init(&c))
- return NULL;
- c.c_filename = filename;
- c.c_arena = arena;
- c.c_future = PyFuture_FromAST(mod, filename);
- if (c.c_future == NULL)
- goto finally;
- if (!flags) {
- local_flags.cf_flags = 0;
- flags = &local_flags;
- }
- merged = c.c_future->ff_features | flags->cf_flags;
- c.c_future->ff_features = merged;
- flags->cf_flags = merged;
- c.c_flags = flags;
- c.c_nestlevel = 0;
-
- c.c_st = PySymtable_Build(mod, filename, c.c_future);
- if (c.c_st == NULL) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_SystemError, "no symtable");
- goto finally;
- }
-
- /* XXX initialize to NULL for now, need to handle */
- c.c_encoding = NULL;
-
- co = compiler_mod(&c, mod);
+ struct compiler c;
+ PyCodeObject *co = NULL;
+ PyCompilerFlags local_flags;
+ int merged;
+
+ if (!__doc__) {
+ __doc__ = PyUnicode_InternFromString("__doc__");
+ if (!__doc__)
+ return NULL;
+ }
+
+ if (!compiler_init(&c))
+ return NULL;
+ c.c_filename = filename;
+ c.c_arena = arena;
+ c.c_future = PyFuture_FromAST(mod, filename);
+ if (c.c_future == NULL)
+ goto finally;
+ if (!flags) {
+ local_flags.cf_flags = 0;
+ flags = &local_flags;
+ }
+ merged = c.c_future->ff_features | flags->cf_flags;
+ c.c_future->ff_features = merged;
+ flags->cf_flags = merged;
+ c.c_flags = flags;
+ c.c_nestlevel = 0;
+
+ c.c_st = PySymtable_Build(mod, filename, c.c_future);
+ if (c.c_st == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError, "no symtable");
+ goto finally;
+ }
+
+ /* XXX initialize to NULL for now, need to handle */
+ c.c_encoding = NULL;
+
+ co = compiler_mod(&c, mod);
finally:
- compiler_free(&c);
- assert(co || PyErr_Occurred());
- return co;
+ compiler_free(&c);
+ assert(co || PyErr_Occurred());
+ return co;
}
PyCodeObject *
PyNode_Compile(struct _node *n, const char *filename)
{
- PyCodeObject *co = NULL;
- mod_ty mod;
- PyArena *arena = PyArena_New();
- if (!arena)
- return NULL;
- mod = PyAST_FromNode(n, NULL, filename, arena);
- if (mod)
- co = PyAST_Compile(mod, filename, NULL, arena);
- PyArena_Free(arena);
- return co;
+ PyCodeObject *co = NULL;
+ mod_ty mod;
+ PyArena *arena = PyArena_New();
+ if (!arena)
+ return NULL;
+ mod = PyAST_FromNode(n, NULL, filename, arena);
+ if (mod)
+ co = PyAST_Compile(mod, filename, NULL, arena);
+ PyArena_Free(arena);
+ return co;
}
static void
compiler_free(struct compiler *c)
{
- if (c->c_st)
- PySymtable_Free(c->c_st);
- if (c->c_future)
- PyObject_Free(c->c_future);
- Py_DECREF(c->c_stack);
+ if (c->c_st)
+ PySymtable_Free(c->c_st);
+ if (c->c_future)
+ PyObject_Free(c->c_future);
+ Py_DECREF(c->c_stack);
}
static PyObject *
list2dict(PyObject *list)
{
- Py_ssize_t i, n;
- PyObject *v, *k;
- PyObject *dict = PyDict_New();
- if (!dict) return NULL;
-
- n = PyList_Size(list);
- for (i = 0; i < n; i++) {
- v = PyLong_FromLong(i);
- if (!v) {
- Py_DECREF(dict);
- return NULL;
- }
- k = PyList_GET_ITEM(list, i);
- k = PyTuple_Pack(2, k, k->ob_type);
- if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
- Py_XDECREF(k);
- Py_DECREF(v);
- Py_DECREF(dict);
- return NULL;
- }
- Py_DECREF(k);
- Py_DECREF(v);
- }
- return dict;
+ Py_ssize_t i, n;
+ PyObject *v, *k;
+ PyObject *dict = PyDict_New();
+ if (!dict) return NULL;
+
+ n = PyList_Size(list);
+ for (i = 0; i < n; i++) {
+ v = PyLong_FromLong(i);
+ if (!v) {
+ Py_DECREF(dict);
+ return NULL;
+ }
+ k = PyList_GET_ITEM(list, i);
+ k = PyTuple_Pack(2, k, k->ob_type);
+ if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
+ Py_XDECREF(k);
+ Py_DECREF(v);
+ Py_DECREF(dict);
+ return NULL;
+ }
+ Py_DECREF(k);
+ Py_DECREF(v);
+ }
+ return dict;
}
/* Return new dict containing names from src that match scope(s).
src is a symbol table dictionary. If the scope of a name matches
-either scope_type or flag is set, insert it into the new dict. The
+either scope_type or flag is set, insert it into the new dict. The
values are integers, starting at offset and increasing by one for
each key.
*/
@@ -371,194 +371,194 @@ each key.
static PyObject *
dictbytype(PyObject *src, int scope_type, int flag, int offset)
{
- Py_ssize_t pos = 0, i = offset, scope;
- PyObject *k, *v, *dest = PyDict_New();
-
- assert(offset >= 0);
- if (dest == NULL)
- return NULL;
-
- while (PyDict_Next(src, &pos, &k, &v)) {
- /* XXX this should probably be a macro in symtable.h */
- long vi;
- assert(PyLong_Check(v));
- vi = PyLong_AS_LONG(v);
- scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
-
- if (scope == scope_type || vi & flag) {
- PyObject *tuple, *item = PyLong_FromLong(i);
- if (item == NULL) {
- Py_DECREF(dest);
- return NULL;
- }
- i++;
- tuple = PyTuple_Pack(2, k, k->ob_type);
- if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
- Py_DECREF(item);
- Py_DECREF(dest);
- Py_XDECREF(tuple);
- return NULL;
- }
- Py_DECREF(item);
- Py_DECREF(tuple);
- }
- }
- return dest;
+ Py_ssize_t pos = 0, i = offset, scope;
+ PyObject *k, *v, *dest = PyDict_New();
+
+ assert(offset >= 0);
+ if (dest == NULL)
+ return NULL;
+
+ while (PyDict_Next(src, &pos, &k, &v)) {
+ /* XXX this should probably be a macro in symtable.h */
+ long vi;
+ assert(PyLong_Check(v));
+ vi = PyLong_AS_LONG(v);
+ scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
+
+ if (scope == scope_type || vi & flag) {
+ PyObject *tuple, *item = PyLong_FromLong(i);
+ if (item == NULL) {
+ Py_DECREF(dest);
+ return NULL;
+ }
+ i++;
+ tuple = PyTuple_Pack(2, k, k->ob_type);
+ if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
+ Py_DECREF(item);
+ Py_DECREF(dest);
+ Py_XDECREF(tuple);
+ return NULL;
+ }
+ Py_DECREF(item);
+ Py_DECREF(tuple);
+ }
+ }
+ return dest;
}
static void
compiler_unit_check(struct compiler_unit *u)
{
- basicblock *block;
- for (block = u->u_blocks; block != NULL; block = block->b_list) {
- assert((void *)block != (void *)0xcbcbcbcb);
- assert((void *)block != (void *)0xfbfbfbfb);
- assert((void *)block != (void *)0xdbdbdbdb);
- if (block->b_instr != NULL) {
- assert(block->b_ialloc > 0);
- assert(block->b_iused > 0);
- assert(block->b_ialloc >= block->b_iused);
- }
- else {
- assert (block->b_iused == 0);
- assert (block->b_ialloc == 0);
- }
- }
+ basicblock *block;
+ for (block = u->u_blocks; block != NULL; block = block->b_list) {
+ assert((void *)block != (void *)0xcbcbcbcb);
+ assert((void *)block != (void *)0xfbfbfbfb);
+ assert((void *)block != (void *)0xdbdbdbdb);
+ if (block->b_instr != NULL) {
+ assert(block->b_ialloc > 0);
+ assert(block->b_iused > 0);
+ assert(block->b_ialloc >= block->b_iused);
+ }
+ else {
+ assert (block->b_iused == 0);
+ assert (block->b_ialloc == 0);
+ }
+ }
}
static void
compiler_unit_free(struct compiler_unit *u)
{
- basicblock *b, *next;
-
- compiler_unit_check(u);
- b = u->u_blocks;
- while (b != NULL) {
- if (b->b_instr)
- PyObject_Free((void *)b->b_instr);
- next = b->b_list;
- PyObject_Free((void *)b);
- b = next;
- }
- Py_CLEAR(u->u_ste);
- Py_CLEAR(u->u_name);
- Py_CLEAR(u->u_consts);
- Py_CLEAR(u->u_names);
- Py_CLEAR(u->u_varnames);
- Py_CLEAR(u->u_freevars);
- Py_CLEAR(u->u_cellvars);
- Py_CLEAR(u->u_private);
- PyObject_Free(u);
+ basicblock *b, *next;
+
+ compiler_unit_check(u);
+ b = u->u_blocks;
+ while (b != NULL) {
+ if (b->b_instr)
+ PyObject_Free((void *)b->b_instr);
+ next = b->b_list;
+ PyObject_Free((void *)b);
+ b = next;
+ }
+ Py_CLEAR(u->u_ste);
+ Py_CLEAR(u->u_name);
+ Py_CLEAR(u->u_consts);
+ Py_CLEAR(u->u_names);
+ Py_CLEAR(u->u_varnames);
+ Py_CLEAR(u->u_freevars);
+ Py_CLEAR(u->u_cellvars);
+ Py_CLEAR(u->u_private);
+ PyObject_Free(u);
}
static int
compiler_enter_scope(struct compiler *c, identifier name, void *key,
- int lineno)
+ int lineno)
{
- struct compiler_unit *u;
-
- u = (struct compiler_unit *)PyObject_Malloc(sizeof(
- struct compiler_unit));
- if (!u) {
- PyErr_NoMemory();
- return 0;
- }
- memset(u, 0, sizeof(struct compiler_unit));
- u->u_argcount = 0;
- u->u_kwonlyargcount = 0;
- u->u_ste = PySymtable_Lookup(c->c_st, key);
- if (!u->u_ste) {
- compiler_unit_free(u);
- return 0;
- }
- Py_INCREF(name);
- u->u_name = name;
- u->u_varnames = list2dict(u->u_ste->ste_varnames);
- u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
- if (!u->u_varnames || !u->u_cellvars) {
- compiler_unit_free(u);
- return 0;
- }
-
- u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
- PyDict_Size(u->u_cellvars));
- if (!u->u_freevars) {
- compiler_unit_free(u);
- return 0;
- }
-
- u->u_blocks = NULL;
- u->u_tmpname = 0;
- u->u_nfblocks = 0;
- u->u_firstlineno = lineno;
- u->u_lineno = 0;
- u->u_lineno_set = 0;
- u->u_consts = PyDict_New();
- if (!u->u_consts) {
- compiler_unit_free(u);
- return 0;
- }
- u->u_names = PyDict_New();
- if (!u->u_names) {
- compiler_unit_free(u);
- return 0;
- }
-
- u->u_private = NULL;
-
- /* Push the old compiler_unit on the stack. */
- if (c->u) {
- PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
- if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
- Py_XDECREF(capsule);
- compiler_unit_free(u);
- return 0;
- }
- Py_DECREF(capsule);
- u->u_private = c->u->u_private;
- Py_XINCREF(u->u_private);
- }
- c->u = u;
-
- c->c_nestlevel++;
- if (compiler_use_new_block(c) == NULL)
- return 0;
-
- return 1;
+ struct compiler_unit *u;
+
+ u = (struct compiler_unit *)PyObject_Malloc(sizeof(
+ struct compiler_unit));
+ if (!u) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ memset(u, 0, sizeof(struct compiler_unit));
+ u->u_argcount = 0;
+ u->u_kwonlyargcount = 0;
+ u->u_ste = PySymtable_Lookup(c->c_st, key);
+ if (!u->u_ste) {
+ compiler_unit_free(u);
+ return 0;
+ }
+ Py_INCREF(name);
+ u->u_name = name;
+ u->u_varnames = list2dict(u->u_ste->ste_varnames);
+ u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
+ if (!u->u_varnames || !u->u_cellvars) {
+ compiler_unit_free(u);
+ return 0;
+ }
+
+ u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
+ PyDict_Size(u->u_cellvars));
+ if (!u->u_freevars) {
+ compiler_unit_free(u);
+ return 0;
+ }
+
+ u->u_blocks = NULL;
+ u->u_tmpname = 0;
+ u->u_nfblocks = 0;
+ u->u_firstlineno = lineno;
+ u->u_lineno = 0;
+ u->u_lineno_set = 0;
+ u->u_consts = PyDict_New();
+ if (!u->u_consts) {
+ compiler_unit_free(u);
+ return 0;
+ }
+ u->u_names = PyDict_New();
+ if (!u->u_names) {
+ compiler_unit_free(u);
+ return 0;
+ }
+
+ u->u_private = NULL;
+
+ /* Push the old compiler_unit on the stack. */
+ if (c->u) {
+ PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
+ if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
+ Py_XDECREF(capsule);
+ compiler_unit_free(u);
+ return 0;
+ }
+ Py_DECREF(capsule);
+ u->u_private = c->u->u_private;
+ Py_XINCREF(u->u_private);
+ }
+ c->u = u;
+
+ c->c_nestlevel++;
+ if (compiler_use_new_block(c) == NULL)
+ return 0;
+
+ return 1;
}
static void
compiler_exit_scope(struct compiler *c)
{
- int n;
- PyObject *capsule;
-
- c->c_nestlevel--;
- compiler_unit_free(c->u);
- /* Restore c->u to the parent unit. */
- n = PyList_GET_SIZE(c->c_stack) - 1;
- if (n >= 0) {
- capsule = PyList_GET_ITEM(c->c_stack, n);
- c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
- assert(c->u);
- /* we are deleting from a list so this really shouldn't fail */
- if (PySequence_DelItem(c->c_stack, n) < 0)
- Py_FatalError("compiler_exit_scope()");
- compiler_unit_check(c->u);
- }
- else
- c->u = NULL;
+ int n;
+ PyObject *capsule;
+
+ c->c_nestlevel--;
+ compiler_unit_free(c->u);
+ /* Restore c->u to the parent unit. */
+ n = PyList_GET_SIZE(c->c_stack) - 1;
+ if (n >= 0) {
+ capsule = PyList_GET_ITEM(c->c_stack, n);
+ c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
+ assert(c->u);
+ /* we are deleting from a list so this really shouldn't fail */
+ if (PySequence_DelItem(c->c_stack, n) < 0)
+ Py_FatalError("compiler_exit_scope()");
+ compiler_unit_check(c->u);
+ }
+ else
+ c->u = NULL;
}
-/* Allocate a new "anonymous" local variable. Used by with statements. */
-
-static PyObject *
-compiler_new_tmpname(struct compiler *c)
-{
- char tmpname[256];
- PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
- return PyUnicode_FromString(tmpname);
+/* Allocate a new "anonymous" local variable. Used by with statements. */
+
+static PyObject *
+compiler_new_tmpname(struct compiler *c)
+{
+ char tmpname[256];
+ PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
+ return PyUnicode_FromString(tmpname);
}
/* Allocate a new block and return a pointer to it.
@@ -568,50 +568,50 @@ compiler_new_tmpname(struct compiler *c)
static basicblock *
compiler_new_block(struct compiler *c)
{
- basicblock *b;
- struct compiler_unit *u;
-
- u = c->u;
- b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
- if (b == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- memset((void *)b, 0, sizeof(basicblock));
- /* Extend the singly linked list of blocks with new block. */
- b->b_list = u->u_blocks;
- u->u_blocks = b;
- return b;
+ basicblock *b;
+ struct compiler_unit *u;
+
+ u = c->u;
+ b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
+ if (b == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset((void *)b, 0, sizeof(basicblock));
+ /* Extend the singly linked list of blocks with new block. */
+ b->b_list = u->u_blocks;
+ u->u_blocks = b;
+ return b;
}
static basicblock *
compiler_use_new_block(struct compiler *c)
{
- basicblock *block = compiler_new_block(c);
- if (block == NULL)
- return NULL;
- c->u->u_curblock = block;
- return block;
+ basicblock *block = compiler_new_block(c);
+ if (block == NULL)
+ return NULL;
+ c->u->u_curblock = block;
+ return block;
}
static basicblock *
compiler_next_block(struct compiler *c)
{
- basicblock *block = compiler_new_block(c);
- if (block == NULL)
- return NULL;
- c->u->u_curblock->b_next = block;
- c->u->u_curblock = block;
- return block;
+ basicblock *block = compiler_new_block(c);
+ if (block == NULL)
+ return NULL;
+ c->u->u_curblock->b_next = block;
+ c->u->u_curblock = block;
+ return block;
}
static basicblock *
compiler_use_next_block(struct compiler *c, basicblock *block)
{
- assert(block != NULL);
- c->u->u_curblock->b_next = block;
- c->u->u_curblock = block;
- return block;
+ assert(block != NULL);
+ c->u->u_curblock->b_next = block;
+ c->u->u_curblock = block;
+ return block;
}
/* Returns the offset of the next instruction in the current block's
@@ -622,44 +622,44 @@ compiler_use_next_block(struct compiler *c, basicblock *block)
static int
compiler_next_instr(struct compiler *c, basicblock *b)
{
- assert(b != NULL);
- if (b->b_instr == NULL) {
- b->b_instr = (struct instr *)PyObject_Malloc(
- sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
- if (b->b_instr == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- b->b_ialloc = DEFAULT_BLOCK_SIZE;
- memset((char *)b->b_instr, 0,
- sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
- }
- else if (b->b_iused == b->b_ialloc) {
- struct instr *tmp;
- size_t oldsize, newsize;
- oldsize = b->b_ialloc * sizeof(struct instr);
- newsize = oldsize << 1;
-
- if (oldsize > (PY_SIZE_MAX >> 1)) {
- PyErr_NoMemory();
- return -1;
- }
-
- if (newsize == 0) {
- PyErr_NoMemory();
- return -1;
- }
- b->b_ialloc <<= 1;
- tmp = (struct instr *)PyObject_Realloc(
- (void *)b->b_instr, newsize);
- if (tmp == NULL) {
- PyErr_NoMemory();
- return -1;
- }
- b->b_instr = tmp;
- memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
- }
- return b->b_iused++;
+ assert(b != NULL);
+ if (b->b_instr == NULL) {
+ b->b_instr = (struct instr *)PyObject_Malloc(
+ sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
+ if (b->b_instr == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ b->b_ialloc = DEFAULT_BLOCK_SIZE;
+ memset((char *)b->b_instr, 0,
+ sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
+ }
+ else if (b->b_iused == b->b_ialloc) {
+ struct instr *tmp;
+ size_t oldsize, newsize;
+ oldsize = b->b_ialloc * sizeof(struct instr);
+ newsize = oldsize << 1;
+
+ if (oldsize > (PY_SIZE_MAX >> 1)) {
+ PyErr_NoMemory();
+ return -1;
+ }
+
+ if (newsize == 0) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ b->b_ialloc <<= 1;
+ tmp = (struct instr *)PyObject_Realloc(
+ (void *)b->b_instr, newsize);
+ if (tmp == NULL) {
+ PyErr_NoMemory();
+ return -1;
+ }
+ b->b_instr = tmp;
+ memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
+ }
+ return b->b_iused++;
}
/* Set the i_lineno member of the instruction at offset off if the
@@ -677,208 +677,208 @@ compiler_next_instr(struct compiler *c, basicblock *b)
static void
compiler_set_lineno(struct compiler *c, int off)
{
- basicblock *b;
- if (c->u->u_lineno_set)
- return;
- c->u->u_lineno_set = 1;
- b = c->u->u_curblock;
- b->b_instr[off].i_lineno = c->u->u_lineno;
+ basicblock *b;
+ if (c->u->u_lineno_set)
+ return;
+ c->u->u_lineno_set = 1;
+ b = c->u->u_curblock;
+ b->b_instr[off].i_lineno = c->u->u_lineno;
}
static int
opcode_stack_effect(int opcode, int oparg)
{
- switch (opcode) {
- case POP_TOP:
- return -1;
- case ROT_TWO:
- case ROT_THREE:
- return 0;
- case DUP_TOP:
- return 1;
- case ROT_FOUR:
- return 0;
-
- case UNARY_POSITIVE:
- case UNARY_NEGATIVE:
- case UNARY_NOT:
- case UNARY_INVERT:
- return 0;
-
- case SET_ADD:
- case LIST_APPEND:
- return -1;
- case MAP_ADD:
- return -2;
-
- case BINARY_POWER:
- case BINARY_MULTIPLY:
- case BINARY_MODULO:
- case BINARY_ADD:
- case BINARY_SUBTRACT:
- case BINARY_SUBSCR:
- case BINARY_FLOOR_DIVIDE:
- case BINARY_TRUE_DIVIDE:
- return -1;
- case INPLACE_FLOOR_DIVIDE:
- case INPLACE_TRUE_DIVIDE:
- return -1;
-
- case INPLACE_ADD:
- case INPLACE_SUBTRACT:
- case INPLACE_MULTIPLY:
- case INPLACE_MODULO:
- return -1;
- case STORE_SUBSCR:
- return -3;
- case STORE_MAP:
- return -2;
- case DELETE_SUBSCR:
- return -2;
-
- case BINARY_LSHIFT:
- case BINARY_RSHIFT:
- case BINARY_AND:
- case BINARY_XOR:
- case BINARY_OR:
- return -1;
- case INPLACE_POWER:
- return -1;
- case GET_ITER:
- return 0;
-
- case PRINT_EXPR:
- return -1;
- case LOAD_BUILD_CLASS:
- return 1;
- case INPLACE_LSHIFT:
- case INPLACE_RSHIFT:
- case INPLACE_AND:
- case INPLACE_XOR:
- case INPLACE_OR:
- return -1;
- case BREAK_LOOP:
- return 0;
- case WITH_CLEANUP:
- return -1; /* XXX Sometimes more */
- case STORE_LOCALS:
- return -1;
- case RETURN_VALUE:
- return -1;
- case IMPORT_STAR:
- return -1;
- case YIELD_VALUE:
- return 0;
-
- case POP_BLOCK:
- return 0;
- case POP_EXCEPT:
- return 0; /* -3 except if bad bytecode */
- case END_FINALLY:
- return -1; /* or -2 or -3 if exception occurred */
-
- case STORE_NAME:
- return -1;
- case DELETE_NAME:
- return 0;
- case UNPACK_SEQUENCE:
- return oparg-1;
- case UNPACK_EX:
- return (oparg&0xFF) + (oparg>>8);
- case FOR_ITER:
- return 1;
-
- case STORE_ATTR:
- return -2;
- case DELETE_ATTR:
- return -1;
- case STORE_GLOBAL:
- return -1;
- case DELETE_GLOBAL:
- return 0;
- case DUP_TOPX:
- return oparg;
- case LOAD_CONST:
- return 1;
- case LOAD_NAME:
- return 1;
- case BUILD_TUPLE:
- case BUILD_LIST:
- case BUILD_SET:
- return 1-oparg;
- case BUILD_MAP:
- return 1;
- case LOAD_ATTR:
- return 0;
- case COMPARE_OP:
- return -1;
- case IMPORT_NAME:
- return 0;
- case IMPORT_FROM:
- return 1;
-
- case JUMP_FORWARD:
- case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
- case JUMP_IF_FALSE_OR_POP: /* "" */
- case JUMP_ABSOLUTE:
- return 0;
-
- case POP_JUMP_IF_FALSE:
- case POP_JUMP_IF_TRUE:
- return -1;
-
- case LOAD_GLOBAL:
- return 1;
-
- case CONTINUE_LOOP:
- return 0;
- case SETUP_LOOP:
- return 0;
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- return 6; /* can push 3 values for the new exception
- + 3 others for the previous exception state */
-
- case LOAD_FAST:
- return 1;
- case STORE_FAST:
- return -1;
- case DELETE_FAST:
- return 0;
-
- case RAISE_VARARGS:
- return -oparg;
+ switch (opcode) {
+ case POP_TOP:
+ return -1;
+ case ROT_TWO:
+ case ROT_THREE:
+ return 0;
+ case DUP_TOP:
+ return 1;
+ case ROT_FOUR:
+ return 0;
+
+ case UNARY_POSITIVE:
+ case UNARY_NEGATIVE:
+ case UNARY_NOT:
+ case UNARY_INVERT:
+ return 0;
+
+ case SET_ADD:
+ case LIST_APPEND:
+ return -1;
+ case MAP_ADD:
+ return -2;
+
+ case BINARY_POWER:
+ case BINARY_MULTIPLY:
+ case BINARY_MODULO:
+ case BINARY_ADD:
+ case BINARY_SUBTRACT:
+ case BINARY_SUBSCR:
+ case BINARY_FLOOR_DIVIDE:
+ case BINARY_TRUE_DIVIDE:
+ return -1;
+ case INPLACE_FLOOR_DIVIDE:
+ case INPLACE_TRUE_DIVIDE:
+ return -1;
+
+ case INPLACE_ADD:
+ case INPLACE_SUBTRACT:
+ case INPLACE_MULTIPLY:
+ case INPLACE_MODULO:
+ return -1;
+ case STORE_SUBSCR:
+ return -3;
+ case STORE_MAP:
+ return -2;
+ case DELETE_SUBSCR:
+ return -2;
+
+ case BINARY_LSHIFT:
+ case BINARY_RSHIFT:
+ case BINARY_AND:
+ case BINARY_XOR:
+ case BINARY_OR:
+ return -1;
+ case INPLACE_POWER:
+ return -1;
+ case GET_ITER:
+ return 0;
+
+ case PRINT_EXPR:
+ return -1;
+ case LOAD_BUILD_CLASS:
+ return 1;
+ case INPLACE_LSHIFT:
+ case INPLACE_RSHIFT:
+ case INPLACE_AND:
+ case INPLACE_XOR:
+ case INPLACE_OR:
+ return -1;
+ case BREAK_LOOP:
+ return 0;
+ case WITH_CLEANUP:
+ return -1; /* XXX Sometimes more */
+ case STORE_LOCALS:
+ return -1;
+ case RETURN_VALUE:
+ return -1;
+ case IMPORT_STAR:
+ return -1;
+ case YIELD_VALUE:
+ return 0;
+
+ case POP_BLOCK:
+ return 0;
+ case POP_EXCEPT:
+ return 0; /* -3 except if bad bytecode */
+ case END_FINALLY:
+ return -1; /* or -2 or -3 if exception occurred */
+
+ case STORE_NAME:
+ return -1;
+ case DELETE_NAME:
+ return 0;
+ case UNPACK_SEQUENCE:
+ return oparg-1;
+ case UNPACK_EX:
+ return (oparg&0xFF) + (oparg>>8);
+ case FOR_ITER:
+ return 1;
+
+ case STORE_ATTR:
+ return -2;
+ case DELETE_ATTR:
+ return -1;
+ case STORE_GLOBAL:
+ return -1;
+ case DELETE_GLOBAL:
+ return 0;
+ case DUP_TOPX:
+ return oparg;
+ case LOAD_CONST:
+ return 1;
+ case LOAD_NAME:
+ return 1;
+ case BUILD_TUPLE:
+ case BUILD_LIST:
+ case BUILD_SET:
+ return 1-oparg;
+ case BUILD_MAP:
+ return 1;
+ case LOAD_ATTR:
+ return 0;
+ case COMPARE_OP:
+ return -1;
+ case IMPORT_NAME:
+ return 0;
+ case IMPORT_FROM:
+ return 1;
+
+ case JUMP_FORWARD:
+ case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
+ case JUMP_IF_FALSE_OR_POP: /* "" */
+ case JUMP_ABSOLUTE:
+ return 0;
+
+ case POP_JUMP_IF_FALSE:
+ case POP_JUMP_IF_TRUE:
+ return -1;
+
+ case LOAD_GLOBAL:
+ return 1;
+
+ case CONTINUE_LOOP:
+ return 0;
+ case SETUP_LOOP:
+ return 0;
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ return 6; /* can push 3 values for the new exception
+ + 3 others for the previous exception state */
+
+ case LOAD_FAST:
+ return 1;
+ case STORE_FAST:
+ return -1;
+ case DELETE_FAST:
+ return 0;
+
+ case RAISE_VARARGS:
+ return -oparg;
#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
- case CALL_FUNCTION:
- return -NARGS(oparg);
- case CALL_FUNCTION_VAR:
- case CALL_FUNCTION_KW:
- return -NARGS(oparg)-1;
- case CALL_FUNCTION_VAR_KW:
- return -NARGS(oparg)-2;
- case MAKE_FUNCTION:
- return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
- case MAKE_CLOSURE:
- return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
+ case CALL_FUNCTION:
+ return -NARGS(oparg);
+ case CALL_FUNCTION_VAR:
+ case CALL_FUNCTION_KW:
+ return -NARGS(oparg)-1;
+ case CALL_FUNCTION_VAR_KW:
+ return -NARGS(oparg)-2;
+ case MAKE_FUNCTION:
+ return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
+ case MAKE_CLOSURE:
+ return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
#undef NARGS
- case BUILD_SLICE:
- if (oparg == 3)
- return -2;
- else
- return -1;
-
- case LOAD_CLOSURE:
- return 1;
- case LOAD_DEREF:
- return 1;
- case STORE_DEREF:
- return -1;
- default:
- fprintf(stderr, "opcode = %d\n", opcode);
- Py_FatalError("opcode_stack_effect()");
-
- }
- return 0; /* not reachable */
+ case BUILD_SLICE:
+ if (oparg == 3)
+ return -2;
+ else
+ return -1;
+
+ case LOAD_CLOSURE:
+ return 1;
+ case LOAD_DEREF:
+ return 1;
+ case STORE_DEREF:
+ return -1;
+ default:
+ fprintf(stderr, "opcode = %d\n", opcode);
+ Py_FatalError("opcode_stack_effect()");
+
+ }
+ return 0; /* not reachable */
}
/* Add an opcode with no argument.
@@ -888,118 +888,118 @@ opcode_stack_effect(int opcode, int oparg)
static int
compiler_addop(struct compiler *c, int opcode)
{
- basicblock *b;
- struct instr *i;
- int off;
- off = compiler_next_instr(c, c->u->u_curblock);
- if (off < 0)
- return 0;
- b = c->u->u_curblock;
- i = &b->b_instr[off];
- i->i_opcode = opcode;
- i->i_hasarg = 0;
- if (opcode == RETURN_VALUE)
- b->b_return = 1;
- compiler_set_lineno(c, off);
- return 1;
+ basicblock *b;
+ struct instr *i;
+ int off;
+ off = compiler_next_instr(c, c->u->u_curblock);
+ if (off < 0)
+ return 0;
+ b = c->u->u_curblock;
+ i = &b->b_instr[off];
+ i->i_opcode = opcode;
+ i->i_hasarg = 0;
+ if (opcode == RETURN_VALUE)
+ b->b_return = 1;
+ compiler_set_lineno(c, off);
+ return 1;
}
static int
compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
{
- PyObject *t, *v;
- Py_ssize_t arg;
- double d;
-
- /* necessary to make sure types aren't coerced (e.g., int and long) */
- /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
- if (PyFloat_Check(o)) {
- d = PyFloat_AS_DOUBLE(o);
- /* all we need is to make the tuple different in either the 0.0
- * or -0.0 case from all others, just to avoid the "coercion".
- */
- if (d == 0.0 && copysign(1.0, d) < 0.0)
- t = PyTuple_Pack(3, o, o->ob_type, Py_None);
- else
- t = PyTuple_Pack(2, o, o->ob_type);
- }
+ PyObject *t, *v;
+ Py_ssize_t arg;
+ double d;
+
+ /* necessary to make sure types aren't coerced (e.g., int and long) */
+ /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
+ if (PyFloat_Check(o)) {
+ d = PyFloat_AS_DOUBLE(o);
+ /* all we need is to make the tuple different in either the 0.0
+ * or -0.0 case from all others, just to avoid the "coercion".
+ */
+ if (d == 0.0 && copysign(1.0, d) < 0.0)
+ t = PyTuple_Pack(3, o, o->ob_type, Py_None);
+ else
+ t = PyTuple_Pack(2, o, o->ob_type);
+ }
#ifndef WITHOUT_COMPLEX
- else if (PyComplex_Check(o)) {
- Py_complex z;
- int real_negzero, imag_negzero;
- /* For the complex case we must make complex(x, 0.)
- different from complex(x, -0.) and complex(0., y)
- different from complex(-0., y), for any x and y.
- All four complex zeros must be distinguished.*/
- z = PyComplex_AsCComplex(o);
- real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
- imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
- if (real_negzero && imag_negzero) {
- t = PyTuple_Pack(5, o, o->ob_type,
- Py_None, Py_None, Py_None);
- }
- else if (imag_negzero) {
- t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
- }
- else if (real_negzero) {
- t = PyTuple_Pack(3, o, o->ob_type, Py_None);
- }
- else {
- t = PyTuple_Pack(2, o, o->ob_type);
- }
+ else if (PyComplex_Check(o)) {
+ Py_complex z;
+ int real_negzero, imag_negzero;
+ /* For the complex case we must make complex(x, 0.)
+ different from complex(x, -0.) and complex(0., y)
+ different from complex(-0., y), for any x and y.
+ All four complex zeros must be distinguished.*/
+ z = PyComplex_AsCComplex(o);
+ real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
+ imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
+ if (real_negzero && imag_negzero) {
+ t = PyTuple_Pack(5, o, o->ob_type,
+ Py_None, Py_None, Py_None);
+ }
+ else if (imag_negzero) {
+ t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
}
+ else if (real_negzero) {
+ t = PyTuple_Pack(3, o, o->ob_type, Py_None);
+ }
+ else {
+ t = PyTuple_Pack(2, o, o->ob_type);
+ }
+ }
#endif /* WITHOUT_COMPLEX */
- else {
- t = PyTuple_Pack(2, o, o->ob_type);
+ else {
+ t = PyTuple_Pack(2, o, o->ob_type);
+ }
+ if (t == NULL)
+ return -1;
+
+ v = PyDict_GetItem(dict, t);
+ if (!v) {
+ if (PyErr_Occurred())
+ return -1;
+ arg = PyDict_Size(dict);
+ v = PyLong_FromLong(arg);
+ if (!v) {
+ Py_DECREF(t);
+ return -1;
+ }
+ if (PyDict_SetItem(dict, t, v) < 0) {
+ Py_DECREF(t);
+ Py_DECREF(v);
+ return -1;
}
- if (t == NULL)
- return -1;
-
- v = PyDict_GetItem(dict, t);
- if (!v) {
- if (PyErr_Occurred())
- return -1;
- arg = PyDict_Size(dict);
- v = PyLong_FromLong(arg);
- if (!v) {
- Py_DECREF(t);
- return -1;
- }
- if (PyDict_SetItem(dict, t, v) < 0) {
- Py_DECREF(t);
- Py_DECREF(v);
- return -1;
- }
- Py_DECREF(v);
- }
- else
- arg = PyLong_AsLong(v);
- Py_DECREF(t);
- return arg;
+ Py_DECREF(v);
+ }
+ else
+ arg = PyLong_AsLong(v);
+ Py_DECREF(t);
+ return arg;
}
static int
compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
- PyObject *o)
+ PyObject *o)
{
int arg = compiler_add_o(c, dict, o);
if (arg < 0)
- return 0;
+ return 0;
return compiler_addop_i(c, opcode, arg);
}
static int
compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
- PyObject *o)
+ PyObject *o)
{
int arg;
PyObject *mangled = _Py_Mangle(c->u->u_private, o);
if (!mangled)
- return 0;
+ return 0;
arg = compiler_add_o(c, dict, mangled);
Py_DECREF(mangled);
if (arg < 0)
- return 0;
+ return 0;
return compiler_addop_i(c, opcode, arg);
}
@@ -1010,43 +1010,43 @@ compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
static int
compiler_addop_i(struct compiler *c, int opcode, int oparg)
{
- struct instr *i;
- int off;
- off = compiler_next_instr(c, c->u->u_curblock);
- if (off < 0)
- return 0;
- i = &c->u->u_curblock->b_instr[off];
- i->i_opcode = opcode;
- i->i_oparg = oparg;
- i->i_hasarg = 1;
- compiler_set_lineno(c, off);
- return 1;
+ struct instr *i;
+ int off;
+ off = compiler_next_instr(c, c->u->u_curblock);
+ if (off < 0)
+ return 0;
+ i = &c->u->u_curblock->b_instr[off];
+ i->i_opcode = opcode;
+ i->i_oparg = oparg;
+ i->i_hasarg = 1;
+ compiler_set_lineno(c, off);
+ return 1;
}
static int
compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
{
- struct instr *i;
- int off;
-
- assert(b != NULL);
- off = compiler_next_instr(c, c->u->u_curblock);
- if (off < 0)
- return 0;
- i = &c->u->u_curblock->b_instr[off];
- i->i_opcode = opcode;
- i->i_target = b;
- i->i_hasarg = 1;
- if (absolute)
- i->i_jabs = 1;
- else
- i->i_jrel = 1;
- compiler_set_lineno(c, off);
- return 1;
+ struct instr *i;
+ int off;
+
+ assert(b != NULL);
+ off = compiler_next_instr(c, c->u->u_curblock);
+ if (off < 0)
+ return 0;
+ i = &c->u->u_curblock->b_instr[off];
+ i->i_opcode = opcode;
+ i->i_target = b;
+ i->i_hasarg = 1;
+ if (absolute)
+ i->i_jabs = 1;
+ else
+ i->i_jrel = 1;
+ compiler_set_lineno(c, off);
+ return 1;
}
-/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
- like to find better names.) NEW_BLOCK() creates a new block and sets
+/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
+ like to find better names.) NEW_BLOCK() creates a new block and sets
it as the current block. NEXT_BLOCK() also creates an implicit jump
from the current block to the new block.
*/
@@ -1057,50 +1057,50 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
#define NEW_BLOCK(C) { \
- if (compiler_use_new_block((C)) == NULL) \
- return 0; \
+ if (compiler_use_new_block((C)) == NULL) \
+ return 0; \
}
#define NEXT_BLOCK(C) { \
- if (compiler_next_block((C)) == NULL) \
- return 0; \
+ if (compiler_next_block((C)) == NULL) \
+ return 0; \
}
#define ADDOP(C, OP) { \
- if (!compiler_addop((C), (OP))) \
- return 0; \
+ if (!compiler_addop((C), (OP))) \
+ return 0; \
}
#define ADDOP_IN_SCOPE(C, OP) { \
- if (!compiler_addop((C), (OP))) { \
- compiler_exit_scope(c); \
- return 0; \
- } \
+ if (!compiler_addop((C), (OP))) { \
+ compiler_exit_scope(c); \
+ return 0; \
+ } \
}
#define ADDOP_O(C, OP, O, TYPE) { \
- if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
- return 0; \
+ if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
+ return 0; \
}
#define ADDOP_NAME(C, OP, O, TYPE) { \
- if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
- return 0; \
+ if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
+ return 0; \
}
#define ADDOP_I(C, OP, O) { \
- if (!compiler_addop_i((C), (OP), (O))) \
- return 0; \
+ if (!compiler_addop_i((C), (OP), (O))) \
+ return 0; \
}
#define ADDOP_JABS(C, OP, O) { \
- if (!compiler_addop_j((C), (OP), (O), 1)) \
- return 0; \
+ if (!compiler_addop_j((C), (OP), (O), 1)) \
+ return 0; \
}
#define ADDOP_JREL(C, OP, O) { \
- if (!compiler_addop_j((C), (OP), (O), 0)) \
- return 0; \
+ if (!compiler_addop_j((C), (OP), (O), 0)) \
+ return 0; \
}
/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
@@ -1108,49 +1108,49 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
*/
#define VISIT(C, TYPE, V) {\
- if (!compiler_visit_ ## TYPE((C), (V))) \
- return 0; \
+ if (!compiler_visit_ ## TYPE((C), (V))) \
+ return 0; \
}
#define VISIT_IN_SCOPE(C, TYPE, V) {\
- if (!compiler_visit_ ## TYPE((C), (V))) { \
- compiler_exit_scope(c); \
- return 0; \
- } \
+ if (!compiler_visit_ ## TYPE((C), (V))) { \
+ compiler_exit_scope(c); \
+ return 0; \
+ } \
}
#define VISIT_SLICE(C, V, CTX) {\
- if (!compiler_visit_slice((C), (V), (CTX))) \
- return 0; \
+ if (!compiler_visit_slice((C), (V), (CTX))) \
+ return 0; \
}
#define VISIT_SEQ(C, TYPE, SEQ) { \
- int _i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
- if (!compiler_visit_ ## TYPE((C), elt)) \
- return 0; \
- } \
+ int _i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
+ if (!compiler_visit_ ## TYPE((C), elt)) \
+ return 0; \
+ } \
}
#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
- int _i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
- if (!compiler_visit_ ## TYPE((C), elt)) { \
- compiler_exit_scope(c); \
- return 0; \
- } \
- } \
+ int _i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
+ if (!compiler_visit_ ## TYPE((C), elt)) { \
+ compiler_exit_scope(c); \
+ return 0; \
+ } \
+ } \
}
static int
compiler_isdocstring(stmt_ty s)
{
if (s->kind != Expr_kind)
- return 0;
+ return 0;
return s->v.Expr.value->kind == Str_kind;
}
@@ -1159,67 +1159,67 @@ compiler_isdocstring(stmt_ty s)
static int
compiler_body(struct compiler *c, asdl_seq *stmts)
{
- int i = 0;
- stmt_ty st;
-
- if (!asdl_seq_LEN(stmts))
- return 1;
- st = (stmt_ty)asdl_seq_GET(stmts, 0);
- if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
- /* don't generate docstrings if -OO */
- i = 1;
- VISIT(c, expr, st->v.Expr.value);
- if (!compiler_nameop(c, __doc__, Store))
- return 0;
- }
- for (; i < asdl_seq_LEN(stmts); i++)
- VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
- return 1;
+ int i = 0;
+ stmt_ty st;
+
+ if (!asdl_seq_LEN(stmts))
+ return 1;
+ st = (stmt_ty)asdl_seq_GET(stmts, 0);
+ if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
+ /* don't generate docstrings if -OO */
+ i = 1;
+ VISIT(c, expr, st->v.Expr.value);
+ if (!compiler_nameop(c, __doc__, Store))
+ return 0;
+ }
+ for (; i < asdl_seq_LEN(stmts); i++)
+ VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
+ return 1;
}
static PyCodeObject *
compiler_mod(struct compiler *c, mod_ty mod)
{
- PyCodeObject *co;
- int addNone = 1;
- static PyObject *module;
- if (!module) {
- module = PyUnicode_InternFromString("<module>");
- if (!module)
- return NULL;
- }
- /* Use 0 for firstlineno initially, will fixup in assemble(). */
- if (!compiler_enter_scope(c, module, mod, 0))
- return NULL;
- switch (mod->kind) {
- case Module_kind:
- if (!compiler_body(c, mod->v.Module.body)) {
- compiler_exit_scope(c);
- return 0;
- }
- break;
- case Interactive_kind:
- c->c_interactive = 1;
- VISIT_SEQ_IN_SCOPE(c, stmt,
- mod->v.Interactive.body);
- break;
- case Expression_kind:
- VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
- addNone = 0;
- break;
- case Suite_kind:
- PyErr_SetString(PyExc_SystemError,
- "suite should not be possible");
- return 0;
- default:
- PyErr_Format(PyExc_SystemError,
- "module kind %d should not be possible",
- mod->kind);
- return 0;
- }
- co = assemble(c, addNone);
- compiler_exit_scope(c);
- return co;
+ PyCodeObject *co;
+ int addNone = 1;
+ static PyObject *module;
+ if (!module) {
+ module = PyUnicode_InternFromString("<module>");
+ if (!module)
+ return NULL;
+ }
+ /* Use 0 for firstlineno initially, will fixup in assemble(). */
+ if (!compiler_enter_scope(c, module, mod, 0))
+ return NULL;
+ switch (mod->kind) {
+ case Module_kind:
+ if (!compiler_body(c, mod->v.Module.body)) {
+ compiler_exit_scope(c);
+ return 0;
+ }
+ break;
+ case Interactive_kind:
+ c->c_interactive = 1;
+ VISIT_SEQ_IN_SCOPE(c, stmt,
+ mod->v.Interactive.body);
+ break;
+ case Expression_kind:
+ VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
+ addNone = 0;
+ break;
+ case Suite_kind:
+ PyErr_SetString(PyExc_SystemError,
+ "suite should not be possible");
+ return 0;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "module kind %d should not be possible",
+ mod->kind);
+ return 0;
+ }
+ co = assemble(c, addNone);
+ compiler_exit_scope(c);
+ return co;
}
/* The test for LOCAL must come before the test for FREE in order to
@@ -1230,24 +1230,24 @@ compiler_mod(struct compiler *c, mod_ty mod)
static int
get_ref_type(struct compiler *c, PyObject *name)
{
- int scope = PyST_GetScope(c->u->u_ste, name);
- if (scope == 0) {
- char buf[350];
- PyOS_snprintf(buf, sizeof(buf),
- "unknown scope for %.100s in %.100s(%s) in %s\n"
- "symbols: %s\nlocals: %s\nglobals: %s",
- PyBytes_AS_STRING(name),
- PyBytes_AS_STRING(c->u->u_name),
- PyObject_REPR(c->u->u_ste->ste_id),
- c->c_filename,
- PyObject_REPR(c->u->u_ste->ste_symbols),
- PyObject_REPR(c->u->u_varnames),
- PyObject_REPR(c->u->u_names)
- );
- Py_FatalError(buf);
- }
-
- return scope;
+ int scope = PyST_GetScope(c->u->u_ste, name);
+ if (scope == 0) {
+ char buf[350];
+ PyOS_snprintf(buf, sizeof(buf),
+ "unknown scope for %.100s in %.100s(%s) in %s\n"
+ "symbols: %s\nlocals: %s\nglobals: %s",
+ PyBytes_AS_STRING(name),
+ PyBytes_AS_STRING(c->u->u_name),
+ PyObject_REPR(c->u->u_ste->ste_id),
+ c->c_filename,
+ PyObject_REPR(c->u->u_ste->ste_symbols),
+ PyObject_REPR(c->u->u_varnames),
+ PyObject_REPR(c->u->u_names)
+ );
+ Py_FatalError(buf);
+ }
+
+ return scope;
}
static int
@@ -1256,637 +1256,637 @@ compiler_lookup_arg(PyObject *dict, PyObject *name)
PyObject *k, *v;
k = PyTuple_Pack(2, name, name->ob_type);
if (k == NULL)
- return -1;
+ return -1;
v = PyDict_GetItem(dict, k);
Py_DECREF(k);
if (v == NULL)
- return -1;
+ return -1;
return PyLong_AS_LONG(v);
}
static int
compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
{
- int i, free = PyCode_GetNumFree(co);
- if (free == 0) {
- ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
- ADDOP_I(c, MAKE_FUNCTION, args);
- return 1;
- }
- for (i = 0; i < free; ++i) {
- /* Bypass com_addop_varname because it will generate
- LOAD_DEREF but LOAD_CLOSURE is needed.
- */
- PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
- int arg, reftype;
-
- /* Special case: If a class contains a method with a
- free variable that has the same name as a method,
- the name will be considered free *and* local in the
- class. It should be handled by the closure, as
- well as by the normal name loookup logic.
- */
- reftype = get_ref_type(c, name);
- if (reftype == CELL)
- arg = compiler_lookup_arg(c->u->u_cellvars, name);
- else /* (reftype == FREE) */
- arg = compiler_lookup_arg(c->u->u_freevars, name);
- if (arg == -1) {
- fprintf(stderr,
- "lookup %s in %s %d %d\n"
- "freevars of %s: %s\n",
- PyObject_REPR(name),
- PyBytes_AS_STRING(c->u->u_name),
- reftype, arg,
- _PyUnicode_AsString(co->co_name),
- PyObject_REPR(co->co_freevars));
- Py_FatalError("compiler_make_closure()");
- }
- ADDOP_I(c, LOAD_CLOSURE, arg);
- }
- ADDOP_I(c, BUILD_TUPLE, free);
- ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
- ADDOP_I(c, MAKE_CLOSURE, args);
- return 1;
+ int i, free = PyCode_GetNumFree(co);
+ if (free == 0) {
+ ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
+ ADDOP_I(c, MAKE_FUNCTION, args);
+ return 1;
+ }
+ for (i = 0; i < free; ++i) {
+ /* Bypass com_addop_varname because it will generate
+ LOAD_DEREF but LOAD_CLOSURE is needed.
+ */
+ PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
+ int arg, reftype;
+
+ /* Special case: If a class contains a method with a
+ free variable that has the same name as a method,
+ the name will be considered free *and* local in the
+ class. It should be handled by the closure, as
+ well as by the normal name loookup logic.
+ */
+ reftype = get_ref_type(c, name);
+ if (reftype == CELL)
+ arg = compiler_lookup_arg(c->u->u_cellvars, name);
+ else /* (reftype == FREE) */
+ arg = compiler_lookup_arg(c->u->u_freevars, name);
+ if (arg == -1) {
+ fprintf(stderr,
+ "lookup %s in %s %d %d\n"
+ "freevars of %s: %s\n",
+ PyObject_REPR(name),
+ PyBytes_AS_STRING(c->u->u_name),
+ reftype, arg,
+ _PyUnicode_AsString(co->co_name),
+ PyObject_REPR(co->co_freevars));
+ Py_FatalError("compiler_make_closure()");
+ }
+ ADDOP_I(c, LOAD_CLOSURE, arg);
+ }
+ ADDOP_I(c, BUILD_TUPLE, free);
+ ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
+ ADDOP_I(c, MAKE_CLOSURE, args);
+ return 1;
}
static int
compiler_decorators(struct compiler *c, asdl_seq* decos)
{
- int i;
+ int i;
- if (!decos)
- return 1;
+ if (!decos)
+ return 1;
- for (i = 0; i < asdl_seq_LEN(decos); i++) {
- VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
- }
- return 1;
+ for (i = 0; i < asdl_seq_LEN(decos); i++) {
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
+ }
+ return 1;
}
static int
compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
- asdl_seq *kw_defaults)
+ asdl_seq *kw_defaults)
{
- int i, default_count = 0;
- for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
- arg_ty arg = asdl_seq_GET(kwonlyargs, i);
- expr_ty default_ = asdl_seq_GET(kw_defaults, i);
- if (default_) {
- ADDOP_O(c, LOAD_CONST, arg->arg, consts);
- if (!compiler_visit_expr(c, default_)) {
- return -1;
- }
- default_count++;
- }
- }
- return default_count;
+ int i, default_count = 0;
+ for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
+ arg_ty arg = asdl_seq_GET(kwonlyargs, i);
+ expr_ty default_ = asdl_seq_GET(kw_defaults, i);
+ if (default_) {
+ ADDOP_O(c, LOAD_CONST, arg->arg, consts);
+ if (!compiler_visit_expr(c, default_)) {
+ return -1;
+ }
+ default_count++;
+ }
+ }
+ return default_count;
}
static int
compiler_visit_argannotation(struct compiler *c, identifier id,
expr_ty annotation, PyObject *names)
{
- if (annotation) {
- VISIT(c, expr, annotation);
- if (PyList_Append(names, id))
- return -1;
- }
- return 0;
+ if (annotation) {
+ VISIT(c, expr, annotation);
+ if (PyList_Append(names, id))
+ return -1;
+ }
+ return 0;
}
static int
compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
PyObject *names)
{
- int i, error;
- for (i = 0; i < asdl_seq_LEN(args); i++) {
- arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
- error = compiler_visit_argannotation(
- c,
- arg->arg,
- arg->annotation,
- names);
- if (error)
- return error;
- }
- return 0;
+ int i, error;
+ for (i = 0; i < asdl_seq_LEN(args); i++) {
+ arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
+ error = compiler_visit_argannotation(
+ c,
+ arg->arg,
+ arg->annotation,
+ names);
+ if (error)
+ return error;
+ }
+ return 0;
}
static int
compiler_visit_annotations(struct compiler *c, arguments_ty args,
expr_ty returns)
{
- /* Push arg annotations and a list of the argument names. Return the #
- of items pushed. The expressions are evaluated out-of-order wrt the
- source code.
-
- More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
- */
- static identifier return_str;
- PyObject *names;
- int len;
- names = PyList_New(0);
- if (!names)
- return -1;
-
- if (compiler_visit_argannotations(c, args->args, names))
- goto error;
- if (args->varargannotation &&
- compiler_visit_argannotation(c, args->vararg,
- args->varargannotation, names))
- goto error;
- if (compiler_visit_argannotations(c, args->kwonlyargs, names))
- goto error;
- if (args->kwargannotation &&
- compiler_visit_argannotation(c, args->kwarg,
- args->kwargannotation, names))
- goto error;
-
- if (!return_str) {
- return_str = PyUnicode_InternFromString("return");
- if (!return_str)
- goto error;
- }
- if (compiler_visit_argannotation(c, return_str, returns, names)) {
- goto error;
- }
-
- len = PyList_GET_SIZE(names);
- if (len > 65534) {
- /* len must fit in 16 bits, and len is incremented below */
- PyErr_SetString(PyExc_SyntaxError,
- "too many annotations");
- goto error;
- }
- if (len) {
- /* convert names to a tuple and place on stack */
- PyObject *elt;
- int i;
- PyObject *s = PyTuple_New(len);
- if (!s)
- goto error;
- for (i = 0; i < len; i++) {
- elt = PyList_GET_ITEM(names, i);
- Py_INCREF(elt);
- PyTuple_SET_ITEM(s, i, elt);
- }
- ADDOP_O(c, LOAD_CONST, s, consts);
- Py_DECREF(s);
- len++; /* include the just-pushed tuple */
- }
- Py_DECREF(names);
- return len;
+ /* Push arg annotations and a list of the argument names. Return the #
+ of items pushed. The expressions are evaluated out-of-order wrt the
+ source code.
+
+ More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
+ */
+ static identifier return_str;
+ PyObject *names;
+ int len;
+ names = PyList_New(0);
+ if (!names)
+ return -1;
+
+ if (compiler_visit_argannotations(c, args->args, names))
+ goto error;
+ if (args->varargannotation &&
+ compiler_visit_argannotation(c, args->vararg,
+ args->varargannotation, names))
+ goto error;
+ if (compiler_visit_argannotations(c, args->kwonlyargs, names))
+ goto error;
+ if (args->kwargannotation &&
+ compiler_visit_argannotation(c, args->kwarg,
+ args->kwargannotation, names))
+ goto error;
+
+ if (!return_str) {
+ return_str = PyUnicode_InternFromString("return");
+ if (!return_str)
+ goto error;
+ }
+ if (compiler_visit_argannotation(c, return_str, returns, names)) {
+ goto error;
+ }
+
+ len = PyList_GET_SIZE(names);
+ if (len > 65534) {
+ /* len must fit in 16 bits, and len is incremented below */
+ PyErr_SetString(PyExc_SyntaxError,
+ "too many annotations");
+ goto error;
+ }
+ if (len) {
+ /* convert names to a tuple and place on stack */
+ PyObject *elt;
+ int i;
+ PyObject *s = PyTuple_New(len);
+ if (!s)
+ goto error;
+ for (i = 0; i < len; i++) {
+ elt = PyList_GET_ITEM(names, i);
+ Py_INCREF(elt);
+ PyTuple_SET_ITEM(s, i, elt);
+ }
+ ADDOP_O(c, LOAD_CONST, s, consts);
+ Py_DECREF(s);
+ len++; /* include the just-pushed tuple */
+ }
+ Py_DECREF(names);
+ return len;
error:
- Py_DECREF(names);
- return -1;
+ Py_DECREF(names);
+ return -1;
}
static int
compiler_function(struct compiler *c, stmt_ty s)
{
- PyCodeObject *co;
- PyObject *first_const = Py_None;
- arguments_ty args = s->v.FunctionDef.args;
- expr_ty returns = s->v.FunctionDef.returns;
- asdl_seq* decos = s->v.FunctionDef.decorator_list;
- stmt_ty st;
- int i, n, docstring, kw_default_count = 0, arglength;
- int num_annotations;
-
- assert(s->kind == FunctionDef_kind);
-
- if (!compiler_decorators(c, decos))
- return 0;
- if (args->kwonlyargs) {
- int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
- args->kw_defaults);
- if (res < 0)
- return 0;
- kw_default_count = res;
- }
- if (args->defaults)
- VISIT_SEQ(c, expr, args->defaults);
- num_annotations = compiler_visit_annotations(c, args, returns);
- if (num_annotations < 0)
- return 0;
- assert((num_annotations & 0xFFFF) == num_annotations);
-
- if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
- s->lineno))
- return 0;
-
- st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
- docstring = compiler_isdocstring(st);
- if (docstring && Py_OptimizeFlag < 2)
- first_const = st->v.Expr.value->v.Str.s;
- if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
- compiler_exit_scope(c);
- return 0;
- }
-
- c->u->u_argcount = asdl_seq_LEN(args->args);
- c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
- n = asdl_seq_LEN(s->v.FunctionDef.body);
- /* if there was a docstring, we need to skip the first statement */
- for (i = docstring; i < n; i++) {
- st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
- VISIT_IN_SCOPE(c, stmt, st);
- }
- co = assemble(c, 1);
- compiler_exit_scope(c);
- if (co == NULL)
- return 0;
-
- arglength = asdl_seq_LEN(args->defaults);
- arglength |= kw_default_count << 8;
- arglength |= num_annotations << 16;
- compiler_make_closure(c, co, arglength);
- Py_DECREF(co);
-
- /* decorators */
- for (i = 0; i < asdl_seq_LEN(decos); i++) {
- ADDOP_I(c, CALL_FUNCTION, 1);
- }
-
- return compiler_nameop(c, s->v.FunctionDef.name, Store);
+ PyCodeObject *co;
+ PyObject *first_const = Py_None;
+ arguments_ty args = s->v.FunctionDef.args;
+ expr_ty returns = s->v.FunctionDef.returns;
+ asdl_seq* decos = s->v.FunctionDef.decorator_list;
+ stmt_ty st;
+ int i, n, docstring, kw_default_count = 0, arglength;
+ int num_annotations;
+
+ assert(s->kind == FunctionDef_kind);
+
+ if (!compiler_decorators(c, decos))
+ return 0;
+ if (args->kwonlyargs) {
+ int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
+ args->kw_defaults);
+ if (res < 0)
+ return 0;
+ kw_default_count = res;
+ }
+ if (args->defaults)
+ VISIT_SEQ(c, expr, args->defaults);
+ num_annotations = compiler_visit_annotations(c, args, returns);
+ if (num_annotations < 0)
+ return 0;
+ assert((num_annotations & 0xFFFF) == num_annotations);
+
+ if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
+ s->lineno))
+ return 0;
+
+ st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
+ docstring = compiler_isdocstring(st);
+ if (docstring && Py_OptimizeFlag < 2)
+ first_const = st->v.Expr.value->v.Str.s;
+ if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
+ compiler_exit_scope(c);
+ return 0;
+ }
+
+ c->u->u_argcount = asdl_seq_LEN(args->args);
+ c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
+ n = asdl_seq_LEN(s->v.FunctionDef.body);
+ /* if there was a docstring, we need to skip the first statement */
+ for (i = docstring; i < n; i++) {
+ st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
+ VISIT_IN_SCOPE(c, stmt, st);
+ }
+ co = assemble(c, 1);
+ compiler_exit_scope(c);
+ if (co == NULL)
+ return 0;
+
+ arglength = asdl_seq_LEN(args->defaults);
+ arglength |= kw_default_count << 8;
+ arglength |= num_annotations << 16;
+ compiler_make_closure(c, co, arglength);
+ Py_DECREF(co);
+
+ /* decorators */
+ for (i = 0; i < asdl_seq_LEN(decos); i++) {
+ ADDOP_I(c, CALL_FUNCTION, 1);
+ }
+
+ return compiler_nameop(c, s->v.FunctionDef.name, Store);
}
static int
compiler_class(struct compiler *c, stmt_ty s)
{
- PyCodeObject *co;
- PyObject *str;
- int i;
- asdl_seq* decos = s->v.ClassDef.decorator_list;
-
- if (!compiler_decorators(c, decos))
- return 0;
-
- /* ultimately generate code for:
- <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
- where:
- <func> is a function/closure created from the class body;
- it has a single argument (__locals__) where the dict
- (or MutableSequence) representing the locals is passed
- <name> is the class name
- <bases> is the positional arguments and *varargs argument
- <keywords> is the keyword arguments and **kwds argument
- This borrows from compiler_call.
- */
-
- /* 1. compile the class body into a code object */
- if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
- return 0;
- /* this block represents what we do in the new scope */
- {
- /* use the class name for name mangling */
- Py_INCREF(s->v.ClassDef.name);
- Py_XDECREF(c->u->u_private);
- c->u->u_private = s->v.ClassDef.name;
- /* force it to have one mandatory argument */
- c->u->u_argcount = 1;
- /* load the first argument (__locals__) ... */
- ADDOP_I(c, LOAD_FAST, 0);
- /* ... and store it into f_locals */
- ADDOP_IN_SCOPE(c, STORE_LOCALS);
- /* load (global) __name__ ... */
- str = PyUnicode_InternFromString("__name__");
- if (!str || !compiler_nameop(c, str, Load)) {
- Py_XDECREF(str);
- compiler_exit_scope(c);
- return 0;
- }
- Py_DECREF(str);
- /* ... and store it as __module__ */
- str = PyUnicode_InternFromString("__module__");
- if (!str || !compiler_nameop(c, str, Store)) {
- Py_XDECREF(str);
- compiler_exit_scope(c);
- return 0;
- }
- Py_DECREF(str);
- /* compile the body proper */
- if (!compiler_body(c, s->v.ClassDef.body)) {
- compiler_exit_scope(c);
- return 0;
- }
- /* return the (empty) __class__ cell */
- str = PyUnicode_InternFromString("__class__");
- if (str == NULL) {
- compiler_exit_scope(c);
- return 0;
- }
- i = compiler_lookup_arg(c->u->u_cellvars, str);
- Py_DECREF(str);
- if (i == -1) {
- /* This happens when nobody references the cell */
- PyErr_Clear();
- /* Return None */
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- }
- else {
- /* Return the cell where to store __class__ */
- ADDOP_I(c, LOAD_CLOSURE, i);
- }
- ADDOP_IN_SCOPE(c, RETURN_VALUE);
- /* create the code object */
- co = assemble(c, 1);
- }
- /* leave the new scope */
- compiler_exit_scope(c);
- if (co == NULL)
- return 0;
-
- /* 2. load the 'build_class' function */
- ADDOP(c, LOAD_BUILD_CLASS);
-
- /* 3. load a function (or closure) made from the code object */
- compiler_make_closure(c, co, 0);
- Py_DECREF(co);
-
- /* 4. load class name */
- ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
-
- /* 5. generate the rest of the code for the call */
- if (!compiler_call_helper(c, 2,
- s->v.ClassDef.bases,
- s->v.ClassDef.keywords,
- s->v.ClassDef.starargs,
- s->v.ClassDef.kwargs))
- return 0;
-
- /* 6. apply decorators */
- for (i = 0; i < asdl_seq_LEN(decos); i++) {
- ADDOP_I(c, CALL_FUNCTION, 1);
+ PyCodeObject *co;
+ PyObject *str;
+ int i;
+ asdl_seq* decos = s->v.ClassDef.decorator_list;
+
+ if (!compiler_decorators(c, decos))
+ return 0;
+
+ /* ultimately generate code for:
+ <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
+ where:
+ <func> is a function/closure created from the class body;
+ it has a single argument (__locals__) where the dict
+ (or MutableSequence) representing the locals is passed
+ <name> is the class name
+ <bases> is the positional arguments and *varargs argument
+ <keywords> is the keyword arguments and **kwds argument
+ This borrows from compiler_call.
+ */
+
+ /* 1. compile the class body into a code object */
+ if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
+ return 0;
+ /* this block represents what we do in the new scope */
+ {
+ /* use the class name for name mangling */
+ Py_INCREF(s->v.ClassDef.name);
+ Py_XDECREF(c->u->u_private);
+ c->u->u_private = s->v.ClassDef.name;
+ /* force it to have one mandatory argument */
+ c->u->u_argcount = 1;
+ /* load the first argument (__locals__) ... */
+ ADDOP_I(c, LOAD_FAST, 0);
+ /* ... and store it into f_locals */
+ ADDOP_IN_SCOPE(c, STORE_LOCALS);
+ /* load (global) __name__ ... */
+ str = PyUnicode_InternFromString("__name__");
+ if (!str || !compiler_nameop(c, str, Load)) {
+ Py_XDECREF(str);
+ compiler_exit_scope(c);
+ return 0;
+ }
+ Py_DECREF(str);
+ /* ... and store it as __module__ */
+ str = PyUnicode_InternFromString("__module__");
+ if (!str || !compiler_nameop(c, str, Store)) {
+ Py_XDECREF(str);
+ compiler_exit_scope(c);
+ return 0;
+ }
+ Py_DECREF(str);
+ /* compile the body proper */
+ if (!compiler_body(c, s->v.ClassDef.body)) {
+ compiler_exit_scope(c);
+ return 0;
+ }
+ /* return the (empty) __class__ cell */
+ str = PyUnicode_InternFromString("__class__");
+ if (str == NULL) {
+ compiler_exit_scope(c);
+ return 0;
+ }
+ i = compiler_lookup_arg(c->u->u_cellvars, str);
+ Py_DECREF(str);
+ if (i == -1) {
+ /* This happens when nobody references the cell */
+ PyErr_Clear();
+ /* Return None */
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
}
+ else {
+ /* Return the cell where to store __class__ */
+ ADDOP_I(c, LOAD_CLOSURE, i);
+ }
+ ADDOP_IN_SCOPE(c, RETURN_VALUE);
+ /* create the code object */
+ co = assemble(c, 1);
+ }
+ /* leave the new scope */
+ compiler_exit_scope(c);
+ if (co == NULL)
+ return 0;
+
+ /* 2. load the 'build_class' function */
+ ADDOP(c, LOAD_BUILD_CLASS);
+
+ /* 3. load a function (or closure) made from the code object */
+ compiler_make_closure(c, co, 0);
+ Py_DECREF(co);
+
+ /* 4. load class name */
+ ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
+
+ /* 5. generate the rest of the code for the call */
+ if (!compiler_call_helper(c, 2,
+ s->v.ClassDef.bases,
+ s->v.ClassDef.keywords,
+ s->v.ClassDef.starargs,
+ s->v.ClassDef.kwargs))
+ return 0;
+
+ /* 6. apply decorators */
+ for (i = 0; i < asdl_seq_LEN(decos); i++) {
+ ADDOP_I(c, CALL_FUNCTION, 1);
+ }
- /* 7. store into <name> */
- if (!compiler_nameop(c, s->v.ClassDef.name, Store))
- return 0;
- return 1;
+ /* 7. store into <name> */
+ if (!compiler_nameop(c, s->v.ClassDef.name, Store))
+ return 0;
+ return 1;
}
static int
compiler_ifexp(struct compiler *c, expr_ty e)
{
- basicblock *end, *next;
-
- assert(e->kind == IfExp_kind);
- end = compiler_new_block(c);
- if (end == NULL)
- return 0;
- next = compiler_new_block(c);
- if (next == NULL)
- return 0;
- VISIT(c, expr, e->v.IfExp.test);
- ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
- VISIT(c, expr, e->v.IfExp.body);
- ADDOP_JREL(c, JUMP_FORWARD, end);
- compiler_use_next_block(c, next);
- VISIT(c, expr, e->v.IfExp.orelse);
- compiler_use_next_block(c, end);
- return 1;
+ basicblock *end, *next;
+
+ assert(e->kind == IfExp_kind);
+ end = compiler_new_block(c);
+ if (end == NULL)
+ return 0;
+ next = compiler_new_block(c);
+ if (next == NULL)
+ return 0;
+ VISIT(c, expr, e->v.IfExp.test);
+ ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
+ VISIT(c, expr, e->v.IfExp.body);
+ ADDOP_JREL(c, JUMP_FORWARD, end);
+ compiler_use_next_block(c, next);
+ VISIT(c, expr, e->v.IfExp.orelse);
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_lambda(struct compiler *c, expr_ty e)
{
- PyCodeObject *co;
- static identifier name;
- int kw_default_count = 0, arglength;
- arguments_ty args = e->v.Lambda.args;
- assert(e->kind == Lambda_kind);
-
- if (!name) {
- name = PyUnicode_InternFromString("<lambda>");
- if (!name)
- return 0;
- }
-
- if (args->kwonlyargs) {
- int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
- args->kw_defaults);
- if (res < 0) return 0;
- kw_default_count = res;
- }
- if (args->defaults)
- VISIT_SEQ(c, expr, args->defaults);
- if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
- return 0;
-
- c->u->u_argcount = asdl_seq_LEN(args->args);
- c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
- VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
- if (c->u->u_ste->ste_generator) {
- ADDOP_IN_SCOPE(c, POP_TOP);
- }
- else {
- ADDOP_IN_SCOPE(c, RETURN_VALUE);
- }
- co = assemble(c, 1);
- compiler_exit_scope(c);
- if (co == NULL)
- return 0;
-
- arglength = asdl_seq_LEN(args->defaults);
- arglength |= kw_default_count << 8;
- compiler_make_closure(c, co, arglength);
- Py_DECREF(co);
-
- return 1;
+ PyCodeObject *co;
+ static identifier name;
+ int kw_default_count = 0, arglength;
+ arguments_ty args = e->v.Lambda.args;
+ assert(e->kind == Lambda_kind);
+
+ if (!name) {
+ name = PyUnicode_InternFromString("<lambda>");
+ if (!name)
+ return 0;
+ }
+
+ if (args->kwonlyargs) {
+ int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
+ args->kw_defaults);
+ if (res < 0) return 0;
+ kw_default_count = res;
+ }
+ if (args->defaults)
+ VISIT_SEQ(c, expr, args->defaults);
+ if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
+ return 0;
+
+ c->u->u_argcount = asdl_seq_LEN(args->args);
+ c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
+ VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
+ if (c->u->u_ste->ste_generator) {
+ ADDOP_IN_SCOPE(c, POP_TOP);
+ }
+ else {
+ ADDOP_IN_SCOPE(c, RETURN_VALUE);
+ }
+ co = assemble(c, 1);
+ compiler_exit_scope(c);
+ if (co == NULL)
+ return 0;
+
+ arglength = asdl_seq_LEN(args->defaults);
+ arglength |= kw_default_count << 8;
+ compiler_make_closure(c, co, arglength);
+ Py_DECREF(co);
+
+ return 1;
}
static int
compiler_if(struct compiler *c, stmt_ty s)
{
- basicblock *end, *next;
- int constant;
- assert(s->kind == If_kind);
- end = compiler_new_block(c);
- if (end == NULL)
- return 0;
-
- constant = expr_constant(s->v.If.test);
- /* constant = 0: "if 0"
- * constant = 1: "if 1", "if 2", ...
- * constant = -1: rest */
- if (constant == 0) {
- if (s->v.If.orelse)
- VISIT_SEQ(c, stmt, s->v.If.orelse);
- } else if (constant == 1) {
- VISIT_SEQ(c, stmt, s->v.If.body);
- } else {
- if (s->v.If.orelse) {
- next = compiler_new_block(c);
- if (next == NULL)
- return 0;
- }
- else
- next = end;
- VISIT(c, expr, s->v.If.test);
- ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
- VISIT_SEQ(c, stmt, s->v.If.body);
- ADDOP_JREL(c, JUMP_FORWARD, end);
- if (s->v.If.orelse) {
- compiler_use_next_block(c, next);
- VISIT_SEQ(c, stmt, s->v.If.orelse);
- }
- }
- compiler_use_next_block(c, end);
- return 1;
+ basicblock *end, *next;
+ int constant;
+ assert(s->kind == If_kind);
+ end = compiler_new_block(c);
+ if (end == NULL)
+ return 0;
+
+ constant = expr_constant(s->v.If.test);
+ /* constant = 0: "if 0"
+ * constant = 1: "if 1", "if 2", ...
+ * constant = -1: rest */
+ if (constant == 0) {
+ if (s->v.If.orelse)
+ VISIT_SEQ(c, stmt, s->v.If.orelse);
+ } else if (constant == 1) {
+ VISIT_SEQ(c, stmt, s->v.If.body);
+ } else {
+ if (s->v.If.orelse) {
+ next = compiler_new_block(c);
+ if (next == NULL)
+ return 0;
+ }
+ else
+ next = end;
+ VISIT(c, expr, s->v.If.test);
+ ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
+ VISIT_SEQ(c, stmt, s->v.If.body);
+ ADDOP_JREL(c, JUMP_FORWARD, end);
+ if (s->v.If.orelse) {
+ compiler_use_next_block(c, next);
+ VISIT_SEQ(c, stmt, s->v.If.orelse);
+ }
+ }
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_for(struct compiler *c, stmt_ty s)
{
- basicblock *start, *cleanup, *end;
-
- start = compiler_new_block(c);
- cleanup = compiler_new_block(c);
- end = compiler_new_block(c);
- if (start == NULL || end == NULL || cleanup == NULL)
- return 0;
- ADDOP_JREL(c, SETUP_LOOP, end);
- if (!compiler_push_fblock(c, LOOP, start))
- return 0;
- VISIT(c, expr, s->v.For.iter);
- ADDOP(c, GET_ITER);
- compiler_use_next_block(c, start);
- /* for expressions must be traced on each iteration,
- so we need to set an extra line number. */
- c->u->u_lineno_set = 0;
- ADDOP_JREL(c, FOR_ITER, cleanup);
- VISIT(c, expr, s->v.For.target);
- VISIT_SEQ(c, stmt, s->v.For.body);
- ADDOP_JABS(c, JUMP_ABSOLUTE, start);
- compiler_use_next_block(c, cleanup);
- ADDOP(c, POP_BLOCK);
- compiler_pop_fblock(c, LOOP, start);
- VISIT_SEQ(c, stmt, s->v.For.orelse);
- compiler_use_next_block(c, end);
- return 1;
+ basicblock *start, *cleanup, *end;
+
+ start = compiler_new_block(c);
+ cleanup = compiler_new_block(c);
+ end = compiler_new_block(c);
+ if (start == NULL || end == NULL || cleanup == NULL)
+ return 0;
+ ADDOP_JREL(c, SETUP_LOOP, end);
+ if (!compiler_push_fblock(c, LOOP, start))
+ return 0;
+ VISIT(c, expr, s->v.For.iter);
+ ADDOP(c, GET_ITER);
+ compiler_use_next_block(c, start);
+ /* for expressions must be traced on each iteration,
+ so we need to set an extra line number. */
+ c->u->u_lineno_set = 0;
+ ADDOP_JREL(c, FOR_ITER, cleanup);
+ VISIT(c, expr, s->v.For.target);
+ VISIT_SEQ(c, stmt, s->v.For.body);
+ ADDOP_JABS(c, JUMP_ABSOLUTE, start);
+ compiler_use_next_block(c, cleanup);
+ ADDOP(c, POP_BLOCK);
+ compiler_pop_fblock(c, LOOP, start);
+ VISIT_SEQ(c, stmt, s->v.For.orelse);
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_while(struct compiler *c, stmt_ty s)
{
- basicblock *loop, *orelse, *end, *anchor = NULL;
- int constant = expr_constant(s->v.While.test);
-
- if (constant == 0) {
- if (s->v.While.orelse)
- VISIT_SEQ(c, stmt, s->v.While.orelse);
- return 1;
- }
- loop = compiler_new_block(c);
- end = compiler_new_block(c);
- if (constant == -1) {
- anchor = compiler_new_block(c);
- if (anchor == NULL)
- return 0;
- }
- if (loop == NULL || end == NULL)
- return 0;
- if (s->v.While.orelse) {
- orelse = compiler_new_block(c);
- if (orelse == NULL)
- return 0;
- }
- else
- orelse = NULL;
-
- ADDOP_JREL(c, SETUP_LOOP, end);
- compiler_use_next_block(c, loop);
- if (!compiler_push_fblock(c, LOOP, loop))
- return 0;
- if (constant == -1) {
- /* while expressions must be traced on each iteration,
- so we need to set an extra line number. */
- c->u->u_lineno_set = 0;
- VISIT(c, expr, s->v.While.test);
- ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
- }
- VISIT_SEQ(c, stmt, s->v.While.body);
- ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
-
- /* XXX should the two POP instructions be in a separate block
- if there is no else clause ?
- */
-
- if (constant == -1) {
- compiler_use_next_block(c, anchor);
- ADDOP(c, POP_BLOCK);
- }
- compiler_pop_fblock(c, LOOP, loop);
- if (orelse != NULL) /* what if orelse is just pass? */
- VISIT_SEQ(c, stmt, s->v.While.orelse);
- compiler_use_next_block(c, end);
-
- return 1;
+ basicblock *loop, *orelse, *end, *anchor = NULL;
+ int constant = expr_constant(s->v.While.test);
+
+ if (constant == 0) {
+ if (s->v.While.orelse)
+ VISIT_SEQ(c, stmt, s->v.While.orelse);
+ return 1;
+ }
+ loop = compiler_new_block(c);
+ end = compiler_new_block(c);
+ if (constant == -1) {
+ anchor = compiler_new_block(c);
+ if (anchor == NULL)
+ return 0;
+ }
+ if (loop == NULL || end == NULL)
+ return 0;
+ if (s->v.While.orelse) {
+ orelse = compiler_new_block(c);
+ if (orelse == NULL)
+ return 0;
+ }
+ else
+ orelse = NULL;
+
+ ADDOP_JREL(c, SETUP_LOOP, end);
+ compiler_use_next_block(c, loop);
+ if (!compiler_push_fblock(c, LOOP, loop))
+ return 0;
+ if (constant == -1) {
+ /* while expressions must be traced on each iteration,
+ so we need to set an extra line number. */
+ c->u->u_lineno_set = 0;
+ VISIT(c, expr, s->v.While.test);
+ ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
+ }
+ VISIT_SEQ(c, stmt, s->v.While.body);
+ ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
+
+ /* XXX should the two POP instructions be in a separate block
+ if there is no else clause ?
+ */
+
+ if (constant == -1) {
+ compiler_use_next_block(c, anchor);
+ ADDOP(c, POP_BLOCK);
+ }
+ compiler_pop_fblock(c, LOOP, loop);
+ if (orelse != NULL) /* what if orelse is just pass? */
+ VISIT_SEQ(c, stmt, s->v.While.orelse);
+ compiler_use_next_block(c, end);
+
+ return 1;
}
static int
compiler_continue(struct compiler *c)
{
- static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
- static const char IN_FINALLY_ERROR_MSG[] =
- "'continue' not supported inside 'finally' clause";
- int i;
-
- if (!c->u->u_nfblocks)
- return compiler_error(c, LOOP_ERROR_MSG);
- i = c->u->u_nfblocks - 1;
- switch (c->u->u_fblock[i].fb_type) {
- case LOOP:
- ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
- break;
- case EXCEPT:
- case FINALLY_TRY:
- while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
- /* Prevent continue anywhere under a finally
- even if hidden in a sub-try or except. */
- if (c->u->u_fblock[i].fb_type == FINALLY_END)
- return compiler_error(c, IN_FINALLY_ERROR_MSG);
- }
- if (i == -1)
- return compiler_error(c, LOOP_ERROR_MSG);
- ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
- break;
- case FINALLY_END:
- return compiler_error(c, IN_FINALLY_ERROR_MSG);
- }
-
- return 1;
+ static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
+ static const char IN_FINALLY_ERROR_MSG[] =
+ "'continue' not supported inside 'finally' clause";
+ int i;
+
+ if (!c->u->u_nfblocks)
+ return compiler_error(c, LOOP_ERROR_MSG);
+ i = c->u->u_nfblocks - 1;
+ switch (c->u->u_fblock[i].fb_type) {
+ case LOOP:
+ ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
+ break;
+ case EXCEPT:
+ case FINALLY_TRY:
+ while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
+ /* Prevent continue anywhere under a finally
+ even if hidden in a sub-try or except. */
+ if (c->u->u_fblock[i].fb_type == FINALLY_END)
+ return compiler_error(c, IN_FINALLY_ERROR_MSG);
+ }
+ if (i == -1)
+ return compiler_error(c, LOOP_ERROR_MSG);
+ ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
+ break;
+ case FINALLY_END:
+ return compiler_error(c, IN_FINALLY_ERROR_MSG);
+ }
+
+ return 1;
}
/* Code generated for "try: <body> finally: <finalbody>" is as follows:
-
- SETUP_FINALLY L
- <code for body>
- POP_BLOCK
- LOAD_CONST <None>
- L: <code for finalbody>
- END_FINALLY
-
+
+ SETUP_FINALLY L
+ <code for body>
+ POP_BLOCK
+ LOAD_CONST <None>
+ L: <code for finalbody>
+ END_FINALLY
+
The special instructions use the block stack. Each block
stack entry contains the instruction that created it (here
SETUP_FINALLY), the level of the value stack at the time the
block stack entry was created, and a label (here L).
-
+
SETUP_FINALLY:
- Pushes the current value stack level and the label
- onto the block stack.
+ Pushes the current value stack level and the label
+ onto the block stack.
POP_BLOCK:
- Pops en entry from the block stack, and pops the value
- stack until its level is the same as indicated on the
- block stack. (The label is ignored.)
+ Pops en entry from the block stack, and pops the value
+ stack until its level is the same as indicated on the
+ block stack. (The label is ignored.)
END_FINALLY:
- Pops a variable number of entries from the *value* stack
- and re-raises the exception they specify. The number of
- entries popped depends on the (pseudo) exception type.
-
+ Pops a variable number of entries from the *value* stack
+ and re-raises the exception they specify. The number of
+ entries popped depends on the (pseudo) exception type.
+
The block stack is unwound when an exception is raised:
when a SETUP_FINALLY entry is found, the exception is pushed
onto the value stack (and the exception condition is cleared),
@@ -1897,29 +1897,29 @@ compiler_continue(struct compiler *c)
static int
compiler_try_finally(struct compiler *c, stmt_ty s)
{
- basicblock *body, *end;
- body = compiler_new_block(c);
- end = compiler_new_block(c);
- if (body == NULL || end == NULL)
- return 0;
-
- ADDOP_JREL(c, SETUP_FINALLY, end);
- compiler_use_next_block(c, body);
- if (!compiler_push_fblock(c, FINALLY_TRY, body))
- return 0;
- VISIT_SEQ(c, stmt, s->v.TryFinally.body);
- ADDOP(c, POP_BLOCK);
- compiler_pop_fblock(c, FINALLY_TRY, body);
-
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- compiler_use_next_block(c, end);
- if (!compiler_push_fblock(c, FINALLY_END, end))
- return 0;
- VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
- ADDOP(c, END_FINALLY);
- compiler_pop_fblock(c, FINALLY_END, end);
-
- return 1;
+ basicblock *body, *end;
+ body = compiler_new_block(c);
+ end = compiler_new_block(c);
+ if (body == NULL || end == NULL)
+ return 0;
+
+ ADDOP_JREL(c, SETUP_FINALLY, end);
+ compiler_use_next_block(c, body);
+ if (!compiler_push_fblock(c, FINALLY_TRY, body))
+ return 0;
+ VISIT_SEQ(c, stmt, s->v.TryFinally.body);
+ ADDOP(c, POP_BLOCK);
+ compiler_pop_fblock(c, FINALLY_TRY, body);
+
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ compiler_use_next_block(c, end);
+ if (!compiler_push_fblock(c, FINALLY_END, end))
+ return 0;
+ VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
+ ADDOP(c, END_FINALLY);
+ compiler_pop_fblock(c, FINALLY_END, end);
+
+ return 1;
}
/*
@@ -1927,856 +1927,856 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
(The contents of the value stack is shown in [], with the top
at the right; 'tb' is trace-back info, 'val' the exception's
associated value, and 'exc' the exception.)
-
- Value stack Label Instruction Argument
- [] SETUP_EXCEPT L1
- [] <code for S>
- [] POP_BLOCK
- [] JUMP_FORWARD L0
-
- [tb, val, exc] L1: DUP )
- [tb, val, exc, exc] <evaluate E1> )
- [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
- [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
- [tb, val, exc] POP
- [tb, val] <assign to V1> (or POP if no V1)
- [tb] POP
- [] <code for S1>
- JUMP_FORWARD L0
-
- [tb, val, exc] L2: DUP
+
+ Value stack Label Instruction Argument
+ [] SETUP_EXCEPT L1
+ [] <code for S>
+ [] POP_BLOCK
+ [] JUMP_FORWARD L0
+
+ [tb, val, exc] L1: DUP )
+ [tb, val, exc, exc] <evaluate E1> )
+ [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
+ [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
+ [tb, val, exc] POP
+ [tb, val] <assign to V1> (or POP if no V1)
+ [tb] POP
+ [] <code for S1>
+ JUMP_FORWARD L0
+
+ [tb, val, exc] L2: DUP
.............................etc.......................
- [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
-
- [] L0: <next statement>
-
+ [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
+
+ [] L0: <next statement>
+
Of course, parts are not generated if Vi or Ei is not present.
*/
static int
compiler_try_except(struct compiler *c, stmt_ty s)
{
- basicblock *body, *orelse, *except, *end;
- int i, n;
-
- body = compiler_new_block(c);
- except = compiler_new_block(c);
- orelse = compiler_new_block(c);
- end = compiler_new_block(c);
- if (body == NULL || except == NULL || orelse == NULL || end == NULL)
- return 0;
- ADDOP_JREL(c, SETUP_EXCEPT, except);
- compiler_use_next_block(c, body);
- if (!compiler_push_fblock(c, EXCEPT, body))
- return 0;
- VISIT_SEQ(c, stmt, s->v.TryExcept.body);
- ADDOP(c, POP_BLOCK);
- compiler_pop_fblock(c, EXCEPT, body);
- ADDOP_JREL(c, JUMP_FORWARD, orelse);
- n = asdl_seq_LEN(s->v.TryExcept.handlers);
- compiler_use_next_block(c, except);
- for (i = 0; i < n; i++) {
- excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
- s->v.TryExcept.handlers, i);
- if (!handler->v.ExceptHandler.type && i < n-1)
- return compiler_error(c, "default 'except:' must be last");
- c->u->u_lineno_set = 0;
- c->u->u_lineno = handler->lineno;
- except = compiler_new_block(c);
- if (except == NULL)
- return 0;
- if (handler->v.ExceptHandler.type) {
- ADDOP(c, DUP_TOP);
- VISIT(c, expr, handler->v.ExceptHandler.type);
- ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
- ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
- }
- ADDOP(c, POP_TOP);
- if (handler->v.ExceptHandler.name) {
- basicblock *cleanup_end, *cleanup_body;
-
- cleanup_end = compiler_new_block(c);
- cleanup_body = compiler_new_block(c);
- if(!(cleanup_end || cleanup_body))
- return 0;
-
- compiler_nameop(c, handler->v.ExceptHandler.name, Store);
- ADDOP(c, POP_TOP);
-
- /*
- try:
- # body
- except type as name:
- try:
- # body
- finally:
- name = None
- del name
- */
-
- /* second try: */
- ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
- compiler_use_next_block(c, cleanup_body);
- if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
- return 0;
-
- /* second # body */
- VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
- ADDOP(c, POP_BLOCK);
- ADDOP(c, POP_EXCEPT);
- compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
-
- /* finally: */
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- compiler_use_next_block(c, cleanup_end);
- if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
- return 0;
-
- /* name = None */
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- compiler_nameop(c, handler->v.ExceptHandler.name, Store);
-
- /* del name */
- compiler_nameop(c, handler->v.ExceptHandler.name, Del);
-
- ADDOP(c, END_FINALLY);
- compiler_pop_fblock(c, FINALLY_END, cleanup_end);
- }
- else {
- basicblock *cleanup_body;
+ basicblock *body, *orelse, *except, *end;
+ int i, n;
+
+ body = compiler_new_block(c);
+ except = compiler_new_block(c);
+ orelse = compiler_new_block(c);
+ end = compiler_new_block(c);
+ if (body == NULL || except == NULL || orelse == NULL || end == NULL)
+ return 0;
+ ADDOP_JREL(c, SETUP_EXCEPT, except);
+ compiler_use_next_block(c, body);
+ if (!compiler_push_fblock(c, EXCEPT, body))
+ return 0;
+ VISIT_SEQ(c, stmt, s->v.TryExcept.body);
+ ADDOP(c, POP_BLOCK);
+ compiler_pop_fblock(c, EXCEPT, body);
+ ADDOP_JREL(c, JUMP_FORWARD, orelse);
+ n = asdl_seq_LEN(s->v.TryExcept.handlers);
+ compiler_use_next_block(c, except);
+ for (i = 0; i < n; i++) {
+ excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
+ s->v.TryExcept.handlers, i);
+ if (!handler->v.ExceptHandler.type && i < n-1)
+ return compiler_error(c, "default 'except:' must be last");
+ c->u->u_lineno_set = 0;
+ c->u->u_lineno = handler->lineno;
+ except = compiler_new_block(c);
+ if (except == NULL)
+ return 0;
+ if (handler->v.ExceptHandler.type) {
+ ADDOP(c, DUP_TOP);
+ VISIT(c, expr, handler->v.ExceptHandler.type);
+ ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
+ ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
+ }
+ ADDOP(c, POP_TOP);
+ if (handler->v.ExceptHandler.name) {
+ basicblock *cleanup_end, *cleanup_body;
+
+ cleanup_end = compiler_new_block(c);
+ cleanup_body = compiler_new_block(c);
+ if(!(cleanup_end || cleanup_body))
+ return 0;
+
+ compiler_nameop(c, handler->v.ExceptHandler.name, Store);
+ ADDOP(c, POP_TOP);
+
+ /*
+ try:
+ # body
+ except type as name:
+ try:
+ # body
+ finally:
+ name = None
+ del name
+ */
+
+ /* second try: */
+ ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
+ compiler_use_next_block(c, cleanup_body);
+ if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
+ return 0;
+
+ /* second # body */
+ VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
+ ADDOP(c, POP_BLOCK);
+ ADDOP(c, POP_EXCEPT);
+ compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
+
+ /* finally: */
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ compiler_use_next_block(c, cleanup_end);
+ if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
+ return 0;
+
+ /* name = None */
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ compiler_nameop(c, handler->v.ExceptHandler.name, Store);
+
+ /* del name */
+ compiler_nameop(c, handler->v.ExceptHandler.name, Del);
+
+ ADDOP(c, END_FINALLY);
+ compiler_pop_fblock(c, FINALLY_END, cleanup_end);
+ }
+ else {
+ basicblock *cleanup_body;
- cleanup_body = compiler_new_block(c);
- if(!cleanup_body)
- return 0;
+ cleanup_body = compiler_new_block(c);
+ if(!cleanup_body)
+ return 0;
- ADDOP(c, POP_TOP);
ADDOP(c, POP_TOP);
- compiler_use_next_block(c, cleanup_body);
- if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
- return 0;
- VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
- ADDOP(c, POP_EXCEPT);
- compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
- }
- ADDOP_JREL(c, JUMP_FORWARD, end);
- compiler_use_next_block(c, except);
- }
- ADDOP(c, END_FINALLY);
- compiler_use_next_block(c, orelse);
- VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
- compiler_use_next_block(c, end);
- return 1;
+ ADDOP(c, POP_TOP);
+ compiler_use_next_block(c, cleanup_body);
+ if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
+ return 0;
+ VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
+ ADDOP(c, POP_EXCEPT);
+ compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
+ }
+ ADDOP_JREL(c, JUMP_FORWARD, end);
+ compiler_use_next_block(c, except);
+ }
+ ADDOP(c, END_FINALLY);
+ compiler_use_next_block(c, orelse);
+ VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_import_as(struct compiler *c, identifier name, identifier asname)
{
- /* The IMPORT_NAME opcode was already generated. This function
- merely needs to bind the result to a name.
-
- If there is a dot in name, we need to split it and emit a
- LOAD_ATTR for each name.
- */
- const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
- const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
- if (dot) {
- /* Consume the base module name to get the first attribute */
- src = dot + 1;
- while (dot) {
- /* NB src is only defined when dot != NULL */
- PyObject *attr;
- dot = Py_UNICODE_strchr(src, '.');
- attr = PyUnicode_FromUnicode(src,
- dot ? dot - src : Py_UNICODE_strlen(src));
- if (!attr)
- return -1;
- ADDOP_O(c, LOAD_ATTR, attr, names);
- Py_DECREF(attr);
- src = dot + 1;
- }
- }
- return compiler_nameop(c, asname, Store);
+ /* The IMPORT_NAME opcode was already generated. This function
+ merely needs to bind the result to a name.
+
+ If there is a dot in name, we need to split it and emit a
+ LOAD_ATTR for each name.
+ */
+ const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
+ const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
+ if (dot) {
+ /* Consume the base module name to get the first attribute */
+ src = dot + 1;
+ while (dot) {
+ /* NB src is only defined when dot != NULL */
+ PyObject *attr;
+ dot = Py_UNICODE_strchr(src, '.');
+ attr = PyUnicode_FromUnicode(src,
+ dot ? dot - src : Py_UNICODE_strlen(src));
+ if (!attr)
+ return -1;
+ ADDOP_O(c, LOAD_ATTR, attr, names);
+ Py_DECREF(attr);
+ src = dot + 1;
+ }
+ }
+ return compiler_nameop(c, asname, Store);
}
static int
compiler_import(struct compiler *c, stmt_ty s)
{
- /* The Import node stores a module name like a.b.c as a single
- string. This is convenient for all cases except
- import a.b.c as d
- where we need to parse that string to extract the individual
- module names.
- XXX Perhaps change the representation to make this case simpler?
- */
- int i, n = asdl_seq_LEN(s->v.Import.names);
-
- for (i = 0; i < n; i++) {
- alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
- int r;
- PyObject *level;
-
- level = PyLong_FromLong(0);
- if (level == NULL)
- return 0;
-
- ADDOP_O(c, LOAD_CONST, level, consts);
- Py_DECREF(level);
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
-
- if (alias->asname) {
- r = compiler_import_as(c, alias->name, alias->asname);
- if (!r)
- return r;
- }
- else {
- identifier tmp = alias->name;
- const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
- Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
- if (dot)
- tmp = PyUnicode_FromUnicode(base,
- dot - base);
- r = compiler_nameop(c, tmp, Store);
- if (dot) {
- Py_DECREF(tmp);
- }
- if (!r)
- return r;
- }
- }
- return 1;
+ /* The Import node stores a module name like a.b.c as a single
+ string. This is convenient for all cases except
+ import a.b.c as d
+ where we need to parse that string to extract the individual
+ module names.
+ XXX Perhaps change the representation to make this case simpler?
+ */
+ int i, n = asdl_seq_LEN(s->v.Import.names);
+
+ for (i = 0; i < n; i++) {
+ alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
+ int r;
+ PyObject *level;
+
+ level = PyLong_FromLong(0);
+ if (level == NULL)
+ return 0;
+
+ ADDOP_O(c, LOAD_CONST, level, consts);
+ Py_DECREF(level);
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
+
+ if (alias->asname) {
+ r = compiler_import_as(c, alias->name, alias->asname);
+ if (!r)
+ return r;
+ }
+ else {
+ identifier tmp = alias->name;
+ const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
+ Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
+ if (dot)
+ tmp = PyUnicode_FromUnicode(base,
+ dot - base);
+ r = compiler_nameop(c, tmp, Store);
+ if (dot) {
+ Py_DECREF(tmp);
+ }
+ if (!r)
+ return r;
+ }
+ }
+ return 1;
}
static int
compiler_from_import(struct compiler *c, stmt_ty s)
{
- int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
-
- PyObject *names = PyTuple_New(n);
- PyObject *level;
-
- if (!names)
- return 0;
-
- level = PyLong_FromLong(s->v.ImportFrom.level);
- if (!level) {
- Py_DECREF(names);
- return 0;
- }
-
- /* build up the names */
- for (i = 0; i < n; i++) {
- alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
- Py_INCREF(alias->name);
- PyTuple_SET_ITEM(names, i, alias->name);
- }
-
- if (s->lineno > c->c_future->ff_lineno) {
- if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
- "__future__")) {
- Py_DECREF(level);
- Py_DECREF(names);
- return compiler_error(c,
- "from __future__ imports must occur "
- "at the beginning of the file");
-
- }
- }
-
- ADDOP_O(c, LOAD_CONST, level, consts);
- Py_DECREF(level);
- ADDOP_O(c, LOAD_CONST, names, consts);
- Py_DECREF(names);
- ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
- for (i = 0; i < n; i++) {
- alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
- identifier store_name;
-
- if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
- assert(n == 1);
- ADDOP(c, IMPORT_STAR);
- return 1;
- }
-
- ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
- store_name = alias->name;
- if (alias->asname)
- store_name = alias->asname;
-
- if (!compiler_nameop(c, store_name, Store)) {
- Py_DECREF(names);
- return 0;
- }
- }
- /* remove imported module */
- ADDOP(c, POP_TOP);
- return 1;
+ int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
+
+ PyObject *names = PyTuple_New(n);
+ PyObject *level;
+
+ if (!names)
+ return 0;
+
+ level = PyLong_FromLong(s->v.ImportFrom.level);
+ if (!level) {
+ Py_DECREF(names);
+ return 0;
+ }
+
+ /* build up the names */
+ for (i = 0; i < n; i++) {
+ alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
+ Py_INCREF(alias->name);
+ PyTuple_SET_ITEM(names, i, alias->name);
+ }
+
+ if (s->lineno > c->c_future->ff_lineno) {
+ if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
+ "__future__")) {
+ Py_DECREF(level);
+ Py_DECREF(names);
+ return compiler_error(c,
+ "from __future__ imports must occur "
+ "at the beginning of the file");
+
+ }
+ }
+
+ ADDOP_O(c, LOAD_CONST, level, consts);
+ Py_DECREF(level);
+ ADDOP_O(c, LOAD_CONST, names, consts);
+ Py_DECREF(names);
+ ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
+ for (i = 0; i < n; i++) {
+ alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
+ identifier store_name;
+
+ if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
+ assert(n == 1);
+ ADDOP(c, IMPORT_STAR);
+ return 1;
+ }
+
+ ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
+ store_name = alias->name;
+ if (alias->asname)
+ store_name = alias->asname;
+
+ if (!compiler_nameop(c, store_name, Store)) {
+ Py_DECREF(names);
+ return 0;
+ }
+ }
+ /* remove imported module */
+ ADDOP(c, POP_TOP);
+ return 1;
}
static int
compiler_assert(struct compiler *c, stmt_ty s)
{
- static PyObject *assertion_error = NULL;
- basicblock *end;
-
- if (Py_OptimizeFlag)
- return 1;
- if (assertion_error == NULL) {
- assertion_error = PyUnicode_InternFromString("AssertionError");
- if (assertion_error == NULL)
- return 0;
- }
- if (s->v.Assert.test->kind == Tuple_kind &&
- asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
- const char* msg =
- "assertion is always true, perhaps remove parentheses?";
- if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
- c->u->u_lineno, NULL, NULL) == -1)
- return 0;
- }
- VISIT(c, expr, s->v.Assert.test);
- end = compiler_new_block(c);
- if (end == NULL)
- return 0;
- ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
- ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
- if (s->v.Assert.msg) {
- VISIT(c, expr, s->v.Assert.msg);
- ADDOP_I(c, CALL_FUNCTION, 1);
- }
- ADDOP_I(c, RAISE_VARARGS, 1);
- compiler_use_next_block(c, end);
- return 1;
+ static PyObject *assertion_error = NULL;
+ basicblock *end;
+
+ if (Py_OptimizeFlag)
+ return 1;
+ if (assertion_error == NULL) {
+ assertion_error = PyUnicode_InternFromString("AssertionError");
+ if (assertion_error == NULL)
+ return 0;
+ }
+ if (s->v.Assert.test->kind == Tuple_kind &&
+ asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
+ const char* msg =
+ "assertion is always true, perhaps remove parentheses?";
+ if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
+ c->u->u_lineno, NULL, NULL) == -1)
+ return 0;
+ }
+ VISIT(c, expr, s->v.Assert.test);
+ end = compiler_new_block(c);
+ if (end == NULL)
+ return 0;
+ ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
+ ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
+ if (s->v.Assert.msg) {
+ VISIT(c, expr, s->v.Assert.msg);
+ ADDOP_I(c, CALL_FUNCTION, 1);
+ }
+ ADDOP_I(c, RAISE_VARARGS, 1);
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_visit_stmt(struct compiler *c, stmt_ty s)
{
- int i, n;
-
- /* Always assign a lineno to the next instruction for a stmt. */
- c->u->u_lineno = s->lineno;
- c->u->u_lineno_set = 0;
-
- switch (s->kind) {
- case FunctionDef_kind:
- return compiler_function(c, s);
- case ClassDef_kind:
- return compiler_class(c, s);
- case Return_kind:
- if (c->u->u_ste->ste_type != FunctionBlock)
- return compiler_error(c, "'return' outside function");
- if (s->v.Return.value) {
- VISIT(c, expr, s->v.Return.value);
- }
- else
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- ADDOP(c, RETURN_VALUE);
- break;
- case Delete_kind:
- VISIT_SEQ(c, expr, s->v.Delete.targets)
- break;
- case Assign_kind:
- n = asdl_seq_LEN(s->v.Assign.targets);
- VISIT(c, expr, s->v.Assign.value);
- for (i = 0; i < n; i++) {
- if (i < n - 1)
- ADDOP(c, DUP_TOP);
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
- }
- break;
- case AugAssign_kind:
- return compiler_augassign(c, s);
- case For_kind:
- return compiler_for(c, s);
- case While_kind:
- return compiler_while(c, s);
- case If_kind:
- return compiler_if(c, s);
- case Raise_kind:
- n = 0;
- if (s->v.Raise.exc) {
- VISIT(c, expr, s->v.Raise.exc);
- n++;
- if (s->v.Raise.cause) {
- VISIT(c, expr, s->v.Raise.cause);
- n++;
- }
- }
- ADDOP_I(c, RAISE_VARARGS, n);
- break;
- case TryExcept_kind:
- return compiler_try_except(c, s);
- case TryFinally_kind:
- return compiler_try_finally(c, s);
- case Assert_kind:
- return compiler_assert(c, s);
- case Import_kind:
- return compiler_import(c, s);
- case ImportFrom_kind:
- return compiler_from_import(c, s);
- case Global_kind:
- case Nonlocal_kind:
- break;
- case Expr_kind:
- if (c->c_interactive && c->c_nestlevel <= 1) {
- VISIT(c, expr, s->v.Expr.value);
- ADDOP(c, PRINT_EXPR);
- }
- else if (s->v.Expr.value->kind != Str_kind &&
- s->v.Expr.value->kind != Num_kind) {
- VISIT(c, expr, s->v.Expr.value);
- ADDOP(c, POP_TOP);
- }
- break;
- case Pass_kind:
- break;
- case Break_kind:
- if (!compiler_in_loop(c))
- return compiler_error(c, "'break' outside loop");
- ADDOP(c, BREAK_LOOP);
- break;
- case Continue_kind:
- return compiler_continue(c);
- case With_kind:
- return compiler_with(c, s);
- }
- return 1;
+ int i, n;
+
+ /* Always assign a lineno to the next instruction for a stmt. */
+ c->u->u_lineno = s->lineno;
+ c->u->u_lineno_set = 0;
+
+ switch (s->kind) {
+ case FunctionDef_kind:
+ return compiler_function(c, s);
+ case ClassDef_kind:
+ return compiler_class(c, s);
+ case Return_kind:
+ if (c->u->u_ste->ste_type != FunctionBlock)
+ return compiler_error(c, "'return' outside function");
+ if (s->v.Return.value) {
+ VISIT(c, expr, s->v.Return.value);
+ }
+ else
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ ADDOP(c, RETURN_VALUE);
+ break;
+ case Delete_kind:
+ VISIT_SEQ(c, expr, s->v.Delete.targets)
+ break;
+ case Assign_kind:
+ n = asdl_seq_LEN(s->v.Assign.targets);
+ VISIT(c, expr, s->v.Assign.value);
+ for (i = 0; i < n; i++) {
+ if (i < n - 1)
+ ADDOP(c, DUP_TOP);
+ VISIT(c, expr,
+ (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
+ }
+ break;
+ case AugAssign_kind:
+ return compiler_augassign(c, s);
+ case For_kind:
+ return compiler_for(c, s);
+ case While_kind:
+ return compiler_while(c, s);
+ case If_kind:
+ return compiler_if(c, s);
+ case Raise_kind:
+ n = 0;
+ if (s->v.Raise.exc) {
+ VISIT(c, expr, s->v.Raise.exc);
+ n++;
+ if (s->v.Raise.cause) {
+ VISIT(c, expr, s->v.Raise.cause);
+ n++;
+ }
+ }
+ ADDOP_I(c, RAISE_VARARGS, n);
+ break;
+ case TryExcept_kind:
+ return compiler_try_except(c, s);
+ case TryFinally_kind:
+ return compiler_try_finally(c, s);
+ case Assert_kind:
+ return compiler_assert(c, s);
+ case Import_kind:
+ return compiler_import(c, s);
+ case ImportFrom_kind:
+ return compiler_from_import(c, s);
+ case Global_kind:
+ case Nonlocal_kind:
+ break;
+ case Expr_kind:
+ if (c->c_interactive && c->c_nestlevel <= 1) {
+ VISIT(c, expr, s->v.Expr.value);
+ ADDOP(c, PRINT_EXPR);
+ }
+ else if (s->v.Expr.value->kind != Str_kind &&
+ s->v.Expr.value->kind != Num_kind) {
+ VISIT(c, expr, s->v.Expr.value);
+ ADDOP(c, POP_TOP);
+ }
+ break;
+ case Pass_kind:
+ break;
+ case Break_kind:
+ if (!compiler_in_loop(c))
+ return compiler_error(c, "'break' outside loop");
+ ADDOP(c, BREAK_LOOP);
+ break;
+ case Continue_kind:
+ return compiler_continue(c);
+ case With_kind:
+ return compiler_with(c, s);
+ }
+ return 1;
}
static int
unaryop(unaryop_ty op)
{
- switch (op) {
- case Invert:
- return UNARY_INVERT;
- case Not:
- return UNARY_NOT;
- case UAdd:
- return UNARY_POSITIVE;
- case USub:
- return UNARY_NEGATIVE;
- default:
- PyErr_Format(PyExc_SystemError,
- "unary op %d should not be possible", op);
- return 0;
- }
+ switch (op) {
+ case Invert:
+ return UNARY_INVERT;
+ case Not:
+ return UNARY_NOT;
+ case UAdd:
+ return UNARY_POSITIVE;
+ case USub:
+ return UNARY_NEGATIVE;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "unary op %d should not be possible", op);
+ return 0;
+ }
}
static int
binop(struct compiler *c, operator_ty op)
{
- switch (op) {
- case Add:
- return BINARY_ADD;
- case Sub:
- return BINARY_SUBTRACT;
- case Mult:
- return BINARY_MULTIPLY;
- case Div:
- return BINARY_TRUE_DIVIDE;
- case Mod:
- return BINARY_MODULO;
- case Pow:
- return BINARY_POWER;
- case LShift:
- return BINARY_LSHIFT;
- case RShift:
- return BINARY_RSHIFT;
- case BitOr:
- return BINARY_OR;
- case BitXor:
- return BINARY_XOR;
- case BitAnd:
- return BINARY_AND;
- case FloorDiv:
- return BINARY_FLOOR_DIVIDE;
- default:
- PyErr_Format(PyExc_SystemError,
- "binary op %d should not be possible", op);
- return 0;
- }
+ switch (op) {
+ case Add:
+ return BINARY_ADD;
+ case Sub:
+ return BINARY_SUBTRACT;
+ case Mult:
+ return BINARY_MULTIPLY;
+ case Div:
+ return BINARY_TRUE_DIVIDE;
+ case Mod:
+ return BINARY_MODULO;
+ case Pow:
+ return BINARY_POWER;
+ case LShift:
+ return BINARY_LSHIFT;
+ case RShift:
+ return BINARY_RSHIFT;
+ case BitOr:
+ return BINARY_OR;
+ case BitXor:
+ return BINARY_XOR;
+ case BitAnd:
+ return BINARY_AND;
+ case FloorDiv:
+ return BINARY_FLOOR_DIVIDE;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "binary op %d should not be possible", op);
+ return 0;
+ }
}
static int
cmpop(cmpop_ty op)
{
- switch (op) {
- case Eq:
- return PyCmp_EQ;
- case NotEq:
- return PyCmp_NE;
- case Lt:
- return PyCmp_LT;
- case LtE:
- return PyCmp_LE;
- case Gt:
- return PyCmp_GT;
- case GtE:
- return PyCmp_GE;
- case Is:
- return PyCmp_IS;
- case IsNot:
- return PyCmp_IS_NOT;
- case In:
- return PyCmp_IN;
- case NotIn:
- return PyCmp_NOT_IN;
- default:
- return PyCmp_BAD;
- }
+ switch (op) {
+ case Eq:
+ return PyCmp_EQ;
+ case NotEq:
+ return PyCmp_NE;
+ case Lt:
+ return PyCmp_LT;
+ case LtE:
+ return PyCmp_LE;
+ case Gt:
+ return PyCmp_GT;
+ case GtE:
+ return PyCmp_GE;
+ case Is:
+ return PyCmp_IS;
+ case IsNot:
+ return PyCmp_IS_NOT;
+ case In:
+ return PyCmp_IN;
+ case NotIn:
+ return PyCmp_NOT_IN;
+ default:
+ return PyCmp_BAD;
+ }
}
static int
inplace_binop(struct compiler *c, operator_ty op)
{
- switch (op) {
- case Add:
- return INPLACE_ADD;
- case Sub:
- return INPLACE_SUBTRACT;
- case Mult:
- return INPLACE_MULTIPLY;
- case Div:
- return INPLACE_TRUE_DIVIDE;
- case Mod:
- return INPLACE_MODULO;
- case Pow:
- return INPLACE_POWER;
- case LShift:
- return INPLACE_LSHIFT;
- case RShift:
- return INPLACE_RSHIFT;
- case BitOr:
- return INPLACE_OR;
- case BitXor:
- return INPLACE_XOR;
- case BitAnd:
- return INPLACE_AND;
- case FloorDiv:
- return INPLACE_FLOOR_DIVIDE;
- default:
- PyErr_Format(PyExc_SystemError,
- "inplace binary op %d should not be possible", op);
- return 0;
- }
+ switch (op) {
+ case Add:
+ return INPLACE_ADD;
+ case Sub:
+ return INPLACE_SUBTRACT;
+ case Mult:
+ return INPLACE_MULTIPLY;
+ case Div:
+ return INPLACE_TRUE_DIVIDE;
+ case Mod:
+ return INPLACE_MODULO;
+ case Pow:
+ return INPLACE_POWER;
+ case LShift:
+ return INPLACE_LSHIFT;
+ case RShift:
+ return INPLACE_RSHIFT;
+ case BitOr:
+ return INPLACE_OR;
+ case BitXor:
+ return INPLACE_XOR;
+ case BitAnd:
+ return INPLACE_AND;
+ case FloorDiv:
+ return INPLACE_FLOOR_DIVIDE;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "inplace binary op %d should not be possible", op);
+ return 0;
+ }
}
static int
compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
{
- int op, scope, arg;
- enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
-
- PyObject *dict = c->u->u_names;
- PyObject *mangled;
- /* XXX AugStore isn't used anywhere! */
-
- mangled = _Py_Mangle(c->u->u_private, name);
- if (!mangled)
- return 0;
-
- op = 0;
- optype = OP_NAME;
- scope = PyST_GetScope(c->u->u_ste, mangled);
- switch (scope) {
- case FREE:
- dict = c->u->u_freevars;
- optype = OP_DEREF;
- break;
- case CELL:
- dict = c->u->u_cellvars;
- optype = OP_DEREF;
- break;
- case LOCAL:
- if (c->u->u_ste->ste_type == FunctionBlock)
- optype = OP_FAST;
- break;
- case GLOBAL_IMPLICIT:
- if (c->u->u_ste->ste_type == FunctionBlock &&
- !c->u->u_ste->ste_unoptimized)
- optype = OP_GLOBAL;
- break;
- case GLOBAL_EXPLICIT:
- optype = OP_GLOBAL;
- break;
- default:
- /* scope can be 0 */
- break;
- }
-
- /* XXX Leave assert here, but handle __doc__ and the like better */
- assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
-
- switch (optype) {
- case OP_DEREF:
- switch (ctx) {
- case Load: op = LOAD_DEREF; break;
- case Store: op = STORE_DEREF; break;
- case AugLoad:
- case AugStore:
- break;
- case Del:
- PyErr_Format(PyExc_SyntaxError,
- "can not delete variable '%S' referenced "
- "in nested scope",
- name);
- Py_DECREF(mangled);
- return 0;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid for deref variable");
- return 0;
- }
- break;
- case OP_FAST:
- switch (ctx) {
- case Load: op = LOAD_FAST; break;
- case Store: op = STORE_FAST; break;
- case Del: op = DELETE_FAST; break;
- case AugLoad:
- case AugStore:
- break;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid for local variable");
- return 0;
- }
- ADDOP_O(c, op, mangled, varnames);
- Py_DECREF(mangled);
- return 1;
- case OP_GLOBAL:
- switch (ctx) {
- case Load: op = LOAD_GLOBAL; break;
- case Store: op = STORE_GLOBAL; break;
- case Del: op = DELETE_GLOBAL; break;
- case AugLoad:
- case AugStore:
- break;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid for global variable");
- return 0;
- }
- break;
- case OP_NAME:
- switch (ctx) {
- case Load: op = LOAD_NAME; break;
- case Store: op = STORE_NAME; break;
- case Del: op = DELETE_NAME; break;
- case AugLoad:
- case AugStore:
- break;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid for name variable");
- return 0;
- }
- break;
- }
-
- assert(op);
- arg = compiler_add_o(c, dict, mangled);
- Py_DECREF(mangled);
- if (arg < 0)
- return 0;
- return compiler_addop_i(c, op, arg);
+ int op, scope, arg;
+ enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
+
+ PyObject *dict = c->u->u_names;
+ PyObject *mangled;
+ /* XXX AugStore isn't used anywhere! */
+
+ mangled = _Py_Mangle(c->u->u_private, name);
+ if (!mangled)
+ return 0;
+
+ op = 0;
+ optype = OP_NAME;
+ scope = PyST_GetScope(c->u->u_ste, mangled);
+ switch (scope) {
+ case FREE:
+ dict = c->u->u_freevars;
+ optype = OP_DEREF;
+ break;
+ case CELL:
+ dict = c->u->u_cellvars;
+ optype = OP_DEREF;
+ break;
+ case LOCAL:
+ if (c->u->u_ste->ste_type == FunctionBlock)
+ optype = OP_FAST;
+ break;
+ case GLOBAL_IMPLICIT:
+ if (c->u->u_ste->ste_type == FunctionBlock &&
+ !c->u->u_ste->ste_unoptimized)
+ optype = OP_GLOBAL;
+ break;
+ case GLOBAL_EXPLICIT:
+ optype = OP_GLOBAL;
+ break;
+ default:
+ /* scope can be 0 */
+ break;
+ }
+
+ /* XXX Leave assert here, but handle __doc__ and the like better */
+ assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
+
+ switch (optype) {
+ case OP_DEREF:
+ switch (ctx) {
+ case Load: op = LOAD_DEREF; break;
+ case Store: op = STORE_DEREF; break;
+ case AugLoad:
+ case AugStore:
+ break;
+ case Del:
+ PyErr_Format(PyExc_SyntaxError,
+ "can not delete variable '%S' referenced "
+ "in nested scope",
+ name);
+ Py_DECREF(mangled);
+ return 0;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid for deref variable");
+ return 0;
+ }
+ break;
+ case OP_FAST:
+ switch (ctx) {
+ case Load: op = LOAD_FAST; break;
+ case Store: op = STORE_FAST; break;
+ case Del: op = DELETE_FAST; break;
+ case AugLoad:
+ case AugStore:
+ break;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid for local variable");
+ return 0;
+ }
+ ADDOP_O(c, op, mangled, varnames);
+ Py_DECREF(mangled);
+ return 1;
+ case OP_GLOBAL:
+ switch (ctx) {
+ case Load: op = LOAD_GLOBAL; break;
+ case Store: op = STORE_GLOBAL; break;
+ case Del: op = DELETE_GLOBAL; break;
+ case AugLoad:
+ case AugStore:
+ break;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid for global variable");
+ return 0;
+ }
+ break;
+ case OP_NAME:
+ switch (ctx) {
+ case Load: op = LOAD_NAME; break;
+ case Store: op = STORE_NAME; break;
+ case Del: op = DELETE_NAME; break;
+ case AugLoad:
+ case AugStore:
+ break;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid for name variable");
+ return 0;
+ }
+ break;
+ }
+
+ assert(op);
+ arg = compiler_add_o(c, dict, mangled);
+ Py_DECREF(mangled);
+ if (arg < 0)
+ return 0;
+ return compiler_addop_i(c, op, arg);
}
static int
compiler_boolop(struct compiler *c, expr_ty e)
{
- basicblock *end;
- int jumpi, i, n;
- asdl_seq *s;
-
- assert(e->kind == BoolOp_kind);
- if (e->v.BoolOp.op == And)
- jumpi = JUMP_IF_FALSE_OR_POP;
- else
- jumpi = JUMP_IF_TRUE_OR_POP;
- end = compiler_new_block(c);
- if (end == NULL)
- return 0;
- s = e->v.BoolOp.values;
- n = asdl_seq_LEN(s) - 1;
- assert(n >= 0);
- for (i = 0; i < n; ++i) {
- VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
- ADDOP_JABS(c, jumpi, end);
- }
- VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
- compiler_use_next_block(c, end);
- return 1;
+ basicblock *end;
+ int jumpi, i, n;
+ asdl_seq *s;
+
+ assert(e->kind == BoolOp_kind);
+ if (e->v.BoolOp.op == And)
+ jumpi = JUMP_IF_FALSE_OR_POP;
+ else
+ jumpi = JUMP_IF_TRUE_OR_POP;
+ end = compiler_new_block(c);
+ if (end == NULL)
+ return 0;
+ s = e->v.BoolOp.values;
+ n = asdl_seq_LEN(s) - 1;
+ assert(n >= 0);
+ for (i = 0; i < n; ++i) {
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
+ ADDOP_JABS(c, jumpi, end);
+ }
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
+ compiler_use_next_block(c, end);
+ return 1;
}
static int
compiler_list(struct compiler *c, expr_ty e)
{
- int n = asdl_seq_LEN(e->v.List.elts);
- if (e->v.List.ctx == Store) {
- int i, seen_star = 0;
- for (i = 0; i < n; i++) {
- expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
- if (elt->kind == Starred_kind && !seen_star) {
- if ((i >= (1 << 8)) ||
- (n-i-1 >= (INT_MAX >> 8)))
- return compiler_error(c,
- "too many expressions in "
- "star-unpacking assignment");
- ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
- seen_star = 1;
- asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
- } else if (elt->kind == Starred_kind) {
- return compiler_error(c,
- "two starred expressions in assignment");
- }
- }
- if (!seen_star) {
- ADDOP_I(c, UNPACK_SEQUENCE, n);
- }
- }
- VISIT_SEQ(c, expr, e->v.List.elts);
- if (e->v.List.ctx == Load) {
- ADDOP_I(c, BUILD_LIST, n);
- }
- return 1;
+ int n = asdl_seq_LEN(e->v.List.elts);
+ if (e->v.List.ctx == Store) {
+ int i, seen_star = 0;
+ for (i = 0; i < n; i++) {
+ expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
+ if (elt->kind == Starred_kind && !seen_star) {
+ if ((i >= (1 << 8)) ||
+ (n-i-1 >= (INT_MAX >> 8)))
+ return compiler_error(c,
+ "too many expressions in "
+ "star-unpacking assignment");
+ ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
+ seen_star = 1;
+ asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
+ } else if (elt->kind == Starred_kind) {
+ return compiler_error(c,
+ "two starred expressions in assignment");
+ }
+ }
+ if (!seen_star) {
+ ADDOP_I(c, UNPACK_SEQUENCE, n);
+ }
+ }
+ VISIT_SEQ(c, expr, e->v.List.elts);
+ if (e->v.List.ctx == Load) {
+ ADDOP_I(c, BUILD_LIST, n);
+ }
+ return 1;
}
static int
compiler_tuple(struct compiler *c, expr_ty e)
{
- int n = asdl_seq_LEN(e->v.Tuple.elts);
- if (e->v.Tuple.ctx == Store) {
- int i, seen_star = 0;
- for (i = 0; i < n; i++) {
- expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
- if (elt->kind == Starred_kind && !seen_star) {
- if ((i >= (1 << 8)) ||
- (n-i-1 >= (INT_MAX >> 8)))
- return compiler_error(c,
- "too many expressions in "
- "star-unpacking assignment");
- ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
- seen_star = 1;
- asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
- } else if (elt->kind == Starred_kind) {
- return compiler_error(c,
- "two starred expressions in assignment");
- }
- }
- if (!seen_star) {
- ADDOP_I(c, UNPACK_SEQUENCE, n);
- }
- }
- VISIT_SEQ(c, expr, e->v.Tuple.elts);
- if (e->v.Tuple.ctx == Load) {
- ADDOP_I(c, BUILD_TUPLE, n);
- }
- return 1;
+ int n = asdl_seq_LEN(e->v.Tuple.elts);
+ if (e->v.Tuple.ctx == Store) {
+ int i, seen_star = 0;
+ for (i = 0; i < n; i++) {
+ expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
+ if (elt->kind == Starred_kind && !seen_star) {
+ if ((i >= (1 << 8)) ||
+ (n-i-1 >= (INT_MAX >> 8)))
+ return compiler_error(c,
+ "too many expressions in "
+ "star-unpacking assignment");
+ ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
+ seen_star = 1;
+ asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
+ } else if (elt->kind == Starred_kind) {
+ return compiler_error(c,
+ "two starred expressions in assignment");
+ }
+ }
+ if (!seen_star) {
+ ADDOP_I(c, UNPACK_SEQUENCE, n);
+ }
+ }
+ VISIT_SEQ(c, expr, e->v.Tuple.elts);
+ if (e->v.Tuple.ctx == Load) {
+ ADDOP_I(c, BUILD_TUPLE, n);
+ }
+ return 1;
}
static int
compiler_compare(struct compiler *c, expr_ty e)
{
- int i, n;
- basicblock *cleanup = NULL;
-
- /* XXX the logic can be cleaned up for 1 or multiple comparisons */
- VISIT(c, expr, e->v.Compare.left);
- n = asdl_seq_LEN(e->v.Compare.ops);
- assert(n > 0);
- if (n > 1) {
- cleanup = compiler_new_block(c);
- if (cleanup == NULL)
- return 0;
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
- }
- for (i = 1; i < n; i++) {
- ADDOP(c, DUP_TOP);
- ADDOP(c, ROT_THREE);
- ADDOP_I(c, COMPARE_OP,
- cmpop((cmpop_ty)(asdl_seq_GET(
- e->v.Compare.ops, i - 1))));
- ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
- NEXT_BLOCK(c);
- if (i < (n - 1))
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
- }
- VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
- ADDOP_I(c, COMPARE_OP,
- cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
- if (n > 1) {
- basicblock *end = compiler_new_block(c);
- if (end == NULL)
- return 0;
- ADDOP_JREL(c, JUMP_FORWARD, end);
- compiler_use_next_block(c, cleanup);
- ADDOP(c, ROT_TWO);
- ADDOP(c, POP_TOP);
- compiler_use_next_block(c, end);
- }
- return 1;
+ int i, n;
+ basicblock *cleanup = NULL;
+
+ /* XXX the logic can be cleaned up for 1 or multiple comparisons */
+ VISIT(c, expr, e->v.Compare.left);
+ n = asdl_seq_LEN(e->v.Compare.ops);
+ assert(n > 0);
+ if (n > 1) {
+ cleanup = compiler_new_block(c);
+ if (cleanup == NULL)
+ return 0;
+ VISIT(c, expr,
+ (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
+ }
+ for (i = 1; i < n; i++) {
+ ADDOP(c, DUP_TOP);
+ ADDOP(c, ROT_THREE);
+ ADDOP_I(c, COMPARE_OP,
+ cmpop((cmpop_ty)(asdl_seq_GET(
+ e->v.Compare.ops, i - 1))));
+ ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
+ NEXT_BLOCK(c);
+ if (i < (n - 1))
+ VISIT(c, expr,
+ (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
+ }
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
+ ADDOP_I(c, COMPARE_OP,
+ cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
+ if (n > 1) {
+ basicblock *end = compiler_new_block(c);
+ if (end == NULL)
+ return 0;
+ ADDOP_JREL(c, JUMP_FORWARD, end);
+ compiler_use_next_block(c, cleanup);
+ ADDOP(c, ROT_TWO);
+ ADDOP(c, POP_TOP);
+ compiler_use_next_block(c, end);
+ }
+ return 1;
}
static int
compiler_call(struct compiler *c, expr_ty e)
{
- VISIT(c, expr, e->v.Call.func);
- return compiler_call_helper(c, 0,
- e->v.Call.args,
- e->v.Call.keywords,
- e->v.Call.starargs,
- e->v.Call.kwargs);
+ VISIT(c, expr, e->v.Call.func);
+ return compiler_call_helper(c, 0,
+ e->v.Call.args,
+ e->v.Call.keywords,
+ e->v.Call.starargs,
+ e->v.Call.kwargs);
}
/* shared code between compiler_call and compiler_class */
static int
compiler_call_helper(struct compiler *c,
- int n, /* Args already pushed */
- asdl_seq *args,
- asdl_seq *keywords,
- expr_ty starargs,
- expr_ty kwargs)
+ int n, /* Args already pushed */
+ asdl_seq *args,
+ asdl_seq *keywords,
+ expr_ty starargs,
+ expr_ty kwargs)
{
- int code = 0;
-
- n += asdl_seq_LEN(args);
- VISIT_SEQ(c, expr, args);
- if (keywords) {
- VISIT_SEQ(c, keyword, keywords);
- n |= asdl_seq_LEN(keywords) << 8;
- }
- if (starargs) {
- VISIT(c, expr, starargs);
- code |= 1;
- }
- if (kwargs) {
- VISIT(c, expr, kwargs);
- code |= 2;
- }
- switch (code) {
- case 0:
- ADDOP_I(c, CALL_FUNCTION, n);
- break;
- case 1:
- ADDOP_I(c, CALL_FUNCTION_VAR, n);
- break;
- case 2:
- ADDOP_I(c, CALL_FUNCTION_KW, n);
- break;
- case 3:
- ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
- break;
- }
- return 1;
+ int code = 0;
+
+ n += asdl_seq_LEN(args);
+ VISIT_SEQ(c, expr, args);
+ if (keywords) {
+ VISIT_SEQ(c, keyword, keywords);
+ n |= asdl_seq_LEN(keywords) << 8;
+ }
+ if (starargs) {
+ VISIT(c, expr, starargs);
+ code |= 1;
+ }
+ if (kwargs) {
+ VISIT(c, expr, kwargs);
+ code |= 2;
+ }
+ switch (code) {
+ case 0:
+ ADDOP_I(c, CALL_FUNCTION, n);
+ break;
+ case 1:
+ ADDOP_I(c, CALL_FUNCTION_VAR, n);
+ break;
+ case 2:
+ ADDOP_I(c, CALL_FUNCTION_KW, n);
+ break;
+ case 3:
+ ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
+ break;
+ }
+ return 1;
}
@@ -2796,228 +2796,228 @@ compiler_call_helper(struct compiler *c,
*/
static int
-compiler_comprehension_generator(struct compiler *c,
- asdl_seq *generators, int gen_index,
- expr_ty elt, expr_ty val, int type)
+compiler_comprehension_generator(struct compiler *c,
+ asdl_seq *generators, int gen_index,
+ expr_ty elt, expr_ty val, int type)
{
- /* generate code for the iterator, then each of the ifs,
- and then write to the element */
-
- comprehension_ty gen;
- basicblock *start, *anchor, *skip, *if_cleanup;
- int i, n;
-
- start = compiler_new_block(c);
- skip = compiler_new_block(c);
- if_cleanup = compiler_new_block(c);
- anchor = compiler_new_block(c);
-
- if (start == NULL || skip == NULL || if_cleanup == NULL ||
- anchor == NULL)
- return 0;
-
- gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
-
- if (gen_index == 0) {
- /* Receive outermost iter as an implicit argument */
- c->u->u_argcount = 1;
- ADDOP_I(c, LOAD_FAST, 0);
- }
- else {
- /* Sub-iter - calculate on the fly */
- VISIT(c, expr, gen->iter);
- ADDOP(c, GET_ITER);
- }
- compiler_use_next_block(c, start);
- ADDOP_JREL(c, FOR_ITER, anchor);
- NEXT_BLOCK(c);
- VISIT(c, expr, gen->target);
-
- /* XXX this needs to be cleaned up...a lot! */
- n = asdl_seq_LEN(gen->ifs);
- for (i = 0; i < n; i++) {
- expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
- VISIT(c, expr, e);
- ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
- NEXT_BLOCK(c);
- }
-
- if (++gen_index < asdl_seq_LEN(generators))
- if (!compiler_comprehension_generator(c,
- generators, gen_index,
- elt, val, type))
- return 0;
-
- /* only append after the last for generator */
- if (gen_index >= asdl_seq_LEN(generators)) {
- /* comprehension specific code */
- switch (type) {
- case COMP_GENEXP:
- VISIT(c, expr, elt);
- ADDOP(c, YIELD_VALUE);
- ADDOP(c, POP_TOP);
- break;
- case COMP_LISTCOMP:
- VISIT(c, expr, elt);
- ADDOP_I(c, LIST_APPEND, gen_index + 1);
- break;
- case COMP_SETCOMP:
- VISIT(c, expr, elt);
- ADDOP_I(c, SET_ADD, gen_index + 1);
- break;
- case COMP_DICTCOMP:
- /* With 'd[k] = v', v is evaluated before k, so we do
- the same. */
- VISIT(c, expr, val);
- VISIT(c, expr, elt);
- ADDOP_I(c, MAP_ADD, gen_index + 1);
- break;
- default:
- return 0;
- }
-
- compiler_use_next_block(c, skip);
- }
- compiler_use_next_block(c, if_cleanup);
- ADDOP_JABS(c, JUMP_ABSOLUTE, start);
- compiler_use_next_block(c, anchor);
-
- return 1;
+ /* generate code for the iterator, then each of the ifs,
+ and then write to the element */
+
+ comprehension_ty gen;
+ basicblock *start, *anchor, *skip, *if_cleanup;
+ int i, n;
+
+ start = compiler_new_block(c);
+ skip = compiler_new_block(c);
+ if_cleanup = compiler_new_block(c);
+ anchor = compiler_new_block(c);
+
+ if (start == NULL || skip == NULL || if_cleanup == NULL ||
+ anchor == NULL)
+ return 0;
+
+ gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
+
+ if (gen_index == 0) {
+ /* Receive outermost iter as an implicit argument */
+ c->u->u_argcount = 1;
+ ADDOP_I(c, LOAD_FAST, 0);
+ }
+ else {
+ /* Sub-iter - calculate on the fly */
+ VISIT(c, expr, gen->iter);
+ ADDOP(c, GET_ITER);
+ }
+ compiler_use_next_block(c, start);
+ ADDOP_JREL(c, FOR_ITER, anchor);
+ NEXT_BLOCK(c);
+ VISIT(c, expr, gen->target);
+
+ /* XXX this needs to be cleaned up...a lot! */
+ n = asdl_seq_LEN(gen->ifs);
+ for (i = 0; i < n; i++) {
+ expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
+ VISIT(c, expr, e);
+ ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
+ NEXT_BLOCK(c);
+ }
+
+ if (++gen_index < asdl_seq_LEN(generators))
+ if (!compiler_comprehension_generator(c,
+ generators, gen_index,
+ elt, val, type))
+ return 0;
+
+ /* only append after the last for generator */
+ if (gen_index >= asdl_seq_LEN(generators)) {
+ /* comprehension specific code */
+ switch (type) {
+ case COMP_GENEXP:
+ VISIT(c, expr, elt);
+ ADDOP(c, YIELD_VALUE);
+ ADDOP(c, POP_TOP);
+ break;
+ case COMP_LISTCOMP:
+ VISIT(c, expr, elt);
+ ADDOP_I(c, LIST_APPEND, gen_index + 1);
+ break;
+ case COMP_SETCOMP:
+ VISIT(c, expr, elt);
+ ADDOP_I(c, SET_ADD, gen_index + 1);
+ break;
+ case COMP_DICTCOMP:
+ /* With 'd[k] = v', v is evaluated before k, so we do
+ the same. */
+ VISIT(c, expr, val);
+ VISIT(c, expr, elt);
+ ADDOP_I(c, MAP_ADD, gen_index + 1);
+ break;
+ default:
+ return 0;
+ }
+
+ compiler_use_next_block(c, skip);
+ }
+ compiler_use_next_block(c, if_cleanup);
+ ADDOP_JABS(c, JUMP_ABSOLUTE, start);
+ compiler_use_next_block(c, anchor);
+
+ return 1;
}
static int
compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
- asdl_seq *generators, expr_ty elt, expr_ty val)
+ asdl_seq *generators, expr_ty elt, expr_ty val)
{
- PyCodeObject *co = NULL;
- expr_ty outermost_iter;
-
- outermost_iter = ((comprehension_ty)
- asdl_seq_GET(generators, 0))->iter;
-
- if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
- goto error;
-
- if (type != COMP_GENEXP) {
- int op;
- switch (type) {
- case COMP_LISTCOMP:
- op = BUILD_LIST;
- break;
- case COMP_SETCOMP:
- op = BUILD_SET;
- break;
- case COMP_DICTCOMP:
- op = BUILD_MAP;
- break;
- default:
- PyErr_Format(PyExc_SystemError,
- "unknown comprehension type %d", type);
- goto error_in_scope;
- }
-
- ADDOP_I(c, op, 0);
- }
-
- if (!compiler_comprehension_generator(c, generators, 0, elt,
- val, type))
- goto error_in_scope;
-
- if (type != COMP_GENEXP) {
- ADDOP(c, RETURN_VALUE);
- }
-
- co = assemble(c, 1);
- compiler_exit_scope(c);
- if (co == NULL)
- goto error;
-
- if (!compiler_make_closure(c, co, 0))
- goto error;
- Py_DECREF(co);
-
- VISIT(c, expr, outermost_iter);
- ADDOP(c, GET_ITER);
- ADDOP_I(c, CALL_FUNCTION, 1);
- return 1;
+ PyCodeObject *co = NULL;
+ expr_ty outermost_iter;
+
+ outermost_iter = ((comprehension_ty)
+ asdl_seq_GET(generators, 0))->iter;
+
+ if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
+ goto error;
+
+ if (type != COMP_GENEXP) {
+ int op;
+ switch (type) {
+ case COMP_LISTCOMP:
+ op = BUILD_LIST;
+ break;
+ case COMP_SETCOMP:
+ op = BUILD_SET;
+ break;
+ case COMP_DICTCOMP:
+ op = BUILD_MAP;
+ break;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "unknown comprehension type %d", type);
+ goto error_in_scope;
+ }
+
+ ADDOP_I(c, op, 0);
+ }
+
+ if (!compiler_comprehension_generator(c, generators, 0, elt,
+ val, type))
+ goto error_in_scope;
+
+ if (type != COMP_GENEXP) {
+ ADDOP(c, RETURN_VALUE);
+ }
+
+ co = assemble(c, 1);
+ compiler_exit_scope(c);
+ if (co == NULL)
+ goto error;
+
+ if (!compiler_make_closure(c, co, 0))
+ goto error;
+ Py_DECREF(co);
+
+ VISIT(c, expr, outermost_iter);
+ ADDOP(c, GET_ITER);
+ ADDOP_I(c, CALL_FUNCTION, 1);
+ return 1;
error_in_scope:
- compiler_exit_scope(c);
+ compiler_exit_scope(c);
error:
- Py_XDECREF(co);
- return 0;
+ Py_XDECREF(co);
+ return 0;
}
static int
compiler_genexp(struct compiler *c, expr_ty e)
{
- static identifier name;
- if (!name) {
- name = PyUnicode_FromString("<genexpr>");
- if (!name)
- return 0;
- }
- assert(e->kind == GeneratorExp_kind);
- return compiler_comprehension(c, e, COMP_GENEXP, name,
- e->v.GeneratorExp.generators,
- e->v.GeneratorExp.elt, NULL);
+ static identifier name;
+ if (!name) {
+ name = PyUnicode_FromString("<genexpr>");
+ if (!name)
+ return 0;
+ }
+ assert(e->kind == GeneratorExp_kind);
+ return compiler_comprehension(c, e, COMP_GENEXP, name,
+ e->v.GeneratorExp.generators,
+ e->v.GeneratorExp.elt, NULL);
}
static int
compiler_listcomp(struct compiler *c, expr_ty e)
{
- static identifier name;
- if (!name) {
- name = PyUnicode_FromString("<listcomp>");
- if (!name)
- return 0;
- }
- assert(e->kind == ListComp_kind);
- return compiler_comprehension(c, e, COMP_LISTCOMP, name,
- e->v.ListComp.generators,
- e->v.ListComp.elt, NULL);
+ static identifier name;
+ if (!name) {
+ name = PyUnicode_FromString("<listcomp>");
+ if (!name)
+ return 0;
+ }
+ assert(e->kind == ListComp_kind);
+ return compiler_comprehension(c, e, COMP_LISTCOMP, name,
+ e->v.ListComp.generators,
+ e->v.ListComp.elt, NULL);
}
static int
compiler_setcomp(struct compiler *c, expr_ty e)
{
- static identifier name;
- if (!name) {
- name = PyUnicode_FromString("<setcomp>");
- if (!name)
- return 0;
- }
- assert(e->kind == SetComp_kind);
- return compiler_comprehension(c, e, COMP_SETCOMP, name,
- e->v.SetComp.generators,
- e->v.SetComp.elt, NULL);
+ static identifier name;
+ if (!name) {
+ name = PyUnicode_FromString("<setcomp>");
+ if (!name)
+ return 0;
+ }
+ assert(e->kind == SetComp_kind);
+ return compiler_comprehension(c, e, COMP_SETCOMP, name,
+ e->v.SetComp.generators,
+ e->v.SetComp.elt, NULL);
}
static int
compiler_dictcomp(struct compiler *c, expr_ty e)
{
- static identifier name;
- if (!name) {
- name = PyUnicode_FromString("<dictcomp>");
- if (!name)
- return 0;
- }
- assert(e->kind == DictComp_kind);
- return compiler_comprehension(c, e, COMP_DICTCOMP, name,
- e->v.DictComp.generators,
- e->v.DictComp.key, e->v.DictComp.value);
+ static identifier name;
+ if (!name) {
+ name = PyUnicode_FromString("<dictcomp>");
+ if (!name)
+ return 0;
+ }
+ assert(e->kind == DictComp_kind);
+ return compiler_comprehension(c, e, COMP_DICTCOMP, name,
+ e->v.DictComp.generators,
+ e->v.DictComp.key, e->v.DictComp.value);
}
static int
compiler_visit_keyword(struct compiler *c, keyword_ty k)
{
- ADDOP_O(c, LOAD_CONST, k->arg, consts);
- VISIT(c, expr, k->value);
- return 1;
+ ADDOP_O(c, LOAD_CONST, k->arg, consts);
+ VISIT(c, expr, k->value);
+ return 1;
}
-/* Test whether expression is constant. For constants, report
+/* Test whether expression is constant. For constants, report
whether they are true or false.
Return values: 1 for true, 0 for false, -1 for non-constant.
@@ -3026,39 +3026,39 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
static int
expr_constant(expr_ty e)
{
- char *id;
- switch (e->kind) {
- case Ellipsis_kind:
- return 1;
- case Num_kind:
- return PyObject_IsTrue(e->v.Num.n);
- case Str_kind:
- return PyObject_IsTrue(e->v.Str.s);
- case Name_kind:
- /* optimize away names that can't be reassigned */
- id = PyBytes_AS_STRING(
- _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
- if (strcmp(id, "True") == 0) return 1;
- if (strcmp(id, "False") == 0) return 0;
- if (strcmp(id, "None") == 0) return 0;
- if (strcmp(id, "__debug__") == 0)
- return ! Py_OptimizeFlag;
- /* fall through */
- default:
- return -1;
- }
+ char *id;
+ switch (e->kind) {
+ case Ellipsis_kind:
+ return 1;
+ case Num_kind:
+ return PyObject_IsTrue(e->v.Num.n);
+ case Str_kind:
+ return PyObject_IsTrue(e->v.Str.s);
+ case Name_kind:
+ /* optimize away names that can't be reassigned */
+ id = PyBytes_AS_STRING(
+ _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
+ if (strcmp(id, "True") == 0) return 1;
+ if (strcmp(id, "False") == 0) return 0;
+ if (strcmp(id, "None") == 0) return 0;
+ if (strcmp(id, "__debug__") == 0)
+ return ! Py_OptimizeFlag;
+ /* fall through */
+ default:
+ return -1;
+ }
}
/*
Implements the with statement from PEP 343.
- The semantics outlined in that PEP are as follows:
+ The semantics outlined in that PEP are as follows:
with EXPR as VAR:
BLOCK
-
+
It is implemented roughly as:
-
+
context = EXPR
exit = context.__exit__ # not calling it
value = context.__enter__()
@@ -3067,9 +3067,9 @@ expr_constant(expr_ty e)
BLOCK
finally:
if an exception was raised:
- exc = copy of (exception, instance, traceback)
+ exc = copy of (exception, instance, traceback)
else:
- exc = (None, None, None)
+ exc = (None, None, None)
exit(*exc)
*/
static int
@@ -3082,40 +3082,40 @@ compiler_with(struct compiler *c, stmt_ty s)
assert(s->kind == With_kind);
if (!enter_attr) {
- enter_attr = PyUnicode_InternFromString("__enter__");
- if (!enter_attr)
- return 0;
+ enter_attr = PyUnicode_InternFromString("__enter__");
+ if (!enter_attr)
+ return 0;
}
if (!exit_attr) {
- exit_attr = PyUnicode_InternFromString("__exit__");
- if (!exit_attr)
- return 0;
+ exit_attr = PyUnicode_InternFromString("__exit__");
+ if (!exit_attr)
+ return 0;
}
block = compiler_new_block(c);
finally = compiler_new_block(c);
if (!block || !finally)
- return 0;
+ return 0;
if (s->v.With.optional_vars) {
- /* Create a temporary variable to hold context.__enter__().
- We need to do this rather than preserving it on the stack
- because SETUP_FINALLY remembers the stack level.
- We need to do the assignment *inside* the try/finally
- so that context.__exit__() is called when the assignment
- fails. But we need to call context.__enter__() *before*
- the try/finally so that if it fails we won't call
- context.__exit__().
- */
- tmpvalue = compiler_new_tmpname(c);
- if (tmpvalue == NULL)
- return 0;
- PyArena_AddPyObject(c->c_arena, tmpvalue);
- }
- tmpexit = compiler_new_tmpname(c);
- if (tmpexit == NULL)
- return 0;
- PyArena_AddPyObject(c->c_arena, tmpexit);
+ /* Create a temporary variable to hold context.__enter__().
+ We need to do this rather than preserving it on the stack
+ because SETUP_FINALLY remembers the stack level.
+ We need to do the assignment *inside* the try/finally
+ so that context.__exit__() is called when the assignment
+ fails. But we need to call context.__enter__() *before*
+ the try/finally so that if it fails we won't call
+ context.__exit__().
+ */
+ tmpvalue = compiler_new_tmpname(c);
+ if (tmpvalue == NULL)
+ return 0;
+ PyArena_AddPyObject(c->c_arena, tmpvalue);
+ }
+ tmpexit = compiler_new_tmpname(c);
+ if (tmpexit == NULL)
+ return 0;
+ PyArena_AddPyObject(c->c_arena, tmpexit);
/* Evaluate EXPR */
VISIT(c, expr, s->v.With.context_expr);
@@ -3123,21 +3123,21 @@ compiler_with(struct compiler *c, stmt_ty s)
/* Squirrel away context.__exit__ by stuffing it under context */
ADDOP(c, DUP_TOP);
ADDOP_O(c, LOAD_ATTR, exit_attr, names);
- if (!compiler_nameop(c, tmpexit, Store))
- return 0;
+ if (!compiler_nameop(c, tmpexit, Store))
+ return 0;
/* Call context.__enter__() */
ADDOP_O(c, LOAD_ATTR, enter_attr, names);
ADDOP_I(c, CALL_FUNCTION, 0);
if (s->v.With.optional_vars) {
- /* Store it in tmpvalue */
- if (!compiler_nameop(c, tmpvalue, Store))
- return 0;
+ /* Store it in tmpvalue */
+ if (!compiler_nameop(c, tmpvalue, Store))
+ return 0;
}
else {
- /* Discard result from context.__enter__() */
- ADDOP(c, POP_TOP);
+ /* Discard result from context.__enter__() */
+ ADDOP(c, POP_TOP);
}
/* Start the try block */
@@ -3145,15 +3145,15 @@ compiler_with(struct compiler *c, stmt_ty s)
compiler_use_next_block(c, block);
if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
- return 0;
+ return 0;
}
if (s->v.With.optional_vars) {
- /* Bind saved result of context.__enter__() to VAR */
- if (!compiler_nameop(c, tmpvalue, Load) ||
- !compiler_nameop(c, tmpvalue, Del))
- return 0;
- VISIT(c, expr, s->v.With.optional_vars);
+ /* Bind saved result of context.__enter__() to VAR */
+ if (!compiler_nameop(c, tmpvalue, Load) ||
+ !compiler_nameop(c, tmpvalue, Del))
+ return 0;
+ VISIT(c, expr, s->v.With.optional_vars);
}
/* BLOCK code */
@@ -3166,14 +3166,14 @@ compiler_with(struct compiler *c, stmt_ty s)
ADDOP_O(c, LOAD_CONST, Py_None, consts);
compiler_use_next_block(c, finally);
if (!compiler_push_fblock(c, FINALLY_END, finally))
- return 0;
+ return 0;
/* Finally block starts; context.__exit__ is on the stack under
the exception or return information. Just issue our magic
opcode. */
- if (!compiler_nameop(c, tmpexit, Load) ||
- !compiler_nameop(c, tmpexit, Del))
- return 0;
+ if (!compiler_nameop(c, tmpexit, Load) ||
+ !compiler_nameop(c, tmpexit, Del))
+ return 0;
ADDOP(c, WITH_CLEANUP);
/* Finally block ends. */
@@ -3185,240 +3185,240 @@ compiler_with(struct compiler *c, stmt_ty s)
static int
compiler_visit_expr(struct compiler *c, expr_ty e)
{
- int i, n;
-
- /* If expr e has a different line number than the last expr/stmt,
- set a new line number for the next instruction.
- */
- if (e->lineno > c->u->u_lineno) {
- c->u->u_lineno = e->lineno;
- c->u->u_lineno_set = 0;
- }
- switch (e->kind) {
- case BoolOp_kind:
- return compiler_boolop(c, e);
- case BinOp_kind:
- VISIT(c, expr, e->v.BinOp.left);
- VISIT(c, expr, e->v.BinOp.right);
- ADDOP(c, binop(c, e->v.BinOp.op));
- break;
- case UnaryOp_kind:
- VISIT(c, expr, e->v.UnaryOp.operand);
- ADDOP(c, unaryop(e->v.UnaryOp.op));
- break;
- case Lambda_kind:
- return compiler_lambda(c, e);
- case IfExp_kind:
- return compiler_ifexp(c, e);
- case Dict_kind:
- n = asdl_seq_LEN(e->v.Dict.values);
- ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
- for (i = 0; i < n; i++) {
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
- ADDOP(c, STORE_MAP);
- }
- break;
- case Set_kind:
- n = asdl_seq_LEN(e->v.Set.elts);
- VISIT_SEQ(c, expr, e->v.Set.elts);
- ADDOP_I(c, BUILD_SET, n);
- break;
- case GeneratorExp_kind:
- return compiler_genexp(c, e);
- case ListComp_kind:
- return compiler_listcomp(c, e);
- case SetComp_kind:
- return compiler_setcomp(c, e);
- case DictComp_kind:
- return compiler_dictcomp(c, e);
- case Yield_kind:
- if (c->u->u_ste->ste_type != FunctionBlock)
- return compiler_error(c, "'yield' outside function");
- if (e->v.Yield.value) {
- VISIT(c, expr, e->v.Yield.value);
- }
- else {
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- }
- ADDOP(c, YIELD_VALUE);
- break;
- case Compare_kind:
- return compiler_compare(c, e);
- case Call_kind:
- return compiler_call(c, e);
- case Num_kind:
- ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
- break;
- case Str_kind:
- ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
- break;
- case Bytes_kind:
- ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
- break;
- case Ellipsis_kind:
- ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
- break;
- /* The following exprs can be assignment targets. */
- case Attribute_kind:
- if (e->v.Attribute.ctx != AugStore)
- VISIT(c, expr, e->v.Attribute.value);
- switch (e->v.Attribute.ctx) {
- case AugLoad:
- ADDOP(c, DUP_TOP);
- /* Fall through to load */
- case Load:
- ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
- break;
- case AugStore:
- ADDOP(c, ROT_TWO);
- /* Fall through to save */
- case Store:
- ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
- break;
- case Del:
- ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
- break;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid in attribute expression");
- return 0;
- }
- break;
- case Subscript_kind:
- switch (e->v.Subscript.ctx) {
- case AugLoad:
- VISIT(c, expr, e->v.Subscript.value);
- VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
- break;
- case Load:
- VISIT(c, expr, e->v.Subscript.value);
- VISIT_SLICE(c, e->v.Subscript.slice, Load);
- break;
- case AugStore:
- VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
- break;
- case Store:
- VISIT(c, expr, e->v.Subscript.value);
- VISIT_SLICE(c, e->v.Subscript.slice, Store);
- break;
- case Del:
- VISIT(c, expr, e->v.Subscript.value);
- VISIT_SLICE(c, e->v.Subscript.slice, Del);
- break;
- case Param:
- default:
- PyErr_SetString(PyExc_SystemError,
- "param invalid in subscript expression");
- return 0;
- }
- break;
- case Starred_kind:
- switch (e->v.Starred.ctx) {
- case Store:
- /* In all legitimate cases, the Starred node was already replaced
- * by compiler_list/compiler_tuple. XXX: is that okay? */
- return compiler_error(c,
- "starred assignment target must be in a list or tuple");
- default:
- return compiler_error(c,
- "can use starred expression only as assignment target");
- }
- break;
- case Name_kind:
- return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
- /* child nodes of List and Tuple will have expr_context set */
- case List_kind:
- return compiler_list(c, e);
- case Tuple_kind:
- return compiler_tuple(c, e);
- }
- return 1;
+ int i, n;
+
+ /* If expr e has a different line number than the last expr/stmt,
+ set a new line number for the next instruction.
+ */
+ if (e->lineno > c->u->u_lineno) {
+ c->u->u_lineno = e->lineno;
+ c->u->u_lineno_set = 0;
+ }
+ switch (e->kind) {
+ case BoolOp_kind:
+ return compiler_boolop(c, e);
+ case BinOp_kind:
+ VISIT(c, expr, e->v.BinOp.left);
+ VISIT(c, expr, e->v.BinOp.right);
+ ADDOP(c, binop(c, e->v.BinOp.op));
+ break;
+ case UnaryOp_kind:
+ VISIT(c, expr, e->v.UnaryOp.operand);
+ ADDOP(c, unaryop(e->v.UnaryOp.op));
+ break;
+ case Lambda_kind:
+ return compiler_lambda(c, e);
+ case IfExp_kind:
+ return compiler_ifexp(c, e);
+ case Dict_kind:
+ n = asdl_seq_LEN(e->v.Dict.values);
+ ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
+ for (i = 0; i < n; i++) {
+ VISIT(c, expr,
+ (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
+ VISIT(c, expr,
+ (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
+ ADDOP(c, STORE_MAP);
+ }
+ break;
+ case Set_kind:
+ n = asdl_seq_LEN(e->v.Set.elts);
+ VISIT_SEQ(c, expr, e->v.Set.elts);
+ ADDOP_I(c, BUILD_SET, n);
+ break;
+ case GeneratorExp_kind:
+ return compiler_genexp(c, e);
+ case ListComp_kind:
+ return compiler_listcomp(c, e);
+ case SetComp_kind:
+ return compiler_setcomp(c, e);
+ case DictComp_kind:
+ return compiler_dictcomp(c, e);
+ case Yield_kind:
+ if (c->u->u_ste->ste_type != FunctionBlock)
+ return compiler_error(c, "'yield' outside function");
+ if (e->v.Yield.value) {
+ VISIT(c, expr, e->v.Yield.value);
+ }
+ else {
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ }
+ ADDOP(c, YIELD_VALUE);
+ break;
+ case Compare_kind:
+ return compiler_compare(c, e);
+ case Call_kind:
+ return compiler_call(c, e);
+ case Num_kind:
+ ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
+ break;
+ case Str_kind:
+ ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
+ break;
+ case Bytes_kind:
+ ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
+ break;
+ case Ellipsis_kind:
+ ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
+ break;
+ /* The following exprs can be assignment targets. */
+ case Attribute_kind:
+ if (e->v.Attribute.ctx != AugStore)
+ VISIT(c, expr, e->v.Attribute.value);
+ switch (e->v.Attribute.ctx) {
+ case AugLoad:
+ ADDOP(c, DUP_TOP);
+ /* Fall through to load */
+ case Load:
+ ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
+ break;
+ case AugStore:
+ ADDOP(c, ROT_TWO);
+ /* Fall through to save */
+ case Store:
+ ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
+ break;
+ case Del:
+ ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
+ break;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid in attribute expression");
+ return 0;
+ }
+ break;
+ case Subscript_kind:
+ switch (e->v.Subscript.ctx) {
+ case AugLoad:
+ VISIT(c, expr, e->v.Subscript.value);
+ VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
+ break;
+ case Load:
+ VISIT(c, expr, e->v.Subscript.value);
+ VISIT_SLICE(c, e->v.Subscript.slice, Load);
+ break;
+ case AugStore:
+ VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
+ break;
+ case Store:
+ VISIT(c, expr, e->v.Subscript.value);
+ VISIT_SLICE(c, e->v.Subscript.slice, Store);
+ break;
+ case Del:
+ VISIT(c, expr, e->v.Subscript.value);
+ VISIT_SLICE(c, e->v.Subscript.slice, Del);
+ break;
+ case Param:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "param invalid in subscript expression");
+ return 0;
+ }
+ break;
+ case Starred_kind:
+ switch (e->v.Starred.ctx) {
+ case Store:
+ /* In all legitimate cases, the Starred node was already replaced
+ * by compiler_list/compiler_tuple. XXX: is that okay? */
+ return compiler_error(c,
+ "starred assignment target must be in a list or tuple");
+ default:
+ return compiler_error(c,
+ "can use starred expression only as assignment target");
+ }
+ break;
+ case Name_kind:
+ return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
+ /* child nodes of List and Tuple will have expr_context set */
+ case List_kind:
+ return compiler_list(c, e);
+ case Tuple_kind:
+ return compiler_tuple(c, e);
+ }
+ return 1;
}
static int
compiler_augassign(struct compiler *c, stmt_ty s)
{
- expr_ty e = s->v.AugAssign.target;
- expr_ty auge;
-
- assert(s->kind == AugAssign_kind);
-
- switch (e->kind) {
- case Attribute_kind:
- auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
- AugLoad, e->lineno, e->col_offset, c->c_arena);
- if (auge == NULL)
- return 0;
- VISIT(c, expr, auge);
- VISIT(c, expr, s->v.AugAssign.value);
- ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
- auge->v.Attribute.ctx = AugStore;
- VISIT(c, expr, auge);
- break;
- case Subscript_kind:
- auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
- AugLoad, e->lineno, e->col_offset, c->c_arena);
- if (auge == NULL)
- return 0;
- VISIT(c, expr, auge);
- VISIT(c, expr, s->v.AugAssign.value);
- ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
- auge->v.Subscript.ctx = AugStore;
- VISIT(c, expr, auge);
- break;
- case Name_kind:
- if (!compiler_nameop(c, e->v.Name.id, Load))
- return 0;
- VISIT(c, expr, s->v.AugAssign.value);
- ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
- return compiler_nameop(c, e->v.Name.id, Store);
- default:
- PyErr_Format(PyExc_SystemError,
- "invalid node type (%d) for augmented assignment",
- e->kind);
- return 0;
- }
- return 1;
+ expr_ty e = s->v.AugAssign.target;
+ expr_ty auge;
+
+ assert(s->kind == AugAssign_kind);
+
+ switch (e->kind) {
+ case Attribute_kind:
+ auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
+ AugLoad, e->lineno, e->col_offset, c->c_arena);
+ if (auge == NULL)
+ return 0;
+ VISIT(c, expr, auge);
+ VISIT(c, expr, s->v.AugAssign.value);
+ ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
+ auge->v.Attribute.ctx = AugStore;
+ VISIT(c, expr, auge);
+ break;
+ case Subscript_kind:
+ auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
+ AugLoad, e->lineno, e->col_offset, c->c_arena);
+ if (auge == NULL)
+ return 0;
+ VISIT(c, expr, auge);
+ VISIT(c, expr, s->v.AugAssign.value);
+ ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
+ auge->v.Subscript.ctx = AugStore;
+ VISIT(c, expr, auge);
+ break;
+ case Name_kind:
+ if (!compiler_nameop(c, e->v.Name.id, Load))
+ return 0;
+ VISIT(c, expr, s->v.AugAssign.value);
+ ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
+ return compiler_nameop(c, e->v.Name.id, Store);
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "invalid node type (%d) for augmented assignment",
+ e->kind);
+ return 0;
+ }
+ return 1;
}
static int
compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
{
- struct fblockinfo *f;
- if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
- PyErr_SetString(PyExc_SystemError,
- "too many statically nested blocks");
- return 0;
- }
- f = &c->u->u_fblock[c->u->u_nfblocks++];
- f->fb_type = t;
- f->fb_block = b;
- return 1;
+ struct fblockinfo *f;
+ if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
+ PyErr_SetString(PyExc_SystemError,
+ "too many statically nested blocks");
+ return 0;
+ }
+ f = &c->u->u_fblock[c->u->u_nfblocks++];
+ f->fb_type = t;
+ f->fb_block = b;
+ return 1;
}
static void
compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
{
- struct compiler_unit *u = c->u;
- assert(u->u_nfblocks > 0);
- u->u_nfblocks--;
- assert(u->u_fblock[u->u_nfblocks].fb_type == t);
- assert(u->u_fblock[u->u_nfblocks].fb_block == b);
+ struct compiler_unit *u = c->u;
+ assert(u->u_nfblocks > 0);
+ u->u_nfblocks--;
+ assert(u->u_fblock[u->u_nfblocks].fb_type == t);
+ assert(u->u_fblock[u->u_nfblocks].fb_block == b);
}
static int
compiler_in_loop(struct compiler *c) {
- int i;
- struct compiler_unit *u = c->u;
- for (i = 0; i < u->u_nfblocks; ++i) {
- if (u->u_fblock[i].fb_type == LOOP)
- return 1;
- }
- return 0;
+ int i;
+ struct compiler_unit *u = c->u;
+ for (i = 0; i < u->u_nfblocks; ++i) {
+ if (u->u_fblock[i].fb_type == LOOP)
+ return 1;
+ }
+ return 0;
}
/* Raises a SyntaxError and returns 0.
If something goes wrong, a different exception may be raised.
@@ -3427,143 +3427,143 @@ compiler_in_loop(struct compiler *c) {
static int
compiler_error(struct compiler *c, const char *errstr)
{
- PyObject *loc;
- PyObject *u = NULL, *v = NULL;
-
- loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
- if (!loc) {
- Py_INCREF(Py_None);
- loc = Py_None;
- }
- u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
- Py_None, loc);
- if (!u)
- goto exit;
- v = Py_BuildValue("(zO)", errstr, u);
- if (!v)
- goto exit;
- PyErr_SetObject(PyExc_SyntaxError, v);
+ PyObject *loc;
+ PyObject *u = NULL, *v = NULL;
+
+ loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
+ if (!loc) {
+ Py_INCREF(Py_None);
+ loc = Py_None;
+ }
+ u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
+ Py_None, loc);
+ if (!u)
+ goto exit;
+ v = Py_BuildValue("(zO)", errstr, u);
+ if (!v)
+ goto exit;
+ PyErr_SetObject(PyExc_SyntaxError, v);
exit:
- Py_DECREF(loc);
- Py_XDECREF(u);
- Py_XDECREF(v);
- return 0;
+ Py_DECREF(loc);
+ Py_XDECREF(u);
+ Py_XDECREF(v);
+ return 0;
}
static int
-compiler_handle_subscr(struct compiler *c, const char *kind,
- expr_context_ty ctx)
+compiler_handle_subscr(struct compiler *c, const char *kind,
+ expr_context_ty ctx)
{
- int op = 0;
-
- /* XXX this code is duplicated */
- switch (ctx) {
- case AugLoad: /* fall through to Load */
- case Load: op = BINARY_SUBSCR; break;
- case AugStore:/* fall through to Store */
- case Store: op = STORE_SUBSCR; break;
- case Del: op = DELETE_SUBSCR; break;
- case Param:
- PyErr_Format(PyExc_SystemError,
- "invalid %s kind %d in subscript\n",
- kind, ctx);
- return 0;
- }
- if (ctx == AugLoad) {
- ADDOP_I(c, DUP_TOPX, 2);
- }
- else if (ctx == AugStore) {
- ADDOP(c, ROT_THREE);
- }
- ADDOP(c, op);
- return 1;
+ int op = 0;
+
+ /* XXX this code is duplicated */
+ switch (ctx) {
+ case AugLoad: /* fall through to Load */
+ case Load: op = BINARY_SUBSCR; break;
+ case AugStore:/* fall through to Store */
+ case Store: op = STORE_SUBSCR; break;
+ case Del: op = DELETE_SUBSCR; break;
+ case Param:
+ PyErr_Format(PyExc_SystemError,
+ "invalid %s kind %d in subscript\n",
+ kind, ctx);
+ return 0;
+ }
+ if (ctx == AugLoad) {
+ ADDOP_I(c, DUP_TOPX, 2);
+ }
+ else if (ctx == AugStore) {
+ ADDOP(c, ROT_THREE);
+ }
+ ADDOP(c, op);
+ return 1;
}
static int
compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
{
- int n = 2;
- assert(s->kind == Slice_kind);
-
- /* only handles the cases where BUILD_SLICE is emitted */
- if (s->v.Slice.lower) {
- VISIT(c, expr, s->v.Slice.lower);
- }
- else {
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- }
-
- if (s->v.Slice.upper) {
- VISIT(c, expr, s->v.Slice.upper);
- }
- else {
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- }
-
- if (s->v.Slice.step) {
- n++;
- VISIT(c, expr, s->v.Slice.step);
- }
- ADDOP_I(c, BUILD_SLICE, n);
- return 1;
+ int n = 2;
+ assert(s->kind == Slice_kind);
+
+ /* only handles the cases where BUILD_SLICE is emitted */
+ if (s->v.Slice.lower) {
+ VISIT(c, expr, s->v.Slice.lower);
+ }
+ else {
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ }
+
+ if (s->v.Slice.upper) {
+ VISIT(c, expr, s->v.Slice.upper);
+ }
+ else {
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ }
+
+ if (s->v.Slice.step) {
+ n++;
+ VISIT(c, expr, s->v.Slice.step);
+ }
+ ADDOP_I(c, BUILD_SLICE, n);
+ return 1;
}
static int
-compiler_visit_nested_slice(struct compiler *c, slice_ty s,
- expr_context_ty ctx)
+compiler_visit_nested_slice(struct compiler *c, slice_ty s,
+ expr_context_ty ctx)
{
- switch (s->kind) {
- case Slice_kind:
- return compiler_slice(c, s, ctx);
- case Index_kind:
- VISIT(c, expr, s->v.Index.value);
- break;
- case ExtSlice_kind:
- default:
- PyErr_SetString(PyExc_SystemError,
- "extended slice invalid in nested slice");
- return 0;
- }
- return 1;
+ switch (s->kind) {
+ case Slice_kind:
+ return compiler_slice(c, s, ctx);
+ case Index_kind:
+ VISIT(c, expr, s->v.Index.value);
+ break;
+ case ExtSlice_kind:
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "extended slice invalid in nested slice");
+ return 0;
+ }
+ return 1;
}
static int
compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
{
- char * kindname = NULL;
- switch (s->kind) {
- case Index_kind:
- kindname = "index";
- if (ctx != AugStore) {
- VISIT(c, expr, s->v.Index.value);
- }
- break;
- case Slice_kind:
- kindname = "slice";
- if (ctx != AugStore) {
- if (!compiler_slice(c, s, ctx))
- return 0;
- }
- break;
- case ExtSlice_kind:
- kindname = "extended slice";
- if (ctx != AugStore) {
- int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
- for (i = 0; i < n; i++) {
- slice_ty sub = (slice_ty)asdl_seq_GET(
- s->v.ExtSlice.dims, i);
- if (!compiler_visit_nested_slice(c, sub, ctx))
- return 0;
- }
- ADDOP_I(c, BUILD_TUPLE, n);
- }
- break;
- default:
- PyErr_Format(PyExc_SystemError,
- "invalid subscript kind %d", s->kind);
- return 0;
- }
- return compiler_handle_subscr(c, kindname, ctx);
+ char * kindname = NULL;
+ switch (s->kind) {
+ case Index_kind:
+ kindname = "index";
+ if (ctx != AugStore) {
+ VISIT(c, expr, s->v.Index.value);
+ }
+ break;
+ case Slice_kind:
+ kindname = "slice";
+ if (ctx != AugStore) {
+ if (!compiler_slice(c, s, ctx))
+ return 0;
+ }
+ break;
+ case ExtSlice_kind:
+ kindname = "extended slice";
+ if (ctx != AugStore) {
+ int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
+ for (i = 0; i < n; i++) {
+ slice_ty sub = (slice_ty)asdl_seq_GET(
+ s->v.ExtSlice.dims, i);
+ if (!compiler_visit_nested_slice(c, sub, ctx))
+ return 0;
+ }
+ ADDOP_I(c, BUILD_TUPLE, n);
+ }
+ break;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "invalid subscript kind %d", s->kind);
+ return 0;
+ }
+ return compiler_handle_subscr(c, kindname, ctx);
}
/* End of the compiler section, beginning of the assembler section */
@@ -3575,64 +3575,64 @@ compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
*/
struct assembler {
- PyObject *a_bytecode; /* string containing bytecode */
- int a_offset; /* offset into bytecode */
- int a_nblocks; /* number of reachable blocks */
- basicblock **a_postorder; /* list of blocks in dfs postorder */
- PyObject *a_lnotab; /* string containing lnotab */
- int a_lnotab_off; /* offset into lnotab */
- int a_lineno; /* last lineno of emitted instruction */
- int a_lineno_off; /* bytecode offset of last lineno */
+ PyObject *a_bytecode; /* string containing bytecode */
+ int a_offset; /* offset into bytecode */
+ int a_nblocks; /* number of reachable blocks */
+ basicblock **a_postorder; /* list of blocks in dfs postorder */
+ PyObject *a_lnotab; /* string containing lnotab */
+ int a_lnotab_off; /* offset into lnotab */
+ int a_lineno; /* last lineno of emitted instruction */
+ int a_lineno_off; /* bytecode offset of last lineno */
};
static void
dfs(struct compiler *c, basicblock *b, struct assembler *a)
{
- int i;
- struct instr *instr = NULL;
-
- if (b->b_seen)
- return;
- b->b_seen = 1;
- if (b->b_next != NULL)
- dfs(c, b->b_next, a);
- for (i = 0; i < b->b_iused; i++) {
- instr = &b->b_instr[i];
- if (instr->i_jrel || instr->i_jabs)
- dfs(c, instr->i_target, a);
- }
- a->a_postorder[a->a_nblocks++] = b;
+ int i;
+ struct instr *instr = NULL;
+
+ if (b->b_seen)
+ return;
+ b->b_seen = 1;
+ if (b->b_next != NULL)
+ dfs(c, b->b_next, a);
+ for (i = 0; i < b->b_iused; i++) {
+ instr = &b->b_instr[i];
+ if (instr->i_jrel || instr->i_jabs)
+ dfs(c, instr->i_target, a);
+ }
+ a->a_postorder[a->a_nblocks++] = b;
}
static int
stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
{
- int i;
- struct instr *instr;
- if (b->b_seen || b->b_startdepth >= depth)
- return maxdepth;
- b->b_seen = 1;
- b->b_startdepth = depth;
- for (i = 0; i < b->b_iused; i++) {
- instr = &b->b_instr[i];
- depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
- if (depth > maxdepth)
- maxdepth = depth;
- assert(depth >= 0); /* invalid code or bug in stackdepth() */
- if (instr->i_jrel || instr->i_jabs) {
- maxdepth = stackdepth_walk(c, instr->i_target,
- depth, maxdepth);
- if (instr->i_opcode == JUMP_ABSOLUTE ||
- instr->i_opcode == JUMP_FORWARD) {
- goto out; /* remaining code is dead */
- }
- }
- }
- if (b->b_next)
- maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
+ int i;
+ struct instr *instr;
+ if (b->b_seen || b->b_startdepth >= depth)
+ return maxdepth;
+ b->b_seen = 1;
+ b->b_startdepth = depth;
+ for (i = 0; i < b->b_iused; i++) {
+ instr = &b->b_instr[i];
+ depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
+ if (depth > maxdepth)
+ maxdepth = depth;
+ assert(depth >= 0); /* invalid code or bug in stackdepth() */
+ if (instr->i_jrel || instr->i_jabs) {
+ maxdepth = stackdepth_walk(c, instr->i_target,
+ depth, maxdepth);
+ if (instr->i_opcode == JUMP_ABSOLUTE ||
+ instr->i_opcode == JUMP_FORWARD) {
+ goto out; /* remaining code is dead */
+ }
+ }
+ }
+ if (b->b_next)
+ maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
out:
- b->b_seen = 0;
- return maxdepth;
+ b->b_seen = 0;
+ return maxdepth;
}
/* Find the flow path that needs the largest stack. We assume that
@@ -3641,49 +3641,49 @@ out:
static int
stackdepth(struct compiler *c)
{
- basicblock *b, *entryblock;
- entryblock = NULL;
- for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
- b->b_seen = 0;
- b->b_startdepth = INT_MIN;
- entryblock = b;
- }
- if (!entryblock)
- return 0;
- return stackdepth_walk(c, entryblock, 0, 0);
+ basicblock *b, *entryblock;
+ entryblock = NULL;
+ for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
+ b->b_seen = 0;
+ b->b_startdepth = INT_MIN;
+ entryblock = b;
+ }
+ if (!entryblock)
+ return 0;
+ return stackdepth_walk(c, entryblock, 0, 0);
}
static int
assemble_init(struct assembler *a, int nblocks, int firstlineno)
{
- memset(a, 0, sizeof(struct assembler));
- a->a_lineno = firstlineno;
- a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
- if (!a->a_bytecode)
- return 0;
- a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
- if (!a->a_lnotab)
- return 0;
- if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
- PyErr_NoMemory();
- return 0;
- }
- a->a_postorder = (basicblock **)PyObject_Malloc(
- sizeof(basicblock *) * nblocks);
- if (!a->a_postorder) {
- PyErr_NoMemory();
- return 0;
- }
- return 1;
+ memset(a, 0, sizeof(struct assembler));
+ a->a_lineno = firstlineno;
+ a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
+ if (!a->a_bytecode)
+ return 0;
+ a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
+ if (!a->a_lnotab)
+ return 0;
+ if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ a->a_postorder = (basicblock **)PyObject_Malloc(
+ sizeof(basicblock *) * nblocks);
+ if (!a->a_postorder) {
+ PyErr_NoMemory();
+ return 0;
+ }
+ return 1;
}
static void
assemble_free(struct assembler *a)
{
- Py_XDECREF(a->a_bytecode);
- Py_XDECREF(a->a_lnotab);
- if (a->a_postorder)
- PyObject_Free(a->a_postorder);
+ Py_XDECREF(a->a_bytecode);
+ Py_XDECREF(a->a_lnotab);
+ if (a->a_postorder)
+ PyObject_Free(a->a_postorder);
}
/* Return the size of a basic block in bytes. */
@@ -3691,22 +3691,22 @@ assemble_free(struct assembler *a)
static int
instrsize(struct instr *instr)
{
- if (!instr->i_hasarg)
- return 1; /* 1 byte for the opcode*/
- if (instr->i_oparg > 0xffff)
- return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
- return 3; /* 1 (opcode) + 2 (oparg) */
+ if (!instr->i_hasarg)
+ return 1; /* 1 byte for the opcode*/
+ if (instr->i_oparg > 0xffff)
+ return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
+ return 3; /* 1 (opcode) + 2 (oparg) */
}
static int
blocksize(basicblock *b)
{
- int i;
- int size = 0;
+ int i;
+ int size = 0;
- for (i = 0; i < b->b_iused; i++)
- size += instrsize(&b->b_instr[i]);
- return size;
+ for (i = 0; i < b->b_iused; i++)
+ size += instrsize(&b->b_instr[i]);
+ return size;
}
/* All about a_lnotab.
@@ -3717,14 +3717,14 @@ for tracebacks).
The array is conceptually a list of
(bytecode offset increment, line number increment)
-pairs. The details are important and delicate, best illustrated by example:
+pairs. The details are important and delicate, best illustrated by example:
- byte code offset source code line number
- 0 1
- 6 2
- 50 7
- 350 307
- 361 308
+ byte code offset source code line number
+ 0 1
+ 6 2
+ 50 7
+ 350 307
+ 361 308
The first trick is that these numbers aren't stored, only the increments
from one row to the next (this doesn't really work, but it's a start):
@@ -3736,116 +3736,116 @@ values larger than 255, so (a) there's a deep assumption that byte code
offsets and their corresponding line #s both increase monotonically, and (b)
if at least one column jumps by more than 255 from one row to the next, more
than one pair is written to the table. In case #b, there's no way to know
-from looking at the table later how many were written. That's the delicate
+from looking at the table later how many were written. That's the delicate
part. A user of c_lnotab desiring to find the source line number
corresponding to a bytecode address A should do something like this
lineno = addr = 0
for addr_incr, line_incr in c_lnotab:
- addr += addr_incr
- if addr > A:
- return lineno
- lineno += line_incr
+ addr += addr_incr
+ if addr > A:
+ return lineno
+ lineno += line_incr
In order for this to work, when the addr field increments by more than 255,
the line # increment in each pair generated must be 0 until the remaining addr
increment is < 256. So, in the example above, assemble_lnotab (it used
to be called com_set_lineno) should not (as was actually done until 2.2)
-expand 300, 300 to 255, 255, 45, 45,
- but to 255, 0, 45, 255, 0, 45.
+expand 300, 300 to 255, 255, 45, 45,
+ but to 255, 0, 45, 255, 0, 45.
*/
static int
assemble_lnotab(struct assembler *a, struct instr *i)
{
- int d_bytecode, d_lineno;
- int len;
- unsigned char *lnotab;
-
- d_bytecode = a->a_offset - a->a_lineno_off;
- d_lineno = i->i_lineno - a->a_lineno;
-
- assert(d_bytecode >= 0);
- assert(d_lineno >= 0);
-
- if(d_bytecode == 0 && d_lineno == 0)
- return 1;
-
- if (d_bytecode > 255) {
- int j, nbytes, ncodes = d_bytecode / 255;
- nbytes = a->a_lnotab_off + 2 * ncodes;
- len = PyBytes_GET_SIZE(a->a_lnotab);
- if (nbytes >= len) {
- if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
- len = nbytes;
- else if (len <= INT_MAX / 2)
- len *= 2;
- else {
- PyErr_NoMemory();
- return 0;
- }
- if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
- return 0;
- }
- lnotab = (unsigned char *)
- PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
- for (j = 0; j < ncodes; j++) {
- *lnotab++ = 255;
- *lnotab++ = 0;
- }
- d_bytecode -= ncodes * 255;
- a->a_lnotab_off += ncodes * 2;
- }
- assert(d_bytecode <= 255);
- if (d_lineno > 255) {
- int j, nbytes, ncodes = d_lineno / 255;
- nbytes = a->a_lnotab_off + 2 * ncodes;
- len = PyBytes_GET_SIZE(a->a_lnotab);
- if (nbytes >= len) {
- if ((len <= INT_MAX / 2) && len * 2 < nbytes)
- len = nbytes;
- else if (len <= INT_MAX / 2)
- len *= 2;
- else {
- PyErr_NoMemory();
- return 0;
- }
- if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
- return 0;
- }
- lnotab = (unsigned char *)
- PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
- *lnotab++ = d_bytecode;
- *lnotab++ = 255;
- d_bytecode = 0;
- for (j = 1; j < ncodes; j++) {
- *lnotab++ = 0;
- *lnotab++ = 255;
- }
- d_lineno -= ncodes * 255;
- a->a_lnotab_off += ncodes * 2;
- }
-
- len = PyBytes_GET_SIZE(a->a_lnotab);
- if (a->a_lnotab_off + 2 >= len) {
- if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
- return 0;
- }
- lnotab = (unsigned char *)
- PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
-
- a->a_lnotab_off += 2;
- if (d_bytecode) {
- *lnotab++ = d_bytecode;
- *lnotab++ = d_lineno;
- }
- else { /* First line of a block; def stmt, etc. */
- *lnotab++ = 0;
- *lnotab++ = d_lineno;
- }
- a->a_lineno = i->i_lineno;
- a->a_lineno_off = a->a_offset;
- return 1;
+ int d_bytecode, d_lineno;
+ int len;
+ unsigned char *lnotab;
+
+ d_bytecode = a->a_offset - a->a_lineno_off;
+ d_lineno = i->i_lineno - a->a_lineno;
+
+ assert(d_bytecode >= 0);
+ assert(d_lineno >= 0);
+
+ if(d_bytecode == 0 && d_lineno == 0)
+ return 1;
+
+ if (d_bytecode > 255) {
+ int j, nbytes, ncodes = d_bytecode / 255;
+ nbytes = a->a_lnotab_off + 2 * ncodes;
+ len = PyBytes_GET_SIZE(a->a_lnotab);
+ if (nbytes >= len) {
+ if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
+ len = nbytes;
+ else if (len <= INT_MAX / 2)
+ len *= 2;
+ else {
+ PyErr_NoMemory();
+ return 0;
+ }
+ if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
+ return 0;
+ }
+ lnotab = (unsigned char *)
+ PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+ for (j = 0; j < ncodes; j++) {
+ *lnotab++ = 255;
+ *lnotab++ = 0;
+ }
+ d_bytecode -= ncodes * 255;
+ a->a_lnotab_off += ncodes * 2;
+ }
+ assert(d_bytecode <= 255);
+ if (d_lineno > 255) {
+ int j, nbytes, ncodes = d_lineno / 255;
+ nbytes = a->a_lnotab_off + 2 * ncodes;
+ len = PyBytes_GET_SIZE(a->a_lnotab);
+ if (nbytes >= len) {
+ if ((len <= INT_MAX / 2) && len * 2 < nbytes)
+ len = nbytes;
+ else if (len <= INT_MAX / 2)
+ len *= 2;
+ else {
+ PyErr_NoMemory();
+ return 0;
+ }
+ if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
+ return 0;
+ }
+ lnotab = (unsigned char *)
+ PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+ *lnotab++ = d_bytecode;
+ *lnotab++ = 255;
+ d_bytecode = 0;
+ for (j = 1; j < ncodes; j++) {
+ *lnotab++ = 0;
+ *lnotab++ = 255;
+ }
+ d_lineno -= ncodes * 255;
+ a->a_lnotab_off += ncodes * 2;
+ }
+
+ len = PyBytes_GET_SIZE(a->a_lnotab);
+ if (a->a_lnotab_off + 2 >= len) {
+ if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
+ return 0;
+ }
+ lnotab = (unsigned char *)
+ PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+
+ a->a_lnotab_off += 2;
+ if (d_bytecode) {
+ *lnotab++ = d_bytecode;
+ *lnotab++ = d_lineno;
+ }
+ else { /* First line of a block; def stmt, etc. */
+ *lnotab++ = 0;
+ *lnotab++ = d_lineno;
+ }
+ a->a_lineno = i->i_lineno;
+ a->a_lineno_off = a->a_offset;
+ return 1;
}
/* assemble_emit()
@@ -3856,232 +3856,232 @@ assemble_lnotab(struct assembler *a, struct instr *i)
static int
assemble_emit(struct assembler *a, struct instr *i)
{
- int size, arg = 0, ext = 0;
- Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
- char *code;
-
- size = instrsize(i);
- if (i->i_hasarg) {
- arg = i->i_oparg;
- ext = arg >> 16;
- }
- if (i->i_lineno && !assemble_lnotab(a, i))
- return 0;
- if (a->a_offset + size >= len) {
- if (len > PY_SSIZE_T_MAX / 2)
- return 0;
- if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
- return 0;
- }
- code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
- a->a_offset += size;
- if (size == 6) {
- assert(i->i_hasarg);
- *code++ = (char)EXTENDED_ARG;
- *code++ = ext & 0xff;
- *code++ = ext >> 8;
- arg &= 0xffff;
- }
- *code++ = i->i_opcode;
- if (i->i_hasarg) {
- assert(size == 3 || size == 6);
- *code++ = arg & 0xff;
- *code++ = arg >> 8;
- }
- return 1;
+ int size, arg = 0, ext = 0;
+ Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
+ char *code;
+
+ size = instrsize(i);
+ if (i->i_hasarg) {
+ arg = i->i_oparg;
+ ext = arg >> 16;
+ }
+ if (i->i_lineno && !assemble_lnotab(a, i))
+ return 0;
+ if (a->a_offset + size >= len) {
+ if (len > PY_SSIZE_T_MAX / 2)
+ return 0;
+ if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
+ return 0;
+ }
+ code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
+ a->a_offset += size;
+ if (size == 6) {
+ assert(i->i_hasarg);
+ *code++ = (char)EXTENDED_ARG;
+ *code++ = ext & 0xff;
+ *code++ = ext >> 8;
+ arg &= 0xffff;
+ }
+ *code++ = i->i_opcode;
+ if (i->i_hasarg) {
+ assert(size == 3 || size == 6);
+ *code++ = arg & 0xff;
+ *code++ = arg >> 8;
+ }
+ return 1;
}
static void
assemble_jump_offsets(struct assembler *a, struct compiler *c)
{
- basicblock *b;
- int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
- int i;
+ basicblock *b;
+ int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
+ int i;
- /* Compute the size of each block and fixup jump args.
- Replace block pointer with position in bytecode. */
+ /* Compute the size of each block and fixup jump args.
+ Replace block pointer with position in bytecode. */
start:
- totsize = 0;
- for (i = a->a_nblocks - 1; i >= 0; i--) {
- b = a->a_postorder[i];
- bsize = blocksize(b);
- b->b_offset = totsize;
- totsize += bsize;
- }
- extended_arg_count = 0;
- for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
- bsize = b->b_offset;
- for (i = 0; i < b->b_iused; i++) {
- struct instr *instr = &b->b_instr[i];
- /* Relative jumps are computed relative to
- the instruction pointer after fetching
- the jump instruction.
- */
- bsize += instrsize(instr);
- if (instr->i_jabs)
- instr->i_oparg = instr->i_target->b_offset;
- else if (instr->i_jrel) {
- int delta = instr->i_target->b_offset - bsize;
- instr->i_oparg = delta;
- }
- else
- continue;
- if (instr->i_oparg > 0xffff)
- extended_arg_count++;
- }
- }
-
- /* XXX: This is an awful hack that could hurt performance, but
- on the bright side it should work until we come up
- with a better solution.
-
- In the meantime, should the goto be dropped in favor
- of a loop?
-
- The issue is that in the first loop blocksize() is called
- which calls instrsize() which requires i_oparg be set
- appropriately. There is a bootstrap problem because
- i_oparg is calculated in the second loop above.
-
- So we loop until we stop seeing new EXTENDED_ARGs.
- The only EXTENDED_ARGs that could be popping up are
- ones in jump instructions. So this should converge
- fairly quickly.
- */
- if (last_extended_arg_count != extended_arg_count) {
- last_extended_arg_count = extended_arg_count;
- goto start;
- }
+ totsize = 0;
+ for (i = a->a_nblocks - 1; i >= 0; i--) {
+ b = a->a_postorder[i];
+ bsize = blocksize(b);
+ b->b_offset = totsize;
+ totsize += bsize;
+ }
+ extended_arg_count = 0;
+ for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
+ bsize = b->b_offset;
+ for (i = 0; i < b->b_iused; i++) {
+ struct instr *instr = &b->b_instr[i];
+ /* Relative jumps are computed relative to
+ the instruction pointer after fetching
+ the jump instruction.
+ */
+ bsize += instrsize(instr);
+ if (instr->i_jabs)
+ instr->i_oparg = instr->i_target->b_offset;
+ else if (instr->i_jrel) {
+ int delta = instr->i_target->b_offset - bsize;
+ instr->i_oparg = delta;
+ }
+ else
+ continue;
+ if (instr->i_oparg > 0xffff)
+ extended_arg_count++;
+ }
+ }
+
+ /* XXX: This is an awful hack that could hurt performance, but
+ on the bright side it should work until we come up
+ with a better solution.
+
+ In the meantime, should the goto be dropped in favor
+ of a loop?
+
+ The issue is that in the first loop blocksize() is called
+ which calls instrsize() which requires i_oparg be set
+ appropriately. There is a bootstrap problem because
+ i_oparg is calculated in the second loop above.
+
+ So we loop until we stop seeing new EXTENDED_ARGs.
+ The only EXTENDED_ARGs that could be popping up are
+ ones in jump instructions. So this should converge
+ fairly quickly.
+ */
+ if (last_extended_arg_count != extended_arg_count) {
+ last_extended_arg_count = extended_arg_count;
+ goto start;
+ }
}
static PyObject *
dict_keys_inorder(PyObject *dict, int offset)
{
- PyObject *tuple, *k, *v;
- Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
-
- tuple = PyTuple_New(size);
- if (tuple == NULL)
- return NULL;
- while (PyDict_Next(dict, &pos, &k, &v)) {
- i = PyLong_AS_LONG(v);
- /* The keys of the dictionary are tuples. (see compiler_add_o)
- The object we want is always first, though. */
- k = PyTuple_GET_ITEM(k, 0);
- Py_INCREF(k);
- assert((i - offset) < size);
- assert((i - offset) >= 0);
- PyTuple_SET_ITEM(tuple, i - offset, k);
- }
- return tuple;
+ PyObject *tuple, *k, *v;
+ Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
+
+ tuple = PyTuple_New(size);
+ if (tuple == NULL)
+ return NULL;
+ while (PyDict_Next(dict, &pos, &k, &v)) {
+ i = PyLong_AS_LONG(v);
+ /* The keys of the dictionary are tuples. (see compiler_add_o)
+ The object we want is always first, though. */
+ k = PyTuple_GET_ITEM(k, 0);
+ Py_INCREF(k);
+ assert((i - offset) < size);
+ assert((i - offset) >= 0);
+ PyTuple_SET_ITEM(tuple, i - offset, k);
+ }
+ return tuple;
}
static int
compute_code_flags(struct compiler *c)
{
- PySTEntryObject *ste = c->u->u_ste;
- int flags = 0, n;
- if (ste->ste_type != ModuleBlock)
- flags |= CO_NEWLOCALS;
- if (ste->ste_type == FunctionBlock) {
- if (!ste->ste_unoptimized)
- flags |= CO_OPTIMIZED;
- if (ste->ste_nested)
- flags |= CO_NESTED;
- if (ste->ste_generator)
- flags |= CO_GENERATOR;
- if (ste->ste_varargs)
- flags |= CO_VARARGS;
- if (ste->ste_varkeywords)
- flags |= CO_VARKEYWORDS;
- }
-
- /* (Only) inherit compilerflags in PyCF_MASK */
- flags |= (c->c_flags->cf_flags & PyCF_MASK);
-
- n = PyDict_Size(c->u->u_freevars);
- if (n < 0)
- return -1;
- if (n == 0) {
- n = PyDict_Size(c->u->u_cellvars);
- if (n < 0)
- return -1;
- if (n == 0) {
- flags |= CO_NOFREE;
- }
- }
-
- return flags;
+ PySTEntryObject *ste = c->u->u_ste;
+ int flags = 0, n;
+ if (ste->ste_type != ModuleBlock)
+ flags |= CO_NEWLOCALS;
+ if (ste->ste_type == FunctionBlock) {
+ if (!ste->ste_unoptimized)
+ flags |= CO_OPTIMIZED;
+ if (ste->ste_nested)
+ flags |= CO_NESTED;
+ if (ste->ste_generator)
+ flags |= CO_GENERATOR;
+ if (ste->ste_varargs)
+ flags |= CO_VARARGS;
+ if (ste->ste_varkeywords)
+ flags |= CO_VARKEYWORDS;
+ }
+
+ /* (Only) inherit compilerflags in PyCF_MASK */
+ flags |= (c->c_flags->cf_flags & PyCF_MASK);
+
+ n = PyDict_Size(c->u->u_freevars);
+ if (n < 0)
+ return -1;
+ if (n == 0) {
+ n = PyDict_Size(c->u->u_cellvars);
+ if (n < 0)
+ return -1;
+ if (n == 0) {
+ flags |= CO_NOFREE;
+ }
+ }
+
+ return flags;
}
static PyCodeObject *
makecode(struct compiler *c, struct assembler *a)
{
- PyObject *tmp;
- PyCodeObject *co = NULL;
- PyObject *consts = NULL;
- PyObject *names = NULL;
- PyObject *varnames = NULL;
- PyObject *filename = NULL;
- PyObject *name = NULL;
- PyObject *freevars = NULL;
- PyObject *cellvars = NULL;
- PyObject *bytecode = NULL;
- int nlocals, flags;
-
- tmp = dict_keys_inorder(c->u->u_consts, 0);
- if (!tmp)
- goto error;
- consts = PySequence_List(tmp); /* optimize_code requires a list */
- Py_DECREF(tmp);
-
- names = dict_keys_inorder(c->u->u_names, 0);
- varnames = dict_keys_inorder(c->u->u_varnames, 0);
- if (!consts || !names || !varnames)
- goto error;
-
- cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
- if (!cellvars)
- goto error;
- freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
- if (!freevars)
- goto error;
- filename = PyUnicode_DecodeFSDefault(c->c_filename);
- if (!filename)
- goto error;
-
- nlocals = PyDict_Size(c->u->u_varnames);
- flags = compute_code_flags(c);
- if (flags < 0)
- goto error;
-
- bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
- if (!bytecode)
- goto error;
-
- tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
- if (!tmp)
- goto error;
- Py_DECREF(consts);
- consts = tmp;
-
- co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
- nlocals, stackdepth(c), flags,
- bytecode, consts, names, varnames,
- freevars, cellvars,
- filename, c->u->u_name,
- c->u->u_firstlineno,
- a->a_lnotab);
+ PyObject *tmp;
+ PyCodeObject *co = NULL;
+ PyObject *consts = NULL;
+ PyObject *names = NULL;
+ PyObject *varnames = NULL;
+ PyObject *filename = NULL;
+ PyObject *name = NULL;
+ PyObject *freevars = NULL;
+ PyObject *cellvars = NULL;
+ PyObject *bytecode = NULL;
+ int nlocals, flags;
+
+ tmp = dict_keys_inorder(c->u->u_consts, 0);
+ if (!tmp)
+ goto error;
+ consts = PySequence_List(tmp); /* optimize_code requires a list */
+ Py_DECREF(tmp);
+
+ names = dict_keys_inorder(c->u->u_names, 0);
+ varnames = dict_keys_inorder(c->u->u_varnames, 0);
+ if (!consts || !names || !varnames)
+ goto error;
+
+ cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
+ if (!cellvars)
+ goto error;
+ freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
+ if (!freevars)
+ goto error;
+ filename = PyUnicode_DecodeFSDefault(c->c_filename);
+ if (!filename)
+ goto error;
+
+ nlocals = PyDict_Size(c->u->u_varnames);
+ flags = compute_code_flags(c);
+ if (flags < 0)
+ goto error;
+
+ bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
+ if (!bytecode)
+ goto error;
+
+ tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
+ if (!tmp)
+ goto error;
+ Py_DECREF(consts);
+ consts = tmp;
+
+ co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
+ nlocals, stackdepth(c), flags,
+ bytecode, consts, names, varnames,
+ freevars, cellvars,
+ filename, c->u->u_name,
+ c->u->u_firstlineno,
+ a->a_lnotab);
error:
- Py_XDECREF(consts);
- Py_XDECREF(names);
- Py_XDECREF(varnames);
- Py_XDECREF(filename);
- Py_XDECREF(name);
- Py_XDECREF(freevars);
- Py_XDECREF(cellvars);
- Py_XDECREF(bytecode);
- return co;
+ Py_XDECREF(consts);
+ Py_XDECREF(names);
+ Py_XDECREF(varnames);
+ Py_XDECREF(filename);
+ Py_XDECREF(name);
+ Py_XDECREF(freevars);
+ Py_XDECREF(cellvars);
+ Py_XDECREF(bytecode);
+ return co;
}
@@ -4090,90 +4090,90 @@ makecode(struct compiler *c, struct assembler *a)
static void
dump_instr(const struct instr *i)
{
- const char *jrel = i->i_jrel ? "jrel " : "";
- const char *jabs = i->i_jabs ? "jabs " : "";
- char arg[128];
+ const char *jrel = i->i_jrel ? "jrel " : "";
+ const char *jabs = i->i_jabs ? "jabs " : "";
+ char arg[128];
- *arg = '\0';
- if (i->i_hasarg)
- sprintf(arg, "arg: %d ", i->i_oparg);
+ *arg = '\0';
+ if (i->i_hasarg)
+ sprintf(arg, "arg: %d ", i->i_oparg);
- fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
- i->i_lineno, i->i_opcode, arg, jabs, jrel);
+ fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
+ i->i_lineno, i->i_opcode, arg, jabs, jrel);
}
static void
dump_basicblock(const basicblock *b)
{
- const char *seen = b->b_seen ? "seen " : "";
- const char *b_return = b->b_return ? "return " : "";
- fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
- b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
- if (b->b_instr) {
- int i;
- for (i = 0; i < b->b_iused; i++) {
- fprintf(stderr, " [%02d] ", i);
- dump_instr(b->b_instr + i);
- }
- }
+ const char *seen = b->b_seen ? "seen " : "";
+ const char *b_return = b->b_return ? "return " : "";
+ fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
+ b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
+ if (b->b_instr) {
+ int i;
+ for (i = 0; i < b->b_iused; i++) {
+ fprintf(stderr, " [%02d] ", i);
+ dump_instr(b->b_instr + i);
+ }
+ }
}
#endif
static PyCodeObject *
assemble(struct compiler *c, int addNone)
{
- basicblock *b, *entryblock;
- struct assembler a;
- int i, j, nblocks;
- PyCodeObject *co = NULL;
-
- /* Make sure every block that falls off the end returns None.
- XXX NEXT_BLOCK() isn't quite right, because if the last
- block ends with a jump or return b_next shouldn't set.
- */
- if (!c->u->u_curblock->b_return) {
- NEXT_BLOCK(c);
- if (addNone)
- ADDOP_O(c, LOAD_CONST, Py_None, consts);
- ADDOP(c, RETURN_VALUE);
- }
-
- nblocks = 0;
- entryblock = NULL;
- for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
- nblocks++;
- entryblock = b;
- }
-
- /* Set firstlineno if it wasn't explicitly set. */
- if (!c->u->u_firstlineno) {
- if (entryblock && entryblock->b_instr)
- c->u->u_firstlineno = entryblock->b_instr->i_lineno;
- else
- c->u->u_firstlineno = 1;
- }
- if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
- goto error;
- dfs(c, entryblock, &a);
-
- /* Can't modify the bytecode after computing jump offsets. */
- assemble_jump_offsets(&a, c);
-
- /* Emit code in reverse postorder from dfs. */
- for (i = a.a_nblocks - 1; i >= 0; i--) {
- b = a.a_postorder[i];
- for (j = 0; j < b->b_iused; j++)
- if (!assemble_emit(&a, &b->b_instr[j]))
- goto error;
- }
-
- if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
- goto error;
- if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
- goto error;
-
- co = makecode(c, &a);
+ basicblock *b, *entryblock;
+ struct assembler a;
+ int i, j, nblocks;
+ PyCodeObject *co = NULL;
+
+ /* Make sure every block that falls off the end returns None.
+ XXX NEXT_BLOCK() isn't quite right, because if the last
+ block ends with a jump or return b_next shouldn't set.
+ */
+ if (!c->u->u_curblock->b_return) {
+ NEXT_BLOCK(c);
+ if (addNone)
+ ADDOP_O(c, LOAD_CONST, Py_None, consts);
+ ADDOP(c, RETURN_VALUE);
+ }
+
+ nblocks = 0;
+ entryblock = NULL;
+ for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
+ nblocks++;
+ entryblock = b;
+ }
+
+ /* Set firstlineno if it wasn't explicitly set. */
+ if (!c->u->u_firstlineno) {
+ if (entryblock && entryblock->b_instr)
+ c->u->u_firstlineno = entryblock->b_instr->i_lineno;
+ else
+ c->u->u_firstlineno = 1;
+ }
+ if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
+ goto error;
+ dfs(c, entryblock, &a);
+
+ /* Can't modify the bytecode after computing jump offsets. */
+ assemble_jump_offsets(&a, c);
+
+ /* Emit code in reverse postorder from dfs. */
+ for (i = a.a_nblocks - 1; i >= 0; i--) {
+ b = a.a_postorder[i];
+ for (j = 0; j < b->b_iused; j++)
+ if (!assemble_emit(&a, &b->b_instr[j]))
+ goto error;
+ }
+
+ if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
+ goto error;
+ if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
+ goto error;
+
+ co = makecode(c, &a);
error:
- assemble_free(&a);
- return co;
+ assemble_free(&a);
+ return co;
}
diff --git a/Python/dynload_aix.c b/Python/dynload_aix.c
index 7a604f2..8d56d7d 100644
--- a/Python/dynload_aix.c
+++ b/Python/dynload_aix.c
@@ -4,10 +4,10 @@
#include "Python.h"
#include "importdl.h"
-#include <ctype.h> /* for isdigit() */
-#include <errno.h> /* for global errno */
-#include <string.h> /* for strerror() */
-#include <stdlib.h> /* for malloc(), free() */
+#include <ctype.h> /* for isdigit() */
+#include <errno.h> /* for global errno */
+#include <string.h> /* for strerror() */
+#include <stdlib.h> /* for malloc(), free() */
#include <sys/ldr.h>
@@ -22,85 +22,85 @@
extern char *Py_GetProgramName(void);
typedef struct Module {
- struct Module *next;
- void *entry;
+ struct Module *next;
+ void *entry;
} Module, *ModulePtr;
const struct filedescr _PyImport_DynLoadFiletab[] = {
- {".so", "rb", C_EXTENSION},
- {"module.so", "rb", C_EXTENSION},
- {0, 0}
+ {".so", "rb", C_EXTENSION},
+ {"module.so", "rb", C_EXTENSION},
+ {0, 0}
};
static int
aix_getoldmodules(void **modlistptr)
{
- register ModulePtr modptr, prevmodptr;
- register struct ld_info *ldiptr;
- register char *ldibuf;
- register int errflag, bufsize = 1024;
- register unsigned int offset;
- char *progname = Py_GetProgramName();
-
- /*
- -- Get the list of loaded modules into ld_info structures.
- */
- if ((ldibuf = malloc(bufsize)) == NULL) {
- PyErr_SetString(PyExc_ImportError, strerror(errno));
- return -1;
- }
- while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
- && errno == ENOMEM) {
- free(ldibuf);
- bufsize += 1024;
- if ((ldibuf = malloc(bufsize)) == NULL) {
- PyErr_SetString(PyExc_ImportError, strerror(errno));
- return -1;
- }
- }
- if (errflag == -1) {
- PyErr_SetString(PyExc_ImportError, strerror(errno));
- return -1;
- }
- /*
- -- Make the modules list from the ld_info structures.
- */
- ldiptr = (struct ld_info *)ldibuf;
- prevmodptr = NULL;
- do {
- if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
- strstr(ldiptr->ldinfo_filename, "python") == NULL) {
- /*
- -- Extract only the modules belonging to the main
- -- executable + those containing "python" as a
- -- substring (like the "python[version]" binary or
- -- "libpython[version].a" in case it's a shared lib).
- */
- offset = (unsigned int)ldiptr->ldinfo_next;
- ldiptr = (struct ld_info *)((char*)ldiptr + offset);
- continue;
- }
- if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
- PyErr_SetString(PyExc_ImportError, strerror(errno));
- while (*modlistptr) {
- modptr = (ModulePtr)*modlistptr;
- *modlistptr = (void *)modptr->next;
- free(modptr);
- }
- return -1;
- }
- modptr->entry = ldiptr->ldinfo_dataorg;
- modptr->next = NULL;
- if (prevmodptr == NULL)
- *modlistptr = (void *)modptr;
- else
- prevmodptr->next = modptr;
- prevmodptr = modptr;
- offset = (unsigned int)ldiptr->ldinfo_next;
- ldiptr = (struct ld_info *)((char*)ldiptr + offset);
- } while (offset);
- free(ldibuf);
- return 0;
+ register ModulePtr modptr, prevmodptr;
+ register struct ld_info *ldiptr;
+ register char *ldibuf;
+ register int errflag, bufsize = 1024;
+ register unsigned int offset;
+ char *progname = Py_GetProgramName();
+
+ /*
+ -- Get the list of loaded modules into ld_info structures.
+ */
+ if ((ldibuf = malloc(bufsize)) == NULL) {
+ PyErr_SetString(PyExc_ImportError, strerror(errno));
+ return -1;
+ }
+ while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
+ && errno == ENOMEM) {
+ free(ldibuf);
+ bufsize += 1024;
+ if ((ldibuf = malloc(bufsize)) == NULL) {
+ PyErr_SetString(PyExc_ImportError, strerror(errno));
+ return -1;
+ }
+ }
+ if (errflag == -1) {
+ PyErr_SetString(PyExc_ImportError, strerror(errno));
+ return -1;
+ }
+ /*
+ -- Make the modules list from the ld_info structures.
+ */
+ ldiptr = (struct ld_info *)ldibuf;
+ prevmodptr = NULL;
+ do {
+ if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
+ strstr(ldiptr->ldinfo_filename, "python") == NULL) {
+ /*
+ -- Extract only the modules belonging to the main
+ -- executable + those containing "python" as a
+ -- substring (like the "python[version]" binary or
+ -- "libpython[version].a" in case it's a shared lib).
+ */
+ offset = (unsigned int)ldiptr->ldinfo_next;
+ ldiptr = (struct ld_info *)((char*)ldiptr + offset);
+ continue;
+ }
+ if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
+ PyErr_SetString(PyExc_ImportError, strerror(errno));
+ while (*modlistptr) {
+ modptr = (ModulePtr)*modlistptr;
+ *modlistptr = (void *)modptr->next;
+ free(modptr);
+ }
+ return -1;
+ }
+ modptr->entry = ldiptr->ldinfo_dataorg;
+ modptr->next = NULL;
+ if (prevmodptr == NULL)
+ *modlistptr = (void *)modptr;
+ else
+ prevmodptr->next = modptr;
+ prevmodptr = modptr;
+ offset = (unsigned int)ldiptr->ldinfo_next;
+ ldiptr = (struct ld_info *)((char*)ldiptr + offset);
+ } while (offset);
+ free(ldibuf);
+ return 0;
}
@@ -108,76 +108,76 @@ static void
aix_loaderror(const char *pathname)
{
- char *message[1024], errbuf[1024];
- register int i,j;
-
- struct errtab {
- int errNo;
- char *errstr;
- } load_errtab[] = {
- {L_ERROR_TOOMANY, "too many errors, rest skipped."},
- {L_ERROR_NOLIB, "can't load library:"},
- {L_ERROR_UNDEF, "can't find symbol in library:"},
- {L_ERROR_RLDBAD,
- "RLD index out of range or bad relocation type:"},
- {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
- {L_ERROR_MEMBER,
- "file not an archive or does not contain requested member:"},
- {L_ERROR_TYPE, "symbol table mismatch:"},
- {L_ERROR_ALIGN, "text alignment in file is wrong."},
- {L_ERROR_SYSTEM, "System error:"},
- {L_ERROR_ERRNO, NULL}
- };
-
-#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
+ char *message[1024], errbuf[1024];
+ register int i,j;
+
+ struct errtab {
+ int errNo;
+ char *errstr;
+ } load_errtab[] = {
+ {L_ERROR_TOOMANY, "too many errors, rest skipped."},
+ {L_ERROR_NOLIB, "can't load library:"},
+ {L_ERROR_UNDEF, "can't find symbol in library:"},
+ {L_ERROR_RLDBAD,
+ "RLD index out of range or bad relocation type:"},
+ {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
+ {L_ERROR_MEMBER,
+ "file not an archive or does not contain requested member:"},
+ {L_ERROR_TYPE, "symbol table mismatch:"},
+ {L_ERROR_ALIGN, "text alignment in file is wrong."},
+ {L_ERROR_SYSTEM, "System error:"},
+ {L_ERROR_ERRNO, NULL}
+ };
+
+#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
- PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
-
- if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
- ERRBUF_APPEND(strerror(errno));
- ERRBUF_APPEND("\n");
- }
- for(i = 0; message[i] && *message[i]; i++) {
- int nerr = atoi(message[i]);
- for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
- if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
- ERRBUF_APPEND(load_errtab[j].errstr);
- }
- while (isdigit(Py_CHARMASK(*message[i]))) message[i]++ ;
- ERRBUF_APPEND(message[i]);
- ERRBUF_APPEND("\n");
- }
- errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
- PyErr_SetString(PyExc_ImportError, errbuf);
- return;
+ PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
+
+ if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
+ ERRBUF_APPEND(strerror(errno));
+ ERRBUF_APPEND("\n");
+ }
+ for(i = 0; message[i] && *message[i]; i++) {
+ int nerr = atoi(message[i]);
+ for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
+ if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
+ ERRBUF_APPEND(load_errtab[j].errstr);
+ }
+ while (isdigit(Py_CHARMASK(*message[i]))) message[i]++ ;
+ ERRBUF_APPEND(message[i]);
+ ERRBUF_APPEND("\n");
+ }
+ errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
+ PyErr_SetString(PyExc_ImportError, errbuf);
+ return;
}
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p;
-
- /*
- -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
- -- of the shared module unresolved. Thus we have to resolve them
- -- explicitly with loadbind. The new module is loaded, then we
- -- resolve its symbols using the list of already loaded modules
- -- (only those that belong to the python executable). Get these
- -- with loadquery(L_GETINFO).
- */
-
- static void *staticmodlistptr = NULL;
-
- if (!staticmodlistptr)
- if (aix_getoldmodules(&staticmodlistptr) == -1)
- return NULL;
- p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
- if (p == NULL) {
- aix_loaderror(pathname);
- return NULL;
- }
-
- return p;
+ dl_funcptr p;
+
+ /*
+ -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
+ -- of the shared module unresolved. Thus we have to resolve them
+ -- explicitly with loadbind. The new module is loaded, then we
+ -- resolve its symbols using the list of already loaded modules
+ -- (only those that belong to the python executable). Get these
+ -- with loadquery(L_GETINFO).
+ */
+
+ static void *staticmodlistptr = NULL;
+
+ if (!staticmodlistptr)
+ if (aix_getoldmodules(&staticmodlistptr) == -1)
+ return NULL;
+ p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
+ if (p == NULL) {
+ aix_loaderror(pathname);
+ return NULL;
+ }
+
+ return p;
}
diff --git a/Python/dynload_atheos.c b/Python/dynload_atheos.c
index b01fdfa..396a35b 100644
--- a/Python/dynload_atheos.c
+++ b/Python/dynload_atheos.c
@@ -9,39 +9,39 @@
const struct filedescr _PyImport_DynLoadFiletab[] = {
- {".so", "rb", C_EXTENSION},
- {"module.so", "rb", C_EXTENSION},
- {0, 0}
+ {".so", "rb", C_EXTENSION},
+ {"module.so", "rb", C_EXTENSION},
+ {0, 0}
};
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- void *p;
- int lib;
- char funcname[258];
-
- if (Py_VerboseFlag)
- printf("load_library %s\n", pathname);
-
- lib = load_library(pathname, 0);
- if (lib < 0) {
- char buf[512];
- if (Py_VerboseFlag)
- perror(pathname);
- PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s: %.200s",
- pathname, strerror(errno));
- PyErr_SetString(PyExc_ImportError, buf);
- return NULL;
- }
- PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
- if (Py_VerboseFlag)
- printf("get_symbol_address %s\n", funcname);
- if (get_symbol_address(lib, funcname, -1, &p) < 0) {
- p = NULL;
- if (Py_VerboseFlag)
- perror(funcname);
- }
-
- return (dl_funcptr) p;
+ void *p;
+ int lib;
+ char funcname[258];
+
+ if (Py_VerboseFlag)
+ printf("load_library %s\n", pathname);
+
+ lib = load_library(pathname, 0);
+ if (lib < 0) {
+ char buf[512];
+ if (Py_VerboseFlag)
+ perror(pathname);
+ PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s: %.200s",
+ pathname, strerror(errno));
+ PyErr_SetString(PyExc_ImportError, buf);
+ return NULL;
+ }
+ PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
+ if (Py_VerboseFlag)
+ printf("get_symbol_address %s\n", funcname);
+ if (get_symbol_address(lib, funcname, -1, &p) < 0) {
+ p = NULL;
+ if (Py_VerboseFlag)
+ perror(funcname);
+ }
+
+ return (dl_funcptr) p;
}
diff --git a/Python/dynload_hpux.c b/Python/dynload_hpux.c
index 51069db..18a8137 100644
--- a/Python/dynload_hpux.c
+++ b/Python/dynload_hpux.c
@@ -14,45 +14,45 @@
#endif
const struct filedescr _PyImport_DynLoadFiletab[] = {
- {SHLIB_EXT, "rb", C_EXTENSION},
- {"module"SHLIB_EXT, "rb", C_EXTENSION},
- {0, 0}
+ {SHLIB_EXT, "rb", C_EXTENSION},
+ {"module"SHLIB_EXT, "rb", C_EXTENSION},
+ {0, 0}
};
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p;
- shl_t lib;
- int flags;
- char funcname[258];
-
- flags = BIND_FIRST | BIND_DEFERRED;
- if (Py_VerboseFlag) {
- flags = BIND_FIRST | BIND_IMMEDIATE |
- BIND_NONFATAL | BIND_VERBOSE;
- printf("shl_load %s\n",pathname);
- }
- lib = shl_load(pathname, flags, 0);
- /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
- if (lib == NULL) {
- char buf[256];
- if (Py_VerboseFlag)
- perror(pathname);
- PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
- pathname);
- PyErr_SetString(PyExc_ImportError, buf);
- return NULL;
- }
- PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname);
- if (Py_VerboseFlag)
- printf("shl_findsym %s\n", funcname);
- if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
- shl_unload(lib);
- p = NULL;
- }
- if (p == NULL && Py_VerboseFlag)
- perror(funcname);
-
- return p;
+ dl_funcptr p;
+ shl_t lib;
+ int flags;
+ char funcname[258];
+
+ flags = BIND_FIRST | BIND_DEFERRED;
+ if (Py_VerboseFlag) {
+ flags = BIND_FIRST | BIND_IMMEDIATE |
+ BIND_NONFATAL | BIND_VERBOSE;
+ printf("shl_load %s\n",pathname);
+ }
+ lib = shl_load(pathname, flags, 0);
+ /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
+ if (lib == NULL) {
+ char buf[256];
+ if (Py_VerboseFlag)
+ perror(pathname);
+ PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
+ pathname);
+ PyErr_SetString(PyExc_ImportError, buf);
+ return NULL;
+ }
+ PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname);
+ if (Py_VerboseFlag)
+ printf("shl_findsym %s\n", funcname);
+ if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
+ shl_unload(lib);
+ p = NULL;
+ }
+ if (p == NULL && Py_VerboseFlag)
+ perror(funcname);
+
+ return p;
}
diff --git a/Python/dynload_next.c b/Python/dynload_next.c
index de4f9ae..cabf9b9 100644
--- a/Python/dynload_next.c
+++ b/Python/dynload_next.c
@@ -9,9 +9,9 @@
#include <mach-o/dyld.h>
const struct filedescr _PyImport_DynLoadFiletab[] = {
- {".so", "rb", C_EXTENSION},
- {"module.so", "rb", C_EXTENSION},
- {0, 0}
+ {".so", "rb", C_EXTENSION},
+ {"module.so", "rb", C_EXTENSION},
+ {0, 0}
};
/*
@@ -29,86 +29,86 @@ const struct filedescr _PyImport_DynLoadFiletab[] = {
#define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR
#else
#define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW| \
- NSLINKMODULE_OPTION_RETURN_ON_ERROR|NSLINKMODULE_OPTION_PRIVATE
+ NSLINKMODULE_OPTION_RETURN_ON_ERROR|NSLINKMODULE_OPTION_PRIVATE
#endif
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p = NULL;
- char funcname[258];
- NSObjectFileImageReturnCode rc;
- NSObjectFileImage image;
- NSModule newModule;
- NSSymbol theSym;
- const char *errString;
- char errBuf[512];
+ dl_funcptr p = NULL;
+ char funcname[258];
+ NSObjectFileImageReturnCode rc;
+ NSObjectFileImage image;
+ NSModule newModule;
+ NSSymbol theSym;
+ const char *errString;
+ char errBuf[512];
- PyOS_snprintf(funcname, sizeof(funcname), "_PyInit_%.200s", shortname);
+ PyOS_snprintf(funcname, sizeof(funcname), "_PyInit_%.200s", shortname);
#ifdef USE_DYLD_GLOBAL_NAMESPACE
- if (NSIsSymbolNameDefined(funcname)) {
- theSym = NSLookupAndBindSymbol(funcname);
- p = (dl_funcptr)NSAddressOfSymbol(theSym);
- return p;
- }
+ if (NSIsSymbolNameDefined(funcname)) {
+ theSym = NSLookupAndBindSymbol(funcname);
+ p = (dl_funcptr)NSAddressOfSymbol(theSym);
+ return p;
+ }
#endif
- rc = NSCreateObjectFileImageFromFile(pathname, &image);
- switch(rc) {
- default:
- case NSObjectFileImageFailure:
- case NSObjectFileImageFormat:
- /* for these a message is printed on stderr by dyld */
- errString = "Can't create object file image";
- break;
- case NSObjectFileImageSuccess:
- errString = NULL;
- break;
- case NSObjectFileImageInappropriateFile:
- errString = "Inappropriate file type for dynamic loading";
- break;
- case NSObjectFileImageArch:
- errString = "Wrong CPU type in object file";
- break;
- case NSObjectFileImageAccess:
- errString = "Can't read object file (no access)";
- break;
- }
- if (errString == NULL) {
- newModule = NSLinkModule(image, pathname, LINKOPTIONS);
- if (newModule == NULL) {
- int errNo;
- const char *fileName, *moreErrorStr;
- NSLinkEditErrors c;
- NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr );
- PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s",
- fileName, moreErrorStr);
- errString = errBuf;
- }
- }
- if (errString != NULL) {
- PyErr_SetString(PyExc_ImportError, errString);
- return NULL;
- }
+ rc = NSCreateObjectFileImageFromFile(pathname, &image);
+ switch(rc) {
+ default:
+ case NSObjectFileImageFailure:
+ case NSObjectFileImageFormat:
+ /* for these a message is printed on stderr by dyld */
+ errString = "Can't create object file image";
+ break;
+ case NSObjectFileImageSuccess:
+ errString = NULL;
+ break;
+ case NSObjectFileImageInappropriateFile:
+ errString = "Inappropriate file type for dynamic loading";
+ break;
+ case NSObjectFileImageArch:
+ errString = "Wrong CPU type in object file";
+ break;
+ case NSObjectFileImageAccess:
+ errString = "Can't read object file (no access)";
+ break;
+ }
+ if (errString == NULL) {
+ newModule = NSLinkModule(image, pathname, LINKOPTIONS);
+ if (newModule == NULL) {
+ int errNo;
+ const char *fileName, *moreErrorStr;
+ NSLinkEditErrors c;
+ NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr );
+ PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s",
+ fileName, moreErrorStr);
+ errString = errBuf;
+ }
+ }
+ if (errString != NULL) {
+ PyErr_SetString(PyExc_ImportError, errString);
+ return NULL;
+ }
#ifdef USE_DYLD_GLOBAL_NAMESPACE
- if (!NSIsSymbolNameDefined(funcname)) {
- /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
- /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
- PyErr_Format(PyExc_ImportError,
- "Loaded module does not contain symbol %.200s",
- funcname);
- return NULL;
- }
- theSym = NSLookupAndBindSymbol(funcname);
+ if (!NSIsSymbolNameDefined(funcname)) {
+ /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
+ /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
+ PyErr_Format(PyExc_ImportError,
+ "Loaded module does not contain symbol %.200s",
+ funcname);
+ return NULL;
+ }
+ theSym = NSLookupAndBindSymbol(funcname);
#else
- theSym = NSLookupSymbolInModule(newModule, funcname);
- if ( theSym == NULL ) {
- /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
- PyErr_Format(PyExc_ImportError,
- "Loaded module does not contain symbol %.200s",
- funcname);
- return NULL;
- }
+ theSym = NSLookupSymbolInModule(newModule, funcname);
+ if ( theSym == NULL ) {
+ /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
+ PyErr_Format(PyExc_ImportError,
+ "Loaded module does not contain symbol %.200s",
+ funcname);
+ return NULL;
+ }
#endif
- p = (dl_funcptr)NSAddressOfSymbol(theSym);
- return p;
+ p = (dl_funcptr)NSAddressOfSymbol(theSym);
+ return p;
}
diff --git a/Python/dynload_os2.c b/Python/dynload_os2.c
index afa14ea..101c024 100644
--- a/Python/dynload_os2.c
+++ b/Python/dynload_os2.c
@@ -10,37 +10,37 @@
const struct filedescr _PyImport_DynLoadFiletab[] = {
- {".pyd", "rb", C_EXTENSION},
- {".dll", "rb", C_EXTENSION},
- {0, 0}
+ {".pyd", "rb", C_EXTENSION},
+ {".dll", "rb", C_EXTENSION},
+ {0, 0}
};
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p;
- APIRET rc;
- HMODULE hDLL;
- char failreason[256];
- char funcname[258];
-
- rc = DosLoadModule(failreason,
- sizeof(failreason),
- pathname,
- &hDLL);
-
- if (rc != NO_ERROR) {
- char errBuf[256];
- PyOS_snprintf(errBuf, sizeof(errBuf),
- "DLL load failed, rc = %d: %.200s",
- rc, failreason);
- PyErr_SetString(PyExc_ImportError, errBuf);
- return NULL;
- }
-
- PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
- rc = DosQueryProcAddr(hDLL, 0L, funcname, &p);
- if (rc != NO_ERROR)
- p = NULL; /* Signify Failure to Acquire Entrypoint */
- return p;
+ dl_funcptr p;
+ APIRET rc;
+ HMODULE hDLL;
+ char failreason[256];
+ char funcname[258];
+
+ rc = DosLoadModule(failreason,
+ sizeof(failreason),
+ pathname,
+ &hDLL);
+
+ if (rc != NO_ERROR) {
+ char errBuf[256];
+ PyOS_snprintf(errBuf, sizeof(errBuf),
+ "DLL load failed, rc = %d: %.200s",
+ rc, failreason);
+ PyErr_SetString(PyExc_ImportError, errBuf);
+ return NULL;
+ }
+
+ PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
+ rc = DosQueryProcAddr(hDLL, 0L, funcname, &p);
+ if (rc != NO_ERROR)
+ p = NULL; /* Signify Failure to Acquire Entrypoint */
+ return p;
}
diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c
index ac8cd42..87dae27 100644
--- a/Python/dynload_shlib.c
+++ b/Python/dynload_shlib.c
@@ -33,111 +33,111 @@
const struct filedescr _PyImport_DynLoadFiletab[] = {
#ifdef __CYGWIN__
- {".dll", "rb", C_EXTENSION},
- {"module.dll", "rb", C_EXTENSION},
+ {".dll", "rb", C_EXTENSION},
+ {"module.dll", "rb", C_EXTENSION},
#else
#if defined(PYOS_OS2) && defined(PYCC_GCC)
- {".pyd", "rb", C_EXTENSION},
- {".dll", "rb", C_EXTENSION},
+ {".pyd", "rb", C_EXTENSION},
+ {".dll", "rb", C_EXTENSION},
#else
#ifdef __VMS
- {".exe", "rb", C_EXTENSION},
- {".EXE", "rb", C_EXTENSION},
- {"module.exe", "rb", C_EXTENSION},
- {"MODULE.EXE", "rb", C_EXTENSION},
+ {".exe", "rb", C_EXTENSION},
+ {".EXE", "rb", C_EXTENSION},
+ {"module.exe", "rb", C_EXTENSION},
+ {"MODULE.EXE", "rb", C_EXTENSION},
#else
- {".so", "rb", C_EXTENSION},
- {"module.so", "rb", C_EXTENSION},
+ {".so", "rb", C_EXTENSION},
+ {"module.so", "rb", C_EXTENSION},
#endif
#endif
#endif
- {0, 0}
+ {0, 0}
};
static struct {
- dev_t dev;
+ dev_t dev;
#ifdef __VMS
- ino_t ino[3];
+ ino_t ino[3];
#else
- ino_t ino;
+ ino_t ino;
#endif
- void *handle;
+ void *handle;
} handles[128];
static int nhandles = 0;
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p;
- void *handle;
- char funcname[258];
- char pathbuf[260];
- int dlopenflags=0;
-
- if (strchr(pathname, '/') == NULL) {
- /* Prefix bare filename with "./" */
- PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
- pathname = pathbuf;
- }
-
- PyOS_snprintf(funcname, sizeof(funcname),
- LEAD_UNDERSCORE "PyInit_%.200s", shortname);
-
- if (fp != NULL) {
- int i;
- struct stat statb;
- fstat(fileno(fp), &statb);
- for (i = 0; i < nhandles; i++) {
- if (statb.st_dev == handles[i].dev &&
- statb.st_ino == handles[i].ino) {
- p = (dl_funcptr) dlsym(handles[i].handle,
- funcname);
- return p;
- }
- }
- if (nhandles < 128) {
- handles[nhandles].dev = statb.st_dev;
+ dl_funcptr p;
+ void *handle;
+ char funcname[258];
+ char pathbuf[260];
+ int dlopenflags=0;
+
+ if (strchr(pathname, '/') == NULL) {
+ /* Prefix bare filename with "./" */
+ PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
+ pathname = pathbuf;
+ }
+
+ PyOS_snprintf(funcname, sizeof(funcname),
+ LEAD_UNDERSCORE "PyInit_%.200s", shortname);
+
+ if (fp != NULL) {
+ int i;
+ struct stat statb;
+ fstat(fileno(fp), &statb);
+ for (i = 0; i < nhandles; i++) {
+ if (statb.st_dev == handles[i].dev &&
+ statb.st_ino == handles[i].ino) {
+ p = (dl_funcptr) dlsym(handles[i].handle,
+ funcname);
+ return p;
+ }
+ }
+ if (nhandles < 128) {
+ handles[nhandles].dev = statb.st_dev;
#ifdef __VMS
- handles[nhandles].ino[0] = statb.st_ino[0];
- handles[nhandles].ino[1] = statb.st_ino[1];
- handles[nhandles].ino[2] = statb.st_ino[2];
+ handles[nhandles].ino[0] = statb.st_ino[0];
+ handles[nhandles].ino[1] = statb.st_ino[1];
+ handles[nhandles].ino[2] = statb.st_ino[2];
#else
- handles[nhandles].ino = statb.st_ino;
+ handles[nhandles].ino = statb.st_ino;
#endif
- }
- }
+ }
+ }
#if !(defined(PYOS_OS2) && defined(PYCC_GCC))
- dlopenflags = PyThreadState_GET()->interp->dlopenflags;
+ dlopenflags = PyThreadState_GET()->interp->dlopenflags;
#endif
- if (Py_VerboseFlag)
- PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname,
- dlopenflags);
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname,
+ dlopenflags);
#ifdef __VMS
- /* VMS currently don't allow a pathname, use a logical name instead */
- /* Concatenate 'python_module_' and shortname */
- /* so "import vms.bar" will use the logical python_module_bar */
- /* As C module use only one name space this is probably not a */
- /* important limitation */
- PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
- shortname);
- pathname = pathbuf;
+ /* VMS currently don't allow a pathname, use a logical name instead */
+ /* Concatenate 'python_module_' and shortname */
+ /* so "import vms.bar" will use the logical python_module_bar */
+ /* As C module use only one name space this is probably not a */
+ /* important limitation */
+ PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
+ shortname);
+ pathname = pathbuf;
#endif
- handle = dlopen(pathname, dlopenflags);
-
- if (handle == NULL) {
- const char *error = dlerror();
- if (error == NULL)
- error = "unknown dlopen() error";
- PyErr_SetString(PyExc_ImportError, error);
- return NULL;
- }
- if (fp != NULL && nhandles < 128)
- handles[nhandles++].handle = handle;
- p = (dl_funcptr) dlsym(handle, funcname);
- return p;
+ handle = dlopen(pathname, dlopenflags);
+
+ if (handle == NULL) {
+ const char *error = dlerror();
+ if (error == NULL)
+ error = "unknown dlopen() error";
+ PyErr_SetString(PyExc_ImportError, error);
+ return NULL;
+ }
+ if (fp != NULL && nhandles < 128)
+ handles[nhandles++].handle = handle;
+ p = (dl_funcptr) dlsym(handle, funcname);
+ return p;
}
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 61c5664..e7d61ce 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -17,11 +17,11 @@ void _Py_DeactivateActCtx(ULONG_PTR cookie);
const struct filedescr _PyImport_DynLoadFiletab[] = {
#ifdef _DEBUG
- {"_d.pyd", "rb", C_EXTENSION},
+ {"_d.pyd", "rb", C_EXTENSION},
#else
- {".pyd", "rb", C_EXTENSION},
+ {".pyd", "rb", C_EXTENSION},
#endif
- {0, 0}
+ {0, 0}
};
@@ -29,18 +29,18 @@ const struct filedescr _PyImport_DynLoadFiletab[] = {
C RTL implementations */
static int strcasecmp (char *string1, char *string2)
-{
- int first, second;
+{
+ int first, second;
- do {
- first = tolower(*string1);
- second = tolower(*string2);
- string1++;
- string2++;
- } while (first && first == second);
+ do {
+ first = tolower(*string1);
+ second = tolower(*string2);
+ string1++;
+ string2++;
+ } while (first && first == second);
- return (first - second);
-}
+ return (first - second);
+}
/* Function to return the name of the "python" DLL that the supplied module
@@ -66,213 +66,213 @@ static int strcasecmp (char *string1, char *string2)
static char *GetPythonImport (HINSTANCE hModule)
{
- unsigned char *dllbase, *import_data, *import_name;
- DWORD pe_offset, opt_offset;
- WORD opt_magic;
- int num_dict_off, import_off;
-
- /* Safety check input */
- if (hModule == NULL) {
- return NULL;
- }
-
- /* Module instance is also the base load address. First portion of
- memory is the MS-DOS loader, which holds the offset to the PE
- header (from the load base) at 0x3C */
- dllbase = (unsigned char *)hModule;
- pe_offset = DWORD_AT(dllbase + 0x3C);
-
- /* The PE signature must be "PE\0\0" */
- if (memcmp(dllbase+pe_offset,"PE\0\0",4)) {
- return NULL;
- }
-
- /* Following the PE signature is the standard COFF header (20
- bytes) and then the optional header. The optional header starts
- with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
- uses 64-bits for some fields). It might also be 0x107 for a ROM
- image, but we don't process that here.
-
- The optional header ends with a data dictionary that directly
- points to certain types of data, among them the import entries
- (in the second table entry). Based on the header type, we
- determine offsets for the data dictionary count and the entry
- within the dictionary pointing to the imports. */
-
- opt_offset = pe_offset + 4 + 20;
- opt_magic = WORD_AT(dllbase+opt_offset);
- if (opt_magic == 0x10B) {
- /* PE32 */
- num_dict_off = 92;
- import_off = 104;
- } else if (opt_magic == 0x20B) {
- /* PE32+ */
- num_dict_off = 108;
- import_off = 120;
- } else {
- /* Unsupported */
- return NULL;
- }
-
- /* Now if an import table exists, offset to it and walk the list of
- imports. The import table is an array (ending when an entry has
- empty values) of structures (20 bytes each), which contains (at
- offset 12) a relative address (to the module base) at which a
- string constant holding the import name is located. */
-
- if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
- /* We have at least 2 tables - the import table is the second
- one. But still it may be that the table size is zero */
- if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
- return NULL;
- import_data = dllbase + DWORD_AT(dllbase +
- opt_offset +
- import_off);
- while (DWORD_AT(import_data)) {
- import_name = dllbase + DWORD_AT(import_data+12);
- if (strlen(import_name) >= 6 &&
- !strncmp(import_name,"python",6)) {
- char *pch;
-
- /* Ensure python prefix is followed only
- by numbers to the end of the basename */
- pch = import_name + 6;
+ unsigned char *dllbase, *import_data, *import_name;
+ DWORD pe_offset, opt_offset;
+ WORD opt_magic;
+ int num_dict_off, import_off;
+
+ /* Safety check input */
+ if (hModule == NULL) {
+ return NULL;
+ }
+
+ /* Module instance is also the base load address. First portion of
+ memory is the MS-DOS loader, which holds the offset to the PE
+ header (from the load base) at 0x3C */
+ dllbase = (unsigned char *)hModule;
+ pe_offset = DWORD_AT(dllbase + 0x3C);
+
+ /* The PE signature must be "PE\0\0" */
+ if (memcmp(dllbase+pe_offset,"PE\0\0",4)) {
+ return NULL;
+ }
+
+ /* Following the PE signature is the standard COFF header (20
+ bytes) and then the optional header. The optional header starts
+ with a magic value of 0x10B for PE32 or 0x20B for PE32+ (PE32+
+ uses 64-bits for some fields). It might also be 0x107 for a ROM
+ image, but we don't process that here.
+
+ The optional header ends with a data dictionary that directly
+ points to certain types of data, among them the import entries
+ (in the second table entry). Based on the header type, we
+ determine offsets for the data dictionary count and the entry
+ within the dictionary pointing to the imports. */
+
+ opt_offset = pe_offset + 4 + 20;
+ opt_magic = WORD_AT(dllbase+opt_offset);
+ if (opt_magic == 0x10B) {
+ /* PE32 */
+ num_dict_off = 92;
+ import_off = 104;
+ } else if (opt_magic == 0x20B) {
+ /* PE32+ */
+ num_dict_off = 108;
+ import_off = 120;
+ } else {
+ /* Unsupported */
+ return NULL;
+ }
+
+ /* Now if an import table exists, offset to it and walk the list of
+ imports. The import table is an array (ending when an entry has
+ empty values) of structures (20 bytes each), which contains (at
+ offset 12) a relative address (to the module base) at which a
+ string constant holding the import name is located. */
+
+ if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
+ /* We have at least 2 tables - the import table is the second
+ one. But still it may be that the table size is zero */
+ if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
+ return NULL;
+ import_data = dllbase + DWORD_AT(dllbase +
+ opt_offset +
+ import_off);
+ while (DWORD_AT(import_data)) {
+ import_name = dllbase + DWORD_AT(import_data+12);
+ if (strlen(import_name) >= 6 &&
+ !strncmp(import_name,"python",6)) {
+ char *pch;
+
+ /* Ensure python prefix is followed only
+ by numbers to the end of the basename */
+ pch = import_name + 6;
#ifdef _DEBUG
- while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
+ while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
#else
- while (*pch && *pch != '.') {
+ while (*pch && *pch != '.') {
#endif
- if (*pch >= '0' && *pch <= '9') {
- pch++;
- } else {
- pch = NULL;
- break;
- }
- }
-
- if (pch) {
- /* Found it - return the name */
- return import_name;
- }
- }
- import_data += 20;
- }
- }
-
- return NULL;
+ if (*pch >= '0' && *pch <= '9') {
+ pch++;
+ } else {
+ pch = NULL;
+ break;
+ }
+ }
+
+ if (pch) {
+ /* Found it - return the name */
+ return import_name;
+ }
+ }
+ import_data += 20;
+ }
+ }
+
+ return NULL;
}
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
- const char *pathname, FILE *fp)
+ const char *pathname, FILE *fp)
{
- dl_funcptr p;
- char funcname[258], *import_python;
-
- PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
-
- {
- HINSTANCE hDLL = NULL;
- char pathbuf[260];
- LPTSTR dummy;
- unsigned int old_mode;
- ULONG_PTR cookie = 0;
- /* We use LoadLibraryEx so Windows looks for dependent DLLs
- in directory of pathname first. However, Windows95
- can sometimes not work correctly unless the absolute
- path is used. If GetFullPathName() fails, the LoadLibrary
- will certainly fail too, so use its error code */
-
- /* Don't display a message box when Python can't load a DLL */
- old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
-
- if (GetFullPathName(pathname,
- sizeof(pathbuf),
- pathbuf,
- &dummy)) {
- ULONG_PTR cookie = _Py_ActivateActCtx();
- /* XXX This call doesn't exist in Windows CE */
- hDLL = LoadLibraryEx(pathname, NULL,
- LOAD_WITH_ALTERED_SEARCH_PATH);
- _Py_DeactivateActCtx(cookie);
- }
-
- /* restore old error mode settings */
- SetErrorMode(old_mode);
-
- if (hDLL==NULL){
- PyObject *message;
- unsigned int errorCode;
-
- /* Get an error string from Win32 error code */
- wchar_t theInfo[256]; /* Pointer to error text
- from system */
- int theLength; /* Length of error text */
-
- errorCode = GetLastError();
-
- theLength = FormatMessageW(
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */
- NULL, /* message source */
- errorCode, /* the message (error) ID */
- MAKELANGID(LANG_NEUTRAL,
- SUBLANG_DEFAULT),
- /* Default language */
- theInfo, /* the buffer */
- sizeof(theInfo), /* the buffer size */
- NULL); /* no additional format args. */
-
- /* Problem: could not get the error message.
- This should not happen if called correctly. */
- if (theLength == 0) {
- message = PyUnicode_FromFormat(
- "DLL load failed with error code %d",
- errorCode);
- } else {
- /* For some reason a \r\n
- is appended to the text */
- if (theLength >= 2 &&
- theInfo[theLength-2] == '\r' &&
- theInfo[theLength-1] == '\n') {
- theLength -= 2;
- theInfo[theLength] = '\0';
- }
- message = PyUnicode_FromString(
- "DLL load failed: ");
-
- PyUnicode_AppendAndDel(&message,
- PyUnicode_FromUnicode(
- theInfo,
- theLength));
- }
- PyErr_SetObject(PyExc_ImportError, message);
- Py_XDECREF(message);
- return NULL;
- } else {
- char buffer[256];
+ dl_funcptr p;
+ char funcname[258], *import_python;
+
+ PyOS_snprintf(funcname, sizeof(funcname), "PyInit_%.200s", shortname);
+
+ {
+ HINSTANCE hDLL = NULL;
+ char pathbuf[260];
+ LPTSTR dummy;
+ unsigned int old_mode;
+ ULONG_PTR cookie = 0;
+ /* We use LoadLibraryEx so Windows looks for dependent DLLs
+ in directory of pathname first. However, Windows95
+ can sometimes not work correctly unless the absolute
+ path is used. If GetFullPathName() fails, the LoadLibrary
+ will certainly fail too, so use its error code */
+
+ /* Don't display a message box when Python can't load a DLL */
+ old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
+
+ if (GetFullPathName(pathname,
+ sizeof(pathbuf),
+ pathbuf,
+ &dummy)) {
+ ULONG_PTR cookie = _Py_ActivateActCtx();
+ /* XXX This call doesn't exist in Windows CE */
+ hDLL = LoadLibraryEx(pathname, NULL,
+ LOAD_WITH_ALTERED_SEARCH_PATH);
+ _Py_DeactivateActCtx(cookie);
+ }
+
+ /* restore old error mode settings */
+ SetErrorMode(old_mode);
+
+ if (hDLL==NULL){
+ PyObject *message;
+ unsigned int errorCode;
+
+ /* Get an error string from Win32 error code */
+ wchar_t theInfo[256]; /* Pointer to error text
+ from system */
+ int theLength; /* Length of error text */
+
+ errorCode = GetLastError();
+
+ theLength = FormatMessageW(
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, /* flags */
+ NULL, /* message source */
+ errorCode, /* the message (error) ID */
+ MAKELANGID(LANG_NEUTRAL,
+ SUBLANG_DEFAULT),
+ /* Default language */
+ theInfo, /* the buffer */
+ sizeof(theInfo), /* the buffer size */
+ NULL); /* no additional format args. */
+
+ /* Problem: could not get the error message.
+ This should not happen if called correctly. */
+ if (theLength == 0) {
+ message = PyUnicode_FromFormat(
+ "DLL load failed with error code %d",
+ errorCode);
+ } else {
+ /* For some reason a \r\n
+ is appended to the text */
+ if (theLength >= 2 &&
+ theInfo[theLength-2] == '\r' &&
+ theInfo[theLength-1] == '\n') {
+ theLength -= 2;
+ theInfo[theLength] = '\0';
+ }
+ message = PyUnicode_FromString(
+ "DLL load failed: ");
+
+ PyUnicode_AppendAndDel(&message,
+ PyUnicode_FromUnicode(
+ theInfo,
+ theLength));
+ }
+ PyErr_SetObject(PyExc_ImportError, message);
+ Py_XDECREF(message);
+ return NULL;
+ } else {
+ char buffer[256];
#ifdef _DEBUG
- PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
+ PyOS_snprintf(buffer, sizeof(buffer), "python%d%d_d.dll",
#else
- PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
+ PyOS_snprintf(buffer, sizeof(buffer), "python%d%d.dll",
#endif
- PY_MAJOR_VERSION,PY_MINOR_VERSION);
- import_python = GetPythonImport(hDLL);
-
- if (import_python &&
- strcasecmp(buffer,import_python)) {
- PyOS_snprintf(buffer, sizeof(buffer),
- "Module use of %.150s conflicts "
- "with this version of Python.",
- import_python);
- PyErr_SetString(PyExc_ImportError,buffer);
- FreeLibrary(hDLL);
- return NULL;
- }
- }
- p = GetProcAddress(hDLL, funcname);
- }
-
- return p;
+ PY_MAJOR_VERSION,PY_MINOR_VERSION);
+ import_python = GetPythonImport(hDLL);
+
+ if (import_python &&
+ strcasecmp(buffer,import_python)) {
+ PyOS_snprintf(buffer, sizeof(buffer),
+ "Module use of %.150s conflicts "
+ "with this version of Python.",
+ import_python);
+ PyErr_SetString(PyExc_ImportError,buffer);
+ FreeLibrary(hDLL);
+ return NULL;
+ }
+ }
+ p = GetProcAddress(hDLL, funcname);
+ }
+
+ return p;
}
diff --git a/Python/errors.c b/Python/errors.c
index 2169a1a..4584205 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -24,166 +24,166 @@ extern "C" {
void
PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *oldtype, *oldvalue, *oldtraceback;
-
- if (traceback != NULL && !PyTraceBack_Check(traceback)) {
- /* XXX Should never happen -- fatal error instead? */
- /* Well, it could be None. */
- Py_DECREF(traceback);
- traceback = NULL;
- }
-
- /* Save these in locals to safeguard against recursive
- invocation through Py_XDECREF */
- oldtype = tstate->curexc_type;
- oldvalue = tstate->curexc_value;
- oldtraceback = tstate->curexc_traceback;
-
- tstate->curexc_type = type;
- tstate->curexc_value = value;
- tstate->curexc_traceback = traceback;
-
- Py_XDECREF(oldtype);
- Py_XDECREF(oldvalue);
- Py_XDECREF(oldtraceback);
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *oldtype, *oldvalue, *oldtraceback;
+
+ if (traceback != NULL && !PyTraceBack_Check(traceback)) {
+ /* XXX Should never happen -- fatal error instead? */
+ /* Well, it could be None. */
+ Py_DECREF(traceback);
+ traceback = NULL;
+ }
+
+ /* Save these in locals to safeguard against recursive
+ invocation through Py_XDECREF */
+ oldtype = tstate->curexc_type;
+ oldvalue = tstate->curexc_value;
+ oldtraceback = tstate->curexc_traceback;
+
+ tstate->curexc_type = type;
+ tstate->curexc_value = value;
+ tstate->curexc_traceback = traceback;
+
+ Py_XDECREF(oldtype);
+ Py_XDECREF(oldvalue);
+ Py_XDECREF(oldtraceback);
}
void
PyErr_SetObject(PyObject *exception, PyObject *value)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *exc_value;
- PyObject *tb = NULL;
-
- if (exception != NULL &&
- !PyExceptionClass_Check(exception)) {
- PyErr_Format(PyExc_SystemError,
- "exception %R not a BaseException subclass",
- exception);
- return;
- }
- Py_XINCREF(value);
- exc_value = tstate->exc_value;
- if (exc_value != NULL && exc_value != Py_None) {
- /* Implicit exception chaining */
- Py_INCREF(exc_value);
- if (value == NULL || !PyExceptionInstance_Check(value)) {
- /* We must normalize the value right now */
- PyObject *args, *fixed_value;
- if (value == NULL || value == Py_None)
- args = PyTuple_New(0);
- else if (PyTuple_Check(value)) {
- Py_INCREF(value);
- args = value;
- }
- else
- args = PyTuple_Pack(1, value);
- fixed_value = args ?
- PyEval_CallObject(exception, args) : NULL;
- Py_XDECREF(args);
- Py_XDECREF(value);
- if (fixed_value == NULL)
- return;
- value = fixed_value;
- }
- /* Avoid reference cycles through the context chain.
- This is O(chain length) but context chains are
- usually very short. Sensitive readers may try
- to inline the call to PyException_GetContext. */
- if (exc_value != value) {
- PyObject *o = exc_value, *context;
- while ((context = PyException_GetContext(o))) {
- Py_DECREF(context);
- if (context == value) {
- PyException_SetContext(o, NULL);
- break;
- }
- o = context;
- }
- PyException_SetContext(value, exc_value);
- } else {
- Py_DECREF(exc_value);
- }
- }
- if (value != NULL && PyExceptionInstance_Check(value))
- tb = PyException_GetTraceback(value);
- Py_XINCREF(exception);
- PyErr_Restore(exception, value, tb);
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *exc_value;
+ PyObject *tb = NULL;
+
+ if (exception != NULL &&
+ !PyExceptionClass_Check(exception)) {
+ PyErr_Format(PyExc_SystemError,
+ "exception %R not a BaseException subclass",
+ exception);
+ return;
+ }
+ Py_XINCREF(value);
+ exc_value = tstate->exc_value;
+ if (exc_value != NULL && exc_value != Py_None) {
+ /* Implicit exception chaining */
+ Py_INCREF(exc_value);
+ if (value == NULL || !PyExceptionInstance_Check(value)) {
+ /* We must normalize the value right now */
+ PyObject *args, *fixed_value;
+ if (value == NULL || value == Py_None)
+ args = PyTuple_New(0);
+ else if (PyTuple_Check(value)) {
+ Py_INCREF(value);
+ args = value;
+ }
+ else
+ args = PyTuple_Pack(1, value);
+ fixed_value = args ?
+ PyEval_CallObject(exception, args) : NULL;
+ Py_XDECREF(args);
+ Py_XDECREF(value);
+ if (fixed_value == NULL)
+ return;
+ value = fixed_value;
+ }
+ /* Avoid reference cycles through the context chain.
+ This is O(chain length) but context chains are
+ usually very short. Sensitive readers may try
+ to inline the call to PyException_GetContext. */
+ if (exc_value != value) {
+ PyObject *o = exc_value, *context;
+ while ((context = PyException_GetContext(o))) {
+ Py_DECREF(context);
+ if (context == value) {
+ PyException_SetContext(o, NULL);
+ break;
+ }
+ o = context;
+ }
+ PyException_SetContext(value, exc_value);
+ } else {
+ Py_DECREF(exc_value);
+ }
+ }
+ if (value != NULL && PyExceptionInstance_Check(value))
+ tb = PyException_GetTraceback(value);
+ Py_XINCREF(exception);
+ PyErr_Restore(exception, value, tb);
}
void
PyErr_SetNone(PyObject *exception)
{
- PyErr_SetObject(exception, (PyObject *)NULL);
+ PyErr_SetObject(exception, (PyObject *)NULL);
}
void
PyErr_SetString(PyObject *exception, const char *string)
{
- PyObject *value = PyUnicode_FromString(string);
- PyErr_SetObject(exception, value);
- Py_XDECREF(value);
+ PyObject *value = PyUnicode_FromString(string);
+ PyErr_SetObject(exception, value);
+ Py_XDECREF(value);
}
PyObject *
PyErr_Occurred(void)
{
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = PyThreadState_GET();
- return tstate->curexc_type;
+ return tstate->curexc_type;
}
int
PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
{
- if (err == NULL || exc == NULL) {
- /* maybe caused by "import exceptions" that failed early on */
- return 0;
- }
- if (PyTuple_Check(exc)) {
- Py_ssize_t i, n;
- n = PyTuple_Size(exc);
- for (i = 0; i < n; i++) {
- /* Test recursively */
- if (PyErr_GivenExceptionMatches(
- err, PyTuple_GET_ITEM(exc, i)))
- {
- return 1;
- }
- }
- return 0;
- }
- /* err might be an instance, so check its class. */
- if (PyExceptionInstance_Check(err))
- err = PyExceptionInstance_Class(err);
-
- if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
- int res = 0;
- PyObject *exception, *value, *tb;
- PyErr_Fetch(&exception, &value, &tb);
- /* PyObject_IsSubclass() can recurse and therefore is
- not safe (see test_bad_getattr in test.pickletester). */
- res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
- /* This function must not fail, so print the error here */
- if (res == -1) {
- PyErr_WriteUnraisable(err);
- res = 0;
- }
- PyErr_Restore(exception, value, tb);
- return res;
- }
-
- return err == exc;
+ if (err == NULL || exc == NULL) {
+ /* maybe caused by "import exceptions" that failed early on */
+ return 0;
+ }
+ if (PyTuple_Check(exc)) {
+ Py_ssize_t i, n;
+ n = PyTuple_Size(exc);
+ for (i = 0; i < n; i++) {
+ /* Test recursively */
+ if (PyErr_GivenExceptionMatches(
+ err, PyTuple_GET_ITEM(exc, i)))
+ {
+ return 1;
+ }
+ }
+ return 0;
+ }
+ /* err might be an instance, so check its class. */
+ if (PyExceptionInstance_Check(err))
+ err = PyExceptionInstance_Class(err);
+
+ if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
+ int res = 0;
+ PyObject *exception, *value, *tb;
+ PyErr_Fetch(&exception, &value, &tb);
+ /* PyObject_IsSubclass() can recurse and therefore is
+ not safe (see test_bad_getattr in test.pickletester). */
+ res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
+ /* This function must not fail, so print the error here */
+ if (res == -1) {
+ PyErr_WriteUnraisable(err);
+ res = 0;
+ }
+ PyErr_Restore(exception, value, tb);
+ return res;
+ }
+
+ return err == exc;
}
int
PyErr_ExceptionMatches(PyObject *exc)
{
- return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
+ return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
}
@@ -191,128 +191,128 @@ PyErr_ExceptionMatches(PyObject *exc)
eval_code2(), do_raise(), and PyErr_Print()
XXX: should PyErr_NormalizeException() also call
- PyException_SetTraceback() with the resulting value and tb?
+ PyException_SetTraceback() with the resulting value and tb?
*/
void
PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
{
- PyObject *type = *exc;
- PyObject *value = *val;
- PyObject *inclass = NULL;
- PyObject *initial_tb = NULL;
- PyThreadState *tstate = NULL;
-
- if (type == NULL) {
- /* There was no exception, so nothing to do. */
- return;
- }
-
- /* If PyErr_SetNone() was used, the value will have been actually
- set to NULL.
- */
- if (!value) {
- value = Py_None;
- Py_INCREF(value);
- }
-
- if (PyExceptionInstance_Check(value))
- inclass = PyExceptionInstance_Class(value);
-
- /* Normalize the exception so that if the type is a class, the
- value will be an instance.
- */
- if (PyExceptionClass_Check(type)) {
- /* if the value was not an instance, or is not an instance
- whose class is (or is derived from) type, then use the
- value as an argument to instantiation of the type
- class.
- */
- if (!inclass || !PyObject_IsSubclass(inclass, type)) {
- PyObject *args, *res;
-
- if (value == Py_None)
- args = PyTuple_New(0);
- else if (PyTuple_Check(value)) {
- Py_INCREF(value);
- args = value;
- }
- else
- args = PyTuple_Pack(1, value);
-
- if (args == NULL)
- goto finally;
- res = PyEval_CallObject(type, args);
- Py_DECREF(args);
- if (res == NULL)
- goto finally;
- Py_DECREF(value);
- value = res;
- }
- /* if the class of the instance doesn't exactly match the
- class of the type, believe the instance
- */
- else if (inclass != type) {
- Py_DECREF(type);
- type = inclass;
- Py_INCREF(type);
- }
- }
- *exc = type;
- *val = value;
- return;
+ PyObject *type = *exc;
+ PyObject *value = *val;
+ PyObject *inclass = NULL;
+ PyObject *initial_tb = NULL;
+ PyThreadState *tstate = NULL;
+
+ if (type == NULL) {
+ /* There was no exception, so nothing to do. */
+ return;
+ }
+
+ /* If PyErr_SetNone() was used, the value will have been actually
+ set to NULL.
+ */
+ if (!value) {
+ value = Py_None;
+ Py_INCREF(value);
+ }
+
+ if (PyExceptionInstance_Check(value))
+ inclass = PyExceptionInstance_Class(value);
+
+ /* Normalize the exception so that if the type is a class, the
+ value will be an instance.
+ */
+ if (PyExceptionClass_Check(type)) {
+ /* if the value was not an instance, or is not an instance
+ whose class is (or is derived from) type, then use the
+ value as an argument to instantiation of the type
+ class.
+ */
+ if (!inclass || !PyObject_IsSubclass(inclass, type)) {
+ PyObject *args, *res;
+
+ if (value == Py_None)
+ args = PyTuple_New(0);
+ else if (PyTuple_Check(value)) {
+ Py_INCREF(value);
+ args = value;
+ }
+ else
+ args = PyTuple_Pack(1, value);
+
+ if (args == NULL)
+ goto finally;
+ res = PyEval_CallObject(type, args);
+ Py_DECREF(args);
+ if (res == NULL)
+ goto finally;
+ Py_DECREF(value);
+ value = res;
+ }
+ /* if the class of the instance doesn't exactly match the
+ class of the type, believe the instance
+ */
+ else if (inclass != type) {
+ Py_DECREF(type);
+ type = inclass;
+ Py_INCREF(type);
+ }
+ }
+ *exc = type;
+ *val = value;
+ return;
finally:
- Py_DECREF(type);
- Py_DECREF(value);
- /* If the new exception doesn't set a traceback and the old
- exception had a traceback, use the old traceback for the
- new exception. It's better than nothing.
- */
- initial_tb = *tb;
- PyErr_Fetch(exc, val, tb);
- if (initial_tb != NULL) {
- if (*tb == NULL)
- *tb = initial_tb;
- else
- Py_DECREF(initial_tb);
- }
- /* normalize recursively */
- tstate = PyThreadState_GET();
- if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
- --tstate->recursion_depth;
- /* throw away the old exception... */
- Py_DECREF(*exc);
- Py_DECREF(*val);
- /* ... and use the recursion error instead */
- *exc = PyExc_RuntimeError;
- *val = PyExc_RecursionErrorInst;
- Py_INCREF(*exc);
- Py_INCREF(*val);
- /* just keeping the old traceback */
- return;
- }
- PyErr_NormalizeException(exc, val, tb);
- --tstate->recursion_depth;
+ Py_DECREF(type);
+ Py_DECREF(value);
+ /* If the new exception doesn't set a traceback and the old
+ exception had a traceback, use the old traceback for the
+ new exception. It's better than nothing.
+ */
+ initial_tb = *tb;
+ PyErr_Fetch(exc, val, tb);
+ if (initial_tb != NULL) {
+ if (*tb == NULL)
+ *tb = initial_tb;
+ else
+ Py_DECREF(initial_tb);
+ }
+ /* normalize recursively */
+ tstate = PyThreadState_GET();
+ if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
+ --tstate->recursion_depth;
+ /* throw away the old exception... */
+ Py_DECREF(*exc);
+ Py_DECREF(*val);
+ /* ... and use the recursion error instead */
+ *exc = PyExc_RuntimeError;
+ *val = PyExc_RecursionErrorInst;
+ Py_INCREF(*exc);
+ Py_INCREF(*val);
+ /* just keeping the old traceback */
+ return;
+ }
+ PyErr_NormalizeException(exc, val, tb);
+ --tstate->recursion_depth;
}
void
PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
{
- PyThreadState *tstate = PyThreadState_GET();
+ PyThreadState *tstate = PyThreadState_GET();
- *p_type = tstate->curexc_type;
- *p_value = tstate->curexc_value;
- *p_traceback = tstate->curexc_traceback;
+ *p_type = tstate->curexc_type;
+ *p_value = tstate->curexc_value;
+ *p_traceback = tstate->curexc_traceback;
- tstate->curexc_type = NULL;
- tstate->curexc_value = NULL;
- tstate->curexc_traceback = NULL;
+ tstate->curexc_type = NULL;
+ tstate->curexc_value = NULL;
+ tstate->curexc_traceback = NULL;
}
void
PyErr_Clear(void)
{
- PyErr_Restore(NULL, NULL, NULL);
+ PyErr_Restore(NULL, NULL, NULL);
}
/* Convenience functions to set a type error exception and return 0 */
@@ -320,293 +320,293 @@ PyErr_Clear(void)
int
PyErr_BadArgument(void)
{
- PyErr_SetString(PyExc_TypeError,
- "bad argument type for built-in operation");
- return 0;
+ PyErr_SetString(PyExc_TypeError,
+ "bad argument type for built-in operation");
+ return 0;
}
PyObject *
PyErr_NoMemory(void)
{
- if (PyErr_ExceptionMatches(PyExc_MemoryError))
- /* already current */
- return NULL;
-
- /* raise the pre-allocated instance if it still exists */
- if (PyExc_MemoryErrorInst)
- {
- /* Clear the previous traceback, otherwise it will be appended
- * to the current one.
- *
- * The following statement is not likely to raise any error;
- * if it does, we simply discard it.
- */
- PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None);
-
- PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
- }
- else
- /* this will probably fail since there's no memory and hee,
- hee, we have to instantiate this class
- */
- PyErr_SetNone(PyExc_MemoryError);
-
- return NULL;
+ if (PyErr_ExceptionMatches(PyExc_MemoryError))
+ /* already current */
+ return NULL;
+
+ /* raise the pre-allocated instance if it still exists */
+ if (PyExc_MemoryErrorInst)
+ {
+ /* Clear the previous traceback, otherwise it will be appended
+ * to the current one.
+ *
+ * The following statement is not likely to raise any error;
+ * if it does, we simply discard it.
+ */
+ PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None);
+
+ PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
+ }
+ else
+ /* this will probably fail since there's no memory and hee,
+ hee, we have to instantiate this class
+ */
+ PyErr_SetNone(PyExc_MemoryError);
+
+ return NULL;
}
PyObject *
PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
{
- PyObject *message;
- PyObject *v;
- int i = errno;
+ PyObject *message;
+ PyObject *v;
+ int i = errno;
#ifdef PLAN9
- char errbuf[ERRMAX];
+ char errbuf[ERRMAX];
#else
#ifndef MS_WINDOWS
- char *s;
+ char *s;
#else
- WCHAR *s_buf = NULL;
+ WCHAR *s_buf = NULL;
#endif /* Unix/Windows */
#endif /* PLAN 9*/
#ifdef EINTR
- if (i == EINTR && PyErr_CheckSignals())
- return NULL;
+ if (i == EINTR && PyErr_CheckSignals())
+ return NULL;
#endif
#ifdef PLAN9
- rerrstr(errbuf, sizeof errbuf);
- message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore");
+ rerrstr(errbuf, sizeof errbuf);
+ message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore");
#else
#ifndef MS_WINDOWS
- if (i == 0)
- s = "Error"; /* Sometimes errno didn't get set */
- else
- s = strerror(i);
- message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
+ if (i == 0)
+ s = "Error"; /* Sometimes errno didn't get set */
+ else
+ s = strerror(i);
+ message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
#else
- if (i == 0)
- message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
- else
- {
- /* Note that the Win32 errors do not lineup with the
- errno error. So if the error is in the MSVC error
- table, we use it, otherwise we assume it really _is_
- a Win32 error code
- */
- if (i > 0 && i < _sys_nerr) {
- message = PyUnicode_FromString(_sys_errlist[i]);
- }
- else {
- int len = FormatMessageW(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, /* no message source */
- i,
- MAKELANGID(LANG_NEUTRAL,
- SUBLANG_DEFAULT),
- /* Default language */
- (LPWSTR) &s_buf,
- 0, /* size not used */
- NULL); /* no args */
- if (len==0) {
- /* Only ever seen this in out-of-mem
- situations */
- s_buf = NULL;
- message = PyUnicode_FromFormat("Windows Error 0x%X", i);
- } else {
- /* remove trailing cr/lf and dots */
- while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
- s_buf[--len] = L'\0';
- message = PyUnicode_FromUnicode(s_buf, len);
- }
- }
- }
+ if (i == 0)
+ message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
+ else
+ {
+ /* Note that the Win32 errors do not lineup with the
+ errno error. So if the error is in the MSVC error
+ table, we use it, otherwise we assume it really _is_
+ a Win32 error code
+ */
+ if (i > 0 && i < _sys_nerr) {
+ message = PyUnicode_FromString(_sys_errlist[i]);
+ }
+ else {
+ int len = FormatMessageW(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, /* no message source */
+ i,
+ MAKELANGID(LANG_NEUTRAL,
+ SUBLANG_DEFAULT),
+ /* Default language */
+ (LPWSTR) &s_buf,
+ 0, /* size not used */
+ NULL); /* no args */
+ if (len==0) {
+ /* Only ever seen this in out-of-mem
+ situations */
+ s_buf = NULL;
+ message = PyUnicode_FromFormat("Windows Error 0x%X", i);
+ } else {
+ /* remove trailing cr/lf and dots */
+ while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
+ s_buf[--len] = L'\0';
+ message = PyUnicode_FromUnicode(s_buf, len);
+ }
+ }
+ }
#endif /* Unix/Windows */
#endif /* PLAN 9*/
- if (message == NULL)
- {
+ if (message == NULL)
+ {
#ifdef MS_WINDOWS
- LocalFree(s_buf);
+ LocalFree(s_buf);
#endif
- return NULL;
- }
-
- if (filenameObject != NULL)
- v = Py_BuildValue("(iOO)", i, message, filenameObject);
- else
- v = Py_BuildValue("(iO)", i, message);
- Py_DECREF(message);
-
- if (v != NULL) {
- PyErr_SetObject(exc, v);
- Py_DECREF(v);
- }
+ return NULL;
+ }
+
+ if (filenameObject != NULL)
+ v = Py_BuildValue("(iOO)", i, message, filenameObject);
+ else
+ v = Py_BuildValue("(iO)", i, message);
+ Py_DECREF(message);
+
+ if (v != NULL) {
+ PyErr_SetObject(exc, v);
+ Py_DECREF(v);
+ }
#ifdef MS_WINDOWS
- LocalFree(s_buf);
+ LocalFree(s_buf);
#endif
- return NULL;
+ return NULL;
}
PyObject *
PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
{
- PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
- PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
- Py_XDECREF(name);
- return result;
+ PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
+ PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
+ Py_XDECREF(name);
+ return result;
}
#ifdef MS_WINDOWS
PyObject *
PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
{
- PyObject *name = filename ?
- PyUnicode_FromUnicode(filename, wcslen(filename)) :
- NULL;
- PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
- Py_XDECREF(name);
- return result;
+ PyObject *name = filename ?
+ PyUnicode_FromUnicode(filename, wcslen(filename)) :
+ NULL;
+ PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
+ Py_XDECREF(name);
+ return result;
}
#endif /* MS_WINDOWS */
PyObject *
PyErr_SetFromErrno(PyObject *exc)
{
- return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
+ return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
}
#ifdef MS_WINDOWS
/* Windows specific error code handling */
PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
- PyObject *exc,
- int ierr,
- PyObject *filenameObject)
+ PyObject *exc,
+ int ierr,
+ PyObject *filenameObject)
{
- int len;
- WCHAR *s_buf = NULL; /* Free via LocalFree */
- PyObject *message;
- PyObject *v;
- DWORD err = (DWORD)ierr;
- if (err==0) err = GetLastError();
- len = FormatMessageW(
- /* Error API error */
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, /* no message source */
- err,
- MAKELANGID(LANG_NEUTRAL,
- SUBLANG_DEFAULT), /* Default language */
- (LPWSTR) &s_buf,
- 0, /* size not used */
- NULL); /* no args */
- if (len==0) {
- /* Only seen this in out of mem situations */
- message = PyUnicode_FromFormat("Windows Error 0x%X", err);
- s_buf = NULL;
- } else {
- /* remove trailing cr/lf and dots */
- while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
- s_buf[--len] = L'\0';
- message = PyUnicode_FromUnicode(s_buf, len);
- }
-
- if (message == NULL)
- {
- LocalFree(s_buf);
- return NULL;
- }
-
- if (filenameObject != NULL)
- v = Py_BuildValue("(iOO)", err, message, filenameObject);
- else
- v = Py_BuildValue("(iO)", err, message);
- Py_DECREF(message);
-
- if (v != NULL) {
- PyErr_SetObject(exc, v);
- Py_DECREF(v);
- }
- LocalFree(s_buf);
- return NULL;
+ int len;
+ WCHAR *s_buf = NULL; /* Free via LocalFree */
+ PyObject *message;
+ PyObject *v;
+ DWORD err = (DWORD)ierr;
+ if (err==0) err = GetLastError();
+ len = FormatMessageW(
+ /* Error API error */
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, /* no message source */
+ err,
+ MAKELANGID(LANG_NEUTRAL,
+ SUBLANG_DEFAULT), /* Default language */
+ (LPWSTR) &s_buf,
+ 0, /* size not used */
+ NULL); /* no args */
+ if (len==0) {
+ /* Only seen this in out of mem situations */
+ message = PyUnicode_FromFormat("Windows Error 0x%X", err);
+ s_buf = NULL;
+ } else {
+ /* remove trailing cr/lf and dots */
+ while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
+ s_buf[--len] = L'\0';
+ message = PyUnicode_FromUnicode(s_buf, len);
+ }
+
+ if (message == NULL)
+ {
+ LocalFree(s_buf);
+ return NULL;
+ }
+
+ if (filenameObject != NULL)
+ v = Py_BuildValue("(iOO)", err, message, filenameObject);
+ else
+ v = Py_BuildValue("(iO)", err, message);
+ Py_DECREF(message);
+
+ if (v != NULL) {
+ PyErr_SetObject(exc, v);
+ Py_DECREF(v);
+ }
+ LocalFree(s_buf);
+ return NULL;
}
PyObject *PyErr_SetExcFromWindowsErrWithFilename(
- PyObject *exc,
- int ierr,
- const char *filename)
+ PyObject *exc,
+ int ierr,
+ const char *filename)
{
- PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
- PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
- ierr,
- name);
- Py_XDECREF(name);
- return ret;
+ PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
+ PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
+ ierr,
+ name);
+ Py_XDECREF(name);
+ return ret;
}
PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
- PyObject *exc,
- int ierr,
- const Py_UNICODE *filename)
+ PyObject *exc,
+ int ierr,
+ const Py_UNICODE *filename)
{
- PyObject *name = filename ?
- PyUnicode_FromUnicode(filename, wcslen(filename)) :
- NULL;
- PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
- ierr,
- name);
- Py_XDECREF(name);
- return ret;
+ PyObject *name = filename ?
+ PyUnicode_FromUnicode(filename, wcslen(filename)) :
+ NULL;
+ PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
+ ierr,
+ name);
+ Py_XDECREF(name);
+ return ret;
}
PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
{
- return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
+ return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
}
PyObject *PyErr_SetFromWindowsErr(int ierr)
{
- return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
- ierr, NULL);
+ return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
+ ierr, NULL);
}
PyObject *PyErr_SetFromWindowsErrWithFilename(
- int ierr,
- const char *filename)
+ int ierr,
+ const char *filename)
{
- PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
- PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
- PyExc_WindowsError,
- ierr, name);
- Py_XDECREF(name);
- return result;
+ PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
+ PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
+ PyExc_WindowsError,
+ ierr, name);
+ Py_XDECREF(name);
+ return result;
}
PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
- int ierr,
- const Py_UNICODE *filename)
+ int ierr,
+ const Py_UNICODE *filename)
{
- PyObject *name = filename ?
- PyUnicode_FromUnicode(filename, wcslen(filename)) :
- NULL;
- PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
- PyExc_WindowsError,
- ierr, name);
- Py_XDECREF(name);
- return result;
+ PyObject *name = filename ?
+ PyUnicode_FromUnicode(filename, wcslen(filename)) :
+ NULL;
+ PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
+ PyExc_WindowsError,
+ ierr, name);
+ Py_XDECREF(name);
+ return result;
}
#endif /* MS_WINDOWS */
void
_PyErr_BadInternalCall(const char *filename, int lineno)
{
- PyErr_Format(PyExc_SystemError,
- "%s:%d: bad argument to internal function",
- filename, lineno);
+ PyErr_Format(PyExc_SystemError,
+ "%s:%d: bad argument to internal function",
+ filename, lineno);
}
/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
@@ -615,8 +615,8 @@ _PyErr_BadInternalCall(const char *filename, int lineno)
void
PyErr_BadInternalCall(void)
{
- PyErr_Format(PyExc_SystemError,
- "bad argument to internal function");
+ PyErr_Format(PyExc_SystemError,
+ "bad argument to internal function");
}
#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
@@ -625,20 +625,20 @@ PyErr_BadInternalCall(void)
PyObject *
PyErr_Format(PyObject *exception, const char *format, ...)
{
- va_list vargs;
- PyObject* string;
+ va_list vargs;
+ PyObject* string;
#ifdef HAVE_STDARG_PROTOTYPES
- va_start(vargs, format);
+ va_start(vargs, format);
#else
- va_start(vargs);
+ va_start(vargs);
#endif
- string = PyUnicode_FromFormatV(format, vargs);
- PyErr_SetObject(exception, string);
- Py_XDECREF(string);
- va_end(vargs);
- return NULL;
+ string = PyUnicode_FromFormatV(format, vargs);
+ PyErr_SetObject(exception, string);
+ Py_XDECREF(string);
+ va_end(vargs);
+ return NULL;
}
@@ -646,51 +646,51 @@ PyErr_Format(PyObject *exception, const char *format, ...)
PyObject *
PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
{
- const char *dot;
- PyObject *modulename = NULL;
- PyObject *classname = NULL;
- PyObject *mydict = NULL;
- PyObject *bases = NULL;
- PyObject *result = NULL;
- dot = strrchr(name, '.');
- if (dot == NULL) {
- PyErr_SetString(PyExc_SystemError,
- "PyErr_NewException: name must be module.class");
- return NULL;
- }
- if (base == NULL)
- base = PyExc_Exception;
- if (dict == NULL) {
- dict = mydict = PyDict_New();
- if (dict == NULL)
- goto failure;
- }
- if (PyDict_GetItemString(dict, "__module__") == NULL) {
- modulename = PyUnicode_FromStringAndSize(name,
- (Py_ssize_t)(dot-name));
- if (modulename == NULL)
- goto failure;
- if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
- goto failure;
- }
- if (PyTuple_Check(base)) {
- bases = base;
- /* INCREF as we create a new ref in the else branch */
- Py_INCREF(bases);
- } else {
- bases = PyTuple_Pack(1, base);
- if (bases == NULL)
- goto failure;
- }
- /* Create a real new-style class. */
- result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
- dot+1, bases, dict);
+ const char *dot;
+ PyObject *modulename = NULL;
+ PyObject *classname = NULL;
+ PyObject *mydict = NULL;
+ PyObject *bases = NULL;
+ PyObject *result = NULL;
+ dot = strrchr(name, '.');
+ if (dot == NULL) {
+ PyErr_SetString(PyExc_SystemError,
+ "PyErr_NewException: name must be module.class");
+ return NULL;
+ }
+ if (base == NULL)
+ base = PyExc_Exception;
+ if (dict == NULL) {
+ dict = mydict = PyDict_New();
+ if (dict == NULL)
+ goto failure;
+ }
+ if (PyDict_GetItemString(dict, "__module__") == NULL) {
+ modulename = PyUnicode_FromStringAndSize(name,
+ (Py_ssize_t)(dot-name));
+ if (modulename == NULL)
+ goto failure;
+ if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
+ goto failure;
+ }
+ if (PyTuple_Check(base)) {
+ bases = base;
+ /* INCREF as we create a new ref in the else branch */
+ Py_INCREF(bases);
+ } else {
+ bases = PyTuple_Pack(1, base);
+ if (bases == NULL)
+ goto failure;
+ }
+ /* Create a real new-style class. */
+ result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
+ dot+1, bases, dict);
failure:
- Py_XDECREF(bases);
- Py_XDECREF(mydict);
- Py_XDECREF(classname);
- Py_XDECREF(modulename);
- return result;
+ Py_XDECREF(bases);
+ Py_XDECREF(mydict);
+ Py_XDECREF(classname);
+ Py_XDECREF(modulename);
+ return result;
}
/* Call when an exception has occurred but there is no way for Python
@@ -698,52 +698,52 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
void
PyErr_WriteUnraisable(PyObject *obj)
{
- PyObject *f, *t, *v, *tb;
- PyErr_Fetch(&t, &v, &tb);
- f = PySys_GetObject("stderr");
- if (f != NULL && f != Py_None) {
- PyFile_WriteString("Exception ", f);
- if (t) {
- PyObject* moduleName;
- char* className;
- assert(PyExceptionClass_Check(t));
- className = PyExceptionClass_Name(t);
- if (className != NULL) {
- char *dot = strrchr(className, '.');
- if (dot != NULL)
- className = dot+1;
- }
-
- moduleName = PyObject_GetAttrString(t, "__module__");
- if (moduleName == NULL)
- PyFile_WriteString("<unknown>", f);
- else {
- char* modstr = _PyUnicode_AsString(moduleName);
- if (modstr &&
- strcmp(modstr, "builtins") != 0)
- {
- PyFile_WriteString(modstr, f);
- PyFile_WriteString(".", f);
- }
- }
- if (className == NULL)
- PyFile_WriteString("<unknown>", f);
- else
- PyFile_WriteString(className, f);
- if (v && v != Py_None) {
- PyFile_WriteString(": ", f);
- PyFile_WriteObject(v, f, 0);
- }
- Py_XDECREF(moduleName);
- }
- PyFile_WriteString(" in ", f);
- PyFile_WriteObject(obj, f, 0);
- PyFile_WriteString(" ignored\n", f);
- PyErr_Clear(); /* Just in case */
- }
- Py_XDECREF(t);
- Py_XDECREF(v);
- Py_XDECREF(tb);
+ PyObject *f, *t, *v, *tb;
+ PyErr_Fetch(&t, &v, &tb);
+ f = PySys_GetObject("stderr");
+ if (f != NULL && f != Py_None) {
+ PyFile_WriteString("Exception ", f);
+ if (t) {
+ PyObject* moduleName;
+ char* className;
+ assert(PyExceptionClass_Check(t));
+ className = PyExceptionClass_Name(t);
+ if (className != NULL) {
+ char *dot = strrchr(className, '.');
+ if (dot != NULL)
+ className = dot+1;
+ }
+
+ moduleName = PyObject_GetAttrString(t, "__module__");
+ if (moduleName == NULL)
+ PyFile_WriteString("<unknown>", f);
+ else {
+ char* modstr = _PyUnicode_AsString(moduleName);
+ if (modstr &&
+ strcmp(modstr, "builtins") != 0)
+ {
+ PyFile_WriteString(modstr, f);
+ PyFile_WriteString(".", f);
+ }
+ }
+ if (className == NULL)
+ PyFile_WriteString("<unknown>", f);
+ else
+ PyFile_WriteString(className, f);
+ if (v && v != Py_None) {
+ PyFile_WriteString(": ", f);
+ PyFile_WriteObject(v, f, 0);
+ }
+ Py_XDECREF(moduleName);
+ }
+ PyFile_WriteString(" in ", f);
+ PyFile_WriteObject(obj, f, 0);
+ PyFile_WriteString(" ignored\n", f);
+ PyErr_Clear(); /* Just in case */
+ }
+ Py_XDECREF(t);
+ Py_XDECREF(v);
+ Py_XDECREF(tb);
}
extern PyObject *PyModule_GetWarningsModule(void);
@@ -756,59 +756,59 @@ extern PyObject *PyModule_GetWarningsModule(void);
void
PyErr_SyntaxLocation(const char *filename, int lineno)
{
- PyObject *exc, *v, *tb, *tmp;
-
- /* add attributes for the line number and filename for the error */
- PyErr_Fetch(&exc, &v, &tb);
- PyErr_NormalizeException(&exc, &v, &tb);
- /* XXX check that it is, indeed, a syntax error. It might not
- * be, though. */
- tmp = PyLong_FromLong(lineno);
- if (tmp == NULL)
- PyErr_Clear();
- else {
- if (PyObject_SetAttrString(v, "lineno", tmp))
- PyErr_Clear();
- Py_DECREF(tmp);
- }
- if (filename != NULL) {
- tmp = PyUnicode_FromString(filename);
- if (tmp == NULL)
- PyErr_Clear();
- else {
- if (PyObject_SetAttrString(v, "filename", tmp))
- PyErr_Clear();
- Py_DECREF(tmp);
- }
-
- tmp = PyErr_ProgramText(filename, lineno);
- if (tmp) {
- if (PyObject_SetAttrString(v, "text", tmp))
- PyErr_Clear();
- Py_DECREF(tmp);
- }
- }
- if (PyObject_SetAttrString(v, "offset", Py_None)) {
- PyErr_Clear();
- }
- if (exc != PyExc_SyntaxError) {
- if (!PyObject_HasAttrString(v, "msg")) {
- tmp = PyObject_Str(v);
- if (tmp) {
- if (PyObject_SetAttrString(v, "msg", tmp))
- PyErr_Clear();
- Py_DECREF(tmp);
- } else {
- PyErr_Clear();
- }
- }
- if (!PyObject_HasAttrString(v, "print_file_and_line")) {
- if (PyObject_SetAttrString(v, "print_file_and_line",
- Py_None))
- PyErr_Clear();
- }
- }
- PyErr_Restore(exc, v, tb);
+ PyObject *exc, *v, *tb, *tmp;
+
+ /* add attributes for the line number and filename for the error */
+ PyErr_Fetch(&exc, &v, &tb);
+ PyErr_NormalizeException(&exc, &v, &tb);
+ /* XXX check that it is, indeed, a syntax error. It might not
+ * be, though. */
+ tmp = PyLong_FromLong(lineno);
+ if (tmp == NULL)
+ PyErr_Clear();
+ else {
+ if (PyObject_SetAttrString(v, "lineno", tmp))
+ PyErr_Clear();
+ Py_DECREF(tmp);
+ }
+ if (filename != NULL) {
+ tmp = PyUnicode_FromString(filename);
+ if (tmp == NULL)
+ PyErr_Clear();
+ else {
+ if (PyObject_SetAttrString(v, "filename", tmp))
+ PyErr_Clear();
+ Py_DECREF(tmp);
+ }
+
+ tmp = PyErr_ProgramText(filename, lineno);
+ if (tmp) {
+ if (PyObject_SetAttrString(v, "text", tmp))
+ PyErr_Clear();
+ Py_DECREF(tmp);
+ }
+ }
+ if (PyObject_SetAttrString(v, "offset", Py_None)) {
+ PyErr_Clear();
+ }
+ if (exc != PyExc_SyntaxError) {
+ if (!PyObject_HasAttrString(v, "msg")) {
+ tmp = PyObject_Str(v);
+ if (tmp) {
+ if (PyObject_SetAttrString(v, "msg", tmp))
+ PyErr_Clear();
+ Py_DECREF(tmp);
+ } else {
+ PyErr_Clear();
+ }
+ }
+ if (!PyObject_HasAttrString(v, "print_file_and_line")) {
+ if (PyObject_SetAttrString(v, "print_file_and_line",
+ Py_None))
+ PyErr_Clear();
+ }
+ }
+ PyErr_Restore(exc, v, tb);
}
/* Attempt to load the line of text that the exception refers to. If it
@@ -820,41 +820,41 @@ PyErr_SyntaxLocation(const char *filename, int lineno)
PyObject *
PyErr_ProgramText(const char *filename, int lineno)
{
- FILE *fp;
- int i;
- char linebuf[1000];
-
- if (filename == NULL || *filename == '\0' || lineno <= 0)
- return NULL;
- fp = fopen(filename, "r" PY_STDIOTEXTMODE);
- if (fp == NULL)
- return NULL;
- for (i = 0; i < lineno; i++) {
- char *pLastChar = &linebuf[sizeof(linebuf) - 2];
- do {
- *pLastChar = '\0';
- if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
- fp, NULL) == NULL)
- break;
- /* fgets read *something*; if it didn't get as
- far as pLastChar, it must have found a newline
- or hit the end of the file; if pLastChar is \n,
- it obviously found a newline; else we haven't
- yet seen a newline, so must continue */
- } while (*pLastChar != '\0' && *pLastChar != '\n');
- }
- fclose(fp);
- if (i == lineno) {
- char *p = linebuf;
- PyObject *res;
- while (*p == ' ' || *p == '\t' || *p == '\014')
- p++;
- res = PyUnicode_FromString(p);
- if (res == NULL)
- PyErr_Clear();
- return res;
- }
- return NULL;
+ FILE *fp;
+ int i;
+ char linebuf[1000];
+
+ if (filename == NULL || *filename == '\0' || lineno <= 0)
+ return NULL;
+ fp = fopen(filename, "r" PY_STDIOTEXTMODE);
+ if (fp == NULL)
+ return NULL;
+ for (i = 0; i < lineno; i++) {
+ char *pLastChar = &linebuf[sizeof(linebuf) - 2];
+ do {
+ *pLastChar = '\0';
+ if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
+ fp, NULL) == NULL)
+ break;
+ /* fgets read *something*; if it didn't get as
+ far as pLastChar, it must have found a newline
+ or hit the end of the file; if pLastChar is \n,
+ it obviously found a newline; else we haven't
+ yet seen a newline, so must continue */
+ } while (*pLastChar != '\0' && *pLastChar != '\n');
+ }
+ fclose(fp);
+ if (i == lineno) {
+ char *p = linebuf;
+ PyObject *res;
+ while (*p == ' ' || *p == '\t' || *p == '\014')
+ p++;
+ res = PyUnicode_FromString(p);
+ if (res == NULL)
+ PyErr_Clear();
+ return res;
+ }
+ return NULL;
}
#ifdef __cplusplus
diff --git a/Python/frozen.c b/Python/frozen.c
index 1e40d7d..57d8257 100644
--- a/Python/frozen.c
+++ b/Python/frozen.c
@@ -12,25 +12,25 @@
the appropriate bytes from M___main__.c. */
static unsigned char M___hello__[] = {
- 99,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
- 0,64,0,0,0,115,10,0,0,0,100,1,0,90,1,0,
- 100,0,0,83,40,2,0,0,0,78,84,40,2,0,0,0,
- 117,4,0,0,0,84,114,117,101,117,11,0,0,0,105,110,
- 105,116,105,97,108,105,122,101,100,40,0,0,0,0,40,0,
- 0,0,0,40,0,0,0,0,117,7,0,0,0,102,108,97,
- 103,46,112,121,117,8,0,0,0,60,109,111,100,117,108,101,
- 62,1,0,0,0,115,0,0,0,0,
+ 99,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
+ 0,64,0,0,0,115,10,0,0,0,100,1,0,90,1,0,
+ 100,0,0,83,40,2,0,0,0,78,84,40,2,0,0,0,
+ 117,4,0,0,0,84,114,117,101,117,11,0,0,0,105,110,
+ 105,116,105,97,108,105,122,101,100,40,0,0,0,0,40,0,
+ 0,0,0,40,0,0,0,0,117,7,0,0,0,102,108,97,
+ 103,46,112,121,117,8,0,0,0,60,109,111,100,117,108,101,
+ 62,1,0,0,0,115,0,0,0,0,
};
#define SIZE (int)sizeof(M___hello__)
static struct _frozen _PyImport_FrozenModules[] = {
- /* Test module */
- {"__hello__", M___hello__, SIZE},
- /* Test package (negative size indicates package-ness) */
- {"__phello__", M___hello__, -SIZE},
- {"__phello__.spam", M___hello__, SIZE},
- {0, 0, 0} /* sentinel */
+ /* Test module */
+ {"__hello__", M___hello__, SIZE},
+ /* Test package (negative size indicates package-ness) */
+ {"__phello__", M___hello__, -SIZE},
+ {"__phello__.spam", M___hello__, SIZE},
+ {0, 0, 0} /* sentinel */
};
/* Embedding apps may change this pointer to point to their favorite
diff --git a/Python/frozenmain.c b/Python/frozenmain.c
index b14c391..f08caf2 100644
--- a/Python/frozenmain.c
+++ b/Python/frozenmain.c
@@ -15,96 +15,96 @@ extern int PyInitFrozenExtensions(void);
int
Py_FrozenMain(int argc, char **argv)
{
- char *p;
- int i, n, sts;
- int inspect = 0;
- int unbuffered = 0;
- char *oldloc;
- wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
- /* We need a second copies, as Python might modify the first one. */
- wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
-
- Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
-
- if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
- inspect = 1;
- if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
- unbuffered = 1;
-
- if (unbuffered) {
- setbuf(stdin, (char *)NULL);
- setbuf(stdout, (char *)NULL);
- setbuf(stderr, (char *)NULL);
- }
-
- if (!argv_copy) {
- fprintf(stderr, "out of memory\n");
- return 1;
- }
-
- oldloc = setlocale(LC_ALL, NULL);
- setlocale(LC_ALL, "");
- for (i = 0; i < argc; i++) {
+ char *p;
+ int i, n, sts;
+ int inspect = 0;
+ int unbuffered = 0;
+ char *oldloc;
+ wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
+ /* We need a second copies, as Python might modify the first one. */
+ wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
+
+ Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
+
+ if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
+ inspect = 1;
+ if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
+ unbuffered = 1;
+
+ if (unbuffered) {
+ setbuf(stdin, (char *)NULL);
+ setbuf(stdout, (char *)NULL);
+ setbuf(stderr, (char *)NULL);
+ }
+
+ if (!argv_copy) {
+ fprintf(stderr, "out of memory\n");
+ return 1;
+ }
+
+ oldloc = setlocale(LC_ALL, NULL);
+ setlocale(LC_ALL, "");
+ for (i = 0; i < argc; i++) {
#ifdef HAVE_BROKEN_MBSTOWCS
- size_t argsize = strlen(argv[i]);
+ size_t argsize = strlen(argv[i]);
#else
- size_t argsize = mbstowcs(NULL, argv[i], 0);
+ size_t argsize = mbstowcs(NULL, argv[i], 0);
#endif
- size_t count;
- if (argsize == (size_t)-1) {
- fprintf(stderr, "Could not convert argument %d to string\n", i);
- return 1;
- }
- argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
- argv_copy2[i] = argv_copy[i];
- if (!argv_copy[i]) {
- fprintf(stderr, "out of memory\n");
- return 1;
- }
- count = mbstowcs(argv_copy[i], argv[i], argsize+1);
- if (count == (size_t)-1) {
- fprintf(stderr, "Could not convert argument %d to string\n", i);
- return 1;
- }
- }
- setlocale(LC_ALL, oldloc);
+ size_t count;
+ if (argsize == (size_t)-1) {
+ fprintf(stderr, "Could not convert argument %d to string\n", i);
+ return 1;
+ }
+ argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
+ argv_copy2[i] = argv_copy[i];
+ if (!argv_copy[i]) {
+ fprintf(stderr, "out of memory\n");
+ return 1;
+ }
+ count = mbstowcs(argv_copy[i], argv[i], argsize+1);
+ if (count == (size_t)-1) {
+ fprintf(stderr, "Could not convert argument %d to string\n", i);
+ return 1;
+ }
+ }
+ setlocale(LC_ALL, oldloc);
#ifdef MS_WINDOWS
- PyInitFrozenExtensions();
+ PyInitFrozenExtensions();
#endif /* MS_WINDOWS */
- Py_SetProgramName(argv_copy[0]);
- Py_Initialize();
+ Py_SetProgramName(argv_copy[0]);
+ Py_Initialize();
#ifdef MS_WINDOWS
- PyWinFreeze_ExeInit();
+ PyWinFreeze_ExeInit();
#endif
- if (Py_VerboseFlag)
- fprintf(stderr, "Python %s\n%s\n",
- Py_GetVersion(), Py_GetCopyright());
+ if (Py_VerboseFlag)
+ fprintf(stderr, "Python %s\n%s\n",
+ Py_GetVersion(), Py_GetCopyright());
- PySys_SetArgv(argc, argv_copy);
+ PySys_SetArgv(argc, argv_copy);
- n = PyImport_ImportFrozenModule("__main__");
- if (n == 0)
- Py_FatalError("__main__ not frozen");
- if (n < 0) {
- PyErr_Print();
- sts = 1;
- }
- else
- sts = 0;
+ n = PyImport_ImportFrozenModule("__main__");
+ if (n == 0)
+ Py_FatalError("__main__ not frozen");
+ if (n < 0) {
+ PyErr_Print();
+ sts = 1;
+ }
+ else
+ sts = 0;
- if (inspect && isatty((int)fileno(stdin)))
- sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
+ if (inspect && isatty((int)fileno(stdin)))
+ sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
#ifdef MS_WINDOWS
- PyWinFreeze_ExeTerm();
+ PyWinFreeze_ExeTerm();
#endif
- Py_Finalize();
- for (i = 0; i < argc; i++) {
- PyMem_Free(argv_copy2[i]);
- }
- PyMem_Free(argv_copy);
- PyMem_Free(argv_copy2);
- return sts;
+ Py_Finalize();
+ for (i = 0; i < argc; i++) {
+ PyMem_Free(argv_copy2[i]);
+ }
+ PyMem_Free(argv_copy);
+ PyMem_Free(argv_copy2);
+ return sts;
}
diff --git a/Python/future.c b/Python/future.c
index 4178541..515dcd9 100644
--- a/Python/future.c
+++ b/Python/future.c
@@ -14,131 +14,131 @@
static int
future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
{
- int i;
- asdl_seq *names;
-
- assert(s->kind == ImportFrom_kind);
-
- names = s->v.ImportFrom.names;
- for (i = 0; i < asdl_seq_LEN(names); i++) {
- alias_ty name = (alias_ty)asdl_seq_GET(names, i);
- const char *feature = _PyUnicode_AsString(name->name);
- if (!feature)
- return 0;
- if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
- continue;
- } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
- ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
- } else if (strcmp(feature, "braces") == 0) {
- PyErr_SetString(PyExc_SyntaxError,
- "not a chance");
- PyErr_SyntaxLocation(filename, s->lineno);
- return 0;
- } else {
- PyErr_Format(PyExc_SyntaxError,
- UNDEFINED_FUTURE_FEATURE, feature);
- PyErr_SyntaxLocation(filename, s->lineno);
- return 0;
- }
- }
- return 1;
+ int i;
+ asdl_seq *names;
+
+ assert(s->kind == ImportFrom_kind);
+
+ names = s->v.ImportFrom.names;
+ for (i = 0; i < asdl_seq_LEN(names); i++) {
+ alias_ty name = (alias_ty)asdl_seq_GET(names, i);
+ const char *feature = _PyUnicode_AsString(name->name);
+ if (!feature)
+ return 0;
+ if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
+ continue;
+ } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
+ ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
+ } else if (strcmp(feature, "braces") == 0) {
+ PyErr_SetString(PyExc_SyntaxError,
+ "not a chance");
+ PyErr_SyntaxLocation(filename, s->lineno);
+ return 0;
+ } else {
+ PyErr_Format(PyExc_SyntaxError,
+ UNDEFINED_FUTURE_FEATURE, feature);
+ PyErr_SyntaxLocation(filename, s->lineno);
+ return 0;
+ }
+ }
+ return 1;
}
static int
future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
{
- int i, found_docstring = 0, done = 0, prev_line = 0;
-
- static PyObject *future;
- if (!future) {
- future = PyUnicode_InternFromString("__future__");
- if (!future)
- return 0;
- }
-
- if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
- return 1;
-
- /* A subsequent pass will detect future imports that don't
- appear at the beginning of the file. There's one case,
- however, that is easier to handle here: A series of imports
- joined by semi-colons, where the first import is a future
- statement but some subsequent import has the future form
- but is preceded by a regular import.
- */
-
-
- for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
- stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
-
- if (done && s->lineno > prev_line)
- return 1;
- prev_line = s->lineno;
-
- /* The tests below will return from this function unless it is
- still possible to find a future statement. The only things
- that can precede a future statement are another future
- statement and a doc string.
- */
-
- if (s->kind == ImportFrom_kind) {
- if (s->v.ImportFrom.module == future) {
- if (done) {
- PyErr_SetString(PyExc_SyntaxError,
- ERR_LATE_FUTURE);
- PyErr_SyntaxLocation(filename,
- s->lineno);
- return 0;
- }
- if (!future_check_features(ff, s, filename))
- return 0;
- ff->ff_lineno = s->lineno;
- }
- else
- done = 1;
- }
- else if (s->kind == Expr_kind && !found_docstring) {
- expr_ty e = s->v.Expr.value;
- if (e->kind != Str_kind)
- done = 1;
- else
- found_docstring = 1;
- }
- else
- done = 1;
- }
- return 1;
+ int i, found_docstring = 0, done = 0, prev_line = 0;
+
+ static PyObject *future;
+ if (!future) {
+ future = PyUnicode_InternFromString("__future__");
+ if (!future)
+ return 0;
+ }
+
+ if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
+ return 1;
+
+ /* A subsequent pass will detect future imports that don't
+ appear at the beginning of the file. There's one case,
+ however, that is easier to handle here: A series of imports
+ joined by semi-colons, where the first import is a future
+ statement but some subsequent import has the future form
+ but is preceded by a regular import.
+ */
+
+
+ for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
+ stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
+
+ if (done && s->lineno > prev_line)
+ return 1;
+ prev_line = s->lineno;
+
+ /* The tests below will return from this function unless it is
+ still possible to find a future statement. The only things
+ that can precede a future statement are another future
+ statement and a doc string.
+ */
+
+ if (s->kind == ImportFrom_kind) {
+ if (s->v.ImportFrom.module == future) {
+ if (done) {
+ PyErr_SetString(PyExc_SyntaxError,
+ ERR_LATE_FUTURE);
+ PyErr_SyntaxLocation(filename,
+ s->lineno);
+ return 0;
+ }
+ if (!future_check_features(ff, s, filename))
+ return 0;
+ ff->ff_lineno = s->lineno;
+ }
+ else
+ done = 1;
+ }
+ else if (s->kind == Expr_kind && !found_docstring) {
+ expr_ty e = s->v.Expr.value;
+ if (e->kind != Str_kind)
+ done = 1;
+ else
+ found_docstring = 1;
+ }
+ else
+ done = 1;
+ }
+ return 1;
}
PyFutureFeatures *
PyFuture_FromAST(mod_ty mod, const char *filename)
{
- PyFutureFeatures *ff;
-
- ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
- if (ff == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- ff->ff_features = 0;
- ff->ff_lineno = -1;
-
- if (!future_parse(ff, mod, filename)) {
- PyObject_Free(ff);
- return NULL;
- }
- return ff;
+ PyFutureFeatures *ff;
+
+ ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
+ if (ff == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ ff->ff_features = 0;
+ ff->ff_lineno = -1;
+
+ if (!future_parse(ff, mod, filename)) {
+ PyObject_Free(ff);
+ return NULL;
+ }
+ return ff;
}
diff --git a/Python/getargs.c b/Python/getargs.c
index 7726324..dec8602 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -14,9 +14,9 @@ int PyArg_ParseTuple(PyObject *, const char *, ...);
int PyArg_VaParse(PyObject *, const char *, va_list);
int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
- const char *, char **, ...);
+ const char *, char **, ...);
int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
- const char *, char **, va_list);
+ const char *, char **, va_list);
#ifdef HAVE_DECLSPEC_DLL
/* Export functions */
@@ -40,100 +40,100 @@ static void seterror(int, const char *, int *, const char *, const char *);
static char *convertitem(PyObject *, const char **, va_list *, int, int *,
char *, size_t, PyObject **);
static char *converttuple(PyObject *, const char **, va_list *, int,
- int *, char *, size_t, int, PyObject **);
+ int *, char *, size_t, int, PyObject **);
static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
- size_t, PyObject **);
+ size_t, PyObject **);
static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
static int getbuffer(PyObject *, Py_buffer *, char**);
static int vgetargskeywords(PyObject *, PyObject *,
- const char *, char **, va_list *, int);
+ const char *, char **, va_list *, int);
static char *skipitem(const char **, va_list *, int);
int
PyArg_Parse(PyObject *args, const char *format, ...)
{
- int retval;
- va_list va;
+ int retval;
+ va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_COMPAT);
- va_end(va);
- return retval;
+ va_start(va, format);
+ retval = vgetargs1(args, format, &va, FLAG_COMPAT);
+ va_end(va);
+ return retval;
}
int
_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
{
- int retval;
- va_list va;
+ int retval;
+ va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
- va_end(va);
- return retval;
+ va_start(va, format);
+ retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
+ va_end(va);
+ return retval;
}
int
PyArg_ParseTuple(PyObject *args, const char *format, ...)
{
- int retval;
- va_list va;
+ int retval;
+ va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, 0);
- va_end(va);
- return retval;
+ va_start(va, format);
+ retval = vgetargs1(args, format, &va, 0);
+ va_end(va);
+ return retval;
}
int
_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
{
- int retval;
- va_list va;
+ int retval;
+ va_list va;
- va_start(va, format);
- retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
+ va_start(va, format);
+ retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
+ va_end(va);
+ return retval;
}
int
PyArg_VaParse(PyObject *args, const char *format, va_list va)
{
- va_list lva;
+ va_list lva;
#ifdef VA_LIST_IS_ARRAY
- memcpy(lva, va, sizeof(va_list));
+ memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(lva, va);
+ __va_copy(lva, va);
#else
- lva = va;
+ lva = va;
#endif
#endif
- return vgetargs1(args, format, &lva, 0);
+ return vgetargs1(args, format, &lva, 0);
}
int
_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
{
- va_list lva;
+ va_list lva;
#ifdef VA_LIST_IS_ARRAY
- memcpy(lva, va, sizeof(va_list));
+ memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(lva, va);
+ __va_copy(lva, va);
#else
- lva = va;
+ lva = va;
#endif
#endif
- return vgetargs1(args, format, &lva, FLAG_SIZE_T);
+ return vgetargs1(args, format, &lva, FLAG_SIZE_T);
}
@@ -146,261 +146,261 @@ _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
static void
cleanup_ptr(PyObject *self)
{
- void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
- if (ptr) {
- PyMem_FREE(ptr);
- }
+ void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
+ if (ptr) {
+ PyMem_FREE(ptr);
+ }
}
static void
cleanup_buffer(PyObject *self)
{
- Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
- if (ptr) {
- PyBuffer_Release(ptr);
- }
+ Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
+ if (ptr) {
+ PyBuffer_Release(ptr);
+ }
}
static int
addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
{
- PyObject *cobj;
- const char *name;
-
- if (!*freelist) {
- *freelist = PyList_New(0);
- if (!*freelist) {
- destr(ptr);
- return -1;
- }
- }
-
- if (destr == cleanup_ptr) {
- name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
- } else if (destr == cleanup_buffer) {
- name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
- } else {
- return -1;
- }
- cobj = PyCapsule_New(ptr, name, destr);
- if (!cobj) {
- destr(ptr);
- return -1;
- }
- if (PyList_Append(*freelist, cobj)) {
- Py_DECREF(cobj);
- return -1;
- }
+ PyObject *cobj;
+ const char *name;
+
+ if (!*freelist) {
+ *freelist = PyList_New(0);
+ if (!*freelist) {
+ destr(ptr);
+ return -1;
+ }
+ }
+
+ if (destr == cleanup_ptr) {
+ name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
+ } else if (destr == cleanup_buffer) {
+ name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
+ } else {
+ return -1;
+ }
+ cobj = PyCapsule_New(ptr, name, destr);
+ if (!cobj) {
+ destr(ptr);
+ return -1;
+ }
+ if (PyList_Append(*freelist, cobj)) {
Py_DECREF(cobj);
- return 0;
+ return -1;
+ }
+ Py_DECREF(cobj);
+ return 0;
}
static void
cleanup_convert(PyObject *self)
{
- typedef int (*destr_t)(PyObject *, void *);
- destr_t destr = (destr_t)PyCapsule_GetContext(self);
- void *ptr = PyCapsule_GetPointer(self,
- GETARGS_CAPSULE_NAME_CLEANUP_CONVERT);
- if (ptr && destr)
- destr(NULL, ptr);
+ typedef int (*destr_t)(PyObject *, void *);
+ destr_t destr = (destr_t)PyCapsule_GetContext(self);
+ void *ptr = PyCapsule_GetPointer(self,
+ GETARGS_CAPSULE_NAME_CLEANUP_CONVERT);
+ if (ptr && destr)
+ destr(NULL, ptr);
}
static int
addcleanup_convert(void *ptr, PyObject **freelist, int (*destr)(PyObject*,void*))
{
- PyObject *cobj;
- if (!*freelist) {
- *freelist = PyList_New(0);
- if (!*freelist) {
- destr(NULL, ptr);
- return -1;
- }
- }
- cobj = PyCapsule_New(ptr, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT,
- cleanup_convert);
- if (!cobj) {
- destr(NULL, ptr);
- return -1;
- }
- if (PyCapsule_SetContext(cobj, destr) == -1) {
- /* This really should not happen. */
- Py_FatalError("capsule refused setting of context.");
- }
- if (PyList_Append(*freelist, cobj)) {
- Py_DECREF(cobj); /* This will also call destr. */
- return -1;
- }
- Py_DECREF(cobj);
- return 0;
+ PyObject *cobj;
+ if (!*freelist) {
+ *freelist = PyList_New(0);
+ if (!*freelist) {
+ destr(NULL, ptr);
+ return -1;
+ }
+ }
+ cobj = PyCapsule_New(ptr, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT,
+ cleanup_convert);
+ if (!cobj) {
+ destr(NULL, ptr);
+ return -1;
+ }
+ if (PyCapsule_SetContext(cobj, destr) == -1) {
+ /* This really should not happen. */
+ Py_FatalError("capsule refused setting of context.");
+ }
+ if (PyList_Append(*freelist, cobj)) {
+ Py_DECREF(cobj); /* This will also call destr. */
+ return -1;
+ }
+ Py_DECREF(cobj);
+ return 0;
}
static int
cleanreturn(int retval, PyObject *freelist)
{
- if (freelist && retval != 0) {
- /* We were successful, reset the destructors so that they
- don't get called. */
- Py_ssize_t len = PyList_GET_SIZE(freelist), i;
- for (i = 0; i < len; i++)
- PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
- }
- Py_XDECREF(freelist);
- return retval;
+ if (freelist && retval != 0) {
+ /* We were successful, reset the destructors so that they
+ don't get called. */
+ Py_ssize_t len = PyList_GET_SIZE(freelist), i;
+ for (i = 0; i < len; i++)
+ PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
+ }
+ Py_XDECREF(freelist);
+ return retval;
}
static int
vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
{
- char msgbuf[256];
- int levels[32];
- const char *fname = NULL;
- const char *message = NULL;
- int min = -1;
- int max = 0;
- int level = 0;
- int endfmt = 0;
- const char *formatsave = format;
- Py_ssize_t i, len;
- char *msg;
- PyObject *freelist = NULL;
- int compat = flags & FLAG_COMPAT;
-
- assert(compat || (args != (PyObject*)NULL));
- flags = flags & ~FLAG_COMPAT;
-
- while (endfmt == 0) {
- int c = *format++;
- switch (c) {
- case '(':
- if (level == 0)
- max++;
- level++;
- if (level >= 30)
- Py_FatalError("too many tuple nesting levels "
- "in argument format string");
- break;
- case ')':
- if (level == 0)
- Py_FatalError("excess ')' in getargs format");
- else
- level--;
- break;
- case '\0':
- endfmt = 1;
- break;
- case ':':
- fname = format;
- endfmt = 1;
- break;
- case ';':
- message = format;
- endfmt = 1;
- break;
- default:
- if (level == 0) {
- if (c == 'O')
- max++;
- else if (isalpha(Py_CHARMASK(c))) {
- if (c != 'e') /* skip encoded */
- max++;
- } else if (c == '|')
- min = max;
- }
- break;
- }
- }
-
- if (level != 0)
- Py_FatalError(/* '(' */ "missing ')' in getargs format");
-
- if (min < 0)
- min = max;
-
- format = formatsave;
-
- if (compat) {
- if (max == 0) {
- if (args == NULL)
- return 1;
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.200s%s takes no arguments",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- PyErr_SetString(PyExc_TypeError, msgbuf);
- return 0;
- }
- else if (min == 1 && max == 1) {
- if (args == NULL) {
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.200s%s takes at least one argument",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- PyErr_SetString(PyExc_TypeError, msgbuf);
- return 0;
- }
- msg = convertitem(args, &format, p_va, flags, levels,
- msgbuf, sizeof(msgbuf), &freelist);
- if (msg == NULL)
- return cleanreturn(1, freelist);
- seterror(levels[0], msg, levels+1, fname, message);
- return cleanreturn(0, freelist);
- }
- else {
- PyErr_SetString(PyExc_SystemError,
- "old style getargs format uses new features");
- return 0;
- }
- }
-
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_SystemError,
- "new style getargs format but argument is not a tuple");
- return 0;
- }
-
- len = PyTuple_GET_SIZE(args);
-
- if (len < min || max < len) {
- if (message == NULL) {
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.150s%s takes %s %d argument%s "
- "(%ld given)",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()",
- min==max ? "exactly"
- : len < min ? "at least" : "at most",
- len < min ? min : max,
- (len < min ? min : max) == 1 ? "" : "s",
- Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
- message = msgbuf;
- }
- PyErr_SetString(PyExc_TypeError, message);
- return 0;
- }
-
- for (i = 0; i < len; i++) {
- if (*format == '|')
- format++;
- msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
- flags, levels, msgbuf,
- sizeof(msgbuf), &freelist);
- if (msg) {
- seterror(i+1, msg, levels, fname, msg);
- return cleanreturn(0, freelist);
- }
- }
-
- if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
- *format != '(' &&
- *format != '|' && *format != ':' && *format != ';') {
- PyErr_Format(PyExc_SystemError,
- "bad format string: %.200s", formatsave);
- return cleanreturn(0, freelist);
- }
-
- return cleanreturn(1, freelist);
+ char msgbuf[256];
+ int levels[32];
+ const char *fname = NULL;
+ const char *message = NULL;
+ int min = -1;
+ int max = 0;
+ int level = 0;
+ int endfmt = 0;
+ const char *formatsave = format;
+ Py_ssize_t i, len;
+ char *msg;
+ PyObject *freelist = NULL;
+ int compat = flags & FLAG_COMPAT;
+
+ assert(compat || (args != (PyObject*)NULL));
+ flags = flags & ~FLAG_COMPAT;
+
+ while (endfmt == 0) {
+ int c = *format++;
+ switch (c) {
+ case '(':
+ if (level == 0)
+ max++;
+ level++;
+ if (level >= 30)
+ Py_FatalError("too many tuple nesting levels "
+ "in argument format string");
+ break;
+ case ')':
+ if (level == 0)
+ Py_FatalError("excess ')' in getargs format");
+ else
+ level--;
+ break;
+ case '\0':
+ endfmt = 1;
+ break;
+ case ':':
+ fname = format;
+ endfmt = 1;
+ break;
+ case ';':
+ message = format;
+ endfmt = 1;
+ break;
+ default:
+ if (level == 0) {
+ if (c == 'O')
+ max++;
+ else if (isalpha(Py_CHARMASK(c))) {
+ if (c != 'e') /* skip encoded */
+ max++;
+ } else if (c == '|')
+ min = max;
+ }
+ break;
+ }
+ }
+
+ if (level != 0)
+ Py_FatalError(/* '(' */ "missing ')' in getargs format");
+
+ if (min < 0)
+ min = max;
+
+ format = formatsave;
+
+ if (compat) {
+ if (max == 0) {
+ if (args == NULL)
+ return 1;
+ PyOS_snprintf(msgbuf, sizeof(msgbuf),
+ "%.200s%s takes no arguments",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()");
+ PyErr_SetString(PyExc_TypeError, msgbuf);
+ return 0;
+ }
+ else if (min == 1 && max == 1) {
+ if (args == NULL) {
+ PyOS_snprintf(msgbuf, sizeof(msgbuf),
+ "%.200s%s takes at least one argument",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()");
+ PyErr_SetString(PyExc_TypeError, msgbuf);
+ return 0;
+ }
+ msg = convertitem(args, &format, p_va, flags, levels,
+ msgbuf, sizeof(msgbuf), &freelist);
+ if (msg == NULL)
+ return cleanreturn(1, freelist);
+ seterror(levels[0], msg, levels+1, fname, message);
+ return cleanreturn(0, freelist);
+ }
+ else {
+ PyErr_SetString(PyExc_SystemError,
+ "old style getargs format uses new features");
+ return 0;
+ }
+ }
+
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_SystemError,
+ "new style getargs format but argument is not a tuple");
+ return 0;
+ }
+
+ len = PyTuple_GET_SIZE(args);
+
+ if (len < min || max < len) {
+ if (message == NULL) {
+ PyOS_snprintf(msgbuf, sizeof(msgbuf),
+ "%.150s%s takes %s %d argument%s "
+ "(%ld given)",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()",
+ min==max ? "exactly"
+ : len < min ? "at least" : "at most",
+ len < min ? min : max,
+ (len < min ? min : max) == 1 ? "" : "s",
+ Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
+ message = msgbuf;
+ }
+ PyErr_SetString(PyExc_TypeError, message);
+ return 0;
+ }
+
+ for (i = 0; i < len; i++) {
+ if (*format == '|')
+ format++;
+ msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
+ flags, levels, msgbuf,
+ sizeof(msgbuf), &freelist);
+ if (msg) {
+ seterror(i+1, msg, levels, fname, msg);
+ return cleanreturn(0, freelist);
+ }
+ }
+
+ if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
+ *format != '(' &&
+ *format != '|' && *format != ':' && *format != ';') {
+ PyErr_Format(PyExc_SystemError,
+ "bad format string: %.200s", formatsave);
+ return cleanreturn(0, freelist);
+ }
+
+ return cleanreturn(1, freelist);
}
@@ -409,37 +409,37 @@ static void
seterror(int iarg, const char *msg, int *levels, const char *fname,
const char *message)
{
- char buf[512];
- int i;
- char *p = buf;
-
- if (PyErr_Occurred())
- return;
- else if (message == NULL) {
- if (fname != NULL) {
- PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
- p += strlen(p);
- }
- if (iarg != 0) {
- PyOS_snprintf(p, sizeof(buf) - (p - buf),
- "argument %d", iarg);
- i = 0;
- p += strlen(p);
- while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
- PyOS_snprintf(p, sizeof(buf) - (p - buf),
- ", item %d", levels[i]-1);
- p += strlen(p);
- i++;
- }
- }
- else {
- PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
- p += strlen(p);
- }
- PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
- message = buf;
- }
- PyErr_SetString(PyExc_TypeError, message);
+ char buf[512];
+ int i;
+ char *p = buf;
+
+ if (PyErr_Occurred())
+ return;
+ else if (message == NULL) {
+ if (fname != NULL) {
+ PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
+ p += strlen(p);
+ }
+ if (iarg != 0) {
+ PyOS_snprintf(p, sizeof(buf) - (p - buf),
+ "argument %d", iarg);
+ i = 0;
+ p += strlen(p);
+ while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
+ PyOS_snprintf(p, sizeof(buf) - (p - buf),
+ ", item %d", levels[i]-1);
+ p += strlen(p);
+ i++;
+ }
+ }
+ else {
+ PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
+ p += strlen(p);
+ }
+ PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
+ message = buf;
+ }
+ PyErr_SetString(PyExc_TypeError, message);
}
@@ -455,9 +455,9 @@ seterror(int iarg, const char *msg, int *levels, const char *fname,
*p_va is undefined,
*levels is a 0-terminated list of item numbers,
*msgbuf contains an error message, whose format is:
- "must be <typename1>, not <typename2>", where:
- <typename1> is the name of the expected type, and
- <typename2> is the name of the actual type,
+ "must be <typename1>, not <typename2>", where:
+ <typename1> is the name of the expected type, and
+ <typename2> is the name of the actual type,
and msgbuf is returned.
*/
@@ -466,72 +466,72 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
int *levels, char *msgbuf, size_t bufsize, int toplevel,
PyObject **freelist)
{
- int level = 0;
- int n = 0;
- const char *format = *p_format;
- int i;
-
- for (;;) {
- int c = *format++;
- if (c == '(') {
- if (level == 0)
- n++;
- level++;
- }
- else if (c == ')') {
- if (level == 0)
- break;
- level--;
- }
- else if (c == ':' || c == ';' || c == '\0')
- break;
- else if (level == 0 && isalpha(Py_CHARMASK(c)))
- n++;
- }
-
- if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
- levels[0] = 0;
- PyOS_snprintf(msgbuf, bufsize,
- toplevel ? "expected %d arguments, not %.50s" :
- "must be %d-item sequence, not %.50s",
- n,
- arg == Py_None ? "None" : arg->ob_type->tp_name);
- return msgbuf;
- }
-
- if ((i = PySequence_Size(arg)) != n) {
- levels[0] = 0;
- PyOS_snprintf(msgbuf, bufsize,
- toplevel ? "expected %d arguments, not %d" :
- "must be sequence of length %d, not %d",
- n, i);
- return msgbuf;
- }
-
- format = *p_format;
- for (i = 0; i < n; i++) {
- char *msg;
- PyObject *item;
- item = PySequence_GetItem(arg, i);
- if (item == NULL) {
- PyErr_Clear();
- levels[0] = i+1;
- levels[1] = 0;
- strncpy(msgbuf, "is not retrievable", bufsize);
- return msgbuf;
- }
- msg = convertitem(item, &format, p_va, flags, levels+1,
- msgbuf, bufsize, freelist);
- /* PySequence_GetItem calls tp->sq_item, which INCREFs */
- Py_XDECREF(item);
- if (msg != NULL) {
- levels[0] = i+1;
- return msg;
- }
- }
-
- *p_format = format;
- return NULL;
+ int level = 0;
+ int n = 0;
+ const char *format = *p_format;
+ int i;
+
+ for (;;) {
+ int c = *format++;
+ if (c == '(') {
+ if (level == 0)
+ n++;
+ level++;
+ }
+ else if (c == ')') {
+ if (level == 0)
+ break;
+ level--;
+ }
+ else if (c == ':' || c == ';' || c == '\0')
+ break;
+ else if (level == 0 && isalpha(Py_CHARMASK(c)))
+ n++;
+ }
+
+ if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
+ levels[0] = 0;
+ PyOS_snprintf(msgbuf, bufsize,
+ toplevel ? "expected %d arguments, not %.50s" :
+ "must be %d-item sequence, not %.50s",
+ n,
+ arg == Py_None ? "None" : arg->ob_type->tp_name);
+ return msgbuf;
+ }
+
+ if ((i = PySequence_Size(arg)) != n) {
+ levels[0] = 0;
+ PyOS_snprintf(msgbuf, bufsize,
+ toplevel ? "expected %d arguments, not %d" :
+ "must be sequence of length %d, not %d",
+ n, i);
+ return msgbuf;
+ }
+
+ format = *p_format;
+ for (i = 0; i < n; i++) {
+ char *msg;
+ PyObject *item;
+ item = PySequence_GetItem(arg, i);
+ if (item == NULL) {
+ PyErr_Clear();
+ levels[0] = i+1;
+ levels[1] = 0;
+ strncpy(msgbuf, "is not retrievable", bufsize);
+ return msgbuf;
+ }
+ msg = convertitem(item, &format, p_va, flags, levels+1,
+ msgbuf, bufsize, freelist);
+ /* PySequence_GetItem calls tp->sq_item, which INCREFs */
+ Py_XDECREF(item);
+ if (msg != NULL) {
+ levels[0] = i+1;
+ return msg;
+ }
+ }
+
+ *p_format = format;
+ return NULL;
}
@@ -541,43 +541,43 @@ static char *
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
{
- char *msg;
- const char *format = *p_format;
-
- if (*format == '(' /* ')' */) {
- format++;
- msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
- bufsize, 0, freelist);
- if (msg == NULL)
- format++;
- }
- else {
- msg = convertsimple(arg, &format, p_va, flags,
- msgbuf, bufsize, freelist);
- if (msg != NULL)
- levels[0] = 0;
- }
- if (msg == NULL)
- *p_format = format;
- return msg;
+ char *msg;
+ const char *format = *p_format;
+
+ if (*format == '(' /* ')' */) {
+ format++;
+ msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
+ bufsize, 0, freelist);
+ if (msg == NULL)
+ format++;
+ }
+ else {
+ msg = convertsimple(arg, &format, p_va, flags,
+ msgbuf, bufsize, freelist);
+ if (msg != NULL)
+ levels[0] = 0;
+ }
+ if (msg == NULL)
+ *p_format = format;
+ return msg;
}
#define UNICODE_DEFAULT_ENCODING(arg) \
- _PyUnicode_AsDefaultEncodedString(arg, NULL)
+ _PyUnicode_AsDefaultEncodedString(arg, NULL)
/* Format an error message generated by convertsimple(). */
static char *
converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
{
- assert(expected != NULL);
- assert(arg != NULL);
- PyOS_snprintf(msgbuf, bufsize,
- "must be %.50s, not %.50s", expected,
- arg == Py_None ? "None" : arg->ob_type->tp_name);
- return msgbuf;
+ assert(expected != NULL);
+ assert(arg != NULL);
+ PyOS_snprintf(msgbuf, bufsize,
+ "must be %.50s, not %.50s", expected,
+ arg == Py_None ? "None" : arg->ob_type->tp_name);
+ return msgbuf;
}
#define CONV_UNICODE "(unicode conversion error)"
@@ -587,13 +587,13 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
static int
float_argument_error(PyObject *arg)
{
- if (PyFloat_Check(arg)) {
- PyErr_SetString(PyExc_TypeError,
- "integer argument expected, got float" );
- return 1;
- }
- else
- return 0;
+ if (PyFloat_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError,
+ "integer argument expected, got float" );
+ return 1;
+ }
+ else
+ return 0;
}
/* Convert a non-tuple argument. Return NULL if conversion went OK,
@@ -609,836 +609,836 @@ static char *
convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
char *msgbuf, size_t bufsize, PyObject **freelist)
{
- /* For # codes */
-#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
- if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
- else q=va_arg(*p_va, int*);
+ /* For # codes */
+#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
+ if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
+ else q=va_arg(*p_va, int*);
#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
- const char *format = *p_format;
- char c = *format++;
- PyObject *uarg;
-
- switch (c) {
-
- case 'b': { /* unsigned byte -- very short int */
- char *p = va_arg(*p_va, char *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<b>", arg, msgbuf, bufsize);
- ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<b>", arg, msgbuf, bufsize);
- else if (ival < 0) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned byte integer is less than minimum");
- return converterr("integer<b>", arg, msgbuf, bufsize);
- }
- else if (ival > UCHAR_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "unsigned byte integer is greater than maximum");
- return converterr("integer<b>", arg, msgbuf, bufsize);
- }
- else
- *p = (unsigned char) ival;
- break;
- }
-
- case 'B': {/* byte sized bitfield - both signed and unsigned
- values allowed */
- char *p = va_arg(*p_va, char *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<B>", arg, msgbuf, bufsize);
- ival = PyLong_AsUnsignedLongMask(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<B>", arg, msgbuf, bufsize);
- else
- *p = (unsigned char) ival;
- break;
- }
-
- case 'h': {/* signed short int */
- short *p = va_arg(*p_va, short *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<h>", arg, msgbuf, bufsize);
- ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<h>", arg, msgbuf, bufsize);
- else if (ival < SHRT_MIN) {
- PyErr_SetString(PyExc_OverflowError,
- "signed short integer is less than minimum");
- return converterr("integer<h>", arg, msgbuf, bufsize);
- }
- else if (ival > SHRT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "signed short integer is greater than maximum");
- return converterr("integer<h>", arg, msgbuf, bufsize);
- }
- else
- *p = (short) ival;
- break;
- }
-
- case 'H': { /* short int sized bitfield, both signed and
- unsigned allowed */
- unsigned short *p = va_arg(*p_va, unsigned short *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<H>", arg, msgbuf, bufsize);
- ival = PyLong_AsUnsignedLongMask(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<H>", arg, msgbuf, bufsize);
- else
- *p = (unsigned short) ival;
- break;
- }
-
- case 'i': {/* signed int */
- int *p = va_arg(*p_va, int *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<i>", arg, msgbuf, bufsize);
- ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<i>", arg, msgbuf, bufsize);
- else if (ival > INT_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "signed integer is greater than maximum");
- return converterr("integer<i>", arg, msgbuf, bufsize);
- }
- else if (ival < INT_MIN) {
- PyErr_SetString(PyExc_OverflowError,
- "signed integer is less than minimum");
- return converterr("integer<i>", arg, msgbuf, bufsize);
- }
- else
- *p = ival;
- break;
- }
-
- case 'I': { /* int sized bitfield, both signed and
- unsigned allowed */
- unsigned int *p = va_arg(*p_va, unsigned int *);
- unsigned int ival;
- if (float_argument_error(arg))
- return converterr("integer<I>", arg, msgbuf, bufsize);
- ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
- if (ival == (unsigned int)-1 && PyErr_Occurred())
- return converterr("integer<I>", arg, msgbuf, bufsize);
- else
- *p = ival;
- break;
- }
-
- case 'n': /* Py_ssize_t */
- {
- PyObject *iobj;
- Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
- Py_ssize_t ival = -1;
- if (float_argument_error(arg))
- return converterr("integer<n>", arg, msgbuf, bufsize);
- iobj = PyNumber_Index(arg);
- if (iobj != NULL) {
- ival = PyLong_AsSsize_t(iobj);
- Py_DECREF(iobj);
- }
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<n>", arg, msgbuf, bufsize);
- *p = ival;
- break;
- }
- case 'l': {/* long int */
- long *p = va_arg(*p_va, long *);
- long ival;
- if (float_argument_error(arg))
- return converterr("integer<l>", arg, msgbuf, bufsize);
- ival = PyLong_AsLong(arg);
- if (ival == -1 && PyErr_Occurred())
- return converterr("integer<l>", arg, msgbuf, bufsize);
- else
- *p = ival;
- break;
- }
-
- case 'k': { /* long sized bitfield */
- unsigned long *p = va_arg(*p_va, unsigned long *);
- unsigned long ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongMask(arg);
- else
- return converterr("integer<k>", arg, msgbuf, bufsize);
- *p = ival;
- break;
- }
+ const char *format = *p_format;
+ char c = *format++;
+ PyObject *uarg;
+
+ switch (c) {
+
+ case 'b': { /* unsigned byte -- very short int */
+ char *p = va_arg(*p_va, char *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<b>", arg, msgbuf, bufsize);
+ ival = PyLong_AsLong(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<b>", arg, msgbuf, bufsize);
+ else if (ival < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned byte integer is less than minimum");
+ return converterr("integer<b>", arg, msgbuf, bufsize);
+ }
+ else if (ival > UCHAR_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "unsigned byte integer is greater than maximum");
+ return converterr("integer<b>", arg, msgbuf, bufsize);
+ }
+ else
+ *p = (unsigned char) ival;
+ break;
+ }
+
+ case 'B': {/* byte sized bitfield - both signed and unsigned
+ values allowed */
+ char *p = va_arg(*p_va, char *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<B>", arg, msgbuf, bufsize);
+ ival = PyLong_AsUnsignedLongMask(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<B>", arg, msgbuf, bufsize);
+ else
+ *p = (unsigned char) ival;
+ break;
+ }
+
+ case 'h': {/* signed short int */
+ short *p = va_arg(*p_va, short *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<h>", arg, msgbuf, bufsize);
+ ival = PyLong_AsLong(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<h>", arg, msgbuf, bufsize);
+ else if (ival < SHRT_MIN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed short integer is less than minimum");
+ return converterr("integer<h>", arg, msgbuf, bufsize);
+ }
+ else if (ival > SHRT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed short integer is greater than maximum");
+ return converterr("integer<h>", arg, msgbuf, bufsize);
+ }
+ else
+ *p = (short) ival;
+ break;
+ }
+
+ case 'H': { /* short int sized bitfield, both signed and
+ unsigned allowed */
+ unsigned short *p = va_arg(*p_va, unsigned short *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<H>", arg, msgbuf, bufsize);
+ ival = PyLong_AsUnsignedLongMask(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<H>", arg, msgbuf, bufsize);
+ else
+ *p = (unsigned short) ival;
+ break;
+ }
+
+ case 'i': {/* signed int */
+ int *p = va_arg(*p_va, int *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<i>", arg, msgbuf, bufsize);
+ ival = PyLong_AsLong(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<i>", arg, msgbuf, bufsize);
+ else if (ival > INT_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed integer is greater than maximum");
+ return converterr("integer<i>", arg, msgbuf, bufsize);
+ }
+ else if (ival < INT_MIN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "signed integer is less than minimum");
+ return converterr("integer<i>", arg, msgbuf, bufsize);
+ }
+ else
+ *p = ival;
+ break;
+ }
+
+ case 'I': { /* int sized bitfield, both signed and
+ unsigned allowed */
+ unsigned int *p = va_arg(*p_va, unsigned int *);
+ unsigned int ival;
+ if (float_argument_error(arg))
+ return converterr("integer<I>", arg, msgbuf, bufsize);
+ ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
+ if (ival == (unsigned int)-1 && PyErr_Occurred())
+ return converterr("integer<I>", arg, msgbuf, bufsize);
+ else
+ *p = ival;
+ break;
+ }
+
+ case 'n': /* Py_ssize_t */
+ {
+ PyObject *iobj;
+ Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
+ Py_ssize_t ival = -1;
+ if (float_argument_error(arg))
+ return converterr("integer<n>", arg, msgbuf, bufsize);
+ iobj = PyNumber_Index(arg);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<n>", arg, msgbuf, bufsize);
+ *p = ival;
+ break;
+ }
+ case 'l': {/* long int */
+ long *p = va_arg(*p_va, long *);
+ long ival;
+ if (float_argument_error(arg))
+ return converterr("integer<l>", arg, msgbuf, bufsize);
+ ival = PyLong_AsLong(arg);
+ if (ival == -1 && PyErr_Occurred())
+ return converterr("integer<l>", arg, msgbuf, bufsize);
+ else
+ *p = ival;
+ break;
+ }
+
+ case 'k': { /* long sized bitfield */
+ unsigned long *p = va_arg(*p_va, unsigned long *);
+ unsigned long ival;
+ if (PyLong_Check(arg))
+ ival = PyLong_AsUnsignedLongMask(arg);
+ else
+ return converterr("integer<k>", arg, msgbuf, bufsize);
+ *p = ival;
+ break;
+ }
#ifdef HAVE_LONG_LONG
- case 'L': {/* PY_LONG_LONG */
- PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
- PY_LONG_LONG ival = PyLong_AsLongLong( arg );
- if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
- return converterr("long<L>", arg, msgbuf, bufsize);
- } else {
- *p = ival;
- }
- break;
- }
-
- case 'K': { /* long long sized bitfield */
- unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
- unsigned PY_LONG_LONG ival;
- if (PyLong_Check(arg))
- ival = PyLong_AsUnsignedLongLongMask(arg);
- else
- return converterr("integer<K>", arg, msgbuf, bufsize);
- *p = ival;
- break;
- }
+ case 'L': {/* PY_LONG_LONG */
+ PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
+ PY_LONG_LONG ival = PyLong_AsLongLong( arg );
+ if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
+ return converterr("long<L>", arg, msgbuf, bufsize);
+ } else {
+ *p = ival;
+ }
+ break;
+ }
+
+ case 'K': { /* long long sized bitfield */
+ unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
+ unsigned PY_LONG_LONG ival;
+ if (PyLong_Check(arg))
+ ival = PyLong_AsUnsignedLongLongMask(arg);
+ else
+ return converterr("integer<K>", arg, msgbuf, bufsize);
+ *p = ival;
+ break;
+ }
#endif
- case 'f': {/* float */
- float *p = va_arg(*p_va, float *);
- double dval = PyFloat_AsDouble(arg);
- if (PyErr_Occurred())
- return converterr("float<f>", arg, msgbuf, bufsize);
- else
- *p = (float) dval;
- break;
- }
-
- case 'd': {/* double */
- double *p = va_arg(*p_va, double *);
- double dval = PyFloat_AsDouble(arg);
- if (PyErr_Occurred())
- return converterr("float<d>", arg, msgbuf, bufsize);
- else
- *p = dval;
- break;
- }
+ case 'f': {/* float */
+ float *p = va_arg(*p_va, float *);
+ double dval = PyFloat_AsDouble(arg);
+ if (PyErr_Occurred())
+ return converterr("float<f>", arg, msgbuf, bufsize);
+ else
+ *p = (float) dval;
+ break;
+ }
+
+ case 'd': {/* double */
+ double *p = va_arg(*p_va, double *);
+ double dval = PyFloat_AsDouble(arg);
+ if (PyErr_Occurred())
+ return converterr("float<d>", arg, msgbuf, bufsize);
+ else
+ *p = dval;
+ break;
+ }
#ifndef WITHOUT_COMPLEX
- case 'D': {/* complex double */
- Py_complex *p = va_arg(*p_va, Py_complex *);
- Py_complex cval;
- cval = PyComplex_AsCComplex(arg);
- if (PyErr_Occurred())
- return converterr("complex<D>", arg, msgbuf, bufsize);
- else
- *p = cval;
- break;
- }
+ case 'D': {/* complex double */
+ Py_complex *p = va_arg(*p_va, Py_complex *);
+ Py_complex cval;
+ cval = PyComplex_AsCComplex(arg);
+ if (PyErr_Occurred())
+ return converterr("complex<D>", arg, msgbuf, bufsize);
+ else
+ *p = cval;
+ break;
+ }
#endif /* WITHOUT_COMPLEX */
- case 'c': {/* char */
- char *p = va_arg(*p_va, char *);
- if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
- *p = PyBytes_AS_STRING(arg)[0];
- else
- return converterr("a byte string of length 1", arg, msgbuf, bufsize);
- break;
- }
-
- case 'C': {/* unicode char */
- int *p = va_arg(*p_va, int *);
- if (PyUnicode_Check(arg) &&
- PyUnicode_GET_SIZE(arg) == 1)
- *p = PyUnicode_AS_UNICODE(arg)[0];
- else
- return converterr("a unicode character", arg, msgbuf, bufsize);
- break;
- }
-
- /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w', 't' codes all
- need to be cleaned up! */
-
- case 's': {/* text string */
- if (*format == '*') {
- Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
-
- if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- PyBuffer_FillInfo(p, arg,
- PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
- 1, 0);
- }
- else { /* any buffer-like object */
- char *buf;
- if (getbuffer(arg, p, &buf) < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- }
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- format++;
- } else if (*format == '#') {
- void **p = (void **)va_arg(*p_va, char **);
- FETCH_SIZE;
-
- if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- *p = PyBytes_AS_STRING(uarg);
- STORE_SIZE(PyBytes_GET_SIZE(uarg));
- }
- else { /* any buffer-like object */
- /* XXX Really? */
- char *buf;
- Py_ssize_t count = convertbuffer(arg, p, &buf);
- if (count < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- STORE_SIZE(count);
- }
- format++;
- } else {
- char **p = va_arg(*p_va, char **);
-
- if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- *p = PyBytes_AS_STRING(uarg);
- }
- else
- return converterr("string", arg, msgbuf, bufsize);
- if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
- return converterr("string without null bytes",
- arg, msgbuf, bufsize);
- }
- break;
- }
-
- case 'y': {/* any buffer-like object, but not PyUnicode */
- void **p = (void **)va_arg(*p_va, char **);
- char *buf;
- Py_ssize_t count;
- if (*format == '*') {
- if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- format++;
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- break;
- }
- count = convertbuffer(arg, p, &buf);
- if (count < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- else if (*format == '#') {
- FETCH_SIZE;
- STORE_SIZE(count);
- format++;
- }
- break;
- }
-
- case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
- if (*format == '*') {
- Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
-
- if (arg == Py_None)
- PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
- else if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- PyBuffer_FillInfo(p, arg,
- PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
- 1, 0);
- }
- else { /* any buffer-like object */
- char *buf;
- if (getbuffer(arg, p, &buf) < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- }
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- format++;
- } else if (*format == '#') { /* any buffer-like object */
- void **p = (void **)va_arg(*p_va, char **);
- FETCH_SIZE;
-
- if (arg == Py_None) {
- *p = 0;
- STORE_SIZE(0);
- }
- else if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- *p = PyBytes_AS_STRING(uarg);
- STORE_SIZE(PyBytes_GET_SIZE(uarg));
- }
- else { /* any buffer-like object */
- /* XXX Really? */
- char *buf;
- Py_ssize_t count = convertbuffer(arg, p, &buf);
- if (count < 0)
- return converterr(buf, arg, msgbuf, bufsize);
- STORE_SIZE(count);
- }
- format++;
- } else {
- char **p = va_arg(*p_va, char **);
- uarg = NULL;
-
- if (arg == Py_None)
- *p = 0;
- else if (PyBytes_Check(arg)) {
- /* Enable null byte check below */
- uarg = arg;
- *p = PyBytes_AS_STRING(arg);
- }
- else if (PyUnicode_Check(arg)) {
- uarg = UNICODE_DEFAULT_ENCODING(arg);
- if (uarg == NULL)
- return converterr(CONV_UNICODE,
- arg, msgbuf, bufsize);
- *p = PyBytes_AS_STRING(uarg);
- }
- else
- return converterr("string or None",
- arg, msgbuf, bufsize);
- if (*format == '#') {
- FETCH_SIZE;
- assert(0); /* XXX redundant with if-case */
- if (arg == Py_None) {
- STORE_SIZE(0);
- }
- else {
- STORE_SIZE(PyBytes_Size(arg));
- }
- format++;
- }
- else if (*p != NULL && uarg != NULL &&
- (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
- return converterr(
- "string without null bytes or None",
- arg, msgbuf, bufsize);
- }
- break;
- }
-
- case 'Z': {/* unicode, may be NULL (None) */
- if (*format == '#') { /* any buffer-like object */
- Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
- FETCH_SIZE;
-
- if (arg == Py_None) {
- *p = 0;
- STORE_SIZE(0);
- }
- else if (PyUnicode_Check(arg)) {
- *p = PyUnicode_AS_UNICODE(arg);
- STORE_SIZE(PyUnicode_GET_SIZE(arg));
- }
- format++;
- } else {
- Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
-
- if (arg == Py_None)
- *p = 0;
- else if (PyUnicode_Check(arg))
- *p = PyUnicode_AS_UNICODE(arg);
- else
- return converterr("string or None",
- arg, msgbuf, bufsize);
- }
- break;
- }
-
- case 'e': {/* encoded string */
- char **buffer;
- const char *encoding;
- PyObject *s;
- int recode_strings;
- Py_ssize_t size;
- const char *ptr;
-
- /* Get 'e' parameter: the encoding name */
- encoding = (const char *)va_arg(*p_va, const char *);
- if (encoding == NULL)
- encoding = PyUnicode_GetDefaultEncoding();
-
- /* Get output buffer parameter:
- 's' (recode all objects via Unicode) or
- 't' (only recode non-string objects)
- */
- if (*format == 's')
- recode_strings = 1;
- else if (*format == 't')
- recode_strings = 0;
- else
- return converterr(
- "(unknown parser marker combination)",
- arg, msgbuf, bufsize);
- buffer = (char **)va_arg(*p_va, char **);
- format++;
- if (buffer == NULL)
- return converterr("(buffer is NULL)",
- arg, msgbuf, bufsize);
-
- /* Encode object */
- if (!recode_strings &&
- (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
- s = arg;
- Py_INCREF(s);
- if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
- return converterr("(AsCharBuffer failed)",
- arg, msgbuf, bufsize);
- }
- else {
- PyObject *u;
-
- /* Convert object to Unicode */
- u = PyUnicode_FromObject(arg);
- if (u == NULL)
- return converterr(
- "string or unicode or text buffer",
- arg, msgbuf, bufsize);
-
- /* Encode object; use default error handling */
- s = PyUnicode_AsEncodedString(u,
- encoding,
- NULL);
- Py_DECREF(u);
- if (s == NULL)
- return converterr("(encoding failed)",
- arg, msgbuf, bufsize);
- if (!PyBytes_Check(s)) {
- Py_DECREF(s);
- return converterr(
- "(encoder failed to return bytes)",
- arg, msgbuf, bufsize);
- }
- size = PyBytes_GET_SIZE(s);
- ptr = PyBytes_AS_STRING(s);
- if (ptr == NULL)
- ptr = "";
- }
-
- /* Write output; output is guaranteed to be 0-terminated */
- if (*format == '#') {
- /* Using buffer length parameter '#':
-
- - if *buffer is NULL, a new buffer of the
- needed size is allocated and the data
- copied into it; *buffer is updated to point
- to the new buffer; the caller is
- responsible for PyMem_Free()ing it after
- usage
-
- - if *buffer is not NULL, the data is
- copied to *buffer; *buffer_len has to be
- set to the size of the buffer on input;
- buffer overflow is signalled with an error;
- buffer has to provide enough room for the
- encoded string plus the trailing 0-byte
-
- - in both cases, *buffer_len is updated to
- the size of the buffer /excluding/ the
- trailing 0-byte
-
- */
- FETCH_SIZE;
-
- format++;
- if (q == NULL && q2 == NULL) {
- Py_DECREF(s);
- return converterr(
- "(buffer_len is NULL)",
- arg, msgbuf, bufsize);
- }
- if (*buffer == NULL) {
- *buffer = PyMem_NEW(char, size + 1);
- if (*buffer == NULL) {
- Py_DECREF(s);
- return converterr(
- "(memory error)",
- arg, msgbuf, bufsize);
- }
- if (addcleanup(*buffer, freelist, cleanup_ptr)) {
- Py_DECREF(s);
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- } else {
- if (size + 1 > BUFFER_LEN) {
- Py_DECREF(s);
- return converterr(
- "(buffer overflow)",
- arg, msgbuf, bufsize);
- }
- }
- memcpy(*buffer, ptr, size+1);
- STORE_SIZE(size);
- } else {
- /* Using a 0-terminated buffer:
-
- - the encoded string has to be 0-terminated
- for this variant to work; if it is not, an
- error raised
-
- - a new buffer of the needed size is
- allocated and the data copied into it;
- *buffer is updated to point to the new
- buffer; the caller is responsible for
- PyMem_Free()ing it after usage
-
- */
- if ((Py_ssize_t)strlen(ptr) != size) {
- Py_DECREF(s);
- return converterr(
- "encoded string without NULL bytes",
- arg, msgbuf, bufsize);
- }
- *buffer = PyMem_NEW(char, size + 1);
- if (*buffer == NULL) {
- Py_DECREF(s);
- return converterr("(memory error)",
- arg, msgbuf, bufsize);
- }
- if (addcleanup(*buffer, freelist, cleanup_ptr)) {
- Py_DECREF(s);
- return converterr("(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- memcpy(*buffer, ptr, size+1);
- }
- Py_DECREF(s);
- break;
- }
-
- case 'u': {/* raw unicode buffer (Py_UNICODE *) */
- Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
- if (!PyUnicode_Check(arg))
- return converterr("str", arg, msgbuf, bufsize);
- *p = PyUnicode_AS_UNICODE(arg);
- if (*format == '#') { /* store pointer and size */
- FETCH_SIZE;
- STORE_SIZE(PyUnicode_GET_SIZE(arg));
- format++;
- }
- break;
- }
-
- case 'S': { /* PyBytes object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyBytes_Check(arg))
- *p = arg;
- else
- return converterr("bytes", arg, msgbuf, bufsize);
- break;
- }
-
- case 'Y': { /* PyByteArray object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyByteArray_Check(arg))
- *p = arg;
- else
- return converterr("buffer", arg, msgbuf, bufsize);
- break;
- }
-
- case 'U': { /* PyUnicode object */
- PyObject **p = va_arg(*p_va, PyObject **);
- if (PyUnicode_Check(arg))
- *p = arg;
- else
- return converterr("str", arg, msgbuf, bufsize);
- break;
- }
-
- case 'O': { /* object */
- PyTypeObject *type;
- PyObject **p;
- if (*format == '!') {
- type = va_arg(*p_va, PyTypeObject*);
- p = va_arg(*p_va, PyObject **);
- format++;
- if (PyType_IsSubtype(arg->ob_type, type))
- *p = arg;
- else
- return converterr(type->tp_name, arg, msgbuf, bufsize);
-
- }
- else if (*format == '?') {
- inquiry pred = va_arg(*p_va, inquiry);
- p = va_arg(*p_va, PyObject **);
- format++;
- if ((*pred)(arg))
- *p = arg;
- else
- return converterr("(unspecified)",
- arg, msgbuf, bufsize);
-
- }
- else if (*format == '&') {
- typedef int (*converter)(PyObject *, void *);
- converter convert = va_arg(*p_va, converter);
- void *addr = va_arg(*p_va, void *);
- int res;
- format++;
- if (! (res = (*convert)(arg, addr)))
- return converterr("(unspecified)",
- arg, msgbuf, bufsize);
- if (res == Py_CLEANUP_SUPPORTED &&
- addcleanup_convert(addr, freelist, convert) == -1)
- return converterr("(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- else {
- p = va_arg(*p_va, PyObject **);
- *p = arg;
- }
- break;
- }
-
-
- case 'w': { /* memory buffer, read-write access */
- void **p = va_arg(*p_va, void **);
- PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
- Py_ssize_t count;
- int temp=-1;
- Py_buffer view;
-
- if (pb && pb->bf_releasebuffer && *format != '*')
- /* Buffer must be released, yet caller does not use
- the Py_buffer protocol. */
- return converterr("pinned buffer", arg, msgbuf, bufsize);
-
-
- if (pb && pb->bf_getbuffer && *format == '*') {
- /* Caller is interested in Py_buffer, and the object
- supports it directly. */
- format++;
- if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
- PyErr_Clear();
- return converterr("read-write buffer", arg, msgbuf, bufsize);
- }
- if (addcleanup(p, freelist, cleanup_buffer)) {
- return converterr(
- "(cleanup problem)",
- arg, msgbuf, bufsize);
- }
- if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
- return converterr("contiguous buffer", arg, msgbuf, bufsize);
- break;
- }
-
- /* Here we have processed w*, only w and w# remain. */
- if (pb == NULL ||
- pb->bf_getbuffer == NULL ||
- ((temp = PyObject_GetBuffer(arg, &view,
- PyBUF_SIMPLE)) != 0) ||
- view.readonly == 1) {
- if (temp==0) {
- PyBuffer_Release(&view);
- }
- return converterr("single-segment read-write buffer",
- arg, msgbuf, bufsize);
+ case 'c': {/* char */
+ char *p = va_arg(*p_va, char *);
+ if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
+ *p = PyBytes_AS_STRING(arg)[0];
+ else
+ return converterr("a byte string of length 1", arg, msgbuf, bufsize);
+ break;
+ }
+
+ case 'C': {/* unicode char */
+ int *p = va_arg(*p_va, int *);
+ if (PyUnicode_Check(arg) &&
+ PyUnicode_GET_SIZE(arg) == 1)
+ *p = PyUnicode_AS_UNICODE(arg)[0];
+ else
+ return converterr("a unicode character", arg, msgbuf, bufsize);
+ break;
+ }
+
+ /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w', 't' codes all
+ need to be cleaned up! */
+
+ case 's': {/* text string */
+ if (*format == '*') {
+ Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
+
+ if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ PyBuffer_FillInfo(p, arg,
+ PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
+ 1, 0);
+ }
+ else { /* any buffer-like object */
+ char *buf;
+ if (getbuffer(arg, p, &buf) < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ }
+ if (addcleanup(p, freelist, cleanup_buffer)) {
+ return converterr(
+ "(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ format++;
+ } else if (*format == '#') {
+ void **p = (void **)va_arg(*p_va, char **);
+ FETCH_SIZE;
+
+ if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ *p = PyBytes_AS_STRING(uarg);
+ STORE_SIZE(PyBytes_GET_SIZE(uarg));
+ }
+ else { /* any buffer-like object */
+ /* XXX Really? */
+ char *buf;
+ Py_ssize_t count = convertbuffer(arg, p, &buf);
+ if (count < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ STORE_SIZE(count);
+ }
+ format++;
+ } else {
+ char **p = va_arg(*p_va, char **);
+
+ if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ *p = PyBytes_AS_STRING(uarg);
+ }
+ else
+ return converterr("string", arg, msgbuf, bufsize);
+ if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
+ return converterr("string without null bytes",
+ arg, msgbuf, bufsize);
+ }
+ break;
+ }
+
+ case 'y': {/* any buffer-like object, but not PyUnicode */
+ void **p = (void **)va_arg(*p_va, char **);
+ char *buf;
+ Py_ssize_t count;
+ if (*format == '*') {
+ if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ format++;
+ if (addcleanup(p, freelist, cleanup_buffer)) {
+ return converterr(
+ "(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ break;
+ }
+ count = convertbuffer(arg, p, &buf);
+ if (count < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ else if (*format == '#') {
+ FETCH_SIZE;
+ STORE_SIZE(count);
+ format++;
+ }
+ break;
+ }
+
+ case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
+ if (*format == '*') {
+ Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
+
+ if (arg == Py_None)
+ PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
+ else if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ PyBuffer_FillInfo(p, arg,
+ PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
+ 1, 0);
+ }
+ else { /* any buffer-like object */
+ char *buf;
+ if (getbuffer(arg, p, &buf) < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ }
+ if (addcleanup(p, freelist, cleanup_buffer)) {
+ return converterr(
+ "(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ format++;
+ } else if (*format == '#') { /* any buffer-like object */
+ void **p = (void **)va_arg(*p_va, char **);
+ FETCH_SIZE;
+
+ if (arg == Py_None) {
+ *p = 0;
+ STORE_SIZE(0);
+ }
+ else if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ *p = PyBytes_AS_STRING(uarg);
+ STORE_SIZE(PyBytes_GET_SIZE(uarg));
+ }
+ else { /* any buffer-like object */
+ /* XXX Really? */
+ char *buf;
+ Py_ssize_t count = convertbuffer(arg, p, &buf);
+ if (count < 0)
+ return converterr(buf, arg, msgbuf, bufsize);
+ STORE_SIZE(count);
+ }
+ format++;
+ } else {
+ char **p = va_arg(*p_va, char **);
+ uarg = NULL;
+
+ if (arg == Py_None)
+ *p = 0;
+ else if (PyBytes_Check(arg)) {
+ /* Enable null byte check below */
+ uarg = arg;
+ *p = PyBytes_AS_STRING(arg);
+ }
+ else if (PyUnicode_Check(arg)) {
+ uarg = UNICODE_DEFAULT_ENCODING(arg);
+ if (uarg == NULL)
+ return converterr(CONV_UNICODE,
+ arg, msgbuf, bufsize);
+ *p = PyBytes_AS_STRING(uarg);
+ }
+ else
+ return converterr("string or None",
+ arg, msgbuf, bufsize);
+ if (*format == '#') {
+ FETCH_SIZE;
+ assert(0); /* XXX redundant with if-case */
+ if (arg == Py_None) {
+ STORE_SIZE(0);
+ }
+ else {
+ STORE_SIZE(PyBytes_Size(arg));
+ }
+ format++;
+ }
+ else if (*p != NULL && uarg != NULL &&
+ (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
+ return converterr(
+ "string without null bytes or None",
+ arg, msgbuf, bufsize);
+ }
+ break;
+ }
+
+ case 'Z': {/* unicode, may be NULL (None) */
+ if (*format == '#') { /* any buffer-like object */
+ Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
+ FETCH_SIZE;
+
+ if (arg == Py_None) {
+ *p = 0;
+ STORE_SIZE(0);
+ }
+ else if (PyUnicode_Check(arg)) {
+ *p = PyUnicode_AS_UNICODE(arg);
+ STORE_SIZE(PyUnicode_GET_SIZE(arg));
+ }
+ format++;
+ } else {
+ Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
+
+ if (arg == Py_None)
+ *p = 0;
+ else if (PyUnicode_Check(arg))
+ *p = PyUnicode_AS_UNICODE(arg);
+ else
+ return converterr("string or None",
+ arg, msgbuf, bufsize);
+ }
+ break;
+ }
+
+ case 'e': {/* encoded string */
+ char **buffer;
+ const char *encoding;
+ PyObject *s;
+ int recode_strings;
+ Py_ssize_t size;
+ const char *ptr;
+
+ /* Get 'e' parameter: the encoding name */
+ encoding = (const char *)va_arg(*p_va, const char *);
+ if (encoding == NULL)
+ encoding = PyUnicode_GetDefaultEncoding();
+
+ /* Get output buffer parameter:
+ 's' (recode all objects via Unicode) or
+ 't' (only recode non-string objects)
+ */
+ if (*format == 's')
+ recode_strings = 1;
+ else if (*format == 't')
+ recode_strings = 0;
+ else
+ return converterr(
+ "(unknown parser marker combination)",
+ arg, msgbuf, bufsize);
+ buffer = (char **)va_arg(*p_va, char **);
+ format++;
+ if (buffer == NULL)
+ return converterr("(buffer is NULL)",
+ arg, msgbuf, bufsize);
+
+ /* Encode object */
+ if (!recode_strings &&
+ (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
+ s = arg;
+ Py_INCREF(s);
+ if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
+ return converterr("(AsCharBuffer failed)",
+ arg, msgbuf, bufsize);
+ }
+ else {
+ PyObject *u;
+
+ /* Convert object to Unicode */
+ u = PyUnicode_FromObject(arg);
+ if (u == NULL)
+ return converterr(
+ "string or unicode or text buffer",
+ arg, msgbuf, bufsize);
+
+ /* Encode object; use default error handling */
+ s = PyUnicode_AsEncodedString(u,
+ encoding,
+ NULL);
+ Py_DECREF(u);
+ if (s == NULL)
+ return converterr("(encoding failed)",
+ arg, msgbuf, bufsize);
+ if (!PyBytes_Check(s)) {
+ Py_DECREF(s);
+ return converterr(
+ "(encoder failed to return bytes)",
+ arg, msgbuf, bufsize);
+ }
+ size = PyBytes_GET_SIZE(s);
+ ptr = PyBytes_AS_STRING(s);
+ if (ptr == NULL)
+ ptr = "";
+ }
+
+ /* Write output; output is guaranteed to be 0-terminated */
+ if (*format == '#') {
+ /* Using buffer length parameter '#':
+
+ - if *buffer is NULL, a new buffer of the
+ needed size is allocated and the data
+ copied into it; *buffer is updated to point
+ to the new buffer; the caller is
+ responsible for PyMem_Free()ing it after
+ usage
+
+ - if *buffer is not NULL, the data is
+ copied to *buffer; *buffer_len has to be
+ set to the size of the buffer on input;
+ buffer overflow is signalled with an error;
+ buffer has to provide enough room for the
+ encoded string plus the trailing 0-byte
+
+ - in both cases, *buffer_len is updated to
+ the size of the buffer /excluding/ the
+ trailing 0-byte
+
+ */
+ FETCH_SIZE;
+
+ format++;
+ if (q == NULL && q2 == NULL) {
+ Py_DECREF(s);
+ return converterr(
+ "(buffer_len is NULL)",
+ arg, msgbuf, bufsize);
+ }
+ if (*buffer == NULL) {
+ *buffer = PyMem_NEW(char, size + 1);
+ if (*buffer == NULL) {
+ Py_DECREF(s);
+ return converterr(
+ "(memory error)",
+ arg, msgbuf, bufsize);
}
+ if (addcleanup(*buffer, freelist, cleanup_ptr)) {
+ Py_DECREF(s);
+ return converterr(
+ "(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ } else {
+ if (size + 1 > BUFFER_LEN) {
+ Py_DECREF(s);
+ return converterr(
+ "(buffer overflow)",
+ arg, msgbuf, bufsize);
+ }
+ }
+ memcpy(*buffer, ptr, size+1);
+ STORE_SIZE(size);
+ } else {
+ /* Using a 0-terminated buffer:
+
+ - the encoded string has to be 0-terminated
+ for this variant to work; if it is not, an
+ error raised
+
+ - a new buffer of the needed size is
+ allocated and the data copied into it;
+ *buffer is updated to point to the new
+ buffer; the caller is responsible for
+ PyMem_Free()ing it after usage
+
+ */
+ if ((Py_ssize_t)strlen(ptr) != size) {
+ Py_DECREF(s);
+ return converterr(
+ "encoded string without NULL bytes",
+ arg, msgbuf, bufsize);
+ }
+ *buffer = PyMem_NEW(char, size + 1);
+ if (*buffer == NULL) {
+ Py_DECREF(s);
+ return converterr("(memory error)",
+ arg, msgbuf, bufsize);
+ }
+ if (addcleanup(*buffer, freelist, cleanup_ptr)) {
+ Py_DECREF(s);
+ return converterr("(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ memcpy(*buffer, ptr, size+1);
+ }
+ Py_DECREF(s);
+ break;
+ }
+
+ case 'u': {/* raw unicode buffer (Py_UNICODE *) */
+ Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
+ if (!PyUnicode_Check(arg))
+ return converterr("str", arg, msgbuf, bufsize);
+ *p = PyUnicode_AS_UNICODE(arg);
+ if (*format == '#') { /* store pointer and size */
+ FETCH_SIZE;
+ STORE_SIZE(PyUnicode_GET_SIZE(arg));
+ format++;
+ }
+ break;
+ }
+
+ case 'S': { /* PyBytes object */
+ PyObject **p = va_arg(*p_va, PyObject **);
+ if (PyBytes_Check(arg))
+ *p = arg;
+ else
+ return converterr("bytes", arg, msgbuf, bufsize);
+ break;
+ }
+
+ case 'Y': { /* PyByteArray object */
+ PyObject **p = va_arg(*p_va, PyObject **);
+ if (PyByteArray_Check(arg))
+ *p = arg;
+ else
+ return converterr("buffer", arg, msgbuf, bufsize);
+ break;
+ }
+
+ case 'U': { /* PyUnicode object */
+ PyObject **p = va_arg(*p_va, PyObject **);
+ if (PyUnicode_Check(arg))
+ *p = arg;
+ else
+ return converterr("str", arg, msgbuf, bufsize);
+ break;
+ }
+
+ case 'O': { /* object */
+ PyTypeObject *type;
+ PyObject **p;
+ if (*format == '!') {
+ type = va_arg(*p_va, PyTypeObject*);
+ p = va_arg(*p_va, PyObject **);
+ format++;
+ if (PyType_IsSubtype(arg->ob_type, type))
+ *p = arg;
+ else
+ return converterr(type->tp_name, arg, msgbuf, bufsize);
+
+ }
+ else if (*format == '?') {
+ inquiry pred = va_arg(*p_va, inquiry);
+ p = va_arg(*p_va, PyObject **);
+ format++;
+ if ((*pred)(arg))
+ *p = arg;
+ else
+ return converterr("(unspecified)",
+ arg, msgbuf, bufsize);
+
+ }
+ else if (*format == '&') {
+ typedef int (*converter)(PyObject *, void *);
+ converter convert = va_arg(*p_va, converter);
+ void *addr = va_arg(*p_va, void *);
+ int res;
+ format++;
+ if (! (res = (*convert)(arg, addr)))
+ return converterr("(unspecified)",
+ arg, msgbuf, bufsize);
+ if (res == Py_CLEANUP_SUPPORTED &&
+ addcleanup_convert(addr, freelist, convert) == -1)
+ return converterr("(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ else {
+ p = va_arg(*p_va, PyObject **);
+ *p = arg;
+ }
+ break;
+ }
+
+
+ case 'w': { /* memory buffer, read-write access */
+ void **p = va_arg(*p_va, void **);
+ PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
+ Py_ssize_t count;
+ int temp=-1;
+ Py_buffer view;
+
+ if (pb && pb->bf_releasebuffer && *format != '*')
+ /* Buffer must be released, yet caller does not use
+ the Py_buffer protocol. */
+ return converterr("pinned buffer", arg, msgbuf, bufsize);
+
+
+ if (pb && pb->bf_getbuffer && *format == '*') {
+ /* Caller is interested in Py_buffer, and the object
+ supports it directly. */
+ format++;
+ if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
+ PyErr_Clear();
+ return converterr("read-write buffer", arg, msgbuf, bufsize);
+ }
+ if (addcleanup(p, freelist, cleanup_buffer)) {
+ return converterr(
+ "(cleanup problem)",
+ arg, msgbuf, bufsize);
+ }
+ if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
+ return converterr("contiguous buffer", arg, msgbuf, bufsize);
+ break;
+ }
+
+ /* Here we have processed w*, only w and w# remain. */
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL ||
+ ((temp = PyObject_GetBuffer(arg, &view,
+ PyBUF_SIMPLE)) != 0) ||
+ view.readonly == 1) {
+ if (temp==0) {
+ PyBuffer_Release(&view);
+ }
+ return converterr("single-segment read-write buffer",
+ arg, msgbuf, bufsize);
+ }
+
+ if ((count = view.len) < 0)
+ return converterr("(unspecified)", arg, msgbuf, bufsize);
+ *p = view.buf;
+ if (*format == '#') {
+ FETCH_SIZE;
+ STORE_SIZE(count);
+ format++;
+ }
+ break;
+ }
+
+ /*TEO: This can be eliminated --- here only for backward
+ compatibility */
+ case 't': { /* 8-bit character buffer, read-only access */
+ char **p = va_arg(*p_va, char **);
+ PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
+ Py_ssize_t count;
+ Py_buffer view;
+
+ if (*format++ != '#')
+ return converterr(
+ "invalid use of 't' format character",
+ arg, msgbuf, bufsize);
+ if (pb == NULL || pb->bf_getbuffer == NULL)
+ return converterr(
+ "bytes or read-only character buffer",
+ arg, msgbuf, bufsize);
+
+ if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0)
+ return converterr("string or single-segment read-only buffer",
+ arg, msgbuf, bufsize);
+
+ count = view.len;
+ *p = view.buf;
+ if (pb->bf_releasebuffer)
+ return converterr(
+ "string or pinned buffer",
+ arg, msgbuf, bufsize);
+
+ PyBuffer_Release(&view);
+
+ if (count < 0)
+ return converterr("(unspecified)", arg, msgbuf, bufsize);
+ {
+ FETCH_SIZE;
+ STORE_SIZE(count);
+ }
+ break;
+ }
+
+ default:
+ return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
+
+ }
- if ((count = view.len) < 0)
- return converterr("(unspecified)", arg, msgbuf, bufsize);
- *p = view.buf;
- if (*format == '#') {
- FETCH_SIZE;
- STORE_SIZE(count);
- format++;
- }
- break;
- }
-
- /*TEO: This can be eliminated --- here only for backward
- compatibility */
- case 't': { /* 8-bit character buffer, read-only access */
- char **p = va_arg(*p_va, char **);
- PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
- Py_ssize_t count;
- Py_buffer view;
-
- if (*format++ != '#')
- return converterr(
- "invalid use of 't' format character",
- arg, msgbuf, bufsize);
- if (pb == NULL || pb->bf_getbuffer == NULL)
- return converterr(
- "bytes or read-only character buffer",
- arg, msgbuf, bufsize);
-
- if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0)
- return converterr("string or single-segment read-only buffer",
- arg, msgbuf, bufsize);
-
- count = view.len;
- *p = view.buf;
- if (pb->bf_releasebuffer)
- return converterr(
- "string or pinned buffer",
- arg, msgbuf, bufsize);
-
- PyBuffer_Release(&view);
-
- if (count < 0)
- return converterr("(unspecified)", arg, msgbuf, bufsize);
- {
- FETCH_SIZE;
- STORE_SIZE(count);
- }
- break;
- }
-
- default:
- return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
-
- }
-
- *p_format = format;
- return NULL;
+ *p_format = format;
+ return NULL;
}
static Py_ssize_t
convertbuffer(PyObject *arg, void **p, char **errmsg)
{
- PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
- Py_ssize_t count;
- Py_buffer view;
-
- *errmsg = NULL;
- *p = NULL;
- if (pb == NULL ||
- pb->bf_getbuffer == NULL ||
- pb->bf_releasebuffer != NULL) {
- *errmsg = "bytes or read-only buffer";
- return -1;
- }
-
- if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0) {
- *errmsg = "bytes or single-segment read-only buffer";
- return -1;
- }
- count = view.len;
- *p = view.buf;
- PyBuffer_Release(&view);
- return count;
+ PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
+ Py_ssize_t count;
+ Py_buffer view;
+
+ *errmsg = NULL;
+ *p = NULL;
+ if (pb == NULL ||
+ pb->bf_getbuffer == NULL ||
+ pb->bf_releasebuffer != NULL) {
+ *errmsg = "bytes or read-only buffer";
+ return -1;
+ }
+
+ if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0) {
+ *errmsg = "bytes or single-segment read-only buffer";
+ return -1;
+ }
+ count = view.len;
+ *p = view.buf;
+ PyBuffer_Release(&view);
+ return count;
}
/* XXX for 3.x, getbuffer and convertbuffer can probably
@@ -1446,32 +1446,32 @@ convertbuffer(PyObject *arg, void **p, char **errmsg)
static int
getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
{
- void *buf;
- Py_ssize_t count;
- PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
- if (pb == NULL) {
- *errmsg = "bytes or buffer";
- return -1;
- }
- if (pb->bf_getbuffer) {
- if (PyObject_GetBuffer(arg, view, 0) < 0) {
- *errmsg = "convertible to a buffer";
- return -1;
- }
- if (!PyBuffer_IsContiguous(view, 'C')) {
- *errmsg = "contiguous buffer";
- return -1;
- }
- return 0;
- }
-
- count = convertbuffer(arg, &buf, errmsg);
- if (count < 0) {
- *errmsg = "convertible to a buffer";
- return count;
- }
- PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
- return 0;
+ void *buf;
+ Py_ssize_t count;
+ PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
+ if (pb == NULL) {
+ *errmsg = "bytes or buffer";
+ return -1;
+ }
+ if (pb->bf_getbuffer) {
+ if (PyObject_GetBuffer(arg, view, 0) < 0) {
+ *errmsg = "convertible to a buffer";
+ return -1;
+ }
+ if (!PyBuffer_IsContiguous(view, 'C')) {
+ *errmsg = "contiguous buffer";
+ return -1;
+ }
+ return 0;
+ }
+
+ count = convertbuffer(arg, &buf, errmsg);
+ if (count < 0) {
+ *errmsg = "convertible to a buffer";
+ return count;
+ }
+ PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
+ return 0;
}
/* Support for keyword arguments donated by
@@ -1480,51 +1480,51 @@ getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
/* Return false (0) for error, else true. */
int
PyArg_ParseTupleAndKeywords(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, ...)
+ PyObject *keywords,
+ const char *format,
+ char **kwlist, ...)
{
- int retval;
- va_list va;
-
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
-
- va_start(va, kwlist);
- retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
- va_end(va);
- return retval;
+ int retval;
+ va_list va;
+
+ if ((args == NULL || !PyTuple_Check(args)) ||
+ (keywords != NULL && !PyDict_Check(keywords)) ||
+ format == NULL ||
+ kwlist == NULL)
+ {
+ PyErr_BadInternalCall();
+ return 0;
+ }
+
+ va_start(va, kwlist);
+ retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
+ va_end(va);
+ return retval;
}
int
_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, ...)
+ PyObject *keywords,
+ const char *format,
+ char **kwlist, ...)
{
- int retval;
- va_list va;
-
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
-
- va_start(va, kwlist);
- retval = vgetargskeywords(args, keywords, format,
- kwlist, &va, FLAG_SIZE_T);
- va_end(va);
- return retval;
+ int retval;
+ va_list va;
+
+ if ((args == NULL || !PyTuple_Check(args)) ||
+ (keywords != NULL && !PyDict_Check(keywords)) ||
+ format == NULL ||
+ kwlist == NULL)
+ {
+ PyErr_BadInternalCall();
+ return 0;
+ }
+
+ va_start(va, kwlist);
+ retval = vgetargskeywords(args, keywords, format,
+ kwlist, &va, FLAG_SIZE_T);
+ va_end(va);
+ return retval;
}
@@ -1534,419 +1534,419 @@ PyArg_VaParseTupleAndKeywords(PyObject *args,
const char *format,
char **kwlist, va_list va)
{
- int retval;
- va_list lva;
-
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
+ int retval;
+ va_list lva;
+
+ if ((args == NULL || !PyTuple_Check(args)) ||
+ (keywords != NULL && !PyDict_Check(keywords)) ||
+ format == NULL ||
+ kwlist == NULL)
+ {
+ PyErr_BadInternalCall();
+ return 0;
+ }
#ifdef VA_LIST_IS_ARRAY
- memcpy(lva, va, sizeof(va_list));
+ memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(lva, va);
+ __va_copy(lva, va);
#else
- lva = va;
+ lva = va;
#endif
#endif
- retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
- return retval;
+ retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
+ return retval;
}
int
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
- PyObject *keywords,
- const char *format,
- char **kwlist, va_list va)
+ PyObject *keywords,
+ const char *format,
+ char **kwlist, va_list va)
{
- int retval;
- va_list lva;
-
- if ((args == NULL || !PyTuple_Check(args)) ||
- (keywords != NULL && !PyDict_Check(keywords)) ||
- format == NULL ||
- kwlist == NULL)
- {
- PyErr_BadInternalCall();
- return 0;
- }
+ int retval;
+ va_list lva;
+
+ if ((args == NULL || !PyTuple_Check(args)) ||
+ (keywords != NULL && !PyDict_Check(keywords)) ||
+ format == NULL ||
+ kwlist == NULL)
+ {
+ PyErr_BadInternalCall();
+ return 0;
+ }
#ifdef VA_LIST_IS_ARRAY
- memcpy(lva, va, sizeof(va_list));
+ memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(lva, va);
+ __va_copy(lva, va);
#else
- lva = va;
+ lva = va;
#endif
#endif
- retval = vgetargskeywords(args, keywords, format,
- kwlist, &lva, FLAG_SIZE_T);
- return retval;
+ retval = vgetargskeywords(args, keywords, format,
+ kwlist, &lva, FLAG_SIZE_T);
+ return retval;
}
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
static int
vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
- char **kwlist, va_list *p_va, int flags)
+ char **kwlist, va_list *p_va, int flags)
{
- char msgbuf[512];
- int levels[32];
- const char *fname, *msg, *custom_msg, *keyword;
- int min = INT_MAX;
- int i, len, nargs, nkeywords;
- PyObject *freelist = NULL, *current_arg;
-
- assert(args != NULL && PyTuple_Check(args));
- assert(keywords == NULL || PyDict_Check(keywords));
- assert(format != NULL);
- assert(kwlist != NULL);
- assert(p_va != NULL);
-
- /* grab the function name or custom error msg first (mutually exclusive) */
- fname = strchr(format, ':');
- if (fname) {
- fname++;
- custom_msg = NULL;
- }
- else {
- custom_msg = strchr(format,';');
- if (custom_msg)
- custom_msg++;
- }
-
- /* scan kwlist and get greatest possible nbr of args */
- for (len=0; kwlist[len]; len++)
- continue;
-
- nargs = PyTuple_GET_SIZE(args);
- nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
- if (nargs + nkeywords > len) {
- PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
- "argument%s (%d given)",
- (fname == NULL) ? "function" : fname,
- (fname == NULL) ? "" : "()",
- len,
- (len == 1) ? "" : "s",
- nargs + nkeywords);
- return 0;
- }
-
- /* convert tuple args and keyword args in same loop, using kwlist to drive process */
- for (i = 0; i < len; i++) {
- keyword = kwlist[i];
- if (*format == '|') {
- min = i;
- format++;
- }
- if (IS_END_OF_FORMAT(*format)) {
- PyErr_Format(PyExc_RuntimeError,
- "More keyword list entries (%d) than "
- "format specifiers (%d)", len, i);
- return cleanreturn(0, freelist);
- }
- current_arg = NULL;
- if (nkeywords) {
- current_arg = PyDict_GetItemString(keywords, keyword);
- }
- if (current_arg) {
- --nkeywords;
- if (i < nargs) {
- /* arg present in tuple and in dict */
- PyErr_Format(PyExc_TypeError,
- "Argument given by name ('%s') "
- "and position (%d)",
- keyword, i+1);
- return cleanreturn(0, freelist);
- }
- }
- else if (nkeywords && PyErr_Occurred())
- return cleanreturn(0, freelist);
- else if (i < nargs)
- current_arg = PyTuple_GET_ITEM(args, i);
-
- if (current_arg) {
- msg = convertitem(current_arg, &format, p_va, flags,
- levels, msgbuf, sizeof(msgbuf), &freelist);
- if (msg) {
- seterror(i+1, msg, levels, fname, custom_msg);
- return cleanreturn(0, freelist);
- }
- continue;
- }
-
- if (i < min) {
- PyErr_Format(PyExc_TypeError, "Required argument "
- "'%s' (pos %d) not found",
- keyword, i+1);
- return cleanreturn(0, freelist);
- }
- /* current code reports success when all required args
- * fulfilled and no keyword args left, with no further
- * validation. XXX Maybe skip this in debug build ?
- */
- if (!nkeywords)
- return cleanreturn(1, freelist);
-
- /* We are into optional args, skip thru to any remaining
- * keyword args */
- msg = skipitem(&format, p_va, flags);
- if (msg) {
- PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
- format);
- return cleanreturn(0, freelist);
- }
- }
-
- if (!IS_END_OF_FORMAT(*format) && *format != '|') {
- PyErr_Format(PyExc_RuntimeError,
- "more argument specifiers than keyword list entries "
- "(remaining format:'%s')", format);
- return cleanreturn(0, freelist);
- }
-
- /* make sure there are no extraneous keyword arguments */
- if (nkeywords > 0) {
- PyObject *key, *value;
- Py_ssize_t pos = 0;
- while (PyDict_Next(keywords, &pos, &key, &value)) {
- int match = 0;
- char *ks;
- if (!PyUnicode_Check(key)) {
- PyErr_SetString(PyExc_TypeError,
- "keywords must be strings");
- return cleanreturn(0, freelist);
- }
- ks = _PyUnicode_AsString(key);
- for (i = 0; i < len; i++) {
- if (!strcmp(ks, kwlist[i])) {
- match = 1;
- break;
- }
- }
- if (!match) {
- PyErr_Format(PyExc_TypeError,
- "'%s' is an invalid keyword "
- "argument for this function",
- ks);
- return cleanreturn(0, freelist);
- }
- }
- }
-
- return cleanreturn(1, freelist);
+ char msgbuf[512];
+ int levels[32];
+ const char *fname, *msg, *custom_msg, *keyword;
+ int min = INT_MAX;
+ int i, len, nargs, nkeywords;
+ PyObject *freelist = NULL, *current_arg;
+
+ assert(args != NULL && PyTuple_Check(args));
+ assert(keywords == NULL || PyDict_Check(keywords));
+ assert(format != NULL);
+ assert(kwlist != NULL);
+ assert(p_va != NULL);
+
+ /* grab the function name or custom error msg first (mutually exclusive) */
+ fname = strchr(format, ':');
+ if (fname) {
+ fname++;
+ custom_msg = NULL;
+ }
+ else {
+ custom_msg = strchr(format,';');
+ if (custom_msg)
+ custom_msg++;
+ }
+
+ /* scan kwlist and get greatest possible nbr of args */
+ for (len=0; kwlist[len]; len++)
+ continue;
+
+ nargs = PyTuple_GET_SIZE(args);
+ nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
+ if (nargs + nkeywords > len) {
+ PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
+ "argument%s (%d given)",
+ (fname == NULL) ? "function" : fname,
+ (fname == NULL) ? "" : "()",
+ len,
+ (len == 1) ? "" : "s",
+ nargs + nkeywords);
+ return 0;
+ }
+
+ /* convert tuple args and keyword args in same loop, using kwlist to drive process */
+ for (i = 0; i < len; i++) {
+ keyword = kwlist[i];
+ if (*format == '|') {
+ min = i;
+ format++;
+ }
+ if (IS_END_OF_FORMAT(*format)) {
+ PyErr_Format(PyExc_RuntimeError,
+ "More keyword list entries (%d) than "
+ "format specifiers (%d)", len, i);
+ return cleanreturn(0, freelist);
+ }
+ current_arg = NULL;
+ if (nkeywords) {
+ current_arg = PyDict_GetItemString(keywords, keyword);
+ }
+ if (current_arg) {
+ --nkeywords;
+ if (i < nargs) {
+ /* arg present in tuple and in dict */
+ PyErr_Format(PyExc_TypeError,
+ "Argument given by name ('%s') "
+ "and position (%d)",
+ keyword, i+1);
+ return cleanreturn(0, freelist);
+ }
+ }
+ else if (nkeywords && PyErr_Occurred())
+ return cleanreturn(0, freelist);
+ else if (i < nargs)
+ current_arg = PyTuple_GET_ITEM(args, i);
+
+ if (current_arg) {
+ msg = convertitem(current_arg, &format, p_va, flags,
+ levels, msgbuf, sizeof(msgbuf), &freelist);
+ if (msg) {
+ seterror(i+1, msg, levels, fname, custom_msg);
+ return cleanreturn(0, freelist);
+ }
+ continue;
+ }
+
+ if (i < min) {
+ PyErr_Format(PyExc_TypeError, "Required argument "
+ "'%s' (pos %d) not found",
+ keyword, i+1);
+ return cleanreturn(0, freelist);
+ }
+ /* current code reports success when all required args
+ * fulfilled and no keyword args left, with no further
+ * validation. XXX Maybe skip this in debug build ?
+ */
+ if (!nkeywords)
+ return cleanreturn(1, freelist);
+
+ /* We are into optional args, skip thru to any remaining
+ * keyword args */
+ msg = skipitem(&format, p_va, flags);
+ if (msg) {
+ PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
+ format);
+ return cleanreturn(0, freelist);
+ }
+ }
+
+ if (!IS_END_OF_FORMAT(*format) && *format != '|') {
+ PyErr_Format(PyExc_RuntimeError,
+ "more argument specifiers than keyword list entries "
+ "(remaining format:'%s')", format);
+ return cleanreturn(0, freelist);
+ }
+
+ /* make sure there are no extraneous keyword arguments */
+ if (nkeywords > 0) {
+ PyObject *key, *value;
+ Py_ssize_t pos = 0;
+ while (PyDict_Next(keywords, &pos, &key, &value)) {
+ int match = 0;
+ char *ks;
+ if (!PyUnicode_Check(key)) {
+ PyErr_SetString(PyExc_TypeError,
+ "keywords must be strings");
+ return cleanreturn(0, freelist);
+ }
+ ks = _PyUnicode_AsString(key);
+ for (i = 0; i < len; i++) {
+ if (!strcmp(ks, kwlist[i])) {
+ match = 1;
+ break;
+ }
+ }
+ if (!match) {
+ PyErr_Format(PyExc_TypeError,
+ "'%s' is an invalid keyword "
+ "argument for this function",
+ ks);
+ return cleanreturn(0, freelist);
+ }
+ }
+ }
+
+ return cleanreturn(1, freelist);
}
static char *
skipitem(const char **p_format, va_list *p_va, int flags)
{
- const char *format = *p_format;
- char c = *format++;
-
- switch (c) {
-
- /* simple codes
- * The individual types (second arg of va_arg) are irrelevant */
-
- case 'b': /* byte -- very short int */
- case 'B': /* byte as bitfield */
- case 'h': /* short int */
- case 'H': /* short int as bitfield */
- case 'i': /* int */
- case 'I': /* int sized bitfield */
- case 'l': /* long int */
- case 'k': /* long int sized bitfield */
+ const char *format = *p_format;
+ char c = *format++;
+
+ switch (c) {
+
+ /* simple codes
+ * The individual types (second arg of va_arg) are irrelevant */
+
+ case 'b': /* byte -- very short int */
+ case 'B': /* byte as bitfield */
+ case 'h': /* short int */
+ case 'H': /* short int as bitfield */
+ case 'i': /* int */
+ case 'I': /* int sized bitfield */
+ case 'l': /* long int */
+ case 'k': /* long int sized bitfield */
#ifdef HAVE_LONG_LONG
- case 'L': /* PY_LONG_LONG */
- case 'K': /* PY_LONG_LONG sized bitfield */
+ case 'L': /* PY_LONG_LONG */
+ case 'K': /* PY_LONG_LONG sized bitfield */
#endif
- case 'f': /* float */
- case 'd': /* double */
+ case 'f': /* float */
+ case 'd': /* double */
#ifndef WITHOUT_COMPLEX
- case 'D': /* complex double */
+ case 'D': /* complex double */
#endif
- case 'c': /* char */
- case 'C': /* unicode char */
- {
- (void) va_arg(*p_va, void *);
- break;
- }
-
- case 'n': /* Py_ssize_t */
- {
- (void) va_arg(*p_va, Py_ssize_t *);
- break;
- }
-
- /* string codes */
-
- case 'e': /* string with encoding */
- {
- (void) va_arg(*p_va, const char *);
- if (!(*format == 's' || *format == 't'))
- /* after 'e', only 's' and 't' is allowed */
- goto err;
- format++;
- /* explicit fallthrough to string cases */
- }
-
- case 's': /* string */
- case 'z': /* string or None */
- case 'y': /* bytes */
- case 'u': /* unicode string */
- case 't': /* buffer, read-only */
- case 'w': /* buffer, read-write */
- {
- (void) va_arg(*p_va, char **);
- if (*format == '#') {
- if (flags & FLAG_SIZE_T)
- (void) va_arg(*p_va, Py_ssize_t *);
- else
- (void) va_arg(*p_va, int *);
- format++;
- } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
- format++;
- }
- break;
- }
-
- /* object codes */
-
- case 'S': /* string object */
- case 'Y': /* string object */
- case 'U': /* unicode string object */
- {
- (void) va_arg(*p_va, PyObject **);
- break;
- }
-
- case 'O': /* object */
- {
- if (*format == '!') {
- format++;
- (void) va_arg(*p_va, PyTypeObject*);
- (void) va_arg(*p_va, PyObject **);
- }
+ case 'c': /* char */
+ case 'C': /* unicode char */
+ {
+ (void) va_arg(*p_va, void *);
+ break;
+ }
+
+ case 'n': /* Py_ssize_t */
+ {
+ (void) va_arg(*p_va, Py_ssize_t *);
+ break;
+ }
+
+ /* string codes */
+
+ case 'e': /* string with encoding */
+ {
+ (void) va_arg(*p_va, const char *);
+ if (!(*format == 's' || *format == 't'))
+ /* after 'e', only 's' and 't' is allowed */
+ goto err;
+ format++;
+ /* explicit fallthrough to string cases */
+ }
+
+ case 's': /* string */
+ case 'z': /* string or None */
+ case 'y': /* bytes */
+ case 'u': /* unicode string */
+ case 't': /* buffer, read-only */
+ case 'w': /* buffer, read-write */
+ {
+ (void) va_arg(*p_va, char **);
+ if (*format == '#') {
+ if (flags & FLAG_SIZE_T)
+ (void) va_arg(*p_va, Py_ssize_t *);
+ else
+ (void) va_arg(*p_va, int *);
+ format++;
+ } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
+ format++;
+ }
+ break;
+ }
+
+ /* object codes */
+
+ case 'S': /* string object */
+ case 'Y': /* string object */
+ case 'U': /* unicode string object */
+ {
+ (void) va_arg(*p_va, PyObject **);
+ break;
+ }
+
+ case 'O': /* object */
+ {
+ if (*format == '!') {
+ format++;
+ (void) va_arg(*p_va, PyTypeObject*);
+ (void) va_arg(*p_va, PyObject **);
+ }
#if 0
/* I don't know what this is for */
- else if (*format == '?') {
- inquiry pred = va_arg(*p_va, inquiry);
- format++;
- if ((*pred)(arg)) {
- (void) va_arg(*p_va, PyObject **);
- }
- }
+ else if (*format == '?') {
+ inquiry pred = va_arg(*p_va, inquiry);
+ format++;
+ if ((*pred)(arg)) {
+ (void) va_arg(*p_va, PyObject **);
+ }
+ }
#endif
- else if (*format == '&') {
- typedef int (*converter)(PyObject *, void *);
- (void) va_arg(*p_va, converter);
- (void) va_arg(*p_va, void *);
- format++;
- }
- else {
- (void) va_arg(*p_va, PyObject **);
- }
- break;
- }
-
- case '(': /* bypass tuple, not handled at all previously */
- {
- char *msg;
- for (;;) {
- if (*format==')')
- break;
- if (IS_END_OF_FORMAT(*format))
- return "Unmatched left paren in format "
- "string";
- msg = skipitem(&format, p_va, flags);
- if (msg)
- return msg;
- }
- format++;
- break;
- }
-
- case ')':
- return "Unmatched right paren in format string";
-
- default:
+ else if (*format == '&') {
+ typedef int (*converter)(PyObject *, void *);
+ (void) va_arg(*p_va, converter);
+ (void) va_arg(*p_va, void *);
+ format++;
+ }
+ else {
+ (void) va_arg(*p_va, PyObject **);
+ }
+ break;
+ }
+
+ case '(': /* bypass tuple, not handled at all previously */
+ {
+ char *msg;
+ for (;;) {
+ if (*format==')')
+ break;
+ if (IS_END_OF_FORMAT(*format))
+ return "Unmatched left paren in format "
+ "string";
+ msg = skipitem(&format, p_va, flags);
+ if (msg)
+ return msg;
+ }
+ format++;
+ break;
+ }
+
+ case ')':
+ return "Unmatched right paren in format string";
+
+ default:
err:
- return "impossible<bad format char>";
+ return "impossible<bad format char>";
- }
+ }
- *p_format = format;
- return NULL;
+ *p_format = format;
+ return NULL;
}
int
PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
{
- Py_ssize_t i, l;
- PyObject **o;
- va_list vargs;
+ Py_ssize_t i, l;
+ PyObject **o;
+ va_list vargs;
#ifdef HAVE_STDARG_PROTOTYPES
- va_start(vargs, max);
+ va_start(vargs, max);
#else
- va_start(vargs);
+ va_start(vargs);
#endif
- assert(min >= 0);
- assert(min <= max);
- if (!PyTuple_Check(args)) {
- PyErr_SetString(PyExc_SystemError,
- "PyArg_UnpackTuple() argument list is not a tuple");
- return 0;
- }
- l = PyTuple_GET_SIZE(args);
- if (l < min) {
- if (name != NULL)
- PyErr_Format(
- PyExc_TypeError,
- "%s expected %s%zd arguments, got %zd",
- name, (min == max ? "" : "at least "), min, l);
- else
- PyErr_Format(
- PyExc_TypeError,
- "unpacked tuple should have %s%zd elements,"
- " but has %zd",
- (min == max ? "" : "at least "), min, l);
- va_end(vargs);
- return 0;
- }
- if (l > max) {
- if (name != NULL)
- PyErr_Format(
- PyExc_TypeError,
- "%s expected %s%zd arguments, got %zd",
- name, (min == max ? "" : "at most "), max, l);
- else
- PyErr_Format(
- PyExc_TypeError,
- "unpacked tuple should have %s%zd elements,"
- " but has %zd",
- (min == max ? "" : "at most "), max, l);
- va_end(vargs);
- return 0;
- }
- for (i = 0; i < l; i++) {
- o = va_arg(vargs, PyObject **);
- *o = PyTuple_GET_ITEM(args, i);
- }
- va_end(vargs);
- return 1;
+ assert(min >= 0);
+ assert(min <= max);
+ if (!PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_SystemError,
+ "PyArg_UnpackTuple() argument list is not a tuple");
+ return 0;
+ }
+ l = PyTuple_GET_SIZE(args);
+ if (l < min) {
+ if (name != NULL)
+ PyErr_Format(
+ PyExc_TypeError,
+ "%s expected %s%zd arguments, got %zd",
+ name, (min == max ? "" : "at least "), min, l);
+ else
+ PyErr_Format(
+ PyExc_TypeError,
+ "unpacked tuple should have %s%zd elements,"
+ " but has %zd",
+ (min == max ? "" : "at least "), min, l);
+ va_end(vargs);
+ return 0;
+ }
+ if (l > max) {
+ if (name != NULL)
+ PyErr_Format(
+ PyExc_TypeError,
+ "%s expected %s%zd arguments, got %zd",
+ name, (min == max ? "" : "at most "), max, l);
+ else
+ PyErr_Format(
+ PyExc_TypeError,
+ "unpacked tuple should have %s%zd elements,"
+ " but has %zd",
+ (min == max ? "" : "at most "), max, l);
+ va_end(vargs);
+ return 0;
+ }
+ for (i = 0; i < l; i++) {
+ o = va_arg(vargs, PyObject **);
+ *o = PyTuple_GET_ITEM(args, i);
+ }
+ va_end(vargs);
+ return 1;
}
@@ -1958,18 +1958,18 @@ PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t m
int
_PyArg_NoKeywords(const char *funcname, PyObject *kw)
{
- if (kw == NULL)
- return 1;
- if (!PyDict_CheckExact(kw)) {
- PyErr_BadInternalCall();
- return 0;
- }
- if (PyDict_Size(kw) == 0)
- return 1;
-
- PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
- funcname);
- return 0;
+ if (kw == NULL)
+ return 1;
+ if (!PyDict_CheckExact(kw)) {
+ PyErr_BadInternalCall();
+ return 0;
+ }
+ if (PyDict_Size(kw) == 0)
+ return 1;
+
+ PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
+ funcname);
+ return 0;
}
#ifdef __cplusplus
};
diff --git a/Python/getcwd.c b/Python/getcwd.c
index 967d484..4bedbd1 100644
--- a/Python/getcwd.c
+++ b/Python/getcwd.c
@@ -26,24 +26,24 @@ extern char *getwd(char *);
char *
getcwd(char *buf, int size)
{
- char localbuf[MAXPATHLEN+1];
- char *ret;
-
- if (size <= 0) {
- errno = EINVAL;
- return NULL;
- }
- ret = getwd(localbuf);
- if (ret != NULL && strlen(localbuf) >= (size_t)size) {
- errno = ERANGE;
- return NULL;
- }
- if (ret == NULL) {
- errno = EACCES; /* Most likely error */
- return NULL;
- }
- strncpy(buf, localbuf, size);
- return buf;
+ char localbuf[MAXPATHLEN+1];
+ char *ret;
+
+ if (size <= 0) {
+ errno = EINVAL;
+ return NULL;
+ }
+ ret = getwd(localbuf);
+ if (ret != NULL && strlen(localbuf) >= (size_t)size) {
+ errno = ERANGE;
+ return NULL;
+ }
+ if (ret == NULL) {
+ errno = EACCES; /* Most likely error */
+ return NULL;
+ }
+ strncpy(buf, localbuf, size);
+ return buf;
}
#else /* !HAVE_GETWD */
@@ -57,27 +57,27 @@ getcwd(char *buf, int size)
char *
getcwd(char *buf, int size)
{
- FILE *fp;
- char *p;
- int sts;
- if (size <= 0) {
- errno = EINVAL;
- return NULL;
- }
- if ((fp = popen(PWD_CMD, "r")) == NULL)
- return NULL;
- if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
- errno = EACCES; /* Most likely error */
- return NULL;
- }
- for (p = buf; *p != '\n'; p++) {
- if (*p == '\0') {
- errno = ERANGE;
- return NULL;
- }
- }
- *p = '\0';
- return buf;
+ FILE *fp;
+ char *p;
+ int sts;
+ if (size <= 0) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if ((fp = popen(PWD_CMD, "r")) == NULL)
+ return NULL;
+ if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
+ errno = EACCES; /* Most likely error */
+ return NULL;
+ }
+ for (p = buf; *p != '\n'; p++) {
+ if (*p == '\0') {
+ errno = ERANGE;
+ return NULL;
+ }
+ }
+ *p = '\0';
+ return buf;
}
#endif /* !HAVE_GETWD */
diff --git a/Python/getopt.c b/Python/getopt.c
index da9341f..5147320 100644
--- a/Python/getopt.c
+++ b/Python/getopt.c
@@ -7,8 +7,8 @@
*
* All Rights Reserved
*
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted,
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice, this permission notice and
* the following disclaimer notice appear unmodified in all copies.
*
@@ -43,84 +43,84 @@ wchar_t *_PyOS_optarg = NULL; /* optional argument */
int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
{
- static wchar_t *opt_ptr = L"";
- wchar_t *ptr;
- wchar_t option;
+ static wchar_t *opt_ptr = L"";
+ wchar_t *ptr;
+ wchar_t option;
- if (*opt_ptr == '\0') {
+ if (*opt_ptr == '\0') {
- if (_PyOS_optind >= argc)
- return -1;
+ if (_PyOS_optind >= argc)
+ return -1;
#ifdef MS_WINDOWS
- else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
- ++_PyOS_optind;
- return 'h';
- }
+ else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
+ ++_PyOS_optind;
+ return 'h';
+ }
#endif
- else if (argv[_PyOS_optind][0] != L'-' ||
- argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
- return -1;
-
- else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
- ++_PyOS_optind;
- return -1;
- }
-
- else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
- ++_PyOS_optind;
- return 'h';
- }
-
- else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
- ++_PyOS_optind;
- return 'V';
- }
-
-
- opt_ptr = &argv[_PyOS_optind++][1];
- }
-
- if ( (option = *opt_ptr++) == L'\0')
- return -1;
-
- if (option == 'J') {
- fprintf(stderr, "-J is reserved for Jython\n");
- return '_';
- }
-
- if (option == 'X') {
- fprintf(stderr,
- "-X is reserved for implementation-specific arguments\n");
- return '_';
- }
-
- if ((ptr = wcschr(optstring, option)) == NULL) {
- if (_PyOS_opterr)
- fprintf(stderr, "Unknown option: -%c\n", (char)option);
-
- return '_';
- }
-
- if (*(ptr + 1) == L':') {
- if (*opt_ptr != L'\0') {
- _PyOS_optarg = opt_ptr;
- opt_ptr = L"";
- }
-
- else {
- if (_PyOS_optind >= argc) {
- if (_PyOS_opterr)
- fprintf(stderr,
- "Argument expected for the -%c option\n", (char)option);
- return '_';
- }
-
- _PyOS_optarg = argv[_PyOS_optind++];
- }
- }
-
- return option;
+ else if (argv[_PyOS_optind][0] != L'-' ||
+ argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
+ return -1;
+
+ else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
+ ++_PyOS_optind;
+ return -1;
+ }
+
+ else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
+ ++_PyOS_optind;
+ return 'h';
+ }
+
+ else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
+ ++_PyOS_optind;
+ return 'V';
+ }
+
+
+ opt_ptr = &argv[_PyOS_optind++][1];
+ }
+
+ if ( (option = *opt_ptr++) == L'\0')
+ return -1;
+
+ if (option == 'J') {
+ fprintf(stderr, "-J is reserved for Jython\n");
+ return '_';
+ }
+
+ if (option == 'X') {
+ fprintf(stderr,
+ "-X is reserved for implementation-specific arguments\n");
+ return '_';
+ }
+
+ if ((ptr = wcschr(optstring, option)) == NULL) {
+ if (_PyOS_opterr)
+ fprintf(stderr, "Unknown option: -%c\n", (char)option);
+
+ return '_';
+ }
+
+ if (*(ptr + 1) == L':') {
+ if (*opt_ptr != L'\0') {
+ _PyOS_optarg = opt_ptr;
+ opt_ptr = L"";
+ }
+
+ else {
+ if (_PyOS_optind >= argc) {
+ if (_PyOS_opterr)
+ fprintf(stderr,
+ "Argument expected for the -%c option\n", (char)option);
+ return '_';
+ }
+
+ _PyOS_optarg = argv[_PyOS_optind++];
+ }
+ }
+
+ return option;
}
#ifdef __cplusplus
diff --git a/Python/import.c b/Python/import.c
index 159a45b..87e85a0 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -19,7 +19,7 @@
#include <fcntl.h>
#endif
#ifdef __cplusplus
-extern "C" {
+extern "C" {
#endif
#ifdef MS_WINDOWS
@@ -67,28 +67,28 @@ typedef unsigned short mode_t;
Python 2.5b3: 62101 (fix wrong code: for x, in ...)
Python 2.5b3: 62111 (fix wrong code: x += yield)
Python 2.5c1: 62121 (fix wrong lnotab with for loops and
- storing constants that should have been removed)
+ storing constants that should have been removed)
Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
Python 2.6a1: 62161 (WITH_CLEANUP optimization)
Python 3000: 3000
- 3010 (removed UNARY_CONVERT)
- 3020 (added BUILD_SET)
- 3030 (added keyword-only parameters)
- 3040 (added signature annotations)
- 3050 (print becomes a function)
- 3060 (PEP 3115 metaclass syntax)
- 3070 (PEP 3109 raise changes)
- 3080 (PEP 3137 make __file__ and __name__ unicode)
- 3090 (kill str8 interning)
- 3100 (merge from 2.6a0, see 62151)
- 3102 (__file__ points to source file)
+ 3010 (removed UNARY_CONVERT)
+ 3020 (added BUILD_SET)
+ 3030 (added keyword-only parameters)
+ 3040 (added signature annotations)
+ 3050 (print becomes a function)
+ 3060 (PEP 3115 metaclass syntax)
+ 3070 (PEP 3109 raise changes)
+ 3080 (PEP 3137 make __file__ and __name__ unicode)
+ 3090 (kill str8 interning)
+ 3100 (merge from 2.6a0, see 62151)
+ 3102 (__file__ points to source file)
Python 3.0a4: 3110 (WITH_CLEANUP optimization).
Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT)
Python 3.1a0: 3140 (optimize list, set and dict comprehensions:
- change LIST_APPEND and SET_ADD, add MAP_ADD)
+ change LIST_APPEND and SET_ADD, add MAP_ADD)
Python 3.1a0: 3150 (optimize conditional branches:
- introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
+ introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
*/
#define MAGIC (3150 | ((long)'\r'<<16) | ((long)'\n'<<24))
@@ -112,12 +112,12 @@ struct _inittab *PyImport_Inittab = _PyImport_Inittab;
struct filedescr * _PyImport_Filetab = NULL;
static const struct filedescr _PyImport_StandardFiletab[] = {
- {".py", "U", PY_SOURCE},
+ {".py", "U", PY_SOURCE},
#ifdef MS_WINDOWS
- {".pyw", "U", PY_SOURCE},
+ {".pyw", "U", PY_SOURCE},
#endif
- {".pyc", "rb", PY_COMPILED},
- {0, 0}
+ {".pyc", "rb", PY_COMPILED},
+ {0, 0}
};
@@ -126,126 +126,126 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
void
_PyImport_Init(void)
{
- const struct filedescr *scan;
- struct filedescr *filetab;
- int countD = 0;
- int countS = 0;
+ const struct filedescr *scan;
+ struct filedescr *filetab;
+ int countD = 0;
+ int countS = 0;
- /* prepare _PyImport_Filetab: copy entries from
- _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
- */
+ /* prepare _PyImport_Filetab: copy entries from
+ _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
+ */
#ifdef HAVE_DYNAMIC_LOADING
- for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
- ++countD;
+ for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
+ ++countD;
#endif
- for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
- ++countS;
- filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
- if (filetab == NULL)
- Py_FatalError("Can't initialize import file table.");
+ for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
+ ++countS;
+ filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
+ if (filetab == NULL)
+ Py_FatalError("Can't initialize import file table.");
#ifdef HAVE_DYNAMIC_LOADING
- memcpy(filetab, _PyImport_DynLoadFiletab,
- countD * sizeof(struct filedescr));
+ memcpy(filetab, _PyImport_DynLoadFiletab,
+ countD * sizeof(struct filedescr));
#endif
- memcpy(filetab + countD, _PyImport_StandardFiletab,
- countS * sizeof(struct filedescr));
- filetab[countD + countS].suffix = NULL;
+ memcpy(filetab + countD, _PyImport_StandardFiletab,
+ countS * sizeof(struct filedescr));
+ filetab[countD + countS].suffix = NULL;
- _PyImport_Filetab = filetab;
+ _PyImport_Filetab = filetab;
- if (Py_OptimizeFlag) {
- /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
- for (; filetab->suffix != NULL; filetab++) {
- if (strcmp(filetab->suffix, ".pyc") == 0)
- filetab->suffix = ".pyo";
- }
- }
+ if (Py_OptimizeFlag) {
+ /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
+ for (; filetab->suffix != NULL; filetab++) {
+ if (strcmp(filetab->suffix, ".pyc") == 0)
+ filetab->suffix = ".pyo";
+ }
+ }
- {
- /* Fix the pyc_magic so that byte compiled code created
- using the all-Unicode method doesn't interfere with
- code created in normal operation mode. */
- pyc_magic = MAGIC + 1;
- }
+ {
+ /* Fix the pyc_magic so that byte compiled code created
+ using the all-Unicode method doesn't interfere with
+ code created in normal operation mode. */
+ pyc_magic = MAGIC + 1;
+ }
}
void
_PyImportHooks_Init(void)
{
- PyObject *v, *path_hooks = NULL, *zimpimport;
- int err = 0;
-
- /* adding sys.path_hooks and sys.path_importer_cache, setting up
- zipimport */
- if (PyType_Ready(&PyNullImporter_Type) < 0)
- goto error;
-
- if (Py_VerboseFlag)
- PySys_WriteStderr("# installing zipimport hook\n");
-
- v = PyList_New(0);
- if (v == NULL)
- goto error;
- err = PySys_SetObject("meta_path", v);
- Py_DECREF(v);
- if (err)
- goto error;
- v = PyDict_New();
- if (v == NULL)
- goto error;
- err = PySys_SetObject("path_importer_cache", v);
- Py_DECREF(v);
- if (err)
- goto error;
- path_hooks = PyList_New(0);
- if (path_hooks == NULL)
- goto error;
- err = PySys_SetObject("path_hooks", path_hooks);
- if (err) {
+ PyObject *v, *path_hooks = NULL, *zimpimport;
+ int err = 0;
+
+ /* adding sys.path_hooks and sys.path_importer_cache, setting up
+ zipimport */
+ if (PyType_Ready(&PyNullImporter_Type) < 0)
+ goto error;
+
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# installing zipimport hook\n");
+
+ v = PyList_New(0);
+ if (v == NULL)
+ goto error;
+ err = PySys_SetObject("meta_path", v);
+ Py_DECREF(v);
+ if (err)
+ goto error;
+ v = PyDict_New();
+ if (v == NULL)
+ goto error;
+ err = PySys_SetObject("path_importer_cache", v);
+ Py_DECREF(v);
+ if (err)
+ goto error;
+ path_hooks = PyList_New(0);
+ if (path_hooks == NULL)
+ goto error;
+ err = PySys_SetObject("path_hooks", path_hooks);
+ if (err) {
error:
- PyErr_Print();
- Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
- "path_importer_cache, or NullImporter failed"
- );
- }
-
- zimpimport = PyImport_ImportModule("zipimport");
- if (zimpimport == NULL) {
- PyErr_Clear(); /* No zip import module -- okay */
- if (Py_VerboseFlag)
- PySys_WriteStderr("# can't import zipimport\n");
- }
- else {
- PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
- "zipimporter");
- Py_DECREF(zimpimport);
- if (zipimporter == NULL) {
- PyErr_Clear(); /* No zipimporter object -- okay */
- if (Py_VerboseFlag)
- PySys_WriteStderr(
- "# can't import zipimport.zipimporter\n");
- }
- else {
- /* sys.path_hooks.append(zipimporter) */
- err = PyList_Append(path_hooks, zipimporter);
- Py_DECREF(zipimporter);
- if (err)
- goto error;
- if (Py_VerboseFlag)
- PySys_WriteStderr(
- "# installed zipimport hook\n");
- }
- }
- Py_DECREF(path_hooks);
+ PyErr_Print();
+ Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
+ "path_importer_cache, or NullImporter failed"
+ );
+ }
+
+ zimpimport = PyImport_ImportModule("zipimport");
+ if (zimpimport == NULL) {
+ PyErr_Clear(); /* No zip import module -- okay */
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# can't import zipimport\n");
+ }
+ else {
+ PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
+ "zipimporter");
+ Py_DECREF(zimpimport);
+ if (zipimporter == NULL) {
+ PyErr_Clear(); /* No zipimporter object -- okay */
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# can't import zipimport.zipimporter\n");
+ }
+ else {
+ /* sys.path_hooks.append(zipimporter) */
+ err = PyList_Append(path_hooks, zipimporter);
+ Py_DECREF(zipimporter);
+ if (err)
+ goto error;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# installed zipimport hook\n");
+ }
+ }
+ Py_DECREF(path_hooks);
}
void
_PyImport_Fini(void)
{
- Py_XDECREF(extensions);
- extensions = NULL;
- PyMem_DEL(_PyImport_Filetab);
- _PyImport_Filetab = NULL;
+ Py_XDECREF(extensions);
+ extensions = NULL;
+ PyMem_DEL(_PyImport_Filetab);
+ _PyImport_Filetab = NULL;
}
@@ -264,42 +264,42 @@ static int import_lock_level = 0;
void
_PyImport_AcquireLock(void)
{
- long me = PyThread_get_thread_ident();
- if (me == -1)
- return; /* Too bad */
- if (import_lock == NULL) {
- import_lock = PyThread_allocate_lock();
- if (import_lock == NULL)
- return; /* Nothing much we can do. */
- }
- if (import_lock_thread == me) {
- import_lock_level++;
- return;
- }
- if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
- {
- PyThreadState *tstate = PyEval_SaveThread();
- PyThread_acquire_lock(import_lock, 1);
- PyEval_RestoreThread(tstate);
- }
- import_lock_thread = me;
- import_lock_level = 1;
+ long me = PyThread_get_thread_ident();
+ if (me == -1)
+ return; /* Too bad */
+ if (import_lock == NULL) {
+ import_lock = PyThread_allocate_lock();
+ if (import_lock == NULL)
+ return; /* Nothing much we can do. */
+ }
+ if (import_lock_thread == me) {
+ import_lock_level++;
+ return;
+ }
+ if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
+ {
+ PyThreadState *tstate = PyEval_SaveThread();
+ PyThread_acquire_lock(import_lock, 1);
+ PyEval_RestoreThread(tstate);
+ }
+ import_lock_thread = me;
+ import_lock_level = 1;
}
int
_PyImport_ReleaseLock(void)
{
- long me = PyThread_get_thread_ident();
- if (me == -1 || import_lock == NULL)
- return 0; /* Too bad */
- if (import_lock_thread != me)
- return -1;
- import_lock_level--;
- if (import_lock_level == 0) {
- import_lock_thread = -1;
- PyThread_release_lock(import_lock);
- }
- return 1;
+ long me = PyThread_get_thread_ident();
+ if (me == -1 || import_lock == NULL)
+ return 0; /* Too bad */
+ if (import_lock_thread != me)
+ return -1;
+ import_lock_level--;
+ if (import_lock_level == 0) {
+ import_lock_thread = -1;
+ PyThread_release_lock(import_lock);
+ }
+ return 1;
}
/* This function used to be called from PyOS_AfterFork to ensure that newly
@@ -318,9 +318,9 @@ static PyObject *
imp_lock_held(PyObject *self, PyObject *noargs)
{
#ifdef WITH_THREAD
- return PyBool_FromLong(import_lock_thread != -1);
+ return PyBool_FromLong(import_lock_thread != -1);
#else
- return PyBool_FromLong(0);
+ return PyBool_FromLong(0);
#endif
}
@@ -328,32 +328,32 @@ static PyObject *
imp_acquire_lock(PyObject *self, PyObject *noargs)
{
#ifdef WITH_THREAD
- _PyImport_AcquireLock();
+ _PyImport_AcquireLock();
#endif
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static PyObject *
imp_release_lock(PyObject *self, PyObject *noargs)
{
#ifdef WITH_THREAD
- if (_PyImport_ReleaseLock() < 0) {
- PyErr_SetString(PyExc_RuntimeError,
- "not holding the import lock");
- return NULL;
- }
+ if (_PyImport_ReleaseLock() < 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "not holding the import lock");
+ return NULL;
+ }
#endif
- Py_INCREF(Py_None);
- return Py_None;
+ Py_INCREF(Py_None);
+ return Py_None;
}
static void
imp_modules_reloading_clear(void)
{
- PyInterpreterState *interp = PyThreadState_Get()->interp;
- if (interp->modules_reloading != NULL)
- PyDict_Clear(interp->modules_reloading);
+ PyInterpreterState *interp = PyThreadState_Get()->interp;
+ if (interp->modules_reloading != NULL)
+ PyDict_Clear(interp->modules_reloading);
}
/* Helper for sys */
@@ -361,28 +361,28 @@ imp_modules_reloading_clear(void)
PyObject *
PyImport_GetModuleDict(void)
{
- PyInterpreterState *interp = PyThreadState_GET()->interp;
- if (interp->modules == NULL)
- Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
- return interp->modules;
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ if (interp->modules == NULL)
+ Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
+ return interp->modules;
}
/* List of names to clear in sys */
static char* sys_deletes[] = {
- "path", "argv", "ps1", "ps2",
- "last_type", "last_value", "last_traceback",
- "path_hooks", "path_importer_cache", "meta_path",
- /* misc stuff */
- "flags", "float_info",
- NULL
+ "path", "argv", "ps1", "ps2",
+ "last_type", "last_value", "last_traceback",
+ "path_hooks", "path_importer_cache", "meta_path",
+ /* misc stuff */
+ "flags", "float_info",
+ NULL
};
static char* sys_files[] = {
- "stdin", "__stdin__",
- "stdout", "__stdout__",
- "stderr", "__stderr__",
- NULL
+ "stdin", "__stdin__",
+ "stdout", "__stdout__",
+ "stderr", "__stderr__",
+ NULL
};
@@ -391,132 +391,132 @@ static char* sys_files[] = {
void
PyImport_Cleanup(void)
{
- Py_ssize_t pos, ndone;
- char *name;
- PyObject *key, *value, *dict;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
- PyObject *modules = interp->modules;
-
- if (modules == NULL)
- return; /* Already done */
-
- /* Delete some special variables first. These are common
- places where user values hide and people complain when their
- destructors fail. Since the modules containing them are
- deleted *last* of all, they would come too late in the normal
- destruction order. Sigh. */
-
- value = PyDict_GetItemString(modules, "builtins");
- if (value != NULL && PyModule_Check(value)) {
- dict = PyModule_GetDict(value);
- if (Py_VerboseFlag)
- PySys_WriteStderr("# clear builtins._\n");
- PyDict_SetItemString(dict, "_", Py_None);
- }
- value = PyDict_GetItemString(modules, "sys");
- if (value != NULL && PyModule_Check(value)) {
- char **p;
- PyObject *v;
- dict = PyModule_GetDict(value);
- for (p = sys_deletes; *p != NULL; p++) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# clear sys.%s\n", *p);
- PyDict_SetItemString(dict, *p, Py_None);
- }
- for (p = sys_files; *p != NULL; p+=2) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# restore sys.%s\n", *p);
- v = PyDict_GetItemString(dict, *(p+1));
- if (v == NULL)
- v = Py_None;
- PyDict_SetItemString(dict, *p, v);
- }
- }
-
- /* First, delete __main__ */
- value = PyDict_GetItemString(modules, "__main__");
- if (value != NULL && PyModule_Check(value)) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# cleanup __main__\n");
- _PyModule_Clear(value);
- PyDict_SetItemString(modules, "__main__", Py_None);
- }
-
- /* The special treatment of "builtins" here is because even
- when it's not referenced as a module, its dictionary is
- referenced by almost every module's __builtins__. Since
- deleting a module clears its dictionary (even if there are
- references left to it), we need to delete the "builtins"
- module last. Likewise, we don't delete sys until the very
- end because it is implicitly referenced (e.g. by print).
-
- Also note that we 'delete' modules by replacing their entry
- in the modules dict with None, rather than really deleting
- them; this avoids a rehash of the modules dictionary and
- also marks them as "non existent" so they won't be
- re-imported. */
-
- /* Next, repeatedly delete modules with a reference count of
- one (skipping builtins and sys) and delete them */
- do {
- ndone = 0;
- pos = 0;
- while (PyDict_Next(modules, &pos, &key, &value)) {
- if (value->ob_refcnt != 1)
- continue;
- if (PyUnicode_Check(key) && PyModule_Check(value)) {
- name = _PyUnicode_AsString(key);
- if (strcmp(name, "builtins") == 0)
- continue;
- if (strcmp(name, "sys") == 0)
- continue;
- if (Py_VerboseFlag)
- PySys_WriteStderr(
- "# cleanup[1] %s\n", name);
- _PyModule_Clear(value);
- PyDict_SetItem(modules, key, Py_None);
- ndone++;
- }
- }
- } while (ndone > 0);
-
- /* Next, delete all modules (still skipping builtins and sys) */
- pos = 0;
- while (PyDict_Next(modules, &pos, &key, &value)) {
- if (PyUnicode_Check(key) && PyModule_Check(value)) {
- name = _PyUnicode_AsString(key);
- if (strcmp(name, "builtins") == 0)
- continue;
- if (strcmp(name, "sys") == 0)
- continue;
- if (Py_VerboseFlag)
- PySys_WriteStderr("# cleanup[2] %s\n", name);
- _PyModule_Clear(value);
- PyDict_SetItem(modules, key, Py_None);
- }
- }
-
- /* Next, delete sys and builtins (in that order) */
- value = PyDict_GetItemString(modules, "sys");
- if (value != NULL && PyModule_Check(value)) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# cleanup sys\n");
- _PyModule_Clear(value);
- PyDict_SetItemString(modules, "sys", Py_None);
- }
- value = PyDict_GetItemString(modules, "builtins");
- if (value != NULL && PyModule_Check(value)) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# cleanup builtins\n");
- _PyModule_Clear(value);
- PyDict_SetItemString(modules, "builtins", Py_None);
- }
-
- /* Finally, clear and delete the modules directory */
- PyDict_Clear(modules);
- interp->modules = NULL;
- Py_DECREF(modules);
- Py_CLEAR(interp->modules_reloading);
+ Py_ssize_t pos, ndone;
+ char *name;
+ PyObject *key, *value, *dict;
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyObject *modules = interp->modules;
+
+ if (modules == NULL)
+ return; /* Already done */
+
+ /* Delete some special variables first. These are common
+ places where user values hide and people complain when their
+ destructors fail. Since the modules containing them are
+ deleted *last* of all, they would come too late in the normal
+ destruction order. Sigh. */
+
+ value = PyDict_GetItemString(modules, "builtins");
+ if (value != NULL && PyModule_Check(value)) {
+ dict = PyModule_GetDict(value);
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# clear builtins._\n");
+ PyDict_SetItemString(dict, "_", Py_None);
+ }
+ value = PyDict_GetItemString(modules, "sys");
+ if (value != NULL && PyModule_Check(value)) {
+ char **p;
+ PyObject *v;
+ dict = PyModule_GetDict(value);
+ for (p = sys_deletes; *p != NULL; p++) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# clear sys.%s\n", *p);
+ PyDict_SetItemString(dict, *p, Py_None);
+ }
+ for (p = sys_files; *p != NULL; p+=2) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# restore sys.%s\n", *p);
+ v = PyDict_GetItemString(dict, *(p+1));
+ if (v == NULL)
+ v = Py_None;
+ PyDict_SetItemString(dict, *p, v);
+ }
+ }
+
+ /* First, delete __main__ */
+ value = PyDict_GetItemString(modules, "__main__");
+ if (value != NULL && PyModule_Check(value)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# cleanup __main__\n");
+ _PyModule_Clear(value);
+ PyDict_SetItemString(modules, "__main__", Py_None);
+ }
+
+ /* The special treatment of "builtins" here is because even
+ when it's not referenced as a module, its dictionary is
+ referenced by almost every module's __builtins__. Since
+ deleting a module clears its dictionary (even if there are
+ references left to it), we need to delete the "builtins"
+ module last. Likewise, we don't delete sys until the very
+ end because it is implicitly referenced (e.g. by print).
+
+ Also note that we 'delete' modules by replacing their entry
+ in the modules dict with None, rather than really deleting
+ them; this avoids a rehash of the modules dictionary and
+ also marks them as "non existent" so they won't be
+ re-imported. */
+
+ /* Next, repeatedly delete modules with a reference count of
+ one (skipping builtins and sys) and delete them */
+ do {
+ ndone = 0;
+ pos = 0;
+ while (PyDict_Next(modules, &pos, &key, &value)) {
+ if (value->ob_refcnt != 1)
+ continue;
+ if (PyUnicode_Check(key) && PyModule_Check(value)) {
+ name = _PyUnicode_AsString(key);
+ if (strcmp(name, "builtins") == 0)
+ continue;
+ if (strcmp(name, "sys") == 0)
+ continue;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# cleanup[1] %s\n", name);
+ _PyModule_Clear(value);
+ PyDict_SetItem(modules, key, Py_None);
+ ndone++;
+ }
+ }
+ } while (ndone > 0);
+
+ /* Next, delete all modules (still skipping builtins and sys) */
+ pos = 0;
+ while (PyDict_Next(modules, &pos, &key, &value)) {
+ if (PyUnicode_Check(key) && PyModule_Check(value)) {
+ name = _PyUnicode_AsString(key);
+ if (strcmp(name, "builtins") == 0)
+ continue;
+ if (strcmp(name, "sys") == 0)
+ continue;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# cleanup[2] %s\n", name);
+ _PyModule_Clear(value);
+ PyDict_SetItem(modules, key, Py_None);
+ }
+ }
+
+ /* Next, delete sys and builtins (in that order) */
+ value = PyDict_GetItemString(modules, "sys");
+ if (value != NULL && PyModule_Check(value)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# cleanup sys\n");
+ _PyModule_Clear(value);
+ PyDict_SetItemString(modules, "sys", Py_None);
+ }
+ value = PyDict_GetItemString(modules, "builtins");
+ if (value != NULL && PyModule_Check(value)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# cleanup builtins\n");
+ _PyModule_Clear(value);
+ PyDict_SetItemString(modules, "builtins", Py_None);
+ }
+
+ /* Finally, clear and delete the modules directory */
+ PyDict_Clear(modules);
+ interp->modules = NULL;
+ Py_DECREF(modules);
+ Py_CLEAR(interp->modules_reloading);
}
@@ -525,7 +525,7 @@ PyImport_Cleanup(void)
long
PyImport_GetMagicNumber(void)
{
- return pyc_magic;
+ return pyc_magic;
}
@@ -537,7 +537,7 @@ PyImport_GetMagicNumber(void)
dictionary is stored by calling _PyImport_FixupExtension()
immediately after the module initialization function succeeds. A
copy can be retrieved from there by calling
- _PyImport_FindExtension().
+ _PyImport_FindExtension().
Modules which do support multiple multiple initialization set
their m_size field to a non-negative number (indicating the size
@@ -548,90 +548,90 @@ PyImport_GetMagicNumber(void)
int
_PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
{
- PyObject *modules, *dict;
- struct PyModuleDef *def;
- if (extensions == NULL) {
- extensions = PyDict_New();
- if (extensions == NULL)
- return -1;
- }
- if (mod == NULL || !PyModule_Check(mod)) {
- PyErr_BadInternalCall();
- return -1;
- }
- def = PyModule_GetDef(mod);
- if (!def) {
- PyErr_BadInternalCall();
- return -1;
- }
- modules = PyImport_GetModuleDict();
- if (PyDict_SetItemString(modules, name, mod) < 0)
- return -1;
- if (_PyState_AddModule(mod, def) < 0) {
- PyDict_DelItemString(modules, name);
- return -1;
- }
- if (def->m_size == -1) {
- if (def->m_base.m_copy) {
- /* Somebody already imported the module,
- likely under a different name.
- XXX this should really not happen. */
- Py_DECREF(def->m_base.m_copy);
- def->m_base.m_copy = NULL;
- }
- dict = PyModule_GetDict(mod);
- if (dict == NULL)
- return -1;
- def->m_base.m_copy = PyDict_Copy(dict);
- if (def->m_base.m_copy == NULL)
- return -1;
- }
- PyDict_SetItemString(extensions, filename, (PyObject*)def);
- return 0;
+ PyObject *modules, *dict;
+ struct PyModuleDef *def;
+ if (extensions == NULL) {
+ extensions = PyDict_New();
+ if (extensions == NULL)
+ return -1;
+ }
+ if (mod == NULL || !PyModule_Check(mod)) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ def = PyModule_GetDef(mod);
+ if (!def) {
+ PyErr_BadInternalCall();
+ return -1;
+ }
+ modules = PyImport_GetModuleDict();
+ if (PyDict_SetItemString(modules, name, mod) < 0)
+ return -1;
+ if (_PyState_AddModule(mod, def) < 0) {
+ PyDict_DelItemString(modules, name);
+ return -1;
+ }
+ if (def->m_size == -1) {
+ if (def->m_base.m_copy) {
+ /* Somebody already imported the module,
+ likely under a different name.
+ XXX this should really not happen. */
+ Py_DECREF(def->m_base.m_copy);
+ def->m_base.m_copy = NULL;
+ }
+ dict = PyModule_GetDict(mod);
+ if (dict == NULL)
+ return -1;
+ def->m_base.m_copy = PyDict_Copy(dict);
+ if (def->m_base.m_copy == NULL)
+ return -1;
+ }
+ PyDict_SetItemString(extensions, filename, (PyObject*)def);
+ return 0;
}
PyObject *
_PyImport_FindExtension(char *name, char *filename)
{
- PyObject *mod, *mdict;
- PyModuleDef* def;
- if (extensions == NULL)
- return NULL;
- def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
- if (def == NULL)
- return NULL;
- if (def->m_size == -1) {
- /* Module does not support repeated initialization */
- if (def->m_base.m_copy == NULL)
- return NULL;
- mod = PyImport_AddModule(name);
- if (mod == NULL)
- return NULL;
- mdict = PyModule_GetDict(mod);
- if (mdict == NULL)
- return NULL;
- if (PyDict_Update(mdict, def->m_base.m_copy))
- return NULL;
- }
- else {
- if (def->m_base.m_init == NULL)
- return NULL;
- mod = def->m_base.m_init();
- if (mod == NULL)
- return NULL;
- PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
- Py_DECREF(mod);
- }
- if (_PyState_AddModule(mod, def) < 0) {
- PyDict_DelItemString(PyImport_GetModuleDict(), name);
- Py_DECREF(mod);
- return NULL;
- }
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # previously loaded (%s)\n",
- name, filename);
- return mod;
-
+ PyObject *mod, *mdict;
+ PyModuleDef* def;
+ if (extensions == NULL)
+ return NULL;
+ def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
+ if (def == NULL)
+ return NULL;
+ if (def->m_size == -1) {
+ /* Module does not support repeated initialization */
+ if (def->m_base.m_copy == NULL)
+ return NULL;
+ mod = PyImport_AddModule(name);
+ if (mod == NULL)
+ return NULL;
+ mdict = PyModule_GetDict(mod);
+ if (mdict == NULL)
+ return NULL;
+ if (PyDict_Update(mdict, def->m_base.m_copy))
+ return NULL;
+ }
+ else {
+ if (def->m_base.m_init == NULL)
+ return NULL;
+ mod = def->m_base.m_init();
+ if (mod == NULL)
+ return NULL;
+ PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
+ Py_DECREF(mod);
+ }
+ if (_PyState_AddModule(mod, def) < 0) {
+ PyDict_DelItemString(PyImport_GetModuleDict(), name);
+ Py_DECREF(mod);
+ return NULL;
+ }
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # previously loaded (%s)\n",
+ name, filename);
+ return mod;
+
}
@@ -644,34 +644,34 @@ _PyImport_FindExtension(char *name, char *filename)
PyObject *
PyImport_AddModule(const char *name)
{
- PyObject *modules = PyImport_GetModuleDict();
- PyObject *m;
+ PyObject *modules = PyImport_GetModuleDict();
+ PyObject *m;
- if ((m = PyDict_GetItemString(modules, name)) != NULL &&
- PyModule_Check(m))
- return m;
- m = PyModule_New(name);
- if (m == NULL)
- return NULL;
- if (PyDict_SetItemString(modules, name, m) != 0) {
- Py_DECREF(m);
- return NULL;
- }
- Py_DECREF(m); /* Yes, it still exists, in modules! */
+ if ((m = PyDict_GetItemString(modules, name)) != NULL &&
+ PyModule_Check(m))
+ return m;
+ m = PyModule_New(name);
+ if (m == NULL)
+ return NULL;
+ if (PyDict_SetItemString(modules, name, m) != 0) {
+ Py_DECREF(m);
+ return NULL;
+ }
+ Py_DECREF(m); /* Yes, it still exists, in modules! */
- return m;
+ return m;
}
/* Remove name from sys.modules, if it's there. */
static void
_RemoveModule(const char *name)
{
- PyObject *modules = PyImport_GetModuleDict();
- if (PyDict_GetItemString(modules, name) == NULL)
- return;
- if (PyDict_DelItemString(modules, name) < 0)
- Py_FatalError("import: deleting existing key in"
- "sys.modules failed");
+ PyObject *modules = PyImport_GetModuleDict();
+ if (PyDict_GetItemString(modules, name) == NULL)
+ return;
+ if (PyDict_DelItemString(modules, name) < 0)
+ Py_FatalError("import: deleting existing key in"
+ "sys.modules failed");
}
static PyObject * get_sourcefile(const char *file);
@@ -686,60 +686,60 @@ static PyObject * get_sourcefile(const char *file);
PyObject *
PyImport_ExecCodeModule(char *name, PyObject *co)
{
- return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
+ return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
}
PyObject *
PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
{
- PyObject *modules = PyImport_GetModuleDict();
- PyObject *m, *d, *v;
-
- m = PyImport_AddModule(name);
- if (m == NULL)
- return NULL;
- /* If the module is being reloaded, we get the old module back
- and re-use its dict to exec the new code. */
- d = PyModule_GetDict(m);
- if (PyDict_GetItemString(d, "__builtins__") == NULL) {
- if (PyDict_SetItemString(d, "__builtins__",
- PyEval_GetBuiltins()) != 0)
- goto error;
- }
- /* Remember the filename as the __file__ attribute */
- v = NULL;
- if (pathname != NULL) {
- v = get_sourcefile(pathname);
- if (v == NULL)
- PyErr_Clear();
- }
- if (v == NULL) {
- v = ((PyCodeObject *)co)->co_filename;
- Py_INCREF(v);
- }
- if (PyDict_SetItemString(d, "__file__", v) != 0)
- PyErr_Clear(); /* Not important enough to report */
- Py_DECREF(v);
-
- v = PyEval_EvalCode((PyCodeObject *)co, d, d);
- if (v == NULL)
- goto error;
- Py_DECREF(v);
-
- if ((m = PyDict_GetItemString(modules, name)) == NULL) {
- PyErr_Format(PyExc_ImportError,
- "Loaded module %.200s not found in sys.modules",
- name);
- return NULL;
- }
-
- Py_INCREF(m);
-
- return m;
+ PyObject *modules = PyImport_GetModuleDict();
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule(name);
+ if (m == NULL)
+ return NULL;
+ /* If the module is being reloaded, we get the old module back
+ and re-use its dict to exec the new code. */
+ d = PyModule_GetDict(m);
+ if (PyDict_GetItemString(d, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(d, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
+ goto error;
+ }
+ /* Remember the filename as the __file__ attribute */
+ v = NULL;
+ if (pathname != NULL) {
+ v = get_sourcefile(pathname);
+ if (v == NULL)
+ PyErr_Clear();
+ }
+ if (v == NULL) {
+ v = ((PyCodeObject *)co)->co_filename;
+ Py_INCREF(v);
+ }
+ if (PyDict_SetItemString(d, "__file__", v) != 0)
+ PyErr_Clear(); /* Not important enough to report */
+ Py_DECREF(v);
+
+ v = PyEval_EvalCode((PyCodeObject *)co, d, d);
+ if (v == NULL)
+ goto error;
+ Py_DECREF(v);
+
+ if ((m = PyDict_GetItemString(modules, name)) == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "Loaded module %.200s not found in sys.modules",
+ name);
+ return NULL;
+ }
+
+ Py_INCREF(m);
+
+ return m;
error:
- _RemoveModule(name);
- return NULL;
+ _RemoveModule(name);
+ return NULL;
}
@@ -751,21 +751,21 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
static char *
make_compiled_pathname(char *pathname, char *buf, size_t buflen)
{
- size_t len = strlen(pathname);
- if (len+2 > buflen)
- return NULL;
+ size_t len = strlen(pathname);
+ if (len+2 > buflen)
+ return NULL;
#ifdef MS_WINDOWS
- /* Treat .pyw as if it were .py. The case of ".pyw" must match
- that used in _PyImport_StandardFiletab. */
- if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
- --len; /* pretend 'w' isn't there */
+ /* Treat .pyw as if it were .py. The case of ".pyw" must match
+ that used in _PyImport_StandardFiletab. */
+ if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
+ --len; /* pretend 'w' isn't there */
#endif
- memcpy(buf, pathname, len);
- buf[len] = Py_OptimizeFlag ? 'o' : 'c';
- buf[len+1] = '\0';
+ memcpy(buf, pathname, len);
+ buf[len] = Py_OptimizeFlag ? 'o' : 'c';
+ buf[len+1] = '\0';
- return buf;
+ return buf;
}
@@ -779,30 +779,30 @@ make_compiled_pathname(char *pathname, char *buf, size_t buflen)
static FILE *
check_compiled_module(char *pathname, time_t mtime, char *cpathname)
{
- FILE *fp;
- long magic;
- long pyc_mtime;
-
- fp = fopen(cpathname, "rb");
- if (fp == NULL)
- return NULL;
- magic = PyMarshal_ReadLongFromFile(fp);
- if (magic != pyc_magic) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# %s has bad magic\n", cpathname);
- fclose(fp);
- return NULL;
- }
- pyc_mtime = PyMarshal_ReadLongFromFile(fp);
- if (pyc_mtime != mtime) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# %s has bad mtime\n", cpathname);
- fclose(fp);
- return NULL;
- }
- if (Py_VerboseFlag)
- PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
- return fp;
+ FILE *fp;
+ long magic;
+ long pyc_mtime;
+
+ fp = fopen(cpathname, "rb");
+ if (fp == NULL)
+ return NULL;
+ magic = PyMarshal_ReadLongFromFile(fp);
+ if (magic != pyc_magic) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# %s has bad magic\n", cpathname);
+ fclose(fp);
+ return NULL;
+ }
+ pyc_mtime = PyMarshal_ReadLongFromFile(fp);
+ if (pyc_mtime != mtime) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# %s has bad mtime\n", cpathname);
+ fclose(fp);
+ return NULL;
+ }
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
+ return fp;
}
@@ -811,18 +811,18 @@ check_compiled_module(char *pathname, time_t mtime, char *cpathname)
static PyCodeObject *
read_compiled_module(char *cpathname, FILE *fp)
{
- PyObject *co;
+ PyObject *co;
- co = PyMarshal_ReadLastObjectFromFile(fp);
- if (co == NULL)
- return NULL;
- if (!PyCode_Check(co)) {
- PyErr_Format(PyExc_ImportError,
- "Non-code object in %.200s", cpathname);
- Py_DECREF(co);
- return NULL;
- }
- return (PyCodeObject *)co;
+ co = PyMarshal_ReadLastObjectFromFile(fp);
+ if (co == NULL)
+ return NULL;
+ if (!PyCode_Check(co)) {
+ PyErr_Format(PyExc_ImportError,
+ "Non-code object in %.200s", cpathname);
+ Py_DECREF(co);
+ return NULL;
+ }
+ return (PyCodeObject *)co;
}
@@ -832,27 +832,27 @@ read_compiled_module(char *cpathname, FILE *fp)
static PyObject *
load_compiled_module(char *name, char *cpathname, FILE *fp)
{
- long magic;
- PyCodeObject *co;
- PyObject *m;
-
- magic = PyMarshal_ReadLongFromFile(fp);
- if (magic != pyc_magic) {
- PyErr_Format(PyExc_ImportError,
- "Bad magic number in %.200s", cpathname);
- return NULL;
- }
- (void) PyMarshal_ReadLongFromFile(fp);
- co = read_compiled_module(cpathname, fp);
- if (co == NULL)
- return NULL;
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # precompiled from %s\n",
- name, cpathname);
- m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
- Py_DECREF(co);
-
- return m;
+ long magic;
+ PyCodeObject *co;
+ PyObject *m;
+
+ magic = PyMarshal_ReadLongFromFile(fp);
+ if (magic != pyc_magic) {
+ PyErr_Format(PyExc_ImportError,
+ "Bad magic number in %.200s", cpathname);
+ return NULL;
+ }
+ (void) PyMarshal_ReadLongFromFile(fp);
+ co = read_compiled_module(cpathname, fp);
+ if (co == NULL)
+ return NULL;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # precompiled from %s\n",
+ name, cpathname);
+ m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
+ Py_DECREF(co);
+
+ return m;
}
/* Parse a source file and return the corresponding code object */
@@ -860,22 +860,22 @@ load_compiled_module(char *name, char *cpathname, FILE *fp)
static PyCodeObject *
parse_source_module(const char *pathname, FILE *fp)
{
- PyCodeObject *co = NULL;
- mod_ty mod;
- PyCompilerFlags flags;
- PyArena *arena = PyArena_New();
- if (arena == NULL)
- return NULL;
+ PyCodeObject *co = NULL;
+ mod_ty mod;
+ PyCompilerFlags flags;
+ PyArena *arena = PyArena_New();
+ if (arena == NULL)
+ return NULL;
- flags.cf_flags = 0;
- mod = PyParser_ASTFromFile(fp, pathname, NULL,
- Py_file_input, 0, 0, &flags,
- NULL, arena);
- if (mod) {
- co = PyAST_Compile(mod, pathname, NULL, arena);
- }
- PyArena_Free(arena);
- return co;
+ flags.cf_flags = 0;
+ mod = PyParser_ASTFromFile(fp, pathname, NULL,
+ Py_file_input, 0, 0, &flags,
+ NULL, arena);
+ if (mod) {
+ co = PyAST_Compile(mod, pathname, NULL, arena);
+ }
+ PyArena_Free(arena);
+ return co;
}
@@ -885,30 +885,30 @@ static FILE *
open_exclusive(char *filename, mode_t mode)
{
#if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
- /* Use O_EXCL to avoid a race condition when another process tries to
- write the same file. When that happens, our open() call fails,
- which is just fine (since it's only a cache).
- XXX If the file exists and is writable but the directory is not
- writable, the file will never be written. Oh well.
- */
- int fd;
- (void) unlink(filename);
- fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
+ /* Use O_EXCL to avoid a race condition when another process tries to
+ write the same file. When that happens, our open() call fails,
+ which is just fine (since it's only a cache).
+ XXX If the file exists and is writable but the directory is not
+ writable, the file will never be written. Oh well.
+ */
+ int fd;
+ (void) unlink(filename);
+ fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
#ifdef O_BINARY
- |O_BINARY /* necessary for Windows */
+ |O_BINARY /* necessary for Windows */
#endif
#ifdef __VMS
- , mode, "ctxt=bin", "shr=nil"
+ , mode, "ctxt=bin", "shr=nil"
#else
- , mode
+ , mode
#endif
- );
- if (fd < 0)
- return NULL;
- return fdopen(fd, "wb");
+ );
+ if (fd < 0)
+ return NULL;
+ return fdopen(fd, "wb");
#else
- /* Best we can do -- on Windows this can't happen anyway */
- return fopen(filename, "wb");
+ /* Best we can do -- on Windows this can't happen anyway */
+ return fopen(filename, "wb");
#endif
}
@@ -921,87 +921,87 @@ open_exclusive(char *filename, mode_t mode)
static void
write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
{
- FILE *fp;
- time_t mtime = srcstat->st_mtime;
+ FILE *fp;
+ time_t mtime = srcstat->st_mtime;
#ifdef MS_WINDOWS /* since Windows uses different permissions */
- mode_t mode = srcstat->st_mode & ~S_IEXEC;
+ mode_t mode = srcstat->st_mode & ~S_IEXEC;
#else
- mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
-#endif
-
- fp = open_exclusive(cpathname, mode);
- if (fp == NULL) {
- if (Py_VerboseFlag)
- PySys_WriteStderr(
- "# can't create %s\n", cpathname);
- return;
- }
- PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
- /* First write a 0 for mtime */
- PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
- PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
- if (fflush(fp) != 0 || ferror(fp)) {
- if (Py_VerboseFlag)
- PySys_WriteStderr("# can't write %s\n", cpathname);
- /* Don't keep partial file */
- fclose(fp);
- (void) unlink(cpathname);
- return;
- }
- /* Now write the true mtime */
- fseek(fp, 4L, 0);
- assert(mtime < LONG_MAX);
- PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
- fflush(fp);
- fclose(fp);
- if (Py_VerboseFlag)
- PySys_WriteStderr("# wrote %s\n", cpathname);
+ mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
+#endif
+
+ fp = open_exclusive(cpathname, mode);
+ if (fp == NULL) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "# can't create %s\n", cpathname);
+ return;
+ }
+ PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
+ /* First write a 0 for mtime */
+ PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
+ PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
+ if (fflush(fp) != 0 || ferror(fp)) {
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# can't write %s\n", cpathname);
+ /* Don't keep partial file */
+ fclose(fp);
+ (void) unlink(cpathname);
+ return;
+ }
+ /* Now write the true mtime */
+ fseek(fp, 4L, 0);
+ assert(mtime < LONG_MAX);
+ PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
+ fflush(fp);
+ fclose(fp);
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("# wrote %s\n", cpathname);
}
static void
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
{
- PyObject *constants, *tmp;
- Py_ssize_t i, n;
+ PyObject *constants, *tmp;
+ Py_ssize_t i, n;
- if (PyUnicode_Compare(co->co_filename, oldname))
- return;
+ if (PyUnicode_Compare(co->co_filename, oldname))
+ return;
- tmp = co->co_filename;
- co->co_filename = newname;
- Py_INCREF(co->co_filename);
- Py_DECREF(tmp);
+ tmp = co->co_filename;
+ co->co_filename = newname;
+ Py_INCREF(co->co_filename);
+ Py_DECREF(tmp);
- constants = co->co_consts;
- n = PyTuple_GET_SIZE(constants);
- for (i = 0; i < n; i++) {
- tmp = PyTuple_GET_ITEM(constants, i);
- if (PyCode_Check(tmp))
- update_code_filenames((PyCodeObject *)tmp,
- oldname, newname);
- }
+ constants = co->co_consts;
+ n = PyTuple_GET_SIZE(constants);
+ for (i = 0; i < n; i++) {
+ tmp = PyTuple_GET_ITEM(constants, i);
+ if (PyCode_Check(tmp))
+ update_code_filenames((PyCodeObject *)tmp,
+ oldname, newname);
+ }
}
static int
update_compiled_module(PyCodeObject *co, char *pathname)
{
- PyObject *oldname, *newname;
+ PyObject *oldname, *newname;
- newname = PyUnicode_DecodeFSDefault(pathname);
- if (newname == NULL)
- return -1;
+ newname = PyUnicode_DecodeFSDefault(pathname);
+ if (newname == NULL)
+ return -1;
- if (!PyUnicode_Compare(co->co_filename, newname)) {
- Py_DECREF(newname);
- return 0;
- }
+ if (!PyUnicode_Compare(co->co_filename, newname)) {
+ Py_DECREF(newname);
+ return 0;
+ }
- oldname = co->co_filename;
- Py_INCREF(oldname);
- update_code_filenames(co, oldname, newname);
- Py_DECREF(oldname);
- Py_DECREF(newname);
- return 1;
+ oldname = co->co_filename;
+ Py_INCREF(oldname);
+ update_code_filenames(co, oldname, newname);
+ Py_DECREF(oldname);
+ Py_DECREF(newname);
+ return 1;
}
/* Load a source module from a given file and return its module
@@ -1011,62 +1011,62 @@ update_compiled_module(PyCodeObject *co, char *pathname)
static PyObject *
load_source_module(char *name, char *pathname, FILE *fp)
{
- struct stat st;
- FILE *fpc;
- char buf[MAXPATHLEN+1];
- char *cpathname;
- PyCodeObject *co;
- PyObject *m;
-
- if (fstat(fileno(fp), &st) != 0) {
- PyErr_Format(PyExc_RuntimeError,
- "unable to get file status from '%s'",
- pathname);
- return NULL;
- }
+ struct stat st;
+ FILE *fpc;
+ char buf[MAXPATHLEN+1];
+ char *cpathname;
+ PyCodeObject *co;
+ PyObject *m;
+
+ if (fstat(fileno(fp), &st) != 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "unable to get file status from '%s'",
+ pathname);
+ return NULL;
+ }
#if SIZEOF_TIME_T > 4
- /* Python's .pyc timestamp handling presumes that the timestamp fits
- in 4 bytes. This will be fine until sometime in the year 2038,
- when a 4-byte signed time_t will overflow.
- */
- if (st.st_mtime >> 32) {
- PyErr_SetString(PyExc_OverflowError,
- "modification time overflows a 4 byte field");
- return NULL;
- }
+ /* Python's .pyc timestamp handling presumes that the timestamp fits
+ in 4 bytes. This will be fine until sometime in the year 2038,
+ when a 4-byte signed time_t will overflow.
+ */
+ if (st.st_mtime >> 32) {
+ PyErr_SetString(PyExc_OverflowError,
+ "modification time overflows a 4 byte field");
+ return NULL;
+ }
#endif
- cpathname = make_compiled_pathname(pathname, buf,
- (size_t)MAXPATHLEN + 1);
- if (cpathname != NULL &&
- (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
- co = read_compiled_module(cpathname, fpc);
- fclose(fpc);
- if (co == NULL)
- return NULL;
- if (update_compiled_module(co, pathname) < 0)
- return NULL;
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # precompiled from %s\n",
- name, cpathname);
- pathname = cpathname;
- }
- else {
- co = parse_source_module(pathname, fp);
- if (co == NULL)
- return NULL;
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # from %s\n",
- name, pathname);
- if (cpathname) {
- PyObject *ro = PySys_GetObject("dont_write_bytecode");
- if (ro == NULL || !PyObject_IsTrue(ro))
- write_compiled_module(co, cpathname, &st);
- }
- }
- m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
- Py_DECREF(co);
-
- return m;
+ cpathname = make_compiled_pathname(pathname, buf,
+ (size_t)MAXPATHLEN + 1);
+ if (cpathname != NULL &&
+ (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
+ co = read_compiled_module(cpathname, fpc);
+ fclose(fpc);
+ if (co == NULL)
+ return NULL;
+ if (update_compiled_module(co, pathname) < 0)
+ return NULL;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # precompiled from %s\n",
+ name, cpathname);
+ pathname = cpathname;
+ }
+ else {
+ co = parse_source_module(pathname, fp);
+ if (co == NULL)
+ return NULL;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # from %s\n",
+ name, pathname);
+ if (cpathname) {
+ PyObject *ro = PySys_GetObject("dont_write_bytecode");
+ if (ro == NULL || !PyObject_IsTrue(ro))
+ write_compiled_module(co, cpathname, &st);
+ }
+ }
+ m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
+ Py_DECREF(co);
+
+ return m;
}
/* Get source file -> unicode or None
@@ -1075,37 +1075,37 @@ load_source_module(char *name, char *pathname, FILE *fp)
static PyObject *
get_sourcefile(const char *file)
{
- char py[MAXPATHLEN + 1];
- Py_ssize_t len;
- PyObject *u;
- struct stat statbuf;
-
- if (!file || !*file) {
- Py_RETURN_NONE;
- }
-
- len = strlen(file);
- /* match '*.py?' */
- if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
- return PyUnicode_DecodeFSDefault(file);
- }
-
- strncpy(py, file, len-1);
- py[len-1] = '\0';
- if (stat(py, &statbuf) == 0 &&
- S_ISREG(statbuf.st_mode)) {
- u = PyUnicode_DecodeFSDefault(py);
- }
- else {
- u = PyUnicode_DecodeFSDefault(file);
- }
- return u;
+ char py[MAXPATHLEN + 1];
+ Py_ssize_t len;
+ PyObject *u;
+ struct stat statbuf;
+
+ if (!file || !*file) {
+ Py_RETURN_NONE;
+ }
+
+ len = strlen(file);
+ /* match '*.py?' */
+ if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
+ return PyUnicode_DecodeFSDefault(file);
+ }
+
+ strncpy(py, file, len-1);
+ py[len-1] = '\0';
+ if (stat(py, &statbuf) == 0 &&
+ S_ISREG(statbuf.st_mode)) {
+ u = PyUnicode_DecodeFSDefault(py);
+ }
+ else {
+ u = PyUnicode_DecodeFSDefault(file);
+ }
+ return u;
}
/* Forward */
static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
static struct filedescr *find_module(char *, char *, PyObject *,
- char *, size_t, FILE **, PyObject **);
+ char *, size_t, FILE **, PyObject **);
static struct _frozen * find_frozen(char *);
/* Load a package and return its module object WITH INCREMENTED
@@ -1114,54 +1114,54 @@ static struct _frozen * find_frozen(char *);
static PyObject *
load_package(char *name, char *pathname)
{
- PyObject *m, *d;
- PyObject *file = NULL;
- PyObject *path = NULL;
- int err;
- char buf[MAXPATHLEN+1];
- FILE *fp = NULL;
- struct filedescr *fdp;
-
- m = PyImport_AddModule(name);
- if (m == NULL)
- return NULL;
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # directory %s\n",
- name, pathname);
- d = PyModule_GetDict(m);
- file = get_sourcefile(pathname);
- if (file == NULL)
- goto error;
- path = Py_BuildValue("[O]", file);
- if (path == NULL)
- goto error;
- err = PyDict_SetItemString(d, "__file__", file);
- if (err == 0)
- err = PyDict_SetItemString(d, "__path__", path);
- if (err != 0)
- goto error;
- buf[0] = '\0';
- fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
- if (fdp == NULL) {
- if (PyErr_ExceptionMatches(PyExc_ImportError)) {
- PyErr_Clear();
- Py_INCREF(m);
- }
- else
- m = NULL;
- goto cleanup;
- }
- m = load_module(name, fp, buf, fdp->type, NULL);
- if (fp != NULL)
- fclose(fp);
- goto cleanup;
+ PyObject *m, *d;
+ PyObject *file = NULL;
+ PyObject *path = NULL;
+ int err;
+ char buf[MAXPATHLEN+1];
+ FILE *fp = NULL;
+ struct filedescr *fdp;
+
+ m = PyImport_AddModule(name);
+ if (m == NULL)
+ return NULL;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # directory %s\n",
+ name, pathname);
+ d = PyModule_GetDict(m);
+ file = get_sourcefile(pathname);
+ if (file == NULL)
+ goto error;
+ path = Py_BuildValue("[O]", file);
+ if (path == NULL)
+ goto error;
+ err = PyDict_SetItemString(d, "__file__", file);
+ if (err == 0)
+ err = PyDict_SetItemString(d, "__path__", path);
+ if (err != 0)
+ goto error;
+ buf[0] = '\0';
+ fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
+ if (fdp == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_ImportError)) {
+ PyErr_Clear();
+ Py_INCREF(m);
+ }
+ else
+ m = NULL;
+ goto cleanup;
+ }
+ m = load_module(name, fp, buf, fdp->type, NULL);
+ if (fp != NULL)
+ fclose(fp);
+ goto cleanup;
error:
- m = NULL;
+ m = NULL;
cleanup:
- Py_XDECREF(path);
- Py_XDECREF(file);
- return m;
+ Py_XDECREF(path);
+ Py_XDECREF(file);
+ return m;
}
@@ -1170,16 +1170,16 @@ load_package(char *name, char *pathname)
static int
is_builtin(char *name)
{
- int i;
- for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
- if (strcmp(name, PyImport_Inittab[i].name) == 0) {
- if (PyImport_Inittab[i].initfunc == NULL)
- return -1;
- else
- return 1;
- }
- }
- return 0;
+ int i;
+ for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
+ if (strcmp(name, PyImport_Inittab[i].name) == 0) {
+ if (PyImport_Inittab[i].initfunc == NULL)
+ return -1;
+ else
+ return 1;
+ }
+ }
+ return 0;
}
@@ -1193,72 +1193,72 @@ is_builtin(char *name)
static PyObject *
get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
- PyObject *p)
-{
- PyObject *importer;
- Py_ssize_t j, nhooks;
-
- /* These conditions are the caller's responsibility: */
- assert(PyList_Check(path_hooks));
- assert(PyDict_Check(path_importer_cache));
-
- nhooks = PyList_Size(path_hooks);
- if (nhooks < 0)
- return NULL; /* Shouldn't happen */
-
- importer = PyDict_GetItem(path_importer_cache, p);
- if (importer != NULL)
- return importer;
-
- /* set path_importer_cache[p] to None to avoid recursion */
- if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
- return NULL;
-
- for (j = 0; j < nhooks; j++) {
- PyObject *hook = PyList_GetItem(path_hooks, j);
- if (hook == NULL)
- return NULL;
- importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
- if (importer != NULL)
- break;
-
- if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
- return NULL;
- }
- PyErr_Clear();
- }
- if (importer == NULL) {
- importer = PyObject_CallFunctionObjArgs(
- (PyObject *)&PyNullImporter_Type, p, NULL
- );
- if (importer == NULL) {
- if (PyErr_ExceptionMatches(PyExc_ImportError)) {
- PyErr_Clear();
- return Py_None;
- }
- }
- }
- if (importer != NULL) {
- int err = PyDict_SetItem(path_importer_cache, p, importer);
- Py_DECREF(importer);
- if (err != 0)
- return NULL;
- }
- return importer;
+ PyObject *p)
+{
+ PyObject *importer;
+ Py_ssize_t j, nhooks;
+
+ /* These conditions are the caller's responsibility: */
+ assert(PyList_Check(path_hooks));
+ assert(PyDict_Check(path_importer_cache));
+
+ nhooks = PyList_Size(path_hooks);
+ if (nhooks < 0)
+ return NULL; /* Shouldn't happen */
+
+ importer = PyDict_GetItem(path_importer_cache, p);
+ if (importer != NULL)
+ return importer;
+
+ /* set path_importer_cache[p] to None to avoid recursion */
+ if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
+ return NULL;
+
+ for (j = 0; j < nhooks; j++) {
+ PyObject *hook = PyList_GetItem(path_hooks, j);
+ if (hook == NULL)
+ return NULL;
+ importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
+ if (importer != NULL)
+ break;
+
+ if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
+ return NULL;
+ }
+ PyErr_Clear();
+ }
+ if (importer == NULL) {
+ importer = PyObject_CallFunctionObjArgs(
+ (PyObject *)&PyNullImporter_Type, p, NULL
+ );
+ if (importer == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_ImportError)) {
+ PyErr_Clear();
+ return Py_None;
+ }
+ }
+ }
+ if (importer != NULL) {
+ int err = PyDict_SetItem(path_importer_cache, p, importer);
+ Py_DECREF(importer);
+ if (err != 0)
+ return NULL;
+ }
+ return importer;
}
PyAPI_FUNC(PyObject *)
PyImport_GetImporter(PyObject *path) {
- PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
+ PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
- if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
- if ((path_hooks = PySys_GetObject("path_hooks"))) {
- importer = get_path_importer(path_importer_cache,
- path_hooks, path);
- }
- }
- Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
- return importer;
+ if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
+ if ((path_hooks = PySys_GetObject("path_hooks"))) {
+ importer = get_path_importer(path_importer_cache,
+ path_hooks, path);
+ }
+ }
+ Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
+ return importer;
}
/* Search the path (default sys.path) for a module. Return the
@@ -1267,7 +1267,7 @@ PyImport_GetImporter(PyObject *path) {
#ifdef MS_COREDLL
extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
- char *, Py_ssize_t);
+ char *, Py_ssize_t);
#endif
static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
@@ -1276,276 +1276,276 @@ static struct filedescr importhookdescr = {"", "", IMP_HOOK};
static struct filedescr *
find_module(char *fullname, char *subname, PyObject *path, char *buf,
- size_t buflen, FILE **p_fp, PyObject **p_loader)
-{
- Py_ssize_t i, npath;
- size_t len, namelen;
- struct filedescr *fdp = NULL;
- char *filemode;
- FILE *fp = NULL;
- PyObject *path_hooks, *path_importer_cache;
- struct stat statbuf;
- static struct filedescr fd_frozen = {"", "", PY_FROZEN};
- static struct filedescr fd_builtin = {"", "", C_BUILTIN};
- static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
- char name[MAXPATHLEN+1];
+ size_t buflen, FILE **p_fp, PyObject **p_loader)
+{
+ Py_ssize_t i, npath;
+ size_t len, namelen;
+ struct filedescr *fdp = NULL;
+ char *filemode;
+ FILE *fp = NULL;
+ PyObject *path_hooks, *path_importer_cache;
+ struct stat statbuf;
+ static struct filedescr fd_frozen = {"", "", PY_FROZEN};
+ static struct filedescr fd_builtin = {"", "", C_BUILTIN};
+ static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
+ char name[MAXPATHLEN+1];
#if defined(PYOS_OS2)
- size_t saved_len;
- size_t saved_namelen;
- char *saved_buf = NULL;
+ size_t saved_len;
+ size_t saved_namelen;
+ char *saved_buf = NULL;
#endif
- if (p_loader != NULL)
- *p_loader = NULL;
-
- if (strlen(subname) > MAXPATHLEN) {
- PyErr_SetString(PyExc_OverflowError,
- "module name is too long");
- return NULL;
- }
- strcpy(name, subname);
-
- /* sys.meta_path import hook */
- if (p_loader != NULL) {
- PyObject *meta_path;
-
- meta_path = PySys_GetObject("meta_path");
- if (meta_path == NULL || !PyList_Check(meta_path)) {
- PyErr_SetString(PyExc_ImportError,
- "sys.meta_path must be a list of "
- "import hooks");
- return NULL;
- }
- Py_INCREF(meta_path); /* zap guard */
- npath = PyList_Size(meta_path);
- for (i = 0; i < npath; i++) {
- PyObject *loader;
- PyObject *hook = PyList_GetItem(meta_path, i);
- loader = PyObject_CallMethod(hook, "find_module",
- "sO", fullname,
- path != NULL ?
- path : Py_None);
- if (loader == NULL) {
- Py_DECREF(meta_path);
- return NULL; /* true error */
- }
- if (loader != Py_None) {
- /* a loader was found */
- *p_loader = loader;
- Py_DECREF(meta_path);
- return &importhookdescr;
- }
- Py_DECREF(loader);
- }
- Py_DECREF(meta_path);
- }
-
- if (find_frozen(fullname) != NULL) {
- strcpy(buf, fullname);
- return &fd_frozen;
- }
-
- if (path == NULL) {
- if (is_builtin(name)) {
- strcpy(buf, name);
- return &fd_builtin;
- }
+ if (p_loader != NULL)
+ *p_loader = NULL;
+
+ if (strlen(subname) > MAXPATHLEN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "module name is too long");
+ return NULL;
+ }
+ strcpy(name, subname);
+
+ /* sys.meta_path import hook */
+ if (p_loader != NULL) {
+ PyObject *meta_path;
+
+ meta_path = PySys_GetObject("meta_path");
+ if (meta_path == NULL || !PyList_Check(meta_path)) {
+ PyErr_SetString(PyExc_ImportError,
+ "sys.meta_path must be a list of "
+ "import hooks");
+ return NULL;
+ }
+ Py_INCREF(meta_path); /* zap guard */
+ npath = PyList_Size(meta_path);
+ for (i = 0; i < npath; i++) {
+ PyObject *loader;
+ PyObject *hook = PyList_GetItem(meta_path, i);
+ loader = PyObject_CallMethod(hook, "find_module",
+ "sO", fullname,
+ path != NULL ?
+ path : Py_None);
+ if (loader == NULL) {
+ Py_DECREF(meta_path);
+ return NULL; /* true error */
+ }
+ if (loader != Py_None) {
+ /* a loader was found */
+ *p_loader = loader;
+ Py_DECREF(meta_path);
+ return &importhookdescr;
+ }
+ Py_DECREF(loader);
+ }
+ Py_DECREF(meta_path);
+ }
+
+ if (find_frozen(fullname) != NULL) {
+ strcpy(buf, fullname);
+ return &fd_frozen;
+ }
+
+ if (path == NULL) {
+ if (is_builtin(name)) {
+ strcpy(buf, name);
+ return &fd_builtin;
+ }
#ifdef MS_COREDLL
- fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
- if (fp != NULL) {
- *p_fp = fp;
- return fdp;
- }
+ fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
+ if (fp != NULL) {
+ *p_fp = fp;
+ return fdp;
+ }
#endif
- path = PySys_GetObject("path");
- }
-
- if (path == NULL || !PyList_Check(path)) {
- PyErr_SetString(PyExc_ImportError,
- "sys.path must be a list of directory names");
- return NULL;
- }
-
- path_hooks = PySys_GetObject("path_hooks");
- if (path_hooks == NULL || !PyList_Check(path_hooks)) {
- PyErr_SetString(PyExc_ImportError,
- "sys.path_hooks must be a list of "
- "import hooks");
- return NULL;
- }
- path_importer_cache = PySys_GetObject("path_importer_cache");
- if (path_importer_cache == NULL ||
- !PyDict_Check(path_importer_cache)) {
- PyErr_SetString(PyExc_ImportError,
- "sys.path_importer_cache must be a dict");
- return NULL;
- }
-
- npath = PyList_Size(path);
- namelen = strlen(name);
- for (i = 0; i < npath; i++) {
- PyObject *v = PyList_GetItem(path, i);
- PyObject *origv = v;
- const char *base;
- Py_ssize_t size;
- if (!v)
- return NULL;
- if (PyUnicode_Check(v)) {
- v = PyUnicode_AsEncodedString(v,
- Py_FileSystemDefaultEncoding, NULL);
- if (v == NULL)
- return NULL;
- }
- else if (!PyBytes_Check(v))
- continue;
- else
- Py_INCREF(v);
-
- base = PyBytes_AS_STRING(v);
- size = PyBytes_GET_SIZE(v);
- len = size;
- if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
- Py_DECREF(v);
- continue; /* Too long */
- }
- strcpy(buf, base);
- Py_DECREF(v);
-
- if (strlen(buf) != len) {
- continue; /* v contains '\0' */
- }
-
- /* sys.path_hooks import hook */
- if (p_loader != NULL) {
- PyObject *importer;
-
- importer = get_path_importer(path_importer_cache,
- path_hooks, origv);
- if (importer == NULL) {
- return NULL;
- }
- /* Note: importer is a borrowed reference */
- if (importer != Py_None) {
- PyObject *loader;
- loader = PyObject_CallMethod(importer,
- "find_module",
- "s", fullname);
- if (loader == NULL)
- return NULL; /* error */
- if (loader != Py_None) {
- /* a loader was found */
- *p_loader = loader;
- return &importhookdescr;
- }
- Py_DECREF(loader);
- continue;
- }
- }
- /* no hook was found, use builtin import */
-
- if (len > 0 && buf[len-1] != SEP
+ path = PySys_GetObject("path");
+ }
+
+ if (path == NULL || !PyList_Check(path)) {
+ PyErr_SetString(PyExc_ImportError,
+ "sys.path must be a list of directory names");
+ return NULL;
+ }
+
+ path_hooks = PySys_GetObject("path_hooks");
+ if (path_hooks == NULL || !PyList_Check(path_hooks)) {
+ PyErr_SetString(PyExc_ImportError,
+ "sys.path_hooks must be a list of "
+ "import hooks");
+ return NULL;
+ }
+ path_importer_cache = PySys_GetObject("path_importer_cache");
+ if (path_importer_cache == NULL ||
+ !PyDict_Check(path_importer_cache)) {
+ PyErr_SetString(PyExc_ImportError,
+ "sys.path_importer_cache must be a dict");
+ return NULL;
+ }
+
+ npath = PyList_Size(path);
+ namelen = strlen(name);
+ for (i = 0; i < npath; i++) {
+ PyObject *v = PyList_GetItem(path, i);
+ PyObject *origv = v;
+ const char *base;
+ Py_ssize_t size;
+ if (!v)
+ return NULL;
+ if (PyUnicode_Check(v)) {
+ v = PyUnicode_AsEncodedString(v,
+ Py_FileSystemDefaultEncoding, NULL);
+ if (v == NULL)
+ return NULL;
+ }
+ else if (!PyBytes_Check(v))
+ continue;
+ else
+ Py_INCREF(v);
+
+ base = PyBytes_AS_STRING(v);
+ size = PyBytes_GET_SIZE(v);
+ len = size;
+ if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
+ Py_DECREF(v);
+ continue; /* Too long */
+ }
+ strcpy(buf, base);
+ Py_DECREF(v);
+
+ if (strlen(buf) != len) {
+ continue; /* v contains '\0' */
+ }
+
+ /* sys.path_hooks import hook */
+ if (p_loader != NULL) {
+ PyObject *importer;
+
+ importer = get_path_importer(path_importer_cache,
+ path_hooks, origv);
+ if (importer == NULL) {
+ return NULL;
+ }
+ /* Note: importer is a borrowed reference */
+ if (importer != Py_None) {
+ PyObject *loader;
+ loader = PyObject_CallMethod(importer,
+ "find_module",
+ "s", fullname);
+ if (loader == NULL)
+ return NULL; /* error */
+ if (loader != Py_None) {
+ /* a loader was found */
+ *p_loader = loader;
+ return &importhookdescr;
+ }
+ Py_DECREF(loader);
+ continue;
+ }
+ }
+ /* no hook was found, use builtin import */
+
+ if (len > 0 && buf[len-1] != SEP
#ifdef ALTSEP
- && buf[len-1] != ALTSEP
+ && buf[len-1] != ALTSEP
#endif
- )
- buf[len++] = SEP;
- strcpy(buf+len, name);
- len += namelen;
+ )
+ buf[len++] = SEP;
+ strcpy(buf+len, name);
+ len += namelen;
- /* Check for package import (buf holds a directory name,
- and there's an __init__ module in that directory */
+ /* Check for package import (buf holds a directory name,
+ and there's an __init__ module in that directory */
#ifdef HAVE_STAT
- if (stat(buf, &statbuf) == 0 && /* it exists */
- S_ISDIR(statbuf.st_mode) && /* it's a directory */
- case_ok(buf, len, namelen, name)) { /* case matches */
- if (find_init_module(buf)) { /* and has __init__.py */
- return &fd_package;
- }
- else {
- char warnstr[MAXPATHLEN+80];
- sprintf(warnstr, "Not importing directory "
- "'%.*s': missing __init__.py",
- MAXPATHLEN, buf);
- if (PyErr_WarnEx(PyExc_ImportWarning,
- warnstr, 1)) {
- return NULL;
- }
- }
- }
+ if (stat(buf, &statbuf) == 0 && /* it exists */
+ S_ISDIR(statbuf.st_mode) && /* it's a directory */
+ case_ok(buf, len, namelen, name)) { /* case matches */
+ if (find_init_module(buf)) { /* and has __init__.py */
+ return &fd_package;
+ }
+ else {
+ char warnstr[MAXPATHLEN+80];
+ sprintf(warnstr, "Not importing directory "
+ "'%.*s': missing __init__.py",
+ MAXPATHLEN, buf);
+ if (PyErr_WarnEx(PyExc_ImportWarning,
+ warnstr, 1)) {
+ return NULL;
+ }
+ }
+ }
#endif
#if defined(PYOS_OS2)
- /* take a snapshot of the module spec for restoration
- * after the 8 character DLL hackery
- */
- saved_buf = strdup(buf);
- saved_len = len;
- saved_namelen = namelen;
+ /* take a snapshot of the module spec for restoration
+ * after the 8 character DLL hackery
+ */
+ saved_buf = strdup(buf);
+ saved_len = len;
+ saved_namelen = namelen;
#endif /* PYOS_OS2 */
- for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
+ for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
- /* OS/2 limits DLLs to 8 character names (w/o
- extension)
- * so if the name is longer than that and its a
- * dynamically loaded module we're going to try,
- * truncate the name before trying
- */
- if (strlen(subname) > 8) {
- /* is this an attempt to load a C extension? */
- const struct filedescr *scan;
- scan = _PyImport_DynLoadFiletab;
- while (scan->suffix != NULL) {
- if (!strcmp(scan->suffix, fdp->suffix))
- break;
- else
- scan++;
- }
- if (scan->suffix != NULL) {
- /* yes, so truncate the name */
- namelen = 8;
- len -= strlen(subname) - namelen;
- buf[len] = '\0';
- }
- }
+ /* OS/2 limits DLLs to 8 character names (w/o
+ extension)
+ * so if the name is longer than that and its a
+ * dynamically loaded module we're going to try,
+ * truncate the name before trying
+ */
+ if (strlen(subname) > 8) {
+ /* is this an attempt to load a C extension? */
+ const struct filedescr *scan;
+ scan = _PyImport_DynLoadFiletab;
+ while (scan->suffix != NULL) {
+ if (!strcmp(scan->suffix, fdp->suffix))
+ break;
+ else
+ scan++;
+ }
+ if (scan->suffix != NULL) {
+ /* yes, so truncate the name */
+ namelen = 8;
+ len -= strlen(subname) - namelen;
+ buf[len] = '\0';
+ }
+ }
#endif /* PYOS_OS2 */
- strcpy(buf+len, fdp->suffix);
- if (Py_VerboseFlag > 1)
- PySys_WriteStderr("# trying %s\n", buf);
- filemode = fdp->mode;
- if (filemode[0] == 'U')
- filemode = "r" PY_STDIOTEXTMODE;
- fp = fopen(buf, filemode);
- if (fp != NULL) {
- if (case_ok(buf, len, namelen, name))
- break;
- else { /* continue search */
- fclose(fp);
- fp = NULL;
- }
- }
+ strcpy(buf+len, fdp->suffix);
+ if (Py_VerboseFlag > 1)
+ PySys_WriteStderr("# trying %s\n", buf);
+ filemode = fdp->mode;
+ if (filemode[0] == 'U')
+ filemode = "r" PY_STDIOTEXTMODE;
+ fp = fopen(buf, filemode);
+ if (fp != NULL) {
+ if (case_ok(buf, len, namelen, name))
+ break;
+ else { /* continue search */
+ fclose(fp);
+ fp = NULL;
+ }
+ }
#if defined(PYOS_OS2)
- /* restore the saved snapshot */
- strcpy(buf, saved_buf);
- len = saved_len;
- namelen = saved_namelen;
+ /* restore the saved snapshot */
+ strcpy(buf, saved_buf);
+ len = saved_len;
+ namelen = saved_namelen;
#endif
- }
+ }
#if defined(PYOS_OS2)
- /* don't need/want the module name snapshot anymore */
- if (saved_buf)
- {
- free(saved_buf);
- saved_buf = NULL;
- }
+ /* don't need/want the module name snapshot anymore */
+ if (saved_buf)
+ {
+ free(saved_buf);
+ saved_buf = NULL;
+ }
#endif
- if (fp != NULL)
- break;
- }
- if (fp == NULL) {
- PyErr_Format(PyExc_ImportError,
- "No module named %.200s", name);
- return NULL;
- }
- *p_fp = fp;
- return fdp;
+ if (fp != NULL)
+ break;
+ }
+ if (fp == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "No module named %.200s", name);
+ return NULL;
+ }
+ *p_fp = fp;
+ return fdp;
}
/* Helpers for main.c
@@ -1553,15 +1553,15 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
*/
struct filedescr *
_PyImport_FindModule(const char *name, PyObject *path, char *buf,
- size_t buflen, FILE **p_fp, PyObject **p_loader)
+ size_t buflen, FILE **p_fp, PyObject **p_loader)
{
- return find_module((char *) name, (char *) name, path,
- buf, buflen, p_fp, p_loader);
+ return find_module((char *) name, (char *) name, path,
+ buf, buflen, p_fp, p_loader);
}
PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
{
- return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
+ return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
}
/* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
@@ -1617,103 +1617,103 @@ case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
/* MS_WINDOWS */
#if defined(MS_WINDOWS)
- WIN32_FIND_DATA data;
- HANDLE h;
-
- if (Py_GETENV("PYTHONCASEOK") != NULL)
- return 1;
-
- h = FindFirstFile(buf, &data);
- if (h == INVALID_HANDLE_VALUE) {
- PyErr_Format(PyExc_NameError,
- "Can't find file for module %.100s\n(filename %.300s)",
- name, buf);
- return 0;
- }
- FindClose(h);
- return strncmp(data.cFileName, name, namelen) == 0;
+ WIN32_FIND_DATA data;
+ HANDLE h;
+
+ if (Py_GETENV("PYTHONCASEOK") != NULL)
+ return 1;
+
+ h = FindFirstFile(buf, &data);
+ if (h == INVALID_HANDLE_VALUE) {
+ PyErr_Format(PyExc_NameError,
+ "Can't find file for module %.100s\n(filename %.300s)",
+ name, buf);
+ return 0;
+ }
+ FindClose(h);
+ return strncmp(data.cFileName, name, namelen) == 0;
/* DJGPP */
#elif defined(DJGPP)
- struct ffblk ffblk;
- int done;
+ struct ffblk ffblk;
+ int done;
- if (Py_GETENV("PYTHONCASEOK") != NULL)
- return 1;
+ if (Py_GETENV("PYTHONCASEOK") != NULL)
+ return 1;
- done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
- if (done) {
- PyErr_Format(PyExc_NameError,
- "Can't find file for module %.100s\n(filename %.300s)",
- name, buf);
- return 0;
- }
- return strncmp(ffblk.ff_name, name, namelen) == 0;
+ done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
+ if (done) {
+ PyErr_Format(PyExc_NameError,
+ "Can't find file for module %.100s\n(filename %.300s)",
+ name, buf);
+ return 0;
+ }
+ return strncmp(ffblk.ff_name, name, namelen) == 0;
/* new-fangled macintosh (macosx) or Cygwin */
#elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
- DIR *dirp;
- struct dirent *dp;
- char dirname[MAXPATHLEN + 1];
- const int dirlen = len - namelen - 1; /* don't want trailing SEP */
-
- if (Py_GETENV("PYTHONCASEOK") != NULL)
- return 1;
-
- /* Copy the dir component into dirname; substitute "." if empty */
- if (dirlen <= 0) {
- dirname[0] = '.';
- dirname[1] = '\0';
- }
- else {
- assert(dirlen <= MAXPATHLEN);
- memcpy(dirname, buf, dirlen);
- dirname[dirlen] = '\0';
- }
- /* Open the directory and search the entries for an exact match. */
- dirp = opendir(dirname);
- if (dirp) {
- char *nameWithExt = buf + len - namelen;
- while ((dp = readdir(dirp)) != NULL) {
- const int thislen =
+ DIR *dirp;
+ struct dirent *dp;
+ char dirname[MAXPATHLEN + 1];
+ const int dirlen = len - namelen - 1; /* don't want trailing SEP */
+
+ if (Py_GETENV("PYTHONCASEOK") != NULL)
+ return 1;
+
+ /* Copy the dir component into dirname; substitute "." if empty */
+ if (dirlen <= 0) {
+ dirname[0] = '.';
+ dirname[1] = '\0';
+ }
+ else {
+ assert(dirlen <= MAXPATHLEN);
+ memcpy(dirname, buf, dirlen);
+ dirname[dirlen] = '\0';
+ }
+ /* Open the directory and search the entries for an exact match. */
+ dirp = opendir(dirname);
+ if (dirp) {
+ char *nameWithExt = buf + len - namelen;
+ while ((dp = readdir(dirp)) != NULL) {
+ const int thislen =
#ifdef _DIRENT_HAVE_D_NAMELEN
- dp->d_namlen;
+ dp->d_namlen;
#else
- strlen(dp->d_name);
+ strlen(dp->d_name);
#endif
- if (thislen >= namelen &&
- strcmp(dp->d_name, nameWithExt) == 0) {
- (void)closedir(dirp);
- return 1; /* Found */
- }
- }
- (void)closedir(dirp);
- }
- return 0 ; /* Not found */
+ if (thislen >= namelen &&
+ strcmp(dp->d_name, nameWithExt) == 0) {
+ (void)closedir(dirp);
+ return 1; /* Found */
+ }
+ }
+ (void)closedir(dirp);
+ }
+ return 0 ; /* Not found */
/* OS/2 */
#elif defined(PYOS_OS2)
- HDIR hdir = 1;
- ULONG srchcnt = 1;
- FILEFINDBUF3 ffbuf;
- APIRET rc;
-
- if (Py_GETENV("PYTHONCASEOK") != NULL)
- return 1;
-
- rc = DosFindFirst(buf,
- &hdir,
- FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
- &ffbuf, sizeof(ffbuf),
- &srchcnt,
- FIL_STANDARD);
- if (rc != NO_ERROR)
- return 0;
- return strncmp(ffbuf.achName, name, namelen) == 0;
+ HDIR hdir = 1;
+ ULONG srchcnt = 1;
+ FILEFINDBUF3 ffbuf;
+ APIRET rc;
+
+ if (Py_GETENV("PYTHONCASEOK") != NULL)
+ return 1;
+
+ rc = DosFindFirst(buf,
+ &hdir,
+ FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
+ &ffbuf, sizeof(ffbuf),
+ &srchcnt,
+ FIL_STANDARD);
+ if (rc != NO_ERROR)
+ return 0;
+ return strncmp(ffbuf.achName, name, namelen) == 0;
/* assuming it's a case-sensitive filesystem, so there's nothing to do! */
#else
- return 1;
+ return 1;
#endif
}
@@ -1724,46 +1724,46 @@ case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
static int
find_init_module(char *buf)
{
- const size_t save_len = strlen(buf);
- size_t i = save_len;
- char *pname; /* pointer to start of __init__ */
- struct stat statbuf;
-
-/* For calling case_ok(buf, len, namelen, name):
- * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
- * ^ ^ ^ ^
- * |--------------------- buf ---------------------|
- * |------------------- len ------------------|
- * |------ name -------|
- * |----- namelen -----|
+ const size_t save_len = strlen(buf);
+ size_t i = save_len;
+ char *pname; /* pointer to start of __init__ */
+ struct stat statbuf;
+
+/* For calling case_ok(buf, len, namelen, name):
+ * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
+ * ^ ^ ^ ^
+ * |--------------------- buf ---------------------|
+ * |------------------- len ------------------|
+ * |------ name -------|
+ * |----- namelen -----|
*/
- if (save_len + 13 >= MAXPATHLEN)
- return 0;
- buf[i++] = SEP;
- pname = buf + i;
- strcpy(pname, "__init__.py");
- if (stat(buf, &statbuf) == 0) {
- if (case_ok(buf,
- save_len + 9, /* len("/__init__") */
- 8, /* len("__init__") */
- pname)) {
- buf[save_len] = '\0';
- return 1;
- }
- }
- i += strlen(pname);
- strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
- if (stat(buf, &statbuf) == 0) {
- if (case_ok(buf,
- save_len + 9, /* len("/__init__") */
- 8, /* len("__init__") */
- pname)) {
- buf[save_len] = '\0';
- return 1;
- }
- }
- buf[save_len] = '\0';
- return 0;
+ if (save_len + 13 >= MAXPATHLEN)
+ return 0;
+ buf[i++] = SEP;
+ pname = buf + i;
+ strcpy(pname, "__init__.py");
+ if (stat(buf, &statbuf) == 0) {
+ if (case_ok(buf,
+ save_len + 9, /* len("/__init__") */
+ 8, /* len("__init__") */
+ pname)) {
+ buf[save_len] = '\0';
+ return 1;
+ }
+ }
+ i += strlen(pname);
+ strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
+ if (stat(buf, &statbuf) == 0) {
+ if (case_ok(buf,
+ save_len + 9, /* len("/__init__") */
+ 8, /* len("__init__") */
+ pname)) {
+ buf[save_len] = '\0';
+ return 1;
+ }
+ }
+ buf[save_len] = '\0';
+ return 0;
}
#endif /* HAVE_STAT */
@@ -1777,93 +1777,93 @@ static int init_builtin(char *); /* Forward */
static PyObject *
load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
{
- PyObject *modules;
- PyObject *m;
- int err;
+ PyObject *modules;
+ PyObject *m;
+ int err;
- /* First check that there's an open file (if we need one) */
- switch (type) {
- case PY_SOURCE:
- case PY_COMPILED:
- if (fp == NULL) {
- PyErr_Format(PyExc_ValueError,
- "file object required for import (type code %d)",
- type);
- return NULL;
- }
- }
+ /* First check that there's an open file (if we need one) */
+ switch (type) {
+ case PY_SOURCE:
+ case PY_COMPILED:
+ if (fp == NULL) {
+ PyErr_Format(PyExc_ValueError,
+ "file object required for import (type code %d)",
+ type);
+ return NULL;
+ }
+ }
- switch (type) {
+ switch (type) {
- case PY_SOURCE:
- m = load_source_module(name, pathname, fp);
- break;
+ case PY_SOURCE:
+ m = load_source_module(name, pathname, fp);
+ break;
- case PY_COMPILED:
- m = load_compiled_module(name, pathname, fp);
- break;
+ case PY_COMPILED:
+ m = load_compiled_module(name, pathname, fp);
+ break;
#ifdef HAVE_DYNAMIC_LOADING
- case C_EXTENSION:
- m = _PyImport_LoadDynamicModule(name, pathname, fp);
- break;
+ case C_EXTENSION:
+ m = _PyImport_LoadDynamicModule(name, pathname, fp);
+ break;
#endif
- case PKG_DIRECTORY:
- m = load_package(name, pathname);
- break;
-
- case C_BUILTIN:
- case PY_FROZEN:
- if (pathname != NULL && pathname[0] != '\0')
- name = pathname;
- if (type == C_BUILTIN)
- err = init_builtin(name);
- else
- err = PyImport_ImportFrozenModule(name);
- if (err < 0)
- return NULL;
- if (err == 0) {
- PyErr_Format(PyExc_ImportError,
- "Purported %s module %.200s not found",
- type == C_BUILTIN ?
- "builtin" : "frozen",
- name);
- return NULL;
- }
- modules = PyImport_GetModuleDict();
- m = PyDict_GetItemString(modules, name);
- if (m == NULL) {
- PyErr_Format(
- PyExc_ImportError,
- "%s module %.200s not properly initialized",
- type == C_BUILTIN ?
- "builtin" : "frozen",
- name);
- return NULL;
- }
- Py_INCREF(m);
- break;
-
- case IMP_HOOK: {
- if (loader == NULL) {
- PyErr_SetString(PyExc_ImportError,
- "import hook without loader");
- return NULL;
- }
- m = PyObject_CallMethod(loader, "load_module", "s", name);
- break;
- }
-
- default:
- PyErr_Format(PyExc_ImportError,
- "Don't know how to import %.200s (type code %d)",
- name, type);
- m = NULL;
-
- }
-
- return m;
+ case PKG_DIRECTORY:
+ m = load_package(name, pathname);
+ break;
+
+ case C_BUILTIN:
+ case PY_FROZEN:
+ if (pathname != NULL && pathname[0] != '\0')
+ name = pathname;
+ if (type == C_BUILTIN)
+ err = init_builtin(name);
+ else
+ err = PyImport_ImportFrozenModule(name);
+ if (err < 0)
+ return NULL;
+ if (err == 0) {
+ PyErr_Format(PyExc_ImportError,
+ "Purported %s module %.200s not found",
+ type == C_BUILTIN ?
+ "builtin" : "frozen",
+ name);
+ return NULL;
+ }
+ modules = PyImport_GetModuleDict();
+ m = PyDict_GetItemString(modules, name);
+ if (m == NULL) {
+ PyErr_Format(
+ PyExc_ImportError,
+ "%s module %.200s not properly initialized",
+ type == C_BUILTIN ?
+ "builtin" : "frozen",
+ name);
+ return NULL;
+ }
+ Py_INCREF(m);
+ break;
+
+ case IMP_HOOK: {
+ if (loader == NULL) {
+ PyErr_SetString(PyExc_ImportError,
+ "import hook without loader");
+ return NULL;
+ }
+ m = PyObject_CallMethod(loader, "load_module", "s", name);
+ break;
+ }
+
+ default:
+ PyErr_Format(PyExc_ImportError,
+ "Don't know how to import %.200s (type code %d)",
+ name, type);
+ m = NULL;
+
+ }
+
+ return m;
}
@@ -1874,34 +1874,34 @@ load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
static int
init_builtin(char *name)
{
- struct _inittab *p;
-
- if (_PyImport_FindExtension(name, name) != NULL)
- return 1;
-
- for (p = PyImport_Inittab; p->name != NULL; p++) {
- PyObject *mod;
- if (strcmp(name, p->name) == 0) {
- if (p->initfunc == NULL) {
- PyErr_Format(PyExc_ImportError,
- "Cannot re-init internal module %.200s",
- name);
- return -1;
- }
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # builtin\n", name);
- mod = (*p->initfunc)();
- if (mod == 0)
- return -1;
- if (_PyImport_FixupExtension(mod, name, name) < 0)
- return -1;
- /* FixupExtension has put the module into sys.modules,
- so we can release our own reference. */
- Py_DECREF(mod);
- return 1;
- }
- }
- return 0;
+ struct _inittab *p;
+
+ if (_PyImport_FindExtension(name, name) != NULL)
+ return 1;
+
+ for (p = PyImport_Inittab; p->name != NULL; p++) {
+ PyObject *mod;
+ if (strcmp(name, p->name) == 0) {
+ if (p->initfunc == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "Cannot re-init internal module %.200s",
+ name);
+ return -1;
+ }
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # builtin\n", name);
+ mod = (*p->initfunc)();
+ if (mod == 0)
+ return -1;
+ if (_PyImport_FixupExtension(mod, name, name) < 0)
+ return -1;
+ /* FixupExtension has put the module into sys.modules,
+ so we can release our own reference. */
+ Py_DECREF(mod);
+ return 1;
+ }
+ }
+ return 0;
}
@@ -1910,63 +1910,63 @@ init_builtin(char *name)
static struct _frozen *
find_frozen(char *name)
{
- struct _frozen *p;
+ struct _frozen *p;
- if (!name)
- return NULL;
+ if (!name)
+ return NULL;
- for (p = PyImport_FrozenModules; ; p++) {
- if (p->name == NULL)
- return NULL;
- if (strcmp(p->name, name) == 0)
- break;
- }
- return p;
+ for (p = PyImport_FrozenModules; ; p++) {
+ if (p->name == NULL)
+ return NULL;
+ if (strcmp(p->name, name) == 0)
+ break;
+ }
+ return p;
}
static PyObject *
get_frozen_object(char *name)
{
- struct _frozen *p = find_frozen(name);
- int size;
-
- if (p == NULL) {
- PyErr_Format(PyExc_ImportError,
- "No such frozen object named %.200s",
- name);
- return NULL;
- }
- if (p->code == NULL) {
- PyErr_Format(PyExc_ImportError,
- "Excluded frozen object named %.200s",
- name);
- return NULL;
- }
- size = p->size;
- if (size < 0)
- size = -size;
- return PyMarshal_ReadObjectFromString((char *)p->code, size);
+ struct _frozen *p = find_frozen(name);
+ int size;
+
+ if (p == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "No such frozen object named %.200s",
+ name);
+ return NULL;
+ }
+ if (p->code == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "Excluded frozen object named %.200s",
+ name);
+ return NULL;
+ }
+ size = p->size;
+ if (size < 0)
+ size = -size;
+ return PyMarshal_ReadObjectFromString((char *)p->code, size);
}
static PyObject *
is_frozen_package(char *name)
{
- struct _frozen *p = find_frozen(name);
- int size;
+ struct _frozen *p = find_frozen(name);
+ int size;
- if (p == NULL) {
- PyErr_Format(PyExc_ImportError,
- "No such frozen object named %.200s",
- name);
- return NULL;
- }
+ if (p == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "No such frozen object named %.200s",
+ name);
+ return NULL;
+ }
- size = p->size;
+ size = p->size;
- if (size < 0)
- Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
+ if (size < 0)
+ Py_RETURN_TRUE;
+ else
+ Py_RETURN_FALSE;
}
@@ -1978,67 +1978,67 @@ is_frozen_package(char *name)
int
PyImport_ImportFrozenModule(char *name)
{
- struct _frozen *p = find_frozen(name);
- PyObject *co;
- PyObject *m;
- int ispackage;
- int size;
-
- if (p == NULL)
- return 0;
- if (p->code == NULL) {
- PyErr_Format(PyExc_ImportError,
- "Excluded frozen object named %.200s",
- name);
- return -1;
- }
- size = p->size;
- ispackage = (size < 0);
- if (ispackage)
- size = -size;
- if (Py_VerboseFlag)
- PySys_WriteStderr("import %s # frozen%s\n",
- name, ispackage ? " package" : "");
- co = PyMarshal_ReadObjectFromString((char *)p->code, size);
- if (co == NULL)
- return -1;
- if (!PyCode_Check(co)) {
- PyErr_Format(PyExc_TypeError,
- "frozen object %.200s is not a code object",
- name);
- goto err_return;
- }
- if (ispackage) {
- /* Set __path__ to the package name */
- PyObject *d, *s, *l;
- int err;
- m = PyImport_AddModule(name);
- if (m == NULL)
- goto err_return;
- d = PyModule_GetDict(m);
- s = PyUnicode_InternFromString(name);
- if (s == NULL)
- goto err_return;
- l = PyList_New(1);
- if (l == NULL) {
- Py_DECREF(s);
- goto err_return;
- }
- PyList_SET_ITEM(l, 0, s);
- err = PyDict_SetItemString(d, "__path__", l);
- Py_DECREF(l);
- if (err != 0)
- goto err_return;
- }
- m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
- if (m == NULL)
- goto err_return;
- Py_DECREF(co);
- Py_DECREF(m);
- return 1;
+ struct _frozen *p = find_frozen(name);
+ PyObject *co;
+ PyObject *m;
+ int ispackage;
+ int size;
+
+ if (p == NULL)
+ return 0;
+ if (p->code == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "Excluded frozen object named %.200s",
+ name);
+ return -1;
+ }
+ size = p->size;
+ ispackage = (size < 0);
+ if (ispackage)
+ size = -size;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr("import %s # frozen%s\n",
+ name, ispackage ? " package" : "");
+ co = PyMarshal_ReadObjectFromString((char *)p->code, size);
+ if (co == NULL)
+ return -1;
+ if (!PyCode_Check(co)) {
+ PyErr_Format(PyExc_TypeError,
+ "frozen object %.200s is not a code object",
+ name);
+ goto err_return;
+ }
+ if (ispackage) {
+ /* Set __path__ to the package name */
+ PyObject *d, *s, *l;
+ int err;
+ m = PyImport_AddModule(name);
+ if (m == NULL)
+ goto err_return;
+ d = PyModule_GetDict(m);
+ s = PyUnicode_InternFromString(name);
+ if (s == NULL)
+ goto err_return;
+ l = PyList_New(1);
+ if (l == NULL) {
+ Py_DECREF(s);
+ goto err_return;
+ }
+ PyList_SET_ITEM(l, 0, s);
+ err = PyDict_SetItemString(d, "__path__", l);
+ Py_DECREF(l);
+ if (err != 0)
+ goto err_return;
+ }
+ m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
+ if (m == NULL)
+ goto err_return;
+ Py_DECREF(co);
+ Py_DECREF(m);
+ return 1;
err_return:
- Py_DECREF(co);
- return -1;
+ Py_DECREF(co);
+ return -1;
}
@@ -2048,15 +2048,15 @@ err_return:
PyObject *
PyImport_ImportModule(const char *name)
{
- PyObject *pname;
- PyObject *result;
+ PyObject *pname;
+ PyObject *result;
- pname = PyUnicode_FromString(name);
- if (pname == NULL)
- return NULL;
- result = PyImport_Import(pname);
- Py_DECREF(pname);
- return result;
+ pname = PyUnicode_FromString(name);
+ if (pname == NULL)
+ return NULL;
+ result = PyImport_Import(pname);
+ Py_DECREF(pname);
+ return result;
}
/* Import a module without blocking
@@ -2071,137 +2071,137 @@ PyImport_ImportModule(const char *name)
PyObject *
PyImport_ImportModuleNoBlock(const char *name)
{
- PyObject *result;
- PyObject *modules;
- long me;
-
- /* Try to get the module from sys.modules[name] */
- modules = PyImport_GetModuleDict();
- if (modules == NULL)
- return NULL;
-
- result = PyDict_GetItemString(modules, name);
- if (result != NULL) {
- Py_INCREF(result);
- return result;
- }
- else {
- PyErr_Clear();
- }
+ PyObject *result;
+ PyObject *modules;
+ long me;
+
+ /* Try to get the module from sys.modules[name] */
+ modules = PyImport_GetModuleDict();
+ if (modules == NULL)
+ return NULL;
+
+ result = PyDict_GetItemString(modules, name);
+ if (result != NULL) {
+ Py_INCREF(result);
+ return result;
+ }
+ else {
+ PyErr_Clear();
+ }
#ifdef WITH_THREAD
- /* check the import lock
- * me might be -1 but I ignore the error here, the lock function
- * takes care of the problem */
- me = PyThread_get_thread_ident();
- if (import_lock_thread == -1 || import_lock_thread == me) {
- /* no thread or me is holding the lock */
- return PyImport_ImportModule(name);
- }
- else {
- PyErr_Format(PyExc_ImportError,
- "Failed to import %.200s because the import lock"
- "is held by another thread.",
- name);
- return NULL;
- }
+ /* check the import lock
+ * me might be -1 but I ignore the error here, the lock function
+ * takes care of the problem */
+ me = PyThread_get_thread_ident();
+ if (import_lock_thread == -1 || import_lock_thread == me) {
+ /* no thread or me is holding the lock */
+ return PyImport_ImportModule(name);
+ }
+ else {
+ PyErr_Format(PyExc_ImportError,
+ "Failed to import %.200s because the import lock"
+ "is held by another thread.",
+ name);
+ return NULL;
+ }
#else
- return PyImport_ImportModule(name);
+ return PyImport_ImportModule(name);
#endif
}
/* Forward declarations for helper routines */
static PyObject *get_parent(PyObject *globals, char *buf,
- Py_ssize_t *p_buflen, int level);
+ Py_ssize_t *p_buflen, int level);
static PyObject *load_next(PyObject *mod, PyObject *altmod,
- char **p_name, char *buf, Py_ssize_t *p_buflen);
+ char **p_name, char *buf, Py_ssize_t *p_buflen);
static int mark_miss(char *name);
static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
- char *buf, Py_ssize_t buflen, int recursive);
+ char *buf, Py_ssize_t buflen, int recursive);
static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
/* The Magnum Opus of dotted-name import :-) */
static PyObject *
import_module_level(char *name, PyObject *globals, PyObject *locals,
- PyObject *fromlist, int level)
+ PyObject *fromlist, int level)
{
- char buf[MAXPATHLEN+1];
- Py_ssize_t buflen = 0;
- PyObject *parent, *head, *next, *tail;
+ char buf[MAXPATHLEN+1];
+ Py_ssize_t buflen = 0;
+ PyObject *parent, *head, *next, *tail;
- if (strchr(name, '/') != NULL
+ if (strchr(name, '/') != NULL
#ifdef MS_WINDOWS
- || strchr(name, '\\') != NULL
+ || strchr(name, '\\') != NULL
#endif
- ) {
- PyErr_SetString(PyExc_ImportError,
- "Import by filename is not supported.");
- return NULL;
- }
-
- parent = get_parent(globals, buf, &buflen, level);
- if (parent == NULL)
- return NULL;
-
- head = load_next(parent, Py_None, &name, buf, &buflen);
- if (head == NULL)
- return NULL;
-
- tail = head;
- Py_INCREF(tail);
- while (name) {
- next = load_next(tail, tail, &name, buf, &buflen);
- Py_DECREF(tail);
- if (next == NULL) {
- Py_DECREF(head);
- return NULL;
- }
- tail = next;
- }
- if (tail == Py_None) {
- /* If tail is Py_None, both get_parent and load_next found
- an empty module name: someone called __import__("") or
- doctored faulty bytecode */
- Py_DECREF(tail);
- Py_DECREF(head);
- PyErr_SetString(PyExc_ValueError,
- "Empty module name");
- return NULL;
- }
-
- if (fromlist != NULL) {
- if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
- fromlist = NULL;
- }
-
- if (fromlist == NULL) {
- Py_DECREF(tail);
- return head;
- }
-
- Py_DECREF(head);
- if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
- Py_DECREF(tail);
- return NULL;
- }
-
- return tail;
+ ) {
+ PyErr_SetString(PyExc_ImportError,
+ "Import by filename is not supported.");
+ return NULL;
+ }
+
+ parent = get_parent(globals, buf, &buflen, level);
+ if (parent == NULL)
+ return NULL;
+
+ head = load_next(parent, Py_None, &name, buf, &buflen);
+ if (head == NULL)
+ return NULL;
+
+ tail = head;
+ Py_INCREF(tail);
+ while (name) {
+ next = load_next(tail, tail, &name, buf, &buflen);
+ Py_DECREF(tail);
+ if (next == NULL) {
+ Py_DECREF(head);
+ return NULL;
+ }
+ tail = next;
+ }
+ if (tail == Py_None) {
+ /* If tail is Py_None, both get_parent and load_next found
+ an empty module name: someone called __import__("") or
+ doctored faulty bytecode */
+ Py_DECREF(tail);
+ Py_DECREF(head);
+ PyErr_SetString(PyExc_ValueError,
+ "Empty module name");
+ return NULL;
+ }
+
+ if (fromlist != NULL) {
+ if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
+ fromlist = NULL;
+ }
+
+ if (fromlist == NULL) {
+ Py_DECREF(tail);
+ return head;
+ }
+
+ Py_DECREF(head);
+ if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
+ Py_DECREF(tail);
+ return NULL;
+ }
+
+ return tail;
}
PyObject *
PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
- PyObject *fromlist, int level)
+ PyObject *fromlist, int level)
{
- PyObject *result;
- _PyImport_AcquireLock();
- result = import_module_level(name, globals, locals, fromlist, level);
- if (_PyImport_ReleaseLock() < 0) {
- Py_XDECREF(result);
- PyErr_SetString(PyExc_RuntimeError,
- "not holding the import lock");
- return NULL;
- }
- return result;
+ PyObject *result;
+ _PyImport_AcquireLock();
+ result = import_module_level(name, globals, locals, fromlist, level);
+ if (_PyImport_ReleaseLock() < 0) {
+ Py_XDECREF(result);
+ PyErr_SetString(PyExc_RuntimeError,
+ "not holding the import lock");
+ return NULL;
+ }
+ return result;
}
/* Return the package that an import is being performed in. If globals comes
@@ -2218,420 +2218,420 @@ PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
static PyObject *
get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
{
- static PyObject *namestr = NULL;
- static PyObject *pathstr = NULL;
- static PyObject *pkgstr = NULL;
- PyObject *pkgname, *modname, *modpath, *modules, *parent;
- int orig_level = level;
-
- if (globals == NULL || !PyDict_Check(globals) || !level)
- return Py_None;
-
- if (namestr == NULL) {
- namestr = PyUnicode_InternFromString("__name__");
- if (namestr == NULL)
- return NULL;
- }
- if (pathstr == NULL) {
- pathstr = PyUnicode_InternFromString("__path__");
- if (pathstr == NULL)
- return NULL;
- }
- if (pkgstr == NULL) {
- pkgstr = PyUnicode_InternFromString("__package__");
- if (pkgstr == NULL)
- return NULL;
- }
-
- *buf = '\0';
- *p_buflen = 0;
- pkgname = PyDict_GetItem(globals, pkgstr);
-
- if ((pkgname != NULL) && (pkgname != Py_None)) {
- /* __package__ is set, so use it */
- char *pkgname_str;
- Py_ssize_t len;
-
- if (!PyUnicode_Check(pkgname)) {
- PyErr_SetString(PyExc_ValueError,
- "__package__ set to non-string");
- return NULL;
- }
- pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
- if (len == 0) {
- if (level > 0) {
- PyErr_SetString(PyExc_ValueError,
- "Attempted relative import in non-package");
- return NULL;
- }
- return Py_None;
- }
- if (len > MAXPATHLEN) {
- PyErr_SetString(PyExc_ValueError,
- "Package name too long");
- return NULL;
- }
- strcpy(buf, pkgname_str);
- } else {
- /* __package__ not set, so figure it out and set it */
- modname = PyDict_GetItem(globals, namestr);
- if (modname == NULL || !PyUnicode_Check(modname))
- return Py_None;
-
- modpath = PyDict_GetItem(globals, pathstr);
- if (modpath != NULL) {
- /* __path__ is set, so modname is already the package name */
- char *modname_str;
- Py_ssize_t len;
- int error;
-
- modname_str = _PyUnicode_AsStringAndSize(modname, &len);
- if (len > MAXPATHLEN) {
- PyErr_SetString(PyExc_ValueError,
- "Module name too long");
- return NULL;
- }
- strcpy(buf, modname_str);
- error = PyDict_SetItem(globals, pkgstr, modname);
- if (error) {
- PyErr_SetString(PyExc_ValueError,
- "Could not set __package__");
- return NULL;
- }
- } else {
- /* Normal module, so work out the package name if any */
- char *start = _PyUnicode_AsString(modname);
- char *lastdot = strrchr(start, '.');
- size_t len;
- int error;
- if (lastdot == NULL && level > 0) {
- PyErr_SetString(PyExc_ValueError,
- "Attempted relative import in non-package");
- return NULL;
- }
- if (lastdot == NULL) {
- error = PyDict_SetItem(globals, pkgstr, Py_None);
- if (error) {
- PyErr_SetString(PyExc_ValueError,
- "Could not set __package__");
- return NULL;
- }
- return Py_None;
- }
- len = lastdot - start;
- if (len >= MAXPATHLEN) {
- PyErr_SetString(PyExc_ValueError,
- "Module name too long");
- return NULL;
- }
- strncpy(buf, start, len);
- buf[len] = '\0';
- pkgname = PyUnicode_FromString(buf);
- if (pkgname == NULL) {
- return NULL;
- }
- error = PyDict_SetItem(globals, pkgstr, pkgname);
- Py_DECREF(pkgname);
- if (error) {
- PyErr_SetString(PyExc_ValueError,
- "Could not set __package__");
- return NULL;
- }
- }
- }
- while (--level > 0) {
- char *dot = strrchr(buf, '.');
- if (dot == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "Attempted relative import beyond "
- "toplevel package");
- return NULL;
- }
- *dot = '\0';
- }
- *p_buflen = strlen(buf);
-
- modules = PyImport_GetModuleDict();
- parent = PyDict_GetItemString(modules, buf);
- if (parent == NULL) {
- if (orig_level < 1) {
- PyObject *err_msg = PyBytes_FromFormat(
- "Parent module '%.200s' not found "
- "while handling absolute import", buf);
- if (err_msg == NULL) {
- return NULL;
- }
- if (!PyErr_WarnEx(PyExc_RuntimeWarning,
- PyBytes_AsString(err_msg), 1)) {
- *buf = '\0';
- *p_buflen = 0;
- parent = Py_None;
- }
- Py_DECREF(err_msg);
- } else {
- PyErr_Format(PyExc_SystemError,
- "Parent module '%.200s' not loaded, "
- "cannot perform relative import", buf);
- }
- }
- return parent;
- /* We expect, but can't guarantee, if parent != None, that:
- - parent.__name__ == buf
- - parent.__dict__ is globals
- If this is violated... Who cares? */
+ static PyObject *namestr = NULL;
+ static PyObject *pathstr = NULL;
+ static PyObject *pkgstr = NULL;
+ PyObject *pkgname, *modname, *modpath, *modules, *parent;
+ int orig_level = level;
+
+ if (globals == NULL || !PyDict_Check(globals) || !level)
+ return Py_None;
+
+ if (namestr == NULL) {
+ namestr = PyUnicode_InternFromString("__name__");
+ if (namestr == NULL)
+ return NULL;
+ }
+ if (pathstr == NULL) {
+ pathstr = PyUnicode_InternFromString("__path__");
+ if (pathstr == NULL)
+ return NULL;
+ }
+ if (pkgstr == NULL) {
+ pkgstr = PyUnicode_InternFromString("__package__");
+ if (pkgstr == NULL)
+ return NULL;
+ }
+
+ *buf = '\0';
+ *p_buflen = 0;
+ pkgname = PyDict_GetItem(globals, pkgstr);
+
+ if ((pkgname != NULL) && (pkgname != Py_None)) {
+ /* __package__ is set, so use it */
+ char *pkgname_str;
+ Py_ssize_t len;
+
+ if (!PyUnicode_Check(pkgname)) {
+ PyErr_SetString(PyExc_ValueError,
+ "__package__ set to non-string");
+ return NULL;
+ }
+ pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
+ if (len == 0) {
+ if (level > 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "Attempted relative import in non-package");
+ return NULL;
+ }
+ return Py_None;
+ }
+ if (len > MAXPATHLEN) {
+ PyErr_SetString(PyExc_ValueError,
+ "Package name too long");
+ return NULL;
+ }
+ strcpy(buf, pkgname_str);
+ } else {
+ /* __package__ not set, so figure it out and set it */
+ modname = PyDict_GetItem(globals, namestr);
+ if (modname == NULL || !PyUnicode_Check(modname))
+ return Py_None;
+
+ modpath = PyDict_GetItem(globals, pathstr);
+ if (modpath != NULL) {
+ /* __path__ is set, so modname is already the package name */
+ char *modname_str;
+ Py_ssize_t len;
+ int error;
+
+ modname_str = _PyUnicode_AsStringAndSize(modname, &len);
+ if (len > MAXPATHLEN) {
+ PyErr_SetString(PyExc_ValueError,
+ "Module name too long");
+ return NULL;
+ }
+ strcpy(buf, modname_str);
+ error = PyDict_SetItem(globals, pkgstr, modname);
+ if (error) {
+ PyErr_SetString(PyExc_ValueError,
+ "Could not set __package__");
+ return NULL;
+ }
+ } else {
+ /* Normal module, so work out the package name if any */
+ char *start = _PyUnicode_AsString(modname);
+ char *lastdot = strrchr(start, '.');
+ size_t len;
+ int error;
+ if (lastdot == NULL && level > 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "Attempted relative import in non-package");
+ return NULL;
+ }
+ if (lastdot == NULL) {
+ error = PyDict_SetItem(globals, pkgstr, Py_None);
+ if (error) {
+ PyErr_SetString(PyExc_ValueError,
+ "Could not set __package__");
+ return NULL;
+ }
+ return Py_None;
+ }
+ len = lastdot - start;
+ if (len >= MAXPATHLEN) {
+ PyErr_SetString(PyExc_ValueError,
+ "Module name too long");
+ return NULL;
+ }
+ strncpy(buf, start, len);
+ buf[len] = '\0';
+ pkgname = PyUnicode_FromString(buf);
+ if (pkgname == NULL) {
+ return NULL;
+ }
+ error = PyDict_SetItem(globals, pkgstr, pkgname);
+ Py_DECREF(pkgname);
+ if (error) {
+ PyErr_SetString(PyExc_ValueError,
+ "Could not set __package__");
+ return NULL;
+ }
+ }
+ }
+ while (--level > 0) {
+ char *dot = strrchr(buf, '.');
+ if (dot == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "Attempted relative import beyond "
+ "toplevel package");
+ return NULL;
+ }
+ *dot = '\0';
+ }
+ *p_buflen = strlen(buf);
+
+ modules = PyImport_GetModuleDict();
+ parent = PyDict_GetItemString(modules, buf);
+ if (parent == NULL) {
+ if (orig_level < 1) {
+ PyObject *err_msg = PyBytes_FromFormat(
+ "Parent module '%.200s' not found "
+ "while handling absolute import", buf);
+ if (err_msg == NULL) {
+ return NULL;
+ }
+ if (!PyErr_WarnEx(PyExc_RuntimeWarning,
+ PyBytes_AsString(err_msg), 1)) {
+ *buf = '\0';
+ *p_buflen = 0;
+ parent = Py_None;
+ }
+ Py_DECREF(err_msg);
+ } else {
+ PyErr_Format(PyExc_SystemError,
+ "Parent module '%.200s' not loaded, "
+ "cannot perform relative import", buf);
+ }
+ }
+ return parent;
+ /* We expect, but can't guarantee, if parent != None, that:
+ - parent.__name__ == buf
+ - parent.__dict__ is globals
+ If this is violated... Who cares? */
}
/* altmod is either None or same as mod */
static PyObject *
load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
- Py_ssize_t *p_buflen)
-{
- char *name = *p_name;
- char *dot = strchr(name, '.');
- size_t len;
- char *p;
- PyObject *result;
-
- if (strlen(name) == 0) {
- /* completely empty module name should only happen in
- 'from . import' (or '__import__("")')*/
- Py_INCREF(mod);
- *p_name = NULL;
- return mod;
- }
-
- if (dot == NULL) {
- *p_name = NULL;
- len = strlen(name);
- }
- else {
- *p_name = dot+1;
- len = dot-name;
- }
- if (len == 0) {
- PyErr_SetString(PyExc_ValueError,
- "Empty module name");
- return NULL;
- }
-
- p = buf + *p_buflen;
- if (p != buf)
- *p++ = '.';
- if (p+len-buf >= MAXPATHLEN) {
- PyErr_SetString(PyExc_ValueError,
- "Module name too long");
- return NULL;
- }
- strncpy(p, name, len);
- p[len] = '\0';
- *p_buflen = p+len-buf;
-
- result = import_submodule(mod, p, buf);
- if (result == Py_None && altmod != mod) {
- Py_DECREF(result);
- /* Here, altmod must be None and mod must not be None */
- result = import_submodule(altmod, p, p);
- if (result != NULL && result != Py_None) {
- if (mark_miss(buf) != 0) {
- Py_DECREF(result);
- return NULL;
- }
- strncpy(buf, name, len);
- buf[len] = '\0';
- *p_buflen = len;
- }
- }
- if (result == NULL)
- return NULL;
-
- if (result == Py_None) {
- Py_DECREF(result);
- PyErr_Format(PyExc_ImportError,
- "No module named %.200s", name);
- return NULL;
- }
-
- return result;
+ Py_ssize_t *p_buflen)
+{
+ char *name = *p_name;
+ char *dot = strchr(name, '.');
+ size_t len;
+ char *p;
+ PyObject *result;
+
+ if (strlen(name) == 0) {
+ /* completely empty module name should only happen in
+ 'from . import' (or '__import__("")')*/
+ Py_INCREF(mod);
+ *p_name = NULL;
+ return mod;
+ }
+
+ if (dot == NULL) {
+ *p_name = NULL;
+ len = strlen(name);
+ }
+ else {
+ *p_name = dot+1;
+ len = dot-name;
+ }
+ if (len == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "Empty module name");
+ return NULL;
+ }
+
+ p = buf + *p_buflen;
+ if (p != buf)
+ *p++ = '.';
+ if (p+len-buf >= MAXPATHLEN) {
+ PyErr_SetString(PyExc_ValueError,
+ "Module name too long");
+ return NULL;
+ }
+ strncpy(p, name, len);
+ p[len] = '\0';
+ *p_buflen = p+len-buf;
+
+ result = import_submodule(mod, p, buf);
+ if (result == Py_None && altmod != mod) {
+ Py_DECREF(result);
+ /* Here, altmod must be None and mod must not be None */
+ result = import_submodule(altmod, p, p);
+ if (result != NULL && result != Py_None) {
+ if (mark_miss(buf) != 0) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ strncpy(buf, name, len);
+ buf[len] = '\0';
+ *p_buflen = len;
+ }
+ }
+ if (result == NULL)
+ return NULL;
+
+ if (result == Py_None) {
+ Py_DECREF(result);
+ PyErr_Format(PyExc_ImportError,
+ "No module named %.200s", name);
+ return NULL;
+ }
+
+ return result;
}
static int
mark_miss(char *name)
{
- PyObject *modules = PyImport_GetModuleDict();
- return PyDict_SetItemString(modules, name, Py_None);
+ PyObject *modules = PyImport_GetModuleDict();
+ return PyDict_SetItemString(modules, name, Py_None);
}
static int
ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
- int recursive)
-{
- int i;
-
- if (!PyObject_HasAttrString(mod, "__path__"))
- return 1;
-
- for (i = 0; ; i++) {
- PyObject *item = PySequence_GetItem(fromlist, i);
- int hasit;
- if (item == NULL) {
- if (PyErr_ExceptionMatches(PyExc_IndexError)) {
- PyErr_Clear();
- return 1;
- }
- return 0;
- }
- if (!PyUnicode_Check(item)) {
- PyErr_SetString(PyExc_TypeError,
- "Item in ``from list'' not a string");
- Py_DECREF(item);
- return 0;
- }
- if (PyUnicode_AS_UNICODE(item)[0] == '*') {
- PyObject *all;
- Py_DECREF(item);
- /* See if the package defines __all__ */
- if (recursive)
- continue; /* Avoid endless recursion */
- all = PyObject_GetAttrString(mod, "__all__");
- if (all == NULL)
- PyErr_Clear();
- else {
- int ret = ensure_fromlist(mod, all, buf, buflen, 1);
- Py_DECREF(all);
- if (!ret)
- return 0;
- }
- continue;
- }
- hasit = PyObject_HasAttr(mod, item);
- if (!hasit) {
- PyObject *item8;
- char *subname;
- PyObject *submod;
- char *p;
- if (!Py_FileSystemDefaultEncoding) {
- item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
- PyUnicode_GetSize(item),
- NULL);
- } else {
- item8 = PyUnicode_AsEncodedString(item,
- Py_FileSystemDefaultEncoding, NULL);
- }
- if (!item8) {
- PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
- return 0;
- }
- subname = PyBytes_AS_STRING(item8);
- if (buflen + strlen(subname) >= MAXPATHLEN) {
- PyErr_SetString(PyExc_ValueError,
- "Module name too long");
- Py_DECREF(item);
- return 0;
- }
- p = buf + buflen;
- *p++ = '.';
- strcpy(p, subname);
- submod = import_submodule(mod, subname, buf);
- Py_DECREF(item8);
- Py_XDECREF(submod);
- if (submod == NULL) {
- Py_DECREF(item);
- return 0;
- }
- }
- Py_DECREF(item);
- }
-
- /* NOTREACHED */
+ int recursive)
+{
+ int i;
+
+ if (!PyObject_HasAttrString(mod, "__path__"))
+ return 1;
+
+ for (i = 0; ; i++) {
+ PyObject *item = PySequence_GetItem(fromlist, i);
+ int hasit;
+ if (item == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_IndexError)) {
+ PyErr_Clear();
+ return 1;
+ }
+ return 0;
+ }
+ if (!PyUnicode_Check(item)) {
+ PyErr_SetString(PyExc_TypeError,
+ "Item in ``from list'' not a string");
+ Py_DECREF(item);
+ return 0;
+ }
+ if (PyUnicode_AS_UNICODE(item)[0] == '*') {
+ PyObject *all;
+ Py_DECREF(item);
+ /* See if the package defines __all__ */
+ if (recursive)
+ continue; /* Avoid endless recursion */
+ all = PyObject_GetAttrString(mod, "__all__");
+ if (all == NULL)
+ PyErr_Clear();
+ else {
+ int ret = ensure_fromlist(mod, all, buf, buflen, 1);
+ Py_DECREF(all);
+ if (!ret)
+ return 0;
+ }
+ continue;
+ }
+ hasit = PyObject_HasAttr(mod, item);
+ if (!hasit) {
+ PyObject *item8;
+ char *subname;
+ PyObject *submod;
+ char *p;
+ if (!Py_FileSystemDefaultEncoding) {
+ item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
+ PyUnicode_GetSize(item),
+ NULL);
+ } else {
+ item8 = PyUnicode_AsEncodedString(item,
+ Py_FileSystemDefaultEncoding, NULL);
+ }
+ if (!item8) {
+ PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
+ return 0;
+ }
+ subname = PyBytes_AS_STRING(item8);
+ if (buflen + strlen(subname) >= MAXPATHLEN) {
+ PyErr_SetString(PyExc_ValueError,
+ "Module name too long");
+ Py_DECREF(item);
+ return 0;
+ }
+ p = buf + buflen;
+ *p++ = '.';
+ strcpy(p, subname);
+ submod = import_submodule(mod, subname, buf);
+ Py_DECREF(item8);
+ Py_XDECREF(submod);
+ if (submod == NULL) {
+ Py_DECREF(item);
+ return 0;
+ }
+ }
+ Py_DECREF(item);
+ }
+
+ /* NOTREACHED */
}
static int
add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
- PyObject *modules)
-{
- if (mod == Py_None)
- return 1;
- /* Irrespective of the success of this load, make a
- reference to it in the parent package module. A copy gets
- saved in the modules dictionary under the full name, so get a
- reference from there, if need be. (The exception is when the
- load failed with a SyntaxError -- then there's no trace in
- sys.modules. In that case, of course, do nothing extra.) */
- if (submod == NULL) {
- submod = PyDict_GetItemString(modules, fullname);
- if (submod == NULL)
- return 1;
- }
- if (PyModule_Check(mod)) {
- /* We can't use setattr here since it can give a
- * spurious warning if the submodule name shadows a
- * builtin name */
- PyObject *dict = PyModule_GetDict(mod);
- if (!dict)
- return 0;
- if (PyDict_SetItemString(dict, subname, submod) < 0)
- return 0;
- }
- else {
- if (PyObject_SetAttrString(mod, subname, submod) < 0)
- return 0;
- }
- return 1;
+ PyObject *modules)
+{
+ if (mod == Py_None)
+ return 1;
+ /* Irrespective of the success of this load, make a
+ reference to it in the parent package module. A copy gets
+ saved in the modules dictionary under the full name, so get a
+ reference from there, if need be. (The exception is when the
+ load failed with a SyntaxError -- then there's no trace in
+ sys.modules. In that case, of course, do nothing extra.) */
+ if (submod == NULL) {
+ submod = PyDict_GetItemString(modules, fullname);
+ if (submod == NULL)
+ return 1;
+ }
+ if (PyModule_Check(mod)) {
+ /* We can't use setattr here since it can give a
+ * spurious warning if the submodule name shadows a
+ * builtin name */
+ PyObject *dict = PyModule_GetDict(mod);
+ if (!dict)
+ return 0;
+ if (PyDict_SetItemString(dict, subname, submod) < 0)
+ return 0;
+ }
+ else {
+ if (PyObject_SetAttrString(mod, subname, submod) < 0)
+ return 0;
+ }
+ return 1;
}
static PyObject *
import_submodule(PyObject *mod, char *subname, char *fullname)
{
- PyObject *modules = PyImport_GetModuleDict();
- PyObject *m = NULL;
-
- /* Require:
- if mod == None: subname == fullname
- else: mod.__name__ + "." + subname == fullname
- */
-
- if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
- Py_INCREF(m);
- }
- else {
- PyObject *path, *loader = NULL;
- char buf[MAXPATHLEN+1];
- struct filedescr *fdp;
- FILE *fp = NULL;
-
- if (mod == Py_None)
- path = NULL;
- else {
- path = PyObject_GetAttrString(mod, "__path__");
- if (path == NULL) {
- PyErr_Clear();
- Py_INCREF(Py_None);
- return Py_None;
- }
- }
-
- buf[0] = '\0';
- fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
- &fp, &loader);
- Py_XDECREF(path);
- if (fdp == NULL) {
- if (!PyErr_ExceptionMatches(PyExc_ImportError))
- return NULL;
- PyErr_Clear();
- Py_INCREF(Py_None);
- return Py_None;
- }
- m = load_module(fullname, fp, buf, fdp->type, loader);
- Py_XDECREF(loader);
- if (fp)
- fclose(fp);
- if (!add_submodule(mod, m, fullname, subname, modules)) {
- Py_XDECREF(m);
- m = NULL;
- }
- }
-
- return m;
+ PyObject *modules = PyImport_GetModuleDict();
+ PyObject *m = NULL;
+
+ /* Require:
+ if mod == None: subname == fullname
+ else: mod.__name__ + "." + subname == fullname
+ */
+
+ if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
+ Py_INCREF(m);
+ }
+ else {
+ PyObject *path, *loader = NULL;
+ char buf[MAXPATHLEN+1];
+ struct filedescr *fdp;
+ FILE *fp = NULL;
+
+ if (mod == Py_None)
+ path = NULL;
+ else {
+ path = PyObject_GetAttrString(mod, "__path__");
+ if (path == NULL) {
+ PyErr_Clear();
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ }
+
+ buf[0] = '\0';
+ fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
+ &fp, &loader);
+ Py_XDECREF(path);
+ if (fdp == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_ImportError))
+ return NULL;
+ PyErr_Clear();
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ m = load_module(fullname, fp, buf, fdp->type, loader);
+ Py_XDECREF(loader);
+ if (fp)
+ fclose(fp);
+ if (!add_submodule(mod, m, fullname, subname, modules)) {
+ Py_XDECREF(m);
+ m = NULL;
+ }
+ }
+
+ return m;
}
@@ -2641,96 +2641,96 @@ import_submodule(PyObject *mod, char *subname, char *fullname)
PyObject *
PyImport_ReloadModule(PyObject *m)
{
- PyInterpreterState *interp = PyThreadState_Get()->interp;
- PyObject *modules_reloading = interp->modules_reloading;
- PyObject *modules = PyImport_GetModuleDict();
- PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
- char *name, *subname;
- char buf[MAXPATHLEN+1];
- struct filedescr *fdp;
- FILE *fp = NULL;
- PyObject *newm;
-
- if (modules_reloading == NULL) {
- Py_FatalError("PyImport_ReloadModule: "
- "no modules_reloading dictionary!");
- return NULL;
- }
-
- if (m == NULL || !PyModule_Check(m)) {
- PyErr_SetString(PyExc_TypeError,
- "reload() argument must be module");
- return NULL;
- }
- name = (char*)PyModule_GetName(m);
- if (name == NULL)
- return NULL;
- if (m != PyDict_GetItemString(modules, name)) {
- PyErr_Format(PyExc_ImportError,
- "reload(): module %.200s not in sys.modules",
- name);
- return NULL;
- }
- existing_m = PyDict_GetItemString(modules_reloading, name);
- if (existing_m != NULL) {
- /* Due to a recursive reload, this module is already
- being reloaded. */
- Py_INCREF(existing_m);
- return existing_m;
- }
- if (PyDict_SetItemString(modules_reloading, name, m) < 0)
- return NULL;
-
- subname = strrchr(name, '.');
- if (subname == NULL)
- subname = name;
- else {
- PyObject *parentname, *parent;
- parentname = PyUnicode_FromStringAndSize(name, (subname-name));
- if (parentname == NULL) {
- imp_modules_reloading_clear();
- return NULL;
- }
- parent = PyDict_GetItem(modules, parentname);
- if (parent == NULL) {
- PyErr_Format(PyExc_ImportError,
- "reload(): parent %U not in sys.modules",
- parentname);
- Py_DECREF(parentname);
- imp_modules_reloading_clear();
- return NULL;
- }
- Py_DECREF(parentname);
- subname++;
- path = PyObject_GetAttrString(parent, "__path__");
- if (path == NULL)
- PyErr_Clear();
- }
- buf[0] = '\0';
- fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
- Py_XDECREF(path);
-
- if (fdp == NULL) {
- Py_XDECREF(loader);
- imp_modules_reloading_clear();
- return NULL;
- }
-
- newm = load_module(name, fp, buf, fdp->type, loader);
- Py_XDECREF(loader);
-
- if (fp)
- fclose(fp);
- if (newm == NULL) {
- /* load_module probably removed name from modules because of
- * the error. Put back the original module object. We're
- * going to return NULL in this case regardless of whether
- * replacing name succeeds, so the return value is ignored.
- */
- PyDict_SetItemString(modules, name, m);
- }
- imp_modules_reloading_clear();
- return newm;
+ PyInterpreterState *interp = PyThreadState_Get()->interp;
+ PyObject *modules_reloading = interp->modules_reloading;
+ PyObject *modules = PyImport_GetModuleDict();
+ PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
+ char *name, *subname;
+ char buf[MAXPATHLEN+1];
+ struct filedescr *fdp;
+ FILE *fp = NULL;
+ PyObject *newm;
+
+ if (modules_reloading == NULL) {
+ Py_FatalError("PyImport_ReloadModule: "
+ "no modules_reloading dictionary!");
+ return NULL;
+ }
+
+ if (m == NULL || !PyModule_Check(m)) {
+ PyErr_SetString(PyExc_TypeError,
+ "reload() argument must be module");
+ return NULL;
+ }
+ name = (char*)PyModule_GetName(m);
+ if (name == NULL)
+ return NULL;
+ if (m != PyDict_GetItemString(modules, name)) {
+ PyErr_Format(PyExc_ImportError,
+ "reload(): module %.200s not in sys.modules",
+ name);
+ return NULL;
+ }
+ existing_m = PyDict_GetItemString(modules_reloading, name);
+ if (existing_m != NULL) {
+ /* Due to a recursive reload, this module is already
+ being reloaded. */
+ Py_INCREF(existing_m);
+ return existing_m;
+ }
+ if (PyDict_SetItemString(modules_reloading, name, m) < 0)
+ return NULL;
+
+ subname = strrchr(name, '.');
+ if (subname == NULL)
+ subname = name;
+ else {
+ PyObject *parentname, *parent;
+ parentname = PyUnicode_FromStringAndSize(name, (subname-name));
+ if (parentname == NULL) {
+ imp_modules_reloading_clear();
+ return NULL;
+ }
+ parent = PyDict_GetItem(modules, parentname);
+ if (parent == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "reload(): parent %U not in sys.modules",
+ parentname);
+ Py_DECREF(parentname);
+ imp_modules_reloading_clear();
+ return NULL;
+ }
+ Py_DECREF(parentname);
+ subname++;
+ path = PyObject_GetAttrString(parent, "__path__");
+ if (path == NULL)
+ PyErr_Clear();
+ }
+ buf[0] = '\0';
+ fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
+ Py_XDECREF(path);
+
+ if (fdp == NULL) {
+ Py_XDECREF(loader);
+ imp_modules_reloading_clear();
+ return NULL;
+ }
+
+ newm = load_module(name, fp, buf, fdp->type, loader);
+ Py_XDECREF(loader);
+
+ if (fp)
+ fclose(fp);
+ if (newm == NULL) {
+ /* load_module probably removed name from modules because of
+ * the error. Put back the original module object. We're
+ * going to return NULL in this case regardless of whether
+ * replacing name succeeds, so the return value is ignored.
+ */
+ PyDict_SetItemString(modules, name, m);
+ }
+ imp_modules_reloading_clear();
+ return newm;
}
@@ -2746,68 +2746,68 @@ PyImport_ReloadModule(PyObject *m)
PyObject *
PyImport_Import(PyObject *module_name)
{
- static PyObject *silly_list = NULL;
- static PyObject *builtins_str = NULL;
- static PyObject *import_str = NULL;
- PyObject *globals = NULL;
- PyObject *import = NULL;
- PyObject *builtins = NULL;
- PyObject *r = NULL;
-
- /* Initialize constant string objects */
- if (silly_list == NULL) {
- import_str = PyUnicode_InternFromString("__import__");
- if (import_str == NULL)
- return NULL;
- builtins_str = PyUnicode_InternFromString("__builtins__");
- if (builtins_str == NULL)
- return NULL;
- silly_list = Py_BuildValue("[s]", "__doc__");
- if (silly_list == NULL)
- return NULL;
- }
-
- /* Get the builtins from current globals */
- globals = PyEval_GetGlobals();
- if (globals != NULL) {
- Py_INCREF(globals);
- builtins = PyObject_GetItem(globals, builtins_str);
- if (builtins == NULL)
- goto err;
- }
- else {
- /* No globals -- use standard builtins, and fake globals */
- builtins = PyImport_ImportModuleLevel("builtins",
- NULL, NULL, NULL, 0);
- if (builtins == NULL)
- return NULL;
- globals = Py_BuildValue("{OO}", builtins_str, builtins);
- if (globals == NULL)
- goto err;
- }
-
- /* Get the __import__ function from the builtins */
- if (PyDict_Check(builtins)) {
- import = PyObject_GetItem(builtins, import_str);
- if (import == NULL)
- PyErr_SetObject(PyExc_KeyError, import_str);
- }
- else
- import = PyObject_GetAttr(builtins, import_str);
- if (import == NULL)
- goto err;
-
- /* Call the __import__ function with the proper argument list
- * Always use absolute import here. */
- r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
- globals, silly_list, 0, NULL);
+ static PyObject *silly_list = NULL;
+ static PyObject *builtins_str = NULL;
+ static PyObject *import_str = NULL;
+ PyObject *globals = NULL;
+ PyObject *import = NULL;
+ PyObject *builtins = NULL;
+ PyObject *r = NULL;
+
+ /* Initialize constant string objects */
+ if (silly_list == NULL) {
+ import_str = PyUnicode_InternFromString("__import__");
+ if (import_str == NULL)
+ return NULL;
+ builtins_str = PyUnicode_InternFromString("__builtins__");
+ if (builtins_str == NULL)
+ return NULL;
+ silly_list = Py_BuildValue("[s]", "__doc__");
+ if (silly_list == NULL)
+ return NULL;
+ }
+
+ /* Get the builtins from current globals */
+ globals = PyEval_GetGlobals();
+ if (globals != NULL) {
+ Py_INCREF(globals);
+ builtins = PyObject_GetItem(globals, builtins_str);
+ if (builtins == NULL)
+ goto err;
+ }
+ else {
+ /* No globals -- use standard builtins, and fake globals */
+ builtins = PyImport_ImportModuleLevel("builtins",
+ NULL, NULL, NULL, 0);
+ if (builtins == NULL)
+ return NULL;
+ globals = Py_BuildValue("{OO}", builtins_str, builtins);
+ if (globals == NULL)
+ goto err;
+ }
+
+ /* Get the __import__ function from the builtins */
+ if (PyDict_Check(builtins)) {
+ import = PyObject_GetItem(builtins, import_str);
+ if (import == NULL)
+ PyErr_SetObject(PyExc_KeyError, import_str);
+ }
+ else
+ import = PyObject_GetAttr(builtins, import_str);
+ if (import == NULL)
+ goto err;
+
+ /* Call the __import__ function with the proper argument list
+ * Always use absolute import here. */
+ r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
+ globals, silly_list, 0, NULL);
err:
- Py_XDECREF(globals);
- Py_XDECREF(builtins);
- Py_XDECREF(import);
+ Py_XDECREF(globals);
+ Py_XDECREF(builtins);
+ Py_XDECREF(import);
- return r;
+ return r;
}
@@ -2818,245 +2818,245 @@ PyImport_Import(PyObject *module_name)
static PyObject *
imp_get_magic(PyObject *self, PyObject *noargs)
{
- char buf[4];
+ char buf[4];
- buf[0] = (char) ((pyc_magic >> 0) & 0xff);
- buf[1] = (char) ((pyc_magic >> 8) & 0xff);
- buf[2] = (char) ((pyc_magic >> 16) & 0xff);
- buf[3] = (char) ((pyc_magic >> 24) & 0xff);
+ buf[0] = (char) ((pyc_magic >> 0) & 0xff);
+ buf[1] = (char) ((pyc_magic >> 8) & 0xff);
+ buf[2] = (char) ((pyc_magic >> 16) & 0xff);
+ buf[3] = (char) ((pyc_magic >> 24) & 0xff);
- return PyBytes_FromStringAndSize(buf, 4);
+ return PyBytes_FromStringAndSize(buf, 4);
}
static PyObject *
imp_get_suffixes(PyObject *self, PyObject *noargs)
{
- PyObject *list;
- struct filedescr *fdp;
-
- list = PyList_New(0);
- if (list == NULL)
- return NULL;
- for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
- PyObject *item = Py_BuildValue("ssi",
- fdp->suffix, fdp->mode, fdp->type);
- if (item == NULL) {
- Py_DECREF(list);
- return NULL;
- }
- if (PyList_Append(list, item) < 0) {
- Py_DECREF(list);
- Py_DECREF(item);
- return NULL;
- }
- Py_DECREF(item);
- }
- return list;
+ PyObject *list;
+ struct filedescr *fdp;
+
+ list = PyList_New(0);
+ if (list == NULL)
+ return NULL;
+ for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
+ PyObject *item = Py_BuildValue("ssi",
+ fdp->suffix, fdp->mode, fdp->type);
+ if (item == NULL) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ if (PyList_Append(list, item) < 0) {
+ Py_DECREF(list);
+ Py_DECREF(item);
+ return NULL;
+ }
+ Py_DECREF(item);
+ }
+ return list;
}
static PyObject *
call_find_module(char *name, PyObject *path)
{
- extern int fclose(FILE *);
- PyObject *fob, *ret;
- PyObject *pathobj;
- struct filedescr *fdp;
- char pathname[MAXPATHLEN+1];
- FILE *fp = NULL;
- int fd = -1;
- char *found_encoding = NULL;
- char *encoding = NULL;
-
- pathname[0] = '\0';
- if (path == Py_None)
- path = NULL;
- fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
- if (fdp == NULL)
- return NULL;
- if (fp != NULL) {
- fd = fileno(fp);
- if (fd != -1)
- fd = dup(fd);
- fclose(fp);
- fp = NULL;
- }
- if (fd != -1) {
- if (strchr(fdp->mode, 'b') == NULL) {
- /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
- memory. */
- found_encoding = PyTokenizer_FindEncoding(fd);
- lseek(fd, 0, 0); /* Reset position */
- if (found_encoding == NULL && PyErr_Occurred())
- return NULL;
- encoding = (found_encoding != NULL) ? found_encoding :
- (char*)PyUnicode_GetDefaultEncoding();
- }
- fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
- (char*)encoding, NULL, NULL, 1);
- if (fob == NULL) {
- close(fd);
- PyMem_FREE(found_encoding);
- return NULL;
- }
- }
- else {
- fob = Py_None;
- Py_INCREF(fob);
- }
- pathobj = PyUnicode_DecodeFSDefault(pathname);
- ret = Py_BuildValue("NN(ssi)",
- fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
- PyMem_FREE(found_encoding);
-
- return ret;
+ extern int fclose(FILE *);
+ PyObject *fob, *ret;
+ PyObject *pathobj;
+ struct filedescr *fdp;
+ char pathname[MAXPATHLEN+1];
+ FILE *fp = NULL;
+ int fd = -1;
+ char *found_encoding = NULL;
+ char *encoding = NULL;
+
+ pathname[0] = '\0';
+ if (path == Py_None)
+ path = NULL;
+ fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
+ if (fdp == NULL)
+ return NULL;
+ if (fp != NULL) {
+ fd = fileno(fp);
+ if (fd != -1)
+ fd = dup(fd);
+ fclose(fp);
+ fp = NULL;
+ }
+ if (fd != -1) {
+ if (strchr(fdp->mode, 'b') == NULL) {
+ /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
+ memory. */
+ found_encoding = PyTokenizer_FindEncoding(fd);
+ lseek(fd, 0, 0); /* Reset position */
+ if (found_encoding == NULL && PyErr_Occurred())
+ return NULL;
+ encoding = (found_encoding != NULL) ? found_encoding :
+ (char*)PyUnicode_GetDefaultEncoding();
+ }
+ fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
+ (char*)encoding, NULL, NULL, 1);
+ if (fob == NULL) {
+ close(fd);
+ PyMem_FREE(found_encoding);
+ return NULL;
+ }
+ }
+ else {
+ fob = Py_None;
+ Py_INCREF(fob);
+ }
+ pathobj = PyUnicode_DecodeFSDefault(pathname);
+ ret = Py_BuildValue("NN(ssi)",
+ fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
+ PyMem_FREE(found_encoding);
+
+ return ret;
}
static PyObject *
imp_find_module(PyObject *self, PyObject *args)
{
- char *name;
- PyObject *ret, *path = NULL;
- if (!PyArg_ParseTuple(args, "es|O:find_module",
- Py_FileSystemDefaultEncoding, &name,
- &path))
- return NULL;
- ret = call_find_module(name, path);
- PyMem_Free(name);
- return ret;
+ char *name;
+ PyObject *ret, *path = NULL;
+ if (!PyArg_ParseTuple(args, "es|O:find_module",
+ Py_FileSystemDefaultEncoding, &name,
+ &path))
+ return NULL;
+ ret = call_find_module(name, path);
+ PyMem_Free(name);
+ return ret;
}
static PyObject *
imp_init_builtin(PyObject *self, PyObject *args)
{
- char *name;
- int ret;
- PyObject *m;
- if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
- return NULL;
- ret = init_builtin(name);
- if (ret < 0)
- return NULL;
- if (ret == 0) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- m = PyImport_AddModule(name);
- Py_XINCREF(m);
- return m;
+ char *name;
+ int ret;
+ PyObject *m;
+ if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
+ return NULL;
+ ret = init_builtin(name);
+ if (ret < 0)
+ return NULL;
+ if (ret == 0) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ m = PyImport_AddModule(name);
+ Py_XINCREF(m);
+ return m;
}
static PyObject *
imp_init_frozen(PyObject *self, PyObject *args)
{
- char *name;
- int ret;
- PyObject *m;
- if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
- return NULL;
- ret = PyImport_ImportFrozenModule(name);
- if (ret < 0)
- return NULL;
- if (ret == 0) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- m = PyImport_AddModule(name);
- Py_XINCREF(m);
- return m;
+ char *name;
+ int ret;
+ PyObject *m;
+ if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
+ return NULL;
+ ret = PyImport_ImportFrozenModule(name);
+ if (ret < 0)
+ return NULL;
+ if (ret == 0) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ m = PyImport_AddModule(name);
+ Py_XINCREF(m);
+ return m;
}
static PyObject *
imp_get_frozen_object(PyObject *self, PyObject *args)
{
- char *name;
+ char *name;
- if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
- return NULL;
- return get_frozen_object(name);
+ if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
+ return NULL;
+ return get_frozen_object(name);
}
static PyObject *
imp_is_frozen_package(PyObject *self, PyObject *args)
{
- char *name;
+ char *name;
- if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
- return NULL;
- return is_frozen_package(name);
+ if (!PyArg_ParseTuple(args, "s:is_frozen_package", &name))
+ return NULL;
+ return is_frozen_package(name);
}
static PyObject *
imp_is_builtin(PyObject *self, PyObject *args)
{
- char *name;
- if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
- return NULL;
- return PyLong_FromLong(is_builtin(name));
+ char *name;
+ if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
+ return NULL;
+ return PyLong_FromLong(is_builtin(name));
}
static PyObject *
imp_is_frozen(PyObject *self, PyObject *args)
{
- char *name;
- struct _frozen *p;
- if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
- return NULL;
- p = find_frozen(name);
- return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
+ char *name;
+ struct _frozen *p;
+ if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
+ return NULL;
+ p = find_frozen(name);
+ return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
}
static FILE *
get_file(char *pathname, PyObject *fob, char *mode)
{
- FILE *fp;
- if (mode[0] == 'U')
- mode = "r" PY_STDIOTEXTMODE;
- if (fob == NULL) {
- fp = fopen(pathname, mode);
- }
- else {
- int fd = PyObject_AsFileDescriptor(fob);
- if (fd == -1)
- return NULL;
- if (!_PyVerify_fd(fd))
- goto error;
- /* the FILE struct gets a new fd, so that it can be closed
- * independently of the file descriptor given
- */
- fd = dup(fd);
- if (fd == -1)
- goto error;
- fp = fdopen(fd, mode);
- }
- if (fp)
- return fp;
+ FILE *fp;
+ if (mode[0] == 'U')
+ mode = "r" PY_STDIOTEXTMODE;
+ if (fob == NULL) {
+ fp = fopen(pathname, mode);
+ }
+ else {
+ int fd = PyObject_AsFileDescriptor(fob);
+ if (fd == -1)
+ return NULL;
+ if (!_PyVerify_fd(fd))
+ goto error;
+ /* the FILE struct gets a new fd, so that it can be closed
+ * independently of the file descriptor given
+ */
+ fd = dup(fd);
+ if (fd == -1)
+ goto error;
+ fp = fdopen(fd, mode);
+ }
+ if (fp)
+ return fp;
error:
- PyErr_SetFromErrno(PyExc_IOError);
- return NULL;
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
}
static PyObject *
imp_load_compiled(PyObject *self, PyObject *args)
{
- char *name;
- char *pathname;
- PyObject *fob = NULL;
- PyObject *m;
- FILE *fp;
- if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
- &name,
- Py_FileSystemDefaultEncoding, &pathname,
- &fob))
- return NULL;
- fp = get_file(pathname, fob, "rb");
- if (fp == NULL) {
- PyMem_Free(pathname);
- return NULL;
- }
- m = load_compiled_module(name, pathname, fp);
- fclose(fp);
- PyMem_Free(pathname);
- return m;
+ char *name;
+ char *pathname;
+ PyObject *fob = NULL;
+ PyObject *m;
+ FILE *fp;
+ if (!PyArg_ParseTuple(args, "ses|O:load_compiled",
+ &name,
+ Py_FileSystemDefaultEncoding, &pathname,
+ &fob))
+ return NULL;
+ fp = get_file(pathname, fob, "rb");
+ if (fp == NULL) {
+ PyMem_Free(pathname);
+ return NULL;
+ }
+ m = load_compiled_module(name, pathname, fp);
+ fclose(fp);
+ PyMem_Free(pathname);
+ return m;
}
#ifdef HAVE_DYNAMIC_LOADING
@@ -3064,28 +3064,28 @@ imp_load_compiled(PyObject *self, PyObject *args)
static PyObject *
imp_load_dynamic(PyObject *self, PyObject *args)
{
- char *name;
- char *pathname;
- PyObject *fob = NULL;
- PyObject *m;
- FILE *fp = NULL;
- if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
- &name,
- Py_FileSystemDefaultEncoding, &pathname,
- &fob))
- return NULL;
- if (fob) {
- fp = get_file(pathname, fob, "r");
- if (fp == NULL) {
- PyMem_Free(pathname);
- return NULL;
- }
- }
- m = _PyImport_LoadDynamicModule(name, pathname, fp);
- PyMem_Free(pathname);
- if (fp)
- fclose(fp);
- return m;
+ char *name;
+ char *pathname;
+ PyObject *fob = NULL;
+ PyObject *m;
+ FILE *fp = NULL;
+ if (!PyArg_ParseTuple(args, "ses|O:load_dynamic",
+ &name,
+ Py_FileSystemDefaultEncoding, &pathname,
+ &fob))
+ return NULL;
+ if (fob) {
+ fp = get_file(pathname, fob, "r");
+ if (fp == NULL) {
+ PyMem_Free(pathname);
+ return NULL;
+ }
+ }
+ m = _PyImport_LoadDynamicModule(name, pathname, fp);
+ PyMem_Free(pathname);
+ if (fp)
+ fclose(fp);
+ return m;
}
#endif /* HAVE_DYNAMIC_LOADING */
@@ -3093,99 +3093,99 @@ imp_load_dynamic(PyObject *self, PyObject *args)
static PyObject *
imp_load_source(PyObject *self, PyObject *args)
{
- char *name;
- char *pathname;
- PyObject *fob = NULL;
- PyObject *m;
- FILE *fp;
- if (!PyArg_ParseTuple(args, "ses|O:load_source",
- &name,
- Py_FileSystemDefaultEncoding, &pathname,
- &fob))
- return NULL;
- fp = get_file(pathname, fob, "r");
- if (fp == NULL) {
- PyMem_Free(pathname);
- return NULL;
- }
- m = load_source_module(name, pathname, fp);
- PyMem_Free(pathname);
- fclose(fp);
- return m;
+ char *name;
+ char *pathname;
+ PyObject *fob = NULL;
+ PyObject *m;
+ FILE *fp;
+ if (!PyArg_ParseTuple(args, "ses|O:load_source",
+ &name,
+ Py_FileSystemDefaultEncoding, &pathname,
+ &fob))
+ return NULL;
+ fp = get_file(pathname, fob, "r");
+ if (fp == NULL) {
+ PyMem_Free(pathname);
+ return NULL;
+ }
+ m = load_source_module(name, pathname, fp);
+ PyMem_Free(pathname);
+ fclose(fp);
+ return m;
}
static PyObject *
imp_load_module(PyObject *self, PyObject *args)
{
- char *name;
- PyObject *fob;
- char *pathname;
- PyObject * ret;
- char *suffix; /* Unused */
- char *mode;
- int type;
- FILE *fp;
-
- if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
- &name, &fob,
- Py_FileSystemDefaultEncoding, &pathname,
- &suffix, &mode, &type))
- return NULL;
- if (*mode) {
- /* Mode must start with 'r' or 'U' and must not contain '+'.
- Implicit in this test is the assumption that the mode
- may contain other modifiers like 'b' or 't'. */
-
- if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
- PyErr_Format(PyExc_ValueError,
- "invalid file open mode %.200s", mode);
- PyMem_Free(pathname);
- return NULL;
- }
- }
- if (fob == Py_None)
- fp = NULL;
- else {
- fp = get_file(NULL, fob, mode);
- if (fp == NULL) {
- PyMem_Free(pathname);
- return NULL;
- }
- }
- ret = load_module(name, fp, pathname, type, NULL);
- PyMem_Free(pathname);
- if (fp)
- fclose(fp);
- return ret;
+ char *name;
+ PyObject *fob;
+ char *pathname;
+ PyObject * ret;
+ char *suffix; /* Unused */
+ char *mode;
+ int type;
+ FILE *fp;
+
+ if (!PyArg_ParseTuple(args, "sOes(ssi):load_module",
+ &name, &fob,
+ Py_FileSystemDefaultEncoding, &pathname,
+ &suffix, &mode, &type))
+ return NULL;
+ if (*mode) {
+ /* Mode must start with 'r' or 'U' and must not contain '+'.
+ Implicit in this test is the assumption that the mode
+ may contain other modifiers like 'b' or 't'. */
+
+ if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
+ PyErr_Format(PyExc_ValueError,
+ "invalid file open mode %.200s", mode);
+ PyMem_Free(pathname);
+ return NULL;
+ }
+ }
+ if (fob == Py_None)
+ fp = NULL;
+ else {
+ fp = get_file(NULL, fob, mode);
+ if (fp == NULL) {
+ PyMem_Free(pathname);
+ return NULL;
+ }
+ }
+ ret = load_module(name, fp, pathname, type, NULL);
+ PyMem_Free(pathname);
+ if (fp)
+ fclose(fp);
+ return ret;
}
static PyObject *
imp_load_package(PyObject *self, PyObject *args)
{
- char *name;
- char *pathname;
- PyObject * ret;
- if (!PyArg_ParseTuple(args, "ses:load_package",
- &name, Py_FileSystemDefaultEncoding, &pathname))
- return NULL;
- ret = load_package(name, pathname);
- PyMem_Free(pathname);
- return ret;
+ char *name;
+ char *pathname;
+ PyObject * ret;
+ if (!PyArg_ParseTuple(args, "ses:load_package",
+ &name, Py_FileSystemDefaultEncoding, &pathname))
+ return NULL;
+ ret = load_package(name, pathname);
+ PyMem_Free(pathname);
+ return ret;
}
static PyObject *
imp_new_module(PyObject *self, PyObject *args)
{
- char *name;
- if (!PyArg_ParseTuple(args, "s:new_module", &name))
- return NULL;
- return PyModule_New(name);
+ char *name;
+ if (!PyArg_ParseTuple(args, "s:new_module", &name))
+ return NULL;
+ return PyModule_New(name);
}
static PyObject *
imp_reload(PyObject *self, PyObject *v)
{
- return PyImport_ReloadModule(v);
+ return PyImport_ReloadModule(v);
}
PyDoc_STRVAR(doc_reload,
@@ -3243,41 +3243,41 @@ Release the interpreter's import lock.\n\
On platforms without threads, this function does nothing.");
static PyMethodDef imp_methods[] = {
- {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
- {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
- {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
- {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
- {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
- {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
- {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
- {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
- {"reload", imp_reload, METH_O, doc_reload},
- /* The rest are obsolete */
- {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
- {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
- {"init_builtin", imp_init_builtin, METH_VARARGS},
- {"init_frozen", imp_init_frozen, METH_VARARGS},
- {"is_builtin", imp_is_builtin, METH_VARARGS},
- {"is_frozen", imp_is_frozen, METH_VARARGS},
- {"load_compiled", imp_load_compiled, METH_VARARGS},
+ {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
+ {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
+ {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
+ {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
+ {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
+ {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
+ {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
+ {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
+ {"reload", imp_reload, METH_O, doc_reload},
+ /* The rest are obsolete */
+ {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
+ {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
+ {"init_builtin", imp_init_builtin, METH_VARARGS},
+ {"init_frozen", imp_init_frozen, METH_VARARGS},
+ {"is_builtin", imp_is_builtin, METH_VARARGS},
+ {"is_frozen", imp_is_frozen, METH_VARARGS},
+ {"load_compiled", imp_load_compiled, METH_VARARGS},
#ifdef HAVE_DYNAMIC_LOADING
- {"load_dynamic", imp_load_dynamic, METH_VARARGS},
+ {"load_dynamic", imp_load_dynamic, METH_VARARGS},
#endif
- {"load_package", imp_load_package, METH_VARARGS},
- {"load_source", imp_load_source, METH_VARARGS},
- {NULL, NULL} /* sentinel */
+ {"load_package", imp_load_package, METH_VARARGS},
+ {"load_source", imp_load_source, METH_VARARGS},
+ {NULL, NULL} /* sentinel */
};
static int
setint(PyObject *d, char *name, int value)
{
- PyObject *v;
- int err;
+ PyObject *v;
+ int err;
- v = PyLong_FromLong((long)value);
- err = PyDict_SetItemString(d, name, v);
- Py_XDECREF(v);
- return err;
+ v = PyLong_FromLong((long)value);
+ err = PyDict_SetItemString(d, name, v);
+ Py_XDECREF(v);
+ return err;
}
typedef struct {
@@ -3287,158 +3287,158 @@ typedef struct {
static int
NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
{
- char *path;
- Py_ssize_t pathlen;
+ char *path;
+ Py_ssize_t pathlen;
- if (!_PyArg_NoKeywords("NullImporter()", kwds))
- return -1;
+ if (!_PyArg_NoKeywords("NullImporter()", kwds))
+ return -1;
- if (!PyArg_ParseTuple(args, "es:NullImporter",
- Py_FileSystemDefaultEncoding, &path))
- return -1;
+ if (!PyArg_ParseTuple(args, "es:NullImporter",
+ Py_FileSystemDefaultEncoding, &path))
+ return -1;
- pathlen = strlen(path);
- if (pathlen == 0) {
- PyMem_Free(path);
- PyErr_SetString(PyExc_ImportError, "empty pathname");
- return -1;
- } else {
+ pathlen = strlen(path);
+ if (pathlen == 0) {
+ PyMem_Free(path);
+ PyErr_SetString(PyExc_ImportError, "empty pathname");
+ return -1;
+ } else {
#ifndef MS_WINDOWS
- struct stat statbuf;
- int rv;
-
- rv = stat(path, &statbuf);
- PyMem_Free(path);
- if (rv == 0) {
- /* it exists */
- if (S_ISDIR(statbuf.st_mode)) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError,
- "existing directory");
- return -1;
- }
- }
+ struct stat statbuf;
+ int rv;
+
+ rv = stat(path, &statbuf);
+ PyMem_Free(path);
+ if (rv == 0) {
+ /* it exists */
+ if (S_ISDIR(statbuf.st_mode)) {
+ /* it's a directory */
+ PyErr_SetString(PyExc_ImportError,
+ "existing directory");
+ return -1;
+ }
+ }
#else /* MS_WINDOWS */
- DWORD rv;
- /* see issue1293 and issue3677:
- * stat() on Windows doesn't recognise paths like
- * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
- */
- rv = GetFileAttributesA(path);
- PyMem_Free(path);
- if (rv != INVALID_FILE_ATTRIBUTES) {
- /* it exists */
- if (rv & FILE_ATTRIBUTE_DIRECTORY) {
- /* it's a directory */
- PyErr_SetString(PyExc_ImportError,
- "existing directory");
- return -1;
- }
- }
+ DWORD rv;
+ /* see issue1293 and issue3677:
+ * stat() on Windows doesn't recognise paths like
+ * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
+ */
+ rv = GetFileAttributesA(path);
+ PyMem_Free(path);
+ if (rv != INVALID_FILE_ATTRIBUTES) {
+ /* it exists */
+ if (rv & FILE_ATTRIBUTE_DIRECTORY) {
+ /* it's a directory */
+ PyErr_SetString(PyExc_ImportError,
+ "existing directory");
+ return -1;
+ }
+ }
#endif
- }
- return 0;
+ }
+ return 0;
}
static PyObject *
NullImporter_find_module(NullImporter *self, PyObject *args)
{
- Py_RETURN_NONE;
+ Py_RETURN_NONE;
}
static PyMethodDef NullImporter_methods[] = {
- {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
- "Always return None"
- },
- {NULL} /* Sentinel */
+ {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
+ "Always return None"
+ },
+ {NULL} /* Sentinel */
};
PyTypeObject PyNullImporter_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- "imp.NullImporter", /*tp_name*/
- sizeof(NullImporter), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_reserved*/
- 0, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- 0, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
- "Null importer object", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- NullImporter_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)NullImporter_init, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew /* tp_new */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "imp.NullImporter", /*tp_name*/
+ sizeof(NullImporter), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ 0, /*tp_dealloc*/
+ 0, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_reserved*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash */
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ "Null importer object", /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ NullImporter_methods, /* tp_methods */
+ 0, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)NullImporter_init, /* tp_init */
+ 0, /* tp_alloc */
+ PyType_GenericNew /* tp_new */
};
static struct PyModuleDef impmodule = {
- PyModuleDef_HEAD_INIT,
- "imp",
- doc_imp,
- 0,
- imp_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "imp",
+ doc_imp,
+ 0,
+ imp_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyInit_imp(void)
{
- PyObject *m, *d;
-
- if (PyType_Ready(&PyNullImporter_Type) < 0)
- return NULL;
-
- m = PyModule_Create(&impmodule);
- if (m == NULL)
- goto failure;
- d = PyModule_GetDict(m);
- if (d == NULL)
- goto failure;
-
- if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
- if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
- if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
- if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
- if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
- if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
- if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
- if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
- if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
- if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
-
- Py_INCREF(&PyNullImporter_Type);
- PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
- return m;
+ PyObject *m, *d;
+
+ if (PyType_Ready(&PyNullImporter_Type) < 0)
+ return NULL;
+
+ m = PyModule_Create(&impmodule);
+ if (m == NULL)
+ goto failure;
+ d = PyModule_GetDict(m);
+ if (d == NULL)
+ goto failure;
+
+ if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
+ if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
+ if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
+ if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
+ if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
+ if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
+ if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
+ if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
+ if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
+ if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
+
+ Py_INCREF(&PyNullImporter_Type);
+ PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
+ return m;
failure:
- Py_XDECREF(m);
- return NULL;
+ Py_XDECREF(m);
+ return NULL;
}
@@ -3453,31 +3453,31 @@ PyInit_imp(void)
int
PyImport_ExtendInittab(struct _inittab *newtab)
{
- static struct _inittab *our_copy = NULL;
- struct _inittab *p;
- int i, n;
+ static struct _inittab *our_copy = NULL;
+ struct _inittab *p;
+ int i, n;
- /* Count the number of entries in both tables */
- for (n = 0; newtab[n].name != NULL; n++)
- ;
- if (n == 0)
- return 0; /* Nothing to do */
- for (i = 0; PyImport_Inittab[i].name != NULL; i++)
- ;
+ /* Count the number of entries in both tables */
+ for (n = 0; newtab[n].name != NULL; n++)
+ ;
+ if (n == 0)
+ return 0; /* Nothing to do */
+ for (i = 0; PyImport_Inittab[i].name != NULL; i++)
+ ;
- /* Allocate new memory for the combined table */
- p = our_copy;
- PyMem_RESIZE(p, struct _inittab, i+n+1);
- if (p == NULL)
- return -1;
+ /* Allocate new memory for the combined table */
+ p = our_copy;
+ PyMem_RESIZE(p, struct _inittab, i+n+1);
+ if (p == NULL)
+ return -1;
- /* Copy the tables into the new memory */
- if (our_copy != PyImport_Inittab)
- memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
- PyImport_Inittab = our_copy = p;
- memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
+ /* Copy the tables into the new memory */
+ if (our_copy != PyImport_Inittab)
+ memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
+ PyImport_Inittab = our_copy = p;
+ memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
- return 0;
+ return 0;
}
/* Shorthand to add a single entry given a name and a function */
@@ -3485,14 +3485,14 @@ PyImport_ExtendInittab(struct _inittab *newtab)
int
PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
{
- struct _inittab newtab[2];
+ struct _inittab newtab[2];
- memset(newtab, '\0', sizeof newtab);
+ memset(newtab, '\0', sizeof newtab);
- newtab[0].name = (char *)name;
- newtab[0].initfunc = initfunc;
+ newtab[0].name = (char *)name;
+ newtab[0].initfunc = initfunc;
- return PyImport_ExtendInittab(newtab);
+ return PyImport_ExtendInittab(newtab);
}
#ifdef __cplusplus
diff --git a/Python/importdl.c b/Python/importdl.c
index d214ba1..507222b 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -13,76 +13,76 @@
#include "importdl.h"
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
- const char *shortname,
- const char *pathname, FILE *fp);
+ const char *shortname,
+ const char *pathname, FILE *fp);
PyObject *
_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
{
- PyObject *m;
- PyObject *path;
- char *lastdot, *shortname, *packagecontext, *oldcontext;
- dl_funcptr p0;
- PyObject* (*p)(void);
- struct PyModuleDef *def;
+ PyObject *m;
+ PyObject *path;
+ char *lastdot, *shortname, *packagecontext, *oldcontext;
+ dl_funcptr p0;
+ PyObject* (*p)(void);
+ struct PyModuleDef *def;
- if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
- Py_INCREF(m);
- return m;
- }
- lastdot = strrchr(name, '.');
- if (lastdot == NULL) {
- packagecontext = NULL;
- shortname = name;
- }
- else {
- packagecontext = name;
- shortname = lastdot+1;
- }
+ if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
+ Py_INCREF(m);
+ return m;
+ }
+ lastdot = strrchr(name, '.');
+ if (lastdot == NULL) {
+ packagecontext = NULL;
+ shortname = name;
+ }
+ else {
+ packagecontext = name;
+ shortname = lastdot+1;
+ }
- p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
- p = (PyObject*(*)(void))p0;
- if (PyErr_Occurred())
- return NULL;
- if (p == NULL) {
- PyErr_Format(PyExc_ImportError,
- "dynamic module does not define init function (PyInit_%.200s)",
- shortname);
- return NULL;
- }
- oldcontext = _Py_PackageContext;
- _Py_PackageContext = packagecontext;
- m = (*p)();
- _Py_PackageContext = oldcontext;
- if (m == NULL)
- return NULL;
+ p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
+ p = (PyObject*(*)(void))p0;
+ if (PyErr_Occurred())
+ return NULL;
+ if (p == NULL) {
+ PyErr_Format(PyExc_ImportError,
+ "dynamic module does not define init function (PyInit_%.200s)",
+ shortname);
+ return NULL;
+ }
+ oldcontext = _Py_PackageContext;
+ _Py_PackageContext = packagecontext;
+ m = (*p)();
+ _Py_PackageContext = oldcontext;
+ if (m == NULL)
+ return NULL;
- if (PyErr_Occurred()) {
- Py_DECREF(m);
- PyErr_Format(PyExc_SystemError,
- "initialization of %s raised unreported exception",
- shortname);
- return NULL;
- }
+ if (PyErr_Occurred()) {
+ Py_DECREF(m);
+ PyErr_Format(PyExc_SystemError,
+ "initialization of %s raised unreported exception",
+ shortname);
+ return NULL;
+ }
- /* Remember pointer to module init function. */
- def = PyModule_GetDef(m);
- def->m_base.m_init = p;
+ /* Remember pointer to module init function. */
+ def = PyModule_GetDef(m);
+ def->m_base.m_init = p;
- /* Remember the filename as the __file__ attribute */
- path = PyUnicode_DecodeFSDefault(pathname);
- if (PyModule_AddObject(m, "__file__", path) < 0)
- PyErr_Clear(); /* Not important enough to report */
+ /* Remember the filename as the __file__ attribute */
+ path = PyUnicode_DecodeFSDefault(pathname);
+ if (PyModule_AddObject(m, "__file__", path) < 0)
+ PyErr_Clear(); /* Not important enough to report */
- if (_PyImport_FixupExtension(m, name, pathname) < 0)
- return NULL;
- if (Py_VerboseFlag)
- PySys_WriteStderr(
- "import %s # dynamically loaded from %s\n",
- name, pathname);
- return m;
+ if (_PyImport_FixupExtension(m, name, pathname) < 0)
+ return NULL;
+ if (Py_VerboseFlag)
+ PySys_WriteStderr(
+ "import %s # dynamically loaded from %s\n",
+ name, pathname);
+ return m;
}
#endif /* HAVE_DYNAMIC_LOADING */
diff --git a/Python/importdl.h b/Python/importdl.h
index 5a2d45c..b4d21be 100644
--- a/Python/importdl.h
+++ b/Python/importdl.h
@@ -8,28 +8,28 @@ extern "C" {
/* Definitions for dynamic loading of extension modules */
enum filetype {
- SEARCH_ERROR,
- PY_SOURCE,
- PY_COMPILED,
- C_EXTENSION,
- PY_RESOURCE, /* Mac only */
- PKG_DIRECTORY,
- C_BUILTIN,
- PY_FROZEN,
- PY_CODERESOURCE, /* Mac only */
- IMP_HOOK
+ SEARCH_ERROR,
+ PY_SOURCE,
+ PY_COMPILED,
+ C_EXTENSION,
+ PY_RESOURCE, /* Mac only */
+ PKG_DIRECTORY,
+ C_BUILTIN,
+ PY_FROZEN,
+ PY_CODERESOURCE, /* Mac only */
+ IMP_HOOK
};
struct filedescr {
- char *suffix;
- char *mode;
- enum filetype type;
+ char *suffix;
+ char *mode;
+ enum filetype type;
};
extern struct filedescr * _PyImport_Filetab;
extern const struct filedescr _PyImport_DynLoadFiletab[];
extern PyObject *_PyImport_LoadDynamicModule(char *name, char *pathname,
- FILE *);
+ FILE *);
/* Max length of module suffix searched for -- accommodates "module.slb" */
#define MAXSUFFIXSIZE 12
diff --git a/Python/makeopcodetargets.py b/Python/makeopcodetargets.py
index a85ac52..5d8e5a9 100755
--- a/Python/makeopcodetargets.py
+++ b/Python/makeopcodetargets.py
@@ -28,7 +28,7 @@ def write_contents(f):
continue
targets[op] = "TARGET_%s" % opname
f.write("static void *opcode_targets[256] = {\n")
- f.write(",\n".join(["\t&&%s" % s for s in targets]))
+ f.write(",\n".join([" &&%s" % s for s in targets]))
f.write("\n};\n")
diff --git a/Python/marshal.c b/Python/marshal.c
index 256285b..21093ee 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -24,28 +24,28 @@
#define MAX_MARSHAL_STACK_DEPTH 2000
#endif
-#define TYPE_NULL '0'
-#define TYPE_NONE 'N'
-#define TYPE_FALSE 'F'
-#define TYPE_TRUE 'T'
-#define TYPE_STOPITER 'S'
-#define TYPE_ELLIPSIS '.'
-#define TYPE_INT 'i'
-#define TYPE_INT64 'I'
-#define TYPE_FLOAT 'f'
-#define TYPE_BINARY_FLOAT 'g'
-#define TYPE_COMPLEX 'x'
-#define TYPE_BINARY_COMPLEX 'y'
-#define TYPE_LONG 'l'
-#define TYPE_STRING 's'
-#define TYPE_TUPLE '('
-#define TYPE_LIST '['
-#define TYPE_DICT '{'
-#define TYPE_CODE 'c'
-#define TYPE_UNICODE 'u'
-#define TYPE_UNKNOWN '?'
-#define TYPE_SET '<'
-#define TYPE_FROZENSET '>'
+#define TYPE_NULL '0'
+#define TYPE_NONE 'N'
+#define TYPE_FALSE 'F'
+#define TYPE_TRUE 'T'
+#define TYPE_STOPITER 'S'
+#define TYPE_ELLIPSIS '.'
+#define TYPE_INT 'i'
+#define TYPE_INT64 'I'
+#define TYPE_FLOAT 'f'
+#define TYPE_BINARY_FLOAT 'g'
+#define TYPE_COMPLEX 'x'
+#define TYPE_BINARY_COMPLEX 'y'
+#define TYPE_LONG 'l'
+#define TYPE_STRING 's'
+#define TYPE_TUPLE '('
+#define TYPE_LIST '['
+#define TYPE_DICT '{'
+#define TYPE_CODE 'c'
+#define TYPE_UNICODE 'u'
+#define TYPE_UNKNOWN '?'
+#define TYPE_SET '<'
+#define TYPE_FROZENSET '>'
#define WFERR_OK 0
#define WFERR_UNMARSHALLABLE 1
@@ -53,79 +53,79 @@
#define WFERR_NOMEMORY 3
typedef struct {
- FILE *fp;
- int error; /* see WFERR_* values */
- int depth;
- /* If fp == NULL, the following are valid: */
- PyObject *str;
- char *ptr;
- char *end;
- PyObject *strings; /* dict on marshal, list on unmarshal */
- int version;
+ FILE *fp;
+ int error; /* see WFERR_* values */
+ int depth;
+ /* If fp == NULL, the following are valid: */
+ PyObject *str;
+ char *ptr;
+ char *end;
+ PyObject *strings; /* dict on marshal, list on unmarshal */
+ int version;
} WFILE;
#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
- else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
- else w_more(c, p)
+ else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
+ else w_more(c, p)
static void
w_more(int c, WFILE *p)
{
- Py_ssize_t size, newsize;
- if (p->str == NULL)
- return; /* An error already occurred */
- size = PyBytes_Size(p->str);
- newsize = size + size + 1024;
- if (newsize > 32*1024*1024) {
- newsize = size + (size >> 3); /* 12.5% overallocation */
- }
- if (_PyBytes_Resize(&p->str, newsize) != 0) {
- p->ptr = p->end = NULL;
- }
- else {
- p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
- p->end =
- PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
- *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
- }
+ Py_ssize_t size, newsize;
+ if (p->str == NULL)
+ return; /* An error already occurred */
+ size = PyBytes_Size(p->str);
+ newsize = size + size + 1024;
+ if (newsize > 32*1024*1024) {
+ newsize = size + (size >> 3); /* 12.5% overallocation */
+ }
+ if (_PyBytes_Resize(&p->str, newsize) != 0) {
+ p->ptr = p->end = NULL;
+ }
+ else {
+ p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
+ p->end =
+ PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
+ *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
+ }
}
static void
w_string(char *s, int n, WFILE *p)
{
- if (p->fp != NULL) {
- fwrite(s, 1, n, p->fp);
- }
- else {
- while (--n >= 0) {
- w_byte(*s, p);
- s++;
- }
- }
+ if (p->fp != NULL) {
+ fwrite(s, 1, n, p->fp);
+ }
+ else {
+ while (--n >= 0) {
+ w_byte(*s, p);
+ s++;
+ }
+ }
}
static void
w_short(int x, WFILE *p)
{
- w_byte((char)( x & 0xff), p);
- w_byte((char)((x>> 8) & 0xff), p);
+ w_byte((char)( x & 0xff), p);
+ w_byte((char)((x>> 8) & 0xff), p);
}
static void
w_long(long x, WFILE *p)
{
- w_byte((char)( x & 0xff), p);
- w_byte((char)((x>> 8) & 0xff), p);
- w_byte((char)((x>>16) & 0xff), p);
- w_byte((char)((x>>24) & 0xff), p);
+ w_byte((char)( x & 0xff), p);
+ w_byte((char)((x>> 8) & 0xff), p);
+ w_byte((char)((x>>16) & 0xff), p);
+ w_byte((char)((x>>24) & 0xff), p);
}
#if SIZEOF_LONG > 4
static void
w_long64(long x, WFILE *p)
{
- w_long(x, p);
- w_long(x>>32, p);
+ w_long(x, p);
+ w_long(x>>32, p);
}
#endif
@@ -144,324 +144,324 @@ w_long64(long x, WFILE *p)
static void
w_PyLong(const PyLongObject *ob, WFILE *p)
{
- Py_ssize_t i, j, n, l;
- digit d;
-
- w_byte(TYPE_LONG, p);
- if (Py_SIZE(ob) == 0) {
- w_long((long)0, p);
- return;
- }
-
- /* set l to number of base PyLong_MARSHAL_BASE digits */
- n = ABS(Py_SIZE(ob));
- l = (n-1) * PyLong_MARSHAL_RATIO;
- d = ob->ob_digit[n-1];
- assert(d != 0); /* a PyLong is always normalized */
- do {
- d >>= PyLong_MARSHAL_SHIFT;
- l++;
- } while (d != 0);
- w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
-
- for (i=0; i < n-1; i++) {
- d = ob->ob_digit[i];
- for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
- w_short(d & PyLong_MARSHAL_MASK, p);
- d >>= PyLong_MARSHAL_SHIFT;
- }
- assert (d == 0);
- }
- d = ob->ob_digit[n-1];
- do {
- w_short(d & PyLong_MARSHAL_MASK, p);
- d >>= PyLong_MARSHAL_SHIFT;
- } while (d != 0);
+ Py_ssize_t i, j, n, l;
+ digit d;
+
+ w_byte(TYPE_LONG, p);
+ if (Py_SIZE(ob) == 0) {
+ w_long((long)0, p);
+ return;
+ }
+
+ /* set l to number of base PyLong_MARSHAL_BASE digits */
+ n = ABS(Py_SIZE(ob));
+ l = (n-1) * PyLong_MARSHAL_RATIO;
+ d = ob->ob_digit[n-1];
+ assert(d != 0); /* a PyLong is always normalized */
+ do {
+ d >>= PyLong_MARSHAL_SHIFT;
+ l++;
+ } while (d != 0);
+ w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
+
+ for (i=0; i < n-1; i++) {
+ d = ob->ob_digit[i];
+ for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
+ w_short(d & PyLong_MARSHAL_MASK, p);
+ d >>= PyLong_MARSHAL_SHIFT;
+ }
+ assert (d == 0);
+ }
+ d = ob->ob_digit[n-1];
+ do {
+ w_short(d & PyLong_MARSHAL_MASK, p);
+ d >>= PyLong_MARSHAL_SHIFT;
+ } while (d != 0);
}
static void
w_object(PyObject *v, WFILE *p)
{
- Py_ssize_t i, n;
-
- p->depth++;
-
- if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
- p->error = WFERR_NESTEDTOODEEP;
- }
- else if (v == NULL) {
- w_byte(TYPE_NULL, p);
- }
- else if (v == Py_None) {
- w_byte(TYPE_NONE, p);
- }
- else if (v == PyExc_StopIteration) {
- w_byte(TYPE_STOPITER, p);
- }
- else if (v == Py_Ellipsis) {
- w_byte(TYPE_ELLIPSIS, p);
- }
- else if (v == Py_False) {
- w_byte(TYPE_FALSE, p);
- }
- else if (v == Py_True) {
- w_byte(TYPE_TRUE, p);
- }
- else if (PyLong_CheckExact(v)) {
- long x = PyLong_AsLong(v);
- if ((x == -1) && PyErr_Occurred()) {
- PyLongObject *ob = (PyLongObject *)v;
- PyErr_Clear();
- w_PyLong(ob, p);
- }
- else {
+ Py_ssize_t i, n;
+
+ p->depth++;
+
+ if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
+ p->error = WFERR_NESTEDTOODEEP;
+ }
+ else if (v == NULL) {
+ w_byte(TYPE_NULL, p);
+ }
+ else if (v == Py_None) {
+ w_byte(TYPE_NONE, p);
+ }
+ else if (v == PyExc_StopIteration) {
+ w_byte(TYPE_STOPITER, p);
+ }
+ else if (v == Py_Ellipsis) {
+ w_byte(TYPE_ELLIPSIS, p);
+ }
+ else if (v == Py_False) {
+ w_byte(TYPE_FALSE, p);
+ }
+ else if (v == Py_True) {
+ w_byte(TYPE_TRUE, p);
+ }
+ else if (PyLong_CheckExact(v)) {
+ long x = PyLong_AsLong(v);
+ if ((x == -1) && PyErr_Occurred()) {
+ PyLongObject *ob = (PyLongObject *)v;
+ PyErr_Clear();
+ w_PyLong(ob, p);
+ }
+ else {
#if SIZEOF_LONG > 4
- long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
- if (y && y != -1) {
- w_byte(TYPE_INT64, p);
- w_long64(x, p);
- }
- else
+ long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
+ if (y && y != -1) {
+ w_byte(TYPE_INT64, p);
+ w_long64(x, p);
+ }
+ else
#endif
- {
- w_byte(TYPE_INT, p);
- w_long(x, p);
- }
- }
- }
- else if (PyFloat_CheckExact(v)) {
- if (p->version > 1) {
- unsigned char buf[8];
- if (_PyFloat_Pack8(PyFloat_AsDouble(v),
- buf, 1) < 0) {
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_byte(TYPE_BINARY_FLOAT, p);
- w_string((char*)buf, 8, p);
- }
- else {
- char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
- 'g', 17, 0, NULL);
- if (!buf) {
- p->error = WFERR_NOMEMORY;
- return;
- }
- n = strlen(buf);
- w_byte(TYPE_FLOAT, p);
- w_byte((int)n, p);
- w_string(buf, (int)n, p);
- PyMem_Free(buf);
- }
- }
+ {
+ w_byte(TYPE_INT, p);
+ w_long(x, p);
+ }
+ }
+ }
+ else if (PyFloat_CheckExact(v)) {
+ if (p->version > 1) {
+ unsigned char buf[8];
+ if (_PyFloat_Pack8(PyFloat_AsDouble(v),
+ buf, 1) < 0) {
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_byte(TYPE_BINARY_FLOAT, p);
+ w_string((char*)buf, 8, p);
+ }
+ else {
+ char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
+ 'g', 17, 0, NULL);
+ if (!buf) {
+ p->error = WFERR_NOMEMORY;
+ return;
+ }
+ n = strlen(buf);
+ w_byte(TYPE_FLOAT, p);
+ w_byte((int)n, p);
+ w_string(buf, (int)n, p);
+ PyMem_Free(buf);
+ }
+ }
#ifndef WITHOUT_COMPLEX
- else if (PyComplex_CheckExact(v)) {
- if (p->version > 1) {
- unsigned char buf[8];
- if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
- buf, 1) < 0) {
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_byte(TYPE_BINARY_COMPLEX, p);
- w_string((char*)buf, 8, p);
- if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
- buf, 1) < 0) {
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_string((char*)buf, 8, p);
- }
- else {
- char *buf;
- w_byte(TYPE_COMPLEX, p);
- buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
- 'g', 17, 0, NULL);
- if (!buf) {
- p->error = WFERR_NOMEMORY;
- return;
- }
- n = strlen(buf);
- w_byte((int)n, p);
- w_string(buf, (int)n, p);
- PyMem_Free(buf);
- buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
- 'g', 17, 0, NULL);
- if (!buf) {
- p->error = WFERR_NOMEMORY;
- return;
- }
- n = strlen(buf);
- w_byte((int)n, p);
- w_string(buf, (int)n, p);
- PyMem_Free(buf);
- }
- }
+ else if (PyComplex_CheckExact(v)) {
+ if (p->version > 1) {
+ unsigned char buf[8];
+ if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
+ buf, 1) < 0) {
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_byte(TYPE_BINARY_COMPLEX, p);
+ w_string((char*)buf, 8, p);
+ if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
+ buf, 1) < 0) {
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_string((char*)buf, 8, p);
+ }
+ else {
+ char *buf;
+ w_byte(TYPE_COMPLEX, p);
+ buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
+ 'g', 17, 0, NULL);
+ if (!buf) {
+ p->error = WFERR_NOMEMORY;
+ return;
+ }
+ n = strlen(buf);
+ w_byte((int)n, p);
+ w_string(buf, (int)n, p);
+ PyMem_Free(buf);
+ buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
+ 'g', 17, 0, NULL);
+ if (!buf) {
+ p->error = WFERR_NOMEMORY;
+ return;
+ }
+ n = strlen(buf);
+ w_byte((int)n, p);
+ w_string(buf, (int)n, p);
+ PyMem_Free(buf);
+ }
+ }
#endif
- else if (PyBytes_CheckExact(v)) {
- w_byte(TYPE_STRING, p);
- n = PyBytes_GET_SIZE(v);
- if (n > INT_MAX) {
- /* huge strings are not supported */
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_long((long)n, p);
- w_string(PyBytes_AS_STRING(v), (int)n, p);
- }
- else if (PyUnicode_CheckExact(v)) {
- PyObject *utf8;
- utf8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(v),
- PyUnicode_GET_SIZE(v),
- "surrogatepass");
- if (utf8 == NULL) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_byte(TYPE_UNICODE, p);
- n = PyBytes_GET_SIZE(utf8);
- if (n > INT_MAX) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_long((long)n, p);
- w_string(PyBytes_AS_STRING(utf8), (int)n, p);
- Py_DECREF(utf8);
- }
- else if (PyTuple_CheckExact(v)) {
- w_byte(TYPE_TUPLE, p);
- n = PyTuple_Size(v);
- w_long((long)n, p);
- for (i = 0; i < n; i++) {
- w_object(PyTuple_GET_ITEM(v, i), p);
- }
- }
- else if (PyList_CheckExact(v)) {
- w_byte(TYPE_LIST, p);
- n = PyList_GET_SIZE(v);
- w_long((long)n, p);
- for (i = 0; i < n; i++) {
- w_object(PyList_GET_ITEM(v, i), p);
- }
- }
- else if (PyDict_CheckExact(v)) {
- Py_ssize_t pos;
- PyObject *key, *value;
- w_byte(TYPE_DICT, p);
- /* This one is NULL object terminated! */
- pos = 0;
- while (PyDict_Next(v, &pos, &key, &value)) {
- w_object(key, p);
- w_object(value, p);
- }
- w_object((PyObject *)NULL, p);
- }
- else if (PyAnySet_CheckExact(v)) {
- PyObject *value, *it;
-
- if (PyObject_TypeCheck(v, &PySet_Type))
- w_byte(TYPE_SET, p);
- else
- w_byte(TYPE_FROZENSET, p);
- n = PyObject_Size(v);
- if (n == -1) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_long((long)n, p);
- it = PyObject_GetIter(v);
- if (it == NULL) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- while ((value = PyIter_Next(it)) != NULL) {
- w_object(value, p);
- Py_DECREF(value);
- }
- Py_DECREF(it);
- if (PyErr_Occurred()) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- }
- else if (PyCode_Check(v)) {
- PyCodeObject *co = (PyCodeObject *)v;
- w_byte(TYPE_CODE, p);
- w_long(co->co_argcount, p);
- w_long(co->co_kwonlyargcount, p);
- w_long(co->co_nlocals, p);
- w_long(co->co_stacksize, p);
- w_long(co->co_flags, p);
- w_object(co->co_code, p);
- w_object(co->co_consts, p);
- w_object(co->co_names, p);
- w_object(co->co_varnames, p);
- w_object(co->co_freevars, p);
- w_object(co->co_cellvars, p);
- w_object(co->co_filename, p);
- w_object(co->co_name, p);
- w_long(co->co_firstlineno, p);
- w_object(co->co_lnotab, p);
- }
- else if (PyObject_CheckBuffer(v)) {
- /* Write unknown buffer-style objects as a string */
- char *s;
- PyBufferProcs *pb = v->ob_type->tp_as_buffer;
- Py_buffer view;
- if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
- w_byte(TYPE_UNKNOWN, p);
- p->error = WFERR_UNMARSHALLABLE;
- }
- w_byte(TYPE_STRING, p);
- n = view.len;
- s = view.buf;
- if (n > INT_MAX) {
- p->depth--;
- p->error = WFERR_UNMARSHALLABLE;
- return;
- }
- w_long((long)n, p);
- w_string(s, (int)n, p);
- if (pb->bf_releasebuffer != NULL)
- (*pb->bf_releasebuffer)(v, &view);
- }
- else {
- w_byte(TYPE_UNKNOWN, p);
- p->error = WFERR_UNMARSHALLABLE;
- }
- p->depth--;
+ else if (PyBytes_CheckExact(v)) {
+ w_byte(TYPE_STRING, p);
+ n = PyBytes_GET_SIZE(v);
+ if (n > INT_MAX) {
+ /* huge strings are not supported */
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_long((long)n, p);
+ w_string(PyBytes_AS_STRING(v), (int)n, p);
+ }
+ else if (PyUnicode_CheckExact(v)) {
+ PyObject *utf8;
+ utf8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(v),
+ PyUnicode_GET_SIZE(v),
+ "surrogatepass");
+ if (utf8 == NULL) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_byte(TYPE_UNICODE, p);
+ n = PyBytes_GET_SIZE(utf8);
+ if (n > INT_MAX) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_long((long)n, p);
+ w_string(PyBytes_AS_STRING(utf8), (int)n, p);
+ Py_DECREF(utf8);
+ }
+ else if (PyTuple_CheckExact(v)) {
+ w_byte(TYPE_TUPLE, p);
+ n = PyTuple_Size(v);
+ w_long((long)n, p);
+ for (i = 0; i < n; i++) {
+ w_object(PyTuple_GET_ITEM(v, i), p);
+ }
+ }
+ else if (PyList_CheckExact(v)) {
+ w_byte(TYPE_LIST, p);
+ n = PyList_GET_SIZE(v);
+ w_long((long)n, p);
+ for (i = 0; i < n; i++) {
+ w_object(PyList_GET_ITEM(v, i), p);
+ }
+ }
+ else if (PyDict_CheckExact(v)) {
+ Py_ssize_t pos;
+ PyObject *key, *value;
+ w_byte(TYPE_DICT, p);
+ /* This one is NULL object terminated! */
+ pos = 0;
+ while (PyDict_Next(v, &pos, &key, &value)) {
+ w_object(key, p);
+ w_object(value, p);
+ }
+ w_object((PyObject *)NULL, p);
+ }
+ else if (PyAnySet_CheckExact(v)) {
+ PyObject *value, *it;
+
+ if (PyObject_TypeCheck(v, &PySet_Type))
+ w_byte(TYPE_SET, p);
+ else
+ w_byte(TYPE_FROZENSET, p);
+ n = PyObject_Size(v);
+ if (n == -1) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_long((long)n, p);
+ it = PyObject_GetIter(v);
+ if (it == NULL) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ while ((value = PyIter_Next(it)) != NULL) {
+ w_object(value, p);
+ Py_DECREF(value);
+ }
+ Py_DECREF(it);
+ if (PyErr_Occurred()) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ }
+ else if (PyCode_Check(v)) {
+ PyCodeObject *co = (PyCodeObject *)v;
+ w_byte(TYPE_CODE, p);
+ w_long(co->co_argcount, p);
+ w_long(co->co_kwonlyargcount, p);
+ w_long(co->co_nlocals, p);
+ w_long(co->co_stacksize, p);
+ w_long(co->co_flags, p);
+ w_object(co->co_code, p);
+ w_object(co->co_consts, p);
+ w_object(co->co_names, p);
+ w_object(co->co_varnames, p);
+ w_object(co->co_freevars, p);
+ w_object(co->co_cellvars, p);
+ w_object(co->co_filename, p);
+ w_object(co->co_name, p);
+ w_long(co->co_firstlineno, p);
+ w_object(co->co_lnotab, p);
+ }
+ else if (PyObject_CheckBuffer(v)) {
+ /* Write unknown buffer-style objects as a string */
+ char *s;
+ PyBufferProcs *pb = v->ob_type->tp_as_buffer;
+ Py_buffer view;
+ if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
+ w_byte(TYPE_UNKNOWN, p);
+ p->error = WFERR_UNMARSHALLABLE;
+ }
+ w_byte(TYPE_STRING, p);
+ n = view.len;
+ s = view.buf;
+ if (n > INT_MAX) {
+ p->depth--;
+ p->error = WFERR_UNMARSHALLABLE;
+ return;
+ }
+ w_long((long)n, p);
+ w_string(s, (int)n, p);
+ if (pb->bf_releasebuffer != NULL)
+ (*pb->bf_releasebuffer)(v, &view);
+ }
+ else {
+ w_byte(TYPE_UNKNOWN, p);
+ p->error = WFERR_UNMARSHALLABLE;
+ }
+ p->depth--;
}
/* version currently has no effect for writing longs. */
void
PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
{
- WFILE wf;
- wf.fp = fp;
- wf.error = WFERR_OK;
- wf.depth = 0;
- wf.strings = NULL;
- wf.version = version;
- w_long(x, &wf);
+ WFILE wf;
+ wf.fp = fp;
+ wf.error = WFERR_OK;
+ wf.depth = 0;
+ wf.strings = NULL;
+ wf.version = version;
+ w_long(x, &wf);
}
void
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
{
- WFILE wf;
- wf.fp = fp;
- wf.error = WFERR_OK;
- wf.depth = 0;
- wf.strings = (version > 0) ? PyDict_New() : NULL;
- wf.version = version;
- w_object(x, &wf);
- Py_XDECREF(wf.strings);
+ WFILE wf;
+ wf.fp = fp;
+ wf.error = WFERR_OK;
+ wf.depth = 0;
+ wf.strings = (version > 0) ? PyDict_New() : NULL;
+ wf.version = version;
+ w_object(x, &wf);
+ Py_XDECREF(wf.strings);
}
typedef WFILE RFILE; /* Same struct with different invariants */
@@ -473,49 +473,49 @@ typedef WFILE RFILE; /* Same struct with different invariants */
static int
r_string(char *s, int n, RFILE *p)
{
- if (p->fp != NULL)
- /* The result fits into int because it must be <=n. */
- return (int)fread(s, 1, n, p->fp);
- if (p->end - p->ptr < n)
- n = (int)(p->end - p->ptr);
- memcpy(s, p->ptr, n);
- p->ptr += n;
- return n;
+ if (p->fp != NULL)
+ /* The result fits into int because it must be <=n. */
+ return (int)fread(s, 1, n, p->fp);
+ if (p->end - p->ptr < n)
+ n = (int)(p->end - p->ptr);
+ memcpy(s, p->ptr, n);
+ p->ptr += n;
+ return n;
}
static int
r_short(RFILE *p)
{
- register short x;
- x = r_byte(p);
- x |= r_byte(p) << 8;
- /* Sign-extension, in case short greater than 16 bits */
- x |= -(x & 0x8000);
- return x;
+ register short x;
+ x = r_byte(p);
+ x |= r_byte(p) << 8;
+ /* Sign-extension, in case short greater than 16 bits */
+ x |= -(x & 0x8000);
+ return x;
}
static long
r_long(RFILE *p)
{
- register long x;
- register FILE *fp = p->fp;
- if (fp) {
- x = getc(fp);
- x |= (long)getc(fp) << 8;
- x |= (long)getc(fp) << 16;
- x |= (long)getc(fp) << 24;
- }
- else {
- x = rs_byte(p);
- x |= (long)rs_byte(p) << 8;
- x |= (long)rs_byte(p) << 16;
- x |= (long)rs_byte(p) << 24;
- }
+ register long x;
+ register FILE *fp = p->fp;
+ if (fp) {
+ x = getc(fp);
+ x |= (long)getc(fp) << 8;
+ x |= (long)getc(fp) << 16;
+ x |= (long)getc(fp) << 24;
+ }
+ else {
+ x = rs_byte(p);
+ x |= (long)rs_byte(p) << 8;
+ x |= (long)rs_byte(p) << 16;
+ x |= (long)rs_byte(p) << 24;
+ }
#if SIZEOF_LONG > 4
- /* Sign extension for 64-bit machines */
- x |= -(x & 0x80000000L);
+ /* Sign extension for 64-bit machines */
+ x |= -(x & 0x80000000L);
#endif
- return x;
+ return x;
}
/* r_long64 deals with the TYPE_INT64 code. On a machine with
@@ -528,536 +528,536 @@ r_long(RFILE *p)
static PyObject *
r_long64(RFILE *p)
{
- long lo4 = r_long(p);
- long hi4 = r_long(p);
+ long lo4 = r_long(p);
+ long hi4 = r_long(p);
#if SIZEOF_LONG > 4
- long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
- return PyLong_FromLong(x);
+ long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
+ return PyLong_FromLong(x);
#else
- unsigned char buf[8];
- int one = 1;
- int is_little_endian = (int)*(char*)&one;
- if (is_little_endian) {
- memcpy(buf, &lo4, 4);
- memcpy(buf+4, &hi4, 4);
- }
- else {
- memcpy(buf, &hi4, 4);
- memcpy(buf+4, &lo4, 4);
- }
- return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
+ unsigned char buf[8];
+ int one = 1;
+ int is_little_endian = (int)*(char*)&one;
+ if (is_little_endian) {
+ memcpy(buf, &lo4, 4);
+ memcpy(buf+4, &hi4, 4);
+ }
+ else {
+ memcpy(buf, &hi4, 4);
+ memcpy(buf+4, &lo4, 4);
+ }
+ return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
#endif
}
static PyObject *
r_PyLong(RFILE *p)
{
- PyLongObject *ob;
- int size, i, j, md, shorts_in_top_digit;
- long n;
- digit d;
-
- n = r_long(p);
- if (n == 0)
- return (PyObject *)_PyLong_New(0);
- if (n < -INT_MAX || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (long size out of range)");
- return NULL;
- }
-
- size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
- shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
- ob = _PyLong_New(size);
- if (ob == NULL)
- return NULL;
- Py_SIZE(ob) = n > 0 ? size : -size;
-
- for (i = 0; i < size-1; i++) {
- d = 0;
- for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
- md = r_short(p);
- if (md < 0 || md > PyLong_MARSHAL_BASE)
- goto bad_digit;
- d += (digit)md << j*PyLong_MARSHAL_SHIFT;
- }
- ob->ob_digit[i] = d;
- }
- d = 0;
- for (j=0; j < shorts_in_top_digit; j++) {
- md = r_short(p);
- if (md < 0 || md > PyLong_MARSHAL_BASE)
- goto bad_digit;
- /* topmost marshal digit should be nonzero */
- if (md == 0 && j == shorts_in_top_digit - 1) {
- Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (unnormalized long data)");
- return NULL;
- }
- d += (digit)md << j*PyLong_MARSHAL_SHIFT;
- }
- /* top digit should be nonzero, else the resulting PyLong won't be
- normalized */
- ob->ob_digit[size-1] = d;
- return (PyObject *)ob;
+ PyLongObject *ob;
+ int size, i, j, md, shorts_in_top_digit;
+ long n;
+ digit d;
+
+ n = r_long(p);
+ if (n == 0)
+ return (PyObject *)_PyLong_New(0);
+ if (n < -INT_MAX || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (long size out of range)");
+ return NULL;
+ }
+
+ size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
+ shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
+ ob = _PyLong_New(size);
+ if (ob == NULL)
+ return NULL;
+ Py_SIZE(ob) = n > 0 ? size : -size;
+
+ for (i = 0; i < size-1; i++) {
+ d = 0;
+ for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
+ md = r_short(p);
+ if (md < 0 || md > PyLong_MARSHAL_BASE)
+ goto bad_digit;
+ d += (digit)md << j*PyLong_MARSHAL_SHIFT;
+ }
+ ob->ob_digit[i] = d;
+ }
+ d = 0;
+ for (j=0; j < shorts_in_top_digit; j++) {
+ md = r_short(p);
+ if (md < 0 || md > PyLong_MARSHAL_BASE)
+ goto bad_digit;
+ /* topmost marshal digit should be nonzero */
+ if (md == 0 && j == shorts_in_top_digit - 1) {
+ Py_DECREF(ob);
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (unnormalized long data)");
+ return NULL;
+ }
+ d += (digit)md << j*PyLong_MARSHAL_SHIFT;
+ }
+ /* top digit should be nonzero, else the resulting PyLong won't be
+ normalized */
+ ob->ob_digit[size-1] = d;
+ return (PyObject *)ob;
bad_digit:
- Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (digit out of range in long)");
- return NULL;
+ Py_DECREF(ob);
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (digit out of range in long)");
+ return NULL;
}
static PyObject *
r_object(RFILE *p)
{
- /* NULL is a valid return value, it does not necessarily means that
- an exception is set. */
- PyObject *v, *v2;
- long i, n;
- int type = r_byte(p);
- PyObject *retval;
-
- p->depth++;
-
- if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
- p->depth--;
- PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
- return NULL;
- }
-
- switch (type) {
-
- case EOF:
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
-
- case TYPE_NULL:
- retval = NULL;
- break;
-
- case TYPE_NONE:
- Py_INCREF(Py_None);
- retval = Py_None;
- break;
-
- case TYPE_STOPITER:
- Py_INCREF(PyExc_StopIteration);
- retval = PyExc_StopIteration;
- break;
-
- case TYPE_ELLIPSIS:
- Py_INCREF(Py_Ellipsis);
- retval = Py_Ellipsis;
- break;
-
- case TYPE_FALSE:
- Py_INCREF(Py_False);
- retval = Py_False;
- break;
-
- case TYPE_TRUE:
- Py_INCREF(Py_True);
- retval = Py_True;
- break;
-
- case TYPE_INT:
- retval = PyLong_FromLong(r_long(p));
- break;
-
- case TYPE_INT64:
- retval = r_long64(p);
- break;
-
- case TYPE_LONG:
- retval = r_PyLong(p);
- break;
-
- case TYPE_FLOAT:
- {
- char buf[256];
- double dx;
- retval = NULL;
- n = r_byte(p);
- if (n == EOF || r_string(buf, (int)n, p) != n) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- break;
- }
- buf[n] = '\0';
- dx = PyOS_string_to_double(buf, NULL, NULL);
- if (dx == -1.0 && PyErr_Occurred())
- break;
- retval = PyFloat_FromDouble(dx);
- break;
- }
-
- case TYPE_BINARY_FLOAT:
- {
- unsigned char buf[8];
- double x;
- if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
- }
- x = _PyFloat_Unpack8(buf, 1);
- if (x == -1.0 && PyErr_Occurred()) {
- retval = NULL;
- break;
- }
- retval = PyFloat_FromDouble(x);
- break;
- }
+ /* NULL is a valid return value, it does not necessarily means that
+ an exception is set. */
+ PyObject *v, *v2;
+ long i, n;
+ int type = r_byte(p);
+ PyObject *retval;
+
+ p->depth++;
+
+ if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
+ p->depth--;
+ PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
+ return NULL;
+ }
+
+ switch (type) {
+
+ case EOF:
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+
+ case TYPE_NULL:
+ retval = NULL;
+ break;
+
+ case TYPE_NONE:
+ Py_INCREF(Py_None);
+ retval = Py_None;
+ break;
+
+ case TYPE_STOPITER:
+ Py_INCREF(PyExc_StopIteration);
+ retval = PyExc_StopIteration;
+ break;
+
+ case TYPE_ELLIPSIS:
+ Py_INCREF(Py_Ellipsis);
+ retval = Py_Ellipsis;
+ break;
+
+ case TYPE_FALSE:
+ Py_INCREF(Py_False);
+ retval = Py_False;
+ break;
+
+ case TYPE_TRUE:
+ Py_INCREF(Py_True);
+ retval = Py_True;
+ break;
+
+ case TYPE_INT:
+ retval = PyLong_FromLong(r_long(p));
+ break;
+
+ case TYPE_INT64:
+ retval = r_long64(p);
+ break;
+
+ case TYPE_LONG:
+ retval = r_PyLong(p);
+ break;
+
+ case TYPE_FLOAT:
+ {
+ char buf[256];
+ double dx;
+ retval = NULL;
+ n = r_byte(p);
+ if (n == EOF || r_string(buf, (int)n, p) != n) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ break;
+ }
+ buf[n] = '\0';
+ dx = PyOS_string_to_double(buf, NULL, NULL);
+ if (dx == -1.0 && PyErr_Occurred())
+ break;
+ retval = PyFloat_FromDouble(dx);
+ break;
+ }
+
+ case TYPE_BINARY_FLOAT:
+ {
+ unsigned char buf[8];
+ double x;
+ if (r_string((char*)buf, 8, p) != 8) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+ }
+ x = _PyFloat_Unpack8(buf, 1);
+ if (x == -1.0 && PyErr_Occurred()) {
+ retval = NULL;
+ break;
+ }
+ retval = PyFloat_FromDouble(x);
+ break;
+ }
#ifndef WITHOUT_COMPLEX
- case TYPE_COMPLEX:
- {
- char buf[256];
- Py_complex c;
- retval = NULL;
- n = r_byte(p);
- if (n == EOF || r_string(buf, (int)n, p) != n) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- break;
- }
- buf[n] = '\0';
- c.real = PyOS_string_to_double(buf, NULL, NULL);
- if (c.real == -1.0 && PyErr_Occurred())
- break;
- n = r_byte(p);
- if (n == EOF || r_string(buf, (int)n, p) != n) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- break;
- }
- buf[n] = '\0';
- c.imag = PyOS_string_to_double(buf, NULL, NULL);
- if (c.imag == -1.0 && PyErr_Occurred())
- break;
- retval = PyComplex_FromCComplex(c);
- break;
- }
-
- case TYPE_BINARY_COMPLEX:
- {
- unsigned char buf[8];
- Py_complex c;
- if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
- }
- c.real = _PyFloat_Unpack8(buf, 1);
- if (c.real == -1.0 && PyErr_Occurred()) {
- retval = NULL;
- break;
- }
- if (r_string((char*)buf, 8, p) != 8) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
- }
- c.imag = _PyFloat_Unpack8(buf, 1);
- if (c.imag == -1.0 && PyErr_Occurred()) {
- retval = NULL;
- break;
- }
- retval = PyComplex_FromCComplex(c);
- break;
- }
+ case TYPE_COMPLEX:
+ {
+ char buf[256];
+ Py_complex c;
+ retval = NULL;
+ n = r_byte(p);
+ if (n == EOF || r_string(buf, (int)n, p) != n) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ break;
+ }
+ buf[n] = '\0';
+ c.real = PyOS_string_to_double(buf, NULL, NULL);
+ if (c.real == -1.0 && PyErr_Occurred())
+ break;
+ n = r_byte(p);
+ if (n == EOF || r_string(buf, (int)n, p) != n) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ break;
+ }
+ buf[n] = '\0';
+ c.imag = PyOS_string_to_double(buf, NULL, NULL);
+ if (c.imag == -1.0 && PyErr_Occurred())
+ break;
+ retval = PyComplex_FromCComplex(c);
+ break;
+ }
+
+ case TYPE_BINARY_COMPLEX:
+ {
+ unsigned char buf[8];
+ Py_complex c;
+ if (r_string((char*)buf, 8, p) != 8) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+ }
+ c.real = _PyFloat_Unpack8(buf, 1);
+ if (c.real == -1.0 && PyErr_Occurred()) {
+ retval = NULL;
+ break;
+ }
+ if (r_string((char*)buf, 8, p) != 8) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+ }
+ c.imag = _PyFloat_Unpack8(buf, 1);
+ if (c.imag == -1.0 && PyErr_Occurred()) {
+ retval = NULL;
+ break;
+ }
+ retval = PyComplex_FromCComplex(c);
+ break;
+ }
#endif
- case TYPE_STRING:
- n = r_long(p);
- if (n < 0 || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
- retval = NULL;
- break;
- }
- v = PyBytes_FromStringAndSize((char *)NULL, n);
- if (v == NULL) {
- retval = NULL;
- break;
- }
- if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) {
- Py_DECREF(v);
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
- }
- retval = v;
- break;
-
- case TYPE_UNICODE:
- {
- char *buffer;
-
- n = r_long(p);
- if (n < 0 || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
- retval = NULL;
- break;
- }
- buffer = PyMem_NEW(char, n);
- if (buffer == NULL) {
- retval = PyErr_NoMemory();
- break;
- }
- if (r_string(buffer, (int)n, p) != n) {
- PyMem_DEL(buffer);
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
- retval = NULL;
- break;
- }
- v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
- PyMem_DEL(buffer);
- retval = v;
- break;
- }
-
- case TYPE_TUPLE:
- n = r_long(p);
- if (n < 0 || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
- retval = NULL;
- break;
- }
- v = PyTuple_New((int)n);
- if (v == NULL) {
- retval = NULL;
- break;
- }
- for (i = 0; i < n; i++) {
- v2 = r_object(p);
- if ( v2 == NULL ) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError,
- "NULL object in marshal data for tuple");
- Py_DECREF(v);
- v = NULL;
- break;
- }
- PyTuple_SET_ITEM(v, (int)i, v2);
- }
- retval = v;
- break;
-
- case TYPE_LIST:
- n = r_long(p);
- if (n < 0 || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
- retval = NULL;
- break;
- }
- v = PyList_New((int)n);
- if (v == NULL) {
- retval = NULL;
- break;
- }
- for (i = 0; i < n; i++) {
- v2 = r_object(p);
- if ( v2 == NULL ) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError,
- "NULL object in marshal data for list");
- Py_DECREF(v);
- v = NULL;
- break;
- }
- PyList_SET_ITEM(v, (int)i, v2);
- }
- retval = v;
- break;
-
- case TYPE_DICT:
- v = PyDict_New();
- if (v == NULL) {
- retval = NULL;
- break;
- }
- for (;;) {
- PyObject *key, *val;
- key = r_object(p);
- if (key == NULL)
- break;
- val = r_object(p);
- if (val != NULL)
- PyDict_SetItem(v, key, val);
- Py_DECREF(key);
- Py_XDECREF(val);
- }
- if (PyErr_Occurred()) {
- Py_DECREF(v);
- v = NULL;
- }
- retval = v;
- break;
-
- case TYPE_SET:
- case TYPE_FROZENSET:
- n = r_long(p);
- if (n < 0 || n > INT_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
- retval = NULL;
- break;
- }
- v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
- if (v == NULL) {
- retval = NULL;
- break;
- }
- for (i = 0; i < n; i++) {
- v2 = r_object(p);
- if ( v2 == NULL ) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError,
- "NULL object in marshal data for set");
- Py_DECREF(v);
- v = NULL;
- break;
- }
- if (PySet_Add(v, v2) == -1) {
- Py_DECREF(v);
- Py_DECREF(v2);
- v = NULL;
- break;
- }
- Py_DECREF(v2);
- }
- retval = v;
- break;
-
- case TYPE_CODE:
- {
- int argcount;
- int kwonlyargcount;
- int nlocals;
- int stacksize;
- int flags;
- PyObject *code = NULL;
- PyObject *consts = NULL;
- PyObject *names = NULL;
- PyObject *varnames = NULL;
- PyObject *freevars = NULL;
- PyObject *cellvars = NULL;
- PyObject *filename = NULL;
- PyObject *name = NULL;
- int firstlineno;
- PyObject *lnotab = NULL;
-
- v = NULL;
-
- /* XXX ignore long->int overflows for now */
- argcount = (int)r_long(p);
- kwonlyargcount = (int)r_long(p);
- nlocals = (int)r_long(p);
- stacksize = (int)r_long(p);
- flags = (int)r_long(p);
- code = r_object(p);
- if (code == NULL)
- goto code_error;
- consts = r_object(p);
- if (consts == NULL)
- goto code_error;
- names = r_object(p);
- if (names == NULL)
- goto code_error;
- varnames = r_object(p);
- if (varnames == NULL)
- goto code_error;
- freevars = r_object(p);
- if (freevars == NULL)
- goto code_error;
- cellvars = r_object(p);
- if (cellvars == NULL)
- goto code_error;
- filename = r_object(p);
- if (filename == NULL)
- goto code_error;
- name = r_object(p);
- if (name == NULL)
- goto code_error;
- firstlineno = (int)r_long(p);
- lnotab = r_object(p);
- if (lnotab == NULL)
- goto code_error;
-
- v = (PyObject *) PyCode_New(
- argcount, kwonlyargcount,
- nlocals, stacksize, flags,
- code, consts, names, varnames,
- freevars, cellvars, filename, name,
- firstlineno, lnotab);
-
- code_error:
- Py_XDECREF(code);
- Py_XDECREF(consts);
- Py_XDECREF(names);
- Py_XDECREF(varnames);
- Py_XDECREF(freevars);
- Py_XDECREF(cellvars);
- Py_XDECREF(filename);
- Py_XDECREF(name);
- Py_XDECREF(lnotab);
- }
- retval = v;
- break;
-
- default:
- /* Bogus data got written, which isn't ideal.
- This will let you keep working and recover. */
- PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
- retval = NULL;
- break;
-
- }
- p->depth--;
- return retval;
+ case TYPE_STRING:
+ n = r_long(p);
+ if (n < 0 || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ retval = NULL;
+ break;
+ }
+ v = PyBytes_FromStringAndSize((char *)NULL, n);
+ if (v == NULL) {
+ retval = NULL;
+ break;
+ }
+ if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) {
+ Py_DECREF(v);
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+ }
+ retval = v;
+ break;
+
+ case TYPE_UNICODE:
+ {
+ char *buffer;
+
+ n = r_long(p);
+ if (n < 0 || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
+ retval = NULL;
+ break;
+ }
+ buffer = PyMem_NEW(char, n);
+ if (buffer == NULL) {
+ retval = PyErr_NoMemory();
+ break;
+ }
+ if (r_string(buffer, (int)n, p) != n) {
+ PyMem_DEL(buffer);
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ retval = NULL;
+ break;
+ }
+ v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
+ PyMem_DEL(buffer);
+ retval = v;
+ break;
+ }
+
+ case TYPE_TUPLE:
+ n = r_long(p);
+ if (n < 0 || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
+ retval = NULL;
+ break;
+ }
+ v = PyTuple_New((int)n);
+ if (v == NULL) {
+ retval = NULL;
+ break;
+ }
+ for (i = 0; i < n; i++) {
+ v2 = r_object(p);
+ if ( v2 == NULL ) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for tuple");
+ Py_DECREF(v);
+ v = NULL;
+ break;
+ }
+ PyTuple_SET_ITEM(v, (int)i, v2);
+ }
+ retval = v;
+ break;
+
+ case TYPE_LIST:
+ n = r_long(p);
+ if (n < 0 || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
+ retval = NULL;
+ break;
+ }
+ v = PyList_New((int)n);
+ if (v == NULL) {
+ retval = NULL;
+ break;
+ }
+ for (i = 0; i < n; i++) {
+ v2 = r_object(p);
+ if ( v2 == NULL ) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for list");
+ Py_DECREF(v);
+ v = NULL;
+ break;
+ }
+ PyList_SET_ITEM(v, (int)i, v2);
+ }
+ retval = v;
+ break;
+
+ case TYPE_DICT:
+ v = PyDict_New();
+ if (v == NULL) {
+ retval = NULL;
+ break;
+ }
+ for (;;) {
+ PyObject *key, *val;
+ key = r_object(p);
+ if (key == NULL)
+ break;
+ val = r_object(p);
+ if (val != NULL)
+ PyDict_SetItem(v, key, val);
+ Py_DECREF(key);
+ Py_XDECREF(val);
+ }
+ if (PyErr_Occurred()) {
+ Py_DECREF(v);
+ v = NULL;
+ }
+ retval = v;
+ break;
+
+ case TYPE_SET:
+ case TYPE_FROZENSET:
+ n = r_long(p);
+ if (n < 0 || n > INT_MAX) {
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
+ retval = NULL;
+ break;
+ }
+ v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
+ if (v == NULL) {
+ retval = NULL;
+ break;
+ }
+ for (i = 0; i < n; i++) {
+ v2 = r_object(p);
+ if ( v2 == NULL ) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for set");
+ Py_DECREF(v);
+ v = NULL;
+ break;
+ }
+ if (PySet_Add(v, v2) == -1) {
+ Py_DECREF(v);
+ Py_DECREF(v2);
+ v = NULL;
+ break;
+ }
+ Py_DECREF(v2);
+ }
+ retval = v;
+ break;
+
+ case TYPE_CODE:
+ {
+ int argcount;
+ int kwonlyargcount;
+ int nlocals;
+ int stacksize;
+ int flags;
+ PyObject *code = NULL;
+ PyObject *consts = NULL;
+ PyObject *names = NULL;
+ PyObject *varnames = NULL;
+ PyObject *freevars = NULL;
+ PyObject *cellvars = NULL;
+ PyObject *filename = NULL;
+ PyObject *name = NULL;
+ int firstlineno;
+ PyObject *lnotab = NULL;
+
+ v = NULL;
+
+ /* XXX ignore long->int overflows for now */
+ argcount = (int)r_long(p);
+ kwonlyargcount = (int)r_long(p);
+ nlocals = (int)r_long(p);
+ stacksize = (int)r_long(p);
+ flags = (int)r_long(p);
+ code = r_object(p);
+ if (code == NULL)
+ goto code_error;
+ consts = r_object(p);
+ if (consts == NULL)
+ goto code_error;
+ names = r_object(p);
+ if (names == NULL)
+ goto code_error;
+ varnames = r_object(p);
+ if (varnames == NULL)
+ goto code_error;
+ freevars = r_object(p);
+ if (freevars == NULL)
+ goto code_error;
+ cellvars = r_object(p);
+ if (cellvars == NULL)
+ goto code_error;
+ filename = r_object(p);
+ if (filename == NULL)
+ goto code_error;
+ name = r_object(p);
+ if (name == NULL)
+ goto code_error;
+ firstlineno = (int)r_long(p);
+ lnotab = r_object(p);
+ if (lnotab == NULL)
+ goto code_error;
+
+ v = (PyObject *) PyCode_New(
+ argcount, kwonlyargcount,
+ nlocals, stacksize, flags,
+ code, consts, names, varnames,
+ freevars, cellvars, filename, name,
+ firstlineno, lnotab);
+
+ code_error:
+ Py_XDECREF(code);
+ Py_XDECREF(consts);
+ Py_XDECREF(names);
+ Py_XDECREF(varnames);
+ Py_XDECREF(freevars);
+ Py_XDECREF(cellvars);
+ Py_XDECREF(filename);
+ Py_XDECREF(name);
+ Py_XDECREF(lnotab);
+ }
+ retval = v;
+ break;
+
+ default:
+ /* Bogus data got written, which isn't ideal.
+ This will let you keep working and recover. */
+ PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
+ retval = NULL;
+ break;
+
+ }
+ p->depth--;
+ return retval;
}
static PyObject *
read_object(RFILE *p)
{
- PyObject *v;
- if (PyErr_Occurred()) {
- fprintf(stderr, "XXX readobject called with exception set\n");
- return NULL;
- }
- v = r_object(p);
- if (v == NULL && !PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
- return v;
+ PyObject *v;
+ if (PyErr_Occurred()) {
+ fprintf(stderr, "XXX readobject called with exception set\n");
+ return NULL;
+ }
+ v = r_object(p);
+ if (v == NULL && !PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
+ return v;
}
int
PyMarshal_ReadShortFromFile(FILE *fp)
{
- RFILE rf;
- assert(fp);
- rf.fp = fp;
- rf.strings = NULL;
- rf.end = rf.ptr = NULL;
- return r_short(&rf);
+ RFILE rf;
+ assert(fp);
+ rf.fp = fp;
+ rf.strings = NULL;
+ rf.end = rf.ptr = NULL;
+ return r_short(&rf);
}
long
PyMarshal_ReadLongFromFile(FILE *fp)
{
- RFILE rf;
- rf.fp = fp;
- rf.strings = NULL;
- rf.ptr = rf.end = NULL;
- return r_long(&rf);
+ RFILE rf;
+ rf.fp = fp;
+ rf.strings = NULL;
+ rf.ptr = rf.end = NULL;
+ return r_long(&rf);
}
#ifdef HAVE_FSTAT
@@ -1065,11 +1065,11 @@ PyMarshal_ReadLongFromFile(FILE *fp)
static off_t
getfilesize(FILE *fp)
{
- struct stat st;
- if (fstat(fileno(fp), &st) != 0)
- return -1;
- else
- return st.st_size;
+ struct stat st;
+ if (fstat(fileno(fp), &st) != 0)
+ return -1;
+ else
+ return st.st_size;
}
#endif
@@ -1088,35 +1088,35 @@ PyMarshal_ReadLastObjectFromFile(FILE *fp)
#define SMALL_FILE_LIMIT (1L << 14)
#define REASONABLE_FILE_LIMIT (1L << 18)
#ifdef HAVE_FSTAT
- off_t filesize;
+ off_t filesize;
#endif
#ifdef HAVE_FSTAT
- filesize = getfilesize(fp);
- if (filesize > 0) {
- char buf[SMALL_FILE_LIMIT];
- char* pBuf = NULL;
- if (filesize <= SMALL_FILE_LIMIT)
- pBuf = buf;
- else if (filesize <= REASONABLE_FILE_LIMIT)
- pBuf = (char *)PyMem_MALLOC(filesize);
- if (pBuf != NULL) {
- PyObject* v;
- size_t n;
- /* filesize must fit into an int, because it
- is smaller than REASONABLE_FILE_LIMIT */
- n = fread(pBuf, 1, (int)filesize, fp);
- v = PyMarshal_ReadObjectFromString(pBuf, n);
- if (pBuf != buf)
- PyMem_FREE(pBuf);
- return v;
- }
-
- }
+ filesize = getfilesize(fp);
+ if (filesize > 0) {
+ char buf[SMALL_FILE_LIMIT];
+ char* pBuf = NULL;
+ if (filesize <= SMALL_FILE_LIMIT)
+ pBuf = buf;
+ else if (filesize <= REASONABLE_FILE_LIMIT)
+ pBuf = (char *)PyMem_MALLOC(filesize);
+ if (pBuf != NULL) {
+ PyObject* v;
+ size_t n;
+ /* filesize must fit into an int, because it
+ is smaller than REASONABLE_FILE_LIMIT */
+ n = fread(pBuf, 1, (int)filesize, fp);
+ v = PyMarshal_ReadObjectFromString(pBuf, n);
+ if (pBuf != buf)
+ PyMem_FREE(pBuf);
+ return v;
+ }
+
+ }
#endif
- /* We don't have fstat, or we do but the file is larger than
- * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
- */
- return PyMarshal_ReadObjectFromFile(fp);
+ /* We don't have fstat, or we do but the file is larger than
+ * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
+ */
+ return PyMarshal_ReadObjectFromFile(fp);
#undef SMALL_FILE_LIMIT
#undef REASONABLE_FILE_LIMIT
@@ -1125,77 +1125,77 @@ PyMarshal_ReadLastObjectFromFile(FILE *fp)
PyObject *
PyMarshal_ReadObjectFromFile(FILE *fp)
{
- RFILE rf;
- PyObject *result;
- rf.fp = fp;
- rf.strings = PyList_New(0);
- rf.depth = 0;
- rf.ptr = rf.end = NULL;
- result = r_object(&rf);
- Py_DECREF(rf.strings);
- return result;
+ RFILE rf;
+ PyObject *result;
+ rf.fp = fp;
+ rf.strings = PyList_New(0);
+ rf.depth = 0;
+ rf.ptr = rf.end = NULL;
+ result = r_object(&rf);
+ Py_DECREF(rf.strings);
+ return result;
}
PyObject *
PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
{
- RFILE rf;
- PyObject *result;
- rf.fp = NULL;
- rf.ptr = str;
- rf.end = str + len;
- rf.strings = PyList_New(0);
- rf.depth = 0;
- result = r_object(&rf);
- Py_DECREF(rf.strings);
- return result;
+ RFILE rf;
+ PyObject *result;
+ rf.fp = NULL;
+ rf.ptr = str;
+ rf.end = str + len;
+ rf.strings = PyList_New(0);
+ rf.depth = 0;
+ result = r_object(&rf);
+ Py_DECREF(rf.strings);
+ return result;
}
PyObject *
PyMarshal_WriteObjectToString(PyObject *x, int version)
{
- WFILE wf;
- PyObject *res = NULL;
-
- wf.fp = NULL;
- wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
- if (wf.str == NULL)
- return NULL;
- wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
- wf.end = wf.ptr + PyBytes_Size(wf.str);
- wf.error = WFERR_OK;
- wf.depth = 0;
- wf.version = version;
- wf.strings = (version > 0) ? PyDict_New() : NULL;
- w_object(x, &wf);
- Py_XDECREF(wf.strings);
- if (wf.str != NULL) {
- char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
- if (wf.ptr - base > PY_SSIZE_T_MAX) {
- Py_DECREF(wf.str);
- PyErr_SetString(PyExc_OverflowError,
- "too much marshal data for a string");
- return NULL;
- }
- if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
- return NULL;
- }
- if (wf.error != WFERR_OK) {
- Py_XDECREF(wf.str);
- if (wf.error == WFERR_NOMEMORY)
- PyErr_NoMemory();
- else
- PyErr_SetString(PyExc_ValueError,
- (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
- :"object too deeply nested to marshal");
- return NULL;
- }
- if (wf.str != NULL) {
- /* XXX Quick hack -- need to do this differently */
- res = PyBytes_FromObject(wf.str);
- Py_DECREF(wf.str);
- }
- return res;
+ WFILE wf;
+ PyObject *res = NULL;
+
+ wf.fp = NULL;
+ wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
+ if (wf.str == NULL)
+ return NULL;
+ wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
+ wf.end = wf.ptr + PyBytes_Size(wf.str);
+ wf.error = WFERR_OK;
+ wf.depth = 0;
+ wf.version = version;
+ wf.strings = (version > 0) ? PyDict_New() : NULL;
+ w_object(x, &wf);
+ Py_XDECREF(wf.strings);
+ if (wf.str != NULL) {
+ char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
+ if (wf.ptr - base > PY_SSIZE_T_MAX) {
+ Py_DECREF(wf.str);
+ PyErr_SetString(PyExc_OverflowError,
+ "too much marshal data for a string");
+ return NULL;
+ }
+ if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
+ return NULL;
+ }
+ if (wf.error != WFERR_OK) {
+ Py_XDECREF(wf.str);
+ if (wf.error == WFERR_NOMEMORY)
+ PyErr_NoMemory();
+ else
+ PyErr_SetString(PyExc_ValueError,
+ (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
+ :"object too deeply nested to marshal");
+ return NULL;
+ }
+ if (wf.str != NULL) {
+ /* XXX Quick hack -- need to do this differently */
+ res = PyBytes_FromObject(wf.str);
+ Py_DECREF(wf.str);
+ }
+ return res;
}
/* And an interface for Python programs... */
@@ -1203,20 +1203,20 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
static PyObject *
marshal_dump(PyObject *self, PyObject *args)
{
- /* XXX Quick hack -- need to do this differently */
- PyObject *x;
- PyObject *f;
- int version = Py_MARSHAL_VERSION;
- PyObject *s;
- PyObject *res;
- if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
- return NULL;
- s = PyMarshal_WriteObjectToString(x, version);
- if (s == NULL)
- return NULL;
- res = PyObject_CallMethod(f, "write", "O", s);
- Py_DECREF(s);
- return res;
+ /* XXX Quick hack -- need to do this differently */
+ PyObject *x;
+ PyObject *f;
+ int version = Py_MARSHAL_VERSION;
+ PyObject *s;
+ PyObject *res;
+ if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
+ return NULL;
+ s = PyMarshal_WriteObjectToString(x, version);
+ if (s == NULL)
+ return NULL;
+ res = PyObject_CallMethod(f, "write", "O", s);
+ Py_DECREF(s);
+ return res;
}
PyDoc_STRVAR(dump_doc,
@@ -1235,35 +1235,35 @@ The version argument indicates the data format that dump should use.");
static PyObject *
marshal_load(PyObject *self, PyObject *f)
{
- /* XXX Quick hack -- need to do this differently */
- PyObject *data, *result;
- RFILE rf;
- data = PyObject_CallMethod(f, "read", "");
- if (data == NULL)
- return NULL;
- rf.fp = NULL;
- if (PyBytes_Check(data)) {
- rf.ptr = PyBytes_AS_STRING(data);
- rf.end = rf.ptr + PyBytes_GET_SIZE(data);
- }
- else if (PyBytes_Check(data)) {
- rf.ptr = PyBytes_AS_STRING(data);
- rf.end = rf.ptr + PyBytes_GET_SIZE(data);
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "f.read() returned neither string "
- "nor bytes but %.100s",
- data->ob_type->tp_name);
- Py_DECREF(data);
- return NULL;
- }
- rf.strings = PyList_New(0);
- rf.depth = 0;
- result = read_object(&rf);
- Py_DECREF(rf.strings);
- Py_DECREF(data);
- return result;
+ /* XXX Quick hack -- need to do this differently */
+ PyObject *data, *result;
+ RFILE rf;
+ data = PyObject_CallMethod(f, "read", "");
+ if (data == NULL)
+ return NULL;
+ rf.fp = NULL;
+ if (PyBytes_Check(data)) {
+ rf.ptr = PyBytes_AS_STRING(data);
+ rf.end = rf.ptr + PyBytes_GET_SIZE(data);
+ }
+ else if (PyBytes_Check(data)) {
+ rf.ptr = PyBytes_AS_STRING(data);
+ rf.end = rf.ptr + PyBytes_GET_SIZE(data);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "f.read() returned neither string "
+ "nor bytes but %.100s",
+ data->ob_type->tp_name);
+ Py_DECREF(data);
+ return NULL;
+ }
+ rf.strings = PyList_New(0);
+ rf.depth = 0;
+ result = read_object(&rf);
+ Py_DECREF(rf.strings);
+ Py_DECREF(data);
+ return result;
}
PyDoc_STRVAR(load_doc,
@@ -1282,11 +1282,11 @@ dump(), load() will substitute None for the unmarshallable type.");
static PyObject *
marshal_dumps(PyObject *self, PyObject *args)
{
- PyObject *x;
- int version = Py_MARSHAL_VERSION;
- if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
- return NULL;
- return PyMarshal_WriteObjectToString(x, version);
+ PyObject *x;
+ int version = Py_MARSHAL_VERSION;
+ if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
+ return NULL;
+ return PyMarshal_WriteObjectToString(x, version);
}
PyDoc_STRVAR(dumps_doc,
@@ -1302,24 +1302,24 @@ The version argument indicates the data format that dumps should use.");
static PyObject *
marshal_loads(PyObject *self, PyObject *args)
{
- RFILE rf;
- Py_buffer p;
- char *s;
- Py_ssize_t n;
- PyObject* result;
- if (!PyArg_ParseTuple(args, "s*:loads", &p))
- return NULL;
- s = p.buf;
- n = p.len;
- rf.fp = NULL;
- rf.ptr = s;
- rf.end = s + n;
- rf.strings = PyList_New(0);
- rf.depth = 0;
- result = read_object(&rf);
- Py_DECREF(rf.strings);
- PyBuffer_Release(&p);
- return result;
+ RFILE rf;
+ Py_buffer p;
+ char *s;
+ Py_ssize_t n;
+ PyObject* result;
+ if (!PyArg_ParseTuple(args, "s*:loads", &p))
+ return NULL;
+ s = p.buf;
+ n = p.len;
+ rf.fp = NULL;
+ rf.ptr = s;
+ rf.end = s + n;
+ rf.strings = PyList_New(0);
+ rf.depth = 0;
+ result = read_object(&rf);
+ Py_DECREF(rf.strings);
+ PyBuffer_Release(&p);
+ return result;
}
PyDoc_STRVAR(loads_doc,
@@ -1330,11 +1330,11 @@ EOFError, ValueError or TypeError. Extra characters in the string are\n\
ignored.");
static PyMethodDef marshal_methods[] = {
- {"dump", marshal_dump, METH_VARARGS, dump_doc},
- {"load", marshal_load, METH_O, load_doc},
- {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
- {"loads", marshal_loads, METH_VARARGS, loads_doc},
- {NULL, NULL} /* sentinel */
+ {"dump", marshal_dump, METH_VARARGS, dump_doc},
+ {"load", marshal_load, METH_O, load_doc},
+ {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
+ {"loads", marshal_loads, METH_VARARGS, loads_doc},
+ {NULL, NULL} /* sentinel */
};
@@ -1369,23 +1369,23 @@ loads() -- read value from a string");
static struct PyModuleDef marshalmodule = {
- PyModuleDef_HEAD_INIT,
- "marshal",
- module_doc,
- 0,
- marshal_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "marshal",
+ module_doc,
+ 0,
+ marshal_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyMODINIT_FUNC
PyMarshal_Init(void)
{
- PyObject *mod = PyModule_Create(&marshalmodule);
- if (mod == NULL)
- return NULL;
- PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
- return mod;
+ PyObject *mod = PyModule_Create(&marshalmodule);
+ if (mod == NULL)
+ return NULL;
+ PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
+ return mod;
}
diff --git a/Python/modsupport.c b/Python/modsupport.c
index 0cbc6f7..f405bae 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -16,41 +16,41 @@ char *_Py_PackageContext = NULL;
static int
countformat(const char *format, int endchar)
{
- int count = 0;
- int level = 0;
- while (level > 0 || *format != endchar) {
- switch (*format) {
- case '\0':
- /* Premature end */
- PyErr_SetString(PyExc_SystemError,
- "unmatched paren in format");
- return -1;
- case '(':
- case '[':
- case '{':
- if (level == 0)
- count++;
- level++;
- break;
- case ')':
- case ']':
- case '}':
- level--;
- break;
- case '#':
- case '&':
- case ',':
- case ':':
- case ' ':
- case '\t':
- break;
- default:
- if (level == 0)
- count++;
- }
- format++;
- }
- return count;
+ int count = 0;
+ int level = 0;
+ while (level > 0 || *format != endchar) {
+ switch (*format) {
+ case '\0':
+ /* Premature end */
+ PyErr_SetString(PyExc_SystemError,
+ "unmatched paren in format");
+ return -1;
+ case '(':
+ case '[':
+ case '{':
+ if (level == 0)
+ count++;
+ level++;
+ break;
+ case ')':
+ case ']':
+ case '}':
+ level--;
+ break;
+ case '#':
+ case '&':
+ case ',':
+ case ':':
+ case ' ':
+ case '\t':
+ break;
+ default:
+ if (level == 0)
+ count++;
+ }
+ format++;
+ }
+ return count;
}
@@ -66,552 +66,552 @@ static PyObject *do_mkvalue(const char**, va_list *, int);
static PyObject *
do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
{
- PyObject *d;
- int i;
- int itemfailed = 0;
- if (n < 0)
- return NULL;
- if ((d = PyDict_New()) == NULL)
- return NULL;
- /* Note that we can't bail immediately on error as this will leak
- refcounts on any 'N' arguments. */
- for (i = 0; i < n; i+= 2) {
- PyObject *k, *v;
- int err;
- k = do_mkvalue(p_format, p_va, flags);
- if (k == NULL) {
- itemfailed = 1;
- Py_INCREF(Py_None);
- k = Py_None;
- }
- v = do_mkvalue(p_format, p_va, flags);
- if (v == NULL) {
- itemfailed = 1;
- Py_INCREF(Py_None);
- v = Py_None;
- }
- err = PyDict_SetItem(d, k, v);
- Py_DECREF(k);
- Py_DECREF(v);
- if (err < 0 || itemfailed) {
- Py_DECREF(d);
- return NULL;
- }
- }
- if (d != NULL && **p_format != endchar) {
- Py_DECREF(d);
- d = NULL;
- PyErr_SetString(PyExc_SystemError,
- "Unmatched paren in format");
- }
- else if (endchar)
- ++*p_format;
- return d;
+ PyObject *d;
+ int i;
+ int itemfailed = 0;
+ if (n < 0)
+ return NULL;
+ if ((d = PyDict_New()) == NULL)
+ return NULL;
+ /* Note that we can't bail immediately on error as this will leak
+ refcounts on any 'N' arguments. */
+ for (i = 0; i < n; i+= 2) {
+ PyObject *k, *v;
+ int err;
+ k = do_mkvalue(p_format, p_va, flags);
+ if (k == NULL) {
+ itemfailed = 1;
+ Py_INCREF(Py_None);
+ k = Py_None;
+ }
+ v = do_mkvalue(p_format, p_va, flags);
+ if (v == NULL) {
+ itemfailed = 1;
+ Py_INCREF(Py_None);
+ v = Py_None;
+ }
+ err = PyDict_SetItem(d, k, v);
+ Py_DECREF(k);
+ Py_DECREF(v);
+ if (err < 0 || itemfailed) {
+ Py_DECREF(d);
+ return NULL;
+ }
+ }
+ if (d != NULL && **p_format != endchar) {
+ Py_DECREF(d);
+ d = NULL;
+ PyErr_SetString(PyExc_SystemError,
+ "Unmatched paren in format");
+ }
+ else if (endchar)
+ ++*p_format;
+ return d;
}
static PyObject *
do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
{
- PyObject *v;
- int i;
- int itemfailed = 0;
- if (n < 0)
- return NULL;
- v = PyList_New(n);
- if (v == NULL)
- return NULL;
- /* Note that we can't bail immediately on error as this will leak
- refcounts on any 'N' arguments. */
- for (i = 0; i < n; i++) {
- PyObject *w = do_mkvalue(p_format, p_va, flags);
- if (w == NULL) {
- itemfailed = 1;
- Py_INCREF(Py_None);
- w = Py_None;
- }
- PyList_SET_ITEM(v, i, w);
- }
-
- if (itemfailed) {
- /* do_mkvalue() should have already set an error */
- Py_DECREF(v);
- return NULL;
- }
- if (**p_format != endchar) {
- Py_DECREF(v);
- PyErr_SetString(PyExc_SystemError,
- "Unmatched paren in format");
- return NULL;
- }
- if (endchar)
- ++*p_format;
- return v;
+ PyObject *v;
+ int i;
+ int itemfailed = 0;
+ if (n < 0)
+ return NULL;
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ /* Note that we can't bail immediately on error as this will leak
+ refcounts on any 'N' arguments. */
+ for (i = 0; i < n; i++) {
+ PyObject *w = do_mkvalue(p_format, p_va, flags);
+ if (w == NULL) {
+ itemfailed = 1;
+ Py_INCREF(Py_None);
+ w = Py_None;
+ }
+ PyList_SET_ITEM(v, i, w);
+ }
+
+ if (itemfailed) {
+ /* do_mkvalue() should have already set an error */
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (**p_format != endchar) {
+ Py_DECREF(v);
+ PyErr_SetString(PyExc_SystemError,
+ "Unmatched paren in format");
+ return NULL;
+ }
+ if (endchar)
+ ++*p_format;
+ return v;
}
static int
_ustrlen(Py_UNICODE *u)
{
- int i = 0;
- Py_UNICODE *v = u;
- while (*v != 0) { i++; v++; }
- return i;
+ int i = 0;
+ Py_UNICODE *v = u;
+ while (*v != 0) { i++; v++; }
+ return i;
}
static PyObject *
do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
{
- PyObject *v;
- int i;
- int itemfailed = 0;
- if (n < 0)
- return NULL;
- if ((v = PyTuple_New(n)) == NULL)
- return NULL;
- /* Note that we can't bail immediately on error as this will leak
- refcounts on any 'N' arguments. */
- for (i = 0; i < n; i++) {
- PyObject *w = do_mkvalue(p_format, p_va, flags);
- if (w == NULL) {
- itemfailed = 1;
- Py_INCREF(Py_None);
- w = Py_None;
- }
- PyTuple_SET_ITEM(v, i, w);
- }
- if (itemfailed) {
- /* do_mkvalue() should have already set an error */
- Py_DECREF(v);
- return NULL;
- }
- if (**p_format != endchar) {
- Py_DECREF(v);
- PyErr_SetString(PyExc_SystemError,
- "Unmatched paren in format");
- return NULL;
- }
- if (endchar)
- ++*p_format;
- return v;
+ PyObject *v;
+ int i;
+ int itemfailed = 0;
+ if (n < 0)
+ return NULL;
+ if ((v = PyTuple_New(n)) == NULL)
+ return NULL;
+ /* Note that we can't bail immediately on error as this will leak
+ refcounts on any 'N' arguments. */
+ for (i = 0; i < n; i++) {
+ PyObject *w = do_mkvalue(p_format, p_va, flags);
+ if (w == NULL) {
+ itemfailed = 1;
+ Py_INCREF(Py_None);
+ w = Py_None;
+ }
+ PyTuple_SET_ITEM(v, i, w);
+ }
+ if (itemfailed) {
+ /* do_mkvalue() should have already set an error */
+ Py_DECREF(v);
+ return NULL;
+ }
+ if (**p_format != endchar) {
+ Py_DECREF(v);
+ PyErr_SetString(PyExc_SystemError,
+ "Unmatched paren in format");
+ return NULL;
+ }
+ if (endchar)
+ ++*p_format;
+ return v;
}
static PyObject *
do_mkvalue(const char **p_format, va_list *p_va, int flags)
{
- for (;;) {
- switch (*(*p_format)++) {
- case '(':
- return do_mktuple(p_format, p_va, ')',
- countformat(*p_format, ')'), flags);
-
- case '[':
- return do_mklist(p_format, p_va, ']',
- countformat(*p_format, ']'), flags);
-
- case '{':
- return do_mkdict(p_format, p_va, '}',
- countformat(*p_format, '}'), flags);
-
- case 'b':
- case 'B':
- case 'h':
- case 'i':
- return PyLong_FromLong((long)va_arg(*p_va, int));
-
- case 'H':
- return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
-
- case 'I':
- {
- unsigned int n;
- n = va_arg(*p_va, unsigned int);
- return PyLong_FromUnsignedLong(n);
- }
-
- case 'n':
+ for (;;) {
+ switch (*(*p_format)++) {
+ case '(':
+ return do_mktuple(p_format, p_va, ')',
+ countformat(*p_format, ')'), flags);
+
+ case '[':
+ return do_mklist(p_format, p_va, ']',
+ countformat(*p_format, ']'), flags);
+
+ case '{':
+ return do_mkdict(p_format, p_va, '}',
+ countformat(*p_format, '}'), flags);
+
+ case 'b':
+ case 'B':
+ case 'h':
+ case 'i':
+ return PyLong_FromLong((long)va_arg(*p_va, int));
+
+ case 'H':
+ return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
+
+ case 'I':
+ {
+ unsigned int n;
+ n = va_arg(*p_va, unsigned int);
+ return PyLong_FromUnsignedLong(n);
+ }
+
+ case 'n':
#if SIZEOF_SIZE_T!=SIZEOF_LONG
- return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
+ return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
#endif
- /* Fall through from 'n' to 'l' if Py_ssize_t is long */
- case 'l':
- return PyLong_FromLong(va_arg(*p_va, long));
+ /* Fall through from 'n' to 'l' if Py_ssize_t is long */
+ case 'l':
+ return PyLong_FromLong(va_arg(*p_va, long));
- case 'k':
- {
- unsigned long n;
- n = va_arg(*p_va, unsigned long);
- return PyLong_FromUnsignedLong(n);
- }
+ case 'k':
+ {
+ unsigned long n;
+ n = va_arg(*p_va, unsigned long);
+ return PyLong_FromUnsignedLong(n);
+ }
#ifdef HAVE_LONG_LONG
- case 'L':
- return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
+ case 'L':
+ return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
- case 'K':
- return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
+ case 'K':
+ return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
#endif
- case 'u':
- {
- PyObject *v;
- Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
- Py_ssize_t n;
- if (**p_format == '#') {
- ++*p_format;
- if (flags & FLAG_SIZE_T)
- n = va_arg(*p_va, Py_ssize_t);
- else
- n = va_arg(*p_va, int);
- }
- else
- n = -1;
- if (u == NULL) {
- v = Py_None;
- Py_INCREF(v);
- }
- else {
- if (n < 0)
- n = _ustrlen(u);
- v = PyUnicode_FromUnicode(u, n);
- }
- return v;
- }
- case 'f':
- case 'd':
- return PyFloat_FromDouble(
- (double)va_arg(*p_va, va_double));
+ case 'u':
+ {
+ PyObject *v;
+ Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
+ Py_ssize_t n;
+ if (**p_format == '#') {
+ ++*p_format;
+ if (flags & FLAG_SIZE_T)
+ n = va_arg(*p_va, Py_ssize_t);
+ else
+ n = va_arg(*p_va, int);
+ }
+ else
+ n = -1;
+ if (u == NULL) {
+ v = Py_None;
+ Py_INCREF(v);
+ }
+ else {
+ if (n < 0)
+ n = _ustrlen(u);
+ v = PyUnicode_FromUnicode(u, n);
+ }
+ return v;
+ }
+ case 'f':
+ case 'd':
+ return PyFloat_FromDouble(
+ (double)va_arg(*p_va, va_double));
#ifndef WITHOUT_COMPLEX
- case 'D':
- return PyComplex_FromCComplex(
- *((Py_complex *)va_arg(*p_va, Py_complex *)));
+ case 'D':
+ return PyComplex_FromCComplex(
+ *((Py_complex *)va_arg(*p_va, Py_complex *)));
#endif /* WITHOUT_COMPLEX */
- case 'c':
- {
- char p[1];
- p[0] = (char)va_arg(*p_va, int);
- return PyBytes_FromStringAndSize(p, 1);
- }
- case 'C':
- {
- int i = va_arg(*p_va, int);
- if (i < 0 || i > PyUnicode_GetMax()) {
- PyErr_SetString(PyExc_OverflowError,
- "%c arg not in range(0x110000)");
- return NULL;
- }
- return PyUnicode_FromOrdinal(i);
- }
-
- case 's':
- case 'z':
- {
- PyObject *v;
- char *str = va_arg(*p_va, char *);
- Py_ssize_t n;
- if (**p_format == '#') {
- ++*p_format;
- if (flags & FLAG_SIZE_T)
- n = va_arg(*p_va, Py_ssize_t);
- else
- n = va_arg(*p_va, int);
- }
- else
- n = -1;
- if (str == NULL) {
- v = Py_None;
- Py_INCREF(v);
- }
- else {
- if (n < 0) {
- size_t m = strlen(str);
- if (m > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "string too long for Python string");
- return NULL;
- }
- n = (Py_ssize_t)m;
- }
- v = PyUnicode_FromStringAndSize(str, n);
- }
- return v;
- }
-
- case 'U':
- {
- PyObject *v;
- char *str = va_arg(*p_va, char *);
- Py_ssize_t n;
- if (**p_format == '#') {
- ++*p_format;
- if (flags & FLAG_SIZE_T)
- n = va_arg(*p_va, Py_ssize_t);
- else
- n = va_arg(*p_va, int);
- }
- else
- n = -1;
- if (str == NULL) {
- v = Py_None;
- Py_INCREF(v);
- }
- else {
- if (n < 0) {
- size_t m = strlen(str);
- if (m > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "string too long for Python string");
- return NULL;
- }
- n = (Py_ssize_t)m;
- }
- v = PyUnicode_FromStringAndSize(str, n);
- }
- return v;
- }
-
- case 'y':
- {
- PyObject *v;
- char *str = va_arg(*p_va, char *);
- Py_ssize_t n;
- if (**p_format == '#') {
- ++*p_format;
- if (flags & FLAG_SIZE_T)
- n = va_arg(*p_va, Py_ssize_t);
- else
- n = va_arg(*p_va, int);
- }
- else
- n = -1;
- if (str == NULL) {
- v = Py_None;
- Py_INCREF(v);
- }
- else {
- if (n < 0) {
- size_t m = strlen(str);
- if (m > PY_SSIZE_T_MAX) {
- PyErr_SetString(PyExc_OverflowError,
- "string too long for Python bytes");
- return NULL;
- }
- n = (Py_ssize_t)m;
- }
- v = PyBytes_FromStringAndSize(str, n);
- }
- return v;
- }
-
- case 'N':
- case 'S':
- case 'O':
- if (**p_format == '&') {
- typedef PyObject *(*converter)(void *);
- converter func = va_arg(*p_va, converter);
- void *arg = va_arg(*p_va, void *);
- ++*p_format;
- return (*func)(arg);
- }
- else {
- PyObject *v;
- v = va_arg(*p_va, PyObject *);
- if (v != NULL) {
- if (*(*p_format - 1) != 'N')
- Py_INCREF(v);
- }
- else if (!PyErr_Occurred())
- /* If a NULL was passed
- * because a call that should
- * have constructed a value
- * failed, that's OK, and we
- * pass the error on; but if
- * no error occurred it's not
- * clear that the caller knew
- * what she was doing. */
- PyErr_SetString(PyExc_SystemError,
- "NULL object passed to Py_BuildValue");
- return v;
- }
-
- case ':':
- case ',':
- case ' ':
- case '\t':
- break;
-
- default:
- PyErr_SetString(PyExc_SystemError,
- "bad format char passed to Py_BuildValue");
- return NULL;
-
- }
- }
+ case 'c':
+ {
+ char p[1];
+ p[0] = (char)va_arg(*p_va, int);
+ return PyBytes_FromStringAndSize(p, 1);
+ }
+ case 'C':
+ {
+ int i = va_arg(*p_va, int);
+ if (i < 0 || i > PyUnicode_GetMax()) {
+ PyErr_SetString(PyExc_OverflowError,
+ "%c arg not in range(0x110000)");
+ return NULL;
+ }
+ return PyUnicode_FromOrdinal(i);
+ }
+
+ case 's':
+ case 'z':
+ {
+ PyObject *v;
+ char *str = va_arg(*p_va, char *);
+ Py_ssize_t n;
+ if (**p_format == '#') {
+ ++*p_format;
+ if (flags & FLAG_SIZE_T)
+ n = va_arg(*p_va, Py_ssize_t);
+ else
+ n = va_arg(*p_va, int);
+ }
+ else
+ n = -1;
+ if (str == NULL) {
+ v = Py_None;
+ Py_INCREF(v);
+ }
+ else {
+ if (n < 0) {
+ size_t m = strlen(str);
+ if (m > PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "string too long for Python string");
+ return NULL;
+ }
+ n = (Py_ssize_t)m;
+ }
+ v = PyUnicode_FromStringAndSize(str, n);
+ }
+ return v;
+ }
+
+ case 'U':
+ {
+ PyObject *v;
+ char *str = va_arg(*p_va, char *);
+ Py_ssize_t n;
+ if (**p_format == '#') {
+ ++*p_format;
+ if (flags & FLAG_SIZE_T)
+ n = va_arg(*p_va, Py_ssize_t);
+ else
+ n = va_arg(*p_va, int);
+ }
+ else
+ n = -1;
+ if (str == NULL) {
+ v = Py_None;
+ Py_INCREF(v);
+ }
+ else {
+ if (n < 0) {
+ size_t m = strlen(str);
+ if (m > PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "string too long for Python string");
+ return NULL;
+ }
+ n = (Py_ssize_t)m;
+ }
+ v = PyUnicode_FromStringAndSize(str, n);
+ }
+ return v;
+ }
+
+ case 'y':
+ {
+ PyObject *v;
+ char *str = va_arg(*p_va, char *);
+ Py_ssize_t n;
+ if (**p_format == '#') {
+ ++*p_format;
+ if (flags & FLAG_SIZE_T)
+ n = va_arg(*p_va, Py_ssize_t);
+ else
+ n = va_arg(*p_va, int);
+ }
+ else
+ n = -1;
+ if (str == NULL) {
+ v = Py_None;
+ Py_INCREF(v);
+ }
+ else {
+ if (n < 0) {
+ size_t m = strlen(str);
+ if (m > PY_SSIZE_T_MAX) {
+ PyErr_SetString(PyExc_OverflowError,
+ "string too long for Python bytes");
+ return NULL;
+ }
+ n = (Py_ssize_t)m;
+ }
+ v = PyBytes_FromStringAndSize(str, n);
+ }
+ return v;
+ }
+
+ case 'N':
+ case 'S':
+ case 'O':
+ if (**p_format == '&') {
+ typedef PyObject *(*converter)(void *);
+ converter func = va_arg(*p_va, converter);
+ void *arg = va_arg(*p_va, void *);
+ ++*p_format;
+ return (*func)(arg);
+ }
+ else {
+ PyObject *v;
+ v = va_arg(*p_va, PyObject *);
+ if (v != NULL) {
+ if (*(*p_format - 1) != 'N')
+ Py_INCREF(v);
+ }
+ else if (!PyErr_Occurred())
+ /* If a NULL was passed
+ * because a call that should
+ * have constructed a value
+ * failed, that's OK, and we
+ * pass the error on; but if
+ * no error occurred it's not
+ * clear that the caller knew
+ * what she was doing. */
+ PyErr_SetString(PyExc_SystemError,
+ "NULL object passed to Py_BuildValue");
+ return v;
+ }
+
+ case ':':
+ case ',':
+ case ' ':
+ case '\t':
+ break;
+
+ default:
+ PyErr_SetString(PyExc_SystemError,
+ "bad format char passed to Py_BuildValue");
+ return NULL;
+
+ }
+ }
}
PyObject *
Py_BuildValue(const char *format, ...)
{
- va_list va;
- PyObject* retval;
- va_start(va, format);
- retval = va_build_value(format, va, 0);
- va_end(va);
- return retval;
+ va_list va;
+ PyObject* retval;
+ va_start(va, format);
+ retval = va_build_value(format, va, 0);
+ va_end(va);
+ return retval;
}
PyObject *
_Py_BuildValue_SizeT(const char *format, ...)
{
- va_list va;
- PyObject* retval;
- va_start(va, format);
- retval = va_build_value(format, va, FLAG_SIZE_T);
- va_end(va);
- return retval;
+ va_list va;
+ PyObject* retval;
+ va_start(va, format);
+ retval = va_build_value(format, va, FLAG_SIZE_T);
+ va_end(va);
+ return retval;
}
PyObject *
Py_VaBuildValue(const char *format, va_list va)
{
- return va_build_value(format, va, 0);
+ return va_build_value(format, va, 0);
}
PyObject *
_Py_VaBuildValue_SizeT(const char *format, va_list va)
{
- return va_build_value(format, va, FLAG_SIZE_T);
+ return va_build_value(format, va, FLAG_SIZE_T);
}
static PyObject *
va_build_value(const char *format, va_list va, int flags)
{
- const char *f = format;
- int n = countformat(f, '\0');
- va_list lva;
+ const char *f = format;
+ int n = countformat(f, '\0');
+ va_list lva;
#ifdef VA_LIST_IS_ARRAY
- memcpy(lva, va, sizeof(va_list));
+ memcpy(lva, va, sizeof(va_list));
#else
#ifdef __va_copy
- __va_copy(lva, va);
+ __va_copy(lva, va);
#else
- lva = va;
+ lva = va;
#endif
#endif
- if (n < 0)
- return NULL;
- if (n == 0) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- if (n == 1)
- return do_mkvalue(&f, &lva, flags);
- return do_mktuple(&f, &lva, '\0', n, flags);
+ if (n < 0)
+ return NULL;
+ if (n == 0) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ if (n == 1)
+ return do_mkvalue(&f, &lva, flags);
+ return do_mktuple(&f, &lva, '\0', n, flags);
}
PyObject *
PyEval_CallFunction(PyObject *obj, const char *format, ...)
{
- va_list vargs;
- PyObject *args;
- PyObject *res;
+ va_list vargs;
+ PyObject *args;
+ PyObject *res;
- va_start(vargs, format);
+ va_start(vargs, format);
- args = Py_VaBuildValue(format, vargs);
- va_end(vargs);
+ args = Py_VaBuildValue(format, vargs);
+ va_end(vargs);
- if (args == NULL)
- return NULL;
+ if (args == NULL)
+ return NULL;
- res = PyEval_CallObject(obj, args);
- Py_DECREF(args);
+ res = PyEval_CallObject(obj, args);
+ Py_DECREF(args);
- return res;
+ return res;
}
PyObject *
PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
{
- va_list vargs;
- PyObject *meth;
- PyObject *args;
- PyObject *res;
+ va_list vargs;
+ PyObject *meth;
+ PyObject *args;
+ PyObject *res;
- meth = PyObject_GetAttrString(obj, methodname);
- if (meth == NULL)
- return NULL;
+ meth = PyObject_GetAttrString(obj, methodname);
+ if (meth == NULL)
+ return NULL;
- va_start(vargs, format);
+ va_start(vargs, format);
- args = Py_VaBuildValue(format, vargs);
- va_end(vargs);
+ args = Py_VaBuildValue(format, vargs);
+ va_end(vargs);
- if (args == NULL) {
- Py_DECREF(meth);
- return NULL;
- }
+ if (args == NULL) {
+ Py_DECREF(meth);
+ return NULL;
+ }
- res = PyEval_CallObject(meth, args);
- Py_DECREF(meth);
- Py_DECREF(args);
+ res = PyEval_CallObject(meth, args);
+ Py_DECREF(meth);
+ Py_DECREF(args);
- return res;
+ return res;
}
int
PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
{
- PyObject *dict;
- if (!PyModule_Check(m)) {
- PyErr_SetString(PyExc_TypeError,
- "PyModule_AddObject() needs module as first arg");
- return -1;
- }
- if (!o) {
- if (!PyErr_Occurred())
- PyErr_SetString(PyExc_TypeError,
- "PyModule_AddObject() needs non-NULL value");
- return -1;
- }
-
- dict = PyModule_GetDict(m);
- if (dict == NULL) {
- /* Internal error -- modules must have a dict! */
- PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
- PyModule_GetName(m));
- return -1;
- }
- if (PyDict_SetItemString(dict, name, o))
- return -1;
- Py_DECREF(o);
- return 0;
+ PyObject *dict;
+ if (!PyModule_Check(m)) {
+ PyErr_SetString(PyExc_TypeError,
+ "PyModule_AddObject() needs module as first arg");
+ return -1;
+ }
+ if (!o) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_TypeError,
+ "PyModule_AddObject() needs non-NULL value");
+ return -1;
+ }
+
+ dict = PyModule_GetDict(m);
+ if (dict == NULL) {
+ /* Internal error -- modules must have a dict! */
+ PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
+ PyModule_GetName(m));
+ return -1;
+ }
+ if (PyDict_SetItemString(dict, name, o))
+ return -1;
+ Py_DECREF(o);
+ return 0;
}
-int
+int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
- PyObject *o = PyLong_FromLong(value);
- if (!o)
- return -1;
- if (PyModule_AddObject(m, name, o) == 0)
- return 0;
- Py_DECREF(o);
- return -1;
+ PyObject *o = PyLong_FromLong(value);
+ if (!o)
+ return -1;
+ if (PyModule_AddObject(m, name, o) == 0)
+ return 0;
+ Py_DECREF(o);
+ return -1;
}
-int
+int
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
{
- PyObject *o = PyUnicode_FromString(value);
- if (!o)
- return -1;
- if (PyModule_AddObject(m, name, o) == 0)
- return 0;
- Py_DECREF(o);
- return -1;
+ PyObject *o = PyUnicode_FromString(value);
+ if (!o)
+ return -1;
+ if (PyModule_AddObject(m, name, o) == 0)
+ return 0;
+ Py_DECREF(o);
+ return -1;
}
diff --git a/Python/mysnprintf.c b/Python/mysnprintf.c
index 8cd412f..a08e249 100644
--- a/Python/mysnprintf.c
+++ b/Python/mysnprintf.c
@@ -19,20 +19,20 @@
Return value (rv):
- When 0 <= rv < size, the output conversion was unexceptional, and
- rv characters were written to str (excluding a trailing \0 byte at
- str[rv]).
+ When 0 <= rv < size, the output conversion was unexceptional, and
+ rv characters were written to str (excluding a trailing \0 byte at
+ str[rv]).
- When rv >= size, output conversion was truncated, and a buffer of
- size rv+1 would have been needed to avoid truncation. str[size-1]
- is \0 in this case.
+ When rv >= size, output conversion was truncated, and a buffer of
+ size rv+1 would have been needed to avoid truncation. str[size-1]
+ is \0 in this case.
- When rv < 0, "something bad happened". str[size-1] is \0 in this
- case too, but the rest of str is unreliable. It could be that
- an error in format codes was detected by libc, or on platforms
- with a non-C99 vsnprintf simply that the buffer wasn't big enough
- to avoid truncation, or on platforms without any vsnprintf that
- PyMem_Malloc couldn't obtain space for a temp buffer.
+ When rv < 0, "something bad happened". str[size-1] is \0 in this
+ case too, but the rest of str is unreliable. It could be that
+ an error in format codes was detected by libc, or on platforms
+ with a non-C99 vsnprintf simply that the buffer wasn't big enough
+ to avoid truncation, or on platforms without any vsnprintf that
+ PyMem_Malloc couldn't obtain space for a temp buffer.
CAUTION: Unlike C99, str != NULL and size > 0 are required.
*/
@@ -40,65 +40,65 @@
int
PyOS_snprintf(char *str, size_t size, const char *format, ...)
{
- int rc;
- va_list va;
+ int rc;
+ va_list va;
- va_start(va, format);
- rc = PyOS_vsnprintf(str, size, format, va);
- va_end(va);
- return rc;
+ va_start(va, format);
+ rc = PyOS_vsnprintf(str, size, format, va);
+ va_end(va);
+ return rc;
}
int
PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
{
- int len; /* # bytes written, excluding \0 */
+ int len; /* # bytes written, excluding \0 */
#ifdef HAVE_SNPRINTF
#define _PyOS_vsnprintf_EXTRA_SPACE 1
#else
#define _PyOS_vsnprintf_EXTRA_SPACE 512
- char *buffer;
+ char *buffer;
#endif
- assert(str != NULL);
- assert(size > 0);
- assert(format != NULL);
- /* We take a size_t as input but return an int. Sanity check
- * our input so that it won't cause an overflow in the
- * vsnprintf return value or the buffer malloc size. */
- if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
- len = -666;
- goto Done;
- }
+ assert(str != NULL);
+ assert(size > 0);
+ assert(format != NULL);
+ /* We take a size_t as input but return an int. Sanity check
+ * our input so that it won't cause an overflow in the
+ * vsnprintf return value or the buffer malloc size. */
+ if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
+ len = -666;
+ goto Done;
+ }
#ifdef HAVE_SNPRINTF
- len = vsnprintf(str, size, format, va);
+ len = vsnprintf(str, size, format, va);
#else
- /* Emulate it. */
- buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
- if (buffer == NULL) {
- len = -666;
- goto Done;
- }
-
- len = vsprintf(buffer, format, va);
- if (len < 0)
- /* ignore the error */;
-
- else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
- Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
-
- else {
- const size_t to_copy = (size_t)len < size ?
- (size_t)len : size - 1;
- assert(to_copy < size);
- memcpy(str, buffer, to_copy);
- str[to_copy] = '\0';
- }
- PyMem_FREE(buffer);
+ /* Emulate it. */
+ buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
+ if (buffer == NULL) {
+ len = -666;
+ goto Done;
+ }
+
+ len = vsprintf(buffer, format, va);
+ if (len < 0)
+ /* ignore the error */;
+
+ else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
+ Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
+
+ else {
+ const size_t to_copy = (size_t)len < size ?
+ (size_t)len : size - 1;
+ assert(to_copy < size);
+ memcpy(str, buffer, to_copy);
+ str[to_copy] = '\0';
+ }
+ PyMem_FREE(buffer);
#endif
Done:
- if (size > 0)
- str[size-1] = '\0';
- return len;
+ if (size > 0)
+ str[size-1] = '\0';
+ return len;
#undef _PyOS_vsnprintf_EXTRA_SPACE
}
diff --git a/Python/mystrtoul.c b/Python/mystrtoul.c
index 347f361..52502cb 100644
--- a/Python/mystrtoul.c
+++ b/Python/mystrtoul.c
@@ -18,43 +18,43 @@
* i * base doesn't overflow unsigned long.
*/
static unsigned long smallmax[] = {
- 0, /* bases 0 and 1 are invalid */
- 0,
- ULONG_MAX / 2,
- ULONG_MAX / 3,
- ULONG_MAX / 4,
- ULONG_MAX / 5,
- ULONG_MAX / 6,
- ULONG_MAX / 7,
- ULONG_MAX / 8,
- ULONG_MAX / 9,
- ULONG_MAX / 10,
- ULONG_MAX / 11,
- ULONG_MAX / 12,
- ULONG_MAX / 13,
- ULONG_MAX / 14,
- ULONG_MAX / 15,
- ULONG_MAX / 16,
- ULONG_MAX / 17,
- ULONG_MAX / 18,
- ULONG_MAX / 19,
- ULONG_MAX / 20,
- ULONG_MAX / 21,
- ULONG_MAX / 22,
- ULONG_MAX / 23,
- ULONG_MAX / 24,
- ULONG_MAX / 25,
- ULONG_MAX / 26,
- ULONG_MAX / 27,
- ULONG_MAX / 28,
- ULONG_MAX / 29,
- ULONG_MAX / 30,
- ULONG_MAX / 31,
- ULONG_MAX / 32,
- ULONG_MAX / 33,
- ULONG_MAX / 34,
- ULONG_MAX / 35,
- ULONG_MAX / 36,
+ 0, /* bases 0 and 1 are invalid */
+ 0,
+ ULONG_MAX / 2,
+ ULONG_MAX / 3,
+ ULONG_MAX / 4,
+ ULONG_MAX / 5,
+ ULONG_MAX / 6,
+ ULONG_MAX / 7,
+ ULONG_MAX / 8,
+ ULONG_MAX / 9,
+ ULONG_MAX / 10,
+ ULONG_MAX / 11,
+ ULONG_MAX / 12,
+ ULONG_MAX / 13,
+ ULONG_MAX / 14,
+ ULONG_MAX / 15,
+ ULONG_MAX / 16,
+ ULONG_MAX / 17,
+ ULONG_MAX / 18,
+ ULONG_MAX / 19,
+ ULONG_MAX / 20,
+ ULONG_MAX / 21,
+ ULONG_MAX / 22,
+ ULONG_MAX / 23,
+ ULONG_MAX / 24,
+ ULONG_MAX / 25,
+ ULONG_MAX / 26,
+ ULONG_MAX / 27,
+ ULONG_MAX / 28,
+ ULONG_MAX / 29,
+ ULONG_MAX / 30,
+ ULONG_MAX / 31,
+ ULONG_MAX / 32,
+ ULONG_MAX / 33,
+ ULONG_MAX / 34,
+ ULONG_MAX / 35,
+ ULONG_MAX / 36,
};
/* maximum digits that can't ever overflow for bases 2 through 36,
@@ -63,229 +63,229 @@ static unsigned long smallmax[] = {
*/
#if SIZEOF_LONG == 4
static int digitlimit[] = {
- 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
- 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
- 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
- 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
+ 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */
+ 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */
+ 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */
+ 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */
#elif SIZEOF_LONG == 8
/* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */
static int digitlimit[] = {
- 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
- 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
- 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
- 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
+ 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */
+ 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */
+ 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */
+ 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */
#else
#error "Need table for SIZEOF_LONG"
#endif
/*
-** strtoul
-** This is a general purpose routine for converting
-** an ascii string to an integer in an arbitrary base.
-** Leading white space is ignored. If 'base' is zero
-** it looks for a leading 0b, 0o or 0x to tell which
-** base. If these are absent it defaults to 10.
-** Base must be 0 or between 2 and 36 (inclusive).
-** If 'ptr' is non-NULL it will contain a pointer to
-** the end of the scan.
-** Errors due to bad pointers will probably result in
-** exceptions - we don't check for them.
+** strtoul
+** This is a general purpose routine for converting
+** an ascii string to an integer in an arbitrary base.
+** Leading white space is ignored. If 'base' is zero
+** it looks for a leading 0b, 0o or 0x to tell which
+** base. If these are absent it defaults to 10.
+** Base must be 0 or between 2 and 36 (inclusive).
+** If 'ptr' is non-NULL it will contain a pointer to
+** the end of the scan.
+** Errors due to bad pointers will probably result in
+** exceptions - we don't check for them.
*/
unsigned long
PyOS_strtoul(register char *str, char **ptr, int base)
{
- register unsigned long result = 0; /* return value of the function */
- register int c; /* current input character */
- register int ovlimit; /* required digits to overflow */
+ register unsigned long result = 0; /* return value of the function */
+ register int c; /* current input character */
+ register int ovlimit; /* required digits to overflow */
- /* skip leading white space */
- while (*str && isspace(Py_CHARMASK(*str)))
- ++str;
+ /* skip leading white space */
+ while (*str && isspace(Py_CHARMASK(*str)))
+ ++str;
- /* check for leading 0b, 0o or 0x for auto-base or base 16 */
- switch (base) {
- case 0: /* look for leading 0b, 0o or 0x */
- if (*str == '0') {
- ++str;
- if (*str == 'x' || *str == 'X') {
- /* there must be at least one digit after 0x */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- base = 16;
- } else if (*str == 'o' || *str == 'O') {
- /* there must be at least one digit after 0o */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- base = 8;
- } else if (*str == 'b' || *str == 'B') {
- /* there must be at least one digit after 0b */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- base = 2;
- } else {
- /* skip all zeroes... */
- while (*str == '0')
- ++str;
- while (isspace(Py_CHARMASK(*str)))
- ++str;
- if (ptr)
- *ptr = str;
- return 0;
- }
- }
- else
- base = 10;
- break;
+ /* check for leading 0b, 0o or 0x for auto-base or base 16 */
+ switch (base) {
+ case 0: /* look for leading 0b, 0o or 0x */
+ if (*str == '0') {
+ ++str;
+ if (*str == 'x' || *str == 'X') {
+ /* there must be at least one digit after 0x */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ base = 16;
+ } else if (*str == 'o' || *str == 'O') {
+ /* there must be at least one digit after 0o */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ base = 8;
+ } else if (*str == 'b' || *str == 'B') {
+ /* there must be at least one digit after 0b */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ base = 2;
+ } else {
+ /* skip all zeroes... */
+ while (*str == '0')
+ ++str;
+ while (isspace(Py_CHARMASK(*str)))
+ ++str;
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ }
+ else
+ base = 10;
+ break;
- /* even with explicit base, skip leading 0? prefix */
- case 16:
- if (*str == '0') {
- ++str;
- if (*str == 'x' || *str == 'X') {
- /* there must be at least one digit after 0x */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- }
- }
- break;
- case 8:
- if (*str == '0') {
- ++str;
- if (*str == 'o' || *str == 'O') {
- /* there must be at least one digit after 0o */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- }
- }
- break;
- case 2:
- if(*str == '0') {
- ++str;
- if (*str == 'b' || *str == 'B') {
- /* there must be at least one digit after 0b */
- if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
- if (ptr)
- *ptr = str;
- return 0;
- }
- ++str;
- }
- }
- break;
- }
+ /* even with explicit base, skip leading 0? prefix */
+ case 16:
+ if (*str == '0') {
+ ++str;
+ if (*str == 'x' || *str == 'X') {
+ /* there must be at least one digit after 0x */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ }
+ }
+ break;
+ case 8:
+ if (*str == '0') {
+ ++str;
+ if (*str == 'o' || *str == 'O') {
+ /* there must be at least one digit after 0o */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ }
+ }
+ break;
+ case 2:
+ if(*str == '0') {
+ ++str;
+ if (*str == 'b' || *str == 'B') {
+ /* there must be at least one digit after 0b */
+ if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
+ ++str;
+ }
+ }
+ break;
+ }
- /* catch silly bases */
- if (base < 2 || base > 36) {
- if (ptr)
- *ptr = str;
- return 0;
- }
+ /* catch silly bases */
+ if (base < 2 || base > 36) {
+ if (ptr)
+ *ptr = str;
+ return 0;
+ }
- /* skip leading zeroes */
- while (*str == '0')
- ++str;
+ /* skip leading zeroes */
+ while (*str == '0')
+ ++str;
- /* base is guaranteed to be in [2, 36] at this point */
- ovlimit = digitlimit[base];
+ /* base is guaranteed to be in [2, 36] at this point */
+ ovlimit = digitlimit[base];
- /* do the conversion until non-digit character encountered */
- while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
- if (ovlimit > 0) /* no overflow check required */
- result = result * base + c;
- else { /* requires overflow check */
- register unsigned long temp_result;
+ /* do the conversion until non-digit character encountered */
+ while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
+ if (ovlimit > 0) /* no overflow check required */
+ result = result * base + c;
+ else { /* requires overflow check */
+ register unsigned long temp_result;
- if (ovlimit < 0) /* guaranteed overflow */
- goto overflowed;
+ if (ovlimit < 0) /* guaranteed overflow */
+ goto overflowed;
- /* there could be an overflow */
- /* check overflow just from shifting */
- if (result > smallmax[base])
- goto overflowed;
+ /* there could be an overflow */
+ /* check overflow just from shifting */
+ if (result > smallmax[base])
+ goto overflowed;
- result *= base;
+ result *= base;
- /* check overflow from the digit's value */
- temp_result = result + c;
- if (temp_result < result)
- goto overflowed;
+ /* check overflow from the digit's value */
+ temp_result = result + c;
+ if (temp_result < result)
+ goto overflowed;
- result = temp_result;
- }
+ result = temp_result;
+ }
- ++str;
- --ovlimit;
- }
+ ++str;
+ --ovlimit;
+ }
- /* set pointer to point to the last character scanned */
- if (ptr)
- *ptr = str;
+ /* set pointer to point to the last character scanned */
+ if (ptr)
+ *ptr = str;
- return result;
+ return result;
overflowed:
- if (ptr) {
- /* spool through remaining digit characters */
- while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
- ++str;
- *ptr = str;
- }
- errno = ERANGE;
- return (unsigned long)-1;
+ if (ptr) {
+ /* spool through remaining digit characters */
+ while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
+ ++str;
+ *ptr = str;
+ }
+ errno = ERANGE;
+ return (unsigned long)-1;
}
/* Checking for overflow in PyOS_strtol is a PITA; see comments
* about PY_ABS_LONG_MIN in longobject.c.
*/
-#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
+#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
long
PyOS_strtol(char *str, char **ptr, int base)
{
- long result;
- unsigned long uresult;
- char sign;
+ long result;
+ unsigned long uresult;
+ char sign;
- while (*str && isspace(Py_CHARMASK(*str)))
- str++;
+ while (*str && isspace(Py_CHARMASK(*str)))
+ str++;
- sign = *str;
- if (sign == '+' || sign == '-')
- str++;
+ sign = *str;
+ if (sign == '+' || sign == '-')
+ str++;
- uresult = PyOS_strtoul(str, ptr, base);
+ uresult = PyOS_strtoul(str, ptr, base);
- if (uresult <= (unsigned long)LONG_MAX) {
- result = (long)uresult;
- if (sign == '-')
- result = -result;
- }
- else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
- result = LONG_MIN;
- }
- else {
- errno = ERANGE;
- result = LONG_MAX;
- }
- return result;
+ if (uresult <= (unsigned long)LONG_MAX) {
+ result = (long)uresult;
+ if (sign == '-')
+ result = -result;
+ }
+ else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
+ result = LONG_MIN;
+ }
+ else {
+ errno = ERANGE;
+ result = LONG_MAX;
+ }
+ return result;
}
diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h
index 043e42a..c47c7c8 100644
--- a/Python/opcode_targets.h
+++ b/Python/opcode_targets.h
@@ -1,258 +1,258 @@
static void *opcode_targets[256] = {
- &&_unknown_opcode,
- &&TARGET_POP_TOP,
- &&TARGET_ROT_TWO,
- &&TARGET_ROT_THREE,
- &&TARGET_DUP_TOP,
- &&TARGET_ROT_FOUR,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_NOP,
- &&TARGET_UNARY_POSITIVE,
- &&TARGET_UNARY_NEGATIVE,
- &&TARGET_UNARY_NOT,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_UNARY_INVERT,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_BINARY_POWER,
- &&TARGET_BINARY_MULTIPLY,
- &&_unknown_opcode,
- &&TARGET_BINARY_MODULO,
- &&TARGET_BINARY_ADD,
- &&TARGET_BINARY_SUBTRACT,
- &&TARGET_BINARY_SUBSCR,
- &&TARGET_BINARY_FLOOR_DIVIDE,
- &&TARGET_BINARY_TRUE_DIVIDE,
- &&TARGET_INPLACE_FLOOR_DIVIDE,
- &&TARGET_INPLACE_TRUE_DIVIDE,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_STORE_MAP,
- &&TARGET_INPLACE_ADD,
- &&TARGET_INPLACE_SUBTRACT,
- &&TARGET_INPLACE_MULTIPLY,
- &&_unknown_opcode,
- &&TARGET_INPLACE_MODULO,
- &&TARGET_STORE_SUBSCR,
- &&TARGET_DELETE_SUBSCR,
- &&TARGET_BINARY_LSHIFT,
- &&TARGET_BINARY_RSHIFT,
- &&TARGET_BINARY_AND,
- &&TARGET_BINARY_XOR,
- &&TARGET_BINARY_OR,
- &&TARGET_INPLACE_POWER,
- &&TARGET_GET_ITER,
- &&TARGET_STORE_LOCALS,
- &&TARGET_PRINT_EXPR,
- &&TARGET_LOAD_BUILD_CLASS,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_INPLACE_LSHIFT,
- &&TARGET_INPLACE_RSHIFT,
- &&TARGET_INPLACE_AND,
- &&TARGET_INPLACE_XOR,
- &&TARGET_INPLACE_OR,
- &&TARGET_BREAK_LOOP,
- &&TARGET_WITH_CLEANUP,
- &&_unknown_opcode,
- &&TARGET_RETURN_VALUE,
- &&TARGET_IMPORT_STAR,
- &&_unknown_opcode,
- &&TARGET_YIELD_VALUE,
- &&TARGET_POP_BLOCK,
- &&TARGET_END_FINALLY,
- &&TARGET_POP_EXCEPT,
- &&TARGET_STORE_NAME,
- &&TARGET_DELETE_NAME,
- &&TARGET_UNPACK_SEQUENCE,
- &&TARGET_FOR_ITER,
- &&TARGET_UNPACK_EX,
- &&TARGET_STORE_ATTR,
- &&TARGET_DELETE_ATTR,
- &&TARGET_STORE_GLOBAL,
- &&TARGET_DELETE_GLOBAL,
- &&TARGET_DUP_TOPX,
- &&TARGET_LOAD_CONST,
- &&TARGET_LOAD_NAME,
- &&TARGET_BUILD_TUPLE,
- &&TARGET_BUILD_LIST,
- &&TARGET_BUILD_SET,
- &&TARGET_BUILD_MAP,
- &&TARGET_LOAD_ATTR,
- &&TARGET_COMPARE_OP,
- &&TARGET_IMPORT_NAME,
- &&TARGET_IMPORT_FROM,
- &&TARGET_JUMP_FORWARD,
- &&TARGET_JUMP_IF_FALSE_OR_POP,
- &&TARGET_JUMP_IF_TRUE_OR_POP,
- &&TARGET_JUMP_ABSOLUTE,
- &&TARGET_POP_JUMP_IF_FALSE,
- &&TARGET_POP_JUMP_IF_TRUE,
- &&TARGET_LOAD_GLOBAL,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_CONTINUE_LOOP,
- &&TARGET_SETUP_LOOP,
- &&TARGET_SETUP_EXCEPT,
- &&TARGET_SETUP_FINALLY,
- &&_unknown_opcode,
- &&TARGET_LOAD_FAST,
- &&TARGET_STORE_FAST,
- &&TARGET_DELETE_FAST,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_RAISE_VARARGS,
- &&TARGET_CALL_FUNCTION,
- &&TARGET_MAKE_FUNCTION,
- &&TARGET_BUILD_SLICE,
- &&TARGET_MAKE_CLOSURE,
- &&TARGET_LOAD_CLOSURE,
- &&TARGET_LOAD_DEREF,
- &&TARGET_STORE_DEREF,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&TARGET_CALL_FUNCTION_VAR,
- &&TARGET_CALL_FUNCTION_KW,
- &&TARGET_CALL_FUNCTION_VAR_KW,
- &&TARGET_EXTENDED_ARG,
- &&_unknown_opcode,
- &&TARGET_LIST_APPEND,
- &&TARGET_SET_ADD,
- &&TARGET_MAP_ADD,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode,
- &&_unknown_opcode
+ &&_unknown_opcode,
+ &&TARGET_POP_TOP,
+ &&TARGET_ROT_TWO,
+ &&TARGET_ROT_THREE,
+ &&TARGET_DUP_TOP,
+ &&TARGET_ROT_FOUR,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_NOP,
+ &&TARGET_UNARY_POSITIVE,
+ &&TARGET_UNARY_NEGATIVE,
+ &&TARGET_UNARY_NOT,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_UNARY_INVERT,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_BINARY_POWER,
+ &&TARGET_BINARY_MULTIPLY,
+ &&_unknown_opcode,
+ &&TARGET_BINARY_MODULO,
+ &&TARGET_BINARY_ADD,
+ &&TARGET_BINARY_SUBTRACT,
+ &&TARGET_BINARY_SUBSCR,
+ &&TARGET_BINARY_FLOOR_DIVIDE,
+ &&TARGET_BINARY_TRUE_DIVIDE,
+ &&TARGET_INPLACE_FLOOR_DIVIDE,
+ &&TARGET_INPLACE_TRUE_DIVIDE,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_STORE_MAP,
+ &&TARGET_INPLACE_ADD,
+ &&TARGET_INPLACE_SUBTRACT,
+ &&TARGET_INPLACE_MULTIPLY,
+ &&_unknown_opcode,
+ &&TARGET_INPLACE_MODULO,
+ &&TARGET_STORE_SUBSCR,
+ &&TARGET_DELETE_SUBSCR,
+ &&TARGET_BINARY_LSHIFT,
+ &&TARGET_BINARY_RSHIFT,
+ &&TARGET_BINARY_AND,
+ &&TARGET_BINARY_XOR,
+ &&TARGET_BINARY_OR,
+ &&TARGET_INPLACE_POWER,
+ &&TARGET_GET_ITER,
+ &&TARGET_STORE_LOCALS,
+ &&TARGET_PRINT_EXPR,
+ &&TARGET_LOAD_BUILD_CLASS,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_INPLACE_LSHIFT,
+ &&TARGET_INPLACE_RSHIFT,
+ &&TARGET_INPLACE_AND,
+ &&TARGET_INPLACE_XOR,
+ &&TARGET_INPLACE_OR,
+ &&TARGET_BREAK_LOOP,
+ &&TARGET_WITH_CLEANUP,
+ &&_unknown_opcode,
+ &&TARGET_RETURN_VALUE,
+ &&TARGET_IMPORT_STAR,
+ &&_unknown_opcode,
+ &&TARGET_YIELD_VALUE,
+ &&TARGET_POP_BLOCK,
+ &&TARGET_END_FINALLY,
+ &&TARGET_POP_EXCEPT,
+ &&TARGET_STORE_NAME,
+ &&TARGET_DELETE_NAME,
+ &&TARGET_UNPACK_SEQUENCE,
+ &&TARGET_FOR_ITER,
+ &&TARGET_UNPACK_EX,
+ &&TARGET_STORE_ATTR,
+ &&TARGET_DELETE_ATTR,
+ &&TARGET_STORE_GLOBAL,
+ &&TARGET_DELETE_GLOBAL,
+ &&TARGET_DUP_TOPX,
+ &&TARGET_LOAD_CONST,
+ &&TARGET_LOAD_NAME,
+ &&TARGET_BUILD_TUPLE,
+ &&TARGET_BUILD_LIST,
+ &&TARGET_BUILD_SET,
+ &&TARGET_BUILD_MAP,
+ &&TARGET_LOAD_ATTR,
+ &&TARGET_COMPARE_OP,
+ &&TARGET_IMPORT_NAME,
+ &&TARGET_IMPORT_FROM,
+ &&TARGET_JUMP_FORWARD,
+ &&TARGET_JUMP_IF_FALSE_OR_POP,
+ &&TARGET_JUMP_IF_TRUE_OR_POP,
+ &&TARGET_JUMP_ABSOLUTE,
+ &&TARGET_POP_JUMP_IF_FALSE,
+ &&TARGET_POP_JUMP_IF_TRUE,
+ &&TARGET_LOAD_GLOBAL,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_CONTINUE_LOOP,
+ &&TARGET_SETUP_LOOP,
+ &&TARGET_SETUP_EXCEPT,
+ &&TARGET_SETUP_FINALLY,
+ &&_unknown_opcode,
+ &&TARGET_LOAD_FAST,
+ &&TARGET_STORE_FAST,
+ &&TARGET_DELETE_FAST,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_RAISE_VARARGS,
+ &&TARGET_CALL_FUNCTION,
+ &&TARGET_MAKE_FUNCTION,
+ &&TARGET_BUILD_SLICE,
+ &&TARGET_MAKE_CLOSURE,
+ &&TARGET_LOAD_CLOSURE,
+ &&TARGET_LOAD_DEREF,
+ &&TARGET_STORE_DEREF,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&TARGET_CALL_FUNCTION_VAR,
+ &&TARGET_CALL_FUNCTION_KW,
+ &&TARGET_CALL_FUNCTION_VAR_KW,
+ &&TARGET_EXTENDED_ARG,
+ &&_unknown_opcode,
+ &&TARGET_LIST_APPEND,
+ &&TARGET_SET_ADD,
+ &&TARGET_MAP_ADD,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode,
+ &&_unknown_opcode
};
diff --git a/Python/peephole.c b/Python/peephole.c
index 0e70183..06e7fe6 100644
--- a/Python/peephole.c
+++ b/Python/peephole.c
@@ -12,256 +12,256 @@
#include "opcode.h"
#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
-#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
+#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
#define CONDITIONAL_JUMP(op) (op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
- || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
+ || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
#define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP \
- || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
- || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
+ || op==POP_JUMP_IF_FALSE || op==POP_JUMP_IF_TRUE \
+ || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP)
#define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP)
#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
#define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
#define ISBASICBLOCK(blocks, start, bytes) \
- (blocks[start]==blocks[start+bytes-1])
+ (blocks[start]==blocks[start+bytes-1])
/* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
- with LOAD_CONST (c1, c2, ... cn).
+ with LOAD_CONST (c1, c2, ... cn).
The consts table must still be in list form so that the
new constant (c1, c2, ... cn) can be appended.
Called with codestr pointing to the first LOAD_CONST.
- Bails out with no change if one or more of the LOAD_CONSTs is missing.
+ Bails out with no change if one or more of the LOAD_CONSTs is missing.
Also works for BUILD_LIST when followed by an "in" or "not in" test.
*/
static int
tuple_of_constants(unsigned char *codestr, Py_ssize_t n, PyObject *consts)
{
- PyObject *newconst, *constant;
- Py_ssize_t i, arg, len_consts;
-
- /* Pre-conditions */
- assert(PyList_CheckExact(consts));
- assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
- assert(GETARG(codestr, (n*3)) == n);
- for (i=0 ; i<n ; i++)
- assert(codestr[i*3] == LOAD_CONST);
-
- /* Buildup new tuple of constants */
- newconst = PyTuple_New(n);
- if (newconst == NULL)
- return 0;
- len_consts = PyList_GET_SIZE(consts);
- for (i=0 ; i<n ; i++) {
- arg = GETARG(codestr, (i*3));
- assert(arg < len_consts);
- constant = PyList_GET_ITEM(consts, arg);
- Py_INCREF(constant);
- PyTuple_SET_ITEM(newconst, i, constant);
- }
-
- /* Append folded constant onto consts */
- if (PyList_Append(consts, newconst)) {
- Py_DECREF(newconst);
- return 0;
- }
- Py_DECREF(newconst);
-
- /* Write NOPs over old LOAD_CONSTS and
- add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
- memset(codestr, NOP, n*3);
- codestr[n*3] = LOAD_CONST;
- SETARG(codestr, (n*3), len_consts);
- return 1;
+ PyObject *newconst, *constant;
+ Py_ssize_t i, arg, len_consts;
+
+ /* Pre-conditions */
+ assert(PyList_CheckExact(consts));
+ assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
+ assert(GETARG(codestr, (n*3)) == n);
+ for (i=0 ; i<n ; i++)
+ assert(codestr[i*3] == LOAD_CONST);
+
+ /* Buildup new tuple of constants */
+ newconst = PyTuple_New(n);
+ if (newconst == NULL)
+ return 0;
+ len_consts = PyList_GET_SIZE(consts);
+ for (i=0 ; i<n ; i++) {
+ arg = GETARG(codestr, (i*3));
+ assert(arg < len_consts);
+ constant = PyList_GET_ITEM(consts, arg);
+ Py_INCREF(constant);
+ PyTuple_SET_ITEM(newconst, i, constant);
+ }
+
+ /* Append folded constant onto consts */
+ if (PyList_Append(consts, newconst)) {
+ Py_DECREF(newconst);
+ return 0;
+ }
+ Py_DECREF(newconst);
+
+ /* Write NOPs over old LOAD_CONSTS and
+ add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
+ memset(codestr, NOP, n*3);
+ codestr[n*3] = LOAD_CONST;
+ SETARG(codestr, (n*3), len_consts);
+ return 1;
}
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
- with LOAD_CONST binop(c1,c2)
+ with LOAD_CONST binop(c1,c2)
The consts table must still be in list form so that the
new constant can be appended.
- Called with codestr pointing to the first LOAD_CONST.
- Abandons the transformation if the folding fails (i.e. 1+'a').
+ Called with codestr pointing to the first LOAD_CONST.
+ Abandons the transformation if the folding fails (i.e. 1+'a').
If the new constant is a sequence, only folds when the size
- is below a threshold value. That keeps pyc files from
- becoming large in the presence of code like: (None,)*1000.
+ is below a threshold value. That keeps pyc files from
+ becoming large in the presence of code like: (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
{
- PyObject *newconst, *v, *w;
- Py_ssize_t len_consts, size;
- int opcode;
-
- /* Pre-conditions */
- assert(PyList_CheckExact(consts));
- assert(codestr[0] == LOAD_CONST);
- assert(codestr[3] == LOAD_CONST);
-
- /* Create new constant */
- v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
- w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
- opcode = codestr[6];
- switch (opcode) {
- case BINARY_POWER:
- newconst = PyNumber_Power(v, w, Py_None);
- break;
- case BINARY_MULTIPLY:
- newconst = PyNumber_Multiply(v, w);
- break;
- case BINARY_TRUE_DIVIDE:
- newconst = PyNumber_TrueDivide(v, w);
- break;
- case BINARY_FLOOR_DIVIDE:
- newconst = PyNumber_FloorDivide(v, w);
- break;
- case BINARY_MODULO:
- newconst = PyNumber_Remainder(v, w);
- break;
- case BINARY_ADD:
- newconst = PyNumber_Add(v, w);
- break;
- case BINARY_SUBTRACT:
- newconst = PyNumber_Subtract(v, w);
- break;
- case BINARY_SUBSCR:
- newconst = PyObject_GetItem(v, w);
- break;
- case BINARY_LSHIFT:
- newconst = PyNumber_Lshift(v, w);
- break;
- case BINARY_RSHIFT:
- newconst = PyNumber_Rshift(v, w);
- break;
- case BINARY_AND:
- newconst = PyNumber_And(v, w);
- break;
- case BINARY_XOR:
- newconst = PyNumber_Xor(v, w);
- break;
- case BINARY_OR:
- newconst = PyNumber_Or(v, w);
- break;
- default:
- /* Called with an unknown opcode */
- PyErr_Format(PyExc_SystemError,
- "unexpected binary operation %d on a constant",
- opcode);
- return 0;
- }
- if (newconst == NULL) {
- PyErr_Clear();
- return 0;
- }
- size = PyObject_Size(newconst);
- if (size == -1)
- PyErr_Clear();
- else if (size > 20) {
- Py_DECREF(newconst);
- return 0;
- }
-
- /* Append folded constant into consts table */
- len_consts = PyList_GET_SIZE(consts);
- if (PyList_Append(consts, newconst)) {
- Py_DECREF(newconst);
- return 0;
- }
- Py_DECREF(newconst);
-
- /* Write NOP NOP NOP NOP LOAD_CONST newconst */
- memset(codestr, NOP, 4);
- codestr[4] = LOAD_CONST;
- SETARG(codestr, 4, len_consts);
- return 1;
+ PyObject *newconst, *v, *w;
+ Py_ssize_t len_consts, size;
+ int opcode;
+
+ /* Pre-conditions */
+ assert(PyList_CheckExact(consts));
+ assert(codestr[0] == LOAD_CONST);
+ assert(codestr[3] == LOAD_CONST);
+
+ /* Create new constant */
+ v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
+ w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
+ opcode = codestr[6];
+ switch (opcode) {
+ case BINARY_POWER:
+ newconst = PyNumber_Power(v, w, Py_None);
+ break;
+ case BINARY_MULTIPLY:
+ newconst = PyNumber_Multiply(v, w);
+ break;
+ case BINARY_TRUE_DIVIDE:
+ newconst = PyNumber_TrueDivide(v, w);
+ break;
+ case BINARY_FLOOR_DIVIDE:
+ newconst = PyNumber_FloorDivide(v, w);
+ break;
+ case BINARY_MODULO:
+ newconst = PyNumber_Remainder(v, w);
+ break;
+ case BINARY_ADD:
+ newconst = PyNumber_Add(v, w);
+ break;
+ case BINARY_SUBTRACT:
+ newconst = PyNumber_Subtract(v, w);
+ break;
+ case BINARY_SUBSCR:
+ newconst = PyObject_GetItem(v, w);
+ break;
+ case BINARY_LSHIFT:
+ newconst = PyNumber_Lshift(v, w);
+ break;
+ case BINARY_RSHIFT:
+ newconst = PyNumber_Rshift(v, w);
+ break;
+ case BINARY_AND:
+ newconst = PyNumber_And(v, w);
+ break;
+ case BINARY_XOR:
+ newconst = PyNumber_Xor(v, w);
+ break;
+ case BINARY_OR:
+ newconst = PyNumber_Or(v, w);
+ break;
+ default:
+ /* Called with an unknown opcode */
+ PyErr_Format(PyExc_SystemError,
+ "unexpected binary operation %d on a constant",
+ opcode);
+ return 0;
+ }
+ if (newconst == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+ size = PyObject_Size(newconst);
+ if (size == -1)
+ PyErr_Clear();
+ else if (size > 20) {
+ Py_DECREF(newconst);
+ return 0;
+ }
+
+ /* Append folded constant into consts table */
+ len_consts = PyList_GET_SIZE(consts);
+ if (PyList_Append(consts, newconst)) {
+ Py_DECREF(newconst);
+ return 0;
+ }
+ Py_DECREF(newconst);
+
+ /* Write NOP NOP NOP NOP LOAD_CONST newconst */
+ memset(codestr, NOP, 4);
+ codestr[4] = LOAD_CONST;
+ SETARG(codestr, 4, len_consts);
+ return 1;
}
static int
fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
{
- PyObject *newconst=NULL, *v;
- Py_ssize_t len_consts;
- int opcode;
-
- /* Pre-conditions */
- assert(PyList_CheckExact(consts));
- assert(codestr[0] == LOAD_CONST);
-
- /* Create new constant */
- v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
- opcode = codestr[3];
- switch (opcode) {
- case UNARY_NEGATIVE:
- /* Preserve the sign of -0.0 */
- if (PyObject_IsTrue(v) == 1)
- newconst = PyNumber_Negative(v);
- break;
- case UNARY_INVERT:
- newconst = PyNumber_Invert(v);
- break;
- default:
- /* Called with an unknown opcode */
- PyErr_Format(PyExc_SystemError,
- "unexpected unary operation %d on a constant",
- opcode);
- return 0;
- }
- if (newconst == NULL) {
- PyErr_Clear();
- return 0;
- }
-
- /* Append folded constant into consts table */
- len_consts = PyList_GET_SIZE(consts);
- if (PyList_Append(consts, newconst)) {
- Py_DECREF(newconst);
- return 0;
- }
- Py_DECREF(newconst);
-
- /* Write NOP LOAD_CONST newconst */
- codestr[0] = NOP;
- codestr[1] = LOAD_CONST;
- SETARG(codestr, 1, len_consts);
- return 1;
+ PyObject *newconst=NULL, *v;
+ Py_ssize_t len_consts;
+ int opcode;
+
+ /* Pre-conditions */
+ assert(PyList_CheckExact(consts));
+ assert(codestr[0] == LOAD_CONST);
+
+ /* Create new constant */
+ v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
+ opcode = codestr[3];
+ switch (opcode) {
+ case UNARY_NEGATIVE:
+ /* Preserve the sign of -0.0 */
+ if (PyObject_IsTrue(v) == 1)
+ newconst = PyNumber_Negative(v);
+ break;
+ case UNARY_INVERT:
+ newconst = PyNumber_Invert(v);
+ break;
+ default:
+ /* Called with an unknown opcode */
+ PyErr_Format(PyExc_SystemError,
+ "unexpected unary operation %d on a constant",
+ opcode);
+ return 0;
+ }
+ if (newconst == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+
+ /* Append folded constant into consts table */
+ len_consts = PyList_GET_SIZE(consts);
+ if (PyList_Append(consts, newconst)) {
+ Py_DECREF(newconst);
+ return 0;
+ }
+ Py_DECREF(newconst);
+
+ /* Write NOP LOAD_CONST newconst */
+ codestr[0] = NOP;
+ codestr[1] = LOAD_CONST;
+ SETARG(codestr, 1, len_consts);
+ return 1;
}
static unsigned int *
markblocks(unsigned char *code, Py_ssize_t len)
{
- unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
- int i,j, opcode, blockcnt = 0;
-
- if (blocks == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
- memset(blocks, 0, len*sizeof(int));
-
- /* Mark labels in the first pass */
- for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
- opcode = code[i];
- switch (opcode) {
- case FOR_ITER:
- case JUMP_FORWARD:
- case JUMP_IF_FALSE_OR_POP:
- case JUMP_IF_TRUE_OR_POP:
- case POP_JUMP_IF_FALSE:
- case POP_JUMP_IF_TRUE:
- case JUMP_ABSOLUTE:
- case CONTINUE_LOOP:
- case SETUP_LOOP:
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- j = GETJUMPTGT(code, i);
- blocks[j] = 1;
- break;
- }
- }
- /* Build block numbers in the second pass */
- for (i=0 ; i<len ; i++) {
- blockcnt += blocks[i]; /* increment blockcnt over labels */
- blocks[i] = blockcnt;
- }
- return blocks;
+ unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
+ int i,j, opcode, blockcnt = 0;
+
+ if (blocks == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ memset(blocks, 0, len*sizeof(int));
+
+ /* Mark labels in the first pass */
+ for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
+ opcode = code[i];
+ switch (opcode) {
+ case FOR_ITER:
+ case JUMP_FORWARD:
+ case JUMP_IF_FALSE_OR_POP:
+ case JUMP_IF_TRUE_OR_POP:
+ case POP_JUMP_IF_FALSE:
+ case POP_JUMP_IF_TRUE:
+ case JUMP_ABSOLUTE:
+ case CONTINUE_LOOP:
+ case SETUP_LOOP:
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ j = GETJUMPTGT(code, i);
+ blocks[j] = 1;
+ break;
+ }
+ }
+ /* Build block numbers in the second pass */
+ for (i=0 ; i<len ; i++) {
+ blockcnt += blocks[i]; /* increment blockcnt over labels */
+ blocks[i] = blockcnt;
+ }
+ return blocks;
}
/* Helper to replace LOAD_NAME None/True/False with LOAD_CONST
@@ -269,411 +269,411 @@ markblocks(unsigned char *code, Py_ssize_t len)
static int
load_global(unsigned char *codestr, Py_ssize_t i, char *name, PyObject *consts)
{
- Py_ssize_t j;
- PyObject *obj;
- if (name == NULL)
- return 0;
- if (strcmp(name, "None") == 0)
- obj = Py_None;
- else if (strcmp(name, "True") == 0)
- obj = Py_True;
- else if (strcmp(name, "False") == 0)
- obj = Py_False;
- else
- return 0;
- for (j = 0; j < PyList_GET_SIZE(consts); j++) {
- if (PyList_GET_ITEM(consts, j) == obj)
- break;
- }
- if (j == PyList_GET_SIZE(consts)) {
- if (PyList_Append(consts, obj) < 0)
- return -1;
- }
- assert(PyList_GET_ITEM(consts, j) == obj);
- codestr[i] = LOAD_CONST;
- SETARG(codestr, i, j);
- return 1;
+ Py_ssize_t j;
+ PyObject *obj;
+ if (name == NULL)
+ return 0;
+ if (strcmp(name, "None") == 0)
+ obj = Py_None;
+ else if (strcmp(name, "True") == 0)
+ obj = Py_True;
+ else if (strcmp(name, "False") == 0)
+ obj = Py_False;
+ else
+ return 0;
+ for (j = 0; j < PyList_GET_SIZE(consts); j++) {
+ if (PyList_GET_ITEM(consts, j) == obj)
+ break;
+ }
+ if (j == PyList_GET_SIZE(consts)) {
+ if (PyList_Append(consts, obj) < 0)
+ return -1;
+ }
+ assert(PyList_GET_ITEM(consts, j) == obj);
+ codestr[i] = LOAD_CONST;
+ SETARG(codestr, i, j);
+ return 1;
}
/* Perform basic peephole optimizations to components of a code object.
- The consts object should still be in list form to allow new constants
+ The consts object should still be in list form to allow new constants
to be appended.
To keep the optimizer simple, it bails out (does nothing) for code that
- has a length over 32,700, and does not calculate extended arguments.
+ has a length over 32,700, and does not calculate extended arguments.
That allows us to avoid overflow and sign issues. Likewise, it bails when
the lineno table has complex encoding for gaps >= 255. EXTENDED_ARG can
appear before MAKE_FUNCTION; in this case both opcodes are skipped.
EXTENDED_ARG preceding any other opcode causes the optimizer to bail.
Optimizations are restricted to simple transformations occuring within a
- single basic block. All transformations keep the code size the same or
- smaller. For those that reduce size, the gaps are initially filled with
- NOPs. Later those NOPs are removed and the jump addresses retargeted in
+ single basic block. All transformations keep the code size the same or
+ smaller. For those that reduce size, the gaps are initially filled with
+ NOPs. Later those NOPs are removed and the jump addresses retargeted in
a single pass. Line numbering is adjusted accordingly. */
PyObject *
PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
PyObject *lineno_obj)
{
- Py_ssize_t i, j, codelen;
- int nops, h, adj;
- int tgt, tgttgt, opcode;
- unsigned char *codestr = NULL;
- unsigned char *lineno;
- int *addrmap = NULL;
- int new_line, cum_orig_line, last_line, tabsiz;
- int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */
- unsigned int *blocks = NULL;
- char *name;
-
- /* Bail out if an exception is set */
- if (PyErr_Occurred())
- goto exitError;
-
- /* Bypass optimization when the lineno table is too complex */
- assert(PyBytes_Check(lineno_obj));
- lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj);
- tabsiz = PyBytes_GET_SIZE(lineno_obj);
- if (memchr(lineno, 255, tabsiz) != NULL)
- goto exitUnchanged;
-
- /* Avoid situations where jump retargeting could overflow */
- assert(PyBytes_Check(code));
- codelen = PyBytes_GET_SIZE(code);
- if (codelen > 32700)
- goto exitUnchanged;
-
- /* Make a modifiable copy of the code string */
- codestr = (unsigned char *)PyMem_Malloc(codelen);
- if (codestr == NULL)
- goto exitError;
- codestr = (unsigned char *)memcpy(codestr,
- PyBytes_AS_STRING(code), codelen);
-
- /* Verify that RETURN_VALUE terminates the codestring. This allows
- the various transformation patterns to look ahead several
- instructions without additional checks to make sure they are not
- looking beyond the end of the code string.
- */
- if (codestr[codelen-1] != RETURN_VALUE)
- goto exitUnchanged;
-
- /* Mapping to new jump targets after NOPs are removed */
- addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
- if (addrmap == NULL)
- goto exitError;
-
- blocks = markblocks(codestr, codelen);
- if (blocks == NULL)
- goto exitError;
- assert(PyList_Check(consts));
-
- for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
- reoptimize_current:
- opcode = codestr[i];
-
- lastlc = cumlc;
- cumlc = 0;
-
- switch (opcode) {
- /* Replace UNARY_NOT POP_JUMP_IF_FALSE
- with POP_JUMP_IF_TRUE */
- case UNARY_NOT:
- if (codestr[i+1] != POP_JUMP_IF_FALSE
- || !ISBASICBLOCK(blocks,i,4))
- continue;
- j = GETARG(codestr, i+1);
- codestr[i] = POP_JUMP_IF_TRUE;
- SETARG(codestr, i, j);
- codestr[i+3] = NOP;
- goto reoptimize_current;
-
- /* not a is b --> a is not b
- not a in b --> a not in b
- not a is not b --> a is b
- not a not in b --> a in b
- */
- case COMPARE_OP:
- j = GETARG(codestr, i);
- if (j < 6 || j > 9 ||
- codestr[i+3] != UNARY_NOT ||
- !ISBASICBLOCK(blocks,i,4))
- continue;
- SETARG(codestr, i, (j^1));
- codestr[i+3] = NOP;
- break;
-
- /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False
- with LOAD_CONST None/True/False */
- case LOAD_NAME:
- case LOAD_GLOBAL:
- j = GETARG(codestr, i);
- name = _PyUnicode_AsString(PyTuple_GET_ITEM(names, j));
- h = load_global(codestr, i, name, consts);
- if (h < 0)
- goto exitError;
- else if (h == 0)
- continue;
- cumlc = lastlc + 1;
- break;
-
- /* Skip over LOAD_CONST trueconst
- POP_JUMP_IF_FALSE xx. This improves
- "while 1" performance. */
- case LOAD_CONST:
- cumlc = lastlc + 1;
- j = GETARG(codestr, i);
- if (codestr[i+3] != POP_JUMP_IF_FALSE ||
- !ISBASICBLOCK(blocks,i,6) ||
- !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
- continue;
- memset(codestr+i, NOP, 6);
- cumlc = 0;
- break;
-
- /* Try to fold tuples of constants (includes a case for lists
- which are only used for "in" and "not in" tests).
- Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
- Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
- Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
- case BUILD_TUPLE:
- case BUILD_LIST:
- j = GETARG(codestr, i);
- h = i - 3 * j;
- if (h >= 0 &&
- j <= lastlc &&
- ((opcode == BUILD_TUPLE &&
- ISBASICBLOCK(blocks, h, 3*(j+1))) ||
- (opcode == BUILD_LIST &&
- codestr[i+3]==COMPARE_OP &&
- ISBASICBLOCK(blocks, h, 3*(j+2)) &&
- (GETARG(codestr,i+3)==6 ||
- GETARG(codestr,i+3)==7))) &&
- tuple_of_constants(&codestr[h], j, consts)) {
- assert(codestr[i] == LOAD_CONST);
- cumlc = 1;
- break;
- }
- if (codestr[i+3] != UNPACK_SEQUENCE ||
- !ISBASICBLOCK(blocks,i,6) ||
- j != GETARG(codestr, i+3))
- continue;
- if (j == 1) {
- memset(codestr+i, NOP, 6);
- } else if (j == 2) {
- codestr[i] = ROT_TWO;
- memset(codestr+i+1, NOP, 5);
- } else if (j == 3) {
- codestr[i] = ROT_THREE;
- codestr[i+1] = ROT_TWO;
- memset(codestr+i+2, NOP, 4);
- }
- break;
-
- /* Fold binary ops on constants.
- LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
- case BINARY_POWER:
- case BINARY_MULTIPLY:
- case BINARY_TRUE_DIVIDE:
- case BINARY_FLOOR_DIVIDE:
- case BINARY_MODULO:
- case BINARY_ADD:
- case BINARY_SUBTRACT:
- case BINARY_SUBSCR:
- case BINARY_LSHIFT:
- case BINARY_RSHIFT:
- case BINARY_AND:
- case BINARY_XOR:
- case BINARY_OR:
- if (lastlc >= 2 &&
- ISBASICBLOCK(blocks, i-6, 7) &&
- fold_binops_on_constants(&codestr[i-6], consts)) {
- i -= 2;
- assert(codestr[i] == LOAD_CONST);
- cumlc = 1;
- }
- break;
-
- /* Fold unary ops on constants.
- LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
- case UNARY_NEGATIVE:
- case UNARY_INVERT:
- if (lastlc >= 1 &&
- ISBASICBLOCK(blocks, i-3, 4) &&
- fold_unaryops_on_constants(&codestr[i-3], consts)) {
- i -= 2;
- assert(codestr[i] == LOAD_CONST);
- cumlc = 1;
- }
- break;
-
- /* Simplify conditional jump to conditional jump where the
- result of the first test implies the success of a similar
- test or the failure of the opposite test.
- Arises in code like:
- "if a and b:"
- "if a or b:"
- "a and b or c"
- "(a and b) and c"
- x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
- --> x:JUMP_IF_FALSE_OR_POP z
- x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
- --> x:POP_JUMP_IF_FALSE y+3
- where y+3 is the instruction following the second test.
- */
- case JUMP_IF_FALSE_OR_POP:
- case JUMP_IF_TRUE_OR_POP:
- tgt = GETJUMPTGT(codestr, i);
- j = codestr[tgt];
- if (CONDITIONAL_JUMP(j)) {
- /* NOTE: all possible jumps here are
- absolute! */
- if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) {
- /* The second jump will be
- taken iff the first is. */
- tgttgt = GETJUMPTGT(codestr, tgt);
- /* The current opcode inherits
- its target's stack behaviour */
- codestr[i] = j;
- SETARG(codestr, i, tgttgt);
- goto reoptimize_current;
- } else {
- /* The second jump is not taken
- if the first is (so jump past
- it), and all conditional
- jumps pop their argument when
- they're not taken (so change
- the first jump to pop its
- argument when it's taken). */
- if (JUMPS_ON_TRUE(opcode))
- codestr[i] = POP_JUMP_IF_TRUE;
- else
- codestr[i] = POP_JUMP_IF_FALSE;
- SETARG(codestr, i, (tgt + 3));
- goto reoptimize_current;
- }
- }
- /* Intentional fallthrough */
-
- /* Replace jumps to unconditional jumps */
- case POP_JUMP_IF_FALSE:
- case POP_JUMP_IF_TRUE:
- case FOR_ITER:
- case JUMP_FORWARD:
- case JUMP_ABSOLUTE:
- case CONTINUE_LOOP:
- case SETUP_LOOP:
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- tgt = GETJUMPTGT(codestr, i);
- /* Replace JUMP_* to a RETURN into just a RETURN */
- if (UNCONDITIONAL_JUMP(opcode) &&
- codestr[tgt] == RETURN_VALUE) {
- codestr[i] = RETURN_VALUE;
- memset(codestr+i+1, NOP, 2);
- continue;
- }
- if (!UNCONDITIONAL_JUMP(codestr[tgt]))
- continue;
- tgttgt = GETJUMPTGT(codestr, tgt);
- if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
- opcode = JUMP_ABSOLUTE;
- if (!ABSOLUTE_JUMP(opcode))
- tgttgt -= i + 3; /* Calc relative jump addr */
- if (tgttgt < 0) /* No backward relative jumps */
- continue;
- codestr[i] = opcode;
- SETARG(codestr, i, tgttgt);
- break;
-
- case EXTENDED_ARG:
- if (codestr[i+3] != MAKE_FUNCTION)
- goto exitUnchanged;
- /* don't visit MAKE_FUNCTION as GETARG will be wrong */
- i += 3;
- break;
-
- /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
- /* Remove unreachable JUMPs after RETURN */
- case RETURN_VALUE:
- if (i+4 >= codelen)
- continue;
- if (codestr[i+4] == RETURN_VALUE &&
- ISBASICBLOCK(blocks,i,5))
- memset(codestr+i+1, NOP, 4);
- else if (UNCONDITIONAL_JUMP(codestr[i+1]) &&
- ISBASICBLOCK(blocks,i,4))
- memset(codestr+i+1, NOP, 3);
- break;
- }
- }
-
- /* Fixup linenotab */
- for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
- addrmap[i] = i - nops;
- if (codestr[i] == NOP)
- nops++;
- }
- cum_orig_line = 0;
- last_line = 0;
- for (i=0 ; i < tabsiz ; i+=2) {
- cum_orig_line += lineno[i];
- new_line = addrmap[cum_orig_line];
- assert (new_line - last_line < 255);
- lineno[i] =((unsigned char)(new_line - last_line));
- last_line = new_line;
- }
-
- /* Remove NOPs and fixup jump targets */
- for (i=0, h=0 ; i<codelen ; ) {
- opcode = codestr[i];
- switch (opcode) {
- case NOP:
- i++;
- continue;
-
- case JUMP_ABSOLUTE:
- case CONTINUE_LOOP:
- case POP_JUMP_IF_FALSE:
- case POP_JUMP_IF_TRUE:
- case JUMP_IF_FALSE_OR_POP:
- case JUMP_IF_TRUE_OR_POP:
- j = addrmap[GETARG(codestr, i)];
- SETARG(codestr, i, j);
- break;
-
- case FOR_ITER:
- case JUMP_FORWARD:
- case SETUP_LOOP:
- case SETUP_EXCEPT:
- case SETUP_FINALLY:
- j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
- SETARG(codestr, i, j);
- break;
- }
- adj = CODESIZE(opcode);
- while (adj--)
- codestr[h++] = codestr[i++];
- }
- assert(h + nops == codelen);
-
- code = PyBytes_FromStringAndSize((char *)codestr, h);
- PyMem_Free(addrmap);
- PyMem_Free(codestr);
- PyMem_Free(blocks);
- return code;
+ Py_ssize_t i, j, codelen;
+ int nops, h, adj;
+ int tgt, tgttgt, opcode;
+ unsigned char *codestr = NULL;
+ unsigned char *lineno;
+ int *addrmap = NULL;
+ int new_line, cum_orig_line, last_line, tabsiz;
+ int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONSTs */
+ unsigned int *blocks = NULL;
+ char *name;
+
+ /* Bail out if an exception is set */
+ if (PyErr_Occurred())
+ goto exitError;
+
+ /* Bypass optimization when the lineno table is too complex */
+ assert(PyBytes_Check(lineno_obj));
+ lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj);
+ tabsiz = PyBytes_GET_SIZE(lineno_obj);
+ if (memchr(lineno, 255, tabsiz) != NULL)
+ goto exitUnchanged;
+
+ /* Avoid situations where jump retargeting could overflow */
+ assert(PyBytes_Check(code));
+ codelen = PyBytes_GET_SIZE(code);
+ if (codelen > 32700)
+ goto exitUnchanged;
+
+ /* Make a modifiable copy of the code string */
+ codestr = (unsigned char *)PyMem_Malloc(codelen);
+ if (codestr == NULL)
+ goto exitError;
+ codestr = (unsigned char *)memcpy(codestr,
+ PyBytes_AS_STRING(code), codelen);
+
+ /* Verify that RETURN_VALUE terminates the codestring. This allows
+ the various transformation patterns to look ahead several
+ instructions without additional checks to make sure they are not
+ looking beyond the end of the code string.
+ */
+ if (codestr[codelen-1] != RETURN_VALUE)
+ goto exitUnchanged;
+
+ /* Mapping to new jump targets after NOPs are removed */
+ addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
+ if (addrmap == NULL)
+ goto exitError;
+
+ blocks = markblocks(codestr, codelen);
+ if (blocks == NULL)
+ goto exitError;
+ assert(PyList_Check(consts));
+
+ for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
+ reoptimize_current:
+ opcode = codestr[i];
+
+ lastlc = cumlc;
+ cumlc = 0;
+
+ switch (opcode) {
+ /* Replace UNARY_NOT POP_JUMP_IF_FALSE
+ with POP_JUMP_IF_TRUE */
+ case UNARY_NOT:
+ if (codestr[i+1] != POP_JUMP_IF_FALSE
+ || !ISBASICBLOCK(blocks,i,4))
+ continue;
+ j = GETARG(codestr, i+1);
+ codestr[i] = POP_JUMP_IF_TRUE;
+ SETARG(codestr, i, j);
+ codestr[i+3] = NOP;
+ goto reoptimize_current;
+
+ /* not a is b --> a is not b
+ not a in b --> a not in b
+ not a is not b --> a is b
+ not a not in b --> a in b
+ */
+ case COMPARE_OP:
+ j = GETARG(codestr, i);
+ if (j < 6 || j > 9 ||
+ codestr[i+3] != UNARY_NOT ||
+ !ISBASICBLOCK(blocks,i,4))
+ continue;
+ SETARG(codestr, i, (j^1));
+ codestr[i+3] = NOP;
+ break;
+
+ /* Replace LOAD_GLOBAL/LOAD_NAME None/True/False
+ with LOAD_CONST None/True/False */
+ case LOAD_NAME:
+ case LOAD_GLOBAL:
+ j = GETARG(codestr, i);
+ name = _PyUnicode_AsString(PyTuple_GET_ITEM(names, j));
+ h = load_global(codestr, i, name, consts);
+ if (h < 0)
+ goto exitError;
+ else if (h == 0)
+ continue;
+ cumlc = lastlc + 1;
+ break;
+
+ /* Skip over LOAD_CONST trueconst
+ POP_JUMP_IF_FALSE xx. This improves
+ "while 1" performance. */
+ case LOAD_CONST:
+ cumlc = lastlc + 1;
+ j = GETARG(codestr, i);
+ if (codestr[i+3] != POP_JUMP_IF_FALSE ||
+ !ISBASICBLOCK(blocks,i,6) ||
+ !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
+ continue;
+ memset(codestr+i, NOP, 6);
+ cumlc = 0;
+ break;
+
+ /* Try to fold tuples of constants (includes a case for lists
+ which are only used for "in" and "not in" tests).
+ Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
+ Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
+ Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
+ case BUILD_TUPLE:
+ case BUILD_LIST:
+ j = GETARG(codestr, i);
+ h = i - 3 * j;
+ if (h >= 0 &&
+ j <= lastlc &&
+ ((opcode == BUILD_TUPLE &&
+ ISBASICBLOCK(blocks, h, 3*(j+1))) ||
+ (opcode == BUILD_LIST &&
+ codestr[i+3]==COMPARE_OP &&
+ ISBASICBLOCK(blocks, h, 3*(j+2)) &&
+ (GETARG(codestr,i+3)==6 ||
+ GETARG(codestr,i+3)==7))) &&
+ tuple_of_constants(&codestr[h], j, consts)) {
+ assert(codestr[i] == LOAD_CONST);
+ cumlc = 1;
+ break;
+ }
+ if (codestr[i+3] != UNPACK_SEQUENCE ||
+ !ISBASICBLOCK(blocks,i,6) ||
+ j != GETARG(codestr, i+3))
+ continue;
+ if (j == 1) {
+ memset(codestr+i, NOP, 6);
+ } else if (j == 2) {
+ codestr[i] = ROT_TWO;
+ memset(codestr+i+1, NOP, 5);
+ } else if (j == 3) {
+ codestr[i] = ROT_THREE;
+ codestr[i+1] = ROT_TWO;
+ memset(codestr+i+2, NOP, 4);
+ }
+ break;
+
+ /* Fold binary ops on constants.
+ LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
+ case BINARY_POWER:
+ case BINARY_MULTIPLY:
+ case BINARY_TRUE_DIVIDE:
+ case BINARY_FLOOR_DIVIDE:
+ case BINARY_MODULO:
+ case BINARY_ADD:
+ case BINARY_SUBTRACT:
+ case BINARY_SUBSCR:
+ case BINARY_LSHIFT:
+ case BINARY_RSHIFT:
+ case BINARY_AND:
+ case BINARY_XOR:
+ case BINARY_OR:
+ if (lastlc >= 2 &&
+ ISBASICBLOCK(blocks, i-6, 7) &&
+ fold_binops_on_constants(&codestr[i-6], consts)) {
+ i -= 2;
+ assert(codestr[i] == LOAD_CONST);
+ cumlc = 1;
+ }
+ break;
+
+ /* Fold unary ops on constants.
+ LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
+ case UNARY_NEGATIVE:
+ case UNARY_INVERT:
+ if (lastlc >= 1 &&
+ ISBASICBLOCK(blocks, i-3, 4) &&
+ fold_unaryops_on_constants(&codestr[i-3], consts)) {
+ i -= 2;
+ assert(codestr[i] == LOAD_CONST);
+ cumlc = 1;
+ }
+ break;
+
+ /* Simplify conditional jump to conditional jump where the
+ result of the first test implies the success of a similar
+ test or the failure of the opposite test.
+ Arises in code like:
+ "if a and b:"
+ "if a or b:"
+ "a and b or c"
+ "(a and b) and c"
+ x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
+ --> x:JUMP_IF_FALSE_OR_POP z
+ x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
+ --> x:POP_JUMP_IF_FALSE y+3
+ where y+3 is the instruction following the second test.
+ */
+ case JUMP_IF_FALSE_OR_POP:
+ case JUMP_IF_TRUE_OR_POP:
+ tgt = GETJUMPTGT(codestr, i);
+ j = codestr[tgt];
+ if (CONDITIONAL_JUMP(j)) {
+ /* NOTE: all possible jumps here are
+ absolute! */
+ if (JUMPS_ON_TRUE(j) == JUMPS_ON_TRUE(opcode)) {
+ /* The second jump will be
+ taken iff the first is. */
+ tgttgt = GETJUMPTGT(codestr, tgt);
+ /* The current opcode inherits
+ its target's stack behaviour */
+ codestr[i] = j;
+ SETARG(codestr, i, tgttgt);
+ goto reoptimize_current;
+ } else {
+ /* The second jump is not taken
+ if the first is (so jump past
+ it), and all conditional
+ jumps pop their argument when
+ they're not taken (so change
+ the first jump to pop its
+ argument when it's taken). */
+ if (JUMPS_ON_TRUE(opcode))
+ codestr[i] = POP_JUMP_IF_TRUE;
+ else
+ codestr[i] = POP_JUMP_IF_FALSE;
+ SETARG(codestr, i, (tgt + 3));
+ goto reoptimize_current;
+ }
+ }
+ /* Intentional fallthrough */
+
+ /* Replace jumps to unconditional jumps */
+ case POP_JUMP_IF_FALSE:
+ case POP_JUMP_IF_TRUE:
+ case FOR_ITER:
+ case JUMP_FORWARD:
+ case JUMP_ABSOLUTE:
+ case CONTINUE_LOOP:
+ case SETUP_LOOP:
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ tgt = GETJUMPTGT(codestr, i);
+ /* Replace JUMP_* to a RETURN into just a RETURN */
+ if (UNCONDITIONAL_JUMP(opcode) &&
+ codestr[tgt] == RETURN_VALUE) {
+ codestr[i] = RETURN_VALUE;
+ memset(codestr+i+1, NOP, 2);
+ continue;
+ }
+ if (!UNCONDITIONAL_JUMP(codestr[tgt]))
+ continue;
+ tgttgt = GETJUMPTGT(codestr, tgt);
+ if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
+ opcode = JUMP_ABSOLUTE;
+ if (!ABSOLUTE_JUMP(opcode))
+ tgttgt -= i + 3; /* Calc relative jump addr */
+ if (tgttgt < 0) /* No backward relative jumps */
+ continue;
+ codestr[i] = opcode;
+ SETARG(codestr, i, tgttgt);
+ break;
+
+ case EXTENDED_ARG:
+ if (codestr[i+3] != MAKE_FUNCTION)
+ goto exitUnchanged;
+ /* don't visit MAKE_FUNCTION as GETARG will be wrong */
+ i += 3;
+ break;
+
+ /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
+ /* Remove unreachable JUMPs after RETURN */
+ case RETURN_VALUE:
+ if (i+4 >= codelen)
+ continue;
+ if (codestr[i+4] == RETURN_VALUE &&
+ ISBASICBLOCK(blocks,i,5))
+ memset(codestr+i+1, NOP, 4);
+ else if (UNCONDITIONAL_JUMP(codestr[i+1]) &&
+ ISBASICBLOCK(blocks,i,4))
+ memset(codestr+i+1, NOP, 3);
+ break;
+ }
+ }
+
+ /* Fixup linenotab */
+ for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
+ addrmap[i] = i - nops;
+ if (codestr[i] == NOP)
+ nops++;
+ }
+ cum_orig_line = 0;
+ last_line = 0;
+ for (i=0 ; i < tabsiz ; i+=2) {
+ cum_orig_line += lineno[i];
+ new_line = addrmap[cum_orig_line];
+ assert (new_line - last_line < 255);
+ lineno[i] =((unsigned char)(new_line - last_line));
+ last_line = new_line;
+ }
+
+ /* Remove NOPs and fixup jump targets */
+ for (i=0, h=0 ; i<codelen ; ) {
+ opcode = codestr[i];
+ switch (opcode) {
+ case NOP:
+ i++;
+ continue;
+
+ case JUMP_ABSOLUTE:
+ case CONTINUE_LOOP:
+ case POP_JUMP_IF_FALSE:
+ case POP_JUMP_IF_TRUE:
+ case JUMP_IF_FALSE_OR_POP:
+ case JUMP_IF_TRUE_OR_POP:
+ j = addrmap[GETARG(codestr, i)];
+ SETARG(codestr, i, j);
+ break;
+
+ case FOR_ITER:
+ case JUMP_FORWARD:
+ case SETUP_LOOP:
+ case SETUP_EXCEPT:
+ case SETUP_FINALLY:
+ j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
+ SETARG(codestr, i, j);
+ break;
+ }
+ adj = CODESIZE(opcode);
+ while (adj--)
+ codestr[h++] = codestr[i++];
+ }
+ assert(h + nops == codelen);
+
+ code = PyBytes_FromStringAndSize((char *)codestr, h);
+ PyMem_Free(addrmap);
+ PyMem_Free(codestr);
+ PyMem_Free(blocks);
+ return code;
exitError:
- code = NULL;
+ code = NULL;
exitUnchanged:
- if (blocks != NULL)
- PyMem_Free(blocks);
- if (addrmap != NULL)
- PyMem_Free(addrmap);
- if (codestr != NULL)
- PyMem_Free(codestr);
- Py_XINCREF(code);
- return code;
+ if (blocks != NULL)
+ PyMem_Free(blocks);
+ if (addrmap != NULL)
+ PyMem_Free(addrmap);
+ if (codestr != NULL)
+ PyMem_Free(codestr);
+ Py_XINCREF(code);
+ return code;
}
diff --git a/Python/pyarena.c b/Python/pyarena.c
index f4cc474..2d63638 100644
--- a/Python/pyarena.c
+++ b/Python/pyarena.c
@@ -12,32 +12,32 @@
*/
#define DEFAULT_BLOCK_SIZE 8192
-#define ALIGNMENT 8
-#define ALIGNMENT_MASK (ALIGNMENT - 1)
-#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
+#define ALIGNMENT 8
+#define ALIGNMENT_MASK (ALIGNMENT - 1)
+#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
typedef struct _block {
- /* Total number of bytes owned by this block available to pass out.
- * Read-only after initialization. The first such byte starts at
- * ab_mem.
- */
- size_t ab_size;
-
- /* Total number of bytes already passed out. The next byte available
- * to pass out starts at ab_mem + ab_offset.
- */
- size_t ab_offset;
-
- /* An arena maintains a singly-linked, NULL-terminated list of
- * all blocks owned by the arena. These are linked via the
- * ab_next member.
- */
- struct _block *ab_next;
-
- /* Pointer to the first allocatable byte owned by this block. Read-
- * only after initialization.
- */
- void *ab_mem;
+ /* Total number of bytes owned by this block available to pass out.
+ * Read-only after initialization. The first such byte starts at
+ * ab_mem.
+ */
+ size_t ab_size;
+
+ /* Total number of bytes already passed out. The next byte available
+ * to pass out starts at ab_mem + ab_offset.
+ */
+ size_t ab_offset;
+
+ /* An arena maintains a singly-linked, NULL-terminated list of
+ * all blocks owned by the arena. These are linked via the
+ * ab_next member.
+ */
+ struct _block *ab_next;
+
+ /* Pointer to the first allocatable byte owned by this block. Read-
+ * only after initialization.
+ */
+ void *ab_mem;
} block;
/* The arena manages two kinds of memory, blocks of raw memory
@@ -46,175 +46,175 @@ typedef struct _block {
*/
struct _arena {
- /* Pointer to the first block allocated for the arena, never NULL.
- It is used only to find the first block when the arena is
- being freed.
- */
- block *a_head;
-
- /* Pointer to the block currently used for allocation. It's
- ab_next field should be NULL. If it is not-null after a
- call to block_alloc(), it means a new block has been allocated
- and a_cur should be reset to point it.
- */
- block *a_cur;
-
- /* A Python list object containing references to all the PyObject
- pointers associated with this area. They will be DECREFed
- when the arena is freed.
- */
- PyObject *a_objects;
+ /* Pointer to the first block allocated for the arena, never NULL.
+ It is used only to find the first block when the arena is
+ being freed.
+ */
+ block *a_head;
+
+ /* Pointer to the block currently used for allocation. It's
+ ab_next field should be NULL. If it is not-null after a
+ call to block_alloc(), it means a new block has been allocated
+ and a_cur should be reset to point it.
+ */
+ block *a_cur;
+
+ /* A Python list object containing references to all the PyObject
+ pointers associated with this area. They will be DECREFed
+ when the arena is freed.
+ */
+ PyObject *a_objects;
#if defined(Py_DEBUG)
- /* Debug output */
- size_t total_allocs;
- size_t total_size;
- size_t total_blocks;
- size_t total_block_size;
- size_t total_big_blocks;
+ /* Debug output */
+ size_t total_allocs;
+ size_t total_size;
+ size_t total_blocks;
+ size_t total_block_size;
+ size_t total_big_blocks;
#endif
};
static block *
block_new(size_t size)
{
- /* Allocate header and block as one unit.
- ab_mem points just past header. */
- block *b = (block *)malloc(sizeof(block) + size);
- if (!b)
- return NULL;
- b->ab_size = size;
- b->ab_mem = (void *)(b + 1);
- b->ab_next = NULL;
- b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) -
- (Py_uintptr_t)(b->ab_mem);
- return b;
+ /* Allocate header and block as one unit.
+ ab_mem points just past header. */
+ block *b = (block *)malloc(sizeof(block) + size);
+ if (!b)
+ return NULL;
+ b->ab_size = size;
+ b->ab_mem = (void *)(b + 1);
+ b->ab_next = NULL;
+ b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) -
+ (Py_uintptr_t)(b->ab_mem);
+ return b;
}
static void
block_free(block *b) {
- while (b) {
- block *next = b->ab_next;
- free(b);
- b = next;
- }
+ while (b) {
+ block *next = b->ab_next;
+ free(b);
+ b = next;
+ }
}
static void *
block_alloc(block *b, size_t size)
{
- void *p;
- assert(b);
- size = ROUNDUP(size);
- if (b->ab_offset + size > b->ab_size) {
- /* If we need to allocate more memory than will fit in
- the default block, allocate a one-off block that is
- exactly the right size. */
- /* TODO(jhylton): Think about space waste at end of block */
- block *newbl = block_new(
- size < DEFAULT_BLOCK_SIZE ?
- DEFAULT_BLOCK_SIZE : size);
- if (!newbl)
- return NULL;
- assert(!b->ab_next);
- b->ab_next = newbl;
- b = newbl;
- }
-
- assert(b->ab_offset + size <= b->ab_size);
- p = (void *)(((char *)b->ab_mem) + b->ab_offset);
- b->ab_offset += size;
- return p;
+ void *p;
+ assert(b);
+ size = ROUNDUP(size);
+ if (b->ab_offset + size > b->ab_size) {
+ /* If we need to allocate more memory than will fit in
+ the default block, allocate a one-off block that is
+ exactly the right size. */
+ /* TODO(jhylton): Think about space waste at end of block */
+ block *newbl = block_new(
+ size < DEFAULT_BLOCK_SIZE ?
+ DEFAULT_BLOCK_SIZE : size);
+ if (!newbl)
+ return NULL;
+ assert(!b->ab_next);
+ b->ab_next = newbl;
+ b = newbl;
+ }
+
+ assert(b->ab_offset + size <= b->ab_size);
+ p = (void *)(((char *)b->ab_mem) + b->ab_offset);
+ b->ab_offset += size;
+ return p;
}
PyArena *
PyArena_New()
{
- PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
- if (!arena)
- return (PyArena*)PyErr_NoMemory();
-
- arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
- arena->a_cur = arena->a_head;
- if (!arena->a_head) {
- free((void *)arena);
- return (PyArena*)PyErr_NoMemory();
- }
- arena->a_objects = PyList_New(0);
- if (!arena->a_objects) {
- block_free(arena->a_head);
- free((void *)arena);
- return (PyArena*)PyErr_NoMemory();
- }
+ PyArena* arena = (PyArena *)malloc(sizeof(PyArena));
+ if (!arena)
+ return (PyArena*)PyErr_NoMemory();
+
+ arena->a_head = block_new(DEFAULT_BLOCK_SIZE);
+ arena->a_cur = arena->a_head;
+ if (!arena->a_head) {
+ free((void *)arena);
+ return (PyArena*)PyErr_NoMemory();
+ }
+ arena->a_objects = PyList_New(0);
+ if (!arena->a_objects) {
+ block_free(arena->a_head);
+ free((void *)arena);
+ return (PyArena*)PyErr_NoMemory();
+ }
#if defined(Py_DEBUG)
- arena->total_allocs = 0;
- arena->total_size = 0;
- arena->total_blocks = 1;
- arena->total_block_size = DEFAULT_BLOCK_SIZE;
- arena->total_big_blocks = 0;
+ arena->total_allocs = 0;
+ arena->total_size = 0;
+ arena->total_blocks = 1;
+ arena->total_block_size = DEFAULT_BLOCK_SIZE;
+ arena->total_big_blocks = 0;
#endif
- return arena;
+ return arena;
}
void
PyArena_Free(PyArena *arena)
{
- int r;
- assert(arena);
+ int r;
+ assert(arena);
#if defined(Py_DEBUG)
- /*
- fprintf(stderr,
- "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
- arena->total_allocs, arena->total_size, arena->total_blocks,
- arena->total_block_size, arena->total_big_blocks,
- PyList_Size(arena->a_objects));
- */
+ /*
+ fprintf(stderr,
+ "alloc=%d size=%d blocks=%d block_size=%d big=%d objects=%d\n",
+ arena->total_allocs, arena->total_size, arena->total_blocks,
+ arena->total_block_size, arena->total_big_blocks,
+ PyList_Size(arena->a_objects));
+ */
#endif
- block_free(arena->a_head);
- /* This property normally holds, except when the code being compiled
- is sys.getobjects(0), in which case there will be two references.
- assert(arena->a_objects->ob_refcnt == 1);
- */
-
- /* Clear all the elements from the list. This is necessary
- to guarantee that they will be DECREFed. */
- r = PyList_SetSlice(arena->a_objects,
- 0, PyList_GET_SIZE(arena->a_objects), NULL);
- assert(r == 0);
- assert(PyList_GET_SIZE(arena->a_objects) == 0);
- Py_DECREF(arena->a_objects);
- free(arena);
+ block_free(arena->a_head);
+ /* This property normally holds, except when the code being compiled
+ is sys.getobjects(0), in which case there will be two references.
+ assert(arena->a_objects->ob_refcnt == 1);
+ */
+
+ /* Clear all the elements from the list. This is necessary
+ to guarantee that they will be DECREFed. */
+ r = PyList_SetSlice(arena->a_objects,
+ 0, PyList_GET_SIZE(arena->a_objects), NULL);
+ assert(r == 0);
+ assert(PyList_GET_SIZE(arena->a_objects) == 0);
+ Py_DECREF(arena->a_objects);
+ free(arena);
}
void *
PyArena_Malloc(PyArena *arena, size_t size)
{
- void *p = block_alloc(arena->a_cur, size);
- if (!p)
- return PyErr_NoMemory();
+ void *p = block_alloc(arena->a_cur, size);
+ if (!p)
+ return PyErr_NoMemory();
#if defined(Py_DEBUG)
- arena->total_allocs++;
- arena->total_size += size;
+ arena->total_allocs++;
+ arena->total_size += size;
#endif
- /* Reset cur if we allocated a new block. */
- if (arena->a_cur->ab_next) {
- arena->a_cur = arena->a_cur->ab_next;
+ /* Reset cur if we allocated a new block. */
+ if (arena->a_cur->ab_next) {
+ arena->a_cur = arena->a_cur->ab_next;
#if defined(Py_DEBUG)
- arena->total_blocks++;
- arena->total_block_size += arena->a_cur->ab_size;
- if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
- ++arena->total_big_blocks;
+ arena->total_blocks++;
+ arena->total_block_size += arena->a_cur->ab_size;
+ if (arena->a_cur->ab_size > DEFAULT_BLOCK_SIZE)
+ ++arena->total_big_blocks;
#endif
- }
- return p;
+ }
+ return p;
}
int
PyArena_AddPyObject(PyArena *arena, PyObject *obj)
{
- int r = PyList_Append(arena->a_objects, obj);
- if (r >= 0) {
- Py_DECREF(obj);
- }
- return r;
+ int r = PyList_Append(arena->a_objects, obj);
+ if (r >= 0) {
+ Py_DECREF(obj);
+ }
+ return r;
}
diff --git a/Python/pymath.c b/Python/pymath.c
index db2920c..9159b6e 100644
--- a/Python/pymath.c
+++ b/Python/pymath.c
@@ -7,9 +7,9 @@
thus rounding from extended precision to double precision. */
double _Py_force_double(double x)
{
- volatile double y;
- y = x;
- return y;
+ volatile double y;
+ y = x;
+ return y;
}
#endif
@@ -34,21 +34,21 @@ void _Py_set_387controlword(unsigned short cw) {
#ifndef HAVE_HYPOT
double hypot(double x, double y)
{
- double yx;
+ double yx;
- x = fabs(x);
- y = fabs(y);
- if (x < y) {
- double temp = x;
- x = y;
- y = temp;
- }
- if (x == 0.)
- return 0.;
- else {
- yx = y/x;
- return x*sqrt(1.+yx*yx);
- }
+ x = fabs(x);
+ y = fabs(y);
+ if (x < y) {
+ double temp = x;
+ x = y;
+ y = temp;
+ }
+ if (x == 0.)
+ return 0.;
+ else {
+ yx = y/x;
+ return x*sqrt(1.+yx*yx);
+ }
}
#endif /* HAVE_HYPOT */
@@ -56,12 +56,12 @@ double hypot(double x, double y)
double
copysign(double x, double y)
{
- /* use atan2 to distinguish -0. from 0. */
- if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
- return fabs(x);
- } else {
- return -fabs(x);
- }
+ /* use atan2 to distinguish -0. from 0. */
+ if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
+ return fabs(x);
+ } else {
+ return -fabs(x);
+ }
}
#endif /* HAVE_COPYSIGN */
@@ -73,7 +73,7 @@ round(double x)
absx = fabs(x);
y = floor(absx);
if (absx - y >= 0.5)
- y += 1.0;
+ y += 1.0;
return copysign(y, x);
}
#endif /* HAVE_ROUND */
@@ -84,41 +84,41 @@ round(double x)
double
log1p(double x)
{
- /* For x small, we use the following approach. Let y be the nearest
- float to 1+x, then
+ /* For x small, we use the following approach. Let y be the nearest
+ float to 1+x, then
- 1+x = y * (1 - (y-1-x)/y)
+ 1+x = y * (1 - (y-1-x)/y)
- so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny,
- the second term is well approximated by (y-1-x)/y. If abs(x) >=
- DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
- then y-1-x will be exactly representable, and is computed exactly
- by (y-1)-x.
+ so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny,
+ the second term is well approximated by (y-1-x)/y. If abs(x) >=
+ DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
+ then y-1-x will be exactly representable, and is computed exactly
+ by (y-1)-x.
- If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
- round-to-nearest then this method is slightly dangerous: 1+x could
- be rounded up to 1+DBL_EPSILON instead of down to 1, and in that
- case y-1-x will not be exactly representable any more and the
- result can be off by many ulps. But this is easily fixed: for a
- floating-point number |x| < DBL_EPSILON/2., the closest
- floating-point number to log(1+x) is exactly x.
- */
+ If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
+ round-to-nearest then this method is slightly dangerous: 1+x could
+ be rounded up to 1+DBL_EPSILON instead of down to 1, and in that
+ case y-1-x will not be exactly representable any more and the
+ result can be off by many ulps. But this is easily fixed: for a
+ floating-point number |x| < DBL_EPSILON/2., the closest
+ floating-point number to log(1+x) is exactly x.
+ */
- double y;
- if (fabs(x) < DBL_EPSILON/2.) {
- return x;
- } else if (-0.5 <= x && x <= 1.) {
- /* WARNING: it's possible than an overeager compiler
- will incorrectly optimize the following two lines
- to the equivalent of "return log(1.+x)". If this
- happens, then results from log1p will be inaccurate
- for small x. */
- y = 1.+x;
- return log(y)-((y-1.)-x)/y;
- } else {
- /* NaNs and infinities should end up here */
- return log(1.+x);
- }
+ double y;
+ if (fabs(x) < DBL_EPSILON/2.) {
+ return x;
+ } else if (-0.5 <= x && x <= 1.) {
+ /* WARNING: it's possible than an overeager compiler
+ will incorrectly optimize the following two lines
+ to the equivalent of "return log(1.+x)". If this
+ happens, then results from log1p will be inaccurate
+ for small x. */
+ y = 1.+x;
+ return log(y)-((y-1.)-x)/y;
+ } else {
+ /* NaNs and infinities should end up here */
+ return log(1.+x);
+ }
}
#endif /* HAVE_LOG1P */
@@ -128,7 +128,7 @@ log1p(double x)
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
+ * software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
@@ -140,51 +140,51 @@ static const double zero = 0.0;
/* asinh(x)
* Method :
- * Based on
- * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
- * we have
- * asinh(x) := x if 1+x*x=1,
- * := sign(x)*(log(x)+ln2)) for large |x|, else
- * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
- * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
+ * Based on
+ * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
+ * we have
+ * asinh(x) := x if 1+x*x=1,
+ * := sign(x)*(log(x)+ln2)) for large |x|, else
+ * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
+ * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
*/
#ifndef HAVE_ASINH
double
asinh(double x)
-{
- double w;
- double absx = fabs(x);
+{
+ double w;
+ double absx = fabs(x);
+
+ if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
+ return x+x;
+ }
+ if (absx < two_pow_m28) { /* |x| < 2**-28 */
+ return x; /* return x inexact except 0 */
+ }
+ if (absx > two_pow_p28) { /* |x| > 2**28 */
+ w = log(absx)+ln2;
+ }
+ else if (absx > 2.0) { /* 2 < |x| < 2**28 */
+ w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
+ }
+ else { /* 2**-28 <= |x| < 2= */
+ double t = x*x;
+ w = log1p(absx + t / (1.0 + sqrt(1.0 + t)));
+ }
+ return copysign(w, x);
- if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
- return x+x;
- }
- if (absx < two_pow_m28) { /* |x| < 2**-28 */
- return x; /* return x inexact except 0 */
- }
- if (absx > two_pow_p28) { /* |x| > 2**28 */
- w = log(absx)+ln2;
- }
- else if (absx > 2.0) { /* 2 < |x| < 2**28 */
- w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
- }
- else { /* 2**-28 <= |x| < 2= */
- double t = x*x;
- w = log1p(absx + t / (1.0 + sqrt(1.0 + t)));
- }
- return copysign(w, x);
-
}
#endif /* HAVE_ASINH */
/* acosh(x)
* Method :
* Based on
- * acosh(x) = log [ x + sqrt(x*x-1) ]
+ * acosh(x) = log [ x + sqrt(x*x-1) ]
* we have
- * acosh(x) := log(x)+ln2, if x is large; else
- * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
- * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
+ * acosh(x) := log(x)+ln2, if x is large; else
+ * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
+ * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
*
* Special cases:
* acosh(x) is NaN with signal if x<1.
@@ -195,35 +195,35 @@ asinh(double x)
double
acosh(double x)
{
- if (Py_IS_NAN(x)) {
- return x+x;
- }
- if (x < 1.) { /* x < 1; return a signaling NaN */
- errno = EDOM;
+ if (Py_IS_NAN(x)) {
+ return x+x;
+ }
+ if (x < 1.) { /* x < 1; return a signaling NaN */
+ errno = EDOM;
#ifdef Py_NAN
- return Py_NAN;
+ return Py_NAN;
#else
- return (x-x)/(x-x);
+ return (x-x)/(x-x);
#endif
- }
- else if (x >= two_pow_p28) { /* x > 2**28 */
- if (Py_IS_INFINITY(x)) {
- return x+x;
- } else {
- return log(x)+ln2; /* acosh(huge)=log(2x) */
- }
- }
- else if (x == 1.) {
- return 0.0; /* acosh(1) = 0 */
- }
- else if (x > 2.) { /* 2 < x < 2**28 */
- double t = x*x;
- return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
- }
- else { /* 1 < x <= 2 */
- double t = x - 1.0;
- return log1p(t + sqrt(2.0*t + t*t));
- }
+ }
+ else if (x >= two_pow_p28) { /* x > 2**28 */
+ if (Py_IS_INFINITY(x)) {
+ return x+x;
+ } else {
+ return log(x)+ln2; /* acosh(huge)=log(2x) */
+ }
+ }
+ else if (x == 1.) {
+ return 0.0; /* acosh(1) = 0 */
+ }
+ else if (x > 2.) { /* 2 < x < 2**28 */
+ double t = x*x;
+ return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
+ }
+ else { /* 1 < x <= 2 */
+ double t = x - 1.0;
+ return log1p(t + sqrt(2.0*t + t*t));
+ }
}
#endif /* HAVE_ACOSH */
@@ -231,9 +231,9 @@ acosh(double x)
* Method :
* 1.Reduced x to positive by atanh(-x) = -atanh(x)
* 2.For x>=0.5
- * 1 2x x
+ * 1 2x x
* atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
- * 2 1 - x 1 - x
+ * 2 1 - x 1 - x
*
* For x<0.5
* atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
@@ -248,31 +248,31 @@ acosh(double x)
double
atanh(double x)
{
- double absx;
- double t;
+ double absx;
+ double t;
- if (Py_IS_NAN(x)) {
- return x+x;
- }
- absx = fabs(x);
- if (absx >= 1.) { /* |x| >= 1 */
- errno = EDOM;
+ if (Py_IS_NAN(x)) {
+ return x+x;
+ }
+ absx = fabs(x);
+ if (absx >= 1.) { /* |x| >= 1 */
+ errno = EDOM;
#ifdef Py_NAN
- return Py_NAN;
+ return Py_NAN;
#else
- return x/zero;
+ return x/zero;
#endif
- }
- if (absx < two_pow_m28) { /* |x| < 2**-28 */
- return x;
- }
- if (absx < 0.5) { /* |x| < 0.5 */
- t = absx+absx;
- t = 0.5 * log1p(t + t*absx / (1.0 - absx));
- }
- else { /* 0.5 <= |x| <= 1.0 */
- t = 0.5 * log1p((absx + absx) / (1.0 - absx));
- }
- return copysign(t, x);
+ }
+ if (absx < two_pow_m28) { /* |x| < 2**-28 */
+ return x;
+ }
+ if (absx < 0.5) { /* |x| < 0.5 */
+ t = absx+absx;
+ t = 0.5 * log1p(t + t*absx / (1.0 - absx));
+ }
+ else { /* 0.5 <= |x| <= 1.0 */
+ t = 0.5 * log1p((absx + absx) / (1.0 - absx));
+ }
+ return copysign(t, x);
}
#endif /* HAVE_ATANH */
diff --git a/Python/pystate.c b/Python/pystate.c
index 3d5cf04..1c468c3 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -58,95 +58,95 @@ static void _PyGILState_NoteThreadState(PyThreadState* tstate);
PyInterpreterState *
PyInterpreterState_New(void)
{
- PyInterpreterState *interp = (PyInterpreterState *)
- malloc(sizeof(PyInterpreterState));
+ PyInterpreterState *interp = (PyInterpreterState *)
+ malloc(sizeof(PyInterpreterState));
- if (interp != NULL) {
- HEAD_INIT();
+ if (interp != NULL) {
+ HEAD_INIT();
#ifdef WITH_THREAD
- if (head_mutex == NULL)
- Py_FatalError("Can't initialize threads for interpreter");
+ if (head_mutex == NULL)
+ Py_FatalError("Can't initialize threads for interpreter");
#endif
- interp->modules = NULL;
- interp->modules_reloading = NULL;
- interp->modules_by_index = NULL;
- interp->sysdict = NULL;
- interp->builtins = NULL;
- interp->tstate_head = NULL;
- interp->codec_search_path = NULL;
- interp->codec_search_cache = NULL;
- interp->codec_error_registry = NULL;
- interp->codecs_initialized = 0;
+ interp->modules = NULL;
+ interp->modules_reloading = NULL;
+ interp->modules_by_index = NULL;
+ interp->sysdict = NULL;
+ interp->builtins = NULL;
+ interp->tstate_head = NULL;
+ interp->codec_search_path = NULL;
+ interp->codec_search_cache = NULL;
+ interp->codec_error_registry = NULL;
+ interp->codecs_initialized = 0;
#ifdef HAVE_DLOPEN
#ifdef RTLD_NOW
- interp->dlopenflags = RTLD_NOW;
+ interp->dlopenflags = RTLD_NOW;
#else
- interp->dlopenflags = RTLD_LAZY;
+ interp->dlopenflags = RTLD_LAZY;
#endif
#endif
#ifdef WITH_TSC
- interp->tscdump = 0;
+ interp->tscdump = 0;
#endif
- HEAD_LOCK();
- interp->next = interp_head;
- interp_head = interp;
- HEAD_UNLOCK();
- }
+ HEAD_LOCK();
+ interp->next = interp_head;
+ interp_head = interp;
+ HEAD_UNLOCK();
+ }
- return interp;
+ return interp;
}
void
PyInterpreterState_Clear(PyInterpreterState *interp)
{
- PyThreadState *p;
- HEAD_LOCK();
- for (p = interp->tstate_head; p != NULL; p = p->next)
- PyThreadState_Clear(p);
- HEAD_UNLOCK();
- Py_CLEAR(interp->codec_search_path);
- Py_CLEAR(interp->codec_search_cache);
- Py_CLEAR(interp->codec_error_registry);
- Py_CLEAR(interp->modules);
- Py_CLEAR(interp->modules_by_index);
- Py_CLEAR(interp->modules_reloading);
- Py_CLEAR(interp->sysdict);
- Py_CLEAR(interp->builtins);
+ PyThreadState *p;
+ HEAD_LOCK();
+ for (p = interp->tstate_head; p != NULL; p = p->next)
+ PyThreadState_Clear(p);
+ HEAD_UNLOCK();
+ Py_CLEAR(interp->codec_search_path);
+ Py_CLEAR(interp->codec_search_cache);
+ Py_CLEAR(interp->codec_error_registry);
+ Py_CLEAR(interp->modules);
+ Py_CLEAR(interp->modules_by_index);
+ Py_CLEAR(interp->modules_reloading);
+ Py_CLEAR(interp->sysdict);
+ Py_CLEAR(interp->builtins);
}
static void
zapthreads(PyInterpreterState *interp)
{
- PyThreadState *p;
- /* No need to lock the mutex here because this should only happen
- when the threads are all really dead (XXX famous last words). */
- while ((p = interp->tstate_head) != NULL) {
- PyThreadState_Delete(p);
- }
+ PyThreadState *p;
+ /* No need to lock the mutex here because this should only happen
+ when the threads are all really dead (XXX famous last words). */
+ while ((p = interp->tstate_head) != NULL) {
+ PyThreadState_Delete(p);
+ }
}
void
PyInterpreterState_Delete(PyInterpreterState *interp)
{
- PyInterpreterState **p;
- zapthreads(interp);
- HEAD_LOCK();
- for (p = &interp_head; ; p = &(*p)->next) {
- if (*p == NULL)
- Py_FatalError(
- "PyInterpreterState_Delete: invalid interp");
- if (*p == interp)
- break;
- }
- if (interp->tstate_head != NULL)
- Py_FatalError("PyInterpreterState_Delete: remaining threads");
- *p = interp->next;
- HEAD_UNLOCK();
- free(interp);
+ PyInterpreterState **p;
+ zapthreads(interp);
+ HEAD_LOCK();
+ for (p = &interp_head; ; p = &(*p)->next) {
+ if (*p == NULL)
+ Py_FatalError(
+ "PyInterpreterState_Delete: invalid interp");
+ if (*p == interp)
+ break;
+ }
+ if (interp->tstate_head != NULL)
+ Py_FatalError("PyInterpreterState_Delete: remaining threads");
+ *p = interp->next;
+ HEAD_UNLOCK();
+ free(interp);
}
@@ -154,141 +154,141 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
static struct _frame *
threadstate_getframe(PyThreadState *self)
{
- return self->frame;
+ return self->frame;
}
static PyThreadState *
new_threadstate(PyInterpreterState *interp, int init)
{
- PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
-
- if (_PyThreadState_GetFrame == NULL)
- _PyThreadState_GetFrame = threadstate_getframe;
-
- if (tstate != NULL) {
- tstate->interp = interp;
-
- tstate->frame = NULL;
- tstate->recursion_depth = 0;
- tstate->overflowed = 0;
- tstate->recursion_critical = 0;
- tstate->tracing = 0;
- tstate->use_tracing = 0;
- tstate->tick_counter = 0;
- tstate->gilstate_counter = 0;
- tstate->async_exc = NULL;
+ PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
+
+ if (_PyThreadState_GetFrame == NULL)
+ _PyThreadState_GetFrame = threadstate_getframe;
+
+ if (tstate != NULL) {
+ tstate->interp = interp;
+
+ tstate->frame = NULL;
+ tstate->recursion_depth = 0;
+ tstate->overflowed = 0;
+ tstate->recursion_critical = 0;
+ tstate->tracing = 0;
+ tstate->use_tracing = 0;
+ tstate->tick_counter = 0;
+ tstate->gilstate_counter = 0;
+ tstate->async_exc = NULL;
#ifdef WITH_THREAD
- tstate->thread_id = PyThread_get_thread_ident();
+ tstate->thread_id = PyThread_get_thread_ident();
#else
- tstate->thread_id = 0;
+ tstate->thread_id = 0;
#endif
- tstate->dict = NULL;
+ tstate->dict = NULL;
- tstate->curexc_type = NULL;
- tstate->curexc_value = NULL;
- tstate->curexc_traceback = NULL;
+ tstate->curexc_type = NULL;
+ tstate->curexc_value = NULL;
+ tstate->curexc_traceback = NULL;
- tstate->exc_type = NULL;
- tstate->exc_value = NULL;
- tstate->exc_traceback = NULL;
+ tstate->exc_type = NULL;
+ tstate->exc_value = NULL;
+ tstate->exc_traceback = NULL;
- tstate->c_profilefunc = NULL;
- tstate->c_tracefunc = NULL;
- tstate->c_profileobj = NULL;
- tstate->c_traceobj = NULL;
+ tstate->c_profilefunc = NULL;
+ tstate->c_tracefunc = NULL;
+ tstate->c_profileobj = NULL;
+ tstate->c_traceobj = NULL;
- if (init)
- _PyThreadState_Init(tstate);
+ if (init)
+ _PyThreadState_Init(tstate);
- HEAD_LOCK();
- tstate->next = interp->tstate_head;
- interp->tstate_head = tstate;
- HEAD_UNLOCK();
- }
+ HEAD_LOCK();
+ tstate->next = interp->tstate_head;
+ interp->tstate_head = tstate;
+ HEAD_UNLOCK();
+ }
- return tstate;
+ return tstate;
}
PyThreadState *
PyThreadState_New(PyInterpreterState *interp)
{
- return new_threadstate(interp, 1);
+ return new_threadstate(interp, 1);
}
PyThreadState *
_PyThreadState_Prealloc(PyInterpreterState *interp)
{
- return new_threadstate(interp, 0);
+ return new_threadstate(interp, 0);
}
void
_PyThreadState_Init(PyThreadState *tstate)
{
#ifdef WITH_THREAD
- _PyGILState_NoteThreadState(tstate);
+ _PyGILState_NoteThreadState(tstate);
#endif
}
PyObject*
PyState_FindModule(struct PyModuleDef* m)
{
- Py_ssize_t index = m->m_base.m_index;
- PyInterpreterState *state = PyThreadState_GET()->interp;
- PyObject *res;
- if (index == 0)
- return NULL;
- if (state->modules_by_index == NULL)
- return NULL;
- if (index > PyList_GET_SIZE(state->modules_by_index))
- return NULL;
- res = PyList_GET_ITEM(state->modules_by_index, index);
- return res==Py_None ? NULL : res;
+ Py_ssize_t index = m->m_base.m_index;
+ PyInterpreterState *state = PyThreadState_GET()->interp;
+ PyObject *res;
+ if (index == 0)
+ return NULL;
+ if (state->modules_by_index == NULL)
+ return NULL;
+ if (index > PyList_GET_SIZE(state->modules_by_index))
+ return NULL;
+ res = PyList_GET_ITEM(state->modules_by_index, index);
+ return res==Py_None ? NULL : res;
}
int
_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
{
- PyInterpreterState *state = PyThreadState_GET()->interp;
- if (!def)
- return -1;
- if (!state->modules_by_index) {
- state->modules_by_index = PyList_New(0);
- if (!state->modules_by_index)
- return -1;
- }
- while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
- if (PyList_Append(state->modules_by_index, Py_None) < 0)
- return -1;
- Py_INCREF(module);
- return PyList_SetItem(state->modules_by_index,
- def->m_base.m_index, module);
+ PyInterpreterState *state = PyThreadState_GET()->interp;
+ if (!def)
+ return -1;
+ if (!state->modules_by_index) {
+ state->modules_by_index = PyList_New(0);
+ if (!state->modules_by_index)
+ return -1;
+ }
+ while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
+ if (PyList_Append(state->modules_by_index, Py_None) < 0)
+ return -1;
+ Py_INCREF(module);
+ return PyList_SetItem(state->modules_by_index,
+ def->m_base.m_index, module);
}
void
PyThreadState_Clear(PyThreadState *tstate)
{
- if (Py_VerboseFlag && tstate->frame != NULL)
- fprintf(stderr,
- "PyThreadState_Clear: warning: thread still has a frame\n");
+ if (Py_VerboseFlag && tstate->frame != NULL)
+ fprintf(stderr,
+ "PyThreadState_Clear: warning: thread still has a frame\n");
- Py_CLEAR(tstate->frame);
+ Py_CLEAR(tstate->frame);
- Py_CLEAR(tstate->dict);
- Py_CLEAR(tstate->async_exc);
+ Py_CLEAR(tstate->dict);
+ Py_CLEAR(tstate->async_exc);
- Py_CLEAR(tstate->curexc_type);
- Py_CLEAR(tstate->curexc_value);
- Py_CLEAR(tstate->curexc_traceback);
+ Py_CLEAR(tstate->curexc_type);
+ Py_CLEAR(tstate->curexc_value);
+ Py_CLEAR(tstate->curexc_traceback);
- Py_CLEAR(tstate->exc_type);
- Py_CLEAR(tstate->exc_value);
- Py_CLEAR(tstate->exc_traceback);
+ Py_CLEAR(tstate->exc_type);
+ Py_CLEAR(tstate->exc_value);
+ Py_CLEAR(tstate->exc_traceback);
- tstate->c_profilefunc = NULL;
- tstate->c_tracefunc = NULL;
- Py_CLEAR(tstate->c_profileobj);
- Py_CLEAR(tstate->c_traceobj);
+ tstate->c_profilefunc = NULL;
+ tstate->c_tracefunc = NULL;
+ Py_CLEAR(tstate->c_profileobj);
+ Py_CLEAR(tstate->c_traceobj);
}
@@ -296,50 +296,50 @@ PyThreadState_Clear(PyThreadState *tstate)
static void
tstate_delete_common(PyThreadState *tstate)
{
- PyInterpreterState *interp;
- PyThreadState **p;
- PyThreadState *prev_p = NULL;
- if (tstate == NULL)
- Py_FatalError("PyThreadState_Delete: NULL tstate");
- interp = tstate->interp;
- if (interp == NULL)
- Py_FatalError("PyThreadState_Delete: NULL interp");
- HEAD_LOCK();
- for (p = &interp->tstate_head; ; p = &(*p)->next) {
- if (*p == NULL)
- Py_FatalError(
- "PyThreadState_Delete: invalid tstate");
- if (*p == tstate)
- break;
- /* Sanity check. These states should never happen but if
- * they do we must abort. Otherwise we'll end up spinning in
- * in a tight loop with the lock held. A similar check is done
- * in thread.c find_key(). */
- if (*p == prev_p)
- Py_FatalError(
- "PyThreadState_Delete: small circular list(!)"
- " and tstate not found.");
- prev_p = *p;
- if ((*p)->next == interp->tstate_head)
- Py_FatalError(
- "PyThreadState_Delete: circular list(!) and"
- " tstate not found.");
- }
- *p = tstate->next;
- HEAD_UNLOCK();
- free(tstate);
+ PyInterpreterState *interp;
+ PyThreadState **p;
+ PyThreadState *prev_p = NULL;
+ if (tstate == NULL)
+ Py_FatalError("PyThreadState_Delete: NULL tstate");
+ interp = tstate->interp;
+ if (interp == NULL)
+ Py_FatalError("PyThreadState_Delete: NULL interp");
+ HEAD_LOCK();
+ for (p = &interp->tstate_head; ; p = &(*p)->next) {
+ if (*p == NULL)
+ Py_FatalError(
+ "PyThreadState_Delete: invalid tstate");
+ if (*p == tstate)
+ break;
+ /* Sanity check. These states should never happen but if
+ * they do we must abort. Otherwise we'll end up spinning in
+ * in a tight loop with the lock held. A similar check is done
+ * in thread.c find_key(). */
+ if (*p == prev_p)
+ Py_FatalError(
+ "PyThreadState_Delete: small circular list(!)"
+ " and tstate not found.");
+ prev_p = *p;
+ if ((*p)->next == interp->tstate_head)
+ Py_FatalError(
+ "PyThreadState_Delete: circular list(!) and"
+ " tstate not found.");
+ }
+ *p = tstate->next;
+ HEAD_UNLOCK();
+ free(tstate);
}
void
PyThreadState_Delete(PyThreadState *tstate)
{
- if (tstate == _PyThreadState_Current)
- Py_FatalError("PyThreadState_Delete: tstate is still current");
- tstate_delete_common(tstate);
+ if (tstate == _PyThreadState_Current)
+ Py_FatalError("PyThreadState_Delete: tstate is still current");
+ tstate_delete_common(tstate);
#ifdef WITH_THREAD
- if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
- PyThread_delete_key_value(autoTLSkey);
+ if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
+ PyThread_delete_key_value(autoTLSkey);
#endif /* WITH_THREAD */
}
@@ -348,15 +348,15 @@ PyThreadState_Delete(PyThreadState *tstate)
void
PyThreadState_DeleteCurrent()
{
- PyThreadState *tstate = _PyThreadState_Current;
- if (tstate == NULL)
- Py_FatalError(
- "PyThreadState_DeleteCurrent: no current tstate");
- _PyThreadState_Current = NULL;
- tstate_delete_common(tstate);
- if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
- PyThread_delete_key_value(autoTLSkey);
- PyEval_ReleaseLock();
+ PyThreadState *tstate = _PyThreadState_Current;
+ if (tstate == NULL)
+ Py_FatalError(
+ "PyThreadState_DeleteCurrent: no current tstate");
+ _PyThreadState_Current = NULL;
+ tstate_delete_common(tstate);
+ if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
+ PyThread_delete_key_value(autoTLSkey);
+ PyEval_ReleaseLock();
}
#endif /* WITH_THREAD */
@@ -364,36 +364,36 @@ PyThreadState_DeleteCurrent()
PyThreadState *
PyThreadState_Get(void)
{
- if (_PyThreadState_Current == NULL)
- Py_FatalError("PyThreadState_Get: no current thread");
+ if (_PyThreadState_Current == NULL)
+ Py_FatalError("PyThreadState_Get: no current thread");
- return _PyThreadState_Current;
+ return _PyThreadState_Current;
}
PyThreadState *
PyThreadState_Swap(PyThreadState *newts)
{
- PyThreadState *oldts = _PyThreadState_Current;
+ PyThreadState *oldts = _PyThreadState_Current;
- _PyThreadState_Current = newts;
- /* It should not be possible for more than one thread state
- to be used for a thread. Check this the best we can in debug
- builds.
- */
+ _PyThreadState_Current = newts;
+ /* It should not be possible for more than one thread state
+ to be used for a thread. Check this the best we can in debug
+ builds.
+ */
#if defined(Py_DEBUG) && defined(WITH_THREAD)
- if (newts) {
- /* This can be called from PyEval_RestoreThread(). Similar
- to it, we need to ensure errno doesn't change.
- */
- int err = errno;
- PyThreadState *check = PyGILState_GetThisThreadState();
- if (check && check->interp == newts->interp && check != newts)
- Py_FatalError("Invalid thread state for this thread");
- errno = err;
- }
+ if (newts) {
+ /* This can be called from PyEval_RestoreThread(). Similar
+ to it, we need to ensure errno doesn't change.
+ */
+ int err = errno;
+ PyThreadState *check = PyGILState_GetThisThreadState();
+ if (check && check->interp == newts->interp && check != newts)
+ Py_FatalError("Invalid thread state for this thread");
+ errno = err;
+ }
#endif
- return oldts;
+ return oldts;
}
/* An extension mechanism to store arbitrary additional per-thread state.
@@ -405,16 +405,16 @@ PyThreadState_Swap(PyThreadState *newts)
PyObject *
PyThreadState_GetDict(void)
{
- if (_PyThreadState_Current == NULL)
- return NULL;
+ if (_PyThreadState_Current == NULL)
+ return NULL;
- if (_PyThreadState_Current->dict == NULL) {
- PyObject *d;
- _PyThreadState_Current->dict = d = PyDict_New();
- if (d == NULL)
- PyErr_Clear();
- }
- return _PyThreadState_Current->dict;
+ if (_PyThreadState_Current->dict == NULL) {
+ PyObject *d;
+ _PyThreadState_Current->dict = d = PyDict_New();
+ if (d == NULL)
+ PyErr_Clear();
+ }
+ return _PyThreadState_Current->dict;
}
@@ -428,36 +428,36 @@ PyThreadState_GetDict(void)
int
PyThreadState_SetAsyncExc(long id, PyObject *exc) {
- PyThreadState *tstate = PyThreadState_GET();
- PyInterpreterState *interp = tstate->interp;
- PyThreadState *p;
-
- /* Although the GIL is held, a few C API functions can be called
- * without the GIL held, and in particular some that create and
- * destroy thread and interpreter states. Those can mutate the
- * list of thread states we're traversing, so to prevent that we lock
- * head_mutex for the duration.
- */
- HEAD_LOCK();
- for (p = interp->tstate_head; p != NULL; p = p->next) {
- if (p->thread_id == id) {
- /* Tricky: we need to decref the current value
- * (if any) in p->async_exc, but that can in turn
- * allow arbitrary Python code to run, including
- * perhaps calls to this function. To prevent
- * deadlock, we need to release head_mutex before
- * the decref.
- */
- PyObject *old_exc = p->async_exc;
- Py_XINCREF(exc);
- p->async_exc = exc;
- HEAD_UNLOCK();
- Py_XDECREF(old_exc);
- return 1;
- }
- }
- HEAD_UNLOCK();
- return 0;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyInterpreterState *interp = tstate->interp;
+ PyThreadState *p;
+
+ /* Although the GIL is held, a few C API functions can be called
+ * without the GIL held, and in particular some that create and
+ * destroy thread and interpreter states. Those can mutate the
+ * list of thread states we're traversing, so to prevent that we lock
+ * head_mutex for the duration.
+ */
+ HEAD_LOCK();
+ for (p = interp->tstate_head; p != NULL; p = p->next) {
+ if (p->thread_id == id) {
+ /* Tricky: we need to decref the current value
+ * (if any) in p->async_exc, but that can in turn
+ * allow arbitrary Python code to run, including
+ * perhaps calls to this function. To prevent
+ * deadlock, we need to release head_mutex before
+ * the decref.
+ */
+ PyObject *old_exc = p->async_exc;
+ Py_XINCREF(exc);
+ p->async_exc = exc;
+ HEAD_UNLOCK();
+ Py_XDECREF(old_exc);
+ return 1;
+ }
+ }
+ HEAD_UNLOCK();
+ return 0;
}
@@ -467,22 +467,22 @@ PyThreadState_SetAsyncExc(long id, PyObject *exc) {
PyInterpreterState *
PyInterpreterState_Head(void)
{
- return interp_head;
+ return interp_head;
}
PyInterpreterState *
PyInterpreterState_Next(PyInterpreterState *interp) {
- return interp->next;
+ return interp->next;
}
PyThreadState *
PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
- return interp->tstate_head;
+ return interp->tstate_head;
}
PyThreadState *
PyThreadState_Next(PyThreadState *tstate) {
- return tstate->next;
+ return tstate->next;
}
/* The implementation of sys._current_frames(). This is intended to be
@@ -493,44 +493,44 @@ PyThreadState_Next(PyThreadState *tstate) {
PyObject *
_PyThread_CurrentFrames(void)
{
- PyObject *result;
- PyInterpreterState *i;
-
- result = PyDict_New();
- if (result == NULL)
- return NULL;
-
- /* for i in all interpreters:
- * for t in all of i's thread states:
- * if t's frame isn't NULL, map t's id to its frame
- * Because these lists can mutute even when the GIL is held, we
- * need to grab head_mutex for the duration.
- */
- HEAD_LOCK();
- for (i = interp_head; i != NULL; i = i->next) {
- PyThreadState *t;
- for (t = i->tstate_head; t != NULL; t = t->next) {
- PyObject *id;
- int stat;
- struct _frame *frame = t->frame;
- if (frame == NULL)
- continue;
- id = PyLong_FromLong(t->thread_id);
- if (id == NULL)
- goto Fail;
- stat = PyDict_SetItem(result, id, (PyObject *)frame);
- Py_DECREF(id);
- if (stat < 0)
- goto Fail;
- }
- }
- HEAD_UNLOCK();
- return result;
+ PyObject *result;
+ PyInterpreterState *i;
+
+ result = PyDict_New();
+ if (result == NULL)
+ return NULL;
+
+ /* for i in all interpreters:
+ * for t in all of i's thread states:
+ * if t's frame isn't NULL, map t's id to its frame
+ * Because these lists can mutute even when the GIL is held, we
+ * need to grab head_mutex for the duration.
+ */
+ HEAD_LOCK();
+ for (i = interp_head; i != NULL; i = i->next) {
+ PyThreadState *t;
+ for (t = i->tstate_head; t != NULL; t = t->next) {
+ PyObject *id;
+ int stat;
+ struct _frame *frame = t->frame;
+ if (frame == NULL)
+ continue;
+ id = PyLong_FromLong(t->thread_id);
+ if (id == NULL)
+ goto Fail;
+ stat = PyDict_SetItem(result, id, (PyObject *)frame);
+ Py_DECREF(id);
+ if (stat < 0)
+ goto Fail;
+ }
+ }
+ HEAD_UNLOCK();
+ return result;
Fail:
- HEAD_UNLOCK();
- Py_DECREF(result);
- return NULL;
+ HEAD_UNLOCK();
+ Py_DECREF(result);
+ return NULL;
}
/* Python "auto thread state" API. */
@@ -547,12 +547,12 @@ _PyThread_CurrentFrames(void)
static int
PyThreadState_IsCurrent(PyThreadState *tstate)
{
- /* Must be the tstate for this thread */
- assert(PyGILState_GetThisThreadState()==tstate);
- /* On Windows at least, simple reads and writes to 32 bit values
- are atomic.
- */
- return tstate == _PyThreadState_Current;
+ /* Must be the tstate for this thread */
+ assert(PyGILState_GetThisThreadState()==tstate);
+ /* On Windows at least, simple reads and writes to 32 bit values
+ are atomic.
+ */
+ return tstate == _PyThreadState_Current;
}
/* Internal initialization/finalization functions called by
@@ -561,21 +561,21 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
void
_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
{
- assert(i && t); /* must init with valid states */
- autoTLSkey = PyThread_create_key();
- autoInterpreterState = i;
- assert(PyThread_get_key_value(autoTLSkey) == NULL);
- assert(t->gilstate_counter == 0);
+ assert(i && t); /* must init with valid states */
+ autoTLSkey = PyThread_create_key();
+ autoInterpreterState = i;
+ assert(PyThread_get_key_value(autoTLSkey) == NULL);
+ assert(t->gilstate_counter == 0);
- _PyGILState_NoteThreadState(t);
+ _PyGILState_NoteThreadState(t);
}
void
_PyGILState_Fini(void)
{
- PyThread_delete_key(autoTLSkey);
- autoTLSkey = 0;
- autoInterpreterState = NULL;
+ PyThread_delete_key(autoTLSkey);
+ autoTLSkey = 0;
+ autoInterpreterState = NULL;
}
/* When a thread state is created for a thread by some mechanism other than
@@ -586,113 +586,113 @@ _PyGILState_Fini(void)
static void
_PyGILState_NoteThreadState(PyThreadState* tstate)
{
- /* If autoTLSkey is 0, this must be the very first threadstate created
- in Py_Initialize(). Don't do anything for now (we'll be back here
- when _PyGILState_Init is called). */
- if (!autoTLSkey)
- return;
+ /* If autoTLSkey is 0, this must be the very first threadstate created
+ in Py_Initialize(). Don't do anything for now (we'll be back here
+ when _PyGILState_Init is called). */
+ if (!autoTLSkey)
+ return;
- /* Stick the thread state for this thread in thread local storage.
+ /* Stick the thread state for this thread in thread local storage.
- The only situation where you can legitimately have more than one
- thread state for an OS level thread is when there are multiple
- interpreters, when:
+ The only situation where you can legitimately have more than one
+ thread state for an OS level thread is when there are multiple
+ interpreters, when:
- a) You shouldn't really be using the PyGILState_ APIs anyway,
- and:
+ a) You shouldn't really be using the PyGILState_ APIs anyway,
+ and:
- b) The slightly odd way PyThread_set_key_value works (see
- comments by its implementation) means that the first thread
- state created for that given OS level thread will "win",
- which seems reasonable behaviour.
- */
- if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
- Py_FatalError("Couldn't create autoTLSkey mapping");
+ b) The slightly odd way PyThread_set_key_value works (see
+ comments by its implementation) means that the first thread
+ state created for that given OS level thread will "win",
+ which seems reasonable behaviour.
+ */
+ if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
+ Py_FatalError("Couldn't create autoTLSkey mapping");
- /* PyGILState_Release must not try to delete this thread state. */
- tstate->gilstate_counter = 1;
+ /* PyGILState_Release must not try to delete this thread state. */
+ tstate->gilstate_counter = 1;
}
/* The public functions */
PyThreadState *
PyGILState_GetThisThreadState(void)
{
- if (autoInterpreterState == NULL || autoTLSkey == 0)
- return NULL;
- return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
+ if (autoInterpreterState == NULL || autoTLSkey == 0)
+ return NULL;
+ return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
}
PyGILState_STATE
PyGILState_Ensure(void)
{
- int current;
- PyThreadState *tcur;
- /* Note that we do not auto-init Python here - apart from
- potential races with 2 threads auto-initializing, pep-311
- spells out other issues. Embedders are expected to have
- called Py_Initialize() and usually PyEval_InitThreads().
- */
- assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
- tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
- if (tcur == NULL) {
- /* Create a new thread state for this thread */
- tcur = PyThreadState_New(autoInterpreterState);
- if (tcur == NULL)
- Py_FatalError("Couldn't create thread-state for new thread");
- /* This is our thread state! We'll need to delete it in the
- matching call to PyGILState_Release(). */
- tcur->gilstate_counter = 0;
- current = 0; /* new thread state is never current */
- }
- else
- current = PyThreadState_IsCurrent(tcur);
- if (current == 0)
- PyEval_RestoreThread(tcur);
- /* Update our counter in the thread-state - no need for locks:
- - tcur will remain valid as we hold the GIL.
- - the counter is safe as we are the only thread "allowed"
- to modify this value
- */
- ++tcur->gilstate_counter;
- return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
+ int current;
+ PyThreadState *tcur;
+ /* Note that we do not auto-init Python here - apart from
+ potential races with 2 threads auto-initializing, pep-311
+ spells out other issues. Embedders are expected to have
+ called Py_Initialize() and usually PyEval_InitThreads().
+ */
+ assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
+ tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
+ if (tcur == NULL) {
+ /* Create a new thread state for this thread */
+ tcur = PyThreadState_New(autoInterpreterState);
+ if (tcur == NULL)
+ Py_FatalError("Couldn't create thread-state for new thread");
+ /* This is our thread state! We'll need to delete it in the
+ matching call to PyGILState_Release(). */
+ tcur->gilstate_counter = 0;
+ current = 0; /* new thread state is never current */
+ }
+ else
+ current = PyThreadState_IsCurrent(tcur);
+ if (current == 0)
+ PyEval_RestoreThread(tcur);
+ /* Update our counter in the thread-state - no need for locks:
+ - tcur will remain valid as we hold the GIL.
+ - the counter is safe as we are the only thread "allowed"
+ to modify this value
+ */
+ ++tcur->gilstate_counter;
+ return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
}
void
PyGILState_Release(PyGILState_STATE oldstate)
{
- PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
- autoTLSkey);
- if (tcur == NULL)
- Py_FatalError("auto-releasing thread-state, "
- "but no thread-state for this thread");
- /* We must hold the GIL and have our thread state current */
- /* XXX - remove the check - the assert should be fine,
- but while this is very new (April 2003), the extra check
- by release-only users can't hurt.
- */
- if (! PyThreadState_IsCurrent(tcur))
- Py_FatalError("This thread state must be current when releasing");
- assert(PyThreadState_IsCurrent(tcur));
- --tcur->gilstate_counter;
- assert(tcur->gilstate_counter >= 0); /* illegal counter value */
-
- /* If we're going to destroy this thread-state, we must
- * clear it while the GIL is held, as destructors may run.
- */
- if (tcur->gilstate_counter == 0) {
- /* can't have been locked when we created it */
- assert(oldstate == PyGILState_UNLOCKED);
- PyThreadState_Clear(tcur);
- /* Delete the thread-state. Note this releases the GIL too!
- * It's vital that the GIL be held here, to avoid shutdown
- * races; see bugs 225673 and 1061968 (that nasty bug has a
- * habit of coming back).
- */
- PyThreadState_DeleteCurrent();
- }
- /* Release the lock if necessary */
- else if (oldstate == PyGILState_UNLOCKED)
- PyEval_SaveThread();
+ PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
+ autoTLSkey);
+ if (tcur == NULL)
+ Py_FatalError("auto-releasing thread-state, "
+ "but no thread-state for this thread");
+ /* We must hold the GIL and have our thread state current */
+ /* XXX - remove the check - the assert should be fine,
+ but while this is very new (April 2003), the extra check
+ by release-only users can't hurt.
+ */
+ if (! PyThreadState_IsCurrent(tcur))
+ Py_FatalError("This thread state must be current when releasing");
+ assert(PyThreadState_IsCurrent(tcur));
+ --tcur->gilstate_counter;
+ assert(tcur->gilstate_counter >= 0); /* illegal counter value */
+
+ /* If we're going to destroy this thread-state, we must
+ * clear it while the GIL is held, as destructors may run.
+ */
+ if (tcur->gilstate_counter == 0) {
+ /* can't have been locked when we created it */
+ assert(oldstate == PyGILState_UNLOCKED);
+ PyThreadState_Clear(tcur);
+ /* Delete the thread-state. Note this releases the GIL too!
+ * It's vital that the GIL be held here, to avoid shutdown
+ * races; see bugs 225673 and 1061968 (that nasty bug has a
+ * habit of coming back).
+ */
+ PyThreadState_DeleteCurrent();
+ }
+ /* Release the lock if necessary */
+ else if (oldstate == PyGILState_UNLOCKED)
+ PyEval_SaveThread();
}
#ifdef __cplusplus
diff --git a/Python/pystrcmp.c b/Python/pystrcmp.c
index 84295e7..f9c2277 100644
--- a/Python/pystrcmp.c
+++ b/Python/pystrcmp.c
@@ -6,21 +6,21 @@
int
PyOS_mystrnicmp(const char *s1, const char *s2, Py_ssize_t size)
{
- if (size == 0)
- return 0;
- while ((--size > 0) &&
- (tolower((unsigned)*s1) == tolower((unsigned)*s2))) {
- if (!*s1++ || !*s2++)
- break;
- }
- return tolower((unsigned)*s1) - tolower((unsigned)*s2);
+ if (size == 0)
+ return 0;
+ while ((--size > 0) &&
+ (tolower((unsigned)*s1) == tolower((unsigned)*s2))) {
+ if (!*s1++ || !*s2++)
+ break;
+ }
+ return tolower((unsigned)*s1) - tolower((unsigned)*s2);
}
int
PyOS_mystricmp(const char *s1, const char *s2)
{
- while (*s1 && (tolower((unsigned)*s1++) == tolower((unsigned)*s2++))) {
- ;
- }
- return (tolower((unsigned)*s1) - tolower((unsigned)*s2));
+ while (*s1 && (tolower((unsigned)*s1++) == tolower((unsigned)*s2++))) {
+ ;
+ }
+ return (tolower((unsigned)*s1) - tolower((unsigned)*s2));
}
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index c2ce71d..f7ddd13 100644
--- a/Python/pystrtod.c
+++ b/Python/pystrtod.c
@@ -12,46 +12,46 @@
static int
case_insensitive_match(const char *s, const char *t)
{
- while(*t && Py_TOLOWER(*s) == *t) {
- s++;
- t++;
- }
- return *t ? 0 : 1;
+ while(*t && Py_TOLOWER(*s) == *t) {
+ s++;
+ t++;
+ }
+ return *t ? 0 : 1;
}
double
_Py_parse_inf_or_nan(const char *p, char **endptr)
{
- double retval;
- const char *s;
- int negate = 0;
-
- s = p;
- if (*s == '-') {
- negate = 1;
- s++;
- }
- else if (*s == '+') {
- s++;
- }
- if (case_insensitive_match(s, "inf")) {
- s += 3;
- if (case_insensitive_match(s, "inity"))
- s += 5;
- retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL;
- }
+ double retval;
+ const char *s;
+ int negate = 0;
+
+ s = p;
+ if (*s == '-') {
+ negate = 1;
+ s++;
+ }
+ else if (*s == '+') {
+ s++;
+ }
+ if (case_insensitive_match(s, "inf")) {
+ s += 3;
+ if (case_insensitive_match(s, "inity"))
+ s += 5;
+ retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL;
+ }
#ifdef Py_NAN
- else if (case_insensitive_match(s, "nan")) {
- s += 3;
- retval = negate ? -Py_NAN : Py_NAN;
- }
+ else if (case_insensitive_match(s, "nan")) {
+ s += 3;
+ retval = negate ? -Py_NAN : Py_NAN;
+ }
#endif
- else {
- s = p;
- retval = -1.0;
- }
- *endptr = (char *)s;
- return retval;
+ else {
+ s = p;
+ retval = -1.0;
+ }
+ *endptr = (char *)s;
+ return retval;
}
/**
@@ -59,7 +59,7 @@ _Py_parse_inf_or_nan(const char *p, char **endptr)
* @nptr: the string to convert to a numeric value.
* @endptr: if non-%NULL, it returns the character after
* the last character used in the conversion.
- *
+ *
* Converts a string to a #gdouble value.
* This function behaves like the standard strtod() function
* does in the C locale. It does this without actually
@@ -76,7 +76,7 @@ _Py_parse_inf_or_nan(const char *p, char **endptr)
* stored in %errno. If the correct value would cause underflow,
* zero is returned and %ERANGE is stored in %errno.
* If memory allocation fails, %ENOMEM is stored in %errno.
- *
+ *
* This function resets %errno before calling strtod() so that
* you can reliably detect overflow and underflow.
*
@@ -88,23 +88,23 @@ _Py_parse_inf_or_nan(const char *p, char **endptr)
double
_PyOS_ascii_strtod(const char *nptr, char **endptr)
{
- double result;
- _Py_SET_53BIT_PRECISION_HEADER;
+ double result;
+ _Py_SET_53BIT_PRECISION_HEADER;
- assert(nptr != NULL);
- /* Set errno to zero, so that we can distinguish zero results
- and underflows */
- errno = 0;
+ assert(nptr != NULL);
+ /* Set errno to zero, so that we can distinguish zero results
+ and underflows */
+ errno = 0;
- _Py_SET_53BIT_PRECISION_START;
- result = _Py_dg_strtod(nptr, endptr);
- _Py_SET_53BIT_PRECISION_END;
+ _Py_SET_53BIT_PRECISION_START;
+ result = _Py_dg_strtod(nptr, endptr);
+ _Py_SET_53BIT_PRECISION_END;
- if (*endptr == nptr)
- /* string might represent an inf or nan */
- result = _Py_parse_inf_or_nan(nptr, endptr);
+ if (*endptr == nptr)
+ /* string might represent an inf or nan */
+ result = _Py_parse_inf_or_nan(nptr, endptr);
- return result;
+ return result;
}
@@ -121,148 +121,148 @@ _PyOS_ascii_strtod(const char *nptr, char **endptr)
double
_PyOS_ascii_strtod(const char *nptr, char **endptr)
{
- char *fail_pos;
- double val = -1.0;
- struct lconv *locale_data;
- const char *decimal_point;
- size_t decimal_point_len;
- const char *p, *decimal_point_pos;
- const char *end = NULL; /* Silence gcc */
- const char *digits_pos = NULL;
- int negate = 0;
-
- assert(nptr != NULL);
-
- fail_pos = NULL;
-
- locale_data = localeconv();
- decimal_point = locale_data->decimal_point;
- decimal_point_len = strlen(decimal_point);
-
- assert(decimal_point_len != 0);
-
- decimal_point_pos = NULL;
-
- /* Parse infinities and nans */
- val = _Py_parse_inf_or_nan(nptr, endptr);
- if (*endptr != nptr)
- return val;
-
- /* Set errno to zero, so that we can distinguish zero results
- and underflows */
- errno = 0;
-
- /* We process the optional sign manually, then pass the remainder to
- the system strtod. This ensures that the result of an underflow
- has the correct sign. (bug #1725) */
- p = nptr;
- /* Process leading sign, if present */
- if (*p == '-') {
- negate = 1;
- p++;
- }
- else if (*p == '+') {
- p++;
- }
-
- /* Some platform strtods accept hex floats; Python shouldn't (at the
- moment), so we check explicitly for strings starting with '0x'. */
- if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))
- goto invalid_string;
-
- /* Check that what's left begins with a digit or decimal point */
- if (!Py_ISDIGIT(*p) && *p != '.')
- goto invalid_string;
-
- digits_pos = p;
- if (decimal_point[0] != '.' ||
- decimal_point[1] != 0)
- {
- /* Look for a '.' in the input; if present, it'll need to be
- swapped for the current locale's decimal point before we
- call strtod. On the other hand, if we find the current
- locale's decimal point then the input is invalid. */
- while (Py_ISDIGIT(*p))
- p++;
-
- if (*p == '.')
- {
- decimal_point_pos = p++;
-
- /* locate end of number */
- while (Py_ISDIGIT(*p))
- p++;
-
- if (*p == 'e' || *p == 'E')
- p++;
- if (*p == '+' || *p == '-')
- p++;
- while (Py_ISDIGIT(*p))
- p++;
- end = p;
- }
- else if (strncmp(p, decimal_point, decimal_point_len) == 0)
- /* Python bug #1417699 */
- goto invalid_string;
- /* For the other cases, we need not convert the decimal
- point */
- }
-
- if (decimal_point_pos) {
- char *copy, *c;
- /* Create a copy of the input, with the '.' converted to the
- locale-specific decimal point */
- copy = (char *)PyMem_MALLOC(end - digits_pos +
- 1 + decimal_point_len);
- if (copy == NULL) {
- *endptr = (char *)nptr;
- errno = ENOMEM;
- return val;
- }
-
- c = copy;
- memcpy(c, digits_pos, decimal_point_pos - digits_pos);
- c += decimal_point_pos - digits_pos;
- memcpy(c, decimal_point, decimal_point_len);
- c += decimal_point_len;
- memcpy(c, decimal_point_pos + 1,
- end - (decimal_point_pos + 1));
- c += end - (decimal_point_pos + 1);
- *c = 0;
-
- val = strtod(copy, &fail_pos);
-
- if (fail_pos)
- {
- if (fail_pos > decimal_point_pos)
- fail_pos = (char *)digits_pos +
- (fail_pos - copy) -
- (decimal_point_len - 1);
- else
- fail_pos = (char *)digits_pos +
- (fail_pos - copy);
- }
-
- PyMem_FREE(copy);
-
- }
- else {
- val = strtod(digits_pos, &fail_pos);
- }
-
- if (fail_pos == digits_pos)
- goto invalid_string;
-
- if (negate && fail_pos != nptr)
- val = -val;
- *endptr = fail_pos;
-
- return val;
+ char *fail_pos;
+ double val = -1.0;
+ struct lconv *locale_data;
+ const char *decimal_point;
+ size_t decimal_point_len;
+ const char *p, *decimal_point_pos;
+ const char *end = NULL; /* Silence gcc */
+ const char *digits_pos = NULL;
+ int negate = 0;
+
+ assert(nptr != NULL);
+
+ fail_pos = NULL;
+
+ locale_data = localeconv();
+ decimal_point = locale_data->decimal_point;
+ decimal_point_len = strlen(decimal_point);
+
+ assert(decimal_point_len != 0);
+
+ decimal_point_pos = NULL;
+
+ /* Parse infinities and nans */
+ val = _Py_parse_inf_or_nan(nptr, endptr);
+ if (*endptr != nptr)
+ return val;
+
+ /* Set errno to zero, so that we can distinguish zero results
+ and underflows */
+ errno = 0;
+
+ /* We process the optional sign manually, then pass the remainder to
+ the system strtod. This ensures that the result of an underflow
+ has the correct sign. (bug #1725) */
+ p = nptr;
+ /* Process leading sign, if present */
+ if (*p == '-') {
+ negate = 1;
+ p++;
+ }
+ else if (*p == '+') {
+ p++;
+ }
+
+ /* Some platform strtods accept hex floats; Python shouldn't (at the
+ moment), so we check explicitly for strings starting with '0x'. */
+ if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))
+ goto invalid_string;
+
+ /* Check that what's left begins with a digit or decimal point */
+ if (!Py_ISDIGIT(*p) && *p != '.')
+ goto invalid_string;
+
+ digits_pos = p;
+ if (decimal_point[0] != '.' ||
+ decimal_point[1] != 0)
+ {
+ /* Look for a '.' in the input; if present, it'll need to be
+ swapped for the current locale's decimal point before we
+ call strtod. On the other hand, if we find the current
+ locale's decimal point then the input is invalid. */
+ while (Py_ISDIGIT(*p))
+ p++;
+
+ if (*p == '.')
+ {
+ decimal_point_pos = p++;
+
+ /* locate end of number */
+ while (Py_ISDIGIT(*p))
+ p++;
+
+ if (*p == 'e' || *p == 'E')
+ p++;
+ if (*p == '+' || *p == '-')
+ p++;
+ while (Py_ISDIGIT(*p))
+ p++;
+ end = p;
+ }
+ else if (strncmp(p, decimal_point, decimal_point_len) == 0)
+ /* Python bug #1417699 */
+ goto invalid_string;
+ /* For the other cases, we need not convert the decimal
+ point */
+ }
+
+ if (decimal_point_pos) {
+ char *copy, *c;
+ /* Create a copy of the input, with the '.' converted to the
+ locale-specific decimal point */
+ copy = (char *)PyMem_MALLOC(end - digits_pos +
+ 1 + decimal_point_len);
+ if (copy == NULL) {
+ *endptr = (char *)nptr;
+ errno = ENOMEM;
+ return val;
+ }
+
+ c = copy;
+ memcpy(c, digits_pos, decimal_point_pos - digits_pos);
+ c += decimal_point_pos - digits_pos;
+ memcpy(c, decimal_point, decimal_point_len);
+ c += decimal_point_len;
+ memcpy(c, decimal_point_pos + 1,
+ end - (decimal_point_pos + 1));
+ c += end - (decimal_point_pos + 1);
+ *c = 0;
+
+ val = strtod(copy, &fail_pos);
+
+ if (fail_pos)
+ {
+ if (fail_pos > decimal_point_pos)
+ fail_pos = (char *)digits_pos +
+ (fail_pos - copy) -
+ (decimal_point_len - 1);
+ else
+ fail_pos = (char *)digits_pos +
+ (fail_pos - copy);
+ }
+
+ PyMem_FREE(copy);
+
+ }
+ else {
+ val = strtod(digits_pos, &fail_pos);
+ }
+
+ if (fail_pos == digits_pos)
+ goto invalid_string;
+
+ if (negate && fail_pos != nptr)
+ val = -val;
+ *endptr = fail_pos;
+
+ return val;
invalid_string:
- *endptr = (char*)nptr;
- errno = EINVAL;
- return -1.0;
+ *endptr = (char*)nptr;
+ errno = EINVAL;
+ return -1.0;
}
#endif
@@ -272,27 +272,27 @@ _PyOS_ascii_strtod(const char *nptr, char **endptr)
double
PyOS_ascii_strtod(const char *nptr, char **endptr)
{
- char *fail_pos;
- const char *p;
- double x;
-
- if (PyErr_WarnEx(PyExc_DeprecationWarning,
- "PyOS_ascii_strtod and PyOS_ascii_atof are "
- "deprecated. Use PyOS_string_to_double "
- "instead.", 1) < 0)
- return -1.0;
-
- /* _PyOS_ascii_strtod already does everything that we want,
- except that it doesn't parse leading whitespace */
- p = nptr;
- while (Py_ISSPACE(*p))
- p++;
- x = _PyOS_ascii_strtod(p, &fail_pos);
- if (fail_pos == p)
- fail_pos = (char *)nptr;
- if (endptr)
- *endptr = (char *)fail_pos;
- return x;
+ char *fail_pos;
+ const char *p;
+ double x;
+
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "PyOS_ascii_strtod and PyOS_ascii_atof are "
+ "deprecated. Use PyOS_string_to_double "
+ "instead.", 1) < 0)
+ return -1.0;
+
+ /* _PyOS_ascii_strtod already does everything that we want,
+ except that it doesn't parse leading whitespace */
+ p = nptr;
+ while (Py_ISSPACE(*p))
+ p++;
+ x = _PyOS_ascii_strtod(p, &fail_pos);
+ if (fail_pos == p)
+ fail_pos = (char *)nptr;
+ if (endptr)
+ *endptr = (char *)fail_pos;
+ return x;
}
/* PyOS_ascii_strtod is DEPRECATED in Python 3.1 */
@@ -300,7 +300,7 @@ PyOS_ascii_strtod(const char *nptr, char **endptr)
double
PyOS_ascii_atof(const char *nptr)
{
- return PyOS_ascii_strtod(nptr, NULL);
+ return PyOS_ascii_strtod(nptr, NULL);
}
/* PyOS_string_to_double is the recommended replacement for the deprecated
@@ -331,39 +331,39 @@ PyOS_ascii_atof(const char *nptr)
double
PyOS_string_to_double(const char *s,
- char **endptr,
- PyObject *overflow_exception)
+ char **endptr,
+ PyObject *overflow_exception)
{
- double x, result=-1.0;
- char *fail_pos;
-
- errno = 0;
- PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)
- x = _PyOS_ascii_strtod(s, &fail_pos);
- PyFPE_END_PROTECT(x)
-
- if (errno == ENOMEM) {
- PyErr_NoMemory();
- fail_pos = (char *)s;
- }
- else if (!endptr && (fail_pos == s || *fail_pos != '\0'))
- PyErr_Format(PyExc_ValueError,
- "could not convert string to float: "
- "%.200s", s);
- else if (fail_pos == s)
- PyErr_Format(PyExc_ValueError,
- "could not convert string to float: "
- "%.200s", s);
- else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)
- PyErr_Format(overflow_exception,
- "value too large to convert to float: "
- "%.200s", s);
- else
- result = x;
-
- if (endptr != NULL)
- *endptr = fail_pos;
- return result;
+ double x, result=-1.0;
+ char *fail_pos;
+
+ errno = 0;
+ PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)
+ x = _PyOS_ascii_strtod(s, &fail_pos);
+ PyFPE_END_PROTECT(x)
+
+ if (errno == ENOMEM) {
+ PyErr_NoMemory();
+ fail_pos = (char *)s;
+ }
+ else if (!endptr && (fail_pos == s || *fail_pos != '\0'))
+ PyErr_Format(PyExc_ValueError,
+ "could not convert string to float: "
+ "%.200s", s);
+ else if (fail_pos == s)
+ PyErr_Format(PyExc_ValueError,
+ "could not convert string to float: "
+ "%.200s", s);
+ else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)
+ PyErr_Format(overflow_exception,
+ "value too large to convert to float: "
+ "%.200s", s);
+ else
+ result = x;
+
+ if (endptr != NULL)
+ *endptr = fail_pos;
+ return result;
}
/* Given a string that may have a decimal point in the current
@@ -372,30 +372,30 @@ PyOS_string_to_double(const char *s,
Py_LOCAL_INLINE(void)
change_decimal_from_locale_to_dot(char* buffer)
{
- struct lconv *locale_data = localeconv();
- const char *decimal_point = locale_data->decimal_point;
-
- if (decimal_point[0] != '.' || decimal_point[1] != 0) {
- size_t decimal_point_len = strlen(decimal_point);
-
- if (*buffer == '+' || *buffer == '-')
- buffer++;
- while (Py_ISDIGIT(*buffer))
- buffer++;
- if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
- *buffer = '.';
- buffer++;
- if (decimal_point_len > 1) {
- /* buffer needs to get smaller */
- size_t rest_len = strlen(buffer +
- (decimal_point_len - 1));
- memmove(buffer,
- buffer + (decimal_point_len - 1),
- rest_len);
- buffer[rest_len] = 0;
- }
- }
- }
+ struct lconv *locale_data = localeconv();
+ const char *decimal_point = locale_data->decimal_point;
+
+ if (decimal_point[0] != '.' || decimal_point[1] != 0) {
+ size_t decimal_point_len = strlen(decimal_point);
+
+ if (*buffer == '+' || *buffer == '-')
+ buffer++;
+ while (Py_ISDIGIT(*buffer))
+ buffer++;
+ if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
+ *buffer = '.';
+ buffer++;
+ if (decimal_point_len > 1) {
+ /* buffer needs to get smaller */
+ size_t rest_len = strlen(buffer +
+ (decimal_point_len - 1));
+ memmove(buffer,
+ buffer + (decimal_point_len - 1),
+ rest_len);
+ buffer[rest_len] = 0;
+ }
+ }
+ }
}
@@ -410,65 +410,65 @@ as necessary to represent the exponent.
Py_LOCAL_INLINE(void)
ensure_minimum_exponent_length(char* buffer, size_t buf_size)
{
- char *p = strpbrk(buffer, "eE");
- if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
- char *start = p + 2;
- int exponent_digit_cnt = 0;
- int leading_zero_cnt = 0;
- int in_leading_zeros = 1;
- int significant_digit_cnt;
-
- /* Skip over the exponent and the sign. */
- p += 2;
-
- /* Find the end of the exponent, keeping track of leading
- zeros. */
- while (*p && Py_ISDIGIT(*p)) {
- if (in_leading_zeros && *p == '0')
- ++leading_zero_cnt;
- if (*p != '0')
- in_leading_zeros = 0;
- ++p;
- ++exponent_digit_cnt;
- }
-
- significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
- if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
- /* If there are 2 exactly digits, we're done,
- regardless of what they contain */
- }
- else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
- int extra_zeros_cnt;
-
- /* There are more than 2 digits in the exponent. See
- if we can delete some of the leading zeros */
- if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
- significant_digit_cnt = MIN_EXPONENT_DIGITS;
- extra_zeros_cnt = exponent_digit_cnt -
- significant_digit_cnt;
-
- /* Delete extra_zeros_cnt worth of characters from the
- front of the exponent */
- assert(extra_zeros_cnt >= 0);
-
- /* Add one to significant_digit_cnt to copy the
- trailing 0 byte, thus setting the length */
- memmove(start,
- start + extra_zeros_cnt,
- significant_digit_cnt + 1);
- }
- else {
- /* If there are fewer than 2 digits, add zeros
- until there are 2, if there's enough room */
- int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
- if (start + zeros + exponent_digit_cnt + 1
- < buffer + buf_size) {
- memmove(start + zeros, start,
- exponent_digit_cnt + 1);
- memset(start, '0', zeros);
- }
- }
- }
+ char *p = strpbrk(buffer, "eE");
+ if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
+ char *start = p + 2;
+ int exponent_digit_cnt = 0;
+ int leading_zero_cnt = 0;
+ int in_leading_zeros = 1;
+ int significant_digit_cnt;
+
+ /* Skip over the exponent and the sign. */
+ p += 2;
+
+ /* Find the end of the exponent, keeping track of leading
+ zeros. */
+ while (*p && Py_ISDIGIT(*p)) {
+ if (in_leading_zeros && *p == '0')
+ ++leading_zero_cnt;
+ if (*p != '0')
+ in_leading_zeros = 0;
+ ++p;
+ ++exponent_digit_cnt;
+ }
+
+ significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
+ if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
+ /* If there are 2 exactly digits, we're done,
+ regardless of what they contain */
+ }
+ else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
+ int extra_zeros_cnt;
+
+ /* There are more than 2 digits in the exponent. See
+ if we can delete some of the leading zeros */
+ if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
+ significant_digit_cnt = MIN_EXPONENT_DIGITS;
+ extra_zeros_cnt = exponent_digit_cnt -
+ significant_digit_cnt;
+
+ /* Delete extra_zeros_cnt worth of characters from the
+ front of the exponent */
+ assert(extra_zeros_cnt >= 0);
+
+ /* Add one to significant_digit_cnt to copy the
+ trailing 0 byte, thus setting the length */
+ memmove(start,
+ start + extra_zeros_cnt,
+ significant_digit_cnt + 1);
+ }
+ else {
+ /* If there are fewer than 2 digits, add zeros
+ until there are 2, if there's enough room */
+ int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
+ if (start + zeros + exponent_digit_cnt + 1
+ < buffer + buf_size) {
+ memmove(start + zeros, start,
+ exponent_digit_cnt + 1);
+ memset(start, '0', zeros);
+ }
+ }
+ }
}
/* Remove trailing zeros after the decimal point from a numeric string; also
@@ -478,40 +478,40 @@ ensure_minimum_exponent_length(char* buffer, size_t buf_size)
Py_LOCAL_INLINE(void)
remove_trailing_zeros(char *buffer)
{
- char *old_fraction_end, *new_fraction_end, *end, *p;
-
- p = buffer;
- if (*p == '-' || *p == '+')
- /* Skip leading sign, if present */
- ++p;
- while (Py_ISDIGIT(*p))
- ++p;
-
- /* if there's no decimal point there's nothing to do */
- if (*p++ != '.')
- return;
-
- /* scan any digits after the point */
- while (Py_ISDIGIT(*p))
- ++p;
- old_fraction_end = p;
-
- /* scan up to ending '\0' */
- while (*p != '\0')
- p++;
- /* +1 to make sure that we move the null byte as well */
- end = p+1;
-
- /* scan back from fraction_end, looking for removable zeros */
- p = old_fraction_end;
- while (*(p-1) == '0')
- --p;
- /* and remove point if we've got that far */
- if (*(p-1) == '.')
- --p;
- new_fraction_end = p;
-
- memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
+ char *old_fraction_end, *new_fraction_end, *end, *p;
+
+ p = buffer;
+ if (*p == '-' || *p == '+')
+ /* Skip leading sign, if present */
+ ++p;
+ while (Py_ISDIGIT(*p))
+ ++p;
+
+ /* if there's no decimal point there's nothing to do */
+ if (*p++ != '.')
+ return;
+
+ /* scan any digits after the point */
+ while (Py_ISDIGIT(*p))
+ ++p;
+ old_fraction_end = p;
+
+ /* scan up to ending '\0' */
+ while (*p != '\0')
+ p++;
+ /* +1 to make sure that we move the null byte as well */
+ end = p+1;
+
+ /* scan back from fraction_end, looking for removable zeros */
+ p = old_fraction_end;
+ while (*(p-1) == '0')
+ --p;
+ /* and remove point if we've got that far */
+ if (*(p-1) == '.')
+ --p;
+ new_fraction_end = p;
+
+ memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
}
/* Ensure that buffer has a decimal point in it. The decimal point will not
@@ -524,91 +524,91 @@ remove_trailing_zeros(char *buffer)
Py_LOCAL_INLINE(char *)
ensure_decimal_point(char* buffer, size_t buf_size, int precision)
{
- int digit_count, insert_count = 0, convert_to_exp = 0;
- char *chars_to_insert, *digits_start;
-
- /* search for the first non-digit character */
- char *p = buffer;
- if (*p == '-' || *p == '+')
- /* Skip leading sign, if present. I think this could only
- ever be '-', but it can't hurt to check for both. */
- ++p;
- digits_start = p;
- while (*p && Py_ISDIGIT(*p))
- ++p;
- digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);
-
- if (*p == '.') {
- if (Py_ISDIGIT(*(p+1))) {
- /* Nothing to do, we already have a decimal
- point and a digit after it */
- }
- else {
- /* We have a decimal point, but no following
- digit. Insert a zero after the decimal. */
- /* can't ever get here via PyOS_double_to_string */
- assert(precision == -1);
- ++p;
- chars_to_insert = "0";
- insert_count = 1;
- }
- }
- else if (!(*p == 'e' || *p == 'E')) {
- /* Don't add ".0" if we have an exponent. */
- if (digit_count == precision) {
- /* issue 5864: don't add a trailing .0 in the case
- where the '%g'-formatted result already has as many
- significant digits as were requested. Switch to
- exponential notation instead. */
- convert_to_exp = 1;
- /* no exponent, no point, and we shouldn't land here
- for infs and nans, so we must be at the end of the
- string. */
- assert(*p == '\0');
- }
- else {
- assert(precision == -1 || digit_count < precision);
- chars_to_insert = ".0";
- insert_count = 2;
- }
- }
- if (insert_count) {
- size_t buf_len = strlen(buffer);
- if (buf_len + insert_count + 1 >= buf_size) {
- /* If there is not enough room in the buffer
- for the additional text, just skip it. It's
- not worth generating an error over. */
- }
- else {
- memmove(p + insert_count, p,
- buffer + strlen(buffer) - p + 1);
- memcpy(p, chars_to_insert, insert_count);
- }
- }
- if (convert_to_exp) {
- int written;
- size_t buf_avail;
- p = digits_start;
- /* insert decimal point */
- assert(digit_count >= 1);
- memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */
- p[1] = '.';
- p += digit_count+1;
- assert(p <= buf_size+buffer);
- buf_avail = buf_size+buffer-p;
- if (buf_avail == 0)
- return NULL;
- /* Add exponent. It's okay to use lower case 'e': we only
- arrive here as a result of using the empty format code or
- repr/str builtins and those never want an upper case 'E' */
- written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1);
- if (!(0 <= written &&
- written < Py_SAFE_DOWNCAST(buf_avail, size_t, int)))
- /* output truncated, or something else bad happened */
- return NULL;
- remove_trailing_zeros(buffer);
- }
- return buffer;
+ int digit_count, insert_count = 0, convert_to_exp = 0;
+ char *chars_to_insert, *digits_start;
+
+ /* search for the first non-digit character */
+ char *p = buffer;
+ if (*p == '-' || *p == '+')
+ /* Skip leading sign, if present. I think this could only
+ ever be '-', but it can't hurt to check for both. */
+ ++p;
+ digits_start = p;
+ while (*p && Py_ISDIGIT(*p))
+ ++p;
+ digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);
+
+ if (*p == '.') {
+ if (Py_ISDIGIT(*(p+1))) {
+ /* Nothing to do, we already have a decimal
+ point and a digit after it */
+ }
+ else {
+ /* We have a decimal point, but no following
+ digit. Insert a zero after the decimal. */
+ /* can't ever get here via PyOS_double_to_string */
+ assert(precision == -1);
+ ++p;
+ chars_to_insert = "0";
+ insert_count = 1;
+ }
+ }
+ else if (!(*p == 'e' || *p == 'E')) {
+ /* Don't add ".0" if we have an exponent. */
+ if (digit_count == precision) {
+ /* issue 5864: don't add a trailing .0 in the case
+ where the '%g'-formatted result already has as many
+ significant digits as were requested. Switch to
+ exponential notation instead. */
+ convert_to_exp = 1;
+ /* no exponent, no point, and we shouldn't land here
+ for infs and nans, so we must be at the end of the
+ string. */
+ assert(*p == '\0');
+ }
+ else {
+ assert(precision == -1 || digit_count < precision);
+ chars_to_insert = ".0";
+ insert_count = 2;
+ }
+ }
+ if (insert_count) {
+ size_t buf_len = strlen(buffer);
+ if (buf_len + insert_count + 1 >= buf_size) {
+ /* If there is not enough room in the buffer
+ for the additional text, just skip it. It's
+ not worth generating an error over. */
+ }
+ else {
+ memmove(p + insert_count, p,
+ buffer + strlen(buffer) - p + 1);
+ memcpy(p, chars_to_insert, insert_count);
+ }
+ }
+ if (convert_to_exp) {
+ int written;
+ size_t buf_avail;
+ p = digits_start;
+ /* insert decimal point */
+ assert(digit_count >= 1);
+ memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */
+ p[1] = '.';
+ p += digit_count+1;
+ assert(p <= buf_size+buffer);
+ buf_avail = buf_size+buffer-p;
+ if (buf_avail == 0)
+ return NULL;
+ /* Add exponent. It's okay to use lower case 'e': we only
+ arrive here as a result of using the empty format code or
+ repr/str builtins and those never want an upper case 'E' */
+ written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1);
+ if (!(0 <= written &&
+ written < Py_SAFE_DOWNCAST(buf_avail, size_t, int)))
+ /* output truncated, or something else bad happened */
+ return NULL;
+ remove_trailing_zeros(buffer);
+ }
+ return buffer;
}
/* see FORMATBUFLEN in unicodeobject.c */
@@ -619,14 +619,14 @@ ensure_decimal_point(char* buffer, size_t buf_size, int precision)
* @buffer: A buffer to place the resulting string in
* @buf_size: The length of the buffer.
* @format: The printf()-style format to use for the
- * code to use for converting.
+ * code to use for converting.
* @d: The #gdouble to convert
*
* Converts a #gdouble to a string, using the '.' as
* decimal point. To format the number you pass in
* a printf()-style format string. Allowed conversion
* specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.
- *
+ *
* 'Z' is the same as 'g', except it always has a decimal and
* at least one digit after the decimal.
*
@@ -634,97 +634,97 @@ ensure_decimal_point(char* buffer, size_t buf_size, int precision)
* On failure returns NULL but does not set any Python exception.
**/
char *
-_PyOS_ascii_formatd(char *buffer,
- size_t buf_size,
- const char *format,
- double d,
- int precision)
+_PyOS_ascii_formatd(char *buffer,
+ size_t buf_size,
+ const char *format,
+ double d,
+ int precision)
{
- char format_char;
- size_t format_len = strlen(format);
-
- /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
- also with at least one character past the decimal. */
- char tmp_format[FLOAT_FORMATBUFLEN];
-
- /* The last character in the format string must be the format char */
- format_char = format[format_len - 1];
-
- if (format[0] != '%')
- return NULL;
-
- /* I'm not sure why this test is here. It's ensuring that the format
- string after the first character doesn't have a single quote, a
- lowercase l, or a percent. This is the reverse of the commented-out
- test about 10 lines ago. */
- if (strpbrk(format + 1, "'l%"))
- return NULL;
-
- /* Also curious about this function is that it accepts format strings
- like "%xg", which are invalid for floats. In general, the
- interface to this function is not very good, but changing it is
- difficult because it's a public API. */
-
- if (!(format_char == 'e' || format_char == 'E' ||
- format_char == 'f' || format_char == 'F' ||
- format_char == 'g' || format_char == 'G' ||
- format_char == 'Z'))
- return NULL;
-
- /* Map 'Z' format_char to 'g', by copying the format string and
- replacing the final char with a 'g' */
- if (format_char == 'Z') {
- if (format_len + 1 >= sizeof(tmp_format)) {
- /* The format won't fit in our copy. Error out. In
- practice, this will never happen and will be
- detected by returning NULL */
- return NULL;
- }
- strcpy(tmp_format, format);
- tmp_format[format_len - 1] = 'g';
- format = tmp_format;
- }
-
-
- /* Have PyOS_snprintf do the hard work */
- PyOS_snprintf(buffer, buf_size, format, d);
-
- /* Do various fixups on the return string */
-
- /* Get the current locale, and find the decimal point string.
- Convert that string back to a dot. */
- change_decimal_from_locale_to_dot(buffer);
-
- /* If an exponent exists, ensure that the exponent is at least
- MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
- for the extra zeros. Also, if there are more than
- MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
- back to MIN_EXPONENT_DIGITS */
- ensure_minimum_exponent_length(buffer, buf_size);
-
- /* If format_char is 'Z', make sure we have at least one character
- after the decimal point (and make sure we have a decimal point);
- also switch to exponential notation in some edge cases where the
- extra character would produce more significant digits that we
- really want. */
- if (format_char == 'Z')
- buffer = ensure_decimal_point(buffer, buf_size, precision);
-
- return buffer;
+ char format_char;
+ size_t format_len = strlen(format);
+
+ /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
+ also with at least one character past the decimal. */
+ char tmp_format[FLOAT_FORMATBUFLEN];
+
+ /* The last character in the format string must be the format char */
+ format_char = format[format_len - 1];
+
+ if (format[0] != '%')
+ return NULL;
+
+ /* I'm not sure why this test is here. It's ensuring that the format
+ string after the first character doesn't have a single quote, a
+ lowercase l, or a percent. This is the reverse of the commented-out
+ test about 10 lines ago. */
+ if (strpbrk(format + 1, "'l%"))
+ return NULL;
+
+ /* Also curious about this function is that it accepts format strings
+ like "%xg", which are invalid for floats. In general, the
+ interface to this function is not very good, but changing it is
+ difficult because it's a public API. */
+
+ if (!(format_char == 'e' || format_char == 'E' ||
+ format_char == 'f' || format_char == 'F' ||
+ format_char == 'g' || format_char == 'G' ||
+ format_char == 'Z'))
+ return NULL;
+
+ /* Map 'Z' format_char to 'g', by copying the format string and
+ replacing the final char with a 'g' */
+ if (format_char == 'Z') {
+ if (format_len + 1 >= sizeof(tmp_format)) {
+ /* The format won't fit in our copy. Error out. In
+ practice, this will never happen and will be
+ detected by returning NULL */
+ return NULL;
+ }
+ strcpy(tmp_format, format);
+ tmp_format[format_len - 1] = 'g';
+ format = tmp_format;
+ }
+
+
+ /* Have PyOS_snprintf do the hard work */
+ PyOS_snprintf(buffer, buf_size, format, d);
+
+ /* Do various fixups on the return string */
+
+ /* Get the current locale, and find the decimal point string.
+ Convert that string back to a dot. */
+ change_decimal_from_locale_to_dot(buffer);
+
+ /* If an exponent exists, ensure that the exponent is at least
+ MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
+ for the extra zeros. Also, if there are more than
+ MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
+ back to MIN_EXPONENT_DIGITS */
+ ensure_minimum_exponent_length(buffer, buf_size);
+
+ /* If format_char is 'Z', make sure we have at least one character
+ after the decimal point (and make sure we have a decimal point);
+ also switch to exponential notation in some edge cases where the
+ extra character would produce more significant digits that we
+ really want. */
+ if (format_char == 'Z')
+ buffer = ensure_decimal_point(buffer, buf_size, precision);
+
+ return buffer;
}
char *
-PyOS_ascii_formatd(char *buffer,
- size_t buf_size,
- const char *format,
- double d)
+PyOS_ascii_formatd(char *buffer,
+ size_t buf_size,
+ const char *format,
+ double d)
{
- if (PyErr_WarnEx(PyExc_DeprecationWarning,
- "PyOS_ascii_formatd is deprecated, "
- "use PyOS_double_to_string instead", 1) < 0)
- return NULL;
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "PyOS_ascii_formatd is deprecated, "
+ "use PyOS_double_to_string instead", 1) < 0)
+ return NULL;
- return _PyOS_ascii_formatd(buffer, buf_size, format, d, -1);
+ return _PyOS_ascii_formatd(buffer, buf_size, format, d, -1);
}
#ifdef PY_NO_SHORT_FLOAT_REPR
@@ -737,146 +737,146 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val,
int flags,
int *type)
{
- char format[32];
- Py_ssize_t bufsize;
- char *buf;
- int t, exp;
- int upper = 0;
-
- /* Validate format_code, and map upper and lower case */
- switch (format_code) {
- case 'e': /* exponent */
- case 'f': /* fixed */
- case 'g': /* general */
- break;
- case 'E':
- upper = 1;
- format_code = 'e';
- break;
- case 'F':
- upper = 1;
- format_code = 'f';
- break;
- case 'G':
- upper = 1;
- format_code = 'g';
- break;
- case 'r': /* repr format */
- /* Supplied precision is unused, must be 0. */
- if (precision != 0) {
- PyErr_BadInternalCall();
- return NULL;
- }
- /* The repr() precision (17 significant decimal digits) is the
- minimal number that is guaranteed to have enough precision
- so that if the number is read back in the exact same binary
- value is recreated. This is true for IEEE floating point
- by design, and also happens to work for all other modern
- hardware. */
- precision = 17;
- format_code = 'g';
- break;
- default:
- PyErr_BadInternalCall();
- return NULL;
- }
-
- /* Here's a quick-and-dirty calculation to figure out how big a buffer
- we need. In general, for a finite float we need:
-
- 1 byte for each digit of the decimal significand, and
-
- 1 for a possible sign
- 1 for a possible decimal point
- 2 for a possible [eE][+-]
- 1 for each digit of the exponent; if we allow 19 digits
- total then we're safe up to exponents of 2**63.
- 1 for the trailing nul byte
-
- This gives a total of 24 + the number of digits in the significand,
- and the number of digits in the significand is:
-
- for 'g' format: at most precision, except possibly
- when precision == 0, when it's 1.
- for 'e' format: precision+1
- for 'f' format: precision digits after the point, at least 1
- before. To figure out how many digits appear before the point
- we have to examine the size of the number. If fabs(val) < 1.0
- then there will be only one digit before the point. If
- fabs(val) >= 1.0, then there are at most
-
- 1+floor(log10(ceiling(fabs(val))))
-
- digits before the point (where the 'ceiling' allows for the
- possibility that the rounding rounds the integer part of val
- up). A safe upper bound for the above quantity is
- 1+floor(exp/3), where exp is the unique integer such that 0.5
- <= fabs(val)/2**exp < 1.0. This exp can be obtained from
- frexp.
-
- So we allow room for precision+1 digits for all formats, plus an
- extra floor(exp/3) digits for 'f' format.
-
- */
-
- if (Py_IS_NAN(val) || Py_IS_INFINITY(val))
- /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */
- bufsize = 5;
- else {
- bufsize = 25 + precision;
- if (format_code == 'f' && fabs(val) >= 1.0) {
- frexp(val, &exp);
- bufsize += exp/3;
- }
- }
-
- buf = PyMem_Malloc(bufsize);
- if (buf == NULL) {
- PyErr_NoMemory();
- return NULL;
- }
-
- /* Handle nan and inf. */
- if (Py_IS_NAN(val)) {
- strcpy(buf, "nan");
- t = Py_DTST_NAN;
- } else if (Py_IS_INFINITY(val)) {
- if (copysign(1., val) == 1.)
- strcpy(buf, "inf");
- else
- strcpy(buf, "-inf");
- t = Py_DTST_INFINITE;
- } else {
- t = Py_DTST_FINITE;
- if (flags & Py_DTSF_ADD_DOT_0)
- format_code = 'Z';
-
- PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
- (flags & Py_DTSF_ALT ? "#" : ""), precision,
- format_code);
- _PyOS_ascii_formatd(buf, bufsize, format, val, precision);
- }
-
- /* Add sign when requested. It's convenient (esp. when formatting
- complex numbers) to include a sign even for inf and nan. */
- if (flags & Py_DTSF_SIGN && buf[0] != '-') {
- size_t len = strlen(buf);
- /* the bufsize calculations above should ensure that we've got
- space to add a sign */
- assert((size_t)bufsize >= len+2);
- memmove(buf+1, buf, len+1);
- buf[0] = '+';
- }
- if (upper) {
- /* Convert to upper case. */
- char *p1;
- for (p1 = buf; *p1; p1++)
- *p1 = Py_TOUPPER(*p1);
- }
-
- if (type)
- *type = t;
- return buf;
+ char format[32];
+ Py_ssize_t bufsize;
+ char *buf;
+ int t, exp;
+ int upper = 0;
+
+ /* Validate format_code, and map upper and lower case */
+ switch (format_code) {
+ case 'e': /* exponent */
+ case 'f': /* fixed */
+ case 'g': /* general */
+ break;
+ case 'E':
+ upper = 1;
+ format_code = 'e';
+ break;
+ case 'F':
+ upper = 1;
+ format_code = 'f';
+ break;
+ case 'G':
+ upper = 1;
+ format_code = 'g';
+ break;
+ case 'r': /* repr format */
+ /* Supplied precision is unused, must be 0. */
+ if (precision != 0) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ /* The repr() precision (17 significant decimal digits) is the
+ minimal number that is guaranteed to have enough precision
+ so that if the number is read back in the exact same binary
+ value is recreated. This is true for IEEE floating point
+ by design, and also happens to work for all other modern
+ hardware. */
+ precision = 17;
+ format_code = 'g';
+ break;
+ default:
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ /* Here's a quick-and-dirty calculation to figure out how big a buffer
+ we need. In general, for a finite float we need:
+
+ 1 byte for each digit of the decimal significand, and
+
+ 1 for a possible sign
+ 1 for a possible decimal point
+ 2 for a possible [eE][+-]
+ 1 for each digit of the exponent; if we allow 19 digits
+ total then we're safe up to exponents of 2**63.
+ 1 for the trailing nul byte
+
+ This gives a total of 24 + the number of digits in the significand,
+ and the number of digits in the significand is:
+
+ for 'g' format: at most precision, except possibly
+ when precision == 0, when it's 1.
+ for 'e' format: precision+1
+ for 'f' format: precision digits after the point, at least 1
+ before. To figure out how many digits appear before the point
+ we have to examine the size of the number. If fabs(val) < 1.0
+ then there will be only one digit before the point. If
+ fabs(val) >= 1.0, then there are at most
+
+ 1+floor(log10(ceiling(fabs(val))))
+
+ digits before the point (where the 'ceiling' allows for the
+ possibility that the rounding rounds the integer part of val
+ up). A safe upper bound for the above quantity is
+ 1+floor(exp/3), where exp is the unique integer such that 0.5
+ <= fabs(val)/2**exp < 1.0. This exp can be obtained from
+ frexp.
+
+ So we allow room for precision+1 digits for all formats, plus an
+ extra floor(exp/3) digits for 'f' format.
+
+ */
+
+ if (Py_IS_NAN(val) || Py_IS_INFINITY(val))
+ /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */
+ bufsize = 5;
+ else {
+ bufsize = 25 + precision;
+ if (format_code == 'f' && fabs(val) >= 1.0) {
+ frexp(val, &exp);
+ bufsize += exp/3;
+ }
+ }
+
+ buf = PyMem_Malloc(bufsize);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ /* Handle nan and inf. */
+ if (Py_IS_NAN(val)) {
+ strcpy(buf, "nan");
+ t = Py_DTST_NAN;
+ } else if (Py_IS_INFINITY(val)) {
+ if (copysign(1., val) == 1.)
+ strcpy(buf, "inf");
+ else
+ strcpy(buf, "-inf");
+ t = Py_DTST_INFINITE;
+ } else {
+ t = Py_DTST_FINITE;
+ if (flags & Py_DTSF_ADD_DOT_0)
+ format_code = 'Z';
+
+ PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
+ (flags & Py_DTSF_ALT ? "#" : ""), precision,
+ format_code);
+ _PyOS_ascii_formatd(buf, bufsize, format, val, precision);
+ }
+
+ /* Add sign when requested. It's convenient (esp. when formatting
+ complex numbers) to include a sign even for inf and nan. */
+ if (flags & Py_DTSF_SIGN && buf[0] != '-') {
+ size_t len = strlen(buf);
+ /* the bufsize calculations above should ensure that we've got
+ space to add a sign */
+ assert((size_t)bufsize >= len+2);
+ memmove(buf+1, buf, len+1);
+ buf[0] = '+';
+ }
+ if (upper) {
+ /* Convert to upper case. */
+ char *p1;
+ for (p1 = buf; *p1; p1++)
+ *p1 = Py_TOUPPER(*p1);
+ }
+
+ if (type)
+ *type = t;
+ return buf;
}
#else
@@ -891,14 +891,14 @@ PyAPI_FUNC(char *) PyOS_double_to_string(double val,
/* The lengths of these are known to the code below, so don't change them */
static char *lc_float_strings[] = {
- "inf",
- "nan",
- "e",
+ "inf",
+ "nan",
+ "e",
};
static char *uc_float_strings[] = {
- "INF",
- "NAN",
- "E",
+ "INF",
+ "NAN",
+ "E",
};
@@ -922,9 +922,9 @@ static char *uc_float_strings[] = {
be nonzero.
type, if non-NULL, will be set to one of these constants to identify
the type of the 'd' argument:
- Py_DTST_FINITE
- Py_DTST_INFINITE
- Py_DTST_NAN
+ Py_DTST_FINITE
+ Py_DTST_INFINITE
+ Py_DTST_NAN
Returns a PyMem_Malloc'd block of memory containing the resulting string,
or NULL on error. If NULL is returned, the Python error has been set.
@@ -932,315 +932,315 @@ static char *uc_float_strings[] = {
static char *
format_float_short(double d, char format_code,
- int mode, Py_ssize_t precision,
- int always_add_sign, int add_dot_0_if_integer,
- int use_alt_formatting, char **float_strings, int *type)
+ int mode, Py_ssize_t precision,
+ int always_add_sign, int add_dot_0_if_integer,
+ int use_alt_formatting, char **float_strings, int *type)
{
- char *buf = NULL;
- char *p = NULL;
- Py_ssize_t bufsize = 0;
- char *digits, *digits_end;
- int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;
- Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;
- _Py_SET_53BIT_PRECISION_HEADER;
-
- /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).
- Must be matched by a call to _Py_dg_freedtoa. */
- _Py_SET_53BIT_PRECISION_START;
- digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,
- &digits_end);
- _Py_SET_53BIT_PRECISION_END;
-
- decpt = (Py_ssize_t)decpt_as_int;
- if (digits == NULL) {
- /* The only failure mode is no memory. */
- PyErr_NoMemory();
- goto exit;
- }
- assert(digits_end != NULL && digits_end >= digits);
- digits_len = digits_end - digits;
-
- if (digits_len && !Py_ISDIGIT(digits[0])) {
- /* Infinities and nans here; adapt Gay's output,
- so convert Infinity to inf and NaN to nan, and
- ignore sign of nan. Then return. */
-
- /* ignore the actual sign of a nan */
- if (digits[0] == 'n' || digits[0] == 'N')
- sign = 0;
-
- /* We only need 5 bytes to hold the result "+inf\0" . */
- bufsize = 5; /* Used later in an assert. */
- buf = (char *)PyMem_Malloc(bufsize);
- if (buf == NULL) {
- PyErr_NoMemory();
- goto exit;
- }
- p = buf;
-
- if (sign == 1) {
- *p++ = '-';
- }
- else if (always_add_sign) {
- *p++ = '+';
- }
- if (digits[0] == 'i' || digits[0] == 'I') {
- strncpy(p, float_strings[OFS_INF], 3);
- p += 3;
-
- if (type)
- *type = Py_DTST_INFINITE;
- }
- else if (digits[0] == 'n' || digits[0] == 'N') {
- strncpy(p, float_strings[OFS_NAN], 3);
- p += 3;
-
- if (type)
- *type = Py_DTST_NAN;
- }
- else {
- /* shouldn't get here: Gay's code should always return
- something starting with a digit, an 'I', or 'N' */
- strncpy(p, "ERR", 3);
- p += 3;
- assert(0);
- }
- goto exit;
- }
-
- /* The result must be finite (not inf or nan). */
- if (type)
- *type = Py_DTST_FINITE;
-
-
- /* We got digits back, format them. We may need to pad 'digits'
- either on the left or right (or both) with extra zeros, so in
- general the resulting string has the form
-
- [<sign>]<zeros><digits><zeros>[<exponent>]
-
- where either of the <zeros> pieces could be empty, and there's a
- decimal point that could appear either in <digits> or in the
- leading or trailing <zeros>.
-
- Imagine an infinite 'virtual' string vdigits, consisting of the
- string 'digits' (starting at index 0) padded on both the left and
- right with infinite strings of zeros. We want to output a slice
-
- vdigits[vdigits_start : vdigits_end]
-
- of this virtual string. Thus if vdigits_start < 0 then we'll end
- up producing some leading zeros; if vdigits_end > digits_len there
- will be trailing zeros in the output. The next section of code
- determines whether to use an exponent or not, figures out the
- position 'decpt' of the decimal point, and computes 'vdigits_start'
- and 'vdigits_end'. */
- vdigits_end = digits_len;
- switch (format_code) {
- case 'e':
- use_exp = 1;
- vdigits_end = precision;
- break;
- case 'f':
- vdigits_end = decpt + precision;
- break;
- case 'g':
- if (decpt <= -4 || decpt >
- (add_dot_0_if_integer ? precision-1 : precision))
- use_exp = 1;
- if (use_alt_formatting)
- vdigits_end = precision;
- break;
- case 'r':
- /* convert to exponential format at 1e16. We used to convert
- at 1e17, but that gives odd-looking results for some values
- when a 16-digit 'shortest' repr is padded with bogus zeros.
- For example, repr(2e16+8) would give 20000000000000010.0;
- the true value is 20000000000000008.0. */
- if (decpt <= -4 || decpt > 16)
- use_exp = 1;
- break;
- default:
- PyErr_BadInternalCall();
- goto exit;
- }
-
- /* if using an exponent, reset decimal point position to 1 and adjust
- exponent accordingly.*/
- if (use_exp) {
- exp = decpt - 1;
- decpt = 1;
- }
- /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <
- decpt < vdigits_end if add_dot_0_if_integer and no exponent */
- vdigits_start = decpt <= 0 ? decpt-1 : 0;
- if (!use_exp && add_dot_0_if_integer)
- vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;
- else
- vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;
-
- /* double check inequalities */
- assert(vdigits_start <= 0 &&
- 0 <= digits_len &&
- digits_len <= vdigits_end);
- /* decimal point should be in (vdigits_start, vdigits_end] */
- assert(vdigits_start < decpt && decpt <= vdigits_end);
-
- /* Compute an upper bound how much memory we need. This might be a few
- chars too long, but no big deal. */
- bufsize =
- /* sign, decimal point and trailing 0 byte */
- 3 +
-
- /* total digit count (including zero padding on both sides) */
- (vdigits_end - vdigits_start) +
-
- /* exponent "e+100", max 3 numerical digits */
- (use_exp ? 5 : 0);
-
- /* Now allocate the memory and initialize p to point to the start of
- it. */
- buf = (char *)PyMem_Malloc(bufsize);
- if (buf == NULL) {
- PyErr_NoMemory();
- goto exit;
- }
- p = buf;
-
- /* Add a negative sign if negative, and a plus sign if non-negative
- and always_add_sign is true. */
- if (sign == 1)
- *p++ = '-';
- else if (always_add_sign)
- *p++ = '+';
-
- /* note that exactly one of the three 'if' conditions is true,
- so we include exactly one decimal point */
- /* Zero padding on left of digit string */
- if (decpt <= 0) {
- memset(p, '0', decpt-vdigits_start);
- p += decpt - vdigits_start;
- *p++ = '.';
- memset(p, '0', 0-decpt);
- p += 0-decpt;
- }
- else {
- memset(p, '0', 0-vdigits_start);
- p += 0 - vdigits_start;
- }
-
- /* Digits, with included decimal point */
- if (0 < decpt && decpt <= digits_len) {
- strncpy(p, digits, decpt-0);
- p += decpt-0;
- *p++ = '.';
- strncpy(p, digits+decpt, digits_len-decpt);
- p += digits_len-decpt;
- }
- else {
- strncpy(p, digits, digits_len);
- p += digits_len;
- }
-
- /* And zeros on the right */
- if (digits_len < decpt) {
- memset(p, '0', decpt-digits_len);
- p += decpt-digits_len;
- *p++ = '.';
- memset(p, '0', vdigits_end-decpt);
- p += vdigits_end-decpt;
- }
- else {
- memset(p, '0', vdigits_end-digits_len);
- p += vdigits_end-digits_len;
- }
-
- /* Delete a trailing decimal pt unless using alternative formatting. */
- if (p[-1] == '.' && !use_alt_formatting)
- p--;
-
- /* Now that we've done zero padding, add an exponent if needed. */
- if (use_exp) {
- *p++ = float_strings[OFS_E][0];
- exp_len = sprintf(p, "%+.02d", exp);
- p += exp_len;
- }
+ char *buf = NULL;
+ char *p = NULL;
+ Py_ssize_t bufsize = 0;
+ char *digits, *digits_end;
+ int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;
+ Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;
+ _Py_SET_53BIT_PRECISION_HEADER;
+
+ /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).
+ Must be matched by a call to _Py_dg_freedtoa. */
+ _Py_SET_53BIT_PRECISION_START;
+ digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,
+ &digits_end);
+ _Py_SET_53BIT_PRECISION_END;
+
+ decpt = (Py_ssize_t)decpt_as_int;
+ if (digits == NULL) {
+ /* The only failure mode is no memory. */
+ PyErr_NoMemory();
+ goto exit;
+ }
+ assert(digits_end != NULL && digits_end >= digits);
+ digits_len = digits_end - digits;
+
+ if (digits_len && !Py_ISDIGIT(digits[0])) {
+ /* Infinities and nans here; adapt Gay's output,
+ so convert Infinity to inf and NaN to nan, and
+ ignore sign of nan. Then return. */
+
+ /* ignore the actual sign of a nan */
+ if (digits[0] == 'n' || digits[0] == 'N')
+ sign = 0;
+
+ /* We only need 5 bytes to hold the result "+inf\0" . */
+ bufsize = 5; /* Used later in an assert. */
+ buf = (char *)PyMem_Malloc(bufsize);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ goto exit;
+ }
+ p = buf;
+
+ if (sign == 1) {
+ *p++ = '-';
+ }
+ else if (always_add_sign) {
+ *p++ = '+';
+ }
+ if (digits[0] == 'i' || digits[0] == 'I') {
+ strncpy(p, float_strings[OFS_INF], 3);
+ p += 3;
+
+ if (type)
+ *type = Py_DTST_INFINITE;
+ }
+ else if (digits[0] == 'n' || digits[0] == 'N') {
+ strncpy(p, float_strings[OFS_NAN], 3);
+ p += 3;
+
+ if (type)
+ *type = Py_DTST_NAN;
+ }
+ else {
+ /* shouldn't get here: Gay's code should always return
+ something starting with a digit, an 'I', or 'N' */
+ strncpy(p, "ERR", 3);
+ p += 3;
+ assert(0);
+ }
+ goto exit;
+ }
+
+ /* The result must be finite (not inf or nan). */
+ if (type)
+ *type = Py_DTST_FINITE;
+
+
+ /* We got digits back, format them. We may need to pad 'digits'
+ either on the left or right (or both) with extra zeros, so in
+ general the resulting string has the form
+
+ [<sign>]<zeros><digits><zeros>[<exponent>]
+
+ where either of the <zeros> pieces could be empty, and there's a
+ decimal point that could appear either in <digits> or in the
+ leading or trailing <zeros>.
+
+ Imagine an infinite 'virtual' string vdigits, consisting of the
+ string 'digits' (starting at index 0) padded on both the left and
+ right with infinite strings of zeros. We want to output a slice
+
+ vdigits[vdigits_start : vdigits_end]
+
+ of this virtual string. Thus if vdigits_start < 0 then we'll end
+ up producing some leading zeros; if vdigits_end > digits_len there
+ will be trailing zeros in the output. The next section of code
+ determines whether to use an exponent or not, figures out the
+ position 'decpt' of the decimal point, and computes 'vdigits_start'
+ and 'vdigits_end'. */
+ vdigits_end = digits_len;
+ switch (format_code) {
+ case 'e':
+ use_exp = 1;
+ vdigits_end = precision;
+ break;
+ case 'f':
+ vdigits_end = decpt + precision;
+ break;
+ case 'g':
+ if (decpt <= -4 || decpt >
+ (add_dot_0_if_integer ? precision-1 : precision))
+ use_exp = 1;
+ if (use_alt_formatting)
+ vdigits_end = precision;
+ break;
+ case 'r':
+ /* convert to exponential format at 1e16. We used to convert
+ at 1e17, but that gives odd-looking results for some values
+ when a 16-digit 'shortest' repr is padded with bogus zeros.
+ For example, repr(2e16+8) would give 20000000000000010.0;
+ the true value is 20000000000000008.0. */
+ if (decpt <= -4 || decpt > 16)
+ use_exp = 1;
+ break;
+ default:
+ PyErr_BadInternalCall();
+ goto exit;
+ }
+
+ /* if using an exponent, reset decimal point position to 1 and adjust
+ exponent accordingly.*/
+ if (use_exp) {
+ exp = decpt - 1;
+ decpt = 1;
+ }
+ /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <
+ decpt < vdigits_end if add_dot_0_if_integer and no exponent */
+ vdigits_start = decpt <= 0 ? decpt-1 : 0;
+ if (!use_exp && add_dot_0_if_integer)
+ vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;
+ else
+ vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;
+
+ /* double check inequalities */
+ assert(vdigits_start <= 0 &&
+ 0 <= digits_len &&
+ digits_len <= vdigits_end);
+ /* decimal point should be in (vdigits_start, vdigits_end] */
+ assert(vdigits_start < decpt && decpt <= vdigits_end);
+
+ /* Compute an upper bound how much memory we need. This might be a few
+ chars too long, but no big deal. */
+ bufsize =
+ /* sign, decimal point and trailing 0 byte */
+ 3 +
+
+ /* total digit count (including zero padding on both sides) */
+ (vdigits_end - vdigits_start) +
+
+ /* exponent "e+100", max 3 numerical digits */
+ (use_exp ? 5 : 0);
+
+ /* Now allocate the memory and initialize p to point to the start of
+ it. */
+ buf = (char *)PyMem_Malloc(bufsize);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ goto exit;
+ }
+ p = buf;
+
+ /* Add a negative sign if negative, and a plus sign if non-negative
+ and always_add_sign is true. */
+ if (sign == 1)
+ *p++ = '-';
+ else if (always_add_sign)
+ *p++ = '+';
+
+ /* note that exactly one of the three 'if' conditions is true,
+ so we include exactly one decimal point */
+ /* Zero padding on left of digit string */
+ if (decpt <= 0) {
+ memset(p, '0', decpt-vdigits_start);
+ p += decpt - vdigits_start;
+ *p++ = '.';
+ memset(p, '0', 0-decpt);
+ p += 0-decpt;
+ }
+ else {
+ memset(p, '0', 0-vdigits_start);
+ p += 0 - vdigits_start;
+ }
+
+ /* Digits, with included decimal point */
+ if (0 < decpt && decpt <= digits_len) {
+ strncpy(p, digits, decpt-0);
+ p += decpt-0;
+ *p++ = '.';
+ strncpy(p, digits+decpt, digits_len-decpt);
+ p += digits_len-decpt;
+ }
+ else {
+ strncpy(p, digits, digits_len);
+ p += digits_len;
+ }
+
+ /* And zeros on the right */
+ if (digits_len < decpt) {
+ memset(p, '0', decpt-digits_len);
+ p += decpt-digits_len;
+ *p++ = '.';
+ memset(p, '0', vdigits_end-decpt);
+ p += vdigits_end-decpt;
+ }
+ else {
+ memset(p, '0', vdigits_end-digits_len);
+ p += vdigits_end-digits_len;
+ }
+
+ /* Delete a trailing decimal pt unless using alternative formatting. */
+ if (p[-1] == '.' && !use_alt_formatting)
+ p--;
+
+ /* Now that we've done zero padding, add an exponent if needed. */
+ if (use_exp) {
+ *p++ = float_strings[OFS_E][0];
+ exp_len = sprintf(p, "%+.02d", exp);
+ p += exp_len;
+ }
exit:
- if (buf) {
- *p = '\0';
- /* It's too late if this fails, as we've already stepped on
- memory that isn't ours. But it's an okay debugging test. */
- assert(p-buf < bufsize);
- }
- if (digits)
- _Py_dg_freedtoa(digits);
-
- return buf;
+ if (buf) {
+ *p = '\0';
+ /* It's too late if this fails, as we've already stepped on
+ memory that isn't ours. But it's an okay debugging test. */
+ assert(p-buf < bufsize);
+ }
+ if (digits)
+ _Py_dg_freedtoa(digits);
+
+ return buf;
}
PyAPI_FUNC(char *) PyOS_double_to_string(double val,
- char format_code,
- int precision,
- int flags,
- int *type)
+ char format_code,
+ int precision,
+ int flags,
+ int *type)
{
- char **float_strings = lc_float_strings;
- int mode;
-
- /* Validate format_code, and map upper and lower case. Compute the
- mode and make any adjustments as needed. */
- switch (format_code) {
- /* exponent */
- case 'E':
- float_strings = uc_float_strings;
- format_code = 'e';
- /* Fall through. */
- case 'e':
- mode = 2;
- precision++;
- break;
-
- /* fixed */
- case 'F':
- float_strings = uc_float_strings;
- format_code = 'f';
- /* Fall through. */
- case 'f':
- mode = 3;
- break;
-
- /* general */
- case 'G':
- float_strings = uc_float_strings;
- format_code = 'g';
- /* Fall through. */
- case 'g':
- mode = 2;
- /* precision 0 makes no sense for 'g' format; interpret as 1 */
- if (precision == 0)
- precision = 1;
- break;
-
- /* repr format */
- case 'r':
- mode = 0;
- /* Supplied precision is unused, must be 0. */
- if (precision != 0) {
- PyErr_BadInternalCall();
- return NULL;
- }
- break;
-
- default:
- PyErr_BadInternalCall();
- return NULL;
- }
-
- return format_float_short(val, format_code, mode, precision,
- flags & Py_DTSF_SIGN,
- flags & Py_DTSF_ADD_DOT_0,
- flags & Py_DTSF_ALT,
- float_strings, type);
+ char **float_strings = lc_float_strings;
+ int mode;
+
+ /* Validate format_code, and map upper and lower case. Compute the
+ mode and make any adjustments as needed. */
+ switch (format_code) {
+ /* exponent */
+ case 'E':
+ float_strings = uc_float_strings;
+ format_code = 'e';
+ /* Fall through. */
+ case 'e':
+ mode = 2;
+ precision++;
+ break;
+
+ /* fixed */
+ case 'F':
+ float_strings = uc_float_strings;
+ format_code = 'f';
+ /* Fall through. */
+ case 'f':
+ mode = 3;
+ break;
+
+ /* general */
+ case 'G':
+ float_strings = uc_float_strings;
+ format_code = 'g';
+ /* Fall through. */
+ case 'g':
+ mode = 2;
+ /* precision 0 makes no sense for 'g' format; interpret as 1 */
+ if (precision == 0)
+ precision = 1;
+ break;
+
+ /* repr format */
+ case 'r':
+ mode = 0;
+ /* Supplied precision is unused, must be 0. */
+ if (precision != 0) {
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+ break;
+
+ default:
+ PyErr_BadInternalCall();
+ return NULL;
+ }
+
+ return format_float_short(val, format_code, mode, precision,
+ flags & Py_DTSF_SIGN,
+ flags & Py_DTSF_ADD_DOT_0,
+ flags & Py_DTSF_ALT,
+ float_strings, type);
}
#endif /* ifdef PY_NO_SHORT_FLOAT_REPR */
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index e84943b..1121e64 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -42,9 +42,9 @@
#ifndef Py_REF_DEBUG
#define PRINT_TOTAL_REFS()
#else /* Py_REF_DEBUG */
-#define PRINT_TOTAL_REFS() fprintf(stderr, \
- "[%" PY_FORMAT_SIZE_T "d refs]\n", \
- _Py_GetRefTotal())
+#define PRINT_TOTAL_REFS() fprintf(stderr, \
+ "[%" PY_FORMAT_SIZE_T "d refs]\n", \
+ _Py_GetRefTotal())
#endif
#ifdef __cplusplus
@@ -61,9 +61,9 @@ static void initsite(void);
static int initstdio(void);
static void flush_io(void);
static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
- PyCompilerFlags *, PyArena *);
+ PyCompilerFlags *, PyArena *);
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
- PyCompilerFlags *);
+ PyCompilerFlags *);
static void err_input(perrdetail *);
static void initsigs(void);
static void call_py_exitfuncs(void);
@@ -97,7 +97,7 @@ since _warnings is builtin. This API should not be used. */
PyObject *
PyModule_GetWarningsModule(void)
{
- return PyImport_ImportModule("warnings");
+ return PyImport_ImportModule("warnings");
}
static int initialized = 0;
@@ -107,7 +107,7 @@ static int initialized = 0;
int
Py_IsInitialized(void)
{
- return initialized;
+ return initialized;
}
/* Global initializations. Can be undone by Py_Finalize(). Don't
@@ -125,191 +125,191 @@ Py_IsInitialized(void)
static int
add_flag(int flag, const char *envs)
{
- int env = atoi(envs);
- if (flag < env)
- flag = env;
- if (flag < 1)
- flag = 1;
- return flag;
+ int env = atoi(envs);
+ if (flag < env)
+ flag = env;
+ if (flag < 1)
+ flag = 1;
+ return flag;
}
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
static char*
get_codeset(void)
{
- char* codeset;
- PyObject *codec, *name;
+ char* codeset;
+ PyObject *codec, *name;
- codeset = nl_langinfo(CODESET);
- if (!codeset || codeset[0] == '\0')
- return NULL;
+ codeset = nl_langinfo(CODESET);
+ if (!codeset || codeset[0] == '\0')
+ return NULL;
- codec = _PyCodec_Lookup(codeset);
- if (!codec)
- goto error;
+ codec = _PyCodec_Lookup(codeset);
+ if (!codec)
+ goto error;
- name = PyObject_GetAttrString(codec, "name");
- Py_CLEAR(codec);
- if (!name)
- goto error;
+ name = PyObject_GetAttrString(codec, "name");
+ Py_CLEAR(codec);
+ if (!name)
+ goto error;
- codeset = strdup(_PyUnicode_AsString(name));
- Py_DECREF(name);
- return codeset;
+ codeset = strdup(_PyUnicode_AsString(name));
+ Py_DECREF(name);
+ return codeset;
error:
- Py_XDECREF(codec);
- PyErr_Clear();
- return NULL;
+ Py_XDECREF(codec);
+ PyErr_Clear();
+ return NULL;
}
#endif
void
Py_InitializeEx(int install_sigs)
{
- PyInterpreterState *interp;
- PyThreadState *tstate;
- PyObject *bimod, *sysmod, *pstderr;
- char *p;
+ PyInterpreterState *interp;
+ PyThreadState *tstate;
+ PyObject *bimod, *sysmod, *pstderr;
+ char *p;
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
- char *codeset;
+ char *codeset;
#endif
- extern void _Py_ReadyTypes(void);
+ extern void _Py_ReadyTypes(void);
- if (initialized)
- return;
- initialized = 1;
+ if (initialized)
+ return;
+ initialized = 1;
#ifdef HAVE_SETLOCALE
- /* Set up the LC_CTYPE locale, so we can obtain
- the locale's charset without having to switch
- locales. */
- setlocale(LC_CTYPE, "");
+ /* Set up the LC_CTYPE locale, so we can obtain
+ the locale's charset without having to switch
+ locales. */
+ setlocale(LC_CTYPE, "");
#endif
- if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
- Py_DebugFlag = add_flag(Py_DebugFlag, p);
- if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
- Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
- if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
- Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
- if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
- Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
-
- interp = PyInterpreterState_New();
- if (interp == NULL)
- Py_FatalError("Py_Initialize: can't make first interpreter");
-
- tstate = PyThreadState_New(interp);
- if (tstate == NULL)
- Py_FatalError("Py_Initialize: can't make first thread");
- (void) PyThreadState_Swap(tstate);
-
- _Py_ReadyTypes();
-
- if (!_PyFrame_Init())
- Py_FatalError("Py_Initialize: can't init frames");
-
- if (!_PyLong_Init())
- Py_FatalError("Py_Initialize: can't init longs");
-
- if (!PyByteArray_Init())
- Py_FatalError("Py_Initialize: can't init bytearray");
-
- _PyFloat_Init();
-
- interp->modules = PyDict_New();
- if (interp->modules == NULL)
- Py_FatalError("Py_Initialize: can't make modules dictionary");
- interp->modules_reloading = PyDict_New();
- if (interp->modules_reloading == NULL)
- Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
-
- /* Init Unicode implementation; relies on the codec registry */
- _PyUnicode_Init();
-
- bimod = _PyBuiltin_Init();
- if (bimod == NULL)
- Py_FatalError("Py_Initialize: can't initialize builtins modules");
- _PyImport_FixupExtension(bimod, "builtins", "builtins");
- interp->builtins = PyModule_GetDict(bimod);
- if (interp->builtins == NULL)
- Py_FatalError("Py_Initialize: can't initialize builtins dict");
- Py_INCREF(interp->builtins);
-
- /* initialize builtin exceptions */
- _PyExc_Init();
-
- sysmod = _PySys_Init();
- if (sysmod == NULL)
- Py_FatalError("Py_Initialize: can't initialize sys");
- interp->sysdict = PyModule_GetDict(sysmod);
- if (interp->sysdict == NULL)
- Py_FatalError("Py_Initialize: can't initialize sys dict");
- Py_INCREF(interp->sysdict);
- _PyImport_FixupExtension(sysmod, "sys", "sys");
- PySys_SetPath(Py_GetPath());
- PyDict_SetItemString(interp->sysdict, "modules",
- interp->modules);
-
- /* Set up a preliminary stderr printer until we have enough
- infrastructure for the io module in place. */
- pstderr = PyFile_NewStdPrinter(fileno(stderr));
- if (pstderr == NULL)
- Py_FatalError("Py_Initialize: can't set preliminary stderr");
- PySys_SetObject("stderr", pstderr);
- PySys_SetObject("__stderr__", pstderr);
-
- _PyImport_Init();
-
- _PyImportHooks_Init();
+ if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
+ Py_DebugFlag = add_flag(Py_DebugFlag, p);
+ if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
+ Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
+ if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
+ Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
+ if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
+ Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
+
+ interp = PyInterpreterState_New();
+ if (interp == NULL)
+ Py_FatalError("Py_Initialize: can't make first interpreter");
+
+ tstate = PyThreadState_New(interp);
+ if (tstate == NULL)
+ Py_FatalError("Py_Initialize: can't make first thread");
+ (void) PyThreadState_Swap(tstate);
+
+ _Py_ReadyTypes();
+
+ if (!_PyFrame_Init())
+ Py_FatalError("Py_Initialize: can't init frames");
+
+ if (!_PyLong_Init())
+ Py_FatalError("Py_Initialize: can't init longs");
+
+ if (!PyByteArray_Init())
+ Py_FatalError("Py_Initialize: can't init bytearray");
+
+ _PyFloat_Init();
+
+ interp->modules = PyDict_New();
+ if (interp->modules == NULL)
+ Py_FatalError("Py_Initialize: can't make modules dictionary");
+ interp->modules_reloading = PyDict_New();
+ if (interp->modules_reloading == NULL)
+ Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
+
+ /* Init Unicode implementation; relies on the codec registry */
+ _PyUnicode_Init();
+
+ bimod = _PyBuiltin_Init();
+ if (bimod == NULL)
+ Py_FatalError("Py_Initialize: can't initialize builtins modules");
+ _PyImport_FixupExtension(bimod, "builtins", "builtins");
+ interp->builtins = PyModule_GetDict(bimod);
+ if (interp->builtins == NULL)
+ Py_FatalError("Py_Initialize: can't initialize builtins dict");
+ Py_INCREF(interp->builtins);
+
+ /* initialize builtin exceptions */
+ _PyExc_Init();
+
+ sysmod = _PySys_Init();
+ if (sysmod == NULL)
+ Py_FatalError("Py_Initialize: can't initialize sys");
+ interp->sysdict = PyModule_GetDict(sysmod);
+ if (interp->sysdict == NULL)
+ Py_FatalError("Py_Initialize: can't initialize sys dict");
+ Py_INCREF(interp->sysdict);
+ _PyImport_FixupExtension(sysmod, "sys", "sys");
+ PySys_SetPath(Py_GetPath());
+ PyDict_SetItemString(interp->sysdict, "modules",
+ interp->modules);
+
+ /* Set up a preliminary stderr printer until we have enough
+ infrastructure for the io module in place. */
+ pstderr = PyFile_NewStdPrinter(fileno(stderr));
+ if (pstderr == NULL)
+ Py_FatalError("Py_Initialize: can't set preliminary stderr");
+ PySys_SetObject("stderr", pstderr);
+ PySys_SetObject("__stderr__", pstderr);
+
+ _PyImport_Init();
+
+ _PyImportHooks_Init();
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
- /* On Unix, set the file system encoding according to the
- user's preference, if the CODESET names a well-known
- Python codec, and Py_FileSystemDefaultEncoding isn't
- initialized by other means. Also set the encoding of
- stdin and stdout if these are terminals. */
-
- codeset = get_codeset();
- if (codeset) {
- if (!Py_FileSystemDefaultEncoding)
- Py_FileSystemDefaultEncoding = codeset;
- else
- free(codeset);
- }
+ /* On Unix, set the file system encoding according to the
+ user's preference, if the CODESET names a well-known
+ Python codec, and Py_FileSystemDefaultEncoding isn't
+ initialized by other means. Also set the encoding of
+ stdin and stdout if these are terminals. */
+
+ codeset = get_codeset();
+ if (codeset) {
+ if (!Py_FileSystemDefaultEncoding)
+ Py_FileSystemDefaultEncoding = codeset;
+ else
+ free(codeset);
+ }
#endif
- if (install_sigs)
- initsigs(); /* Signal handling stuff, including initintr() */
-
- /* Initialize warnings. */
- _PyWarnings_Init();
- if (PySys_HasWarnOptions()) {
- PyObject *warnings_module = PyImport_ImportModule("warnings");
- if (!warnings_module)
- PyErr_Clear();
- Py_XDECREF(warnings_module);
- }
-
- initmain(); /* Module __main__ */
- if (initstdio() < 0)
- Py_FatalError(
- "Py_Initialize: can't initialize sys standard streams");
-
- /* auto-thread-state API, if available */
+ if (install_sigs)
+ initsigs(); /* Signal handling stuff, including initintr() */
+
+ /* Initialize warnings. */
+ _PyWarnings_Init();
+ if (PySys_HasWarnOptions()) {
+ PyObject *warnings_module = PyImport_ImportModule("warnings");
+ if (!warnings_module)
+ PyErr_Clear();
+ Py_XDECREF(warnings_module);
+ }
+
+ initmain(); /* Module __main__ */
+ if (initstdio() < 0)
+ Py_FatalError(
+ "Py_Initialize: can't initialize sys standard streams");
+
+ /* auto-thread-state API, if available */
#ifdef WITH_THREAD
- _PyGILState_Init(interp, tstate);
+ _PyGILState_Init(interp, tstate);
#endif /* WITH_THREAD */
- if (!Py_NoSiteFlag)
- initsite(); /* Module site */
+ if (!Py_NoSiteFlag)
+ initsite(); /* Module site */
}
void
Py_Initialize(void)
{
- Py_InitializeEx(1);
+ Py_InitializeEx(1);
}
@@ -322,25 +322,25 @@ extern void dump_counts(FILE*);
static void
flush_std_files(void)
{
- PyObject *fout = PySys_GetObject("stdout");
- PyObject *ferr = PySys_GetObject("stderr");
- PyObject *tmp;
+ PyObject *fout = PySys_GetObject("stdout");
+ PyObject *ferr = PySys_GetObject("stderr");
+ PyObject *tmp;
- if (fout != NULL && fout != Py_None) {
- tmp = PyObject_CallMethod(fout, "flush", "");
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
- }
+ if (fout != NULL && fout != Py_None) {
+ tmp = PyObject_CallMethod(fout, "flush", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(tmp);
+ }
- if (ferr != NULL || ferr != Py_None) {
- tmp = PyObject_CallMethod(ferr, "flush", "");
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
- }
+ if (ferr != NULL || ferr != Py_None) {
+ tmp = PyObject_CallMethod(ferr, "flush", "");
+ if (tmp == NULL)
+ PyErr_Clear();
+ else
+ Py_DECREF(tmp);
+ }
}
/* Undo the effect of Py_Initialize().
@@ -360,169 +360,169 @@ flush_std_files(void)
void
Py_Finalize(void)
{
- PyInterpreterState *interp;
- PyThreadState *tstate;
-
- if (!initialized)
- return;
-
- wait_for_thread_shutdown();
-
- /* The interpreter is still entirely intact at this point, and the
- * exit funcs may be relying on that. In particular, if some thread
- * or exit func is still waiting to do an import, the import machinery
- * expects Py_IsInitialized() to return true. So don't say the
- * interpreter is uninitialized until after the exit funcs have run.
- * Note that Threading.py uses an exit func to do a join on all the
- * threads created thru it, so this also protects pending imports in
- * the threads created via Threading.
- */
- call_py_exitfuncs();
- initialized = 0;
-
- /* Flush stdout+stderr */
- flush_std_files();
-
- /* Get current thread state and interpreter pointer */
- tstate = PyThreadState_GET();
- interp = tstate->interp;
-
- /* Disable signal handling */
- PyOS_FiniInterrupts();
-
- /* Clear type lookup cache */
- PyType_ClearCache();
-
- /* Collect garbage. This may call finalizers; it's nice to call these
- * before all modules are destroyed.
- * XXX If a __del__ or weakref callback is triggered here, and tries to
- * XXX import a module, bad things can happen, because Python no
- * XXX longer believes it's initialized.
- * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
- * XXX is easy to provoke that way. I've also seen, e.g.,
- * XXX Exception exceptions.ImportError: 'No module named sha'
- * XXX in <function callback at 0x008F5718> ignored
- * XXX but I'm unclear on exactly how that one happens. In any case,
- * XXX I haven't seen a real-life report of either of these.
- */
- PyGC_Collect();
+ PyInterpreterState *interp;
+ PyThreadState *tstate;
+
+ if (!initialized)
+ return;
+
+ wait_for_thread_shutdown();
+
+ /* The interpreter is still entirely intact at this point, and the
+ * exit funcs may be relying on that. In particular, if some thread
+ * or exit func is still waiting to do an import, the import machinery
+ * expects Py_IsInitialized() to return true. So don't say the
+ * interpreter is uninitialized until after the exit funcs have run.
+ * Note that Threading.py uses an exit func to do a join on all the
+ * threads created thru it, so this also protects pending imports in
+ * the threads created via Threading.
+ */
+ call_py_exitfuncs();
+ initialized = 0;
+
+ /* Flush stdout+stderr */
+ flush_std_files();
+
+ /* Get current thread state and interpreter pointer */
+ tstate = PyThreadState_GET();
+ interp = tstate->interp;
+
+ /* Disable signal handling */
+ PyOS_FiniInterrupts();
+
+ /* Clear type lookup cache */
+ PyType_ClearCache();
+
+ /* Collect garbage. This may call finalizers; it's nice to call these
+ * before all modules are destroyed.
+ * XXX If a __del__ or weakref callback is triggered here, and tries to
+ * XXX import a module, bad things can happen, because Python no
+ * XXX longer believes it's initialized.
+ * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
+ * XXX is easy to provoke that way. I've also seen, e.g.,
+ * XXX Exception exceptions.ImportError: 'No module named sha'
+ * XXX in <function callback at 0x008F5718> ignored
+ * XXX but I'm unclear on exactly how that one happens. In any case,
+ * XXX I haven't seen a real-life report of either of these.
+ */
+ PyGC_Collect();
#ifdef COUNT_ALLOCS
- /* With COUNT_ALLOCS, it helps to run GC multiple times:
- each collection might release some types from the type
- list, so they become garbage. */
- while (PyGC_Collect() > 0)
- /* nothing */;
+ /* With COUNT_ALLOCS, it helps to run GC multiple times:
+ each collection might release some types from the type
+ list, so they become garbage. */
+ while (PyGC_Collect() > 0)
+ /* nothing */;
#endif
- /* Destroy all modules */
- PyImport_Cleanup();
-
- /* Flush stdout+stderr (again, in case more was printed) */
- flush_std_files();
-
- /* Collect final garbage. This disposes of cycles created by
- * new-style class definitions, for example.
- * XXX This is disabled because it caused too many problems. If
- * XXX a __del__ or weakref callback triggers here, Python code has
- * XXX a hard time running, because even the sys module has been
- * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
- * XXX One symptom is a sequence of information-free messages
- * XXX coming from threads (if a __del__ or callback is invoked,
- * XXX other threads can execute too, and any exception they encounter
- * XXX triggers a comedy of errors as subsystem after subsystem
- * XXX fails to find what it *expects* to find in sys to help report
- * XXX the exception and consequent unexpected failures). I've also
- * XXX seen segfaults then, after adding print statements to the
- * XXX Python code getting called.
- */
+ /* Destroy all modules */
+ PyImport_Cleanup();
+
+ /* Flush stdout+stderr (again, in case more was printed) */
+ flush_std_files();
+
+ /* Collect final garbage. This disposes of cycles created by
+ * new-style class definitions, for example.
+ * XXX This is disabled because it caused too many problems. If
+ * XXX a __del__ or weakref callback triggers here, Python code has
+ * XXX a hard time running, because even the sys module has been
+ * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
+ * XXX One symptom is a sequence of information-free messages
+ * XXX coming from threads (if a __del__ or callback is invoked,
+ * XXX other threads can execute too, and any exception they encounter
+ * XXX triggers a comedy of errors as subsystem after subsystem
+ * XXX fails to find what it *expects* to find in sys to help report
+ * XXX the exception and consequent unexpected failures). I've also
+ * XXX seen segfaults then, after adding print statements to the
+ * XXX Python code getting called.
+ */
#if 0
- PyGC_Collect();
+ PyGC_Collect();
#endif
- /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
- _PyImport_Fini();
+ /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
+ _PyImport_Fini();
- /* Debugging stuff */
+ /* Debugging stuff */
#ifdef COUNT_ALLOCS
- dump_counts(stdout);
+ dump_counts(stdout);
#endif
- PRINT_TOTAL_REFS();
+ PRINT_TOTAL_REFS();
#ifdef Py_TRACE_REFS
- /* Display all objects still alive -- this can invoke arbitrary
- * __repr__ overrides, so requires a mostly-intact interpreter.
- * Alas, a lot of stuff may still be alive now that will be cleaned
- * up later.
- */
- if (Py_GETENV("PYTHONDUMPREFS"))
- _Py_PrintReferences(stderr);
+ /* Display all objects still alive -- this can invoke arbitrary
+ * __repr__ overrides, so requires a mostly-intact interpreter.
+ * Alas, a lot of stuff may still be alive now that will be cleaned
+ * up later.
+ */
+ if (Py_GETENV("PYTHONDUMPREFS"))
+ _Py_PrintReferences(stderr);
#endif /* Py_TRACE_REFS */
- /* Clear interpreter state */
- PyInterpreterState_Clear(interp);
+ /* Clear interpreter state */
+ PyInterpreterState_Clear(interp);
- /* Now we decref the exception classes. After this point nothing
- can raise an exception. That's okay, because each Fini() method
- below has been checked to make sure no exceptions are ever
- raised.
- */
+ /* Now we decref the exception classes. After this point nothing
+ can raise an exception. That's okay, because each Fini() method
+ below has been checked to make sure no exceptions are ever
+ raised.
+ */
- _PyExc_Fini();
+ _PyExc_Fini();
- /* Cleanup auto-thread-state */
+ /* Cleanup auto-thread-state */
#ifdef WITH_THREAD
- _PyGILState_Fini();
+ _PyGILState_Fini();
#endif /* WITH_THREAD */
- /* Delete current thread */
- PyThreadState_Swap(NULL);
- PyInterpreterState_Delete(interp);
-
- /* Sundry finalizers */
- PyMethod_Fini();
- PyFrame_Fini();
- PyCFunction_Fini();
- PyTuple_Fini();
- PyList_Fini();
- PySet_Fini();
- PyBytes_Fini();
- PyByteArray_Fini();
- PyLong_Fini();
- PyFloat_Fini();
- PyDict_Fini();
-
- /* Cleanup Unicode implementation */
- _PyUnicode_Fini();
-
- /* reset file system default encoding */
- if (!Py_HasFileSystemDefaultEncoding) {
- free((char*)Py_FileSystemDefaultEncoding);
- Py_FileSystemDefaultEncoding = NULL;
- }
-
- /* XXX Still allocated:
- - various static ad-hoc pointers to interned strings
- - int and float free list blocks
- - whatever various modules and libraries allocate
- */
-
- PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
+ /* Delete current thread */
+ PyThreadState_Swap(NULL);
+ PyInterpreterState_Delete(interp);
+
+ /* Sundry finalizers */
+ PyMethod_Fini();
+ PyFrame_Fini();
+ PyCFunction_Fini();
+ PyTuple_Fini();
+ PyList_Fini();
+ PySet_Fini();
+ PyBytes_Fini();
+ PyByteArray_Fini();
+ PyLong_Fini();
+ PyFloat_Fini();
+ PyDict_Fini();
+
+ /* Cleanup Unicode implementation */
+ _PyUnicode_Fini();
+
+ /* reset file system default encoding */
+ if (!Py_HasFileSystemDefaultEncoding) {
+ free((char*)Py_FileSystemDefaultEncoding);
+ Py_FileSystemDefaultEncoding = NULL;
+ }
+
+ /* XXX Still allocated:
+ - various static ad-hoc pointers to interned strings
+ - int and float free list blocks
+ - whatever various modules and libraries allocate
+ */
+
+ PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
#ifdef Py_TRACE_REFS
- /* Display addresses (& refcnts) of all objects still alive.
- * An address can be used to find the repr of the object, printed
- * above by _Py_PrintReferences.
- */
- if (Py_GETENV("PYTHONDUMPREFS"))
- _Py_PrintReferenceAddresses(stderr);
+ /* Display addresses (& refcnts) of all objects still alive.
+ * An address can be used to find the repr of the object, printed
+ * above by _Py_PrintReferences.
+ */
+ if (Py_GETENV("PYTHONDUMPREFS"))
+ _Py_PrintReferenceAddresses(stderr);
#endif /* Py_TRACE_REFS */
#ifdef PYMALLOC_DEBUG
- if (Py_GETENV("PYTHONMALLOCSTATS"))
- _PyObject_DebugMallocStats();
+ if (Py_GETENV("PYTHONMALLOCSTATS"))
+ _PyObject_DebugMallocStats();
#endif
- call_ll_exitfuncs();
+ call_ll_exitfuncs();
}
/* Create and initialize a new interpreter and thread, and return the
@@ -541,81 +541,81 @@ Py_Finalize(void)
PyThreadState *
Py_NewInterpreter(void)
{
- PyInterpreterState *interp;
- PyThreadState *tstate, *save_tstate;
- PyObject *bimod, *sysmod;
-
- if (!initialized)
- Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
-
- interp = PyInterpreterState_New();
- if (interp == NULL)
- return NULL;
-
- tstate = PyThreadState_New(interp);
- if (tstate == NULL) {
- PyInterpreterState_Delete(interp);
- return NULL;
- }
-
- save_tstate = PyThreadState_Swap(tstate);
-
- /* XXX The following is lax in error checking */
-
- interp->modules = PyDict_New();
- interp->modules_reloading = PyDict_New();
-
- bimod = _PyImport_FindExtension("builtins", "builtins");
- if (bimod != NULL) {
- interp->builtins = PyModule_GetDict(bimod);
- if (interp->builtins == NULL)
- goto handle_error;
- Py_INCREF(interp->builtins);
- }
-
- /* initialize builtin exceptions */
- _PyExc_Init();
-
- sysmod = _PyImport_FindExtension("sys", "sys");
- if (bimod != NULL && sysmod != NULL) {
- PyObject *pstderr;
- interp->sysdict = PyModule_GetDict(sysmod);
- if (interp->sysdict == NULL)
- goto handle_error;
- Py_INCREF(interp->sysdict);
- PySys_SetPath(Py_GetPath());
- PyDict_SetItemString(interp->sysdict, "modules",
- interp->modules);
- /* Set up a preliminary stderr printer until we have enough
- infrastructure for the io module in place. */
- pstderr = PyFile_NewStdPrinter(fileno(stderr));
- if (pstderr == NULL)
- Py_FatalError("Py_Initialize: can't set preliminary stderr");
- PySys_SetObject("stderr", pstderr);
- PySys_SetObject("__stderr__", pstderr);
-
- _PyImportHooks_Init();
- if (initstdio() < 0)
- Py_FatalError(
- "Py_Initialize: can't initialize sys standard streams");
- initmain();
- if (!Py_NoSiteFlag)
- initsite();
- }
-
- if (!PyErr_Occurred())
- return tstate;
+ PyInterpreterState *interp;
+ PyThreadState *tstate, *save_tstate;
+ PyObject *bimod, *sysmod;
+
+ if (!initialized)
+ Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
+
+ interp = PyInterpreterState_New();
+ if (interp == NULL)
+ return NULL;
+
+ tstate = PyThreadState_New(interp);
+ if (tstate == NULL) {
+ PyInterpreterState_Delete(interp);
+ return NULL;
+ }
+
+ save_tstate = PyThreadState_Swap(tstate);
+
+ /* XXX The following is lax in error checking */
+
+ interp->modules = PyDict_New();
+ interp->modules_reloading = PyDict_New();
+
+ bimod = _PyImport_FindExtension("builtins", "builtins");
+ if (bimod != NULL) {
+ interp->builtins = PyModule_GetDict(bimod);
+ if (interp->builtins == NULL)
+ goto handle_error;
+ Py_INCREF(interp->builtins);
+ }
+
+ /* initialize builtin exceptions */
+ _PyExc_Init();
+
+ sysmod = _PyImport_FindExtension("sys", "sys");
+ if (bimod != NULL && sysmod != NULL) {
+ PyObject *pstderr;
+ interp->sysdict = PyModule_GetDict(sysmod);
+ if (interp->sysdict == NULL)
+ goto handle_error;
+ Py_INCREF(interp->sysdict);
+ PySys_SetPath(Py_GetPath());
+ PyDict_SetItemString(interp->sysdict, "modules",
+ interp->modules);
+ /* Set up a preliminary stderr printer until we have enough
+ infrastructure for the io module in place. */
+ pstderr = PyFile_NewStdPrinter(fileno(stderr));
+ if (pstderr == NULL)
+ Py_FatalError("Py_Initialize: can't set preliminary stderr");
+ PySys_SetObject("stderr", pstderr);
+ PySys_SetObject("__stderr__", pstderr);
+
+ _PyImportHooks_Init();
+ if (initstdio() < 0)
+ Py_FatalError(
+ "Py_Initialize: can't initialize sys standard streams");
+ initmain();
+ if (!Py_NoSiteFlag)
+ initsite();
+ }
+
+ if (!PyErr_Occurred())
+ return tstate;
handle_error:
- /* Oops, it didn't work. Undo it all. */
+ /* Oops, it didn't work. Undo it all. */
- PyErr_Print();
- PyThreadState_Clear(tstate);
- PyThreadState_Swap(save_tstate);
- PyThreadState_Delete(tstate);
- PyInterpreterState_Delete(interp);
+ PyErr_Print();
+ PyThreadState_Clear(tstate);
+ PyThreadState_Swap(save_tstate);
+ PyThreadState_Delete(tstate);
+ PyInterpreterState_Delete(interp);
- return NULL;
+ return NULL;
}
/* Delete an interpreter and its last thread. This requires that the
@@ -633,19 +633,19 @@ handle_error:
void
Py_EndInterpreter(PyThreadState *tstate)
{
- PyInterpreterState *interp = tstate->interp;
+ PyInterpreterState *interp = tstate->interp;
- if (tstate != PyThreadState_GET())
- Py_FatalError("Py_EndInterpreter: thread is not current");
- if (tstate->frame != NULL)
- Py_FatalError("Py_EndInterpreter: thread still has a frame");
- if (tstate != interp->tstate_head || tstate->next != NULL)
- Py_FatalError("Py_EndInterpreter: not the last thread");
+ if (tstate != PyThreadState_GET())
+ Py_FatalError("Py_EndInterpreter: thread is not current");
+ if (tstate->frame != NULL)
+ Py_FatalError("Py_EndInterpreter: thread still has a frame");
+ if (tstate != interp->tstate_head || tstate->next != NULL)
+ Py_FatalError("Py_EndInterpreter: not the last thread");
- PyImport_Cleanup();
- PyInterpreterState_Clear(interp);
- PyThreadState_Swap(NULL);
- PyInterpreterState_Delete(interp);
+ PyImport_Cleanup();
+ PyInterpreterState_Clear(interp);
+ PyThreadState_Swap(NULL);
+ PyInterpreterState_Delete(interp);
}
static wchar_t *progname = L"python";
@@ -653,14 +653,14 @@ static wchar_t *progname = L"python";
void
Py_SetProgramName(wchar_t *pn)
{
- if (pn && *pn)
- progname = pn;
+ if (pn && *pn)
+ progname = pn;
}
wchar_t *
Py_GetProgramName(void)
{
- return progname;
+ return progname;
}
static wchar_t *default_home = NULL;
@@ -669,23 +669,23 @@ static wchar_t env_home[PATH_MAX+1];
void
Py_SetPythonHome(wchar_t *home)
{
- default_home = home;
+ default_home = home;
}
wchar_t *
Py_GetPythonHome(void)
{
- wchar_t *home = default_home;
- if (home == NULL && !Py_IgnoreEnvironmentFlag) {
- char* chome = Py_GETENV("PYTHONHOME");
- if (chome) {
- size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
- if (r != (size_t)-1 && r <= PATH_MAX)
- home = env_home;
- }
+ wchar_t *home = default_home;
+ if (home == NULL && !Py_IgnoreEnvironmentFlag) {
+ char* chome = Py_GETENV("PYTHONHOME");
+ if (chome) {
+ size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
+ if (r != (size_t)-1 && r <= PATH_MAX)
+ home = env_home;
+ }
- }
- return home;
+ }
+ return home;
}
/* Create __main__ module */
@@ -693,18 +693,18 @@ Py_GetPythonHome(void)
static void
initmain(void)
{
- PyObject *m, *d;
- m = PyImport_AddModule("__main__");
- if (m == NULL)
- Py_FatalError("can't create __main__ module");
- d = PyModule_GetDict(m);
- if (PyDict_GetItemString(d, "__builtins__") == NULL) {
- PyObject *bimod = PyImport_ImportModule("builtins");
- if (bimod == NULL ||
- PyDict_SetItemString(d, "__builtins__", bimod) != 0)
- Py_FatalError("can't add __builtins__ to __main__");
- Py_DECREF(bimod);
- }
+ PyObject *m, *d;
+ m = PyImport_AddModule("__main__");
+ if (m == NULL)
+ Py_FatalError("can't create __main__ module");
+ d = PyModule_GetDict(m);
+ if (PyDict_GetItemString(d, "__builtins__") == NULL) {
+ PyObject *bimod = PyImport_ImportModule("builtins");
+ if (bimod == NULL ||
+ PyDict_SetItemString(d, "__builtins__", bimod) != 0)
+ Py_FatalError("can't add __builtins__ to __main__");
+ Py_DECREF(bimod);
+ }
}
/* Import the site module (not into __main__ though) */
@@ -712,396 +712,396 @@ initmain(void)
static void
initsite(void)
{
- PyObject *m, *f;
- m = PyImport_ImportModule("site");
- if (m == NULL) {
- f = PySys_GetObject("stderr");
- if (f == NULL || f == Py_None)
- return;
- if (Py_VerboseFlag) {
- PyFile_WriteString(
- "'import site' failed; traceback:\n", f);
- PyErr_Print();
- }
- else {
- PyFile_WriteString(
- "'import site' failed; use -v for traceback\n", f);
- PyErr_Clear();
- }
- }
- else {
- Py_DECREF(m);
- }
+ PyObject *m, *f;
+ m = PyImport_ImportModule("site");
+ if (m == NULL) {
+ f = PySys_GetObject("stderr");
+ if (f == NULL || f == Py_None)
+ return;
+ if (Py_VerboseFlag) {
+ PyFile_WriteString(
+ "'import site' failed; traceback:\n", f);
+ PyErr_Print();
+ }
+ else {
+ PyFile_WriteString(
+ "'import site' failed; use -v for traceback\n", f);
+ PyErr_Clear();
+ }
+ }
+ else {
+ Py_DECREF(m);
+ }
}
static PyObject*
create_stdio(PyObject* io,
- int fd, int write_mode, char* name,
- char* encoding, char* errors)
-{
- PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
- const char* mode;
- PyObject *line_buffering;
- int buffering, isatty;
-
- /* stdin is always opened in buffered mode, first because it shouldn't
- make a difference in common use cases, second because TextIOWrapper
- depends on the presence of a read1() method which only exists on
- buffered streams.
- */
- if (Py_UnbufferedStdioFlag && write_mode)
- buffering = 0;
- else
- buffering = -1;
- if (write_mode)
- mode = "wb";
- else
- mode = "rb";
- buf = PyObject_CallMethod(io, "open", "isiOOOi",
- fd, mode, buffering,
- Py_None, Py_None, Py_None, 0);
- if (buf == NULL)
- goto error;
-
- if (buffering) {
- raw = PyObject_GetAttrString(buf, "raw");
- if (raw == NULL)
- goto error;
- }
- else {
- raw = buf;
- Py_INCREF(raw);
- }
-
- text = PyUnicode_FromString(name);
- if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
- goto error;
- res = PyObject_CallMethod(raw, "isatty", "");
- if (res == NULL)
- goto error;
- isatty = PyObject_IsTrue(res);
- Py_DECREF(res);
- if (isatty == -1)
- goto error;
- if (isatty || Py_UnbufferedStdioFlag)
- line_buffering = Py_True;
- else
- line_buffering = Py_False;
-
- Py_CLEAR(raw);
- Py_CLEAR(text);
-
- stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
- buf, encoding, errors,
- "\n", line_buffering);
- Py_CLEAR(buf);
- if (stream == NULL)
- goto error;
-
- if (write_mode)
- mode = "w";
- else
- mode = "r";
- text = PyUnicode_FromString(mode);
- if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
- goto error;
- Py_CLEAR(text);
- return stream;
+ int fd, int write_mode, char* name,
+ char* encoding, char* errors)
+{
+ PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
+ const char* mode;
+ PyObject *line_buffering;
+ int buffering, isatty;
+
+ /* stdin is always opened in buffered mode, first because it shouldn't
+ make a difference in common use cases, second because TextIOWrapper
+ depends on the presence of a read1() method which only exists on
+ buffered streams.
+ */
+ if (Py_UnbufferedStdioFlag && write_mode)
+ buffering = 0;
+ else
+ buffering = -1;
+ if (write_mode)
+ mode = "wb";
+ else
+ mode = "rb";
+ buf = PyObject_CallMethod(io, "open", "isiOOOi",
+ fd, mode, buffering,
+ Py_None, Py_None, Py_None, 0);
+ if (buf == NULL)
+ goto error;
+
+ if (buffering) {
+ raw = PyObject_GetAttrString(buf, "raw");
+ if (raw == NULL)
+ goto error;
+ }
+ else {
+ raw = buf;
+ Py_INCREF(raw);
+ }
+
+ text = PyUnicode_FromString(name);
+ if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
+ goto error;
+ res = PyObject_CallMethod(raw, "isatty", "");
+ if (res == NULL)
+ goto error;
+ isatty = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (isatty == -1)
+ goto error;
+ if (isatty || Py_UnbufferedStdioFlag)
+ line_buffering = Py_True;
+ else
+ line_buffering = Py_False;
+
+ Py_CLEAR(raw);
+ Py_CLEAR(text);
+
+ stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
+ buf, encoding, errors,
+ "\n", line_buffering);
+ Py_CLEAR(buf);
+ if (stream == NULL)
+ goto error;
+
+ if (write_mode)
+ mode = "w";
+ else
+ mode = "r";
+ text = PyUnicode_FromString(mode);
+ if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
+ goto error;
+ Py_CLEAR(text);
+ return stream;
error:
- Py_XDECREF(buf);
- Py_XDECREF(stream);
- Py_XDECREF(text);
- Py_XDECREF(raw);
- return NULL;
+ Py_XDECREF(buf);
+ Py_XDECREF(stream);
+ Py_XDECREF(text);
+ Py_XDECREF(raw);
+ return NULL;
}
/* Initialize sys.stdin, stdout, stderr and builtins.open */
static int
initstdio(void)
{
- PyObject *iomod = NULL, *wrapper;
- PyObject *bimod = NULL;
- PyObject *m;
- PyObject *std = NULL;
- int status = 0, fd;
- PyObject * encoding_attr;
- char *encoding = NULL, *errors;
-
- /* Hack to avoid a nasty recursion issue when Python is invoked
- in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
- if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
- goto error;
- }
- Py_DECREF(m);
-
- if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
- goto error;
- }
- Py_DECREF(m);
-
- if (!(bimod = PyImport_ImportModule("builtins"))) {
- goto error;
- }
-
- if (!(iomod = PyImport_ImportModule("io"))) {
- goto error;
- }
- if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
- goto error;
- }
-
- /* Set builtins.open */
- if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
- goto error;
- }
-
- encoding = Py_GETENV("PYTHONIOENCODING");
- errors = NULL;
- if (encoding) {
- encoding = strdup(encoding);
- errors = strchr(encoding, ':');
- if (errors) {
- *errors = '\0';
- errors++;
- }
- }
-
- /* Set sys.stdin */
- fd = fileno(stdin);
- /* Under some conditions stdin, stdout and stderr may not be connected
- * and fileno() may point to an invalid file descriptor. For example
- * GUI apps don't have valid standard streams by default.
- */
- if (fd < 0) {
+ PyObject *iomod = NULL, *wrapper;
+ PyObject *bimod = NULL;
+ PyObject *m;
+ PyObject *std = NULL;
+ int status = 0, fd;
+ PyObject * encoding_attr;
+ char *encoding = NULL, *errors;
+
+ /* Hack to avoid a nasty recursion issue when Python is invoked
+ in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
+ if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
+ goto error;
+ }
+ Py_DECREF(m);
+
+ if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
+ goto error;
+ }
+ Py_DECREF(m);
+
+ if (!(bimod = PyImport_ImportModule("builtins"))) {
+ goto error;
+ }
+
+ if (!(iomod = PyImport_ImportModule("io"))) {
+ goto error;
+ }
+ if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
+ goto error;
+ }
+
+ /* Set builtins.open */
+ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
+ goto error;
+ }
+
+ encoding = Py_GETENV("PYTHONIOENCODING");
+ errors = NULL;
+ if (encoding) {
+ encoding = strdup(encoding);
+ errors = strchr(encoding, ':');
+ if (errors) {
+ *errors = '\0';
+ errors++;
+ }
+ }
+
+ /* Set sys.stdin */
+ fd = fileno(stdin);
+ /* Under some conditions stdin, stdout and stderr may not be connected
+ * and fileno() may point to an invalid file descriptor. For example
+ * GUI apps don't have valid standard streams by default.
+ */
+ if (fd < 0) {
#ifdef MS_WINDOWS
- std = Py_None;
- Py_INCREF(std);
+ std = Py_None;
+ Py_INCREF(std);
#else
- goto error;
+ goto error;
#endif
- }
- else {
- std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
- if (std == NULL)
- goto error;
- } /* if (fd < 0) */
- PySys_SetObject("__stdin__", std);
- PySys_SetObject("stdin", std);
- Py_DECREF(std);
-
- /* Set sys.stdout */
- fd = fileno(stdout);
- if (fd < 0) {
+ }
+ else {
+ std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
+ if (std == NULL)
+ goto error;
+ } /* if (fd < 0) */
+ PySys_SetObject("__stdin__", std);
+ PySys_SetObject("stdin", std);
+ Py_DECREF(std);
+
+ /* Set sys.stdout */
+ fd = fileno(stdout);
+ if (fd < 0) {
#ifdef MS_WINDOWS
- std = Py_None;
- Py_INCREF(std);
+ std = Py_None;
+ Py_INCREF(std);
#else
- goto error;
+ goto error;
#endif
- }
- else {
- std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
- if (std == NULL)
- goto error;
- } /* if (fd < 0) */
- PySys_SetObject("__stdout__", std);
- PySys_SetObject("stdout", std);
- Py_DECREF(std);
+ }
+ else {
+ std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
+ if (std == NULL)
+ goto error;
+ } /* if (fd < 0) */
+ PySys_SetObject("__stdout__", std);
+ PySys_SetObject("stdout", std);
+ Py_DECREF(std);
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
- /* Set sys.stderr, replaces the preliminary stderr */
- fd = fileno(stderr);
- if (fd < 0) {
+ /* Set sys.stderr, replaces the preliminary stderr */
+ fd = fileno(stderr);
+ if (fd < 0) {
#ifdef MS_WINDOWS
- std = Py_None;
- Py_INCREF(std);
+ std = Py_None;
+ Py_INCREF(std);
#else
- goto error;
+ goto error;
#endif
- }
- else {
- std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
- if (std == NULL)
- goto error;
- } /* if (fd < 0) */
-
- /* Same as hack above, pre-import stderr's codec to avoid recursion
- when import.c tries to write to stderr in verbose mode. */
- encoding_attr = PyObject_GetAttrString(std, "encoding");
- if (encoding_attr != NULL) {
- const char * encoding;
- encoding = _PyUnicode_AsString(encoding_attr);
- if (encoding != NULL) {
- _PyCodec_Lookup(encoding);
- }
- }
- PyErr_Clear(); /* Not a fatal error if codec isn't available */
-
- PySys_SetObject("__stderr__", std);
- PySys_SetObject("stderr", std);
- Py_DECREF(std);
+ }
+ else {
+ std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
+ if (std == NULL)
+ goto error;
+ } /* if (fd < 0) */
+
+ /* Same as hack above, pre-import stderr's codec to avoid recursion
+ when import.c tries to write to stderr in verbose mode. */
+ encoding_attr = PyObject_GetAttrString(std, "encoding");
+ if (encoding_attr != NULL) {
+ const char * encoding;
+ encoding = _PyUnicode_AsString(encoding_attr);
+ if (encoding != NULL) {
+ _PyCodec_Lookup(encoding);
+ }
+ }
+ PyErr_Clear(); /* Not a fatal error if codec isn't available */
+
+ PySys_SetObject("__stderr__", std);
+ PySys_SetObject("stderr", std);
+ Py_DECREF(std);
#endif
- if (0) {
+ if (0) {
error:
- status = -1;
- }
+ status = -1;
+ }
- if (encoding)
- free(encoding);
- Py_XDECREF(bimod);
- Py_XDECREF(iomod);
- return status;
+ if (encoding)
+ free(encoding);
+ Py_XDECREF(bimod);
+ Py_XDECREF(iomod);
+ return status;
}
/* Parse input from a file and execute it */
int
PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
- PyCompilerFlags *flags)
+ PyCompilerFlags *flags)
{
- if (filename == NULL)
- filename = "???";
- if (Py_FdIsInteractive(fp, filename)) {
- int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
- if (closeit)
- fclose(fp);
- return err;
- }
- else
- return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
+ if (filename == NULL)
+ filename = "???";
+ if (Py_FdIsInteractive(fp, filename)) {
+ int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
+ if (closeit)
+ fclose(fp);
+ return err;
+ }
+ else
+ return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
}
int
PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
{
- PyObject *v;
- int ret;
- PyCompilerFlags local_flags;
-
- if (flags == NULL) {
- flags = &local_flags;
- local_flags.cf_flags = 0;
- }
- v = PySys_GetObject("ps1");
- if (v == NULL) {
- PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
- Py_XDECREF(v);
- }
- v = PySys_GetObject("ps2");
- if (v == NULL) {
- PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
- Py_XDECREF(v);
- }
- for (;;) {
- ret = PyRun_InteractiveOneFlags(fp, filename, flags);
- PRINT_TOTAL_REFS();
- if (ret == E_EOF)
- return 0;
- /*
- if (ret == E_NOMEM)
- return -1;
- */
- }
+ PyObject *v;
+ int ret;
+ PyCompilerFlags local_flags;
+
+ if (flags == NULL) {
+ flags = &local_flags;
+ local_flags.cf_flags = 0;
+ }
+ v = PySys_GetObject("ps1");
+ if (v == NULL) {
+ PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
+ Py_XDECREF(v);
+ }
+ v = PySys_GetObject("ps2");
+ if (v == NULL) {
+ PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
+ Py_XDECREF(v);
+ }
+ for (;;) {
+ ret = PyRun_InteractiveOneFlags(fp, filename, flags);
+ PRINT_TOTAL_REFS();
+ if (ret == E_EOF)
+ return 0;
+ /*
+ if (ret == E_NOMEM)
+ return -1;
+ */
+ }
}
/* compute parser flags based on compiler flags */
static int PARSER_FLAGS(PyCompilerFlags *flags)
{
- int parser_flags = 0;
- if (!flags)
- return 0;
- if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
- parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
- if (flags->cf_flags & PyCF_IGNORE_COOKIE)
- parser_flags |= PyPARSE_IGNORE_COOKIE;
- if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
- parser_flags |= PyPARSE_BARRY_AS_BDFL;
- return parser_flags;
+ int parser_flags = 0;
+ if (!flags)
+ return 0;
+ if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
+ parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
+ if (flags->cf_flags & PyCF_IGNORE_COOKIE)
+ parser_flags |= PyPARSE_IGNORE_COOKIE;
+ if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
+ parser_flags |= PyPARSE_BARRY_AS_BDFL;
+ return parser_flags;
}
#if 0
/* Keep an example of flags with future keyword support. */
#define PARSER_FLAGS(flags) \
- ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
- PyPARSE_DONT_IMPLY_DEDENT : 0) \
- | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
- PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
+ ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
+ PyPARSE_DONT_IMPLY_DEDENT : 0) \
+ | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
+ PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
#endif
int
PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
{
- PyObject *m, *d, *v, *w, *oenc = NULL;
- mod_ty mod;
- PyArena *arena;
- char *ps1 = "", *ps2 = "", *enc = NULL;
- int errcode = 0;
-
- if (fp == stdin) {
- /* Fetch encoding from sys.stdin */
- v = PySys_GetObject("stdin");
- if (v == NULL || v == Py_None)
- return -1;
- oenc = PyObject_GetAttrString(v, "encoding");
- if (!oenc)
- return -1;
- enc = _PyUnicode_AsString(oenc);
- }
- v = PySys_GetObject("ps1");
- if (v != NULL) {
- v = PyObject_Str(v);
- if (v == NULL)
- PyErr_Clear();
- else if (PyUnicode_Check(v))
- ps1 = _PyUnicode_AsString(v);
- }
- w = PySys_GetObject("ps2");
- if (w != NULL) {
- w = PyObject_Str(w);
- if (w == NULL)
- PyErr_Clear();
- else if (PyUnicode_Check(w))
- ps2 = _PyUnicode_AsString(w);
- }
- arena = PyArena_New();
- if (arena == NULL) {
- Py_XDECREF(v);
- Py_XDECREF(w);
- Py_XDECREF(oenc);
- return -1;
- }
- mod = PyParser_ASTFromFile(fp, filename, enc,
- Py_single_input, ps1, ps2,
- flags, &errcode, arena);
- Py_XDECREF(v);
- Py_XDECREF(w);
- Py_XDECREF(oenc);
- if (mod == NULL) {
- PyArena_Free(arena);
- if (errcode == E_EOF) {
- PyErr_Clear();
- return E_EOF;
- }
- PyErr_Print();
- return -1;
- }
- m = PyImport_AddModule("__main__");
- if (m == NULL) {
- PyArena_Free(arena);
- return -1;
- }
- d = PyModule_GetDict(m);
- v = run_mod(mod, filename, d, d, flags, arena);
- PyArena_Free(arena);
- flush_io();
- if (v == NULL) {
- PyErr_Print();
- return -1;
- }
- Py_DECREF(v);
- return 0;
+ PyObject *m, *d, *v, *w, *oenc = NULL;
+ mod_ty mod;
+ PyArena *arena;
+ char *ps1 = "", *ps2 = "", *enc = NULL;
+ int errcode = 0;
+
+ if (fp == stdin) {
+ /* Fetch encoding from sys.stdin */
+ v = PySys_GetObject("stdin");
+ if (v == NULL || v == Py_None)
+ return -1;
+ oenc = PyObject_GetAttrString(v, "encoding");
+ if (!oenc)
+ return -1;
+ enc = _PyUnicode_AsString(oenc);
+ }
+ v = PySys_GetObject("ps1");
+ if (v != NULL) {
+ v = PyObject_Str(v);
+ if (v == NULL)
+ PyErr_Clear();
+ else if (PyUnicode_Check(v))
+ ps1 = _PyUnicode_AsString(v);
+ }
+ w = PySys_GetObject("ps2");
+ if (w != NULL) {
+ w = PyObject_Str(w);
+ if (w == NULL)
+ PyErr_Clear();
+ else if (PyUnicode_Check(w))
+ ps2 = _PyUnicode_AsString(w);
+ }
+ arena = PyArena_New();
+ if (arena == NULL) {
+ Py_XDECREF(v);
+ Py_XDECREF(w);
+ Py_XDECREF(oenc);
+ return -1;
+ }
+ mod = PyParser_ASTFromFile(fp, filename, enc,
+ Py_single_input, ps1, ps2,
+ flags, &errcode, arena);
+ Py_XDECREF(v);
+ Py_XDECREF(w);
+ Py_XDECREF(oenc);
+ if (mod == NULL) {
+ PyArena_Free(arena);
+ if (errcode == E_EOF) {
+ PyErr_Clear();
+ return E_EOF;
+ }
+ PyErr_Print();
+ return -1;
+ }
+ m = PyImport_AddModule("__main__");
+ if (m == NULL) {
+ PyArena_Free(arena);
+ return -1;
+ }
+ d = PyModule_GetDict(m);
+ v = run_mod(mod, filename, d, d, flags, arena);
+ PyArena_Free(arena);
+ flush_io();
+ if (v == NULL) {
+ PyErr_Print();
+ return -1;
+ }
+ Py_DECREF(v);
+ return 0;
}
/* Check whether a file maybe a pyc file: Look at the extension,
@@ -1110,737 +1110,737 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
static int
maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
{
- if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
- return 1;
-
- /* Only look into the file if we are allowed to close it, since
- it then should also be seekable. */
- if (closeit) {
- /* Read only two bytes of the magic. If the file was opened in
- text mode, the bytes 3 and 4 of the magic (\r\n) might not
- be read as they are on disk. */
- unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
- unsigned char buf[2];
- /* Mess: In case of -x, the stream is NOT at its start now,
- and ungetc() was used to push back the first newline,
- which makes the current stream position formally undefined,
- and a x-platform nightmare.
- Unfortunately, we have no direct way to know whether -x
- was specified. So we use a terrible hack: if the current
- stream position is not 0, we assume -x was specified, and
- give up. Bug 132850 on SourceForge spells out the
- hopelessness of trying anything else (fseek and ftell
- don't work predictably x-platform for text-mode files).
- */
- int ispyc = 0;
- if (ftell(fp) == 0) {
- if (fread(buf, 1, 2, fp) == 2 &&
- ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
- ispyc = 1;
- rewind(fp);
- }
- return ispyc;
- }
- return 0;
+ if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
+ return 1;
+
+ /* Only look into the file if we are allowed to close it, since
+ it then should also be seekable. */
+ if (closeit) {
+ /* Read only two bytes of the magic. If the file was opened in
+ text mode, the bytes 3 and 4 of the magic (\r\n) might not
+ be read as they are on disk. */
+ unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
+ unsigned char buf[2];
+ /* Mess: In case of -x, the stream is NOT at its start now,
+ and ungetc() was used to push back the first newline,
+ which makes the current stream position formally undefined,
+ and a x-platform nightmare.
+ Unfortunately, we have no direct way to know whether -x
+ was specified. So we use a terrible hack: if the current
+ stream position is not 0, we assume -x was specified, and
+ give up. Bug 132850 on SourceForge spells out the
+ hopelessness of trying anything else (fseek and ftell
+ don't work predictably x-platform for text-mode files).
+ */
+ int ispyc = 0;
+ if (ftell(fp) == 0) {
+ if (fread(buf, 1, 2, fp) == 2 &&
+ ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
+ ispyc = 1;
+ rewind(fp);
+ }
+ return ispyc;
+ }
+ return 0;
}
int
PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
- PyCompilerFlags *flags)
-{
- PyObject *m, *d, *v;
- const char *ext;
- int set_file_name = 0, ret, len;
-
- m = PyImport_AddModule("__main__");
- if (m == NULL)
- return -1;
- d = PyModule_GetDict(m);
- if (PyDict_GetItemString(d, "__file__") == NULL) {
- PyObject *f;
- f = PyUnicode_DecodeFSDefault(filename);
- if (f == NULL)
- return -1;
- if (PyDict_SetItemString(d, "__file__", f) < 0) {
- Py_DECREF(f);
- return -1;
- }
- set_file_name = 1;
- Py_DECREF(f);
- }
- len = strlen(filename);
- ext = filename + len - (len > 4 ? 4 : 0);
- if (maybe_pyc_file(fp, filename, ext, closeit)) {
- /* Try to run a pyc file. First, re-open in binary */
- if (closeit)
- fclose(fp);
- if ((fp = fopen(filename, "rb")) == NULL) {
- fprintf(stderr, "python: Can't reopen .pyc file\n");
- ret = -1;
- goto done;
- }
- /* Turn on optimization if a .pyo file is given */
- if (strcmp(ext, ".pyo") == 0)
- Py_OptimizeFlag = 1;
- v = run_pyc_file(fp, filename, d, d, flags);
- } else {
- v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
- closeit, flags);
- }
- flush_io();
- if (v == NULL) {
- PyErr_Print();
- ret = -1;
- goto done;
- }
- Py_DECREF(v);
- ret = 0;
+ PyCompilerFlags *flags)
+{
+ PyObject *m, *d, *v;
+ const char *ext;
+ int set_file_name = 0, ret, len;
+
+ m = PyImport_AddModule("__main__");
+ if (m == NULL)
+ return -1;
+ d = PyModule_GetDict(m);
+ if (PyDict_GetItemString(d, "__file__") == NULL) {
+ PyObject *f;
+ f = PyUnicode_DecodeFSDefault(filename);
+ if (f == NULL)
+ return -1;
+ if (PyDict_SetItemString(d, "__file__", f) < 0) {
+ Py_DECREF(f);
+ return -1;
+ }
+ set_file_name = 1;
+ Py_DECREF(f);
+ }
+ len = strlen(filename);
+ ext = filename + len - (len > 4 ? 4 : 0);
+ if (maybe_pyc_file(fp, filename, ext, closeit)) {
+ /* Try to run a pyc file. First, re-open in binary */
+ if (closeit)
+ fclose(fp);
+ if ((fp = fopen(filename, "rb")) == NULL) {
+ fprintf(stderr, "python: Can't reopen .pyc file\n");
+ ret = -1;
+ goto done;
+ }
+ /* Turn on optimization if a .pyo file is given */
+ if (strcmp(ext, ".pyo") == 0)
+ Py_OptimizeFlag = 1;
+ v = run_pyc_file(fp, filename, d, d, flags);
+ } else {
+ v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
+ closeit, flags);
+ }
+ flush_io();
+ if (v == NULL) {
+ PyErr_Print();
+ ret = -1;
+ goto done;
+ }
+ Py_DECREF(v);
+ ret = 0;
done:
- if (set_file_name && PyDict_DelItemString(d, "__file__"))
- PyErr_Clear();
- return ret;
+ if (set_file_name && PyDict_DelItemString(d, "__file__"))
+ PyErr_Clear();
+ return ret;
}
int
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
{
- PyObject *m, *d, *v;
- m = PyImport_AddModule("__main__");
- if (m == NULL)
- return -1;
- d = PyModule_GetDict(m);
- v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
- if (v == NULL) {
- PyErr_Print();
- return -1;
- }
- Py_DECREF(v);
- return 0;
+ PyObject *m, *d, *v;
+ m = PyImport_AddModule("__main__");
+ if (m == NULL)
+ return -1;
+ d = PyModule_GetDict(m);
+ v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
+ if (v == NULL) {
+ PyErr_Print();
+ return -1;
+ }
+ Py_DECREF(v);
+ return 0;
}
static int
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
- int *lineno, int *offset, const char **text)
-{
- long hold;
- PyObject *v;
-
- /* old style errors */
- if (PyTuple_Check(err))
- return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
- lineno, offset, text);
-
- /* new style errors. `err' is an instance */
-
- if (! (v = PyObject_GetAttrString(err, "msg")))
- goto finally;
- *message = v;
-
- if (!(v = PyObject_GetAttrString(err, "filename")))
- goto finally;
- if (v == Py_None)
- *filename = NULL;
- else if (! (*filename = _PyUnicode_AsString(v)))
- goto finally;
-
- Py_DECREF(v);
- if (!(v = PyObject_GetAttrString(err, "lineno")))
- goto finally;
- hold = PyLong_AsLong(v);
- Py_DECREF(v);
- v = NULL;
- if (hold < 0 && PyErr_Occurred())
- goto finally;
- *lineno = (int)hold;
-
- if (!(v = PyObject_GetAttrString(err, "offset")))
- goto finally;
- if (v == Py_None) {
- *offset = -1;
- Py_DECREF(v);
- v = NULL;
- } else {
- hold = PyLong_AsLong(v);
- Py_DECREF(v);
- v = NULL;
- if (hold < 0 && PyErr_Occurred())
- goto finally;
- *offset = (int)hold;
- }
-
- if (!(v = PyObject_GetAttrString(err, "text")))
- goto finally;
- if (v == Py_None)
- *text = NULL;
- else if (!PyUnicode_Check(v) ||
- !(*text = _PyUnicode_AsString(v)))
- goto finally;
- Py_DECREF(v);
- return 1;
+ int *lineno, int *offset, const char **text)
+{
+ long hold;
+ PyObject *v;
+
+ /* old style errors */
+ if (PyTuple_Check(err))
+ return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
+ lineno, offset, text);
+
+ /* new style errors. `err' is an instance */
+
+ if (! (v = PyObject_GetAttrString(err, "msg")))
+ goto finally;
+ *message = v;
+
+ if (!(v = PyObject_GetAttrString(err, "filename")))
+ goto finally;
+ if (v == Py_None)
+ *filename = NULL;
+ else if (! (*filename = _PyUnicode_AsString(v)))
+ goto finally;
+
+ Py_DECREF(v);
+ if (!(v = PyObject_GetAttrString(err, "lineno")))
+ goto finally;
+ hold = PyLong_AsLong(v);
+ Py_DECREF(v);
+ v = NULL;
+ if (hold < 0 && PyErr_Occurred())
+ goto finally;
+ *lineno = (int)hold;
+
+ if (!(v = PyObject_GetAttrString(err, "offset")))
+ goto finally;
+ if (v == Py_None) {
+ *offset = -1;
+ Py_DECREF(v);
+ v = NULL;
+ } else {
+ hold = PyLong_AsLong(v);
+ Py_DECREF(v);
+ v = NULL;
+ if (hold < 0 && PyErr_Occurred())
+ goto finally;
+ *offset = (int)hold;
+ }
+
+ if (!(v = PyObject_GetAttrString(err, "text")))
+ goto finally;
+ if (v == Py_None)
+ *text = NULL;
+ else if (!PyUnicode_Check(v) ||
+ !(*text = _PyUnicode_AsString(v)))
+ goto finally;
+ Py_DECREF(v);
+ return 1;
finally:
- Py_XDECREF(v);
- return 0;
+ Py_XDECREF(v);
+ return 0;
}
void
PyErr_Print(void)
{
- PyErr_PrintEx(1);
+ PyErr_PrintEx(1);
}
static void
print_error_text(PyObject *f, int offset, const char *text)
{
- char *nl;
- if (offset >= 0) {
- if (offset > 0 && offset == (int)strlen(text))
- offset--;
- for (;;) {
- nl = strchr(text, '\n');
- if (nl == NULL || nl-text >= offset)
- break;
- offset -= (int)(nl+1-text);
- text = nl+1;
- }
- while (*text == ' ' || *text == '\t') {
- text++;
- offset--;
- }
- }
- PyFile_WriteString(" ", f);
- PyFile_WriteString(text, f);
- if (*text == '\0' || text[strlen(text)-1] != '\n')
- PyFile_WriteString("\n", f);
- if (offset == -1)
- return;
- PyFile_WriteString(" ", f);
- offset--;
- while (offset > 0) {
- PyFile_WriteString(" ", f);
- offset--;
- }
- PyFile_WriteString("^\n", f);
+ char *nl;
+ if (offset >= 0) {
+ if (offset > 0 && offset == (int)strlen(text))
+ offset--;
+ for (;;) {
+ nl = strchr(text, '\n');
+ if (nl == NULL || nl-text >= offset)
+ break;
+ offset -= (int)(nl+1-text);
+ text = nl+1;
+ }
+ while (*text == ' ' || *text == '\t') {
+ text++;
+ offset--;
+ }
+ }
+ PyFile_WriteString(" ", f);
+ PyFile_WriteString(text, f);
+ if (*text == '\0' || text[strlen(text)-1] != '\n')
+ PyFile_WriteString("\n", f);
+ if (offset == -1)
+ return;
+ PyFile_WriteString(" ", f);
+ offset--;
+ while (offset > 0) {
+ PyFile_WriteString(" ", f);
+ offset--;
+ }
+ PyFile_WriteString("^\n", f);
}
static void
handle_system_exit(void)
{
- PyObject *exception, *value, *tb;
- int exitcode = 0;
-
- if (Py_InspectFlag)
- /* Don't exit if -i flag was given. This flag is set to 0
- * when entering interactive mode for inspecting. */
- return;
-
- PyErr_Fetch(&exception, &value, &tb);
- fflush(stdout);
- if (value == NULL || value == Py_None)
- goto done;
- if (PyExceptionInstance_Check(value)) {
- /* The error code should be in the `code' attribute. */
- PyObject *code = PyObject_GetAttrString(value, "code");
- if (code) {
- Py_DECREF(value);
- value = code;
- if (value == Py_None)
- goto done;
- }
- /* If we failed to dig out the 'code' attribute,
- just let the else clause below print the error. */
- }
- if (PyLong_Check(value))
- exitcode = (int)PyLong_AsLong(value);
- else {
- PyObject_Print(value, stderr, Py_PRINT_RAW);
- PySys_WriteStderr("\n");
- exitcode = 1;
- }
+ PyObject *exception, *value, *tb;
+ int exitcode = 0;
+
+ if (Py_InspectFlag)
+ /* Don't exit if -i flag was given. This flag is set to 0
+ * when entering interactive mode for inspecting. */
+ return;
+
+ PyErr_Fetch(&exception, &value, &tb);
+ fflush(stdout);
+ if (value == NULL || value == Py_None)
+ goto done;
+ if (PyExceptionInstance_Check(value)) {
+ /* The error code should be in the `code' attribute. */
+ PyObject *code = PyObject_GetAttrString(value, "code");
+ if (code) {
+ Py_DECREF(value);
+ value = code;
+ if (value == Py_None)
+ goto done;
+ }
+ /* If we failed to dig out the 'code' attribute,
+ just let the else clause below print the error. */
+ }
+ if (PyLong_Check(value))
+ exitcode = (int)PyLong_AsLong(value);
+ else {
+ PyObject_Print(value, stderr, Py_PRINT_RAW);
+ PySys_WriteStderr("\n");
+ exitcode = 1;
+ }
done:
- /* Restore and clear the exception info, in order to properly decref
- * the exception, value, and traceback. If we just exit instead,
- * these leak, which confuses PYTHONDUMPREFS output, and may prevent
- * some finalizers from running.
- */
- PyErr_Restore(exception, value, tb);
- PyErr_Clear();
- Py_Exit(exitcode);
- /* NOTREACHED */
+ /* Restore and clear the exception info, in order to properly decref
+ * the exception, value, and traceback. If we just exit instead,
+ * these leak, which confuses PYTHONDUMPREFS output, and may prevent
+ * some finalizers from running.
+ */
+ PyErr_Restore(exception, value, tb);
+ PyErr_Clear();
+ Py_Exit(exitcode);
+ /* NOTREACHED */
}
void
PyErr_PrintEx(int set_sys_last_vars)
{
- PyObject *exception, *v, *tb, *hook;
-
- if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
- handle_system_exit();
- }
- PyErr_Fetch(&exception, &v, &tb);
- if (exception == NULL)
- return;
- PyErr_NormalizeException(&exception, &v, &tb);
- if (tb == NULL) {
- tb = Py_None;
- Py_INCREF(tb);
- }
- PyException_SetTraceback(v, tb);
- if (exception == NULL)
- return;
- /* Now we know v != NULL too */
- if (set_sys_last_vars) {
- PySys_SetObject("last_type", exception);
- PySys_SetObject("last_value", v);
- PySys_SetObject("last_traceback", tb);
- }
- hook = PySys_GetObject("excepthook");
- if (hook) {
- PyObject *args = PyTuple_Pack(3, exception, v, tb);
- PyObject *result = PyEval_CallObject(hook, args);
- if (result == NULL) {
- PyObject *exception2, *v2, *tb2;
- if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
- handle_system_exit();
- }
- PyErr_Fetch(&exception2, &v2, &tb2);
- PyErr_NormalizeException(&exception2, &v2, &tb2);
- /* It should not be possible for exception2 or v2
- to be NULL. However PyErr_Display() can't
- tolerate NULLs, so just be safe. */
- if (exception2 == NULL) {
- exception2 = Py_None;
- Py_INCREF(exception2);
- }
- if (v2 == NULL) {
- v2 = Py_None;
- Py_INCREF(v2);
- }
- fflush(stdout);
- PySys_WriteStderr("Error in sys.excepthook:\n");
- PyErr_Display(exception2, v2, tb2);
- PySys_WriteStderr("\nOriginal exception was:\n");
- PyErr_Display(exception, v, tb);
- Py_DECREF(exception2);
- Py_DECREF(v2);
- Py_XDECREF(tb2);
- }
- Py_XDECREF(result);
- Py_XDECREF(args);
- } else {
- PySys_WriteStderr("sys.excepthook is missing\n");
- PyErr_Display(exception, v, tb);
- }
- Py_XDECREF(exception);
- Py_XDECREF(v);
- Py_XDECREF(tb);
+ PyObject *exception, *v, *tb, *hook;
+
+ if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
+ handle_system_exit();
+ }
+ PyErr_Fetch(&exception, &v, &tb);
+ if (exception == NULL)
+ return;
+ PyErr_NormalizeException(&exception, &v, &tb);
+ if (tb == NULL) {
+ tb = Py_None;
+ Py_INCREF(tb);
+ }
+ PyException_SetTraceback(v, tb);
+ if (exception == NULL)
+ return;
+ /* Now we know v != NULL too */
+ if (set_sys_last_vars) {
+ PySys_SetObject("last_type", exception);
+ PySys_SetObject("last_value", v);
+ PySys_SetObject("last_traceback", tb);
+ }
+ hook = PySys_GetObject("excepthook");
+ if (hook) {
+ PyObject *args = PyTuple_Pack(3, exception, v, tb);
+ PyObject *result = PyEval_CallObject(hook, args);
+ if (result == NULL) {
+ PyObject *exception2, *v2, *tb2;
+ if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
+ handle_system_exit();
+ }
+ PyErr_Fetch(&exception2, &v2, &tb2);
+ PyErr_NormalizeException(&exception2, &v2, &tb2);
+ /* It should not be possible for exception2 or v2
+ to be NULL. However PyErr_Display() can't
+ tolerate NULLs, so just be safe. */
+ if (exception2 == NULL) {
+ exception2 = Py_None;
+ Py_INCREF(exception2);
+ }
+ if (v2 == NULL) {
+ v2 = Py_None;
+ Py_INCREF(v2);
+ }
+ fflush(stdout);
+ PySys_WriteStderr("Error in sys.excepthook:\n");
+ PyErr_Display(exception2, v2, tb2);
+ PySys_WriteStderr("\nOriginal exception was:\n");
+ PyErr_Display(exception, v, tb);
+ Py_DECREF(exception2);
+ Py_DECREF(v2);
+ Py_XDECREF(tb2);
+ }
+ Py_XDECREF(result);
+ Py_XDECREF(args);
+ } else {
+ PySys_WriteStderr("sys.excepthook is missing\n");
+ PyErr_Display(exception, v, tb);
+ }
+ Py_XDECREF(exception);
+ Py_XDECREF(v);
+ Py_XDECREF(tb);
}
static void
print_exception(PyObject *f, PyObject *value)
{
- int err = 0;
- PyObject *type, *tb;
-
- if (!PyExceptionInstance_Check(value)) {
- PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
- PyFile_WriteString(Py_TYPE(value)->tp_name, f);
- PyFile_WriteString(" found\n", f);
- return;
- }
-
- Py_INCREF(value);
- fflush(stdout);
- type = (PyObject *) Py_TYPE(value);
- tb = PyException_GetTraceback(value);
- if (tb && tb != Py_None)
- err = PyTraceBack_Print(tb, f);
- if (err == 0 &&
- PyObject_HasAttrString(value, "print_file_and_line"))
- {
- PyObject *message;
- const char *filename, *text;
- int lineno, offset;
- if (!parse_syntax_error(value, &message, &filename,
- &lineno, &offset, &text))
- PyErr_Clear();
- else {
- char buf[10];
- PyFile_WriteString(" File \"", f);
- if (filename == NULL)
- PyFile_WriteString("<string>", f);
- else
- PyFile_WriteString(filename, f);
- PyFile_WriteString("\", line ", f);
- PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
- PyFile_WriteString(buf, f);
- PyFile_WriteString("\n", f);
- if (text != NULL)
- print_error_text(f, offset, text);
- Py_DECREF(value);
- value = message;
- /* Can't be bothered to check all those
- PyFile_WriteString() calls */
- if (PyErr_Occurred())
- err = -1;
- }
- }
- if (err) {
- /* Don't do anything else */
- }
- else {
- PyObject* moduleName;
- char* className;
- assert(PyExceptionClass_Check(type));
- className = PyExceptionClass_Name(type);
- if (className != NULL) {
- char *dot = strrchr(className, '.');
- if (dot != NULL)
- className = dot+1;
- }
-
- moduleName = PyObject_GetAttrString(type, "__module__");
- if (moduleName == NULL || !PyUnicode_Check(moduleName))
- {
- Py_DECREF(moduleName);
- err = PyFile_WriteString("<unknown>", f);
- }
- else {
- char* modstr = _PyUnicode_AsString(moduleName);
- if (modstr && strcmp(modstr, "builtins"))
- {
- err = PyFile_WriteString(modstr, f);
- err += PyFile_WriteString(".", f);
- }
- Py_DECREF(moduleName);
- }
- if (err == 0) {
- if (className == NULL)
- err = PyFile_WriteString("<unknown>", f);
- else
- err = PyFile_WriteString(className, f);
- }
- }
- if (err == 0 && (value != Py_None)) {
- PyObject *s = PyObject_Str(value);
- /* only print colon if the str() of the
- object is not the empty string
- */
- if (s == NULL)
- err = -1;
- else if (!PyUnicode_Check(s) ||
- PyUnicode_GetSize(s) != 0)
- err = PyFile_WriteString(": ", f);
- if (err == 0)
- err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
- Py_XDECREF(s);
- }
- /* try to write a newline in any case */
- err += PyFile_WriteString("\n", f);
- Py_XDECREF(tb);
- Py_DECREF(value);
- /* If an error happened here, don't show it.
- XXX This is wrong, but too many callers rely on this behavior. */
- if (err != 0)
- PyErr_Clear();
+ int err = 0;
+ PyObject *type, *tb;
+
+ if (!PyExceptionInstance_Check(value)) {
+ PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
+ PyFile_WriteString(Py_TYPE(value)->tp_name, f);
+ PyFile_WriteString(" found\n", f);
+ return;
+ }
+
+ Py_INCREF(value);
+ fflush(stdout);
+ type = (PyObject *) Py_TYPE(value);
+ tb = PyException_GetTraceback(value);
+ if (tb && tb != Py_None)
+ err = PyTraceBack_Print(tb, f);
+ if (err == 0 &&
+ PyObject_HasAttrString(value, "print_file_and_line"))
+ {
+ PyObject *message;
+ const char *filename, *text;
+ int lineno, offset;
+ if (!parse_syntax_error(value, &message, &filename,
+ &lineno, &offset, &text))
+ PyErr_Clear();
+ else {
+ char buf[10];
+ PyFile_WriteString(" File \"", f);
+ if (filename == NULL)
+ PyFile_WriteString("<string>", f);
+ else
+ PyFile_WriteString(filename, f);
+ PyFile_WriteString("\", line ", f);
+ PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
+ PyFile_WriteString(buf, f);
+ PyFile_WriteString("\n", f);
+ if (text != NULL)
+ print_error_text(f, offset, text);
+ Py_DECREF(value);
+ value = message;
+ /* Can't be bothered to check all those
+ PyFile_WriteString() calls */
+ if (PyErr_Occurred())
+ err = -1;
+ }
+ }
+ if (err) {
+ /* Don't do anything else */
+ }
+ else {
+ PyObject* moduleName;
+ char* className;
+ assert(PyExceptionClass_Check(type));
+ className = PyExceptionClass_Name(type);
+ if (className != NULL) {
+ char *dot = strrchr(className, '.');
+ if (dot != NULL)
+ className = dot+1;
+ }
+
+ moduleName = PyObject_GetAttrString(type, "__module__");
+ if (moduleName == NULL || !PyUnicode_Check(moduleName))
+ {
+ Py_DECREF(moduleName);
+ err = PyFile_WriteString("<unknown>", f);
+ }
+ else {
+ char* modstr = _PyUnicode_AsString(moduleName);
+ if (modstr && strcmp(modstr, "builtins"))
+ {
+ err = PyFile_WriteString(modstr, f);
+ err += PyFile_WriteString(".", f);
+ }
+ Py_DECREF(moduleName);
+ }
+ if (err == 0) {
+ if (className == NULL)
+ err = PyFile_WriteString("<unknown>", f);
+ else
+ err = PyFile_WriteString(className, f);
+ }
+ }
+ if (err == 0 && (value != Py_None)) {
+ PyObject *s = PyObject_Str(value);
+ /* only print colon if the str() of the
+ object is not the empty string
+ */
+ if (s == NULL)
+ err = -1;
+ else if (!PyUnicode_Check(s) ||
+ PyUnicode_GetSize(s) != 0)
+ err = PyFile_WriteString(": ", f);
+ if (err == 0)
+ err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
+ Py_XDECREF(s);
+ }
+ /* try to write a newline in any case */
+ err += PyFile_WriteString("\n", f);
+ Py_XDECREF(tb);
+ Py_DECREF(value);
+ /* If an error happened here, don't show it.
+ XXX This is wrong, but too many callers rely on this behavior. */
+ if (err != 0)
+ PyErr_Clear();
}
static const char *cause_message =
- "\nThe above exception was the direct cause "
- "of the following exception:\n\n";
+ "\nThe above exception was the direct cause "
+ "of the following exception:\n\n";
static const char *context_message =
- "\nDuring handling of the above exception, "
- "another exception occurred:\n\n";
+ "\nDuring handling of the above exception, "
+ "another exception occurred:\n\n";
static void
print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
{
- int err = 0, res;
- PyObject *cause, *context;
-
- if (seen != NULL) {
- /* Exception chaining */
- if (PySet_Add(seen, value) == -1)
- PyErr_Clear();
- else if (PyExceptionInstance_Check(value)) {
- cause = PyException_GetCause(value);
- context = PyException_GetContext(value);
- if (cause) {
- res = PySet_Contains(seen, cause);
- if (res == -1)
- PyErr_Clear();
- if (res == 0) {
- print_exception_recursive(
- f, cause, seen);
- err |= PyFile_WriteString(
- cause_message, f);
- }
- }
- else if (context) {
- res = PySet_Contains(seen, context);
- if (res == -1)
- PyErr_Clear();
- if (res == 0) {
- print_exception_recursive(
- f, context, seen);
- err |= PyFile_WriteString(
- context_message, f);
- }
- }
- Py_XDECREF(context);
- Py_XDECREF(cause);
- }
- }
- print_exception(f, value);
- if (err != 0)
- PyErr_Clear();
+ int err = 0, res;
+ PyObject *cause, *context;
+
+ if (seen != NULL) {
+ /* Exception chaining */
+ if (PySet_Add(seen, value) == -1)
+ PyErr_Clear();
+ else if (PyExceptionInstance_Check(value)) {
+ cause = PyException_GetCause(value);
+ context = PyException_GetContext(value);
+ if (cause) {
+ res = PySet_Contains(seen, cause);
+ if (res == -1)
+ PyErr_Clear();
+ if (res == 0) {
+ print_exception_recursive(
+ f, cause, seen);
+ err |= PyFile_WriteString(
+ cause_message, f);
+ }
+ }
+ else if (context) {
+ res = PySet_Contains(seen, context);
+ if (res == -1)
+ PyErr_Clear();
+ if (res == 0) {
+ print_exception_recursive(
+ f, context, seen);
+ err |= PyFile_WriteString(
+ context_message, f);
+ }
+ }
+ Py_XDECREF(context);
+ Py_XDECREF(cause);
+ }
+ }
+ print_exception(f, value);
+ if (err != 0)
+ PyErr_Clear();
}
void
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
{
- PyObject *seen;
- PyObject *f = PySys_GetObject("stderr");
- if (f == Py_None) {
- /* pass */
- }
- else if (f == NULL) {
- _PyObject_Dump(value);
- fprintf(stderr, "lost sys.stderr\n");
- }
- else {
- /* We choose to ignore seen being possibly NULL, and report
- at least the main exception (it could be a MemoryError).
- */
- seen = PySet_New(NULL);
- if (seen == NULL)
- PyErr_Clear();
- print_exception_recursive(f, value, seen);
- Py_XDECREF(seen);
- }
+ PyObject *seen;
+ PyObject *f = PySys_GetObject("stderr");
+ if (f == Py_None) {
+ /* pass */
+ }
+ else if (f == NULL) {
+ _PyObject_Dump(value);
+ fprintf(stderr, "lost sys.stderr\n");
+ }
+ else {
+ /* We choose to ignore seen being possibly NULL, and report
+ at least the main exception (it could be a MemoryError).
+ */
+ seen = PySet_New(NULL);
+ if (seen == NULL)
+ PyErr_Clear();
+ print_exception_recursive(f, value, seen);
+ Py_XDECREF(seen);
+ }
}
PyObject *
PyRun_StringFlags(const char *str, int start, PyObject *globals,
- PyObject *locals, PyCompilerFlags *flags)
+ PyObject *locals, PyCompilerFlags *flags)
{
- PyObject *ret = NULL;
- mod_ty mod;
- PyArena *arena = PyArena_New();
- if (arena == NULL)
- return NULL;
+ PyObject *ret = NULL;
+ mod_ty mod;
+ PyArena *arena = PyArena_New();
+ if (arena == NULL)
+ return NULL;
- mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
- if (mod != NULL)
- ret = run_mod(mod, "<string>", globals, locals, flags, arena);
- PyArena_Free(arena);
- return ret;
+ mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
+ if (mod != NULL)
+ ret = run_mod(mod, "<string>", globals, locals, flags, arena);
+ PyArena_Free(arena);
+ return ret;
}
PyObject *
PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
- PyObject *locals, int closeit, PyCompilerFlags *flags)
-{
- PyObject *ret;
- mod_ty mod;
- PyArena *arena = PyArena_New();
- if (arena == NULL)
- return NULL;
-
- mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
- flags, NULL, arena);
- if (closeit)
- fclose(fp);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
- ret = run_mod(mod, filename, globals, locals, flags, arena);
- PyArena_Free(arena);
- return ret;
+ PyObject *locals, int closeit, PyCompilerFlags *flags)
+{
+ PyObject *ret;
+ mod_ty mod;
+ PyArena *arena = PyArena_New();
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
+ flags, NULL, arena);
+ if (closeit)
+ fclose(fp);
+ if (mod == NULL) {
+ PyArena_Free(arena);
+ return NULL;
+ }
+ ret = run_mod(mod, filename, globals, locals, flags, arena);
+ PyArena_Free(arena);
+ return ret;
}
static void
flush_io(void)
{
- PyObject *f, *r;
- PyObject *type, *value, *traceback;
+ PyObject *f, *r;
+ PyObject *type, *value, *traceback;
- /* Save the current exception */
- PyErr_Fetch(&type, &value, &traceback);
+ /* Save the current exception */
+ PyErr_Fetch(&type, &value, &traceback);
- f = PySys_GetObject("stderr");
- if (f != NULL) {
- r = PyObject_CallMethod(f, "flush", "");
- if (r)
- Py_DECREF(r);
- else
- PyErr_Clear();
- }
- f = PySys_GetObject("stdout");
- if (f != NULL) {
- r = PyObject_CallMethod(f, "flush", "");
- if (r)
- Py_DECREF(r);
- else
- PyErr_Clear();
- }
+ f = PySys_GetObject("stderr");
+ if (f != NULL) {
+ r = PyObject_CallMethod(f, "flush", "");
+ if (r)
+ Py_DECREF(r);
+ else
+ PyErr_Clear();
+ }
+ f = PySys_GetObject("stdout");
+ if (f != NULL) {
+ r = PyObject_CallMethod(f, "flush", "");
+ if (r)
+ Py_DECREF(r);
+ else
+ PyErr_Clear();
+ }
- PyErr_Restore(type, value, traceback);
+ PyErr_Restore(type, value, traceback);
}
static PyObject *
run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
- PyCompilerFlags *flags, PyArena *arena)
+ PyCompilerFlags *flags, PyArena *arena)
{
- PyCodeObject *co;
- PyObject *v;
- co = PyAST_Compile(mod, filename, flags, arena);
- if (co == NULL)
- return NULL;
- v = PyEval_EvalCode(co, globals, locals);
- Py_DECREF(co);
- return v;
+ PyCodeObject *co;
+ PyObject *v;
+ co = PyAST_Compile(mod, filename, flags, arena);
+ if (co == NULL)
+ return NULL;
+ v = PyEval_EvalCode(co, globals, locals);
+ Py_DECREF(co);
+ return v;
}
static PyObject *
run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
- PyObject *locals, PyCompilerFlags *flags)
-{
- PyCodeObject *co;
- PyObject *v;
- long magic;
- long PyImport_GetMagicNumber(void);
-
- magic = PyMarshal_ReadLongFromFile(fp);
- if (magic != PyImport_GetMagicNumber()) {
- PyErr_SetString(PyExc_RuntimeError,
- "Bad magic number in .pyc file");
- return NULL;
- }
- (void) PyMarshal_ReadLongFromFile(fp);
- v = PyMarshal_ReadLastObjectFromFile(fp);
- fclose(fp);
- if (v == NULL || !PyCode_Check(v)) {
- Py_XDECREF(v);
- PyErr_SetString(PyExc_RuntimeError,
- "Bad code object in .pyc file");
- return NULL;
- }
- co = (PyCodeObject *)v;
- v = PyEval_EvalCode(co, globals, locals);
- if (v && flags)
- flags->cf_flags |= (co->co_flags & PyCF_MASK);
- Py_DECREF(co);
- return v;
+ PyObject *locals, PyCompilerFlags *flags)
+{
+ PyCodeObject *co;
+ PyObject *v;
+ long magic;
+ long PyImport_GetMagicNumber(void);
+
+ magic = PyMarshal_ReadLongFromFile(fp);
+ if (magic != PyImport_GetMagicNumber()) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Bad magic number in .pyc file");
+ return NULL;
+ }
+ (void) PyMarshal_ReadLongFromFile(fp);
+ v = PyMarshal_ReadLastObjectFromFile(fp);
+ fclose(fp);
+ if (v == NULL || !PyCode_Check(v)) {
+ Py_XDECREF(v);
+ PyErr_SetString(PyExc_RuntimeError,
+ "Bad code object in .pyc file");
+ return NULL;
+ }
+ co = (PyCodeObject *)v;
+ v = PyEval_EvalCode(co, globals, locals);
+ if (v && flags)
+ flags->cf_flags |= (co->co_flags & PyCF_MASK);
+ Py_DECREF(co);
+ return v;
}
PyObject *
Py_CompileStringFlags(const char *str, const char *filename, int start,
- PyCompilerFlags *flags)
-{
- PyCodeObject *co;
- mod_ty mod;
- PyArena *arena = PyArena_New();
- if (arena == NULL)
- return NULL;
-
- mod = PyParser_ASTFromString(str, filename, start, flags, arena);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
- if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
- PyObject *result = PyAST_mod2obj(mod);
- PyArena_Free(arena);
- return result;
- }
- co = PyAST_Compile(mod, filename, flags, arena);
- PyArena_Free(arena);
- return (PyObject *)co;
+ PyCompilerFlags *flags)
+{
+ PyCodeObject *co;
+ mod_ty mod;
+ PyArena *arena = PyArena_New();
+ if (arena == NULL)
+ return NULL;
+
+ mod = PyParser_ASTFromString(str, filename, start, flags, arena);
+ if (mod == NULL) {
+ PyArena_Free(arena);
+ return NULL;
+ }
+ if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
+ PyObject *result = PyAST_mod2obj(mod);
+ PyArena_Free(arena);
+ return result;
+ }
+ co = PyAST_Compile(mod, filename, flags, arena);
+ PyArena_Free(arena);
+ return (PyObject *)co;
}
struct symtable *
Py_SymtableString(const char *str, const char *filename, int start)
{
- struct symtable *st;
- mod_ty mod;
- PyCompilerFlags flags;
- PyArena *arena = PyArena_New();
- if (arena == NULL)
- return NULL;
+ struct symtable *st;
+ mod_ty mod;
+ PyCompilerFlags flags;
+ PyArena *arena = PyArena_New();
+ if (arena == NULL)
+ return NULL;
- flags.cf_flags = 0;
- mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
- if (mod == NULL) {
- PyArena_Free(arena);
- return NULL;
- }
- st = PySymtable_Build(mod, filename, 0);
- PyArena_Free(arena);
- return st;
+ flags.cf_flags = 0;
+ mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
+ if (mod == NULL) {
+ PyArena_Free(arena);
+ return NULL;
+ }
+ st = PySymtable_Build(mod, filename, 0);
+ PyArena_Free(arena);
+ return st;
}
/* Preferred access to parser is through AST. */
mod_ty
PyParser_ASTFromString(const char *s, const char *filename, int start,
- PyCompilerFlags *flags, PyArena *arena)
-{
- mod_ty mod;
- PyCompilerFlags localflags;
- perrdetail err;
- int iflags = PARSER_FLAGS(flags);
-
- node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
- &_PyParser_Grammar, start, &err,
- &iflags);
- if (flags == NULL) {
- localflags.cf_flags = 0;
- flags = &localflags;
- }
- if (n) {
- flags->cf_flags |= iflags & PyCF_MASK;
- mod = PyAST_FromNode(n, flags, filename, arena);
- PyNode_Free(n);
- return mod;
- }
- else {
- err_input(&err);
- return NULL;
- }
+ PyCompilerFlags *flags, PyArena *arena)
+{
+ mod_ty mod;
+ PyCompilerFlags localflags;
+ perrdetail err;
+ int iflags = PARSER_FLAGS(flags);
+
+ node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
+ &_PyParser_Grammar, start, &err,
+ &iflags);
+ if (flags == NULL) {
+ localflags.cf_flags = 0;
+ flags = &localflags;
+ }
+ if (n) {
+ flags->cf_flags |= iflags & PyCF_MASK;
+ mod = PyAST_FromNode(n, flags, filename, arena);
+ PyNode_Free(n);
+ return mod;
+ }
+ else {
+ err_input(&err);
+ return NULL;
+ }
}
mod_ty
PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
- int start, char *ps1,
- char *ps2, PyCompilerFlags *flags, int *errcode,
- PyArena *arena)
-{
- mod_ty mod;
- PyCompilerFlags localflags;
- perrdetail err;
- int iflags = PARSER_FLAGS(flags);
-
- node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
- &_PyParser_Grammar,
- start, ps1, ps2, &err, &iflags);
- if (flags == NULL) {
- localflags.cf_flags = 0;
- flags = &localflags;
- }
- if (n) {
- flags->cf_flags |= iflags & PyCF_MASK;
- mod = PyAST_FromNode(n, flags, filename, arena);
- PyNode_Free(n);
- return mod;
- }
- else {
- err_input(&err);
- if (errcode)
- *errcode = err.error;
- return NULL;
- }
+ int start, char *ps1,
+ char *ps2, PyCompilerFlags *flags, int *errcode,
+ PyArena *arena)
+{
+ mod_ty mod;
+ PyCompilerFlags localflags;
+ perrdetail err;
+ int iflags = PARSER_FLAGS(flags);
+
+ node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
+ &_PyParser_Grammar,
+ start, ps1, ps2, &err, &iflags);
+ if (flags == NULL) {
+ localflags.cf_flags = 0;
+ flags = &localflags;
+ }
+ if (n) {
+ flags->cf_flags |= iflags & PyCF_MASK;
+ mod = PyAST_FromNode(n, flags, filename, arena);
+ PyNode_Free(n);
+ return mod;
+ }
+ else {
+ err_input(&err);
+ if (errcode)
+ *errcode = err.error;
+ return NULL;
+ }
}
/* Simplified interface to parsefile -- return node or set exception */
@@ -1848,14 +1848,14 @@ PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
node *
PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
{
- perrdetail err;
- node *n = PyParser_ParseFileFlags(fp, filename, NULL,
- &_PyParser_Grammar,
- start, NULL, NULL, &err, flags);
- if (n == NULL)
- err_input(&err);
+ perrdetail err;
+ node *n = PyParser_ParseFileFlags(fp, filename, NULL,
+ &_PyParser_Grammar,
+ start, NULL, NULL, &err, flags);
+ if (n == NULL)
+ err_input(&err);
- return n;
+ return n;
}
/* Simplified interface to parsestring -- return node or set exception */
@@ -1863,30 +1863,30 @@ PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int fla
node *
PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
{
- perrdetail err;
- node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
- start, &err, flags);
- if (n == NULL)
- err_input(&err);
- return n;
+ perrdetail err;
+ node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
+ start, &err, flags);
+ if (n == NULL)
+ err_input(&err);
+ return n;
}
node *
PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
- int start, int flags)
+ int start, int flags)
{
- perrdetail err;
- node *n = PyParser_ParseStringFlagsFilename(str, filename,
- &_PyParser_Grammar, start, &err, flags);
- if (n == NULL)
- err_input(&err);
- return n;
+ perrdetail err;
+ node *n = PyParser_ParseStringFlagsFilename(str, filename,
+ &_PyParser_Grammar, start, &err, flags);
+ if (n == NULL)
+ err_input(&err);
+ return n;
}
node *
PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
{
- return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
+ return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
}
/* May want to move a more generalized form of this to parsetok.c or
@@ -1895,7 +1895,7 @@ PyParser_SimpleParseStringFilename(const char *str, const char *filename, int st
void
PyParser_SetError(perrdetail *err)
{
- err_input(err);
+ err_input(err);
}
/* Set the error appropriate to the given input error code (see errcode.h) */
@@ -1903,111 +1903,111 @@ PyParser_SetError(perrdetail *err)
static void
err_input(perrdetail *err)
{
- PyObject *v, *w, *errtype, *errtext;
- PyObject* u = NULL;
- char *msg = NULL;
- errtype = PyExc_SyntaxError;
- switch (err->error) {
- case E_ERROR:
- return;
- case E_SYNTAX:
- errtype = PyExc_IndentationError;
- if (err->expected == INDENT)
- msg = "expected an indented block";
- else if (err->token == INDENT)
- msg = "unexpected indent";
- else if (err->token == DEDENT)
- msg = "unexpected unindent";
- else {
- errtype = PyExc_SyntaxError;
- msg = "invalid syntax";
- }
- break;
- case E_TOKEN:
- msg = "invalid token";
- break;
- case E_EOFS:
- msg = "EOF while scanning triple-quoted string literal";
- break;
- case E_EOLS:
- msg = "EOL while scanning string literal";
- break;
- case E_INTR:
- if (!PyErr_Occurred())
- PyErr_SetNone(PyExc_KeyboardInterrupt);
- goto cleanup;
- case E_NOMEM:
- PyErr_NoMemory();
- goto cleanup;
- case E_EOF:
- msg = "unexpected EOF while parsing";
- break;
- case E_TABSPACE:
- errtype = PyExc_TabError;
- msg = "inconsistent use of tabs and spaces in indentation";
- break;
- case E_OVERFLOW:
- msg = "expression too long";
- break;
- case E_DEDENT:
- errtype = PyExc_IndentationError;
- msg = "unindent does not match any outer indentation level";
- break;
- case E_TOODEEP:
- errtype = PyExc_IndentationError;
- msg = "too many levels of indentation";
- break;
- case E_DECODE: {
- PyObject *type, *value, *tb;
- PyErr_Fetch(&type, &value, &tb);
- if (value != NULL) {
- u = PyObject_Str(value);
- if (u != NULL) {
- msg = _PyUnicode_AsString(u);
- }
- }
- if (msg == NULL)
- msg = "unknown decode error";
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(tb);
- break;
- }
- case E_LINECONT:
- msg = "unexpected character after line continuation character";
- break;
-
- case E_IDENTIFIER:
- msg = "invalid character in identifier";
- break;
- default:
- fprintf(stderr, "error=%d\n", err->error);
- msg = "unknown parsing error";
- break;
- }
- /* err->text may not be UTF-8 in case of decoding errors.
- Explicitly convert to an object. */
- if (!err->text) {
- errtext = Py_None;
- Py_INCREF(Py_None);
- } else {
- errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
- "replace");
- }
- v = Py_BuildValue("(ziiN)", err->filename,
- err->lineno, err->offset, errtext);
- w = NULL;
- if (v != NULL)
- w = Py_BuildValue("(sO)", msg, v);
- Py_XDECREF(u);
- Py_XDECREF(v);
- PyErr_SetObject(errtype, w);
- Py_XDECREF(w);
+ PyObject *v, *w, *errtype, *errtext;
+ PyObject* u = NULL;
+ char *msg = NULL;
+ errtype = PyExc_SyntaxError;
+ switch (err->error) {
+ case E_ERROR:
+ return;
+ case E_SYNTAX:
+ errtype = PyExc_IndentationError;
+ if (err->expected == INDENT)
+ msg = "expected an indented block";
+ else if (err->token == INDENT)
+ msg = "unexpected indent";
+ else if (err->token == DEDENT)
+ msg = "unexpected unindent";
+ else {
+ errtype = PyExc_SyntaxError;
+ msg = "invalid syntax";
+ }
+ break;
+ case E_TOKEN:
+ msg = "invalid token";
+ break;
+ case E_EOFS:
+ msg = "EOF while scanning triple-quoted string literal";
+ break;
+ case E_EOLS:
+ msg = "EOL while scanning string literal";
+ break;
+ case E_INTR:
+ if (!PyErr_Occurred())
+ PyErr_SetNone(PyExc_KeyboardInterrupt);
+ goto cleanup;
+ case E_NOMEM:
+ PyErr_NoMemory();
+ goto cleanup;
+ case E_EOF:
+ msg = "unexpected EOF while parsing";
+ break;
+ case E_TABSPACE:
+ errtype = PyExc_TabError;
+ msg = "inconsistent use of tabs and spaces in indentation";
+ break;
+ case E_OVERFLOW:
+ msg = "expression too long";
+ break;
+ case E_DEDENT:
+ errtype = PyExc_IndentationError;
+ msg = "unindent does not match any outer indentation level";
+ break;
+ case E_TOODEEP:
+ errtype = PyExc_IndentationError;
+ msg = "too many levels of indentation";
+ break;
+ case E_DECODE: {
+ PyObject *type, *value, *tb;
+ PyErr_Fetch(&type, &value, &tb);
+ if (value != NULL) {
+ u = PyObject_Str(value);
+ if (u != NULL) {
+ msg = _PyUnicode_AsString(u);
+ }
+ }
+ if (msg == NULL)
+ msg = "unknown decode error";
+ Py_XDECREF(type);
+ Py_XDECREF(value);
+ Py_XDECREF(tb);
+ break;
+ }
+ case E_LINECONT:
+ msg = "unexpected character after line continuation character";
+ break;
+
+ case E_IDENTIFIER:
+ msg = "invalid character in identifier";
+ break;
+ default:
+ fprintf(stderr, "error=%d\n", err->error);
+ msg = "unknown parsing error";
+ break;
+ }
+ /* err->text may not be UTF-8 in case of decoding errors.
+ Explicitly convert to an object. */
+ if (!err->text) {
+ errtext = Py_None;
+ Py_INCREF(Py_None);
+ } else {
+ errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
+ "replace");
+ }
+ v = Py_BuildValue("(ziiN)", err->filename,
+ err->lineno, err->offset, errtext);
+ w = NULL;
+ if (v != NULL)
+ w = Py_BuildValue("(sO)", msg, v);
+ Py_XDECREF(u);
+ Py_XDECREF(v);
+ PyErr_SetObject(errtype, w);
+ Py_XDECREF(w);
cleanup:
- if (err->text != NULL) {
- PyObject_FREE(err->text);
- err->text = NULL;
- }
+ if (err->text != NULL) {
+ PyObject_FREE(err->text);
+ err->text = NULL;
+ }
}
/* Print fatal error message and abort */
@@ -2015,32 +2015,32 @@ cleanup:
void
Py_FatalError(const char *msg)
{
- fprintf(stderr, "Fatal Python error: %s\n", msg);
- fflush(stderr); /* it helps in Windows debug build */
- if (PyErr_Occurred()) {
- PyErr_Print();
- }
+ fprintf(stderr, "Fatal Python error: %s\n", msg);
+ fflush(stderr); /* it helps in Windows debug build */
+ if (PyErr_Occurred()) {
+ PyErr_Print();
+ }
#ifdef MS_WINDOWS
- {
- size_t len = strlen(msg);
- WCHAR* buffer;
- size_t i;
-
- /* Convert the message to wchar_t. This uses a simple one-to-one
- conversion, assuming that the this error message actually uses ASCII
- only. If this ceases to be true, we will have to convert. */
- buffer = alloca( (len+1) * (sizeof *buffer));
- for( i=0; i<=len; ++i)
- buffer[i] = msg[i];
- OutputDebugStringW(L"Fatal Python error: ");
- OutputDebugStringW(buffer);
- OutputDebugStringW(L"\n");
- }
+ {
+ size_t len = strlen(msg);
+ WCHAR* buffer;
+ size_t i;
+
+ /* Convert the message to wchar_t. This uses a simple one-to-one
+ conversion, assuming that the this error message actually uses ASCII
+ only. If this ceases to be true, we will have to convert. */
+ buffer = alloca( (len+1) * (sizeof *buffer));
+ for( i=0; i<=len; ++i)
+ buffer[i] = msg[i];
+ OutputDebugStringW(L"Fatal Python error: ");
+ OutputDebugStringW(buffer);
+ OutputDebugStringW(L"\n");
+ }
#ifdef _DEBUG
- DebugBreak();
+ DebugBreak();
#endif
#endif /* MS_WINDOWS */
- abort();
+ abort();
}
/* Clean up and exit */
@@ -2053,17 +2053,17 @@ static void (*pyexitfunc)(void) = NULL;
/* For the atexit module. */
void _Py_PyAtExit(void (*func)(void))
{
- pyexitfunc = func;
+ pyexitfunc = func;
}
static void
call_py_exitfuncs(void)
{
- if (pyexitfunc == NULL)
- return;
+ if (pyexitfunc == NULL)
+ return;
- (*pyexitfunc)();
- PyErr_Clear();
+ (*pyexitfunc)();
+ PyErr_Clear();
}
/* Wait until threading._shutdown completes, provided
@@ -2074,23 +2074,23 @@ static void
wait_for_thread_shutdown(void)
{
#ifdef WITH_THREAD
- PyObject *result;
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
- "threading");
- if (threading == NULL) {
- /* threading not imported */
- PyErr_Clear();
- return;
- }
- result = PyObject_CallMethod(threading, "_shutdown", "");
- if (result == NULL) {
- PyErr_WriteUnraisable(threading);
- }
- else {
- Py_DECREF(result);
- }
- Py_DECREF(threading);
+ PyObject *result;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
+ "threading");
+ if (threading == NULL) {
+ /* threading not imported */
+ PyErr_Clear();
+ return;
+ }
+ result = PyObject_CallMethod(threading, "_shutdown", "");
+ if (result == NULL) {
+ PyErr_WriteUnraisable(threading);
+ }
+ else {
+ Py_DECREF(result);
+ }
+ Py_DECREF(threading);
#endif
}
@@ -2100,43 +2100,43 @@ static int nexitfuncs = 0;
int Py_AtExit(void (*func)(void))
{
- if (nexitfuncs >= NEXITFUNCS)
- return -1;
- exitfuncs[nexitfuncs++] = func;
- return 0;
+ if (nexitfuncs >= NEXITFUNCS)
+ return -1;
+ exitfuncs[nexitfuncs++] = func;
+ return 0;
}
static void
call_ll_exitfuncs(void)
{
- while (nexitfuncs > 0)
- (*exitfuncs[--nexitfuncs])();
+ while (nexitfuncs > 0)
+ (*exitfuncs[--nexitfuncs])();
- fflush(stdout);
- fflush(stderr);
+ fflush(stdout);
+ fflush(stderr);
}
void
Py_Exit(int sts)
{
- Py_Finalize();
+ Py_Finalize();
- exit(sts);
+ exit(sts);
}
static void
initsigs(void)
{
#ifdef SIGPIPE
- PyOS_setsig(SIGPIPE, SIG_IGN);
+ PyOS_setsig(SIGPIPE, SIG_IGN);
#endif
#ifdef SIGXFZ
- PyOS_setsig(SIGXFZ, SIG_IGN);
+ PyOS_setsig(SIGXFZ, SIG_IGN);
#endif
#ifdef SIGXFSZ
- PyOS_setsig(SIGXFSZ, SIG_IGN);
+ PyOS_setsig(SIGXFSZ, SIG_IGN);
#endif
- PyOS_InitInterrupts(); /* May imply initsignal() */
+ PyOS_InitInterrupts(); /* May imply initsignal() */
}
@@ -2149,13 +2149,13 @@ initsigs(void)
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
- if (isatty((int)fileno(fp)))
- return 1;
- if (!Py_InteractiveFlag)
- return 0;
- return (filename == NULL) ||
- (strcmp(filename, "<stdin>") == 0) ||
- (strcmp(filename, "???") == 0);
+ if (isatty((int)fileno(fp)))
+ return 1;
+ if (!Py_InteractiveFlag)
+ return 0;
+ return (filename == NULL) ||
+ (strcmp(filename, "<stdin>") == 0) ||
+ (strcmp(filename, "???") == 0);
}
@@ -2173,21 +2173,21 @@ Py_FdIsInteractive(FILE *fp, const char *filename)
int
PyOS_CheckStack(void)
{
- __try {
- /* alloca throws a stack overflow exception if there's
- not enough space left on the stack */
- alloca(PYOS_STACK_MARGIN * sizeof(void*));
- return 0;
- } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
- EXCEPTION_EXECUTE_HANDLER :
- EXCEPTION_CONTINUE_SEARCH) {
- int errcode = _resetstkoflw();
- if (errcode == 0)
- {
- Py_FatalError("Could not reset the stack!");
- }
- }
- return 1;
+ __try {
+ /* alloca throws a stack overflow exception if there's
+ not enough space left on the stack */
+ alloca(PYOS_STACK_MARGIN * sizeof(void*));
+ return 0;
+ } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
+ EXCEPTION_EXECUTE_HANDLER :
+ EXCEPTION_CONTINUE_SEARCH) {
+ int errcode = _resetstkoflw();
+ if (errcode == 0)
+ {
+ Py_FatalError("Could not reset the stack!");
+ }
+ }
+ return 1;
}
#endif /* WIN32 && _MSC_VER */
@@ -2203,33 +2203,33 @@ PyOS_sighandler_t
PyOS_getsig(int sig)
{
#ifdef HAVE_SIGACTION
- struct sigaction context;
- if (sigaction(sig, NULL, &context) == -1)
- return SIG_ERR;
- return context.sa_handler;
+ struct sigaction context;
+ if (sigaction(sig, NULL, &context) == -1)
+ return SIG_ERR;
+ return context.sa_handler;
#else
- PyOS_sighandler_t handler;
+ PyOS_sighandler_t handler;
/* Special signal handling for the secure CRT in Visual Studio 2005 */
#if defined(_MSC_VER) && _MSC_VER >= 1400
- switch (sig) {
- /* Only these signals are valid */
- case SIGINT:
- case SIGILL:
- case SIGFPE:
- case SIGSEGV:
- case SIGTERM:
- case SIGBREAK:
- case SIGABRT:
- break;
- /* Don't call signal() with other values or it will assert */
- default:
- return SIG_ERR;
- }
+ switch (sig) {
+ /* Only these signals are valid */
+ case SIGINT:
+ case SIGILL:
+ case SIGFPE:
+ case SIGSEGV:
+ case SIGTERM:
+ case SIGBREAK:
+ case SIGABRT:
+ break;
+ /* Don't call signal() with other values or it will assert */
+ default:
+ return SIG_ERR;
+ }
#endif /* _MSC_VER && _MSC_VER >= 1400 */
- handler = signal(sig, SIG_IGN);
- if (handler != SIG_ERR)
- signal(sig, handler);
- return handler;
+ handler = signal(sig, SIG_IGN);
+ if (handler != SIG_ERR)
+ signal(sig, handler);
+ return handler;
#endif
}
@@ -2237,24 +2237,24 @@ PyOS_sighandler_t
PyOS_setsig(int sig, PyOS_sighandler_t handler)
{
#ifdef HAVE_SIGACTION
- /* Some code in Modules/signalmodule.c depends on sigaction() being
- * used here if HAVE_SIGACTION is defined. Fix that if this code
- * changes to invalidate that assumption.
- */
- struct sigaction context, ocontext;
- context.sa_handler = handler;
- sigemptyset(&context.sa_mask);
- context.sa_flags = 0;
- if (sigaction(sig, &context, &ocontext) == -1)
- return SIG_ERR;
- return ocontext.sa_handler;
+ /* Some code in Modules/signalmodule.c depends on sigaction() being
+ * used here if HAVE_SIGACTION is defined. Fix that if this code
+ * changes to invalidate that assumption.
+ */
+ struct sigaction context, ocontext;
+ context.sa_handler = handler;
+ sigemptyset(&context.sa_mask);
+ context.sa_flags = 0;
+ if (sigaction(sig, &context, &ocontext) == -1)
+ return SIG_ERR;
+ return ocontext.sa_handler;
#else
- PyOS_sighandler_t oldhandler;
- oldhandler = signal(sig, handler);
+ PyOS_sighandler_t oldhandler;
+ oldhandler = signal(sig, handler);
#ifdef HAVE_SIGINTERRUPT
- siginterrupt(sig, 1);
+ siginterrupt(sig, 1);
#endif
- return oldhandler;
+ return oldhandler;
#endif
}
@@ -2264,71 +2264,71 @@ PyOS_setsig(int sig, PyOS_sighandler_t handler)
PyAPI_FUNC(node *)
PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
{
- return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
+ return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
}
#undef PyParser_SimpleParseString
PyAPI_FUNC(node *)
PyParser_SimpleParseString(const char *str, int start)
{
- return PyParser_SimpleParseStringFlags(str, start, 0);
+ return PyParser_SimpleParseStringFlags(str, start, 0);
}
#undef PyRun_AnyFile
PyAPI_FUNC(int)
PyRun_AnyFile(FILE *fp, const char *name)
{
- return PyRun_AnyFileExFlags(fp, name, 0, NULL);
+ return PyRun_AnyFileExFlags(fp, name, 0, NULL);
}
#undef PyRun_AnyFileEx
PyAPI_FUNC(int)
PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
{
- return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
+ return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
}
#undef PyRun_AnyFileFlags
PyAPI_FUNC(int)
PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
{
- return PyRun_AnyFileExFlags(fp, name, 0, flags);
+ return PyRun_AnyFileExFlags(fp, name, 0, flags);
}
#undef PyRun_File
PyAPI_FUNC(PyObject *)
PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
{
- return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
+ return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
}
#undef PyRun_FileEx
PyAPI_FUNC(PyObject *)
PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
{
- return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
+ return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
}
#undef PyRun_FileFlags
PyAPI_FUNC(PyObject *)
PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
- PyCompilerFlags *flags)
+ PyCompilerFlags *flags)
{
- return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
+ return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
}
#undef PyRun_SimpleFile
PyAPI_FUNC(int)
PyRun_SimpleFile(FILE *f, const char *p)
{
- return PyRun_SimpleFileExFlags(f, p, 0, NULL);
+ return PyRun_SimpleFileExFlags(f, p, 0, NULL);
}
#undef PyRun_SimpleFileEx
PyAPI_FUNC(int)
PyRun_SimpleFileEx(FILE *f, const char *p, int c)
{
- return PyRun_SimpleFileExFlags(f, p, c, NULL);
+ return PyRun_SimpleFileExFlags(f, p, c, NULL);
}
@@ -2336,35 +2336,35 @@ PyRun_SimpleFileEx(FILE *f, const char *p, int c)
PyAPI_FUNC(PyObject *)
PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
{
- return PyRun_StringFlags(str, s, g, l, NULL);
+ return PyRun_StringFlags(str, s, g, l, NULL);
}
#undef PyRun_SimpleString
PyAPI_FUNC(int)
PyRun_SimpleString(const char *s)
{
- return PyRun_SimpleStringFlags(s, NULL);
+ return PyRun_SimpleStringFlags(s, NULL);
}
#undef Py_CompileString
PyAPI_FUNC(PyObject *)
Py_CompileString(const char *str, const char *p, int s)
{
- return Py_CompileStringFlags(str, p, s, NULL);
+ return Py_CompileStringFlags(str, p, s, NULL);
}
#undef PyRun_InteractiveOne
PyAPI_FUNC(int)
PyRun_InteractiveOne(FILE *f, const char *p)
{
- return PyRun_InteractiveOneFlags(f, p, NULL);
+ return PyRun_InteractiveOneFlags(f, p, NULL);
}
#undef PyRun_InteractiveLoop
PyAPI_FUNC(int)
PyRun_InteractiveLoop(FILE *f, const char *p)
{
- return PyRun_InteractiveLoopFlags(f, p, NULL);
+ return PyRun_InteractiveLoopFlags(f, p, NULL);
}
#ifdef __cplusplus
diff --git a/Python/structmember.c b/Python/structmember.c
index 9109f23..ddedea5 100644
--- a/Python/structmember.c
+++ b/Python/structmember.c
@@ -8,293 +8,293 @@
PyObject *
PyMember_GetOne(const char *addr, PyMemberDef *l)
{
- PyObject *v;
+ PyObject *v;
- addr += l->offset;
- switch (l->type) {
- case T_BOOL:
- v = PyBool_FromLong(*(char*)addr);
- break;
- case T_BYTE:
- v = PyLong_FromLong(*(char*)addr);
- break;
- case T_UBYTE:
- v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
- break;
- case T_SHORT:
- v = PyLong_FromLong(*(short*)addr);
- break;
- case T_USHORT:
- v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
- break;
- case T_INT:
- v = PyLong_FromLong(*(int*)addr);
- break;
- case T_UINT:
- v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
- break;
- case T_LONG:
- v = PyLong_FromLong(*(long*)addr);
- break;
- case T_ULONG:
- v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
- break;
- case T_PYSSIZET:
- v = PyLong_FromSsize_t(*(Py_ssize_t*)addr);
- break;
- case T_FLOAT:
- v = PyFloat_FromDouble((double)*(float*)addr);
- break;
- case T_DOUBLE:
- v = PyFloat_FromDouble(*(double*)addr);
- break;
- case T_STRING:
- if (*(char**)addr == NULL) {
- Py_INCREF(Py_None);
- v = Py_None;
- }
- else
- v = PyUnicode_FromString(*(char**)addr);
- break;
- case T_STRING_INPLACE:
- v = PyUnicode_FromString((char*)addr);
- break;
- case T_CHAR:
- v = PyUnicode_FromStringAndSize((char*)addr, 1);
- break;
- case T_OBJECT:
- v = *(PyObject **)addr;
- if (v == NULL)
- v = Py_None;
- Py_INCREF(v);
- break;
- case T_OBJECT_EX:
- v = *(PyObject **)addr;
- if (v == NULL)
- PyErr_SetString(PyExc_AttributeError, l->name);
- Py_XINCREF(v);
- break;
+ addr += l->offset;
+ switch (l->type) {
+ case T_BOOL:
+ v = PyBool_FromLong(*(char*)addr);
+ break;
+ case T_BYTE:
+ v = PyLong_FromLong(*(char*)addr);
+ break;
+ case T_UBYTE:
+ v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
+ break;
+ case T_SHORT:
+ v = PyLong_FromLong(*(short*)addr);
+ break;
+ case T_USHORT:
+ v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
+ break;
+ case T_INT:
+ v = PyLong_FromLong(*(int*)addr);
+ break;
+ case T_UINT:
+ v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
+ break;
+ case T_LONG:
+ v = PyLong_FromLong(*(long*)addr);
+ break;
+ case T_ULONG:
+ v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
+ break;
+ case T_PYSSIZET:
+ v = PyLong_FromSsize_t(*(Py_ssize_t*)addr);
+ break;
+ case T_FLOAT:
+ v = PyFloat_FromDouble((double)*(float*)addr);
+ break;
+ case T_DOUBLE:
+ v = PyFloat_FromDouble(*(double*)addr);
+ break;
+ case T_STRING:
+ if (*(char**)addr == NULL) {
+ Py_INCREF(Py_None);
+ v = Py_None;
+ }
+ else
+ v = PyUnicode_FromString(*(char**)addr);
+ break;
+ case T_STRING_INPLACE:
+ v = PyUnicode_FromString((char*)addr);
+ break;
+ case T_CHAR:
+ v = PyUnicode_FromStringAndSize((char*)addr, 1);
+ break;
+ case T_OBJECT:
+ v = *(PyObject **)addr;
+ if (v == NULL)
+ v = Py_None;
+ Py_INCREF(v);
+ break;
+ case T_OBJECT_EX:
+ v = *(PyObject **)addr;
+ if (v == NULL)
+ PyErr_SetString(PyExc_AttributeError, l->name);
+ Py_XINCREF(v);
+ break;
#ifdef HAVE_LONG_LONG
- case T_LONGLONG:
- v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
- break;
- case T_ULONGLONG:
- v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
- break;
+ case T_LONGLONG:
+ v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
+ break;
+ case T_ULONGLONG:
+ v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
+ break;
#endif /* HAVE_LONG_LONG */
- case T_NONE:
- v = Py_None;
- Py_INCREF(v);
- break;
- default:
- PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
- v = NULL;
- }
- return v;
+ case T_NONE:
+ v = Py_None;
+ Py_INCREF(v);
+ break;
+ default:
+ PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
+ v = NULL;
+ }
+ return v;
}
-#define WARN(msg) \
- do { \
- if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) \
- return -1; \
+#define WARN(msg) \
+ do { \
+ if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) \
+ return -1; \
} while (0)
int
PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
{
- PyObject *oldv;
+ PyObject *oldv;
- addr += l->offset;
+ addr += l->offset;
- if ((l->flags & READONLY))
- {
- PyErr_SetString(PyExc_AttributeError, "readonly attribute");
- return -1;
- }
- if (v == NULL) {
- if (l->type == T_OBJECT_EX) {
- /* Check if the attribute is set. */
- if (*(PyObject **)addr == NULL) {
- PyErr_SetString(PyExc_AttributeError, l->name);
- return -1;
- }
- }
- else if (l->type != T_OBJECT) {
- PyErr_SetString(PyExc_TypeError,
- "can't delete numeric/char attribute");
- return -1;
- }
- }
- switch (l->type) {
- case T_BOOL:{
- if (!PyBool_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "attribute value type must be bool");
- return -1;
- }
- if (v == Py_True)
- *(char*)addr = (char) 1;
- else
- *(char*)addr = (char) 0;
- break;
- }
- case T_BYTE:{
- long long_val = PyLong_AsLong(v);
- if ((long_val == -1) && PyErr_Occurred())
- return -1;
- *(char*)addr = (char)long_val;
- /* XXX: For compatibility, only warn about truncations
- for now. */
- if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN))
- WARN("Truncation of value to char");
- break;
- }
- case T_UBYTE:{
- long long_val = PyLong_AsLong(v);
- if ((long_val == -1) && PyErr_Occurred())
- return -1;
- *(unsigned char*)addr = (unsigned char)long_val;
- if ((long_val > UCHAR_MAX) || (long_val < 0))
- WARN("Truncation of value to unsigned char");
- break;
- }
- case T_SHORT:{
- long long_val = PyLong_AsLong(v);
- if ((long_val == -1) && PyErr_Occurred())
- return -1;
- *(short*)addr = (short)long_val;
- if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
- WARN("Truncation of value to short");
- break;
- }
- case T_USHORT:{
- long long_val = PyLong_AsLong(v);
- if ((long_val == -1) && PyErr_Occurred())
- return -1;
- *(unsigned short*)addr = (unsigned short)long_val;
- if ((long_val > USHRT_MAX) || (long_val < 0))
- WARN("Truncation of value to unsigned short");
- break;
- }
- case T_INT:{
- long long_val = PyLong_AsLong(v);
- if ((long_val == -1) && PyErr_Occurred())
- return -1;
- *(int *)addr = (int)long_val;
- if ((long_val > INT_MAX) || (long_val < INT_MIN))
- WARN("Truncation of value to int");
- break;
- }
- case T_UINT:{
- unsigned long ulong_val = PyLong_AsUnsignedLong(v);
- if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) {
- /* XXX: For compatibility, accept negative int values
- as well. */
- PyErr_Clear();
- ulong_val = PyLong_AsLong(v);
- if ((ulong_val == (unsigned long)-1) &&
- PyErr_Occurred())
- return -1;
- *(unsigned int *)addr = (unsigned int)ulong_val;
- WARN("Writing negative value into unsigned field");
- } else
- *(unsigned int *)addr = (unsigned int)ulong_val;
- if (ulong_val > UINT_MAX)
- WARN("Truncation of value to unsigned int");
- break;
- }
- case T_LONG:{
- *(long*)addr = PyLong_AsLong(v);
- if ((*(long*)addr == -1) && PyErr_Occurred())
- return -1;
- break;
- }
- case T_ULONG:{
- *(unsigned long*)addr = PyLong_AsUnsignedLong(v);
- if ((*(unsigned long*)addr == (unsigned long)-1)
- && PyErr_Occurred()) {
- /* XXX: For compatibility, accept negative int values
- as well. */
- PyErr_Clear();
- *(unsigned long*)addr = PyLong_AsLong(v);
- if ((*(unsigned long*)addr == (unsigned long)-1)
- && PyErr_Occurred())
- return -1;
- WARN("Writing negative value into unsigned field");
- }
- break;
- }
- case T_PYSSIZET:{
- *(Py_ssize_t*)addr = PyLong_AsSsize_t(v);
- if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1)
- && PyErr_Occurred())
- return -1;
- break;
- }
- case T_FLOAT:{
- double double_val = PyFloat_AsDouble(v);
- if ((double_val == -1) && PyErr_Occurred())
- return -1;
- *(float*)addr = (float)double_val;
- break;
- }
- case T_DOUBLE:
- *(double*)addr = PyFloat_AsDouble(v);
- if ((*(double*)addr == -1) && PyErr_Occurred())
- return -1;
- break;
- case T_OBJECT:
- case T_OBJECT_EX:
- Py_XINCREF(v);
- oldv = *(PyObject **)addr;
- *(PyObject **)addr = v;
- Py_XDECREF(oldv);
- break;
- case T_CHAR: {
- char *string;
- Py_ssize_t len;
+ if ((l->flags & READONLY))
+ {
+ PyErr_SetString(PyExc_AttributeError, "readonly attribute");
+ return -1;
+ }
+ if (v == NULL) {
+ if (l->type == T_OBJECT_EX) {
+ /* Check if the attribute is set. */
+ if (*(PyObject **)addr == NULL) {
+ PyErr_SetString(PyExc_AttributeError, l->name);
+ return -1;
+ }
+ }
+ else if (l->type != T_OBJECT) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't delete numeric/char attribute");
+ return -1;
+ }
+ }
+ switch (l->type) {
+ case T_BOOL:{
+ if (!PyBool_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "attribute value type must be bool");
+ return -1;
+ }
+ if (v == Py_True)
+ *(char*)addr = (char) 1;
+ else
+ *(char*)addr = (char) 0;
+ break;
+ }
+ case T_BYTE:{
+ long long_val = PyLong_AsLong(v);
+ if ((long_val == -1) && PyErr_Occurred())
+ return -1;
+ *(char*)addr = (char)long_val;
+ /* XXX: For compatibility, only warn about truncations
+ for now. */
+ if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN))
+ WARN("Truncation of value to char");
+ break;
+ }
+ case T_UBYTE:{
+ long long_val = PyLong_AsLong(v);
+ if ((long_val == -1) && PyErr_Occurred())
+ return -1;
+ *(unsigned char*)addr = (unsigned char)long_val;
+ if ((long_val > UCHAR_MAX) || (long_val < 0))
+ WARN("Truncation of value to unsigned char");
+ break;
+ }
+ case T_SHORT:{
+ long long_val = PyLong_AsLong(v);
+ if ((long_val == -1) && PyErr_Occurred())
+ return -1;
+ *(short*)addr = (short)long_val;
+ if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
+ WARN("Truncation of value to short");
+ break;
+ }
+ case T_USHORT:{
+ long long_val = PyLong_AsLong(v);
+ if ((long_val == -1) && PyErr_Occurred())
+ return -1;
+ *(unsigned short*)addr = (unsigned short)long_val;
+ if ((long_val > USHRT_MAX) || (long_val < 0))
+ WARN("Truncation of value to unsigned short");
+ break;
+ }
+ case T_INT:{
+ long long_val = PyLong_AsLong(v);
+ if ((long_val == -1) && PyErr_Occurred())
+ return -1;
+ *(int *)addr = (int)long_val;
+ if ((long_val > INT_MAX) || (long_val < INT_MIN))
+ WARN("Truncation of value to int");
+ break;
+ }
+ case T_UINT:{
+ unsigned long ulong_val = PyLong_AsUnsignedLong(v);
+ if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) {
+ /* XXX: For compatibility, accept negative int values
+ as well. */
+ PyErr_Clear();
+ ulong_val = PyLong_AsLong(v);
+ if ((ulong_val == (unsigned long)-1) &&
+ PyErr_Occurred())
+ return -1;
+ *(unsigned int *)addr = (unsigned int)ulong_val;
+ WARN("Writing negative value into unsigned field");
+ } else
+ *(unsigned int *)addr = (unsigned int)ulong_val;
+ if (ulong_val > UINT_MAX)
+ WARN("Truncation of value to unsigned int");
+ break;
+ }
+ case T_LONG:{
+ *(long*)addr = PyLong_AsLong(v);
+ if ((*(long*)addr == -1) && PyErr_Occurred())
+ return -1;
+ break;
+ }
+ case T_ULONG:{
+ *(unsigned long*)addr = PyLong_AsUnsignedLong(v);
+ if ((*(unsigned long*)addr == (unsigned long)-1)
+ && PyErr_Occurred()) {
+ /* XXX: For compatibility, accept negative int values
+ as well. */
+ PyErr_Clear();
+ *(unsigned long*)addr = PyLong_AsLong(v);
+ if ((*(unsigned long*)addr == (unsigned long)-1)
+ && PyErr_Occurred())
+ return -1;
+ WARN("Writing negative value into unsigned field");
+ }
+ break;
+ }
+ case T_PYSSIZET:{
+ *(Py_ssize_t*)addr = PyLong_AsSsize_t(v);
+ if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1)
+ && PyErr_Occurred())
+ return -1;
+ break;
+ }
+ case T_FLOAT:{
+ double double_val = PyFloat_AsDouble(v);
+ if ((double_val == -1) && PyErr_Occurred())
+ return -1;
+ *(float*)addr = (float)double_val;
+ break;
+ }
+ case T_DOUBLE:
+ *(double*)addr = PyFloat_AsDouble(v);
+ if ((*(double*)addr == -1) && PyErr_Occurred())
+ return -1;
+ break;
+ case T_OBJECT:
+ case T_OBJECT_EX:
+ Py_XINCREF(v);
+ oldv = *(PyObject **)addr;
+ *(PyObject **)addr = v;
+ Py_XDECREF(oldv);
+ break;
+ case T_CHAR: {
+ char *string;
+ Py_ssize_t len;
- if (!PyUnicode_Check(v)) {
- PyErr_BadArgument();
- return -1;
- }
- string = _PyUnicode_AsStringAndSize(v, &len);
- if (len != 1) {
- PyErr_BadArgument();
- return -1;
- }
- *(char*)addr = string[0];
- break;
- }
- case T_STRING:
- case T_STRING_INPLACE:
- PyErr_SetString(PyExc_TypeError, "readonly attribute");
- return -1;
+ if (!PyUnicode_Check(v)) {
+ PyErr_BadArgument();
+ return -1;
+ }
+ string = _PyUnicode_AsStringAndSize(v, &len);
+ if (len != 1) {
+ PyErr_BadArgument();
+ return -1;
+ }
+ *(char*)addr = string[0];
+ break;
+ }
+ case T_STRING:
+ case T_STRING_INPLACE:
+ PyErr_SetString(PyExc_TypeError, "readonly attribute");
+ return -1;
#ifdef HAVE_LONG_LONG
- case T_LONGLONG:{
- PY_LONG_LONG value;
- *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
- if ((value == -1) && PyErr_Occurred())
- return -1;
- break;
- }
- case T_ULONGLONG:{
- unsigned PY_LONG_LONG value;
- /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
- doesn't ??? */
- if (PyLong_Check(v))
- *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
- else
- *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsLong(v);
- if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred())
- return -1;
- break;
- }
+ case T_LONGLONG:{
+ PY_LONG_LONG value;
+ *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
+ if ((value == -1) && PyErr_Occurred())
+ return -1;
+ break;
+ }
+ case T_ULONGLONG:{
+ unsigned PY_LONG_LONG value;
+ /* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
+ doesn't ??? */
+ if (PyLong_Check(v))
+ *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
+ else
+ *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsLong(v);
+ if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred())
+ return -1;
+ break;
+ }
#endif /* HAVE_LONG_LONG */
- default:
- PyErr_Format(PyExc_SystemError,
- "bad memberdescr type for %s", l->name);
- return -1;
- }
- return 0;
+ default:
+ PyErr_Format(PyExc_SystemError,
+ "bad memberdescr type for %s", l->name);
+ return -1;
+ }
+ return 0;
}
diff --git a/Python/symtable.c b/Python/symtable.c
index 9619f75..0cbde7d 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -25,147 +25,147 @@
static PySTEntryObject *
ste_new(struct symtable *st, identifier name, _Py_block_ty block,
- void *key, int lineno)
+ void *key, int lineno)
{
- PySTEntryObject *ste = NULL;
- PyObject *k;
-
- k = PyLong_FromVoidPtr(key);
- if (k == NULL)
- goto fail;
- ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
- if (ste == NULL)
- goto fail;
- ste->ste_table = st;
- ste->ste_id = k;
- ste->ste_tmpname = 0;
-
- ste->ste_name = name;
- Py_INCREF(name);
-
- ste->ste_symbols = NULL;
- ste->ste_varnames = NULL;
- ste->ste_children = NULL;
-
- ste->ste_symbols = PyDict_New();
- if (ste->ste_symbols == NULL)
- goto fail;
-
- ste->ste_varnames = PyList_New(0);
- if (ste->ste_varnames == NULL)
- goto fail;
-
- ste->ste_children = PyList_New(0);
- if (ste->ste_children == NULL)
- goto fail;
-
- ste->ste_type = block;
- ste->ste_unoptimized = 0;
- ste->ste_nested = 0;
- ste->ste_free = 0;
- ste->ste_varargs = 0;
- ste->ste_varkeywords = 0;
- ste->ste_opt_lineno = 0;
- ste->ste_tmpname = 0;
- ste->ste_lineno = lineno;
-
- if (st->st_cur != NULL &&
- (st->st_cur->ste_nested ||
- st->st_cur->ste_type == FunctionBlock))
- ste->ste_nested = 1;
- ste->ste_child_free = 0;
- ste->ste_generator = 0;
- ste->ste_returns_value = 0;
-
- if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
- goto fail;
-
- return ste;
+ PySTEntryObject *ste = NULL;
+ PyObject *k;
+
+ k = PyLong_FromVoidPtr(key);
+ if (k == NULL)
+ goto fail;
+ ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
+ if (ste == NULL)
+ goto fail;
+ ste->ste_table = st;
+ ste->ste_id = k;
+ ste->ste_tmpname = 0;
+
+ ste->ste_name = name;
+ Py_INCREF(name);
+
+ ste->ste_symbols = NULL;
+ ste->ste_varnames = NULL;
+ ste->ste_children = NULL;
+
+ ste->ste_symbols = PyDict_New();
+ if (ste->ste_symbols == NULL)
+ goto fail;
+
+ ste->ste_varnames = PyList_New(0);
+ if (ste->ste_varnames == NULL)
+ goto fail;
+
+ ste->ste_children = PyList_New(0);
+ if (ste->ste_children == NULL)
+ goto fail;
+
+ ste->ste_type = block;
+ ste->ste_unoptimized = 0;
+ ste->ste_nested = 0;
+ ste->ste_free = 0;
+ ste->ste_varargs = 0;
+ ste->ste_varkeywords = 0;
+ ste->ste_opt_lineno = 0;
+ ste->ste_tmpname = 0;
+ ste->ste_lineno = lineno;
+
+ if (st->st_cur != NULL &&
+ (st->st_cur->ste_nested ||
+ st->st_cur->ste_type == FunctionBlock))
+ ste->ste_nested = 1;
+ ste->ste_child_free = 0;
+ ste->ste_generator = 0;
+ ste->ste_returns_value = 0;
+
+ if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
+ goto fail;
+
+ return ste;
fail:
- Py_XDECREF(ste);
- return NULL;
+ Py_XDECREF(ste);
+ return NULL;
}
static PyObject *
ste_repr(PySTEntryObject *ste)
{
- return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
- ste->ste_name,
- PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
+ return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
+ ste->ste_name,
+ PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
}
static void
ste_dealloc(PySTEntryObject *ste)
{
- ste->ste_table = NULL;
- Py_XDECREF(ste->ste_id);
- Py_XDECREF(ste->ste_name);
- Py_XDECREF(ste->ste_symbols);
- Py_XDECREF(ste->ste_varnames);
- Py_XDECREF(ste->ste_children);
- PyObject_Del(ste);
+ ste->ste_table = NULL;
+ Py_XDECREF(ste->ste_id);
+ Py_XDECREF(ste->ste_name);
+ Py_XDECREF(ste->ste_symbols);
+ Py_XDECREF(ste->ste_varnames);
+ Py_XDECREF(ste->ste_children);
+ PyObject_Del(ste);
}
#define OFF(x) offsetof(PySTEntryObject, x)
static PyMemberDef ste_memberlist[] = {
- {"id", T_OBJECT, OFF(ste_id), READONLY},
- {"name", T_OBJECT, OFF(ste_name), READONLY},
- {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
- {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
- {"children", T_OBJECT, OFF(ste_children), READONLY},
- {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
- {"nested", T_INT, OFF(ste_nested), READONLY},
- {"type", T_INT, OFF(ste_type), READONLY},
- {"lineno", T_INT, OFF(ste_lineno), READONLY},
- {NULL}
+ {"id", T_OBJECT, OFF(ste_id), READONLY},
+ {"name", T_OBJECT, OFF(ste_name), READONLY},
+ {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
+ {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
+ {"children", T_OBJECT, OFF(ste_children), READONLY},
+ {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
+ {"nested", T_INT, OFF(ste_nested), READONLY},
+ {"type", T_INT, OFF(ste_type), READONLY},
+ {"lineno", T_INT, OFF(ste_lineno), READONLY},
+ {NULL}
};
PyTypeObject PySTEntry_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "symtable entry",
- sizeof(PySTEntryObject),
- 0,
- (destructor)ste_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- (reprfunc)ste_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- ste_memberlist, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- 0, /* tp_new */
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ "symtable entry",
+ sizeof(PySTEntryObject),
+ 0,
+ (destructor)ste_dealloc, /* tp_dealloc */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ (reprfunc)ste_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ 0, /* tp_doc */
+ 0, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ 0, /* tp_iter */
+ 0, /* tp_iternext */
+ 0, /* tp_methods */
+ ste_memberlist, /* tp_members */
+ 0, /* tp_getset */
+ 0, /* tp_base */
+ 0, /* tp_dict */
+ 0, /* tp_descr_get */
+ 0, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
};
static int symtable_analyze(struct symtable *st);
static int symtable_warn(struct symtable *st, char *msg, int lineno);
-static int symtable_enter_block(struct symtable *st, identifier name,
- _Py_block_ty block, void *ast, int lineno);
+static int symtable_enter_block(struct symtable *st, identifier name,
+ _Py_block_ty block, void *ast, int lineno);
static int symtable_exit_block(struct symtable *st, void *ast);
static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
static int symtable_visit_expr(struct symtable *st, expr_ty s);
@@ -186,11 +186,11 @@ static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
static identifier top = NULL, lambda = NULL, genexpr = NULL,
- listcomp = NULL, setcomp = NULL, dictcomp = NULL,
- __class__ = NULL, __locals__ = NULL;
+ listcomp = NULL, setcomp = NULL, dictcomp = NULL,
+ __class__ = NULL, __locals__ = NULL;
#define GET_IDENTIFIER(VAR) \
- ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
+ ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
#define DUPLICATE_ARGUMENT \
"duplicate argument '%U' in function definition"
@@ -198,135 +198,135 @@ static identifier top = NULL, lambda = NULL, genexpr = NULL,
static struct symtable *
symtable_new(void)
{
- struct symtable *st;
-
- st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
- if (st == NULL)
- return NULL;
-
- st->st_filename = NULL;
- st->st_blocks = NULL;
-
- if ((st->st_stack = PyList_New(0)) == NULL)
- goto fail;
- if ((st->st_blocks = PyDict_New()) == NULL)
- goto fail;
- st->st_cur = NULL;
- st->st_private = NULL;
- return st;
+ struct symtable *st;
+
+ st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
+ if (st == NULL)
+ return NULL;
+
+ st->st_filename = NULL;
+ st->st_blocks = NULL;
+
+ if ((st->st_stack = PyList_New(0)) == NULL)
+ goto fail;
+ if ((st->st_blocks = PyDict_New()) == NULL)
+ goto fail;
+ st->st_cur = NULL;
+ st->st_private = NULL;
+ return st;
fail:
- PySymtable_Free(st);
- return NULL;
+ PySymtable_Free(st);
+ return NULL;
}
struct symtable *
PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
{
- struct symtable *st = symtable_new();
- asdl_seq *seq;
- int i;
-
- if (st == NULL)
- return st;
- st->st_filename = filename;
- st->st_future = future;
- /* Make the initial symbol information gathering pass */
- if (!GET_IDENTIFIER(top) ||
- !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
- PySymtable_Free(st);
- return NULL;
- }
-
- st->st_top = st->st_cur;
- st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
- switch (mod->kind) {
- case Module_kind:
- seq = mod->v.Module.body;
- for (i = 0; i < asdl_seq_LEN(seq); i++)
- if (!symtable_visit_stmt(st,
- (stmt_ty)asdl_seq_GET(seq, i)))
- goto error;
- break;
- case Expression_kind:
- if (!symtable_visit_expr(st, mod->v.Expression.body))
- goto error;
- break;
- case Interactive_kind:
- seq = mod->v.Interactive.body;
- for (i = 0; i < asdl_seq_LEN(seq); i++)
- if (!symtable_visit_stmt(st,
- (stmt_ty)asdl_seq_GET(seq, i)))
- goto error;
- break;
- case Suite_kind:
- PyErr_SetString(PyExc_RuntimeError,
- "this compiler does not handle Suites");
- goto error;
- }
- if (!symtable_exit_block(st, (void *)mod)) {
- PySymtable_Free(st);
- return NULL;
- }
- /* Make the second symbol analysis pass */
- if (symtable_analyze(st))
- return st;
- PySymtable_Free(st);
- return NULL;
+ struct symtable *st = symtable_new();
+ asdl_seq *seq;
+ int i;
+
+ if (st == NULL)
+ return st;
+ st->st_filename = filename;
+ st->st_future = future;
+ /* Make the initial symbol information gathering pass */
+ if (!GET_IDENTIFIER(top) ||
+ !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
+ PySymtable_Free(st);
+ return NULL;
+ }
+
+ st->st_top = st->st_cur;
+ st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
+ switch (mod->kind) {
+ case Module_kind:
+ seq = mod->v.Module.body;
+ for (i = 0; i < asdl_seq_LEN(seq); i++)
+ if (!symtable_visit_stmt(st,
+ (stmt_ty)asdl_seq_GET(seq, i)))
+ goto error;
+ break;
+ case Expression_kind:
+ if (!symtable_visit_expr(st, mod->v.Expression.body))
+ goto error;
+ break;
+ case Interactive_kind:
+ seq = mod->v.Interactive.body;
+ for (i = 0; i < asdl_seq_LEN(seq); i++)
+ if (!symtable_visit_stmt(st,
+ (stmt_ty)asdl_seq_GET(seq, i)))
+ goto error;
+ break;
+ case Suite_kind:
+ PyErr_SetString(PyExc_RuntimeError,
+ "this compiler does not handle Suites");
+ goto error;
+ }
+ if (!symtable_exit_block(st, (void *)mod)) {
+ PySymtable_Free(st);
+ return NULL;
+ }
+ /* Make the second symbol analysis pass */
+ if (symtable_analyze(st))
+ return st;
+ PySymtable_Free(st);
+ return NULL;
error:
- (void) symtable_exit_block(st, (void *)mod);
- PySymtable_Free(st);
- return NULL;
+ (void) symtable_exit_block(st, (void *)mod);
+ PySymtable_Free(st);
+ return NULL;
}
void
PySymtable_Free(struct symtable *st)
{
- Py_XDECREF(st->st_blocks);
- Py_XDECREF(st->st_stack);
- PyMem_Free((void *)st);
+ Py_XDECREF(st->st_blocks);
+ Py_XDECREF(st->st_stack);
+ PyMem_Free((void *)st);
}
PySTEntryObject *
PySymtable_Lookup(struct symtable *st, void *key)
{
- PyObject *k, *v;
-
- k = PyLong_FromVoidPtr(key);
- if (k == NULL)
- return NULL;
- v = PyDict_GetItem(st->st_blocks, k);
- if (v) {
- assert(PySTEntry_Check(v));
- Py_INCREF(v);
- }
- else {
- PyErr_SetString(PyExc_KeyError,
- "unknown symbol table entry");
- }
-
- Py_DECREF(k);
- return (PySTEntryObject *)v;
+ PyObject *k, *v;
+
+ k = PyLong_FromVoidPtr(key);
+ if (k == NULL)
+ return NULL;
+ v = PyDict_GetItem(st->st_blocks, k);
+ if (v) {
+ assert(PySTEntry_Check(v));
+ Py_INCREF(v);
+ }
+ else {
+ PyErr_SetString(PyExc_KeyError,
+ "unknown symbol table entry");
+ }
+
+ Py_DECREF(k);
+ return (PySTEntryObject *)v;
}
-int
+int
PyST_GetScope(PySTEntryObject *ste, PyObject *name)
{
- PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
- if (!v)
- return 0;
- assert(PyLong_Check(v));
- return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
+ PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
+ if (!v)
+ return 0;
+ assert(PyLong_Check(v));
+ return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
}
/* Analyze raw symbol information to determine scope of each name.
The next several functions are helpers for symtable_analyze(),
- which determines whether a name is local, global, or free. In addition,
+ which determines whether a name is local, global, or free. In addition,
it determines which local variables are cell variables; they provide
- bindings that are used for free variables in enclosed blocks.
+ bindings that are used for free variables in enclosed blocks.
- There are also two kinds of global variables, implicit and explicit. An
+ There are also two kinds of global variables, implicit and explicit. An
explicit global is declared with the global statement. An implicit
global is a free variable for which the compiler has found no binding
in an enclosing function scope. The implicit global is either a global
@@ -342,7 +342,7 @@ PyST_GetScope(PySTEntryObject *ste, PyObject *name)
PySTEntryObjects created during pass 1.
When a function is entered during the second pass, the parent passes
- the set of all name bindings visible to its children. These bindings
+ the set of all name bindings visible to its children. These bindings
are used to determine if non-local variables are free or implicit globals.
Names which are explicitly declared nonlocal must exist in this set of
visible names - if they do not, a syntax error is raised. After doing
@@ -365,14 +365,14 @@ PyST_GetScope(PySTEntryObject *ste, PyObject *name)
*/
#define SET_SCOPE(DICT, NAME, I) { \
- PyObject *o = PyLong_FromLong(I); \
- if (!o) \
- return 0; \
- if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
- Py_DECREF(o); \
- return 0; \
- } \
- Py_DECREF(o); \
+ PyObject *o = PyLong_FromLong(I); \
+ if (!o) \
+ return 0; \
+ if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
+ Py_DECREF(o); \
+ return 0; \
+ } \
+ Py_DECREF(o); \
}
/* Decide on scope of name, given flags.
@@ -382,86 +382,86 @@ PyST_GetScope(PySTEntryObject *ste, PyObject *name)
global. A name that was global can be changed to local.
*/
-static int
+static int
analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
- PyObject *bound, PyObject *local, PyObject *free,
- PyObject *global)
+ PyObject *bound, PyObject *local, PyObject *free,
+ PyObject *global)
{
- if (flags & DEF_GLOBAL) {
- if (flags & DEF_PARAM) {
- PyErr_Format(PyExc_SyntaxError,
- "name '%U' is parameter and global",
- name);
- PyErr_SyntaxLocation(ste->ste_table->st_filename,
- ste->ste_lineno);
-
- return 0;
- }
- if (flags & DEF_NONLOCAL) {
- PyErr_Format(PyExc_SyntaxError,
- "name '%U' is nonlocal and global",
- name);
- return 0;
- }
- SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
- if (PySet_Add(global, name) < 0)
- return 0;
- if (bound && (PySet_Discard(bound, name) < 0))
- return 0;
- return 1;
- }
+ if (flags & DEF_GLOBAL) {
+ if (flags & DEF_PARAM) {
+ PyErr_Format(PyExc_SyntaxError,
+ "name '%U' is parameter and global",
+ name);
+ PyErr_SyntaxLocation(ste->ste_table->st_filename,
+ ste->ste_lineno);
+
+ return 0;
+ }
if (flags & DEF_NONLOCAL) {
- if (flags & DEF_PARAM) {
- PyErr_Format(PyExc_SyntaxError,
- "name '%U' is parameter and nonlocal",
- name);
- return 0;
- }
- if (!bound) {
- PyErr_Format(PyExc_SyntaxError,
- "nonlocal declaration not allowed at module level");
- return 0;
- }
- if (!PySet_Contains(bound, name)) {
- PyErr_Format(PyExc_SyntaxError,
- "no binding for nonlocal '%U' found",
- name);
-
- return 0;
- }
- SET_SCOPE(scopes, name, FREE);
- ste->ste_free = 1;
- return PySet_Add(free, name) >= 0;
+ PyErr_Format(PyExc_SyntaxError,
+ "name '%U' is nonlocal and global",
+ name);
+ return 0;
+ }
+ SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
+ if (PySet_Add(global, name) < 0)
+ return 0;
+ if (bound && (PySet_Discard(bound, name) < 0))
+ return 0;
+ return 1;
+ }
+ if (flags & DEF_NONLOCAL) {
+ if (flags & DEF_PARAM) {
+ PyErr_Format(PyExc_SyntaxError,
+ "name '%U' is parameter and nonlocal",
+ name);
+ return 0;
}
- if (flags & DEF_BOUND) {
- SET_SCOPE(scopes, name, LOCAL);
- if (PySet_Add(local, name) < 0)
- return 0;
- if (PySet_Discard(global, name) < 0)
- return 0;
- return 1;
- }
- /* If an enclosing block has a binding for this name, it
- is a free variable rather than a global variable.
- Note that having a non-NULL bound implies that the block
- is nested.
- */
- if (bound && PySet_Contains(bound, name)) {
- SET_SCOPE(scopes, name, FREE);
- ste->ste_free = 1;
- return PySet_Add(free, name) >= 0;
- }
- /* If a parent has a global statement, then call it global
- explicit? It could also be global implicit.
- */
- if (global && PySet_Contains(global, name)) {
- SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
- return 1;
- }
- if (ste->ste_nested)
- ste->ste_free = 1;
- SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
- return 1;
+ if (!bound) {
+ PyErr_Format(PyExc_SyntaxError,
+ "nonlocal declaration not allowed at module level");
+ return 0;
+ }
+ if (!PySet_Contains(bound, name)) {
+ PyErr_Format(PyExc_SyntaxError,
+ "no binding for nonlocal '%U' found",
+ name);
+
+ return 0;
+ }
+ SET_SCOPE(scopes, name, FREE);
+ ste->ste_free = 1;
+ return PySet_Add(free, name) >= 0;
+ }
+ if (flags & DEF_BOUND) {
+ SET_SCOPE(scopes, name, LOCAL);
+ if (PySet_Add(local, name) < 0)
+ return 0;
+ if (PySet_Discard(global, name) < 0)
+ return 0;
+ return 1;
+ }
+ /* If an enclosing block has a binding for this name, it
+ is a free variable rather than a global variable.
+ Note that having a non-NULL bound implies that the block
+ is nested.
+ */
+ if (bound && PySet_Contains(bound, name)) {
+ SET_SCOPE(scopes, name, FREE);
+ ste->ste_free = 1;
+ return PySet_Add(free, name) >= 0;
+ }
+ /* If a parent has a global statement, then call it global
+ explicit? It could also be global implicit.
+ */
+ if (global && PySet_Contains(global, name)) {
+ SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
+ return 1;
+ }
+ if (ste->ste_nested)
+ ste->ste_free = 1;
+ SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
+ return 1;
}
#undef SET_SCOPE
@@ -480,153 +480,153 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
static int
analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
{
- PyObject *name, *v, *v_cell;
- int success = 0;
- Py_ssize_t pos = 0;
-
- v_cell = PyLong_FromLong(CELL);
- if (!v_cell)
- return 0;
- while (PyDict_Next(scopes, &pos, &name, &v)) {
- long scope;
- assert(PyLong_Check(v));
- scope = PyLong_AS_LONG(v);
- if (scope != LOCAL)
- continue;
- if (!PySet_Contains(free, name))
- continue;
- if (restricted != NULL &&
- PyUnicode_CompareWithASCIIString(name, restricted))
- continue;
- /* Replace LOCAL with CELL for this name, and remove
- from free. It is safe to replace the value of name
- in the dict, because it will not cause a resize.
- */
- if (PyDict_SetItem(scopes, name, v_cell) < 0)
- goto error;
- if (PySet_Discard(free, name) < 0)
- goto error;
- }
- success = 1;
+ PyObject *name, *v, *v_cell;
+ int success = 0;
+ Py_ssize_t pos = 0;
+
+ v_cell = PyLong_FromLong(CELL);
+ if (!v_cell)
+ return 0;
+ while (PyDict_Next(scopes, &pos, &name, &v)) {
+ long scope;
+ assert(PyLong_Check(v));
+ scope = PyLong_AS_LONG(v);
+ if (scope != LOCAL)
+ continue;
+ if (!PySet_Contains(free, name))
+ continue;
+ if (restricted != NULL &&
+ PyUnicode_CompareWithASCIIString(name, restricted))
+ continue;
+ /* Replace LOCAL with CELL for this name, and remove
+ from free. It is safe to replace the value of name
+ in the dict, because it will not cause a resize.
+ */
+ if (PyDict_SetItem(scopes, name, v_cell) < 0)
+ goto error;
+ if (PySet_Discard(free, name) < 0)
+ goto error;
+ }
+ success = 1;
error:
- Py_DECREF(v_cell);
- return success;
+ Py_DECREF(v_cell);
+ return success;
}
/* Check for illegal statements in unoptimized namespaces */
static int
check_unoptimized(const PySTEntryObject* ste) {
- const char* trailer;
-
- if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
- || !(ste->ste_free || ste->ste_child_free))
- return 1;
-
- trailer = (ste->ste_child_free ?
- "contains a nested function with free variables" :
- "is a nested function");
-
- switch (ste->ste_unoptimized) {
- case OPT_TOPLEVEL: /* import * at top-level is fine */
- return 1;
- case OPT_IMPORT_STAR:
- PyErr_Format(PyExc_SyntaxError,
- "import * is not allowed in function '%U' because it %s",
- ste->ste_name, trailer);
- break;
- }
-
- PyErr_SyntaxLocation(ste->ste_table->st_filename,
- ste->ste_opt_lineno);
- return 0;
+ const char* trailer;
+
+ if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
+ || !(ste->ste_free || ste->ste_child_free))
+ return 1;
+
+ trailer = (ste->ste_child_free ?
+ "contains a nested function with free variables" :
+ "is a nested function");
+
+ switch (ste->ste_unoptimized) {
+ case OPT_TOPLEVEL: /* import * at top-level is fine */
+ return 1;
+ case OPT_IMPORT_STAR:
+ PyErr_Format(PyExc_SyntaxError,
+ "import * is not allowed in function '%U' because it %s",
+ ste->ste_name, trailer);
+ break;
+ }
+
+ PyErr_SyntaxLocation(ste->ste_table->st_filename,
+ ste->ste_opt_lineno);
+ return 0;
}
-/* Enter the final scope information into the ste_symbols dict.
- *
+/* Enter the final scope information into the ste_symbols dict.
+ *
* All arguments are dicts. Modifies symbols, others are read-only.
*/
static int
-update_symbols(PyObject *symbols, PyObject *scopes,
+update_symbols(PyObject *symbols, PyObject *scopes,
PyObject *bound, PyObject *free, int classflag)
{
- PyObject *name = NULL, *itr = NULL;
- PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
- Py_ssize_t pos = 0;
-
- /* Update scope information for all symbols in this scope */
- while (PyDict_Next(symbols, &pos, &name, &v)) {
- long scope, flags;
- assert(PyLong_Check(v));
- flags = PyLong_AS_LONG(v);
- v_scope = PyDict_GetItem(scopes, name);
- assert(v_scope && PyLong_Check(v_scope));
- scope = PyLong_AS_LONG(v_scope);
- flags |= (scope << SCOPE_OFFSET);
- v_new = PyLong_FromLong(flags);
- if (!v_new)
- return 0;
- if (PyDict_SetItem(symbols, name, v_new) < 0) {
- Py_DECREF(v_new);
- return 0;
- }
- Py_DECREF(v_new);
- }
-
- /* Record not yet resolved free variables from children (if any) */
- v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
- if (!v_free)
- return 0;
-
- itr = PyObject_GetIter(free);
- if (!itr)
- goto error;
-
- while ((name = PyIter_Next(itr))) {
- v = PyDict_GetItem(symbols, name);
-
- /* Handle symbol that already exists in this scope */
- if (v) {
- /* Handle a free variable in a method of
- the class that has the same name as a local
- or global in the class scope.
- */
- if (classflag &&
- PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
- long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
- v_new = PyLong_FromLong(flags);
- if (!v_new) {
- goto error;
- }
- if (PyDict_SetItem(symbols, name, v_new) < 0) {
- Py_DECREF(v_new);
- goto error;
- }
- Py_DECREF(v_new);
- }
- /* It's a cell, or already free in this scope */
- Py_DECREF(name);
- continue;
- }
- /* Handle global symbol */
- if (!PySet_Contains(bound, name)) {
- Py_DECREF(name);
- continue; /* it's a global */
- }
- /* Propagate new free symbol up the lexical stack */
- if (PyDict_SetItem(symbols, name, v_free) < 0) {
- goto error;
- }
- Py_DECREF(name);
+ PyObject *name = NULL, *itr = NULL;
+ PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
+ Py_ssize_t pos = 0;
+
+ /* Update scope information for all symbols in this scope */
+ while (PyDict_Next(symbols, &pos, &name, &v)) {
+ long scope, flags;
+ assert(PyLong_Check(v));
+ flags = PyLong_AS_LONG(v);
+ v_scope = PyDict_GetItem(scopes, name);
+ assert(v_scope && PyLong_Check(v_scope));
+ scope = PyLong_AS_LONG(v_scope);
+ flags |= (scope << SCOPE_OFFSET);
+ v_new = PyLong_FromLong(flags);
+ if (!v_new)
+ return 0;
+ if (PyDict_SetItem(symbols, name, v_new) < 0) {
+ Py_DECREF(v_new);
+ return 0;
}
- Py_DECREF(itr);
- Py_DECREF(v_free);
- return 1;
+ Py_DECREF(v_new);
+ }
+
+ /* Record not yet resolved free variables from children (if any) */
+ v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
+ if (!v_free)
+ return 0;
+
+ itr = PyObject_GetIter(free);
+ if (!itr)
+ goto error;
+
+ while ((name = PyIter_Next(itr))) {
+ v = PyDict_GetItem(symbols, name);
+
+ /* Handle symbol that already exists in this scope */
+ if (v) {
+ /* Handle a free variable in a method of
+ the class that has the same name as a local
+ or global in the class scope.
+ */
+ if (classflag &&
+ PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
+ long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
+ v_new = PyLong_FromLong(flags);
+ if (!v_new) {
+ goto error;
+ }
+ if (PyDict_SetItem(symbols, name, v_new) < 0) {
+ Py_DECREF(v_new);
+ goto error;
+ }
+ Py_DECREF(v_new);
+ }
+ /* It's a cell, or already free in this scope */
+ Py_DECREF(name);
+ continue;
+ }
+ /* Handle global symbol */
+ if (!PySet_Contains(bound, name)) {
+ Py_DECREF(name);
+ continue; /* it's a global */
+ }
+ /* Propagate new free symbol up the lexical stack */
+ if (PyDict_SetItem(symbols, name, v_free) < 0) {
+ goto error;
+ }
+ Py_DECREF(name);
+ }
+ Py_DECREF(itr);
+ Py_DECREF(v_free);
+ return 1;
error:
- Py_XDECREF(v_free);
- Py_XDECREF(itr);
- Py_XDECREF(name);
- return 0;
-}
+ Py_XDECREF(v_free);
+ Py_XDECREF(itr);
+ Py_XDECREF(name);
+ return 0;
+}
/* Make final symbol table decisions for block of ste.
@@ -649,238 +649,238 @@ error:
*/
static int
-analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
- PyObject *global, PyObject* child_free);
+analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
+ PyObject *global, PyObject* child_free);
static int
-analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
- PyObject *global)
+analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
+ PyObject *global)
{
- PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
- PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
- PyObject *temp;
- int i, success = 0;
- Py_ssize_t pos = 0;
-
- local = PySet_New(NULL); /* collect new names bound in block */
- if (!local)
- goto error;
- scopes = PyDict_New(); /* collect scopes defined for each name */
- if (!scopes)
- goto error;
-
- /* Allocate new global and bound variable dictionaries. These
- dictionaries hold the names visible in nested blocks. For
- ClassBlocks, the bound and global names are initialized
- before analyzing names, because class bindings aren't
- visible in methods. For other blocks, they are initialized
- after names are analyzed.
- */
-
- /* TODO(jhylton): Package these dicts in a struct so that we
- can write reasonable helper functions?
- */
- newglobal = PySet_New(NULL);
- if (!newglobal)
- goto error;
- newfree = PySet_New(NULL);
- if (!newfree)
- goto error;
- newbound = PySet_New(NULL);
- if (!newbound)
- goto error;
-
- /* Class namespace has no effect on names visible in
- nested functions, so populate the global and bound
- sets to be passed to child blocks before analyzing
- this one.
- */
- if (ste->ste_type == ClassBlock) {
- /* Pass down known globals */
- temp = PyNumber_InPlaceOr(newglobal, global);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- /* Pass down previously bound symbols */
- if (bound) {
- temp = PyNumber_InPlaceOr(newbound, bound);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- }
- }
-
- while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
- long flags = PyLong_AS_LONG(v);
- if (!analyze_name(ste, scopes, name, flags,
- bound, local, free, global))
- goto error;
- }
-
- /* Populate global and bound sets to be passed to children. */
- if (ste->ste_type != ClassBlock) {
- /* Add function locals to bound set */
- if (ste->ste_type == FunctionBlock) {
- temp = PyNumber_InPlaceOr(newbound, local);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- }
- /* Pass down previously bound symbols */
- if (bound) {
- temp = PyNumber_InPlaceOr(newbound, bound);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- }
- /* Pass down known globals */
- temp = PyNumber_InPlaceOr(newglobal, global);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- }
- else {
- /* Special-case __class__ */
- if (!GET_IDENTIFIER(__class__))
- goto error;
- assert(PySet_Contains(local, __class__) == 1);
- if (PySet_Add(newbound, __class__) < 0)
- goto error;
- }
-
- /* Recursively call analyze_block() on each child block.
-
- newbound, newglobal now contain the names visible in
- nested blocks. The free variables in the children will
- be collected in allfree.
- */
- allfree = PySet_New(NULL);
- if (!allfree)
- goto error;
- for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
- PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
- PySTEntryObject* entry;
- assert(c && PySTEntry_Check(c));
- entry = (PySTEntryObject*)c;
- if (!analyze_child_block(entry, newbound, newfree, newglobal,
- allfree))
- goto error;
- /* Check if any children have free variables */
- if (entry->ste_free || entry->ste_child_free)
- ste->ste_child_free = 1;
- }
-
- temp = PyNumber_InPlaceOr(newfree, allfree);
- if (!temp)
- goto error;
- Py_DECREF(temp);
-
- /* Check if any local variables must be converted to cell variables */
- if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
- NULL))
- goto error;
- else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
- "__class__"))
- goto error;
- /* Records the results of the analysis in the symbol table entry */
- if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
- ste->ste_type == ClassBlock))
- goto error;
- if (!check_unoptimized(ste))
- goto error;
-
- temp = PyNumber_InPlaceOr(free, newfree);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- success = 1;
+ PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
+ PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
+ PyObject *temp;
+ int i, success = 0;
+ Py_ssize_t pos = 0;
+
+ local = PySet_New(NULL); /* collect new names bound in block */
+ if (!local)
+ goto error;
+ scopes = PyDict_New(); /* collect scopes defined for each name */
+ if (!scopes)
+ goto error;
+
+ /* Allocate new global and bound variable dictionaries. These
+ dictionaries hold the names visible in nested blocks. For
+ ClassBlocks, the bound and global names are initialized
+ before analyzing names, because class bindings aren't
+ visible in methods. For other blocks, they are initialized
+ after names are analyzed.
+ */
+
+ /* TODO(jhylton): Package these dicts in a struct so that we
+ can write reasonable helper functions?
+ */
+ newglobal = PySet_New(NULL);
+ if (!newglobal)
+ goto error;
+ newfree = PySet_New(NULL);
+ if (!newfree)
+ goto error;
+ newbound = PySet_New(NULL);
+ if (!newbound)
+ goto error;
+
+ /* Class namespace has no effect on names visible in
+ nested functions, so populate the global and bound
+ sets to be passed to child blocks before analyzing
+ this one.
+ */
+ if (ste->ste_type == ClassBlock) {
+ /* Pass down known globals */
+ temp = PyNumber_InPlaceOr(newglobal, global);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ /* Pass down previously bound symbols */
+ if (bound) {
+ temp = PyNumber_InPlaceOr(newbound, bound);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ }
+ }
+
+ while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
+ long flags = PyLong_AS_LONG(v);
+ if (!analyze_name(ste, scopes, name, flags,
+ bound, local, free, global))
+ goto error;
+ }
+
+ /* Populate global and bound sets to be passed to children. */
+ if (ste->ste_type != ClassBlock) {
+ /* Add function locals to bound set */
+ if (ste->ste_type == FunctionBlock) {
+ temp = PyNumber_InPlaceOr(newbound, local);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ }
+ /* Pass down previously bound symbols */
+ if (bound) {
+ temp = PyNumber_InPlaceOr(newbound, bound);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ }
+ /* Pass down known globals */
+ temp = PyNumber_InPlaceOr(newglobal, global);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ }
+ else {
+ /* Special-case __class__ */
+ if (!GET_IDENTIFIER(__class__))
+ goto error;
+ assert(PySet_Contains(local, __class__) == 1);
+ if (PySet_Add(newbound, __class__) < 0)
+ goto error;
+ }
+
+ /* Recursively call analyze_block() on each child block.
+
+ newbound, newglobal now contain the names visible in
+ nested blocks. The free variables in the children will
+ be collected in allfree.
+ */
+ allfree = PySet_New(NULL);
+ if (!allfree)
+ goto error;
+ for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
+ PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
+ PySTEntryObject* entry;
+ assert(c && PySTEntry_Check(c));
+ entry = (PySTEntryObject*)c;
+ if (!analyze_child_block(entry, newbound, newfree, newglobal,
+ allfree))
+ goto error;
+ /* Check if any children have free variables */
+ if (entry->ste_free || entry->ste_child_free)
+ ste->ste_child_free = 1;
+ }
+
+ temp = PyNumber_InPlaceOr(newfree, allfree);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+
+ /* Check if any local variables must be converted to cell variables */
+ if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
+ NULL))
+ goto error;
+ else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
+ "__class__"))
+ goto error;
+ /* Records the results of the analysis in the symbol table entry */
+ if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
+ ste->ste_type == ClassBlock))
+ goto error;
+ if (!check_unoptimized(ste))
+ goto error;
+
+ temp = PyNumber_InPlaceOr(free, newfree);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ success = 1;
error:
- Py_XDECREF(scopes);
- Py_XDECREF(local);
- Py_XDECREF(newbound);
- Py_XDECREF(newglobal);
- Py_XDECREF(newfree);
- Py_XDECREF(allfree);
- if (!success)
- assert(PyErr_Occurred());
- return success;
+ Py_XDECREF(scopes);
+ Py_XDECREF(local);
+ Py_XDECREF(newbound);
+ Py_XDECREF(newglobal);
+ Py_XDECREF(newfree);
+ Py_XDECREF(allfree);
+ if (!success)
+ assert(PyErr_Occurred());
+ return success;
}
static int
-analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
- PyObject *global, PyObject* child_free)
+analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
+ PyObject *global, PyObject* child_free)
{
- PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
- PyObject *temp;
-
- /* Copy the bound and global dictionaries.
-
- These dictionary are used by all blocks enclosed by the
- current block. The analyze_block() call modifies these
- dictionaries.
-
- */
- temp_bound = PySet_New(bound);
- if (!temp_bound)
- goto error;
- temp_free = PySet_New(free);
- if (!temp_free)
- goto error;
- temp_global = PySet_New(global);
- if (!temp_global)
- goto error;
-
- if (!analyze_block(entry, temp_bound, temp_free, temp_global))
- goto error;
- temp = PyNumber_InPlaceOr(child_free, temp_free);
- if (!temp)
- goto error;
- Py_DECREF(temp);
- Py_DECREF(temp_bound);
- Py_DECREF(temp_free);
- Py_DECREF(temp_global);
- return 1;
+ PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
+ PyObject *temp;
+
+ /* Copy the bound and global dictionaries.
+
+ These dictionary are used by all blocks enclosed by the
+ current block. The analyze_block() call modifies these
+ dictionaries.
+
+ */
+ temp_bound = PySet_New(bound);
+ if (!temp_bound)
+ goto error;
+ temp_free = PySet_New(free);
+ if (!temp_free)
+ goto error;
+ temp_global = PySet_New(global);
+ if (!temp_global)
+ goto error;
+
+ if (!analyze_block(entry, temp_bound, temp_free, temp_global))
+ goto error;
+ temp = PyNumber_InPlaceOr(child_free, temp_free);
+ if (!temp)
+ goto error;
+ Py_DECREF(temp);
+ Py_DECREF(temp_bound);
+ Py_DECREF(temp_free);
+ Py_DECREF(temp_global);
+ return 1;
error:
- Py_XDECREF(temp_bound);
- Py_XDECREF(temp_free);
- Py_XDECREF(temp_global);
- return 0;
+ Py_XDECREF(temp_bound);
+ Py_XDECREF(temp_free);
+ Py_XDECREF(temp_global);
+ return 0;
}
static int
symtable_analyze(struct symtable *st)
{
- PyObject *free, *global;
- int r;
-
- free = PySet_New(NULL);
- if (!free)
- return 0;
- global = PySet_New(NULL);
- if (!global) {
- Py_DECREF(free);
- return 0;
- }
- r = analyze_block(st->st_top, NULL, free, global);
- Py_DECREF(free);
- Py_DECREF(global);
- return r;
+ PyObject *free, *global;
+ int r;
+
+ free = PySet_New(NULL);
+ if (!free)
+ return 0;
+ global = PySet_New(NULL);
+ if (!global) {
+ Py_DECREF(free);
+ return 0;
+ }
+ r = analyze_block(st->st_top, NULL, free, global);
+ Py_DECREF(free);
+ Py_DECREF(global);
+ return r;
}
static int
symtable_warn(struct symtable *st, char *msg, int lineno)
{
- if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
- lineno, NULL, NULL) < 0) {
- if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
- PyErr_SetString(PyExc_SyntaxError, msg);
- PyErr_SyntaxLocation(st->st_filename,
- st->st_cur->ste_lineno);
- }
- return 0;
- }
- return 1;
+ if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
+ lineno, NULL, NULL) < 0) {
+ if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
+ PyErr_SetString(PyExc_SyntaxError, msg);
+ PyErr_SyntaxLocation(st->st_filename,
+ st->st_cur->ste_lineno);
+ }
+ return 0;
+ }
+ return 1;
}
/* symtable_enter_block() gets a reference via ste_new.
@@ -891,211 +891,211 @@ symtable_warn(struct symtable *st, char *msg, int lineno)
static int
symtable_exit_block(struct symtable *st, void *ast)
{
- Py_ssize_t end;
-
- Py_CLEAR(st->st_cur);
- end = PyList_GET_SIZE(st->st_stack) - 1;
- if (end >= 0) {
- st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
- end);
- if (st->st_cur == NULL)
- return 0;
- Py_INCREF(st->st_cur);
- if (PySequence_DelItem(st->st_stack, end) < 0)
- return 0;
- }
- return 1;
+ Py_ssize_t end;
+
+ Py_CLEAR(st->st_cur);
+ end = PyList_GET_SIZE(st->st_stack) - 1;
+ if (end >= 0) {
+ st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
+ end);
+ if (st->st_cur == NULL)
+ return 0;
+ Py_INCREF(st->st_cur);
+ if (PySequence_DelItem(st->st_stack, end) < 0)
+ return 0;
+ }
+ return 1;
}
static int
-symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
- void *ast, int lineno)
+symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
+ void *ast, int lineno)
{
- PySTEntryObject *prev = NULL;
-
- if (st->st_cur) {
- prev = st->st_cur;
- if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
- return 0;
- }
- Py_DECREF(st->st_cur);
- }
- st->st_cur = ste_new(st, name, block, ast, lineno);
- if (st->st_cur == NULL)
- return 0;
- if (name == GET_IDENTIFIER(top))
- st->st_global = st->st_cur->ste_symbols;
- if (prev) {
- if (PyList_Append(prev->ste_children,
- (PyObject *)st->st_cur) < 0) {
- return 0;
- }
- }
- return 1;
+ PySTEntryObject *prev = NULL;
+
+ if (st->st_cur) {
+ prev = st->st_cur;
+ if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
+ return 0;
+ }
+ Py_DECREF(st->st_cur);
+ }
+ st->st_cur = ste_new(st, name, block, ast, lineno);
+ if (st->st_cur == NULL)
+ return 0;
+ if (name == GET_IDENTIFIER(top))
+ st->st_global = st->st_cur->ste_symbols;
+ if (prev) {
+ if (PyList_Append(prev->ste_children,
+ (PyObject *)st->st_cur) < 0) {
+ return 0;
+ }
+ }
+ return 1;
}
static long
symtable_lookup(struct symtable *st, PyObject *name)
{
- PyObject *o;
- PyObject *mangled = _Py_Mangle(st->st_private, name);
- if (!mangled)
- return 0;
- o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
- Py_DECREF(mangled);
- if (!o)
- return 0;
- return PyLong_AsLong(o);
+ PyObject *o;
+ PyObject *mangled = _Py_Mangle(st->st_private, name);
+ if (!mangled)
+ return 0;
+ o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
+ Py_DECREF(mangled);
+ if (!o)
+ return 0;
+ return PyLong_AsLong(o);
}
static int
-symtable_add_def(struct symtable *st, PyObject *name, int flag)
+symtable_add_def(struct symtable *st, PyObject *name, int flag)
{
- PyObject *o;
- PyObject *dict;
- long val;
- PyObject *mangled = _Py_Mangle(st->st_private, name);
-
-
- if (!mangled)
- return 0;
- dict = st->st_cur->ste_symbols;
- if ((o = PyDict_GetItem(dict, mangled))) {
- val = PyLong_AS_LONG(o);
- if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
- /* Is it better to use 'mangled' or 'name' here? */
- PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
- PyErr_SyntaxLocation(st->st_filename,
- st->st_cur->ste_lineno);
- goto error;
- }
- val |= flag;
- } else
- val = flag;
- o = PyLong_FromLong(val);
+ PyObject *o;
+ PyObject *dict;
+ long val;
+ PyObject *mangled = _Py_Mangle(st->st_private, name);
+
+
+ if (!mangled)
+ return 0;
+ dict = st->st_cur->ste_symbols;
+ if ((o = PyDict_GetItem(dict, mangled))) {
+ val = PyLong_AS_LONG(o);
+ if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
+ /* Is it better to use 'mangled' or 'name' here? */
+ PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
+ PyErr_SyntaxLocation(st->st_filename,
+ st->st_cur->ste_lineno);
+ goto error;
+ }
+ val |= flag;
+ } else
+ val = flag;
+ o = PyLong_FromLong(val);
+ if (o == NULL)
+ goto error;
+ if (PyDict_SetItem(dict, mangled, o) < 0) {
+ Py_DECREF(o);
+ goto error;
+ }
+ Py_DECREF(o);
+
+ if (flag & DEF_PARAM) {
+ if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
+ goto error;
+ } else if (flag & DEF_GLOBAL) {
+ /* XXX need to update DEF_GLOBAL for other flags too;
+ perhaps only DEF_FREE_GLOBAL */
+ val = flag;
+ if ((o = PyDict_GetItem(st->st_global, mangled))) {
+ val |= PyLong_AS_LONG(o);
+ }
+ o = PyLong_FromLong(val);
if (o == NULL)
- goto error;
- if (PyDict_SetItem(dict, mangled, o) < 0) {
- Py_DECREF(o);
- goto error;
- }
- Py_DECREF(o);
-
- if (flag & DEF_PARAM) {
- if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
- goto error;
- } else if (flag & DEF_GLOBAL) {
- /* XXX need to update DEF_GLOBAL for other flags too;
- perhaps only DEF_FREE_GLOBAL */
- val = flag;
- if ((o = PyDict_GetItem(st->st_global, mangled))) {
- val |= PyLong_AS_LONG(o);
- }
- o = PyLong_FromLong(val);
- if (o == NULL)
- goto error;
- if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
- Py_DECREF(o);
- goto error;
- }
- Py_DECREF(o);
- }
- Py_DECREF(mangled);
- return 1;
+ goto error;
+ if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
+ Py_DECREF(o);
+ goto error;
+ }
+ Py_DECREF(o);
+ }
+ Py_DECREF(mangled);
+ return 1;
error:
- Py_DECREF(mangled);
- return 0;
+ Py_DECREF(mangled);
+ return 0;
}
/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
They use the ASDL name to synthesize the name of the C type and the visit
- function.
-
+ function.
+
VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
useful if the first node in the sequence requires special treatment.
*/
#define VISIT(ST, TYPE, V) \
- if (!symtable_visit_ ## TYPE((ST), (V))) \
- return 0;
+ if (!symtable_visit_ ## TYPE((ST), (V))) \
+ return 0;
#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
- if (!symtable_visit_ ## TYPE((ST), (V))) { \
- symtable_exit_block((ST), (S)); \
- return 0; \
- }
+ if (!symtable_visit_ ## TYPE((ST), (V))) { \
+ symtable_exit_block((ST), (S)); \
+ return 0; \
+ }
#define VISIT_SEQ(ST, TYPE, SEQ) { \
- int i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (i = 0; i < asdl_seq_LEN(seq); i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
- if (!symtable_visit_ ## TYPE((ST), elt)) \
- return 0; \
- } \
+ int i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (i = 0; i < asdl_seq_LEN(seq); i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
+ if (!symtable_visit_ ## TYPE((ST), elt)) \
+ return 0; \
+ } \
}
#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
- int i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (i = 0; i < asdl_seq_LEN(seq); i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
- if (!symtable_visit_ ## TYPE((ST), elt)) { \
- symtable_exit_block((ST), (S)); \
- return 0; \
- } \
- } \
+ int i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (i = 0; i < asdl_seq_LEN(seq); i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
+ if (!symtable_visit_ ## TYPE((ST), elt)) { \
+ symtable_exit_block((ST), (S)); \
+ return 0; \
+ } \
+ } \
}
#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
- int i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (i = (START); i < asdl_seq_LEN(seq); i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
- if (!symtable_visit_ ## TYPE((ST), elt)) \
- return 0; \
- } \
+ int i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (i = (START); i < asdl_seq_LEN(seq); i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
+ if (!symtable_visit_ ## TYPE((ST), elt)) \
+ return 0; \
+ } \
}
#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
- int i; \
- asdl_seq *seq = (SEQ); /* avoid variable capture */ \
- for (i = (START); i < asdl_seq_LEN(seq); i++) { \
- TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
- if (!symtable_visit_ ## TYPE((ST), elt)) { \
- symtable_exit_block((ST), (S)); \
- return 0; \
- } \
- } \
+ int i; \
+ asdl_seq *seq = (SEQ); /* avoid variable capture */ \
+ for (i = (START); i < asdl_seq_LEN(seq); i++) { \
+ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
+ if (!symtable_visit_ ## TYPE((ST), elt)) { \
+ symtable_exit_block((ST), (S)); \
+ return 0; \
+ } \
+ } \
}
#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
- int i = 0; \
- asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
- for (i = 0; i < asdl_seq_LEN(seq); i++) { \
- expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
- if (!elt) continue; /* can be NULL */ \
- if (!symtable_visit_expr((ST), elt)) \
- return 0; \
- } \
+ int i = 0; \
+ asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
+ for (i = 0; i < asdl_seq_LEN(seq); i++) { \
+ expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
+ if (!elt) continue; /* can be NULL */ \
+ if (!symtable_visit_expr((ST), elt)) \
+ return 0; \
+ } \
}
static int
symtable_new_tmpname(struct symtable *st)
{
- char tmpname[256];
- identifier tmp;
-
- PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
- ++st->st_cur->ste_tmpname);
- tmp = PyUnicode_InternFromString(tmpname);
- if (!tmp)
- return 0;
- if (!symtable_add_def(st, tmp, DEF_LOCAL))
- return 0;
- Py_DECREF(tmp);
- return 1;
+ char tmpname[256];
+ identifier tmp;
+
+ PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
+ ++st->st_cur->ste_tmpname);
+ tmp = PyUnicode_InternFromString(tmpname);
+ if (!tmp)
+ return 0;
+ if (!symtable_add_def(st, tmp, DEF_LOCAL))
+ return 0;
+ Py_DECREF(tmp);
+ return 1;
}
@@ -1103,586 +1103,586 @@ symtable_new_tmpname(struct symtable *st)
static int
symtable_visit_stmt(struct symtable *st, stmt_ty s)
{
- switch (s->kind) {
- case FunctionDef_kind:
- if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
- return 0;
- if (s->v.FunctionDef.args->defaults)
- VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
- if (s->v.FunctionDef.args->kw_defaults)
- VISIT_KWONLYDEFAULTS(st,
- s->v.FunctionDef.args->kw_defaults);
- if (!symtable_visit_annotations(st, s))
- return 0;
- if (s->v.FunctionDef.decorator_list)
- VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
- if (!symtable_enter_block(st, s->v.FunctionDef.name,
- FunctionBlock, (void *)s, s->lineno))
- return 0;
- VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
- VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
- if (!symtable_exit_block(st, s))
- return 0;
- break;
- case ClassDef_kind: {
- PyObject *tmp;
- if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
- return 0;
- VISIT_SEQ(st, expr, s->v.ClassDef.bases);
- VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
- if (s->v.ClassDef.starargs)
- VISIT(st, expr, s->v.ClassDef.starargs);
- if (s->v.ClassDef.kwargs)
- VISIT(st, expr, s->v.ClassDef.kwargs);
- if (s->v.ClassDef.decorator_list)
- VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
- if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
- (void *)s, s->lineno))
- return 0;
- if (!GET_IDENTIFIER(__class__) ||
- !symtable_add_def(st, __class__, DEF_LOCAL) ||
- !GET_IDENTIFIER(__locals__) ||
- !symtable_add_def(st, __locals__, DEF_PARAM)) {
- symtable_exit_block(st, s);
- return 0;
- }
- tmp = st->st_private;
- st->st_private = s->v.ClassDef.name;
- VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
- st->st_private = tmp;
- if (!symtable_exit_block(st, s))
- return 0;
- break;
- }
- case Return_kind:
- if (s->v.Return.value) {
- VISIT(st, expr, s->v.Return.value);
- st->st_cur->ste_returns_value = 1;
- if (st->st_cur->ste_generator) {
- PyErr_SetString(PyExc_SyntaxError,
- RETURN_VAL_IN_GENERATOR);
- PyErr_SyntaxLocation(st->st_filename,
- s->lineno);
- return 0;
- }
- }
- break;
- case Delete_kind:
- VISIT_SEQ(st, expr, s->v.Delete.targets);
- break;
- case Assign_kind:
- VISIT_SEQ(st, expr, s->v.Assign.targets);
- VISIT(st, expr, s->v.Assign.value);
- break;
- case AugAssign_kind:
- VISIT(st, expr, s->v.AugAssign.target);
- VISIT(st, expr, s->v.AugAssign.value);
- break;
- case For_kind:
- VISIT(st, expr, s->v.For.target);
- VISIT(st, expr, s->v.For.iter);
- VISIT_SEQ(st, stmt, s->v.For.body);
- if (s->v.For.orelse)
- VISIT_SEQ(st, stmt, s->v.For.orelse);
- break;
- case While_kind:
- VISIT(st, expr, s->v.While.test);
- VISIT_SEQ(st, stmt, s->v.While.body);
- if (s->v.While.orelse)
- VISIT_SEQ(st, stmt, s->v.While.orelse);
- break;
- case If_kind:
- /* XXX if 0: and lookup_yield() hacks */
- VISIT(st, expr, s->v.If.test);
- VISIT_SEQ(st, stmt, s->v.If.body);
- if (s->v.If.orelse)
- VISIT_SEQ(st, stmt, s->v.If.orelse);
- break;
- case Raise_kind:
- if (s->v.Raise.exc) {
- VISIT(st, expr, s->v.Raise.exc);
- if (s->v.Raise.cause) {
- VISIT(st, expr, s->v.Raise.cause);
+ switch (s->kind) {
+ case FunctionDef_kind:
+ if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
+ return 0;
+ if (s->v.FunctionDef.args->defaults)
+ VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
+ if (s->v.FunctionDef.args->kw_defaults)
+ VISIT_KWONLYDEFAULTS(st,
+ s->v.FunctionDef.args->kw_defaults);
+ if (!symtable_visit_annotations(st, s))
+ return 0;
+ if (s->v.FunctionDef.decorator_list)
+ VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
+ if (!symtable_enter_block(st, s->v.FunctionDef.name,
+ FunctionBlock, (void *)s, s->lineno))
+ return 0;
+ VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
+ VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
+ if (!symtable_exit_block(st, s))
+ return 0;
+ break;
+ case ClassDef_kind: {
+ PyObject *tmp;
+ if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
+ return 0;
+ VISIT_SEQ(st, expr, s->v.ClassDef.bases);
+ VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
+ if (s->v.ClassDef.starargs)
+ VISIT(st, expr, s->v.ClassDef.starargs);
+ if (s->v.ClassDef.kwargs)
+ VISIT(st, expr, s->v.ClassDef.kwargs);
+ if (s->v.ClassDef.decorator_list)
+ VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
+ if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
+ (void *)s, s->lineno))
+ return 0;
+ if (!GET_IDENTIFIER(__class__) ||
+ !symtable_add_def(st, __class__, DEF_LOCAL) ||
+ !GET_IDENTIFIER(__locals__) ||
+ !symtable_add_def(st, __locals__, DEF_PARAM)) {
+ symtable_exit_block(st, s);
+ return 0;
+ }
+ tmp = st->st_private;
+ st->st_private = s->v.ClassDef.name;
+ VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
+ st->st_private = tmp;
+ if (!symtable_exit_block(st, s))
+ return 0;
+ break;
+ }
+ case Return_kind:
+ if (s->v.Return.value) {
+ VISIT(st, expr, s->v.Return.value);
+ st->st_cur->ste_returns_value = 1;
+ if (st->st_cur->ste_generator) {
+ PyErr_SetString(PyExc_SyntaxError,
+ RETURN_VAL_IN_GENERATOR);
+ PyErr_SyntaxLocation(st->st_filename,
+ s->lineno);
+ return 0;
}
- }
- break;
- case TryExcept_kind:
- VISIT_SEQ(st, stmt, s->v.TryExcept.body);
- VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
- VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
- break;
- case TryFinally_kind:
- VISIT_SEQ(st, stmt, s->v.TryFinally.body);
- VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
- break;
- case Assert_kind:
- VISIT(st, expr, s->v.Assert.test);
- if (s->v.Assert.msg)
- VISIT(st, expr, s->v.Assert.msg);
- break;
- case Import_kind:
- VISIT_SEQ(st, alias, s->v.Import.names);
- /* XXX Don't have the lineno available inside
- visit_alias */
- if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
- st->st_cur->ste_opt_lineno = s->lineno;
- break;
- case ImportFrom_kind:
- VISIT_SEQ(st, alias, s->v.ImportFrom.names);
- /* XXX Don't have the lineno available inside
- visit_alias */
- if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
- st->st_cur->ste_opt_lineno = s->lineno;
- break;
- case Global_kind: {
- int i;
- asdl_seq *seq = s->v.Global.names;
- for (i = 0; i < asdl_seq_LEN(seq); i++) {
- identifier name = (identifier)asdl_seq_GET(seq, i);
- char *c_name = _PyUnicode_AsString(name);
- long cur = symtable_lookup(st, name);
- if (cur < 0)
- return 0;
- if (cur & (DEF_LOCAL | USE)) {
- char buf[256];
- if (cur & DEF_LOCAL)
- PyOS_snprintf(buf, sizeof(buf),
- GLOBAL_AFTER_ASSIGN,
- c_name);
- else
- PyOS_snprintf(buf, sizeof(buf),
- GLOBAL_AFTER_USE,
- c_name);
- if (!symtable_warn(st, buf, s->lineno))
- return 0;
- }
- if (!symtable_add_def(st, name, DEF_GLOBAL))
- return 0;
- }
- break;
- }
- case Nonlocal_kind: {
- int i;
- asdl_seq *seq = s->v.Nonlocal.names;
- for (i = 0; i < asdl_seq_LEN(seq); i++) {
- identifier name = (identifier)asdl_seq_GET(seq, i);
- char *c_name = _PyUnicode_AsString(name);
- long cur = symtable_lookup(st, name);
- if (cur < 0)
- return 0;
- if (cur & (DEF_LOCAL | USE)) {
- char buf[256];
- if (cur & DEF_LOCAL)
- PyOS_snprintf(buf, sizeof(buf),
- NONLOCAL_AFTER_ASSIGN,
- c_name);
- else
- PyOS_snprintf(buf, sizeof(buf),
- NONLOCAL_AFTER_USE,
- c_name);
- if (!symtable_warn(st, buf, s->lineno))
- return 0;
- }
- if (!symtable_add_def(st, name, DEF_NONLOCAL))
- return 0;
- }
- break;
- }
- case Expr_kind:
- VISIT(st, expr, s->v.Expr.value);
- break;
- case Pass_kind:
- case Break_kind:
- case Continue_kind:
- /* nothing to do here */
- break;
- case With_kind:
- if (!symtable_new_tmpname(st))
- return 0;
- VISIT(st, expr, s->v.With.context_expr);
- if (s->v.With.optional_vars) {
- if (!symtable_new_tmpname(st))
- return 0;
- VISIT(st, expr, s->v.With.optional_vars);
- }
- VISIT_SEQ(st, stmt, s->v.With.body);
- break;
- }
- return 1;
+ }
+ break;
+ case Delete_kind:
+ VISIT_SEQ(st, expr, s->v.Delete.targets);
+ break;
+ case Assign_kind:
+ VISIT_SEQ(st, expr, s->v.Assign.targets);
+ VISIT(st, expr, s->v.Assign.value);
+ break;
+ case AugAssign_kind:
+ VISIT(st, expr, s->v.AugAssign.target);
+ VISIT(st, expr, s->v.AugAssign.value);
+ break;
+ case For_kind:
+ VISIT(st, expr, s->v.For.target);
+ VISIT(st, expr, s->v.For.iter);
+ VISIT_SEQ(st, stmt, s->v.For.body);
+ if (s->v.For.orelse)
+ VISIT_SEQ(st, stmt, s->v.For.orelse);
+ break;
+ case While_kind:
+ VISIT(st, expr, s->v.While.test);
+ VISIT_SEQ(st, stmt, s->v.While.body);
+ if (s->v.While.orelse)
+ VISIT_SEQ(st, stmt, s->v.While.orelse);
+ break;
+ case If_kind:
+ /* XXX if 0: and lookup_yield() hacks */
+ VISIT(st, expr, s->v.If.test);
+ VISIT_SEQ(st, stmt, s->v.If.body);
+ if (s->v.If.orelse)
+ VISIT_SEQ(st, stmt, s->v.If.orelse);
+ break;
+ case Raise_kind:
+ if (s->v.Raise.exc) {
+ VISIT(st, expr, s->v.Raise.exc);
+ if (s->v.Raise.cause) {
+ VISIT(st, expr, s->v.Raise.cause);
+ }
+ }
+ break;
+ case TryExcept_kind:
+ VISIT_SEQ(st, stmt, s->v.TryExcept.body);
+ VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
+ VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
+ break;
+ case TryFinally_kind:
+ VISIT_SEQ(st, stmt, s->v.TryFinally.body);
+ VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
+ break;
+ case Assert_kind:
+ VISIT(st, expr, s->v.Assert.test);
+ if (s->v.Assert.msg)
+ VISIT(st, expr, s->v.Assert.msg);
+ break;
+ case Import_kind:
+ VISIT_SEQ(st, alias, s->v.Import.names);
+ /* XXX Don't have the lineno available inside
+ visit_alias */
+ if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
+ st->st_cur->ste_opt_lineno = s->lineno;
+ break;
+ case ImportFrom_kind:
+ VISIT_SEQ(st, alias, s->v.ImportFrom.names);
+ /* XXX Don't have the lineno available inside
+ visit_alias */
+ if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
+ st->st_cur->ste_opt_lineno = s->lineno;
+ break;
+ case Global_kind: {
+ int i;
+ asdl_seq *seq = s->v.Global.names;
+ for (i = 0; i < asdl_seq_LEN(seq); i++) {
+ identifier name = (identifier)asdl_seq_GET(seq, i);
+ char *c_name = _PyUnicode_AsString(name);
+ long cur = symtable_lookup(st, name);
+ if (cur < 0)
+ return 0;
+ if (cur & (DEF_LOCAL | USE)) {
+ char buf[256];
+ if (cur & DEF_LOCAL)
+ PyOS_snprintf(buf, sizeof(buf),
+ GLOBAL_AFTER_ASSIGN,
+ c_name);
+ else
+ PyOS_snprintf(buf, sizeof(buf),
+ GLOBAL_AFTER_USE,
+ c_name);
+ if (!symtable_warn(st, buf, s->lineno))
+ return 0;
+ }
+ if (!symtable_add_def(st, name, DEF_GLOBAL))
+ return 0;
+ }
+ break;
+ }
+ case Nonlocal_kind: {
+ int i;
+ asdl_seq *seq = s->v.Nonlocal.names;
+ for (i = 0; i < asdl_seq_LEN(seq); i++) {
+ identifier name = (identifier)asdl_seq_GET(seq, i);
+ char *c_name = _PyUnicode_AsString(name);
+ long cur = symtable_lookup(st, name);
+ if (cur < 0)
+ return 0;
+ if (cur & (DEF_LOCAL | USE)) {
+ char buf[256];
+ if (cur & DEF_LOCAL)
+ PyOS_snprintf(buf, sizeof(buf),
+ NONLOCAL_AFTER_ASSIGN,
+ c_name);
+ else
+ PyOS_snprintf(buf, sizeof(buf),
+ NONLOCAL_AFTER_USE,
+ c_name);
+ if (!symtable_warn(st, buf, s->lineno))
+ return 0;
+ }
+ if (!symtable_add_def(st, name, DEF_NONLOCAL))
+ return 0;
+ }
+ break;
+ }
+ case Expr_kind:
+ VISIT(st, expr, s->v.Expr.value);
+ break;
+ case Pass_kind:
+ case Break_kind:
+ case Continue_kind:
+ /* nothing to do here */
+ break;
+ case With_kind:
+ if (!symtable_new_tmpname(st))
+ return 0;
+ VISIT(st, expr, s->v.With.context_expr);
+ if (s->v.With.optional_vars) {
+ if (!symtable_new_tmpname(st))
+ return 0;
+ VISIT(st, expr, s->v.With.optional_vars);
+ }
+ VISIT_SEQ(st, stmt, s->v.With.body);
+ break;
+ }
+ return 1;
}
-static int
+static int
symtable_visit_expr(struct symtable *st, expr_ty e)
{
- switch (e->kind) {
- case BoolOp_kind:
- VISIT_SEQ(st, expr, e->v.BoolOp.values);
- break;
- case BinOp_kind:
- VISIT(st, expr, e->v.BinOp.left);
- VISIT(st, expr, e->v.BinOp.right);
- break;
- case UnaryOp_kind:
- VISIT(st, expr, e->v.UnaryOp.operand);
- break;
- case Lambda_kind: {
- if (!GET_IDENTIFIER(lambda) ||
- !symtable_add_def(st, lambda, DEF_LOCAL))
- return 0;
- if (e->v.Lambda.args->defaults)
- VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
- if (!symtable_enter_block(st, lambda,
- FunctionBlock, (void *)e, e->lineno))
- return 0;
- VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
- VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
- if (!symtable_exit_block(st, (void *)e))
- return 0;
- break;
- }
- case IfExp_kind:
- VISIT(st, expr, e->v.IfExp.test);
- VISIT(st, expr, e->v.IfExp.body);
- VISIT(st, expr, e->v.IfExp.orelse);
- break;
- case Dict_kind:
- VISIT_SEQ(st, expr, e->v.Dict.keys);
- VISIT_SEQ(st, expr, e->v.Dict.values);
- break;
- case Set_kind:
- VISIT_SEQ(st, expr, e->v.Set.elts);
- break;
- case GeneratorExp_kind:
- if (!symtable_visit_genexp(st, e))
- return 0;
- break;
- case ListComp_kind:
- if (!symtable_visit_listcomp(st, e))
- return 0;
- break;
- case SetComp_kind:
- if (!symtable_visit_setcomp(st, e))
- return 0;
- break;
- case DictComp_kind:
- if (!symtable_visit_dictcomp(st, e))
- return 0;
- break;
- case Yield_kind:
- if (e->v.Yield.value)
- VISIT(st, expr, e->v.Yield.value);
- st->st_cur->ste_generator = 1;
- if (st->st_cur->ste_returns_value) {
- PyErr_SetString(PyExc_SyntaxError,
- RETURN_VAL_IN_GENERATOR);
- PyErr_SyntaxLocation(st->st_filename,
- e->lineno);
- return 0;
- }
- break;
- case Compare_kind:
- VISIT(st, expr, e->v.Compare.left);
- VISIT_SEQ(st, expr, e->v.Compare.comparators);
- break;
- case Call_kind:
- VISIT(st, expr, e->v.Call.func);
- VISIT_SEQ(st, expr, e->v.Call.args);
- VISIT_SEQ(st, keyword, e->v.Call.keywords);
- if (e->v.Call.starargs)
- VISIT(st, expr, e->v.Call.starargs);
- if (e->v.Call.kwargs)
- VISIT(st, expr, e->v.Call.kwargs);
- break;
- case Num_kind:
- case Str_kind:
- case Bytes_kind:
- case Ellipsis_kind:
- /* Nothing to do here. */
- break;
- /* The following exprs can be assignment targets. */
- case Attribute_kind:
- VISIT(st, expr, e->v.Attribute.value);
- break;
- case Subscript_kind:
- VISIT(st, expr, e->v.Subscript.value);
- VISIT(st, slice, e->v.Subscript.slice);
- break;
- case Starred_kind:
- VISIT(st, expr, e->v.Starred.value);
- break;
- case Name_kind:
- if (!symtable_add_def(st, e->v.Name.id,
- e->v.Name.ctx == Load ? USE : DEF_LOCAL))
- return 0;
- /* Special-case super: it counts as a use of __class__ */
- if (e->v.Name.ctx == Load &&
- st->st_cur->ste_type == FunctionBlock &&
- !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
- if (!GET_IDENTIFIER(__class__) ||
- !symtable_add_def(st, __class__, USE))
- return 0;
- }
- break;
- /* child nodes of List and Tuple will have expr_context set */
- case List_kind:
- VISIT_SEQ(st, expr, e->v.List.elts);
- break;
- case Tuple_kind:
- VISIT_SEQ(st, expr, e->v.Tuple.elts);
- break;
- }
- return 1;
+ switch (e->kind) {
+ case BoolOp_kind:
+ VISIT_SEQ(st, expr, e->v.BoolOp.values);
+ break;
+ case BinOp_kind:
+ VISIT(st, expr, e->v.BinOp.left);
+ VISIT(st, expr, e->v.BinOp.right);
+ break;
+ case UnaryOp_kind:
+ VISIT(st, expr, e->v.UnaryOp.operand);
+ break;
+ case Lambda_kind: {
+ if (!GET_IDENTIFIER(lambda) ||
+ !symtable_add_def(st, lambda, DEF_LOCAL))
+ return 0;
+ if (e->v.Lambda.args->defaults)
+ VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
+ if (!symtable_enter_block(st, lambda,
+ FunctionBlock, (void *)e, e->lineno))
+ return 0;
+ VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
+ VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
+ if (!symtable_exit_block(st, (void *)e))
+ return 0;
+ break;
+ }
+ case IfExp_kind:
+ VISIT(st, expr, e->v.IfExp.test);
+ VISIT(st, expr, e->v.IfExp.body);
+ VISIT(st, expr, e->v.IfExp.orelse);
+ break;
+ case Dict_kind:
+ VISIT_SEQ(st, expr, e->v.Dict.keys);
+ VISIT_SEQ(st, expr, e->v.Dict.values);
+ break;
+ case Set_kind:
+ VISIT_SEQ(st, expr, e->v.Set.elts);
+ break;
+ case GeneratorExp_kind:
+ if (!symtable_visit_genexp(st, e))
+ return 0;
+ break;
+ case ListComp_kind:
+ if (!symtable_visit_listcomp(st, e))
+ return 0;
+ break;
+ case SetComp_kind:
+ if (!symtable_visit_setcomp(st, e))
+ return 0;
+ break;
+ case DictComp_kind:
+ if (!symtable_visit_dictcomp(st, e))
+ return 0;
+ break;
+ case Yield_kind:
+ if (e->v.Yield.value)
+ VISIT(st, expr, e->v.Yield.value);
+ st->st_cur->ste_generator = 1;
+ if (st->st_cur->ste_returns_value) {
+ PyErr_SetString(PyExc_SyntaxError,
+ RETURN_VAL_IN_GENERATOR);
+ PyErr_SyntaxLocation(st->st_filename,
+ e->lineno);
+ return 0;
+ }
+ break;
+ case Compare_kind:
+ VISIT(st, expr, e->v.Compare.left);
+ VISIT_SEQ(st, expr, e->v.Compare.comparators);
+ break;
+ case Call_kind:
+ VISIT(st, expr, e->v.Call.func);
+ VISIT_SEQ(st, expr, e->v.Call.args);
+ VISIT_SEQ(st, keyword, e->v.Call.keywords);
+ if (e->v.Call.starargs)
+ VISIT(st, expr, e->v.Call.starargs);
+ if (e->v.Call.kwargs)
+ VISIT(st, expr, e->v.Call.kwargs);
+ break;
+ case Num_kind:
+ case Str_kind:
+ case Bytes_kind:
+ case Ellipsis_kind:
+ /* Nothing to do here. */
+ break;
+ /* The following exprs can be assignment targets. */
+ case Attribute_kind:
+ VISIT(st, expr, e->v.Attribute.value);
+ break;
+ case Subscript_kind:
+ VISIT(st, expr, e->v.Subscript.value);
+ VISIT(st, slice, e->v.Subscript.slice);
+ break;
+ case Starred_kind:
+ VISIT(st, expr, e->v.Starred.value);
+ break;
+ case Name_kind:
+ if (!symtable_add_def(st, e->v.Name.id,
+ e->v.Name.ctx == Load ? USE : DEF_LOCAL))
+ return 0;
+ /* Special-case super: it counts as a use of __class__ */
+ if (e->v.Name.ctx == Load &&
+ st->st_cur->ste_type == FunctionBlock &&
+ !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
+ if (!GET_IDENTIFIER(__class__) ||
+ !symtable_add_def(st, __class__, USE))
+ return 0;
+ }
+ break;
+ /* child nodes of List and Tuple will have expr_context set */
+ case List_kind:
+ VISIT_SEQ(st, expr, e->v.List.elts);
+ break;
+ case Tuple_kind:
+ VISIT_SEQ(st, expr, e->v.Tuple.elts);
+ break;
+ }
+ return 1;
}
static int
symtable_implicit_arg(struct symtable *st, int pos)
{
- PyObject *id = PyUnicode_FromFormat(".%d", pos);
- if (id == NULL)
- return 0;
- if (!symtable_add_def(st, id, DEF_PARAM)) {
- Py_DECREF(id);
- return 0;
- }
- Py_DECREF(id);
- return 1;
+ PyObject *id = PyUnicode_FromFormat(".%d", pos);
+ if (id == NULL)
+ return 0;
+ if (!symtable_add_def(st, id, DEF_PARAM)) {
+ Py_DECREF(id);
+ return 0;
+ }
+ Py_DECREF(id);
+ return 1;
}
-static int
+static int
symtable_visit_params(struct symtable *st, asdl_seq *args)
{
- int i;
-
- if (!args)
- return -1;
-
- for (i = 0; i < asdl_seq_LEN(args); i++) {
- arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
- if (!symtable_add_def(st, arg->arg, DEF_PARAM))
- return 0;
- }
-
- return 1;
+ int i;
+
+ if (!args)
+ return -1;
+
+ for (i = 0; i < asdl_seq_LEN(args); i++) {
+ arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
+ if (!symtable_add_def(st, arg->arg, DEF_PARAM))
+ return 0;
+ }
+
+ return 1;
}
-static int
+static int
symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
{
- int i;
-
- if (!args)
- return -1;
-
- for (i = 0; i < asdl_seq_LEN(args); i++) {
- arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
- if (arg->annotation)
- VISIT(st, expr, arg->annotation);
- }
-
- return 1;
+ int i;
+
+ if (!args)
+ return -1;
+
+ for (i = 0; i < asdl_seq_LEN(args); i++) {
+ arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
+ if (arg->annotation)
+ VISIT(st, expr, arg->annotation);
+ }
+
+ return 1;
}
static int
symtable_visit_annotations(struct symtable *st, stmt_ty s)
{
- arguments_ty a = s->v.FunctionDef.args;
-
- if (a->args && !symtable_visit_argannotations(st, a->args))
- return 0;
- if (a->varargannotation)
- VISIT(st, expr, a->varargannotation);
- if (a->kwargannotation)
- VISIT(st, expr, a->kwargannotation);
- if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
- return 0;
- if (s->v.FunctionDef.returns)
- VISIT(st, expr, s->v.FunctionDef.returns);
- return 1;
+ arguments_ty a = s->v.FunctionDef.args;
+
+ if (a->args && !symtable_visit_argannotations(st, a->args))
+ return 0;
+ if (a->varargannotation)
+ VISIT(st, expr, a->varargannotation);
+ if (a->kwargannotation)
+ VISIT(st, expr, a->kwargannotation);
+ if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
+ return 0;
+ if (s->v.FunctionDef.returns)
+ VISIT(st, expr, s->v.FunctionDef.returns);
+ return 1;
}
-static int
+static int
symtable_visit_arguments(struct symtable *st, arguments_ty a)
{
- /* skip default arguments inside function block
- XXX should ast be different?
- */
- if (a->args && !symtable_visit_params(st, a->args))
- return 0;
- if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
- return 0;
- if (a->vararg) {
- if (!symtable_add_def(st, a->vararg, DEF_PARAM))
- return 0;
- st->st_cur->ste_varargs = 1;
- }
- if (a->kwarg) {
- if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
- return 0;
- st->st_cur->ste_varkeywords = 1;
- }
- return 1;
+ /* skip default arguments inside function block
+ XXX should ast be different?
+ */
+ if (a->args && !symtable_visit_params(st, a->args))
+ return 0;
+ if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
+ return 0;
+ if (a->vararg) {
+ if (!symtable_add_def(st, a->vararg, DEF_PARAM))
+ return 0;
+ st->st_cur->ste_varargs = 1;
+ }
+ if (a->kwarg) {
+ if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
+ return 0;
+ st->st_cur->ste_varkeywords = 1;
+ }
+ return 1;
}
-static int
+static int
symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
{
- if (eh->v.ExceptHandler.type)
- VISIT(st, expr, eh->v.ExceptHandler.type);
- if (eh->v.ExceptHandler.name)
- if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
- return 0;
- VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
- return 1;
+ if (eh->v.ExceptHandler.type)
+ VISIT(st, expr, eh->v.ExceptHandler.type);
+ if (eh->v.ExceptHandler.name)
+ if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
+ return 0;
+ VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
+ return 1;
}
-static int
+static int
symtable_visit_alias(struct symtable *st, alias_ty a)
{
- /* Compute store_name, the name actually bound by the import
- operation. It is diferent than a->name when a->name is a
- dotted package name (e.g. spam.eggs)
- */
- PyObject *store_name;
- PyObject *name = (a->asname == NULL) ? a->name : a->asname;
- const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
- Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
- if (dot) {
- store_name = PyUnicode_FromUnicode(base, dot - base);
- if (!store_name)
- return 0;
- }
- else {
- store_name = name;
- Py_INCREF(store_name);
- }
- if (PyUnicode_CompareWithASCIIString(name, "*")) {
- int r = symtable_add_def(st, store_name, DEF_IMPORT);
- Py_DECREF(store_name);
- return r;
- }
- else {
- if (st->st_cur->ste_type != ModuleBlock) {
- int lineno = st->st_cur->ste_lineno;
- PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
- PyErr_SyntaxLocation(st->st_filename, lineno);
- Py_DECREF(store_name);
- return 0;
- }
- st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
- Py_DECREF(store_name);
- return 1;
- }
+ /* Compute store_name, the name actually bound by the import
+ operation. It is diferent than a->name when a->name is a
+ dotted package name (e.g. spam.eggs)
+ */
+ PyObject *store_name;
+ PyObject *name = (a->asname == NULL) ? a->name : a->asname;
+ const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
+ Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
+ if (dot) {
+ store_name = PyUnicode_FromUnicode(base, dot - base);
+ if (!store_name)
+ return 0;
+ }
+ else {
+ store_name = name;
+ Py_INCREF(store_name);
+ }
+ if (PyUnicode_CompareWithASCIIString(name, "*")) {
+ int r = symtable_add_def(st, store_name, DEF_IMPORT);
+ Py_DECREF(store_name);
+ return r;
+ }
+ else {
+ if (st->st_cur->ste_type != ModuleBlock) {
+ int lineno = st->st_cur->ste_lineno;
+ PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
+ PyErr_SyntaxLocation(st->st_filename, lineno);
+ Py_DECREF(store_name);
+ return 0;
+ }
+ st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
+ Py_DECREF(store_name);
+ return 1;
+ }
}
-static int
+static int
symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
{
- VISIT(st, expr, lc->target);
- VISIT(st, expr, lc->iter);
- VISIT_SEQ(st, expr, lc->ifs);
- return 1;
+ VISIT(st, expr, lc->target);
+ VISIT(st, expr, lc->iter);
+ VISIT_SEQ(st, expr, lc->ifs);
+ return 1;
}
-static int
+static int
symtable_visit_keyword(struct symtable *st, keyword_ty k)
{
- VISIT(st, expr, k->value);
- return 1;
+ VISIT(st, expr, k->value);
+ return 1;
}
-static int
+static int
symtable_visit_slice(struct symtable *st, slice_ty s)
{
- switch (s->kind) {
- case Slice_kind:
- if (s->v.Slice.lower)
- VISIT(st, expr, s->v.Slice.lower)
- if (s->v.Slice.upper)
- VISIT(st, expr, s->v.Slice.upper)
- if (s->v.Slice.step)
- VISIT(st, expr, s->v.Slice.step)
- break;
- case ExtSlice_kind:
- VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
- break;
- case Index_kind:
- VISIT(st, expr, s->v.Index.value)
- break;
- }
- return 1;
+ switch (s->kind) {
+ case Slice_kind:
+ if (s->v.Slice.lower)
+ VISIT(st, expr, s->v.Slice.lower)
+ if (s->v.Slice.upper)
+ VISIT(st, expr, s->v.Slice.upper)
+ if (s->v.Slice.step)
+ VISIT(st, expr, s->v.Slice.step)
+ break;
+ case ExtSlice_kind:
+ VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
+ break;
+ case Index_kind:
+ VISIT(st, expr, s->v.Index.value)
+ break;
+ }
+ return 1;
}
-static int
+static int
symtable_handle_comprehension(struct symtable *st, expr_ty e,
identifier scope_name, asdl_seq *generators,
expr_ty elt, expr_ty value)
{
- int is_generator = (e->kind == GeneratorExp_kind);
- int needs_tmp = !is_generator;
- comprehension_ty outermost = ((comprehension_ty)
- asdl_seq_GET(generators, 0));
- /* Outermost iterator is evaluated in current scope */
- VISIT(st, expr, outermost->iter);
- /* Create comprehension scope for the rest */
- if (!scope_name ||
- !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, e->lineno)) {
- return 0;
- }
- st->st_cur->ste_generator = is_generator;
- /* Outermost iter is received as an argument */
- if (!symtable_implicit_arg(st, 0)) {
- symtable_exit_block(st, (void *)e);
- return 0;
- }
- /* Allocate temporary name if needed */
- if (needs_tmp && !symtable_new_tmpname(st)) {
- symtable_exit_block(st, (void *)e);
- return 0;
- }
- VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
- VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
- VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
- generators, 1, (void*)e);
- if (value)
- VISIT_IN_BLOCK(st, expr, value, (void*)e);
- VISIT_IN_BLOCK(st, expr, elt, (void*)e);
- return symtable_exit_block(st, (void *)e);
+ int is_generator = (e->kind == GeneratorExp_kind);
+ int needs_tmp = !is_generator;
+ comprehension_ty outermost = ((comprehension_ty)
+ asdl_seq_GET(generators, 0));
+ /* Outermost iterator is evaluated in current scope */
+ VISIT(st, expr, outermost->iter);
+ /* Create comprehension scope for the rest */
+ if (!scope_name ||
+ !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, e->lineno)) {
+ return 0;
+ }
+ st->st_cur->ste_generator = is_generator;
+ /* Outermost iter is received as an argument */
+ if (!symtable_implicit_arg(st, 0)) {
+ symtable_exit_block(st, (void *)e);
+ return 0;
+ }
+ /* Allocate temporary name if needed */
+ if (needs_tmp && !symtable_new_tmpname(st)) {
+ symtable_exit_block(st, (void *)e);
+ return 0;
+ }
+ VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
+ VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
+ VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
+ generators, 1, (void*)e);
+ if (value)
+ VISIT_IN_BLOCK(st, expr, value, (void*)e);
+ VISIT_IN_BLOCK(st, expr, elt, (void*)e);
+ return symtable_exit_block(st, (void *)e);
}
-static int
+static int
symtable_visit_genexp(struct symtable *st, expr_ty e)
{
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
- e->v.GeneratorExp.generators,
- e->v.GeneratorExp.elt, NULL);
+ return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
+ e->v.GeneratorExp.generators,
+ e->v.GeneratorExp.elt, NULL);
}
-static int
+static int
symtable_visit_listcomp(struct symtable *st, expr_ty e)
{
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
- e->v.ListComp.generators,
- e->v.ListComp.elt, NULL);
+ return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
+ e->v.ListComp.generators,
+ e->v.ListComp.elt, NULL);
}
static int
symtable_visit_setcomp(struct symtable *st, expr_ty e)
{
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
- e->v.SetComp.generators,
- e->v.SetComp.elt, NULL);
+ return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
+ e->v.SetComp.generators,
+ e->v.SetComp.elt, NULL);
}
static int
symtable_visit_dictcomp(struct symtable *st, expr_ty e)
{
- return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
- e->v.DictComp.generators,
- e->v.DictComp.key,
- e->v.DictComp.value);
+ return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
+ e->v.DictComp.generators,
+ e->v.DictComp.key,
+ e->v.DictComp.value);
}
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 69ee9c1..33dfbb3 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -45,63 +45,63 @@ extern const char *PyWin_DLLVersionString;
PyObject *
PySys_GetObject(const char *name)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
- if (sd == NULL)
- return NULL;
- return PyDict_GetItemString(sd, name);
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *sd = tstate->interp->sysdict;
+ if (sd == NULL)
+ return NULL;
+ return PyDict_GetItemString(sd, name);
}
int
PySys_SetObject(const char *name, PyObject *v)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *sd = tstate->interp->sysdict;
- if (v == NULL) {
- if (PyDict_GetItemString(sd, name) == NULL)
- return 0;
- else
- return PyDict_DelItemString(sd, name);
- }
- else
- return PyDict_SetItemString(sd, name, v);
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *sd = tstate->interp->sysdict;
+ if (v == NULL) {
+ if (PyDict_GetItemString(sd, name) == NULL)
+ return 0;
+ else
+ return PyDict_DelItemString(sd, name);
+ }
+ else
+ return PyDict_SetItemString(sd, name, v);
}
static PyObject *
sys_displayhook(PyObject *self, PyObject *o)
{
- PyObject *outf;
- PyInterpreterState *interp = PyThreadState_GET()->interp;
- PyObject *modules = interp->modules;
- PyObject *builtins = PyDict_GetItemString(modules, "builtins");
-
- if (builtins == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
- return NULL;
- }
-
- /* Print value except if None */
- /* After printing, also assign to '_' */
- /* Before, set '_' to None to avoid recursion */
- if (o == Py_None) {
- Py_INCREF(Py_None);
- return Py_None;
- }
- if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
- return NULL;
- outf = PySys_GetObject("stdout");
- if (outf == NULL || outf == Py_None) {
- PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
- return NULL;
- }
- if (PyFile_WriteObject(o, outf, 0) != 0)
- return NULL;
- if (PyFile_WriteString("\n", outf) != 0)
- return NULL;
- if (PyObject_SetAttrString(builtins, "_", o) != 0)
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *outf;
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ PyObject *modules = interp->modules;
+ PyObject *builtins = PyDict_GetItemString(modules, "builtins");
+
+ if (builtins == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
+ return NULL;
+ }
+
+ /* Print value except if None */
+ /* After printing, also assign to '_' */
+ /* Before, set '_' to None to avoid recursion */
+ if (o == Py_None) {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+ if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
+ return NULL;
+ outf = PySys_GetObject("stdout");
+ if (outf == NULL || outf == Py_None) {
+ PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
+ return NULL;
+ }
+ if (PyFile_WriteObject(o, outf, 0) != 0)
+ return NULL;
+ if (PyFile_WriteString("\n", outf) != 0)
+ return NULL;
+ if (PyObject_SetAttrString(builtins, "_", o) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(displayhook_doc,
@@ -113,12 +113,12 @@ PyDoc_STRVAR(displayhook_doc,
static PyObject *
sys_excepthook(PyObject* self, PyObject* args)
{
- PyObject *exc, *value, *tb;
- if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
- return NULL;
- PyErr_Display(exc, value, tb);
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *exc, *value, *tb;
+ if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
+ return NULL;
+ PyErr_Display(exc, value, tb);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(excepthook_doc,
@@ -130,14 +130,14 @@ PyDoc_STRVAR(excepthook_doc,
static PyObject *
sys_exc_info(PyObject *self, PyObject *noargs)
{
- PyThreadState *tstate;
- tstate = PyThreadState_GET();
- return Py_BuildValue(
- "(OOO)",
- tstate->exc_type != NULL ? tstate->exc_type : Py_None,
- tstate->exc_value != NULL ? tstate->exc_value : Py_None,
- tstate->exc_traceback != NULL ?
- tstate->exc_traceback : Py_None);
+ PyThreadState *tstate;
+ tstate = PyThreadState_GET();
+ return Py_BuildValue(
+ "(OOO)",
+ tstate->exc_type != NULL ? tstate->exc_type : Py_None,
+ tstate->exc_value != NULL ? tstate->exc_value : Py_None,
+ tstate->exc_traceback != NULL ?
+ tstate->exc_traceback : Py_None);
}
PyDoc_STRVAR(exc_info_doc,
@@ -150,12 +150,12 @@ clause in the current stack frame or in an older stack frame."
static PyObject *
sys_exit(PyObject *self, PyObject *args)
{
- PyObject *exit_code = 0;
- if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
- return NULL;
- /* Raise SystemExit so callers may catch it or clean up. */
- PyErr_SetObject(PyExc_SystemExit, exit_code);
- return NULL;
+ PyObject *exit_code = 0;
+ if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code))
+ return NULL;
+ /* Raise SystemExit so callers may catch it or clean up. */
+ PyErr_SetObject(PyExc_SystemExit, exit_code);
+ return NULL;
}
PyDoc_STRVAR(exit_doc,
@@ -172,7 +172,7 @@ exit status will be one (i.e., failure)."
static PyObject *
sys_getdefaultencoding(PyObject *self)
{
- return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
+ return PyUnicode_FromString(PyUnicode_GetDefaultEncoding());
}
PyDoc_STRVAR(getdefaultencoding_doc,
@@ -185,13 +185,13 @@ implementation."
static PyObject *
sys_setdefaultencoding(PyObject *self, PyObject *args)
{
- char *encoding;
- if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
- return NULL;
- if (PyUnicode_SetDefaultEncoding(encoding))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ char *encoding;
+ if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
+ return NULL;
+ if (PyUnicode_SetDefaultEncoding(encoding))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setdefaultencoding_doc,
@@ -203,10 +203,10 @@ Set the current default string encoding used by the Unicode implementation."
static PyObject *
sys_getfilesystemencoding(PyObject *self)
{
- if (Py_FileSystemDefaultEncoding)
- return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
- Py_INCREF(Py_None);
- return Py_None;
+ if (Py_FileSystemDefaultEncoding)
+ return PyUnicode_FromString(Py_FileSystemDefaultEncoding);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(getfilesystemencoding_doc,
@@ -219,13 +219,13 @@ operating system filenames."
static PyObject *
sys_setfilesystemencoding(PyObject *self, PyObject *args)
{
- PyObject *new_encoding;
- if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding))
- return NULL;
- if (_Py_SetFileSystemEncoding(new_encoding))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ PyObject *new_encoding;
+ if (!PyArg_ParseTuple(args, "U:setfilesystemencoding", &new_encoding))
+ return NULL;
+ if (_Py_SetFileSystemEncoding(new_encoding))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setfilesystemencoding_doc,
@@ -238,19 +238,19 @@ operating system filenames."
static PyObject *
sys_intern(PyObject *self, PyObject *args)
{
- PyObject *s;
- if (!PyArg_ParseTuple(args, "U:intern", &s))
- return NULL;
- if (PyUnicode_CheckExact(s)) {
- Py_INCREF(s);
- PyUnicode_InternInPlace(&s);
- return s;
- }
- else {
- PyErr_Format(PyExc_TypeError,
- "can't intern %.400s", s->ob_type->tp_name);
- return NULL;
- }
+ PyObject *s;
+ if (!PyArg_ParseTuple(args, "U:intern", &s))
+ return NULL;
+ if (PyUnicode_CheckExact(s)) {
+ Py_INCREF(s);
+ PyUnicode_InternInPlace(&s);
+ return s;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "can't intern %.400s", s->ob_type->tp_name);
+ return NULL;
+ }
}
PyDoc_STRVAR(intern_doc,
@@ -271,116 +271,116 @@ static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static int
trace_init(void)
{
- static char *whatnames[7] = {"call", "exception", "line", "return",
- "c_call", "c_exception", "c_return"};
- PyObject *name;
- int i;
- for (i = 0; i < 7; ++i) {
- if (whatstrings[i] == NULL) {
- name = PyUnicode_InternFromString(whatnames[i]);
- if (name == NULL)
- return -1;
- whatstrings[i] = name;
- }
- }
- return 0;
+ static char *whatnames[7] = {"call", "exception", "line", "return",
+ "c_call", "c_exception", "c_return"};
+ PyObject *name;
+ int i;
+ for (i = 0; i < 7; ++i) {
+ if (whatstrings[i] == NULL) {
+ name = PyUnicode_InternFromString(whatnames[i]);
+ if (name == NULL)
+ return -1;
+ whatstrings[i] = name;
+ }
+ }
+ return 0;
}
static PyObject *
call_trampoline(PyThreadState *tstate, PyObject* callback,
- PyFrameObject *frame, int what, PyObject *arg)
+ PyFrameObject *frame, int what, PyObject *arg)
{
- PyObject *args = PyTuple_New(3);
- PyObject *whatstr;
- PyObject *result;
-
- if (args == NULL)
- return NULL;
- Py_INCREF(frame);
- whatstr = whatstrings[what];
- Py_INCREF(whatstr);
- if (arg == NULL)
- arg = Py_None;
- Py_INCREF(arg);
- PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
- PyTuple_SET_ITEM(args, 1, whatstr);
- PyTuple_SET_ITEM(args, 2, arg);
-
- /* call the Python-level function */
- PyFrame_FastToLocals(frame);
- result = PyEval_CallObject(callback, args);
- PyFrame_LocalsToFast(frame, 1);
- if (result == NULL)
- PyTraceBack_Here(frame);
-
- /* cleanup */
- Py_DECREF(args);
- return result;
+ PyObject *args = PyTuple_New(3);
+ PyObject *whatstr;
+ PyObject *result;
+
+ if (args == NULL)
+ return NULL;
+ Py_INCREF(frame);
+ whatstr = whatstrings[what];
+ Py_INCREF(whatstr);
+ if (arg == NULL)
+ arg = Py_None;
+ Py_INCREF(arg);
+ PyTuple_SET_ITEM(args, 0, (PyObject *)frame);
+ PyTuple_SET_ITEM(args, 1, whatstr);
+ PyTuple_SET_ITEM(args, 2, arg);
+
+ /* call the Python-level function */
+ PyFrame_FastToLocals(frame);
+ result = PyEval_CallObject(callback, args);
+ PyFrame_LocalsToFast(frame, 1);
+ if (result == NULL)
+ PyTraceBack_Here(frame);
+
+ /* cleanup */
+ Py_DECREF(args);
+ return result;
}
static int
profile_trampoline(PyObject *self, PyFrameObject *frame,
- int what, PyObject *arg)
+ int what, PyObject *arg)
{
- PyThreadState *tstate = frame->f_tstate;
- PyObject *result;
-
- if (arg == NULL)
- arg = Py_None;
- result = call_trampoline(tstate, self, frame, what, arg);
- if (result == NULL) {
- PyEval_SetProfile(NULL, NULL);
- return -1;
- }
- Py_DECREF(result);
- return 0;
+ PyThreadState *tstate = frame->f_tstate;
+ PyObject *result;
+
+ if (arg == NULL)
+ arg = Py_None;
+ result = call_trampoline(tstate, self, frame, what, arg);
+ if (result == NULL) {
+ PyEval_SetProfile(NULL, NULL);
+ return -1;
+ }
+ Py_DECREF(result);
+ return 0;
}
static int
trace_trampoline(PyObject *self, PyFrameObject *frame,
- int what, PyObject *arg)
+ int what, PyObject *arg)
{
- PyThreadState *tstate = frame->f_tstate;
- PyObject *callback;
- PyObject *result;
-
- if (what == PyTrace_CALL)
- callback = self;
- else
- callback = frame->f_trace;
- if (callback == NULL)
- return 0;
- result = call_trampoline(tstate, callback, frame, what, arg);
- if (result == NULL) {
- PyEval_SetTrace(NULL, NULL);
- Py_XDECREF(frame->f_trace);
- frame->f_trace = NULL;
- return -1;
- }
- if (result != Py_None) {
- PyObject *temp = frame->f_trace;
- frame->f_trace = NULL;
- Py_XDECREF(temp);
- frame->f_trace = result;
- }
- else {
- Py_DECREF(result);
- }
- return 0;
+ PyThreadState *tstate = frame->f_tstate;
+ PyObject *callback;
+ PyObject *result;
+
+ if (what == PyTrace_CALL)
+ callback = self;
+ else
+ callback = frame->f_trace;
+ if (callback == NULL)
+ return 0;
+ result = call_trampoline(tstate, callback, frame, what, arg);
+ if (result == NULL) {
+ PyEval_SetTrace(NULL, NULL);
+ Py_XDECREF(frame->f_trace);
+ frame->f_trace = NULL;
+ return -1;
+ }
+ if (result != Py_None) {
+ PyObject *temp = frame->f_trace;
+ frame->f_trace = NULL;
+ Py_XDECREF(temp);
+ frame->f_trace = result;
+ }
+ else {
+ Py_DECREF(result);
+ }
+ return 0;
}
static PyObject *
sys_settrace(PyObject *self, PyObject *args)
{
- if (trace_init() == -1)
- return NULL;
- if (args == Py_None)
- PyEval_SetTrace(NULL, NULL);
- else
- PyEval_SetTrace(trace_trampoline, args);
- Py_INCREF(Py_None);
- return Py_None;
+ if (trace_init() == -1)
+ return NULL;
+ if (args == Py_None)
+ PyEval_SetTrace(NULL, NULL);
+ else
+ PyEval_SetTrace(trace_trampoline, args);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(settrace_doc,
@@ -393,13 +393,13 @@ function call. See the debugger chapter in the library manual."
static PyObject *
sys_gettrace(PyObject *self, PyObject *args)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *temp = tstate->c_traceobj;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *temp = tstate->c_traceobj;
- if (temp == NULL)
- temp = Py_None;
- Py_INCREF(temp);
- return temp;
+ if (temp == NULL)
+ temp = Py_None;
+ Py_INCREF(temp);
+ return temp;
}
PyDoc_STRVAR(gettrace_doc,
@@ -412,14 +412,14 @@ See the debugger chapter in the library manual."
static PyObject *
sys_setprofile(PyObject *self, PyObject *args)
{
- if (trace_init() == -1)
- return NULL;
- if (args == Py_None)
- PyEval_SetProfile(NULL, NULL);
- else
- PyEval_SetProfile(profile_trampoline, args);
- Py_INCREF(Py_None);
- return Py_None;
+ if (trace_init() == -1)
+ return NULL;
+ if (args == Py_None)
+ PyEval_SetProfile(NULL, NULL);
+ else
+ PyEval_SetProfile(profile_trampoline, args);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setprofile_doc,
@@ -432,13 +432,13 @@ and return. See the profiler chapter in the library manual."
static PyObject *
sys_getprofile(PyObject *self, PyObject *args)
{
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *temp = tstate->c_profileobj;
+ PyThreadState *tstate = PyThreadState_GET();
+ PyObject *temp = tstate->c_profileobj;
- if (temp == NULL)
- temp = Py_None;
- Py_INCREF(temp);
- return temp;
+ if (temp == NULL)
+ temp = Py_None;
+ Py_INCREF(temp);
+ return temp;
}
PyDoc_STRVAR(getprofile_doc,
@@ -451,10 +451,10 @@ See the profiler chapter in the library manual."
static PyObject *
sys_setcheckinterval(PyObject *self, PyObject *args)
{
- if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
- return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setcheckinterval_doc,
@@ -467,7 +467,7 @@ n instructions. This also affects how often thread switches occur."
static PyObject *
sys_getcheckinterval(PyObject *self, PyObject *args)
{
- return PyLong_FromLong(_Py_CheckInterval);
+ return PyLong_FromLong(_Py_CheckInterval);
}
PyDoc_STRVAR(getcheckinterval_doc,
@@ -478,17 +478,17 @@ PyDoc_STRVAR(getcheckinterval_doc,
static PyObject *
sys_settscdump(PyObject *self, PyObject *args)
{
- int bool;
- PyThreadState *tstate = PyThreadState_Get();
-
- if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
- return NULL;
- if (bool)
- tstate->interp->tscdump = 1;
- else
- tstate->interp->tscdump = 0;
- Py_INCREF(Py_None);
- return Py_None;
+ int bool;
+ PyThreadState *tstate = PyThreadState_Get();
+
+ if (!PyArg_ParseTuple(args, "i:settscdump", &bool))
+ return NULL;
+ if (bool)
+ tstate->interp->tscdump = 1;
+ else
+ tstate->interp->tscdump = 0;
+ Py_INCREF(Py_None);
+ return Py_None;
}
@@ -504,17 +504,17 @@ processor's time-stamp counter."
static PyObject *
sys_setrecursionlimit(PyObject *self, PyObject *args)
{
- int new_limit;
- if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
- return NULL;
- if (new_limit <= 0) {
- PyErr_SetString(PyExc_ValueError,
- "recursion limit must be positive");
- return NULL;
- }
- Py_SetRecursionLimit(new_limit);
- Py_INCREF(Py_None);
- return Py_None;
+ int new_limit;
+ if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
+ return NULL;
+ if (new_limit <= 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "recursion limit must be positive");
+ return NULL;
+ }
+ Py_SetRecursionLimit(new_limit);
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setrecursionlimit_doc,
@@ -529,7 +529,7 @@ dependent."
static PyObject *
sys_getrecursionlimit(PyObject *self)
{
- return PyLong_FromLong(Py_GetRecursionLimit());
+ return PyLong_FromLong(Py_GetRecursionLimit());
}
PyDoc_STRVAR(getrecursionlimit_doc,
@@ -554,16 +554,16 @@ Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\
static PyObject *
sys_getwindowsversion(PyObject *self)
{
- OSVERSIONINFO ver;
- ver.dwOSVersionInfoSize = sizeof(ver);
- if (!GetVersionEx(&ver))
- return PyErr_SetFromWindowsErr(0);
- return Py_BuildValue("HHHHs",
- ver.dwMajorVersion,
- ver.dwMinorVersion,
- ver.dwBuildNumber,
- ver.dwPlatformId,
- ver.szCSDVersion);
+ OSVERSIONINFO ver;
+ ver.dwOSVersionInfoSize = sizeof(ver);
+ if (!GetVersionEx(&ver))
+ return PyErr_SetFromWindowsErr(0);
+ return Py_BuildValue("HHHHs",
+ ver.dwMajorVersion,
+ ver.dwMinorVersion,
+ ver.dwBuildNumber,
+ ver.dwPlatformId,
+ ver.szCSDVersion);
}
#endif /* MS_WINDOWS */
@@ -572,15 +572,15 @@ sys_getwindowsversion(PyObject *self)
static PyObject *
sys_setdlopenflags(PyObject *self, PyObject *args)
{
- int new_val;
- PyThreadState *tstate = PyThreadState_GET();
- if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
- return NULL;
- if (!tstate)
- return NULL;
- tstate->interp->dlopenflags = new_val;
- Py_INCREF(Py_None);
- return Py_None;
+ int new_val;
+ PyThreadState *tstate = PyThreadState_GET();
+ if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val))
+ return NULL;
+ if (!tstate)
+ return NULL;
+ tstate->interp->dlopenflags = new_val;
+ Py_INCREF(Py_None);
+ return Py_None;
}
PyDoc_STRVAR(setdlopenflags_doc,
@@ -598,10 +598,10 @@ h2py script.");
static PyObject *
sys_getdlopenflags(PyObject *self, PyObject *args)
{
- PyThreadState *tstate = PyThreadState_GET();
- if (!tstate)
- return NULL;
- return PyLong_FromLong(tstate->interp->dlopenflags);
+ PyThreadState *tstate = PyThreadState_GET();
+ if (!tstate)
+ return NULL;
+ return PyLong_FromLong(tstate->interp->dlopenflags);
}
PyDoc_STRVAR(getdlopenflags_doc,
@@ -610,7 +610,7 @@ PyDoc_STRVAR(getdlopenflags_doc,
Return the current value of the flags that are used for dlopen calls.\n\
The flag constants are defined in the ctypes and DLFCN modules.");
-#endif /* HAVE_DLOPEN */
+#endif /* HAVE_DLOPEN */
#ifdef USE_MALLOPT
/* Link with -lmalloc (or -lmpc) on an SGI */
@@ -619,70 +619,70 @@ The flag constants are defined in the ctypes and DLFCN modules.");
static PyObject *
sys_mdebug(PyObject *self, PyObject *args)
{
- int flag;
- if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
- return NULL;
- mallopt(M_DEBUG, flag);
- Py_INCREF(Py_None);
- return Py_None;
+ int flag;
+ if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
+ return NULL;
+ mallopt(M_DEBUG, flag);
+ Py_INCREF(Py_None);
+ return Py_None;
}
#endif /* USE_MALLOPT */
static PyObject *
sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *res = NULL;
- static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
- static char *kwlist[] = {"object", "default", 0};
- PyObject *o, *dflt = NULL;
- PyObject *method;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
- kwlist, &o, &dflt))
- return NULL;
-
- /* Initialize static variable for GC head size */
- if (gc_head_size == NULL) {
- gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
- if (gc_head_size == NULL)
- return NULL;
- }
-
- /* Make sure the type is initialized. float gets initialized late */
- if (PyType_Ready(Py_TYPE(o)) < 0)
- return NULL;
-
- method = _PyObject_LookupSpecial(o, "__sizeof__",
- &str__sizeof__);
- if (method == NULL) {
- if (!PyErr_Occurred())
- PyErr_Format(PyExc_TypeError,
- "Type %.100s doesn't define __sizeof__",
- Py_TYPE(o)->tp_name);
- }
- else {
- res = PyObject_CallFunctionObjArgs(method, NULL);
- Py_DECREF(method);
- }
-
- /* Has a default value been given */
- if ((res == NULL) && (dflt != NULL) &&
- PyErr_ExceptionMatches(PyExc_TypeError))
- {
- PyErr_Clear();
- Py_INCREF(dflt);
- return dflt;
- }
- else if (res == NULL)
- return res;
-
- /* add gc_head size */
- if (PyObject_IS_GC(o)) {
- PyObject *tmp = res;
- res = PyNumber_Add(tmp, gc_head_size);
- Py_DECREF(tmp);
- }
- return res;
+ PyObject *res = NULL;
+ static PyObject *str__sizeof__ = NULL, *gc_head_size = NULL;
+ static char *kwlist[] = {"object", "default", 0};
+ PyObject *o, *dflt = NULL;
+ PyObject *method;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
+ kwlist, &o, &dflt))
+ return NULL;
+
+ /* Initialize static variable for GC head size */
+ if (gc_head_size == NULL) {
+ gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head));
+ if (gc_head_size == NULL)
+ return NULL;
+ }
+
+ /* Make sure the type is initialized. float gets initialized late */
+ if (PyType_Ready(Py_TYPE(o)) < 0)
+ return NULL;
+
+ method = _PyObject_LookupSpecial(o, "__sizeof__",
+ &str__sizeof__);
+ if (method == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_Format(PyExc_TypeError,
+ "Type %.100s doesn't define __sizeof__",
+ Py_TYPE(o)->tp_name);
+ }
+ else {
+ res = PyObject_CallFunctionObjArgs(method, NULL);
+ Py_DECREF(method);
+ }
+
+ /* Has a default value been given */
+ if ((res == NULL) && (dflt != NULL) &&
+ PyErr_ExceptionMatches(PyExc_TypeError))
+ {
+ PyErr_Clear();
+ Py_INCREF(dflt);
+ return dflt;
+ }
+ else if (res == NULL)
+ return res;
+
+ /* add gc_head size */
+ if (PyObject_IS_GC(o)) {
+ PyObject *tmp = res;
+ res = PyNumber_Add(tmp, gc_head_size);
+ Py_DECREF(tmp);
+ }
+ return res;
}
PyDoc_STRVAR(getsizeof_doc,
@@ -693,14 +693,14 @@ Return the size of object in bytes.");
static PyObject *
sys_getrefcount(PyObject *self, PyObject *arg)
{
- return PyLong_FromSsize_t(arg->ob_refcnt);
+ return PyLong_FromSsize_t(arg->ob_refcnt);
}
#ifdef Py_REF_DEBUG
static PyObject *
sys_gettotalrefcount(PyObject *self)
{
- return PyLong_FromSsize_t(_Py_GetRefTotal());
+ return PyLong_FromSsize_t(_Py_GetRefTotal());
}
#endif /* Py_REF_DEBUG */
@@ -716,9 +716,9 @@ reference as an argument to getrefcount()."
static PyObject *
sys_getcounts(PyObject *self)
{
- extern PyObject *get_counts(void);
+ extern PyObject *get_counts(void);
- return get_counts();
+ return get_counts();
}
#endif
@@ -737,23 +737,23 @@ purposes only."
static PyObject *
sys_getframe(PyObject *self, PyObject *args)
{
- PyFrameObject *f = PyThreadState_GET()->frame;
- int depth = -1;
-
- if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
- return NULL;
-
- while (depth > 0 && f != NULL) {
- f = f->f_back;
- --depth;
- }
- if (f == NULL) {
- PyErr_SetString(PyExc_ValueError,
- "call stack is not deep enough");
- return NULL;
- }
- Py_INCREF(f);
- return (PyObject*)f;
+ PyFrameObject *f = PyThreadState_GET()->frame;
+ int depth = -1;
+
+ if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
+ return NULL;
+
+ while (depth > 0 && f != NULL) {
+ f = f->f_back;
+ --depth;
+ }
+ if (f == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "call stack is not deep enough");
+ return NULL;
+ }
+ Py_INCREF(f);
+ return (PyObject*)f;
}
PyDoc_STRVAR(current_frames_doc,
@@ -768,7 +768,7 @@ This function should be used for specialized purposes only."
static PyObject *
sys_current_frames(PyObject *self, PyObject *noargs)
{
- return _PyThread_CurrentFrames();
+ return _PyThread_CurrentFrames();
}
PyDoc_STRVAR(call_tracing_doc,
@@ -782,10 +782,10 @@ a debugger from a checkpoint, to recursively debug some other code."
static PyObject *
sys_call_tracing(PyObject *self, PyObject *args)
{
- PyObject *func, *funcargs;
- if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
- return NULL;
- return _PyEval_CallTracing(func, funcargs);
+ PyObject *func, *funcargs;
+ if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs))
+ return NULL;
+ return _PyEval_CallTracing(func, funcargs);
}
PyDoc_STRVAR(callstats_doc,
@@ -832,8 +832,8 @@ extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
static PyObject *
sys_clear_type_cache(PyObject* self, PyObject* args)
{
- PyType_ClearCache();
- Py_RETURN_NONE;
+ PyType_ClearCache();
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(sys_clear_type_cache__doc__,
@@ -842,101 +842,101 @@ Clear the internal type lookup cache.");
static PyMethodDef sys_methods[] = {
- /* Might as well keep this in alphabetic order */
- {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
- callstats_doc},
- {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
- sys_clear_type_cache__doc__},
- {"_current_frames", sys_current_frames, METH_NOARGS,
- current_frames_doc},
- {"displayhook", sys_displayhook, METH_O, displayhook_doc},
- {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
- {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
- {"exit", sys_exit, METH_VARARGS, exit_doc},
- {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
- METH_NOARGS, getdefaultencoding_doc},
+ /* Might as well keep this in alphabetic order */
+ {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
+ callstats_doc},
+ {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS,
+ sys_clear_type_cache__doc__},
+ {"_current_frames", sys_current_frames, METH_NOARGS,
+ current_frames_doc},
+ {"displayhook", sys_displayhook, METH_O, displayhook_doc},
+ {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc},
+ {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
+ {"exit", sys_exit, METH_VARARGS, exit_doc},
+ {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
+ METH_NOARGS, getdefaultencoding_doc},
#ifdef HAVE_DLOPEN
- {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
- getdlopenflags_doc},
+ {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS,
+ getdlopenflags_doc},
#endif
#ifdef COUNT_ALLOCS
- {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
+ {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS},
#endif
#ifdef DYNAMIC_EXECUTION_PROFILE
- {"getdxp", _Py_GetDXProfile, METH_VARARGS},
+ {"getdxp", _Py_GetDXProfile, METH_VARARGS},
#endif
- {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
- METH_NOARGS, getfilesystemencoding_doc},
+ {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding,
+ METH_NOARGS, getfilesystemencoding_doc},
#ifdef Py_TRACE_REFS
- {"getobjects", _Py_GetObjects, METH_VARARGS},
+ {"getobjects", _Py_GetObjects, METH_VARARGS},
#endif
#ifdef Py_REF_DEBUG
- {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
+ {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS},
#endif
- {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
- {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
- getrecursionlimit_doc},
- {"getsizeof", (PyCFunction)sys_getsizeof,
- METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
- {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
+ {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc},
+ {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS,
+ getrecursionlimit_doc},
+ {"getsizeof", (PyCFunction)sys_getsizeof,
+ METH_VARARGS | METH_KEYWORDS, getsizeof_doc},
+ {"_getframe", sys_getframe, METH_VARARGS, getframe_doc},
#ifdef MS_WINDOWS
- {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
- getwindowsversion_doc},
+ {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
+ getwindowsversion_doc},
#endif /* MS_WINDOWS */
- {"intern", sys_intern, METH_VARARGS, intern_doc},
+ {"intern", sys_intern, METH_VARARGS, intern_doc},
#ifdef USE_MALLOPT
- {"mdebug", sys_mdebug, METH_VARARGS},
+ {"mdebug", sys_mdebug, METH_VARARGS},
#endif
- {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
- setdefaultencoding_doc},
- {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS,
- setfilesystemencoding_doc},
- {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
- setcheckinterval_doc},
- {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
- getcheckinterval_doc},
+ {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS,
+ setdefaultencoding_doc},
+ {"setfilesystemencoding", sys_setfilesystemencoding, METH_VARARGS,
+ setfilesystemencoding_doc},
+ {"setcheckinterval", sys_setcheckinterval, METH_VARARGS,
+ setcheckinterval_doc},
+ {"getcheckinterval", sys_getcheckinterval, METH_NOARGS,
+ getcheckinterval_doc},
#ifdef HAVE_DLOPEN
- {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
- setdlopenflags_doc},
+ {"setdlopenflags", sys_setdlopenflags, METH_VARARGS,
+ setdlopenflags_doc},
#endif
- {"setprofile", sys_setprofile, METH_O, setprofile_doc},
- {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
- {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
- setrecursionlimit_doc},
+ {"setprofile", sys_setprofile, METH_O, setprofile_doc},
+ {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc},
+ {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS,
+ setrecursionlimit_doc},
#ifdef WITH_TSC
- {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
+ {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc},
#endif
- {"settrace", sys_settrace, METH_O, settrace_doc},
- {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
- {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
- {NULL, NULL} /* sentinel */
+ {"settrace", sys_settrace, METH_O, settrace_doc},
+ {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc},
+ {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
+ {NULL, NULL} /* sentinel */
};
static PyObject *
list_builtin_module_names(void)
{
- PyObject *list = PyList_New(0);
- int i;
- if (list == NULL)
- return NULL;
- for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
- PyObject *name = PyUnicode_FromString(
- PyImport_Inittab[i].name);
- if (name == NULL)
- break;
- PyList_Append(list, name);
- Py_DECREF(name);
- }
- if (PyList_Sort(list) != 0) {
- Py_DECREF(list);
- list = NULL;
- }
- if (list) {
- PyObject *v = PyList_AsTuple(list);
- Py_DECREF(list);
- list = v;
- }
- return list;
+ PyObject *list = PyList_New(0);
+ int i;
+ if (list == NULL)
+ return NULL;
+ for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
+ PyObject *name = PyUnicode_FromString(
+ PyImport_Inittab[i].name);
+ if (name == NULL)
+ break;
+ PyList_Append(list, name);
+ Py_DECREF(name);
+ }
+ if (PyList_Sort(list) != 0) {
+ Py_DECREF(list);
+ list = NULL;
+ }
+ if (list) {
+ PyObject *v = PyList_AsTuple(list);
+ Py_DECREF(list);
+ list = v;
+ }
+ return list;
}
static PyObject *warnoptions = NULL;
@@ -944,27 +944,27 @@ static PyObject *warnoptions = NULL;
void
PySys_ResetWarnOptions(void)
{
- if (warnoptions == NULL || !PyList_Check(warnoptions))
- return;
- PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
+ if (warnoptions == NULL || !PyList_Check(warnoptions))
+ return;
+ PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
}
void
PySys_AddWarnOption(const wchar_t *s)
{
- PyObject *str;
-
- if (warnoptions == NULL || !PyList_Check(warnoptions)) {
- Py_XDECREF(warnoptions);
- warnoptions = PyList_New(0);
- if (warnoptions == NULL)
- return;
- }
- str = PyUnicode_FromWideChar(s, -1);
- if (str != NULL) {
- PyList_Append(warnoptions, str);
- Py_DECREF(str);
- }
+ PyObject *str;
+
+ if (warnoptions == NULL || !PyList_Check(warnoptions)) {
+ Py_XDECREF(warnoptions);
+ warnoptions = PyList_New(0);
+ if (warnoptions == NULL)
+ return;
+ }
+ str = PyUnicode_FromWideChar(s, -1);
+ if (str != NULL) {
+ PyList_Append(warnoptions, str);
+ Py_DECREF(str);
+ }
}
int
@@ -1076,64 +1076,64 @@ static const char *svn_revision;
static void
svnversion_init(void)
{
- const char *python, *br_start, *br_end, *br_end2, *svnversion;
- Py_ssize_t len;
- int istag = 0;
-
- if (svn_initialized)
- return;
-
- python = strstr(headurl, "/python/");
- if (!python) {
- strcpy(branch, "unknown branch");
- strcpy(shortbranch, "unknown");
- }
- else {
- br_start = python + 8;
- br_end = strchr(br_start, '/');
- assert(br_end);
-
- /* Works even for trunk,
- as we are in trunk/Python/sysmodule.c */
- br_end2 = strchr(br_end+1, '/');
-
- istag = strncmp(br_start, "tags", 4) == 0;
- if (strncmp(br_start, "trunk", 5) == 0) {
- strcpy(branch, "trunk");
- strcpy(shortbranch, "trunk");
- }
- else if (istag || strncmp(br_start, "branches", 8) == 0) {
- len = br_end2 - br_start;
- strncpy(branch, br_start, len);
- branch[len] = '\0';
-
- len = br_end2 - (br_end + 1);
- strncpy(shortbranch, br_end + 1, len);
- shortbranch[len] = '\0';
- }
- else {
- Py_FatalError("bad HeadURL");
- return;
- }
- }
-
-
- svnversion = _Py_svnversion();
- if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
- svn_revision = svnversion;
- else if (istag) {
- len = strlen(_patchlevel_revision);
- assert(len >= 13);
- assert(len < (sizeof(patchlevel_revision) + 13));
- strncpy(patchlevel_revision, _patchlevel_revision + 11,
- len - 13);
- patchlevel_revision[len - 13] = '\0';
- svn_revision = patchlevel_revision;
- }
- else
- svn_revision = "";
-
- svn_initialized = 1;
+ const char *python, *br_start, *br_end, *br_end2, *svnversion;
+ Py_ssize_t len;
+ int istag = 0;
+
+ if (svn_initialized)
+ return;
+
+ python = strstr(headurl, "/python/");
+ if (!python) {
+ strcpy(branch, "unknown branch");
+ strcpy(shortbranch, "unknown");
+ }
+ else {
+ br_start = python + 8;
+ br_end = strchr(br_start, '/');
+ assert(br_end);
+
+ /* Works even for trunk,
+ as we are in trunk/Python/sysmodule.c */
+ br_end2 = strchr(br_end+1, '/');
+
+ istag = strncmp(br_start, "tags", 4) == 0;
+ if (strncmp(br_start, "trunk", 5) == 0) {
+ strcpy(branch, "trunk");
+ strcpy(shortbranch, "trunk");
+ }
+ else if (istag || strncmp(br_start, "branches", 8) == 0) {
+ len = br_end2 - br_start;
+ strncpy(branch, br_start, len);
+ branch[len] = '\0';
+
+ len = br_end2 - (br_end + 1);
+ strncpy(shortbranch, br_end + 1, len);
+ shortbranch[len] = '\0';
+ }
+ else {
+ Py_FatalError("bad HeadURL");
+ return;
+ }
+ }
+
+
+ svnversion = _Py_svnversion();
+ if (strcmp(svnversion, "Unversioned directory") != 0 && strcmp(svnversion, "exported") != 0)
+ svn_revision = svnversion;
+ else if (istag) {
+ len = strlen(_patchlevel_revision);
+ assert(len >= 13);
+ assert(len < (sizeof(patchlevel_revision) + 13));
+ strncpy(patchlevel_revision, _patchlevel_revision + 11,
+ len - 13);
+ patchlevel_revision[len - 13] = '\0';
+ svn_revision = patchlevel_revision;
+ }
+ else
+ svn_revision = "";
+
+ svn_initialized = 1;
}
/* Return svnversion output if available.
@@ -1142,15 +1142,15 @@ svnversion_init(void)
const char*
Py_SubversionRevision()
{
- svnversion_init();
- return svn_revision;
+ svnversion_init();
+ return svn_revision;
}
const char*
Py_SubversionShortBranch()
{
- svnversion_init();
- return shortbranch;
+ svnversion_init();
+ return shortbranch;
}
@@ -1162,71 +1162,71 @@ Flags provided through command line arguments or environment vars.");
static PyTypeObject FlagsType;
static PyStructSequence_Field flags_fields[] = {
- {"debug", "-d"},
- {"division_warning", "-Q"},
- {"inspect", "-i"},
- {"interactive", "-i"},
- {"optimize", "-O or -OO"},
- {"dont_write_bytecode", "-B"},
- {"no_user_site", "-s"},
- {"no_site", "-S"},
- {"ignore_environment", "-E"},
- {"verbose", "-v"},
+ {"debug", "-d"},
+ {"division_warning", "-Q"},
+ {"inspect", "-i"},
+ {"interactive", "-i"},
+ {"optimize", "-O or -OO"},
+ {"dont_write_bytecode", "-B"},
+ {"no_user_site", "-s"},
+ {"no_site", "-S"},
+ {"ignore_environment", "-E"},
+ {"verbose", "-v"},
#ifdef RISCOS
- {"riscos_wimp", "???"},
+ {"riscos_wimp", "???"},
#endif
- /* {"unbuffered", "-u"}, */
- /* {"skip_first", "-x"}, */
- {"bytes_warning", "-b"},
- {0}
+ /* {"unbuffered", "-u"}, */
+ /* {"skip_first", "-x"}, */
+ {"bytes_warning", "-b"},
+ {0}
};
static PyStructSequence_Desc flags_desc = {
- "sys.flags", /* name */
- flags__doc__, /* doc */
- flags_fields, /* fields */
+ "sys.flags", /* name */
+ flags__doc__, /* doc */
+ flags_fields, /* fields */
#ifdef RISCOS
- 12
+ 12
#else
- 11
+ 11
#endif
};
static PyObject*
make_flags(void)
{
- int pos = 0;
- PyObject *seq;
+ int pos = 0;
+ PyObject *seq;
- seq = PyStructSequence_New(&FlagsType);
- if (seq == NULL)
- return NULL;
+ seq = PyStructSequence_New(&FlagsType);
+ if (seq == NULL)
+ return NULL;
#define SetFlag(flag) \
- PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
-
- SetFlag(Py_DebugFlag);
- SetFlag(Py_DivisionWarningFlag);
- SetFlag(Py_InspectFlag);
- SetFlag(Py_InteractiveFlag);
- SetFlag(Py_OptimizeFlag);
- SetFlag(Py_DontWriteBytecodeFlag);
- SetFlag(Py_NoUserSiteDirectory);
- SetFlag(Py_NoSiteFlag);
- SetFlag(Py_IgnoreEnvironmentFlag);
- SetFlag(Py_VerboseFlag);
+ PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag))
+
+ SetFlag(Py_DebugFlag);
+ SetFlag(Py_DivisionWarningFlag);
+ SetFlag(Py_InspectFlag);
+ SetFlag(Py_InteractiveFlag);
+ SetFlag(Py_OptimizeFlag);
+ SetFlag(Py_DontWriteBytecodeFlag);
+ SetFlag(Py_NoUserSiteDirectory);
+ SetFlag(Py_NoSiteFlag);
+ SetFlag(Py_IgnoreEnvironmentFlag);
+ SetFlag(Py_VerboseFlag);
#ifdef RISCOS
- SetFlag(Py_RISCOSWimpFlag);
+ SetFlag(Py_RISCOSWimpFlag);
#endif
- /* SetFlag(saw_unbuffered_flag); */
- /* SetFlag(skipfirstline); */
+ /* SetFlag(saw_unbuffered_flag); */
+ /* SetFlag(skipfirstline); */
SetFlag(Py_BytesWarningFlag);
#undef SetFlag
- if (PyErr_Occurred()) {
- return NULL;
- }
- return seq;
+ if (PyErr_Occurred()) {
+ return NULL;
+ }
+ return seq;
}
PyDoc_STRVAR(version_info__doc__,
@@ -1237,320 +1237,320 @@ Version information as a named tuple.");
static PyTypeObject VersionInfoType;
static PyStructSequence_Field version_info_fields[] = {
- {"major", "Major release number"},
- {"minor", "Minor release number"},
- {"micro", "Patch release number"},
- {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
- {"serial", "Serial release number"},
- {0}
+ {"major", "Major release number"},
+ {"minor", "Minor release number"},
+ {"micro", "Patch release number"},
+ {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"},
+ {"serial", "Serial release number"},
+ {0}
};
static PyStructSequence_Desc version_info_desc = {
- "sys.version_info", /* name */
- version_info__doc__, /* doc */
- version_info_fields, /* fields */
- 5
+ "sys.version_info", /* name */
+ version_info__doc__, /* doc */
+ version_info_fields, /* fields */
+ 5
};
static PyObject *
make_version_info(void)
{
- PyObject *version_info;
- char *s;
- int pos = 0;
-
- version_info = PyStructSequence_New(&VersionInfoType);
- if (version_info == NULL) {
- return NULL;
- }
-
- /*
- * These release level checks are mutually exclusive and cover
- * the field, so don't get too fancy with the pre-processor!
- */
+ PyObject *version_info;
+ char *s;
+ int pos = 0;
+
+ version_info = PyStructSequence_New(&VersionInfoType);
+ if (version_info == NULL) {
+ return NULL;
+ }
+
+ /*
+ * These release level checks are mutually exclusive and cover
+ * the field, so don't get too fancy with the pre-processor!
+ */
#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
- s = "alpha";
+ s = "alpha";
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
- s = "beta";
+ s = "beta";
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
- s = "candidate";
+ s = "candidate";
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
- s = "final";
+ s = "final";
#endif
#define SetIntItem(flag) \
- PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
+ PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag))
#define SetStrItem(flag) \
- PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
+ PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag))
- SetIntItem(PY_MAJOR_VERSION);
- SetIntItem(PY_MINOR_VERSION);
- SetIntItem(PY_MICRO_VERSION);
- SetStrItem(s);
- SetIntItem(PY_RELEASE_SERIAL);
+ SetIntItem(PY_MAJOR_VERSION);
+ SetIntItem(PY_MINOR_VERSION);
+ SetIntItem(PY_MICRO_VERSION);
+ SetStrItem(s);
+ SetIntItem(PY_RELEASE_SERIAL);
#undef SetIntItem
#undef SetStrItem
- if (PyErr_Occurred()) {
- Py_CLEAR(version_info);
- return NULL;
- }
- return version_info;
+ if (PyErr_Occurred()) {
+ Py_CLEAR(version_info);
+ return NULL;
+ }
+ return version_info;
}
static struct PyModuleDef sysmodule = {
- PyModuleDef_HEAD_INIT,
- "sys",
- sys_doc,
- -1, /* multiple "initialization" just copies the module dict. */
- sys_methods,
- NULL,
- NULL,
- NULL,
- NULL
+ PyModuleDef_HEAD_INIT,
+ "sys",
+ sys_doc,
+ -1, /* multiple "initialization" just copies the module dict. */
+ sys_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
};
PyObject *
_PySys_Init(void)
{
- PyObject *m, *v, *sysdict;
- char *s;
-
- m = PyModule_Create(&sysmodule);
- if (m == NULL)
- return NULL;
- sysdict = PyModule_GetDict(m);
-#define SET_SYS_FROM_STRING(key, value) \
- v = value; \
- if (v != NULL) \
- PyDict_SetItemString(sysdict, key, v); \
- Py_XDECREF(v)
-
- /* Check that stdin is not a directory
- Using shell redirection, you can redirect stdin to a directory,
- crashing the Python interpreter. Catch this common mistake here
- and output a useful error message. Note that under MS Windows,
- the shell already prevents that. */
+ PyObject *m, *v, *sysdict;
+ char *s;
+
+ m = PyModule_Create(&sysmodule);
+ if (m == NULL)
+ return NULL;
+ sysdict = PyModule_GetDict(m);
+#define SET_SYS_FROM_STRING(key, value) \
+ v = value; \
+ if (v != NULL) \
+ PyDict_SetItemString(sysdict, key, v); \
+ Py_XDECREF(v)
+
+ /* Check that stdin is not a directory
+ Using shell redirection, you can redirect stdin to a directory,
+ crashing the Python interpreter. Catch this common mistake here
+ and output a useful error message. Note that under MS Windows,
+ the shell already prevents that. */
#if !defined(MS_WINDOWS)
- {
- struct stat sb;
- if (fstat(fileno(stdin), &sb) == 0 &&
- S_ISDIR(sb.st_mode)) {
- /* There's nothing more we can do. */
- /* Py_FatalError() will core dump, so just exit. */
- PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
- exit(EXIT_FAILURE);
- }
- }
+ {
+ struct stat sb;
+ if (fstat(fileno(stdin), &sb) == 0 &&
+ S_ISDIR(sb.st_mode)) {
+ /* There's nothing more we can do. */
+ /* Py_FatalError() will core dump, so just exit. */
+ PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
+ exit(EXIT_FAILURE);
+ }
+ }
#endif
- /* stdin/stdout/stderr are now set by pythonrun.c */
-
- PyDict_SetItemString(sysdict, "__displayhook__",
- PyDict_GetItemString(sysdict, "displayhook"));
- PyDict_SetItemString(sysdict, "__excepthook__",
- PyDict_GetItemString(sysdict, "excepthook"));
- SET_SYS_FROM_STRING("version",
- PyUnicode_FromString(Py_GetVersion()));
- SET_SYS_FROM_STRING("hexversion",
- PyLong_FromLong(PY_VERSION_HEX));
- svnversion_init();
- SET_SYS_FROM_STRING("subversion",
- Py_BuildValue("(UUU)", "CPython", branch,
- svn_revision));
- SET_SYS_FROM_STRING("dont_write_bytecode",
- PyBool_FromLong(Py_DontWriteBytecodeFlag));
- SET_SYS_FROM_STRING("api_version",
- PyLong_FromLong(PYTHON_API_VERSION));
- SET_SYS_FROM_STRING("copyright",
- PyUnicode_FromString(Py_GetCopyright()));
- SET_SYS_FROM_STRING("platform",
- PyUnicode_FromString(Py_GetPlatform()));
- SET_SYS_FROM_STRING("executable",
- PyUnicode_FromWideChar(
- Py_GetProgramFullPath(), -1));
- SET_SYS_FROM_STRING("prefix",
- PyUnicode_FromWideChar(Py_GetPrefix(), -1));
- SET_SYS_FROM_STRING("exec_prefix",
- PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
- SET_SYS_FROM_STRING("maxsize",
- PyLong_FromSsize_t(PY_SSIZE_T_MAX));
- SET_SYS_FROM_STRING("float_info",
- PyFloat_GetInfo());
- SET_SYS_FROM_STRING("int_info",
- PyLong_GetInfo());
- SET_SYS_FROM_STRING("maxunicode",
- PyLong_FromLong(PyUnicode_GetMax()));
- SET_SYS_FROM_STRING("builtin_module_names",
- list_builtin_module_names());
- {
- /* Assumes that longs are at least 2 bytes long.
- Should be safe! */
- unsigned long number = 1;
- char *value;
-
- s = (char *) &number;
- if (s[0] == 0)
- value = "big";
- else
- value = "little";
- SET_SYS_FROM_STRING("byteorder",
- PyUnicode_FromString(value));
- }
+ /* stdin/stdout/stderr are now set by pythonrun.c */
+
+ PyDict_SetItemString(sysdict, "__displayhook__",
+ PyDict_GetItemString(sysdict, "displayhook"));
+ PyDict_SetItemString(sysdict, "__excepthook__",
+ PyDict_GetItemString(sysdict, "excepthook"));
+ SET_SYS_FROM_STRING("version",
+ PyUnicode_FromString(Py_GetVersion()));
+ SET_SYS_FROM_STRING("hexversion",
+ PyLong_FromLong(PY_VERSION_HEX));
+ svnversion_init();
+ SET_SYS_FROM_STRING("subversion",
+ Py_BuildValue("(UUU)", "CPython", branch,
+ svn_revision));
+ SET_SYS_FROM_STRING("dont_write_bytecode",
+ PyBool_FromLong(Py_DontWriteBytecodeFlag));
+ SET_SYS_FROM_STRING("api_version",
+ PyLong_FromLong(PYTHON_API_VERSION));
+ SET_SYS_FROM_STRING("copyright",
+ PyUnicode_FromString(Py_GetCopyright()));
+ SET_SYS_FROM_STRING("platform",
+ PyUnicode_FromString(Py_GetPlatform()));
+ SET_SYS_FROM_STRING("executable",
+ PyUnicode_FromWideChar(
+ Py_GetProgramFullPath(), -1));
+ SET_SYS_FROM_STRING("prefix",
+ PyUnicode_FromWideChar(Py_GetPrefix(), -1));
+ SET_SYS_FROM_STRING("exec_prefix",
+ PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
+ SET_SYS_FROM_STRING("maxsize",
+ PyLong_FromSsize_t(PY_SSIZE_T_MAX));
+ SET_SYS_FROM_STRING("float_info",
+ PyFloat_GetInfo());
+ SET_SYS_FROM_STRING("int_info",
+ PyLong_GetInfo());
+ SET_SYS_FROM_STRING("maxunicode",
+ PyLong_FromLong(PyUnicode_GetMax()));
+ SET_SYS_FROM_STRING("builtin_module_names",
+ list_builtin_module_names());
+ {
+ /* Assumes that longs are at least 2 bytes long.
+ Should be safe! */
+ unsigned long number = 1;
+ char *value;
+
+ s = (char *) &number;
+ if (s[0] == 0)
+ value = "big";
+ else
+ value = "little";
+ SET_SYS_FROM_STRING("byteorder",
+ PyUnicode_FromString(value));
+ }
#ifdef MS_COREDLL
- SET_SYS_FROM_STRING("dllhandle",
- PyLong_FromVoidPtr(PyWin_DLLhModule));
- SET_SYS_FROM_STRING("winver",
- PyUnicode_FromString(PyWin_DLLVersionString));
+ SET_SYS_FROM_STRING("dllhandle",
+ PyLong_FromVoidPtr(PyWin_DLLhModule));
+ SET_SYS_FROM_STRING("winver",
+ PyUnicode_FromString(PyWin_DLLVersionString));
#endif
- if (warnoptions == NULL) {
- warnoptions = PyList_New(0);
- }
- else {
- Py_INCREF(warnoptions);
- }
- if (warnoptions != NULL) {
- PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
- }
-
- /* version_info */
- if (VersionInfoType.tp_name == 0)
- PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
- SET_SYS_FROM_STRING("version_info", make_version_info());
- /* prevent user from creating new instances */
- VersionInfoType.tp_init = NULL;
- VersionInfoType.tp_new = NULL;
-
- /* flags */
- if (FlagsType.tp_name == 0)
- PyStructSequence_InitType(&FlagsType, &flags_desc);
- SET_SYS_FROM_STRING("flags", make_flags());
- /* prevent user from creating new instances */
- FlagsType.tp_init = NULL;
- FlagsType.tp_new = NULL;
-
- /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
+ if (warnoptions == NULL) {
+ warnoptions = PyList_New(0);
+ }
+ else {
+ Py_INCREF(warnoptions);
+ }
+ if (warnoptions != NULL) {
+ PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
+ }
+
+ /* version_info */
+ if (VersionInfoType.tp_name == 0)
+ PyStructSequence_InitType(&VersionInfoType, &version_info_desc);
+ SET_SYS_FROM_STRING("version_info", make_version_info());
+ /* prevent user from creating new instances */
+ VersionInfoType.tp_init = NULL;
+ VersionInfoType.tp_new = NULL;
+
+ /* flags */
+ if (FlagsType.tp_name == 0)
+ PyStructSequence_InitType(&FlagsType, &flags_desc);
+ SET_SYS_FROM_STRING("flags", make_flags());
+ /* prevent user from creating new instances */
+ FlagsType.tp_init = NULL;
+ FlagsType.tp_new = NULL;
+
+ /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */
#ifndef PY_NO_SHORT_FLOAT_REPR
- SET_SYS_FROM_STRING("float_repr_style",
- PyUnicode_FromString("short"));
+ SET_SYS_FROM_STRING("float_repr_style",
+ PyUnicode_FromString("short"));
#else
- SET_SYS_FROM_STRING("float_repr_style",
- PyUnicode_FromString("legacy"));
+ SET_SYS_FROM_STRING("float_repr_style",
+ PyUnicode_FromString("legacy"));
#endif
#undef SET_SYS_FROM_STRING
- if (PyErr_Occurred())
- return NULL;
- return m;
+ if (PyErr_Occurred())
+ return NULL;
+ return m;
}
static PyObject *
makepathobject(const wchar_t *path, wchar_t delim)
{
- int i, n;
- const wchar_t *p;
- PyObject *v, *w;
-
- n = 1;
- p = path;
- while ((p = wcschr(p, delim)) != NULL) {
- n++;
- p++;
- }
- v = PyList_New(n);
- if (v == NULL)
- return NULL;
- for (i = 0; ; i++) {
- p = wcschr(path, delim);
- if (p == NULL)
- p = path + wcslen(path); /* End of string */
- w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
- if (w == NULL) {
- Py_DECREF(v);
- return NULL;
- }
- PyList_SetItem(v, i, w);
- if (*p == '\0')
- break;
- path = p+1;
- }
- return v;
+ int i, n;
+ const wchar_t *p;
+ PyObject *v, *w;
+
+ n = 1;
+ p = path;
+ while ((p = wcschr(p, delim)) != NULL) {
+ n++;
+ p++;
+ }
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ for (i = 0; ; i++) {
+ p = wcschr(path, delim);
+ if (p == NULL)
+ p = path + wcslen(path); /* End of string */
+ w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path));
+ if (w == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyList_SetItem(v, i, w);
+ if (*p == '\0')
+ break;
+ path = p+1;
+ }
+ return v;
}
void
PySys_SetPath(const wchar_t *path)
{
- PyObject *v;
- if ((v = makepathobject(path, DELIM)) == NULL)
- Py_FatalError("can't create sys.path");
- if (PySys_SetObject("path", v) != 0)
- Py_FatalError("can't assign sys.path");
- Py_DECREF(v);
+ PyObject *v;
+ if ((v = makepathobject(path, DELIM)) == NULL)
+ Py_FatalError("can't create sys.path");
+ if (PySys_SetObject("path", v) != 0)
+ Py_FatalError("can't assign sys.path");
+ Py_DECREF(v);
}
static PyObject *
makeargvobject(int argc, wchar_t **argv)
{
- PyObject *av;
- if (argc <= 0 || argv == NULL) {
- /* Ensure at least one (empty) argument is seen */
- static wchar_t *empty_argv[1] = {L""};
- argv = empty_argv;
- argc = 1;
- }
- av = PyList_New(argc);
- if (av != NULL) {
- int i;
- for (i = 0; i < argc; i++) {
+ PyObject *av;
+ if (argc <= 0 || argv == NULL) {
+ /* Ensure at least one (empty) argument is seen */
+ static wchar_t *empty_argv[1] = {L""};
+ argv = empty_argv;
+ argc = 1;
+ }
+ av = PyList_New(argc);
+ if (av != NULL) {
+ int i;
+ for (i = 0; i < argc; i++) {
#ifdef __VMS
- PyObject *v;
-
- /* argv[0] is the script pathname if known */
- if (i == 0) {
- char* fn = decc$translate_vms(argv[0]);
- if ((fn == (char *)0) || fn == (char *)-1)
- v = PyUnicode_FromString(argv[0]);
- else
- v = PyUnicode_FromString(
- decc$translate_vms(argv[0]));
- } else
- v = PyUnicode_FromString(argv[i]);
+ PyObject *v;
+
+ /* argv[0] is the script pathname if known */
+ if (i == 0) {
+ char* fn = decc$translate_vms(argv[0]);
+ if ((fn == (char *)0) || fn == (char *)-1)
+ v = PyUnicode_FromString(argv[0]);
+ else
+ v = PyUnicode_FromString(
+ decc$translate_vms(argv[0]));
+ } else
+ v = PyUnicode_FromString(argv[i]);
#else
- PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
+ PyObject *v = PyUnicode_FromWideChar(argv[i], -1);
#endif
- if (v == NULL) {
- Py_DECREF(av);
- av = NULL;
- break;
- }
- PyList_SetItem(av, i, v);
- }
- }
- return av;
+ if (v == NULL) {
+ Py_DECREF(av);
+ av = NULL;
+ break;
+ }
+ PyList_SetItem(av, i, v);
+ }
+ }
+ return av;
}
#ifdef HAVE_REALPATH
static wchar_t*
_wrealpath(const wchar_t *path, wchar_t *resolved_path)
{
- char cpath[PATH_MAX];
- char cresolved_path[PATH_MAX];
- char *res;
- size_t r;
- r = wcstombs(cpath, path, PATH_MAX);
- if (r == (size_t)-1 || r >= PATH_MAX) {
- errno = EINVAL;
- return NULL;
- }
- res = realpath(cpath, cresolved_path);
- if (res == NULL)
- return NULL;
- r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);
- if (r == (size_t)-1 || r >= PATH_MAX) {
- errno = EINVAL;
- return NULL;
- }
- return resolved_path;
+ char cpath[PATH_MAX];
+ char cresolved_path[PATH_MAX];
+ char *res;
+ size_t r;
+ r = wcstombs(cpath, path, PATH_MAX);
+ if (r == (size_t)-1 || r >= PATH_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ res = realpath(cpath, cresolved_path);
+ if (res == NULL)
+ return NULL;
+ r = mbstowcs(resolved_path, cresolved_path, PATH_MAX);
+ if (r == (size_t)-1 || r >= PATH_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return resolved_path;
}
#endif
@@ -1558,101 +1558,101 @@ void
PySys_SetArgv(int argc, wchar_t **argv)
{
#if defined(HAVE_REALPATH)
- wchar_t fullpath[MAXPATHLEN];
+ wchar_t fullpath[MAXPATHLEN];
#elif defined(MS_WINDOWS) && !defined(MS_WINCE)
- wchar_t fullpath[MAX_PATH];
+ wchar_t fullpath[MAX_PATH];
#endif
- PyObject *av = makeargvobject(argc, argv);
- PyObject *path = PySys_GetObject("path");
- if (av == NULL)
- Py_FatalError("no mem for sys.argv");
- if (PySys_SetObject("argv", av) != 0)
- Py_FatalError("can't assign sys.argv");
- if (path != NULL) {
- wchar_t *argv0 = argv[0];
- wchar_t *p = NULL;
- Py_ssize_t n = 0;
- PyObject *a;
- extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
+ PyObject *av = makeargvobject(argc, argv);
+ PyObject *path = PySys_GetObject("path");
+ if (av == NULL)
+ Py_FatalError("no mem for sys.argv");
+ if (PySys_SetObject("argv", av) != 0)
+ Py_FatalError("can't assign sys.argv");
+ if (path != NULL) {
+ wchar_t *argv0 = argv[0];
+ wchar_t *p = NULL;
+ Py_ssize_t n = 0;
+ PyObject *a;
+ extern int _Py_wreadlink(const wchar_t *, wchar_t *, size_t);
#ifdef HAVE_READLINK
- wchar_t link[MAXPATHLEN+1];
- wchar_t argv0copy[2*MAXPATHLEN+1];
- int nr = 0;
- if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0)
- nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
- if (nr > 0) {
- /* It's a symlink */
- link[nr] = '\0';
- if (link[0] == SEP)
- argv0 = link; /* Link to absolute path */
- else if (wcschr(link, SEP) == NULL)
- ; /* Link without path */
- else {
- /* Must join(dirname(argv0), link) */
- wchar_t *q = wcsrchr(argv0, SEP);
- if (q == NULL)
- argv0 = link; /* argv0 without path */
- else {
- /* Must make a copy */
- wcscpy(argv0copy, argv0);
- q = wcsrchr(argv0copy, SEP);
- wcscpy(q+1, link);
- argv0 = argv0copy;
- }
- }
- }
+ wchar_t link[MAXPATHLEN+1];
+ wchar_t argv0copy[2*MAXPATHLEN+1];
+ int nr = 0;
+ if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0)
+ nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
+ if (nr > 0) {
+ /* It's a symlink */
+ link[nr] = '\0';
+ if (link[0] == SEP)
+ argv0 = link; /* Link to absolute path */
+ else if (wcschr(link, SEP) == NULL)
+ ; /* Link without path */
+ else {
+ /* Must join(dirname(argv0), link) */
+ wchar_t *q = wcsrchr(argv0, SEP);
+ if (q == NULL)
+ argv0 = link; /* argv0 without path */
+ else {
+ /* Must make a copy */
+ wcscpy(argv0copy, argv0);
+ q = wcsrchr(argv0copy, SEP);
+ wcscpy(q+1, link);
+ argv0 = argv0copy;
+ }
+ }
+ }
#endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */
- if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
- wchar_t *q;
+ if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
+ wchar_t *q;
#if defined(MS_WINDOWS) && !defined(MS_WINCE)
- /* This code here replaces the first element in argv with the full
- path that it represents. Under CE, there are no relative paths so
- the argument must be the full path anyway. */
- wchar_t *ptemp;
- if (GetFullPathNameW(argv0,
- sizeof(fullpath)/sizeof(fullpath[0]),
- fullpath,
- &ptemp)) {
- argv0 = fullpath;
- }
+ /* This code here replaces the first element in argv with the full
+ path that it represents. Under CE, there are no relative paths so
+ the argument must be the full path anyway. */
+ wchar_t *ptemp;
+ if (GetFullPathNameW(argv0,
+ sizeof(fullpath)/sizeof(fullpath[0]),
+ fullpath,
+ &ptemp)) {
+ argv0 = fullpath;
+ }
#endif
- p = wcsrchr(argv0, SEP);
- /* Test for alternate separator */
- q = wcsrchr(p ? p : argv0, '/');
- if (q != NULL)
- p = q;
- if (p != NULL) {
- n = p + 1 - argv0;
- if (n > 1 && p[-1] != ':')
- n--; /* Drop trailing separator */
- }
- }
+ p = wcsrchr(argv0, SEP);
+ /* Test for alternate separator */
+ q = wcsrchr(p ? p : argv0, '/');
+ if (q != NULL)
+ p = q;
+ if (p != NULL) {
+ n = p + 1 - argv0;
+ if (n > 1 && p[-1] != ':')
+ n--; /* Drop trailing separator */
+ }
+ }
#else /* All other filename syntaxes */
- if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
+ if (argc > 0 && argv0 != NULL && wcscmp(argv0, L"-c") != 0) {
#if defined(HAVE_REALPATH)
- if (_wrealpath(argv0, fullpath)) {
- argv0 = fullpath;
- }
+ if (_wrealpath(argv0, fullpath)) {
+ argv0 = fullpath;
+ }
#endif
- p = wcsrchr(argv0, SEP);
- }
- if (p != NULL) {
- n = p + 1 - argv0;
+ p = wcsrchr(argv0, SEP);
+ }
+ if (p != NULL) {
+ n = p + 1 - argv0;
#if SEP == '/' /* Special case for Unix filename syntax */
- if (n > 1)
- n--; /* Drop trailing separator */
+ if (n > 1)
+ n--; /* Drop trailing separator */
#endif /* Unix */
- }
+ }
#endif /* All others */
- a = PyUnicode_FromWideChar(argv0, n);
- if (a == NULL)
- Py_FatalError("no mem for sys.path insertion");
- if (PyList_Insert(path, 0, a) < 0)
- Py_FatalError("sys.path.insert(0) failed");
- Py_DECREF(a);
- }
- Py_DECREF(av);
+ a = PyUnicode_FromWideChar(argv0, n);
+ if (a == NULL)
+ Py_FatalError("no mem for sys.path insertion");
+ if (PyList_Insert(path, 0, a) < 0)
+ Py_FatalError("sys.path.insert(0) failed");
+ Py_DECREF(a);
+ }
+ Py_DECREF(av);
}
/* Reimplementation of PyFile_WriteString() no calling indirectly
@@ -1661,37 +1661,37 @@ PySys_SetArgv(int argc, wchar_t **argv)
static int
sys_pyfile_write(const char *text, PyObject *file)
{
- PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL;
- int err;
+ PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL;
+ int err;
- unicode = PyUnicode_FromString(text);
- if (unicode == NULL)
- goto error;
+ unicode = PyUnicode_FromString(text);
+ if (unicode == NULL)
+ goto error;
- writer = PyObject_GetAttrString(file, "write");
- if (writer == NULL)
- goto error;
+ writer = PyObject_GetAttrString(file, "write");
+ if (writer == NULL)
+ goto error;
- args = PyTuple_Pack(1, unicode);
- if (args == NULL)
- goto error;
+ args = PyTuple_Pack(1, unicode);
+ if (args == NULL)
+ goto error;
- result = PyEval_CallObject(writer, args);
- if (result == NULL) {
- goto error;
- } else {
- err = 0;
- goto finally;
- }
+ result = PyEval_CallObject(writer, args);
+ if (result == NULL) {
+ goto error;
+ } else {
+ err = 0;
+ goto finally;
+ }
error:
- err = -1;
+ err = -1;
finally:
- Py_XDECREF(unicode);
- Py_XDECREF(writer);
- Py_XDECREF(args);
- Py_XDECREF(result);
- return err;
+ Py_XDECREF(unicode);
+ Py_XDECREF(writer);
+ Py_XDECREF(args);
+ Py_XDECREF(result);
+ return err;
}
@@ -1726,44 +1726,44 @@ finally:
static void
mywrite(char *name, FILE *fp, const char *format, va_list va)
{
- PyObject *file;
- PyObject *error_type, *error_value, *error_traceback;
- char buffer[1001];
- int written;
-
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
- file = PySys_GetObject(name);
- written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
- if (sys_pyfile_write(buffer, file) != 0) {
- PyErr_Clear();
- fputs(buffer, fp);
- }
- if (written < 0 || (size_t)written >= sizeof(buffer)) {
- const char *truncated = "... truncated";
- if (sys_pyfile_write(truncated, file) != 0) {
- PyErr_Clear();
- fputs(truncated, fp);
- }
- }
- PyErr_Restore(error_type, error_value, error_traceback);
+ PyObject *file;
+ PyObject *error_type, *error_value, *error_traceback;
+ char buffer[1001];
+ int written;
+
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+ file = PySys_GetObject(name);
+ written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
+ if (sys_pyfile_write(buffer, file) != 0) {
+ PyErr_Clear();
+ fputs(buffer, fp);
+ }
+ if (written < 0 || (size_t)written >= sizeof(buffer)) {
+ const char *truncated = "... truncated";
+ if (sys_pyfile_write(truncated, file) != 0) {
+ PyErr_Clear();
+ fputs(truncated, fp);
+ }
+ }
+ PyErr_Restore(error_type, error_value, error_traceback);
}
void
PySys_WriteStdout(const char *format, ...)
{
- va_list va;
+ va_list va;
- va_start(va, format);
- mywrite("stdout", stdout, format, va);
- va_end(va);
+ va_start(va, format);
+ mywrite("stdout", stdout, format, va);
+ va_end(va);
}
void
PySys_WriteStderr(const char *format, ...)
{
- va_list va;
+ va_list va;
- va_start(va, format);
- mywrite("stderr", stderr, format, va);
- va_end(va);
+ va_start(va, format);
+ mywrite("stderr", stderr, format, va);
+ va_end(va);
}
diff --git a/Python/thread.c b/Python/thread.c
index 1d7bed9..b4e3ad0 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -46,7 +46,7 @@
#endif
/* Check if we're running on HP-UX and _SC_THREADS is defined. If so, then
- enough of the Posix threads package is implimented to support python
+ enough of the Posix threads package is implimented to support python
threads.
This is valid for HP-UX 11.23 running on an ia64 system. If needed, add
@@ -64,8 +64,8 @@
#ifdef Py_DEBUG
static int thread_debug = 0;
-#define dprintf(args) (void)((thread_debug & 1) && printf args)
-#define d2printf(args) ((thread_debug & 8) && printf args)
+#define dprintf(args) (void)((thread_debug & 1) && printf args)
+#define d2printf(args) ((thread_debug & 8) && printf args)
#else
#define dprintf(args)
#define d2printf(args)
@@ -79,20 +79,20 @@ void
PyThread_init_thread(void)
{
#ifdef Py_DEBUG
- char *p = Py_GETENV("PYTHONTHREADDEBUG");
-
- if (p) {
- if (*p)
- thread_debug = atoi(p);
- else
- thread_debug = 1;
- }
+ char *p = Py_GETENV("PYTHONTHREADDEBUG");
+
+ if (p) {
+ if (*p)
+ thread_debug = atoi(p);
+ else
+ thread_debug = 1;
+ }
#endif /* Py_DEBUG */
- if (initialized)
- return;
- initialized = 1;
- dprintf(("PyThread_init_thread called\n"));
- PyThread__init_thread();
+ if (initialized)
+ return;
+ initialized = 1;
+ dprintf(("PyThread_init_thread called\n"));
+ PyThread__init_thread();
}
/* Support for runtime thread stack size tuning.
@@ -151,21 +151,21 @@ static size_t _pythread_stacksize = 0;
size_t
PyThread_get_stacksize(void)
{
- return _pythread_stacksize;
+ return _pythread_stacksize;
}
/* Only platforms defining a THREAD_SET_STACKSIZE() macro
in thread_<platform>.h support changing the stack size.
Return 0 if stack size is valid,
- -1 if stack size value is invalid,
- -2 if setting stack size is not supported. */
+ -1 if stack size value is invalid,
+ -2 if setting stack size is not supported. */
int
PyThread_set_stacksize(size_t size)
{
#if defined(THREAD_SET_STACKSIZE)
- return THREAD_SET_STACKSIZE(size);
+ return THREAD_SET_STACKSIZE(size);
#else
- return -2;
+ return -2;
#endif
}
@@ -217,15 +217,15 @@ that calls to PyThread_create_key() are serialized externally.
* to enforce exclusion internally.
*/
struct key {
- /* Next record in the list, or NULL if this is the last record. */
- struct key *next;
+ /* Next record in the list, or NULL if this is the last record. */
+ struct key *next;
- /* The thread id, according to PyThread_get_thread_ident(). */
- long id;
+ /* The thread id, according to PyThread_get_thread_ident(). */
+ long id;
- /* The key and its associated value. */
- int key;
- void *value;
+ /* The key and its associated value. */
+ int key;
+ void *value;
};
static struct key *keyhead = NULL;
@@ -256,41 +256,41 @@ static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */
static struct key *
find_key(int key, void *value)
{
- struct key *p, *prev_p;
- long id = PyThread_get_thread_ident();
-
- if (!keymutex)
- return NULL;
- PyThread_acquire_lock(keymutex, 1);
- prev_p = NULL;
- for (p = keyhead; p != NULL; p = p->next) {
- if (p->id == id && p->key == key)
- goto Done;
- /* Sanity check. These states should never happen but if
- * they do we must abort. Otherwise we'll end up spinning in
- * in a tight loop with the lock held. A similar check is done
- * in pystate.c tstate_delete_common(). */
- if (p == prev_p)
- Py_FatalError("tls find_key: small circular list(!)");
- prev_p = p;
- if (p->next == keyhead)
- Py_FatalError("tls find_key: circular list(!)");
- }
- if (value == NULL) {
- assert(p == NULL);
- goto Done;
- }
- p = (struct key *)malloc(sizeof(struct key));
- if (p != NULL) {
- p->id = id;
- p->key = key;
- p->value = value;
- p->next = keyhead;
- keyhead = p;
- }
+ struct key *p, *prev_p;
+ long id = PyThread_get_thread_ident();
+
+ if (!keymutex)
+ return NULL;
+ PyThread_acquire_lock(keymutex, 1);
+ prev_p = NULL;
+ for (p = keyhead; p != NULL; p = p->next) {
+ if (p->id == id && p->key == key)
+ goto Done;
+ /* Sanity check. These states should never happen but if
+ * they do we must abort. Otherwise we'll end up spinning in
+ * in a tight loop with the lock held. A similar check is done
+ * in pystate.c tstate_delete_common(). */
+ if (p == prev_p)
+ Py_FatalError("tls find_key: small circular list(!)");
+ prev_p = p;
+ if (p->next == keyhead)
+ Py_FatalError("tls find_key: circular list(!)");
+ }
+ if (value == NULL) {
+ assert(p == NULL);
+ goto Done;
+ }
+ p = (struct key *)malloc(sizeof(struct key));
+ if (p != NULL) {
+ p->id = id;
+ p->key = key;
+ p->value = value;
+ p->next = keyhead;
+ keyhead = p;
+ }
Done:
- PyThread_release_lock(keymutex);
- return p;
+ PyThread_release_lock(keymutex);
+ return p;
}
/* Return a new key. This must be called before any other functions in
@@ -300,32 +300,32 @@ find_key(int key, void *value)
int
PyThread_create_key(void)
{
- /* All parts of this function are wrong if it's called by multiple
- * threads simultaneously.
- */
- if (keymutex == NULL)
- keymutex = PyThread_allocate_lock();
- return ++nkeys;
+ /* All parts of this function are wrong if it's called by multiple
+ * threads simultaneously.
+ */
+ if (keymutex == NULL)
+ keymutex = PyThread_allocate_lock();
+ return ++nkeys;
}
/* Forget the associations for key across *all* threads. */
void
PyThread_delete_key(int key)
{
- struct key *p, **q;
-
- PyThread_acquire_lock(keymutex, 1);
- q = &keyhead;
- while ((p = *q) != NULL) {
- if (p->key == key) {
- *q = p->next;
- free((void *)p);
- /* NB This does *not* free p->value! */
- }
- else
- q = &p->next;
- }
- PyThread_release_lock(keymutex);
+ struct key *p, **q;
+
+ PyThread_acquire_lock(keymutex, 1);
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->key == key) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ }
+ else
+ q = &p->next;
+ }
+ PyThread_release_lock(keymutex);
}
/* Confusing: If the current thread has an association for key,
@@ -337,14 +337,14 @@ PyThread_delete_key(int key)
int
PyThread_set_key_value(int key, void *value)
{
- struct key *p;
-
- assert(value != NULL);
- p = find_key(key, value);
- if (p == NULL)
- return -1;
- else
- return 0;
+ struct key *p;
+
+ assert(value != NULL);
+ p = find_key(key, value);
+ if (p == NULL)
+ return -1;
+ else
+ return 0;
}
/* Retrieve the value associated with key in the current thread, or NULL
@@ -353,34 +353,34 @@ PyThread_set_key_value(int key, void *value)
void *
PyThread_get_key_value(int key)
{
- struct key *p = find_key(key, NULL);
+ struct key *p = find_key(key, NULL);
- if (p == NULL)
- return NULL;
- else
- return p->value;
+ if (p == NULL)
+ return NULL;
+ else
+ return p->value;
}
/* Forget the current thread's association for key, if any. */
void
PyThread_delete_key_value(int key)
{
- long id = PyThread_get_thread_ident();
- struct key *p, **q;
-
- PyThread_acquire_lock(keymutex, 1);
- q = &keyhead;
- while ((p = *q) != NULL) {
- if (p->key == key && p->id == id) {
- *q = p->next;
- free((void *)p);
- /* NB This does *not* free p->value! */
- break;
- }
- else
- q = &p->next;
- }
- PyThread_release_lock(keymutex);
+ long id = PyThread_get_thread_ident();
+ struct key *p, **q;
+
+ PyThread_acquire_lock(keymutex, 1);
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->key == key && p->id == id) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ break;
+ }
+ else
+ q = &p->next;
+ }
+ PyThread_release_lock(keymutex);
}
/* Forget everything not associated with the current thread id.
@@ -391,27 +391,27 @@ PyThread_delete_key_value(int key)
void
PyThread_ReInitTLS(void)
{
- long id = PyThread_get_thread_ident();
- struct key *p, **q;
-
- if (!keymutex)
- return;
-
- /* As with interpreter_lock in PyEval_ReInitThreads()
- we just create a new lock without freeing the old one */
- keymutex = PyThread_allocate_lock();
-
- /* Delete all keys which do not match the current thread id */
- q = &keyhead;
- while ((p = *q) != NULL) {
- if (p->id != id) {
- *q = p->next;
- free((void *)p);
- /* NB This does *not* free p->value! */
- }
- else
- q = &p->next;
- }
+ long id = PyThread_get_thread_ident();
+ struct key *p, **q;
+
+ if (!keymutex)
+ return;
+
+ /* As with interpreter_lock in PyEval_ReInitThreads()
+ we just create a new lock without freeing the old one */
+ keymutex = PyThread_allocate_lock();
+
+ /* Delete all keys which do not match the current thread id */
+ q = &keyhead;
+ while ((p = *q) != NULL) {
+ if (p->id != id) {
+ *q = p->next;
+ free((void *)p);
+ /* NB This does *not* free p->value! */
+ }
+ else
+ q = &p->next;
+ }
}
#endif /* Py_HAVE_NATIVE_TLS */
diff --git a/Python/thread_atheos.h b/Python/thread_atheos.h
index c9f5e23..c390b57 100644
--- a/Python/thread_atheos.h
+++ b/Python/thread_atheos.h
@@ -19,8 +19,8 @@ extern int exit_thread(int);
/* Use an atomic counter and a semaphore for maximum speed. */
typedef struct fastmutex {
- sem_id sem;
- atomic_t count;
+ sem_id sem;
+ atomic_t count;
} fastmutex_t;
@@ -33,49 +33,49 @@ static int fastmutex_unlock(fastmutex_t * mutex);
static int fastmutex_create(const char *name, fastmutex_t * mutex)
{
- mutex->count = 0;
- mutex->sem = create_semaphore(name, 0, 0);
- return (mutex->sem < 0) ? -1 : 0;
+ mutex->count = 0;
+ mutex->sem = create_semaphore(name, 0, 0);
+ return (mutex->sem < 0) ? -1 : 0;
}
static int fastmutex_destroy(fastmutex_t * mutex)
{
- if (fastmutex_timedlock(mutex, 0) == 0 || errno == EWOULDBLOCK) {
- return delete_semaphore(mutex->sem);
- }
- return 0;
+ if (fastmutex_timedlock(mutex, 0) == 0 || errno == EWOULDBLOCK) {
+ return delete_semaphore(mutex->sem);
+ }
+ return 0;
}
static int fastmutex_lock(fastmutex_t * mutex)
{
- atomic_t prev = atomic_add(&mutex->count, 1);
- if (prev > 0)
- return lock_semaphore(mutex->sem);
- return 0;
+ atomic_t prev = atomic_add(&mutex->count, 1);
+ if (prev > 0)
+ return lock_semaphore(mutex->sem);
+ return 0;
}
static int fastmutex_timedlock(fastmutex_t * mutex, bigtime_t timeout)
{
- atomic_t prev = atomic_add(&mutex->count, 1);
- if (prev > 0)
- return lock_semaphore_x(mutex->sem, 1, 0, timeout);
- return 0;
+ atomic_t prev = atomic_add(&mutex->count, 1);
+ if (prev > 0)
+ return lock_semaphore_x(mutex->sem, 1, 0, timeout);
+ return 0;
}
static int fastmutex_unlock(fastmutex_t * mutex)
{
- atomic_t prev = atomic_add(&mutex->count, -1);
- if (prev > 1)
- return unlock_semaphore(mutex->sem);
- return 0;
+ atomic_t prev = atomic_add(&mutex->count, -1);
+ if (prev > 1)
+ return unlock_semaphore(mutex->sem);
+ return 0;
}
-#endif /* FASTLOCK */
+#endif /* FASTLOCK */
/*
@@ -84,8 +84,8 @@ static int fastmutex_unlock(fastmutex_t * mutex)
*/
static void PyThread__init_thread(void)
{
- /* Do nothing. */
- return;
+ /* Do nothing. */
+ return;
}
@@ -98,90 +98,90 @@ static atomic_t thread_count = 0;
long PyThread_start_new_thread(void (*func) (void *), void *arg)
{
- status_t success = -1;
- thread_id tid;
- char name[OS_NAME_LENGTH];
- atomic_t this_thread;
-
- dprintf(("PyThread_start_new_thread called\n"));
-
- this_thread = atomic_add(&thread_count, 1);
- PyOS_snprintf(name, sizeof(name), "python thread (%d)", this_thread);
-
- tid = spawn_thread(name, func, NORMAL_PRIORITY, 0, arg);
- if (tid < 0) {
- dprintf(("PyThread_start_new_thread spawn_thread failed: %s\n", strerror(errno)));
- } else {
- success = resume_thread(tid);
- if (success < 0) {
- dprintf(("PyThread_start_new_thread resume_thread failed: %s\n", strerror(errno)));
- }
- }
-
- return (success < 0 ? -1 : tid);
+ status_t success = -1;
+ thread_id tid;
+ char name[OS_NAME_LENGTH];
+ atomic_t this_thread;
+
+ dprintf(("PyThread_start_new_thread called\n"));
+
+ this_thread = atomic_add(&thread_count, 1);
+ PyOS_snprintf(name, sizeof(name), "python thread (%d)", this_thread);
+
+ tid = spawn_thread(name, func, NORMAL_PRIORITY, 0, arg);
+ if (tid < 0) {
+ dprintf(("PyThread_start_new_thread spawn_thread failed: %s\n", strerror(errno)));
+ } else {
+ success = resume_thread(tid);
+ if (success < 0) {
+ dprintf(("PyThread_start_new_thread resume_thread failed: %s\n", strerror(errno)));
+ }
+ }
+
+ return (success < 0 ? -1 : tid);
}
long PyThread_get_thread_ident(void)
{
- return get_thread_id(NULL);
+ return get_thread_id(NULL);
}
static void do_PyThread_exit_thread(int no_cleanup)
{
- dprintf(("PyThread_exit_thread called\n"));
-
- /* Thread-safe way to read a variable without a mutex: */
- if (atomic_add(&thread_count, 0) == 0) {
- /* No threads around, so exit main(). */
- if (no_cleanup)
- _exit(0);
- else
- exit(0);
- } else {
- /* We're a thread */
- exit_thread(0);
- }
+ dprintf(("PyThread_exit_thread called\n"));
+
+ /* Thread-safe way to read a variable without a mutex: */
+ if (atomic_add(&thread_count, 0) == 0) {
+ /* No threads around, so exit main(). */
+ if (no_cleanup)
+ _exit(0);
+ else
+ exit(0);
+ } else {
+ /* We're a thread */
+ exit_thread(0);
+ }
}
void PyThread_exit_thread(void)
{
- do_PyThread_exit_thread(0);
+ do_PyThread_exit_thread(0);
}
void PyThread__exit_thread(void)
{
- do_PyThread_exit_thread(1);
+ do_PyThread_exit_thread(1);
}
#ifndef NO_EXIT_PROG
static void do_PyThread_exit_prog(int status, int no_cleanup)
{
- dprintf(("PyThread_exit_prog(%d) called\n", status));
+ dprintf(("PyThread_exit_prog(%d) called\n", status));
- /* No need to do anything, the threads get torn down if main()exits. */
- if (no_cleanup)
- _exit(status);
- else
- exit(status);
+ /* No need to do anything, the threads get torn down if main()exits. */
+ if (no_cleanup)
+ _exit(status);
+ else
+ exit(status);
}
void PyThread_exit_prog(int status)
{
- do_PyThread_exit_prog(status, 0);
+ do_PyThread_exit_prog(status, 0);
}
void PyThread__exit_prog(int status)
{
- do_PyThread_exit_prog(status, 1);
+ do_PyThread_exit_prog(status, 1);
}
-#endif /* NO_EXIT_PROG */
+#endif /* NO_EXIT_PROG */
/*
@@ -194,107 +194,107 @@ static atomic_t lock_count = 0;
PyThread_type_lock PyThread_allocate_lock(void)
{
#ifdef FASTLOCK
- fastmutex_t *lock;
+ fastmutex_t *lock;
#else
- sem_id sema;
+ sem_id sema;
#endif
- char name[OS_NAME_LENGTH];
- atomic_t this_lock;
+ char name[OS_NAME_LENGTH];
+ atomic_t this_lock;
- dprintf(("PyThread_allocate_lock called\n"));
+ dprintf(("PyThread_allocate_lock called\n"));
#ifdef FASTLOCK
- lock = (fastmutex_t *) malloc(sizeof(fastmutex_t));
- if (lock == NULL) {
- dprintf(("PyThread_allocate_lock failed: out of memory\n"));
- return (PyThread_type_lock) NULL;
- }
+ lock = (fastmutex_t *) malloc(sizeof(fastmutex_t));
+ if (lock == NULL) {
+ dprintf(("PyThread_allocate_lock failed: out of memory\n"));
+ return (PyThread_type_lock) NULL;
+ }
#endif
- this_lock = atomic_add(&lock_count, 1);
- PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
+ this_lock = atomic_add(&lock_count, 1);
+ PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
#ifdef FASTLOCK
- if (fastmutex_create(name, lock) < 0) {
- dprintf(("PyThread_allocate_lock failed: %s\n",
- strerror(errno)));
- free(lock);
- lock = NULL;
- }
- dprintf(("PyThread_allocate_lock()-> %p\n", lock));
- return (PyThread_type_lock) lock;
+ if (fastmutex_create(name, lock) < 0) {
+ dprintf(("PyThread_allocate_lock failed: %s\n",
+ strerror(errno)));
+ free(lock);
+ lock = NULL;
+ }
+ dprintf(("PyThread_allocate_lock()-> %p\n", lock));
+ return (PyThread_type_lock) lock;
#else
- sema = create_semaphore(name, 1, 0);
- if (sema < 0) {
- dprintf(("PyThread_allocate_lock failed: %s\n",
- strerror(errno)));
- sema = 0;
- }
- dprintf(("PyThread_allocate_lock()-> %p\n", sema));
- return (PyThread_type_lock) sema;
+ sema = create_semaphore(name, 1, 0);
+ if (sema < 0) {
+ dprintf(("PyThread_allocate_lock failed: %s\n",
+ strerror(errno)));
+ sema = 0;
+ }
+ dprintf(("PyThread_allocate_lock()-> %p\n", sema));
+ return (PyThread_type_lock) sema;
#endif
}
void PyThread_free_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_free_lock(%p) called\n", lock));
+ dprintf(("PyThread_free_lock(%p) called\n", lock));
#ifdef FASTLOCK
- if (fastmutex_destroy((fastmutex_t *) lock) < 0) {
- dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
- strerror(errno)));
- }
- free(lock);
+ if (fastmutex_destroy((fastmutex_t *) lock) < 0) {
+ dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
+ strerror(errno)));
+ }
+ free(lock);
#else
- if (delete_semaphore((sem_id) lock) < 0) {
- dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
- strerror(errno)));
- }
+ if (delete_semaphore((sem_id) lock) < 0) {
+ dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
+ strerror(errno)));
+ }
#endif
}
int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
- int retval;
+ int retval;
- dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock,
- waitflag));
+ dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock,
+ waitflag));
#ifdef FASTLOCK
- if (waitflag)
- retval = fastmutex_lock((fastmutex_t *) lock);
- else
- retval = fastmutex_timedlock((fastmutex_t *) lock, 0);
+ if (waitflag)
+ retval = fastmutex_lock((fastmutex_t *) lock);
+ else
+ retval = fastmutex_timedlock((fastmutex_t *) lock, 0);
#else
- if (waitflag)
- retval = lock_semaphore((sem_id) lock);
- else
- retval = lock_semaphore_x((sem_id) lock, 1, 0, 0);
+ if (waitflag)
+ retval = lock_semaphore((sem_id) lock);
+ else
+ retval = lock_semaphore_x((sem_id) lock, 1, 0, 0);
#endif
- if (retval < 0) {
- dprintf(("PyThread_acquire_lock(%p, %d) failed: %s\n",
- lock, waitflag, strerror(errno)));
- }
- dprintf(("PyThread_acquire_lock(%p, %d)-> %d\n", lock, waitflag,
- retval));
- return retval < 0 ? 0 : 1;
+ if (retval < 0) {
+ dprintf(("PyThread_acquire_lock(%p, %d) failed: %s\n",
+ lock, waitflag, strerror(errno)));
+ }
+ dprintf(("PyThread_acquire_lock(%p, %d)-> %d\n", lock, waitflag,
+ retval));
+ return retval < 0 ? 0 : 1;
}
void PyThread_release_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_release_lock(%p) called\n", lock));
+ dprintf(("PyThread_release_lock(%p) called\n", lock));
#ifdef FASTLOCK
- if (fastmutex_unlock((fastmutex_t *) lock) < 0) {
- dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
- strerror(errno)));
- }
+ if (fastmutex_unlock((fastmutex_t *) lock) < 0) {
+ dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
+ strerror(errno)));
+ }
#else
- if (unlock_semaphore((sem_id) lock) < 0) {
- dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
- strerror(errno)));
- }
+ if (unlock_semaphore((sem_id) lock) < 0) {
+ dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
+ strerror(errno)));
+ }
#endif
}
diff --git a/Python/thread_cthread.h b/Python/thread_cthread.h
index ca776c6..8cb5044 100644
--- a/Python/thread_cthread.h
+++ b/Python/thread_cthread.h
@@ -14,12 +14,12 @@ static void
PyThread__init_thread(void)
{
#ifndef HURD_C_THREADS
- /* Roland McGrath said this should not be used since this is
- done while linking to threads */
- cthread_init();
+ /* Roland McGrath said this should not be used since this is
+ done while linking to threads */
+ cthread_init();
#else
/* do nothing */
- ;
+ ;
#endif
}
@@ -29,77 +29,77 @@ PyThread__init_thread(void)
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
- int success = 0; /* init not needed when SOLARIS_THREADS and */
- /* C_THREADS implemented properly */
-
- dprintf(("PyThread_start_new_thread called\n"));
- if (!initialized)
- PyThread_init_thread();
- /* looks like solaris detaches the thread to never rejoin
- * so well do it here
- */
- cthread_detach(cthread_fork((cthread_fn_t) func, arg));
- return success < 0 ? -1 : 0;
+ int success = 0; /* init not needed when SOLARIS_THREADS and */
+ /* C_THREADS implemented properly */
+
+ dprintf(("PyThread_start_new_thread called\n"));
+ if (!initialized)
+ PyThread_init_thread();
+ /* looks like solaris detaches the thread to never rejoin
+ * so well do it here
+ */
+ cthread_detach(cthread_fork((cthread_fn_t) func, arg));
+ return success < 0 ? -1 : 0;
}
long
PyThread_get_thread_ident(void)
{
- if (!initialized)
- PyThread_init_thread();
- return (long) cthread_self();
+ if (!initialized)
+ PyThread_init_thread();
+ return (long) cthread_self();
}
static void
do_PyThread_exit_thread(int no_cleanup)
{
- dprintf(("PyThread_exit_thread called\n"));
- if (!initialized)
- if (no_cleanup)
- _exit(0);
- else
- exit(0);
- cthread_exit(0);
+ dprintf(("PyThread_exit_thread called\n"));
+ if (!initialized)
+ if (no_cleanup)
+ _exit(0);
+ else
+ exit(0);
+ cthread_exit(0);
}
void
PyThread_exit_thread(void)
{
- do_PyThread_exit_thread(0);
+ do_PyThread_exit_thread(0);
}
void
PyThread__exit_thread(void)
{
- do_PyThread_exit_thread(1);
+ do_PyThread_exit_thread(1);
}
#ifndef NO_EXIT_PROG
static
void do_PyThread_exit_prog(int status, int no_cleanup)
{
- dprintf(("PyThread_exit_prog(%d) called\n", status));
- if (!initialized)
- if (no_cleanup)
- _exit(status);
- else
- exit(status);
- if (no_cleanup)
- _exit(status);
- else
- exit(status);
+ dprintf(("PyThread_exit_prog(%d) called\n", status));
+ if (!initialized)
+ if (no_cleanup)
+ _exit(status);
+ else
+ exit(status);
+ if (no_cleanup)
+ _exit(status);
+ else
+ exit(status);
}
void
PyThread_exit_prog(int status)
{
- do_PyThread_exit_prog(status, 0);
+ do_PyThread_exit_prog(status, 0);
}
void
PyThread__exit_prog(int status)
{
- do_PyThread_exit_prog(status, 1);
+ do_PyThread_exit_prog(status, 1);
}
#endif /* NO_EXIT_PROG */
@@ -109,48 +109,48 @@ PyThread__exit_prog(int status)
PyThread_type_lock
PyThread_allocate_lock(void)
{
- mutex_t lock;
-
- dprintf(("PyThread_allocate_lock called\n"));
- if (!initialized)
- PyThread_init_thread();
-
- lock = mutex_alloc();
- if (mutex_init(lock)) {
- perror("mutex_init");
- free((void *) lock);
- lock = 0;
- }
- dprintf(("PyThread_allocate_lock() -> %p\n", lock));
- return (PyThread_type_lock) lock;
+ mutex_t lock;
+
+ dprintf(("PyThread_allocate_lock called\n"));
+ if (!initialized)
+ PyThread_init_thread();
+
+ lock = mutex_alloc();
+ if (mutex_init(lock)) {
+ perror("mutex_init");
+ free((void *) lock);
+ lock = 0;
+ }
+ dprintf(("PyThread_allocate_lock() -> %p\n", lock));
+ return (PyThread_type_lock) lock;
}
void
PyThread_free_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_free_lock(%p) called\n", lock));
- mutex_free(lock);
+ dprintf(("PyThread_free_lock(%p) called\n", lock));
+ mutex_free(lock);
}
int
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
- int success = FALSE;
-
- dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
- if (waitflag) { /* blocking */
- mutex_lock((mutex_t)lock);
- success = TRUE;
- } else { /* non blocking */
- success = mutex_try_lock((mutex_t)lock);
- }
- dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
- return success;
+ int success = FALSE;
+
+ dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
+ if (waitflag) { /* blocking */
+ mutex_lock((mutex_t)lock);
+ success = TRUE;
+ } else { /* non blocking */
+ success = mutex_try_lock((mutex_t)lock);
+ }
+ dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
+ return success;
}
void
PyThread_release_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_release_lock(%p) called\n", lock));
- mutex_unlock((mutex_t )lock);
+ dprintf(("PyThread_release_lock(%p) called\n", lock));
+ mutex_unlock((mutex_t )lock);
}
diff --git a/Python/thread_foobar.h b/Python/thread_foobar.h
index 67491a1..c2dffa6 100644
--- a/Python/thread_foobar.h
+++ b/Python/thread_foobar.h
@@ -13,67 +13,67 @@ PyThread__init_thread(void)
long
PyThread_start_new_thread(void (*func)(void *), void *arg)
{
- int success = 0; /* init not needed when SOLARIS_THREADS and */
- /* C_THREADS implemented properly */
+ int success = 0; /* init not needed when SOLARIS_THREADS and */
+ /* C_THREADS implemented properly */
- dprintf(("PyThread_start_new_thread called\n"));
- if (!initialized)
- PyThread_init_thread();
- return success < 0 ? -1 : 0;
+ dprintf(("PyThread_start_new_thread called\n"));
+ if (!initialized)
+ PyThread_init_thread();
+ return success < 0 ? -1 : 0;
}
long
PyThread_get_thread_ident(void)
{
- if (!initialized)
- PyThread_init_thread();
+ if (!initialized)
+ PyThread_init_thread();
}
static
void do_PyThread_exit_thread(int no_cleanup)
{
- dprintf(("PyThread_exit_thread called\n"));
- if (!initialized)
- if (no_cleanup)
- _exit(0);
- else
- exit(0);
+ dprintf(("PyThread_exit_thread called\n"));
+ if (!initialized)
+ if (no_cleanup)
+ _exit(0);
+ else
+ exit(0);
}
void
PyThread_exit_thread(void)
{
- do_PyThread_exit_thread(0);
+ do_PyThread_exit_thread(0);
}
void
PyThread__exit_thread(void)
{
- do_PyThread_exit_thread(1);
+ do_PyThread_exit_thread(1);
}
#ifndef NO_EXIT_PROG
static
void do_PyThread_exit_prog(int status, int no_cleanup)
{
- dprintf(("PyThread_exit_prog(%d) called\n", status));
- if (!initialized)
- if (no_cleanup)
- _exit(status);
- else
- exit(status);
+ dprintf(("PyThread_exit_prog(%d) called\n", status));
+ if (!initialized)
+ if (no_cleanup)
+ _exit(status);
+ else
+ exit(status);
}
void
PyThread_exit_prog(int status)
{
- do_PyThread_exit_prog(status, 0);
+ do_PyThread_exit_prog(status, 0);
}
void
PyThread__exit_prog(int status)
{
- do_PyThread_exit_prog(status, 1);
+ do_PyThread_exit_prog(status, 1);
}
#endif /* NO_EXIT_PROG */
@@ -84,32 +84,32 @@ PyThread_type_lock
PyThread_allocate_lock(void)
{
- dprintf(("PyThread_allocate_lock called\n"));
- if (!initialized)
- PyThread_init_thread();
+ dprintf(("PyThread_allocate_lock called\n"));
+ if (!initialized)
+ PyThread_init_thread();
- dprintf(("PyThread_allocate_lock() -> %p\n", lock));
- return (PyThread_type_lock) lock;
+ dprintf(("PyThread_allocate_lock() -> %p\n", lock));
+ return (PyThread_type_lock) lock;
}
void
PyThread_free_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_free_lock(%p) called\n", lock));
+ dprintf(("PyThread_free_lock(%p) called\n", lock));
}
int
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
{
- int success;
+ int success;
- dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
- dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
- return success;
+ dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
+ dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
+ return success;
}
void
PyThread_release_lock(PyThread_type_lock lock)
{
- dprintf(("PyThread_release_lock(%p) called\n", lock));
+ dprintf(("PyThread_release_lock(%p) called\n", lock));
}
diff --git a/Python/thread_lwp.h b/Python/thread_lwp.h