diff -r 3c29d05c0710 Lib/test/test_coroutines.py --- a/Lib/test/test_coroutines.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_coroutines.py Sat Sep 05 13:33:52 2015 +0300 @@ -1,5 +1,7 @@ import contextlib +import copy import inspect +import pickle import sys import types import unittest @@ -1318,6 +1320,28 @@ class CoroutineTest(unittest.TestCase): run_async(foo()) self.assertEqual(CNT, 0) + def test_copy(self): + async def func(): pass + coro = func() + with self.assertRaises(TypeError): + copy.copy(coro) + aw = coro.__await__() + with self.assertRaises(TypeError): + copy.copy(aw) + aw.close() + + def test_pickle(self): + async def func(): pass + coro = func() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(coro, proto) + aw = coro.__await__() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(aw, proto) + aw.close() + class CoroAsyncIOCompatTest(unittest.TestCase): diff -r 3c29d05c0710 Lib/test/test_csv.py --- a/Lib/test/test_csv.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_csv.py Sat Sep 05 13:33:52 2015 +0300 @@ -1,6 +1,7 @@ # Copyright (C) 2001,2002 Python Software Foundation # csv package unit tests +import copy import io import sys import os @@ -9,6 +10,7 @@ from io import StringIO from tempfile import TemporaryFile import csv import gc +import pickle from test import support class Test_Csv(unittest.TestCase): @@ -424,6 +426,17 @@ class TestDialectRegistry(unittest.TestC self.assertRaises(TypeError, csv.reader, [], quoting = -1) self.assertRaises(TypeError, csv.reader, [], quoting = 100) + def test_copy(self): + for name in csv.list_dialects(): + dialect = csv.get_dialect(name) + self.assertRaises(TypeError, copy.copy, dialect) + + def test_pickle(self): + for name in csv.list_dialects(): + dialect = csv.get_dialect(name) + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + self.assertRaises(TypeError, pickle.dumps, dialect, proto) + class TestCsvBase(unittest.TestCase): def readerAssertEqual(self, input, expected_result): with TemporaryFile("w+", newline='') as fileobj: diff -r 3c29d05c0710 Lib/test/test_dictviews.py --- a/Lib/test/test_dictviews.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_dictviews.py Sat Sep 05 13:33:52 2015 +0300 @@ -1,4 +1,6 @@ import collections +import copy +import pickle import unittest class DictSetTest(unittest.TestCase): @@ -198,6 +200,20 @@ class DictSetTest(unittest.TestCase): d[42] = d.values() self.assertRaises(RecursionError, repr, d) + def test_pickle(self): + d = {1: 10, "a": "ABC"} + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + self.assertRaises(TypeError, pickle.dumps, d.keys(), proto) + self.assertRaises(TypeError, pickle.dumps, d.values(), proto) + self.assertRaises(TypeError, pickle.dumps, d.items(), proto) + + def test_copy(self): + d = {1: 10, "a": "ABC"} + self.assertRaises(TypeError, copy.copy, d.keys()) + self.assertRaises(TypeError, copy.copy, d.values()) + self.assertRaises(TypeError, copy.copy, d.items()) + + def test_abc_registry(self): d = dict(a=1) diff -r 3c29d05c0710 Lib/test/test_generators.py --- a/Lib/test/test_generators.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_generators.py Sat Sep 05 13:33:52 2015 +0300 @@ -1,4 +1,6 @@ +import copy import gc +import pickle import sys import unittest import warnings @@ -260,6 +262,21 @@ class ExceptionTest(unittest.TestCase): # hence no warning. next(g) + def test_copy(self): + def f(): + yield 1 + g = f() + with self.assertRaises(TypeError): + copy.copy(g) + + def test_pickle(self): + def f(): + yield 1 + g = f() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(g, proto) + class YieldFromTests(unittest.TestCase): def test_generator_gi_yieldfrom(self): diff -r 3c29d05c0710 Lib/test/test_io.py --- a/Lib/test/test_io.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_io.py Sat Sep 05 13:33:52 2015 +0300 @@ -21,6 +21,7 @@ import abc import array +import copy import errno import locale import os diff -r 3c29d05c0710 Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Fri Sep 04 23:57:25 2015 +0200 +++ b/Lib/test/test_xml_etree.py Sat Sep 05 13:33:52 2015 +0300 @@ -5,6 +5,7 @@ # For this purpose, the module-level "ET" symbol is temporarily # monkey-patched when running the "test_xml_etree_c" test suite. +import copy import html import io import operator @@ -2082,6 +2083,19 @@ class ElementIterTest(unittest.TestCase) self.assertEqual(self._ilist(doc), all_tags) self.assertEqual(self._ilist(doc, '*'), all_tags) + def test_copy(self): + a = ET.Element('a') + it = a.iter() + with self.assertRaises(TypeError): + copy.copy(it) + + def test_pickle(self): + a = ET.Element('a') + it = a.iter() + for proto in range(pickle.HIGHEST_PROTOCOL + 1): + with self.assertRaises((TypeError, pickle.PicklingError)): + pickle.dumps(it, proto) + class TreeBuilderTest(unittest.TestCase): sample1 = ('tp_base) + if (!PyType_HasFeature(t, Py_TPFLAGS_HEAPTYPE)) + return 0; + return 1; +} + static PyObject * reduce_newobj(PyObject *obj, int proto) { @@ -4049,10 +4059,28 @@ reduce_newobj(PyObject *obj, int proto) PyObject *newobj, *newargs, *state, *listitems, *dictitems; PyObject *result; + if (Py_TYPE(obj)->tp_new == NULL) { + PyErr_Format(PyExc_TypeError, + "can't pickle %s objects", + Py_TYPE(obj)->tp_name); + return NULL; + } if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0) return NULL; if (args == NULL) { + if (!PyList_Check(obj) && !PyDict_Check(obj) && + !allheaptypes(Py_TYPE(obj))) { + _Py_IDENTIFIER(__getstate__); + PyObject *getstate = _PyObject_LookupSpecial(obj, &PyId___getstate__); + if (getstate == NULL) { + PyErr_Format(PyExc_TypeError, + "can't pickle %s objects", + Py_TYPE(obj)->tp_name); + return NULL; + } + Py_DECREF(getstate); + } args = PyTuple_New(0); if (args == NULL) { Py_XDECREF(kwargs);