lmaurits / prettytable

Automatically exported from code.google.com/p/prettytable
Other
22 stars 6 forks source link

slicing does not work on Python 2.4: deepcopy fails with TypeError due to function attribute _sort_key #40

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run tests on Python 2.4

What is the expected output? What do you see instead?
Tests should pass. Instead, slicing tests fail:

+ /usr/bin/python prettytable_test.py
...............................EE...
======================================================================
ERROR: testSliceFirstTwoRows (__main__.SlicingTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "prettytable_test.py", line 192, in testSliceFirstTwoRows
    y = self.x[0:2]
  File "/builddir/build/BUILD/prettytable-0.6.1/prettytable.py", line 171, in __getitem__
    newtable = copy.deepcopy(self)
  File "/usr/lib64/python2.4/copy.py", line 204, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.4/copy.py", line 351, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python2.4/copy.py", line 174, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python2.4/copy.py", line 268, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python2.4/copy.py", line 204, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.4/copy.py", line 336, in _reconstruct
    y = callable(*args)
  File "/usr/lib64/python2.4/copy_reg.py", line 92, in __newobj__
    return cls.__new__(cls, *args)
TypeError: function() takes at least 2 arguments (0 given)
======================================================================
ERROR: testSliceLastTwoRows (__main__.SlicingTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "prettytable_test.py", line 201, in testSliceLastTwoRows
    y = self.x[-2:]
  File "/builddir/build/BUILD/prettytable-0.6.1/prettytable.py", line 171, in __getitem__
    newtable = copy.deepcopy(self)
  File "/usr/lib64/python2.4/copy.py", line 204, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.4/copy.py", line 351, in _reconstruct
    state = deepcopy(state, memo)
  File "/usr/lib64/python2.4/copy.py", line 174, in deepcopy
    y = copier(x, memo)
  File "/usr/lib64/python2.4/copy.py", line 268, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib64/python2.4/copy.py", line 204, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib64/python2.4/copy.py", line 336, in _reconstruct
    y = callable(*args)
  File "/usr/lib64/python2.4/copy_reg.py", line 92, in __newobj__
    return cls.__new__(cls, *args)
TypeError: function() takes at least 2 arguments (0 given)
----------------------------------------------------------------------
Ran 36 tests in 0.050s
FAILED (errors=2)

What version of the product are you using? On what operating system?
prettytable-0.6.1, python-2.4.3 (RHEL5)

Please provide any additional information below.
This patch fixes the test suite, and it should be safe to use None because 
.sort() accepts None for the key. But I think it will still break if the caller 
slices a PrettyTable with a custom sort_key set on it. It might be necessary to 
write a custom __deepcopy__ or avoid using deepcopy.

diff -ur prettytable-0.6.1.orig/prettytable.py 
prettytable-0.6.1.patched/prettytable.py
--- prettytable-0.6.1.orig/prettytable.py   2012-06-04 08:22:23.000000000 +1000
+++ prettytable-0.6.1.patched/prettytable.py    2013-11-19 08:26:01.231044684 +1000
@@ -137,7 +137,7 @@

         self._sortby = kwargs["sortby"] or None
         self._reversesort = kwargs["reversesort"] or False
-        self._sort_key = kwargs["sort_key"] or (lambda x: x)
+        self._sort_key = kwargs["sort_key"]

         self._int_format = kwargs["float_format"] or {}
         self._float_format = kwargs["float_format"] or {}

Original issue reported on code.google.com by dan...@gmail.com on 18 Nov 2013 at 10:31

GoogleCodeExporter commented 9 years ago
Hi there.  Thanks for pointing this out.  I assume you are stuck using 2.4 for 
your application for some reason?

Original comment by luke@maurits.id.au on 24 Nov 2013 at 7:25

GoogleCodeExporter commented 9 years ago
Right, we are still supporting back to RHEL5 which has Python 2.4.

Original comment by dan...@gmail.com on 25 Nov 2013 at 3:34