Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing PID to lockfile in Python 2 Fails #10

Open
badboybeyer opened this issue Nov 19, 2015 · 0 comments
Open

Writing PID to lockfile in Python 2 Fails #10

badboybeyer opened this issue Nov 19, 2015 · 0 comments

Comments

@badboybeyer
Copy link

The aquireLock method is failing to write it's PID to a new lockfile:

Traceback (most recent call last):

  File "build/bdist.linux-x86_64/egg/serial/serialutil.py", line 261, in __init_
_
  File "build/bdist.linux-x86_64/egg/serial/serialposix.py", line 366, in open
  File "build/bdist.linux-x86_64/egg/serial/serialposix.py", line 301, in acquir
eLock
TypeError: str() takes at most 1 argument (2 given)

The offending line is pid = bytes(str(os.getpid()) + "\n", encoding='UTF-8'). bytes is defined for python 2 in the serialutil file as bytes = str. The str function in python 2 does not support the encoding argument. I fixed this with an extra error check:

diff --git a/serial/serialposix.py b/serial/serialposix.py
index 2e82171..596dc71 100644
--- a/serial/serialposix.py
+++ b/serial/serialposix.py
@@ -298,7 +298,10 @@ def acquireLock(port, path, secondTry = False):
     else:
         try:
             fhLock = os.open(path, os.O_EXCL|os.O_CREAT|os.O_RDWR)
-            pid = bytes(str(os.getpid()) + "\n", encoding='UTF-8')
+            try:
+                pid = bytes(str(os.getpid()) + "\n", encoding='UTF-8')
+            except TypeError:
+                pid = bytes(str(os.getpid()) + "\n")
             os.write(fhLock,pid)
             os.close(fhLock)
             return True

I am certain this is the wrong solution. I believe that bytes in python2 should be emulated by the bytearray built-in. At least, that is the evolutionary path from the python3 documentation. Except, bytearray is redefined by a class in the serialutil file. I am unsure why a custom class was required to replace the bytearray built-in, so I defer to someone more experience with the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant