This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add unittest support for pyc projects
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: unittest fails with "Start directory is not importable" when trying to run sourceless tests
View: 26859
Assigned To: Nosy List: brgirgis, eric.araujo, ezio.melotti, martin.panter, michael.foord, r.david.murray, rbcollins
Priority: normal Keywords:

Created on 2017-12-23 21:26 by brgirgis, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4998 closed brgirgis, 2017-12-23 21:26
Messages (10)
msg308970 - (view) Author: Bassem Girgis (brgirgis) * Date: 2017-12-23 21:26
This PR makes it possible to "unittest" projects that are all pyc with no __init__.py which is hardcoded in few checks in unittest.loader
msg308972 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2017-12-23 23:02
Could you tell more about these projects?  In current Python 3 pyc files are in a __pycache__ sub-directory, not directly in the package dir; what tool produces «pyc projects»?
msg309001 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-24 16:19
Éric: in python3 if no .py is found but a .pyc is found where the .py is expected, it will be imported and run.  This supports sourceless package distributions, which is something we only sorta-support :)

The change looks simple enough that it might be worth doing, but I'll leave that to the unittest maintainers.  If accepted there should be comments as to why a .pyc is being checked for.
msg309040 - (view) Author: Bassem Girgis (brgirgis) * Date: 2017-12-25 18:10
Hi Eric,

Yes it is like David said. For the projects you don't distribute the code
with the deployment package, you end up distributing only pyc files. As of
how to get these files, you can make use of py_compile or compileall
modules. For compileall, however, you have to pass the legacy flag to avoid
the __pycache__ standard.

What I found strange in the unittest code while trying to resolve this
issue is that, it is relying on hardcoded searches for __init__.py to find
if a module is loadable. I would have imagined that there is a standard why
to answer this question.

Best regards,
Bassem

Bassem Girgis, PhD

Email: brgirgis@gmail.com

On Sun, Dec 24, 2017 at 10:53 AM, Serhiy Storchaka <report@bugs.python.org>
wrote:

>
> Change by Serhiy Storchaka <storchaka+cpython@gmail.com>:
>
>
> ----------
> stage:  -> test needed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue32419>
> _______________________________________
>
msg309041 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-25 18:14
There may be now, but I don't think there was when unittest was written.  Also, if someone decided to use namespace packages for tests for some reason, the current check would also probably fail, so it may be worth looking for (or creating if needed) a standard way to check via importlib.
msg314746 - (view) Author: Bassem Girgis (brgirgis) * Date: 2018-03-31 18:18
Please let me know what I need to do in order to close this issue and finally merge the code.
msg314753 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2018-04-01 01:51
Whats the use for *unittest* - a tool to help folk develop - to run a test which is only present in sourceless form?
msg314754 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2018-04-01 01:52
Oh, and why look for __init__ - in part, because it predates namespace packages, but also because unlike regular imports unittest will do negative things like reading the entire filesystem otherwise.
msg314757 - (view) Author: Bassem Girgis (brgirgis) * Date: 2018-04-01 03:16
I Robert,

One use scenario is to run deployment tests for sourceless packages and to
ensure the integrity of the production environment. Very useful in
container environments!

In regards to the looking for the __init__ file, it is obvious that the
logic you mentioned is the reason behind that. However, if the unittest
code used a standard way to figure out "loadable" packages, whatever that
is, it would have captured the fact that python3 can indeed function with
all pyc files.

I would say the fix in here is a dirty one. A better way to solve it is to
call a standard utility which can detect if a package is loadable. This
would resolve this issue and help with any future changes in the standard
as well.

Best regards,
Bassem

Bassem Girgis, PhD

Email: brgirgis@gmail.com

On Sat, Mar 31, 2018 at 8:52 PM, Robert Collins <report@bugs.python.org>
wrote:

>
> Robert Collins <robertc@robertcollins.net> added the comment:
>
> Oh, and why look for __init__ - in part, because it predates namespace
> packages, but also because unlike regular imports unittest will do negative
> things like reading the entire filesystem otherwise.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue32419>
> _______________________________________
>
msg332767 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-12-30 22:11
Seems to be a lot of overlap with Issue 26859 by Xavier. Looks like Xavier included a test case, but Bassemʼs changes on Git Hub seem more thorough in the impementation and doc strings.
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76600
2019-03-29 06:37:48methanesetstatus: open -> closed
superseder: unittest fails with "Start directory is not importable" when trying to run sourceless tests
resolution: duplicate
stage: test needed -> resolved
2018-12-30 22:11:17martin.pantersetnosy: + martin.panter
messages: + msg332767
2018-04-01 03:16:19brgirgissetmessages: + msg314757
2018-04-01 01:52:42rbcollinssetmessages: + msg314754
2018-04-01 01:51:24rbcollinssetmessages: + msg314753
2018-03-31 18:18:46brgirgissetmessages: + msg314746
2017-12-25 18:14:35r.david.murraysetmessages: + msg309041
2017-12-25 18:10:03brgirgissetmessages: + msg309040
2017-12-24 16:53:05serhiy.storchakasetstage: test needed
2017-12-24 16:19:28r.david.murraysetnosy: + r.david.murray
messages: + msg309001
2017-12-24 08:30:38serhiy.storchakasetnosy: + rbcollins, ezio.melotti, michael.foord

components: + Library (Lib), - Extension Modules
versions: - Python 3.6, Python 3.8
2017-12-23 23:02:37eric.araujosetnosy: + eric.araujo
messages: + msg308972
2017-12-23 21:26:00brgirgiscreate