docstr: Compile and import everything from a Cython code cell.
  
  The contents of the cell are written to a `.pyx` file in the
  directory `IPYTHONDIR/cython` using a filename with the hash of the
  code. This file is then cythonized and compiled. The resulting module
  is imported and all of its symbols are injected into the user's
  namespace. The usage is similar to that of `%%cython_pyximport` but
  you don't have to pass a module name::
  
      %%cython
      def f(x):
          return 2.0*x
  
  To compile OpenMP codes, pass the required  `--compile-args`
  and `--link-args`.  For example with gcc::
  
      %%cython --compile-args=-fopenmp --link-args=-fopenmp
      ...
  
  To enable profile guided optimisation, pass the ``--pgo`` option.
  Note that the cell itself needs to take care of establishing a suitable
  profile when executed. This can be done by implementing the functions to
  optimise, and then calling them directly in the same cell on some realistic
  training data like this::
  
      %%cython --pgo
      def critical_function(data):
          for item in data:
              ...
  
      # execute function several times to build profile
      from somewhere import some_typical_data
      for _ in range(100):
          critical_function(some_typical_data)
  
  In Python 3.5 and later, you can distinguish between the profile and
  non-profile runs as follows::
  
      if "_pgo_" in __name__:
          ...  # execute critical code here
docstr_tok: ["Compile","and","import","everything","from","a","Cython","code","cell",".","The","contents","of","the","cell","are","written","to","a","`",".pyx","`","file","in","the","directory","`","IPYTHONDIR\/cython","`","using","a","filename","with","the","hash","of","the","code",".","This","file","is","then","cythonized","and","compiled",".","The","resulting","module","is","imported","and","all","of","its","symbols","are","injected","into","the","user's","namespace",".","The","usage","is","similar","to","that","of","`","%","%","cython_pyximport","`","but","you","do","n't","have","to","pass","a","module","name",":",":","%","%","cython","def","f","(","x",")",":","return","2.0","*","x","To","compile","OpenMP","codes",",","pass","the","required","`","--","compile-args","`","and","`","--","link-args","`",".","For","example","with","gcc",":",":","%","%","cython","--","compile-args=-fopenmp","--","link-args=-fopenmp","...","To","enable","profile","guided","optimisation",",","pass","the","``","--","pgo","``","option",".","Note","that","the","cell","itself","needs","to","take","care","of","establishing","a","suitable","profile","when","executed",".","This","can","be","done","by","implementing","the","functions","to","optimise",",","and","then","calling","them","directly","in","the","same","cell","on","some","realistic","training","data","like","this",":",":","%","%","cython","--","pgo","def","critical_function","(","data",")",":","for","item","in","data",":","...","#","execute","function","several","times","to","build","profile","from","somewhere","import","some_typical_data","for","_","in","range","(","100",")",":","critical_function","(","some_typical_data",")","In","Python","3.5","and","later",",","you","can","distinguish","between","the","profile","and","non-profile","runs","as","follows",":",":","if","``","_pgo_","''","in","__name__",":","...","#","execute","critical","code","here"]
code: def cython(self, line, cell):
      """Compile and import everything from a Cython code cell.
  
      The contents of the cell are written to a `.pyx` file in the
      directory `IPYTHONDIR/cython` using a filename with the hash of the
      code. This file is then cythonized and compiled. The resulting module
      is imported and all of its symbols are injected into the user's
      namespace. The usage is similar to that of `%%cython_pyximport` but
      you don't have to pass a module name::
  
          %%cython
          def f(x):
              return 2.0*x
  
      To compile OpenMP codes, pass the required  `--compile-args`
      and `--link-args`.  For example with gcc::
  
          %%cython --compile-args=-fopenmp --link-args=-fopenmp
          ...
  
      To enable profile guided optimisation, pass the ``--pgo`` option.
      Note that the cell itself needs to take care of establishing a suitable
      profile when executed. This can be done by implementing the functions to
      optimise, and then calling them directly in the same cell on some realistic
      training data like this::
  
          %%cython --pgo
          def critical_function(data):
              for item in data:
                  ...
  
          # execute function several times to build profile
          from somewhere import some_typical_data
          for _ in range(100):
              critical_function(some_typical_data)
  
      In Python 3.5 and later, you can distinguish between the profile and
      non-profile runs as follows::
  
          if "_pgo_" in __name__:
              ...  # execute critical code here
      """
      args = magic_arguments.parse_argstring(self.cython, line)
      code = cell if cell.endswith("\n") else cell + "\n"
      lib_dir = os.path.join(get_ipython_cache_dir(), "cython")
      key = (
          code,
          line,
          sys.version_info,
          sys.executable,
          cython_version,
      )
  
      if not os.path.exists(lib_dir):
          os.makedirs(lib_dir)
  
      if args.pgo:
          key += ("pgo",)
      if args.force:
          # Force a new module name by adding the current time to the
          # key which is hashed to determine the module name.
          key += (time.time(),)
  
      if args.name:
          module_name = str(args.name)  # no-op in Py3
      else:
          module_name = (
              "_cython_magic_"
              + hashlib.md5(str(key).encode("utf-8")).hexdigest()
          )
      html_file = os.path.join(lib_dir, module_name + ".html")
      module_path = os.path.join(lib_dir, module_name + self.so_ext)
  
      have_module = os.path.isfile(module_path)
      need_cythonize = args.pgo or not have_module
  
      if args.annotate:
          if not os.path.isfile(html_file):
              need_cythonize = True
  
      extension = None
      if need_cythonize:
          extensions = self._cythonize(
              module_name, code, lib_dir, args, quiet=args.quiet
          )
          if extensions is None:
              # Compilation failed and printed error message
              return None
          assert len(extensions) == 1
          extension = extensions[0]
          self._code_cache[key] = module_name
  
          if args.pgo:
              self._profile_pgo_wrapper(extension, lib_dir)
  
      try:
          self._build_extension(
              extension,
              lib_dir,
              pgo_step_name="use" if args.pgo else None,
              quiet=args.quiet,
          )
      except distutils.errors.CompileError:
          # Build failed and printed error message
          return None
  
      module = load_dynamic(module_name, module_path)
      self._import_all(module)
  
      if args.annotate:
          try:
              with io.open(html_file, encoding="utf-8") as f:
                  annotated_html = f.read()
          except IOError as e:
              # File could not be opened. Most likely the user has a version
              # of Cython before 0.15.1 (when `cythonize` learned the
              # `force` keyword argument) and has already compiled this
              # exact source without annotation.
              print(
                  "Cython completed successfully but the annotated "
                  "source could not be read.",
                  file=sys.stderr,
              )
              print(e, file=sys.stderr)
          else:
              return display.HTML(
                  self.clean_annotated_html(annotated_html)
              )
  
imports: from __future__ import absolute_import, print_function
  import io
  import os
  import re
  import sys
  import time
  import copy
  import distutils.log
  import textwrap
  from distutils.core import Distribution, Extension
  from distutils.command.build_ext import build_ext
  from IPython.core import display
  from IPython.core import magic_arguments
  from IPython.core.magic import Magics, magics_class, cell_magic
  from IPython.utils.text import dedent
  from ..Shadow import __version__
  from ..Compiler.Errors import CompileError
  from .Inline import cython_inline, load_dynamic
  from .Dependencies import cythonize
label_desc: Use image node_id 5 for calling the CythonMagics obj's underlying member method code with example usage: obj.cython(line, cell) and returns: None, None, display