Bug 1816859

Summary: lib389 - Replace exec() with setattr()
Product: Red Hat Enterprise Linux 8 Reporter: mreynolds
Component: 389-ds-baseAssignee: mreynolds
Status: CLOSED ERRATA QA Contact: RHDS QE <ds-qe-bugs>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 8.2CC: pasik, spichugi, tbordaz, vashirov
Target Milestone: rcFlags: mreynolds: mirror+
Target Release: 8.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: 389-ds-base-1.4.3.8-2.module+el8.3.0+6591+ebfc9766 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-11-04 03:07:44 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description mreynolds 2020-03-24 21:59:07 UTC
Description of problem:


I noticed that https://pagure.io/389-ds-base/blob/master/f/src/lib389/lib389/_constants.py uses exec() a lot to define module global variables. That's rather slow and not very elegant. You can get the current module object with sys.modules[__name__] and then use setattr() instead:

import sys

# current module object
mod = sys.modules[__name__]

setattr(mod, "HOST_STANDALONE{0}".format(N), "LOCALHOST")


Upstream ticket:

https://pagure.io/389-ds-base/issue/50337

Comment 3 Viktor Ashirov 2020-06-02 12:19:22 UTC
# grep setattr /usr/lib/python3.6/site-packages/lib389/_constants.py 
    setattr(mod, "HOST_STANDALONE{0}".format(N), "LOCALHOST")
    setattr(mod, "PORT_STANDALONE{0}".format(N), i)
    setattr(mod, "SECUREPORT_STANDALONE{0}".format(N), i + 24700)
    setattr(mod, "SERVERID_STANDALONE{0}".format(N), "standalone{0}".format(N))
    setattr(mod, "REPLICAID_STANDALONE_{0}".format(N), 65535)
    setattr(mod, "HOST_MASTER_{0}".format(N), "LOCALHOST")
    setattr(mod, "PORT_MASTER_{0}".format(N), i)
    setattr(mod, "SECUREPORT_MASTER_{0}".format(N), i + 24700)
    setattr(mod, "SERVERID_MASTER_{0}".format(N), "master{0}".format(N))
    setattr(mod, "REPLICAID_MASTER_{0}".format(N), N)
    setattr(mod, "HOST_HUB_{0}".format(N), "LOCALHOST")
    setattr(mod, "PORT_HUB_{0}".format(N), i)
    setattr(mod, "SECUREPORT_HUB_{0}".format(N), i + 24700)
    setattr(mod, "SERVERID_HUB_{0}".format(N), "hub{0}".format(N))
    setattr(mod, "REPLICAID_HUB_{0}".format(N), 65535)
    setattr(mod, "HOST_CONSUMER_{0}".format(N), "LOCALHOST")
    setattr(mod, "PORT_CONSUMER_{0}".format(N), i)
    setattr(mod, "SECUREPORT_CONSUMER_{0}".format(N), i + 24700)
    setattr(mod, "SERVERID_CONSUMER_{0}".format(N), "consumer{0}".format(N))

Test that uses these constant passes:
============================================================================ test session starts ============================================================================
platform linux -- Python 3.6.8, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3.6
cachedir: .pytest_cache
389-ds-base: 1.4.3.8-2.module+el8.3.0+6591+ebfc9766
nss: 3.44.0-15.el8
nspr: 4.21.0-2.el8_0
openldap: 2.4.46-11.el8_1
cyrus-sasl: not installed
FIPS: disabled
rootdir: /workspace/ds/dirsrvtests, inifile: pytest.ini
collected 1 item                                                                                                                                                            

dirsrvtests/tests/suites/acl/repeated_ldap_add_test.py::test_repeated_ldap_add PASSED                                                                                 [100%]

====================================================================== 1 passed, 47 warnings in 46.72s ======================================================================

Marking as VERIFIED.

Comment 6 errata-xmlrpc 2020-11-04 03:07:44 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (389-ds:1.4 bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHEA-2020:4695