Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

clean up use of mutable objects as argument defaults #828

@choksi81

Description

@choksi81

The use of mutable objects for function argument default values in python is usually unintentional and bug-prone. Example:

def myfunc(firstarg, secondarg=[the list in the above example will be a single object persistent across all function calls, so modification affects future calls to the function.

We should clean these up in our codebase. Here are the places where it appears lists and dictionaries are used in such a way:

trunk/seattlelib/deserialize.repy:def deserialize_removeObjects(strIn, partitions=:


as)):
trunk/seattlelib/dylink.repy:def _default_context(import_context, additional_globals=[dylink_import_module(module,import_context, new_callfunc="import", additional_globals=[](]):
trunk/seattlelib/dylink.repy:def)):
trunk/seattlelib/dylink.repy:  def _dy_import_module(module,new_callfunc="import",additional_globals=[advertise_lookup(key, maxvals=100, lookuptype=['central','opendht','DOR'](]):
trunk/seattlelib/advertise.repy:def), \
trunk/softwareupdater/test/test_updater_local.py:def runRsyncTest(testtype, updatefolder, otherargs=[test_rsync(testtype, softwareurl, chgFile=[](]):
trunk/softwareupdater/test/test_rsync.py:def)):
trunk/multiplexer/src/deserialize.py:def deserialize_removeObjects(strIn, partitions=[]):
trunk/seattlelib/Multiplexer.repy:  def __init__(self, socket, info={}):
trunk/repy/tests/run_tests.py:def exec_repy_script(filename, restrictionsfile, arguments={}, script_args=''):
trunk/multiplexer/src/Multiplexer.py:  def __init__(self, socket, info={}):

The above list intentionally leaves out:

  • trunk/continuousbuild/PyRSS2Gen.py because it's 3rd-party code not part of the actual seattle codebase and changing it would probably risk adding bugs.
  • trunk/autograder/emulab/sshxmlrpc.py because it's 3rd-party code and there doesn't appear to be a bug in the usage (the list is never modified).
    We also need to add a mention of this to the style guide.

I'm CC'ing people who I think are the current maintainers of various of the above files so they can jump in and fix them.

Even if the usages aren't buggy, we should change the above code to avoid the risk that someone could later modify the code and make it buggy. That is, we should fix using mutable objects in this way even if there is no bug currently. If there turns out to be a really good reason for the usage, it should be commented (along with a reminder of the risk of using the mutable object).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions