mirror of
https://github.com/samba-team/samba.git
synced 2025-12-19 12:23:49 +03:00
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 <dmulder@suse.com> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Autobuild-User(master): David Mulder <dmulder@samba.org> Autobuild-Date(master): Fri Oct 2 14:49:36 UTC 2020 on sn-devel-184
This commit is contained in:
committed by
David Mulder
parent
85d2ff2f00
commit
a3cd315321
@@ -1,6 +1,7 @@
|
||||
# Samba common functions
|
||||
#
|
||||
# Copyright (C) Matthieu Patou <mat@matws.net>
|
||||
# Copyright (C) Lumir Balhar <lbalhar@redhat.com> 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
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 <str>' and in PY3 it is
|
||||
# 'class <str>' 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
|
||||
|
||||
Reference in New Issue
Block a user