From: Sergio Pascual Date: Thu, 22 May 2014 12:37:05 +0000 (+0200) Subject: Bring an unmodified copy of ply X-Git-Tag: archive/raspbian/2.0.2-2+rpi1~1^2^2^2~3 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=1d522e62527cf5b95e6a0b9a9a6c6d874386ecff;p=python-astropy.git Bring an unmodified copy of ply Gbp-Pq: Name use_extern_ply.patch --- diff --git a/astropy/extern/ply/lex.py b/astropy/extern/ply/lex.py index 5f61c3c..69f01d6 100644 --- a/astropy/extern/ply/lex.py +++ b/astropy/extern/ply/lex.py @@ -34,7 +34,7 @@ __version__ = "3.4" __tabversion__ = "3.2" # Version of table file used -import re, sys, types, copy, os, inspect +import re, sys, types, copy, os # This tuple contains known string types try: @@ -548,7 +548,7 @@ class LexerReflect(object): self.tokens = [] self.reflags = reflags self.stateinfo = { 'INITIAL' : 'inclusive'} - self.modules = {} + self.files = {} self.error = 0 if log is None: @@ -729,8 +729,7 @@ class LexerReflect(object): for fname, f in self.funcsym[state]: line = func_code(f).co_firstlineno file = func_code(f).co_filename - module = inspect.getmodule(f) - self.modules[module] = 1 + self.files[file] = 1 tokname = self.toknames[fname] if isinstance(f, types.MethodType): @@ -800,8 +799,7 @@ class LexerReflect(object): f = efunc line = func_code(f).co_firstlineno file = func_code(f).co_filename - module = inspect.getmodule(f) - self.modules[module] = 1 + self.files[file] = 1 if isinstance(f, types.MethodType): reqargs = 2 @@ -816,26 +814,35 @@ class LexerReflect(object): self.log.error("%s:%d: Rule '%s' requires an argument", file,line,f.__name__) self.error = 1 - for module in self.modules: - self.validate_module(module) + for f in self.files: + self.validate_file(f) # ----------------------------------------------------------------------------- - # validate_module() + # validate_file() # # This checks to see if there are duplicated t_rulename() functions or strings # in the parser input file. This is done using a simple regular expression - # match on each line in the source code of the given module. + # match on each line in the given file. # ----------------------------------------------------------------------------- - def validate_module(self, module): - lines, linen = inspect.getsourcelines(module) + def validate_file(self,filename): + import os.path + base,ext = os.path.splitext(filename) + if ext != '.py': return # No idea what the file is. Return OK + + try: + f = open(filename) + lines = f.readlines() + f.close() + except IOError: + return # Couldn't find the file. Don't worry about it fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(') sre = re.compile(r'\s*(t_[a-zA-Z_0-9]*)\s*=') counthash = { } - linen += 1 + linen = 1 for l in lines: m = fre.match(l) if not m: @@ -846,7 +853,6 @@ class LexerReflect(object): if not prev: counthash[name] = linen else: - filename = inspect.getsourcefile(module) self.log.error("%s:%d: Rule %s redefined. Previously defined on line %d",filename,linen,name,prev) self.error = 1 linen += 1 diff --git a/astropy/extern/ply/yacc.py b/astropy/extern/ply/yacc.py index 1d313db..73daf87 100644 --- a/astropy/extern/ply/yacc.py +++ b/astropy/extern/ply/yacc.py @@ -84,7 +84,7 @@ resultlimit = 40 # Size limit of results when running in debug mod pickle_protocol = 0 # Protocol to use when writing pickle files -import re, types, sys, os.path, inspect +import re, types, sys, os.path # Compatibility function for python 2.6/3.0 if sys.version_info[0] < 3: @@ -1191,7 +1191,7 @@ class Production(object): return len(self.prod) def __nonzero__(self): - return True + return 1 def __getitem__(self,index): return self.prod[index] @@ -2765,7 +2765,7 @@ class ParserReflect(object): self.start = None self.error_func = None self.tokens = None - self.modules = {} + self.files = {} self.grammar = [] self.error = 0 @@ -2789,7 +2789,7 @@ class ParserReflect(object): self.validate_tokens() self.validate_precedence() self.validate_pfunctions() - self.validate_modules() + self.validate_files() return self.error # Compute a signature over the grammar @@ -2814,7 +2814,7 @@ class ParserReflect(object): return sig.digest() # ----------------------------------------------------------------------------- - # validate_modules() + # validate_file() # # This method checks to see if there are duplicated p_rulename() functions # in the parser module file. Without this function, it is really easy for @@ -2824,12 +2824,20 @@ class ParserReflect(object): # to try and detect duplicates. # ----------------------------------------------------------------------------- - def validate_modules(self): + def validate_files(self): # Match def p_funcname( fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(') - for module in self.modules.keys(): - lines, linen = inspect.getsourcelines(module) + for filename in self.files.keys(): + base,ext = os.path.splitext(filename) + if ext != '.py': return 1 # No idea. Assume it's okay. + + try: + f = open(filename) + lines = f.readlines() + f.close() + except IOError: + continue counthash = { } for linen,l in enumerate(lines): @@ -2841,7 +2849,6 @@ class ParserReflect(object): if not prev: counthash[name] = linen else: - filename = inspect.getsourcefile(module) self.log.warning("%s:%d: Function %s redefined. Previously defined on line %d", filename,linen,name,prev) # Get the start symbol @@ -2872,8 +2879,7 @@ class ParserReflect(object): eline = func_code(self.error_func).co_firstlineno efile = func_code(self.error_func).co_filename - module = inspect.getmodule(self.error_func) - self.modules[module] = 1 + self.files[efile] = 1 if (func_code(self.error_func).co_argcount != 1+ismethod): self.log.error("%s:%d: p_error() requires 1 argument",efile,eline) @@ -2956,8 +2962,8 @@ class ParserReflect(object): if name == 'p_error': continue if isinstance(item,(types.FunctionType,types.MethodType)): line = func_code(item).co_firstlineno - module = inspect.getmodule(item) - p_functions.append((line,module,name,item.__doc__)) + file = func_code(item).co_filename + p_functions.append((line,file,name,item.__doc__)) # Sort all of the actions by line number p_functions.sort() @@ -2973,8 +2979,7 @@ class ParserReflect(object): self.error = 1 return - for line, module, name, doc in self.pfuncs: - file = inspect.getsourcefile(module) + for line, file, name, doc in self.pfuncs: func = self.pdict[name] if isinstance(func, types.MethodType): reqargs = 2 @@ -3000,7 +3005,7 @@ class ParserReflect(object): # Looks like a valid grammar rule # Mark the file in which defined. - self.modules[module] = 1 + self.files[file] = 1 # Secondary validation step that looks for p_ definitions that are not functions # or functions that look like they might be grammar rules.