Ticket #5129: py3-execfile-5129.apply-to-r33601.diff

File py3-execfile-5129.apply-to-r33601.diff, 3.0 KB (added by allenap, 4 years ago)

Like compat3k.execfile.5.diff, but can be applied to branches/py3-execfile-5129.

  • twisted/test/test_compat.py

     
    206206    Tests for the Python 3-friendly L{execfile} implementation.
    207207    """
    208208
    209     def setUp(self):
    210         self.script = FilePath(self.mktemp())
    211         self.script.setContent("foo += 1\n")
     209    def writeScript(self, content):
     210        """
     211        Write L{content} to a new temporary file, returning the L{FilePath}
     212        for the new file.
     213        """
     214        script = FilePath(self.mktemp())
     215        script.setContent(content.encode("ascii"))
     216        return script
    212217
    213218
    214219    def test_execfileGlobals(self):
    215220        """
    216221        L{execfile} executes the specified file in the given global namespace.
    217222        """
     223        script = self.writeScript("foo += 1\n")
    218224        globalNamespace = {"foo": 1}
    219         execfile(self.script.path, globalNamespace)
     225        execfile(script.path, globalNamespace)
    220226        self.assertEqual(2, globalNamespace["foo"])
    221227
    222228
     
    225231        L{execfile} executes the specified file in the given global and local
    226232        namespaces.
    227233        """
     234        script = self.writeScript("foo += 1\n")
    228235        globalNamespace = {"foo": 10}
    229236        localNamespace = {"foo": 20}
    230         execfile(self.script.path, globalNamespace, localNamespace)
     237        execfile(script.path, globalNamespace, localNamespace)
    231238        self.assertEqual(10, globalNamespace["foo"])
    232239        self.assertEqual(21, localNamespace["foo"])
     240
     241
     242    def test_execfileUniversalNewlines(self):
     243        """
     244        L{execfile} reads in the specified file using universal newlines so
     245        that scripts written on one platform will work on another.
     246        """
     247        for lineEnding in "\n", "\r", "\r\n":
     248            script = self.writeScript("foo = 'okay'" + lineEnding)
     249            globalNamespace = {"foo": None}
     250            execfile(script.path, globalNamespace)
     251            self.assertEqual("okay", globalNamespace["foo"])
  • twisted/python/compat.py

     
    191191    """
    192192    if locals is None:
    193193        locals = globals
    194     fin = open(filename, "rb")
     194    fin = open(filename, "rbU")
    195195    try:
    196196        source = fin.read()
    197197    finally:
  • twisted/python/test/test_dist.py

     
    7979    def writeSetup(self, name, *path):
    8080        """
    8181        Write out a C{setup.py} file to a location determined by
    82         L{self.basedir} and L{path}. L{self.setup_template} is used to
     82        L{self.basedir} and L{path}. L{self.setupTemplate} is used to
    8383        generate its contents.
    8484        """
    8585        outdir = self.basedir.descendant(path)