mirror of
https://github.com/python/cpython.git
synced 2024-12-02 06:14:49 +08:00
8ce8a784bd
svn+ssh://pythondev@svn.python.org/python/trunk ........ r58221 | georg.brandl | 2007-09-20 10:57:59 -0700 (Thu, 20 Sep 2007) | 2 lines Patch #1181: add os.environ.clear() method. ........ r58225 | sean.reifschneider | 2007-09-20 23:33:28 -0700 (Thu, 20 Sep 2007) | 3 lines Issue1704287: "make install" fails unless you do "make" first. Make oldsharedmods and sharedmods in "libinstall". ........ r58232 | guido.van.rossum | 2007-09-22 13:18:03 -0700 (Sat, 22 Sep 2007) | 4 lines Patch # 188 by Philip Jenvey. Make tell() mark CRLF as a newline. With unit test. ........ r58242 | georg.brandl | 2007-09-24 10:55:47 -0700 (Mon, 24 Sep 2007) | 2 lines Fix typo and double word. ........ r58245 | georg.brandl | 2007-09-24 10:59:28 -0700 (Mon, 24 Sep 2007) | 2 lines #1196: document default radix for int(). ........ r58247 | georg.brandl | 2007-09-24 11:08:24 -0700 (Mon, 24 Sep 2007) | 2 lines #1177: accept 2xx responses for https too, not only http. ........ r58249 | andrew.kuchling | 2007-09-24 16:45:51 -0700 (Mon, 24 Sep 2007) | 1 line Remove stray odd character; grammar fix ........ r58250 | andrew.kuchling | 2007-09-24 16:46:28 -0700 (Mon, 24 Sep 2007) | 1 line Typo fix ........ r58251 | andrew.kuchling | 2007-09-24 17:09:42 -0700 (Mon, 24 Sep 2007) | 1 line Add various items ........ r58268 | vinay.sajip | 2007-09-26 22:34:45 -0700 (Wed, 26 Sep 2007) | 1 line Change to flush and close logic to fix #1760556. ........ r58269 | vinay.sajip | 2007-09-26 22:38:51 -0700 (Wed, 26 Sep 2007) | 1 line Change to basicConfig() to fix #1021. ........ r58270 | georg.brandl | 2007-09-26 23:26:58 -0700 (Wed, 26 Sep 2007) | 2 lines #1208: document match object's boolean value. ........ r58271 | vinay.sajip | 2007-09-26 23:56:13 -0700 (Wed, 26 Sep 2007) | 1 line Minor date change. ........ r58272 | vinay.sajip | 2007-09-27 00:35:10 -0700 (Thu, 27 Sep 2007) | 1 line Change to LogRecord.__init__() to fix #1206. Note that archaic use of type(x) == types.DictType is because of keeping 1.5.2 compatibility. While this is much less relevant these days, there probably needs to be a separate commit for removing all archaic constructs at the same time. ........ r58288 | brett.cannon | 2007-09-30 12:45:10 -0700 (Sun, 30 Sep 2007) | 9 lines tuple.__repr__ did not consider a reference loop as it is not possible from Python code; but it is possible from C. object.__str__ had the issue of not expecting a type to doing something within it's tp_str implementation that could trigger an infinite recursion, but it could in C code.. Both found thanks to BaseException and how it handles its repr. Closes issue #1686386. Thanks to Thomas Herve for taking an initial stab at coming up with a solution. ........ r58289 | brett.cannon | 2007-09-30 13:37:19 -0700 (Sun, 30 Sep 2007) | 3 lines Fix error introduced by r58288; if a tuple is length 0 return its repr and don't worry about any self-referring tuples. ........ r58294 | facundo.batista | 2007-10-02 10:01:24 -0700 (Tue, 02 Oct 2007) | 11 lines Made the various is_* operations return booleans. This was discussed with Cawlishaw by mail, and he basically confirmed that to these is_* operations, there's no need to return Decimal(0) and Decimal(1) if the language supports the False and True booleans. Also added a few tests for the these functions in extra.decTest, since they are mostly untested (apart from the doctests). Thanks Mark Dickinson ........ r58295 | facundo.batista | 2007-10-02 11:21:18 -0700 (Tue, 02 Oct 2007) | 4 lines Added a class to store the digits of log(10), so that they can be made available when necessary without recomputing. Thanks Mark Dickinson ........ r58299 | mark.summerfield | 2007-10-03 01:53:21 -0700 (Wed, 03 Oct 2007) | 4 lines Added note in footnote about string comparisons about unicodedata.normalize(). ........ r58304 | raymond.hettinger | 2007-10-03 14:18:11 -0700 (Wed, 03 Oct 2007) | 1 line enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy. ........ r58305 | raymond.hettinger | 2007-10-03 17:20:27 -0700 (Wed, 03 Oct 2007) | 1 line itertools.count() no longer limited to sys.maxint. ........ r58306 | kurt.kaiser | 2007-10-03 18:49:54 -0700 (Wed, 03 Oct 2007) | 3 lines Assume that the user knows when he wants to end the line; don't insert something he didn't select or complete. ........ r58307 | kurt.kaiser | 2007-10-03 19:07:50 -0700 (Wed, 03 Oct 2007) | 2 lines Remove unused theme that was causing a fault in p3k. ........ r58308 | kurt.kaiser | 2007-10-03 19:09:17 -0700 (Wed, 03 Oct 2007) | 2 lines Clean up EditorWindow close. ........ r58309 | kurt.kaiser | 2007-10-03 19:53:07 -0700 (Wed, 03 Oct 2007) | 7 lines textView cleanup. Patch 1718043 Tal Einat. M idlelib/EditorWindow.py M idlelib/aboutDialog.py M idlelib/textView.py M idlelib/NEWS.txt ........ r58310 | kurt.kaiser | 2007-10-03 20:11:12 -0700 (Wed, 03 Oct 2007) | 3 lines configDialog cleanup. Patch 1730217 Tal Einat. ........ r58311 | neal.norwitz | 2007-10-03 23:00:48 -0700 (Wed, 03 Oct 2007) | 4 lines Coverity #151: Remove deadcode. All this code already exists above starting at line 653. ........ r58325 | fred.drake | 2007-10-04 19:46:12 -0700 (Thu, 04 Oct 2007) | 1 line wrap lines to <80 characters before fixing errors ........ r58326 | raymond.hettinger | 2007-10-04 19:47:07 -0700 (Thu, 04 Oct 2007) | 6 lines Add __asdict__() to NamedTuple and refine the docs. Add maxlen support to deque() and fixup docs. Partially fix __reduce__(). The None as a third arg was no longer supported. Still needs work on __reduce__() to handle recursive inputs. ........ r58327 | fred.drake | 2007-10-04 19:48:32 -0700 (Thu, 04 Oct 2007) | 3 lines move descriptions of ac_(in|out)_buffer_size to the right place http://bugs.python.org/issue1053 ........ r58329 | neal.norwitz | 2007-10-04 20:39:17 -0700 (Thu, 04 Oct 2007) | 3 lines dict could be NULL, so we need to XDECREF. Fix a compiler warning about passing a PyTypeObject* instead of PyObject*. ........ r58330 | neal.norwitz | 2007-10-04 20:41:19 -0700 (Thu, 04 Oct 2007) | 2 lines Fix Coverity #158: Check the correct variable. ........ r58332 | neal.norwitz | 2007-10-04 22:01:38 -0700 (Thu, 04 Oct 2007) | 7 lines Fix Coverity #159. This code was broken if save() returned a negative number since i contained a boolean value and then we compared i < 0 which should never be true. Will backport (assuming it's necessary) ........ r58334 | neal.norwitz | 2007-10-04 22:29:17 -0700 (Thu, 04 Oct 2007) | 1 line Add a note about fixing some more warnings found by Coverity. ........ r58338 | raymond.hettinger | 2007-10-05 12:07:31 -0700 (Fri, 05 Oct 2007) | 1 line Restore BEGIN/END THREADS macros which were squashed in the previous checkin ........ r58343 | gregory.p.smith | 2007-10-06 00:48:10 -0700 (Sat, 06 Oct 2007) | 3 lines Stab in the dark attempt to fix the test_bsddb3 failure on sparc and S-390 ubuntu buildbots. ........ r58344 | gregory.p.smith | 2007-10-06 00:51:59 -0700 (Sat, 06 Oct 2007) | 2 lines Allows BerkeleyDB 4.6.x >= 4.6.21 for the bsddb module. ........ r58348 | gregory.p.smith | 2007-10-06 08:47:37 -0700 (Sat, 06 Oct 2007) | 3 lines Use the host the author likely meant in the first place. pop.gmail.com is reliable. gmail.org is someones personal domain. ........ r58351 | neal.norwitz | 2007-10-06 12:16:28 -0700 (Sat, 06 Oct 2007) | 3 lines Ensure that this test will pass even if another test left an unwritable TESTFN. Also use the safe unlink in test_support instead of rolling our own here. ........ r58368 | georg.brandl | 2007-10-08 00:50:24 -0700 (Mon, 08 Oct 2007) | 3 lines #1123: fix the docs for the str.split(None, sep) case. Also expand a few other methods' docs, which had more info in the deprecated string module docs. ........ r58369 | georg.brandl | 2007-10-08 01:06:05 -0700 (Mon, 08 Oct 2007) | 2 lines Update docstring of sched, also remove an unused assignment. ........ r58370 | raymond.hettinger | 2007-10-08 02:14:28 -0700 (Mon, 08 Oct 2007) | 5 lines Add comments to NamedTuple code. Let the field spec be either a string or a non-string sequence (suggested by Martin Blais with use cases). Improve the error message in the case of a SyntaxError (caused by a duplicate field name). ........ r58371 | raymond.hettinger | 2007-10-08 02:56:29 -0700 (Mon, 08 Oct 2007) | 1 line Missed a line in the docs ........ r58372 | raymond.hettinger | 2007-10-08 03:11:51 -0700 (Mon, 08 Oct 2007) | 1 line Better variable names ........ r58376 | georg.brandl | 2007-10-08 07:12:47 -0700 (Mon, 08 Oct 2007) | 3 lines #1199: docs for tp_as_{number,sequence,mapping}, by Amaury Forgeot d'Arc. No need to merge this to py3k! ........ r58380 | raymond.hettinger | 2007-10-08 14:26:58 -0700 (Mon, 08 Oct 2007) | 1 line Eliminate camelcase function name ........ r58381 | andrew.kuchling | 2007-10-08 16:23:03 -0700 (Mon, 08 Oct 2007) | 1 line Eliminate camelcase function name ........ r58382 | raymond.hettinger | 2007-10-08 18:36:23 -0700 (Mon, 08 Oct 2007) | 1 line Make the error messages more specific ........ r58384 | gregory.p.smith | 2007-10-08 23:02:21 -0700 (Mon, 08 Oct 2007) | 10 lines Splits Modules/_bsddb.c up into bsddb.h and _bsddb.c and adds a C API object available as bsddb.db.api. This is based on the patch submitted by Duncan Grisby here: http://sourceforge.net/tracker/index.php?func=detail&aid=1551895&group_id=13900&atid=313900 See this thread for additional info: http://sourceforge.net/mailarchive/forum.php?thread_name=E1GAVDK-0002rk-Iw%40apasphere.com&forum_name=pybsddb-users It also cleans up the code a little by removing some ifdef/endifs for python prior to 2.1 and for unsupported Berkeley DB <= 3.2. ........ r58385 | gregory.p.smith | 2007-10-08 23:50:43 -0700 (Mon, 08 Oct 2007) | 5 lines Fix a double free when positioning a database cursor to a non-existant string key (and probably a few other situations with string keys). This was reported with a patch as pybsddb sourceforge bug 1708868 by jjjhhhlll at gmail. ........ r58386 | gregory.p.smith | 2007-10-09 00:19:11 -0700 (Tue, 09 Oct 2007) | 3 lines Use the highest cPickle protocol in bsddb.dbshelve. This comes from sourceforge pybsddb patch 1551443 by w_barnes. ........ r58394 | gregory.p.smith | 2007-10-09 11:26:02 -0700 (Tue, 09 Oct 2007) | 2 lines remove another sleepycat reference ........ r58396 | kurt.kaiser | 2007-10-09 12:31:30 -0700 (Tue, 09 Oct 2007) | 3 lines Allow interrupt only when executing user code in subprocess Patch 1225 Tal Einat modified from IDLE-Spoon. ........ r58399 | brett.cannon | 2007-10-09 17:07:50 -0700 (Tue, 09 Oct 2007) | 5 lines Remove file-level typedefs that were inconsistently used throughout the file. Just move over to the public API names. Closes issue1238. ........ r58401 | raymond.hettinger | 2007-10-09 17:26:46 -0700 (Tue, 09 Oct 2007) | 1 line Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques. ........ r58403 | kurt.kaiser | 2007-10-09 17:55:40 -0700 (Tue, 09 Oct 2007) | 2 lines Allow cursor color change w/o restart. Patch 1725576 Tal Einat. ........ r58404 | kurt.kaiser | 2007-10-09 18:06:47 -0700 (Tue, 09 Oct 2007) | 2 lines show paste if > 80 columns. Patch 1659326 Tal Einat. ........ r58415 | thomas.heller | 2007-10-11 12:51:32 -0700 (Thu, 11 Oct 2007) | 5 lines On OS X, use os.uname() instead of gestalt.sysv(...) to get the operating system version. This allows to use ctypes when Python was configured with --disable-toolbox-glue. ........ r58419 | neal.norwitz | 2007-10-11 20:01:01 -0700 (Thu, 11 Oct 2007) | 1 line Get rid of warning about not being able to create an existing directory. ........ r58420 | neal.norwitz | 2007-10-11 20:01:30 -0700 (Thu, 11 Oct 2007) | 1 line Get rid of warnings on a bunch of platforms by using a proper prototype. ........ r58421 | neal.norwitz | 2007-10-11 20:01:54 -0700 (Thu, 11 Oct 2007) | 4 lines Get rid of compiler warning about retval being used (returned) without being initialized. (gcc warning and Coverity 202) ........ r58422 | neal.norwitz | 2007-10-11 20:03:23 -0700 (Thu, 11 Oct 2007) | 1 line Fix Coverity 168: Close the file before returning (exiting). ........ r58423 | neal.norwitz | 2007-10-11 20:04:18 -0700 (Thu, 11 Oct 2007) | 4 lines Fix Coverity 180: Don't overallocate. We don't need structs, but pointers. Also fix a memory leak. ........ r58424 | neal.norwitz | 2007-10-11 20:05:19 -0700 (Thu, 11 Oct 2007) | 5 lines Fix Coverity 185-186: If the passed in FILE is NULL, uninitialized memory would be accessed. Will backport. ........ r58425 | neal.norwitz | 2007-10-11 20:52:34 -0700 (Thu, 11 Oct 2007) | 1 line Get this module to compile with bsddb versions prior to 4.3 ........ r58430 | martin.v.loewis | 2007-10-12 01:56:52 -0700 (Fri, 12 Oct 2007) | 3 lines Bug #1216: Restore support for Visual Studio 2002. Will backport to 2.5. ........ r58433 | raymond.hettinger | 2007-10-12 10:53:11 -0700 (Fri, 12 Oct 2007) | 1 line Fix test of count.__repr__() to ignore the 'L' if the count is a long ........ r58434 | gregory.p.smith | 2007-10-12 11:44:06 -0700 (Fri, 12 Oct 2007) | 4 lines Fixes http://bugs.python.org/issue1233 - bsddb.dbshelve.DBShelf.append was useless due to inverted logic. Also adds a test case for RECNO dbs to test_dbshelve. ........ r58445 | georg.brandl | 2007-10-13 06:20:03 -0700 (Sat, 13 Oct 2007) | 2 lines Fix email example. ........ r58450 | gregory.p.smith | 2007-10-13 16:02:05 -0700 (Sat, 13 Oct 2007) | 2 lines Fix an uncollectable reference leak in bsddb.db.DBShelf.append ........ r58453 | neal.norwitz | 2007-10-13 17:18:40 -0700 (Sat, 13 Oct 2007) | 8 lines Let the O/S supply a port if none of the default ports can be used. This should make the tests more robust at the expense of allowing tests to be sloppier by not requiring them to cleanup after themselves. (It will legitamitely help when running two test suites simultaneously or if another process is already using one of the predefined ports.) Also simplifies (slightLy) the exception handling elsewhere. ........ r58459 | neal.norwitz | 2007-10-14 11:30:21 -0700 (Sun, 14 Oct 2007) | 2 lines Don't raise a string exception, they don't work anymore. ........ r58460 | neal.norwitz | 2007-10-14 11:40:37 -0700 (Sun, 14 Oct 2007) | 1 line Use unittest for assertions ........ r58468 | armin.rigo | 2007-10-15 00:48:35 -0700 (Mon, 15 Oct 2007) | 2 lines test_bigbits was not testing what it seemed to. ........ r58471 | guido.van.rossum | 2007-10-15 08:54:11 -0700 (Mon, 15 Oct 2007) | 3 lines Change a PyErr_Print() into a PyErr_Clear(), per discussion in issue 1031213. ........ r58500 | raymond.hettinger | 2007-10-16 12:18:30 -0700 (Tue, 16 Oct 2007) | 1 line Improve error messages ........ r58506 | raymond.hettinger | 2007-10-16 14:28:32 -0700 (Tue, 16 Oct 2007) | 1 line More docs, error messages, and tests ........ r58507 | andrew.kuchling | 2007-10-16 15:58:03 -0700 (Tue, 16 Oct 2007) | 1 line Add items ........ r58508 | brett.cannon | 2007-10-16 16:24:06 -0700 (Tue, 16 Oct 2007) | 3 lines Remove ``:const:`` notation on None in parameter list. Since the markup is not rendered for parameters it just showed up as ``:const:`None` `` in the output. ........ r58509 | brett.cannon | 2007-10-16 16:26:45 -0700 (Tue, 16 Oct 2007) | 3 lines Re-order some functions whose parameters differ between PyObject and const char * so that they are next to each other. ........ r58522 | armin.rigo | 2007-10-17 11:46:37 -0700 (Wed, 17 Oct 2007) | 5 lines Fix the overflow checking of list_repeat. Introduce overflow checking into list_inplace_repeat. Backport candidate, possibly. ........ r58530 | facundo.batista | 2007-10-17 20:16:03 -0700 (Wed, 17 Oct 2007) | 7 lines Issue #1580738. When HTTPConnection reads the whole stream with read(), it closes itself. When the stream is read in several calls to read(n), it should behave in the same way if HTTPConnection knows where the end of the stream is (through self.length). Added a test case for this behaviour. ........ r58531 | facundo.batista | 2007-10-17 20:44:48 -0700 (Wed, 17 Oct 2007) | 3 lines Issue 1289, just a typo. ........ r58532 | gregory.p.smith | 2007-10-18 00:56:54 -0700 (Thu, 18 Oct 2007) | 4 lines cleanup test_dbtables to use mkdtemp. cleanup dbtables to pass txn as a keyword argument whenever possible to avoid bugs and confusion. (dbtables.py line 447 self.db.get using txn as a non-keyword was an actual bug due to this) ........ r58533 | gregory.p.smith | 2007-10-18 01:34:20 -0700 (Thu, 18 Oct 2007) | 4 lines Fix a weird bug in dbtables: if it chose a random rowid string that contained NULL bytes it would cause the database all sorts of problems in the future leading to very strange random failures and corrupt dbtables.bsdTableDb dbs. ........ r58534 | gregory.p.smith | 2007-10-18 09:32:02 -0700 (Thu, 18 Oct 2007) | 3 lines A cleaner fix than the one committed last night. Generate random rowids that do not contain null bytes. ........ r58537 | gregory.p.smith | 2007-10-18 10:17:57 -0700 (Thu, 18 Oct 2007) | 2 lines mention bsddb fixes. ........ r58538 | raymond.hettinger | 2007-10-18 14:13:06 -0700 (Thu, 18 Oct 2007) | 1 line Remove useless warning ........ r58539 | gregory.p.smith | 2007-10-19 00:31:20 -0700 (Fri, 19 Oct 2007) | 2 lines squelch the warning that this test is supposed to trigger. ........ r58542 | georg.brandl | 2007-10-19 05:32:39 -0700 (Fri, 19 Oct 2007) | 2 lines Clarify wording for apply(). ........ r58544 | mark.summerfield | 2007-10-19 05:48:17 -0700 (Fri, 19 Oct 2007) | 3 lines Added a cross-ref to each other. ........ r58545 | georg.brandl | 2007-10-19 10:38:49 -0700 (Fri, 19 Oct 2007) | 2 lines #1284: "S" means "seen", not unread. ........ r58548 | thomas.heller | 2007-10-19 11:11:41 -0700 (Fri, 19 Oct 2007) | 4 lines Fix ctypes on 32-bit systems when Python is configured --with-system-ffi. See also https://bugs.launchpad.net/bugs/72505. Ported from release25-maint branch. ........ r58550 | facundo.batista | 2007-10-19 12:25:57 -0700 (Fri, 19 Oct 2007) | 8 lines The constructor from tuple was way too permissive: it allowed bad coefficient numbers, floats in the sign, and other details that generated directly the wrong number in the best case, or triggered misfunctionality in the alorithms. Test cases added for these issues. Thanks Mark Dickinson. ........ r58559 | georg.brandl | 2007-10-20 06:22:53 -0700 (Sat, 20 Oct 2007) | 2 lines Fix code being interpreted as a target. ........ r58561 | georg.brandl | 2007-10-20 06:36:24 -0700 (Sat, 20 Oct 2007) | 2 lines Document new "cmdoption" directive. ........ r58562 | georg.brandl | 2007-10-20 08:21:22 -0700 (Sat, 20 Oct 2007) | 2 lines Make a path more Unix-standardy. ........ r58564 | georg.brandl | 2007-10-20 10:51:39 -0700 (Sat, 20 Oct 2007) | 2 lines Document new directive "envvar". ........ r58567 | georg.brandl | 2007-10-20 11:08:14 -0700 (Sat, 20 Oct 2007) | 6 lines * Add new toplevel chapter, "Using Python." (how to install, configure and setup python on different platforms -- at least in theory.) * Move the Python on Mac docs in that chapter. * Add a new chapter about the command line invocation, by stargaming. ........ r58568 | georg.brandl | 2007-10-20 11:33:20 -0700 (Sat, 20 Oct 2007) | 2 lines Change title, for now. ........ r58569 | georg.brandl | 2007-10-20 11:39:25 -0700 (Sat, 20 Oct 2007) | 2 lines Add entry to ACKS. ........ r58570 | georg.brandl | 2007-10-20 12:05:45 -0700 (Sat, 20 Oct 2007) | 2 lines Clarify -E docs. ........ r58571 | georg.brandl | 2007-10-20 12:08:36 -0700 (Sat, 20 Oct 2007) | 2 lines Even more clarification. ........ r58572 | andrew.kuchling | 2007-10-20 12:25:37 -0700 (Sat, 20 Oct 2007) | 1 line Fix protocol name ........ r58573 | andrew.kuchling | 2007-10-20 12:35:18 -0700 (Sat, 20 Oct 2007) | 1 line Various items ........ r58574 | andrew.kuchling | 2007-10-20 12:39:35 -0700 (Sat, 20 Oct 2007) | 1 line Use correct header line ........ r58576 | armin.rigo | 2007-10-21 02:14:15 -0700 (Sun, 21 Oct 2007) | 3 lines Add a crasher for the long-standing issue with closing a file while another thread uses it. ........ r58577 | georg.brandl | 2007-10-21 03:01:56 -0700 (Sun, 21 Oct 2007) | 2 lines Remove duplicate crasher. ........ r58578 | georg.brandl | 2007-10-21 03:24:20 -0700 (Sun, 21 Oct 2007) | 2 lines Unify "byte code" to "bytecode". Also sprinkle :term: markup for it. ........ r58579 | georg.brandl | 2007-10-21 03:32:54 -0700 (Sun, 21 Oct 2007) | 2 lines Add markup to new function descriptions. ........ r58580 | georg.brandl | 2007-10-21 03:45:46 -0700 (Sun, 21 Oct 2007) | 2 lines Add :term:s for descriptors. ........ r58581 | georg.brandl | 2007-10-21 03:46:24 -0700 (Sun, 21 Oct 2007) | 2 lines Unify "file-descriptor" to "file descriptor". ........ r58582 | georg.brandl | 2007-10-21 03:52:38 -0700 (Sun, 21 Oct 2007) | 2 lines Add :term: for generators. ........ r58583 | georg.brandl | 2007-10-21 05:10:28 -0700 (Sun, 21 Oct 2007) | 2 lines Add :term:s for iterator. ........ r58584 | georg.brandl | 2007-10-21 05:15:05 -0700 (Sun, 21 Oct 2007) | 2 lines Add :term:s for "new-style class". ........ r58588 | neal.norwitz | 2007-10-21 21:47:54 -0700 (Sun, 21 Oct 2007) | 1 line Add Chris Monson so he can edit PEPs. ........ r58594 | guido.van.rossum | 2007-10-22 09:27:19 -0700 (Mon, 22 Oct 2007) | 4 lines Issue #1307, patch by Derek Shockey. When "MAIL" is received without args, an exception happens instead of sending a 501 syntax error response. ........ r58598 | travis.oliphant | 2007-10-22 19:40:56 -0700 (Mon, 22 Oct 2007) | 1 line Add phuang patch from Issue 708374 which adds offset parameter to mmap module. ........ r58601 | neal.norwitz | 2007-10-22 22:44:27 -0700 (Mon, 22 Oct 2007) | 2 lines Bug #1313, fix typo (wrong variable name) in example. ........ r58609 | georg.brandl | 2007-10-23 11:21:35 -0700 (Tue, 23 Oct 2007) | 2 lines Update Pygments version from externals. ........ r58618 | guido.van.rossum | 2007-10-23 12:25:41 -0700 (Tue, 23 Oct 2007) | 3 lines Issue 1307 by Derek Shockey, fox the same bug for RCPT. Neal: please backport! ........ r58620 | raymond.hettinger | 2007-10-23 13:37:41 -0700 (Tue, 23 Oct 2007) | 1 line Shorter name for namedtuple() ........ r58621 | andrew.kuchling | 2007-10-23 13:55:47 -0700 (Tue, 23 Oct 2007) | 1 line Update name ........ r58622 | raymond.hettinger | 2007-10-23 14:23:07 -0700 (Tue, 23 Oct 2007) | 1 line Fixup news entry ........ r58623 | raymond.hettinger | 2007-10-23 18:28:33 -0700 (Tue, 23 Oct 2007) | 1 line Optimize sum() for integer and float inputs. ........ r58624 | raymond.hettinger | 2007-10-23 19:05:51 -0700 (Tue, 23 Oct 2007) | 1 line Fixup error return and add support for intermixed ints and floats/ ........ r58628 | vinay.sajip | 2007-10-24 03:47:06 -0700 (Wed, 24 Oct 2007) | 1 line Bug #1321: Fixed logic error in TimedRotatingFileHandler.__init__() ........ r58641 | facundo.batista | 2007-10-24 12:11:08 -0700 (Wed, 24 Oct 2007) | 4 lines Issue 1290. CharacterData.__repr__ was constructing a string in response that keeped having a non-ascii character. ........ r58643 | thomas.heller | 2007-10-24 12:50:45 -0700 (Wed, 24 Oct 2007) | 1 line Added unittest for calling a function with paramflags (backport from py3k branch). ........ r58645 | matthias.klose | 2007-10-24 13:00:44 -0700 (Wed, 24 Oct 2007) | 2 lines - Build using system ffi library on arm*-linux*. ........ r58651 | georg.brandl | 2007-10-24 14:40:38 -0700 (Wed, 24 Oct 2007) | 2 lines Bug #1287: make os.environ.pop() work as expected. ........ r58652 | raymond.hettinger | 2007-10-24 19:26:58 -0700 (Wed, 24 Oct 2007) | 1 line Missing DECREFs ........ r58653 | matthias.klose | 2007-10-24 23:37:24 -0700 (Wed, 24 Oct 2007) | 2 lines - Build using system ffi library on arm*-linux*, pass --with-system-ffi to CONFIG_ARGS ........ r58655 | thomas.heller | 2007-10-25 12:47:32 -0700 (Thu, 25 Oct 2007) | 2 lines ffi_type_longdouble may be already #defined. See issue 1324. ........ r58656 | kurt.kaiser | 2007-10-25 15:43:45 -0700 (Thu, 25 Oct 2007) | 3 lines Correct an ancient bug in an unused path by removing that path: register() is now idempotent. ........ r58660 | kurt.kaiser | 2007-10-25 17:10:09 -0700 (Thu, 25 Oct 2007) | 4 lines 1. Add comments to provide top-level documentation. 2. Refactor to use more descriptive names. 3. Enhance tests in main(). ........ r58675 | georg.brandl | 2007-10-26 11:30:41 -0700 (Fri, 26 Oct 2007) | 2 lines Fix new pop() method on os.environ on ignorecase-platforms. ........ r58696 | neal.norwitz | 2007-10-27 15:32:21 -0700 (Sat, 27 Oct 2007) | 1 line Update URL for Pygments. 0.8.1 is no longer available ........ r58697 | hyeshik.chang | 2007-10-28 04:19:02 -0700 (Sun, 28 Oct 2007) | 3 lines - Add support for FreeBSD 8 which is recently forked from FreeBSD 7. - Regenerate IN module for most recent maintenance tree of FreeBSD 6 and 7. ........ r58698 | hyeshik.chang | 2007-10-28 05:38:09 -0700 (Sun, 28 Oct 2007) | 2 lines Enable platform-specific tweaks for FreeBSD 8 (exactly same to FreeBSD 7's yet) ........ r58700 | kurt.kaiser | 2007-10-28 12:03:59 -0700 (Sun, 28 Oct 2007) | 2 lines Add confirmation dialog before printing. Patch 1717170 Tal Einat. ........ r58706 | guido.van.rossum | 2007-10-29 13:52:45 -0700 (Mon, 29 Oct 2007) | 3 lines Patch 1353 by Jacob Winther. Add mp4 mapping to mimetypes.py. ........ r58709 | guido.van.rossum | 2007-10-29 15:15:05 -0700 (Mon, 29 Oct 2007) | 6 lines Backport fixes for the code that decodes octal escapes (and for PyString also hex escapes) -- this was reaching beyond the end of the input string buffer, even though it is not supposed to be \0-terminated. This has no visible effect but is clearly the correct thing to do. (In 3.0 it had a visible effect after removing ob_sstate from PyString.) ........ r58710 | kurt.kaiser | 2007-10-29 19:38:54 -0700 (Mon, 29 Oct 2007) | 7 lines check in Tal Einat's update to tabpage.py Patch 1612746 M configDialog.py M NEWS.txt AM tabbedpages.py ........ r58715 | georg.brandl | 2007-10-30 10:51:18 -0700 (Tue, 30 Oct 2007) | 2 lines Use correct markup. ........ r58716 | georg.brandl | 2007-10-30 10:57:12 -0700 (Tue, 30 Oct 2007) | 2 lines Make example about hiding None return values at the prompt clearer. ........ r58728 | neal.norwitz | 2007-10-30 23:33:20 -0700 (Tue, 30 Oct 2007) | 1 line Fix some compiler warnings for signed comparisons on Unix and Windows. ........ r58731 | martin.v.loewis | 2007-10-31 10:19:33 -0700 (Wed, 31 Oct 2007) | 2 lines Adding Christian Heimes. ........ r58737 | raymond.hettinger | 2007-10-31 14:57:58 -0700 (Wed, 31 Oct 2007) | 1 line Clarify the reasons why pickle is almost always better than marshal ........ r58739 | raymond.hettinger | 2007-10-31 15:15:49 -0700 (Wed, 31 Oct 2007) | 1 line Sets are marshalable. ........
3301 lines
97 KiB
C
3301 lines
97 KiB
C
/*
|
|
* This file includes functions to transform a concrete syntax tree (CST) to
|
|
* an abstract syntax tree (AST). The main function is PyAST_FromNode().
|
|
*
|
|
*/
|
|
#include "Python.h"
|
|
#include "Python-ast.h"
|
|
#include "grammar.h"
|
|
#include "node.h"
|
|
#include "pyarena.h"
|
|
#include "ast.h"
|
|
#include "token.h"
|
|
#include "parsetok.h"
|
|
#include "graminit.h"
|
|
|
|
#include <assert.h>
|
|
|
|
/* Data structure used internally */
|
|
struct compiling {
|
|
char *c_encoding; /* source encoding */
|
|
PyArena *c_arena; /* arena for allocating memeory */
|
|
};
|
|
|
|
static asdl_seq *seq_for_testlist(struct compiling *, const node *);
|
|
static expr_ty ast_for_expr(struct compiling *, const node *);
|
|
static stmt_ty ast_for_stmt(struct compiling *, const node *);
|
|
static asdl_seq *ast_for_suite(struct compiling *, const node *);
|
|
static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
|
|
expr_context_ty);
|
|
static expr_ty ast_for_testlist(struct compiling *, const node *);
|
|
static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
|
|
|
|
/* Note different signature for ast_for_call */
|
|
static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
|
|
|
|
static PyObject *parsenumber(const char *);
|
|
static PyObject *parsestr(const node *n, const char *encoding, int *bytesmode);
|
|
static PyObject *parsestrplus(struct compiling *, const node *n,
|
|
int *bytesmode);
|
|
|
|
#ifndef LINENO
|
|
#define LINENO(n) ((n)->n_lineno)
|
|
#endif
|
|
|
|
#define COMP_GENEXP 0
|
|
#define COMP_LISTCOMP 1
|
|
#define COMP_SETCOMP 2
|
|
|
|
static identifier
|
|
new_identifier(const char* n, PyArena *arena)
|
|
{
|
|
PyObject* id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
|
|
Py_UNICODE *u = PyUnicode_AS_UNICODE(id);
|
|
/* Check whether there are non-ASCII characters in the
|
|
identifier; if so, normalize to NFKC. */
|
|
for (; *u; u++) {
|
|
if (*u >= 128) {
|
|
PyObject *m = PyImport_ImportModule("unicodedata");
|
|
PyObject *id2;
|
|
if (!m)
|
|
return NULL;
|
|
id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id);
|
|
Py_DECREF(m);
|
|
if (!id2)
|
|
return NULL;
|
|
Py_DECREF(id);
|
|
id = id2;
|
|
break;
|
|
}
|
|
}
|
|
PyUnicode_InternInPlace(&id);
|
|
PyArena_AddPyObject(arena, id);
|
|
return id;
|
|
}
|
|
|
|
#define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena)
|
|
|
|
/* This routine provides an invalid object for the syntax error.
|
|
The outermost routine must unpack this error and create the
|
|
proper object. We do this so that we don't have to pass
|
|
the filename to everything function.
|
|
|
|
XXX Maybe we should just pass the filename...
|
|
*/
|
|
|
|
static int
|
|
ast_error(const node *n, const char *errstr)
|
|
{
|
|
PyObject *u = Py_BuildValue("zi", errstr, LINENO(n));
|
|
if (!u)
|
|
return 0;
|
|
PyErr_SetObject(PyExc_SyntaxError, u);
|
|
Py_DECREF(u);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
ast_error_finish(const char *filename)
|
|
{
|
|
PyObject *type, *value, *tback, *errstr, *loc, *tmp;
|
|
long lineno;
|
|
|
|
assert(PyErr_Occurred());
|
|
if (!PyErr_ExceptionMatches(PyExc_SyntaxError))
|
|
return;
|
|
|
|
PyErr_Fetch(&type, &value, &tback);
|
|
errstr = PyTuple_GetItem(value, 0);
|
|
if (!errstr)
|
|
return;
|
|
Py_INCREF(errstr);
|
|
lineno = PyInt_AsLong(PyTuple_GetItem(value, 1));
|
|
if (lineno == -1) {
|
|
Py_DECREF(errstr);
|
|
return;
|
|
}
|
|
Py_DECREF(value);
|
|
|
|
loc = PyErr_ProgramText(filename, lineno);
|
|
if (!loc) {
|
|
Py_INCREF(Py_None);
|
|
loc = Py_None;
|
|
}
|
|
tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc);
|
|
Py_DECREF(loc);
|
|
if (!tmp) {
|
|
Py_DECREF(errstr);
|
|
return;
|
|
}
|
|
value = PyTuple_Pack(2, errstr, tmp);
|
|
Py_DECREF(errstr);
|
|
Py_DECREF(tmp);
|
|
if (!value)
|
|
return;
|
|
PyErr_Restore(type, value, tback);
|
|
}
|
|
|
|
/* num_stmts() returns number of contained statements.
|
|
|
|
Use this routine to determine how big a sequence is needed for
|
|
the statements in a parse tree. Its raison d'etre is this bit of
|
|
grammar:
|
|
|
|
stmt: simple_stmt | compound_stmt
|
|
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
|
|
|
|
A simple_stmt can contain multiple small_stmt elements joined
|
|
by semicolons. If the arg is a simple_stmt, the number of
|
|
small_stmt elements is returned.
|
|
*/
|
|
|
|
static int
|
|
num_stmts(const node *n)
|
|
{
|
|
int i, l;
|
|
node *ch;
|
|
|
|
switch (TYPE(n)) {
|
|
case single_input:
|
|
if (TYPE(CHILD(n, 0)) == NEWLINE)
|
|
return 0;
|
|
else
|
|
return num_stmts(CHILD(n, 0));
|
|
case file_input:
|
|
l = 0;
|
|
for (i = 0; i < NCH(n); i++) {
|
|
ch = CHILD(n, i);
|
|
if (TYPE(ch) == stmt)
|
|
l += num_stmts(ch);
|
|
}
|
|
return l;
|
|
case stmt:
|
|
return num_stmts(CHILD(n, 0));
|
|
case compound_stmt:
|
|
return 1;
|
|
case simple_stmt:
|
|
return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
|
|
case suite:
|
|
if (NCH(n) == 1)
|
|
return num_stmts(CHILD(n, 0));
|
|
else {
|
|
l = 0;
|
|
for (i = 2; i < (NCH(n) - 1); i++)
|
|
l += num_stmts(CHILD(n, i));
|
|
return l;
|
|
}
|
|
default: {
|
|
char buf[128];
|
|
|
|
sprintf(buf, "Non-statement found: %d %d\n",
|
|
TYPE(n), NCH(n));
|
|
Py_FatalError(buf);
|
|
}
|
|
}
|
|
assert(0);
|
|
return 0;
|
|
}
|
|
|
|
/* Transform the CST rooted at node * to the appropriate AST
|
|
*/
|
|
|
|
mod_ty
|
|
PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,
|
|
PyArena *arena)
|
|
{
|
|
int i, j, k, num;
|
|
asdl_seq *stmts = NULL;
|
|
stmt_ty s;
|
|
node *ch;
|
|
struct compiling c;
|
|
|
|
if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
|
|
c.c_encoding = "utf-8";
|
|
if (TYPE(n) == encoding_decl) {
|
|
#if 0
|
|
ast_error(n, "encoding declaration in Unicode string");
|
|
goto error;
|
|
#endif
|
|
n = CHILD(n, 0);
|
|
}
|
|
} else if (TYPE(n) == encoding_decl) {
|
|
c.c_encoding = STR(n);
|
|
n = CHILD(n, 0);
|
|
} else {
|
|
/* PEP 3120 */
|
|
c.c_encoding = "utf-8";
|
|
}
|
|
c.c_arena = arena;
|
|
|
|
k = 0;
|
|
switch (TYPE(n)) {
|
|
case file_input:
|
|
stmts = asdl_seq_new(num_stmts(n), arena);
|
|
if (!stmts)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n) - 1; i++) {
|
|
ch = CHILD(n, i);
|
|
if (TYPE(ch) == NEWLINE)
|
|
continue;
|
|
REQ(ch, stmt);
|
|
num = num_stmts(ch);
|
|
if (num == 1) {
|
|
s = ast_for_stmt(&c, ch);
|
|
if (!s)
|
|
goto error;
|
|
asdl_seq_SET(stmts, k++, s);
|
|
}
|
|
else {
|
|
ch = CHILD(ch, 0);
|
|
REQ(ch, simple_stmt);
|
|
for (j = 0; j < num; j++) {
|
|
s = ast_for_stmt(&c, CHILD(ch, j * 2));
|
|
if (!s)
|
|
goto error;
|
|
asdl_seq_SET(stmts, k++, s);
|
|
}
|
|
}
|
|
}
|
|
return Module(stmts, arena);
|
|
case eval_input: {
|
|
expr_ty testlist_ast;
|
|
|
|
/* XXX Why not comp_for here? */
|
|
testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
|
|
if (!testlist_ast)
|
|
goto error;
|
|
return Expression(testlist_ast, arena);
|
|
}
|
|
case single_input:
|
|
if (TYPE(CHILD(n, 0)) == NEWLINE) {
|
|
stmts = asdl_seq_new(1, arena);
|
|
if (!stmts)
|
|
goto error;
|
|
asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
|
|
arena));
|
|
if (!asdl_seq_GET(stmts, 0))
|
|
goto error;
|
|
return Interactive(stmts, arena);
|
|
}
|
|
else {
|
|
n = CHILD(n, 0);
|
|
num = num_stmts(n);
|
|
stmts = asdl_seq_new(num, arena);
|
|
if (!stmts)
|
|
goto error;
|
|
if (num == 1) {
|
|
s = ast_for_stmt(&c, n);
|
|
if (!s)
|
|
goto error;
|
|
asdl_seq_SET(stmts, 0, s);
|
|
}
|
|
else {
|
|
/* Only a simple_stmt can contain multiple statements. */
|
|
REQ(n, simple_stmt);
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
if (TYPE(CHILD(n, i)) == NEWLINE)
|
|
break;
|
|
s = ast_for_stmt(&c, CHILD(n, i));
|
|
if (!s)
|
|
goto error;
|
|
asdl_seq_SET(stmts, i / 2, s);
|
|
}
|
|
}
|
|
|
|
return Interactive(stmts, arena);
|
|
}
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"invalid node %d for PyAST_FromNode", TYPE(n));
|
|
goto error;
|
|
}
|
|
error:
|
|
ast_error_finish(filename);
|
|
return NULL;
|
|
}
|
|
|
|
/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
|
|
*/
|
|
|
|
static operator_ty
|
|
get_operator(const node *n)
|
|
{
|
|
switch (TYPE(n)) {
|
|
case VBAR:
|
|
return BitOr;
|
|
case CIRCUMFLEX:
|
|
return BitXor;
|
|
case AMPER:
|
|
return BitAnd;
|
|
case LEFTSHIFT:
|
|
return LShift;
|
|
case RIGHTSHIFT:
|
|
return RShift;
|
|
case PLUS:
|
|
return Add;
|
|
case MINUS:
|
|
return Sub;
|
|
case STAR:
|
|
return Mult;
|
|
case SLASH:
|
|
return Div;
|
|
case DOUBLESLASH:
|
|
return FloorDiv;
|
|
case PERCENT:
|
|
return Mod;
|
|
default:
|
|
return (operator_ty)0;
|
|
}
|
|
}
|
|
|
|
static const char* FORBIDDEN[] = {
|
|
"None",
|
|
"True",
|
|
"False",
|
|
NULL,
|
|
};
|
|
|
|
static int
|
|
forbidden_name(expr_ty e, const node *n)
|
|
{
|
|
const char **p;
|
|
assert(PyUnicode_Check(e->v.Name.id));
|
|
for (p = FORBIDDEN; *p; p++) {
|
|
if (PyUnicode_CompareWithASCIIString(e->v.Name.id, *p) == 0) {
|
|
ast_error(n, "assignment to keyword");
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Set the context ctx for expr_ty e, recursively traversing e.
|
|
|
|
Only sets context for expr kinds that "can appear in assignment context"
|
|
(according to ../Parser/Python.asdl). For other expr kinds, it sets
|
|
an appropriate syntax error and returns false.
|
|
*/
|
|
|
|
static int
|
|
set_context(expr_ty e, expr_context_ty ctx, const node *n)
|
|
{
|
|
asdl_seq *s = NULL;
|
|
/* If a particular expression type can't be used for assign / delete,
|
|
set expr_name to its name and an error message will be generated.
|
|
*/
|
|
const char* expr_name = NULL;
|
|
|
|
/* The ast defines augmented store and load contexts, but the
|
|
implementation here doesn't actually use them. The code may be
|
|
a little more complex than necessary as a result. It also means
|
|
that expressions in an augmented assignment have a Store context.
|
|
Consider restructuring so that augmented assignment uses
|
|
set_context(), too.
|
|
*/
|
|
assert(ctx != AugStore && ctx != AugLoad);
|
|
|
|
switch (e->kind) {
|
|
case Attribute_kind:
|
|
e->v.Attribute.ctx = ctx;
|
|
break;
|
|
case Subscript_kind:
|
|
e->v.Subscript.ctx = ctx;
|
|
break;
|
|
case Starred_kind:
|
|
e->v.Starred.ctx = ctx;
|
|
if (!set_context(e->v.Starred.value, ctx, n))
|
|
return 0;
|
|
break;
|
|
case Name_kind:
|
|
if (ctx == Store) {
|
|
if (forbidden_name(e, n))
|
|
return 0; /* forbidden_name() calls ast_error() */
|
|
}
|
|
e->v.Name.ctx = ctx;
|
|
break;
|
|
case List_kind:
|
|
e->v.List.ctx = ctx;
|
|
s = e->v.List.elts;
|
|
break;
|
|
case Tuple_kind:
|
|
if (asdl_seq_LEN(e->v.Tuple.elts) == 0)
|
|
return ast_error(n, "can't assign to ()");
|
|
e->v.Tuple.ctx = ctx;
|
|
s = e->v.Tuple.elts;
|
|
break;
|
|
case Lambda_kind:
|
|
expr_name = "lambda";
|
|
break;
|
|
case Call_kind:
|
|
expr_name = "function call";
|
|
break;
|
|
case BoolOp_kind:
|
|
case BinOp_kind:
|
|
case UnaryOp_kind:
|
|
expr_name = "operator";
|
|
break;
|
|
case GeneratorExp_kind:
|
|
expr_name = "generator expression";
|
|
break;
|
|
case Yield_kind:
|
|
expr_name = "yield expression";
|
|
break;
|
|
case ListComp_kind:
|
|
expr_name = "list comprehension";
|
|
break;
|
|
case SetComp_kind:
|
|
expr_name = "set comprehension";
|
|
break;
|
|
case DictComp_kind:
|
|
expr_name = "dict comprehension";
|
|
break;
|
|
case Dict_kind:
|
|
case Set_kind:
|
|
case Num_kind:
|
|
case Str_kind:
|
|
expr_name = "literal";
|
|
break;
|
|
case Ellipsis_kind:
|
|
expr_name = "Ellipsis";
|
|
break;
|
|
case Compare_kind:
|
|
expr_name = "comparison";
|
|
break;
|
|
case IfExp_kind:
|
|
expr_name = "conditional expression";
|
|
break;
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unexpected expression in assignment %d (line %d)",
|
|
e->kind, e->lineno);
|
|
return 0;
|
|
}
|
|
/* Check for error string set by switch */
|
|
if (expr_name) {
|
|
char buf[300];
|
|
PyOS_snprintf(buf, sizeof(buf),
|
|
"can't %s %s",
|
|
ctx == Store ? "assign to" : "delete",
|
|
expr_name);
|
|
return ast_error(n, buf);
|
|
}
|
|
|
|
/* If the LHS is a list or tuple, we need to set the assignment
|
|
context for all the contained elements.
|
|
*/
|
|
if (s) {
|
|
int i;
|
|
|
|
for (i = 0; i < asdl_seq_LEN(s); i++) {
|
|
if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static operator_ty
|
|
ast_for_augassign(const node *n)
|
|
{
|
|
REQ(n, augassign);
|
|
n = CHILD(n, 0);
|
|
switch (STR(n)[0]) {
|
|
case '+':
|
|
return Add;
|
|
case '-':
|
|
return Sub;
|
|
case '/':
|
|
if (STR(n)[1] == '/')
|
|
return FloorDiv;
|
|
else
|
|
return Div;
|
|
case '%':
|
|
return Mod;
|
|
case '<':
|
|
return LShift;
|
|
case '>':
|
|
return RShift;
|
|
case '&':
|
|
return BitAnd;
|
|
case '^':
|
|
return BitXor;
|
|
case '|':
|
|
return BitOr;
|
|
case '*':
|
|
if (STR(n)[1] == '*')
|
|
return Pow;
|
|
else
|
|
return Mult;
|
|
default:
|
|
PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
|
|
return (operator_ty)0;
|
|
}
|
|
}
|
|
|
|
static cmpop_ty
|
|
ast_for_comp_op(const node *n)
|
|
{
|
|
/* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
|
|
|'is' 'not'
|
|
*/
|
|
REQ(n, comp_op);
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
switch (TYPE(n)) {
|
|
case LESS:
|
|
return Lt;
|
|
case GREATER:
|
|
return Gt;
|
|
case EQEQUAL: /* == */
|
|
return Eq;
|
|
case LESSEQUAL:
|
|
return LtE;
|
|
case GREATEREQUAL:
|
|
return GtE;
|
|
case NOTEQUAL:
|
|
return NotEq;
|
|
case NAME:
|
|
if (strcmp(STR(n), "in") == 0)
|
|
return In;
|
|
if (strcmp(STR(n), "is") == 0)
|
|
return Is;
|
|
default:
|
|
PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
|
|
STR(n));
|
|
return (cmpop_ty)0;
|
|
}
|
|
}
|
|
else if (NCH(n) == 2) {
|
|
/* handle "not in" and "is not" */
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
case NAME:
|
|
if (strcmp(STR(CHILD(n, 1)), "in") == 0)
|
|
return NotIn;
|
|
if (strcmp(STR(CHILD(n, 0)), "is") == 0)
|
|
return IsNot;
|
|
default:
|
|
PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
|
|
STR(CHILD(n, 0)), STR(CHILD(n, 1)));
|
|
return (cmpop_ty)0;
|
|
}
|
|
}
|
|
PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
|
|
NCH(n));
|
|
return (cmpop_ty)0;
|
|
}
|
|
|
|
static asdl_seq *
|
|
seq_for_testlist(struct compiling *c, const node *n)
|
|
{
|
|
/* testlist: test (',' test)* [','] */
|
|
asdl_seq *seq;
|
|
expr_ty expression;
|
|
int i;
|
|
assert(TYPE(n) == testlist || TYPE(n) == testlist_comp);
|
|
|
|
seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
|
if (!seq)
|
|
return NULL;
|
|
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == test_nocond);
|
|
|
|
expression = ast_for_expr(c, CHILD(n, i));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
assert(i / 2 < seq->size);
|
|
asdl_seq_SET(seq, i / 2, expression);
|
|
}
|
|
return seq;
|
|
}
|
|
|
|
static arg_ty
|
|
compiler_arg(struct compiling *c, const node *n)
|
|
{
|
|
identifier name;
|
|
expr_ty annotation = NULL;
|
|
node *ch;
|
|
|
|
assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
|
|
ch = CHILD(n, 0);
|
|
name = NEW_IDENTIFIER(ch);
|
|
if (!name)
|
|
return NULL;
|
|
|
|
if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
|
|
annotation = ast_for_expr(c, CHILD(n, 2));
|
|
if (!annotation)
|
|
return NULL;
|
|
}
|
|
|
|
return arg(name, annotation, c->c_arena);
|
|
}
|
|
|
|
/* returns -1 if failed to handle keyword only arguments
|
|
returns new position to keep processing if successful
|
|
(',' tfpdef ['=' test])*
|
|
^^^
|
|
start pointing here
|
|
*/
|
|
static int
|
|
handle_keywordonly_args(struct compiling *c, const node *n, int start,
|
|
asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
|
|
{
|
|
node *ch;
|
|
expr_ty expression, annotation;
|
|
arg_ty arg;
|
|
int i = start;
|
|
int j = 0; /* index for kwdefaults and kwonlyargs */
|
|
assert(kwonlyargs != NULL);
|
|
assert(kwdefaults != NULL);
|
|
while (i < NCH(n)) {
|
|
ch = CHILD(n, i);
|
|
switch (TYPE(ch)) {
|
|
case vfpdef:
|
|
case tfpdef:
|
|
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
|
|
expression = ast_for_expr(c, CHILD(n, i + 2));
|
|
asdl_seq_SET(kwdefaults, j, expression);
|
|
i += 2; /* '=' and test */
|
|
}
|
|
else { /* setting NULL if no default value exists */
|
|
asdl_seq_SET(kwdefaults, j, NULL);
|
|
}
|
|
if (NCH(ch) == 3) {
|
|
/* ch is NAME ':' test */
|
|
annotation = ast_for_expr(c, CHILD(ch, 2));
|
|
if (!annotation) {
|
|
ast_error(ch, "expected expression");
|
|
goto error;
|
|
}
|
|
}
|
|
else {
|
|
annotation = NULL;
|
|
}
|
|
ch = CHILD(ch, 0);
|
|
arg = arg(NEW_IDENTIFIER(ch), annotation, c->c_arena);
|
|
if (!arg) {
|
|
ast_error(ch, "expecting name");
|
|
goto error;
|
|
}
|
|
asdl_seq_SET(kwonlyargs, j++, arg);
|
|
i += 2; /* the name and the comma */
|
|
break;
|
|
case DOUBLESTAR:
|
|
return i;
|
|
default:
|
|
ast_error(ch, "unexpected node");
|
|
goto error;
|
|
}
|
|
}
|
|
return i;
|
|
error:
|
|
return -1;
|
|
}
|
|
|
|
/* Create AST for argument list. */
|
|
|
|
static arguments_ty
|
|
ast_for_arguments(struct compiling *c, const node *n)
|
|
{
|
|
/* This function handles both typedargslist (function definition)
|
|
and varargslist (lambda definition).
|
|
|
|
parameters: '(' [typedargslist] ')'
|
|
typedargslist: ((tfpdef ['=' test] ',')*
|
|
('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
|
|
| '**' tfpdef)
|
|
| tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
|
|
tfpdef: NAME [':' test]
|
|
varargslist: ((vfpdef ['=' test] ',')*
|
|
('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef]
|
|
| '**' vfpdef)
|
|
| vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
|
|
vfpdef: NAME
|
|
*/
|
|
int i, j, k, nposargs = 0, nkwonlyargs = 0;
|
|
int nposdefaults = 0, found_default = 0;
|
|
asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
|
|
identifier vararg = NULL, kwarg = NULL;
|
|
arg_ty arg;
|
|
expr_ty varargannotation = NULL, kwargannotation = NULL;
|
|
node *ch;
|
|
|
|
if (TYPE(n) == parameters) {
|
|
if (NCH(n) == 2) /* () as argument list */
|
|
return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
NULL, c->c_arena);
|
|
n = CHILD(n, 1);
|
|
}
|
|
assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
|
|
|
|
/* first count the number of positional args & defaults */
|
|
for (i = 0; i < NCH(n); i++) {
|
|
ch = CHILD(n, i);
|
|
if (TYPE(ch) == STAR) {
|
|
/* skip star and possible argument */
|
|
i++;
|
|
i += (TYPE(CHILD(n, i)) == tfpdef
|
|
|| TYPE(CHILD(n, i)) == vfpdef);
|
|
break;
|
|
}
|
|
if (TYPE(ch) == DOUBLESTAR) break;
|
|
if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
|
|
if (TYPE(ch) == EQUAL) nposdefaults++;
|
|
}
|
|
/* count the number of keyword only args &
|
|
defaults for keyword only args */
|
|
for ( ; i < NCH(n); ++i) {
|
|
ch = CHILD(n, i);
|
|
if (TYPE(ch) == DOUBLESTAR) break;
|
|
if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
|
|
}
|
|
posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
|
|
if (!posargs && nposargs)
|
|
goto error;
|
|
kwonlyargs = (nkwonlyargs ?
|
|
asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
|
|
if (!kwonlyargs && nkwonlyargs)
|
|
goto error;
|
|
posdefaults = (nposdefaults ?
|
|
asdl_seq_new(nposdefaults, c->c_arena) : NULL);
|
|
if (!posdefaults && nposdefaults)
|
|
goto error;
|
|
/* The length of kwonlyargs and kwdefaults are same
|
|
since we set NULL as default for keyword only argument w/o default
|
|
- we have sequence data structure, but no dictionary */
|
|
kwdefaults = (nkwonlyargs ?
|
|
asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
|
|
if (!kwdefaults && nkwonlyargs)
|
|
goto error;
|
|
|
|
if (nposargs + nkwonlyargs > 255) {
|
|
ast_error(n, "more than 255 arguments");
|
|
return NULL;
|
|
}
|
|
|
|
/* tfpdef: NAME [':' test]
|
|
vfpdef: NAME
|
|
*/
|
|
i = 0;
|
|
j = 0; /* index for defaults */
|
|
k = 0; /* index for args */
|
|
while (i < NCH(n)) {
|
|
ch = CHILD(n, i);
|
|
switch (TYPE(ch)) {
|
|
case tfpdef:
|
|
case vfpdef:
|
|
/* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
|
|
anything other than EQUAL or a comma? */
|
|
/* XXX Should NCH(n) check be made a separate check? */
|
|
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
|
|
expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
|
|
if (!expression)
|
|
goto error;
|
|
assert(posdefaults != NULL);
|
|
asdl_seq_SET(posdefaults, j++, expression);
|
|
i += 2;
|
|
found_default = 1;
|
|
}
|
|
else if (found_default) {
|
|
ast_error(n,
|
|
"non-default argument follows default argument");
|
|
goto error;
|
|
}
|
|
arg = compiler_arg(c, ch);
|
|
if (!arg)
|
|
goto error;
|
|
asdl_seq_SET(posargs, k++, arg);
|
|
|
|
i += 2; /* the name and the comma */
|
|
break;
|
|
case STAR:
|
|
if (i+1 >= NCH(n)) {
|
|
ast_error(CHILD(n, i), "no name for vararg");
|
|
goto error;
|
|
}
|
|
ch = CHILD(n, i+1); /* tfpdef or COMMA */
|
|
if (TYPE(ch) == COMMA) {
|
|
int res = 0;
|
|
i += 2; /* now follows keyword only arguments */
|
|
res = handle_keywordonly_args(c, n, i,
|
|
kwonlyargs, kwdefaults);
|
|
if (res == -1) goto error;
|
|
i = res; /* res has new position to process */
|
|
}
|
|
else {
|
|
vararg = NEW_IDENTIFIER(CHILD(ch, 0));
|
|
if (NCH(ch) > 1) {
|
|
/* there is an annotation on the vararg */
|
|
varargannotation = ast_for_expr(c, CHILD(ch, 2));
|
|
}
|
|
i += 3;
|
|
if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
|
|
|| TYPE(CHILD(n, i)) == vfpdef)) {
|
|
int res = 0;
|
|
res = handle_keywordonly_args(c, n, i,
|
|
kwonlyargs, kwdefaults);
|
|
if (res == -1) goto error;
|
|
i = res; /* res has new position to process */
|
|
}
|
|
}
|
|
break;
|
|
case DOUBLESTAR:
|
|
ch = CHILD(n, i+1); /* tfpdef */
|
|
assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
|
|
kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
|
|
if (NCH(ch) > 1) {
|
|
/* there is an annotation on the kwarg */
|
|
kwargannotation = ast_for_expr(c, CHILD(ch, 2));
|
|
}
|
|
i += 3;
|
|
break;
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unexpected node in varargslist: %d @ %d",
|
|
TYPE(ch), i);
|
|
goto error;
|
|
}
|
|
}
|
|
return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
|
|
kwargannotation, posdefaults, kwdefaults, c->c_arena);
|
|
error:
|
|
Py_XDECREF(vararg);
|
|
Py_XDECREF(kwarg);
|
|
return NULL;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_dotted_name(struct compiling *c, const node *n)
|
|
{
|
|
expr_ty e;
|
|
identifier id;
|
|
int lineno, col_offset;
|
|
int i;
|
|
|
|
REQ(n, dotted_name);
|
|
|
|
lineno = LINENO(n);
|
|
col_offset = n->n_col_offset;
|
|
|
|
id = NEW_IDENTIFIER(CHILD(n, 0));
|
|
if (!id)
|
|
return NULL;
|
|
e = Name(id, Load, lineno, col_offset, c->c_arena);
|
|
if (!e)
|
|
return NULL;
|
|
|
|
for (i = 2; i < NCH(n); i+=2) {
|
|
id = NEW_IDENTIFIER(CHILD(n, i));
|
|
if (!id)
|
|
return NULL;
|
|
e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
|
|
if (!e)
|
|
return NULL;
|
|
}
|
|
|
|
return e;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_decorator(struct compiling *c, const node *n)
|
|
{
|
|
/* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
|
|
expr_ty d = NULL;
|
|
expr_ty name_expr;
|
|
|
|
REQ(n, decorator);
|
|
REQ(CHILD(n, 0), AT);
|
|
REQ(RCHILD(n, -1), NEWLINE);
|
|
|
|
name_expr = ast_for_dotted_name(c, CHILD(n, 1));
|
|
if (!name_expr)
|
|
return NULL;
|
|
|
|
if (NCH(n) == 3) { /* No arguments */
|
|
d = name_expr;
|
|
name_expr = NULL;
|
|
}
|
|
else if (NCH(n) == 5) { /* Call with no arguments */
|
|
d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
if (!d)
|
|
return NULL;
|
|
name_expr = NULL;
|
|
}
|
|
else {
|
|
d = ast_for_call(c, CHILD(n, 3), name_expr);
|
|
if (!d)
|
|
return NULL;
|
|
name_expr = NULL;
|
|
}
|
|
|
|
return d;
|
|
}
|
|
|
|
static asdl_seq*
|
|
ast_for_decorators(struct compiling *c, const node *n)
|
|
{
|
|
asdl_seq* decorator_seq;
|
|
expr_ty d;
|
|
int i;
|
|
|
|
REQ(n, decorators);
|
|
decorator_seq = asdl_seq_new(NCH(n), c->c_arena);
|
|
if (!decorator_seq)
|
|
return NULL;
|
|
|
|
for (i = 0; i < NCH(n); i++) {
|
|
d = ast_for_decorator(c, CHILD(n, i));
|
|
if (!d)
|
|
return NULL;
|
|
asdl_seq_SET(decorator_seq, i, d);
|
|
}
|
|
return decorator_seq;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
|
|
{
|
|
/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
|
|
identifier name;
|
|
arguments_ty args;
|
|
asdl_seq *body;
|
|
expr_ty returns = NULL;
|
|
int name_i = 1;
|
|
|
|
REQ(n, funcdef);
|
|
|
|
name = NEW_IDENTIFIER(CHILD(n, name_i));
|
|
if (!name)
|
|
return NULL;
|
|
args = ast_for_arguments(c, CHILD(n, name_i + 1));
|
|
if (!args)
|
|
return NULL;
|
|
if (TYPE(CHILD(n, name_i+2)) == RARROW) {
|
|
returns = ast_for_expr(c, CHILD(n, name_i + 3));
|
|
if (!returns)
|
|
return NULL;
|
|
name_i += 2;
|
|
}
|
|
body = ast_for_suite(c, CHILD(n, name_i + 3));
|
|
if (!body)
|
|
return NULL;
|
|
|
|
return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_decorated(struct compiling *c, const node *n)
|
|
{
|
|
/* decorated: decorators (classdef | funcdef) */
|
|
stmt_ty thing = NULL;
|
|
asdl_seq *decorator_seq = NULL;
|
|
|
|
REQ(n, decorated);
|
|
|
|
decorator_seq = ast_for_decorators(c, CHILD(n, 0));
|
|
if (!decorator_seq)
|
|
return NULL;
|
|
|
|
assert(TYPE(CHILD(n, 1)) == funcdef ||
|
|
TYPE(CHILD(n, 1)) == classdef);
|
|
|
|
if (TYPE(CHILD(n, 1)) == funcdef) {
|
|
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
|
|
} else if (TYPE(CHILD(n, 1)) == classdef) {
|
|
thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
|
|
}
|
|
return thing;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_lambdef(struct compiling *c, const node *n)
|
|
{
|
|
/* lambdef: 'lambda' [varargslist] ':' test
|
|
lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
|
|
arguments_ty args;
|
|
expr_ty expression;
|
|
|
|
if (NCH(n) == 3) {
|
|
args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
NULL, c->c_arena);
|
|
if (!args)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, 2));
|
|
if (!expression)
|
|
return NULL;
|
|
}
|
|
else {
|
|
args = ast_for_arguments(c, CHILD(n, 1));
|
|
if (!args)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, 3));
|
|
if (!expression)
|
|
return NULL;
|
|
}
|
|
|
|
return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_ifexpr(struct compiling *c, const node *n)
|
|
{
|
|
/* test: or_test 'if' or_test 'else' test */
|
|
expr_ty expression, body, orelse;
|
|
|
|
assert(NCH(n) == 5);
|
|
body = ast_for_expr(c, CHILD(n, 0));
|
|
if (!body)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, 2));
|
|
if (!expression)
|
|
return NULL;
|
|
orelse = ast_for_expr(c, CHILD(n, 4));
|
|
if (!orelse)
|
|
return NULL;
|
|
return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
|
|
/*
|
|
Count the number of 'for' loops in a comprehension.
|
|
|
|
Helper for ast_for_comprehension().
|
|
*/
|
|
|
|
static int
|
|
count_comp_fors(const node *n)
|
|
{
|
|
int n_fors = 0;
|
|
|
|
count_comp_for:
|
|
n_fors++;
|
|
REQ(n, comp_for);
|
|
if (NCH(n) == 5)
|
|
n = CHILD(n, 4);
|
|
else
|
|
return n_fors;
|
|
count_comp_iter:
|
|
REQ(n, comp_iter);
|
|
n = CHILD(n, 0);
|
|
if (TYPE(n) == comp_for)
|
|
goto count_comp_for;
|
|
else if (TYPE(n) == comp_if) {
|
|
if (NCH(n) == 3) {
|
|
n = CHILD(n, 2);
|
|
goto count_comp_iter;
|
|
}
|
|
else
|
|
return n_fors;
|
|
}
|
|
|
|
/* Should never be reached */
|
|
PyErr_SetString(PyExc_SystemError,
|
|
"logic error in count_comp_fors");
|
|
return -1;
|
|
}
|
|
|
|
/* Count the number of 'if' statements in a comprehension.
|
|
|
|
Helper for ast_for_comprehension().
|
|
*/
|
|
|
|
static int
|
|
count_comp_ifs(const node *n)
|
|
{
|
|
int n_ifs = 0;
|
|
|
|
while (1) {
|
|
REQ(n, comp_iter);
|
|
if (TYPE(CHILD(n, 0)) == comp_for)
|
|
return n_ifs;
|
|
n = CHILD(n, 0);
|
|
REQ(n, comp_if);
|
|
n_ifs++;
|
|
if (NCH(n) == 2)
|
|
return n_ifs;
|
|
n = CHILD(n, 2);
|
|
}
|
|
}
|
|
|
|
static asdl_seq *
|
|
ast_for_comprehension(struct compiling *c, const node *n)
|
|
{
|
|
int i, n_fors;
|
|
asdl_seq *comps;
|
|
|
|
n_fors = count_comp_fors(n);
|
|
if (n_fors == -1)
|
|
return NULL;
|
|
|
|
comps = asdl_seq_new(n_fors, c->c_arena);
|
|
if (!comps)
|
|
return NULL;
|
|
|
|
for (i = 0; i < n_fors; i++) {
|
|
comprehension_ty comp;
|
|
asdl_seq *t;
|
|
expr_ty expression;
|
|
node *for_ch;
|
|
|
|
REQ(n, comp_for);
|
|
|
|
for_ch = CHILD(n, 1);
|
|
t = ast_for_exprlist(c, for_ch, Store);
|
|
if (!t)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, 3));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
/* Check the # of children rather than the length of t, since
|
|
(x for x, in ...) has 1 element in t, but still requires a Tuple. */
|
|
if (NCH(for_ch) == 1)
|
|
comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
|
|
NULL, c->c_arena);
|
|
else
|
|
comp = comprehension(Tuple(t, Store, LINENO(n), n->n_col_offset,
|
|
c->c_arena),
|
|
expression, NULL, c->c_arena);
|
|
|
|
if (!comp)
|
|
return NULL;
|
|
|
|
if (NCH(n) == 5) {
|
|
int j, n_ifs;
|
|
asdl_seq *ifs;
|
|
|
|
n = CHILD(n, 4);
|
|
n_ifs = count_comp_ifs(n);
|
|
if (n_ifs == -1)
|
|
return NULL;
|
|
|
|
ifs = asdl_seq_new(n_ifs, c->c_arena);
|
|
if (!ifs)
|
|
return NULL;
|
|
|
|
for (j = 0; j < n_ifs; j++) {
|
|
REQ(n, comp_iter);
|
|
n = CHILD(n, 0);
|
|
REQ(n, comp_if);
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
asdl_seq_SET(ifs, j, expression);
|
|
if (NCH(n) == 3)
|
|
n = CHILD(n, 2);
|
|
}
|
|
/* on exit, must guarantee that n is a comp_for */
|
|
if (TYPE(n) == comp_iter)
|
|
n = CHILD(n, 0);
|
|
comp->ifs = ifs;
|
|
}
|
|
asdl_seq_SET(comps, i, comp);
|
|
}
|
|
return comps;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_itercomp(struct compiling *c, const node *n, int type)
|
|
{
|
|
/* testlist_comp: test ( comp_for | (',' test)* [','] )
|
|
argument: [test '='] test [comp_for] # Really [keyword '='] test */
|
|
expr_ty elt;
|
|
asdl_seq *comps;
|
|
|
|
assert(NCH(n) > 1);
|
|
|
|
elt = ast_for_expr(c, CHILD(n, 0));
|
|
if (!elt)
|
|
return NULL;
|
|
|
|
comps = ast_for_comprehension(c, CHILD(n, 1));
|
|
if (!comps)
|
|
return NULL;
|
|
|
|
if (type == COMP_GENEXP)
|
|
return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else if (type == COMP_LISTCOMP)
|
|
return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else if (type == COMP_SETCOMP)
|
|
return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else
|
|
/* Should never happen */
|
|
return NULL;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_dictcomp(struct compiling *c, const node *n)
|
|
{
|
|
expr_ty key, value;
|
|
asdl_seq *comps;
|
|
|
|
assert(NCH(n) > 3);
|
|
REQ(CHILD(n, 1), COLON);
|
|
|
|
key = ast_for_expr(c, CHILD(n, 0));
|
|
if (!key)
|
|
return NULL;
|
|
|
|
value = ast_for_expr(c, CHILD(n, 2));
|
|
if (!value)
|
|
return NULL;
|
|
|
|
comps = ast_for_comprehension(c, CHILD(n, 3));
|
|
if (!comps)
|
|
return NULL;
|
|
|
|
return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_genexp(struct compiling *c, const node *n)
|
|
{
|
|
assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
|
|
return ast_for_itercomp(c, n, COMP_GENEXP);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_listcomp(struct compiling *c, const node *n)
|
|
{
|
|
assert(TYPE(n) == (testlist_comp));
|
|
return ast_for_itercomp(c, n, COMP_LISTCOMP);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_setcomp(struct compiling *c, const node *n)
|
|
{
|
|
assert(TYPE(n) == (dictorsetmaker));
|
|
return ast_for_itercomp(c, n, COMP_SETCOMP);
|
|
}
|
|
|
|
|
|
static expr_ty
|
|
ast_for_atom(struct compiling *c, const node *n)
|
|
{
|
|
/* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
|
|
| '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
|
|
| '...' | 'None' | 'True' | 'False'
|
|
*/
|
|
node *ch = CHILD(n, 0);
|
|
int bytesmode = 0;
|
|
|
|
switch (TYPE(ch)) {
|
|
case NAME:
|
|
/* All names start in Load context, but may later be
|
|
changed. */
|
|
return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
case STRING: {
|
|
PyObject *str = parsestrplus(c, n, &bytesmode);
|
|
if (!str) {
|
|
if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
|
|
PyObject *type, *value, *tback, *errstr;
|
|
PyErr_Fetch(&type, &value, &tback);
|
|
errstr = ((PyUnicodeErrorObject *)value)->reason;
|
|
if (errstr) {
|
|
char *s = "";
|
|
char buf[128];
|
|
s = PyString_AsString(errstr);
|
|
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
|
|
ast_error(n, buf);
|
|
} else {
|
|
ast_error(n, "(unicode error) unknown error");
|
|
}
|
|
Py_DECREF(type);
|
|
Py_DECREF(value);
|
|
Py_XDECREF(tback);
|
|
}
|
|
return NULL;
|
|
}
|
|
PyArena_AddPyObject(c->c_arena, str);
|
|
if (bytesmode)
|
|
return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else
|
|
return Str(str, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
case NUMBER: {
|
|
PyObject *pynum = parsenumber(STR(ch));
|
|
if (!pynum)
|
|
return NULL;
|
|
|
|
PyArena_AddPyObject(c->c_arena, pynum);
|
|
return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
case ELLIPSIS: /* Ellipsis */
|
|
return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
|
|
case LPAR: /* some parenthesized expressions */
|
|
ch = CHILD(n, 1);
|
|
|
|
if (TYPE(ch) == RPAR)
|
|
return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
|
|
if (TYPE(ch) == yield_expr)
|
|
return ast_for_expr(c, ch);
|
|
|
|
/* testlist_comp: test ( comp_for | (',' test)* [','] ) */
|
|
if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
|
|
return ast_for_genexp(c, ch);
|
|
|
|
return ast_for_testlist(c, ch);
|
|
case LSQB: /* list (or list comprehension) */
|
|
ch = CHILD(n, 1);
|
|
|
|
if (TYPE(ch) == RSQB)
|
|
return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
|
|
REQ(ch, testlist_comp);
|
|
if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
|
|
asdl_seq *elts = seq_for_testlist(c, ch);
|
|
if (!elts)
|
|
return NULL;
|
|
|
|
return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else
|
|
return ast_for_listcomp(c, ch);
|
|
case LBRACE: {
|
|
/* dictorsetmaker: test ':' test (',' test ':' test)* [','] |
|
|
* test (gen_for | (',' test)* [',']) */
|
|
int i, size;
|
|
asdl_seq *keys, *values;
|
|
|
|
ch = CHILD(n, 1);
|
|
if (TYPE(ch) == RBRACE) {
|
|
/* it's an empty dict */
|
|
return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
|
|
} else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
|
|
/* it's a simple set */
|
|
asdl_seq *elts;
|
|
size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */
|
|
elts = asdl_seq_new(size, c->c_arena);
|
|
if (!elts)
|
|
return NULL;
|
|
for (i = 0; i < NCH(ch); i += 2) {
|
|
expr_ty expression;
|
|
expression = ast_for_expr(c, CHILD(ch, i));
|
|
if (!expression)
|
|
return NULL;
|
|
asdl_seq_SET(elts, i / 2, expression);
|
|
}
|
|
return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
|
|
} else if (TYPE(CHILD(ch, 1)) == comp_for) {
|
|
/* it's a set comprehension */
|
|
return ast_for_setcomp(c, ch);
|
|
} else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) {
|
|
return ast_for_dictcomp(c, ch);
|
|
} else {
|
|
/* it's a dict */
|
|
size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */
|
|
keys = asdl_seq_new(size, c->c_arena);
|
|
if (!keys)
|
|
return NULL;
|
|
|
|
values = asdl_seq_new(size, c->c_arena);
|
|
if (!values)
|
|
return NULL;
|
|
|
|
for (i = 0; i < NCH(ch); i += 4) {
|
|
expr_ty expression;
|
|
|
|
expression = ast_for_expr(c, CHILD(ch, i));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
asdl_seq_SET(keys, i / 4, expression);
|
|
|
|
expression = ast_for_expr(c, CHILD(ch, i + 2));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
asdl_seq_SET(values, i / 4, expression);
|
|
}
|
|
return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
}
|
|
default:
|
|
PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
static slice_ty
|
|
ast_for_slice(struct compiling *c, const node *n)
|
|
{
|
|
node *ch;
|
|
expr_ty lower = NULL, upper = NULL, step = NULL;
|
|
|
|
REQ(n, subscript);
|
|
|
|
/*
|
|
subscript: test | [test] ':' [test] [sliceop]
|
|
sliceop: ':' [test]
|
|
*/
|
|
ch = CHILD(n, 0);
|
|
if (NCH(n) == 1 && TYPE(ch) == test) {
|
|
/* 'step' variable hold no significance in terms of being used over
|
|
other vars */
|
|
step = ast_for_expr(c, ch);
|
|
if (!step)
|
|
return NULL;
|
|
|
|
return Index(step, c->c_arena);
|
|
}
|
|
|
|
if (TYPE(ch) == test) {
|
|
lower = ast_for_expr(c, ch);
|
|
if (!lower)
|
|
return NULL;
|
|
}
|
|
|
|
/* If there's an upper bound it's in the second or third position. */
|
|
if (TYPE(ch) == COLON) {
|
|
if (NCH(n) > 1) {
|
|
node *n2 = CHILD(n, 1);
|
|
|
|
if (TYPE(n2) == test) {
|
|
upper = ast_for_expr(c, n2);
|
|
if (!upper)
|
|
return NULL;
|
|
}
|
|
}
|
|
} else if (NCH(n) > 2) {
|
|
node *n2 = CHILD(n, 2);
|
|
|
|
if (TYPE(n2) == test) {
|
|
upper = ast_for_expr(c, n2);
|
|
if (!upper)
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
ch = CHILD(n, NCH(n) - 1);
|
|
if (TYPE(ch) == sliceop) {
|
|
if (NCH(ch) == 1) {
|
|
/* No expression, so step is None */
|
|
ch = CHILD(ch, 0);
|
|
step = Name(new_identifier("None", c->c_arena), Load,
|
|
LINENO(ch), ch->n_col_offset, c->c_arena);
|
|
if (!step)
|
|
return NULL;
|
|
} else {
|
|
ch = CHILD(ch, 1);
|
|
if (TYPE(ch) == test) {
|
|
step = ast_for_expr(c, ch);
|
|
if (!step)
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return Slice(lower, upper, step, c->c_arena);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_binop(struct compiling *c, const node *n)
|
|
{
|
|
/* Must account for a sequence of expressions.
|
|
How should A op B op C by represented?
|
|
BinOp(BinOp(A, op, B), op, C).
|
|
*/
|
|
|
|
int i, nops;
|
|
expr_ty expr1, expr2, result;
|
|
operator_ty newoperator;
|
|
|
|
expr1 = ast_for_expr(c, CHILD(n, 0));
|
|
if (!expr1)
|
|
return NULL;
|
|
|
|
expr2 = ast_for_expr(c, CHILD(n, 2));
|
|
if (!expr2)
|
|
return NULL;
|
|
|
|
newoperator = get_operator(CHILD(n, 1));
|
|
if (!newoperator)
|
|
return NULL;
|
|
|
|
result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
if (!result)
|
|
return NULL;
|
|
|
|
nops = (NCH(n) - 1) / 2;
|
|
for (i = 1; i < nops; i++) {
|
|
expr_ty tmp_result, tmp;
|
|
const node* next_oper = CHILD(n, i * 2 + 1);
|
|
|
|
newoperator = get_operator(next_oper);
|
|
if (!newoperator)
|
|
return NULL;
|
|
|
|
tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
|
|
if (!tmp)
|
|
return NULL;
|
|
|
|
tmp_result = BinOp(result, newoperator, tmp,
|
|
LINENO(next_oper), next_oper->n_col_offset,
|
|
c->c_arena);
|
|
if (!tmp_result)
|
|
return NULL;
|
|
result = tmp_result;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
|
|
{
|
|
/* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
|
subscriptlist: subscript (',' subscript)* [',']
|
|
subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
|
|
*/
|
|
REQ(n, trailer);
|
|
if (TYPE(CHILD(n, 0)) == LPAR) {
|
|
if (NCH(n) == 2)
|
|
return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
else
|
|
return ast_for_call(c, CHILD(n, 1), left_expr);
|
|
}
|
|
else if (TYPE(CHILD(n, 0)) == DOT ) {
|
|
return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load,
|
|
LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else {
|
|
REQ(CHILD(n, 0), LSQB);
|
|
REQ(CHILD(n, 2), RSQB);
|
|
n = CHILD(n, 1);
|
|
if (NCH(n) == 1) {
|
|
slice_ty slc = ast_for_slice(c, CHILD(n, 0));
|
|
if (!slc)
|
|
return NULL;
|
|
return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
else {
|
|
/* The grammar is ambiguous here. The ambiguity is resolved
|
|
by treating the sequence as a tuple literal if there are
|
|
no slice features.
|
|
*/
|
|
int j;
|
|
slice_ty slc;
|
|
expr_ty e;
|
|
int simple = 1;
|
|
asdl_seq *slices, *elts;
|
|
slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
|
if (!slices)
|
|
return NULL;
|
|
for (j = 0; j < NCH(n); j += 2) {
|
|
slc = ast_for_slice(c, CHILD(n, j));
|
|
if (!slc)
|
|
return NULL;
|
|
if (slc->kind != Index_kind)
|
|
simple = 0;
|
|
asdl_seq_SET(slices, j / 2, slc);
|
|
}
|
|
if (!simple) {
|
|
return Subscript(left_expr, ExtSlice(slices, c->c_arena),
|
|
Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
/* extract Index values and put them in a Tuple */
|
|
elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
|
|
if (!elts)
|
|
return NULL;
|
|
for (j = 0; j < asdl_seq_LEN(slices); ++j) {
|
|
slc = (slice_ty)asdl_seq_GET(slices, j);
|
|
assert(slc->kind == Index_kind && slc->v.Index.value);
|
|
asdl_seq_SET(elts, j, slc->v.Index.value);
|
|
}
|
|
e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
if (!e)
|
|
return NULL;
|
|
return Subscript(left_expr, Index(e, c->c_arena),
|
|
Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
}
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_factor(struct compiling *c, const node *n)
|
|
{
|
|
node *pfactor, *ppower, *patom, *pnum;
|
|
expr_ty expression;
|
|
|
|
/* If the unary - operator is applied to a constant, don't generate
|
|
a UNARY_NEGATIVE opcode. Just store the approriate value as a
|
|
constant. The peephole optimizer already does something like
|
|
this but it doesn't handle the case where the constant is
|
|
(sys.maxint - 1). In that case, we want a PyIntObject, not a
|
|
PyLongObject.
|
|
*/
|
|
if (TYPE(CHILD(n, 0)) == MINUS
|
|
&& NCH(n) == 2
|
|
&& TYPE((pfactor = CHILD(n, 1))) == factor
|
|
&& NCH(pfactor) == 1
|
|
&& TYPE((ppower = CHILD(pfactor, 0))) == power
|
|
&& NCH(ppower) == 1
|
|
&& TYPE((patom = CHILD(ppower, 0))) == atom
|
|
&& TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
|
|
char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
|
|
if (s == NULL)
|
|
return NULL;
|
|
s[0] = '-';
|
|
strcpy(s + 1, STR(pnum));
|
|
PyObject_FREE(STR(pnum));
|
|
STR(pnum) = s;
|
|
return ast_for_atom(c, patom);
|
|
}
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
switch (TYPE(CHILD(n, 0))) {
|
|
case PLUS:
|
|
return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
case MINUS:
|
|
return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
case TILDE:
|
|
return UnaryOp(Invert, expression, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
}
|
|
PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
|
|
TYPE(CHILD(n, 0)));
|
|
return NULL;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_power(struct compiling *c, const node *n)
|
|
{
|
|
/* power: atom trailer* ('**' factor)*
|
|
*/
|
|
int i;
|
|
expr_ty e, tmp;
|
|
REQ(n, power);
|
|
e = ast_for_atom(c, CHILD(n, 0));
|
|
if (!e)
|
|
return NULL;
|
|
if (NCH(n) == 1)
|
|
return e;
|
|
for (i = 1; i < NCH(n); i++) {
|
|
node *ch = CHILD(n, i);
|
|
if (TYPE(ch) != trailer)
|
|
break;
|
|
tmp = ast_for_trailer(c, ch, e);
|
|
if (!tmp)
|
|
return NULL;
|
|
tmp->lineno = e->lineno;
|
|
tmp->col_offset = e->col_offset;
|
|
e = tmp;
|
|
}
|
|
if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
|
|
expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
|
|
if (!f)
|
|
return NULL;
|
|
tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
|
|
if (!tmp)
|
|
return NULL;
|
|
e = tmp;
|
|
}
|
|
return e;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_starred(struct compiling *c, const node *n)
|
|
{
|
|
expr_ty tmp;
|
|
REQ(n, star_expr);
|
|
|
|
tmp = ast_for_expr(c, CHILD(n, 1));
|
|
if (!tmp)
|
|
return NULL;
|
|
|
|
/* The Load context is changed later. */
|
|
return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
|
|
/* Do not name a variable 'expr'! Will cause a compile error.
|
|
*/
|
|
|
|
static expr_ty
|
|
ast_for_expr(struct compiling *c, const node *n)
|
|
{
|
|
/* handle the full range of simple expressions
|
|
test: or_test ['if' or_test 'else' test] | lambdef
|
|
test_nocond: or_test | lambdef_nocond
|
|
or_test: and_test ('or' and_test)*
|
|
and_test: not_test ('and' not_test)*
|
|
not_test: 'not' not_test | comparison
|
|
comparison: expr (comp_op expr)*
|
|
expr: xor_expr ('|' xor_expr)*
|
|
xor_expr: and_expr ('^' and_expr)*
|
|
and_expr: shift_expr ('&' shift_expr)*
|
|
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
|
|
arith_expr: term (('+'|'-') term)*
|
|
term: factor (('*'|'/'|'%'|'//') factor)*
|
|
factor: ('+'|'-'|'~') factor | power
|
|
power: atom trailer* ('**' factor)*
|
|
*/
|
|
|
|
asdl_seq *seq;
|
|
int i;
|
|
|
|
loop:
|
|
switch (TYPE(n)) {
|
|
case test:
|
|
case test_nocond:
|
|
if (TYPE(CHILD(n, 0)) == lambdef ||
|
|
TYPE(CHILD(n, 0)) == lambdef_nocond)
|
|
return ast_for_lambdef(c, CHILD(n, 0));
|
|
else if (NCH(n) > 1)
|
|
return ast_for_ifexpr(c, n);
|
|
/* Fallthrough */
|
|
case or_test:
|
|
case and_test:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
|
if (!seq)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
expr_ty e = ast_for_expr(c, CHILD(n, i));
|
|
if (!e)
|
|
return NULL;
|
|
asdl_seq_SET(seq, i / 2, e);
|
|
}
|
|
if (!strcmp(STR(CHILD(n, 1)), "and"))
|
|
return BoolOp(And, seq, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
assert(!strcmp(STR(CHILD(n, 1)), "or"));
|
|
return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
|
|
case not_test:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
else {
|
|
expr_ty expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
|
|
return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
case comparison:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
else {
|
|
expr_ty expression;
|
|
asdl_int_seq *ops;
|
|
asdl_seq *cmps;
|
|
ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
|
|
if (!ops)
|
|
return NULL;
|
|
cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
|
|
if (!cmps) {
|
|
return NULL;
|
|
}
|
|
for (i = 1; i < NCH(n); i += 2) {
|
|
cmpop_ty newoperator;
|
|
|
|
newoperator = ast_for_comp_op(CHILD(n, i));
|
|
if (!newoperator) {
|
|
return NULL;
|
|
}
|
|
|
|
expression = ast_for_expr(c, CHILD(n, i + 1));
|
|
if (!expression) {
|
|
return NULL;
|
|
}
|
|
|
|
asdl_seq_SET(ops, i / 2, newoperator);
|
|
asdl_seq_SET(cmps, i / 2, expression);
|
|
}
|
|
expression = ast_for_expr(c, CHILD(n, 0));
|
|
if (!expression) {
|
|
return NULL;
|
|
}
|
|
|
|
return Compare(expression, ops, cmps, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
}
|
|
break;
|
|
|
|
case star_expr:
|
|
if (TYPE(CHILD(n, 0)) == STAR) {
|
|
return ast_for_starred(c, n);
|
|
}
|
|
/* Fallthrough */
|
|
/* The next five cases all handle BinOps. The main body of code
|
|
is the same in each case, but the switch turned inside out to
|
|
reuse the code for each type of operator.
|
|
*/
|
|
case expr:
|
|
case xor_expr:
|
|
case and_expr:
|
|
case shift_expr:
|
|
case arith_expr:
|
|
case term:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
return ast_for_binop(c, n);
|
|
case yield_expr: {
|
|
expr_ty exp = NULL;
|
|
if (NCH(n) == 2) {
|
|
exp = ast_for_testlist(c, CHILD(n, 1));
|
|
if (!exp)
|
|
return NULL;
|
|
}
|
|
return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
case factor:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
return ast_for_factor(c, n);
|
|
case power:
|
|
return ast_for_power(c, n);
|
|
default:
|
|
PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
|
|
return NULL;
|
|
}
|
|
/* should never get here unless if error is set */
|
|
return NULL;
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_call(struct compiling *c, const node *n, expr_ty func)
|
|
{
|
|
/*
|
|
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
|
|
| '**' test)
|
|
argument: [test '='] test [comp_for] # Really [keyword '='] test
|
|
*/
|
|
|
|
int i, nargs, nkeywords, ngens;
|
|
asdl_seq *args;
|
|
asdl_seq *keywords;
|
|
expr_ty vararg = NULL, kwarg = NULL;
|
|
|
|
REQ(n, arglist);
|
|
|
|
nargs = 0;
|
|
nkeywords = 0;
|
|
ngens = 0;
|
|
for (i = 0; i < NCH(n); i++) {
|
|
node *ch = CHILD(n, i);
|
|
if (TYPE(ch) == argument) {
|
|
if (NCH(ch) == 1)
|
|
nargs++;
|
|
else if (TYPE(CHILD(ch, 1)) == comp_for)
|
|
ngens++;
|
|
else
|
|
nkeywords++;
|
|
}
|
|
}
|
|
if (ngens > 1 || (ngens && (nargs || nkeywords))) {
|
|
ast_error(n, "Generator expression must be parenthesized "
|
|
"if not sole argument");
|
|
return NULL;
|
|
}
|
|
|
|
if (nargs + nkeywords + ngens > 255) {
|
|
ast_error(n, "more than 255 arguments");
|
|
return NULL;
|
|
}
|
|
|
|
args = asdl_seq_new(nargs + ngens, c->c_arena);
|
|
if (!args)
|
|
return NULL;
|
|
keywords = asdl_seq_new(nkeywords, c->c_arena);
|
|
if (!keywords)
|
|
return NULL;
|
|
nargs = 0;
|
|
nkeywords = 0;
|
|
for (i = 0; i < NCH(n); i++) {
|
|
node *ch = CHILD(n, i);
|
|
if (TYPE(ch) == argument) {
|
|
expr_ty e;
|
|
if (NCH(ch) == 1) {
|
|
if (nkeywords) {
|
|
ast_error(CHILD(ch, 0),
|
|
"non-keyword arg after keyword arg");
|
|
return NULL;
|
|
}
|
|
e = ast_for_expr(c, CHILD(ch, 0));
|
|
if (!e)
|
|
return NULL;
|
|
asdl_seq_SET(args, nargs++, e);
|
|
}
|
|
else if (TYPE(CHILD(ch, 1)) == comp_for) {
|
|
e = ast_for_genexp(c, ch);
|
|
if (!e)
|
|
return NULL;
|
|
asdl_seq_SET(args, nargs++, e);
|
|
}
|
|
else {
|
|
keyword_ty kw;
|
|
identifier key;
|
|
|
|
/* CHILD(ch, 0) is test, but must be an identifier? */
|
|
e = ast_for_expr(c, CHILD(ch, 0));
|
|
if (!e)
|
|
return NULL;
|
|
/* f(lambda x: x[0] = 3) ends up getting parsed with
|
|
* LHS test = lambda x: x[0], and RHS test = 3.
|
|
* SF bug 132313 points out that complaining about a keyword
|
|
* then is very confusing.
|
|
*/
|
|
if (e->kind == Lambda_kind) {
|
|
ast_error(CHILD(ch, 0), "lambda cannot contain assignment");
|
|
return NULL;
|
|
} else if (e->kind != Name_kind) {
|
|
ast_error(CHILD(ch, 0), "keyword can't be an expression");
|
|
return NULL;
|
|
} else if (forbidden_name(e, ch)) {
|
|
return NULL;
|
|
}
|
|
key = e->v.Name.id;
|
|
e = ast_for_expr(c, CHILD(ch, 2));
|
|
if (!e)
|
|
return NULL;
|
|
kw = keyword(key, e, c->c_arena);
|
|
if (!kw)
|
|
return NULL;
|
|
asdl_seq_SET(keywords, nkeywords++, kw);
|
|
}
|
|
}
|
|
else if (TYPE(ch) == STAR) {
|
|
vararg = ast_for_expr(c, CHILD(n, i+1));
|
|
i++;
|
|
}
|
|
else if (TYPE(ch) == DOUBLESTAR) {
|
|
kwarg = ast_for_expr(c, CHILD(n, i+1));
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_testlist(struct compiling *c, const node* n)
|
|
{
|
|
/* testlist_comp: test (comp_for | (',' test)* [',']) */
|
|
/* testlist: test (',' test)* [','] */
|
|
/* testlist1: test (',' test)* */
|
|
assert(NCH(n) > 0);
|
|
if (TYPE(n) == testlist_comp) {
|
|
if (NCH(n) > 1)
|
|
assert(TYPE(CHILD(n, 1)) != comp_for);
|
|
}
|
|
else {
|
|
assert(TYPE(n) == testlist ||
|
|
TYPE(n) == testlist1);
|
|
}
|
|
if (NCH(n) == 1)
|
|
return ast_for_expr(c, CHILD(n, 0));
|
|
else {
|
|
asdl_seq *tmp = seq_for_testlist(c, n);
|
|
if (!tmp)
|
|
return NULL;
|
|
return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_expr_stmt(struct compiling *c, const node *n)
|
|
{
|
|
REQ(n, expr_stmt);
|
|
/* expr_stmt: testlist (augassign (yield_expr|testlist)
|
|
| ('=' (yield_expr|testlist))*)
|
|
testlist: test (',' test)* [',']
|
|
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^='
|
|
| '<<=' | '>>=' | '**=' | '//='
|
|
test: ... here starts the operator precendence dance
|
|
*/
|
|
|
|
if (NCH(n) == 1) {
|
|
expr_ty e = ast_for_testlist(c, CHILD(n, 0));
|
|
if (!e)
|
|
return NULL;
|
|
|
|
return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else if (TYPE(CHILD(n, 1)) == augassign) {
|
|
expr_ty expr1, expr2;
|
|
operator_ty newoperator;
|
|
node *ch = CHILD(n, 0);
|
|
|
|
expr1 = ast_for_testlist(c, ch);
|
|
if (!expr1)
|
|
return NULL;
|
|
/* TODO(nas): Remove duplicated error checks (set_context does it) */
|
|
switch (expr1->kind) {
|
|
case GeneratorExp_kind:
|
|
ast_error(ch, "augmented assignment to generator "
|
|
"expression not possible");
|
|
return NULL;
|
|
case Yield_kind:
|
|
ast_error(ch, "augmented assignment to yield "
|
|
"expression not possible");
|
|
return NULL;
|
|
case Name_kind: {
|
|
if (forbidden_name(expr1, ch))
|
|
return NULL;
|
|
break;
|
|
}
|
|
case Attribute_kind:
|
|
case Subscript_kind:
|
|
break;
|
|
default:
|
|
ast_error(ch, "illegal expression for augmented "
|
|
"assignment");
|
|
return NULL;
|
|
}
|
|
set_context(expr1, Store, ch);
|
|
|
|
ch = CHILD(n, 2);
|
|
if (TYPE(ch) == testlist)
|
|
expr2 = ast_for_testlist(c, ch);
|
|
else
|
|
expr2 = ast_for_expr(c, ch);
|
|
if (!expr2)
|
|
return NULL;
|
|
|
|
newoperator = ast_for_augassign(CHILD(n, 1));
|
|
if (!newoperator)
|
|
return NULL;
|
|
|
|
return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else {
|
|
int i;
|
|
asdl_seq *targets;
|
|
node *value;
|
|
expr_ty expression;
|
|
|
|
/* a normal assignment */
|
|
REQ(CHILD(n, 1), EQUAL);
|
|
targets = asdl_seq_new(NCH(n) / 2, c->c_arena);
|
|
if (!targets)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n) - 2; i += 2) {
|
|
expr_ty e;
|
|
node *ch = CHILD(n, i);
|
|
if (TYPE(ch) == yield_expr) {
|
|
ast_error(ch, "assignment to yield expression not possible");
|
|
return NULL;
|
|
}
|
|
e = ast_for_testlist(c, ch);
|
|
|
|
/* set context to assign */
|
|
if (!e)
|
|
return NULL;
|
|
|
|
if (!set_context(e, Store, CHILD(n, i)))
|
|
return NULL;
|
|
|
|
asdl_seq_SET(targets, i / 2, e);
|
|
}
|
|
value = CHILD(n, NCH(n) - 1);
|
|
if (TYPE(value) == testlist)
|
|
expression = ast_for_testlist(c, value);
|
|
else
|
|
expression = ast_for_expr(c, value);
|
|
if (!expression)
|
|
return NULL;
|
|
return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
}
|
|
|
|
static asdl_seq *
|
|
ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
|
|
{
|
|
asdl_seq *seq;
|
|
int i;
|
|
expr_ty e;
|
|
|
|
REQ(n, exprlist);
|
|
|
|
seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
|
if (!seq)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
e = ast_for_expr(c, CHILD(n, i));
|
|
if (!e)
|
|
return NULL;
|
|
asdl_seq_SET(seq, i / 2, e);
|
|
if (context && !set_context(e, context, CHILD(n, i)))
|
|
return NULL;
|
|
}
|
|
return seq;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_del_stmt(struct compiling *c, const node *n)
|
|
{
|
|
asdl_seq *expr_list;
|
|
|
|
/* del_stmt: 'del' exprlist */
|
|
REQ(n, del_stmt);
|
|
|
|
expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
|
|
if (!expr_list)
|
|
return NULL;
|
|
return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_flow_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/*
|
|
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
|
|
| yield_stmt
|
|
break_stmt: 'break'
|
|
continue_stmt: 'continue'
|
|
return_stmt: 'return' [testlist]
|
|
yield_stmt: yield_expr
|
|
yield_expr: 'yield' testlist
|
|
raise_stmt: 'raise' [test [',' test [',' test]]]
|
|
*/
|
|
node *ch;
|
|
|
|
REQ(n, flow_stmt);
|
|
ch = CHILD(n, 0);
|
|
switch (TYPE(ch)) {
|
|
case break_stmt:
|
|
return Break(LINENO(n), n->n_col_offset, c->c_arena);
|
|
case continue_stmt:
|
|
return Continue(LINENO(n), n->n_col_offset, c->c_arena);
|
|
case yield_stmt: { /* will reduce to yield_expr */
|
|
expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
|
|
if (!exp)
|
|
return NULL;
|
|
return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
case return_stmt:
|
|
if (NCH(ch) == 1)
|
|
return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else {
|
|
expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
case raise_stmt:
|
|
if (NCH(ch) == 1)
|
|
return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
|
|
else if (NCH(ch) >= 2) {
|
|
expr_ty cause = NULL;
|
|
expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
if (NCH(ch) == 4) {
|
|
cause = ast_for_expr(c, CHILD(ch, 3));
|
|
if (!cause)
|
|
return NULL;
|
|
}
|
|
return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unexpected flow_stmt: %d", TYPE(ch));
|
|
return NULL;
|
|
}
|
|
|
|
PyErr_SetString(PyExc_SystemError, "unhandled flow statement");
|
|
return NULL;
|
|
}
|
|
|
|
static alias_ty
|
|
alias_for_import_name(struct compiling *c, const node *n)
|
|
{
|
|
/*
|
|
import_as_name: NAME ['as' NAME]
|
|
dotted_as_name: dotted_name ['as' NAME]
|
|
dotted_name: NAME ('.' NAME)*
|
|
*/
|
|
PyObject *str;
|
|
|
|
loop:
|
|
switch (TYPE(n)) {
|
|
case import_as_name:
|
|
str = NULL;
|
|
if (NCH(n) == 3) {
|
|
str = NEW_IDENTIFIER(CHILD(n, 2));
|
|
}
|
|
return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
|
|
case dotted_as_name:
|
|
if (NCH(n) == 1) {
|
|
n = CHILD(n, 0);
|
|
goto loop;
|
|
}
|
|
else {
|
|
alias_ty a = alias_for_import_name(c, CHILD(n, 0));
|
|
if (!a)
|
|
return NULL;
|
|
assert(!a->asname);
|
|
a->asname = NEW_IDENTIFIER(CHILD(n, 2));
|
|
return a;
|
|
}
|
|
break;
|
|
case dotted_name:
|
|
if (NCH(n) == 1)
|
|
return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena);
|
|
else {
|
|
/* Create a string of the form "a.b.c" */
|
|
int i;
|
|
size_t len;
|
|
char *s;
|
|
PyObject *uni;
|
|
|
|
len = 0;
|
|
for (i = 0; i < NCH(n); i += 2)
|
|
/* length of string plus one for the dot */
|
|
len += strlen(STR(CHILD(n, i))) + 1;
|
|
len--; /* the last name doesn't have a dot */
|
|
str = PyString_FromStringAndSize(NULL, len);
|
|
if (!str)
|
|
return NULL;
|
|
s = PyString_AS_STRING(str);
|
|
if (!s)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
char *sch = STR(CHILD(n, i));
|
|
strcpy(s, STR(CHILD(n, i)));
|
|
s += strlen(sch);
|
|
*s++ = '.';
|
|
}
|
|
--s;
|
|
*s = '\0';
|
|
uni = PyUnicode_DecodeUTF8(PyString_AS_STRING(str),
|
|
PyString_GET_SIZE(str),
|
|
NULL);
|
|
Py_DECREF(str);
|
|
if (!uni)
|
|
return NULL;
|
|
str = uni;
|
|
PyUnicode_InternInPlace(&str);
|
|
PyArena_AddPyObject(c->c_arena, str);
|
|
return alias(str, NULL, c->c_arena);
|
|
}
|
|
break;
|
|
case STAR:
|
|
str = PyUnicode_InternFromString("*");
|
|
PyArena_AddPyObject(c->c_arena, str);
|
|
return alias(str, NULL, c->c_arena);
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unexpected import name: %d", TYPE(n));
|
|
return NULL;
|
|
}
|
|
|
|
PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
|
|
return NULL;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_import_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/*
|
|
import_stmt: import_name | import_from
|
|
import_name: 'import' dotted_as_names
|
|
import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
|
|
'import' ('*' | '(' import_as_names ')' | import_as_names)
|
|
*/
|
|
int lineno;
|
|
int col_offset;
|
|
int i;
|
|
asdl_seq *aliases;
|
|
|
|
REQ(n, import_stmt);
|
|
lineno = LINENO(n);
|
|
col_offset = n->n_col_offset;
|
|
n = CHILD(n, 0);
|
|
if (TYPE(n) == import_name) {
|
|
n = CHILD(n, 1);
|
|
REQ(n, dotted_as_names);
|
|
aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
|
|
if (!aliases)
|
|
return NULL;
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
|
|
if (!import_alias)
|
|
return NULL;
|
|
asdl_seq_SET(aliases, i / 2, import_alias);
|
|
}
|
|
return Import(aliases, lineno, col_offset, c->c_arena);
|
|
}
|
|
else if (TYPE(n) == import_from) {
|
|
int n_children;
|
|
int idx, ndots = 0;
|
|
alias_ty mod = NULL;
|
|
identifier modname;
|
|
|
|
/* Count the number of dots (for relative imports) and check for the
|
|
optional module name */
|
|
for (idx = 1; idx < NCH(n); idx++) {
|
|
if (TYPE(CHILD(n, idx)) == dotted_name) {
|
|
mod = alias_for_import_name(c, CHILD(n, idx));
|
|
idx++;
|
|
break;
|
|
} else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
|
|
/* three consecutive dots are tokenized as one ELLIPSIS */
|
|
ndots += 3;
|
|
continue;
|
|
} else if (TYPE(CHILD(n, idx)) != DOT) {
|
|
break;
|
|
}
|
|
ndots++;
|
|
}
|
|
idx++; /* skip over the 'import' keyword */
|
|
switch (TYPE(CHILD(n, idx))) {
|
|
case STAR:
|
|
/* from ... import * */
|
|
n = CHILD(n, idx);
|
|
n_children = 1;
|
|
if (ndots) {
|
|
ast_error(n, "'import *' not allowed with 'from .'");
|
|
return NULL;
|
|
}
|
|
break;
|
|
case LPAR:
|
|
/* from ... import (x, y, z) */
|
|
n = CHILD(n, idx + 1);
|
|
n_children = NCH(n);
|
|
break;
|
|
case import_as_names:
|
|
/* from ... import x, y, z */
|
|
n = CHILD(n, idx);
|
|
n_children = NCH(n);
|
|
if (n_children % 2 == 0) {
|
|
ast_error(n, "trailing comma not allowed without"
|
|
" surrounding parentheses");
|
|
return NULL;
|
|
}
|
|
break;
|
|
default:
|
|
ast_error(n, "Unexpected node-type in from-import");
|
|
return NULL;
|
|
}
|
|
|
|
aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
|
|
if (!aliases)
|
|
return NULL;
|
|
|
|
/* handle "from ... import *" special b/c there's no children */
|
|
if (TYPE(n) == STAR) {
|
|
alias_ty import_alias = alias_for_import_name(c, n);
|
|
if (!import_alias)
|
|
return NULL;
|
|
asdl_seq_SET(aliases, 0, import_alias);
|
|
}
|
|
else {
|
|
for (i = 0; i < NCH(n); i += 2) {
|
|
alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
|
|
if (!import_alias)
|
|
return NULL;
|
|
asdl_seq_SET(aliases, i / 2, import_alias);
|
|
}
|
|
}
|
|
if (mod != NULL)
|
|
modname = mod->name;
|
|
else
|
|
modname = new_identifier("", c->c_arena);
|
|
return ImportFrom(modname, aliases, ndots, lineno, col_offset,
|
|
c->c_arena);
|
|
}
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unknown import statement: starts with command '%s'",
|
|
STR(CHILD(n, 0)));
|
|
return NULL;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_global_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/* global_stmt: 'global' NAME (',' NAME)* */
|
|
identifier name;
|
|
asdl_seq *s;
|
|
int i;
|
|
|
|
REQ(n, global_stmt);
|
|
s = asdl_seq_new(NCH(n) / 2, c->c_arena);
|
|
if (!s)
|
|
return NULL;
|
|
for (i = 1; i < NCH(n); i += 2) {
|
|
name = NEW_IDENTIFIER(CHILD(n, i));
|
|
if (!name)
|
|
return NULL;
|
|
asdl_seq_SET(s, i / 2, name);
|
|
}
|
|
return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_nonlocal_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
|
|
identifier name;
|
|
asdl_seq *s;
|
|
int i;
|
|
|
|
REQ(n, nonlocal_stmt);
|
|
s = asdl_seq_new(NCH(n) / 2, c->c_arena);
|
|
if (!s)
|
|
return NULL;
|
|
for (i = 1; i < NCH(n); i += 2) {
|
|
name = NEW_IDENTIFIER(CHILD(n, i));
|
|
if (!name)
|
|
return NULL;
|
|
asdl_seq_SET(s, i / 2, name);
|
|
}
|
|
return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_assert_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/* assert_stmt: 'assert' test [',' test] */
|
|
REQ(n, assert_stmt);
|
|
if (NCH(n) == 2) {
|
|
expr_ty expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else if (NCH(n) == 4) {
|
|
expr_ty expr1, expr2;
|
|
|
|
expr1 = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expr1)
|
|
return NULL;
|
|
expr2 = ast_for_expr(c, CHILD(n, 3));
|
|
if (!expr2)
|
|
return NULL;
|
|
|
|
return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
PyErr_Format(PyExc_SystemError,
|
|
"improper number of parts to 'assert' statement: %d",
|
|
NCH(n));
|
|
return NULL;
|
|
}
|
|
|
|
static asdl_seq *
|
|
ast_for_suite(struct compiling *c, const node *n)
|
|
{
|
|
/* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
|
|
asdl_seq *seq;
|
|
stmt_ty s;
|
|
int i, total, num, end, pos = 0;
|
|
node *ch;
|
|
|
|
REQ(n, suite);
|
|
|
|
total = num_stmts(n);
|
|
seq = asdl_seq_new(total, c->c_arena);
|
|
if (!seq)
|
|
return NULL;
|
|
if (TYPE(CHILD(n, 0)) == simple_stmt) {
|
|
n = CHILD(n, 0);
|
|
/* simple_stmt always ends with a NEWLINE,
|
|
and may have a trailing SEMI
|
|
*/
|
|
end = NCH(n) - 1;
|
|
if (TYPE(CHILD(n, end - 1)) == SEMI)
|
|
end--;
|
|
/* loop by 2 to skip semi-colons */
|
|
for (i = 0; i < end; i += 2) {
|
|
ch = CHILD(n, i);
|
|
s = ast_for_stmt(c, ch);
|
|
if (!s)
|
|
return NULL;
|
|
asdl_seq_SET(seq, pos++, s);
|
|
}
|
|
}
|
|
else {
|
|
for (i = 2; i < (NCH(n) - 1); i++) {
|
|
ch = CHILD(n, i);
|
|
REQ(ch, stmt);
|
|
num = num_stmts(ch);
|
|
if (num == 1) {
|
|
/* small_stmt or compound_stmt with only one child */
|
|
s = ast_for_stmt(c, ch);
|
|
if (!s)
|
|
return NULL;
|
|
asdl_seq_SET(seq, pos++, s);
|
|
}
|
|
else {
|
|
int j;
|
|
ch = CHILD(ch, 0);
|
|
REQ(ch, simple_stmt);
|
|
for (j = 0; j < NCH(ch); j += 2) {
|
|
/* statement terminates with a semi-colon ';' */
|
|
if (NCH(CHILD(ch, j)) == 0) {
|
|
assert((j + 1) == NCH(ch));
|
|
break;
|
|
}
|
|
s = ast_for_stmt(c, CHILD(ch, j));
|
|
if (!s)
|
|
return NULL;
|
|
asdl_seq_SET(seq, pos++, s);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert(pos == seq->size);
|
|
return seq;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_if_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
|
|
['else' ':' suite]
|
|
*/
|
|
char *s;
|
|
|
|
REQ(n, if_stmt);
|
|
|
|
if (NCH(n) == 4) {
|
|
expr_ty expression;
|
|
asdl_seq *suite_seq;
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, 3));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
|
|
s = STR(CHILD(n, 4));
|
|
/* s[2], the third character in the string, will be
|
|
's' for el_s_e, or
|
|
'i' for el_i_f
|
|
*/
|
|
if (s[2] == 's') {
|
|
expr_ty expression;
|
|
asdl_seq *seq1, *seq2;
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
seq1 = ast_for_suite(c, CHILD(n, 3));
|
|
if (!seq1)
|
|
return NULL;
|
|
seq2 = ast_for_suite(c, CHILD(n, 6));
|
|
if (!seq2)
|
|
return NULL;
|
|
|
|
return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
else if (s[2] == 'i') {
|
|
int i, n_elif, has_else = 0;
|
|
expr_ty expression;
|
|
asdl_seq *suite_seq;
|
|
asdl_seq *orelse = NULL;
|
|
n_elif = NCH(n) - 4;
|
|
/* must reference the child n_elif+1 since 'else' token is third,
|
|
not fourth, child from the end. */
|
|
if (TYPE(CHILD(n, (n_elif + 1))) == NAME
|
|
&& STR(CHILD(n, (n_elif + 1)))[2] == 's') {
|
|
has_else = 1;
|
|
n_elif -= 3;
|
|
}
|
|
n_elif /= 4;
|
|
|
|
if (has_else) {
|
|
asdl_seq *suite_seq2;
|
|
|
|
orelse = asdl_seq_new(1, c->c_arena);
|
|
if (!orelse)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
|
|
if (!suite_seq2)
|
|
return NULL;
|
|
|
|
asdl_seq_SET(orelse, 0,
|
|
If(expression, suite_seq, suite_seq2,
|
|
LINENO(CHILD(n, NCH(n) - 6)),
|
|
CHILD(n, NCH(n) - 6)->n_col_offset,
|
|
c->c_arena));
|
|
/* the just-created orelse handled the last elif */
|
|
n_elif--;
|
|
}
|
|
|
|
for (i = 0; i < n_elif; i++) {
|
|
int off = 5 + (n_elif - i - 1) * 4;
|
|
asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
|
|
if (!newobj)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(n, off));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, off + 2));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
asdl_seq_SET(newobj, 0,
|
|
If(expression, suite_seq, orelse,
|
|
LINENO(CHILD(n, off)),
|
|
CHILD(n, off)->n_col_offset, c->c_arena));
|
|
orelse = newobj;
|
|
}
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, 3));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
return If(expression, suite_seq, orelse,
|
|
LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unexpected token in 'if' statement: %s", s);
|
|
return NULL;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_while_stmt(struct compiling *c, const node *n)
|
|
{
|
|
/* while_stmt: 'while' test ':' suite ['else' ':' suite] */
|
|
REQ(n, while_stmt);
|
|
|
|
if (NCH(n) == 4) {
|
|
expr_ty expression;
|
|
asdl_seq *suite_seq;
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, 3));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
else if (NCH(n) == 7) {
|
|
expr_ty expression;
|
|
asdl_seq *seq1, *seq2;
|
|
|
|
expression = ast_for_expr(c, CHILD(n, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
seq1 = ast_for_suite(c, CHILD(n, 3));
|
|
if (!seq1)
|
|
return NULL;
|
|
seq2 = ast_for_suite(c, CHILD(n, 6));
|
|
if (!seq2)
|
|
return NULL;
|
|
|
|
return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
"wrong number of tokens for 'while' statement: %d",
|
|
NCH(n));
|
|
return NULL;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_for_stmt(struct compiling *c, const node *n)
|
|
{
|
|
asdl_seq *_target, *seq = NULL, *suite_seq;
|
|
expr_ty expression;
|
|
expr_ty target;
|
|
const node *node_target;
|
|
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
|
|
REQ(n, for_stmt);
|
|
|
|
if (NCH(n) == 9) {
|
|
seq = ast_for_suite(c, CHILD(n, 8));
|
|
if (!seq)
|
|
return NULL;
|
|
}
|
|
|
|
node_target = CHILD(n, 1);
|
|
_target = ast_for_exprlist(c, node_target, Store);
|
|
if (!_target)
|
|
return NULL;
|
|
/* Check the # of children rather than the length of _target, since
|
|
for x, in ... has 1 element in _target, but still requires a Tuple. */
|
|
if (NCH(node_target) == 1)
|
|
target = (expr_ty)asdl_seq_GET(_target, 0);
|
|
else
|
|
target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
|
|
|
|
expression = ast_for_testlist(c, CHILD(n, 3));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, CHILD(n, 5));
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
|
|
c->c_arena);
|
|
}
|
|
|
|
static excepthandler_ty
|
|
ast_for_except_clause(struct compiling *c, const node *exc, node *body)
|
|
{
|
|
/* except_clause: 'except' [test ['as' test]] */
|
|
REQ(exc, except_clause);
|
|
REQ(body, suite);
|
|
|
|
if (NCH(exc) == 1) {
|
|
asdl_seq *suite_seq = ast_for_suite(c, body);
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
|
|
exc->n_col_offset, c->c_arena);
|
|
}
|
|
else if (NCH(exc) == 2) {
|
|
expr_ty expression;
|
|
asdl_seq *suite_seq;
|
|
|
|
expression = ast_for_expr(c, CHILD(exc, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, body);
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
return excepthandler(expression, NULL, suite_seq, LINENO(exc),
|
|
exc->n_col_offset, c->c_arena);
|
|
}
|
|
else if (NCH(exc) == 4) {
|
|
asdl_seq *suite_seq;
|
|
expr_ty expression;
|
|
identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
|
|
if (!e)
|
|
return NULL;
|
|
expression = ast_for_expr(c, CHILD(exc, 1));
|
|
if (!expression)
|
|
return NULL;
|
|
suite_seq = ast_for_suite(c, body);
|
|
if (!suite_seq)
|
|
return NULL;
|
|
|
|
return excepthandler(expression, e, suite_seq, LINENO(exc),
|
|
exc->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
"wrong number of children for 'except' clause: %d",
|
|
NCH(exc));
|
|
return NULL;
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_try_stmt(struct compiling *c, const node *n)
|
|
{
|
|
const int nch = NCH(n);
|
|
int n_except = (nch - 3)/3;
|
|
asdl_seq *body, *orelse = NULL, *finally = NULL;
|
|
|
|
REQ(n, try_stmt);
|
|
|
|
body = ast_for_suite(c, CHILD(n, 2));
|
|
if (body == NULL)
|
|
return NULL;
|
|
|
|
if (TYPE(CHILD(n, nch - 3)) == NAME) {
|
|
if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
|
|
if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
|
|
/* we can assume it's an "else",
|
|
because nch >= 9 for try-else-finally and
|
|
it would otherwise have a type of except_clause */
|
|
orelse = ast_for_suite(c, CHILD(n, nch - 4));
|
|
if (orelse == NULL)
|
|
return NULL;
|
|
n_except--;
|
|
}
|
|
|
|
finally = ast_for_suite(c, CHILD(n, nch - 1));
|
|
if (finally == NULL)
|
|
return NULL;
|
|
n_except--;
|
|
}
|
|
else {
|
|
/* we can assume it's an "else",
|
|
otherwise it would have a type of except_clause */
|
|
orelse = ast_for_suite(c, CHILD(n, nch - 1));
|
|
if (orelse == NULL)
|
|
return NULL;
|
|
n_except--;
|
|
}
|
|
}
|
|
else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
|
|
ast_error(n, "malformed 'try' statement");
|
|
return NULL;
|
|
}
|
|
|
|
if (n_except > 0) {
|
|
int i;
|
|
stmt_ty except_st;
|
|
/* process except statements to create a try ... except */
|
|
asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena);
|
|
if (handlers == NULL)
|
|
return NULL;
|
|
|
|
for (i = 0; i < n_except; i++) {
|
|
excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
|
|
CHILD(n, 5 + i * 3));
|
|
if (!e)
|
|
return NULL;
|
|
asdl_seq_SET(handlers, i, e);
|
|
}
|
|
|
|
except_st = TryExcept(body, handlers, orelse, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
if (!finally)
|
|
return except_st;
|
|
|
|
/* if a 'finally' is present too, we nest the TryExcept within a
|
|
TryFinally to emulate try ... except ... finally */
|
|
body = asdl_seq_new(1, c->c_arena);
|
|
if (body == NULL)
|
|
return NULL;
|
|
asdl_seq_SET(body, 0, except_st);
|
|
}
|
|
|
|
/* must be a try ... finally (except clauses are in body, if any exist) */
|
|
assert(finally != NULL);
|
|
return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static expr_ty
|
|
ast_for_with_var(struct compiling *c, const node *n)
|
|
{
|
|
REQ(n, with_var);
|
|
return ast_for_expr(c, CHILD(n, 1));
|
|
}
|
|
|
|
/* with_stmt: 'with' test [ with_var ] ':' suite */
|
|
static stmt_ty
|
|
ast_for_with_stmt(struct compiling *c, const node *n)
|
|
{
|
|
expr_ty context_expr, optional_vars = NULL;
|
|
int suite_index = 3; /* skip 'with', test, and ':' */
|
|
asdl_seq *suite_seq;
|
|
|
|
assert(TYPE(n) == with_stmt);
|
|
context_expr = ast_for_expr(c, CHILD(n, 1));
|
|
if (TYPE(CHILD(n, 2)) == with_var) {
|
|
optional_vars = ast_for_with_var(c, CHILD(n, 2));
|
|
|
|
if (!optional_vars) {
|
|
return NULL;
|
|
}
|
|
if (!set_context(optional_vars, Store, n)) {
|
|
return NULL;
|
|
}
|
|
suite_index = 4;
|
|
}
|
|
|
|
suite_seq = ast_for_suite(c, CHILD(n, suite_index));
|
|
if (!suite_seq) {
|
|
return NULL;
|
|
}
|
|
return With(context_expr, optional_vars, suite_seq, LINENO(n),
|
|
n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
|
|
{
|
|
/* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
|
|
asdl_seq *s;
|
|
expr_ty call, dummy;
|
|
|
|
REQ(n, classdef);
|
|
|
|
if (NCH(n) == 4) { /* class NAME ':' suite */
|
|
s = ast_for_suite(c, CHILD(n, 3));
|
|
if (!s)
|
|
return NULL;
|
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
|
|
decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
|
|
s = ast_for_suite(c, CHILD(n,5));
|
|
if (!s)
|
|
return NULL;
|
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s,
|
|
decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
/* class NAME '(' arglist ')' ':' suite */
|
|
/* build up a fake Call node so we can extract its pieces */
|
|
dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena);
|
|
call = ast_for_call(c, CHILD(n, 3), dummy);
|
|
if (!call)
|
|
return NULL;
|
|
s = ast_for_suite(c, CHILD(n, 6));
|
|
if (!s)
|
|
return NULL;
|
|
|
|
return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)),
|
|
call->v.Call.args, call->v.Call.keywords,
|
|
call->v.Call.starargs, call->v.Call.kwargs, s,
|
|
decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
|
|
}
|
|
|
|
static stmt_ty
|
|
ast_for_stmt(struct compiling *c, const node *n)
|
|
{
|
|
if (TYPE(n) == stmt) {
|
|
assert(NCH(n) == 1);
|
|
n = CHILD(n, 0);
|
|
}
|
|
if (TYPE(n) == simple_stmt) {
|
|
assert(num_stmts(n) == 1);
|
|
n = CHILD(n, 0);
|
|
}
|
|
if (TYPE(n) == small_stmt) {
|
|
REQ(n, small_stmt);
|
|
n = CHILD(n, 0);
|
|
/* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
|
|
| import_stmt | global_stmt | nonlocal_stmt | assert_stmt
|
|
*/
|
|
switch (TYPE(n)) {
|
|
case expr_stmt:
|
|
return ast_for_expr_stmt(c, n);
|
|
case del_stmt:
|
|
return ast_for_del_stmt(c, n);
|
|
case pass_stmt:
|
|
return Pass(LINENO(n), n->n_col_offset, c->c_arena);
|
|
case flow_stmt:
|
|
return ast_for_flow_stmt(c, n);
|
|
case import_stmt:
|
|
return ast_for_import_stmt(c, n);
|
|
case global_stmt:
|
|
return ast_for_global_stmt(c, n);
|
|
case nonlocal_stmt:
|
|
return ast_for_nonlocal_stmt(c, n);
|
|
case assert_stmt:
|
|
return ast_for_assert_stmt(c, n);
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unhandled small_stmt: TYPE=%d NCH=%d\n",
|
|
TYPE(n), NCH(n));
|
|
return NULL;
|
|
}
|
|
}
|
|
else {
|
|
/* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
|
|
| funcdef | classdef | decorated
|
|
*/
|
|
node *ch = CHILD(n, 0);
|
|
REQ(n, compound_stmt);
|
|
switch (TYPE(ch)) {
|
|
case if_stmt:
|
|
return ast_for_if_stmt(c, ch);
|
|
case while_stmt:
|
|
return ast_for_while_stmt(c, ch);
|
|
case for_stmt:
|
|
return ast_for_for_stmt(c, ch);
|
|
case try_stmt:
|
|
return ast_for_try_stmt(c, ch);
|
|
case with_stmt:
|
|
return ast_for_with_stmt(c, ch);
|
|
case funcdef:
|
|
return ast_for_funcdef(c, ch, NULL);
|
|
case classdef:
|
|
return ast_for_classdef(c, ch, NULL);
|
|
case decorated:
|
|
return ast_for_decorated(c, ch);
|
|
default:
|
|
PyErr_Format(PyExc_SystemError,
|
|
"unhandled small_stmt: TYPE=%d NCH=%d\n",
|
|
TYPE(n), NCH(n));
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
static PyObject *
|
|
parsenumber(const char *s)
|
|
{
|
|
const char *end;
|
|
long x;
|
|
double dx;
|
|
#ifndef WITHOUT_COMPLEX
|
|
Py_complex c;
|
|
int imflag;
|
|
#endif
|
|
|
|
errno = 0;
|
|
end = s + strlen(s) - 1;
|
|
#ifndef WITHOUT_COMPLEX
|
|
imflag = *end == 'j' || *end == 'J';
|
|
#endif
|
|
if (s[0] == '0') {
|
|
x = (long) PyOS_strtoul((char *)s, (char **)&end, 0);
|
|
if (x < 0 && errno == 0) {
|
|
return PyLong_FromString((char *)s,
|
|
(char **)0,
|
|
0);
|
|
}
|
|
}
|
|
else
|
|
x = PyOS_strtol((char *)s, (char **)&end, 0);
|
|
if (*end == '\0') {
|
|
if (errno != 0)
|
|
return PyLong_FromString((char *)s, (char **)0, 0);
|
|
return PyInt_FromLong(x);
|
|
}
|
|
/* XXX Huge floats may silently fail */
|
|
#ifndef WITHOUT_COMPLEX
|
|
if (imflag) {
|
|
c.real = 0.;
|
|
PyFPE_START_PROTECT("atof", return 0)
|
|
c.imag = PyOS_ascii_atof(s);
|
|
PyFPE_END_PROTECT(c)
|
|
return PyComplex_FromCComplex(c);
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
PyFPE_START_PROTECT("atof", return 0)
|
|
dx = PyOS_ascii_atof(s);
|
|
PyFPE_END_PROTECT(dx)
|
|
return PyFloat_FromDouble(dx);
|
|
}
|
|
}
|
|
|
|
static PyObject *
|
|
decode_utf8(const char **sPtr, const char *end, char* encoding)
|
|
{
|
|
PyObject *u, *v;
|
|
char *s, *t;
|
|
t = s = (char *)*sPtr;
|
|
/* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
|
|
while (s < end && (*s & 0x80)) s++;
|
|
*sPtr = s;
|
|
u = PyUnicode_DecodeUTF8(t, s - t, NULL);
|
|
if (u == NULL)
|
|
return NULL;
|
|
v = PyUnicode_AsEncodedString(u, encoding, NULL);
|
|
Py_DECREF(u);
|
|
return v;
|
|
}
|
|
|
|
static PyObject *
|
|
decode_unicode(const char *s, size_t len, int rawmode, const char *encoding)
|
|
{
|
|
PyObject *v, *u;
|
|
char *buf;
|
|
char *p;
|
|
const char *end;
|
|
if (encoding == NULL) {
|
|
buf = (char *)s;
|
|
u = NULL;
|
|
} else if (strcmp(encoding, "iso-8859-1") == 0) {
|
|
buf = (char *)s;
|
|
u = NULL;
|
|
} else {
|
|
/* "\XX" may become "\u005c\uHHLL" (12 bytes) */
|
|
u = PyString_FromStringAndSize((char *)NULL, len * 4);
|
|
if (u == NULL)
|
|
return NULL;
|
|
p = buf = PyString_AsString(u);
|
|
end = s + len;
|
|
while (s < end) {
|
|
if (*s == '\\') {
|
|
*p++ = *s++;
|
|
if (*s & 0x80) {
|
|
strcpy(p, "u005c");
|
|
p += 5;
|
|
}
|
|
}
|
|
if (*s & 0x80) { /* XXX inefficient */
|
|
PyObject *w;
|
|
char *r;
|
|
Py_ssize_t rn, i;
|
|
w = decode_utf8(&s, end, "utf-16-be");
|
|
if (w == NULL) {
|
|
Py_DECREF(u);
|
|
return NULL;
|
|
}
|
|
assert(PyBytes_Check(w));
|
|
r = PyBytes_AsString(w);
|
|
rn = PyBytes_Size(w);
|
|
assert(rn % 2 == 0);
|
|
for (i = 0; i < rn; i += 2) {
|
|
sprintf(p, "\\u%02x%02x",
|
|
r[i + 0] & 0xFF,
|
|
r[i + 1] & 0xFF);
|
|
p += 6;
|
|
}
|
|
Py_DECREF(w);
|
|
} else {
|
|
*p++ = *s++;
|
|
}
|
|
}
|
|
len = p - buf;
|
|
s = buf;
|
|
}
|
|
if (rawmode)
|
|
v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL);
|
|
else
|
|
v = PyUnicode_DecodeUnicodeEscape(s, len, NULL);
|
|
Py_XDECREF(u);
|
|
return v;
|
|
}
|
|
|
|
/* s is a Python string literal, including the bracketing quote characters,
|
|
* and r &/or u prefixes (if any), and embedded escape sequences (if any).
|
|
* parsestr parses it, and returns the decoded Python string object.
|
|
*/
|
|
static PyObject *
|
|
parsestr(const node *n, const char *encoding, int *bytesmode)
|
|
{
|
|
size_t len;
|
|
const char *s = STR(n);
|
|
int quote = Py_CHARMASK(*s);
|
|
int rawmode = 0;
|
|
int need_encoding;
|
|
|
|
if (isalpha(quote) || quote == '_') {
|
|
if (quote == 'b' || quote == 'B') {
|
|
quote = *++s;
|
|
*bytesmode = 1;
|
|
}
|
|
if (quote == 'r' || quote == 'R') {
|
|
quote = *++s;
|
|
rawmode = 1;
|
|
}
|
|
}
|
|
if (quote != '\'' && quote != '\"') {
|
|
PyErr_BadInternalCall();
|
|
return NULL;
|
|
}
|
|
s++;
|
|
len = strlen(s);
|
|
if (len > INT_MAX) {
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
"string to parse is too long");
|
|
return NULL;
|
|
}
|
|
if (s[--len] != quote) {
|
|
PyErr_BadInternalCall();
|
|
return NULL;
|
|
}
|
|
if (len >= 4 && s[0] == quote && s[1] == quote) {
|
|
s += 2;
|
|
len -= 2;
|
|
if (s[--len] != quote || s[--len] != quote) {
|
|
PyErr_BadInternalCall();
|
|
return NULL;
|
|
}
|
|
}
|
|
if (!*bytesmode) {
|
|
return decode_unicode(s, len, rawmode, encoding);
|
|
}
|
|
if (*bytesmode) {
|
|
/* Disallow non-ascii characters (but not escapes) */
|
|
const char *c;
|
|
for (c = s; *c; c++) {
|
|
if (Py_CHARMASK(*c) >= 0x80) {
|
|
ast_error(n, "bytes can only contain ASCII "
|
|
"literal characters.");
|
|
return NULL;
|
|
}
|
|
}
|
|
}
|
|
need_encoding = (!*bytesmode && encoding != NULL &&
|
|
strcmp(encoding, "utf-8") != 0 &&
|
|
strcmp(encoding, "iso-8859-1") != 0);
|
|
if (rawmode || strchr(s, '\\') == NULL) {
|
|
if (need_encoding) {
|
|
PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
|
|
if (u == NULL)
|
|
return NULL;
|
|
v = PyUnicode_AsEncodedString(u, encoding, NULL);
|
|
Py_DECREF(u);
|
|
return v;
|
|
} else {
|
|
return PyString_FromStringAndSize(s, len);
|
|
}
|
|
}
|
|
|
|
return PyString_DecodeEscape(s, len, NULL, 1,
|
|
need_encoding ? encoding : NULL);
|
|
}
|
|
|
|
/* Build a Python string object out of a STRING atom. This takes care of
|
|
* compile-time literal catenation, calling parsestr() on each piece, and
|
|
* pasting the intermediate results together.
|
|
*/
|
|
static PyObject *
|
|
parsestrplus(struct compiling *c, const node *n, int *bytesmode)
|
|
{
|
|
PyObject *v;
|
|
int i;
|
|
REQ(CHILD(n, 0), STRING);
|
|
v = parsestr(CHILD(n, 0), c->c_encoding, bytesmode);
|
|
if (v != NULL) {
|
|
/* String literal concatenation */
|
|
for (i = 1; i < NCH(n); i++) {
|
|
PyObject *s;
|
|
int subbm = 0;
|
|
s = parsestr(CHILD(n, i), c->c_encoding, &subbm);
|
|
if (s == NULL)
|
|
goto onError;
|
|
if (*bytesmode != subbm) {
|
|
ast_error(n, "cannot mix bytes and nonbytes"
|
|
"literals");
|
|
goto onError;
|
|
}
|
|
if (PyString_Check(v) && PyString_Check(s)) {
|
|
PyString_ConcatAndDel(&v, s);
|
|
if (v == NULL)
|
|
goto onError;
|
|
}
|
|
else {
|
|
PyObject *temp = PyUnicode_Concat(v, s);
|
|
Py_DECREF(s);
|
|
Py_DECREF(v);
|
|
v = temp;
|
|
if (v == NULL)
|
|
goto onError;
|
|
}
|
|
}
|
|
}
|
|
return v;
|
|
|
|
onError:
|
|
Py_XDECREF(v);
|
|
return NULL;
|
|
}
|