Skip to content

Writing PID to lockfile in Python 2 Fails #10

@badboybeyer

Description

@badboybeyer

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions