Commit a32e3a41 authored by Marius Wachtler's avatar Marius Wachtler

Merge pull request #1066 from undingen/minor_compat5

Fix imp.load_module if the module already exists and ban creating instances of None type
parents eb3b2738 23b021d8
......@@ -84,7 +84,7 @@ JitCodeBlock::JitCodeBlock(llvm::StringRef name)
int32_t* offset_ptr = (int32_t*)((uint8_t*)eh_frame_addr + 0x20);
int32_t* size_ptr = (int32_t*)((uint8_t*)eh_frame_addr + 0x24);
int64_t offset = (int8_t*)code.get() - (int8_t*)offset_ptr;
assert(offset >= INT_MIN && offset <= INT_MAX);
RELEASE_ASSERT(offset >= INT_MIN && offset <= INT_MAX, "");
*offset_ptr = offset;
*size_ptr = code_size;
......
......@@ -168,7 +168,7 @@ static void writeTrivialEhFrame(void* eh_frame_addr, void* func_addr, uint64_t f
int32_t* size_ptr = (int32_t*)((uint8_t*)eh_frame_addr + 0x24);
int64_t offset = (int8_t*)func_addr - (int8_t*)offset_ptr;
assert(offset >= INT_MIN && offset <= INT_MAX);
RELEASE_ASSERT(offset >= INT_MIN && offset <= INT_MAX, "");
*offset_ptr = offset;
assert(func_size <= UINT_MAX);
......
......@@ -3251,7 +3251,7 @@ static Box* typeName(Box* b, void*) {
}
static void typeSetName(Box* b, Box* v, void*) {
assert(b->cls == type_cls);
assert(PyType_Check(b));
BoxedClass* type = static_cast<BoxedClass*>(b);
// Awkward... in CPython you can only set __name__ for heaptype classes
......@@ -3913,6 +3913,7 @@ void setupRuntime() {
none_cls->giveAttr("__nonzero__", new BoxedFunction(FunctionMetadata::create((void*)noneNonzero, BOXED_BOOL, 1)));
none_cls->giveAttr("__doc__", None);
none_cls->tp_hash = (hashfunc)_Py_HashPointer;
none_cls->tp_new = NULL; // don't allow creating instances
none_cls->freeze();
none_cls->tp_repr = none_repr;
......@@ -4171,21 +4172,24 @@ BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
BoxedDict* d = getSysModulesDict();
BoxedModule* module = NULL;
// Surprisingly, there are times that we need to return the existing module if
// one exists:
Box* existing = d->getOrNull(name);
if (existing && PyModule_Check(existing)) {
return static_cast<BoxedModule*>(existing);
module = static_cast<BoxedModule*>(existing);
} else {
module = new BoxedModule();
moduleInit(module, name, boxString(doc ? doc : ""));
d->d[name] = module;
}
BoxedModule* module = new BoxedModule();
moduleInit(module, name, boxString(doc ? doc : ""));
if (fn)
module->giveAttr("__file__", boxString(fn));
module->setattr(internStringMortal("__file__"), boxString(fn), NULL);
d->d[name] = module;
if (name->s() == "__main__")
module->giveAttr("__builtins__", builtins_module);
module->setattr(internStringMortal("__builtins__"), builtins_module, NULL);
return module;
}
......
......@@ -54,3 +54,9 @@ print sorted.__name__
# should all fail:
set_name(sorted, "blah")
set_name(sorted, 5)
import abc
class D(C):
__metaclass__ = abc.ABCMeta
D.__name__ = "has_abc_meta"
print D
......@@ -35,3 +35,11 @@ def n(s):
return str(s).replace(".pyc", ".py")
print n(m), n(m.__name__), n(m.__file__), hasattr(m, "__path__")
import sys, types
name = "json"
m = sys.modules[name] = types.ModuleType(name)
print sorted(dir(m))
s = imp.find_module(name)
m = imp.load_module(name, *s)
print name in m.__file__, sorted(dir(m))
print None.__class__
print type(None).__doc__, None.__doc__
try:
type(None)()
except TypeError as e:
print e
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