def QueryValue(space, w_hkey, w_subkey): """string = QueryValue(key, sub_key) - retrieves the unnamed value for a key. key is an already open key, or any one of the predefined HKEY_* constants. sub_key is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key. Values in the registry have name, type, and data components. This method retrieves the data for a key's first value that has a NULL name. But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!""" hkey = hkey_w(w_hkey, space) if space.is_w(w_subkey, space.w_None): subkey = None else: subkey = space.str_w(w_subkey) bufsize_p = lltype.malloc(rwin32.PLONG.TO, 1, flavor='raw') try: ret = rwinreg.RegQueryValue(hkey, subkey, None, bufsize_p) if ret != 0: raiseWindowsError(space, ret, 'RegQueryValue') buf = lltype.malloc(rffi.CCHARP.TO, bufsize_p[0], flavor='raw') try: ret = rwinreg.RegQueryValue(hkey, subkey, buf, bufsize_p) if ret != 0: raiseWindowsError(space, ret, 'RegQueryValue') return space.wrap(rffi.charp2strn(buf, bufsize_p[0] - 1)) finally: lltype.free(buf, flavor='raw') finally: lltype.free(bufsize_p, flavor='raw') if ret != 0: raiseWindowsError(space, ret, 'RegQueryValue')
def QueryValue(space, w_hkey, w_subkey): """string = QueryValue(key, sub_key) - retrieves the unnamed value for a key. key is an already open key, or any one of the predefined HKEY_* constants. sub_key is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key. Values in the registry have name, type, and data components. This method retrieves the data for a key's first value that has a NULL name. But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!""" hkey = hkey_w(w_hkey, space) if space.is_w(w_subkey, space.w_None): subkey = None else: subkey = space.str_w(w_subkey) bufsize_p = lltype.malloc(rwin32.PLONG.TO, 1, flavor="raw") try: ret = rwinreg.RegQueryValue(hkey, subkey, None, bufsize_p) if ret != 0: raiseWindowsError(space, ret, "RegQueryValue") buf = lltype.malloc(rffi.CCHARP.TO, bufsize_p[0], flavor="raw") try: ret = rwinreg.RegQueryValue(hkey, subkey, buf, bufsize_p) if ret != 0: raiseWindowsError(space, ret, "RegQueryValue") return space.wrap(rffi.charp2strn(buf, bufsize_p[0] - 1)) finally: lltype.free(buf, flavor="raw") finally: lltype.free(bufsize_p, flavor="raw") if ret != 0: raiseWindowsError(space, ret, "RegQueryValue")
def convert_from_regdata(space, buf, buflen, typ): if typ == rwinreg.REG_DWORD: if not buflen: return space.wrap(0) d = rffi.cast(rwin32.LPDWORD, buf)[0] return space.wrap(d) elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: if not buflen: return space.wrap("") s = rffi.charp2strn(rffi.cast(rffi.CCHARP, buf), buflen) return space.wrap(s) elif typ == rwinreg.REG_MULTI_SZ: if not buflen: return space.newlist([]) i = 0 l = [] while i < buflen and buf[i]: s = [] while i < buflen and buf[i] != '\0': s.append(buf[i]) i += 1 if len(s) == 0: break s = ''.join(s) l.append(space.wrap(s)) i += 1 return space.newlist(l) else: # REG_BINARY and all other types return space.wrap(rffi.charpsize2str(buf, buflen))
def JSStringGetUTF8CString(s): # XXX horribly inefficient lgt = _JSStringGetMaximumUTF8CStringSize(s) buf = lltype.malloc(rffi.CCHARP.TO, lgt, flavor="raw") newlgt = _JSStringGetUTF8CString(s, buf, lgt) res = rffi.charp2strn(buf, newlgt) lltype.free(buf, flavor="raw") return res
def JSStringGetUTF8CString(s): # XXX horribly inefficient lgt = _JSStringGetMaximumUTF8CStringSize(s) buf = lltype.malloc(rffi.CCHARP.TO, lgt, flavor='raw') newlgt = _JSStringGetUTF8CString(s, buf, lgt) res = rffi.charp2strn(buf, newlgt) lltype.free(buf, flavor='raw') return res
def strftime(space, format, w_tup=None): """strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.""" buf_value = _gettmarg(space, w_tup) # Checks added to make sure strftime() does not crash Python by # indexing blindly into some array for a textual representation # by some bad index (fixes bug #897625). # No check for year since handled in gettmarg(). if rffi.getintfield(buf_value, "c_tm_mon") < 0 or rffi.getintfield(buf_value, "c_tm_mon") > 11: raise OperationError(space.w_ValueError, space.wrap("month out of range")) if rffi.getintfield(buf_value, "c_tm_mday") < 1 or rffi.getintfield(buf_value, "c_tm_mday") > 31: raise OperationError(space.w_ValueError, space.wrap("day of month out of range")) if rffi.getintfield(buf_value, "c_tm_hour") < 0 or rffi.getintfield(buf_value, "c_tm_hour") > 23: raise OperationError(space.w_ValueError, space.wrap("hour out of range")) if rffi.getintfield(buf_value, "c_tm_min") < 0 or rffi.getintfield(buf_value, "c_tm_min") > 59: raise OperationError(space.w_ValueError, space.wrap("minute out of range")) if rffi.getintfield(buf_value, "c_tm_sec") < 0 or rffi.getintfield(buf_value, "c_tm_sec") > 61: raise OperationError(space.w_ValueError, space.wrap("seconds out of range")) if rffi.getintfield(buf_value, "c_tm_yday") < 0 or rffi.getintfield(buf_value, "c_tm_yday") > 365: raise OperationError(space.w_ValueError, space.wrap("day of year out of range")) if rffi.getintfield(buf_value, "c_tm_isdst") < -1 or rffi.getintfield(buf_value, "c_tm_isdst") > 1: raise OperationError(space.w_ValueError, space.wrap("daylight savings flag out of range")) if _WIN: # check that the format string contains only valid directives length = len(format) i = 0 while i < length: if format[i] == "%": i += 1 if i < length and format[i] == "#": # not documented by python i += 1 if i >= length or format[i] not in "aAbBcdfHIjmMpSUwWxXyYzZ%": raise OperationError(space.w_ValueError, space.wrap("invalid format string")) i += 1 i = 1024 while True: outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor="raw") try: buflen = c_strftime(outbuf, i, format, buf_value) if buflen > 0 or i >= 256 * len(format): # if the buffer is 256 times as long as the format, # it's probably not failing for lack of room! # More likely, the format yields an empty result, # e.g. an empty format, or %Z when the timezone # is unknown. result = rffi.charp2strn(outbuf, buflen) return space.wrap(result) finally: lltype.free(outbuf, flavor="raw") i += i
def charp2string(space, address, maxlength=-1): if address == 0: return space.w_None charp_addr = rffi.cast(rffi.CCHARP, address) if maxlength == -1: s = rffi.charp2str(charp_addr) else: s = rffi.charp2strn(charp_addr, maxlength) return space.wrap(s)
def gethostname(): size = 1024 buf = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') try: res = _c.gethostname(buf, size) if res < 0: raise last_error() return rffi.charp2strn(buf, size) finally: lltype.free(buf, flavor='raw')
def set_error(self, space, code): err = rffi.charp2strn(XML_ErrorString(code), 200) lineno = XML_GetCurrentLineNumber(self.itself) colno = XML_GetCurrentColumnNumber(self.itself) msg = "%s: line: %d, column: %d" % (err, lineno, colno) w_module = space.getbuiltinmodule('pyexpat') w_errorcls = space.getattr(w_module, space.wrap('error')) w_error = space.call_function(w_errorcls, space.wrap(msg), space.wrap(code), space.wrap(colno), space.wrap(lineno)) self.w_error = w_error return OperationError(w_errorcls, w_error)
def set_error(self, space, code): err = rffi.charp2strn(XML_ErrorString(code), 200) lineno = XML_GetCurrentLineNumber(self.itself) colno = XML_GetCurrentColumnNumber(self.itself) msg = "%s: line %d, column %d" % (err, lineno, colno) w_errorcls = space.fromcache(Cache).w_error w_error = space.call_function(w_errorcls, space.wrap(msg)) space.setattr(w_error, space.wrap("code"), space.wrap(code)) space.setattr(w_error, space.wrap("offset"), space.wrap(colno)) space.setattr(w_error, space.wrap("lineno"), space.wrap(lineno)) self.w_error = w_error return OperationError(w_errorcls, w_error)
def set_error(self, space, code): err = rffi.charp2strn(XML_ErrorString(code), 200) lineno = XML_GetCurrentLineNumber(self.itself) colno = XML_GetCurrentColumnNumber(self.itself) msg = "%s: line: %d, column: %d" % (err, lineno, colno) w_module = space.getbuiltinmodule('pyexpat') w_errorcls = space.getattr(w_module, space.wrap('error')) w_error = space.call_function( w_errorcls, space.wrap(msg), space.wrap(code), space.wrap(colno), space.wrap(lineno)) self.w_error = w_error return OperationError(w_errorcls, w_error)
def QueryValue(space, w_hkey, w_subkey): """string = QueryValue(key, sub_key) - retrieves the unnamed value for a key. key is an already open key, or any one of the predefined HKEY_* constants. sub_key is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key. Values in the registry have name, type, and data components. This method retrieves the data for a key's first value that has a NULL name. But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!""" hkey = hkey_w(w_hkey, space) if space.is_w(w_subkey, space.w_None): subkey = None else: subkey = space.str_w(w_subkey) with lltype.scoped_alloc(rwin32.PLONG.TO, 1) as bufsize_p: ret = rwinreg.RegQueryValue(hkey, subkey, None, bufsize_p) bufSize = intmask(bufsize_p[0]) if ret == rwinreg.ERROR_MORE_DATA: bufSize = 256 elif ret != 0: raiseWindowsError(space, ret, 'RegQueryValue') while True: with lltype.scoped_alloc(rffi.CCHARP.TO, bufSize) as buf: ret = rwinreg.RegQueryValue(hkey, subkey, buf, bufsize_p) if ret == rwinreg.ERROR_MORE_DATA: # Resize and retry bufSize *= 2 bufsize_p[0] = bufSize continue if ret != 0: raiseWindowsError(space, ret, 'RegQueryValue') length = intmask(bufsize_p[0] - 1) return space.wrap(rffi.charp2strn(buf, length))
def w_convert_charp_n(self, space, data, length): ll_length = rffi.cast(lltype.Signed, length) if data: return self.w_convert(space, rffi.charp2strn(data, ll_length)) else: return space.w_None
def strftime(space, format, w_tup=None): """strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.""" buf_value = _gettmarg(space, w_tup) # Checks added to make sure strftime() does not crash Python by # indexing blindly into some array for a textual representation # by some bad index (fixes bug #897625). # No check for year since handled in gettmarg(). if rffi.getintfield(buf_value, 'c_tm_mon') < 0 or rffi.getintfield( buf_value, 'c_tm_mon') > 11: raise OperationError(space.w_ValueError, space.wrap("month out of range")) if rffi.getintfield(buf_value, 'c_tm_mday') < 1 or rffi.getintfield( buf_value, 'c_tm_mday') > 31: raise OperationError(space.w_ValueError, space.wrap("day of month out of range")) if rffi.getintfield(buf_value, 'c_tm_hour') < 0 or rffi.getintfield( buf_value, 'c_tm_hour') > 23: raise OperationError(space.w_ValueError, space.wrap("hour out of range")) if rffi.getintfield(buf_value, 'c_tm_min') < 0 or rffi.getintfield( buf_value, 'c_tm_min') > 59: raise OperationError(space.w_ValueError, space.wrap("minute out of range")) if rffi.getintfield(buf_value, 'c_tm_sec') < 0 or rffi.getintfield( buf_value, 'c_tm_sec') > 61: raise OperationError(space.w_ValueError, space.wrap("seconds out of range")) if rffi.getintfield(buf_value, 'c_tm_yday') < 0 or rffi.getintfield( buf_value, 'c_tm_yday') > 365: raise OperationError(space.w_ValueError, space.wrap("day of year out of range")) if rffi.getintfield(buf_value, 'c_tm_isdst') < -1 or rffi.getintfield( buf_value, 'c_tm_isdst') > 1: raise OperationError(space.w_ValueError, space.wrap("daylight savings flag out of range")) if _WIN: # check that the format string contains only valid directives length = len(format) i = 0 while i < length: if format[i] == '%': i += 1 if i < length and format[i] == '#': # not documented by python i += 1 if i >= length or format[i] not in "aAbBcdHIjmMpSUwWxXyYzZ%": raise OperationError(space.w_ValueError, space.wrap("invalid format string")) i += 1 i = 1024 while True: outbuf = lltype.malloc(rffi.CCHARP.TO, i, flavor='raw') try: buflen = c_strftime(outbuf, i, format, buf_value) if buflen > 0 or i >= 256 * len(format): # if the buffer is 256 times as long as the format, # it's probably not failing for lack of room! # More likely, the format yields an empty result, # e.g. an empty format, or %Z when the timezone # is unknown. result = rffi.charp2strn(outbuf, intmask(buflen)) return space.wrap(result) finally: lltype.free(outbuf, flavor='raw') i += i
def charp2string(space, address, maxlength=sys.maxint): if address == 0: return space.w_None s = rffi.charp2strn(rffi.cast(rffi.CCHARP, address), maxlength) return space.wrap(s)