def subprocess_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' class Popen(object): returncode = pid = 0 stdin = stdout = stderr = file() def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0): pass def communicate(self, input=None): return ('string', 'string') def wait(self): return self.returncode def poll(self): return self.returncode def send_signal(self, signal): pass def terminate(self): pass def kill(self): pass ''') for func_name, func in fake.locals.items(): module.locals[func_name] = func
def hashlib_transform(module): if module.name == 'hashlib': fake = ASTNGBuilder(MANAGER).string_build(''' class fakehash(object): digest_size = -1 def __init__(self, value): pass def digest(self): return u'' def hexdigest(self): return u'' def update(self, value): pass class md5(fakehash): pass class sha1(fakehash): pass class sha256(fakehash): pass ''') for hashfunc in ('sha256', 'sha1', 'md5'): module.locals[hashfunc] = fake.locals[hashfunc]
def collections_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' class defaultdict(dict): default_factory = None def __missing__(self, key): pass class deque(object): maxlen = 0 def __init__(iterable=None, maxlen=None): pass def append(self, x): pass def appendleft(self, x): pass def clear(self): pass def count(self, x): return 0 def extend(self, iterable): pass def extendleft(self, iterable): pass def pop(self): pass def popleft(self): pass def remove(self, value): pass def reverse(self): pass def rotate(self, n): pass ''') for klass in ('deque', 'defaultdict'): module.locals[klass] = fake.locals[klass]
def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" if modname in self.astng_cache: return self.astng_cache[modname] if modname == '__main__': from logilab.astng.builder import ASTNGBuilder return ASTNGBuilder(self).string_build('', modname) old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): module = self.zip_import_data(filepath) if module is not None: return module if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) except Exception as ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg) return self.astng_from_module(module, modname) return self.astng_from_file(filepath, modname, fallback=False) finally: os.chdir(old_cwd)
def astng_from_file(self, filepath, modname=None, fallback=True, source=False): """given a module name, return the astng object""" try: filepath = get_source_file(filepath, include_no_ext=True) source = True except NoSourceFile: pass if modname is None: try: modname = '.'.join(modpath_from_file(filepath)) except ImportError: modname = filepath if modname in self.astng_cache: return self.astng_cache[modname] if source: from logilab.astng.builder import ASTNGBuilder return ASTNGBuilder(self).file_build(filepath, modname) elif fallback and modname: return self.astng_from_module_name(modname) raise ASTNGBuildingException('unable to get astng for file %s' % filepath)
def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" if modname in self._cache: return self._cache[modname] old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): data, zmodname = zip_import_data(filepath) if data is not None: from logilab.astng.builder import ASTNGBuilder try: return ASTNGBuilder(self).string_build( data, zmodname, filepath) except (SyntaxError, KeyboardInterrupt, SystemExit): raise if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) # catch SystemError as well, we may get that on badly # initialized C-module except (SystemError, ImportError), ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg) return self.astng_from_module(module, modname) return self.astng_from_file(filepath, modname, fallback=False)
def test_numpy_crash(self): try: import numpy except ImportError: self.skipTest('test skipped: numpy is not available') else: builder = ASTNGBuilder() data = """ from numpy import multiply multiply(1, 2, 3) """ astng = builder.string_build(data, __name__, __file__) callfunc = astng.node.nodes[1].expr # well, InferenceError instead of a crash is better self.assertRaises(InferenceError, list, callfunc.infer())
def astng_from_file(self, filepath, modname=None, fallback=True): """given a module name, return the astng object""" try: filepath = get_source_file(filepath, include_no_ext=True) source = True except NoSourceFile: source = False if modname is None: modname = '.'.join(modpath_from_file(filepath)) if modname in self._cache: return self._cache[modname] if source: try: from logilab.astng.builder import ASTNGBuilder return ASTNGBuilder(self).file_build(filepath, modname) except (SyntaxError, KeyboardInterrupt, SystemExit): raise except Exception, ex: raise if __debug__: print 'error while building astng for', filepath import traceback traceback.print_exc() msg = 'Unable to load module %s (%s)' % (modname, ex) raise ASTNGBuildingException(msg), None, sys.exc_info()[-1]
def urlparse_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' def urlparse(url, scheme='', allow_fragments=True): return ParseResult() class ParseResult(object): def __init__(self): self.scheme = '' self.netloc = '' self.path = '' self.params = '' self.query = '' self.fragment = '' self.username = None self.password = None self.hostname = None self.port = None def geturl(self): return '' ''') for func_name, func in fake.locals.items(): module.locals[func_name] = func
def test_new_style_class_detection(self): try: import pygtk except ImportError: self.skipTest('test skipped: pygtk is not available') # XXX may fail on some pygtk version, because objects in # gobject._gobject have __module__ set to gobject :( builder = ASTNGBuilder() data = """ import pygtk pygtk.require("2.6") import gobject class A(gobject.GObject): pass """ astng = builder.string_build(data, __name__, __file__) a = astng['A'] self.assertTrue(a.newstyle)
def pyqt4_qtcore_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' def SIGNAL(signal_name): pass class QObject(object): def emit(self, signal): pass ''') for klass in ('QObject',): module.locals[klass] = fake.locals[klass]
def test_numpy_crash(self): """test don't crash on numpy""" #a crash occured somewhere in the past, and an # InferenceError instead of a crash was better, but now we even infer! try: import numpy except ImportError: self.skipTest('test skipped: numpy is not available') builder = ASTNGBuilder() data = """ from numpy import multiply multiply(1, 2, 3) """ astng = builder.string_build(data, __name__, __file__) callfunc = astng.body[1].value.func infered = callfunc.infered() self.assertEqual(len(infered), 1) self.assertIsInstance(infered[0], Instance)
def test_new_style_class_detection(self): try: import pygtk except ImportError: self.skipTest('test skipped: pygtk is not available') # XXX may fail on some pygtk version, because objects in # gobject._gobject have __module__ set to gobject :( builder = ASTNGBuilder() data = """ import pygtk pygtk.require("2.6") import gobject class A(gobject.GObject): pass """ astng = builder.string_build(data, __name__, __file__) a = astng['A'] self.failUnless(a.newstyle)
def sh_transform(module): if module.name == 'sh': for name, items in ASTNGBuilder(MANAGER).string_build( FAKE_SH).locals.items(): for item in items: item.parent = module module.locals[name] = items for item in ('ErrorReturnCode_128', ): module.locals[item] = [scoped_nodes.Class(item, None)]
def zip_import_data(self, filepath): if zipimport is None: return None from logilab.astng.builder import ASTNGBuilder builder = ASTNGBuilder(self) for ext in ('.zip', '.egg'): try: eggpath, resource = filepath.rsplit(ext + '/', 1) except ValueError: continue try: importer = zipimport.zipimporter(eggpath + ext) zmodname = resource.replace('/', '.') if importer.is_package(resource): zmodname = zmodname + '.__init__' module = builder.string_build(importer.get_source(resource), zmodname, filepath) return module except: continue return None
def mechanize_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' class Browser(object): def open(self, url, data=None, timeout=None): return None def open_novisit(self, url, data=None, timeout=None): return None def open_local_file(self, filename): return None ''') module.locals['Browser'] = fake.locals['Browser']
def hashlib_transform(module): if module.name == 'django.utils.translation': fake = ASTNGBuilder(MANAGER).string_build(''' def ugettext_lazy(value): return u'' def ugettext(value): return u'' ''') for hashfunc in ('ugettext_lazy', 'ugettext'): module.locals[hashfunc] = fake.locals[hashfunc]
def astng_from_module(self, module, modname=None): """given an imported module, return the astng object""" modname = modname or module.__name__ if modname in self._cache: return self._cache[modname] try: # some builtin modules don't have __file__ attribute filepath = module.__file__ if is_python_source(filepath): return self.astng_from_file(filepath, modname) except AttributeError: pass from logilab.astng.builder import ASTNGBuilder return ASTNGBuilder(self).module_build(module, modname)
class Python3TC(TestCase): def setUp(self): self.manager = ASTNGManager() self.builder = ASTNGBuilder(self.manager) self.manager.astng_cache.clear() @require_version('3.0') def test_starred_notation(self): astng = self.builder.string_build("*a, b = [1, 2, 3]", 'test', 'test') # Get the star node node = next(next(next(astng.get_children()).get_children()).get_children()) self.assertTrue(isinstance(node.ass_type(), Assign))
def test_new_style_class_detection(self): try: import pygtk except ImportError: self.skipTest('test skipped: pygtk is not available') else: # XXX may fail on some pygtk version, because objects in # gobject._gobject have __module__ set to gobject :( builder = ASTNGBuilder() data = """ import pygtk pygtk.require("2.6") import gobject class A(gobject.GObject): def __init__(self, val): gobject.GObject.__init__(self) self._val = val def _get_val(self): print "get" return self._val def _set_val(self, val): print "set" self._val = val val = property(_get_val, _set_val) if __name__ == "__main__": print gobject.GObject.__bases__ a = A(7) print a.val a.val = 6 print a.val """ astng = builder.string_build(data, __name__, __file__) a = astng['A'] self.failUnless(a.newstyle)
class Python3TC(TestCase): def setUp(self): self.manager = ASTNGManager() self.builder = ASTNGBuilder(self.manager) self.manager.astng_cache.clear() @require_version('3.0') def test_starred_notation(self): astng = self.builder.string_build("*a, b = [1, 2, 3]", 'test', 'test') # Get the star node node = next( next(next(astng.get_children()).get_children()).get_children()) self.assertTrue(isinstance(node.ass_type(), Assign))
class Python3TC(TestCase): def setUp(self): self.manager = ASTNGManager() self.builder = ASTNGBuilder(self.manager) self.manager.astng_cache.clear() def test_starred_notation(self): if sys.version_info < (3, 0): self.skipTest("test python 3k specific") astng = self.builder.string_build("*a, b = [1, 2, 3]", 'test', 'test') # Get the star node node = next(next(next(astng.get_children()).get_children()).get_children()) self.assertTrue(isinstance(node.ass_type(), Assign))
def hashlib_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' class md5(object): def __init__(self, value): pass def hexdigest(self): return u'' class sha1(object): def __init__(self, value): pass def hexdigest(self): return u'' ''') for hashfunc in ('sha1', 'md5'): module.locals[hashfunc] = fake.locals[hashfunc]
def pkg_resources_transform(module): fake = ASTNGBuilder(MANAGER).string_build(''' def resource_exists(package_or_requirement, resource_name): pass def resource_isdir(package_or_requirement, resource_name): pass def resource_filename(package_or_requirement, resource_name): pass def resource_stream(package_or_requirement, resource_name): pass def resource_string(package_or_requirement, resource_name): pass def resource_listdir(package_or_requirement, resource_name): pass def extraction_error(): pass def get_cache_path(archive_name, names=()): pass def postprocess(tempname, filename): pass def set_extraction_path(path): pass def cleanup_resources(force=False): pass ''') for func_name, func in fake.locals.items(): module.locals[func_name] = func
class LeConst(LanguageElement): """This language element represent a constant element. At this point I think this can only be a builtin. """ from logilab.astng.builder import ASTNGBuilder from logilab.common.compat import builtins BUILTINS = ASTNGBuilder().inspect_build(builtins) KIND = 'b' def bounded_accessibles(self): name = self.astng_element.pytype() if name.startswith('__builtin__'): name = name.split('.')[-1] builtin_astng_element = self.BUILTINS[name] element = LanguageElement.create( builtin_astng_element, name=name, context_string=self.context_string) return element.bounded_accessibles() raise RuntimeError("The const type %s is no builtin." % self.astng_element)
def httpretty_transform(module): if module.name == 'httpretty': fake = ASTNGBuilder(MANAGER).string_build(CODE_FIX) for hashfunc in ('enable', 'disable', 'register_uri', 'GET', 'POST'): module.locals[hashfunc] = fake.locals[hashfunc]
class FakeBuilder(object): def __init__(self, module): self.builder = ASTNGBuilder(MANAGER) self.module = module def add_ormobject(self, orm_type, orm_name): if orm_type in dt.done: return dt.done.add(orm_type) module_name = orm_type.__module__ pymodule = namedAny(module_name) assert pymodule module = MANAGER.astng_from_module(pymodule) assert module class_node = module[orm_name] t = '' t += 'class %s:\n' % (orm_name, ) t += ' q = None\n' t += ' _connection = None\n' orm_ti = dt.orm_classes.get(orm_name) for name in sorted(orm_ti.get_column_names()): t += ' %s = None\n' % (name, ) for name, class_name in sorted(orm_ti.get_foreign_columns()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = None\n' % (class_name, ) t += ' %s = %s()\n' % (name, class_name) for name, class_name in sorted(orm_ti.get_single_joins()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = %s()\n' % (name, class_name) t += '\n' nodes = self.builder.string_build(t) for key, value in nodes[orm_name].items(): class_node.locals[key] = [value] def add_parameter_access(self): name = 'ParameterAccess' t = 'class %s(object):\n' % (name, ) for detail in get_all_details(): t += ' def %s(self): pass\n' % (detail.key, ) nodes = self.builder.string_build(t) self.module.locals[name] = nodes.locals[name] def add_interfaces(self, module): f = namedAny(module.name).__file__.replace('.pyc', '.py') data = open(f).read() data = re.sub(r'(def \S+)\(\)', r'\1(self)', data) data = re.sub(r'(def \S+)\(', r'\1(self, ', data) data = data.replace('self, self', 'self') nodes = self.builder.string_build(data) self.module.locals = nodes.locals def add_wizard_step(self, module): from stoqlib.gui.wizards.purchasewizard import WizardEditorStep pymod = namedAny(module.name) for attr in dir(pymod): value = getattr(pymod, attr) if attr in module and isinstance(module[attr], From): continue if my_issubclass(value, WizardEditorStep): self.add_delegate(value, attr) def add_delegate(self, delegate, attr): from kiwi.environ import environ from kiwi.ui.builderloader import BuilderWidgetTree import gtk if not delegate.gladefile: return f = environ.find_resource('glade', delegate.gladefile + '.ui') tree = BuilderWidgetTree(delegate, f, None) t = '' t += 'import kiwi\n' t += 'class %s(object):\n' % (attr, ) for widget in sorted(tree.get_widgets()): try: name = gtk.Buildable.get_name(widget) except TypeError: continue t += ' %s = %s.%s()\n' % (name, widget.__module__, widget.__class__.__name__) print t real_node = self.module[attr] self.module.body.remove(real_node) print vars(self.module) nodes = self.builder.string_build(t) for key, value in nodes.items(): self.module.locals[key] = [value] self.module.body.append(value) new_node = self.module.locals[attr][0] for key, value in real_node.locals.items(): print key new_node[key] = [value]
def __init__(self, module): self.builder = ASTNGBuilder(MANAGER) self.module = module
class FakeBuilder(object): def __init__(self, module): self.builder = ASTNGBuilder(MANAGER) self.module = module def add_ormobject(self, orm_type, orm_name): if orm_type in dt.done: return dt.done.add(orm_type) module_name = orm_type.__module__ pymodule = namedAny(module_name) assert pymodule module = MANAGER.astng_from_module(pymodule) assert module class_node = module[orm_name] t = '' t += 'class %s:\n' % (orm_name, ) t += ' q = None\n' t += ' _connection = None\n' orm_ti = dt.orm_classes.get(orm_name) for name in sorted(orm_ti.get_column_names()): t += ' %s = None\n' % (name, ) for name, class_name in sorted(orm_ti.get_foreign_columns()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = None\n' % (class_name, ) t += ' %s = %s()\n' % (name, class_name) for name, class_name in sorted(orm_ti.get_single_joins()): self.add_ormobject( dt.orm_classes[class_name].orm_type, class_name) t += ' %s = %s()\n' % (name, class_name) t += '\n' nodes = self.builder.string_build(t) for key, value in nodes[orm_name].items(): class_node.locals[key] = [value] def add_parameter_access(self): name = 'ParameterAccess' t = 'class %s(object):\n' % (name, ) for detail in get_all_details(): t += ' def %s(self): pass\n' % (detail.key, ) nodes = self.builder.string_build(t) self.module.locals[name] = nodes.locals[name] def add_interfaces(self, module): f = namedAny(module.name).__file__.replace('.pyc', '.py') data = open(f).read() data = re.sub(r'(def \S+)\(\)', r'\1(self)', data) data = re.sub(r'(def \S+)\(', r'\1(self, ', data) data = data.replace('self, self', 'self') nodes = self.builder.string_build(data) self.module.locals = nodes.locals def add_wizard_step(self, module): from stoqlib.gui.wizards.purchasewizard import WizardEditorStep pymod = namedAny(module.name) for attr in dir(pymod): value = getattr(pymod, attr) if attr in module and isinstance(module[attr], From): continue if my_issubclass(value, WizardEditorStep): self.add_delegate(value, attr) def add_delegate(self, delegate, attr): from kiwi.environ import environ from kiwi.ui.builderloader import BuilderWidgetTree import gtk if not delegate.gladefile: return f = environ.find_resource('glade', delegate.gladefile + '.ui') tree = BuilderWidgetTree(delegate, f, None) t = '' t += 'import kiwi\n' t += 'class %s(object):\n' % (attr, ) for widget in sorted(tree.get_widgets()): try: name = gtk.Buildable.get_name(widget) except TypeError: continue t += ' %s = %s.%s()\n' % (name, widget.__module__, widget.__class__.__name__) print t real_node = self.module[attr] self.module.body.remove(real_node) print vars(self.module) nodes = self.builder.string_build(t) for key, value in nodes.items(): self.module.locals[key] = [value] self.module.body.append(value) new_node = self.module.locals[attr][0] for key, value in real_node.locals.items(): print key new_node[key] = [value]
def setUp(self): self.manager = ASTNGManager() self.builder = ASTNGBuilder(self.manager) self.manager.astng_cache.clear()
import unittest from pyparsing import alphas, Word, alphanums, Forward, Literal, commaSeparatedList, Group, Suppress, OneOrMore, Optional from logilab.astng.builder import ASTNGBuilder from prambanan.cmd import patch_astng_manager import prambanan.compiler.astng_patch from prambanan.compiler.hint import TypeHintProcessor from prambanan.compiler.manager import PrambananManager manager = PrambananManager([]) processor = TypeHintProcessor() patch_astng_manager(manager) builder = ASTNGBuilder(manager) code = '''''' astng = builder.string_build(code, __name__, __file__) ELEMENT = Forward() EREF = Group(ELEMENT) ID = Word(alphanums+".") TYPE = Group((ID+Suppress(":")+ID | ID)) INSTANCE = ("i("+TYPE+")") CLASS = "c("+TYPE+")" LIST = "l("+EREF+")" DICT ="d("+EREF+","+EREF+")" TUPLE = "t("+OneOrMore(EREF+Suppress(Optional(",")))+")"
def test_living_property(self): builder = ASTNGBuilder() builder._done = {} builder._module = sys.modules[__name__] builder.object_build(build_module('module_name', ''), Whatever)
def gi_repository_transform(module): if module.name == 'gi.repository': fake = ASTNGBuilder(MANAGER).string_build(CODE_FIX) for func in ('Notify', ): module.locals[func] = fake.locals[func]
def build_astng_tree(self): """build astng tree from the _ast tree """ from logilab.astng.builder import ASTNGBuilder tree = ASTNGBuilder().string_build(self.sourcecode) return tree
def hashlib_transform(module): if module.name == 'hashlib': fake = ASTNGBuilder(MANAGER).string_build(CODE_FIX) for hashfunc in ('sha1', 'md5', 'sha512'): module.locals[hashfunc] = fake.locals[hashfunc]
def scapy_transform(module): if module.name == 'scapy.all': fake = ASTNGBuilder(MANAGER).string_build(CODE_FIX) for func in ('IP', 'TCP', 'UDP', 'traceroute'): module.locals[func] = fake.locals[func]