Decompiling PYC file with Uncompyle6 getting AssertionError - python

I am trying to recover the original python scripts from a pyinstaller exe. I was able to extract the files from the exe and obtain the magic number. From what I read, the main .py file (found in the extract folder with no suffix) just needs to be renamed to main.pyc and the magic number appended should work. I followed these instructions and it worked, but my issue is the other .pyc files (found in the PYZ-00.pyz_extracted) all error out when I try to decompile them with uncompyle6 - even when I append the magic number. The error I'm getting is:
Unknown type 0
Traceback (most recent call last):
File "c:\users\hahenry\anaconda3\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\hahenry\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\hahenry\Anaconda3\Scripts\uncompyle6.exe_main_.py", line 7, in
File "c:\users\hahenry\anaconda3\lib\site-packages\uncompyle6\bin\uncompile.py", line 193, in main_bin
result = main(src_base, out_base, pyc_paths, source_paths, outfile,
File "c:\users\hahenry\anaconda3\lib\site-packages\uncompyle6\main.py", line 316, in main
deparsed = decompile_file(
File "c:\users\hahenry\anaconda3\lib\site-packages\uncompyle6\main.py", line 208, in decompile_file
decompile(
File "c:\users\hahenry\anaconda3\lib\site-packages\uncompyle6\main.py", line 83, in decompile
assert iscode(co)
AssertionError

Related

Python buildozer, ValueError

Good afternoon! I'm asking for help, I can't figure out python buildozer for a day, I've already prepared everything, and at the final stage, when the "buildozer android debug" command is entered into the terminal, it gives such an error at the end. Can anyone tell me what to do about it and how to fix it?
Here is the error itself:
Traceback (most recent call last):
File "/usr/local/bin/buildozer", line 11, in <module>
load_entry_point('buildozer==0.40.dev0', 'console_scripts', 'buildozer')()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/scripts/client.py", line 13, in main
Buildozer().run_command(sys.argv[1:])
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 1071, in run_command
self.target.run_commands(args)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/target.py", line 92, in run_commands
func(args)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/target.py", line 102, in cmd_debug
self.buildozer.prepare_for_build()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 178, in prepare_for_build
self.target.install_platform()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/targets/android.py", line 666, in install_platform
self._install_android_ndk()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/targets/android.py", line 459, in _install_android_ndk
cwd=self.buildozer.global_platform_dir)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 699, in download
urlretrieve(url, filename, report_hook)
File "/usr/lib/python3.6/urllib/request.py", line 1826, in retrieve
block = fp.read(bs)
File "/usr/lib/python3.6/tempfile.py", line 624, in func_wrapper
return func(*args, **kwargs)
ValueError: read of closed file
I tried to find an answer to this question, but so far without success.

WinError 32, Python, file used by another process

I have a script that is searching through files in a folder for multiple functions, one of which is verifying if they are valid. If they are corrupt, it moves them to another location. When trying to move the file, I get an error that it is being used by another process. I've looked through other similar questions, but none seem to resolve my issue.
path = filedialog.askdirectory()
pathbad = filedialog.askdirectory()
conv = os.path.basename(os.path.normpath(path))
bad = os.path.basename(os.path.normpath(pathbad))
jpg_list = []
badfiles = []
nojpg = []
cbz_list = [str(pp) for pp in path.glob("**/*.cbz")]
cbr_list = [str(pp) for pp in path.glob("**/*.cbr")]
file_list = cbr_list + cbz_list
for file in file_list:
if ('.cbr' in file) or ('.rar' in file):
try:
with rarfile.RarFile(file) as MyRar:
rarcontents = MyRar.namelist()
rarcontents = [x.lower() for x in rarcontents]
if any('.jpg' in s for s in rarcontents):
jpg_list.append(file)
else:
nojpg.append(file)
except (rarfile.BadRarFile):
badfiles.append(file)
except:
continue
if len(badfiles) > 0:
print('Moving ', len(badfiles), ' bad archives to "Bad Files":\n')
for zfile in badfiles:
nfile = zfile.replace(conv, bad)
shutil.move(zfile, nfile)
It errors on the shutil.move command. The file is actually copied to the new location, but not deleted from the old. I have verified that the file is not open in any application outside of python, and the only place it's open in python uses a with statement, which (as I understand it) closes the file when complete.
Edit:
Complete Stack Trace
File "C:\Program Files\Python39\lib\shutil.py", line 806, in move
os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Q:\\Download\\Comics\\Various Files\\Convert\\Poptropica 01.cbr' -> 'Q:\\Download\\Comics\\Various Files\\Bad Files\\Poptropica 01.cbr'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\azure\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\azure\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
run()
File "c:\Users\azure\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
File "C:\Program Files\Python39\lib\runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Program Files\Python39\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "i:\dev\comix\cbz_JPG-to-WEBP.py", line 138, in <module>
shutil.move(zfile, nfile)
File "C:\Program Files\Python39\lib\shutil.py", line 827, in move
os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Q:\\Download\\Comics\\Various Files\\Convert\\Poptropica 01.cbr'
If you are on windows open the task manager and close the python prosseses
Restart PC

problems with working compile and multiprocessing at the same time

i work in VSCode and when I run this file:
from multiprocessing import Process
def mp_setup_and_run(processes_num, *args):
processes = {}
for i in range(processes_num):
processes[i] = Process(
target=function_example,
args=args,
daemon=True,)
processes[i].start()
for i in range(processes_num):
processes[i].join()
def function_example(*data):
print(data)
if __name__ == "__main__":
compiled = compile("z**2 + c", "<string>", "eval")
mp_setup_and_run(3, compiled)
I get an exception/s:
PS C:\Python\projects\mondebrot_painter> cd 'c:\Python\projects\mondebrot_painter'; & 'C:\Program Files\Python38\python.exe' 'c:\Users\ASUS\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\lib\python\debugpy\no_wheels\debugpy\launcher' '51560' '--' 'c:\Python\projects\mondebrot_painter\test.py'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python38\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Program Files\Python38\lib\multiprocessing\spawn.py", line 126, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python38\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\ASUS\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\lib\python\debugpy\no_wheels\debugpy\__main__.py", line 45, in <module>
cli.main()
File "c:\Users\ASUS\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\lib\python\debugpy\no_wheels\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\ASUS\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\lib\python\debugpy\no_wheels\debugpy/..\debugpy\server\cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "C:\Program Files\Python38\lib\runpy.py", line 263, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Program Files\Python38\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Program Files\Python38\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Python\projects\mondebrot_painter\test.py", line 45, in <module>
result = mp_setup_and_run(3, compiled)
File "c:\Python\projects\mondebrot_painter\test.py", line 19, in mp_setup_and_run
processes[i].start()
File "C:\Program Files\Python38\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python38\lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python38\lib\multiprocessing\context.py", line 326, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python38\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
reduction.dump(process_obj, to_child)
File "C:\Program Files\Python38\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle 'code' object
and the debugger redirects me to the <string> file:
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(sys), STORE_NAME(sys), LOAD_NAME(sys.path), LOAD_METHOD(insert), LOAD_CONST(0), LOAD_CONST('c:\\Users\\ASUS\\.vscode\\extensions\\ms-python.python-2020.5.80290\\pythonFiles\\lib\\python\\debugpy\\no_wheels\\debugpy\\_vendored\\pydevd'), CALL_METHOD{2}, POP_TOP, LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(pydevd), STORE_NAME(pydevd), LOAD_CONST('http_json'), LOAD_NAME(pydevd.PydevdCustomization), STORE_ATTR(DEFAULT_PROTOCOL), LOAD_NAME(pydevd.settrace), LOAD_CONST('127.0.0.1'), LOAD_CONST(51592), LOAD_CONST(False), LOAD_CONST(False), LOAD_CONST(True), LOAD_CONST(None), LOAD_CONST('92e8bb604eeece436b2401def85a7ab95455e6c26fd9d660cb8175e691d71bd0'), LOAD_CONST('127.0.0.1'), LOAD_CONST('92e8bb604eeece436b2401def85a7ab95455e6c26fd9d660cb8175e691d71bd0'), LOAD_CONST(True), LOAD_CONST(True), LOAD_CONST(51592), LOAD_CONST(9040), LOAD_CONST(False), LOAD_CONST(('client', 'client-access-token', 'json-dap-http', 'multiprocess', 'port', 'ppid', 'server')), BUILD_CONST_KEY_MAP{7}, LOAD_CONST(('host', 'port', 'suspend', 'trace_only_current_thread', 'patch_multiprocessing', 'access_token', 'client_access_token', '__setup_holder__')), CALL_FUNCTION_KW{8}, POP_TOP, LOAD_CONST(0), LOAD_CONST(('spawn_main',)), IMPORT_NAME(multiprocessing.spawn), IMPORT_FROM(spawn_main), STORE_NAME(spawn_main), POP_TOP, LOAD_NAME(spawn_main), LOAD_CONST(9040), LOAD_CONST(892), LOAD_CONST(('parent_pid', 'pipe_handle')), CALL_FUNCTION_KW{2}, POP_TOP, return None
if I run the program from the console, I get this message:
C:\Python\projects\mondebrot_painter>python set_generator.py
Traceback (most recent call last):
File "set_generator.py", line 121, in <module>
set_ = mp_setup_and_run(senter, length, quality, processes_num, max_iter, compiled, mode)
File "set_generator.py", line 86, in mp_setup_and_run
processes[i].start()
File "C:\Program Files\Python38\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python38\lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python38\lib\multiprocessing\context.py", line 326, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python38\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
reduction.dump(process_obj, to_child)
File "C:\Program Files\Python38\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle 'code' object
C:\Python\projects\mondebrot_painter>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python38\lib\multiprocessing\spawn.py", line 107, in spawn_main
new_handle = reduction.duplicate(pipe_handle,
File "C:\Program Files\Python38\lib\multiprocessing\reduction.py", line 79, in duplicate
return _winapi.DuplicateHandle(
PermissionError: [WinError 5] Access Denied
I am somewhat lost and don’t understand what is happening and why I can't pass compiled.
If you simplify away the multiprocessing code and just use this from the console, you'll see the TypeError you are getting:
$ python
...
>>> compiled = compile("z**2 + c", "<string>", "eval")
>>> import pickle
>>> pickle.dumps(compiled)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't pickle code objects
This makes sense because the documentation tells us that pickle can handle:
None, True, and False
integers, floating point numbers, complex numbers
strings, bytes, bytearrays
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module (using def, not lambda)
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section Pickling Class Instances for details).
and compiled is not one of these.1
What's not said here, but is crucial to know, is that the multiprocessing module must be able to use the pickle code to serialize objects, so as to send them from one Python process to another. Since your compiled expression is not serializable, it cannot be sent from one Python process to another.
The trick is to serialize the expression, not the compiled expression. That is, instead of:
mp_setup_and_run(3, compiled)
use:
mp_setup_and_run(3, "z**2 + c")
Then, in mp_setup_and_run, have it pass the expression to the function. Have each function make its own call to compile. You'll do three separate compiles, in your three separate processes that run with the multiprocessing module, but that's OK.
1Of course, the documentation also says:
Attempts to pickle unpicklable objects will raise the PicklingError exception
when you and I both got TypeError instead. But this is the reason for the TypeError.

pyinstaller, file not found traceback error

I modified the .spec file with the following
`a.datas += [
('WORDS', '\words.txt', 'DATA'),
('CITIES', '\cities.txt', 'DATA'),
('ANIMALS', '/canimals.txt', 'DATA'),
]`
after a = Analysis(...). When i go to run pyinstaller on the spec file i get the following traceback error
`Cannot find ('ANIMALS', '/canimals.txt', 1, 'x')
Traceback (most recent call last):
File "c:\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python37\Scripts\pyinstaller.exe\__main__.py", line 9, in <module>
File "c:\python37\lib\site-packages\PyInstaller\__main__.py", line 111, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\python37\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\python37\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\python37\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
exec(code, spec_namespace)
File "hangman.spec", line 37, in <module>
console=True )
File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 424, in __init__
strip_binaries=self.strip, upx_binaries=self.upx,
File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 196, in __init__
self.__postinit__()
File "c:\python37\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
self.assemble()
File "c:\python37\lib\site-packages\PyInstaller\building\api.py", line 281, in assemble
pylib_name=pylib_name)
File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 334, in __init__
super(CArchiveWriter, self).__init__(archive_path, logical_toc)
File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 61, in __init__
self._add_from_table_of_contents(logical_toc)
File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 86, in _add_from_table_of_contents
self.add(toc_entry) # The guts of the archive.
File "c:\python37\lib\site-packages\PyInstaller\archive\writers.py", line 381, in add
fh = open(pathnm, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/canimals.txt'`
when its running for the other files in the a.datas it doesnt produce any errors, ive verified the existence of the 'canimals.txt' file and its there, and ive deleted and recreated the file and its still giving me the same error, i dont understand whats going on since the file clearly exists. im new with pyinstaller and i was following suggestions from my previous question and now ive seem to reach an impasse. What do i do?
edit: this is the code that queries the program to open the specified files
if decision == '1':
b = WordBank('words.txt')
bank = b.returnBank()
elif decision == '2':
b = WordBank('cities.txt')
bank = b.returnBank()
elif decision == '3':
b = WordBank('canimals.txt')
bank = b.returnBank()
the WordBank class is as follows
class WordBank:
def __init__(self, file):
file = open(file, 'r')
self.word_bank = file.readlines()
self.word_bank = ''.join(self.word_bank)
self.word_bank = self.word_bank.split('\n')
i get the error
Traceback (most recent call last):
file "hangman.py", line 212. in <module>
file "wordbank.py", line 3, in __init__
FileNotFoundError: [Errno 2] No such file or directory 'cities.txt'
[11452] failed to execute script hangman
do i have to specify the file path using the os module?
Your problem is evident from the error message:
FileNotFoundError: [Errno 2] No such file or directory: '/canimals.txt'`
You say that the file "canimals.txt" exists. But your code is not looking for a file named "canimals.txt". It's looking for a file named "/canimals.txt". That forward slash is your problem. I'm guessing that you're on Windows, where a forward slash is just an arbitrary character in a filename or directory name.
The problem is further made clear by the fact that you say the other two files work, and you have backslashes in front of their names rather than a forward slash. Turn the forward slash into a backslash, and I bet your code will work as you desire.
Also, you should be able to remove the backslashes from in front of the file names and have your code still work. A single backslash used that way is attempting to "escape" the character that follows it. But escaping regular letters just gives you the letter itself, so in this case, the backslashes aren't doing anything.

Python Error: Unexpected end of data while reading a .tar file

I'm trying to run TensorFlow's translate.py file, but after a few seconds already I keep getting this error:
tarfile.ReadError: unexpected end of data
I have no idea why this happens and I've tried several things, including downloading the needed data myself and comment the line in the code that do the downloading, but nothing worked.
Here is the site that I follow:
https://www.tensorflow.org/tutorials/seq2seq
And just in case you need everything, here are the lines from the command to the error:
C:\Users\Mueller>python translate.py
Preparing WMT data in /tmp
Extracting tar file /tmp\training-giga-fren.tar
Traceback (most recent call last):
File "translate.py", line 322, in <module>
tf.app.run()
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\site-package
s\tensorflow\python\platform\app.py", line 48, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "translate.py", line 319, in main
train()
File "translate.py", line 173, in train
FLAGS.data_dir, FLAGS.from_vocab_size, FLAGS.to_vocab_size)
File "C:\Users\Mueller\data_utils.py", line 267, in prepare_wmt_data
train_path = get_wmt_enfr_train_set(data_dir)
File "C:\Users\Mueller\data_utils.py", line 83, in get_wmt_enfr_train_set
corpus_tar.extractall(directory)
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\tarfile.py",
line 1996, in extractall
numeric_owner=numeric_owner)
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\tarfile.py",
line 2038, in extract
numeric_owner=numeric_owner)
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\tarfile.py",
line 2108, in _extract_member
self.makefile(tarinfo, targetpath)
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\tarfile.py",
line 2156, in makefile
copyfileobj(source, target, tarinfo.size, ReadError)
File "C:\Users\Mueller\AppData\Local\Programs\Python\Python35\lib\tarfile.py",
line 243, in copyfileobj
raise exception("unexpected end of data")
tarfile.ReadError: unexpected end of data
Does anybody know what is happening here?
If your machine is running behind a proxy server, its possible that the downloaded tar file maybe getting stripped/blocked by proxy server, resulting in corrupted downloaded file.

Categories

Resources