• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python py_compile.compile函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中py_compile.compile函数的典型用法代码示例。如果您正苦于以下问题:Python compile函数的具体用法?Python compile怎么用?Python compile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了compile函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_overiden_unchecked_hash_based_pyc

 def test_overiden_unchecked_hash_based_pyc(self):
     with util.create_modules('_temp') as mapping, \
          unittest.mock.patch('_imp.check_hash_based_pycs', 'always'):
         source = mapping['_temp']
         pyc = self.util.cache_from_source(source)
         with open(source, 'wb') as fp:
             fp.write(b'state = "old"')
         os.utime(source, (50, 50))
         py_compile.compile(
             source,
             invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
         )
         loader = self.machinery.SourceFileLoader('_temp', source)
         mod = types.ModuleType('_temp')
         mod.__spec__ = self.util.spec_from_loader('_temp', loader)
         loader.exec_module(mod)
         self.assertEqual(mod.state, 'old')
         # Update the source file, which should be ignored.
         with open(source, 'wb') as fp:
             fp.write(b'state = "new"')
         loader.exec_module(mod)
         self.assertEqual(mod.state, 'new')
         with open(pyc, 'rb') as fp:
             data = fp.read()
         self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b1)
         self.assertEqual(
             self.util.source_hash(b'state = "new"'),
             data[8:16],
         )
开发者ID:FFMG,项目名称:myoddweb.piger,代码行数:29,代码来源:test_file_loader.py


示例2: copy_python

def copy_python():
    
    if not os.path.exists(py_dir):
        os.mkdir(py_dir)

    for x in os.listdir(srcdir):
        y = os.path.join(srcdir, x)
        ext = os.path.splitext(x)[1]
        if os.path.isdir(y) and x not in ('test', 'hotshot', 'distutils',
                'site-packages', 'idlelib', 'lib2to3', 'dist-packages', '__pycache__'):
            shutil.copytree(y, os.path.join(py_dir, x),
                    ignore=ignore_in_dirs)
        if os.path.isfile(y) and ext in ('.py', '.so'):
            shutil.copy2(y, py_dir)

    #site_dest = os.path.join(py_dir, 'site-packages')
    copy_site_packages(site_packages, site_dest)
    create_site_py()

    for x in os.walk(py_dir):
        for f in x[-1]:
            if f.endswith('.py'):
                y = os.path.join(x[0], f)
                rel = os.path.relpath(y, py_dir)
                try:
                    py_compile.compile(y, cfile=y+'o',dfile=rel, doraise=True, optimize=2)
                    os.remove(y)
                    z = y+'c'
                    if os.path.exists(z):
                        os.remove(z)
                except:
                    print ('Failed to byte-compile', y)
开发者ID:datwsui,项目名称:Sigil,代码行数:32,代码来源:linux_python_gather.py


示例3: redo_pyc

def redo_pyc(egg):
    if not os.path.isdir(egg):
        return
    for dirpath, dirnames, filenames in os.walk(egg):
        for filename in filenames:
            if not filename.endswith('.py'):
                continue
            filepath = os.path.join(dirpath, filename)
            if not (os.path.exists(filepath+'c')
                    or os.path.exists(filepath+'o')):
                # If it wasn't compiled, it may not be compilable
                continue

            # OK, it looks like we should try to compile.

            # Remove old files.
            for suffix in 'co':
                if os.path.exists(filepath+suffix):
                    os.remove(filepath+suffix)

            # Compile under current optimization
            try:
                py_compile.compile(filepath)
            except py_compile.PyCompileError:
                logger.warning("Couldn't compile %s", filepath)
            else:
                # Recompile under other optimization. :)
                args = [sys.executable]
                if __debug__:
                    args.append('-O')
                args.extend(['-m', 'py_compile', filepath])

                call_subprocess(args)
开发者ID:mitchellrj,项目名称:buildout,代码行数:33,代码来源:easy_install.py


示例4: test_module_with_large_stack

