# Buggy code
def copy_to_my_settings(self, unl, which):
    """copy_to_my_settings - copy setting from leoSettings.leo

    :param str unl: Leo UNL to copy from
    :param int which: 1-3, leaf, leaf's parent, leaf's grandparent
    :return: unl of leaf copy in myLeoSettings.leo
    :rtype: str
    """
    trace = False and not g.unitTesting
    if trace:
        g.es(unl)
    path, unl = unl.split("#", 1)
    # Undo the replacements made in p.getUNL.
    path = path.replace("file://", "")
    unl = unl.replace("%20", " ").split("-->")
    tail = []
    if which > 1:  # copying parent or grandparent but select leaf later
        tail = unl[-(which - 1) :]
    unl = unl[: len(unl) + 1 - which]
    my_settings_c = self.c.openMyLeoSettings()
    my_settings_c.save()  # if it didn't exist before, save required
    settings = g.findNodeAnywhere(my_settings_c, "@settings")
    c2 = g.app.loadManager.openSettingsFile(path)
    found, maxdepth, maxp = g.recursiveUNLFind(unl, c2)
    if found:
        if trace:
            g.trace("FOUND", unl)
        dest = maxp.get_UNL()
    else:
        if trace:
            g.trace("CREATING", unl)
        nd = settings.insertAsLastChild()
        dest = nd.get_UNL()
        self.copy_recursively(maxp, nd)
        my_settings_c.redraw()
        shortcutsDict, settingsDict = g.app.loadManager.createSettingsDicts(
            my_settings_c, False
        )
        self.c.config.settingsDict.update(settingsDict)
        my_settings_c.config.settingsDict.update(settingsDict)
    if trace:
        g.trace("-->".join([dest] + tail))
    return "-->".join([dest] + tail)


# Traceback (TypeError)
$ python launchLeo.py

setting leoID from os.getenv('USER'): '<user>'
reading settings in /Users/<user>/build-dirs/leo-editor/leo/config/leoSettings.leo
reading settings in /Users/<user>/.leo/myLeoSettings.leo
Using default leo file name:
/Users/<user>/.leo/workbook.leo

** isPython3: True
Leo 5.4, build 20170301100728, Wed Mar  1 10:07:28 CST 2017
Git repo info: branch = master, commit = 8f01f57131ae
Python 3.5.3, PyQt version 5.6.2
darwin
reading settings in /Users/<user>/build-dirs/leo-editor/leo/doc/CheatSheet.leo
wrote recent file: /Users/<user>/.leo/.leoRecentFiles.txt
reading settings in /Users/<user>/.leo/myLeoSettings.leo
read only: unl:///Users/<user>/build-dirs/leo-editor/leo/config/leoSettings.leo
error parsing unl:///Users/<user>/build-dirs/leo-editor/leo/config/leoSettings.leo
Traceback (most recent call last):

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoFileCommands.py", line 1243, in parse_leo_file
s = str(s, encoding='utf-8')

TypeError: coercing to str: need a bytes-like object, NoneType found

exception executing command
Traceback (most recent call last):

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoCommands.py", line 5501, in doCommand
val = command(event)

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoConfig.py", line 2149, in f
self.find_setting(setting)

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoConfig.py", line 2112, in find_setting
unl = self.copy_to_my_settings(unl, which=2)

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoConfig.py", line 2068, in copy_to_my_settings
c2 = g.app.loadManager.openSettingsFile(path)

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoApp.py", line 1903, in openSettingsFile
readAtFileNodesFlag=False, silent=True)

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoFileCommands.py", line 833, in openLeoFile
silent=silent,

File "/Users/<user>/build-dirs/leo-editor/leo/core/leoFileCommands.py", line 698, in getLeoFile
theFile.close()

AttributeError: 'NoneType' object has no attribute 'close'

# Fixed code
def copy_to_my_settings(self, unl, which):
    """copy_to_my_settings - copy setting from leoSettings.leo

    :param str unl: Leo UNL to copy from
    :param int which: 1-3, leaf, leaf's parent, leaf's grandparent
    :return: unl of leaf copy in myLeoSettings.leo
    :rtype: str
    """
    trace = False and not g.unitTesting
    if trace:
        g.es(unl)
    path, unl = unl.split("#", 1)
    # Undo the replacements made in p.getUNL.
    path = path.replace("file://", "")
    path = path.replace("unl://", "")
    # Fix #434: Potential bug in settings
    unl = unl.replace("%20", " ").split("-->")
    tail = []
    if which > 1:  # copying parent or grandparent but select leaf later
        tail = unl[-(which - 1) :]
    unl = unl[: len(unl) + 1 - which]
    my_settings_c = self.c.openMyLeoSettings()
    my_settings_c.save()  # if it didn't exist before, save required
    settings = g.findNodeAnywhere(my_settings_c, "@settings")
    c2 = g.app.loadManager.openSettingsFile(path)
    if not c2:
        return ""  # Fix 434.
    found, maxdepth, maxp = g.recursiveUNLFind(unl, c2)
    if found:
        if trace:
            g.trace("FOUND", unl)
        dest = maxp.get_UNL()
    else:
        if trace:
            g.trace("CREATING", unl)
        nd = settings.insertAsLastChild()
        dest = nd.get_UNL()
        self.copy_recursively(maxp, nd)
        my_settings_c.redraw()
        shortcutsDict, settingsDict = g.app.loadManager.createSettingsDicts(
            my_settings_c, False
        )
        self.c.config.settingsDict.update(settingsDict)
        my_settings_c.config.settingsDict.update(settingsDict)
    if trace:
        g.trace("-->".join([dest] + tail))
    return "-->".join([dest] + tail)