Beispiel #1
0
def test_php_escaping_double_quote():
    """Test the helper escaping funtions for 'double quotes'"""
    # Decoding - PHP -> Python
    assert php.phpdecode("'", quotechar='"') == "'"         # we do nothing with single quotes
    assert php.phpdecode(r"\n", quotechar='"') == "\n"      # See table of escaped characters
    assert php.phpdecode(r"\r", quotechar='"') == "\r"      # See table of escaped characters
    assert php.phpdecode(r"\t", quotechar='"') == "\t"      # See table of escaped characters
    assert php.phpdecode(r"\v", quotechar='"') == "\v"      # See table of escaped characters
    assert php.phpdecode(r"\f", quotechar='"') == "\f"      # See table of escaped characters
    assert php.phpdecode(r"\\", quotechar='"') == "\\"      # See table of escaped characters
    #assert php.phpdecode(r"\$", quotechar='"') == "$"      # See table of escaped characters - this may cause confusion with actual variables in roundtripping
    assert php.phpdecode(r"\$", quotechar='"') == "\\$"     # Just to check that we don't unescape this
    assert php.phpdecode(r'\"', quotechar='"') == '"'       # See table of escaped characters
    assert php.phpdecode(r'\117\143\164\141\154', quotechar='"') == 'Octal'       # Octal: \[0-7]{1,3}
    assert php.phpdecode(r'\x48\x65\x78', quotechar='"') == 'Hex'                 # Hex: \x[0-9A-Fa-f]{1,2}
    assert php.phpdecode(r'\117\\c\164\141\154', quotechar='"') == 'O\ctal'  # Mixed
    # Decoding - special examples
    assert php.phpdecode(r"Don't escape me here\'s", quotechar='"') == r"Don't escape me here\'s"  # See bug #589
    assert php.phpdecode("Line1\nLine2") == "Line1\nLine2"      # Preserve newlines in multiline messages
    assert php.phpdecode("Line1\r\nLine2") == "Line1\r\nLine2"  # DOS PHP files
    # Encoding - Python -> PHP
    assert php.phpencode("'", quotechar='"') == "'"
    assert php.phpencode("\n", quotechar='"') == "\n"       # See table of escaped characters - we leave newlines unescaped so that we can try best to preserve pretty printing. See bug 588
    assert php.phpencode("\r", quotechar='"') == r"\r"      # See table of escaped characters
    assert php.phpencode("\t", quotechar='"') == r"\t"      # See table of escaped characters
    assert php.phpencode("\v", quotechar='"') == r"\v"      # See table of escaped characters
    assert php.phpencode("\f", quotechar='"') == r"\f"      # See table of escaped characters
    assert php.phpencode(r"\\", quotechar='"') == r"\\"      # See table of escaped characters
    #assert php.phpencode("\$", quotechar='"') == "$"      # See table of escaped characters - this may cause confusion with actual variables in roundtripping
    assert php.phpencode("\$", quotechar='"') == r"\$"     # Just to check that we don't unescape this
    assert php.phpencode('"', quotechar='"') == r'\"'
    assert php.phpencode(r"Don't escape me here\'s", quotechar='"') == r"Don't escape me here\'s"  # See bug #589
Beispiel #2
0
def test_php_escaping_double_quote():
    """Test the helper escaping funtions for 'double quotes'"""
    # Decoding - PHP -> Python
    assert php.phpdecode(
        "'", quotechar='"') == "'"  # we do nothing with single quotes
    assert php.phpdecode(
        r"\n", quotechar='"') == "\n"  # See table of escaped characters
    assert php.phpdecode(
        r"\r", quotechar='"') == "\r"  # See table of escaped characters
    assert php.phpdecode(
        r"\t", quotechar='"') == "\t"  # See table of escaped characters
    assert php.phpdecode(
        r"\v", quotechar='"') == "\v"  # See table of escaped characters
    assert php.phpdecode(
        r"\f", quotechar='"') == "\f"  # See table of escaped characters
    assert php.phpdecode(
        r"\\", quotechar='"') == "\\"  # See table of escaped characters
    #assert php.phpdecode(r"\$", quotechar='"') == "$"      # See table of escaped characters - this may cause confusion with actual variables in roundtripping
    assert php.phpdecode(
        r"\$",
        quotechar='"') == "\\$"  # Just to check that we don't unescape this
    assert php.phpdecode(
        r'\"', quotechar='"') == '"'  # See table of escaped characters
    assert php.phpdecode(r'\117\143\164\141\154',
                         quotechar='"') == 'Octal'  # Octal: \[0-7]{1,3}
    assert php.phpdecode(r'\x48\x65\x78',
                         quotechar='"') == 'Hex'  # Hex: \x[0-9A-Fa-f]{1,2}
    assert php.phpdecode(r'\117\\c\164\141\154',
                         quotechar='"') == 'O\ctal'  # Mixed
    # Decoding - special examples
    assert php.phpdecode(
        r"Don't escape me here\'s",
        quotechar='"') == r"Don't escape me here\'s"  # See bug #589
    assert php.phpdecode(
        "Line1\nLine2"
    ) == "Line1\nLine2"  # Preserve newlines in multiline messages
    assert php.phpdecode("Line1\r\nLine2") == "Line1\r\nLine2"  # DOS PHP files
    # Encoding - Python -> PHP
    assert php.phpencode("'", quotechar='"') == "'"
    assert php.phpencode(
        "\n", quotechar='"'
    ) == "\n"  # See table of escaped characters - we leave newlines unescaped so that we can try best to preserve pretty printing. See bug 588
    assert php.phpencode(
        "\r", quotechar='"') == r"\r"  # See table of escaped characters
    assert php.phpencode(
        "\t", quotechar='"') == r"\t"  # See table of escaped characters
    assert php.phpencode(
        "\v", quotechar='"') == r"\v"  # See table of escaped characters
    assert php.phpencode(
        "\f", quotechar='"') == r"\f"  # See table of escaped characters
    assert php.phpencode(
        r"\\", quotechar='"') == r"\\"  # See table of escaped characters
    #assert php.phpencode("\$", quotechar='"') == "$"      # See table of escaped characters - this may cause confusion with actual variables in roundtripping
    assert php.phpencode(
        "\$",
        quotechar='"') == r"\$"  # Just to check that we don't unescape this
    assert php.phpencode('"', quotechar='"') == r'\"'
    assert php.phpencode(
        r"Don't escape me here\'s",
        quotechar='"') == r"Don't escape me here\'s"  # See bug #589
Beispiel #3
0
def test_php_escaping_single_quote():
    """
    Test the helper escaping funtions for 'single quotes'

    The tests are built mostly from examples from the PHP
    `string type definition <http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single>`_.
    """
    # Decoding - PHP -> Python
    assert php.phpdecode(
        r"\'"
    ) == r"'"  # To specify a literal single quote, escape it with a backslash (\).
    assert php.phpdecode(r'"') == r'"'
    assert php.phpdecode(
        r"\\'"
    ) == r"\'"  # To specify a literal backslash before a single quote, or at the end of the string, double it (\\)
    assert php.phpdecode(
        r"\x"
    ) == r"\x"  # Note that attempting to escape any other character will print the backslash too.
    assert php.phpdecode(r'\t') == r'\t'
    assert php.phpdecode(r'\n') == r'\n'
    assert php.phpdecode(
        r"this is a simple string") == r"this is a simple string"
    assert php.phpdecode("""You can also have embedded newlines in
strings this way as it is
okay to do""") == """You can also have embedded newlines in
strings this way as it is
okay to do"""
    assert php.phpdecode(r"This will not expand: \n a newline"
                         ) == r"This will not expand: \n a newline"
    assert php.phpdecode(r'Arnold once said: "I\'ll be back"'
                         ) == r'''Arnold once said: "I'll be back"'''
    assert php.phpdecode(r'You deleted C:\\*.*?') == r"You deleted C:\*.*?"
    assert php.phpdecode(r'You deleted C:\*.*?') == r"You deleted C:\*.*?"
    assert php.phpdecode(
        r'\117\143\164\141\154'
    ) == r'\117\143\164\141\154'  # We don't handle Octal like " does
    assert php.phpdecode(
        r'\x48\x65\x78') == r'\x48\x65\x78'  # Don't handle Hex either
    # Should implement for false interpretation of double quoted data.
    # Encoding - Python -> PHP
    assert php.phpencode(
        r"'"
    ) == r"\'"  # To specify a literal single quote, escape it with a backslash (\).
    assert php.phpencode(
        r"\'"
    ) == r"\\'"  # To specify a literal backslash before a single quote, or at the end of the string, double it (\\)
    assert php.phpencode(r'"') == r'"'
    assert php.phpencode(
        r"\x"
    ) == r"\x"  # Note that attempting to escape any other character will print the backslash too.
    assert php.phpencode(r"\t") == r"\t"
    assert php.phpencode(r"\n") == r"\n"
    assert php.phpencode(r"""String with
newline""") == r"""String with
newline"""
    assert php.phpencode(r"This will not expand: \n a newline"
                         ) == r"This will not expand: \n a newline"
    assert php.phpencode(r'''Arnold once said: "I'll be back"'''
                         ) == r'''Arnold once said: "I\'ll be back"'''
    assert php.phpencode(r'You deleted C:\*.*?') == r"You deleted C:\*.*?"
