From: <pj...@us...> - 2008-04-13 19:09:10
|
Revision: 4343 http://jython.svn.sourceforge.net/jython/?rev=4343&view=rev Author: pjenvey Date: 2008-04-13 12:08:52 -0700 (Sun, 13 Apr 2008) Log Message: ----------- fix re.subx on empty strings with no match: CPython actually returns pattern[0:0] in this case Modified Paths: -------------- trunk/jython/Lib/test/test_re_jy.py trunk/jython/src/org/python/modules/sre/PatternObject.java Modified: trunk/jython/Lib/test/test_re_jy.py =================================================================== --- trunk/jython/Lib/test/test_re_jy.py 2008-04-13 18:55:39 UTC (rev 4342) +++ trunk/jython/Lib/test/test_re_jy.py 2008-04-13 19:08:52 UTC (rev 4343) @@ -4,10 +4,21 @@ class ReTest(unittest.TestCase): - def test_unkown_groupname(self): - self.assertRaises(IndexError, - re.match("(?P<int>\d+)\.(\d*)", '3.14').group, "misspelled") + def test_unkown_groupname(self): + self.assertRaises(IndexError, + re.match("(?P<int>\d+)\.(\d*)", '3.14').group, + "misspelled") + def test_no_empty_string_no_match(self): + # must return pattern[0:0] here + result = re.sub('^foo', 'bar', u'') + self.assert_(isinstance(result, str)) + self.assertEqual(result, '') + + result = re.sub(u'^foo', 'bar', '') + self.assert_(isinstance(result, unicode)) + self.assertEqual(result, u'') + def test_main(): test.test_support.run_unittest(ReTest) Modified: trunk/jython/src/org/python/modules/sre/PatternObject.java =================================================================== --- trunk/jython/src/org/python/modules/sre/PatternObject.java 2008-04-13 18:55:39 UTC (rev 4342) +++ trunk/jython/src/org/python/modules/sre/PatternObject.java 2008-04-13 19:08:52 UTC (rev 4343) @@ -122,7 +122,8 @@ int n = 0; int i = 0; - + + boolean appended = false; while (count == 0 || n < count) { state.state_reset(); state.ptr = state.start; @@ -138,6 +139,7 @@ if (i < b) { /* get segment before this match */ buf.append(string.substring(i, b)); + appended = true; } if (! (i == b && i == e && n > 0)) { PyObject item; @@ -151,6 +153,7 @@ if (item != Py.None) { buf.append(item.toString()); + appended = true; } i = e; n++; @@ -164,8 +167,14 @@ } if (i < state.endpos) { buf.append(string.substring(i, state.endpos)); + appended = true; } + if (!appended) { + return pattern == null ? Py.EmptyString : + pattern.__getslice__(Py.newInteger(0), Py.newInteger(0)); + } + if (subn) return new PyTuple(instring.createInstance(buf.toString()), Py.newInteger(n)); else This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |