def make_cmp_method(op):
    def descr_op(self, space, w_other):
        return getattr(space, op)(self.w_mapping, w_other)
    descr_name = 'descr_' + op
    descr_op.__name__ = descr_name
    setattr(W_DictProxyObject, descr_name, descr_op)
    cmp_methods['__%s__' % op] = interp2app(getattr(W_DictProxyObject, descr_name))
Beispiel #2
0
def make_cmp_method(op):
    def descr_op(self, space, w_other):
        return getattr(space, op)(self.w_mapping, w_other)
    descr_name = 'descr_' + op
    descr_op.__name__ = descr_name
    setattr(W_DictProxyObject, descr_name, descr_op)
    cmp_methods['__%s__' % op] = interp2app(getattr(W_DictProxyObject, descr_name))
    def setup_class(cls):
        if option.runappdirect:
            py.test.skip("interp only test")
        from pypy.interpreter.typedef import TypeDef, interp2app
        from pypy.interpreter.baseobjspace import Wrappable

        class W_Stuff(Wrappable):
            pass

        def descr_new(space, w_subtype):
            return W_Stuff()

        W_Stuff.typedef = TypeDef(
            'Stuff',
            __new__=interp2app(descr_new),
        )
        cls.w_Stuff = cls.space.gettypefor(W_Stuff)
Beispiel #4
0
    def setup_class(cls):
        if cls.runappdirect:
            py.test.skip("interp only test")
        from pypy.interpreter.typedef import TypeDef, interp2app
        from pypy.interpreter.baseobjspace import W_Root

        class W_Stuff(W_Root):
            pass

        def descr_new(space, w_subtype):
            return W_Stuff()

        W_Stuff.typedef = TypeDef(
            'Stuff',
            __new__ = interp2app(descr_new),
        )
        cls.w_Stuff = cls.space.gettypefor(W_Stuff)
Beispiel #5
0
    also accepts optional keyword arguments which override settings
    provided by the dialect.

    The returned object is an iterator.  Each iteration returns a row
    of the CSV file (which can span multiple input lines)"""
    w_iter = space.iter(w_iterator)
    dialect = _build_dialect(space, w_dialect, w_delimiter, w_doublequote,
                             w_escapechar, w_lineterminator, w_quotechar,
                             w_quoting, w_skipinitialspace, w_strict)
    return W_Reader(space, dialect, w_iter)

W_Reader.typedef = TypeDef(
        '_csv.reader',
        dialect = interp_attrproperty_w('dialect', W_Reader),
        line_num = interp_attrproperty('line_num', W_Reader),
        __iter__ = interp2app(W_Reader.iter_w),
        next = interp2app(W_Reader.next_w),
        __doc__ = """CSV reader

Reader objects are responsible for reading and parsing tabular data
in CSV format.""")
W_Reader.typedef.acceptable_as_base_class = False

# ____________________________________________________________

class FieldLimit:
    limit = 128 * 1024   # max parsed field size
field_limit = FieldLimit()

@unwrap_spec(new_limit=int)
def csv_field_size_limit(space, new_limit=-1):
Beispiel #6
0
        self.last_ec = ec
        self.last_dict = w_dict
        return w_dict

    def descr_local__new__(space, w_subtype, __args__):
        local = space.allocate_instance(Local, w_subtype)
        Local.__init__(local, space, __args__)
        return space.wrap(local)

    def descr_local__init__(self, space):
        # No arguments allowed
        pass

Local.typedef = TypeDef("thread._local",
                        __doc__ = "Thread-local data",
                        __new__ = interp2app(Local.descr_local__new__.im_func),
                        __init__ = interp2app(Local.descr_local__init__),
                        __dict__ = GetSetProperty(descr_get_dict, cls=Local),
                        )

def thread_is_stopping(ec):
    tlobjs = ec._thread_local_objs
    if tlobjs is None:
        return
    ec._thread_local_objs = None
    for wref in tlobjs.items():
        local = wref()
        if local is not None:
            del local.dicts[ec]
            local.last_dict = None
            local.last_ec = None
Beispiel #7
0
                  w_strict           = None,
                  ):
    """
    csv_writer = csv.writer(fileobj [, dialect='excel']
                            [optional keyword args])
    for row in sequence:
        csv_writer.writerow(row)

    [or]

    csv_writer = csv.writer(fileobj [, dialect='excel']
                            [optional keyword args])
    csv_writer.writerows(rows)

    The \"fileobj\" argument can be any object that supports the file API."""
    dialect = _build_dialect(space, w_dialect, w_delimiter, w_doublequote,
                             w_escapechar, w_lineterminator, w_quotechar,
                             w_quoting, w_skipinitialspace, w_strict)
    return W_Writer(space, dialect, w_fileobj)

