debugging collections and ast and try forms

parent d6d944a9
......@@ -30,6 +30,7 @@ SOURCES = \
language/python/range.scm \
language/python/set.scm \
language/python/dir.scm \
language/python/module/_ast.scm \
language/python/checksum.scm \
language/python/format2.scm \
language/python/code.scm \
......@@ -223,7 +224,6 @@ SOURCES = \
language/python/module/inspect.py \
language/python/module/typing.py \
language/python/module/codeop.scm \
language/python/module/_ast.scm \
language/python/module/ast.py
......
This diff is collapsed.
......@@ -84,7 +84,7 @@
(define (get-pos y)
(aif it (object-property y 'position)
`(,(car it) ,(cdr it))
`(,(car it) ,(cadr it))
(list 0 0)))
(cond-expand
......@@ -867,7 +867,7 @@ empty list."
(((#:* x) . arg)
(cons (list '* (exp vs (getarg x)))
(lp arg)))
((x . args)
(lp args))
......@@ -1545,7 +1545,7 @@ empty list."
(dont-warn-l l)
`(,(C '@@values) ,l ,ll ,xl)))
((_ (#:from ("." . nn) l))
((_ (#:from ("." . nn) l))
;; Make sure to load the module in
(let* ((nm '())
(u (get-current-module))
......@@ -1880,7 +1880,7 @@ empty list."
(()
(reverse r))))
,@(if else `((#:except #t ,(exp vs else))) '())
,@(if else `((#:else ,(exp vs else))) '())
,@(if fin `(#:finally (,(G 'lambda) () ,(exp vs fin))) '()))))
(#:subexpr
......@@ -2308,6 +2308,7 @@ empty list."
x vs ll)))
(#:True #t)
(#:... 'Ellipsis)
(#:None (E 'None))
(#:null `(,(G 'quote) ()))
(#:False #f)
......@@ -3412,3 +3413,4 @@ empty list."
(pk (syntax->datum x))
#f))
(set! (@@ (language python module _ast) mk-string) mk-string)
......@@ -6,7 +6,12 @@
#:use-module (language python try)
#:use-module (language python list)
#:use-module (language python for)
#:use-module (language python def)
#:use-module (language python dict)
#:use-module ((language python module _ast) #:select
((compx . A:compx) (Module . A:Module) (comp . A:comp)
PyCF_ONLY_AST
(Expression . A:Expression)))
#:use-module (oop pf-objects)
#:use-module ((ice-9 local-eval) #:select ((the-environment . locals)))
#:re-export (locals)
......@@ -153,10 +158,25 @@ global environment @var{global}."
((pair? x)
(local-eval x locals globals))))
(define* (compile x filename mode
#:optional (flags 0) (dont_inherit #f) (optimize -1))
(or (and=> (p x) comp)
(raise SyntaxError)))
(def (compile x filename mode (= flags 0) (= dont_inherit #f) (= optimize -1))
(cond
((= (logand flags PyCF_ONLY_AST) PyCF_ONLY_AST)
(cond
((equal? mode "exec")
(or (and=> (and=> (p x) A:compx) A:Module)
(raise SyntaxError)))
(else
(or (and=> (and=> (pe x) A:comp) A:Expression)
(raise SyntaxError)))))
(else
(cond
((equal? mode "exec")
(or (and=> (p x) comp)
(raise SyntaxError)))
(else
(or (and=> (pe x) comp)
(raise SyntaxError)))))))
(define-syntax exec
(lambda (x)
......
......@@ -6,7 +6,7 @@
IndexError KeyError AttributeError ArgumentError
SyntaxError SystemException
OSError ProcessLookupError PermissionError
None NotImplemented NotImplementedError
None Ellipsis NotImplemented NotImplementedError
AssertionError ImportError
ModuleNotFoundError BlockingIOError
InterruptedError BaseException
......@@ -146,6 +146,7 @@
(define-python-class ProcessLookupError (OSError))
(define-python-class TimeOutError (OSError))
(define None 'None)
(define Ellipsis 'Ellipsis)
(define-er TypeError 'TypeError)
......
......@@ -423,7 +423,7 @@ property alist) using the data in ARGS."
(define pload
(lambda (p . q)
(define (run u lam)
(define (run u lam q)
(aif it (search-p
(string-append p "." (car u)))
(begin
......@@ -432,7 +432,7 @@ property alist) using the data in ARGS."
(pk "Compile File IA" it "to .go" go)
((@ (system base compile) compile-file)
it #:output-file go)))
(apply guile-load (get-go2 it)))
(apply guile-load (get-go2 it) q))
(aif it (%search-load-path (string-append
p "." (car u)))
(begin
......@@ -456,7 +456,7 @@ property alist) using the data in ARGS."
(if (pair? l)
(let lp2 ((u (caar l)))
(if (pair? u)
(run u (lambda () (lp2 (cdr u))))
(run u (lambda () (lp2 (cdr u))) q)
(lp (cdr l))))
(apply guile-load p q)))))
(lambda (k)
......@@ -465,7 +465,7 @@ property alist) using the data in ARGS."
(let lp2 ((u (caar l)))
(if (pair? u)
(run u (lambda ()
(lp2 (cdr u))))
(lp2 (cdr u))) q)
(lp (cdr l))))
(if (pair? q)
((car q))
......
......@@ -21,10 +21,9 @@
pylist-pop! pylist-count pylist-extend! len in
pylist-insert! pylist-remove! pylist-sort!
pylist-index pylist-null pylist-delete!
pylist pylist-listing Ellipsis Slice
pylist pylist-listing Slice
py-all py-any py-reversed))
(define-python-class Ellipsis ())
(define-python-class Slice ())
(define-syntax-rule (pset o n val . rest)
(cond
......
This diff is collapsed.
......@@ -45,7 +45,7 @@
tuple bytes bytearray locals globals
exec type object __import__ frozenset
Warning BytesWarning DeprecationWarning
py-list raise Ellipsis Slice
py-list Ellipsis Slice
)
#:export (print repr complex float int str
......@@ -65,7 +65,7 @@
((or guile-2.0 guile-2.2)
(module-re-export! mod l))))
(re-export! (current-module) '(send open eval))
(re-export! (current-module) '(send open eval raise))
(define (vars x)
(for ((k v : x)) ((l '()))
......@@ -223,7 +223,7 @@
(else
(next-method))))
(define-method (isinstance (i <real>) y)
(define-method (isinstance (i <real>) y)
(if (issubclass y float)
#t
(if (pair? y)
......
......@@ -20,7 +20,6 @@
"""
from _ast import *
def parse(source, filename='<unknown>', mode='exec', *,
type_comments=False, feature_version=None):
"""
......@@ -41,7 +40,6 @@ def parse(source, filename='<unknown>', mode='exec', *,
return compile(source, filename, mode, flags,
_feature_version=feature_version)
def literal_eval(node_or_string):
"""
Safely evaluate an expression node or a string containing a Python
......@@ -67,7 +65,7 @@ def literal_eval(node_or_string):
return - operand
return _convert_num(node)
def _convert(node):
if isinstance(node, Constant):
if isinstance(node, Constant):
return node.value
elif isinstance(node, Tuple):
return tuple(map(_convert, node.elts))
......@@ -89,7 +87,6 @@ def literal_eval(node_or_string):
return _convert_signed_num(node)
return _convert(node_or_string)
def dump(node, annotate_fields=True, include_attributes=False):
"""
Return a formatted dump of the tree in node. This is mainly useful for
......@@ -124,11 +121,11 @@ def dump(node, annotate_fields=True, include_attributes=False):
elif isinstance(node, list):
return '[%s]' % ', '.join(_format(x) for x in node)
return repr(node)
if not isinstance(node, AST):
raise TypeError('expected AST, got %r' % node.__class__.__name__)
return _format(node)
def copy_location(new_node, old_node):
"""
Copy source location (`lineno`, `col_offset`, `end_lineno`, and `end_col_offset`
......@@ -140,7 +137,6 @@ def copy_location(new_node, old_node):
setattr(new_node, attr, getattr(old_node, attr))
return new_node
def fix_missing_locations(node):
"""
When you compile a node tree with compile(), the compiler expects lineno and
......@@ -175,7 +171,6 @@ def fix_missing_locations(node):
_fix(node, 1, 0, 1, 0)
return node
def increment_lineno(node, n=1):
"""
Increment the line number and end line number of each node in the tree
......@@ -189,7 +184,6 @@ def increment_lineno(node, n=1):
child.end_lineno = getattr(child, 'end_lineno', 0) + n
return node
def iter_fields(node):
"""
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
......@@ -201,7 +195,6 @@ def iter_fields(node):
except AttributeError:
pass
def iter_child_nodes(node):
"""
Yield all direct child nodes of *node*, that is, all fields that are nodes
......@@ -215,7 +208,6 @@ def iter_child_nodes(node):
if isinstance(item, AST):
yield item
def get_docstring(node, clean=True):
"""
Return the docstring for the given node or None if no docstring can
......@@ -264,7 +256,6 @@ def _splitlines_no_ff(source):
lines.append(next_line)
return lines
def _pad_whitespace(source):
"""Replace all chars except '\f\t' in a line with spaces."""
result = ''
......@@ -275,7 +266,6 @@ def _pad_whitespace(source):
result += ' '
return result
def get_source_segment(source, node, *, padded=False):
"""Get source code segment of the *source* that generated *node*.
If some location information (`lineno`, `end_lineno`, `col_offset`,
......@@ -308,21 +298,19 @@ def get_source_segment(source, node, *, padded=False):
lines.append(last)
return ''.join(lines)
def walk(node):
"""
Recursively yield all descendant nodes in the tree starting at *node*
(including *node* itself), in no specified order. This is useful if you
only want to modify nodes in place and don't care about the context.
"""
from collections import deque
todo = deque([node])
import collections as c
todo = c.deque([node])
while todo:
node = todo.popleft()
todo.extend(iter_child_nodes(node))
yield node
class NodeVisitor(object):
"""
A node visitor base class that walks the abstract syntax tree and calls a
......@@ -377,7 +365,6 @@ class NodeVisitor(object):
return visitor(node)
return self.generic_visit(node)
class NodeTransformer(NodeVisitor):
"""
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
......@@ -428,7 +415,6 @@ class NodeTransformer(NodeVisitor):
setattr(node, field, new_node)
return node
# The following code is for backward compatibility.
# It will be removed in future.
......@@ -493,9 +479,11 @@ _const_types = {
NameConstant: (type(None), bool),
Ellipsis: (type(...),),
}
_const_types_not = {
Num: (bool,),
}
_const_node_type_names = {
bool: 'NameConstant', # should be before int
type(None): 'NameConstant',
......@@ -506,3 +494,4 @@ _const_node_type_names = {
bytes: 'Bytes',
type(...): 'Ellipsis',
}
......@@ -727,20 +727,24 @@
(pylist-ref d key))))))
(define-python-class deque ()
(define __bool__
(lambda (self)
(> (ref self '_i) 0)))
(define __init__
(lambda* (self #:optional (iterable '()) (maxlen None))
(let ((head (link)))
(set-prev! head head)
(set-next! head head)
(set self '_head head)
(set self 'maxlen maxlen)
(set self 'maxlen maxlen)
(for ((x : iterable)) ((i 0))
(if (eq? i maxlen)
(begin
(set self '_i i)
(break))
(begin
(pylist-append! self x)
(append self x)
(+ i 1)))
#:final
(set self '_i i)))))
......@@ -749,14 +753,16 @@
(lambda (self x)
(let ((m (ref self 'maxlen))
(i (ref self '_i)))
(if (= m (+ i 1))
(if (and (not (eq? m None)) (= m (+ i 1)))
(raise ValueError "deque reached its limit"))
(let ((head (ref self '_head))
(link (link)))
(let* ((head (ref self '_head))
(link (link))
(last (get-last head)))
(set-key! link x)
(set-prev! link (get-last head))
(set-prev! link last)
(set-next! link head)
(set-prev! head link)
(set-next! last link)
(set self '_i (+ i 1))))))
(define appendleft
......@@ -765,12 +771,14 @@
(i (ref self '_i)))
(if (= m (+ i 1))
(raise ValueError "deque reached its limit"))
(let ((head (ref self '_head))
(link (link)))
(set-key! link x)
(set-next! link (get-first head))
(set-prev! link head)
(set-next! head link)
(let* ((head (ref self '_head))
(link (link))
(first (get-first head)))
(set-key! link x)
(set-next! link first)
(set-prev! link head)
(set-next! head link)
(set-prev! first link)
(set self '_i (+ i 1))))))
(define clear
......
......@@ -71,10 +71,11 @@
(define-syntax handler_
(syntax-rules (=>)
((handler ecx)
((handler e ecx)
(m
(lambda (k tag . l)
(handler ecx tag l))))
(set! e
(handler ecx tag l)))))
((handler ((#:except E => lam) . ecx) tag l)
(if (check-exception E tag l)
......@@ -92,8 +93,8 @@
(handler ecx tag l)))
((handler ((#:else code ...)) tag l)
(begin code ...))
(error "No else clause notlast"))
((handler () tag l)
(apply throw 'python tag l))
......@@ -105,21 +106,47 @@
(define-syntax try
(syntax-rules ()
((try code exc ... (#:else els ...) #:finally fin)
(dynamic-wind
(lambda () #f)
(lambda ()
(let ((e #f))
(catch #t
code
(handler e (exc ...)))
(if e
e
(begin els ...))))
(lambda ()
(if (not (fluid-ref in-yield))
(fin)))))
((try code exc ... #:finally fin)
(dynamic-wind
(lambda () #f)
(lambda ()
(catch #t
code
(handler (exc ...))))
(let ((e #t))
(catch #t
code
(handler e (exc ...)))))
(lambda ()
(if (not (fluid-ref in-yield))
(fin)))))
((try code exc ... (#:else els ...))
(let ((e #f))
(catch #t
code
(mm (handler e (exc ...))))
(if e
e
(begin els ...))))
((try code exc ...)
(catch #t
code
(mm (handler (exc ...)))))))
(let ((e #t))
(catch #t
code
(mm (handler e (exc ...))))))))
(define raise
......
......@@ -464,6 +464,8 @@ explicitly tell it to not update etc.
(define-method (scheme? x) #f)
(define boolObj (slask-it #t))
(define (type-1 meta obj)
(catch #t
(lambda ()
......@@ -473,6 +475,9 @@ explicitly tell it to not update etc.
((eq? obj 'None)
NoneObj)
((eq? obj 'Ellipse)
EllipseObj)
(else
(aif it (find-in-class-raw obj '__class__ #f)
......@@ -1957,6 +1962,12 @@ explicitly tell it to not update etc.
(define-python-class NoneObj ()
(define __new__
(lambda x 'None)))
(set NoneObj '__name__ 'None)
(define-python-class EllipseObj ()
(define __new__
(lambda x 'None)))
(set EllipseObj '__name__ 'Ellipse)
(define-method (py-dict x)
(if (eq? x 'None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment