Commit 2eee8416 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge commit '5fd4fe' into refcounting

parents 586ceed6 5fd4fe6e
......@@ -673,7 +673,7 @@ public:
// For instances with hc attrs:
size_t getHCAttrsOffset();
HCAttrs* getHCAttrsPtr();
void setDictBacked(Box* d);
void setDictBacked(STOLEN(Box*) d);
// For instances with dict attrs:
BoxedDict* getDict();
void setDict(BoxedDict* d);
......
......@@ -6465,14 +6465,26 @@ Box* _typeNew(BoxedClass* metatype, BoxedString* name, BoxedTuple* bases, BoxedD
made->setattr(dict_str, dict_descr, NULL);
}
bool are_all_dict_keys_strs = true;
for (const auto& p : *attr_dict) {
auto k = coerceUnicodeToStr<CXX>(p.first);
RELEASE_ASSERT(k->cls == str_cls, "%s", k->cls->tp_name);
BoxedString* s = static_cast<BoxedString*>(k);
if (p.first->cls != str_cls) {
are_all_dict_keys_strs = false;
break;
}
}
if (are_all_dict_keys_strs) {
for (const auto& p : *attr_dict) {
BoxedString* s = static_cast<BoxedString*>(p.first);
Py_INCREF(s);
internStringMortalInplace(s);
AUTO_DECREF(s);
made->setattr(s, p.second, NULL);
Py_DECREF(s);
}
} else {
assert(0 && "check refcounting");
Box* copy = PyDict_Copy(attr_dict);
RELEASE_ASSERT(copy, "");
made->setDictBacked(copy);
}
static BoxedString* module_str = getStaticString("__module__");
......
......@@ -1324,7 +1324,7 @@ static Box* typeSubDict(Box* obj, void* context) {
abort();
}
void Box::setDictBacked(Box* val) {
void Box::setDictBacked(STOLEN(Box*) val) {
assert(this->cls->instancesHaveHCAttrs());
RELEASE_ASSERT(val->cls == dict_cls || val->cls == attrwrapper_cls, "");
......
......@@ -81,7 +81,7 @@ test_descrtut `exec in DefaultDict()`
test_descr wontfix: crashes at "self.__dict__ = self"
test_dictcomps we need to disallow assigning to dictcomps
test_dict misc failures related to things like gc, abc, comparisons, detecting mutations during iterations
test_dictviews various unique bugs
test_dictviews segfault calling repr on recursive dictview. remove test/tests/test_dictview.py when the orig test passes
test_difflib [unknown]
test_distutils [unknown]
test_dis [unknown]
......
......@@ -57,3 +57,9 @@ print D.__base__
print type("test", (), {})
print type("test", (), {"__module__":"fake"})
# test non str attr keys
t = type("test", (), {u"test" : 2, 1000L : 3, 1.0 : 4})
print t.__dict__[u"test"], t.test
print t.__dict__[1000L]
print t.__dict__[1.0]
# This is a copy of cpythons test with the recursive repr test disabled
# remove this test when we can pass cpythons test
import unittest
from test import test_support
class DictSetTest(unittest.TestCase):
def test_constructors_not_callable(self):
kt = type({}.viewkeys())
self.assertRaises(TypeError, kt, {})
self.assertRaises(TypeError, kt)
it = type({}.viewitems())
self.assertRaises(TypeError, it, {})
self.assertRaises(TypeError, it)
vt = type({}.viewvalues())
self.assertRaises(TypeError, vt, {})
self.assertRaises(TypeError, vt)
def test_dict_keys(self):
d = {1: 10, "a": "ABC"}
keys = d.viewkeys()
self.assertEqual(len(keys), 2)
self.assertEqual(set(keys), set([1, "a"]))
self.assertEqual(keys, set([1, "a"]))
self.assertNotEqual(keys, set([1, "a", "b"]))
self.assertNotEqual(keys, set([1, "b"]))
self.assertNotEqual(keys, set([1]))
self.assertNotEqual(keys, 42)
self.assertIn(1, keys)
self.assertIn("a", keys)
self.assertNotIn(10, keys)
self.assertNotIn("Z", keys)
self.assertEqual(d.viewkeys(), d.viewkeys())
e = {1: 11, "a": "def"}
self.assertEqual(d.viewkeys(), e.viewkeys())
del e["a"]
self.assertNotEqual(d.viewkeys(), e.viewkeys())
def test_dict_items(self):
d = {1: 10, "a": "ABC"}
items = d.viewitems()
self.assertEqual(len(items), 2)
self.assertEqual(set(items), set([(1, 10), ("a", "ABC")]))
self.assertEqual(items, set([(1, 10), ("a", "ABC")]))
self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"]))
self.assertNotEqual(items, set([(1, 10), ("a", "def")]))
self.assertNotEqual(items, set([(1, 10)]))
self.assertNotEqual(items, 42)
self.assertIn((1, 10), items)
self.assertIn(("a", "ABC"), items)
self.assertNotIn((1, 11), items)
self.assertNotIn(1, items)
self.assertNotIn((), items)
self.assertNotIn((1,), items)
self.assertNotIn((1, 2, 3), items)
self.assertEqual(d.viewitems(), d.viewitems())
e = d.copy()
self.assertEqual(d.viewitems(), e.viewitems())
e["a"] = "def"
self.assertNotEqual(d.viewitems(), e.viewitems())
def test_dict_mixed_keys_items(self):
d = {(1, 1): 11, (2, 2): 22}
e = {1: 1, 2: 2}
self.assertEqual(d.viewkeys(), e.viewitems())
self.assertNotEqual(d.viewitems(), e.viewkeys())
def test_dict_values(self):
d = {1: 10, "a": "ABC"}
values = d.viewvalues()
self.assertEqual(set(values), set([10, "ABC"]))
self.assertEqual(len(values), 2)
def test_dict_repr(self):
d = {1: 10, "a": "ABC"}
self.assertIsInstance(repr(d), str)
r = repr(d.viewitems())
self.assertIsInstance(r, str)
self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or
r == "dict_items([(1, 10), ('a', 'ABC')])")
r = repr(d.viewkeys())
self.assertIsInstance(r, str)
self.assertTrue(r == "dict_keys(['a', 1])" or
r == "dict_keys([1, 'a'])")
r = repr(d.viewvalues())
self.assertIsInstance(r, str)
self.assertTrue(r == "dict_values(['ABC', 10])" or
r == "dict_values([10, 'ABC'])")
def test_keys_set_operations(self):
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 2}
d3 = {'d': 4, 'e': 5}
self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'})
self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'})
self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())
self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'})
self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'})
self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())
self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'})
self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'})
self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'})
self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'})
self.assertEqual(d1.viewkeys() | set(d3.viewkeys()),
{'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())
self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'})
self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())
self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'})
self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),
{'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() - d1.viewkeys(), set())
self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'})
self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'})
self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set())
self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'})
self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'})
def test_items_set_operations(self):
d1 = {'a': 1, 'b': 2}
d2 = {'a': 2, 'b': 2}
d3 = {'d': 4, 'e': 5}
self.assertEqual(
d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)})
self.assertEqual(d1.viewitems() & d3.viewitems(), set())
self.assertEqual(d1.viewitems() & set(d1.viewitems()),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)})
self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())
self.assertEqual(d1.viewitems() | d1.viewitems(),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() | d2.viewitems(),
{('a', 1), ('a', 2), ('b', 2)})
self.assertEqual(d1.viewitems() | d3.viewitems(),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
self.assertEqual(d1.viewitems() | set(d1.viewitems()),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() | set(d2.viewitems()),
{('a', 1), ('a', 2), ('b', 2)})
self.assertEqual(d1.viewitems() | set(d3.viewitems()),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())
self.assertEqual(d1.viewitems() ^ d2.viewitems(),
{('a', 1), ('a', 2)})
self.assertEqual(d1.viewitems() ^ d3.viewitems(),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
self.assertEqual(d1.viewitems() - d1.viewitems(), set())
self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)})
self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() - set(d1.viewitems()), set())
self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)})
self.assertEqual(d1.viewitems() - set(d3.viewitems()),
{('a', 1), ('b', 2)})
# Pyston change: disabled test
"""
def test_recursive_repr(self):
d = {}
d[42] = d.viewvalues()
self.assertRaises(RuntimeError, repr, d)
"""
def test_main():
test_support.run_unittest(DictSetTest)
if __name__ == "__main__":
# pyston change: remove duration in test output
# test_main()
import sys, StringIO, re
orig_stdout = sys.stdout
out = StringIO.StringIO()
sys.stdout = out
test_main()
sys.stdout = orig_stdout
print re.sub(" [.0-9]+s", " TIME", out.getvalue())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment