简体   繁体   English

Python 3.4和较旧的Python脚本(用于Blender中,TypeErrors)

[英]Python 3.4 with an older Python script for use in Blender, TypeErrors

I've converted a older Python script with the Python 2to3 tool to make it compatible with Blender. 我已经使用Python 2to3工具转换了较旧的Python脚本,以使其与Blender兼容。 The script makes communication possible between Blender3D and a OMRON PLC. 该脚本使Blender3D和OMRON PLC之间的通信成为可能。 However after conversion the script gives the following error in the Blender 3D console: 但是,转换后,脚本在Blender 3D控制台中给出以下错误:

TypeError: 'str' does not support the buffer interface

This occurs in the following lines: 这发生在以下几行中:

def _send(self,  raw):
    self.sock.send( raw)
    #print ' Send:' + repr(raw)
def _recieve(self):
    pr = self.sock.recv(8)
    length = binstr2int( pr[4:8])
    r = pr + self.sock.recv( length)
    #print ' Recv:' + repr(r)
    return r

I've searched the problem using google and it seems its a problem that occurs when you convert or use an older script for Python 3+. 我已经使用Google搜索了问题,当您将其转换为Python 3+或使用较旧的脚本时,似乎出现了此问题。 Adding .encode() and .decode() to the objects where the problems occur has not helped either. 将.encode()和.decode()添加到发生问题的对象也没有帮助。

def _send(self,  raw):
    self.sock.send( raw.encode())
    #print ' Send:' + repr(raw)
def _recieve(self):
    pr = self.sock.recv(8).decode()
    length = binstr2int( pr[4:8])
    r = pr + self.sock.recv( length)
    #print ' Recv:' + repr(r)
    return r

This gives a different kind of error: 这给出了另一种错误:

TypeError: can't convert 'bytes' to str implicitly

The script was not made by me, I'm simply using it for a school project to make communication possible between software (Blender3D) and hardware (PLC). 该脚本不是我制作的,我只是在学校项目中使用它,以使软件(Blender3D)和硬件(PLC)之间的通信成为可能。

Help would be much appreciated, thanks. 帮助将不胜感激,谢谢。

I've put the converted version of the script here. 我将转换后的脚本版本放在这里。

  1. Bytes-only operations like self._send(c1.raw) needs to be encoded correctly. 诸如self._send(c1.raw)类的仅字节操作需要正确编码。 Use either self._send(bytes(c1.raw, "utf-8")) or self._send(c1.raw.encode("utf-8")) 使用self._send(bytes(c1.raw, "utf-8"))self._send(c1.raw.encode("utf-8"))

    However, since your classes like FinsTCPframe is actually operating bytes stream instead of unicode literals, I'd suggest to just stick to bytes (append b in front of any affected string constants, like b'' instead of '' 但是,由于FinsTCPframe类的类FinsTCPframe是在操作字节流而不是unicode文字,因此建议您坚持使用字节(将b附加在任何受影响的字符串常量之前,例如b''而不是''

  2. ord() around an item of a bytes instance (for example, if s is a bytes instance, s[i] is the item) should be removed, since retrieving one element from bytes in Python 3.x is already a int instance. 应该删除bytes实例某个项目周围的ord() (例如,如果s是一个bytes实例,则s[i]是该项目),因为从Python 3.x中的字节中检索一个元素已经是一个int实例。 If you are not 100% sure what's inside your ord() call, try this (dirty) workaround: 如果您不能100%确定ord()调用中包含的内容,请尝试以下解决方法:

    safe_ord = lambda c: ord(c) if isinstance(c, str) else c

    And replace corresponding ord() with safe_ord() 并将对应的ord()替换为safe_ord()

  3. chr() in python 3 returns a str , but if you need bytes instead, try this: python 3中的chr()返回str ,但是如果您需要bytes ,请尝试以下操作:

    bytes_chr = lambda x: bytes([x]) (from this stackoverflow question) bytes_chr = lambda x: bytes([x]) (来自此stackoverflow问题)

    And replace corresponding chr() with bytes_chr() 并将相应的chr()替换为bytes_chr()

  4. Division / is resulting in a float by default, so if you want to retain the old behavior, double the symbol: // 默认情况下,除法/会导致浮点运算,因此,如果要保留旧的行为,请将该符号加倍: //

A full list of porting guide can be found on the official python docs site , consult this if you need to do the porting work in the future :) 移植指南的完整列表可以在官方python docs网站上找到 ,如果将来需要进行移植工作,请查阅此指南:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM