Browse Source

Intermediate changes

robot-piglet 8 months ago
parent
commit
e2cd55ed29

+ 5 - 11
contrib/python/beniget/.dist-info/METADATA

@@ -1,30 +1,24 @@
 Metadata-Version: 2.1
 Name: beniget
-Version: 0.4.1
+Version: 0.4.2.post1
 Summary: Extract semantic information about static Python code
 Home-page: https://github.com/serge-sans-paille/beniget/
 Author: serge-sans-paille
 Author-email: serge.guelton@telecom-bretagne.eu
 License: BSD 3-Clause
-Platform: UNKNOWN
 Classifier: Development Status :: 4 - Beta
 Classifier: Environment :: Console
 Classifier: Intended Audience :: Developers
 Classifier: License :: OSI Approved :: BSD License
 Classifier: Natural Language :: English
-Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 2.7
 Classifier: Programming Language :: Python :: 3
-Classifier: Programming Language :: Python :: 3.4
-Classifier: Programming Language :: Python :: 3.5
-Classifier: Programming Language :: Python :: 3.6
-Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
-Requires-Dist: gast (~=0.5.0)
+Requires-Python: >=3.6
+License-File: LICENSE
+Requires-Dist: gast >=0.5.0
 
 
-A static analyzer for Python2 and Python3 code.
+A static analyzer for Python code.
 
 Beniget provides a static over-approximation of the global and
 local definitions inside Python Module/Class/Function.
 It can also compute def-use chains from each definition.
-

+ 1 - 2
contrib/python/beniget/README.rst

