Make f-strings backward compatible for Python < 3.12

This commit is contained in:
2026-03-12 22:10:51 +01:00
parent b00e5d9829
commit 264f8a0083
3 changed files with 14 additions and 10 deletions
+3
View File
@@ -84,5 +84,8 @@ For the full documentation, see the Python docstrings.
## Changelog ## Changelog
### Version 0.1.1
- backward compatibility for Python < 3.12: in Python < 3.12, f-string expression parts could not contain backslashes. The code contained an f-string with backslahes in the expression part. As an alternative to the f-string, normal strings are concatenated now.
### Version 0.1.0 ### Version 0.1.0
- Changed the name from `config-parser` to `jcloud-config-parser` - Changed the name from `config-parser` to `jcloud-config-parser`
+1 -1
View File
@@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "jcloud-config-parser" name = "jcloud-config-parser"
version = "0.1.0" version = "0.1.1"
description = "A configuration file parser." description = "A configuration file parser."
license = "Apache-2.0" license = "Apache-2.0"
+10 -9
View File
@@ -328,7 +328,7 @@ class JSONObject(JSONType):
current_key = '' current_key = ''
current_value = None current_value = None
else: else:
raise JSONObjectSyntaxError(f'expected \':\'') raise JSONObjectSyntaxError('expected \':\'')
if current_key: if current_key:
if current_value is not None: if current_value is not None:
@@ -336,7 +336,7 @@ class JSONObject(JSONType):
current_key = '' current_key = ''
current_value = None current_value = None
else: else:
raise JSONObjectSyntaxError(f'expected value') raise JSONObjectSyntaxError('expected value')
json_object = {} json_object = {}
for k, v in _object.items(): for k, v in _object.items():
@@ -471,27 +471,27 @@ def parse_type(value: str) -> typing.Type[JSONType]:
if current_value is None: if current_value is None:
current_value = '' current_value = ''
else: else:
raise JSONObjectSyntaxError(f'expected \',\'') raise JSONObjectSyntaxError('expected \',\'')
if not string and c == ',' and not arrays and not objects: if not string and c == ',' and not arrays and not objects:
if current_value is not None: if current_value is not None:
if parse_type(current_key) != JSONString: if parse_type(current_key) != JSONString:
raise JSONObjectSyntaxError(f'keys have to be strings') raise JSONObjectSyntaxError('keys have to be strings')
parse_type(current_value[:-1]) parse_type(current_value[:-1])
current_key = '' current_key = ''
current_value = None current_value = None
else: else:
raise JSONObjectSyntaxError(f'expected \':\'') raise JSONObjectSyntaxError('expected \':\'')
if current_key: if current_key:
if current_value is not None: if current_value is not None:
if parse_type(current_key) != JSONString: if parse_type(current_key) != JSONString:
raise JSONObjectSyntaxError(f'keys have to be strings') raise JSONObjectSyntaxError('keys have to be strings')
parse_type(current_value) parse_type(current_value)
current_key = '' current_key = ''
current_value = None current_value = None
else: else:
raise JSONObjectSyntaxError(f'expected value') raise JSONObjectSyntaxError('expected value')
if string: if string:
raise JSONStringSyntaxError('unterminated string literal') raise JSONStringSyntaxError('unterminated string literal')
@@ -568,11 +568,12 @@ def parse_type(value: str) -> typing.Type[JSONType]:
if value[0] != '"': if value[0] != '"':
raise JSONStringSyntaxError(f'expected \'"\' as first char, got \'{value[0]}\'') raise JSONStringSyntaxError(f'expected \'"\' as first char, got \'{value[0]}\'')
if '\n' in value: if '\n' in value:
raise JSONStringSyntaxError(f'line feeds are not allowed inside of strings. Use escape sequences.') raise JSONStringSyntaxError('line feeds are not allowed inside of strings. Use escape sequences.')
if value.count('"') < 2: if value.count('"') < 2:
raise JSONStringSyntaxError('unterminated string literal') raise JSONStringSyntaxError('unterminated string literal')
if value.count('"') - value.count('\\"') > 2 or value[-1] != '"': if value.count('"') - value.count('\\"') > 2 or value[-1] != '"':
raise JSONStringSyntaxError(f'unexpected token: \'{"\"".join(value.split("\"")[2:])}\'') # backward compatibility (in Python < 3.12, f-string expression parts could not contain backslashes)
raise JSONStringSyntaxError('unexpected token: \'' + '\"'.join(value.split('\"')[2:]) + '\'')
parse_escape_sequences(value) parse_escape_sequences(value)
return JSONString return JSONString