For %load, strip indent from lines until finding an unindented line. https://github.com/ipython/ipython/issues/9775
(lines)
| 136 | return blocks, not_found |
| 137 | |
| 138 | def strip_initial_indent(lines): |
| 139 | """For %load, strip indent from lines until finding an unindented line. |
| 140 | |
| 141 | https://github.com/ipython/ipython/issues/9775 |
| 142 | """ |
| 143 | indent_re = re.compile(r'\s+') |
| 144 | |
| 145 | it = iter(lines) |
| 146 | first_line = next(it) |
| 147 | indent_match = indent_re.match(first_line) |
| 148 | |
| 149 | if indent_match: |
| 150 | # First line was indented |
| 151 | indent = indent_match.group() |
| 152 | yield first_line[len(indent):] |
| 153 | |
| 154 | for line in it: |
| 155 | if line.startswith(indent): |
| 156 | yield line[len(indent) :] |
| 157 | elif line in ("\n", "\r\n") or len(line) == 0: |
| 158 | yield line |
| 159 | else: |
| 160 | # Less indented than the first line - stop dedenting |
| 161 | yield line |
| 162 | break |
| 163 | else: |
| 164 | yield first_line |
| 165 | |
| 166 | # Pass the remaining lines through without dedenting |
| 167 | for line in it: |
| 168 | yield line |
| 169 | |
| 170 | |
| 171 | class InteractivelyDefined(Exception): |