@@ -5,8 +5,7 @@ Beniget is a collection of Compile-time analyse on Python Abstract Syntax Tree(A
 It's a building block to write static analyzer or compiler for Python.
 
 Beniget relies on `gast <https://pypi.org/project/gast/>`_ to provide a cross
-version abstraction of the AST, effectively working on both Python2 and
-Python3.
+version abstraction of the AST, effectively working across all Python 3 versions greater than 3.6.
 
 API
 ---

+ 1 - 0
contrib/python/beniget/beniget/__init__.py

@@ -1,2 +1,3 @@
 from __future__ import absolute_import
+from beniget.version import __version__
 from beniget.beniget import Ancestors, DefUseChains, UseDefChains

+ 64 - 0
contrib/python/beniget/beniget/__main__.py

@@ -0,0 +1,64 @@
+import sys
+
+import gast as ast
+from beniget import Ancestors, DefUseChains
+
+class Beniget(ast.NodeVisitor):
+    def __init__(self, filename, module):
+        super(Beniget, self).__init__()
+
+        self.filename = filename or "<stdin>"
+
+        self.ancestors = Ancestors()
+        self.ancestors.visit(module)
+
+        self.defuses = DefUseChains(self.filename)
+        self.defuses.visit(module)
+
+        self.visit(module)
+
+    def check_unused(self, node, skipped_types=()):
+        for local_def in self.defuses.locals[node]:
+            if not local_def.users():
+                if local_def.name() == "_":
+                    continue  # typical naming by-pass
+                if isinstance(local_def.node, skipped_types):
+                    continue
+
+                location = local_def.node
+                while not hasattr(location, "lineno"):
+                    location = self.ancestors.parent(location)
+
+                if isinstance(location, ast.ImportFrom):
+                    if location.module == "__future__":
+                        continue
+
+                print(
+                    "W: '{}' is defined but not used at {}:{}:{}".format(
+                        local_def.name(),
+                        self.filename,
+                        location.lineno,
+                        location.col_offset,
+                    )
+                )
+
+    def visit_Module(self, node):
+        self.generic_visit(node)
+        if self.filename.endswith("__init__.py"):
+            return
+        self.check_unused(
+            node, skipped_types=(ast.FunctionDef, ast.AsyncFunctionDef,
+                                 ast.ClassDef, ast.Name)
+        )
+
+    def visit_FunctionDef(self, node):
+        self.generic_visit(node)
+        self.check_unused(node)
+
+paths = sys.argv[1:] or (None,)
+
+for path in paths:
+    with open(path) if path else sys.stdin as target:
+        module = ast.parse(target.read())
+        Beniget(path, module)
+

File diff suppressed because it is too large
+ 502 - 228
contrib/python/beniget/beniget/beniget.py


+ 86 - 0
contrib/python/beniget/beniget/ordered_set.py

@@ -0,0 +1,86 @@
+"""
+Copied from https://github.com/bustawin/ordered-set-37
+"""
+# Unlicense
+# This is free and unencumbered software released into the public domain.
+
+# Anyone is free to copy, modify, publish, use, compile, sell, or
+# distribute this software, either in source code form or as a compiled
+# binary, for any purpose, commercial or non-commercial, and by any
+# means.
+
+# In jurisdictions that recognize copyright laws, the author or authors
+# of this software dedicate any and all copyright interest in the
+# software to the public domain. We make this dedication for the benefit
+# of the public at large and to the detriment of our heirs and
+# successors. We intend this dedication to be an overt act of
+# relinquishment in perpetuity of all present and future rights to this
+# software under copyright law.
+
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+# For more information, please refer to <http://unlicense.org/>
+
+
+import sys
+
+from collections import OrderedDict
+import itertools
+from typing import TYPE_CHECKING, MutableSet
+
+if TYPE_CHECKING:
+    # trying to avoid polluting the global namespace with typing names.
+    from typing import TypeVar, Iterator, Iterable, Optional
+    T = TypeVar("T")
+
+class ordered_set(MutableSet['T']):
+    """
+    A set that preserves insertion order by internally using a dict.
+    """
+    
+    __slots__ = ('values',)
+
+    def __init__(self, elements: 'Optional[Iterable[T]]' = None):
+        self.values = OrderedDict.fromkeys(elements or [])
+
+    def add(self, x: 'T') -> None:
+        self.values[x] = None
+    
+    def update(self, values:'Iterable[T]') -> None:
+        self.values.update((k, None) for k in values)
+
+    def clear(self) -> None:
+        self.values.clear()
+
+    def discard(self, x: 'T') -> None:
+        self.values.pop(x, None)
+
+    def __getitem__(self, index:int) -> 'T':
+        try:
+            return next(itertools.islice(self.values, index, index + 1))
+        except StopIteration:
+            raise IndexError(f"index {index} out of range")
+
+    def __contains__(self, x: object) -> bool:
+        return self.values.__contains__(x)
+    
+    def __add__(self, other:'ordered_set[T]') -> 'ordered_set[T]':
+        return ordered_set(itertools.chain(self, other))
+
+    def __len__(self) -> int:
+        return self.values.__len__()
+
+    def __iter__(self) -> 'Iterator[T]':
+        return self.values.__iter__()
+
+    def __str__(self) -> str:
+        return f"{{{', '.join(str(i) for i in self)}}}"
+
+    def __repr__(self) -> str:
+        return f"<ordered_set {self}>"

+ 1 - 0
contrib/python/beniget/beniget/version.py

@@ -0,0 +1 @@
+__version__ = '0.4.2.post1'

+ 4 - 1
contrib/python/beniget/ya.make

@@ -2,7 +2,7 @@
 
 PY3_LIBRARY()
 
-VERSION(0.4.1)
+VERSION(0.4.2.post1)
 
 LICENSE(BSD-3-Clause)
 
@@ -15,7 +15,10 @@ NO_LINT()
 PY_SRCS(
     TOP_LEVEL
     beniget/__init__.py
+    beniget/__main__.py
     beniget/beniget.py
+    beniget/ordered_set.py
+    beniget/version.py
 )
 
 RESOURCE_FILES(

+ 1 - 1
yt/python/yt/yson/__init__.py

@@ -22,7 +22,7 @@ Examples:
 >>> yson.dumps(True)
 '"true"'
 
->>> number = yson.YsonInteger(10)
+>>> number = yson.YsonInt64(10)
 >>> number.attributes["my_attr"] = "hello"
 >>> yson.dumps(number)
 '<"attr"="hello">10'

Some files were not shown because too many files changed in this diff