Skip to content

Commit ca60572

Browse files
author
Richard Jones
committed
Add compatibility package to allow us to deal with Python versions 2.3..2.6.
Outstanding issues noted in roundup/anypy/TODO.txt
1 parent 27ef29f commit ca60572

File tree

19 files changed

+370
-69
lines changed

19 files changed

+370
-69
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Fixes:
1010
- HTML file uploads served as application/octet-stream
1111
- New item action reject creation of new users
1212
- Item retirement was not being controlled
13+
- Roundup is now compatible with Python 2.6
1314
- XXX need to include Stefan's changes in here too
1415

1516

doc/upgrading.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ Should be replaced with::
6161
</form>
6262

6363

64+
Fix for Python 2.6+ users
65+
-------------------------
66+
67+
If you use Python 2.6 you should edit your tracker's
68+
``detectors/nosyreaction.py`` file to change::
69+
70+
import sets
71+
72+
at the top to::
73+
74+
from roundup.anypy.sets_ import set
75+
76+
and then all instances of ``sets.Set()`` to ``set()`` in the later code.
77+
78+
79+
6480
Trackers currently allowing HTML file uploading
6581
-----------------------------------------------
6682

roundup/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def do_specification(self, args):
729729
print _('%(key)s: %(value)s')%locals()
730730

731731
def do_display(self, args):
732-
"""Usage: display designator[,designator]*
732+
''"""Usage: display designator[,designator]*
733733
Show the property values for the given node(s).
734734
735735
This lists the properties and their associated values for the given

roundup/anypy/README.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
roundup.anypy package - Python version compatibility layer
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Roundup currently supports Python 2.3 to 2.6; however, some modules
5+
have been introduced, while others have been deprecated. The modules
6+
in this package provide the functionalities which are used by Roundup
7+
8+
- adapting the most recent Python usage
9+
- using new built-in functionality
10+
- avoiding deprecation warnings
11+
12+
Use the modules in this package to preserve Roundup's compatibility.
13+
14+
sets_: sets compatibility module
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
17+
Since Python 2.4, there is a built-in type 'set'; therefore, the 'sets'
18+
module is deprecated since version 2.6. As far as Roundup is concerned,
19+
the usage is identical; see
20+
http://docs.python.org/library/sets.html#comparison-to-the-built-in-set-types
21+
22+
Uses the built-in type 'set' if available, and thus avoids
23+
deprecation warnings. Simple usage:
24+
25+
Change all::
26+
from sets import Set
27+
28+
to::
29+
from roundup.anypy.sets_ import set
30+
31+
and use 'set' instead of 'Set' (or sets.Set, respectively).
32+
To avoid unnecessary imports, you can::
33+
34+
try:
35+
set
36+
except NameError:
37+
from roundup.anypy.sets_ import set
38+
39+
hashlib_: md5/sha/hashlib compatibility
40+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41+
42+
The md5 and sha modules are deprecated since Python 2.6; the hashlib
43+
module, introduced with Python 2.5, is recommended instead.
44+
45+
Change all::
46+
import md5
47+
md5.md5(), md5.new()
48+
import sha
49+
sha.sha(), sha.new()
50+
51+
to::
52+
from roundup.anypy.hashlib_ import md5
53+
md5()
54+
from roundup.anypy.hashlib_ import sha1
55+
sha1()
56+
57+
# vim: si

