Commit Graph

865 Commits

Author SHA1 Message Date
Victor Stinner f8599279b6
[3.9] bpo-41194: The _ast module cannot be loaded more than once (GH-21290) (GH-21292)
* bpo-41194: Pass module state in Python-ast.c (GH-21284)

Rework asdl_c.py to pass the module state to functions in
Python-ast.c, instead of using astmodulestate_global.

Handle also PyState_AddModule() failure in init_types().

(cherry picked from commit 74419f0c64)

* bpo-41194: The _ast module cannot be loaded more than once (GH-21290)

Fix a crash in the _ast module: it can no longer be loaded more than
once. It now uses a global state rather than a module state.

* Move _ast module state: use a global state instead.
* Set _astmodule.m_size to -1, so the extension cannot be loaded more
  than once.

(cherry picked from commit 91e1bc18bd)
2020-07-03 16:57:19 +02:00
Guido van Rossum 2a1ee1d970
[3.9] bpo-35975: Only use cf_feature_version if PyCF_ONLY_AST in cf_flags (#21022) 2020-06-27 17:34:30 -07:00
Pablo Galindo dab533d0ee
[3.9] bpo-41076: Pre-feed the parser with the f-string expression location (GH-21054) (GH-21190)
This commit changes the parsing of f-string expressions with the new parser. The parser gets pre-fed with the location of the expression itself (not the f-string, which was what we were doing before). This allows us to completely skip the shifting of the AST nodes after the parsing is completed..
(cherry picked from commit 1f0f4abb11)
2020-06-28 01:15:28 +01:00
Pablo Galindo 102ca529ef
[3.9] bpo-40769: Allow extra surrounding parentheses for invalid annotated assignment rule (GH-20387) (GH-21186)
(cherry picked from commit c8f29ad986)
2020-06-28 00:40:41 +01:00
Miss Islington (bot) cb0dc52d37
bpo-41084: Adjust message when an f-string expression causes a SyntaxError (GH-21084)
Prefix the error message with `fstring: `, when parsing an f-string expression throws a `SyntaxError`.
(cherry picked from commit 2e0a920e9e)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
2020-06-27 12:43:49 -07:00
Lysandros Nikolaou 5193d0a665
[3.9] bpo-41132: Use pymalloc allocator in the f-string parser (GH-21173) (GH-21183)
(cherry picked from commit 6dcbc2422d)

Automerge-Triggered-By: @pablogsal
2020-06-27 11:35:18 -07:00
Lysandros Nikolaou d01a3e76ee
[3.9] bpo-41119: Output correct error message for list/tuple followed by colon (GH-21160) (GH-21172)
(cherry picked from commit 4b85e60601)
2020-06-27 00:14:12 +01:00
Lysandros Nikolaou 71bb921829
[3.9] bpo-41060: Avoid SEGFAULT when calling GET_INVALID_TARGET in the grammar (GH-21020) (GH-21024)
`GET_INVALID_TARGET` might unexpectedly return `NULL`, which if not
caught will cause a SEGFAULT. Therefore, this commit introduces a new
inline function `RAISE_SYNTAX_ERROR_INVALID_TARGET` that always
checks for `GET_INVALID_TARGET` returning NULL and can be used in
the grammar, replacing the long C ternary operation used till now.

(cherry picked from commit 6c4e0bd974)

Automerge-Triggered-By: @pablogsal
2020-06-20 19:47:22 -07:00
Miss Islington (bot) c9f83c173b
bpo-40958: Avoid 'possible loss of data' warning on Windows (GH-20970)
(cherry picked from commit 861efc6e8f)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
2020-06-20 10:35:03 -07:00
Lysandros Nikolaou a5442b26f4
[3.9] bpo-40334: Produce better error messages on invalid targets (GH-20106) (GH-20973)
* bpo-40334: Produce better error messages on invalid targets (GH-20106)

The following error messages get produced:
- `cannot delete ...` for invalid `del` targets
- `... is an illegal 'for' target` for invalid targets in for
  statements
- `... is an illegal 'with' target` for invalid targets in
  with statements

Additionally, a few `cut`s were added in various places before the
invocation of the `invalid_*` rule, in order to speed things
up.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
(cherry picked from commit 01ece63d42)
2020-06-19 01:03:58 +01:00
Miss Islington (bot) 7795ae8f05
bpo-40958: Avoid buffer overflow in the parser when indexing the current line (GH-20875) (GH-20919)
(cherry picked from commit 51c5896b62)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-16 18:36:59 +01:00
Miss Islington (bot) b498c7f1b3
bpo-36020: Remove snprintf macro in pyerrors.h (GH-20889)
On Windows, GH-include "pyerrors.h" no longer defines "snprintf" and
"vsnprintf" macros.

PyOS_snprintf() and PyOS_vsnprintf() should be used to get portable
behavior.

Replace snprintf() calls with PyOS_snprintf() and replace vsnprintf()
calls with PyOS_vsnprintf().
(cherry picked from commit e822e37946)

Co-authored-by: Victor Stinner <vstinner@python.org>
2020-06-15 13:20:10 -07:00
Pablo Galindo 30b59fd7cf
[3.9] Improve readability and style in parser files (GH-20884) (GH-20885)
(cherry picked from commit fb61c42)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-15 15:08:00 +01:00
Pablo Galindo 3782497cc2
[3.9] bpo-40939: Fix test_keyword for the old parser (GH-20814) 2020-06-11 19:29:13 +01:00
Miss Islington (bot) e3ce3bba92
bpo-40847: Consider a line with only a LINECONT a blank line (GH-20769)
A line with only a line continuation character should be considered
a blank line at tokenizer level so that only a single NEWLINE token
gets emitted. The old parser was working around the issue, but the
new parser threw a `SyntaxError` for valid input. For example,
an empty line following a line continuation character was interpreted
as a `SyntaxError`.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
(cherry picked from commit 896f4cf63f)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
2020-06-10 17:14:16 -07:00
Miss Islington (bot) d55ed7b107
Raise specialised syntax error for invalid lambda parameters (GH-20776)
(cherry picked from commit c6483c9896)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-10 06:24:41 -07:00
Miss Islington (bot) 8df4f3942f
bpo-40903: Handle multiple '=' in invalid assignment rules in the PEG parser (GH-20697)
Automerge-Triggered-By: @pablogsal
(cherry picked from commit 9f495908c5)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-08 02:22:06 -07:00
Miss Islington (bot) 6440911736
bpo-40904: Fix segfault in the new parser with f-string containing yield statements with no value (GH-20701)
(cherry picked from commit 972ab03276)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-07 18:08:53 -07:00
Miss Islington (bot) 15fec5627a
bpo-40880: Fix invalid read in newline_in_string in pegen.c (GH-20666)
* bpo-40880: Fix invalid read in newline_in_string in pegen.c

* Update Parser/pegen/pegen.c

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>

* Add NEWS entry

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
(cherry picked from commit 2e6593db00)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-05 17:13:14 -07:00
Miss Islington (bot) 79e6c15aed
bpo-40883: Fix memory leak in fstring_compile_expr in parse_string.c (GH-20667)
(cherry picked from commit a54096e305)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-06-05 17:10:57 -07:00
Victor Stinner 5d2396c8cf
[3.9] bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20613)
* bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20579)

Fix GIL usage in PyOS_Readline(): lock the GIL to set an exception.

Pass tstate to my_fgets() and _PyOS_WindowsConsoleReadline(). Cleanup
these functions.

(cherry picked from commit c353764fd5)

* bpo-40826: Add _PyOS_InterruptOccurred(tstate) function (GH-20599)

my_fgets() now calls _PyOS_InterruptOccurred(tstate) to check for
pending signals, rather calling PyOS_InterruptOccurred().

my_fgets() is called with the GIL released, whereas
PyOS_InterruptOccurred() must be called with the GIL held.

test_repl: use text=True and avoid SuppressCrashReport in
test_multiline_string_parsing().

Fix my_fgets() on Windows: fgets(fp) does crash if fileno(fp) is closed.

(cherry picked from commit fa7ab6aa0f)
2020-06-03 17:49:25 +02:00
Miss Islington (bot) bcbe5c59dd
bpo-40217: Ensure Py_VISIT(Py_TYPE(self)) is always called for PyType_FromSpec types (reverts GH-19414) (GH-20264)
Heap types now always visit the type in tp_traverse. See added docs for details.

This reverts commit 0169d3003b.

Automerge-Triggered-By: @encukou
(cherry picked from commit 1cf15af9a6)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-28 08:12:23 -07:00
Pablo Galindo 9b83829e7d
[3.9] bpo-40614: Respect feature version for f-string debug expressions (GH-20196) (GH-20464)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
(cherry picked from commit c116c94)

