Commit 25922a87 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge branch 'django_dependencies'

parents d544b623 bc62488a
...@@ -498,6 +498,8 @@ PyAPI_DATA(PyTypeObject*) type_cls; ...@@ -498,6 +498,8 @@ PyAPI_DATA(PyTypeObject*) type_cls;
// Pyston change: this is no longer a static object // Pyston change: this is no longer a static object
//PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ //PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
//PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */ //PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
PyAPI_DATA(PyTypeObject*) object_cls;
#define PyBaseObject_Type (*object_cls)
// Pyston changes: these aren't direct macros any more [they potentially could be though] // Pyston changes: these aren't direct macros any more [they potentially could be though]
PyAPI_FUNC(bool) _PyType_Check(PyObject*) PYSTON_NOEXCEPT; PyAPI_FUNC(bool) _PyType_Check(PyObject*) PYSTON_NOEXCEPT;
......
...@@ -183,6 +183,12 @@ def warn(message, category=None, stacklevel=1): ...@@ -183,6 +183,12 @@ def warn(message, category=None, stacklevel=1):
assert issubclass(category, Warning) assert issubclass(category, Warning)
# Get context information # Get context information
try: try:
# Pyston change: manually skip the call to _getframe.
# A ValueError() is supposed to specify that the "depth" argument is greater
# than the stack level, so it doesn't seem appropriate for us to throw as
# a signal that it's unimplemented.
raise ValueError()
caller = sys._getframe(stacklevel) caller = sys._getframe(stacklevel)
except ValueError: except ValueError:
globals = sys.__dict__ globals = sys.__dict__
......
...@@ -521,7 +521,39 @@ static PyObject* call_function_tail(PyObject* callable, PyObject* args) { ...@@ -521,7 +521,39 @@ static PyObject* call_function_tail(PyObject* callable, PyObject* args) {
} }
extern "C" PyObject* PyObject_CallMethod(PyObject* o, const char* name, const char* format, ...) noexcept { extern "C" PyObject* PyObject_CallMethod(PyObject* o, const char* name, const char* format, ...) noexcept {
Py_FatalError("unimplemented"); va_list va;
PyObject* args;
PyObject* func = NULL;
PyObject* retval = NULL;
if (o == NULL || name == NULL)
return null_error();
func = PyObject_GetAttrString(o, name);
if (func == NULL) {
PyErr_SetString(PyExc_AttributeError, name);
return 0;
}
if (!PyCallable_Check(func)) {
type_error("attribute of type '%.200s' is not callable", func);
goto exit;
}
if (format && *format) {
va_start(va, format);
args = Py_VaBuildValue(format, va);
va_end(va);
} else
args = PyTuple_New(0);
retval = call_function_tail(func, args);
exit:
/* args gets consumed in call_function_tail */
Py_XDECREF(func);
return retval;
} }
extern "C" PyObject* PyObject_CallMethodObjArgs(PyObject* callable, PyObject* name, ...) noexcept { extern "C" PyObject* PyObject_CallMethodObjArgs(PyObject* callable, PyObject* name, ...) noexcept {
......
...@@ -432,6 +432,57 @@ extern "C" int PyModule_AddIntConstant(PyObject* _m, const char* name, long valu ...@@ -432,6 +432,57 @@ extern "C" int PyModule_AddIntConstant(PyObject* _m, const char* name, long valu
return PyModule_AddObject(_m, name, boxInt(value)); return PyModule_AddObject(_m, name, boxInt(value));
} }
extern "C" PyObject* PyEval_CallMethod(PyObject* obj, const char* methodname, const char* format, ...) noexcept {
va_list vargs;
PyObject* meth;
PyObject* args;
PyObject* res;
meth = PyObject_GetAttrString(obj, methodname);
if (meth == NULL)
return NULL;
va_start(vargs, format);
args = Py_VaBuildValue(format, vargs);
va_end(vargs);
if (args == NULL) {
Py_DECREF(meth);
return NULL;
}
res = PyEval_CallObject(meth, args);
Py_DECREF(meth);
Py_DECREF(args);
return res;
}
extern "C" PyObject* PyEval_CallObjectWithKeywords(PyObject* func, PyObject* arg, PyObject* kw) noexcept {
PyObject* result;
if (arg == NULL) {
arg = PyTuple_New(0);
if (arg == NULL)
return NULL;
} else if (!PyTuple_Check(arg)) {
PyErr_SetString(PyExc_TypeError, "argument list must be a tuple");
return NULL;
} else
Py_INCREF(arg);
if (kw != NULL && !PyDict_Check(kw)) {
PyErr_SetString(PyExc_TypeError, "keyword list must be a dictionary");
Py_DECREF(arg);
return NULL;
}
result = PyObject_Call(func, arg, kw);
Py_DECREF(arg);
return result;
}
} // namespace pyston } // namespace pyston
...@@ -2297,6 +2297,8 @@ void commonClassSetup(BoxedClass* cls) { ...@@ -2297,6 +2297,8 @@ void commonClassSetup(BoxedClass* cls) {
if (PyType_Check(b)) if (PyType_Check(b))
inherit_slots(cls, static_cast<BoxedClass*>(b)); inherit_slots(cls, static_cast<BoxedClass*>(b));
} }
assert(cls->tp_dict && cls->tp_dict->cls == attrwrapper_cls);
} }
extern "C" void PyType_Modified(PyTypeObject* type) noexcept { extern "C" void PyType_Modified(PyTypeObject* type) noexcept {
......
...@@ -483,8 +483,8 @@ Box* issubclass_func(Box* child, Box* parent) { ...@@ -483,8 +483,8 @@ Box* issubclass_func(Box* child, Box* parent) {
return boxBool(classobjIssubclass(static_cast<BoxedClassobj*>(child), static_cast<BoxedClassobj*>(parent))); return boxBool(classobjIssubclass(static_cast<BoxedClassobj*>(child), static_cast<BoxedClassobj*>(parent)));
} }
assert(isSubclass(child->cls, type_cls)); RELEASE_ASSERT(isSubclass(child->cls, type_cls), "");
if (parent->cls != type_cls) if (!isSubclass(parent->cls, type_cls))
return False; return False;
return boxBool(isSubclass(static_cast<BoxedClass*>(child), static_cast<BoxedClass*>(parent))); return boxBool(isSubclass(static_cast<BoxedClass*>(child), static_cast<BoxedClass*>(parent)));
......
...@@ -373,30 +373,6 @@ extern "C" int PyObject_Not(PyObject* o) noexcept { ...@@ -373,30 +373,6 @@ extern "C" int PyObject_Not(PyObject* o) noexcept {
Py_FatalError("unimplemented"); Py_FatalError("unimplemented");
} }
extern "C" PyObject* PyEval_CallObjectWithKeywords(PyObject* func, PyObject* arg, PyObject* kw) noexcept {
PyObject* result;
if (arg == NULL) {
arg = PyTuple_New(0);
if (arg == NULL)
return NULL;
} else if (!PyTuple_Check(arg)) {
PyErr_SetString(PyExc_TypeError, "argument list must be a tuple");
return NULL;
} else
Py_INCREF(arg);
if (kw != NULL && !PyDict_Check(kw)) {
PyErr_SetString(PyExc_TypeError, "keyword list must be a dictionary");
Py_DECREF(arg);
return NULL;
}
result = PyObject_Call(func, arg, kw);
Py_DECREF(arg);
return result;
}
extern "C" PyObject* PyObject_Call(PyObject* callable_object, PyObject* args, PyObject* kw) noexcept { extern "C" PyObject* PyObject_Call(PyObject* callable_object, PyObject* args, PyObject* kw) noexcept {
try { try {
if (kw) if (kw)
......
...@@ -383,6 +383,9 @@ void BoxedClass::finishInitialization() { ...@@ -383,6 +383,9 @@ void BoxedClass::finishInitialization() {
tp_clear = tp_base->tp_clear; tp_clear = tp_base->tp_clear;
} }
assert(!this->tp_dict);
this->tp_dict = makeAttrWrapper(this);
commonClassSetup(this); commonClassSetup(this);
} }
...@@ -1523,16 +1526,6 @@ extern "C" Box* getattr(Box* obj, const char* attr) { ...@@ -1523,16 +1526,6 @@ extern "C" Box* getattr(Box* obj, const char* attr) {
static StatCounter slowpath_getattr("slowpath_getattr"); static StatCounter slowpath_getattr("slowpath_getattr");
slowpath_getattr.log(); slowpath_getattr.log();
bool is_dunder = (attr[0] == '_' && attr[1] == '_');
if (is_dunder) {
if (strcmp(attr, "__dict__") == 0) {
// TODO this is wrong, should be added at the class level as a getset
if (obj->cls->instancesHaveHCAttrs())
return makeAttrWrapper(obj);
}
}
if (VERBOSITY() >= 2) { if (VERBOSITY() >= 2) {
#if !DISABLE_STATS #if !DISABLE_STATS
std::string per_name_stat_name = "getattr__" + std::string(attr); std::string per_name_stat_name = "getattr__" + std::string(attr);
...@@ -1578,21 +1571,6 @@ extern "C" Box* getattr(Box* obj, const char* attr) { ...@@ -1578,21 +1571,6 @@ extern "C" Box* getattr(Box* obj, const char* attr) {
return val; return val;
} }
if (is_dunder) {
// There's more to it than this:
if (strcmp(attr, "__class__") == 0) {
assert(obj->cls != instance_cls); // I think in this case __class__ is supposed to be the classobj?
return obj->cls;
}
// This doesn't belong here either:
if (strcmp(attr, "__bases__") == 0 && isSubclass(obj->cls, type_cls)) {
BoxedClass* cls = static_cast<BoxedClass*>(obj);
assert(cls->tp_bases);
return cls->tp_bases;
}
}
raiseAttributeError(obj, attr); raiseAttributeError(obj, attr);
} }
...@@ -1658,44 +1636,6 @@ void setattrInternal(Box* obj, const std::string& attr, Box* val, SetattrRewrite ...@@ -1658,44 +1636,6 @@ void setattrInternal(Box* obj, const std::string& attr, Box* val, SetattrRewrite
} }
} }
if (attr == "__class__") {
if (!isSubclass(val->cls, type_cls))
raiseExcHelper(TypeError, "__class__ must be set to new-style class, not '%s' object", val->cls->tp_name);
auto new_cls = static_cast<BoxedClass*>(val);
// Conservative Pyston checks: make sure that both classes are derived only from Pyston types,
// and that they don't define any extra C-level fields
RELEASE_ASSERT(val->cls == type_cls, "");
RELEASE_ASSERT(obj->cls->cls == type_cls, "");
for (auto _base : static_cast<BoxedTuple*>(obj->cls->tp_mro)->elts) {
BoxedClass* base = static_cast<BoxedClass*>(_base);
RELEASE_ASSERT(base->is_pyston_class, "");
}
for (auto _base : static_cast<BoxedTuple*>(new_cls->tp_mro)->elts) {
BoxedClass* base = static_cast<BoxedClass*>(_base);
RELEASE_ASSERT(base->is_pyston_class, "");
}
RELEASE_ASSERT(obj->cls->tp_basicsize == object_cls->tp_basicsize + sizeof(HCAttrs) + sizeof(Box**), "");
RELEASE_ASSERT(new_cls->tp_basicsize == object_cls->tp_basicsize + sizeof(HCAttrs) + sizeof(Box**), "");
RELEASE_ASSERT(obj->cls->attrs_offset != 0, "");
RELEASE_ASSERT(new_cls->attrs_offset != 0, "");
RELEASE_ASSERT(obj->cls->tp_weaklistoffset != 0, "");
RELEASE_ASSERT(new_cls->tp_weaklistoffset != 0, "");
// Normal Python checks.
// TODO there are more checks to add here, and they should throw errors not asserts
RELEASE_ASSERT(obj->cls->tp_basicsize == new_cls->tp_basicsize, "");
RELEASE_ASSERT(obj->cls->tp_dictoffset == new_cls->tp_dictoffset, "");
RELEASE_ASSERT(obj->cls->tp_weaklistoffset == new_cls->tp_weaklistoffset, "");
RELEASE_ASSERT(obj->cls->attrs_offset == new_cls->attrs_offset, "");
obj->cls = new_cls;
return;
}
// Lookup a descriptor // Lookup a descriptor
Box* descr = NULL; Box* descr = NULL;
RewriterVar* r_descr = NULL; RewriterVar* r_descr = NULL;
...@@ -3769,6 +3709,9 @@ Box* typeNew(Box* _cls, Box* arg1, Box* arg2, Box** _args) { ...@@ -3769,6 +3709,9 @@ Box* typeNew(Box* _cls, Box* arg1, Box* arg2, Box** _args) {
// TODO: how much of these should be in BoxedClass::finishInitialization()? // TODO: how much of these should be in BoxedClass::finishInitialization()?
made->tp_dictoffset = base->tp_dictoffset; made->tp_dictoffset = base->tp_dictoffset;
if (!made->getattr("__dict__") && (made->instancesHaveHCAttrs() || made->instancesHaveDictAttrs()))
made->giveAttr("__dict__", dict_descr);
for (const auto& p : attr_dict->d) { for (const auto& p : attr_dict->d) {
auto k = coerceUnicodeToStr(p.first); auto k = coerceUnicodeToStr(p.first);
......
...@@ -231,6 +231,14 @@ Box* setUpdate(BoxedSet* self, BoxedTuple* args) { ...@@ -231,6 +231,14 @@ Box* setUpdate(BoxedSet* self, BoxedTuple* args) {
return None; return None;
} }
Box* setCopy(BoxedSet* self) {
assert(self->cls == set_cls);
BoxedSet* rtn = new BoxedSet();
rtn->s.insert(self->s.begin(), self->s.end());
return rtn;
}
Box* setContains(BoxedSet* self, Box* v) { Box* setContains(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls); assert(self->cls == set_cls || self->cls == frozenset_cls);
return boxBool(self->s.count(v) != 0); return boxBool(self->s.count(v) != 0);
...@@ -313,6 +321,8 @@ void setupSet() { ...@@ -313,6 +321,8 @@ void setupSet() {
set_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)setClear, NONE, 1))); set_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)setClear, NONE, 1)));
set_cls->giveAttr("update", new BoxedFunction(boxRTFunction((void*)setUpdate, NONE, 1, 0, true, false))); set_cls->giveAttr("update", new BoxedFunction(boxRTFunction((void*)setUpdate, NONE, 1, 0, true, false)));
set_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)setCopy, UNKNOWN, 1)));
set_cls->freeze(); set_cls->freeze();
frozenset_cls->freeze(); frozenset_cls->freeze();
} }
......
This diff is collapsed.
...@@ -630,5 +630,10 @@ Box* makeAttrWrapper(Box* b); ...@@ -630,5 +630,10 @@ Box* makeAttrWrapper(Box* b);
// Our default for tp_alloc: // Our default for tp_alloc:
extern "C" PyObject* PystonType_GenericAlloc(BoxedClass* cls, Py_ssize_t nitems) noexcept; extern "C" PyObject* PystonType_GenericAlloc(BoxedClass* cls, Py_ssize_t nitems) noexcept;
// A descriptor that you can add to your class to provide instances with a __dict__ accessor.
// Classes created in Python get this automatically, but builtin types (including extension types)
// are supposed to add one themselves. type_cls and function_cls do this, for example.
extern Box* dict_descr;
} }
#endif #endif
...@@ -5,6 +5,7 @@ print o.items() ...@@ -5,6 +5,7 @@ print o.items()
for i in xrange(30): for i in xrange(30):
o[(i ** 2) ^ 0xace] = i o[(i ** 2) ^ 0xace] = i
print o print o
print o.copy()
print collections.deque().maxlen print collections.deque().maxlen
......
# expected: fail
# - we don't support setting __dict__ yet
class C(object):
pass
c1 = C()
c2 = C()
c1.a = 2
c1.b = 3
c2.a = 4
c2.b = 5
def p():
print sorted(c1.__dict__.items()), sorted(c2.__dict__.items())
p()
c1.__dict__ = c2.__dict__
p()
c1.a = 6
c2.b = 7
p()
...@@ -13,7 +13,8 @@ def fake(): ...@@ -13,7 +13,8 @@ def fake():
class TestClass(object): class TestClass(object):
def __init__(self): def __init__(self):
self.__dict__ = {n: n for n in fake()} for n in fake():
setattr(self, n, n)
def __dir__(self): def __dir__(self):
return fake() return fake()
...@@ -21,7 +22,8 @@ class TestClass(object): ...@@ -21,7 +22,8 @@ class TestClass(object):
class TestClass2(object): class TestClass2(object):
def __init__(self): def __init__(self):
self.__dict__ = {'dictAttr': False, 'attribute1': None} self.dictAttr = False
self.attribute1 = None
self.other_attribute = False self.other_attribute = False
def method1(self): def method1(self):
...@@ -63,7 +65,7 @@ test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__', ...@@ -63,7 +65,7 @@ test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__', test_in_dir(['__str__', '__new__', '__repr__', '__dir__', '__init__',
'__module__', 'method1', 'dictAttr', 'attribute1'], TestClass2) '__module__', 'method1', 'dictAttr', 'attribute1'], TestClass2)
test_in_dir(['attribute1', 'dictAttr', '__init__', '__module__', 'method1', test_in_dir(['attribute1', 'dictAttr', '__init__', '__module__', 'method1',
'other_attribute', '__dict__'], TestClass2()) 'other_attribute'], TestClass2())
test_in_dir(fake(), TestClass()) test_in_dir(fake(), TestClass())
print len(fake()) == len(dir(TestClass())) print len(fake()) == len(dir(TestClass()))
...@@ -91,3 +93,7 @@ for x in d1: ...@@ -91,3 +93,7 @@ for x in d1:
l.append(x) l.append(x)
l.sort() l.sort()
print l print l
c = C1()
c.__dict__.update(dict(a=1, b=5))
print sorted(c.__dict__.items())
o = object()
r = o.__reduce__()
print type(r), len(r)
class C(object):
def __repr__(self):
return "<C>"
c = C()
r = c.__reduce__()
print type(r), len(r)
assert len(r) == 2
c2 = r[0](*r[1])
print c, c2
...@@ -74,3 +74,8 @@ except KeyError, e: ...@@ -74,3 +74,8 @@ except KeyError, e:
def f2(): def f2():
print {5} print {5}
f2() f2()
s = set([])
s2 = s.copy()
s.add(1)
print s, s2
def show(obj):
print obj.__class__
for b in obj.__class__.__mro__:
print b,
print
if isinstance(obj, type):
print obj.__bases__, obj.__mro__
if hasattr(obj, "__dict__"):
print sorted(obj.__dict__.items())
show(object())
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