<commit_before># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Live value resolution.

Live values are extracted from the known execution context.

Requires activity and reaching definitions analyses.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gast

from tensorflow.python.autograph.pyct import anno
from tensorflow.python.autograph.pyct import transformer
from tensorflow.python.autograph.pyct.static_analysis.annos import NodeAnno

# TODO(aqj): Do we need this? Do other builtins fail in similar ways
# See b/114389775 for a related bug in pyct
# These symbols are legal in Python, but don't appear in the namespace.
_special_symbols = {'range': range}


class LiveValueResolver(transformer.Base):
  """Annotates nodes with live values."""

  def __init__(self, context, literals):
    super(LiveValueResolver, self).__init__(context)
    self.literals = literals

  def visit_ClassDef(self, node):
    self.generic_visit(node)
    anno.setanno(node, 'live_val', self.entity_info.namespace[node.name])
    return node

  def visit_Name(self, node):
    self.generic_visit(node)
    if isinstance(node.ctx, gast.Load):
      defs = anno.getanno(node, anno.Static.DEFINITIONS, ())

      is_defined = bool(defs)
      has_single_def = len(defs) == 1

      if not is_defined:
        if node.id in self.literals:
          anno.setanno(node, 'live_val', self.literals[node.id])
        elif node.id in self.entity_info.namespace:
          obj = self.entity_info.namespace[node.id]
          anno.setanno(node, 'live_val', obj)
          if hasattr(obj, '__name__'):
            anno.setanno(node, 'fqn', (obj.__name__,))
          elif hasattr(obj, '__class__'):
            obj_class = obj.__class__
            anno.setanno(node, 'fqn',
                         (obj_class.__module__, obj_class.__name__))
          else:
            # If the symbol value is for example a primitive, then it will not
            # have a name.
            pass
        elif node.id in _special_symbols:
          anno.setanno(node, 'live_val', _special_symbols[node.id])
        else:
          pass
          # TODO(mdan): Should we raise an error here?
          # Can encounter this when:
          #  * a symbol truly lacks reference
          #  * a symbol is new, like the new name of a function we just renamed.
      else:<commit_msg>Include the print function in the list of special functions - its name is not found in the namespace in Python 3.<commit_after># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Live value resolution.

Live values are extracted from the known execution context.

Requires activity and reaching definitions analyses.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gast

from tensorflow.python.autograph.pyct import anno
from tensorflow.python.autograph.pyct import transformer
from tensorflow.python.autograph.pyct.static_analysis.annos import NodeAnno


# TODO(aqj): Do we need this? Do other builtins fail in similar ways
# See b/114389775 for a related bug in pyct
# These symbols are legal in Python, but don't appear in the namespace.
_SPECIAL_SYMBOLS = {'range': range, 'print': print}


class LiveValueResolver(transformer.Base):
  """Annotates nodes with live values."""

  def __init__(self, context, literals):
    super(LiveValueResolver, self).__init__(context)
    self.literals = literals

  def visit_ClassDef(self, node):
    self.generic_visit(node)
    anno.setanno(node, 'live_val', self.entity_info.namespace[node.name])
    return node

  def visit_Name(self, node):
    self.generic_visit(node)
    if isinstance(node.ctx, gast.Load):
      defs = anno.getanno(node, anno.Static.DEFINITIONS, ())

      is_defined = bool(defs)
      has_single_def = len(defs) == 1

      if not is_defined:
        if node.id in self.literals:
          anno.setanno(node, 'live_val', self.literals[node.id])
        elif node.id in self.entity_info.namespace:
          obj = self.entity_info.namespace[node.id]
          anno.setanno(node, 'live_val', obj)
          if hasattr(obj, '__name__'):
            anno.setanno(node, 'fqn', (obj.__name__,))
          elif hasattr(obj, '__class__'):
            obj_class = obj.__class__
            anno.setanno(node, 'fqn',
                         (obj_class.__module__, obj_class.__name__))
          else:
            # If the symbol value is for example a primitive, then it will not
            # have a name.
            pass
        elif node.id in _SPECIAL_SYMBOLS:
          # Note: if the user redefined any of these symbols, then they would
          # be visible in the namespace and we would never reach this branch.
          anno.setanno(node, 'live_val', _SPECIAL_SYMBOLS[node.id])
        else:
          pass
          # TODO(mdan): Should we raise an error here?
          # Can encounter this when:
          #  * a symbol truly lacks reference
          #  * a symbol is new, like the new name of a function we just renamed.
      else:
{'fraction_non_alphanumeric': 0.10616277246354085, 'fraction_numerical': 0.0058021013015524545, 'mean_word_length': 3.1415584415584417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3521961', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1737, 'n_words': 639}