示例#1
0
文件: test_uu.py 项目: BillyboyD/main
    def test_encode(self):
        fin = fout = None
        try:
            test_support.unlink(self.tmpin)
            fin = open(self.tmpin, 'wb')
            fin.write(plaintext)
            fin.close()

            fin = open(self.tmpin, 'rb')
            fout = open(self.tmpout, 'w')
            uu.encode(fin, fout, self.tmpin, mode=0644)
            fin.close()
            fout.close()

            fout = open(self.tmpout, 'r')
            s = fout.read()
            fout.close()
            self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))

            # in_file and out_file as filenames
            uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0644)
            test_support.force_gc_collect()
            fout = open(self.tmpout, 'r')
            s = fout.read()
            fout.close()
            self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))

        finally:
            self._kill(fin)
            self._kill(fout)
示例#2
0
 def tearDown(self):
     test_support.force_gc_collect("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793")
     map(os.unlink, self._msgfiles)
     os.rmdir(os.path.join(self._dir, "cur"))
     os.rmdir(os.path.join(self._dir, "tmp"))
     os.rmdir(os.path.join(self._dir, "new"))
     os.rmdir(self._dir)
示例#3
0
    def test_getweakrefcount(self):
        o = C()
        
        def runTest():
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "got wrong number of weak reference objects")

            proxy1 = weakref.proxy(o)
            proxy2 = weakref.proxy(o, self.callback)
            self.assertTrue(weakref.getweakrefcount(o) == 4,
                         "got wrong number of weak reference objects")

            del ref1, ref2, proxy1, proxy2

        runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefcount(o) == 0,
                     "weak reference objects not unlinked from"
                     " referent when discarded.")

        # assumes ints do not support weakrefs
        self.assertTrue(weakref.getweakrefcount(1) == 0,
                     "got wrong number of weak reference objects for int")
示例#4
0
    def test_ref_reuse(self):
        def runTest():
            o = C()
            ref1 = weakref.ref(o)
            # create a proxy to make sure that there's an intervening creation
            # between these two; it should make no difference
            proxy = weakref.proxy(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")

            o = C()
            proxy = weakref.proxy(o)
            ref1 = weakref.ref(o)
            ref2 = weakref.ref(o)
            self.assertTrue(ref1 is ref2,
                         "reference object w/out callback should be re-used")
            self.assertTrue(weakref.getweakrefcount(o) == 2,
                         "wrong weak ref count for object")
            del proxy
            return o, ref1

        o, ref1 = runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefcount(o) == 1,
                     "wrong weak ref count for object after deleting proxy")
示例#5
0
    def test_proxy_ref(self):
        def inner1():
            o = C()
            o.bar = 1
            ref1 = weakref.proxy(o, self.callback)
            ref2 = weakref.proxy(o, self.callback)

            del o

            return ref1, ref2

        ref1, ref2 = inner1()
        test_support.force_gc_collect()
        
        def check(proxy):
            proxy.bar

        self.assertRaises(weakref.ReferenceError, check, ref1)
        self.assertRaises(weakref.ReferenceError, check, ref2)
        
        #http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=325860
        def inner2():
            o = C()
            ref = weakref.proxy(o)
            del o
            
            return ref

        ref = inner2()

        test_support.force_gc_collect()
        self.assertRaises(weakref.ReferenceError, bool, ref)
        self.assertTrue(self.cbcalled == 2)
示例#6
0
 def tearDown(self):
     force_gc_collect("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793")
     for file in os.listdir(self.package_dir):
         os.remove(os.path.join(self.package_dir, file))
     os.rmdir(self.package_dir)
     os.rmdir(self.test_dir)
     self.assertNotEqual(sys.path.count(self.test_dir), 0)
     sys.path.remove(self.test_dir)
     self.remove_modules()
示例#7
0
 def check_basic_callback(self, factory):
     self.cbcalled = 0
     o = factory()
     ref = weakref.ref(o, self.callback)
     del o
     test_support.force_gc_collect()
     self.assertTrue(self.cbcalled == 1,
                  "callback did not properly set 'cbcalled'")
     self.assertTrue(ref() is None,
                  "ref2 should be dead after deleting object reference")
示例#8
0
 def check_basic_callback(self, factory):
     self.cbcalled = 0
     o = factory()
     ref = weakref.ref(o, self.callback)
     del o
     test_support.force_gc_collect()
     self.assertTrue(self.cbcalled == 1,
                  "callback did not properly set 'cbcalled'")
     self.assertTrue(ref() is None,
                  "ref2 should be dead after deleting object reference")
示例#9
0
 def test_proxy_deletion(self):
     # Test clearing of SF bug #762891
     class Foo:
         result = None
         def __delitem__(self, accessor):
             self.result = accessor
     g = Foo()
     f = weakref.proxy(g)
     del f[0]
     test_support.force_gc_collect()
     self.assertEqual(f.result, 0)
示例#10
0
 def test_proxy_deletion(self):
     # Test clearing of SF bug #762891
     class Foo:
         result = None
         def __delitem__(self, accessor):
             self.result = accessor
     g = Foo()
     f = weakref.proxy(g)
     del f[0]
     test_support.force_gc_collect()
     self.assertEqual(f.result, 0)
示例#11
0
 def test_multiple_callbacks(self):
     o = C()
     ref1 = weakref.ref(o, self.callback)
     ref2 = weakref.ref(o, self.callback)
     del o
     test_support.force_gc_collect()
     self.assertTrue(ref1() is None,
                  "expected reference to be invalidated")
     self.assertTrue(ref2() is None,
                  "expected reference to be invalidated")
     self.assertTrue(self.cbcalled == 2,
                  "callback not called the right number of times")
示例#12
0
 def test_multiple_callbacks(self):
     o = C()
     ref1 = weakref.ref(o, self.callback)
     ref2 = weakref.ref(o, self.callback)
     del o
     test_support.force_gc_collect()
     self.assertTrue(ref1() is None,
                  "expected reference to be invalidated")
     self.assertTrue(ref2() is None,
                  "expected reference to be invalidated")
     self.assertTrue(self.cbcalled == 2,
                  "callback not called the right number of times")
示例#13
0
文件: test_uu.py 项目: BillyboyD/main
 def _kill(self, f):
     # close and remove file
     test_support.force_gc_collect()
     try:
         f.close()
     except (SystemExit, KeyboardInterrupt):
         raise
     except:
         pass
     try:
         os.unlink(f.name)
     except (SystemExit, KeyboardInterrupt):
         raise
     except:
         pass
示例#14
0
        def runTest():
            def runInnerTest():
                o = C()
                ref1 = weakref.ref(o, self.callback)
                ref2 = weakref.ref(o, self.callback)
                del ref2
                return o, ref1

            o, ref1 = runInnerTest()
            test_support.force_gc_collect()
            self.assertTrue(weakref.getweakrefs(o) == [ref1],
                         "list of refs does not match")

            del ref1
            return o
示例#15
0
        def runTest():
            def runInnerTest():
                o = C()
                ref1 = weakref.ref(o, self.callback)
                ref2 = weakref.ref(o, self.callback)
                del ref2
                return o, ref1

            o, ref1 = runInnerTest()
            test_support.force_gc_collect()
            self.assertTrue(weakref.getweakrefs(o) == [ref1],
                         "list of refs does not match")

            del ref1
            return o
示例#16
0
 def _kill(self, f):
     # close and remove file
     test_support.force_gc_collect()
     try:
         f.close()
     except (SystemExit, KeyboardInterrupt):
         raise
     except:
         pass
     try:
         os.unlink(f.name)
     except (SystemExit, KeyboardInterrupt):
         raise
     except:
         pass
示例#17
0
    def test_weak_keys(self):
        #
        #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
        #  len(d), in d.
        #

        def runTest():
            dict, objects = self.make_weak_keyed_dict()
            for o in objects:
                self.assertTrue(
                    weakref.getweakrefcount(o) == 1,
                    "wrong number of weak references to %r!" % o)
                self.assertTrue(o.arg is dict[o],
                                "wrong object returned by weak dict!")
            items1 = dict.items()
            items2 = dict.copy().items()
            del o
            self.assertTrue(
                set(items1) == set(items2),
                "cloning of weak-keyed dictionary did not work!")
            del items1, items2
            self.assertTrue(len(dict) == self.COUNT)
            del objects[0]

            return dict, objects

        dict, objects = runTest()
        test_support.force_gc_collect()

        self.assertTrue(
            len(dict) == (self.COUNT - 1),
            "deleting object did not cause dictionary update")
        del objects
        if not test_support.due_to_ironpython_incompatibility():
            # IronPython currently fails because of some garbage alive on the stack
            self.assertTrue(
                len(dict) == 0,
                "deleting the keys did not clear the dictionary")
        o = Object(42)
        dict[o] = "What is the meaning of the universe?"
        self.assertIn(o, dict)
        self.assertNotIn(34, dict)
示例#18
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertTrue(
             weakref.getweakrefcount(o) == 1,
             "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                         "wrong object returned by weak dict!")
     if test_support.due_to_ironpython_incompatibility():
         del o
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                     "cloning of weak-valued dictionary did not work!")
     del items1, items2
     test_support.force_gc_collect()
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     if test_support.due_to_ironpython_incompatibility():
         gc.collect()
         del objects
     else:
         self.assertTrue(
             len(dict) == (self.COUNT - 1),
             "deleting object did not cause dictionary update")
         del objects, o
     test_support.force_gc_collect()
     if not test_support.due_to_ironpython_incompatibility():
         # we currently fail because of some garbage alive on the stack
         self.assertTrue(
             len(dict) == 0,
             "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
         self.assertRaises(KeyError, dict.__getitem__, 1)
         dict[2] = C()
         self.assertRaises(KeyError, dict.__getitem__, 2)
示例#19
0
 def test_weak_values(self):
     #
     #  This exercises d.copy(), d.items(), d[], del d[], len(d).
     #
     dict, objects = self.make_weak_valued_dict()
     for o in objects:
         self.assertTrue(weakref.getweakrefcount(o) == 1,
                      "wrong number of weak references to %r!" % o)
         self.assertTrue(o is dict[o.arg],
                      "wrong object returned by weak dict!")
     if test_support.due_to_ironpython_incompatibility():
         del o
     items1 = dict.items()
     items2 = dict.copy().items()
     items1.sort()
     items2.sort()
     self.assertTrue(items1 == items2,
                  "cloning of weak-valued dictionary did not work!")
     del items1, items2
     test_support.force_gc_collect()
     self.assertTrue(len(dict) == self.COUNT)
     del objects[0]
     if test_support.due_to_ironpython_incompatibility():
         gc.collect()
         del objects
     else:
         self.assertTrue(len(dict) == (self.COUNT - 1),
                     "deleting object did not cause dictionary update")
         del objects, o
     test_support.force_gc_collect()
     if not test_support.due_to_ironpython_incompatibility():
         # we currently fail because of some garbage alive on the stack
         self.assertTrue(len(dict) == 0,
                      "deleting the values did not clear the dictionary")
         # regression on SF bug #447152:
         dict = weakref.WeakValueDictionary()
         self.assertRaises(KeyError, dict.__getitem__, 1)
         dict[2] = C()
         self.assertRaises(KeyError, dict.__getitem__, 2)
示例#20
0
    def test_getweakrefs(self):
        def runTest():
            o = C()
            ref1 = weakref.ref(o, self.callback)
            ref2 = weakref.ref(o, self.callback)
            del ref1
            return o, ref2

        o, ref2 = runTest()
        test_support.force_gc_collect()
        self.assertTrue(
            weakref.getweakrefs(o) == [ref2], "list of refs does not match")

        def runTest():
            def runInnerTest():
                o = C()
                ref1 = weakref.ref(o, self.callback)
                ref2 = weakref.ref(o, self.callback)
                del ref2
                return o, ref1

            o, ref1 = runInnerTest()
            test_support.force_gc_collect()
            self.assertTrue(
                weakref.getweakrefs(o) == [ref1],
                "list of refs does not match")

            del ref1
            return o

        o = runTest()
        test_support.force_gc_collect()
        self.assertTrue(
            weakref.getweakrefs(o) == [], "list of refs not cleared")

        # assumes ints do not support weakrefs
        self.assertTrue(
            weakref.getweakrefs(1) == [],
            "list of refs does not match for int")
示例#21
0
    def test_weak_keys(self):
        #
        #  This exercises d.copy(), d.items(), d[] = v, d[], del d[],
        #  len(d), in d.
        #
        
        def runTest():
            dict, objects = self.make_weak_keyed_dict()
            for o in objects:
                self.assertTrue(weakref.getweakrefcount(o) == 1,
                             "wrong number of weak references to %r!" % o)
                self.assertTrue(o.arg is dict[o],
                             "wrong object returned by weak dict!")
            items1 = dict.items()
            items2 = dict.copy().items()
            del o
            self.assertTrue(set(items1) == set(items2),
                         "cloning of weak-keyed dictionary did not work!")
            del items1, items2
            self.assertTrue(len(dict) == self.COUNT)
            del objects[0]
            
            return dict, objects

        dict, objects = runTest()
        test_support.force_gc_collect()

        self.assertTrue(len(dict) == (self.COUNT - 1),
                     "deleting object did not cause dictionary update")
        del objects
        if not test_support.due_to_ironpython_incompatibility():
            # IronPython currently fails because of some garbage alive on the stack
            self.assertTrue(len(dict) == 0,
                         "deleting the keys did not clear the dictionary")
        o = Object(42)
        dict[o] = "What is the meaning of the universe?"
        self.assertIn(o, dict)
        self.assertNotIn(34, dict)
示例#22
0
    def test_getweakrefs(self):
        def runTest():
            o = C()
            ref1 = weakref.ref(o, self.callback)
            ref2 = weakref.ref(o, self.callback)
            del ref1
            return o, ref2

        o, ref2 = runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefs(o) == [ref2],
                     "list of refs does not match")

        def runTest():
            def runInnerTest():
                o = C()
                ref1 = weakref.ref(o, self.callback)
                ref2 = weakref.ref(o, self.callback)
                del ref2
                return o, ref1

            o, ref1 = runInnerTest()
            test_support.force_gc_collect()
            self.assertTrue(weakref.getweakrefs(o) == [ref1],
                         "list of refs does not match")

            del ref1
            return o

        o = runTest()
        test_support.force_gc_collect()
        self.assertTrue(weakref.getweakrefs(o) == [],
                     "list of refs not cleared")

        # assumes ints do not support weakrefs
        self.assertTrue(weakref.getweakrefs(1) == [],
                     "list of refs does not match for int")
示例#23
0
    def test_sf_bug_840829(self):
        # "weakref callbacks and gc corrupt memory"
        # subtype_dealloc erroneously exposed a new-style instance
        # already in the process of getting deallocated to gc,
        # causing double-deallocation if the instance had a weakref
        # callback that triggered gc.
        # If the bug exists, there probably won't be an obvious symptom
        # in a release build.  In a debug build, a segfault will occur
        # when the second attempt to remove the instance from the "list
        # of all objects" occurs.

        import gc

        class C(object):
            pass

        c = C()
        wr = weakref.ref(c, lambda ignore: gc.collect())
        del c
        test_support.force_gc_collect()

        # There endeth the first part.  It gets worse.
        del wr
        test_support.force_gc_collect()

        c1 = C()
        c1.i = C()
        wr = weakref.ref(c1.i, lambda ignore: gc.collect())

        c2 = C()
        c2.c1 = c1
        del c1  # still alive because c2 points to it
        test_support.force_gc_collect()

        # Now when subtype_dealloc gets called on c2, it's not enough just
        # that c2 is immune from gc while the weakref callbacks associated
        # with c2 execute (there are none in this 2nd half of the test, btw).
        # subtype_dealloc goes on to call the base classes' deallocs too,
        # so any gc triggered by weakref callbacks associated with anything
        # torn down by a base class dealloc can also trigger double
        # deallocation of c2.
        del c2
        test_support.force_gc_collect()
示例#24
0
    def test_sf_bug_840829(self):
        # "weakref callbacks and gc corrupt memory"
        # subtype_dealloc erroneously exposed a new-style instance
        # already in the process of getting deallocated to gc,
        # causing double-deallocation if the instance had a weakref
        # callback that triggered gc.
        # If the bug exists, there probably won't be an obvious symptom
        # in a release build.  In a debug build, a segfault will occur
        # when the second attempt to remove the instance from the "list
        # of all objects" occurs.

        import gc

        class C(object):
            pass

        c = C()
        wr = weakref.ref(c, lambda ignore: gc.collect())
        del c
        test_support.force_gc_collect()

        # There endeth the first part.  It gets worse.
        del wr
        test_support.force_gc_collect()

        c1 = C()
        c1.i = C()
        wr = weakref.ref(c1.i, lambda ignore: gc.collect())

        c2 = C()
        c2.c1 = c1
        del c1  # still alive because c2 points to it
        test_support.force_gc_collect()

        # Now when subtype_dealloc gets called on c2, it's not enough just
        # that c2 is immune from gc while the weakref callbacks associated
        # with c2 execute (there are none in this 2nd half of the test, btw).
        # subtype_dealloc goes on to call the base classes' deallocs too,
        # so any gc triggered by weakref callbacks associated with anything
        # torn down by a base class dealloc can also trigger double
        # deallocation of c2.
        del c2
        test_support.force_gc_collect()
示例#25
0
 def check_shared_without_callback(self, makeref):
     o = Object(1)
     p1 = makeref(o, None)
     p2 = makeref(o, None)
     self.assertTrue(p1 is p2, "both callbacks were None in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o)
     p2 = makeref(o, None)
     self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o)
     p2 = makeref(o)
     self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o, None)
     p2 = makeref(o)
     self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")
