[pypy-svn] r16258 - in pypy/dist/pypy/tool: . test

lac at codespeak.net lac at codespeak.net
Tue Aug 23 13:34:51 CEST 2005


Author: lac
Date: Tue Aug 23 13:34:49 2005
New Revision: 16258

Modified:
   pypy/dist/pypy/tool/getdocstrings.py
   pypy/dist/pypy/tool/test/test_getdocstrings.py
Log:
Works now, needs tidying.


Modified: pypy/dist/pypy/tool/getdocstrings.py
==============================================================================
--- pypy/dist/pypy/tool/getdocstrings.py	(original)
+++ pypy/dist/pypy/tool/getdocstrings.py	Tue Aug 23 13:34:49 2005
@@ -3,12 +3,10 @@
 from os import listdir
 
 where = autopath.pypydir + '/objspace/std/'
-triplequotes = '(' + "'''" + '|' + '"""' + ')'
 quote = '(' + "'" + '|' + '"' + ')'
-doc = re.compile(r"__doc__\s+=\s+" + triplequotes +
-                 r"(?P<docstring>.*)"+ triplequotes ,
-                 re.DOTALL
-                 )
+triplequotes = '(' + "'''" + '|' + '"""' + ')'
+# Note: this will produce erroneous result if you nest triple quotes
+# in your docstring.
 
 def mk_std_filelist():
     ''' go to pypy/objs/std and get all the *type.py files '''
@@ -19,9 +17,6 @@
             filelist.append(f)
     return filelist
 
-def mk_typelist(filelist):
-    ''' generate a list of the types we expect to find in our files'''
-    return [f[:-7] for f in filelist]
 
 def compile_doc():
     return re.compile(r"__doc__\s+=\s+" + triplequotes +
@@ -30,90 +25,87 @@
                       )
 
 def compile_typedef(match):
-    return re.compile(r"(?P<whitespace>\s*)"
+    return re.compile(r"(?P<whitespace>\s+)"
                       + r"(?P<typeassign>" + match
                       + "_typedef = StdTypeDef+\s*\(\s*"
-                      + quote + match +  quote + ",)",
-                      re.DOTALL
-                      )
+                      + quote + match +  quote + ",).*"
+                      + r"(?P<indent>^\s+)"
+                      + r"(?P<newassign>__new__\s*=\s*newmethod)",
+                      re.DOTALL | re.MULTILINE)
 
-def compile_typedef_match(matchstring, sourcefile):
-    return re.compile(r"(?P<whitespace>\s+)"
-                      + r"(?P<typeassign>" + matchstring
-                      + "_typedef = StdTypeDef+\s*\(\s*"
-                      + quote + matchstring +  quote + ",)"
-                      + r"(?P<indent>.*\s+)"
-                      + r"(?P<newassign>__new__)",
-                      re.DOTALL
-                      ).match(sourcefile).span()
+def get_pypydoc(match, sourcefile):
+    doc = compile_doc()
+    
+    try: # if this works we already have a docstring
+        pypydoc = doc.search(sourcefile).group('docstring')
+
+    except AttributeError: # No pypy docstring
+        return None
+
+    return pypydoc
+
+def sub_pypydoc(match, sourcefile, cpydoc):
+    try:
+        typedef = compile_typedef(match)
+        tdsearch = typedef.search(sourcefile).group('typeassign')
+        newsearch = typedef.search(sourcefile).group('newassign')
+        if not tdsearch:
+            print 'tdsearch, not found', match
+        if not newsearch:
+            print 'newsearch, not found', match
+    except AttributeError:
+        pass # so stringtype does not blow up.
+    return None
+
+def get_cpydoc(match):
+    try:
+        cpydoc = eval(match + '.__doc__')
+
+    except NameError: # No CPython docstring
+        cpydoc = None
+    return cpydoc
 
 if __name__ == '__main__':
 
-    filenames = listdir(where)
+    #filenames = mk_std_filelist()
+    #filenames = ['basestringtype.py']
+    filenames = ['tupletype.py']
 
     docstrings = []
 
     for f in filenames:
