Skip to content

Deletion issues #46

@ferdnyc

Description

@ferdnyc

I discovered this while working on #45, but it's equally true in the master branch.

The problems

A: Deletes work at the class level

All of this code is fine, and can be run in tinypy without issue:

class D:
    a = 12
    b = 14
    c = []

    def __init__(self):
        for i in range(20):
            D.c.append(i)

    def delete(self):
        del D.a
        del D.b

    def delete_list_elements(self):
        D.c.pop(2)
        D.c.pop(4)

    def delete_list(self):
        del D.c

B: They work at the instance level, except when deleting a class collection

All of this code compiles fine, but will hit a runtime error when it tries to execute e.delete_list() (where e = E()):

class E:
    a = 99
    b = 100
    c = []

    def __init__(self):
        self.a = 22
        self.b = 44
        for i in range(10):
            self.c.append(i)

    def delete(self):
        del self.a
        del self.b

    def delete_list_elements(self):
        self.c.pop(2)
        self.c.pop(4)

    def delete_list(self):
        del self.c            # <== Runtime FAIL

C: Deletes at the function/script level fail to even compile

Any of THIS code will never make it through the bytecode-compilation stage:

def create_delete():
    x = 2
    y = 4
    del x      # <== Compile FAIL
    del y      # <== Compile FAIL

m = 9
n = 10
del m      # <== Compile FAIL
del n      # <== Compile FAIL

Demonstrating A and B

With this test code:

print("Creating d")
d = D()
print("Calling d.delete")
d.delete()
print("Calling d.delete_list_elements()")
d.delete_list_elements()
print("Calling d.delete_list()")
d.delete_list()
print("Creating e")
e = E()
print("Calling e.delete()")
e.delete()
print("Calling e.delete_list_elements()")
e.delete_list_elements()
print("calling e.delete_list()")
e.delete_list()
print("Done.")

The runtime error looks like this:

$ ./tpy delete_bug.py
Creating d
Calling d.delete
Calling d.delete_list_elements()
Calling d.delete_list()
Creating e
Calling e.delete()
Calling e.delete_list_elements()
calling e.delete_list()

File "delete_bug.py", line 63, in ?
 e.delete_list()
File "delete_bug.py", line 45, in delete_list
         del self.c

Exception:
(tpd_dict_del) KeyError: c
[1]    269283 IOT instruction (core dumped)  ./tpy delete_bug.py

Demonstrating C

The compile errors are uglier:

$ ./tpy delete_bug2.py
File "tinypy/compiler/py2bc.py", line 9, in compile
     r = encode.encode(fname,s,t)
File "tinypy/compiler/encode.py", line 798, in encode
     do(t)
File "tinypy/compiler/encode.py", line 785, in do
         return fmap[t.type](t)
File "tinypy/compiler/encode.py", line 761, in do_module
     free_tmp(do(t.items[0])) #REG
File "tinypy/compiler/encode.py", line 785, in do
         return fmap[t.type](t)
File "tinypy/compiler/encode.py", line 731, in do_statements
     for tt in t.items: free_tmp(do(tt))
File "tinypy/compiler/encode.py", line 785, in do
         return fmap[t.type](t)
File "tinypy/compiler/encode.py", line 555, in do_def
     free_tmp(do(items[2])) #REG
File "tinypy/compiler/encode.py", line 785, in do
         return fmap[t.type](t)
File "tinypy/compiler/encode.py", line 731, in do_statements
     for tt in t.items: free_tmp(do(tt))
File "tinypy/compiler/encode.py", line 785, in do
         return fmap[t.type](t)
File "tinypy/compiler/encode.py", line 465, in do_del
         r = do(t.items[0])

Exception:
(tp_get) TypeError: ?
[1]    269743 IOT instruction (core dumped)  ./tpy delete_bug2.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions