# [PATCH 1/2] git-p4 tests: cd to testdir before running python

5 messages from 2016-04-23 to 2016-04-26. Participants: Luke Diamand, Junio C Hamano.
Thread: https://gitlist.dev/t/42121

## Luke Diamand, 2016-04-23 14:13

Subject: [PATCH 0/2] git-p4: support python3 in the tests
Message-ID: <1461420826-4416-1-git-send-email-luke@diamand.org>
URL: https://gitlist.dev/e/1461420826-4416-1-git-send-email-luke%40diamand.org

```
This patchset updates the git-p4 tests so that they work with
either Python2 or Python3.

Note that this does *not* fix git-p4 to work with Python3 - that's
a much bigger challenge.

Luke Diamand (2):
  git-p4 tests: cd to testdir before running python
  git-p4 tests: work with python3 as well as python2

 t/lib-git-p4.sh            | 7 ++++---
 t/t9802-git-p4-filetype.sh | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)

-- 
2.8.1.218.gd2cea43.dirty

```

## Luke Diamand, 2016-04-23 14:13

Subject: [PATCH 1/2] git-p4 tests: cd to testdir before running python
Message-ID: <1461420826-4416-2-git-send-email-luke@diamand.org>
URL: https://gitlist.dev/e/1461420826-4416-2-git-send-email-luke%40diamand.org
In-Reply-To: <1461420826-4416-1-git-send-email-luke@diamand.org>

```
The python one-liner for getting the current time prints out
error messages if the current directory is deleted while it is
running if using python3.

Avoid these messages by switching back to the test directory
before running python3, instead of remaining in the trash
directory.

Signed-off-by: Luke Diamand <luke@diamand.org>
---
 t/lib-git-p4.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/lib-git-p4.sh b/t/lib-git-p4.sh
index f9ae1d7..77802fe 100644
--- a/t/lib-git-p4.sh
+++ b/t/lib-git-p4.sh
@@ -50,7 +50,7 @@ native_path() {
 # at runtime (e.g. via NTP). The 'clock_gettime(CLOCK_MONOTONIC)'
 # function could fix that but it is not in Python until 3.3.
 time_in_seconds() {
-	python -c 'import time; print int(time.time())'
+	(cd "$TEST_DIRECTORY" && python -c 'import time; print(int(time.time()))')
 }
 
 # Try to pick a unique port: guess a large number, then hope
-- 
2.8.1.218.gd2cea43.dirty

```

## Luke Diamand, 2016-04-23 14:13

Subject: [PATCH 2/2] git-p4 tests: work with python3 as well as python2
Message-ID: <1461420826-4416-3-git-send-email-luke@diamand.org>
URL: https://gitlist.dev/e/1461420826-4416-3-git-send-email-luke%40diamand.org
In-Reply-To: <1461420826-4416-1-git-send-email-luke@diamand.org>

```
Update the git-p4 tests so that they work with both
Python2 and Python3.

We have to be explicit about the difference between
Unicode text strings (Python3 default) and raw binary
strings which will be exchanged with Perforce.

Additionally, print always takes braces in Python3.

Signed-off-by: Luke Diamand <luke@diamand.org>
---
 t/lib-git-p4.sh            | 5 +++--
 t/t9802-git-p4-filetype.sh | 6 +++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/t/lib-git-p4.sh b/t/lib-git-p4.sh
index 77802fe..b97d27c 100644
--- a/t/lib-git-p4.sh
+++ b/t/lib-git-p4.sh
@@ -198,9 +198,10 @@ marshal_dump() {
 	cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
 	import marshal
 	import sys
+	instream = getattr(sys.stdin, 'buffer', sys.stdin)
 	for i in range($line):
-	    d = marshal.load(sys.stdin)
-	print d['$what']
+	    d = marshal.load(instream)
+	print(d[b'$what'].decode('utf-8'))
 	EOF
 	"$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
 }
diff --git a/t/t9802-git-p4-filetype.sh b/t/t9802-git-p4-filetype.sh
index 66d3fc9..eb9a8ed 100755
--- a/t/t9802-git-p4-filetype.sh
+++ b/t/t9802-git-p4-filetype.sh
@@ -223,12 +223,12 @@ build_gendouble() {
 	import sys
 	import struct
 
-	s = struct.pack(">LL18s",
+	s = struct.pack(b">LL18s",
 			0x00051607,  # AppleDouble
 			0x00020000,  # version 2
-			""           # pad to 26 bytes
+			b""          # pad to 26 bytes
 	)
-	sys.stdout.write(s)
+	getattr(sys.stdout, 'buffer', sys.stdout).write(s)
 	EOF
 }
 
-- 
2.8.1.218.gd2cea43.dirty

```

## Junio C Hamano, 2016-04-25 22:07

Subject: Re: [PATCH 0/2] git-p4: support python3 in the tests
Message-ID: <xmqqzishibpz.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqzishibpz.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <1461420826-4416-1-git-send-email-luke@diamand.org>

```
Luke Diamand <luke@diamand.org> writes:

> This patchset updates the git-p4 tests so that they work with
> either Python2 or Python3.
>
> Note that this does *not* fix git-p4 to work with Python3 - that's
> a much bigger challenge.

We use Python outside p4 tests (e.g. remote-svn test), and the way
they invoke the interpreter is to say "$PYTHON_PATH" and avoid
saying "python" which picks whatever random version of Python
interpreter happens to be the first on $PATH.  Shouldn't the tests
touched by this series be doing the same?


>
> Luke Diamand (2):
>   git-p4 tests: cd to testdir before running python
>   git-p4 tests: work with python3 as well as python2
>
>  t/lib-git-p4.sh            | 7 ++++---
>  t/t9802-git-p4-filetype.sh | 6 +++---
>  2 files changed, 7 insertions(+), 6 deletions(-)

```

## Luke Diamand, 2016-04-26 07:27

Subject: Re: [PATCH 0/2] git-p4: support python3 in the tests
Message-ID: <CAE5ih7_MzKWG+=PDMW8a5uJ28uGGnvh-25-3b4bgnO3VdBiyLA@mail.gmail.com>
URL: https://gitlist.dev/e/CAE5ih7_MzKWG%2B%3DPDMW8a5uJ28uGGnvh-25-3b4bgnO3VdBiyLA%40mail.gmail.com
In-Reply-To: <xmqqzishibpz.fsf@gitster.mtv.corp.google.com>

```
On 25 April 2016 at 23:07, Junio C Hamano <gitster@pobox.com> wrote:
> Luke Diamand <luke@diamand.org> writes:
>
>> This patchset updates the git-p4 tests so that they work with
>> either Python2 or Python3.
>>
>> Note that this does *not* fix git-p4 to work with Python3 - that's
>> a much bigger challenge.
>
> We use Python outside p4 tests (e.g. remote-svn test), and the way
> they invoke the interpreter is to say "$PYTHON_PATH" and avoid
> saying "python" which picks whatever random version of Python
> interpreter happens to be the first on $PATH.  Shouldn't the tests
> touched by this series be doing the same?

Yes, they should. I'll update them accordingly.

But the real reason for doing this is that at some point, git-p4 has
to start working with python3, since python2 is going away (albeit not
until 2020).

Luke

```
