[Python-checkins] python/dist/src/Python newcompile.c, 1.1.2.77, 1.1.2.78

nnorwitz at users.sourceforge.net nnorwitz at users.sourceforge.net
Sat Mar 20 14:46:54 EST 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15883/Python
Modified Files:
 Tag: ast-branch
	newcompile.c 
Log Message:
get name mangling working
Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.77
retrieving revision 1.1.2.78
diff -C2 -d -r1.1.2.77 -r1.1.2.78
*** newcompile.c	20 Mar 2004 17:44:21 -0000	1.1.2.77
--- newcompile.c	20 Mar 2004 19:46:51 -0000	1.1.2.78
***************
*** 33,37 ****
 
 Invalid behaviour:
- #: name mangling in classes (__vars) doesn't work
 #: doing from __future__ import division doesn't work 
 doesn't output BINARY_TRUE_DIVISION
--- 33,36 ----
***************
*** 85,88 ****
--- 84,89 ----
 	PyObject *u_freevars; /* free variables */
 
+ 	PyObject *u_private;	/* for private name mangling */
+ 
 	int u_argcount; /* number of arguments for block */ 
 	int u_nblocks; /* number of used blocks in u_blocks
***************
*** 157,186 ****
 static PyObject *__doc__;
 
! int
! _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
 {
 	/* Name mangling: __private becomes _classname__private.
 	 This is independent from how the name is used. */
 	size_t nlen, plen;
! 	if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
! 		return 0;
 	nlen = strlen(name);
! 	if (nlen+2 >= maxlen)
! 		return 0; /* Don't mangle __extremely_long_names */
! 	if (name[nlen-1] == '_' && name[nlen-2] == '_')
! 		return 0; /* Don't mangle __whatever__ */
 	/* Strip leading underscores from class name */
 	while (*p == '_')
 		p++;
! 	if (*p == '0円')
! 		return 0; /* Don't mangle if class is just underscores */
 	plen = strlen(p);
! 	if (plen + nlen >= maxlen)
! 		plen = maxlen-nlen-2; /* Truncate class name if too long */
! 	/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
! 	buffer[0] = '_';
 	strncpy(buffer+1, p, plen);
 	strcpy(buffer+1+plen, name);
! 	return 1;
 }
 
--- 158,196 ----
 static PyObject *__doc__;
 
! PyObject *
! _Py_Mangle(PyObject *private, PyObject *ident)
 {
 	/* Name mangling: __private becomes _classname__private.
 	 This is independent from how the name is used. */
+ const char *p, *name = PyString_AsString(ident);
+ char *buffer;
 	size_t nlen, plen;
! 	if (private == NULL || name == NULL || name[0] != '_' || name[1] != '_') {
! Py_INCREF(ident);
! 		return ident;
! }
! p = PyString_AsString(private);
 	nlen = strlen(name);
! 	if (name[nlen-1] == '_' && name[nlen-2] == '_') {
! Py_INCREF(ident);
! 		return ident; /* Don't mangle __whatever__ */
! }
 	/* Strip leading underscores from class name */
 	while (*p == '_')
 		p++;
! 	if (*p == '0円') {
! Py_INCREF(ident);
! 		return ident; /* Don't mangle if class is just underscores */
! }
 	plen = strlen(p);
! ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
! if (!ident)
! return 0;
! 	/* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
! buffer = PyString_AS_STRING(ident);
! buffer[0] = '_';
 	strncpy(buffer+1, p, plen);
 	strcpy(buffer+1+plen, name);
! 	return ident;
 }
 
***************
*** 389,392 ****
--- 399,404 ----
 		return 0;
 
+ u->u_private = NULL;
+ 
 	/* A little debugging output */
 	compiler_display_symbols(name, u->u_ste->ste_symbols);
***************
*** 399,402 ****
--- 411,416 ----
 		Py_DECREF(wrapper);
 		fprintf(stderr, "stack = %s\n", PyObject_REPR(c->c_stack));
+ u->u_private = c->u->u_private;
+ Py_XINCREF(u->u_private);
 	}
 	c->u = u;
