From be9969c3278cf23f7de123410cec23d873f75c7f Mon Sep 17 00:00:00 2001 From: scott snyder <snyder@bnl.gov> Date: Tue, 21 Jan 2020 16:23:45 +0100 Subject: [PATCH] DQDefects: More python 3 fixes With python 3, we generally should not explictly encode strings before passing them to C++ -- pyroot will end up doing that. We do need the encodes for py 2, though. --- DataQuality/DQDefects/python/db.py | 18 ++++++++++----- DataQuality/DQDefects/python/tags.py | 22 ++++++++++++------- .../DQDefects/python/tests/__init__.py | 2 +- DataQuality/DQDefects/python/virtual_mixin.py | 18 +++++++++------ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/DataQuality/DQDefects/python/db.py b/DataQuality/DQDefects/python/db.py index ba6e4ce0064..be88bac2b33 100644 --- a/DataQuality/DQDefects/python/db.py +++ b/DataQuality/DQDefects/python/db.py @@ -31,6 +31,10 @@ from .virtual_mixin import DefectsDBVirtualDefectsMixin from .virtual_calculator import calculate_virtual_defects import six +if six.PY2: + def _encode (s, enc): return s.encode(enc) +else: + def _encode (s, enc): return s class DefectsDB(DefectsDBVirtualDefectsMixin, @@ -92,8 +96,9 @@ class DefectsDB(DefectsDBVirtualDefectsMixin, raise TypeError('tag argument must be a 2-element sequence') if len(tag) != 2: raise TypeError('tag argument must be a 2-element sequence') - self._tag = tag - self._tag = tagtype(self._tag[0].encode('ascii'), self._tag[1].encode('ascii')) + self._tag = tag + self._tag = tagtype(_encode(self._tag[0],'ascii'), + _encode(self._tag[1],'ascii')) # COOL has no way of emptying a storage buffer. Creating a new storage # buffer flushes the old one. Therefore, if an exception happens @@ -152,8 +157,9 @@ class DefectsDB(DefectsDBVirtualDefectsMixin, if already_exists: raise DefectExistsError('Defect %s already exists' % oldname) - self.defects_folder.createChannel(did, name.encode('ascii'), - description.encode('utf-8')) + self.defects_folder.createChannel(did, + _encode(name,'ascii'), + _encode(description,'utf-8')) self._new_defect(did, name) def retrieve(self, since=None, until=None, channels=None, nonpresent=False, @@ -336,8 +342,8 @@ class DefectsDB(DefectsDBVirtualDefectsMixin, p["present"] = present p["recoverable"] = recoverable - p["user"] = added_by.encode('utf-8') - p["comment"] = comment.encode('utf-8') + p["user"] = _encode(added_by, 'utf-8') + p["comment"] = _encode(comment, 'utf-8') defect_id = self.defect_chan_as_id(defect_id, True) diff --git a/DataQuality/DQDefects/python/tags.py b/DataQuality/DQDefects/python/tags.py index 14f07fd529e..a911aa0c90c 100644 --- a/DataQuality/DQDefects/python/tags.py +++ b/DataQuality/DQDefects/python/tags.py @@ -11,7 +11,13 @@ from DQDefects import DEFECT_LOGIC_TAG_FORMAT from .exceptions import InvalidTagError, InvalidDefectTagError, InvalidLogicTagError from .virtual_mixin import NONHEAD_MODIFICATION_MSG - + +import six +if six.PY2: + def _encode (s, enc): return s.encode(enc) +else: + def _encode (s, enc): return s + class DefectsDBTagsMixin(object): def __init__(self): super(DefectsDBTagsMixin, self).__init__() @@ -49,8 +55,8 @@ class DefectsDBTagsMixin(object): Give the current HEAD of `folder` a new tag and lock it. """ LOCKED = cool.HvsTagLock.LOCKED - name = name.encode('ascii') - description = description.encode('utf-8') + name = _encode(name,'ascii') + description = _encode(description,'utf-8') folder.cloneTagAsUserTag('HEAD', name, description) folder.setTagLockStatus(name, LOCKED) @@ -63,8 +69,8 @@ class DefectsDBTagsMixin(object): and has description "(v%i) blah" """ - defects_tag = defects_tag.encode('ascii') - logics_tag = logics_tag.encode('ascii') + defects_tag = _encode(defects_tag,'ascii') + logics_tag = _encode(logics_tag,'ascii') logic_revision = int(logics_tag.split("-")[-1]) defect_part = "-".join(defects_tag.split("-")[1:]) hierarchical_tag = "DetStatus-v%02i-%s" % (logic_revision, defect_part) @@ -158,7 +164,7 @@ class DefectsDBTagsMixin(object): Parameters: `description` : What changed in this tag? (optional, default "") """ - description = description.encode('utf-8') + description = _encode(description,'utf-8') assert self.logics_tag == "HEAD", NONHEAD_MODIFICATION_MSG new_tag_name = self.next_logics_tag @@ -176,8 +182,8 @@ class DefectsDBTagsMixin(object): `name` : Name of the new tag `description` : Description of the contents of this tag """ - name = name.encode('ascii') - description = description.encode('utf-8') + name = _encode(name,'ascii') + description = _encode(description,'utf-8') if name.startswith("DetStatus"): raise RuntimeError("Only specify the last part of the defect tag") diff --git a/DataQuality/DQDefects/python/tests/__init__.py b/DataQuality/DQDefects/python/tests/__init__.py index d1383228d2d..533078dfa0d 100644 --- a/DataQuality/DQDefects/python/tests/__init__.py +++ b/DataQuality/DQDefects/python/tests/__init__.py @@ -55,7 +55,7 @@ def test_database_creation(): @with_setup(create_database, teardown_database) def test_database_creation_unicode(): assert exists("test_defects.db") - log.info("Created database %s", unicode(TEST_DATABASE)) + log.info("Created database %s", six.ensure_text(TEST_DATABASE)) @with_setup(create_database, teardown_database) def test_database_retrieval(): diff --git a/DataQuality/DQDefects/python/virtual_mixin.py b/DataQuality/DQDefects/python/virtual_mixin.py index a85fd561e63..4270c90afa8 100644 --- a/DataQuality/DQDefects/python/virtual_mixin.py +++ b/DataQuality/DQDefects/python/virtual_mixin.py @@ -7,6 +7,10 @@ from .exceptions import DefectUnknownError, DefectExistsError from .ids import choose_new_defect_id from .virtual_logic import DefectLogic import six +if six.PY2: + def _encode (s, enc): return s.encode(enc) +else: + def _encode (s, enc): return s NONHEAD_MODIFICATION_MSG = ("Operations which modify virtual defects can only " "be done on the HEAD tag.") @@ -55,8 +59,8 @@ class DefectsDBVirtualDefectsMixin(object): # change other tags in a known manner with _update_virtual_defect assert self.logics_tag == "HEAD", NONHEAD_MODIFICATION_MSG assert not self._read_only, "Insertion on read-only database" - clauses = clauses.encode('ascii') - comment = comment.encode('utf-8') if comment is not None else comment + clauses = _encode(clauses,'ascii') + comment = _encode(comment,'utf-8') if comment is not None else comment self._update_virtual_defect(defect_name, clauses, comment) def _update_virtual_defect(self, defect_name, clauses, comment=None, tag=None): @@ -83,8 +87,8 @@ class DefectsDBVirtualDefectsMixin(object): Will fix up all virtual defect dependencies in all tags. """ assert not self._read_only, "Channel rename on read-only database" - defect_name = defect_name.encode('ascii') - new_defect_name = new_defect_name.encode('ascii') + defect_name = _encode(defect_name,'ascii') + new_defect_name = _encode(new_defect_name,'ascii') try: oldname = self.normalize_defect_names(new_defect_name) @@ -149,9 +153,9 @@ class DefectsDBVirtualDefectsMixin(object): assert self.logics_tag == "HEAD", NONHEAD_MODIFICATION_MSG assert not self._read_only, "Insertion on read-only database" from DQUtils.channel_mapping import get_channel_ids_names - clauses = clauses.encode('ascii') - defect_name = defect_name.encode('ascii') - comment = comment.encode('utf-8') if comment is not None else comment + clauses = _encode(clauses,'ascii') + defect_name = _encode(defect_name,'ascii') + comment = _encode(comment,'utf-8') if comment is not None else comment # Force load of defects_folder to populate _defect_payload store = self.defect_logic_folder.storeObject -- GitLab