def stringify(path, nt=False): if isinstance(path, String): path = parse(path.string) if not isinstance(path, Path): raise OldError(u"expected a path object") prefix = path.prefix if isinstance(prefix, URLPrefix): string = prefix.protocol if len(string) > 0: string += u":" if nt and string == u"": string += ur"\\" + prefix.domain + u"\\" else: string += ur"//" + prefix.domain if len(path.pathseq) > 0: string += u"/" string += stringify_pathseq(path.pathseq, nt) return rstring.assert_str0(string) elif isinstance(prefix, PosixPrefix): if prefix.is_absolute: string = stringify_pathseq([u""] + path.pathseq, nt) elif len(path.pathseq) > 0 and path.pathseq[0].count(u":") > 0: string = stringify_pathseq([u"."] + path.pathseq, nt) else: string = stringify_pathseq(path.pathseq, nt) if string == u"": string = u"." if prefix.label != u"": string = prefix.label + u":" + string return rstring.assert_str0(string) else: raise OldError(u"custom prefix passed to stringification [corruption]")
def _posix_rnormpath(path): """Normalize path, eliminating double slashes, etc.""" slash, dot = '/', '.' assert_str0(dot) if path == '': return dot initial_slashes = path.startswith('/') # POSIX allows one or two initial slashes, but treats three or more # as single slash. if (initial_slashes and path.startswith('//') and not path.startswith('///')): initial_slashes = 2 comps = path.split('/') new_comps = [] for comp in comps: if comp == '' or comp == '.': continue if (comp != '..' or (not initial_slashes and not new_comps) or (new_comps and new_comps[-1] == '..')): new_comps.append(comp) elif new_comps: new_comps.pop() comps = new_comps path = slash.join(comps) if initial_slashes: path = slash * initial_slashes + path assert_str0(path) return path or dot
def build_path_args(path, args): pathname = pathobj.to_path(path) path = pathobj.os_stringify(pathname).encode('utf-8') if '\x00' in path: raise OldError(u"NUL byte in spawnv path string") argv = [] for arg in args.contents: if isinstance(arg, pathobj.Path): a = pathobj.os_stringify(arg).encode('utf-8') else: a = as_cstring(arg) if '\x00' in a: raise OldError(u"NUL byte in spawnv arg string") argv.append(rstring.assert_str0(a)) return rstring.assert_str0(path), argv
def get_code(self, space, w_fullname): fullname = space.text_w(w_fullname) filename = self.make_filename(fullname) for compiled, _, ext in ENUMERATE_EXTS: if '\x00' in filename: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case break filename = assert_str0(filename) if self.have_modulefile(space, filename + ext): w_source = self.get_data(space, filename + ext) source = space.bytes_w(w_source) if compiled: if len(source) < 16: raise oefmt(get_error(space), "bad pyc data") magic = importing._get_long(source[:4]) if not self.can_use_pyc(space, filename + ext, magic, source): continue # zipimport ignores the size field w_code = importing.read_compiled_module( space, filename + ext, source[16:]) else: co_filename = self.make_co_filename(filename + ext) w_code = importing.parse_source_module( space, co_filename, source) return w_code raise oefmt(get_error(space), "Cannot find source or code for %R in %R", w_fullname, space.newfilename(self.name))
def get_L2cache_linux2_sparc(): debug_start("gc-hardware") cpu = 0 L2cache = sys.maxint while True: try: fd = os.open('/sys/devices/system/cpu/cpu' + assert_str0(str(cpu)) + '/l2_cache_size', os.O_RDONLY, 0644) try: line = os.read(fd, 4096) finally: os.close(fd) end = len(line) - 1 assert end > 0 number = int(line[:end]) except OSError: break if number < L2cache: L2cache = number cpu += 1 debug_print("L2cache =", L2cache) debug_stop("gc-hardware") if L2cache < sys.maxint: return L2cache else: # Print a top-level warning even in non-debug builds llop.debug_print(lltype.Void, "Warning: cannot find your CPU L2 cache size in " "/sys/devices/system/cpu/cpuX/l2_cache_size") return -1
def charp2strn(cp, maxlen): b = builder_class(maxlen) i = 0 while i < maxlen and cp[i] != lastchar: b.append(cp[i]) i += 1 return assert_str0(b.build())
def find_pyvenv_cfg(dirname): try: fd = os.open(os.path.join(dirname, 'pyvenv.cfg'), os.O_RDONLY, 0) try: content = os.read(fd, 16384) finally: os.close(fd) except OSError: return '' # painfully parse the file for a line 'home = PATH' for line in content.splitlines(): line += '\x00' i = 0 while line[i] == ' ': i += 1 if (line[i] == 'h' and line[i + 1] == 'o' and line[i + 2] == 'm' and line[i + 3] == 'e'): i += 4 while line[i] == ' ': i += 1 if line[i] == '=': line = line[i + 1:] n = line.find('\x00') assert n >= 0 line = line[:n] return assert_str0(line.strip()) return ''
def rawwcharp2unicoden(wcp, maxlen): b = UnicodeBuilder(maxlen) i = 0 while i < maxlen and rffi.cast(lltype.Signed, wcp[i]) != 0: b.append(code_to_unichr(wcp[i])) i += 1 return assert_str0(b.build())
def get_L2cache_linux2_sparc(): debug_start("gc-hardware") cpu = 0 L2cache = sys.maxint while True: try: fd = os.open( '/sys/devices/system/cpu/cpu' + assert_str0(str(cpu)) + '/l2_cache_size', os.O_RDONLY, 0644) try: line = os.read(fd, 4096) finally: os.close(fd) end = len(line) - 1 assert end > 0 number = int(line[:end]) except OSError: break if number < L2cache: L2cache = number cpu += 1 debug_print("L2cache =", L2cache) debug_stop("gc-hardware") if L2cache < sys.maxint: return L2cache else: # Print a top-level warning even in non-debug builds llop.debug_print( lltype.Void, "Warning: cannot find your CPU L2 cache size in " "/sys/devices/system/cpu/cpuX/l2_cache_size") return -1
def _find_loader(self, space, fullname): if '\x00' in fullname: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case return False, None fullname = assert_str0(fullname) filename = self.make_filename(fullname) filename = assert_str0(filename) for _, _, ext in ENUMERATE_EXTS: if self.have_modulefile(space, filename + ext): return True, None # See if this is a directory (part of a namespace pkg) dirpath = self.prefix + fullname.split(".")[-1] if self.have_modulefile(space, dirpath + ZIPSEP): return True, self.filename + os.path.sep + self.corr_zname(dirpath) return False, None
def load_string_or_none_nonul(loader): t = readchr(loader) if t == TYPE_STRING: length = readlong(loader) return assert_str0(readstr(loader, length)) elif t == TYPE_NONE: return None else: raise ValueError("expected a string or None")
def path(space, w_obj): w_string = space.convert_type(w_obj, space.w_string, "to_path", raise_error=False) if w_string is space.w_nil: w_string = space.convert_type(w_obj, space.w_string, "to_str") string = space.str_w(w_string) if "\x00" in string: raise space.error(space.w_ArgumentError, "string contains null byte") else: return assert_str0(string)
def rawwcharp2utf8en(wcp, maxlen): b = StringBuilder(maxlen) i = 0 while i < maxlen: v = r_uint(wcp[i]) if v == 0: break b.append(unichr_as_utf8(v, True)) i += 1 return assert_str0(b.build())
def charp2str(cp): size = 0 while cp[size] != lastchar: size += 1 b = builder_class(size) i = 0 while cp[i] != lastchar: b.append(cp[i]) i += 1 return assert_str0(b.build())
def load_feature(space, path, orig_path): if not os.path.exists(assert_str0(path)): raise space.error(space.w_LoadError, orig_path) f = open_file_as_stream(path) try: contents = f.readall() finally: f.close() space.execute(contents, filepath=path)
def destroy(self, interp): if self.status == PHP_SESSION_ACTIVE: path = interp.space.str_w(interp.config.get_ini_w('session.save_path')) fname = self.name + "-" + self.session_id dest = os.path.join(path, fname) try: assert dest is not None dest = assert_str0(dest) os.remove(dest) except: pass self.session_id = "" self.status = PHP_SESSION_NONE
def destroy(self, interp): if self.status == PHP_SESSION_ACTIVE: path = interp.space.str_w( interp.config.get_ini_w('session.save_path')) fname = self.name + "-" + self.session_id dest = rpath.join(path, [fname]) try: assert dest is not None dest = assert_str0(dest) os.remove(dest) except: pass self.session_id = "" self.status = PHP_SESSION_NONE
def is_package(self, space, w_fullname): fullname = space.text_w(w_fullname) filename = self.make_filename(fullname) for _, is_package, ext in ENUMERATE_EXTS: if '\x00' in filename: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case break filename = assert_str0(filename) if self.have_modulefile(space, filename + ext): return space.newbool(is_package) raise oefmt(get_error(space), "Cannot find module %R in %R", space.newfilename(filename), space.newfilename(self.name))
def find_feature(space, path): assert path is not None if os.path.exists(path): return path if not path.endswith(".rb"): path += ".rb" if not (path.startswith("/") or path.startswith("./") or path.startswith("../")): w_load_path = space.globals.get(space, "$LOAD_PATH") for w_base in space.listview(w_load_path): base = space.str_w(w_base) full = os.path.join(base, path) if os.path.exists(assert_str0(full)): path = os.path.join(base, path) break return path
def __init__(self, name, numargs, cached=False, factory=None): assert name is not None assert isinstance(name, str) name = rstring.assert_str0(name) self.name = name self.numargs = numargs self.cached = cached if factory is None: factory = self._cache self.factory = factory if numargs: atom_signature = factory.getsignature(name, 0, cached) else: atom_signature = self self.atom_signature = atom_signature factory.init_extra_attrs(self)
def load_module(self, space, w_fullname): fullname = space.text_w(w_fullname) filename = self.make_filename(fullname) for compiled, is_package, ext in ENUMERATE_EXTS: if '\x00' in filename: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case break filename = assert_str0(filename) fname = filename + ext try: buf = self.zip_file.read(fname) except (KeyError, OSError, BadZipfile): pass except RZlibError as e: # in this case, CPython raises the direct exception coming # from the zlib module: let's do the same raise zlib_error(space, e.msg) else: if is_package: pkgpath = (self.filename + os.path.sep + self.corr_zname(filename)) else: pkgpath = None try: if compiled: w_result = self.import_pyc_file( space, fullname, fname, buf, pkgpath) if w_result is None: continue else: w_result = self.import_py_file(space, fullname, fname, buf, pkgpath) if space.sys.get_flag('verbose') >= 1: w_stderr = space.sys.get('stderr') message = "import %s # loaded from Zip %s%s%s\n" % ( fullname, self.filename, os.path.sep, fname) space.call_method(w_stderr, "write", space.newtext(message)) return w_result except: w_mods = space.sys.get('modules') space.call_method(w_mods, 'pop', w_fullname, space.w_None) raise raise oefmt(get_error(space), "can't find module %R", w_fullname)
def stringify(path, nt=False): path = to_path(path) prefix = path.prefix pathseq = list(path.pathseq) if isinstance(prefix, URLPrefix): string = prefix.protocol if len(string) > 0: string += u":" if nt and string == u"": # Turns //name/ to NT UNC path. string += ur"\\" + prefix.domain # I am not sure if this is the if len(pathseq) == 0: # correct action. pathseq.append(u"") # Adds a slash. else: string += ur"//" + prefix.domain pathseq.insert(0, string) elif isinstance(prefix, PosixPrefix): if prefix.is_absolute: if len(pathseq) == 0: # Ensures a slash is produced pathseq.append(u"") pathseq.insert(0, u"") elif len(pathseq) > 0 and pathseq[0].count( u":") > 0: # Makes sure the first pathseq.insert(0, u".") # item not recognized as elif len(pathseq) > 0 and len(pathseq[0]) == 0: # posix label. pathseq.insert(0, u".") # rest of this ensures elif len(path.pathseq) == 0: # that we get "." pathseq.insert(0, u".") # produced in any case. # The above cases ensure that this way of adding the prefix # can be actually correct. if prefix.label != u"": pathseq[0] = prefix.label + u":" + pathseq[0] else: raise OldError(u"custom prefix passed to stringification [corruption]") if nt: for name in pathseq: if name.count(u"\\") > 0: raise OldError( ur"nt_stringify cannot stringify file/directory names that contain '\\'" ) string = (u"\\" if nt else u"/").join(pathseq) return rstring.assert_str0(string)
def unmarshal_pycode(space, u, tc): w_codeobj = objectmodel.instantiate(PyCode) u.save_ref(tc, w_codeobj) argcount = u.get_int() kwonlyargcount = u.get_int() nlocals = u.get_int() stacksize = u.get_int() flags = u.get_int() code = space.bytes_w(u.load_w_obj()) consts_w = _unmarshal_tuple_w(u) names = _unmarshal_strlist(u) varnames = _unmarshal_strlist(u) freevars = _unmarshal_strlist(u) cellvars = _unmarshal_strlist(u) filename = space.utf8_0_w(u.load_w_obj()) name = space.utf8_w(u.load_w_obj()) firstlineno = u.get_int() lnotab = space.bytes_w(u.load_w_obj()) filename = assert_str0(filename) PyCode.__init__(w_codeobj, space, argcount, kwonlyargcount, nlocals, stacksize, flags, code, consts_w[:], names, varnames, filename, name, firstlineno, lnotab, freevars, cellvars) return w_codeobj
def get_source(self, space, fullname): filename = self.make_filename(fullname) found = False for compiled, _, ext in ENUMERATE_EXTS: if '\x00' in filename: # Special case to make the annotator happy: # filenames inside ZIPs shouldn't contain NULs so no module can # possibly be found in this case break filename = assert_str0(filename) fname = filename + ext if self.have_modulefile(space, fname): if not compiled: w_data = self.get_data(space, fname) # XXX CPython does not handle the coding cookie either. return space.call_method(w_data, "decode", space.newtext("utf-8")) else: found = True if found: # We have the module, but no source. return space.w_None raise oefmt(get_error(space), "Cannot find source for %R in %R", space.newfilename(filename), space.newfilename(self.name))
def path(space, w_obj): string = Coerce.str(space, w_obj) if "\x00" in string: raise space.error(space.w_ArgumentError, "string contains null byte") else: return assert_str0(string)
def get_L2cache_linux2_ia64(): debug_start("gc-hardware") cpu = 0 L2cache = sys.maxint L3cache = sys.maxint while True: cpudir = '/sys/devices/system/cpu/cpu' + assert_str0(str(cpu)) index = 0 while True: cachedir = cpudir + '/cache/index' + assert_str0(str(index)) try: fd = os.open(cachedir + '/level', os.O_RDONLY, 0644) try: level = int(os.read(fd, 4096)[:-1]) finally: os.close(fd) except OSError: break if level not in (2, 3): index += 1 continue try: fd = os.open(cachedir + '/size', os.O_RDONLY, 0644) try: data = os.read(fd, 4096) finally: os.close(fd) except OSError: break end = 0 while '0' <= data[end] <= '9': end += 1 if end == 0: index += 1 continue if data[end] not in ('K', 'k'): # assume kilobytes for now index += 1 continue number = int(data[:end]) number *= 1024 if level == 2: if number < L2cache: L2cache = number if level == 3: if number < L3cache: L3cache = number index += 1 if index == 0: break cpu += 1 mangled = L2cache + L3cache debug_print("L2cache =", mangled) debug_stop("gc-hardware") if mangled > 0: return mangled else: # Print a top-level warning even in non-debug builds llop.debug_print(lltype.Void, "Warning: cannot find your CPU L2 & L3 cache size in " "/sys/devices/system/cpu/cpuX/cache") return -1
def load_string_nonul(loader): if readchr(loader) != TYPE_STRING: raise ValueError("expected a string") length = readlong(loader) return assert_str0(readstr(loader, length))
def as_bytes(self): from rpython.rlib.runicode import unicode_encode_utf_8 res = unicode_encode_utf_8(self.unistr, len(self.unistr), "strict") return rstring.assert_str0(res)
def charp2str(cp): size = 0 while cp[size] != lastchar: size += 1 return assert_str0(charpsize2str(cp, size))
def charp2strn(cp, maxlen): size = 0 while size < maxlen and cp[size] != lastchar: size += 1 return assert_str0(charpsize2str(cp, size))
def get_L2cache_linux2_ia64(): debug_start("gc-hardware") cpu = 0 L2cache = sys.maxint L3cache = sys.maxint while True: cpudir = '/sys/devices/system/cpu/cpu' + assert_str0(str(cpu)) index = 0 while True: cachedir = cpudir + '/cache/index' + assert_str0(str(index)) try: fd = os.open(cachedir + '/level', os.O_RDONLY, 0644) try: level = int(os.read(fd, 4096)[:-1]) finally: os.close(fd) except OSError: break if level not in (2, 3): index += 1 continue try: fd = os.open(cachedir + '/size', os.O_RDONLY, 0644) try: data = os.read(fd, 4096) finally: os.close(fd) except OSError: break end = 0 while '0' <= data[end] <= '9': end += 1 if end == 0: index += 1 continue if data[end] not in ('K', 'k'): # assume kilobytes for now index += 1 continue number = int(data[:end]) number *= 1024 if level == 2: if number < L2cache: L2cache = number if level == 3: if number < L3cache: L3cache = number index += 1 if index == 0: break cpu += 1 mangled = L2cache + L3cache debug_print("L2cache =", mangled) debug_stop("gc-hardware") if mangled > 0: return mangled else: # Print a top-level warning even in non-debug builds llop.debug_print( lltype.Void, "Warning: cannot find your CPU L2 & L3 cache size in " "/sys/devices/system/cpu/cpuX/cache") return -1