-        if f.endswith('type.py'):
-            match = f[:-7]
-            s = match + '.__doc__'
-
-            try:
-                cpydoc = eval(match + '.__doc__')
-                #cpydoc = 'cpy_stuff'
-            except NameError: # No CPython docstring
-                cpydoc = None
-
-            sourcefile = file(where + f).read()
+        match = f[:-7]
+        sourcefile = file(where + f).read()
+        
+        pypydoc = get_pypydoc(match, sourcefile)
+        cpydoc = get_cpydoc(match)
+
+        if pypydoc:
+            print match, 'already has a pypydoc'
+        elif not cpydoc:
+            print match, 'does not have a cpydoc'
 
+        else:
+            print match, 'has cpydoc.   Trying to insert'
+            docstring="__doc__ = '''" + cpydoc + "'''"
 
-            # will produce erroneous result if you nest triple quotes
-            # in your docstring.  
+            typedef = compile_typedef(match)
             
-            doc = re.compile(r"__doc__\s+=\s+" + triplequotes +
-                             r"(?P<docstring>.*)"+ triplequotes ,
-                             re.DOTALL
-                             )
-            typedef = re.compile(r"(?P<whitespace>\s+)"
-                                 + r"(?P<typeassign>" + match
-                                 + "_typedef = StdTypeDef+\s*\(\s*"
-                                 + quote + match +  quote + ",)"
-                                 + r"(?P<indent>.*\s+)"
-                                 + r"(?P<newassign>__new__)",
-                                 re.DOTALL)
-
             try:
-                pypydoc = doc.search(sourcefile).group('docstring')
-                #pypydoc = 'pypy_stuff'
-            except AttributeError: # No pypy docstring
-                pypydoc = None
-                tdsearch = None
-                try:
-                    tdsearch = typedef.search(sourcefile).group('typeassign')
-                    newsearch = typedef.search(sourcefile).group('newassign')
-                    if tdsearch:
-                        print tdsearch, ' found', match
-                        print newsearch
-                    else:
-                        print tdsearch, ' not found', match
-
-                except AttributeError:
-                    pass # so stringtype does not blow up.
-
-            docstrings.append((match, cpydoc, pypydoc))
-
-    for (m, c, p) in docstrings:
-        if p:
-            print m, 'already has a pypydoc'
-        elif not c:  
-            print m, 'does not have a cpydoc'
-        elif not tdsearch:
-            print m, 'has cpydoc but no ..._typedef = StdTypeDef.  Skipping'
-        else:
-            print m, 'has cpydoc and ..._typedef = StdTypeDef.  Inserting'
-
-
+                newsearch = typedef.search(sourcefile)
+                if newsearch:
+                    print match, '__new__ found'
+                    print newsearch.groupdict()
+                    print newsearch.group('newassign')
+                    print re.sub(newsearch.group('indent') +
+                                 newsearch.group('newassign'),
+                                 newsearch.group('indent') +
+                                 docstring + '\n' +
+                                 newsearch.group('indent') +
+                                 newsearch.group('newassign'),
+                                 sourcefile)
+
+            except AttributeError:
+                print match, 'no __new__ found'
+                
             
-
-
-    

Modified: pypy/dist/pypy/tool/test/test_getdocstrings.py
==============================================================================
--- pypy/dist/pypy/tool/test/test_getdocstrings.py	(original)
+++ pypy/dist/pypy/tool/test/test_getdocstrings.py	Tue Aug 23 13:34:49 2005
@@ -6,8 +6,8 @@
 pypy_dir = autopath.pypydir
 # Naming weirdness: why not both pypy_dir and this_dir or pypydir and thisdir
 
-from pypy.tool.getdocstrings import quote, triplequotes, doc
-from pypy.tool.getdocstrings import mk_std_filelist, mk_typelist
+from pypy.tool.getdocstrings import quote, triplequotes
+from pypy.tool.getdocstrings import mk_std_filelist
 
 class TestDocStringInserter:
     def setup_method(self, method):
@@ -25,13 +25,6 @@
             'booltype.py', 'objecttype.py', 'stringtype.py',
             'listtype.py']
 
-    def test_typelist(self):
-        assert mk_typelist(mk_std_filelist()) == [
-            'basestring', 'unicode', 'int', 'none', 'long',
-            'slice', 'iter', 'float', 'type', 'dict',
-            'dictproxy', 'tuple', 'bool', 'object', 'string',
-            'list']
-
     def test_gottestfile(self):
         s = self.fd1.read()       # whole file as string
 



More information about the Pypy-commit mailing list