[Jython-checkins] jython (2.5): Fixed #1730 -- allowing arbitrary attributes in functools.partial, its

alex.gronholm jython-checkins at python.org
Sat Apr 14 00:43:06 CEST 2012


http://hg.python.org/jython/rev/99b8d4f16639
changeset:   6582:99b8d4f16639
branch:      2.5
parent:      6580:76310e1a0785
user:        Alex Grönholm <alex.gronholm at nextday.fi>
date:        Sat Apr 14 01:39:48 2012 +0300
summary:
  Fixed #1730 -- allowing arbitrary attributes in functools.partial, its subclasses and subclasses of zipimport.zipimporter to match CPython behavior

files:
  Lib/test/test_functools_jy.py                            |  20 ++++++++
  Lib/test/test_zipimport_jy.py                            |  11 ++++
  NEWS                                                     |   1 +
  src/org/python/modules/_functools/PyPartial.java         |  16 ++++++
  src/org/python/modules/zipimport/zipimporterDerived.java |  24 ++++++++++
  src/templates/zipimporter.derived                        |   2 +-
  6 files changed, 73 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_functools_jy.py b/Lib/test/test_functools_jy.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_functools_jy.py
@@ -0,0 +1,20 @@
+from functools import partial
+import unittest
+
+from test import test_support
+
+
+class PartialDictTest(unittest.TestCase):
+    def test_assign_attribute(self):
+        partial(lambda: None).somevar = 1
+
+    def test_subclass_assign_attribute(self):
+        class A(partial): pass
+        A(lambda: None).somevar = 1
+
+
+def test_main():
+    test_support.run_unittest(PartialDictTest)
+
+if __name__ == "__main__":
+    test_main()
diff --git a/Lib/test/test_zipimport_jy.py b/Lib/test/test_zipimport_jy.py
--- a/Lib/test/test_zipimport_jy.py
+++ b/Lib/test/test_zipimport_jy.py
@@ -1,8 +1,12 @@
+from zipimport import zipimporter
+from tempfile import NamedTemporaryFile
 import unittest
 import sys
+import os.path
 import java.lang.Package
 
 from test import test_support
+from zipfile import ZipFile
 
 class SyspathZipimportTest(unittest.TestCase):
     def setUp(self):
@@ -31,8 +35,15 @@
         from syspathpkg import module
         self.assertEquals(module.__name__, 'syspathpkg.module')
 
+class ZipImporterDictTest(unittest.TestCase):
+    def test_subclass_assign_attribute(self):
+        class A(zipimporter): pass
+        path = os.path.abspath('tests/modjy/lib_python_folder/test_modules.zip')
+        A(path).somevar = 1
+
 def test_main():
     test_support.run_unittest(SyspathZipimportTest)
+    test_support.run_unittest(ZipImporterDictTest)
 
 if __name__ == "__main__":
     test_main()
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@
 
 Jython 2.5.3b2
   Bugs Fixed
+    - [ 1730 ] functools.partial incorrectly makes __doc__ property readonly
     - [ 1537 ] expat: org.python.apache.xerces.parsers.SAXParser
     - [ 1268 ] SAX parsers wants to load external DTDs, causing an exception
     - [ 1805 ] threading.Thread always gets name "Thread" instead of a discriminating one
diff --git a/src/org/python/modules/_functools/PyPartial.java b/src/org/python/modules/_functools/PyPartial.java
--- a/src/org/python/modules/_functools/PyPartial.java
+++ b/src/org/python/modules/_functools/PyPartial.java
@@ -149,6 +149,22 @@
     }
 
     @Override
+    public void __setattr__(String name, PyObject value) {
+    	partial___setattr__(name, value);
+    }
+
+    @ExposedMethod
+    final void partial___setattr__(String name, PyObject value) {
+        ensureDict();
+        super.__setattr__(name, value);
+    }
+
+    @Override
+    public PyObject fastGetDict() {
+        return __dict__;
+    }
+
+    @Override
     @ExposedGet(name = "__dict__")
     public PyObject getDict() {
         ensureDict();
diff --git a/src/org/python/modules/zipimport/zipimporterDerived.java b/src/org/python/modules/zipimport/zipimporterDerived.java
--- a/src/org/python/modules/zipimport/zipimporterDerived.java
+++ b/src/org/python/modules/zipimport/zipimporterDerived.java
@@ -16,9 +16,33 @@
 
     private PyObject[]slots;
 
+    private PyObject dict;
+
+    public PyObject fastGetDict() {
+        return dict;
+    }
+
+    public PyObject getDict() {
+        return dict;
+    }
+
+    public void setDict(PyObject newDict) {
+        if (newDict instanceof PyStringMap||newDict instanceof PyDictionary) {
+            dict=newDict;
+        } else {
+            throw Py.TypeError("__dict__ must be set to a Dictionary "+newDict.getClass().getName());
+        }
+    }
+
+    public void delDict() {
+        // deleting an object's instance dict makes it grow a new one
+        dict=new PyStringMap();
+    }
+
     public zipimporterDerived(PyType subtype) {
         super(subtype);
         slots=new PyObject[subtype.getNumSlots()];
+        dict=subtype.instDict();
     }
 
     public PyString __str__() {
diff --git a/src/templates/zipimporter.derived b/src/templates/zipimporter.derived
--- a/src/templates/zipimporter.derived
+++ b/src/templates/zipimporter.derived
@@ -1,4 +1,4 @@
 base_class: zipimporter
-want_dict: false
+want_dict: true
 ctr:
 incl: object

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list