Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interaction of LOP with builtins is badly broken #31

Open
KOLANICH opened this issue May 17, 2019 · 5 comments
Open

Interaction of LOP with builtins is badly broken #31

KOLANICH opened this issue May 17, 2019 · 5 comments

Comments

@KOLANICH
Copy link

from lazy_object_proxy import Proxy
import os

def a():
	return "."

c = Proxy(a)
os.path.exists(c)
@ionelmc
Copy link
Owner

ionelmc commented May 18, 2019

Oooof ... looks like python2 os.stat will raise TypeError: coercing to Unicode: need string or buffer, Proxy found while on python3 you get a TypeError: 'str' object cannot be interpreted as an integer.

It would look like it fails at the argument parsing.

The problem is that the proxies (cext, simple and slots) all implement __index__ and it looks like stat tries to parse it as a fd (by converting to a number through __index__()).

Something like this would work with the pure python implementations but it's destructive you see ...

import os
from lazy_object_proxy.slots import Proxy

del Proxy.__index__  # DESTRUCTIVE

class PathProxy(Proxy):
    def __fspath__(self):
        return str(self)

os.stat(PathProxy(lambda: '.'))

@KOLANICH
Copy link
Author

Thank you for the info. Can it be solved by

  • annotating the callable with return type
  • making Proxy to use that type on order to determine the methods (and type) of the result prior actual calling the callable?

@ionelmc
Copy link
Owner

ionelmc commented May 19, 2019

Annotations wouldn't help at all.

os.stat's checks basically do this:

  • if it has __index__ then convert it to int using that
  • if it's a string then just use it
  • if it has __fspath__ method use that to get the string

Python is pretty broken with regard to capability checking (iow: checking if an object can do something or has a certain special method). While you can have a descriptor as a special method, and make it raise AttributeError most of the python code written in C will happily only check the existence of a slot for special method and forget about that silly descriptor stuff.

@ionelmc
Copy link
Owner

ionelmc commented Jun 12, 2020

FYI v1.5.0 adds support for __fspath__. Perhaps this is no longer a problem on latest Python?

@KOLANICH
Copy link
Author

Will try, thank you for getting me know. Though I am not sure if it will work for the versions of python not checking __fspath__ (i.e. 3.4 that is the latest version supporting Win XP).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants