Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3976E200B35 for ; Tue, 21 Jun 2016 04:41:47 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 37F47160A55; Tue, 21 Jun 2016 02:41:47 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E3514160A65 for ; Tue, 21 Jun 2016 04:41:44 +0200 (CEST) Received: (qmail 28521 invoked by uid 500); 21 Jun 2016 02:41:44 -0000 Mailing-List: contact commits-help@hawq.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hawq.incubator.apache.org Delivered-To: mailing list commits@hawq.incubator.apache.org Received: (qmail 28512 invoked by uid 99); 21 Jun 2016 02:41:44 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Jun 2016 02:41:44 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 8D61FC0E96 for ; Tue, 21 Jun 2016 02:41:43 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.646 X-Spam-Level: X-Spam-Status: No, score=-4.646 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id 1jcH4V8kUAy5 for ; Tue, 21 Jun 2016 02:41:30 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id AF27560CE1 for ; Tue, 21 Jun 2016 02:41:18 +0000 (UTC) Received: (qmail 27232 invoked by uid 99); 21 Jun 2016 02:41:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Jun 2016 02:41:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 871BEE009D; Tue, 21 Jun 2016 02:41:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: rlei@apache.org To: commits@hawq.incubator.apache.org Date: Tue, 21 Jun 2016 02:41:27 -0000 Message-Id: <5a374e5f2fac4e9abd380cdc1af6964e@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [11/28] incubator-hawq git commit: HAWQ-837. Add python modules into HAWQ code archived-at: Tue, 21 Jun 2016 02:41:47 -0000 http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/options.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/options.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/options.py new file mode 100755 index 0000000..8712794 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/options.py @@ -0,0 +1,275 @@ +"Main module for running pychecker a Tkinter GUI for all the options" + +import sys +import os +import Tkinter, tkFileDialog + +from OptionTypes import * +from string import capitalize, strip, rstrip, split + +import Config + +MAX_SUBBOX_ROWS = 8 +MAX_BOX_COLS = 3 +PAD = 10 +EDITOR = "xterm -e vi -n +%(line)d %(file)s" +if sys.platform == 'win32': + EDITOR = "notepad %(file)s" + +def col_weight(grid): + "Set column weights so that sticky grid settings actually work" + unused, col = grid.grid_size() + for c in range(col): + grid.columnconfigure(c, weight=1) + +def spawn(cmd_list): + try: + if os.fork(): + try: + os.execvp(cmd_list[0], cmd_list) + finally: + sys.exit() + except AttributeError: + os.execvp(cmd_list[0], cmd_list) + +def edit(file, line): + "Fire up an external editor to see the file at the given line" + unused = file, line + list = split(EDITOR) + cmd_list = [] + for word in list: + cmd_list.append(word % locals()) + spawn(cmd_list) + +def closeCB(): + sys.exit(0) + +class Results: + "Display the warnings produced by checker" + + def __init__(self, w): + self.top = Tkinter.Toplevel(w, name="results") + self.top.transient(w) + self.top.bind('', self.hide) + self.top.bind('', self.hide) + self.text = Tkinter.Text(self.top, name="text") + self.text.grid() + self.text.bind('', self.showFile) + close = Tkinter.Button(self.top, + name="close", + default=Tkinter.ACTIVE, + command=self.hide) + close.grid() + self.text.update_idletasks() + + def show(self, text): + self.text.delete("0.1", "end") + self.text.insert("0.1", text) + self.top.deiconify() + self.top.lift() + + def hide(self, *unused): + self.top.withdraw() + + def line(self): + return split(self.text.index(Tkinter.CURRENT), ".")[0] + + def showFile(self, unused): + import re + line = self.line() + text = self.text.get(line + ".0", line + ".end") + text = rstrip(text) + result = re.search("(.*):([0-9]+):", text) + if result: + file, line = result.groups() + edit(file, int(line)) + self.text.after(0, self.selectLine) + + def selectLine(self): + line = self.line() + self.text.tag_remove(Tkinter.SEL, "1.0", Tkinter.END) + self.text.tag_add(Tkinter.SEL, line + ".0", line + ".end") + +class ConfigDialog: + "Dialog for editing options" + + def __init__(self, tk): + self._tk = tk + self._cfg, _, _ = Config.setupFromArgs(sys.argv) + + self._help = None + self._optMap = {} + self._opts = [] + self._file = Tkinter.StringVar() + self._results = None + + if len(sys.argv) > 1: + self._file.set(sys.argv[1]) + + for name, group in Config._OPTIONS: + opts = [] + for _, useValue, longArg, member, description in group: + value = None + if member: + value = getattr(self._cfg, member) + description = member + ": " + capitalize(description) + description = strip(description) + tk.option_add('*' + longArg + ".help", description) + if useValue: + if type(value) == type([]): + field = List(longArg, value) + elif type(value) == type(1): + field = Number(longArg, int(value)) + elif type(value) == type(''): + field = Text(longArg, value) + else: + field = Boolean(longArg, value) + else: + field = Boolean(longArg, value) + self._optMap[longArg] = field + opts.append(field) + self._opts.append( (name, opts)) + + def _add_fields(self, w, opts): + count = 0 + for opt in opts: + f = opt.field(w) + c, r = divmod(count, MAX_SUBBOX_ROWS) + f.grid(row=r, column=c, sticky=Tkinter.NSEW) + count = count + 1 + + def _add_group(self, w, name, opts): + colFrame = Tkinter.Frame(w) + + label = Tkinter.Label(colFrame, text=name + ":") + label.grid(row=0, column=0, sticky=Tkinter.NSEW) + + gframe = Tkinter.Frame(colFrame, relief=Tkinter.GROOVE, borderwidth=2) + gframe.grid(row=1, column=0, sticky=Tkinter.NSEW) + self._add_fields(gframe, opts) + + label = Tkinter.Label(colFrame) + label.grid(row=2, column=0, sticky=Tkinter.NSEW) + colFrame.rowconfigure(2, weight=1) + return colFrame + + def main(self): + frame = Tkinter.Frame(self._tk, name="opts") + frame.grid() + self._tk.option_readfile('Options.ad') + self._fields = {} + row, col = 0, 0 + rowFrame = Tkinter.Frame(frame) + rowFrame.grid(row=row) + row = row + 1 + for name, opts in self._opts: + w = self._add_group(rowFrame, name, opts) + w.grid(row=row, column=col, sticky=Tkinter.NSEW, padx=PAD) + col = col + 1 + if col >= MAX_BOX_COLS: + col_weight(rowFrame) + rowFrame=Tkinter.Frame(frame) + rowFrame.grid(row=row, sticky=Tkinter.NSEW) + col = 0 + row = row + 1 + col_weight(rowFrame) + + self._help = Tkinter.Label(self._tk, name="helpBox") + self._help.grid(row=row) + self._help.config(takefocus=0) + buttons = Tkinter.Frame(self._tk, name="buttons") + ok = Tkinter.Button(buttons, name="ok", command=self.ok, default=Tkinter.ACTIVE) + ok.grid(row=row, column=0) + default = Tkinter.Button(buttons, name="default", command=self.default) + default.grid(row=row, column=1) + close = Tkinter.Button(buttons, name="close", command=closeCB) + close.grid(row=row, column=2) + buttons.grid() + + f = Tkinter.Frame(self._tk, name="fileStuff") + Tkinter.Button(f, name="getfile", command=self.file).grid(row=0, column=1) + fileEntry = Tkinter.Entry(f, name="fname", textvariable=self._file) + fileEntry.grid(row=0, column=2) + Tkinter.Button(f, name="check", command=self.check).grid(row=0, column=3) + f.grid(sticky=Tkinter.EW) + + self._tk.bind_all('', self.focus) + self._tk.bind_all('', self.focus) + self._tk.bind_all('', self.click) + fileEntry.bind('', self.check) + self._tk.mainloop() + + # + # Callbacks + # + + def help(self, w): + if type(w) == type(''): # occurs with file dialog... + return + if self._help == w: # ignore help events on help... + return + help = w.option_get("help", "help") + self._help.configure(text=help) + + def focus(self, ev): + self.help(ev.widget) + + def click(self, ev): + self.help(ev.widget) + + def ok(self): + opts = [] + # Pull command-line args + for _, group in self._opts: + for opt in group: + arg = opt.arg() + if arg: + opts.append(arg) + + # Calculate config + self._cfg, _, _ = Config.setupFromArgs(opts) + + # Set controls based on new config + for _, group in Config._OPTIONS: + for _, _, longArg, member, _ in group: + if member: + self._optMap[longArg].set(getattr(self._cfg, member)) + + def default(self): + self._cfg, _, _ = Config.setupFromArgs(sys.argv) + for _, group in Config._OPTIONS: + for _, _, longArg, member, _ in group: + if member: + self._optMap[longArg].set(getattr(self._cfg, member)) + else: + self._optMap[longArg].set(0) + + def file(self): + self._file.set(tkFileDialog.askopenfilename()) + + def check(self, *unused): + import checker + import StringIO + + self.ok() # show effect of all settings + + checker._allModules = {} + warnings = checker.getWarnings([self._file.get()], self._cfg) + capture = StringIO.StringIO() + + if not self._results: + self._results = Results(self._help) + checker._printWarnings(warnings, capture) + + value = strip(capture.getvalue()) + if not value: + value = "None" + self._results.show(value) + + +if __name__=='__main__': + dirs = os.path.join(os.path.split(os.getcwd())[:-1]) + sys.path.append(dirs[0]) + tk = Tkinter.Tk() + tk.title('PyChecker') + ConfigDialog(tk).main() http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/pcmodules.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/pcmodules.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/pcmodules.py new file mode 100644 index 0000000..81f3c22 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/pcmodules.py @@ -0,0 +1,19 @@ +""" +Track loaded PyCheckerModules together with the directory they were loaded from. +This allows us to differentiate between loaded modules with the same name +but from different paths, in a way that sys.modules doesn't do. +""" + +__pcmodules = {} + +def getPCModule(moduleName, moduleDir=None): + global __pcmodules + return __pcmodules.get((moduleName, moduleDir), None) + +def getPCModules(): + global __pcmodules + return __pcmodules.values() + +def addPCModule(pcmodule): + global __pcmodules + __pcmodules[(pcmodule.moduleName, pcmodule.moduleDir)] = pcmodule http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/printer.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/printer.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/printer.py new file mode 100644 index 0000000..3b8e9b1 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/printer.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +# Copyright (c) 2001, MetaSlash Inc. All rights reserved. + +"Helper functions for printing out info about objects" + +from pychecker import utils + +def printFunction(spaces, prefix, func, className = None) : + params = '' + argcount = func.func_code.co_argcount + defaultArgStart = argcount + if func.func_defaults != None : + defaultArgStart = argcount - len(func.func_defaults) + for i in range(0, argcount) : + arg = func.func_code.co_varnames[i] + if i >= defaultArgStart : + arg = arg + " = %s" % utils.safestr(func.func_defaults[i - defaultArgStart]) + params = params + "%s, " % arg + params = "(%s)" % params[:-2] + if className == None : + className = "" + else : + className = className + "." + print "%s%s%s%s%s" % (spaces, prefix, className, func.func_name, params) + + +def module(module) : + print "Module: ", module.moduleName + if module.module == None : + return + + print " Imports: ", module.modules.keys() + print " Variables:", module.variables.keys() + print "" + for function in module.functions.values() : + printFunction(" ", "Function: ", function.function) + print "" + for c in module.classes.values() : + for method in c.methods.values() : + if method != None : + printFunction(" ", "", method.function, c.name) + print "" + +def attrs(object) : + for attr in dir(object) : + print " %s: %s" % (attr, `getattr(object, attr)`) http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/python.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/python.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/python.py new file mode 100644 index 0000000..6f9d6ab --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/python.py @@ -0,0 +1,427 @@ +#!/usr/bin/env python + +# Copyright (c) 2001-2004, MetaSlash Inc. All rights reserved. +# Portions Copyright (c) 2005, Google, Inc. All rights reserved. + +""" +Setup a lot of info about Python builtin types, functions, methods, etc. +""" + +import types +import sys + +from pychecker import utils +from pychecker import Stack +from pychecker import Warning + + +BOOL = types.IntType +# name (type, args: min, max, kwArgs? +GLOBAL_FUNC_INFO = { '__import__': (types.ModuleType, 1, 4), + 'abs': (Stack.TYPE_UNKNOWN, 1, 1), + 'apply': (Stack.TYPE_UNKNOWN, 2, 3), + 'buffer': (types.BufferType, 1, 3), + 'callable': (BOOL, 1, 1), + 'chr': (types.StringType, 1, 1), + 'cmp': (types.IntType, 2, 2), + 'coerce': ([ types.NoneType, types.TupleType ], 2, 2), + 'compile': (types.CodeType, 3, 3), + 'complex': (types.ComplexType, 1, 2, ['real', 'imag']), + 'delattr': (types.NoneType, 2, 2), + 'dir': (types.ListType, 0, 1), + 'divmod': (types.TupleType, 2, 2), + 'eval': (Stack.TYPE_UNKNOWN, 1, 3), + 'execfile': (types.NoneType, 1, 3), + 'filter': (types.ListType, 2, 2), + 'float': (types.FloatType, 1, 1), + 'getattr': (Stack.TYPE_UNKNOWN, 2, 3), + 'globals': (types.DictType, 0, 0), + 'hasattr': (BOOL, 2, 2), + 'hash': (types.IntType, 1, 1), + 'hex': (types.StringType, 1, 1), + 'id': (types.IntType, 1, 1), + 'input': (Stack.TYPE_UNKNOWN, 0, 1), + 'int': (types.IntType, 1, 2, ['x']), + 'intern': (types.StringType, 1, 1), + 'isinstance': (BOOL, 2, 2), + 'issubclass': (BOOL, 2, 2), + 'len': (types.IntType, 1, 1), + 'list': (types.ListType, 1, 1, ['sequence']), + 'locals': (types.DictType, 0, 0), + 'long': (types.LongType, 1, 2, ['x']), + 'map': (types.ListType, 2, None), + 'max': (Stack.TYPE_UNKNOWN, 1, None), + 'min': (Stack.TYPE_UNKNOWN, 1, None), + 'oct': (types.StringType, 1, 1), + 'open': (types.FileType, 1, 3, ['name', 'mode', 'buffering']), + 'ord': (types.IntType, 1, 1), + 'pow': (Stack.TYPE_UNKNOWN, 2, 3), + 'range': (types.ListType, 1, 3), + 'raw_input': (types.StringType, 0, 1), + 'reduce': (Stack.TYPE_UNKNOWN, 2, 3), + 'reload': (types.ModuleType, 1, 1), + 'repr': (types.StringType, 1, 1), + 'round': (types.FloatType, 1, 2), + 'setattr': (types.NoneType, 3, 3), + 'slice': (types.SliceType, 1, 3), + 'str': (types.StringType, 1, 1), + 'tuple': (types.TupleType, 1, 1), + 'type': (types.TypeType, 1, 1), + 'vars': (types.DictType, 0, 1), + 'xrange': (types.ListType, 1, 3), + } + +if hasattr(types, 'UnicodeType') : + GLOBAL_FUNC_INFO['unichr'] = (types.UnicodeType, 1, 1) + GLOBAL_FUNC_INFO['unicode'] = (types.UnicodeType, 1, 3, ['string', 'encoding', 'errors']) + +if utils.pythonVersion() >= utils.PYTHON_2_2 : + GLOBAL_FUNC_INFO['compile'] = (types.CodeType, 3, 5) + GLOBAL_FUNC_INFO['dict'] = (types.DictType, 0, 1, ['items']) + GLOBAL_FUNC_INFO['file'] = GLOBAL_FUNC_INFO['open'] + GLOBAL_FUNC_INFO['float'] = (types.FloatType, 0, 1, ['x']) + GLOBAL_FUNC_INFO['int'] = (types.IntType, 0, 2, ['x']) + GLOBAL_FUNC_INFO['list'] = (types.ListType, 0, 1, ['sequence']) + GLOBAL_FUNC_INFO['long'] = (types.LongType, 0, 2, ['x']) + GLOBAL_FUNC_INFO['str'] = (types.StringType, 0, 1, ['object']) + # FIXME: type doesn't take 2 args, only 1 or 3 + GLOBAL_FUNC_INFO['type'] = (types.TypeType, 1, 3, ['name', 'bases', 'dict']) + GLOBAL_FUNC_INFO['tuple'] = (types.TupleType, 0, 1, ['sequence']) + + GLOBAL_FUNC_INFO['classmethod'] = (types.MethodType, 1, 1) + GLOBAL_FUNC_INFO['iter'] = (Stack.TYPE_UNKNOWN, 1, 2) + GLOBAL_FUNC_INFO['property'] = (Stack.TYPE_UNKNOWN, 0, 4, ['fget', 'fset', 'fdel', 'doc']) + GLOBAL_FUNC_INFO['super'] = (Stack.TYPE_UNKNOWN, 1, 2) + GLOBAL_FUNC_INFO['staticmethod'] = (types.MethodType, 1, 1) + GLOBAL_FUNC_INFO['unicode'] = (types.UnicodeType, 0, 3, ['string', 'encoding', 'errors']) + + GLOBAL_FUNC_INFO['bool'] = (BOOL, 1, 1, ['x']) + +if utils.pythonVersion() >= utils.PYTHON_2_3: + GLOBAL_FUNC_INFO['dict'] = (types.DictType, 0, 1, []) + +if utils.pythonVersion() >= utils.PYTHON_2_5: + GLOBAL_FUNC_INFO['max'] = (Stack.TYPE_UNKNOWN, 1, None, ['key']) + GLOBAL_FUNC_INFO['min'] = (Stack.TYPE_UNKNOWN, 1, None, ['key']) + +def tryAddGlobal(name, *args): + if globals().has_key(name): + GLOBAL_FUNC_INFO[name] = args + +zipMinArgs = 1 +if utils.pythonVersion() >= utils.PYTHON_2_4: + zipMinArgs = 0 + +tryAddGlobal('zip', types.ListType, zipMinArgs, None) + +tryAddGlobal('enumerate', types.TupleType, 1, 1, ['sequence']) +# sum() could also return float/long +tryAddGlobal('sum', types.IntType, 1, 2, ['start']) +# sorted() and reversed() always return an iterator (FIXME: support iterator) +tryAddGlobal('sorted', Stack.TYPE_UNKNOWN, 1, 1) +tryAddGlobal('reversed', Stack.TYPE_UNKNOWN, 1, 1) + +tryAddGlobal('all', BOOL, 1, 1) +tryAddGlobal('any', BOOL, 1, 1) + +_STRING_METHODS = { 'capitalize': (types.StringType, 0, 0), + 'center': (types.StringType, 1, 1), + 'count': (types.IntType, 1, 1), + 'encode': (types.StringType, 0, 2), + 'endswith': (BOOL, 1, 3), + 'expandtabs': (types.StringType, 0, 1), + 'find': (types.IntType, 1, 3), + 'index': (types.IntType, 1, 3), + 'isalnum': (BOOL, 0, 0), + 'isalpha': (BOOL, 0, 0), + 'isdigit': (BOOL, 0, 0), + 'islower': (BOOL, 0, 0), + 'isspace': (BOOL, 0, 0), + 'istitle': (BOOL, 0, 0), + 'isupper': (BOOL, 0, 0), + 'join': (types.StringType, 1, 1), + 'ljust': (types.StringType, 1, 1), + 'lower': (types.StringType, 0, 0), + 'lstrip': (types.StringType, 0, 0), + 'replace': (types.StringType, 2, 3), + 'rfind': (types.IntType, 1, 3), + 'rindex': (types.IntType, 1, 3), + 'rjust': (types.StringType, 1, 1), + 'rstrip': (types.StringType, 0, 0), + 'split': (types.ListType, 0, 2), + 'splitlines': (types.ListType, 0, 1), + 'startswith': (BOOL, 1, 3), + 'strip': (types.StringType, 0, 0), + 'swapcase': (types.StringType, 0, 0), + 'title': (types.StringType, 0, 0), + 'translate': (types.StringType, 1, 2), + 'upper': (types.StringType, 0, 0), + } + +if utils.pythonVersion() >= utils.PYTHON_2_2 : + _STRING_METHODS['decode'] = (types.UnicodeType, 0, 2) + _STRING_METHODS['zfill'] = (types.StringType, 1, 1) + +if utils.pythonVersion() >= utils.PYTHON_2_4: + _STRING_METHODS['rsplit'] = (types.StringType, 0, 2) + _STRING_METHODS['center'] = (types.StringType, 1, 2), + _STRING_METHODS['ljust'] = (types.StringType, 1, 2), + _STRING_METHODS['rjust'] = (types.StringType, 1, 2), + +BUILTIN_METHODS = { types.DictType : + { 'clear': (types.NoneType, 0, 0), + 'copy': (types.DictType, 0, 0), + 'get': (Stack.TYPE_UNKNOWN, 1, 2), + 'has_key': (BOOL, 1, 1), + 'items': (types.ListType, 0, 0), + 'keys': (types.ListType, 0, 0), + 'popitem': (types.TupleType, 0, 0), + 'setdefault': (Stack.TYPE_UNKNOWN, 1, 2), + 'update': (types.NoneType, 1, 1), + 'values': (types.ListType, 0, 0), + }, + types.ListType : + { 'append': (types.NoneType, 1, 1), + 'count': (types.IntType, 1, 1), + 'extend': (types.NoneType, 1, 1), + 'index': (types.IntType, 1, 1), + 'insert': (types.NoneType, 2, 2), + 'pop': (Stack.TYPE_UNKNOWN, 0, 1), + 'remove': (types.NoneType, 1, 1), + 'reverse': (types.NoneType, 0, 0), + 'sort': (types.NoneType, 0, 1), + }, + types.FileType : + { 'close': (types.NoneType, 0, 0), + 'fileno': (types.IntType, 0, 0), + 'flush': (types.NoneType, 0, 0), + 'isatty': (BOOL, 0, 0), + 'read': (types.StringType, 0, 1), + 'readinto': (types.NoneType, 1, 1), + 'readline': (types.StringType, 0, 1), + 'readlines': (types.ListType, 0, 1), + 'seek': (types.NoneType, 1, 2), + 'tell': (types.IntType, 0, 0), + 'truncate': (types.NoneType, 0, 1), + 'write': (types.NoneType, 1, 1), + 'writelines': (types.NoneType, 1, 1), + 'xreadlines': (types.ListType, 0, 0), + }, + } + +if utils.pythonVersion() >= utils.PYTHON_2_4: + GLOBAL_FUNC_INFO['set'] = (Stack.TYPE_UNKNOWN, 0, 1) + GLOBAL_FUNC_INFO['frozenset'] = (Stack.TYPE_UNKNOWN, 0, 1) + + kwargs = ['cmp', 'key', 'reverse'] + BUILTIN_METHODS[types.ListType]['sort'] = (types.NoneType, 0, 3, kwargs) + BUILTIN_METHODS[types.DictType]['update'] = (types.NoneType, 1, 1, []) + +if hasattr({}, 'pop'): + BUILTIN_METHODS[types.DictType]['pop'] = (Stack.TYPE_UNKNOWN, 1, 2) + +if utils.pythonVersion() >= utils.PYTHON_2_5: + _STRING_METHODS['partition'] = (types.TupleType, 1, 1) + _STRING_METHODS['rpartition'] = (types.TupleType, 1, 1) + +if utils.pythonVersion() >= utils.PYTHON_2_6: + GLOBAL_FUNC_INFO['bin'] = (types.StringType, 1, 1) + GLOBAL_FUNC_INFO['bytesarray'] = (bytearray, 0, 1) + GLOBAL_FUNC_INFO['bytes'] = (bytes, 0, 1) + GLOBAL_FUNC_INFO['format'] = (types.StringType, 1, 2) + GLOBAL_FUNC_INFO['next'] = (Stack.TYPE_UNKNOWN, 1, 2) + GLOBAL_FUNC_INFO['print'] = (types.NoneType, 0, None, + ['sep', 'end', 'file']) + +def _setupBuiltinMethods() : + if utils.pythonVersion() >= utils.PYTHON_2_2 : + PY22_DICT_METHODS = { 'iteritems': (types.ListType, 0, 0), + 'iterkeys': (types.ListType, 0, 0), + 'itervalues': (types.ListType, 0, 0), + } + + BUILTIN_METHODS[types.DictType].update(PY22_DICT_METHODS) + + try : + BUILTIN_METHODS[types.ComplexType] = \ + { 'conjugate': (types.ComplexType, 0, 0), } + except AttributeError : + pass + + if len(dir('')) > 0 : + BUILTIN_METHODS[types.StringType] = _STRING_METHODS + + try : + BUILTIN_METHODS[types.UnicodeType] = _STRING_METHODS + except AttributeError : + pass + +_setupBuiltinMethods() + +MUTABLE_TYPES = (types.ListType, types.DictType, types.InstanceType,) + +# identifiers which will become a keyword in a future version +FUTURE_KEYWORDS = { 'yield': '2.2', 'with': '2.5', 'as': '2.5' } + +METHODLESS_OBJECTS = { types.NoneType : None, types.IntType : None, + types.LongType : None, types.FloatType : None, + types.BufferType : None, types.TupleType : None, + types.EllipsisType : None, + } + +def _setupBuiltinAttrs() : + item = Stack.Item(None, None) + BUILTIN_ATTRS[types.MethodType] = dir(item.__init__) + del item + + if utils.pythonVersion() >= utils.PYTHON_2_2 : + # FIXME: I'm sure more types need to be added here + BUILTIN_ATTRS[types.StringType] = dir(''.__class__) + BUILTIN_ATTRS[types.ListType] = dir([].__class__) + BUILTIN_ATTRS[types.DictType] = dir({}.__class__) + + try : + import warnings + _MSG = "xrange object's 'start', 'stop' and 'step' attributes are deprecated" + warnings.filterwarnings('ignore', _MSG) + del warnings, _MSG + except (ImportError, AssertionError): + pass + BUILTIN_ATTRS[types.XRangeType] = dir(xrange(0)) + + try: BUILTIN_ATTRS[types.ComplexType] = dir(complex(0, 1)) + except: pass + + try: BUILTIN_ATTRS[types.UnicodeType] = dir(unicode('')) + except: pass + + try: BUILTIN_ATTRS[types.CodeType] = dir(_setupBuiltinAttrs.func_code) + except: pass + + try: BUILTIN_ATTRS[types.FileType] = dir(sys.__stdin__) + except: pass + + try: + raise TypeError + except TypeError : + try: + tb = sys.exc_info()[2] + BUILTIN_ATTRS[types.TracebackType] = dir(tb) + BUILTIN_ATTRS[types.FrameType] = dir(tb.tb_frame) + except: + pass + tb = None + +BUILTIN_ATTRS = { types.StringType : dir(''), + types.TypeType : dir(type(type)), + types.ListType : dir([]), + types.DictType : dir({}), + types.FunctionType : dir(_setupBuiltinAttrs), + types.BuiltinFunctionType : dir(len), + types.BuiltinMethodType : dir([].append), + types.ClassType : dir(Stack.Item), + types.UnboundMethodType : dir(Stack.Item.__init__), + types.LambdaType : dir(lambda: None), + types.SliceType : dir(slice(0)), + } + +# have to setup the rest this way to support different versions of Python +_setupBuiltinAttrs() + +PENDING_DEPRECATED_MODULES = { 'string': None, 'types': None, + } +DEPRECATED_MODULES = { 'FCNTL': 'fcntl', 'gopherlib': None, + 'macfs': 'Carbon.File or Carbon.Folder', + 'posixfile': 'fcntl', 'pre': None, 'regsub': 're', + 'statcache': 'os.stat()', + 'stringold': None, 'tzparse': None, + 'TERMIOS': 'termios', 'whrandom':'random', + 'xmllib': 'xml.sax', + + # C Modules + 'mpz': None, 'pcre': None, 'pypcre': None, + 'rgbimg': None, 'strop': None, 'xreadlines': 'file', + } +DEPRECATED_ATTRS = { 'array.read': None, 'array.write': None, + 'operator.isCallable': None, + 'operator.sequenceIncludes': None, + 'pty.master_open': None, 'pty.slave_open': None, + 'random.stdgamma': 'random.gammavariate', + 'rfc822.AddrlistClass': 'rfc822.AddressList', + 'string.atof': None, 'string.atoi': None, + 'string.atol': None, 'string.zfill': None, + 'sys.exc_traceback': None, 'sys.exit_thread': None, + 'tempfile.mktemp': None, 'tempfile.template': None, + } + +# FIXME: can't check these right now, maybe later +DEPRECATED_METHODS = { + 'email.Message.get_type': 'email.Message.get_content_type', + 'email.Message.get_subtype': 'email.Message.get_content_subtype', + 'email.Message.get_main_type': 'email.Message.get_content_maintype', + 'htmllib.HTMLParser.do_nextid': None, + 'pstats.Stats.ignore': None, + 'random.Random.cunifvariate': None, + 'random.Random.stdgamma': 'Random.gammavariate', + } + +_OS_AND_POSIX_FUNCS = { 'tempnam': None, 'tmpnam': None } +SECURITY_FUNCS = { 'os' : _OS_AND_POSIX_FUNCS, 'posix': _OS_AND_POSIX_FUNCS } + +SPECIAL_METHODS = { + '__call__': None, # any number > 1 + '__cmp__': 2, + '__coerce__': 2, + '__contains__': 2, + '__del__': 1, + '__hash__': 1, + '__iter__': 1, + '__len__': 1, + '__new__': None, # new-style class constructor + '__nonzero__': 1, + + '__hex__': 1, + '__oct__': 1, + '__repr__': 1, + '__str__': 1, + + '__invert__': 1, '__neg__': 1, '__pos__': 1, '__abs__': 1, + '__complex__': 1, '__int__': 1, '__long__': 1, '__float__': 1, + + '__eq__': 2, '__ne__': 2, + '__ge__': 2, '__gt__': 2, + '__le__': 2, '__lt__': 2, + + '__getattribute__': 2, # only in new-style classes + '__get__': 3, '__set__': 3, '__delete__': 2, + '__getattr__': 2, '__setattr__': 3, '__delattr__': 2, + '__getitem__': 2, '__setitem__': 3, '__delitem__': 2, + '__getslice__': 3, '__setslice__': 4, '__delslice__': 3, + # getslice is deprecated + + '__add__': 2, '__radd__': 2, '__iadd__': 2, + '__sub__': 2, '__rsub__': 2, '__isub__': 2, + '__mul__': 2, '__rmul__': 2, '__imul__': 2, + '__div__': 2, '__rdiv__': 2, '__idiv__': 2, + # __pow__: 2 or 3 __ipow__: 2 or 3 + '__pow__': 3, '__rpow__': 2, '__ipow__': 3, + '__truediv__': 2, '__rtruediv__': 2, '__itruediv__': 2, + '__floordiv__': 2, '__rfloordiv__': 2, '__ifloordiv__': 2, + '__mod__': 2, '__rmod__': 2, '__imod__': 2, + '__divmod__': 2, '__rdivmod__': 2, # no inplace op for divmod() + + '__lshift__': 2, '__rlshift__': 2, '__ilshift__': 2, + '__rshift__': 2, '__rrshift__': 2, '__irshift__': 2, + + '__and__': 2, '__rand__': 2, '__iand__': 2, + '__xor__': 2, '__rxor__': 2, '__ixor__': 2, + '__or__': 2, '__ror__': 2, '__ior__': 2, + + # these are related to pickling + '__getstate__': 1, '__setstate__': 2, + '__copy__': 1, '__deepcopy__': 2, + '__getinitargs__': 1, '__getnewargs__': 1, + '__reduce__': 1, '__reduce_ex__': 2, + } + +NEW_STYLE_CLASS_METHODS = ['__getattribute__', '__set__', '__get__', '__delete__'] http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/utils.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/utils.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/utils.py new file mode 100644 index 0000000..298174b --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/utils.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python + +# Copyright (c) 2001-2004, MetaSlash Inc. All rights reserved. + +""" +Print out warnings from Python source files. +""" + +import sys +import string +import copy + +from pychecker import msgs +from pychecker import Config +from pychecker.Warning import Warning + + +VAR_ARGS_BITS = 8 +MAX_ARGS_MASK = ((1 << VAR_ARGS_BITS) - 1) + +INIT = '__init__' +LAMBDA = '' + +# number of instructions to check backwards if it was a return +BACK_RETURN_INDEX = 4 + + +_cfg = [] + +def cfg() : + return _cfg[-1] + +def initConfig(cfg) : + _cfg.append(cfg) + +def pushConfig() : + newCfg = copy.copy(cfg()) + _cfg.append(newCfg) + +def popConfig() : + del _cfg[-1] + + +def shouldUpdateArgs(operand) : + return operand == Config.CHECKER_VAR + +def updateCheckerArgs(argStr, func, lastLineNum, warnings) : + try : + argList = string.split(argStr) + # don't require long options to start w/--, we can add that for them + for i in range(0, len(argList)) : + if argList[i][0] != '-' : + argList[i] = '--' + argList[i] + + cfg().processArgs(argList) + return 1 + except Config.UsageError, detail : + warn = Warning(func, lastLineNum, msgs.INVALID_CHECKER_ARGS % detail) + warnings.append(warn) + return 0 + + +def debug(*args) : + if cfg().debug: print args + + +PYTHON_1_5 = 0x10502 +PYTHON_2_0 = 0x20000 +PYTHON_2_1 = 0x20100 +PYTHON_2_2 = 0x20200 +PYTHON_2_3 = 0x20300 +PYTHON_2_4 = 0x20400 +PYTHON_2_5 = 0x20500 +PYTHON_2_6 = 0x20600 +PYTHON_3_0 = 0x30000 + +def pythonVersion() : + return sys.hexversion >> 8 + +def startswith(s, substr) : + "Ugh, supporting python 1.5 is a pain" + return s[0:len(substr)] == substr + +def endswith(s, substr) : + "Ugh, supporting python 1.5 is a pain" + return s[-len(substr):] == substr + + +# generic method that can be slapped into any class, thus the self parameter +def std_repr(self) : + return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), safestr(self)) + +try: + unicode, UnicodeError +except NameError: + class UnicodeError(Exception): pass + +def safestr(value): + try: + return str(value) + except UnicodeError: + return unicode(value) http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/warn.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/warn.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/warn.py new file mode 100644 index 0000000..8126f2e --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker/warn.py @@ -0,0 +1,778 @@ +#!/usr/bin/env python + +# Copyright (c) 2001-2002, MetaSlash Inc. All rights reserved. +# Portions Copyright (c) 2005, Google, Inc. All rights reserved. + +""" +Print out warnings from Python source files. +""" + +import os.path +import sys +import string +import types +import traceback +import imp +import re + +from pychecker import OP +from pychecker import Stack +from pychecker import function +from pychecker import python +from pychecker import pcmodules + +from pychecker import msgs +from pychecker import utils +from pychecker import CodeChecks +from pychecker.Warning import Warning + + +def cfg() : + return utils.cfg() + +def _checkSelfArg(method, warnings) : + """Return a Warning if there is no self parameter or + the first parameter to a method is not self.""" + + if not cfg().methodArgName: + return + + code = method.function.func_code + err = None + if method.isStaticMethod(): + if code.co_argcount > 0 and cfg().methodArgName == code.co_varnames[0]: + err = msgs.SELF_IS_ARG % 'staticmethod' + elif code.co_argcount < 1: + err = msgs.NO_METHOD_ARGS % cfg().methodArgName + else: + if method.isClassMethod(): + if code.co_varnames[0] not in cfg().classmethodArgNames: + err = msgs.SELF_NOT_FIRST_ARG % \ + (cfg().classmethodArgNames, 'class') + elif code.co_varnames[0] != cfg().methodArgName: + err = msgs.SELF_NOT_FIRST_ARG % (cfg().methodArgName, '') + + if err is not None : + warnings.append(Warning(code, code, err)) + + +def _checkNoSelfArg(func, warnings) : + "Return a Warning if there is a self parameter to a function." + + code = func.function.func_code + if code.co_argcount > 0 and cfg().methodArgName in code.co_varnames: + warnings.append(Warning(code, code, msgs.SELF_IS_ARG % 'function')) + + +def _checkSubclass(c1, c2): + try: + return issubclass(c1.classObject, c2.classObject) + except (TypeError, AttributeError): + return 0 + + +_IGNORE_RETURN_TYPES = ( Stack.TYPE_FUNC_RETURN, Stack.TYPE_ATTRIBUTE, + Stack.TYPE_GLOBAL, Stack.TYPE_COMPARISON, + Stack.TYPE_UNKNOWN) + +def _checkReturnWarnings(code) : + is_getattr = code.func_code.co_name in ('__getattr__', '__getattribute__') + if is_getattr : + for line, retval, dummy in code.returnValues : + if retval.isNone() : + err = msgs.DONT_RETURN_NONE % code.func_code.co_name + code.addWarning(err, line+1) + + # there must be at least 2 real return values to check for consistency + returnValuesLen = len(code.returnValues) + if returnValuesLen < 2 : + return + + # if the last return is implicit, check if there are non None returns + lastReturn = code.returnValues[-1] + + # Python 2.4 optimizes the dead implicit return out, so we can't + # distinguish implicit and explicit "return None" + if utils.pythonVersion() < utils.PYTHON_2_4 and \ + not code.starts_and_ends_with_finally and \ + cfg().checkImplicitReturns and lastReturn[1].isImplicitNone(): + for line, retval, dummy in code.returnValues[:-1] : + if not retval.isNone() : + code.addWarning(msgs.IMPLICIT_AND_EXPLICIT_RETURNS, + lastReturn[0]+1) + break + + # __get*__ funcs can return different types, don't warn about inconsistency + if utils.startswith(code.func_code.co_name, '__get') and \ + utils.endswith(code.func_code.co_name, '__') : + return + + returnType, returnData = None, None + for line, value, dummy in code.returnValues : + if not value.isNone() : + valueType = value.getType(code.typeMap) + if returnType is None and valueType not in _IGNORE_RETURN_TYPES : + returnData = value + returnType = valueType + continue + + # always ignore None, None can be returned w/any other type + # FIXME: if we stored func return values, we could do better + if returnType is not None and not value.isNone() and \ + valueType not in _IGNORE_RETURN_TYPES and \ + returnData.type not in _IGNORE_RETURN_TYPES : + ok = returnType in (type(value.data), valueType) + if ok : + if returnType == types.TupleType : + # FIXME: this isn't perfect, if len == 0 + # the length can really be 0 OR unknown + # we shouldn't check the lengths for equality + # ONLY IF one of the lengths is truly unknown + if returnData.length > 0 and value.length > 0: + ok = returnData.length == value.length + else : + ok = _checkSubclass(returnType, valueType) or \ + _checkSubclass(valueType, returnType) + if not ok : + code.addWarning(msgs.INCONSISTENT_RETURN_TYPE, line) + + +def _checkComplex(code, maxValue, value, func, err) : + if maxValue and value > maxValue : + line = func.function.func_code.co_firstlineno + code.addWarning(err % (func.function.__name__, value), line) + + +def _checkCode(code, codeSource) : + while code.index < code.maxCode : + op, oparg, operand = code.popNextOp() + dispatch_func = CodeChecks.DISPATCH[op] + if dispatch_func is not None : + dispatch_func(oparg, operand, codeSource, code) + +def _name_unused(var) : + if var in cfg().unusedNames : + return 0 + for name in cfg().unusedNames : + if name != '_' and utils.startswith(var, name) : + return 0 + return 1 + +def _checkUnusedParam(var, line, func, code) : + if line is not None and line == 0 and _name_unused(var) : + if ((cfg().ignoreSelfUnused or var != cfg().methodArgName) and + (cfg().varArgumentsUsed or func.varArgName() != var)) : + code.addWarning(msgs.UNUSED_PARAMETER % var, code.func_code) + +def _handleNestedCode(func_code, code, codeSource): + nested = not (codeSource.main or codeSource.in_class) + if func_code.co_name == utils.LAMBDA or nested: + utils.debug(' handling nested code') + varnames = None + if nested and func_code.co_name != utils.LAMBDA: + varnames = func_code.co_varnames + \ + codeSource.calling_code[-1].function.func_code.co_varnames + # save the original return value and restore after checking + returnValues = code.returnValues + code.init(function.create_fake(func_code.co_name, func_code, {}, + varnames)) + _checkCode(code, codeSource) + code.returnValues = returnValues + +def _findUnreachableCode(code) : + # code after RETURN or RAISE is unreachable unless there's a branch to it + unreachable = {} + terminals = code.returnValues[:-1] + code.raiseValues + terminals.sort(lambda a, b: cmp(a[2], b[2])) + for line, dummy, i in terminals : + if not code.branches.has_key(i) : + unreachable[i] = line + + # find the index of the last return + lastLine = lastItem = lastIndex = None + if code.returnValues: + lastLine, lastItem, lastIndex = code.returnValues[-1] + if len(code.returnValues) >= 2 : + lastIndex = code.returnValues[-2][2] + if code.raiseValues : + lastIndex = max(lastIndex, code.raiseValues[-1][2]) + + # remove last return if it's unreachable AND implicit + if unreachable.get(lastIndex) == lastLine and lastItem and \ + lastItem.isImplicitNone(): + del code.returnValues[-1] + del unreachable[lastIndex] + + if cfg().unreachableCode : + for index in unreachable.keys() : + try : + if not OP.JUMP_FORWARD(ord(code.bytes[index])) : + code.addWarning(msgs.CODE_UNREACHABLE, unreachable[index]) + except IndexError : + pass + + +def _checkFunction(module, func, c = None, main = 0, in_class = 0) : + "Return a list of Warnings found in a function/method." + + # always push a new config object, so we can pop at end of function + utils.pushConfig() + + code = CodeChecks.Code() + code.init(func) + if main: + for key in func.function.func_globals.keys(): + code.unusedLocals[key] = -1 + codeSource = CodeChecks.CodeSource(module, func, c, main, in_class, code) + try : + _checkCode(code, codeSource) + if not in_class : + _findUnreachableCode(code) + + # handle lambdas and nested functions + codeSource.calling_code.append(func) + for func_code in code.codeObjects.values() : + _handleNestedCode(func_code, code, codeSource) + del codeSource.calling_code[-1] + + except (SystemExit, KeyboardInterrupt) : + exc_type, exc_value, exc_tb = sys.exc_info() + raise exc_type, exc_value + except : + exc_type, exc_value, exc_tb = sys.exc_info() + exc_list = traceback.format_exception(exc_type, exc_value, exc_tb) + for index in range(0, len(exc_list)) : + exc_list[index] = string.replace(exc_list[index], "\n", "\n\t") + code.addWarning(msgs.CHECKER_BROKEN % string.join(exc_list, "")) + + if cfg().checkReturnValues : + _checkReturnWarnings(code) + + if cfg().localVariablesUsed : + for var, line in code.unusedLocals.items() : + if line is not None and line > 0 and _name_unused(var) : + code.addWarning(msgs.UNUSED_LOCAL % var, line) + + if cfg().argumentsUsed : + op = code.getFirstOp() + if not (OP.RAISE_VARARGS(op) or OP.RETURN_VALUE(op)) : + for var, line in code.unusedLocals.items() : + _checkUnusedParam(var, line, func, code) + + # Check code complexity: + # loops should be counted as one branch, but there are typically 3 + # branches in byte code to setup a loop, so subtract off 2/3's of them + # / 2 to approximate real branches + branches = (len(code.branches.keys()) - (2 * code.loops)) / 2 + lines = (code.getLineNum() - code.func_code.co_firstlineno) + returns = len(code.returnValues) + if not main and not in_class : + args = code.func_code.co_argcount + locals = len(code.func_code.co_varnames) - args + _checkComplex(code, cfg().maxArgs, args, func, msgs.TOO_MANY_ARGS) + _checkComplex(code, cfg().maxLocals, locals, func, msgs.TOO_MANY_LOCALS) + _checkComplex(code, cfg().maxLines, lines, func, msgs.FUNC_TOO_LONG) + _checkComplex(code, cfg().maxReturns, returns, func, msgs.TOO_MANY_RETURNS) + _checkComplex(code, cfg().maxBranches, branches, func, msgs.TOO_MANY_BRANCHES) + + if not (main or in_class) : + utils.popConfig() + func.returnValues = code.returnValues + return (code.warnings, code.globalRefs, code.functionsCalled, + code.codeObjects.values(), code.returnValues) + + +def _getUnused(module, globalRefs, dict, msg, filterPrefix = None) : + "Return a list of warnings for unused globals" + + warnings = [] + for ref in dict.keys() : + check = not filterPrefix or utils.startswith(ref, filterPrefix) + if check and globalRefs.get(ref) == None : + lineInfo = module.moduleLineNums.get(ref) + if lineInfo: + warnings.append(Warning(lineInfo[0], lineInfo[1], msg % ref)) + return warnings + + +def _get_func_info(method) : + try: + fc = getattr(method.im_func, 'func_code', None) + if fc is not None : + return fc.co_filename, fc.co_firstlineno + except AttributeError: + # if the object derives from any object in 2.2, + # the builtin methods are wrapper_descriptors and + # have no im_func attr + pass + return None, None + +_DOT_INIT = '.' + utils.INIT + +def _baseInitCalled(classInitInfo, base, functionsCalled) : + baseInit = getattr(base, utils.INIT, None) + if baseInit is None or _get_func_info(baseInit) == classInitInfo : + return 1 + + initName = utils.safestr(base) + _DOT_INIT + if functionsCalled.has_key(initName) : + return 1 + + # ok, do this the hard way, there may be aliases, so check here + names = string.split(initName, '.') + + # first look in our list of PyCheckerModules + moduleName = names[0] + moduleDir = os.path.dirname(classInitInfo[0]) + pcmodule = pcmodules.getPCModule(moduleName, moduleDir) + if pcmodule: + obj = pcmodule.module + else: + # fall back to looking in sys.modules + try: + # i think this can raise an exception if the module is a library + # (.so) + obj = sys.modules[names[0]] + except KeyError: + return 1 + for i in range(1, len(names)) : + obj = getattr(obj, names[i], None) + if obj is None: + return 0 + if functionsCalled.has_key(string.join(names[i:], '.')) : + return 1 + + return 0 + +def _checkBaseClassInit(moduleFilename, c, func_code, funcInfo) : + """Return a list of warnings that occur + for each base class whose __init__() is not called""" + + warnings = [] + functionsCalled, _, returnValues = funcInfo + for line, stackItem, dummy in returnValues : + if stackItem.data != None : + if not stackItem.isNone() or cfg().returnNoneFromInit : + warn = Warning(func_code, line, msgs.RETURN_FROM_INIT) + warnings.append(warn) + + classInit = getattr(c.classObject, utils.INIT, None) + if cfg().baseClassInitted and classInit is not None : + classInitInfo = _get_func_info(classInit) + for base in getattr(c.classObject, '__bases__', None) or (): + if not _baseInitCalled(classInitInfo, base, functionsCalled): + warn = Warning(moduleFilename, func_code, + msgs.BASE_CLASS_NOT_INIT % utils.safestr(base)) + warnings.append(warn) + return warnings + + +def _checkOverridenMethods(func, baseClasses, warnings) : + for baseClass in baseClasses : + if func.func_name != utils.INIT and \ + not function.same_signature(func, baseClass) : + err = msgs.METHOD_SIGNATURE_MISMATCH % (func.func_name, utils.safestr(baseClass)) + warnings.append(Warning(func.func_code, func.func_code, err)) + break + + +def _updateFunctionWarnings(module, func, c, warnings, globalRefs, + main = 0, in_class = 0) : + "Update function warnings and global references" + + newWarnings, newGlobalRefs, funcs, codeObjects, returnValues = \ + _checkFunction(module, func, c, main, in_class) + warnings.extend(newWarnings) + globalRefs.update(newGlobalRefs) + + return funcs, codeObjects, returnValues + + +def getBlackList(moduleList) : + blacklist = [] + for badBoy in moduleList : + if badBoy[-3:] == ".py": + badBoy = badBoy[0:-3] + try : + file, path, flags = imp.find_module(badBoy) + if file : + file.close() + # apparently, imp.find_module can return None, path, (triple) + # This happened to me with twisted 2.2.0 in a separate path + if path: + blacklist.append(normalize_path(path)) + except ImportError : + pass + return blacklist + +def getStandardLibraries() : + """ + Return a list of standard libraries. + + @rtype: list of str or None + """ + if cfg().ignoreStandardLibrary : + try : + from distutils import sysconfig + + std_libs = [ + sysconfig.get_python_lib(plat_specific=0), + sysconfig.get_python_lib(plat_specific=1) + ] + ret = [] + for std_lib in std_libs: + path = os.path.split(std_lib) + if path[1] == 'site-packages' : + ret.append(path[0]) + return ret + except ImportError : + return None + +def normalize_path(path): + return os.path.normpath(os.path.normcase(path)) + +def removeWarnings(warnings, blacklist, std_lib, cfg): + """ + @param blacklist: list of absolute paths not to warn for + @type blacklist: str + @param std_lib: list of standard library directories + @type std_lib: list of str or None + """ + if std_lib is not None: + std_lib = [normalize_path(p) for p in std_lib] + for index in range(len(warnings)-1, -1, -1) : + filename = normalize_path(warnings[index].file) + # the blacklist contains paths to packages and modules we do not + # want warnings for + # when we find a match, make sure we continue the warnings for loop + found = False + for path in blacklist: + if not found and filename.startswith(path): + found = True + del warnings[index] + if found: + continue + if std_lib: + found = False + for path in std_lib: + if not found and utils.startswith(filename, path) : + found = True + del warnings[index] + if found: + continue + elif cfg.only: + # ignore files not specified on the cmd line if requested + if os.path.abspath(filename) not in cfg.files: + del warnings[index] + continue + + # filter by warning/error level if requested + if cfg.level and warnings[index].level < cfg.level: + del warnings[index] + + if cfg.limit: + # sort by severity first, then normal sort (by file/line) + warnings.sort(lambda a, b: cmp(a.level, b.level) or cmp(a, b)) + + # strip duplicates + lastWarning = None + for index in range(len(warnings)-1, -1, -1): + warning = warnings[index] + + # remove duplicate warnings + if lastWarning is not None and cmp(lastWarning, warning) == 0: + del warnings[index] + else: + lastWarning = warning + + num_ignored = len(warnings) - cfg.limit + if num_ignored > 0: + del warnings[:-cfg.limit] + msg = msgs.TOO_MANY_WARNINGS % num_ignored + warnings.append(Warning('', 0, msg)) + + return warnings + + +class _SuppressionError(Exception) : + pass + +def _updateSuppressions(suppress, warnings) : + if not utils.updateCheckerArgs(suppress, 'suppressions', 0, warnings) : + utils.popConfig() + raise _SuppressionError + +_CLASS_NAME_RE = re.compile("(\\..+)?") + +def getSuppression(name, suppressions, warnings) : + try : + utils.pushConfig() + + # cheesy hack to deal with new-style classes. i don't see a + # better way to get the name, '<' is an invalid identifier, so + # we can reliably check it and extract name from: + # [.identifier[.identifier]...] + matches = _CLASS_NAME_RE.match(name) + if matches: + # pull out the names and make a complete identifier (ignore None) + name = string.join(filter(None, matches.groups()), '') + + suppress = suppressions[0].get(name, None) + if suppress is not None : + _updateSuppressions(suppress, warnings) + + regexList = suppressions[1].keys() + regexList.sort() + for regex in regexList : + match = regex.match(name) + if match and match.group() == name : + suppress = 1 + _updateSuppressions(suppressions[1][regex], warnings) + + if not suppress : + utils.popConfig() + + return suppress + except _SuppressionError : + return None + +def _findFunctionWarnings(module, globalRefs, warnings, suppressions) : + for func in module.functions.values() : + func_code = func.function.func_code + utils.debug("function:", func_code) + + name = '%s.%s' % (module.moduleName, func.function.__name__) + suppress = getSuppression(name, suppressions, warnings) + if cfg().noDocFunc and func.function.__doc__ == None : + err = msgs.NO_FUNC_DOC % func.function.__name__ + warnings.append(Warning(module.filename(), func_code, err)) + + _checkNoSelfArg(func, warnings) + _updateFunctionWarnings(module, func, None, warnings, globalRefs) + if suppress is not None : + utils.popConfig() + +def _getModuleFromFilename(module, filename): + if module.filename() != filename: + for m in module.modules.values(): + if m.filename() == filename: + return m + return module + +# Create object for non-2.2 interpreters, any class object will do +try: + if object: pass +except NameError: + object = _SuppressionError + +# Create property for pre-2.2 interpreters +try : + if property: pass +except NameError: + property = None + +def _findClassWarnings(module, c, class_code, + globalRefs, warnings, suppressions) : + try: + className = utils.safestr(c.classObject) + except TypeError: + # goofy __getattr__ + return + classSuppress = getSuppression(className, suppressions, warnings) + baseClasses = c.allBaseClasses() + for base in baseClasses : + baseModule = utils.safestr(base) + if '.' in baseModule : + # make sure we handle import x.y.z + packages = string.split(baseModule, '.') + baseModuleDir = string.join(packages[:-1], '.') + globalRefs[baseModuleDir] = baseModule + + # handle class variables + if class_code is not None : + func = function.create_fake(c.name, class_code) + _updateFunctionWarnings(module, func, c, warnings, globalRefs, 0, 1) + + filename = module.filename() + func_code = None + for method in c.methods.values() : + if method == None : + continue + func_code = method.function.func_code + utils.debug("method:", func_code) + + try: + name = utils.safestr(c.classObject) + '.' + method.function.func_name + except AttributeError: + # func_name may not exist + continue + methodSuppress = getSuppression(name, suppressions, warnings) + + if cfg().checkSpecialMethods: + funcname = method.function.func_name + if funcname[:2] == '__' == funcname[-2:] and \ + funcname != '__init__': + err = None + argCount = python.SPECIAL_METHODS.get(funcname, -1) + if argCount != -1: + # if the args are None, it can be any # of args + if argCount is not None: + minArgs = maxArgs = argCount + err = CodeChecks.getFunctionArgErr(funcname, + func_code.co_argcount, minArgs, maxArgs) + else: + err = msgs.NOT_SPECIAL_METHOD % funcname + + if err is not None: + warnings.append(Warning(filename, func_code, err)) + + if cfg().checkOverridenMethods : + _checkOverridenMethods(method.function, baseClasses, warnings) + + if cfg().noDocFunc and method.function.__doc__ == None : + err = msgs.NO_FUNC_DOC % method.function.__name__ + warnings.append(Warning(filename, func_code, err)) + + _checkSelfArg(method, warnings) + tmpModule = _getModuleFromFilename(module, func_code.co_filename) + funcInfo = _updateFunctionWarnings(tmpModule, method, c, warnings, globalRefs) + if func_code.co_name == utils.INIT : + if utils.INIT in dir(c.classObject) : + warns = _checkBaseClassInit(filename, c, func_code, funcInfo) + warnings.extend(warns) + elif cfg().initDefinedInSubclass : + err = msgs.NO_INIT_IN_SUBCLASS % c.name + warnings.append(Warning(filename, c.getFirstLine(), err)) + if methodSuppress is not None : + utils.popConfig() + + if c.memberRefs and cfg().membersUsed : + memberList = c.memberRefs.keys() + memberList.sort() + err = msgs.UNUSED_MEMBERS % (string.join(memberList, ', '), c.name) + warnings.append(Warning(filename, c.getFirstLine(), err)) + + try: + newStyleClass = issubclass(c.classObject, object) + except TypeError: + # FIXME: perhaps this should warn b/c it may be a class??? + newStyleClass = 0 + + slots = c.statics.get('__slots__') + if slots is not None and cfg().slots: + lineNum = c.lineNums['__slots__'] + if not newStyleClass: + err = msgs.USING_SLOTS_IN_CLASSIC_CLASS % c.name + warnings.append(Warning(filename, lineNum, err)) + elif cfg().emptySlots: + try: + if len(slots.data) == 0: + err = msgs.EMPTY_SLOTS % c.name + warnings.append(Warning(filename, lineNum, err)) + except AttributeError: + # happens when slots is an instance of a class w/o __len__ + pass + + if not newStyleClass and property is not None and cfg().classicProperties: + for static in c.statics.keys(): + if type(getattr(c.classObject, static, None)) == property: + err = msgs.USING_PROPERTIES_IN_CLASSIC_CLASS % (static, c.name) + warnings.append(Warning(filename, c.lineNums[static], err)) + + coerceMethod = c.methods.get('__coerce__') + if newStyleClass and coerceMethod: + lineNum = coerceMethod.function.func_code.co_firstlineno + err = msgs.USING_COERCE_IN_NEW_CLASS % c.name + warnings.append(Warning(filename, lineNum, err)) + + for newClassMethodName in python.NEW_STYLE_CLASS_METHODS: + newClassMethod = c.methods.get(newClassMethodName) + if not newStyleClass and newClassMethod: + lineNum = newClassMethod.function.func_code.co_firstlineno + err = msgs.USING_NEW_STYLE_METHOD_IN_OLD_CLASS % (newClassMethodName, c.name) + warnings.append(Warning(filename, lineNum, err)) + + if cfg().noDocClass and c.classObject.__doc__ == None : + method = c.methods.get(utils.INIT, None) + if method != None : + func_code = method.function.func_code + # FIXME: check to make sure this is in our file, + # not a base class file??? + err = msgs.NO_CLASS_DOC % c.classObject.__name__ + warnings.append(Warning(filename, func_code, err)) + + # we have to do this here, b/c checkFunction doesn't popConfig for classes + # this allows us to have __pychecker__ apply to all methods + # when defined at class scope + if class_code is not None : + utils.popConfig() + + if classSuppress is not None : + utils.popConfig() + + +def find(moduleList, initialCfg, suppressions = None) : + "Return a list of warnings found in the module list" + + if suppressions is None : + suppressions = {}, {} + + utils.initConfig(initialCfg) + + warnings = [] + for module in moduleList : + if module.moduleName in cfg().blacklist : + continue + + modSuppress = getSuppression(module.moduleName, suppressions, warnings) + globalRefs, classCodes = {}, {} + + # main_code can be null if there was a syntax error + if module.main_code != None : + funcInfo = _updateFunctionWarnings(module, module.main_code, + None, warnings, globalRefs, 1) + for code in funcInfo[1] : + classCodes[code.co_name] = code + + _findFunctionWarnings(module, globalRefs, warnings, suppressions) + + for c in module.classes.values() : + _findClassWarnings(module, c, classCodes.get(c.name), + globalRefs, warnings, suppressions) + + if cfg().noDocModule and \ + module.module != None and module.module.__doc__ == None : + warnings.append(Warning(module.filename(), 1, msgs.NO_MODULE_DOC)) + + if cfg().allVariablesUsed or cfg().privateVariableUsed : + prefix = None + if not cfg().allVariablesUsed : + prefix = "_" + for ignoreVar in cfg().variablesToIgnore + cfg().unusedNames : + globalRefs[ignoreVar] = ignoreVar + warnings.extend(_getUnused(module, globalRefs, module.variables, + msgs.VAR_NOT_USED, prefix)) + if cfg().importUsed : + if module.moduleName != utils.INIT or cfg().packageImportUsed : + # always ignore readline module, if [raw_]input() is used + if globalRefs.has_key('input') or \ + globalRefs.has_key('raw_input'): + globalRefs['readline'] = 0 + warnings.extend(_getUnused(module, globalRefs, module.modules, + msgs.IMPORT_NOT_USED)) + + if module.main_code != None : + utils.popConfig() + if modSuppress is not None : + utils.popConfig() + + std_lib = None + if cfg().ignoreStandardLibrary : + std_lib = getStandardLibraries() + return removeWarnings(warnings, getBlackList(cfg().blacklist), std_lib, + cfg()) + +if 0: + # if you want to test w/psyco, include this + import psyco + psyco.bind(_checkCode) http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/Check.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/Check.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/Check.py new file mode 100644 index 0000000..0cc5776 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/Check.py @@ -0,0 +1,96 @@ + +from pychecker2.Warning import Warning +from pychecker2.File import File +from pychecker2 import Options + +import time +import os +import stat + +class WarningOpt(Options.BoolOpt): + __pychecker__ = 'no-callinit' + + def __init__(self, longName, warning): + self.warning = warning + self.longName = longName + self.default = warning.value + + def set_value(self, unused): + self.warning.value = not self.warning.value + + def get_value(self): + return self.warning.value + + def get_description(self): + return self.warning.description + + def reset(self): + self.warning.value = self.default + +class CheckList: + + def __init__(self, checks): + self.checks = checks + self.modules = {} + + def check_file(self, f): + for c in self.checks: + c.check(f, self) + + def check_module(self, m): + f = None + try: + f = self.modules[m] + except KeyError: + import inspect + try: + fname = inspect.getsourcefile(m) + if fname: + f = File(fname) + except TypeError: + pass + self.modules[m] = f + if f: + self.check_file(f) + return f + + def __getstate__(self): + modules = [] + for k, v in self.modules.items(): + modules.append( (k.__name__, v) ) + return (time.time(), self.checks, modules) + + def __setstate__(self, data): + import inspect + cache_time, self.checks, modules = data + self.modules = {} + for k, v in modules: + module = __import__(k, globals(), {}, ['']) + # Don't recover files that are newer than the save time + try: + fname = inspect.getsourcefile(module) + last_modified = os.stat(fname)[stat.ST_MTIME] + except TypeError: + last_modified = 0 + + if last_modified < cache_time: + self.modules[module] = v + +class Check: + + def __str__(self): + return self.__class__.__name__ + + def get_warnings(self, options): + for attr in vars(self.__class__): + object = getattr(self, attr) + if isinstance(object, Warning): + options.add(WarningOpt(attr, object)) + + def get_options(self, options): + pass + + def check(self, file, checker): + pass + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ClassChecks.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ClassChecks.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ClassChecks.py new file mode 100644 index 0000000..4870094 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ClassChecks.py @@ -0,0 +1,364 @@ +from pychecker2.Check import Check +from pychecker2.Check import Warning +from pychecker2 import symbols +from pychecker2.util import * + +from compiler.misc import mangle +from compiler import ast, walk + +_ignorable = {} +for ignore in ['repr', 'dict', 'class', 'doc', 'str']: + _ignorable['__%s__' % ignore] = 1 + +class GetDefs(BaseVisitor): + "Record definitions of a attribute of self, who's name is provided" + def __init__(self, name): + self.selfname = name + self.result = {} + + def visitAssAttr(self, node): + if isinstance(node.expr, ast.Name) and \ + node.expr.name == self.selfname and \ + isinstance(node.parent, (ast.Assign, ast.AssTuple)): + self.result[node.attrname] = node + + def visitClass(self, node): # ignore nested classes + pass + +class GetRefs(BaseVisitor): + "Record references to a attribute of self, who's name is provided" + def __init__(self, name): + self.selfname = name + self.result = {} + + def visitAssAttr(self, node): + if isinstance(node.expr, ast.Name) and \ + node.expr.name == self.selfname and \ + not isinstance(node.parent, (ast.Assign, ast.AssTuple)): + self.result[node.attrname] = node + self.visitChildren(node) + + def visitGetattr(self, node): + if isinstance(node.expr, ast.Name) and \ + node.expr.name == self.selfname: + self.result[node.attrname] = node + self.visitChildren(node) + + def visitClass(self, node): # ignore nested classes + pass + + +def _get_methods(class_scope): + return type_filter(class_scope.get_children(), symbols.FunctionScope) + +class NotSimpleName(Exception): pass + +# compress Getattr(Getattr(Name(x), y), z) -> "x.y.z" +def get_name(node): + if isinstance(node, ast.Getattr): + return get_name(node.expr) + (node.attrname, ) + elif isinstance(node, ast.Name): + return (node.name,) + else: + raise NotSimpleName + +def get_base_names(scope): + names = [] + for b in scope.node.bases: + try: + names.append(get_name(b)) + except NotSimpleName: # FIXME: hiding expressions + pass + return names + +def find_in_module(package, remotename, names, checker): + # No other names, must be a name from the module + if not names: + f = checker.check_module(package) + if f: + return find_scope_going_down(f.root_scope, [remotename], checker) + return None + + # complex name lookup + try: + # first, get the real name of the package + name = package.__name__ + module = __import__(name, globals(), {}, ['']) + except AttributeError: + # ok, so its a fake module... go with that + module = package + if remotename: + name += "." + remotename + # now import it, and chase down any other modules + submodule = getattr(module, names[0], None) + if type(submodule) == type(symbols): + return find_in_module(submodule, None, names[1:], checker) + + # object in the module is not another module, so chase down the source + f = checker.check_module(submodule) + if f: + return find_scope_going_down(f.root_scope, names, checker) + return None + +def find_scope_going_down(scope, names, checker): + "Drill down scopes to find definition of x.y.z" + for c in scope.get_children(): + if getattr(c, 'name', '') == names[0]: + if len(names) == 1: + return c + return find_scope_going_down(c, names[1:], checker) + # Not defined here, check for import + return find_imported_class(scope.imports, names, checker) + +def find_imported_class(imports, names, checker): + # may be defined by import + for i in range(1, len(names) + 1): + # try x, then x.y, then x.y.z as imported names + try: + name = ".".join(names[:i]) + ref = imports[name] + # now look for the rest of the name + result = find_in_module(ref.module, ref.remotename, names[i:], checker) + if result: + return result + except (KeyError, ImportError): + pass + return None + +def find_scope_going_up(scope, names, checker): + "Search up to find scope defining x of x.y.z" + for p in parents(scope): + if p.defs.has_key(names[0]): + return find_scope_going_down(p, names, checker) + # name imported via 'from module import *' + try: + return find_in_module(p.imports[names[0]].module, None, names, checker) + except KeyError: + return None + +def get_base_classes(scope, checker): + result = [] + for name in get_base_names(scope): + base = find_scope_going_up(scope, name, checker) + if base: + result.append(base) + result.extend(get_base_classes(base, checker)) + return result + +def conformsTo(a, b): + alen = len(a.node.argnames) + blen = len(b.node.argnames) + # f(a, *args, **kw) conforms to f(a, b, *args, **kw) + # f(a, *args) conforms to f(a, b, *args) + # f(a, *args) conforms to f(a, b, c) + # f(a, b, c, *args) does not conform to f(a, b) + if alen == blen: + if a.node.kwargs == b.node.kwargs and a.node.varargs == b.node.varargs: + return 1 + if a.node.varargs and alen - 1 <= blen: + return a.node.kwargs == b.node.kwargs + return None + +class AttributeCheck(Check): + "check `self.attr' expressions for attr" + + unknownAttribute = Warning('Report unknown object attributes in methods', + 'Class %s has no attribute %s') + unusedAttribute = Warning('Report attributes unused in methods', + 'Attribute %s is not used in class %s') + methodRedefined = Warning('Report the redefinition of class methods', + 'Method %s in class %s redefined') + signatureChanged = Warning('Report methods whose signatures do not ' + 'match base class methods', + 'Signature does not match method ' + '%s in base class %s') + attributeInitialized = \ + Warning('Report attributes not initialized in __init__', + 'Attribute %s is not initialized in __init__') + + def check(self, file, checker): + def visit_with_self(Visitor, method): + if not method.node.argnames: + return {} + return walk(method.node, Visitor(method.node.argnames[0])).result + + # for all class scopes + for node, scope in file.class_scopes(): + init_attributes = None # attributes initilized in __init__ + attributes = {} # "self.foo = " kinda things + methods = {} # methods -> scopes + + # get attributes defined on self + for m in _get_methods(scope): + defs = visit_with_self(GetDefs, m) + if m.name == '__init__': + init_attributes = defs + attributes.update(defs) + methods[mangle(m.name, scope.name)] = m + + # complain about attributes not initialized in __init__ + if init_attributes is not None: + for name, node in dict_minus(attributes, init_attributes).items(): + file.warning(node, self.attributeInitialized, name) + + # collect inherited gunk: methods and attributes + # check for non-conformant methods + inherited_methods = scope.defs.copy() + inherited_attributes = attributes.copy() + for base in get_base_classes(scope, checker): + for m in _get_methods(base): + inherited_attributes.update(visit_with_self(GetDefs, m)) + mname = mangle(m.name, base.name) + if m.name != "__init__" and \ + methods.has_key(mname) and \ + not conformsTo(methods[mname], m): + file.warning(methods[mname].node, + self.signatureChanged, m.name, base.name) + else: + methods[mname] = m + inherited_methods.update(base.defs) + + # complain about attributes with the same name as methods + both = dict_intersect(attributes, inherited_methods) + for name, node in both.items(): + file.warning(node, self.methodRedefined, name, scope.name) + + # find refs on self + refs = {} + for m in _get_methods(scope): + refs.update(visit_with_self(GetRefs, m)) + + # Now complain about refs on self that aren't known + unknown = dict_minus(refs, inherited_methods) + unknown = dict_minus(unknown, _ignorable) + unknown = dict_minus(unknown, scope.defs) + unknown = dict_minus(unknown, inherited_attributes) + for name, node in unknown.items(): + file.warning(node, self.unknownAttribute, scope.name, name) + + unused = dict_minus(attributes, refs) + for name, node in unused.items(): + if name.startswith('__'): + file.warning(node, self.unusedAttribute, name, scope.name) + +class GetReturns(BaseVisitor): + + def __init__(self): + self.result = [] + + def visitReturn(self, node): + self.result.append(node) + + def visitFunction(self, node): pass + visitClass = visitFunction + +class InitCheck(Check): + + initReturnsValue = Warning('Report value returned from __init__', + 'Method __init__ should not return a value') + + def check(self, file, unused_checker): + + for node, scope in file.class_scopes(): + for m in _get_methods(scope): + if m.name == '__init__': + for r in walk(m.node.code, GetReturns()).result: + if isinstance(r.value, ast.Const) and \ + r.value.value is None: + continue + if isinstance(r.value, ast.Name) and \ + r.value.name == 'None': + continue + file.warning(r, self.initReturnsValue) + + + +special = { + '__cmp__': 2, '__del__': 1, '__delitem__': 2, '__eq__': 2, + '__ge__': 2, '__getitem__': 2, '__gt__': 2, '__hash__': 1, + '__le__': 2, '__len__': 1, '__lt__': 2, '__ne__': 2, + '__nonzero__': 1, '__repr__': 1, '__setitem__': 3, '__str__': 1, + '__getattr__': 2, '__setattr__': 3, + '__delattr__': 2, '__len__': 1, '__delitem__': 2, '__iter__': 1, + '__contains__': 2,'__setslice__': 4,'__delslice__': 3, + '__add__': 2, '__sub__': 2, '__mul__': 2, '__floordiv__': 2, + '__mod__': 2, '__divmod__': 2, '__lshift__': 2, + '__rshift__': 2, '__and__': 2, '__xor__': 2, '__or__': 2, + '__div__': 2, '__truediv__': 2, '__radd__': 2, '__rsub__': 2, + '__rmul__': 2, '__rdiv__': 2, '__rmod__': 2, '__rdivmod__': 2, + '__rpow__': 2, '__rlshift__': 2, '__rrshift__': 2, '__rand__': 2, + '__rxor__': 2, '__ror__': 2, '__iadd__': 2, '__isub__': 2, + '__imul__': 2, '__idiv__': 2, '__imod__': 2, '__ilshift__': 2, + '__irshift__': 2, '__iand__': 2, '__ixor__': 2, '__ior__': 2, + '__neg__': 1, '__pos__': 1, '__abs__': 1, '__invert__': 1, + '__complex__': 1, '__int__': 1, '__long__': 1, '__float__': 1, + '__oct__': 1, '__hex__': 1, '__coerce__': 2, + '__new__': None, + '__getinitargs__': 1, '__reduce__': 1, + '__getstate__': 1,'__setstate__': 2, + '__copy__': 1, '__deepcopy__': 1, + '__pow__': 2, '__ipow__': 2, # 2 or 3 + '__call__': None, # any number > 1 + '__getslice__': 3, # deprecated + '__getattribute__': 2, + } + +def check_special(scope): + try: + count = special[scope.name] + max_args = len(scope.node.argnames) + min_args = max_args - len(scope.node.defaults) + if min_args > count or max_args < count or \ + scope.node.varargs or scope.node.kwargs: + return special[scope.name] + except KeyError: + pass + return None + +class SpecialCheck(Check): + + specialMethod = Warning('Report special methods with incorrect ' + 'number of arguments', + 'The %s method requires %d argument%s, ' + 'including self') + + notSpecial = Warning('Report methods with "__" prefix and suffix ' + 'which are not defined as special methods', + 'The method %s is not a special method, ' + 'but is reserved.') + + def check(self, file, unused_checker): + + for node, scope in file.class_scopes(): + for m in _get_methods(scope): + n = check_special(m) + if n: + file.warning(m.node, self.specialMethod, m.name, n, + n > 1 and "s" or "") + name = m.name + if name.startswith('__') and name.endswith('__') and \ + name != '__init__' and not special.has_key(name): + file.warning(m.node, self.notSpecial, name) + +class BackQuote(BaseVisitor): + + def __init__(self, selfname): + self.results = [] + self.selfname = selfname + + def visitBackquote(self, node): + if isinstance(node.expr, ast.Name) and node.expr.name == self.selfname: + self.results.append(node) + +class ReprCheck(Check): + + backquoteSelf = Warning('Report use of `self` in __repr__ methods', + 'Using `self` in __repr__') + def check(self, file, unused_checker): + for node, scope in file.class_scopes(): + for m in _get_methods(scope): + if m.name == '__repr__' and m.node.argnames: + visitor = BackQuote(m.node.argnames[0]) + for n in walk(m.node.code, visitor).results: + file.warning(n, self.backquoteSelf) + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ConditionalChecks.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ConditionalChecks.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ConditionalChecks.py new file mode 100644 index 0000000..0271dfd --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/ConditionalChecks.py @@ -0,0 +1,44 @@ +from pychecker2.Check import Check +from pychecker2.util import BaseVisitor + +from compiler import ast, walk + +class ConstantFinder(BaseVisitor): + + def __init__(self): + self.result = [] + + def visitConst(self, node): + if isinstance(node.parent, (ast.Or, ast.Not, ast.And)): + self.result.append((node, `node.value`)) + + def visitName(self, node): + if node.name == 'None': + if isinstance(node.parent, (ast.Or, ast.Not, ast.And)): + self.result.append((node, 'None')) + +class GetConditionalConstants(BaseVisitor): + + def __init__(self): + self.result = [] + + def visitIf(self, node): + for test, code in node.tests: + self.result.extend(walk(test, ConstantFinder()).result) + + def visitWhile(self, node): + self.result.extend(walk(node.test, ConstantFinder()).result) + visitListCompIf = visitWhile + visitAssert = visitWhile + +class ConstantCheck(Check): + + constantInConditional = \ + Warning('Report constants used in conditionals', + 'Constant used in conditional %s') + + def check(self, file, unused_checker): + if file.parseTree: + v = GetConditionalConstants() + for n, value in walk(file.parseTree, v).result: + file.warning(n, self.constantInConditional, value) http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/80e25b46/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/File.py ---------------------------------------------------------------------- diff --git a/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/File.py b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/File.py new file mode 100644 index 0000000..d277d31 --- /dev/null +++ b/tools/bin/pythonSrc/pychecker-0.8.18/pychecker2/File.py @@ -0,0 +1,39 @@ +from pychecker2.util import parents + +from compiler import ast + +class File: + def __init__(self, name): + self.name = name + self.parseTree = None + self.scopes = {} + self.root_scope = None + self.warnings = [] + + def __cmp__(self, other): + return cmp(self.name, other.name) + + def warning(self, line, warn, *args): + lineno = getattr(line, 'lineno', line) + if not lineno and hasattr(line, 'parent'): + for p in parents(line): + if p.lineno: + lineno = p.lineno + break + self.warnings.append( (lineno, warn, args) ) + + def scope_filter(self, type): + return [x for x in self.scopes.iteritems() if isinstance(x[0], type)] + + def function_scopes(self): + return self.scope_filter(ast.Function) + + def class_scopes(self): + return self.scope_filter(ast.Class) + + def not_class_scopes(self): + result = [] + for n, s in self.scopes.iteritems(): + if not isinstance(n, ast.Class): + result.append( (n, s) ) + return result