def testImportBuiltInProtorpcClasses(self):
    """Test that built in Protorpc classes are skipped."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                      descriptor.describe_file(descriptor),
    ]

    root = new.module('root')
    nested = new.module('root.nested')
    root.nested = nested
    modules = {
        'root': root,
        'root.nested': nested,
        'protorpc.descriptor': descriptor,
    }

    definition.import_file_set(file_set, modules=modules)

    self.assertEquals(root, modules['root'])
    self.assertEquals(nested, modules['root.nested'])
    self.assertEquals(nested.nested, modules['root.nested.nested'])
    self.assertEquals(descriptor, modules['protorpc.descriptor'])

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                           modules['protorpc.descriptor'],
                          ]))
    def loadscript(self, scriptfile):
        """Execute scriptfile in this sandbox.  Before it is executed, change
        to script directory and shadow pdffit and pdffit2 modules

        scriptfile  --  path to the script to be loaded
        """
        # go to script directory
        orgdir = os.getcwd()
        scriptdir = os.path.dirname(scriptfile) or "."
        scriptbase = os.path.basename(scriptfile)
        os.chdir(scriptdir)
        # shadow pdffit and pdffit2 modules
        savemodules = { }
        for modname in [ 'pdffit', 'pdffit2' ]:
            if modname in sys.modules:
                savemodules[modname] = sys.modules[modname]
        import new
        sys.modules['pdffit'] = new.module('pdffit')
        sys.modules['pdffit2'] = pdffit2 = new.module('pdffit2')
        # shadow PdfFit class
        pdffit2.PdfFit = None
        # execute the file
        execfile(scriptbase, self.sandbox())
        # restore modules
        del sys.modules['pdffit']
        del sys.modules['pdffit2']
        for modname, mod in savemodules.iteritems():
            sys.modules[modname] = mod
        # get to the original directory
        os.chdir(orgdir)
        return
  def testImportFileSet(self):
    """Test importing a whole file set."""
    file_set = descriptor.FileSet()
    file_set.files = [self.MakeFileDescriptor(u'standalone'),
                      self.MakeFileDescriptor(u'root.nested'),
                      self.MakeFileDescriptor(u'root.nested.nested'),
                     ]

    root = new.module('root')
    nested = new.module('root.nested')
    root.nested = nested
    modules = {
        'root': root,
        'root.nested': nested,
    }

    definition.import_file_set(file_set, modules=modules)

    self.assertEquals(root, modules['root'])
    self.assertEquals(nested, modules['root.nested'])
    self.assertEquals(nested.nested, modules['root.nested.nested'])

    self.assertEquals(file_set,
                      descriptor.describe_file_set(
                          [modules['standalone'],
                           modules['root.nested'],
                           modules['root.nested.nested'],
                          ]))
示例#4
0
文件: core.py 项目: LLNL/WVL
	def remote_supply_code(self, name, module, sourceaddr):
		if Pyro.config.PYRO_MOBILE_CODE and self.codeValidator(name,module,sourceaddr):
			import imp,marshal,new
			Log.msg('ObjBase','loading supplied code: ',name,'from',str(sourceaddr))
			name=name.split('.')
			# make the module hierarchy and add all names to sys.modules
			path=''
			mod=new.module("pyro-agent-context")

			for m in name:
				path+='.'+m
				# use already loaded modules instead of overwriting them
				real_path = path[1:]
				if sys.modules.has_key(real_path):
					mod = sys.modules[real_path]
				else:
					setattr(mod,m,new.module(path[1:]))
					mod=getattr(mod,m)
					sys.modules[path[1:]]=mod
				
			if module[0:4]!=imp.get_magic():
				# compile source code
				code=compile(module,'<downloaded>','exec')
			else:
				# read bytecode from the client
				code=marshal.loads(module[8:])
			# finally, execute the module code in the right module.	A
			exec code in mod.__dict__
		else:
			Log.warn('ObjBase','attempt to supply code denied: ',name,'from',str(sourceaddr))
			raise PyroError('attempt to supply code denied')
示例#5
0
def macro(name, filename, source, **identifiers):
    """macro(name, source, **identifiers)
    This allows you to create macro-like behaviors in python.  See
    twisted.python.hook for an example of its usage.
    """
    if not identifiers.has_key('name'):
        identifiers['name'] = name
    source = source % identifiers
    codeplace = "<%s (macro)>" % filename
    code = compile(source, codeplace, 'exec')
    sm = sys.modules
    tprm = "twisted.python.reflect.macros"
    if not sm.has_key(tprm):
        macros = new.module(tprm)
        sm[tprm] = macros
        macros.count = 0
    macros = sm[tprm]
    macros.count += 1
    macroname = 'macro_' + str(macros.count)
    tprmm = tprm + '.' + macroname
    mymod = new.module(tprmm)
    sys.modules[tprmm] = mymod
    setattr(macros, macroname, mymod)
    dict = mymod.__dict__
    exec code in dict, dict
    return dict[name]
示例#6
0
文件: rpy.py 项目: sota/pypy
def get_hacked_sre_compile(my_compile):
    """Return a copy of the sre_compile module for which the _sre
    module is a custom module that has _sre.compile == my_compile
    and CODESIZE == rsre_char.CODESIZE.
    """
    import sre_compile, sre_constants, __builtin__, new

    sre_hacked = new.module("_sre_hacked")
    sre_hacked.compile = my_compile
    sre_hacked.MAGIC = sre_compile.MAGIC
    sre_hacked.CODESIZE = rsre_char.CODESIZE
    sre_hacked.MAXREPEAT = sre_constants.MAX_REPEAT
    sre_hacked.getlower = rsre_char.getlower

    def my_import(name, *args):
        if name == "_sre":
            return sre_hacked
        else:
            return default_import(name, *args)

    src = sre_compile.__file__
    if src.lower().endswith(".pyc") or src.lower().endswith(".pyo"):
        src = src[:-1]
    mod = new.module("sre_compile_hacked")
    default_import = __import__
    try:
        __builtin__.__import__ = my_import
        execfile(src, mod.__dict__)
    finally:
        __builtin__.__import__ = default_import
    return mod
示例#7
0
文件: testing.py 项目: CGTIC/Plone_SP
def fake_import(fake_module):
    module_name = 'martiantest.fake.' + fake_module.__name__
    module = new.module(module_name)
    module_name_parts = module_name.split('.')
    module.__file__ =  '/' + '/'.join(module_name_parts)
    
    glob = {}
    for name in dir(fake_module):
        if name.startswith('__') and '.' not in name:
            continue
        obj = getattr(fake_module, name)
        glob[name] = obj
        try:
            obj = obj.im_func
        except AttributeError:
            pass
        __module__ = None
        try:
            __module__ == obj.__dict__.get('__module__')
        except AttributeError:
            try:
                __module__ = obj.__module__
            except AttributeError:
                pass
        if __module__ is None or __module__ == '__builtin__':
            try:
                obj.__module__ = module.__name__
            except AttributeError:
                pass
        setattr(module, name, obj)

    # provide correct globals for functions
    for name in dir(module):
        if name.startswith('__'):
            continue
        obj = getattr(module, name)
        try:
            code = obj.func_code
            new_func = new.function(code, glob, name)
            new_func.__module__ = module.__name__
            setattr(module, name, new_func)
            glob[name] = new_func
        except AttributeError:
            pass

    if not 'martiantest' in sys.modules:
        sys.modules['martiantest'] = new.module('martiantest')
        sys.modules['martiantest.fake'] = new.module('martiantest.fake')
        sys.modules['martiantest'].fake = sys.modules['martiantest.fake']

    sys.modules[module_name] = module
    setattr(sys.modules['martiantest.fake'], module_name.split('.')[-1],
            module)
    
    return module
示例#8
0
 def test_import_settings(self):
     # import_settings() copies settings from another module into the
     # caller's global scope.
     source = new.module(b"source")
     source.SETTING = factory.getRandomString()
     target = new.module(b"target")
     target._source = source
     target._import_settings = import_settings
     eval("_import_settings(_source)", vars(target))
     expected = {"SETTING": source.SETTING}
     observed = find_settings(target)
     self.assertEqual(expected, observed)
示例#9
0
文件: protocol.py 项目: LLNL/WVL
	def _retrieveCode(self, mname, level):
		import imp, marshal, new

		# Call the special method on the server to retrieve the code.
		# No need for complex exception stuff like when the server needs
		# code from the client (see handleInvocation): because the server
		# is a Pyro object we can actually *call* it :-)
		module = self.remoteInvocation("remote_retrieve_code",0,mname)
		mname = mname.split('.')
		path = ''
		mod = new.module("pyro-server-context")
		for m in mname:
			path += '.' + m
			# use already loaded modules instead of overwriting them
			real_path = path[1:]
			if sys.modules.has_key(real_path):
				mod = sys.modules[real_path]
			else:
				setattr(mod, m, new.module(real_path))
				mod = getattr(mod, m)
				sys.modules[real_path] = mod
				
		if module[0:4] != imp.get_magic():
			code = compile(module, "<downloaded>", "exec")
		else:
			code = marshal.loads(module[8:])
 
		try:
			loaded = 0
			# XXX probably want maxtries here...
			while not loaded:
				import __builtin__
				importer = agent_import(__builtin__.__import__)
				__builtin__.__import__ = importer
 
				try:
					exec code in mod.__dict__
					loaded = 1
				except ImportError, x:
					mname = importer.name
 
					if importer is not None:
						__builtin__.__import__ = importer.orig_import
						importer = None
 
					# XXX probably want maxrecursion here...
					self._retrieveCode(mname, level+1)
 
		finally:
			if importer is not None:
				__builtin__.__import__ = importer.orig_import
示例#10
0
    def test_pickle_submodule(self):
        import pickle
        import sys, new

        mod = new.module('pack.mod')
        sys.modules['pack.mod'] = mod
        pack = new.module('pack')
        pack.mod = mod
        sys.modules['pack'] = pack

        import pack.mod
        pckl   = pickle.dumps(pack.mod)
        result = pickle.loads(pckl)
        assert pack.mod is result
  def testWithModules(self):
    """Test what happens when no modules provided."""
    modules = [new.module('package1'), new.module('package1')]

    file1 = descriptor.FileDescriptor()
    file1.package = 'package1'
    file2 = descriptor.FileDescriptor()
    file2.package = 'package2'

    expected = descriptor.FileSet()
    expected.files = [file1, file1]

    described = descriptor.describe_file_set(modules)
    described.check_initialized()
    self.assertEquals(expected, described)
    def test_module_reload(self):
        Worker = type("Worker", (object,), {})
        buildbot_module = new.module('buildbot_module')
        buildbot_module.Worker = Worker
        with mock.patch.dict(sys.modules,
                             {'buildbot_module': buildbot_module}):
            scope = buildbot_module.__dict__
            deprecatedWorkerModuleAttribute(scope, Worker)
            # Overwrite with Twisted's module wrapper.
            import buildbot_module

            # Module reload is effectively re-run of module contents.
            Worker = type("Worker", (object,), {})
            buildbot_module.Worker = Worker
            scope = buildbot_module.__dict__
            deprecatedWorkerModuleAttribute(scope, Worker)
            # Overwrite with Twisted's module wrapper.
            import buildbot_module

        with assertNotProducesWarnings(DeprecatedWorkerAPIWarning):
            W = buildbot_module.Worker
        self.assertIdentical(W, Worker)

        with assertProducesWarning(
                DeprecatedWorkerNameWarning,
                message_pattern=r"buildbot_module\.Slave was deprecated in "
                                r"Buildbot 0.9.0: Use Worker instead."):
            S = buildbot_module.Slave
        self.assertIdentical(S, Worker)
示例#13
0
    def test_copy_continulet_real(self):
        import new, sys

        mod = new.module("test_copy_continulet_real")
        sys.modules["test_copy_continulet_real"] = mod
        exec """if 1:
            from _continuation import continulet
            import copy
            def f(co, x):
                co.switch(x + 1)
                co.switch(x + 2)
                return x + 3
            co = continulet(f, 40)
            res = co.switch()
            assert res == 41
            co2 = copy.deepcopy(co)
            #
            res = co2.switch()
            assert res == 42
            assert co2.is_pending()
            res = co2.switch()
            assert res == 43
            assert not co2.is_pending()
            #
            res = co.switch()
            assert res == 42
            assert co.is_pending()
            res = co.switch()
            assert res == 43
            assert not co.is_pending()
        """ in mod.__dict__
示例#14
0
文件: core.py 项目: avenet/tagfs
	def remote_retrieve_code(self, name):
		# XXX codeValidator: can we somehow get the client's address it is sent to?
		# XXX this code is ugly. And duplicated in protocol.py remoteInvocation.
		if Pyro.config.PYRO_MOBILE_CODE and self.codeValidator(name,None,None):
			Log.msg("ObjBase","supplying code: ",name)
			try:
				importmodule=new.module("pyro-server-import")
				try:
					exec "import " + name in importmodule.__dict__
				except ImportError:
					Log.error("ObjBase","Client wanted a non-existing module:", name)
					raise PyroError("Client wanted a non-existing module", name)
				m=eval("importmodule."+name)
				# try to load the module's compiled source, or the real .py source if that fails.
				# note that the source code (.py) is opened with universal newline mode
				(filebase,ext)=os.path.splitext(m.__file__)
				if ext.startswith(".PY"):
					exts = ( (".PYO","rb"), (".PYC","rb"), (".PY","rU") )	# uppercase
				else:
					exts = ( (".pyo","rb"), (".pyc","rb"), (".py","rU") )	# lowercase
				for ext,mode in exts:
					try:
						m=open(filebase+ext, mode).read()
						return m  # supply the module to the client!
					except:
						pass
				Log.error("ObjBase","cannot read module source code for module:", name)
				raise PyroError("cannot read module source code")
			finally:
				del importmodule
		else:
			Log.error("ObjBase","attempt to retrieve code denied:", name)
			raise PyroError("attempt to retrieve code denied")
 def load_sysconfigs(self):
     configs = self.sysconfigs[:]
     configs.reverse()
     self.sysconfig_modules = []
     for index, (explicit, name) in enumerate(configs):
         # @@: At some point I'd like to give the specialized
         # modules some access to the values in earlier modules,
         # e.g., to specialize those values or functions.  That's
         # why these modules are loaded backwards.
         if name.endswith(".py"):
             if not os.path.exists(name):
                 if explicit:
                     raise BadCommand, ("sysconfig file %s does not exist" % name)
                 else:
                     continue
             globs = {}
             execfile(name, globs)
             mod = new.module("__sysconfig_%i__" % index)
             for name, value in globs.items():
                 setattr(mod, name, value)
             mod.__file__ = name
         else:
             try:
                 mod = import_string.simple_import(name)
             except ImportError, e:
                 if explicit:
                     raise
                 else:
                     continue
         mod.paste_command = self
         self.sysconfig_modules.insert(0, mod)
示例#16
0
    def test_pickle2(self):
        # To test a bug where too much stuff gets pickled when
        # a tasklet halted on stackless.schedule() is pickled.
        import new, sys

        mod = new.module("mod")
        sys.modules["mod"] = mod
        try:
            exec """