W_Writer.typedef = TypeDef(
        '_csv.writer',
        dialect = interp_attrproperty_w('dialect', W_Writer),
        writerow = interp2app(W_Writer.writerow),
        writerows = interp2app(W_Writer.writerows),
        __doc__ = """CSV writer

Writer objects are responsible for generating tabular data
in CSV format from sequence input.""")
W_Writer.typedef.acceptable_as_base_class = False
Beispiel #8
0
    w_strict=None,
):
    """
    csv_writer = csv.writer(fileobj [, dialect='excel']
                            [optional keyword args])
    for row in sequence:
        csv_writer.writerow(row)

    [or]

    csv_writer = csv.writer(fileobj [, dialect='excel']
                            [optional keyword args])
    csv_writer.writerows(rows)

    The \"fileobj\" argument can be any object that supports the file API."""
    dialect = _build_dialect(space, w_dialect, w_delimiter, w_doublequote,
                             w_escapechar, w_lineterminator, w_quotechar,
                             w_quoting, w_skipinitialspace, w_strict)
    return W_Writer(space, dialect, w_fileobj)


W_Writer.typedef = TypeDef('_csv.writer',
                           dialect=interp_attrproperty_w('dialect', W_Writer),
                           writerow=interp2app(W_Writer.writerow),
                           writerows=interp2app(W_Writer.writerows),
                           __doc__="""CSV writer

Writer objects are responsible for generating tabular data
in CSV format from sequence input.""")
W_Writer.typedef.acceptable_as_base_class = False
Beispiel #9
0
        if not (flags & structmemberdefs.READONLY):
            set = GettersAndSetters.member_setter.im_func
        GetSetProperty.__init__(self,
                                get,
                                set,
                                del_,
                                doc,
                                cls=None,
                                use_closure=True,
                                tag="cpyext_2")


# change the typedef name
W_MemberDescr.typedef = TypeDef(
    "member_descriptor",
    __get__=interp2app(GetSetProperty.descr_property_get),
    __set__=interp2app(GetSetProperty.descr_property_set),
    __delete__=interp2app(GetSetProperty.descr_property_del),
    __name__=interp_attrproperty('name',
                                 cls=GetSetProperty,
                                 wrapfn="newtext_or_none"),
    __objclass__=GetSetProperty(GetSetProperty.descr_get_objclass),
    __doc__=interp_attrproperty('doc',
                                cls=GetSetProperty,
                                wrapfn="newtext_or_none"),
)
assert not W_MemberDescr.typedef.acceptable_as_base_class  # no __new__


@bootstrap_function
def init_memberdescrobject(space):
Beispiel #10
0
                space.w_TypeError,
                space.wrap("setting dictionary to a non-dict"))
        self.getdict()  # force a dict to exist first
        ident = thread.get_ident()
        self.dicts[ident] = w_dict

    def descr_local__new__(space, w_subtype, __args__):
        # XXX check __args__
        local = space.allocate_instance(Local, w_subtype)
        Local.__init__(local, space, __args__)
        return space.wrap(local)


Local.typedef = TypeDef(
    "thread._local",
    __doc__="Thread-local data",
    __new__=interp2app(Local.descr_local__new__.im_func,
                       unwrap_spec=[ObjSpace, W_Root, Arguments]),
    __dict__=GetSetProperty(descr_get_dict, descr_set_dict, cls=Local),
)


def getlocaltype(space):
    return space.gettypeobject(Local.typedef)


def finish_thread(w_obj):
    assert isinstance(w_obj, Local)
    ident = thread.get_ident()
    del w_obj.dicts[ident]
cmp_methods = {}
def make_cmp_method(op):
    def descr_op(self, space, w_other):
        return getattr(space, op)(self.w_mapping, w_other)
    descr_name = 'descr_' + op
    descr_op.__name__ = descr_name
    setattr(W_DictProxyObject, descr_name, descr_op)
    cmp_methods['__%s__' % op] = interp2app(getattr(W_DictProxyObject, descr_name))

for op in ['eq', 'ne', 'gt', 'ge', 'lt', 'le']:
    make_cmp_method(op)


W_DictProxyObject.typedef = TypeDef(
    'mappingproxy',
    __new__=interp2app(W_DictProxyObject.descr_new),
    __init__=interp2app(W_DictProxyObject.descr_init),
    __len__=interp2app(W_DictProxyObject.descr_len),
    __getitem__=interp2app(W_DictProxyObject.descr_getitem),
    __contains__=interp2app(W_DictProxyObject.descr_contains),
    __iter__=interp2app(W_DictProxyObject.descr_iter),
    __str__=interp2app(W_DictProxyObject.descr_str),
    __repr__=interp2app(W_DictProxyObject.descr_repr),
    get=interp2app(W_DictProxyObject.get_w),
    keys=interp2app(W_DictProxyObject.keys_w),
    values=interp2app(W_DictProxyObject.values_w),
    items=interp2app(W_DictProxyObject.items_w),
    copy=interp2app(W_DictProxyObject.copy_w),
    **cmp_methods
)
Beispiel #12
0
cmp_methods = {}
def make_cmp_method(op):
    def descr_op(self, space, w_other):
        return getattr(space, op)(self.w_mapping, w_other)
    descr_name = 'descr_' + op
    descr_op.__name__ = descr_name
    setattr(W_DictProxyObject, descr_name, descr_op)
    cmp_methods['__%s__' % op] = interp2app(getattr(W_DictProxyObject, descr_name))

for op in ['eq', 'ne', 'gt', 'ge', 'lt', 'le']:
    make_cmp_method(op)


W_DictProxyObject.typedef = TypeDef(
    'dictproxy',
    __new__=interp2app(W_DictProxyObject.descr_new),
    __init__=interp2app(W_DictProxyObject.descr_init),
    __len__=interp2app(W_DictProxyObject.descr_len),
    __getitem__=interp2app(W_DictProxyObject.descr_getitem),
    __contains__=interp2app(W_DictProxyObject.descr_contains),
    __iter__=interp2app(W_DictProxyObject.descr_iter),
    __str__=interp2app(W_DictProxyObject.descr_str),
    __repr__=interp2app(W_DictProxyObject.descr_repr),
    get=interp2app(W_DictProxyObject.get_w),
    keys=interp2app(W_DictProxyObject.keys_w),
    iterkeys=interp2app(W_DictProxyObject.descr_iterkeys),
    values=interp2app(W_DictProxyObject.values_w),
    itervalues=interp2app(W_DictProxyObject.descr_itervalues),
    items=interp2app(W_DictProxyObject.items_w),
    iteritems=interp2app(W_DictProxyObject.descr_iteritems),
    copy=interp2app(W_DictProxyObject.copy_w),
Beispiel #13
0
    def setdict(self, space, w_dict):
        if not space.is_true(space.isinstance(w_dict, space.w_dict)):
            raise OperationError(space.w_TypeError,
                                space.wrap("setting dictionary to a non-dict"))
        self.getdict()   # force a dict to exist first
        ident = thread.get_ident()
        self.dicts[ident] = w_dict

    def descr_local__new__(space, w_subtype, __args__):
        # XXX check __args__
        local = space.allocate_instance(Local, w_subtype)
        Local.__init__(local, space, __args__)
        return space.wrap(local)

Local.typedef = TypeDef("thread._local",
                        __doc__ = "Thread-local data",
                        __new__ = interp2app(Local.descr_local__new__.im_func,
                                    unwrap_spec=[ObjSpace, W_Root, Arguments]),
                        __dict__ = GetSetProperty(descr_get_dict,
                                                  descr_set_dict, cls=Local),
                        )

def getlocaltype(space):
    return space.gettypeobject(Local.typedef)


def finish_thread(w_obj):
    assert isinstance(w_obj, Local)
    ident = thread.get_ident()
    del w_obj.dicts[ident]
Beispiel #14
0
        flags = rffi.cast(lltype.Signed, member.c_flags)
        doc = set = None
        if member.c_doc:
            doc = rffi.charp2str(member.c_doc)
        get = GettersAndSetters.member_getter.im_func
        del_ = GettersAndSetters.member_delete.im_func
        if not (flags & structmemberdefs.READONLY):
            set = GettersAndSetters.member_setter.im_func
        GetSetProperty.__init__(self, get, set, del_, doc,
                                cls=None, use_closure=True,
                                tag="cpyext_2")

# change the typedef name
W_MemberDescr.typedef = TypeDef(
    "member_descriptor",
    __get__ = interp2app(GetSetProperty.descr_property_get),
    __set__ = interp2app(GetSetProperty.descr_property_set),
    __delete__ = interp2app(GetSetProperty.descr_property_del),
    __name__ = interp_attrproperty('name', cls=GetSetProperty),
    __objclass__ = GetSetProperty(GetSetProperty.descr_get_objclass),
    __doc__ = interp_attrproperty('doc', cls=GetSetProperty),
    )
assert not W_MemberDescr.typedef.acceptable_as_base_class  # no __new__

PyDescrObject = lltype.ForwardReference()
PyDescrObjectPtr = lltype.Ptr(PyDescrObject)
PyDescrObjectFields = PyObjectFields + (
    ("d_type", PyTypeObjectPtr),
    ("d_name", PyObject),
    )
cpython_struct("PyDescrObject", PyDescrObjectFields,
Beispiel #15
0
    def __init__(self, space):
        self.costate = AppCoState(space)
        self.costate.post_install()

    def descr_method__new__(space, w_subtype):
        costate = space.allocate_instance(W_UserCoState, w_subtype)
        W_UserCoState.__init__(costate, space)
        return space.wrap(costate)

    def w_getcurrent(self):
        space = self.costate.space
        return space.wrap(self.costate.current)

    def w_spawn(self, w_subtype=None):
        space = self.costate.space
        if space.is_w(w_subtype, space.w_None):
            w_subtype = space.gettypeobject(AppCoroutine.typedef)
        co = space.allocate_instance(AppCoroutine, w_subtype)
        AppCoroutine.__init__(co, space, state=self.costate)
        return space.wrap(co)


W_UserCoState.typedef = TypeDef(
    "usercostate",
    __new__=interp2app(W_UserCoState.descr_method__new__.im_func),
    __module__='_stackless',
    getcurrent=interp2app(W_UserCoState.w_getcurrent),
    spawn=interp2app(W_UserCoState.w_spawn),
)
W_UserCoState.acceptable_as_base_class = False
Beispiel #16
0
    also accepts optional keyword arguments which override settings
    provided by the dialect.

    The returned object is an iterator.  Each iteration returns a row
    of the CSV file (which can span multiple input lines)"""
    w_iter = space.iter(w_iterator)
    dialect = _build_dialect(space, w_dialect, w_delimiter, w_doublequote,
                             w_escapechar, w_lineterminator, w_quotechar,
                             w_quoting, w_skipinitialspace, w_strict)
    return W_Reader(space, dialect, w_iter)

W_Reader.typedef = TypeDef(
        '_csv.reader',
        dialect = interp_attrproperty_w('dialect', W_Reader),
        line_num = interp_attrproperty('line_num', W_Reader),
        __iter__ = interp2app(W_Reader.iter_w),
        __next__ = interp2app(W_Reader.next_w),
        __doc__ = """CSV reader

Reader objects are responsible for reading and parsing tabular data
in CSV format.""")
W_Reader.typedef.acceptable_as_base_class = False

# ____________________________________________________________

class FieldLimit:
    limit = 128 * 1024   # max parsed field size
field_limit = FieldLimit()

@unwrap_spec(new_limit=int)
def csv_field_size_limit(space, new_limit=-1):
Beispiel #17
0
        return w_dict

    def descr_local__new__(space, w_subtype, __args__):
        local = space.allocate_instance(Local, w_subtype)
        Local.__init__(local, space, __args__)
        return local

    def descr_local__init__(self, space):
        # No arguments allowed
        pass


Local.typedef = TypeDef(
    "thread._local",
    __doc__="Thread-local data",
    __new__=interp2app(Local.descr_local__new__.im_func),
    __init__=interp2app(Local.descr_local__init__),
    __dict__=GetSetProperty(descr_get_dict, cls=Local),
)


def thread_is_stopping(ec):
    tlobjs = ec._thread_local_objs
    if tlobjs is None:
        return
    ec._thread_local_objs = None
    for wref in tlobjs.items():
        local = wref()
        if local is not None:
            del local.dicts[ec]
            local.last_dict = None

class W_UserCoState(Wrappable):
    def __init__(self, space):
        self.costate = AppCoState(space)
        self.costate.post_install()

    def descr_method__new__(space, w_subtype):
        costate = space.allocate_instance(W_UserCoState, w_subtype)
        W_UserCoState.__init__(costate, space)
        return space.wrap(costate)

    def w_getcurrent(self):
        space = self.costate.space
        return space.wrap(self.costate.current)

    def w_spawn(self, w_subtype=None):
        space = self.costate.space
        if space.is_w(w_subtype, space.w_None):
            w_subtype = space.gettypeobject(AppCoroutine.typedef)
        co = space.allocate_instance(AppCoroutine, w_subtype)
        AppCoroutine.__init__(co, space, state=self.costate)
        return space.wrap(co)

W_UserCoState.typedef = TypeDef("usercostate",
    __new__ = interp2app(W_UserCoState.descr_method__new__.im_func),
    __module__ = '_stackless',
    getcurrent = interp2app(W_UserCoState.w_getcurrent),
    spawn      = interp2app(W_UserCoState.w_spawn),
)
Beispiel #19
0
                            self.name, length)
            return self.call_o(space, h_self, __args__.arguments_w[skip_args])

        if sig == llapi.HPyFunc_VARARGS:
            return self.call_varargs_kw(space,
                                        h_self,
                                        __args__,
                                        skip_args,
                                        has_keywords=False)
        else:  # shouldn't happen!
            raise oefmt(space.w_RuntimeError, "unknown calling convention")


W_ExtensionFunction.typedef = TypeDef(
    'extension_function',
    __call__=interp2app(W_ExtensionFunction.descr_call),
    __doc__=interp_attrproperty('doc',
                                cls=W_ExtensionFunction,
                                wrapfn="newtext_or_none"),
)
W_ExtensionFunction.typedef.acceptable_as_base_class = False


class W_ExtensionMethod(W_ExtensionFunction):
    def __init__(self, space, name, sig, doc, cfuncptr, w_objclass):
        W_ExtensionFunction.__init__(self, space, name, sig, doc, cfuncptr,
                                     space.w_None)
        self.w_objclass = w_objclass

    def descr_call(self, space, __args__):
        # XXX: basically a copy of cpyext's W_PyCMethodObject.descr_call()
Beispiel #20
0
        # XXX: needs a stricter test
        if not space.isinstance_w(w_instance, self.w_objclass):
            w_objclass = self.w_objclass
            assert isinstance(w_objclass, W_TypeObject)
            raise oefmt(space.w_TypeError,
                "descriptor '%8' requires a '%s' object but received a '%T'",
                self.name, w_objclass.name, w_instance)
        #
        return self.call(space, __args__)

    def call(self, space, __args__):
        raise oefmt(space.w_RuntimeError, "bad slot wrapper")

W_SlotWrapper.typedef = TypeDef(
    'slot_wrapper',
    __get__ = interp2app(descr_function_get),
    __call__ = interp2app(W_SlotWrapper.descr_call),
    )
W_SlotWrapper.typedef.acceptable_as_base_class = False

# ~~~~~~~~~~ concrete W_SlotWrapper subclasses ~~~~~~~~~~~~~
# these are the equivalent of the various functions wrap_* inside CPython's typeobject.c

class W_wrap_binaryfunc(W_SlotWrapper):
    def call(self, space, __args__):
        func = llapi.cts.cast("HPyFunc_binaryfunc", self.cfuncptr)
        self.check_args(space, __args__, 2)
        ctx = space.fromcache(State).ctx
        w_self = __args__.arguments_w[0]
        w_other = __args__.arguments_w[1]
        with handles.using(space, w_self, w_other) as (h_self, h_other):