我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sys.dont_write_bytecode()。
def byte_compile(self, to_compile): if sys.dont_write_bytecode: self.warn('byte-compiling is disabled, skipping.') return from distutils.util import byte_compile try: # try to make the byte compile messages quieter log.set_verbosity(self.verbose - 1) byte_compile(to_compile, optimize=0, force=1, dry_run=self.dry_run) if self.optimize: byte_compile( to_compile, optimize=self.optimize, force=1, dry_run=self.dry_run, ) finally: log.set_verbosity(self.verbose) # restore original verbosity
def byte_compile(self, files): if sys.dont_write_bytecode: self.warn('byte-compiling is disabled, skipping.') return from distutils.util import byte_compile # Get the "--root" directory supplied to the "install" command, # and use it as a prefix to strip off the purported filename # encoded in bytecode files. This is far from complete, but it # should at least generate usable bytecode in RPM distributions. install_root = self.get_finalized_command('install').root if self.compile: byte_compile(files, optimize=0, force=self.force, prefix=install_root, dry_run=self.dry_run) if self.optimize > 0: byte_compile(files, optimize=self.optimize, force=self.force, prefix=install_root, verbose=self.verbose, dry_run=self.dry_run) # -- Utility methods -----------------------------------------------
def byte_compile(self, files): if sys.dont_write_bytecode: self.warn('byte-compiling is disabled, skipping.') return from distutils.util import byte_compile prefix = self.build_lib if prefix[-1] != os.sep: prefix = prefix + os.sep # XXX this code is essentially the same as the 'byte_compile() # method of the "install_lib" command, except for the determination # of the 'prefix' string. Hmmm. if self.compile: byte_compile(files, optimize=0, force=self.force, prefix=prefix, dry_run=self.dry_run) if self.optimize > 0: byte_compile(files, optimize=self.optimize, force=self.force, prefix=prefix, dry_run=self.dry_run)
def load_plugin(self, file_name): # XAPI plugins run in a py24 environment and may be not compatible with # py34's syntax. In order to prevent unit test scanning the source file # under py34 environment, the plugins will be imported with this # function at run time. plugin_path = self._get_plugin_path() # add plugin path into search path. if plugin_path not in sys.path: sys.path.append(plugin_path) # be sure not to create c files next to the plugins sys.dont_write_bytecode = True name = file_name.split('.')[0] path = os.path.join(plugin_path, file_name) return imp.load_source(name, path)
def byte_compile(self, to_compile): if sys.dont_write_bytecode: self.warn('byte-compiling is disabled, skipping.') return from distutils.util import byte_compile try: # try to make the byte compile messages quieter log.set_verbosity(self.verbose - 1) byte_compile(to_compile, optimize=0, force=1, dry_run=self.dry_run) if self.optimize: byte_compile( to_compile, optimize=self.optimize, force=1, dry_run=self.dry_run ) finally: log.set_verbosity(self.verbose) # restore original verbosity
def main(import_): __builtins__.__import__ = import_ benchmarks = (from_cache, builtin_mod, source_using_bytecode, source_wo_bytecode, source_writing_bytecode, decimal_using_bytecode, decimal_writing_bytecode, decimal_wo_bytecode,) seconds = 1 seconds_plural = 's' if seconds > 1 else '' repeat = 3 header = "Measuring imports/second over {} second{}, best out of {}\n" print(header.format(seconds, seconds_plural, repeat)) for benchmark in benchmarks: print(benchmark.__doc__, "[", end=' ') sys.stdout.flush() results = [] for result in benchmark(seconds=seconds, repeat=repeat): results.append(result) print(result, end=' ') sys.stdout.flush() assert not sys.dont_write_bytecode print("]", "best is", format(max(results), ',d'))
def _check_module(self, depth): pkg_dir, mod_fname, mod_name = ( self._make_pkg("x=1\n", depth)) forget(mod_name) try: if verbose: print "Running from source:", mod_name d1 = run_module(mod_name) # Read from source self.assertIn("x", d1) self.assertTrue(d1["x"] == 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) if not sys.dont_write_bytecode: if verbose: print "Running from compiled:", mod_name d2 = run_module(mod_name) # Read from bytecode self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print "Module executed successfully"
def _check_package(self, depth): pkg_dir, mod_fname, mod_name = ( self._make_pkg("x=1\n", depth, "__main__")) pkg_name, _, _ = mod_name.rpartition(".") forget(mod_name) try: if verbose: print "Running from source:", pkg_name d1 = run_module(pkg_name) # Read from source self.assertIn("x", d1) self.assertTrue(d1["x"] == 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) if not sys.dont_write_bytecode: if verbose: print "Running from compiled:", pkg_name d2 = run_module(pkg_name) # Read from bytecode self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose: print "Package executed successfully"