Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f28ba36

Browse files
Issue #20532: Tests which use _testcapi now are marked as CPython only.
2 parents 622be34 + 5cfc79d commit f28ba36

29 files changed

Lines changed: 282 additions & 116 deletions

Lib/test/string_tests.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import unittest, string, sys, struct
66
from test import support
77
from collections import UserList
8-
import _testcapi
98

109
class Sequence:
1110
def __init__(self, seq='wxyz'): self.seq = seq
@@ -1199,19 +1198,27 @@ def test_formatting(self):
11991198
# Outrageously large width or precision should raise ValueError.
12001199
self.checkraises(ValueError, '%%%df' % (2**64), '__mod__', (3.2))
12011200
self.checkraises(ValueError, '%%.%df' % (2**64), '__mod__', (3.2))
1201+
self.checkraises(OverflowError, '%*s', '__mod__',
1202+
(sys.maxsize + 1, ''))
1203+
self.checkraises(OverflowError, '%.*f', '__mod__',
1204+
(sys.maxsize + 1, 1. / 7))
1205+
1206+
class X(object): pass
1207+
self.checkraises(TypeError, 'abc', '__mod__', X())
12021208

1209+
@support.cpython_only
1210+
def test_formatting_c_limits(self):
1211+
from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX
1212+
SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1
12031213
self.checkraises(OverflowError, '%*s', '__mod__',
1204-
(_testcapi.PY_SSIZE_T_MAX + 1, ''))
1214+
(PY_SSIZE_T_MAX + 1, ''))
12051215
self.checkraises(OverflowError, '%.*f', '__mod__',
1206-
(_testcapi.INT_MAX + 1, 1. / 7))
1216+
(INT_MAX + 1, 1. / 7))
12071217
# Issue 15989
12081218
self.checkraises(OverflowError, '%*s', '__mod__',
1209-
(1 << (_testcapi.PY_SSIZE_T_MAX.bit_length() + 1), ''))
1219+
(SIZE_MAX + 1, ''))
12101220
self.checkraises(OverflowError, '%.*f', '__mod__',
1211-
(_testcapi.UINT_MAX + 1, 1. / 7))
1212-
1213-
class X(object): pass
1214-
self.checkraises(TypeError, 'abc', '__mod__', X())
1221+
(UINT_MAX + 1, 1. / 7))
12151222

12161223
def test_floatformatting(self):
12171224
# float formatting

Lib/test/support/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import logging.handlers
2626
import struct
2727
import tempfile
28-
import _testcapi
2928

3029
try:
3130
import _thread, threading
@@ -1373,6 +1372,7 @@ def calcvobjsize(fmt):
13731372
_TPFLAGS_HEAPTYPE = 1<<9
13741373

13751374
def check_sizeof(test, o, size):
1375+
import _testcapi
13761376
result = sys.getsizeof(o)
13771377
# add GC header size
13781378
if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\
@@ -2172,4 +2172,5 @@ def run_in_subinterp(code):
21722172
raise unittest.SkipTest("run_in_subinterp() cannot be used "
21732173
"if tracemalloc module is tracing "
21742174
"memory allocations")
2175+
import _testcapi
21752176
return _testcapi.run_in_subinterp(code)

Lib/test/test_atexit.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import unittest
33
import io
44
import atexit
5-
import _testcapi
65
from test import support
76

87
### helpers

Lib/test/test_capi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import threading
1919
except ImportError:
2020
threading = None
21-
import _testcapi
21+
# Skip this test if the _testcapi module isn't available.
22+
_testcapi = support.import_module('_testcapi')
2223

2324

2425
def testfunction(self):

Lib/test/test_code.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
import unittest
106106
import weakref
107-
import _testcapi
107+
from test.support import run_doctest, run_unittest, cpython_only
108108

109109

110110
def consts(t):
@@ -126,7 +126,9 @@ def dump(co):
126126

127127
class CodeTest(unittest.TestCase):
128128

129+
@cpython_only
129130
def test_newempty(self):
131+
import _testcapi
130132
co = _testcapi.code_newempty("filename", "funcname", 15)
131133
self.assertEqual(co.co_filename, "filename")
132134
self.assertEqual(co.co_name, "funcname")
@@ -159,7 +161,6 @@ def callback(code):
159161

