Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:46:40+00:00 2026-05-29T09:46:40+00:00

def leopardRemoveWireless(networkName): plistPath = ‘/Library/Preferences/SystemConfiguration/preferences.plist’ # Sanity checks for the plist if os.path.exists(plistPath): try:

  • 0
def leopardRemoveWireless(networkName):
  plistPath = '/Library/Preferences/SystemConfiguration/preferences.plist'
  # Sanity checks for the plist
  if os.path.exists(plistPath):
    try:
      pl = NSMutableDictionary.dictionaryWithContentsOfFile_(plistPath)
    except:
      print 'Unable to parse file at path: %s' % plistPath
      sys.exit(1)
  else:
    print 'File does not exist at path: %s' % plistPath
    sys.exit(1)
  # Create a copy of the dictionary due to emuration
  copy = NSDictionary.dictionaryWithDictionary_(pl)
  # Iterate through network sets
  for Set in copy['Sets']:
    UserDefinedName = copy['Sets'][Set]['UserDefinedName']
    print 'Processing location: %s' % UserDefinedName

    for enX in copy['Sets'][Set]['Network']['Interface']:
      print 'Processing interface: %s' % enX
      # I think this will always be a single key but this works either way
      for key in copy['Sets'][Set]['Network']['Interface'][enX]:
        print 'Processing Service: %s' % key
        # Try to grab the PreferredNetworks key if any
        try:
          # Iterate through preferred network sets
          index = 0
          for PreferredNetwork in copy['Sets'][Set]['Network']['Interface'][enX][key]['PreferredNetworks']:
            SSID_STR = PreferredNetwork['SSID_STR']
            print 'Processing SSID: %s' % SSID_STR
            # If the preferred network matches our removal SSID
            if SSID_STR == networkName:
              print 'Found SSID %s to remove' % SSID_STR
              # Delete our in ram copy
              print 'Processing Set: %s' % Set
              print 'Processing enX: %s' % enX
              print 'Processing key: %s' % key
              del pl['Sets'][Set]['Network']['Interface'][enX][key]['PreferredNetworks'][index]
            index += 1
        except KeyError:
           print 'Skipping interface without PreferredNetworks'

I am editing a fairly complex (dictionary) plist and then writing the changes back to the file after I find a specific key value pair. The issue is that even though I am making a copy of the Property lists dictionary:

copy = NSDictionary.dictionaryWithDictionary_(pl)

It is giving me the standard “mutated while enumerated” error when I edit the original, just the loop keys as stand ins (notice the lack of quotes).

del pl['Sets'][Set]['Network']['Interface'][enX][key]['PreferredNetworks'][index]

Is my syntax somehow causing it to try and edit the pl dictionary rather then the copy? Here is the output:

…
Processing Set: D5C0A0F4-613A-4121-B6AE-4CBA6E2635FF
Processing enX: en1
Processing key: AirPort
Traceback (most recent call last):
File “/Users/tester/Desktop/wifiutil”, line 1164, in
sys.exit(main())
File “/Users/tester/Desktop/wifiutil”, line 1135, in main
removeWireless(osVersion,network)
File “/Users/tester/Desktop/wifiutil”, line 1051, in removeWireless
leopardRemoveWireless(network)
File “/Users/tester/Desktop/wifiutil”, line 528, in leopardRemoveWireless
for PreferredNetwork in copy[‘Sets’][Set][‘Network’][‘Interface’][enX][key][‘PreferredNetworks’]:
File “/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC/objc/_convenience.py”, line 431, in enumeratorGenerator
yield container_unwrap(anEnumerator.nextObject(), StopIteration)
objc.error: NSGenericException – * Collection was mutated while being enumerated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:46:41+00:00Added an answer on May 29, 2026 at 9:46 am

    I believe that the issue is the nested nature of the dictionary. dictionaryWithDictionary_() doesn’t do anything like a deep copy; all it does is create a new NSDictionary and copy the pointers for the values (it does copy the keys themselves, since that is the nature of NSDictionary).

    This means that, although you have a new top-level which you can use for enumeration, the inner dictionaries and arrays are the exact same objects as are in the original.

    Your last loop:

    for PreferredNetwork in copy['Sets'][Set]['Network']['Interface'][enX][key]['PreferredNetworks']:
    

    is enumerating one of those inner arrays, which you then try to mutate with a del statement:

    del pl['Sets'][Set]['Network']['Interface'][enX][key]['PreferredNetworks'][index]
    

    This wasn’t copied; it is the same array object that you are using in the for, which causes an exception. You can test this by passing the two expressions to id().

    You’ll have to either do a full-depth copy of the original dictionary, or (probably better) take a copy of the last level before you enumerate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def load_lib path = File.join(File.dirname(__FILE__), 'lib') failures = [] Dir.glob(#{path}/**/*.rb).each { |file| puts loading:
def path(request, mypath): mypath = request.path_info _listdir = os.listdir(mypath) # ['folder1', 'folder2', 'folder3', 'folder4']
def get_pkgs(): pkgs = [] for importer, modname, ispkg in \ pkgutil.walk_packages(path=None, onerror=lambda x:
def foo (): x = re.compile('^abc') foo2(x) def foo2(x): How do I get x
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.