Co-authored-by: Shantanu <hauntsaninja@users.noreply.github.com>
2020-05-27 22:01:11 +01:00
Lysandros Nikolaou c011d1b5be
[3.9] Backport GH-20440: Set p->error_indicator in more places (GH-20457) 2020-05-27 21:20:43 +01:00
Lysandros Nikolaou 1bfe659ee5
[3.9] Backport GH-20370 and GH-20436: Soft keywords (GH-20458) 2020-05-27 21:20:07 +01:00
Miss Islington (bot) fda47659bb
Fix peg_generator compiler warnings under MSVC (GH-20405)
(cherry picked from commit a2bbedc8b1)

Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
2020-05-25 21:52:21 -07:00
Lysandros Nikolaou 791a46ed58
[3.9] bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399) (GH-20404)
When a `SyntaxError` in the expression part of a fstring is found,
the filename attribute of the `SyntaxError` is always `<fstring>`.
With this commit, it gets changed to always have the name of the file
the fstring resides in.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>.
(cherry picked from commit f7b1e46156)
2020-05-26 02:24:31 +01:00
Miss Islington (bot) 31084be618
bpo-40750: Do not expand the new parser debug flags if Py_BUILD_CORE is not defined (GH-20393)
(cherry picked from commit deb4355a37)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-25 12:37:56 -07:00
Miss Islington (bot) 82da2c3eb4
bpo-40750: Support -d flag in the new parser (GH-20340)
(cherry picked from commit 800a35c623)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-25 10:58:03 -07:00
Miss Islington (bot) 1a4e9e6f35
bpo-36290: Fix keytword collision handling in AST node constructors (GH-12382)
(cherry picked from commit c73914a562)

Co-authored-by: Rémi Lapeyre <remi.lapeyre@lenstra.fr>
2020-05-24 14:32:32 -07:00
Miss Islington (bot) 11fb605cb8
Use Py_ssize_t for the column number in the PEG support code (GH-20341)
(cherry picked from commit b23d7adfdf)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-23 22:20:44 -07:00
Miss Islington (bot) 55c8923524
bpo-40334: Produce better error messages for non-parenthesized genexps (GH-20153)
The error message, generated for a non-parenthesized generator expression
in function calls, was still the generic `invalid syntax`, when the generator expression wasn't appearing as the first argument in the call. With this patch, even on input like `f(a, b, c for c in d, e)`, the correct error message gets produced.
(cherry picked from commit ae14583302)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
2020-05-21 18:14:55 -07:00
Miss Islington (bot) d00aaf306a
bpo-40715: Reject dict unpacking on dict comprehensions (GH-20292)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
(cherry picked from commit b8a65ec1d3)

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
2020-05-21 15:58:16 -07:00
Pablo Galindo 275d7e1080
[3.9] bpo-40176: Improve error messages for trailing comma on from import (GH-20294) (GH-20302)
(cherry picked from commit 72e0aa2)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
2020-05-21 22:04:54 +01:00
Pablo Galindo ced4e5c227
Regenerate the parser (#20195) 2020-05-18 23:47:51 +02:00
Lysandros Nikolaou 75b863aa97
bpo-40334: Reproduce error message for type comments on bare '*' in the new parser (GH-20151) 2020-05-18 20:14:47 +01:00
Batuhan Taskaya 63b8e0cba3
bpo-40528: Improve AST generation script to do builds simultaneously (GH-19968)
- Switch from getopt to argparse.
- Removed the limitation of not being able to produce both C and H simultaneously.

This will make it run faster since it parses the asdl definition once and uses the generated tree to generate both the header and the C source.
2020-05-18 18:42:10 +01:00
Lysandros Nikolaou 7b7a21bc4f
bpo-40661: Fix segfault when parsing invalid input (GH-20165)
Fix segfaults when parsing very complex invalid input, like `import äˆ ð£„¯ð¢·žð±‹á”€ð””ð‘©±å®ä±¬ð©¾\n𗶽`.

Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
2020-05-18 18:32:03 +01:00
Lysandros Nikolaou 2c8cd06afe
bpo-40334: Improvements to error-handling code in the PEG parser (GH-20003)
The following improvements are implemented in this commit:
- `p->error_indicator` is set, in case malloc or realloc fail.
- Avoid memory leaks in the case that realloc fails.
- Call `PyErr_NoMemory()` instead of `PyErr_Format()`, because it requires no memory.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-17 04:19:23 +01:00
Pablo Galindo 16ab07063c
bpo-40334: Correctly identify invalid target in assignment errors (GH-20076)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
2020-05-15 02:04:52 +01:00
Lysandros Nikolaou ce21cfca7b
bpo-40618: Disallow invalid targets in augassign and except clauses (GH-20083)
This commit fixes the new parser to disallow invalid targets in the
following scenarios:
- Augmented assignments must only accept a single target (Name,
  Attribute or Subscript), but no tuples or lists.
- `except` clauses should only accept a single `Name` as a target.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
2020-05-14 21:13:50 +01:00
Pablo Galindo bcc3036095
bpo-40619: Correctly handle error lines in programs without file mode (GH-20090) 2020-05-14 21:11:48 +01:00
Lysandros Nikolaou a15c9b3a05
bpo-40334: Always show the caret on SyntaxErrors (GH-20050)
This commit fixes SyntaxError locations when the caret is not displayed,
by doing the following:

- `col_number` always gets set to the location of the offending
  node/expr. When no caret is to be displayed, this gets achieved
  by setting the object holding the error line to None.

- Introduce a new function `_PyPegen_raise_error_known_location`,
  which can be called, when an arbitrary `lineno`/`col_offset`
  needs to be passed. This function then gets used in the grammar
  (through some new macros and inline functions) so that SyntaxError
  locations of the new parser match that of the old.
2020-05-13 20:36:27 +01:00
Serhiy Storchaka 74ea6b5a75
bpo-40593: Improve syntax errors for invalid characters in source code. (GH-20033) 2020-05-12 12:42:04 +03:00
Shantanu 27c0d9b54a
bpo-40334: produce specialized errors for invalid del targets (GH-19911) 2020-05-11 14:53:58 -07:00
Pablo Galindo 5b956ca42d
bpo-40585: Normalize errors messages in codeop when comparing them (GH-20030)
With the new parser, the error message contains always the trailing
newlines, causing the comparison of the repr of the error messages
in codeop to fail. This commit makes the new parser mirror the old parser's
behaviour regarding trailing newlines.
2020-05-11 01:41:26 +01:00
Pablo Galindo ac7a92cc0a
bpo-40334: Avoid collisions between parser variables and grammar variables (GH-19987)
This is for the C generator:
- Disallow rule and variable names starting with `_`
- Rename most local variable names generated by the parser to start with `_`

Exceptions:
- Renaming `p` to `_p` will be a separate PR
- There are still some names that might clash, e.g.
  - anything starting with `Py`
  - C reserved words (`if` etc.)
  - Macros like `EXTRA` and `CHECK`
2020-05-09 21:34:50 -07:00
Joannah Nanjekye d10091aa17
bpo-40502: Initialize n->n_col_offset (GH-19988)
* initialize n->n_col_offset

* 📜🤖 Added by blurb_it.

* Move initialization

Co-authored-by: nanjekyejoannah <joannah.nanjekye@ibm.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2020-05-08 17:58:28 -03:00
Pablo Galindo db9163ceef
bpo-40555: Check for p->error_indicator in loop rules after the main loop is done (GH-19986) 2020-05-08 03:38:44 +01:00
Lysandros Nikolaou 4638c64295
bpo-40334: Error message for invalid default args in function call (GH-19973)
When parsing something like `f(g()=2)`, where the name of a default arg
is not a NAME, but an arbitrary expression, a specialised error message
is emitted.
2020-05-07 11:44:06 +01:00