Skip to content

Commit e9516c7

Browse files
committed
Merge 'origin/main' into type-slot-ts-v2
2 parents 5e38497 + 7e8b153 commit e9516c7

22 files changed

Lines changed: 249 additions & 206 deletions

Doc/deprecations/pending-removal-in-future.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ although there is currently no date scheduled for their removal.
4747
:data:`calendar.FEBRUARY`.
4848
(Contributed by Prince Roshan in :gh:`103636`.)
4949

50+
* :mod:`codecs`: use :func:`open` instead of :func:`codecs.open`. (:gh:`133038`)
51+
5052
* :attr:`codeobject.co_lnotab`: use the :meth:`codeobject.co_lines` method
5153
instead.
5254

Doc/library/codecs.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ wider range of codecs when working with binary files:
208208
.. versionchanged:: 3.11
209209
The ``'U'`` mode has been removed.
210210

211+
.. deprecated:: next
212+
213+
:func:`codecs.open` has been superseded by :func:`open`.
214+
211215

212216
.. function:: EncodedFile(file, data_encoding, file_encoding=None, errors='strict')
213217

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,10 @@ Deprecated
15971597
as a single positional argument.
15981598
(Contributed by Serhiy Storchaka in :gh:`109218`.)
15991599

1600+
* :mod:`codecs`:
1601+
:func:`codecs.open` is now deprecated. Use :func:`open` instead.
1602+
(Contributed by Inada Naoki in :gh:`133036`.)
1603+
16001604
* :mod:`functools`:
16011605
Calling the Python implementation of :func:`functools.reduce` with *function*
16021606
or *sequence* as keyword arguments is now deprecated.

Lib/_pyio.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,8 +2056,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
20562056
raise ValueError("invalid encoding: %r" % encoding)
20572057

20582058
if not codecs.lookup(encoding)._is_text_encoding:
2059-
msg = ("%r is not a text encoding; "
2060-
"use codecs.open() to handle arbitrary codecs")
2059+
msg = "%r is not a text encoding"
20612060
raise LookupError(msg % encoding)
20622061

20632062
if errors is None:

Lib/codecs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,6 @@ def __reduce_ex__(self, proto):
884884
### Shortcuts
885885

886886
def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
887-
888887
""" Open an encoded file using the given mode and return
889888
a wrapped version providing transparent encoding/decoding.
890889
@@ -912,8 +911,11 @@ def open(filename, mode='r', encoding=None, errors='strict', buffering=-1):
912911
.encoding which allows querying the used encoding. This
913912
attribute is only available if an encoding was specified as
914913
parameter.
915-
916914
"""
915+
import warnings
916+
warnings.warn("codecs.open() is deprecated. Use open() instead.",
917+
DeprecationWarning, stacklevel=2)
918+
917919
if encoding is not None and \
918920
'b' not in mode:
919921
# Force opening of the file in binary mode

Lib/test/test_codecs.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import encodings
99
from unittest import mock
10+
import warnings
1011

1112
from test import support
1213
from test.support import os_helper
@@ -20,13 +21,12 @@
2021
except ImportError:
2122
_testinternalcapi = None
2223

23-
try:
24-
import ctypes
25-
except ImportError:
26-
ctypes = None
27-
SIZEOF_WCHAR_T = -1
28-
else:
29-
SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar)
24+
25+
def codecs_open_no_warn(*args, **kwargs):
26+
"""Call codecs.open(*args, **kwargs) ignoring DeprecationWarning."""
27+
with warnings.catch_warnings():
28+
warnings.simplefilter("ignore")
29+
return codecs.open(*args, **kwargs)
3030

3131
def coding_checker(self, coder):
3232
def check(input, expect):
@@ -35,13 +35,13 @@ def check(input, expect):
3535

3636
# On small versions of Windows like Windows IoT or Windows Nano Server not all codepages are present
3737
def is_code_page_present(cp):
38-
from ctypes import POINTER, WINFUNCTYPE, WinDLL
38+
from ctypes import POINTER, WINFUNCTYPE, WinDLL, Structure
3939
from ctypes.wintypes import BOOL, BYTE, WCHAR, UINT, DWORD
4040

