From a3cd31532125ccb537af6cd9d27c761b7da4b03a Mon Sep 17 00:00:00 2001 From: David Mulder Date: Fri, 11 Sep 2020 14:29:46 -0600 Subject: [PATCH] python2 reduction: Merge remaining compat code into common The remaining compat code (get_string, get_bytes, cmp) are useful helper routines which we should simply merge into common (especially since there is some duplication here). Signed-off-by: David Mulder Reviewed-by: Douglas Bagnall Autobuild-User(master): David Mulder Autobuild-Date(master): Fri Oct 2 14:49:36 UTC 2020 on sn-devel-184 --- python/samba/common.py | 52 +++++++++++-- python/samba/compat.py | 76 ------------------- python/samba/emulate/traffic.py | 2 +- python/samba/gp_parse/__init__.py | 2 +- python/samba/gp_sec_ext.py | 2 +- python/samba/gpclass.py | 2 +- python/samba/join.py | 2 +- python/samba/kcc/__init__.py | 4 +- python/samba/ms_forest_updates_markdown.py | 2 +- python/samba/netcmd/computer.py | 2 +- python/samba/netcmd/contact.py | 2 +- python/samba/netcmd/domain.py | 2 +- python/samba/netcmd/drs.py | 2 +- python/samba/netcmd/group.py | 2 +- python/samba/netcmd/user.py | 4 +- python/samba/provision/sambadns.py | 2 +- python/samba/samba3/__init__.py | 2 +- python/samba/samdb.py | 2 +- python/samba/schema.py | 2 +- .../tests/auth_log_netlogon_bad_creds.py | 2 +- python/samba/tests/auth_log_winbind.py | 2 +- python/samba/tests/blackbox/netads_json.py | 2 +- .../samba/tests/blackbox/samba_dnsupdate.py | 2 +- python/samba/tests/dcerpc/misc.py | 8 +- python/samba/tests/gpo.py | 2 +- python/samba/tests/krb5/kcrypto.py | 2 +- python/samba/tests/ntlm_auth.py | 2 +- python/samba/tests/prefork_restart.py | 2 +- python/samba/tests/py_credentials.py | 2 +- python/samba/tests/samba_tool/help.py | 2 +- python/samba/tests/samba_tool/user.py | 4 +- python/samba/upgradehelpers.py | 4 +- selftest/filter-subunit | 6 +- .../script/tests/test_wbinfo_sids2xids_int.py | 2 +- source4/dsdb/tests/python/acl.py | 2 +- source4/dsdb/tests/python/deletetest.py | 2 +- source4/dsdb/tests/python/ldap.py | 2 +- source4/dsdb/tests/python/sam.py | 2 +- source4/dsdb/tests/python/sort.py | 10 +-- .../tests/python/tombstone_reanimation.py | 2 +- source4/dsdb/tests/python/vlv.py | 4 +- source4/scripting/bin/samba_dnsupdate | 2 +- source4/scripting/bin/samba_spnupdate | 2 +- source4/scripting/bin/samba_upgradeprovision | 6 +- source4/torture/drs/python/drs_base.py | 8 +- source4/torture/drs/python/getnc_exop.py | 14 ++-- .../torture/drs/python/replica_sync_rodc.py | 2 +- .../drs/python/samba_tool_drs_no_dns.py | 2 +- .../drs/python/samba_tool_drs_showrepl.py | 2 +- 49 files changed, 112 insertions(+), 160 deletions(-) delete mode 100644 python/samba/compat.py diff --git a/python/samba/common.py b/python/samba/common.py index a8faa90065d..d5945307c3a 100644 --- a/python/samba/common.py +++ b/python/samba/common.py @@ -1,6 +1,7 @@ # Samba common functions # # Copyright (C) Matthieu Patou +# Copyright (C) Lumir Balhar 2017 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,15 +17,17 @@ # along with this program. If not, see . # -from samba.compat import PY3 +def cmp(x, y): + """ + Replacement for built-in function cmp that was removed in Python 3 -if PY3: - # cmp() exists only in Python 2 - def cmp(a, b): - return (a > b) - (a < b) + Compare the two objects x and y and return an integer according to + the outcome. The return value is negative if x < y, zero if x == y + and strictly positive if x > y. + """ - raw_input = input + return (x > y) - (x < y) def confirm(msg, forced=False, allow_all=False): @@ -53,7 +56,7 @@ def confirm(msg, forced=False, allow_all=False): prompt = '[y/N/all/none]' while True: - v = raw_input(msg + ' %s ' % prompt) + v = input(msg + ' %s ' % prompt) v = v.upper() if v in mapping: return mapping[v] @@ -67,3 +70,38 @@ def normalise_int32(ivalue): return str(ivalue) +# Sometimes in PY3 we have variables whose content can be 'bytes' or +# 'str' and we can't be sure which. Generally this is because the +# code variable can be initialised (or reassigned) a value from different +# api(s) or functions depending on complex conditions or logic. Or another +# common case is in PY2 the variable is 'type ' and in PY3 it is +# 'class ' and the function to use e.g. b64encode requires 'bytes' +# in PY3. In such cases it would be nice to avoid excessive testing in +# the client code. Calling such a helper function should be avoided +# if possible but sometimes this just isn't possible. +# If a 'str' object is passed in it is encoded using 'utf8' or if 'bytes' +# is passed in it is returned unchanged. +# Using this function is PY2/PY3 code should ensure in most cases +# the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly +# encodes the variable (see PY2 implementation of this function below) +def get_bytes(bytesorstring): + tmp = bytesorstring + if isinstance(bytesorstring, str): + tmp = bytesorstring.encode('utf8') + elif not isinstance(bytesorstring, bytes): + raise ValueError('Expected byte or string for %s:%s' % (type(bytesorstring), bytesorstring)) + return tmp + +# helper function to get a string from a variable that maybe 'str' or +# 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed +# it is returned unchanged +# Using this function is PY2/PY3 code should ensure in most cases +# the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly +# decodes the variable (see PY2 implementation of this function below) +def get_string(bytesorstring): + tmp = bytesorstring + if isinstance(bytesorstring, bytes): + tmp = bytesorstring.decode('utf8') + elif not isinstance(bytesorstring, str): + raise ValueError('Expected byte of string for %s:%s' % (type(bytesorstring), bytesorstring)) + return tmp diff --git a/python/samba/compat.py b/python/samba/compat.py deleted file mode 100644 index a2b6f3c8645..00000000000 --- a/python/samba/compat.py +++ /dev/null @@ -1,76 +0,0 @@ -# module which helps with porting to Python 3 -# -# Copyright (C) Lumir Balhar 2017 -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -"""module which helps with porting to Python 3""" - -import sys - -PY3 = sys.version_info[0] == 3 - -if PY3: - # Sometimes in PY3 we have variables whose content can be 'bytes' or - # 'str' and we can't be sure which. Generally this is because the - # code variable can be initialised (or reassigned) a value from different - # api(s) or functions depending on complex conditions or logic. Or another - # common case is in PY2 the variable is 'type ' and in PY3 it is - # 'class ' and the function to use e.g. b64encode requires 'bytes' - # in PY3. In such cases it would be nice to avoid excessive testing in - # the client code. Calling such a helper function should be avoided - # if possible but sometimes this just isn't possible. - # If a 'str' object is passed in it is encoded using 'utf8' or if 'bytes' - # is passed in it is returned unchanged. - # Using this function is PY2/PY3 code should ensure in most cases - # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly - # encodes the variable (see PY2 implementation of this function below) - def get_bytes(bytesorstring): - tmp = bytesorstring - if isinstance(bytesorstring, str): - tmp = bytesorstring.encode('utf8') - elif not isinstance(bytesorstring, bytes): - raise ValueError('Expected byte or string for %s:%s' % (type(bytesorstring), bytesorstring)) - return tmp - - # helper function to get a string from a variable that maybe 'str' or - # 'bytes' if 'bytes' then it is decoded using 'utf8'. If 'str' is passed - # it is returned unchanged - # Using this function is PY2/PY3 code should ensure in most cases - # the PY2 code runs unchanged in PY2 whereas the code in PY3 possibly - # decodes the variable (see PY2 implementation of this function below) - def get_string(bytesorstring): - tmp = bytesorstring - if isinstance(bytesorstring, bytes): - tmp = bytesorstring.decode('utf8') - elif not isinstance(bytesorstring, str): - raise ValueError('Expected byte of string for %s:%s' % (type(bytesorstring), bytesorstring)) - return tmp - - def cmp_fn(x, y): - """ - Replacement for built-in function cmp that was removed in Python 3 - - Compare the two objects x and y and return an integer according to - the outcome. The return value is negative if x < y, zero if x == y - and strictly positive if x > y. - """ - - return (x > y) - (x < y) - # compat functions - from functools import cmp_to_key as cmp_to_key_fn - - -else: - raise NotImplementedError("Samba versions >= 4.11 do not support Python 2.x") diff --git a/python/samba/emulate/traffic.py b/python/samba/emulate/traffic.py index c428b4e369e..9b6bdb6af57 100644 --- a/python/samba/emulate/traffic.py +++ b/python/samba/emulate/traffic.py @@ -53,7 +53,7 @@ from samba.dsdb import ( from samba.dcerpc.misc import SEC_CHAN_BDC from samba import gensec from samba import sd_utils -from samba.compat import get_string +from samba.common import get_string from samba.logger import get_samba_logger import bisect diff --git a/python/samba/gp_parse/__init__.py b/python/samba/gp_parse/__init__.py index 8ddd52d3657..bc6058638f1 100644 --- a/python/samba/gp_parse/__init__.py +++ b/python/samba/gp_parse/__init__.py @@ -21,7 +21,7 @@ from xml.dom import minidom from io import BytesIO from xml.etree.ElementTree import ElementTree, fromstring, tostring from hashlib import md5 -from samba.compat import get_bytes +from samba.common import get_bytes ENTITY_USER_ID = 0 diff --git a/python/samba/gp_sec_ext.py b/python/samba/gp_sec_ext.py index 070bde9fd60..136ba220de7 100644 --- a/python/samba/gp_sec_ext.py +++ b/python/samba/gp_sec_ext.py @@ -18,7 +18,7 @@ import os.path from samba.gpclass import gp_inf_ext from samba.auth import system_session -from samba.compat import get_string +from samba.common import get_string try: from ldb import LdbError from samba.samdb import SamDB diff --git a/python/samba/gpclass.py b/python/samba/gpclass.py index 8e9bfb9f0e3..ac73671eb58 100644 --- a/python/samba/gpclass.py +++ b/python/samba/gpclass.py @@ -23,7 +23,7 @@ sys.path.insert(0, "bin/python") from samba import NTSTATUSError from configparser import ConfigParser from io import StringIO -from samba.compat import get_bytes +from samba.common import get_bytes from abc import ABCMeta, abstractmethod import xml.etree.ElementTree as etree import re diff --git a/python/samba/join.py b/python/samba/join.py index dca9ff634d5..59de000a401 100644 --- a/python/samba/join.py +++ b/python/samba/join.py @@ -49,7 +49,7 @@ import re import os import tempfile from collections import OrderedDict -from samba.compat import get_string +from samba.common import get_string from samba.netcmd import CommandError diff --git a/python/samba/kcc/__init__.py b/python/samba/kcc/__init__.py index 734c7641883..73cdc9f1ef0 100644 --- a/python/samba/kcc/__init__.py +++ b/python/samba/kcc/__init__.py @@ -44,7 +44,7 @@ from samba.kcc.graph import Vertex from samba.kcc.debug import DEBUG, DEBUG_FN, logger from samba.kcc import debug -from samba.compat import cmp_fn +from samba.common import cmp def sort_dsa_by_gc_and_guid(dsa1, dsa2): @@ -61,7 +61,7 @@ def sort_dsa_by_gc_and_guid(dsa1, dsa2): return -1 if not dsa1.is_gc() and dsa2.is_gc(): return +1 - return cmp_fn(ndr_pack(dsa1.dsa_guid), ndr_pack(dsa2.dsa_guid)) + return cmp(ndr_pack(dsa1.dsa_guid), ndr_pack(dsa2.dsa_guid)) def is_smtp_replication_available(): diff --git a/python/samba/ms_forest_updates_markdown.py b/python/samba/ms_forest_updates_markdown.py index 62e9ad9b354..e219211d027 100644 --- a/python/samba/ms_forest_updates_markdown.py +++ b/python/samba/ms_forest_updates_markdown.py @@ -27,7 +27,7 @@ import re import os import markdown import xml.etree.ElementTree as ET -from samba.compat import get_string +from samba.common import get_string # Display specifier updates or otherwise (ignored in forest_update.py) diff --git a/python/samba/netcmd/computer.py b/python/samba/netcmd/computer.py index 86c7b824d82..9d7a220a005 100644 --- a/python/samba/netcmd/computer.py +++ b/python/samba/netcmd/computer.py @@ -34,7 +34,7 @@ from samba.ndr import ndr_unpack, ndr_pack, ndr_print from samba.remove_dc import remove_dns_references from samba.auth import system_session from samba.samdb import SamDB -from samba.compat import get_bytes +from samba.common import get_bytes from subprocess import check_call, CalledProcessError from . import common diff --git a/python/samba/netcmd/contact.py b/python/samba/netcmd/contact.py index d3eeb23be64..064a3ce6691 100644 --- a/python/samba/netcmd/contact.py +++ b/python/samba/netcmd/contact.py @@ -36,7 +36,7 @@ from samba.netcmd import ( SuperCommand, Option, ) -from samba.compat import get_bytes +from samba.common import get_bytes from . import common diff --git a/python/samba/netcmd/domain.py b/python/samba/netcmd/domain.py index 238f3c306c1..000688f4e7a 100644 --- a/python/samba/netcmd/domain.py +++ b/python/samba/netcmd/domain.py @@ -101,7 +101,7 @@ from samba.provision.common import ( from samba.netcmd.pso import cmd_domain_passwordsettings_pso from samba.netcmd.domain_backup import cmd_domain_backup -from samba.compat import get_string +from samba.common import get_string string_version_to_constant = { "2008_R2": DS_DOMAIN_FUNCTION_2008_R2, diff --git a/python/samba/netcmd/drs.py b/python/samba/netcmd/drs.py index cebcca5f489..023b09d0506 100644 --- a/python/samba/netcmd/drs.py +++ b/python/samba/netcmd/drs.py @@ -46,7 +46,7 @@ from samba.uptodateness import ( get_utdv_summary, get_kcc_and_dsas, ) -from samba.compat import get_string +from samba.common import get_string from samba.samdb import get_default_backend_store def drsuapi_connect(ctx): diff --git a/python/samba/netcmd/group.py b/python/samba/netcmd/group.py index 2942d553f2d..b5c86d33019 100644 --- a/python/samba/netcmd/group.py +++ b/python/samba/netcmd/group.py @@ -36,7 +36,7 @@ from samba.dsdb import ( ) from collections import defaultdict from subprocess import check_call, CalledProcessError -from samba.compat import get_bytes +from samba.common import get_bytes import os import tempfile from . import common diff --git a/python/samba/netcmd/user.py b/python/samba/netcmd/user.py index c49dccd704f..f9762e761ea 100644 --- a/python/samba/netcmd/user.py +++ b/python/samba/netcmd/user.py @@ -53,8 +53,8 @@ from samba.netcmd import ( SuperCommand, Option, ) -from samba.compat import get_bytes -from samba.compat import get_string +from samba.common import get_bytes +from samba.common import get_string from . import common # python[3]-gpgme is abandoned since ubuntu 1804 and debian 9 diff --git a/python/samba/provision/sambadns.py b/python/samba/provision/sambadns.py index 4aa132abfa6..8a5d8a93442 100644 --- a/python/samba/provision/sambadns.py +++ b/python/samba/provision/sambadns.py @@ -60,7 +60,7 @@ from samba.provision.common import ( ) from samba.samdb import get_default_backend_store -from samba.compat import get_string +from samba.common import get_string def get_domainguid(samdb, domaindn): res = samdb.search(base=domaindn, scope=ldb.SCOPE_BASE, attrs=["objectGUID"]) diff --git a/python/samba/samba3/__init__.py b/python/samba/samba3/__init__.py index b092f450953..f15bb291394 100644 --- a/python/samba/samba3/__init__.py +++ b/python/samba/samba3/__init__.py @@ -28,7 +28,7 @@ import tdb from samba.samba3 import passdb from samba.samba3 import param as s3param -from samba.compat import get_bytes +from samba.common import get_bytes def fetch_uint32(db, key): try: diff --git a/python/samba/samdb.py b/python/samba/samdb.py index 1da59dcdaab..0ec91ed3970 100644 --- a/python/samba/samdb.py +++ b/python/samba/samdb.py @@ -32,7 +32,7 @@ from samba import dsdb, dsdb_dns from samba.ndr import ndr_unpack, ndr_pack from samba.dcerpc import drsblobs, misc from samba.common import normalise_int32 -from samba.compat import get_bytes, cmp +from samba.common import get_bytes, cmp from samba.dcerpc import security import binascii diff --git a/python/samba/schema.py b/python/samba/schema.py index caea7e358ae..54fc9fc3125 100644 --- a/python/samba/schema.py +++ b/python/samba/schema.py @@ -28,7 +28,7 @@ from samba.dcerpc import security from samba.ms_schema import read_ms_schema from samba.ndr import ndr_pack from samba.samdb import SamDB -from samba.compat import get_string +from samba.common import get_string from samba import dsdb from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL diff --git a/python/samba/tests/auth_log_netlogon_bad_creds.py b/python/samba/tests/auth_log_netlogon_bad_creds.py index a89c0312d1a..a74ea5c706d 100644 --- a/python/samba/tests/auth_log_netlogon_bad_creds.py +++ b/python/samba/tests/auth_log_netlogon_bad_creds.py @@ -37,7 +37,7 @@ from samba.tests import delete_force from samba.dsdb import UF_WORKSTATION_TRUST_ACCOUNT, UF_PASSWD_NOTREQD from samba.dcerpc.misc import SEC_CHAN_WKSTA from samba.dcerpc.netlogon import NETLOGON_NEG_STRONG_KEYS -from samba.compat import get_string +from samba.common import get_string from samba.dcerpc.windows_event_ids import ( EVT_ID_UNSUCCESSFUL_LOGON, EVT_LOGON_NETWORK diff --git a/python/samba/tests/auth_log_winbind.py b/python/samba/tests/auth_log_winbind.py index dcb12fdb4c0..617fec42dbe 100644 --- a/python/samba/tests/auth_log_winbind.py +++ b/python/samba/tests/auth_log_winbind.py @@ -25,7 +25,7 @@ import time from samba.auth import system_session from samba.credentials import Credentials -from samba.compat import get_string, get_bytes +from samba.common import get_string, get_bytes from samba.dcerpc.messaging import AUTH_EVENT_NAME, MSG_AUTH_LOG from samba.dsdb import UF_NORMAL_ACCOUNT from samba.messaging import Messaging diff --git a/python/samba/tests/blackbox/netads_json.py b/python/samba/tests/blackbox/netads_json.py index 1c254468d36..bcae96259c9 100644 --- a/python/samba/tests/blackbox/netads_json.py +++ b/python/samba/tests/blackbox/netads_json.py @@ -19,7 +19,7 @@ import json import re import samba.tests -from samba.compat import get_string +from samba.common import get_string COMMAND = "bin/net ads" # extract keys from non-json version diff --git a/python/samba/tests/blackbox/samba_dnsupdate.py b/python/samba/tests/blackbox/samba_dnsupdate.py index 4360f741217..ae65426e57d 100644 --- a/python/samba/tests/blackbox/samba_dnsupdate.py +++ b/python/samba/tests/blackbox/samba_dnsupdate.py @@ -18,7 +18,7 @@ import samba.tests from io import StringIO -from samba.compat import get_string +from samba.common import get_string from samba.netcmd.main import cmd_sambatool from samba.credentials import Credentials from samba.auth import system_session diff --git a/python/samba/tests/dcerpc/misc.py b/python/samba/tests/dcerpc/misc.py index 009d8d02768..6b58e9451c8 100644 --- a/python/samba/tests/dcerpc/misc.py +++ b/python/samba/tests/dcerpc/misc.py @@ -19,19 +19,13 @@ from samba.dcerpc import misc import samba.tests -from samba.compat import PY3 +from samba.common import cmp text1 = "76f53846-a7c2-476a-ae2c-20e2b80d7b34" text2 = "344edffa-330a-4b39-b96e-2c34da52e8b1" text3 = "00112233-4455-6677-8899-aabbccddeeff" -if PY3: - # cmp() exists only in Python 2 - def cmp(a, b): - return (a > b) - (a < b) - - class GUIDTests(samba.tests.TestCase): def test_str(self): diff --git a/python/samba/tests/gpo.py b/python/samba/tests/gpo.py index c8440929b11..115b71ac61d 100644 --- a/python/samba/tests/gpo.py +++ b/python/samba/tests/gpo.py @@ -32,7 +32,7 @@ from samba.gp_smb_conf_ext import gp_smb_conf_ext import logging from samba.credentials import Credentials from samba.gp_msgs_ext import gp_msgs_ext -from samba.compat import get_bytes +from samba.common import get_bytes from samba.dcerpc import preg from samba.ndr import ndr_pack import codecs diff --git a/python/samba/tests/krb5/kcrypto.py b/python/samba/tests/krb5/kcrypto.py index 2572fa5bab3..64bdbecd8b2 100755 --- a/python/samba/tests/krb5/kcrypto.py +++ b/python/samba/tests/krb5/kcrypto.py @@ -62,7 +62,7 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from samba.tests import TestCase from samba.credentials import Credentials from samba import generate_random_bytes as get_random_bytes -from samba.compat import get_string, get_bytes +from samba.common import get_string, get_bytes class Enctype(object): DES_CRC = 1 diff --git a/python/samba/tests/ntlm_auth.py b/python/samba/tests/ntlm_auth.py index f6ae708ad24..b909db4e8a1 100644 --- a/python/samba/tests/ntlm_auth.py +++ b/python/samba/tests/ntlm_auth.py @@ -19,7 +19,7 @@ import os from subprocess import Popen, PIPE from samba.tests.ntlm_auth_base import NTLMAuthTestCase -from samba.compat import get_string +from samba.common import get_string class NTLMAuthHelpersTests(NTLMAuthTestCase): diff --git a/python/samba/tests/prefork_restart.py b/python/samba/tests/prefork_restart.py index 8557ae0d8d7..2acbcc9befc 100644 --- a/python/samba/tests/prefork_restart.py +++ b/python/samba/tests/prefork_restart.py @@ -33,7 +33,7 @@ from samba.dcerpc import echo, netlogon from samba.messaging import Messaging from samba.samdb import SamDB from samba.credentials import Credentials, DONT_USE_KERBEROS -from samba.compat import get_string +from samba.common import get_string from samba.dsdb import ( UF_WORKSTATION_TRUST_ACCOUNT, UF_PASSWD_NOTREQD) diff --git a/python/samba/tests/py_credentials.py b/python/samba/tests/py_credentials.py index b16c726a023..ecb8271b595 100644 --- a/python/samba/tests/py_credentials.py +++ b/python/samba/tests/py_credentials.py @@ -39,7 +39,7 @@ from samba.dsdb import ( from samba.ndr import ndr_pack from samba.samdb import SamDB from samba import NTSTATUSError, ntstatus -from samba.compat import get_string +from samba.common import get_string import ctypes diff --git a/python/samba/tests/samba_tool/help.py b/python/samba/tests/samba_tool/help.py index 669895041ea..fa7836d8432 100644 --- a/python/samba/tests/samba_tool/help.py +++ b/python/samba/tests/samba_tool/help.py @@ -21,7 +21,7 @@ import re from samba.tests.samba_tool.base import SambaToolCmdTest from samba.tests import BlackboxProcessError from samba.tests import check_help_consistency -from samba.compat import get_string +from samba.common import get_string class HelpTestCase(SambaToolCmdTest): diff --git a/python/samba/tests/samba_tool/user.py b/python/samba/tests/samba_tool/user.py index c710623339c..22f76333ae2 100644 --- a/python/samba/tests/samba_tool/user.py +++ b/python/samba/tests/samba_tool/user.py @@ -27,8 +27,8 @@ from samba import ( ) from samba.ndr import ndr_unpack from samba.dcerpc import drsblobs -from samba.compat import get_bytes -from samba.compat import get_string +from samba.common import get_bytes +from samba.common import get_string from samba.tests import env_loadparm diff --git a/python/samba/upgradehelpers.py b/python/samba/upgradehelpers.py index bc16590df28..69f6e3675e8 100644 --- a/python/samba/upgradehelpers.py +++ b/python/samba/upgradehelpers.py @@ -28,7 +28,7 @@ import re import shutil import samba -from samba.compat import cmp_fn +from samba.common import cmp from samba import Ldb, version, ntacls from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE import ldb @@ -284,7 +284,7 @@ def dn_sort(x, y): len2 = len(tab2) - 1 # Note: python range go up to upper limit but do not include it for i in range(0, minimum): - ret = cmp_fn(tab1[len1 - i], tab2[len2 - i]) + ret = cmp(tab1[len1 - i], tab2[len2 - i]) if ret != 0: return ret else: diff --git a/selftest/filter-subunit b/selftest/filter-subunit index d67fbaceb51..99e1c41e5ee 100755 --- a/selftest/filter-subunit +++ b/selftest/filter-subunit @@ -102,12 +102,8 @@ else: flapping=flapping) try: - from samba.compat import PY3 from io import TextIOWrapper as TextIOWrapper - if PY3: - forgiving_stdin = TextIOWrapper(sys.stdin.buffer, errors='ignore', encoding='utf-8') - else: - forgiving_stdin = sys.stdin + forgiving_stdin = TextIOWrapper(sys.stdin.buffer, errors='ignore', encoding='utf-8') ret = subunithelper.parse_results(msg_ops, statistics, forgiving_stdin) except subunithelper.ImmediateFail: sys.stdout.flush() diff --git a/source3/script/tests/test_wbinfo_sids2xids_int.py b/source3/script/tests/test_wbinfo_sids2xids_int.py index cb8d8fe2af3..748141949ed 100755 --- a/source3/script/tests/test_wbinfo_sids2xids_int.py +++ b/source3/script/tests/test_wbinfo_sids2xids_int.py @@ -4,7 +4,7 @@ from __future__ import print_function import sys import os import subprocess -from samba.compat import get_string +from samba.common import get_string if len(sys.argv) != 3: diff --git a/source4/dsdb/tests/python/acl.py b/source4/dsdb/tests/python/acl.py index 6c46a6130df..33810ac6895 100755 --- a/source4/dsdb/tests/python/acl.py +++ b/source4/dsdb/tests/python/acl.py @@ -11,7 +11,7 @@ sys.path.insert(0, "bin/python") import samba from samba.tests.subunitrun import SubunitOptions, TestProgram -from samba.compat import get_string +from samba.common import get_string import samba.getopt as options from samba.join import DCJoinContext diff --git a/source4/dsdb/tests/python/deletetest.py b/source4/dsdb/tests/python/deletetest.py index a2a19a12650..612396002d6 100755 --- a/source4/dsdb/tests/python/deletetest.py +++ b/source4/dsdb/tests/python/deletetest.py @@ -20,7 +20,7 @@ from ldb import ERR_UNWILLING_TO_PERFORM, ERR_OPERATIONS_ERROR from samba.samdb import SamDB from samba.tests import delete_force from samba import dsdb -from samba.compat import get_string +from samba.common import get_string parser = optparse.OptionParser("deletetest.py [options] ") sambaopts = options.SambaOptions(parser) diff --git a/source4/dsdb/tests/python/ldap.py b/source4/dsdb/tests/python/ldap.py index a90bf367b1b..9c836c04267 100755 --- a/source4/dsdb/tests/python/ldap.py +++ b/source4/dsdb/tests/python/ldap.py @@ -52,7 +52,7 @@ from samba.dsdb import (UF_NORMAL_ACCOUNT, from samba.ndr import ndr_pack, ndr_unpack from samba.dcerpc import security, lsa from samba.tests import delete_force -from samba.compat import get_string +from samba.common import get_string parser = optparse.OptionParser("ldap.py [options] ") sambaopts = options.SambaOptions(parser) diff --git a/source4/dsdb/tests/python/sam.py b/source4/dsdb/tests/python/sam.py index d6ca63aedf6..41c348bce5f 100755 --- a/source4/dsdb/tests/python/sam.py +++ b/source4/dsdb/tests/python/sam.py @@ -16,7 +16,7 @@ import samba.getopt as options from samba.credentials import Credentials, DONT_USE_KERBEROS from samba.auth import system_session -from samba.compat import get_string +from samba.common import get_string from ldb import SCOPE_BASE, LdbError from ldb import ERR_NO_SUCH_OBJECT, ERR_ATTRIBUTE_OR_VALUE_EXISTS diff --git a/source4/dsdb/tests/python/sort.py b/source4/dsdb/tests/python/sort.py index 7b929eaefb6..51d797af38d 100644 --- a/source4/dsdb/tests/python/sort.py +++ b/source4/dsdb/tests/python/sort.py @@ -14,8 +14,8 @@ import re sys.path.insert(0, "bin/python") import samba from samba.tests.subunitrun import SubunitOptions, TestProgram -from samba.compat import cmp_fn -from samba.compat import cmp_to_key_fn +from samba.common import cmp +from functools import cmp_to_key import samba.getopt as options from samba.auth import system_session @@ -285,10 +285,10 @@ class BaseSortTests(samba.tests.TestCase): return locale.strcoll(a[0], b[0]) def cmp_binary(a, b): - return cmp_fn(a[0], b[0]) + return cmp(a[0], b[0]) def cmp_numeric(a, b): - return cmp_fn(int(a[0]), int(b[0])) + return cmp(int(a[0]), int(b[0])) # For testing simplicity, the attributes in here need to be # unique for each user. Otherwise there are multiple possible @@ -303,7 +303,7 @@ class BaseSortTests(samba.tests.TestCase): for sort_attr, result_attr in attr_pairs: forward = sorted(((norm(x[sort_attr]), norm(x[result_attr])) for x in self.users), - key=cmp_to_key_fn(sort_functions[sort_attr])) + key=cmp_to_key(sort_functions[sort_attr])) reverse = list(reversed(forward)) for rev in (0, 1): diff --git a/source4/dsdb/tests/python/tombstone_reanimation.py b/source4/dsdb/tests/python/tombstone_reanimation.py index 3c15c8e176f..ad2ef80c09e 100755 --- a/source4/dsdb/tests/python/tombstone_reanimation.py +++ b/source4/dsdb/tests/python/tombstone_reanimation.py @@ -31,7 +31,7 @@ from samba.dcerpc import security from samba.dcerpc import drsblobs from samba.dcerpc.drsuapi import * from samba.tests.password_test import PasswordCommon -from samba.compat import get_string +from samba.common import get_string import samba.tests from ldb import (SCOPE_BASE, FLAG_MOD_ADD, FLAG_MOD_DELETE, FLAG_MOD_REPLACE, Dn, Message, diff --git a/source4/dsdb/tests/python/vlv.py b/source4/dsdb/tests/python/vlv.py index 86ac2b72240..c7578c6f04b 100644 --- a/source4/dsdb/tests/python/vlv.py +++ b/source4/dsdb/tests/python/vlv.py @@ -18,8 +18,8 @@ import samba.getopt as options from samba.auth import system_session import ldb from samba.samdb import SamDB -from samba.compat import get_bytes -from samba.compat import get_string +from samba.common import get_bytes +from samba.common import get_string import time diff --git a/source4/scripting/bin/samba_dnsupdate b/source4/scripting/bin/samba_dnsupdate index 50381d88823..fcd4cc5b7da 100755 --- a/source4/scripting/bin/samba_dnsupdate +++ b/source4/scripting/bin/samba_dnsupdate @@ -49,7 +49,7 @@ from samba.dcerpc import netlogon, winbind from samba.netcmd.dns import cmd_dns from samba import gensec from samba.kcc import kcc_utils -from samba.compat import get_string +from samba.common import get_string import ldb import dns.resolver diff --git a/source4/scripting/bin/samba_spnupdate b/source4/scripting/bin/samba_spnupdate index 9d3a7fab4d9..84ff771eab2 100755 --- a/source4/scripting/bin/samba_spnupdate +++ b/source4/scripting/bin/samba_spnupdate @@ -41,7 +41,7 @@ from samba import getopt as options from samba.auth import system_session from samba.samdb import SamDB from samba.credentials import Credentials, DONT_USE_KERBEROS -from samba.compat import get_string +from samba.common import get_string parser = optparse.OptionParser("samba_spnupdate") sambaopts = options.SambaOptions(parser) diff --git a/source4/scripting/bin/samba_upgradeprovision b/source4/scripting/bin/samba_upgradeprovision index 6ac4da6987a..376c4c06318 100755 --- a/source4/scripting/bin/samba_upgradeprovision +++ b/source4/scripting/bin/samba_upgradeprovision @@ -70,7 +70,7 @@ from samba.upgradehelpers import (dn_sort, get_paths, newprovision, increment_calculated_keyversion_number, print_provision_ranges) from samba.xattr import copytree_with_xattrs -from samba.compat import cmp_to_key_fn +from functools import cmp_to_key # make sure the script dies immediately when hitting control-C, # rather than raising KeyboardInterrupt. As we do all database @@ -1130,8 +1130,8 @@ def update_partition(ref_samdb, samdb, basedn, names, schema, provisionUSNs, pre # Sort the missing object in order to have object of the lowest level # first (which can be containers for higher level objects) - listMissing.sort(key=cmp_to_key_fn(dn_sort)) - listPresent.sort(key=cmp_to_key_fn(dn_sort)) + listMissing.sort(key=cmp_to_key(dn_sort)) + listPresent.sort(key=cmp_to_key(dn_sort)) # The following lines is to load the up to # date schema into our current LDB diff --git a/source4/torture/drs/python/drs_base.py b/source4/torture/drs/python/drs_base.py index 33fb1b3a89a..0046ae235f1 100644 --- a/source4/torture/drs/python/drs_base.py +++ b/source4/torture/drs/python/drs_base.py @@ -39,8 +39,8 @@ from ldb import ( Message, FLAG_MOD_REPLACE, ) -from samba.compat import cmp_fn -from samba.compat import get_string +from samba.common import cmp +from samba.common import get_string class DrsBaseTestCase(SambaToolCmdTest): @@ -529,7 +529,7 @@ class AbstractLink: print("AbstractLink.__internal_cmp__(%r, %r) => wrong type" % (self, other)) return NotImplemented - c = cmp_fn(self.selfGUID_blob, other.selfGUID_blob) + c = cmp(self.selfGUID_blob, other.selfGUID_blob) if c != 0: if verbose: print("AbstractLink.__internal_cmp__(%r, %r) => %d different identifier" % (self, other, c)) @@ -550,7 +550,7 @@ class AbstractLink: print("AbstractLink.__internal_cmp__(%r, %r) => %d different FLAG_ACTIVE" % (self, other, c)) return c - c = cmp_fn(self.targetGUID_blob, other.targetGUID_blob) + c = cmp(self.targetGUID_blob, other.targetGUID_blob) if c != 0: if verbose: print("AbstractLink.__internal_cmp__(%r, %r) => %d different target" % (self, other, c)) diff --git a/source4/torture/drs/python/getnc_exop.py b/source4/torture/drs/python/getnc_exop.py index 87571ca252b..446c7821d54 100644 --- a/source4/torture/drs/python/getnc_exop.py +++ b/source4/torture/drs/python/getnc_exop.py @@ -42,8 +42,8 @@ from ldb import SCOPE_BASE from samba.dcerpc import drsuapi, misc, drsblobs from samba.drs_utils import drs_DsBind from samba.ndr import ndr_unpack, ndr_pack -from samba.compat import cmp_to_key_fn -from samba.compat import cmp_fn +from functools import cmp_to_key +from samba.common import cmp def _linked_attribute_compare(la1, la2): @@ -52,7 +52,7 @@ def _linked_attribute_compare(la1, la2): la2, la2_target = la2 # Ascending host object GUID - c = cmp_fn(ndr_pack(la1.identifier.guid), ndr_pack(la2.identifier.guid)) + c = cmp(ndr_pack(la1.identifier.guid), ndr_pack(la2.identifier.guid)) if c != 0: return c @@ -68,7 +68,7 @@ def _linked_attribute_compare(la1, la2): return 1 if la1_active else -1 # Ascending target object GUID - return cmp_fn(ndr_pack(la1_target), ndr_pack(la2_target)) + return cmp(ndr_pack(la1_target), ndr_pack(la2_target)) class DrsReplicaSyncTestCase(drs_base.DrsBaseTestCase): @@ -1057,7 +1057,7 @@ class DrsReplicaSyncSortTestCase(drs_base.DrsBaseTestCase): link.identifier.guid, target_guid) in expected_links) - no_inactive.sort(key=cmp_to_key_fn(_linked_attribute_compare)) + no_inactive.sort(key=cmp_to_key(_linked_attribute_compare)) # assert the two arrays are the same self.assertEqual(len(expected_links), ctr.linked_attributes_count) @@ -1081,7 +1081,7 @@ class DrsReplicaSyncSortTestCase(drs_base.DrsBaseTestCase): link.identifier.guid, target_guid) in expected_links) - has_inactive.sort(key=cmp_to_key_fn(_linked_attribute_compare)) + has_inactive.sort(key=cmp_to_key(_linked_attribute_compare)) # assert the two arrays are the same self.assertEqual(len(expected_links), ctr.linked_attributes_count) @@ -1129,7 +1129,7 @@ class DrsReplicaSyncSortTestCase(drs_base.DrsBaseTestCase): link.value.blob).guid no_inactive.append((link, target_guid)) - no_inactive.sort(key=cmp_to_key_fn(_linked_attribute_compare)) + no_inactive.sort(key=cmp_to_key(_linked_attribute_compare)) # assert the two arrays are the same self.assertEqual([x[0] for x in no_inactive], ctr.linked_attributes) diff --git a/source4/torture/drs/python/replica_sync_rodc.py b/source4/torture/drs/python/replica_sync_rodc.py index b904e17575c..cbdcc12e5ae 100644 --- a/source4/torture/drs/python/replica_sync_rodc.py +++ b/source4/torture/drs/python/replica_sync_rodc.py @@ -32,7 +32,7 @@ import drs_base import samba.tests import time import ldb -from samba.compat import get_string +from samba.common import get_string from ldb import ( SCOPE_BASE, LdbError, ERR_NO_SUCH_OBJECT) diff --git a/source4/torture/drs/python/samba_tool_drs_no_dns.py b/source4/torture/drs/python/samba_tool_drs_no_dns.py index 326181a7fd7..41ae51374ce 100644 --- a/source4/torture/drs/python/samba_tool_drs_no_dns.py +++ b/source4/torture/drs/python/samba_tool_drs_no_dns.py @@ -30,7 +30,7 @@ import ldb import drs_base from samba.tests import BlackboxProcessError -from samba.compat import get_string +from samba.common import get_string class SambaToolDrsNoDnsTests(drs_base.DrsBaseTestCase): diff --git a/source4/torture/drs/python/samba_tool_drs_showrepl.py b/source4/torture/drs/python/samba_tool_drs_showrepl.py index f04ee122f04..e77f15d2b54 100644 --- a/source4/torture/drs/python/samba_tool_drs_showrepl.py +++ b/source4/torture/drs/python/samba_tool_drs_showrepl.py @@ -26,7 +26,7 @@ import re import json import ldb import random -from samba.compat import get_string +from samba.common import get_string GUID_RE = r'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}' HEX8_RE = r'0x[\da-f]{8}'