Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
src/sage/misc/replace_dot_all.py: Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Jan 29, 2023
1 parent fe617e9 commit 1c19d43
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/sage/misc/replace_dot_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ def find_replacements(location, package_regex=None, verbose=False):
EXAMPLES::
sage: from sage.misc.replace_dot_all import *
sage: location = sage.env.SAGE_SRC + '/sage/structure/element.pyx'
sage: find_replacements(location, verbose=True)
sage: location = os.path.join(sage.env.SAGE_SRC, 'sage/plot/arc.py')
sage: find_replacements(location, package_regex='sage[.]plot[.]all', verbose=True)
[[..., ..., 'from sage.plot.graphics import Graphics']]
"""
if package_regex is None:
package_regex = default_package_regex
Expand Down Expand Up @@ -259,7 +260,7 @@ def find_replacements(location, package_regex=None, verbose=False):
return replacements


def process_line(location, line, replacements, import_index, verbose=False):
def process_line(location, line, replacements, row_index, verbose=False):
r"""
Modify a single source code ``line`` according to the given ``replacements``.
Expand All @@ -268,7 +269,7 @@ def process_line(location, line, replacements, import_index, verbose=False):
- ``location`` -- a file path; only used for logging
- ``line`` -- a source code line
- ``replacements`` -- the array output from :func:`find_replacements`
- ``import_index`` -- the column number where ``import`` appears
- ``row_index`` -- the line number where ``import`` appears
- ``verbose`` -- if True, issue print statements when interesting examples are found
OUTPUT:
Expand All @@ -283,22 +284,27 @@ def process_line(location, line, replacements, import_index, verbose=False):
Replacing the first line which needs a replacement in the file with filepath ``src/sage/structure/element.pyx``::
sage: from sage.misc.replace_dot_all import *
sage: location = os.path.join(sage.env.SAGE_SRC, 'sage/structure/element.pyx')
sage: replacements = find_replacements(location, verbose=True)
sage: location = os.path.join(sage.env.SAGE_SRC, 'sage/plot/arc.py')
sage: replacements = find_replacements(location, package_regex='sage[.]plot[.]all', verbose=True); replacements
[[471, 24, 'from sage.plot.graphics import Graphics']]
sage: with open(location, "r") as file:
....: lines = file.readlines()
sage: row_index, import_index, *_ = replacements[0]
sage: line = lines[row_index]; line
sage: new_line, replacements = process_line(location, line, replacements, import_index)
sage: new_line
sage: row_index, col_number, *_ = replacements[0]
sage: line = lines[row_index]
sage: print(line.rstrip())
from sage.plot.all import Graphics
sage: new_line, replacements = process_line(location, line, replacements, row_index)
sage: print(new_line)
from sage.plot.graphics import Graphics
sage: replacements
[]
"""
line = line.rstrip() # stripping line break
new_line = ''
global log_messages, interesting_examples
if len(replacements) == 0:
return line, replacements
if import_index == replacements[0][0]: # if line marked as containing .all
if row_index == replacements[0][0]: # if line marked as containing .all
replacement = replacements.pop(0)
leading_space = 0
while line and line[leading_space] == ' ' and leading_space < len(line)-1:
Expand Down

0 comments on commit 1c19d43

Please sign in to comment.