***************
*** 654,657 ****
--- 668,685 ----
 }
 
+ static int
+ compiler_addop_name(struct compiler *c, int opcode, PyObject *o)
+ {
+ int arg;
+ PyObject *mangled = _Py_Mangle(c->u->u_private, o);
+ if (!mangled)
+ return 0;
+ arg = compiler_add_o(c, c->u->u_names, mangled);
+ Py_DECREF(mangled);
+ if (arg < 0)
+ return 0;
+ return compiler_addop_i(c, opcode, arg);
+ }
+ 
 /* Add an opcode with an integer argument.
 Returns 0 on failure, 1 on success.
***************
*** 723,726 ****
--- 751,761 ----
 }
 
+ #define ADDOP_NAME(C, OP, O) { \
+ 	if (!compiler_addop_name((C), (OP), (O))) { \
+ Py_DECREF(O); \
+ 		return 0; \
+ } \
+ }
+ 
 #define ADDOP_I(C, OP, O) { \
 	if (!compiler_addop_i((C), (OP), (O))) \
***************
*** 958,961 ****
--- 993,998 ----
 	if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s))
 		return 0;
+ c->u->u_private = s->v.ClassDef.name;
+ Py_INCREF(c->u->u_private);
 str = PyString_InternFromString("__name__");
 	if (!str || !compiler_nameop(c, str, Load)) {
***************
*** 1350,1354 ****
 		identifier store_name;
 		ADDOP_O(c, LOAD_CONST, Py_None, consts);
! 		ADDOP_O(c, IMPORT_NAME, alias->name, names);
 
 /* XXX: handling of store_name should be cleaned up */
--- 1387,1391 ----
 		identifier store_name;
 		ADDOP_O(c, LOAD_CONST, Py_None, consts);
! 		ADDOP_NAME(c, IMPORT_NAME, alias->name);
 
 /* XXX: handling of store_name should be cleaned up */
***************
*** 1395,1399 ****
 
 	ADDOP_O(c, LOAD_CONST, names, consts);
! 	ADDOP_O(c, IMPORT_NAME, s->v.ImportFrom.module, names);
 	for (i = 0; i < n; i++) {
 		alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
--- 1432,1436 ----
 
 	ADDOP_O(c, LOAD_CONST, names, consts);
! 	ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module);
 	for (i = 0; i < n; i++) {
 		alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
***************
*** 1407,1411 ****
 		}
 		 
! 		ADDOP_O(c, IMPORT_FROM, alias->name, names);
 		store_name = alias->name;
 		if (alias->asname)
--- 1444,1448 ----
 		}
 		 
! 		ADDOP_NAME(c, IMPORT_FROM, alias->name);
 		store_name = alias->name;
 		if (alias->asname)
***************
*** 1761,1765 ****
 
 	assert(op);
! 	ADDOP_O(c, op, name, names);
 	return 1;
 }
--- 1798,1802 ----
 
 	assert(op);
! 	ADDOP_NAME(c, op, name);
 	return 1;
 }
***************
*** 2059,2063 ****
 			/* Fall through to load */
 		case Load:
! 			ADDOP_O(c, LOAD_ATTR, e->v.Attribute.attr, names);
 			break;
 		case AugStore:
--- 2096,2100 ----
 			/* Fall through to load */
 		case Load:
! 			ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr);
 			break;
 		case AugStore:
***************
*** 2065,2072 ****
 			/* Fall through to save */
 		case Store:
! 			ADDOP_O(c, STORE_ATTR, e->v.Attribute.attr, names);
 			break;
 		case Del:
! 			ADDOP_O(c, DELETE_ATTR, e->v.Attribute.attr, names);
 			break;
 		case Param:
--- 2102,2109 ----
 			/* Fall through to save */
 		case Store:
! 			ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr);
 			break;
 		case Del:
! 			ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr);
 			break;
 		case Param:


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /