helly           Thu May 18 21:41:37 2006 UTC

  Added files:                 
    /php-src/ext/spl/tests      iterator_036.phpt iterator_037.phpt 

  Modified files:              
    /php-src/ext/spl    spl_iterators.c spl_iterators.h 
  Log:
  - Fixed CachingIterator issues with string conversion/__toString()
    Partly by adding new mode TOSTRING_USE_INNER
  
  
http://cvs.php.net/viewcvs.cgi/php-src/ext/spl/spl_iterators.c?r1=1.127&r2=1.128&diff_format=u
Index: php-src/ext/spl/spl_iterators.c
diff -u php-src/ext/spl/spl_iterators.c:1.127 
php-src/ext/spl/spl_iterators.c:1.128
--- php-src/ext/spl/spl_iterators.c:1.127       Tue May 16 08:19:56 2006
+++ php-src/ext/spl/spl_iterators.c     Thu May 18 21:41:37 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_iterators.c,v 1.127 2006/05/16 08:19:56 helly Exp $ */
+/* $Id: spl_iterators.c,v 1.128 2006/05/18 21:41:37 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -844,6 +844,18 @@
 
 static inline int spl_dual_it_fetch(spl_dual_it_object *intern, int check_more 
TSRMLS_DC);
 
+static inline int spl_cit_check_flags(int flags)
+{
+       int cnt = 0;
+
+       cnt += (flags & CIT_CALL_TOSTRING) ? 1 : 0;
+       cnt += (flags & CIT_TOSTRING_USE_KEY) ? 1 : 0;
+       cnt += (flags & CIT_TOSTRING_USE_CURRENT) ? 1 : 0;
+       cnt += (flags & CIT_TOSTRING_USE_INNER) ? 1 : 0;
+       
+       return cnt <= 1 ? SUCCESS : FAILURE;
+}
+
 static spl_dual_it_object* spl_dual_it_construct(INTERNAL_FUNCTION_PARAMETERS, 
zend_class_entry *ce_base, zend_class_entry *ce_inner, dual_it_type dit_type)
 {
        zval                 *zobject, *retval;
@@ -888,10 +900,9 @@
                                php_set_error_handling(EH_NORMAL, NULL 
TSRMLS_CC);
                                return NULL;
                        }
-                       if (((flags & CIT_CALL_TOSTRING) && (flags & 
(CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT)))
-                       || ((flags & 
(CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT)) == 
(CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT))) {
+                       if (spl_cit_check_flags(flags) != SUCCESS) {
                                php_set_error_handling(EH_NORMAL, NULL 
TSRMLS_CC);
-                               
zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only 
one of CATCH_GET_CHILD, CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT", 
0 TSRMLS_CC);
+                               
zend_throw_exception(spl_ce_InvalidArgumentException, "Flags must contain only 
one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, 
TOSTRING_USE_CURRENT", 0 TSRMLS_CC);
                                return NULL;
                        }
                        intern->u.caching.flags |= flags & CIT_PUBLIC;
@@ -1672,8 +1683,9 @@
                                        }
                                        if (intern->u.caching.flags & 
CIT_CATCH_GET_CHILD) {
                                                zend_clear_exception(TSRMLS_C);
+                                       } else {
+                                               return;
                                        }
-                                       return;
                                } else {
                                        INIT_PZVAL(&zflags);
                                        ZVAL_LONG(&zflags, 
intern->u.caching.flags & CIT_PUBLIC);
@@ -1681,13 +1693,17 @@
                                        zval_ptr_dtor(&zchildren);
                                }
                        }
-                       zval_ptr_dtor(&retval);         
+                       zval_ptr_dtor(&retval);
                }
-               if (intern->u.caching.flags & CIT_CALL_TOSTRING) {
+               if (intern->u.caching.flags & 
(CIT_TOSTRING_USE_INNER|CIT_CALL_TOSTRING)) {
                        int  use_copy;
                        zval expr_copy;
                        ALLOC_ZVAL(intern->u.caching.zstr);
-                       *intern->u.caching.zstr = *intern->current.data;
+                       if (intern->u.caching.flags & CIT_TOSTRING_USE_INNER) {
+                               *intern->u.caching.zstr = 
*intern->inner.zobject;
+                       } else {
+                               *intern->u.caching.zstr = *intern->current.data;
+                       }
                        if (UG(unicode)) {
                                zend_make_unicode_zval(intern->u.caching.zstr, 
&expr_copy, &use_copy);
                        } else {
@@ -1775,7 +1791,7 @@
 
        intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() 
TSRMLS_CC);
 
-       if (!(intern->u.caching.flags & 
(CIT_CALL_TOSTRING|CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT)))     {
+       if (!(intern->u.caching.flags & 
(CIT_CALL_TOSTRING|CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT|CIT_TOSTRING_USE_INNER)))
      {
                zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 
TSRMLS_CC, "%v does not fetch string value (see CachingIterator::__construct)", 
Z_OBJCE_P(getThis())->name);
        }
        if (intern->u.caching.flags & CIT_TOSTRING_USE_KEY) {
@@ -1785,19 +1801,18 @@
                        RETURN_UNICODEL(intern->current.str_key.u, 
intern->current.str_key_len, 1);
                } else {
                        RETVAL_LONG(intern->current.int_key);
-                       convert_to_string(return_value);
+                       convert_to_text(return_value);
                        return;
                }
        } else if (intern->u.caching.flags & CIT_TOSTRING_USE_CURRENT) {
-               RETVAL_ZVAL(intern->current.data, 1, 0);
-               
-               return;
-       }
-       if (intern->u.caching.zstr) {
-               *return_value = *intern->u.caching.zstr;
+               *return_value = *intern->current.data;
                zval_copy_ctor(return_value);
                convert_to_text(return_value);
                INIT_PZVAL(return_value);
+               return;
+       }
+       if (intern->u.caching.zstr) {
+               RETURN_ZVAL(intern->u.caching.zstr, 1, 0);
        } else {
                RETURN_NULL();
        }
@@ -1928,15 +1943,18 @@
                return;
        }
 
-       if (((flags & CIT_CALL_TOSTRING) && (flags & 
(CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT)))
-       || ((flags & (CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT)) == 
(CIT_TOSTRING_USE_KEY|CIT_TOSTRING_USE_CURRENT))) {
-               zend_throw_exception(spl_ce_InvalidArgumentException , "Flags 
must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, 
CIT_TOSTRING_USE_CURRENT", 0 TSRMLS_CC);
+       if (spl_cit_check_flags(flags) != SUCCESS) {
+               zend_throw_exception(spl_ce_InvalidArgumentException , "Flags 
must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, TOSTRING_USE_CURRENT, 
TOSTRING_USE_INNER", 0 TSRMLS_CC);
                return;
        }
-       if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & 
~CIT_CALL_TOSTRING) == 0) {
+       if ((intern->u.caching.flags & CIT_CALL_TOSTRING) != 0 && (flags & 
CIT_CALL_TOSTRING) == 0) {
                zend_throw_exception(spl_ce_InvalidArgumentException, 
"Unsetting flag CALL_TO_STRING is not possible", 0 TSRMLS_CC);
                return;
        }
+       if ((intern->u.caching.flags & CIT_TOSTRING_USE_INNER) != 0 && (flags & 
CIT_TOSTRING_USE_INNER) == 0) {
+               zend_throw_exception(spl_ce_InvalidArgumentException, 
"Unsetting flag TOSTRING_USE_INNER is not possible", 0 TSRMLS_CC);
+               return;
+       }
        if ((flags && CIT_FULL_CACHE) != 0 && (intern->u.caching.flags & 
CIT_FULL_CACHE) == 0) {
                /* clear on (re)enable */
                zend_hash_clean(HASH_OF(intern->u.caching.zcache));
