Exemple #1
0
from hippy.builtin import (wrap_method, Optional, ThisUnwrapper, StringArg,
                           LongArg, BoolArg, InstanceUnwrapper,
                           handle_as_exception)
from hippy.klass import def_class
from hippy.builtin_klass import GetterSetterWrapper, k_Exception

from hippy.module.date import timelib
from hippy.module.date import W_DateTime, W_DateTimeZone
from hippy.module.date.dateinterval_klass import W_DateInterval, k_DateInterval
from hippy.module.date import common


@wrap_method([
    'interp',
    ThisUnwrapper(W_DateTime),
    Optional(StringArg()),
    Optional(InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone'))
],
             name='DateTime::__construct',
             error_handler=handle_as_exception)
def construct(interp, this, format_string=None, w_datetimezone=None):

    error = common.initialize_date(interp, 'DateTime::__construct', this,
                                   format_string, w_datetimezone)

    if error:
        raise PHPException(
            k_Exception.call_args(interp, [
                interp.space.wrap("%s(): %s" %
                                  ('DateTime::__construct', error))
            ]))
Exemple #2
0
        return space.w_True


@wrap(['space', W_Root])
def serialize(space, w_obj):
    """ Generates a storable representation of a value"""
    return space.newstr(space.serialize(w_obj))


@wrap(['space', W_Root])
def strval(space, w_obj):
    """ Get string value of a variable"""
    return space.wrap(w_obj.str(space, quiet=False))


@wrap(['space', StringArg(None)], error=False)
def unserialize(space, data):
    """ Creates a PHP value from a stored representation"""
    return serialize_mod.unserialize(space, data)


@wrap(['interp', 'args_w'])
def var_dump(interp, args_w):
    """ Dumps information about a variable"""
    for w_x in args_w:
        if w_x:
            w_x = w_x.copy()  # xxx ideally we should ask and get a
            # unique object, but that's not supported
            # so far
            interp.writestr(w_x.var_dump(interp.space, indent='',
                                         recursion={}))
Exemple #3
0
                return self._label_parse(s, pos_p, pos_ue)
            else:
                self.path = s[pos_s:pos_ue]
        return self

    def _label_parse(self, s, pos_p, pos_ue):
        pos_p += 1
        if pos_ue != pos_p:
            self.fragment = s[pos_p:pos_ue]
        return self

    def _just_path(self, s, pos_s):
        return self._nohost(s, pos_s, pos_s + len(s))


@wrap(['space', StringArg(None), Optional(BoolArg(None))])
def base64_decode(space, data, strict=False):
    res = b64_decode(data, strict)
    if res is None:
        return space.w_False
    return space.wrap(res)


@wrap(['space', StringArg(None)])
def base64_encode(space, data):
    res = b64_encode(data)
    if res is None:
        return space.w_False
    return space.wrap(res[:-1])

Exemple #4
0
from hippy.builtin import (wrap_method, Optional, ThisUnwrapper, StringArg,
                           LongArg, BoolArg, InstanceUnwrapper,
                           handle_as_exception)
from hippy.klass import def_class
from hippy.builtin_klass import GetterSetterWrapper, k_Exception

from hippy.module.date import timelib
from hippy.module.date import W_DateTime, W_DateTimeZone
from hippy.module.date.dateinterval_klass import W_DateInterval, k_DateInterval
from hippy.module.date import common


@wrap_method([
    'interp',
    ThisUnwrapper(W_DateTime),
    Optional(StringArg()),
    Optional(InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone'))
],
             name='DateTime::__construct',
             error_handler=handle_as_exception)
def construct(interp, this, format_string=None, w_datetimezone=None):
    error = common.initialize_date(interp, 'DateTime::__construct', this,
                                   format_string, w_datetimezone)
    if error:
        raise PHPException(
            k_Exception.call_args(interp, [
                interp.space.wrap("%s(): %s" %
                                  ('DateTime::__construct', error))
            ]))

Exemple #5
0
def dns_get_mx():
    """ Alias of getmxrr"""
    return NotImplementedError()


def dns_get_record():
    """ Fetch DNS Resource Records associated with a hostname"""
    return NotImplementedError()


def fsockopen():
    """ Open Internet or Unix domain socket connection"""
    return NotImplementedError()


@wrap(['space', StringArg(None)])
def gethostbyaddr(space, ip_address):
    """ Get the Internet host name corresponding to a given IP address"""
    h, _, _ = rsocket.gethostbyaddr(ip_address)
    return space.newstr(h)


@wrap(['space', StringArg(None)])
def gethostbyname(space, name):
    """ Get the IPv4 address corresponding to a given Internet host name"""
    h = rsocket.gethostbyname(name)
    return space.newstr(h.get_host())


@wrap(['space', StringArg(None)])
def gethostbynamel(space, name):
Exemple #6
0
    void = lltype.nullptr(rffi.VOIDP.TO)
    timelib.c_gettimeofday(timeval, void)
    sec = intmask(timeval.c_tv_sec)
    usec = intmask(timeval.c_tv_usec)
    builder = StringBuilder()
    if prefix:
        builder.append(prefix)
    builder.append(_zero_pad(hex(sec)[2:], 8))
    builder.append(_zero_pad(hex(usec)[2:], 5))
    if more_entropy:
        builder.append(".")
        builder.append(str(_random.random())[2:11])
    return space.newstr(builder.build())


@wrap(['space', StringArg(None), StringArg(None)])
def unpack(space, formats, string):
    """ Unpack data from binary string"""
    return _unpack(space, formats, string)


@wrap(['interp', int])
def usleep(interp, microseconds):
    """ Delay execution in microseconds"""
    if microseconds < 0:
        interp.warn(
            "usleep(): Number of microseconds must be greater than or equal to 0"
        )
        return interp.space.w_False

    import time
from hippy import consts
from hippy.builtin import wrap_method, Optional, ThisUnwrapper
from hippy.builtin import StringArg
from hippy.builtin import LongArg
from hippy.builtin import InstanceUnwrapper, handle_as_exception
from hippy.klass import def_class
from hippy.objspace import getspace

from hippy.module.date import timelib
from hippy.module.date import W_DateTimeZone, W_DateTime
from hippy.module.date import common




@wrap_method(['interp', ThisUnwrapper(W_DateTimeZone), StringArg()],
             name='DateTimeZone::__construct', error_handler=handle_as_exception)
def construct(interp, this, timezone_name):
    common.initialize_timezone(interp, "DateTimeZone::__construct", this, timezone_name)


@wrap_method(['space', ThisUnwrapper(W_DateTimeZone)],
             name='DateTimeZone::getName', error=False)
def get_name(space, this):
    return space.wrap(this.timezone_info.get_name())


@wrap_method(['interp', ThisUnwrapper(W_DateTimeZone),
              InstanceUnwrapper(W_DateTime, 'DateTime', null=False)],
             name='DateTimeZone::getOffset', error=False)
def get_offset(interp, this, w_datetime):
Exemple #8
0
from hippy import consts
from hippy.builtin import wrap_method, Optional, ThisUnwrapper
from hippy.builtin import StringArg
from hippy.builtin import LongArg
from hippy.builtin import InstanceUnwrapper, handle_as_exception
from hippy.klass import def_class
from hippy.objspace import getspace

from hippy.module.date import timelib
from hippy.module.date import W_DateTimeZone, W_DateTime
from hippy.module.date import common


@wrap_method(['interp', ThisUnwrapper(W_DateTimeZone),
              StringArg()],
             name='DateTimeZone::__construct',
             error_handler=handle_as_exception)
def construct(interp, this, timezone_name):
    common.initialize_timezone(interp, "DateTimeZone::__construct", this,
                               timezone_name)


@wrap_method(['space', ThisUnwrapper(W_DateTimeZone)],
             name='DateTimeZone::getName',
             error=False)
def get_name(space, this):
    return space.wrap(rffi.charp2str(this.timelib_timezone.c_name))


@wrap_method([
Exemple #9
0
                return self._label_parse(s, pos_p, pos_ue)
            else:
                self.path = s[pos_s:pos_ue]
        return self

    def _label_parse(self, s, pos_p, pos_ue):
        pos_p += 1
        if pos_ue != pos_p:
            self.fragment = s[pos_p:pos_ue]
        return self

    def _just_path(self, s, pos_s):
        return self._nohost(s, pos_s, pos_s + len(s))


@wrap(['space', StringArg(None), Optional(BoolArg(None))])
def base64_decode(space, data, strict=False):
    res = b64_decode(data, strict)
    if res is None:
        return space.w_False
    return space.wrap(res)


@wrap(['space', StringArg(None)])
def base64_encode(space, data):
    res = b64_encode(data)
    if res is None:
        return space.w_False
    return space.wrap(res[:-1])

Exemple #10
0
    for i in range(len(interp.autoload_stack)):
        w_autoload1, _ = interp.autoload_stack[i]
        if interp.space.eq_w(w_autoload1, w_autoload):
            del interp.autoload_stack[i]
            return interp.space.w_True
    else:
        return interp.space.w_False


@wrap(['interp'], error=False)
def spl_autoload_functions(interp):
    return interp.space.new_array_from_list([
        w_autoload1 for (w_autoload1, _) in interp.autoload_stack])


@wrap(['interp', Optional(StringArg())], error=False)
def spl_autoload_extensions(interp, file_extensions=None):
    if file_extensions:
        interp.autoload_extensions = file_extensions.split(",")

    return interp.space.wrap(",".join(interp.autoload_extensions))


def _spl_autoload(interp, class_name, file_extensions_list):
    class_id = class_name.lower()

    for extension in file_extensions_list:
        for path in interp.include_path:
            fname = rpath.join(path, ["%s%s" % (class_id, extension)])
            if rpath.exists(fname):
                bc = interp.compile_file(fname)
Exemple #11
0
    date.timelib_time.c_relative.c_d = timelib.timelib_daynr_from_weeknr(
        year, week, day)
    date.timelib_time.c_have_relative = rffi.cast(
        timelib.timelib_time.TO.c_have_relative, 1)

    timelib.timelib_update_ts(date.timelib_time,
                              lltype.nullptr(timelib.timelib_tzinfo.TO))

    return date


@wrap([
    'interp',
    InstanceUnwrapper(W_DateTime, 'DateTime', null=False),
    StringArg()
],
      error=False)
def date_modify(interp, date, string_modifier):
    date.timelib_time = timelib.timelib_time_modify(
        date.timelib_time, string_modifier, date.timezone.timelib_timezone)

    return date


@wrap(['space',
       InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone', null=False)],
      error=False)
def timezone_name_get(space, w_datetimezone):
    return space.wrap(rffi.charp2str(w_datetimezone.timelib_timezone.c_name))
Exemple #12
0
from rpython.rlib.rarithmetic import intmask

from hippy import consts
from hippy.error import PHPException
from hippy.builtin import wrap_method, ThisUnwrapper, StringArg
from hippy.builtin_klass import GetterSetterWrapper, k_Exception
from hippy.klass import def_class
from hippy.module.date import timelib
from hippy.objects.instanceobject import W_InstanceObject


class W_DateInterval(W_InstanceObject):
    pass


@wrap_method(['interp', ThisUnwrapper(W_DateInterval), StringArg(None)],
             name='DateInterval::__construct')
def construct(interp, this, spec):

    exc_obj = k_Exception.call_args(
        interp, [interp.space.wrap('Unknown or bad format (%s)' % spec)]
    )

    if not (len(spec) > 1 and spec[0] == 'P'):
        raise PHPException(exc_obj)

    index = 1
    time = False
    formats = {'y': 0, 'm': 0, 'd':0, 'h':0, 'i':0 ,'s': 0}

    while index < len(spec):
Exemple #13
0
            results.append((self._get_fmtdect(char), rep, name))
        return results

    @jit.unroll_safe  # assuming that the format string isn't too crazy
    def build(self):
        self.fmt_interpreted = self.interpret()

        for fmtdesc, repetitions, name in self.fmt_interpreted:
            if repetitions < 0:
                repetitions = len(self.string) - self.string_index
                if fmtdesc.fmtchar in ('h', 'H'):
                    repetitions *= 2
            fmtdesc.unpack(self, fmtdesc, repetitions, name)
        return self.result


@wrap(['space', StringArg(None), 'args_w'])
def pack(space, formats, args_w):
    results = Pack(space, formats, args_w).build()
    return space.newstr(results)


def _unpack(space, formats, string):
    try:
        pairs = Unpack(space, formats, string).build()
    except FormatException as e:
        space.ec.warn("unpack(): %s" % (e.message))

        return space.w_False
    return space.new_array_from_pairs(pairs)
Exemple #14
0
        return interp.space.wrap(result)
    return interp.space.w_False



@wrap(['interp', Optional(str),
       Optional(InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone'))], error=False)
def date_create(interp, format_string=None, w_datetimezone=None):
    date = W_DateTime(k_DateTime, [])
    error = common.initialize_date(interp, 'date_create', date, format_string, w_datetimezone)
    if error:
        return interp.space.w_False
    else:
        return date

@wrap(['interp', StringArg(), StringArg(),
       Optional(InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone'))], error=False)
def date_create_from_format(interp, format_string, time_string, w_datetimezone=None):
    w_date = W_DateTime(interp._class_get('DateTime'), [])

    error = common.initialize_date_from_format(
        interp, 'date_create_from_format',
        w_date, format_string, time_string,
        w_datetimezone
    )

    if error:
        return interp.space.w_False
    else:
        return date