Commits

Olemis Lang committed 510437a

Brython #26 : Implement default parameter in MatchObject.groups

Comments (0)

Files changed (3)

+# Placed by Bitbucket
+t26/t26_r4181bd84a226_js_undefined_list_slice.diff
 t26/t26_r4181bd84a226_re.match.diff
-# Placed by Bitbucket

t26/t26_r4181bd84a226_js_undefined_list_slice.diff

+# HG changeset patch
+# Parent 832e5eca977c61e4863857cecbaa436a2433e5db
+Brython #26 : Reference to undefined in native JS objects lost on slicing
+
+diff -r 832e5eca977c src/py_list.js
+--- a/src/py_list.js	Sun Apr 07 01:39:14 2013 -0500
++++ b/src/py_list.js	Sun Apr 07 01:58:21 2013 -0500
+@@ -134,6 +134,7 @@
+             else {
+                 for(i=start;i<stop;i+=step){
+                     if(items[i]!==undefined){res.push(items[i])}
++                    else {res.push(None)}
  1. Olemis Lang author

    Pierre Quentel : I needed to apply the following changes because Javascript RegExp objects report unmatched optional groups inside match list using undefined . Just removing them creates a few problems

    1. list length will not be consistent with the behavior of slice ops
    2. neither will be list.getitem which returns None
    3. Match results (groups) will not return expected values

    Please review .

+                 }
+                 return res
+             }
+@@ -142,6 +143,7 @@
+             else {
+                 for(i=start;i>=stop;i+=step){
+                     if(items[i]!==undefined){res.push(items[i])}
++                    else {res.push(None)}
+                 }
+                 return res
+             }

t26/t26_r4181bd84a226_re.match.diff

 # HG changeset patch
-# Parent 4181bd84a226231a2acead361ff769639992e163
+# Parent b4a5b0a706964cd75f95ec91691cf9e5fd8ab132
 Brython #26 : re.match
 
-diff -r 4181bd84a226 site/tests/index.html
---- a/site/tests/index.html	Sat Apr 06 12:04:19 2013 -0500
-+++ b/site/tests/index.html	Sun Apr 07 01:39:14 2013 -0500
+diff -r b4a5b0a70696 site/tests/index.html
+--- a/site/tests/index.html	Sun Apr 07 01:58:59 2013 -0500
++++ b/site/tests/index.html	Sun Apr 07 02:05:01 2013 -0500
 @@ -84,20 +84,27 @@
  
  Test suite:
  </select>
  </div>
    <div id="editor"></div>
-diff -r 4181bd84a226 site/tests/test_re.py
+diff -r b4a5b0a70696 site/tests/test_re.py
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/site/tests/test_re.py	Sun Apr 07 01:39:14 2013 -0500
++++ b/site/tests/test_re.py	Sun Apr 07 02:05:01 2013 -0500
 @@ -0,0 +1,42 @@
 +import re
 +
 +assert m.string == 'hello world'
 +assert m.groups() == ()
 +
-+# Samples code in Python 3 doc MatchObject.groups
++# Samples code in Python 3 doc MatchObject.groups (indices only)
 +
 +m = re.match(r"(\d+)\.(\d+)", "24.1632")
 +assert m.groups() == ('24', '1632')
 +assert m.groups() == ('24', None)
 +assert m.groups('0') == ('24', '0')
 +
-+m = re.match(r"(\d+)\.?(\d+)? --", "24 --")
++m = re.match(r"(\d+)\.?(\d+)? (--)", "24 --")
 +assert m.groups() == ('24', None, '--')
 +assert m.groups('0') == ('24', '0', '--')
 +
-+# Samples code in Python 3 doc MatchObject.group
++# Samples code in Python 3 doc MatchObject.group (indices only)
 +
 +m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
 +assert m.group(0) == 'Isaac Newton'
 +
 +print('all tests ok..')
 +
-diff -r 4181bd84a226 src/Lib/re.py
---- a/src/Lib/re.py	Sat Apr 06 12:04:19 2013 -0500
-+++ b/src/Lib/re.py	Sun Apr 07 01:39:14 2013 -0500
-@@ -2,20 +2,38 @@
+diff -r b4a5b0a70696 src/Lib/re.py
+--- a/src/Lib/re.py	Sun Apr 07 01:58:59 2013 -0500
++++ b/src/Lib/re.py	Sun Apr 07 02:05:01 2013 -0500
+@@ -2,20 +2,42 @@
  
  class MatchObject:
  
 +            return tuple(self._jsmatch[x] for x in args)
 +
 +    def groups(self, default_=None):
-+        return tuple(self._jsmatch[1:])
++        if default_ is None:
++            return tuple(self._jsmatch[1:])
++        else:
++            return tuple(x if x is not None else default_ 
++                         for x in self._jsmatch[1:])
  
  def search(pattern,src,flags=None):
 +    # FIXME : Check whether `g` is appropriate as this is used for match too