Skip to content

Commit 1da774b

Browse files
Fix bug related to mutable default parameters
1 parent f116ae3 commit 1da774b

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

zfslib/zfslib.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from collections import OrderedDict
1717
from datetime import datetime, timedelta, date as dt_date
1818

19+
is_py2=(sys.version_info[0] == 2)
1920

2021
class Connection:
2122
host = None
@@ -48,7 +49,8 @@ def __init__(self, host="localhost", trust=False, sshcipher=None, identityfile=N
4849

4950
# Data is cached unless force=True, snapshots have been made since last read
5051
# or properties is different
51-
def load_poolset(self, properties=[], get_mounts=True, force=False, _test_data=None):
52+
def load_poolset(self, properties=None, get_mounts=True, force=False, _test_data=None):
53+
properties = [] if properties is None else properties
5254
if force or self._dirty or not self._props_last == properties:
5355
self._poolset._load(properties=properties, get_mounts=get_mounts, _test_data=_test_data)
5456
self._dirty = False
@@ -57,7 +59,8 @@ def load_poolset(self, properties=[], get_mounts=True, force=False, _test_data=N
5759
return self._poolset
5860

5961

60-
def snapshot_recursively(self, name, snapshotname, properties={}):
62+
def snapshot_recursively(self, name, snapshotname, properties=None):
63+
properties = {} if properties is None else properties
6164
plist = sum( map( lambda x: ['-o', '%s=%s' % x ], properties.items() ), [] )
6265
subprocess.check_call(self.command + ["zfs", "snapshot", "-r" ] + plist + [ "%s@%s" % (name, snapshotname)])
6366
self._dirty = True
@@ -106,6 +109,7 @@ def lookup(self, name):
106109
# get_mounts will automated grabbing of mountpoint and mounted properties and
107110
# store flag for downstream code to know that these flags are available
108111
def _load(self, get_mounts=True, properties=None, _test_data=None):
112+
global is_py2
109113

110114
_pdef=['name', 'creation']
111115

@@ -131,7 +135,7 @@ def _load(self, get_mounts=True, properties=None, _test_data=None):
131135
_base_cmd = self.connection.command
132136

133137
def extract_properties(s):
134-
s = s.decode('utf-8') if not isPy2() and isinstance(s, bytes) else s
138+
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
135139
items = s.strip().split( '\t' )
136140
assert len( items ) == len( properties ), (properties, items)
137141
propvalues = map( lambda x: None if x == '-' else x, items[ 1: ] )
@@ -533,6 +537,7 @@ def __init__(self, pool, name, parent=None):
533537
# - M The path has been modified
534538
# - R The path has been renamed
535539
def get_diffs(self, snap_from, snap_to=None, include=None, exclude=None, file_type=None, chg_type=None):
540+
global is_py2
536541
self.assertHaveMounts()
537542
assert self.mounted, "Cannot get diffs for Unmounted Dataset. Verify mounted flag on Dataset before calling"
538543

@@ -550,6 +555,8 @@ def __tv(k, v):
550555
if isinstance(v, str): return [v]
551556
if isinstance(v, list): return v
552557
raise AssertionError("{} can only be a str or list. Got: {}".format(k, type(v)))
558+
559+
553560
file_type = __tv('file_type', file_type)
554561
chg_type = __tv('chg_type', chg_type)
555562

@@ -564,9 +571,11 @@ def __tv(k, v):
564571

565572

566573
stdout = subprocess.check_output(cmd)
574+
567575
def __row(s):
568-
s = s.decode('utf-8') if not isPy2() and isinstance(s, bytes) else s
576+
if not is_py2 and isinstance(s, bytes): s = s.decode('utf-8')
569577
return s.strip().split( '\t' )
578+
570579
rows = list(map(lambda s: __row(s), stdout.splitlines()))
571580
diffs = []
572581
for row in rows:
@@ -945,13 +954,13 @@ def idfun(x): return x
945954
return result
946955

947956
def expand_user(path):
948-
if isPy2():
957+
global is_py2
958+
if is_py2:
949959
return os.path.expanduser(path)
950960
else:
951961
return pathlib.Path(path).expanduser()
952962

953-
def isPy2():
954-
return (sys.version_info[0] == 2)
963+
955964

956965

957966
''' END Utilities '''

0 commit comments

Comments
 (0)