Search code examples
pythondictionaryexecconfig

Python 3.3 config file - dictionary getting filled with random things


I have been making a webserver for the past month, and decided I wanted to have global settings in a config file. The config file content:

#CONFIG FILE
name = "Webserver"
version = "0.0"
python_version = "Python 33"
database_type = "SQLite 3"

When I want to access all values in the file, I read I should use the following code:

settingscfg = {}
exec(open("settings.cfg").read(), settingscfg)
print(settingscfg)

The output however, I expected it to be {"name":"Webserver", "version":"0.0", ...etc}. The output I got instead is a real weird one:

{'version': '0.0', 'database_type': 'SQLite 3', 'name': 'Webserver', 'python_version': 'Python 33', '__builtins__': {'slice': <class 'slice'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, 'ConnectionRefusedError': <class 'ConnectionRefusedError'>, 'NotADirectoryError': <class 'NotADirectoryError'>, 'iter': <built-in function iter>, 'int': <class 'int'>, 'property': <class 'property'>, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'PermissionError': <class 'PermissionError'>, 'pow': <built-in function pow>, 'list': <class 'list'>, 'ConnectionAbortedError': <class 'ConnectionAbortedError'>, 'delattr': <built-in function delattr>, 'NotImplementedError': <class 'NotImplementedError'>, 'AttributeError': <class 'AttributeError'>, 'EOFError': <class 'EOFError'>, 'setattr': <built-in function setattr>, 'super': <class 'super'>, 'globals': <built-in function globals>, 'GeneratorExit': <class 'GeneratorExit'>, 'next': <built-in function next>, 'id': <built-in function id>, 'NameError': <class 'NameError'>, 'WindowsError': <class 'OSError'>, 'chr': <built-in function chr>, 'UnicodeError': <class 'UnicodeError'>, 'ImportWarning': <class 'ImportWarning'>, 'ord': <built-in function ord>, 'BlockingIOError': <class 'BlockingIOError'>, 'TypeError': <class 'TypeError'>, 'divmod': <built-in function divmod>, 'Warning': <class 'Warning'>, 'classmethod': <class 'classmethod'>, 'OSError': <class 'OSError'>, 'getattr': <built-in function getattr>, 'copyright': Copyright (c) 2001-2013 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'str': <class 'str'>, 'sorted': <built-in function sorted>, 'AssertionError': <class 'AssertionError'>, 'SystemExit': <class 'SystemExit'>, 'FileExistsError': <class 'FileExistsError'>, 'BufferError': <class 'BufferError'>, 'tuple': <class 'tuple'>, 'RuntimeError': <class 'RuntimeError'>, 'oct': <built-in function oct>, 'len': <built-in function len>, 'EnvironmentError': <class 'OSError'>, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>, 'type': <class 'type'>, 'min': <built-in function min>, 'dir': <built-in function dir>, 'ReferenceError': <class 'ReferenceError'>, 'RuntimeWarning': <class 'RuntimeWarning'>, 'max': <built-in function max>, 'compile': <built-in function compile>, 'memoryview': <class 'memoryview'>, 'map': <class 'map'>, 'ascii': <built-in function ascii>, '__package__': None, 'reversed': <class 'reversed'>, 'IOError': <class 'OSError'>, 'any': <built-in function any>, 'bytearray': <class 'bytearray'>, 'ChildProcessError': <class 'ChildProcessError'>, 'MemoryError': <class 'MemoryError'>, 'ValueError': <class 'ValueError'>, 'FloatingPointError': <class 'FloatingPointError'>, 'bool': <class 'bool'>, 'KeyError': <class 'KeyError'>, 'quit': Use quit() or Ctrl-Z plus Return to exit, 'bin': <built-in function bin>, 'Ellipsis': Ellipsis, 'frozenset': <class 'frozenset'>, 'print': <built-in function print>, 'UserWarning': <class 'UserWarning'>, 'IndentationError': <class 'IndentationError'>, 'ConnectionResetError': <class 'ConnectionResetError'>, 'SystemError': <class 'SystemError'>, 'NotImplemented': NotImplemented, 'hasattr': <built-in function hasattr>, 'staticmethod': <class 'staticmethod'>, 'False': False, 'exec': <built-in function exec>, 'range': <class 'range'>, 'BytesWarning': <class 'BytesWarning'>, 'sum': <built-in function sum>, 'StopIteration': <class 'StopIteration'>, 'repr': <built-in function repr>, 'exit': Use exit() or Ctrl-Z plus Return to exit, 'abs': <built-in function abs>, 'SyntaxWarning': <class 'SyntaxWarning'>, 'filter': <class 'filter'>, 'format': <built-in function format>, 'locals': <built-in function locals>, 'ConnectionError': <class 'ConnectionError'>, 'ImportError': <class 'ImportError'>, 'complex': <class 'complex'>, 'SyntaxError': <class 'SyntaxError'>, '__name__': 'builtins', 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, '__debug__': True, 'None': None, 'bytes': <class 'bytes'>, 'open': <built-in function open>, 'object': <class 'object'>, 'ArithmeticError': <class 'ArithmeticError'>, 'zip': <class 'zip'>, '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", 'LookupError': <class 'LookupError'>, 'True': True, 'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information., 'TimeoutError': <class 'TimeoutError'>, 'FutureWarning': <class 'FutureWarning'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'IndexError': <class 'IndexError'>, 'isinstance': <built-in function isinstance>, 'round': <built-in function round>, 'ProcessLookupError': <class 'ProcessLookupError'>, 'set': <class 'set'>, 'TabError': <class 'TabError'>, 'InterruptedError': <class 'InterruptedError'>, 'help': Type help() for interactive help, or help(object) for help about object., 'IsADirectoryError': <class 'IsADirectoryError'>, 'FileNotFoundError': <class 'FileNotFoundError'>, 'float': <class 'float'>, 'enumerate': <class 'enumerate'>, 'OverflowError': <class 'OverflowError'>, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, 'Exception': <class 'Exception'>, 'license': Type license() to see the full license text, 'hash': <built-in function hash>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'BaseException': <class 'BaseException'>, 'dict': <class 'dict'>, 'all': <built-in function all>, 'UnicodeWarning': <class 'UnicodeWarning'>, '__build_class__': <built-in function __build_class__>, 'ResourceWarning': <class 'ResourceWarning'>, 'issubclass': <built-in function issubclass>, 'vars': <built-in function vars>, '__import__': <built-in function __import__>, 'hex': <built-in function hex>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'input': <built-in function input>, 'callable': <built-in function callable>, 'eval': <built-in function eval>, 'BrokenPipeError': <class 'BrokenPipeError'>}}

Sure, the first four entries that are printed are right, but after that there's all kind of strange entries.

EDIT: Also, if I print(settingscfg.get("version")) it will return 0.0, but the entire dictionary is still filled with crap.


Solution

  • Using exec in python is often frowned upon both because it only works right if you completely trust your input data, and because it can be slow because the interpreter can only start parsing the code once you reach that line.

    For an application like this, it would probably be better to use configparser, a standard library module made just for this sort of thing. In order to use this, you would have to change your configuration file slightly, to

    [CONFIG]
    name = "Webserver"
    version = "0.0"
    python_version = "Python 33"
    database_type = "SQLite 3"
    

    In order to load this, you would use the code

    import configparser
    config = configparser.ConfigParser()
    config.read("settings.cfg")
    settingscfg = config["CONFIG"]
    

    Then settingscfg will be the dictionary you are looking for.