4141
MAX_LEADBYTES = 12 # 5 ranges, 2 bytes ea., 0 term.
4242
MAX_DEFAULTCHAR = 2 # single or double byte
4343
MAX_PATH = 260
44-
class CPINFOEXW(ctypes.Structure):
44+
class CPINFOEXW(Structure):
4545
_fields_ = [("MaxCharSize", UINT),
4646
("DefaultChar", BYTE*MAX_DEFAULTCHAR),
4747
("LeadByte", BYTE*MAX_LEADBYTES),
@@ -719,19 +719,19 @@ def test_bug691291(self):
719719
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
720720
with open(os_helper.TESTFN, 'wb') as fp:
721721
fp.write(s)
722-
with codecs.open(os_helper.TESTFN, 'r',
722+
with codecs_open_no_warn(os_helper.TESTFN, 'r',
723723
encoding=self.encoding) as reader:
724724
self.assertEqual(reader.read(), s1)
725725

726726
def test_invalid_modes(self):
727727
for mode in ('U', 'rU', 'r+U'):
728728
with self.assertRaises(ValueError) as cm:
729-
codecs.open(os_helper.TESTFN, mode, encoding=self.encoding)
729+
codecs_open_no_warn(os_helper.TESTFN, mode, encoding=self.encoding)
730730
self.assertIn('invalid mode', str(cm.exception))
731731

732732
for mode in ('rt', 'wt', 'at', 'r+t'):
733733
with self.assertRaises(ValueError) as cm:
734-
codecs.open(os_helper.TESTFN, mode, encoding=self.encoding)
734+
codecs_open_no_warn(os_helper.TESTFN, mode, encoding=self.encoding)
735735
self.assertIn("can't have text and binary mode at once",
736736
str(cm.exception))
737737

@@ -1844,9 +1844,9 @@ def test_all(self):
18441844
def test_open(self):
18451845
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
18461846
for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'):
1847-
with self.subTest(mode), \
1848-
codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
1849-
self.assertIsInstance(file, codecs.StreamReaderWriter)
1847+
with self.subTest(mode), self.assertWarns(DeprecationWarning):
1848+
with codecs.open(os_helper.TESTFN, mode, 'ascii') as file:
1849+
self.assertIsInstance(file, codecs.StreamReaderWriter)
18501850

18511851
def test_undefined(self):
18521852
self.assertRaises(UnicodeError, codecs.encode, 'abc', 'undefined')
@@ -1863,7 +1863,7 @@ def test_file_closes_if_lookup_error_raised(self):
18631863
mock_open = mock.mock_open()
18641864
with mock.patch('builtins.open', mock_open) as file:
18651865
with self.assertRaises(LookupError):
1866-
codecs.open(os_helper.TESTFN, 'wt', 'invalid-encoding')
1866+
codecs_open_no_warn(os_helper.TESTFN, 'wt', 'invalid-encoding')
18671867

18681868
file().close.assert_called()
18691869

@@ -2883,7 +2883,7 @@ def test_seek0(self):
28832883
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
28842884
for encoding in tests:
28852885
# Check if the BOM is written only once
2886-
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
2886+
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
28872887
f.write(data)
28882888
f.write(data)
28892889
f.seek(0)
@@ -2892,7 +2892,7 @@ def test_seek0(self):
28922892
self.assertEqual(f.read(), data * 2)
28932893

28942894
# Check that the BOM is written after a seek(0)
2895-
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
2895+
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
28962896
f.write(data[0])
28972897
self.assertNotEqual(f.tell(), 0)
28982898
f.seek(0)
@@ -2901,7 +2901,7 @@ def test_seek0(self):
29012901
self.assertEqual(f.read(), data)
29022902

29032903
# (StreamWriter) Check that the BOM is written after a seek(0)
2904-
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
2904+
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
29052905
f.writer.write(data[0])
29062906
self.assertNotEqual(f.writer.tell(), 0)
29072907
f.writer.seek(0)
@@ -2911,7 +2911,7 @@ def test_seek0(self):
29112911

29122912
# Check that the BOM is not written after a seek() at a position
29132913
# different than the start
2914-
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
2914+
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
29152915
f.write(data)
29162916
f.seek(f.tell())
29172917
f.write(data)
@@ -2920,7 +2920,7 @@ def test_seek0(self):
29202920

29212921
# (StreamWriter) Check that the BOM is not written after a seek()
29222922
# at a position different than the start
2923-
with codecs.open(os_helper.TESTFN, 'w+', encoding=encoding) as f:
2923+
with codecs_open_no_warn(os_helper.TESTFN, 'w+', encoding=encoding) as f:
29242924
f.writer.write(data)
29252925
f.writer.seek(f.writer.tell())
29262926
f.writer.write(data)

Lib/test/test_dict.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,31 @@ def __next__(self):
266266

267267
self.assertRaises(ValueError, {}.update, [(1, 2, 3)])
268268

269+
def test_update_type_error(self):
270+
with self.assertRaises(TypeError) as cm:
271+
{}.update([object() for _ in range(3)])
272+
273+
self.assertEqual(str(cm.exception), "object is not iterable")
274+
self.assertEqual(
275+
cm.exception.__notes__,
276+
['Cannot convert dictionary update sequence element #0 to a sequence'],
277+
)
278+
279+
def badgen():
280+
yield "key"
281+
raise TypeError("oops")
282+
yield "value"
283+
284+
with self.assertRaises(TypeError) as cm:
285+
dict([badgen() for _ in range(3)])
286+
287+
self.assertEqual(str(cm.exception), "oops")
288+
self.assertEqual(
289+
cm.exception.__notes__,
290+
['Cannot convert dictionary update sequence element #0 to a sequence'],
291+
)
292+
293+
269294
def test_fromkeys(self):
270295
self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
271296
d = {}

Lib/test/test_generated_cases.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def test_pep7_condition(self):
419419
def test_error_if_plain(self):
420420
input = """
421421
inst(OP, (--)) {
422-
ERROR_IF(cond, label);
422+
ERROR_IF(cond);
423423
}
424424
"""
425425
output = """
@@ -432,7 +432,7 @@ def test_error_if_plain(self):
432432
next_instr += 1;
433433
INSTRUCTION_STATS(OP);
434434
if (cond) {
435-
JUMP_TO_LABEL(label);
435+
JUMP_TO_LABEL(error);
436436
}
437437
DISPATCH();
438438
}
@@ -442,7 +442,7 @@ def test_error_if_plain(self):
442442
def test_error_if_plain_with_comment(self):
443443
input = """
444444
inst(OP, (--)) {
445-
ERROR_IF(cond, label); // Comment is ok
445+
ERROR_IF(cond); // Comment is ok
446446
}
447447
"""
448448
output = """
@@ -455,7 +455,7 @@ def test_error_if_plain_with_comment(self):
455455
next_instr += 1;
456456
INSTRUCTION_STATS(OP);
457457
if (cond) {
458-
JUMP_TO_LABEL(label);
458+
JUMP_TO_LABEL(error);
459459
}
460460
DISPATCH();
461461
}
@@ -467,7 +467,7 @@ def test_error_if_pop(self):
467467
inst(OP, (left, right -- res)) {
468468
SPAM(left, right);
469469
INPUTS_DEAD();
470-
ERROR_IF(cond, label);
470+
ERROR_IF(cond);
471471
res = 0;
472472
}
473473
"""
@@ -487,7 +487,7 @@ def test_error_if_pop(self):
487487
left = stack_pointer[-2];
488488
SPAM(left, right);
489489
if (cond) {
490-
JUMP_TO_LABEL(pop_2_label);
490+
JUMP_TO_LABEL(pop_2_error);
491491
}
492492
res = 0;
493493
stack_pointer[-2] = res;
@@ -503,7 +503,7 @@ def test_error_if_pop_with_result(self):
503503
inst(OP, (left, right -- res)) {
504504
res = SPAM(left, right);
505505
INPUTS_DEAD();
506-
ERROR_IF(cond, label);
506+
ERROR_IF(cond);
507507
}
508508
"""
509509
output = """
@@ -522,7 +522,7 @@ def test_error_if_pop_with_result(self):
522522
left = stack_pointer[-2];
523523
res = SPAM(left, right);
524524
if (cond) {
525-
JUMP_TO_LABEL(pop_2_label);
525+
JUMP_TO_LABEL(pop_2_error);
526526
}
527527
stack_pointer[-2] = res;
528528
stack_pointer += -1;
@@ -903,7 +903,7 @@ def test_array_error_if(self):
903903
inst(OP, (extra, values[oparg] --)) {
904904
DEAD(extra);
905905
DEAD(values);
906-
ERROR_IF(oparg == 0, somewhere);
906+
ERROR_IF(oparg == 0);
907907
}
908908
"""
909909
output = """
@@ -922,7 +922,7 @@ def test_array_error_if(self):
922922
if (oparg == 0) {
923923
stack_pointer += -1 - oparg;
924924
assert(WITHIN_STACK_BOUNDS());
925-
JUMP_TO_LABEL(somewhere);
925+
JUMP_TO_LABEL(error);
926926
}
927927
stack_pointer += -1 - oparg;
928928
assert(WITHIN_STACK_BOUNDS());
@@ -1319,7 +1319,7 @@ def test_pop_on_error_peeks(self):
13191319
13201320
op(THIRD, (j, k --)) {
13211321
INPUTS_DEAD(); // Mark j and k as used
1322-
ERROR_IF(cond, error);
1322+
ERROR_IF(cond);
13231323
}
13241324
13251325
macro(TEST) = FIRST + SECOND + THIRD;
@@ -1369,7 +1369,7 @@ def test_push_then_error(self):
13691369
13701370
op(SECOND, (a -- a, b)) {
13711371
b = 1;
1372-
ERROR_IF(cond, error);
1372+
ERROR_IF(cond);
13731373
}
13741374
13751375
macro(TEST) = FIRST + SECOND;
@@ -1414,10 +1414,10 @@ def test_error_if_true(self):
14141414

14151415
input = """
14161416
inst(OP1, ( --)) {
1417-
ERROR_IF(true, here);
1417+
ERROR_IF(true);
14181418
}
14191419
inst(OP2, ( --)) {
1420-
ERROR_IF(1, there);
1420+
ERROR_IF(1);
14211421
}
14221422
"""
14231423
output = """
@@ -1429,7 +1429,7 @@ def test_error_if_true(self):
14291429
frame->instr_ptr = next_instr;
14301430
next_instr += 1;
14311431
INSTRUCTION_STATS(OP1);
1432-
JUMP_TO_LABEL(here);
1432+
JUMP_TO_LABEL(error);
14331433
}
14341434
14351435
TARGET(OP2) {
@@ -1440,7 +1440,7 @@ def test_error_if_true(self):
14401440
frame->instr_ptr = next_instr;
14411441
next_instr += 1;
14421442
INSTRUCTION_STATS(OP2);
1443-
JUMP_TO_LABEL(there);
1443+
JUMP_TO_LABEL(error);
14441444
}
14451445
"""
14461446
self.run_cases_test(input, output)
@@ -1716,7 +1716,7 @@ def test_no_escaping_calls_in_branching_macros(self):
17161716

17171717
input = """
17181718
inst(OP, ( -- )) {
1719-
ERROR_IF(escaping_call(), error);
1719+
ERROR_IF(escaping_call());
17201720
}
17211721
"""
17221722
with self.assertRaises(SyntaxError):

Lib/test/test_multibytecodec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ def test_bug1728403(self):
314314
f.write(b'\xa1')
315315
finally:
316316
f.close()
317-
f = codecs.open(TESTFN, encoding='cp949')
317+
with self.assertWarns(DeprecationWarning):
318+
f = codecs.open(TESTFN, encoding='cp949')
318319
try:
319320
self.assertRaises(UnicodeDecodeError, f.read, 2)
320321
finally:

0 commit comments

Comments
 (0)