r"""An object that can accumulate lines of Python source before execution. This object is designed to be fed python source line-by-line, using :meth:`push`. It will return on each push whether the currently pushed code could be executed already. In addition, it provides a method called
| 277 | #----------------------------------------------------------------------------- |
| 278 | |
| 279 | class InputSplitter(object): |
| 280 | r"""An object that can accumulate lines of Python source before execution. |
| 281 | |
| 282 | This object is designed to be fed python source line-by-line, using |
| 283 | :meth:`push`. It will return on each push whether the currently pushed |
| 284 | code could be executed already. In addition, it provides a method called |
| 285 | :meth:`push_accepts_more` that can be used to query whether more input |
| 286 | can be pushed into a single interactive block. |
| 287 | |
| 288 | This is a simple example of how an interactive terminal-based client can use |
| 289 | this tool:: |
| 290 | |
| 291 | isp = InputSplitter() |
| 292 | while isp.push_accepts_more(): |
| 293 | indent = ' '*isp.indent_spaces |
| 294 | prompt = '>>> ' + indent |
| 295 | line = indent + raw_input(prompt) |
| 296 | isp.push(line) |
| 297 | print 'Input source was:\n', isp.source_reset(), |
| 298 | """ |
| 299 | # A cache for storing the current indentation |
| 300 | # The first value stores the most recently processed source input |
| 301 | # The second value is the number of spaces for the current indentation |
| 302 | # If self.source matches the first value, the second value is a valid |
| 303 | # current indentation. Otherwise, the cache is invalid and the indentation |
| 304 | # must be recalculated. |
| 305 | _indent_spaces_cache = None, None |
| 306 | # String, indicating the default input encoding. It is computed by default |
| 307 | # at initialization time via get_input_encoding(), but it can be reset by a |
| 308 | # client with specific knowledge of the encoding. |
| 309 | encoding = '' |
| 310 | # String where the current full source input is stored, properly encoded. |
| 311 | # Reading this attribute is the normal way of querying the currently pushed |
| 312 | # source code, that has been properly encoded. |
| 313 | source = '' |
| 314 | # Code object corresponding to the current source. It is automatically |
| 315 | # synced to the source, so it can be queried at any time to obtain the code |
| 316 | # object; it will be None if the source doesn't compile to valid Python. |
| 317 | code = None |
| 318 | |
| 319 | # Private attributes |
| 320 | |
| 321 | # List with lines of input accumulated so far |
| 322 | _buffer = None |
| 323 | # Command compiler |
| 324 | _compile = None |
| 325 | # Boolean indicating whether the current block is complete |
| 326 | _is_complete = None |
| 327 | # Boolean indicating whether the current block has an unrecoverable syntax error |
| 328 | _is_invalid = False |
| 329 | |
| 330 | def __init__(self): |
| 331 | """Create a new InputSplitter instance. |
| 332 | """ |
| 333 | self._buffer = [] |
| 334 | self._compile = codeop.CommandCompiler() |
| 335 | self.encoding = get_input_encoding() |
| 336 |
no outgoing calls