Re: os.system vs subrocess.call

2019-11-28 Thread Stephan Lukits


> On 28. Nov 2019, at 12:05, Ulrich Goebel  wrote:
> 
> Hi,
> 
> I have to call commands from inside a python skript. These commands are in 
> fact other python scripts. So I made
> 
>os.system('\.Test.py')
> 
> That works.
> 
> Now I tried to use
> 
>supprocess.call(['.\', 'test.py'])

[ins] In [1]: from os import system

[ins] In [2]: system('./test.py')
hallo world
Out[2]: 0

[ins] In [3]: from subprocess import call

[ins] In [4]: call('./test.py')
hallo world
Out[4]: 0

In the first call you call ’.Test.py’
In the second call you call ’test.py’

“supprocess” doesn’t exist

How about

subprocess.call(‘\.Test.py’)

Or

subprocess.call([‘\.Test.py’])

Whereas the later makes more sense if you want to pass arguments to Test.py

Greetings Stephan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sandboxing eval() (was: Calculator)

2020-01-20 Thread Stephan Lukits



> On 19. Jan 2020, at 19:35, mus...@posteo.org wrote:
> 
> Is it actually possible to build a "sandbox" around eval, permitting it
> only to do some arithmetic and use some math functions, but no
> filesystem acces or module imports?
> 
> I have an application that loads calculation recipes (a few lines of
> variable assignments and arithmetic) from a database. 
> 
> exec(string, globals, locals)
> 
> with locals containing the input variables, and globals has a
> __builtin__ object with a few math functions. It works, but is it safe?

https://github.com/danthedeckie/simpleeval

Might be a good starting point.

Greetings Stephan
-- 
https://mail.python.org/mailman/listinfo/python-list


dynamic import of dynamically created modules failes

2020-03-31 Thread Stephan Lukits

Hello,

background:

- a daemon creates package p1 (e.g. directory with __init__.py-file) and 
in p1 a module m1 is created.


- Then the daemon wants to import from m1, which functions (so far all 
the time).


- Then a module m2 is created in p1 and the daemon wants to import from 
m2 which fails (most of the time but *not* always) with ModuleNotFoundError.


See the little test script at the end which reproduces the problem.

The very strange thing for me is, that the import of m2 in the test 
script sometimes works.  I wonder if there is some 
package-content-caching.  And if so how to prevent or re-trigger it.


(neither a reload of p1 nor a deletion of p1 from sys.modules does the 
trick)



-test-script---

"""

(python --version: Python 3.8.2)

Dynamic import of dynamically created modules right after creation.
"""

from shutil import rmtree
from os import path, chdir, mkdir, listdir
from importlib import import_module, reload
import sys

# for test-package creation
P1 = 'p1'
INIT = '__init__.py'
INIT_CONTENT = """\
# -*- coding: utf-8 -*-
"""

# for first test-module creation
M1 = 'm1'
M1_CONTENT = """\
# -*- coding: utf-8 -*-

answer = 42
"""

# for second test-module creation
M2 = 'm2'
M2_CONTENT = """\
# -*- coding: utf-8 -*-

hello = 'world'
"""

chdir(path.dirname(__file__))    # make sure we are in the right directory

if path.isdir(P1):
    rmtree(P1)   # always start off under the same 
conditions



mkdir(P1)    # create test-package and first test-module
with open(path.join(P1, INIT), 'w') as f:
    f.write(INIT_CONTENT)
with open(path.join(P1, M1+'.py'), 'w') as f:
    f.write(M1_CONTENT)

# import from the just created module; this worked always so far
from p1.m1 import answer

print(f'{answer=}')

with open(path.join(P1, M2+'.py'), 'w') as f:
    f.write(M2_CONTENT)  # create the second test-module

# check current directory, file and module structure
print('wd-content:', ', '.join(listdir()))
print('p1-content:', ', '.join(listdir(P1)))
print('p1-modlues:', ', '.join([m for m in sys.modules if 
m.startswith('p1')]))

# reload(sys.modules['p1'])  # neither a reload
# del sys.modules['p1']  # nor a deletion of p1 does the trick

# here it most of the time fails (but NOT all the time)
# so far if it fails it fails in all three variants
# so far if it works the 'from ...'-import works already
try:
    from p1.m2 import hello
except ModuleNotFoundError:
    try:
    hello = getattr(import_module('p1.m2'), 'hello')
    except ModuleNotFoundError:
    try:
    hello = getattr(__import__('p1.m2', fromlist=[None]), 'hello')
    except ModuleNotFoundError:
    raise
    else:
    print("__import__-import worked")
    else:
    print("import_module-import worked")
else:
    print("'from ... '-import worked")

print(f'{hello=}')

--
https://mail.python.org/mailman/listinfo/python-list


Re: dynamic import of dynamically created modules failes

2020-03-31 Thread Stephan Lukits


On 3/31/20 9:01 PM, Pieter van Oostrum wrote:

"Dieter Maurer"  writes:


Stephan Lukits wrote at 2020-3-31 17:44 +0300:

background:

- a daemon creates package p1 (e.g. directory with __init__.py-file) and
in p1 a module m1 is created.

- Then the daemon wants to import from m1, which functions (so far all
the time).

- Then a module m2 is created in p1 and the daemon wants to import from
m2 which fails (most of the time but *not* always) with ModuleNotFoundError.

See the little test script at the end which reproduces the problem.
...

I remember a similar report (some time ago). There, caching has
been responsible. I have forgotten how to invalidate the caches.
But, you could try logic found in
`importlib._bootstrap_external.PathFinder.invalidate_caches`.

The first import creates a file __pycache__ in the directory p1.
To remove it use rmtree(path.join(P1,'__pycache__'))
Then the second import will succeed.



Thank you, but the behavior doesn't change.  I added


rmtree(path.join(P1, '__pycache__'))


Before the import:


try:
    from p1.m2 import hello


and still get (please note that there isn't a __pycache__ directory 
anymore):


answer=42
wd-content: __init__.py, p1
p1-content: __init__.py, __pycache__, m1.py, m2.py
p1-modlues: p1, p1.m1
p1-content after rmtree: __init__.py, m1.py, m2.py
Traceback (most recent call last):
  File "dynamic_modules/__init__.py", line 68, in 
    from p1.m2 import hello
ModuleNotFoundError: No module named 'p1.m2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dynamic_modules/__init__.py", line 71, in 
    hello = getattr(import_module('p1.m2'), 'hello')
  File 
"/home/goedel/.pyenv/versions/3.8.2/lib/python3.8/importlib/__init__.py", 
line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1014, in _gcd_import
  File "", line 991, in _find_and_load
  File "", line 973, in 
_find_and_load_unlocked

ModuleNotFoundError: No module named 'p1.m2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dynamic_modules/__init__.py", line 74, in 
    hello = getattr(__import__('p1.m2', fromlist=[None]), 'hello')
ModuleNotFoundError: No module named 'p1.m2'

--
https://mail.python.org/mailman/listinfo/python-list


[Solved] Re: dynamic import of dynamically created modules failes

2020-03-31 Thread Stephan Lukits



On 3/31/20 8:00 PM, Dieter Maurer wrote:

Stephan Lukits wrote at 2020-3-31 17:44 +0300:

background:

- a daemon creates package p1 (e.g. directory with __init__.py-file) and
in p1 a module m1 is created.

- Then the daemon wants to import from m1, which functions (so far all
the time).

- Then a module m2 is created in p1 and the daemon wants to import from
m2 which fails (most of the time but *not* always) with ModuleNotFoundError.

See the little test script at the end which reproduces the problem.
...

I remember a similar report (some time ago). There, caching has
been responsible. I have forgotten how to invalidate the caches.
But, you could try logic found in
`importlib._bootstrap_external.PathFinder.invalidate_caches`.


Yes, removing the entry for p1 from 'sys.path_importer_cache' seems to 
solve the problem.


Thank you.



--
https://mail.python.org/mailman/listinfo/python-list


Re: Typing modules

2020-07-20 Thread Stephan Lukits



> On 20 Jul 2020, at 16:31, Jonathan Gossage  wrote:
> 
> I have the following code and I would like to type the variable *contents*:
> 
> contents: something = importlib._import_module(name)
> 
> 
> I have been unable to find out what *something* should be.

types.ModuleType

> 
> -- 
> Jonathan Gossage
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list