def di_construct(interp, this, path): if path == "": raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "Directory name must not be empty." )])) this.path = path this.file_name = path this.index = 0 if not os.path.isdir(path): raise PHPException(k_UnexpectedValueException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path)])) try: w_dir = W_DirResource(interp.space, path) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError # rare case, but annotation fix this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(): error while opening stream" )]))
def rdi_construct(interp, this, path, flags=FI_KEY_AS_PATHNAME | FI_CURRENT_AS_FILEINFO): if not os.path.isdir(path): raise PHPException( k_UnexpectedValueException.call_args(interp, [ interp.space.wrap( "RecursiveDirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path) ])) this.flags = flags this.path = path this.file_name = path this.index = 0 try: w_dir = W_DirResource(interp.space, path, this.flags & FI_SKIP_DOTS) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "RecursiveDirectoryIterator::__construct(): error while opening stream" ) ]))
def openfile(interp, this, open_mode='r', use_include_path=False, w_ctx=None): if open_mode == '': raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile(): Invalid parameters") ])) args = [ interp.space.wrap(this.file_name), interp.space.wrap(open_mode), interp.space.wrap(use_include_path) ] if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile() expects " "parameter 3 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower()) ])) args.append(w_ctx) try: file_object = SplFileObjectClass.call_args(interp, args) return file_object except OSError, e: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile(): %s" % os.strerror(e.errno)) ]))
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): format = None times = 0 if spec[index] == 'T': index += 1 time = True while spec[index].isdigit(): times = times * 10 times = times + (ord(spec[index]) - ord('0')) index += 1 if times: if spec[index] == 'Y': format = 'y' elif spec[index] == 'M' and not time: format = 'm' elif spec[index] == 'D': format = 'd' elif spec[index] == 'W': format = 'd' times *= 7 elif spec[index] == 'H': format = 'h' elif spec[index] == 'M' and time: format = 'i' elif spec[index] == 'S': format = 's' if not formats[format]: formats[format] = times else: raise PHPException(exc_obj) index += 1 this.time_diff = timelib.timelib_rel_time_ctor() this.time_diff.c_y = rffi.cast(lltype.Signed, formats['y']) this.time_diff.c_m = rffi.cast(lltype.Signed, formats['m']) this.time_diff.c_d = rffi.cast(lltype.Signed, formats['d']) this.time_diff.c_h = rffi.cast(lltype.Signed, formats['h']) this.time_diff.c_i = rffi.cast(lltype.Signed, formats['i']) this.time_diff.c_s = rffi.cast(lltype.Signed, formats['s'])
def sfo_seek(interp, this, line_pos): if line_pos < 0: raise PHPException(k_LogicException.call_args( interp, [interp.space.wrap( "SplFileObject::seek(): Can't seek file %s " "to negative line %d" % (this.file_name, line_pos))])) this.w_res.seek_to_line(line_pos, this.flags & SFO_DROP_NEW_LINE)
def sfo_get_current_line(interp, this): try: return _fgets(interp, this) except IOError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::fgets(): File cannot be read")]))
def sfo_rewind(interp, this): try: this.w_res.rewind() except OSError, e: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::rewind(): %s" % os.strerror(e.errno))]))
def _sfo_readline(interp, sfo): if sfo.open_mode not in ('w', 'a', 'x', 'c'): return sfo.w_res.readline(sfo.flags & SFO_DROP_NEW_LINE) else: raise PHPException( k_RuntimeException.call_args( interp, [interp.space.wrap("SplFileObject: File cannot be read")]))
def sfo_construct(interp, this, filename, open_mode='r', use_include_path=False, w_ctx=None): this.file_name = filename this.path_name = rpath.realpath(filename) this.delimiter = "," this.enclosure = '"' this.flags = 0 this.open_mode = open_mode this.use_include_path = use_include_path this.w_res = None this.max_line_len = 0 if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileObject::__construct() expects " "parameter 4 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower()) ])) assert filename is not None if os.path.isdir(filename): raise PHPException( k_LogicException.call_args(interp, [ interp.space.wrap("Cannot use SplFileObject with directories") ])) try: this.w_res = _fopen(interp.space, filename, this.open_mode, use_include_path, w_ctx) if this.w_res == interp.space.w_False: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileObject::__construct(): Failed to open stream") ])) except FopenError as e: raise PHPException( k_RuntimeException.call_args(interp, [interp.space.wrap(e.reasons.pop())]))
def get_linktarget(interp, this): filename = this.file_name assert filename is not None try: return interp.space.wrap(os.readlink(filename)) except OSError, e: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getLinkTarget(): %s" % os.strerror(e.errno))]))
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)) ]))
def get_type(interp, this): filename = this.file_name if not filename: return interp.space.w_False try: return _filetype(interp.space, filename) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getType(): stat failed for %s" % filename)]))
def getmtime(interp, this): filename = this.file_name assert filename is not None try: res = os.stat(filename).st_mtime return interp.space.wrap(int(res)) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getMTime(): " "stat failed for %s" % this.file_name)]))
def get_size(interp, this): filename = this.file_name if not filename: return interp.space.w_False try: res = os.stat(filename).st_size return interp.space.wrap(res) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getSize(): stat failed for %s" % filename)]))
def sfo_fgets(interp, this, args_w=[]): if len(args_w) != 0: interp.space.ec.warn("SplFileObject::fgets() expects exactly 0 " "parameters, %d given" % len(args_w)) return interp.space.w_Null try: return _fgets(interp, this) except IOError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::fgets(): File cannot be read")]))
def construct(interp, this, w_class, property_name): space = interp.space if space.is_str(w_class): class_name = space.str_w(w_class) klass = interp.lookup_class_or_intf(class_name) if klass is None: msg = "Class %s does not exist" % class_name raise PHPException(k_ReflectionException.call_args( interp, [space.wrap(msg)])) elif isinstance(w_class, W_InstanceObject): klass = w_class.klass class_name = klass.name else: msg = ("The parameter class is expected to be either a string " "or an object") raise PHPException(k_ReflectionException.call_args( interp, [space.wrap(msg)])) this.class_name = class_name this.name = property_name this.ref_klass = klass this.flags = 0 try: this.ref_prop = klass.properties[property_name] if this.ref_prop.is_static(): this.flags |= IS_STATIC if this.ref_prop.is_public(): this.flags |= IS_PUBLIC elif this.ref_prop.is_private(): this.flags |= IS_PRIVATE elif this.ref_prop.is_protected(): this.flags |= IS_PROTECTED except KeyError: if (isinstance(w_class, W_InstanceObject) and w_class.map.lookup(property_name) is not None): this.ref_prop = None this.flags = consts.ACC_IMPLICIT_PUBLIC return msg = "Property %s::$%s does not exist" % (class_name, property_name) raise PHPException(k_ReflectionException.call_args( interp, [interp.space.wrap(msg)]))
def initialize_timezone(interp, func_name, this, name, warning=False): this.timelib_timezone = timelib.timelib_parse_tzfile( rffi.str2charp(name), timelib.timelib_builtin_db()) if this.timelib_timezone == lltype.nullptr(timelib.timelib_tzinfo.TO): message = "%s(): Unknown or bad timezone (%s)" % (func_name, name) if warning: interp.space.ec.warn(message) else: raise PHPException( k_Exception.call_args(interp, [interp.space.wrap(message)])) return False return True
def get_value(interp, this, w_obj=None): property = this.ref_prop if property is None: return w_obj.getattr(interp, this.name, w_obj.getclass(), give_notice=False) if not property.is_public(): msg = "Cannot access non-public member %s::%s" % (this.class_name, this.name) raise PHPException(k_ReflectionException.call_args( interp, [interp.space.wrap(msg)])) if not property.is_static(): w_value = w_obj.getattr(interp, this.name, w_obj.getclass(), give_notice=False) else: w_value = property.getvalue(interp.space).deref() return w_value
def create_from_format(interp, format_string, time_string, w_datetimezone=None): func_name = "DateTime::createFromFormat" w_date = W_DateTime(interp._class_get('DateTime'), []) error = common.initialize_date_from_format(interp, func_name, w_date, format_string, time_string, w_datetimezone) if error: raise PHPException( interp._class_get('Exception').call_args( interp, [interp.space.wrap("%s(): %s" % (func_name, error))])) return w_date
def _create_iter(self, space, contextclass, byref): klass = self.getclass() if klass.is_iterator: return InstanceIterator(space, self) elif klass.is_iterable: interp = space.ec.interpreter w_iterator = interp.getmeth(self, 'getIterator').call_args(interp, []) if not (isinstance(w_iterator, W_InstanceObject) and w_iterator.getclass().is_subclass_of_class_or_intf_name('Traversable')): from hippy.builtin_klass import k_Exception raise PHPException(k_Exception.call_args(interp, [space.wrap( "Objects returned by %s::getIterator() must be " "traversable or implement interface Iterator" % klass.name)])) return w_iterator.create_iter(space) else: return self._create_fixed_iter(space, contextclass, byref)
def set_value(interp, this, w_arg_1, w_arg_2=None): if not this.ref_prop.is_public(): msg = "Cannot access non-public member %s::%s" % (this.class_name, this.name) raise PHPException(k_ReflectionException.call_args( interp, [interp.space.wrap(msg)])) if not this.ref_prop.is_static(): w_obj = w_arg_1 w_value = w_arg_2 w_obj.setattr(interp, this.name, w_value, None) else: if w_arg_2 is None: w_value = w_arg_1 else: w_value = w_arg_2 this.ref_prop.r_value.store(w_value)
def handle_as_exception(interp, message): from hippy.builtin_klass import k_Exception raise PHPException(k_Exception.call_args( interp, [interp.space.wrap(message)]))