ยปCore Development>Code coverage>Lib/test/test_memoryio.py

Python code coverage for Lib/test/test_memoryio.py

#countcontent
1n/a"""Unit tests for memory-based file-like objects.
2n/aStringIO -- for unicode strings
3n/aBytesIO -- for bytes
4n/a"""
5n/a
6n/aimport unittest
7n/afrom test import support
8n/a
9n/aimport io
10n/aimport _pyio as pyio
11n/aimport pickle
12n/aimport sys
13n/a
14n/aclass MemorySeekTestMixin:
15n/a
16n/a def testInit(self):
17n/a buf = self.buftype("1234567890")
18n/a bytesIo = self.ioclass(buf)
19n/a
20n/a def testRead(self):
21n/a buf = self.buftype("1234567890")
22n/a bytesIo = self.ioclass(buf)
23n/a
24n/a self.assertEqual(buf[:1], bytesIo.read(1))
25n/a self.assertEqual(buf[1:5], bytesIo.read(4))
26n/a self.assertEqual(buf[5:], bytesIo.read(900))
27n/a self.assertEqual(self.EOF, bytesIo.read())
28n/a
29n/a def testReadNoArgs(self):
30n/a buf = self.buftype("1234567890")
31n/a bytesIo = self.ioclass(buf)
32n/a
33n/a self.assertEqual(buf, bytesIo.read())
34n/a self.assertEqual(self.EOF, bytesIo.read())
35n/a
36n/a def testSeek(self):
37n/a buf = self.buftype("1234567890")
38n/a bytesIo = self.ioclass(buf)
39n/a
40n/a bytesIo.read(5)
41n/a bytesIo.seek(0)
42n/a self.assertEqual(buf, bytesIo.read())
43n/a
44n/a bytesIo.seek(3)
45n/a self.assertEqual(buf[3:], bytesIo.read())
46n/a self.assertRaises(TypeError, bytesIo.seek, 0.0)
47n/a
48n/a def testTell(self):
49n/a buf = self.buftype("1234567890")
50n/a bytesIo = self.ioclass(buf)
51n/a
52n/a self.assertEqual(0, bytesIo.tell())
53n/a bytesIo.seek(5)
54n/a self.assertEqual(5, bytesIo.tell())
55n/a bytesIo.seek(10000)
56n/a self.assertEqual(10000, bytesIo.tell())
57n/a
58n/a
59n/aclass MemoryTestMixin:
60n/a
61n/a def test_detach(self):
62n/a buf = self.ioclass()
63n/a self.assertRaises(self.UnsupportedOperation, buf.detach)
64n/a
65n/a def write_ops(self, f, t):
66n/a self.assertEqual(f.write(t("blah.")), 5)
67n/a self.assertEqual(f.seek(0), 0)
68n/a self.assertEqual(f.write(t("Hello.")), 6)
69n/a self.assertEqual(f.tell(), 6)
70n/a self.assertEqual(f.seek(5), 5)
71n/a self.assertEqual(f.tell(), 5)
72n/a self.assertEqual(f.write(t(" world\n\n\n")), 9)
73n/a self.assertEqual(f.seek(0), 0)
74n/a self.assertEqual(f.write(t("h")), 1)
75n/a self.assertEqual(f.truncate(12), 12)
76n/a self.assertEqual(f.tell(), 1)
77n/a
78n/a def test_write(self):
79n/a buf = self.buftype("hello world\n")
80n/a memio = self.ioclass(buf)
81n/a
82n/a self.write_ops(memio, self.buftype)
83n/a self.assertEqual(memio.getvalue(), buf)
84n/a memio = self.ioclass()
85n/a self.write_ops(memio, self.buftype)
86n/a self.assertEqual(memio.getvalue(), buf)
87n/a self.assertRaises(TypeError, memio.write, None)
88n/a memio.close()
89n/a self.assertRaises(ValueError, memio.write, self.buftype(""))
90n/a
91n/a def test_writelines(self):
92n/a buf = self.buftype("1234567890")
93n/a memio = self.ioclass()
94n/a
95n/a self.assertEqual(memio.writelines([buf] * 100), None)
96n/a self.assertEqual(memio.getvalue(), buf * 100)
97n/a memio.writelines([])
98n/a self.assertEqual(memio.getvalue(), buf * 100)
99n/a memio = self.ioclass()
100n/a self.assertRaises(TypeError, memio.writelines, [buf] + [1])
101n/a self.assertEqual(memio.getvalue(), buf)
102n/a self.assertRaises(TypeError, memio.writelines, None)
103n/a memio.close()
104n/a self.assertRaises(ValueError, memio.writelines, [])
105n/a
106n/a def test_writelines_error(self):
107n/a memio = self.ioclass()
108n/a def error_gen():
109n/a yield self.buftype('spam')
110n/a raise KeyboardInterrupt
111n/a
112n/a self.assertRaises(KeyboardInterrupt, memio.writelines, error_gen())
113n/a
114n/a def test_truncate(self):
115n/a buf = self.buftype("1234567890")
116n/a memio = self.ioclass(buf)
117n/a
118n/a self.assertRaises(ValueError, memio.truncate, -1)
119n/a memio.seek(6)
120n/a self.assertEqual(memio.truncate(), 6)
121n/a self.assertEqual(memio.getvalue(), buf[:6])
122n/a self.assertEqual(memio.truncate(4), 4)
123n/a self.assertEqual(memio.getvalue(), buf[:4])
124n/a self.assertEqual(memio.tell(), 6)
125n/a memio.seek(0, 2)
126n/a memio.write(buf)
127n/a self.assertEqual(memio.getvalue(), buf[:4] + buf)
128n/a pos = memio.tell()
129n/a self.assertEqual(memio.truncate(None), pos)
130n/a self.assertEqual(memio.tell(), pos)
131n/a self.assertRaises(TypeError, memio.truncate, '0')
132n/a memio.close()
133n/a self.assertRaises(ValueError, memio.truncate, 0)
134n/a
135n/a def test_init(self):
136n/a buf = self.buftype("1234567890")
137n/a memio = self.ioclass(buf)
138n/a self.assertEqual(memio.getvalue(), buf)
139n/a memio = self.ioclass(None)
140n/a self.assertEqual(memio.getvalue(), self.EOF)
141n/a memio.__init__(buf * 2)
142n/a self.assertEqual(memio.getvalue(), buf * 2)
143n/a memio.__init__(buf)
144n/a self.assertEqual(memio.getvalue(), buf)
145n/a self.assertRaises(TypeError, memio.__init__, [])
146n/a
147n/a def test_read(self):
148n/a buf = self.buftype("1234567890")
149n/a memio = self.ioclass(buf)
150n/a
151n/a self.assertEqual(memio.read(0), self.EOF)
152n/a self.assertEqual(memio.read(1), buf[:1])
153n/a self.assertEqual(memio.read(4), buf[1:5])
154n/a self.assertEqual(memio.read(900), buf[5:])
155n/a self.assertEqual(memio.read(), self.EOF)
156n/a memio.seek(0)
157n/a self.assertEqual(memio.read(), buf)
158n/a self.assertEqual(memio.read(), self.EOF)
159n/a self.assertEqual(memio.tell(), 10)
160n/a memio.seek(0)
161n/a self.assertEqual(memio.read(-1), buf)
162n/a memio.seek(0)
163n/a self.assertEqual(type(memio.read()), type(buf))
164n/a memio.seek(100)
165n/a self.assertEqual(type(memio.read()), type(buf))
166n/a memio.seek(0)
167n/a self.assertEqual(memio.read(None), buf)
168n/a self.assertRaises(TypeError, memio.read, '')
169n/a memio.seek(len(buf) + 1)
170n/a self.assertEqual(memio.read(1), self.EOF)
171n/a memio.seek(len(buf) + 1)
172n/a self.assertEqual(memio.read(), self.EOF)
173n/a memio.close()
174n/a self.assertRaises(ValueError, memio.read)
175n/a
176n/a def test_readline(self):
177n/a buf = self.buftype("1234567890\n")
178n/a memio = self.ioclass(buf * 2)
179n/a
180n/a self.assertEqual(memio.readline(0), self.EOF)
181n/a self.assertEqual(memio.readline(), buf)
182n/a self.assertEqual(memio.readline(), buf)
183n/a self.assertEqual(memio.readline(), self.EOF)
184n/a memio.seek(0)
185n/a self.assertEqual(memio.readline(5), buf[:5])
186n/a self.assertEqual(memio.readline(5), buf[5:10])
187n/a self.assertEqual(memio.readline(5), buf[10:15])
188n/a memio.seek(0)
189n/a self.assertEqual(memio.readline(-1), buf)
190n/a memio.seek(0)
191n/a self.assertEqual(memio.readline(0), self.EOF)
192n/a # Issue #24989: Buffer overread
193n/a memio.seek(len(buf) * 2 + 1)
194n/a self.assertEqual(memio.readline(), self.EOF)
195n/a
196n/a buf = self.buftype("1234567890\n")
197n/a memio = self.ioclass((buf * 3)[:-1])
198n/a self.assertEqual(memio.readline(), buf)
199n/a self.assertEqual(memio.readline(), buf)
200n/a self.assertEqual(memio.readline(), buf[:-1])
201n/a self.assertEqual(memio.readline(), self.EOF)
202n/a memio.seek(0)
203n/a self.assertEqual(type(memio.readline()), type(buf))
204n/a self.assertEqual(memio.readline(), buf)
205n/a self.assertRaises(TypeError, memio.readline, '')
206n/a memio.close()
207n/a self.assertRaises(ValueError, memio.readline)
208n/a
209n/a def test_readlines(self):
210n/a buf = self.buftype("1234567890\n")
211n/a memio = self.ioclass(buf * 10)
212n/a
213n/a self.assertEqual(memio.readlines(), [buf] * 10)
214n/a memio.seek(5)
215n/a self.assertEqual(memio.readlines(), [buf[5:]] + [buf] * 9)
216n/a memio.seek(0)
217n/a self.assertEqual(memio.readlines(15), [buf] * 2)
218n/a memio.seek(0)
219n/a self.assertEqual(memio.readlines(-1), [buf] * 10)
220n/a memio.seek(0)
221n/a self.assertEqual(memio.readlines(0), [buf] * 10)
222n/a memio.seek(0)
223n/a self.assertEqual(type(memio.readlines()[0]), type(buf))
224n/a memio.seek(0)
225n/a self.assertEqual(memio.readlines(None), [buf] * 10)
226n/a self.assertRaises(TypeError, memio.readlines, '')
227n/a # Issue #24989: Buffer overread
228n/a memio.seek(len(buf) * 10 + 1)
229n/a self.assertEqual(memio.readlines(), [])
230n/a memio.close()
231n/a self.assertRaises(ValueError, memio.readlines)
232n/a
233n/a def test_iterator(self):
234n/a buf = self.buftype("1234567890\n")
235n/a memio = self.ioclass(buf * 10)
236n/a
237n/a self.assertEqual(iter(memio), memio)
238n/a self.assertTrue(hasattr(memio, '__iter__'))
239n/a self.assertTrue(hasattr(memio, '__next__'))
240n/a i = 0
241n/a for line in memio:
242n/a self.assertEqual(line, buf)
243n/a i += 1
244n/a self.assertEqual(i, 10)
245n/a memio.seek(0)
246n/a i = 0
247n/a for line in memio:
248n/a self.assertEqual(line, buf)
249n/a i += 1
250n/a self.assertEqual(i, 10)
251n/a # Issue #24989: Buffer overread
252n/a memio.seek(len(buf) * 10 + 1)
253n/a self.assertEqual(list(memio), [])
254n/a memio = self.ioclass(buf * 2)
255n/a memio.close()
256n/a self.assertRaises(ValueError, memio.__next__)
257n/a
258n/a def test_getvalue(self):
259n/a buf = self.buftype("1234567890")
260n/a memio = self.ioclass(buf)
261n/a
262n/a self.assertEqual(memio.getvalue(), buf)
263n/a memio.read()
264n/a self.assertEqual(memio.getvalue(), buf)
265n/a self.assertEqual(type(memio.getvalue()), type(buf))
266n/a memio = self.ioclass(buf * 1000)
267n/a self.assertEqual(memio.getvalue()[-3:], self.buftype("890"))
268n/a memio = self.ioclass(buf)
269n/a memio.close()
270n/a self.assertRaises(ValueError, memio.getvalue)
271n/a
272n/a def test_seek(self):
273n/a buf = self.buftype("1234567890")
274n/a memio = self.ioclass(buf)
275n/a
276n/a memio.read(5)
277n/a self.assertRaises(ValueError, memio.seek, -1)
278n/a self.assertRaises(ValueError, memio.seek, 1, -1)
279n/a self.assertRaises(ValueError, memio.seek, 1, 3)
280n/a self.assertEqual(memio.seek(0), 0)
281n/a self.assertEqual(memio.seek(0, 0), 0)
282n/a self.assertEqual(memio.read(), buf)
283n/a self.assertEqual(memio.seek(3), 3)
284n/a self.assertEqual(memio.seek(0, 1), 3)
285n/a self.assertEqual(memio.read(), buf[3:])
286n/a self.assertEqual(memio.seek(len(buf)), len(buf))
287n/a self.assertEqual(memio.read(), self.EOF)
288n/a memio.seek(len(buf) + 1)
289n/a self.assertEqual(memio.read(), self.EOF)
290n/a self.assertEqual(memio.seek(0, 2), len(buf))
291n/a self.assertEqual(memio.read(), self.EOF)
292n/a memio.close()
293n/a self.assertRaises(ValueError, memio.seek, 0)
294n/a
295n/a def test_overseek(self):
296n/a buf = self.buftype("1234567890")
297n/a memio = self.ioclass(buf)
298n/a
299n/a self.assertEqual(memio.seek(len(buf) + 1), 11)
300n/a self.assertEqual(memio.read(), self.EOF)
301n/a self.assertEqual(memio.tell(), 11)
302n/a self.assertEqual(memio.getvalue(), buf)
303n/a memio.write(self.EOF)
304n/a self.assertEqual(memio.getvalue(), buf)
305n/a memio.write(buf)
306n/a self.assertEqual(memio.getvalue(), buf + self.buftype('\0') + buf)
307n/a
308n/a def test_tell(self):
309n/a buf = self.buftype("1234567890")
310n/a memio = self.ioclass(buf)
311n/a
312n/a self.assertEqual(memio.tell(), 0)
313n/a memio.seek(5)
314n/a self.assertEqual(memio.tell(), 5)
315n/a memio.seek(10000)
316n/a self.assertEqual(memio.tell(), 10000)
317n/a memio.close()
318n/a self.assertRaises(ValueError, memio.tell)
319n/a
320n/a def test_flush(self):
321n/a buf = self.buftype("1234567890")
322n/a memio = self.ioclass(buf)
323n/a
324n/a self.assertEqual(memio.flush(), None)
325n/a
326n/a def test_flags(self):
327n/a memio = self.ioclass()
328n/a
329n/a self.assertEqual(memio.writable(), True)
330n/a self.assertEqual(memio.readable(), True)
331n/a self.assertEqual(memio.seekable(), True)
332n/a self.assertEqual(memio.isatty(), False)
333n/a self.assertEqual(memio.closed, False)
334n/a memio.close()
335n/a self.assertRaises(ValueError, memio.writable)
336n/a self.assertRaises(ValueError, memio.readable)
337n/a self.assertRaises(ValueError, memio.seekable)
338n/a self.assertRaises(ValueError, memio.isatty)
339n/a self.assertEqual(memio.closed, True)
340n/a
341n/a def test_subclassing(self):
342n/a buf = self.buftype("1234567890")
343n/a def test1():
344n/a class MemIO(self.ioclass):
345n/a pass
346n/a m = MemIO(buf)
347n/a return m.getvalue()
348n/a def test2():
349n/a class MemIO(self.ioclass):
350n/a def __init__(me, a, b):
351n/a self.ioclass.__init__(me, a)
352n/a m = MemIO(buf, None)
353n/a return m.getvalue()
354n/a self.assertEqual(test1(), buf)
355n/a self.assertEqual(test2(), buf)
356n/a
357n/a def test_instance_dict_leak(self):
358n/a # Test case for issue #6242.
359n/a # This will be caught by regrtest.py -R if this leak.
360n/a for _ in range(100):
361n/a memio = self.ioclass()
362n/a memio.foo = 1
363n/a
364n/a def test_pickling(self):
365n/a buf = self.buftype("1234567890")
366n/a memio = self.ioclass(buf)
367n/a memio.foo = 42
368n/a memio.seek(2)
369n/a
370n/a class PickleTestMemIO(self.ioclass):
371n/a def __init__(me, initvalue, foo):
372n/a self.ioclass.__init__(me, initvalue)
373n/a me.foo = foo
374n/a # __getnewargs__ is undefined on purpose. This checks that PEP 307
375n/a # is used to provide pickling support.
376n/a
377n/a # Pickle expects the class to be on the module level. Here we use a
378n/a # little hack to allow the PickleTestMemIO class to derive from
379n/a # self.ioclass without having to define all combinations explicitly on
380n/a # the module-level.
381n/a import __main__
382n/a PickleTestMemIO.__module__ = '__main__'
383n/a PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__
384n/a __main__.PickleTestMemIO = PickleTestMemIO
385n/a submemio = PickleTestMemIO(buf, 80)
386n/a submemio.seek(2)
387n/a
388n/a # We only support pickle protocol 2 and onward since we use extended
389n/a # __reduce__ API of PEP 307 to provide pickling support.
390n/a for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
391n/a for obj in (memio, submemio):
392n/a obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))
393n/a self.assertEqual(obj.getvalue(), obj2.getvalue())
394n/a self.assertEqual(obj.__class__, obj2.__class__)
395n/a self.assertEqual(obj.foo, obj2.foo)
396n/a self.assertEqual(obj.tell(), obj2.tell())
397n/a obj2.close()
398n/a self.assertRaises(ValueError, pickle.dumps, obj2, proto)
399n/a del __main__.PickleTestMemIO
400n/a
401n/a
402n/aclass PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
403n/a # Test _pyio.BytesIO; class also inherited for testing C implementation
404n/a
405n/a UnsupportedOperation = pyio.UnsupportedOperation
406n/a
407n/a @staticmethod
408n/a def buftype(s):
409n/a return s.encode("ascii")
410n/a ioclass = pyio.BytesIO
411n/a EOF = b""
412n/a
413n/a def test_getbuffer(self):
414n/a memio = self.ioclass(b"1234567890")
415n/a buf = memio.getbuffer()
416n/a self.assertEqual(bytes(buf), b"1234567890")
417n/a memio.seek(5)
418n/a buf = memio.getbuffer()
419n/a self.assertEqual(bytes(buf), b"1234567890")
420n/a # Trying to change the size of the BytesIO while a buffer is exported
421n/a # raises a BufferError.
422n/a self.assertRaises(BufferError, memio.write, b'x' * 100)
423n/a self.assertRaises(BufferError, memio.truncate)
424n/a self.assertRaises(BufferError, memio.close)
425n/a self.assertFalse(memio.closed)
426n/a # Mutating the buffer updates the BytesIO
427n/a buf[3:6] = b"abc"
428n/a self.assertEqual(bytes(buf), b"123abc7890")
429n/a self.assertEqual(memio.getvalue(), b"123abc7890")
430n/a # After the buffer gets released, we can resize and close the BytesIO
431n/a # again
432n/a del buf
433n/a support.gc_collect()
434n/a memio.truncate()
435n/a memio.close()
436n/a self.assertRaises(ValueError, memio.getbuffer)
437n/a
438n/a def test_read1(self):
439n/a buf = self.buftype("1234567890")
440n/a self.assertEqual(self.ioclass(buf).read1(), buf)
441n/a self.assertEqual(self.ioclass(buf).read1(-1), buf)
442n/a
443n/a def test_readinto(self):
444n/a buf = self.buftype("1234567890")
445n/a memio = self.ioclass(buf)
446n/a
447n/a b = bytearray(b"hello")
448n/a self.assertEqual(memio.readinto(b), 5)
449n/a self.assertEqual(b, b"12345")
450n/a self.assertEqual(memio.readinto(b), 5)
451n/a self.assertEqual(b, b"67890")
452n/a self.assertEqual(memio.readinto(b), 0)
453n/a self.assertEqual(b, b"67890")
454n/a b = bytearray(b"hello world")
455n/a memio.seek(0)
456n/a self.assertEqual(memio.readinto(b), 10)
457n/a self.assertEqual(b, b"1234567890d")
458n/a b = bytearray(b"")
459n/a memio.seek(0)
460n/a self.assertEqual(memio.readinto(b), 0)
461n/a self.assertEqual(b, b"")
462n/a self.assertRaises(TypeError, memio.readinto, '')
463n/a import array
464n/a a = array.array('b', b"hello world")
465n/a memio = self.ioclass(buf)
466n/a memio.readinto(a)
467n/a self.assertEqual(a.tobytes(), b"1234567890d")
468n/a memio.close()
469n/a self.assertRaises(ValueError, memio.readinto, b)
470n/a memio = self.ioclass(b"123")
471n/a b = bytearray()
472n/a memio.seek(42)
473n/a memio.readinto(b)
474n/a self.assertEqual(b, b"")
475n/a
476n/a def test_relative_seek(self):
477n/a buf = self.buftype("1234567890")
478n/a memio = self.ioclass(buf)
479n/a
480n/a self.assertEqual(memio.seek(-1, 1), 0)
481n/a self.assertEqual(memio.seek(3, 1), 3)
482n/a self.assertEqual(memio.seek(-4, 1), 0)
483n/a self.assertEqual(memio.seek(-1, 2), 9)
484n/a self.assertEqual(memio.seek(1, 1), 10)
485n/a self.assertEqual(memio.seek(1, 2), 11)
486n/a memio.seek(-3, 2)
487n/a self.assertEqual(memio.read(), buf[-3:])
488n/a memio.seek(0)
489n/a memio.seek(1, 1)
490n/a self.assertEqual(memio.read(), buf[1:])
491n/a
492n/a def test_unicode(self):
493n/a memio = self.ioclass()
494n/a
495n/a self.assertRaises(TypeError, self.ioclass, "1234567890")
496n/a self.assertRaises(TypeError, memio.write, "1234567890")
497n/a self.assertRaises(TypeError, memio.writelines, ["1234567890"])
498n/a
499n/a def test_bytes_array(self):
500n/a buf = b"1234567890"
501n/a import array
502n/a a = array.array('b', list(buf))
503n/a memio = self.ioclass(a)
504n/a self.assertEqual(memio.getvalue(), buf)
505n/a self.assertEqual(memio.write(a), 10)
506n/a self.assertEqual(memio.getvalue(), buf)
507n/a
508n/a def test_issue5449(self):
509n/a buf = self.buftype("1234567890")
510n/a self.ioclass(initial_bytes=buf)
511n/a self.assertRaises(TypeError, self.ioclass, buf, foo=None)
512n/a
513n/a
514n/aclass TextIOTestMixin:
515n/a
516n/a def test_newlines_property(self):
517n/a memio = self.ioclass(newline=None)
518n/a # The C StringIO decodes newlines in write() calls, but the Python
519n/a # implementation only does when reading. This function forces them to
520n/a # be decoded for testing.
521n/a def force_decode():
522n/a memio.seek(0)
523n/a memio.read()
524n/a self.assertEqual(memio.newlines, None)
525n/a memio.write("a\n")
526n/a force_decode()
527n/a self.assertEqual(memio.newlines, "\n")
528n/a memio.write("b\r\n")
529n/a force_decode()
530n/a self.assertEqual(memio.newlines, ("\n", "\r\n"))
531n/a memio.write("c\rd")
532n/a force_decode()
533n/a self.assertEqual(memio.newlines, ("\r", "\n", "\r\n"))
534n/a
535n/a def test_relative_seek(self):
536n/a memio = self.ioclass()
537n/a
538n/a self.assertRaises(OSError, memio.seek, -1, 1)
539n/a self.assertRaises(OSError, memio.seek, 3, 1)
540n/a self.assertRaises(OSError, memio.seek, -3, 1)
541n/a self.assertRaises(OSError, memio.seek, -1, 2)
542n/a self.assertRaises(OSError, memio.seek, 1, 1)
543n/a self.assertRaises(OSError, memio.seek, 1, 2)
544n/a
545n/a def test_textio_properties(self):
546n/a memio = self.ioclass()
547n/a
548n/a # These are just dummy values but we nevertheless check them for fear
549n/a # of unexpected breakage.
550n/a self.assertIsNone(memio.encoding)
551n/a self.assertIsNone(memio.errors)
552n/a self.assertFalse(memio.line_buffering)
553n/a
554n/a def test_newline_default(self):
555n/a memio = self.ioclass("a\nb\r\nc\rd")
556n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
557n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
558n/a
559n/a memio = self.ioclass()
560n/a self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
561n/a memio.seek(0)
562n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
563n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
564n/a
565n/a def test_newline_none(self):
566n/a # newline=None
567n/a memio = self.ioclass("a\nb\r\nc\rd", newline=None)
568n/a self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"])
569n/a memio.seek(0)
570n/a self.assertEqual(memio.read(1), "a")
571n/a self.assertEqual(memio.read(2), "\nb")
572n/a self.assertEqual(memio.read(2), "\nc")
573n/a self.assertEqual(memio.read(1), "\n")
574n/a self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
575n/a
576n/a memio = self.ioclass(newline=None)
577n/a self.assertEqual(2, memio.write("a\n"))
578n/a self.assertEqual(3, memio.write("b\r\n"))
579n/a self.assertEqual(3, memio.write("c\rd"))
580n/a memio.seek(0)
581n/a self.assertEqual(memio.read(), "a\nb\nc\nd")
582n/a self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
583n/a
584n/a memio = self.ioclass("a\r\nb", newline=None)
585n/a self.assertEqual(memio.read(3), "a\nb")
586n/a
587n/a def test_newline_empty(self):
588n/a # newline=""
589n/a memio = self.ioclass("a\nb\r\nc\rd", newline="")
590n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
591n/a memio.seek(0)
592n/a self.assertEqual(memio.read(4), "a\nb\r")
593n/a self.assertEqual(memio.read(2), "\nc")
594n/a self.assertEqual(memio.read(1), "\r")
595n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
596n/a
597n/a memio = self.ioclass(newline="")
598n/a self.assertEqual(2, memio.write("a\n"))
599n/a self.assertEqual(2, memio.write("b\r"))
600n/a self.assertEqual(2, memio.write("\nc"))
601n/a self.assertEqual(2, memio.write("\rd"))
602n/a memio.seek(0)
603n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
604n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
605n/a
606n/a def test_newline_lf(self):
607n/a # newline="\n"
608n/a memio = self.ioclass("a\nb\r\nc\rd", newline="\n")
609n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
610n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
611n/a
612n/a memio = self.ioclass(newline="\n")
613n/a self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
614n/a memio.seek(0)
615n/a self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
616n/a self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
617n/a
618n/a def test_newline_cr(self):
619n/a # newline="\r"
620n/a memio = self.ioclass("a\nb\r\nc\rd", newline="\r")
621n/a self.assertEqual(memio.read(), "a\rb\r\rc\rd")
622n/a memio.seek(0)
623n/a self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
624n/a self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
625n/a
626n/a memio = self.ioclass(newline="\r")
627n/a self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
628n/a memio.seek(0)
629n/a self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
630n/a memio.seek(0)
631n/a self.assertEqual(memio.readlines(), ["a\r", "b\r", "\r", "c\r", "d"])
632n/a self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
633n/a
634n/a def test_newline_crlf(self):
635n/a # newline="\r\n"
636n/a memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n")
637n/a self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd")
638n/a memio.seek(0)
639n/a self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
640n/a memio.seek(0)
641n/a self.assertEqual(memio.readlines(), ["a\r\n", "b\r\r\n", "c\rd"])
642n/a self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
643n/a
644n/a memio = self.ioclass(newline="\r\n")
645n/a self.assertEqual(memio.write("a\nb\r\nc\rd"), 8)
646n/a memio.seek(0)
647n/a self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
648n/a self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
649n/a
650n/a def test_issue5265(self):
651n/a # StringIO can duplicate newlines in universal newlines mode
652n/a memio = self.ioclass("a\r\nb\r\n", newline=None)
653n/a self.assertEqual(memio.read(5), "a\nb\n")
654n/a self.assertEqual(memio.getvalue(), "a\nb\n")
655n/a
656n/a def test_newline_argument(self):
657n/a self.assertRaises(TypeError, self.ioclass, newline=b"\n")
658n/a self.assertRaises(ValueError, self.ioclass, newline="error")
659n/a # These should not raise an error
660n/a for newline in (None, "", "\n", "\r", "\r\n"):
661n/a self.ioclass(newline=newline)
662n/a
663n/a
664n/aclass PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin,
665n/a TextIOTestMixin, unittest.TestCase):
666n/a buftype = str
667n/a ioclass = pyio.StringIO
668n/a UnsupportedOperation = pyio.UnsupportedOperation
669n/a EOF = ""
670n/a
671n/a def test_lone_surrogates(self):
672n/a # Issue #20424
673n/a memio = self.ioclass('\ud800')
674n/a self.assertEqual(memio.read(), '\ud800')
675n/a
676n/a memio = self.ioclass()
677n/a memio.write('\ud800')
678n/a self.assertEqual(memio.getvalue(), '\ud800')
679n/a
680n/a
681n/aclass PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
682n/a """Test if pickle restores properly the internal state of StringIO.
683n/a """
684n/a buftype = str
685n/a UnsupportedOperation = pyio.UnsupportedOperation
686n/a EOF = ""
687n/a
688n/a class ioclass(pyio.StringIO):
689n/a def __new__(cls, *args, **kwargs):
690n/a return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs)))
691n/a def __init__(self, *args, **kwargs):
692n/a pass
693n/a
694n/a
695n/aclass CBytesIOTest(PyBytesIOTest):
696n/a ioclass = io.BytesIO
697n/a UnsupportedOperation = io.UnsupportedOperation
698n/a
699n/a def test_getstate(self):
700n/a memio = self.ioclass()
701n/a state = memio.__getstate__()
702n/a self.assertEqual(len(state), 3)
703n/a bytearray(state[0]) # Check if state[0] supports the buffer interface.
704n/a self.assertIsInstance(state[1], int)
705n/a if state[2] is not None:
706n/a self.assertIsInstance(state[2], dict)
707n/a memio.close()
708n/a self.assertRaises(ValueError, memio.__getstate__)
709n/a
710n/a def test_setstate(self):
711n/a # This checks whether __setstate__ does proper input validation.
712n/a memio = self.ioclass()
713n/a memio.__setstate__((b"no error", 0, None))
714n/a memio.__setstate__((bytearray(b"no error"), 0, None))
715n/a memio.__setstate__((b"no error", 0, {'spam': 3}))
716n/a self.assertRaises(ValueError, memio.__setstate__, (b"", -1, None))
717n/a self.assertRaises(TypeError, memio.__setstate__, ("unicode", 0, None))
718n/a self.assertRaises(TypeError, memio.__setstate__, (b"", 0.0, None))
719n/a self.assertRaises(TypeError, memio.__setstate__, (b"", 0, 0))
720n/a self.assertRaises(TypeError, memio.__setstate__, (b"len-test", 0))
721n/a self.assertRaises(TypeError, memio.__setstate__)
722n/a self.assertRaises(TypeError, memio.__setstate__, 0)
723n/a memio.close()
724n/a self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None))
725n/a
726n/a check_sizeof = support.check_sizeof
727n/a
728n/a @support.cpython_only
729n/a def test_sizeof(self):
730n/a basesize = support.calcobjsize('P2n2Pn')
731n/a check = self.check_sizeof
732n/a self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
733n/a check(io.BytesIO(), basesize )
734n/a check(io.BytesIO(b'a' * 1000), basesize + sys.getsizeof(b'a' * 1000))
735n/a
736n/a # Various tests of copy-on-write behaviour for BytesIO.
737n/a
738n/a def _test_cow_mutation(self, mutation):
739n/a # Common code for all BytesIO copy-on-write mutation tests.
740n/a imm = b' ' * 1024
741n/a old_rc = sys.getrefcount(imm)
742n/a memio = self.ioclass(imm)
743n/a self.assertEqual(sys.getrefcount(imm), old_rc + 1)
744n/a mutation(memio)
745n/a self.assertEqual(sys.getrefcount(imm), old_rc)
746n/a
747n/a @support.cpython_only
748n/a def test_cow_truncate(self):
749n/a # Ensure truncate causes a copy.
750n/a def mutation(memio):
751n/a memio.truncate(1)
752n/a self._test_cow_mutation(mutation)
753n/a
754n/a @support.cpython_only
755n/a def test_cow_write(self):
756n/a # Ensure write that would not cause a resize still results in a copy.
757n/a def mutation(memio):
758n/a memio.seek(0)
759n/a memio.write(b'foo')
760n/a self._test_cow_mutation(mutation)
761n/a
762n/a @support.cpython_only
763n/a def test_cow_setstate(self):
764n/a # __setstate__ should cause buffer to be released.
765n/a memio = self.ioclass(b'foooooo')
766n/a state = memio.__getstate__()
767n/a def mutation(memio):
768n/a memio.__setstate__(state)
769n/a self._test_cow_mutation(mutation)
770n/a
771n/a @support.cpython_only
772n/a def test_cow_mutable(self):
773n/a # BytesIO should accept only Bytes for copy-on-write sharing, since
774n/a # arbitrary buffer-exporting objects like bytearray() aren't guaranteed
775n/a # to be immutable.
776n/a ba = bytearray(1024)
777n/a old_rc = sys.getrefcount(ba)
778n/a memio = self.ioclass(ba)
779n/a self.assertEqual(sys.getrefcount(ba), old_rc)
780n/a
781n/aclass CStringIOTest(PyStringIOTest):
782n/a ioclass = io.StringIO
783n/a UnsupportedOperation = io.UnsupportedOperation
784n/a
785n/a # XXX: For the Python version of io.StringIO, this is highly
786n/a # dependent on the encoding used for the underlying buffer.
787n/a def test_widechar(self):
788n/a buf = self.buftype("\U0002030a\U00020347")
789n/a memio = self.ioclass(buf)
790n/a
791n/a self.assertEqual(memio.getvalue(), buf)
792n/a self.assertEqual(memio.write(buf), len(buf))
793n/a self.assertEqual(memio.tell(), len(buf))
794n/a self.assertEqual(memio.getvalue(), buf)
795n/a self.assertEqual(memio.write(buf), len(buf))
796n/a self.assertEqual(memio.tell(), len(buf) * 2)
797n/a self.assertEqual(memio.getvalue(), buf + buf)
798n/a
799n/a def test_getstate(self):
800n/a memio = self.ioclass()
801n/a state = memio.__getstate__()
802n/a self.assertEqual(len(state), 4)
803n/a self.assertIsInstance(state[0], str)
804n/a self.assertIsInstance(state[1], str)
805n/a self.assertIsInstance(state[2], int)
806n/a if state[3] is not None:
807n/a self.assertIsInstance(state[3], dict)
808n/a memio.close()
809n/a self.assertRaises(ValueError, memio.__getstate__)
810n/a
811n/a def test_setstate(self):
812n/a # This checks whether __setstate__ does proper input validation.
813n/a memio = self.ioclass()
814n/a memio.__setstate__(("no error", "\n", 0, None))
815n/a memio.__setstate__(("no error", "", 0, {'spam': 3}))
816n/a self.assertRaises(ValueError, memio.__setstate__, ("", "f", 0, None))
817n/a self.assertRaises(ValueError, memio.__setstate__, ("", "", -1, None))
818n/a self.assertRaises(TypeError, memio.__setstate__, (b"", "", 0, None))
819n/a self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None))
820n/a self.assertRaises(TypeError, memio.__setstate__, ("", "", 0.0, None))
821n/a self.assertRaises(TypeError, memio.__setstate__, ("", "", 0, 0))
822n/a self.assertRaises(TypeError, memio.__setstate__, ("len-test", 0))
823n/a self.assertRaises(TypeError, memio.__setstate__)
824n/a self.assertRaises(TypeError, memio.__setstate__, 0)
825n/a memio.close()
826n/a self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None))
827n/a
828n/a
829n/aclass CStringIOPickleTest(PyStringIOPickleTest):
830n/a UnsupportedOperation = io.UnsupportedOperation
831n/a
832n/a class ioclass(io.StringIO):
833n/a def __new__(cls, *args, **kwargs):
834n/a return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs)))
835n/a def __init__(self, *args, **kwargs):
836n/a pass
837n/a
838n/a
839n/aif __name__ == '__main__':
840n/a unittest.main()