[pypy-svn] r15368 - in pypy/dist/pypy/module/_codecs: . test

nik at codespeak.net nik at codespeak.net
Fri Jul 29 19:42:29 CEST 2005


Author: nik
Date: Fri Jul 29 19:42:27 2005
New Revision: 15368

Added:
   pypy/dist/pypy/module/_codecs/test/
   pypy/dist/pypy/module/_codecs/test/__init__.py
   pypy/dist/pypy/module/_codecs/test/autopath.py
   pypy/dist/pypy/module/_codecs/test/test_codecs.py
Modified:
   pypy/dist/pypy/module/_codecs/app_codecs.py
Log:
fixed unicode_internal encoding to behave more like CPython. needed for
array.


Modified: pypy/dist/pypy/module/_codecs/app_codecs.py
==============================================================================
--- pypy/dist/pypy/module/_codecs/app_codecs.py	(original)
+++ pypy/dist/pypy/module/_codecs/app_codecs.py	Fri Jul 29 19:42:27 2005
@@ -220,7 +220,10 @@
     res = ''.join(res)
     return res, len(res)
 
-unicode_bytes = (len(hex(sys.maxunicode))-1)/2
+if sys.maxunicode == 65535:
+    unicode_bytes = 2
+else:
+    unicode_bytes = 4
 
 def unicode_internal_encode( obj,errors='strict'):
     """None
@@ -229,9 +232,13 @@
         p = []
         t = [ord(x) for x in obj]
         for i in t:
+            bytes = []
             for j in xrange(unicode_bytes):
-                p += chr(i%256)
+                bytes += chr(i%256)
                 i >>= 8
+            if sys.byteorder == "big":
+                bytes.reverse()
+            p += bytes
         res = ''.join(p)
         return res, len(res)
     else:
@@ -246,10 +253,20 @@
     else:
         p=[]
         i=0
+        if sys.byteorder == "big":
+            start = unicode_bytes - 1
+            stop = -1
+            step = -1
+        else:
+            start = 0
+            stop = unicode_bytes
+            step = 1
         while i < len(unistr)-unicode_bytes+1:
             t = 0
-            for j in range(unicode_bytes):
-                t += ord(unistr[i+j])<<(j*8)
+            h = 0
+            for j in range(start, stop, step):
+                t += ord(unistr[i+j])<<(h*8)
+                h += 1
             i += unicode_bytes
             p += unichr(t)
         res = u''.join(p)

Added: pypy/dist/pypy/module/_codecs/test/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_codecs/test/__init__.py	Fri Jul 29 19:42:27 2005
@@ -0,0 +1 @@
+# empty
\ No newline at end of file

Added: pypy/dist/pypy/module/_codecs/test/autopath.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_codecs/test/autopath.py	Fri Jul 29 19:42:27 2005
@@ -0,0 +1,120 @@
+"""
+self cloning, automatic path configuration 
+
+copy this into any subdirectory of pypy from which scripts need 
+to be run, typically all of the test subdirs. 
+The idea is that any such script simply issues
+
+    import autopath
+
+and this will make sure that the parent directory containing "pypy"
+is in sys.path. 
+
+If you modify the master "autopath.py" version (in pypy/tool/autopath.py) 
+you can directly run it which will copy itself on all autopath.py files
+it finds under the pypy root directory. 
+
+This module always provides these attributes:
+
+    pypydir    pypy root directory path 
+    this_dir   directory where this autopath.py resides 
+
+"""
+
+
+def __dirinfo(part):
+    """ return (partdir, this_dir) and insert parent of partdir
+    into sys.path.  If the parent directories don't have the part
+    an EnvironmentError is raised."""
+
+    import sys, os
+    try:
+        head = this_dir = os.path.realpath(os.path.dirname(__file__))
+    except NameError:
+        head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+
+    while head:
+        partdir = head
+        head, tail = os.path.split(head)
+        if tail == part:
+            break
+    else:
+        raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
+    
+    checkpaths = sys.path[:]
+    pypy_root = os.path.join(head, '')
+    
+    while checkpaths:
+        orig = checkpaths.pop()
+        fullorig = os.path.join(os.path.realpath(orig), '')
+        if fullorig.startswith(pypy_root):
+            if os.path.exists(os.path.join(fullorig, '__init__.py')):
+                sys.path.remove(orig)
+    try:
+        sys.path.remove(head)
+    except ValueError:
+        pass
+    sys.path.insert(0, head)
+
+    munged = {}
+    for name, mod in sys.modules.items():
+        fn = getattr(mod, '__file__', None)
+        if '.' in name or not isinstance(fn, str):
+            continue
+        newname = os.path.splitext(os.path.basename(fn))[0]
+        if not newname.startswith(part + '.'):
+            continue
+        path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
+        if path.startswith(pypy_root) and newname != part:
+            modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
+            if newname != '__init__':
+                modpaths.append(newname)
+            modpath = '.'.join(modpaths)
+            if modpath not in sys.modules:
+                munged[modpath] = mod
+
+    for name, mod in munged.iteritems():
+        if name not in sys.modules:
+            sys.modules[name] = mod
+        if '.' in name:
+            prename = name[:name.rfind('.')]
+            postname = name[len(prename)+1:]
+            if prename not in sys.modules:
+                __import__(prename)
+                if not hasattr(sys.modules[prename], postname):
+                    setattr(sys.modules[prename], postname, mod)
+
+    return partdir, this_dir
+
+def __clone():
+    """ clone master version of autopath.py into all subdirs """
+    from os.path import join, walk
+    if not this_dir.endswith(join('pypy','tool')):
+        raise EnvironmentError("can only clone master version "
+                               "'%s'" % join(pypydir, 'tool',_myname))
+
+
+    def sync_walker(arg, dirname, fnames):
+        if _myname in fnames:
+            fn = join(dirname, _myname)
+            f = open(fn, 'rwb+')
+            try:
+                if f.read() == arg:
+                    print "checkok", fn
+                else:
+                    print "syncing", fn
+                    f = open(fn, 'w')
+                    f.write(arg)
+            finally:
+                f.close()
+    s = open(join(pypydir, 'tool', _myname), 'rb').read()
+    walk(pypydir, sync_walker, s)
+
+_myname = 'autopath.py'
+
+# set guaranteed attributes
+
+pypydir, this_dir = __dirinfo('pypy')
+
+if __name__ == '__main__':
+    __clone()

Added: pypy/dist/pypy/module/_codecs/test/test_codecs.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_codecs/test/test_codecs.py	Fri Jul 29 19:42:27 2005
@@ -0,0 +1,37 @@
+import autopath
+
+class AppTestCodecs:
+
+    def test_unicode_internal_encode(self):
+        import sys
+        enc = u"a".encode("unicode_internal")
+        if sys.maxunicode == 65535: # UCS2 build
+            if sys.byteorder == "big":
+                assert enc == "\x00a"
+            else:
+                assert enc == "a\x00"
+        else: # UCS4 build
+            enc2 = u"\U00010098".encode("unicode_internal")
+            if sys.byteorder == "big":
+                assert enc == "\x00\x00\x00a"
+                assert enc2 == "\x00\x01\x00\x98"
+            else:
+                assert enc == "a\x00\x00\x00"
+                assert enc2 == "\x98\x00\x01\x00"
+
+    def test_unicode_internal_decode(self):
+        import sys
+        if sys.maxunicode == 65535: # UCS2 build
+            if sys.byteorder == "big":
+                bytes = "\x00a"
+            else:
+                bytes = "a\x00"
+        else: # UCS4 build
+            if sys.byteorder == "big":
+                bytes = "\x00\x00\x00a"
+                bytes2 = "\x00\x01\x00\x98"
+            else:
+                bytes = "a\x00\x00\x00"
+                bytes2 = "\x98\x00\x01\x00"
+            assert bytes2.decode("unicode_internal") == u"\U00010098"
+        assert bytes.decode("unicode_internal") == u"a"



More information about the Pypy-commit mailing list