mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 03:50:16 +00:00
remove IOError
This commit is contained in:
parent
b49e769581
commit
d6435f151b
@ -515,7 +515,7 @@ PK_API void py_clearexc(py_StackRef p0);
|
|||||||
#define NameError(n) py_exception(tp_NameError, "name '%n' is not defined", (n))
|
#define NameError(n) py_exception(tp_NameError, "name '%n' is not defined", (n))
|
||||||
#define TypeError(...) py_exception(tp_TypeError, __VA_ARGS__)
|
#define TypeError(...) py_exception(tp_TypeError, __VA_ARGS__)
|
||||||
#define RuntimeError(...) py_exception(tp_RuntimeError, __VA_ARGS__)
|
#define RuntimeError(...) py_exception(tp_RuntimeError, __VA_ARGS__)
|
||||||
#define IOError(...) py_exception(tp_IOError, __VA_ARGS__)
|
#define OSError(...) py_exception(tp_OSError, __VA_ARGS__)
|
||||||
#define ValueError(...) py_exception(tp_ValueError, __VA_ARGS__)
|
#define ValueError(...) py_exception(tp_ValueError, __VA_ARGS__)
|
||||||
#define IndexError(...) py_exception(tp_IndexError, __VA_ARGS__)
|
#define IndexError(...) py_exception(tp_IndexError, __VA_ARGS__)
|
||||||
#define ImportError(...) py_exception(tp_ImportError, __VA_ARGS__)
|
#define ImportError(...) py_exception(tp_ImportError, __VA_ARGS__)
|
||||||
@ -709,7 +709,6 @@ enum py_PredefinedTypes {
|
|||||||
tp_StopIteration,
|
tp_StopIteration,
|
||||||
tp_SyntaxError,
|
tp_SyntaxError,
|
||||||
tp_StackOverflowError,
|
tp_StackOverflowError,
|
||||||
tp_IOError,
|
|
||||||
tp_OSError,
|
tp_OSError,
|
||||||
tp_NotImplementedError,
|
tp_NotImplementedError,
|
||||||
tp_TypeError,
|
tp_TypeError,
|
||||||
|
@ -153,7 +153,6 @@ void VM__ctor(VM* self) {
|
|||||||
|
|
||||||
INJECT_BUILTIN_EXC(SyntaxError, tp_Exception);
|
INJECT_BUILTIN_EXC(SyntaxError, tp_Exception);
|
||||||
INJECT_BUILTIN_EXC(StackOverflowError, tp_Exception);
|
INJECT_BUILTIN_EXC(StackOverflowError, tp_Exception);
|
||||||
INJECT_BUILTIN_EXC(IOError, tp_Exception);
|
|
||||||
INJECT_BUILTIN_EXC(OSError, tp_Exception);
|
INJECT_BUILTIN_EXC(OSError, tp_Exception);
|
||||||
INJECT_BUILTIN_EXC(NotImplementedError, tp_Exception);
|
INJECT_BUILTIN_EXC(NotImplementedError, tp_Exception);
|
||||||
INJECT_BUILTIN_EXC(TypeError, tp_Exception);
|
INJECT_BUILTIN_EXC(TypeError, tp_Exception);
|
||||||
|
@ -32,14 +32,17 @@ static bool os_chdir(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(0, tp_str);
|
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||||
const char* path = py_tostr(py_arg(0));
|
const char* path = py_tostr(py_arg(0));
|
||||||
int code = platform_chdir(path);
|
int code = platform_chdir(path);
|
||||||
if(code != 0) return py_exception(tp_OSError, "chdir() failed: %d", code);
|
if(code != 0) {
|
||||||
|
const char* msg = strerror(errno);
|
||||||
|
return OSError("[Errno %d] %s: '%s'", errno, msg, path);
|
||||||
|
}
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool os_getcwd(int argc, py_Ref argv) {
|
static bool os_getcwd(int argc, py_Ref argv) {
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
if(!platform_getcwd(buf, sizeof(buf))) return py_exception(tp_OSError, "getcwd() failed");
|
if(!platform_getcwd(buf, sizeof(buf))) return OSError("getcwd() failed");
|
||||||
py_newstr(py_retval(), buf);
|
py_newstr(py_retval(), buf);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -53,15 +56,29 @@ static bool os_system(int argc, py_Ref argv) {
|
|||||||
py_newint(py_retval(), code);
|
py_newint(py_retval(), code);
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return py_exception(tp_OSError, "system() is not supported on this platform");
|
return OSError("system() is not supported on this platform");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool os_remove(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||||
|
const char* path = py_tostr(py_arg(0));
|
||||||
|
int code = remove(path);
|
||||||
|
if(code != 0) {
|
||||||
|
const char* msg = strerror(errno);
|
||||||
|
return OSError("[Errno %d] %s: '%s'", errno, msg, path);
|
||||||
|
}
|
||||||
|
py_newnone(py_retval());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void pk__add_module_os() {
|
void pk__add_module_os() {
|
||||||
py_Ref mod = py_newmodule("os");
|
py_Ref mod = py_newmodule("os");
|
||||||
py_bindfunc(mod, "chdir", os_chdir);
|
py_bindfunc(mod, "chdir", os_chdir);
|
||||||
py_bindfunc(mod, "getcwd", os_getcwd);
|
py_bindfunc(mod, "getcwd", os_getcwd);
|
||||||
py_bindfunc(mod, "system", os_system);
|
py_bindfunc(mod, "system", os_system);
|
||||||
|
py_bindfunc(mod, "remove", os_remove);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -82,7 +99,7 @@ static bool io_FileIO__new__(int argc, py_Ref argv) {
|
|||||||
ud->file = fopen(ud->path, ud->mode);
|
ud->file = fopen(ud->path, ud->mode);
|
||||||
if(ud->file == NULL) {
|
if(ud->file == NULL) {
|
||||||
const char* msg = strerror(errno);
|
const char* msg = strerror(errno);
|
||||||
return IOError("[Errno %d] %s: %s", errno, msg, ud->path);
|
return OSError("[Errno %d] %s: '%s'", errno, msg, ud->path);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -200,6 +217,7 @@ void pk__add_module_io() {
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
void pk__add_module_os() {}
|
void pk__add_module_os() {}
|
||||||
|
|
||||||
void pk__add_module_io() {}
|
void pk__add_module_io() {}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
try:
|
try:
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
print("[`os` Test Enabled]")
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
print('os is not enabled, skipping test...')
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
a = open('123.txt', 'wt')
|
a = open('123.txt', 'wt')
|
||||||
@ -44,15 +44,12 @@ with open('123.txt', 'rt') as f:
|
|||||||
with open('123.txt', 'a') as f:
|
with open('123.txt', 'a') as f:
|
||||||
f.write('测试')
|
f.write('测试')
|
||||||
|
|
||||||
exit()
|
with open('123.txt', 'r') as f:
|
||||||
|
|
||||||
# default mode is 'r'
|
|
||||||
with open('123.txt') as f:
|
|
||||||
assert f.read() == '123456' + '测试'
|
assert f.read() == '123456' + '测试'
|
||||||
|
|
||||||
assert os.path.exists('123.txt')
|
# assert os.path.exists('123.txt')
|
||||||
os.remove('123.txt')
|
os.remove('123.txt')
|
||||||
assert not os.path.exists('123.txt')
|
# assert not os.path.exists('123.txt')
|
||||||
|
|
||||||
|
|
||||||
with open('123.bin', 'wb') as f:
|
with open('123.bin', 'wb') as f:
|
||||||
@ -67,6 +64,6 @@ def f_():
|
|||||||
|
|
||||||
f_()
|
f_()
|
||||||
|
|
||||||
assert os.path.exists('123.bin')
|
# assert os.path.exists('123.bin')
|
||||||
os.remove('123.bin')
|
os.remove('123.bin')
|
||||||
assert not os.path.exists('123.bin')
|
# assert not os.path.exists('123.bin')
|
Loading…
x
Reference in New Issue
Block a user