Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/json/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ def _iterencode_dict(dct, _current_indent_level):
_current_indent_level += 1
newline_indent = '\n' + _indent * _current_indent_level
item_separator = _item_separator + newline_indent
yield newline_indent
else:
newline_indent = None
item_separator = _item_separator
Expand Down Expand Up @@ -381,6 +380,8 @@ def _iterencode_dict(dct, _current_indent_level):
f'not {key.__class__.__name__}')
if first:
first = False
if newline_indent is not None:
yield newline_indent
else:
yield item_separator
yield _encoder(key)
Expand Down Expand Up @@ -413,7 +414,7 @@ def _iterencode_dict(dct, _current_indent_level):
except BaseException as exc:
exc.add_note(f'when serializing {type(dct).__name__} item {key!r}')
raise
if newline_indent is not None:
if not first and newline_indent is not None:
_current_indent_level -= 1
yield '\n' + _indent * _current_indent_level
yield '}'
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Python test set -- part 1, grammar.
# This just tests whether the parser accepts them all.

from test.support import check_syntax_error
from test.support import check_syntax_error, skip_wasi_stack_overflow
from test.support import import_helper
import annotationlib
import inspect
Expand Down Expand Up @@ -249,6 +249,18 @@ def test_eof_error(self):
compile(s, "<test>", "exec")
self.assertIn("was never closed", str(cm.exception))

@skip_wasi_stack_overflow()
def test_max_level(self):
# Macro defined in Parser/lexer/state.h
MAXLEVEL = 200

result = eval("(" * MAXLEVEL + ")" * MAXLEVEL)
self.assertEqual(result, ())

with self.assertRaises(SyntaxError) as cm:
eval("(" * (MAXLEVEL + 1) + ")" * (MAXLEVEL + 1))
self.assertStartsWith(str(cm.exception), 'too many nested parentheses')

var_annot_global: int # a global annotated is necessary for test_var_annot


Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_json/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def test_dump_skipkeys(self):
self.assertIn('valid_key', o)
self.assertNotIn(b'invalid_key', o)

def test_dump_skipkeys_indent_empty(self):
v = {b'invalid_key': False}
self.assertEqual(self.json.dumps(v, skipkeys=True, indent=4), '{}')

def test_skipkeys_indent(self):
v = {b'invalid_key': False, 'valid_key': True}
self.assertEqual(self.json.dumps(v, skipkeys=True, indent=4), '{\n "valid_key": true\n}')

def test_encode_truefalse(self):
self.assertEqual(self.dumps(
{True: False, False: True}, sort_keys=True),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix formatting issues in :func:`json.dump` when both *indent* and *skipkeys* are used.
13 changes: 8 additions & 5 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,12 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs

if (*first) {
*first = false;
if (s->indent != Py_None) {
if (write_newline_indent(writer, indent_level, indent_cache) < 0) {
Py_DECREF(keystr);
return -1;
}
}
}
else {
if (PyUnicodeWriter_WriteStr(writer, item_separator) < 0) {
Expand Down Expand Up @@ -1670,11 +1676,8 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
if (s->indent != Py_None) {
indent_level++;
separator = get_item_separator(s, indent_level, indent_cache);
if (separator == NULL ||
write_newline_indent(writer, indent_level, indent_cache) < 0)
{
if (separator == NULL)
goto bail;
}
}

if (s->sort_keys || !PyDict_CheckExact(dct)) {
Expand Down Expand Up @@ -1714,7 +1717,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
goto bail;
Py_CLEAR(ident);
}
if (s->indent != Py_None) {
if (s->indent != Py_None && !first) {
indent_level--;
if (write_newline_indent(writer, indent_level, indent_cache) < 0) {
goto bail;
Expand Down
2 changes: 1 addition & 1 deletion Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Python/asm_trampoline.S
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
# }
_Py_trampoline_func_start:
#ifdef __x86_64__
#if defined(__CET__) && (__CET__ & 1)
endbr64
#endif
sub $8, %rsp
call *%rcx
add $8, %rsp
Expand All @@ -34,3 +37,22 @@ _Py_trampoline_func_start:
.globl _Py_trampoline_func_end
_Py_trampoline_func_end:
.section .note.GNU-stack,"",@progbits
# Note for indicating the assembly code supports CET
#if defined(__x86_64__) && defined(__CET__) && (__CET__ & 1)
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:
#endif // __x86_64__
4 changes: 4 additions & 0 deletions Python/perf_jit_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,11 @@ static void elf_init_ehframe(ELFObjectContext* ctx) {
*/
#ifdef __x86_64__
/* x86_64 calling convention unwinding rules */
# if defined(__CET__) && (__CET__ & 1)
DWRF_U8(DWRF_CFA_advance_loc | 8); // Advance location by 8 bytes when CET protection is enabled
# else
DWRF_U8(DWRF_CFA_advance_loc | 4); // Advance location by 4 bytes
# endif
DWRF_U8(DWRF_CFA_def_cfa_offset); // Redefine CFA offset
DWRF_UV(16); // New offset: SP + 16
DWRF_U8(DWRF_CFA_advance_loc | 6); // Advance location by 6 bytes
Expand Down
2 changes: 1 addition & 1 deletion Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# define MAXSTACK 4000
# endif
#else
# define MAXSTACK 4000
# define MAXSTACK 6000
#endif

"""
Expand Down
Loading