summaryrefslogtreecommitdiffstats
path: root/Lib/compiler/pycodegen.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2000-09-01 20:33:26 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2000-09-01 20:33:26 (GMT)
commit2051608616178ca6b73d5d59d887ddd67c5835e3 (patch)
treed7a04d3c59976fcd173c48e66e93f15faf826f64 /Lib/compiler/pycodegen.py
parent7527e00058bcfeb21c56386769a83ddff2005754 (diff)
downloadcpython-2051608616178ca6b73d5d59d887ddd67c5835e3.zip
cpython-2051608616178ca6b73d5d59d887ddd67c5835e3.tar.gz
cpython-2051608616178ca6b73d5d59d887ddd67c5835e3.tar.bz2
Update magic number.
Fix import support to work with import as variant of Python 2.0. The grammar for import changed, requiring changes in transformer and code generator, even to handle compilation of imports with as.
Diffstat (limited to 'Lib/compiler/pycodegen.py')
-rw-r--r--Lib/compiler/pycodegen.py30
1 files changed, 22 insertions, 8 deletions
diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py
index 9d9b982..a4c9e5b 100644
--- a/Lib/compiler/pycodegen.py
+++ b/Lib/compiler/pycodegen.py
@@ -1,6 +1,7 @@
import os
import marshal
import stat
+import string
import struct
import types
from cStringIO import StringIO
@@ -44,7 +45,7 @@ class Module:
f.write(self.getPycHeader())
marshal.dump(self.code, f)
- MAGIC = (50811 | (ord('\r')<<16) | (ord('\n')<<24))
+ MAGIC = (50823 | (ord('\r')<<16) | (ord('\n')<<24))
def getPycHeader(self):
# compile.c uses marshal to write a long directly, with
@@ -420,19 +421,32 @@ class CodeGenerator:
def visitImport(self, node):
self.set_lineno(node)
- for name in node.names:
+ for name, alias in node.names:
+ self.emit('LOAD_CONST', None)
self.emit('IMPORT_NAME', name)
- self.storeName(name)
+ self._resolveDots(name)
+ self.storeName(alias or name)
def visitFrom(self, node):
self.set_lineno(node)
+ fromlist = map(lambda (name, alias): name, node.names)
+ self.emit('LOAD_CONST', tuple(fromlist))
self.emit('IMPORT_NAME', node.modname)
- for name in node.names:
+ for name, alias in node.names:
if name == '*':
self.namespace = 0
self.emit('IMPORT_FROM', name)
+ self._resolveDots(name)
+ self.storeName(alias or name)
self.emit('POP_TOP')
+ def _resolveDots(self, name):
+ elts = string.split(name, ".")
+ if len(elts) == 1:
+ return
+ for elt in elts[1:]:
+ self.emit('LOAD_ATTR', elt)
+
def visitGetattr(self, node):
self.visit(node.expr)
self.emit('LOAD_ATTR', node.attrname)
@@ -787,12 +801,12 @@ class LocalNameFinder:
pass
def visitImport(self, node):
- for name in node.names:
- self.names.add(name)
+ for name, alias in node.names:
+ self.names.add(alias or name)
def visitFrom(self, node):
- for name in node.names:
- self.names.add(name)
+ for name, alias in node.names:
+ self.names.add(alias or name)
def visitClass(self, node):
self.names.add(node.name)