On 2020/05/10 1:24, Daniel Shahaf wrote:
> Yasuhito FUTATSUKI wrote on Fri, 08 May 2020 20:55 +0900:
>> On 2020/05/08 2:46, Daniel Shahaf wrote:
>>> Yasuhito FUTATSUKI wrote on Thu, 07 May 2020 20:46 +0900:  
>>>> I think it is need to escape characters in char *value when we print  
>>                                ^some (not all)
>>>> them as Python's str value. The patch below may work for this purpose,
>>>> but I want someone to write more nice code :)  
>>>
>>> How about simply adding the human-readable value in a comment? —  
>>
>> It's very nice. One of the reason I don't like my code is just
>> readability of the value of "value".
> 
> Sure, in general it's nice for protocols and serialization formats to
> be texty, in order for them to be human-readable and -writable.  On
> this instance, however, generating Python string literals that are both
> correct and human-readable seems to me like it'd be an effort spent for
> little gain.  (I think there's a good chance that no one will _ever_
> run entries-dump by hand again once it properly supports Python 3.)
> 
> One easy way to make the output nicer is to name the lambda function.

Yes, it's also one of the reasons that it uses lambda function.
I use it only to reduce the occurence of 'value' in

    e.name = value if isinstance(value, str) else value.decode()

without using temporary named object, in the first patch.

Then I updated the patch. Not to use lambda function, I added
a method to set str attribute from bytes object to "Entry" class,
and move its definition to the output of entries-dump to show
what it does.

Cheers,
-- 
Yasuhito FUTATSUKI <futat...@poem.co.jp>/<futat...@yf.bsdclub.org>
Index: subversion/tests/cmdline/entries-dump.c
===================================================================
--- subversion/tests/cmdline/entries-dump.c     (revision 1877407)
+++ subversion/tests/cmdline/entries-dump.c     (working copy)
@@ -23,6 +23,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
 
 #include <apr_pools.h>
 #include <apr_general.h>
@@ -34,6 +35,7 @@
 #include "svn_pools.h"
 #include "svn_wc.h"
 #include "svn_dirent_uri.h"
+#include "svn_xml.h"
 
 #include "private/svn_wc_private.h"
 
@@ -41,12 +43,41 @@
 #include "../../libsvn_wc/lock.h"
 
 static void
-str_value(const char *name, const char *value)
+print_prefix()
 {
+  puts("class Entry(object):\n"
+       "  \"Object represents pre-1.6 svn_wc_* entries "
+       "used by entries-dump helper\"\n"
+       "  if b'' == '':\n"
+       "    def set_strval(self, name, val):\n"
+       "      self.__setattr__(name, val)\n"
+       "  else:\n"
+       "    def set_strval(self, name, val):\n"
+       "      self.__setattr__(name, val.decode('utf-8', 'surrogateescape'))");
+}
+
+static void
+str_value(const char *name, const char *value, apr_pool_t *pool)
+{
   if (value == NULL)
     printf("e.%s = None\n", name);
   else
-    printf("e.%s = '%s'\n", name, value);
+    {
+      svn_stringbuf_t *escaped_value = NULL;
+      svn_xml_escape_attr_cstring(&escaped_value, value, pool);
+
+      /* Print the human-readable value. */
+      assert(NULL == strchr(escaped_value->data, '\n'));
+      printf("# e.%s = %s\n", name, escaped_value->data);
+
+      /* Print the machine-readable value. */
+      printf("e.set_strval('%s', b'", name);
+      while(*value)
+        {
+          printf("\\x%02x", (unsigned int)(unsigned char)*value++);
+        }      
+      printf("')\n");
+    }
 }
 
 
@@ -130,11 +161,11 @@
       SVN_ERR_ASSERT(strcmp(key, entry->name) == 0);
 
       printf("e = Entry()\n");
-      str_value("name", entry->name);
+      str_value("name", entry->name, pool);
       int_value("revision", entry->revision);
-      str_value("url", entry->url);
-      str_value("repos", entry->repos);
-      str_value("uuid", entry->uuid);
+      str_value("url", entry->url, pool);
+      str_value("repos", entry->repos, pool);
+      str_value("uuid", entry->uuid, pool);
       int_value("kind", entry->kind);
       int_value("schedule", entry->schedule);
       bool_value("copied", entry->copied);
@@ -141,27 +172,27 @@
       bool_value("deleted", entry->deleted);
       bool_value("absent", entry->absent);
       bool_value("incomplete", entry->incomplete);
-      str_value("copyfrom_url", entry->copyfrom_url);
+      str_value("copyfrom_url", entry->copyfrom_url, pool);
       int_value("copyfrom_rev", entry->copyfrom_rev);
-      str_value("conflict_old", entry->conflict_old);
-      str_value("conflict_new", entry->conflict_new);
-      str_value("conflict_wrk", entry->conflict_wrk);
-      str_value("prejfile", entry->prejfile);
+      str_value("conflict_old", entry->conflict_old, pool);
+      str_value("conflict_new", entry->conflict_new, pool);
+      str_value("conflict_wrk", entry->conflict_wrk, pool);
+      str_value("prejfile", entry->prejfile, pool);
       /* skip: text_time */
       /* skip: prop_time */
       /* skip: checksum */
       int_value("cmt_rev", entry->cmt_rev);
       /* skip: cmt_date */
-      str_value("cmt_author", entry->cmt_author);
-      str_value("lock_token", entry->lock_token);
-      str_value("lock_owner", entry->lock_owner);
-      str_value("lock_comment", entry->lock_comment);
+      str_value("cmt_author", entry->cmt_author, pool);
+      str_value("lock_token", entry->lock_token, pool);
+      str_value("lock_owner", entry->lock_owner, pool);
+      str_value("lock_comment", entry->lock_comment, pool);
       /* skip: lock_creation_date */
       /* skip: has_props */
       /* skip: has_prop_mods */
       /* skip: cachable_props */
       /* skip: present_props */
-      str_value("changelist", entry->changelist);
+      str_value("changelist", entry->changelist, pool);
       /* skip: working_size */
       /* skip: keep_local */
       int_value("depth", entry->depth);
@@ -384,15 +415,25 @@
     cmd = NULL;
 
   if (!cmd || !strcmp(cmd, "--entries"))
-    err = entries_dump(path, NULL, pool);
+    {
+      print_prefix();
+      err = entries_dump(path, NULL, pool);
+    }
   else if (!strcmp(cmd, "--subdirs"))
-    err = directory_dump(path, pool);
+    {
+      err = directory_dump(path, pool);
+    }
   else if (!strcmp(cmd, "--tree-dump"))
-    err = tree_dump(path, pool);
+    {
+      print_prefix();
+      err = tree_dump(path, pool);
+    }
   else
-    err = svn_error_createf(SVN_ERR_INCORRECT_PARAMS, NULL,
-                            "Invalid command '%s'",
-                            cmd);
+    {
+      err = svn_error_createf(SVN_ERR_INCORRECT_PARAMS, NULL,
+                              "Invalid command '%s'",
+                              cmd);
+    }
   if (err)
     {
       svn_handle_error2(err, stderr, FALSE, "entries-dump: ");
Index: subversion/tests/cmdline/svntest/main.py
===================================================================
--- subversion/tests/cmdline/svntest/main.py    (revision 1877407)
+++ subversion/tests/cmdline/svntest/main.py    (working copy)
@@ -902,8 +902,6 @@
     ### report on this? or continue to just skip it?
     return None
 
-  class Entry(object):
-    pass
   entries = { }
   exec(''.join(filter_dbg(stdout_lines)))
   return entries
@@ -927,8 +925,6 @@
     ### report on this? or continue to just skip it?
     return None
 
-  class Entry(object):
-    pass
   dirs = { }
   exec(''.join(filter_dbg(stdout_lines)))
   return dirs

Reply via email to