@@ -64,7 +64,7 @@ def __init__(
6464 """
6565 self .autosave = autosave
6666 self ._path = Path (path )
67- self ._lines = self .path . read_text (). splitlines ( )
67+ self ._lines = self ._read_lines ( self . _path )
6868 self ._parser = SpecParser (
6969 Path (sourcedir or self .path .parent ), macros , force_parse
7070 )
@@ -101,6 +101,10 @@ def __exit__(
101101 ) -> None :
102102 self .save ()
103103
104+ @staticmethod
105+ def _read_lines (path : Path ) -> List [str ]:
106+ return path .read_text (encoding = "utf8" , errors = "surrogateescape" ).splitlines ()
107+
104108 @property
105109 def path (self ) -> Path :
106110 """Path to the spec file."""
@@ -154,28 +158,33 @@ def rpm_spec(self) -> rpm.spec:
154158
155159 def reload (self ) -> None :
156160 """Reload the spec file content."""
157- self ._lines = self .path . read_text (). splitlines ( )
161+ self ._lines = self ._read_lines ( self . path )
158162
159163 def save (self ) -> None :
160164 """Save the spec file content."""
161- self .path .write_text (str (self ))
165+ self .path .write_text (str (self ), encoding = "utf8" , errors = "surrogateescape" )
162166
163167 def expand (
164168 self ,
165169 expression : str ,
166170 extra_macros : Optional [List [Tuple [str , str ]]] = None ,
171+ skip_parsing : bool = False ,
167172 ) -> str :
168173 """
169174 Expands an expression in the context of the spec file.
170175
171176 Args:
172177 expression: Expression to expand.
173178 extra_macros: Extra macros to be defined before expansion is performed.
179+ skip_parsing: Do not parse the spec file before expansion is performed.
180+ Defaults to False. Mutually exclusive with extra_macros. Set this to True
181+ only if you are certain that the global macro context is up-to-date.
174182
175183 Returns:
176184 Expanded expression.
177185 """
178- self ._parser .parse (str (self ), extra_macros )
186+ if not skip_parsing or extra_macros is not None :
187+ self ._parser .parse (str (self ), extra_macros )
179188 return Macros .expand (expression )
180189
181190 def get_active_macros (self ) -> List [Macro ]:
0 commit comments