Skip to content

Commit

Permalink
fixed StopIteration errors from syncing code with source line numbers…
Browse files Browse the repository at this point in the history
… by switching to new algorithm (#36, #38, #44)
  • Loading branch information
gpoore committed Apr 28, 2021
1 parent 1d2f4eb commit f2210e2
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 260 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

## v0.6.0 (dev)

*
* Synchronization of code with source line numbers uses a new algorithm that
eliminates sync failure in the form of `StopIteration` errors (#36, #38,
#44). Synchronization will now be faster and will not raise errors, but
may also be less accurate occasionally.



Expand Down
20 changes: 11 additions & 9 deletions codebraid/converters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,8 @@ def __init__(self,
command: str,
code: Union[str, List[str]],
custom_options: dict,
source_name: str, *,
*,
source_name: Optional[str]=None,
source_start_line_number: Optional[int]=None,
inline: Optional[bool]=None):
self.__pre_init__()
Expand Down Expand Up @@ -1082,14 +1083,14 @@ def __init__(self, *,
raise TypeError
self.raw_source_paths = paths
# Names are based on paths BEFORE any expansion
self.source_names = [p.as_posix() for p in paths]
source_names = [p.as_posix() for p in paths]
if not all(isinstance(x, bool) for x in (expanduser, expandvars)):
raise TypeError
if expandvars:
paths = [pathlib.Path(os.path.expandvars(str(p))) for p in paths]
if expanduser:
paths = [p.expanduser() for p in paths]
self.expanded_source_paths = paths
self.expanded_source_paths = collections.OrderedDict(zip(source_names, paths))
source_strings = []
for p in paths:
try:
Expand All @@ -1101,7 +1102,7 @@ def __init__(self, *,
if not source_string:
source_string = '\n'
source_strings.append(source_string)
self.source_strings = source_strings
self.sources = collections.OrderedDict(zip(source_names, source_strings))
if self.from_formats is not None:
if from_format is None:
try:
Expand All @@ -1125,11 +1126,12 @@ def __init__(self, *,
strings and all(isinstance(x, str) for x in strings)):
raise TypeError
# Normalize newlines, as if read from file with universal newlines
self.source_strings = [io.StringIO(s, newline=None).read() or '\n' for s in strings]
source_strings = [io.StringIO(s, newline=None).read() or '\n' for s in strings]
if len(strings) == 1:
self.source_names = ['<string>']
source_names = ['<string>']
else:
self.source_names = ['<string({0})>'.format(n+1) for n in range(len(strings))]
source_names = ['<string({0})>'.format(n+1) for n in range(len(strings))]
self.sources = collections.OrderedDict(zip(source_names, source_strings))
self.raw_source_paths = None
self.expanded_source_paths = None
if from_format is None:
Expand All @@ -1139,7 +1141,7 @@ def __init__(self, *,
self.from_format = from_format
else:
raise TypeError
if len(self.source_strings) > 1 and from_format not in self.multi_source_formats:
if len(self.sources) > 1 and from_format not in self.multi_source_formats:
raise TypeError('Multiple sources are not supported for format {0}'.format(from_format))

if not isinstance(no_cache, bool):
Expand All @@ -1165,7 +1167,7 @@ def __init__(self, *,
cache_key_hasher.update(b'<string>')
else:
cache_source_paths = []
for p in self.expanded_source_paths:
for p in self.expanded_source_paths.values():
try:
p_final = pathlib.Path('~') / p.absolute().relative_to(pathlib.Path.home())
except ValueError:
Expand Down
Loading

0 comments on commit f2210e2

Please sign in to comment.