annotate Python/ast.c @ 40450:cc992d75d5b3

Four months of trunk changes (including a few releases...) 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) ........
author Thomas Wouters <thomas@python.org>
date Wed, 13 Dec 2006 04:49:30 +0000
parents 1cce0526d9b0
children 54cd47788811
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2 * This file includes functions to transform a concrete syntax tree (CST) to
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4 *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
5 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
6 #include "Python.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
7 #include "Python-ast.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8 #include "grammar.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 #include "node.h"
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
10 #include "pyarena.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 #include "ast.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 #include "token.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13 #include "parsetok.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 #include "graminit.h"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16 #include <assert.h>
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 /* Data structure used internally */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 struct compiling {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
20 char *c_encoding; /* source encoding */
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
21 PyArena *c_arena; /* arena for allocating memeory */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 };
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 static asdl_seq *seq_for_testlist(struct compiling *, const node *);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25 static expr_ty ast_for_expr(struct compiling *, const node *);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 static stmt_ty ast_for_stmt(struct compiling *, const node *);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 static asdl_seq *ast_for_suite(struct compiling *, const node *);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
28 static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
29 static expr_ty ast_for_testlist(struct compiling *, const node *);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
30 static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
32 /* Note different signature for ast_for_call */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 static PyObject *parsenumber(const char *);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36 static PyObject *parsestr(const char *s, const char *encoding);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37 static PyObject *parsestrplus(struct compiling *, const node *n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
38
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39 #ifndef LINENO
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
40 #define LINENO(n) ((n)->n_lineno)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
43 static identifier
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
44 new_identifier(const char* n, PyArena *arena) {
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
45 PyObject* id = PyString_InternFromString(n);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
46 PyArena_AddPyObject(arena, id);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
47 return id;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
49
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
50 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
51
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
52 /* This routine provides an invalid object for the syntax error.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53 The outermost routine must unpack this error and create the
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
54 proper object. We do this so that we don't have to pass
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
55 the filename to everything function.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
56
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
57 XXX Maybe we should just pass the filename...
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
58 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
59
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
60 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61 ast_error(const node *n, const char *errstr)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
62 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64 if (!u)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
65 return 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 PyErr_SetObject(PyExc_SyntaxError, u);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67 Py_DECREF(u);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
70
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71 static void
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
72 ast_error_finish(const char *filename)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
73 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74 PyObject *type, *value, *tback, *errstr, *loc, *tmp;
36231
d3f4c467e439 Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents: 36224
diff changeset
75 long lineno;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
76
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77 assert(PyErr_Occurred());
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
78 if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
79 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
80
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81 PyErr_Fetch(&type, &value, &tback);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82 errstr = PyTuple_GetItem(value, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
83 if (!errstr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
84 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85 Py_INCREF(errstr);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
86 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
87 if (lineno == -1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
88 Py_DECREF(errstr);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
89 return;
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
90 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91 Py_DECREF(value);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
92
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
93 loc = PyErr_ProgramText(filename, lineno);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
94 if (!loc) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
95 Py_INCREF(Py_None);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
96 loc = Py_None;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97 }
36231
d3f4c467e439 Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents: 36224
diff changeset
98 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
99 Py_DECREF(loc);
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
100 if (!tmp) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
101 Py_DECREF(errstr);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
102 return;
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
103 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
104 value = PyTuple_Pack(2, errstr, tmp);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
105 Py_DECREF(errstr);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
106 Py_DECREF(tmp);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
107 if (!value)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
108 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
109 PyErr_Restore(type, value, tback);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
110 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
111
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
112 /* num_stmts() returns number of contained statements.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
113
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
114 Use this routine to determine how big a sequence is needed for
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
115 the statements in a parse tree. Its raison d'etre is this bit of
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
116 grammar:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
117
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
118 stmt: simple_stmt | compound_stmt
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
119 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
120
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
121 A simple_stmt can contain multiple small_stmt elements joined
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
122 by semicolons. If the arg is a simple_stmt, the number of
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
123 small_stmt elements is returned.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
124 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
125
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
126 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
127 num_stmts(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
128 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
129 int i, l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
130 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
131
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
132 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133 case single_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
134 if (TYPE(CHILD(n, 0)) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
135 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
136 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
137 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
138 case file_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
139 l = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
140 for (i = 0; i < NCH(n); i++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
141 ch = CHILD(n, i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
142 if (TYPE(ch) == stmt)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
143 l += num_stmts(ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
144 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
145 return l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
146 case stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
147 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
148 case compound_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
149 return 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
150 case simple_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
151 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
152 case suite:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
153 if (NCH(n) == 1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
154 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
155 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
156 l = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
157 for (i = 2; i < (NCH(n) - 1); i++)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
158 l += num_stmts(CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
159 return l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
160 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
161 default: {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
162 char buf[128];
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
163
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
164 sprintf(buf, "Non-statement found: %d %d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
165 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
166 Py_FatalError(buf);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
167 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
168 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
169 assert(0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
170 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
171 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
172
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
173 /* Transform the CST rooted at node * to the appropriate AST
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
174 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
175
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
176 mod_ty
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
177 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
178 PyArena *arena)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
179 {
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
180 int i, j, k, num;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
181 asdl_seq *stmts = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
182 stmt_ty s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
183 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
184 struct compiling c;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
185
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
186 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
187 c.c_encoding = "utf-8";
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
188 if (TYPE(n) == encoding_decl) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
189 ast_error(n, "encoding declaration in Unicode string");
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
190 goto error;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
191 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
192 } else if (TYPE(n) == encoding_decl) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
193 c.c_encoding = STR(n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
194 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
195 } else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
196 c.c_encoding = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
197 }
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
198 c.c_arena = arena;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
199
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
200 k = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
201 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
202 case file_input:
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
203 stmts = asdl_seq_new(num_stmts(n), arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
204 if (!stmts)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
205 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
206 for (i = 0; i < NCH(n) - 1; i++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
207 ch = CHILD(n, i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
208 if (TYPE(ch) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
209 continue;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
210 REQ(ch, stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
211 num = num_stmts(ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
212 if (num == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
213 s = ast_for_stmt(&c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
214 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
215 goto error;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
216 asdl_seq_SET(stmts, k++, s);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
217 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
218 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
219 ch = CHILD(ch, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
220 REQ(ch, simple_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
221 for (j = 0; j < num; j++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
222 s = ast_for_stmt(&c, CHILD(ch, j * 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
223 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
224 goto error;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
225 asdl_seq_SET(stmts, k++, s);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
226 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
227 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
228 }
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
229 return Module(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
230 case eval_input: {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
231 expr_ty testlist_ast;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
232
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
233 /* XXX Why not gen_for here? */
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
234 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
235 if (!testlist_ast)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
236 goto error;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
237 return Expression(testlist_ast, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
238 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
239 case single_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
240 if (TYPE(CHILD(n, 0)) == NEWLINE) {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
241 stmts = asdl_seq_new(1, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
242 if (!stmts)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
243 goto error;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
244 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
245 arena));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
246 return Interactive(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
247 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
248 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
249 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
250 num = num_stmts(n);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
251 stmts = asdl_seq_new(num, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
252 if (!stmts)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
253 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
254 if (num == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
255 s = ast_for_stmt(&c, n);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
256 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
257 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
258 asdl_seq_SET(stmts, 0, s);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
259 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
260 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
261 /* Only a simple_stmt can contain multiple statements. */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
262 REQ(n, simple_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
263 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
264 if (TYPE(CHILD(n, i)) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
265 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
266 s = ast_for_stmt(&c, CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
267 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
268 goto error;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
269 asdl_seq_SET(stmts, i / 2, s);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
270 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
271 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
272
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
273 return Interactive(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
274 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
275 default:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
276 goto error;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
277 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
278 error:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
279 ast_error_finish(filename);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
280 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
281 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
282
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
283 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
284 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
285
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
286 static operator_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
287 get_operator(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
288 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
289 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
290 case VBAR:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
291 return BitOr;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
292 case CIRCUMFLEX:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
293 return BitXor;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
294 case AMPER:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
295 return BitAnd;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
296 case LEFTSHIFT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
297 return LShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
298 case RIGHTSHIFT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
299 return RShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
300 case PLUS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
301 return Add;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
302 case MINUS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
303 return Sub;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
304 case STAR:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
305 return Mult;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
306 case SLASH:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
307 return Div;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
308 case DOUBLESLASH:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
309 return FloorDiv;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
310 case PERCENT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
311 return Mod;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
312 default:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
313 return (operator_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
314 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
315 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
316
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
317 /* Set the context ctx for expr_ty e, recursively traversing e.
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
318
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
319 Only sets context for expr kinds that "can appear in assignment context"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
320 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
321 an appropriate syntax error and returns false.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
322 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
323
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
324 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
325 set_context(expr_ty e, expr_context_ty ctx, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
326 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
327 asdl_seq *s = NULL;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
328 /* If a particular expression type can't be used for assign / delete,
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
329 set expr_name to its name and an error message will be generated.
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
330 */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
331 const char* expr_name = NULL;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
332
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
333 /* The ast defines augmented store and load contexts, but the
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
334 implementation here doesn't actually use them. The code may be
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
335 a little more complex than necessary as a result. It also means
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
336 that expressions in an augmented assignment have a Store context.
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
337 Consider restructuring so that augmented assignment uses
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
338 set_context(), too.
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
339 */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
340 assert(ctx != AugStore && ctx != AugLoad);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
341
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
342 switch (e->kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
343 case Attribute_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
344 if (ctx == Store &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
345 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
346 return ast_error(n, "assignment to None");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
347 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
348 e->v.Attribute.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
349 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
350 case Subscript_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
351 e->v.Subscript.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
352 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
353 case Name_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
354 if (ctx == Store &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
355 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
356 return ast_error(n, "assignment to None");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
357 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
358 e->v.Name.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
359 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
360 case List_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
361 e->v.List.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
362 s = e->v.List.elts;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
363 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
364 case Tuple_kind:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
365 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
366 return ast_error(n, "can't assign to ()");
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
367 e->v.Tuple.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
368 s = e->v.Tuple.elts;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
369 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
370 case Lambda_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
371 expr_name = "lambda";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
372 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
373 case Call_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
374 expr_name = "function call";
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
375 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
376 case BoolOp_kind:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
377 case BinOp_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
378 case UnaryOp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
379 expr_name = "operator";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
380 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
381 case GeneratorExp_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
382 expr_name = "generator expression";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
383 break;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
384 case Yield_kind:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
385 expr_name = "yield expression";
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
386 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
387 case ListComp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
388 expr_name = "list comprehension";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
389 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
390 case Dict_kind:
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
391 case Set_kind:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
392 case Num_kind:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
393 case Str_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
394 expr_name = "literal";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
395 break;
39783
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
396 case Ellipsis_kind:
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
397 expr_name = "Ellipsis";
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
398 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
399 case Compare_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
400 expr_name = "comparison";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
401 break;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
402 case IfExp_kind:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
403 expr_name = "conditional expression";
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
404 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
405 default:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
406 PyErr_Format(PyExc_SystemError,
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
407 "unexpected expression in assignment %d (line %d)",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
408 e->kind, e->lineno);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
409 return 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
410 }
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
411 /* Check for error string set by switch */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
412 if (expr_name) {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
413 char buf[300];
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
414 PyOS_snprintf(buf, sizeof(buf),
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
415 "can't %s %s",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
416 ctx == Store ? "assign to" : "delete",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
417 expr_name);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
418 return ast_error(n, buf);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
419 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
420
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
421 /* If the LHS is a list or tuple, we need to set the assignment
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
422 context for all the contained elements.
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
423 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
424 if (s) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
425 int i;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
426
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
427 for (i = 0; i < asdl_seq_LEN(s); i++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
428 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
429 return 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
430 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
431 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
432 return 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
433 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
434
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
435 static operator_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
436 ast_for_augassign(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
437 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
438 REQ(n, augassign);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
439 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
440 switch (STR(n)[0]) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
441 case '+':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
442 return Add;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
443 case '-':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
444 return Sub;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
445 case '/':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
446 if (STR(n)[1] == '/')
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
447 return FloorDiv;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
448 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
449 return Div;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
450 case '%':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
451 return Mod;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
452 case '<':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
453 return LShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
454 case '>':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
455 return RShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
456 case '&':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
457 return BitAnd;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
458 case '^':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
459 return BitXor;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
460 case '|':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
461 return BitOr;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
462 case '*':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
463 if (STR(n)[1] == '*')
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
464 return Pow;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
465 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
466 return Mult;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
467 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
468 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
469 return (operator_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
470 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
471 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
472
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
473 static cmpop_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
474 ast_for_comp_op(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
475 {
39648
efe3f2c0da5e Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
476 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
477 |'is' 'not'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
478 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
479 REQ(n, comp_op);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
480 if (NCH(n) == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
481 n = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
482 switch (TYPE(n)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
483 case LESS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
484 return Lt;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
485 case GREATER:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
486 return Gt;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
487 case EQEQUAL: /* == */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
488 return Eq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
489 case LESSEQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
490 return LtE;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
491 case GREATEREQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
492 return GtE;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
493 case NOTEQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
494 return NotEq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
495 case NAME:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
496 if (strcmp(STR(n), "in") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
497 return In;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
498 if (strcmp(STR(n), "is") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
499 return Is;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
500 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
501 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
502 STR(n));
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
503 return (cmpop_ty)0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
504 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
505 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
506 else if (NCH(n) == 2) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
507 /* handle "not in" and "is not" */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
508 switch (TYPE(CHILD(n, 0))) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
509 case NAME:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
510 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
511 return NotIn;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
512 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
513 return IsNot;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
514 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
515 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
516 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
517 return (cmpop_ty)0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
518 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
519 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
520 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
521 NCH(n));
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
522 return (cmpop_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
523 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
524
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
525 static asdl_seq *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
526 seq_for_testlist(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
527 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
528 /* testlist: test (',' test)* [','] */
35759
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
529 asdl_seq *seq;
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
530 expr_ty expression;
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
531 int i;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
532 assert(TYPE(n) == testlist
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
533 || TYPE(n) == listmaker
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
534 || TYPE(n) == testlist_gexp
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
535 || TYPE(n) == testlist_safe
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
536 || TYPE(n) == testlist1
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
537 );
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
538
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
539 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
540 if (!seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
541 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
542
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
543 for (i = 0; i < NCH(n); i += 2) {
36670
296cda9b3e2f Fix assertion errors in debug build, brought on by PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36666
diff changeset
544 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == old_test);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
545
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
546 expression = ast_for_expr(c, CHILD(n, i));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
547 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
548 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
549
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
550 assert(i / 2 < seq->size);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
551 asdl_seq_SET(seq, i / 2, expression);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
552 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
553 return seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
554 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
555
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
556 static expr_ty
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
557 compiler_complex_args(struct compiling *c, const node *n)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
558 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
559 int i, len = (NCH(n) + 1) / 2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
560 expr_ty result;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
561 asdl_seq *args = asdl_seq_new(len, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
562 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
563 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
564
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
565 /* fpdef: NAME | '(' fplist ')'
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
566 fplist: fpdef (',' fpdef)* [',']
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
567 */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
568 REQ(n, fplist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
569 for (i = 0; i < len; i++) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
570 const node *fpdef_node = CHILD(n, 2*i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
571 const node *child;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
572 expr_ty arg;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
573 set_name:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
574 /* fpdef_node is either a NAME or an fplist */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
575 child = CHILD(fpdef_node, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
576 if (TYPE(child) == NAME) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
577 if (!strcmp(STR(child), "None")) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
578 ast_error(child, "assignment to None");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
579 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
580 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
581 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
582 child->n_col_offset, c->c_arena);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
583 }
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
584 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
585 assert(TYPE(fpdef_node) == fpdef);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
586 /* fpdef_node[0] is not a name, so it must be a '(', get CHILD[1] */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
587 child = CHILD(fpdef_node, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
588 assert(TYPE(child) == fplist);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
589 /* NCH == 1 means we have (x), we need to elide the extra parens */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
590 if (NCH(child) == 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
591 fpdef_node = CHILD(child, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
592 assert(TYPE(fpdef_node) == fpdef);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
593 goto set_name;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
594 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
595 arg = compiler_complex_args(c, child);
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
596 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
597 asdl_seq_SET(args, i, arg);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
598 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
599
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
600 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena);
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
601 if (!set_context(result, Store, n))
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
602 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
603 return result;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
604 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
605
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
606 /* returns -1 if failed to handle keyword only arguments
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
607 returns new position to keep processing if successful
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
608 (',' NAME ['=' test])*
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
609 ^^^
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
610 start pointing here
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
611 */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
612 static int
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
613 handle_keywordonly_args(struct compiling *c, const node *n, int start,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
614 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
615 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
616 node *ch;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
617 expr_ty name;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
618 int i = start;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
619 int j = 0; /* index for kwdefaults and kwonlyargs */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
620 assert(kwonlyargs != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
621 assert(kwdefaults != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
622 while (i < NCH(n)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
623 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
624 switch (TYPE(ch)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
625 case NAME:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
626 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
627 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
628 if (!expression) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
629 ast_error(ch, "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
630 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
631 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
632 asdl_seq_SET(kwdefaults, j, expression);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
633 i += 2; /* '=' and test */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
634 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
635 else { /* setting NULL if no default value exists */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
636 asdl_seq_SET(kwdefaults, j, NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
637 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
638 if (!strcmp(STR(ch), "None")) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
639 ast_error(ch, "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
640 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
641 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
642 name = Name(NEW_IDENTIFIER(ch),
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
643 Param, LINENO(ch), ch->n_col_offset,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
644 c->c_arena);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
645 if (!name) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
646 ast_error(ch, "expecting name");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
647 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
648 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
649 asdl_seq_SET(kwonlyargs, j++, name);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
650 i += 2; /* the name and the comma */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
651 break;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
652 case DOUBLESTAR:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
653 return i;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
654 default:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
655 ast_error(ch, "unexpected node");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
656 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
657 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
658 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
659 return i;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
660 error:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
661 return -1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
662 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
663
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
664 /* Create AST for argument list. */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
665
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
666 static arguments_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
667 ast_for_arguments(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
668 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
669 /* parameters: '(' [varargslist] ')'
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
670 varargslist: (fpdef ['=' test] ',')*
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
671 ('*' [NAME] (',' fpdef ['=' test])* [',' '**' NAME] | '**' NAME)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
672 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
673 */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
674 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
675 int nposdefaults = 0, found_default = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
676 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
677 identifier vararg = NULL, kwarg = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
678 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
679
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
680 if (TYPE(n) == parameters) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
681 if (NCH(n) == 2) /* () as argument list */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
682 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
683 n = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
684 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
685 REQ(n, varargslist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
686
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
687 /* first count the number of positional args & defaults */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
688 for (i = 0; i < NCH(n); i++) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
689 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
690 if (TYPE(ch) == STAR) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
691 if (TYPE(CHILD(n, i+1)) == NAME) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
692 /* skip NAME of vararg */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
693 /* so that following can count only keyword only args */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
694 i += 2;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
695 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
696 else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
697 i++;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
698 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
699 break;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
700 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
701 if (TYPE(ch) == fpdef) nposargs++;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
702 if (TYPE(ch) == EQUAL) nposdefaults++;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
703 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
704 /* count the number of keyword only args &
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
705 defaults for keyword only args */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
706 for ( ; i < NCH(n); ++i) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
707 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
708 if (TYPE(ch) == DOUBLESTAR) break;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
709 if (TYPE(ch) == NAME) nkwonlyargs++;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
710 }
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
711
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
712 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
713 if (!posargs && nposargs)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
714 return NULL; /* Don't need to goto error; no objects allocated */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
715 kwonlyargs = (nkwonlyargs ?
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
716 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
717 if (!kwonlyargs && nkwonlyargs)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
718 return NULL; /* Don't need to goto error; no objects allocated */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
719 posdefaults = (nposdefaults ?
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
720 asdl_seq_new(nposdefaults, c->c_arena) : NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
721 if (!posdefaults && nposdefaults)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
722 return NULL; /* Don't need to goto error; no objects allocated */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
723 /* The length of kwonlyargs and kwdefaults are same
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
724 since we set NULL as default for keyword only argument w/o default
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
725 - we have sequence data structure, but no dictionary */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
726 kwdefaults = (nkwonlyargs ?
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
727 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
728 if (!kwdefaults && nkwonlyargs)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
729 return NULL; /* Don't need to goto error; no objects allocated */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
730
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
731 if (nposargs + nkwonlyargs > 255) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
732 ast_error(n, "more than 255 arguments");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
733 return NULL;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
734 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
735
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
736 /* fpdef: NAME | '(' fplist ')'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
737 fplist: fpdef (',' fpdef)* [',']
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
738 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
739 i = 0;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
740 j = 0; /* index for defaults */
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
741 k = 0; /* index for args */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
742 while (i < NCH(n)) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
743 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
744 switch (TYPE(ch)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
745 case fpdef:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
746 handle_fpdef:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
747 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
748 anything other than EQUAL or a comma? */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
749 /* XXX Should NCH(n) check be made a separate check? */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
750 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
751 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
752 if (!expression)
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
753 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
754 assert(posdefaults != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
755 asdl_seq_SET(posdefaults, j++, expression);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
756
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
757 i += 2;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
758 found_default = 1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
759 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
760 else if (found_default) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
761 ast_error(n,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
762 "non-default argument follows default argument");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
763 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
764 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
765 if (NCH(ch) == 3) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
766 ch = CHILD(ch, 1);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
767 /* def foo((x)): is not complex, special case. */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
768 if (NCH(ch) != 1) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
769 /* We have complex arguments, setup for unpacking. */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
770 asdl_seq_SET(posargs, k++,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
771 compiler_complex_args(c, ch));
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
772 } else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
773 /* def foo((x)): setup for checking NAME below. */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
774 /* Loop because there can be many parens and tuple
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
775 unpacking mixed in. */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
776 ch = CHILD(ch, 0);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
777 assert(TYPE(ch) == fpdef);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
778 goto handle_fpdef;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
779 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
780 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
781 if (TYPE(CHILD(ch, 0)) == NAME) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
782 expr_ty name;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
783 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
784 ast_error(CHILD(ch, 0), "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
785 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
786 }
35759
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
787 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
788 Param, LINENO(ch), ch->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
789 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
790 if (!name)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
791 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
792 asdl_seq_SET(posargs, k++, name);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
793
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
794 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
795 i += 2; /* the name and the comma */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
796 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
797 case STAR:
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
798 if (i+1 >= NCH(n)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
799 ast_error(CHILD(n, i), "no name for vararg");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
800 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
801 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
802 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
803 ast_error(CHILD(n, i+1), "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
804 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
805 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
806 if (TYPE(CHILD(n, i+1)) == COMMA) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
807 int res = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
808 i += 2; /* now follows keyword only arguments */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
809 res = handle_keywordonly_args(c, n, i,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
810 kwonlyargs, kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
811 if (res == -1) goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
812 i = res; /* res has new position to process */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
813 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
814 else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
815 vararg = NEW_IDENTIFIER(CHILD(n, i+1));
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
816 i += 3;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
817 if (i < NCH(n) && TYPE(CHILD(n, i)) == NAME) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
818 int res = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
819 res = handle_keywordonly_args(c, n, i,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
820 kwonlyargs, kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
821 if (res == -1) goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
822 i = res; /* res has new position to process */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
823 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
824 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
825 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
826 case DOUBLESTAR:
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
827 if (!strcmp(STR(CHILD(n, i+1)), "None")) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
828 ast_error(CHILD(n, i+1), "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
829 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
830 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
831 kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
832 i += 3;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
833 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
834 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
835 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
836 "unexpected node in varargslist: %d @ %d",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
837 TYPE(ch), i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
838 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
839 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
840 }
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
841 return arguments(posargs, vararg, kwonlyargs, kwarg,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
842 posdefaults, kwdefaults, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
843 error:
35873
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
844 Py_XDECREF(vararg);
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
845 Py_XDECREF(kwarg);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
846 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
847 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
848
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
849 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
850 ast_for_dotted_name(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
851 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
852 expr_ty e;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
853 identifier id;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
854 int lineno, col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
855 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
856
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
857 REQ(n, dotted_name);
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
858
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
859 lineno = LINENO(n);
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
860 col_offset = n->n_col_offset;
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
861
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
862 id = NEW_IDENTIFIER(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
863 if (!id)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
864 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
865 e = Name(id, Load, lineno, col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
866 if (!e)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
867 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
868
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
869 for (i = 2; i < NCH(n); i+=2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
870 id = NEW_IDENTIFIER(CHILD(n, i));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
871 if (!id)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
872 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
873 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
874 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
875 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
876 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
877
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
878 return e;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
879 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
880
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
881 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
882 ast_for_decorator(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
883 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
884 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
885 expr_ty d = NULL;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
886 expr_ty name_expr;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
887
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
888 REQ(n, decorator);
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
889 REQ(CHILD(n, 0), AT);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
890 REQ(RCHILD(n, -1), NEWLINE);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
891
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
892 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
893 if (!name_expr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
894 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
895
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
896 if (NCH(n) == 3) { /* No arguments */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
897 d = name_expr;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
898 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
899 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
900 else if (NCH(n) == 5) { /* Call with no arguments */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
901 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
902 n->n_col_offset, c->c_arena);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
903 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
904 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
905 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
906 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
907 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
908 d = ast_for_call(c, CHILD(n, 3), name_expr);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
909 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
910 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
911 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
912 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
913
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
914 return d;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
915 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
916
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
917 static asdl_seq*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
918 ast_for_decorators(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
919 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
920 asdl_seq* decorator_seq;
35873
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
921 expr_ty d;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
922 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
923
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
924 REQ(n, decorators);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
925 decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
926 if (!decorator_seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
927 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
928
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
929 for (i = 0; i < NCH(n); i++) {
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
930 d = ast_for_decorator(c, CHILD(n, i));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
931 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
932 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
933 asdl_seq_SET(decorator_seq, i, d);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
934 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
935 return decorator_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
936 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
937
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
938 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
939 ast_for_funcdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
940 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
941 /* funcdef: 'def' [decorators] NAME parameters ':' suite */
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
942 identifier name;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
943 arguments_ty args;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
944 asdl_seq *body;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
945 asdl_seq *decorator_seq = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
946 int name_i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
947
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
948 REQ(n, funcdef);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
949
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
950 if (NCH(n) == 6) { /* decorators are present */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
951 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
952 if (!decorator_seq)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
953 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
954 name_i = 2;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
955 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
956 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
957 name_i = 1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
958 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
959
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
960 name = NEW_IDENTIFIER(CHILD(n, name_i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
961 if (!name)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
962 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
963 else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
964 ast_error(CHILD(n, name_i), "assignment to None");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
965 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
966 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
967 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
968 if (!args)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
969 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
970 body = ast_for_suite(c, CHILD(n, name_i + 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
971 if (!body)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
972 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
973
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
974 return FunctionDef(name, args, body, decorator_seq, LINENO(n),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
975 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
976 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
977
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
978 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
979 ast_for_lambdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
980 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
981 /* lambdef: 'lambda' [varargslist] ':' test */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
982 arguments_ty args;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
983 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
984
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
985 if (NCH(n) == 3) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
986 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
987 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
988 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
989 expression = ast_for_expr(c, CHILD(n, 2));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
990 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
991 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
992 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
993 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
994 args = ast_for_arguments(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
995 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
996 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
997 expression = ast_for_expr(c, CHILD(n, 3));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
998 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
999 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1000 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1001
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1002 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1003 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1004
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1005 static expr_ty
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1006 ast_for_ifexpr(struct compiling *c, const node *n)
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1007 {
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1008 /* test: or_test 'if' or_test 'else' test */
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1009 expr_ty expression, body, orelse;
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1010
36676
67c74efad8c2 Fix old not-reading-pep-308-right artifact.
Thomas Wouters <thomas@python.org>
parents: 36670
diff changeset
1011 assert(NCH(n) == 5);
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1012 body = ast_for_expr(c, CHILD(n, 0));
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1013 if (!body)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1014 return NULL;
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1015 expression = ast_for_expr(c, CHILD(n, 2));
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1016 if (!expression)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1017 return NULL;
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1018 orelse = ast_for_expr(c, CHILD(n, 4));
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1019 if (!orelse)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1020 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1021 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1022 c->c_arena);
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1023 }
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1024
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1025 /* XXX(nnorwitz): the listcomp and genexpr code should be refactored
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1026 so there is only a single version. Possibly for loops can also re-use
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1027 the code.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1028 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1029
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1030 /* Count the number of 'for' loop in a list comprehension.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1031
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1032 Helper for ast_for_listcomp().
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1033 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1034
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1035 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1036 count_list_fors(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1037 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1038 int n_fors = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1039 node *ch = CHILD(n, 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1040
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1041 count_list_for:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1042 n_fors++;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1043 REQ(ch, list_for);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1044 if (NCH(ch) == 5)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1045 ch = CHILD(ch, 4);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1046 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1047 return n_fors;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1048 count_list_iter:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1049 REQ(ch, list_iter);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1050 ch = CHILD(ch, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1051 if (TYPE(ch) == list_for)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1052 goto count_list_for;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1053 else if (TYPE(ch) == list_if) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1054 if (NCH(ch) == 3) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1055 ch = CHILD(ch, 2);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1056 goto count_list_iter;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1057 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1058 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1059 return n_fors;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1060 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1061
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1062 /* Should never be reached */
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1063 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors");
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1064 return -1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1065 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1066
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1067 /* Count the number of 'if' statements in a list comprehension.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1068
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1069 Helper for ast_for_listcomp().
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1070 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1071
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1072 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1073 count_list_ifs(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1074 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1075 int n_ifs = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1076
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1077 count_list_iter:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1078 REQ(n, list_iter);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1079 if (TYPE(CHILD(n, 0)) == list_for)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1080 return n_ifs;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1081 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1082 REQ(n, list_if);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1083 n_ifs++;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1084 if (NCH(n) == 2)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1085 return n_ifs;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1086 n = CHILD(n, 2);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1087 goto count_list_iter;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1088 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1089
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1090 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1091 ast_for_listcomp(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1092 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1093 /* listmaker: test ( list_for | (',' test)* [','] )
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1094 list_for: 'for' exprlist 'in' testlist_safe [list_iter]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1095 list_iter: list_for | list_if
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1096 list_if: 'if' test [list_iter]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1097 testlist_safe: test [(',' test)+ [',']]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1098 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1099 expr_ty elt;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1100 asdl_seq *listcomps;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1101 int i, n_fors;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1102 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1103
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1104 REQ(n, listmaker);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1105 assert(NCH(n) > 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1106
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1107 elt = ast_for_expr(c, CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1108 if (!elt)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1109 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1110
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1111 n_fors = count_list_fors(n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1112 if (n_fors == -1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1113 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1114
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1115 listcomps = asdl_seq_new(n_fors, c->c_arena);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1116 if (!listcomps)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1117 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1118
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1119 ch = CHILD(n, 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1120 for (i = 0; i < n_fors; i++) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1121 comprehension_ty lc;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1122 asdl_seq *t;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1123 expr_ty expression;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1124 node *for_ch;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1125
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1126 REQ(ch, list_for);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1127
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1128 for_ch = CHILD(ch, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1129 t = ast_for_exprlist(c, for_ch, Store);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1130 if (!t)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1131 return NULL;
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1132 expression = ast_for_testlist(c, CHILD(ch, 3));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1133 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1134 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1135
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1136 /* Check the # of children rather than the length of t, since
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1137 [x for x, in ... ] has 1 element in t, but still requires a Tuple. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1138 if (NCH(for_ch) == 1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1139 lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1140 c->c_arena);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1141 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1142 lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1143 c->c_arena),
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1144 expression, NULL, c->c_arena);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1145 if (!lc)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1146 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1147
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1148 if (NCH(ch) == 5) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1149 int j, n_ifs;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1150 asdl_seq *ifs;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1151
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1152 ch = CHILD(ch, 4);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1153 n_ifs = count_list_ifs(ch);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1154 if (n_ifs == -1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1155 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1156
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1157 ifs = asdl_seq_new(n_ifs, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1158 if (!ifs)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1159 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1160
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1161 for (j = 0; j < n_ifs; j++) {
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1162 REQ(ch, list_iter);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1163 ch = CHILD(ch, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1164 REQ(ch, list_if);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1165
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1166 asdl_seq_SET(ifs, j, ast_for_expr(c, CHILD(ch, 1)));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1167 if (NCH(ch) == 3)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1168 ch = CHILD(ch, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1169 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1170 /* on exit, must guarantee that ch is a list_for */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1171 if (TYPE(ch) == list_iter)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1172 ch = CHILD(ch, 0);
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1173 lc->ifs = ifs;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1174 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1175 asdl_seq_SET(listcomps, i, lc);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1176 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1177
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1178 return ListComp(elt, listcomps, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1179 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1180
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1181 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1182 Count the number of 'for' loops in a generator expression.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1183
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1184 Helper for ast_for_genexp().
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1185 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1186
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1187 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1188 count_gen_fors(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1189 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1190 int n_fors = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1191 node *ch = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1192
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1193 count_gen_for:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1194 n_fors++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1195 REQ(ch, gen_for);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1196 if (NCH(ch) == 5)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1197 ch = CHILD(ch, 4);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1198 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1199 return n_fors;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1200 count_gen_iter:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1201 REQ(ch, gen_iter);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1202 ch = CHILD(ch, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1203 if (TYPE(ch) == gen_for)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1204 goto count_gen_for;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1205 else if (TYPE(ch) == gen_if) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1206 if (NCH(ch) == 3) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1207 ch = CHILD(ch, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1208 goto count_gen_iter;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1209 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1210 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1211 return n_fors;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1212 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1213
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1214 /* Should never be reached */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1215 PyErr_SetString(PyExc_SystemError,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1216 "logic error in count_gen_fors");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1217 return -1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1218 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1219
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1220 /* Count the number of 'if' statements in a generator expression.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1221
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1222 Helper for ast_for_genexp().
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1223 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1224
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1225 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1226 count_gen_ifs(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1227 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1228 int n_ifs = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1229
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1230 while (1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1231 REQ(n, gen_iter);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1232 if (TYPE(CHILD(n, 0)) == gen_for)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1233 return n_ifs;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1234 n = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1235 REQ(n, gen_if);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1236 n_ifs++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1237 if (NCH(n) == 2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1238 return n_ifs;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1239 n = CHILD(n, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1240 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1241 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1242
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1243 /* TODO(jhylton): Combine with list comprehension code? */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1244 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1245 ast_for_genexp(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1246 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1247 /* testlist_gexp: test ( gen_for | (',' test)* [','] )
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1248 argument: [test '='] test [gen_for] # Really [keyword '='] test */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1249 expr_ty elt;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1250 asdl_seq *genexps;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1251 int i, n_fors;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1252 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1253
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1254 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1255 assert(NCH(n) > 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1256
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1257 elt = ast_for_expr(c, CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1258 if (!elt)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1259 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1260
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1261 n_fors = count_gen_fors(n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1262 if (n_fors == -1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1263 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1264
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1265 genexps = asdl_seq_new(n_fors, c->c_arena);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1266 if (!genexps)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1267 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1268
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1269 ch = CHILD(n, 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1270 for (i = 0; i < n_fors; i++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1271 comprehension_ty ge;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1272 asdl_seq *t;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1273 expr_ty expression;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1274 node *for_ch;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1275
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1276 REQ(ch, gen_for);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1277
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1278 for_ch = CHILD(ch, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1279 t = ast_for_exprlist(c, for_ch, Store);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1280 if (!t)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1281 return NULL;
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1282 expression = ast_for_expr(c, CHILD(ch, 3));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1283 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1284 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1285
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1286 /* Check the # of children rather than the length of t, since
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1287 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1288 if (NCH(for_ch) == 1)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1289 ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1290 NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1291 else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1292 ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1293 c->c_arena),
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1294 expression, NULL, c->c_arena);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1295
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1296 if (!ge)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1297 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1298
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1299 if (NCH(ch) == 5) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1300 int j, n_ifs;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1301 asdl_seq *ifs;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1302
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1303 ch = CHILD(ch, 4);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1304 n_ifs = count_gen_ifs(ch);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1305 if (n_ifs == -1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1306 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1307
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1308 ifs = asdl_seq_new(n_ifs, c->c_arena);
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1309 if (!ifs)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1310 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1311
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1312 for (j = 0; j < n_ifs; j++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1313 REQ(ch, gen_iter);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1314 ch = CHILD(ch, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1315 REQ(ch, gen_if);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1316
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1317 expression = ast_for_expr(c, CHILD(ch, 1));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1318 if (!expression)
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1319 return NULL;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1320 asdl_seq_SET(ifs, j, expression);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1321 if (NCH(ch) == 3)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1322 ch = CHILD(ch, 2);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1323 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1324 /* on exit, must guarantee that ch is a gen_for */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1325 if (TYPE(ch) == gen_iter)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1326 ch = CHILD(ch, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1327 ge->ifs = ifs;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1328 }
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1329 asdl_seq_SET(genexps, i, ge);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1330 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1331
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1332 return GeneratorExp(elt, genexps, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1333 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1334
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1335 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1336 ast_for_atom(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1337 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1338 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']'
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1339 | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING+
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1340 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1341 node *ch = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1342
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1343 switch (TYPE(ch)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1344 case NAME:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1345 /* All names start in Load context, but may later be
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1346 changed. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1347 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1348 case STRING: {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1349 PyObject *str = parsestrplus(c, n);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1350 if (!str)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1351 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1352
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1353 PyArena_AddPyObject(c->c_arena, str);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1354 return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1355 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1356 case NUMBER: {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1357 PyObject *pynum = parsenumber(STR(ch));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1358 if (!pynum)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1359 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1360
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1361 PyArena_AddPyObject(c->c_arena, pynum);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1362 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1363 }
39783
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
1364 case DOT:
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
1365 /* Ellipsis */
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
1366 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1367 case LPAR: /* some parenthesized expressions */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1368 ch = CHILD(n, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1369
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1370 if (TYPE(ch) == RPAR)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1371 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1372
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1373 if (TYPE(ch) == yield_expr)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1374 return ast_for_expr(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1375
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1376 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1377 return ast_for_genexp(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1378
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1379 return ast_for_testlist_gexp(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1380 case LSQB: /* list (or list comprehension) */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1381 ch = CHILD(n, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1382
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1383 if (TYPE(ch) == RSQB)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1384 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1385
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1386 REQ(ch, listmaker);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1387 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1388 asdl_seq *elts = seq_for_testlist(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1389 if (!elts)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1390 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1391
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1392 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1393 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1394 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1395 return ast_for_listcomp(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1396 case LBRACE: {
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1397 /* dictsetmaker: test ':' test (',' test ':' test)* [','] |
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1398 * test (',' test)* [','] */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1399 int i, size;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1400 asdl_seq *keys, *values;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1401
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1402 ch = CHILD(n, 1);
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1403 if (NCH(ch) == 1 || (NCH(ch) > 0 && STR(CHILD(ch, 1))[0] == ',')) {
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1404 /* it's a set */
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1405 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1406 keys = asdl_seq_new(size, c->c_arena);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1407 if (!keys)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1408 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1409
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1410 for (i = 0; i < NCH(ch); i += 2) {
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1411 expr_ty expression;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1412 expression = ast_for_expr(c, CHILD(ch, i));
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1413 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1414 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1415 asdl_seq_SET(keys, i / 2, expression);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1416 }
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1417 return Set(keys, LINENO(n), n->n_col_offset, c->c_arena);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1418 } else {
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1419 /* it's a dict */
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1420 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1421 keys = asdl_seq_new(size, c->c_arena);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1422 if (!keys)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1423 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1424
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1425 values = asdl_seq_new(size, c->c_arena);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1426 if (!values)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1427 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1428
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1429 for (i = 0; i < NCH(ch); i += 4) {
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1430 expr_ty expression;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1431
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1432 expression = ast_for_expr(c, CHILD(ch, i));
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1433 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1434 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1435
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1436 asdl_seq_SET(keys, i / 4, expression);
35873
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
1437
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1438 expression = ast_for_expr(c, CHILD(ch, i + 2));
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1439 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1440 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1441
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1442 asdl_seq_SET(values, i / 4, expression);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1443 }
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1444 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1445 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1446 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1447 default:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1448 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1449 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1450 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1451 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1452
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1453 static slice_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1454 ast_for_slice(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1455 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1456 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1457 expr_ty lower = NULL, upper = NULL, step = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1458
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1459 REQ(n, subscript);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1460
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1461 /*
39783
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
1462 subscript: test | [test] ':' [test] [sliceop]
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1463 sliceop: ':' [test]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1464 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1465 ch = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1466 if (NCH(n) == 1 && TYPE(ch) == test) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1467 /* 'step' variable hold no significance in terms of being used over
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1468 other vars */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1469 step = ast_for_expr(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1470 if (!step)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1471 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1472
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1473 return Index(step, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1474 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1475
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1476 if (TYPE(ch) == test) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1477 lower = ast_for_expr(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1478 if (!lower)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1479 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1480 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1481
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1482 /* If there's an upper bound it's in the second or third position. */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1483 if (TYPE(ch) == COLON) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1484 if (NCH(n) > 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1485 node *n2 = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1486
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1487 if (TYPE(n2) == test) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1488 upper = ast_for_expr(c, n2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1489 if (!upper)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1490 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1491 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1492 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1493 } else if (NCH(n) > 2) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1494 node *n2 = CHILD(n, 2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1495
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1496 if (TYPE(n2) == test) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1497 upper = ast_for_expr(c, n2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1498 if (!upper)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1499 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1500 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1501 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1502
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1503 ch = CHILD(n, NCH(n) - 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1504 if (TYPE(ch) == sliceop) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1505 if (NCH(ch) == 1) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1506 /* No expression, so step is None */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1507 ch = CHILD(ch, 0);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1508 step = Name(new_identifier("None", c->c_arena), Load,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1509 LINENO(ch), ch->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1510 if (!step)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1511 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1512 } else {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1513 ch = CHILD(ch, 1);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1514 if (TYPE(ch) == test) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1515 step = ast_for_expr(c, ch);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1516 if (!step)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1517 return NULL;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1518 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1519 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1520 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1521
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1522 return Slice(lower, upper, step, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1523 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1524
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1525 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1526 ast_for_binop(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1527 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1528 /* Must account for a sequence of expressions.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1529 How should A op B op C by represented?
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1530 BinOp(BinOp(A, op, B), op, C).
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1531 */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1532
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1533 int i, nops;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1534 expr_ty expr1, expr2, result;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1535 operator_ty newoperator;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1536
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1537 expr1 = ast_for_expr(c, CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1538 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1539 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1540
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1541 expr2 = ast_for_expr(c, CHILD(n, 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1542 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1543 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1544
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1545 newoperator = get_operator(CHILD(n, 1));
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1546 if (!newoperator)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1547 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1548
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1549 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1550 c->c_arena);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1551 if (!result)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1552 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1553
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1554 nops = (NCH(n) - 1) / 2;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1555 for (i = 1; i < nops; i++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1556 expr_ty tmp_result, tmp;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1557 const node* next_oper = CHILD(n, i * 2 + 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1558
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1559 newoperator = get_operator(next_oper);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1560 if (!newoperator)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1561 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1562
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1563 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1564 if (!tmp)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1565 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1566
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1567 tmp_result = BinOp(result, newoperator, tmp,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1568 LINENO(next_oper), next_oper->n_col_offset,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1569 c->c_arena);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1570 if (!tmp)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1571 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1572 result = tmp_result;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1573 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1574 return result;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1575 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1576
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1577 static expr_ty
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1578 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1579 {
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1580 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1581 subscriptlist: subscript (',' subscript)* [',']
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1582 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1583 */
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1584 REQ(n, trailer);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1585 if (TYPE(CHILD(n, 0)) == LPAR) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1586 if (NCH(n) == 2)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1587 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1588 n->n_col_offset, c->c_arena);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1589 else
36679
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1590 return ast_for_call(c, CHILD(n, 1), left_expr);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1591 }
36679
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1592 else if (TYPE(CHILD(n, 0)) == DOT ) {
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1593 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1594 LINENO(n), n->n_col_offset, c->c_arena);
36679
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1595 }
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1596 else {
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1597 REQ(CHILD(n, 0), LSQB);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1598 REQ(CHILD(n, 2), RSQB);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1599 n = CHILD(n, 1);
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1600 if (NCH(n) == 1) {
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1601 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1602 if (!slc)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1603 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1604 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1605 c->c_arena);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1606 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1607 else {
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1608 /* The grammar is ambiguous here. The ambiguity is resolved
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1609 by treating the sequence as a tuple literal if there are
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1610 no slice features.
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1611 */
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1612 int j;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1613 slice_ty slc;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1614 expr_ty e;
36780
376792bfb57e Fix uninitialized value. (Why are we using bools instead of ints, like we do
Thomas Wouters <thomas@python.org>
parents: 36742
diff changeset
1615 bool simple = true;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1616 asdl_seq *slices, *elts;
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1617 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1618 if (!slices)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1619 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1620 for (j = 0; j < NCH(n); j += 2) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1621 slc = ast_for_slice(c, CHILD(n, j));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1622 if (!slc)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1623 return NULL;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1624 if (slc->kind != Index_kind)
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1625 simple = false;
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1626 asdl_seq_SET(slices, j / 2, slc);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1627 }
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1628 if (!simple) {
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1629 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1630 Load, LINENO(n), n->n_col_offset, c->c_arena);
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1631 }
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1632 /* extract Index values and put them in a Tuple */
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1633 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36786
diff changeset
1634 if (!elts)
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36786
diff changeset
1635 return NULL;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1636 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1637 slc = (slice_ty)asdl_seq_GET(slices, j);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1638 assert(slc->kind == Index_kind && slc->v.Index.value);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1639 asdl_seq_SET(elts, j, slc->v.Index.value);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1640 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1641 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1642 if (!e)
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1643 return NULL;
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1644 return Subscript(left_expr, Index(e, c->c_arena),
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1645 Load, LINENO(n), n->n_col_offset, c->c_arena);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1646 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1647 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1648 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1649
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1650 static expr_ty
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1651 ast_for_factor(struct compiling *c, const node *n)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1652 {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1653 node *pfactor, *ppower, *patom, *pnum;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1654 expr_ty expression;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1655
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1656 /* If the unary - operator is applied to a constant, don't generate
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1657 a UNARY_NEGATIVE opcode. Just store the approriate value as a
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1658 constant. The peephole optimizer already does something like
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1659 this but it doesn't handle the case where the constant is
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1660 (sys.maxint - 1). In that case, we want a PyIntObject, not a
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1661 PyLongObject.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1662 */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1663 if (TYPE(CHILD(n, 0)) == MINUS
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1664 && NCH(n) == 2
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1665 && TYPE((pfactor = CHILD(n, 1))) == factor
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1666 && NCH(pfactor) == 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1667 && TYPE((ppower = CHILD(pfactor, 0))) == power
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1668 && NCH(ppower) == 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1669 && TYPE((patom = CHILD(ppower, 0))) == atom
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1670 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1671 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1672 if (s == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1673 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1674 s[0] = '-';
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1675 strcpy(s + 1, STR(pnum));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1676 PyObject_FREE(STR(pnum));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1677 STR(pnum) = s;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1678 return ast_for_atom(c, patom);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1679 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1680
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1681 expression = ast_for_expr(c, CHILD(n, 1));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1682 if (!expression)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1683 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1684
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1685 switch (TYPE(CHILD(n, 0))) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1686 case PLUS:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1687 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1688 c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1689 case MINUS:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1690 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1691 c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1692 case TILDE:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1693 return UnaryOp(Invert, expression, LINENO(n),
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1694 n->n_col_offset, c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1695 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1696 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1697 TYPE(CHILD(n, 0)));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1698 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1699 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1700
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1701 static expr_ty
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1702 ast_for_power(struct compiling *c, const node *n)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1703 {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1704 /* power: atom trailer* ('**' factor)*
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1705 */
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1706 int i;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1707 expr_ty e, tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1708 REQ(n, power);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1709 e = ast_for_atom(c, CHILD(n, 0));
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1710 if (!e)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1711 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1712 if (NCH(n) == 1)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1713 return e;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1714 for (i = 1; i < NCH(n); i++) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1715 node *ch = CHILD(n, i);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1716 if (TYPE(ch) != trailer)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1717 break;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1718 tmp = ast_for_trailer(c, ch, e);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1719 if (!tmp)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1720 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1721 tmp->lineno = e->lineno;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1722 tmp->col_offset = e->col_offset;
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1723 e = tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1724 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1725 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1726 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1727 if (!f)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1728 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1729 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1730 if (!tmp)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1731 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1732 e = tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1733 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1734 return e;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1735 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1736
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1737 /* Do not name a variable 'expr'! Will cause a compile error.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1738 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1739
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1740 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1741 ast_for_expr(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1742 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1743 /* handle the full range of simple expressions
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1744 test: or_test ['if' or_test 'else' test] | lambdef
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1745 or_test: and_test ('or' and_test)*
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1746 and_test: not_test ('and' not_test)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1747 not_test: 'not' not_test | comparison
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1748 comparison: expr (comp_op expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1749 expr: xor_expr ('|' xor_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1750 xor_expr: and_expr ('^' and_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1751 and_expr: shift_expr ('&' shift_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1752 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1753 arith_expr: term (('+'|'-') term)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1754 term: factor (('*'|'/'|'%'|'//') factor)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1755 factor: ('+'|'-'|'~') factor | power
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1756 power: atom trailer* ('**' factor)*
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1757
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1758 As well as modified versions that exist for backward compatibility,
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1759 to explicitly allow:
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1760 [ x for x in lambda: 0, lambda: 1 ]
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1761 (which would be ambiguous without these extra rules)
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1762
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1763 old_test: or_test | old_lambdef
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1764 old_lambdef: 'lambda' [vararglist] ':' old_test
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1765
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1766 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1767
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1768 asdl_seq *seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1769 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1770
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1771 loop:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1772 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1773 case test:
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1774 case old_test:
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1775 if (TYPE(CHILD(n, 0)) == lambdef ||
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1776 TYPE(CHILD(n, 0)) == old_lambdef)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1777 return ast_for_lambdef(c, CHILD(n, 0));
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1778 else if (NCH(n) > 1)
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1779 return ast_for_ifexpr(c, n);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1780 /* Fallthrough */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1781 case or_test:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1782 case and_test:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1783 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1784 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1785 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1786 }
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1787 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1788 if (!seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1789 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1790 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1791 expr_ty e = ast_for_expr(c, CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1792 if (!e)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1793 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1794 asdl_seq_SET(seq, i / 2, e);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1795 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1796 if (!strcmp(STR(CHILD(n, 1)), "and"))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1797 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1798 c->c_arena);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1799 assert(!strcmp(STR(CHILD(n, 1)), "or"));
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1800 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1801 case not_test:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1802 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1803 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1804 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1805 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1806 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1807 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1808 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1809 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1810
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1811 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1812 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1813 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1814 case comparison:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1815 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1816 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1817 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1818 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1819 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1820 expr_ty expression;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1821 asdl_int_seq *ops;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1822 asdl_seq *cmps;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1823 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1824 if (!ops)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1825 return NULL;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1826 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1827 if (!cmps) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1828 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1829 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1830 for (i = 1; i < NCH(n); i += 2) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1831 cmpop_ty newoperator;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1832
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1833 newoperator = ast_for_comp_op(CHILD(n, i));
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1834 if (!newoperator) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1835 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1836 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1837
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1838 expression = ast_for_expr(c, CHILD(n, i + 1));
35873
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
1839 if (!expression) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1840 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1841 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1842
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1843 asdl_seq_SET(ops, i / 2, newoperator);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1844 asdl_seq_SET(cmps, i / 2, expression);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1845 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1846 expression = ast_for_expr(c, CHILD(n, 0));
35873
76ed7fdc9d85 Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35870
diff changeset
1847 if (!expression) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1848 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1849 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1850
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1851 return Compare(expression, ops, cmps, LINENO(n),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
1852 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1853 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1854 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1855
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1856 /* The next five cases all handle BinOps. The main body of code
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1857 is the same in each case, but the switch turned inside out to
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1858 reuse the code for each type of operator.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1859 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1860 case expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1861 case xor_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1862 case and_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1863 case shift_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1864 case arith_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1865 case term:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1866 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1867 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1868 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1869 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1870 return ast_for_binop(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1871 case yield_expr: {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1872 expr_ty exp = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1873 if (NCH(n) == 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1874 exp = ast_for_testlist(c, CHILD(n, 1));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1875 if (!exp)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1876 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1877 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1878 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1879 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1880 case factor:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1881 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1882 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1883 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1884 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1885 return ast_for_factor(c, n);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1886 case power:
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1887 return ast_for_power(c, n);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1888 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
1889 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1890 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1891 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1892 /* should never get here unless if error is set */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1893 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1894 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1895
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1896 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1897 ast_for_call(struct compiling *c, const node *n, expr_ty func)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1898 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1899 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1900 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1901 | '**' test)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1902 argument: [test '='] test [gen_for] # Really [keyword '='] test
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1903 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1904
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1905 int i, nargs, nkeywords, ngens;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1906 asdl_seq *args;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1907 asdl_seq *keywords;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1908 expr_ty vararg = NULL, kwarg = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1909
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1910 REQ(n, arglist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1911
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1912 nargs = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1913 nkeywords = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1914 ngens = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1915 for (i = 0; i < NCH(n); i++) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1916 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1917 if (TYPE(ch) == argument) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1918 if (NCH(ch) == 1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1919 nargs++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1920 else if (TYPE(CHILD(ch, 1)) == gen_for)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1921 ngens++;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1922 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1923 nkeywords++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1924 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1925 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1926 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1927 ast_error(n, "Generator expression must be parenthesized "
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1928 "if not sole argument");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1929 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1930 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1931
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1932 if (nargs + nkeywords + ngens > 255) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1933 ast_error(n, "more than 255 arguments");
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1934 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1935 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1936
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1937 args = asdl_seq_new(nargs + ngens, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1938 if (!args)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1939 return NULL;
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1940 keywords = asdl_seq_new(nkeywords, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1941 if (!keywords)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1942 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1943 nargs = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1944 nkeywords = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1945 for (i = 0; i < NCH(n); i++) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1946 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1947 if (TYPE(ch) == argument) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1948 expr_ty e;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1949 if (NCH(ch) == 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1950 if (nkeywords) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1951 ast_error(CHILD(ch, 0),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1952 "non-keyword arg after keyword arg");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1953 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1954 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1955 e = ast_for_expr(c, CHILD(ch, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1956 if (!e)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1957 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1958 asdl_seq_SET(args, nargs++, e);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1959 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1960 else if (TYPE(CHILD(ch, 1)) == gen_for) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1961 e = ast_for_genexp(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1962 if (!e)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1963 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1964 asdl_seq_SET(args, nargs++, e);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1965 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1966 else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1967 keyword_ty kw;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1968 identifier key;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1969
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1970 /* CHILD(ch, 0) is test, but must be an identifier? */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1971 e = ast_for_expr(c, CHILD(ch, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1972 if (!e)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1973 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1974 /* f(lambda x: x[0] = 3) ends up getting parsed with
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1975 * LHS test = lambda x: x[0], and RHS test = 3.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1976 * SF bug 132313 points out that complaining about a keyword
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1977 * then is very confusing.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1978 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1979 if (e->kind == Lambda_kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1980 ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1981 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1982 } else if (e->kind != Name_kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1983 ast_error(CHILD(ch, 0), "keyword can't be an expression");
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1984 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1985 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1986 key = e->v.Name.id;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1987 e = ast_for_expr(c, CHILD(ch, 2));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1988 if (!e)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1989 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1990 kw = keyword(key, e, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1991 if (!kw)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
1992 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1993 asdl_seq_SET(keywords, nkeywords++, kw);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1994 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1995 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1996 else if (TYPE(ch) == STAR) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1997 vararg = ast_for_expr(c, CHILD(n, i+1));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1998 i++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1999 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2000 else if (TYPE(ch) == DOUBLESTAR) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2001 kwarg = ast_for_expr(c, CHILD(n, i+1));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2002 i++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2003 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2004 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2005
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2006 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2007 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2008
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2009 static expr_ty
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2010 ast_for_testlist(struct compiling *c, const node* n)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2011 {
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2012 /* testlist_gexp: test (',' test)* [','] */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2013 /* testlist: test (',' test)* [','] */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2014 /* testlist_safe: test (',' test)+ [','] */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2015 /* testlist1: test (',' test)* */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2016 assert(NCH(n) > 0);
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2017 if (TYPE(n) == testlist_gexp) {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2018 if (NCH(n) > 1)
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2019 assert(TYPE(CHILD(n, 1)) != gen_for);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2020 }
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2021 else {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2022 assert(TYPE(n) == testlist ||
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2023 TYPE(n) == testlist_safe ||
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2024 TYPE(n) == testlist1);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2025 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2026 if (NCH(n) == 1)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2027 return ast_for_expr(c, CHILD(n, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2028 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2029 asdl_seq *tmp = seq_for_testlist(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2030 if (!tmp)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2031 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2032 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2033 }
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2034 }
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2035
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2036 static expr_ty
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2037 ast_for_testlist_gexp(struct compiling *c, const node* n)
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2038 {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2039 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2040 /* argument: test [ gen_for ] */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2041 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2042 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2043 return ast_for_genexp(c, n);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2044 return ast_for_testlist(c, n);
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2045 }
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2046
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2047 /* like ast_for_testlist() but returns a sequence */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2048 static asdl_seq*
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2049 ast_for_class_bases(struct compiling *c, const node* n)
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2050 {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2051 /* testlist: test (',' test)* [','] */
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2052 assert(NCH(n) > 0);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2053 REQ(n, testlist);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2054 if (NCH(n) == 1) {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2055 expr_ty base;
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2056 asdl_seq *bases = asdl_seq_new(1, c->c_arena);
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2057 if (!bases)
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2058 return NULL;
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2059 base = ast_for_expr(c, CHILD(n, 0));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2060 if (!base)
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2061 return NULL;
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2062 asdl_seq_SET(bases, 0, base);
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2063 return bases;
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2064 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2065
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2066 return seq_for_testlist(c, n);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2067 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2068
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2069 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2070 ast_for_expr_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2071 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2072 REQ(n, expr_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2073 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2074 | ('=' (yield_expr|testlist))*)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2075 testlist: test (',' test)* [',']
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2076 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2077 | '<<=' | '>>=' | '**=' | '//='
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2078 test: ... here starts the operator precendence dance
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2079 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2080
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2081 if (NCH(n) == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2082 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2083 if (!e)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2084 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2085
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2086 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2087 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2088 else if (TYPE(CHILD(n, 1)) == augassign) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2089 expr_ty expr1, expr2;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2090 operator_ty newoperator;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2091 node *ch = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2092
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2093 expr1 = ast_for_testlist(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2094 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2095 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2096 /* TODO(nas): Remove duplicated error checks (set_context does it) */
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2097 switch (expr1->kind) {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2098 case GeneratorExp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2099 ast_error(ch, "augmented assignment to generator "
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2100 "expression not possible");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2101 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2102 case Yield_kind:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2103 ast_error(ch, "augmented assignment to yield "
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2104 "expression not possible");
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2105 return NULL;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2106 case Name_kind: {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2107 const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2108 if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2109 ast_error(ch, "assignment to None");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2110 return NULL;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2111 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2112 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2113 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2114 case Attribute_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2115 case Subscript_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2116 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2117 default:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2118 ast_error(ch, "illegal expression for augmented "
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2119 "assignment");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2120 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2121 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2122 set_context(expr1, Store, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2123
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2124 ch = CHILD(n, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2125 if (TYPE(ch) == testlist)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2126 expr2 = ast_for_testlist(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2127 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2128 expr2 = ast_for_expr(c, ch);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2129 if (!expr2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2130 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2131
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2132 newoperator = ast_for_augassign(CHILD(n, 1));
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2133 if (!newoperator)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2134 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2135
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2136 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2137 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2138 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2139 int i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2140 asdl_seq *targets;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2141 node *value;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2142 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2143
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2144 /* a normal assignment */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2145 REQ(CHILD(n, 1), EQUAL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2146 targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2147 if (!targets)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2148 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2149 for (i = 0; i < NCH(n) - 2; i += 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2150 expr_ty e;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2151 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2152 if (TYPE(ch) == yield_expr) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2153 ast_error(ch, "assignment to yield expression not possible");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2154 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2155 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2156 e = ast_for_testlist(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2157
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2158 /* set context to assign */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2159 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2160 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2161
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2162 if (!set_context(e, Store, CHILD(n, i)))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2163 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2164
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2165 asdl_seq_SET(targets, i / 2, e);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2166 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2167 value = CHILD(n, NCH(n) - 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2168 if (TYPE(value) == testlist)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2169 expression = ast_for_testlist(c, value);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2170 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2171 expression = ast_for_expr(c, value);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2172 if (!expression)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2173 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2174 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2175 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2176 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2177
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2178 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2179 ast_for_print_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2180 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2181 /* print_stmt: 'print' ( [ test (',' test)* [','] ]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2182 | '>>' test [ (',' test)+ [','] ] )
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2183 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2184 expr_ty dest = NULL, expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2185 asdl_seq *seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2186 bool nl;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2187 int i, j, start = 1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2188
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2189 REQ(n, print_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2190 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2191 dest = ast_for_expr(c, CHILD(n, 2));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2192 if (!dest)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2193 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2194 start = 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2195 }
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2196 seq = asdl_seq_new((NCH(n) + 1 - start) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2197 if (!seq)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2198 return NULL;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2199 for (i = start, j = 0; i < NCH(n); i += 2, ++j) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2200 expression = ast_for_expr(c, CHILD(n, i));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2201 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2202 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2203 asdl_seq_SET(seq, j, expression);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2204 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2205 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2206 return Print(dest, seq, nl, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2207 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2208
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2209 static asdl_seq *
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2210 ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2211 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2212 asdl_seq *seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2213 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2214 expr_ty e;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2215
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2216 REQ(n, exprlist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2217
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2218 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2219 if (!seq)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2220 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2221 for (i = 0; i < NCH(n); i += 2) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2222 e = ast_for_expr(c, CHILD(n, i));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2223 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2224 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2225 asdl_seq_SET(seq, i / 2, e);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2226 if (context && !set_context(e, context, CHILD(n, i)))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2227 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2228 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2229 return seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2230 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2231
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2232 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2233 ast_for_del_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2234 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2235 asdl_seq *expr_list;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2236
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2237 /* del_stmt: 'del' exprlist */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2238 REQ(n, del_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2239
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2240 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2241 if (!expr_list)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2242 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2243 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2244 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2245
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2246 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2247 ast_for_flow_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2248 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2249 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2250 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2251 | yield_stmt
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2252 break_stmt: 'break'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2253 continue_stmt: 'continue'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2254 return_stmt: 'return' [testlist]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2255 yield_stmt: yield_expr
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2256 yield_expr: 'yield' testlist
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2257 raise_stmt: 'raise' [test [',' test [',' test]]]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2258 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2259 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2260
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2261 REQ(n, flow_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2262 ch = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2263 switch (TYPE(ch)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2264 case break_stmt:
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2265 return Break(LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2266 case continue_stmt:
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2267 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2268 case yield_stmt: { /* will reduce to yield_expr */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2269 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2270 if (!exp)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2271 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2272 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2273 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2274 case return_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2275 if (NCH(ch) == 1)
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2276 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2277 else {
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2278 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2279 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2280 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2281 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2282 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2283 case raise_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2284 if (NCH(ch) == 1)
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2285 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2286 else if (NCH(ch) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2287 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2288 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2289 return NULL;
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2290 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2291 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2292 else if (NCH(ch) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2293 expr_ty expr1, expr2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2294
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2295 expr1 = ast_for_expr(c, CHILD(ch, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2296 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2297 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2298 expr2 = ast_for_expr(c, CHILD(ch, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2299 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2300 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2301
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2302 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2303 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2304 else if (NCH(ch) == 6) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2305 expr_ty expr1, expr2, expr3;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2306
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2307 expr1 = ast_for_expr(c, CHILD(ch, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2308 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2309 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2310 expr2 = ast_for_expr(c, CHILD(ch, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2311 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2312 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2313 expr3 = ast_for_expr(c, CHILD(ch, 5));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2314 if (!expr3)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2315 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2316
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2317 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2318 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2319 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2320 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2321 "unexpected flow_stmt: %d", TYPE(ch));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2322 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2323 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2324
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2325 PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2326 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2327 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2328
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2329 static alias_ty
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2330 alias_for_import_name(struct compiling *c, const node *n)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2331 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2332 /*
36742
658f8ff2a442 Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents: 36718
diff changeset
2333 import_as_name: NAME ['as' NAME]
658f8ff2a442 Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents: 36718
diff changeset
2334 dotted_as_name: dotted_name ['as' NAME]
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2335 dotted_name: NAME ('.' NAME)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2336 */
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2337 PyObject *str;
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2338
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2339 loop:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2340 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2341 case import_as_name:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2342 str = NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2343 if (NCH(n) == 3) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2344 str = NEW_IDENTIFIER(CHILD(n, 2));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2345 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2346 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2347 case dotted_as_name:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2348 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2349 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2350 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2351 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2352 else {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2353 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
2354 if (!a)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
2355 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2356 assert(!a->asname);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2357 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2358 return a;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2359 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2360 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2361 case dotted_name:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2362 if (NCH(n) == 1)
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2363 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2364 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2365 /* Create a string of the form "a.b.c" */
36242
0f5189607264 Repair bizarre indentation created by VC 7.1.
Tim Peters <tim.peters@gmail.com>
parents: 36241
diff changeset
2366 int i;
36241
557e9b6ee7e1 alias_for_import_name(): Dueling compiler warnings ;-)
Tim Peters <tim.peters@gmail.com>
parents: 36231
diff changeset
2367 size_t len;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2368 char *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2369
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2370 len = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2371 for (i = 0; i < NCH(n); i += 2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2372 /* length of string plus one for the dot */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2373 len += strlen(STR(CHILD(n, i))) + 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2374 len--; /* the last name doesn't have a dot */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2375 str = PyString_FromStringAndSize(NULL, len);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2376 if (!str)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2377 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2378 s = PyString_AS_STRING(str);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2379 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2380 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2381 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2382 char *sch = STR(CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2383 strcpy(s, STR(CHILD(n, i)));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2384 s += strlen(sch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2385 *s++ = '.';
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2386 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2387 --s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2388 *s = '\0';
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2389 PyString_InternInPlace(&str);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2390 PyArena_AddPyObject(c->c_arena, str);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2391 return alias(str, NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2392 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2393 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2394 case STAR:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2395 str = PyString_InternFromString("*");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2396 PyArena_AddPyObject(c->c_arena, str);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2397 return alias(str, NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2398 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2399 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2400 "unexpected import name: %d", TYPE(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2401 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2402 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2403
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2404 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2405 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2406 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2407
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2408 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2409 ast_for_import_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2410 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2411 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2412 import_stmt: import_name | import_from
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2413 import_name: 'import' dotted_as_names
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2414 import_from: 'from' ('.'* dotted_name | '.') 'import'
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2415 ('*' | '(' import_as_names ')' | import_as_names)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2416 */
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2417 int lineno;
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2418 int col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2419 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2420 asdl_seq *aliases;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2421
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2422 REQ(n, import_stmt);
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2423 lineno = LINENO(n);
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2424 col_offset = n->n_col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2425 n = CHILD(n, 0);
36682
52cbc447e1c9 And some more cleanup.
Thomas Wouters <thomas@python.org>
parents: 36681
diff changeset
2426 if (TYPE(n) == import_name) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2427 n = CHILD(n, 1);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2428 REQ(n, dotted_as_names);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2429 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2430 if (!aliases)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2431 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2432 for (i = 0; i < NCH(n); i += 2) {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2433 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2434 if (!import_alias)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2435 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2436 asdl_seq_SET(aliases, i / 2, import_alias);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2437 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2438 return Import(aliases, lineno, col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2439 }
36682
52cbc447e1c9 And some more cleanup.
Thomas Wouters <thomas@python.org>
parents: 36681
diff changeset
2440 else if (TYPE(n) == import_from) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2441 int n_children;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2442 int idx, ndots = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2443 alias_ty mod = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2444 identifier modname;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2445
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2446 /* Count the number of dots (for relative imports) and check for the
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2447 optional module name */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2448 for (idx = 1; idx < NCH(n); idx++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2449 if (TYPE(CHILD(n, idx)) == dotted_name) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2450 mod = alias_for_import_name(c, CHILD(n, idx));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2451 idx++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2452 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2453 } else if (TYPE(CHILD(n, idx)) != DOT) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2454 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2455 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2456 ndots++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2457 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2458 idx++; /* skip over the 'import' keyword */
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2459 switch (TYPE(CHILD(n, idx))) {
36681
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2460 case STAR:
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2461 /* from ... import * */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2462 n = CHILD(n, idx);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2463 n_children = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2464 if (ndots) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2465 ast_error(n, "'import *' not allowed with 'from .'");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2466 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2467 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2468 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2469 case LPAR:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2470 /* from ... import (x, y, z) */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2471 n = CHILD(n, idx + 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2472 n_children = NCH(n);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2473 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2474 case import_as_names:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2475 /* from ... import x, y, z */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2476 n = CHILD(n, idx);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2477 n_children = NCH(n);
36681
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2478 if (n_children % 2 == 0) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2479 ast_error(n, "trailing comma not allowed without"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2480 " surrounding parentheses");
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2481 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2482 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2483 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2484 default:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2485 ast_error(n, "Unexpected node-type in from-import");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2486 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2487 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2488
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2489 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2490 if (!aliases)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2491 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2492
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2493 /* handle "from ... import *" special b/c there's no children */
36681
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2494 if (TYPE(n) == STAR) {
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2495 alias_ty import_alias = alias_for_import_name(c, n);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2496 if (!import_alias)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2497 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2498 asdl_seq_SET(aliases, 0, import_alias);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2499 }
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2500 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2501 for (i = 0; i < NCH(n); i += 2) {
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2502 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2503 if (!import_alias)
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2504 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2505 asdl_seq_SET(aliases, i / 2, import_alias);
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2506 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2507 }
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2508 if (mod != NULL)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2509 modname = mod->name;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2510 else
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2511 modname = new_identifier("", c->c_arena);
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2512 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2513 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2514 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2515 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2516 "unknown import statement: starts with command '%s'",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2517 STR(CHILD(n, 0)));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2518 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2519 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2520
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2521 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2522 ast_for_global_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2523 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2524 /* global_stmt: 'global' NAME (',' NAME)* */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2525 identifier name;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2526 asdl_seq *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2527 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2528
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2529 REQ(n, global_stmt);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2530 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2531 if (!s)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2532 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2533 for (i = 1; i < NCH(n); i += 2) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2534 name = NEW_IDENTIFIER(CHILD(n, i));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2535 if (!name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2536 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2537 asdl_seq_SET(s, i / 2, name);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2538 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2539 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2540 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2541
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2542 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2543 ast_for_assert_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2544 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2545 /* assert_stmt: 'assert' test [',' test] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2546 REQ(n, assert_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2547 if (NCH(n) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2548 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2549 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2550 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2551 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2552 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2553 else if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2554 expr_ty expr1, expr2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2555
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2556 expr1 = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2557 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2558 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2559 expr2 = ast_for_expr(c, CHILD(n, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2560 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2561 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2562
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2563 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2564 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2565 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2566 "improper number of parts to 'assert' statement: %d",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2567 NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2568 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2569 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2570
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2571 static asdl_seq *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2572 ast_for_suite(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2573 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2574 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2575 asdl_seq *seq;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2576 stmt_ty s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2577 int i, total, num, end, pos = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2578 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2579
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2580 REQ(n, suite);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2581
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2582 total = num_stmts(n);
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2583 seq = asdl_seq_new(total, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2584 if (!seq)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2585 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2586 if (TYPE(CHILD(n, 0)) == simple_stmt) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2587 n = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2588 /* simple_stmt always ends with a NEWLINE,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2589 and may have a trailing SEMI
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2590 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2591 end = NCH(n) - 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2592 if (TYPE(CHILD(n, end - 1)) == SEMI)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2593 end--;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2594 /* loop by 2 to skip semi-colons */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2595 for (i = 0; i < end; i += 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2596 ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2597 s = ast_for_stmt(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2598 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2599 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2600 asdl_seq_SET(seq, pos++, s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2601 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2602 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2603 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2604 for (i = 2; i < (NCH(n) - 1); i++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2605 ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2606 REQ(ch, stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2607 num = num_stmts(ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2608 if (num == 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2609 /* small_stmt or compound_stmt with only one child */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2610 s = ast_for_stmt(c, ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2611 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2612 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2613 asdl_seq_SET(seq, pos++, s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2614 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2615 else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2616 int j;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2617 ch = CHILD(ch, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2618 REQ(ch, simple_stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2619 for (j = 0; j < NCH(ch); j += 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2620 /* statement terminates with a semi-colon ';' */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2621 if (NCH(CHILD(ch, j)) == 0) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2622 assert((j + 1) == NCH(ch));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2623 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2624 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2625 s = ast_for_stmt(c, CHILD(ch, j));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2626 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2627 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2628 asdl_seq_SET(seq, pos++, s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2629 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2630 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2631 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2632 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2633 assert(pos == seq->size);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2634 return seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2635 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2636
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2637 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2638 ast_for_if_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2639 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2640 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2641 ['else' ':' suite]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2642 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2643 char *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2644
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2645 REQ(n, if_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2646
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2647 if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2648 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2649 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2650
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2651 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2652 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2653 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2654 suite_seq = ast_for_suite(c, CHILD(n, 3));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2655 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2656 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2657
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2658 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2659 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2660
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2661 s = STR(CHILD(n, 4));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2662 /* s[2], the third character in the string, will be
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2663 's' for el_s_e, or
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2664 'i' for el_i_f
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2665 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2666 if (s[2] == 's') {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2667 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2668 asdl_seq *seq1, *seq2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2669
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2670 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2671 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2672 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2673 seq1 = ast_for_suite(c, CHILD(n, 3));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2674 if (!seq1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2675 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2676 seq2 = ast_for_suite(c, CHILD(n, 6));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2677 if (!seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2678 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2679
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2680 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2681 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2682 else if (s[2] == 'i') {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2683 int i, n_elif, has_else = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2684 asdl_seq *orelse = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2685 n_elif = NCH(n) - 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2686 /* must reference the child n_elif+1 since 'else' token is third,
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2687 not fourth, child from the end. */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2688 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2689 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2690 has_else = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2691 n_elif -= 3;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2692 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2693 n_elif /= 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2694
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2695 if (has_else) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2696 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2697 asdl_seq *seq1, *seq2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2698
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2699 orelse = asdl_seq_new(1, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2700 if (!orelse)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2701 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2702 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2703 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2704 return NULL;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2705 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2706 if (!seq1)
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2707 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2708 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2709 if (!seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2710 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2711
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2712 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2713 LINENO(CHILD(n, NCH(n) - 6)), CHILD(n, NCH(n) - 6)->n_col_offset,
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 36001
diff changeset
2714 c->c_arena));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2715 /* the just-created orelse handled the last elif */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2716 n_elif--;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2717 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2718
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2719 for (i = 0; i < n_elif; i++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2720 int off = 5 + (n_elif - i - 1) * 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2721 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2722 asdl_seq *suite_seq;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2723 asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2724 if (!newobj)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2725 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2726 expression = ast_for_expr(c, CHILD(n, off));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2727 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2728 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2729 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2730 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2731 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2732
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2733 asdl_seq_SET(newobj, 0,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2734 If(expression, suite_seq, orelse,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2735 LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2736 orelse = newobj;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2737 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2738 return If(ast_for_expr(c, CHILD(n, 1)),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2739 ast_for_suite(c, CHILD(n, 3)),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2740 orelse, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2741 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2742
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2743 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2744 "unexpected token in 'if' statement: %s", s);
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2745 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2746 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2747
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2748 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2749 ast_for_while_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2750 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2751 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2752 REQ(n, while_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2753
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2754 if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2755 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2756 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2757
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2758 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2759 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2760 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2761 suite_seq = ast_for_suite(c, CHILD(n, 3));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2762 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2763 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2764 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2765 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2766 else if (NCH(n) == 7) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2767 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2768 asdl_seq *seq1, *seq2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2769
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2770 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2771 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2772 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2773 seq1 = ast_for_suite(c, CHILD(n, 3));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2774 if (!seq1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2775 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2776 seq2 = ast_for_suite(c, CHILD(n, 6));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2777 if (!seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2778 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2779
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2780 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2781 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2782
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2783 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2784 "wrong number of tokens for 'while' statement: %d",
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2785 NCH(n));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2786 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2787 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2788
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2789 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2790 ast_for_for_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2791 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2792 asdl_seq *_target, *seq = NULL, *suite_seq;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2793 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2794 expr_ty target;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2795 const node *node_target;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2796 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2797 REQ(n, for_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2798
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2799 if (NCH(n) == 9) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2800 seq = ast_for_suite(c, CHILD(n, 8));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2801 if (!seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2802 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2803 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2804
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2805 node_target = CHILD(n, 1);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2806 _target = ast_for_exprlist(c, node_target, Store);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2807 if (!_target)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2808 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2809 /* Check the # of children rather than the length of _target, since
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2810 for x, in ... has 1 element in _target, but still requires a Tuple. */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2811 if (NCH(node_target) == 1)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2812 target = (expr_ty)asdl_seq_GET(_target, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2813 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2814 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2815
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2816 expression = ast_for_testlist(c, CHILD(n, 3));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2817 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2818 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2819 suite_seq = ast_for_suite(c, CHILD(n, 5));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2820 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2821 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2822
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2823 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2824 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2825 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2826
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2827 static excepthandler_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2828 ast_for_except_clause(struct compiling *c, const node *exc, node *body)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2829 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2830 /* except_clause: 'except' [test [',' test]] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2831 REQ(exc, except_clause);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2832 REQ(body, suite);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2833
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2834 if (NCH(exc) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2835 asdl_seq *suite_seq = ast_for_suite(c, body);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2836 if (!suite_seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2837 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2838
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2839 return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2840 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2841 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2842 else if (NCH(exc) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2843 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2844 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2845
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2846 expression = ast_for_expr(c, CHILD(exc, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2847 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2848 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2849 suite_seq = ast_for_suite(c, body);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2850 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2851 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2852
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2853 return excepthandler(expression, NULL, suite_seq, LINENO(exc),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2854 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2855 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2856 else if (NCH(exc) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2857 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2858 expr_ty expression;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2859 expr_ty e = ast_for_expr(c, CHILD(exc, 3));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2860 if (!e)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2861 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2862 if (!set_context(e, Store, CHILD(exc, 3)))
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2863 return NULL;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2864 expression = ast_for_expr(c, CHILD(exc, 1));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2865 if (!expression)
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2866 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2867 suite_seq = ast_for_suite(c, body);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2868 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2869 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2870
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2871 return excepthandler(expression, e, suite_seq, LINENO(exc),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2872 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2873 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2874
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2875 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2876 "wrong number of children for 'except' clause: %d",
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2877 NCH(exc));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2878 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2879 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2880
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2881 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2882 ast_for_try_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2883 {
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2884 const int nch = NCH(n);
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2885 int n_except = (nch - 3)/3;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2886 asdl_seq *body, *orelse = NULL, *finally = NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2887
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2888 REQ(n, try_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2889
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2890 body = ast_for_suite(c, CHILD(n, 2));
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2891 if (body == NULL)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2892 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2893
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2894 if (TYPE(CHILD(n, nch - 3)) == NAME) {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2895 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2896 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2897 /* we can assume it's an "else",
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2898 because nch >= 9 for try-else-finally and
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2899 it would otherwise have a type of except_clause */
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2900 orelse = ast_for_suite(c, CHILD(n, nch - 4));
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2901 if (orelse == NULL)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2902 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2903 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2904 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2905
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2906 finally = ast_for_suite(c, CHILD(n, nch - 1));
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2907 if (finally == NULL)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2908 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2909 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2910 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2911 else {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2912 /* we can assume it's an "else",
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2913 otherwise it would have a type of except_clause */
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2914 orelse = ast_for_suite(c, CHILD(n, nch - 1));
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2915 if (orelse == NULL)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2916 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2917 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2918 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2919 }
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2920 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
35867
f4842f42700b remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents: 35799
diff changeset
2921 ast_error(n, "malformed 'try' statement");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2922 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2923 }
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2924
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2925 if (n_except > 0) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2926 int i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2927 stmt_ty except_st;
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2928 /* process except statements to create a try ... except */
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2929 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2930 if (handlers == NULL)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2931 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2932
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2933 for (i = 0; i < n_except; i++) {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2934 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2935 CHILD(n, 5 + i * 3));
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2936 if (!e)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2937 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2938 asdl_seq_SET(handlers, i, e);
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2939 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2940
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2941 except_st = TryExcept(body, handlers, orelse, LINENO(n),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
2942 n->n_col_offset, c->c_arena);
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2943 if (!finally)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2944 return except_st;
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2945
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2946 /* if a 'finally' is present too, we nest the TryExcept within a
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2947 TryFinally to emulate try ... except ... finally */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2948 body = asdl_seq_new(1, c->c_arena);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2949 if (body == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2950 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2951 asdl_seq_SET(body, 0, except_st);
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2952 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2953
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2954 /* must be a try ... finally (except clauses are in body, if any exist) */
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2955 assert(finally != NULL);
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2956 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2957 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2958
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2959 static expr_ty
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2960 ast_for_with_var(struct compiling *c, const node *n)
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2961 {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2962 REQ(n, with_var);
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2963 return ast_for_expr(c, CHILD(n, 1));
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2964 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2965
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2966 /* with_stmt: 'with' test [ with_var ] ':' suite */
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2967 static stmt_ty
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2968 ast_for_with_stmt(struct compiling *c, const node *n)
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2969 {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2970 expr_ty context_expr, optional_vars = NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2971 int suite_index = 3; /* skip 'with', test, and ':' */
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2972 asdl_seq *suite_seq;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2973
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2974 assert(TYPE(n) == with_stmt);
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2975 context_expr = ast_for_expr(c, CHILD(n, 1));
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2976 if (TYPE(CHILD(n, 2)) == with_var) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2977 optional_vars = ast_for_with_var(c, CHILD(n, 2));
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2978
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2979 if (!optional_vars) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2980 return NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2981 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2982 if (!set_context(optional_vars, Store, n)) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2983 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2984 }
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2985 suite_index = 4;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2986 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2987
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2988 suite_seq = ast_for_suite(c, CHILD(n, suite_index));
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2989 if (!suite_seq) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2990 return NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2991 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2992 return With(context_expr, optional_vars, suite_seq, LINENO(n),
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2993 n->n_col_offset, c->c_arena);
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2994 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2995
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2996 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2997 ast_for_classdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2998 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2999 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3000 asdl_seq *bases, *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3001
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3002 REQ(n, classdef);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3003
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3004 if (!strcmp(STR(CHILD(n, 1)), "None")) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3005 ast_error(n, "assignment to None");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3006 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3007 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3008
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3009 if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3010 s = ast_for_suite(c, CHILD(n, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3011 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3012 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3013 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
3014 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3015 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3016 /* check for empty base list */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3017 if (TYPE(CHILD(n,3)) == RPAR) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3018 s = ast_for_suite(c, CHILD(n,5));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3019 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3020 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3021 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
3022 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3023 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3024
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3025 /* else handle the base class list */
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
3026 bases = ast_for_class_bases(c, CHILD(n, 3));
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
3027 if (!bases)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3028 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3029
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3030 s = ast_for_suite(c, CHILD(n, 6));
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
3031 if (!s)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3032 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
3033 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
3034 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3035 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3036
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3037 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3038 ast_for_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3039 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3040 if (TYPE(n) == stmt) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3041 assert(NCH(n) == 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3042 n = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3043 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3044 if (TYPE(n) == simple_stmt) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3045 assert(num_stmts(n) == 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3046 n = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3047 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3048 if (TYPE(n) == small_stmt) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3049 REQ(n, small_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3050 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3051 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39693
diff changeset
3052 | flow_stmt | import_stmt | global_stmt | assert_stmt
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3053 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3054 switch (TYPE(n)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3055 case expr_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3056 return ast_for_expr_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3057 case print_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3058 return ast_for_print_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3059 case del_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3060 return ast_for_del_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3061 case pass_stmt:
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
3062 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3063 case flow_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3064 return ast_for_flow_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3065 case import_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3066 return ast_for_import_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3067 case global_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3068 return ast_for_global_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3069 case assert_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3070 return ast_for_assert_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3071 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
3072 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3073 "unhandled small_stmt: TYPE=%d NCH=%d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3074 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3075 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3076 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3077 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3078 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3079 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3080 | funcdef | classdef
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3081 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3082 node *ch = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3083 REQ(n, compound_stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3084 switch (TYPE(ch)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3085 case if_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3086 return ast_for_if_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3087 case while_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3088 return ast_for_while_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3089 case for_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3090 return ast_for_for_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3091 case try_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3092 return ast_for_try_stmt(c, ch);
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
3093 case with_stmt:
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
3094 return ast_for_with_stmt(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3095 case funcdef:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3096 return ast_for_funcdef(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3097 case classdef:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3098 return ast_for_classdef(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3099 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
3100 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3101 "unhandled small_stmt: TYPE=%d NCH=%d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3102 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3103 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3104 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3105 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3106 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3107
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3108 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3109 parsenumber(const char *s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3110 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3111 const char *end;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3112 long x;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3113 double dx;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3114 #ifndef WITHOUT_COMPLEX
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3115 Py_complex c;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3116 int imflag;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3117 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3118
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3119 errno = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3120 end = s + strlen(s) - 1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3121 #ifndef WITHOUT_COMPLEX
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3122 imflag = *end == 'j' || *end == 'J';
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3123 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3124 if (*end == 'l' || *end == 'L')
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3125 return PyLong_FromString((char *)s, (char **)0, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3126 if (s[0] == '0') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3127 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3128 if (x < 0 && errno == 0) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3129 return PyLong_FromString((char *)s,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3130 (char **)0,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3131 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3132 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3133 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3134 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3135 x = PyOS_strtol((char *)s, (char **)&end, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3136 if (*end == '\0') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3137 if (errno != 0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3138 return PyLong_FromString((char *)s, (char **)0, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3139 return PyInt_FromLong(x);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3140 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3141 /* XXX Huge floats may silently fail */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3142 #ifndef WITHOUT_COMPLEX
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3143 if (imflag) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3144 c.real = 0.;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3145 PyFPE_START_PROTECT("atof", return 0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3146 c.imag = PyOS_ascii_atof(s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3147 PyFPE_END_PROTECT(c)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3148 return PyComplex_FromCComplex(c);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3149 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3150 else
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3151 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3152 {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3153 PyFPE_START_PROTECT("atof", return 0)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3154 dx = PyOS_ascii_atof(s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3155 PyFPE_END_PROTECT(dx)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3156 return PyFloat_FromDouble(dx);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3157 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3158 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3159
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3160 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3161 decode_utf8(const char **sPtr, const char *end, char* encoding)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3162 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3163 #ifndef Py_USING_UNICODE
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3164 Py_FatalError("decode_utf8 should not be called in this build.");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3165 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3166 #else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3167 PyObject *u, *v;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3168 char *s, *t;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3169 t = s = (char *)*sPtr;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3170 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3171 while (s < end && (*s & 0x80)) s++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3172 *sPtr = s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3173 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3174 if (u == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3175 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3176 v = PyUnicode_AsEncodedString(u, encoding, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3177 Py_DECREF(u);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3178 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3179 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3180 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3181
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3182 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3183 decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3184 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3185 PyObject *v, *u;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3186 char *buf;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3187 char *p;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3188 const char *end;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3189 if (encoding == NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3190 buf = (char *)s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3191 u = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3192 } else if (strcmp(encoding, "iso-8859-1") == 0) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3193 buf = (char *)s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3194 u = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3195 } else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3196 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3197 u = PyString_FromStringAndSize((char *)NULL, len * 4);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3198 if (u == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3199 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3200 p = buf = PyString_AsString(u);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3201 end = s + len;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3202 while (s < end) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3203 if (*s == '\\') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3204 *p++ = *s++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3205 if (*s & 0x80) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3206 strcpy(p, "u005c");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3207 p += 5;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3208 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3209 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3210 if (*s & 0x80) { /* XXX inefficient */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3211 PyObject *w;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3212 char *r;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3213 Py_ssize_t rn, i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3214 w = decode_utf8(&s, end, "utf-16-be");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3215 if (w == NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3216 Py_DECREF(u);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3217 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3218 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3219 r = PyString_AsString(w);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3220 rn = PyString_Size(w);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3221 assert(rn % 2 == 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3222 for (i = 0; i < rn; i += 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3223 sprintf(p, "\\u%02x%02x",
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3224 r[i + 0] & 0xFF,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3225 r[i + 1] & 0xFF);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3226 p += 6;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3227 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3228 Py_DECREF(w);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3229 } else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3230 *p++ = *s++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3231 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3232 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3233 len = p - buf;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3234 s = buf;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3235 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3236 if (rawmode)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3237 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3238 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3239 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3240 Py_XDECREF(u);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3241 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3242 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3243
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3244 /* s is a Python string literal, including the bracketing quote characters,
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3245 * and r &/or u prefixes (if any), and embedded escape sequences (if any).
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3246 * parsestr parses it, and returns the decoded Python string object.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3247 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3248 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3249 parsestr(const char *s, const char *encoding)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3250 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3251 size_t len;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3252 int quote = Py_CHARMASK(*s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3253 int rawmode = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3254 int need_encoding;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3255 int unicode = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3256
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3257 if (isalpha(quote) || quote == '_') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3258 if (quote == 'u' || quote == 'U') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3259 quote = *++s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3260 unicode = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3261 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3262 if (quote == 'r' || quote == 'R') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3263 quote = *++s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3264 rawmode = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3265 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3266 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3267 if (quote != '\'' && quote != '\"') {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3268 PyErr_BadInternalCall();
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3269 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3270 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3271 s++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3272 len = strlen(s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3273 if (len > INT_MAX) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3274 PyErr_SetString(PyExc_OverflowError,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3275 "string to parse is too long");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3276 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3277 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3278 if (s[--len] != quote) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3279 PyErr_BadInternalCall();
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3280 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3281 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3282 if (len >= 4 && s[0] == quote && s[1] == quote) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3283 s += 2;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3284 len -= 2;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3285 if (s[--len] != quote || s[--len] != quote) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3286 PyErr_BadInternalCall();
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3287 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3288 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3289 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3290 #ifdef Py_USING_UNICODE
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3291 if (unicode || Py_UnicodeFlag) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3292 return decode_unicode(s, len, rawmode, encoding);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3293 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3294 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3295 need_encoding = (encoding != NULL &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3296 strcmp(encoding, "utf-8") != 0 &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3297 strcmp(encoding, "iso-8859-1") != 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3298 if (rawmode || strchr(s, '\\') == NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3299 if (need_encoding) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3300 #ifndef Py_USING_UNICODE
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3301 /* This should not happen - we never see any other
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3302 encoding. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3303 Py_FatalError(
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36877
diff changeset
3304 "cannot deal with encodings in this build.");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3305 #else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3306 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3307 if (u == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3308 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3309 v = PyUnicode_AsEncodedString(u, encoding, NULL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3310 Py_DECREF(u);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3311 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3312 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3313 } else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3314 return PyString_FromStringAndSize(s, len);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3315 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3316 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3317
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3318 return PyString_DecodeEscape(s, len, NULL, unicode,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3319 need_encoding ? encoding : NULL);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3320 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3321
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3322 /* Build a Python string object out of a STRING atom. This takes care of
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3323 * compile-time literal catenation, calling parsestr() on each piece, and
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3324 * pasting the intermediate results together.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3325 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3326 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3327 parsestrplus(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3328 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3329 PyObject *v;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3330 int i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3331 REQ(CHILD(n, 0), STRING);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3332 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3333 /* String literal concatenation */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3334 for (i = 1; i < NCH(n); i++) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3335 PyObject *s;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3336 s = parsestr(STR(CHILD(n, i)), c->c_encoding);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3337 if (s == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3338 goto onError;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3339 if (PyString_Check(v) && PyString_Check(s)) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3340 PyString_ConcatAndDel(&v, s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3341 if (v == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3342 goto onError;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3343 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3344 #ifdef Py_USING_UNICODE
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3345 else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3346 PyObject *temp = PyUnicode_Concat(v, s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3347 Py_DECREF(s);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3348 Py_DECREF(v);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3349 v = temp;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3350 if (v == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3351 goto onError;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3352 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3353 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3354 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3355 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3356 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3357
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3358 onError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3359 Py_XDECREF(v);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3360 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3361 }