示例#26
0
 def check_shared_without_callback(self, makeref):
     o = Object(1)
     p1 = makeref(o, None)
     p2 = makeref(o, None)
     self.assertTrue(p1 is p2, "both callbacks were None in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o)
     p2 = makeref(o, None)
     self.assertTrue(p1 is p2, "callbacks were NULL, None in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o)
     p2 = makeref(o)
     self.assertTrue(p1 is p2, "both callbacks were NULL in the C API")
     del p1, p2
     test_support.force_gc_collect()
     p1 = makeref(o, None)
     p2 = makeref(o)
     self.assertTrue(p1 is p2, "callbacks were None, NULL in the C API")
示例#27
0
 def tearDown(self):
     del sys.path[0]
     force_gc_collect(
         "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793"
     )
     shutil.rmtree(self.dirname)
示例#28
0
 def tearDown(self):
     force_gc_collect()
     try:
         os.unlink(TESTFN)
     except OSError:
         pass
示例#29
0
    def test_access_parameter(self):
        # Test for "access" keyword parameter
        mapsize = 10
        with open(TESTFN, "wb") as f:
            f.write("a" * mapsize)
        f = open(TESTFN, "rb")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
        self.assertEqual(m[:], 'a' * mapsize,
                         "Readonly memory map data incorrect.")

        # Ensuring that readonly mmap can't be slice assigned
        try:
            m[:] = 'b' * mapsize
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be item assigned
        try:
            m[0] = 'b'
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be write() to
        try:
            m.seek(0, 0)
            m.write('abc')
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be write_byte() to
        try:
            m.seek(0, 0)
            m.write_byte('d')
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be resized
        try:
            m.resize(2 * mapsize)
        except SystemError:  # resize is not universally supported
            pass
        except TypeError:
            pass
        else:
            self.fail("Able to resize readonly memory map")
        f.close()
        del m, f
        with open(TESTFN, "rb") as f:
            self.assertEqual(f.read(), 'a' * mapsize,
                             "Readonly memory map data file was modified")

        # Opening mmap with size too big
        f = open(TESTFN, "r+b")
        try:
            m = mmap.mmap(f.fileno(), mapsize + 1)
        except ValueError:
            # we do not expect a ValueError on Windows
            # CAUTION:  This also changes the size of the file on disk, and
            # later tests assume that the length hasn't changed.  We need to
            # repair that.
            if hasattr(sys, 'getwindowsversion'):
                self.fail("Opening mmap with size+1 should work on Windows.")
        else:
            # we expect a ValueError on Unix, but not on Windows
            if not hasattr(sys, 'getwindowsversion'):
                self.fail("Opening mmap with size+1 should raise ValueError.")
            m.close()
        f.close()
        if hasattr(sys, 'getwindowsversion'):
            # Repair damage from the resizing test.
            del m, f
            force_gc_collect()
            f = open(TESTFN, 'r+b')
            f.truncate(mapsize)
            f.close()

        # Opening mmap with access=ACCESS_WRITE
        f = open(TESTFN, "r+b")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
        # Modifying write-through memory map
        m[:] = 'c' * mapsize
        self.assertEqual(
            m[:], 'c' * mapsize,
            "Write-through memory map memory not updated properly.")
        m.flush()
        m.close()
        f.close()
        f = open(TESTFN, 'rb')
        stuff = f.read()
        f.close()
        self.assertEqual(
            stuff, 'c' * mapsize,
            "Write-through memory map data file not updated properly.")

        # Opening mmap with access=ACCESS_COPY
        f = open(TESTFN, "r+b")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
        # Modifying copy-on-write memory map
        m[:] = 'd' * mapsize
        self.assertEqual(
            m[:], 'd' * mapsize,
            "Copy-on-write memory map data not written correctly.")
        m.flush()
        with open(TESTFN, "rb") as f:
            self.assertEqual(
                f.read(), 'c' * mapsize,
                "Copy-on-write test data file should not be modified.")
        # Ensuring copy-on-write maps cannot be resized
        self.assertRaises(TypeError, m.resize, 2 * mapsize)
        f.close()
        del m, f

        # Ensuring invalid access parameter raises exception
        f = open(TESTFN, "r+b")
        self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
        f.close()

        if os.name == "posix":
            # Try incompatible flags, prot and access parameters.
            f = open(TESTFN, "r+b")
            self.assertRaises(ValueError,
                              mmap.mmap,
                              f.fileno(),
                              mapsize,
                              flags=mmap.MAP_PRIVATE,
                              prot=mmap.PROT_READ,
                              access=mmap.ACCESS_WRITE)
            f.close()
示例#30
0
 def tearDown(self):
     force_gc_collect()
     try:
         os.unlink(TESTFN)
     except OSError:
         pass
示例#31
0
    def test_access_parameter(self):
        # Test for "access" keyword parameter
        mapsize = 10
        with open(TESTFN, "wb") as f:
            f.write("a"*mapsize)
        f = open(TESTFN, "rb")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
        self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")

        # Ensuring that readonly mmap can't be slice assigned
        try:
            m[:] = 'b'*mapsize
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be item assigned
        try:
            m[0] = 'b'
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be write() to
        try:
            m.seek(0,0)
            m.write('abc')
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be write_byte() to
        try:
            m.seek(0,0)
            m.write_byte('d')
        except TypeError:
            pass
        else:
            self.fail("Able to write to readonly memory map")

        # Ensuring that readonly mmap can't be resized
        try:
            m.resize(2*mapsize)
        except SystemError:   # resize is not universally supported
            pass
        except TypeError:
            pass
        else:
            self.fail("Able to resize readonly memory map")
        f.close()
        del m, f
        with open(TESTFN, "rb") as f:
            self.assertEqual(f.read(), 'a'*mapsize,
               "Readonly memory map data file was modified")

        # Opening mmap with size too big
        f = open(TESTFN, "r+b")
        try:
            m = mmap.mmap(f.fileno(), mapsize+1)
        except ValueError:
            # we do not expect a ValueError on Windows
            # CAUTION:  This also changes the size of the file on disk, and
            # later tests assume that the length hasn't changed.  We need to
            # repair that.
            if hasattr(sys, 'getwindowsversion'):
                self.fail("Opening mmap with size+1 should work on Windows.")
        else:
            # we expect a ValueError on Unix, but not on Windows
            if not hasattr(sys, 'getwindowsversion'):
                self.fail("Opening mmap with size+1 should raise ValueError.")
            m.close()
        f.close()
        if hasattr(sys, 'getwindowsversion'):
            # Repair damage from the resizing test.
            del m, f
            force_gc_collect()
            f = open(TESTFN, 'r+b')
            f.truncate(mapsize)
            f.close()

        # Opening mmap with access=ACCESS_WRITE
        f = open(TESTFN, "r+b")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
        # Modifying write-through memory map
        m[:] = 'c'*mapsize
        self.assertEqual(m[:], 'c'*mapsize,
               "Write-through memory map memory not updated properly.")
        m.flush()
        m.close()
        f.close()
        f = open(TESTFN, 'rb')
        stuff = f.read()
        f.close()
        self.assertEqual(stuff, 'c'*mapsize,
               "Write-through memory map data file not updated properly.")

        # Opening mmap with access=ACCESS_COPY
        f = open(TESTFN, "r+b")
        m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
        # Modifying copy-on-write memory map
        m[:] = 'd'*mapsize
        self.assertEqual(m[:], 'd' * mapsize,
               "Copy-on-write memory map data not written correctly.")
        m.flush()
        with open(TESTFN, "rb") as f:
            self.assertEqual(f.read(), 'c'*mapsize,
                   "Copy-on-write test data file should not be modified.")
        # Ensuring copy-on-write maps cannot be resized
        self.assertRaises(TypeError, m.resize, 2*mapsize)
        f.close()
        del m, f

        # Ensuring invalid access parameter raises exception
        f = open(TESTFN, "r+b")
        self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
        f.close()

        if os.name == "posix":
            # Try incompatible flags, prot and access parameters.
            f = open(TESTFN, "r+b")
            self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
                              flags=mmap.MAP_PRIVATE,
                              prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
            f.close()
示例#32
0
 def tearDown(self):
     del self.netrc
     test_support.force_gc_collect(
         "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793"
     )
     os.unlink(temp_filename)
示例#33
0
 def remove(k):
     del data[k]
     test_support.force_gc_collect()
示例#34
0
 def tearDown(self):
     del sys.path[0]
     force_gc_collect("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793")
     shutil.rmtree(self.dirname)
示例#35
0
 def tearDown (self):
     del self.netrc
     test_support.force_gc_collect("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=321793")
     os.unlink(temp_filename)
示例#36
0
 def remove(k):
     del data[k]
     test_support.force_gc_collect()