Skip to content

comment match mangles comment if it contains a ' character #240

Open

Description

@nickhuber
>>> import iptc
>>> rule = iptc.Rule()
>>> match = rule.create_match('comment')
>>> match.comment = "don't"
>>> match.comment
"don\\'t"

This makes the comment on the rule when it is added by don\'t instead of what I tried to set.

Activity

  1. ldx commented on May 24, 2018

    @ldx
    Owner

    The problem seems to be in the native library:

    void xtables_save_string(const char *value)
    {
    	static const char no_quote_chars[] = "_-0123456789"
    		"abcdefghijklmnopqrstuvwxyz"
    		"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	static const char escape_chars[] = "\"\\'";
    	size_t length;
    	const char *p;
    
    	length = strspn(value, no_quote_chars);
    	if (length > 0 && value[length] == 0) {
    		/* no quoting required */
    		putchar(' ');
    		fputs(value, stdout);
    	} else {
    		/* there is at least one dangerous character in the
    		   value, which we have to quote.  Write double quotes
    		   around the value and escape special characters with
    		   a backslash */
    		printf(" \"");
    
    		for (p = strpbrk(value, escape_chars); p != NULL;
    		     p = strpbrk(value, escape_chars)) {
    			if (p > value)
    				fwrite(value, 1, p - value, stdout);
    			putchar('\\');
    			putchar(*p);
    			value = p + 1;
    		}
    
    		/* print the rest and finish the double quoted
    		   string */
    		fputs(value, stdout);
    		putchar('\"');
    	}
    }
    

    It quotes the characters ", \ and '. Let me think about a workaround here.

  2. added a commit that references this issue on May 24, 2018
    6bb04f7
  3. ldx commented on May 24, 2018

    @ldx
    Owner

    I pushed a commit with a workaround. Feel free to give it a shot, though I'm still thinking whether this is the right way to work around the issue.

  4. multun commented on Oct 22, 2018

    @multun

    Parsing also fails when the comment contains an exclamation mark. How about using shlex instead of a regex ?

  5. ldx commented on Oct 23, 2018

    @ldx
    Owner

    @multun do you mean when parsing the output from save()?

  6. multun commented on Oct 26, 2018

    @multun

    @ldx yup

    res = re.findall(IPTCModule.pattern, buf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy