code: def test_pyparser():
      """Test case: general parsing."""
  
      def _check_blocks(actual, expected):
          assert actual, "No parse results"
          for i in range(len(actual)):
              assert i < len(expected), "Unexpected block %d:\n%r" % (
                  i,
                  actual[i],
              )
              valid = False
              if isinstance(expected[i], type):
                  if isinstance(actual[i], expected[i]):
                      valid = True
              elif isinstance(expected[i], tuple):
                  if (
                      isinstance(actual[i], expected[i][0])
                      and actual[i].type == expected[i][1]
                  ):
                      valid = True
              if not valid:
                  assert False, "Invalid block: expected %r, got %r" % (
                      expected[i],
                      actual[i],
                  )
  
      def check_code(code, blocks=None, filename=None):
          code = textwrap.dedent(code)
          if not code.endswith("\n"):
              code += "\n"
          if filename:
              print("Testing file %s..." % filename, end=" ")
          else:
              check_code.index = getattr(check_code, "index", 0) + 1
              print(
                  "Testing code fragment %d..." % check_code.index,
                  end=" ",
              )
          preparsed = None
          parsed = None
          unparsed = None
          try:
              preparsed = pyparser.parse_text(code)
              parsed = preparsed.parse(2)
              try:
                  unparsed = parsed.unparse()
              except ValueError as e:
                  for i, tok in enumerate(parsed.tokens):
                      print("%3d %r" % (i, tok))
                  raise AssertionError("Cannot unparse code: %s" % e)
              assert_same_code(code, unparsed)
              if blocks:
                  _check_blocks(parsed.parsed, blocks)
              print("ok")
          except AssertionError as e:
              print()
              print("Error: " + str(e))
              print("Original code fragment:\n" + code)
              if unparsed:
                  print("Unparsed code:\n" + unparsed)
              if parsed:
                  print(parsed)
                  for i, tok in enumerate(parsed.tokens):
                      print("%3d %r" % (i, tok))
              raise
          except Exception as e:
              print()
              print("Error: " + str(e))
              if preparsed:
                  print("Preparsed tokens:")
                  for i, tok in enumerate(preparsed.tokens):
                      print("%4d %r" % (i, tok))
              else:
                  print("Initial parsing has failed...")
              raise
  
      check_code(
          """
          # -*- encoding: utf-8 -*-
          # copyright: 2016 h2o.ai
          \"\"\"
          A code example.
  
          It's not supposed to be functional, or even functionable.
          \"\"\"
          # Standard library imports
          import sys
          import time
          import this
  
          import h2o
          from h2o import H2OFrame, init
          from . import *
  
  
  
          # Do some initalization for legacy python versions
          handler = lambda: None  # noop
                                  # (will redefine later)
  
          ################################################################################
  
          # comment 1
          class Foo(object):
              #------ Public -------------------------------------------------------------
              def bar(self):
                  pass
  
          # def foo():
          #     print(1)
          #
          #     print(2)
  
          # comment 2
          @decorated(
              1, 2, (3))
          @dddd
          def bar():
              # be
              # happy
              print("bar!")
          # bye""",
          [
              Ws,
              Comment,
              Docstring,
              Ws,
              Import_stdlib,
              Ws,
              Import_1stpty,
              Ws,
              Expression,
              Ws,
              Expression,
              Ws,
              Comment_banner,
              Ws,
              Class,
              Ws,
              Comment_code,
              Ws,
              Function,
              Comment,
              Ws,
          ],
      )
  
      for directory in [".", "../../h2o-py", "../../py"]:
          absdir = os.path.abspath(directory)
          for dir_name, subdirs, files in os.walk(absdir):
              for f in files:
                  if f.endswith(".py"):
                      filename = os.path.join(dir_name, f)
                      with open(
                          filename, "rt", encoding="utf-8"
                      ) as fff:
                          check_code(fff.read(), filename=filename)
  
file_path: h2o-3/h2o-bindings/bin/pyunit_parser_test.py
imports: import os
  import re
  import textwrap
  import tokenize
  import pyparser
label_desc: Use image node_id 4 for calling a global function with example usage: test_pyparser() without return types