def test_module_with_large_stack(module):
    # create module w/list of 65000 elements to test bug #561858
    filename = module + os.extsep + 'py'

    # create a file with a list of 65000 elements
    f = open(filename, 'w+')
    f.write('d = [\n')
    for i in range(65000):
        f.write('"",\n')
    f.write(']')
    f.close()

    # compile & remove .py file, we only need .pyc (or .pyo)
    f = open(filename, 'r')
    py_compile.compile(filename)
    f.close()
    os.unlink(filename)

    # need to be able to load from current dir
    sys.path.append('')

    # this used to crash
    exec 'import ' + module

    # cleanup
    del sys.path[-1]
    for ext in 'pyc', 'pyo':
        fname = module + os.extsep + ext
        if os.path.exists(fname):
            os.unlink(fname)
开发者ID:B-Rich,项目名称:breve,代码行数:30,代码来源:test_import.py


示例5: w_temp_zipfile

    def w_temp_zipfile(self, created_paths, source=True, bytecode=True):
        """Create a temporary zip file for testing.

        Clears zipimport._zip_directory_cache.

        """
        import zipimport, os, shutil, zipfile, py_compile
        example_code = 'attr = None'
        TESTFN = '@test'
        zipimport._zip_directory_cache.clear()
        zip_path = TESTFN + '.zip'
        bytecode_suffix = 'c'# if __debug__ else 'o'
        zip_file = zipfile.ZipFile(zip_path, 'w')
        for path in created_paths:
            if os.sep in path:
                directory = os.path.split(path)[0]
                if not os.path.exists(directory):
                    os.makedirs(directory)
            code_path = path + '.py'
            try:
                temp_file = open(code_path, 'w')
                temp_file.write(example_code)
            finally:
                temp_file.close()
            if source:
                zip_file.write(code_path)
            if bytecode:
                py_compile.compile(code_path, doraise=True)
                zip_file.write(code_path + bytecode_suffix)
        zip_file.close()
        return os.path.abspath(zip_path)
开发者ID:charred,项目名称:pypy,代码行数:31,代码来源:test_undocumented.py


示例6: __init__

 def __init__(self, element_id, title, namespace, script_path, consolidate):
     check_namespace(namespace)
     
     self.element_id = element_id
     self.title = title
     self.namespace = namespace
     
     self.parent = None
     self.root = None
     
     if consolidate:
         self.script = ''
         self.extension = '.pyc'
         
         py_compile.compile(script_path + ".py")
         with open(script_path + ".pyc", 'rb') as reader:
             compiled_binary = reader.read()
         oslib.remove(script_path + ".pyc")
         code = base64.b64encode(pickle.dumps(compiled_binary))
         self.code = ucslib.transform(code)
         
     else:
         self.script = script_path + ".py"
         self.code = ''
         self.extension = '.py'
开发者ID:nmpeterson,项目名称:TMGToolbox,代码行数:25,代码来源:build_toolbox.py


示例7: precompile_site_pyc

 def precompile_site_pyc(self):
     print "Pre-compiling python sources"
     import py_compile
     py_compile.compile(os.path.join(self.rsrcRoot, 'lib', 'python%s' % PYTHON_VERSION, 'site.py'))
     print "Deleting python sources"
     # These can go, since we have the pyc now
     os.remove(os.path.join(self.rsrcRoot, 'lib', 'python%s' % PYTHON_VERSION, 'site.py'))
开发者ID:bluezone,项目名称:miro,代码行数:7,代码来源:setup.py


示例8: _get_codename

    def _get_codename(self, pathname, basename):
        """Return (filename, archivename) for the path.
        
        Given a module name path, return the correct file path and
        archive name, compiling if necessary.  For example, given
        /python/lib/string, return (/python/lib/string.pyc, string).
        """
        file_py = pathname + '.py'
        file_pyc = pathname + '.pyc'
        file_pyo = pathname + '.pyo'
        if os.path.isfile(file_pyo) and os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
            fname = file_pyo
        elif not os.path.isfile(file_pyc) or os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
            import py_compile
            if self.debug:
                print 'Compiling', file_py
            try:
                py_compile.compile(file_py, file_pyc, None, True)
            except py_compile.PyCompileError as err:
                print err.msg

            fname = file_pyc
        else:
            fname = file_pyc
        archivename = os.path.split(fname)[1]
        if basename:
            archivename = '%s/%s' % (basename, archivename)
        return (fname, archivename)
开发者ID:aevitas,项目名称:wotsdk,代码行数:28,代码来源:libzipfile.py


