disable finally cuz it is buggy

This commit is contained in:
blueloveTH 2025-08-30 21:34:37 +08:00
parent b57b16efaf
commit 1d16d1a6f7
7 changed files with 24 additions and 67 deletions

View File

@ -159,7 +159,7 @@ __ERROR:
| F-String | `f'value is {x}'` | ✅ |
| Unpacking | `a, b = 1, 2` | ✅ |
| Star Unpacking | `a, *b = [1, 2, 3]` | ✅ |
| Exception | `raise/try..catch..finally` | ✅ |
| Exception | `raise/try..except..` | ✅ |
| Dynamic Code | `eval()/exec()` | ✅ |
| Reflection | `hasattr()/getattr()/setattr()` | ✅ |
| Import | `import/from..import` | ✅ |

View File

@ -20,7 +20,7 @@ The following table shows the basic features of pkpy with respect to [cpython](h
| F-String | `f'value is {x}'` | ✅ |
| Unpacking | `a, b = 1, 2` | ✅ |
| Star Unpacking | `a, *b = [1, 2, 3]` | ✅ |
| Exception | `raise/try..catch..finally` | ✅ |
| Exception | `raise/try..except..` | ✅ |
| Dynamic Code | `eval()/exec()` | ✅ |
| Reflection | `hasattr()/getattr()/setattr()` | ✅ |
| Import | `import/from..import` | ✅ |

View File

@ -35,7 +35,6 @@ typedef enum CodeBlockType {
CodeBlockType_WITH,
/* context blocks (flag-based) */
CodeBlockType_EXCEPT,
CodeBlockType_FINALLY,
} CodeBlockType;
typedef enum Opcode {

View File

@ -124,8 +124,6 @@ OPCODE(RE_RAISE)
OPCODE(PUSH_EXCEPTION)
OPCODE(BEGIN_EXC_HANDLING)
OPCODE(END_EXC_HANDLING)
OPCODE(BEGIN_FINALLY)
OPCODE(END_FINALLY)
/**************************/
OPCODE(FORMAT_STRING)
/**************************/

View File

@ -1136,10 +1136,6 @@ static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) {
Ctx__emit_(self, OP_END_EXC_HANDLING, 1, line);
break;
}
case CodeBlockType_FINALLY: {
Ctx__emit_(self, OP_END_FINALLY, 1, line);
break;
}
default: break;
}
index = block->parent;
@ -2000,7 +1996,7 @@ static Error* exprSlice0(Compiler* self) {
check(EXPR(self));
slice->step = Ctx__s_popx(ctx());
} // else ::
} // else :
} // else :
return NULL;
}
@ -2646,21 +2642,11 @@ static Error* compile_try_except(Compiler* self) {
bool has_finally = curr()->type == TK_FINALLY;
if(!has_finally) {
patches[patches_length++] = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
} else {
return SyntaxError(self, "finally clause is not supported yet");
}
Ctx__exit_block(ctx());
if(has_finally) {
consume(TK_FINALLY);
Ctx__emit_(ctx(), OP_BEGIN_FINALLY, BC_NOARG, prev()->line);
// finally only, no except block
Ctx__enter_block(ctx(), CodeBlockType_FINALLY);
check(compile_block_body(self));
Ctx__exit_block(ctx());
Ctx__emit_(ctx(), OP_END_FINALLY, BC_NOARG, BC_KEEPLINE);
// re-raise if needed
Ctx__emit_(ctx(), OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
return NULL;
}
Ctx__exit_block(ctx());
do {
if(patches_length == 8) {
@ -2701,16 +2687,12 @@ static Error* compile_try_except(Compiler* self) {
// ...
// match one & handled, jump to the end
for(int i = 0; i < patches_length; i++)
for(int i = 0; i < patches_length; i++) {
Ctx__patch_jump(ctx(), patches[i]);
if(match(TK_FINALLY)) {
Ctx__emit_(ctx(), OP_BEGIN_FINALLY, BC_NOARG, prev()->line);
Ctx__enter_block(ctx(), CodeBlockType_FINALLY);
check(compile_block_body(self));
Ctx__exit_block(ctx());
Ctx__emit_(ctx(), OP_END_FINALLY, BC_NOARG, BC_KEEPLINE);
}
if(match(TK_FINALLY)) { return SyntaxError(self, "finally clause is not supported yet"); }
// re-raise if needed
Ctx__emit_(ctx(), OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
return NULL;

View File

@ -1190,27 +1190,6 @@ __NEXT_STEP:
py_clearexc(NULL);
DISPATCH();
}
case OP_BEGIN_FINALLY: {
if(self->curr_exception.type) {
assert(!self->is_curr_exc_handled);
// temporarily handle the exception if any
self->is_curr_exc_handled = true;
}
DISPATCH();
}
case OP_END_FINALLY: {
if(byte.arg == BC_NOARG) {
if(self->curr_exception.type) {
assert(self->is_curr_exc_handled);
// revert the exception handling if needed
self->is_curr_exc_handled = false;
}
} else {
// break or continue inside finally block
py_clearexc(NULL);
}
DISPATCH();
}
//////////////////
case OP_FORMAT_STRING: {
py_Ref spec = c11__at(py_TValue, &frame->co->consts, byte.arg);

View File

@ -146,7 +146,20 @@ except SyntaxError as e:
ok = True
assert ok
# nested try
def g():
try:
raise KeyError
except KeyError:
pass
if 0:
try:
raise IndexError
except IndexError:
g()
"""
# finally, only
def finally_only():
try:
@ -224,18 +237,4 @@ def finally_return():
return 1
assert finally_return() == 1
# nested try
def g():
try:
raise KeyError
except KeyError:
pass
if 0:
try:
raise IndexError
except IndexError:
g()
"""