Beispiel #4
0
def test_php_escaping_single_quote():
    """Test the helper escaping funtions for 'single quotes'

    The tests are built mostly from examples from the PHP
    `string type definition <http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single>`_.
    """
    # Decoding - PHP -> Python
    assert php.phpdecode(r"\'") == r"'"  # To specify a literal single quote, escape it with a backslash (\).
    assert php.phpdecode(r'"') == r'"'
    assert (
        php.phpdecode(r"\\'") == r"\'"
    )  # To specify a literal backslash before a single quote, or at the end of the string, double it (\\)
    assert (
        php.phpdecode(r"\x") == r"\x"
    )  # Note that attempting to escape any other character will print the backslash too.
    assert php.phpdecode(r"\t") == r"\t"
    assert php.phpdecode(r"\n") == r"\n"
    assert php.phpdecode(r"this is a simple string") == r"this is a simple string"
    assert (
        php.phpdecode(
            """You can also have embedded newlines in
strings this way as it is
okay to do"""
        )
        == """You can also have embedded newlines in
strings this way as it is
okay to do"""
    )
    assert php.phpdecode(r"This will not expand: \n a newline") == r"This will not expand: \n a newline"
    assert php.phpdecode(r'Arnold once said: "I\'ll be back"') == r'''Arnold once said: "I'll be back"'''
    assert php.phpdecode(r"You deleted C:\\*.*?") == r"You deleted C:\*.*?"
    assert php.phpdecode(r"You deleted C:\*.*?") == r"You deleted C:\*.*?"
    assert php.phpdecode(r"\117\143\164\141\154") == r"\117\143\164\141\154"  # We don't handle Octal like " does
    assert php.phpdecode(r"\x48\x65\x78") == r"\x48\x65\x78"  # Don't handle Hex either
    # Should implement for false interpretation of double quoted data.
    # Encoding - Python -> PHP
    assert php.phpencode(r"'") == r"\'"  # To specify a literal single quote, escape it with a backslash (\).
    assert (
        php.phpencode(r"\'") == r"\\'"
    )  # To specify a literal backslash before a single quote, or at the end of the string, double it (\\)
    assert php.phpencode(r'"') == r'"'
    assert (
        php.phpencode(r"\x") == r"\x"
    )  # Note that attempting to escape any other character will print the backslash too.
    assert php.phpencode(r"\t") == r"\t"
    assert php.phpencode(r"\n") == r"\n"
    assert (
        php.phpencode(
            r"""String with
newline"""
        )
        == r"""String with
newline"""
    )
    assert php.phpencode(r"This will not expand: \n a newline") == r"This will not expand: \n a newline"
    assert php.phpencode(r'''Arnold once said: "I'll be back"''') == r'''Arnold once said: "I\'ll be back"'''
    assert php.phpencode(r"You deleted C:\*.*?") == r"You deleted C:\*.*?"
Beispiel #5
0
 def convertline(self, line):
     line = unicode(line, 'utf-8')
     returnline = ""
     # handle multiline msgid if we're in one
     if self.inmultilinemsgid:
         # see if there's more
         endpos = line.rfind("%s;" % self.quotechar)
         # if there was no '; or the quote is escaped, we have to continue
         if endpos >= 0 and line[endpos-1] != '\\':
             self.inmultilinemsgid = False
         # if we're echoing...
         if self.inecho:
             returnline = line
     # otherwise, this could be a comment
     elif line.strip()[:2] == '//' or line.strip()[:2] == '/*':
         returnline = quote.rstripeol(line)+eol
     else:
         line = quote.rstripeol(line)
         equalspos = line.find('=')
         hashpos = line.find("#")
         # if no equals, just repeat it
         if equalspos == -1:
             returnline = quote.rstripeol(line)+eol
         elif 0 <= hashpos < equalspos:
             # Assume that this is a '#' comment line
             returnline = quote.rstripeol(line)+eol
         # otherwise, this is a definition
         else:
             # now deal with the current string...
             key = line[:equalspos].strip()
             lookupkey = key.replace(" ", "")
             # Calculate space around the equal sign
             prespace = line[len(line[:equalspos].rstrip()):equalspos]
             postspacestart = len(line[equalspos+1:])
             postspaceend = len(line[equalspos+1:].lstrip())
             postspace = line[equalspos+1:equalspos+(postspacestart-postspaceend)+1]
             self.quotechar = line[equalspos+(postspacestart-postspaceend)+1]
             inlinecomment_pos = line.rfind("%s;" % self.quotechar)
             if inlinecomment_pos > -1:
                 inlinecomment = line[inlinecomment_pos+2:]
             else:
                 inlinecomment = ""
             if self.inputdict.has_key(lookupkey):
                 self.inecho = False
                 value = php.phpencode(self.inputdict[lookupkey], self.quotechar)
                 if isinstance(value, str):
                     value = value.decode('utf8')
                 returnline = key + prespace + "=" + postspace + self.quotechar + value + self.quotechar + ';' + inlinecomment + eol
             else:
                 self.inecho = True
                 returnline = line+eol
             # no string termination means carry string on to next line
             endpos = line.rfind("%s;" % self.quotechar)
             # if there was no '; or the quote is escaped, we have to continue
             if endpos == -1 or line[endpos-1] == '\\':
                 self.inmultilinemsgid = True
     if isinstance(returnline, unicode):
         returnline = returnline.encode('utf-8')
     return returnline
Beispiel #6
0
    def convertline(self, line):
        line = six.text_type(line, 'utf-8')
        returnline = ""

        # handle multiline msgid if we're in one
        if self.inmultilinemsgid:
            # see if there's more
            endpos = line.rfind("%s%s" % (self.quotechar, self.enddel))
            # if there was no '; or the quote is escaped, we have to continue
            if endpos >= 0 and line[endpos - 1] != '\\':
                self.inmultilinemsgid = False
            # if we're echoing...
            if self.inecho:
                returnline = line
        # otherwise, this could be a comment
        elif line.strip()[:2] == '//' or line.strip()[:2] == '/*':
            returnline = quote.rstripeol(line) + eol
        elif line.lower().replace(" ", "").find('array(') != -1:
            self.inarray = True
            self.prename = line[:line.find('=')].strip() + "->"
            self.equaldel = "=>"
            self.enddel = ","
            returnline = quote.rstripeol(line) + eol
        elif self.inarray and line.find(');') != -1:
            self.inarray = False
            self.equaldel = "="
            self.enddel = ";"
            self.prename = ""
            returnline = quote.rstripeol(line) + eol
        else:
            line = quote.rstripeol(line)
            equalspos = line.find(self.equaldel)
            hashpos = line.find("#")
            # if no equals, just repeat it
            if equalspos == -1:
                returnline = quote.rstripeol(line) + eol
            elif 0 <= hashpos < equalspos:
                # Assume that this is a '#' comment line
                returnline = quote.rstripeol(line) + eol

            # otherwise, this is a definition
            else:
                # now deal with the current string...
                key = line[:equalspos].rstrip()
                lookupkey = self.prename + key.lstrip()
                # Calculate space around the equal sign
                prespace = line[len(line[:equalspos].rstrip()):equalspos]
                postspacestart = len(line[equalspos + len(self.equaldel):])
                postspaceend = len(line[equalspos +
                                        len(self.equaldel):].lstrip())
                postspace = line[equalspos + len(self.equaldel):equalspos +
                                 (postspacestart - postspaceend) +
                                 len(self.equaldel)]
                self.quotechar = line[equalspos +
                                      (postspacestart - postspaceend) +
                                      len(self.equaldel)]
                inlinecomment_pos = line.rfind("%s%s" %
                                               (self.quotechar, self.enddel))
                if inlinecomment_pos > -1:
                    inlinecomment = line[inlinecomment_pos + 2:]
                else:
                    inlinecomment = ""

                if lookupkey in self.inputstore.locationindex:
                    unit = self.inputstore.locationindex[lookupkey]
                    if ((unit.isfuzzy() and not self.includefuzzy)
                            or len(unit.target) == 0):
                        value = unit.source
                    else:
                        value = unit.target

                    value = php.phpencode(value, self.quotechar)
                    self.inecho = False

                    if isinstance(value, bytes):
                        value = value.decode('utf8')

                    params = {
                        "key": key,
                        "pre": prespace,
                        "del": self.equaldel,
                        "post": postspace,
                        "quote": self.quotechar,
                        "value": value,
                        "enddel": self.enddel,
                        "comment": inlinecomment,
                        "eol": eol,
                    }
                    returnline = ("%(key)s%(pre)s%(del)s%(post)s%(quote)s"
                                  "%(value)s%(quote)s%(enddel)s%(comment)s"
                                  "%(eol)s" % params)
                else:
                    self.inecho = True
                    returnline = line + eol

                # no string termination means carry string on to next line
                endpos = line.rfind("%s%s" % (self.quotechar, self.enddel))
                # if there was no '; or the quote is escaped, we have to
                # continue
                if endpos == -1 or line[endpos - 1] == '\\':
                    self.inmultilinemsgid = True

        if isinstance(returnline, six.text_type):
            returnline = returnline.encode('utf-8')

        return returnline
Beispiel #7
0
 def convertline(self, line):
     line = unicode(line, 'utf-8')
     returnline = ""
     # handle multiline msgid if we're in one
     if self.inmultilinemsgid:
         # see if there's more
         endpos = line.rfind("%s%s" % (self.quotechar, self.enddel))
         # if there was no '; or the quote is escaped, we have to continue
         if endpos >= 0 and line[endpos-1] != '\\':
             self.inmultilinemsgid = False
         # if we're echoing...
         if self.inecho:
             returnline = line
     # otherwise, this could be a comment
     elif line.strip()[:2] == '//' or line.strip()[:2] == '/*':
         returnline = quote.rstripeol(line) + eol
     elif line.find('array(') != -1:
         self.inarray = True
         self.prename = line[:line.find('=')].strip() + "->"
         self.equaldel = "=>"
         self.enddel = ","
         returnline = quote.rstripeol(line) + eol
     elif self.inarray and line.find(');') != -1:
         self.inarray = False
         self.equaldel = "="
         self.enddel = ";"
         self.prename = ""
         returnline = quote.rstripeol(line) + eol
     else:
         line = quote.rstripeol(line)
         equalspos = line.find(self.equaldel)
         hashpos = line.find("#")
         # if no equals, just repeat it
         if equalspos == -1:
             returnline = quote.rstripeol(line) + eol
         elif 0 <= hashpos < equalspos:
             # Assume that this is a '#' comment line
             returnline = quote.rstripeol(line) + eol
         # otherwise, this is a definition
         else:
             # now deal with the current string...
             key = line[:equalspos].rstrip()
             lookupkey = self.prename + key.lstrip()
             # Calculate space around the equal sign
             prespace = line[len(line[:equalspos].rstrip()):equalspos]
             postspacestart = len(line[equalspos+len(self.equaldel):])
             postspaceend = len(line[equalspos+len(self.equaldel):].lstrip())
             postspace = line[equalspos+len(self.equaldel):equalspos+(postspacestart-postspaceend)+len(self.equaldel)]
             self.quotechar = line[equalspos+(postspacestart-postspaceend)+len(self.equaldel)]
             inlinecomment_pos = line.rfind("%s%s" % (self.quotechar,
                                                      self.enddel))
             if inlinecomment_pos > -1:
                 inlinecomment = line[inlinecomment_pos+2:]
             else:
                 inlinecomment = ""
             if lookupkey in self.inputstore.locationindex:
                 unit = self.inputstore.locationindex[lookupkey]
                 if (unit.isfuzzy() and not self.includefuzzy) or len(unit.target) == 0:
                     value = unit.source
                 else:
                     value = unit.target
                 value = php.phpencode(value, self.quotechar)
                 self.inecho = False
                 if isinstance(value, str):
                     value = value.decode('utf8')
                 returnline = "%(key)s%(pre)s%(del)s%(post)s%(quote)s%(value)s%(quote)s%(enddel)s%(comment)s%(eol)s" % {
                                  "key": key,
                                  "pre": prespace, "del": self.equaldel,
                                  "post": postspace,
                                  "quote": self.quotechar, "value": value,
                                  "enddel": self.enddel,
                                  "comment": inlinecomment, "eol": eol,
                               }
             else:
                 self.inecho = True
                 returnline = line + eol
             # no string termination means carry string on to next line
             endpos = line.rfind("%s%s" % (self.quotechar, self.enddel))
             # if there was no '; or the quote is escaped, we have to
             # continue
             if endpos == -1 or line[endpos-1] == '\\':
                 self.inmultilinemsgid = True
     if isinstance(returnline, unicode):
         returnline = returnline.encode('utf-8')
     return returnline