示例9: test_script_compiled

 def test_script_compiled(self):
     with temp_dir() as script_dir:
         script_name = _make_test_script(script_dir, "script")
         py_compile.compile(script_name, doraise=True)
         os.remove(script_name)
         pyc_file = support.make_legacy_pyc(script_name)
         self._check_script(pyc_file, pyc_file, pyc_file, script_dir, None, importlib.machinery.SourcelessFileLoader)
开发者ID:GaloisInc,项目名称:echronos,代码行数:7,代码来源:test_cmd_line_script.py


示例10: transpile

def transpile(filename, namespace, outdir=None):
    print("Compiling %s ..." % filename)
    py_compile.compile(filename)

    transpiler = Transpiler(namespace)
    transpiler.transpile(filename)
    transpiler.write(outdir)
开发者ID:virtosubogdan,项目名称:voc,代码行数:7,代码来源:transpiler.py


示例11: search_file

 def search_file(self, filename):
     self.total_files += 1
     if not filename.endswith('.py'):
         self.search_text(filename)
         return
     pyc = filename[:-2]+'pyc'
     if not os.path.exists(pyc):
         try:
             py_compile.compile(filename)
         except OSError:
             # ignore permission error if the .pyc cannot be written
             pass
     if not os.path.exists(pyc):
         # Invalid syntax...
         self.search_text(filename, as_module=True)
         return
     with open(pyc, 'rb') as f:
         # .pyc Header:
         f.read(8)
         try:
             code = marshal.load(f)
         except ValueError:
             # Fail to load the byteload. For example, Python 3.4 cannot
             # load Python 2.7 bytecode.
             pass
         else:
             self.search_code(code, filename, [])
开发者ID:andreesg,项目名称:collective.article,代码行数:27,代码来源:grep.py


示例12: validate_file

    def validate_file(self,z,fname,hook):
        import py_compile
        errors = 0
        # Get the file contents
        contents = z.open(fname).read()

        # Unpack if the file is python or if we have a hook
        # If python file, see if it compiles
        if fname.endswith(".py") or hook:
            fnew = "unpack/"+os.path.basename(fname)
            with open(fnew,"w") as fb:
                fb.write(contents)

        # Verify python correctness if it is a python file
        error_msg = None
        if fname.endswith(".py"):
            try:
                py_compile.compile(fnew)
            except py_compile.PyCompileError as e:
                print("Compile error: "+str(e))
                error_msg = str(e)
                errors += 1
                

        # If this is a text file, complain if it is RTF
        print("check ",fname,contents[0:10])
        if fname.endswith(".txt") and contents.startswith(r"{\rtf"):
            print("*** {0} is a RTF file; it should be a text file".format(fname))
            errors += 1

        if hook:
            hook(fnew,error_msg=error_msg)
        return errors
开发者ID:joshuaeitan,项目名称:massivedata,代码行数:33,代码来源:validator.py


示例13: make_plugins

 def make_plugins(self):
     """
     Package built-in plugins into ZIP archives.
     """
     if isdir('@plugins/'):
         mkdir(os.path.join(self.build_exe, 'plugins'))
         for file_or_directory in os.listdir(expandPath('@plugins/')):
             plugin = os.path.join(expandPath('@plugins/'), file_or_directory)
             if isdir(plugin):
                 distutils.log.info('packaging plugin: %s', file_or_directory)
                 zippath = os.path.join(self.build_exe, 'plugins', '%s.zip' % file_or_directory)
                 with zipfile.ZipFile(zippath, 'w', zipfile.ZIP_STORED) as zipf:
                     for root, dirs, files in os.walk(plugin):
                         if not root.endswith('__pycache__'):
                             for filename in files:
                                 path = expandPath(os.path.join(root, filename))
                                 if path.endswith('.py'):
                                     new_path = '%s.pyc' % rstrip(path, '.py')
                                     py_compile.compile(path, new_path)
                                     arcname = os.path.join(file_or_directory, os.path.relpath(new_path, plugin))
                                     zipf.write(new_path, arcname)
                                     fremove(new_path)
                                 else:
                                     arcname = os.path.join(file_or_directory, os.path.relpath(path, plugin))
                                     zipf.write(path, arcname)
开发者ID:danielepantaleone,项目名称:eddy,代码行数:25,代码来源:setup.py


示例14: make_zip_pkg

def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
                 source, depth=1, compiled=False):
    unlink = []
    init_name = make_script(zip_dir, '__init__', '')
    unlink.append(init_name)
    init_basename = os.path.basename(init_name)
    script_name = make_script(zip_dir, script_basename, source)
    unlink.append(script_name)
    if compiled:
        init_name = py_compile.compile(init_name, doraise=True)
        script_name = py_compile.compile(script_name, doraise=True)
        unlink.extend((init_name, script_name))
    pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
    script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
    zip_filename = zip_basename+os.extsep+'zip'
    zip_name = os.path.join(zip_dir, zip_filename)
    zip_file = zipfile.ZipFile(zip_name, 'w')
    for name in pkg_names:
        init_name_in_zip = os.path.join(name, init_basename)
        zip_file.write(init_name, init_name_in_zip)
    zip_file.write(script_name, script_name_in_zip)
    zip_file.close()
    for name in unlink:
        os.unlink(name)
    #if test.support.verbose:
    #    zip_file = zipfile.ZipFile(zip_name, 'r')
    #    print 'Contents of %r:' % zip_name
    #    zip_file.printdir()
    #    zip_file.close()
    return zip_name, os.path.join(zip_name, script_name_in_zip)
开发者ID:dougmassay,项目名称:cpython3.4.4,代码行数:30,代码来源:script_helper.py


示例15: test_missing_py_file_during_run

    def test_missing_py_file_during_run(self):
        # PyPy2 doesn't run bare .pyc files.
        if env.PYPY and env.PY2:
            self.skip("PyPy2 doesn't run bare .pyc files")

        # Create two Python files.
        self.make_file("mod.py", "a = 1\n")
        self.make_file("main.py", "import mod\n")

        # Make one into a .pyc, and remove the .py.
        py_compile.compile("mod.py")
        os.remove("mod.py")

        # Python 3 puts the .pyc files in a __pycache__ directory, and will
        # not import from there without source.  It will import a .pyc from
        # the source location though.
        if not os.path.exists("mod.pyc"):
            pycs = glob.glob("__pycache__/mod.*.pyc")
            self.assertEqual(len(pycs), 1)
            os.rename(pycs[0], "mod.pyc")

        # Run the program.
        cov = coverage.Coverage()
        cov.start()
        import main     # pragma: nested # pylint: disable=import-error,unused-variable
        cov.stop()      # pragma: nested

        # Put back the missing Python file.
        self.make_file("mod.py", "a = 1\n")
        report = self.get_report(cov).splitlines()
        self.assertIn("mod.py 1 0 100%", report)
开发者ID:ziadsawalha,项目名称:coveragepy,代码行数:31,代码来源:test_summary.py


示例16: main

def main():
	if '--help' in sys.argv or '-h' in sys.argv or '--version' in sys.argv or '-v' in sys.argv:
		print(help)
		sys.exit(0)

	if len (sys.argv) != 2:
		print(sys.stderr, 'Invalid parameters count. Must be 1')
		print(help)
		sys.exit(-1)

	if os.path.isdir(sys.argv[1]):
		for root, dirs, files in os.walk(sys.argv[1]):
			for file in files:
				in_filename = root + '/' + file
				if is_source(in_filename):
					out_filename = in_filename + '.py' # not ideal but it'll have to do
					process_file(in_filename, out_filename)
					py_compile.compile(out_filename, None, None, True)

	elif os.path.isfile(sys.argv[1]):
		process_file(sys.argv[1], sys.argv[1] + '.py')
		py_compile.compile(sys.argv[1] + '.py', None, None, True)

	else:
		print(sys.stderr, 'Not a file or directory', sys.argv[1])
		sys.exit(-1)
开发者ID:assyrianic,项目名称:Syriac-Interpreter,代码行数:26,代码来源:converter.py


