blueloveTH 1 jaar geleden
bovenliggende
commit
0b649f3bef
8 gewijzigde bestanden met toevoegingen van 27 en 19 verwijderingen
  1. 1 1
      include/pocketpy/pocketpy.h
  2. 4 5
      python/functools.py
  3. 0 0
      src/common/_generated.c
  4. 10 3
      src/common/sstream.c
  5. 1 1
      src2/main.c
  6. 0 0
      tests/42_decorator.py
  7. 11 1
      tests/43_closure.py
  8. 0 8
      tests/43_closure_ex.py

+ 1 - 1
include/pocketpy/pocketpy.h

@@ -373,7 +373,7 @@ int py_dict__len(py_Ref self);
 bool py_dict__apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx);
 
 /************* Others *************/
-int py_replinput(char* buf);
+int py_replinput(char* buf, int max_size);
 
 /// Python favored string formatting. (just put here, not for users)
 /// %d: int

+ 4 - 5
python/functools.py

@@ -1,5 +1,3 @@
-from pkpy import next
-
 class cache:
     def __init__(self, f):
         self.f = f
@@ -13,9 +11,10 @@ class cache:
 def reduce(function, sequence, initial=...):
     it = iter(sequence)
     if initial is ...:
-        value = next(it)
-        if value is StopIteration:
-            raise TypeError("reduce() of empty iterable with no initial value")
+        try:
+            value = next(it)
+        except StopIteration:
+            raise TypeError("reduce() of empty sequence with no initial value")
     else:
         value = initial
     for element in it:

File diff suppressed because it is too large
+ 0 - 0
src/common/_generated.c


+ 10 - 3
src/common/sstream.c

@@ -147,7 +147,7 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) {
     return retval;
 }
 
-void c11_sbuf__py_submit(c11_sbuf *self, py_Ref out){
+void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out) {
     c11_string* res = c11_sbuf__submit(self);
     py_newstrn(out, res->data, res->size);
     c11_string__delete(res);
@@ -233,7 +233,9 @@ void pk_sprintf(c11_sbuf* ss, const char* fmt, ...) {
     va_end(args);
 }
 
-int py_replinput(char* buf) {
+int py_replinput(char* buf, int max_size) {
+    buf[0] = '\0';
+
     int size = 0;
     bool multiline = false;
     printf(">>> ");
@@ -252,7 +254,7 @@ int py_replinput(char* buf) {
                     printf("... ");
                 }
             } else {
-                if(last == ':' || last == '(' || last == '[' || last == '{') {
+                if(last == ':' || last == '(' || last == '[' || last == '{' || buf[0] == '@') {
                     printf("... ");
                     multiline = true;
                 } else {
@@ -261,6 +263,11 @@ int py_replinput(char* buf) {
             }
         }
 
+        if(size == max_size - 1) {
+            buf[size] = '\0';
+            return size;
+        }
+
         buf[size++] = c;
     }
 

+ 1 - 1
src2/main.c

@@ -46,7 +46,7 @@ int main(int argc, char** argv) {
         printf("Type \"exit()\" to exit.\n");
 
         while(true) {
-            int size = py_replinput(buf);
+            int size = py_replinput(buf, sizeof(buf));
             assert(size < sizeof(buf));
             if(size >= 0) {
                 if(!py_exec(buf, "<stdin>", REPL_MODE, NULL)) py_printexc();

+ 0 - 0
tests/44_decorator.py → tests/42_decorator.py


+ 11 - 1
tests/42_closure.py → tests/43_closure.py

@@ -24,4 +24,14 @@ def f3(x, y):
     return lambda z: x + y + z
 
 a = f3(1, 2)
-assert a(3) == 6
+assert a(3) == 6
+
+# closure ex
+def f(n):
+    def g(x):
+        if x==n:
+            return n
+        return g(x+1)
+    return g(0)
+
+assert f(10) == 10

+ 0 - 8
tests/43_closure_ex.py

@@ -1,8 +0,0 @@
-def f(n):
-    def g(x):
-        if x==n:
-            return n
-        return g(x+1)
-    return g(0)
-
-assert f(10) == 10

Some files were not shown because too many files changed in this diff