本文整理汇总了Python中topaz.error.error_for_oserror函数的典型用法代码示例。如果您正苦于以下问题:Python error_for_oserror函数的具体用法?Python error_for_oserror怎么用?Python error_for_oserror使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了error_for_oserror函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: method_kill
def method_kill(self, space, w_signal, args_w):
if not args_w:
raise space.error(space.w_ArgumentError,
"wrong number of arguments (%d for at least 2)" % (len(args_w) + 1)
)
if space.is_kind_of(w_signal, space.w_fixnum):
sig = space.int_w(w_signal)
else:
s = Coerce.str(space, w_signal)
if s.startswith("SIG"):
s = s[len("SIG"):]
try:
sig = SIGNALS[s]
except KeyError:
raise space.error(space.w_ArgumentError,
"unsupported name `SIG%s'" % s
)
if sig < 0:
for w_arg in args_w:
pid = Coerce.int(space, w_arg)
try:
killpg(pid, -sig)
except OSError as e:
raise error_for_oserror(space, e)
else:
for w_arg in args_w:
pid = Coerce.int(space, w_arg)
try:
kill(pid, sig)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(len(args_w))
示例2: method_exec
def method_exec(self, space, args_w):
if len(args_w) > 1 and space.respond_to(args_w[0], "to_hash"):
raise space.error(space.w_NotImplementedError, "exec with environment")
if len(args_w) > 1 and space.respond_to(args_w[-1], "to_hash"):
raise space.error(space.w_NotImplementedError, "exec with options")
if space.respond_to(args_w[0], "to_ary"):
w_cmd = space.convert_type(args_w[0], space.w_array, "to_ary")
cmd_w = space.listview(w_cmd)
if len(cmd_w) != 2:
raise space.error(space.w_ArgumentError, "wrong first argument")
cmd, argv0 = [
space.str0_w(space.convert_type(
w_e, space.w_string, "to_str"
)) for w_e in cmd_w
]
else:
w_cmd = space.convert_type(args_w[0], space.w_string, "to_str")
cmd = space.str0_w(w_cmd)
argv0 = None
if len(args_w) > 1 or argv0 is not None:
if argv0 is None:
sepidx = cmd.rfind(os.sep) + 1
if sepidx > 0:
argv0 = cmd[sepidx:]
else:
argv0 = cmd
args = [argv0]
args += [
space.str0_w(space.convert_type(
w_arg, space.w_string, "to_str"
)) for w_arg in args_w[1:]
]
try:
os.execv(cmd, args)
except OSError as e:
raise error_for_oserror(space, e)
else:
if not cmd:
raise error_for_errno(space, errno.ENOENT)
shell = os.environ.get("RUBYSHELL") or os.environ.get("COMSPEC") or "/bin/sh"
sepidx = shell.rfind(os.sep) + 1
if sepidx > 0:
argv0 = shell[sepidx:]
else:
argv0 = shell
try:
os.execv(shell, [argv0, "-c", cmd])
except OSError as e:
raise error_for_oserror(space, e)
示例3: method_initialize
def method_initialize(self, space, path):
self.path = path
try:
self.dirp = opendir(path)
except OSError as e:
raise error_for_oserror(space, e)
self.open = True
示例4: method_truncate
def method_truncate(self, space, length):
self.ensure_not_closed(space)
try:
ftruncate(self.fd, length)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(0)
示例5: method_read
def method_read(self, space, w_length=None, w_str=None):
self.ensure_not_closed(space)
if w_length:
length = space.int_w(w_length)
if length < 0:
raise space.error(space.w_ArgumentError,
"negative length %d given" % length
)
else:
length = -1
read_bytes = 0
read_chunks = []
while length < 0 or read_bytes < length:
if length > 0:
max_read = int(length - read_bytes)
else:
max_read = 8192
try:
current_read = os.read(self.fd, max_read)
except OSError as e:
raise error_for_oserror(space, e)
if len(current_read) == 0:
break
read_bytes += len(current_read)
read_chunks += current_read
# Return nil on EOF if length is given
if read_bytes == 0:
return space.w_nil
w_read_str = space.newstr_fromchars(read_chunks)
if w_str is not None:
w_str.clear(space)
w_str.extend(space, w_read_str)
return w_str
else:
return w_read_str
示例6: method_write
def method_write(self, space, w_str):
self.ensure_not_closed(space)
string = space.str_w(space.send(w_str, "to_s"))
try:
bytes_written = os.write(self.fd, string)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(bytes_written)
示例7: singleton_method_chmod
def singleton_method_chmod(self, space, mode, args_w):
for arg_w in args_w:
path = Coerce.path(space, arg_w)
try:
os.chmod(path, mode)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(len(args_w))
示例8: singleton_method_lstat
def singleton_method_lstat(self, space, filename):
try:
stat_val = os.lstat(filename)
except OSError as e:
raise error_for_oserror(space, e)
stat_obj = W_FileStatObject(space)
stat_obj.set_stat(stat_val)
return stat_obj
示例9: singleton_method_delete
def singleton_method_delete(self, space, args_w):
for w_path in args_w:
path = Coerce.path(space, w_path)
try:
os.unlink(path)
except OSError as e:
raise error_for_oserror(space, e)
return space.newint(len(args_w))
示例10: method_stat
def method_stat(self, space):
try:
stat_val = os.fstat(self.fd)
except OSError as e:
raise error_for_oserror(space, e)
stat_obj = W_FileStatObject(space)
stat_obj.set_stat(stat_val)
return stat_obj
示例11: method_chdir
def method_chdir(self, space, path=None, block=None):
if path is None:
path = os.environ["HOME"]
current_dir = os.getcwd()
try:
os.chdir(path)
except OSError as e:
raise error_for_oserror(space, e)
if block is not None:
try:
return space.invoke_block(block, [space.newstr_fromstr(path)])
finally:
try:
os.chdir(current_dir)
except OSError as e:
raise error_for_oserror(space, e)
else:
return space.newint(0)
示例12: method_getc
def method_getc(self, space):
self.ensure_not_closed(space)
try:
c = os.read(self.fd, 1)
except OSError as e:
raise error_for_oserror(space, e)
if not c:
return space.w_nil
return space.newstr_fromstr(c)
示例13: method_clock_gettime
def method_clock_gettime(self, space, clockid, args_w):
if len(args_w) > 1:
raise space.error(space.w_ArgumentError,
"wrong number of arguments (given %d, expected 1..2)"
)
if len(args_w) == 1:
unit = Coerce.symbol(space, args_w[0])
else:
unit = "float_second"
if rtime.HAS_CLOCK_GETTIME:
with lltype.scoped_alloc(rtime.TIMESPEC) as a:
if rtime.c_clock_gettime(clockid, a) == 0:
sec = rffi.getintfield(a, 'c_tv_sec')
nsec = rffi.getintfield(a, 'c_tv_nsec')
else:
raise error_for_oserror(space, OSError(
errno.EINVAL, "clock_gettime")
)
elif clockid == CLOCK_PROCESS_CPUTIME_ID:
r = rtime.clock()
sec = int(r)
nsec = r * 1000000000
else:
raise error_for_oserror(space, OSError(
errno.EINVAL, "clock_gettime")
)
if unit == "float_second":
return space.newfloat(sec + nsec * 0.000000001)
elif unit == "float_millisecond":
return space.newfloat(sec * 1000 + nsec * 0.000001)
elif unit == "float_microsecond":
return space.newfloat(sec * 1000000 + nsec * 0.001)
elif unit == "second":
return space.newint(int(sec))
elif unit == "millisecond":
return space.newint(int(sec) * 1000)
elif unit == "microsecond":
return space.newint(sec * 1000000)
elif unit == "nanosecond":
return space.newint(sec * 1000000000 + int(nsec))
else:
raise space.error(space.w_ArgumentError,
"unexpected unit: %s" % unit
)
示例14: method_read
def method_read(self, space, args_w):
self.ensure_open(space)
try:
filename = readdir(self.dirp)
except OSError as e:
raise error_for_oserror(space, e)
if filename is None:
return space.w_nil
else:
return space.newstr_fromstr(filename)
示例15: method_waitpid
def method_waitpid(self, space, pid=-1):
try:
pid, status = os.waitpid(pid, 0)
except OSError as e:
raise error_for_oserror(space, e)
status = WEXITSTATUS(status)
w_status = space.send(
space.find_const(self, "Status"),
"new",
[space.newint(pid), space.newint(status)]
)
space.globals.set(space, "$?", w_status)
return space.newint(pid)