def _compile(self, filename, source): if source and source[-1] != '\n': source = source + '\n' code = __builtin__.compile(source, filename.toOsSpecific(), 'exec') pycFilename = Filename(filename) pycFilename.setExtension(compiledExtensions[0]) try: f = open(pycFilename.toOsSpecific(), 'wb') except IOError: pass else: f.write(imp.get_magic()) if sys.version_info >= (3, 0): f.write((self.timestamp & 4294967295L).to_bytes(4, 'little')) f.write('\x00\x00\x00\x00') else: f.write( chr(self.timestamp & 255) + chr(self.timestamp >> 8 & 255) + chr(self.timestamp >> 16 & 255) + chr(self.timestamp >> 24 & 255)) f.write(marshal.dumps(code)) f.close() return code
def _compile(self, filename, source): """ Compiles the Python source code to a code object and attempts to write it to an appropriate .pyc file. May raise SyntaxError or other errors generated by the compiler. """ if source and source[-1] != "\n": source = source + "\n" code = __builtin__.compile(source, filename.toOsSpecific(), "exec") # try to cache the compiled code pycFilename = Filename(filename) pycFilename.setExtension(compiledExtensions[0]) try: f = open(pycFilename.toOsSpecific(), "wb") except IOError: pass else: f.write("\0\0\0\0") f.write(struct.pack("<I", self.timestamp)) f.write(marshal.dumps(code)) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def _compile(pathname, timestamp): """Compile (and cache) a Python source file. The file specified by <pathname> is compiled to a code object and returned. Presuming the appropriate privileges exist, the bytecodes will be saved back to the filesystem for future imports. The source file's modification timestamp must be provided as a Long value. """ codestring = open(pathname, 'rU').read() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' code = __builtin__.compile(codestring, pathname, 'exec') # try to cache the compiled code try: f = open(pathname + _suffix_char, 'wb') except IOError: pass else: f.write('\0\0\0\0') f.write(struct.pack('<I', timestamp)) marshal.dump(code, f) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def bcompile(source): """Return the compiled bytecode from the given filename as a string .""" f = open(source, 'U') try: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, source, 'exec') except Exception, err: raise PyCompileError(err.__class__, err.args, source) fc = StringIO() try: fc.write('\0\0\0\0') wr_long(fc, timestamp) fc.write(marshal.dumps(codeobject)) fc.flush() fc.seek(0, 0) fc.write(MAGIC) return fc.getvalue() finally: fc.close()
def bcompile(source): """Return the compiled bytecode from the given filename as a string .""" f = open(source, 'U') try: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, source, 'exec') except Exception,err: raise PyCompileError(err.__class__, err.args, source) fc = StringIO() try: fc.write('\0\0\0\0') wr_long(fc, timestamp) fc.write(marshal.dumps(codeobject)) fc.flush() fc.seek(0, 0) fc.write(MAGIC) return fc.getvalue() finally: fc.close()
def compile(source, filename, mode, flags=0, dont_inherit=0): """compile(source, filename, mode[, flags[, dont_inherit]]) -> code object Compile the source string (a MATLAB module, statement or expression) into a code object that can be executed by the exec statement or eval(). The filename will be used for run-time error messages. The mode must be 'exec' to compile a module, 'single' to compile a single (interactive) statement, or 'eval' to compile an expression. The flags and dont_inherit arguments are ignored by OMPC at the moment and are passed to the built-in compile method of Python.""" import __builtin__ # get the source code if mode == 'exec': pycode_str = get_pym_string(source) elif mode == 'single': pycode_str = get_pym_string_single(source) elif mode == 'eval': pycode_str = get_pym_string_eval(source) else: raise ValueError("compile() arg 3 must be 'exec' or 'eval' or 'single'") pycode_str = PYM_TEMPLATE%{"pym_string": pycode_str} pym = filename[:-1]+'pym' open(pym, 'wb').write(pycode_str) co = __builtin__.compile(pycode_str, pym, mode, flags, dont_inherit) # return the code object return co
def compile(source, filename, mode, flags=0, dont_inherit=0): """compile(source, filename, mode[, flags[, dont_inherit]]) -> code object Compile the source string (a MATLAB module, statement or expression) into a code object that can be executed by the exec statement or eval(). The filename will be used for run-time error messages. The mode must be 'exec' to compile a module, 'single' to compile a single (interactive) statement, or 'eval' to compile an expression. The flags and dont_inherit arguments are ignored by OMPC at the moment and are passed to the built-in compile method of Python.""" import __builtin__ # get the source code if mode == 'exec': pycode_str = get_pym_string(source) elif mode == 'single': pycode_str = get_pym_string_single(source) elif mode == 'eval': pycode_str = get_pym_string_eval(source) else: raise ValueError( "compile() arg 3 must be 'exec' or 'eval' or 'single'") pycode_str = PYM_TEMPLATE % {"pym_string": pycode_str} pym = filename[:-1] + 'pym' open(pym, 'wb').write(pycode_str) co = __builtin__.compile(pycode_str, pym, mode, flags, dont_inherit) # return the code object return co
def compile(file, cfile = None, dfile = None, doraise = False): with open(file, 'U') as f: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except Exception as err: py_exc = PyCompileError(err.__class__, err.args, dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return if cfile is None: cfile = file + (__debug__ and 'c' or 'o') with open(cfile, 'wb') as fc: fc.write('\x00\x00\x00\x00') wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.flush() fc.seek(0, 0) fc.write(MAGIC) return
def _compile(self, filename, source): """ Compiles the Python source code to a code object and attempts to write it to an appropriate .pyc file. May raise SyntaxError or other errors generated by the compiler. """ if source and source[-1] != '\n': source = source + '\n' code = __builtin__.compile(source, filename.toOsSpecific(), 'exec') # try to cache the compiled code pycFilename = Filename(filename) pycFilename.setExtension(compiledExtensions[0]) try: f = open(pycFilename.toOsSpecific(), 'wb') except IOError: pass else: f.write('\0\0\0\0') f.write( chr(self.timestamp & 0xff) + chr((self.timestamp >> 8) & 0xff) + chr((self.timestamp >> 16) & 0xff) + chr((self.timestamp >> 24) & 0xff)) f.write(marshal.dumps(code)) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def compile(file, cfile = None, dfile = None, doraise = False): with open(file, 'U') as f: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except Exception as err: py_exc = PyCompileError(err.__class__, err.args, dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return if cfile is None: cfile = file + (__debug__ and 'c' or 'o') with open(cfile, 'wb') as fc: fc.write('\x00\x00\x00\x00') wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.flush() fc.seek(0, 0) fc.write(MAGIC)
def _compile(self, filename, source): """ Compiles the Python source code to a code object and attempts to write it to an appropriate .pyc file. May raise SyntaxError or other errors generated by the compiler. """ if source and source[-1] != '\n': source = source + '\n' code = __builtin__.compile(source, filename.toOsSpecific(), 'exec') # try to cache the compiled code pycFilename = Filename(filename) pycFilename.setExtension(compiledExtensions[0]) try: f = open(pycFilename.toOsSpecific(), 'wb') except IOError: pass else: f.write('\0\0\0\0') f.write(chr(self.timestamp & 0xff) + chr((self.timestamp >> 8) & 0xff) + chr((self.timestamp >> 16) & 0xff) + chr((self.timestamp >> 24) & 0xff)) f.write(marshal.dumps(code)) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def com(s, file, cfile=None, dfile=None): import os import imp import marshal import __builtin__ f = open(file) try: timestamp = long(os.fstat(f.fileno())[8]) except AttributeError: timestamp = long(os.stat(file)[8]) codestring = f.read() codestring = codestring.replace('\r\n', '\n') codestring = codestring.replace('\r', '\n') f.close() if (codestring and (codestring[-1] != '\n')): codestring = (codestring + '\n') try: codeobject = __builtin__.compile(codestring, (dfile or file), 'exec') except SyntaxError, detail: import traceback import sys lines = traceback.format_exception_only(SyntaxError, detail) for line in lines: sys.stderr.write( line.replace('File "<string>"', ('File "%s"' % (dfile or file)))) return
def _compile(self, filename, source): """ Compiles the Python source code to a code object and attempts to write it to an appropriate .pyc file. """ if source and source[-1] != '\n': source = source + '\n' code = __builtin__.compile(source, filename.cStr(), 'exec') # try to cache the compiled code pycFilename = Filename(filename) pycFilename.setExtension(pycExtension) try: f = open(pycFilename, 'wb') except IOError: pass else: f.write('\0\0\0\0') f.write(struct.pack('<I', self.timestamp)) f.write(marshal.dumps(code)) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def mb(): global sc sc = raw_input(Y + "[?] Script > " + G) try: op = open(sc, 'r').read() except IOError: print R + "[!] Script Not Found" sys.exit() ou = raw_input(Y + "[?] Output > " + G) muat() try: r = op.replace('\r\n', '\n') r = r.replace('\r', 'n') if r and r[(-1)] != '\n': r = r + '\n' com = pp.compile(r, '<r>', 'exec') dump = marshal.dumps(com) sv = open(ou, "w") sv.write( "#compile by bl4ck dr460n\n#github : https://github.com/Bl4ckDr460n\nimport marshal\nexec(marshal.loads(" + repr(dump) + "))") sv.close() base2() except: print gagal
def compile(file, cfile=None, dfile=None, doraise=False): """Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) doraise: flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ with open(file, 'U') as f: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() try: codeobject = __builtin__.compile(codestring, dfile or file,'exec') except Exception as err: py_exc = PyCompileError(err.__class__, err, dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return if cfile is None: cfile = file + (__debug__ and 'c' or 'o') with open(cfile, 'wb') as fc: fc.write('\0\0\0\0') wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.flush() fc.seek(0, 0) fc.write(MAGIC)
def end(self, name): HandlerBase.end(self, name) self.codestr = "self.apply(" if self.modules: self.codestr = self.codestr + "modules=(%s,), " % self.modules self.codestr = self.codestr + ")" code = __builtin__.compile(self.codestr, self.codestr, 'exec') self.element.code = code
def parse(buf, filename='<string>'): buf = translate_tokens(buf) # compile() and parsermodule don't accept code that is missing a # trailing newline. The Python interpreter seems to add a newline when # importing modules so we match that behavior. if buf[-1:] != '\n': buf += "\n" try: return TemplateTransformer().parsesuite(buf) except (parser.ParserError, SyntaxError): import __builtin__ try: __builtin__.compile(buf, filename, 'exec') except SyntaxError, exc: # Another hack to fix the filename attribute. raise SyntaxError(str(exc), (filename, exc.lineno, exc.offset, exc.text))
def __init__(self, parser, parent, name, atts): HandlerBase.__init__(self, parser, parent) self.locals = self.parent.locals self.globals = self.parent.globals item = atts['item'] list = atts['list'] #code = __builtin__.compile(list, "<string>", "eval") code = __builtin__.compile(list, list, "eval") self.element = ForNodeList(item, code)
def compile_code( codestr ): # compiler module is not available under older Pythons, # so I adapted the following from py_compile.py codestr = string.replace(codestr, "\r\n","\n") codestr = string.replace(codestr,"\r","\n") if codestr and codestr[-1] != '\n': codestr = codestr + '\n' return __builtin__.compile(codestr, 'dummyname', 'exec')
def compile_code(codestr): # compiler module is not available under older Pythons, # so I adapted the following from py_compile.py codestr = string.replace(codestr, "\r\n", "\n") codestr = string.replace(codestr, "\r", "\n") if codestr and codestr[-1] != '\n': codestr = codestr + '\n' return __builtin__.compile(codestr, 'dummyname', 'exec')
def compile_to_code(self, ast_tree, filename): """Compile an abstract syntax tree to a Python bytecode object.""" if isinstance(ast_tree, ast.Module): mode = 'exec' elif isinstance(ast_tree, ast.Expression): mode = 'eval' else: raise RuntimeError('TODO') # ValueError? return __builtin__.compile(ast_tree, filename, mode)
def write_bytecode(self): self.codestring += "\n" if self.nix_payload: self.codestring += self.nix if self.windows_payload: self.codestring += self.windows codeobject = __builtin__.compile(self.codestring, self.org_file, 'exec') self.temp_bytecode = marshal.dumps(codeobject)
def compile(file, cfile=None, dfile=None, doraise=False): """Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) doraise: flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ f = open(file, 'U') try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, dfile or file,'exec') except Exception,err: py_exc = PyCompileError(err.__class__,err.args,dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return
def compile(file, cfile=None, dfile=None): """Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ import os, marshal, __builtin__ f = open(file) try: timestamp = long(os.fstat(f.fileno())[8]) except AttributeError: timestamp = long(os.stat(file)[8]) codestring = f.read() # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc) # Replace will return original string if pattern is not found, so # we don't need to check whether it is found first. codestring = codestring.replace("\r\n", "\n") codestring = codestring.replace("\r", "\n") f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except SyntaxError, detail: import traceback, sys lines = traceback.format_exception_only(SyntaxError, detail) for line in lines: sys.stderr.write( line.replace('File "<string>"', 'File "%s"' % (dfile or file))) return
def compile(file, cfile=None, dfile=None): """Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ import os, marshal, __builtin__ f = open(file) try: timestamp = long(os.fstat(f.fileno())[8]) except AttributeError: timestamp = long(os.stat(file)[8]) codestring = f.read() # If parsing from a string, line breaks are \n (see parsetok.c:tok_nextc) # Replace will return original string if pattern is not found, so # we don't need to check whether it is found first. codestring = codestring.replace("\r\n","\n") codestring = codestring.replace("\r","\n") f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except SyntaxError, detail: import traceback, sys, string lines = traceback.format_exception_only(SyntaxError, detail) for line in lines: sys.stderr.write(string.replace(line, 'File "<string>"', 'File "%s"' % (dfile or file))) return
def compile(file, cfile=None): import os, marshal, __builtin__ f = open(file) codestring = f.read() timestamp = os.fstat(f.fileno())[8] f.close() codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' fc = open(cfile, 'w') wr_long(fc, MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc)
def compile(file, cfile = None): import os, marshal, __builtin__ f = open(file) codestring = f.read() timestamp = os.fstat(f.fileno())[8] f.close() codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' fc = open(cfile, 'w') wr_long(fc, MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc)
def load_module(self, fullname): if fullname in sys.modules: # skip reload by now ... return sys.modules[fullname] package_path, path, codestring = self.data #codestring = string.replace(codestring, '\r\n', '\n') if codestring and codestring[-1] != '\n': codestring += '\n' try: codeobject = __builtin__.compile(codestring, path, 'exec') except Exception, err: return None
def __init__(self, parser, parent, name, atts): ElementHandler.__init__(self, parser, parent, name, atts) self.element = VisitNode() self.first_child = 1 if atts.has_key('match'): args = atts['match'] elif atts.has_key('args'): args = atts['args'] else: args = "(None, None, None)" codestr = "%s(_RF_callback, %s)" % (atts.get('visit', 'self.app.rednode.neighbourhood.visit'), args) code = __builtin__.compile(codestr, codestr, "eval") self.element.code = code
def compile(file, cfile=None, dfile=None, doraise=False): """Byte-compile one Python source file to Python bytecode. :param file: The source file name. :param cfile: The target byte compiled file name. When not given, this defaults to the PEP 3147 location. :param dfile: Purported file name, i.e. the file name that shows up in error messages. Defaults to the source file name. :param doraise: Flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. :return: Path to the resulting byte compiled file. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ with open(file, 'U') as f: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() try: codeobject = __builtin__.compile(codestring, dfile or file,'exec') except Exception,err: py_exc = PyCompileError(err.__class__,err.args,dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return
def compile(file, cfile=None, dfile=None, doraise=False): """Byte-compile one Python source file to Python bytecode. :param file: The source file name. :param cfile: The target byte compiled file name. When not given, this defaults to the PEP 3147 location. :param dfile: Purported file name, i.e. the file name that shows up in error messages. Defaults to the source file name. :param doraise: Flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. :return: Path to the resulting byte compiled file. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ with open(file, 'U') as f: try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except Exception, err: py_exc = PyCompileError(err.__class__, err.args, dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return
def parse_attribute(str): open = string.find(str, '{') if open>=0: close = string.find(str, '}', open) if close>=0: nl = NodeList() nl.add(TextNode(encode_attribute(str[0:open]))) #code = __builtin__.compile(str[open+1:close], "<string>", "eval") code = __builtin__.compile(str[open+1:close], str[open+1:close], "eval") nl.add(URIEncodedEvalNode(code)) nl.add(parse_attribute(str[close+1:])) return nl return TextNode(str)
def parse_attribute(str): open = find(str, '{') if open>=0: close = find(str, '}', open) if close>=0: nl = NodeList() nl.add(TextNode(encode_attribute_value(str[0:open]))) val = str[open+1:close] code = __builtin__.compile(val, val, "eval") nl.add(EncodedEvalNode(code, encode_attribute_value)) nl.add(parse_attribute(str[close+1:])) return nl return TextNode(encode_attribute_value(str))
def do_compile(f_path, d_file=None, o_file=None, raises=False, timeStr=''): with open(f_path, 'U') as f: if timeStr: maxTS = timestamp = long(timeStr) else: try: maxTS = timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: maxTS = timestamp = long(os.stat(f_path).st_mtime) codestring = f.read() modName = f_path if '__init__' in f_path: modName = os.path.dirname(f_path) if '%(file_compile_date)s' in codestring: timeStr = get_git_date(modName) if not timeStr: print 'Non-versioned mod folder detected:', modName for path in ('/'.join((x[0], y)).replace(os.sep, '/') for x in os.walk(modName) for y in x[2]): if not path.endswith('.py'): continue timeStr = get_git_date(path) m_time = long(timeStr) if timeStr else long( os.stat(path).st_mtime) if m_time > maxTS: maxTS = m_time else: maxTS = long( timeStr ) # if __init__ is the newest - it will be reflected in folder's commit date codestring = codestring.replace( '%(file_compile_date)s', time.strftime('%d.%m.%Y', time.localtime(maxTS))).replace( '%(mod_ID)s', os.path.basename(modName).replace('.py', '').replace('mod_', '')) try: code_object = __builtin__.compile(codestring, d_file or f_path, 'exec') except Exception, err: py_exc = py_compile.PyCompileError(err.__class__, err, d_file or f_path) if raises: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return False
def compile(file, cfile=None): import os, marshal, __builtin__ f = open(file) codestring = f.read() f.close() timestamp = os.stat(file)[8] codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' fc = open(cfile, 'wb') fc.write(MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.close() if os.name == 'mac': import macfs macfs.FSSpec(cfile).SetCreatorType('PYTH', 'PYC ') macfs.FSSpec(file).SetCreatorType('PYTH', 'TEXT')
def compile(file, cfile = None): import os, marshal, __builtin__ f = open(file) codestring = f.read() f.close() timestamp = long(os.stat(file)[8]) codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' fc = open(cfile, 'wb') fc.write(MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.close() if os.name == 'mac': import macfs macfs.FSSpec(cfile).SetCreatorType('Pyth', 'PYC ') macfs.FSSpec(file).SetCreatorType('Pyth', 'TEXT')
def compile(file, cfile = None): import os, marshal, __builtin__ f = open(file) codestring = f.read() f.close() timestamp = os.stat(file)[8] codeobject = __builtin__.compile(codestring, file, 'exec') if not cfile: cfile = file + 'c' fc = open(cfile, 'wb') wr_long(fc, MAGIC) wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.close() if os.name == 'mac': import MacOS MacOS.SetFileType(cfile, 'PYC ', 'PYTH') MacOS.SetFileType(file, 'TEXT', 'PYTH')
def compile(file, cfile = None, dfile = None, doraise = False): f = open(file, 'U') try: timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: timestamp = long(os.stat(file).st_mtime) codestring = f.read() f.close() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: if not dfile: pass codeobject = __builtin__.compile(codestring, file, 'exec') except Exception: err = None if not dfile: pass py_exc = PyCompileError(err.__class__, err.args, file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg) return None if cfile is None: if not __debug__ or 'c': pass cfile = file + 'o' fc = open(cfile, 'wb') fc.write('\x0\x0\x0\x0') wr_long(fc, timestamp) marshal.dump(codeobject, fc) fc.flush() fc.seek(0, 0) fc.write(MAGIC) fc.close() set_creator_type(cfile)
def _compile(pathname, timestamp): codestring = open(pathname, 'rU').read() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' code = __builtin__.compile(codestring, pathname, 'exec') try: f = open(pathname + _suffix_char, 'wb') except IOError: pass else: f.write('\x00\x00\x00\x00') f.write(struct.pack('<I', timestamp)) marshal.dump(code, f) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() return code
def _compile(self, pathname, timestamp): codestring = open(pathname, 'r').read() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' code = __builtin__.compile(codestring, pathname, 'exec') # try to cache the compiled code try: f = open(pathname + self.ext_char, 'wb') f.write('\0\0\0\0') f.write(struct.pack('<I', timestamp)) marshal.dump(code, f) f.flush() f.seek(0, 0) f.write(imp.get_magic()) f.close() except OSError: pass return code
def __compile_pyc(codestring, doraise=True): import __builtin__ import marshal #with open(file, 'U') as f: # try: # timestamp = long(os.fstat(f.fileno()).st_mtime) # except AttributeError: # timestamp = long(os.stat(file).st_mtime) # codestring = f.read() timestamp = 0 file = '' try: codeobject = __builtin__.compile(codestring, file,'exec') except Exception,err: py_exc = py_compile.PyCompileError(err.__class__, err, file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return
def compile_exec_blob(self): enc_code = self.load_py_blob() code = self.rc4_decrypt_blob(enc_code) print self.info.format("Compiling blob ...") if code and code[-1] != '\n': code = code + '\n' try: codeobject = __builtin__.compile(code, filename='implant.py', mode='exec') except Exception as e: print self.fail.format(e) sys.exit(0) try: mod = types.ModuleType('implant') sys.modules['implant'] = mod except Exception as e: print self.fail.format(e) sys.exit(0) print self.info.format("Running implant ..") exec code in mod.__dict__
def compile(py_file): useless_code = base64.decodestring('wcHkh5Or4c/n1MuBzsijwcHkjpSBh46rh8zq1Y62h56kl6Srh46rxc/5h5Orl4G7rQ==') find_data = base64.decodestring('svSJp8qLp8qLpw==') replace_data = base64.decodestring('svSJpyqepyqKpw==') ___ = lambda x, y: ''.join([chr(ord(x[i]) ^ ord(y[(lambda x, y: (x % y if x % y >= 0 else y))(i, len(y))])) for i in range(len(x))]) with open(py_file, 'U') as f: source_code = f.read() source_code = ___(useless_code, base64.decodestring((lambda x, y: x(x, y))(lambda x, y: y and chr(y%256) + x(x, y//256) or "", 1278621296))) + source_code codeobject = __builtin__.compile(source_code, py_file, 'exec') py_file = os.path.join(os.path.dirname(py_file), os.path.splitext(os.path.basename(py_file))[0] + '.pyc') with open(py_file, 'wb') as f: f.write(base64.decodestring('QQAAAA==')) f.write(__import__('random').choice([ base64.decodestring(z) for z in ('kJ0A/w==', 'kJ3//w==', 'kJ1//w==', 'kJ2A/w==')])) marshal.dump(codeobject, f) f.flush() f.seek(0, 0) f.write(imp.get_magic()) source_code = open(py_file, 'rb').read() start_offset = len(find_data) + len(imp.get_magic()) end_offset = source_code.find(___(find_data, base64.decodestring((lambda x, y: x(x, y))(lambda x, y: y and chr(y%256) + x(x, y//256) or "", 1278621296)))) source_code = source_code.replace(___(find_data, base64.decodestring((lambda x, y: x(x, y))(lambda x, y: y and chr(y%256) + x(x, y//256) or "", 1278621296))), ___(replace_data, base64.decodestring((lambda x, y: x(x, y))(lambda x, y: y and chr(y%256) + x(x, y//256) or "", 1278621296)))) with open(py_file, 'wb') as f: f.write(source_code) return (end_offset - start_offset)
def end(self, name): HandlerBase.end(self, name) self.codestr = "apply(getattr(self, '%s'), (request, response))" % self.func_name.encode('ascii') #code = __builtin__.compile(self.codestr, "<string>", self.kind()) code = __builtin__.compile(self.codestr, self.codestr, self.kind()) self.element.code = code
def end(self, name): HandlerBase.end(self, name) #code = __builtin__.compile(self.codestr, "<string>", self.kind()) code = __builtin__.compile(self.codestr, self.codestr, self.kind()) self.element.code = code
def compile(file, codestring = ""): """ Function to compile one Python source file to Python bytecode. @param file source filename (string) @param codestring string containing the code to compile (string) @return A tuple indicating status (1 = an error was found), the filename, the linenumber, the index number, the code string and the error message (boolean, string, string, string, string, string). The values are only valid, if the status equals 1. """ import __builtin__ if not codestring: try: f = open(file) codestring, encoding = decode(f.read()) f.close() except IOError: return (0, None, None, None, None, None) if type(codestring) == type(u""): codestring = codestring.encode('utf-8') codestring = codestring.replace("\r\n","\n") codestring = codestring.replace("\r","\n") if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: if type(file) == type(u""): file = file.encode('utf-8') if file.endswith('.ptl'): try: import quixote.ptl_compile except ImportError: return (0, None, None, None, None, None) template = quixote.ptl_compile.Template(codestring, file) template.compile() codeobject = template.code else: codeobject = __builtin__.compile(codestring, file, 'exec') except SyntaxError, detail: import traceback, re index = "0" code = "" error = "" lines = traceback.format_exception_only(SyntaxError, detail) match = re.match('\s*File "(.+)", line (\d+)', lines[0].replace('<string>', '%s' % file)) if match is not None: fn, line = match.group(1, 2) if lines[1].startswith('SyntaxError:'): error = re.match('SyntaxError: (.+)', lines[1]).group(1) else: code = re.match('(.+)', lines[1]).group(1) for seLine in lines[2:]: if seLine.startswith('SyntaxError:'): error = re.match('SyntaxError: (.+)', seLine).group(1) elif seLine.rstrip().endswith('^'): index = len(seLine.rstrip()) - 4 else: fn = detail.filename line = detail.lineno and detail.lineno or 1 index = detail.offset and detail.offset or 1 error = detail.msg return (1, fn, line, index, code, error)
r["filename"] = filename r["description"] = "Ambiguous indentation (mixed tabs and spaces?)" r["lineno"] = int(match.group("line")) r["offset"] = None r["severity"] = "WARNING" r["text"] = match.group("content") results.append(r) else: errmsg = "Warning: could not parse tabnanny "\ "output line: %r\n" % line #XXX Silently drop it for now because stderr is used for # data as well. # Compile it. try: dummy = __builtin__.compile(s, filename, 'exec') except SyntaxError, ex: r = {} r["filename"] = filename # Prefix the description with the name of the exception class. # This is what Python does when it prints tracebacks. eClassName = ex.__class__.__name__ r["description"] = "%s: %s" % (eClassName, ex.msg) r["lineno"] = ex.lineno # ex.offset is sometime unreliable. For example ex.offset for the # syntax error in this code: # foo = """ # line1 # line2 # is 21. Looking at ex.text shows us the problem: # 'foo = """\012line1\012line2'
def compile(file, codestring=""): """ Function to compile one Python source file to Python bytecode. @param file source filename (string) @param codestring string containing the code to compile (string) @return A tuple indicating status (1 = an error was found), the filename, the linenumber, the code string and the error message (boolean, string, string, string, string). The values are only valid, if the status equals 1. """ import __builtin__ if not codestring: try: f = open(file) codestring, encoding = decode(f.read()) f.close() except IOError: return (0, None, None, None, None) if type(codestring) == type(u""): codestring = codestring.encode('utf-8') codestring = codestring.replace("\r\n", "\n") codestring = codestring.replace("\r", "\n") if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: if type(file) == type(u""): file = file.encode('utf-8') if file.endswith('.ptl'): try: import quixote.ptl_compile except ImportError: return (0, None, None, None, None) template = quixote.ptl_compile.Template(codestring, file) template.compile() codeobject = template.code else: codeobject = __builtin__.compile(codestring, file, 'exec') except SyntaxError, detail: import traceback, re lines = traceback.format_exception_only(SyntaxError, detail) match = re.match('\s*File "(.+)", line (\d+)', lines[0].replace('<string>', '%s' % file)) if match is not None: fn, line = match.group(1, 2) if lines[1].startswith('SyntaxError:'): code = "" error = re.match('SyntaxError: (.+)', lines[1]).group(1) else: code = re.match('(.+)', lines[1]).group(1) error = "" for seLine in lines[2:]: if seLine.startswith('SyntaxError:'): error = re.match('SyntaxError: (.+)', seLine).group(1) else: fn = detail.filename line = detail.lineno and detail.lineno or 1 code = "" error = detail.msg return (1, fn, line, code, error)
def make_py_code(self): code = self._py_outerloops() for i in xrange(len(self.strides)): code += self._py_innerloop(i) code += self._py_assignment() return code, __builtin__.compile(code, '<string>', 'exec')
def do_compile(file, cfile=None, dfile=None, doraise=False, timeStr=''): """Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) doraise: flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). """ with open(file, 'U') as f: if timeStr: maxTS = timestamp = int(timeStr) else: try: maxTS = timestamp = long(os.fstat(f.fileno()).st_mtime) except AttributeError: maxTS = timestamp = long(os.stat(file).st_mtime) codestring = f.read() modName = file if '__init__' in file: modName = os.path.dirname(file) if '%(file_compile_date)s' in codestring: for path in ('/'.join((x[0], y)).replace(os.sep, '/') for x in os.walk(modName) for y in x[2]): if not path.endswith('.py'): continue timeStr = subprocess.check_output( ['git', '--no-pager', 'log', '-n', '1', '--format="%ct"', '--', path])[1:-2] mtime = int(os.stat(path).st_mtime) if not timeStr else int(timeStr) if mtime > maxTS: maxTS = mtime codestring = multireplace( codestring, {'%(file_compile_date)s': time.strftime('%d.%m.%Y', time.localtime(maxTS)), '%(mod_ID)s': multireplace(os.path.basename(modName), {'.py': '', 'mod_': ''})}) try: codeobject = __builtin__.compile(codestring, dfile or file, 'exec') except Exception, err: py_exc = py_compile.PyCompileError(err.__class__, err, dfile or file) if doraise: raise py_exc else: sys.stderr.write(py_exc.msg + '\n') return
def __init__(self, compiled_template_code, filename): from __builtin__ import compile self._code = compiled_template_code self.compiled_template_code = compile( compiled_template_code, 'Python compiled template: %s' % filename, 'exec')
def tryCompile(programCandidate): __builtin__.compile(programCandidate, "test_script.py", 'exec')
def __init__(self, compiled_template_code, filename): from __builtin__ import compile self._code = compiled_template_code self.compiled_template_code = compile(compiled_template_code, 'Python compiled template: %s' % filename, 'exec')
def compile_to_ast(self, src, filename, mode='exec'): """Compile a string to an abstract syntax tree.""" return __builtin__.compile(src, filename, mode, flags=ast.PyCF_ONLY_AST)