annotate Python/ast.c @ 41554:06145fbc7ab9

Merged revisions 53952-54987 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r53954 | jeremy.hylton | 2007-02-26 10:41:18 -0800 (Mon, 26 Feb 2007) | 10 lines Do not copy free variables to locals in class namespaces. Fixes bug 1569356, but at the cost of a minor incompatibility in locals(). Add test that verifies that the class namespace is not polluted. Also clarify the behavior in the library docs. Along the way, cleaned up the dict_to_map and map_to_dict implementations and added some comments that explain what they do. ........ r53955 | jeremy.hylton | 2007-02-26 11:00:20 -0800 (Mon, 26 Feb 2007) | 2 lines Fix assertion. ........ r53969 | neal.norwitz | 2007-02-26 14:41:45 -0800 (Mon, 26 Feb 2007) | 3 lines When printing an unraisable error, don't print exceptions. before the name. This duplicates the behavior whening normally printing exceptions. ........ r53970 | andrew.kuchling | 2007-02-26 15:02:47 -0800 (Mon, 26 Feb 2007) | 1 line Markup fix ........ r53975 | neal.norwitz | 2007-02-26 15:48:27 -0800 (Mon, 26 Feb 2007) | 3 lines SF #1669182, 2.5 was already fixed. Just assert in 2.6 since string exceptions are gone. ........ r53976 | andrew.kuchling | 2007-02-26 15:54:17 -0800 (Mon, 26 Feb 2007) | 1 line Add some items ........ r53981 | jeremy.hylton | 2007-02-26 17:01:59 -0800 (Mon, 26 Feb 2007) | 4 lines Fix long-standing bug in name mangling for package imports Reported by Mike Verdone. ........ r53993 | jeremy.hylton | 2007-02-27 08:00:06 -0800 (Tue, 27 Feb 2007) | 2 lines tabify ........ r53994 | jeremy.hylton | 2007-02-27 08:13:23 -0800 (Tue, 27 Feb 2007) | 5 lines tabify Note that ast.c still has a mix of tabs and spaces, because it attempts to use four-space indents for more of the new code. ........ r53996 | jeremy.hylton | 2007-02-27 09:24:48 -0800 (Tue, 27 Feb 2007) | 2 lines whitespace normalization ........ r53997 | jeremy.hylton | 2007-02-27 10:29:45 -0800 (Tue, 27 Feb 2007) | 24 lines Add checking for a number of metaclass error conditions. We add some new rules that are required for preserving internal invariants of types. 1. If type (or a subclass of type) appears in bases, it must appear before any non-type bases. If a non-type base (like a regular new-style class) occurred first, it could trick type into allocating the new class an __dict__ which must be impossible. 2. There are several checks that are made of bases when creating a type. Those checks are now repeated when assigning to __bases__. We also add the restriction that assignment to __bases__ may not change the metaclass of the type. Add new tests for these cases and for a few other oddball errors that were no previously tested. Remove a crasher test that was fixed. Also some internal refactoring: Extract the code to find the most derived metaclass of a type and its bases. It is now needed in two places. Rewrite the TypeError checks in test_descr to use doctest. The tests now clearly show what exception they expect to see. ........ r53998 | jeremy.hylton | 2007-02-27 10:33:31 -0800 (Tue, 27 Feb 2007) | 2 lines Add news about changes to metaclasses and __bases__ error checking. ........ r54016 | armin.rigo | 2007-02-28 01:25:29 -0800 (Wed, 28 Feb 2007) | 3 lines Modify the segfaulting example to show why r53997 is not a solution to it. ........ r54022 | brett.cannon | 2007-02-28 10:15:00 -0800 (Wed, 28 Feb 2007) | 2 lines Add a test for instantiating SyntaxError with no arguments. ........ r54026 | raymond.hettinger | 2007-02-28 10:27:41 -0800 (Wed, 28 Feb 2007) | 1 line Docstring nit. ........ r54033 | raymond.hettinger | 2007-02-28 10:37:52 -0800 (Wed, 28 Feb 2007) | 1 line Prepare collections module for pure python code entries. ........ r54053 | raymond.hettinger | 2007-02-28 22:16:43 -0800 (Wed, 28 Feb 2007) | 1 line Add collections.NamedTuple ........ r54054 | neal.norwitz | 2007-02-28 23:04:41 -0800 (Wed, 28 Feb 2007) | 1 line Add Pat and Eric for work on PEP 3101 in the sandbox ........ r54061 | andrew.kuchling | 2007-03-01 06:36:12 -0800 (Thu, 01 Mar 2007) | 1 line Add NamedTuple ........ r54080 | georg.brandl | 2007-03-02 06:37:12 -0800 (Fri, 02 Mar 2007) | 2 lines Bug #1628895: some better tries to find HTML documentation in pydoc. ........ r54086 | raymond.hettinger | 2007-03-02 11:20:46 -0800 (Fri, 02 Mar 2007) | 1 line Fix embarrassing typo and fix constantification of None ........ r54088 | georg.brandl | 2007-03-02 12:30:14 -0800 (Fri, 02 Mar 2007) | 2 lines Bugs #1668032, #1668036, #1669304: clarify behavior of PyMem_Realloc and _Resize. ........ r54114 | georg.brandl | 2007-03-04 09:18:54 -0800 (Sun, 04 Mar 2007) | 2 lines Fix a bug in test_dict and test_userdict, found at the PyPy sprint. ........ r54124 | skip.montanaro | 2007-03-04 12:52:28 -0800 (Sun, 04 Mar 2007) | 2 lines Teach setup.py how to find Berkeley DB on Macs using MacPorts. ........ r54125 | skip.montanaro | 2007-03-04 12:54:12 -0800 (Sun, 04 Mar 2007) | 1 line note MacPorts/BerkDB change in setup.py ........ r54136 | neal.norwitz | 2007-03-04 23:52:01 -0800 (Sun, 04 Mar 2007) | 1 line Added Pete for 3101 too ........ r54138 | facundo.batista | 2007-03-05 08:31:54 -0800 (Mon, 05 Mar 2007) | 1 line Minor corrections to docs, and an explanation comentary ........ r54139 | georg.brandl | 2007-03-05 14:28:08 -0800 (Mon, 05 Mar 2007) | 3 lines Patch #1674228: when assigning a slice (old-style), check for the sq_ass_slice instead of the sq_slice slot. ........ r54149 | georg.brandl | 2007-03-06 01:33:01 -0800 (Tue, 06 Mar 2007) | 3 lines Nit: a struct field is set to GenericAlloc, not GenericAlloc(). ........ r54150 | georg.brandl | 2007-03-06 02:02:47 -0800 (Tue, 06 Mar 2007) | 3 lines Patch #1671450: add a section about subclassing builtin types to the "extending and embedding" tutorial. ........ r54152 | martin.v.loewis | 2007-03-06 02:41:24 -0800 (Tue, 06 Mar 2007) | 2 lines Patch #1121142: Implement ZipFile.open. ........ r54154 | georg.brandl | 2007-03-06 03:51:14 -0800 (Tue, 06 Mar 2007) | 2 lines A test case for the fix in #1674228. ........ r54156 | georg.brandl | 2007-03-06 03:52:24 -0800 (Tue, 06 Mar 2007) | 2 lines Patch #1672481: fix bug in idlelib.MultiCall. ........ r54159 | georg.brandl | 2007-03-06 04:17:50 -0800 (Tue, 06 Mar 2007) | 1 line Bug #1674503: close the file opened by execfile() in an error condition. ........ r54160 | georg.brandl | 2007-03-06 05:32:52 -0800 (Tue, 06 Mar 2007) | 3 lines Fix another reincarnation of bug #1576657 in defaultdict. ........ r54162 | georg.brandl | 2007-03-06 05:35:00 -0800 (Tue, 06 Mar 2007) | 2 lines A test case for the defaultdict KeyError bug. ........ r54164 | georg.brandl | 2007-03-06 05:37:45 -0800 (Tue, 06 Mar 2007) | 3 lines Patch #1663234: you can now run doctest on test files and modules using "python -m doctest [-v] filename ...". ........ r54165 | martin.v.loewis | 2007-03-06 06:43:00 -0800 (Tue, 06 Mar 2007) | 3 lines Patch #912410: Replace HTML entity references for attribute values in HTMLParser. ........ r54166 | skip.montanaro | 2007-03-06 07:41:38 -0800 (Tue, 06 Mar 2007) | 1 line patch 1673619 - identify extension modules which cannot be built ........ r54167 | guido.van.rossum | 2007-03-06 07:50:01 -0800 (Tue, 06 Mar 2007) | 5 lines Patch #1646728: datetime.fromtimestamp fails with negative fractional times. With unittest. Somebody please backport to 2.5. ........ r54169 | georg.brandl | 2007-03-06 09:49:14 -0800 (Tue, 06 Mar 2007) | 2 lines Fix cmp vs. key argument for list.sort. ........ r54170 | georg.brandl | 2007-03-06 10:21:32 -0800 (Tue, 06 Mar 2007) | 2 lines Small nit, found by Neal. ........ r54171 | georg.brandl | 2007-03-06 10:29:58 -0800 (Tue, 06 Mar 2007) | 3 lines Patch #1602128: clarify that richcmp methods can return NotImplemented and should return True or False otherwise. ........ r54173 | georg.brandl | 2007-03-06 10:41:12 -0800 (Tue, 06 Mar 2007) | 2 lines Patch #1638879: don't accept strings with embedded NUL bytes in long(). ........ r54175 | georg.brandl | 2007-03-06 10:47:31 -0800 (Tue, 06 Mar 2007) | 2 lines Patch #1673121: update README wrt. OSX default shell. ........ r54177 | georg.brandl | 2007-03-06 10:59:11 -0800 (Tue, 06 Mar 2007) | 3 lines Patch #1654417: make operator.{get,set,del}slice use the full range of Py_ssize_t. ........ r54180 | walter.doerwald | 2007-03-06 12:38:57 -0800 (Tue, 06 Mar 2007) | 4 lines Patch for bug #1633621: if curses.resizeterm() or curses.resize_term() is called, update _curses.LINES, _curses.COLS, curses.LINES and curses.COLS. ........ r54182 | walter.doerwald | 2007-03-06 13:15:24 -0800 (Tue, 06 Mar 2007) | 2 lines Document change to curses. ........ r54188 | georg.brandl | 2007-03-06 16:34:46 -0800 (Tue, 06 Mar 2007) | 5 lines Variant of patch #697613: don't exit the interpreter on a SystemExit exception if the -i command line option or PYTHONINSPECT environment variable is given, but break into the interactive interpreter just like on other exceptions or normal program exit. (backport) ........ r54189 | georg.brandl | 2007-03-06 16:40:28 -0800 (Tue, 06 Mar 2007) | 4 lines Patch #703779: unset __file__ in __main__ after running a file. This makes the filenames the warning module prints much more sensible when a PYTHONSTARTUP file is used. ........ r54192 | george.yoshida | 2007-03-06 20:21:18 -0800 (Tue, 06 Mar 2007) | 2 lines add versionadded info ........ r54195 | georg.brandl | 2007-03-06 23:39:06 -0800 (Tue, 06 Mar 2007) | 2 lines Patch #812285: allow multiple auth schemes in AbstractBasicAuthHandler. ........ r54197 | georg.brandl | 2007-03-07 00:31:51 -0800 (Wed, 07 Mar 2007) | 3 lines Patch #1001604: glob.glob() now returns unicode filenames if it was given a unicode argument and os.listdir() returns unicode filenames. ........ r54199 | georg.brandl | 2007-03-07 01:09:40 -0800 (Wed, 07 Mar 2007) | 3 lines Patches #1550273, #1550272: fix a few bugs in unittest and add a comprehensive test suite for the module. ........ r54201 | georg.brandl | 2007-03-07 01:21:06 -0800 (Wed, 07 Mar 2007) | 3 lines Patch #787789: allow to pass custom TestRunner instances to unittest's main() function. ........ r54202 | georg.brandl | 2007-03-07 01:34:45 -0800 (Wed, 07 Mar 2007) | 2 lines Patch #1669331: clarify shutil.copyfileobj() behavior wrt. file position. ........ r54204 | martin.v.loewis | 2007-03-07 03:04:33 -0800 (Wed, 07 Mar 2007) | 2 lines Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', ''). ........ r54206 | georg.brandl | 2007-03-07 03:37:42 -0800 (Wed, 07 Mar 2007) | 2 lines Patch #1675471: convert test_pty to unittest. ........ r54207 | georg.brandl | 2007-03-07 03:54:49 -0800 (Wed, 07 Mar 2007) | 4 lines Add some sanity checks to unittest.TestSuite's addTest(s) methods. Fixes #878275. ........ r54209 | guido.van.rossum | 2007-03-07 07:16:29 -0800 (Wed, 07 Mar 2007) | 3 lines Windows doesn't support negative timestamps. Skip the tests involving them if os.name == "nt". ........ r54219 | martin.v.loewis | 2007-03-08 05:42:43 -0800 (Thu, 08 Mar 2007) | 2 lines Add missing ) in parenthical remark. ........ r54220 | georg.brandl | 2007-03-08 09:49:06 -0800 (Thu, 08 Mar 2007) | 2 lines Fix #1676656: \em is different from \emph... ........ r54222 | georg.brandl | 2007-03-08 10:37:31 -0800 (Thu, 08 Mar 2007) | 2 lines Add a NEWS entry for rev. 54207,8. ........ r54225 | raymond.hettinger | 2007-03-08 11:24:27 -0800 (Thu, 08 Mar 2007) | 1 line SF 1676321: empty() returned wrong result ........ r54227 | collin.winter | 2007-03-08 11:58:14 -0800 (Thu, 08 Mar 2007) | 1 line Backported r54226 from p3yk: Move test_unittest, test_doctest and test_doctest2 higher up in the testing order. ........ r54230 | raymond.hettinger | 2007-03-08 13:33:47 -0800 (Thu, 08 Mar 2007) | 1 line SF #1637850: make_table in difflib did not work with unicode ........ r54232 | collin.winter | 2007-03-08 14:16:25 -0800 (Thu, 08 Mar 2007) | 1 line Patch #1668482: don't use '-' in mkstemp ........ r54233 | brett.cannon | 2007-03-08 15:58:11 -0800 (Thu, 08 Mar 2007) | 10 lines Introduce test.test_support.TransientResource. It's a context manager to surround calls to resources that may or may not be available. Specifying the expected exception and attributes to be raised if the resource is not available prevents overly broad catching of exceptions. This is meant to help suppress spurious failures by raising test.test_support.ResourceDenied if the exception matches. It would probably be good to go through the various network tests and surround the calls to catch connection timeouts (as done with test_socket_ssl in this commit). ........ r54234 | collin.winter | 2007-03-08 19:15:56 -0800 (Thu, 08 Mar 2007) | 1 line Patch #1481079: Support of HTTP_REFERER in CGIHTTPServer.py ........ r54235 | collin.winter | 2007-03-08 19:26:32 -0800 (Thu, 08 Mar 2007) | 1 line Add NEWS item for patch #1481079 (r54234). ........ r54237 | neal.norwitz | 2007-03-08 21:59:01 -0800 (Thu, 08 Mar 2007) | 1 line Fix SF #1676971, Complex OverflowError has a typo ........ r54239 | georg.brandl | 2007-03-09 04:58:41 -0800 (Fri, 09 Mar 2007) | 2 lines Typo. ........ r54240 | martin.v.loewis | 2007-03-09 07:35:55 -0800 (Fri, 09 Mar 2007) | 2 lines Patch #957003: Implement smtplib.LMTP. ........ r54243 | collin.winter | 2007-03-09 10:09:10 -0800 (Fri, 09 Mar 2007) | 2 lines Bug #1629566: clarify the docs on the return values of parsedate() and parsedate_tz() in email.utils and rfc822. ........ r54244 | thomas.heller | 2007-03-09 11:21:28 -0800 (Fri, 09 Mar 2007) | 3 lines Fix bug #1646630: ctypes.string_at(buf, 0) and ctypes.wstring_at(buf, 0) returned string up to the first NUL character. ........ r54245 | martin.v.loewis | 2007-03-09 11:36:01 -0800 (Fri, 09 Mar 2007) | 2 lines Add Ziga Seilnacht. ........ r54247 | collin.winter | 2007-03-09 12:33:07 -0800 (Fri, 09 Mar 2007) | 2 lines Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError. ........ r54248 | thomas.heller | 2007-03-09 12:39:22 -0800 (Fri, 09 Mar 2007) | 7 lines Bug #1651235: When a tuple was passed to a ctypes function call, Python would crash instead of raising an error. The crash was caused by a section of code that should have been removed long ago, at that time ctypes had other ways to pass parameters to function calls. ........ r54250 | collin.winter | 2007-03-09 15:30:39 -0800 (Fri, 09 Mar 2007) | 1 line Hashing simplification pointed out by Thomas Wouters. ........ r54252 | collin.winter | 2007-03-09 18:23:40 -0800 (Fri, 09 Mar 2007) | 5 lines * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink. * Drop safe_remove() in favor of test_support.unlink(). * Fix the indentation of test_samefile so that it runs. ........ r54253 | collin.winter | 2007-03-09 18:51:26 -0800 (Fri, 09 Mar 2007) | 3 lines Bug #1531963: Make SocketServer.TCPServer's server_address always be equal to calling getsockname() on the server's socket. Will backport. ........ r54254 | neal.norwitz | 2007-03-09 19:19:18 -0800 (Fri, 09 Mar 2007) | 4 lines Simplify a little by handling the TCP case first. Update to use predominant style of spaces around = in args list and print to stderr if debugging. ........ r54256 | collin.winter | 2007-03-09 19:35:34 -0800 (Fri, 09 Mar 2007) | 1 line Add proper attribution for a bug fix. ........ r54257 | georg.brandl | 2007-03-09 23:38:14 -0800 (Fri, 09 Mar 2007) | 2 lines Typos. ........ r54260 | collin.winter | 2007-03-10 06:33:32 -0800 (Sat, 10 Mar 2007) | 1 line Convert an assert to a raise so it works even in the presence of -O. ........ r54262 | collin.winter | 2007-03-10 06:41:48 -0800 (Sat, 10 Mar 2007) | 2 lines Patch #1599845: Add an option to disable the implicit calls to server_bind() and server_activate() in the constructors for TCPServer, SimpleXMLRPCServer and DocXMLRPCServer. ........ r54268 | georg.brandl | 2007-03-11 00:28:46 -0800 (Sun, 11 Mar 2007) | 2 lines Add missing "return" statements in exception handler. ........ r54270 | ziga.seilnacht | 2007-03-11 08:54:54 -0700 (Sun, 11 Mar 2007) | 3 lines Patch #1675981: remove unreachable code from type.__new__() method. __dict__ and __weakref__ are removed from the slots tuple earlier in the code, in the loop that mangles slot names. Will backport. ........ r54271 | collin.winter | 2007-03-11 09:00:20 -0700 (Sun, 11 Mar 2007) | 3 lines Patch #1192590: Fix pdb's "ignore" and "condition" commands so they trap the IndexError caused by passing in an invalid breakpoint number. Will backport. ........ r54274 | vinay.sajip | 2007-03-11 11:32:07 -0700 (Sun, 11 Mar 2007) | 1 line Fix resource leak reported in SF #1516995. ........ r54278 | collin.winter | 2007-03-11 18:55:54 -0700 (Sun, 11 Mar 2007) | 4 lines Patch #1678662: ftp.python.org does not exist. So the testcode in urllib.py must use a more stable FTP. Will backport. ........ r54280 | barry.warsaw | 2007-03-11 20:20:01 -0700 (Sun, 11 Mar 2007) | 8 lines Tokio Kikuchi's fix for SF bug #1629369; folding whitespace allowed in the display name of an email address, e.g. Foo \tBar <foo@example.com> Test case added by Barry. ........ r54282 | skip.montanaro | 2007-03-11 20:30:50 -0700 (Sun, 11 Mar 2007) | 4 lines Sane humans would call these invalid tests, but Andrew McNamara pointed out that given the inputs in these tests Excel does indeed produce the output these tests expect. Document that for future confused folks. ........ r54283 | martin.v.loewis | 2007-03-12 03:50:39 -0700 (Mon, 12 Mar 2007) | 2 lines Bug #1675511: Use -Kpic instead of -xcode=pic32 on Solaris/x86. ........ r54285 | martin.v.loewis | 2007-03-12 04:01:10 -0700 (Mon, 12 Mar 2007) | 2 lines Patch #1677862: Require a space or tab after import in .pth files. ........ r54287 | georg.brandl | 2007-03-12 06:17:36 -0700 (Mon, 12 Mar 2007) | 8 lines Backport from Py3k branch: Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir. Had to change a few bits of the patch because classobjs and __methods__ are still in Py2.6. ........ r54288 | georg.brandl | 2007-03-12 07:30:05 -0700 (Mon, 12 Mar 2007) | 3 lines Bug #1678647: write a newline after printing an exception in any case, even when converting the value to a string failed. ........ r54290 | collin.winter | 2007-03-12 08:57:19 -0700 (Mon, 12 Mar 2007) | 1 line Patch #1678088: convert test_operations to use unittest, fold the result into test_dict. ........ r54291 | collin.winter | 2007-03-12 09:11:39 -0700 (Mon, 12 Mar 2007) | 3 lines Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167. Will backport. ........ r54292 | georg.brandl | 2007-03-12 09:15:09 -0700 (Mon, 12 Mar 2007) | 2 lines Typo fix. ........ r54295 | collin.winter | 2007-03-12 10:24:07 -0700 (Mon, 12 Mar 2007) | 1 line Patch #1670993: Refactor test_threadedtempfile.py to use unittest. ........ r54296 | tim.peters | 2007-03-12 11:07:52 -0700 (Mon, 12 Mar 2007) | 2 lines Whitespace normalization. ........ r54297 | tim.peters | 2007-03-12 11:09:22 -0700 (Mon, 12 Mar 2007) | 2 lines Set missing svn:eol-style property on text files. ........ r54315 | brett.cannon | 2007-03-12 19:34:09 -0700 (Mon, 12 Mar 2007) | 8 lines Add test.test_support.transient_internet . Returns a context manager that nests test.test_support.TransientResource context managers that capture exceptions raised when the Internet connection is flaky. Initially using in test_socket_ssl but should probably be expanded to cover any test that should not raise the captured exceptions if the Internet connection works. ........ r54316 | brett.cannon | 2007-03-12 20:05:40 -0700 (Mon, 12 Mar 2007) | 2 lines Fix a typo where the variable name was not updated. ........ r54318 | neal.norwitz | 2007-03-12 21:59:58 -0700 (Mon, 12 Mar 2007) | 1 line Add Jerry Seutter for a bunch of his recent patches refactoring tests ........ r54319 | neal.norwitz | 2007-03-12 22:07:14 -0700 (Mon, 12 Mar 2007) | 7 lines Add some other acks for recent checkins: Brian Leair - 922167 Tomer Filiba - 1591665 Jeremy Jones - 1192590 ........ r54321 | neal.norwitz | 2007-03-12 22:31:38 -0700 (Mon, 12 Mar 2007) | 9 lines Fix some style nits: * lines too long * wrong indentation * space after a function name * wrong function name in error string * simplifying some logic Also add an error check to PyDict_SetItemString. ........ r54322 | georg.brandl | 2007-03-13 00:23:16 -0700 (Tue, 13 Mar 2007) | 2 lines Typo and grammar fixes. ........ r54323 | georg.brandl | 2007-03-13 00:50:57 -0700 (Tue, 13 Mar 2007) | 2 lines Patch #1679379: add documentation for fnmatch.translate(). ........ r54325 | georg.brandl | 2007-03-13 00:57:51 -0700 (Tue, 13 Mar 2007) | 2 lines Patch #1642844: comments to clarify the complexobject constructor. ........ r54326 | georg.brandl | 2007-03-13 01:14:27 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1668100: urllib2 now correctly raises URLError instead of OSError if accessing a local file via the file:// protocol fails. ........ r54327 | georg.brandl | 2007-03-13 02:32:11 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1635454: the csv.DictWriter class now includes the offending field names in its exception message if you try to write a record with a dictionary containing fields not in the CSV field names list. ........ r54328 | georg.brandl | 2007-03-13 02:41:31 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1555098: use str.join() instead of repeated string concatenation in robotparser. ........ r54329 | georg.brandl | 2007-03-13 03:06:48 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1542681: add entries for "with", "as" and "CONTEXTMANAGERS" to pydoc's help keywords. ........ r54331 | georg.brandl | 2007-03-13 03:19:22 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1569798: fix a bug in distutils when building Python from a directory within sys.exec_prefix. ........ r54333 | martin.v.loewis | 2007-03-13 03:24:00 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1449244: Support Unicode strings in email.message.Message.{set_charset,get_content_charset}. Will backport. ........ r54335 | lars.gustaebel | 2007-03-13 03:47:19 -0700 (Tue, 13 Mar 2007) | 34 lines This is the implementation of POSIX.1-2001 (pax) format read/write support. The TarInfo class now contains all necessary logic to process and create tar header data which has been moved there from the TarFile class. The fromtarfile() method was added. The new path and linkpath properties are aliases for the name and linkname attributes in correspondence to the pax naming scheme. The TarFile constructor and classmethods now accept a number of keyword arguments which could only be set as attributes before (e.g. dereference, ignore_zeros). The encoding and pax_headers arguments were added for pax support. There is a new tarinfo keyword argument that allows using subclassed TarInfo objects in TarFile. The boolean TarFile.posix attribute is deprecated, because now three tar formats are supported. Instead, the desired format for writing is specified using the constants USTAR_FORMAT, GNU_FORMAT and PAX_FORMAT as the format keyword argument. This change affects TarInfo.tobuf() as well. The test suite has been heavily reorganized and partially rewritten. A new testtar.tar was added that contains sample data in many formats from 4 different tar programs. Some bugs and quirks that also have been fixed: Directory names do no longer have a trailing slash in TarInfo.name or TarFile.getnames(). Adding the same file twice does not create a hardlink file member. The TarFile constructor does no longer need a name argument. The TarFile._mode attribute was renamed to mode and contains either 'r', 'w' or 'a'. ........ r54336 | georg.brandl | 2007-03-13 05:34:25 -0700 (Tue, 13 Mar 2007) | 3 lines Bug #1622896: fix a rare corner case where the bz2 module raised an error in spite of a succesful compression. ........ r54338 | lars.gustaebel | 2007-03-13 08:47:07 -0700 (Tue, 13 Mar 2007) | 3 lines Quick fix for tests that fail on systems with an encoding other than 'iso8859-1'. ........ r54339 | georg.brandl | 2007-03-13 10:43:32 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1603688: ConfigParser.SafeConfigParser now checks values that are set for invalid interpolation sequences that would lead to errors on reading back those values. ........ r54341 | georg.brandl | 2007-03-13 11:15:41 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1581073: add a flag to textwrap that prevents the dropping of whitespace while wrapping. ........ r54343 | georg.brandl | 2007-03-13 11:24:40 -0700 (Tue, 13 Mar 2007) | 2 lines Patch #1605192: list allowed states in error messages for imaplib. ........ r54344 | georg.brandl | 2007-03-13 11:31:49 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1537850: tempfile.NamedTemporaryFile now has a "delete" parameter which can be set to False to prevent the default delete-on-close behavior. ........ r54345 | collin.winter | 2007-03-13 11:53:04 -0700 (Tue, 13 Mar 2007) | 9 lines Add acks for recent patch checkins: Arvin Schnell - 1668482 S?\195?\169bastien Martini - 1481079 Heiko Wundram - 1491866 Damon Kohler - 1545011 Peter Parente - 1599845 Bjorn Lindqvist - 1678662 ........ r54346 | georg.brandl | 2007-03-13 12:00:36 -0700 (Tue, 13 Mar 2007) | 2 lines Acks for recent patches. ........ r54347 | georg.brandl | 2007-03-13 12:18:18 -0700 (Tue, 13 Mar 2007) | 3 lines Fix a tab. ........ r54348 | georg.brandl | 2007-03-13 12:32:21 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1533909: the timeit module now accepts callables in addition to strings for the code to time and the setup code. Also added two convenience functions for instantiating a Timer and calling its methods. ........ r54352 | georg.brandl | 2007-03-13 13:02:57 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1530482: add pydoc.render_doc() which returns the documentation for a thing instead of paging it to stdout, which pydoc.doc() does. ........ r54357 | thomas.heller | 2007-03-13 13:42:52 -0700 (Tue, 13 Mar 2007) | 1 line Patch #1649190: Adding support for _Bool to ctypes as c_bool, by David Remahl. ........ r54358 | georg.brandl | 2007-03-13 13:46:32 -0700 (Tue, 13 Mar 2007) | 2 lines Patch #1444529: the builtin compile() now accepts keyword arguments. (backport) ........ r54359 | thomas.heller | 2007-03-13 14:01:39 -0700 (Tue, 13 Mar 2007) | 1 line Add versionadded marker for ctypes.c_bool. ........ r54360 | georg.brandl | 2007-03-13 14:08:15 -0700 (Tue, 13 Mar 2007) | 3 lines Patch #1393667: pdb now has a "run" command which restarts the debugged Python program, optionally with different arguments. ........ r54361 | georg.brandl | 2007-03-13 14:32:01 -0700 (Tue, 13 Mar 2007) | 3 lines Deprecate commands.getstatus(). ........ r54362 | georg.brandl | 2007-03-13 14:32:56 -0700 (Tue, 13 Mar 2007) | 2 lines NEWS entry for getstatus() deprecation. ........ r54363 | georg.brandl | 2007-03-13 14:58:44 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #1429539: pdb now correctly initializes the __main__ module for the debugged script, which means that imports from __main__ work correctly now. ........ r54364 | georg.brandl | 2007-03-13 15:07:36 -0700 (Tue, 13 Mar 2007) | 4 lines Patch #957650: "%var%" environment variable references are now properly expanded in ntpath.expandvars(), also "~user" home directory references are recognized and handled on Windows. ........ r54365 | georg.brandl | 2007-03-13 15:16:30 -0700 (Tue, 13 Mar 2007) | 2 lines Patch #1194449: correctly detect unbound methods in pydoc. ........ r54367 | georg.brandl | 2007-03-13 15:49:43 -0700 (Tue, 13 Mar 2007) | 5 lines Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters with ASCII value less than 32. Also, it correctly quotes dots only if they occur on a single line, as opposed to the previous behavior of quoting dots if they are the second character of any line. ........ r54368 | collin.winter | 2007-03-13 16:02:15 -0700 (Tue, 13 Mar 2007) | 1 line Inline PyImport_GetModulesReloading(). ........ r54371 | barry.warsaw | 2007-03-13 21:59:50 -0700 (Tue, 13 Mar 2007) | 6 lines SF bug #1582282; decode_header() incorrectly splits not-conformant RFC 2047-like headers where there is no whitespace between encoded words. This fix changes the matching regexp to include a trailing lookahead assertion that the closing ?= must be followed by whitespace, newline, or end-of-string. This also changes the regexp to add the MULTILINE flag. ........ r54372 | gregory.p.smith | 2007-03-14 00:17:40 -0700 (Wed, 14 Mar 2007) | 2 lines correct order and names of the less often used keyword parameters. ........ r54373 | gregory.p.smith | 2007-03-14 00:19:50 -0700 (Wed, 14 Mar 2007) | 5 lines Its time to stop listing (Unix, Windows) when we really mean "everything but Mac OS 9" now that nobody is likely to use Python on Mac OS 9 and most of the (Mac) platform items are all OS X special API specific since OS X is unixy enough for these modules to be available out of the box. ........ r54376 | georg.brandl | 2007-03-14 01:27:52 -0700 (Wed, 14 Mar 2007) | 4 lines Bug #767111: fix long-standing bug in urllib which caused an AttributeError instead of an IOError when the server's response didn't contain a valid HTTP status line. ........ r54378 | ziga.seilnacht | 2007-03-14 05:24:09 -0700 (Wed, 14 Mar 2007) | 4 lines Patch #1680015: Don't modify __slots__ tuple if it contains an unicode name. Remove a reference leak that happened if the name could not be converted to string. Will backport. ........ r54386 | martin.v.loewis | 2007-03-14 13:02:31 -0700 (Wed, 14 Mar 2007) | 3 lines Patch #1559413: Fix test_cmd_line if sys.executable contains a space. Will backport. ........ r54389 | brett.cannon | 2007-03-14 14:40:13 -0700 (Wed, 14 Mar 2007) | 3 lines Note how test_socket_ssl has various exceptions that deal with a flaky Net connection are silenced. ........ r54390 | brett.cannon | 2007-03-14 14:44:15 -0700 (Wed, 14 Mar 2007) | 2 lines Raise ResourceDenied in test_urllib2net when the Net connection goes bad. ........ r54391 | neal.norwitz | 2007-03-14 21:41:20 -0700 (Wed, 14 Mar 2007) | 1 line Wrap a long line and fix a typo (is -> if) ........ r54392 | georg.brandl | 2007-03-15 00:38:14 -0700 (Thu, 15 Mar 2007) | 3 lines Patch #1680978: consistently use "alive" instead of "active" in the thread lib doc. ........ r54394 | georg.brandl | 2007-03-15 00:41:30 -0700 (Thu, 15 Mar 2007) | 3 lines Patch #1681153: the wave module now closes a file object it opened if initialization failed. ........ r54397 | ziga.seilnacht | 2007-03-15 04:44:55 -0700 (Thu, 15 Mar 2007) | 3 lines Patch #1462488: prevent a segfault in object_reduce_ex() by splitting the implementation for __reduce__ and __reduce_ex__ into two separate functions. Fixes bug #931877. Will backport. ........ r54404 | collin.winter | 2007-03-15 21:11:30 -0700 (Thu, 15 Mar 2007) | 3 lines Patch #1642547: Fix an error/crash when encountering syntax errors in complex if statements. Will backport. ........ r54406 | georg.brandl | 2007-03-16 00:55:09 -0700 (Fri, 16 Mar 2007) | 5 lines Bug #1681228: the webbrowser module now correctly uses the default GNOME or KDE browser, depending on whether there is a session of one of those present. Also, it tries the Windows default browser before trying Mozilla variants. (backport) ........ r54407 | georg.brandl | 2007-03-16 01:22:40 -0700 (Fri, 16 Mar 2007) | 4 lines Patch #1273829: os.walk() now has a "followlinks" parameter. If set to True (which is not the default), it visits symlinks pointing to directories. ........ r54408 | georg.brandl | 2007-03-16 01:24:21 -0700 (Fri, 16 Mar 2007) | 2 lines Add \versionadded tag. ........ r54409 | georg.brandl | 2007-03-16 01:33:47 -0700 (Fri, 16 Mar 2007) | 2 lines RFE #1670167: fix in isinstance() docs. ........ r54412 | ziga.seilnacht | 2007-03-16 04:59:38 -0700 (Fri, 16 Mar 2007) | 3 lines Patch #1623563: allow __class__ assignment for classes with __slots__. The old and the new class are still required to have the same slot names, but the order in which they are specified is not relevant. ........ r54413 | ziga.seilnacht | 2007-03-16 05:11:11 -0700 (Fri, 16 Mar 2007) | 2 lines Whitespace cleanup. Also remove the empty lines from the previous check in. ........ r54414 | jeremy.hylton | 2007-03-16 07:49:11 -0700 (Fri, 16 Mar 2007) | 2 lines Remove warning: funcion declaration isn't a prototype ........ r54415 | jeremy.hylton | 2007-03-16 08:59:47 -0700 (Fri, 16 Mar 2007) | 11 lines Clean up formatting of this file. The file should now follow PEP 7, except that it uses 4 space indents (in the style of Py3k). This particular code would be really hard to read with the regular tab idents. Other changes: - reflow long lines - change multi-line conditionals to have test at end of line ........ r54417 | collin.winter | 2007-03-16 14:13:35 -0700 (Fri, 16 Mar 2007) | 1 line Patch #1676994: Refactor test_popen2 to use unittest. ........ r54418 | collin.winter | 2007-03-16 14:15:35 -0700 (Fri, 16 Mar 2007) | 1 line Remove test/output/test_popen2 (missed in r54417). ........ r54419 | collin.winter | 2007-03-16 15:16:08 -0700 (Fri, 16 Mar 2007) | 1 line Patch 1339796: add a relpath() function to os.path. ........ r54421 | georg.brandl | 2007-03-17 09:08:45 -0700 (Sat, 17 Mar 2007) | 5 lines Patch #1675423: PyComplex_AsCComplex() now tries to convert an object to complex using its __complex__() method before falling back to the __float__() method. Therefore, the functions in the cmath module now can operate on objects that define a __complex__() method. (backport) ........ r54423 | gregory.p.smith | 2007-03-17 15:33:35 -0700 (Sat, 17 Mar 2007) | 2 lines move note to the correct section ........ r54426 | georg.brandl | 2007-03-18 01:25:00 -0700 (Sun, 18 Mar 2007) | 2 lines Patch #1682878: the new socket methods are recv_into and recvfrom_into, not *_buf. ........ r54432 | georg.brandl | 2007-03-18 11:28:25 -0700 (Sun, 18 Mar 2007) | 2 lines Patch #1678339: test case for bug in difflib. ........ r54439 | collin.winter | 2007-03-19 11:52:08 -0700 (Mon, 19 Mar 2007) | 1 line Patch #1630118: add a SpooledTemporaryFile class to tempfile. ........ r54441 | georg.brandl | 2007-03-19 12:02:48 -0700 (Mon, 19 Mar 2007) | 2 lines Patch #1683328: fixes and enhancements for "unparse" demo. ........ r54456 | neal.norwitz | 2007-03-19 22:07:28 -0700 (Mon, 19 Mar 2007) | 1 line Add some doc that was left out from some change to platform.py ........ r54457 | neal.norwitz | 2007-03-19 22:08:23 -0700 (Mon, 19 Mar 2007) | 1 line Add a comment about 3k migration ........ r54458 | neal.norwitz | 2007-03-19 22:21:21 -0700 (Mon, 19 Mar 2007) | 1 line Get rid of deprecation warning when testing commands.getstatus() ........ r54459 | neal.norwitz | 2007-03-19 22:23:09 -0700 (Mon, 19 Mar 2007) | 4 lines Try backing out 54407 to see if it corrects the problems on the Windows buildbots. This rev was backported, so we will need to keep both branches in sync, pending the outcome of the test after this checkin. ........ r54460 | neal.norwitz | 2007-03-19 23:13:25 -0700 (Mon, 19 Mar 2007) | 1 line Try to make this test more resistant to dropping from previous runs (ie, files that may exist but cause the test to fail). Should be backported (assuming it works :-) ........ r54461 | neal.norwitz | 2007-03-19 23:16:26 -0700 (Mon, 19 Mar 2007) | 1 line Try to make this test more resistant to dropping from previous runs (ie, files that may exist but cause the test to fail). Should be backported (assuming it works :-) ........ r54462 | neal.norwitz | 2007-03-19 23:53:17 -0700 (Mon, 19 Mar 2007) | 5 lines Try to be a little more resilient to errors. This might help the test pass, but my guess is that it won't. I'm guessing that some other test is leaving this file open which means it can't be removed under Windows AFAIK. ........ r54463 | neal.norwitz | 2007-03-20 01:14:57 -0700 (Tue, 20 Mar 2007) | 8 lines Try to get test_urllib to pass on Windows by closing the file. I'm guessing that's the problem. h.getfile() must be called *after* h.getreply() and the fp can be None. I'm not entirely convinced this is the best fix (or even correct). The buildbots will tell us if things improve or not. I don't know if this needs to be backported (assuming it actually works). ........ r54465 | raymond.hettinger | 2007-03-20 14:27:24 -0700 (Tue, 20 Mar 2007) | 1 line Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability. ........ r54468 | georg.brandl | 2007-03-20 16:05:14 -0700 (Tue, 20 Mar 2007) | 2 lines Fix for glob.py if filesystem encoding is None. ........ r54479 | neal.norwitz | 2007-03-20 23:39:48 -0700 (Tue, 20 Mar 2007) | 1 line Remove unused file spotted by Paul Hankin ........ r54480 | georg.brandl | 2007-03-21 02:00:39 -0700 (Wed, 21 Mar 2007) | 3 lines Patch #1682205: a TypeError while unpacking an iterable is no longer masked by a generic one with the message "unpack non-sequence". ........ r54482 | georg.brandl | 2007-03-21 02:10:29 -0700 (Wed, 21 Mar 2007) | 2 lines New test for rev. 54407 which only uses directories under TESTFN. ........ r54483 | georg.brandl | 2007-03-21 02:16:53 -0700 (Wed, 21 Mar 2007) | 2 lines Patch #1684834: document some utility C API functions. ........ r54485 | georg.brandl | 2007-03-21 04:51:25 -0700 (Wed, 21 Mar 2007) | 2 lines Fix #1684254: split BROWSER contents with shlex to avoid displaying 'URL'. ........ r54487 | andrew.kuchling | 2007-03-21 07:32:43 -0700 (Wed, 21 Mar 2007) | 1 line Add comments on maintenance of this file ........ r54489 | andrew.kuchling | 2007-03-21 09:57:32 -0700 (Wed, 21 Mar 2007) | 1 line Fix sentence, and fix typo in example ........ r54490 | andrew.kuchling | 2007-03-21 09:59:20 -0700 (Wed, 21 Mar 2007) | 1 line Put code examples at left margin instead of indenting them ........ r54491 | facundo.batista | 2007-03-21 12:41:24 -0700 (Wed, 21 Mar 2007) | 1 line Minor clarification, saying that blocking means no timeout (from bug #882297) ........ r54492 | ziga.seilnacht | 2007-03-21 13:07:56 -0700 (Wed, 21 Mar 2007) | 2 lines Bug #1675967: re patterns pickled with older Python versions can now be unpickled. Will backport. ........ r54495 | raymond.hettinger | 2007-03-21 13:33:57 -0700 (Wed, 21 Mar 2007) | 1 line Add test and fix for fromkeys() optional argument. ........ r54524 | georg.brandl | 2007-03-22 01:05:45 -0700 (Thu, 22 Mar 2007) | 2 lines Bug #1685704: use -m switch in timeit docs. ........ r54533 | thomas.heller | 2007-03-22 12:44:31 -0700 (Thu, 22 Mar 2007) | 5 lines Back out "Patch #1643874: memory leak in ctypes fixed." The code in this patch leaves no way to give up the ownership of a BSTR instance. ........ r54538 | thomas.heller | 2007-03-22 13:34:37 -0700 (Thu, 22 Mar 2007) | 2 lines Explain the purpose of the b_needsfree flag (forward ported from release25-maint). ........ r54539 | guido.van.rossum | 2007-03-22 21:58:42 -0700 (Thu, 22 Mar 2007) | 12 lines - Bug #1683368: The object.__init__() and object.__new__() methods are now stricter in rejecting excess arguments. The only time when either allows excess arguments is when it is not overridden and the other one is. For backwards compatibility, when both are overridden, it is a deprecation warning (for now; maybe a Py3k warning later). When merging this into 3.0, the warnings should become errors. Note: without the change to string.py, lots of spurious warnings happen. What's going on there? ........ r54540 | neal.norwitz | 2007-03-22 22:17:23 -0700 (Thu, 22 Mar 2007) | 1 line Add Mark Dickinson for SF # 1675423. ........ r54541 | martin.v.loewis | 2007-03-23 03:35:49 -0700 (Fri, 23 Mar 2007) | 3 lines Patch #1686451: Fix return type for PySequence_{Count,Index,Fast_GET_SIZE}. Will backport. ........ r54543 | martin.v.loewis | 2007-03-23 06:27:15 -0700 (Fri, 23 Mar 2007) | 3 lines Bug #978833: Revert r50844, as it broke _socketobject.dup. Will backport. ........ r54545 | guido.van.rossum | 2007-03-23 11:53:03 -0700 (Fri, 23 Mar 2007) | 8 lines Add a type.__init__() method that enforces the same signature as type.__new__(), and then calls object.__init__(cls), just to be anal. This allows us to restore the code in string.py's _TemplateMetaclass that called super(...).__init__(name, bases, dct), which I commented out yesterday since it broke due to the stricter argument checking added to object.__init__(). ........ r54546 | facundo.batista | 2007-03-23 11:54:07 -0700 (Fri, 23 Mar 2007) | 4 lines Added a 'create_connect()' function to socket.py, which creates a connection with an optional timeout, and modified httplib.py to use this function in HTTPConnection. Applies patch 1676823. ........ r54547 | guido.van.rossum | 2007-03-23 12:39:01 -0700 (Fri, 23 Mar 2007) | 2 lines Add note about type.__init__(). ........ r54553 | thomas.heller | 2007-03-23 12:55:27 -0700 (Fri, 23 Mar 2007) | 5 lines Prevent creation (followed by a segfault) of array types when the size overflows the valid Py_ssize_t range. Check return values of PyMem_Malloc. Will backport to release25-maint. ........ r54555 | facundo.batista | 2007-03-23 13:23:08 -0700 (Fri, 23 Mar 2007) | 6 lines Surrounded with try/finally to socket's default timeout setting changes in the tests, so failing one test won't produce strange results in others. Also relaxed the timeout settings in the test (where actually the value didn't mean anything). ........ r54556 | collin.winter | 2007-03-23 15:24:39 -0700 (Fri, 23 Mar 2007) | 1 line Make test_relpath() pass on Windows. ........ r54559 | ziga.seilnacht | 2007-03-24 07:24:26 -0700 (Sat, 24 Mar 2007) | 6 lines Patch #1489771: update syntax rules in Python Reference Manual. Python 2.5 added support for explicit relative import statements and yield expressions, which were missing in the manual. Also fix grammar productions that used the names from the Grammar file, markup that broke the generated grammar.txt, and wrap some lines that broke the pdf output. Will backport. ........ r54565 | georg.brandl | 2007-03-24 15:20:34 -0700 (Sat, 24 Mar 2007) | 2 lines Remove typo accent. ........ r54566 | georg.brandl | 2007-03-24 15:27:56 -0700 (Sat, 24 Mar 2007) | 2 lines Revert accidental change. ........ r54567 | brett.cannon | 2007-03-24 18:32:36 -0700 (Sat, 24 Mar 2007) | 3 lines Change the docs to no longer claim that unittest is preferred over doctest for regression tests. ........ r54568 | facundo.batista | 2007-03-24 18:53:21 -0700 (Sat, 24 Mar 2007) | 4 lines Redone the tests, using the infrastructure already present for threading and socket serving. ........ r54570 | facundo.batista | 2007-03-24 20:20:05 -0700 (Sat, 24 Mar 2007) | 3 lines Closing the HTTP connection after each test, and listening more. ........ r54572 | georg.brandl | 2007-03-25 11:44:35 -0700 (Sun, 25 Mar 2007) | 2 lines Markup fix. ........ r54573 | georg.brandl | 2007-03-25 12:04:55 -0700 (Sun, 25 Mar 2007) | 2 lines Markup fix. ........ r54580 | facundo.batista | 2007-03-26 13:18:31 -0700 (Mon, 26 Mar 2007) | 5 lines Added an optional timeout to FTP class. Also I started a test_ftplib.py file to test the ftp lib (right now I included a basic test, the timeout one, and nothing else). ........ r54581 | georg.brandl | 2007-03-26 13:28:28 -0700 (Mon, 26 Mar 2007) | 2 lines Some nits. ........ r54582 | facundo.batista | 2007-03-26 13:56:09 -0700 (Mon, 26 Mar 2007) | 4 lines Forgot to add the file before the previous commit, here go the ftplib tests. ........ r54585 | facundo.batista | 2007-03-27 11:23:21 -0700 (Tue, 27 Mar 2007) | 5 lines Added an optional timeout to poplib.POP3. Also created a test_poplib.py file with a basic test and the timeout ones. Docs are also updated. ........ r54586 | facundo.batista | 2007-03-27 11:50:29 -0700 (Tue, 27 Mar 2007) | 3 lines The basic test cases of poplib.py. ........ r54594 | facundo.batista | 2007-03-27 20:45:20 -0700 (Tue, 27 Mar 2007) | 4 lines Bug 1688393. Adds a control of negative values in socket.recvfrom, which caused an ugly crash. ........ r54599 | facundo.batista | 2007-03-28 11:25:54 -0700 (Wed, 28 Mar 2007) | 5 lines Added timeout to smtplib (to SMTP and SMTP_SSL). Also created the test_smtplib.py file, with a basic test and the timeout ones. Docs are updated too. ........ r54603 | collin.winter | 2007-03-28 16:34:06 -0700 (Wed, 28 Mar 2007) | 3 lines Consolidate patches #1690164, 1683397, and 1690169, all of which refactor XML-related test suites. The patches are applied together because they use a common output/xmltests file. Thanks to Jerry Seutter for all three patches. ........ r54604 | collin.winter | 2007-03-28 19:28:16 -0700 (Wed, 28 Mar 2007) | 1 line Make test_zipfile clean up its temporary files properly. ........ r54605 | georg.brandl | 2007-03-29 00:41:32 -0700 (Thu, 29 Mar 2007) | 2 lines These are actually methods. ........ r54606 | georg.brandl | 2007-03-29 05:42:07 -0700 (Thu, 29 Mar 2007) | 4 lines In Windows' time.clock(), when QueryPerformanceFrequency() fails, the C lib's clock() is used, but it must be divided by CLOCKS_PER_SEC as for the POSIX implementation (thanks to #pypy). ........ r54608 | facundo.batista | 2007-03-29 11:22:35 -0700 (Thu, 29 Mar 2007) | 5 lines Added timout parameter to telnetlib.Telnet. Also created test_telnetlib.py with a basic test and timeout ones. Docs are also updated. ........ r54613 | facundo.batista | 2007-03-30 06:00:35 -0700 (Fri, 30 Mar 2007) | 4 lines Added the posibility to pass the timeout to FTP.connect, not only when instantiating the class. Docs and tests are updated. ........ r54614 | collin.winter | 2007-03-30 07:01:25 -0700 (Fri, 30 Mar 2007) | 1 line Bug #1688274: add documentation for C-level class objects. ........ r54615 | marc-andre.lemburg | 2007-03-30 08:01:42 -0700 (Fri, 30 Mar 2007) | 4 lines Bump the patch level version of distutils since there were a few bug fixes since the 2.5.0 release. ........ r54617 | georg.brandl | 2007-03-30 08:49:05 -0700 (Fri, 30 Mar 2007) | 2 lines Markup fix. ........ r54618 | georg.brandl | 2007-03-30 10:39:39 -0700 (Fri, 30 Mar 2007) | 2 lines Label name fix. ........ r54619 | georg.brandl | 2007-03-30 10:47:21 -0700 (Fri, 30 Mar 2007) | 2 lines Duplicate label fix. ........ r54620 | georg.brandl | 2007-03-30 10:48:39 -0700 (Fri, 30 Mar 2007) | 2 lines Markup fix. ........ r54623 | andrew.kuchling | 2007-03-30 11:00:15 -0700 (Fri, 30 Mar 2007) | 1 line Add item. (Oops, accidentally checked this in on my branch) ........ r54624 | georg.brandl | 2007-03-30 12:01:38 -0700 (Fri, 30 Mar 2007) | 2 lines Duplicate label fix. ........ r54625 | georg.brandl | 2007-03-30 12:14:02 -0700 (Fri, 30 Mar 2007) | 2 lines Markup fix. ........ r54629 | georg.brandl | 2007-03-31 03:17:31 -0700 (Sat, 31 Mar 2007) | 2 lines repair string literal. ........ r54630 | georg.brandl | 2007-03-31 04:54:58 -0700 (Sat, 31 Mar 2007) | 2 lines Markup fix. ........ r54631 | georg.brandl | 2007-03-31 04:58:36 -0700 (Sat, 31 Mar 2007) | 2 lines Duplicate label fix. ........ r54632 | georg.brandl | 2007-03-31 04:59:54 -0700 (Sat, 31 Mar 2007) | 2 lines Typo fix. ........ r54633 | neal.norwitz | 2007-03-31 11:54:18 -0700 (Sat, 31 Mar 2007) | 1 line Fix method names. Will backport. ........ r54634 | georg.brandl | 2007-03-31 11:56:11 -0700 (Sat, 31 Mar 2007) | 4 lines Bug #1655392: don't add -L/usr/lib/pythonX.Y/config to the LDFLAGS returned by python-config if Python was built with --enable-shared because that prevented the shared library from being used. ........ r54637 | collin.winter | 2007-03-31 12:31:34 -0700 (Sat, 31 Mar 2007) | 1 line Shut up an occaisonal buildbot error due to test files being left around. ........ r54644 | neal.norwitz | 2007-04-01 11:24:22 -0700 (Sun, 01 Apr 2007) | 11 lines SF #1685563, MSVCCompiler creates redundant and long PATH strings If MSVCCompiler.initialize() was called multiple times, the path would get duplicated. On Windows, this is a problem because the path is limited to 4k. There's no benefit in adding a path multiple times, so prevent that from occuring. We also normalize the path before checking for duplicates so things like /a and /a/ won't both be stored. Will backport. ........ r54646 | brett.cannon | 2007-04-01 11:47:27 -0700 (Sun, 01 Apr 2007) | 8 lines time.strptime's caching of its locale object was being recreated when the locale changed but not used during the function call it was recreated during. The test in this checkin is untested (OS X does not have the proper locale support for me to test), although the fix for the bug this deals with was tested by the OP (#1290505). Once the buildbots verify the test at least doesn't fail it becomes a backport candidate. ........ r54647 | brett.cannon | 2007-04-01 12:46:19 -0700 (Sun, 01 Apr 2007) | 3 lines Fix the test for recreating the locale cache object by not worrying about if one of the test locales cannot be set. ........ r54649 | georg.brandl | 2007-04-01 14:29:15 -0700 (Sun, 01 Apr 2007) | 2 lines Fix a lot of markup and meta-information glitches. ........ r54650 | georg.brandl | 2007-04-01 14:39:52 -0700 (Sun, 01 Apr 2007) | 2 lines Another fix. ........ r54651 | georg.brandl | 2007-04-01 15:39:10 -0700 (Sun, 01 Apr 2007) | 2 lines Lots of explicit class names for method and member descs. ........ r54652 | georg.brandl | 2007-04-01 15:40:12 -0700 (Sun, 01 Apr 2007) | 2 lines Explicit class names. ........ r54653 | georg.brandl | 2007-04-01 15:47:31 -0700 (Sun, 01 Apr 2007) | 2 lines Some semantic fixes. ........ r54654 | georg.brandl | 2007-04-01 16:29:10 -0700 (Sun, 01 Apr 2007) | 2 lines Remove bogus entry. ........ r54655 | georg.brandl | 2007-04-01 16:31:30 -0700 (Sun, 01 Apr 2007) | 2 lines Fix the class name of strings. ........ r54658 | raymond.hettinger | 2007-04-02 10:29:30 -0700 (Mon, 02 Apr 2007) | 1 line SF #1693079: Cannot save empty array in shelve ........ r54663 | raymond.hettinger | 2007-04-02 15:54:21 -0700 (Mon, 02 Apr 2007) | 3 lines Array module's buffer interface can now handle empty arrays. ........ r54664 | guido.van.rossum | 2007-04-02 16:55:37 -0700 (Mon, 02 Apr 2007) | 5 lines Fix warnings about object.__init__() signature. Two (test_array and test_descr) were bug IMO; the third (copy_reg) is a work-around which recognizes that object.__init__() doesn't do anything. ........ r54666 | raymond.hettinger | 2007-04-02 17:02:11 -0700 (Mon, 02 Apr 2007) | 1 line SF 1602378 Clarify docstrings for bisect ........ r54668 | raymond.hettinger | 2007-04-02 18:39:43 -0700 (Mon, 02 Apr 2007) | 3 lines SF #1382213: Tutorial section 9.5.1 ignores MRO for new-style classes ........ r54669 | matthias.klose | 2007-04-02 21:35:59 -0700 (Mon, 02 Apr 2007) | 4 lines - Fix an off-by-one bug in locale.strxfrm(). patch taken from http://bugs.debian.org/416934. ........ r54671 | georg.brandl | 2007-04-03 00:04:27 -0700 (Tue, 03 Apr 2007) | 9 lines Fix the strange case of \begin{methoddesc}[NNTP]{...} where \ifx#1\@undefined ended up comparing N and N, therefore executing the true part of the conditional, blowing up at \@undefined. ........ r54672 | facundo.batista | 2007-04-03 07:05:08 -0700 (Tue, 03 Apr 2007) | 4 lines Now using unittest for the tests infraestructure. Also split the tests in those who need the network, and that who doesn't. ........ r54673 | walter.doerwald | 2007-04-03 09:08:10 -0700 (Tue, 03 Apr 2007) | 4 lines Move the functionality for catching warnings in test_warnings.py into a separate class to that reusing the functionality in test_structmembers.py doesn't rerun the tests from test_warnings.py. ........ r54674 | walter.doerwald | 2007-04-03 09:16:24 -0700 (Tue, 03 Apr 2007) | 2 lines Document that CatchWarningTests is reused by test_structmembers.py. ........ r54675 | walter.doerwald | 2007-04-03 09:53:43 -0700 (Tue, 03 Apr 2007) | 4 lines Add tests for the filename. Test that the stacklevel is handled correctly. ........ r54676 | facundo.batista | 2007-04-03 10:29:48 -0700 (Tue, 03 Apr 2007) | 6 lines Added a SSL server to test_socket_ssl.py to be able to test locally. Now, it checks if have openssl available and run those specific tests (it starts openssl at the beggining of all the tests and then kills it at the end). ........ r54677 | walter.doerwald | 2007-04-03 11:33:29 -0700 (Tue, 03 Apr 2007) | 6 lines Implement a contextmanager test.test_support.catch_warning that can be used to catch the last warning issued by the warning framework. Change test_warnings.py and test_structmembers.py to use this new contextmanager. ........ r54678 | facundo.batista | 2007-04-03 14:15:34 -0700 (Tue, 03 Apr 2007) | 4 lines Changed the whole structure of startup and checking if the server is available. Hope to not get more false alarms. ........ r54681 | facundo.batista | 2007-04-04 07:10:40 -0700 (Wed, 04 Apr 2007) | 4 lines Fixed the way that the .pem files are looked for, and changed how to kill the process in win32 to use the _handle attribute. ........ r54682 | guido.van.rossum | 2007-04-04 10:43:02 -0700 (Wed, 04 Apr 2007) | 4 lines Fix a race condition in this test -- instead of assuming that it will take the test server thread at most 0.5 seconds to get ready, use an event variable. ........ r54683 | collin.winter | 2007-04-04 11:14:17 -0700 (Wed, 04 Apr 2007) | 1 line Clean up imports. ........ r54684 | collin.winter | 2007-04-04 11:16:24 -0700 (Wed, 04 Apr 2007) | 1 line Stop using test_support.verify(). ........ r54685 | martin.v.loewis | 2007-04-04 11:30:36 -0700 (Wed, 04 Apr 2007) | 2 lines Bug #1686475: Support stat'ing open files on Windows again. Will backport to 2.5. ........ r54687 | collin.winter | 2007-04-04 11:33:40 -0700 (Wed, 04 Apr 2007) | 1 line Make test_getopt use unittest. ........ r54688 | collin.winter | 2007-04-04 11:36:30 -0700 (Wed, 04 Apr 2007) | 1 line Make test_softspace use unittest. ........ r54689 | ziga.seilnacht | 2007-04-04 11:38:47 -0700 (Wed, 04 Apr 2007) | 2 lines Fix WalkTests.test_traversal() on Windows. The cleanup in MakedirTests.setUp() can now be removed. ........ r54695 | raymond.hettinger | 2007-04-05 11:00:03 -0700 (Thu, 05 Apr 2007) | 3 lines Bug #1563759: struct.unpack doens't support buffer protocol objects ........ r54697 | collin.winter | 2007-04-05 13:05:07 -0700 (Thu, 05 Apr 2007) | 1 line Convert test_long_future to use unittest. ........ r54698 | collin.winter | 2007-04-05 13:08:56 -0700 (Thu, 05 Apr 2007) | 1 line Convert test_normalization to use unittest. ........ r54699 | andrew.kuchling | 2007-04-05 18:11:58 -0700 (Thu, 05 Apr 2007) | 1 line Some grammar fixes ........ r54704 | collin.winter | 2007-04-06 12:27:40 -0700 (Fri, 06 Apr 2007) | 1 line Convert test_stringprep to use unittest. ........ r54705 | collin.winter | 2007-04-06 12:32:32 -0700 (Fri, 06 Apr 2007) | 1 line Import cleanup in test_crypt. ........ r54706 | collin.winter | 2007-04-06 13:00:05 -0700 (Fri, 06 Apr 2007) | 1 line Convert test_gc to use unittest. ........ r54707 | collin.winter | 2007-04-06 13:03:11 -0700 (Fri, 06 Apr 2007) | 1 line Convert test_module to use unittest. ........ r54711 | collin.winter | 2007-04-06 21:40:43 -0700 (Fri, 06 Apr 2007) | 1 line Convert test_fileinput to use unittest. ........ r54712 | brett.cannon | 2007-04-07 21:29:32 -0700 (Sat, 07 Apr 2007) | 5 lines Doc that file.next() has undefined behaviour when called on a file opened with 'w'. Closes bug #1569057. To be backported once 2.5 branch is unfrozen. ........ r54726 | vinay.sajip | 2007-04-09 09:16:10 -0700 (Mon, 09 Apr 2007) | 1 line Added optional timeout to SocketHandler.makeSocket (SF #1695948) ........ r54727 | ziga.seilnacht | 2007-04-09 12:10:29 -0700 (Mon, 09 Apr 2007) | 3 lines Patch #1695862: remove old test directory that causes test_urllib failures on Windows buildbots. The change is a one time fix and will be removed after a successful buildbot run. ........ r54729 | facundo.batista | 2007-04-09 20:00:37 -0700 (Mon, 09 Apr 2007) | 3 lines Minor fix to the tests pass ok even with -O. ........ r54730 | collin.winter | 2007-04-09 21:44:49 -0700 (Mon, 09 Apr 2007) | 1 line Typo fix. ........ r54732 | facundo.batista | 2007-04-10 05:58:45 -0700 (Tue, 10 Apr 2007) | 5 lines General clean-up. Lot of margin corrections, comments, some typos. Exceptions now are raised in the new style. And a mockup class is now also new style. Thanks Santiago Pereson. ........ r54741 | georg.brandl | 2007-04-10 14:39:38 -0700 (Tue, 10 Apr 2007) | 2 lines Repair a duplicate label and some obsolete uses of \setindexsubitem. ........ r54746 | andrew.kuchling | 2007-04-11 06:39:00 -0700 (Wed, 11 Apr 2007) | 1 line Add window.chgat() method, submitted via e-mail by Fabian Kreutz ........ r54747 | andrew.kuchling | 2007-04-11 06:42:25 -0700 (Wed, 11 Apr 2007) | 1 line Point readers at the patch submission instructions ........ r54748 | andrew.kuchling | 2007-04-11 06:47:13 -0700 (Wed, 11 Apr 2007) | 1 line Describe undocumented third argument to touchline() ........ r54757 | georg.brandl | 2007-04-11 10:16:24 -0700 (Wed, 11 Apr 2007) | 3 lines Add some missing NULL checks which trigger crashes on low-memory conditions. Found by Victor Stinner. Will backport when 2.5 branch is unfrozen. ........ r54760 | raymond.hettinger | 2007-04-11 11:40:58 -0700 (Wed, 11 Apr 2007) | 1 line SF 1191699: Make slices picklable ........ r54762 | georg.brandl | 2007-04-11 12:25:11 -0700 (Wed, 11 Apr 2007) | 2 lines Exceptions are no longer old-style instances. Fix accordingly. ........ r54763 | georg.brandl | 2007-04-11 16:28:44 -0700 (Wed, 11 Apr 2007) | 2 lines Repair missing spaces after \UNIX. ........ r54772 | raymond.hettinger | 2007-04-11 21:10:00 -0700 (Wed, 11 Apr 2007) | 1 line SF 1193128: Let str.translate(None) be an identity transformation ........ r54784 | georg.brandl | 2007-04-12 00:01:19 -0700 (Thu, 12 Apr 2007) | 2 lines Patch #1698951: clarify deprecation message in rexec and Bastion ........ r54785 | ziga.seilnacht | 2007-04-12 01:46:51 -0700 (Thu, 12 Apr 2007) | 2 lines Patch #1695862: remove the cleanup code, now that Windows buildbots are green again. ........ r54786 | walter.doerwald | 2007-04-12 03:35:00 -0700 (Thu, 12 Apr 2007) | 3 lines Fix utf-8-sig incremental decoder, which didn't recognise a BOM when the first chunk fed to the decoder started with a BOM, but was longer than 3 bytes. ........ r54807 | barry.warsaw | 2007-04-13 11:47:14 -0700 (Fri, 13 Apr 2007) | 8 lines Port r54805 from python25-maint branch: Add code to read from master_fd in the parent, breaking when we get an OSError (EIO can occur on Linux) or there's no more data to read. Without this, test_pty.py can hang on the waitpid() because the child is blocking on the stdout write. This will definitely happen on Mac OS X and could potentially happen on other platforms. See the comment for details. ........ r54812 | kristjan.jonsson | 2007-04-13 15:07:33 -0700 (Fri, 13 Apr 2007) | 1 line Fix a bug when using the __lltrace__ opcode tracer, and a problem sith signed chars in frameobject.c which can occur with opcodes > 127 ........ r54814 | kristjan.jonsson | 2007-04-13 15:20:13 -0700 (Fri, 13 Apr 2007) | 1 line Fix potential crash in path manipulation on windows ........ r54816 | trent.mick | 2007-04-13 16:22:05 -0700 (Fri, 13 Apr 2007) | 4 lines Add the necessary dependency for the Windows VC6 build to ensure 'pythoncore' is built before '_ctypes' is attempted. Will backport to 2.5 once it is unfrozen for 2.5.1. ........ r54825 | neal.norwitz | 2007-04-13 22:25:50 -0700 (Fri, 13 Apr 2007) | 3 lines When __slots__ are set to a unicode string, make it work the same as setting a plain string, ie don't expand to single letter identifiers. ........ r54841 | neal.norwitz | 2007-04-16 00:37:55 -0700 (Mon, 16 Apr 2007) | 1 line SF #1701207, Fix bogus assertion (and test it!) ........ r54844 | collin.winter | 2007-04-16 15:10:32 -0700 (Mon, 16 Apr 2007) | 1 line Check the availability of the urlfetch resource earlier than before. ........ r54849 | martin.v.loewis | 2007-04-16 22:02:01 -0700 (Mon, 16 Apr 2007) | 2 lines Add Travis Oliphant. ........ r54873 | brett.cannon | 2007-04-18 20:44:17 -0700 (Wed, 18 Apr 2007) | 2 lines Silence a compiler warning about incompatible pointer types. ........ r54874 | neal.norwitz | 2007-04-18 22:52:37 -0700 (Wed, 18 Apr 2007) | 2 lines SF #1703270, add missing declaration in readline.c to avoid compiler warning. ........ r54875 | armin.rigo | 2007-04-19 07:44:48 -0700 (Thu, 19 Apr 2007) | 8 lines Revert r53997 as per http://mail.python.org/pipermail/python-dev/2007-March/071796.html . I've kept a couple of still-valid extra tests in test_descr, but didn't bother to sort through the new comments and refactorings added in r53997 to see if some of them could be kept. If so, they could go in a follow-up check-in. ........ r54876 | armin.rigo | 2007-04-19 07:56:48 -0700 (Thu, 19 Apr 2007) | 2 lines Fix a usage of the dangerous pattern decref - modify field - incref. ........ r54884 | neal.norwitz | 2007-04-19 22:20:38 -0700 (Thu, 19 Apr 2007) | 9 lines Add an optional address to copy the failure mails to. Detect a conflict in the only file that should have outstanding changes when this script is run. This doesn't matter on the trunk, but does when run on a branch. Trunk always has the date set to today in boilerplate.tex. Each time a release is cut with a different date, a conflict occurs. (We could copy a known good version, but then we would lose changes to this file.) ........ r54918 | georg.brandl | 2007-04-21 13:35:38 -0700 (Sat, 21 Apr 2007) | 3 lines Bug #1704790: bind name "sys" locally in __del__ method so that it is not cleared before __del__ is run. ........ r54920 | facundo.batista | 2007-04-21 18:18:56 -0700 (Sat, 21 Apr 2007) | 5 lines Added tests for other methods of SSL object. Now we cover all the object methods. This is the final step to close the #451607 bug. ........ r54927 | facundo.batista | 2007-04-23 10:08:31 -0700 (Mon, 23 Apr 2007) | 5 lines As specified in RFC 2616, 2xx code indicates that the client's request was successfully received, understood, and accepted. Now in these cases no error is raised. Also fixed tests. ........ r54929 | collin.winter | 2007-04-23 20:43:46 -0700 (Mon, 23 Apr 2007) | 1 line Convert PyUnit -> unittest. ........ r54931 | collin.winter | 2007-04-23 21:09:52 -0700 (Mon, 23 Apr 2007) | 1 line Remove code that hasn't been called in years. ........ r54932 | neal.norwitz | 2007-04-23 21:53:12 -0700 (Mon, 23 Apr 2007) | 1 line Fix SF #1703110, Incorrect example for add_password() (use uri, not host) ........ r54934 | georg.brandl | 2007-04-24 03:36:42 -0700 (Tue, 24 Apr 2007) | 2 lines Some new year updates. ........ r54938 | facundo.batista | 2007-04-24 06:54:38 -0700 (Tue, 24 Apr 2007) | 4 lines Added a comment about last change in urllib2.py (all 2xx responses are ok now). ........ r54939 | georg.brandl | 2007-04-24 08:10:09 -0700 (Tue, 24 Apr 2007) | 2 lines Bug #1705717: error in sys.argv docs. ........ r54941 | georg.brandl | 2007-04-24 08:27:13 -0700 (Tue, 24 Apr 2007) | 4 lines Bug #1706381: Specifying the SWIG option "-c++" in the setup.py file (as opposed to the command line) will now write file names ending in ".cpp" too. ........ r54944 | raymond.hettinger | 2007-04-24 15:13:43 -0700 (Tue, 24 Apr 2007) | 1 line Fix markup ........ r54945 | kristjan.jonsson | 2007-04-24 17:10:50 -0700 (Tue, 24 Apr 2007) | 1 line Merge change 54909 from release25-maint: Fix several minor issues discovered using code analysis in VisualStudio 2005 Team Edition ........ r54947 | kristjan.jonsson | 2007-04-24 17:17:39 -0700 (Tue, 24 Apr 2007) | 1 line Make pythoncore compile cleanly with VisualStudio 2005. Used an explicit typecast to get a 64 bit integer, and undefined the Yield macro that conflicts with winbase.h ........ r54948 | kristjan.jonsson | 2007-04-24 17:19:26 -0700 (Tue, 24 Apr 2007) | 1 line Remove obsolete comment. Importing of .dll files has been discontinued, only .pyd files supported on windows now. ........ r54949 | georg.brandl | 2007-04-24 23:24:59 -0700 (Tue, 24 Apr 2007) | 2 lines Patch #1698768: updated the "using Python on the Mac" intro. ........ r54951 | georg.brandl | 2007-04-24 23:25:55 -0700 (Tue, 24 Apr 2007) | 2 lines Markup fix. ........ r54953 | neal.norwitz | 2007-04-24 23:30:05 -0700 (Tue, 24 Apr 2007) | 3 lines Whitespace normalization. Ugh, we really need to do this more often. You might want to review this change as it's my first time. Be gentle. :-) ........ r54956 | collin.winter | 2007-04-25 10:29:52 -0700 (Wed, 25 Apr 2007) | 1 line Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule(). ........ r54957 | collin.winter | 2007-04-25 10:37:35 -0700 (Wed, 25 Apr 2007) | 1 line Remove functionality from test_datetime.test_main() that does reference count checking; 'regrtest.py -R' is the way to do this kind of testing. ........ r54958 | collin.winter | 2007-04-25 10:57:53 -0700 (Wed, 25 Apr 2007) | 1 line Change test_support.have_unicode to use True/False instead of 1/0. ........ r54959 | tim.peters | 2007-04-25 11:47:18 -0700 (Wed, 25 Apr 2007) | 2 lines Whitespace normalization. ........ r54960 | tim.peters | 2007-04-25 11:48:35 -0700 (Wed, 25 Apr 2007) | 2 lines Set missing svn:eol-style property on text files. ........ r54961 | collin.winter | 2007-04-25 11:54:36 -0700 (Wed, 25 Apr 2007) | 1 line Import and raise statement cleanup. ........ r54969 | collin.winter | 2007-04-25 13:41:34 -0700 (Wed, 25 Apr 2007) | 1 line Convert test_ossaudiodev to use unittest. ........ r54974 | collin.winter | 2007-04-25 14:50:25 -0700 (Wed, 25 Apr 2007) | 1 line Fix an issue related to the unittest conversion. ........ r54979 | fred.drake | 2007-04-25 21:42:19 -0700 (Wed, 25 Apr 2007) | 1 line fix some markup errors ........ r54982 | kristjan.jonsson | 2007-04-26 02:15:08 -0700 (Thu, 26 Apr 2007) | 1 line Export function sanitize_the_mode from fileobject.c as _PyFile_SanitizeMode(). Use this function in posixmodule.c when implementing fdopen(). This fixes test_subprocess.py for a VisualStudio 2005 compile. ........ r54983 | kristjan.jonsson | 2007-04-26 06:44:16 -0700 (Thu, 26 Apr 2007) | 1 line The locale "En" appears not to be valid on windows underi VisualStudio.2005. Added "English" to the test_locale.py to make the testsuite pass for that build ........ r54984 | steve.holden | 2007-04-26 07:23:12 -0700 (Thu, 26 Apr 2007) | 1 line Minor wording change on slicing aide-memoire. ........ r54985 | kristjan.jonsson | 2007-04-26 08:24:54 -0700 (Thu, 26 Apr 2007) | 1 line Accomodate 64 bit time_t in the _bsddb module. ........
author Guido van Rossum <guido@python.org>
date Fri, 27 Apr 2007 19:54:29 +0000
parents 26df566f80f0
children 29ae667f3239
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 *);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
28 static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
29 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
30 static expr_ty ast_for_testlist(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 *);
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
36 static PyObject *parsestr(const node *n, const char *encoding, int *bytesmode);
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
37 static PyObject *parsestrplus(struct compiling *, const node *n,
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
38 int *bytesmode);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
39
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
40 #ifndef LINENO
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
41 #define LINENO(n) ((n)->n_lineno)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
43
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
44 #define COMP_GENEXP 0
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
45 #define COMP_LISTCOMP 1
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
46 #define COMP_SETCOMP 2
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
47
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
48 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
49 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
50 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
51 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
52 return id;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
54
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
55 #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
56
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
57 /* 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
58 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
59 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
60 the filename to everything function.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
62 XXX Maybe we should just pass the filename...
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
63 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
64
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
65 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 ast_error(const node *n, const char *errstr)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69 if (!u)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
70 return 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71 PyErr_SetObject(PyExc_SyntaxError, u);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
72 Py_DECREF(u);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
73 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
74 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
75
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
76 static void
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77 ast_error_finish(const char *filename)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
78 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
79 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
80 long lineno;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
82 assert(PyErr_Occurred());
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
83 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
84 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
85
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
86 PyErr_Fetch(&type, &value, &tback);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
87 errstr = PyTuple_GetItem(value, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
88 if (!errstr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
89 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
90 Py_INCREF(errstr);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
91 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
92 if (lineno == -1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
93 Py_DECREF(errstr);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
94 return;
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
95 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
96 Py_DECREF(value);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
97
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
98 loc = PyErr_ProgramText(filename, lineno);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
99 if (!loc) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
100 Py_INCREF(Py_None);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
101 loc = Py_None;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
102 }
36231
d3f4c467e439 Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents: 36224
diff changeset
103 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
104 Py_DECREF(loc);
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
105 if (!tmp) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
106 Py_DECREF(errstr);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
107 return;
36000
cc3261597136 SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents: 35990
diff changeset
108 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
109 value = PyTuple_Pack(2, errstr, tmp);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
110 Py_DECREF(errstr);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
111 Py_DECREF(tmp);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
112 if (!value)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
113 return;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
114 PyErr_Restore(type, value, tback);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
115 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
116
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
117 /* num_stmts() returns number of contained statements.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
118
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
119 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
120 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
121 grammar:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
122
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
123 stmt: simple_stmt | compound_stmt
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
124 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
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 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
127 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
128 small_stmt elements is returned.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
129 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
130
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
131 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
132 num_stmts(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
134 int i, l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
135 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
136
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
137 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
138 case single_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
139 if (TYPE(CHILD(n, 0)) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
140 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
141 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
142 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
143 case file_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
144 l = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
145 for (i = 0; i < NCH(n); i++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
146 ch = CHILD(n, i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
147 if (TYPE(ch) == stmt)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
148 l += num_stmts(ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
149 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
150 return l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
151 case stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
152 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
153 case compound_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
154 return 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
155 case simple_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
156 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
157 case suite:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
158 if (NCH(n) == 1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
159 return num_stmts(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
160 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
161 l = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
162 for (i = 2; i < (NCH(n) - 1); i++)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
163 l += num_stmts(CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
164 return l;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
165 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
166 default: {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
167 char buf[128];
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 sprintf(buf, "Non-statement found: %d %d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
170 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
171 Py_FatalError(buf);
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 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
174 assert(0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
175 return 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
176 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
177
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
178 /* 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
179 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
180
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
181 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
182 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
183 PyArena *arena)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
184 {
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
185 int i, j, k, num;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
186 asdl_seq *stmts = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
187 stmt_ty s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
188 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
189 struct compiling c;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
190
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
191 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
192 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
193 if (TYPE(n) == encoding_decl) {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
194 ast_error(n, "encoding declaration in Unicode string");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
195 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
196 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
197 } else if (TYPE(n) == encoding_decl) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
198 c.c_encoding = STR(n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
199 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
200 } else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
201 c.c_encoding = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
202 }
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 c.c_arena = arena;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
204
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
205 k = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
206 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
207 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
208 stmts = asdl_seq_new(num_stmts(n), arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
209 if (!stmts)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
210 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
211 for (i = 0; i < NCH(n) - 1; i++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
212 ch = CHILD(n, i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
213 if (TYPE(ch) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
214 continue;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
215 REQ(ch, stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
216 num = num_stmts(ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
217 if (num == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
218 s = ast_for_stmt(&c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
219 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
220 goto error;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
221 asdl_seq_SET(stmts, k++, s);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
222 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
223 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
224 ch = CHILD(ch, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
225 REQ(ch, simple_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
226 for (j = 0; j < num; j++) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
227 s = ast_for_stmt(&c, CHILD(ch, j * 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
228 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
229 goto error;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
230 asdl_seq_SET(stmts, k++, s);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
231 }
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 }
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
234 return Module(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
235 case eval_input: {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
236 expr_ty testlist_ast;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
237
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
238 /* XXX Why not comp_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
239 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
240 if (!testlist_ast)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
241 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
242 return Expression(testlist_ast, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
243 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
244 case single_input:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
245 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
246 stmts = asdl_seq_new(1, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
247 if (!stmts)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
248 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
249 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
250 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
251 return Interactive(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
252 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
253 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
254 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
255 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
256 stmts = asdl_seq_new(num, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
257 if (!stmts)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
258 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
259 if (num == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
260 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
261 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
262 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
263 asdl_seq_SET(stmts, 0, s);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
264 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
265 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
266 /* Only a simple_stmt can contain multiple statements. */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
267 REQ(n, simple_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
268 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
269 if (TYPE(CHILD(n, i)) == NEWLINE)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
270 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
271 s = ast_for_stmt(&c, CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
272 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
273 goto error;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
274 asdl_seq_SET(stmts, i / 2, s);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
275 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
276 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
277
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
278 return Interactive(stmts, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
279 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
280 default:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
281 goto error;
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 error:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
284 ast_error_finish(filename);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
285 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
286 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
287
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
288 /* 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
289 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
290
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
291 static operator_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
292 get_operator(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
293 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
294 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
295 case VBAR:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
296 return BitOr;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
297 case CIRCUMFLEX:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
298 return BitXor;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
299 case AMPER:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
300 return BitAnd;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
301 case LEFTSHIFT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
302 return LShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
303 case RIGHTSHIFT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
304 return RShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
305 case PLUS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
306 return Add;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
307 case MINUS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
308 return Sub;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
309 case STAR:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
310 return Mult;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
311 case SLASH:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
312 return Div;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
313 case DOUBLESLASH:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
314 return FloorDiv;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
315 case PERCENT:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
316 return Mod;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
317 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
318 return (operator_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
319 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
320 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
321
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
322 /* 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
323
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
324 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
325 (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
326 an appropriate syntax error and returns false.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
327 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
328
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
329 static int
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
330 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
331 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
332 asdl_seq *s = NULL;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
333 /* 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
334 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
335 */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
336 const char* expr_name = NULL;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
337
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
338 /* 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
339 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
340 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
341 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
342 Consider restructuring so that augmented assignment uses
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
343 set_context(), too.
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
344 */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
345 assert(ctx != AugStore && ctx != AugLoad);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
346
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
347 switch (e->kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
348 case Attribute_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
349 if (ctx == Store &&
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
350 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
351 return ast_error(n, "assignment to None");
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
352 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
353 e->v.Attribute.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
354 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
355 case Subscript_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
356 e->v.Subscript.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
357 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
358 case Name_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
359 if (ctx == Store &&
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
360 !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
361 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
362 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
363 e->v.Name.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
364 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
365 case List_kind:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
366 e->v.List.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
367 s = e->v.List.elts;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
368 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
369 case Tuple_kind:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
370 if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
371 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
372 e->v.Tuple.ctx = ctx;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
373 s = e->v.Tuple.elts;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
374 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
375 case Lambda_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
376 expr_name = "lambda";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
377 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
378 case Call_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
379 expr_name = "function call";
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
380 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
381 case BoolOp_kind:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
382 case BinOp_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
383 case UnaryOp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
384 expr_name = "operator";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
385 break;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
386 case GeneratorExp_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
387 expr_name = "generator expression";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
388 break;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
389 case Yield_kind:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
390 expr_name = "yield expression";
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
391 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
392 case ListComp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
393 expr_name = "list comprehension";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
394 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
395 case Dict_kind:
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
396 case Set_kind:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
397 case Num_kind:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
398 case Str_kind:
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
399 expr_name = "literal";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
400 break;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
401 case Ellipsis_kind:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
402 expr_name = "Ellipsis";
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
403 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
404 case Compare_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
405 expr_name = "comparison";
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
406 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
407 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
408 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
409 break;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
410 default:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
411 PyErr_Format(PyExc_SystemError,
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
412 "unexpected expression in assignment %d (line %d)",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
413 e->kind, e->lineno);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
414 return 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
415 }
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
416 /* Check for error string set by switch */
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
417 if (expr_name) {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
418 char buf[300];
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
419 PyOS_snprintf(buf, sizeof(buf),
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
420 "can't %s %s",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
421 ctx == Store ? "assign to" : "delete",
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
422 expr_name);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
423 return ast_error(n, buf);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
424 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
425
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
426 /* 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
427 context for all the contained elements.
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
428 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
429 if (s) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
430 int i;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
431
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
432 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
433 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
434 return 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
435 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
436 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
437 return 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
438 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
439
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
440 static operator_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
441 ast_for_augassign(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
442 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
443 REQ(n, augassign);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
444 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
445 switch (STR(n)[0]) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
446 case '+':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
447 return Add;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
448 case '-':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
449 return Sub;
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 if (STR(n)[1] == '/')
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
452 return FloorDiv;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
453 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
454 return Div;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
455 case '%':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
456 return Mod;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
457 case '<':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
458 return LShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
459 case '>':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
460 return RShift;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
461 case '&':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
462 return BitAnd;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
463 case '^':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
464 return BitXor;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
465 case '|':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
466 return BitOr;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
467 case '*':
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
468 if (STR(n)[1] == '*')
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
469 return Pow;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
470 else
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
471 return Mult;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
472 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
473 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
474 return (operator_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
475 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
476 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
477
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
478 static cmpop_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
479 ast_for_comp_op(const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
480 {
39648
efe3f2c0da5e Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
481 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
482 |'is' 'not'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
483 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
484 REQ(n, comp_op);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
485 if (NCH(n) == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
486 n = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
487 switch (TYPE(n)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
488 case LESS:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
489 return Lt;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
490 case GREATER:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
491 return Gt;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
492 case EQEQUAL: /* == */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
493 return Eq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
494 case LESSEQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
495 return LtE;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
496 case GREATEREQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
497 return GtE;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
498 case NOTEQUAL:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
499 return NotEq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
500 case NAME:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
501 if (strcmp(STR(n), "in") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
502 return In;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
503 if (strcmp(STR(n), "is") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
504 return Is;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
505 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
506 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
507 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
508 return (cmpop_ty)0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
509 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
510 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
511 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
512 /* 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
513 switch (TYPE(CHILD(n, 0))) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
514 case NAME:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
515 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
516 return NotIn;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
517 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
518 return IsNot;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
519 default:
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: %s %s",
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
521 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
522 return (cmpop_ty)0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
523 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
524 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
525 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
526 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
527 return (cmpop_ty)0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
528 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
529
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
530 static asdl_seq *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
531 seq_for_testlist(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
532 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
533 /* testlist: test (',' test)* [','] */
35759
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
534 asdl_seq *seq;
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
535 expr_ty expression;
d91ed7107fd2 ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents: 35748
diff changeset
536 int i;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
537 assert(TYPE(n) == testlist || TYPE(n) == testlist_comp);
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) {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
544 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == test_nocond);
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
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
556 static arg_ty
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
557 compiler_simple_arg(struct compiling *c, const node *n)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
558 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
559 identifier name;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
560 expr_ty annotation = NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
561 node *ch;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
562
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
563 assert(TYPE(n) == tname || TYPE(n) == vname);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
564 ch = CHILD(n, 0);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
565 if (!strcmp(STR(ch), "None")) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
566 ast_error(ch, "assignment to None");
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
567 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
568 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
569 name = NEW_IDENTIFIER(ch);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
570 if (!name)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
571 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
572
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
573 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
574 annotation = ast_for_expr(c, CHILD(n, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
575 if (!annotation)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
576 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
577 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
578
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
579 return SimpleArg(name, annotation, c->c_arena);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
580 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
581
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
582 static arg_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
583 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
584 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
585 int i, len = (NCH(n) + 1) / 2;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
586 arg_ty arg;
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
587 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
588 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
589 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
590
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
591 assert(TYPE(n) == tfplist || TYPE(n) == vfplist);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
592 for (i = 0; i < len; i++) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
593 const node *child = CHILD(n, 2*i);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
594 /* def foo(((x), y)): -- x is not nested complex, special case. */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
595 while (NCH(child) == 3 && NCH(CHILD(child, 1)) == 1)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
596 child = CHILD(CHILD(child, 1), 0);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
597
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
598 /* child either holds a tname or '(', a tfplist, ')' */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
599 switch (TYPE(CHILD(child, 0))) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
600 case tname:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
601 case vname:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
602 arg = compiler_simple_arg(c, CHILD(child, 0));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
603 break;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
604 case LPAR:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
605 arg = compiler_complex_args(c, CHILD(child, 1));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
606 break;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
607 default:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
608 PyErr_Format(PyExc_SystemError,
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
609 "unexpected node in args: %d @ %d",
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
610 TYPE(CHILD(child, 0)), i);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
611 arg = NULL;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
612 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
613 if (!arg)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
614 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
615 asdl_seq_SET(args, i, arg);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
616 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
617
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
618 return NestedArgs(args, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
619 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
620
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
621 /* 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
622 returns new position to keep processing if successful
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
623 (',' tname ['=' test])*
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
624 ^^^
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
625 start pointing here
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
626 */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
627 static int
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
628 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
629 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
630 {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
631 node *ch;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
632 expr_ty expression, annotation;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
633 arg_ty arg;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
634 int i = start;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
635 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
636 assert(kwonlyargs != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
637 assert(kwdefaults != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
638 while (i < NCH(n)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
639 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
640 switch (TYPE(ch)) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
641 case vname:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
642 case tname:
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
643 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
644 expression = ast_for_expr(c, CHILD(n, i + 2));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
645 if (!expression) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
646 ast_error(ch, "assignment to None");
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(kwdefaults, j, expression);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
650 i += 2; /* '=' and test */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
651 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
652 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
653 asdl_seq_SET(kwdefaults, j, NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
654 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
655 if (NCH(ch) == 3) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
656 /* ch is NAME ':' test */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
657 annotation = ast_for_expr(c, CHILD(ch, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
658 if (!annotation) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
659 ast_error(ch, "expected expression");
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
660 goto error;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
661 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
662 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
663 else {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
664 annotation = NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
665 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
666 ch = CHILD(ch, 0);
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
667 if (!strcmp(STR(ch), "None")) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
668 ast_error(ch, "assignment to None");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
669 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
670 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
671 arg = SimpleArg(NEW_IDENTIFIER(ch), annotation, c->c_arena);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
672 if (!arg) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
673 ast_error(ch, "expecting name");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
674 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
675 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
676 asdl_seq_SET(kwonlyargs, j++, arg);
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
677 i += 2; /* the name and the comma */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
678 break;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
679 case DOUBLESTAR:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
680 return i;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
681 default:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
682 ast_error(ch, "unexpected node");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
683 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
684 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
685 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
686 return i;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
687 error:
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
688 return -1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
689 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
690
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
691 /* Create AST for argument list. */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
692
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
693 static arguments_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
694 ast_for_arguments(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
695 {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
696 /* This function handles both typedargslist (function definition)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
697 and varargslist (lambda definition).
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
698
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
699 parameters: '(' [typedargslist] ')'
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
700 typedargslist: ((tfpdef ['=' test] ',')*
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
701 ('*' [tname] (',' tname ['=' test])* [',' '**' tname]
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
702 | '**' tname)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
703 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
704 varargslist: ((vfpdef ['=' test] ',')*
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
705 ('*' [vname] (',' vname ['=' test])* [',' '**' vname]
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
706 | '**' vname)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
707 | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
708 */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
709 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
710 int nposdefaults = 0, found_default = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
711 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
712 identifier vararg = NULL, kwarg = NULL;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
713 arg_ty arg;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
714 expr_ty varargannotation = NULL, kwargannotation = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
715 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
716
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
717 if (TYPE(n) == parameters) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
718 if (NCH(n) == 2) /* () as argument list */
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
719 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
720 NULL, c->c_arena);
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
721 n = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
722 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
723 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
724
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
725 /* first count the number of positional args & defaults */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
726 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
727 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
728 if (TYPE(ch) == STAR) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
729 if (TYPE(CHILD(n, i+1)) == tname
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
730 || TYPE(CHILD(n, i+1)) == vname) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
731 /* skip NAME of vararg */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
732 /* 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
733 i += 2;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
734 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
735 else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
736 i++;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
737 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
738 break;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
739 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
740 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
741 if (TYPE(ch) == EQUAL) nposdefaults++;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
742 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
743 /* count the number of keyword only args &
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
744 defaults for keyword only args */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
745 for ( ; i < NCH(n); ++i) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
746 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
747 if (TYPE(ch) == DOUBLESTAR) break;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
748 if (TYPE(ch) == tname || TYPE(ch) == vname) nkwonlyargs++;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
749 }
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
750
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
751 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
752 if (!posargs && nposargs)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
753 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
754 kwonlyargs = (nkwonlyargs ?
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
755 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
756 if (!kwonlyargs && nkwonlyargs)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
757 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
758 posdefaults = (nposdefaults ?
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
759 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
760 if (!posdefaults && nposdefaults)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
761 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
762 /* 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
763 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
764 - we have sequence data structure, but no dictionary */
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
765 kwdefaults = (nkwonlyargs ?
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
766 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
767 if (!kwdefaults && nkwonlyargs)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
768 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
769
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
770 if (nposargs + nkwonlyargs > 255) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
771 ast_error(n, "more than 255 arguments");
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
772 return NULL;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
773 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
774
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
775 /* tname: NAME [':' test]
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
776 tfpdef: tname | '(' tfplist ')'
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
777 tfplist: tfpdef (',' tfpdef)* [',']
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
778 vname: NAME
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
779 vfpdef: NAME | '(' vfplist ')'
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
780 vfplist: vfpdef (',' vfpdef)* [',']
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
781 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
782 i = 0;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
783 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
784 k = 0; /* index for args */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
785 while (i < NCH(n)) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
786 ch = CHILD(n, i);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
787 switch (TYPE(ch)) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
788 case tfpdef:
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
789 case vfpdef:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
790 /* 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
791 anything other than EQUAL or a comma? */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
792 /* 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
793 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
794 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
795 if (!expression)
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
796 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
797 assert(posdefaults != NULL);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
798 asdl_seq_SET(posdefaults, j++, expression);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
799 i += 2;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
800 found_default = 1;
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 else if (found_default) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
803 ast_error(n,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
804 "non-default argument follows default argument");
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
805 goto error;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
806 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
807 /* def foo((x)): is not complex, special case. */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
808 while (NCH(ch) == 3 && NCH(CHILD(ch, 1)) == 1)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
809 ch = CHILD(CHILD(ch, 1), 0);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
810
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
811 if (NCH(ch) != 1)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
812 arg = compiler_complex_args(c, CHILD(ch, 1));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
813 else
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
814 arg = compiler_simple_arg(c, CHILD(ch, 0));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
815 if (!arg)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
816 goto error;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
817 asdl_seq_SET(posargs, k++, arg);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
818
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
819 i += 2; /* the name and the comma */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
820 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
821 case STAR:
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
822 if (i+1 >= NCH(n)) {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
823 ast_error(CHILD(n, i), "no name for vararg");
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
824 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
825 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
826 ch = CHILD(n, i+1); /* tname or COMMA */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
827 if (TYPE(ch) == COMMA) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
828 int res = 0;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
829 i += 2; /* now follows keyword only arguments */
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
830 res = handle_keywordonly_args(c, n, i,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
831 kwonlyargs, kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
832 if (res == -1) goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
833 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
834 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
835 else if (!strcmp(STR(CHILD(ch, 0)), "None")) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
836 ast_error(CHILD(ch, 0), "assignment to None");
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
837 goto error;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
838 }
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
839 else {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
840 vararg = NEW_IDENTIFIER(CHILD(ch, 0));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
841 if (NCH(ch) > 1) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
842 /* there is an annotation on the vararg */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
843 varargannotation = ast_for_expr(c, CHILD(ch, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
844 }
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
845 i += 3;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
846 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tname
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
847 || TYPE(CHILD(n, i)) == vname)) {
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
848 int res = 0;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
849 res = handle_keywordonly_args(c, n, i,
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
850 kwonlyargs, kwdefaults);
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
851 if (res == -1) goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
852 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
853 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
854 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
855 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
856 case DOUBLESTAR:
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
857 ch = CHILD(n, i+1); /* tname */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
858 assert(TYPE(ch) == tname || TYPE(ch) == vname);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
859 if (!strcmp(STR(CHILD(ch, 0)), "None")) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
860 ast_error(CHILD(ch, 0), "assignment to None");
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
861 goto error;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
862 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
863 kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
864 if (NCH(ch) > 1) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
865 /* there is an annotation on the kwarg */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
866 kwargannotation = ast_for_expr(c, CHILD(ch, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
867 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
868 i += 3;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
869 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
870 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
871 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
872 "unexpected node in varargslist: %d @ %d",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
873 TYPE(ch), i);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
874 goto error;
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
875 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
876 }
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
877 return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
878 kwargannotation, posdefaults, kwdefaults, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
879 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
880 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
881 Py_XDECREF(kwarg);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
882 return NULL;
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
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
885 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
886 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
887 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
888 expr_ty e;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
889 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
890 int lineno, col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
891 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
892
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
893 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
894
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
895 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
896 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
897
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
898 id = NEW_IDENTIFIER(CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
899 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
900 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
901 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
902 if (!e)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
903 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
904
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
905 for (i = 2; i < NCH(n); i+=2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
906 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
907 if (!id)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
908 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
909 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
910 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
911 return 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 e;
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 expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
918 ast_for_decorator(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
919 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
920 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
921 expr_ty d = NULL;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
922 expr_ty name_expr;
35742
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, decorator);
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
925 REQ(CHILD(n, 0), AT);
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
926 REQ(RCHILD(n, -1), NEWLINE);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
927
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
928 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
929 if (!name_expr)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
930 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
931
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
932 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
933 d = name_expr;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
934 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
935 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
936 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
937 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
938 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
939 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
940 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
941 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
942 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
943 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
944 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
945 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
946 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
947 name_expr = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
948 }
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 return d;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
951 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
952
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
953 static asdl_seq*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
954 ast_for_decorators(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
955 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
956 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
957 expr_ty d;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
958 int i;
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 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
961 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
962 if (!decorator_seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
963 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
964
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
965 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
966 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
967 if (!d)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
968 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
969 asdl_seq_SET(decorator_seq, i, d);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
970 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
971 return decorator_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
972 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
973
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
974 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
975 ast_for_funcdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
976 {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
977 /* funcdef: 'def' [decorators] NAME parameters ['->' test] ':' suite */
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
978 identifier name;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
979 arguments_ty args;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
980 asdl_seq *body;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
981 asdl_seq *decorator_seq = NULL;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
982 expr_ty returns = NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
983 int name_i;
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 REQ(n, funcdef);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
986
41509
26df566f80f0 Allow decorators and return annotations to be used together (fixes SF#1697248)
Nick Coghlan <ncoghlan@gmail.com>
parents: 41482
diff changeset
987 if (NCH(n) == 6 || NCH(n) == 8) { /* decorators are present */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
988 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
989 if (!decorator_seq)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
990 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
991 name_i = 2;
35742
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 {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
994 name_i = 1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
995 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
996
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
997 name = NEW_IDENTIFIER(CHILD(n, name_i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
998 if (!name)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
999 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1000 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
1001 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
1002 return NULL;
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 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
1005 if (!args)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1006 return NULL;
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1007 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1008 returns = ast_for_expr(c, CHILD(n, name_i + 3));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1009 if (!returns)
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1010 return NULL;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1011 name_i += 2;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1012 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1013 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
1014 if (!body)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1015 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1016
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1017 return FunctionDef(name, args, body, decorator_seq, returns, 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
1018 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1019 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1020
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1021 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1022 ast_for_lambdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1023 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1024 /* lambdef: 'lambda' [varargslist] ':' test
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1025 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1026 arguments_ty args;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1027 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1028
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1029 if (NCH(n) == 3) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1030 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1031 NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1032 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1033 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1034 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
1035 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1036 return NULL;
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 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1039 args = ast_for_arguments(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1040 if (!args)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1041 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1042 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
1043 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1044 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1045 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1046
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1047 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
1048 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1049
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1050 static expr_ty
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1051 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
1052 {
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1053 /* 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
1054 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
1055
36676
67c74efad8c2 Fix old not-reading-pep-308-right artifact.
Thomas Wouters <thomas@python.org>
parents: 36670
diff changeset
1056 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
1057 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
1058 if (!body)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1059 return NULL;
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1060 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
1061 if (!expression)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1062 return NULL;
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1063 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
1064 if (!orelse)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1065 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
1066 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
1067 c->c_arena);
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1068 }
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1069
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1070 /*
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1071 Count the number of 'for' loops in a comprehension.
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1072
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1073 Helper for ast_for_comprehension().
35742
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
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1076 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1077 count_comp_fors(const node *n)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1078 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1079 int n_fors = 0;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1080 node *ch = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1081
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1082 count_comp_for:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1083 n_fors++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1084 REQ(ch, comp_for);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1085 if (NCH(ch) == 5)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1086 ch = CHILD(ch, 4);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1087 else
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1088 return n_fors;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1089 count_comp_iter:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1090 REQ(ch, comp_iter);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1091 ch = CHILD(ch, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1092 if (TYPE(ch) == comp_for)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1093 goto count_comp_for;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1094 else if (TYPE(ch) == comp_if) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1095 if (NCH(ch) == 3) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1096 ch = CHILD(ch, 2);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1097 goto count_comp_iter;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1098 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1099 else
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1100 return n_fors;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1101 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1102
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1103 /* Should never be reached */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1104 PyErr_SetString(PyExc_SystemError,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1105 "logic error in count_comp_fors");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1106 return -1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1107 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1108
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1109 /* Count the number of 'if' statements in a comprehension.
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1110
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1111 Helper for ast_for_comprehension().
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1112 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1113
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1114 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1115 count_comp_ifs(const node *n)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1116 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1117 int n_ifs = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1118
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1119 while (1) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1120 REQ(n, comp_iter);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1121 if (TYPE(CHILD(n, 0)) == comp_for)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1122 return n_ifs;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1123 n = CHILD(n, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1124 REQ(n, comp_if);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1125 n_ifs++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1126 if (NCH(n) == 2)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1127 return n_ifs;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1128 n = CHILD(n, 2);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1129 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1130 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1131
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1132 static expr_ty
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1133 ast_for_comprehension(struct compiling *c, const node *n, int type)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1134 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1135 /* testlist_comp: test ( comp_for | (',' test)* [','] )
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1136 argument: [test '='] test [comp_for] # Really [keyword '='] test */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1137 expr_ty elt;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1138 asdl_seq *comps;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1139 int i, n_fors;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1140 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1141
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1142 assert(NCH(n) > 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1143
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1144 elt = ast_for_expr(c, CHILD(n, 0));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1145 if (!elt)
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
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1148 n_fors = count_comp_fors(n);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1149 if (n_fors == -1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1150 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
1151
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1152 comps = asdl_seq_new(n_fors, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1153 if (!comps)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1154 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
1155
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1156 ch = CHILD(n, 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1157 for (i = 0; i < n_fors; i++) {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1158 comprehension_ty comp;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1159 asdl_seq *t;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1160 expr_ty expression;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1161 node *for_ch;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1162
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1163 REQ(ch, comp_for);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1164
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1165 for_ch = CHILD(ch, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1166 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
1167 if (!t)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1168 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
1169 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
1170 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1171 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
1172
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1173 /* 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
1174 (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
1175 if (NCH(for_ch) == 1)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1176 comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1177 NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1178 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1179 comp = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1180 c->c_arena),
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1181 expression, NULL, 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
1182
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1183 if (!comp)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1184 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
1185
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1186 if (NCH(ch) == 5) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1187 int j, n_ifs;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1188 asdl_seq *ifs;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1189
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1190 ch = CHILD(ch, 4);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1191 n_ifs = count_comp_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
1192 if (n_ifs == -1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1193 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
1194
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
1195 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
1196 if (!ifs)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1197 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
1198
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1199 for (j = 0; j < n_ifs; j++) {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1200 REQ(ch, comp_iter);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1201 ch = CHILD(ch, 0);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1202 REQ(ch, comp_if);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1203
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1204 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
1205 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
1206 return NULL;
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
1207 asdl_seq_SET(ifs, j, expression);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1208 if (NCH(ch) == 3)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1209 ch = CHILD(ch, 2);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1210 }
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1211 /* on exit, must guarantee that ch is a comp_for */
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1212 if (TYPE(ch) == comp_iter)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1213 ch = CHILD(ch, 0);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1214 comp->ifs = ifs;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1215 }
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1216 asdl_seq_SET(comps, i, comp);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1217 }
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1218
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1219 if (type == COMP_GENEXP)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1220 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1221 else if (type == COMP_LISTCOMP)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1222 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1223 else if (type == COMP_SETCOMP)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1224 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1225 else
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1226 /* Should never happen */
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1227 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1228 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1229
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1230 static expr_ty
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1231 ast_for_genexp(struct compiling *c, const node *n)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1232 {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1233 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1234 return ast_for_comprehension(c, n, COMP_GENEXP);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1235 }
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1236
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1237 static expr_ty
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1238 ast_for_listcomp(struct compiling *c, const node *n)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1239 {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1240 assert(TYPE(n) == (testlist_comp));
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1241 return ast_for_comprehension(c, n, COMP_LISTCOMP);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1242 }
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1243
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1244 static expr_ty
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1245 ast_for_setcomp(struct compiling *c, const node *n)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1246 {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1247 assert(TYPE(n) == (dictorsetmaker));
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1248 return ast_for_comprehension(c, n, COMP_SETCOMP);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1249 }
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1250
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1251
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1252 static expr_ty
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1253 ast_for_atom(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1254 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1255 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1256 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1257 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1258 node *ch = CHILD(n, 0);
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1259 int bytesmode = 0;
35742
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 switch (TYPE(ch)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1262 case NAME:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1263 /* 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
1264 changed. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1265 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
1266 case STRING: {
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1267 PyObject *str = parsestrplus(c, n, &bytesmode);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1268 if (!str)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1269 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
1270
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1271 PyArena_AddPyObject(c->c_arena, str);
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1272 if (bytesmode)
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1273 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1274 else
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
1275 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
1276 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1277 case NUMBER: {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1278 PyObject *pynum = parsenumber(STR(ch));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1279 if (!pynum)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1280 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
1281
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1282 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
1283 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
1284 }
41230
b1a18c7ed5ca Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents: 41226
diff changeset
1285 case ELLIPSIS: /* Ellipsis */
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1286 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
1287 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
1288 ch = CHILD(n, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1289
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1290 if (TYPE(ch) == RPAR)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1291 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
1292
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1293 if (TYPE(ch) == yield_expr)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1294 return ast_for_expr(c, ch);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1295
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1296 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1297 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1298 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
1299
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1300 return ast_for_testlist(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1301 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
1302 ch = CHILD(n, 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1303
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1304 if (TYPE(ch) == RSQB)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1305 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
1306
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1307 REQ(ch, testlist_comp);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1308 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
1309 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
1310 if (!elts)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1311 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
1312
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1313 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
1314 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1315 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1316 return ast_for_listcomp(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1317 case LBRACE: {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1318 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1319 * test (gen_for | (',' test)* [',']) */
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1320 int i, size;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1321 asdl_seq *keys, *values;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1322
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1323 ch = CHILD(n, 1);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1324 if (TYPE(ch) == RBRACE) {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1325 /* it's an empty dict */
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1326 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1327 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1328 /* it's a simple set */
41482
a5e410639e24 Make it compile with C89.
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
1329 asdl_seq *elts;
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1330 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
41482
a5e410639e24 Make it compile with C89.
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
1331 elts = asdl_seq_new(size, c->c_arena);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1332 if (!elts)
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1333 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1334 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
1335 expr_ty expression;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1336 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
1337 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1338 return NULL;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1339 asdl_seq_SET(elts, i / 2, expression);
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1340 }
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1341 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1342 } else if (TYPE(CHILD(ch, 1)) == comp_for) {
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1343 /* it's a set comprehension */
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1344 return ast_for_setcomp(c, ch);
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1345 } else {
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1346 /* it's a dict */
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1347 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
1348 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
1349 if (!keys)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1350 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1351
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1352 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
1353 if (!values)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1354 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1355
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1356 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
1357 expr_ty expression;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1358
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1359 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
1360 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1361 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
1362
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1363 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
1364
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1365 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
1366 if (!expression)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1367 return NULL;
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1368
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1369 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
1370 }
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
1371 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
1372 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1373 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1374 default:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1375 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
1376 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1377 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1378 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1379
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1380 static slice_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1381 ast_for_slice(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1382 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1383 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1384 expr_ty lower = NULL, upper = NULL, step = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1385
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1386 REQ(n, subscript);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1387
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1388 /*
39783
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
1389 subscript: test | [test] ':' [test] [sliceop]
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1390 sliceop: ':' [test]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1391 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1392 ch = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1393 if (NCH(n) == 1 && TYPE(ch) == test) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1394 /* '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
1395 other vars */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1396 step = ast_for_expr(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1397 if (!step)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1398 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1399
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1400 return Index(step, c->c_arena);
35742
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
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1403 if (TYPE(ch) == test) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1404 lower = ast_for_expr(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1405 if (!lower)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1406 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1407 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1408
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1409 /* 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
1410 if (TYPE(ch) == COLON) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1411 if (NCH(n) > 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1412 node *n2 = CHILD(n, 1);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1413
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1414 if (TYPE(n2) == test) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1415 upper = ast_for_expr(c, n2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1416 if (!upper)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1417 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1418 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1419 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1420 } 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
1421 node *n2 = CHILD(n, 2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1422
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1423 if (TYPE(n2) == test) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1424 upper = ast_for_expr(c, n2);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1425 if (!upper)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1426 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1427 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1428 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1429
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1430 ch = CHILD(n, NCH(n) - 1);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1431 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
1432 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
1433 /* 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
1434 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
1435 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
1436 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
1437 if (!step)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1438 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
1439 } 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
1440 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
1441 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
1442 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
1443 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
1444 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
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 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1448
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
1449 return Slice(lower, upper, step, c->c_arena);
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 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1453 ast_for_binop(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1454 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1455 /* Must account for a sequence of expressions.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1456 How should A op B op C by represented?
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1457 BinOp(BinOp(A, op, B), op, C).
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1458 */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1459
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1460 int i, nops;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1461 expr_ty expr1, expr2, result;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1462 operator_ty newoperator;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1463
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1464 expr1 = ast_for_expr(c, CHILD(n, 0));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1465 if (!expr1)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1466 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1467
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1468 expr2 = ast_for_expr(c, CHILD(n, 2));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1469 if (!expr2)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1470 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1471
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1472 newoperator = get_operator(CHILD(n, 1));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1473 if (!newoperator)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1474 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1475
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1476 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1477 c->c_arena);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1478 if (!result)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1479 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1480
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1481 nops = (NCH(n) - 1) / 2;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1482 for (i = 1; i < nops; i++) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1483 expr_ty tmp_result, tmp;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1484 const node* next_oper = CHILD(n, i * 2 + 1);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1485
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1486 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
1487 if (!newoperator)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1488 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1489
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1490 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1491 if (!tmp)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1492 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1493
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1494 tmp_result = BinOp(result, newoperator, tmp,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1495 LINENO(next_oper), next_oper->n_col_offset,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1496 c->c_arena);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1497 if (!tmp)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1498 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1499 result = tmp_result;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1500 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
1501 return result;
35742
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
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1504 static expr_ty
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1505 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
1506 {
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1507 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1508 subscriptlist: subscript (',' subscript)* [',']
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1509 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1510 */
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1511 REQ(n, trailer);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1512 if (TYPE(CHILD(n, 0)) == LPAR) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1513 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
1514 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
1515 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
1516 else
36679
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1517 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
1518 }
36679
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1519 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
1520 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
1521 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
1522 }
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1523 else {
e1391910b026 Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36676
diff changeset
1524 REQ(CHILD(n, 0), LSQB);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1525 REQ(CHILD(n, 2), RSQB);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1526 n = CHILD(n, 1);
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1527 if (NCH(n) == 1) {
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1528 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
1529 if (!slc)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1530 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
1531 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
1532 c->c_arena);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1533 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1534 else {
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1535 /* The grammar is ambiguous here. The ambiguity is resolved
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1536 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
1537 no slice features.
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1538 */
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1539 int j;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1540 slice_ty slc;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1541 expr_ty e;
40941
2ca14d6a369b Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40894
diff changeset
1542 int simple = 1;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1543 asdl_seq *slices, *elts;
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1544 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
1545 if (!slices)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1546 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1547 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
1548 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
1549 if (!slc)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1550 return NULL;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1551 if (slc->kind != Index_kind)
40941
2ca14d6a369b Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40894
diff changeset
1552 simple = 0;
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1553 asdl_seq_SET(slices, j / 2, slc);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1554 }
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1555 if (!simple) {
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1556 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
1557 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
1558 }
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1559 /* extract Index values and put them in a Tuple */
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1560 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
1561 if (!elts)
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36786
diff changeset
1562 return NULL;
36686
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1563 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1564 slc = (slice_ty)asdl_seq_GET(slices, j);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1565 assert(slc->kind == Index_kind && slc->v.Index.value);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1566 asdl_seq_SET(elts, j, slc->v.Index.value);
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1567 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1568 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
1569 if (!e)
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1570 return NULL;
8611f39cd82b Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36682
diff changeset
1571 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
1572 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
1573 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1574 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1575 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1576
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1577 static expr_ty
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1578 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
1579 {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1580 node *pfactor, *ppower, *patom, *pnum;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1581 expr_ty expression;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1582
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1583 /* 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
1584 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
1585 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
1586 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
1587 (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
1588 PyLongObject.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1589 */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1590 if (TYPE(CHILD(n, 0)) == MINUS
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1591 && NCH(n) == 2
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1592 && TYPE((pfactor = CHILD(n, 1))) == factor
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1593 && NCH(pfactor) == 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1594 && TYPE((ppower = CHILD(pfactor, 0))) == power
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1595 && NCH(ppower) == 1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1596 && TYPE((patom = CHILD(ppower, 0))) == atom
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1597 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1598 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
1599 if (s == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1600 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1601 s[0] = '-';
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1602 strcpy(s + 1, STR(pnum));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1603 PyObject_FREE(STR(pnum));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1604 STR(pnum) = s;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1605 return ast_for_atom(c, patom);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1606 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1607
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1608 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
1609 if (!expression)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1610 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1611
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1612 switch (TYPE(CHILD(n, 0))) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1613 case PLUS:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1614 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
1615 c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1616 case MINUS:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1617 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
1618 c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1619 case TILDE:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1620 return UnaryOp(Invert, expression, LINENO(n),
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1621 n->n_col_offset, c->c_arena);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1622 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1623 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1624 TYPE(CHILD(n, 0)));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1625 return NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1626 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1627
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1628 static expr_ty
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1629 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
1630 {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1631 /* power: atom trailer* ('**' factor)*
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1632 */
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1633 int i;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1634 expr_ty e, tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1635 REQ(n, power);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1636 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
1637 if (!e)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1638 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1639 if (NCH(n) == 1)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1640 return e;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1641 for (i = 1; i < NCH(n); i++) {
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1642 node *ch = CHILD(n, i);
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1643 if (TYPE(ch) != trailer)
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1644 break;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1645 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
1646 if (!tmp)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1647 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1648 tmp->lineno = e->lineno;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1649 tmp->col_offset = e->col_offset;
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1650 e = tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1651 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1652 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
1653 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
1654 if (!f)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1655 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
1656 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
1657 if (!tmp)
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1658 return NULL;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1659 e = tmp;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1660 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1661 return e;
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1662 }
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1663
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1664 /* 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
1665 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1666
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1667 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1668 ast_for_expr(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1669 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1670 /* 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
1671 test: or_test ['if' or_test 'else' test] | lambdef
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1672 test_nocond: or_test | lambdef_nocond
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1673 or_test: and_test ('or' and_test)*
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1674 and_test: not_test ('and' not_test)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1675 not_test: 'not' not_test | comparison
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1676 comparison: expr (comp_op expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1677 expr: xor_expr ('|' xor_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1678 xor_expr: and_expr ('^' and_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1679 and_expr: shift_expr ('&' shift_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1680 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1681 arith_expr: term (('+'|'-') term)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1682 term: factor (('*'|'/'|'%'|'//') factor)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1683 factor: ('+'|'-'|'~') factor | power
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1684 power: atom trailer* ('**' factor)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1685 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1686
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1687 asdl_seq *seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1688 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1689
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1690 loop:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1691 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1692 case test:
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1693 case test_nocond:
36666
6165927431d6 PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents: 36444
diff changeset
1694 if (TYPE(CHILD(n, 0)) == lambdef ||
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1695 TYPE(CHILD(n, 0)) == lambdef_nocond)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1696 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
1697 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
1698 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
1699 /* Fallthrough */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1700 case or_test:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1701 case and_test:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1702 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1703 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1704 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1705 }
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
1706 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
1707 if (!seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1708 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1709 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1710 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
1711 if (!e)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1712 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1713 asdl_seq_SET(seq, i / 2, e);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1714 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1715 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
1716 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
1717 c->c_arena);
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1718 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
1719 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
1720 case not_test:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1721 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1722 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1723 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1724 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1725 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1726 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
1727 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1728 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1729
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
1730 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
1731 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1732 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1733 case comparison:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1734 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1735 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1736 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1737 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1738 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1739 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
1740 asdl_int_seq *ops;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1741 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
1742 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
1743 if (!ops)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1744 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
1745 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
1746 if (!cmps) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1747 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1748 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1749 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
1750 cmpop_ty newoperator;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1751
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
1752 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
1753 if (!newoperator) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1754 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1755 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1756
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1757 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
1758 if (!expression) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1759 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1760 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1761
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
1762 asdl_seq_SET(ops, i / 2, newoperator);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1763 asdl_seq_SET(cmps, i / 2, expression);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1764 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1765 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
1766 if (!expression) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1767 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1768 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1769
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
1770 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
1771 n->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1772 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1773 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1774
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1775 /* 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
1776 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
1777 reuse the code for each type of operator.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1778 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1779 case expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1780 case xor_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1781 case and_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1782 case shift_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1783 case arith_expr:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1784 case term:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1785 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1786 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1787 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1788 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1789 return ast_for_binop(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1790 case yield_expr: {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1791 expr_ty exp = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1792 if (NCH(n) == 2) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1793 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
1794 if (!exp)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1795 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1796 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1797 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
1798 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1799 case factor:
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1800 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1801 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1802 goto loop;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1803 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1804 return ast_for_factor(c, n);
35799
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1805 case power:
842caa245917 Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35798
diff changeset
1806 return ast_for_power(c, n);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1807 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
1808 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
1809 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1810 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1811 /* 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
1812 return NULL;
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
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1815 static expr_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1816 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
1817 {
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 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1820 | '**' test)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1821 argument: [test '='] test [comp_for] # Really [keyword '='] test
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1822 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1823
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1824 int i, nargs, nkeywords, ngens;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1825 asdl_seq *args;
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
1826 asdl_seq *keywords;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1827 expr_ty vararg = NULL, kwarg = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1828
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1829 REQ(n, arglist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1830
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1831 nargs = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1832 nkeywords = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1833 ngens = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1834 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
1835 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1836 if (TYPE(ch) == argument) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1837 if (NCH(ch) == 1)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1838 nargs++;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1839 else if (TYPE(CHILD(ch, 1)) == comp_for)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1840 ngens++;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1841 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1842 nkeywords++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1843 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1844 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1845 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1846 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
1847 "if not sole argument");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1848 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1849 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1850
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1851 if (nargs + nkeywords + ngens > 255) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1852 ast_error(n, "more than 255 arguments");
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1853 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1854 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1855
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
1856 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
1857 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
1858 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
1859 keywords = asdl_seq_new(nkeywords, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1860 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
1861 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1862 nargs = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1863 nkeywords = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1864 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
1865 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1866 if (TYPE(ch) == argument) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1867 expr_ty e;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1868 if (NCH(ch) == 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1869 if (nkeywords) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1870 ast_error(CHILD(ch, 0),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1871 "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
1872 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1873 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1874 e = ast_for_expr(c, CHILD(ch, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1875 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
1876 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1877 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
1878 }
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1879 else if (TYPE(CHILD(ch, 1)) == comp_for) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1880 e = ast_for_genexp(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1881 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
1882 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1883 asdl_seq_SET(args, nargs++, e);
35742
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 else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1886 keyword_ty kw;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1887 identifier key;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1888
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1889 /* 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
1890 e = ast_for_expr(c, CHILD(ch, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1891 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
1892 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1893 /* 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
1894 * 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
1895 * 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
1896 * then is very confusing.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1897 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1898 if (e->kind == Lambda_kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1899 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
1900 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1901 } else if (e->kind != Name_kind) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1902 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
1903 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1904 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1905 key = e->v.Name.id;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1906 e = ast_for_expr(c, CHILD(ch, 2));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1907 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
1908 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1909 kw = keyword(key, e, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1910 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
1911 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1912 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
1913 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1914 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1915 else if (TYPE(ch) == STAR) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1916 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
1917 i++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1918 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1919 else if (TYPE(ch) == DOUBLESTAR) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1920 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
1921 i++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1922 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1923 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1924
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
1925 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
1926 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1927
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1928 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
1929 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
1930 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1931 /* testlist_comp: test (comp_for | (',' test)* [',']) */
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1932 /* 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
1933 /* testlist1: test (',' test)* */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1934 assert(NCH(n) > 0);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1935 if (TYPE(n) == testlist_comp) {
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1936 if (NCH(n) > 1)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 41235
diff changeset
1937 assert(TYPE(CHILD(n, 1)) != comp_for);
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1938 }
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1939 else {
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1940 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
1941 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
1942 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1943 if (NCH(n) == 1)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1944 return ast_for_expr(c, CHILD(n, 0));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1945 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1946 asdl_seq *tmp = seq_for_testlist(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1947 if (!tmp)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1948 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1949 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
1950 }
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1951 }
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
1952
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1953 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1954 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
1955 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1956 REQ(n, expr_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1957 /* expr_stmt: testlist (augassign (yield_expr|testlist)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1958 | ('=' (yield_expr|testlist))*)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1959 testlist: test (',' test)* [',']
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1960 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1961 | '<<=' | '>>=' | '**=' | '//='
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1962 test: ... here starts the operator precendence dance
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1963 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1964
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1965 if (NCH(n) == 1) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1966 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
1967 if (!e)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1968 return NULL;
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 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
1971 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1972 else if (TYPE(CHILD(n, 1)) == augassign) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1973 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
1974 operator_ty newoperator;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1975 node *ch = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1976
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1977 expr1 = ast_for_testlist(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1978 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1979 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1980 /* 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
1981 switch (expr1->kind) {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1982 case GeneratorExp_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1983 ast_error(ch, "augmented assignment to generator "
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1984 "expression not possible");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1985 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1986 case Yield_kind:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1987 ast_error(ch, "augmented assignment to yield "
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1988 "expression not possible");
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1989 return NULL;
36418
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1990 case Name_kind: {
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1991 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
1992 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
1993 ast_error(ch, "assignment to None");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1994 return NULL;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1995 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1996 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1997 }
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1998 case Attribute_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
1999 case Subscript_kind:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2000 break;
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2001 default:
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2002 ast_error(ch, "illegal expression for augmented "
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2003 "assignment");
a625e8876407 Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36242
diff changeset
2004 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2005 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2006 set_context(expr1, Store, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2007
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2008 ch = CHILD(n, 2);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2009 if (TYPE(ch) == testlist)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2010 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
2011 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2012 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
2013 if (!expr2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2014 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2015
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
2016 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
2017 if (!newoperator)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2018 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2019
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2020 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
2021 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2022 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2023 int i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2024 asdl_seq *targets;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2025 node *value;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2026 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2027
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2028 /* a normal assignment */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2029 REQ(CHILD(n, 1), EQUAL);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2030 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
2031 if (!targets)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2032 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2033 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
2034 expr_ty e;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2035 node *ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2036 if (TYPE(ch) == yield_expr) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2037 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
2038 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2039 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2040 e = ast_for_testlist(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2041
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2042 /* set context to assign */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2043 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2044 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2045
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2046 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
2047 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2048
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2049 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
2050 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2051 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
2052 if (TYPE(value) == testlist)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2053 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
2054 else
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2055 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
2056 if (!expression)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2057 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2058 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
2059 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2060 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2061
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2062 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
2063 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
2064 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2065 asdl_seq *seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2066 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2067 expr_ty e;
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 REQ(n, exprlist);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2070
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
2071 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
2072 if (!seq)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2073 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2074 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
2075 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
2076 if (!e)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2077 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2078 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
2079 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
2080 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2081 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2082 return seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2083 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2084
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2085 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2086 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
2087 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2088 asdl_seq *expr_list;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2089
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2090 /* del_stmt: 'del' exprlist */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2091 REQ(n, del_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2092
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2093 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
2094 if (!expr_list)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2095 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
2096 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
2097 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2098
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2099 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2100 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
2101 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2102 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2103 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
2104 | yield_stmt
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2105 break_stmt: 'break'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2106 continue_stmt: 'continue'
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2107 return_stmt: 'return' [testlist]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2108 yield_stmt: yield_expr
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2109 yield_expr: 'yield' testlist
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2110 raise_stmt: 'raise' [test [',' test [',' test]]]
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2111 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2112 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2113
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2114 REQ(n, flow_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2115 ch = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2116 switch (TYPE(ch)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2117 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
2118 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
2119 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
2120 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
2121 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
2122 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
2123 if (!exp)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2124 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
2125 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
2126 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2127 case return_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2128 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
2129 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
2130 else {
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2131 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
2132 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2133 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
2134 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
2135 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2136 case raise_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2137 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
2138 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
2139 else if (NCH(ch) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2140 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
2141 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2142 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
2143 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
2144 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2145 else if (NCH(ch) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2146 expr_ty expr1, expr2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2147
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2148 expr1 = ast_for_expr(c, CHILD(ch, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2149 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2150 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2151 expr2 = ast_for_expr(c, CHILD(ch, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2152 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2153 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2154
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2155 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
2156 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2157 else if (NCH(ch) == 6) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2158 expr_ty expr1, expr2, expr3;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2159
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2160 expr1 = ast_for_expr(c, CHILD(ch, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2161 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2162 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2163 expr2 = ast_for_expr(c, CHILD(ch, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2164 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2165 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2166 expr3 = ast_for_expr(c, CHILD(ch, 5));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2167 if (!expr3)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2168 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2169
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2170 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
2171 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2172 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2173 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2174 "unexpected flow_stmt: %d", TYPE(ch));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2175 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2176 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2177
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2178 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
2179 return NULL;
35742
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
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2182 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
2183 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
2184 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2185 /*
36742
658f8ff2a442 Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents: 36718
diff changeset
2186 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
2187 dotted_as_name: dotted_name ['as' NAME]
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2188 dotted_name: NAME ('.' NAME)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2189 */
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
2190 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
2191
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2192 loop:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2193 switch (TYPE(n)) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2194 case import_as_name:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2195 str = NULL;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2196 if (NCH(n) == 3) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2197 str = NEW_IDENTIFIER(CHILD(n, 2));
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2198 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2199 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
2200 case dotted_as_name:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2201 if (NCH(n) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2202 n = CHILD(n, 0);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2203 goto loop;
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 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
2206 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
2207 if (!a)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
2208 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2209 assert(!a->asname);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2210 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2211 return a;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2212 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2213 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2214 case dotted_name:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2215 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
2216 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
2217 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2218 /* 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
2219 int i;
36241
557e9b6ee7e1 alias_for_import_name(): Dueling compiler warnings ;-)
Tim Peters <tim.peters@gmail.com>
parents: 36231
diff changeset
2220 size_t len;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2221 char *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2222
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2223 len = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2224 for (i = 0; i < NCH(n); i += 2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2225 /* length of string plus one for the dot */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2226 len += strlen(STR(CHILD(n, i))) + 1;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2227 len--; /* the last name doesn't have a dot */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2228 str = PyString_FromStringAndSize(NULL, len);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2229 if (!str)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2230 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2231 s = PyString_AS_STRING(str);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2232 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2233 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2234 for (i = 0; i < NCH(n); i += 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2235 char *sch = STR(CHILD(n, i));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2236 strcpy(s, STR(CHILD(n, i)));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2237 s += strlen(sch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2238 *s++ = '.';
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 --s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2241 *s = '\0';
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2242 PyString_InternInPlace(&str);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2243 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
2244 return alias(str, NULL, c->c_arena);
35742
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 break;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2247 case STAR:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2248 str = PyString_InternFromString("*");
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2249 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
2250 return alias(str, NULL, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2251 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2252 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2253 "unexpected import name: %d", TYPE(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2254 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2255 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2256
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2257 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2258 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2259 }
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 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2262 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
2263 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2264 /*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2265 import_stmt: import_name | import_from
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2266 import_name: 'import' dotted_as_names
41235
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2267 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2268 'import' ('*' | '(' import_as_names ')' | import_as_names)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2269 */
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2270 int lineno;
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2271 int col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2272 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2273 asdl_seq *aliases;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2274
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2275 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
2276 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
2277 col_offset = n->n_col_offset;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2278 n = CHILD(n, 0);
36682
52cbc447e1c9 And some more cleanup.
Thomas Wouters <thomas@python.org>
parents: 36681
diff changeset
2279 if (TYPE(n) == import_name) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2280 n = CHILD(n, 1);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2281 REQ(n, dotted_as_names);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2282 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
2283 if (!aliases)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2284 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2285 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
2286 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
2287 if (!import_alias)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2288 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2289 asdl_seq_SET(aliases, i / 2, import_alias);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2290 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2291 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
2292 }
36682
52cbc447e1c9 And some more cleanup.
Thomas Wouters <thomas@python.org>
parents: 36681
diff changeset
2293 else if (TYPE(n) == import_from) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2294 int n_children;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2295 int idx, ndots = 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2296 alias_ty mod = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2297 identifier modname;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2298
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2299 /* 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
2300 optional module name */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2301 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
2302 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
2303 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
2304 idx++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2305 break;
41235
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2306 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2307 /* three consecutive dots are tokenized as one ELLIPSIS */
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2308 ndots += 3;
6c8c33a43a15 "from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents: 41230
diff changeset
2309 continue;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2310 } 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
2311 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2312 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2313 ndots++;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2314 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2315 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
2316 switch (TYPE(CHILD(n, idx))) {
36681
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2317 case STAR:
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2318 /* from ... import * */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2319 n = CHILD(n, idx);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2320 n_children = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2321 if (ndots) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2322 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
2323 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2324 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2325 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2326 case LPAR:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2327 /* from ... import (x, y, z) */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2328 n = CHILD(n, idx + 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2329 n_children = NCH(n);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2330 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2331 case import_as_names:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2332 /* from ... import x, y, z */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2333 n = CHILD(n, idx);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2334 n_children = NCH(n);
36681
e9f0db429f2b Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents: 36679
diff changeset
2335 if (n_children % 2 == 0) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2336 ast_error(n, "trailing comma not allowed without"
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2337 " surrounding parentheses");
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2338 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2339 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2340 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2341 default:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2342 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
2343 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2344 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2345
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2346 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
2347 if (!aliases)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2348 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2349
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2350 /* 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
2351 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
2352 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
2353 if (!import_alias)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2354 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2355 asdl_seq_SET(aliases, 0, import_alias);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2356 }
36718
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2357 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2358 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
2359 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
2360 if (!import_alias)
45e6f901902c Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 36712
diff changeset
2361 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2362 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
2363 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2364 }
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2365 if (mod != NULL)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2366 modname = mod->name;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2367 else
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2368 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
2369 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
2370 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2371 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2372 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2373 "unknown import statement: starts with command '%s'",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2374 STR(CHILD(n, 0)));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2375 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2376 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2377
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2378 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2379 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
2380 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2381 /* global_stmt: 'global' NAME (',' NAME)* */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2382 identifier name;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2383 asdl_seq *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2384 int i;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2385
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2386 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
2387 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
2388 if (!s)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2389 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2390 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
2391 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
2392 if (!name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2393 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2394 asdl_seq_SET(s, i / 2, name);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2395 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2396 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
2397 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2398
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2399 static stmt_ty
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2400 ast_for_nonlocal_stmt(struct compiling *c, const node *n)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2401 {
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2402 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2403 identifier name;
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2404 asdl_seq *s;
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2405 int i;
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2406
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2407 REQ(n, nonlocal_stmt);
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2408 s = asdl_seq_new(NCH(n) / 2, c->c_arena);
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2409 if (!s)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2410 return NULL;
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2411 for (i = 1; i < NCH(n); i += 2) {
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2412 name = NEW_IDENTIFIER(CHILD(n, i));
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2413 if (!name)
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2414 return NULL;
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2415 asdl_seq_SET(s, i / 2, name);
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2416 }
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2417 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2418 }
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2419
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2420 static stmt_ty
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2421 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
2422 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2423 /* assert_stmt: 'assert' test [',' test] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2424 REQ(n, assert_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2425 if (NCH(n) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2426 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
2427 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2428 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2429 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
2430 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2431 else if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2432 expr_ty expr1, expr2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2433
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2434 expr1 = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2435 if (!expr1)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2436 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2437 expr2 = ast_for_expr(c, CHILD(n, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2438 if (!expr2)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2439 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2440
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2441 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
2442 }
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2443 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2444 "improper number of parts to 'assert' statement: %d",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2445 NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2446 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2447 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2448
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2449 static asdl_seq *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2450 ast_for_suite(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2451 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2452 /* 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
2453 asdl_seq *seq;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2454 stmt_ty s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2455 int i, total, num, end, pos = 0;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2456 node *ch;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2457
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2458 REQ(n, suite);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2459
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2460 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
2461 seq = asdl_seq_new(total, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2462 if (!seq)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2463 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2464 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
2465 n = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2466 /* 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
2467 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
2468 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2469 end = NCH(n) - 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2470 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
2471 end--;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2472 /* 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
2473 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
2474 ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2475 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
2476 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2477 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2478 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
2479 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2480 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2481 else {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2482 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
2483 ch = CHILD(n, i);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2484 REQ(ch, stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2485 num = num_stmts(ch);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2486 if (num == 1) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2487 /* 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
2488 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
2489 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2490 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2491 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
2492 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2493 else {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2494 int j;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2495 ch = CHILD(ch, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2496 REQ(ch, simple_stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2497 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
2498 /* 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
2499 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
2500 assert((j + 1) == NCH(ch));
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2501 break;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2502 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2503 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
2504 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2505 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2506 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
2507 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2508 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2509 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2510 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2511 assert(pos == seq->size);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2512 return seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2513 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2514
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2515 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2516 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
2517 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2518 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2519 ['else' ':' suite]
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 char *s;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2522
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2523 REQ(n, if_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2524
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2525 if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2526 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2527 asdl_seq *suite_seq;
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 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2530 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2531 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2532 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
2533 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2534 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2535
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2536 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2537 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2538 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2539
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2540 s = STR(CHILD(n, 4));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2541 /* 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
2542 's' for el_s_e, or
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2543 'i' for el_i_f
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 if (s[2] == 's') {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2546 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2547 asdl_seq *seq1, *seq2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2548
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2549 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2550 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2551 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2552 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
2553 if (!seq1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2554 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2555 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
2556 if (!seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2557 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2558
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2559 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2560 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2561 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2562 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
2563 int i, n_elif, has_else = 0;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2564 expr_ty expression;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2565 asdl_seq *suite_seq;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2566 asdl_seq *orelse = NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2567 n_elif = NCH(n) - 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2568 /* 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
2569 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
2570 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
2571 && 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
2572 has_else = 1;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2573 n_elif -= 3;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2574 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2575 n_elif /= 4;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2576
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2577 if (has_else) {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2578 asdl_seq *suite_seq2;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2579
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2580 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
2581 if (!orelse)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2582 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2583 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
2584 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2585 return NULL;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2586 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2587 if (!suite_seq)
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2588 return NULL;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2589 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2590 if (!suite_seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2591 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2592
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2593 asdl_seq_SET(orelse, 0,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2594 If(expression, suite_seq, suite_seq2,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2595 LINENO(CHILD(n, NCH(n) - 6)),
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2596 CHILD(n, NCH(n) - 6)->n_col_offset,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2597 c->c_arena));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2598 /* 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
2599 n_elif--;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2600 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2601
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2602 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
2603 int off = 5 + (n_elif - i - 1) * 4;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2604 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
2605 if (!newobj)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2606 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2607 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
2608 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2609 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2610 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
2611 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2612 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2613
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2614 asdl_seq_SET(newobj, 0,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2615 If(expression, suite_seq, orelse,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2616 LINENO(CHILD(n, off)),
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2617 CHILD(n, off)->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
2618 orelse = newobj;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2619 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2620 expression = ast_for_expr(c, CHILD(n, 1));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2621 if (!expression)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2622 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2623 suite_seq = ast_for_suite(c, CHILD(n, 3));
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2624 if (!suite_seq)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2625 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2626 return If(expression, suite_seq, orelse,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
2627 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
2628 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2629
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2630 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2631 "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
2632 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2633 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2634
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2635 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2636 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
2637 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2638 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2639 REQ(n, while_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2640
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2641 if (NCH(n) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2642 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2643 asdl_seq *suite_seq;
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 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2646 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2647 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2648 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
2649 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2650 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2651 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
2652 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2653 else if (NCH(n) == 7) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2654 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2655 asdl_seq *seq1, *seq2;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2656
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2657 expression = ast_for_expr(c, CHILD(n, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2658 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2659 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2660 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
2661 if (!seq1)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2662 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2663 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
2664 if (!seq2)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2665 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2666
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2667 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
2668 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2669
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2670 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2671 "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
2672 NCH(n));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2673 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2674 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2675
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2676 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2677 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
2678 {
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2679 asdl_seq *_target, *seq = NULL, *suite_seq;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2680 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2681 expr_ty target;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2682 const node *node_target;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2683 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2684 REQ(n, for_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2685
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2686 if (NCH(n) == 9) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2687 seq = ast_for_suite(c, CHILD(n, 8));
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2688 if (!seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2689 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2690 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2691
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2692 node_target = CHILD(n, 1);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2693 _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
2694 if (!_target)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2695 return NULL;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2696 /* 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
2697 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
2698 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
2699 target = (expr_ty)asdl_seq_GET(_target, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2700 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2701 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
2702
35798
857f68fba275 Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 35771
diff changeset
2703 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
2704 if (!expression)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2705 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2706 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
2707 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2708 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2709
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
2710 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
2711 c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2712 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2713
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2714 static excepthandler_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2715 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
2716 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2717 /* except_clause: 'except' [test [',' test]] */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2718 REQ(exc, except_clause);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2719 REQ(body, suite);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2720
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2721 if (NCH(exc) == 1) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2722 asdl_seq *suite_seq = ast_for_suite(c, body);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2723 if (!suite_seq)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2724 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2725
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2726 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
2727 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2728 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2729 else if (NCH(exc) == 2) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2730 expr_ty expression;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2731 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2732
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2733 expression = ast_for_expr(c, CHILD(exc, 1));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2734 if (!expression)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2735 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2736 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
2737 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2738 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2739
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2740 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
2741 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2742 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2743 else if (NCH(exc) == 4) {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2744 asdl_seq *suite_seq;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2745 expr_ty expression;
40588
4bab65fcca8e Some more changes related to the new except syntax and semantics,
Guido van Rossum <guido@python.org>
parents: 40529
diff changeset
2746 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2747 if (!e)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2748 return NULL;
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2749 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
2750 if (!expression)
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2751 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2752 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
2753 if (!suite_seq)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2754 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2755
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2756 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
2757 exc->n_col_offset, c->c_arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2758 }
36072
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2759
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2760 PyErr_Format(PyExc_SystemError,
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2761 "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
2762 NCH(exc));
de16a85c3b4c Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36066
diff changeset
2763 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2764 }
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 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2767 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
2768 {
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2769 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
2770 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
2771 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
2772
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2773 REQ(n, try_stmt);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2774
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2775 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
2776 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
2777 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2778
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2779 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
2780 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
2781 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
2782 /* 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
2783 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
2784 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
2785 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
2786 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
2787 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2788 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2789 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2790
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2791 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
2792 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
2793 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2794 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2795 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2796 else {
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2797 /* 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
2798 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
2799 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
2800 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
2801 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2802 n_except--;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2803 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2804 }
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2805 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
2806 ast_error(n, "malformed 'try' statement");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2807 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2808 }
36066
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2809
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2810 if (n_except > 0) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2811 int i;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2812 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
2813 /* 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
2814 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
2815 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
2816 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2817
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2818 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
2819 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
2820 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
2821 if (!e)
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2822 return NULL;
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2823 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
2824 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2825
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2826 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
2827 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
2828 if (!finally)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2829 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
2830
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2831 /* 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
2832 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
2833 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
2834 if (body == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2835 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2836 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
2837 }
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2838
719ba02d2e2c SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
2839 /* 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
2840 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
2841 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
2842 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2843
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2844 static expr_ty
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2845 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
2846 {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2847 REQ(n, with_var);
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2848 return ast_for_expr(c, CHILD(n, 1));
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2849 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2850
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2851 /* with_stmt: 'with' test [ with_var ] ':' suite */
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2852 static stmt_ty
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2853 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
2854 {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2855 expr_ty context_expr, optional_vars = NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2856 int suite_index = 3; /* skip 'with', test, and ':' */
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2857 asdl_seq *suite_seq;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2858
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2859 assert(TYPE(n) == with_stmt);
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2860 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
2861 if (TYPE(CHILD(n, 2)) == with_var) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2862 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
2863
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2864 if (!optional_vars) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2865 return NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2866 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2867 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
2868 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2869 }
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2870 suite_index = 4;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2871 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2872
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2873 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
2874 if (!suite_seq) {
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2875 return NULL;
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2876 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36780
diff changeset
2877 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
2878 n->n_col_offset, c->c_arena);
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2879 }
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2880
35742
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_classdef(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2883 {
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2884 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2885 asdl_seq *s;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2886 expr_ty call, dummy;
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2887
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2888 REQ(n, classdef);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2889
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2890 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
2891 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
2892 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2893 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2894
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2895 if (NCH(n) == 4) { /* class NAME ':' suite */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2896 s = ast_for_suite(c, CHILD(n, 3));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2897 if (!s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2898 return NULL;
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2899 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2900 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
2901 }
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2902
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2903 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2904 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
2905 if (!s)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2906 return NULL;
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2907 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2908 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
2909 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2910
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2911 /* class NAME '(' arglist ')' ':' suite */
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2912 /* build up a fake Call node so we can extract its pieces */
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2913 dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2914 call = ast_for_call(c, CHILD(n, 3), dummy);
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2915 if (!call)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2916 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2917 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
2918 if (!s)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2919 return NULL;
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2920
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2921 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)),
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2922 call->v.Call.args, call->v.Call.keywords,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2923 call->v.Call.starargs, call->v.Call.kwargs, s,
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40965
diff changeset
2924 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
2925 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2926
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2927 static stmt_ty
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2928 ast_for_stmt(struct compiling *c, const node *n)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2929 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2930 if (TYPE(n) == stmt) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2931 assert(NCH(n) == 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2932 n = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2933 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2934 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
2935 assert(num_stmts(n) == 1);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2936 n = CHILD(n, 0);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2937 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2938 if (TYPE(n) == small_stmt) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
2939 REQ(n, small_stmt);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
2940 n = CHILD(n, 0);
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2941 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2942 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2943 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2944 switch (TYPE(n)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2945 case expr_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2946 return ast_for_expr_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2947 case del_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2948 return ast_for_del_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2949 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
2950 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
2951 case flow_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2952 return ast_for_flow_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2953 case import_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2954 return ast_for_import_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2955 case global_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2956 return ast_for_global_stmt(c, n);
40965
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2957 case nonlocal_stmt:
30e5cb3a55ac Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 40941
diff changeset
2958 return ast_for_nonlocal_stmt(c, n);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2959 case assert_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2960 return ast_for_assert_stmt(c, n);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2961 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2962 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2963 "unhandled small_stmt: TYPE=%d NCH=%d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2964 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2965 return NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2966 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2967 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2968 else {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2969 /* 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
2970 | funcdef | classdef
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2971 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2972 node *ch = CHILD(n, 0);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2973 REQ(n, compound_stmt);
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2974 switch (TYPE(ch)) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2975 case if_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2976 return ast_for_if_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2977 case while_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2978 return ast_for_while_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2979 case for_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2980 return ast_for_for_stmt(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2981 case try_stmt:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2982 return ast_for_try_stmt(c, ch);
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2983 case with_stmt:
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36686
diff changeset
2984 return ast_for_with_stmt(c, ch);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2985 case funcdef:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2986 return ast_for_funcdef(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2987 case classdef:
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2988 return ast_for_classdef(c, ch);
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2989 default:
35870
f531c05cd240 Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents: 35868
diff changeset
2990 PyErr_Format(PyExc_SystemError,
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2991 "unhandled small_stmt: TYPE=%d NCH=%d\n",
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2992 TYPE(n), NCH(n));
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2993 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2994 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2995 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2996 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2997
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2998 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2999 parsenumber(const char *s)
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3000 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3001 const char *end;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3002 long x;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3003 double dx;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3004 #ifndef WITHOUT_COMPLEX
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3005 Py_complex c;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3006 int imflag;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3007 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3008
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3009 errno = 0;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3010 end = s + strlen(s) - 1;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3011 #ifndef WITHOUT_COMPLEX
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3012 imflag = *end == 'j' || *end == 'J';
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3013 #endif
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3014 if (*end == 'l' || *end == 'L')
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3015 return PyLong_FromString((char *)s, (char **)0, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3016 if (s[0] == '0') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3017 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3018 if (x < 0 && errno == 0) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3019 return PyLong_FromString((char *)s,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3020 (char **)0,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3021 0);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3022 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3023 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3024 else
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3025 x = PyOS_strtol((char *)s, (char **)&end, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3026 if (*end == '\0') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3027 if (errno != 0)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3028 return PyLong_FromString((char *)s, (char **)0, 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3029 return PyInt_FromLong(x);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3030 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3031 /* XXX Huge floats may silently fail */
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3032 #ifndef WITHOUT_COMPLEX
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3033 if (imflag) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3034 c.real = 0.;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3035 PyFPE_START_PROTECT("atof", return 0)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3036 c.imag = PyOS_ascii_atof(s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3037 PyFPE_END_PROTECT(c)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3038 return PyComplex_FromCComplex(c);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3039 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3040 else
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3041 #endif
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3042 {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3043 PyFPE_START_PROTECT("atof", return 0)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3044 dx = PyOS_ascii_atof(s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3045 PyFPE_END_PROTECT(dx)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3046 return PyFloat_FromDouble(dx);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3047 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3048 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3049
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3050 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3051 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
3052 {
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3053 #ifndef Py_USING_UNICODE
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3054 Py_FatalError("decode_utf8 should not be called in this build.");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3055 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3056 #else
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3057 PyObject *u, *v;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3058 char *s, *t;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3059 t = s = (char *)*sPtr;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3060 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3061 while (s < end && (*s & 0x80)) s++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3062 *sPtr = s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3063 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3064 if (u == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3065 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3066 v = PyUnicode_AsEncodedString(u, encoding, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3067 Py_DECREF(u);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3068 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3069 #endif
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3070 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3071
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3072 static PyObject *
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3073 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
3074 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3075 PyObject *v, *u;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3076 char *buf;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3077 char *p;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3078 const char *end;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3079 if (encoding == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3080 buf = (char *)s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3081 u = NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3082 } else if (strcmp(encoding, "iso-8859-1") == 0) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3083 buf = (char *)s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3084 u = NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3085 } else {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3086 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3087 u = PyString_FromStringAndSize((char *)NULL, len * 4);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3088 if (u == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3089 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3090 p = buf = PyString_AsString(u);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3091 end = s + len;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3092 while (s < end) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3093 if (*s == '\\') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3094 *p++ = *s++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3095 if (*s & 0x80) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3096 strcpy(p, "u005c");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3097 p += 5;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3098 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3099 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3100 if (*s & 0x80) { /* XXX inefficient */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3101 PyObject *w;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3102 char *r;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3103 Py_ssize_t rn, i;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3104 w = decode_utf8(&s, end, "utf-16-be");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3105 if (w == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3106 Py_DECREF(u);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3107 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3108 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3109 r = PyString_AsString(w);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3110 rn = PyString_Size(w);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3111 assert(rn % 2 == 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3112 for (i = 0; i < rn; i += 2) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3113 sprintf(p, "\\u%02x%02x",
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3114 r[i + 0] & 0xFF,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3115 r[i + 1] & 0xFF);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3116 p += 6;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3117 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3118 Py_DECREF(w);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3119 } else {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3120 *p++ = *s++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3121 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3122 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3123 len = p - buf;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3124 s = buf;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3125 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3126 if (rawmode)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3127 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3128 else
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3129 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3130 Py_XDECREF(u);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3131 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3132 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3133
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3134 /* 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
3135 * 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
3136 * 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
3137 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3138 static PyObject *
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
3139 parsestr(const node *n, const char *encoding, int *bytesmode)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3140 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3141 size_t len;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3142 const char *s = STR(n);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3143 int quote = Py_CHARMASK(*s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3144 int rawmode = 0;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3145 int need_encoding;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3146 int unicode = 0;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3147
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3148 if (isalpha(quote) || quote == '_') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3149 if (quote == 'u' || quote == 'U') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3150 quote = *++s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3151 unicode = 1;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3152 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3153 if (quote == 'b' || quote == 'B') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3154 quote = *++s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3155 *bytesmode = 1;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3156 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3157 if (quote == 'r' || quote == 'R') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3158 quote = *++s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3159 rawmode = 1;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3160 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3161 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3162 if (quote != '\'' && quote != '\"') {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3163 PyErr_BadInternalCall();
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3164 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3165 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3166 if (unicode && *bytesmode) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3167 ast_error(n, "string cannot be both bytes and unicode");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3168 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3169 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3170 s++;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3171 len = strlen(s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3172 if (len > INT_MAX) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3173 PyErr_SetString(PyExc_OverflowError,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3174 "string to parse is too long");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3175 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3176 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3177 if (s[--len] != quote) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3178 PyErr_BadInternalCall();
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3179 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3180 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3181 if (len >= 4 && s[0] == quote && s[1] == quote) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3182 s += 2;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3183 len -= 2;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3184 if (s[--len] != quote || s[--len] != quote) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3185 PyErr_BadInternalCall();
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3186 return NULL;
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
3187 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3188 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3189 #ifdef Py_USING_UNICODE
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3190 if (unicode || Py_UnicodeFlag) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3191 return decode_unicode(s, len, rawmode, encoding);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3192 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3193 #endif
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3194 if (*bytesmode) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3195 /* Disallow non-ascii characters (but not escapes) */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3196 const char *c;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3197 for (c = s; *c; c++) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3198 if (Py_CHARMASK(*c) >= 0x80) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3199 ast_error(n, "bytes can only contain ASCII "
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3200 "literal characters.");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3201 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3202 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3203 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3204 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3205 need_encoding = (!*bytesmode && encoding != NULL &&
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3206 strcmp(encoding, "utf-8") != 0 &&
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3207 strcmp(encoding, "iso-8859-1") != 0);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3208 if (rawmode || strchr(s, '\\') == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3209 if (need_encoding) {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3210 #ifndef Py_USING_UNICODE
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3211 /* This should not happen - we never see any other
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3212 encoding. */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3213 Py_FatalError(
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3214 "cannot deal with encodings in this build.");
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3215 #else
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3216 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3217 if (u == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3218 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3219 v = PyUnicode_AsEncodedString(u, encoding, NULL);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3220 Py_DECREF(u);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3221 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3222 #endif
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3223 } else {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3224 return PyString_FromStringAndSize(s, len);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3225 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3226 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3227
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3228 return PyString_DecodeEscape(s, len, NULL, unicode,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3229 need_encoding ? encoding : NULL);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3230 }
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3231
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3232 /* 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
3233 * 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
3234 * pasting the intermediate results together.
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3235 */
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3236 static PyObject *
40894
0ea24f4f2467 Bytes literal.
Thomas Wouters <thomas@python.org>
parents: 40774
diff changeset
3237 parsestrplus(struct compiling *c, const node *n, int *bytesmode)
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3238 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3239 PyObject *v;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3240 int i;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3241 REQ(CHILD(n, 0), STRING);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3242 v = parsestr(CHILD(n, 0), c->c_encoding, bytesmode);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3243 if (v != NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3244 /* String literal concatenation */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3245 for (i = 1; i < NCH(n); i++) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3246 PyObject *s;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3247 int subbm = 0;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3248 s = parsestr(CHILD(n, i), c->c_encoding, &subbm);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3249 if (s == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3250 goto onError;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3251 if (*bytesmode != subbm) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3252 ast_error(n, "cannot mix bytes and nonbytes"
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3253 "literals");
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3254 goto onError;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3255 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3256 if (PyString_Check(v) && PyString_Check(s)) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3257 PyString_ConcatAndDel(&v, s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3258 if (v == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3259 goto onError;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3260 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3261 #ifdef Py_USING_UNICODE
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3262 else {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3263 PyObject *temp = PyUnicode_Concat(v, s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3264 Py_DECREF(s);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3265 Py_DECREF(v);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3266 v = temp;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3267 if (v == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3268 goto onError;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3269 }
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3270 #endif
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
3271 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3272 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3273 return v;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3274
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3275 onError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3276 Py_XDECREF(v);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41509
diff changeset
3277 return NULL;
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3278 }