def _expression_to_evaluate(expression): keepends = True lines = expression.splitlines(keepends) # find first non-empty line chars_to_strip = 0 for line in lines: if line.strip(): # i.e.: check first non-empty line for c in iter_chars(line): if c.isspace(): chars_to_strip += 1 else: break break if chars_to_strip: # I.e.: check that the chars we'll remove are really only whitespaces. proceed = True new_lines = [] for line in lines: if not proceed: break for c in iter_chars(line[:chars_to_strip]): if not c.isspace(): proceed = False break new_lines.append(line[chars_to_strip:]) if proceed: if isinstance(expression, bytes): expression = b''.join(new_lines) else: expression = u''.join(new_lines) return expression
def _expression_to_evaluate(expression): keepends = True lines = expression.splitlines(keepends) # find first non-empty line chars_to_strip = 0 for line in lines: if line.strip(): # i.e.: check first non-empty line for c in iter_chars(line): if c.isspace(): chars_to_strip += 1 else: break break if chars_to_strip: # I.e.: check that the chars we'll remove are really only whitespaces. proceed = True new_lines = [] for line in lines: if not proceed: break for c in iter_chars(line[:chars_to_strip]): if not c.isspace(): proceed = False break new_lines.append(line[chars_to_strip:]) if proceed: if isinstance(expression, bytes): expression = b''.join(new_lines) else: expression = u''.join(new_lines) if IS_PY2 and isinstance(expression, unicode): # In Python 2 we need to compile with bytes and not unicode (otherwise it'd use # the default encoding which could be ascii). # See https://github.com/microsoft/ptvsd/issues/1864 and https://bugs.python.org/issue18870 # for why we're using the utf-8 bom. # i.e.: ... if an utf-8 bom is present, it is considered utf-8 in eval/exec. expression = codecs.BOM_UTF8 + expression.encode('utf-8') return expression