mirror of
https://github.com/python/cpython.git
synced 2025-10-27 08:46:53 +00:00
which unfortunately means the errors from the bytes type change somewhat: bytes([300]) still raises a ValueError, but bytes([10**100]) now raises a TypeError (either that, or bytes(1.0) also raises a ValueError -- PyNumber_AsSsize_t() can only raise one type of exception.) Merged revisions 51188-51433 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r51189 | kurt.kaiser | 2006-08-10 19:11:09 +0200 (Thu, 10 Aug 2006) | 4 lines Retrieval of previous shell command was not always preserving indentation since 1.2a1) Patch 1528468 Tal Einat. ........ r51190 | guido.van.rossum | 2006-08-10 19:41:07 +0200 (Thu, 10 Aug 2006) | 3 lines Chris McDonough's patch to defend against certain DoS attacks on FieldStorage. SF bug #1112549. ........ r51191 | guido.van.rossum | 2006-08-10 19:42:50 +0200 (Thu, 10 Aug 2006) | 2 lines News item for SF bug 1112549. ........ r51192 | guido.van.rossum | 2006-08-10 20:09:25 +0200 (Thu, 10 Aug 2006) | 2 lines Fix title -- it's rc1, not beta3. ........ r51194 | martin.v.loewis | 2006-08-10 21:04:00 +0200 (Thu, 10 Aug 2006) | 3 lines Update dangling references to the 3.2 database to mention that this is UCD 4.1 now. ........ r51195 | tim.peters | 2006-08-11 00:45:34 +0200 (Fri, 11 Aug 2006) | 6 lines Followup to bug #1069160. PyThreadState_SetAsyncExc(): internal correctness changes wrt refcount safety and deadlock avoidance. Also added a basic test case (relying on ctypes) and repaired the docs. ........ r51196 | tim.peters | 2006-08-11 00:48:45 +0200 (Fri, 11 Aug 2006) | 2 lines Whitespace normalization. ........ r51197 | tim.peters | 2006-08-11 01:22:13 +0200 (Fri, 11 Aug 2006) | 5 lines Whitespace normalization broke test_cgi, because a line of quoted test data relied on preserving a single trailing blank. Changed the string from raw to regular, and forced in the trailing blank via an explicit \x20 escape. ........ r51198 | tim.peters | 2006-08-11 02:49:01 +0200 (Fri, 11 Aug 2006) | 10 lines test_PyThreadState_SetAsyncExc(): This is failing on some 64-bit boxes. I have no idea what the ctypes docs mean by "integers", and blind-guessing here that it intended to mean the signed C "int" type, in which case perhaps I can repair this by feeding the thread id argument to type ctypes.c_long(). Also made the worker thread daemonic, so it doesn't hang Python shutdown if the test continues to fail. ........ r51199 | tim.peters | 2006-08-11 05:49:10 +0200 (Fri, 11 Aug 2006) | 6 lines force_test_exit(): This has been completely ineffective at stopping test_signal from hanging forever on the Tru64 buildbot. That could be because there's no such thing as signal.SIGALARM. Changed to the idiotic (but standard) signal.SIGALRM instead, and added some more debug output. ........ r51202 | neal.norwitz | 2006-08-11 08:09:41 +0200 (Fri, 11 Aug 2006) | 6 lines Fix the failures on cygwin (2006-08-10 fixed the actual locking issue). The first hunk changes the colon to an ! like other Windows variants. We need to always wait on the child so the lock gets released and no other tests fail. This is the try/finally in the second hunk. ........ r51205 | georg.brandl | 2006-08-11 09:15:38 +0200 (Fri, 11 Aug 2006) | 3 lines Add Chris McDonough (latest cgi.py patch) ........ r51206 | georg.brandl | 2006-08-11 09:26:10 +0200 (Fri, 11 Aug 2006) | 3 lines logging's atexit hook now runs even if the rest of the module has already been cleaned up. ........ r51212 | thomas.wouters | 2006-08-11 17:02:39 +0200 (Fri, 11 Aug 2006) | 4 lines Add ignore of *.pyc and *.pyo to Lib/xml/etree/. ........ r51215 | thomas.heller | 2006-08-11 21:55:35 +0200 (Fri, 11 Aug 2006) | 7 lines When a ctypes C callback function is called, zero out the result storage before converting the result to C data. See the comment in the code for details. Provide a better context for errors when the conversion of a callback function's result cannot be converted. ........ r51218 | neal.norwitz | 2006-08-12 03:43:40 +0200 (Sat, 12 Aug 2006) | 6 lines Klocwork made another run and found a bunch more problems. This is the first batch of fixes that should be easy to verify based on context. This fixes problem numbers: 220 (ast), 323-324 (symtable), 321-322 (structseq), 215 (array), 210 (hotshot), 182 (codecs), 209 (etree). ........ r51219 | neal.norwitz | 2006-08-12 03:45:47 +0200 (Sat, 12 Aug 2006) | 9 lines Even though _Py_Mangle() isn't truly public anyone can call it and there was no verification that privateobj was a PyString. If it wasn't a string, this could have allowed a NULL pointer to creep in below and crash. I wonder if this should be PyString_CheckExact? Must identifiers be strings or can they be subclasses? Klocwork #275 ........ r51220 | neal.norwitz | 2006-08-12 03:46:42 +0200 (Sat, 12 Aug 2006) | 5 lines It's highly unlikely, though possible for PyEval_Get*() to return NULLs. So be safe and do an XINCREF. Klocwork # 221-222. ........ r51221 | neal.norwitz | 2006-08-12 03:47:59 +0200 (Sat, 12 Aug 2006) | 7 lines This code is actually not used unless WITHOUT_COMPLEX is defined. However, there was no error checking that PyFloat_FromDouble returned a valid pointer. I believe this change is correct as it seemed to follow other code in the area. Klocwork # 292. ........ r51222 | neal.norwitz | 2006-08-12 03:49:12 +0200 (Sat, 12 Aug 2006) | 5 lines Handle NULL nodes while parsing. I'm not entirely sure this is correct. There might be something else that needs to be done to setup the error. Klocwork #295. ........ r51223 | neal.norwitz | 2006-08-12 03:50:38 +0200 (Sat, 12 Aug 2006) | 6 lines If _stat_float_times is false, we will try to INCREF ival which could be NULL. Return early in that case. The caller checks for PyErr_Occurred so this should be ok. Klocwork #297 ........ r51224 | neal.norwitz | 2006-08-12 03:51:12 +0200 (Sat, 12 Aug 2006) | 3 lines Move the assert which checks for a NULL pointer first. Klocwork #274. ........ r51225 | neal.norwitz | 2006-08-12 03:53:28 +0200 (Sat, 12 Aug 2006) | 5 lines Try to handle a malloc failure. I'm not entirely sure this is correct. There might be something else we need to do to handle the exception. Klocwork # 212-213 ........ r51226 | neal.norwitz | 2006-08-12 03:57:47 +0200 (Sat, 12 Aug 2006) | 6 lines I'm not sure why this code allocates this string for the error message. I think it would be better to always use snprintf and have the format limit the size of the name appropriately (like %.200s). Klocwork #340 ........ r51227 | neal.norwitz | 2006-08-12 04:06:34 +0200 (Sat, 12 Aug 2006) | 3 lines Check returned pointer is valid. Klocwork #233 ........ r51228 | neal.norwitz | 2006-08-12 04:12:30 +0200 (Sat, 12 Aug 2006) | 1 line Whoops, how did that get in there. :-) Revert all the parts of 51227 that were not supposed to go it. Only Modules/_ctypes/cfields.c was supposed to be changed ........ r51229 | neal.norwitz | 2006-08-12 04:33:36 +0200 (Sat, 12 Aug 2006) | 4 lines Don't deref v if it's NULL. Klocwork #214 ........ r51230 | neal.norwitz | 2006-08-12 05:16:54 +0200 (Sat, 12 Aug 2006) | 5 lines Check return of PyMem_MALLOC (garbage) is non-NULL. Check seq in both portions of if/else. Klocwork #289-290. ........ r51231 | neal.norwitz | 2006-08-12 05:17:41 +0200 (Sat, 12 Aug 2006) | 4 lines PyModule_GetDict() can fail, produce fatal errors if this happens on startup. Klocwork #298-299. ........ r51232 | neal.norwitz | 2006-08-12 05:18:50 +0200 (Sat, 12 Aug 2006) | 5 lines Verify verdat which is returned from malloc is not NULL. Ensure we don't pass NULL to free. Klocwork #306 (at least the first part, checking malloc) ........ r51233 | tim.peters | 2006-08-12 06:42:47 +0200 (Sat, 12 Aug 2006) | 35 lines test_signal: Signal handling on the Tru64 buildbot appears to be utterly insane. Plug some theoretical insecurities in the test script: - Verify that the SIGALRM handler was actually installed. - Don't call alarm() before the handler is installed. - Move everything that can fail inside the try/finally, so the test cleans up after itself more often. - Try sending all the expected signals in force_test_exit(), not just SIGALRM. Since that was fixed to actually send SIGALRM (instead of invisibly dying with an AttributeError), we've seen that sending SIGALRM alone does not stop this from hanging. - Move the "kill the child" business into the finally clause, so the child doesn't survive test failure to send SIGALRM to other tests later (there are also baffling SIGALRM-related failures in test_socket). - Cancel the alarm in the finally clause -- if the test dies early, we again don't want SIGALRM showing up to confuse a later test. Alas, this still relies on timing luck wrt the spawned script that sends the test signals, but it's hard to see how waiting for seconds can so often be so unlucky. test_threadedsignals: curiously, this test never fails on Tru64, but doesn't normally signal SIGALRM. Anyway, fixed an obvious (but probably inconsequential) logic error. ........ r51234 | tim.peters | 2006-08-12 07:17:41 +0200 (Sat, 12 Aug 2006) | 8 lines Ah, fudge. One of the prints here actually "shouldn't be" protected by "if verbose:", which caused the test to fail on all non-Windows boxes. Note that I deliberately didn't convert this to unittest yet, because I expect it would be even harder to debug this on Tru64 after conversion. ........ r51235 | georg.brandl | 2006-08-12 10:32:02 +0200 (Sat, 12 Aug 2006) | 3 lines Repair logging test spew caused by rev. 51206. ........ r51236 | neal.norwitz | 2006-08-12 19:03:09 +0200 (Sat, 12 Aug 2006) | 8 lines Patch #1538606, Patch to fix __index__() clipping. I modified this patch some by fixing style, some error checking, and adding XXX comments. This patch requires review and some changes are to be expected. I'm checking in now to get the greatest possible review and establish a baseline for moving forward. I don't want this to hold up release if possible. ........ r51238 | neal.norwitz | 2006-08-12 20:44:06 +0200 (Sat, 12 Aug 2006) | 10 lines Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots were failing due to inappropriate clipping of numbers larger than 2**31 with new-style classes. (typeobject.c) In reviewing the code for classic classes, there were 2 problems. Any negative value return could be returned. Always return -1 if there was an error. Also make the checks similar with the new-style classes. I believe this is correct for 32 and 64 bit boxes, including Windows64. Add a test of classic classes too. ........ r51240 | neal.norwitz | 2006-08-13 02:20:49 +0200 (Sun, 13 Aug 2006) | 1 line SF bug #1539336, distutils example code missing ........ r51245 | neal.norwitz | 2006-08-13 20:10:10 +0200 (Sun, 13 Aug 2006) | 6 lines Move/copy assert for tstate != NULL before first use. Verify that PyEval_Get{Globals,Locals} returned valid pointers. Klocwork 231-232 ........ r51246 | neal.norwitz | 2006-08-13 20:10:28 +0200 (Sun, 13 Aug 2006) | 5 lines Handle a whole lot of failures from PyString_FromInternedString(). Should fix most of Klocwork 234-272. ........ r51247 | neal.norwitz | 2006-08-13 20:10:47 +0200 (Sun, 13 Aug 2006) | 8 lines cpathname could be NULL if it was longer than MAXPATHLEN. Don't try to write the .pyc to NULL. Check results of PyList_GetItem() and PyModule_GetDict() are not NULL. Klocwork 282, 283, 285 ........ r51248 | neal.norwitz | 2006-08-13 20:11:08 +0200 (Sun, 13 Aug 2006) | 6 lines Fix segfault when doing string formatting on subclasses of long if __oct__, __hex__ don't return a string. Klocwork 308 ........ r51250 | neal.norwitz | 2006-08-13 20:11:27 +0200 (Sun, 13 Aug 2006) | 5 lines Check return result of PyModule_GetDict(). Fix a bunch of refleaks in the init of the module. This would only be found when running python -v. ........ r51251 | neal.norwitz | 2006-08-13 20:11:43 +0200 (Sun, 13 Aug 2006) | 5 lines Handle malloc and fopen failures more gracefully. Klocwork 180-181 ........ r51252 | neal.norwitz | 2006-08-13 20:12:03 +0200 (Sun, 13 Aug 2006) | 7 lines It's very unlikely, though possible that source is not a string. Verify that PyString_AsString() returns a valid pointer. (The problem can arise when zlib.decompress doesn't return a string.) Klocwork 346 ........ r51253 | neal.norwitz | 2006-08-13 20:12:26 +0200 (Sun, 13 Aug 2006) | 5 lines Handle failures from lookup. Klocwork 341-342 ........ r51254 | neal.norwitz | 2006-08-13 20:12:45 +0200 (Sun, 13 Aug 2006) | 6 lines Handle failure from PyModule_GetDict() (Klocwork 208). Fix a bunch of refleaks in the init of the module. This would only be found when running python -v. ........ r51255 | neal.norwitz | 2006-08-13 20:13:02 +0200 (Sun, 13 Aug 2006) | 4 lines Really address the issue of where to place the assert for leftblock. (Followup of Klocwork 274) ........ r51256 | neal.norwitz | 2006-08-13 20:13:36 +0200 (Sun, 13 Aug 2006) | 4 lines Handle malloc failure. Klocwork 281 ........ r51258 | neal.norwitz | 2006-08-13 20:40:39 +0200 (Sun, 13 Aug 2006) | 4 lines Handle alloca failures. Klocwork 225-228 ........ r51259 | neal.norwitz | 2006-08-13 20:41:15 +0200 (Sun, 13 Aug 2006) | 1 line Get rid of compiler warning ........ r51261 | neal.norwitz | 2006-08-14 02:51:15 +0200 (Mon, 14 Aug 2006) | 1 line Ignore pgen.exe and kill_python.exe for cygwin ........ r51262 | neal.norwitz | 2006-08-14 02:59:03 +0200 (Mon, 14 Aug 2006) | 4 lines Can't return NULL from a void function. If there is a memory error, about the best we can do is call PyErr_WriteUnraisable and go on. We won't be able to do the call below either, so verify delstr is valid. ........ r51263 | neal.norwitz | 2006-08-14 03:49:54 +0200 (Mon, 14 Aug 2006) | 1 line Update purify doc some. ........ r51264 | thomas.heller | 2006-08-14 09:13:05 +0200 (Mon, 14 Aug 2006) | 2 lines Remove unused, buggy test function. Fixes klockwork issue #207. ........ r51265 | thomas.heller | 2006-08-14 09:14:09 +0200 (Mon, 14 Aug 2006) | 2 lines Check for NULL return value from new_CArgObject(). Fixes klockwork issues #183, #184, #185. ........ r51266 | thomas.heller | 2006-08-14 09:50:14 +0200 (Mon, 14 Aug 2006) | 2 lines Check for NULL return value of GenericCData_new(). Fixes klockwork issues #188, #189. ........ r51274 | thomas.heller | 2006-08-14 12:02:24 +0200 (Mon, 14 Aug 2006) | 2 lines Revert the change that tries to zero out a closure's result storage area because the size if unknown in source/callproc.c. ........ r51276 | marc-andre.lemburg | 2006-08-14 12:55:19 +0200 (Mon, 14 Aug 2006) | 11 lines Slightly revised version of patch #1538956: Replace UnicodeDecodeErrors raised during == and != compares of Unicode and other objects with a new UnicodeWarning. All other comparisons continue to raise exceptions. Exceptions other than UnicodeDecodeErrors are also left untouched. ........ r51277 | thomas.heller | 2006-08-14 13:17:48 +0200 (Mon, 14 Aug 2006) | 13 lines Apply the patch #1532975 plus ideas from the patch #1533481. ctypes instances no longer have the internal and undocumented '_as_parameter_' attribute which was used to adapt them to foreign function calls; this mechanism is replaced by a function pointer in the type's stgdict. In the 'from_param' class methods, try the _as_parameter_ attribute if other conversions are not possible. This makes the documented _as_parameter_ mechanism work as intended. Change the ctypes version number to 1.0.1. ........ r51278 | marc-andre.lemburg | 2006-08-14 13:44:34 +0200 (Mon, 14 Aug 2006) | 3 lines Readd NEWS items that were accidentally removed by r51276. ........ r51279 | georg.brandl | 2006-08-14 14:36:06 +0200 (Mon, 14 Aug 2006) | 3 lines Improve markup in PyUnicode_RichCompare. ........ r51280 | marc-andre.lemburg | 2006-08-14 14:57:27 +0200 (Mon, 14 Aug 2006) | 3 lines Correct an accidentally removed previous patch. ........ r51281 | thomas.heller | 2006-08-14 18:17:41 +0200 (Mon, 14 Aug 2006) | 3 lines Patch #1536908: Add support for AMD64 / OpenBSD. Remove the -no-stack-protector compiler flag for OpenBSD as it has been reported to be unneeded. ........ r51282 | thomas.heller | 2006-08-14 18:20:04 +0200 (Mon, 14 Aug 2006) | 1 line News item for rev 51281. ........ r51283 | georg.brandl | 2006-08-14 22:25:39 +0200 (Mon, 14 Aug 2006) | 3 lines Fix refleak introduced in rev. 51248. ........ r51284 | georg.brandl | 2006-08-14 23:34:08 +0200 (Mon, 14 Aug 2006) | 5 lines Make tabnanny recognize IndentationErrors raised by tokenize. Add a test to test_inspect to make sure indented source is recognized correctly. (fixes #1224621) ........ r51285 | georg.brandl | 2006-08-14 23:42:55 +0200 (Mon, 14 Aug 2006) | 3 lines Patch #1535500: fix segfault in BZ2File.writelines and make sure it raises the correct exceptions. ........ r51287 | georg.brandl | 2006-08-14 23:45:32 +0200 (Mon, 14 Aug 2006) | 3 lines Add an additional test: BZ2File write methods should raise IOError when file is read-only. ........ r51289 | georg.brandl | 2006-08-14 23:55:28 +0200 (Mon, 14 Aug 2006) | 3 lines Patch #1536071: trace.py should now find the full module name of a file correctly even on Windows. ........ r51290 | georg.brandl | 2006-08-15 00:01:24 +0200 (Tue, 15 Aug 2006) | 3 lines Cookie.py shouldn't "bogusly" use string._idmap. ........ r51291 | georg.brandl | 2006-08-15 00:10:24 +0200 (Tue, 15 Aug 2006) | 3 lines Patch #1511317: don't crash on invalid hostname info ........ r51292 | tim.peters | 2006-08-15 02:25:04 +0200 (Tue, 15 Aug 2006) | 2 lines Whitespace normalization. ........ r51293 | neal.norwitz | 2006-08-15 06:14:57 +0200 (Tue, 15 Aug 2006) | 3 lines Georg fixed one of my bugs, so I'll repay him with 2 NEWS entries. Now we're even. :-) ........ r51295 | neal.norwitz | 2006-08-15 06:58:28 +0200 (Tue, 15 Aug 2006) | 8 lines Fix the test for SocketServer so it should pass on cygwin and not fail sporadically on other platforms. This is really a band-aid that doesn't fix the underlying issue in SocketServer. It's not clear if it's worth it to fix SocketServer, however, I opened a bug to track it: http://python.org/sf/1540386 ........ r51296 | neal.norwitz | 2006-08-15 06:59:30 +0200 (Tue, 15 Aug 2006) | 3 lines Update the docstring to use a version a little newer than 1999. This was taken from a Debian patch. Should we update the version for each release? ........ r51298 | neal.norwitz | 2006-08-15 08:29:03 +0200 (Tue, 15 Aug 2006) | 2 lines Subclasses of int/long are allowed to define an __index__. ........ r51300 | thomas.heller | 2006-08-15 15:07:21 +0200 (Tue, 15 Aug 2006) | 1 line Check for NULL return value from new_CArgObject calls. ........ r51303 | kurt.kaiser | 2006-08-16 05:15:26 +0200 (Wed, 16 Aug 2006) | 2 lines The 'with' statement is now a Code Context block opener ........ r51304 | anthony.baxter | 2006-08-16 05:42:26 +0200 (Wed, 16 Aug 2006) | 1 line preparing for 2.5c1 ........ r51305 | anthony.baxter | 2006-08-16 05:58:37 +0200 (Wed, 16 Aug 2006) | 1 line preparing for 2.5c1 - no, really this time ........ r51306 | kurt.kaiser | 2006-08-16 07:01:42 +0200 (Wed, 16 Aug 2006) | 9 lines Patch #1540892: site.py Quitter() class attempts to close sys.stdin before raising SystemExit, allowing IDLE to honor quit() and exit(). M Lib/site.py M Lib/idlelib/PyShell.py M Lib/idlelib/CREDITS.txt M Lib/idlelib/NEWS.txt M Misc/NEWS ........ r51307 | ka-ping.yee | 2006-08-16 09:02:50 +0200 (Wed, 16 Aug 2006) | 6 lines Update code and tests to support the 'bytes_le' attribute (for little-endian byte order on Windows), and to work around clocks with low resolution yielding duplicate UUIDs. Anthony Baxter has approved this change. ........ r51308 | kurt.kaiser | 2006-08-16 09:04:17 +0200 (Wed, 16 Aug 2006) | 2 lines Get quit() and exit() to work cleanly when not using subprocess. ........ r51309 | marc-andre.lemburg | 2006-08-16 10:13:26 +0200 (Wed, 16 Aug 2006) | 2 lines Revert to having static version numbers again. ........ r51310 | martin.v.loewis | 2006-08-16 14:55:10 +0200 (Wed, 16 Aug 2006) | 2 lines Build _hashlib on Windows. Build OpenSSL with masm assembler code. Fixes #1535502. ........ r51311 | thomas.heller | 2006-08-16 15:03:11 +0200 (Wed, 16 Aug 2006) | 6 lines Add commented assert statements to check that the result of PyObject_stgdict() and PyType_stgdict() calls are non-NULL before dereferencing the result. Hopefully this fixes what klocwork is complaining about. Fix a few other nits as well. ........ r51312 | anthony.baxter | 2006-08-16 15:08:25 +0200 (Wed, 16 Aug 2006) | 1 line news entry for 51307 ........ r51313 | andrew.kuchling | 2006-08-16 15:22:20 +0200 (Wed, 16 Aug 2006) | 1 line Add UnicodeWarning ........ r51314 | andrew.kuchling | 2006-08-16 15:41:52 +0200 (Wed, 16 Aug 2006) | 1 line Bump document version to 1.0; remove pystone paragraph ........ r51315 | andrew.kuchling | 2006-08-16 15:51:32 +0200 (Wed, 16 Aug 2006) | 1 line Link to docs; remove an XXX comment ........ r51316 | martin.v.loewis | 2006-08-16 15:58:51 +0200 (Wed, 16 Aug 2006) | 1 line Make cl build step compile-only (/c). Remove libs from source list. ........ r51317 | thomas.heller | 2006-08-16 16:07:44 +0200 (Wed, 16 Aug 2006) | 5 lines The __repr__ method of a NULL py_object does no longer raise an exception. Remove a stray '?' character from the exception text when the value is retrieved of such an object. Includes tests. ........ r51318 | andrew.kuchling | 2006-08-16 16:18:23 +0200 (Wed, 16 Aug 2006) | 1 line Update bug/patch counts ........ r51319 | andrew.kuchling | 2006-08-16 16:21:14 +0200 (Wed, 16 Aug 2006) | 1 line Wording/typo fixes ........ r51320 | thomas.heller | 2006-08-16 17:10:12 +0200 (Wed, 16 Aug 2006) | 9 lines Remove the special casing of Py_None when converting the return value of the Python part of a callback function to C. If it cannot be converted, call PyErr_WriteUnraisable with the exception we got. Before, arbitrary data has been passed to the calling C code in this case. (I'm not really sure the NEWS entry is understandable, but I cannot find better words) ........ r51321 | marc-andre.lemburg | 2006-08-16 18:11:01 +0200 (Wed, 16 Aug 2006) | 2 lines Add NEWS item mentioning the reverted distutils version number patch. ........ r51322 | fredrik.lundh | 2006-08-16 18:47:07 +0200 (Wed, 16 Aug 2006) | 5 lines SF#1534630 ignore data that arrives before the opening start tag ........ r51324 | andrew.kuchling | 2006-08-16 19:11:18 +0200 (Wed, 16 Aug 2006) | 1 line Grammar fix ........ r51328 | thomas.heller | 2006-08-16 20:02:11 +0200 (Wed, 16 Aug 2006) | 12 lines Tutorial: Clarify somewhat how parameters are passed to functions (especially explain what integer means). Correct the table - Python integers and longs can both be used. Further clarification to the table comparing ctypes types, Python types, and C types. Reference: Replace integer by C ``int`` where it makes sense. ........ r51329 | kurt.kaiser | 2006-08-16 23:45:59 +0200 (Wed, 16 Aug 2006) | 8 lines File menu hotkeys: there were three 'p' assignments. Reassign the 'Save Copy As' and 'Print' hotkeys to 'y' and 't'. Change the Shell menu hotkey from 's' to 'l'. M Bindings.py M PyShell.py M NEWS.txt ........ r51330 | neil.schemenauer | 2006-08-17 01:38:05 +0200 (Thu, 17 Aug 2006) | 3 lines Fix a bug in the ``compiler`` package that caused invalid code to be generated for generator expressions. ........ r51342 | martin.v.loewis | 2006-08-17 21:19:32 +0200 (Thu, 17 Aug 2006) | 3 lines Merge 51340 and 51341 from 2.5 branch: Leave tk build directory to restore original path. Invoke debug mk1mf.pl after running Configure. ........ r51354 | martin.v.loewis | 2006-08-18 05:47:18 +0200 (Fri, 18 Aug 2006) | 3 lines Bug #1541863: uuid.uuid1 failed to generate unique identifiers on systems with low clock resolution. ........ r51355 | neal.norwitz | 2006-08-18 05:57:54 +0200 (Fri, 18 Aug 2006) | 1 line Add template for 2.6 on HEAD ........ r51356 | neal.norwitz | 2006-08-18 06:01:38 +0200 (Fri, 18 Aug 2006) | 1 line More post-release wibble ........ r51357 | neal.norwitz | 2006-08-18 06:58:33 +0200 (Fri, 18 Aug 2006) | 1 line Try to get Windows bots working again ........ r51358 | neal.norwitz | 2006-08-18 07:10:00 +0200 (Fri, 18 Aug 2006) | 1 line Try to get Windows bots working again. Take 2 ........ r51359 | neal.norwitz | 2006-08-18 07:39:20 +0200 (Fri, 18 Aug 2006) | 1 line Try to get Unix bots install working again. ........ r51360 | neal.norwitz | 2006-08-18 07:41:46 +0200 (Fri, 18 Aug 2006) | 1 line Set version to 2.6a0, seems more consistent. ........ r51362 | neal.norwitz | 2006-08-18 08:14:52 +0200 (Fri, 18 Aug 2006) | 1 line More version wibble ........ r51364 | georg.brandl | 2006-08-18 09:27:59 +0200 (Fri, 18 Aug 2006) | 4 lines Bug #1541682: Fix example in the "Refcount details" API docs. Additionally, remove a faulty example showing PySequence_SetItem applied to a newly created list object and add notes that this isn't a good idea. ........ r51366 | anthony.baxter | 2006-08-18 09:29:02 +0200 (Fri, 18 Aug 2006) | 3 lines Updating IDLE's version number to match Python's (as per python-dev discussion). ........ r51367 | anthony.baxter | 2006-08-18 09:30:07 +0200 (Fri, 18 Aug 2006) | 1 line RPM specfile updates ........ r51368 | georg.brandl | 2006-08-18 09:35:47 +0200 (Fri, 18 Aug 2006) | 2 lines Typo in tp_clear docs. ........ r51378 | andrew.kuchling | 2006-08-18 15:57:13 +0200 (Fri, 18 Aug 2006) | 1 line Minor edits ........ r51379 | thomas.heller | 2006-08-18 16:38:46 +0200 (Fri, 18 Aug 2006) | 6 lines Add asserts to check for 'impossible' NULL values, with comments. In one place where I'n not 1000% sure about the non-NULL, raise a RuntimeError for safety. This should fix the klocwork issues that Neal sent me. If so, it should be applied to the release25-maint branch also. ........ r51400 | neal.norwitz | 2006-08-19 06:22:33 +0200 (Sat, 19 Aug 2006) | 5 lines Move initialization of interned strings to before allocating the object so we don't leak op. (Fixes an earlier patch to this code) Klockwork #350 ........ r51401 | neal.norwitz | 2006-08-19 06:23:04 +0200 (Sat, 19 Aug 2006) | 4 lines Move assert to after NULL check, otherwise we deref NULL in the assert. Klocwork #307 ........ r51402 | neal.norwitz | 2006-08-19 06:25:29 +0200 (Sat, 19 Aug 2006) | 2 lines SF #1542693: Remove semi-colon at end of PyImport_ImportModuleEx macro ........ r51403 | neal.norwitz | 2006-08-19 06:28:55 +0200 (Sat, 19 Aug 2006) | 6 lines Move initialization to after the asserts for non-NULL values. Klocwork 286-287. (I'm not backporting this, but if someone wants to, feel free.) ........ r51404 | neal.norwitz | 2006-08-19 06:52:03 +0200 (Sat, 19 Aug 2006) | 6 lines Handle PyString_FromInternedString() failing (unlikely, but possible). Klocwork #325 (I'm not backporting this, but if someone wants to, feel free.) ........ r51416 | georg.brandl | 2006-08-20 15:15:39 +0200 (Sun, 20 Aug 2006) | 2 lines Patch #1542948: fix urllib2 header casing issue. With new test. ........ r51428 | jeremy.hylton | 2006-08-21 18:19:37 +0200 (Mon, 21 Aug 2006) | 3 lines Move peephole optimizer to separate file. ........ r51429 | jeremy.hylton | 2006-08-21 18:20:29 +0200 (Mon, 21 Aug 2006) | 2 lines Move peephole optimizer to separate file. (Forgot .h in previous checkin.) ........ r51432 | neal.norwitz | 2006-08-21 19:59:46 +0200 (Mon, 21 Aug 2006) | 5 lines Fix bug #1543303, tarfile adds padding that breaks gunzip. Patch # 1543897. Will backport to 2.5 ........ r51433 | neal.norwitz | 2006-08-21 20:01:30 +0200 (Mon, 21 Aug 2006) | 2 lines Add assert to make Klocwork happy (#276) ........
1361 lines
32 KiB
C
1361 lines
32 KiB
C
#include "Python.h"
|
|
#include "structmember.h"
|
|
|
|
/* collections module implementation of a deque() datatype
|
|
Written and maintained by Raymond D. Hettinger <python@rcn.com>
|
|
Copyright (c) 2004 Python Software Foundation.
|
|
All rights reserved.
|
|
*/
|
|
|
|
/* The block length may be set to any number over 1. Larger numbers
|
|
* reduce the number of calls to the memory allocator but take more
|
|
* memory. Ideally, BLOCKLEN should be set with an eye to the
|
|
* length of a cache line.
|
|
*/
|
|
|
|
#define BLOCKLEN 62
|
|
#define CENTER ((BLOCKLEN - 1) / 2)
|
|
|
|
/* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
|
|
* This list is not circular (the leftmost block has leftlink==NULL,
|
|
* and the rightmost block has rightlink==NULL). A deque d's first
|
|
* element is at d.leftblock[leftindex] and its last element is at
|
|
* d.rightblock[rightindex]; note that, unlike as for Python slice
|
|
* indices, these indices are inclusive on both ends. By being inclusive
|
|
* on both ends, algorithms for left and right operations become
|
|
* symmetrical which simplifies the design.
|
|
*
|
|
* The list of blocks is never empty, so d.leftblock and d.rightblock
|
|
* are never equal to NULL.
|
|
*
|
|
* The indices, d.leftindex and d.rightindex are always in the range
|
|
* 0 <= index < BLOCKLEN.
|
|
* Their exact relationship is:
|
|
* (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
|
|
*
|
|
* Empty deques have d.len == 0; d.leftblock==d.rightblock;
|
|
* d.leftindex == CENTER+1; and d.rightindex == CENTER.
|
|
* Checking for d.len == 0 is the intended way to see whether d is empty.
|
|
*
|
|
* Whenever d.leftblock == d.rightblock,
|
|
* d.leftindex + d.len - 1 == d.rightindex.
|
|
*
|
|
* However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
|
|
* become indices into distinct blocks and either may be larger than the
|
|
* other.
|
|
*/
|
|
|
|
typedef struct BLOCK {
|
|
struct BLOCK *leftlink;
|
|
struct BLOCK *rightlink;
|
|
PyObject *data[BLOCKLEN];
|
|
} block;
|
|
|
|
static block *
|
|
newblock(block *leftlink, block *rightlink, int len) {
|
|
block *b;
|
|
/* To prevent len from overflowing INT_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 INT_MAX-2 entries in total.
|
|
*/
|
|
if (len >= INT_MAX - 2*BLOCKLEN) {
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
"cannot add more blocks to the deque");
|
|
return NULL;
|
|
}
|
|
b = PyMem_Malloc(sizeof(block));
|
|
if (b == NULL) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
b->leftlink = leftlink;
|
|
b->rightlink = rightlink;
|
|
return b;
|
|
}
|
|
|
|
typedef struct {
|
|
PyObject_HEAD
|
|
block *leftblock;
|
|
block *rightblock;
|
|
int leftindex; /* in range(BLOCKLEN) */
|
|
int rightindex; /* in range(BLOCKLEN) */
|
|
int len;
|
|
long state; /* incremented whenever the indices move */
|
|
PyObject *weakreflist; /* List of weak references */
|
|
} dequeobject;
|
|
|
|
static PyTypeObject deque_type;
|
|
|
|
static PyObject *
|
|
deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
dequeobject *deque;
|
|
block *b;
|
|
|
|
if (!_PyArg_NoKeywords("deque()", kwds))
|
|
return NULL;
|
|
|
|
/* 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;
|
|
|
|
return (PyObject *)deque;
|
|
}
|
|
|
|
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;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
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;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the 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);
|
|
PyMem_Free(deque->rightblock);
|
|
prevblock->rightlink = NULL;
|
|
deque->rightblock = prevblock;
|
|
deque->rightindex = BLOCKLEN - 1;
|
|
}
|
|
}
|
|
return item;
|
|
}
|
|
|
|
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;
|
|
PyMem_Free(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.");
|
|
|
|
static PyObject *
|
|
deque_extend(dequeobject *deque, PyObject *iterable)
|
|
{
|
|
PyObject *it, *item;
|
|
|
|
it = PyObject_GetIter(iterable);
|
|
if (it == NULL)
|
|
return NULL;
|
|
|
|
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;
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(extend_doc,
|
|
"Extend the right side of the deque with elements from the iterable");
|
|
|
|
static PyObject *
|
|
deque_extendleft(dequeobject *deque, PyObject *iterable)
|
|
{
|
|
PyObject *it, *item;
|
|
|
|
it = PyObject_GetIter(iterable);
|
|
if (it == NULL)
|
|
return NULL;
|
|
|
|
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;
|
|
}
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
PyDoc_STRVAR(extendleft_doc,
|
|
"Extend the left side of the deque with elements from the iterable");
|
|
|
|
static int
|
|
_deque_rotate(dequeobject *deque, Py_ssize_t n)
|
|
{
|
|
int 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)
|
|
{
|
|
int n=1;
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:rotate", &n))
|
|
return NULL;
|
|
if (_deque_rotate(deque, n) == 0)
|
|
Py_RETURN_NONE;
|
|
return NULL;
|
|
}
|
|
|
|
PyDoc_STRVAR(rotate_doc,
|
|
"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
|
|
|
|
static Py_ssize_t
|
|
deque_len(dequeobject *deque)
|
|
{
|
|
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;
|
|
}
|
|
|
|
PyDoc_STRVAR(remove_doc,
|
|
"D.remove(value) -- remove first occurrence of value.");
|
|
|
|
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;
|
|
}
|
|
|
|
static PyObject *
|
|
deque_item(dequeobject *deque, int i)
|
|
{
|
|
block *b;
|
|
PyObject *item;
|
|
int 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
|
|
performance near the end points. If for some reason this method becomes
|
|
popular, it is not hard to re-implement this using direct data movement
|
|
(similar to code in list slice assignment) and achieve a two or threefold
|
|
performance boost.
|
|
*/
|
|
|
|
static int
|
|
deque_del_item(dequeobject *deque, Py_ssize_t i)
|
|
{
|
|
PyObject *item;
|
|
|
|
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);
|
|
|
|
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;
|
|
}
|
|
|
|
static PyObject *
|
|
deque_clearmethod(dequeobject *deque)
|
|
{
|
|
int rv;
|
|
|
|
rv = deque_clear(deque);
|
|
assert (rv != -1);
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
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);
|
|
PyMem_Free(deque->leftblock);
|
|
}
|
|
deque->leftblock = NULL;
|
|
deque->rightblock = NULL;
|
|
deque->ob_type->tp_free(deque);
|
|
}
|
|
|
|
static int
|
|
deque_traverse(dequeobject *deque, visitproc visit, void *arg)
|
|
{
|
|
block *b;
|
|
PyObject *item;
|
|
int index;
|
|
int indexlo = deque->leftindex;
|
|
|
|
for (b = deque->leftblock; b != NULL; b = b->rightlink) {
|
|
const int 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 long
|
|
deque_nohash(PyObject *self)
|
|
{
|
|
PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
|
|
return -1;
|
|
}
|
|
|
|
static PyObject *
|
|
deque_copy(PyObject *deque)
|
|
{
|
|
return PyObject_CallFunctionObjArgs((PyObject *)(deque->ob_type),
|
|
deque, NULL);
|
|
}
|
|
|
|
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
|
|
|
|
static PyObject *
|
|
deque_reduce(dequeobject *deque)
|
|
{
|
|
PyObject *dict, *result, *it;
|
|
|
|
dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
|
|
if (dict == NULL) {
|
|
PyErr_Clear();
|
|
dict = Py_None;
|
|
Py_INCREF(dict);
|
|
}
|
|
it = PyObject_GetIter((PyObject *)deque);
|
|
if (it == NULL) {
|
|
Py_DECREF(dict);
|
|
return NULL;
|
|
}
|
|
result = Py_BuildValue("O()ON", deque->ob_type, dict, it);
|
|
Py_DECREF(dict);
|
|
return result;
|
|
}
|
|
|
|
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
|
|
|
|
static PyObject *
|
|
deque_repr(PyObject *deque)
|
|
{
|
|
PyObject *aslist, *result, *fmt;
|
|
int i;
|
|
|
|
i = Py_ReprEnter(deque);
|
|
if (i != 0) {
|
|
if (i < 0)
|
|
return NULL;
|
|
return PyString_FromString("[...]");
|
|
}
|
|
|
|
aslist = PySequence_List(deque);
|
|
if (aslist == NULL) {
|
|
Py_ReprLeave(deque);
|
|
return NULL;
|
|
}
|
|
|
|
fmt = PyString_FromString("deque(%r)");
|
|
if (fmt == NULL) {
|
|
Py_DECREF(aslist);
|
|
Py_ReprLeave(deque);
|
|
return NULL;
|
|
}
|
|
result = PyString_Format(fmt, aslist);
|
|
Py_DECREF(fmt);
|
|
Py_DECREF(aslist);
|
|
Py_ReprLeave(deque);
|
|
return result;
|
|
}
|
|
|
|
static int
|
|
deque_tp_print(PyObject *deque, FILE *fp, int flags)
|
|
{
|
|
PyObject *it, *item;
|
|
char *emit = ""; /* No separator emitted on first pass */
|
|
char *separator = ", ";
|
|
int i;
|
|
|
|
i = Py_ReprEnter(deque);
|
|
if (i != 0) {
|
|
if (i < 0)
|
|
return i;
|
|
fputs("[...]", fp);
|
|
return 0;
|
|
}
|
|
|
|
it = PyObject_GetIter(deque);
|
|
if (it == NULL)
|
|
return -1;
|
|
|
|
fputs("deque([", fp);
|
|
while ((item = PyIter_Next(it)) != NULL) {
|
|
fputs(emit, fp);
|
|
emit = separator;
|
|
if (PyObject_Print(item, fp, 0) != 0) {
|
|
Py_DECREF(item);
|
|
Py_DECREF(it);
|
|
Py_ReprLeave(deque);
|
|
return -1;
|
|
}
|
|
Py_DECREF(item);
|
|
}
|
|
Py_ReprLeave(deque);
|
|
Py_DECREF(it);
|
|
if (PyErr_Occurred())
|
|
return -1;
|
|
fputs("])", fp);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
deque_richcompare(PyObject *v, PyObject *w, int op)
|
|
{
|
|
PyObject *it1=NULL, *it2=NULL, *x, *y;
|
|
int b, vs, ws, 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;
|
|
}
|
|
|
|
static int
|
|
deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *iterable = NULL;
|
|
|
|
if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
|
|
return -1;
|
|
|
|
if (iterable != NULL) {
|
|
PyObject *rv = deque_extend(deque, iterable);
|
|
if (rv == NULL)
|
|
return -1;
|
|
Py_DECREF(rv);
|
|
}
|
|
return 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 */
|
|
};
|
|
|
|
/* deque object ********************************************************/
|
|
|
|
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");
|
|
|
|
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 */
|
|
};
|
|
|
|
PyDoc_STRVAR(deque_doc,
|
|
"deque(iterable) --> deque object\n\
|
|
\n\
|
|
Build an ordered collection accessible from endpoints only.");
|
|
|
|
static PyTypeObject deque_type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"collections.deque", /* tp_name */
|
|
sizeof(dequeobject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
(destructor)deque_dealloc, /* tp_dealloc */
|
|
deque_tp_print, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
deque_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
&deque_as_sequence, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
deque_nohash, /* 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 */
|
|
0, /* 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
|
|
int index;
|
|
block *b;
|
|
dequeobject *deque;
|
|
long state; /* state when the iterator is created */
|
|
int counter; /* number of items remaining for iteration */
|
|
} dequeiterobject;
|
|
|
|
PyTypeObject dequeiter_type;
|
|
|
|
static PyObject *
|
|
deque_iter(dequeobject *deque)
|
|
{
|
|
dequeiterobject *it;
|
|
|
|
it = PyObject_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;
|
|
return (PyObject *)it;
|
|
}
|
|
|
|
static void
|
|
dequeiter_dealloc(dequeiterobject *dio)
|
|
{
|
|
Py_XDECREF(dio->deque);
|
|
dio->ob_type->tp_free(dio);
|
|
}
|
|
|
|
static PyObject *
|
|
dequeiter_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->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 PyInt_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 */
|
|
};
|
|
|
|
PyTypeObject dequeiter_type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"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_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 */
|
|
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)dequeiter_next, /* tp_iternext */
|
|
dequeiter_methods, /* tp_methods */
|
|
0,
|
|
};
|
|
|
|
/*********************** Deque Reverse Iterator **************************/
|
|
|
|
PyTypeObject dequereviter_type;
|
|
|
|
static PyObject *
|
|
deque_reviter(dequeobject *deque)
|
|
{
|
|
dequeiterobject *it;
|
|
|
|
it = PyObject_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;
|
|
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;
|
|
}
|
|
|
|
PyTypeObject dequereviter_type = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /* ob_size */
|
|
"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_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 */
|
|
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)dequereviter_next, /* tp_iternext */
|
|
dequeiter_methods, /* tp_methods */
|
|
0,
|
|
};
|
|
|
|
/* defaultdict type *********************************************************/
|
|
|
|
typedef struct {
|
|
PyDictObject dict;
|
|
PyObject *default_factory;
|
|
} defdictobject;
|
|
|
|
static PyTypeObject defdict_type; /* Forward */
|
|
|
|
PyDoc_STRVAR(defdict_missing_doc,
|
|
"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
|
|
if self.default_factory is None: raise KeyError(key)\n\
|
|
self[key] = value = self.default_factory()\n\
|
|
return value\n\
|
|
");
|
|
|
|
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) */
|
|
PyErr_SetObject(PyExc_KeyError, key);
|
|
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.");
|
|
|
|
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().
|
|
*/
|
|
return PyObject_CallFunctionObjArgs((PyObject *)dd->dict.ob_type,
|
|
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 *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, "iteritems", "()");
|
|
if (items == NULL) {
|
|
Py_DECREF(args);
|
|
return NULL;
|
|
}
|
|
result = PyTuple_Pack(5, dd->dict.ob_type, args,
|
|
Py_None, Py_None, items);
|
|
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}
|
|
};
|
|
|
|
static PyMemberDef defdict_members[] = {
|
|
{"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);
|
|
}
|
|
|
|
static int
|
|
defdict_print(defdictobject *dd, FILE *fp, int flags)
|
|
{
|
|
int sts;
|
|
fprintf(fp, "defaultdict(");
|
|
if (dd->default_factory == NULL)
|
|
fprintf(fp, "None");
|
|
else {
|
|
PyObject_Print(dd->default_factory, fp, 0);
|
|
}
|
|
fprintf(fp, ", ");
|
|
sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
|
|
fprintf(fp, ")");
|
|
return sts;
|
|
}
|
|
|
|
static PyObject *
|
|
defdict_repr(defdictobject *dd)
|
|
{
|
|
PyObject *defrepr;
|
|
PyObject *baserepr;
|
|
PyObject *result;
|
|
baserepr = PyDict_Type.tp_repr((PyObject *)dd);
|
|
if (baserepr == NULL)
|
|
return NULL;
|
|
if (dd->default_factory == NULL)
|
|
defrepr = PyString_FromString("None");
|
|
else
|
|
defrepr = PyObject_Repr(dd->default_factory);
|
|
if (defrepr == NULL) {
|
|
Py_DECREF(baserepr);
|
|
return NULL;
|
|
}
|
|
result = PyString_FromFormat("defaultdict(%s, %s)",
|
|
PyString_AS_STRING(defrepr),
|
|
PyString_AS_STRING(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);
|
|
}
|
|
|
|
static int
|
|
defdict_tp_clear(defdictobject *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);
|
|
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,
|
|
"defaultdict(default_factory) --> dict with default factory\n\
|
|
\n\
|
|
The default factory is called without arguments to produce\n\
|
|
a new value when a key is not present, in __getitem__ only.\n\
|
|
A defaultdict compares equal to a dict with the same items.\n\
|
|
");
|
|
|
|
/* See comment in xxsubtype.c */
|
|
#define DEFERRED_ADDRESS(ADDR) 0
|
|
|
|
static PyTypeObject defdict_type = {
|
|
PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
|
|
0, /* ob_size */
|
|
"collections.defaultdict", /* tp_name */
|
|
sizeof(defdictobject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
(destructor)defdict_dealloc, /* tp_dealloc */
|
|
(printfunc)defdict_print, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(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 ********************************************************/
|
|
|
|
PyDoc_STRVAR(module_doc,
|
|
"High performance data structures.\n\
|
|
- deque: ordered collection accessible from endpoints only\n\
|
|
- defaultdict: dict subclass with a default value factory\n\
|
|
");
|
|
|
|
PyMODINIT_FUNC
|
|
initcollections(void)
|
|
{
|
|
PyObject *m;
|
|
|
|
m = Py_InitModule3("collections", NULL, module_doc);
|
|
if (m == NULL)
|
|
return;
|
|
|
|
if (PyType_Ready(&deque_type) < 0)
|
|
return;
|
|
Py_INCREF(&deque_type);
|
|
PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
|
|
|
|
defdict_type.tp_base = &PyDict_Type;
|
|
if (PyType_Ready(&defdict_type) < 0)
|
|
return;
|
|
Py_INCREF(&defdict_type);
|
|
PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
|
|
|
|
if (PyType_Ready(&dequeiter_type) < 0)
|
|
return;
|
|
|
|
if (PyType_Ready(&dequereviter_type) < 0)
|
|
return;
|
|
|
|
return;
|
|
}
|