summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-11-17 22:58:56 (GMT)
committerGuido van Rossum <guido@python.org>1993-11-17 22:58:56 (GMT)
commitc45611d0e33ddd96fb9fffa8dc42a96384d5d270 (patch)
tree343959ada4542b5d4abc0a50802f434072da8e8b /Objects
parent71e57d090d070686746ee63b855e38cb337dc5cc (diff)
downloadcpython-c45611d0e33ddd96fb9fffa8dc42a96384d5d270.zip
cpython-c45611d0e33ddd96fb9fffa8dc42a96384d5d270.tar.gz
cpython-c45611d0e33ddd96fb9fffa8dc42a96384d5d270.tar.bz2
* import.c (get_module): total rewrite, to ensure proper search order: for
each dir in sys.path, try each possible extension. (Note: C extensions are loaded before Python modules in the same directory, to allow having a C version used when dynamic loading is supported and a Python version as a back-up.) * import.c (reload_module): test for error from getmodulename() * moduleobject.c: implement module name as dict entry '__name__' instead of special-casing it in module_getattr(); this way a module (or function!) can access its own module name, and programs that know what they are doing can rename modules. * stdwinmodule.c (initstdwin): strip ".py" suffix of argv[0].
Diffstat (limited to 'Objects')
-rw-r--r--Objects/moduleobject.c51
1 files changed, 30 insertions, 21 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index dca5543..9a687b4 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -29,7 +29,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
typedef struct {
OB_HEAD
- object *md_name;
object *md_dict;
} moduleobject;
@@ -37,16 +36,24 @@ object *
newmoduleobject(name)
char *name;
{
- moduleobject *m = NEWOBJ(moduleobject, &Moduletype);
+ moduleobject *m;
+ object *nameobj;
+ m = NEWOBJ(moduleobject, &Moduletype);
if (m == NULL)
return NULL;
- m->md_name = newstringobject(name);
+ nameobj = newstringobject(name);
m->md_dict = newdictobject();
- if (m->md_name == NULL || m->md_dict == NULL) {
- DECREF(m);
- return NULL;
- }
+ if (m->md_dict == NULL || nameobj == NULL)
+ goto fail;
+ if (dictinsert(m->md_dict, "__name__", nameobj) != 0)
+ goto fail;
+ DECREF(nameobj);
return (object *)m;
+
+ fail:
+ XDECREF(nameobj);
+ DECREF(m);
+ return NULL;
}
object *
@@ -64,11 +71,17 @@ char *
getmodulename(m)
object *m;
{
+ object *nameobj;
if (!is_moduleobject(m)) {
err_badarg();
return NULL;
}
- return getstringvalue(((moduleobject *)m) -> md_name);
+ nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__");
+ if (nameobj == NULL || !is_stringobject(nameobj)) {
+ err_setstr(SystemError, "nameless module");
+ return NULL;
+ }
+ return getstringvalue(nameobj);
}
/* Methods */
@@ -77,8 +90,6 @@ static void
module_dealloc(m)
moduleobject *m;
{
- if (m->md_name != NULL)
- DECREF(m->md_name);
if (m->md_dict != NULL)
DECREF(m->md_dict);
free((char *)m);
@@ -89,7 +100,12 @@ module_repr(m)
moduleobject *m;
{
char buf[100];
- sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name));
+ char *name = getmodulename((object *)m);
+ if (name == NULL) {
+ err_clear();
+ name = "?";
+ }
+ sprintf(buf, "<module '%.80s'>", name);
return newstringobject(buf);
}
@@ -103,10 +119,6 @@ module_getattr(m, name)
INCREF(m->md_dict);
return m->md_dict;
}
- if (strcmp(name, "__name__") == 0) {
- INCREF(m->md_name);
- return m->md_name;
- }
res = dictlookup(m->md_dict, name);
if (res == NULL)
err_setstr(AttributeError, name);
@@ -126,12 +138,9 @@ module_setattr(m, name, v)
object *v;
{
object *ac;
- if (name[0] == '_' && name[1] == '_') {
- int n = strlen(name);
- if (name[n-1] == '_' && name[n-2] == '_') {
- err_setstr(TypeError, "read-only special attribute");
- return -1;
- }
+ if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
+ err_setstr(TypeError, "read-only special attribute");
+ return -1;
}
ac = dictlookup(m->md_dict, name);
if (ac != NULL && is_accessobject(ac))