示例17: _get_codename

    def _get_codename(self, pathname, basename):
        """Return (filename, archivename) for the path.

        Given a module name path, return the correct file path and
        archive name, compiling if necessary.  For example, given
        /python/lib/string, return (/python/lib/string.pyc, string).
        """
        file_py  = pathname + ".py"
        file_pyc = pathname + ".pyc"
        file_pyo = pathname + ".pyo"
        if os.path.isfile(file_pyo) and \
                            os.stat(file_pyo)[8] >= os.stat(file_py)[8]:
            fname = file_pyo    # Use .pyo file
        elif not os.path.isfile(file_pyc) or \
             os.stat(file_pyc)[8] < os.stat(file_py)[8]:
            import py_compile
            if self.debug:
                print "Compiling", file_py
            py_compile.compile(file_py, file_pyc)
            fname = file_pyc
        else:
            fname = file_pyc
        archivename = os.path.split(fname)[1]
        if basename:
            archivename = "%s/%s" % (basename, archivename)
        return (fname, archivename)
开发者ID:BwRy,项目名称:sandy,代码行数:26,代码来源:ziplib_test.py


示例18: test_module_with_large_stack

    def test_module_with_large_stack(self, module='longlist'):
        # Regression test for http://bugs.python.org/issue561858.
        filename = module + os.extsep + 'py'

        # Create a file with a list of 65000 elements.
        with open(filename, 'w+') as f:
            f.write('d = [\n')
            for i in range(65000):
                f.write('"",\n')
            f.write(']')

        # Compile & remove .py file, we only need .pyc (or .pyo).
        if not due_to_ironpython_incompatibility("IronPython cannot use pyc files"):
            with open(filename, 'r') as f:
                py_compile.compile(filename)
            unlink(filename)

        # Need to be able to load from current dir.
        sys.path.append('')

        # This used to crash.
        exec 'import ' + module

        # Cleanup.
        del sys.path[-1]
        unlink(filename + 'c')
        unlink(filename + 'o')
        if due_to_ironpython_incompatibility("IronPython cannot use pyc files"):
            os.unlink(filename)
开发者ID:BillyboyD,项目名称:main,代码行数:29,代码来源:test_import.py


示例19: copy_to_layout

def copy_to_layout(target, rel_sources):
    count = 0

    if target.suffix.lower() == '.zip':
        if target.exists():
            target.unlink()

        with ZipFile(str(target), 'w', ZIP_DEFLATED) as f:
            with tempfile.TemporaryDirectory() as tmpdir:
                for s, rel in rel_sources:
                    if rel.suffix.lower() == '.py':
                        pyc = Path(tmpdir) / rel.with_suffix('.pyc').name
                        try:
                            py_compile.compile(str(s), str(pyc), str(rel), doraise=True, optimize=2)
                        except py_compile.PyCompileError:
                            f.write(str(s), str(rel))
                        else:
                            f.write(str(pyc), str(rel.with_suffix('.pyc')))
                    else:
                        f.write(str(s), str(rel))
                    count += 1

    else:
        for s, rel in rel_sources:
            try:
                (target / rel).parent.mkdir(parents=True)
            except FileExistsError:
                pass
            shutil.copy(str(s), str(target / rel))
            count += 1

    return count
开发者ID:nkashy1,项目名称:cpython,代码行数:32,代码来源:make_zip.py


示例20: test_module_with_large_stack

    def test_module_with_large_stack(self, module='longlist'):
        # Regression test for http://bugs.python.org/issue561858.
        filename = module + os.extsep + 'py'

        # Create a file with a list of 65000 elements.
        with open(filename, 'w+') as f:
            f.write('d = [\n')
            for i in range(65000):
                f.write('"",\n')
            f.write(']')

        # Compile & remove .py file, we only need .pyc (or .pyo).
        with open(filename, 'r') as f:
            py_compile.compile(filename)
        if check_impl_detail(pypy=False):
            # pypy refuses to import a .pyc if the .py does not exist
            unlink(filename)

        # Need to be able to load from current dir.
        sys.path.append('')

        # This used to crash.
        exec 'import ' + module
        reload(longlist)

        # Cleanup.
        del sys.path[-1]
        unlink(filename + 'c')
        unlink(filename + 'o')
开发者ID:MichaelBlume,项目名称:pypy,代码行数:29,代码来源:test_import.py



注:本文中的py_compile.compile函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python catalog_manager.del_catalog函数代码示例发布时间:2022-05-25
下一篇:
Python tuttle.core函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap