Repository: dmwm/wmcore
Issue: Wrong string conversion in DBFormatter.formatDict method

**Impact of the bug**
WMAgent (apparently only with oracle backend)

**Describe the bug**
When deploying WMAgent, the step where the resource-control tables get populate fail because the DAO output fails to get properly formatted. Issue seems to come from the DBFormatter changes applied in this PR (which despite being closed, it has actually being merged):
https://github.com/dmwm/WMCore/pull/10057

so it's related to the complicated way of dealing with strings in py2/py3.

**How to reproduce it**
Source the agent environment and run this command (in an agent running oracle):
```
$manage execute-agent wmagent-resource-control --add-T1s --plugin=SimpleCondorPlugin --pending-slots=50 --running-slots=50 --down
```

**Expected behavior**
Strings/unicode should be properly converted and sites should be successfully inserted into the WMAgent resource-control database.

**Additional context and error message**
Traceback from running the command above:
```
cmst1@vocms0192:/data/srv/wmagent/current $ $manage execute-agent wmagent-resource-control --add-T1s --plugin=SimpleCondorPlugin --pending-slots=50 --running-slots=50 --down
WMAgent with Rucio enabled: true
Executing wmagent-resource-control --add-T1s --plugin=SimpleCondorPlugin --pending-slots=50 --running-slots=50 --down ...
Retrieved 7 maps from https://cms-cric.cern.ch/
Traceback (most recent call last):
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/bin/wmagent-resource-control", line 325, in <module>
    options.pendingSlots, options.runningSlots, state=options.state)
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/bin/wmagent-resource-control", line 131, in addSites
    ceName or siteName, allSites[siteName], plugin, cmsName or siteName)
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/bin/wmagent-resource-control", line 209, in updateSiteInfo
    if resourceControl.listSiteInfo(siteName) is None:
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/lib/python2.7/site-packages/WMCore/ResourceControl/ResourceControl.py", line 125, in listSiteInfo
    transaction=self.existingTransaction())
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/lib/python2.7/site-packages/WMCore/WMBS/MySQL/Locations/GetSiteInfo.py", line 32, in execute
    return self.formatDict(results)
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/cms/wmagent/1.4.5.pre3-comp/lib/python2.7/site-packages/WMCore/Database/DBFormatter.py", line 81, in formatDict
    entry[str(descriptions[index].lower())] = bytes(i[index])
  File "/data/srv/wmagent/v1.4.5.pre3-comp/sw.amaltaro/slc7_amd64_gcc630/external/py2-future/0.18.2-comp/lib/python2.7/site-packages/future/types/newbytes.py", line 97, in __new__
    raise TypeError('unicode string argument without an encoding')
TypeError: unicode string argument without an encoding
```


Changed files:
src/python/WMCore/Database/DBFormatter.py

diff --git a/src/python/WMCore/Database/DBFormatter.py b/src/python/WMCore/Database/DBFormatter.py
index 04bb668b7..4175ec6c1 100644
--- a/src/python/WMCore/Database/DBFormatter.py
+++ b/src/python/WMCore/Database/DBFormatter.py
@@ -78,7 +78,7 @@ class DBFormatter(WMObject):
                 for index in range(0, len(descriptions)):
                     # WARNING: Oracle returns table names in CAP!
                     if isinstance(i[index], str):
-                        entry[str(descriptions[index].lower())] = bytes(i[index])
+                        entry[str(descriptions[index].lower())] = i[index].encode("utf-8")
                     else:
                         entry[str(descriptions[index].lower())] = i[index]
 
@@ -99,7 +99,7 @@ class DBFormatter(WMObject):
             for i in r.fetchall():
                 for index in range(0, len(descriptions)):
                     if isinstance(i[index], str):
-                        listOut.append(bytes(i[index]))
+                        listOut.append(i[index].encode("utf-8"))
                     else:
                         listOut.append(i[index])
             r.close()