roundup/anypy/TODO.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Python compatiblity TODO
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
- the popen2 module is deprecated as of Python 2.6;
5+
the subprocess module is available since Python 2.4,
6+
thus a roundup.anypy.subprocess_ module is needed
7+
8+
- the MimeWriter module is deprecated as of Python 2.6. The email package is
9+
available since Python 2.2, thus we should manage without a ...email_
10+
module; however, it has suffered some API changes over the time
11+
(http://docs.python.org/library/email.html#package-history),
12+
so this is not sure.
13+
14+
Here's an incomplete replacement table:
15+
16+
MimeWriter usage checked for
17+
-> email usage Python ...
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
19+
MimeWriter.MimeWriter
20+
-> email.Message.Message (2.3)
21+
22+
MimeWriter.MimeWrite.addheader
23+
-> email.Message.Message.add_header (2.3)
24+
25+
- test.test_sqlite.sqliteDBTest.testStringUnicode fails
26+
27+
# vim: si

roundup/anypy/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
roundup.anypy - compatibility layer for any Python 2.3+
3+
"""
4+
VERSION = '.'.join(map(str,
5+
(0,
6+
1, # hashlib_, sets_
7+
)))

roundup/anypy/hashlib_.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
anypy.hashlib_: encapsulation of hashlib/md5/sha1/sha
3+
"""
4+
5+
try:
6+
from hashlib import md5, sha1 # new in Python 2.5
7+
except ImportError:
8+
from md5 import md5 # deprecated in Python 2.6
9+
from sha import sha as sha1 # deprecated in Python 2.6
10+
11+
# vim: ts=8 sts=4 sw=4 si

roundup/anypy/sets_.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
anypy.sets_: sets compatibility module
3+
4+
uses the built-in type 'set' if available, and thus avoids
5+
deprecation warnings. Simple usage:
6+
7+
Change all
8+
from sets import Set
9+
to
10+
from roundup.anypy.sets_ import set
11+
12+
and use 'set' instead of 'Set'.
13+
To avoid unnecessary imports, you can:
14+
15+
try:
16+
set
17+
except NameError:
18+
from roundup.anypy.sets_ import set
19+
20+
see:
21+
http://docs.python.org/library/sets.html#comparison-to-the-built-in-set-types
22+
23+
"""
24+
25+
try:
26+
set = set # built-in since Python 2.4
27+
except NameError, TypeError:
28+
from sets import Set as set # deprecated as of Python 2.6
29+
30+
# vim: ts=8 sts=4 sw=4 si et

roundup/backends/indexer_common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#$Id: indexer_common.py,v 1.11 2008-09-11 19:41:07 schlatterbeck Exp $
2-
import re, sets
2+
import re
3+
# Python 2.3 ... 2.6 compatibility:
4+
from roundup.anypy.sets_ import set
35

46
from roundup import hyperdb
57

@@ -17,7 +19,7 @@ def _isLink(propclass):
1719

1820
class Indexer:
1921
def __init__(self, db):
20-
self.stopwords = sets.Set(STOPWORDS)
22+
self.stopwords = set(STOPWORDS)
2123
for word in db.config[('main', 'indexer_stopwords')]:
2224
self.stopwords.add(word)
2325

@@ -28,11 +30,11 @@ def getHits(self, search_terms, klass):
2830
return self.find(search_terms)
2931

3032
def search(self, search_terms, klass, ignore={}):
31-
'''Display search results looking for [search, terms] associated
33+
"""Display search results looking for [search, terms] associated
3234
with the hyperdb Class "klass". Ignore hits on {class: property}.
3335
3436
"dre" is a helper, not an argument.
35-
'''
37+
"""
3638
# do the index lookup
3739
hits = self.getHits(search_terms, klass)
3840
if not hits:

roundup/backends/indexer_rdbms.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#$Id: indexer_rdbms.py,v 1.18 2008-09-01 00:43:02 richard Exp $
2-
''' This implements the full-text indexer over two RDBMS tables. The first
2+
""" This implements the full-text indexer over two RDBMS tables. The first
33
is a mapping of words to occurance IDs. The second maps the IDs to (Class,
44
propname, itemid) instances.
5-
'''
6-
import re, sets
5+
"""
6+
import re
7+
# Python 2.3 ... 2.6 compatibility:
8+
from roundup.anypy.sets_ import set
79

810
from roundup.backends.indexer_common import Indexer as IndexerBase
911

@@ -14,27 +16,27 @@ def __init__(self, db):
1416
self.reindex = 0
1517

1618
def close(self):
17-
'''close the indexing database'''
19+
"""close the indexing database"""
1820
# just nuke the circular reference
1921
self.db = None
2022

2123
def save_index(self):
22-
'''Save the changes to the index.'''
24+
"""Save the changes to the index."""
2325
# not necessary - the RDBMS connection will handle this for us
2426
pass
2527

2628
def force_reindex(self):
27-
'''Force a reindexing of the database. This essentially
29+
"""Force a reindexing of the database. This essentially
2830
empties the tables ids and index and sets a flag so
29-
that the databases are reindexed'''
31+
that the databases are reindexed"""
3032
self.reindex = 1
3133

3234
def should_reindex(self):
33-
'''returns True if the indexes need to be rebuilt'''
35+
"""returns True if the indexes need to be rebuilt"""
3436
return self.reindex
3537

3638
def add_text(self, identifier, text, mime_type='text/plain'):
37-
''' "identifier" is (classname, itemid, property) '''
39+
""" "identifier" is (classname, itemid, property) """
3840
if mime_type != 'text/plain':
3941
return
4042

@@ -65,7 +67,7 @@ def add_text(self, identifier, text, mime_type='text/plain'):
6567
text = unicode(text, "utf-8", "replace").upper()
6668
wordlist = [w.encode("utf-8", "replace")
6769
for w in re.findall(r'(?u)\b\w{2,25}\b', text)]
68-
words = sets.Set()
70+
words = set()
6971
for word in wordlist:
7072
if self.is_stopword(word): continue
7173
if len(word) > 25: continue
@@ -77,10 +79,10 @@ def add_text(self, identifier, text, mime_type='text/plain'):
7779
self.db.cursor.executemany(sql, words)
7880

7981
def find(self, wordlist):
80-
'''look up all the words in the wordlist.
82+
"""look up all the words in the wordlist.
8183
If none are found return an empty dictionary
8284
* more rules here
83-
'''
85+
"""
8486
if not wordlist:
8587
return []
8688

0 commit comments

Comments
 (0)