From 60055fce7d19a517f1f132c648ac461724e3bef6 Mon Sep 17 00:00:00 2001 From: George Peng Date: Fri, 26 Apr 2024 16:45:05 +0800 Subject: [PATCH] Updates name template for compatibility with Cython --- walrus.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/walrus.py b/walrus.py index 346e10a..4ad4dbe 100644 --- a/walrus.py +++ b/walrus.py @@ -276,9 +276,15 @@ def predicate(filename: str) -> bool: # Main Conversion Implementation # walrus wrapper template + +# NAME_TEMPLATE = '''\ +# if False: +# %(indentation)s%(name_list)s = NotImplemented +# '''.splitlines() # `str.splitlines` will remove trailing newline +# make it Cython compilable NAME_TEMPLATE = '''\ -if False: -%(indentation)s%(name_list)s = NotImplemented +if '%(name)s' not in dir(): +%(indentation)s%(name)s = NotImplemented '''.splitlines() # `str.splitlines` will remove trailing newline CALL_TEMPLATE = '_walrus_wrapper_%(name)s_%(uuid)s(%(expr)s)' FUNC_TEMPLATE = '''\ @@ -1017,10 +1023,10 @@ def _concat(self) -> None: else: linesep = '' if self._vars: - name_list = ' = '.join(sorted(set(self._vars))) - self._buffer += indent + ( - '%s%s' % (self._linesep, indent) - ).join(NAME_TEMPLATE) % dict(indentation=self._indentation, name_list=name_list) + self._linesep + for name in sorted(set(self._vars)): + self._buffer += indent + ( + '%s%s' % (self._linesep, indent) + ).join(NAME_TEMPLATE) % dict(indentation=self._indentation, name=name) + self._linesep for func in sorted(self._func, key=lambda func: func['name']): if self._buffer: self._buffer += linesep @@ -1205,10 +1211,10 @@ def _concat(self) -> None: else: linesep = '' if self._vars: - name_list = ' = '.join(sorted(set(self._vars))) - self._buffer += indent + ( - '%s%s' % (self._linesep, indent) - ).join(NAME_TEMPLATE) % dict(indentation=self._indentation, name_list=name_list) + self._linesep + for name in sorted(set(self._vars)): + self._buffer += indent + ( + '%s%s' % (self._linesep, indent) + ).join(NAME_TEMPLATE) % dict(indentation=self._indentation, name=name) + self._linesep for func in sorted(self._func, key=lambda func: func['name']): if self._buffer: self._buffer += linesep