import pickle, sys
import stackless
import socket

def task_should_be_picklable():
    stackless.schedule()

def task_socket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    stackless.schedule()

def task_pickle(ref_task):
    p = pickle.dumps(ref_task)
    
ref_task = stackless.tasklet(task_should_be_picklable)()
stackless.tasklet(task_socket)()
stackless.tasklet(task_pickle)(ref_task)
stackless.run()
""" in mod.__dict__
        finally:
            del sys.modules["mod"]
示例#17
0
文件: driver.py 项目: chrisburr/ganga
def exec_module_code(path, ns=None):
    """
            loads the code from path and execute it in the given namespace
            If ns is None the code is executed in the newly created module namespace
        """
    (testdir, filename) = os.path.split(path)
    cwd = os.getcwd()
    if testdir:
        try:
            os.chdir(testdir)
        except:
            pass

    name = os.path.splitext(filename)[0]
    code_str = read_file(filename)
    code = compile(code_str, filename, "exec")
    (name, ext) = os.path.splitext(filename)
    if name in sys.modules:
        mod = sys.modules[name]  # necessary for reload()
    else:
        mod = new.module(name)
        sys.modules[name] = mod
    mod.__name__ = name
    mod.__file__ = filename
    if ns is None:
        exec(code, mod.__dict__)
    else:
        exec(code, ns)

    # restore cwd
    os.chdir(cwd)
    return mod
示例#18
0
文件: util.py 项目: tjlee/factory
def importString(source, filename, globals_=None, locals_=None):
    """construct a module from source code.
    
    The module will I{not} appear in L{sys.modules}.
    
    @arg source: source code of the module
    @type source: string
    
    @arg filename: a filename to use for the module's __file__
    @type filename: string
    
    @arg globals_: globals to pass to L{eval}(). Defaults to globals()
    @type globals_: dict

    @arg locals_: locals to pass to L{eval}(). Defaults to empty dict
    @type locals_: dict    
    
    @rtype: a module
    """
    if globals_ is None: globals_ = globals()
    if locals_ is None: locals_ = {}
    locals_['__file__'] = filename
    co = compile(source, filename, 'exec')
    eval(co, globals_, locals_)
    mod = new.module(os.path.splitext(os.path.basename(filename))[0])
    mod.__dict__.update(locals_)
    return mod
示例#19
0
 def import_module(self, path):
     if os.path.isfile(path):
         sys.path.insert(0, os.path.dirname(path))
         name = os.path.split(path)[-1].split('.')[0]
         filename, pathname, description = imp.find_module(name, [os.path.dirname(path)])
         module = imp.load_module(name, filename, pathname, description)
         module.functest_module_path = path
         module.__file__ = os.path.abspath(path)
         sys.path.pop(0)
     elif os.path.isdir(path):
         if os.path.isfile(os.path.join(path, '__init__.py')):
             sys.path.insert(0, os.path.abspath(os.path.join(path, os.path.pardir)))
             name = os.path.split(path)[-1]
             filename, pathname, description = imp.find_module(
                 name, [os.path.abspath(os.path.join(path, os.path.pardir))])
             module = imp.load_module(name, filename, pathname, description)
             module.functest_module_path = path
             module.__file__ = os.path.abspath(os.path.join(path, '__init__.py'))
             sys.path.pop(0)
         else:
             module = new.module(os.path.split(path)[-1])
             module.functest_module_path = path
     else:
         raise ImportError('path is not file or directory')
     return module
    def load_module(self, fullname):
        '''
        Generate the code for the module and execute it
        '''
        logging.info('load_module:%s' % fullname)
        
        metadata = AppImporterAndLoader.find_metadata_by_name(fullname)       
        if not metadata:
            return None

        code = AppImporterAndLoader.getAppModuleCode(fullname, True)
        
        mod = new.module(fullname.encode('latin-1'))
        sys.modules[fullname] = mod
        mod.__file__ = "%s" % self.__class__.__name__
        mod.__loader__ = self
        
        #logging.info(code[0])
        compiled = compile(code[0], 'schemaloader', 'exec')
        exec compiled  in mod.__dict__    
        
        #logging.info("code is '%s'" % code[1] )
        #compiled = compile(code[1], 'schemaloader', 'exec')
        #exec compiled  in mod.__dict__ 
        
        return mod
def _get_script_function_models(script_name, source, old_functions, ignore_errors=False):
    script_module = new.module(str(script_name))
    try:
        exec source in script_module.__dict__
    except Exception:
        logging.warn('Compilation failed for \'%s\'', script_name, exc_info=True)
        if ignore_errors:
            return []
        raise HttpBadRequestException('compilation_error', {'traceback': traceback.format_exc()})
    functions = inspect.getmembers(script_module,
                                   lambda x: inspect.isfunction(x) and x.__module__ == script_module.__name__)
    function_models = []
    old_funcs = {f.name: f for f in old_functions}

    lines = source.splitlines()
    for f in functions:
        f_name = unicode(f[0])
        line_number = 1
        for i, line in enumerate(lines):
            if 'def %s' % f_name in line:
                line_number = i + 1
                break
        if f_name in old_funcs:
            updated_function = old_funcs[f_name]
            updated_function.line_number = line_number
            function_models.append(updated_function)
        else:
            function_models.append(ScriptFunction(name=f_name, line_number=line_number))
    return function_models
示例#22
0
    def load_module(self, fullname, loadingShared = False):
        #print >>sys.stderr, "load_module(%s), dir_path = %s, filename = %s" % (fullname, self.dir_path, self.filename)
        if self.fileType == FTFrozenModule:
            return self._import_frozen_module(fullname)
        if self.fileType == FTExtensionModule:
            return self._import_extension_module(fullname)

        # Check if this is a child of a shared package.
        if not loadingShared and self.packagePath and '.' in fullname:
            parentname = fullname.rsplit('.', 1)[0]
            if parentname in sharedPackages:
                # It is.  That means it's a shared package too.
                parent = sys.modules[parentname]
                path = getattr(parent, '__path__', None)
                importer = VFSSharedImporter()
                sharedPackages[fullname] = True
                loader = importer.find_module(fullname, path = path)
                assert loader
                return loader.load_module(fullname)
        
        code = self._read_code()
        if not code:
            raise ImportError, 'No Python code in %s' % (fullname)
        
        mod = sys.modules.setdefault(fullname, new.module(fullname))
        mod.__file__ = self.filename.toOsSpecific()
        mod.__loader__ = self
        if self.packagePath:
            mod.__path__ = [self.packagePath.toOsSpecific()]
            #print >> sys.stderr, "loaded %s, path = %s" % (fullname, mod.__path__)

        exec code in mod.__dict__
        return mod
示例#23
0
def run(codeid, version, function, in_a_deferred=False):
    if in_a_deferred:
        deferred.defer(_run_deferred, codeid, version, function)
        return None
    code = Code.get_by_id(codeid)
    m = new.module(str(code.name))
    exec code.source in m.__dict__
    f = getattr(m, function)
    start = time.time()
    try:
        r = f()
        runtime = time.time() - start
        rr = RunResultTO()
        rr.result = xml_escape(pprint.pformat(r).decode(errors='replace'))
        rr.succeeded = True
        rr.time = int(runtime)
        return rr
    except:
        runtime = time.time() - start
        rr = RunResultTO()
        format_exc = traceback.format_exc()
        rr.result = xml_escape(format_exc.decode(errors='replace'))
        rr.succeeded = False
        rr.time = int(runtime)
        return rr
示例#24
0
def unimport_module(name):
    old = sys.modules[name]
    sys.modules[name] = new.module('jinja2')
    try:
        yield
    finally:
        sys.modules[name] = old
示例#25
0
    def test_pickle_continulet_real(self):
        import new, sys

        mod = new.module("test_pickle_continulet_real")
        sys.modules["test_pickle_continulet_real"] = mod
        mod.version = self.version
        exec """if 1:
            from _continuation import continulet
            import pickle
            def f(co, x):
                co.switch(x + 1)
                co.switch(x + 2)
                return x + 3
            co = continulet(f, 40)
            res = co.switch()
            assert res == 41
            pckl = pickle.dumps(co, version)
            print repr(pckl)
            co2 = pickle.loads(pckl)
            #
            res = co2.switch()
            assert res == 42
            assert co2.is_pending()
            res = co2.switch()
            assert res == 43
            assert not co2.is_pending()
            #
            res = co.switch()
            assert res == 42
            assert co.is_pending()
            res = co.switch()
            assert res == 43
            assert not co.is_pending()
        """ in mod.__dict__
示例#26
0
def compile_source(source, name):
    user = users.get_current_user()
    m = new.module(str(name))
    try:
        exec source in m.__dict__
    except Exception:
        logging.warn("Compilation failed for [%s]" % name, exc_info=True)
        to = CodeTO()
        to.id = None
        to.timestamp = -1
        to.author = None
        to.name = None
        to.source = None
        to.functions = []
        to.version = -1
        to.compile_error = unicode(traceback.format_exc())
        return to

    functions = inspect.getmembers(m, lambda x: inspect.isfunction(x) and x.__module__ == m.__name__)
    code = Code().all().filter("name =", name).get()
    if not code:
        code = Code()
    code.author = user
    code.timestamp = now()
    code.name = name
    code.source = source
    code.functions = [unicode(f[0]) for f in functions]
    code.version = code.version + 1 if code.version else 1
    code.put()
    to = CodeTO.fromDBCode(code)
    to.compile_error = None
    return to
示例#27
0
文件: tools.py 项目: ostrokach/biskit
def tryImportModule( module, old_as=None, namespace=None ):
    """
    Try to import a class from a module. If that fails, 'import' a
    default class of the same name that raises an exception when used.
    
    @param module: name of the module
    @type  module: str
    @param namespace: namespace for the import [default: globals() ]
    @type  namespace: dict
    
    @return: True if import succeeded, False otherwise
    @rtype: bool
    """
    old_as = old_as or module
    g = namespace or globals()
    try:
        exec 'import %s as %s' % (module, old_as) in g
        return True
    
    except ImportError, e:

        m = new.module( old_as, doc='Pseudo module. Import of real one failed.' )
        m.error = str(e)
        
        g.update( {old_as: m} )
示例#28
0
文件: util.py 项目: atubbs/spitfire
def load_module_from_src(src_code, filename, module_name):
  module = new.module(module_name)
  sys.modules[module_name] = module

  bytecode = compile(src_code, filename, 'exec')
  exec bytecode in module.__dict__
  return module
示例#29
0
def define_file(file_descriptor, module=None):
  """Define module from FileDescriptor.

  Args:
    file_descriptor: FileDescriptor instance to describe module from.
    module: Module to add contained objects to.  Module name overrides value
      in file_descriptor.package.  Definitions are added to existing
      module if provided.

  Returns:
    If no module provided, will create a new module with its name set to the
    file descriptor's package.  If a module is provided, returns the same
    module.
  """
  if module is None:
    module = new.module(file_descriptor.package)

  for enum_descriptor in file_descriptor.enum_types or []:
    enum_class = define_enum(enum_descriptor, module.__name__)
    setattr(module, enum_descriptor.name, enum_class)

  for message_descriptor in file_descriptor.message_types or []:
    message_class = define_message(message_descriptor, module.__name__)
    setattr(module, message_descriptor.name, message_class)

  for service_descriptor in file_descriptor.service_types or []:
    service_class = define_service(service_descriptor, module)
    setattr(module, service_descriptor.name, service_class)

  return module
示例#30
0
def _create_module(code, name, filename, store=True, ns={}, exec_module=None):
    for recompiled in range(2):
        name = get_template_name(name, filename)
        mod = new.module(name)
        mod.__file__ = filename
        mod.__ctime__ = time.time()
        mod.__dict__.update(ns)
        try:
            if exec_module:
                exec_module(mod, code)
            else:
                exec code in mod.__dict__
        except Exception:
            if store:
                sys.modules[name] = mod
            raise_template_error(module=name, filename=filename)
        if getattr(mod, "kid_version", None) == __version__:
            break
        # the module has been compiled against an old Kid version,
        # recompile to ensure compatibility and best performance
        if recompiled:  # already tried recompiling, to no avail
            raise TemplateImportError(
                "Cannot recompile template file" " %r for Kid version %s" % (filename, __version__)
            )
        template = KidFile(filename)
        template.stale = True
        template._python = template._code = None
        code = template.compile(dump_source=environ.get("KID_OUTPUT_PY"))
    if store:
        sys.modules[name] = mod
    return mod
示例#31
0
def import_data_module(modname):
    mod = None

    if hasattr(sys.modules, modname):
        mod = sys.modules[modname]
    else:
        mod = new.module(modname.encode('latin-1'))
        mod.__file__ = "__datamodel__BaseDataModel__"
        sys.modules[modname] = mod

    return mod
示例#32
0
	def remote_supply_code(self, name, module, sourceaddr):
		# XXX this is nasty code, and also duplicated in protocol.py _retrieveCode()
		if Pyro.config.PYRO_MOBILE_CODE and self.codeValidator(name,module,sourceaddr):
			try:
				imp.acquire_lock()   # threadsafe imports
				if name in sys.modules and getattr(sys.modules[name],'_PYRO_bytecode',None):
					# already have this module, don't import again
					# we checked for the _PYRO_bytecode attribute because that is only
					# present when all loading code below completed successfully
					return
				Log.msg('ObjBase','loading supplied code: ',name,'from',str(sourceaddr))
				if module[0:4]!=imp.get_magic():
					# compile source code
					code=compile(module,'<downloaded>','exec')
				else:
					# read bytecode from the client
					code=marshal.loads(module[8:])

				# make the module hierarchy and add all names to sys.modules
				name=name.split('.')
				path=''
				mod=new.module("pyro-agent-context")
				for m in name:
					path+='.'+m
					# use already loaded modules instead of overwriting them
					real_path = path[1:]
					if sys.modules.has_key(real_path):
						mod = sys.modules[real_path]
					else:
						setattr(mod,m,new.module(path[1:]))
						mod=getattr(mod,m)
						sys.modules[path[1:]]=mod
				# execute the module code in the right module.
				exec code in mod.__dict__
				# store the bytecode for possible later reference if we need to pass it on
				mod.__dict__['_PYRO_bytecode'] = module
			finally:
				imp.release_lock()
		else:
			Log.warn('ObjBase','attempt to supply code denied: ',name,'from',str(sourceaddr))
			raise PyroError('attempt to supply code denied')
示例#33
0
    def readSettingsFromPySrcStr(self, theString):
        """Return a dictionary of the settings in a Python src string."""

        globalsDict = {'True': (1==1),
                       'False': (0==1),
                       }
        newSettings = {'self':self}
        exec((theString+os.linesep), globalsDict, newSettings)        
        del newSettings['self']
        module = new.module('temp_settings_module')
        module.__dict__.update(newSettings)
        return self.readSettingsFromModule(module)
示例#34
0
def procure_module(name):
    """Return an established or else new module object having NAME.

    First checks sys.modules, then sys.stub_modules."""

    if sys.modules.has_key(name):
	it = sys.modules[name]
    elif sys.stub_modules.has_key(name):
	it = sys.stub_modules[name]
    else:
	it = new.module(name)
    return it								# ===>
示例#35
0
 def test_known_type_known_class(self, cdbaccessmock):
     sys.modules['MockModule'] = new.module('MockModule')
     sys.modules['MockModule'].__dict__['MockClass'] = \
                         new.classobj('MockClass', (object,), {})
     loggermock = mock.Mock(spec=Acspy.Common.Log.Logger)
     containerservices = CS.ContainerServices()
     containerservices._ContainerServices__logger = loggermock
     self.assertEqual(False, \
               containerservices._ContainerServices__importComponentStubs( \
               None, 'IDL:alma/MockModule/MockClass:1.0') is None)
     self.assertEqual(False, loggermock.logWarning.called)
     del sys.modules['MockModule']
示例#36
0
 def test_bad_class_fault(self):
     mockmodule = new.module('MockClass')
     sys.modules['MockModule'] = mockmodule
     offshootpoamock = mock.Mock(spec=omniORB.PortableServer.POA)
     offshootpoamock.create_POA.return_value = \
                                  mock.Mock(spec=omniORB.PortableServer.POA)
     self.tc.createPOAForComponent.return_value = offshootpoamock
     self.assertRaises(Acspy.Container.CannotActivateComponentExImpl,
                       self.tc.activate_component,
                       12345, 12, 'Test', 'MockClass',
                       'IDL:alma/acspytest/MockClass:1.0')
     del sys.modules['MockModule']
示例#37
0
def findOrCreate(name):
    from clojure.lang.symbol import Symbol
    if isinstance(name, Symbol):
        name = name.name
    if name in sys.modules:
        return sys.modules[name]

    mod = new.module(name)
    sys.modules[name] = mod

    addDefaultImports(mod)
    return mod
示例#38
0
    def setUp(self):
        self.old_env = os.environ.copy()
        self.mods = sys.modules.copy()

        if 'DJANGO_SETTINGS_MODULE' in os.environ.keys():
            from django import conf
            import copy

            self.mod = copy.deepcopy(conf.settings)
            mod = conf.settings
            self.existing = True
        else:
            os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
            mod = new.module('settings')

            sys.modules['settings'] = mod

            self.existing = False

        app = new.module('adapters')

        app_models = new.module('adapters.models')
        setattr(app, 'models', app_models)
        setattr(app, '__file__', '')
        setattr(app_models, '__file__', '')

        sys.modules['adapters'] = app
        sys.modules['adapters.models'] = app_models

        self.app = app
        self.models = app_models

        setattr(mod, 'DATABASE_ENGINE', 'sqlite3')
        setattr(mod, 'DATABASE_NAME', ':memory:')
        setattr(mod, 'INSTALLED_APPS', ('adapters', ))
        setattr(mod, 'USE_I18N', False)

        from pyamf.adapters import _django_db_models_base as models_adapter

        self.adapter = models_adapter
示例#39
0
 def test_constructor_fault(self):
     mockmodule = new.module('MockClass')
     mockmodule.__dict__['MockClass'] = new.classobj('MockClass', (), {})
     sys.modules['MockClass'] = mockmodule
     offshootpoamock = mock.Mock(spec=omniORB.PortableServer.POA)
     offshootpoamock.create_POA.return_value = \
                                  mock.Mock(spec=omniORB.PortableServer.POA)
     self.tc.createPOAForComponent.return_value = offshootpoamock
     self.assertEqual(True, 
                      self.tc.activate_component(12345, 12, 'Test',
                                            'MockClass',
                                            'IDL:alma/acspytest/MockClass:1.0') is not None)
     del sys.modules['MockClass']
示例#40
0
def getModule(package, path):
    module = cache.get(path)
    if module is None:
        name = os.path.splitext(os.path.basename(path))[0]
        module = cache[path] = new.module(name)
        module.mapi = ModAPI(package, path)
        module.__file__ = path
        if path.endswith('.py'):
            runPy(path, module.__dict__)
        elif path.endswith('.pyc'):
            runPyc(path, module.__dict__)

    return module
示例#41
0
    def LoadModule(self, module_name, source):
        result = {
            '__name__': module_name,
            'messages': messages,
            'remote': remote,
        }
        exec source in result

        module = new.module(module_name)
        for name, value in result.iteritems():
            setattr(module, name, value)

        return module
示例#42
0
 def _importAsDummyModule(self, contents):
     """Used by the Compiler to do correct importing from Cheetah templates
     when the template is compiled via the Template class' interface rather
     than via 'cheetah compile'.
     """
     tmpFilename = self._genTmpFilename()
     name = tmpFilename.replace('.py', '')
     co = compile(contents + '\n', tmpFilename, 'exec')
     mod = new.module(name)
     #mod.__file__ = co.co_filename
     #mod.__co__ = co
     exec co in mod.__dict__
     return mod
示例#43
0
    def setUp(self):
        BaseTestCase.setUp(self)

        import new

        self.mod_name = '%s.%s' % (__name__, 'settings')
        self.settings = sys.modules[self.mod_name] = new.module(self.mod_name)

        self.old_env = os.environ.get('DJANGO_SETTINGS_MODULE', None)

        os.environ['DJANGO_SETTINGS_MODULE'] = self.mod_name

        self.settings.SECRET_KEY = 'unittest'
示例#44
0
 def test_package_bug(self):
     import os, sys
     import new
     mod = new.module('xxuuv')
     mod.__path__ = [self.zipfile + '/xxuuv']
     sys.modules['xxuuv'] = mod
     #
     self.writefile("xxuuv/__init__.py", "")
     self.writefile("xxuuv/yy.py", "def f(x): return x")
     mod = __import__("xxuuv.yy", globals(), locals(), ['__doc__'])
     assert mod.__file__ == (self.zipfile + os.path.sep + "xxuuv" +
                             os.path.sep + "yy.py")
     assert mod.f(3) == 3
示例#45
0
def main(use_pygame_console=0, interactmethod=default_interactmethod, print_banner=True, clear_main=True):
    si, se, so = sys.stdin, sys.stderr, sys.stdout
    try:
        if 0 and use_pygame_console: # pygame currently borked
            from pyrepl.pygame_console import PyGameConsole, FakeStdin, FakeStdout
            con = PyGameConsole()
            sys.stderr = sys.stdout = FakeStdout(con)
            sys.stdin = FakeStdin(con)
        else:
            from pyrepl.unix_console import UnixConsole
            try:
                import locale
            except ImportError:
                encoding = None
            else:
                if hasattr(locale, 'nl_langinfo') \
                       and hasattr(locale, 'CODESET'):
                    encoding = locale.nl_langinfo(locale.CODESET)
                elif os.environ.get('TERM_PROGRAM') == 'Apple_Terminal':
                    # /me whistles innocently...
                    code = int(os.popen(
                        "defaults read com.apple.Terminal StringEncoding"
                        ).read())
                    if code == 4:
                        encoding = 'utf-8'
                        # More could go here -- and what's here isn't
                        # bulletproof.  What would be?  AppleScript?
                        # Doesn't seem to be possible.
                    else:
                        encoding = None
                else:
                    encoding = None # so you get ASCII...
            con = UnixConsole(0, 1, None, encoding)
        if print_banner:
            print "Python", sys.version, "on", sys.platform
            print 'Type "help", "copyright", "credits" or "license" '\
                  'for more information.'
        sys.path.insert(0, os.getcwd())

        if clear_main and __name__ != '__main__':
            mainmod = new.module('__main__')
            sys.modules['__main__'] = mainmod
        else:
            mainmod = sys.modules['__main__']

        rc = ReaderConsole(con, mainmod.__dict__)
        rc.reader._module_list_ready = False
        rc.run_user_init_file()
        getattr(rc, interactmethod)()
    finally:
        sys.stdin, sys.stderr, sys.stdout = si, se, so
示例#46
0
 def _reset_module(self):
     ''' 
     Internal method for resetting the module before compilation
     '''
     self.create_fom_mask_func()
     self.script_module = new.module('genx_script_module')
     #self.script_module = Temp()
     #self.script_module.__dict__ = {}
     # Bind data for preprocessing with the script
     self.script_module.__dict__['data'] = self.data
     # Flag to indicate to the Sim funtion if a simulation is conducted (True)
     # or a fit is running (False).
     self.script_module.__dict__['_sim'] = False
     self.compiled = False
示例#47
0
    def readSettingsFromPySrcStr(self, theString):
        
        """Return a dictionary of the settings in a Python src string."""

        globalsDict = {'True':1,
                       'False':0,
                       'SettingsContainer':SettingsContainer,
                       }
        newSettings = {'self':self}
        exec theString in globalsDict, newSettings
        del newSettings['self'], newSettings['True'], newSettings['False']
        module = new.module('temp_settings_module')
        module.__dict__.update(newSettings)
        return self.readSettingsFromModule(module)
示例#48
0
def import_trees_module(name):
    # create module & add to this one
    mod_name = 'pytreestb.' + name
    mod = new.module(mod_name, ("The Trees Toolbox '%s' module" % mod_name) +
                     _session.help(name))
    __self__.__setattr__(name, mod)

    sys.modules[mod_name] = mod

    # find functions and add to module
    m_files = glob.glob(os.path.join(trees_home, name, '*.m'))
    func_names = [os.path.splitext(os.path.split(x)[-1])[0] for x in m_files]
    func_names.remove('Contents')
    wrap_functions(func_names, None, mod)
示例#49
0
    def load_module(self, fullname):

        if len(fullname.split(".")) < 4:
            module = new.module(fullname)
            module.__file__ = "fake:" + fullname
            module.__path__ = []
            return sys.modules.setdefault(fullname, module)

        module_name = fullname.split(".")[-1]
        repo_root = "/tmp/github"
        repo_dir = "%s/%s" % (repo_root, module_name)

        if len(fullname.split(".")) == 4:

            user = fullname.split(".")[-2]

            if not os.path.exists(repo_root):
                os.mkdir(repo_root)

            if os.path.exists(repo_dir):
                subprocess.call("cd %s && git pull -q" % repo_dir, shell=True)
            else:
                subprocess.call("git clone git://github.com/%s/%s.git %s -q" %
                                (user, module_name, repo_dir),
                                shell=True)
            sys.path.insert(0, repo_root)

            try:
                return __import__(module_name)
            except ImportError:
                sys.path.pop(0)

                module = imp.new_module(fullname)
                module.__file__ = "fake:" + fullname
                module.__path__ = [fullname]
                module.__loader__ = self
                sys.modules.setdefault(fullname, module)
                return module

        if len(fullname.split(".")) > 4:
            try:
                return __import__(".".join(fullname.split(".")[3:]))
            except ImportError:
                sys.modules.pop(module_name, None)

                sys.path.insert(
                    0, os.path.join(repo_dir,
                                    "/".join(fullname.split(".")[5:])))

                return __import__(".".join(fullname.split(".")[4:]))
示例#50
0
 def test_pyc_in_package(self):
     import os, sys
     import new
     mod = new.module('xxuuw')
     mod.__path__ = [self.zipfile + '/xxuuw']
     sys.modules['xxuuw'] = mod
     #
     self.writefile("xxuuw/__init__.py", "")
     self.writefile("xxuuw/zz.pyc", self.test_pyc)
     mod = __import__("xxuuw.zz", globals(), locals(), ['__doc__'])
     assert mod.__file__ == (self.zipfile + os.path.sep + "xxuuw" +
                             os.path.sep + "zz.pyc")
     assert mod.get_file() == mod.__file__
     assert mod.get_name() == mod.__name__
示例#51
0
文件: iter.py 项目: Kefkius/mazabot
def choice(iterable):
    if isinstance(iterable, (list, tuple)):
        return random.choice(iterable)
    else:
        n = 1
        m = new.module('')  # Guaranteed unique value.
        ret = m
        for x in iterable:
            if random.random() < 1 / n:
                ret = x
            n += 1
        if ret is m:
            raise IndexError
        return ret
示例#52
0
def macro(name, filename, source, **identifiers):
    """macro(name, source, **identifiers)

    This allows you to create macro-like behaviors in python.  See
    twisted.python.hook for an example of its usage.
    """
    if not identifiers.has_key('name'):
        identifiers['name'] = name
    source = source % identifiers
    codeplace = "<%s (macro)>" % filename
    code = compile(source, codeplace, 'exec')

    # shield your eyes!
    sm = sys.modules
    tprm = "twisted.python.reflect.macros"
    if not sm.has_key(tprm):
        macros = new.module(tprm)
        sm[tprm] = macros
        macros.count = 0
    macros = sm[tprm]
    macros.count += 1
    macroname = 'macro_' + str(macros.count)
    tprmm = tprm + '.' + macroname
    mymod = new.module(tprmm)
    sys.modules[tprmm] = mymod
    setattr(macros, macroname, mymod)
    dict = mymod.__dict__

    # Before we go on, I guess I should explain why I just did that.  Basically
    # it's a gross hack to get epydoc to work right, but the general idea is
    # that it will be a useful aid in debugging in _any_ app which expects
    # sys.modules to have the same globals as some function.  For example, it
    # would be useful if you were foolishly trying to pickle a wrapped function
    # directly from a class that had been hooked.

    exec code in dict, dict
    return dict[name]
示例#53
0
def _create_module_and_parents(name):
    """Create a module, and all the necessary parents"""
    parts = name.split(".")
    # first create the top-level module
    parent = _create_module(parts[0])
    created_parts = [parts[0]]
    parts.pop(0)
    # now, create any remaining child modules
    while parts:
        child_name = parts.pop(0)
        module = new.module(child_name)
        setattr(parent, child_name, module)
        created_parts.append(child_name)
        sys.modules[".".join(created_parts)] = module
        parent = module
示例#54
0
    def testDefineModule_ReuseModule(self):
        """Test updating module with additional definitions."""
        file_descriptor = self.MakeFileDescriptor('my.package')

        module = new.module('override')
        self.assertEquals(module,
                          definition.define_file(file_descriptor, module))

        self.assertEquals('override', module.MyEnum.__module__)
        self.assertEquals('override', module.MyMessage.__module__)
        self.assertEquals('override', module.MyService.__module__)

        # One thing is different between original descriptor and new.
        file_descriptor.package = 'override'
        self.assertEquals(file_descriptor, descriptor.describe_file(module))
示例#55
0
def js_source(functions, use_pdb=True):
    mod = new.module('_js_src')
    function_names = []
    for func in functions:
        name = func.__name__
        if hasattr(mod, name):
            raise ValueError("exported function name %r is duplicated"
                             % (name,))
        mod.__dict__[name] = func
        function_names.append(name)
    sys.modules['_js_src'] = mod
    try:
        return rpython2javascript(mod, function_names, use_pdb=use_pdb)
    finally:
        del sys.modules['_js_src']
示例#56
0
 def test_module_reload(self):
     componentdata = {}
     componentdata[Acspy.Container.HANDLE] = 1
     componentdata[Acspy.Container.NAME] = 'TestComponent'
     componentdata[Acspy.Container.CORBAREF] = mock.Mock(spec=omniORB.PortableServer.POA)
     componentdata[Acspy.Container.POAOFFSHOOT] = mock.Mock(spec=omniORB.PortableServer.POA)
     componentdata[Acspy.Container.POA] = mock.Mock(spec=omniORB.PortableServer.POA)
     componentdata[Acspy.Container.PYREF] = new.classobj('MockClass', (Acspy.Container.ContainerServices,
                                                                       Acspy.Container.ACSComponent,
                                                                       Acspy.Container.ComponentLifecycle), {})()
     componentdata[Acspy.Container.COMPMODULE] = new.module('MockClass')
     self.testcontainer.compHandles[1] = componentdata[Acspy.Container.NAME]
     self.testcontainer.components[componentdata[Acspy.Container.NAME]] = componentdata
     self.testcontainer.compModuleCount[componentdata[Acspy.Container.COMPMODULE]] = 1
     self.assertEqual(True, self.testcontainer.deactivate_component(1) is None)
示例#57
0
    def load_module(self, fullname):
        if self.fileType == FTCompiledModule:
            return self._import_compiled_module(fullname)

        code = self._read_code()
        if not code:
            raise ImportError

        mod = sys.modules.setdefault(fullname, new.module(fullname))
        mod.__file__ = self.filename.cStr()
        mod.__loader__ = self
        if self.package:
            mod.__path__ = []
        exec code in mod.__dict__
        return mod
示例#58
0
文件: __init__.py 项目: manbaum/Logix
def XXloadmodule(filename):
    import marshal, new

    cfile = filename + ".lxc"
    f = file(cfile, "rb")
    f.read(8)
    code = marshal.load(f)
    f.close()

    mod = new.module(filename)
    mod.__file__ = os.path.abspath(cfile).replace('/', '.')
    lmodules[filename.replace('/', '.')] = mod

    exec code in vars(mod)

    return mod
示例#59
0
 def test_dummy_class(self):
     mockmodule = new.module('MockClass')
     mockmodule.__dict__['MockClass'] = new.classobj('MockClass',
                                                     (Acspy.Container.ContainerServices,
                                                      Acspy.Container.ACSComponent,
                                                      Acspy.Container.ComponentLifecycle), {})
     sys.modules['MockClass'] = mockmodule
     offshootpoamock = mock.Mock(spec=omniORB.PortableServer.POA)
     offshootpoamock.create_POA.return_value = \
                                  mock.Mock(spec=omniORB.PortableServer.POA)
     self.tc.createPOAForComponent.return_value = offshootpoamock
     self.assertEqual(True, 
                      self.tc.activate_component(12345, 12, 'Test',
                                            'MockClass',
                                            'IDL:alma/acspytest/MockClass:1.0') is not None)
     del sys.modules['MockClass']
示例#60
0
    def test_add_invalid(self):
        mod = new.module('spam')
        self.assertRaises(TypeError, pyamf.add_type, mod)
        self.assertRaises(TypeError, pyamf.add_type, {})
        self.assertRaises(TypeError, pyamf.add_type, 'spam')
        self.assertRaises(TypeError, pyamf.add_type, u'eggs')
        self.assertRaises(TypeError, pyamf.add_type, 1)
        self.assertRaises(TypeError, pyamf.add_type, 234234L)
        self.assertRaises(TypeError, pyamf.add_type, 34.23)
        self.assertRaises(TypeError, pyamf.add_type, None)
        self.assertRaises(TypeError, pyamf.add_type, object())

        class A:
            pass

        self.assertRaises(TypeError, pyamf.add_type, A())