summaryrefslogtreecommitdiff
path: root/sphinx/pycode
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-02-28 10:21:00 +0100
committerGeorg Brandl <georg@python.org>2010-02-28 10:21:00 +0100
commit887ee5f5291ccf51bdddf137a4cd8df20b3482a5 (patch)
tree3dbe8d2ef949192fa98e98f0489fb0dbfdc8be2b /sphinx/pycode
parentcb653c494f300f5cc5208524b100878eca8ff8be (diff)
parenta8cca36b28a33b22e7d79600cfbbd33f5ec4f09a (diff)
downloadsphinx-887ee5f5291ccf51bdddf137a4cd8df20b3482a5.tar.gz
merge with trunk
Diffstat (limited to 'sphinx/pycode')
-rw-r--r--sphinx/pycode/__init__.py45
-rw-r--r--sphinx/pycode/pgen2/driver.py2
-rw-r--r--sphinx/pycode/pgen2/grammar.py2
-rw-r--r--sphinx/pycode/pgen2/pgen.py8
4 files changed, 17 insertions, 40 deletions
diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py
index 93a11ca8..73c2042f 100644
--- a/sphinx/pycode/__init__.py
+++ b/sphinx/pycode/__init__.py
@@ -14,8 +14,10 @@ import sys
from os import path
from cStringIO import StringIO
+from sphinx.errors import PycodeError
from sphinx.pycode import nodes
from sphinx.pycode.pgen2 import driver, token, tokenize, parse, literals
+from sphinx.util import get_module_source
from sphinx.util.docstrings import prepare_docstring, prepare_commentdoc
@@ -136,14 +138,6 @@ class AttrDocVisitor(nodes.NodeVisitor):
self.collected[namespace, name] = docstring
-class PycodeError(Exception):
- def __str__(self):
- res = self.args[0]
- if len(self.args) > 1:
- res += ' (exception was: %r)' % self.args[1]
- return res
-
-
class ModuleAnalyzer(object):
# cache for analyzer objects -- caches both by module and file name
cache = {}
@@ -173,33 +167,11 @@ class ModuleAnalyzer(object):
return entry
try:
- if modname not in sys.modules:
- try:
- __import__(modname)
- except ImportError, err:
- raise PycodeError('error importing %r' % modname, err)
- mod = sys.modules[modname]
- if hasattr(mod, '__loader__'):
- try:
- source = mod.__loader__.get_source(modname)
- except Exception, err:
- raise PycodeError('error getting source for %r' % modname,
- err)
+ type, source = get_module_source(modname)
+ if type == 'string':
obj = cls.for_string(source, modname)
- cls.cache['module', modname] = obj
- return obj
- filename = getattr(mod, '__file__', None)
- if filename is None:
- raise PycodeError('no source found for module %r' % modname)
- filename = path.normpath(path.abspath(filename))
- lfilename = filename.lower()
- if lfilename.endswith('.pyo') or lfilename.endswith('.pyc'):
- filename = filename[:-1]
- elif not lfilename.endswith('.py'):
- raise PycodeError('source is not a .py file: %r' % filename)
- if not path.isfile(filename):
- raise PycodeError('source file is not present: %r' % filename)
- obj = cls.for_file(filename, modname)
+ else:
+ obj = cls.for_file(source, modname)
except PycodeError, err:
cls.cache['module', modname] = err
raise
@@ -214,6 +186,11 @@ class ModuleAnalyzer(object):
# file-like object yielding source lines
self.source = source
+ # cache the source code as well
+ pos = self.source.tell()
+ self.code = self.source.read()
+ self.source.seek(pos)
+
# will be filled by tokenize()
self.tokens = None
# will be filled by parse()
diff --git a/sphinx/pycode/pgen2/driver.py b/sphinx/pycode/pgen2/driver.py
index edc882fa..39e347b7 100644
--- a/sphinx/pycode/pgen2/driver.py
+++ b/sphinx/pycode/pgen2/driver.py
@@ -35,7 +35,7 @@ class Driver(object):
def parse_tokens(self, tokens, debug=False):
"""Parse a series of tokens and return the syntax tree."""
- # XXX Move the prefix computation into a wrapper around tokenize.
+ # X X X Move the prefix computation into a wrapper around tokenize.
p = parse.Parser(self.grammar, self.convert)
p.setup()
lineno = 1
diff --git a/sphinx/pycode/pgen2/grammar.py b/sphinx/pycode/pgen2/grammar.py
index 381d80e8..5a433578 100644
--- a/sphinx/pycode/pgen2/grammar.py
+++ b/sphinx/pycode/pgen2/grammar.py
@@ -16,7 +16,7 @@ fallback token code OP, but the parser needs the actual token code.
import pickle
# Local imports
-from sphinx.pycode.pgen2 import token, tokenize
+from sphinx.pycode.pgen2 import token
class Grammar(object):
diff --git a/sphinx/pycode/pgen2/pgen.py b/sphinx/pycode/pgen2/pgen.py
index d6895eae..0a04447d 100644
--- a/sphinx/pycode/pgen2/pgen.py
+++ b/sphinx/pycode/pgen2/pgen.py
@@ -54,12 +54,12 @@ class ParserGenerator(object):
first = {}
for label in rawfirst:
ilabel = self.make_label(c, label)
- ##assert ilabel not in first # XXX failed on <> ... !=
+ ##assert ilabel not in first # X X X failed on <> ... !=
first[ilabel] = 1
return first
def make_label(self, c, label):
- # XXX Maybe this should be a method on a subclass of converter?
+ # X X X Maybe this should be a method on a subclass of converter?
ilabel = len(c.labels)
if label[0].isalpha():
# Either a symbol name or a named token
@@ -157,9 +157,9 @@ class ParserGenerator(object):
#self.dump_nfa(name, a, z)
dfa = self.make_dfa(a, z)
#self.dump_dfa(name, dfa)
- oldlen = len(dfa)
+ #oldlen = len(dfa)
self.simplify_dfa(dfa)
- newlen = len(dfa)
+ #newlen = len(dfa)
dfas[name] = dfa
#print name, oldlen, newlen
if startsymbol is None: