Use re.UNICODE for category/package name regexes.
authorZac Medico <zmedico@gentoo.org>
Sat, 22 Sep 2012 21:52:35 +0000 (14:52 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 22 Sep 2012 22:05:31 +0000 (15:05 -0700)
This only affects r'\w' with Python 2.x, since Python 3 already
defaults to re.UNICODE behavior when compiling unicode str objects
(unless re.ASCII is specified). If a repository wants to ban unicode
categore/package names then we can add a layout.conf setting for that,
as discussed in bug #435934.

pym/_emerge/is_valid_package_atom.py
pym/portage/dbapi/__init__.py
pym/portage/dep/__init__.py
pym/portage/manifest.py
pym/portage/versions.py

index 7cb2a5bb1ee700081daf5c0d229f160135a4eb9d..a1e429414e730f0ed71779f60d6c7b1cbe254fa7 100644 (file)
@@ -1,11 +1,11 @@
-# Copyright 1999-2011 Gentoo Foundation
+# Copyright 1999-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import re
 from portage.dep import isvalidatom
 
 def insert_category_into_atom(atom, category):
-       alphanum = re.search(r'\w', atom)
+       alphanum = re.search(r'\w', atom, re.UNICODE)
        if alphanum:
                ret = atom[:alphanum.start()] + "%s/" % category + \
                        atom[alphanum.start():]
index ad22f3969d0514f9cc22f878f7110e6d19cfb0b4..fc7c7eb3bcab32ada739974a08943b4fef1e570c 100644 (file)
@@ -21,7 +21,7 @@ from portage.exception import InvalidData
 from portage.localization import _
 
 class dbapi(object):
-       _category_re = re.compile(r'^\w[-.+\w]*$')
+       _category_re = re.compile(r'^\w[-.+\w]*$', re.UNICODE)
        _categories = None
        _use_mutable = False
        _known_keys = frozenset(x for x in auxdbkeys
index b4b240d436bef454cf0a650ec84c312783a3f48f..6e030047c87ca614495db90f6331b194dd126dcf 100644 (file)
@@ -72,7 +72,7 @@ def _get_slot_re(eapi_attrs):
        else:
                slot_re = _slot
 
-       slot_re = re.compile('^' + slot_re + '$', re.VERBOSE)
+       slot_re = re.compile('^' + slot_re + '$', re.VERBOSE | re.UNICODE)
 
        _slot_re_cache[cache_key] = slot_re
        return slot_re
@@ -90,7 +90,7 @@ def _get_slot_dep_re(eapi_attrs):
        else:
                slot_re = _slot
 
-       slot_re = re.compile('^' + slot_re + '$', re.VERBOSE)
+       slot_re = re.compile('^' + slot_re + '$', re.VERBOSE | re.UNICODE)
 
        _slot_dep_re_cache[cache_key] = slot_re
        return slot_re
@@ -123,7 +123,7 @@ def _get_atom_re(eapi_attrs):
                '(?P<star>=' + cpv_re + r'\*)|' +
                '(?P<simple>' + cp_re + '))' + 
                '(' + _slot_separator + _slot_loose + ')?' +
-               _repo + ')(' + _use + ')?$', re.VERBOSE)
+               _repo + ')(' + _use + ')?$', re.VERBOSE | re.UNICODE)
 
        _atom_re_cache[cache_key] = atom_re
        return atom_re
@@ -145,7 +145,7 @@ def _get_atom_wildcard_re(eapi_attrs):
                _extended_cat + r')/(' + pkg_re + r'))' + \
                '|(?P<star>=((' + _extended_cat + r')/(' + pkg_re + r'))-(?P<version>\*\d+\*)))' + \
                '(:(?P<slot>' + _slot_loose + r'))?(' +
-               _repo_separator + r'(?P<repo>' + _repo_name + r'))?$')
+               _repo_separator + r'(?P<repo>' + _repo_name + r'))?$', re.UNICODE)
 
        _atom_wildcard_re_cache[cache_key] = atom_re
        return atom_re
@@ -1585,7 +1585,7 @@ def extended_cp_match(extended_cp, other_cp):
        extended_cp_re = _extended_cp_re_cache.get(extended_cp)
        if extended_cp_re is None:
                extended_cp_re = re.compile("^" + re.escape(extended_cp).replace(
-                       r'\*', '[^/]*') + "$")
+                       r'\*', '[^/]*') + "$", re.UNICODE)
                _extended_cp_re_cache[extended_cp] = extended_cp_re
        return extended_cp_re.match(other_cp) is not None
 
index b2f1ff2dcc976a65f975986c19386cd73663487e..25886bb1e35530ba2054a671c1b3fcc3f3eb9bad 100644 (file)
@@ -4,6 +4,7 @@
 import errno
 import io
 import re
+import sys
 import warnings
 
 import portage
@@ -24,6 +25,11 @@ from portage.const import (MANIFEST1_HASH_FUNCTIONS, MANIFEST2_HASH_DEFAULTS,
        MANIFEST2_HASH_FUNCTIONS, MANIFEST2_IDENTIFIERS, MANIFEST2_REQUIRED_HASH)
 from portage.localization import _
 
+if sys.hexversion >= 0x3000000:
+       _unicode = str
+else:
+       _unicode = unicode
+
 # Characters prohibited by repoman's file.name check.
 _prohibited_filename_chars_re = re.compile(r'[^a-zA-Z0-9._\-+:]')
 
@@ -108,6 +114,14 @@ class Manifest2Entry(ManifestEntry):
        def __ne__(self, other):
                return not self.__eq__(other)
 
+       if sys.hexversion < 0x3000000:
+
+               __unicode__ = __str__
+
+               def __str__(self):
+                       return _unicode_encode(self.__unicode__(),
+                               encoding=_encodings['repo.content'], errors='strict')
+
 class Manifest(object):
        parsers = (parseManifest2,)
        def __init__(self, pkgdir, distdir, fetchlist_dict=None,
@@ -289,7 +303,7 @@ class Manifest(object):
                                        # thin manifests with no DIST entries, myentries is
                                        # non-empty for all currently known use cases.
                                        write_atomic(self.getFullname(), "".join("%s\n" %
-                                               str(myentry) for myentry in myentries))
+                                               _unicode(myentry) for myentry in myentries))
                                else:
                                        # With thin manifest, there's no need to have
                                        # a Manifest file if there are no DIST entries.
index 242623fde0ffae866e5c69c22025b42fdc0763ea..a9b7e64fea717ceaa4d585f4ab1c6b2affb2423c 100644 (file)
@@ -79,7 +79,7 @@ def _get_pv_re(eapi_attrs):
        else:
                pv_re = _pv['dots_disallowed_in_PN']
 
-       pv_re = re.compile('^' + pv_re + '$', re.VERBOSE)
+       pv_re = re.compile(_unicode_decode('^' + pv_re + '$'), re.VERBOSE | re.UNICODE)
 
        _pv_re_cache[cache_key] = pv_re
        return pv_re
@@ -292,7 +292,7 @@ def _pkgsplit(mypkg, eapi=None):
 
        return  (m.group('pn'), m.group('ver'), rev) 
 
-_cat_re = re.compile('^%s$' % _cat)
+_cat_re = re.compile('^%s$' % _cat, re.UNICODE)
 _missing_cat = 'null'
 
 def catpkgsplit(mydata, silent=1, eapi=None):