Skip to content

Commit 99298c7

Browse files
committed
Address review
1 parent 7cf8d5a commit 99298c7

2 files changed

Lines changed: 82 additions & 75 deletions

File tree

‎Lib/test/test_syntax.py‎

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,9 +2874,10 @@ def check_warning(self, code, errtext, filename="<testcase>", mode="exec"):
28742874

28752875
def check_no_warning(self, code, filename="<testcase>", mode="exec"):
28762876
"""Check that compiling code does not raise any warnings."""
2877+
import warnings
28772878
with warnings.catch_warnings(record=True) as caught:
28782879
warnings.simplefilter("always")
2879-
compile(source, filename, mode)
2880+
compile(code, filename, mode)
28802881
self.assertEqual(caught, [])
28812882

28822883
def test_return_in_finally(self):
@@ -2949,73 +2950,74 @@ def test_break_and_continue_in_finally(self):
29492950
""")
29502951
self.check_warning(source, f"'{kw}' in a 'finally' block")
29512952

2952-
def test_from_lazy_imports(self):
2953-
# gh-150459
2954-
self.check_warning(
2955-
"from . lazy import x",
2956-
"did you mean 'lazy from . import'?",
2957-
)
2958-
self.check_warning(
2959-
"from . lazy import x as y",
2960-
"did you mean 'lazy from . import'?",
2961-
)
2962-
self.check_warning(
2963-
"from . lazy import *",
2964-
"did you mean 'lazy from . import'?",
2965-
)
2966-
self.check_warning(
2967-
"from .. lazy import x",
2968-
"did you mean 'lazy from .. import'?",
2969-
)
2970-
self.check_warning(
2971-
"from ... lazy import x",
2972-
"did you mean 'lazy from ... import'?",
2973-
)
2974-
self.check_warning(
2975-
"from .... lazy import x",
2976-
"did you mean 'lazy from .... import'?",
2977-
)
2978-
self.check_warning(
2979-
"from . \\\n lazy import x",
2980-
"did you mean 'lazy from . import'?",
2981-
)
2982-
self.check_warning(
2983-
"from .\\\nlazy import x",
2984-
"did you mean 'lazy from . import'?",
2985-
)
2986-
self.check_warning(
2987-
"from .\tlazy import x",
2988-
"did you mean 'lazy from . import'?",
2989-
)
2990-
2991-
def test_not_from_lazy_imports(self):
2992-
self.check_no_warning("from .lazy import x")
2993-
self.check_no_warning("from .lazy import *")
2994-
self.check_no_warning("from ..lazy import x")
2995-
self.check_no_warning("from ...lazy import x")
2996-
self.check_no_warning("from .lazy.sub import x")
2997-
self.check_no_warning("from ..lazy.sub import x")
2998-
self.check_no_warning("from ...lazy.sub import x")
2999-
self.check_no_warning("from . lazier import x")
3000-
self.check_no_warning("from . lazy_module import x")
3001-
self.check_no_warning("from . lazy.sub import x")
3002-
self.check_no_warning("from . sub.lazy import x")
3003-
self.check_no_warning("from lazy import x")
3004-
self.check_no_warning("from lazy.sub import x")
3005-
self.check_no_warning("lazy from . lazy import x")
3006-
self.check_no_warning("from . import lazy")
3007-
3008-
def test_from_lazy_imports_as_error(self):
3009-
with warnings.catch_warnings():
3010-
warnings.simplefilter("error", SyntaxWarning)
3011-
with self.assertRaisesRegex(
3012-
SyntaxError,
3013-
re.escape("did you mean 'lazy from . import'?"),
3014-
) as cm:
3015-
compile("from . lazy import x", "<test>", "exec")
3016-
self.assertEqual(cm.exception.lineno, 1)
3017-
self.assertEqual(cm.exception.offset, 8)
3018-
self.assertEqual(cm.exception.end_offset, 12)
2953+
def test_from_lazy_imports(self):
2954+
# gh-150459
2955+
self.check_warning(
2956+
"from . lazy import x",
2957+
"did you mean 'lazy from . import'?",
2958+
)
2959+
self.check_warning(
2960+
"from . lazy import x as y",
2961+
"did you mean 'lazy from . import'?",
2962+
)
2963+
self.check_warning(
2964+
"from . lazy import *",
2965+
"did you mean 'lazy from . import'?",
2966+
)
2967+
self.check_warning(
2968+
"from .. lazy import x",
2969+
"did you mean 'lazy from .. import'?",
2970+
)
2971+
self.check_warning(
2972+
"from ... lazy import x",
2973+
"did you mean 'lazy from ... import'?",
2974+
)
2975+
self.check_warning(
2976+
"from .... lazy import x",
2977+
"did you mean 'lazy from .... import'?",
2978+
)
2979+
self.check_warning(
2980+
"from . \\\n lazy import x",
2981+
"did you mean 'lazy from . import'?",
2982+
)
2983+
self.check_warning(
2984+
"from .\\\nlazy import x",
2985+
"did you mean 'lazy from . import'?",
2986+
)
2987+
self.check_warning(
2988+
"from .\tlazy import x",
2989+
"did you mean 'lazy from . import'?",
2990+
)
2991+
2992+
def test_not_from_lazy_imports(self):
2993+
self.check_no_warning("from .lazy import x")
2994+
self.check_no_warning("from .lazy import *")
2995+
self.check_no_warning("from ..lazy import x")
2996+
self.check_no_warning("from ...lazy import x")
2997+
self.check_no_warning("from .lazy.sub import x")
2998+
self.check_no_warning("from ..lazy.sub import x")
2999+
self.check_no_warning("from ...lazy.sub import x")
3000+
self.check_no_warning("from . lazier import x")
3001+
self.check_no_warning("from . lazy_module import x")
3002+
self.check_no_warning("from . lazy.sub import x")
3003+
self.check_no_warning("from . sub.lazy import x")
3004+
self.check_no_warning("from lazy import x")
3005+
self.check_no_warning("from lazy.sub import x")
3006+
self.check_no_warning("lazy from . lazy import x")
3007+
self.check_no_warning("from . import lazy")
3008+
3009+
def test_from_lazy_imports_as_error(self):
3010+
import warnings
3011+
with warnings.catch_warnings():
3012+
warnings.simplefilter("error", SyntaxWarning)
3013+
with self.assertRaisesRegex(
3014+
SyntaxError,
3015+
re.escape("did you mean 'lazy from . import'?"),
3016+
) as cm:
3017+
compile("from . lazy import x", "<test>", "exec")
3018+
self.assertEqual(cm.exception.lineno, 1)
3019+
self.assertEqual(cm.exception.offset, 8)
3020+
self.assertEqual(cm.exception.end_offset, 12)
30193021

30203022

30213023
class SyntaxErrorTestCase(unittest.TestCase):

‎Parser/action_helpers.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,27 +2002,32 @@ _warn_relative_import_of_lazy(Parser *p, asdl_seq *dots, expr_ty module)
20022002
}
20032003

20042004
int count = _PyPegen_seq_count_dots(dots);
2005-
char *buf = PyMem_RawMalloc(sizeof(char *) * (count + 1));
2005+
char *buf = PyMem_RawMalloc(count + 1);
20062006
if (buf == NULL) {
20072007
PyErr_NoMemory();
20082008
return -1;
20092009
}
2010-
memset(buf, '.', sizeof(char *) * count);
2010+
memset(buf, '.', count);
20112011
buf[count] = '\0';
20122012

20132013
PyObject *msg = PyUnicode_FromFormat(
20142014
"'from %s lazy import' is the same as 'from %slazy import'; "
20152015
"did you mean 'lazy from %s import'?",
20162016
buf, buf, buf);
2017-
free(buf);
2017+
PyMem_RawFree(buf);
20182018
if (msg == NULL) {
20192019
return -1;
20202020
}
20212021

2022-
return _PyErr_EmitSyntaxWarning(msg, p->tok->filename,
2023-
module->lineno, module->col_offset,
2024-
module->end_lineno, module->end_col_offset,
2025-
p->tok->module);
2022+
int res = _PyErr_EmitSyntaxWarning(msg,
2023+
p->tok->filename,
2024+
module->lineno,
2025+
module->col_offset + 1,
2026+
module->end_lineno,
2027+
module->end_col_offset + 1,
2028+
p->tok->module);
2029+
Py_DECREF(msg);
2030+
return res;
20262031
}
20272032

20282033
stmt_ty

0 commit comments

Comments
 (0)