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

3368 lines
97 KiB

36 years ago
36 years ago
* Optimized list appends and pops by making fewer calls the underlying system realloc(). This is achieved by tracking the overallocation size in a new field and using that information to skip calls to realloc() whenever possible. * Simplified and tightened the amount of overallocation. For larger lists, this overallocates by 1/8th (compared to the previous scheme which ranged between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the maximum overallocation is one byte (formerly it could be upto eight bytes). This saves memory in applications with large numbers of small lists. * Eliminated the NRESIZE macro in favor of a new, static list_resize function that encapsulates the resizing logic. Coverting this back to macro would give a small (under 1%) speed-up. This was too small to warrant the loss of readability, maintainability, and de-coupling. * Some functions using NRESIZE had grown unnecessarily complex in their efforts to bend to the macro's calling pattern. With the new list_resize function in place, those other functions could be simplified. That is being saved for a separate patch. * The ob_item==NULL check could be eliminated from the new list_resize function. This would entail finding each piece of code that sets ob_item to NULL and adding a new line to invalidate the overallocation tracking field. Rather than impose a new requirement on other pieces of list code, it was preferred to leave the NULL check in place and retain the benefits of decoupling, maintainability and information hiding (only PyList_New() and list_sort() need to know about the new field). This approach also reduces the odds of breaking an extension module. (Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters, and Armin Rigo.)
22 years ago
* Optimized list appends and pops by making fewer calls the underlying system realloc(). This is achieved by tracking the overallocation size in a new field and using that information to skip calls to realloc() whenever possible. * Simplified and tightened the amount of overallocation. For larger lists, this overallocates by 1/8th (compared to the previous scheme which ranged between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the maximum overallocation is one byte (formerly it could be upto eight bytes). This saves memory in applications with large numbers of small lists. * Eliminated the NRESIZE macro in favor of a new, static list_resize function that encapsulates the resizing logic. Coverting this back to macro would give a small (under 1%) speed-up. This was too small to warrant the loss of readability, maintainability, and de-coupling. * Some functions using NRESIZE had grown unnecessarily complex in their efforts to bend to the macro's calling pattern. With the new list_resize function in place, those other functions could be simplified. That is being saved for a separate patch. * The ob_item==NULL check could be eliminated from the new list_resize function. This would entail finding each piece of code that sets ob_item to NULL and adding a new line to invalidate the overallocation tracking field. Rather than impose a new requirement on other pieces of list code, it was preferred to leave the NULL check in place and retain the benefits of decoupling, maintainability and information hiding (only PyList_New() and list_sort() need to know about the new field). This approach also reduces the odds of breaking an extension module. (Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters, and Armin Rigo.)
22 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60568 | christian.heimes | 2008-02-04 19:48:38 +0100 (Mon, 04 Feb 2008) | 1 line Increase debugging to investige failing tests on some build bots ........ r60570 | christian.heimes | 2008-02-04 20:30:05 +0100 (Mon, 04 Feb 2008) | 1 line Small adjustments for test compact freelist test. It's no passing on Windows as well. ........ r60573 | amaury.forgeotdarc | 2008-02-04 21:53:14 +0100 (Mon, 04 Feb 2008) | 2 lines Correct quotes in NEWS file ........ r60575 | amaury.forgeotdarc | 2008-02-04 22:45:05 +0100 (Mon, 04 Feb 2008) | 13 lines #1750076: Debugger did not step on every iteration of a while statement. The mapping between bytecode offsets and source lines (lnotab) did not contain an entry for the beginning of the loop. Now it does, and the lnotab can be a bit larger: in particular, several statements on the same line generate several entries. However, this does not bother the settrace function, which will trigger only one 'line' event. The lnotab seems to be exactly the same as with python2.4. ........ r60584 | amaury.forgeotdarc | 2008-02-05 01:26:21 +0100 (Tue, 05 Feb 2008) | 3 lines Change r60575 broke test_compile: there is no need to emit co_lnotab item when both offsets are zeros. ........ r60587 | skip.montanaro | 2008-02-05 03:32:16 +0100 (Tue, 05 Feb 2008) | 1 line sync with most recent version from python-mode sf project ........ r60588 | lars.gustaebel | 2008-02-05 12:51:40 +0100 (Tue, 05 Feb 2008) | 5 lines Issue #2004: Use mode 0700 for temporary directories and default permissions for missing directories. (will backport to 2.5) ........ r60590 | georg.brandl | 2008-02-05 13:01:24 +0100 (Tue, 05 Feb 2008) | 2 lines Convert external links to internal links. Fixes #2010. ........ r60592 | marc-andre.lemburg | 2008-02-05 15:50:40 +0100 (Tue, 05 Feb 2008) | 3 lines Keep distutils Python 2.1 compatible (or even Python 2.4 in this case). ........ r60593 | andrew.kuchling | 2008-02-05 17:06:57 +0100 (Tue, 05 Feb 2008) | 5 lines Update PEP URL. (This code is duplicated between pydoc and DocXMLRPCServer; maybe it should be refactored as a GHOP project.) 2.5.2 backport candidate. ........ r60596 | guido.van.rossum | 2008-02-05 18:32:15 +0100 (Tue, 05 Feb 2008) | 2 lines In the experimental 'Scanner' feature, the group count was set wrong. ........ r60602 | facundo.batista | 2008-02-05 20:03:32 +0100 (Tue, 05 Feb 2008) | 3 lines Issue 1951. Converts wave test cases to unittest. ........ r60603 | georg.brandl | 2008-02-05 20:07:10 +0100 (Tue, 05 Feb 2008) | 2 lines Actually run the test. ........ r60604 | skip.montanaro | 2008-02-05 20:24:30 +0100 (Tue, 05 Feb 2008) | 2 lines correct object name ........ r60605 | georg.brandl | 2008-02-05 20:58:17 +0100 (Tue, 05 Feb 2008) | 7 lines * Use the same code to profile for test_profile and test_cprofile. * Convert both to unittest. * Use the same unit testing code. * Include the expected output in both test files. * Make it possible to regenerate the expected output by running the file as a script with an '-r' argument. ........ r60613 | raymond.hettinger | 2008-02-06 02:49:00 +0100 (Wed, 06 Feb 2008) | 1 line Sync-up with Py3k work. ........ r60614 | christian.heimes | 2008-02-06 13:44:34 +0100 (Wed, 06 Feb 2008) | 1 line Limit free list of method and builtin function objects to 256 entries each. ........ r60616 | christian.heimes | 2008-02-06 14:33:44 +0100 (Wed, 06 Feb 2008) | 7 lines Unified naming convention for free lists and their limits. All free lists in Object/ are named ``free_list``, the counter ``numfree`` and the upper limit is a macro ``PyName_MAXFREELIST`` inside an #ifndef block. The chances should make it easier to adjust Python for platforms with less memory, e.g. mobile phones. ........
18 years ago
36 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60618 | walter.doerwald | 2008-02-06 15:31:55 +0100 (Wed, 06 Feb 2008) | 6 lines Remove month parameter from Calendar.yeardatescalendar(), Calendar.yeardays2calendar() and Calendar.yeardayscalendar() as the methods don't have such a parameter. Fixes issue #2017. Rewrap content to 80 chars. ........ r60622 | facundo.batista | 2008-02-06 20:28:49 +0100 (Wed, 06 Feb 2008) | 4 lines Fixes issue 1959. Converted tests to unittest. Thanks Giampaolo Rodola. ........ r60626 | thomas.heller | 2008-02-06 21:29:17 +0100 (Wed, 06 Feb 2008) | 3 lines Fixed refcounts and error handling. Should not be merged to py3k branch. ........ r60630 | mark.dickinson | 2008-02-06 23:10:50 +0100 (Wed, 06 Feb 2008) | 4 lines Issue 1979: Make Decimal comparisons (other than !=, ==) involving NaN raise InvalidOperation (and return False if InvalidOperation is trapped). ........ r60632 | mark.dickinson | 2008-02-06 23:25:16 +0100 (Wed, 06 Feb 2008) | 2 lines Remove incorrect usage of :const: in documentation. ........ r60634 | georg.brandl | 2008-02-07 00:45:51 +0100 (Thu, 07 Feb 2008) | 2 lines Revert accidental changes to test_queue in r60605. ........ r60636 | raymond.hettinger | 2008-02-07 01:54:20 +0100 (Thu, 07 Feb 2008) | 1 line Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence. ........ r60637 | mark.dickinson | 2008-02-07 02:14:23 +0100 (Thu, 07 Feb 2008) | 2 lines Fix broken link in decimal documentation. ........ r60638 | mark.dickinson | 2008-02-07 02:42:06 +0100 (Thu, 07 Feb 2008) | 3 lines IEEE 754 should be IEEE 854; give precise reference for comparisons involving NaNs. ........ r60639 | raymond.hettinger | 2008-02-07 03:12:52 +0100 (Thu, 07 Feb 2008) | 1 line Return ints instead of longs for tuple.count() and tuple.index(). ........ r60640 | raymond.hettinger | 2008-02-07 04:10:33 +0100 (Thu, 07 Feb 2008) | 1 line Merge 60627. ........ r60641 | raymond.hettinger | 2008-02-07 04:25:46 +0100 (Thu, 07 Feb 2008) | 1 line Merge r60628, r60631, and r60633. Register UserList and UserString will the appropriate ABCs. ........ r60642 | brett.cannon | 2008-02-07 08:47:31 +0100 (Thu, 07 Feb 2008) | 3 lines Cast a struct to a void pointer so as to do a type-safe pointer comparison (mistmatch found by clang). ........ r60643 | brett.cannon | 2008-02-07 09:04:07 +0100 (Thu, 07 Feb 2008) | 2 lines Remove unnecessary curly braces around an int literal. ........ r60644 | andrew.kuchling | 2008-02-07 12:43:47 +0100 (Thu, 07 Feb 2008) | 1 line Update URL ........ r60645 | facundo.batista | 2008-02-07 17:16:29 +0100 (Thu, 07 Feb 2008) | 4 lines Fixes issue 2026. Tests converted to unittest. Thanks Giampaolo Rodola. ........ r60646 | christian.heimes | 2008-02-07 18:15:30 +0100 (Thu, 07 Feb 2008) | 1 line Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet. ........ r60648 | facundo.batista | 2008-02-07 20:06:52 +0100 (Thu, 07 Feb 2008) | 6 lines Fixes Issue 1401. When redirected, a possible POST get converted to GET, so it loses its payload. So, it also must lose the headers related to the payload (if it has no content any more, it shouldn't indicate content length and type). ........ r60649 | walter.doerwald | 2008-02-07 20:30:22 +0100 (Thu, 07 Feb 2008) | 3 lines Clarify that the output of TextCalendar.formatmonth() and TextCalendar.formatyear() for custom instances won't be influenced by calls to the module global setfirstweekday() function. Fixes #2018. ........ r60651 | walter.doerwald | 2008-02-07 20:48:34 +0100 (Thu, 07 Feb 2008) | 3 lines Fix documentation for Calendar.iterweekdays(): firstweekday is a property. Fixes second part of #2018. ........ r60653 | walter.doerwald | 2008-02-07 20:57:32 +0100 (Thu, 07 Feb 2008) | 2 lines Fix typo in docstring for Calendar.itermonthdays(). ........ r60655 | raymond.hettinger | 2008-02-07 21:04:37 +0100 (Thu, 07 Feb 2008) | 1 line The float conversion recipe is simpler in Py2.6 ........ r60657 | raymond.hettinger | 2008-02-07 21:10:49 +0100 (Thu, 07 Feb 2008) | 1 line Fix typo ........ r60660 | brett.cannon | 2008-02-07 23:27:10 +0100 (Thu, 07 Feb 2008) | 3 lines Make sure a switch statement does not have repetitive case statements. Error found through LLVM post-2.1 svn. ........ r60661 | christian.heimes | 2008-02-08 01:11:31 +0100 (Fri, 08 Feb 2008) | 1 line Deallocate content of the dict free list on interpreter shutdown ........ r60662 | christian.heimes | 2008-02-08 01:14:34 +0100 (Fri, 08 Feb 2008) | 1 line Use prefix decrement ........ r60663 | amaury.forgeotdarc | 2008-02-08 01:56:02 +0100 (Fri, 08 Feb 2008) | 5 lines issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. ........ r60667 | jeffrey.yasskin | 2008-02-08 07:45:40 +0100 (Fri, 08 Feb 2008) | 2 lines Oops! 2.6's Rational.__ne__ didn't work. ........ r60671 | hyeshik.chang | 2008-02-08 18:10:20 +0100 (Fri, 08 Feb 2008) | 2 lines Update big5hkscs codec to conform to the HKSCS:2004 revision. ........ r60673 | raymond.hettinger | 2008-02-08 23:30:04 +0100 (Fri, 08 Feb 2008) | 4 lines Remove unnecessary modulo division. The preceding test guarantees that 0 <= i < len. ........ r60674 | raymond.hettinger | 2008-02-09 00:02:27 +0100 (Sat, 09 Feb 2008) | 1 line Speed-up __iter__() mixin method. ........ r60675 | raymond.hettinger | 2008-02-09 00:34:21 +0100 (Sat, 09 Feb 2008) | 1 line Fill-in missing Set comparisons ........ r60677 | raymond.hettinger | 2008-02-09 00:57:06 +0100 (Sat, 09 Feb 2008) | 1 line Add advice on choosing between DictMixin and MutableMapping ........
18 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
Merged revisions 60284-60349 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r60286 | christian.heimes | 2008-01-25 15:54:23 +0100 (Fri, 25 Jan 2008) | 1 line setup.py doesn't pick up changes to a header file ........ r60287 | christian.heimes | 2008-01-25 16:52:11 +0100 (Fri, 25 Jan 2008) | 2 lines Added the Python core headers Include/*.h and pyconfig.h as dependencies for the extensions in Modules/ It forces a rebuild of all extensions when a header files has been modified ........ r60291 | raymond.hettinger | 2008-01-25 20:24:46 +0100 (Fri, 25 Jan 2008) | 4 lines Changes 54857 and 54840 broke code and were reverted in Py2.5 just before it was released, but that reversion never made it to the Py2.6 head. ........ r60296 | guido.van.rossum | 2008-01-25 20:50:26 +0100 (Fri, 25 Jan 2008) | 2 lines Rewrite the list_inline_repeat overflow check slightly differently. ........ r60301 | thomas.wouters | 2008-01-25 22:09:34 +0100 (Fri, 25 Jan 2008) | 4 lines Use the right (portable) definition of the max of a Py_ssize_t. ........ r60303 | thomas.wouters | 2008-01-26 02:47:05 +0100 (Sat, 26 Jan 2008) | 5 lines Make 'testall' work again when building in a separate directory. test_distutils still fails when doing that. ........ r60305 | neal.norwitz | 2008-01-26 06:54:48 +0100 (Sat, 26 Jan 2008) | 3 lines Prevent this test from failing if there are transient network problems by retrying the host for up to 3 times. ........ r60306 | neal.norwitz | 2008-01-26 08:26:12 +0100 (Sat, 26 Jan 2008) | 12 lines Use a condition variable (threading.Event) rather than sleeps and checking a global to determine when the server is ready to be used. This slows the test down, but should make it correct. There was a race condition before where the server could have assigned a port, yet it wasn't ready to serve requests. If the client sent a request before the server was completely ready, it would get an exception. There was machinery to try to handle this condition. All of that should be unnecessary and removed if this change works. A NOTE was added as a comment about what needs to be fixed. The buildbots will tell us if there are more errors or if this test is now stable. ........ r60307 | neal.norwitz | 2008-01-26 08:38:03 +0100 (Sat, 26 Jan 2008) | 3 lines Fix exception in tearDown on ppc buildbot. If there's no directory, that shouldn't cause the test to fail. Just like it setUp. ........ r60308 | raymond.hettinger | 2008-01-26 09:19:06 +0100 (Sat, 26 Jan 2008) | 3 lines Make PySet_Add() work with frozensets. Works like PyTuple_SetItem() to build-up values in a brand new frozenset. ........ r60309 | neal.norwitz | 2008-01-26 09:26:00 +0100 (Sat, 26 Jan 2008) | 1 line The OS X buildbot had errors with the unavailable exceptions disabled. Restore it. ........ r60310 | raymond.hettinger | 2008-01-26 09:37:28 +0100 (Sat, 26 Jan 2008) | 4 lines Let marshal build-up sets and frozensets one element at a time. Saves the unnecessary creation of a tuple as intermediate container. ........ r60311 | raymond.hettinger | 2008-01-26 09:41:13 +0100 (Sat, 26 Jan 2008) | 1 line Update test code for change to PySet_Add(). ........ r60312 | raymond.hettinger | 2008-01-26 10:31:11 +0100 (Sat, 26 Jan 2008) | 1 line Revert PySet_Add() changes. ........ r60314 | georg.brandl | 2008-01-26 10:43:35 +0100 (Sat, 26 Jan 2008) | 2 lines #1934: fix os.path.isabs docs. ........ r60316 | georg.brandl | 2008-01-26 12:00:18 +0100 (Sat, 26 Jan 2008) | 2 lines Add missing things in re docstring. ........ r60317 | georg.brandl | 2008-01-26 12:02:22 +0100 (Sat, 26 Jan 2008) | 2 lines Slashes allowed on Windows. ........ r60319 | georg.brandl | 2008-01-26 14:41:21 +0100 (Sat, 26 Jan 2008) | 2 lines Fix markup again. ........ r60320 | andrew.kuchling | 2008-01-26 14:50:51 +0100 (Sat, 26 Jan 2008) | 1 line Add some items ........ r60321 | georg.brandl | 2008-01-26 15:02:38 +0100 (Sat, 26 Jan 2008) | 2 lines Clarify "b" mode under Unix. ........ r60322 | georg.brandl | 2008-01-26 15:03:47 +0100 (Sat, 26 Jan 2008) | 3 lines #1940: make it possible to use curses.filter() before curses.initscr() as the documentation says. ........ r60324 | georg.brandl | 2008-01-26 15:14:20 +0100 (Sat, 26 Jan 2008) | 3 lines #1473257: add generator.gi_code attribute that refers to the original code object backing the generator. Patch by Collin Winter. ........ r60325 | georg.brandl | 2008-01-26 15:19:22 +0100 (Sat, 26 Jan 2008) | 2 lines Move C API entries to the corresponding section. ........ r60326 | christian.heimes | 2008-01-26 17:43:35 +0100 (Sat, 26 Jan 2008) | 1 line Unit test fix from Giampaolo Rodola, #1938 ........ r60327 | gregory.p.smith | 2008-01-26 19:51:05 +0100 (Sat, 26 Jan 2008) | 2 lines Update docs for new callpack params added in r60188 ........ r60329 | neal.norwitz | 2008-01-26 21:24:36 +0100 (Sat, 26 Jan 2008) | 3 lines Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots, this might fix the problem. ........ r60330 | neal.norwitz | 2008-01-26 22:02:45 +0100 (Sat, 26 Jan 2008) | 1 line Always try to remove the test file even if close raises an exception ........ r60331 | neal.norwitz | 2008-01-26 22:21:59 +0100 (Sat, 26 Jan 2008) | 3 lines Reduce the race condition by signalling when the server is ready and not trying to connect before. ........ r60334 | neal.norwitz | 2008-01-27 00:13:46 +0100 (Sun, 27 Jan 2008) | 5 lines On some systems (e.g., Ubuntu on hppa) the flush() doesn't cause the exception, but the close() does. Will backport. ........ r60335 | neal.norwitz | 2008-01-27 00:14:17 +0100 (Sun, 27 Jan 2008) | 2 lines Consistently use tempfile.tempdir for the db_home directory. ........ r60338 | neal.norwitz | 2008-01-27 02:44:05 +0100 (Sun, 27 Jan 2008) | 4 lines Eliminate the sleeps that assume the server will start in .5 seconds. This should make the test less flaky. It also speeds up the test by about 75% on my box (20+ seconds -> ~4 seconds). ........ r60342 | neal.norwitz | 2008-01-27 06:02:34 +0100 (Sun, 27 Jan 2008) | 6 lines Try to prevent this test from being flaky. We might need a sleep in here which isn't as bad as it sounds. The close() *should* raise an exception, so if it didn't we should give more time to sync and really raise it. Will backport. ........ r60344 | jeffrey.yasskin | 2008-01-27 06:40:35 +0100 (Sun, 27 Jan 2008) | 3 lines Make rational.gcd() public and allow Rational to take decimal strings, per Raymond's advice. ........ r60345 | neal.norwitz | 2008-01-27 08:36:03 +0100 (Sun, 27 Jan 2008) | 3 lines Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS nor UNIX is defined. This may have caused problems on cygwin. ........ r60346 | neal.norwitz | 2008-01-27 08:37:38 +0100 (Sun, 27 Jan 2008) | 3 lines Use int for the sign rather than a char. char can be signed or unsigned. It's system dependent. This might fix the problem with test_rfind failing. ........ r60347 | neal.norwitz | 2008-01-27 08:41:33 +0100 (Sun, 27 Jan 2008) | 1 line Add stdarg include for va_list to get this to compile on cygwin ........ r60348 | raymond.hettinger | 2008-01-27 11:13:57 +0100 (Sun, 27 Jan 2008) | 1 line Docstring nit ........ r60349 | raymond.hettinger | 2008-01-27 11:47:55 +0100 (Sun, 27 Jan 2008) | 1 line Removed an unnecessary and confusing paragraph from the namedtuple docs. ........
18 years ago
* Optimized list appends and pops by making fewer calls the underlying system realloc(). This is achieved by tracking the overallocation size in a new field and using that information to skip calls to realloc() whenever possible. * Simplified and tightened the amount of overallocation. For larger lists, this overallocates by 1/8th (compared to the previous scheme which ranged between 1/4th to 1/32nd over-allocation). For smaller lists (n<6), the maximum overallocation is one byte (formerly it could be upto eight bytes). This saves memory in applications with large numbers of small lists. * Eliminated the NRESIZE macro in favor of a new, static list_resize function that encapsulates the resizing logic. Coverting this back to macro would give a small (under 1%) speed-up. This was too small to warrant the loss of readability, maintainability, and de-coupling. * Some functions using NRESIZE had grown unnecessarily complex in their efforts to bend to the macro's calling pattern. With the new list_resize function in place, those other functions could be simplified. That is being saved for a separate patch. * The ob_item==NULL check could be eliminated from the new list_resize function. This would entail finding each piece of code that sets ob_item to NULL and adding a new line to invalidate the overallocation tracking field. Rather than impose a new requirement on other pieces of list code, it was preferred to leave the NULL check in place and retain the benefits of decoupling, maintainability and information hiding (only PyList_New() and list_sort() need to know about the new field). This approach also reduces the odds of breaking an extension module. (Collaborative effort by Raymond Hettinger, Hye-Shik Chang, Tim Peters, and Armin Rigo.)
22 years ago
36 years ago
36 years ago
36 years ago
36 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
36 years ago
36 years ago
36 years ago
  1. /* List object implementation */
  2. #include "Python.h"
  3. #include "pycore_object.h"
  4. #include "pycore_pystate.h"
  5. #include "pycore_accu.h"
  6. #ifdef STDC_HEADERS
  7. #include <stddef.h>
  8. #else
  9. #include <sys/types.h> /* For size_t */
  10. #endif
  11. /*[clinic input]
  12. class list "PyListObject *" "&PyList_Type"
  13. [clinic start generated code]*/
  14. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f9b222678f9f71e0]*/
  15. #include "clinic/listobject.c.h"
  16. /* Ensure ob_item has room for at least newsize elements, and set
  17. * ob_size to newsize. If newsize > ob_size on entry, the content
  18. * of the new slots at exit is undefined heap trash; it's the caller's
  19. * responsibility to overwrite them with sane values.
  20. * The number of allocated elements may grow, shrink, or stay the same.
  21. * Failure is impossible if newsize <= self.allocated on entry, although
  22. * that partly relies on an assumption that the system realloc() never
  23. * fails when passed a number of bytes <= the number of bytes last
  24. * allocated (the C standard doesn't guarantee this, but it's hard to
  25. * imagine a realloc implementation where it wouldn't be true).
  26. * Note that self->ob_item may change, and even if newsize is less
  27. * than ob_size on entry.
  28. */
  29. static int
  30. list_resize(PyListObject *self, Py_ssize_t newsize)
  31. {
  32. PyObject **items;
  33. size_t new_allocated, num_allocated_bytes;
  34. Py_ssize_t allocated = self->allocated;
  35. /* Bypass realloc() when a previous overallocation is large enough
  36. to accommodate the newsize. If the newsize falls lower than half
  37. the allocated size, then proceed with the realloc() to shrink the list.
  38. */
  39. if (allocated >= newsize && newsize >= (allocated >> 1)) {
  40. assert(self->ob_item != NULL || newsize == 0);
  41. Py_SIZE(self) = newsize;
  42. return 0;
  43. }
  44. /* This over-allocates proportional to the list size, making room
  45. * for additional growth. The over-allocation is mild, but is
  46. * enough to give linear-time amortized behavior over a long
  47. * sequence of appends() in the presence of a poorly-performing
  48. * system realloc().
  49. * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
  50. * Note: new_allocated won't overflow because the largest possible value
  51. * is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
  52. */
  53. new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
  54. if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
  55. PyErr_NoMemory();
  56. return -1;
  57. }
  58. if (newsize == 0)
  59. new_allocated = 0;
  60. num_allocated_bytes = new_allocated * sizeof(PyObject *);
  61. items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
  62. if (items == NULL) {
  63. PyErr_NoMemory();
  64. return -1;
  65. }
  66. self->ob_item = items;
  67. Py_SIZE(self) = newsize;
  68. self->allocated = new_allocated;
  69. return 0;
  70. }
  71. static int
  72. list_preallocate_exact(PyListObject *self, Py_ssize_t size)
  73. {
  74. assert(self->ob_item == NULL);
  75. assert(size > 0);
  76. PyObject **items = PyMem_New(PyObject*, size);
  77. if (items == NULL) {
  78. PyErr_NoMemory();
  79. return -1;
  80. }
  81. self->ob_item = items;
  82. self->allocated = size;
  83. return 0;
  84. }
  85. /* Debug statistic to compare allocations with reuse through the free list */
  86. #undef SHOW_ALLOC_COUNT
  87. #ifdef SHOW_ALLOC_COUNT
  88. static size_t count_alloc = 0;
  89. static size_t count_reuse = 0;
  90. static void
  91. show_alloc(void)
  92. {
  93. PyInterpreterState *interp = _PyInterpreterState_Get();
  94. if (!interp->core_config.show_alloc_count) {
  95. return;
  96. }
  97. fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
  98. count_alloc);
  99. fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
  100. "d\n", count_reuse);
  101. fprintf(stderr, "%.2f%% reuse rate\n\n",
  102. (100.0*count_reuse/(count_alloc+count_reuse)));
  103. }
  104. #endif
  105. /* Empty list reuse scheme to save calls to malloc and free */
  106. #ifndef PyList_MAXFREELIST
  107. #define PyList_MAXFREELIST 80
  108. #endif
  109. static PyListObject *free_list[PyList_MAXFREELIST];
  110. static int numfree = 0;
  111. int
  112. PyList_ClearFreeList(void)
  113. {
  114. PyListObject *op;
  115. int ret = numfree;
  116. while (numfree) {
  117. op = free_list[--numfree];
  118. assert(PyList_CheckExact(op));
  119. PyObject_GC_Del(op);
  120. }
  121. return ret;
  122. }
  123. void
  124. PyList_Fini(void)
  125. {
  126. PyList_ClearFreeList();
  127. }
  128. /* Print summary info about the state of the optimized allocator */
  129. void
  130. _PyList_DebugMallocStats(FILE *out)
  131. {
  132. _PyDebugAllocatorStats(out,
  133. "free PyListObject",
  134. numfree, sizeof(PyListObject));
  135. }
  136. PyObject *
  137. PyList_New(Py_ssize_t size)
  138. {
  139. PyListObject *op;
  140. #ifdef SHOW_ALLOC_COUNT
  141. static int initialized = 0;
  142. if (!initialized) {
  143. Py_AtExit(show_alloc);
  144. initialized = 1;
  145. }
  146. #endif
  147. if (size < 0) {
  148. PyErr_BadInternalCall();
  149. return NULL;
  150. }
  151. if (numfree) {
  152. numfree--;
  153. op = free_list[numfree];
  154. _Py_NewReference((PyObject *)op);
  155. #ifdef SHOW_ALLOC_COUNT
  156. count_reuse++;
  157. #endif
  158. } else {
  159. op = PyObject_GC_New(PyListObject, &PyList_Type);
  160. if (op == NULL)
  161. return NULL;
  162. #ifdef SHOW_ALLOC_COUNT
  163. count_alloc++;
  164. #endif
  165. }
  166. if (size <= 0)
  167. op->ob_item = NULL;
  168. else {
  169. op->ob_item = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
  170. if (op->ob_item == NULL) {
  171. Py_DECREF(op);
  172. return PyErr_NoMemory();
  173. }
  174. }
  175. Py_SIZE(op) = size;
  176. op->allocated = size;
  177. _PyObject_GC_TRACK(op);
  178. return (PyObject *) op;
  179. }
  180. static PyObject *
  181. list_new_prealloc(Py_ssize_t size)
  182. {
  183. PyListObject *op = (PyListObject *) PyList_New(0);
  184. if (size == 0 || op == NULL) {
  185. return (PyObject *) op;
  186. }
  187. assert(op->ob_item == NULL);
  188. op->ob_item = PyMem_New(PyObject *, size);
  189. if (op->ob_item == NULL) {
  190. Py_DECREF(op);
  191. return PyErr_NoMemory();
  192. }
  193. op->allocated = size;
  194. return (PyObject *) op;
  195. }
  196. Py_ssize_t
  197. PyList_Size(PyObject *op)
  198. {
  199. if (!PyList_Check(op)) {
  200. PyErr_BadInternalCall();
  201. return -1;
  202. }
  203. else
  204. return Py_SIZE(op);
  205. }
  206. static inline int
  207. valid_index(Py_ssize_t i, Py_ssize_t limit)
  208. {
  209. /* The cast to size_t lets us use just a single comparison
  210. to check whether i is in the range: 0 <= i < limit.
  211. See: Section 14.2 "Bounds Checking" in the Agner Fog
  212. optimization manual found at:
  213. https://www.agner.org/optimize/optimizing_cpp.pdf
  214. */
  215. return (size_t) i < (size_t) limit;
  216. }
  217. static PyObject *indexerr = NULL;
  218. PyObject *
  219. PyList_GetItem(PyObject *op, Py_ssize_t i)
  220. {
  221. if (!PyList_Check(op)) {
  222. PyErr_BadInternalCall();
  223. return NULL;
  224. }
  225. if (!valid_index(i, Py_SIZE(op))) {
  226. if (indexerr == NULL) {
  227. indexerr = PyUnicode_FromString(
  228. "list index out of range");
  229. if (indexerr == NULL)
  230. return NULL;
  231. }
  232. PyErr_SetObject(PyExc_IndexError, indexerr);
  233. return NULL;
  234. }
  235. return ((PyListObject *)op) -> ob_item[i];
  236. }
  237. int
  238. PyList_SetItem(PyObject *op, Py_ssize_t i,
  239. PyObject *newitem)
  240. {
  241. PyObject **p;
  242. if (!PyList_Check(op)) {
  243. Py_XDECREF(newitem);
  244. PyErr_BadInternalCall();
  245. return -1;
  246. }
  247. if (!valid_index(i, Py_SIZE(op))) {
  248. Py_XDECREF(newitem);
  249. PyErr_SetString(PyExc_IndexError,
  250. "list assignment index out of range");
  251. return -1;
  252. }
  253. p = ((PyListObject *)op) -> ob_item + i;
  254. Py_XSETREF(*p, newitem);
  255. return 0;
  256. }
  257. static int
  258. ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
  259. {
  260. Py_ssize_t i, n = Py_SIZE(self);
  261. PyObject **items;
  262. if (v == NULL) {
  263. PyErr_BadInternalCall();
  264. return -1;
  265. }
  266. if (n == PY_SSIZE_T_MAX) {
  267. PyErr_SetString(PyExc_OverflowError,
  268. "cannot add more objects to list");
  269. return -1;
  270. }
  271. if (list_resize(self, n+1) < 0)
  272. return -1;
  273. if (where < 0) {
  274. where += n;
  275. if (where < 0)
  276. where = 0;
  277. }
  278. if (where > n)
  279. where = n;
  280. items = self->ob_item;
  281. for (i = n; --i >= where; )
  282. items[i+1] = items[i];
  283. Py_INCREF(v);
  284. items[where] = v;
  285. return 0;
  286. }
  287. int
  288. PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
  289. {
  290. if (!PyList_Check(op)) {
  291. PyErr_BadInternalCall();
  292. return -1;
  293. }
  294. return ins1((PyListObject *)op, where, newitem);
  295. }
  296. static int
  297. app1(PyListObject *self, PyObject *v)
  298. {
  299. Py_ssize_t n = PyList_GET_SIZE(self);
  300. assert (v != NULL);
  301. if (n == PY_SSIZE_T_MAX) {
  302. PyErr_SetString(PyExc_OverflowError,
  303. "cannot add more objects to list");
  304. return -1;
  305. }
  306. if (list_resize(self, n+1) < 0)
  307. return -1;
  308. Py_INCREF(v);
  309. PyList_SET_ITEM(self, n, v);
  310. return 0;
  311. }
  312. int
  313. PyList_Append(PyObject *op, PyObject *newitem)
  314. {
  315. if (PyList_Check(op) && (newitem != NULL))
  316. return app1((PyListObject *)op, newitem);
  317. PyErr_BadInternalCall();
  318. return -1;
  319. }
  320. /* Methods */
  321. static void
  322. list_dealloc(PyListObject *op)
  323. {
  324. Py_ssize_t i;
  325. PyObject_GC_UnTrack(op);
  326. Py_TRASHCAN_SAFE_BEGIN(op)
  327. if (op->ob_item != NULL) {
  328. /* Do it backwards, for Christian Tismer.
  329. There's a simple test case where somehow this reduces
  330. thrashing when a *very* large list is created and
  331. immediately deleted. */
  332. i = Py_SIZE(op);
  333. while (--i >= 0) {
  334. Py_XDECREF(op->ob_item[i]);
  335. }
  336. PyMem_FREE(op->ob_item);
  337. }
  338. if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
  339. free_list[numfree++] = op;
  340. else
  341. Py_TYPE(op)->tp_free((PyObject *)op);
  342. Py_TRASHCAN_SAFE_END(op)
  343. }
  344. static PyObject *
  345. list_repr(PyListObject *v)
  346. {
  347. Py_ssize_t i;
  348. PyObject *s;
  349. _PyUnicodeWriter writer;
  350. if (Py_SIZE(v) == 0) {
  351. return PyUnicode_FromString("[]");
  352. }
  353. i = Py_ReprEnter((PyObject*)v);
  354. if (i != 0) {
  355. return i > 0 ? PyUnicode_FromString("[...]") : NULL;
  356. }
  357. _PyUnicodeWriter_Init(&writer);
  358. writer.overallocate = 1;
  359. /* "[" + "1" + ", 2" * (len - 1) + "]" */
  360. writer.min_length = 1 + 1 + (2 + 1) * (Py_SIZE(v) - 1) + 1;
  361. if (_PyUnicodeWriter_WriteChar(&writer, '[') < 0)
  362. goto error;
  363. /* Do repr() on each element. Note that this may mutate the list,
  364. so must refetch the list size on each iteration. */
  365. for (i = 0; i < Py_SIZE(v); ++i) {
  366. if (i > 0) {
  367. if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
  368. goto error;
  369. }
  370. s = PyObject_Repr(v->ob_item[i]);
  371. if (s == NULL)
  372. goto error;
  373. if (_PyUnicodeWriter_WriteStr(&writer, s) < 0) {
  374. Py_DECREF(s);
  375. goto error;
  376. }
  377. Py_DECREF(s);
  378. }
  379. writer.overallocate = 0;
  380. if (_PyUnicodeWriter_WriteChar(&writer, ']') < 0)
  381. goto error;
  382. Py_ReprLeave((PyObject *)v);
  383. return _PyUnicodeWriter_Finish(&writer);
  384. error:
  385. _PyUnicodeWriter_Dealloc(&writer);
  386. Py_ReprLeave((PyObject *)v);
  387. return NULL;
  388. }
  389. static Py_ssize_t
  390. list_length(PyListObject *a)
  391. {
  392. return Py_SIZE(a);
  393. }
  394. static int
  395. list_contains(PyListObject *a, PyObject *el)
  396. {
  397. Py_ssize_t i;
  398. int cmp;
  399. for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
  400. cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
  401. Py_EQ);
  402. return cmp;
  403. }
  404. static PyObject *
  405. list_item(PyListObject *a, Py_ssize_t i)
  406. {
  407. if (!valid_index(i, Py_SIZE(a))) {
  408. if (indexerr == NULL) {
  409. indexerr = PyUnicode_FromString(
  410. "list index out of range");
  411. if (indexerr == NULL)
  412. return NULL;
  413. }
  414. PyErr_SetObject(PyExc_IndexError, indexerr);
  415. return NULL;
  416. }
  417. Py_INCREF(a->ob_item[i]);
  418. return a->ob_item[i];
  419. }
  420. static PyObject *
  421. list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
  422. {
  423. PyListObject *np;
  424. PyObject **src, **dest;
  425. Py_ssize_t i, len;
  426. if (ilow < 0)
  427. ilow = 0;
  428. else if (ilow > Py_SIZE(a))
  429. ilow = Py_SIZE(a);
  430. if (ihigh < ilow)
  431. ihigh = ilow;
  432. else if (ihigh > Py_SIZE(a))
  433. ihigh = Py_SIZE(a);
  434. len = ihigh - ilow;
  435. np = (PyListObject *) list_new_prealloc(len);
  436. if (np == NULL)
  437. return NULL;
  438. src = a->ob_item + ilow;
  439. dest = np->ob_item;
  440. for (i = 0; i < len; i++) {
  441. PyObject *v = src[i];
  442. Py_INCREF(v);
  443. dest[i] = v;
  444. }
  445. Py_SIZE(np) = len;
  446. return (PyObject *)np;
  447. }
  448. PyObject *
  449. PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
  450. {
  451. if (!PyList_Check(a)) {
  452. PyErr_BadInternalCall();
  453. return NULL;
  454. }
  455. return list_slice((PyListObject *)a, ilow, ihigh);
  456. }
  457. static PyObject *
  458. list_concat(PyListObject *a, PyObject *bb)
  459. {
  460. Py_ssize_t size;
  461. Py_ssize_t i;
  462. PyObject **src, **dest;
  463. PyListObject *np;
  464. if (!PyList_Check(bb)) {
  465. PyErr_Format(PyExc_TypeError,
  466. "can only concatenate list (not \"%.200s\") to list",
  467. bb->ob_type->tp_name);
  468. return NULL;
  469. }
  470. #define b ((PyListObject *)bb)
  471. if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
  472. return PyErr_NoMemory();
  473. size = Py_SIZE(a) + Py_SIZE(b);
  474. np = (PyListObject *) list_new_prealloc(size);
  475. if (np == NULL) {
  476. return NULL;
  477. }
  478. src = a->ob_item;
  479. dest = np->ob_item;
  480. for (i = 0; i < Py_SIZE(a); i++) {
  481. PyObject *v = src[i];
  482. Py_INCREF(v);
  483. dest[i] = v;
  484. }
  485. src = b->ob_item;
  486. dest = np->ob_item + Py_SIZE(a);
  487. for (i = 0; i < Py_SIZE(b); i++) {
  488. PyObject *v = src[i];
  489. Py_INCREF(v);
  490. dest[i] = v;
  491. }
  492. Py_SIZE(np) = size;
  493. return (PyObject *)np;
  494. #undef b
  495. }
  496. static PyObject *
  497. list_repeat(PyListObject *a, Py_ssize_t n)
  498. {
  499. Py_ssize_t i, j;
  500. Py_ssize_t size;
  501. PyListObject *np;
  502. PyObject **p, **items;
  503. PyObject *elem;
  504. if (n < 0)
  505. n = 0;
  506. if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
  507. return PyErr_NoMemory();
  508. size = Py_SIZE(a) * n;
  509. if (size == 0)
  510. return PyList_New(0);
  511. np = (PyListObject *) list_new_prealloc(size);
  512. if (np == NULL)
  513. return NULL;
  514. if (Py_SIZE(a) == 1) {
  515. items = np->ob_item;
  516. elem = a->ob_item[0];
  517. for (i = 0; i < n; i++) {
  518. items[i] = elem;
  519. Py_INCREF(elem);
  520. }
  521. }
  522. else {
  523. p = np->ob_item;
  524. items = a->ob_item;
  525. for (i = 0; i < n; i++) {
  526. for (j = 0; j < Py_SIZE(a); j++) {
  527. *p = items[j];
  528. Py_INCREF(*p);
  529. p++;
  530. }
  531. }
  532. }
  533. Py_SIZE(np) = size;
  534. return (PyObject *) np;
  535. }
  536. static int
  537. _list_clear(PyListObject *a)
  538. {
  539. Py_ssize_t i;
  540. PyObject **item = a->ob_item;
  541. if (item != NULL) {
  542. /* Because XDECREF can recursively invoke operations on
  543. this list, we make it empty first. */
  544. i = Py_SIZE(a);
  545. Py_SIZE(a) = 0;
  546. a->ob_item = NULL;
  547. a->allocated = 0;
  548. while (--i >= 0) {
  549. Py_XDECREF(item[i]);
  550. }
  551. PyMem_FREE(item);
  552. }
  553. /* Never fails; the return value can be ignored.
  554. Note that there is no guarantee that the list is actually empty
  555. at this point, because XDECREF may have populated it again! */
  556. return 0;
  557. }
  558. /* a[ilow:ihigh] = v if v != NULL.
  559. * del a[ilow:ihigh] if v == NULL.
  560. *
  561. * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
  562. * guaranteed the call cannot fail.
  563. */
  564. static int
  565. list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
  566. {
  567. /* Because [X]DECREF can recursively invoke list operations on
  568. this list, we must postpone all [X]DECREF activity until
  569. after the list is back in its canonical shape. Therefore
  570. we must allocate an additional array, 'recycle', into which
  571. we temporarily copy the items that are deleted from the
  572. list. :-( */
  573. PyObject *recycle_on_stack[8];
  574. PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
  575. PyObject **item;
  576. PyObject **vitem = NULL;
  577. PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
  578. Py_ssize_t n; /* # of elements in replacement list */
  579. Py_ssize_t norig; /* # of elements in list getting replaced */
  580. Py_ssize_t d; /* Change in size */
  581. Py_ssize_t k;
  582. size_t s;
  583. int result = -1; /* guilty until proved innocent */
  584. #define b ((PyListObject *)v)
  585. if (v == NULL)
  586. n = 0;
  587. else {
  588. if (a == b) {
  589. /* Special case "a[i:j] = a" -- copy b first */
  590. v = list_slice(b, 0, Py_SIZE(b));
  591. if (v == NULL)
  592. return result;
  593. result = list_ass_slice(a, ilow, ihigh, v);
  594. Py_DECREF(v);
  595. return result;
  596. }
  597. v_as_SF = PySequence_Fast(v, "can only assign an iterable");
  598. if(v_as_SF == NULL)
  599. goto Error;
  600. n = PySequence_Fast_GET_SIZE(v_as_SF);
  601. vitem = PySequence_Fast_ITEMS(v_as_SF);
  602. }
  603. if (ilow < 0)
  604. ilow = 0;
  605. else if (ilow > Py_SIZE(a))
  606. ilow = Py_SIZE(a);
  607. if (ihigh < ilow)
  608. ihigh = ilow;
  609. else if (ihigh > Py_SIZE(a))
  610. ihigh = Py_SIZE(a);
  611. norig = ihigh - ilow;
  612. assert(norig >= 0);
  613. d = n - norig;
  614. if (Py_SIZE(a) + d == 0) {
  615. Py_XDECREF(v_as_SF);
  616. return _list_clear(a);
  617. }
  618. item = a->ob_item;
  619. /* recycle the items that we are about to remove */
  620. s = norig * sizeof(PyObject *);
  621. /* If norig == 0, item might be NULL, in which case we may not memcpy from it. */
  622. if (s) {
  623. if (s > sizeof(recycle_on_stack)) {
  624. recycle = (PyObject **)PyMem_MALLOC(s);
  625. if (recycle == NULL) {
  626. PyErr_NoMemory();
  627. goto Error;
  628. }
  629. }
  630. memcpy(recycle, &item[ilow], s);
  631. }
  632. if (d < 0) { /* Delete -d items */
  633. Py_ssize_t tail;
  634. tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
  635. memmove(&item[ihigh+d], &item[ihigh], tail);
  636. if (list_resize(a, Py_SIZE(a) + d) < 0) {
  637. memmove(&item[ihigh], &item[ihigh+d], tail);
  638. memcpy(&item[ilow], recycle, s);
  639. goto Error;
  640. }
  641. item = a->ob_item;
  642. }
  643. else if (d > 0) { /* Insert d items */
  644. k = Py_SIZE(a);
  645. if (list_resize(a, k+d) < 0)
  646. goto Error;
  647. item = a->ob_item;
  648. memmove(&item[ihigh+d], &item[ihigh],
  649. (k - ihigh)*sizeof(PyObject *));
  650. }
  651. for (k = 0; k < n; k++, ilow++) {
  652. PyObject *w = vitem[k];
  653. Py_XINCREF(w);
  654. item[ilow] = w;
  655. }
  656. for (k = norig - 1; k >= 0; --k)
  657. Py_XDECREF(recycle[k]);
  658. result = 0;
  659. Error:
  660. if (recycle != recycle_on_stack)
  661. PyMem_FREE(recycle);
  662. Py_XDECREF(v_as_SF);
  663. return result;
  664. #undef b
  665. }
  666. int
  667. PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
  668. {
  669. if (!PyList_Check(a)) {
  670. PyErr_BadInternalCall();
  671. return -1;
  672. }
  673. return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
  674. }
  675. static PyObject *
  676. list_inplace_repeat(PyListObject *self, Py_ssize_t n)
  677. {
  678. PyObject **items;
  679. Py_ssize_t size, i, j, p;
  680. size = PyList_GET_SIZE(self);
  681. if (size == 0 || n == 1) {
  682. Py_INCREF(self);
  683. return (PyObject *)self;
  684. }
  685. if (n < 1) {
  686. (void)_list_clear(self);
  687. Py_INCREF(self);
  688. return (PyObject *)self;
  689. }
  690. if (size > PY_SSIZE_T_MAX / n) {
  691. return PyErr_NoMemory();
  692. }
  693. if (list_resize(self, size*n) < 0)
  694. return NULL;
  695. p = size;
  696. items = self->ob_item;
  697. for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
  698. for (j = 0; j < size; j++) {
  699. PyObject *o = items[j];
  700. Py_INCREF(o);
  701. items[p++] = o;
  702. }
  703. }
  704. Py_INCREF(self);
  705. return (PyObject *)self;
  706. }
  707. static int
  708. list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
  709. {
  710. if (!valid_index(i, Py_SIZE(a))) {
  711. PyErr_SetString(PyExc_IndexError,
  712. "list assignment index out of range");
  713. return -1;
  714. }
  715. if (v == NULL)
  716. return list_ass_slice(a, i, i+1, v);
  717. Py_INCREF(v);
  718. Py_SETREF(a->ob_item[i], v);
  719. return 0;
  720. }
  721. /*[clinic input]
  722. list.insert
  723. index: Py_ssize_t
  724. object: object
  725. /
  726. Insert object before index.
  727. [clinic start generated code]*/
  728. static PyObject *
  729. list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
  730. /*[clinic end generated code: output=7f35e32f60c8cb78 input=858514cf894c7eab]*/
  731. {
  732. if (ins1(self, index, object) == 0)
  733. Py_RETURN_NONE;
  734. return NULL;
  735. }
  736. /*[clinic input]
  737. list.clear
  738. Remove all items from list.
  739. [clinic start generated code]*/
  740. static PyObject *
  741. list_clear_impl(PyListObject *self)
  742. /*[clinic end generated code: output=67a1896c01f74362 input=ca3c1646856742f6]*/
  743. {
  744. _list_clear(self);
  745. Py_RETURN_NONE;
  746. }
  747. /*[clinic input]
  748. list.copy
  749. Return a shallow copy of the list.
  750. [clinic start generated code]*/
  751. static PyObject *
  752. list_copy_impl(PyListObject *self)
  753. /*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
  754. {
  755. return list_slice(self, 0, Py_SIZE(self));
  756. }
  757. /*[clinic input]
  758. list.append
  759. object: object
  760. /
  761. Append object to the end of the list.
  762. [clinic start generated code]*/
  763. static PyObject *
  764. list_append(PyListObject *self, PyObject *object)
  765. /*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
  766. {
  767. if (app1(self, object) == 0)
  768. Py_RETURN_NONE;
  769. return NULL;
  770. }
  771. /*[clinic input]
  772. list.extend
  773. iterable: object
  774. /
  775. Extend list by appending elements from the iterable.
  776. [clinic start generated code]*/
  777. static PyObject *
  778. list_extend(PyListObject *self, PyObject *iterable)
  779. /*[clinic end generated code: output=630fb3bca0c8e789 input=9ec5ba3a81be3a4d]*/
  780. {
  781. PyObject *it; /* iter(v) */
  782. Py_ssize_t m; /* size of self */
  783. Py_ssize_t n; /* guess for size of iterable */
  784. Py_ssize_t mn; /* m + n */
  785. Py_ssize_t i;
  786. PyObject *(*iternext)(PyObject *);
  787. /* Special cases:
  788. 1) lists and tuples which can use PySequence_Fast ops
  789. 2) extending self to self requires making a copy first
  790. */
  791. if (PyList_CheckExact(iterable) || PyTuple_CheckExact(iterable) ||
  792. (PyObject *)self == iterable) {
  793. PyObject **src, **dest;
  794. iterable = PySequence_Fast(iterable, "argument must be iterable");
  795. if (!iterable)
  796. return NULL;
  797. n = PySequence_Fast_GET_SIZE(iterable);
  798. if (n == 0) {
  799. /* short circuit when iterable is empty */
  800. Py_DECREF(iterable);
  801. Py_RETURN_NONE;
  802. }
  803. m = Py_SIZE(self);
  804. /* It should not be possible to allocate a list large enough to cause
  805. an overflow on any relevant platform */
  806. assert(m < PY_SSIZE_T_MAX - n);
  807. if (list_resize(self, m + n) < 0) {
  808. Py_DECREF(iterable);
  809. return NULL;
  810. }
  811. /* note that we may still have self == iterable here for the
  812. * situation a.extend(a), but the following code works
  813. * in that case too. Just make sure to resize self
  814. * before calling PySequence_Fast_ITEMS.
  815. */
  816. /* populate the end of self with iterable's items */
  817. src = PySequence_Fast_ITEMS(iterable);
  818. dest = self->ob_item + m;
  819. for (i = 0; i < n; i++) {
  820. PyObject *o = src[i];
  821. Py_INCREF(o);
  822. dest[i] = o;
  823. }
  824. Py_DECREF(iterable);
  825. Py_RETURN_NONE;
  826. }
  827. it = PyObject_GetIter(iterable);
  828. if (it == NULL)
  829. return NULL;
  830. iternext = *it->ob_type->tp_iternext;
  831. /* Guess a result list size. */
  832. n = PyObject_LengthHint(iterable, 8);
  833. if (n < 0) {
  834. Py_DECREF(it);
  835. return NULL;
  836. }
  837. m = Py_SIZE(self);
  838. if (m > PY_SSIZE_T_MAX - n) {
  839. /* m + n overflowed; on the chance that n lied, and there really
  840. * is enough room, ignore it. If n was telling the truth, we'll
  841. * eventually run out of memory during the loop.
  842. */
  843. }
  844. else {
  845. mn = m + n;
  846. /* Make room. */
  847. if (list_resize(self, mn) < 0)
  848. goto error;
  849. /* Make the list sane again. */
  850. Py_SIZE(self) = m;
  851. }
  852. /* Run iterator to exhaustion. */
  853. for (;;) {
  854. PyObject *item = iternext(it);
  855. if (item == NULL) {
  856. if (PyErr_Occurred()) {
  857. if (PyErr_ExceptionMatches(PyExc_StopIteration))
  858. PyErr_Clear();
  859. else
  860. goto error;
  861. }
  862. break;
  863. }
  864. if (Py_SIZE(self) < self->allocated) {
  865. /* steals ref */
  866. PyList_SET_ITEM(self, Py_SIZE(self), item);
  867. ++Py_SIZE(self);
  868. }
  869. else {
  870. int status = app1(self, item);
  871. Py_DECREF(item); /* append creates a new ref */
  872. if (status < 0)
  873. goto error;
  874. }
  875. }
  876. /* Cut back result list if initial guess was too large. */
  877. if (Py_SIZE(self) < self->allocated) {
  878. if (list_resize(self, Py_SIZE(self)) < 0)
  879. goto error;
  880. }
  881. Py_DECREF(it);
  882. Py_RETURN_NONE;
  883. error:
  884. Py_DECREF(it);
  885. return NULL;
  886. }
  887. PyObject *
  888. _PyList_Extend(PyListObject *self, PyObject *iterable)
  889. {
  890. return list_extend(self, iterable);
  891. }
  892. static PyObject *
  893. list_inplace_concat(PyListObject *self, PyObject *other)
  894. {
  895. PyObject *result;
  896. result = list_extend(self, other);
  897. if (result == NULL)
  898. return result;
  899. Py_DECREF(result);
  900. Py_INCREF(self);
  901. return (PyObject *)self;
  902. }
  903. /*[clinic input]
  904. list.pop
  905. index: Py_ssize_t = -1
  906. /
  907. Remove and return item at index (default last).
  908. Raises IndexError if list is empty or index is out of range.
  909. [clinic start generated code]*/
  910. static PyObject *
  911. list_pop_impl(PyListObject *self, Py_ssize_t index)
  912. /*[clinic end generated code: output=6bd69dcb3f17eca8 input=b83675976f329e6f]*/
  913. {
  914. PyObject *v;
  915. int status;
  916. if (Py_SIZE(self) == 0) {
  917. /* Special-case most common failure cause */
  918. PyErr_SetString(PyExc_IndexError, "pop from empty list");
  919. return NULL;
  920. }
  921. if (index < 0)
  922. index += Py_SIZE(self);
  923. if (!valid_index(index, Py_SIZE(self))) {
  924. PyErr_SetString(PyExc_IndexError, "pop index out of range");
  925. return NULL;
  926. }
  927. v = self->ob_item[index];
  928. if (index == Py_SIZE(self) - 1) {
  929. status = list_resize(self, Py_SIZE(self) - 1);
  930. if (status >= 0)
  931. return v; /* and v now owns the reference the list had */
  932. else
  933. return NULL;
  934. }
  935. Py_INCREF(v);
  936. status = list_ass_slice(self, index, index+1, (PyObject *)NULL);
  937. if (status < 0) {
  938. Py_DECREF(v);
  939. return NULL;
  940. }
  941. return v;
  942. }
  943. /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
  944. static void
  945. reverse_slice(PyObject **lo, PyObject **hi)
  946. {
  947. assert(lo && hi);
  948. --hi;
  949. while (lo < hi) {
  950. PyObject *t = *lo;
  951. *lo = *hi;
  952. *hi = t;
  953. ++lo;
  954. --hi;
  955. }
  956. }
  957. /* Lots of code for an adaptive, stable, natural mergesort. There are many
  958. * pieces to this algorithm; read listsort.txt for overviews and details.
  959. */
  960. /* A sortslice contains a pointer to an array of keys and a pointer to
  961. * an array of corresponding values. In other words, keys[i]
  962. * corresponds with values[i]. If values == NULL, then the keys are
  963. * also the values.
  964. *
  965. * Several convenience routines are provided here, so that keys and
  966. * values are always moved in sync.
  967. */
  968. typedef struct {
  969. PyObject **keys;
  970. PyObject **values;
  971. } sortslice;
  972. Py_LOCAL_INLINE(void)
  973. sortslice_copy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j)
  974. {
  975. s1->keys[i] = s2->keys[j];
  976. if (s1->values != NULL)
  977. s1->values[i] = s2->values[j];
  978. }
  979. Py_LOCAL_INLINE(void)
  980. sortslice_copy_incr(sortslice *dst, sortslice *src)
  981. {
  982. *dst->keys++ = *src->keys++;
  983. if (dst->values != NULL)
  984. *dst->values++ = *src->values++;
  985. }
  986. Py_LOCAL_INLINE(void)
  987. sortslice_copy_decr(sortslice *dst, sortslice *src)
  988. {
  989. *dst->keys-- = *src->keys--;
  990. if (dst->values != NULL)
  991. *dst->values-- = *src->values--;
  992. }
  993. Py_LOCAL_INLINE(void)
  994. sortslice_memcpy(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
  995. Py_ssize_t n)
  996. {
  997. memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
  998. if (s1->values != NULL)
  999. memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
  1000. }
  1001. Py_LOCAL_INLINE(void)
  1002. sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
  1003. Py_ssize_t n)
  1004. {
  1005. memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
  1006. if (s1->values != NULL)
  1007. memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
  1008. }
  1009. Py_LOCAL_INLINE(void)
  1010. sortslice_advance(sortslice *slice, Py_ssize_t n)
  1011. {
  1012. slice->keys += n;
  1013. if (slice->values != NULL)
  1014. slice->values += n;
  1015. }
  1016. /* Comparison function: ms->key_compare, which is set at run-time in
  1017. * listsort_impl to optimize for various special cases.
  1018. * Returns -1 on error, 1 if x < y, 0 if x >= y.
  1019. */
  1020. #define ISLT(X, Y) (*(ms->key_compare))(X, Y, ms)
  1021. /* Compare X to Y via "<". Goto "fail" if the comparison raises an
  1022. error. Else "k" is set to true iff X<Y, and an "if (k)" block is
  1023. started. It makes more sense in context <wink>. X and Y are PyObject*s.
  1024. */
  1025. #define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
  1026. if (k)
  1027. /* The maximum number of entries in a MergeState's pending-runs stack.
  1028. * This is enough to sort arrays of size up to about
  1029. * 32 * phi ** MAX_MERGE_PENDING
  1030. * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
  1031. * with 2**64 elements.
  1032. */
  1033. #define MAX_MERGE_PENDING 85
  1034. /* When we get into galloping mode, we stay there until both runs win less
  1035. * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
  1036. */
  1037. #define MIN_GALLOP 7
  1038. /* Avoid malloc for small temp arrays. */
  1039. #define MERGESTATE_TEMP_SIZE 256
  1040. /* One MergeState exists on the stack per invocation of mergesort. It's just
  1041. * a convenient way to pass state around among the helper functions.
  1042. */
  1043. struct s_slice {
  1044. sortslice base;
  1045. Py_ssize_t len;
  1046. };
  1047. typedef struct s_MergeState MergeState;
  1048. struct s_MergeState {
  1049. /* This controls when we get *into* galloping mode. It's initialized
  1050. * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
  1051. * random data, and lower for highly structured data.
  1052. */
  1053. Py_ssize_t min_gallop;
  1054. /* 'a' is temp storage to help with merges. It contains room for
  1055. * alloced entries.
  1056. */
  1057. sortslice a; /* may point to temparray below */
  1058. Py_ssize_t alloced;
  1059. /* A stack of n pending runs yet to be merged. Run #i starts at
  1060. * address base[i] and extends for len[i] elements. It's always
  1061. * true (so long as the indices are in bounds) that
  1062. *
  1063. * pending[i].base + pending[i].len == pending[i+1].base
  1064. *
  1065. * so we could cut the storage for this, but it's a minor amount,
  1066. * and keeping all the info explicit simplifies the code.
  1067. */
  1068. int n;
  1069. struct s_slice pending[MAX_MERGE_PENDING];
  1070. /* 'a' points to this when possible, rather than muck with malloc. */
  1071. PyObject *temparray[MERGESTATE_TEMP_SIZE];
  1072. /* This is the function we will use to compare two keys,
  1073. * even when none of our special cases apply and we have to use
  1074. * safe_object_compare. */
  1075. int (*key_compare)(PyObject *, PyObject *, MergeState *);
  1076. /* This function is used by unsafe_object_compare to optimize comparisons
  1077. * when we know our list is type-homogeneous but we can't assume anything else.
  1078. * In the pre-sort check it is set equal to key->ob_type->tp_richcompare */
  1079. PyObject *(*key_richcompare)(PyObject *, PyObject *, int);
  1080. /* This function is used by unsafe_tuple_compare to compare the first elements
  1081. * of tuples. It may be set to safe_object_compare, but the idea is that hopefully
  1082. * we can assume more, and use one of the special-case compares. */
  1083. int (*tuple_elem_compare)(PyObject *, PyObject *, MergeState *);
  1084. };
  1085. /* binarysort is the best method for sorting small arrays: it does
  1086. few compares, but can do data movement quadratic in the number of
  1087. elements.
  1088. [lo, hi) is a contiguous slice of a list, and is sorted via
  1089. binary insertion. This sort is stable.
  1090. On entry, must have lo <= start <= hi, and that [lo, start) is already
  1091. sorted (pass start == lo if you don't know!).
  1092. If islt() complains return -1, else 0.
  1093. Even in case of error, the output slice will be some permutation of
  1094. the input (nothing is lost or duplicated).
  1095. */
  1096. static int
  1097. binarysort(MergeState *ms, sortslice lo, PyObject **hi, PyObject **start)
  1098. {
  1099. Py_ssize_t k;
  1100. PyObject **l, **p, **r;
  1101. PyObject *pivot;
  1102. assert(lo.keys <= start && start <= hi);
  1103. /* assert [lo, start) is sorted */
  1104. if (lo.keys == start)
  1105. ++start;
  1106. for (; start < hi; ++start) {
  1107. /* set l to where *start belongs */
  1108. l = lo.keys;
  1109. r = start;
  1110. pivot = *r;
  1111. /* Invariants:
  1112. * pivot >= all in [lo, l).
  1113. * pivot < all in [r, start).
  1114. * The second is vacuously true at the start.
  1115. */
  1116. assert(l < r);
  1117. do {
  1118. p = l + ((r - l) >> 1);
  1119. IFLT(pivot, *p)
  1120. r = p;
  1121. else
  1122. l = p+1;
  1123. } while (l < r);
  1124. assert(l == r);
  1125. /* The invariants still hold, so pivot >= all in [lo, l) and
  1126. pivot < all in [l, start), so pivot belongs at l. Note
  1127. that if there are elements equal to pivot, l points to the
  1128. first slot after them -- that's why this sort is stable.
  1129. Slide over to make room.
  1130. Caution: using memmove is much slower under MSVC 5;
  1131. we're not usually moving many slots. */
  1132. for (p = start; p > l; --p)
  1133. *p = *(p-1);
  1134. *l = pivot;
  1135. if (lo.values != NULL) {
  1136. Py_ssize_t offset = lo.values - lo.keys;
  1137. p = start + offset;
  1138. pivot = *p;
  1139. l += offset;
  1140. for (p = start + offset; p > l; --p)
  1141. *p = *(p-1);
  1142. *l = pivot;
  1143. }
  1144. }
  1145. return 0;
  1146. fail:
  1147. return -1;
  1148. }
  1149. /*
  1150. Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
  1151. is required on entry. "A run" is the longest ascending sequence, with
  1152. lo[0] <= lo[1] <= lo[2] <= ...
  1153. or the longest descending sequence, with
  1154. lo[0] > lo[1] > lo[2] > ...
  1155. Boolean *descending is set to 0 in the former case, or to 1 in the latter.
  1156. For its intended use in a stable mergesort, the strictness of the defn of
  1157. "descending" is needed so that the caller can safely reverse a descending
  1158. sequence without violating stability (strict > ensures there are no equal
  1159. elements to get out of order).
  1160. Returns -1 in case of error.
  1161. */
  1162. static Py_ssize_t
  1163. count_run(MergeState *ms, PyObject **lo, PyObject **hi, int *descending)
  1164. {
  1165. Py_ssize_t k;
  1166. Py_ssize_t n;
  1167. assert(lo < hi);
  1168. *descending = 0;
  1169. ++lo;
  1170. if (lo == hi)
  1171. return 1;
  1172. n = 2;
  1173. IFLT(*lo, *(lo-1)) {
  1174. *descending = 1;
  1175. for (lo = lo+1; lo < hi; ++lo, ++n) {
  1176. IFLT(*lo, *(lo-1))
  1177. ;
  1178. else
  1179. break;
  1180. }
  1181. }
  1182. else {
  1183. for (lo = lo+1; lo < hi; ++lo, ++n) {
  1184. IFLT(*lo, *(lo-1))
  1185. break;
  1186. }
  1187. }
  1188. return n;
  1189. fail:
  1190. return -1;
  1191. }
  1192. /*
  1193. Locate the proper position of key in a sorted vector; if the vector contains
  1194. an element equal to key, return the position immediately to the left of
  1195. the leftmost equal element. [gallop_right() does the same except returns
  1196. the position to the right of the rightmost equal element (if any).]
  1197. "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
  1198. "hint" is an index at which to begin the search, 0 <= hint < n. The closer
  1199. hint is to the final result, the faster this runs.
  1200. The return value is the int k in 0..n such that
  1201. a[k-1] < key <= a[k]
  1202. pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
  1203. key belongs at index k; or, IOW, the first k elements of a should precede
  1204. key, and the last n-k should follow key.
  1205. Returns -1 on error. See listsort.txt for info on the method.
  1206. */
  1207. static Py_ssize_t
  1208. gallop_left(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
  1209. {
  1210. Py_ssize_t ofs;
  1211. Py_ssize_t lastofs;
  1212. Py_ssize_t k;
  1213. assert(key && a && n > 0 && hint >= 0 && hint < n);
  1214. a += hint;
  1215. lastofs = 0;
  1216. ofs = 1;
  1217. IFLT(*a, key) {
  1218. /* a[hint] < key -- gallop right, until
  1219. * a[hint + lastofs] < key <= a[hint + ofs]
  1220. */
  1221. const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
  1222. while (ofs < maxofs) {
  1223. IFLT(a[ofs], key) {
  1224. lastofs = ofs;
  1225. ofs = (ofs << 1) + 1;
  1226. if (ofs <= 0) /* int overflow */
  1227. ofs = maxofs;
  1228. }
  1229. else /* key <= a[hint + ofs] */
  1230. break;
  1231. }
  1232. if (ofs > maxofs)
  1233. ofs = maxofs;
  1234. /* Translate back to offsets relative to &a[0]. */
  1235. lastofs += hint;
  1236. ofs += hint;
  1237. }
  1238. else {
  1239. /* key <= a[hint] -- gallop left, until
  1240. * a[hint - ofs] < key <= a[hint - lastofs]
  1241. */
  1242. const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
  1243. while (ofs < maxofs) {
  1244. IFLT(*(a-ofs), key)
  1245. break;
  1246. /* key <= a[hint - ofs] */
  1247. lastofs = ofs;
  1248. ofs = (ofs << 1) + 1;
  1249. if (ofs <= 0) /* int overflow */
  1250. ofs = maxofs;
  1251. }
  1252. if (ofs > maxofs)
  1253. ofs = maxofs;
  1254. /* Translate back to positive offsets relative to &a[0]. */
  1255. k = lastofs;
  1256. lastofs = hint - ofs;
  1257. ofs = hint - k;
  1258. }
  1259. a -= hint;
  1260. assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
  1261. /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
  1262. * right of lastofs but no farther right than ofs. Do a binary
  1263. * search, with invariant a[lastofs-1] < key <= a[ofs].
  1264. */
  1265. ++lastofs;
  1266. while (lastofs < ofs) {
  1267. Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
  1268. IFLT(a[m], key)
  1269. lastofs = m+1; /* a[m] < key */
  1270. else
  1271. ofs = m; /* key <= a[m] */
  1272. }
  1273. assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
  1274. return ofs;
  1275. fail:
  1276. return -1;
  1277. }
  1278. /*
  1279. Exactly like gallop_left(), except that if key already exists in a[0:n],
  1280. finds the position immediately to the right of the rightmost equal value.
  1281. The return value is the int k in 0..n such that
  1282. a[k-1] <= key < a[k]
  1283. or -1 if error.
  1284. The code duplication is massive, but this is enough different given that
  1285. we're sticking to "<" comparisons that it's much harder to follow if
  1286. written as one routine with yet another "left or right?" flag.
  1287. */
  1288. static Py_ssize_t
  1289. gallop_right(MergeState *ms, PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
  1290. {
  1291. Py_ssize_t ofs;
  1292. Py_ssize_t lastofs;
  1293. Py_ssize_t k;
  1294. assert(key && a && n > 0 && hint >= 0 && hint < n);
  1295. a += hint;
  1296. lastofs = 0;
  1297. ofs = 1;
  1298. IFLT(key, *a) {
  1299. /* key < a[hint] -- gallop left, until
  1300. * a[hint - ofs] <= key < a[hint - lastofs]
  1301. */
  1302. const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
  1303. while (ofs < maxofs) {
  1304. IFLT(key, *(a-ofs)) {
  1305. lastofs = ofs;
  1306. ofs = (ofs << 1) + 1;
  1307. if (ofs <= 0) /* int overflow */
  1308. ofs = maxofs;
  1309. }
  1310. else /* a[hint - ofs] <= key */
  1311. break;
  1312. }
  1313. if (ofs > maxofs)
  1314. ofs = maxofs;
  1315. /* Translate back to positive offsets relative to &a[0]. */
  1316. k = lastofs;
  1317. lastofs = hint - ofs;
  1318. ofs = hint - k;
  1319. }
  1320. else {
  1321. /* a[hint] <= key -- gallop right, until
  1322. * a[hint + lastofs] <= key < a[hint + ofs]
  1323. */
  1324. const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
  1325. while (ofs < maxofs) {
  1326. IFLT(key, a[ofs])
  1327. break;
  1328. /* a[hint + ofs] <= key */
  1329. lastofs = ofs;
  1330. ofs = (ofs << 1) + 1;
  1331. if (ofs <= 0) /* int overflow */
  1332. ofs = maxofs;
  1333. }
  1334. if (ofs > maxofs)
  1335. ofs = maxofs;
  1336. /* Translate back to offsets relative to &a[0]. */
  1337. lastofs += hint;
  1338. ofs += hint;
  1339. }
  1340. a -= hint;
  1341. assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
  1342. /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
  1343. * right of lastofs but no farther right than ofs. Do a binary
  1344. * search, with invariant a[lastofs-1] <= key < a[ofs].
  1345. */
  1346. ++lastofs;
  1347. while (lastofs < ofs) {
  1348. Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
  1349. IFLT(key, a[m])
  1350. ofs = m; /* key < a[m] */
  1351. else
  1352. lastofs = m+1; /* a[m] <= key */
  1353. }
  1354. assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
  1355. return ofs;
  1356. fail:
  1357. return -1;
  1358. }
  1359. /* Conceptually a MergeState's constructor. */
  1360. static void
  1361. merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
  1362. {
  1363. assert(ms != NULL);
  1364. if (has_keyfunc) {
  1365. /* The temporary space for merging will need at most half the list
  1366. * size rounded up. Use the minimum possible space so we can use the
  1367. * rest of temparray for other things. In particular, if there is
  1368. * enough extra space, listsort() will use it to store the keys.
  1369. */
  1370. ms->alloced = (list_size + 1) / 2;
  1371. /* ms->alloced describes how many keys will be stored at
  1372. ms->temparray, but we also need to store the values. Hence,
  1373. ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
  1374. if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
  1375. ms->alloced = MERGESTATE_TEMP_SIZE / 2;
  1376. ms->a.values = &ms->temparray[ms->alloced];
  1377. }
  1378. else {
  1379. ms->alloced = MERGESTATE_TEMP_SIZE;
  1380. ms->a.values = NULL;
  1381. }
  1382. ms->a.keys = ms->temparray;
  1383. ms->n = 0;
  1384. ms->min_gallop = MIN_GALLOP;
  1385. }
  1386. /* Free all the temp memory owned by the MergeState. This must be called
  1387. * when you're done with a MergeState, and may be called before then if
  1388. * you want to free the temp memory early.
  1389. */
  1390. static void
  1391. merge_freemem(MergeState *ms)
  1392. {
  1393. assert(ms != NULL);
  1394. if (ms->a.keys != ms->temparray)
  1395. PyMem_Free(ms->a.keys);
  1396. }
  1397. /* Ensure enough temp memory for 'need' array slots is available.
  1398. * Returns 0 on success and -1 if the memory can't be gotten.
  1399. */
  1400. static int
  1401. merge_getmem(MergeState *ms, Py_ssize_t need)
  1402. {
  1403. int multiplier;
  1404. assert(ms != NULL);
  1405. if (need <= ms->alloced)
  1406. return 0;
  1407. multiplier = ms->a.values != NULL ? 2 : 1;
  1408. /* Don't realloc! That can cost cycles to copy the old data, but
  1409. * we don't care what's in the block.
  1410. */
  1411. merge_freemem(ms);
  1412. if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject *) / multiplier) {
  1413. PyErr_NoMemory();
  1414. return -1;
  1415. }
  1416. ms->a.keys = (PyObject **)PyMem_Malloc(multiplier * need
  1417. * sizeof(PyObject *));
  1418. if (ms->a.keys != NULL) {
  1419. ms->alloced = need;
  1420. if (ms->a.values != NULL)
  1421. ms->a.values = &ms->a.keys[need];
  1422. return 0;
  1423. }
  1424. PyErr_NoMemory();
  1425. return -1;
  1426. }
  1427. #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
  1428. merge_getmem(MS, NEED))
  1429. /* Merge the na elements starting at ssa with the nb elements starting at
  1430. * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
  1431. * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
  1432. * should have na <= nb. See listsort.txt for more info. Return 0 if
  1433. * successful, -1 if error.
  1434. */
  1435. static Py_ssize_t
  1436. merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
  1437. sortslice ssb, Py_ssize_t nb)
  1438. {
  1439. Py_ssize_t k;
  1440. sortslice dest;
  1441. int result = -1; /* guilty until proved innocent */
  1442. Py_ssize_t min_gallop;
  1443. assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
  1444. assert(ssa.keys + na == ssb.keys);
  1445. if (MERGE_GETMEM(ms, na) < 0)
  1446. return -1;
  1447. sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
  1448. dest = ssa;
  1449. ssa = ms->a;
  1450. sortslice_copy_incr(&dest, &ssb);
  1451. --nb;
  1452. if (nb == 0)
  1453. goto Succeed;
  1454. if (na == 1)
  1455. goto CopyB;
  1456. min_gallop = ms->min_gallop;
  1457. for (;;) {
  1458. Py_ssize_t acount = 0; /* # of times A won in a row */
  1459. Py_ssize_t bcount = 0; /* # of times B won in a row */
  1460. /* Do the straightforward thing until (if ever) one run
  1461. * appears to win consistently.
  1462. */
  1463. for (;;) {
  1464. assert(na > 1 && nb > 0);
  1465. k = ISLT(ssb.keys[0], ssa.keys[0]);
  1466. if (k) {
  1467. if (k < 0)
  1468. goto Fail;
  1469. sortslice_copy_incr(&dest, &ssb);
  1470. ++bcount;
  1471. acount = 0;
  1472. --nb;
  1473. if (nb == 0)
  1474. goto Succeed;
  1475. if (bcount >= min_gallop)
  1476. break;
  1477. }
  1478. else {
  1479. sortslice_copy_incr(&dest, &ssa);
  1480. ++acount;
  1481. bcount = 0;
  1482. --na;
  1483. if (na == 1)
  1484. goto CopyB;
  1485. if (acount >= min_gallop)
  1486. break;
  1487. }
  1488. }
  1489. /* One run is winning so consistently that galloping may
  1490. * be a huge win. So try that, and continue galloping until
  1491. * (if ever) neither run appears to be winning consistently
  1492. * anymore.
  1493. */
  1494. ++min_gallop;
  1495. do {
  1496. assert(na > 1 && nb > 0);
  1497. min_gallop -= min_gallop > 1;
  1498. ms->min_gallop = min_gallop;
  1499. k = gallop_right(ms, ssb.keys[0], ssa.keys, na, 0);
  1500. acount = k;
  1501. if (k) {
  1502. if (k < 0)
  1503. goto Fail;
  1504. sortslice_memcpy(&dest, 0, &ssa, 0, k);
  1505. sortslice_advance(&dest, k);
  1506. sortslice_advance(&ssa, k);
  1507. na -= k;
  1508. if (na == 1)
  1509. goto CopyB;
  1510. /* na==0 is impossible now if the comparison
  1511. * function is consistent, but we can't assume
  1512. * that it is.
  1513. */
  1514. if (na == 0)
  1515. goto Succeed;
  1516. }
  1517. sortslice_copy_incr(&dest, &ssb);
  1518. --nb;
  1519. if (nb == 0)
  1520. goto Succeed;
  1521. k = gallop_left(ms, ssa.keys[0], ssb.keys, nb, 0);
  1522. bcount = k;
  1523. if (k) {
  1524. if (k < 0)
  1525. goto Fail;
  1526. sortslice_memmove(&dest, 0, &ssb, 0, k);
  1527. sortslice_advance(&dest, k);
  1528. sortslice_advance(&ssb, k);
  1529. nb -= k;
  1530. if (nb == 0)
  1531. goto Succeed;
  1532. }
  1533. sortslice_copy_incr(&dest, &ssa);
  1534. --na;
  1535. if (na == 1)
  1536. goto CopyB;
  1537. } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
  1538. ++min_gallop; /* penalize it for leaving galloping mode */
  1539. ms->min_gallop = min_gallop;
  1540. }
  1541. Succeed:
  1542. result = 0;
  1543. Fail:
  1544. if (na)
  1545. sortslice_memcpy(&dest, 0, &ssa, 0, na);
  1546. return result;
  1547. CopyB:
  1548. assert(na == 1 && nb > 0);
  1549. /* The last element of ssa belongs at the end of the merge. */
  1550. sortslice_memmove(&dest, 0, &ssb, 0, nb);
  1551. sortslice_copy(&dest, nb, &ssa, 0);
  1552. return 0;
  1553. }
  1554. /* Merge the na elements starting at pa with the nb elements starting at
  1555. * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
  1556. * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
  1557. * should have na >= nb. See listsort.txt for more info. Return 0 if
  1558. * successful, -1 if error.
  1559. */
  1560. static Py_ssize_t
  1561. merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
  1562. sortslice ssb, Py_ssize_t nb)
  1563. {
  1564. Py_ssize_t k;
  1565. sortslice dest, basea, baseb;
  1566. int result = -1; /* guilty until proved innocent */
  1567. Py_ssize_t min_gallop;
  1568. assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
  1569. assert(ssa.keys + na == ssb.keys);
  1570. if (MERGE_GETMEM(ms, nb) < 0)
  1571. return -1;
  1572. dest = ssb;
  1573. sortslice_advance(&dest, nb-1);
  1574. sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
  1575. basea = ssa;
  1576. baseb = ms->a;
  1577. ssb.keys = ms->a.keys + nb - 1;
  1578. if (ssb.values != NULL)
  1579. ssb.values = ms->a.values + nb - 1;
  1580. sortslice_advance(&ssa, na - 1);
  1581. sortslice_copy_decr(&dest, &ssa);
  1582. --na;
  1583. if (na == 0)
  1584. goto Succeed;
  1585. if (nb == 1)
  1586. goto CopyA;
  1587. min_gallop = ms->min_gallop;
  1588. for (;;) {
  1589. Py_ssize_t acount = 0; /* # of times A won in a row */
  1590. Py_ssize_t bcount = 0; /* # of times B won in a row */
  1591. /* Do the straightforward thing until (if ever) one run
  1592. * appears to win consistently.
  1593. */
  1594. for (;;) {
  1595. assert(na > 0 && nb > 1);
  1596. k = ISLT(ssb.keys[0], ssa.keys[0]);
  1597. if (k) {
  1598. if (k < 0)
  1599. goto Fail;
  1600. sortslice_copy_decr(&dest, &ssa);
  1601. ++acount;
  1602. bcount = 0;
  1603. --na;
  1604. if (na == 0)
  1605. goto Succeed;
  1606. if (acount >= min_gallop)
  1607. break;
  1608. }
  1609. else {
  1610. sortslice_copy_decr(&dest, &ssb);
  1611. ++bcount;
  1612. acount = 0;
  1613. --nb;
  1614. if (nb == 1)
  1615. goto CopyA;
  1616. if (bcount >= min_gallop)
  1617. break;
  1618. }
  1619. }
  1620. /* One run is winning so consistently that galloping may
  1621. * be a huge win. So try that, and continue galloping until
  1622. * (if ever) neither run appears to be winning consistently
  1623. * anymore.
  1624. */
  1625. ++min_gallop;
  1626. do {
  1627. assert(na > 0 && nb > 1);
  1628. min_gallop -= min_gallop > 1;
  1629. ms->min_gallop = min_gallop;
  1630. k = gallop_right(ms, ssb.keys[0], basea.keys, na, na-1);
  1631. if (k < 0)
  1632. goto Fail;
  1633. k = na - k;
  1634. acount = k;
  1635. if (k) {
  1636. sortslice_advance(&dest, -k);
  1637. sortslice_advance(&ssa, -k);
  1638. sortslice_memmove(&dest, 1, &ssa, 1, k);
  1639. na -= k;
  1640. if (na == 0)
  1641. goto Succeed;
  1642. }
  1643. sortslice_copy_decr(&dest, &ssb);
  1644. --nb;
  1645. if (nb == 1)
  1646. goto CopyA;
  1647. k = gallop_left(ms, ssa.keys[0], baseb.keys, nb, nb-1);
  1648. if (k < 0)
  1649. goto Fail;
  1650. k = nb - k;
  1651. bcount = k;
  1652. if (k) {
  1653. sortslice_advance(&dest, -k);
  1654. sortslice_advance(&ssb, -k);
  1655. sortslice_memcpy(&dest, 1, &ssb, 1, k);
  1656. nb -= k;
  1657. if (nb == 1)
  1658. goto CopyA;
  1659. /* nb==0 is impossible now if the comparison
  1660. * function is consistent, but we can't assume
  1661. * that it is.
  1662. */
  1663. if (nb == 0)
  1664. goto Succeed;
  1665. }
  1666. sortslice_copy_decr(&dest, &ssa);
  1667. --na;
  1668. if (na == 0)
  1669. goto Succeed;
  1670. } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
  1671. ++min_gallop; /* penalize it for leaving galloping mode */
  1672. ms->min_gallop = min_gallop;
  1673. }
  1674. Succeed:
  1675. result = 0;
  1676. Fail:
  1677. if (nb)
  1678. sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
  1679. return result;
  1680. CopyA:
  1681. assert(nb == 1 && na > 0);
  1682. /* The first element of ssb belongs at the front of the merge. */
  1683. sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
  1684. sortslice_advance(&dest, -na);
  1685. sortslice_advance(&ssa, -na);
  1686. sortslice_copy(&dest, 0, &ssb, 0);
  1687. return 0;
  1688. }
  1689. /* Merge the two runs at stack indices i and i+1.
  1690. * Returns 0 on success, -1 on error.
  1691. */
  1692. static Py_ssize_t
  1693. merge_at(MergeState *ms, Py_ssize_t i)
  1694. {
  1695. sortslice ssa, ssb;
  1696. Py_ssize_t na, nb;
  1697. Py_ssize_t k;
  1698. assert(ms != NULL);
  1699. assert(ms->n >= 2);
  1700. assert(i >= 0);
  1701. assert(i == ms->n - 2 || i == ms->n - 3);
  1702. ssa = ms->pending[i].base;
  1703. na = ms->pending[i].len;
  1704. ssb = ms->pending[i+1].base;
  1705. nb = ms->pending[i+1].len;
  1706. assert(na > 0 && nb > 0);
  1707. assert(ssa.keys + na == ssb.keys);
  1708. /* Record the length of the combined runs; if i is the 3rd-last
  1709. * run now, also slide over the last run (which isn't involved
  1710. * in this merge). The current run i+1 goes away in any case.
  1711. */
  1712. ms->pending[i].len = na + nb;
  1713. if (i == ms->n - 3)
  1714. ms->pending[i+1] = ms->pending[i+2];
  1715. --ms->n;
  1716. /* Where does b start in a? Elements in a before that can be
  1717. * ignored (already in place).
  1718. */
  1719. k = gallop_right(ms, *ssb.keys, ssa.keys, na, 0);
  1720. if (k < 0)
  1721. return -1;
  1722. sortslice_advance(&ssa, k);
  1723. na -= k;
  1724. if (na == 0)
  1725. return 0;
  1726. /* Where does a end in b? Elements in b after that can be
  1727. * ignored (already in place).
  1728. */
  1729. nb = gallop_left(ms, ssa.keys[na-1], ssb.keys, nb, nb-1);
  1730. if (nb <= 0)
  1731. return nb;
  1732. /* Merge what remains of the runs, using a temp array with
  1733. * min(na, nb) elements.
  1734. */
  1735. if (na <= nb)
  1736. return merge_lo(ms, ssa, na, ssb, nb);
  1737. else
  1738. return merge_hi(ms, ssa, na, ssb, nb);
  1739. }
  1740. /* Examine the stack of runs waiting to be merged, merging adjacent runs
  1741. * until the stack invariants are re-established:
  1742. *
  1743. * 1. len[-3] > len[-2] + len[-1]
  1744. * 2. len[-2] > len[-1]
  1745. *
  1746. * See listsort.txt for more info.
  1747. *
  1748. * Returns 0 on success, -1 on error.
  1749. */
  1750. static int
  1751. merge_collapse(MergeState *ms)
  1752. {
  1753. struct s_slice *p = ms->pending;
  1754. assert(ms);
  1755. while (ms->n > 1) {
  1756. Py_ssize_t n = ms->n - 2;
  1757. if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
  1758. (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
  1759. if (p[n-1].len < p[n+1].len)
  1760. --n;
  1761. if (merge_at(ms, n) < 0)
  1762. return -1;
  1763. }
  1764. else if (p[n].len <= p[n+1].len) {
  1765. if (merge_at(ms, n) < 0)
  1766. return -1;
  1767. }
  1768. else
  1769. break;
  1770. }
  1771. return 0;
  1772. }
  1773. /* Regardless of invariants, merge all runs on the stack until only one
  1774. * remains. This is used at the end of the mergesort.
  1775. *
  1776. * Returns 0 on success, -1 on error.
  1777. */
  1778. static int
  1779. merge_force_collapse(MergeState *ms)
  1780. {
  1781. struct s_slice *p = ms->pending;
  1782. assert(ms);
  1783. while (ms->n > 1) {
  1784. Py_ssize_t n = ms->n - 2;
  1785. if (n > 0 && p[n-1].len < p[n+1].len)
  1786. --n;
  1787. if (merge_at(ms, n) < 0)
  1788. return -1;
  1789. }
  1790. return 0;
  1791. }
  1792. /* Compute a good value for the minimum run length; natural runs shorter
  1793. * than this are boosted artificially via binary insertion.
  1794. *
  1795. * If n < 64, return n (it's too small to bother with fancy stuff).
  1796. * Else if n is an exact power of 2, return 32.
  1797. * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
  1798. * strictly less than, an exact power of 2.
  1799. *
  1800. * See listsort.txt for more info.
  1801. */
  1802. static Py_ssize_t
  1803. merge_compute_minrun(Py_ssize_t n)
  1804. {
  1805. Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
  1806. assert(n >= 0);
  1807. while (n >= 64) {
  1808. r |= n & 1;
  1809. n >>= 1;
  1810. }
  1811. return n + r;
  1812. }
  1813. static void
  1814. reverse_sortslice(sortslice *s, Py_ssize_t n)
  1815. {
  1816. reverse_slice(s->keys, &s->keys[n]);
  1817. if (s->values != NULL)
  1818. reverse_slice(s->values, &s->values[n]);
  1819. }
  1820. /* Here we define custom comparison functions to optimize for the cases one commonly
  1821. * encounters in practice: homogeneous lists, often of one of the basic types. */
  1822. /* This struct holds the comparison function and helper functions
  1823. * selected in the pre-sort check. */
  1824. /* These are the special case compare functions.
  1825. * ms->key_compare will always point to one of these: */
  1826. /* Heterogeneous compare: default, always safe to fall back on. */
  1827. static int
  1828. safe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
  1829. {
  1830. /* No assumptions necessary! */
  1831. return PyObject_RichCompareBool(v, w, Py_LT);
  1832. }
  1833. /* Homogeneous compare: safe for any two compareable objects of the same type.
  1834. * (ms->key_richcompare is set to ob_type->tp_richcompare in the
  1835. * pre-sort check.)
  1836. */
  1837. static int
  1838. unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
  1839. {
  1840. PyObject *res_obj; int res;
  1841. /* No assumptions, because we check first: */
  1842. if (v->ob_type->tp_richcompare != ms->key_richcompare)
  1843. return PyObject_RichCompareBool(v, w, Py_LT);
  1844. assert(ms->key_richcompare != NULL);
  1845. res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
  1846. if (res_obj == Py_NotImplemented) {
  1847. Py_DECREF(res_obj);
  1848. return PyObject_RichCompareBool(v, w, Py_LT);
  1849. }
  1850. if (res_obj == NULL)
  1851. return -1;
  1852. if (PyBool_Check(res_obj)) {
  1853. res = (res_obj == Py_True);
  1854. }
  1855. else {
  1856. res = PyObject_IsTrue(res_obj);
  1857. }
  1858. Py_DECREF(res_obj);
  1859. /* Note that we can't assert
  1860. * res == PyObject_RichCompareBool(v, w, Py_LT);
  1861. * because of evil compare functions like this:
  1862. * lambda a, b: int(random.random() * 3) - 1)
  1863. * (which is actually in test_sort.py) */
  1864. return res;
  1865. }
  1866. /* Latin string compare: safe for any two latin (one byte per char) strings. */
  1867. static int
  1868. unsafe_latin_compare(PyObject *v, PyObject *w, MergeState *ms)
  1869. {
  1870. Py_ssize_t len;
  1871. int res;
  1872. /* Modified from Objects/unicodeobject.c:unicode_compare, assuming: */
  1873. assert(v->ob_type == w->ob_type);
  1874. assert(v->ob_type == &PyUnicode_Type);
  1875. assert(PyUnicode_KIND(v) == PyUnicode_KIND(w));
  1876. assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
  1877. len = Py_MIN(PyUnicode_GET_LENGTH(v), PyUnicode_GET_LENGTH(w));
  1878. res = memcmp(PyUnicode_DATA(v), PyUnicode_DATA(w), len);
  1879. res = (res != 0 ?
  1880. res < 0 :
  1881. PyUnicode_GET_LENGTH(v) < PyUnicode_GET_LENGTH(w));
  1882. assert(res == PyObject_RichCompareBool(v, w, Py_LT));;
  1883. return res;
  1884. }
  1885. /* Bounded int compare: compare any two longs that fit in a single machine word. */
  1886. static int
  1887. unsafe_long_compare(PyObject *v, PyObject *w, MergeState *ms)
  1888. {
  1889. PyLongObject *vl, *wl; sdigit v0, w0; int res;
  1890. /* Modified from Objects/longobject.c:long_compare, assuming: */
  1891. assert(v->ob_type == w->ob_type);
  1892. assert(v->ob_type == &PyLong_Type);
  1893. assert(Py_ABS(Py_SIZE(v)) <= 1);
  1894. assert(Py_ABS(Py_SIZE(w)) <= 1);
  1895. vl = (PyLongObject*)v;
  1896. wl = (PyLongObject*)w;
  1897. v0 = Py_SIZE(vl) == 0 ? 0 : (sdigit)vl->ob_digit[0];
  1898. w0 = Py_SIZE(wl) == 0 ? 0 : (sdigit)wl->ob_digit[0];
  1899. if (Py_SIZE(vl) < 0)
  1900. v0 = -v0;
  1901. if (Py_SIZE(wl) < 0)
  1902. w0 = -w0;
  1903. res = v0 < w0;
  1904. assert(res == PyObject_RichCompareBool(v, w, Py_LT));
  1905. return res;
  1906. }
  1907. /* Float compare: compare any two floats. */
  1908. static int
  1909. unsafe_float_compare(PyObject *v, PyObject *w, MergeState *ms)
  1910. {
  1911. int res;
  1912. /* Modified from Objects/floatobject.c:float_richcompare, assuming: */
  1913. assert(v->ob_type == w->ob_type);
  1914. assert(v->ob_type == &PyFloat_Type);
  1915. res = PyFloat_AS_DOUBLE(v) < PyFloat_AS_DOUBLE(w);
  1916. assert(res == PyObject_RichCompareBool(v, w, Py_LT));
  1917. return res;
  1918. }
  1919. /* Tuple compare: compare *any* two tuples, using
  1920. * ms->tuple_elem_compare to compare the first elements, which is set
  1921. * using the same pre-sort check as we use for ms->key_compare,
  1922. * but run on the list [x[0] for x in L]. This allows us to optimize compares
  1923. * on two levels (as long as [x[0] for x in L] is type-homogeneous.) The idea is
  1924. * that most tuple compares don't involve x[1:]. */
  1925. static int
  1926. unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
  1927. {
  1928. PyTupleObject *vt, *wt;
  1929. Py_ssize_t i, vlen, wlen;
  1930. int k;
  1931. /* Modified from Objects/tupleobject.c:tuplerichcompare, assuming: */
  1932. assert(v->ob_type == w->ob_type);
  1933. assert(v->ob_type == &PyTuple_Type);
  1934. assert(Py_SIZE(v) > 0);
  1935. assert(Py_SIZE(w) > 0);
  1936. vt = (PyTupleObject *)v;
  1937. wt = (PyTupleObject *)w;
  1938. vlen = Py_SIZE(vt);
  1939. wlen = Py_SIZE(wt);
  1940. for (i = 0; i < vlen && i < wlen; i++) {
  1941. k = PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_EQ);
  1942. if (k < 0)
  1943. return -1;
  1944. if (!k)
  1945. break;
  1946. }
  1947. if (i >= vlen || i >= wlen)
  1948. return vlen < wlen;
  1949. if (i == 0)
  1950. return ms->tuple_elem_compare(vt->ob_item[i], wt->ob_item[i], ms);
  1951. else
  1952. return PyObject_RichCompareBool(vt->ob_item[i], wt->ob_item[i], Py_LT);
  1953. }
  1954. /* An adaptive, stable, natural mergesort. See listsort.txt.
  1955. * Returns Py_None on success, NULL on error. Even in case of error, the
  1956. * list will be some permutation of its input state (nothing is lost or
  1957. * duplicated).
  1958. */
  1959. /*[clinic input]
  1960. list.sort
  1961. *
  1962. key as keyfunc: object = None
  1963. reverse: bool(accept={int}) = False
  1964. Stable sort *IN PLACE*.
  1965. [clinic start generated code]*/
  1966. static PyObject *
  1967. list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
  1968. /*[clinic end generated code: output=57b9f9c5e23fbe42 input=b0fcf743982c5b90]*/
  1969. {
  1970. MergeState ms;
  1971. Py_ssize_t nremaining;
  1972. Py_ssize_t minrun;
  1973. sortslice lo;
  1974. Py_ssize_t saved_ob_size, saved_allocated;
  1975. PyObject **saved_ob_item;
  1976. PyObject **final_ob_item;
  1977. PyObject *result = NULL; /* guilty until proved innocent */
  1978. Py_ssize_t i;
  1979. PyObject **keys;
  1980. assert(self != NULL);
  1981. assert(PyList_Check(self));
  1982. if (keyfunc == Py_None)
  1983. keyfunc = NULL;
  1984. /* The list is temporarily made empty, so that mutations performed
  1985. * by comparison functions can't affect the slice of memory we're
  1986. * sorting (allowing mutations during sorting is a core-dump
  1987. * factory, since ob_item may change).
  1988. */
  1989. saved_ob_size = Py_SIZE(self);
  1990. saved_ob_item = self->ob_item;
  1991. saved_allocated = self->allocated;
  1992. Py_SIZE(self) = 0;
  1993. self->ob_item = NULL;
  1994. self->allocated = -1; /* any operation will reset it to >= 0 */
  1995. if (keyfunc == NULL) {
  1996. keys = NULL;
  1997. lo.keys = saved_ob_item;
  1998. lo.values = NULL;
  1999. }
  2000. else {
  2001. if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
  2002. /* Leverage stack space we allocated but won't otherwise use */
  2003. keys = &ms.temparray[saved_ob_size+1];
  2004. else {
  2005. keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
  2006. if (keys == NULL) {
  2007. PyErr_NoMemory();
  2008. goto keyfunc_fail;
  2009. }
  2010. }
  2011. for (i = 0; i < saved_ob_size ; i++) {
  2012. keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
  2013. NULL);
  2014. if (keys[i] == NULL) {
  2015. for (i=i-1 ; i>=0 ; i--)
  2016. Py_DECREF(keys[i]);
  2017. if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
  2018. PyMem_FREE(keys);
  2019. goto keyfunc_fail;
  2020. }
  2021. }
  2022. lo.keys = keys;
  2023. lo.values = saved_ob_item;
  2024. }
  2025. /* The pre-sort check: here's where we decide which compare function to use.
  2026. * How much optimization is safe? We test for homogeneity with respect to
  2027. * several properties that are expensive to check at compare-time, and
  2028. * set ms appropriately. */
  2029. if (saved_ob_size > 1) {
  2030. /* Assume the first element is representative of the whole list. */
  2031. int keys_are_in_tuples = (lo.keys[0]->ob_type == &PyTuple_Type &&
  2032. Py_SIZE(lo.keys[0]) > 0);
  2033. PyTypeObject* key_type = (keys_are_in_tuples ?
  2034. PyTuple_GET_ITEM(lo.keys[0], 0)->ob_type :
  2035. lo.keys[0]->ob_type);
  2036. int keys_are_all_same_type = 1;
  2037. int strings_are_latin = 1;
  2038. int ints_are_bounded = 1;
  2039. /* Prove that assumption by checking every key. */
  2040. for (i=0; i < saved_ob_size; i++) {
  2041. if (keys_are_in_tuples &&
  2042. !(lo.keys[i]->ob_type == &PyTuple_Type && Py_SIZE(lo.keys[i]) != 0)) {
  2043. keys_are_in_tuples = 0;
  2044. keys_are_all_same_type = 0;
  2045. break;
  2046. }
  2047. /* Note: for lists of tuples, key is the first element of the tuple
  2048. * lo.keys[i], not lo.keys[i] itself! We verify type-homogeneity
  2049. * for lists of tuples in the if-statement directly above. */
  2050. PyObject *key = (keys_are_in_tuples ?
  2051. PyTuple_GET_ITEM(lo.keys[i], 0) :
  2052. lo.keys[i]);
  2053. if (key->ob_type != key_type) {
  2054. keys_are_all_same_type = 0;
  2055. break;
  2056. }
  2057. if (key_type == &PyLong_Type) {
  2058. if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
  2059. ints_are_bounded = 0;
  2060. }
  2061. else if (key_type == &PyUnicode_Type){
  2062. if (strings_are_latin &&
  2063. PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
  2064. strings_are_latin = 0;
  2065. }
  2066. }
  2067. /* Choose the best compare, given what we now know about the keys. */
  2068. if (keys_are_all_same_type) {
  2069. if (key_type == &PyUnicode_Type && strings_are_latin) {
  2070. ms.key_compare = unsafe_latin_compare;
  2071. }
  2072. else if (key_type == &PyLong_Type && ints_are_bounded) {
  2073. ms.key_compare = unsafe_long_compare;
  2074. }
  2075. else if (key_type == &PyFloat_Type) {
  2076. ms.key_compare = unsafe_float_compare;
  2077. }
  2078. else if ((ms.key_richcompare = key_type->tp_richcompare) != NULL) {
  2079. ms.key_compare = unsafe_object_compare;
  2080. }
  2081. }
  2082. else {
  2083. ms.key_compare = safe_object_compare;
  2084. }
  2085. if (keys_are_in_tuples) {
  2086. /* Make sure we're not dealing with tuples of tuples
  2087. * (remember: here, key_type refers list [key[0] for key in keys]) */
  2088. if (key_type == &PyTuple_Type)
  2089. ms.tuple_elem_compare = safe_object_compare;
  2090. else
  2091. ms.tuple_elem_compare = ms.key_compare;
  2092. ms.key_compare = unsafe_tuple_compare;
  2093. }
  2094. }
  2095. /* End of pre-sort check: ms is now set properly! */
  2096. merge_init(&ms, saved_ob_size, keys != NULL);
  2097. nremaining = saved_ob_size;
  2098. if (nremaining < 2)
  2099. goto succeed;
  2100. /* Reverse sort stability achieved by initially reversing the list,
  2101. applying a stable forward sort, then reversing the final result. */
  2102. if (reverse) {
  2103. if (keys != NULL)
  2104. reverse_slice(&keys[0], &keys[saved_ob_size]);
  2105. reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
  2106. }
  2107. /* March over the array once, left to right, finding natural runs,
  2108. * and extending short natural runs to minrun elements.
  2109. */
  2110. minrun = merge_compute_minrun(nremaining);
  2111. do {
  2112. int descending;
  2113. Py_ssize_t n;
  2114. /* Identify next run. */
  2115. n = count_run(&ms, lo.keys, lo.keys + nremaining, &descending);
  2116. if (n < 0)
  2117. goto fail;
  2118. if (descending)
  2119. reverse_sortslice(&lo, n);
  2120. /* If short, extend to min(minrun, nremaining). */
  2121. if (n < minrun) {
  2122. const Py_ssize_t force = nremaining <= minrun ?
  2123. nremaining : minrun;
  2124. if (binarysort(&ms, lo, lo.keys + force, lo.keys + n) < 0)
  2125. goto fail;
  2126. n = force;
  2127. }
  2128. /* Push run onto pending-runs stack, and maybe merge. */
  2129. assert(ms.n < MAX_MERGE_PENDING);
  2130. ms.pending[ms.n].base = lo;
  2131. ms.pending[ms.n].len = n;
  2132. ++ms.n;
  2133. if (merge_collapse(&ms) < 0)
  2134. goto fail;
  2135. /* Advance to find next run. */
  2136. sortslice_advance(&lo, n);
  2137. nremaining -= n;
  2138. } while (nremaining);
  2139. if (merge_force_collapse(&ms) < 0)
  2140. goto fail;
  2141. assert(ms.n == 1);
  2142. assert(keys == NULL
  2143. ? ms.pending[0].base.keys == saved_ob_item
  2144. : ms.pending[0].base.keys == &keys[0]);
  2145. assert(ms.pending[0].len == saved_ob_size);
  2146. lo = ms.pending[0].base;
  2147. succeed:
  2148. result = Py_None;
  2149. fail:
  2150. if (keys != NULL) {
  2151. for (i = 0; i < saved_ob_size; i++)
  2152. Py_DECREF(keys[i]);
  2153. if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
  2154. PyMem_FREE(keys);
  2155. }
  2156. if (self->allocated != -1 && result != NULL) {
  2157. /* The user mucked with the list during the sort,
  2158. * and we don't already have another error to report.
  2159. */
  2160. PyErr_SetString(PyExc_ValueError, "list modified during sort");
  2161. result = NULL;
  2162. }
  2163. if (reverse && saved_ob_size > 1)
  2164. reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
  2165. merge_freemem(&ms);
  2166. keyfunc_fail:
  2167. final_ob_item = self->ob_item;
  2168. i = Py_SIZE(self);
  2169. Py_SIZE(self) = saved_ob_size;
  2170. self->ob_item = saved_ob_item;
  2171. self->allocated = saved_allocated;
  2172. if (final_ob_item != NULL) {
  2173. /* we cannot use _list_clear() for this because it does not
  2174. guarantee that the list is really empty when it returns */
  2175. while (--i >= 0) {
  2176. Py_XDECREF(final_ob_item[i]);
  2177. }
  2178. PyMem_FREE(final_ob_item);
  2179. }
  2180. Py_XINCREF(result);
  2181. return result;
  2182. }
  2183. #undef IFLT
  2184. #undef ISLT
  2185. int
  2186. PyList_Sort(PyObject *v)
  2187. {
  2188. if (v == NULL || !PyList_Check(v)) {
  2189. PyErr_BadInternalCall();
  2190. return -1;
  2191. }
  2192. v = list_sort_impl((PyListObject *)v, NULL, 0);
  2193. if (v == NULL)
  2194. return -1;
  2195. Py_DECREF(v);
  2196. return 0;
  2197. }
  2198. /*[clinic input]
  2199. list.reverse
  2200. Reverse *IN PLACE*.
  2201. [clinic start generated code]*/
  2202. static PyObject *
  2203. list_reverse_impl(PyListObject *self)
  2204. /*[clinic end generated code: output=482544fc451abea9 input=eefd4c3ae1bc9887]*/
  2205. {
  2206. if (Py_SIZE(self) > 1)
  2207. reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
  2208. Py_RETURN_NONE;
  2209. }
  2210. int
  2211. PyList_Reverse(PyObject *v)
  2212. {
  2213. PyListObject *self = (PyListObject *)v;
  2214. if (v == NULL || !PyList_Check(v)) {
  2215. PyErr_BadInternalCall();
  2216. return -1;
  2217. }
  2218. if (Py_SIZE(self) > 1)
  2219. reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
  2220. return 0;
  2221. }
  2222. PyObject *
  2223. PyList_AsTuple(PyObject *v)
  2224. {
  2225. PyObject *w;
  2226. PyObject **p, **q;
  2227. Py_ssize_t n;
  2228. if (v == NULL || !PyList_Check(v)) {
  2229. PyErr_BadInternalCall();
  2230. return NULL;
  2231. }
  2232. n = Py_SIZE(v);
  2233. w = PyTuple_New(n);
  2234. if (w == NULL)
  2235. return NULL;
  2236. p = ((PyTupleObject *)w)->ob_item;
  2237. q = ((PyListObject *)v)->ob_item;
  2238. while (--n >= 0) {
  2239. Py_INCREF(*q);
  2240. *p = *q;
  2241. p++;
  2242. q++;
  2243. }
  2244. return w;
  2245. }
  2246. /*[clinic input]
  2247. list.index
  2248. value: object
  2249. start: slice_index(accept={int}) = 0
  2250. stop: slice_index(accept={int}, c_default="PY_SSIZE_T_MAX") = sys.maxsize
  2251. /
  2252. Return first index of value.
  2253. Raises ValueError if the value is not present.
  2254. [clinic start generated code]*/
  2255. static PyObject *
  2256. list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
  2257. Py_ssize_t stop)
  2258. /*[clinic end generated code: output=ec51b88787e4e481 input=40ec5826303a0eb1]*/
  2259. {
  2260. Py_ssize_t i;
  2261. if (start < 0) {
  2262. start += Py_SIZE(self);
  2263. if (start < 0)
  2264. start = 0;
  2265. }
  2266. if (stop < 0) {
  2267. stop += Py_SIZE(self);
  2268. if (stop < 0)
  2269. stop = 0;
  2270. }
  2271. for (i = start; i < stop && i < Py_SIZE(self); i++) {
  2272. int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
  2273. if (cmp > 0)
  2274. return PyLong_FromSsize_t(i);
  2275. else if (cmp < 0)
  2276. return NULL;
  2277. }
  2278. PyErr_Format(PyExc_ValueError, "%R is not in list", value);
  2279. return NULL;
  2280. }
  2281. /*[clinic input]
  2282. list.count
  2283. value: object
  2284. /
  2285. Return number of occurrences of value.
  2286. [clinic start generated code]*/
  2287. static PyObject *
  2288. list_count(PyListObject *self, PyObject *value)
  2289. /*[clinic end generated code: output=b1f5d284205ae714 input=3bdc3a5e6f749565]*/
  2290. {
  2291. Py_ssize_t count = 0;
  2292. Py_ssize_t i;
  2293. for (i = 0; i < Py_SIZE(self); i++) {
  2294. int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
  2295. if (cmp > 0)
  2296. count++;
  2297. else if (cmp < 0)
  2298. return NULL;
  2299. }
  2300. return PyLong_FromSsize_t(count);
  2301. }
  2302. /*[clinic input]
  2303. list.remove
  2304. value: object
  2305. /
  2306. Remove first occurrence of value.
  2307. Raises ValueError if the value is not present.
  2308. [clinic start generated code]*/
  2309. static PyObject *
  2310. list_remove(PyListObject *self, PyObject *value)
  2311. /*[clinic end generated code: output=f087e1951a5e30d1 input=2dc2ba5bb2fb1f82]*/
  2312. {
  2313. Py_ssize_t i;
  2314. for (i = 0; i < Py_SIZE(self); i++) {
  2315. int cmp = PyObject_RichCompareBool(self->ob_item[i], value, Py_EQ);
  2316. if (cmp > 0) {
  2317. if (list_ass_slice(self, i, i+1,
  2318. (PyObject *)NULL) == 0)
  2319. Py_RETURN_NONE;
  2320. return NULL;
  2321. }
  2322. else if (cmp < 0)
  2323. return NULL;
  2324. }
  2325. PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
  2326. return NULL;
  2327. }
  2328. static int
  2329. list_traverse(PyListObject *o, visitproc visit, void *arg)
  2330. {
  2331. Py_ssize_t i;
  2332. for (i = Py_SIZE(o); --i >= 0; )
  2333. Py_VISIT(o->ob_item[i]);
  2334. return 0;
  2335. }
  2336. static PyObject *
  2337. list_richcompare(PyObject *v, PyObject *w, int op)
  2338. {
  2339. PyListObject *vl, *wl;
  2340. Py_ssize_t i;
  2341. if (!PyList_Check(v) || !PyList_Check(w))
  2342. Py_RETURN_NOTIMPLEMENTED;
  2343. vl = (PyListObject *)v;
  2344. wl = (PyListObject *)w;
  2345. if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
  2346. /* Shortcut: if the lengths differ, the lists differ */
  2347. if (op == Py_EQ)
  2348. Py_RETURN_FALSE;
  2349. else
  2350. Py_RETURN_TRUE;
  2351. }
  2352. /* Search for the first index where items are different */
  2353. for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
  2354. int k = PyObject_RichCompareBool(vl->ob_item[i],
  2355. wl->ob_item[i], Py_EQ);
  2356. if (k < 0)
  2357. return NULL;
  2358. if (!k)
  2359. break;
  2360. }
  2361. if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
  2362. /* No more items to compare -- compare sizes */
  2363. Py_RETURN_RICHCOMPARE(Py_SIZE(vl), Py_SIZE(wl), op);
  2364. }
  2365. /* We have an item that differs -- shortcuts for EQ/NE */
  2366. if (op == Py_EQ) {
  2367. Py_RETURN_FALSE;
  2368. }
  2369. if (op == Py_NE) {
  2370. Py_RETURN_TRUE;
  2371. }
  2372. /* Compare the final item again using the proper operator */
  2373. return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
  2374. }
  2375. /*[clinic input]
  2376. list.__init__
  2377. iterable: object(c_default="NULL") = ()
  2378. /
  2379. Built-in mutable sequence.
  2380. If no argument is given, the constructor creates a new empty list.
  2381. The argument must be an iterable if specified.
  2382. [clinic start generated code]*/
  2383. static int
  2384. list___init___impl(PyListObject *self, PyObject *iterable)
  2385. /*[clinic end generated code: output=0f3c21379d01de48 input=b3f3fe7206af8f6b]*/
  2386. {
  2387. /* Verify list invariants established by PyType_GenericAlloc() */
  2388. assert(0 <= Py_SIZE(self));
  2389. assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
  2390. assert(self->ob_item != NULL ||
  2391. self->allocated == 0 || self->allocated == -1);
  2392. /* Empty previous contents */
  2393. if (self->ob_item != NULL) {
  2394. (void)_list_clear(self);
  2395. }
  2396. if (iterable != NULL) {
  2397. if (_PyObject_HasLen(iterable)) {
  2398. Py_ssize_t iter_len = PyObject_Size(iterable);
  2399. if (iter_len == -1) {
  2400. if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
  2401. return -1;
  2402. }
  2403. PyErr_Clear();
  2404. }
  2405. if (iter_len > 0 && self->ob_item == NULL
  2406. && list_preallocate_exact(self, iter_len)) {
  2407. return -1;
  2408. }
  2409. }
  2410. PyObject *rv = list_extend(self, iterable);
  2411. if (rv == NULL)
  2412. return -1;
  2413. Py_DECREF(rv);
  2414. }
  2415. return 0;
  2416. }
  2417. /*[clinic input]
  2418. list.__sizeof__
  2419. Return the size of the list in memory, in bytes.
  2420. [clinic start generated code]*/
  2421. static PyObject *
  2422. list___sizeof___impl(PyListObject *self)
  2423. /*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
  2424. {
  2425. Py_ssize_t res;
  2426. res = _PyObject_SIZE(Py_TYPE(self)) + self->allocated * sizeof(void*);
  2427. return PyLong_FromSsize_t(res);
  2428. }
  2429. static PyObject *list_iter(PyObject *seq);
  2430. static PyObject *list_subscript(PyListObject*, PyObject*);
  2431. static PyMethodDef list_methods[] = {
  2432. {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, "x.__getitem__(y) <==> x[y]"},
  2433. LIST___REVERSED___METHODDEF
  2434. LIST___SIZEOF___METHODDEF
  2435. LIST_CLEAR_METHODDEF
  2436. LIST_COPY_METHODDEF
  2437. LIST_APPEND_METHODDEF
  2438. LIST_INSERT_METHODDEF
  2439. LIST_EXTEND_METHODDEF
  2440. LIST_POP_METHODDEF
  2441. LIST_REMOVE_METHODDEF
  2442. LIST_INDEX_METHODDEF
  2443. LIST_COUNT_METHODDEF
  2444. LIST_REVERSE_METHODDEF
  2445. LIST_SORT_METHODDEF
  2446. {NULL, NULL} /* sentinel */
  2447. };
  2448. static PySequenceMethods list_as_sequence = {
  2449. (lenfunc)list_length, /* sq_length */
  2450. (binaryfunc)list_concat, /* sq_concat */
  2451. (ssizeargfunc)list_repeat, /* sq_repeat */
  2452. (ssizeargfunc)list_item, /* sq_item */
  2453. 0, /* sq_slice */
  2454. (ssizeobjargproc)list_ass_item, /* sq_ass_item */
  2455. 0, /* sq_ass_slice */
  2456. (objobjproc)list_contains, /* sq_contains */
  2457. (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
  2458. (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
  2459. };
  2460. static PyObject *
  2461. list_subscript(PyListObject* self, PyObject* item)
  2462. {
  2463. if (PyIndex_Check(item)) {
  2464. Py_ssize_t i;
  2465. i = PyNumber_AsSsize_t(item, PyExc_IndexError);
  2466. if (i == -1 && PyErr_Occurred())
  2467. return NULL;
  2468. if (i < 0)
  2469. i += PyList_GET_SIZE(self);
  2470. return list_item(self, i);
  2471. }
  2472. else if (PySlice_Check(item)) {
  2473. Py_ssize_t start, stop, step, slicelength, cur, i;
  2474. PyObject* result;
  2475. PyObject* it;
  2476. PyObject **src, **dest;
  2477. if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
  2478. return NULL;
  2479. }
  2480. slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
  2481. step);
  2482. if (slicelength <= 0) {
  2483. return PyList_New(0);
  2484. }
  2485. else if (step == 1) {
  2486. return list_slice(self, start, stop);
  2487. }
  2488. else {
  2489. result = list_new_prealloc(slicelength);
  2490. if (!result) return NULL;
  2491. src = self->ob_item;
  2492. dest = ((PyListObject *)result)->ob_item;
  2493. for (cur = start, i = 0; i < slicelength;
  2494. cur += (size_t)step, i++) {
  2495. it = src[cur];
  2496. Py_INCREF(it);
  2497. dest[i] = it;
  2498. }
  2499. Py_SIZE(result) = slicelength;
  2500. return result;
  2501. }
  2502. }
  2503. else {
  2504. PyErr_Format(PyExc_TypeError,
  2505. "list indices must be integers or slices, not %.200s",
  2506. item->ob_type->tp_name);
  2507. return NULL;
  2508. }
  2509. }
  2510. static int
  2511. list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
  2512. {
  2513. if (PyIndex_Check(item)) {
  2514. Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
  2515. if (i == -1 && PyErr_Occurred())
  2516. return -1;
  2517. if (i < 0)
  2518. i += PyList_GET_SIZE(self);
  2519. return list_ass_item(self, i, value);
  2520. }
  2521. else if (PySlice_Check(item)) {
  2522. Py_ssize_t start, stop, step, slicelength;
  2523. if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
  2524. return -1;
  2525. }
  2526. slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop,
  2527. step);
  2528. if (step == 1)
  2529. return list_ass_slice(self, start, stop, value);
  2530. /* Make sure s[5:2] = [..] inserts at the right place:
  2531. before 5, not before 2. */
  2532. if ((step < 0 && start < stop) ||
  2533. (step > 0 && start > stop))
  2534. stop = start;
  2535. if (value == NULL) {
  2536. /* delete slice */
  2537. PyObject **garbage;
  2538. size_t cur;
  2539. Py_ssize_t i;
  2540. int res;
  2541. if (slicelength <= 0)
  2542. return 0;
  2543. if (step < 0) {
  2544. stop = start + 1;
  2545. start = stop + step*(slicelength - 1) - 1;
  2546. step = -step;
  2547. }
  2548. garbage = (PyObject**)
  2549. PyMem_MALLOC(slicelength*sizeof(PyObject*));
  2550. if (!garbage) {
  2551. PyErr_NoMemory();
  2552. return -1;
  2553. }
  2554. /* drawing pictures might help understand these for
  2555. loops. Basically, we memmove the parts of the
  2556. list that are *not* part of the slice: step-1
  2557. items for each item that is part of the slice,
  2558. and then tail end of the list that was not
  2559. covered by the slice */
  2560. for (cur = start, i = 0;
  2561. cur < (size_t)stop;
  2562. cur += step, i++) {
  2563. Py_ssize_t lim = step - 1;
  2564. garbage[i] = PyList_GET_ITEM(self, cur);
  2565. if (cur + step >= (size_t)Py_SIZE(self)) {
  2566. lim = Py_SIZE(self) - cur - 1;
  2567. }
  2568. memmove(self->ob_item + cur - i,
  2569. self->ob_item + cur + 1,
  2570. lim * sizeof(PyObject *));
  2571. }
  2572. cur = start + (size_t)slicelength * step;
  2573. if (cur < (size_t)Py_SIZE(self)) {
  2574. memmove(self->ob_item + cur - slicelength,
  2575. self->ob_item + cur,
  2576. (Py_SIZE(self) - cur) *
  2577. sizeof(PyObject *));
  2578. }
  2579. Py_SIZE(self) -= slicelength;
  2580. res = list_resize(self, Py_SIZE(self));
  2581. for (i = 0; i < slicelength; i++) {
  2582. Py_DECREF(garbage[i]);
  2583. }
  2584. PyMem_FREE(garbage);
  2585. return res;
  2586. }
  2587. else {
  2588. /* assign slice */
  2589. PyObject *ins, *seq;
  2590. PyObject **garbage, **seqitems, **selfitems;
  2591. Py_ssize_t cur, i;
  2592. /* protect against a[::-1] = a */
  2593. if (self == (PyListObject*)value) {
  2594. seq = list_slice((PyListObject*)value, 0,
  2595. PyList_GET_SIZE(value));
  2596. }
  2597. else {
  2598. seq = PySequence_Fast(value,
  2599. "must assign iterable "
  2600. "to extended slice");
  2601. }
  2602. if (!seq)
  2603. return -1;
  2604. if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
  2605. PyErr_Format(PyExc_ValueError,
  2606. "attempt to assign sequence of "
  2607. "size %zd to extended slice of "
  2608. "size %zd",
  2609. PySequence_Fast_GET_SIZE(seq),
  2610. slicelength);
  2611. Py_DECREF(seq);
  2612. return -1;
  2613. }
  2614. if (!slicelength) {
  2615. Py_DECREF(seq);
  2616. return 0;
  2617. }
  2618. garbage = (PyObject**)
  2619. PyMem_MALLOC(slicelength*sizeof(PyObject*));
  2620. if (!garbage) {
  2621. Py_DECREF(seq);
  2622. PyErr_NoMemory();
  2623. return -1;
  2624. }
  2625. selfitems = self->ob_item;
  2626. seqitems = PySequence_Fast_ITEMS(seq);
  2627. for (cur = start, i = 0; i < slicelength;
  2628. cur += (size_t)step, i++) {
  2629. garbage[i] = selfitems[cur];
  2630. ins = seqitems[i];
  2631. Py_INCREF(ins);
  2632. selfitems[cur] = ins;
  2633. }
  2634. for (i = 0; i < slicelength; i++) {
  2635. Py_DECREF(garbage[i]);
  2636. }
  2637. PyMem_FREE(garbage);
  2638. Py_DECREF(seq);
  2639. return 0;
  2640. }
  2641. }
  2642. else {
  2643. PyErr_Format(PyExc_TypeError,
  2644. "list indices must be integers or slices, not %.200s",
  2645. item->ob_type->tp_name);
  2646. return -1;
  2647. }
  2648. }
  2649. static PyMappingMethods list_as_mapping = {
  2650. (lenfunc)list_length,
  2651. (binaryfunc)list_subscript,
  2652. (objobjargproc)list_ass_subscript
  2653. };
  2654. PyTypeObject PyList_Type = {
  2655. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  2656. "list",
  2657. sizeof(PyListObject),
  2658. 0,
  2659. (destructor)list_dealloc, /* tp_dealloc */
  2660. 0, /* tp_print */
  2661. 0, /* tp_getattr */
  2662. 0, /* tp_setattr */
  2663. 0, /* tp_reserved */
  2664. (reprfunc)list_repr, /* tp_repr */
  2665. 0, /* tp_as_number */
  2666. &list_as_sequence, /* tp_as_sequence */
  2667. &list_as_mapping, /* tp_as_mapping */
  2668. PyObject_HashNotImplemented, /* tp_hash */
  2669. 0, /* tp_call */
  2670. 0, /* tp_str */
  2671. PyObject_GenericGetAttr, /* tp_getattro */
  2672. 0, /* tp_setattro */
  2673. 0, /* tp_as_buffer */
  2674. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  2675. Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
  2676. list___init____doc__, /* tp_doc */
  2677. (traverseproc)list_traverse, /* tp_traverse */
  2678. (inquiry)_list_clear, /* tp_clear */
  2679. list_richcompare, /* tp_richcompare */
  2680. 0, /* tp_weaklistoffset */
  2681. list_iter, /* tp_iter */
  2682. 0, /* tp_iternext */
  2683. list_methods, /* tp_methods */
  2684. 0, /* tp_members */
  2685. 0, /* tp_getset */
  2686. 0, /* tp_base */
  2687. 0, /* tp_dict */
  2688. 0, /* tp_descr_get */
  2689. 0, /* tp_descr_set */
  2690. 0, /* tp_dictoffset */
  2691. (initproc)list___init__, /* tp_init */
  2692. PyType_GenericAlloc, /* tp_alloc */
  2693. PyType_GenericNew, /* tp_new */
  2694. PyObject_GC_Del, /* tp_free */
  2695. };
  2696. /*********************** List Iterator **************************/
  2697. typedef struct {
  2698. PyObject_HEAD
  2699. Py_ssize_t it_index;
  2700. PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
  2701. } listiterobject;
  2702. static void listiter_dealloc(listiterobject *);
  2703. static int listiter_traverse(listiterobject *, visitproc, void *);
  2704. static PyObject *listiter_next(listiterobject *);
  2705. static PyObject *listiter_len(listiterobject *, PyObject *);
  2706. static PyObject *listiter_reduce_general(void *_it, int forward);
  2707. static PyObject *listiter_reduce(listiterobject *, PyObject *);
  2708. static PyObject *listiter_setstate(listiterobject *, PyObject *state);
  2709. PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
  2710. PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
  2711. PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
  2712. static PyMethodDef listiter_methods[] = {
  2713. {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
  2714. {"__reduce__", (PyCFunction)listiter_reduce, METH_NOARGS, reduce_doc},
  2715. {"__setstate__", (PyCFunction)listiter_setstate, METH_O, setstate_doc},
  2716. {NULL, NULL} /* sentinel */
  2717. };
  2718. PyTypeObject PyListIter_Type = {
  2719. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  2720. "list_iterator", /* tp_name */
  2721. sizeof(listiterobject), /* tp_basicsize */
  2722. 0, /* tp_itemsize */
  2723. /* methods */
  2724. (destructor)listiter_dealloc, /* tp_dealloc */
  2725. 0, /* tp_print */
  2726. 0, /* tp_getattr */
  2727. 0, /* tp_setattr */
  2728. 0, /* tp_reserved */
  2729. 0, /* tp_repr */
  2730. 0, /* tp_as_number */
  2731. 0, /* tp_as_sequence */
  2732. 0, /* tp_as_mapping */
  2733. 0, /* tp_hash */
  2734. 0, /* tp_call */
  2735. 0, /* tp_str */
  2736. PyObject_GenericGetAttr, /* tp_getattro */
  2737. 0, /* tp_setattro */
  2738. 0, /* tp_as_buffer */
  2739. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  2740. 0, /* tp_doc */
  2741. (traverseproc)listiter_traverse, /* tp_traverse */
  2742. 0, /* tp_clear */
  2743. 0, /* tp_richcompare */
  2744. 0, /* tp_weaklistoffset */
  2745. PyObject_SelfIter, /* tp_iter */
  2746. (iternextfunc)listiter_next, /* tp_iternext */
  2747. listiter_methods, /* tp_methods */
  2748. 0, /* tp_members */
  2749. };
  2750. static PyObject *
  2751. list_iter(PyObject *seq)
  2752. {
  2753. listiterobject *it;
  2754. if (!PyList_Check(seq)) {
  2755. PyErr_BadInternalCall();
  2756. return NULL;
  2757. }
  2758. it = PyObject_GC_New(listiterobject, &PyListIter_Type);
  2759. if (it == NULL)
  2760. return NULL;
  2761. it->it_index = 0;
  2762. Py_INCREF(seq);
  2763. it->it_seq = (PyListObject *)seq;
  2764. _PyObject_GC_TRACK(it);
  2765. return (PyObject *)it;
  2766. }
  2767. static void
  2768. listiter_dealloc(listiterobject *it)
  2769. {
  2770. _PyObject_GC_UNTRACK(it);
  2771. Py_XDECREF(it->it_seq);
  2772. PyObject_GC_Del(it);
  2773. }
  2774. static int
  2775. listiter_traverse(listiterobject *it, visitproc visit, void *arg)
  2776. {
  2777. Py_VISIT(it->it_seq);
  2778. return 0;
  2779. }
  2780. static PyObject *
  2781. listiter_next(listiterobject *it)
  2782. {
  2783. PyListObject *seq;
  2784. PyObject *item;
  2785. assert(it != NULL);
  2786. seq = it->it_seq;
  2787. if (seq == NULL)
  2788. return NULL;
  2789. assert(PyList_Check(seq));
  2790. if (it->it_index < PyList_GET_SIZE(seq)) {
  2791. item = PyList_GET_ITEM(seq, it->it_index);
  2792. ++it->it_index;
  2793. Py_INCREF(item);
  2794. return item;
  2795. }
  2796. it->it_seq = NULL;
  2797. Py_DECREF(seq);
  2798. return NULL;
  2799. }
  2800. static PyObject *
  2801. listiter_len(listiterobject *it, PyObject *Py_UNUSED(ignored))
  2802. {
  2803. Py_ssize_t len;
  2804. if (it->it_seq) {
  2805. len = PyList_GET_SIZE(it->it_seq) - it->it_index;
  2806. if (len >= 0)
  2807. return PyLong_FromSsize_t(len);
  2808. }
  2809. return PyLong_FromLong(0);
  2810. }
  2811. static PyObject *
  2812. listiter_reduce(listiterobject *it, PyObject *Py_UNUSED(ignored))
  2813. {
  2814. return listiter_reduce_general(it, 1);
  2815. }
  2816. static PyObject *
  2817. listiter_setstate(listiterobject *it, PyObject *state)
  2818. {
  2819. Py_ssize_t index = PyLong_AsSsize_t(state);
  2820. if (index == -1 && PyErr_Occurred())
  2821. return NULL;
  2822. if (it->it_seq != NULL) {
  2823. if (index < 0)
  2824. index = 0;
  2825. else if (index > PyList_GET_SIZE(it->it_seq))
  2826. index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
  2827. it->it_index = index;
  2828. }
  2829. Py_RETURN_NONE;
  2830. }
  2831. /*********************** List Reverse Iterator **************************/
  2832. typedef struct {
  2833. PyObject_HEAD
  2834. Py_ssize_t it_index;
  2835. PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
  2836. } listreviterobject;
  2837. static void listreviter_dealloc(listreviterobject *);
  2838. static int listreviter_traverse(listreviterobject *, visitproc, void *);
  2839. static PyObject *listreviter_next(listreviterobject *);
  2840. static PyObject *listreviter_len(listreviterobject *, PyObject *);
  2841. static PyObject *listreviter_reduce(listreviterobject *, PyObject *);
  2842. static PyObject *listreviter_setstate(listreviterobject *, PyObject *);
  2843. static PyMethodDef listreviter_methods[] = {
  2844. {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
  2845. {"__reduce__", (PyCFunction)listreviter_reduce, METH_NOARGS, reduce_doc},
  2846. {"__setstate__", (PyCFunction)listreviter_setstate, METH_O, setstate_doc},
  2847. {NULL, NULL} /* sentinel */
  2848. };
  2849. PyTypeObject PyListRevIter_Type = {
  2850. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  2851. "list_reverseiterator", /* tp_name */
  2852. sizeof(listreviterobject), /* tp_basicsize */
  2853. 0, /* tp_itemsize */
  2854. /* methods */
  2855. (destructor)listreviter_dealloc, /* tp_dealloc */
  2856. 0, /* tp_print */
  2857. 0, /* tp_getattr */
  2858. 0, /* tp_setattr */
  2859. 0, /* tp_reserved */
  2860. 0, /* tp_repr */
  2861. 0, /* tp_as_number */
  2862. 0, /* tp_as_sequence */
  2863. 0, /* tp_as_mapping */
  2864. 0, /* tp_hash */
  2865. 0, /* tp_call */
  2866. 0, /* tp_str */
  2867. PyObject_GenericGetAttr, /* tp_getattro */
  2868. 0, /* tp_setattro */
  2869. 0, /* tp_as_buffer */
  2870. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  2871. 0, /* tp_doc */
  2872. (traverseproc)listreviter_traverse, /* tp_traverse */
  2873. 0, /* tp_clear */
  2874. 0, /* tp_richcompare */
  2875. 0, /* tp_weaklistoffset */
  2876. PyObject_SelfIter, /* tp_iter */
  2877. (iternextfunc)listreviter_next, /* tp_iternext */
  2878. listreviter_methods, /* tp_methods */
  2879. 0,
  2880. };
  2881. /*[clinic input]
  2882. list.__reversed__
  2883. Return a reverse iterator over the list.
  2884. [clinic start generated code]*/
  2885. static PyObject *
  2886. list___reversed___impl(PyListObject *self)
  2887. /*[clinic end generated code: output=b166f073208c888c input=eadb6e17f8a6a280]*/
  2888. {
  2889. listreviterobject *it;
  2890. it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
  2891. if (it == NULL)
  2892. return NULL;
  2893. assert(PyList_Check(self));
  2894. it->it_index = PyList_GET_SIZE(self) - 1;
  2895. Py_INCREF(self);
  2896. it->it_seq = self;
  2897. PyObject_GC_Track(it);
  2898. return (PyObject *)it;
  2899. }
  2900. static void
  2901. listreviter_dealloc(listreviterobject *it)
  2902. {
  2903. PyObject_GC_UnTrack(it);
  2904. Py_XDECREF(it->it_seq);
  2905. PyObject_GC_Del(it);
  2906. }
  2907. static int
  2908. listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
  2909. {
  2910. Py_VISIT(it->it_seq);
  2911. return 0;
  2912. }
  2913. static PyObject *
  2914. listreviter_next(listreviterobject *it)
  2915. {
  2916. PyObject *item;
  2917. Py_ssize_t index;
  2918. PyListObject *seq;
  2919. assert(it != NULL);
  2920. seq = it->it_seq;
  2921. if (seq == NULL) {
  2922. return NULL;
  2923. }
  2924. assert(PyList_Check(seq));
  2925. index = it->it_index;
  2926. if (index>=0 && index < PyList_GET_SIZE(seq)) {
  2927. item = PyList_GET_ITEM(seq, index);
  2928. it->it_index--;
  2929. Py_INCREF(item);
  2930. return item;
  2931. }
  2932. it->it_index = -1;
  2933. it->it_seq = NULL;
  2934. Py_DECREF(seq);
  2935. return NULL;
  2936. }
  2937. static PyObject *
  2938. listreviter_len(listreviterobject *it, PyObject *Py_UNUSED(ignored))
  2939. {
  2940. Py_ssize_t len = it->it_index + 1;
  2941. if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
  2942. len = 0;
  2943. return PyLong_FromSsize_t(len);
  2944. }
  2945. static PyObject *
  2946. listreviter_reduce(listreviterobject *it, PyObject *Py_UNUSED(ignored))
  2947. {
  2948. return listiter_reduce_general(it, 0);
  2949. }
  2950. static PyObject *
  2951. listreviter_setstate(listreviterobject *it, PyObject *state)
  2952. {
  2953. Py_ssize_t index = PyLong_AsSsize_t(state);
  2954. if (index == -1 && PyErr_Occurred())
  2955. return NULL;
  2956. if (it->it_seq != NULL) {
  2957. if (index < -1)
  2958. index = -1;
  2959. else if (index > PyList_GET_SIZE(it->it_seq) - 1)
  2960. index = PyList_GET_SIZE(it->it_seq) - 1;
  2961. it->it_index = index;
  2962. }
  2963. Py_RETURN_NONE;
  2964. }
  2965. /* common pickling support */
  2966. static PyObject *
  2967. listiter_reduce_general(void *_it, int forward)
  2968. {
  2969. _Py_IDENTIFIER(iter);
  2970. _Py_IDENTIFIER(reversed);
  2971. PyObject *list;
  2972. /* the objects are not the same, index is of different types! */
  2973. if (forward) {
  2974. listiterobject *it = (listiterobject *)_it;
  2975. if (it->it_seq)
  2976. return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
  2977. it->it_seq, it->it_index);
  2978. } else {
  2979. listreviterobject *it = (listreviterobject *)_it;
  2980. if (it->it_seq)
  2981. return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed),
  2982. it->it_seq, it->it_index);
  2983. }
  2984. /* empty iterator, create an empty list */
  2985. list = PyList_New(0);
  2986. if (list == NULL)
  2987. return NULL;
  2988. return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
  2989. }