160162

161163
def test_main(verbose=None):
162-
from test.support import run_doctest, run_unittest
163164
from test import test_code
164165
run_doctest(test_code, verbose)
165166
run_unittest(CodeTest, CodeWeakRefTest)

Lib/test/test_codecs.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _testcapi
21
import codecs
32
import contextlib
43
import io
@@ -1760,7 +1759,7 @@ def test_basic(self):
17601759

17611760
class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling):
17621761
def test_basics(self):
1763-
s = "abc123" # all codecs should be able to encode these
1762+
s = "abc123" # all codecs should be able to encode these
17641763
for encoding in all_unicode_encodings:
17651764
name = codecs.lookup(encoding).name
17661765
if encoding.endswith("_codec"):
@@ -1772,9 +1771,9 @@ def test_basics(self):
17721771
with support.check_warnings():
17731772
# unicode-internal has been deprecated
17741773
(b, size) = codecs.getencoder(encoding)(s)
1775-
self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding))
1774+
self.assertEqual(size, len(s), "encoding=%r" % encoding)
17761775
(chars, size) = codecs.getdecoder(encoding)(b)
1777-
self.assertEqual(chars, s, "%r != %r (encoding=%r)" % (chars, s, encoding))
1776+
self.assertEqual(chars, s, "encoding=%r" % encoding)
17781777

17791778
if encoding not in broken_unicode_with_streams:
17801779
# check stream reader/writer
@@ -1792,15 +1791,13 @@ def test_basics(self):
17921791
for c in encodedresult:
17931792
q.write(bytes([c]))
17941793
decodedresult += reader.read()
1795-
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
1794+
self.assertEqual(decodedresult, s, "encoding=%r" % encoding)
17961795

17971796
if encoding not in broken_incremental_coders:
1798-
# check incremental decoder/encoder (fetched via the Python
1799-
# and C API) and iterencode()/iterdecode()
1797+
# check incremental decoder/encoder and iterencode()/iterdecode()
18001798
try:
18011799
encoder = codecs.getincrementalencoder(encoding)()
1802-
cencoder = _testcapi.codec_incrementalencoder(encoding)
1803-
except LookupError: # no IncrementalEncoder
1800+
except LookupError: # no IncrementalEncoder
18041801
pass
18051802
else:
18061803
# check incremental decoder/encoder
@@ -1813,45 +1810,71 @@ def test_basics(self):
18131810
for c in encodedresult:
18141811
decodedresult += decoder.decode(bytes([c]))
18151812
decodedresult += decoder.decode(b"", True)
1816-
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
1813+
self.assertEqual(decodedresult, s,
1814+
"encoding=%r" % encoding)
18171815

1816+
# check iterencode()/iterdecode()
1817+
result = "".join(codecs.iterdecode(
1818+
codecs.iterencode(s, encoding), encoding))
1819+
self.assertEqual(result, s, "encoding=%r" % encoding)
1820+
1821+
# check iterencode()/iterdecode() with empty string
1822+
result = "".join(codecs.iterdecode(
1823+
codecs.iterencode("", encoding), encoding))
1824+
self.assertEqual(result, "")
1825+
1826+
if encoding not in ("idna", "mbcs"):
1827+
# check incremental decoder/encoder with errors argument
1828+
try:
1829+
encoder = codecs.getincrementalencoder(encoding)("ignore")
1830+
except LookupError: # no IncrementalEncoder
1831+
pass
1832+
else:
1833+
encodedresult = b"".join(encoder.encode(c) for c in s)
1834+
decoder = codecs.getincrementaldecoder(encoding)("ignore")
1835+
decodedresult = "".join(decoder.decode(bytes([c]))
1836+
for c in encodedresult)
1837+
self.assertEqual(decodedresult, s,
1838+
"encoding=%r" % encoding)
1839+
1840+
@support.cpython_only
1841+
def test_basics_capi(self):
1842+
from _testcapi import codec_incrementalencoder, codec_incrementaldecoder
1843+
s = "abc123" # all codecs should be able to encode these
1844+
for encoding in all_unicode_encodings:
1845+
if encoding not in broken_incremental_coders:
1846+
# check incremental decoder/encoder (fetched via the C API)
1847+
try:
1848+
cencoder = codec_incrementalencoder(encoding)
1849+
except LookupError: # no IncrementalEncoder
1850+
pass
1851+
else:
18181852
# check C API
18191853
encodedresult = b""
18201854
for c in s:
18211855
encodedresult += cencoder.encode(c)
18221856
encodedresult += cencoder.encode("", True)
1823-
cdecoder = _testcapi.codec_incrementaldecoder(encoding)
1857+
cdecoder = codec_incrementaldecoder(encoding)
18241858
decodedresult = ""
18251859
for c in encodedresult:
18261860
decodedresult += cdecoder.decode(bytes([c]))
18271861
decodedresult += cdecoder.decode(b"", True)
1828-
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
1829-
1830-
# check iterencode()/iterdecode()
1831-
result = "".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding))
1832-
self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding))
1833-
1834-
# check iterencode()/iterdecode() with empty string
1835-
result = "".join(codecs.iterdecode(codecs.iterencode("", encoding), encoding))
1836-
self.assertEqual(result, "")
1862+
self.assertEqual(decodedresult, s,
1863+
"encoding=%r" % encoding)
18371864

18381865
if encoding not in ("idna", "mbcs"):
18391866
# check incremental decoder/encoder with errors argument
18401867
try:
1841-
encoder = codecs.getincrementalencoder(encoding)("ignore")
1842-
cencoder = _testcapi.codec_incrementalencoder(encoding, "ignore")
1843-
except LookupError: # no IncrementalEncoder
1868+
cencoder = codec_incrementalencoder(encoding, "ignore")
1869+
except LookupError: # no IncrementalEncoder
18441870
pass
18451871
else:
1846-
encodedresult = b"".join(encoder.encode(c) for c in s)
1847-
decoder = codecs.getincrementaldecoder(encoding)("ignore")
1848-
decodedresult = "".join(decoder.decode(bytes([c])) for c in encodedresult)
1849-
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
1850-
18511872
encodedresult = b"".join(cencoder.encode(c) for c in s)
1852-
cdecoder = _testcapi.codec_incrementaldecoder(encoding, "ignore")
1853-
decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult)
1854-
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
1873+
cdecoder = codec_incrementaldecoder(encoding, "ignore")
1874+
decodedresult = "".join(cdecoder.decode(bytes([c]))
1875+
for c in encodedresult)
1876+
self.assertEqual(decodedresult, s,
1877+
"encoding=%r" % encoding)
18551878

18561879
def test_seek(self):
18571880
# all codecs should be able to encode these

Lib/test/test_descr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,7 @@ def setter(self_, value):
20502050
prop2 = property(fset=setter)
20512051
self.assertEqual(prop2.__doc__, None)
20522052

2053+
@support.cpython_only
20532054
def test_testcapi_no_segfault(self):
20542055
# this segfaulted in 2.5b2
20552056
try:

Lib/test/test_devpoll.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import select
88
import sys
99
import unittest
10-
from test.support import TESTFN, run_unittest
11-
from _testcapi import USHRT_MAX
10+
from test.support import TESTFN, run_unittest, cpython_only
1211

1312
try:
1413
select.devpoll
@@ -121,15 +120,24 @@ def test_fd_non_inheritable(self):
121120
self.addCleanup(devpoll.close)
122121
self.assertEqual(os.get_inheritable(devpoll.fileno()), False)
123122

124-
125123
def test_events_mask_overflow(self):
126124
pollster = select.devpoll()
127125
w, r = os.pipe()
128126
pollster.register(w)
129127
# Issue #17919
130128
self.assertRaises(OverflowError, pollster.register, 0, -1)
131-
self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1)
129+
self.assertRaises(OverflowError, pollster.register, 0, 1 << 64)
132130
self.assertRaises(OverflowError, pollster.modify, 1, -1)
131+
self.assertRaises(OverflowError, pollster.modify, 1, 1 << 64)
132+
133+
@cpython_only
134+
def test_events_mask_overflow_c_limits(self):
135+
from _testcapi import USHRT_MAX
136+
pollster = select.devpoll()
137+
w, r = os.pipe()
138+
pollster.register(w)
139+
# Issue #17919
140+
self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1)
133141
self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1)
134142

