Merged revisions 51434-53004 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r51434 | neal.norwitz | 2006-08-21 20:20:10 +0200 (Mon, 21 Aug 2006) | 1 line Fix a couple of ssize-t issues reported by Alexander Belopolsky on python-dev ........ r51439 | neal.norwitz | 2006-08-21 21:47:08 +0200 (Mon, 21 Aug 2006) | 6 lines Patch #1542451: disallow continue anywhere under a finally I'm undecided if this should be backported to 2.5 or 2.5.1. Armin suggested to wait (I'm of the same opinion). Thomas W thinks it's fine to go in 2.5. ........ r51443 | neal.norwitz | 2006-08-21 22:16:24 +0200 (Mon, 21 Aug 2006) | 4 lines Handle a few more error conditions. Klocwork 301 and 302. Will backport. ........ r51450 | neal.norwitz | 2006-08-22 00:21:19 +0200 (Tue, 22 Aug 2006) | 5 lines Patch #1541585: fix buffer overrun when performing repr() on a unicode string in a build with wide unicode (UCS-4) support. This code could be improved, so add an XXX comment. ........ r51456 | neal.norwitz | 2006-08-22 01:44:48 +0200 (Tue, 22 Aug 2006) | 1 line Try to get the windows bots working again with the new peephole.c ........ r51461 | anthony.baxter | 2006-08-22 09:36:59 +0200 (Tue, 22 Aug 2006) | 1 line patch for documentation for recent uuid changes (from ping) ........ r51473 | neal.norwitz | 2006-08-22 15:56:56 +0200 (Tue, 22 Aug 2006) | 1 line Alexander Belopolsky pointed out that pos is a size_t ........ r51489 | jeremy.hylton | 2006-08-22 22:46:00 +0200 (Tue, 22 Aug 2006) | 2 lines Expose column offset information in parse trees. ........ r51497 | andrew.kuchling | 2006-08-23 01:13:43 +0200 (Wed, 23 Aug 2006) | 1 line Move functional howto into trunk ........ r51515 | jeremy.hylton | 2006-08-23 20:37:43 +0200 (Wed, 23 Aug 2006) | 2 lines Baby steps towards better tests for tokenize ........ r51525 | alex.martelli | 2006-08-23 22:42:02 +0200 (Wed, 23 Aug 2006) | 6 lines x**2 should about equal x*x (including for a float x such that the result is inf) but didn't; added a test to test_float to verify that, and ignored the ERANGE value for errno in the pow operation to make the new test pass (with help from Marilyn Davis at the Google Python Sprint -- thanks!). ........ r51526 | jeremy.hylton | 2006-08-23 23:14:03 +0200 (Wed, 23 Aug 2006) | 20 lines Bug fixes large and small for tokenize. Small: Always generate a NL or NEWLINE token following a COMMENT token. The old code did not generate an NL token if the comment was on a line by itself. Large: The output of untokenize() will now match the input exactly if it is passed the full token sequence. The old, crufty output is still generated if a limited input sequence is provided, where limited means that it does not include position information for tokens. Remaining bug: There is no CONTINUATION token (\) so there is no way for untokenize() to handle such code. Also, expanded the number of doctests in hopes of eventually removing the old-style tests that compare against a golden file. Bug fix candidate for Python 2.5.1. (Sigh.) ........ r51527 | jeremy.hylton | 2006-08-23 23:26:46 +0200 (Wed, 23 Aug 2006) | 5 lines Replace dead code with an assert. Now that COMMENT tokens are reliably followed by NL or NEWLINE, there is never a need to add extra newlines in untokenize. ........ r51530 | alex.martelli | 2006-08-24 00:17:59 +0200 (Thu, 24 Aug 2006) | 7 lines Reverting the patch that tried to fix the issue whereby x**2 raises OverflowError while x*x succeeds and produces infinity; apparently these inconsistencies cannot be fixed across ``all'' platforms and there's a widespread feeling that therefore ``every'' platform should keep suffering forevermore. Ah well. ........ r51565 | thomas.wouters | 2006-08-24 20:40:20 +0200 (Thu, 24 Aug 2006) | 6 lines Fix SF bug #1545837: array.array borks on deepcopy. array.__deepcopy__() needs to take an argument, even if it doesn't actually use it. Will backport to 2.5 and 2.4 (if applicable.) ........ r51580 | martin.v.loewis | 2006-08-25 02:03:34 +0200 (Fri, 25 Aug 2006) | 3 lines Patch #1545507: Exclude ctypes package in Win64 MSI file. Will backport to 2.5. ........ r51589 | neal.norwitz | 2006-08-25 03:52:49 +0200 (Fri, 25 Aug 2006) | 1 line importing types is not necessary if we use isinstance ........ r51604 | thomas.heller | 2006-08-25 09:27:33 +0200 (Fri, 25 Aug 2006) | 3 lines Port _ctypes.pyd to win64 on AMD64. ........ r51605 | thomas.heller | 2006-08-25 09:34:51 +0200 (Fri, 25 Aug 2006) | 3 lines Add missing file for _ctypes.pyd port to win64 on AMD64. ........ r51606 | thomas.heller | 2006-08-25 11:26:33 +0200 (Fri, 25 Aug 2006) | 6 lines Build _ctypes.pyd for win AMD64 into the MSVC project file. Since MSVC doesn't know about .asm files, a helper batch file is needed to find ml64.exe in predefined locations. The helper script hardcodes the path to the MS Platform SDK. ........ r51608 | armin.rigo | 2006-08-25 14:44:28 +0200 (Fri, 25 Aug 2006) | 4 lines The regular expression engine in '_sre' can segfault when interpreting bogus bytecode. It is unclear whether this is a real bug or a "won't fix" case like bogus_code_obj.py. ........ r51617 | tim.peters | 2006-08-26 00:05:39 +0200 (Sat, 26 Aug 2006) | 2 lines Whitespace normalization. ........ r51618 | tim.peters | 2006-08-26 00:06:44 +0200 (Sat, 26 Aug 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r51619 | tim.peters | 2006-08-26 00:26:21 +0200 (Sat, 26 Aug 2006) | 3 lines A new test here relied on preserving invisible trailing whitespace in expected output. Stop that. ........ r51624 | jack.diederich | 2006-08-26 20:42:06 +0200 (Sat, 26 Aug 2006) | 4 lines - Move functions common to all path modules into genericpath.py and have the OS speicifc path modules import them. - Have os2emxpath import common functions fron ntpath instead of using copies ........ r51642 | neal.norwitz | 2006-08-29 07:40:58 +0200 (Tue, 29 Aug 2006) | 1 line Fix a couple of typos. ........ r51647 | marc-andre.lemburg | 2006-08-29 12:34:12 +0200 (Tue, 29 Aug 2006) | 5 lines Fix a buglet in the error reporting (SF bug report #1546372). This should probably go into Python 2.5 or 2.5.1 as well. ........ r51663 | armin.rigo | 2006-08-31 10:51:06 +0200 (Thu, 31 Aug 2006) | 3 lines Doc fix: hashlib objects don't always return a digest of 16 bytes. Backport candidate for 2.5. ........ r51664 | nick.coghlan | 2006-08-31 14:00:43 +0200 (Thu, 31 Aug 2006) | 1 line Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5) ........ r51665 | nick.coghlan | 2006-08-31 14:51:25 +0200 (Thu, 31 Aug 2006) | 1 line Remove the old decimal context management tests from test_contextlib (guess who didn't run the test suite before committing...) ........ r51669 | brett.cannon | 2006-08-31 20:54:26 +0200 (Thu, 31 Aug 2006) | 4 lines Make sure memory is properly cleaned up in file_init. Backport candidate. ........ r51671 | brett.cannon | 2006-08-31 23:47:52 +0200 (Thu, 31 Aug 2006) | 2 lines Fix comment about indentation level in C files. ........ r51674 | brett.cannon | 2006-09-01 00:42:37 +0200 (Fri, 01 Sep 2006) | 3 lines Have pre-existing C files use 8 spaces indents (to match old PEP 7 style), but have all new files use 4 spaces (to match current PEP 7 style). ........ r51676 | fred.drake | 2006-09-01 05:57:19 +0200 (Fri, 01 Sep 2006) | 3 lines - SF patch #1550263: Enhance and correct unittest docs - various minor cleanups for improved consistency ........ r51677 | georg.brandl | 2006-09-02 00:30:52 +0200 (Sat, 02 Sep 2006) | 2 lines evalfile() should be execfile(). ........ r51681 | neal.norwitz | 2006-09-02 04:43:17 +0200 (Sat, 02 Sep 2006) | 1 line SF #1547931, fix typo (missing and). Will backport to 2.5 ........ r51683 | neal.norwitz | 2006-09-02 04:50:35 +0200 (Sat, 02 Sep 2006) | 1 line Bug #1548092: fix curses.tparm seg fault on invalid input. Needs backport to 2.5.1 and earlier. ........ r51684 | neal.norwitz | 2006-09-02 04:58:13 +0200 (Sat, 02 Sep 2006) | 4 lines Bug #1550714: fix SystemError from itertools.tee on negative value for n. Needs backport to 2.5.1 and earlier. ........ r51685 | nick.coghlan | 2006-09-02 05:54:17 +0200 (Sat, 02 Sep 2006) | 1 line Make decimal.ContextManager a private implementation detail of decimal.localcontext() ........ r51686 | nick.coghlan | 2006-09-02 06:04:18 +0200 (Sat, 02 Sep 2006) | 1 line Further corrections to the decimal module context management documentation ........ r51688 | raymond.hettinger | 2006-09-02 19:07:23 +0200 (Sat, 02 Sep 2006) | 1 line Fix documentation nits for decimal context managers. ........ r51690 | neal.norwitz | 2006-09-02 20:51:34 +0200 (Sat, 02 Sep 2006) | 1 line Add missing word in comment ........ r51691 | neal.norwitz | 2006-09-02 21:40:19 +0200 (Sat, 02 Sep 2006) | 7 lines Hmm, this test has failed at least twice recently on the OpenBSD and Debian sparc buildbots. Since this goes through a lot of tests and hits the disk a lot it could be slow (especially if NFS is involved). I'm not sure if that's the problem, but printing periodic msgs shouldn't hurt. The code was stolen from test_compiler. ........ r51693 | nick.coghlan | 2006-09-03 03:02:00 +0200 (Sun, 03 Sep 2006) | 1 line Fix final documentation nits before backporting decimal module fixes to 2.5 ........ r51694 | nick.coghlan | 2006-09-03 03:06:07 +0200 (Sun, 03 Sep 2006) | 1 line Typo fix for decimal docs ........ r51697 | nick.coghlan | 2006-09-03 03:20:46 +0200 (Sun, 03 Sep 2006) | 1 line NEWS entry on trunk for decimal module changes ........ r51704 | raymond.hettinger | 2006-09-04 17:32:48 +0200 (Mon, 04 Sep 2006) | 1 line Fix endcase for str.rpartition() ........ r51716 | tim.peters | 2006-09-05 04:18:09 +0200 (Tue, 05 Sep 2006) | 12 lines "Conceptual" merge of rev 51711 from the 2.5 branch. i_divmod(): As discussed on Python-Dev, changed the overflow checking to live happily with recent gcc optimizations that assume signed integer arithmetic never overflows. This differs from the corresponding change on the 2.5 and 2.4 branches, using a less obscure approach, but one that /may/ tickle platform idiocies in their definitions of LONG_MIN. The 2.4 + 2.5 change avoided introducing a dependence on LONG_MIN, at the cost of substantially goofier code. ........ r51717 | tim.peters | 2006-09-05 04:21:19 +0200 (Tue, 05 Sep 2006) | 2 lines Whitespace normalization. ........ r51719 | tim.peters | 2006-09-05 04:22:17 +0200 (Tue, 05 Sep 2006) | 2 lines Add missing svn:eol-style property to text files. ........ r51720 | neal.norwitz | 2006-09-05 04:24:03 +0200 (Tue, 05 Sep 2006) | 2 lines Fix SF bug #1546288, crash in dict_equal. ........ r51721 | neal.norwitz | 2006-09-05 04:25:41 +0200 (Tue, 05 Sep 2006) | 1 line Fix SF #1552093, eval docstring typo (3 ps in mapping) ........ r51724 | neal.norwitz | 2006-09-05 04:35:08 +0200 (Tue, 05 Sep 2006) | 1 line This was found by Guido AFAIK on p3yk (sic) branch. ........ r51725 | neal.norwitz | 2006-09-05 04:36:20 +0200 (Tue, 05 Sep 2006) | 1 line Add a NEWS entry for str.rpartition() change ........ r51728 | neal.norwitz | 2006-09-05 04:57:01 +0200 (Tue, 05 Sep 2006) | 1 line Patch #1540470, for OpenBSD 4.0. Backport candidate for 2.[34]. ........ r51729 | neal.norwitz | 2006-09-05 05:53:08 +0200 (Tue, 05 Sep 2006) | 12 lines Bug #1520864 (again): unpacking singleton tuples in list comprehensions and generator expressions (x for x, in ... ) works again. Sigh, I only fixed for loops the first time, not list comps and genexprs too. I couldn't find any more unpacking cases where there is a similar bug lurking. This code should be refactored to eliminate the duplication. I'm sure the listcomp/genexpr code can be refactored. I'm not sure if the for loop can re-use any of the same code though. Will backport to 2.5 (the only place it matters). ........ r51731 | neal.norwitz | 2006-09-05 05:58:26 +0200 (Tue, 05 Sep 2006) | 1 line Add a comment about some refactoring. (There's probably more that should be done.) I will reformat this file in the next checkin due to the inconsistent tabs/spaces. ........ r51732 | neal.norwitz | 2006-09-05 06:00:12 +0200 (Tue, 05 Sep 2006) | 1 line M-x untabify ........ r51737 | hyeshik.chang | 2006-09-05 14:07:09 +0200 (Tue, 05 Sep 2006) | 7 lines Fix a few bugs on cjkcodecs found by Oren Tirosh: - gbk and gb18030 codec now handle U+30FB KATAKANA MIDDLE DOT correctly. - iso2022_jp_2 codec now encodes into G0 for KS X 1001, GB2312 codepoints to conform the standard. - iso2022_jp_3 and iso2022_jp_2004 codec can encode JIS X 2013:2 codepoints now. ........ r51738 | hyeshik.chang | 2006-09-05 14:14:57 +0200 (Tue, 05 Sep 2006) | 2 lines Fix a typo: 2013 -> 0213 ........ r51740 | georg.brandl | 2006-09-05 14:44:58 +0200 (Tue, 05 Sep 2006) | 3 lines Bug #1552618: change docs of dict.has_key() to reflect recommendation to use "in". ........ r51742 | andrew.kuchling | 2006-09-05 15:02:40 +0200 (Tue, 05 Sep 2006) | 1 line Rearrange example a bit, and show rpartition() when separator is not found ........ r51744 | andrew.kuchling | 2006-09-05 15:15:41 +0200 (Tue, 05 Sep 2006) | 1 line [Bug #1525469] SimpleXMLRPCServer still uses the sys.exc_{value,type} module-level globals instead of calling sys.exc_info(). Reported by Russell Warren ........ r51745 | andrew.kuchling | 2006-09-05 15:19:18 +0200 (Tue, 05 Sep 2006) | 3 lines [Bug #1526834] Fix crash in pdb when you do 'b f('; the function name was placed into a regex pattern and the unbalanced paren caused re.compile() to report an error ........ r51751 | kristjan.jonsson | 2006-09-05 19:58:12 +0200 (Tue, 05 Sep 2006) | 6 lines Update the PCBuild8 solution. Facilitate cross-compilation by having binaries in separate Win32 and x64 directories. Rationalized configs by making proper use of platforms/configurations. Remove pythoncore_pgo project. Add new PGIRelease and PGORelease configurations to perform Profile Guided Optimisation. Removed I64 support, but this can be easily added by copying the x64 platform settings. ........ r51758 | gustavo.niemeyer | 2006-09-06 03:58:52 +0200 (Wed, 06 Sep 2006) | 3 lines Fixing #1531862: Do not close standard file descriptors in the subprocess module. ........ r51760 | neal.norwitz | 2006-09-06 05:58:34 +0200 (Wed, 06 Sep 2006) | 1 line Revert 51758 because it broke all the buildbots ........ r51762 | georg.brandl | 2006-09-06 08:03:59 +0200 (Wed, 06 Sep 2006) | 3 lines Bug #1551427: fix a wrong NULL pointer check in the win32 version of os.urandom(). ........ r51765 | georg.brandl | 2006-09-06 08:09:31 +0200 (Wed, 06 Sep 2006) | 3 lines Bug #1550983: emit better error messages for erroneous relative imports (if not in package and if beyond toplevel package). ........ r51767 | neal.norwitz | 2006-09-06 08:28:06 +0200 (Wed, 06 Sep 2006) | 1 line with and as are now keywords. There are some generated files I can't recreate. ........ r51770 | georg.brandl | 2006-09-06 08:50:05 +0200 (Wed, 06 Sep 2006) | 5 lines Bug #1542051: Exceptions now correctly call PyObject_GC_UnTrack. Also make sure that every exception class has __module__ set to 'exceptions'. ........ r51785 | georg.brandl | 2006-09-06 22:05:58 +0200 (Wed, 06 Sep 2006) | 2 lines Fix missing import of the types module in logging.config. ........ r51789 | marc-andre.lemburg | 2006-09-06 22:40:22 +0200 (Wed, 06 Sep 2006) | 3 lines Add news item for bug fix of SF bug report #1546372. ........ r51797 | gustavo.niemeyer | 2006-09-07 02:48:33 +0200 (Thu, 07 Sep 2006) | 3 lines Fixed subprocess bug #1531862 again, after removing tests offending buildbot ........ r51798 | raymond.hettinger | 2006-09-07 04:42:48 +0200 (Thu, 07 Sep 2006) | 1 line Fix refcounts and add error checks. ........ r51803 | nick.coghlan | 2006-09-07 12:50:34 +0200 (Thu, 07 Sep 2006) | 1 line Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314 ........ r51805 | ronald.oussoren | 2006-09-07 14:03:10 +0200 (Thu, 07 Sep 2006) | 2 lines Fix a glaring error and update some version numbers. ........ r51814 | andrew.kuchling | 2006-09-07 15:56:23 +0200 (Thu, 07 Sep 2006) | 1 line Typo fix ........ r51815 | andrew.kuchling | 2006-09-07 15:59:38 +0200 (Thu, 07 Sep 2006) | 8 lines [Bug #1552726] Avoid repeatedly polling in interactive mode -- only put a timeout on the select() if an input hook has been defined. Patch by Richard Boulton. This select() code is only executed with readline 2.1, or if READLINE_CALLBACKS is defined. Backport candidate for 2.5, 2.4, probably earlier versions too. ........ r51816 | armin.rigo | 2006-09-07 17:06:00 +0200 (Thu, 07 Sep 2006) | 2 lines Add a warning notice on top of the generated grammar.txt. ........ r51819 | thomas.heller | 2006-09-07 20:56:28 +0200 (Thu, 07 Sep 2006) | 5 lines Anonymous structure fields that have a bit-width specified did not work, and they gave a strange error message from PyArg_ParseTuple: function takes exactly 2 arguments (3 given). With tests. ........ r51820 | thomas.heller | 2006-09-07 21:09:54 +0200 (Thu, 07 Sep 2006) | 4 lines The cast function did not accept c_char_p or c_wchar_p instances as first argument, and failed with a 'bad argument to internal function' error message. ........ r51827 | nick.coghlan | 2006-09-08 12:04:38 +0200 (Fri, 08 Sep 2006) | 1 line Add missing NEWS entry for rev 51803 ........ r51828 | andrew.kuchling | 2006-09-08 15:25:23 +0200 (Fri, 08 Sep 2006) | 1 line Add missing word ........ r51829 | andrew.kuchling | 2006-09-08 15:35:49 +0200 (Fri, 08 Sep 2006) | 1 line Explain SQLite a bit more clearly ........ r51830 | andrew.kuchling | 2006-09-08 15:36:36 +0200 (Fri, 08 Sep 2006) | 1 line Explain SQLite a bit more clearly ........ r51832 | andrew.kuchling | 2006-09-08 16:02:45 +0200 (Fri, 08 Sep 2006) | 1 line Use native SQLite types ........ r51833 | andrew.kuchling | 2006-09-08 16:03:01 +0200 (Fri, 08 Sep 2006) | 1 line Use native SQLite types ........ r51835 | andrew.kuchling | 2006-09-08 16:05:10 +0200 (Fri, 08 Sep 2006) | 1 line Fix typo in example ........ r51837 | brett.cannon | 2006-09-09 09:11:46 +0200 (Sat, 09 Sep 2006) | 6 lines Remove the __unicode__ method from exceptions. Allows unicode() to be called on exception classes. Would require introducing a tp_unicode slot to make it work otherwise. Fixes bug #1551432 and will be backported. ........ r51854 | neal.norwitz | 2006-09-11 06:24:09 +0200 (Mon, 11 Sep 2006) | 8 lines Forward port of 51850 from release25-maint branch. As mentioned on python-dev, reverting patch #1504333 because it introduced an infinite loop in rev 47154. This patch also adds a test to prevent the regression. ........ r51855 | neal.norwitz | 2006-09-11 06:28:16 +0200 (Mon, 11 Sep 2006) | 5 lines Properly handle a NULL returned from PyArena_New(). (Also fix some whitespace) Klocwork #364. ........ r51856 | neal.norwitz | 2006-09-11 06:32:57 +0200 (Mon, 11 Sep 2006) | 1 line Add a "crasher" taken from the sgml bug report referenced in the comment ........ r51858 | georg.brandl | 2006-09-11 11:38:35 +0200 (Mon, 11 Sep 2006) | 12 lines Forward-port of rev. 51857: Building with HP's cc on HP-UX turned up a couple of problems. _PyGILState_NoteThreadState was declared as static inconsistently. Make it static as it's not necessary outside of this module. Some tests failed because errno was reset to 0. (I think the tests that failed were at least: test_fcntl and test_mailbox). Ensure that errno doesn't change after a call to Py_END_ALLOW_THREADS. This only affected debug builds. ........ r51865 | martin.v.loewis | 2006-09-12 21:49:20 +0200 (Tue, 12 Sep 2006) | 2 lines Forward-port 51862: Add sgml_input.html. ........ r51866 | andrew.kuchling | 2006-09-12 22:50:23 +0200 (Tue, 12 Sep 2006) | 1 line Markup typo fix ........ r51867 | andrew.kuchling | 2006-09-12 23:09:02 +0200 (Tue, 12 Sep 2006) | 1 line Some editing, markup fixes ........ r51868 | andrew.kuchling | 2006-09-12 23:21:51 +0200 (Tue, 12 Sep 2006) | 1 line More wordsmithing ........ r51877 | andrew.kuchling | 2006-09-14 13:22:18 +0200 (Thu, 14 Sep 2006) | 1 line Make --help mention that -v can be supplied multiple times ........ r51878 | andrew.kuchling | 2006-09-14 13:28:50 +0200 (Thu, 14 Sep 2006) | 1 line Rewrite help message to remove some of the parentheticals. (There were a lot of them.) ........ r51883 | ka-ping.yee | 2006-09-15 02:34:19 +0200 (Fri, 15 Sep 2006) | 2 lines Fix grammar errors and improve clarity. ........ r51885 | georg.brandl | 2006-09-15 07:22:24 +0200 (Fri, 15 Sep 2006) | 3 lines Correct elementtree module index entry. ........ r51889 | fred.drake | 2006-09-15 17:18:04 +0200 (Fri, 15 Sep 2006) | 4 lines - fix module name in links in formatted documentation - minor markup cleanup (forward-ported from release25-maint revision 51888) ........ r51891 | fred.drake | 2006-09-15 18:11:27 +0200 (Fri, 15 Sep 2006) | 3 lines revise explanation of returns_unicode to reflect bool values and to include the default value (merged from release25-maint revision 51890) ........ r51897 | martin.v.loewis | 2006-09-16 19:36:37 +0200 (Sat, 16 Sep 2006) | 2 lines Patch #1557515: Add RLIMIT_SBSIZE. ........ r51903 | ronald.oussoren | 2006-09-17 20:42:53 +0200 (Sun, 17 Sep 2006) | 2 lines Port of revision 51902 in release25-maint to the trunk ........ r51904 | ronald.oussoren | 2006-09-17 21:23:27 +0200 (Sun, 17 Sep 2006) | 3 lines Tweak Mac/Makefile in to ensure that pythonw gets rebuild when the major version of python changes (2.5 -> 2.6). Bug #1552935. ........ r51913 | guido.van.rossum | 2006-09-18 23:36:16 +0200 (Mon, 18 Sep 2006) | 2 lines Make this thing executable. ........ r51920 | gregory.p.smith | 2006-09-19 19:35:04 +0200 (Tue, 19 Sep 2006) | 5 lines Fixes a bug with bsddb.DB.stat where the flags and txn keyword arguments are transposed. (reported by Louis Zechtzer) ..already committed to release24-maint ..needs committing to release25-maint ........ r51926 | brett.cannon | 2006-09-20 20:34:28 +0200 (Wed, 20 Sep 2006) | 3 lines Accidentally didn't commit Misc/NEWS entry on when __unicode__() was removed from exceptions. ........ r51927 | brett.cannon | 2006-09-20 20:43:13 +0200 (Wed, 20 Sep 2006) | 6 lines Allow exceptions to be directly sliced again (e.g., ``BaseException(1,2,3)[0:2]``). Discovered in Python 2.5.0 by Thomas Heller and reported to python-dev. This should be backported to 2.5 . ........ r51928 | brett.cannon | 2006-09-20 21:28:35 +0200 (Wed, 20 Sep 2006) | 2 lines Make python.vim output more deterministic. ........ r51949 | walter.doerwald | 2006-09-21 17:09:55 +0200 (Thu, 21 Sep 2006) | 2 lines Fix typo. ........ r51950 | jack.diederich | 2006-09-21 19:50:26 +0200 (Thu, 21 Sep 2006) | 5 lines * regression bug, count_next was coercing a Py_ssize_t to an unsigned Py_size_t which breaks negative counts * added test for negative numbers will backport to 2.5.1 ........ r51953 | jack.diederich | 2006-09-21 22:34:49 +0200 (Thu, 21 Sep 2006) | 1 line added itertools.count(-n) fix ........ r51971 | neal.norwitz | 2006-09-22 10:16:26 +0200 (Fri, 22 Sep 2006) | 10 lines Fix %zd string formatting on Mac OS X so it prints negative numbers. In addition to testing positive numbers, verify negative numbers work in configure. In order to avoid compiler warnings on OS X 10.4, also change the order of the check for the format character to use (PY_FORMAT_SIZE_T) in the sprintf format for Py_ssize_t. This patch changes PY_FORMAT_SIZE_T from "" to "l" if it wasn't defined at configure time. Need to verify the buildbot results. Backport candidate (if everyone thinks this patch can't be improved). ........ r51972 | neal.norwitz | 2006-09-22 10:18:10 +0200 (Fri, 22 Sep 2006) | 7 lines Bug #1557232: fix seg fault with def f((((x)))) and def f(((x),)). These tests should be improved. Hopefully this fixes variations when flipping back and forth between fpdef and fplist. Backport candidate. ........ r51975 | neal.norwitz | 2006-09-22 10:47:23 +0200 (Fri, 22 Sep 2006) | 4 lines Mostly revert this file to the same version as before. Only force setting of PY_FORMAT_SIZE_T to "l" for Mac OSX. I don't know a better define to use. This should get rid of the warnings on other platforms and Mac too. ........ r51986 | fred.drake | 2006-09-23 02:26:31 +0200 (Sat, 23 Sep 2006) | 1 line add boilerplate "What's New" document so the docs will build ........ r51987 | neal.norwitz | 2006-09-23 06:11:38 +0200 (Sat, 23 Sep 2006) | 1 line Remove extra semi-colons reported by Johnny Lee on python-dev. Backport if anyone cares. ........ r51989 | neal.norwitz | 2006-09-23 20:11:58 +0200 (Sat, 23 Sep 2006) | 1 line SF Bug #1563963, add missing word and cleanup first sentance ........ r51990 | brett.cannon | 2006-09-23 21:53:20 +0200 (Sat, 23 Sep 2006) | 3 lines Make output on test_strptime() be more verbose in face of failure. This is in hopes that more information will help debug the failing test on HPPA Ubuntu. ........ r51991 | georg.brandl | 2006-09-24 12:36:01 +0200 (Sun, 24 Sep 2006) | 2 lines Fix webbrowser.BackgroundBrowser on Windows. ........ r51993 | georg.brandl | 2006-09-24 14:35:36 +0200 (Sun, 24 Sep 2006) | 4 lines Fix a bug in the parser's future statement handling that led to "with" not being recognized as a keyword after, e.g., this statement: from __future__ import division, with_statement ........ r51995 | georg.brandl | 2006-09-24 14:50:24 +0200 (Sun, 24 Sep 2006) | 4 lines Fix a bug in traceback.format_exception_only() that led to an error being raised when print_exc() was called without an exception set. In version 2.4, this printed "None", restored that behavior. ........ r52000 | armin.rigo | 2006-09-25 17:16:26 +0200 (Mon, 25 Sep 2006) | 2 lines Another crasher. ........ r52011 | brett.cannon | 2006-09-27 01:38:24 +0200 (Wed, 27 Sep 2006) | 2 lines Make the error message for when the time data and format do not match clearer. ........ r52014 | andrew.kuchling | 2006-09-27 18:37:30 +0200 (Wed, 27 Sep 2006) | 1 line Add news item for rev. 51815 ........ r52018 | andrew.kuchling | 2006-09-27 21:23:05 +0200 (Wed, 27 Sep 2006) | 1 line Make examples do error checking on Py_InitModule ........ r52032 | brett.cannon | 2006-09-29 00:10:14 +0200 (Fri, 29 Sep 2006) | 2 lines Very minor grammatical fix in a comment. ........ r52048 | george.yoshida | 2006-09-30 07:14:02 +0200 (Sat, 30 Sep 2006) | 4 lines SF bug #1567976 : fix typo Will backport to 2.5. ........ r52051 | gregory.p.smith | 2006-09-30 08:08:20 +0200 (Sat, 30 Sep 2006) | 2 lines wording change ........ r52053 | georg.brandl | 2006-09-30 09:24:48 +0200 (Sat, 30 Sep 2006) | 2 lines Bug #1567375: a minor logical glitch in example description. ........ r52056 | georg.brandl | 2006-09-30 09:31:57 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1565661: in webbrowser, split() the command for the default GNOME browser in case it is a command with args. ........ r52058 | georg.brandl | 2006-09-30 10:43:30 +0200 (Sat, 30 Sep 2006) | 4 lines Patch #1567691: super() and new.instancemethod() now don't accept keyword arguments any more (previously they accepted them, but didn't use them). ........ r52061 | georg.brandl | 2006-09-30 11:03:42 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1566800: make sure that EnvironmentError can be called with any number of arguments, as was the case in Python 2.4. ........ r52063 | georg.brandl | 2006-09-30 11:06:45 +0200 (Sat, 30 Sep 2006) | 2 lines Bug #1566663: remove obsolete example from datetime docs. ........ r52065 | georg.brandl | 2006-09-30 11:13:21 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1566602: correct failure of posixpath unittest when $HOME ends with a slash. ........ r52068 | georg.brandl | 2006-09-30 12:58:01 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1457823: cgi.(Sv)FormContentDict's constructor now takes keep_blank_values and strict_parsing keyword arguments. ........ r52069 | georg.brandl | 2006-09-30 13:06:47 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1560617: in pyclbr, return full module name not only for classes, but also for functions. ........ r52072 | georg.brandl | 2006-09-30 13:17:34 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1556784: allow format strings longer than 127 characters in datetime's strftime function. ........ r52075 | georg.brandl | 2006-09-30 13:22:28 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1446043: correctly raise a LookupError if an encoding name given to encodings.search_function() contains a dot. ........ r52078 | georg.brandl | 2006-09-30 14:02:57 +0200 (Sat, 30 Sep 2006) | 3 lines Bug #1546052: clarify that PyString_FromString(AndSize) copies the string pointed to by its parameter. ........ r52080 | georg.brandl | 2006-09-30 14:16:03 +0200 (Sat, 30 Sep 2006) | 3 lines Convert test_import to unittest. ........ r52083 | kurt.kaiser | 2006-10-01 23:16:45 +0200 (Sun, 01 Oct 2006) | 5 lines Some syntax errors were being caught by tokenize during the tabnanny check, resulting in obscure error messages. Do the syntax check first. Bug 1562716, 1562719 ........ r52084 | kurt.kaiser | 2006-10-01 23:54:37 +0200 (Sun, 01 Oct 2006) | 3 lines Add comment explaining that error msgs may be due to user code when running w/o subprocess. ........ r52086 | martin.v.loewis | 2006-10-02 16:55:51 +0200 (Mon, 02 Oct 2006) | 3 lines Fix test for uintptr_t. Fixes #1568842. Will backport. ........ r52089 | martin.v.loewis | 2006-10-02 17:20:37 +0200 (Mon, 02 Oct 2006) | 3 lines Guard uintptr_t test with HAVE_STDINT_H, test for stdint.h. Will backport. ........ r52100 | vinay.sajip | 2006-10-03 20:02:37 +0200 (Tue, 03 Oct 2006) | 1 line Documentation omitted the additional parameter to LogRecord.__init__ which was added in 2.5. (See SF #1569622). ........ r52101 | vinay.sajip | 2006-10-03 20:20:26 +0200 (Tue, 03 Oct 2006) | 1 line Documentation clarified to mention optional parameters. ........ r52102 | vinay.sajip | 2006-10-03 20:21:56 +0200 (Tue, 03 Oct 2006) | 1 line Modified LogRecord.__init__ to make the func parameter optional. (See SF #1569622). ........ r52121 | brett.cannon | 2006-10-03 23:58:55 +0200 (Tue, 03 Oct 2006) | 2 lines Fix minor typo in a comment. ........ r52123 | brett.cannon | 2006-10-04 01:23:14 +0200 (Wed, 04 Oct 2006) | 2 lines Convert test_imp over to unittest. ........ r52128 | barry.warsaw | 2006-10-04 04:06:36 +0200 (Wed, 04 Oct 2006) | 3 lines decode_rfc2231(): As Christian Robottom Reis points out, it makes no sense to test for parts > 3 when we use .split(..., 2). ........ r52129 | jeremy.hylton | 2006-10-04 04:24:52 +0200 (Wed, 04 Oct 2006) | 9 lines Fix for SF bug 1569998: break permitted inside try. The compiler was checking that there was something on the fblock stack, but not that there was a loop on the stack. Fixed that and added a test for the specific syntax error. Bug fix candidate. ........ r52130 | martin.v.loewis | 2006-10-04 07:47:34 +0200 (Wed, 04 Oct 2006) | 4 lines Fix integer negation and absolute value to not rely on undefined behaviour of the C compiler anymore. Will backport to 2.5 and 2.4. ........ r52135 | martin.v.loewis | 2006-10-04 11:21:20 +0200 (Wed, 04 Oct 2006) | 1 line Forward port r52134: Add uuids for 2.4.4. ........ r52137 | armin.rigo | 2006-10-04 12:23:57 +0200 (Wed, 04 Oct 2006) | 3 lines Compilation problem caused by conflicting typedefs for uint32_t (unsigned long vs. unsigned int). ........ r52139 | armin.rigo | 2006-10-04 14:17:45 +0200 (Wed, 04 Oct 2006) | 23 lines Forward-port of r52136,52138: a review of overflow-detecting code. * unified the way intobject, longobject and mystrtoul handle values around -sys.maxint-1. * in general, trying to entierely avoid overflows in any computation involving signed ints or longs is extremely involved. Fixed a few simple cases where a compiler might be too clever (but that's all guesswork). * more overflow checks against bad data in marshal.c. * 2.5 specific: fixed a number of places that were still confusing int and Py_ssize_t. Some of them could potentially have caused "real-world" breakage. * list.pop(x): fixing overflow issues on x was messy. I just reverted to PyArg_ParseTuple("n"), which does the right thing. (An obscure test was trying to give a Decimal to list.pop()... doesn't make sense any more IMHO) * trying to write a few tests... ........ r52147 | andrew.kuchling | 2006-10-04 15:42:43 +0200 (Wed, 04 Oct 2006) | 6 lines Cause a PyObject_Malloc() failure to trigger a MemoryError, and then add 'if (PyErr_Occurred())' checks to various places so that NULL is returned properly. 2.4 backport candidate. ........ r52148 | martin.v.loewis | 2006-10-04 17:25:28 +0200 (Wed, 04 Oct 2006) | 1 line Add MSVC8 project files to create wininst-8.exe. ........ r52196 | brett.cannon | 2006-10-06 00:02:31 +0200 (Fri, 06 Oct 2006) | 7 lines Clarify what "re-initialization" means for init_builtin() and init_dynamic(). Also remove warning about re-initialization as possibly raising an execption as both call _PyImport_FindExtension() which pulls any module that was already imported from the Python process' extension cache and just copies the __dict__ into the module stored in sys.modules. ........ r52200 | fred.drake | 2006-10-06 02:03:45 +0200 (Fri, 06 Oct 2006) | 3 lines - update links - remove Sleepycat name now that they have been bought ........ r52204 | andrew.kuchling | 2006-10-06 12:41:01 +0200 (Fri, 06 Oct 2006) | 1 line Case fix ........ r52208 | georg.brandl | 2006-10-06 14:46:08 +0200 (Fri, 06 Oct 2006) | 3 lines Fix name. ........ r52211 | andrew.kuchling | 2006-10-06 15:18:26 +0200 (Fri, 06 Oct 2006) | 1 line [Bug #1545341] Allow 'classifier' parameter to be a tuple as well as a list. Will backport. ........ r52212 | armin.rigo | 2006-10-06 18:33:22 +0200 (Fri, 06 Oct 2006) | 4 lines A very minor bug fix: this code looks like it is designed to accept any hue value and do the modulo itself, except it doesn't quite do it in all cases. At least, the "cannot get here" comment was wrong. ........ r52213 | andrew.kuchling | 2006-10-06 20:51:55 +0200 (Fri, 06 Oct 2006) | 1 line Comment grammar ........ r52218 | skip.montanaro | 2006-10-07 13:05:02 +0200 (Sat, 07 Oct 2006) | 6 lines Note that the excel_tab class is registered as the "excel-tab" dialect. Fixes 1572471. Make a similar change for the excel class and clean up references to the Dialects and Formatting Parameters section in a few places. ........ r52221 | georg.brandl | 2006-10-08 09:11:54 +0200 (Sun, 08 Oct 2006) | 3 lines Add missing NEWS entry for rev. 52129. ........ r52223 | hyeshik.chang | 2006-10-08 15:48:34 +0200 (Sun, 08 Oct 2006) | 3 lines Bug #1572832: fix a bug in ISO-2022 codecs which may cause segfault when encoding non-BMP unicode characters. (Submitted by Ray Chason) ........ r52227 | ronald.oussoren | 2006-10-08 19:37:58 +0200 (Sun, 08 Oct 2006) | 4 lines Add version number to the link to the python documentation in /Developer/Documentation/Python, better for users that install multiple versions of python. ........ r52229 | ronald.oussoren | 2006-10-08 19:40:02 +0200 (Sun, 08 Oct 2006) | 2 lines Fix for bug #1570284 ........ r52233 | ronald.oussoren | 2006-10-08 19:49:52 +0200 (Sun, 08 Oct 2006) | 6 lines MacOSX: distutils changes the values of BASECFLAGS and LDFLAGS when using a universal build of python on OSX 10.3 to ensure that those flags can be used to compile code (the universal build uses compiler flags that aren't supported on 10.3). This patches gives the same treatment to CFLAGS, PY_CFLAGS and BLDSHARED. ........ r52236 | ronald.oussoren | 2006-10-08 19:51:46 +0200 (Sun, 08 Oct 2006) | 5 lines MacOSX: The universal build requires that users have the MacOSX10.4u SDK installed to build extensions. This patch makes distutils emit a warning when the compiler should use an SDK but that SDK is not installed, hopefully reducing some confusion. ........ r52238 | ronald.oussoren | 2006-10-08 20:18:26 +0200 (Sun, 08 Oct 2006) | 3 lines MacOSX: add more logic to recognize the correct startup file to patch to the shell profile patching post-install script. ........ r52242 | andrew.kuchling | 2006-10-09 19:10:12 +0200 (Mon, 09 Oct 2006) | 1 line Add news item for rev. 52211 change ........ r52245 | andrew.kuchling | 2006-10-09 20:05:19 +0200 (Mon, 09 Oct 2006) | 1 line Fix wording in comment ........ r52251 | georg.brandl | 2006-10-09 21:03:06 +0200 (Mon, 09 Oct 2006) | 2 lines Patch #1572724: fix typo ('=' instead of '==') in _msi.c. ........ r52255 | barry.warsaw | 2006-10-09 21:43:24 +0200 (Mon, 09 Oct 2006) | 2 lines List gc.get_count() in the module docstring. ........ r52257 | martin.v.loewis | 2006-10-09 22:44:25 +0200 (Mon, 09 Oct 2006) | 1 line Bug #1565150: Fix subsecond processing for os.utime on Windows. ........ r52268 | ronald.oussoren | 2006-10-10 09:55:06 +0200 (Tue, 10 Oct 2006) | 2 lines MacOSX: fix permission problem in the generated installer ........ r52293 | georg.brandl | 2006-10-12 09:38:04 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1575746: fix typo in property() docs. ........ r52295 | georg.brandl | 2006-10-12 09:57:21 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #813342: Start the IDLE subprocess with -Qnew if the parent is started with that option. ........ r52297 | georg.brandl | 2006-10-12 10:22:53 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1565919: document set types in the Language Reference. ........ r52299 | georg.brandl | 2006-10-12 11:20:33 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1550524: better heuristics to find correct class definition in inspect.findsource(). ........ r52301 | georg.brandl | 2006-10-12 11:47:12 +0200 (Thu, 12 Oct 2006) | 4 lines Bug #1548891: The cStringIO.StringIO() constructor now encodes unicode arguments with the system default encoding just like the write() method does, instead of converting it to a raw buffer. ........ r52303 | georg.brandl | 2006-10-12 13:14:40 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1546628: add a note about urlparse.urljoin() and absolute paths. ........ r52305 | georg.brandl | 2006-10-12 13:27:59 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1545497: when given an explicit base, int() did ignore NULs embedded in the string to convert. ........ r52307 | georg.brandl | 2006-10-12 13:41:11 +0200 (Thu, 12 Oct 2006) | 3 lines Add a note to fpectl docs that it's not built by default (bug #1556261). ........ r52309 | georg.brandl | 2006-10-12 13:46:57 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1560114: the Mac filesystem does have accurate information about the case of filenames. ........ r52311 | georg.brandl | 2006-10-12 13:59:27 +0200 (Thu, 12 Oct 2006) | 2 lines Small grammar fix, thanks Sjoerd. ........ r52313 | georg.brandl | 2006-10-12 14:03:07 +0200 (Thu, 12 Oct 2006) | 2 lines Fix tarfile depending on buggy int('1\0', base) behavior. ........ r52315 | georg.brandl | 2006-10-12 14:33:07 +0200 (Thu, 12 Oct 2006) | 2 lines Bug #1283491: follow docstring convention wrt. keyword-able args in sum(). ........ r52316 | georg.brandl | 2006-10-12 15:08:16 +0200 (Thu, 12 Oct 2006) | 3 lines Bug #1560179: speed up posixpath.(dir|base)name ........ r52327 | brett.cannon | 2006-10-14 08:36:45 +0200 (Sat, 14 Oct 2006) | 3 lines Clean up the language of a sentence relating to the connect() function and user-defined datatypes. ........ r52332 | neal.norwitz | 2006-10-14 23:33:38 +0200 (Sat, 14 Oct 2006) | 3 lines Update the peephole optimizer to remove more dead code (jumps after returns) and inline jumps to returns. ........ r52333 | martin.v.loewis | 2006-10-15 09:54:40 +0200 (Sun, 15 Oct 2006) | 4 lines Patch #1576954: Update VC6 build directory; remove redundant files in VC7. Will backport to 2.5. ........ r52335 | martin.v.loewis | 2006-10-15 10:43:33 +0200 (Sun, 15 Oct 2006) | 1 line Patch #1576166: Support os.utime for directories on Windows NT+. ........ r52336 | martin.v.loewis | 2006-10-15 10:51:22 +0200 (Sun, 15 Oct 2006) | 2 lines Patch #1577551: Add ctypes and ET build support for VC6. Will backport to 2.5. ........ r52338 | martin.v.loewis | 2006-10-15 11:35:51 +0200 (Sun, 15 Oct 2006) | 1 line Loosen the test for equal time stamps. ........ r52339 | martin.v.loewis | 2006-10-15 11:43:39 +0200 (Sun, 15 Oct 2006) | 2 lines Bug #1567666: Emulate GetFileAttributesExA for Win95. Will backport to 2.5. ........ r52341 | martin.v.loewis | 2006-10-15 13:02:07 +0200 (Sun, 15 Oct 2006) | 2 lines Round to int, because some systems support sub-second time stamps in stat, but not in utime. Also be consistent with modifying only mtime, not atime. ........ r52342 | martin.v.loewis | 2006-10-15 13:57:40 +0200 (Sun, 15 Oct 2006) | 2 lines Set the eol-style for project files to "CRLF". ........ r52343 | martin.v.loewis | 2006-10-15 13:59:56 +0200 (Sun, 15 Oct 2006) | 3 lines Drop binary property on dsp files, set eol-style to CRLF instead. ........ r52344 | martin.v.loewis | 2006-10-15 14:01:43 +0200 (Sun, 15 Oct 2006) | 2 lines Remove binary property, set eol-style to CRLF instead. ........ r52346 | martin.v.loewis | 2006-10-15 16:30:38 +0200 (Sun, 15 Oct 2006) | 2 lines Mention the bdist_msi module. Will backport to 2.5. ........ r52354 | brett.cannon | 2006-10-16 05:09:52 +0200 (Mon, 16 Oct 2006) | 3 lines Fix turtle so that you can launch the demo2 function on its own instead of only when the module is launched as a script. ........ r52356 | martin.v.loewis | 2006-10-17 17:18:06 +0200 (Tue, 17 Oct 2006) | 2 lines Patch #1457736: Update VC6 to use current PCbuild settings. Will backport to 2.5. ........ r52360 | martin.v.loewis | 2006-10-17 20:09:55 +0200 (Tue, 17 Oct 2006) | 2 lines Remove obsolete file. Will backport. ........ r52363 | martin.v.loewis | 2006-10-17 20:59:23 +0200 (Tue, 17 Oct 2006) | 4 lines Forward-port r52358: - Bug #1578513: Cross compilation was broken by a change to configure. Repair so that it's back to how it was in 2.4.3. ........ r52365 | thomas.heller | 2006-10-17 21:30:48 +0200 (Tue, 17 Oct 2006) | 6 lines ctypes callback functions only support 'fundamental' result types. Check this and raise an error when something else is used - before this change ctypes would hang or crash when such a callback was called. This is a partial fix for #1574584. Will backport to release25-maint. ........ r52377 | tim.peters | 2006-10-18 07:06:06 +0200 (Wed, 18 Oct 2006) | 2 lines newIobject(): repaired incorrect cast to quiet MSVC warning. ........ r52378 | tim.peters | 2006-10-18 07:09:12 +0200 (Wed, 18 Oct 2006) | 2 lines Whitespace normalization. ........ r52379 | tim.peters | 2006-10-18 07:10:28 +0200 (Wed, 18 Oct 2006) | 2 lines Add missing svn:eol-style to text files. ........ r52387 | martin.v.loewis | 2006-10-19 12:58:46 +0200 (Thu, 19 Oct 2006) | 3 lines Add check for the PyArg_ParseTuple format, and declare it if it is supported. ........ r52388 | martin.v.loewis | 2006-10-19 13:00:37 +0200 (Thu, 19 Oct 2006) | 3 lines Fix various minor errors in passing arguments to PyArg_ParseTuple. ........ r52389 | martin.v.loewis | 2006-10-19 18:01:37 +0200 (Thu, 19 Oct 2006) | 2 lines Restore CFLAGS after checking for __attribute__ ........ r52390 | andrew.kuchling | 2006-10-19 23:55:55 +0200 (Thu, 19 Oct 2006) | 1 line [Bug #1576348] Fix typo in example ........ r52414 | walter.doerwald | 2006-10-22 10:59:41 +0200 (Sun, 22 Oct 2006) | 2 lines Port test___future__ to unittest. ........ r52415 | ronald.oussoren | 2006-10-22 12:45:18 +0200 (Sun, 22 Oct 2006) | 3 lines Patch #1580674: with this patch os.readlink uses the filesystem encoding to decode unicode objects and returns an unicode object when the argument is one. ........ r52416 | martin.v.loewis | 2006-10-22 12:46:18 +0200 (Sun, 22 Oct 2006) | 3 lines Patch #1580872: Remove duplicate declaration of PyCallable_Check. Will backport to 2.5. ........ r52418 | martin.v.loewis | 2006-10-22 12:55:15 +0200 (Sun, 22 Oct 2006) | 4 lines - Patch #1560695: Add .note.GNU-stack to ctypes' sysv.S so that ctypes isn't considered as requiring executable stacks. Will backport to 2.5. ........ r52420 | martin.v.loewis | 2006-10-22 15:45:13 +0200 (Sun, 22 Oct 2006) | 3 lines Remove passwd.adjunct.byname from list of maps for test_nis. Will backport to 2.5. ........ r52431 | georg.brandl | 2006-10-24 18:54:16 +0200 (Tue, 24 Oct 2006) | 2 lines Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated ........ r52446 | andrew.kuchling | 2006-10-26 21:10:46 +0200 (Thu, 26 Oct 2006) | 1 line [Bug #1579796] Wrong syntax for PyDateTime_IMPORT in documentation. Reported by David Faure. ........ r52449 | andrew.kuchling | 2006-10-26 21:16:46 +0200 (Thu, 26 Oct 2006) | 1 line Typo fix ........ r52452 | martin.v.loewis | 2006-10-27 08:16:31 +0200 (Fri, 27 Oct 2006) | 3 lines Patch #1549049: Rewrite type conversion in structmember. Fixes #1545696 and #1566140. Will backport to 2.5. ........ r52454 | martin.v.loewis | 2006-10-27 08:42:27 +0200 (Fri, 27 Oct 2006) | 2 lines Check for values.h. Will backport. ........ r52456 | martin.v.loewis | 2006-10-27 09:06:52 +0200 (Fri, 27 Oct 2006) | 2 lines Get DBL_MAX from float.h not values.h. Will backport. ........ r52458 | martin.v.loewis | 2006-10-27 09:13:28 +0200 (Fri, 27 Oct 2006) | 2 lines Patch #1567274: Support SMTP over TLS. ........ r52459 | andrew.kuchling | 2006-10-27 13:33:29 +0200 (Fri, 27 Oct 2006) | 1 line Set svn:keywords property ........ r52460 | andrew.kuchling | 2006-10-27 13:36:41 +0200 (Fri, 27 Oct 2006) | 1 line Add item ........ r52461 | andrew.kuchling | 2006-10-27 13:37:01 +0200 (Fri, 27 Oct 2006) | 1 line Some wording changes and markup fixes ........ r52462 | andrew.kuchling | 2006-10-27 14:18:38 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1585690] Note that line_num was added in Python 2.5 ........ r52464 | andrew.kuchling | 2006-10-27 14:50:38 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1583946] Reword description of server and issuer ........ r52466 | andrew.kuchling | 2006-10-27 15:06:25 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1562583] Mention the set_reuse_addr() method ........ r52469 | andrew.kuchling | 2006-10-27 15:22:46 +0200 (Fri, 27 Oct 2006) | 4 lines [Bug #1542016] Report PCALL_POP value. This makes the return value of sys.callstats() match its docstring. Backport candidate. Though it's an API change, this is a pretty obscure portion of the API. ........ r52473 | andrew.kuchling | 2006-10-27 16:53:41 +0200 (Fri, 27 Oct 2006) | 1 line Point users to the subprocess module in the docs for os.system, os.spawn*, os.popen2, and the popen2 and commands modules ........ r52476 | andrew.kuchling | 2006-10-27 18:39:10 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1576241] Let functools.wraps work with built-in functions ........ r52478 | andrew.kuchling | 2006-10-27 18:55:34 +0200 (Fri, 27 Oct 2006) | 1 line [Bug #1575506] The _singlefileMailbox class was using the wrong file object in its flush() method, causing an error ........ r52480 | andrew.kuchling | 2006-10-27 19:06:16 +0200 (Fri, 27 Oct 2006) | 1 line Clarify docstring ........ r52481 | andrew.kuchling | 2006-10-27 19:11:23 +0200 (Fri, 27 Oct 2006) | 5 lines [Patch #1574068 by Scott Dial] urllib and urllib2 were using base64.encodestring() for encoding authentication data. encodestring() can include newlines for very long input, which produced broken HTTP headers. ........ r52483 | andrew.kuchling | 2006-10-27 20:13:46 +0200 (Fri, 27 Oct 2006) | 1 line Check db_setup_debug for a few print statements; change sqlite_setup_debug to False ........ r52484 | andrew.kuchling | 2006-10-27 20:15:02 +0200 (Fri, 27 Oct 2006) | 1 line [Patch #1503717] Tiny patch from Chris AtLee to stop a lengthy line from being printed ........ r52485 | thomas.heller | 2006-10-27 20:31:36 +0200 (Fri, 27 Oct 2006) | 5 lines WindowsError.str should display the windows error code, not the posix error code; with test. Fixes #1576174. Will backport to release25-maint. ........ r52487 | thomas.heller | 2006-10-27 21:05:53 +0200 (Fri, 27 Oct 2006) | 4 lines Modulefinder now handles absolute and relative imports, including tests. Will backport to release25-maint. ........ r52488 | georg.brandl | 2006-10-27 22:39:43 +0200 (Fri, 27 Oct 2006) | 2 lines Patch #1552024: add decorator support to unparse.py demo script. ........ r52492 | walter.doerwald | 2006-10-28 12:47:12 +0200 (Sat, 28 Oct 2006) | 2 lines Port test_bufio to unittest. ........ r52493 | georg.brandl | 2006-10-28 15:10:17 +0200 (Sat, 28 Oct 2006) | 6 lines Convert test_global, test_scope and test_grammar to unittest. I tried to enclose all tests which must be run at the toplevel (instead of inside a method) in exec statements. ........ r52494 | georg.brandl | 2006-10-28 15:11:41 +0200 (Sat, 28 Oct 2006) | 3 lines Update outstanding bugs test file. ........ r52495 | georg.brandl | 2006-10-28 15:51:49 +0200 (Sat, 28 Oct 2006) | 3 lines Convert test_math to unittest. ........ r52496 | georg.brandl | 2006-10-28 15:56:58 +0200 (Sat, 28 Oct 2006) | 3 lines Convert test_opcodes to unittest. ........ r52497 | georg.brandl | 2006-10-28 18:04:04 +0200 (Sat, 28 Oct 2006) | 2 lines Fix nth() itertool recipe. ........ r52500 | georg.brandl | 2006-10-28 22:25:09 +0200 (Sat, 28 Oct 2006) | 2 lines make test_grammar pass with python -O ........ r52501 | neal.norwitz | 2006-10-28 23:15:30 +0200 (Sat, 28 Oct 2006) | 6 lines Add some asserts. In sysmodule, I think these were to try to silence some warnings from Klokwork. They verify the assumptions of the format of svn version output. The assert in the thread module helped debug a problem on HP-UX. ........ r52502 | neal.norwitz | 2006-10-28 23:16:54 +0200 (Sat, 28 Oct 2006) | 5 lines Fix warnings with HP's C compiler. It doesn't recognize that infinite loops are, um, infinite. These conditions should not be able to happen. Will backport. ........ r52503 | neal.norwitz | 2006-10-28 23:17:51 +0200 (Sat, 28 Oct 2006) | 5 lines Fix crash in test on HP-UX. Apparently, it's not possible to delete a lock if it's held (even by the current thread). Will backport. ........ r52504 | neal.norwitz | 2006-10-28 23:19:07 +0200 (Sat, 28 Oct 2006) | 6 lines Fix bug #1565514, SystemError not raised on too many nested blocks. It seems like this should be a different error than SystemError, but I don't have any great ideas and SystemError was raised in 2.4 and earlier. Will backport. ........ r52505 | neal.norwitz | 2006-10-28 23:20:12 +0200 (Sat, 28 Oct 2006) | 4 lines Prevent crash if alloc of garbage fails. Found by Typo.pl. Will backport. ........ r52506 | neal.norwitz | 2006-10-28 23:21:00 +0200 (Sat, 28 Oct 2006) | 4 lines Don't inline Py_ADDRESS_IN_RANGE with gcc 4+ either. Will backport. ........ r52513 | neal.norwitz | 2006-10-28 23:56:49 +0200 (Sat, 28 Oct 2006) | 2 lines Fix test_modulefinder so it doesn't fail when run after test_distutils. ........ r52514 | neal.norwitz | 2006-10-29 00:12:26 +0200 (Sun, 29 Oct 2006) | 4 lines From SF 1557890, fix problem of using wrong type in example. Will backport. ........ r52517 | georg.brandl | 2006-10-29 09:39:22 +0100 (Sun, 29 Oct 2006) | 4 lines Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and fix all codecs file wrappers to work correctly with the "with" statement (bug #1586513). ........ r52519 | georg.brandl | 2006-10-29 09:47:08 +0100 (Sun, 29 Oct 2006) | 3 lines Clean up a leftover from old listcomp generation code. ........ r52520 | georg.brandl | 2006-10-29 09:53:06 +0100 (Sun, 29 Oct 2006) | 4 lines Bug #1586448: the compiler module now emits the same bytecode for list comprehensions as the builtin compiler, using the LIST_APPEND opcode. ........ r52521 | georg.brandl | 2006-10-29 10:01:01 +0100 (Sun, 29 Oct 2006) | 3 lines Remove trailing comma. ........ r52522 | georg.brandl | 2006-10-29 10:05:04 +0100 (Sun, 29 Oct 2006) | 3 lines Bug #1357915: allow all sequence types for shell arguments in subprocess. ........ r52524 | georg.brandl | 2006-10-29 10:16:12 +0100 (Sun, 29 Oct 2006) | 3 lines Patch #1583880: fix tarfile's problems with long names and posix/ GNU modes. ........ r52526 | georg.brandl | 2006-10-29 10:18:00 +0100 (Sun, 29 Oct 2006) | 3 lines Test assert if __debug__ is true. ........ r52527 | georg.brandl | 2006-10-29 10:32:16 +0100 (Sun, 29 Oct 2006) | 2 lines Fix the new EncodedFile test to work with big endian platforms. ........ r52529 | georg.brandl | 2006-10-29 15:39:09 +0100 (Sun, 29 Oct 2006) | 2 lines Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders. ........ r52532 | georg.brandl | 2006-10-29 19:01:08 +0100 (Sun, 29 Oct 2006) | 2 lines Bug #1586773: extend hashlib docstring. ........ r52534 | neal.norwitz | 2006-10-29 19:30:10 +0100 (Sun, 29 Oct 2006) | 4 lines Update comments, remove commented out code. Move assembler structure next to assembler code to make it easier to move it to a separate file. ........ r52535 | georg.brandl | 2006-10-29 19:31:42 +0100 (Sun, 29 Oct 2006) | 3 lines Bug #1576657: when setting a KeyError for a tuple key, make sure that the tuple isn't used as the "exception arguments tuple". ........ r52537 | georg.brandl | 2006-10-29 20:13:40 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_mmap to unittest. ........ r52538 | georg.brandl | 2006-10-29 20:20:45 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_poll to unittest. ........ r52539 | georg.brandl | 2006-10-29 20:24:43 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_nis to unittest. ........ r52540 | georg.brandl | 2006-10-29 20:35:03 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_types to unittest. ........ r52541 | georg.brandl | 2006-10-29 20:51:16 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_cookie to unittest. ........ r52542 | georg.brandl | 2006-10-29 21:09:12 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_cgi to unittest. ........ r52543 | georg.brandl | 2006-10-29 21:24:01 +0100 (Sun, 29 Oct 2006) | 3 lines Completely convert test_httplib to unittest. ........ r52544 | georg.brandl | 2006-10-29 21:28:26 +0100 (Sun, 29 Oct 2006) | 2 lines Convert test_MimeWriter to unittest. ........ r52545 | georg.brandl | 2006-10-29 21:31:17 +0100 (Sun, 29 Oct 2006) | 3 lines Convert test_openpty to unittest. ........ r52546 | georg.brandl | 2006-10-29 21:35:12 +0100 (Sun, 29 Oct 2006) | 3 lines Remove leftover test output file. ........ r52547 | georg.brandl | 2006-10-29 22:54:18 +0100 (Sun, 29 Oct 2006) | 3 lines Move the check for openpty to the beginning. ........ r52548 | walter.doerwald | 2006-10-29 23:06:28 +0100 (Sun, 29 Oct 2006) | 2 lines Add tests for basic argument errors. ........ r52549 | walter.doerwald | 2006-10-30 00:02:27 +0100 (Mon, 30 Oct 2006) | 3 lines Add tests for incremental codecs with an errors argument. ........ r52550 | neal.norwitz | 2006-10-30 00:39:03 +0100 (Mon, 30 Oct 2006) | 1 line Fix refleak ........ r52552 | neal.norwitz | 2006-10-30 00:58:36 +0100 (Mon, 30 Oct 2006) | 1 line I'm assuming this is correct, it fixes the tests so they pass again ........ r52555 | vinay.sajip | 2006-10-31 18:32:37 +0100 (Tue, 31 Oct 2006) | 1 line Change to improve speed of _fixupChildren ........ r52556 | vinay.sajip | 2006-10-31 18:34:31 +0100 (Tue, 31 Oct 2006) | 1 line Added relativeCreated to Formatter doc (has been in the system for a long time - was unaccountably left out of the docs and not noticed until now). ........ r52588 | thomas.heller | 2006-11-02 20:48:24 +0100 (Thu, 02 Nov 2006) | 5 lines Replace the XXX marker in the 'Arrays and pointers' reference manual section with a link to the tutorial sections. Will backport to release25-maint. ........ r52592 | thomas.heller | 2006-11-02 21:22:29 +0100 (Thu, 02 Nov 2006) | 6 lines Fix a code example by adding a missing import. Fixes #1557890. Will backport to release25-maint. ........ r52598 | tim.peters | 2006-11-03 03:32:46 +0100 (Fri, 03 Nov 2006) | 2 lines Whitespace normalization. ........ r52619 | martin.v.loewis | 2006-11-04 19:14:06 +0100 (Sat, 04 Nov 2006) | 4 lines - Patch #1060577: Extract list of RPM files from spec file in bdist_rpm Will backport to 2.5. ........ r52621 | neal.norwitz | 2006-11-04 20:25:22 +0100 (Sat, 04 Nov 2006) | 4 lines Bug #1588287: fix invalid assertion for `1,2` in debug builds. Will backport ........ r52630 | andrew.kuchling | 2006-11-05 22:04:37 +0100 (Sun, 05 Nov 2006) | 1 line Update link ........ r52631 | skip.montanaro | 2006-11-06 15:34:52 +0100 (Mon, 06 Nov 2006) | 1 line note that user can control directory location even if default dir is used ........ r52644 | ronald.oussoren | 2006-11-07 16:53:38 +0100 (Tue, 07 Nov 2006) | 2 lines Fix a number of typos in strings and comments (sf#1589070) ........ r52647 | ronald.oussoren | 2006-11-07 17:00:34 +0100 (Tue, 07 Nov 2006) | 2 lines Whitespace changes to make the source more compliant with PEP8 (SF#1589070) ........ r52651 | thomas.heller | 2006-11-07 19:01:18 +0100 (Tue, 07 Nov 2006) | 3 lines Fix markup. Will backport to release25-maint. ........ r52653 | thomas.heller | 2006-11-07 19:20:47 +0100 (Tue, 07 Nov 2006) | 3 lines Fix grammatical error as well. Will backport to release25-maint. ........ r52657 | andrew.kuchling | 2006-11-07 21:39:16 +0100 (Tue, 07 Nov 2006) | 1 line Add missing word ........ r52662 | martin.v.loewis | 2006-11-08 07:46:37 +0100 (Wed, 08 Nov 2006) | 4 lines Correctly forward exception in instance_contains(). Fixes #1591996. Patch contributed by Neal Norwitz. Will backport. ........ r52664 | martin.v.loewis | 2006-11-08 07:48:36 +0100 (Wed, 08 Nov 2006) | 2 lines News entry for 52662. ........ r52665 | martin.v.loewis | 2006-11-08 08:35:55 +0100 (Wed, 08 Nov 2006) | 2 lines Patch #1351744: Add askyesnocancel helper for tkMessageBox. ........ r52666 | georg.brandl | 2006-11-08 08:45:59 +0100 (Wed, 08 Nov 2006) | 2 lines Patch #1592072: fix docs for return value of PyErr_CheckSignals. ........ r52668 | georg.brandl | 2006-11-08 11:04:29 +0100 (Wed, 08 Nov 2006) | 3 lines Bug #1592533: rename variable in heapq doc example, to avoid shadowing "sorted". ........ r52671 | andrew.kuchling | 2006-11-08 14:35:34 +0100 (Wed, 08 Nov 2006) | 1 line Add section on the functional module ........ r52672 | andrew.kuchling | 2006-11-08 15:14:30 +0100 (Wed, 08 Nov 2006) | 1 line Add section on operator module; make a few edits ........ r52673 | andrew.kuchling | 2006-11-08 15:24:03 +0100 (Wed, 08 Nov 2006) | 1 line Add table of contents; this required fixing a few headings. Some more smalle edits. ........ r52674 | andrew.kuchling | 2006-11-08 15:30:14 +0100 (Wed, 08 Nov 2006) | 1 line More edits ........ r52686 | martin.v.loewis | 2006-11-09 12:06:03 +0100 (Thu, 09 Nov 2006) | 3 lines Patch #838546: Make terminal become controlling in pty.fork(). Will backport to 2.5. ........ r52688 | martin.v.loewis | 2006-11-09 12:27:32 +0100 (Thu, 09 Nov 2006) | 2 lines Patch #1592250: Add elidge argument to Tkinter.Text.search. ........ r52690 | andrew.kuchling | 2006-11-09 14:27:07 +0100 (Thu, 09 Nov 2006) | 7 lines [Bug #1569790] mailbox.Maildir.get_folder() loses factory information Both the Maildir and MH classes had this bug; the patch fixes both classes and adds a test. Will backport to 25-maint. ........ r52692 | andrew.kuchling | 2006-11-09 14:51:14 +0100 (Thu, 09 Nov 2006) | 1 line [Patch #1514544 by David Watson] use fsync() to ensure data is really on disk ........ r52695 | walter.doerwald | 2006-11-09 17:23:26 +0100 (Thu, 09 Nov 2006) | 2 lines Replace C++ comment with C comment (fixes SF bug #1593525). ........ r52712 | andrew.kuchling | 2006-11-09 22:16:46 +0100 (Thu, 09 Nov 2006) | 11 lines [Patch #1514543] mailbox (Maildir): avoid losing messages on name clash Two changes: Where possible, use link()/remove() to move files into a directory; this makes it easier to avoid overwriting an existing file. Use _create_carefully() to create files in tmp/, which uses O_EXCL. Backport candidate. ........ r52716 | phillip.eby | 2006-11-10 01:33:36 +0100 (Fri, 10 Nov 2006) | 4 lines Fix SF#1566719: not creating site-packages (or other target directory) when installing .egg-info for a project that contains no modules or packages, while using --root (as in bdist_rpm). ........ r52719 | andrew.kuchling | 2006-11-10 14:14:01 +0100 (Fri, 10 Nov 2006) | 1 line Reword entry ........ r52725 | andrew.kuchling | 2006-11-10 15:39:01 +0100 (Fri, 10 Nov 2006) | 1 line [Feature request #1542920] Link to wsgi.org ........ r52731 | georg.brandl | 2006-11-11 19:29:11 +0100 (Sat, 11 Nov 2006) | 2 lines Bug #1594742: wrong word in stringobject doc. ........ r52733 | georg.brandl | 2006-11-11 19:32:47 +0100 (Sat, 11 Nov 2006) | 2 lines Bug #1594758: wording improvement for dict.update() docs. ........ r52736 | martin.v.loewis | 2006-11-12 11:32:47 +0100 (Sun, 12 Nov 2006) | 3 lines Patch #1065257: Support passing open files as body in HTTPConnection.request(). ........ r52737 | martin.v.loewis | 2006-11-12 11:41:39 +0100 (Sun, 12 Nov 2006) | 2 lines Patch #1355023: support whence argument for GzipFile.seek. ........ r52738 | martin.v.loewis | 2006-11-12 19:24:26 +0100 (Sun, 12 Nov 2006) | 2 lines Bug #1067760: Deprecate passing floats to file.seek. ........ r52739 | martin.v.loewis | 2006-11-12 19:48:13 +0100 (Sun, 12 Nov 2006) | 3 lines Patch #1359217: Ignore 2xx response before 150 response. Will backport to 2.5. ........ r52741 | martin.v.loewis | 2006-11-12 19:56:03 +0100 (Sun, 12 Nov 2006) | 4 lines Patch #1360200: Use unmangled_version RPM spec field to deal with file name mangling. Will backport to 2.5. ........ r52753 | walter.doerwald | 2006-11-15 17:23:46 +0100 (Wed, 15 Nov 2006) | 2 lines Fix typo. ........ r52754 | georg.brandl | 2006-11-15 18:42:03 +0100 (Wed, 15 Nov 2006) | 2 lines Bug #1594809: add a note to README regarding PYTHONPATH and make install. ........ r52762 | georg.brandl | 2006-11-16 16:05:14 +0100 (Thu, 16 Nov 2006) | 2 lines Bug #1597576: mention that the new base64 api has been introduced in py2.4. ........ r52764 | georg.brandl | 2006-11-16 17:50:59 +0100 (Thu, 16 Nov 2006) | 3 lines Bug #1597824: return the registered function from atexit.register() to facilitate usage as a decorator. ........ r52765 | georg.brandl | 2006-11-16 18:08:45 +0100 (Thu, 16 Nov 2006) | 4 lines Bug #1588217: don't parse "= " as a soft line break in binascii's a2b_qp() function, instead leave it in the string as quopri.decode() does. ........ r52776 | andrew.kuchling | 2006-11-17 14:30:25 +0100 (Fri, 17 Nov 2006) | 17 lines Remove file-locking in MH.pack() method. This change looks massive but it's mostly a re-indenting after removing some try...finally blocks. Also adds a test case that does a pack() while the mailbox is locked; this test would have turned up bugs in the original code on some platforms. In both nmh and GNU Mailutils' implementation of MH-format mailboxes, no locking is done of individual message files when renaming them. The original mailbox.py code did do locking, which meant that message files had to be opened. This code was buggy on certain platforms (found through reading the code); there were code paths that closed the file object and then called _unlock_file() on it. Will backport to 25-maint once I see how the buildbots react to this patch. ........ r52780 | martin.v.loewis | 2006-11-18 19:00:23 +0100 (Sat, 18 Nov 2006) | 5 lines Patch #1538878: Don't make tkSimpleDialog dialogs transient if the parent window is withdrawn. This mirrors what dialog.tcl does. Will backport to 2.5. ........ r52782 | martin.v.loewis | 2006-11-18 19:05:35 +0100 (Sat, 18 Nov 2006) | 4 lines Patch #1594554: Always close a tkSimpleDialog on ok(), even if an exception occurs. Will backport to 2.5. ........ r52784 | martin.v.loewis | 2006-11-18 19:42:11 +0100 (Sat, 18 Nov 2006) | 3 lines Patch #1472877: Fix Tix subwidget name resolution. Will backport to 2.5. ........ r52786 | andrew.kuchling | 2006-11-18 23:17:33 +0100 (Sat, 18 Nov 2006) | 1 line Expand checking in test_sha ........ r52787 | georg.brandl | 2006-11-19 09:48:30 +0100 (Sun, 19 Nov 2006) | 3 lines Patch [ 1586791 ] better error msgs for some TypeErrors ........ r52788 | martin.v.loewis | 2006-11-19 11:41:41 +0100 (Sun, 19 Nov 2006) | 4 lines Make cStringIO.truncate raise IOError for negative arguments (even for -1). Fixes the last bit of #1359365. ........ r52789 | andrew.kuchling | 2006-11-19 19:40:01 +0100 (Sun, 19 Nov 2006) | 1 line Add a test case of data w/ bytes > 127 ........ r52790 | martin.v.loewis | 2006-11-19 19:51:54 +0100 (Sun, 19 Nov 2006) | 3 lines Patch #1070046: Marshal new-style objects like InstanceType in xmlrpclib. ........ r52792 | neal.norwitz | 2006-11-19 22:26:53 +0100 (Sun, 19 Nov 2006) | 4 lines Speed up function calls into the math module by using METH_O. There should be no functional changes. However, the error msgs are slightly different. Also verified that the module dict is not NULL on init. ........ r52794 | george.yoshida | 2006-11-20 03:24:48 +0100 (Mon, 20 Nov 2006) | 2 lines markup fix ........ r52795 | georg.brandl | 2006-11-20 08:12:58 +0100 (Mon, 20 Nov 2006) | 3 lines Further markup fix. ........ r52800 | andrew.kuchling | 2006-11-20 14:39:37 +0100 (Mon, 20 Nov 2006) | 2 lines Jython compatibility fix: if uu.decode() opened its output file, be sure to close it. ........ r52811 | neal.norwitz | 2006-11-21 06:26:22 +0100 (Tue, 21 Nov 2006) | 9 lines Bug #1599782: Fix segfault on bsddb.db.DB().type(). The problem is that _DB_get_type() can't be called without the GIL because it calls a bunch of PyErr_* APIs when an error occurs. There were no other cases in this file that it was called without the GIL. Removing the BEGIN/END THREAD around _DB_get_type() made everything work. Will backport. ........ r52814 | neal.norwitz | 2006-11-21 06:51:51 +0100 (Tue, 21 Nov 2006) | 1 line Oops, convert tabs to spaces ........ r52815 | neal.norwitz | 2006-11-21 07:23:44 +0100 (Tue, 21 Nov 2006) | 1 line Fix SF #1599879, socket.gethostname should ref getfqdn directly. ........ r52817 | martin.v.loewis | 2006-11-21 19:20:25 +0100 (Tue, 21 Nov 2006) | 4 lines Conditionalize definition of _CRT_SECURE_NO_DEPRECATE and _CRT_NONSTDC_NO_DEPRECATE. Will backport. ........ r52821 | martin.v.loewis | 2006-11-22 09:50:02 +0100 (Wed, 22 Nov 2006) | 4 lines Patch #1362975: Rework CodeContext indentation algorithm to avoid hard-coding pixel widths. Also make the text's scrollbar a child of the text frame, not the top widget. ........ r52826 | walter.doerwald | 2006-11-23 06:03:56 +0100 (Thu, 23 Nov 2006) | 3 lines Change decode() so that it works with a buffer (i.e. unicode(..., 'utf-8-sig')) SF bug #1601501. ........ r52833 | georg.brandl | 2006-11-23 10:55:07 +0100 (Thu, 23 Nov 2006) | 2 lines Bug #1601630: little improvement to getopt docs ........ r52835 | michael.hudson | 2006-11-23 14:54:04 +0100 (Thu, 23 Nov 2006) | 3 lines a test for an error condition not covered by existing tests (noticed this when writing the equivalent code for pypy) ........ r52839 | raymond.hettinger | 2006-11-23 22:06:03 +0100 (Thu, 23 Nov 2006) | 1 line Fix and/add typo ........ r52840 | raymond.hettinger | 2006-11-23 22:35:19 +0100 (Thu, 23 Nov 2006) | 1 line ... and the number of the counting shall be three. ........ r52841 | thomas.heller | 2006-11-24 19:45:39 +0100 (Fri, 24 Nov 2006) | 1 line Fix bug #1598620: A ctypes structure cannot contain itself. ........ r52843 | martin.v.loewis | 2006-11-25 16:39:19 +0100 (Sat, 25 Nov 2006) | 3 lines Disable _XOPEN_SOURCE on NetBSD 1.x. Will backport to 2.5 ........ r52845 | georg.brandl | 2006-11-26 20:27:47 +0100 (Sun, 26 Nov 2006) | 2 lines Bug #1603321: make pstats.Stats accept Unicode file paths. ........ r52850 | georg.brandl | 2006-11-27 19:46:21 +0100 (Mon, 27 Nov 2006) | 2 lines Bug #1603789: grammatical error in Tkinter docs. ........ r52855 | thomas.heller | 2006-11-28 21:21:54 +0100 (Tue, 28 Nov 2006) | 7 lines Fix #1563807: _ctypes built on AIX fails with ld ffi error. The contents of ffi_darwin.c must be compiled unless __APPLE__ is defined and __ppc__ is not. Will backport. ........ r52862 | armin.rigo | 2006-11-29 22:59:22 +0100 (Wed, 29 Nov 2006) | 3 lines Forgot a case where the locals can now be a general mapping instead of just a dictionary. (backporting...) ........ r52872 | guido.van.rossum | 2006-11-30 20:23:13 +0100 (Thu, 30 Nov 2006) | 2 lines Update version. ........ r52890 | walter.doerwald | 2006-12-01 17:59:47 +0100 (Fri, 01 Dec 2006) | 3 lines Move xdrlib tests from the module into a separate test script, port the tests to unittest and add a few new tests. ........ r52900 | raymond.hettinger | 2006-12-02 03:00:39 +0100 (Sat, 02 Dec 2006) | 1 line Add name to credits (for untokenize). ........ r52905 | martin.v.loewis | 2006-12-03 10:54:46 +0100 (Sun, 03 Dec 2006) | 2 lines Move IDLE news into NEWS.txt. ........ r52906 | martin.v.loewis | 2006-12-03 12:23:45 +0100 (Sun, 03 Dec 2006) | 4 lines Patch #1544279: Improve thread-safety of the socket module by moving the sock_addr_t storage out of the socket object. Will backport to 2.5. ........ r52908 | martin.v.loewis | 2006-12-03 13:01:53 +0100 (Sun, 03 Dec 2006) | 3 lines Patch #1371075: Make ConfigParser accept optional dict type for ordering, sorting, etc. ........ r52910 | matthias.klose | 2006-12-03 18:16:41 +0100 (Sun, 03 Dec 2006) | 2 lines - Fix build failure on kfreebsd and on the hurd. ........ r52915 | george.yoshida | 2006-12-04 12:41:54 +0100 (Mon, 04 Dec 2006) | 2 lines fix a versionchanged tag ........ r52917 | george.yoshida | 2006-12-05 06:39:50 +0100 (Tue, 05 Dec 2006) | 3 lines Fix pickle doc typo Patch #1608758 ........ r52938 | georg.brandl | 2006-12-06 23:21:18 +0100 (Wed, 06 Dec 2006) | 2 lines Patch #1610437: fix a tarfile bug with long filename headers. ........ r52945 | brett.cannon | 2006-12-07 00:38:48 +0100 (Thu, 07 Dec 2006) | 3 lines Fix a bad assumption that all objects assigned to '__loader__' on a module will have a '_files' attribute. ........ r52951 | georg.brandl | 2006-12-07 10:30:06 +0100 (Thu, 07 Dec 2006) | 3 lines RFE #1592899: mention string.maketrans() in docs for str.translate, remove reference to the old regex module in the former's doc. ........ r52962 | raymond.hettinger | 2006-12-08 04:17:18 +0100 (Fri, 08 Dec 2006) | 1 line Eliminate two redundant calls to PyObject_Hash(). ........ r52963 | raymond.hettinger | 2006-12-08 05:24:33 +0100 (Fri, 08 Dec 2006) | 3 lines Port Armin's fix for a dict resize vulnerability (svn revision 46589, sf bug 1456209). ........ r52964 | raymond.hettinger | 2006-12-08 05:57:50 +0100 (Fri, 08 Dec 2006) | 4 lines Port Georg's dictobject.c fix keys that were tuples got unpacked on the way to setting a KeyError (svn revision 52535, sf bug 1576657). ........ r52966 | raymond.hettinger | 2006-12-08 18:35:25 +0100 (Fri, 08 Dec 2006) | 2 lines Add test for SF bug 1576657 ........ r52970 | georg.brandl | 2006-12-08 21:46:11 +0100 (Fri, 08 Dec 2006) | 3 lines #1577756: svnversion doesn't react to LANG=C, use LC_ALL=C to force English output. ........ r52972 | georg.brandl | 2006-12-09 10:08:29 +0100 (Sat, 09 Dec 2006) | 3 lines Patch #1608267: fix a race condition in os.makedirs() is the directory to be created is already there. ........ r52975 | matthias.klose | 2006-12-09 13:15:27 +0100 (Sat, 09 Dec 2006) | 2 lines - Fix the build of the library reference in info format. ........ r52994 | neal.norwitz | 2006-12-11 02:01:06 +0100 (Mon, 11 Dec 2006) | 1 line Fix a typo ........ r52996 | georg.brandl | 2006-12-11 08:56:33 +0100 (Mon, 11 Dec 2006) | 2 lines Move errno imports back to individual functions. ........ r52998 | vinay.sajip | 2006-12-11 15:07:16 +0100 (Mon, 11 Dec 2006) | 1 line Patch by Jeremy Katz (SF #1609407) ........ r53000 | vinay.sajip | 2006-12-11 15:26:23 +0100 (Mon, 11 Dec 2006) | 1 line Patch by "cuppatea" (SF #1503765) ........
5661 lines
145 KiB
C
5661 lines
145 KiB
C
/* Type object implementation */
|
|
|
|
#include "Python.h"
|
|
#include "structmember.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
static PyMemberDef type_members[] = {
|
|
{"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
|
|
{"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
|
|
{"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
|
|
{"__weakrefoffset__", T_LONG,
|
|
offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
|
|
{"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
|
|
{"__dictoffset__", T_LONG,
|
|
offsetof(PyTypeObject, tp_dictoffset), READONLY},
|
|
{"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
|
|
{0}
|
|
};
|
|
|
|
static PyObject *
|
|
type_name(PyTypeObject *type, void *context)
|
|
{
|
|
const char *s;
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
|
PyHeapTypeObject* et = (PyHeapTypeObject*)type;
|
|
|
|
Py_INCREF(et->ht_name);
|
|
return et->ht_name;
|
|
}
|
|
else {
|
|
s = strrchr(type->tp_name, '.');
|
|
if (s == NULL)
|
|
s = type->tp_name;
|
|
else
|
|
s++;
|
|
return PyString_FromString(s);
|
|
}
|
|
}
|
|
|
|
static int
|
|
type_set_name(PyTypeObject *type, PyObject *value, void *context)
|
|
{
|
|
PyHeapTypeObject* et;
|
|
|
|
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't set %s.__name__", type->tp_name);
|
|
return -1;
|
|
}
|
|
if (!value) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't delete %s.__name__", type->tp_name);
|
|
return -1;
|
|
}
|
|
if (!PyString_Check(value)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can only assign string to %s.__name__, not '%s'",
|
|
type->tp_name, value->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
if (strlen(PyString_AS_STRING(value))
|
|
!= (size_t)PyString_GET_SIZE(value)) {
|
|
PyErr_Format(PyExc_ValueError,
|
|
"__name__ must not contain null bytes");
|
|
return -1;
|
|
}
|
|
|
|
et = (PyHeapTypeObject*)type;
|
|
|
|
Py_INCREF(value);
|
|
|
|
Py_DECREF(et->ht_name);
|
|
et->ht_name = value;
|
|
|
|
type->tp_name = PyString_AS_STRING(value);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
type_module(PyTypeObject *type, void *context)
|
|
{
|
|
PyObject *mod;
|
|
char *s;
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
|
mod = PyDict_GetItemString(type->tp_dict, "__module__");
|
|
if (!mod) {
|
|
PyErr_Format(PyExc_AttributeError, "__module__");
|
|
return 0;
|
|
}
|
|
Py_XINCREF(mod);
|
|
return mod;
|
|
}
|
|
else {
|
|
s = strrchr(type->tp_name, '.');
|
|
if (s != NULL)
|
|
return PyString_FromStringAndSize(
|
|
type->tp_name, (Py_ssize_t)(s - type->tp_name));
|
|
return PyString_FromString("__builtin__");
|
|
}
|
|
}
|
|
|
|
static int
|
|
type_set_module(PyTypeObject *type, PyObject *value, void *context)
|
|
{
|
|
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't set %s.__module__", type->tp_name);
|
|
return -1;
|
|
}
|
|
if (!value) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't delete %s.__module__", type->tp_name);
|
|
return -1;
|
|
}
|
|
|
|
return PyDict_SetItemString(type->tp_dict, "__module__", value);
|
|
}
|
|
|
|
static PyObject *
|
|
type_get_bases(PyTypeObject *type, void *context)
|
|
{
|
|
Py_INCREF(type->tp_bases);
|
|
return type->tp_bases;
|
|
}
|
|
|
|
static PyTypeObject *best_base(PyObject *);
|
|
static int mro_internal(PyTypeObject *);
|
|
static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
|
|
static int add_subclass(PyTypeObject*, PyTypeObject*);
|
|
static void remove_subclass(PyTypeObject *, PyTypeObject *);
|
|
static void update_all_slots(PyTypeObject *);
|
|
|
|
typedef int (*update_callback)(PyTypeObject *, void *);
|
|
static int update_subclasses(PyTypeObject *type, PyObject *name,
|
|
update_callback callback, void *data);
|
|
static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
|
|
update_callback callback, void *data);
|
|
|
|
static int
|
|
mro_subclasses(PyTypeObject *type, PyObject* temp)
|
|
{
|
|
PyTypeObject *subclass;
|
|
PyObject *ref, *subclasses, *old_mro;
|
|
Py_ssize_t i, n;
|
|
|
|
subclasses = type->tp_subclasses;
|
|
if (subclasses == NULL)
|
|
return 0;
|
|
assert(PyList_Check(subclasses));
|
|
n = PyList_GET_SIZE(subclasses);
|
|
for (i = 0; i < n; i++) {
|
|
ref = PyList_GET_ITEM(subclasses, i);
|
|
assert(PyWeakref_CheckRef(ref));
|
|
subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
|
|
assert(subclass != NULL);
|
|
if ((PyObject *)subclass == Py_None)
|
|
continue;
|
|
assert(PyType_Check(subclass));
|
|
old_mro = subclass->tp_mro;
|
|
if (mro_internal(subclass) < 0) {
|
|
subclass->tp_mro = old_mro;
|
|
return -1;
|
|
}
|
|
else {
|
|
PyObject* tuple;
|
|
tuple = PyTuple_Pack(2, subclass, old_mro);
|
|
Py_DECREF(old_mro);
|
|
if (!tuple)
|
|
return -1;
|
|
if (PyList_Append(temp, tuple) < 0)
|
|
return -1;
|
|
Py_DECREF(tuple);
|
|
}
|
|
if (mro_subclasses(subclass, temp) < 0)
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
type_set_bases(PyTypeObject *type, PyObject *value, void *context)
|
|
{
|
|
Py_ssize_t i;
|
|
int r = 0;
|
|
PyObject *ob, *temp;
|
|
PyTypeObject *new_base, *old_base;
|
|
PyObject *old_bases, *old_mro;
|
|
|
|
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't set %s.__bases__", type->tp_name);
|
|
return -1;
|
|
}
|
|
if (!value) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't delete %s.__bases__", type->tp_name);
|
|
return -1;
|
|
}
|
|
if (!PyTuple_Check(value)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can only assign tuple to %s.__bases__, not %s",
|
|
type->tp_name, value->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
if (PyTuple_GET_SIZE(value) == 0) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can only assign non-empty tuple to %s.__bases__, not ()",
|
|
type->tp_name);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
|
|
ob = PyTuple_GET_ITEM(value, i);
|
|
if (!PyType_Check(ob)) {
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
|
|
type->tp_name, ob->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
if (PyType_Check(ob)) {
|
|
if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"a __bases__ item causes an inheritance cycle");
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
new_base = best_base(value);
|
|
|
|
if (!new_base) {
|
|
return -1;
|
|
}
|
|
|
|
if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
|
|
return -1;
|
|
|
|
Py_INCREF(new_base);
|
|
Py_INCREF(value);
|
|
|
|
old_bases = type->tp_bases;
|
|
old_base = type->tp_base;
|
|
old_mro = type->tp_mro;
|
|
|
|
type->tp_bases = value;
|
|
type->tp_base = new_base;
|
|
|
|
if (mro_internal(type) < 0) {
|
|
goto bail;
|
|
}
|
|
|
|
temp = PyList_New(0);
|
|
if (!temp)
|
|
goto bail;
|
|
|
|
r = mro_subclasses(type, temp);
|
|
|
|
if (r < 0) {
|
|
for (i = 0; i < PyList_Size(temp); i++) {
|
|
PyTypeObject* cls;
|
|
PyObject* mro;
|
|
PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
|
|
"", 2, 2, &cls, &mro);
|
|
Py_DECREF(cls->tp_mro);
|
|
cls->tp_mro = mro;
|
|
Py_INCREF(cls->tp_mro);
|
|
}
|
|
Py_DECREF(temp);
|
|
goto bail;
|
|
}
|
|
|
|
Py_DECREF(temp);
|
|
|
|
/* any base that was in __bases__ but now isn't, we
|
|
need to remove |type| from its tp_subclasses.
|
|
conversely, any class now in __bases__ that wasn't
|
|
needs to have |type| added to its subclasses. */
|
|
|
|
/* for now, sod that: just remove from all old_bases,
|
|
add to all new_bases */
|
|
|
|
for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
|
|
ob = PyTuple_GET_ITEM(old_bases, i);
|
|
if (PyType_Check(ob)) {
|
|
remove_subclass(
|
|
(PyTypeObject*)ob, type);
|
|
}
|
|
}
|
|
|
|
for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
|
|
ob = PyTuple_GET_ITEM(value, i);
|
|
if (PyType_Check(ob)) {
|
|
if (add_subclass((PyTypeObject*)ob, type) < 0)
|
|
r = -1;
|
|
}
|
|
}
|
|
|
|
update_all_slots(type);
|
|
|
|
Py_DECREF(old_bases);
|
|
Py_DECREF(old_base);
|
|
Py_DECREF(old_mro);
|
|
|
|
return r;
|
|
|
|
bail:
|
|
Py_DECREF(type->tp_bases);
|
|
Py_DECREF(type->tp_base);
|
|
if (type->tp_mro != old_mro) {
|
|
Py_DECREF(type->tp_mro);
|
|
}
|
|
|
|
type->tp_bases = old_bases;
|
|
type->tp_base = old_base;
|
|
type->tp_mro = old_mro;
|
|
|
|
return -1;
|
|
}
|
|
|
|
static PyObject *
|
|
type_dict(PyTypeObject *type, void *context)
|
|
{
|
|
if (type->tp_dict == NULL) {
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
return PyDictProxy_New(type->tp_dict);
|
|
}
|
|
|
|
static PyObject *
|
|
type_get_doc(PyTypeObject *type, void *context)
|
|
{
|
|
PyObject *result;
|
|
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
|
|
return PyString_FromString(type->tp_doc);
|
|
result = PyDict_GetItemString(type->tp_dict, "__doc__");
|
|
if (result == NULL) {
|
|
result = Py_None;
|
|
Py_INCREF(result);
|
|
}
|
|
else if (result->ob_type->tp_descr_get) {
|
|
result = result->ob_type->tp_descr_get(result, NULL,
|
|
(PyObject *)type);
|
|
}
|
|
else {
|
|
Py_INCREF(result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static PyGetSetDef type_getsets[] = {
|
|
{"__name__", (getter)type_name, (setter)type_set_name, NULL},
|
|
{"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
|
|
{"__module__", (getter)type_module, (setter)type_set_module, NULL},
|
|
{"__dict__", (getter)type_dict, NULL, NULL},
|
|
{"__doc__", (getter)type_get_doc, NULL, NULL},
|
|
{0}
|
|
};
|
|
|
|
static PyObject *
|
|
type_repr(PyTypeObject *type)
|
|
{
|
|
PyObject *mod, *name, *rtn;
|
|
char *kind;
|
|
|
|
mod = type_module(type, NULL);
|
|
if (mod == NULL)
|
|
PyErr_Clear();
|
|
else if (!PyString_Check(mod)) {
|
|
Py_DECREF(mod);
|
|
mod = NULL;
|
|
}
|
|
name = type_name(type, NULL);
|
|
if (name == NULL)
|
|
return NULL;
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
|
|
kind = "class";
|
|
else
|
|
kind = "type";
|
|
|
|
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
|
|
rtn = PyString_FromFormat("<%s '%s.%s'>",
|
|
kind,
|
|
PyString_AS_STRING(mod),
|
|
PyString_AS_STRING(name));
|
|
}
|
|
else
|
|
rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
|
|
|
|
Py_XDECREF(mod);
|
|
Py_DECREF(name);
|
|
return rtn;
|
|
}
|
|
|
|
static PyObject *
|
|
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *obj;
|
|
|
|
if (type->tp_new == NULL) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"cannot create '%.100s' instances",
|
|
type->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
obj = type->tp_new(type, args, kwds);
|
|
if (obj != NULL) {
|
|
/* Ugly exception: when the call was type(something),
|
|
don't call tp_init on the result. */
|
|
if (type == &PyType_Type &&
|
|
PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
|
|
(kwds == NULL ||
|
|
(PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
|
|
return obj;
|
|
/* If the returned object is not an instance of type,
|
|
it won't be initialized. */
|
|
if (!PyType_IsSubtype(obj->ob_type, type))
|
|
return obj;
|
|
type = obj->ob_type;
|
|
if (type->tp_init != NULL &&
|
|
type->tp_init(obj, args, kwds) < 0) {
|
|
Py_DECREF(obj);
|
|
obj = NULL;
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
PyObject *
|
|
PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
|
|
{
|
|
PyObject *obj;
|
|
const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
|
|
/* note that we need to add one, for the sentinel */
|
|
|
|
if (PyType_IS_GC(type))
|
|
obj = _PyObject_GC_Malloc(size);
|
|
else
|
|
obj = (PyObject *)PyObject_MALLOC(size);
|
|
|
|
if (obj == NULL)
|
|
return PyErr_NoMemory();
|
|
|
|
memset(obj, '\0', size);
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
|
|
Py_INCREF(type);
|
|
|
|
if (type->tp_itemsize == 0)
|
|
PyObject_INIT(obj, type);
|
|
else
|
|
(void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
|
|
|
|
if (PyType_IS_GC(type))
|
|
_PyObject_GC_TRACK(obj);
|
|
return obj;
|
|
}
|
|
|
|
PyObject *
|
|
PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
return type->tp_alloc(type, 0);
|
|
}
|
|
|
|
/* Helpers for subtyping */
|
|
|
|
static int
|
|
traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_ssize_t i, n;
|
|
PyMemberDef *mp;
|
|
|
|
n = type->ob_size;
|
|
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
|
|
for (i = 0; i < n; i++, mp++) {
|
|
if (mp->type == T_OBJECT_EX) {
|
|
char *addr = (char *)self + mp->offset;
|
|
PyObject *obj = *(PyObject **)addr;
|
|
if (obj != NULL) {
|
|
int err = visit(obj, arg);
|
|
if (err)
|
|
return err;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
subtype_traverse(PyObject *self, visitproc visit, void *arg)
|
|
{
|
|
PyTypeObject *type, *base;
|
|
traverseproc basetraverse;
|
|
|
|
/* Find the nearest base with a different tp_traverse,
|
|
and traverse slots while we're at it */
|
|
type = self->ob_type;
|
|
base = type;
|
|
while ((basetraverse = base->tp_traverse) == subtype_traverse) {
|
|
if (base->ob_size) {
|
|
int err = traverse_slots(base, self, visit, arg);
|
|
if (err)
|
|
return err;
|
|
}
|
|
base = base->tp_base;
|
|
assert(base);
|
|
}
|
|
|
|
if (type->tp_dictoffset != base->tp_dictoffset) {
|
|
PyObject **dictptr = _PyObject_GetDictPtr(self);
|
|
if (dictptr && *dictptr)
|
|
Py_VISIT(*dictptr);
|
|
}
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
|
|
/* For a heaptype, the instances count as references
|
|
to the type. Traverse the type so the collector
|
|
can find cycles involving this link. */
|
|
Py_VISIT(type);
|
|
|
|
if (basetraverse)
|
|
return basetraverse(self, visit, arg);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
clear_slots(PyTypeObject *type, PyObject *self)
|
|
{
|
|
Py_ssize_t i, n;
|
|
PyMemberDef *mp;
|
|
|
|
n = type->ob_size;
|
|
mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
|
|
for (i = 0; i < n; i++, mp++) {
|
|
if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
|
|
char *addr = (char *)self + mp->offset;
|
|
PyObject *obj = *(PyObject **)addr;
|
|
if (obj != NULL) {
|
|
*(PyObject **)addr = NULL;
|
|
Py_DECREF(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static int
|
|
subtype_clear(PyObject *self)
|
|
{
|
|
PyTypeObject *type, *base;
|
|
inquiry baseclear;
|
|
|
|
/* Find the nearest base with a different tp_clear
|
|
and clear slots while we're at it */
|
|
type = self->ob_type;
|
|
base = type;
|
|
while ((baseclear = base->tp_clear) == subtype_clear) {
|
|
if (base->ob_size)
|
|
clear_slots(base, self);
|
|
base = base->tp_base;
|
|
assert(base);
|
|
}
|
|
|
|
/* There's no need to clear the instance dict (if any);
|
|
the collector will call its tp_clear handler. */
|
|
|
|
if (baseclear)
|
|
return baseclear(self);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
subtype_dealloc(PyObject *self)
|
|
{
|
|
PyTypeObject *type, *base;
|
|
destructor basedealloc;
|
|
|
|
/* Extract the type; we expect it to be a heap type */
|
|
type = self->ob_type;
|
|
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
|
|
|
|
/* Test whether the type has GC exactly once */
|
|
|
|
if (!PyType_IS_GC(type)) {
|
|
/* It's really rare to find a dynamic type that doesn't have
|
|
GC; it can only happen when deriving from 'object' and not
|
|
adding any slots or instance variables. This allows
|
|
certain simplifications: there's no need to call
|
|
clear_slots(), or DECREF the dict, or clear weakrefs. */
|
|
|
|
/* Maybe call finalizer; exit early if resurrected */
|
|
if (type->tp_del) {
|
|
type->tp_del(self);
|
|
if (self->ob_refcnt > 0)
|
|
return;
|
|
}
|
|
|
|
/* Find the nearest base with a different tp_dealloc */
|
|
base = type;
|
|
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
|
|
assert(base->ob_size == 0);
|
|
base = base->tp_base;
|
|
assert(base);
|
|
}
|
|
|
|
/* Call the base tp_dealloc() */
|
|
assert(basedealloc);
|
|
basedealloc(self);
|
|
|
|
/* Can't reference self beyond this point */
|
|
Py_DECREF(type);
|
|
|
|
/* Done */
|
|
return;
|
|
}
|
|
|
|
/* We get here only if the type has GC */
|
|
|
|
/* UnTrack and re-Track around the trashcan macro, alas */
|
|
/* See explanation at end of function for full disclosure */
|
|
PyObject_GC_UnTrack(self);
|
|
++_PyTrash_delete_nesting;
|
|
Py_TRASHCAN_SAFE_BEGIN(self);
|
|
--_PyTrash_delete_nesting;
|
|
/* DO NOT restore GC tracking at this point. weakref callbacks
|
|
* (if any, and whether directly here or indirectly in something we
|
|
* call) may trigger GC, and if self is tracked at that point, it
|
|
* will look like trash to GC and GC will try to delete self again.
|
|
*/
|
|
|
|
/* Find the nearest base with a different tp_dealloc */
|
|
base = type;
|
|
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
|
|
base = base->tp_base;
|
|
assert(base);
|
|
}
|
|
|
|
/* If we added a weaklist, we clear it. Do this *before* calling
|
|
the finalizer (__del__), clearing slots, or clearing the instance
|
|
dict. */
|
|
|
|
if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
|
|
PyObject_ClearWeakRefs(self);
|
|
|
|
/* Maybe call finalizer; exit early if resurrected */
|
|
if (type->tp_del) {
|
|
_PyObject_GC_TRACK(self);
|
|
type->tp_del(self);
|
|
if (self->ob_refcnt > 0)
|
|
goto endlabel; /* resurrected */
|
|
else
|
|
_PyObject_GC_UNTRACK(self);
|
|
}
|
|
|
|
/* Clear slots up to the nearest base with a different tp_dealloc */
|
|
base = type;
|
|
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
|
|
if (base->ob_size)
|
|
clear_slots(base, self);
|
|
base = base->tp_base;
|
|
assert(base);
|
|
}
|
|
|
|
/* If we added a dict, DECREF it */
|
|
if (type->tp_dictoffset && !base->tp_dictoffset) {
|
|
PyObject **dictptr = _PyObject_GetDictPtr(self);
|
|
if (dictptr != NULL) {
|
|
PyObject *dict = *dictptr;
|
|
if (dict != NULL) {
|
|
Py_DECREF(dict);
|
|
*dictptr = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Call the base tp_dealloc(); first retrack self if
|
|
* basedealloc knows about gc.
|
|
*/
|
|
if (PyType_IS_GC(base))
|
|
_PyObject_GC_TRACK(self);
|
|
assert(basedealloc);
|
|
basedealloc(self);
|
|
|
|
/* Can't reference self beyond this point */
|
|
Py_DECREF(type);
|
|
|
|
endlabel:
|
|
++_PyTrash_delete_nesting;
|
|
Py_TRASHCAN_SAFE_END(self);
|
|
--_PyTrash_delete_nesting;
|
|
|
|
/* Explanation of the weirdness around the trashcan macros:
|
|
|
|
Q. What do the trashcan macros do?
|
|
|
|
A. Read the comment titled "Trashcan mechanism" in object.h.
|
|
For one, this explains why there must be a call to GC-untrack
|
|
before the trashcan begin macro. Without understanding the
|
|
trashcan code, the answers to the following questions don't make
|
|
sense.
|
|
|
|
Q. Why do we GC-untrack before the trashcan and then immediately
|
|
GC-track again afterward?
|
|
|
|
A. In the case that the base class is GC-aware, the base class
|
|
probably GC-untracks the object. If it does that using the
|
|
UNTRACK macro, this will crash when the object is already
|
|
untracked. Because we don't know what the base class does, the
|
|
only safe thing is to make sure the object is tracked when we
|
|
call the base class dealloc. But... The trashcan begin macro
|
|
requires that the object is *untracked* before it is called. So
|
|
the dance becomes:
|
|
|
|
GC untrack
|
|
trashcan begin
|
|
GC track
|
|
|
|
Q. Why did the last question say "immediately GC-track again"?
|
|
It's nowhere near immediately.
|
|
|
|
A. Because the code *used* to re-track immediately. Bad Idea.
|
|
self has a refcount of 0, and if gc ever gets its hands on it
|
|
(which can happen if any weakref callback gets invoked), it
|
|
looks like trash to gc too, and gc also tries to delete self
|
|
then. But we're already deleting self. Double dealloction is
|
|
a subtle disaster.
|
|
|
|
Q. Why the bizarre (net-zero) manipulation of
|
|
_PyTrash_delete_nesting around the trashcan macros?
|
|
|
|
A. Some base classes (e.g. list) also use the trashcan mechanism.
|
|
The following scenario used to be possible:
|
|
|
|
- suppose the trashcan level is one below the trashcan limit
|
|
|
|
- subtype_dealloc() is called
|
|
|
|
- the trashcan limit is not yet reached, so the trashcan level
|
|
is incremented and the code between trashcan begin and end is
|
|
executed
|
|
|
|
- this destroys much of the object's contents, including its
|
|
slots and __dict__
|
|
|
|
- basedealloc() is called; this is really list_dealloc(), or
|
|
some other type which also uses the trashcan macros
|
|
|
|
- the trashcan limit is now reached, so the object is put on the
|
|
trashcan's to-be-deleted-later list
|
|
|
|
- basedealloc() returns
|
|
|
|
- subtype_dealloc() decrefs the object's type
|
|
|
|
- subtype_dealloc() returns
|
|
|
|
- later, the trashcan code starts deleting the objects from its
|
|
to-be-deleted-later list
|
|
|
|
- subtype_dealloc() is called *AGAIN* for the same object
|
|
|
|
- at the very least (if the destroyed slots and __dict__ don't
|
|
cause problems) the object's type gets decref'ed a second
|
|
time, which is *BAD*!!!
|
|
|
|
The remedy is to make sure that if the code between trashcan
|
|
begin and end in subtype_dealloc() is called, the code between
|
|
trashcan begin and end in basedealloc() will also be called.
|
|
This is done by decrementing the level after passing into the
|
|
trashcan block, and incrementing it just before leaving the
|
|
block.
|
|
|
|
But now it's possible that a chain of objects consisting solely
|
|
of objects whose deallocator is subtype_dealloc() will defeat
|
|
the trashcan mechanism completely: the decremented level means
|
|
that the effective level never reaches the limit. Therefore, we
|
|
*increment* the level *before* entering the trashcan block, and
|
|
matchingly decrement it after leaving. This means the trashcan
|
|
code will trigger a little early, but that's no big deal.
|
|
|
|
Q. Are there any live examples of code in need of all this
|
|
complexity?
|
|
|
|
A. Yes. See SF bug 668433 for code that crashed (when Python was
|
|
compiled in debug mode) before the trashcan level manipulations
|
|
were added. For more discussion, see SF patches 581742, 575073
|
|
and bug 574207.
|
|
*/
|
|
}
|
|
|
|
static PyTypeObject *solid_base(PyTypeObject *type);
|
|
|
|
/* type test with subclassing support */
|
|
|
|
int
|
|
PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
|
|
{
|
|
PyObject *mro;
|
|
|
|
mro = a->tp_mro;
|
|
if (mro != NULL) {
|
|
/* Deal with multiple inheritance without recursion
|
|
by walking the MRO tuple */
|
|
Py_ssize_t i, n;
|
|
assert(PyTuple_Check(mro));
|
|
n = PyTuple_GET_SIZE(mro);
|
|
for (i = 0; i < n; i++) {
|
|
if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
else {
|
|
/* a is not completely initilized yet; follow tp_base */
|
|
do {
|
|
if (a == b)
|
|
return 1;
|
|
a = a->tp_base;
|
|
} while (a != NULL);
|
|
return b == &PyBaseObject_Type;
|
|
}
|
|
}
|
|
|
|
/* Internal routines to do a method lookup in the type
|
|
without looking in the instance dictionary
|
|
(so we can't use PyObject_GetAttr) but still binding
|
|
it to the instance. The arguments are the object,
|
|
the method name as a C string, and the address of a
|
|
static variable used to cache the interned Python string.
|
|
|
|
Two variants:
|
|
|
|
- lookup_maybe() returns NULL without raising an exception
|
|
when the _PyType_Lookup() call fails;
|
|
|
|
- lookup_method() always raises an exception upon errors.
|
|
*/
|
|
|
|
static PyObject *
|
|
lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
|
|
{
|
|
PyObject *res;
|
|
|
|
if (*attrobj == NULL) {
|
|
*attrobj = PyString_InternFromString(attrstr);
|
|
if (*attrobj == NULL)
|
|
return NULL;
|
|
}
|
|
res = _PyType_Lookup(self->ob_type, *attrobj);
|
|
if (res != NULL) {
|
|
descrgetfunc f;
|
|
if ((f = res->ob_type->tp_descr_get) == NULL)
|
|
Py_INCREF(res);
|
|
else
|
|
res = f(res, self, (PyObject *)(self->ob_type));
|
|
}
|
|
return res;
|
|
}
|
|
|
|
static PyObject *
|
|
lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
|
|
{
|
|
PyObject *res = lookup_maybe(self, attrstr, attrobj);
|
|
if (res == NULL && !PyErr_Occurred())
|
|
PyErr_SetObject(PyExc_AttributeError, *attrobj);
|
|
return res;
|
|
}
|
|
|
|
/* A variation of PyObject_CallMethod that uses lookup_method()
|
|
instead of PyObject_GetAttrString(). This uses the same convention
|
|
as lookup_method to cache the interned name string object. */
|
|
|
|
static PyObject *
|
|
call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
|
|
{
|
|
va_list va;
|
|
PyObject *args, *func = 0, *retval;
|
|
va_start(va, format);
|
|
|
|
func = lookup_maybe(o, name, nameobj);
|
|
if (func == NULL) {
|
|
va_end(va);
|
|
if (!PyErr_Occurred())
|
|
PyErr_SetObject(PyExc_AttributeError, *nameobj);
|
|
return NULL;
|
|
}
|
|
|
|
if (format && *format)
|
|
args = Py_VaBuildValue(format, va);
|
|
else
|
|
args = PyTuple_New(0);
|
|
|
|
va_end(va);
|
|
|
|
if (args == NULL)
|
|
return NULL;
|
|
|
|
assert(PyTuple_Check(args));
|
|
retval = PyObject_Call(func, args, NULL);
|
|
|
|
Py_DECREF(args);
|
|
Py_DECREF(func);
|
|
|
|
return retval;
|
|
}
|
|
|
|
/* Clone of call_method() that returns NotImplemented when the lookup fails. */
|
|
|
|
static PyObject *
|
|
call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
|
|
{
|
|
va_list va;
|
|
PyObject *args, *func = 0, *retval;
|
|
va_start(va, format);
|
|
|
|
func = lookup_maybe(o, name, nameobj);
|
|
if (func == NULL) {
|
|
va_end(va);
|
|
if (!PyErr_Occurred()) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
if (format && *format)
|
|
args = Py_VaBuildValue(format, va);
|
|
else
|
|
args = PyTuple_New(0);
|
|
|
|
va_end(va);
|
|
|
|
if (args == NULL)
|
|
return NULL;
|
|
|
|
assert(PyTuple_Check(args));
|
|
retval = PyObject_Call(func, args, NULL);
|
|
|
|
Py_DECREF(args);
|
|
Py_DECREF(func);
|
|
|
|
return retval;
|
|
}
|
|
|
|
/*
|
|
Method resolution order algorithm C3 described in
|
|
"A Monotonic Superclass Linearization for Dylan",
|
|
by Kim Barrett, Bob Cassel, Paul Haahr,
|
|
David A. Moon, Keith Playford, and P. Tucker Withington.
|
|
(OOPSLA 1996)
|
|
|
|
Some notes about the rules implied by C3:
|
|
|
|
No duplicate bases.
|
|
It isn't legal to repeat a class in a list of base classes.
|
|
|
|
The next three properties are the 3 constraints in "C3".
|
|
|
|
Local precendece order.
|
|
If A precedes B in C's MRO, then A will precede B in the MRO of all
|
|
subclasses of C.
|
|
|
|
Monotonicity.
|
|
The MRO of a class must be an extension without reordering of the
|
|
MRO of each of its superclasses.
|
|
|
|
Extended Precedence Graph (EPG).
|
|
Linearization is consistent if there is a path in the EPG from
|
|
each class to all its successors in the linearization. See
|
|
the paper for definition of EPG.
|
|
*/
|
|
|
|
static int
|
|
tail_contains(PyObject *list, int whence, PyObject *o) {
|
|
Py_ssize_t j, size;
|
|
size = PyList_GET_SIZE(list);
|
|
|
|
for (j = whence+1; j < size; j++) {
|
|
if (PyList_GET_ITEM(list, j) == o)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
class_name(PyObject *cls)
|
|
{
|
|
PyObject *name = PyObject_GetAttrString(cls, "__name__");
|
|
if (name == NULL) {
|
|
PyErr_Clear();
|
|
Py_XDECREF(name);
|
|
name = PyObject_Repr(cls);
|
|
}
|
|
if (name == NULL)
|
|
return NULL;
|
|
if (!PyString_Check(name)) {
|
|
Py_DECREF(name);
|
|
return NULL;
|
|
}
|
|
return name;
|
|
}
|
|
|
|
static int
|
|
check_duplicates(PyObject *list)
|
|
{
|
|
Py_ssize_t i, j, n;
|
|
/* Let's use a quadratic time algorithm,
|
|
assuming that the bases lists is short.
|
|
*/
|
|
n = PyList_GET_SIZE(list);
|
|
for (i = 0; i < n; i++) {
|
|
PyObject *o = PyList_GET_ITEM(list, i);
|
|
for (j = i + 1; j < n; j++) {
|
|
if (PyList_GET_ITEM(list, j) == o) {
|
|
o = class_name(o);
|
|
PyErr_Format(PyExc_TypeError,
|
|
"duplicate base class %s",
|
|
o ? PyString_AS_STRING(o) : "?");
|
|
Py_XDECREF(o);
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Raise a TypeError for an MRO order disagreement.
|
|
|
|
It's hard to produce a good error message. In the absence of better
|
|
insight into error reporting, report the classes that were candidates
|
|
to be put next into the MRO. There is some conflict between the
|
|
order in which they should be put in the MRO, but it's hard to
|
|
diagnose what constraint can't be satisfied.
|
|
*/
|
|
|
|
static void
|
|
set_mro_error(PyObject *to_merge, int *remain)
|
|
{
|
|
Py_ssize_t i, n, off, to_merge_size;
|
|
char buf[1000];
|
|
PyObject *k, *v;
|
|
PyObject *set = PyDict_New();
|
|
if (!set) return;
|
|
|
|
to_merge_size = PyList_GET_SIZE(to_merge);
|
|
for (i = 0; i < to_merge_size; i++) {
|
|
PyObject *L = PyList_GET_ITEM(to_merge, i);
|
|
if (remain[i] < PyList_GET_SIZE(L)) {
|
|
PyObject *c = PyList_GET_ITEM(L, remain[i]);
|
|
if (PyDict_SetItem(set, c, Py_None) < 0) {
|
|
Py_DECREF(set);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
n = PyDict_Size(set);
|
|
|
|
off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
|
|
consistent method resolution\norder (MRO) for bases");
|
|
i = 0;
|
|
while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
|
|
PyObject *name = class_name(k);
|
|
off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
|
|
name ? PyString_AS_STRING(name) : "?");
|
|
Py_XDECREF(name);
|
|
if (--n && (size_t)(off+1) < sizeof(buf)) {
|
|
buf[off++] = ',';
|
|
buf[off] = '\0';
|
|
}
|
|
}
|
|
PyErr_SetString(PyExc_TypeError, buf);
|
|
Py_DECREF(set);
|
|
}
|
|
|
|
static int
|
|
pmerge(PyObject *acc, PyObject* to_merge) {
|
|
Py_ssize_t i, j, to_merge_size, empty_cnt;
|
|
int *remain;
|
|
int ok;
|
|
|
|
to_merge_size = PyList_GET_SIZE(to_merge);
|
|
|
|
/* remain stores an index into each sublist of to_merge.
|
|
remain[i] is the index of the next base in to_merge[i]
|
|
that is not included in acc.
|
|
*/
|
|
remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
|
|
if (remain == NULL)
|
|
return -1;
|
|
for (i = 0; i < to_merge_size; i++)
|
|
remain[i] = 0;
|
|
|
|
again:
|
|
empty_cnt = 0;
|
|
for (i = 0; i < to_merge_size; i++) {
|
|
PyObject *candidate;
|
|
|
|
PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
|
|
|
|
if (remain[i] >= PyList_GET_SIZE(cur_list)) {
|
|
empty_cnt++;
|
|
continue;
|
|
}
|
|
|
|
/* Choose next candidate for MRO.
|
|
|
|
The input sequences alone can determine the choice.
|
|
If not, choose the class which appears in the MRO
|
|
of the earliest direct superclass of the new class.
|
|
*/
|
|
|
|
candidate = PyList_GET_ITEM(cur_list, remain[i]);
|
|
for (j = 0; j < to_merge_size; j++) {
|
|
PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
|
|
if (tail_contains(j_lst, remain[j], candidate)) {
|
|
goto skip; /* continue outer loop */
|
|
}
|
|
}
|
|
ok = PyList_Append(acc, candidate);
|
|
if (ok < 0) {
|
|
PyMem_Free(remain);
|
|
return -1;
|
|
}
|
|
for (j = 0; j < to_merge_size; j++) {
|
|
PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
|
|
if (remain[j] < PyList_GET_SIZE(j_lst) &&
|
|
PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
|
|
remain[j]++;
|
|
}
|
|
}
|
|
goto again;
|
|
skip: ;
|
|
}
|
|
|
|
if (empty_cnt == to_merge_size) {
|
|
PyMem_FREE(remain);
|
|
return 0;
|
|
}
|
|
set_mro_error(to_merge, remain);
|
|
PyMem_FREE(remain);
|
|
return -1;
|
|
}
|
|
|
|
static PyObject *
|
|
mro_implementation(PyTypeObject *type)
|
|
{
|
|
Py_ssize_t i, n;
|
|
int ok;
|
|
PyObject *bases, *result;
|
|
PyObject *to_merge, *bases_aslist;
|
|
|
|
if(type->tp_dict == NULL) {
|
|
if(PyType_Ready(type) < 0)
|
|
return NULL;
|
|
}
|
|
|
|
/* Find a superclass linearization that honors the constraints
|
|
of the explicit lists of bases and the constraints implied by
|
|
each base class.
|
|
|
|
to_merge is a list of lists, where each list is a superclass
|
|
linearization implied by a base class. The last element of
|
|
to_merge is the declared list of bases.
|
|
*/
|
|
|
|
bases = type->tp_bases;
|
|
n = PyTuple_GET_SIZE(bases);
|
|
|
|
to_merge = PyList_New(n+1);
|
|
if (to_merge == NULL)
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
PyObject *base = PyTuple_GET_ITEM(bases, i);
|
|
PyObject *parentMRO;
|
|
parentMRO = PySequence_List(((PyTypeObject*)base)->tp_mro);
|
|
if (parentMRO == NULL) {
|
|
Py_DECREF(to_merge);
|
|
return NULL;
|
|
}
|
|
|
|
PyList_SET_ITEM(to_merge, i, parentMRO);
|
|
}
|
|
|
|
bases_aslist = PySequence_List(bases);
|
|
if (bases_aslist == NULL) {
|
|
Py_DECREF(to_merge);
|
|
return NULL;
|
|
}
|
|
/* This is just a basic sanity check. */
|
|
if (check_duplicates(bases_aslist) < 0) {
|
|
Py_DECREF(to_merge);
|
|
Py_DECREF(bases_aslist);
|
|
return NULL;
|
|
}
|
|
PyList_SET_ITEM(to_merge, n, bases_aslist);
|
|
|
|
result = Py_BuildValue("[O]", (PyObject *)type);
|
|
if (result == NULL) {
|
|
Py_DECREF(to_merge);
|
|
return NULL;
|
|
}
|
|
|
|
ok = pmerge(result, to_merge);
|
|
Py_DECREF(to_merge);
|
|
if (ok < 0) {
|
|
Py_DECREF(result);
|
|
return NULL;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static PyObject *
|
|
mro_external(PyObject *self)
|
|
{
|
|
PyTypeObject *type = (PyTypeObject *)self;
|
|
|
|
return mro_implementation(type);
|
|
}
|
|
|
|
static int
|
|
mro_internal(PyTypeObject *type)
|
|
{
|
|
PyObject *mro, *result, *tuple;
|
|
int checkit = 0;
|
|
|
|
if (type->ob_type == &PyType_Type) {
|
|
result = mro_implementation(type);
|
|
}
|
|
else {
|
|
static PyObject *mro_str;
|
|
checkit = 1;
|
|
mro = lookup_method((PyObject *)type, "mro", &mro_str);
|
|
if (mro == NULL)
|
|
return -1;
|
|
result = PyObject_CallObject(mro, NULL);
|
|
Py_DECREF(mro);
|
|
}
|
|
if (result == NULL)
|
|
return -1;
|
|
tuple = PySequence_Tuple(result);
|
|
Py_DECREF(result);
|
|
if (tuple == NULL)
|
|
return -1;
|
|
if (checkit) {
|
|
Py_ssize_t i, len;
|
|
PyObject *cls;
|
|
PyTypeObject *solid;
|
|
|
|
solid = solid_base(type);
|
|
|
|
len = PyTuple_GET_SIZE(tuple);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
PyTypeObject *t;
|
|
cls = PyTuple_GET_ITEM(tuple, i);
|
|
if (!PyType_Check(cls)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"mro() returned a non-class ('%.500s')",
|
|
cls->ob_type->tp_name);
|
|
Py_DECREF(tuple);
|
|
return -1;
|
|
}
|
|
t = (PyTypeObject*)cls;
|
|
if (!PyType_IsSubtype(solid, solid_base(t))) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"mro() returned base with unsuitable layout ('%.500s')",
|
|
t->tp_name);
|
|
Py_DECREF(tuple);
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
type->tp_mro = tuple;
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Calculate the best base amongst multiple base classes.
|
|
This is the first one that's on the path to the "solid base". */
|
|
|
|
static PyTypeObject *
|
|
best_base(PyObject *bases)
|
|
{
|
|
Py_ssize_t i, n;
|
|
PyTypeObject *base, *winner, *candidate, *base_i;
|
|
PyObject *base_proto;
|
|
|
|
assert(PyTuple_Check(bases));
|
|
n = PyTuple_GET_SIZE(bases);
|
|
assert(n > 0);
|
|
base = NULL;
|
|
winner = NULL;
|
|
for (i = 0; i < n; i++) {
|
|
base_proto = PyTuple_GET_ITEM(bases, i);
|
|
if (!PyType_Check(base_proto)) {
|
|
PyErr_SetString(
|
|
PyExc_TypeError,
|
|
"bases must be types");
|
|
return NULL;
|
|
}
|
|
base_i = (PyTypeObject *)base_proto;
|
|
if (base_i->tp_dict == NULL) {
|
|
if (PyType_Ready(base_i) < 0)
|
|
return NULL;
|
|
}
|
|
candidate = solid_base(base_i);
|
|
if (winner == NULL) {
|
|
winner = candidate;
|
|
base = base_i;
|
|
}
|
|
else if (PyType_IsSubtype(winner, candidate))
|
|
;
|
|
else if (PyType_IsSubtype(candidate, winner)) {
|
|
winner = candidate;
|
|
base = base_i;
|
|
}
|
|
else {
|
|
PyErr_SetString(
|
|
PyExc_TypeError,
|
|
"multiple bases have "
|
|
"instance lay-out conflict");
|
|
return NULL;
|
|
}
|
|
}
|
|
if (base == NULL)
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"a new-style class can't have only classic bases");
|
|
return base;
|
|
}
|
|
|
|
static int
|
|
extra_ivars(PyTypeObject *type, PyTypeObject *base)
|
|
{
|
|
size_t t_size = type->tp_basicsize;
|
|
size_t b_size = base->tp_basicsize;
|
|
|
|
assert(t_size >= b_size); /* Else type smaller than base! */
|
|
if (type->tp_itemsize || base->tp_itemsize) {
|
|
/* If itemsize is involved, stricter rules */
|
|
return t_size != b_size ||
|
|
type->tp_itemsize != base->tp_itemsize;
|
|
}
|
|
if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
|
|
type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
|
|
t_size -= sizeof(PyObject *);
|
|
if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
|
|
type->tp_dictoffset + sizeof(PyObject *) == t_size)
|
|
t_size -= sizeof(PyObject *);
|
|
|
|
return t_size != b_size;
|
|
}
|
|
|
|
static PyTypeObject *
|
|
solid_base(PyTypeObject *type)
|
|
{
|
|
PyTypeObject *base;
|
|
|
|
if (type->tp_base)
|
|
base = solid_base(type->tp_base);
|
|
else
|
|
base = &PyBaseObject_Type;
|
|
if (extra_ivars(type, base))
|
|
return type;
|
|
else
|
|
return base;
|
|
}
|
|
|
|
static void object_dealloc(PyObject *);
|
|
static int object_init(PyObject *, PyObject *, PyObject *);
|
|
static int update_slot(PyTypeObject *, PyObject *);
|
|
static void fixup_slot_dispatchers(PyTypeObject *);
|
|
|
|
static PyObject *
|
|
subtype_dict(PyObject *obj, void *context)
|
|
{
|
|
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
|
PyObject *dict;
|
|
|
|
if (dictptr == NULL) {
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
"This object has no __dict__");
|
|
return NULL;
|
|
}
|
|
dict = *dictptr;
|
|
if (dict == NULL)
|
|
*dictptr = dict = PyDict_New();
|
|
Py_XINCREF(dict);
|
|
return dict;
|
|
}
|
|
|
|
static int
|
|
subtype_setdict(PyObject *obj, PyObject *value, void *context)
|
|
{
|
|
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
|
PyObject *dict;
|
|
|
|
if (dictptr == NULL) {
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
"This object has no __dict__");
|
|
return -1;
|
|
}
|
|
if (value != NULL && !PyDict_Check(value)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__dict__ must be set to a dictionary, "
|
|
"not a '%.200s'", value->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
dict = *dictptr;
|
|
Py_XINCREF(value);
|
|
*dictptr = value;
|
|
Py_XDECREF(dict);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
subtype_getweakref(PyObject *obj, void *context)
|
|
{
|
|
PyObject **weaklistptr;
|
|
PyObject *result;
|
|
|
|
if (obj->ob_type->tp_weaklistoffset == 0) {
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
"This object has no __weakref__");
|
|
return NULL;
|
|
}
|
|
assert(obj->ob_type->tp_weaklistoffset > 0);
|
|
assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
|
|
(size_t)(obj->ob_type->tp_basicsize));
|
|
weaklistptr = (PyObject **)
|
|
((char *)obj + obj->ob_type->tp_weaklistoffset);
|
|
if (*weaklistptr == NULL)
|
|
result = Py_None;
|
|
else
|
|
result = *weaklistptr;
|
|
Py_INCREF(result);
|
|
return result;
|
|
}
|
|
|
|
/* Three variants on the subtype_getsets list. */
|
|
|
|
static PyGetSetDef subtype_getsets_full[] = {
|
|
{"__dict__", subtype_dict, subtype_setdict,
|
|
PyDoc_STR("dictionary for instance variables (if defined)")},
|
|
{"__weakref__", subtype_getweakref, NULL,
|
|
PyDoc_STR("list of weak references to the object (if defined)")},
|
|
{0}
|
|
};
|
|
|
|
static PyGetSetDef subtype_getsets_dict_only[] = {
|
|
{"__dict__", subtype_dict, subtype_setdict,
|
|
PyDoc_STR("dictionary for instance variables (if defined)")},
|
|
{0}
|
|
};
|
|
|
|
static PyGetSetDef subtype_getsets_weakref_only[] = {
|
|
{"__weakref__", subtype_getweakref, NULL,
|
|
PyDoc_STR("list of weak references to the object (if defined)")},
|
|
{0}
|
|
};
|
|
|
|
static int
|
|
valid_identifier(PyObject *s)
|
|
{
|
|
unsigned char *p;
|
|
Py_ssize_t i, n;
|
|
|
|
if (!PyString_Check(s)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__slots__ items must be strings, not '%.200s'",
|
|
s->ob_type->tp_name);
|
|
return 0;
|
|
}
|
|
p = (unsigned char *) PyString_AS_STRING(s);
|
|
n = PyString_GET_SIZE(s);
|
|
/* We must reject an empty name. As a hack, we bump the
|
|
length to 1 so that the loop will balk on the trailing \0. */
|
|
if (n == 0)
|
|
n = 1;
|
|
for (i = 0; i < n; i++, p++) {
|
|
if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"__slots__ must be identifiers");
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
#ifdef Py_USING_UNICODE
|
|
/* Replace Unicode objects in slots. */
|
|
|
|
static PyObject *
|
|
_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
|
|
{
|
|
PyObject *tmp = slots;
|
|
PyObject *o, *o1;
|
|
Py_ssize_t i;
|
|
ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
|
|
for (i = 0; i < nslots; i++) {
|
|
if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
|
|
if (tmp == slots) {
|
|
tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
|
|
if (tmp == NULL)
|
|
return NULL;
|
|
}
|
|
o1 = _PyUnicode_AsDefaultEncodedString
|
|
(o, NULL);
|
|
if (o1 == NULL) {
|
|
Py_DECREF(tmp);
|
|
return 0;
|
|
}
|
|
Py_INCREF(o1);
|
|
Py_DECREF(o);
|
|
PyTuple_SET_ITEM(tmp, i, o1);
|
|
}
|
|
}
|
|
return tmp;
|
|
}
|
|
#endif
|
|
|
|
static PyObject *
|
|
type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *name, *bases, *dict;
|
|
static char *kwlist[] = {"name", "bases", "dict", 0};
|
|
PyObject *slots, *tmp, *newslots;
|
|
PyTypeObject *type, *base, *tmptype, *winner;
|
|
PyHeapTypeObject *et;
|
|
PyMemberDef *mp;
|
|
Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
|
|
int j, may_add_dict, may_add_weak;
|
|
|
|
assert(args != NULL && PyTuple_Check(args));
|
|
assert(kwds == NULL || PyDict_Check(kwds));
|
|
|
|
/* Special case: type(x) should return x->ob_type */
|
|
{
|
|
const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
|
|
const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
|
|
|
|
if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
|
|
PyObject *x = PyTuple_GET_ITEM(args, 0);
|
|
Py_INCREF(x->ob_type);
|
|
return (PyObject *) x->ob_type;
|
|
}
|
|
|
|
/* SF bug 475327 -- if that didn't trigger, we need 3
|
|
arguments. but PyArg_ParseTupleAndKeywords below may give
|
|
a msg saying type() needs exactly 3. */
|
|
if (nargs + nkwds != 3) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"type() takes 1 or 3 arguments");
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
/* Check arguments: (name, bases, dict) */
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
|
|
&name,
|
|
&PyTuple_Type, &bases,
|
|
&PyDict_Type, &dict))
|
|
return NULL;
|
|
|
|
/* Determine the proper metatype to deal with this,
|
|
and check for metatype conflicts while we're at it.
|
|
Note that if some other metatype wins to contract,
|
|
it's possible that its instances are not types. */
|
|
nbases = PyTuple_GET_SIZE(bases);
|
|
winner = metatype;
|
|
for (i = 0; i < nbases; i++) {
|
|
tmp = PyTuple_GET_ITEM(bases, i);
|
|
tmptype = tmp->ob_type;
|
|
if (PyType_IsSubtype(winner, tmptype))
|
|
continue;
|
|
if (PyType_IsSubtype(tmptype, winner)) {
|
|
winner = tmptype;
|
|
continue;
|
|
}
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"metaclass conflict: "
|
|
"the metaclass of a derived class "
|
|
"must be a (non-strict) subclass "
|
|
"of the metaclasses of all its bases");
|
|
return NULL;
|
|
}
|
|
if (winner != metatype) {
|
|
if (winner->tp_new != type_new) /* Pass it to the winner */
|
|
return winner->tp_new(winner, args, kwds);
|
|
metatype = winner;
|
|
}
|
|
|
|
/* Adjust for empty tuple bases */
|
|
if (nbases == 0) {
|
|
bases = PyTuple_Pack(1, &PyBaseObject_Type);
|
|
if (bases == NULL)
|
|
return NULL;
|
|
nbases = 1;
|
|
}
|
|
else
|
|
Py_INCREF(bases);
|
|
|
|
/* XXX From here until type is allocated, "return NULL" leaks bases! */
|
|
|
|
/* Calculate best base, and check that all bases are type objects */
|
|
base = best_base(bases);
|
|
if (base == NULL) {
|
|
Py_DECREF(bases);
|
|
return NULL;
|
|
}
|
|
if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"type '%.100s' is not an acceptable base type",
|
|
base->tp_name);
|
|
Py_DECREF(bases);
|
|
return NULL;
|
|
}
|
|
|
|
/* Check for a __slots__ sequence variable in dict, and count it */
|
|
slots = PyDict_GetItemString(dict, "__slots__");
|
|
nslots = 0;
|
|
add_dict = 0;
|
|
add_weak = 0;
|
|
may_add_dict = base->tp_dictoffset == 0;
|
|
may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
|
|
if (slots == NULL) {
|
|
if (may_add_dict) {
|
|
add_dict++;
|
|
}
|
|
if (may_add_weak) {
|
|
add_weak++;
|
|
}
|
|
}
|
|
else {
|
|
/* Have slots */
|
|
|
|
/* Make it into a tuple */
|
|
if (PyString_Check(slots))
|
|
slots = PyTuple_Pack(1, slots);
|
|
else
|
|
slots = PySequence_Tuple(slots);
|
|
if (slots == NULL) {
|
|
Py_DECREF(bases);
|
|
return NULL;
|
|
}
|
|
assert(PyTuple_Check(slots));
|
|
|
|
/* Are slots allowed? */
|
|
nslots = PyTuple_GET_SIZE(slots);
|
|
if (nslots > 0 && base->tp_itemsize != 0) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"nonempty __slots__ "
|
|
"not supported for subtype of '%s'",
|
|
base->tp_name);
|
|
bad_slots:
|
|
Py_DECREF(bases);
|
|
Py_DECREF(slots);
|
|
return NULL;
|
|
}
|
|
|
|
#ifdef Py_USING_UNICODE
|
|
tmp = _unicode_to_string(slots, nslots);
|
|
if (tmp != slots) {
|
|
Py_DECREF(slots);
|
|
slots = tmp;
|
|
}
|
|
if (!tmp)
|
|
return NULL;
|
|
#endif
|
|
/* Check for valid slot names and two special cases */
|
|
for (i = 0; i < nslots; i++) {
|
|
PyObject *tmp = PyTuple_GET_ITEM(slots, i);
|
|
char *s;
|
|
if (!valid_identifier(tmp))
|
|
goto bad_slots;
|
|
assert(PyString_Check(tmp));
|
|
s = PyString_AS_STRING(tmp);
|
|
if (strcmp(s, "__dict__") == 0) {
|
|
if (!may_add_dict || add_dict) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"__dict__ slot disallowed: "
|
|
"we already got one");
|
|
goto bad_slots;
|
|
}
|
|
add_dict++;
|
|
}
|
|
if (strcmp(s, "__weakref__") == 0) {
|
|
if (!may_add_weak || add_weak) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"__weakref__ slot disallowed: "
|
|
"either we already got one, "
|
|
"or __itemsize__ != 0");
|
|
goto bad_slots;
|
|
}
|
|
add_weak++;
|
|
}
|
|
}
|
|
|
|
/* Copy slots into yet another tuple, demangling names */
|
|
newslots = PyTuple_New(nslots - add_dict - add_weak);
|
|
if (newslots == NULL)
|
|
goto bad_slots;
|
|
for (i = j = 0; i < nslots; i++) {
|
|
char *s;
|
|
tmp = PyTuple_GET_ITEM(slots, i);
|
|
s = PyString_AS_STRING(tmp);
|
|
if ((add_dict && strcmp(s, "__dict__") == 0) ||
|
|
(add_weak && strcmp(s, "__weakref__") == 0))
|
|
continue;
|
|
tmp =_Py_Mangle(name, tmp);
|
|
if (!tmp)
|
|
goto bad_slots;
|
|
PyTuple_SET_ITEM(newslots, j, tmp);
|
|
j++;
|
|
}
|
|
assert(j == nslots - add_dict - add_weak);
|
|
nslots = j;
|
|
Py_DECREF(slots);
|
|
slots = newslots;
|
|
|
|
/* Secondary bases may provide weakrefs or dict */
|
|
if (nbases > 1 &&
|
|
((may_add_dict && !add_dict) ||
|
|
(may_add_weak && !add_weak))) {
|
|
for (i = 0; i < nbases; i++) {
|
|
tmp = PyTuple_GET_ITEM(bases, i);
|
|
if (tmp == (PyObject *)base)
|
|
continue; /* Skip primary base */
|
|
assert(PyType_Check(tmp));
|
|
tmptype = (PyTypeObject *)tmp;
|
|
if (may_add_dict && !add_dict &&
|
|
tmptype->tp_dictoffset != 0)
|
|
add_dict++;
|
|
if (may_add_weak && !add_weak &&
|
|
tmptype->tp_weaklistoffset != 0)
|
|
add_weak++;
|
|
if (may_add_dict && !add_dict)
|
|
continue;
|
|
if (may_add_weak && !add_weak)
|
|
continue;
|
|
/* Nothing more to check */
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* XXX From here until type is safely allocated,
|
|
"return NULL" may leak slots! */
|
|
|
|
/* Allocate the type object */
|
|
type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
|
|
if (type == NULL) {
|
|
Py_XDECREF(slots);
|
|
Py_DECREF(bases);
|
|
return NULL;
|
|
}
|
|
|
|
/* Keep name and slots alive in the extended type object */
|
|
et = (PyHeapTypeObject *)type;
|
|
Py_INCREF(name);
|
|
et->ht_name = name;
|
|
et->ht_slots = slots;
|
|
|
|
/* Initialize tp_flags */
|
|
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
|
|
Py_TPFLAGS_BASETYPE;
|
|
if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
|
|
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
|
|
|
|
/* Initialize essential fields */
|
|
type->tp_as_number = &et->as_number;
|
|
type->tp_as_sequence = &et->as_sequence;
|
|
type->tp_as_mapping = &et->as_mapping;
|
|
type->tp_as_buffer = &et->as_buffer;
|
|
type->tp_name = PyString_AS_STRING(name);
|
|
|
|
/* Set tp_base and tp_bases */
|
|
type->tp_bases = bases;
|
|
Py_INCREF(base);
|
|
type->tp_base = base;
|
|
|
|
/* Initialize tp_dict from passed-in dict */
|
|
type->tp_dict = dict = PyDict_Copy(dict);
|
|
if (dict == NULL) {
|
|
Py_DECREF(type);
|
|
return NULL;
|
|
}
|
|
|
|
/* Set __module__ in the dict */
|
|
if (PyDict_GetItemString(dict, "__module__") == NULL) {
|
|
tmp = PyEval_GetGlobals();
|
|
if (tmp != NULL) {
|
|
tmp = PyDict_GetItemString(tmp, "__name__");
|
|
if (tmp != NULL) {
|
|
if (PyDict_SetItemString(dict, "__module__",
|
|
tmp) < 0)
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Set tp_doc to a copy of dict['__doc__'], if the latter is there
|
|
and is a string. The __doc__ accessor will first look for tp_doc;
|
|
if that fails, it will still look into __dict__.
|
|
*/
|
|
{
|
|
PyObject *doc = PyDict_GetItemString(dict, "__doc__");
|
|
if (doc != NULL && PyString_Check(doc)) {
|
|
const size_t n = (size_t)PyString_GET_SIZE(doc);
|
|
char *tp_doc = (char *)PyObject_MALLOC(n+1);
|
|
if (tp_doc == NULL) {
|
|
Py_DECREF(type);
|
|
return NULL;
|
|
}
|
|
memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
|
|
type->tp_doc = tp_doc;
|
|
}
|
|
}
|
|
|
|
/* Special-case __new__: if it's a plain function,
|
|
make it a static function */
|
|
tmp = PyDict_GetItemString(dict, "__new__");
|
|
if (tmp != NULL && PyFunction_Check(tmp)) {
|
|
tmp = PyStaticMethod_New(tmp);
|
|
if (tmp == NULL) {
|
|
Py_DECREF(type);
|
|
return NULL;
|
|
}
|
|
PyDict_SetItemString(dict, "__new__", tmp);
|
|
Py_DECREF(tmp);
|
|
}
|
|
|
|
/* Add descriptors for custom slots from __slots__, or for __dict__ */
|
|
mp = PyHeapType_GET_MEMBERS(et);
|
|
slotoffset = base->tp_basicsize;
|
|
if (slots != NULL) {
|
|
for (i = 0; i < nslots; i++, mp++) {
|
|
mp->name = PyString_AS_STRING(
|
|
PyTuple_GET_ITEM(slots, i));
|
|
mp->type = T_OBJECT_EX;
|
|
mp->offset = slotoffset;
|
|
if (base->tp_weaklistoffset == 0 &&
|
|
strcmp(mp->name, "__weakref__") == 0) {
|
|
add_weak++;
|
|
mp->type = T_OBJECT;
|
|
mp->flags = READONLY;
|
|
type->tp_weaklistoffset = slotoffset;
|
|
}
|
|
slotoffset += sizeof(PyObject *);
|
|
}
|
|
}
|
|
if (add_dict) {
|
|
if (base->tp_itemsize)
|
|
type->tp_dictoffset = -(long)sizeof(PyObject *);
|
|
else
|
|
type->tp_dictoffset = slotoffset;
|
|
slotoffset += sizeof(PyObject *);
|
|
}
|
|
if (add_weak) {
|
|
assert(!base->tp_itemsize);
|
|
type->tp_weaklistoffset = slotoffset;
|
|
slotoffset += sizeof(PyObject *);
|
|
}
|
|
type->tp_basicsize = slotoffset;
|
|
type->tp_itemsize = base->tp_itemsize;
|
|
type->tp_members = PyHeapType_GET_MEMBERS(et);
|
|
|
|
if (type->tp_weaklistoffset && type->tp_dictoffset)
|
|
type->tp_getset = subtype_getsets_full;
|
|
else if (type->tp_weaklistoffset && !type->tp_dictoffset)
|
|
type->tp_getset = subtype_getsets_weakref_only;
|
|
else if (!type->tp_weaklistoffset && type->tp_dictoffset)
|
|
type->tp_getset = subtype_getsets_dict_only;
|
|
else
|
|
type->tp_getset = NULL;
|
|
|
|
/* Special case some slots */
|
|
if (type->tp_dictoffset != 0 || nslots > 0) {
|
|
if (base->tp_getattr == NULL && base->tp_getattro == NULL)
|
|
type->tp_getattro = PyObject_GenericGetAttr;
|
|
if (base->tp_setattr == NULL && base->tp_setattro == NULL)
|
|
type->tp_setattro = PyObject_GenericSetAttr;
|
|
}
|
|
type->tp_dealloc = subtype_dealloc;
|
|
|
|
/* Enable GC unless there are really no instance variables possible */
|
|
if (!(type->tp_basicsize == sizeof(PyObject) &&
|
|
type->tp_itemsize == 0))
|
|
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
|
|
|
|
/* Always override allocation strategy to use regular heap */
|
|
type->tp_alloc = PyType_GenericAlloc;
|
|
if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
|
|
type->tp_free = PyObject_GC_Del;
|
|
type->tp_traverse = subtype_traverse;
|
|
type->tp_clear = subtype_clear;
|
|
}
|
|
else
|
|
type->tp_free = PyObject_Del;
|
|
|
|
/* Initialize the rest */
|
|
if (PyType_Ready(type) < 0) {
|
|
Py_DECREF(type);
|
|
return NULL;
|
|
}
|
|
|
|
/* Put the proper slots in place */
|
|
fixup_slot_dispatchers(type);
|
|
|
|
return (PyObject *)type;
|
|
}
|
|
|
|
/* Internal API to look for a name through the MRO.
|
|
This returns a borrowed reference, and doesn't set an exception! */
|
|
PyObject *
|
|
_PyType_Lookup(PyTypeObject *type, PyObject *name)
|
|
{
|
|
Py_ssize_t i, n;
|
|
PyObject *mro, *res, *base, *dict;
|
|
|
|
/* Look in tp_dict of types in MRO */
|
|
mro = type->tp_mro;
|
|
|
|
/* If mro is NULL, the type is either not yet initialized
|
|
by PyType_Ready(), or already cleared by type_clear().
|
|
Either way the safest thing to do is to return NULL. */
|
|
if (mro == NULL)
|
|
return NULL;
|
|
|
|
assert(PyTuple_Check(mro));
|
|
n = PyTuple_GET_SIZE(mro);
|
|
for (i = 0; i < n; i++) {
|
|
base = PyTuple_GET_ITEM(mro, i);
|
|
assert(PyType_Check(base));
|
|
dict = ((PyTypeObject *)base)->tp_dict;
|
|
assert(dict && PyDict_Check(dict));
|
|
res = PyDict_GetItem(dict, name);
|
|
if (res != NULL)
|
|
return res;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* This is similar to PyObject_GenericGetAttr(),
|
|
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
|
|
static PyObject *
|
|
type_getattro(PyTypeObject *type, PyObject *name)
|
|
{
|
|
PyTypeObject *metatype = type->ob_type;
|
|
PyObject *meta_attribute, *attribute;
|
|
descrgetfunc meta_get;
|
|
|
|
/* Initialize this type (we'll assume the metatype is initialized) */
|
|
if (type->tp_dict == NULL) {
|
|
if (PyType_Ready(type) < 0)
|
|
return NULL;
|
|
}
|
|
|
|
/* No readable descriptor found yet */
|
|
meta_get = NULL;
|
|
|
|
/* Look for the attribute in the metatype */
|
|
meta_attribute = _PyType_Lookup(metatype, name);
|
|
|
|
if (meta_attribute != NULL) {
|
|
meta_get = meta_attribute->ob_type->tp_descr_get;
|
|
|
|
if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
|
|
/* Data descriptors implement tp_descr_set to intercept
|
|
* writes. Assume the attribute is not overridden in
|
|
* type's tp_dict (and bases): call the descriptor now.
|
|
*/
|
|
return meta_get(meta_attribute, (PyObject *)type,
|
|
(PyObject *)metatype);
|
|
}
|
|
Py_INCREF(meta_attribute);
|
|
}
|
|
|
|
/* No data descriptor found on metatype. Look in tp_dict of this
|
|
* type and its bases */
|
|
attribute = _PyType_Lookup(type, name);
|
|
if (attribute != NULL) {
|
|
/* Implement descriptor functionality, if any */
|
|
descrgetfunc local_get = attribute->ob_type->tp_descr_get;
|
|
|
|
Py_XDECREF(meta_attribute);
|
|
|
|
if (local_get != NULL) {
|
|
/* NULL 2nd argument indicates the descriptor was
|
|
* found on the target object itself (or a base) */
|
|
return local_get(attribute, (PyObject *)NULL,
|
|
(PyObject *)type);
|
|
}
|
|
|
|
Py_INCREF(attribute);
|
|
return attribute;
|
|
}
|
|
|
|
/* No attribute found in local __dict__ (or bases): use the
|
|
* descriptor from the metatype, if any */
|
|
if (meta_get != NULL) {
|
|
PyObject *res;
|
|
res = meta_get(meta_attribute, (PyObject *)type,
|
|
(PyObject *)metatype);
|
|
Py_DECREF(meta_attribute);
|
|
return res;
|
|
}
|
|
|
|
/* If an ordinary attribute was found on the metatype, return it now */
|
|
if (meta_attribute != NULL) {
|
|
return meta_attribute;
|
|
}
|
|
|
|
/* Give up */
|
|
PyErr_Format(PyExc_AttributeError,
|
|
"type object '%.50s' has no attribute '%.400s'",
|
|
type->tp_name, PyString_AS_STRING(name));
|
|
return NULL;
|
|
}
|
|
|
|
static int
|
|
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
|
|
{
|
|
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"can't set attributes of built-in/extension type '%s'",
|
|
type->tp_name);
|
|
return -1;
|
|
}
|
|
/* XXX Example of how I expect this to be used...
|
|
if (update_subclasses(type, name, invalidate_cache, NULL) < 0)
|
|
return -1;
|
|
*/
|
|
if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
|
|
return -1;
|
|
return update_slot(type, name);
|
|
}
|
|
|
|
static void
|
|
type_dealloc(PyTypeObject *type)
|
|
{
|
|
PyHeapTypeObject *et;
|
|
|
|
/* Assert this is a heap-allocated type object */
|
|
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
|
|
_PyObject_GC_UNTRACK(type);
|
|
PyObject_ClearWeakRefs((PyObject *)type);
|
|
et = (PyHeapTypeObject *)type;
|
|
Py_XDECREF(type->tp_base);
|
|
Py_XDECREF(type->tp_dict);
|
|
Py_XDECREF(type->tp_bases);
|
|
Py_XDECREF(type->tp_mro);
|
|
Py_XDECREF(type->tp_cache);
|
|
Py_XDECREF(type->tp_subclasses);
|
|
/* A type's tp_doc is heap allocated, unlike the tp_doc slots
|
|
* of most other objects. It's okay to cast it to char *.
|
|
*/
|
|
PyObject_Free((char *)type->tp_doc);
|
|
Py_XDECREF(et->ht_name);
|
|
Py_XDECREF(et->ht_slots);
|
|
type->ob_type->tp_free((PyObject *)type);
|
|
}
|
|
|
|
static PyObject *
|
|
type_subclasses(PyTypeObject *type, PyObject *args_ignored)
|
|
{
|
|
PyObject *list, *raw, *ref;
|
|
Py_ssize_t i, n;
|
|
|
|
list = PyList_New(0);
|
|
if (list == NULL)
|
|
return NULL;
|
|
raw = type->tp_subclasses;
|
|
if (raw == NULL)
|
|
return list;
|
|
assert(PyList_Check(raw));
|
|
n = PyList_GET_SIZE(raw);
|
|
for (i = 0; i < n; i++) {
|
|
ref = PyList_GET_ITEM(raw, i);
|
|
assert(PyWeakref_CheckRef(ref));
|
|
ref = PyWeakref_GET_OBJECT(ref);
|
|
if (ref != Py_None) {
|
|
if (PyList_Append(list, ref) < 0) {
|
|
Py_DECREF(list);
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
static PyMethodDef type_methods[] = {
|
|
{"mro", (PyCFunction)mro_external, METH_NOARGS,
|
|
PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
|
|
{"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
|
|
PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
|
|
{0}
|
|
};
|
|
|
|
PyDoc_STRVAR(type_doc,
|
|
"type(object) -> the object's type\n"
|
|
"type(name, bases, dict) -> a new type");
|
|
|
|
static int
|
|
type_traverse(PyTypeObject *type, visitproc visit, void *arg)
|
|
{
|
|
/* Because of type_is_gc(), the collector only calls this
|
|
for heaptypes. */
|
|
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
|
|
|
|
Py_VISIT(type->tp_dict);
|
|
Py_VISIT(type->tp_cache);
|
|
Py_VISIT(type->tp_mro);
|
|
Py_VISIT(type->tp_bases);
|
|
Py_VISIT(type->tp_base);
|
|
|
|
/* There's no need to visit type->tp_subclasses or
|
|
((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
|
|
in cycles; tp_subclasses is a list of weak references,
|
|
and slots is a tuple of strings. */
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
type_clear(PyTypeObject *type)
|
|
{
|
|
/* Because of type_is_gc(), the collector only calls this
|
|
for heaptypes. */
|
|
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
|
|
|
|
/* The only field we need to clear is tp_mro, which is part of a
|
|
hard cycle (its first element is the class itself) that won't
|
|
be broken otherwise (it's a tuple and tuples don't have a
|
|
tp_clear handler). None of the other fields need to be
|
|
cleared, and here's why:
|
|
|
|
tp_dict:
|
|
It is a dict, so the collector will call its tp_clear.
|
|
|
|
tp_cache:
|
|
Not used; if it were, it would be a dict.
|
|
|
|
tp_bases, tp_base:
|
|
If these are involved in a cycle, there must be at least
|
|
one other, mutable object in the cycle, e.g. a base
|
|
class's dict; the cycle will be broken that way.
|
|
|
|
tp_subclasses:
|
|
A list of weak references can't be part of a cycle; and
|
|
lists have their own tp_clear.
|
|
|
|
slots (in PyHeapTypeObject):
|
|
A tuple of strings can't be part of a cycle.
|
|
*/
|
|
|
|
Py_CLEAR(type->tp_mro);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
type_is_gc(PyTypeObject *type)
|
|
{
|
|
return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
|
|
}
|
|
|
|
PyTypeObject PyType_Type = {
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
0, /* ob_size */
|
|
"type", /* tp_name */
|
|
sizeof(PyHeapTypeObject), /* tp_basicsize */
|
|
sizeof(PyMemberDef), /* tp_itemsize */
|
|
(destructor)type_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
(reprfunc)type_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
0, /* tp_hash */
|
|
(ternaryfunc)type_call, /* tp_call */
|
|
0, /* tp_str */
|
|
(getattrofunc)type_getattro, /* tp_getattro */
|
|
(setattrofunc)type_setattro, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
type_doc, /* tp_doc */
|
|
(traverseproc)type_traverse, /* tp_traverse */
|
|
(inquiry)type_clear, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
type_methods, /* tp_methods */
|
|
type_members, /* tp_members */
|
|
type_getsets, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
|
|
0, /* tp_init */
|
|
0, /* tp_alloc */
|
|
type_new, /* tp_new */
|
|
PyObject_GC_Del, /* tp_free */
|
|
(inquiry)type_is_gc, /* tp_is_gc */
|
|
};
|
|
|
|
|
|
/* The base type of all types (eventually)... except itself. */
|
|
|
|
static int
|
|
object_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
/* If we don't have a tp_new for a new-style class, new will use this one.
|
|
Therefore this should take no arguments/keywords. However, this new may
|
|
also be inherited by objects that define a tp_init but no tp_new. These
|
|
objects WILL pass argumets to tp_new, because it gets the same args as
|
|
tp_init. So only allow arguments if we aren't using the default init, in
|
|
which case we expect init to handle argument parsing. */
|
|
static PyObject *
|
|
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
if (type->tp_init == object_init && (PyTuple_GET_SIZE(args) ||
|
|
(kwds && PyDict_Check(kwds) && PyDict_Size(kwds)))) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"default __new__ takes no parameters");
|
|
return NULL;
|
|
}
|
|
return type->tp_alloc(type, 0);
|
|
}
|
|
|
|
static void
|
|
object_dealloc(PyObject *self)
|
|
{
|
|
self->ob_type->tp_free(self);
|
|
}
|
|
|
|
static PyObject *
|
|
object_repr(PyObject *self)
|
|
{
|
|
PyTypeObject *type;
|
|
PyObject *mod, *name, *rtn;
|
|
|
|
type = self->ob_type;
|
|
mod = type_module(type, NULL);
|
|
if (mod == NULL)
|
|
PyErr_Clear();
|
|
else if (!PyString_Check(mod)) {
|
|
Py_DECREF(mod);
|
|
mod = NULL;
|
|
}
|
|
name = type_name(type, NULL);
|
|
if (name == NULL)
|
|
return NULL;
|
|
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
|
|
rtn = PyString_FromFormat("<%s.%s object at %p>",
|
|
PyString_AS_STRING(mod),
|
|
PyString_AS_STRING(name),
|
|
self);
|
|
else
|
|
rtn = PyString_FromFormat("<%s object at %p>",
|
|
type->tp_name, self);
|
|
Py_XDECREF(mod);
|
|
Py_DECREF(name);
|
|
return rtn;
|
|
}
|
|
|
|
static PyObject *
|
|
object_str(PyObject *self)
|
|
{
|
|
unaryfunc f;
|
|
|
|
f = self->ob_type->tp_repr;
|
|
if (f == NULL)
|
|
f = object_repr;
|
|
return f(self);
|
|
}
|
|
|
|
static PyObject *
|
|
object_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
PyObject *res;
|
|
|
|
switch (op) {
|
|
|
|
case Py_EQ:
|
|
res = (self == other) ? Py_True : Py_False;
|
|
break;
|
|
|
|
case Py_NE:
|
|
res = (self != other) ? Py_True : Py_False;
|
|
break;
|
|
|
|
default:
|
|
res = Py_NotImplemented;
|
|
break;
|
|
}
|
|
|
|
Py_INCREF(res);
|
|
return res;
|
|
}
|
|
|
|
static PyObject *
|
|
object_get_class(PyObject *self, void *closure)
|
|
{
|
|
Py_INCREF(self->ob_type);
|
|
return (PyObject *)(self->ob_type);
|
|
}
|
|
|
|
static int
|
|
equiv_structs(PyTypeObject *a, PyTypeObject *b)
|
|
{
|
|
return a == b ||
|
|
(a != NULL &&
|
|
b != NULL &&
|
|
a->tp_basicsize == b->tp_basicsize &&
|
|
a->tp_itemsize == b->tp_itemsize &&
|
|
a->tp_dictoffset == b->tp_dictoffset &&
|
|
a->tp_weaklistoffset == b->tp_weaklistoffset &&
|
|
((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
|
|
(b->tp_flags & Py_TPFLAGS_HAVE_GC)));
|
|
}
|
|
|
|
static int
|
|
same_slots_added(PyTypeObject *a, PyTypeObject *b)
|
|
{
|
|
PyTypeObject *base = a->tp_base;
|
|
Py_ssize_t size;
|
|
|
|
if (base != b->tp_base)
|
|
return 0;
|
|
if (equiv_structs(a, base) && equiv_structs(b, base))
|
|
return 1;
|
|
size = base->tp_basicsize;
|
|
if (a->tp_dictoffset == size && b->tp_dictoffset == size)
|
|
size += sizeof(PyObject *);
|
|
if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
|
|
size += sizeof(PyObject *);
|
|
return size == a->tp_basicsize && size == b->tp_basicsize;
|
|
}
|
|
|
|
static int
|
|
compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
|
|
{
|
|
PyTypeObject *newbase, *oldbase;
|
|
|
|
if (newto->tp_dealloc != oldto->tp_dealloc ||
|
|
newto->tp_free != oldto->tp_free)
|
|
{
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s assignment: "
|
|
"'%s' deallocator differs from '%s'",
|
|
attr,
|
|
newto->tp_name,
|
|
oldto->tp_name);
|
|
return 0;
|
|
}
|
|
newbase = newto;
|
|
oldbase = oldto;
|
|
while (equiv_structs(newbase, newbase->tp_base))
|
|
newbase = newbase->tp_base;
|
|
while (equiv_structs(oldbase, oldbase->tp_base))
|
|
oldbase = oldbase->tp_base;
|
|
if (newbase != oldbase &&
|
|
(newbase->tp_base != oldbase->tp_base ||
|
|
!same_slots_added(newbase, oldbase))) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s assignment: "
|
|
"'%s' object layout differs from '%s'",
|
|
attr,
|
|
newto->tp_name,
|
|
oldto->tp_name);
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
object_set_class(PyObject *self, PyObject *value, void *closure)
|
|
{
|
|
PyTypeObject *oldto = self->ob_type;
|
|
PyTypeObject *newto;
|
|
|
|
if (value == NULL) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"can't delete __class__ attribute");
|
|
return -1;
|
|
}
|
|
if (!PyType_Check(value)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__class__ must be set to new-style class, not '%s' object",
|
|
value->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
newto = (PyTypeObject *)value;
|
|
if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
|
|
!(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
|
{
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__class__ assignment: only for heap types");
|
|
return -1;
|
|
}
|
|
if (compatible_for_assignment(newto, oldto, "__class__")) {
|
|
Py_INCREF(newto);
|
|
self->ob_type = newto;
|
|
Py_DECREF(oldto);
|
|
return 0;
|
|
}
|
|
else {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
static PyGetSetDef object_getsets[] = {
|
|
{"__class__", object_get_class, object_set_class,
|
|
PyDoc_STR("the object's class")},
|
|
{0}
|
|
};
|
|
|
|
|
|
/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
|
|
We fall back to helpers in copy_reg for:
|
|
- pickle protocols < 2
|
|
- calculating the list of slot names (done only once per class)
|
|
- the __newobj__ function (which is used as a token but never called)
|
|
*/
|
|
|
|
static PyObject *
|
|
import_copy_reg(void)
|
|
{
|
|
static PyObject *copy_reg_str;
|
|
|
|
if (!copy_reg_str) {
|
|
copy_reg_str = PyString_InternFromString("copy_reg");
|
|
if (copy_reg_str == NULL)
|
|
return NULL;
|
|
}
|
|
|
|
return PyImport_Import(copy_reg_str);
|
|
}
|
|
|
|
static PyObject *
|
|
slotnames(PyObject *cls)
|
|
{
|
|
PyObject *clsdict;
|
|
PyObject *copy_reg;
|
|
PyObject *slotnames;
|
|
|
|
if (!PyType_Check(cls)) {
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
clsdict = ((PyTypeObject *)cls)->tp_dict;
|
|
slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
|
|
if (slotnames != NULL && PyList_Check(slotnames)) {
|
|
Py_INCREF(slotnames);
|
|
return slotnames;
|
|
}
|
|
|
|
copy_reg = import_copy_reg();
|
|
if (copy_reg == NULL)
|
|
return NULL;
|
|
|
|
slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
|
|
Py_DECREF(copy_reg);
|
|
if (slotnames != NULL &&
|
|
slotnames != Py_None &&
|
|
!PyList_Check(slotnames))
|
|
{
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"copy_reg._slotnames didn't return a list or None");
|
|
Py_DECREF(slotnames);
|
|
slotnames = NULL;
|
|
}
|
|
|
|
return slotnames;
|
|
}
|
|
|
|
static PyObject *
|
|
reduce_2(PyObject *obj)
|
|
{
|
|
PyObject *cls, *getnewargs;
|
|
PyObject *args = NULL, *args2 = NULL;
|
|
PyObject *getstate = NULL, *state = NULL, *names = NULL;
|
|
PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
|
|
PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
|
|
Py_ssize_t i, n;
|
|
|
|
cls = PyObject_GetAttrString(obj, "__class__");
|
|
if (cls == NULL)
|
|
return NULL;
|
|
|
|
getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
|
|
if (getnewargs != NULL) {
|
|
args = PyObject_CallObject(getnewargs, NULL);
|
|
Py_DECREF(getnewargs);
|
|
if (args != NULL && !PyTuple_Check(args)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__getnewargs__ should return a tuple, "
|
|
"not '%.200s'", args->ob_type->tp_name);
|
|
goto end;
|
|
}
|
|
}
|
|
else {
|
|
PyErr_Clear();
|
|
args = PyTuple_New(0);
|
|
}
|
|
if (args == NULL)
|
|
goto end;
|
|
|
|
getstate = PyObject_GetAttrString(obj, "__getstate__");
|
|
if (getstate != NULL) {
|
|
state = PyObject_CallObject(getstate, NULL);
|
|
Py_DECREF(getstate);
|
|
if (state == NULL)
|
|
goto end;
|
|
}
|
|
else {
|
|
PyErr_Clear();
|
|
state = PyObject_GetAttrString(obj, "__dict__");
|
|
if (state == NULL) {
|
|
PyErr_Clear();
|
|
state = Py_None;
|
|
Py_INCREF(state);
|
|
}
|
|
names = slotnames(cls);
|
|
if (names == NULL)
|
|
goto end;
|
|
if (names != Py_None) {
|
|
assert(PyList_Check(names));
|
|
slots = PyDict_New();
|
|
if (slots == NULL)
|
|
goto end;
|
|
n = 0;
|
|
/* Can't pre-compute the list size; the list
|
|
is stored on the class so accessible to other
|
|
threads, which may be run by DECREF */
|
|
for (i = 0; i < PyList_GET_SIZE(names); i++) {
|
|
PyObject *name, *value;
|
|
name = PyList_GET_ITEM(names, i);
|
|
value = PyObject_GetAttr(obj, name);
|
|
if (value == NULL)
|
|
PyErr_Clear();
|
|
else {
|
|
int err = PyDict_SetItem(slots, name,
|
|
value);
|
|
Py_DECREF(value);
|
|
if (err)
|
|
goto end;
|
|
n++;
|
|
}
|
|
}
|
|
if (n) {
|
|
state = Py_BuildValue("(NO)", state, slots);
|
|
if (state == NULL)
|
|
goto end;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!PyList_Check(obj)) {
|
|
listitems = Py_None;
|
|
Py_INCREF(listitems);
|
|
}
|
|
else {
|
|
listitems = PyObject_GetIter(obj);
|
|
if (listitems == NULL)
|
|
goto end;
|
|
}
|
|
|
|
if (!PyDict_Check(obj)) {
|
|
dictitems = Py_None;
|
|
Py_INCREF(dictitems);
|
|
}
|
|
else {
|
|
dictitems = PyObject_CallMethod(obj, "iteritems", "");
|
|
if (dictitems == NULL)
|
|
goto end;
|
|
}
|
|
|
|
copy_reg = import_copy_reg();
|
|
if (copy_reg == NULL)
|
|
goto end;
|
|
newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
|
|
if (newobj == NULL)
|
|
goto end;
|
|
|
|
n = PyTuple_GET_SIZE(args);
|
|
args2 = PyTuple_New(n+1);
|
|
if (args2 == NULL)
|
|
goto end;
|
|
PyTuple_SET_ITEM(args2, 0, cls);
|
|
cls = NULL;
|
|
for (i = 0; i < n; i++) {
|
|
PyObject *v = PyTuple_GET_ITEM(args, i);
|
|
Py_INCREF(v);
|
|
PyTuple_SET_ITEM(args2, i+1, v);
|
|
}
|
|
|
|
res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
|
|
|
|
end:
|
|
Py_XDECREF(cls);
|
|
Py_XDECREF(args);
|
|
Py_XDECREF(args2);
|
|
Py_XDECREF(slots);
|
|
Py_XDECREF(state);
|
|
Py_XDECREF(names);
|
|
Py_XDECREF(listitems);
|
|
Py_XDECREF(dictitems);
|
|
Py_XDECREF(copy_reg);
|
|
Py_XDECREF(newobj);
|
|
return res;
|
|
}
|
|
|
|
static PyObject *
|
|
object_reduce_ex(PyObject *self, PyObject *args)
|
|
{
|
|
/* Call copy_reg._reduce_ex(self, proto) */
|
|
PyObject *reduce, *copy_reg, *res;
|
|
int proto = 0;
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
|
|
return NULL;
|
|
|
|
reduce = PyObject_GetAttrString(self, "__reduce__");
|
|
if (reduce == NULL)
|
|
PyErr_Clear();
|
|
else {
|
|
PyObject *cls, *clsreduce, *objreduce;
|
|
int override;
|
|
cls = PyObject_GetAttrString(self, "__class__");
|
|
if (cls == NULL) {
|
|
Py_DECREF(reduce);
|
|
return NULL;
|
|
}
|
|
clsreduce = PyObject_GetAttrString(cls, "__reduce__");
|
|
Py_DECREF(cls);
|
|
if (clsreduce == NULL) {
|
|
Py_DECREF(reduce);
|
|
return NULL;
|
|
}
|
|
objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
|
|
"__reduce__");
|
|
override = (clsreduce != objreduce);
|
|
Py_DECREF(clsreduce);
|
|
if (override) {
|
|
res = PyObject_CallObject(reduce, NULL);
|
|
Py_DECREF(reduce);
|
|
return res;
|
|
}
|
|
else
|
|
Py_DECREF(reduce);
|
|
}
|
|
|
|
if (proto >= 2)
|
|
return reduce_2(self);
|
|
|
|
copy_reg = import_copy_reg();
|
|
if (!copy_reg)
|
|
return NULL;
|
|
|
|
res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
|
|
Py_DECREF(copy_reg);
|
|
|
|
return res;
|
|
}
|
|
|
|
static PyMethodDef object_methods[] = {
|
|
{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
|
|
PyDoc_STR("helper for pickle")},
|
|
{"__reduce__", object_reduce_ex, METH_VARARGS,
|
|
PyDoc_STR("helper for pickle")},
|
|
{0}
|
|
};
|
|
|
|
|
|
PyTypeObject PyBaseObject_Type = {
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
0, /* ob_size */
|
|
"object", /* tp_name */
|
|
sizeof(PyObject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
object_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
object_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
(hashfunc)_Py_HashPointer, /* tp_hash */
|
|
0, /* tp_call */
|
|
object_str, /* tp_str */
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
PyDoc_STR("The most base type"), /* tp_doc */
|
|
0, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
object_richcompare, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
object_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
object_getsets, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
object_init, /* tp_init */
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
object_new, /* tp_new */
|
|
PyObject_Del, /* tp_free */
|
|
};
|
|
|
|
|
|
/* Add the methods from tp_methods to the __dict__ in a type object */
|
|
|
|
static int
|
|
add_methods(PyTypeObject *type, PyMethodDef *meth)
|
|
{
|
|
PyObject *dict = type->tp_dict;
|
|
|
|
for (; meth->ml_name != NULL; meth++) {
|
|
PyObject *descr;
|
|
if (PyDict_GetItemString(dict, meth->ml_name) &&
|
|
!(meth->ml_flags & METH_COEXIST))
|
|
continue;
|
|
if (meth->ml_flags & METH_CLASS) {
|
|
if (meth->ml_flags & METH_STATIC) {
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"method cannot be both class and static");
|
|
return -1;
|
|
}
|
|
descr = PyDescr_NewClassMethod(type, meth);
|
|
}
|
|
else if (meth->ml_flags & METH_STATIC) {
|
|
PyObject *cfunc = PyCFunction_New(meth, NULL);
|
|
if (cfunc == NULL)
|
|
return -1;
|
|
descr = PyStaticMethod_New(cfunc);
|
|
Py_DECREF(cfunc);
|
|
}
|
|
else {
|
|
descr = PyDescr_NewMethod(type, meth);
|
|
}
|
|
if (descr == NULL)
|
|
return -1;
|
|
if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
|
|
return -1;
|
|
Py_DECREF(descr);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
add_members(PyTypeObject *type, PyMemberDef *memb)
|
|
{
|
|
PyObject *dict = type->tp_dict;
|
|
|
|
for (; memb->name != NULL; memb++) {
|
|
PyObject *descr;
|
|
if (PyDict_GetItemString(dict, memb->name))
|
|
continue;
|
|
descr = PyDescr_NewMember(type, memb);
|
|
if (descr == NULL)
|
|
return -1;
|
|
if (PyDict_SetItemString(dict, memb->name, descr) < 0)
|
|
return -1;
|
|
Py_DECREF(descr);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
add_getset(PyTypeObject *type, PyGetSetDef *gsp)
|
|
{
|
|
PyObject *dict = type->tp_dict;
|
|
|
|
for (; gsp->name != NULL; gsp++) {
|
|
PyObject *descr;
|
|
if (PyDict_GetItemString(dict, gsp->name))
|
|
continue;
|
|
descr = PyDescr_NewGetSet(type, gsp);
|
|
|
|
if (descr == NULL)
|
|
return -1;
|
|
if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
|
|
return -1;
|
|
Py_DECREF(descr);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
inherit_special(PyTypeObject *type, PyTypeObject *base)
|
|
{
|
|
Py_ssize_t oldsize, newsize;
|
|
|
|
/* Copying basicsize is connected to the GC flags */
|
|
oldsize = base->tp_basicsize;
|
|
newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
|
|
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
|
|
(base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
|
|
(!type->tp_traverse && !type->tp_clear)) {
|
|
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
|
|
if (type->tp_traverse == NULL)
|
|
type->tp_traverse = base->tp_traverse;
|
|
if (type->tp_clear == NULL)
|
|
type->tp_clear = base->tp_clear;
|
|
}
|
|
{
|
|
/* The condition below could use some explanation.
|
|
It appears that tp_new is not inherited for static types
|
|
whose base class is 'object'; this seems to be a precaution
|
|
so that old extension types don't suddenly become
|
|
callable (object.__new__ wouldn't insure the invariants
|
|
that the extension type's own factory function ensures).
|
|
Heap types, of course, are under our control, so they do
|
|
inherit tp_new; static extension types that specify some
|
|
other built-in type as the default are considered
|
|
new-style-aware so they also inherit object.__new__. */
|
|
if (base != &PyBaseObject_Type ||
|
|
(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
|
|
if (type->tp_new == NULL)
|
|
type->tp_new = base->tp_new;
|
|
}
|
|
}
|
|
type->tp_basicsize = newsize;
|
|
|
|
/* Copy other non-function slots */
|
|
|
|
#undef COPYVAL
|
|
#define COPYVAL(SLOT) \
|
|
if (type->SLOT == 0) type->SLOT = base->SLOT
|
|
|
|
COPYVAL(tp_itemsize);
|
|
COPYVAL(tp_weaklistoffset);
|
|
COPYVAL(tp_dictoffset);
|
|
}
|
|
|
|
/* Map rich comparison operators to their __xx__ namesakes */
|
|
static char *name_op[] = {
|
|
"__lt__",
|
|
"__le__",
|
|
"__eq__",
|
|
"__ne__",
|
|
"__gt__",
|
|
"__ge__",
|
|
/* These are only for overrides_cmp_or_hash(): */
|
|
"__cmp__",
|
|
"__hash__",
|
|
};
|
|
|
|
static int
|
|
overrides_cmp_or_hash(PyTypeObject *type)
|
|
{
|
|
int i;
|
|
PyObject *dict = type->tp_dict;
|
|
|
|
assert(dict != NULL);
|
|
for (i = 0; i < 8; i++) {
|
|
if (PyDict_GetItemString(dict, name_op[i]) != NULL)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
inherit_slots(PyTypeObject *type, PyTypeObject *base)
|
|
{
|
|
PyTypeObject *basebase;
|
|
|
|
#undef SLOTDEFINED
|
|
#undef COPYSLOT
|
|
#undef COPYNUM
|
|
#undef COPYSEQ
|
|
#undef COPYMAP
|
|
#undef COPYBUF
|
|
|
|
#define SLOTDEFINED(SLOT) \
|
|
(base->SLOT != 0 && \
|
|
(basebase == NULL || base->SLOT != basebase->SLOT))
|
|
|
|
#define COPYSLOT(SLOT) \
|
|
if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
|
|
|
|
#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
|
|
#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
|
|
#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
|
|
#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
|
|
|
|
/* This won't inherit indirect slots (from tp_as_number etc.)
|
|
if type doesn't provide the space. */
|
|
|
|
if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
|
|
basebase = base->tp_base;
|
|
if (basebase->tp_as_number == NULL)
|
|
basebase = NULL;
|
|
COPYNUM(nb_add);
|
|
COPYNUM(nb_subtract);
|
|
COPYNUM(nb_multiply);
|
|
COPYNUM(nb_remainder);
|
|
COPYNUM(nb_divmod);
|
|
COPYNUM(nb_power);
|
|
COPYNUM(nb_negative);
|
|
COPYNUM(nb_positive);
|
|
COPYNUM(nb_absolute);
|
|
COPYNUM(nb_bool);
|
|
COPYNUM(nb_invert);
|
|
COPYNUM(nb_lshift);
|
|
COPYNUM(nb_rshift);
|
|
COPYNUM(nb_and);
|
|
COPYNUM(nb_xor);
|
|
COPYNUM(nb_or);
|
|
COPYNUM(nb_int);
|
|
COPYNUM(nb_long);
|
|
COPYNUM(nb_float);
|
|
COPYNUM(nb_oct);
|
|
COPYNUM(nb_hex);
|
|
COPYNUM(nb_inplace_add);
|
|
COPYNUM(nb_inplace_subtract);
|
|
COPYNUM(nb_inplace_multiply);
|
|
COPYNUM(nb_inplace_remainder);
|
|
COPYNUM(nb_inplace_power);
|
|
COPYNUM(nb_inplace_lshift);
|
|
COPYNUM(nb_inplace_rshift);
|
|
COPYNUM(nb_inplace_and);
|
|
COPYNUM(nb_inplace_xor);
|
|
COPYNUM(nb_inplace_or);
|
|
COPYNUM(nb_true_divide);
|
|
COPYNUM(nb_floor_divide);
|
|
COPYNUM(nb_inplace_true_divide);
|
|
COPYNUM(nb_inplace_floor_divide);
|
|
COPYNUM(nb_index);
|
|
}
|
|
|
|
if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
|
|
basebase = base->tp_base;
|
|
if (basebase->tp_as_sequence == NULL)
|
|
basebase = NULL;
|
|
COPYSEQ(sq_length);
|
|
COPYSEQ(sq_concat);
|
|
COPYSEQ(sq_repeat);
|
|
COPYSEQ(sq_item);
|
|
COPYSEQ(sq_slice);
|
|
COPYSEQ(sq_ass_item);
|
|
COPYSEQ(sq_ass_slice);
|
|
COPYSEQ(sq_contains);
|
|
COPYSEQ(sq_inplace_concat);
|
|
COPYSEQ(sq_inplace_repeat);
|
|
}
|
|
|
|
if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
|
|
basebase = base->tp_base;
|
|
if (basebase->tp_as_mapping == NULL)
|
|
basebase = NULL;
|
|
COPYMAP(mp_length);
|
|
COPYMAP(mp_subscript);
|
|
COPYMAP(mp_ass_subscript);
|
|
}
|
|
|
|
if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
|
|
basebase = base->tp_base;
|
|
if (basebase->tp_as_buffer == NULL)
|
|
basebase = NULL;
|
|
COPYBUF(bf_getreadbuffer);
|
|
COPYBUF(bf_getwritebuffer);
|
|
COPYBUF(bf_getsegcount);
|
|
COPYBUF(bf_getcharbuffer);
|
|
}
|
|
|
|
basebase = base->tp_base;
|
|
|
|
COPYSLOT(tp_dealloc);
|
|
COPYSLOT(tp_print);
|
|
if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
|
|
type->tp_getattr = base->tp_getattr;
|
|
type->tp_getattro = base->tp_getattro;
|
|
}
|
|
if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
|
|
type->tp_setattr = base->tp_setattr;
|
|
type->tp_setattro = base->tp_setattro;
|
|
}
|
|
/* tp_compare see tp_richcompare */
|
|
COPYSLOT(tp_repr);
|
|
/* tp_hash see tp_richcompare */
|
|
COPYSLOT(tp_call);
|
|
COPYSLOT(tp_str);
|
|
{
|
|
/* Copy comparison-related slots only when
|
|
not overriding them anywhere */
|
|
if (type->tp_compare == NULL &&
|
|
type->tp_richcompare == NULL &&
|
|
type->tp_hash == NULL &&
|
|
!overrides_cmp_or_hash(type))
|
|
{
|
|
type->tp_compare = base->tp_compare;
|
|
type->tp_richcompare = base->tp_richcompare;
|
|
type->tp_hash = base->tp_hash;
|
|
}
|
|
}
|
|
{
|
|
COPYSLOT(tp_iter);
|
|
COPYSLOT(tp_iternext);
|
|
}
|
|
{
|
|
COPYSLOT(tp_descr_get);
|
|
COPYSLOT(tp_descr_set);
|
|
COPYSLOT(tp_dictoffset);
|
|
COPYSLOT(tp_init);
|
|
COPYSLOT(tp_alloc);
|
|
COPYSLOT(tp_is_gc);
|
|
if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
|
|
(base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
|
|
/* They agree about gc. */
|
|
COPYSLOT(tp_free);
|
|
}
|
|
else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
|
|
type->tp_free == NULL &&
|
|
base->tp_free == _PyObject_Del) {
|
|
/* A bit of magic to plug in the correct default
|
|
* tp_free function when a derived class adds gc,
|
|
* didn't define tp_free, and the base uses the
|
|
* default non-gc tp_free.
|
|
*/
|
|
type->tp_free = PyObject_GC_Del;
|
|
}
|
|
/* else they didn't agree about gc, and there isn't something
|
|
* obvious to be done -- the type is on its own.
|
|
*/
|
|
}
|
|
}
|
|
|
|
static int add_operators(PyTypeObject *);
|
|
|
|
int
|
|
PyType_Ready(PyTypeObject *type)
|
|
{
|
|
PyObject *dict, *bases;
|
|
PyTypeObject *base;
|
|
Py_ssize_t i, n;
|
|
|
|
if (type->tp_flags & Py_TPFLAGS_READY) {
|
|
assert(type->tp_dict != NULL);
|
|
return 0;
|
|
}
|
|
assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
|
|
|
|
type->tp_flags |= Py_TPFLAGS_READYING;
|
|
|
|
#ifdef Py_TRACE_REFS
|
|
/* PyType_Ready is the closest thing we have to a choke point
|
|
* for type objects, so is the best place I can think of to try
|
|
* to get type objects into the doubly-linked list of all objects.
|
|
* Still, not all type objects go thru PyType_Ready.
|
|
*/
|
|
_Py_AddToAllObjects((PyObject *)type, 0);
|
|
#endif
|
|
|
|
/* Initialize tp_base (defaults to BaseObject unless that's us) */
|
|
base = type->tp_base;
|
|
if (base == NULL && type != &PyBaseObject_Type) {
|
|
base = type->tp_base = &PyBaseObject_Type;
|
|
Py_INCREF(base);
|
|
}
|
|
|
|
/* Now the only way base can still be NULL is if type is
|
|
* &PyBaseObject_Type.
|
|
*/
|
|
|
|
/* Initialize the base class */
|
|
if (base != NULL && base->tp_dict == NULL) {
|
|
if (PyType_Ready(base) < 0)
|
|
goto error;
|
|
}
|
|
|
|
/* Initialize ob_type if NULL. This means extensions that want to be
|
|
compilable separately on Windows can call PyType_Ready() instead of
|
|
initializing the ob_type field of their type objects. */
|
|
/* The test for base != NULL is really unnecessary, since base is only
|
|
NULL when type is &PyBaseObject_Type, and we know its ob_type is
|
|
not NULL (it's initialized to &PyType_Type). But coverity doesn't
|
|
know that. */
|
|
if (type->ob_type == NULL && base != NULL)
|
|
type->ob_type = base->ob_type;
|
|
|
|
/* Initialize tp_bases */
|
|
bases = type->tp_bases;
|
|
if (bases == NULL) {
|
|
if (base == NULL)
|
|
bases = PyTuple_New(0);
|
|
else
|
|
bases = PyTuple_Pack(1, base);
|
|
if (bases == NULL)
|
|
goto error;
|
|
type->tp_bases = bases;
|
|
}
|
|
|
|
/* Initialize tp_dict */
|
|
dict = type->tp_dict;
|
|
if (dict == NULL) {
|
|
dict = PyDict_New();
|
|
if (dict == NULL)
|
|
goto error;
|
|
type->tp_dict = dict;
|
|
}
|
|
|
|
/* Add type-specific descriptors to tp_dict */
|
|
if (add_operators(type) < 0)
|
|
goto error;
|
|
if (type->tp_methods != NULL) {
|
|
if (add_methods(type, type->tp_methods) < 0)
|
|
goto error;
|
|
}
|
|
if (type->tp_members != NULL) {
|
|
if (add_members(type, type->tp_members) < 0)
|
|
goto error;
|
|
}
|
|
if (type->tp_getset != NULL) {
|
|
if (add_getset(type, type->tp_getset) < 0)
|
|
goto error;
|
|
}
|
|
|
|
/* Calculate method resolution order */
|
|
if (mro_internal(type) < 0) {
|
|
goto error;
|
|
}
|
|
|
|
/* Inherit special flags from dominant base */
|
|
if (type->tp_base != NULL)
|
|
inherit_special(type, type->tp_base);
|
|
|
|
/* Initialize tp_dict properly */
|
|
bases = type->tp_mro;
|
|
assert(bases != NULL);
|
|
assert(PyTuple_Check(bases));
|
|
n = PyTuple_GET_SIZE(bases);
|
|
for (i = 1; i < n; i++) {
|
|
PyObject *b = PyTuple_GET_ITEM(bases, i);
|
|
if (PyType_Check(b))
|
|
inherit_slots(type, (PyTypeObject *)b);
|
|
}
|
|
|
|
/* Sanity check for tp_free. */
|
|
if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
|
|
(type->tp_free == NULL || type->tp_free == PyObject_Del)) {
|
|
/* This base class needs to call tp_free, but doesn't have
|
|
* one, or its tp_free is for non-gc'ed objects.
|
|
*/
|
|
PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
|
|
"gc and is a base type but has inappropriate "
|
|
"tp_free slot",
|
|
type->tp_name);
|
|
goto error;
|
|
}
|
|
|
|
/* if the type dictionary doesn't contain a __doc__, set it from
|
|
the tp_doc slot.
|
|
*/
|
|
if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
|
|
if (type->tp_doc != NULL) {
|
|
PyObject *doc = PyString_FromString(type->tp_doc);
|
|
if (doc == NULL)
|
|
goto error;
|
|
PyDict_SetItemString(type->tp_dict, "__doc__", doc);
|
|
Py_DECREF(doc);
|
|
} else {
|
|
PyDict_SetItemString(type->tp_dict,
|
|
"__doc__", Py_None);
|
|
}
|
|
}
|
|
|
|
/* Hack for tp_hash and __hash__.
|
|
If after all that, tp_hash is still NULL, and __hash__ is not in
|
|
tp_dict, set tp_dict['__hash__'] equal to None.
|
|
This signals that __hash__ is not inherited.
|
|
*/
|
|
if (type->tp_hash == NULL) {
|
|
if (PyDict_GetItemString(type->tp_dict, "__hash__") == NULL) {
|
|
if (PyDict_SetItemString(type->tp_dict, "__hash__", Py_None) < 0)
|
|
goto error;
|
|
}
|
|
}
|
|
|
|
/* Some more special stuff */
|
|
base = type->tp_base;
|
|
if (base != NULL) {
|
|
if (type->tp_as_number == NULL)
|
|
type->tp_as_number = base->tp_as_number;
|
|
if (type->tp_as_sequence == NULL)
|
|
type->tp_as_sequence = base->tp_as_sequence;
|
|
if (type->tp_as_mapping == NULL)
|
|
type->tp_as_mapping = base->tp_as_mapping;
|
|
if (type->tp_as_buffer == NULL)
|
|
type->tp_as_buffer = base->tp_as_buffer;
|
|
}
|
|
|
|
/* Link into each base class's list of subclasses */
|
|
bases = type->tp_bases;
|
|
n = PyTuple_GET_SIZE(bases);
|
|
for (i = 0; i < n; i++) {
|
|
PyObject *b = PyTuple_GET_ITEM(bases, i);
|
|
if (PyType_Check(b) &&
|
|
add_subclass((PyTypeObject *)b, type) < 0)
|
|
goto error;
|
|
}
|
|
|
|
/* All done -- set the ready flag */
|
|
assert(type->tp_dict != NULL);
|
|
type->tp_flags =
|
|
(type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
|
|
return 0;
|
|
|
|
error:
|
|
type->tp_flags &= ~Py_TPFLAGS_READYING;
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
add_subclass(PyTypeObject *base, PyTypeObject *type)
|
|
{
|
|
Py_ssize_t i;
|
|
int result;
|
|
PyObject *list, *ref, *newobj;
|
|
|
|
list = base->tp_subclasses;
|
|
if (list == NULL) {
|
|
base->tp_subclasses = list = PyList_New(0);
|
|
if (list == NULL)
|
|
return -1;
|
|
}
|
|
assert(PyList_Check(list));
|
|
newobj = PyWeakref_NewRef((PyObject *)type, NULL);
|
|
i = PyList_GET_SIZE(list);
|
|
while (--i >= 0) {
|
|
ref = PyList_GET_ITEM(list, i);
|
|
assert(PyWeakref_CheckRef(ref));
|
|
if (PyWeakref_GET_OBJECT(ref) == Py_None)
|
|
return PyList_SetItem(list, i, newobj);
|
|
}
|
|
result = PyList_Append(list, newobj);
|
|
Py_DECREF(newobj);
|
|
return result;
|
|
}
|
|
|
|
static void
|
|
remove_subclass(PyTypeObject *base, PyTypeObject *type)
|
|
{
|
|
Py_ssize_t i;
|
|
PyObject *list, *ref;
|
|
|
|
list = base->tp_subclasses;
|
|
if (list == NULL) {
|
|
return;
|
|
}
|
|
assert(PyList_Check(list));
|
|
i = PyList_GET_SIZE(list);
|
|
while (--i >= 0) {
|
|
ref = PyList_GET_ITEM(list, i);
|
|
assert(PyWeakref_CheckRef(ref));
|
|
if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
|
|
/* this can't fail, right? */
|
|
PySequence_DelItem(list, i);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int
|
|
check_num_args(PyObject *ob, int n)
|
|
{
|
|
if (!PyTuple_CheckExact(ob)) {
|
|
PyErr_SetString(PyExc_SystemError,
|
|
"PyArg_UnpackTuple() argument list is not a tuple");
|
|
return 0;
|
|
}
|
|
if (n == PyTuple_GET_SIZE(ob))
|
|
return 1;
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
|
|
return 0;
|
|
}
|
|
|
|
/* Generic wrappers for overloadable 'operators' such as __getitem__ */
|
|
|
|
/* There's a wrapper *function* for each distinct function typedef used
|
|
for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
|
|
wrapper *table* for each distinct operation (e.g. __len__, __add__).
|
|
Most tables have only one entry; the tables for binary operators have two
|
|
entries, one regular and one with reversed arguments. */
|
|
|
|
static PyObject *
|
|
wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
lenfunc func = (lenfunc)wrapped;
|
|
Py_ssize_t res;
|
|
|
|
if (!check_num_args(args, 0))
|
|
return NULL;
|
|
res = (*func)(self);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyInt_FromLong((long)res);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
inquiry func = (inquiry)wrapped;
|
|
int res;
|
|
|
|
if (!check_num_args(args, 0))
|
|
return NULL;
|
|
res = (*func)(self);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyBool_FromLong((long)res);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
binaryfunc func = (binaryfunc)wrapped;
|
|
PyObject *other;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
other = PyTuple_GET_ITEM(args, 0);
|
|
return (*func)(self, other);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
binaryfunc func = (binaryfunc)wrapped;
|
|
PyObject *other;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
other = PyTuple_GET_ITEM(args, 0);
|
|
return (*func)(self, other);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
binaryfunc func = (binaryfunc)wrapped;
|
|
PyObject *other;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
other = PyTuple_GET_ITEM(args, 0);
|
|
if (!PyType_IsSubtype(other->ob_type, self->ob_type)) {
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
return (*func)(other, self);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ternaryfunc func = (ternaryfunc)wrapped;
|
|
PyObject *other;
|
|
PyObject *third = Py_None;
|
|
|
|
/* Note: This wrapper only works for __pow__() */
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
|
|
return NULL;
|
|
return (*func)(self, other, third);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ternaryfunc func = (ternaryfunc)wrapped;
|
|
PyObject *other;
|
|
PyObject *third = Py_None;
|
|
|
|
/* Note: This wrapper only works for __pow__() */
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
|
|
return NULL;
|
|
return (*func)(other, self, third);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
unaryfunc func = (unaryfunc)wrapped;
|
|
|
|
if (!check_num_args(args, 0))
|
|
return NULL;
|
|
return (*func)(self);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizeargfunc func = (ssizeargfunc)wrapped;
|
|
PyObject* o;
|
|
Py_ssize_t i;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
|
|
return NULL;
|
|
i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
|
|
if (i == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
return (*func)(self, i);
|
|
}
|
|
|
|
static Py_ssize_t
|
|
getindex(PyObject *self, PyObject *arg)
|
|
{
|
|
Py_ssize_t i;
|
|
|
|
i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
|
if (i == -1 && PyErr_Occurred())
|
|
return -1;
|
|
if (i < 0) {
|
|
PySequenceMethods *sq = self->ob_type->tp_as_sequence;
|
|
if (sq && sq->sq_length) {
|
|
Py_ssize_t n = (*sq->sq_length)(self);
|
|
if (n < 0)
|
|
return -1;
|
|
i += n;
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizeargfunc func = (ssizeargfunc)wrapped;
|
|
PyObject *arg;
|
|
Py_ssize_t i;
|
|
|
|
if (PyTuple_GET_SIZE(args) == 1) {
|
|
arg = PyTuple_GET_ITEM(args, 0);
|
|
i = getindex(self, arg);
|
|
if (i == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
return (*func)(self, i);
|
|
}
|
|
check_num_args(args, 1);
|
|
assert(PyErr_Occurred());
|
|
return NULL;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
|
|
Py_ssize_t i, j;
|
|
|
|
if (!PyArg_ParseTuple(args, "nn", &i, &j))
|
|
return NULL;
|
|
return (*func)(self, i, j);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizeobjargproc func = (ssizeobjargproc)wrapped;
|
|
Py_ssize_t i;
|
|
int res;
|
|
PyObject *arg, *value;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
|
|
return NULL;
|
|
i = getindex(self, arg);
|
|
if (i == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
res = (*func)(self, i, value);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizeobjargproc func = (ssizeobjargproc)wrapped;
|
|
Py_ssize_t i;
|
|
int res;
|
|
PyObject *arg;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
arg = PyTuple_GET_ITEM(args, 0);
|
|
i = getindex(self, arg);
|
|
if (i == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
res = (*func)(self, i, NULL);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
|
|
Py_ssize_t i, j;
|
|
int res;
|
|
PyObject *value;
|
|
|
|
if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
|
|
return NULL;
|
|
res = (*func)(self, i, j, value);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
|
|
Py_ssize_t i, j;
|
|
int res;
|
|
|
|
if (!PyArg_ParseTuple(args, "nn", &i, &j))
|
|
return NULL;
|
|
res = (*func)(self, i, j, NULL);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
/* XXX objobjproc is a misnomer; should be objargpred */
|
|
static PyObject *
|
|
wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
objobjproc func = (objobjproc)wrapped;
|
|
int res;
|
|
PyObject *value;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
value = PyTuple_GET_ITEM(args, 0);
|
|
res = (*func)(self, value);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
else
|
|
return PyBool_FromLong(res);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
objobjargproc func = (objobjargproc)wrapped;
|
|
int res;
|
|
PyObject *key, *value;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
|
|
return NULL;
|
|
res = (*func)(self, key, value);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
objobjargproc func = (objobjargproc)wrapped;
|
|
int res;
|
|
PyObject *key;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
key = PyTuple_GET_ITEM(args, 0);
|
|
res = (*func)(self, key, NULL);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
cmpfunc func = (cmpfunc)wrapped;
|
|
int res;
|
|
PyObject *other;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
other = PyTuple_GET_ITEM(args, 0);
|
|
if (other->ob_type->tp_compare != func &&
|
|
!PyType_IsSubtype(other->ob_type, self->ob_type)) {
|
|
PyErr_Format(
|
|
PyExc_TypeError,
|
|
"%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
|
|
self->ob_type->tp_name,
|
|
self->ob_type->tp_name,
|
|
other->ob_type->tp_name);
|
|
return NULL;
|
|
}
|
|
res = (*func)(self, other);
|
|
if (PyErr_Occurred())
|
|
return NULL;
|
|
return PyInt_FromLong((long)res);
|
|
}
|
|
|
|
/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
|
|
This is called the Carlo Verre hack after its discoverer. */
|
|
static int
|
|
hackcheck(PyObject *self, setattrofunc func, char *what)
|
|
{
|
|
PyTypeObject *type = self->ob_type;
|
|
while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
|
|
type = type->tp_base;
|
|
/* If type is NULL now, this is a really weird type.
|
|
In the spirit of backwards compatibility (?), just shut up. */
|
|
if (type && type->tp_setattro != func) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"can't apply this %s to %s object",
|
|
what,
|
|
type->tp_name);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
setattrofunc func = (setattrofunc)wrapped;
|
|
int res;
|
|
PyObject *name, *value;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
|
|
return NULL;
|
|
if (!hackcheck(self, func, "__setattr__"))
|
|
return NULL;
|
|
res = (*func)(self, name, value);
|
|
if (res < 0)
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
setattrofunc func = (setattrofunc)wrapped;
|
|
int res;
|
|
PyObject *name;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
name = PyTuple_GET_ITEM(args, 0);
|
|
if (!hackcheck(self, func, "__delattr__"))
|
|
return NULL;
|
|
res = (*func)(self, name, NULL);
|
|
if (res < 0)
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
hashfunc func = (hashfunc)wrapped;
|
|
long res;
|
|
|
|
if (!check_num_args(args, 0))
|
|
return NULL;
|
|
res = (*func)(self);
|
|
if (res == -1 && PyErr_Occurred())
|
|
return NULL;
|
|
return PyInt_FromLong(res);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
|
|
{
|
|
ternaryfunc func = (ternaryfunc)wrapped;
|
|
|
|
return (*func)(self, args, kwds);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
|
|
{
|
|
richcmpfunc func = (richcmpfunc)wrapped;
|
|
PyObject *other;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
other = PyTuple_GET_ITEM(args, 0);
|
|
return (*func)(self, other, op);
|
|
}
|
|
|
|
#undef RICHCMP_WRAPPER
|
|
#define RICHCMP_WRAPPER(NAME, OP) \
|
|
static PyObject * \
|
|
richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
|
|
{ \
|
|
return wrap_richcmpfunc(self, args, wrapped, OP); \
|
|
}
|
|
|
|
RICHCMP_WRAPPER(lt, Py_LT)
|
|
RICHCMP_WRAPPER(le, Py_LE)
|
|
RICHCMP_WRAPPER(eq, Py_EQ)
|
|
RICHCMP_WRAPPER(ne, Py_NE)
|
|
RICHCMP_WRAPPER(gt, Py_GT)
|
|
RICHCMP_WRAPPER(ge, Py_GE)
|
|
|
|
static PyObject *
|
|
wrap_next(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
unaryfunc func = (unaryfunc)wrapped;
|
|
PyObject *res;
|
|
|
|
if (!check_num_args(args, 0))
|
|
return NULL;
|
|
res = (*func)(self);
|
|
if (res == NULL && !PyErr_Occurred())
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
return res;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
descrgetfunc func = (descrgetfunc)wrapped;
|
|
PyObject *obj;
|
|
PyObject *type = NULL;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
|
|
return NULL;
|
|
if (obj == Py_None)
|
|
obj = NULL;
|
|
if (type == Py_None)
|
|
type = NULL;
|
|
if (type == NULL &&obj == NULL) {
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"__get__(None, None) is invalid");
|
|
return NULL;
|
|
}
|
|
return (*func)(self, obj, type);
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
descrsetfunc func = (descrsetfunc)wrapped;
|
|
PyObject *obj, *value;
|
|
int ret;
|
|
|
|
if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
|
|
return NULL;
|
|
ret = (*func)(self, obj, value);
|
|
if (ret < 0)
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
|
|
{
|
|
descrsetfunc func = (descrsetfunc)wrapped;
|
|
PyObject *obj;
|
|
int ret;
|
|
|
|
if (!check_num_args(args, 1))
|
|
return NULL;
|
|
obj = PyTuple_GET_ITEM(args, 0);
|
|
ret = (*func)(self, obj, NULL);
|
|
if (ret < 0)
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
|
|
{
|
|
initproc func = (initproc)wrapped;
|
|
|
|
if (func(self, args, kwds) < 0)
|
|
return NULL;
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
|
|
static PyObject *
|
|
tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyTypeObject *type, *subtype, *staticbase;
|
|
PyObject *arg0, *res;
|
|
|
|
if (self == NULL || !PyType_Check(self))
|
|
Py_FatalError("__new__() called with non-type 'self'");
|
|
type = (PyTypeObject *)self;
|
|
if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s.__new__(): not enough arguments",
|
|
type->tp_name);
|
|
return NULL;
|
|
}
|
|
arg0 = PyTuple_GET_ITEM(args, 0);
|
|
if (!PyType_Check(arg0)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s.__new__(X): X is not a type object (%s)",
|
|
type->tp_name,
|
|
arg0->ob_type->tp_name);
|
|
return NULL;
|
|
}
|
|
subtype = (PyTypeObject *)arg0;
|
|
if (!PyType_IsSubtype(subtype, type)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s.__new__(%s): %s is not a subtype of %s",
|
|
type->tp_name,
|
|
subtype->tp_name,
|
|
subtype->tp_name,
|
|
type->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
/* Check that the use doesn't do something silly and unsafe like
|
|
object.__new__(dict). To do this, we check that the
|
|
most derived base that's not a heap type is this type. */
|
|
staticbase = subtype;
|
|
while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
|
staticbase = staticbase->tp_base;
|
|
/* If staticbase is NULL now, it is a really weird type.
|
|
In the spirit of backwards compatibility (?), just shut up. */
|
|
if (staticbase && staticbase->tp_new != type->tp_new) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%s.__new__(%s) is not safe, use %s.__new__()",
|
|
type->tp_name,
|
|
subtype->tp_name,
|
|
staticbase == NULL ? "?" : staticbase->tp_name);
|
|
return NULL;
|
|
}
|
|
|
|
args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
|
|
if (args == NULL)
|
|
return NULL;
|
|
res = type->tp_new(subtype, args, kwds);
|
|
Py_DECREF(args);
|
|
return res;
|
|
}
|
|
|
|
static struct PyMethodDef tp_new_methoddef[] = {
|
|
{"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
|
|
PyDoc_STR("T.__new__(S, ...) -> "
|
|
"a new object with type S, a subtype of T")},
|
|
{0}
|
|
};
|
|
|
|
static int
|
|
add_tp_new_wrapper(PyTypeObject *type)
|
|
{
|
|
PyObject *func;
|
|
|
|
if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
|
|
return 0;
|
|
func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
|
|
if (func == NULL)
|
|
return -1;
|
|
if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
|
|
Py_DECREF(func);
|
|
return -1;
|
|
}
|
|
Py_DECREF(func);
|
|
return 0;
|
|
}
|
|
|
|
/* Slot wrappers that call the corresponding __foo__ slot. See comments
|
|
below at override_slots() for more explanation. */
|
|
|
|
#define SLOT0(FUNCNAME, OPSTR) \
|
|
static PyObject * \
|
|
FUNCNAME(PyObject *self) \
|
|
{ \
|
|
static PyObject *cache_str; \
|
|
return call_method(self, OPSTR, &cache_str, "()"); \
|
|
}
|
|
|
|
#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
|
|
static PyObject * \
|
|
FUNCNAME(PyObject *self, ARG1TYPE arg1) \
|
|
{ \
|
|
static PyObject *cache_str; \
|
|
return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
|
|
}
|
|
|
|
/* Boolean helper for SLOT1BINFULL().
|
|
right.__class__ is a nontrivial subclass of left.__class__. */
|
|
static int
|
|
method_is_overloaded(PyObject *left, PyObject *right, char *name)
|
|
{
|
|
PyObject *a, *b;
|
|
int ok;
|
|
|
|
b = PyObject_GetAttrString((PyObject *)(right->ob_type), name);
|
|
if (b == NULL) {
|
|
PyErr_Clear();
|
|
/* If right doesn't have it, it's not overloaded */
|
|
return 0;
|
|
}
|
|
|
|
a = PyObject_GetAttrString((PyObject *)(left->ob_type), name);
|
|
if (a == NULL) {
|
|
PyErr_Clear();
|
|
Py_DECREF(b);
|
|
/* If right has it but left doesn't, it's overloaded */
|
|
return 1;
|
|
}
|
|
|
|
ok = PyObject_RichCompareBool(a, b, Py_NE);
|
|
Py_DECREF(a);
|
|
Py_DECREF(b);
|
|
if (ok < 0) {
|
|
PyErr_Clear();
|
|
return 0;
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
|
|
|
|
#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
|
|
static PyObject * \
|
|
FUNCNAME(PyObject *self, PyObject *other) \
|
|
{ \
|
|
static PyObject *cache_str, *rcache_str; \
|
|
int do_other = self->ob_type != other->ob_type && \
|
|
other->ob_type->tp_as_number != NULL && \
|
|
other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
|
|
if (self->ob_type->tp_as_number != NULL && \
|
|
self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
|
|
PyObject *r; \
|
|
if (do_other && \
|
|
PyType_IsSubtype(other->ob_type, self->ob_type) && \
|
|
method_is_overloaded(self, other, ROPSTR)) { \
|
|
r = call_maybe( \
|
|
other, ROPSTR, &rcache_str, "(O)", self); \
|
|
if (r != Py_NotImplemented) \
|
|
return r; \
|
|
Py_DECREF(r); \
|
|
do_other = 0; \
|
|
} \
|
|
r = call_maybe( \
|
|
self, OPSTR, &cache_str, "(O)", other); \
|
|
if (r != Py_NotImplemented || \
|
|
other->ob_type == self->ob_type) \
|
|
return r; \
|
|
Py_DECREF(r); \
|
|
} \
|
|
if (do_other) { \
|
|
return call_maybe( \
|
|
other, ROPSTR, &rcache_str, "(O)", self); \
|
|
} \
|
|
Py_INCREF(Py_NotImplemented); \
|
|
return Py_NotImplemented; \
|
|
}
|
|
|
|
#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
|
|
SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
|
|
|
|
#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
|
|
static PyObject * \
|
|
FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
|
|
{ \
|
|
static PyObject *cache_str; \
|
|
return call_method(self, OPSTR, &cache_str, \
|
|
"(" ARGCODES ")", arg1, arg2); \
|
|
}
|
|
|
|
static Py_ssize_t
|
|
slot_sq_length(PyObject *self)
|
|
{
|
|
static PyObject *len_str;
|
|
PyObject *res = call_method(self, "__len__", &len_str, "()");
|
|
Py_ssize_t len;
|
|
|
|
if (res == NULL)
|
|
return -1;
|
|
len = PyInt_AsSsize_t(res);
|
|
Py_DECREF(res);
|
|
if (len < 0) {
|
|
if (!PyErr_Occurred())
|
|
PyErr_SetString(PyExc_ValueError,
|
|
"__len__() should return >= 0");
|
|
return -1;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
/* Super-optimized version of slot_sq_item.
|
|
Other slots could do the same... */
|
|
static PyObject *
|
|
slot_sq_item(PyObject *self, Py_ssize_t i)
|
|
{
|
|
static PyObject *getitem_str;
|
|
PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
|
|
descrgetfunc f;
|
|
|
|
if (getitem_str == NULL) {
|
|
getitem_str = PyString_InternFromString("__getitem__");
|
|
if (getitem_str == NULL)
|
|
return NULL;
|
|
}
|
|
func = _PyType_Lookup(self->ob_type, getitem_str);
|
|
if (func != NULL) {
|
|
if ((f = func->ob_type->tp_descr_get) == NULL)
|
|
Py_INCREF(func);
|
|
else {
|
|
func = f(func, self, (PyObject *)(self->ob_type));
|
|
if (func == NULL) {
|
|
return NULL;
|
|
}
|
|
}
|
|
ival = PyInt_FromSsize_t(i);
|
|
if (ival != NULL) {
|
|
args = PyTuple_New(1);
|
|
if (args != NULL) {
|
|
PyTuple_SET_ITEM(args, 0, ival);
|
|
retval = PyObject_Call(func, args, NULL);
|
|
Py_XDECREF(args);
|
|
Py_XDECREF(func);
|
|
return retval;
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
PyErr_SetObject(PyExc_AttributeError, getitem_str);
|
|
}
|
|
Py_XDECREF(args);
|
|
Py_XDECREF(ival);
|
|
Py_XDECREF(func);
|
|
return NULL;
|
|
}
|
|
|
|
SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn")
|
|
|
|
static int
|
|
slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
|
|
{
|
|
PyObject *res;
|
|
static PyObject *delitem_str, *setitem_str;
|
|
|
|
if (value == NULL)
|
|
res = call_method(self, "__delitem__", &delitem_str,
|
|
"(n)", index);
|
|
else
|
|
res = call_method(self, "__setitem__", &setitem_str,
|
|
"(nO)", index, value);
|
|
if (res == NULL)
|
|
return -1;
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
|
|
{
|
|
PyObject *res;
|
|
static PyObject *delslice_str, *setslice_str;
|
|
|
|
if (value == NULL)
|
|
res = call_method(self, "__delslice__", &delslice_str,
|
|
"(nn)", i, j);
|
|
else
|
|
res = call_method(self, "__setslice__", &setslice_str,
|
|
"(nnO)", i, j, value);
|
|
if (res == NULL)
|
|
return -1;
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
slot_sq_contains(PyObject *self, PyObject *value)
|
|
{
|
|
PyObject *func, *res, *args;
|
|
int result = -1;
|
|
|
|
static PyObject *contains_str;
|
|
|
|
func = lookup_maybe(self, "__contains__", &contains_str);
|
|
if (func != NULL) {
|
|
args = PyTuple_Pack(1, value);
|
|
if (args == NULL)
|
|
res = NULL;
|
|
else {
|
|
res = PyObject_Call(func, args, NULL);
|
|
Py_DECREF(args);
|
|
}
|
|
Py_DECREF(func);
|
|
if (res != NULL) {
|
|
result = PyObject_IsTrue(res);
|
|
Py_DECREF(res);
|
|
}
|
|
}
|
|
else if (! PyErr_Occurred()) {
|
|
/* Possible results: -1 and 1 */
|
|
result = (int)_PySequence_IterSearch(self, value,
|
|
PY_ITERSEARCH_CONTAINS);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#define slot_mp_length slot_sq_length
|
|
|
|
SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
|
|
|
|
static int
|
|
slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
|
|
{
|
|
PyObject *res;
|
|
static PyObject *delitem_str, *setitem_str;
|
|
|
|
if (value == NULL)
|
|
res = call_method(self, "__delitem__", &delitem_str,
|
|
"(O)", key);
|
|
else
|
|
res = call_method(self, "__setitem__", &setitem_str,
|
|
"(OO)", key, value);
|
|
if (res == NULL)
|
|
return -1;
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
|
|
SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
|
|
SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
|
|
SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
|
|
SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
|
|
|
|
static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
|
|
|
|
SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
|
|
nb_power, "__pow__", "__rpow__")
|
|
|
|
static PyObject *
|
|
slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
|
|
{
|
|
static PyObject *pow_str;
|
|
|
|
if (modulus == Py_None)
|
|
return slot_nb_power_binary(self, other);
|
|
/* Three-arg power doesn't use __rpow__. But ternary_op
|
|
can call this when the second argument's type uses
|
|
slot_nb_power, so check before calling self.__pow__. */
|
|
if (self->ob_type->tp_as_number != NULL &&
|
|
self->ob_type->tp_as_number->nb_power == slot_nb_power) {
|
|
return call_method(self, "__pow__", &pow_str,
|
|
"(OO)", other, modulus);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
SLOT0(slot_nb_negative, "__neg__")
|
|
SLOT0(slot_nb_positive, "__pos__")
|
|
SLOT0(slot_nb_absolute, "__abs__")
|
|
|
|
static int
|
|
slot_nb_bool(PyObject *self)
|
|
{
|
|
PyObject *func, *args;
|
|
static PyObject *bool_str, *len_str;
|
|
int result = -1;
|
|
int from_len = 0;
|
|
|
|
func = lookup_maybe(self, "__bool__", &bool_str);
|
|
if (func == NULL) {
|
|
if (PyErr_Occurred())
|
|
return -1;
|
|
func = lookup_maybe(self, "__len__", &len_str);
|
|
if (func == NULL)
|
|
return PyErr_Occurred() ? -1 : 1;
|
|
from_len = 1;
|
|
}
|
|
args = PyTuple_New(0);
|
|
if (args != NULL) {
|
|
PyObject *temp = PyObject_Call(func, args, NULL);
|
|
Py_DECREF(args);
|
|
if (temp != NULL) {
|
|
if (from_len) {
|
|
/* enforced by slot_nb_len */
|
|
result = PyObject_IsTrue(temp);
|
|
}
|
|
else if (PyBool_Check(temp)) {
|
|
result = PyObject_IsTrue(temp);
|
|
}
|
|
else {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__bool__ should return "
|
|
"bool, returned %s",
|
|
temp->ob_type->tp_name);
|
|
result = -1;
|
|
}
|
|
Py_DECREF(temp);
|
|
}
|
|
}
|
|
Py_DECREF(func);
|
|
return result;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
slot_nb_index(PyObject *self)
|
|
{
|
|
static PyObject *index_str;
|
|
return call_method(self, "__index__", &index_str, "()");
|
|
}
|
|
|
|
|
|
SLOT0(slot_nb_invert, "__invert__")
|
|
SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
|
|
SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
|
|
SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
|
|
SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
|
|
SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
|
|
|
|
SLOT0(slot_nb_int, "__int__")
|
|
SLOT0(slot_nb_long, "__long__")
|
|
SLOT0(slot_nb_float, "__float__")
|
|
SLOT0(slot_nb_oct, "__oct__")
|
|
SLOT0(slot_nb_hex, "__hex__")
|
|
SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
|
|
SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
|
|
"__floordiv__", "__rfloordiv__")
|
|
SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
|
|
SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
|
|
SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
|
|
|
|
static int
|
|
half_compare(PyObject *self, PyObject *other)
|
|
{
|
|
PyObject *func, *args, *res;
|
|
static PyObject *cmp_str;
|
|
Py_ssize_t c;
|
|
|
|
func = lookup_method(self, "__cmp__", &cmp_str);
|
|
if (func == NULL) {
|
|
PyErr_Clear();
|
|
}
|
|
else {
|
|
args = PyTuple_Pack(1, other);
|
|
if (args == NULL)
|
|
res = NULL;
|
|
else {
|
|
res = PyObject_Call(func, args, NULL);
|
|
Py_DECREF(args);
|
|
}
|
|
Py_DECREF(func);
|
|
if (res != Py_NotImplemented) {
|
|
if (res == NULL)
|
|
return -2;
|
|
c = PyInt_AsLong(res);
|
|
Py_DECREF(res);
|
|
if (c == -1 && PyErr_Occurred())
|
|
return -2;
|
|
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
|
|
}
|
|
Py_DECREF(res);
|
|
}
|
|
return 2;
|
|
}
|
|
|
|
/* This slot is published for the benefit of try_3way_compare in object.c */
|
|
int
|
|
_PyObject_SlotCompare(PyObject *self, PyObject *other)
|
|
{
|
|
int c;
|
|
|
|
if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
|
|
c = half_compare(self, other);
|
|
if (c <= 1)
|
|
return c;
|
|
}
|
|
if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
|
|
c = half_compare(other, self);
|
|
if (c < -1)
|
|
return -2;
|
|
if (c <= 1)
|
|
return -c;
|
|
}
|
|
return (void *)self < (void *)other ? -1 :
|
|
(void *)self > (void *)other ? 1 : 0;
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_repr(PyObject *self)
|
|
{
|
|
PyObject *func, *res;
|
|
static PyObject *repr_str;
|
|
|
|
func = lookup_method(self, "__repr__", &repr_str);
|
|
if (func != NULL) {
|
|
res = PyEval_CallObject(func, NULL);
|
|
Py_DECREF(func);
|
|
return res;
|
|
}
|
|
PyErr_Clear();
|
|
return PyString_FromFormat("<%s object at %p>",
|
|
self->ob_type->tp_name, self);
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_str(PyObject *self)
|
|
{
|
|
PyObject *func, *res;
|
|
static PyObject *str_str;
|
|
|
|
func = lookup_method(self, "__str__", &str_str);
|
|
if (func != NULL) {
|
|
res = PyEval_CallObject(func, NULL);
|
|
Py_DECREF(func);
|
|
return res;
|
|
}
|
|
else {
|
|
PyErr_Clear();
|
|
return slot_tp_repr(self);
|
|
}
|
|
}
|
|
|
|
static long
|
|
slot_tp_hash(PyObject *self)
|
|
{
|
|
PyObject *func, *res;
|
|
static PyObject *hash_str;
|
|
long h;
|
|
|
|
func = lookup_method(self, "__hash__", &hash_str);
|
|
|
|
if (func == Py_None) {
|
|
Py_DECREF(func);
|
|
func = NULL;
|
|
}
|
|
|
|
if (func == NULL) {
|
|
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
|
|
self->ob_type->tp_name);
|
|
return -1;
|
|
}
|
|
|
|
res = PyEval_CallObject(func, NULL);
|
|
Py_DECREF(func);
|
|
if (res == NULL)
|
|
return -1;
|
|
if (PyLong_Check(res))
|
|
h = PyLong_Type.tp_hash(res);
|
|
else
|
|
h = PyInt_AsLong(res);
|
|
Py_DECREF(res);
|
|
if (h == -1 && !PyErr_Occurred())
|
|
h = -2;
|
|
return h;
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
static PyObject *call_str;
|
|
PyObject *meth = lookup_method(self, "__call__", &call_str);
|
|
PyObject *res;
|
|
|
|
if (meth == NULL)
|
|
return NULL;
|
|
|
|
/* PyObject_Call() will end up calling slot_tp_call() again if
|
|
the object returned for __call__ has __call__ itself defined
|
|
upon it. This can be an infinite recursion if you set
|
|
__call__ in a class to an instance of it. */
|
|
if (Py_EnterRecursiveCall(" in __call__")) {
|
|
Py_DECREF(meth);
|
|
return NULL;
|
|
}
|
|
res = PyObject_Call(meth, args, kwds);
|
|
Py_LeaveRecursiveCall();
|
|
|
|
Py_DECREF(meth);
|
|
return res;
|
|
}
|
|
|
|
/* There are two slot dispatch functions for tp_getattro.
|
|
|
|
- slot_tp_getattro() is used when __getattribute__ is overridden
|
|
but no __getattr__ hook is present;
|
|
|
|
- slot_tp_getattr_hook() is used when a __getattr__ hook is present.
|
|
|
|
The code in update_one_slot() always installs slot_tp_getattr_hook(); this
|
|
detects the absence of __getattr__ and then installs the simpler slot if
|
|
necessary. */
|
|
|
|
static PyObject *
|
|
slot_tp_getattro(PyObject *self, PyObject *name)
|
|
{
|
|
static PyObject *getattribute_str = NULL;
|
|
return call_method(self, "__getattribute__", &getattribute_str,
|
|
"(O)", name);
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_getattr_hook(PyObject *self, PyObject *name)
|
|
{
|
|
PyTypeObject *tp = self->ob_type;
|
|
PyObject *getattr, *getattribute, *res;
|
|
static PyObject *getattribute_str = NULL;
|
|
static PyObject *getattr_str = NULL;
|
|
|
|
if (getattr_str == NULL) {
|
|
getattr_str = PyString_InternFromString("__getattr__");
|
|
if (getattr_str == NULL)
|
|
return NULL;
|
|
}
|
|
if (getattribute_str == NULL) {
|
|
getattribute_str =
|
|
PyString_InternFromString("__getattribute__");
|
|
if (getattribute_str == NULL)
|
|
return NULL;
|
|
}
|
|
getattr = _PyType_Lookup(tp, getattr_str);
|
|
if (getattr == NULL) {
|
|
/* No __getattr__ hook: use a simpler dispatcher */
|
|
tp->tp_getattro = slot_tp_getattro;
|
|
return slot_tp_getattro(self, name);
|
|
}
|
|
getattribute = _PyType_Lookup(tp, getattribute_str);
|
|
if (getattribute == NULL ||
|
|
(getattribute->ob_type == &PyWrapperDescr_Type &&
|
|
((PyWrapperDescrObject *)getattribute)->d_wrapped ==
|
|
(void *)PyObject_GenericGetAttr))
|
|
res = PyObject_GenericGetAttr(self, name);
|
|
else
|
|
res = PyObject_CallFunctionObjArgs(getattribute, self, name, NULL);
|
|
if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
PyErr_Clear();
|
|
res = PyObject_CallFunctionObjArgs(getattr, self, name, NULL);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
static int
|
|
slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
|
|
{
|
|
PyObject *res;
|
|
static PyObject *delattr_str, *setattr_str;
|
|
|
|
if (value == NULL)
|
|
res = call_method(self, "__delattr__", &delattr_str,
|
|
"(O)", name);
|
|
else
|
|
res = call_method(self, "__setattr__", &setattr_str,
|
|
"(OO)", name, value);
|
|
if (res == NULL)
|
|
return -1;
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
half_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
PyObject *func, *args, *res;
|
|
static PyObject *op_str[6];
|
|
|
|
func = lookup_method(self, name_op[op], &op_str[op]);
|
|
if (func == NULL) {
|
|
PyErr_Clear();
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
args = PyTuple_Pack(1, other);
|
|
if (args == NULL)
|
|
res = NULL;
|
|
else {
|
|
res = PyObject_Call(func, args, NULL);
|
|
Py_DECREF(args);
|
|
}
|
|
Py_DECREF(func);
|
|
return res;
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_richcompare(PyObject *self, PyObject *other, int op)
|
|
{
|
|
PyObject *res;
|
|
|
|
if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
|
|
res = half_richcompare(self, other, op);
|
|
if (res != Py_NotImplemented)
|
|
return res;
|
|
Py_DECREF(res);
|
|
}
|
|
if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
|
|
res = half_richcompare(other, self, _Py_SwappedOp[op]);
|
|
if (res != Py_NotImplemented) {
|
|
return res;
|
|
}
|
|
Py_DECREF(res);
|
|
}
|
|
Py_INCREF(Py_NotImplemented);
|
|
return Py_NotImplemented;
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_iter(PyObject *self)
|
|
{
|
|
PyObject *func, *res;
|
|
static PyObject *iter_str, *getitem_str;
|
|
|
|
func = lookup_method(self, "__iter__", &iter_str);
|
|
if (func != NULL) {
|
|
PyObject *args;
|
|
args = res = PyTuple_New(0);
|
|
if (args != NULL) {
|
|
res = PyObject_Call(func, args, NULL);
|
|
Py_DECREF(args);
|
|
}
|
|
Py_DECREF(func);
|
|
return res;
|
|
}
|
|
PyErr_Clear();
|
|
func = lookup_method(self, "__getitem__", &getitem_str);
|
|
if (func == NULL) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"'%.200s' object is not iterable",
|
|
self->ob_type->tp_name);
|
|
return NULL;
|
|
}
|
|
Py_DECREF(func);
|
|
return PySeqIter_New(self);
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_iternext(PyObject *self)
|
|
{
|
|
static PyObject *next_str;
|
|
return call_method(self, "next", &next_str, "()");
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
{
|
|
PyTypeObject *tp = self->ob_type;
|
|
PyObject *get;
|
|
static PyObject *get_str = NULL;
|
|
|
|
if (get_str == NULL) {
|
|
get_str = PyString_InternFromString("__get__");
|
|
if (get_str == NULL)
|
|
return NULL;
|
|
}
|
|
get = _PyType_Lookup(tp, get_str);
|
|
if (get == NULL) {
|
|
/* Avoid further slowdowns */
|
|
if (tp->tp_descr_get == slot_tp_descr_get)
|
|
tp->tp_descr_get = NULL;
|
|
Py_INCREF(self);
|
|
return self;
|
|
}
|
|
if (obj == NULL)
|
|
obj = Py_None;
|
|
if (type == NULL)
|
|
type = Py_None;
|
|
return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
|
|
}
|
|
|
|
static int
|
|
slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
|
|
{
|
|
PyObject *res;
|
|
static PyObject *del_str, *set_str;
|
|
|
|
if (value == NULL)
|
|
res = call_method(self, "__delete__", &del_str,
|
|
"(O)", target);
|
|
else
|
|
res = call_method(self, "__set__", &set_str,
|
|
"(OO)", target, value);
|
|
if (res == NULL)
|
|
return -1;
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
static PyObject *init_str;
|
|
PyObject *meth = lookup_method(self, "__init__", &init_str);
|
|
PyObject *res;
|
|
|
|
if (meth == NULL)
|
|
return -1;
|
|
res = PyObject_Call(meth, args, kwds);
|
|
Py_DECREF(meth);
|
|
if (res == NULL)
|
|
return -1;
|
|
if (res != Py_None) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"__init__() should return None, not '%.200s'",
|
|
res->ob_type->tp_name);
|
|
Py_DECREF(res);
|
|
return -1;
|
|
}
|
|
Py_DECREF(res);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
static PyObject *new_str;
|
|
PyObject *func;
|
|
PyObject *newargs, *x;
|
|
Py_ssize_t i, n;
|
|
|
|
if (new_str == NULL) {
|
|
new_str = PyString_InternFromString("__new__");
|
|
if (new_str == NULL)
|
|
return NULL;
|
|
}
|
|
func = PyObject_GetAttr((PyObject *)type, new_str);
|
|
if (func == NULL)
|
|
return NULL;
|
|
assert(PyTuple_Check(args));
|
|
n = PyTuple_GET_SIZE(args);
|
|
newargs = PyTuple_New(n+1);
|
|
if (newargs == NULL)
|
|
return NULL;
|
|
Py_INCREF(type);
|
|
PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
|
|
for (i = 0; i < n; i++) {
|
|
x = PyTuple_GET_ITEM(args, i);
|
|
Py_INCREF(x);
|
|
PyTuple_SET_ITEM(newargs, i+1, x);
|
|
}
|
|
x = PyObject_Call(func, newargs, kwds);
|
|
Py_DECREF(newargs);
|
|
Py_DECREF(func);
|
|
return x;
|
|
}
|
|
|
|
static void
|
|
slot_tp_del(PyObject *self)
|
|
{
|
|
static PyObject *del_str = NULL;
|
|
PyObject *del, *res;
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
/* Temporarily resurrect the object. */
|
|
assert(self->ob_refcnt == 0);
|
|
self->ob_refcnt = 1;
|
|
|
|
/* Save the current exception, if any. */
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
|
|
|
/* Execute __del__ method, if any. */
|
|
del = lookup_maybe(self, "__del__", &del_str);
|
|
if (del != NULL) {
|
|
res = PyEval_CallObject(del, NULL);
|
|
if (res == NULL)
|
|
PyErr_WriteUnraisable(del);
|
|
else
|
|
Py_DECREF(res);
|
|
Py_DECREF(del);
|
|
}
|
|
|
|
/* Restore the saved exception. */
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
|
|
|
/* Undo the temporary resurrection; can't use DECREF here, it would
|
|
* cause a recursive call.
|
|
*/
|
|
assert(self->ob_refcnt > 0);
|
|
if (--self->ob_refcnt == 0)
|
|
return; /* this is the normal path out */
|
|
|
|
/* __del__ resurrected it! Make it look like the original Py_DECREF
|
|
* never happened.
|
|
*/
|
|
{
|
|
Py_ssize_t refcnt = self->ob_refcnt;
|
|
_Py_NewReference(self);
|
|
self->ob_refcnt = refcnt;
|
|
}
|
|
assert(!PyType_IS_GC(self->ob_type) ||
|
|
_Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
|
|
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
|
* we need to undo that. */
|
|
_Py_DEC_REFTOTAL;
|
|
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
|
* chain, so no more to do there.
|
|
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
|
* _Py_NewReference bumped tp_allocs: both of those need to be
|
|
* undone.
|
|
*/
|
|
#ifdef COUNT_ALLOCS
|
|
--self->ob_type->tp_frees;
|
|
--self->ob_type->tp_allocs;
|
|
#endif
|
|
}
|
|
|
|
|
|
/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
|
|
functions. The offsets here are relative to the 'PyHeapTypeObject'
|
|
structure, which incorporates the additional structures used for numbers,
|
|
sequences and mappings.
|
|
Note that multiple names may map to the same slot (e.g. __eq__,
|
|
__ne__ etc. all map to tp_richcompare) and one name may map to multiple
|
|
slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
|
|
terminated with an all-zero entry. (This table is further initialized and
|
|
sorted in init_slotdefs() below.) */
|
|
|
|
typedef struct wrapperbase slotdef;
|
|
|
|
#undef TPSLOT
|
|
#undef FLSLOT
|
|
#undef ETSLOT
|
|
#undef SQSLOT
|
|
#undef MPSLOT
|
|
#undef NBSLOT
|
|
#undef UNSLOT
|
|
#undef IBSLOT
|
|
#undef BINSLOT
|
|
#undef RBINSLOT
|
|
|
|
#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
{NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
|
|
PyDoc_STR(DOC)}
|
|
#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
|
|
{NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
|
|
PyDoc_STR(DOC), FLAGS}
|
|
#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
{NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
|
|
PyDoc_STR(DOC)}
|
|
#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
|
|
#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
|
|
#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
|
|
#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
|
|
"x." NAME "() <==> " DOC)
|
|
#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
|
|
"x." NAME "(y) <==> x" DOC "y")
|
|
#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
|
|
"x." NAME "(y) <==> x" DOC "y")
|
|
#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
|
|
"x." NAME "(y) <==> y" DOC "x")
|
|
#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
|
|
"x." NAME "(y) <==> " DOC)
|
|
#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
|
|
ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
|
|
"x." NAME "(y) <==> " DOC)
|
|
|
|
static slotdef slotdefs[] = {
|
|
SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
|
|
"x.__len__() <==> len(x)"),
|
|
/* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
|
|
The logic in abstract.c always falls back to nb_add/nb_multiply in
|
|
this case. Defining both the nb_* and the sq_* slots to call the
|
|
user-defined methods has unexpected side-effects, as shown by
|
|
test_descr.notimplemented() */
|
|
SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
|
|
"x.__add__(y) <==> x+y"),
|
|
SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
|
|
"x.__mul__(n) <==> x*n"),
|
|
SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
|
|
"x.__rmul__(n) <==> n*x"),
|
|
SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
|
|
"x.__getitem__(y) <==> x[y]"),
|
|
SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
|
|
"x.__getslice__(i, j) <==> x[i:j]\n\
|
|
\n\
|
|
Use of negative indices is not supported."),
|
|
SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
|
|
"x.__setitem__(i, y) <==> x[i]=y"),
|
|
SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
|
|
"x.__delitem__(y) <==> del x[y]"),
|
|
SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
|
|
wrap_ssizessizeobjargproc,
|
|
"x.__setslice__(i, j, y) <==> x[i:j]=y\n\
|
|
\n\
|
|
Use of negative indices is not supported."),
|
|
SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
|
|
"x.__delslice__(i, j) <==> del x[i:j]\n\
|
|
\n\
|
|
Use of negative indices is not supported."),
|
|
SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
|
|
"x.__contains__(y) <==> y in x"),
|
|
SQSLOT("__iadd__", sq_inplace_concat, NULL,
|
|
wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
|
|
SQSLOT("__imul__", sq_inplace_repeat, NULL,
|
|
wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
|
|
|
|
MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
|
|
"x.__len__() <==> len(x)"),
|
|
MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
|
|
wrap_binaryfunc,
|
|
"x.__getitem__(y) <==> x[y]"),
|
|
MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
|
|
wrap_objobjargproc,
|
|
"x.__setitem__(i, y) <==> x[i]=y"),
|
|
MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
|
|
wrap_delitem,
|
|
"x.__delitem__(y) <==> del x[y]"),
|
|
|
|
BINSLOT("__add__", nb_add, slot_nb_add,
|
|
"+"),
|
|
RBINSLOT("__radd__", nb_add, slot_nb_add,
|
|
"+"),
|
|
BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
|
|
"-"),
|
|
RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
|
|
"-"),
|
|
BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
|
|
"*"),
|
|
RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
|
|
"*"),
|
|
BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
|
|
"%"),
|
|
RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
|
|
"%"),
|
|
BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
|
|
"divmod(x, y)"),
|
|
RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
|
|
"divmod(y, x)"),
|
|
NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
|
|
"x.__pow__(y[, z]) <==> pow(x, y[, z])"),
|
|
NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
|
|
"y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
|
|
UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
|
|
UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
|
|
UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
|
|
"abs(x)"),
|
|
UNSLOT("__bool__", nb_bool, slot_nb_bool, wrap_inquirypred,
|
|
"x != 0"),
|
|
UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
|
|
BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
|
|
RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
|
|
BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
|
|
RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
|
|
BINSLOT("__and__", nb_and, slot_nb_and, "&"),
|
|
RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
|
|
BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
|
|
RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
|
|
BINSLOT("__or__", nb_or, slot_nb_or, "|"),
|
|
RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
|
|
UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
|
|
"int(x)"),
|
|
UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
|
|
"long(x)"),
|
|
UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
|
|
"float(x)"),
|
|
UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
|
|
"oct(x)"),
|
|
UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
|
|
"hex(x)"),
|
|
NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
|
|
"x[y:z] <==> x[y.__index__():z.__index__()]"),
|
|
IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
|
|
wrap_binaryfunc, "+"),
|
|
IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
|
|
wrap_binaryfunc, "-"),
|
|
IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
|
|
wrap_binaryfunc, "*"),
|
|
IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
|
|
wrap_binaryfunc, "%"),
|
|
IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
|
|
wrap_binaryfunc, "**"),
|
|
IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
|
|
wrap_binaryfunc, "<<"),
|
|
IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
|
|
wrap_binaryfunc, ">>"),
|
|
IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
|
|
wrap_binaryfunc, "&"),
|
|
IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
|
|
wrap_binaryfunc, "^"),
|
|
IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
|
|
wrap_binaryfunc, "|"),
|
|
BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
|
|
RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
|
|
BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
|
|
RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
|
|
IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
|
|
slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
|
|
IBSLOT("__itruediv__", nb_inplace_true_divide,
|
|
slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
|
|
|
|
TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
|
|
"x.__str__() <==> str(x)"),
|
|
TPSLOT("__str__", tp_print, NULL, NULL, ""),
|
|
TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
|
|
"x.__repr__() <==> repr(x)"),
|
|
TPSLOT("__repr__", tp_print, NULL, NULL, ""),
|
|
TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
|
|
"x.__cmp__(y) <==> cmp(x,y)"),
|
|
TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
|
|
"x.__hash__() <==> hash(x)"),
|
|
FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
|
|
"x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
|
|
TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
|
|
wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
|
|
TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
|
|
TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
|
|
TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
|
|
TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
|
|
"x.__setattr__('name', value) <==> x.name = value"),
|
|
TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
|
|
TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
|
|
"x.__delattr__('name') <==> del x.name"),
|
|
TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
|
|
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
|
|
"x.__lt__(y) <==> x<y"),
|
|
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
|
|
"x.__le__(y) <==> x<=y"),
|
|
TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
|
|
"x.__eq__(y) <==> x==y"),
|
|
TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
|
|
"x.__ne__(y) <==> x!=y"),
|
|
TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
|
|
"x.__gt__(y) <==> x>y"),
|
|
TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
|
|
"x.__ge__(y) <==> x>=y"),
|
|
TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
|
|
"x.__iter__() <==> iter(x)"),
|
|
TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
|
|
"x.next() -> the next value, or raise StopIteration"),
|
|
TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
|
|
"descr.__get__(obj[, type]) -> value"),
|
|
TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
|
|
"descr.__set__(obj, value)"),
|
|
TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
|
|
wrap_descr_delete, "descr.__delete__(obj)"),
|
|
FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
|
|
"x.__init__(...) initializes x; "
|
|
"see x.__class__.__doc__ for signature",
|
|
PyWrapperFlag_KEYWORDS),
|
|
TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
|
|
TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
|
|
{NULL}
|
|
};
|
|
|
|
/* Given a type pointer and an offset gotten from a slotdef entry, return a
|
|
pointer to the actual slot. This is not quite the same as simply adding
|
|
the offset to the type pointer, since it takes care to indirect through the
|
|
proper indirection pointer (as_buffer, etc.); it returns NULL if the
|
|
indirection pointer is NULL. */
|
|
static void **
|
|
slotptr(PyTypeObject *type, int ioffset)
|
|
{
|
|
char *ptr;
|
|
long offset = ioffset;
|
|
|
|
/* Note: this depends on the order of the members of PyHeapTypeObject! */
|
|
assert(offset >= 0);
|
|
assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
|
|
if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
|
|
ptr = (char *)type->tp_as_sequence;
|
|
offset -= offsetof(PyHeapTypeObject, as_sequence);
|
|
}
|
|
else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
|
|
ptr = (char *)type->tp_as_mapping;
|
|
offset -= offsetof(PyHeapTypeObject, as_mapping);
|
|
}
|
|
else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
|
|
ptr = (char *)type->tp_as_number;
|
|
offset -= offsetof(PyHeapTypeObject, as_number);
|
|
}
|
|
else {
|
|
ptr = (char *)type;
|
|
}
|
|
if (ptr != NULL)
|
|
ptr += offset;
|
|
return (void **)ptr;
|
|
}
|
|
|
|
/* Length of array of slotdef pointers used to store slots with the
|
|
same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
|
|
the same __name__, for any __name__. Since that's a static property, it is
|
|
appropriate to declare fixed-size arrays for this. */
|
|
#define MAX_EQUIV 10
|
|
|
|
/* Return a slot pointer for a given name, but ONLY if the attribute has
|
|
exactly one slot function. The name must be an interned string. */
|
|
static void **
|
|
resolve_slotdups(PyTypeObject *type, PyObject *name)
|
|
{
|
|
/* XXX Maybe this could be optimized more -- but is it worth it? */
|
|
|
|
/* pname and ptrs act as a little cache */
|
|
static PyObject *pname;
|
|
static slotdef *ptrs[MAX_EQUIV];
|
|
slotdef *p, **pp;
|
|
void **res, **ptr;
|
|
|
|
if (pname != name) {
|
|
/* Collect all slotdefs that match name into ptrs. */
|
|
pname = name;
|
|
pp = ptrs;
|
|
for (p = slotdefs; p->name_strobj; p++) {
|
|
if (p->name_strobj == name)
|
|
*pp++ = p;
|
|
}
|
|
*pp = NULL;
|
|
}
|
|
|
|
/* Look in all matching slots of the type; if exactly one of these has
|
|
a filled-in slot, return its value. Otherwise return NULL. */
|
|
res = NULL;
|
|
for (pp = ptrs; *pp; pp++) {
|
|
ptr = slotptr(type, (*pp)->offset);
|
|
if (ptr == NULL || *ptr == NULL)
|
|
continue;
|
|
if (res != NULL)
|
|
return NULL;
|
|
res = ptr;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/* Common code for update_slots_callback() and fixup_slot_dispatchers(). This
|
|
does some incredibly complex thinking and then sticks something into the
|
|
slot. (It sees if the adjacent slotdefs for the same slot have conflicting
|
|
interests, and then stores a generic wrapper or a specific function into
|
|
the slot.) Return a pointer to the next slotdef with a different offset,
|
|
because that's convenient for fixup_slot_dispatchers(). */
|
|
static slotdef *
|
|
update_one_slot(PyTypeObject *type, slotdef *p)
|
|
{
|
|
PyObject *descr;
|
|
PyWrapperDescrObject *d;
|
|
void *generic = NULL, *specific = NULL;
|
|
int use_generic = 0;
|
|
int offset = p->offset;
|
|
void **ptr = slotptr(type, offset);
|
|
|
|
if (ptr == NULL) {
|
|
do {
|
|
++p;
|
|
} while (p->offset == offset);
|
|
return p;
|
|
}
|
|
do {
|
|
descr = _PyType_Lookup(type, p->name_strobj);
|
|
if (descr == NULL)
|
|
continue;
|
|
if (descr->ob_type == &PyWrapperDescr_Type) {
|
|
void **tptr = resolve_slotdups(type, p->name_strobj);
|
|
if (tptr == NULL || tptr == ptr)
|
|
generic = p->function;
|
|
d = (PyWrapperDescrObject *)descr;
|
|
if (d->d_base->wrapper == p->wrapper &&
|
|
PyType_IsSubtype(type, d->d_type))
|
|
{
|
|
if (specific == NULL ||
|
|
specific == d->d_wrapped)
|
|
specific = d->d_wrapped;
|
|
else
|
|
use_generic = 1;
|
|
}
|
|
}
|
|
else if (descr->ob_type == &PyCFunction_Type &&
|
|
PyCFunction_GET_FUNCTION(descr) ==
|
|
(PyCFunction)tp_new_wrapper &&
|
|
strcmp(p->name, "__new__") == 0)
|
|
{
|
|
/* The __new__ wrapper is not a wrapper descriptor,
|
|
so must be special-cased differently.
|
|
If we don't do this, creating an instance will
|
|
always use slot_tp_new which will look up
|
|
__new__ in the MRO which will call tp_new_wrapper
|
|
which will look through the base classes looking
|
|
for a static base and call its tp_new (usually
|
|
PyType_GenericNew), after performing various
|
|
sanity checks and constructing a new argument
|
|
list. Cut all that nonsense short -- this speeds
|
|
up instance creation tremendously. */
|
|
specific = (void *)type->tp_new;
|
|
/* XXX I'm not 100% sure that there isn't a hole
|
|
in this reasoning that requires additional
|
|
sanity checks. I'll buy the first person to
|
|
point out a bug in this reasoning a beer. */
|
|
}
|
|
else {
|
|
use_generic = 1;
|
|
generic = p->function;
|
|
}
|
|
} while ((++p)->offset == offset);
|
|
if (specific && !use_generic)
|
|
*ptr = specific;
|
|
else
|
|
*ptr = generic;
|
|
return p;
|
|
}
|
|
|
|
/* In the type, update the slots whose slotdefs are gathered in the pp array.
|
|
This is a callback for update_subclasses(). */
|
|
static int
|
|
update_slots_callback(PyTypeObject *type, void *data)
|
|
{
|
|
slotdef **pp = (slotdef **)data;
|
|
|
|
for (; *pp; pp++)
|
|
update_one_slot(type, *pp);
|
|
return 0;
|
|
}
|
|
|
|
/* Comparison function for qsort() to compare slotdefs by their offset, and
|
|
for equal offset by their address (to force a stable sort). */
|
|
static int
|
|
slotdef_cmp(const void *aa, const void *bb)
|
|
{
|
|
const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
|
|
int c = a->offset - b->offset;
|
|
if (c != 0)
|
|
return c;
|
|
else
|
|
/* Cannot use a-b, as this gives off_t,
|
|
which may lose precision when converted to int. */
|
|
return (a > b) ? 1 : (a < b) ? -1 : 0;
|
|
}
|
|
|
|
/* Initialize the slotdefs table by adding interned string objects for the
|
|
names and sorting the entries. */
|
|
static void
|
|
init_slotdefs(void)
|
|
{
|
|
slotdef *p;
|
|
static int initialized = 0;
|
|
|
|
if (initialized)
|
|
return;
|
|
for (p = slotdefs; p->name; p++) {
|
|
p->name_strobj = PyString_InternFromString(p->name);
|
|
if (!p->name_strobj)
|
|
Py_FatalError("Out of memory interning slotdef names");
|
|
}
|
|
qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
|
|
slotdef_cmp);
|
|
initialized = 1;
|
|
}
|
|
|
|
/* Update the slots after assignment to a class (type) attribute. */
|
|
static int
|
|
update_slot(PyTypeObject *type, PyObject *name)
|
|
{
|
|
slotdef *ptrs[MAX_EQUIV];
|
|
slotdef *p;
|
|
slotdef **pp;
|
|
int offset;
|
|
|
|
init_slotdefs();
|
|
pp = ptrs;
|
|
for (p = slotdefs; p->name; p++) {
|
|
/* XXX assume name is interned! */
|
|
if (p->name_strobj == name)
|
|
*pp++ = p;
|
|
}
|
|
*pp = NULL;
|
|
for (pp = ptrs; *pp; pp++) {
|
|
p = *pp;
|
|
offset = p->offset;
|
|
while (p > slotdefs && (p-1)->offset == offset)
|
|
--p;
|
|
*pp = p;
|
|
}
|
|
if (ptrs[0] == NULL)
|
|
return 0; /* Not an attribute that affects any slots */
|
|
return update_subclasses(type, name,
|
|
update_slots_callback, (void *)ptrs);
|
|
}
|
|
|
|
/* Store the proper functions in the slot dispatches at class (type)
|
|
definition time, based upon which operations the class overrides in its
|
|
dict. */
|
|
static void
|
|
fixup_slot_dispatchers(PyTypeObject *type)
|
|
{
|
|
slotdef *p;
|
|
|
|
init_slotdefs();
|
|
for (p = slotdefs; p->name; )
|
|
p = update_one_slot(type, p);
|
|
}
|
|
|
|
static void
|
|
update_all_slots(PyTypeObject* type)
|
|
{
|
|
slotdef *p;
|
|
|
|
init_slotdefs();
|
|
for (p = slotdefs; p->name; p++) {
|
|
/* update_slot returns int but can't actually fail */
|
|
update_slot(type, p->name_strobj);
|
|
}
|
|
}
|
|
|
|
/* recurse_down_subclasses() and update_subclasses() are mutually
|
|
recursive functions to call a callback for all subclasses,
|
|
but refraining from recursing into subclasses that define 'name'. */
|
|
|
|
static int
|
|
update_subclasses(PyTypeObject *type, PyObject *name,
|
|
update_callback callback, void *data)
|
|
{
|
|
if (callback(type, data) < 0)
|
|
return -1;
|
|
return recurse_down_subclasses(type, name, callback, data);
|
|
}
|
|
|
|
static int
|
|
recurse_down_subclasses(PyTypeObject *type, PyObject *name,
|
|
update_callback callback, void *data)
|
|
{
|
|
PyTypeObject *subclass;
|
|
PyObject *ref, *subclasses, *dict;
|
|
Py_ssize_t i, n;
|
|
|
|
subclasses = type->tp_subclasses;
|
|
if (subclasses == NULL)
|
|
return 0;
|
|
assert(PyList_Check(subclasses));
|
|
n = PyList_GET_SIZE(subclasses);
|
|
for (i = 0; i < n; i++) {
|
|
ref = PyList_GET_ITEM(subclasses, i);
|
|
assert(PyWeakref_CheckRef(ref));
|
|
subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
|
|
assert(subclass != NULL);
|
|
if ((PyObject *)subclass == Py_None)
|
|
continue;
|
|
assert(PyType_Check(subclass));
|
|
/* Avoid recursing down into unaffected classes */
|
|
dict = subclass->tp_dict;
|
|
if (dict != NULL && PyDict_Check(dict) &&
|
|
PyDict_GetItem(dict, name) != NULL)
|
|
continue;
|
|
if (update_subclasses(subclass, name, callback, data) < 0)
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* This function is called by PyType_Ready() to populate the type's
|
|
dictionary with method descriptors for function slots. For each
|
|
function slot (like tp_repr) that's defined in the type, one or more
|
|
corresponding descriptors are added in the type's tp_dict dictionary
|
|
under the appropriate name (like __repr__). Some function slots
|
|
cause more than one descriptor to be added (for example, the nb_add
|
|
slot adds both __add__ and __radd__ descriptors) and some function
|
|
slots compete for the same descriptor (for example both sq_item and
|
|
mp_subscript generate a __getitem__ descriptor).
|
|
|
|
In the latter case, the first slotdef entry encoutered wins. Since
|
|
slotdef entries are sorted by the offset of the slot in the
|
|
PyHeapTypeObject, this gives us some control over disambiguating
|
|
between competing slots: the members of PyHeapTypeObject are listed
|
|
from most general to least general, so the most general slot is
|
|
preferred. In particular, because as_mapping comes before as_sequence,
|
|
for a type that defines both mp_subscript and sq_item, mp_subscript
|
|
wins.
|
|
|
|
This only adds new descriptors and doesn't overwrite entries in
|
|
tp_dict that were previously defined. The descriptors contain a
|
|
reference to the C function they must call, so that it's safe if they
|
|
are copied into a subtype's __dict__ and the subtype has a different
|
|
C function in its slot -- calling the method defined by the
|
|
descriptor will call the C function that was used to create it,
|
|
rather than the C function present in the slot when it is called.
|
|
(This is important because a subtype may have a C function in the
|
|
slot that calls the method from the dictionary, and we want to avoid
|
|
infinite recursion here.) */
|
|
|
|
static int
|
|
add_operators(PyTypeObject *type)
|
|
{
|
|
PyObject *dict = type->tp_dict;
|
|
slotdef *p;
|
|
PyObject *descr;
|
|
void **ptr;
|
|
|
|
init_slotdefs();
|
|
for (p = slotdefs; p->name; p++) {
|
|
if (p->wrapper == NULL)
|
|
continue;
|
|
ptr = slotptr(type, p->offset);
|
|
if (!ptr || !*ptr)
|
|
continue;
|
|
if (PyDict_GetItem(dict, p->name_strobj))
|
|
continue;
|
|
descr = PyDescr_NewWrapper(type, p, *ptr);
|
|
if (descr == NULL)
|
|
return -1;
|
|
if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
|
|
return -1;
|
|
Py_DECREF(descr);
|
|
}
|
|
if (type->tp_new != NULL) {
|
|
if (add_tp_new_wrapper(type) < 0)
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Cooperative 'super' */
|
|
|
|
typedef struct {
|
|
PyObject_HEAD
|
|
PyTypeObject *type;
|
|
PyObject *obj;
|
|
PyTypeObject *obj_type;
|
|
} superobject;
|
|
|
|
static PyMemberDef super_members[] = {
|
|
{"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
|
|
"the class invoking super()"},
|
|
{"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
|
|
"the instance invoking super(); may be None"},
|
|
{"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
|
|
"the type of the instance invoking super(); may be None"},
|
|
{0}
|
|
};
|
|
|
|
static void
|
|
super_dealloc(PyObject *self)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
|
|
_PyObject_GC_UNTRACK(self);
|
|
Py_XDECREF(su->obj);
|
|
Py_XDECREF(su->type);
|
|
Py_XDECREF(su->obj_type);
|
|
self->ob_type->tp_free(self);
|
|
}
|
|
|
|
static PyObject *
|
|
super_repr(PyObject *self)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
|
|
if (su->obj_type)
|
|
return PyString_FromFormat(
|
|
"<super: <class '%s'>, <%s object>>",
|
|
su->type ? su->type->tp_name : "NULL",
|
|
su->obj_type->tp_name);
|
|
else
|
|
return PyString_FromFormat(
|
|
"<super: <class '%s'>, NULL>",
|
|
su->type ? su->type->tp_name : "NULL");
|
|
}
|
|
|
|
static PyObject *
|
|
super_getattro(PyObject *self, PyObject *name)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
int skip = su->obj_type == NULL;
|
|
|
|
if (!skip) {
|
|
/* We want __class__ to return the class of the super object
|
|
(i.e. super, or a subclass), not the class of su->obj. */
|
|
skip = (PyString_Check(name) &&
|
|
PyString_GET_SIZE(name) == 9 &&
|
|
strcmp(PyString_AS_STRING(name), "__class__") == 0);
|
|
}
|
|
|
|
if (!skip) {
|
|
PyObject *mro, *res, *tmp, *dict;
|
|
PyTypeObject *starttype;
|
|
descrgetfunc f;
|
|
Py_ssize_t i, n;
|
|
|
|
starttype = su->obj_type;
|
|
mro = starttype->tp_mro;
|
|
|
|
if (mro == NULL)
|
|
n = 0;
|
|
else {
|
|
assert(PyTuple_Check(mro));
|
|
n = PyTuple_GET_SIZE(mro);
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
|
|
break;
|
|
}
|
|
i++;
|
|
res = NULL;
|
|
for (; i < n; i++) {
|
|
tmp = PyTuple_GET_ITEM(mro, i);
|
|
if (PyType_Check(tmp))
|
|
dict = ((PyTypeObject *)tmp)->tp_dict;
|
|
else
|
|
continue;
|
|
res = PyDict_GetItem(dict, name);
|
|
if (res != NULL) {
|
|
Py_INCREF(res);
|
|
f = res->ob_type->tp_descr_get;
|
|
if (f != NULL) {
|
|
tmp = f(res,
|
|
/* Only pass 'obj' param if
|
|
this is instance-mode super
|
|
(See SF ID #743627)
|
|
*/
|
|
(su->obj == (PyObject *)
|
|
su->obj_type
|
|
? (PyObject *)NULL
|
|
: su->obj),
|
|
(PyObject *)starttype);
|
|
Py_DECREF(res);
|
|
res = tmp;
|
|
}
|
|
return res;
|
|
}
|
|
}
|
|
}
|
|
return PyObject_GenericGetAttr(self, name);
|
|
}
|
|
|
|
static PyTypeObject *
|
|
supercheck(PyTypeObject *type, PyObject *obj)
|
|
{
|
|
/* Check that a super() call makes sense. Return a type object.
|
|
|
|
obj can be a new-style class, or an instance of one:
|
|
|
|
- If it is a class, it must be a subclass of 'type'. This case is
|
|
used for class methods; the return value is obj.
|
|
|
|
- If it is an instance, it must be an instance of 'type'. This is
|
|
the normal case; the return value is obj.__class__.
|
|
|
|
But... when obj is an instance, we want to allow for the case where
|
|
obj->ob_type is not a subclass of type, but obj.__class__ is!
|
|
This will allow using super() with a proxy for obj.
|
|
*/
|
|
|
|
/* Check for first bullet above (special case) */
|
|
if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
|
|
Py_INCREF(obj);
|
|
return (PyTypeObject *)obj;
|
|
}
|
|
|
|
/* Normal case */
|
|
if (PyType_IsSubtype(obj->ob_type, type)) {
|
|
Py_INCREF(obj->ob_type);
|
|
return obj->ob_type;
|
|
}
|
|
else {
|
|
/* Try the slow way */
|
|
static PyObject *class_str = NULL;
|
|
PyObject *class_attr;
|
|
|
|
if (class_str == NULL) {
|
|
class_str = PyString_FromString("__class__");
|
|
if (class_str == NULL)
|
|
return NULL;
|
|
}
|
|
|
|
class_attr = PyObject_GetAttr(obj, class_str);
|
|
|
|
if (class_attr != NULL &&
|
|
PyType_Check(class_attr) &&
|
|
(PyTypeObject *)class_attr != obj->ob_type)
|
|
{
|
|
int ok = PyType_IsSubtype(
|
|
(PyTypeObject *)class_attr, type);
|
|
if (ok)
|
|
return (PyTypeObject *)class_attr;
|
|
}
|
|
|
|
if (class_attr == NULL)
|
|
PyErr_Clear();
|
|
else
|
|
Py_DECREF(class_attr);
|
|
}
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
"super(type, obj): "
|
|
"obj must be an instance or subtype of type");
|
|
return NULL;
|
|
}
|
|
|
|
static PyObject *
|
|
super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
superobject *newobj;
|
|
|
|
if (obj == NULL || obj == Py_None || su->obj != NULL) {
|
|
/* Not binding to an object, or already bound */
|
|
Py_INCREF(self);
|
|
return self;
|
|
}
|
|
if (su->ob_type != &PySuper_Type)
|
|
/* If su is an instance of a (strict) subclass of super,
|
|
call its type */
|
|
return PyObject_CallFunctionObjArgs((PyObject *)su->ob_type,
|
|
su->type, obj, NULL);
|
|
else {
|
|
/* Inline the common case */
|
|
PyTypeObject *obj_type = supercheck(su->type, obj);
|
|
if (obj_type == NULL)
|
|
return NULL;
|
|
newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
|
|
NULL, NULL);
|
|
if (newobj == NULL)
|
|
return NULL;
|
|
Py_INCREF(su->type);
|
|
Py_INCREF(obj);
|
|
newobj->type = su->type;
|
|
newobj->obj = obj;
|
|
newobj->obj_type = obj_type;
|
|
return (PyObject *)newobj;
|
|
}
|
|
}
|
|
|
|
static int
|
|
super_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
PyTypeObject *type;
|
|
PyObject *obj = NULL;
|
|
PyTypeObject *obj_type = NULL;
|
|
|
|
if (!_PyArg_NoKeywords("super", kwds))
|
|
return -1;
|
|
if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
|
|
return -1;
|
|
if (obj == Py_None)
|
|
obj = NULL;
|
|
if (obj != NULL) {
|
|
obj_type = supercheck(type, obj);
|
|
if (obj_type == NULL)
|
|
return -1;
|
|
Py_INCREF(obj);
|
|
}
|
|
Py_INCREF(type);
|
|
su->type = type;
|
|
su->obj = obj;
|
|
su->obj_type = obj_type;
|
|
return 0;
|
|
}
|
|
|
|
PyDoc_STRVAR(super_doc,
|
|
"super(type) -> unbound super object\n"
|
|
"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
|
|
"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
|
|
"Typical use to call a cooperative superclass method:\n"
|
|
"class C(B):\n"
|
|
" def meth(self, arg):\n"
|
|
" super(C, self).meth(arg)");
|
|
|
|
static int
|
|
super_traverse(PyObject *self, visitproc visit, void *arg)
|
|
{
|
|
superobject *su = (superobject *)self;
|
|
|
|
Py_VISIT(su->obj);
|
|
Py_VISIT(su->type);
|
|
Py_VISIT(su->obj_type);
|
|
|
|
return 0;
|
|
}
|
|
|
|
PyTypeObject PySuper_Type = {
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
0, /* ob_size */
|
|
"super", /* tp_name */
|
|
sizeof(superobject), /* tp_basicsize */
|
|
0, /* tp_itemsize */
|
|
/* methods */
|
|
super_dealloc, /* tp_dealloc */
|
|
0, /* tp_print */
|
|
0, /* tp_getattr */
|
|
0, /* tp_setattr */
|
|
0, /* tp_compare */
|
|
super_repr, /* tp_repr */
|
|
0, /* tp_as_number */
|
|
0, /* tp_as_sequence */
|
|
0, /* tp_as_mapping */
|
|
0, /* tp_hash */
|
|
0, /* tp_call */
|
|
0, /* tp_str */
|
|
super_getattro, /* tp_getattro */
|
|
0, /* tp_setattro */
|
|
0, /* tp_as_buffer */
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
super_doc, /* tp_doc */
|
|
super_traverse, /* tp_traverse */
|
|
0, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
0, /* tp_methods */
|
|
super_members, /* tp_members */
|
|
0, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
super_descr_get, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
0, /* tp_dictoffset */
|
|
super_init, /* tp_init */
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
PyType_GenericNew, /* tp_new */
|
|
PyObject_GC_Del, /* tp_free */
|
|
};
|