git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 00/11] git-p4.py: Cast byte strings to unicode strings in python3

From
Ben Keene <seraphire@gmail.com>
Date
Dec 5, 2019, 16:16 UTC
Message-ID
<be2a6839-aa73-dbf8-de19-823d3ae5265a@gmail.com>
In-Reply-To
<CAE5ih7-6EbEM4z5BtY87=82H_tLypiOPq4WY5mm3190QExTZWQ@mail.gmail.com>
On 12/5/2019 4:54 AM, Luke Diamand wrote:
Show 66 quoted lines
> On Wed, 4 Dec 2019 at 22:29, Ben Keene via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>> Issue: The current git-p4.py script does not work with python3.
>>
>> I have attempted to use the P4 integration built into GIT and I was unable
>> to get the program to run because I have Python 3.8 installed on my
>> computer. I was able to get the program to run when I downgraded my python
>> to version 2.7. However, python 2 is reaching its end of life.
>>
>> Submission: I am submitting a patch for the git-p4.py script that partially
>> supports python 3.8. This code was able to pass the basic tests (t9800) when
>> run against Python3. This provides basic functionality.
>>
>> In an attempt to pass the t9822 P4 path-encoding test, a new parameter for
>> git P4 Clone was introduced.
>>
>> --encoding Format-identifier
>>
>> This will create the GIT repository following the current functionality;
>> however, before importing the files from P4, it will set the
>> git-p4.pathEncoding option so any files or paths that are encoded with
>> non-ASCII/non-UTF-8 formats will import correctly.
>>
>> Technical details: The script was updated by futurize (
>> https://python-future.org/futurize.html) to support Py2/Py3 syntax. The few
>> references to classes in future were reworked so that future would not be
>> required. The existing code test for Unicode support was extended to
>> normalize the classes “unicode” and “bytes” to across platforms:
>>
>>   * ‘unicode’ is an alias for ‘str’ in Py3 and is the unicode class in Py2.
>>   * ‘bytes’ is bytes in Py3 and an alias for ‘str’ in Py2.
>>
>> New coercion methods were written for both Python2 and Python3:
>>
>>   * as_string(text) – In Python3, this encodes a bytes object as a UTF-8
>>     encoded Unicode string.
>>   * as_bytes(text) – In Python3, this decodes a Unicode string to an array of
>>     bytes.
>>
>> In Python2, these functions do not change the data since a ‘str’ object
>> function in both roles as strings and byte arrays. This reduces the
>> potential impact on backward compatibility with Python 2.
>>
>>   * to_unicode(text) – ensures that the supplied data is encoded as a UTF-8
>>     string. This function will encode data in both Python2 and Python3. *
>>        path_as_string(path) – This function is an extension function that
>>        honors the option “git-p4.pathEncoding” to convert a set of bytes or
>>        characters to UTF-8. If the str/bytes cannot decode as ASCII, it will
>>        use the encodeWithUTF8() method to convert the custom encoded bytes to
>>        Unicode in UTF-8.
>>
>>
>>
>> Generally speaking, information in the script is converted to Unicode as
>> early as possible and converted back to a byte array just before passing to
>> external programs or files. The exception to this rule is P4 Repository file
>> paths.
>>
>> Paths are not converted but left as “bytes” so the original file path
>> encoding can be preserved. This formatting is required for commands that
>> interact with the P4 file path. When the file path is used by GIT, it is
>> converted with encodeWithUTF8().
>>
> Almost all the tests pass now - nice!
>
> (There's one test that fails for me, t9830-git-p4-symlink-dir.sh).

Which version of Python are running the failing test against?  I run it against Python 2.7 and it passes the test. I don't expect all Python 3.x tests to pass yet, just t9800.

Show 5 quoted lines
>
> Nitpicking:
>
> - There are some bits of trailing whitespace around - can you strip
> those out? You can use "git diff --check".

Is there a way that I can find out which branches I need to remove white space from now that they have been committed?

> - Also I think the convention for git commits is that they be limited
> to 72 (?) characters.
I'm going through all my commits and fixing them.
> - In 10dc commit message, s/behvior/behavior
> - Maybe submit 4fc4 as a separate patch series? It doesn't seem
> directly related to your python3 changes.
I moved the enhancements to https://github.com/git/git/pull/675
Show 19 quoted lines
> - s/howerver/however/
>
> The comment at line 3261 (showing the fast-import syntax) has wonky
> indentation, and needs a space after the '#'.
>
> This code looked like we're duplicating stuff:
>
> +    if isinstance(path, unicode):
> +        path = path.replace("%", "%25") \
> +                   .replace("*", "%2A") \
> +                   .replace("#", "%23") \
> +                   .replace("@", "%40")
> +    else:
> +        path = path.replace(b"%", b"%25") \
> +                   .replace(b"*", b"%2A") \
> +                   .replace(b"#", b"%23") \
> +                   .replace(b"@", b"%40")
>
> I wonder if we can have a helper to do this?

I was just looking at this code block, and at this time, I'm not sure if the text coming in will be Unicode or bytes, so I'm hesitant to change it until more of the code is converted, but I understand about the duplication.

Show 19 quoted lines
>
> In patchRCSKeywords() you've added code to cleanup outFile. But I
> wonder if we could just use a 'finally' block, or a contextexpr ("with
> blah as outFile:")
>
> I don't know if it's worth doing now that you've got it going, but at
> one point I tried simplifying code like this:
>
>     path_as_string(file['depotFile'])
> and
>     marshalled[b'data']
>
> by using a dictionary with overloaded operators which would do the
> bytes/string conversion automatically. However, your approach isn't
> actually _that_ invasive, so maybe this is not necessary.
>
> Looks good though, thanks!
> Luke
>

I toyed with making a class object that would hold the path data and have methods to cast to bytes and encodeWithUTF8() and Unicode versions, but it quickly got out of hand.

Previous: Luke DiamandNext: Denton Liu
Message 44 of 77 in “git-p4.py: Cast byte strings to unicode strings in python3”
  1. 0/1 git-p4.py: Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Nov 13, 2019
  2. 1/1 Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Nov 13, 2019
  3. Junio C HamanoNov 14, 2019
  4. Luke DiamandNov 14, 2019
  5. 0/3 git-p4.py: Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Nov 15, 2019
  6. 1/3 Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Nov 15, 2019
  7. 2/3 FIX: cast as unicode fails when a value is already unicodeBen Keene via GitGitGadget, Nov 15, 2019
  8. 3/3 FIX: wrap return for read_pipe_lines in ustring() and wrap GitLFS read of the pointer file in ustring()Ben Keene via GitGitGadget, Nov 15, 2019
  9. 0/1 git-p4.py: Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Dec 2, 2019
  10. 1/1 Python3 support for t9800 tests. Basic P4/Python3 supportBen Keene via GitGitGadget, Dec 2, 2019
  11. Denton LiuDec 3, 2019
  12. Ben KeeneDec 3, 2019
  13. Denton LiuDec 4, 2019
  14. 00/11 git-p4.py: Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Dec 4, 2019
  15. 01/11 git-p4: select p4 binary by operating-systemBen Keene via GitGitGadget, Dec 4, 2019
  16. Denton LiuDec 5, 2019
  17. Ben KeeneDec 5, 2019
  18. 02/11 git-p4: change the expansion test from basestring to listBen Keene via GitGitGadget, Dec 4, 2019
  19. Denton LiuDec 5, 2019
  20. Ben KeeneDec 5, 2019
  21. 03/11 git-p4: add new helper functions for python3 conversionBen Keene via GitGitGadget, Dec 4, 2019
  22. Denton LiuDec 5, 2019
  23. Ben KeeneDec 5, 2019
  24. 05/11 git-p4: Add new functions in preparation of usageBen Keene via GitGitGadget, Dec 4, 2019
  25. Denton LiuDec 5, 2019
  26. Ben KeeneDec 5, 2019
  27. 04/11 git-p4: python3 syntax changesBen Keene via GitGitGadget, Dec 4, 2019
  28. Denton LiuDec 5, 2019
  29. 08/11 git-p4: p4CmdList - support Unicode encodingBen Keene via GitGitGadget, Dec 4, 2019
  30. Junio C HamanoDec 5, 2019
  31. Ben KeeneDec 5, 2019
  32. 07/11 git-p4: Add a helper class for stream writingBen Keene via GitGitGadget, Dec 4, 2019
  33. Junio C HamanoDec 5, 2019
  34. Ben KeeneDec 5, 2019
  35. 06/11 git-p4: Fix assumed path separators to be more Windows friendlyBen Keene via GitGitGadget, Dec 4, 2019
  36. Junio C HamanoDec 5, 2019
  37. Ben KeeneDec 5, 2019
  38. 09/11 git-p4: Add usability enhancementsBen Keene via GitGitGadget, Dec 4, 2019
  39. Junio C HamanoDec 5, 2019
  40. Ben KeeneDec 5, 2019
  41. 10/11 git-p4: Support python3 for basic P4 clone, sync, and submitBen Keene via GitGitGadget, Dec 4, 2019
  42. 11/11 git-p4: Added --encoding parameter to p4 cloneBen Keene via GitGitGadget, Dec 4, 2019
  43. Luke DiamandDec 5, 2019
  44. Ben KeeneDec 5, 2019
  45. Denton LiuDec 5, 2019
  46. Ben KeeneDec 5, 2019
  47. 00/15 git-p4.py: Cast byte strings to unicode strings in python3Ben Keene via GitGitGadget, Dec 7, 2019
  48. 01/15 t/gitweb-lib.sh: drop confusing quotesJeff King via GitGitGadget, Dec 7, 2019
  49. 02/15 t/gitweb-lib.sh: set $REQUEST_URIJeff King via GitGitGadget, Dec 7, 2019
  50. 03/15 git-p4: select P4 binary by operating-systemBen Keene via GitGitGadget, Dec 7, 2019
  51. Junio C HamanoDec 9, 2019
  52. 05/15 git-p4: promote encodeWithUTF8() to a global functionBen Keene via GitGitGadget, Dec 7, 2019
  53. Junio C HamanoDec 11, 2019
  54. 04/15 git-p4: change the expansion test from basestring to listBen Keene via GitGitGadget, Dec 7, 2019
  55. Junio C HamanoDec 9, 2019
  56. Ben KeeneDec 13, 2019
  57. 06/15 git-p4: remove p4_write_pipe() and write_pipe() return valuesBen Keene via GitGitGadget, Dec 7, 2019
  58. 11/15 git-p4: add Py23File() - helper class for stream writingBen Keene via GitGitGadget, Dec 7, 2019
  59. 08/15 git-p4: add casting helper functions for python 3 conversionBen Keene via GitGitGadget, Dec 7, 2019
  60. 09/15 git-p4: python 3 syntax changesBen Keene via GitGitGadget, Dec 7, 2019
  61. 10/15 git-p4: fix assumed path separators to be more Windows friendlyBen Keene via GitGitGadget, Dec 7, 2019
  62. 15/15 git-p4: Add depot manipulation functionsBen Keene via GitGitGadget, Dec 7, 2019
  63. 14/15 git-p4: added --encoding parameter to p4 cloneBen Keene via GitGitGadget, Dec 7, 2019
  64. 07/15 git-p4: add new support function gitConfigSet()Ben Keene via GitGitGadget, Dec 7, 2019
  65. Junio C HamanoDec 11, 2019
  66. 13/15 git-p4: support Python 3 for basic P4 clone, sync, and submit (t9800)Ben Keene via GitGitGadget, Dec 7, 2019
  67. 12/15 git-p4: p4CmdList - support Unicode encodingBen Keene via GitGitGadget, Dec 7, 2019
  68. Jeff KingDec 7, 2019
  69. Ben KeeneDec 7, 2019
  70. Junio C HamanoDec 11, 2019
  71. Denton LiuDec 11, 2019
  72. Junio C HamanoDec 11, 2019
  73. Luke DiamandDec 11, 2019
  74. Junio C HamanoDec 11, 2019
  75. Yang ZhaoDec 11, 2019
  76. Ben KeeneDec 12, 2019
  77. Ben KeeneDec 13, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.