135143

Lib/test/test_exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ def g():
832832
self.assertIn("maximum recursion depth exceeded", str(v))
833833

834834

835+
@cpython_only
835836
def test_MemoryError(self):
836837
# PyErr_NoMemory always raises the same exception instance.
837838
# Check that the traceback is not doubled.
@@ -890,6 +891,7 @@ class C(object):
890891
self.assertEqual(error5.a, 1)
891892
self.assertEqual(error5.__doc__, "")
892893

894+
@cpython_only
893895
def test_memory_error_cleanup(self):
894896
# Issue #5437: preallocated MemoryError instances should not keep
895897
# traceback objects alive.

Lib/test/test_fcntl.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import os
55
import struct
66
import sys
7-
import _testcapi
87
import unittest
9-
from test.support import verbose, TESTFN, unlink, run_unittest, import_module
8+
from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
9+
cpython_only)
1010

1111
# Skip test if no fcntl module.
1212
fcntl = import_module('fcntl')
@@ -45,6 +45,12 @@ def get_lockdata():
4545

4646
lockdata = get_lockdata()
4747

48+
class BadFile:
49+
def __init__(self, fn):
50+
self.fn = fn
51+
def fileno(self):
52+
return self.fn
53+
4854
class TestFcntl(unittest.TestCase):
4955

5056
def setUp(self):
@@ -78,24 +84,27 @@ def test_fcntl_file_descriptor(self):
7884
self.f.close()
7985

8086
def test_fcntl_bad_file(self):
81-
class F:
82-
def __init__(self, fn):
83-
self.fn = fn
84-
def fileno(self):
85-
return self.fn
86-
self.assertRaises(ValueError, fcntl.fcntl, -1, fcntl.F_SETFL, os.O_NONBLOCK)
87-
self.assertRaises(ValueError, fcntl.fcntl, F(-1), fcntl.F_SETFL, os.O_NONBLOCK)
88-
self.assertRaises(TypeError, fcntl.fcntl, 'spam', fcntl.F_SETFL, os.O_NONBLOCK)
89-
self.assertRaises(TypeError, fcntl.fcntl, F('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
87+
with self.assertRaises(ValueError):
88+
fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
89+
with self.assertRaises(ValueError):
90+
fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
91+
with self.assertRaises(TypeError):
92+
fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
93+
with self.assertRaises(TypeError):
94+
fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
95+
96+
@cpython_only
97+
def test_fcntl_bad_file_overflow(self):
98+
from _testcapi import INT_MAX, INT_MIN
9099
# Issue 15989
91-
self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MAX + 1,
92-
fcntl.F_SETFL, os.O_NONBLOCK)
93-
self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MAX + 1),
94-
fcntl.F_SETFL, os.O_NONBLOCK)
95-
self.assertRaises(OverflowError, fcntl.fcntl, _testcapi.INT_MIN - 1,
96-
fcntl.F_SETFL, os.O_NONBLOCK)
97-
self.assertRaises(OverflowError, fcntl.fcntl, F(_testcapi.INT_MIN - 1),
98-
fcntl.F_SETFL, os.O_NONBLOCK)
100+
with self.assertRaises(OverflowError):
101+
fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
102+
with self.assertRaises(OverflowError):
103+
fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
104+
with self.assertRaises(OverflowError):
105+
fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
106+
with self.assertRaises(OverflowError):
107+
fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
99108

100109
@unittest.skipIf(
101110
platform.machine().startswith('arm') and platform.system() == 'Linux',
@@ -128,6 +137,10 @@ def test_flock(self):
128137

129138
self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
130139
self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
140+
141+
@cpython_only
142+
def test_flock_overflow(self):
143+
import _testcapi
131144
self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
132145
fcntl.LOCK_SH)
133146

0 commit comments

Comments
 (0)