Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GoogleCodeExporter opened this issue Aug 11, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant