From 881764dc56f85531706fd8289d0c5560d52d3a9a Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Thu, 24 Sep 2020 21:42:21 -0400 Subject: [PATCH] Return env interpolation on Python 2 to support pypy 2 --- src/dependency_injector/providers.pyx | 32 +++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/dependency_injector/providers.pyx b/src/dependency_injector/providers.pyx index ebad44a5..a799d352 100644 --- a/src/dependency_injector/providers.pyx +++ b/src/dependency_injector/providers.pyx @@ -52,17 +52,31 @@ if yaml: yaml.add_constructor('!path', yaml_env_marker_constructor) -class EnvInterpolation(iniconfigparser.BasicInterpolation): - """Interpolation which expands environment variables in values.""" +if sys.version_info[0] == 3: + class EnvInterpolation(iniconfigparser.BasicInterpolation): + """Interpolation which expands environment variables in values.""" - def before_get(self, parser, section, option, value, defaults): - value = super().before_get(parser, section, option, value, defaults) - return os.path.expandvars(value) + def before_get(self, parser, section, option, value, defaults): + value = super().before_get(parser, section, option, value, defaults) + return os.path.expandvars(value) -def _parse_ini_file(filepath): - parser = iniconfigparser.ConfigParser(interpolation=EnvInterpolation()) - parser.read(filepath) - return parser + def _parse_ini_file(filepath): + parser = iniconfigparser.ConfigParser(interpolation=EnvInterpolation()) + parser.read(filepath) + return parser +else: + import StringIO + + def _parse_ini_file(filepath): + parser = iniconfigparser.ConfigParser() + try: + with open(filepath) as config_file: + config_string = os.path.expandvars(config_file.read()) + except IOError: + return parser + else: + parser.readfp(StringIO.StringIO(config_string)) + return parser cdef class Provider: