[py-svn] r8317 - in py/dist/py/code: . testing

hpk at codespeak.net hpk at codespeak.net
Sun Jan 16 21:22:01 CET 2005


Author: hpk
Date: Sun Jan 16 21:22:01 2005
New Revision: 8317

Modified:
   py/dist/py/code/source.py
   py/dist/py/code/testing/test_source.py
Log:
conceptually a Sourcecode object is a list of lines 
so it has an obvious iterator and len() 

introduce default "rstripping" for constructing
Source objects. 



Modified: py/dist/py/code/source.py
==============================================================================
--- py/dist/py/code/source.py	(original)
+++ py/dist/py/code/source.py	Sun Jan 16 21:22:01 2005
@@ -6,18 +6,21 @@
 
 class Source(object):
     """ a mutable object holding a source code fragment,
-        automatically deindenting it.
+        possibly deindenting it.
     """
     def __init__(self, *parts, **kwargs):
         self.lines = lines = []
         de = kwargs.get('deindent', True)
+        rstrip = kwargs.get('rstrip', True) 
         for part in parts:
             if isinstance(part, Source):
                 partlines = part.lines
             elif isinstance(part, str):
+                if rstrip:
+                    part = part.rstrip()
                 partlines = part.split('\n')
             else:
-                partlines = getsource(part, deindent=False).lines
+                partlines = getsource(part, deindent=de).lines
             if de:
                 partlines = deindent(partlines)
             lines.extend(partlines)
@@ -28,6 +31,9 @@
         else:
             return self.__getslice__(key)
 
+    def __len__(self): 
+        return len(self.lines) 
+
     def __getslice__(self, start, end):
         newsource = Source()
         newsource.lines = self.lines[start:end]
@@ -47,14 +53,14 @@
         source.lines[:] = self.lines[start:end+1]
         return source
 
-    def putaround(self, before='', after=''):
-        """ modifies a new source object embedded/indented
-            between before and after.
+    def putaround(self, before='', after='', indent=' ' * 4): 
+        """ return a copy of the source object with 
+            'before' and 'after' wrapped around it. 
         """
         before = Source(before)
         after = Source(after)
         newsource = Source()
-        lines = ['    ' + line for line in self.lines]
+        lines = [ (indent + line) for line in self.lines]
         newsource.lines = before.lines + lines +  after.lines
         return newsource
 
@@ -94,7 +100,9 @@
                     trysource = self[start:end+1]
                     if trysource.isparseable():
                         break
-        assert start <= lineno <= end, "could not locate statement"
+        #print str(self[start-2:start+2])
+        assert start <= lineno <= end, "%d <= %d <= %d" %(start,lineno,end)
+        #        "could not locate statement in %r" % self[lineno])
         return start, end + 1
 
     def getblockend(self, lineno):
@@ -220,7 +228,7 @@
             offset = 0
     newlines = []
     for line in lines:
-        #line = line.expandtabs()
+        line = line.expandtabs()
         s = line.lstrip()
         if s and line[:offset].isspace():
             line = line[offset:]

Modified: py/dist/py/code/testing/test_source.py
==============================================================================
--- py/dist/py/code/testing/test_source.py	(original)
+++ py/dist/py/code/testing/test_source.py	Sun Jan 16 21:22:01 2005
@@ -11,9 +11,14 @@
 
     x = Source("""
         3
-    """)
+    """, rstrip=False)
     assert str(x) == "\n3\n    "
 
+    x = Source("""
+        3
+    """, rstrip=True)
+    assert str(x) == "\n3"
+
 def test_source_from_function():
     source = py.code.Source(test_source_str_function)
     assert str(source).startswith('def test_source_str_function():')
@@ -54,7 +59,7 @@
     assert Source(" \nif 1:\n  pass").isparseable()
     assert not Source(" \nif 1:\npass").isparseable()
 
-class TestSliceAccess:
+class TestAccesses:
     source = Source("""\
         def f(x):
             pass
@@ -71,6 +76,14 @@
         x = self.source[0]
         assert x == "def f(x):"
 
+    def test_len(self):
+        assert len(self.source) == 4 
+
+    def test_iter(self):
+        l = [x for x in self.source] 
+        assert len(l) == 4 
+            
+
 class TestCodeHacks:
     def test_newcode(self):
         from py.__impl__.code.source import newcode



More information about the pytest-commit mailing list