[Python-Dev] Accessing globals without dict lookup (original) (raw)
Guido van Rossum guido@python.org
Mon, 11 Feb 2002 10:02:38 -0500
- Previous message: [Python-Dev] Accessing globals without dict lookup
- Next message: [Python-Dev] Accessing globals without dict lookup
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Very nice. One case I would like to see covered is that of a global that is deleted. Something like:
import eggs i = -2 max = 3 j = 4 def foo(n): y = abs(i) + max return eggs.ham(y + n) del j I presume there would still be an entry in spam's module dict with a NULL objptr.
Yes.
The whole think makes sense to me if it avoids the possible two PyDictGetItem calls in the LOADGLOBAL opcode. As I understand it, if accessed inside a function, LOADGLOBAL could be implemented something like this:
case LOADGLOBAL:
Surely you meant LOAD_GLOBAL_CELL.
cell = funccells[oparg]; if (cell.objptr) x = cell->objptr; else x = cell->cellptr->objptr; if (x == NULL) { ... error recovery ... break; } PyINCREF(x); continue;
This looks a lot better to me (no complex function calls).
Here's my version:
case LOAD_GLOBAL_CELL:
cell = func_cells[oparg];
x = cell->objptr;
if (x == NULL) {
x = cell->cellptr->objptr;
if (x == NULL) {
... error recovery ...
break;
}
}
Py_INCREF(x);
continue;
What happens in the module's top-level code where there is presumably no funccells array? Do we simply have two different opcodes, one for use at the global level and one for use in functions?
It could use LOAD_GLOBAL which should use PyMapping_GetItem on the globals dict. Or maybe even LOAD_NAME which should do the same. But we could also somehow create a func_cells array (hm, it would have to be called differently then I suppose).
(I've added these to the FAQs in PEP 280 too.)
--Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Accessing globals without dict lookup
- Next message: [Python-Dev] Accessing globals without dict lookup
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]