@@ -2544,6 +2562,7 @@
        REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "CATCH_GET_CHILD",      
CIT_CATCH_GET_CHILD); 
        REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "TOSTRING_USE_KEY",     
CIT_TOSTRING_USE_KEY);
        REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "TOSTRING_USE_CURRENT", 
CIT_TOSTRING_USE_CURRENT);
+       REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, 
"TOSTRING_USE_INNER",CIT_TOSTRING_USE_INNER);
        REGISTER_SPL_CLASS_CONST_LONG(CachingIterator, "FULL_CACHE",           
CIT_FULL_CACHE); 
 
        REGISTER_SPL_SUB_CLASS_EX(RecursiveCachingIterator, CachingIterator, 
spl_dual_it_new, spl_funcs_RecursiveCachingIterator);
http://cvs.php.net/viewcvs.cgi/php-src/ext/spl/spl_iterators.h?r1=1.32&r2=1.33&diff_format=u
Index: php-src/ext/spl/spl_iterators.h
diff -u php-src/ext/spl/spl_iterators.h:1.32 
php-src/ext/spl/spl_iterators.h:1.33
--- php-src/ext/spl/spl_iterators.h:1.32        Wed May 10 16:51:18 2006
+++ php-src/ext/spl/spl_iterators.h     Thu May 18 21:41:37 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_iterators.h,v 1.32 2006/05/10 16:51:18 andrei Exp $ */
+/* $Id: spl_iterators.h,v 1.33 2006/05/18 21:41:37 helly Exp $ */
 
 #ifndef SPL_ITERATORS_H
 #define SPL_ITERATORS_H
@@ -78,9 +78,10 @@
 enum {
        /* public */
        CIT_CALL_TOSTRING        = 0x00000001,
-       CIT_CATCH_GET_CHILD      = 0x00000002,
-       CIT_TOSTRING_USE_KEY     = 0x00000010,
-       CIT_TOSTRING_USE_CURRENT = 0x00000020,
+       CIT_TOSTRING_USE_KEY     = 0x00000002,
+       CIT_TOSTRING_USE_CURRENT = 0x00000004,
+       CIT_TOSTRING_USE_INNER   = 0x00000008,
+       CIT_CATCH_GET_CHILD      = 0x00000010,
        CIT_FULL_CACHE           = 0x00000100,
        CIT_PUBLIC               = 0x0000FFFF,
        /* private */

http://cvs.php.net/viewcvs.cgi/php-src/ext/spl/tests/iterator_036.phpt?view=markup&rev=1.1
Index: php-src/ext/spl/tests/iterator_036.phpt
+++ php-src/ext/spl/tests/iterator_036.phpt
--TEST--
SPL: CachingIterator and __toString and flags = 0
--SKIPIF--
<?php if (!extension_loaded("spl")) print "skip"; ?>
--FILE--
<?php

function test($it)
{
        foreach($it as $v)
        {
                var_dump((string)$it);
        }
}

$ar = new ArrayIterator(array(1, 2, 3));

test(new CachingIterator($ar, 0));

?>
===DONE===
--EXPECTF--     

Fatal error: Method CachingIterator::__toString() must not throw an exception 
in %siterator_036.php on line %d

http://cvs.php.net/viewcvs.cgi/php-src/ext/spl/tests/iterator_037.phpt?view=markup&rev=1.1
Index: php-src/ext/spl/tests/iterator_037.phpt
+++ php-src/ext/spl/tests/iterator_037.phpt
--TEST--
SPL: CachingIterator and __toString
--SKIPIF--
<?php if (!extension_loaded("spl")) print "skip"; ?>
--FILE--
<?php

function test($ar, $flags)
{
        echo "===$flags===\n";
        $it = new CachingIterator($ar, 0);
        try
        {
                $it->setFlags($flags);
        }
        catch (Exception $e)
        {
                echo 'Exception: ' . $e->getMessage() . "\n";
                var_dump($it->getFlags());
                return;
        }
        var_dump($it->getFlags());
        try
        {
                foreach($it as $v)
                {
                        var_dump((string)$it);
                }
        }
        catch (Exception $e)
        {
                echo 'Exception: ' . $e->getMessage() . "\n";
        }
}

class MyItem
{
        function __construct($value)
        {
                $this->value = $value;
        }

        function __toString()
        {
                return (string)$this->value;
        }
}

class MyArrayIterator extends ArrayIterator
{
        function __toString()
        {
                return $this->key() . ':' . $this->current();
        }
}

$ar = new MyArrayIterator(array(1, 2, 3));

test($ar, CachingIterator::CALL_TOSTRING);
test($ar, CachingIterator::TOSTRING_USE_KEY);
test($ar, CachingIterator::TOSTRING_USE_CURRENT);

$ar = new MyArrayIterator(array(new MyItem(1), new MyItem(2), new MyItem(3)));

test($ar, CachingIterator::TOSTRING_USE_INNER);
test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_KEY);
test($ar, CachingIterator::CALL_TOSTRING | 
CachingIterator::TOSTRING_USE_CURRENT);
test($ar, CachingIterator::CALL_TOSTRING | CachingIterator::TOSTRING_USE_INNER);
test($ar, CachingIterator::TOSTRING_USE_KEY | 
CachingIterator::TOSTRING_USE_CURRENT);
test($ar, CachingIterator::TOSTRING_USE_KEY | 
CachingIterator::TOSTRING_USE_INNER);

echo "===X===\n";
try
{
        $it = new CachingIterator($ar, CachingIterator::CALL_TOSTRING);
        $it->setFlags(0);
}
catch (Exception $e)
{
        echo 'Exception: ' . $e->getMessage() . "\n";
}
try
{
        $it = new CachingIterator($ar, CachingIterator::TOSTRING_USE_INNER);
        $it->setFlags(0);
}
catch (Exception $e)
{
        echo 'Exception: ' . $e->getMessage() . "\n";
}

?>
===DONE===
--EXPECTF--     
===1===
int(1)
string(1) "1"
string(1) "2"
string(1) "3"
===2===
int(2)
string(1) "0"
string(1) "1"
string(1) "2"
===4===
int(4)
string(1) "1"
string(1) "2"
string(1) "3"
===8===
int(8)
string(3) "0:1"
string(3) "1:2"
string(3) "2:3"
===3===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===5===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===9===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===6===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===10===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===X===
Exception: Unsetting flag CALL_TO_STRING is not possible
Exception: Unsetting flag TOSTRING_USE_INNER is not possible
===DONE===
--UEXPECTF--    
===1===
int(1)
unicode(1) "1"
unicode(1) "2"
unicode(1) "3"
===2===
int(2)
unicode(1) "0"
unicode(1) "1"
unicode(1) "2"
===4===
int(4)
unicode(1) "1"
unicode(1) "2"
unicode(1) "3"
===8===
int(8)
unicode(3) "0:1"
unicode(3) "1:2"
unicode(3) "2:3"
===3===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===5===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===9===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===6===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===10===
Exception: Flags must contain only one of CALL_TOSTRING, TOSTRING_USE_KEY, 
TOSTRING_USE_CURRENT, TOSTRING_USE_INNER
int(0)
===X===
Exception: Unsetting flag CALL_TO_STRING is not possible
Exception: Unsetting flag TOSTRING_USE_INNER is not possible
===DONE===

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to