Commit 38f385e2 authored by Marius Wachtler's avatar Marius Wachtler

Make PyImport_ImportModule call PyImport_Import

this is how cpython does it.
The implementation of PyImport_ImportModule is still very different and I haven't verified it any further
but at least this now lets "from lxml import objectify" work (it internally imports "lxml.etree")

Also remove a few asserts when assembly generation failed
and teach type_analysis about None and while there cache the scope result.
parent 0980b4b5
......@@ -418,9 +418,13 @@ private:
}
void* visit_name(AST_Name* node) override {
auto name_scope = scope_info->getScopeTypeOfName(node->id);
if (node->lookup_type == ScopeInfo::VarScopeType::UNKNOWN)
node->lookup_type = scope_info->getScopeTypeOfName(node->id);
auto name_scope = node->lookup_type;
if (name_scope == ScopeInfo::VarScopeType::GLOBAL) {
if (node->id.s() == "None")
return NONE;
return UNKNOWN;
}
......
......@@ -1693,7 +1693,7 @@ assembler::Register Rewriter::allocReg(Location dest, Location otherThan) {
// Spill the register whose next use is farthest in the future
assert(found);
spillRegister(best_reg, /* preserve */ otherThan);
assert(vars_by_location.count(best_reg) == 0);
assert(failed || vars_by_location.count(best_reg) == 0);
return best_reg;
} else if (dest.type == Location::Register) {
assembler::Register reg(dest.regnum);
......@@ -1736,6 +1736,8 @@ assembler::XMMRegister Rewriter::allocXMMReg(Location dest, Location otherThan)
}
void Rewriter::addLocationToVar(RewriterVar* var, Location l) {
if (failed)
return;
assert(!var->isInLocation(l));
assert(vars_by_location.count(l) == 0);
......
......@@ -1033,20 +1033,6 @@ extern "C" int PyErr_WarnEx(PyObject* category, const char* text, Py_ssize_t sta
return -1;
}
extern "C" PyObject* PyImport_Import(PyObject* module_name) noexcept {
RELEASE_ASSERT(module_name, "");
RELEASE_ASSERT(module_name->cls == str_cls, "");
try {
std::string _module_name = static_cast<BoxedString*>(module_name)->s();
return importModuleLevel(_module_name, None, None, -1);
} catch (ExcInfo e) {
fatalOrError(PyExc_NotImplementedError, "unimplemented");
return nullptr;
}
}
extern "C" void* PyObject_Malloc(size_t sz) noexcept {
return gc_compat_malloc(sz);
}
......
......@@ -594,22 +594,25 @@ static void ensureFromlist(Box* module, Box* fromlist, std::string& buf, bool re
}
}
extern "C" PyObject* PyImport_ImportModule(const char* name) noexcept {
if (strcmp("__builtin__", name) == 0)
return builtins_module;
extern "C" PyObject* PyImport_Import(PyObject* module_name) noexcept {
RELEASE_ASSERT(module_name, "");
RELEASE_ASSERT(module_name->cls == str_cls, "");
try {
// TODO: check if this has the same behaviour as the cpython implementation
std::string str = name;
BoxedList* silly_list = new BoxedList();
listAppendInternal(silly_list, boxString("__doc__"));
return import(0, silly_list, str);
return import(0, silly_list, ((BoxedString*)module_name)->s());
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
extern "C" PyObject* PyImport_ImportModule(const char* name) noexcept {
return PyImport_Import(boxString(name));
}
/* Get the module object corresponding to a module name.
First check the modules dictionary if there's one there,
if not, create a new one and insert it in the modules dictionary.
......
......@@ -110,9 +110,14 @@ template <ExceptionStyle S> Box* coerceUnicodeToStr(Box* unicode) noexcept(S ==
Box* r = PyUnicode_AsASCIIString(unicode);
if (!r) {
if (S == CAPI) {
PyErr_SetString(TypeError, "Cannot use non-ascii unicode strings as attribute names or keywords");
return NULL;
} else {
PyErr_Clear();
raiseExcHelper(TypeError, "Cannot use non-ascii unicode strings as attribute names or keywords");
}
}
return r;
}
......
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